forked from swiftlang/swift
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathTypeRefBuilder.cpp
368 lines (316 loc) · 11 KB
/
TypeRefBuilder.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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
//===--- TypeRefBuilder.cpp - Swift Type Reference Builder ----------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2016 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See http://swift.org/LICENSE.txt for license information
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
//
// Implements utilities for constructing TypeRefs and looking up field and
// enum case types.
//
//===----------------------------------------------------------------------===//
#include "swift/Reflection/TypeRefBuilder.h"
#include "swift/Basic/Demangle.h"
#include "swift/Reflection/Records.h"
#include "swift/Reflection/TypeLowering.h"
#include "swift/Reflection/TypeRef.h"
#include "swift/Remote/MetadataReader.h"
using namespace swift;
using namespace reflection;
TypeRefBuilder::TypeRefBuilder() : TC(*this) {}
const TypeRef * TypeRefBuilder::
lookupTypeWitness(const std::string &MangledTypeName,
const std::string &Member,
const TypeRef *Protocol) {
TypeRefID key;
key.addString(MangledTypeName);
key.addString(Member);
key.addPointer(Protocol);
auto found = AssociatedTypeCache.find(key);
if (found != AssociatedTypeCache.end())
return found->second;
// Cache missed - we need to look through all of the assocty sections
// for all images that we've been notified about.
for (auto &Info : ReflectionInfos) {
for (const auto &AssocTyDescriptor : Info.assocty) {
std::string ConformingTypeName(AssocTyDescriptor.ConformingTypeName);
if (ConformingTypeName.compare(MangledTypeName) != 0)
continue;
std::string ProtocolMangledName(AssocTyDescriptor.ProtocolTypeName);
auto DemangledProto = Demangle::demangleTypeAsNode(ProtocolMangledName);
auto TR = swift::remote::decodeMangledType(*this, DemangledProto);
if (Protocol != TR)
continue;
for (auto &AssocTy : AssocTyDescriptor) {
if (Member.compare(AssocTy.getName()) != 0)
continue;
auto SubstitutedTypeName = AssocTy.getMangledSubstitutedTypeName();
auto Demangled = Demangle::demangleTypeAsNode(SubstitutedTypeName);
auto *TypeWitness = swift::remote::decodeMangledType(*this, Demangled);
AssociatedTypeCache.insert(std::make_pair(key, TypeWitness));
return TypeWitness;
}
}
}
return nullptr;
}
const TypeRef * TypeRefBuilder::
lookupSuperclass(const std::string &MangledTypeName) {
// Superclasses are recorded as a special associated type named 'super'
// on the 'AnyObject' protocol.
return lookupTypeWitness(MangledTypeName, "super",
ProtocolTypeRef::create(*this, "Ps9AnyObject_"));
}
const TypeRef * TypeRefBuilder::
lookupSuperclass(const TypeRef *TR) {
const TypeRef *Superclass = nullptr;
if (auto *Nominal = dyn_cast<NominalTypeRef>(TR)) {
Superclass = lookupSuperclass(Nominal->getMangledName());
} else {
auto BG = cast<BoundGenericTypeRef>(TR);
Superclass = lookupSuperclass(BG->getMangledName());
}
if (Superclass == nullptr)
return nullptr;
return Superclass->subst(*this, TR->getSubstMap());
}
const FieldDescriptor *
TypeRefBuilder::getFieldTypeInfo(const TypeRef *TR) {
std::string MangledName;
if (auto N = dyn_cast<NominalTypeRef>(TR))
MangledName = N->getMangledName();
else if (auto BG = dyn_cast<BoundGenericTypeRef>(TR))
MangledName = BG->getMangledName();
else if (auto P = dyn_cast<ProtocolTypeRef>(TR))
MangledName = P->getMangledName();
else
return {};
std::vector<std::pair<std::string, const TypeRef *>> Fields;
for (auto Info : ReflectionInfos) {
for (auto &FD : Info.fieldmd) {
if (!FD.hasMangledTypeName())
continue;
auto CandidateMangledName = FD.getMangledTypeName();
if (MangledName.compare(CandidateMangledName) != 0)
continue;
return &FD;
}
}
return nullptr;
}
std::vector<FieldTypeInfo>
TypeRefBuilder::getFieldTypeRefs(const TypeRef *TR, const FieldDescriptor *FD) {
if (FD == nullptr)
return {};
auto Subs = TR->getSubstMap();
std::vector<FieldTypeInfo> Fields;
for (auto &Field : *FD) {
auto FieldName = Field.getFieldName();
// Empty cases of enums do not have a type
if (FD->isEnum() && !Field.hasMangledTypeName()) {
Fields.push_back(FieldTypeInfo::forEmptyCase(FieldName));
continue;
}
auto Demangled
= Demangle::demangleTypeAsNode(Field.getMangledTypeName());
auto Unsubstituted = swift::remote::decodeMangledType(*this, Demangled);
if (!Unsubstituted)
return {};
auto Substituted = Unsubstituted->subst(*this, Subs);
if (FD->isEnum() && Field.isIndirectCase()) {
Fields.push_back(FieldTypeInfo::forIndirectCase(FieldName, Substituted));
continue;
}
Fields.push_back(FieldTypeInfo::forField(FieldName, Substituted));
}
return Fields;
}
const BuiltinTypeDescriptor *
TypeRefBuilder::getBuiltinTypeInfo(const TypeRef *TR) {
std::string MangledName;
if (auto B = dyn_cast<BuiltinTypeRef>(TR))
MangledName = B->getMangledName();
else if (auto N = dyn_cast<NominalTypeRef>(TR))
MangledName = N->getMangledName();
else
return nullptr;
for (auto Info : ReflectionInfos) {
for (auto &BuiltinTypeDescriptor : Info.builtin) {
assert(BuiltinTypeDescriptor.Size > 0);
assert(BuiltinTypeDescriptor.Alignment > 0);
assert(BuiltinTypeDescriptor.Stride > 0);
if (!BuiltinTypeDescriptor.hasMangledTypeName())
continue;
auto CandidateMangledName = BuiltinTypeDescriptor.getMangledTypeName();
if (MangledName.compare(CandidateMangledName) != 0)
continue;
return &BuiltinTypeDescriptor;
}
}
return nullptr;
}
const CaptureDescriptor *
TypeRefBuilder::getCaptureDescriptor(uintptr_t RemoteAddress) {
for (auto Info : ReflectionInfos) {
for (auto &CD : Info.capture) {
auto OtherAddr = ((uintptr_t) &CD -
Info.LocalStartAddress +
Info.RemoteStartAddress);
if (OtherAddr == RemoteAddress)
return &CD;
}
}
return nullptr;
}
/// Get the unsubstituted capture types for a closure context.
ClosureContextInfo
TypeRefBuilder::getClosureContextInfo(const CaptureDescriptor &CD) {
ClosureContextInfo Info;
for (auto i = CD.capture_begin(), e = CD.capture_end(); i != e; ++i) {
const TypeRef *TR = nullptr;
if (i->hasMangledTypeName()) {
auto MangledName = i->getMangledTypeName();
auto DemangleTree = Demangle::demangleTypeAsNode(MangledName);
TR = swift::remote::decodeMangledType(*this, DemangleTree);
}
Info.CaptureTypes.push_back(TR);
}
for (auto i = CD.source_begin(), e = CD.source_end(); i != e; ++i) {
const TypeRef *TR = nullptr;
if (i->hasMangledTypeName()) {
auto MangledName = i->getMangledTypeName();
auto DemangleTree = Demangle::demangleTypeAsNode(MangledName);
TR = swift::remote::decodeMangledType(*this, DemangleTree);
}
const MetadataSource *MS = nullptr;
if (i->hasMangledMetadataSource()) {
auto MangledMetadataSource = i->getMangledMetadataSource();
MS = MetadataSource::decode(MSB, MangledMetadataSource);
}
Info.MetadataSources.push_back({TR, MS});
}
Info.NumBindings = CD.NumBindings;
return Info;
}
///
/// Dumping reflection metadata
///
void
TypeRefBuilder::dumpTypeRef(const std::string &MangledName,
std::ostream &OS, bool printTypeName) {
auto TypeName = Demangle::demangleTypeAsString(MangledName);
OS << TypeName << '\n';
auto DemangleTree = Demangle::demangleTypeAsNode(MangledName);
auto TR = swift::remote::decodeMangledType(*this, DemangleTree);
if (!TR) {
OS << "!!! Invalid typeref: " << MangledName << '\n';
return;
}
TR->dump(OS);
OS << '\n';
}
void TypeRefBuilder::dumpFieldSection(std::ostream &OS) {
for (const auto §ions : ReflectionInfos) {
for (const auto &descriptor : sections.fieldmd) {
auto TypeName
= Demangle::demangleTypeAsString(descriptor.getMangledTypeName());
OS << TypeName << '\n';
for (size_t i = 0; i < TypeName.size(); ++i)
OS << '-';
OS << '\n';
for (auto &field : descriptor) {
OS << field.getFieldName();
if (field.hasMangledTypeName()) {
OS << ": ";
dumpTypeRef(field.getMangledTypeName(), OS);
} else {
OS << "\n\n";
}
}
}
}
}
void TypeRefBuilder::dumpAssociatedTypeSection(std::ostream &OS) {
for (const auto §ions : ReflectionInfos) {
for (const auto &descriptor : sections.assocty) {
auto conformingTypeName = Demangle::demangleTypeAsString(
descriptor.getMangledConformingTypeName());
auto protocolName = Demangle::demangleTypeAsString(
descriptor.getMangledProtocolTypeName());
OS << "- " << conformingTypeName << " : " << protocolName;
OS << '\n';
for (const auto &associatedType : descriptor) {
OS << "typealias " << associatedType.getName() << " = ";
dumpTypeRef(associatedType.getMangledSubstitutedTypeName(), OS);
}
}
}
}
void TypeRefBuilder::dumpBuiltinTypeSection(std::ostream &OS) {
for (const auto §ions : ReflectionInfos) {
for (const auto &descriptor : sections.builtin) {
auto typeName = Demangle::demangleTypeAsString(
descriptor.getMangledTypeName());
OS << "\n- " << typeName << ":\n";
OS << "Size: " << descriptor.Size << "\n";
OS << "Alignment: " << descriptor.Alignment << "\n";
OS << "Stride: " << descriptor.Stride << "\n";
OS << "NumExtraInhabitants: " << descriptor.NumExtraInhabitants << "\n";
}
}
}
void ClosureContextInfo::dump() const {
dump(std::cerr);
}
void ClosureContextInfo::dump(std::ostream &OS) const {
OS << "- Capture types:\n";
for (auto *TR : CaptureTypes) {
if (TR == nullptr)
OS << "!!! Invalid typeref\n";
else
TR->dump(OS);
}
OS << "- Metadata sources:\n";
for (auto MS : MetadataSources) {
if (MS.first == nullptr)
OS << "!!! Invalid typeref\n";
else
MS.first->dump(OS);
if (MS.second == nullptr)
OS << "!!! Invalid metadata source\n";
else
MS.second->dump(OS);
}
OS << "\n";
}
void TypeRefBuilder::dumpCaptureSection(std::ostream &OS) {
for (const auto §ions : ReflectionInfos) {
for (const auto &descriptor : sections.capture) {
auto info = getClosureContextInfo(descriptor);
info.dump(OS);
}
}
}
void TypeRefBuilder::dumpAllSections(std::ostream &OS) {
OS << "FIELDS:\n";
OS << "=======\n";
dumpFieldSection(OS);
OS << '\n';
OS << "ASSOCIATED TYPES:\n";
OS << "=================\n";
dumpAssociatedTypeSection(OS);
OS << '\n';
OS << "BUILTIN TYPES:\n";
OS << "==============\n";
dumpBuiltinTypeSection(OS);
OS << '\n';
OS << "CAPTURE DESCRIPTORS:\n";
OS << "====================\n";
dumpCaptureSection(OS);
OS << '\n';
}