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.

llvm-svn: 83442
This commit is contained in:
Anders Carlsson
2009-10-07 01:06:45 +00:00
parent e666bc9f64
commit ff971e8bf4
5 changed files with 58 additions and 42 deletions

View File

@@ -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<llvm::Constant *> 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<llvm::Constant *> 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)

View File

@@ -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();
}

View File

@@ -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; }

View File

@@ -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<uintptr_t, unsigned> 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<OverloadableAttr>()) {
// 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<VarDecl>(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:
// <operator-name> ::= cv <type> # (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<BuiltinType>(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<CXXConstructorDecl>(D) &&
"Use mangleCXXCtor for constructor decls!");
@@ -1299,7 +1300,7 @@ namespace clang {
"Use mangleCXXDtor for destructor decls!");
PrettyStackTraceDecl CrashInfo(const_cast<NamedDecl *>(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<CXXDestructorDecl>(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<CXXDestructorDecl>(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);

View File

@@ -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