From b540491851910c2efa68196a8bdc2eed1071c17d Mon Sep 17 00:00:00 2001 From: Anders Carlsson Date: Wed, 7 Oct 2009 01:06:45 +0000 Subject: [PATCH] Add a MangleContext and pass it to all mangle functions. It will be used for keeping state, such as identifiers assigned to anonymous structs as well as scope encoding. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@83442 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/CodeGen/CGCXX.cpp | 14 ++++++------ lib/CodeGen/CodeGenModule.cpp | 4 ++-- lib/CodeGen/CodeGenModule.h | 6 ++++- lib/CodeGen/Mangle.cpp | 42 ++++++++++++++++++----------------- lib/CodeGen/Mangle.h | 34 ++++++++++++++++++---------- 5 files changed, 58 insertions(+), 42 deletions(-) diff --git a/lib/CodeGen/CGCXX.cpp b/lib/CodeGen/CGCXX.cpp index bbaa226c21..506f305bf3 100644 --- a/lib/CodeGen/CGCXX.cpp +++ b/lib/CodeGen/CGCXX.cpp @@ -135,7 +135,7 @@ CodeGenFunction::EmitStaticCXXBlockVarDeclInit(const VarDecl &D, llvm::SmallString<256> GuardVName; llvm::raw_svector_ostream GuardVOut(GuardVName); - mangleGuardVariable(&D, getContext(), GuardVOut); + mangleGuardVariable(CGM.getMangleContext(), &D, GuardVOut); // Create the guard variable. llvm::GlobalValue *GuardV = @@ -607,7 +607,7 @@ const char *CodeGenModule::getMangledCXXCtorName(const CXXConstructorDecl *D, CXXCtorType Type) { llvm::SmallString<256> Name; llvm::raw_svector_ostream Out(Name); - mangleCXXCtor(D, Type, Context, Out); + mangleCXXCtor(getMangleContext(), D, Type, Out); Name += '\0'; return UniqueMangledName(Name.begin(), Name.end()); @@ -643,7 +643,7 @@ const char *CodeGenModule::getMangledCXXDtorName(const CXXDestructorDecl *D, CXXDtorType Type) { llvm::SmallString<256> Name; llvm::raw_svector_ostream Out(Name); - mangleCXXDtor(D, Type, Context, Out); + mangleCXXDtor(getMangleContext(), D, Type, Out); Name += '\0'; return UniqueMangledName(Name.begin(), Name.end()); @@ -661,7 +661,7 @@ llvm::Constant *CodeGenModule::GenerateRtti(const CXXRecordDecl *RD) { llvm::raw_svector_ostream Out(OutName); QualType ClassTy; ClassTy = getContext().getTagDeclType(RD); - mangleCXXRtti(ClassTy, getContext(), Out); + mangleCXXRtti(getMangleContext(), ClassTy, Out); llvm::GlobalVariable::LinkageTypes linktype; linktype = llvm::GlobalValue::WeakAnyLinkage; std::vector info; @@ -1187,7 +1187,7 @@ llvm::Value *CodeGenFunction::GenerateVtable(const CXXRecordDecl *RD) { llvm::raw_svector_ostream Out(OutName); QualType ClassTy; ClassTy = getContext().getTagDeclType(RD); - mangleCXXVtable(ClassTy, getContext(), Out); + mangleCXXVtable(CGM.getMangleContext(), ClassTy, Out); llvm::GlobalVariable::LinkageTypes linktype; linktype = llvm::GlobalValue::WeakAnyLinkage; std::vector methods; @@ -1285,7 +1285,7 @@ llvm::Constant *CodeGenModule::BuildThunk(const CXXMethodDecl *MD, bool Extern, int64_t nv, int64_t v) { llvm::SmallString<256> OutName; llvm::raw_svector_ostream Out(OutName); - mangleThunk(MD, nv, v, getContext(), Out); + mangleThunk(getMangleContext(), MD, nv, v, Out); llvm::GlobalVariable::LinkageTypes linktype; linktype = llvm::GlobalValue::WeakAnyLinkage; if (!Extern) @@ -1310,7 +1310,7 @@ llvm::Constant *CodeGenModule::BuildCovariantThunk(const CXXMethodDecl *MD, int64_t v_r) { llvm::SmallString<256> OutName; llvm::raw_svector_ostream Out(OutName); - mangleCovariantThunk(MD, nv_t, v_t, nv_r, v_r, getContext(), Out); + mangleCovariantThunk(getMangleContext(), MD, nv_t, v_t, nv_r, v_r, Out); llvm::GlobalVariable::LinkageTypes linktype; linktype = llvm::GlobalValue::WeakAnyLinkage; if (!Extern) diff --git a/lib/CodeGen/CodeGenModule.cpp b/lib/CodeGen/CodeGenModule.cpp index 50b74f31fa..246beaa403 100644 --- a/lib/CodeGen/CodeGenModule.cpp +++ b/lib/CodeGen/CodeGenModule.cpp @@ -39,7 +39,7 @@ CodeGenModule::CodeGenModule(ASTContext &C, const CompileOptions &compileOpts, Diagnostic &diags) : BlockModule(C, M, TD, Types, *this), Context(C), Features(C.getLangOptions()), CompileOpts(compileOpts), TheModule(M), - TheTargetData(TD), Diags(diags), Types(C, M, TD), Runtime(0), + TheTargetData(TD), Diags(diags), Types(C, M, TD), MangleCtx(C), Runtime(0), MemCpyFn(0), MemMoveFn(0), MemSetFn(0), CFConstantStringClassRef(0), VMContext(M.getContext()) { @@ -166,7 +166,7 @@ const char *CodeGenModule::getMangledName(const NamedDecl *ND) { llvm::SmallString<256> Name; llvm::raw_svector_ostream Out(Name); - if (!mangleName(ND, Context, Out)) { + if (!mangleName(getMangleContext(), ND, Out)) { assert(ND->getIdentifier() && "Attempt to mangle unnamed decl."); return ND->getNameAsCString(); } diff --git a/lib/CodeGen/CodeGenModule.h b/lib/CodeGen/CodeGenModule.h index b46b4ef860..49bb0bffec 100644 --- a/lib/CodeGen/CodeGenModule.h +++ b/lib/CodeGen/CodeGenModule.h @@ -22,6 +22,7 @@ #include "CGCall.h" #include "CGCXX.h" #include "CodeGenTypes.h" +#include "Mangle.h" #include "llvm/Module.h" #include "llvm/ADT/DenseMap.h" #include "llvm/ADT/StringMap.h" @@ -123,9 +124,11 @@ class CodeGenModule : public BlockModule { const llvm::TargetData &TheTargetData; Diagnostic &Diags; CodeGenTypes Types; + MangleContext MangleCtx; + CGObjCRuntime* Runtime; CGDebugInfo* DebugInfo; - + llvm::Function *MemCpyFn; llvm::Function *MemMoveFn; llvm::Function *MemSetFn; @@ -217,6 +220,7 @@ public: const LangOptions &getLangOptions() const { return Features; } llvm::Module &getModule() const { return TheModule; } CodeGenTypes &getTypes() { return Types; } + MangleContext &getMangleContext() { return MangleCtx; } Diagnostic &getDiags() const { return Diags; } const llvm::TargetData &getTargetData() const { return TheTargetData; } llvm::LLVMContext &getLLVMContext() { return VMContext; } diff --git a/lib/CodeGen/Mangle.cpp b/lib/CodeGen/Mangle.cpp index 702edf05a9..269c1130f7 100644 --- a/lib/CodeGen/Mangle.cpp +++ b/lib/CodeGen/Mangle.cpp @@ -29,7 +29,7 @@ using namespace clang; namespace { class VISIBILITY_HIDDEN CXXNameMangler { - ASTContext &Context; + MangleContext &Context; llvm::raw_ostream &Out; const CXXMethodDecl *Structor; @@ -39,7 +39,7 @@ namespace { llvm::DenseMap Substitutions; public: - CXXNameMangler(ASTContext &C, llvm::raw_ostream &os) + CXXNameMangler(MangleContext &C, llvm::raw_ostream &os) : Context(C), Out(os), Structor(0), StructorType(0) { } bool mangle(const NamedDecl *D); @@ -127,12 +127,13 @@ bool CXXNameMangler::mangleFunctionDecl(const FunctionDecl *FD) { // (always). if (!FD->hasAttr()) { // C functions are not mangled, and "main" is never mangled. - if (!Context.getLangOptions().CPlusPlus || FD->isMain()) + if (!Context.getASTContext().getLangOptions().CPlusPlus || FD->isMain()) return false; // No mangling in an "implicit extern C" header. if (FD->getLocation().isValid() && - Context.getSourceManager().isInExternCSystemHeader(FD->getLocation())) + Context.getASTContext().getSourceManager(). + isInExternCSystemHeader(FD->getLocation())) return false; // No name mangling in a C linkage specification. @@ -165,7 +166,7 @@ bool CXXNameMangler::mangle(const NamedDecl *D) { return mangleFunctionDecl(FD); if (const VarDecl *VD = dyn_cast(D)) { - if (!Context.getLangOptions().CPlusPlus || + if (!Context.getASTContext().getLangOptions().CPlusPlus || isInCLinkageSpecification(D) || D->getDeclContext()->isTranslationUnit()) return false; @@ -446,7 +447,7 @@ void CXXNameMangler::mangleUnqualifiedName(const NamedDecl *ND) { case DeclarationName::CXXConversionFunctionName: // ::= cv # (cast) Out << "cv"; - mangleType(Context.getCanonicalType(Name.getCXXNameType())); + mangleType(Context.getASTContext().getCanonicalType(Name.getCXXNameType())); break; case DeclarationName::CXXOperatorName: @@ -671,7 +672,7 @@ void CXXNameMangler::mangleQualifiers(Qualifiers Quals) { void CXXNameMangler::mangleType(QualType T) { // Only operate on the canonical type! - T = Context.getCanonicalType(T); + T = Context.getASTContext().getCanonicalType(T); bool IsSubstitutable = !isa(T); if (IsSubstitutable && mangleSubstitution(T)) @@ -1291,7 +1292,7 @@ namespace clang { /// and this routine will return false. In this case, the caller should just /// emit the identifier of the declaration (\c D->getIdentifier()) as its /// name. - bool mangleName(const NamedDecl *D, ASTContext &Context, + bool mangleName(MangleContext &Context, const NamedDecl *D, llvm::raw_ostream &os) { assert(!isa(D) && "Use mangleCXXCtor for constructor decls!"); @@ -1299,7 +1300,7 @@ namespace clang { "Use mangleCXXDtor for destructor decls!"); PrettyStackTraceDecl CrashInfo(const_cast(D), SourceLocation(), - Context.getSourceManager(), + Context.getASTContext().getSourceManager(), "Mangling declaration"); CXXNameMangler Mangler(Context, os); @@ -1312,8 +1313,8 @@ namespace clang { /// \brief Mangles the a thunk with the offset n for the declaration D and /// emits that name to the given output stream. - void mangleThunk(const FunctionDecl *FD, int64_t nv, int64_t v, - ASTContext &Context, llvm::raw_ostream &os) { + void mangleThunk(MangleContext &Context, const FunctionDecl *FD, + int64_t nv, int64_t v, llvm::raw_ostream &os) { // FIXME: Hum, we might have to thunk these, fix. assert(!isa(FD) && "Use mangleCXXDtor for destructor decls!"); @@ -1325,8 +1326,9 @@ namespace clang { /// \brief Mangles the a covariant thunk for the declaration D and emits that /// name to the given output stream. - void mangleCovariantThunk(const FunctionDecl *FD, int64_t nv_t, int64_t v_t, - int64_t nv_r, int64_t v_r, ASTContext &Context, + void mangleCovariantThunk(MangleContext &Context, const FunctionDecl *FD, + int64_t nv_t, int64_t v_t, + int64_t nv_r, int64_t v_r, llvm::raw_ostream &os) { // FIXME: Hum, we might have to thunk these, fix. assert(!isa(FD) && @@ -1339,7 +1341,7 @@ namespace clang { /// mangleGuardVariable - Returns the mangled name for a guard variable /// for the passed in VarDecl. - void mangleGuardVariable(const VarDecl *D, ASTContext &Context, + void mangleGuardVariable(MangleContext &Context, const VarDecl *D, llvm::raw_ostream &os) { CXXNameMangler Mangler(Context, os); Mangler.mangleGuardVariable(D); @@ -1347,23 +1349,23 @@ namespace clang { os.flush(); } - void mangleCXXCtor(const CXXConstructorDecl *D, CXXCtorType Type, - ASTContext &Context, llvm::raw_ostream &os) { + void mangleCXXCtor(MangleContext &Context, const CXXConstructorDecl *D, + CXXCtorType Type, llvm::raw_ostream &os) { CXXNameMangler Mangler(Context, os); Mangler.mangleCXXCtor(D, Type); os.flush(); } - void mangleCXXDtor(const CXXDestructorDecl *D, CXXDtorType Type, - ASTContext &Context, llvm::raw_ostream &os) { + void mangleCXXDtor(MangleContext &Context, const CXXDestructorDecl *D, + CXXDtorType Type, llvm::raw_ostream &os) { CXXNameMangler Mangler(Context, os); Mangler.mangleCXXDtor(D, Type); os.flush(); } - void mangleCXXVtable(QualType Type, ASTContext &Context, + void mangleCXXVtable(MangleContext &Context, QualType Type, llvm::raw_ostream &os) { CXXNameMangler Mangler(Context, os); Mangler.mangleCXXVtable(Type); @@ -1371,7 +1373,7 @@ namespace clang { os.flush(); } - void mangleCXXRtti(QualType Type, ASTContext &Context, + void mangleCXXRtti(MangleContext &Context, QualType Type, llvm::raw_ostream &os) { CXXNameMangler Mangler(Context, os); Mangler.mangleCXXRtti(Type); diff --git a/lib/CodeGen/Mangle.h b/lib/CodeGen/Mangle.h index 91143a5c4b..8ab5d5edf3 100644 --- a/lib/CodeGen/Mangle.h +++ b/lib/CodeGen/Mangle.h @@ -33,21 +33,31 @@ namespace clang { class NamedDecl; class VarDecl; - bool mangleName(const NamedDecl *D, ASTContext &Context, + class MangleContext { + ASTContext &Context; + public: + explicit MangleContext(ASTContext &Context) + : Context(Context) { } + + ASTContext &getASTContext() const { return Context; } + }; + + bool mangleName(MangleContext &Context, const NamedDecl *D, llvm::raw_ostream &os); - void mangleThunk(const FunctionDecl *FD, int64_t n, int64_t vn, - ASTContext &Context, llvm::raw_ostream &os); - void mangleCovariantThunk(const FunctionDecl *FD, int64_t nv_t, int64_t v_t, - int64_t nv_r, int64_t v_r, ASTContext &Context, + void mangleThunk(MangleContext &Context, const FunctionDecl *FD, + int64_t n, int64_t vn, llvm::raw_ostream &os); + void mangleCovariantThunk(MangleContext &Context, const FunctionDecl *FD, + int64_t nv_t, int64_t v_t, + int64_t nv_r, int64_t v_r, llvm::raw_ostream &os); - void mangleGuardVariable(const VarDecl *D, ASTContext &Context, + void mangleGuardVariable(MangleContext &Context, const VarDecl *D, llvm::raw_ostream &os); - void mangleCXXVtable(QualType T, ASTContext &Context, llvm::raw_ostream &os); - void mangleCXXRtti(QualType T, ASTContext &Context, llvm::raw_ostream &os); - void mangleCXXCtor(const CXXConstructorDecl *D, CXXCtorType Type, - ASTContext &Context, llvm::raw_ostream &os); - void mangleCXXDtor(const CXXDestructorDecl *D, CXXDtorType Type, - ASTContext &Context, llvm::raw_ostream &os); + void mangleCXXVtable(MangleContext &Context, QualType T, llvm::raw_ostream &os); + void mangleCXXRtti(MangleContext &Context, QualType T, llvm::raw_ostream &os); + void mangleCXXCtor(MangleContext &Context, const CXXConstructorDecl *D, + CXXCtorType Type, llvm::raw_ostream &os); + void mangleCXXDtor(MangleContext &Context, const CXXDestructorDecl *D, + CXXDtorType Type, llvm::raw_ostream &os); } #endif -- 2.40.0