forked from swiftlang/swift
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIRGenMangler.cpp
309 lines (275 loc) · 10.1 KB
/
IRGenMangler.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
//===--- IRGenMangler.cpp - mangling of IRGen symbols ---------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
#include "IRGenMangler.h"
#include "swift/AST/ExistentialLayout.h"
#include "swift/AST/IRGenOptions.h"
#include "swift/AST/ProtocolAssociations.h"
#include "swift/AST/ProtocolConformance.h"
#include "swift/Demangling/ManglingMacros.h"
#include "swift/Demangling/Demangle.h"
#include "swift/ABI/MetadataValues.h"
#include "swift/ClangImporter/ClangModule.h"
#include "llvm/Support/SaveAndRestore.h"
using namespace swift;
using namespace irgen;
const char *getManglingForWitness(swift::Demangle::ValueWitnessKind kind) {
switch (kind) {
#define VALUE_WITNESS(MANGLING, NAME) \
case swift::Demangle::ValueWitnessKind::NAME: return #MANGLING;
#include "swift/Demangling/ValueWitnessMangling.def"
}
llvm_unreachable("not a function witness");
}
std::string IRGenMangler::mangleValueWitness(Type type, ValueWitness witness) {
beginMangling();
appendType(type);
const char *Code = nullptr;
switch (witness) {
#define GET_MANGLING(ID) \
case ValueWitness::ID: Code = getManglingForWitness(swift::Demangle::ValueWitnessKind::ID); break;
GET_MANGLING(InitializeBufferWithCopyOfBuffer) \
GET_MANGLING(Destroy) \
GET_MANGLING(InitializeWithCopy) \
GET_MANGLING(AssignWithCopy) \
GET_MANGLING(InitializeWithTake) \
GET_MANGLING(AssignWithTake) \
GET_MANGLING(GetEnumTagSinglePayload) \
GET_MANGLING(StoreEnumTagSinglePayload) \
GET_MANGLING(GetEnumTag) \
GET_MANGLING(DestructiveProjectEnumData) \
GET_MANGLING(DestructiveInjectEnumTag)
#undef GET_MANGLING
case ValueWitness::Size:
case ValueWitness::Flags:
case ValueWitness::ExtraInhabitantCount:
case ValueWitness::Stride:
llvm_unreachable("not a function witness");
}
appendOperator("w", Code);
return finalize();
}
std::string IRGenMangler::manglePartialApplyForwarder(StringRef FuncName) {
if (FuncName.empty()) {
beginMangling();
} else {
if (FuncName.startswith(MANGLING_PREFIX_STR)) {
Buffer << FuncName;
} else {
beginMangling();
appendIdentifier(FuncName);
}
}
appendOperator("TA");
return finalize();
}
SymbolicMangling
IRGenMangler::withSymbolicReferences(IRGenModule &IGM,
llvm::function_ref<void ()> body) {
Mod = IGM.getSwiftModule();
OptimizeProtocolNames = false;
UseObjCProtocolNames = true;
llvm::SaveAndRestore<bool>
AllowSymbolicReferencesLocally(AllowSymbolicReferences);
llvm::SaveAndRestore<std::function<bool (SymbolicReferent)>>
CanSymbolicReferenceLocally(CanSymbolicReference);
AllowSymbolicReferences = true;
CanSymbolicReference = [&IGM](SymbolicReferent s) -> bool {
if (auto type = s.dyn_cast<const NominalTypeDecl *>()) {
// FIXME: Sometimes we fail to emit metadata for Clang imported types
// even after noting use of their type descriptor or metadata. Work
// around by not symbolic-referencing imported types for now.
if (type->hasClangNode())
return false;
// TODO: We ought to be able to use indirect symbolic references even
// when the referent may be in another file, once the on-disk
// ObjectMemoryReader can handle them.
// Private entities are known to be accessible.
auto formalAccessScope = type->getFormalAccessScope(nullptr, true);
if ((formalAccessScope.isPublic() || formalAccessScope.isInternal()) &&
(!IGM.CurSourceFile ||
IGM.CurSourceFile != type->getParentSourceFile()))
return false;
// @objc protocols don't have descriptors.
if (auto proto = dyn_cast<ProtocolDecl>(type))
if (proto->isObjC())
return false;
return true;
} else if (auto opaque = s.dyn_cast<const OpaqueTypeDecl *>()) {
// Always symbolically reference opaque types.
return true;
} else {
llvm_unreachable("symbolic referent not handled");
}
};
SymbolicReferences.clear();
body();
return {finalize(), std::move(SymbolicReferences)};
}
SymbolicMangling
IRGenMangler::mangleTypeForReflection(IRGenModule &IGM,
Type Ty) {
return withSymbolicReferences(IGM, [&]{
appendType(Ty);
});
}
std::string IRGenMangler::mangleProtocolConformanceDescriptor(
const RootProtocolConformance *conformance) {
beginMangling();
if (isa<NormalProtocolConformance>(conformance)) {
appendProtocolConformance(conformance);
appendOperator("Mc");
} else {
auto protocol = cast<SelfProtocolConformance>(conformance)->getProtocol();
appendProtocolName(protocol);
appendOperator("MS");
}
return finalize();
}
SymbolicMangling
IRGenMangler::mangleProtocolConformanceForReflection(IRGenModule &IGM,
Type ty, ProtocolConformanceRef conformance) {
return withSymbolicReferences(IGM, [&]{
if (conformance.isConcrete()) {
appendProtocolConformance(conformance.getConcrete());
} else {
// Use a special mangling for abstract conformances.
appendType(ty);
appendProtocolName(conformance.getAbstract());
}
});
}
std::string IRGenMangler::mangleTypeForLLVMTypeName(CanType Ty) {
// To make LLVM IR more readable we always add a 'T' prefix so that type names
// don't start with a digit and don't need to be quoted.
Buffer << 'T';
if (auto P = dyn_cast<ProtocolType>(Ty)) {
appendProtocolName(P->getDecl(), /*allowStandardSubstitution=*/false);
appendOperator("P");
} else {
appendType(Ty);
}
return finalize();
}
std::string IRGenMangler::
mangleProtocolForLLVMTypeName(ProtocolCompositionType *type) {
ExistentialLayout layout = type->getExistentialLayout();
if (type->isAny()) {
Buffer << "Any";
} else if (layout.isAnyObject()) {
Buffer << "AnyObject";
} else {
// To make LLVM IR more readable we always add a 'T' prefix so that type names
// don't start with a digit and don't need to be quoted.
Buffer << 'T';
auto protocols = layout.getProtocols();
for (unsigned i = 0, e = protocols.size(); i != e; ++i) {
appendProtocolName(protocols[i]->getDecl());
if (i == 0)
appendOperator("_");
}
if (auto superclass = layout.explicitSuperclass) {
// We share type infos for different instantiations of a generic type
// when the archetypes have the same exemplars. We cannot mangle
// archetypes, and the mangling does not have to be unique, so we just
// mangle the unbound generic form of the type.
if (superclass->hasArchetype()) {
superclass = superclass->getClassOrBoundGenericClass()
->getDeclaredType();
}
appendType(CanType(superclass));
appendOperator("Xc");
} else if (layout.getLayoutConstraint()) {
appendOperator("Xl");
} else {
appendOperator("p");
}
}
return finalize();
}
std::string IRGenMangler::
mangleSymbolNameForSymbolicMangling(const SymbolicMangling &mangling,
MangledTypeRefRole role) {
beginManglingWithoutPrefix();
const char *prefix;
switch (role) {
case MangledTypeRefRole::DefaultAssociatedTypeWitness:
prefix = "default assoc type ";
break;
case MangledTypeRefRole::Metadata:
case MangledTypeRefRole::Reflection:
prefix = "symbolic ";
break;
}
auto prefixLen = strlen(prefix);
Buffer << prefix << mangling.String;
for (auto &symbol : mangling.SymbolicReferences) {
// Fill in the placeholder space with something printable.
auto referent = symbol.first;
auto offset = symbol.second;
Storage[prefixLen + offset]
= Storage[prefixLen + offset+1]
= Storage[prefixLen + offset+2]
= Storage[prefixLen + offset+3]
= Storage[prefixLen + offset+4]
= '_';
Buffer << ' ';
if (auto ty = referent.dyn_cast<const NominalTypeDecl*>())
appendContext(ty);
else if (auto opaque = referent.dyn_cast<const OpaqueTypeDecl*>())
appendOpaqueDeclName(opaque);
else
llvm_unreachable("unhandled referent");
}
return finalize();
}
std::string IRGenMangler::mangleSymbolNameForAssociatedConformanceWitness(
const NormalProtocolConformance *conformance,
CanType associatedType,
const ProtocolDecl *proto) {
beginManglingWithoutPrefix();
if (conformance) {
Buffer << "associated conformance ";
appendProtocolConformance(conformance);
} else {
Buffer << "default associated conformance";
}
bool isFirstAssociatedTypeIdentifier = true;
appendAssociatedTypePath(associatedType, isFirstAssociatedTypeIdentifier);
appendProtocolName(proto);
return finalize();
}
std::string IRGenMangler::mangleSymbolNameForKeyPathMetadata(
const char *kind,
CanGenericSignature genericSig,
CanType type,
ProtocolConformanceRef conformance) {
beginManglingWithoutPrefix();
Buffer << kind << " ";
if (genericSig)
appendGenericSignature(genericSig);
if (type)
appendType(type);
if (conformance.isConcrete())
appendConcreteProtocolConformance(conformance.getConcrete());
else if (conformance.isAbstract())
appendProtocolName(conformance.getAbstract());
else
assert(conformance.isInvalid() && "Unknown protocol conformance");
return finalize();
}
std::string IRGenMangler::mangleSymbolNameForGenericEnvironment(
CanGenericSignature genericSig) {
beginManglingWithoutPrefix();
Buffer << "generic environment ";
appendGenericSignature(genericSig);
return finalize();
}