Skip to content

Commit 0d0dc20

Browse files
committed
AST: Mangling for primitive AnyObject
A protocol composition with an explicit 'AnyObject' member is now mangled as <protocol list> 'Xl'. For subclass existentials, I changed the mangling from <protocol list> <class> 'XE' to <protocol list> <class> 'Xl'. Not used for anything just yet.
1 parent 38a22d1 commit 0d0dc20

File tree

8 files changed

+53
-13
lines changed

8 files changed

+53
-13
lines changed

docs/ABI.rst

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1044,7 +1044,8 @@ Types
10441044
type ::= associated-type
10451045
type ::= nominal-type
10461046
type ::= protocol-list 'p' // existential type
1047-
type ::= protocol-list superclass 'XE' // existential type with superclass
1047+
type ::= protocol-list superclass 'Xc' // existential type with superclass
1048+
type ::= protocol-list 'Xl' // existential type with AnyObject
10481049
type ::= type-list 't' // tuple
10491050
type ::= type generic-signature 'u' // generic type
10501051
type ::= 'x' // generic param, depth=0, idx=0

include/swift/Demangling/DemangleNodes.def

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@ NODE(ProtocolConformance)
124124
NODE(ProtocolDescriptor)
125125
NODE(ProtocolList)
126126
NODE(ProtocolListWithClass)
127+
NODE(ProtocolListWithAnyObject)
127128
NODE(ProtocolWitness)
128129
NODE(ProtocolWitnessTable)
129130
NODE(ProtocolWitnessTableAccessor)

