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 ())) {
@@ -431,6 +483,10 @@ std::string ASTMangler::mangleGlobalInit(const PatternBindingDecl *pd,
431
483
Pattern *pattern = pd->getPattern (pbdEntry);
432
484
bool first = true ;
433
485
pattern->forEachVariable ([&](VarDecl *D) {
486
+ if (auto abiD = getABIDecl (D)) {
487
+ D = abiD;
488
+ }
489
+
434
490
if (first) {
435
491
BaseEntitySignature base (D);
436
492
appendContextOf (D, base);
@@ -519,6 +575,10 @@ std::string ASTMangler::mangleAutoDiffLinearMap(
519
575
520
576
void ASTMangler::beginManglingWithAutoDiffOriginalFunction (
521
577
const AbstractFunctionDecl *afd) {
578
+ if (auto abiAFD = getABIDecl (afd)) {
579
+ return beginManglingWithAutoDiffOriginalFunction (abiAFD);
580
+ }
581
+
522
582
if (auto *attr = afd->getAttrs ().getAttribute <SILGenNameAttr>()) {
523
583
beginManglingWithoutPrefix ();
524
584
appendOperator (attr->Name );
@@ -852,6 +912,10 @@ void ASTMangler::appendAnyDecl(const ValueDecl *Decl) {
852
912
} else if (auto GTD = dyn_cast<GenericTypeDecl>(Decl)) {
853
913
appendAnyGenericType (GTD);
854
914
} else if (isa<AssociatedTypeDecl>(Decl)) {
915
+ if (auto abiDecl = getABIDecl (Decl)) {
916
+ return appendAnyDecl (abiDecl);
917
+ }
918
+
855
919
BaseEntitySignature base (Decl);
856
920
appendContextOf (Decl, base);
857
921
appendDeclName (Decl);
@@ -904,6 +968,10 @@ std::string ASTMangler::mangleAccessorEntityAsUSR(AccessorKind kind,
904
968
}
905
969
906
970
std::string ASTMangler::mangleLocalTypeDecl (const TypeDecl *type) {
971
+ if (auto abiType = getABIDecl (type)) {
972
+ return mangleLocalTypeDecl (abiType);
973
+ }
974
+
907
975
beginManglingWithoutPrefix ();
908
976
AllowNamelessEntities = true ;
909
977
OptimizeProtocolNames = false ;
@@ -954,6 +1022,10 @@ std::string ASTMangler::mangleHasSymbolQuery(const ValueDecl *Decl) {
954
1022
} else if (auto GTD = dyn_cast<GenericTypeDecl>(Decl)) {
955
1023
appendAnyGenericType (GTD);
956
1024
} else if (isa<AssociatedTypeDecl>(Decl)) {
1025
+ if (auto abiDecl = getABIDecl (Decl)) {
1026
+ Decl = abiDecl;
1027
+ }
1028
+
957
1029
BaseEntitySignature nullBase (nullptr );
958
1030
appendContextOf (Decl, nullBase);
959
1031
appendDeclName (Decl);
@@ -1061,6 +1133,7 @@ getOverriddenSwiftProtocolObjCName(const ValueDecl *decl,
1061
1133
}
1062
1134
1063
1135
void ASTMangler::appendDeclName (const ValueDecl *decl, DeclBaseName name) {
1136
+ ASSERT (!getABIDecl (decl) && " caller should make sure we get ABI decls" );
1064
1137
if (name.empty ())
1065
1138
name = decl->getBaseName ();
1066
1139
assert (!name.isSpecial () && " Cannot print special names" );
@@ -1196,6 +1269,10 @@ void ASTMangler::appendExistentialLayout(
1196
1269
bool DroppedRequiresClass = false ;
1197
1270
bool SawRequiresClass = false ;
1198
1271
for (auto proto : layout.getProtocols ()) {
1272
+ if (auto abiProto = getABIDecl (proto)) {
1273
+ proto = abiProto;
1274
+ }
1275
+
1199
1276
// Skip requirements to conform to an invertible protocols.
1200
1277
// We only mangle inverse requirements, but as a constrained existential.
1201
1278
if (proto->getInvertibleProtocolKind ())
@@ -1538,6 +1615,9 @@ void ASTMangler::appendType(Type type, GenericSignature sig,
1538
1615
Decl = typeAlias->getDecl ();
1539
1616
else
1540
1617
Decl = type->getAnyGeneric ();
1618
+ if (auto abiDecl = getABIDecl (Decl)) {
1619
+ Decl = abiDecl;
1620
+ }
1541
1621
if (shouldMangleAsGeneric (type)) {
1542
1622
// Try to mangle the entire name as a substitution.
1543
1623
if (tryMangleTypeSubstitution (tybase, sig))
@@ -2035,6 +2115,11 @@ void ASTMangler::appendSymbolicExtendedExistentialType(
2035
2115
Type type,
2036
2116
GenericSignature sig,
2037
2117
const ValueDecl *forDecl) {
2118
+ if (auto abiShapeReferent = getABIDecl (shapeReferent)) {
2119
+ return appendSymbolicExtendedExistentialType (abiShapeReferent.value (), type,
2120
+ sig, forDecl);
2121
+ }
2122
+
2038
2123
assert (shapeReferent.getKind () ==
2039
2124
SymbolicReferent::ExtendedExistentialTypeShape);
2040
2125
assert (canSymbolicReference (shapeReferent));
@@ -2560,6 +2645,7 @@ void ASTMangler::appendContext(const DeclContext *ctx,
2560
2645
void ASTMangler::appendModule (const ModuleDecl *module,
2561
2646
StringRef useModuleName) {
2562
2647
assert (!module->getParent () && " cannot mangle nested modules!" );
2648
+ ASSERT (!getABIDecl (module));
2563
2649
2564
2650
// Use the module real name in mangling; this is the physical name
2565
2651
// of the module on-disk, which can be different if -module-alias is
@@ -2618,6 +2704,10 @@ void ASTMangler::appendProtocolName(const ProtocolDecl *protocol,
2618
2704
bool allowStandardSubstitution) {
2619
2705
assert (AllowMarkerProtocols || !protocol->isMarkerProtocol ());
2620
2706
2707
+ if (auto abiProtocol = getABIDecl (protocol)) {
2708
+ return appendProtocolName (abiProtocol, allowStandardSubstitution);
2709
+ }
2710
+
2621
2711
if (allowStandardSubstitution && tryAppendStandardSubstitution (protocol))
2622
2712
return ;
2623
2713
@@ -2679,6 +2769,10 @@ ASTMangler::getClangDeclForMangling(const ValueDecl *vd) {
2679
2769
}
2680
2770
2681
2771
void ASTMangler::appendSymbolicReference (SymbolicReferent referent) {
2772
+ if (auto abiReferent = getABIDecl (referent)) {
2773
+ return appendSymbolicReference (abiReferent.value ());
2774
+ }
2775
+
2682
2776
// Drop in a placeholder. The real reference value has to be filled in during
2683
2777
// lowering to IR.
2684
2778
auto offset = Buffer.str ().size ();
@@ -2812,6 +2906,10 @@ void ASTMangler::appendContextualInverses(const GenericTypeDecl *contextDecl,
2812
2906
void ASTMangler::appendExtension (const ExtensionDecl* ext,
2813
2907
BaseEntitySignature &base,
2814
2908
StringRef useModuleName) {
2909
+ if (auto abiExt = getABIDecl (ext)) {
2910
+ return appendExtension (abiExt, base, useModuleName);
2911
+ }
2912
+
2815
2913
auto decl = ext->getExtendedNominal ();
2816
2914
// Recover from erroneous extension.
2817
2915
if (!decl)
@@ -2862,6 +2960,10 @@ void ASTMangler::appendAnyGenericType(const GenericTypeDecl *decl) {
2862
2960
2863
2961
void ASTMangler::appendAnyGenericType (const GenericTypeDecl *decl,
2864
2962
BaseEntitySignature &base) {
2963
+ if (auto abiDecl = getABIDecl (decl)) {
2964
+ return appendAnyGenericType (abiDecl);
2965
+ }
2966
+
2865
2967
auto *nominal = dyn_cast<NominalTypeDecl>(decl);
2866
2968
2867
2969
if (nominal && isa<BuiltinTupleDecl>(nominal))
@@ -3774,6 +3876,10 @@ ASTMangler::dropProtocolsFromAssociatedTypes(Type type,
3774
3876
void ASTMangler::appendAssociatedTypeName (DependentMemberType *dmt,
3775
3877
GenericSignature sig) {
3776
3878
if (auto assocTy = dmt->getAssocType ()) {
3879
+ if (auto abiAssocTy = getABIDecl (assocTy)) {
3880
+ assocTy = abiAssocTy;
3881
+ }
3882
+
3777
3883
appendIdentifier (assocTy->getName ().str ());
3778
3884
3779
3885
// If the base type is known to have a single protocol conformance
@@ -3880,6 +3986,10 @@ CanType ASTMangler::getDeclTypeForMangling(
3880
3986
const ValueDecl *decl,
3881
3987
GenericSignature &genericSig,
3882
3988
GenericSignature &parentGenericSig) {
3989
+ if (auto abiDecl = getABIDecl (decl)) {
3990
+ return getDeclTypeForMangling (abiDecl, genericSig, parentGenericSig);
3991
+ }
3992
+
3883
3993
genericSig = GenericSignature ();
3884
3994
parentGenericSig = GenericSignature ();
3885
3995
@@ -3979,6 +4089,10 @@ bool ASTMangler::tryAppendStandardSubstitution(const GenericTypeDecl *decl) {
3979
4089
3980
4090
void ASTMangler::appendConstructorEntity (const ConstructorDecl *ctor,
3981
4091
bool isAllocating) {
4092
+ if (auto abiCtor = getABIDecl (ctor)) {
4093
+ return appendConstructorEntity (abiCtor, isAllocating);
4094
+ }
4095
+
3982
4096
BaseEntitySignature base (ctor);
3983
4097
appendContextOf (ctor, base);
3984
4098
appendDeclType (ctor, base);
@@ -3992,6 +4106,10 @@ void ASTMangler::appendConstructorEntity(const ConstructorDecl *ctor,
3992
4106
3993
4107
void ASTMangler::appendDestructorEntity (const DestructorDecl *dtor,
3994
4108
DestructorKind kind) {
4109
+ if (auto abiDtor = getABIDecl (dtor)) {
4110
+ return appendDestructorEntity (abiDtor, kind);
4111
+ }
4112
+
3995
4113
BaseEntitySignature base (dtor);
3996
4114
appendContextOf (dtor, base);
3997
4115
switch (kind) {
@@ -4010,6 +4128,10 @@ void ASTMangler::appendDestructorEntity(const DestructorDecl *dtor,
4010
4128
void ASTMangler::appendAccessorEntity (StringRef accessorKindCode,
4011
4129
const AbstractStorageDecl *decl,
4012
4130
bool isStatic) {
4131
+ if (auto abiDecl = getABIDecl (decl)) {
4132
+ return appendAccessorEntity (accessorKindCode, abiDecl, isStatic);
4133
+ }
4134
+
4013
4135
BaseEntitySignature base (decl);
4014
4136
appendContextOf (decl, base);
4015
4137
if (auto *varDecl = dyn_cast<VarDecl>(decl)) {
@@ -4037,6 +4159,10 @@ void ASTMangler::appendEntity(const ValueDecl *decl,
4037
4159
BaseEntitySignature &base,
4038
4160
StringRef EntityOp,
4039
4161
bool isStatic) {
4162
+ if (auto abiDecl = getABIDecl (decl)) {
4163
+ return appendEntity (abiDecl, base, EntityOp, isStatic);
4164
+ }
4165
+
4040
4166
appendContextOf (decl, base);
4041
4167
appendDeclName (decl);
4042
4168
appendDeclType (decl, base);
@@ -4048,7 +4174,11 @@ void ASTMangler::appendEntity(const ValueDecl *decl,
4048
4174
void ASTMangler::appendEntity (const ValueDecl *decl) {
4049
4175
assert (!isa<ConstructorDecl>(decl));
4050
4176
assert (!isa<DestructorDecl>(decl));
4051
-
4177
+
4178
+ if (auto abiDecl = getABIDecl (decl)) {
4179
+ return appendEntity (abiDecl);
4180
+ }
4181
+
4052
4182
// Handle accessors specially, they are mangled as modifiers on the accessed
4053
4183
// declaration.
4054
4184
if (auto accessor = dyn_cast<AccessorDecl>(decl)) {
@@ -4408,6 +4538,10 @@ ASTMangler::mangleOpaqueTypeDescriptorRecord(const OpaqueTypeDecl *decl) {
4408
4538
4409
4539
void ASTMangler::appendDistributedThunk (
4410
4540
const AbstractFunctionDecl *thunk, bool asReference) {
4541
+ if (auto abiThunk = getABIDecl (thunk)) {
4542
+ return appendDistributedThunk (abiThunk, asReference);
4543
+ }
4544
+
4411
4545
// Marker protocols cannot be checked at runtime, so there is no point
4412
4546
// in recording them for distributed thunks.
4413
4547
llvm::SaveAndRestore<bool > savedAllowMarkerProtocols (AllowMarkerProtocols,
@@ -4470,6 +4604,9 @@ void ASTMangler::appendDistributedThunk(
4470
4604
if (stubClassLookupResults.size () > 0 ) {
4471
4605
stubActorDecl =
4472
4606
dyn_cast_or_null<NominalTypeDecl>(stubClassLookupResults.front ());
4607
+ if (auto abiStub = getABIDecl (stubActorDecl)) {
4608
+ stubActorDecl = abiStub;
4609
+ }
4473
4610
}
4474
4611
}
4475
4612
@@ -4489,7 +4626,11 @@ void ASTMangler::appendDistributedThunk(
4489
4626
" mangled as thunks" );
4490
4627
// A distributed getter is mangled as the name of its storage (i.e. "the
4491
4628
// var")
4492
- appendIdentifier (accessor->getStorage ()->getBaseIdentifier ().str ());
4629
+ auto storage = accessor->getStorage ();
4630
+ if (auto abiStorage = getABIDecl (storage)) {
4631
+ storage = abiStorage;
4632
+ }
4633
+ appendIdentifier (storage->getBaseIdentifier ().str ());
4493
4634
} else {
4494
4635
appendIdentifier (thunk->getBaseIdentifier ().str ());
4495
4636
}
@@ -4705,6 +4846,10 @@ ASTMangler::mangleMacroExpansion(const FreestandingMacroExpansion *expansion) {
4705
4846
4706
4847
std::string ASTMangler::mangleAttachedMacroExpansion (
4707
4848
const Decl *decl, CustomAttr *attr, MacroRole role) {
4849
+ if (auto abiDecl = getABIDecl (decl)) {
4850
+ return mangleAttachedMacroExpansion (decl, attr, role);
4851
+ }
4852
+
4708
4853
// FIXME(kavon): using the decl causes a cycle. Is a null base fine?
4709
4854
BaseEntitySignature nullBase (nullptr );
4710
4855
@@ -4791,6 +4936,7 @@ std::string ASTMangler::mangleAttachedMacroExpansion(
4791
4936
static void gatherExistentialRequirements (SmallVectorImpl<Requirement> &reqs,
4792
4937
ParameterizedProtocolType *PPT) {
4793
4938
auto protoTy = PPT->getBaseType ();
4939
+ ASSERT (!getABIDecl (protoTy->getDecl ()) && " need to figure out behavior" );
4794
4940
PPT->getRequirements (protoTy->getDecl ()->getSelfInterfaceType (), reqs);
4795
4941
}
4796
4942
@@ -4811,6 +4957,7 @@ static void extractExistentialInverseRequirements(
4811
4957
for (auto ip : PCT->getInverses ()) {
4812
4958
auto *proto = ctx.getProtocol (getKnownProtocolKind (ip));
4813
4959
assert (proto);
4960
+ ASSERT (!getABIDecl (proto) && " can't use @abi on inverse protocols" );
4814
4961
inverses.push_back ({existentialSelf, proto, SourceLoc ()});
4815
4962
}
4816
4963
}
0 commit comments