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 ())
@@ -1553,6 +1630,9 @@ void ASTMangler::appendType(Type type, GenericSignature sig,
1553
1630
Decl = typeAlias->getDecl ();
1554
1631
else
1555
1632
Decl = type->getAnyGeneric ();
1633
+ if (auto abiDecl = getABIDecl (Decl)) {
1634
+ Decl = abiDecl;
1635
+ }
1556
1636
if (shouldMangleAsGeneric (type)) {
1557
1637
// Try to mangle the entire name as a substitution.
1558
1638
if (tryMangleTypeSubstitution (tybase, sig))
@@ -2050,6 +2130,11 @@ void ASTMangler::appendSymbolicExtendedExistentialType(
2050
2130
Type type,
2051
2131
GenericSignature sig,
2052
2132
const ValueDecl *forDecl) {
2133
+ if (auto abiShapeReferent = getABIDecl (shapeReferent)) {
2134
+ return appendSymbolicExtendedExistentialType (abiShapeReferent.value (), type,
2135
+ sig, forDecl);
2136
+ }
2137
+
2053
2138
assert (shapeReferent.getKind () ==
2054
2139
SymbolicReferent::ExtendedExistentialTypeShape);
2055
2140
assert (canSymbolicReference (shapeReferent));
@@ -2581,6 +2666,7 @@ void ASTMangler::appendContext(const DeclContext *ctx,
2581
2666
void ASTMangler::appendModule (const ModuleDecl *module ,
2582
2667
StringRef useModuleName) {
2583
2668
assert (!module ->getParent () && " cannot mangle nested modules!" );
2669
+ ASSERT (!getABIDecl (module ));
2584
2670
2585
2671
// Use the module real name in mangling; this is the physical name
2586
2672
// of the module on-disk, which can be different if -module-alias is
@@ -2639,6 +2725,10 @@ void ASTMangler::appendProtocolName(const ProtocolDecl *protocol,
2639
2725
bool allowStandardSubstitution) {
2640
2726
assert (AllowMarkerProtocols || !protocol->isMarkerProtocol ());
2641
2727
2728
+ if (auto abiProtocol = getABIDecl (protocol)) {
2729
+ return appendProtocolName (abiProtocol, allowStandardSubstitution);
2730
+ }
2731
+
2642
2732
if (allowStandardSubstitution && tryAppendStandardSubstitution (protocol))
2643
2733
return ;
2644
2734
@@ -2700,6 +2790,10 @@ ASTMangler::getClangDeclForMangling(const ValueDecl *vd) {
2700
2790
}
2701
2791
2702
2792
void ASTMangler::appendSymbolicReference (SymbolicReferent referent) {
2793
+ if (auto abiReferent = getABIDecl (referent)) {
2794
+ return appendSymbolicReference (abiReferent.value ());
2795
+ }
2796
+
2703
2797
// Drop in a placeholder. The real reference value has to be filled in during
2704
2798
// lowering to IR.
2705
2799
auto offset = Buffer.str ().size ();
@@ -2833,6 +2927,10 @@ void ASTMangler::appendContextualInverses(const GenericTypeDecl *contextDecl,
2833
2927
void ASTMangler::appendExtension (const ExtensionDecl* ext,
2834
2928
BaseEntitySignature &base,
2835
2929
StringRef useModuleName) {
2930
+ if (auto abiExt = getABIDecl (ext)) {
2931
+ return appendExtension (abiExt, base, useModuleName);
2932
+ }
2933
+
2836
2934
auto decl = ext->getExtendedNominal ();
2837
2935
// Recover from erroneous extension.
2838
2936
if (!decl)
@@ -2883,6 +2981,10 @@ void ASTMangler::appendAnyGenericType(const GenericTypeDecl *decl) {
2883
2981
2884
2982
void ASTMangler::appendAnyGenericType (const GenericTypeDecl *decl,
2885
2983
BaseEntitySignature &base) {
2984
+ if (auto abiDecl = getABIDecl (decl)) {
2985
+ return appendAnyGenericType (abiDecl);
2986
+ }
2987
+
2886
2988
auto *nominal = dyn_cast<NominalTypeDecl>(decl);
2887
2989
2888
2990
if (nominal && isa<BuiltinTupleDecl>(nominal))
@@ -3795,6 +3897,10 @@ ASTMangler::dropProtocolsFromAssociatedTypes(Type type,
3795
3897
void ASTMangler::appendAssociatedTypeName (DependentMemberType *dmt,
3796
3898
GenericSignature sig) {
3797
3899
if (auto assocTy = dmt->getAssocType ()) {
3900
+ if (auto abiAssocTy = getABIDecl (assocTy)) {
3901
+ assocTy = abiAssocTy;
3902
+ }
3903
+
3798
3904
appendIdentifier (assocTy->getName ().str ());
3799
3905
3800
3906
// If the base type is known to have a single protocol conformance
@@ -3901,6 +4007,10 @@ CanType ASTMangler::getDeclTypeForMangling(
3901
4007
const ValueDecl *decl,
3902
4008
GenericSignature &genericSig,
3903
4009
GenericSignature &parentGenericSig) {
4010
+ if (auto abiDecl = getABIDecl (decl)) {
4011
+ return getDeclTypeForMangling (abiDecl, genericSig, parentGenericSig);
4012
+ }
4013
+
3904
4014
genericSig = GenericSignature ();
3905
4015
parentGenericSig = GenericSignature ();
3906
4016
@@ -4000,6 +4110,10 @@ bool ASTMangler::tryAppendStandardSubstitution(const GenericTypeDecl *decl) {
4000
4110
4001
4111
void ASTMangler::appendConstructorEntity (const ConstructorDecl *ctor,
4002
4112
bool isAllocating) {
4113
+ if (auto abiCtor = getABIDecl (ctor)) {
4114
+ return appendConstructorEntity (abiCtor, isAllocating);
4115
+ }
4116
+
4003
4117
BaseEntitySignature base (ctor);
4004
4118
appendContextOf (ctor, base);
4005
4119
appendDeclType (ctor, base);
@@ -4013,6 +4127,10 @@ void ASTMangler::appendConstructorEntity(const ConstructorDecl *ctor,
4013
4127
4014
4128
void ASTMangler::appendDestructorEntity (const DestructorDecl *dtor,
4015
4129
DestructorKind kind) {
4130
+ if (auto abiDtor = getABIDecl (dtor)) {
4131
+ return appendDestructorEntity (abiDtor, kind);
4132
+ }
4133
+
4016
4134
BaseEntitySignature base (dtor);
4017
4135
appendContextOf (dtor, base);
4018
4136
switch (kind) {
@@ -4031,6 +4149,10 @@ void ASTMangler::appendDestructorEntity(const DestructorDecl *dtor,
4031
4149
void ASTMangler::appendAccessorEntity (StringRef accessorKindCode,
4032
4150
const AbstractStorageDecl *decl,
4033
4151
bool isStatic) {
4152
+ if (auto abiDecl = getABIDecl (decl)) {
4153
+ return appendAccessorEntity (accessorKindCode, abiDecl, isStatic);
4154
+ }
4155
+
4034
4156
BaseEntitySignature base (decl);
4035
4157
appendContextOf (decl, base);
4036
4158
if (auto *varDecl = dyn_cast<VarDecl>(decl)) {
@@ -4058,6 +4180,10 @@ void ASTMangler::appendEntity(const ValueDecl *decl,
4058
4180
BaseEntitySignature &base,
4059
4181
StringRef EntityOp,
4060
4182
bool isStatic) {
4183
+ if (auto abiDecl = getABIDecl (decl)) {
4184
+ return appendEntity (abiDecl, base, EntityOp, isStatic);
4185
+ }
4186
+
4061
4187
appendContextOf (decl, base);
4062
4188
appendDeclName (decl);
4063
4189
appendDeclType (decl, base);
@@ -4069,7 +4195,11 @@ void ASTMangler::appendEntity(const ValueDecl *decl,
4069
4195
void ASTMangler::appendEntity (const ValueDecl *decl) {
4070
4196
assert (!isa<ConstructorDecl>(decl));
4071
4197
assert (!isa<DestructorDecl>(decl));
4072
-
4198
+
4199
+ if (auto abiDecl = getABIDecl (decl)) {
4200
+ return appendEntity (abiDecl);
4201
+ }
4202
+
4073
4203
// Handle accessors specially, they are mangled as modifiers on the accessed
4074
4204
// declaration.
4075
4205
if (auto accessor = dyn_cast<AccessorDecl>(decl)) {
@@ -4424,6 +4554,10 @@ ASTMangler::mangleOpaqueTypeDescriptorRecord(const OpaqueTypeDecl *decl) {
4424
4554
4425
4555
void ASTMangler::appendDistributedThunk (
4426
4556
const AbstractFunctionDecl *thunk, bool asReference) {
4557
+ if (auto abiThunk = getABIDecl (thunk)) {
4558
+ return appendDistributedThunk (abiThunk, asReference);
4559
+ }
4560
+
4427
4561
// Marker protocols cannot be checked at runtime, so there is no point
4428
4562
// in recording them for distributed thunks.
4429
4563
llvm::SaveAndRestore<bool > savedAllowMarkerProtocols (AllowMarkerProtocols,
@@ -4486,6 +4620,9 @@ void ASTMangler::appendDistributedThunk(
4486
4620
if (stubClassLookupResults.size () > 0 ) {
4487
4621
stubActorDecl =
4488
4622
dyn_cast_or_null<NominalTypeDecl>(stubClassLookupResults.front ());
4623
+ if (auto abiStub = getABIDecl (stubActorDecl)) {
4624
+ stubActorDecl = abiStub;
4625
+ }
4489
4626
}
4490
4627
}
4491
4628
@@ -4505,7 +4642,11 @@ void ASTMangler::appendDistributedThunk(
4505
4642
" mangled as thunks" );
4506
4643
// A distributed getter is mangled as the name of its storage (i.e. "the
4507
4644
// var")
4508
- appendIdentifier (accessor->getStorage ()->getBaseIdentifier ().str ());
4645
+ auto storage = accessor->getStorage ();
4646
+ if (auto abiStorage = getABIDecl (storage)) {
4647
+ storage = abiStorage;
4648
+ }
4649
+ appendIdentifier (storage->getBaseIdentifier ().str ());
4509
4650
} else {
4510
4651
appendIdentifier (thunk->getBaseIdentifier ().str ());
4511
4652
}
@@ -4812,6 +4953,10 @@ getPrecheckedLocalContextDiscriminator(const Decl *decl, Identifier name) {
4812
4953
4813
4954
std::string ASTMangler::mangleAttachedMacroExpansion (
4814
4955
const Decl *decl, CustomAttr *attr, MacroRole role) {
4956
+ if (auto abiDecl = getABIDecl (decl)) {
4957
+ return mangleAttachedMacroExpansion (decl, attr, role);
4958
+ }
4959
+
4815
4960
// FIXME(kavon): using the decl causes a cycle. Is a null base fine?
4816
4961
BaseEntitySignature nullBase (nullptr );
4817
4962
@@ -4926,6 +5071,7 @@ std::string ASTMangler::mangleAttachedMacroExpansion(
4926
5071
static void gatherExistentialRequirements (SmallVectorImpl<Requirement> &reqs,
4927
5072
ParameterizedProtocolType *PPT) {
4928
5073
auto protoTy = PPT->getBaseType ();
5074
+ ASSERT (!getABIDecl (protoTy->getDecl ()) && " need to figure out behavior" );
4929
5075
PPT->getRequirements (protoTy->getDecl ()->getSelfInterfaceType (), reqs);
4930
5076
}
4931
5077
@@ -4946,6 +5092,7 @@ static void extractExistentialInverseRequirements(
4946
5092
for (auto ip : PCT->getInverses ()) {
4947
5093
auto *proto = ctx.getProtocol (getKnownProtocolKind (ip));
4948
5094
assert (proto);
5095
+ ASSERT (!getABIDecl (proto) && " can't use @abi on inverse protocols" );
4949
5096
inverses.push_back ({existentialSelf, proto, SourceLoc ()});
4950
5097
}
4951
5098
}
0 commit comments