66
66
using namespace swift ;
67
67
using namespace swift ::Mangle;
68
68
69
+ template <typename DeclType>
70
+ static DeclType *getABIDecl (DeclType *D) {
71
+ if (!D)
72
+ return nullptr ;
73
+
74
+ auto abiRole = ABIRoleInfo (D);
75
+ if (!abiRole.providesABI ())
76
+ return abiRole.getCounterpart ();
77
+ return nullptr ;
78
+ }
79
+
80
+ static std::optional<ASTMangler::SymbolicReferent>
81
+ getABIDecl (ASTMangler::SymbolicReferent ref) {
82
+ switch (ref.getKind ()) {
83
+ case ASTMangler::SymbolicReferent::NominalType:
84
+ if (auto abiTypeDecl = getABIDecl (ref.getNominalType ())) {
85
+ return ASTMangler::SymbolicReferent (abiTypeDecl);
86
+ }
87
+ break ;
88
+
89
+ case ASTMangler::SymbolicReferent::OpaqueType:
90
+ if (auto abiTypeDecl = getABIDecl (ref.getOpaqueType ())) {
91
+ return ASTMangler::SymbolicReferent (abiTypeDecl);
92
+ }
93
+ break ;
94
+
95
+ case ASTMangler::SymbolicReferent::ExtendedExistentialTypeShape:
96
+ // Do nothing; mangling will use the underlying ABI decls in the end.
97
+ break ;
98
+ }
99
+
100
+ return std::nullopt;
101
+ }
102
+
103
+ void ASTMangler::addSubstitution (const Decl *decl) {
104
+ if (auto abiDecl = getABIDecl (decl)) {
105
+ return addSubstitution (abiDecl);
106
+ }
107
+ return Mangler::addSubstitution (decl);
108
+ }
109
+
110
+ bool ASTMangler::tryMangleSubstitution (const Decl *decl) {
111
+ if (auto abiDecl = getABIDecl (decl)) {
112
+ return tryMangleSubstitution (abiDecl);
113
+ }
114
+ return Mangler::tryMangleSubstitution (decl);
115
+ }
116
+
69
117
bool ASTMangler::inversesAllowed (const Decl *decl) {
70
118
if (!decl)
71
119
return true ;
@@ -302,6 +350,10 @@ std::string ASTMangler::mangleClosureWitnessThunk(
302
350
}
303
351
304
352
std::string ASTMangler::mangleGlobalVariableFull (const VarDecl *decl) {
353
+ if (auto abiDecl = getABIDecl (decl)) {
354
+ return mangleGlobalVariableFull (abiDecl);
355
+ }
356
+
305
357
// Clang globals get mangled using Clang's mangler.
306
358
if (auto clangDecl =
307
359
dyn_cast_or_null<clang::DeclaratorDecl>(decl->getClangDecl ())) {
@@ -427,6 +479,10 @@ std::string ASTMangler::mangleGlobalInit(const PatternBindingDecl *pd,
427
479
Pattern *pattern = pd->getPattern (pbdEntry);
428
480
bool first = true ;
429
481
pattern->forEachVariable ([&](VarDecl *D) {
482
+ if (auto abiD = getABIDecl (D)) {
483
+ D = abiD;
484
+ }
485
+
430
486
if (first) {
431
487
BaseEntitySignature base (D);
432
488
appendContextOf (D, base);
@@ -515,6 +571,10 @@ std::string ASTMangler::mangleAutoDiffLinearMap(
515
571
516
572
void ASTMangler::beginManglingWithAutoDiffOriginalFunction (
517
573
const AbstractFunctionDecl *afd) {
574
+ if (auto abiAFD = getABIDecl (afd)) {
575
+ return beginManglingWithAutoDiffOriginalFunction (abiAFD);
576
+ }
577
+
518
578
if (auto *attr = afd->getAttrs ().getAttribute <SILGenNameAttr>()) {
519
579
beginManglingWithoutPrefix ();
520
580
appendOperator (attr->Name );
@@ -848,6 +908,10 @@ void ASTMangler::appendAnyDecl(const ValueDecl *Decl) {
848
908
} else if (auto GTD = dyn_cast<GenericTypeDecl>(Decl)) {
849
909
appendAnyGenericType (GTD);
850
910
} else if (isa<AssociatedTypeDecl>(Decl)) {
911
+ if (auto abiDecl = getABIDecl (Decl)) {
912
+ return appendAnyDecl (abiDecl);
913
+ }
914
+
851
915
BaseEntitySignature base (Decl);
852
916
appendContextOf (Decl, base);
853
917
appendDeclName (Decl);
@@ -900,6 +964,10 @@ std::string ASTMangler::mangleAccessorEntityAsUSR(AccessorKind kind,
900
964
}
901
965
902
966
std::string ASTMangler::mangleLocalTypeDecl (const TypeDecl *type) {
967
+ if (auto abiType = getABIDecl (type)) {
968
+ return mangleLocalTypeDecl (abiType);
969
+ }
970
+
903
971
beginManglingWithoutPrefix ();
904
972
AllowNamelessEntities = true ;
905
973
OptimizeProtocolNames = false ;
@@ -950,6 +1018,10 @@ std::string ASTMangler::mangleHasSymbolQuery(const ValueDecl *Decl) {
950
1018
} else if (auto GTD = dyn_cast<GenericTypeDecl>(Decl)) {
951
1019
appendAnyGenericType (GTD);
952
1020
} else if (isa<AssociatedTypeDecl>(Decl)) {
1021
+ if (auto abiDecl = getABIDecl (Decl)) {
1022
+ Decl = abiDecl;
1023
+ }
1024
+
953
1025
BaseEntitySignature nullBase (nullptr );
954
1026
appendContextOf (Decl, nullBase);
955
1027
appendDeclName (Decl);
@@ -1057,6 +1129,7 @@ getOverriddenSwiftProtocolObjCName(const ValueDecl *decl,
1057
1129
}
1058
1130
1059
1131
void ASTMangler::appendDeclName (const ValueDecl *decl, DeclBaseName name) {
1132
+ ASSERT (!getABIDecl (decl) && " caller should make sure we get ABI decls" );
1060
1133
if (name.empty ())
1061
1134
name = decl->getBaseName ();
1062
1135
assert (!name.isSpecial () && " Cannot print special names" );
@@ -1192,6 +1265,10 @@ void ASTMangler::appendExistentialLayout(
1192
1265
bool DroppedRequiresClass = false ;
1193
1266
bool SawRequiresClass = false ;
1194
1267
for (auto proto : layout.getProtocols ()) {
1268
+ if (auto abiProto = getABIDecl (proto)) {
1269
+ proto = abiProto;
1270
+ }
1271
+
1195
1272
// Skip requirements to conform to an invertible protocols.
1196
1273
// We only mangle inverse requirements, but as a constrained existential.
1197
1274
if (proto->getInvertibleProtocolKind ())
@@ -1533,6 +1610,9 @@ void ASTMangler::appendType(Type type, GenericSignature sig,
1533
1610
Decl = typeAlias->getDecl ();
1534
1611
else
1535
1612
Decl = type->getAnyGeneric ();
1613
+ if (auto abiDecl = getABIDecl (Decl)) {
1614
+ Decl = abiDecl;
1615
+ }
1536
1616
if (shouldMangleAsGeneric (type)) {
1537
1617
// Try to mangle the entire name as a substitution.
1538
1618
if (tryMangleTypeSubstitution (tybase, sig))
@@ -2017,6 +2097,11 @@ void ASTMangler::appendSymbolicExtendedExistentialType(
2017
2097
Type type,
2018
2098
GenericSignature sig,
2019
2099
const ValueDecl *forDecl) {
2100
+ if (auto abiShapeReferent = getABIDecl (shapeReferent)) {
2101
+ return appendSymbolicExtendedExistentialType (abiShapeReferent.value (), type,
2102
+ sig, forDecl);
2103
+ }
2104
+
2020
2105
assert (shapeReferent.getKind () ==
2021
2106
SymbolicReferent::ExtendedExistentialTypeShape);
2022
2107
assert (canSymbolicReference (shapeReferent));
@@ -2542,6 +2627,7 @@ void ASTMangler::appendContext(const DeclContext *ctx,
2542
2627
void ASTMangler::appendModule (const ModuleDecl *module ,
2543
2628
StringRef useModuleName) {
2544
2629
assert (!module ->getParent () && " cannot mangle nested modules!" );
2630
+ ASSERT (!getABIDecl (module ));
2545
2631
2546
2632
// Use the module real name in mangling; this is the physical name
2547
2633
// of the module on-disk, which can be different if -module-alias is
@@ -2600,6 +2686,10 @@ void ASTMangler::appendProtocolName(const ProtocolDecl *protocol,
2600
2686
bool allowStandardSubstitution) {
2601
2687
assert (AllowMarkerProtocols || !protocol->isMarkerProtocol ());
2602
2688
2689
+ if (auto abiProtocol = getABIDecl (protocol)) {
2690
+ return appendProtocolName (abiProtocol, allowStandardSubstitution);
2691
+ }
2692
+
2603
2693
if (allowStandardSubstitution && tryAppendStandardSubstitution (protocol))
2604
2694
return ;
2605
2695
@@ -2661,6 +2751,10 @@ ASTMangler::getClangDeclForMangling(const ValueDecl *vd) {
2661
2751
}
2662
2752
2663
2753
void ASTMangler::appendSymbolicReference (SymbolicReferent referent) {
2754
+ if (auto abiReferent = getABIDecl (referent)) {
2755
+ return appendSymbolicReference (abiReferent.value ());
2756
+ }
2757
+
2664
2758
// Drop in a placeholder. The real reference value has to be filled in during
2665
2759
// lowering to IR.
2666
2760
auto offset = Buffer.str ().size ();
@@ -2794,6 +2888,10 @@ void ASTMangler::appendContextualInverses(const GenericTypeDecl *contextDecl,
2794
2888
void ASTMangler::appendExtension (const ExtensionDecl* ext,
2795
2889
BaseEntitySignature &base,
2796
2890
StringRef useModuleName) {
2891
+ if (auto abiExt = getABIDecl (ext)) {
2892
+ return appendExtension (abiExt, base, useModuleName);
2893
+ }
2894
+
2797
2895
auto decl = ext->getExtendedNominal ();
2798
2896
// Recover from erroneous extension.
2799
2897
if (!decl)
@@ -2844,6 +2942,10 @@ void ASTMangler::appendAnyGenericType(const GenericTypeDecl *decl) {
2844
2942
2845
2943
void ASTMangler::appendAnyGenericType (const GenericTypeDecl *decl,
2846
2944
BaseEntitySignature &base) {
2945
+ if (auto abiDecl = getABIDecl (decl)) {
2946
+ return appendAnyGenericType (abiDecl);
2947
+ }
2948
+
2847
2949
auto *nominal = dyn_cast<NominalTypeDecl>(decl);
2848
2950
2849
2951
if (nominal && isa<BuiltinTupleDecl>(nominal))
@@ -3749,6 +3851,10 @@ ASTMangler::dropProtocolsFromAssociatedTypes(Type type,
3749
3851
void ASTMangler::appendAssociatedTypeName (DependentMemberType *dmt,
3750
3852
GenericSignature sig) {
3751
3853
if (auto assocTy = dmt->getAssocType ()) {
3854
+ if (auto abiAssocTy = getABIDecl (assocTy)) {
3855
+ assocTy = abiAssocTy;
3856
+ }
3857
+
3752
3858
appendIdentifier (assocTy->getName ().str ());
3753
3859
3754
3860
// If the base type is known to have a single protocol conformance
@@ -3855,6 +3961,10 @@ CanType ASTMangler::getDeclTypeForMangling(
3855
3961
const ValueDecl *decl,
3856
3962
GenericSignature &genericSig,
3857
3963
GenericSignature &parentGenericSig) {
3964
+ if (auto abiDecl = getABIDecl (decl)) {
3965
+ return getDeclTypeForMangling (abiDecl, genericSig, parentGenericSig);
3966
+ }
3967
+
3858
3968
genericSig = GenericSignature ();
3859
3969
parentGenericSig = GenericSignature ();
3860
3970
@@ -3954,6 +4064,10 @@ bool ASTMangler::tryAppendStandardSubstitution(const GenericTypeDecl *decl) {
3954
4064
3955
4065
void ASTMangler::appendConstructorEntity (const ConstructorDecl *ctor,
3956
4066
bool isAllocating) {
4067
+ if (auto abiCtor = getABIDecl (ctor)) {
4068
+ return appendConstructorEntity (abiCtor, isAllocating);
4069
+ }
4070
+
3957
4071
BaseEntitySignature base (ctor);
3958
4072
appendContextOf (ctor, base);
3959
4073
appendDeclType (ctor, base);
@@ -3967,6 +4081,10 @@ void ASTMangler::appendConstructorEntity(const ConstructorDecl *ctor,
3967
4081
3968
4082
void ASTMangler::appendDestructorEntity (const DestructorDecl *dtor,
3969
4083
DestructorKind kind) {
4084
+ if (auto abiDtor = getABIDecl (dtor)) {
4085
+ return appendDestructorEntity (abiDtor, kind);
4086
+ }
4087
+
3970
4088
BaseEntitySignature base (dtor);
3971
4089
appendContextOf (dtor, base);
3972
4090
switch (kind) {
@@ -3985,6 +4103,10 @@ void ASTMangler::appendDestructorEntity(const DestructorDecl *dtor,
3985
4103
void ASTMangler::appendAccessorEntity (StringRef accessorKindCode,
3986
4104
const AbstractStorageDecl *decl,
3987
4105
bool isStatic) {
4106
+ if (auto abiDecl = getABIDecl (decl)) {
4107
+ return appendAccessorEntity (accessorKindCode, abiDecl, isStatic);
4108
+ }
4109
+
3988
4110
BaseEntitySignature base (decl);
3989
4111
appendContextOf (decl, base);
3990
4112
if (auto *varDecl = dyn_cast<VarDecl>(decl)) {
@@ -4012,6 +4134,10 @@ void ASTMangler::appendEntity(const ValueDecl *decl,
4012
4134
BaseEntitySignature &base,
4013
4135
StringRef EntityOp,
4014
4136
bool isStatic) {
4137
+ if (auto abiDecl = getABIDecl (decl)) {
4138
+ return appendEntity (abiDecl, base, EntityOp, isStatic);
4139
+ }
4140
+
4015
4141
appendContextOf (decl, base);
4016
4142
appendDeclName (decl);
4017
4143
appendDeclType (decl, base);
@@ -4023,7 +4149,11 @@ void ASTMangler::appendEntity(const ValueDecl *decl,
4023
4149
void ASTMangler::appendEntity (const ValueDecl *decl) {
4024
4150
assert (!isa<ConstructorDecl>(decl));
4025
4151
assert (!isa<DestructorDecl>(decl));
4026
-
4152
+
4153
+ if (auto abiDecl = getABIDecl (decl)) {
4154
+ return appendEntity (abiDecl);
4155
+ }
4156
+
4027
4157
// Handle accessors specially, they are mangled as modifiers on the accessed
4028
4158
// declaration.
4029
4159
if (auto accessor = dyn_cast<AccessorDecl>(decl)) {
@@ -4383,6 +4513,10 @@ ASTMangler::mangleOpaqueTypeDescriptorRecord(const OpaqueTypeDecl *decl) {
4383
4513
4384
4514
void ASTMangler::appendDistributedThunk (
4385
4515
const AbstractFunctionDecl *thunk, bool asReference) {
4516
+ if (auto abiThunk = getABIDecl (thunk)) {
4517
+ return appendDistributedThunk (abiThunk, asReference);
4518
+ }
4519
+
4386
4520
// Marker protocols cannot be checked at runtime, so there is no point
4387
4521
// in recording them for distributed thunks.
4388
4522
llvm::SaveAndRestore<bool > savedAllowMarkerProtocols (AllowMarkerProtocols,
@@ -4429,6 +4563,10 @@ void ASTMangler::appendDistributedThunk(
4429
4563
};
4430
4564
4431
4565
if (auto *P = referenceInProtocolContextOrRequirement ()) {
4566
+ if (auto abiP = getABIDecl (P)) {
4567
+ P = abiP;
4568
+ }
4569
+
4432
4570
appendContext (P->getDeclContext (), base,
4433
4571
thunk->getAlternateModuleName ());
4434
4572
appendIdentifier (Twine (" $" , P->getNameStr ()).str ());
@@ -4443,7 +4581,11 @@ void ASTMangler::appendDistributedThunk(
4443
4581
" mangled as thunks" );
4444
4582
// A distributed getter is mangled as the name of its storage (i.e. "the
4445
4583
// var")
4446
- appendIdentifier (accessor->getStorage ()->getBaseIdentifier ().str ());
4584
+ auto storage = accessor->getStorage ();
4585
+ if (auto abiStorage = getABIDecl (storage)) {
4586
+ storage = abiStorage;
4587
+ }
4588
+ appendIdentifier (storage->getBaseIdentifier ().str ());
4447
4589
} else {
4448
4590
appendIdentifier (thunk->getBaseIdentifier ().str ());
4449
4591
}
@@ -4658,6 +4800,10 @@ ASTMangler::mangleMacroExpansion(const FreestandingMacroExpansion *expansion) {
4658
4800
4659
4801
std::string ASTMangler::mangleAttachedMacroExpansion (
4660
4802
const Decl *decl, CustomAttr *attr, MacroRole role) {
4803
+ if (auto abiDecl = getABIDecl (decl)) {
4804
+ return mangleAttachedMacroExpansion (decl, attr, role);
4805
+ }
4806
+
4661
4807
// FIXME(kavon): using the decl causes a cycle. Is a null base fine?
4662
4808
BaseEntitySignature nullBase (nullptr );
4663
4809
@@ -4744,6 +4890,7 @@ std::string ASTMangler::mangleAttachedMacroExpansion(
4744
4890
static void gatherExistentialRequirements (SmallVectorImpl<Requirement> &reqs,
4745
4891
ParameterizedProtocolType *PPT) {
4746
4892
auto protoTy = PPT->getBaseType ();
4893
+ ASSERT (!getABIDecl (protoTy->getDecl ()) && " need to figure out behavior" );
4747
4894
PPT->getRequirements (protoTy->getDecl ()->getSelfInterfaceType (), reqs);
4748
4895
}
4749
4896
@@ -4764,6 +4911,7 @@ static void extractExistentialInverseRequirements(
4764
4911
for (auto ip : PCT->getInverses ()) {
4765
4912
auto *proto = ctx.getProtocol (getKnownProtocolKind (ip));
4766
4913
assert (proto);
4914
+ ASSERT (!getABIDecl (proto) && " can't use @abi on inverse protocols" );
4767
4915
inverses.push_back ({existentialSelf, proto, SourceLoc ()});
4768
4916
}
4769
4917
}
0 commit comments