include/swift/Demangling/Demangler.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -447,6 +447,7 @@ class Demangler : public NodeFactory {
447447
NodePointer demangleMetatypeRepresentation();
448448
NodePointer demangleFunctionEntity();
449449
NodePointer demangleEntity(Node::Kind Kind);
450+
NodePointer demangleProtocolList();
450451
NodePointer demangleProtocolListType();
451452
NodePointer demangleGenericSignature(bool hasParamCounts);
452453
NodePointer demangleGenericRequirement();

lib/AST/ASTMangler.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -653,7 +653,9 @@ void ASTMangler::appendType(Type type) {
653653

654654
if (layout.superclass) {
655655
appendType(layout.superclass);
656-
return appendOperator("XE");
656+
return appendOperator("Xc");
657+
} else if (layout.requiresClass & !layout.requiresClassImplied) {
658+
return appendOperator("Xl");
657659
}
658660
return appendOperator("p");
659661
}

lib/Demangling/Demangler.cpp

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1581,12 +1581,17 @@ NodePointer Demangler::demangleSpecialType() {
15811581
case 'p':
15821582
return createType(createWithChild(Node::Kind::ExistentialMetatype,
15831583
popNode(Node::Kind::Type)));
1584-
case 'E': {
1584+
case 'c': {
15851585
NodePointer Superclass = popNode(Node::Kind::Type);
1586-
NodePointer Protocols = demangleProtocolListType();
1586+
NodePointer Protocols = demangleProtocolList();
15871587
return createType(createWithChildren(Node::Kind::ProtocolListWithClass,
15881588
Protocols, Superclass));
15891589
}
1590+
case 'l': {
1591+
NodePointer Protocols = demangleProtocolList();
1592+
return createType(createWithChild(Node::Kind::ProtocolListWithAnyObject,
1593+
Protocols));
1594+
}
15901595
case 'X':
15911596
case 'x': {
15921597
// SIL box types.
@@ -1738,7 +1743,7 @@ NodePointer Demangler::demangleEntity(Node::Kind Kind) {
17381743
return createWithChildren(Kind, Context, Name, Type);
17391744
}
17401745

1741-
NodePointer Demangler::demangleProtocolListType() {
1746+
NodePointer Demangler::demangleProtocolList() {
17421747
NodePointer TypeList = createNode(Node::Kind::TypeList);
17431748
NodePointer ProtoList = createWithChild(Node::Kind::ProtocolList, TypeList);
17441749
if (!popNode(Node::Kind::EmptyList)) {
@@ -1753,6 +1758,11 @@ NodePointer Demangler::demangleProtocolListType() {
17531758

17541759
TypeList->reverseChildren();
17551760
}
1761+
return ProtoList;
1762+
}
1763+
1764+
NodePointer Demangler::demangleProtocolListType() {
1765+
NodePointer ProtoList = demangleProtocolList();
17561766
return createType(ProtoList);
17571767
}
17581768

lib/Demangling/NodePrinter.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,7 @@ class NodePrinter {
279279
return Node->getChild(0)->getNumChildren() <= 1;
280280

281281
case Node::Kind::ProtocolListWithClass:
282+
case Node::Kind::ProtocolListWithAnyObject:
282283
case Node::Kind::Allocator:
283284
case Node::Kind::ArgumentTuple:
284285
case Node::Kind::AssociatedTypeMetadataAccessor:
@@ -1344,6 +1345,20 @@ NodePointer NodePrinter::print(NodePointer Node, bool asPrefixContext) {
13441345
printChildren(protocols, " & ");
13451346
return nullptr;
13461347
}
1348+
case Node::Kind::ProtocolListWithAnyObject: {
1349+
if (Node->getNumChildren() < 1)
1350+
return nullptr;
1351+
NodePointer protocols = Node->getChild(0);
1352+
if (protocols->getNumChildren() < 1)
1353+
return nullptr;
1354+
if (protocols->getChild(0)->getNumChildren() == 0) {
1355+
Printer << "AnyObject";
1356+
} else {
1357+
printChildren(protocols->getChild(0), " & ");
1358+
Printer << " & AnyObject";
1359+
}
1360+
return nullptr;
1361+
}
13471362
case Node::Kind::AssociatedType:
13481363
// Don't print for now.
13491364
return nullptr;

lib/Demangling/Remangler.cpp

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,8 @@ class Remangler {
249249
mangleChildNodes(Proto);
250250
}
251251

252-
void mangleProtocolList(Node *protocols, Node *superclass);
252+
void mangleProtocolList(Node *protocols, Node *superclass,
253+
bool hasExplicitAnyObject);
253254

254255
bool trySubstitution(Node *node, SubstitutionEntry &entry,
255256
bool treatAsIdentifier = false);
@@ -1403,29 +1404,38 @@ void Remangler::mangleProtocolDescriptor(Node *node) {
14031404
Buffer << "Mp";
14041405
}
14051406

1406-
void Remangler::mangleProtocolList(Node *node, Node *superclass) {
1407+
void Remangler::mangleProtocolList(Node *node, Node *superclass,
1408+
bool hasExplicitAnyObject) {
1409+
auto *protocols = getSingleChild(node, Node::Kind::TypeList);
14071410
bool FirstElem = true;
1408-
for (NodePointer Child : *node) {
1411+
for (NodePointer Child : *protocols) {
14091412
manglePureProtocol(Child);
14101413
mangleListSeparator(FirstElem);
14111414
}
14121415
mangleEndOfList(FirstElem);
14131416
if (superclass) {
14141417
mangleType(superclass);
1415-
Buffer << "XE";
1418+
Buffer << "Xc";
1419+
return;
1420+
} else if (hasExplicitAnyObject) {
1421+
Buffer << "Xl";
14161422
return;
14171423
}
14181424
Buffer << 'p';
14191425
}
14201426

14211427
void Remangler::mangleProtocolList(Node *node) {
1422-
auto *protocols = getSingleChild(node, Node::Kind::TypeList);
1423-
mangleProtocolList(protocols, nullptr);
1428+
mangleProtocolList(node, nullptr, false);
14241429
}
14251430

14261431
void Remangler::mangleProtocolListWithClass(Node *node) {
14271432
mangleProtocolList(node->getChild(0),
1428-
node->getChild(1));
1433+
node->getChild(1),
1434+
false);
1435+
}
1436+
1437+
void Remangler::mangleProtocolListWithAnyObject(Node *node) {
1438+
mangleProtocolList(node->getChild(0), nullptr, true);
14291439
}
14301440

14311441
void Remangler::mangleProtocolWitness(Node *node) {

test/SILGen/subclass_existentials.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ class Derived : Base<Int>, P {
1919

2020
protocol R {}
2121

22-
// CHECK-LABEL: sil hidden @_T021subclass_existentials11conversionsyAA1P_AA4BaseCySiGXE8baseAndP_AA7DerivedC7derivedAA1R_AIXE0hF1RAaC_AFXEXp0eF5PTypeAIm0H4TypeAaK_AIXEXp0hF5RTypetF : $@convention(thin) (@owned Base<Int> & P, @owned Derived, @owned Derived & R, @thick (Base<Int> & P).Type, @thick Derived.Type, @thick (Derived & R).Type) -> () {
22+
// CHECK-LABEL: sil hidden @_T021subclass_existentials11conversionsyAA1P_AA4BaseCySiGXc8baseAndP_AA7DerivedC7derivedAA1R_AIXc0hF1RAaC_AFXcXp0eF5PTypeAIm0H4TypeAaK_AIXcXp0hF5RTypetF : $@convention(thin) (@owned Base<Int> & P, @owned Derived, @owned Derived & R, @thick (Base<Int> & P).Type, @thick Derived.Type, @thick (Derived & R).Type) -> () {
2323

2424
func conversions(
2525
baseAndP: Base<Int> & P,

0 commit comments

Comments
 (0)