Skip to content

Commit b71bef1

Browse files
committed
[ABI] Mangling for default associated conformance accessors.
Default associated conformance accessors will be used in default witness tables to fill in associated conformances for defaulted associated types. Add (de|re|)mangling support for them and make them linking entities in IRGen.
1 parent d076e41 commit b71bef1

File tree

10 files changed

+72
-2
lines changed

10 files changed

+72
-2
lines changed

docs/ABI/Mangling.rst

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,8 @@ types where the metadata itself has unknown layout.)
141141
global ::= protocol 'TL' // protocol requirements base descriptor
142142
global ::= assoc-type-name 'Tl' // associated type descriptor
143143
global ::= assoc-type-name 'TM' // default associated type witness accessor
144-
global ::= type assoc-type-path protocol // associated conformance descriptor
144+
global ::= type assoc-type-path protocol 'Tn' // associated conformance descriptor
145+
global ::= type assoc-type-path protocol 'TN' // default associated conformance witness accessor
145146

146147
REABSTRACT-THUNK-TYPE ::= 'R' // reabstraction thunk helper function
147148
REABSTRACT-THUNK-TYPE ::= 'r' // reabstraction thunk

include/swift/Demangling/DemangleNodes.def

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,7 @@ NODE(DispatchThunk)
211211
NODE(MethodDescriptor)
212212
NODE(ProtocolRequirementsBaseDescriptor)
213213
NODE(AssociatedConformanceDescriptor)
214+
NODE(DefaultAssociatedConformanceAccessor)
214215
NODE(AssociatedTypeDescriptor)
215216
NODE(ThrowsAnnotation)
216217
NODE(EmptyList)

include/swift/IRGen/Linking.h

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,11 @@ class LinkEntity {
213213
/// is stored in the data.
214214
AssociatedConformanceDescriptor,
215215

216+
/// A default accessor for an associated conformance of a protocol.
217+
/// The pointer is a ProtocolDecl*; the index of the associated conformance
218+
/// is stored in the data.
219+
DefaultAssociatedConformanceAccessor,
220+
216221
/// A function which returns the default type metadata for the associated
217222
/// type of a protocol. The secondary pointer is a ProtocolDecl*.
218223
/// The index of the associated type declaration is stored in the data.
@@ -834,6 +839,17 @@ class LinkEntity {
834839
return entity;
835840
}
836841

842+
static LinkEntity
843+
forDefaultAssociatedConformanceAccessor(AssociatedConformance conformance) {
844+
LinkEntity entity;
845+
entity.setForProtocolAndAssociatedConformance(
846+
Kind::DefaultAssociatedConformanceAccessor,
847+
conformance.getSourceProtocol(),
848+
conformance.getAssociation(),
849+
conformance.getAssociatedRequirement());
850+
return entity;
851+
}
852+
837853
static LinkEntity forReflectionBuiltinDescriptor(CanType type) {
838854
LinkEntity entity;
839855
entity.setForType(Kind::ReflectionBuiltinDescriptor, type);
@@ -924,7 +940,8 @@ class LinkEntity {
924940
LINKENTITY_GET_FIELD(Data, AssociatedConformanceIndex));
925941
}
926942

927-
assert(getKind() == Kind::AssociatedConformanceDescriptor);
943+
assert(getKind() == Kind::AssociatedConformanceDescriptor ||
944+
getKind() == Kind::DefaultAssociatedConformanceAccessor);
928945
return getAssociatedConformanceByIndex(
929946
cast<ProtocolDecl>(getDecl()),
930947
LINKENTITY_GET_FIELD(Data, AssociatedConformanceIndex));

lib/Demangling/Demangler.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1835,6 +1835,15 @@ NodePointer Demangler::demangleThunkOrSpecialization() {
18351835
protoTy, assocTypePath, requirementTy);
18361836
}
18371837

