Skip to content

Commit 84b5c86

Browse files
committed
[ASTMangler] Pass ASTContext to all instantiations of ASTMangler
1 parent d1a26d0 commit 84b5c86

File tree

87 files changed

+339
-321
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

87 files changed

+339
-321
lines changed

include/swift/AST/ASTMangler.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#ifndef SWIFT_AST_ASTMANGLER_H
1414
#define SWIFT_AST_ASTMANGLER_H
1515

16+
#include "swift/AST/ASTContext.h"
1617
#include "swift/AST/Decl.h"
1718
#include "swift/AST/FreestandingMacroExpansion.h"
1819
#include "swift/AST/Types.h"
@@ -43,6 +44,7 @@ enum class DestructorKind {
4344
/// The mangler for AST declarations.
4445
class ASTMangler : public Mangler {
4546
protected:
47+
const ASTContext &Context;
4648
ModuleDecl *Mod = nullptr;
4749

4850
/// Optimize out protocol names if a type only conforms to one protocol.
@@ -185,13 +187,15 @@ class ASTMangler : public Mangler {
185187
};
186188

187189
/// lldb overrides the defaulted argument to 'true'.
188-
ASTMangler(bool DWARFMangling = false) {
190+
ASTMangler(const ASTContext &Ctx, bool DWARFMangling = false) : Context(Ctx) {
189191
if (DWARFMangling) {
190192
DWARFMangling = true;
191193
RespectOriginallyDefinedIn = false;
192194
}
193195
}
194196

197+
const ASTContext &getASTContext() { return Context; }
198+
195199
void addTypeSubstitution(Type type, GenericSignature sig) {
196200
type = dropProtocolsFromAssociatedTypes(type, sig);
197201
addSubstitution(type.getPointer());

include/swift/IRGen/Linking.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1452,9 +1452,9 @@ class LinkEntity {
14521452
return entity;
14531453
}
14541454

1455-
void mangle(llvm::raw_ostream &out) const;
1456-
void mangle(SmallVectorImpl<char> &buffer) const;
1457-
std::string mangleAsString() const;
1455+
void mangle(ASTContext &Ctx, llvm::raw_ostream &out) const;
1456+
void mangle(ASTContext &Ctx, SmallVectorImpl<char> &buffer) const;
1457+
std::string mangleAsString(ASTContext &Ctx) const;
14581458

14591459
SILDeclRef getSILDeclRef() const;
14601460
SILLinkage getLinkage(ForDefinition_t isDefinition) const;

include/swift/SIL/GenericSpecializationMangler.h

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#ifndef SWIFT_SIL_UTILS_GENERICSPECIALIZATIONMANGLER_H
1414
#define SWIFT_SIL_UTILS_GENERICSPECIALIZATIONMANGLER_H
1515

16+
#include "swift/AST/ASTContext.h"
1617
#include "swift/AST/ASTMangler.h"
1718
#include "swift/AST/Effects.h"
1819
#include "swift/Basic/NullablePtr.h"
@@ -50,14 +51,14 @@ class SpecializationMangler : public Mangle::ASTMangler {
5051
PossibleEffects RemovedEffects;
5152

5253
protected:
53-
SpecializationMangler(SpecializationPass P, swift::SerializedKind_t Serialized,
54+
SpecializationMangler(ASTContext &Ctx, SpecializationPass P, swift::SerializedKind_t Serialized,
5455
SILFunction *F)
55-
: Pass(P), Serialized(Serialized), Function(F),
56+
: ASTMangler(Ctx), Pass(P), Serialized(Serialized), Function(F),
5657
ArgOpBuffer(ArgOpStorage) {}
5758

58-
SpecializationMangler(SpecializationPass P, swift::SerializedKind_t Serialized,
59+
SpecializationMangler(ASTContext &Ctx, SpecializationPass P, swift::SerializedKind_t Serialized,
5960
std::string functionName)
60-
: Pass(P), Serialized(Serialized), Function(nullptr),
61+
: ASTMangler(Ctx), Pass(P), Serialized(Serialized), Function(nullptr),
6162
FunctionName(functionName), ArgOpBuffer(ArgOpStorage) {}
6263

6364
void beginMangling();
@@ -73,8 +74,8 @@ class SpecializationMangler : public Mangle::ASTMangler {
7374
// The mangler for specialized generic functions.
7475
class GenericSpecializationMangler : public SpecializationMangler {
7576

76-
GenericSpecializationMangler(std::string origFuncName)
77-
: SpecializationMangler(SpecializationPass::GenericSpecializer,
77+
GenericSpecializationMangler(ASTContext &Ctx, std::string origFuncName)
78+
: SpecializationMangler(Ctx, SpecializationPass::GenericSpecializer,
7879
IsNotSerialized, origFuncName) {}
7980

8081
GenericSignature getGenericSignature() {
@@ -90,8 +91,8 @@ class GenericSpecializationMangler : public SpecializationMangler {
9091
void appendRemovedParams(const SmallBitVector &paramsRemoved);
9192

9293
public:
93-
GenericSpecializationMangler(SILFunction *F, swift::SerializedKind_t Serialized)
94-
: SpecializationMangler(SpecializationPass::GenericSpecializer,
94+
GenericSpecializationMangler(ASTContext &Ctx, SILFunction *F, swift::SerializedKind_t Serialized)
95+
: SpecializationMangler(Ctx, SpecializationPass::GenericSpecializer,
9596
Serialized, F) {}
9697

9798
std::string mangleNotReabstracted(SubstitutionMap subs,
@@ -118,7 +119,7 @@ class GenericSpecializationMangler : public SpecializationMangler {
118119
return manglePrespecialized(getGenericSignature(), subs);
119120
}
120121

121-
static std::string manglePrespecialization(std::string unspecializedName,
122+
static std::string manglePrespecialization(ASTContext &Ctx, std::string unspecializedName,
122123
GenericSignature genericSig,
123124
GenericSignature specializedSig);
124125
};

include/swift/SIL/TypeSubstCloner.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -383,7 +383,7 @@ class TypeSubstCloner : public SILClonerWithScopes<ImplClass> {
383383
return ParentFunction;
384384

385385
// Clone the function with the substituted type for the debug info.
386-
Mangle::GenericSpecializationMangler Mangler(ParentFunction,
386+
Mangle::GenericSpecializationMangler Mangler(M.getASTContext(), ParentFunction,
387387
IsNotSerialized);
388388
std::string MangledName =
389389
Mangler.mangleForDebugInfo(RemappedSig, SubsMap, ForInlining);

include/swift/SILOptimizer/Utils/DifferentiationMangler.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#ifndef SWIFT_SIL_UTILS_DIFFERENTIATIONMANGLER_H
1414
#define SWIFT_SIL_UTILS_DIFFERENTIATIONMANGLER_H
1515

16+
#include "swift/AST/ASTContext.h"
1617
#include "swift/AST/ASTMangler.h"
1718
#include "swift/AST/AutoDiff.h"
1819
#include "swift/Basic/NullablePtr.h"
@@ -25,7 +26,7 @@ namespace Mangle {
2526
/// A mangler for generated differentiation functions.
2627
class DifferentiationMangler : public ASTMangler {
2728
public:
28-
DifferentiationMangler() {}
29+
DifferentiationMangler(const ASTContext &Ctx) : ASTMangler(Ctx) {}
2930
/// Returns the mangled name for a differentiation function of the given kind.
3031
std::string mangleAutoDiffFunction(StringRef originalName,
3132
Demangle::AutoDiffFunctionKind kind,

include/swift/SILOptimizer/Utils/SpecializationMangler.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,10 @@ class PartialSpecializationMangler : public SpecializationMangler {
3232
bool isReAbstracted;
3333

3434
public:
35-
PartialSpecializationMangler(SILFunction *F,
35+
PartialSpecializationMangler(ASTContext &Ctx, SILFunction *F,
3636
CanSILFunctionType SpecializedFnTy,
3737
swift::SerializedKind_t Serialized, bool isReAbstracted)
38-
: SpecializationMangler(SpecializationPass::GenericSpecializer,
38+
: SpecializationMangler(Ctx, SpecializationPass::GenericSpecializer,
3939
Serialized, F),
4040
SpecializedFnTy(SpecializedFnTy), isReAbstracted(isReAbstracted) {}
4141

@@ -92,7 +92,7 @@ class FunctionSignatureSpecializationMangler : public SpecializationMangler {
9292
ReturnValueModifierIntBase ReturnValue;
9393

9494
public:
95-
FunctionSignatureSpecializationMangler(SpecializationPass Pass,
95+
FunctionSignatureSpecializationMangler(ASTContext &Ctx, SpecializationPass Pass,
9696
swift::SerializedKind_t Serialized,
9797
SILFunction *F);
9898
// For arguments / return values

include/swift/Serialization/SerializedSILLoader.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ class SILDifferentiabilityWitness;
4242
/// on each SILDeserializer.
4343
class SerializedSILLoader {
4444
private:
45+
ASTContext &Context;
4546
std::vector<std::unique_ptr<SILDeserializer>> LoadedSILSections;
4647

4748
explicit SerializedSILLoader(

lib/APIDigester/ModuleAnalyzerNodes.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1118,7 +1118,7 @@ static StringRef calculateMangledName(SDKContext &Ctx, ValueDecl *VD) {
11181118
if (auto *attr = VD->getAttrs().getAttribute<SILGenNameAttr>()) {
11191119
return Ctx.buffer(attr->Name);
11201120
}
1121-
Mangle::ASTMangler NewMangler;
1121+
Mangle::ASTMangler NewMangler(VD->getASTContext());
11221122
return Ctx.buffer(NewMangler.mangleAnyDecl(VD, true,
11231123
/*bool respectOriginallyDefinedIn*/true));
11241124
}

lib/AST/ASTMangler.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -575,7 +575,7 @@ void ASTMangler::appendIndexSubset(IndexSubset *indices) {
575575

576576
static NodePointer mangleSILDifferentiabilityWitnessAsNode(
577577
StringRef originalName, DifferentiabilityKind kind,
578-
const AutoDiffConfig &config, Demangler &demangler) {
578+
const AutoDiffConfig &config, Demangler &demangler, ASTMangler *mangler) {
579579
auto *diffWitnessNode = demangler.createNode(
580580
Node::Kind::DifferentiabilityWitness);
581581
auto origNode = demangler.demangleSymbol(originalName);
@@ -596,7 +596,7 @@ static NodePointer mangleSILDifferentiabilityWitnessAsNode(
596596
Node::Kind::IndexSubset, config.resultIndices->getString()),
597597
demangler);
598598
if (auto genSig = config.derivativeGenericSignature) {
599-
ASTMangler genSigMangler;
599+
ASTMangler genSigMangler(mangler->getASTContext());
600600
auto genSigSymbol = genSigMangler.mangleGenericSignature(genSig);
601601
auto demangledGenSig = demangler.demangleSymbol(genSigSymbol);
602602
assert(demangledGenSig);
@@ -616,7 +616,7 @@ std::string ASTMangler::mangleSILDifferentiabilityWitness(StringRef originalName
616616
if (isMangledName(originalName)) {
617617
Demangler demangler;
618618
auto *node = mangleSILDifferentiabilityWitnessAsNode(
619-
originalName, kind, config, demangler);
619+
originalName, kind, config, demangler, this);
620620
auto mangling = mangleNode(node);
621621
if (!mangling.isSuccess()) {
622622
llvm_unreachable("unexpected mangling failure");

lib/AST/ASTPrinter.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3147,32 +3147,32 @@ std::optional<std::string> PrintAST::mangledNameToPrint(const Decl *D) {
31473147

31483148
// For functions, mangle the entity directly.
31493149
if (auto func = dyn_cast<FuncDecl>(D)) {
3150-
ASTMangler mangler;
3150+
ASTMangler mangler(D->getASTContext());
31513151
return mangler.mangleEntity(func);
31523152
}
31533153

31543154
// For initializers, mangle the allocating initializer.
31553155
if (auto init = dyn_cast<ConstructorDecl>(D)) {
3156-
ASTMangler mangler;
3156+
ASTMangler mangler(D->getASTContext());
31573157
return mangler.mangleConstructorEntity(init, /*isAllocating=*/true);
31583158
}
31593159

31603160
// For variables, mangle the entity directly.
31613161
if (auto var = dyn_cast<VarDecl>(D)) {
3162-
ASTMangler mangler;
3162+
ASTMangler mangler(D->getASTContext());
31633163
return mangler.mangleEntity(var);
31643164
}
31653165

31663166
// For subscripts, mangle the entity directly.
31673167
if (auto subscript = dyn_cast<SubscriptDecl>(D)) {
3168-
ASTMangler mangler;
3168+
ASTMangler mangler(D->getASTContext());
31693169
return mangler.mangleEntity(subscript);
31703170
}
31713171

31723172
// For nominal types, mangle the type metadata accessor.
31733173
if (auto nominal = dyn_cast<NominalTypeDecl>(D)) {
31743174
if (!isa<ProtocolDecl>(nominal) && !nominal->getGenericSignature()) {
3175-
ASTMangler mangler;
3175+
ASTMangler mangler(D->getASTContext());
31763176
std::string name = mangler.mangleNominalType(nominal);
31773177
name += "Ma";
31783178
return name;

lib/AST/Decl.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
12
//===--- Decl.cpp - Swift Language Decl ASTs ------------------------------===//
23
//
34
// This source file is part of the Swift.org open source project
@@ -3852,7 +3853,7 @@ OpaqueTypeDecl *ValueDecl::getOpaqueResultTypeDecl() const {
38523853
auto file = cast<FileUnit>(getDeclContext()->getModuleScopeContext());
38533854
// Don't look up when the decl is from source, otherwise a cycle will happen.
38543855
if (file->getKind() == FileUnitKind::SerializedAST) {
3855-
Mangle::ASTMangler mangler;
3856+
Mangle::ASTMangler mangler(getASTContext());
38563857
auto name = mangler.mangleOpaqueTypeDecl(this);
38573858
return file->lookupOpaqueResultType(name);
38583859
}
@@ -6174,7 +6175,7 @@ ClassDecl::MetaclassKind ClassDecl::getMetaclassKind() const {
61746175
static StringRef mangleObjCRuntimeName(const NominalTypeDecl *nominal,
61756176
llvm::SmallVectorImpl<char> &buffer) {
61766177
{
6177-
Mangle::ASTMangler Mangler;
6178+
Mangle::ASTMangler Mangler(nominal->getASTContext());
61786179
std::string MangledName = Mangler.mangleObjCRuntimeName(nominal);
61796180

61806181
buffer.clear();
@@ -10031,7 +10032,7 @@ Identifier OpaqueTypeDecl::getOpaqueReturnTypeIdentifier() const {
1003110032
SmallString<64> mangleBuf;
1003210033
{
1003310034
llvm::raw_svector_ostream os(mangleBuf);
10034-
Mangle::ASTMangler mangler;
10035+
Mangle::ASTMangler mangler(getASTContext());
1003510036
os << mangler.mangleOpaqueTypeDecl(this);
1003610037
}
1003710038

lib/AST/FrontendSourceFileDepGraphFactory.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ using namespace fine_grained_dependencies;
6161
static std::string identifierForContext(const DeclContext *DC) {
6262
if (!DC) return "";
6363

64-
Mangle::ASTMangler Mangler;
64+
Mangle::ASTMangler Mangler(DC->getASTContext());
6565
if (const auto *context = dyn_cast<NominalTypeDecl>(DC)) {
6666
return Mangler.mangleTypeAsContextUSR(context);
6767
}

lib/AST/USRGeneration.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,13 @@ static inline StringRef getUSRSpacePrefix() {
3939

4040
bool ide::printTypeUSR(Type Ty, raw_ostream &OS) {
4141
assert(!Ty->hasArchetype() && "cannot have contextless archetypes mangled.");
42-
Mangle::ASTMangler Mangler;
42+
Mangle::ASTMangler Mangler(Ty->getASTContext());
4343
OS << Mangler.mangleTypeAsUSR(Ty->getRValueType());
4444
return false;
4545
}
4646

4747
bool ide::printDeclTypeUSR(const ValueDecl *D, raw_ostream &OS) {
48-
Mangle::ASTMangler Mangler;
48+
Mangle::ASTMangler Mangler(D->getASTContext());
4949
std::string MangledName = Mangler.mangleDeclType(D);
5050
OS << MangledName;
5151
return false;
@@ -254,7 +254,7 @@ swift::USRGenerationRequest::evaluate(Evaluator &evaluator,
254254
}))
255255
return std::string();
256256

257-
Mangle::ASTMangler NewMangler;
257+
Mangle::ASTMangler NewMangler(D->getASTContext());
258258
return NewMangler.mangleDeclAsUSR(D, getUSRSpacePrefix());
259259
}
260260

@@ -276,7 +276,7 @@ swift::MangleLocalTypeDeclRequest::evaluate(Evaluator &evaluator,
276276
if (isa<ModuleDecl>(D))
277277
return std::string(); // Ignore.
278278

279-
Mangle::ASTMangler NewMangler;
279+
Mangle::ASTMangler NewMangler(D->getASTContext());
280280
return NewMangler.mangleLocalTypeDecl(D);
281281
}
282282

@@ -320,7 +320,7 @@ bool ide::printAccessorUSR(const AbstractStorageDecl *D, AccessorKind AccKind,
320320
return printObjCUSRForAccessor(SD, AccKind, OS);
321321
}
322322

323-
Mangle::ASTMangler NewMangler;
323+
Mangle::ASTMangler NewMangler(D->getASTContext());
324324
std::string Mangled = NewMangler.mangleAccessorEntityAsUSR(AccKind,
325325
SD, getUSRSpacePrefix(), SD->isStatic());
326326

lib/ClangImporter/SwiftDeclSynthesizer.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -954,7 +954,7 @@ getAccessorDeclarationName(clang::ASTContext &Ctx, NominalTypeDecl *structDecl,
954954
VarDecl *fieldDecl, const char *suffix) {
955955
std::string id;
956956
llvm::raw_string_ostream IdStream(id);
957-
Mangle::ASTMangler mangler;
957+
Mangle::ASTMangler mangler(structDecl->getASTContext());
958958
IdStream << "$" << mangler.mangleDeclAsUSR(structDecl, "") << "$"
959959
<< fieldDecl->getName() << "$" << suffix;
960960

lib/ConstExtract/ConstExtract.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ std::string toMangledTypeNameString(const swift::Type &Type) {
111111
auto PrintingType = Type;
112112
if (Type->hasArchetype())
113113
PrintingType = Type->mapTypeOutOfContext();
114-
return Mangle::ASTMangler().mangleTypeWithoutPrefix(PrintingType->getCanonicalType());
114+
return Mangle::ASTMangler(Type->getASTContext()).mangleTypeWithoutPrefix(PrintingType->getCanonicalType());
115115
}
116116

117117
} // namespace

lib/IRGen/GenClass.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -711,7 +711,7 @@ irgen::getPhysicalClassMemberAccessStrategy(IRGenModule &IGM,
711711

712712
case FieldAccess::NonConstantDirect: {
713713
std::string symbol =
714-
LinkEntity::forFieldOffset(field).mangleAsString();
714+
LinkEntity::forFieldOffset(field).mangleAsString(IGM.Context);
715715
return MemberAccessStrategy::getDirectGlobal(std::move(symbol),
716716
MemberAccessStrategy::OffsetKind::Bytes_Word);
717717
}
@@ -2337,7 +2337,7 @@ namespace {
23372337
llvm::raw_svector_ostream os(buffer);
23382338
os << LinkEntity::forTypeMetadata(*prespecialization,
23392339
TypeMetadataAddress::FullMetadata)
2340-
.mangleAsString();
2340+
.mangleAsString(IGM.Context);
23412341
return os.str();
23422342
}
23432343

@@ -3053,7 +3053,7 @@ llvm::MDString *irgen::typeIdForMethod(IRGenModule &IGM, SILDeclRef method) {
30533053
assert(!method.getOverridden() && "must always be base method");
30543054

30553055
auto entity = LinkEntity::forMethodDescriptor(method);
3056-
auto mangled = entity.mangleAsString();
3056+
auto mangled = entity.mangleAsString(IGM.Context);
30573057
auto typeId = llvm::MDString::get(*IGM.LLVMContext, mangled);
30583058
return typeId;
30593059
}

0 commit comments

Comments
 (0)