Skip to content

Commit ebe2a96

Browse files
committed
Swift: Assign indexes to extensions looking at all the extensions of a type
1 parent 463f79b commit ebe2a96

File tree

2 files changed

+9
-25
lines changed

2 files changed

+9
-25
lines changed

swift/extractor/mangler/SwiftMangler.cpp

Lines changed: 8 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -98,46 +98,31 @@ SwiftMangledName SwiftMangler::visitExtensionDecl(const swift::ExtensionDecl* de
9898
return {};
9999
}
100100

101-
auto parent = getParent(decl);
102101
auto target = decl->getExtendedType();
103-
return initMangled(decl) << fetch(target) << getExtensionIndex(decl, parent);
102+
return initMangled(decl) << fetch(target) << getExtensionIndex(decl);
104103
}
105104

106-
unsigned SwiftMangler::getExtensionIndex(const swift::ExtensionDecl* decl,
107-
const swift::Decl* parent) {
105+
unsigned SwiftMangler::getExtensionIndex(const swift::ExtensionDecl* decl) {
108106
// to avoid iterating multiple times on the parent of multiple extensions, we preload extension
109107
// indexes once for each encountered parent into the `preloadedExtensionIndexes` mapping.
110108
// Because we mangle declarations only once in a given trap/dispatcher context, we can safely
111109
// discard preloaded indexes on use
112110
if (auto found = preloadedExtensionIndexes.extract(decl)) {
113111
return found.mapped();
114112
}
115-
if (auto parentModule = llvm::dyn_cast<swift::ModuleDecl>(parent)) {
116-
llvm::SmallVector<swift::Decl*> siblings;
117-
parentModule->getTopLevelDecls(siblings);
118-
indexExtensions(siblings);
119-
} else if (auto iterableParent = llvm::dyn_cast<swift::IterableDeclContext>(parent)) {
120-
indexExtensions(iterableParent->getAllMembers());
121-
} else {
122-
// TODO use a generic logging handle for Swift entities here, once it's available
123-
CODEQL_ASSERT(false, "non-local context must be module or iterable decl context");
113+
for (const auto& sibling : decl->getExtendedNominal()->getExtensions()) {
114+
auto index = 0u;
115+
if (sibling->getDeclContext() == decl->getDeclContext()) {
116+
preloadedExtensionIndexes.emplace(sibling, index);
117+
index++;
118+
}
124119
}
125120
auto found = preloadedExtensionIndexes.extract(decl);
126121
// TODO use a generic logging handle for Swift entities here, once it's available
127122
CODEQL_ASSERT(found, "extension not found within parent");
128123
return found.mapped();
129124
}
130125

131-
void SwiftMangler::indexExtensions(llvm::ArrayRef<swift::Decl*> siblings) {
132-
auto index = 0u;
133-
for (auto sibling : siblings) {
134-
if (sibling->getKind() == swift::DeclKind::Extension) {
135-
preloadedExtensionIndexes.emplace(sibling, index);
136-
}
137-
++index;
138-
}
139-
}
140-
141126
SwiftMangledName SwiftMangler::visitGenericTypeParamDecl(const swift::GenericTypeParamDecl* decl) {
142127
auto ret = visitValueDecl(decl, /*force=*/true);
143128
if (decl->isParameterPack()) {

swift/extractor/mangler/SwiftMangler.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,8 +112,7 @@ class SwiftMangler : private swift::TypeVisitor<SwiftMangler, SwiftMangledName>,
112112
virtual SwiftMangledName fetch(const swift::TypeBase* type) = 0;
113113
SwiftMangledName fetch(swift::Type type) { return fetch(type.getPointer()); }
114114

115-
void indexExtensions(llvm::ArrayRef<swift::Decl*> siblings);
116-
unsigned int getExtensionIndex(const swift::ExtensionDecl* decl, const swift::Decl* parent);
115+
unsigned int getExtensionIndex(const swift::ExtensionDecl* decl);
117116
static SwiftMangledName initMangled(const swift::TypeBase* type);
118117
SwiftMangledName initMangled(const swift::Decl* decl);
119118
SwiftMangledName visitTypeDiscriminatedValueDecl(const swift::ValueDecl* decl);

0 commit comments

Comments
 (0)