1838+
case 'N': {
1839+
NodePointer requirementTy = popProtocol();
1840+
auto assocTypePath = popAssocTypePath();
1841+
NodePointer protoTy = popNode(Node::Kind::Type);
1842+
return createWithChildren(
1843+
Node::Kind::DefaultAssociatedConformanceAccessor,
1844+
protoTy, assocTypePath, requirementTy);
1845+
}
1846+
18381847
case 'H':
18391848
case 'h': {
18401849
auto nodeKind = c == 'H' ? Node::Kind::KeyPathEqualsThunkHelper

lib/Demangling/NodePrinter.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,7 @@ class NodePrinter {
323323
case Node::Kind::DeclContext:
324324
case Node::Kind::DefaultArgumentInitializer:
325325
case Node::Kind::DefaultAssociatedTypeMetadataAccessor:
326+
case Node::Kind::DefaultAssociatedConformanceAccessor:
326327
case Node::Kind::DependentAssociatedTypeRef:
327328
case Node::Kind::DependentGenericSignature:
328329
case Node::Kind::DependentGenericParamCount:
@@ -1564,6 +1565,14 @@ NodePointer NodePrinter::print(NodePointer Node, bool asPrefixContext) {
15641565
Printer << ": ";
15651566
print(Node->getChild(2));
15661567
return nullptr;
1568+
case Node::Kind::DefaultAssociatedConformanceAccessor:
1569+
Printer << "default associated conformance accessor for ";
1570+
print(Node->getChild(0));
1571+
Printer << ".";
1572+
print(Node->getChild(1));
1573+
Printer << ": ";
1574+
print(Node->getChild(2));
1575+
return nullptr;
15671576
case Node::Kind::AssociatedTypeDescriptor:
15681577
Printer << "associated type descriptor for ";
15691578
print(Node->getChild(0));

lib/Demangling/OldRemangler.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -888,6 +888,10 @@ void Remangler::mangleAssociatedConformanceDescriptor(Node *node) {
888888
Out << "<associated-conformance-descriptor>";
889889
}
890890

891+
void Remangler::mangleDefaultAssociatedConformanceAccessor(Node *node) {
892+
Out << "<default-associated-conformance-descriptor>";
893+
}
894+
891895
void Remangler::mangleAssociatedTypeMetadataAccessor(Node *node) {
892896
Out << "Wt";
893897
mangleChildNodes(node); // protocol conformance, identifier

lib/Demangling/Remangler.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -579,6 +579,11 @@ void Remangler::mangleAssociatedConformanceDescriptor(Node *node) {
579579
Buffer << "Tn";
580580
}
581581

582+
void Remangler::mangleDefaultAssociatedConformanceAccessor(Node *node) {
583+
mangleChildNodes(node);
584+
Buffer << "TN";
585+
}
586+
582587
void Remangler::mangleAssociatedTypeMetadataAccessor(Node *node) {
583588
mangleChildNodes(node); // protocol conformance, identifier
584589
Buffer << "Wt";

lib/IRGen/IRGenMangler.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,19 @@ class IRGenMangler : public Mangle::ASTMangler {
197197
return finalize();
198198
}
199199

200+
std::string mangleDefaultAssociatedConformanceAccessor(
201+
const ProtocolDecl *proto,
202+
CanType subject,
203+
const ProtocolDecl *requirement) {
204+
beginMangling();
205+
appendAnyGenericType(proto);
206+
bool isFirstAssociatedTypeIdentifier = true;
207+
appendAssociatedTypePath(subject, isFirstAssociatedTypeIdentifier);
208+
appendProtocolName(requirement);
209+
appendOperator("TN");
210+
return finalize();
211+
}
212+
200213
std::string mangleProtocolConformanceDescriptor(
201214
const ProtocolConformance *Conformance) {
202215
beginMangling();

lib/IRGen/Linking.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,14 @@ std::string LinkEntity::mangleAsString() const {
187187
assocConformance.second);
188188
}
189189

190+
case Kind::DefaultAssociatedConformanceAccessor: {
191+
auto assocConformance = getAssociatedConformance();
192+
return mangler.mangleDefaultAssociatedConformanceAccessor(
193+
cast<ProtocolDecl>(getDecl()),
194+
assocConformance.first,
195+
assocConformance.second);
196+
}
197+
190198
case Kind::ProtocolConformanceDescriptor:
191199
return mangler.mangleProtocolConformanceDescriptor(
192200
cast<NormalProtocolConformance>(getProtocolConformance()));
@@ -498,6 +506,7 @@ SILLinkage LinkEntity::getLinkage(ForDefinition_t forDefinition) const {
498506
case Kind::AssociatedTypeMetadataAccessFunction:
499507
case Kind::DefaultAssociatedTypeMetadataAccessFunction:
500508
case Kind::AssociatedTypeWitnessTableAccessFunction:
509+
case Kind::DefaultAssociatedConformanceAccessor:
501510
case Kind::GenericProtocolWitnessTableCache:
502511
case Kind::GenericProtocolWitnessTableInstantiationFunction:
503512
return SILLinkage::Private;
@@ -629,6 +638,7 @@ bool LinkEntity::isAvailableExternally(IRGenModule &IGM) const {
629638
case Kind::TypeMetadataCompletionFunction:
630639
case Kind::TypeMetadataPattern:
631640
case Kind::DefaultAssociatedTypeMetadataAccessFunction:
641+
case Kind::DefaultAssociatedConformanceAccessor:
632642
return false;
633643

634644
case Kind::ValueWitness:

test/Demangle/Inputs/manglings.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -332,3 +332,4 @@ A.h<A>(A) -> ()inlined generic function <(A, A1)> of
332332
$S1T19protocol_resilience17ResilientProtocolPTl --> associated type descriptor for T
333333
$S18resilient_protocol21ResilientBaseProtocolTL --> protocol requirements base descriptor for resilient_protocol.ResilientBaseProtocol
334334
$S1t1PP10AssocType2_AA1QTn --> associated conformance descriptor for t.P.AssocType2: t.Q
335+
$S1t1PP10AssocType2_AA1QTN --> default associated conformance accessor for t.P.AssocType2: t.Q

0 commit comments

Comments
 (0)