@@ -116,13 +116,15 @@ class ReflectionContext {
116
116
return BuiltinTypeRef::create (*this , mangledName);
117
117
}
118
118
119
- NominalTypeRef *createNominalType (const std::string &mangledName) {
120
- return NominalTypeRef::create (*this , mangledName);
119
+ NominalTypeRef *createNominalType (const std::string &mangledName,
120
+ TypeRef *parent) {
121
+ return NominalTypeRef::create (*this , mangledName, parent);
121
122
}
122
123
123
124
BoundGenericTypeRef *createBoundGenericType (const std::string &mangledName,
124
- const std::vector<TypeRef *> &args) {
125
- return BoundGenericTypeRef::create (*this , mangledName, args);
125
+ const std::vector<TypeRef *> &args,
126
+ TypeRef *parent) {
127
+ return BoundGenericTypeRef::create (*this , mangledName, args, parent);
126
128
}
127
129
128
130
TupleTypeRef *createTupleType (const std::vector<TypeRef *> &elements,
@@ -270,6 +272,28 @@ class ReflectionContext {
270
272
return *Reader;
271
273
}
272
274
275
+ std::pair<std::string, Type>
276
+ decodeNominalType (const Demangle::NodePointer &Node) {
277
+ using NodeKind = Demangle::Node::Kind;
278
+ if (Node->getKind () == NodeKind::Type)
279
+ return decodeNominalType (Node->getChild (0 ));
280
+
281
+ assert (Node->getNumChildren () == 2 );
282
+ auto moduleOrParentType = Node->getChild (0 );
283
+
284
+ // Nested types are handled a bit funny here because a
285
+ // nominal typeref always stores its full mangled name,
286
+ // in addition to a reference to the parent type. The
287
+ // mangled name already includes the module and parent
288
+ // types, if any.
289
+ TypeRef *parent = nullptr ;
290
+ if (moduleOrParentType->getKind () != NodeKind::Module)
291
+ parent = decodeMangledType (moduleOrParentType);
292
+
293
+ auto mangledName = Demangle::mangleNode (Node);
294
+ return std::make_pair (mangledName, parent);
295
+ }
296
+
273
297
Type decodeMangledType (const Demangle::NodePointer &Node) {
274
298
using NodeKind = Demangle::Node::Kind;
275
299
switch (Node->getKind ()) {
@@ -279,26 +303,38 @@ class ReflectionContext {
279
303
return decodeMangledType (Node->getChild (0 ));
280
304
case NodeKind::Type:
281
305
return decodeMangledType (Node->getChild (0 ));
306
+ case NodeKind::Class:
307
+ case NodeKind::Enum:
308
+ case NodeKind::Structure: {
309
+ std::string mangledName;
310
+ Type parent;
311
+
312
+ std::tie (mangledName, parent) = decodeNominalType (Node);
313
+ return Builder.createNominalType (mangledName, parent);
314
+ }
282
315
case NodeKind::BoundGenericClass:
283
316
case NodeKind::BoundGenericEnum:
284
317
case NodeKind::BoundGenericStructure: {
285
- auto mangledName = Demangle::mangleNode (Node->getChild (0 ));
318
+ assert (Node->getNumChildren () == 2 );
319
+
320
+ std::string mangledName;
321
+ Type parent;
322
+
323
+ std::tie (mangledName, parent) = decodeNominalType (Node->getChild (0 ));
324
+
325
+ std::vector<Type> args;
326
+
286
327
auto genericArgs = Node->getChild (1 );
287
- std::vector<TypeRef *> args;
328
+ assert (genericArgs->getKind () == NodeKind::TypeList);
329
+
288
330
for (auto genericArg : *genericArgs) {
289
- auto paramTypeRef = decodeMangledType (genericArg);
290
- if (!paramTypeRef )
331
+ auto paramType = decodeMangledType (genericArg);
332
+ if (!paramType )
291
333
return Type ();
292
- args.push_back (paramTypeRef );
334
+ args.push_back (paramType );
293
335
}
294
336
295
- return Builder.createBoundGenericType (mangledName, args);
296
- }
297
- case NodeKind::Class:
298
- case NodeKind::Enum:
299
- case NodeKind::Structure: {
300
- auto mangledName = Demangle::mangleNode (Node);
301
- return Builder.createNominalType (mangledName);
337
+ return Builder.createBoundGenericType (mangledName, args, parent);
302
338
}
303
339
case NodeKind::BuiltinTypeName: {
304
340
auto mangledName = Demangle::mangleNode (Node);
@@ -724,14 +760,9 @@ class ReflectionContext {
724
760
Type Nominal;
725
761
if (Descriptor->GenericParams .NumPrimaryParams ) {
726
762
auto Args = getGenericSubst (MetadataAddress);
727
- auto BG = Builder.createBoundGenericType (MangledName, Args);
728
- BG->setParent (Parent);
729
- Nominal = BG;
763
+ Nominal = Builder.createBoundGenericType (MangledName, Args, Parent);
730
764
} else {
731
- auto TR = decodeMangledType (DemangleNode);
732
- auto N = cast<NominalTypeRef>(TR);
733
- N->setParent (Parent);
734
- Nominal = TR;
765
+ Nominal = decodeMangledType (DemangleNode);
735
766
}
736
767
TypeCache.insert ({MetadataAddress, Nominal});
737
768
return Nominal;
0 commit comments