Skip to content

Commit 2dc6104

Browse files
authored
Merge pull request #8271 from eeckstein/fix-ext-mangling
mangling: Correctly demangle + remangle extensions of generic types.
2 parents 124b293 + 160d268 commit 2dc6104

File tree

3 files changed

+32
-18
lines changed

3 files changed

+32
-18
lines changed

lib/Demangling/Demangler.cpp

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -893,17 +893,22 @@ NodePointer Demangler::demangleBoundGenericArgs(NodePointer Nominal,
893893

894894
if (TypeListIdx >= TypeLists.size())
895895
return nullptr;
896-
NodePointer args = TypeLists[TypeListIdx];
896+
NodePointer args = TypeLists[TypeListIdx++];
897897

898898
// Generic arguments for the outermost type come first.
899899
NodePointer Context = Nominal->getFirstChild();
900900

901-
if (Context->getKind() != Node::Kind::Module &&
902-
Context->getKind() != Node::Kind::Function &&
903-
Context->getKind() != Node::Kind::Extension) {
904-
NodePointer BoundParent = demangleBoundGenericArgs(Context, TypeLists,
905-
TypeListIdx + 1);
906-
901+
if (TypeListIdx < TypeLists.size()) {
902+
NodePointer BoundParent = nullptr;
903+
if (Context->getKind() == Node::Kind::Extension) {
904+
BoundParent = demangleBoundGenericArgs(Context->getChild(1), TypeLists,
905+
TypeListIdx);
906+
BoundParent = createWithChildren(Node::Kind::Extension,
907+
Context->getFirstChild(),
908+
BoundParent);
909+
} else {
910+
BoundParent = demangleBoundGenericArgs(Context, TypeLists, TypeListIdx);
911+
}
907912
// Rebuild this type with the new parent type, which may have
908913
// had its generic arguments applied.
909914
Nominal = createWithChildren(Nominal->getKind(), BoundParent,

lib/Demangling/Remangler.cpp

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -449,13 +449,11 @@ void Remangler::mangleGenericArgs(Node *node, char &Separator) {
449449
switch (node->getKind()) {
450450
case Node::Kind::Structure:
451451
case Node::Kind::Enum:
452-
case Node::Kind::Class: {
453-
NodePointer parentOrModule = node->getChild(0);
454-
mangleGenericArgs(parentOrModule, Separator);
452+
case Node::Kind::Class:
453+
mangleGenericArgs(node->getChild(0), Separator);
455454
Buffer << Separator;
456455
Separator = '_';
457456
break;
458-
}
459457

460458
case Node::Kind::BoundGenericStructure:
461459
case Node::Kind::BoundGenericEnum:
@@ -471,6 +469,10 @@ void Remangler::mangleGenericArgs(Node *node, char &Separator) {
471469
break;
472470
}
473471

472+
case Node::Kind::Extension:
473+
mangleGenericArgs(node->getChild(1), Separator);
474+
break;
475+
474476
default:
475477
break;
476478
}
@@ -1741,13 +1743,11 @@ bool Demangle::isSpecialized(Node *node) {
17411743

17421744
case Node::Kind::Structure:
17431745
case Node::Kind::Enum:
1744-
case Node::Kind::Class: {
1745-
Node *parentOrModule = node->getChild(0);
1746-
if (isSpecialized(parentOrModule))
1747-
return true;
1746+
case Node::Kind::Class:
1747+
return isSpecialized(node->getChild(0));
17481748

1749-
return false;
1750-
}
1749+
case Node::Kind::Extension:
1750+
return isSpecialized(node->getChild(1));
17511751

17521752
default:
17531753
return false;
@@ -1781,6 +1781,15 @@ NodePointer Demangle::getUnspecialized(Node *node, NodeFactory &Factory) {
17811781
return nominalType;
17821782
}
17831783

1784+
case Node::Kind::Extension: {
1785+
NodePointer parent = node->getChild(1);
1786+
if (!isSpecialized(parent))
1787+
return node;
1788+
NodePointer result = Factory.createNode(Node::Kind::Extension);
1789+
result->addChild(node->getFirstChild(), Factory);
1790+
result->addChild(getUnspecialized(parent, Factory), Factory);
1791+
return result;
1792+
}
17841793
default:
17851794
unreachable("bad nominal type kind");
17861795
}

test/Demangle/Inputs/manglings.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -245,4 +245,4 @@ _T03foo4_123ABTf3psbpsb_n ---> function signature specialization <Arg[0] = [Cons
245245
_T04main5innerys5Int32Vz_yADctF25closure_with_box_argumentxz_Bi32__lXXTf1nc_n ---> function signature specialization <Arg[1] = [Closure Propagated : closure_with_box_argument, Argument Types : [<A> { var A } <Builtin.Int32>]> of main.inner (inout Swift.Int32, (Swift.Int32) -> ()) -> ()
246246
_T03foo6testityyyc_yyctF1a1bTf3pfpf_n ---> function signature specialization <Arg[0] = [Constant Propagated Function : a], Arg[1] = [Constant Propagated Function : b]> of foo.testit (() -> (), () -> ()) -> ()
247247
_SocketJoinOrLeaveMulticast ---> _SocketJoinOrLeaveMulticast
248-
248+
_T0s10DictionaryV3t17E6Index2V1loiSbAEyxq__G_AGtFZ ---> static (extension in t17):Swift.Dictionary.Index2.< infix ((extension in t17):[A : B].Index2, (extension in t17):[A : B].Index2) -> Swift.Bool

0 commit comments

Comments
 (0)