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 ())
@@ -1539,6 +1616,9 @@ void ASTMangler::appendType(Type type, GenericSignature sig,
1539
1616
Decl = typeAlias->getDecl ();
1540
1617
else
1541
1618
Decl = type->getAnyGeneric ();
1619
+ if (auto abiDecl = getABIDecl (Decl)) {
1620
+ Decl = abiDecl;
1621
+ }
1542
1622
if (shouldMangleAsGeneric (type)) {
1543
1623
// Try to mangle the entire name as a substitution.
1544
1624
if (tryMangleTypeSubstitution (tybase, sig))
@@ -2020,6 +2100,11 @@ void ASTMangler::appendSymbolicExtendedExistentialType(
2020
2100
Type type,
2021
2101
GenericSignature sig,
2022
2102
const ValueDecl *forDecl) {
2103
+ if (auto abiShapeReferent = getABIDecl (shapeReferent)) {
2104
+ return appendSymbolicExtendedExistentialType (abiShapeReferent.value (), type,
2105
+ sig, forDecl);
2106
+ }
2107
+
2023
2108
assert (shapeReferent.getKind () ==
2024
2109
SymbolicReferent::ExtendedExistentialTypeShape);
2025
2110
assert (canSymbolicReference (shapeReferent));
@@ -2545,6 +2630,7 @@ void ASTMangler::appendContext(const DeclContext *ctx,
2545
2630
void ASTMangler::appendModule (const ModuleDecl *module ,
2546
2631
StringRef useModuleName) {
2547
2632
assert (!module ->getParent () && " cannot mangle nested modules!" );
2633
+ ASSERT (!getABIDecl (module ));
2548
2634
2549
2635
// Use the module real name in mangling; this is the physical name
2550
2636
// of the module on-disk, which can be different if -module-alias is
@@ -2603,6 +2689,10 @@ void ASTMangler::appendProtocolName(const ProtocolDecl *protocol,
2603
2689
bool allowStandardSubstitution) {
2604
2690
assert (AllowMarkerProtocols || !protocol->isMarkerProtocol ());
2605
2691
2692
+ if (auto abiProtocol = getABIDecl (protocol)) {
2693
+ return appendProtocolName (abiProtocol, allowStandardSubstitution);
2694
+ }
2695
+
2606
2696
if (allowStandardSubstitution && tryAppendStandardSubstitution (protocol))
2607
2697
return ;
2608
2698
@@ -2664,6 +2754,10 @@ ASTMangler::getClangDeclForMangling(const ValueDecl *vd) {
2664
2754
}
2665
2755
2666
2756
void ASTMangler::appendSymbolicReference (SymbolicReferent referent) {
2757
+ if (auto abiReferent = getABIDecl (referent)) {
2758
+ return appendSymbolicReference (abiReferent.value ());
2759
+ }
2760
+
2667
2761
// Drop in a placeholder. The real reference value has to be filled in during
2668
2762
// lowering to IR.
2669
2763
auto offset = Buffer.str ().size ();
@@ -2797,6 +2891,10 @@ void ASTMangler::appendContextualInverses(const GenericTypeDecl *contextDecl,
2797
2891
void ASTMangler::appendExtension (const ExtensionDecl* ext,
2798
2892
BaseEntitySignature &base,
2799
2893
StringRef useModuleName) {
2894
+ if (auto abiExt = getABIDecl (ext)) {
2895
+ return appendExtension (abiExt, base, useModuleName);
2896
+ }
2897
+
2800
2898
auto decl = ext->getExtendedNominal ();
2801
2899
// Recover from erroneous extension.
2802
2900
if (!decl)
@@ -2847,6 +2945,10 @@ void ASTMangler::appendAnyGenericType(const GenericTypeDecl *decl) {
2847
2945
2848
2946
void ASTMangler::appendAnyGenericType (const GenericTypeDecl *decl,
2849
2947
BaseEntitySignature &base) {
2948
+ if (auto abiDecl = getABIDecl (decl)) {
2949
+ return appendAnyGenericType (abiDecl);
2950
+ }
2951
+
2850
2952
auto *nominal = dyn_cast<NominalTypeDecl>(decl);
2851
2953
2852
2954
if (nominal && isa<BuiltinTupleDecl>(nominal))
@@ -3745,6 +3847,10 @@ ASTMangler::dropProtocolsFromAssociatedTypes(Type type,
3745
3847
void ASTMangler::appendAssociatedTypeName (DependentMemberType *dmt,
3746
3848
GenericSignature sig) {
3747
3849
if (auto assocTy = dmt->getAssocType ()) {
3850
+ if (auto abiAssocTy = getABIDecl (assocTy)) {
3851
+ assocTy = abiAssocTy;
3852
+ }
3853
+
3748
3854
appendIdentifier (assocTy->getName ().str ());
3749
3855
3750
3856
// If the base type is known to have a single protocol conformance
@@ -3851,6 +3957,10 @@ CanType ASTMangler::getDeclTypeForMangling(
3851
3957
const ValueDecl *decl,
3852
3958
GenericSignature &genericSig,
3853
3959
GenericSignature &parentGenericSig) {
3960
+ if (auto abiDecl = getABIDecl (decl)) {
3961
+ return getDeclTypeForMangling (abiDecl, genericSig, parentGenericSig);
3962
+ }
3963
+
3854
3964
genericSig = GenericSignature ();
3855
3965
parentGenericSig = GenericSignature ();
3856
3966
@@ -3950,6 +4060,10 @@ bool ASTMangler::tryAppendStandardSubstitution(const GenericTypeDecl *decl) {
3950
4060
3951
4061
void ASTMangler::appendConstructorEntity (const ConstructorDecl *ctor,
3952
4062
bool isAllocating) {
4063
+ if (auto abiCtor = getABIDecl (ctor)) {
4064
+ return appendConstructorEntity (abiCtor, isAllocating);
4065
+ }
4066
+
3953
4067
BaseEntitySignature base (ctor);
3954
4068
appendContextOf (ctor, base);
3955
4069
appendDeclType (ctor, base);
@@ -3963,6 +4077,10 @@ void ASTMangler::appendConstructorEntity(const ConstructorDecl *ctor,
3963
4077
3964
4078
void ASTMangler::appendDestructorEntity (const DestructorDecl *dtor,
3965
4079
DestructorKind kind) {
4080
+ if (auto abiDtor = getABIDecl (dtor)) {
4081
+ return appendDestructorEntity (abiDtor, kind);
4082
+ }
4083
+
3966
4084
BaseEntitySignature base (dtor);
3967
4085
appendContextOf (dtor, base);
3968
4086
switch (kind) {
@@ -3981,6 +4099,10 @@ void ASTMangler::appendDestructorEntity(const DestructorDecl *dtor,
3981
4099
void ASTMangler::appendAccessorEntity (StringRef accessorKindCode,
3982
4100
const AbstractStorageDecl *decl,
3983
4101
bool isStatic) {
4102
+ if (auto abiDecl = getABIDecl (decl)) {
4103
+ return appendAccessorEntity (accessorKindCode, abiDecl, isStatic);
4104
+ }
4105
+
3984
4106
BaseEntitySignature base (decl);
3985
4107
appendContextOf (decl, base);
3986
4108
if (auto *varDecl = dyn_cast<VarDecl>(decl)) {
@@ -4008,6 +4130,10 @@ void ASTMangler::appendEntity(const ValueDecl *decl,
4008
4130
BaseEntitySignature &base,
4009
4131
StringRef EntityOp,
4010
4132
bool isStatic) {
4133
+ if (auto abiDecl = getABIDecl (decl)) {
4134
+ return appendEntity (abiDecl, base, EntityOp, isStatic);
4135
+ }
4136
+
4011
4137
appendContextOf (decl, base);
4012
4138
appendDeclName (decl);
4013
4139
appendDeclType (decl, base);
@@ -4019,7 +4145,11 @@ void ASTMangler::appendEntity(const ValueDecl *decl,
4019
4145
void ASTMangler::appendEntity (const ValueDecl *decl) {
4020
4146
assert (!isa<ConstructorDecl>(decl));
4021
4147
assert (!isa<DestructorDecl>(decl));
4022
-
4148
+
4149
+ if (auto abiDecl = getABIDecl (decl)) {
4150
+ return appendEntity (abiDecl);
4151
+ }
4152
+
4023
4153
// Handle accessors specially, they are mangled as modifiers on the accessed
4024
4154
// declaration.
4025
4155
if (auto accessor = dyn_cast<AccessorDecl>(decl)) {
@@ -4379,6 +4509,10 @@ ASTMangler::mangleOpaqueTypeDescriptorRecord(const OpaqueTypeDecl *decl) {
4379
4509
4380
4510
void ASTMangler::appendDistributedThunk (
4381
4511
const AbstractFunctionDecl *thunk, bool asReference) {
4512
+ if (auto abiThunk = getABIDecl (thunk)) {
4513
+ return appendDistributedThunk (abiThunk, asReference);
4514
+ }
4515
+
4382
4516
// Marker protocols cannot be checked at runtime, so there is no point
4383
4517
// in recording them for distributed thunks.
4384
4518
llvm::SaveAndRestore<bool > savedAllowMarkerProtocols (AllowMarkerProtocols,
@@ -4425,6 +4559,10 @@ void ASTMangler::appendDistributedThunk(
4425
4559
};
4426
4560
4427
4561
if (auto *P = referenceInProtocolContextOrRequirement ()) {
4562
+ if (auto abiP = getABIDecl (P)) {
4563
+ P = abiP;
4564
+ }
4565
+
4428
4566
appendContext (P->getDeclContext (), base,
4429
4567
thunk->getAlternateModuleName ());
4430
4568
appendIdentifier (Twine (" $" , P->getNameStr ()).str ());
@@ -4439,7 +4577,11 @@ void ASTMangler::appendDistributedThunk(
4439
4577
" mangled as thunks" );
4440
4578
// A distributed getter is mangled as the name of its storage (i.e. "the
4441
4579
// var")
4442
- appendIdentifier (accessor->getStorage ()->getBaseIdentifier ().str ());
4580
+ auto storage = accessor->getStorage ();
4581
+ if (auto abiStorage = getABIDecl (storage)) {
4582
+ storage = abiStorage;
4583
+ }
4584
+ appendIdentifier (storage->getBaseIdentifier ().str ());
4443
4585
} else {
4444
4586
appendIdentifier (thunk->getBaseIdentifier ().str ());
4445
4587
}
@@ -4654,6 +4796,10 @@ ASTMangler::mangleMacroExpansion(const FreestandingMacroExpansion *expansion) {
4654
4796
4655
4797
std::string ASTMangler::mangleAttachedMacroExpansion (
4656
4798
const Decl *decl, CustomAttr *attr, MacroRole role) {
4799
+ if (auto abiDecl = getABIDecl (decl)) {
4800
+ return mangleAttachedMacroExpansion (decl, attr, role);
4801
+ }
4802
+
4657
4803
// FIXME(kavon): using the decl causes a cycle. Is a null base fine?
4658
4804
BaseEntitySignature nullBase (nullptr );
4659
4805
@@ -4740,6 +4886,7 @@ std::string ASTMangler::mangleAttachedMacroExpansion(
4740
4886
static void gatherExistentialRequirements (SmallVectorImpl<Requirement> &reqs,
4741
4887
ParameterizedProtocolType *PPT) {
4742
4888
auto protoTy = PPT->getBaseType ();
4889
+ ASSERT (!getABIDecl (protoTy->getDecl ()) && " need to figure out behavior" );
4743
4890
PPT->getRequirements (protoTy->getDecl ()->getSelfInterfaceType (), reqs);
4744
4891
}
4745
4892
@@ -4760,6 +4907,7 @@ static void extractExistentialInverseRequirements(
4760
4907
for (auto ip : PCT->getInverses ()) {
4761
4908
auto *proto = ctx.getProtocol (getKnownProtocolKind (ip));
4762
4909
assert (proto);
4910
+ ASSERT (!getABIDecl (proto) && " can't use @abi on inverse protocols" );
4763
4911
inverses.push_back ({existentialSelf, proto, SourceLoc ()});
4764
4912
}
4765
4913
}
0 commit comments