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 );
@@ -838,6 +898,10 @@ void ASTMangler::appendAnyDecl(const ValueDecl *Decl) {
838
898
} else if (auto GTD = dyn_cast<GenericTypeDecl>(Decl)) {
839
899
appendAnyGenericType (GTD);
840
900
} else if (isa<AssociatedTypeDecl>(Decl)) {
901
+ if (auto abiDecl = getABIDecl (Decl)) {
902
+ return appendAnyDecl (abiDecl);
903
+ }
904
+
841
905
BaseEntitySignature base (Decl);
842
906
appendContextOf (Decl, base);
843
907
appendDeclName (Decl);
@@ -890,6 +954,10 @@ std::string ASTMangler::mangleAccessorEntityAsUSR(AccessorKind kind,
890
954
}
891
955
892
956
std::string ASTMangler::mangleLocalTypeDecl (const TypeDecl *type) {
957
+ if (auto abiType = getABIDecl (type)) {
958
+ return mangleLocalTypeDecl (abiType);
959
+ }
960
+
893
961
beginManglingWithoutPrefix ();
894
962
AllowNamelessEntities = true ;
895
963
OptimizeProtocolNames = false ;
@@ -940,6 +1008,10 @@ std::string ASTMangler::mangleHasSymbolQuery(const ValueDecl *Decl) {
940
1008
} else if (auto GTD = dyn_cast<GenericTypeDecl>(Decl)) {
941
1009
appendAnyGenericType (GTD);
942
1010
} else if (isa<AssociatedTypeDecl>(Decl)) {
1011
+ if (auto abiDecl = getABIDecl (Decl)) {
1012
+ Decl = abiDecl;
1013
+ }
1014
+
943
1015
BaseEntitySignature nullBase (nullptr );
944
1016
appendContextOf (Decl, nullBase);
945
1017
appendDeclName (Decl);
@@ -1047,6 +1119,7 @@ getOverriddenSwiftProtocolObjCName(const ValueDecl *decl,
1047
1119
}
1048
1120
1049
1121
void ASTMangler::appendDeclName (const ValueDecl *decl, DeclBaseName name) {
1122
+ ASSERT (!getABIDecl (decl) && " caller should make sure we get ABI decls" );
1050
1123
if (name.empty ())
1051
1124
name = decl->getBaseName ();
1052
1125
assert (!name.isSpecial () && " Cannot print special names" );
@@ -1182,6 +1255,10 @@ void ASTMangler::appendExistentialLayout(
1182
1255
bool DroppedRequiresClass = false ;
1183
1256
bool SawRequiresClass = false ;
1184
1257
for (auto proto : layout.getProtocols ()) {
1258
+ if (auto abiProto = getABIDecl (proto)) {
1259
+ proto = abiProto;
1260
+ }
1261
+
1185
1262
// Skip requirements to conform to an invertible protocols.
1186
1263
// We only mangle inverse requirements, but as a constrained existential.
1187
1264
if (proto->getInvertibleProtocolKind ())
@@ -1520,6 +1597,9 @@ void ASTMangler::appendType(Type type, GenericSignature sig,
1520
1597
Decl = typeAlias->getDecl ();
1521
1598
else
1522
1599
Decl = type->getAnyGeneric ();
1600
+ if (auto abiDecl = getABIDecl (Decl)) {
1601
+ Decl = abiDecl;
1602
+ }
1523
1603
if (shouldMangleAsGeneric (type)) {
1524
1604
// Try to mangle the entire name as a substitution.
1525
1605
if (tryMangleTypeSubstitution (tybase, sig))
@@ -2001,6 +2081,11 @@ void ASTMangler::appendSymbolicExtendedExistentialType(
2001
2081
Type type,
2002
2082
GenericSignature sig,
2003
2083
const ValueDecl *forDecl) {
2084
+ if (auto abiShapeReferent = getABIDecl (shapeReferent)) {
2085
+ return appendSymbolicExtendedExistentialType (abiShapeReferent.value (), type,
2086
+ sig, forDecl);
2087
+ }
2088
+
2004
2089
assert (shapeReferent.getKind () ==
2005
2090
SymbolicReferent::ExtendedExistentialTypeShape);
2006
2091
assert (canSymbolicReference (shapeReferent));
@@ -2523,6 +2608,7 @@ void ASTMangler::appendContext(const DeclContext *ctx,
2523
2608
void ASTMangler::appendModule (const ModuleDecl *module ,
2524
2609
StringRef useModuleName) {
2525
2610
assert (!module ->getParent () && " cannot mangle nested modules!" );
2611
+ ASSERT (!getABIDecl (module ));
2526
2612
2527
2613
// Use the module real name in mangling; this is the physical name
2528
2614
// of the module on-disk, which can be different if -module-alias is
@@ -2581,6 +2667,10 @@ void ASTMangler::appendProtocolName(const ProtocolDecl *protocol,
2581
2667
bool allowStandardSubstitution) {
2582
2668
assert (AllowMarkerProtocols || !protocol->isMarkerProtocol ());
2583
2669
2670
+ if (auto abiProtocol = getABIDecl (protocol)) {
2671
+ return appendProtocolName (abiProtocol, allowStandardSubstitution);
2672
+ }
2673
+
2584
2674
if (allowStandardSubstitution && tryAppendStandardSubstitution (protocol))
2585
2675
return ;
2586
2676
@@ -2642,6 +2732,10 @@ ASTMangler::getClangDeclForMangling(const ValueDecl *vd) {
2642
2732
}
2643
2733
2644
2734
void ASTMangler::appendSymbolicReference (SymbolicReferent referent) {
2735
+ if (auto abiReferent = getABIDecl (referent)) {
2736
+ return appendSymbolicReference (abiReferent.value ());
2737
+ }
2738
+
2645
2739
// Drop in a placeholder. The real reference value has to be filled in during
2646
2740
// lowering to IR.
2647
2741
auto offset = Buffer.str ().size ();
@@ -2775,6 +2869,10 @@ void ASTMangler::appendContextualInverses(const GenericTypeDecl *contextDecl,
2775
2869
void ASTMangler::appendExtension (const ExtensionDecl* ext,
2776
2870
BaseEntitySignature &base,
2777
2871
StringRef useModuleName) {
2872
+ if (auto abiExt = getABIDecl (ext)) {
2873
+ return appendExtension (abiExt, base, useModuleName);
2874
+ }
2875
+
2778
2876
auto decl = ext->getExtendedNominal ();
2779
2877
// Recover from erroneous extension.
2780
2878
if (!decl)
@@ -2825,6 +2923,10 @@ void ASTMangler::appendAnyGenericType(const GenericTypeDecl *decl) {
2825
2923
2826
2924
void ASTMangler::appendAnyGenericType (const GenericTypeDecl *decl,
2827
2925
BaseEntitySignature &base) {
2926
+ if (auto abiDecl = getABIDecl (decl)) {
2927
+ return appendAnyGenericType (abiDecl);
2928
+ }
2929
+
2828
2930
auto *nominal = dyn_cast<NominalTypeDecl>(decl);
2829
2931
2830
2932
if (nominal && isa<BuiltinTupleDecl>(nominal))
@@ -3723,6 +3825,10 @@ ASTMangler::dropProtocolsFromAssociatedTypes(Type type,
3723
3825
void ASTMangler::appendAssociatedTypeName (DependentMemberType *dmt,
3724
3826
GenericSignature sig) {
3725
3827
if (auto assocTy = dmt->getAssocType ()) {
3828
+ if (auto abiAssocTy = getABIDecl (assocTy)) {
3829
+ assocTy = abiAssocTy;
3830
+ }
3831
+
3726
3832
appendIdentifier (assocTy->getName ().str ());
3727
3833
3728
3834
// If the base type is known to have a single protocol conformance
@@ -3829,6 +3935,10 @@ CanType ASTMangler::getDeclTypeForMangling(
3829
3935
const ValueDecl *decl,
3830
3936
GenericSignature &genericSig,
3831
3937
GenericSignature &parentGenericSig) {
3938
+ if (auto abiDecl = getABIDecl (decl)) {
3939
+ return getDeclTypeForMangling (abiDecl, genericSig, parentGenericSig);
3940
+ }
3941
+
3832
3942
genericSig = GenericSignature ();
3833
3943
parentGenericSig = GenericSignature ();
3834
3944
@@ -3928,6 +4038,10 @@ bool ASTMangler::tryAppendStandardSubstitution(const GenericTypeDecl *decl) {
3928
4038
3929
4039
void ASTMangler::appendConstructorEntity (const ConstructorDecl *ctor,
3930
4040
bool isAllocating) {
4041
+ if (auto abiCtor = getABIDecl (ctor)) {
4042
+ return appendConstructorEntity (abiCtor, isAllocating);
4043
+ }
4044
+
3931
4045
BaseEntitySignature base (ctor);
3932
4046
appendContextOf (ctor, base);
3933
4047
appendDeclType (ctor, base);
@@ -3941,6 +4055,10 @@ void ASTMangler::appendConstructorEntity(const ConstructorDecl *ctor,
3941
4055
3942
4056
void ASTMangler::appendDestructorEntity (const DestructorDecl *dtor,
3943
4057
DestructorKind kind) {
4058
+ if (auto abiDtor = getABIDecl (dtor)) {
4059
+ return appendDestructorEntity (abiDtor, kind);
4060
+ }
4061
+
3944
4062
BaseEntitySignature base (dtor);
3945
4063
appendContextOf (dtor, base);
3946
4064
switch (kind) {
@@ -3959,6 +4077,10 @@ void ASTMangler::appendDestructorEntity(const DestructorDecl *dtor,
3959
4077
void ASTMangler::appendAccessorEntity (StringRef accessorKindCode,
3960
4078
const AbstractStorageDecl *decl,
3961
4079
bool isStatic) {
4080
+ if (auto abiDecl = getABIDecl (decl)) {
4081
+ return appendAccessorEntity (accessorKindCode, abiDecl, isStatic);
4082
+ }
4083
+
3962
4084
BaseEntitySignature base (decl);
3963
4085
appendContextOf (decl, base);
3964
4086
if (auto *varDecl = dyn_cast<VarDecl>(decl)) {
@@ -3986,6 +4108,10 @@ void ASTMangler::appendEntity(const ValueDecl *decl,
3986
4108
BaseEntitySignature &base,
3987
4109
StringRef EntityOp,
3988
4110
bool isStatic) {
4111
+ if (auto abiDecl = getABIDecl (decl)) {
4112
+ return appendEntity (abiDecl, base, EntityOp, isStatic);
4113
+ }
4114
+
3989
4115
appendContextOf (decl, base);
3990
4116
appendDeclName (decl);
3991
4117
appendDeclType (decl, base);
@@ -3997,7 +4123,11 @@ void ASTMangler::appendEntity(const ValueDecl *decl,
3997
4123
void ASTMangler::appendEntity (const ValueDecl *decl) {
3998
4124
assert (!isa<ConstructorDecl>(decl));
3999
4125
assert (!isa<DestructorDecl>(decl));
4000
-
4126
+
4127
+ if (auto abiDecl = getABIDecl (decl)) {
4128
+ return appendEntity (abiDecl);
4129
+ }
4130
+
4001
4131
// Handle accessors specially, they are mangled as modifiers on the accessed
4002
4132
// declaration.
4003
4133
if (auto accessor = dyn_cast<AccessorDecl>(decl)) {
@@ -4357,6 +4487,10 @@ ASTMangler::mangleOpaqueTypeDescriptorRecord(const OpaqueTypeDecl *decl) {
4357
4487
4358
4488
void ASTMangler::appendDistributedThunk (
4359
4489
const AbstractFunctionDecl *thunk, bool asReference) {
4490
+ if (auto abiThunk = getABIDecl (thunk)) {
4491
+ return appendDistributedThunk (abiThunk, asReference);
4492
+ }
4493
+
4360
4494
// Marker protocols cannot be checked at runtime, so there is no point
4361
4495
// in recording them for distributed thunks.
4362
4496
llvm::SaveAndRestore<bool > savedAllowMarkerProtocols (AllowMarkerProtocols,
@@ -4403,6 +4537,10 @@ void ASTMangler::appendDistributedThunk(
4403
4537
};
4404
4538
4405
4539
if (auto *P = referenceInProtocolContextOrRequirement ()) {
4540
+ if (auto abiP = getABIDecl (P)) {
4541
+ P = abiP;
4542
+ }
4543
+
4406
4544
appendContext (P->getDeclContext (), base,
4407
4545
thunk->getAlternateModuleName ());
4408
4546
appendIdentifier (Twine (" $" , P->getNameStr ()).str ());
@@ -4417,7 +4555,11 @@ void ASTMangler::appendDistributedThunk(
4417
4555
" mangled as thunks" );
4418
4556
// A distributed getter is mangled as the name of its storage (i.e. "the
4419
4557
// var")
4420
- appendIdentifier (accessor->getStorage ()->getBaseIdentifier ().str ());
4558
+ auto storage = accessor->getStorage ();
4559
+ if (auto abiStorage = getABIDecl (storage)) {
4560
+ storage = abiStorage;
4561
+ }
4562
+ appendIdentifier (storage->getBaseIdentifier ().str ());
4421
4563
} else {
4422
4564
appendIdentifier (thunk->getBaseIdentifier ().str ());
4423
4565
}
@@ -4632,6 +4774,10 @@ ASTMangler::mangleMacroExpansion(const FreestandingMacroExpansion *expansion) {
4632
4774
4633
4775
std::string ASTMangler::mangleAttachedMacroExpansion (
4634
4776
const Decl *decl, CustomAttr *attr, MacroRole role) {
4777
+ if (auto abiDecl = getABIDecl (decl)) {
4778
+ return mangleAttachedMacroExpansion (decl, attr, role);
4779
+ }
4780
+
4635
4781
// FIXME(kavon): using the decl causes a cycle. Is a null base fine?
4636
4782
BaseEntitySignature nullBase (nullptr );
4637
4783
@@ -4718,6 +4864,7 @@ std::string ASTMangler::mangleAttachedMacroExpansion(
4718
4864
static void gatherExistentialRequirements (SmallVectorImpl<Requirement> &reqs,
4719
4865
ParameterizedProtocolType *PPT) {
4720
4866
auto protoTy = PPT->getBaseType ();
4867
+ ASSERT (!getABIDecl (protoTy->getDecl ()) && " need to figure out behavior" );
4721
4868
PPT->getRequirements (protoTy->getDecl ()->getSelfInterfaceType (), reqs);
4722
4869
}
4723
4870
@@ -4738,6 +4885,7 @@ static void extractExistentialInverseRequirements(
4738
4885
for (auto ip : PCT->getInverses ()) {
4739
4886
auto *proto = ctx.getProtocol (getKnownProtocolKind (ip));
4740
4887
assert (proto);
4888
+ ASSERT (!getABIDecl (proto) && " can't use @abi on inverse protocols" );
4741
4889
inverses.push_back ({existentialSelf, proto, SourceLoc ()});
4742
4890
}
4743
4891
}
0 commit comments