@@ -28,7 +28,6 @@ using namespace swift;
28
28
Demangle::NodePointer
29
29
swift::_buildDemanglingForContext (const ContextDescriptor *context,
30
30
llvm::ArrayRef<NodePointer> demangledGenerics,
31
- bool concretizedGenerics,
32
31
Demangle::Demangler &Dem) {
33
32
unsigned usedDemangledGenerics = 0 ;
34
33
NodePointer node = nullptr ;
@@ -45,13 +44,6 @@ swift::_buildDemanglingForContext(const ContextDescriptor *context,
45
44
46
45
auto getGenericArgsTypeListForContext =
47
46
[&](const ContextDescriptor *context) -> NodePointer {
48
- // ABI TODO: As a hack to maintain existing broken behavior,
49
- // if there were any generic arguments eliminated by same type
50
- // constraints, we don't mangle any of them into intermediate contexts,
51
- // and pile all of the non-concrete arguments into the innermost context.
52
- if (concretizedGenerics)
53
- return nullptr ;
54
-
55
47
if (demangledGenerics.empty ())
56
48
return nullptr ;
57
49
@@ -73,7 +65,6 @@ swift::_buildDemanglingForContext(const ContextDescriptor *context,
73
65
return genericArgsList;
74
66
};
75
67
76
- auto innermostComponent = descriptorPath.front ();
77
68
for (auto component : reversed (descriptorPath)) {
78
69
switch (auto kind = component->getKind ()) {
79
70
case ContextDescriptorKind::Module: {
@@ -91,9 +82,6 @@ swift::_buildDemanglingForContext(const ContextDescriptor *context,
91
82
selfType = selfType->getChild (0 );
92
83
93
84
// Substitute in the generic arguments.
94
- // TODO: This kludge only kinda works if there are no same-type
95
- // constraints. We'd need to handle those correctly everywhere else too
96
- // though.
97
85
auto genericArgsList = getGenericArgsTypeListForContext (component);
98
86
99
87
if (selfType->getKind () == Node::Kind::BoundGenericEnum
@@ -197,28 +185,6 @@ swift::_buildDemanglingForContext(const ContextDescriptor *context,
197
185
node = genericNode;
198
186
}
199
187
200
- // ABI TODO: If there were concretized generic arguments, just pile
201
- // all the non-concretized generic arguments into the innermost context.
202
- if (concretizedGenerics
203
- && !demangledGenerics.empty ()
204
- && component == innermostComponent) {
205
- auto unspecializedType = Dem.createNode (Node::Kind::Type);
206
- unspecializedType->addChild (node, Dem);
207
-
208
- auto genericTypeList = Dem.createNode (Node::Kind::TypeList);
209
- for (auto arg : demangledGenerics) {
210
- if (!arg) continue ;
211
- genericTypeList->addChild (arg, Dem);
212
- }
213
-
214
- if (genericTypeList->getNumChildren () > 0 ) {
215
- auto genericNode = Dem.createNode (genericNodeKind);
216
- genericNode->addChild (unspecializedType, Dem);
217
- genericNode->addChild (genericTypeList, Dem);
218
- node = genericNode;
219
- }
220
- }
221
-
222
188
break ;
223
189
}
224
190
@@ -312,8 +278,10 @@ _buildDemanglingForNominalType(const Metadata *type, Demangle::Demangler &Dem) {
312
278
313
279
// Demangle the generic arguments.
314
280
std::vector<NodePointer> demangledGenerics;
315
- bool concretizedGenerics = false ;
281
+
316
282
if (auto generics = description->getGenericContext ()) {
283
+ std::vector<const Metadata *> allGenericArgs;
284
+ bool concretizedGenerics = false ;
317
285
auto genericArgs = description->getGenericArguments (type);
318
286
for (auto param : generics->getGenericParams ()) {
319
287
switch (param.getKind ()) {
@@ -332,10 +300,12 @@ _buildDemanglingForNominalType(const Metadata *type, Demangle::Demangler &Dem) {
332
300
_swift_buildDemanglingForMetadata (paramType, Dem);
333
301
if (!paramDemangling)
334
302
return nullptr ;
303
+ allGenericArgs.push_back (paramType);
335
304
demangledGenerics.push_back (paramDemangling);
336
305
} else {
337
306
// Leave a gap for us to fill in by looking at same type info.
338
307
demangledGenerics.push_back (nullptr );
308
+ allGenericArgs.push_back (nullptr );
339
309
concretizedGenerics = true ;
340
310
}
341
311
break ;
@@ -358,11 +328,88 @@ _buildDemanglingForNominalType(const Metadata *type, Demangle::Demangler &Dem) {
358
328
359
329
// If we have concretized generic arguments, check for same type
360
330
// requirements to get the argument values.
361
- // ABI TODO
331
+ if (concretizedGenerics) {
332
+ // Retrieve the mapping information needed for depth/index -> flat index.
333
+ std::vector<unsigned > genericParamCounts;
334
+ (void )_gatherGenericParameterCounts (description, genericParamCounts);
335
+
336
+ // Walk through the generic requirements to evaluate same-type
337
+ // constraints that are needed to fill in missing generic arguments.
338
+ for (const auto &req : generics->getGenericRequirements ()) {
339
+ // We only care about same-type constraints.
340
+ if (req.Flags .getKind () != GenericRequirementKind::SameType)
341
+ continue ;
342
+
343
+ // Where the left-hand side is a generic parameter.
344
+ if (req.Param .begin () != req.Param .end ())
345
+ continue ;
346
+
347
+ // If we don't yet have an argument for this parameter, it's a
348
+ // same-type-to-concrete constraint.
349
+ unsigned lhsFlatIndex = req.Param .getRootParamIndex ();
350
+ if (!allGenericArgs[lhsFlatIndex]) {
351
+ // Substitute into the right-hand side.
352
+ auto genericArg =
353
+ _getTypeByMangledName (req.getMangledTypeName (),
354
+ [&](unsigned depth, unsigned index) {
355
+ if (auto flatIndex = _depthIndexToFlatIndex (depth, index,
356
+ genericParamCounts))
357
+ return allGenericArgs[*flatIndex];
358
+
359
+ return (const Metadata *)nullptr ;
360
+ });
361
+ if (!genericArg)
362
+ return nullptr ;
363
+
364
+ allGenericArgs[lhsFlatIndex] = genericArg;
365
+
366
+ // Form a demangling for this argument.
367
+ auto argDemangling =
368
+ _swift_buildDemanglingForMetadata (genericArg, Dem);
369
+ if (!argDemangling)
370
+ return nullptr ;
371
+
372
+ demangledGenerics[lhsFlatIndex] = argDemangling;
373
+ continue ;
374
+ }
375
+
376
+ // If we do have an argument for this parameter, it might be that
377
+ // the right-hand side is itself a generic parameter, which means
378
+ // we have a same-type constraint A == B where A is already filled in.
379
+ Demangler demangler;
380
+ NodePointer node = demangler.demangleType (req.getMangledTypeName ());
381
+ if (!node)
382
+ continue ;
383
+
384
+ // Find the flat index that the right-hand side refers to.
385
+ if (node->getKind () == Demangle::Node::Kind::Type)
386
+ node = node->getChild (0 );
387
+ if (node->getKind () != Demangle::Node::Kind::DependentGenericParamType)
388
+ continue ;
389
+
390
+ auto rhsFlatIndex =
391
+ _depthIndexToFlatIndex (node->getChild (0 )->getIndex (),
392
+ node->getChild (1 )->getIndex (),
393
+ genericParamCounts);
394
+ if (!rhsFlatIndex || *rhsFlatIndex > allGenericArgs.size ())
395
+ return nullptr ;
396
+
397
+ if (allGenericArgs[*rhsFlatIndex] || !allGenericArgs[lhsFlatIndex])
398
+ continue ;
399
+
400
+ allGenericArgs[*rhsFlatIndex] = allGenericArgs[lhsFlatIndex];
401
+ demangledGenerics[*rhsFlatIndex] = demangledGenerics[lhsFlatIndex];
402
+ }
403
+
404
+ // Make sure that all of the demangled generics are filled in now.
405
+ for (const auto node : demangledGenerics) {
406
+ if (!node)
407
+ return nullptr ;
408
+ }
409
+ }
362
410
}
363
411
364
- return _buildDemanglingForContext (description, demangledGenerics,
365
- concretizedGenerics, Dem);
412
+ return _buildDemanglingForContext (description, demangledGenerics, Dem);
366
413
}
367
414
368
415
// Build a demangled type tree for a type.
@@ -449,8 +496,7 @@ swift::_swift_buildDemanglingForMetadata(const Metadata *type,
449
496
#endif
450
497
451
498
auto protocolNode =
452
- _buildDemanglingForContext (protocol.getSwiftProtocol (), { }, false ,
453
- Dem);
499
+ _buildDemanglingForContext (protocol.getSwiftProtocol (), { }, Dem);
454
500
if (!protocolNode)
455
501
return nullptr ;
456
502
0 commit comments