Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 0aed7d5

Browse files
committed
Swift: more mangling
1 parent 2592129 commit 0aed7d5

12 files changed

Lines changed: 77 additions & 99 deletions

File tree

swift/extractor/SwiftExtractor.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,14 @@ static std::string mangledDeclName(const swift::ValueDecl& decl) {
6161
// prefix adds a couple of special symbols, we don't necessary need them
6262
return mangler.mangleEntity(&decl);
6363
}
64+
if (decl.getKind() == swift::DeclKind::GenericTypeParam) {
65+
// internal mangling does not distinguish generic type parameters with the same name and
66+
// position of different functions. We prepend the context (that is, the function) to
67+
// circumvent that
68+
auto context = llvm::dyn_cast<swift::ValueDecl>(decl.getDeclContext()->getAsDecl());
69+
assert(context);
70+
return mangledDeclName(*context) + '_' + mangler.mangleAnyDecl(&decl, /* prefix = */ false);
71+
}
6472
return mangler.mangleAnyDecl(&decl, /* prefix = */ false);
6573
}
6674

@@ -71,6 +79,7 @@ static fs::path getFilename(swift::ModuleDecl& module,
7179
return resolvePath(primaryFile->getFilename());
7280
}
7381
if (lazyDeclaration) {
82+
<<<<<<< HEAD
7483
// this code will be thrown away in the near future
7584
auto decl = llvm::dyn_cast<swift::ValueDecl>(lazyDeclaration);
7685
assert(decl);
@@ -85,6 +94,11 @@ static fs::path getFilename(swift::ModuleDecl& module,
8594
// half a SHA2 is enough
8695
ret += std::string_view(mangled).substr(0, mangled.size() / 2);
8796
return ret;
97+
=======
98+
// static int i;
99+
// return mangledDeclName(*lazyDeclaration) + std::to_string(i++);
100+
return mangledDeclName(*lazyDeclaration);
101+
>>>>>>> 4cbad80695 (Swift: more mangling)
88102
}
89103
// PCM clang module
90104
if (module.isNonSwiftModule()) {

swift/extractor/infra/SwiftDispatcher.h

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -153,15 +153,6 @@ class SwiftDispatcher {
153153
auto& stored = store[e];
154154
if (!stored.valid()) {
155155
auto inserted = fetching.insert(e);
156-
if (!inserted.second) {
157-
if constexpr (IsTypePointer<E>) {
158-
std::string dump;
159-
llvm::raw_string_ostream oss{dump};
160-
e->dump(oss);
161-
emitDebugInfo(dump);
162-
}
163-
return undefined_label;
164-
}
165156
assert(inserted.second && "detected infinite fetchLabel recursion");
166157
stored = createLabel(e, type);
167158
fetching.erase(e);

swift/extractor/infra/SwiftMangledName.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ struct SwiftMangledName {
2121
// streaming labels or ints into a SwiftMangledName just appends them
2222
template <typename Tag>
2323
SwiftMangledName& operator<<(TrapLabel<Tag> label) {
24+
assert(label && "using undefined label in mangled name");
2425
parts.emplace_back(label);
2526
return *this;
2627
}

swift/extractor/mangler/SwiftMangler.cpp

Lines changed: 45 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,8 @@ SwiftMangledName SwiftMangler::visitModuleDecl(const swift::ModuleDecl* decl) {
5252
return ret;
5353
}
5454

55-
SwiftMangledName SwiftMangler::visitValueDecl(const swift::ValueDecl* decl) {
56-
if (!decl->hasName() || decl->getDeclContext()->isLocalContext()) {
55+
SwiftMangledName SwiftMangler::visitValueDecl(const swift::ValueDecl* decl, bool force) {
56+
if (!force && (!decl->hasName() || decl->getDeclContext()->isLocalContext())) {
5757
return {};
5858
}
5959
auto ret = initMangled(decl);
@@ -91,8 +91,12 @@ SwiftMangledName SwiftMangler::visitExtensionDecl(const swift::ExtensionDecl* de
9191
if (decl->getDeclContext()->isLocalContext()) {
9292
return {};
9393
}
94-
auto ret = initMangled(decl);
9594
auto extended = decl->getExtendedNominal();
95+
if (!extended) {
96+
// may happen in incomplete ASTs
97+
return {};
98+
}
99+
auto ret = initMangled(decl);
96100
ret << dispatcher.fetchLabel(extended);
97101
// get the index of all extensions of the same nominal type within this decl's module
98102
auto index = 0u;
@@ -110,26 +114,25 @@ SwiftMangledName SwiftMangler::visitExtensionDecl(const swift::ExtensionDecl* de
110114
}
111115

112116
SwiftMangledName SwiftMangler::visitGenericTypeDecl(const swift::GenericTypeDecl* decl) {
113-
if (auto ret = visitValueDecl(decl)) {
114-
if (auto genericParams = decl->getGenericParams()) {
115-
ret << '<' << genericParams->size() << '>';
116-
}
117-
// TODO: almost same code as for function type
118-
if (!decl->getGenericRequirements().empty()) {
119-
ret << "where_";
120-
for (const auto& req : decl->getGenericRequirements()) {
121-
ret << dispatcher.fetchLabel(req.getFirstType()->getCanonicalType());
122-
ret << (req.getKind() == swift::RequirementKind::SameType ? '=' : ':');
123-
if (req.getKind() == swift::RequirementKind::Layout) {
124-
ret << '(' << req.getLayoutConstraint().getString() << ')';
125-
} else {
126-
ret << dispatcher.fetchLabel(req.getSecondType()->getCanonicalType());
127-
}
128-
}
129-
}
130-
return ret;
117+
auto ret = visitValueDecl(decl);
118+
if (!ret) {
119+
return {};
131120
}
132-
return {};
121+
if (auto genericParams = decl->getParsedGenericParams()) {
122+
ret << '<' << genericParams->size() << '>';
123+
}
124+
return ret;
125+
}
126+
127+
SwiftMangledName SwiftMangler::visitAbstractTypeParamDecl(
128+
const swift::AbstractTypeParamDecl* decl) {
129+
return visitValueDecl(decl, /* force */ true);
130+
}
131+
132+
SwiftMangledName SwiftMangler::visitGenericTypeParamDecl(const swift::GenericTypeParamDecl* decl) {
133+
auto ret = visitAbstractTypeParamDecl(decl);
134+
ret << '_' << decl->getDepth() << '_' << decl->getIndex();
135+
return ret;
133136
}
134137

135138
SwiftMangledName SwiftMangler::visitModuleType(const swift::ModuleType* type) {
@@ -151,15 +154,15 @@ SwiftMangledName SwiftMangler::visitTupleType(const swift::TupleType* type) {
151154

152155
SwiftMangledName SwiftMangler::visitBuiltinType(const swift::BuiltinType* type) {
153156
auto ret = initMangled(type);
154-
ret << dispatcher.fetchLabel(type->getASTContext().TheBuiltinModule);
155157
llvm::SmallString<32> buffer;
156158
ret << type->getTypeName(buffer, /* prependBuiltinNamespace= */ false);
157159
return ret;
158160
}
159161

160162
SwiftMangledName SwiftMangler::visitAnyGenericType(const swift::AnyGenericType* type) {
161163
auto ret = initMangled(type);
162-
ret << dispatcher.fetchLabel(type->getDecl());
164+
auto decl = type->getDecl();
165+
ret << dispatcher.fetchLabel(decl);
163166
if (auto parent = type->getParent()) {
164167
ret << dispatcher.fetchLabel(parent);
165168
}
@@ -189,6 +192,9 @@ SwiftMangledName SwiftMangler::visitAnyFunctionType(const swift::AnyFunctionType
189192
if (param.isOwned()) {
190193
ret << "_owned";
191194
}
195+
if (param.isShared()) {
196+
ret << "_shared";
197+
}
192198
if (param.isVariadic()) {
193199
ret << "...";
194200
}
@@ -203,6 +209,9 @@ SwiftMangledName SwiftMangler::visitAnyFunctionType(const swift::AnyFunctionType
203209
if (type->isSendable()) {
204210
ret << "_sendable";
205211
}
212+
if (type->isNoEscape()) {
213+
ret << "_noescape";
214+
}
206215
// TODO: see if this needs to be used in identifying types, if not it needs to be removed from
207216
// type printing
208217
assert(type->hasExtInfo() && "type must have ext info");
@@ -287,9 +296,14 @@ SwiftMangledName SwiftMangler::visitDictionaryType(const swift::DictionaryType*
287296
SwiftMangledName SwiftMangler::visitTypeAliasType(const swift::TypeAliasType* type) {
288297
auto ret = initMangled(type);
289298
ret << dispatcher.fetchLabel(type->getDecl());
299+
if (auto parent = type->getParent()) {
300+
ret << dispatcher.fetchLabel(parent);
301+
}
302+
ret << '<';
290303
for (auto replacement : type->getSubstitutionMap().getReplacementTypes()) {
291304
ret << dispatcher.fetchLabel(replacement);
292305
}
306+
ret << '>';
293307
return ret;
294308
}
295309

@@ -300,6 +314,13 @@ SwiftMangledName SwiftMangler::visitArchetypeType(const swift::ArchetypeType* ty
300314
return ret;
301315
}
302316

317+
SwiftMangledName SwiftMangler::visitOpaqueTypeArchetypeType(
318+
const swift::OpaqueTypeArchetypeType* type) {
319+
auto ret = visitArchetypeType(type);
320+
ret << dispatcher.fetchLabel(type->getDecl());
321+
return ret;
322+
}
323+
303324
SwiftMangledName SwiftMangler::visitProtocolCompositionType(
304325
const swift::ProtocolCompositionType* type) {
305326
auto ret = initMangled(type);

swift/extractor/mangler/SwiftMangler.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,14 +43,16 @@ class SwiftMangler : private swift::TypeVisitor<SwiftMangler, SwiftMangledName>,
4343
static SwiftMangledName visitDecl(const swift::Decl* decl) { return {}; }
4444

4545
// current default, falling back to internal mangling
46-
SwiftMangledName visitValueDecl(const swift::ValueDecl* decl);
46+
SwiftMangledName visitValueDecl(const swift::ValueDecl* decl, bool force = false);
4747

4848
SwiftMangledName visitModuleDecl(const swift::ModuleDecl* decl);
4949
SwiftMangledName visitExtensionDecl(const swift::ExtensionDecl* decl);
5050
SwiftMangledName visitAbstractFunctionDecl(const swift::AbstractFunctionDecl* decl);
5151
SwiftMangledName visitSubscriptDecl(const swift::SubscriptDecl* decl);
5252
SwiftMangledName visitVarDecl(const swift::VarDecl* decl);
5353
SwiftMangledName visitGenericTypeDecl(const swift::GenericTypeDecl* decl);
54+
SwiftMangledName visitAbstractTypeParamDecl(const swift::AbstractTypeParamDecl* decl);
55+
SwiftMangledName visitGenericTypeParamDecl(const swift::GenericTypeParamDecl* decl);
5456

5557
// default fallback for not yet mangled types. This should never be called in normal situations
5658
// TODO: make it assert once we mangle all types
@@ -79,6 +81,7 @@ class SwiftMangler : private swift::TypeVisitor<SwiftMangler, SwiftMangledName>,
7981
SwiftMangledName visitDictionaryType(const swift::DictionaryType* type);
8082
SwiftMangledName visitTypeAliasType(const swift::TypeAliasType* type);
8183
SwiftMangledName visitArchetypeType(const swift::ArchetypeType* type);
84+
SwiftMangledName visitOpaqueTypeArchetypeType(const swift::OpaqueTypeArchetypeType* type);
8285
SwiftMangledName visitProtocolCompositionType(const swift::ProtocolCompositionType* type);
8386
SwiftMangledName visitParenType(const swift::ParenType* type);
8487
SwiftMangledName visitLValueType(const swift::LValueType* type);

swift/extractor/trap/TrapLabel.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ class UntypedTrapLabel {
4444
}
4545

4646
std::string str() const {
47+
assert(valid() && "outputting an undefined label!");
4748
std::string ret(strSize(), '\0');
4849
ret[0] = '#';
4950
std::to_chars(ret.data() + 1, ret.data() + ret.size(), id_, 16);

swift/integration-tests/posix-only/deduplication/DB-CHECK.expected

Lines changed: 0 additions & 61 deletions
This file was deleted.

swift/integration-tests/posix-only/deduplication/Decls.expected

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,15 @@
3939
| Sources/deduplication/def.swift:24:36:24:39 | _ | ParamDecl | A [GenericTypeParamType] |
4040
| Sources/deduplication/def.swift:24:42:24:45 | _ | ParamDecl | B [GenericTypeParamType] |
4141
| Sources/deduplication/def.swift:24:48:24:51 | _ | ParamDecl | C [GenericTypeParamType] |
42+
| Sources/deduplication/def.swift:26:1:26:21 | Protocol1 | ProtocolDecl | Protocol1.Protocol [MetatypeType] |
43+
| Sources/deduplication/def.swift:27:1:29:1 | Protocol2 | ProtocolDecl | Protocol2.Protocol [MetatypeType] |
44+
| Sources/deduplication/def.swift:28:5:28:20 | Associated | AssociatedTypeDecl | Self.Associated.Type [MetatypeType] |
4245
| Sources/deduplication/def.swift:30:1:30:14 | Class | ClassDecl | Class.Type [MetatypeType] |
4346
| Sources/deduplication/def.swift:30:7:30:7 | Class.deinit() | Deinitializer | (Class) -> () -> () [FunctionType] |
4447
| Sources/deduplication/def.swift:30:7:30:7 | Class.init() | Initializer | (Class.Type) -> () -> Class [FunctionType] |
4548
| Sources/deduplication/def.swift:30:7:30:7 | self | ParamDecl | Class [ClassType] |
4649
| Sources/deduplication/def.swift:30:7:30:7 | self | ParamDecl | Class [ClassType] |
50+
| Sources/deduplication/def.swift:32:1:32:128 | def_generic_function_with_conformance(_:_:_:) | ConcreteFuncDecl | <A, B, C where A : Protocol1, A : Protocol2, B : Class, C == A.Associated> (A, B, C) -> () [GenericFunctionType] |
4751
| Sources/deduplication/def.swift:32:44:32:60 | A | GenericTypeParamDecl | A.Type [MetatypeType] |
4852
| Sources/deduplication/def.swift:32:71:32:75 | B | GenericTypeParamDecl | B.Type [MetatypeType] |
4953
| Sources/deduplication/def.swift:32:82:32:82 | C | GenericTypeParamDecl | C.Type [MetatypeType] |
@@ -70,6 +74,7 @@
7074
| Sources/deduplication/use.swift:6:33:6:36 | _ | ParamDecl | Int [StructType] |
7175
| Sources/deduplication/use.swift:7:1:7:61 | use_async_throwing_function_type(_:) | NamedFunction | (Int) async throws -> () [FunctionType] |
7276
| Sources/deduplication/use.swift:7:39:7:42 | _ | ParamDecl | Int [StructType] |
77+
| Sources/deduplication/use.swift:8:1:8:150 | use_generic_function_with_conformance_type(_:_:_:) | ConcreteFuncDecl | <AA, BB, CC where AA : Protocol1, AA : Protocol2, BB : Class, CC == AA.Associated> (AA, BB, CC) -> () [GenericFunctionType] |
7378
| Sources/deduplication/use.swift:8:49:8:49 | AA | GenericTypeParamDecl | AA.Type [MetatypeType] |
7479
| Sources/deduplication/use.swift:8:53:8:53 | BB | GenericTypeParamDecl | BB.Type [MetatypeType] |
7580
| Sources/deduplication/use.swift:8:57:8:57 | CC | GenericTypeParamDecl | CC.Type [MetatypeType] |

0 commit comments

Comments
 (0)