Skip to content

Commit 7f6a4da

Browse files
authored
Merge pull request #85062 from hamishknight/not-the-bees
2 parents ba04e5d + c7e9809 commit 7f6a4da

File tree

13 files changed

+145
-70
lines changed

13 files changed

+145
-70
lines changed

include/swift/AST/ASTMangler.h

Lines changed: 42 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,12 @@ class ASTMangler : public Mangler {
106106
/// defined in.
107107
bool RespectOriginallyDefinedIn = true;
108108

109+
/// Whether to always mangle using the declaration's API, ignoring e.g
110+
/// attached `@abi` attributes. This is necessary for USR mangling since for
111+
/// semantic functionality we're only concerned about the API entity, and need
112+
/// to be able to do name lookups to find the original decl based on the USR.
113+
bool UseAPIMangling = false;
114+
109115
public:
110116
class SymbolicReferent {
111117
public:
@@ -187,17 +193,26 @@ class ASTMangler : public Mangler {
187193
HasSymbolQuery,
188194
};
189195

190-
/// lldb overrides the defaulted argument to 'true'.
191-
ASTMangler(const ASTContext &Ctx, bool DWARFMangling = false) : Context(Ctx) {
196+
/// lldb overrides \p DWARFMangling to 'true'.
197+
ASTMangler(const ASTContext &Ctx, bool DWARFMangling = false,
198+
bool UseAPIMangling = false)
199+
: Context(Ctx) {
192200
if (DWARFMangling) {
193-
DWARFMangling = true;
201+
this->DWARFMangling = true;
194202
RespectOriginallyDefinedIn = false;
195203
}
204+
this->UseAPIMangling = UseAPIMangling;
196205
Flavor = Ctx.LangOpts.hasFeature(Feature::Embedded)
197206
? ManglingFlavor::Embedded
198207
: ManglingFlavor::Default;
199208
}
200209

210+
/// Create an ASTMangler suitable for mangling a USR for use in semantic
211+
/// functionality.
212+
static ASTMangler forUSR(const ASTContext &Ctx) {
213+
return ASTMangler(Ctx, /*DWARFMangling*/ true, /*UseAPIMangling*/ true);
214+
}
215+
201216
const ASTContext &getASTContext() { return Context; }
202217

203218
void addTypeSubstitution(Type type, GenericSignature sig) {
@@ -392,9 +407,8 @@ class ASTMangler : public Mangler {
392407
std::string mangleTypeAsContextUSR(const NominalTypeDecl *type);
393408

394409
void appendAnyDecl(const ValueDecl *Decl);
395-
std::string mangleAnyDecl(const ValueDecl *Decl, bool prefix,
396-
bool respectOriginallyDefinedIn = false);
397-
std::string mangleDeclAsUSR(const ValueDecl *Decl, StringRef USRPrefix);
410+
std::string mangleAnyDecl(const ValueDecl *decl, bool addPrefix);
411+
std::string mangleDeclWithPrefix(const ValueDecl *decl, StringRef prefix);
398412

399413
std::string mangleAccessorEntityAsUSR(AccessorKind kind,
400414
const AbstractStorageDecl *decl,
@@ -791,6 +805,28 @@ class ASTMangler : public Mangler {
791805
const ValueDecl *forDecl);
792806

793807
void appendLifetimeDependence(LifetimeDependenceInfo info);
808+
809+
void gatherExistentialRequirements(SmallVectorImpl<Requirement> &reqs,
810+
ParameterizedProtocolType *PPT) const;
811+
812+
/// Extracts a list of inverse requirements from a PCT serving as the
813+
/// constraint type of an existential.
814+
void extractExistentialInverseRequirements(
815+
SmallVectorImpl<InverseRequirement> &inverses,
816+
ProtocolCompositionType *PCT) const;
817+
818+
template <typename DeclType>
819+
DeclType *getABIDecl(DeclType *D) const {
820+
if (!D || UseAPIMangling)
821+
return nullptr;
822+
823+
auto abiRole = ABIRoleInfo(D);
824+
if (!abiRole.providesABI())
825+
return abiRole.getCounterpart();
826+
return nullptr;
827+
}
828+
829+
std::optional<SymbolicReferent> getABIDecl(SymbolicReferent ref) const;
794830
};
795831

796832
} // end namespace Mangle

lib/APIDigester/ModuleAnalyzerNodes.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1120,8 +1120,7 @@ static StringRef calculateMangledName(SDKContext &Ctx, ValueDecl *VD) {
11201120
return Ctx.buffer(attr->Name);
11211121
}
11221122
Mangle::ASTMangler NewMangler(VD->getASTContext());
1123-
return Ctx.buffer(NewMangler.mangleAnyDecl(VD, true,
1124-
/*bool respectOriginallyDefinedIn*/true));
1123+
return Ctx.buffer(NewMangler.mangleAnyDecl(VD, /*addPrefix*/ true));
11251124
}
11261125

11271126
static StringRef calculateLocation(SDKContext &SDKCtx, Decl *D) {

lib/AST/ASTMangler.cpp

Lines changed: 19 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -68,33 +68,22 @@
6868
using namespace swift;
6969
using namespace swift::Mangle;
7070

71-
template<typename DeclType>
72-
static DeclType *getABIDecl(DeclType *D) {
73-
if (!D)
74-
return nullptr;
75-
76-
auto abiRole = ABIRoleInfo(D);
77-
if (!abiRole.providesABI())
78-
return abiRole.getCounterpart();
79-
return nullptr;
80-
}
81-
82-
static std::optional<ASTMangler::SymbolicReferent>
83-
getABIDecl(ASTMangler::SymbolicReferent ref) {
71+
std::optional<ASTMangler::SymbolicReferent>
72+
ASTMangler::getABIDecl(SymbolicReferent ref) const {
8473
switch (ref.getKind()) {
85-
case ASTMangler::SymbolicReferent::NominalType:
74+
case SymbolicReferent::NominalType:
8675
if (auto abiTypeDecl = getABIDecl(ref.getNominalType())) {
87-
return ASTMangler::SymbolicReferent(abiTypeDecl);
76+
return SymbolicReferent(abiTypeDecl);
8877
}
8978
break;
9079

91-
case ASTMangler::SymbolicReferent::OpaqueType:
80+
case SymbolicReferent::OpaqueType:
9281
if (auto abiTypeDecl = getABIDecl(ref.getOpaqueType())) {
93-
return ASTMangler::SymbolicReferent(abiTypeDecl);
82+
return SymbolicReferent(abiTypeDecl);
9483
}
9584
break;
9685

97-
case ASTMangler::SymbolicReferent::ExtendedExistentialTypeShape:
86+
case SymbolicReferent::ExtendedExistentialTypeShape:
9887
// Do nothing; mangling will use the underlying ABI decls in the end.
9988
break;
10089
}
@@ -969,8 +958,6 @@ std::string ASTMangler::mangleTypeAsContextUSR(const NominalTypeDecl *type) {
969958
}
970959

971960
std::string ASTMangler::mangleTypeAsUSR(Type Ty) {
972-
DWARFMangling = true;
973-
RespectOriginallyDefinedIn = false;
974961
beginMangling();
975962

976963
Ty = getTypeForDWARFMangling(Ty);
@@ -1006,42 +993,34 @@ void ASTMangler::appendAnyDecl(const ValueDecl *Decl) {
1006993
}
1007994
}
1008995

1009-
std::string
1010-
ASTMangler::mangleAnyDecl(const ValueDecl *Decl,
1011-
bool prefix,
1012-
bool respectOriginallyDefinedIn) {
996+
std::string ASTMangler::mangleAnyDecl(const ValueDecl *decl, bool addPrefix) {
1013997
DWARFMangling = true;
1014-
RespectOriginallyDefinedIn = respectOriginallyDefinedIn;
1015-
if (prefix) {
998+
if (addPrefix) {
1016999
beginMangling();
10171000
} else {
10181001
beginManglingWithoutPrefix();
10191002
}
10201003
llvm::SaveAndRestore<bool> allowUnnamedRAII(AllowNamelessEntities, true);
10211004

1022-
appendAnyDecl(Decl);
1005+
appendAnyDecl(decl);
10231006

10241007
// We have a custom prefix, so finalize() won't verify for us. If we're not
10251008
// in invalid code (coming from an IDE caller) verify manually.
1026-
if (CONDITIONAL_ASSERT_enabled() && !prefix && !Decl->isInvalid())
1009+
if (CONDITIONAL_ASSERT_enabled() && !addPrefix && !decl->isInvalid())
10271010
verify(Storage.str(), Flavor);
10281011
return finalize();
10291012
}
10301013

1031-
std::string ASTMangler::mangleDeclAsUSR(const ValueDecl *Decl,
1032-
StringRef USRPrefix) {
1033-
llvm::SaveAndRestore<bool> respectOriginallyDefinedInRAII(
1034-
RespectOriginallyDefinedIn, false);
1035-
return (llvm::Twine(USRPrefix) + mangleAnyDecl(Decl, false)).str();
1014+
std::string ASTMangler::mangleDeclWithPrefix(const ValueDecl *decl,
1015+
StringRef prefix) {
1016+
return (llvm::Twine(prefix) + mangleAnyDecl(decl, /*addPrefix*/ false)).str();
10361017
}
10371018

10381019
std::string ASTMangler::mangleAccessorEntityAsUSR(AccessorKind kind,
10391020
const AbstractStorageDecl *decl,
10401021
StringRef USRPrefix,
10411022
bool isStatic) {
10421023
beginManglingWithoutPrefix();
1043-
llvm::SaveAndRestore<bool> respectOriginallyDefinedInRAII(
1044-
RespectOriginallyDefinedIn, false);
10451024
llvm::SaveAndRestore<bool> allowUnnamedRAII(AllowNamelessEntities, true);
10461025
Buffer << USRPrefix;
10471026
appendAccessorEntity(getCodeForAccessorKind(kind), decl, isStatic);
@@ -5329,18 +5308,18 @@ std::string ASTMangler::mangleAttachedMacroExpansion(
53295308
return finalize();
53305309
}
53315310

5332-
static void gatherExistentialRequirements(SmallVectorImpl<Requirement> &reqs,
5333-
ParameterizedProtocolType *PPT) {
5311+
void ASTMangler::gatherExistentialRequirements(
5312+
SmallVectorImpl<Requirement> &reqs, ParameterizedProtocolType *PPT) const {
53345313
auto protoTy = PPT->getBaseType();
53355314
ASSERT(!getABIDecl(protoTy->getDecl()) && "need to figure out behavior");
53365315
PPT->getRequirements(protoTy->getDecl()->getSelfInterfaceType(), reqs);
53375316
}
53385317

53395318
/// Extracts a list of inverse requirements from a PCT serving as the constraint
53405319
/// type of an existential.
5341-
static void extractExistentialInverseRequirements(
5342-
SmallVectorImpl<InverseRequirement> &inverses,
5343-
ProtocolCompositionType *PCT) {
5320+
void ASTMangler::extractExistentialInverseRequirements(
5321+
SmallVectorImpl<InverseRequirement> &inverses,
5322+
ProtocolCompositionType *PCT) const {
53445323
if (!PCT->hasInverse())
53455324
return;
53465325

lib/AST/ASTPrinter.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1348,9 +1348,9 @@ void PrintAST::printAttributes(const Decl *D) {
13481348
if (Options.PrintSyntheticSILGenName
13491349
&& !D->getAttrs().hasAttribute<SILGenNameAttr>()) {
13501350
if (canPrintSyntheticSILGenName(D)) {
1351-
auto mangledName = Mangle::ASTMangler(D->getASTContext())
1352-
.mangleAnyDecl(cast<ValueDecl>(D), /*prefix=*/true,
1353-
/*respectOriginallyDefinedIn=*/true);
1351+
auto mangledName =
1352+
Mangle::ASTMangler(D->getASTContext())
1353+
.mangleAnyDecl(cast<ValueDecl>(D), /*addPrefix*/ true);
13541354
Printer.printAttrName("@_silgen_name");
13551355
Printer << "(";
13561356
Printer.printEscapedStringLiteral(mangledName);

lib/AST/USRGeneration.cpp

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,8 @@ static inline StringRef getUSRSpacePrefix() {
4040
bool ide::printTypeUSR(Type Ty, raw_ostream &OS) {
4141
assert(!Ty->hasArchetype() && "cannot have contextless archetypes mangled.");
4242
Ty = Ty->getCanonicalType()->getRValueType();
43-
Mangle::ASTMangler Mangler(Ty->getASTContext());
43+
using namespace Mangle;
44+
ASTMangler Mangler = ASTMangler::forUSR(Ty->getASTContext());
4445
OS << Mangler.mangleTypeAsUSR(Ty);
4546
return false;
4647
}
@@ -245,8 +246,9 @@ swift::SwiftUSRGenerationRequest::evaluate(Evaluator &evaluator,
245246
if (declIFaceTy.findIf([](Type t) -> bool { return t->is<ModuleType>(); }))
246247
return std::string();
247248

248-
Mangle::ASTMangler NewMangler(D->getASTContext());
249-
return NewMangler.mangleDeclAsUSR(D, getUSRSpacePrefix());
249+
using namespace Mangle;
250+
ASTMangler NewMangler = ASTMangler::forUSR(D->getASTContext());
251+
return NewMangler.mangleDeclWithPrefix(D, getUSRSpacePrefix());
250252
}
251253

252254
std::string
@@ -375,7 +377,8 @@ bool ide::printAccessorUSR(const AbstractStorageDecl *D, AccessorKind AccKind,
375377
return printObjCUSRForAccessor(SD, AccKind, OS);
376378
}
377379

378-
Mangle::ASTMangler NewMangler(D->getASTContext());
380+
using namespace Mangle;
381+
ASTMangler NewMangler = ASTMangler::forUSR(D->getASTContext());
379382
std::string Mangled = NewMangler.mangleAccessorEntityAsUSR(AccKind,
380383
SD, getUSRSpacePrefix(), SD->isStatic());
381384

lib/ClangImporter/SwiftDeclSynthesizer.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -998,7 +998,7 @@ getAccessorDeclarationName(clang::ASTContext &Ctx, NominalTypeDecl *structDecl,
998998
std::string id;
999999
llvm::raw_string_ostream IdStream(id);
10001000
Mangle::ASTMangler mangler(structDecl->getASTContext());
1001-
IdStream << "$" << mangler.mangleDeclAsUSR(structDecl, "") << "$"
1001+
IdStream << "$" << mangler.mangleDeclWithPrefix(structDecl, "") << "$"
10021002
<< fieldDecl->getName() << "$" << suffix;
10031003

10041004
return clang::DeclarationName(&Ctx.Idents.get(IdStream.str()));

lib/PrintAsClang/ModuleContentsWriter.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -155,8 +155,7 @@ static std::string getMangledNameString(const Decl *D) {
155155
if (!VD)
156156
return std::string();
157157
Mangle::ASTMangler mangler(VD->getASTContext());
158-
return mangler.mangleAnyDecl(VD, /*prefix=*/true,
159-
/*respectOriginallyDefinedIn=*/true);
158+
return mangler.mangleAnyDecl(VD, /*addPrefix*/ true);
160159
}
161160

162161
static std::string getTypeString(const ValueDecl *VD) {

lib/Serialization/ModuleFile.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -757,7 +757,7 @@ void ModuleFile::loadDerivativeFunctionConfigurations(
757757
return;
758758
auto &ctx = originalAFD->getASTContext();
759759
Mangle::ASTMangler Mangler(ctx);
760-
auto mangledName = Mangler.mangleDeclAsUSR(originalAFD, "");
760+
auto mangledName = Mangler.mangleDeclWithPrefix(originalAFD, "");
761761
auto configs = Core->DerivativeFunctionConfigurations->find(mangledName);
762762
if (configs == Core->DerivativeFunctionConfigurations->end())
763763
return;

lib/Serialization/Serialization.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6910,14 +6910,15 @@ static void recordDerivativeFunctionConfig(
69106910
auto &ctx = AFD->getASTContext();
69116911
Mangle::ASTMangler Mangler(AFD->getASTContext());
69126912
for (auto *attr : AFD->getAttrs().getAttributes<DifferentiableAttr>()) {
6913-
auto mangledName = ctx.getIdentifier(Mangler.mangleDeclAsUSR(AFD, ""));
6913+
auto mangledName = ctx.getIdentifier(Mangler.mangleDeclWithPrefix(AFD, ""));
69146914
derivativeConfigs[mangledName].insert(
69156915
{ctx.getIdentifier(attr->getParameterIndices()->getString()),
69166916
attr->getDerivativeGenericSignature()});
69176917
}
69186918
for (auto *attr : AFD->getAttrs().getAttributes<DerivativeAttr>()) {
69196919
auto *origAFD = attr->getOriginalFunction(ctx);
6920-
auto mangledName = ctx.getIdentifier(Mangler.mangleDeclAsUSR(origAFD, ""));
6920+
auto mangledName =
6921+
ctx.getIdentifier(Mangler.mangleDeclWithPrefix(origAFD, ""));
69216922
derivativeConfigs[mangledName].insert(
69226923
{ctx.getIdentifier(attr->getParameterIndices()->getString()),
69236924
AFD->getGenericSignature()});

test/Index/index_abi_attr.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// RUN: %target-swift-ide-test -print-indexed-symbols -include-locals -source-filename %s | %FileCheck %s
2+
3+
// Make sure we use the API decl for mangling the USR.
4+
@abi(func bar())
5+
public func foo() {}
6+
// CHECK: [[@LINE-1]]:13 | function/Swift | foo() | s:14swift_ide_test3fooyyF | Def | rel: 0

0 commit comments

Comments
 (0)