@@ -1330,6 +1330,39 @@ NodePointer Demangler::demangleExtensionContext() {
13301330 return Ext;
13311331}
13321332
1333+ // / Associate any \c OpaqueReturnType nodes with the declaration whose opaque
1334+ // / return type they refer back to.
1335+ static Node *setParentForOpaqueReturnTypeNodes (Demangler &D,
1336+ Node *parent,
1337+ Node *visitedNode) {
1338+ if (!parent || !visitedNode)
1339+ return nullptr ;
1340+ if (visitedNode->getKind () == Node::Kind::OpaqueReturnType) {
1341+ // If this node is not already parented, parent it.
1342+ if (visitedNode->hasChildren ()
1343+ && visitedNode->getLastChild ()->getKind () == Node::Kind::OpaqueReturnTypeParent) {
1344+ return parent;
1345+ }
1346+ visitedNode->addChild (D.createNode (Node::Kind::OpaqueReturnTypeParent,
1347+ (Node::IndexType)parent), D);
1348+ return parent;
1349+ }
1350+
1351+ // If this node is one that may in turn define its own opaque return type,
1352+ // stop recursion, since any opaque return type nodes underneath would refer
1353+ // to the nested declaration rather than the one we're looking at.
1354+ if (visitedNode->getKind () == Node::Kind::Function
1355+ || visitedNode->getKind () == Node::Kind::Variable
1356+ || visitedNode->getKind () == Node::Kind::Subscript) {
1357+ return parent;
1358+ }
1359+
1360+ for (size_t i = 0 , e = visitedNode->getNumChildren (); i < e; ++i) {
1361+ setParentForOpaqueReturnTypeNodes (D, parent, visitedNode->getChild (i));
1362+ }
1363+ return parent;
1364+ }
1365+
13331366NodePointer Demangler::demanglePlainFunction () {
13341367 NodePointer GenSig = popNode (Node::Kind::DependentGenericSignature);
13351368 NodePointer Type = popFunctionType (Node::Kind::FunctionType);
@@ -1343,10 +1376,12 @@ NodePointer Demangler::demanglePlainFunction() {
13431376 auto Name = popNode (isDeclName);
13441377 auto Ctx = popContext ();
13451378
1346- if (LabelList)
1347- return createWithChildren (Node::Kind::Function, Ctx, Name, LabelList, Type);
1348-
1349- return createWithChildren (Node::Kind::Function, Ctx, Name, Type);
1379+ NodePointer result = LabelList
1380+ ? createWithChildren (Node::Kind::Function, Ctx, Name, LabelList, Type)
1381+ : createWithChildren (Node::Kind::Function, Ctx, Name, Type);
1382+
1383+ result = setParentForOpaqueReturnTypeNodes (*this , result, Type);
1384+ return result;
13501385}
13511386
13521387NodePointer Demangler::popFunctionType (Node::Kind kind, bool hasClangType) {
@@ -2273,7 +2308,8 @@ NodePointer Demangler::demangleArchetype() {
22732308 int ordinal = demangleIndex ();
22742309 if (ordinal < 0 )
22752310 return NULL ;
2276- return createType (createNode (Node::Kind::OpaqueReturnTypeIndexed, ordinal));
2311+ return createType (createWithChild (Node::Kind::OpaqueReturnType,
2312+ createNode (Node::Kind::OpaqueReturnTypeIndex, ordinal)));
22772313 }
22782314
22792315 case ' x' : {
@@ -3363,7 +3399,7 @@ NodePointer Demangler::demangleSpecialType() {
33633399 return nullptr ;
33643400 // Build layout.
33653401 auto layout = createNode (Node::Kind::SILBoxLayout);
3366- for (unsigned i = 0 , e = fieldTypes->getNumChildren (); i < e; ++i) {
3402+ for (size_t i = 0 , e = fieldTypes->getNumChildren (); i < e; ++i) {
33673403 auto fieldType = fieldTypes->getChild (i);
33683404 assert (fieldType->getKind () == Node::Kind::Type);
33693405 bool isMutable = false ;
@@ -3617,8 +3653,11 @@ NodePointer Demangler::demangleEntity(Node::Kind Kind) {
36173653 NodePointer LabelList = popFunctionParamLabels (Type);
36183654 NodePointer Name = popNode (isDeclName);
36193655 NodePointer Context = popContext ();
3620- return LabelList ? createWithChildren (Kind, Context, Name, LabelList, Type)
3621- : createWithChildren (Kind, Context, Name, Type);
3656+ auto result = LabelList ? createWithChildren (Kind, Context, Name, LabelList, Type)
3657+ : createWithChildren (Kind, Context, Name, Type);
3658+
3659+ result = setParentForOpaqueReturnTypeNodes (*this , result, Type);
3660+ return result;
36223661}
36233662
36243663NodePointer Demangler::demangleVariable () {
@@ -3640,6 +3679,8 @@ NodePointer Demangler::demangleSubscript() {
36403679 addChild (Subscript, LabelList);
36413680 Subscript = addChild (Subscript, Type);
36423681 addChild (Subscript, PrivateName);
3682+
3683+ Subscript = setParentForOpaqueReturnTypeNodes (*this , Subscript, Type);
36433684
36443685 return demangleAccessor (Subscript);
36453686}
0 commit comments