diff options
author | Peter Collingbourne <peter@pcc.me.uk> | 2011-01-13 18:57:25 +0000 |
---|---|---|
committer | Peter Collingbourne <peter@pcc.me.uk> | 2011-01-13 18:57:25 +0000 |
commit | 0ff0b3762766d4e2d13a76cd9dea080195b73a5d (patch) | |
tree | 05ba8dfe749d5b6669bcebaded7354f5465f8194 | |
parent | 018778af3dac8e1930c59768bbbd2db41307fe1f (diff) | |
download | bcm5719-llvm-0ff0b3762766d4e2d13a76cd9dea080195b73a5d.tar.gz bcm5719-llvm-0ff0b3762766d4e2d13a76cd9dea080195b73a5d.zip |
Move name mangling support from CodeGen to AST. In the
process, perform a number of refactorings:
- Move MiscNameMangler member functions to MangleContext
- Remove GlobalDecl dependency from MangleContext
- Make MangleContext abstract and move Itanium/Microsoft functionality
to their own classes/files
- Implement ASTContext::createMangleContext and have CodeGen use it
No (intended) functionality change.
llvm-svn: 123386
24 files changed, 1652 insertions, 1481 deletions
diff --git a/clang/include/clang/AST/ASTContext.h b/clang/include/clang/AST/ASTContext.h index e97f96972d3..a0544ef8bdd 100644 --- a/clang/include/clang/AST/ASTContext.h +++ b/clang/include/clang/AST/ASTContext.h @@ -57,6 +57,7 @@ namespace clang { class CXXRecordDecl; class Decl; class FieldDecl; + class MangleContext; class ObjCIvarDecl; class ObjCIvarRefExpr; class ObjCPropertyDecl; @@ -1058,6 +1059,8 @@ public: bool isNearlyEmpty(const CXXRecordDecl *RD) const; + MangleContext *createMangleContext(); + void ShallowCollectObjCIvars(const ObjCInterfaceDecl *OI, llvm::SmallVectorImpl<ObjCIvarDecl*> &Ivars) const; diff --git a/clang/lib/CodeGen/Mangle.h b/clang/include/clang/AST/Mangle.h index 402a9b8cc35..8dbca8daede 100644 --- a/clang/lib/CodeGen/Mangle.h +++ b/clang/include/clang/AST/Mangle.h @@ -7,20 +7,15 @@ // //===----------------------------------------------------------------------===// // -// Implements C++ name mangling according to the Itanium C++ ABI, -// which is used in GCC 3.2 and newer (and many compilers that are -// ABI-compatible with GCC): -// -// http://www.codesourcery.com/public/cxx-abi/abi.html +// Defines the C++ name mangling interface. // //===----------------------------------------------------------------------===// -#ifndef LLVM_CLANG_CODEGEN_MANGLE_H -#define LLVM_CLANG_CODEGEN_MANGLE_H +#ifndef LLVM_CLANG_AST_MANGLE_H +#define LLVM_CLANG_AST_MANGLE_H -#include "CGCXX.h" -#include "GlobalDecl.h" #include "clang/AST/Type.h" +#include "clang/Basic/ABI.h" #include "llvm/ADT/DenseMap.h" #include "llvm/ADT/StringRef.h" #include "llvm/ADT/SmallString.h" @@ -36,8 +31,6 @@ namespace clang { class NamedDecl; class ObjCMethodDecl; class VarDecl; - -namespace CodeGen { struct ThisAdjustment; struct ThunkInfo; @@ -74,9 +67,6 @@ class MangleContext { ASTContext &Context; Diagnostic &Diags; - llvm::DenseMap<const TagDecl *, uint64_t> AnonStructIds; - unsigned Discriminator; - llvm::DenseMap<const NamedDecl*, unsigned> Uniquifier; llvm::DenseMap<const BlockDecl*, unsigned> GlobalBlockIds; llvm::DenseMap<const BlockDecl*, unsigned> LocalBlockIds; @@ -91,15 +81,8 @@ public: Diagnostic &getDiags() const { return Diags; } - void startNewFunction() { LocalBlockIds.clear(); } + virtual void startNewFunction() { LocalBlockIds.clear(); } - uint64_t getAnonymousStructId(const TagDecl *TD) { - std::pair<llvm::DenseMap<const TagDecl *, - uint64_t>::iterator, bool> Result = - AnonStructIds.insert(std::make_pair(TD, AnonStructIds.size())); - return Result.first->second; - } - unsigned getBlockId(const BlockDecl *BD, bool Local) { llvm::DenseMap<const BlockDecl *, unsigned> &BlockIds = Local? LocalBlockIds : GlobalBlockIds; @@ -111,69 +94,57 @@ public: /// @name Mangler Entry Points /// @{ - virtual bool shouldMangleDeclName(const NamedDecl *D); - virtual void mangleName(const NamedDecl *D, llvm::SmallVectorImpl<char> &); + virtual bool shouldMangleDeclName(const NamedDecl *D) = 0; + virtual void mangleName(const NamedDecl *D, llvm::SmallVectorImpl<char> &)=0; virtual void mangleThunk(const CXXMethodDecl *MD, const ThunkInfo &Thunk, - llvm::SmallVectorImpl<char> &); + llvm::SmallVectorImpl<char> &) = 0; virtual void mangleCXXDtorThunk(const CXXDestructorDecl *DD, CXXDtorType Type, const ThisAdjustment &ThisAdjustment, - llvm::SmallVectorImpl<char> &); + llvm::SmallVectorImpl<char> &) = 0; virtual void mangleReferenceTemporary(const VarDecl *D, - llvm::SmallVectorImpl<char> &); + llvm::SmallVectorImpl<char> &) = 0; virtual void mangleCXXVTable(const CXXRecordDecl *RD, - llvm::SmallVectorImpl<char> &); + llvm::SmallVectorImpl<char> &) = 0; virtual void mangleCXXVTT(const CXXRecordDecl *RD, - llvm::SmallVectorImpl<char> &); + llvm::SmallVectorImpl<char> &) = 0; virtual void mangleCXXCtorVTable(const CXXRecordDecl *RD, int64_t Offset, const CXXRecordDecl *Type, - llvm::SmallVectorImpl<char> &); - virtual void mangleCXXRTTI(QualType T, llvm::SmallVectorImpl<char> &); - virtual void mangleCXXRTTIName(QualType T, llvm::SmallVectorImpl<char> &); + llvm::SmallVectorImpl<char> &) = 0; + virtual void mangleCXXRTTI(QualType T, llvm::SmallVectorImpl<char> &) = 0; + virtual void mangleCXXRTTIName(QualType T, llvm::SmallVectorImpl<char> &) = 0; virtual void mangleCXXCtor(const CXXConstructorDecl *D, CXXCtorType Type, - llvm::SmallVectorImpl<char> &); + llvm::SmallVectorImpl<char> &) = 0; virtual void mangleCXXDtor(const CXXDestructorDecl *D, CXXDtorType Type, - llvm::SmallVectorImpl<char> &); - void mangleBlock(GlobalDecl GD, - const BlockDecl *BD, llvm::SmallVectorImpl<char> &); + llvm::SmallVectorImpl<char> &) = 0; + + void mangleGlobalBlock(const BlockDecl *BD, + llvm::SmallVectorImpl<char> &Res); + void mangleCtorBlock(const CXXConstructorDecl *CD, CXXCtorType CT, + const BlockDecl *BD, llvm::SmallVectorImpl<char> &Res); + void mangleDtorBlock(const CXXDestructorDecl *CD, CXXDtorType DT, + const BlockDecl *BD, llvm::SmallVectorImpl<char> &Res); + void mangleBlock(const DeclContext *DC, const BlockDecl *BD, + llvm::SmallVectorImpl<char> &Res); + // Do the right thing. + void mangleBlock(const BlockDecl *BD, llvm::SmallVectorImpl<char> &Res); + + void mangleObjCMethodName(const ObjCMethodDecl *MD, + llvm::SmallVectorImpl<char> &); // This is pretty lame. - void mangleItaniumGuardVariable(const VarDecl *D, - llvm::SmallVectorImpl<char> &); - - void mangleInitDiscriminator() { - Discriminator = 0; - } - - bool getNextDiscriminator(const NamedDecl *ND, unsigned &disc) { - unsigned &discriminator = Uniquifier[ND]; - if (!discriminator) - discriminator = ++Discriminator; - if (discriminator == 1) - return false; - disc = discriminator-2; - return true; + virtual void mangleItaniumGuardVariable(const VarDecl *D, + llvm::SmallVectorImpl<char> &) { + assert(0 && "Target does not support mangling guard variables"); } /// @} }; -/// MiscNameMangler - Mangles Objective-C method names and blocks. -class MiscNameMangler { - MangleContext &Context; - llvm::raw_svector_ostream Out; - - ASTContext &getASTContext() const { return Context.getASTContext(); } - -public: - MiscNameMangler(MangleContext &C, llvm::SmallVectorImpl<char> &Res); - - llvm::raw_svector_ostream &getStream() { return Out; } - - void mangleBlock(GlobalDecl GD, const BlockDecl *BD); - void mangleObjCMethodName(const ObjCMethodDecl *MD); -}; +MangleContext *createItaniumMangleContext(ASTContext &Context, + Diagnostic &Diags); +MangleContext *createMicrosoftMangleContext(ASTContext &Context, + Diagnostic &Diags); } -} #endif diff --git a/clang/include/clang/Basic/ABI.h b/clang/include/clang/Basic/ABI.h new file mode 100644 index 00000000000..2a05958496f --- /dev/null +++ b/clang/include/clang/Basic/ABI.h @@ -0,0 +1,126 @@ +//===----- ABI.h - ABI related declarations ---------------------*- C++ -*-===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// +// +// These enums/classes describe ABI related information about constructors, +// destructors and thunks. +// +//===----------------------------------------------------------------------===// + +#ifndef CLANG_BASIC_ABI_H +#define CLANG_BASIC_ABI_H + +#include <stdint.h> + +namespace clang { + +/// CXXCtorType - C++ constructor types +enum CXXCtorType { + Ctor_Complete, // Complete object ctor + Ctor_Base, // Base object ctor + Ctor_CompleteAllocating // Complete object allocating ctor +}; + +/// CXXDtorType - C++ destructor types +enum CXXDtorType { + Dtor_Deleting, // Deleting dtor + Dtor_Complete, // Complete object dtor + Dtor_Base // Base object dtor +}; + +/// ReturnAdjustment - A return adjustment. +struct ReturnAdjustment { + /// NonVirtual - The non-virtual adjustment from the derived object to its + /// nearest virtual base. + int64_t NonVirtual; + + /// VBaseOffsetOffset - The offset (in bytes), relative to the address point + /// of the virtual base class offset. + int64_t VBaseOffsetOffset; + + ReturnAdjustment() : NonVirtual(0), VBaseOffsetOffset(0) { } + + bool isEmpty() const { return !NonVirtual && !VBaseOffsetOffset; } + + friend bool operator==(const ReturnAdjustment &LHS, + const ReturnAdjustment &RHS) { + return LHS.NonVirtual == RHS.NonVirtual && + LHS.VBaseOffsetOffset == RHS.VBaseOffsetOffset; + } + + friend bool operator<(const ReturnAdjustment &LHS, + const ReturnAdjustment &RHS) { + if (LHS.NonVirtual < RHS.NonVirtual) + return true; + + return LHS.NonVirtual == RHS.NonVirtual && + LHS.VBaseOffsetOffset < RHS.VBaseOffsetOffset; + } +}; + +/// ThisAdjustment - A 'this' pointer adjustment. +struct ThisAdjustment { + /// NonVirtual - The non-virtual adjustment from the derived object to its + /// nearest virtual base. + int64_t NonVirtual; + + /// VCallOffsetOffset - The offset (in bytes), relative to the address point, + /// of the virtual call offset. + int64_t VCallOffsetOffset; + + ThisAdjustment() : NonVirtual(0), VCallOffsetOffset(0) { } + + bool isEmpty() const { return !NonVirtual && !VCallOffsetOffset; } + + friend bool operator==(const ThisAdjustment &LHS, + const ThisAdjustment &RHS) { + return LHS.NonVirtual == RHS.NonVirtual && + LHS.VCallOffsetOffset == RHS.VCallOffsetOffset; + } + + friend bool operator<(const ThisAdjustment &LHS, + const ThisAdjustment &RHS) { + if (LHS.NonVirtual < RHS.NonVirtual) + return true; + + return LHS.NonVirtual == RHS.NonVirtual && + LHS.VCallOffsetOffset < RHS.VCallOffsetOffset; + } +}; + +/// ThunkInfo - The 'this' pointer adjustment as well as an optional return +/// adjustment for a thunk. +struct ThunkInfo { + /// This - The 'this' pointer adjustment. + ThisAdjustment This; + + /// Return - The return adjustment. + ReturnAdjustment Return; + + ThunkInfo() { } + + ThunkInfo(const ThisAdjustment &This, const ReturnAdjustment &Return) + : This(This), Return(Return) { } + + friend bool operator==(const ThunkInfo &LHS, const ThunkInfo &RHS) { + return LHS.This == RHS.This && LHS.Return == RHS.Return; + } + + friend bool operator<(const ThunkInfo &LHS, const ThunkInfo &RHS) { + if (LHS.This < RHS.This) + return true; + + return LHS.This == RHS.This && LHS.Return < RHS.Return; + } + + bool isEmpty() const { return This.isEmpty() && Return.isEmpty(); } +}; + +} // end namespace clang + +#endif // CLANG_BASIC_ABI_H diff --git a/clang/lib/AST/ASTContext.cpp b/clang/lib/AST/ASTContext.cpp index 05e1973e614..3abfe22caa8 100644 --- a/clang/lib/AST/ASTContext.cpp +++ b/clang/lib/AST/ASTContext.cpp @@ -22,6 +22,7 @@ #include "clang/AST/ExternalASTSource.h" #include "clang/AST/ASTMutationListener.h" #include "clang/AST/RecordLayout.h" +#include "clang/AST/Mangle.h" #include "clang/Basic/Builtins.h" #include "clang/Basic/SourceManager.h" #include "clang/Basic/TargetInfo.h" @@ -5835,4 +5836,16 @@ bool ASTContext::isNearlyEmpty(const CXXRecordDecl *RD) const { return ABI->isNearlyEmpty(RD); } +MangleContext *ASTContext::createMangleContext() { + switch (Target.getCXXABI()) { + case CXXABI_ARM: + case CXXABI_Itanium: + return createItaniumMangleContext(*this, getDiagnostics()); + case CXXABI_Microsoft: + return createMicrosoftMangleContext(*this, getDiagnostics()); + } + assert(0 && "Unsupported ABI"); + return 0; +} + CXXABI::~CXXABI() {} diff --git a/clang/lib/AST/CMakeLists.txt b/clang/lib/AST/CMakeLists.txt index e98a89646c3..b49b89ed886 100644 --- a/clang/lib/AST/CMakeLists.txt +++ b/clang/lib/AST/CMakeLists.txt @@ -26,7 +26,10 @@ add_clang_library(clangAST ExprCXX.cpp InheritViz.cpp ItaniumCXXABI.cpp + ItaniumMangle.cpp + Mangle.cpp MicrosoftCXXABI.cpp + MicrosoftMangle.cpp NestedNameSpecifier.cpp ParentMap.cpp RecordLayout.cpp diff --git a/clang/lib/CodeGen/Mangle.cpp b/clang/lib/AST/ItaniumMangle.cpp index b8ae1e7e41b..8daba88ea86 100644 --- a/clang/lib/CodeGen/Mangle.cpp +++ b/clang/lib/AST/ItaniumMangle.cpp @@ -1,4 +1,4 @@ -//===--- Mangle.cpp - Mangle C++ Names --------------------------*- C++ -*-===// +//===--- ItaniumMangle.cpp - Itanium C++ Name Mangling ----------*- C++ -*-===// // // The LLVM Compiler Infrastructure // @@ -14,18 +14,18 @@ // http://www.codesourcery.com/public/cxx-abi/abi.html // //===----------------------------------------------------------------------===// -#include "Mangle.h" +#include "clang/AST/Mangle.h" #include "clang/AST/ASTContext.h" #include "clang/AST/Decl.h" #include "clang/AST/DeclCXX.h" #include "clang/AST/DeclObjC.h" #include "clang/AST/DeclTemplate.h" #include "clang/AST/ExprCXX.h" +#include "clang/Basic/ABI.h" #include "clang/Basic/SourceManager.h" #include "llvm/ADT/StringExtras.h" #include "llvm/Support/raw_ostream.h" #include "llvm/Support/ErrorHandling.h" -#include "CGVTables.h" #define MANGLE_CHECKER 0 @@ -34,68 +34,6 @@ #endif using namespace clang; -using namespace CodeGen; - -MiscNameMangler::MiscNameMangler(MangleContext &C, - llvm::SmallVectorImpl<char> &Res) - : Context(C), Out(Res) { } - -void MiscNameMangler::mangleBlock(GlobalDecl GD, const BlockDecl *BD) { - // Mangle the context of the block. - // FIXME: We currently mimic GCC's mangling scheme, which leaves much to be - // desired. Come up with a better mangling scheme. - const DeclContext *DC = BD->getDeclContext(); - while (isa<BlockDecl>(DC) || isa<EnumDecl>(DC)) - DC = DC->getParent(); - if (DC->isFunctionOrMethod()) { - Out << "__"; - if (const ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(DC)) - mangleObjCMethodName(Method); - else { - const NamedDecl *ND = cast<NamedDecl>(DC); - if (IdentifierInfo *II = ND->getIdentifier()) - Out << II->getName(); - else if (const CXXDestructorDecl *D = dyn_cast<CXXDestructorDecl>(ND)) { - llvm::SmallString<64> Buffer; - Context.mangleCXXDtor(D, GD.getDtorType(), Buffer); - Out << Buffer; - } - else if (const CXXConstructorDecl *D = dyn_cast<CXXConstructorDecl>(ND)) { - llvm::SmallString<64> Buffer; - Context.mangleCXXCtor(D, GD.getCtorType(), Buffer); - Out << Buffer; - } - else { - // FIXME: We were doing a mangleUnqualifiedName() before, but that's - // a private member of a class that will soon itself be private to the - // Itanium C++ ABI object. What should we do now? Right now, I'm just - // calling the mangleName() method on the MangleContext; is there a - // better way? - llvm::SmallString<64> Buffer; - Context.mangleName(ND, Buffer); - Out << Buffer; - } - } - Out << "_block_invoke_" << Context.getBlockId(BD, true); - } else { - Out << "__block_global_" << Context.getBlockId(BD, false); - } -} - -void MiscNameMangler::mangleObjCMethodName(const ObjCMethodDecl *MD) { - llvm::SmallString<64> Name; - llvm::raw_svector_ostream OS(Name); - - const ObjCContainerDecl *CD = - dyn_cast<ObjCContainerDecl>(MD->getDeclContext()); - assert (CD && "Missing container decl in GetNameForMethod"); - OS << (MD->isInstanceMethod() ? '-' : '+') << '[' << CD->getName(); - if (const ObjCCategoryImplDecl *CID = dyn_cast<ObjCCategoryImplDecl>(CD)) - OS << '(' << CID << ')'; - OS << ' ' << MD->getSelector().getAsString() << ']'; - - Out << OS.str().size() << OS.str(); -} namespace { @@ -127,9 +65,77 @@ static const CXXMethodDecl *getStructor(const CXXMethodDecl *MD) { static const unsigned UnknownArity = ~0U; +class ItaniumMangleContext : public MangleContext { + llvm::DenseMap<const TagDecl *, uint64_t> AnonStructIds; + unsigned Discriminator; + llvm::DenseMap<const NamedDecl*, unsigned> Uniquifier; + +public: + explicit ItaniumMangleContext(ASTContext &Context, + Diagnostic &Diags) + : MangleContext(Context, Diags) { } + + uint64_t getAnonymousStructId(const TagDecl *TD) { + std::pair<llvm::DenseMap<const TagDecl *, + uint64_t>::iterator, bool> Result = + AnonStructIds.insert(std::make_pair(TD, AnonStructIds.size())); + return Result.first->second; + } + + void startNewFunction() { + MangleContext::startNewFunction(); + mangleInitDiscriminator(); + } + + /// @name Mangler Entry Points + /// @{ + + bool shouldMangleDeclName(const NamedDecl *D); + void mangleName(const NamedDecl *D, llvm::SmallVectorImpl<char> &); + void mangleThunk(const CXXMethodDecl *MD, + const ThunkInfo &Thunk, + llvm::SmallVectorImpl<char> &); + void mangleCXXDtorThunk(const CXXDestructorDecl *DD, CXXDtorType Type, + const ThisAdjustment &ThisAdjustment, + llvm::SmallVectorImpl<char> &); + void mangleReferenceTemporary(const VarDecl *D, + llvm::SmallVectorImpl<char> &); + void mangleCXXVTable(const CXXRecordDecl *RD, + llvm::SmallVectorImpl<char> &); + void mangleCXXVTT(const CXXRecordDecl *RD, + llvm::SmallVectorImpl<char> &); + void mangleCXXCtorVTable(const CXXRecordDecl *RD, int64_t Offset, + const CXXRecordDecl *Type, + llvm::SmallVectorImpl<char> &); + void mangleCXXRTTI(QualType T, llvm::SmallVectorImpl<char> &); + void mangleCXXRTTIName(QualType T, llvm::SmallVectorImpl<char> &); + void mangleCXXCtor(const CXXConstructorDecl *D, CXXCtorType Type, + llvm::SmallVectorImpl<char> &); + void mangleCXXDtor(const CXXDestructorDecl *D, CXXDtorType Type, + llvm::SmallVectorImpl<char> &); + + void mangleItaniumGuardVariable(const VarDecl *D, + llvm::SmallVectorImpl<char> &); + + void mangleInitDiscriminator() { + Discriminator = 0; + } + + bool getNextDiscriminator(const NamedDecl *ND, unsigned &disc) { + unsigned &discriminator = Uniquifier[ND]; + if (!discriminator) + discriminator = ++Discriminator; + if (discriminator == 1) + return false; + disc = discriminator-2; + return true; + } + /// @} +}; + /// CXXNameMangler - Manage the mangling of a single name. class CXXNameMangler { - MangleContext &Context; + ItaniumMangleContext &Context; llvm::raw_svector_ostream Out; const CXXMethodDecl *Structor; @@ -143,13 +149,13 @@ class CXXNameMangler { ASTContext &getASTContext() const { return Context.getASTContext(); } public: - CXXNameMangler(MangleContext &C, llvm::SmallVectorImpl<char> &Res) + CXXNameMangler(ItaniumMangleContext &C, llvm::SmallVectorImpl<char> &Res) : Context(C), Out(Res), Structor(0), StructorType(0), SeqID(0) { } - CXXNameMangler(MangleContext &C, llvm::SmallVectorImpl<char> &Res, + CXXNameMangler(ItaniumMangleContext &C, llvm::SmallVectorImpl<char> &Res, const CXXConstructorDecl *D, CXXCtorType Type) : Context(C), Out(Res), Structor(getStructor(D)), StructorType(Type), SeqID(0) { } - CXXNameMangler(MangleContext &C, llvm::SmallVectorImpl<char> &Res, + CXXNameMangler(ItaniumMangleContext &C, llvm::SmallVectorImpl<char> &Res, const CXXDestructorDecl *D, CXXDtorType Type) : Context(C), Out(Res), Structor(getStructor(D)), StructorType(Type), SeqID(0) { } @@ -259,6 +265,7 @@ private: void mangleTemplateParameter(unsigned Index); }; + } static bool isInCLinkageSpecification(const Decl *D) { @@ -272,7 +279,7 @@ static bool isInCLinkageSpecification(const Decl *D) { return false; } -bool MangleContext::shouldMangleDeclName(const NamedDecl *D) { +bool ItaniumMangleContext::shouldMangleDeclName(const NamedDecl *D) { // In C, functions with no attributes never need to be mangled. Fastpath them. if (!getASTContext().getLangOptions().CPlusPlus && !D->hasAttrs()) return false; @@ -896,7 +903,7 @@ void CXXNameMangler::manglePrefix(const DeclContext *DC, bool NoFunction) { if (const BlockDecl *Block = dyn_cast<BlockDecl>(DC)) { manglePrefix(DC->getParent(), NoFunction); llvm::SmallString<64> Name; - Context.mangleBlock(GlobalDecl(), Block, Name); + Context.mangleBlock(Block, Name); Out << Name.size() << Name; return; } @@ -1150,7 +1157,7 @@ void CXXNameMangler::mangleQualifiers(Qualifiers Quals) { void CXXNameMangler::mangleObjCMethodName(const ObjCMethodDecl *MD) { llvm::SmallString<64> Buffer; - MiscNameMangler(Context, Buffer).mangleObjCMethodName(MD); + Context.mangleObjCMethodName(MD, Buffer); Out << Buffer; } @@ -2470,8 +2477,8 @@ void CXXNameMangler::addSubstitution(uintptr_t Ptr) { /// 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. -void MangleContext::mangleName(const NamedDecl *D, - llvm::SmallVectorImpl<char> &Res) { +void ItaniumMangleContext::mangleName(const NamedDecl *D, + llvm::SmallVectorImpl<char> &Res) { assert((isa<FunctionDecl>(D) || isa<VarDecl>(D)) && "Invalid mangleName() call, argument is not a variable or function!"); assert(!isa<CXXConstructorDecl>(D) && !isa<CXXDestructorDecl>(D) && @@ -2485,27 +2492,23 @@ void MangleContext::mangleName(const NamedDecl *D, return Mangler.mangle(D); } -void MangleContext::mangleCXXCtor(const CXXConstructorDecl *D, CXXCtorType Type, - llvm::SmallVectorImpl<char> &Res) { +void ItaniumMangleContext::mangleCXXCtor(const CXXConstructorDecl *D, + CXXCtorType Type, + llvm::SmallVectorImpl<char> &Res) { CXXNameMangler Mangler(*this, Res, D, Type); Mangler.mangle(D); } -void MangleContext::mangleCXXDtor(const CXXDestructorDecl *D, CXXDtorType Type, - llvm::SmallVectorImpl<char> &Res) { +void ItaniumMangleContext::mangleCXXDtor(const CXXDestructorDecl *D, + CXXDtorType Type, + llvm::SmallVectorImpl<char> &Res) { CXXNameMangler Mangler(*this, Res, D, Type); Mangler.mangle(D); } -void MangleContext::mangleBlock(GlobalDecl GD, const BlockDecl *BD, - llvm::SmallVectorImpl<char> &Res) { - MiscNameMangler Mangler(*this, Res); - Mangler.mangleBlock(GD, BD); -} - -void MangleContext::mangleThunk(const CXXMethodDecl *MD, - const ThunkInfo &Thunk, - llvm::SmallVectorImpl<char> &Res) { +void ItaniumMangleContext::mangleThunk(const CXXMethodDecl *MD, + const ThunkInfo &Thunk, + llvm::SmallVectorImpl<char> &Res) { // <special-name> ::= T <call-offset> <base encoding> // # base is the nominal target function of thunk // <special-name> ::= Tc <call-offset> <call-offset> <base encoding> @@ -2533,9 +2536,10 @@ void MangleContext::mangleThunk(const CXXMethodDecl *MD, } void -MangleContext::mangleCXXDtorThunk(const CXXDestructorDecl *DD, CXXDtorType Type, - const ThisAdjustment &ThisAdjustment, - llvm::SmallVectorImpl<char> &Res) { +ItaniumMangleContext::mangleCXXDtorThunk(const CXXDestructorDecl *DD, + CXXDtorType Type, + const ThisAdjustment &ThisAdjustment, + llvm::SmallVectorImpl<char> &Res) { // <special-name> ::= T <call-offset> <base encoding> // # base is the nominal target function of thunk @@ -2551,8 +2555,8 @@ MangleContext::mangleCXXDtorThunk(const CXXDestructorDecl *DD, CXXDtorType Type, /// mangleGuardVariable - Returns the mangled name for a guard variable /// for the passed in VarDecl. -void MangleContext::mangleItaniumGuardVariable(const VarDecl *D, - llvm::SmallVectorImpl<char> &Res) { +void ItaniumMangleContext::mangleItaniumGuardVariable(const VarDecl *D, + llvm::SmallVectorImpl<char> &Res) { // <special-name> ::= GV <object name> # Guard variable for one-time // # initialization CXXNameMangler Mangler(*this, Res); @@ -2560,7 +2564,7 @@ void MangleContext::mangleItaniumGuardVariable(const VarDecl *D, Mangler.mangleName(D); } -void MangleContext::mangleReferenceTemporary(const VarDecl *D, +void ItaniumMangleContext::mangleReferenceTemporary(const VarDecl *D, llvm::SmallVectorImpl<char> &Res) { // We match the GCC mangling here. // <special-name> ::= GR <object name> @@ -2569,25 +2573,26 @@ void MangleContext::mangleReferenceTemporary(const VarDecl *D, Mangler.mangleName(D); } -void MangleContext::mangleCXXVTable(const CXXRecordDecl *RD, - llvm::SmallVectorImpl<char> &Res) { +void ItaniumMangleContext::mangleCXXVTable(const CXXRecordDecl *RD, + llvm::SmallVectorImpl<char> &Res) { // <special-name> ::= TV <type> # virtual table CXXNameMangler Mangler(*this, Res); Mangler.getStream() << "_ZTV"; Mangler.mangleNameOrStandardSubstitution(RD); } -void MangleContext::mangleCXXVTT(const CXXRecordDecl *RD, - llvm::SmallVectorImpl<char> &Res) { +void ItaniumMangleContext::mangleCXXVTT(const CXXRecordDecl *RD, + llvm::SmallVectorImpl<char> &Res) { // <special-name> ::= TT <type> # VTT structure CXXNameMangler Mangler(*this, Res); Mangler.getStream() << "_ZTT"; Mangler.mangleNameOrStandardSubstitution(RD); } -void MangleContext::mangleCXXCtorVTable(const CXXRecordDecl *RD, int64_t Offset, - const CXXRecordDecl *Type, - llvm::SmallVectorImpl<char> &Res) { +void ItaniumMangleContext::mangleCXXCtorVTable(const CXXRecordDecl *RD, + int64_t Offset, + const CXXRecordDecl *Type, + llvm::SmallVectorImpl<char> &Res) { // <special-name> ::= TC <type> <offset number> _ <base type> CXXNameMangler Mangler(*this, Res); Mangler.getStream() << "_ZTC"; @@ -2597,8 +2602,8 @@ void MangleContext::mangleCXXCtorVTable(const CXXRecordDecl *RD, int64_t Offset, Mangler.mangleNameOrStandardSubstitution(Type); } -void MangleContext::mangleCXXRTTI(QualType Ty, - llvm::SmallVectorImpl<char> &Res) { +void ItaniumMangleContext::mangleCXXRTTI(QualType Ty, + llvm::SmallVectorImpl<char> &Res) { // <special-name> ::= TI <type> # typeinfo structure assert(!Ty.hasQualifiers() && "RTTI info cannot have top-level qualifiers"); CXXNameMangler Mangler(*this, Res); @@ -2606,10 +2611,15 @@ void MangleContext::mangleCXXRTTI(QualType Ty, Mangler.mangleType(Ty); } -void MangleContext::mangleCXXRTTIName(QualType Ty, - llvm::SmallVectorImpl<char> &Res) { +void ItaniumMangleContext::mangleCXXRTTIName(QualType Ty, + llvm::SmallVectorImpl<char> &Res) { // <special-name> ::= TS <type> # typeinfo name (null terminated byte string) CXXNameMangler Mangler(*this, Res); Mangler.getStream() << "_ZTS"; Mangler.mangleType(Ty); } + +MangleContext *clang::createItaniumMangleContext(ASTContext &Context, + Diagnostic &Diags) { + return new ItaniumMangleContext(Context, Diags); +} diff --git a/clang/lib/AST/Mangle.cpp b/clang/lib/AST/Mangle.cpp new file mode 100644 index 00000000000..83d9ed66788 --- /dev/null +++ b/clang/lib/AST/Mangle.cpp @@ -0,0 +1,132 @@ +//===--- Mangle.cpp - Mangle C++ Names --------------------------*- C++ -*-===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// +// +// Implements generic name mangling support for blocks and Objective-C. +// +//===----------------------------------------------------------------------===// +#include "clang/AST/Mangle.h" +#include "clang/AST/ASTContext.h" +#include "clang/AST/Decl.h" +#include "clang/AST/DeclCXX.h" +#include "clang/AST/DeclObjC.h" +#include "clang/AST/DeclTemplate.h" +#include "clang/AST/ExprCXX.h" +#include "clang/Basic/ABI.h" +#include "clang/Basic/SourceManager.h" +#include "llvm/ADT/StringExtras.h" +#include "llvm/Support/raw_ostream.h" +#include "llvm/Support/ErrorHandling.h" + +#define MANGLE_CHECKER 0 + +#if MANGLE_CHECKER +#include <cxxabi.h> +#endif + +using namespace clang; + +// FIXME: For blocks we currently mimic GCC's mangling scheme, which leaves +// much to be desired. Come up with a better mangling scheme. + +namespace { + +static void mangleFunctionBlock(MangleContext &Context, + llvm::StringRef Outer, + const BlockDecl *BD, + llvm::SmallVectorImpl<char> &Res) { + llvm::raw_svector_ostream Out(Res); + Out << "__" << Outer << "_block_invoke_" << Context.getBlockId(BD, true); +} + +static void checkMangleDC(const DeclContext *DC, const BlockDecl *BD) { +#ifndef NDEBUG + const DeclContext *ExpectedDC = BD->getDeclContext(); + while (isa<BlockDecl>(ExpectedDC) || isa<EnumDecl>(ExpectedDC)) + ExpectedDC = ExpectedDC->getParent(); + assert(DC == ExpectedDC && "Given decl context did not match expected!"); +#endif +} + +} + +void MangleContext::mangleGlobalBlock(const BlockDecl *BD, + llvm::SmallVectorImpl<char> &Res) { + llvm::raw_svector_ostream Out(Res); + Out << "__block_global_" << getBlockId(BD, false); +} + +void MangleContext::mangleCtorBlock(const CXXConstructorDecl *CD, + CXXCtorType CT, const BlockDecl *BD, + llvm::SmallVectorImpl<char> &Res) { + checkMangleDC(CD, BD); + llvm::SmallString<64> Buffer; + mangleCXXCtor(CD, CT, Buffer); + mangleFunctionBlock(*this, Buffer, BD, Res); +} + +void MangleContext::mangleDtorBlock(const CXXDestructorDecl *DD, + CXXDtorType DT, const BlockDecl *BD, + llvm::SmallVectorImpl<char> &Res) { + checkMangleDC(DD, BD); + llvm::SmallString<64> Buffer; + mangleCXXDtor(DD, DT, Buffer); + mangleFunctionBlock(*this, Buffer, BD, Res); +} + +void MangleContext::mangleBlock(const DeclContext *DC, const BlockDecl *BD, + llvm::SmallVectorImpl<char> &Res) { + assert(!isa<CXXConstructorDecl>(DC) && !isa<CXXDestructorDecl>(DC)); + checkMangleDC(DC, BD); + + llvm::SmallString<64> Buffer; + if (const ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(DC)) { + mangleObjCMethodName(Method, Buffer); + } else { + const NamedDecl *ND = cast<NamedDecl>(DC); + if (IdentifierInfo *II = ND->getIdentifier()) + Buffer = II->getName(); + else { + // FIXME: We were doing a mangleUnqualifiedName() before, but that's + // a private member of a class that will soon itself be private to the + // Itanium C++ ABI object. What should we do now? Right now, I'm just + // calling the mangleName() method on the MangleContext; is there a + // better way? + mangleName(ND, Buffer); + } + } + + mangleFunctionBlock(*this, Buffer, BD, Res); +} + +void MangleContext::mangleObjCMethodName(const ObjCMethodDecl *MD, + llvm::SmallVectorImpl<char> &Res) { + llvm::SmallString<64> Name; + llvm::raw_svector_ostream OS(Name), Out(Res); + + const ObjCContainerDecl *CD = + dyn_cast<ObjCContainerDecl>(MD->getDeclContext()); + assert (CD && "Missing container decl in GetNameForMethod"); + OS << (MD->isInstanceMethod() ? '-' : '+') << '[' << CD->getName(); + if (const ObjCCategoryImplDecl *CID = dyn_cast<ObjCCategoryImplDecl>(CD)) + OS << '(' << CID << ')'; + OS << ' ' << MD->getSelector().getAsString() << ']'; + + Out << OS.str().size() << OS.str(); +} + +void MangleContext::mangleBlock(const BlockDecl *BD, + llvm::SmallVectorImpl<char> &Res) { + const DeclContext *DC = BD->getDeclContext(); + while (isa<BlockDecl>(DC) || isa<EnumDecl>(DC)) + DC = DC->getParent(); + if (DC->isFunctionOrMethod()) + mangleBlock(DC, BD, Res); + else + mangleGlobalBlock(BD, Res); +} diff --git a/clang/lib/AST/MicrosoftMangle.cpp b/clang/lib/AST/MicrosoftMangle.cpp new file mode 100644 index 00000000000..ef50899e439 --- /dev/null +++ b/clang/lib/AST/MicrosoftMangle.cpp @@ -0,0 +1,1184 @@ +//===--- MicrosoftMangle.cpp - Microsoft Visual C++ Name Mangling ---------===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// +// +// This provides C++ name mangling targetting the Microsoft Visual C++ ABI. +// +//===----------------------------------------------------------------------===// + +#include "clang/AST/Mangle.h" +#include "clang/AST/ASTContext.h" +#include "clang/AST/CharUnits.h" +#include "clang/AST/Decl.h" +#include "clang/AST/DeclCXX.h" +#include "clang/AST/DeclObjC.h" +#include "clang/AST/DeclTemplate.h" +#include "clang/AST/ExprCXX.h" +#include "clang/Basic/ABI.h" + +using namespace clang; + +namespace { + +/// MicrosoftCXXNameMangler - Manage the mangling of a single name for the +/// Microsoft Visual C++ ABI. +class MicrosoftCXXNameMangler { + MangleContext &Context; + llvm::raw_svector_ostream Out; + + ASTContext &getASTContext() const { return Context.getASTContext(); } + +public: + MicrosoftCXXNameMangler(MangleContext &C, llvm::SmallVectorImpl<char> &Res) + : Context(C), Out(Res) { } + + void mangle(const NamedDecl *D, llvm::StringRef Prefix = "?"); + void mangleName(const NamedDecl *ND); + void mangleFunctionEncoding(const FunctionDecl *FD); + void mangleVariableEncoding(const VarDecl *VD); + void mangleNumber(int64_t Number); + void mangleType(QualType T); + +private: + void mangleUnqualifiedName(const NamedDecl *ND) { + mangleUnqualifiedName(ND, ND->getDeclName()); + } + void mangleUnqualifiedName(const NamedDecl *ND, DeclarationName Name); + void mangleSourceName(const IdentifierInfo *II); + void manglePostfix(const DeclContext *DC, bool NoFunction=false); + void mangleOperatorName(OverloadedOperatorKind OO); + void mangleQualifiers(Qualifiers Quals, bool IsMember); + + void mangleObjCMethodName(const ObjCMethodDecl *MD); + + // Declare manglers for every type class. +#define ABSTRACT_TYPE(CLASS, PARENT) +#define NON_CANONICAL_TYPE(CLASS, PARENT) +#define TYPE(CLASS, PARENT) void mangleType(const CLASS##Type *T); +#include "clang/AST/TypeNodes.def" + + void mangleType(const TagType*); + void mangleType(const FunctionType *T, const FunctionDecl *D, + bool IsStructor, bool IsInstMethod); + void mangleType(const ArrayType *T, bool IsGlobal); + void mangleExtraDimensions(QualType T); + void mangleFunctionClass(const FunctionDecl *FD); + void mangleCallingConvention(const FunctionType *T, bool IsInstMethod = false); + void mangleThrowSpecification(const FunctionProtoType *T); + +}; + +/// MicrosoftMangleContext - Overrides the default MangleContext for the +/// Microsoft Visual C++ ABI. +class MicrosoftMangleContext : public MangleContext { +public: + MicrosoftMangleContext(ASTContext &Context, + Diagnostic &Diags) : MangleContext(Context, Diags) { } + virtual bool shouldMangleDeclName(const NamedDecl *D); + virtual void mangleName(const NamedDecl *D, llvm::SmallVectorImpl<char> &); + virtual void mangleThunk(const CXXMethodDecl *MD, + const ThunkInfo &Thunk, + llvm::SmallVectorImpl<char> &); + virtual void mangleCXXDtorThunk(const CXXDestructorDecl *DD, CXXDtorType Type, + const ThisAdjustment &ThisAdjustment, + llvm::SmallVectorImpl<char> &); + virtual void mangleCXXVTable(const CXXRecordDecl *RD, + llvm::SmallVectorImpl<char> &); + virtual void mangleCXXVTT(const CXXRecordDecl *RD, + llvm::SmallVectorImpl<char> &); + virtual void mangleCXXCtorVTable(const CXXRecordDecl *RD, int64_t Offset, + const CXXRecordDecl *Type, + llvm::SmallVectorImpl<char> &); + virtual void mangleCXXRTTI(QualType T, llvm::SmallVectorImpl<char> &); + virtual void mangleCXXRTTIName(QualType T, llvm::SmallVectorImpl<char> &); + virtual void mangleCXXCtor(const CXXConstructorDecl *D, CXXCtorType Type, + llvm::SmallVectorImpl<char> &); + virtual void mangleCXXDtor(const CXXDestructorDecl *D, CXXDtorType Type, + llvm::SmallVectorImpl<char> &); + virtual void mangleReferenceTemporary(const clang::VarDecl *, + llvm::SmallVectorImpl<char> &); +}; + +} + +static bool isInCLinkageSpecification(const Decl *D) { + D = D->getCanonicalDecl(); + for (const DeclContext *DC = D->getDeclContext(); + !DC->isTranslationUnit(); DC = DC->getParent()) { + if (const LinkageSpecDecl *Linkage = dyn_cast<LinkageSpecDecl>(DC)) + return Linkage->getLanguage() == LinkageSpecDecl::lang_c; + } + + return false; +} + +bool MicrosoftMangleContext::shouldMangleDeclName(const NamedDecl *D) { + // In C, functions with no attributes never need to be mangled. Fastpath them. + if (!getASTContext().getLangOptions().CPlusPlus && !D->hasAttrs()) + return false; + + // Any decl can be declared with __asm("foo") on it, and this takes precedence + // over all other naming in the .o file. + if (D->hasAttr<AsmLabelAttr>()) + return true; + + // Clang's "overloadable" attribute extension to C/C++ implies name mangling + // (always) as does passing a C++ member function and a function + // whose name is not a simple identifier. + const FunctionDecl *FD = dyn_cast<FunctionDecl>(D); + if (FD && (FD->hasAttr<OverloadableAttr>() || isa<CXXMethodDecl>(FD) || + !FD->getDeclName().isIdentifier())) + return true; + + // Otherwise, no mangling is done outside C++ mode. + if (!getASTContext().getLangOptions().CPlusPlus) + return false; + + // Variables at global scope with internal linkage are not mangled. + if (!FD) { + const DeclContext *DC = D->getDeclContext(); + if (DC->isTranslationUnit() && D->getLinkage() == InternalLinkage) + return false; + } + + // C functions and "main" are not mangled. + if ((FD && FD->isMain()) || isInCLinkageSpecification(D)) + return false; + + return true; +} + +void MicrosoftCXXNameMangler::mangle(const NamedDecl *D, + llvm::StringRef Prefix) { + // MSVC doesn't mangle C++ names the same way it mangles extern "C" names. + // Therefore it's really important that we don't decorate the + // name with leading underscores or leading/trailing at signs. So, emit a + // asm marker at the start so we get the name right. + Out << '\01'; // LLVM IR Marker for __asm("foo") + + // Any decl can be declared with __asm("foo") on it, and this takes precedence + // over all other naming in the .o file. + if (const AsmLabelAttr *ALA = D->getAttr<AsmLabelAttr>()) { + // If we have an asm name, then we use it as the mangling. + Out << ALA->getLabel(); + return; + } + + // <mangled-name> ::= ? <name> <type-encoding> + Out << Prefix; + mangleName(D); + if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) + mangleFunctionEncoding(FD); + else if (const VarDecl *VD = dyn_cast<VarDecl>(D)) + mangleVariableEncoding(VD); + // TODO: Fields? Can MSVC even mangle them? +} + +void MicrosoftCXXNameMangler::mangleFunctionEncoding(const FunctionDecl *FD) { + // <type-encoding> ::= <function-class> <function-type> + + // Don't mangle in the type if this isn't a decl we should typically mangle. + if (!Context.shouldMangleDeclName(FD)) + return; + + // We should never ever see a FunctionNoProtoType at this point. + // We don't even know how to mangle their types anyway :). + const FunctionProtoType *FT = cast<FunctionProtoType>(FD->getType()); + + bool InStructor = false, InInstMethod = false; + const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD); + if (MD) { + if (MD->isInstance()) + InInstMethod = true; + if (isa<CXXConstructorDecl>(MD) || isa<CXXDestructorDecl>(MD)) + InStructor = true; + } + + // First, the function class. + mangleFunctionClass(FD); + + mangleType(FT, FD, InStructor, InInstMethod); +} + +void MicrosoftCXXNameMangler::mangleVariableEncoding(const VarDecl *VD) { + // <type-encoding> ::= <storage-class> <variable-type> + // <storage-class> ::= 0 # private static member + // ::= 1 # protected static member + // ::= 2 # public static member + // ::= 3 # global + // ::= 4 # static local + + // The first character in the encoding (after the name) is the storage class. + if (VD->isStaticDataMember()) { + // If it's a static member, it also encodes the access level. + switch (VD->getAccess()) { + default: + case AS_private: Out << '0'; break; + case AS_protected: Out << '1'; break; + case AS_public: Out << '2'; break; + } + } + else if (!VD->isStaticLocal()) + Out << '3'; + else + Out << '4'; + // Now mangle the type. + // <variable-type> ::= <type> <cvr-qualifiers> + // ::= <type> A # pointers, references, arrays + // Pointers and references are odd. The type of 'int * const foo;' gets + // mangled as 'QAHA' instead of 'PAHB', for example. + QualType Ty = VD->getType(); + if (Ty->isPointerType() || Ty->isReferenceType()) { + mangleType(Ty); + Out << 'A'; + } else if (Ty->isArrayType()) { + // Global arrays are funny, too. + mangleType(static_cast<ArrayType *>(Ty.getTypePtr()), true); + Out << 'A'; + } else { + mangleType(Ty.getLocalUnqualifiedType()); + mangleQualifiers(Ty.getLocalQualifiers(), false); + } +} + +void MicrosoftCXXNameMangler::mangleName(const NamedDecl *ND) { + // <name> ::= <unscoped-name> {[<named-scope>]+ | [<nested-name>]}? @ + const DeclContext *DC = ND->getDeclContext(); + + // Always start with the unqualified name. + mangleUnqualifiedName(ND); + + // If this is an extern variable declared locally, the relevant DeclContext + // is that of the containing namespace, or the translation unit. + if (isa<FunctionDecl>(DC) && ND->hasLinkage()) + while (!DC->isNamespace() && !DC->isTranslationUnit()) + DC = DC->getParent(); + + manglePostfix(DC); + + // Terminate the whole name with an '@'. + Out << '@'; +} + +void MicrosoftCXXNameMangler::mangleNumber(int64_t Number) { + // <number> ::= [?] <decimal digit> # <= 9 + // ::= [?] <hex digit>+ @ # > 9; A = 0, B = 1, etc... + if (Number < 0) { + Out << '?'; + Number = -Number; + } + if (Number >= 1 && Number <= 10) { + Out << Number-1; + } else { + // We have to build up the encoding in reverse order, so it will come + // out right when we write it out. + char Encoding[16]; + char *EndPtr = Encoding+sizeof(Encoding); + char *CurPtr = EndPtr; + while (Number) { + *--CurPtr = 'A' + (Number % 16); + Number /= 16; + } + Out.write(CurPtr, EndPtr-CurPtr); + Out << '@'; + } +} + +void +MicrosoftCXXNameMangler::mangleUnqualifiedName(const NamedDecl *ND, + DeclarationName Name) { + // <unqualified-name> ::= <operator-name> + // ::= <ctor-dtor-name> + // ::= <source-name> + switch (Name.getNameKind()) { + case DeclarationName::Identifier: { + if (const IdentifierInfo *II = Name.getAsIdentifierInfo()) { + mangleSourceName(II); + break; + } + + // Otherwise, an anonymous entity. We must have a declaration. + assert(ND && "mangling empty name without declaration"); + + if (const NamespaceDecl *NS = dyn_cast<NamespaceDecl>(ND)) { + if (NS->isAnonymousNamespace()) { + Out << "?A"; + break; + } + } + + // We must have an anonymous struct. + const TagDecl *TD = cast<TagDecl>(ND); + if (const TypedefDecl *D = TD->getTypedefForAnonDecl()) { + assert(TD->getDeclContext() == D->getDeclContext() && + "Typedef should not be in another decl context!"); + assert(D->getDeclName().getAsIdentifierInfo() && + "Typedef was not named!"); + mangleSourceName(D->getDeclName().getAsIdentifierInfo()); + break; + } + + // When VC encounters an anonymous type with no tag and no typedef, + // it literally emits '<unnamed-tag>'. + Out << "<unnamed-tag>"; + break; + } + + case DeclarationName::ObjCZeroArgSelector: + case DeclarationName::ObjCOneArgSelector: + case DeclarationName::ObjCMultiArgSelector: + assert(false && "Can't mangle Objective-C selector names here!"); + break; + + case DeclarationName::CXXConstructorName: + assert(false && "Can't mangle constructors yet!"); + break; + + case DeclarationName::CXXDestructorName: + assert(false && "Can't mangle destructors yet!"); + break; + + case DeclarationName::CXXConversionFunctionName: + // <operator-name> ::= ?B # (cast) + // The target type is encoded as the return type. + Out << "?B"; + break; + + case DeclarationName::CXXOperatorName: + mangleOperatorName(Name.getCXXOverloadedOperator()); + break; + + case DeclarationName::CXXLiteralOperatorName: + // FIXME: Was this added in VS2010? Does MS even know how to mangle this? + assert(false && "Don't know how to mangle literal operators yet!"); + break; + + case DeclarationName::CXXUsingDirective: + assert(false && "Can't mangle a using directive name!"); + break; + } +} + +void MicrosoftCXXNameMangler::manglePostfix(const DeclContext *DC, + bool NoFunction) { + // <postfix> ::= <unqualified-name> [<postfix>] + // ::= <template-postfix> <template-args> [<postfix>] + // ::= <template-param> + // ::= <substitution> [<postfix>] + + if (!DC) return; + + while (isa<LinkageSpecDecl>(DC)) + DC = DC->getParent(); + + if (DC->isTranslationUnit()) + return; + + if (const BlockDecl *BD = dyn_cast<BlockDecl>(DC)) { + llvm::SmallString<64> Name; + Context.mangleBlock(BD, Name); + Out << Name << '@'; + return manglePostfix(DC->getParent(), NoFunction); + } + + if (NoFunction && (isa<FunctionDecl>(DC) || isa<ObjCMethodDecl>(DC))) + return; + else if (const ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(DC)) + mangleObjCMethodName(Method); + else { + mangleUnqualifiedName(cast<NamedDecl>(DC)); + manglePostfix(DC->getParent(), NoFunction); + } +} + +void MicrosoftCXXNameMangler::mangleOperatorName(OverloadedOperatorKind OO) { + switch (OO) { + // ?0 # constructor + // ?1 # destructor + // <operator-name> ::= ?2 # new + case OO_New: Out << "?2"; break; + // <operator-name> ::= ?3 # delete + case OO_Delete: Out << "?3"; break; + // <operator-name> ::= ?4 # = + case OO_Equal: Out << "?4"; break; + // <operator-name> ::= ?5 # >> + case OO_GreaterGreater: Out << "?5"; break; + // <operator-name> ::= ?6 # << + case OO_LessLess: Out << "?6"; break; + // <operator-name> ::= ?7 # ! + case OO_Exclaim: Out << "?7"; break; + // <operator-name> ::= ?8 # == + case OO_EqualEqual: Out << "?8"; break; + // <operator-name> ::= ?9 # != + case OO_ExclaimEqual: Out << "?9"; break; + // <operator-name> ::= ?A # [] + case OO_Subscript: Out << "?A"; break; + // ?B # conversion + // <operator-name> ::= ?C # -> + case OO_Arrow: Out << "?C"; break; + // <operator-name> ::= ?D # * + case OO_Star: Out << "?D"; break; + // <operator-name> ::= ?E # ++ + case OO_PlusPlus: Out << "?E"; break; + // <operator-name> ::= ?F # -- + case OO_MinusMinus: Out << "?F"; break; + // <operator-name> ::= ?G # - + case OO_Minus: Out << "?G"; break; + // <operator-name> ::= ?H # + + case OO_Plus: Out << "?H"; break; + // <operator-name> ::= ?I # & + case OO_Amp: Out << "?I"; break; + // <operator-name> ::= ?J # ->* + case OO_ArrowStar: Out << "?J"; break; + // <operator-name> ::= ?K # / + case OO_Slash: Out << "?K"; break; + // <operator-name> ::= ?L # % + case OO_Percent: Out << "?L"; break; + // <operator-name> ::= ?M # < + case OO_Less: Out << "?M"; break; + // <operator-name> ::= ?N # <= + case OO_LessEqual: Out << "?N"; break; + // <operator-name> ::= ?O # > + case OO_Greater: Out << "?O"; break; + // <operator-name> ::= ?P # >= + case OO_GreaterEqual: Out << "?P"; break; + // <operator-name> ::= ?Q # , + case OO_Comma: Out << "?Q"; break; + // <operator-name> ::= ?R # () + case OO_Call: Out << "?R"; break; + // <operator-name> ::= ?S # ~ + case OO_Tilde: Out << "?S"; break; + // <operator-name> ::= ?T # ^ + case OO_Caret: Out << "?T"; break; + // <operator-name> ::= ?U # | + case OO_Pipe: Out << "?U"; break; + // <operator-name> ::= ?V # && + case OO_AmpAmp: Out << "?V"; break; + // <operator-name> ::= ?W # || + case OO_PipePipe: Out << "?W"; break; + // <operator-name> ::= ?X # *= + case OO_StarEqual: Out << "?X"; break; + // <operator-name> ::= ?Y # += + case OO_PlusEqual: Out << "?Y"; break; + // <operator-name> ::= ?Z # -= + case OO_MinusEqual: Out << "?Z"; break; + // <operator-name> ::= ?_0 # /= + case OO_SlashEqual: Out << "?_0"; break; + // <operator-name> ::= ?_1 # %= + case OO_PercentEqual: Out << "?_1"; break; + // <operator-name> ::= ?_2 # >>= + case OO_GreaterGreaterEqual: Out << "?_2"; break; + // <operator-name> ::= ?_3 # <<= + case OO_LessLessEqual: Out << "?_3"; break; + // <operator-name> ::= ?_4 # &= + case OO_AmpEqual: Out << "?_4"; break; + // <operator-name> ::= ?_5 # |= + case OO_PipeEqual: Out << "?_5"; break; + // <operator-name> ::= ?_6 # ^= + case OO_CaretEqual: Out << "?_6"; break; + // ?_7 # vftable + // ?_8 # vbtable + // ?_9 # vcall + // ?_A # typeof + // ?_B # local static guard + // ?_C # string + // ?_D # vbase destructor + // ?_E # vector deleting destructor + // ?_F # default constructor closure + // ?_G # scalar deleting destructor + // ?_H # vector constructor iterator + // ?_I # vector destructor iterator + // ?_J # vector vbase constructor iterator + // ?_K # virtual displacement map + // ?_L # eh vector constructor iterator + // ?_M # eh vector destructor iterator + // ?_N # eh vector vbase constructor iterator + // ?_O # copy constructor closure + // ?_P<name> # udt returning <name> + // ?_Q # <unknown> + // ?_R0 # RTTI Type Descriptor + // ?_R1 # RTTI Base Class Descriptor at (a,b,c,d) + // ?_R2 # RTTI Base Class Array + // ?_R3 # RTTI Class Hierarchy Descriptor + // ?_R4 # RTTI Complete Object Locator + // ?_S # local vftable + // ?_T # local vftable constructor closure + // <operator-name> ::= ?_U # new[] + case OO_Array_New: Out << "?_U"; break; + // <operator-name> ::= ?_V # delete[] + case OO_Array_Delete: Out << "?_V"; break; + + case OO_Conditional: + assert(false && "Don't know how to mangle ?:"); + break; + + case OO_None: + case NUM_OVERLOADED_OPERATORS: + assert(false && "Not an overloaded operator"); + break; + } +} + +void MicrosoftCXXNameMangler::mangleSourceName(const IdentifierInfo *II) { + // <source name> ::= <identifier> @ + Out << II->getName() << '@'; +} + +void MicrosoftCXXNameMangler::mangleObjCMethodName(const ObjCMethodDecl *MD) { + llvm::SmallString<64> Buffer; + Context.mangleObjCMethodName(MD, Buffer); + Out << Buffer; +} + +void MicrosoftCXXNameMangler::mangleQualifiers(Qualifiers Quals, + bool IsMember) { + // <cvr-qualifiers> ::= [E] [F] [I] <base-cvr-qualifiers> + // 'E' means __ptr64 (32-bit only); 'F' means __unaligned (32/64-bit only); + // 'I' means __restrict (32/64-bit). + // Note that the MSVC __restrict keyword isn't the same as the C99 restrict + // keyword! + // <base-cvr-qualifiers> ::= A # near + // ::= B # near const + // ::= C # near volatile + // ::= D # near const volatile + // ::= E # far (16-bit) + // ::= F # far const (16-bit) + // ::= G # far volatile (16-bit) + // ::= H # far const volatile (16-bit) + // ::= I # huge (16-bit) + // ::= J # huge const (16-bit) + // ::= K # huge volatile (16-bit) + // ::= L # huge const volatile (16-bit) + // ::= M <basis> # based + // ::= N <basis> # based const + // ::= O <basis> # based volatile + // ::= P <basis> # based const volatile + // ::= Q # near member + // ::= R # near const member + // ::= S # near volatile member + // ::= T # near const volatile member + // ::= U # far member (16-bit) + // ::= V # far const member (16-bit) + // ::= W # far volatile member (16-bit) + // ::= X # far const volatile member (16-bit) + // ::= Y # huge member (16-bit) + // ::= Z # huge const member (16-bit) + // ::= 0 # huge volatile member (16-bit) + // ::= 1 # huge const volatile member (16-bit) + // ::= 2 <basis> # based member + // ::= 3 <basis> # based const member + // ::= 4 <basis> # based volatile member + // ::= 5 <basis> # based const volatile member + // ::= 6 # near function (pointers only) + // ::= 7 # far function (pointers only) + // ::= 8 # near method (pointers only) + // ::= 9 # far method (pointers only) + // ::= _A <basis> # based function (pointers only) + // ::= _B <basis> # based function (far?) (pointers only) + // ::= _C <basis> # based method (pointers only) + // ::= _D <basis> # based method (far?) (pointers only) + // ::= _E # block (Clang) + // <basis> ::= 0 # __based(void) + // ::= 1 # __based(segment)? + // ::= 2 <name> # __based(name) + // ::= 3 # ? + // ::= 4 # ? + // ::= 5 # not really based + if (!IsMember) { + if (!Quals.hasVolatile()) { + if (!Quals.hasConst()) + Out << 'A'; + else + Out << 'B'; + } else { + if (!Quals.hasConst()) + Out << 'C'; + else + Out << 'D'; + } + } else { + if (!Quals.hasVolatile()) { + if (!Quals.hasConst()) + Out << 'Q'; + else + Out << 'R'; + } else { + if (!Quals.hasConst()) + Out << 'S'; + else + Out << 'T'; + } + } + + // FIXME: For now, just drop all extension qualifiers on the floor. +} + +void MicrosoftCXXNameMangler::mangleType(QualType T) { + // Only operate on the canonical type! + T = getASTContext().getCanonicalType(T); + + Qualifiers Quals = T.getLocalQualifiers(); + if (Quals) { + // We have to mangle these now, while we still have enough information. + // <pointer-cvr-qualifiers> ::= P # pointer + // ::= Q # const pointer + // ::= R # volatile pointer + // ::= S # const volatile pointer + if (T->isAnyPointerType() || T->isMemberPointerType() || + T->isBlockPointerType()) { + if (!Quals.hasVolatile()) + Out << 'Q'; + else { + if (!Quals.hasConst()) + Out << 'R'; + else + Out << 'S'; + } + } else + // Just emit qualifiers like normal. + // NB: When we mangle a pointer/reference type, and the pointee + // type has no qualifiers, the lack of qualifier gets mangled + // in there. + mangleQualifiers(Quals, false); + } else if (T->isAnyPointerType() || T->isMemberPointerType() || + T->isBlockPointerType()) { + Out << 'P'; + } + switch (T->getTypeClass()) { +#define ABSTRACT_TYPE(CLASS, PARENT) +#define NON_CANONICAL_TYPE(CLASS, PARENT) \ +case Type::CLASS: \ +llvm_unreachable("can't mangle non-canonical type " #CLASS "Type"); \ +return; +#define TYPE(CLASS, PARENT) \ +case Type::CLASS: \ +mangleType(static_cast<const CLASS##Type*>(T.getTypePtr())); \ +break; +#include "clang/AST/TypeNodes.def" + } +} + +void MicrosoftCXXNameMangler::mangleType(const BuiltinType *T) { + // <type> ::= <builtin-type> + // <builtin-type> ::= X # void + // ::= C # signed char + // ::= D # char + // ::= E # unsigned char + // ::= F # short + // ::= G # unsigned short (or wchar_t if it's not a builtin) + // ::= H # int + // ::= I # unsigned int + // ::= J # long + // ::= K # unsigned long + // L # <none> + // ::= M # float + // ::= N # double + // ::= O # long double (__float80 is mangled differently) + // ::= _D # __int8 (yup, it's a distinct type in MSVC) + // ::= _E # unsigned __int8 + // ::= _F # __int16 + // ::= _G # unsigned __int16 + // ::= _H # __int32 + // ::= _I # unsigned __int32 + // ::= _J # long long, __int64 + // ::= _K # unsigned long long, __int64 + // ::= _L # __int128 + // ::= _M # unsigned __int128 + // ::= _N # bool + // _O # <array in parameter> + // ::= _T # __float80 (Intel) + // ::= _W # wchar_t + // ::= _Z # __float80 (Digital Mars) + switch (T->getKind()) { + case BuiltinType::Void: Out << 'X'; break; + case BuiltinType::SChar: Out << 'C'; break; + case BuiltinType::Char_U: case BuiltinType::Char_S: Out << 'D'; break; + case BuiltinType::UChar: Out << 'E'; break; + case BuiltinType::Short: Out << 'F'; break; + case BuiltinType::UShort: Out << 'G'; break; + case BuiltinType::Int: Out << 'H'; break; + case BuiltinType::UInt: Out << 'I'; break; + case BuiltinType::Long: Out << 'J'; break; + case BuiltinType::ULong: Out << 'K'; break; + case BuiltinType::Float: Out << 'M'; break; + case BuiltinType::Double: Out << 'N'; break; + // TODO: Determine size and mangle accordingly + case BuiltinType::LongDouble: Out << 'O'; break; + // TODO: __int8 and friends + case BuiltinType::LongLong: Out << "_J"; break; + case BuiltinType::ULongLong: Out << "_K"; break; + case BuiltinType::Int128: Out << "_L"; break; + case BuiltinType::UInt128: Out << "_M"; break; + case BuiltinType::Bool: Out << "_N"; break; + case BuiltinType::WChar_S: + case BuiltinType::WChar_U: Out << "_W"; break; + + case BuiltinType::Overload: + case BuiltinType::Dependent: + assert(false && + "Overloaded and dependent types shouldn't get to name mangling"); + break; + case BuiltinType::UndeducedAuto: + assert(0 && "Should not see undeduced auto here"); + break; + case BuiltinType::ObjCId: Out << "PAUobjc_object@@"; break; + case BuiltinType::ObjCClass: Out << "PAUobjc_class@@"; break; + case BuiltinType::ObjCSel: Out << "PAUobjc_selector@@"; break; + + case BuiltinType::Char16: + case BuiltinType::Char32: + case BuiltinType::NullPtr: + assert(false && "Don't know how to mangle this type"); + break; + } +} + +// <type> ::= <function-type> +void MicrosoftCXXNameMangler::mangleType(const FunctionProtoType *T) { + // Structors only appear in decls, so at this point we know it's not a + // structor type. + // I'll probably have mangleType(MemberPointerType) call the mangleType() + // method directly. + mangleType(T, NULL, false, false); +} +void MicrosoftCXXNameMangler::mangleType(const FunctionNoProtoType *T) { + llvm_unreachable("Can't mangle K&R function prototypes"); +} + +void MicrosoftCXXNameMangler::mangleType(const FunctionType *T, + const FunctionDecl *D, + bool IsStructor, + bool IsInstMethod) { + // <function-type> ::= <this-cvr-qualifiers> <calling-convention> + // <return-type> <argument-list> <throw-spec> + const FunctionProtoType *Proto = cast<FunctionProtoType>(T); + + // If this is a C++ instance method, mangle the CVR qualifiers for the + // this pointer. + if (IsInstMethod) + mangleQualifiers(Qualifiers::fromCVRMask(Proto->getTypeQuals()), false); + + mangleCallingConvention(T, IsInstMethod); + + // <return-type> ::= <type> + // ::= @ # structors (they have no declared return type) + if (IsStructor) + Out << '@'; + else + mangleType(Proto->getResultType()); + + // <argument-list> ::= X # void + // ::= <type>+ @ + // ::= <type>* Z # varargs + if (Proto->getNumArgs() == 0 && !Proto->isVariadic()) { + Out << 'X'; + } else { + if (D) { + // If we got a decl, use the "types-as-written" to make sure arrays + // get mangled right. + for (FunctionDecl::param_const_iterator Parm = D->param_begin(), + ParmEnd = D->param_end(); + Parm != ParmEnd; ++Parm) + mangleType((*Parm)->getTypeSourceInfo()->getType()); + } else { + for (FunctionProtoType::arg_type_iterator Arg = Proto->arg_type_begin(), + ArgEnd = Proto->arg_type_end(); + Arg != ArgEnd; ++Arg) + mangleType(*Arg); + } + // <builtin-type> ::= Z # ellipsis + if (Proto->isVariadic()) + Out << 'Z'; + else + Out << '@'; + } + + mangleThrowSpecification(Proto); +} + +void MicrosoftCXXNameMangler::mangleFunctionClass(const FunctionDecl *FD) { + // <function-class> ::= A # private: near + // ::= B # private: far + // ::= C # private: static near + // ::= D # private: static far + // ::= E # private: virtual near + // ::= F # private: virtual far + // ::= G # private: thunk near + // ::= H # private: thunk far + // ::= I # protected: near + // ::= J # protected: far + // ::= K # protected: static near + // ::= L # protected: static far + // ::= M # protected: virtual near + // ::= N # protected: virtual far + // ::= O # protected: thunk near + // ::= P # protected: thunk far + // ::= Q # public: near + // ::= R # public: far + // ::= S # public: static near + // ::= T # public: static far + // ::= U # public: virtual near + // ::= V # public: virtual far + // ::= W # public: thunk near + // ::= X # public: thunk far + // ::= Y # global near + // ::= Z # global far + if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD)) { + switch (MD->getAccess()) { + default: + case AS_private: + if (MD->isStatic()) + Out << 'C'; + else if (MD->isVirtual()) + Out << 'E'; + else + Out << 'A'; + break; + case AS_protected: + if (MD->isStatic()) + Out << 'K'; + else if (MD->isVirtual()) + Out << 'M'; + else + Out << 'I'; + break; + case AS_public: + if (MD->isStatic()) + Out << 'S'; + else if (MD->isVirtual()) + Out << 'U'; + else + Out << 'Q'; + } + } else + Out << 'Y'; +} +void MicrosoftCXXNameMangler::mangleCallingConvention(const FunctionType *T, + bool IsInstMethod) { + // <calling-convention> ::= A # __cdecl + // ::= B # __export __cdecl + // ::= C # __pascal + // ::= D # __export __pascal + // ::= E # __thiscall + // ::= F # __export __thiscall + // ::= G # __stdcall + // ::= H # __export __stdcall + // ::= I # __fastcall + // ::= J # __export __fastcall + // The 'export' calling conventions are from a bygone era + // (*cough*Win16*cough*) when functions were declared for export with + // that keyword. (It didn't actually export them, it just made them so + // that they could be in a DLL and somebody from another module could call + // them.) + CallingConv CC = T->getCallConv(); + if (CC == CC_Default) + CC = IsInstMethod ? getASTContext().getDefaultMethodCallConv() : CC_C; + switch (CC) { + case CC_Default: + case CC_C: Out << 'A'; break; + case CC_X86Pascal: Out << 'C'; break; + case CC_X86ThisCall: Out << 'E'; break; + case CC_X86StdCall: Out << 'G'; break; + case CC_X86FastCall: Out << 'I'; break; + } +} +void MicrosoftCXXNameMangler::mangleThrowSpecification( + const FunctionProtoType *FT) { + // <throw-spec> ::= Z # throw(...) (default) + // ::= @ # throw() or __declspec/__attribute__((nothrow)) + // ::= <type>+ + // NOTE: Since the Microsoft compiler ignores throw specifications, they are + // all actually mangled as 'Z'. (They're ignored because their associated + // functionality isn't implemented, and probably never will be.) + Out << 'Z'; +} + +void MicrosoftCXXNameMangler::mangleType(const UnresolvedUsingType *T) { + assert(false && "Don't know how to mangle UnresolvedUsingTypes yet!"); +} + +// <type> ::= <union-type> | <struct-type> | <class-type> | <enum-type> +// <union-type> ::= T <name> +// <struct-type> ::= U <name> +// <class-type> ::= V <name> +// <enum-type> ::= W <size> <name> +void MicrosoftCXXNameMangler::mangleType(const EnumType *T) { + mangleType(static_cast<const TagType*>(T)); +} +void MicrosoftCXXNameMangler::mangleType(const RecordType *T) { + mangleType(static_cast<const TagType*>(T)); +} +void MicrosoftCXXNameMangler::mangleType(const TagType *T) { + switch (T->getDecl()->getTagKind()) { + case TTK_Union: + Out << 'T'; + break; + case TTK_Struct: + Out << 'U'; + break; + case TTK_Class: + Out << 'V'; + break; + case TTK_Enum: + Out << 'W'; + Out << getASTContext().getTypeSizeInChars( + cast<EnumDecl>(T->getDecl())->getIntegerType()).getQuantity(); + break; + } + mangleName(T->getDecl()); +} + +// <type> ::= <array-type> +// <array-type> ::= P <cvr-qualifiers> [Y <dimension-count> <dimension>+] +// <element-type> # as global +// ::= Q <cvr-qualifiers> [Y <dimension-count> <dimension>+] +// <element-type> # as param +// It's supposed to be the other way around, but for some strange reason, it +// isn't. Today this behavior is retained for the sole purpose of backwards +// compatibility. +void MicrosoftCXXNameMangler::mangleType(const ArrayType *T, bool IsGlobal) { + // This isn't a recursive mangling, so now we have to do it all in this + // one call. + if (IsGlobal) + Out << 'P'; + else + Out << 'Q'; + mangleExtraDimensions(T->getElementType()); +} +void MicrosoftCXXNameMangler::mangleType(const ConstantArrayType *T) { + mangleType(static_cast<const ArrayType *>(T), false); +} +void MicrosoftCXXNameMangler::mangleType(const VariableArrayType *T) { + mangleType(static_cast<const ArrayType *>(T), false); +} +void MicrosoftCXXNameMangler::mangleType(const DependentSizedArrayType *T) { + mangleType(static_cast<const ArrayType *>(T), false); +} +void MicrosoftCXXNameMangler::mangleType(const IncompleteArrayType *T) { + mangleType(static_cast<const ArrayType *>(T), false); +} +void MicrosoftCXXNameMangler::mangleExtraDimensions(QualType ElementTy) { + llvm::SmallVector<llvm::APInt, 3> Dimensions; + for (;;) { + if (ElementTy->isConstantArrayType()) { + const ConstantArrayType *CAT = + static_cast<const ConstantArrayType *>(ElementTy.getTypePtr()); + Dimensions.push_back(CAT->getSize()); + ElementTy = CAT->getElementType(); + } else if (ElementTy->isVariableArrayType()) { + assert(false && "Don't know how to mangle VLAs!"); + } else if (ElementTy->isDependentSizedArrayType()) { + // The dependent expression has to be folded into a constant (TODO). + assert(false && "Don't know how to mangle dependent-sized arrays!"); + } else if (ElementTy->isIncompleteArrayType()) continue; + else break; + } + mangleQualifiers(ElementTy.getQualifiers(), false); + // If there are any additional dimensions, mangle them now. + if (Dimensions.size() > 0) { + Out << 'Y'; + // <dimension-count> ::= <number> # number of extra dimensions + mangleNumber(Dimensions.size()); + for (unsigned Dim = 0; Dim < Dimensions.size(); ++Dim) { + mangleNumber(Dimensions[Dim].getLimitedValue()); + } + } + mangleType(ElementTy.getLocalUnqualifiedType()); +} + +// <type> ::= <pointer-to-member-type> +// <pointer-to-member-type> ::= <pointer-cvr-qualifiers> <cvr-qualifiers> +// <class name> <type> +void MicrosoftCXXNameMangler::mangleType(const MemberPointerType *T) { + QualType PointeeType = T->getPointeeType(); + if (const FunctionProtoType *FPT = dyn_cast<FunctionProtoType>(PointeeType)) { + Out << '8'; + mangleName(cast<RecordType>(T->getClass())->getDecl()); + mangleType(FPT, NULL, false, true); + } else { + mangleQualifiers(PointeeType.getQualifiers(), true); + mangleName(cast<RecordType>(T->getClass())->getDecl()); + mangleType(PointeeType.getLocalUnqualifiedType()); + } +} + +void MicrosoftCXXNameMangler::mangleType(const TemplateTypeParmType *T) { + assert(false && "Don't know how to mangle TemplateTypeParmTypes yet!"); +} + +// <type> ::= <pointer-type> +// <pointer-type> ::= <pointer-cvr-qualifiers> <cvr-qualifiers> <type> +void MicrosoftCXXNameMangler::mangleType(const PointerType *T) { + QualType PointeeTy = T->getPointeeType(); + if (PointeeTy->isArrayType()) { + // Pointers to arrays are mangled like arrays. + mangleExtraDimensions(T->getPointeeType()); + } else if (PointeeTy->isFunctionType()) { + // Function pointers are special. + Out << '6'; + mangleType(static_cast<const FunctionType *>(PointeeTy.getTypePtr()), + NULL, false, false); + } else { + if (!PointeeTy.hasQualifiers()) + // Lack of qualifiers is mangled as 'A'. + Out << 'A'; + mangleType(PointeeTy); + } +} +void MicrosoftCXXNameMangler::mangleType(const ObjCObjectPointerType *T) { + // Object pointers never have qualifiers. + Out << 'A'; + mangleType(T->getPointeeType()); +} + +// <type> ::= <reference-type> +// <reference-type> ::= A <cvr-qualifiers> <type> +void MicrosoftCXXNameMangler::mangleType(const LValueReferenceType *T) { + Out << 'A'; + QualType PointeeTy = T->getPointeeType(); + if (!PointeeTy.hasQualifiers()) + // Lack of qualifiers is mangled as 'A'. + Out << 'A'; + mangleType(PointeeTy); +} + +void MicrosoftCXXNameMangler::mangleType(const RValueReferenceType *T) { + assert(false && "Don't know how to mangle RValueReferenceTypes yet!"); +} + +void MicrosoftCXXNameMangler::mangleType(const ComplexType *T) { + assert(false && "Don't know how to mangle ComplexTypes yet!"); +} + +void MicrosoftCXXNameMangler::mangleType(const VectorType *T) { + assert(false && "Don't know how to mangle VectorTypes yet!"); +} +void MicrosoftCXXNameMangler::mangleType(const ExtVectorType *T) { + assert(false && "Don't know how to mangle ExtVectorTypes yet!"); +} +void MicrosoftCXXNameMangler::mangleType(const DependentSizedExtVectorType *T) { + assert(false && "Don't know how to mangle DependentSizedExtVectorTypes yet!"); +} + +void MicrosoftCXXNameMangler::mangleType(const ObjCInterfaceType *T) { + // ObjC interfaces have structs underlying them. + Out << 'U'; + mangleName(T->getDecl()); +} + +void MicrosoftCXXNameMangler::mangleType(const ObjCObjectType *T) { + // We don't allow overloading by different protocol qualification, + // so mangling them isn't necessary. + mangleType(T->getBaseType()); +} + +void MicrosoftCXXNameMangler::mangleType(const BlockPointerType *T) { + Out << "_E"; + mangleType(T->getPointeeType()); +} + +void MicrosoftCXXNameMangler::mangleType(const InjectedClassNameType *T) { + assert(false && "Don't know how to mangle InjectedClassNameTypes yet!"); +} + +void MicrosoftCXXNameMangler::mangleType(const TemplateSpecializationType *T) { + assert(false && "Don't know how to mangle TemplateSpecializationTypes yet!"); +} + +void MicrosoftCXXNameMangler::mangleType(const DependentNameType *T) { + assert(false && "Don't know how to mangle DependentNameTypes yet!"); +} + +void MicrosoftCXXNameMangler::mangleType( + const DependentTemplateSpecializationType *T) { + assert(false && + "Don't know how to mangle DependentTemplateSpecializationTypes yet!"); +} + +void MicrosoftCXXNameMangler::mangleType(const PackExpansionType *T) { + assert(false && "Don't know how to mangle PackExpansionTypes yet!"); +} + +void MicrosoftCXXNameMangler::mangleType(const TypeOfType *T) { + assert(false && "Don't know how to mangle TypeOfTypes yet!"); +} + +void MicrosoftCXXNameMangler::mangleType(const TypeOfExprType *T) { + assert(false && "Don't know how to mangle TypeOfExprTypes yet!"); +} + +void MicrosoftCXXNameMangler::mangleType(const DecltypeType *T) { + assert(false && "Don't know how to mangle DecltypeTypes yet!"); +} + +void MicrosoftMangleContext::mangleName(const NamedDecl *D, + llvm::SmallVectorImpl<char> &Name) { + assert((isa<FunctionDecl>(D) || isa<VarDecl>(D)) && + "Invalid mangleName() call, argument is not a variable or function!"); + assert(!isa<CXXConstructorDecl>(D) && !isa<CXXDestructorDecl>(D) && + "Invalid mangleName() call on 'structor decl!"); + + PrettyStackTraceDecl CrashInfo(D, SourceLocation(), + getASTContext().getSourceManager(), + "Mangling declaration"); + + MicrosoftCXXNameMangler Mangler(*this, Name); + return Mangler.mangle(D); +} +void MicrosoftMangleContext::mangleThunk(const CXXMethodDecl *MD, + const ThunkInfo &Thunk, + llvm::SmallVectorImpl<char> &) { + assert(false && "Can't yet mangle thunks!"); +} +void MicrosoftMangleContext::mangleCXXDtorThunk(const CXXDestructorDecl *DD, + CXXDtorType Type, + const ThisAdjustment &, + llvm::SmallVectorImpl<char> &) { + assert(false && "Can't yet mangle destructor thunks!"); +} +void MicrosoftMangleContext::mangleCXXVTable(const CXXRecordDecl *RD, + llvm::SmallVectorImpl<char> &) { + assert(false && "Can't yet mangle virtual tables!"); +} +void MicrosoftMangleContext::mangleCXXVTT(const CXXRecordDecl *RD, + llvm::SmallVectorImpl<char> &) { + llvm_unreachable("The MS C++ ABI does not have virtual table tables!"); +} +void MicrosoftMangleContext::mangleCXXCtorVTable(const CXXRecordDecl *RD, + int64_t Offset, + const CXXRecordDecl *Type, + llvm::SmallVectorImpl<char> &) { + llvm_unreachable("The MS C++ ABI does not have constructor vtables!"); +} +void MicrosoftMangleContext::mangleCXXRTTI(QualType T, + llvm::SmallVectorImpl<char> &) { + assert(false && "Can't yet mangle RTTI!"); +} +void MicrosoftMangleContext::mangleCXXRTTIName(QualType T, + llvm::SmallVectorImpl<char> &) { + assert(false && "Can't yet mangle RTTI names!"); +} +void MicrosoftMangleContext::mangleCXXCtor(const CXXConstructorDecl *D, + CXXCtorType Type, + llvm::SmallVectorImpl<char> &) { + assert(false && "Can't yet mangle constructors!"); +} +void MicrosoftMangleContext::mangleCXXDtor(const CXXDestructorDecl *D, + CXXDtorType Type, + llvm::SmallVectorImpl<char> &) { + assert(false && "Can't yet mangle destructors!"); +} +void MicrosoftMangleContext::mangleReferenceTemporary(const clang::VarDecl *, + llvm::SmallVectorImpl<char> &) { + assert(false && "Can't yet mangle reference temporaries!"); +} + +MangleContext *clang::createMicrosoftMangleContext(ASTContext &Context, + Diagnostic &Diags) { + return new MicrosoftMangleContext(Context, Diags); +} diff --git a/clang/lib/CodeGen/CGBlocks.cpp b/clang/lib/CodeGen/CGBlocks.cpp index 36767711b48..daa9052fdac 100644 --- a/clang/lib/CodeGen/CGBlocks.cpp +++ b/clang/lib/CodeGen/CGBlocks.cpp @@ -799,7 +799,7 @@ CodeGenFunction::GenerateBlockFunction(GlobalDecl GD, const BlockExpr *BExpr, const llvm::FunctionType *LTy = Types.GetFunctionType(FI, IsVariadic); MangleBuffer Name; - CGM.getMangledName(GD, Name, BD); + CGM.getBlockMangledName(GD, Name, BD); llvm::Function *Fn = llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage, Name.getString(), &CGM.getModule()); diff --git a/clang/lib/CodeGen/CGCXX.cpp b/clang/lib/CodeGen/CGCXX.cpp index b734e62205c..bb217fbcc9d 100644 --- a/clang/lib/CodeGen/CGCXX.cpp +++ b/clang/lib/CodeGen/CGCXX.cpp @@ -16,12 +16,12 @@ #include "CGCXXABI.h" #include "CodeGenFunction.h" #include "CodeGenModule.h" -#include "Mangle.h" #include "clang/AST/ASTContext.h" #include "clang/AST/RecordLayout.h" #include "clang/AST/Decl.h" #include "clang/AST/DeclCXX.h" #include "clang/AST/DeclObjC.h" +#include "clang/AST/Mangle.h" #include "clang/AST/StmtCXX.h" #include "clang/Frontend/CodeGenOptions.h" #include "llvm/ADT/StringExtras.h" diff --git a/clang/lib/CodeGen/CGCXX.h b/clang/lib/CodeGen/CGCXX.h deleted file mode 100644 index 1e6adb05a0d..00000000000 --- a/clang/lib/CodeGen/CGCXX.h +++ /dev/null @@ -1,36 +0,0 @@ -//===----- CGCXX.h - C++ related code CodeGen declarations ------*- C++ -*-===// -// -// The LLVM Compiler Infrastructure -// -// This file is distributed under the University of Illinois Open Source -// License. See LICENSE.TXT for details. -// -//===----------------------------------------------------------------------===// -// -// These classes wrap the information about a call or function -// definition used to handle ABI compliancy. -// -//===----------------------------------------------------------------------===// - -#ifndef CLANG_CODEGEN_CGCXX_H -#define CLANG_CODEGEN_CGCXX_H - -namespace clang { - -/// CXXCtorType - C++ constructor types -enum CXXCtorType { - Ctor_Complete, // Complete object ctor - Ctor_Base, // Base object ctor - Ctor_CompleteAllocating // Complete object allocating ctor -}; - -/// CXXDtorType - C++ destructor types -enum CXXDtorType { - Dtor_Deleting, // Deleting dtor - Dtor_Complete, // Complete object dtor - Dtor_Base // Base object dtor -}; - -} // end namespace clang - -#endif // CLANG_CODEGEN_CGCXX_H diff --git a/clang/lib/CodeGen/CGCXXABI.h b/clang/lib/CodeGen/CGCXXABI.h index 56c4ea98ae4..8bc1385d7f8 100644 --- a/clang/lib/CodeGen/CGCXXABI.h +++ b/clang/lib/CodeGen/CGCXXABI.h @@ -32,18 +32,20 @@ namespace clang { class CXXMethodDecl; class CXXRecordDecl; class FieldDecl; + class MangleContext; namespace CodeGen { class CodeGenFunction; class CodeGenModule; - class MangleContext; /// Implements C++ ABI-specific code generation functions. class CGCXXABI { protected: CodeGenModule &CGM; + llvm::OwningPtr<MangleContext> MangleCtx; - CGCXXABI(CodeGenModule &CGM) : CGM(CGM) {} + CGCXXABI(CodeGenModule &CGM) + : CGM(CGM), MangleCtx(CGM.getContext().createMangleContext()) {} protected: ImplicitParamDecl *&getThisDecl(CodeGenFunction &CGF) { @@ -74,7 +76,9 @@ public: virtual ~CGCXXABI(); /// Gets the mangle context. - virtual MangleContext &getMangleContext() = 0; + MangleContext &getMangleContext() { + return *MangleCtx; + } /// Find the LLVM type used to represent the given member pointer /// type. diff --git a/clang/lib/CodeGen/CGDecl.cpp b/clang/lib/CodeGen/CGDecl.cpp index 77984b11cca..16a3a196bbb 100644 --- a/clang/lib/CodeGen/CGDecl.cpp +++ b/clang/lib/CodeGen/CGDecl.cpp @@ -148,7 +148,7 @@ static std::string GetStaticDeclName(CodeGenFunction &CGF, const VarDecl &D, const DeclContext *DC = ND->getDeclContext(); if (const BlockDecl *BD = dyn_cast<BlockDecl>(DC)) { MangleBuffer Name; - CGM.getMangledName(GlobalDecl(), Name, BD); + CGM.getBlockMangledName(GlobalDecl(), Name, BD); ContextName = Name.getString(); } else diff --git a/clang/lib/CodeGen/CGObjC.cpp b/clang/lib/CodeGen/CGObjC.cpp index 3574ba2c185..c7e0837b3dc 100644 --- a/clang/lib/CodeGen/CGObjC.cpp +++ b/clang/lib/CodeGen/CGObjC.cpp @@ -137,6 +137,8 @@ void CodeGenFunction::StartObjCMethod(const ObjCMethodDecl *OMD, E = OMD->param_end(); PI != E; ++PI) Args.push_back(std::make_pair(*PI, (*PI)->getType())); + CurGD = OMD; + StartFunction(OMD, OMD->getResultType(), Fn, Args, OMD->getLocStart()); } diff --git a/clang/lib/CodeGen/CGVTables.h b/clang/lib/CodeGen/CGVTables.h index 1d2e8c1e044..3e3cd4b165a 100644 --- a/clang/lib/CodeGen/CGVTables.h +++ b/clang/lib/CodeGen/CGVTables.h @@ -16,6 +16,7 @@ #include "llvm/ADT/DenseMap.h" #include "llvm/GlobalVariable.h" +#include "clang/Basic/ABI.h" #include "GlobalDecl.h" namespace clang { @@ -24,94 +25,6 @@ namespace clang { namespace CodeGen { class CodeGenModule; -/// ReturnAdjustment - A return adjustment. -struct ReturnAdjustment { - /// NonVirtual - The non-virtual adjustment from the derived object to its - /// nearest virtual base. - int64_t NonVirtual; - - /// VBaseOffsetOffset - The offset (in bytes), relative to the address point - /// of the virtual base class offset. - int64_t VBaseOffsetOffset; - - ReturnAdjustment() : NonVirtual(0), VBaseOffsetOffset(0) { } - - bool isEmpty() const { return !NonVirtual && !VBaseOffsetOffset; } - - friend bool operator==(const ReturnAdjustment &LHS, - const ReturnAdjustment &RHS) { - return LHS.NonVirtual == RHS.NonVirtual && - LHS.VBaseOffsetOffset == RHS.VBaseOffsetOffset; - } - - friend bool operator<(const ReturnAdjustment &LHS, - const ReturnAdjustment &RHS) { - if (LHS.NonVirtual < RHS.NonVirtual) - return true; - - return LHS.NonVirtual == RHS.NonVirtual && - LHS.VBaseOffsetOffset < RHS.VBaseOffsetOffset; - } -}; - -/// ThisAdjustment - A 'this' pointer adjustment. -struct ThisAdjustment { - /// NonVirtual - The non-virtual adjustment from the derived object to its - /// nearest virtual base. - int64_t NonVirtual; - - /// VCallOffsetOffset - The offset (in bytes), relative to the address point, - /// of the virtual call offset. - int64_t VCallOffsetOffset; - - ThisAdjustment() : NonVirtual(0), VCallOffsetOffset(0) { } - - bool isEmpty() const { return !NonVirtual && !VCallOffsetOffset; } - - friend bool operator==(const ThisAdjustment &LHS, - const ThisAdjustment &RHS) { - return LHS.NonVirtual == RHS.NonVirtual && - LHS.VCallOffsetOffset == RHS.VCallOffsetOffset; - } - - friend bool operator<(const ThisAdjustment &LHS, - const ThisAdjustment &RHS) { - if (LHS.NonVirtual < RHS.NonVirtual) - return true; - - return LHS.NonVirtual == RHS.NonVirtual && - LHS.VCallOffsetOffset < RHS.VCallOffsetOffset; - } -}; - -/// ThunkInfo - The 'this' pointer adjustment as well as an optional return -/// adjustment for a thunk. -struct ThunkInfo { - /// This - The 'this' pointer adjustment. - ThisAdjustment This; - - /// Return - The return adjustment. - ReturnAdjustment Return; - - ThunkInfo() { } - - ThunkInfo(const ThisAdjustment &This, const ReturnAdjustment &Return) - : This(This), Return(Return) { } - - friend bool operator==(const ThunkInfo &LHS, const ThunkInfo &RHS) { - return LHS.This == RHS.This && LHS.Return == RHS.Return; - } - - friend bool operator<(const ThunkInfo &LHS, const ThunkInfo &RHS) { - if (LHS.This < RHS.This) - return true; - - return LHS.This == RHS.This && LHS.Return < RHS.Return; - } - - bool isEmpty() const { return This.isEmpty() && Return.isEmpty(); } -}; - // BaseSubobject - Uniquely identifies a direct or indirect base class. // Stores both the base class decl and the offset from the most derived class to // the base class. diff --git a/clang/lib/CodeGen/CMakeLists.txt b/clang/lib/CodeGen/CMakeLists.txt index b985d685045..1f336789935 100644 --- a/clang/lib/CodeGen/CMakeLists.txt +++ b/clang/lib/CodeGen/CMakeLists.txt @@ -35,7 +35,6 @@ add_clang_library(clangCodeGen CodeGenTBAA.cpp CodeGenTypes.cpp ItaniumCXXABI.cpp - Mangle.cpp MicrosoftCXXABI.cpp ModuleBuilder.cpp TargetInfo.cpp diff --git a/clang/lib/CodeGen/CodeGenFunction.h b/clang/lib/CodeGen/CodeGenFunction.h index 7077b89ec2c..5f21d9fa434 100644 --- a/clang/lib/CodeGen/CodeGenFunction.h +++ b/clang/lib/CodeGen/CodeGenFunction.h @@ -18,6 +18,7 @@ #include "clang/AST/ExprCXX.h" #include "clang/AST/ExprObjC.h" #include "clang/AST/CharUnits.h" +#include "clang/Basic/ABI.h" #include "clang/Basic/TargetInfo.h" #include "llvm/ADT/DenseMap.h" #include "llvm/ADT/SmallVector.h" @@ -26,7 +27,6 @@ #include "CGBlocks.h" #include "CGBuilder.h" #include "CGCall.h" -#include "CGCXX.h" #include "CGValue.h" namespace llvm { diff --git a/clang/lib/CodeGen/CodeGenModule.cpp b/clang/lib/CodeGen/CodeGenModule.cpp index 9ed3150ff56..df90011104c 100644 --- a/clang/lib/CodeGen/CodeGenModule.cpp +++ b/clang/lib/CodeGen/CodeGenModule.cpp @@ -18,7 +18,6 @@ #include "CGCall.h" #include "CGCXXABI.h" #include "CGObjCRuntime.h" -#include "Mangle.h" #include "TargetInfo.h" #include "clang/Frontend/CodeGenOptions.h" #include "clang/AST/ASTContext.h" @@ -26,6 +25,7 @@ #include "clang/AST/DeclObjC.h" #include "clang/AST/DeclCXX.h" #include "clang/AST/DeclTemplate.h" +#include "clang/AST/Mangle.h" #include "clang/AST/RecordLayout.h" #include "clang/Basic/Builtins.h" #include "clang/Basic/Diagnostic.h" @@ -275,7 +275,7 @@ llvm::StringRef CodeGenModule::getMangledName(GlobalDecl GD) { else if (const CXXDestructorDecl *D = dyn_cast<CXXDestructorDecl>(ND)) getCXXABI().getMangleContext().mangleCXXDtor(D, GD.getDtorType(), Buffer); else if (const BlockDecl *BD = dyn_cast<BlockDecl>(ND)) - getCXXABI().getMangleContext().mangleBlock(GD, BD, Buffer); + getCXXABI().getMangleContext().mangleBlock(BD, Buffer); else getCXXABI().getMangleContext().mangleName(ND, Buffer); @@ -289,9 +289,18 @@ llvm::StringRef CodeGenModule::getMangledName(GlobalDecl GD) { return Str; } -void CodeGenModule::getMangledName(GlobalDecl GD, MangleBuffer &Buffer, - const BlockDecl *BD) { - getCXXABI().getMangleContext().mangleBlock(GD, BD, Buffer.getBuffer()); +void CodeGenModule::getBlockMangledName(GlobalDecl GD, MangleBuffer &Buffer, + const BlockDecl *BD) { + MangleContext &MangleCtx = getCXXABI().getMangleContext(); + const Decl *D = GD.getDecl(); + if (D == 0) + MangleCtx.mangleGlobalBlock(BD, Buffer.getBuffer()); + else if (const CXXConstructorDecl *CD = dyn_cast<CXXConstructorDecl>(D)) + MangleCtx.mangleCtorBlock(CD, GD.getCtorType(), BD, Buffer.getBuffer()); + else if (const CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(D)) + MangleCtx.mangleDtorBlock(DD, GD.getDtorType(), BD, Buffer.getBuffer()); + else + MangleCtx.mangleBlock(cast<DeclContext>(D), BD, Buffer.getBuffer()); } llvm::GlobalValue *CodeGenModule::GetGlobalValue(llvm::StringRef Name) { @@ -1304,7 +1313,6 @@ static void ReplaceUsesOfNonProtoTypeWithRealFunction(llvm::GlobalValue *Old, void CodeGenModule::EmitGlobalFunctionDefinition(GlobalDecl GD) { const FunctionDecl *D = cast<FunctionDecl>(GD.getDecl()); const llvm::FunctionType *Ty = getTypes().GetFunctionType(GD); - getCXXABI().getMangleContext().mangleInitDiscriminator(); // Get or create the prototype for the function. llvm::Constant *Entry = GetAddrOfFunction(GD, Ty); diff --git a/clang/lib/CodeGen/CodeGenModule.h b/clang/lib/CodeGen/CodeGenModule.h index 14a0e497a00..b1395be8c54 100644 --- a/clang/lib/CodeGen/CodeGenModule.h +++ b/clang/lib/CodeGen/CodeGenModule.h @@ -14,17 +14,17 @@ #ifndef CLANG_CODEGEN_CODEGENMODULE_H #define CLANG_CODEGEN_CODEGENMODULE_H +#include "clang/Basic/ABI.h" #include "clang/Basic/LangOptions.h" #include "clang/AST/Attr.h" #include "clang/AST/DeclCXX.h" #include "clang/AST/DeclObjC.h" +#include "clang/AST/Mangle.h" #include "CGBlocks.h" #include "CGCall.h" -#include "CGCXX.h" #include "CGVTables.h" #include "CodeGenTypes.h" #include "GlobalDecl.h" -#include "Mangle.h" #include "llvm/Module.h" #include "llvm/ADT/DenseMap.h" #include "llvm/ADT/StringMap.h" @@ -66,6 +66,7 @@ namespace clang { class Diagnostic; class AnnotateAttr; class CXXDestructorDecl; + class MangleBuffer; namespace CodeGen { @@ -74,7 +75,6 @@ namespace CodeGen { class CGCXXABI; class CGDebugInfo; class CGObjCRuntime; - class MangleBuffer; struct OrderGlobalInits { unsigned int priority; @@ -480,7 +480,8 @@ public: unsigned &CallingConv); llvm::StringRef getMangledName(GlobalDecl GD); - void getMangledName(GlobalDecl GD, MangleBuffer &Buffer, const BlockDecl *BD); + void getBlockMangledName(GlobalDecl GD, MangleBuffer &Buffer, + const BlockDecl *BD); void EmitTentativeDefinition(const VarDecl *D); diff --git a/clang/lib/CodeGen/CodeGenTBAA.cpp b/clang/lib/CodeGen/CodeGenTBAA.cpp index 61dc41ed450..71b43010e63 100644 --- a/clang/lib/CodeGen/CodeGenTBAA.cpp +++ b/clang/lib/CodeGen/CodeGenTBAA.cpp @@ -16,8 +16,8 @@ //===----------------------------------------------------------------------===// #include "CodeGenTBAA.h" -#include "Mangle.h" #include "clang/AST/ASTContext.h" +#include "clang/AST/Mangle.h" #include "llvm/LLVMContext.h" #include "llvm/Metadata.h" #include "llvm/Constants.h" diff --git a/clang/lib/CodeGen/CodeGenTBAA.h b/clang/lib/CodeGen/CodeGenTBAA.h index 29f7f544d31..c4583473a0e 100644 --- a/clang/lib/CodeGen/CodeGenTBAA.h +++ b/clang/lib/CodeGen/CodeGenTBAA.h @@ -26,11 +26,11 @@ namespace llvm { namespace clang { class ASTContext; class LangOptions; + class MangleContext; class QualType; class Type; namespace CodeGen { - class MangleContext; class CGRecordLayout; /// CodeGenTBAA - This class organizes the cross-module state that is used diff --git a/clang/lib/CodeGen/GlobalDecl.h b/clang/lib/CodeGen/GlobalDecl.h index 26dea402f4b..ab3e2019a6a 100644 --- a/clang/lib/CodeGen/GlobalDecl.h +++ b/clang/lib/CodeGen/GlobalDecl.h @@ -15,9 +15,9 @@ #ifndef CLANG_CODEGEN_GLOBALDECL_H #define CLANG_CODEGEN_GLOBALDECL_H -#include "CGCXX.h" #include "clang/AST/DeclCXX.h" #include "clang/AST/DeclObjC.h" +#include "clang/Basic/ABI.h" namespace clang { diff --git a/clang/lib/CodeGen/ItaniumCXXABI.cpp b/clang/lib/CodeGen/ItaniumCXXABI.cpp index 99575dfc8f5..554d2403c01 100644 --- a/clang/lib/CodeGen/ItaniumCXXABI.cpp +++ b/clang/lib/CodeGen/ItaniumCXXABI.cpp @@ -22,7 +22,7 @@ #include "CGRecordLayout.h" #include "CodeGenFunction.h" #include "CodeGenModule.h" -#include "Mangle.h" +#include <clang/AST/Mangle.h> #include <clang/AST/Type.h> #include <llvm/Target/TargetData.h> #include <llvm/Value.h> @@ -35,7 +35,6 @@ class ItaniumCXXABI : public CodeGen::CGCXXABI { private: const llvm::IntegerType *PtrDiffTy; protected: - CodeGen::MangleContext MangleCtx; bool IsARM; // It's a little silly for us to cache this. @@ -52,12 +51,7 @@ protected: public: ItaniumCXXABI(CodeGen::CodeGenModule &CGM, bool IsARM = false) : - CGCXXABI(CGM), PtrDiffTy(0), MangleCtx(getContext(), CGM.getDiags()), - IsARM(IsARM) { } - - CodeGen::MangleContext &getMangleContext() { - return MangleCtx; - } + CGCXXABI(CGM), PtrDiffTy(0), IsARM(IsARM) { } bool isZeroInitializable(const MemberPointerType *MPT); diff --git a/clang/lib/CodeGen/MicrosoftCXXABI.cpp b/clang/lib/CodeGen/MicrosoftCXXABI.cpp index 875c66c2a47..ea2e55fcb28 100644 --- a/clang/lib/CodeGen/MicrosoftCXXABI.cpp +++ b/clang/lib/CodeGen/MicrosoftCXXABI.cpp @@ -16,105 +16,17 @@ #include "CGCXXABI.h" #include "CodeGenModule.h" -#include "Mangle.h" -#include "clang/AST/ASTContext.h" #include "clang/AST/Decl.h" #include "clang/AST/DeclCXX.h" -#include "clang/AST/DeclTemplate.h" -#include "clang/AST/ExprCXX.h" -#include "CGVTables.h" using namespace clang; using namespace CodeGen; namespace { -/// MicrosoftCXXNameMangler - Manage the mangling of a single name for the -/// Microsoft Visual C++ ABI. -class MicrosoftCXXNameMangler { - MangleContext &Context; - llvm::raw_svector_ostream Out; - - ASTContext &getASTContext() const { return Context.getASTContext(); } - -public: - MicrosoftCXXNameMangler(MangleContext &C, llvm::SmallVectorImpl<char> &Res) - : Context(C), Out(Res) { } - - void mangle(const NamedDecl *D, llvm::StringRef Prefix = "?"); - void mangleName(const NamedDecl *ND); - void mangleFunctionEncoding(const FunctionDecl *FD); - void mangleVariableEncoding(const VarDecl *VD); - void mangleNumber(int64_t Number); - void mangleType(QualType T); - -private: - void mangleUnqualifiedName(const NamedDecl *ND) { - mangleUnqualifiedName(ND, ND->getDeclName()); - } - void mangleUnqualifiedName(const NamedDecl *ND, DeclarationName Name); - void mangleSourceName(const IdentifierInfo *II); - void manglePostfix(const DeclContext *DC, bool NoFunction=false); - void mangleOperatorName(OverloadedOperatorKind OO); - void mangleQualifiers(Qualifiers Quals, bool IsMember); - - void mangleObjCMethodName(const ObjCMethodDecl *MD); - - // Declare manglers for every type class. -#define ABSTRACT_TYPE(CLASS, PARENT) -#define NON_CANONICAL_TYPE(CLASS, PARENT) -#define TYPE(CLASS, PARENT) void mangleType(const CLASS##Type *T); -#include "clang/AST/TypeNodes.def" - - void mangleType(const TagType*); - void mangleType(const FunctionType *T, const FunctionDecl *D, - bool IsStructor, bool IsInstMethod); - void mangleType(const ArrayType *T, bool IsGlobal); - void mangleExtraDimensions(QualType T); - void mangleFunctionClass(const FunctionDecl *FD); - void mangleCallingConvention(const FunctionType *T, bool IsInstMethod = false); - void mangleThrowSpecification(const FunctionProtoType *T); - -}; - -/// MicrosoftMangleContext - Overrides the default MangleContext for the -/// Microsoft Visual C++ ABI. -class MicrosoftMangleContext : public MangleContext { -public: - MicrosoftMangleContext(ASTContext &Context, - Diagnostic &Diags) : MangleContext(Context, Diags) { } - virtual bool shouldMangleDeclName(const NamedDecl *D); - virtual void mangleName(const NamedDecl *D, llvm::SmallVectorImpl<char> &); - virtual void mangleThunk(const CXXMethodDecl *MD, - const ThunkInfo &Thunk, - llvm::SmallVectorImpl<char> &); - virtual void mangleCXXDtorThunk(const CXXDestructorDecl *DD, CXXDtorType Type, - const ThisAdjustment &ThisAdjustment, - llvm::SmallVectorImpl<char> &); - virtual void mangleCXXVTable(const CXXRecordDecl *RD, - llvm::SmallVectorImpl<char> &); - virtual void mangleCXXVTT(const CXXRecordDecl *RD, - llvm::SmallVectorImpl<char> &); - virtual void mangleCXXCtorVTable(const CXXRecordDecl *RD, int64_t Offset, - const CXXRecordDecl *Type, - llvm::SmallVectorImpl<char> &); - virtual void mangleCXXRTTI(QualType T, llvm::SmallVectorImpl<char> &); - virtual void mangleCXXRTTIName(QualType T, llvm::SmallVectorImpl<char> &); - virtual void mangleCXXCtor(const CXXConstructorDecl *D, CXXCtorType Type, - llvm::SmallVectorImpl<char> &); - virtual void mangleCXXDtor(const CXXDestructorDecl *D, CXXDtorType Type, - llvm::SmallVectorImpl<char> &); -}; - class MicrosoftCXXABI : public CGCXXABI { - MicrosoftMangleContext MangleCtx; public: - MicrosoftCXXABI(CodeGenModule &CGM) - : CGCXXABI(CGM), MangleCtx(CGM.getContext(), CGM.getDiags()) {} - - MicrosoftMangleContext &getMangleContext() { - return MangleCtx; - } + MicrosoftCXXABI(CodeGenModule &CGM) : CGCXXABI(CGM) {} void BuildConstructorSignature(const CXXConstructorDecl *Ctor, CXXCtorType Type, @@ -147,1074 +59,6 @@ public: } -static bool isInCLinkageSpecification(const Decl *D) { - D = D->getCanonicalDecl(); - for (const DeclContext *DC = D->getDeclContext(); - !DC->isTranslationUnit(); DC = DC->getParent()) { - if (const LinkageSpecDecl *Linkage = dyn_cast<LinkageSpecDecl>(DC)) - return Linkage->getLanguage() == LinkageSpecDecl::lang_c; - } - - return false; -} - -bool MicrosoftMangleContext::shouldMangleDeclName(const NamedDecl *D) { - // In C, functions with no attributes never need to be mangled. Fastpath them. - if (!getASTContext().getLangOptions().CPlusPlus && !D->hasAttrs()) - return false; - - // Any decl can be declared with __asm("foo") on it, and this takes precedence - // over all other naming in the .o file. - if (D->hasAttr<AsmLabelAttr>()) - return true; - - // Clang's "overloadable" attribute extension to C/C++ implies name mangling - // (always) as does passing a C++ member function and a function - // whose name is not a simple identifier. - const FunctionDecl *FD = dyn_cast<FunctionDecl>(D); - if (FD && (FD->hasAttr<OverloadableAttr>() || isa<CXXMethodDecl>(FD) || - !FD->getDeclName().isIdentifier())) - return true; - - // Otherwise, no mangling is done outside C++ mode. - if (!getASTContext().getLangOptions().CPlusPlus) - return false; - - // Variables at global scope with internal linkage are not mangled. - if (!FD) { - const DeclContext *DC = D->getDeclContext(); - if (DC->isTranslationUnit() && D->getLinkage() == InternalLinkage) - return false; - } - - // C functions and "main" are not mangled. - if ((FD && FD->isMain()) || isInCLinkageSpecification(D)) - return false; - - return true; -} - -void MicrosoftCXXNameMangler::mangle(const NamedDecl *D, - llvm::StringRef Prefix) { - // MSVC doesn't mangle C++ names the same way it mangles extern "C" names. - // Therefore it's really important that we don't decorate the - // name with leading underscores or leading/trailing at signs. So, emit a - // asm marker at the start so we get the name right. - Out << '\01'; // LLVM IR Marker for __asm("foo") - - // Any decl can be declared with __asm("foo") on it, and this takes precedence - // over all other naming in the .o file. - if (const AsmLabelAttr *ALA = D->getAttr<AsmLabelAttr>()) { - // If we have an asm name, then we use it as the mangling. - Out << ALA->getLabel(); - return; - } - - // <mangled-name> ::= ? <name> <type-encoding> - Out << Prefix; - mangleName(D); - if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) - mangleFunctionEncoding(FD); - else if (const VarDecl *VD = dyn_cast<VarDecl>(D)) - mangleVariableEncoding(VD); - // TODO: Fields? Can MSVC even mangle them? -} - -void MicrosoftCXXNameMangler::mangleFunctionEncoding(const FunctionDecl *FD) { - // <type-encoding> ::= <function-class> <function-type> - - // Don't mangle in the type if this isn't a decl we should typically mangle. - if (!Context.shouldMangleDeclName(FD)) - return; - - // We should never ever see a FunctionNoProtoType at this point. - // We don't even know how to mangle their types anyway :). - const FunctionProtoType *FT = cast<FunctionProtoType>(FD->getType()); - - bool InStructor = false, InInstMethod = false; - const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD); - if (MD) { - if (MD->isInstance()) - InInstMethod = true; - if (isa<CXXConstructorDecl>(MD) || isa<CXXDestructorDecl>(MD)) - InStructor = true; - } - - // First, the function class. - mangleFunctionClass(FD); - - mangleType(FT, FD, InStructor, InInstMethod); -} - -void MicrosoftCXXNameMangler::mangleVariableEncoding(const VarDecl *VD) { - // <type-encoding> ::= <storage-class> <variable-type> - // <storage-class> ::= 0 # private static member - // ::= 1 # protected static member - // ::= 2 # public static member - // ::= 3 # global - // ::= 4 # static local - - // The first character in the encoding (after the name) is the storage class. - if (VD->isStaticDataMember()) { - // If it's a static member, it also encodes the access level. - switch (VD->getAccess()) { - default: - case AS_private: Out << '0'; break; - case AS_protected: Out << '1'; break; - case AS_public: Out << '2'; break; - } - } - else if (!VD->isStaticLocal()) - Out << '3'; - else - Out << '4'; - // Now mangle the type. - // <variable-type> ::= <type> <cvr-qualifiers> - // ::= <type> A # pointers, references, arrays - // Pointers and references are odd. The type of 'int * const foo;' gets - // mangled as 'QAHA' instead of 'PAHB', for example. - QualType Ty = VD->getType(); - if (Ty->isPointerType() || Ty->isReferenceType()) { - mangleType(Ty); - Out << 'A'; - } else if (Ty->isArrayType()) { - // Global arrays are funny, too. - mangleType(static_cast<ArrayType *>(Ty.getTypePtr()), true); - Out << 'A'; - } else { - mangleType(Ty.getLocalUnqualifiedType()); - mangleQualifiers(Ty.getLocalQualifiers(), false); - } -} - -void MicrosoftCXXNameMangler::mangleName(const NamedDecl *ND) { - // <name> ::= <unscoped-name> {[<named-scope>]+ | [<nested-name>]}? @ - const DeclContext *DC = ND->getDeclContext(); - - // Always start with the unqualified name. - mangleUnqualifiedName(ND); - - // If this is an extern variable declared locally, the relevant DeclContext - // is that of the containing namespace, or the translation unit. - if (isa<FunctionDecl>(DC) && ND->hasLinkage()) - while (!DC->isNamespace() && !DC->isTranslationUnit()) - DC = DC->getParent(); - - manglePostfix(DC); - - // Terminate the whole name with an '@'. - Out << '@'; -} - -void MicrosoftCXXNameMangler::mangleNumber(int64_t Number) { - // <number> ::= [?] <decimal digit> # <= 9 - // ::= [?] <hex digit>+ @ # > 9; A = 0, B = 1, etc... - if (Number < 0) { - Out << '?'; - Number = -Number; - } - if (Number >= 1 && Number <= 10) { - Out << Number-1; - } else { - // We have to build up the encoding in reverse order, so it will come - // out right when we write it out. - char Encoding[16]; - char *EndPtr = Encoding+sizeof(Encoding); - char *CurPtr = EndPtr; - while (Number) { - *--CurPtr = 'A' + (Number % 16); - Number /= 16; - } - Out.write(CurPtr, EndPtr-CurPtr); - Out << '@'; - } -} - -void -MicrosoftCXXNameMangler::mangleUnqualifiedName(const NamedDecl *ND, - DeclarationName Name) { - // <unqualified-name> ::= <operator-name> - // ::= <ctor-dtor-name> - // ::= <source-name> - switch (Name.getNameKind()) { - case DeclarationName::Identifier: { - if (const IdentifierInfo *II = Name.getAsIdentifierInfo()) { - mangleSourceName(II); - break; - } - - // Otherwise, an anonymous entity. We must have a declaration. - assert(ND && "mangling empty name without declaration"); - - if (const NamespaceDecl *NS = dyn_cast<NamespaceDecl>(ND)) { - if (NS->isAnonymousNamespace()) { - Out << "?A"; - break; - } - } - - // We must have an anonymous struct. - const TagDecl *TD = cast<TagDecl>(ND); - if (const TypedefDecl *D = TD->getTypedefForAnonDecl()) { - assert(TD->getDeclContext() == D->getDeclContext() && - "Typedef should not be in another decl context!"); - assert(D->getDeclName().getAsIdentifierInfo() && - "Typedef was not named!"); - mangleSourceName(D->getDeclName().getAsIdentifierInfo()); - break; - } - - // When VC encounters an anonymous type with no tag and no typedef, - // it literally emits '<unnamed-tag>'. - Out << "<unnamed-tag>"; - break; - } - - case DeclarationName::ObjCZeroArgSelector: - case DeclarationName::ObjCOneArgSelector: - case DeclarationName::ObjCMultiArgSelector: - assert(false && "Can't mangle Objective-C selector names here!"); - break; - - case DeclarationName::CXXConstructorName: - assert(false && "Can't mangle constructors yet!"); - break; - - case DeclarationName::CXXDestructorName: - assert(false && "Can't mangle destructors yet!"); - break; - - case DeclarationName::CXXConversionFunctionName: - // <operator-name> ::= ?B # (cast) - // The target type is encoded as the return type. - Out << "?B"; - break; - - case DeclarationName::CXXOperatorName: - mangleOperatorName(Name.getCXXOverloadedOperator()); - break; - - case DeclarationName::CXXLiteralOperatorName: - // FIXME: Was this added in VS2010? Does MS even know how to mangle this? - assert(false && "Don't know how to mangle literal operators yet!"); - break; - - case DeclarationName::CXXUsingDirective: - assert(false && "Can't mangle a using directive name!"); - break; - } -} - -void MicrosoftCXXNameMangler::manglePostfix(const DeclContext *DC, - bool NoFunction) { - // <postfix> ::= <unqualified-name> [<postfix>] - // ::= <template-postfix> <template-args> [<postfix>] - // ::= <template-param> - // ::= <substitution> [<postfix>] - - if (!DC) return; - - while (isa<LinkageSpecDecl>(DC)) - DC = DC->getParent(); - - if (DC->isTranslationUnit()) - return; - - if (const BlockDecl *BD = dyn_cast<BlockDecl>(DC)) { - llvm::SmallString<64> Name; - Context.mangleBlock(GlobalDecl(), BD, Name); - Out << Name << '@'; - return manglePostfix(DC->getParent(), NoFunction); - } - - if (NoFunction && (isa<FunctionDecl>(DC) || isa<ObjCMethodDecl>(DC))) - return; - else if (const ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(DC)) - mangleObjCMethodName(Method); - else { - mangleUnqualifiedName(cast<NamedDecl>(DC)); - manglePostfix(DC->getParent(), NoFunction); - } -} - -void MicrosoftCXXNameMangler::mangleOperatorName(OverloadedOperatorKind OO) { - switch (OO) { - // ?0 # constructor - // ?1 # destructor - // <operator-name> ::= ?2 # new - case OO_New: Out << "?2"; break; - // <operator-name> ::= ?3 # delete - case OO_Delete: Out << "?3"; break; - // <operator-name> ::= ?4 # = - case OO_Equal: Out << "?4"; break; - // <operator-name> ::= ?5 # >> - case OO_GreaterGreater: Out << "?5"; break; - // <operator-name> ::= ?6 # << - case OO_LessLess: Out << "?6"; break; - // <operator-name> ::= ?7 # ! - case OO_Exclaim: Out << "?7"; break; - // <operator-name> ::= ?8 # == - case OO_EqualEqual: Out << "?8"; break; - // <operator-name> ::= ?9 # != - case OO_ExclaimEqual: Out << "?9"; break; - // <operator-name> ::= ?A # [] - case OO_Subscript: Out << "?A"; break; - // ?B # conversion - // <operator-name> ::= ?C # -> - case OO_Arrow: Out << "?C"; break; - // <operator-name> ::= ?D # * - case OO_Star: Out << "?D"; break; - // <operator-name> ::= ?E # ++ - case OO_PlusPlus: Out << "?E"; break; - // <operator-name> ::= ?F # -- - case OO_MinusMinus: Out << "?F"; break; - // <operator-name> ::= ?G # - - case OO_Minus: Out << "?G"; break; - // <operator-name> ::= ?H # + - case OO_Plus: Out << "?H"; break; - // <operator-name> ::= ?I # & - case OO_Amp: Out << "?I"; break; - // <operator-name> ::= ?J # ->* - case OO_ArrowStar: Out << "?J"; break; - // <operator-name> ::= ?K # / - case OO_Slash: Out << "?K"; break; - // <operator-name> ::= ?L # % - case OO_Percent: Out << "?L"; break; - // <operator-name> ::= ?M # < - case OO_Less: Out << "?M"; break; - // <operator-name> ::= ?N # <= - case OO_LessEqual: Out << "?N"; break; - // <operator-name> ::= ?O # > - case OO_Greater: Out << "?O"; break; - // <operator-name> ::= ?P # >= - case OO_GreaterEqual: Out << "?P"; break; - // <operator-name> ::= ?Q # , - case OO_Comma: Out << "?Q"; break; - // <operator-name> ::= ?R # () - case OO_Call: Out << "?R"; break; - // <operator-name> ::= ?S # ~ - case OO_Tilde: Out << "?S"; break; - // <operator-name> ::= ?T # ^ - case OO_Caret: Out << "?T"; break; - // <operator-name> ::= ?U # | - case OO_Pipe: Out << "?U"; break; - // <operator-name> ::= ?V # && - case OO_AmpAmp: Out << "?V"; break; - // <operator-name> ::= ?W # || - case OO_PipePipe: Out << "?W"; break; - // <operator-name> ::= ?X # *= - case OO_StarEqual: Out << "?X"; break; - // <operator-name> ::= ?Y # += - case OO_PlusEqual: Out << "?Y"; break; - // <operator-name> ::= ?Z # -= - case OO_MinusEqual: Out << "?Z"; break; - // <operator-name> ::= ?_0 # /= - case OO_SlashEqual: Out << "?_0"; break; - // <operator-name> ::= ?_1 # %= - case OO_PercentEqual: Out << "?_1"; break; - // <operator-name> ::= ?_2 # >>= - case OO_GreaterGreaterEqual: Out << "?_2"; break; - // <operator-name> ::= ?_3 # <<= - case OO_LessLessEqual: Out << "?_3"; break; - // <operator-name> ::= ?_4 # &= - case OO_AmpEqual: Out << "?_4"; break; - // <operator-name> ::= ?_5 # |= - case OO_PipeEqual: Out << "?_5"; break; - // <operator-name> ::= ?_6 # ^= - case OO_CaretEqual: Out << "?_6"; break; - // ?_7 # vftable - // ?_8 # vbtable - // ?_9 # vcall - // ?_A # typeof - // ?_B # local static guard - // ?_C # string - // ?_D # vbase destructor - // ?_E # vector deleting destructor - // ?_F # default constructor closure - // ?_G # scalar deleting destructor - // ?_H # vector constructor iterator - // ?_I # vector destructor iterator - // ?_J # vector vbase constructor iterator - // ?_K # virtual displacement map - // ?_L # eh vector constructor iterator - // ?_M # eh vector destructor iterator - // ?_N # eh vector vbase constructor iterator - // ?_O # copy constructor closure - // ?_P<name> # udt returning <name> - // ?_Q # <unknown> - // ?_R0 # RTTI Type Descriptor - // ?_R1 # RTTI Base Class Descriptor at (a,b,c,d) - // ?_R2 # RTTI Base Class Array - // ?_R3 # RTTI Class Hierarchy Descriptor - // ?_R4 # RTTI Complete Object Locator - // ?_S # local vftable - // ?_T # local vftable constructor closure - // <operator-name> ::= ?_U # new[] - case OO_Array_New: Out << "?_U"; break; - // <operator-name> ::= ?_V # delete[] - case OO_Array_Delete: Out << "?_V"; break; - - case OO_Conditional: - assert(false && "Don't know how to mangle ?:"); - break; - - case OO_None: - case NUM_OVERLOADED_OPERATORS: - assert(false && "Not an overloaded operator"); - break; - } -} - -void MicrosoftCXXNameMangler::mangleSourceName(const IdentifierInfo *II) { - // <source name> ::= <identifier> @ - Out << II->getName() << '@'; -} - -void MicrosoftCXXNameMangler::mangleObjCMethodName(const ObjCMethodDecl *MD) { - llvm::SmallString<64> Buffer; - MiscNameMangler(Context, Buffer).mangleObjCMethodName(MD); - Out << Buffer; -} - -void MicrosoftCXXNameMangler::mangleQualifiers(Qualifiers Quals, - bool IsMember) { - // <cvr-qualifiers> ::= [E] [F] [I] <base-cvr-qualifiers> - // 'E' means __ptr64 (32-bit only); 'F' means __unaligned (32/64-bit only); - // 'I' means __restrict (32/64-bit). - // Note that the MSVC __restrict keyword isn't the same as the C99 restrict - // keyword! - // <base-cvr-qualifiers> ::= A # near - // ::= B # near const - // ::= C # near volatile - // ::= D # near const volatile - // ::= E # far (16-bit) - // ::= F # far const (16-bit) - // ::= G # far volatile (16-bit) - // ::= H # far const volatile (16-bit) - // ::= I # huge (16-bit) - // ::= J # huge const (16-bit) - // ::= K # huge volatile (16-bit) - // ::= L # huge const volatile (16-bit) - // ::= M <basis> # based - // ::= N <basis> # based const - // ::= O <basis> # based volatile - // ::= P <basis> # based const volatile - // ::= Q # near member - // ::= R # near const member - // ::= S # near volatile member - // ::= T # near const volatile member - // ::= U # far member (16-bit) - // ::= V # far const member (16-bit) - // ::= W # far volatile member (16-bit) - // ::= X # far const volatile member (16-bit) - // ::= Y # huge member (16-bit) - // ::= Z # huge const member (16-bit) - // ::= 0 # huge volatile member (16-bit) - // ::= 1 # huge const volatile member (16-bit) - // ::= 2 <basis> # based member - // ::= 3 <basis> # based const member - // ::= 4 <basis> # based volatile member - // ::= 5 <basis> # based const volatile member - // ::= 6 # near function (pointers only) - // ::= 7 # far function (pointers only) - // ::= 8 # near method (pointers only) - // ::= 9 # far method (pointers only) - // ::= _A <basis> # based function (pointers only) - // ::= _B <basis> # based function (far?) (pointers only) - // ::= _C <basis> # based method (pointers only) - // ::= _D <basis> # based method (far?) (pointers only) - // ::= _E # block (Clang) - // <basis> ::= 0 # __based(void) - // ::= 1 # __based(segment)? - // ::= 2 <name> # __based(name) - // ::= 3 # ? - // ::= 4 # ? - // ::= 5 # not really based - if (!IsMember) { - if (!Quals.hasVolatile()) { - if (!Quals.hasConst()) - Out << 'A'; - else - Out << 'B'; - } else { - if (!Quals.hasConst()) - Out << 'C'; - else - Out << 'D'; - } - } else { - if (!Quals.hasVolatile()) { - if (!Quals.hasConst()) - Out << 'Q'; - else - Out << 'R'; - } else { - if (!Quals.hasConst()) - Out << 'S'; - else - Out << 'T'; - } - } - - // FIXME: For now, just drop all extension qualifiers on the floor. -} - -void MicrosoftCXXNameMangler::mangleType(QualType T) { - // Only operate on the canonical type! - T = getASTContext().getCanonicalType(T); - - Qualifiers Quals = T.getLocalQualifiers(); - if (Quals) { - // We have to mangle these now, while we still have enough information. - // <pointer-cvr-qualifiers> ::= P # pointer - // ::= Q # const pointer - // ::= R # volatile pointer - // ::= S # const volatile pointer - if (T->isAnyPointerType() || T->isMemberPointerType() || - T->isBlockPointerType()) { - if (!Quals.hasVolatile()) - Out << 'Q'; - else { - if (!Quals.hasConst()) - Out << 'R'; - else - Out << 'S'; - } - } else - // Just emit qualifiers like normal. - // NB: When we mangle a pointer/reference type, and the pointee - // type has no qualifiers, the lack of qualifier gets mangled - // in there. - mangleQualifiers(Quals, false); - } else if (T->isAnyPointerType() || T->isMemberPointerType() || - T->isBlockPointerType()) { - Out << 'P'; - } - switch (T->getTypeClass()) { -#define ABSTRACT_TYPE(CLASS, PARENT) -#define NON_CANONICAL_TYPE(CLASS, PARENT) \ -case Type::CLASS: \ -llvm_unreachable("can't mangle non-canonical type " #CLASS "Type"); \ -return; -#define TYPE(CLASS, PARENT) \ -case Type::CLASS: \ -mangleType(static_cast<const CLASS##Type*>(T.getTypePtr())); \ -break; -#include "clang/AST/TypeNodes.def" - } -} - -void MicrosoftCXXNameMangler::mangleType(const BuiltinType *T) { - // <type> ::= <builtin-type> - // <builtin-type> ::= X # void - // ::= C # signed char - // ::= D # char - // ::= E # unsigned char - // ::= F # short - // ::= G # unsigned short (or wchar_t if it's not a builtin) - // ::= H # int - // ::= I # unsigned int - // ::= J # long - // ::= K # unsigned long - // L # <none> - // ::= M # float - // ::= N # double - // ::= O # long double (__float80 is mangled differently) - // ::= _D # __int8 (yup, it's a distinct type in MSVC) - // ::= _E # unsigned __int8 - // ::= _F # __int16 - // ::= _G # unsigned __int16 - // ::= _H # __int32 - // ::= _I # unsigned __int32 - // ::= _J # long long, __int64 - // ::= _K # unsigned long long, __int64 - // ::= _L # __int128 - // ::= _M # unsigned __int128 - // ::= _N # bool - // _O # <array in parameter> - // ::= _T # __float80 (Intel) - // ::= _W # wchar_t - // ::= _Z # __float80 (Digital Mars) - switch (T->getKind()) { - case BuiltinType::Void: Out << 'X'; break; - case BuiltinType::SChar: Out << 'C'; break; - case BuiltinType::Char_U: case BuiltinType::Char_S: Out << 'D'; break; - case BuiltinType::UChar: Out << 'E'; break; - case BuiltinType::Short: Out << 'F'; break; - case BuiltinType::UShort: Out << 'G'; break; - case BuiltinType::Int: Out << 'H'; break; - case BuiltinType::UInt: Out << 'I'; break; - case BuiltinType::Long: Out << 'J'; break; - case BuiltinType::ULong: Out << 'K'; break; - case BuiltinType::Float: Out << 'M'; break; - case BuiltinType::Double: Out << 'N'; break; - // TODO: Determine size and mangle accordingly - case BuiltinType::LongDouble: Out << 'O'; break; - // TODO: __int8 and friends - case BuiltinType::LongLong: Out << "_J"; break; - case BuiltinType::ULongLong: Out << "_K"; break; - case BuiltinType::Int128: Out << "_L"; break; - case BuiltinType::UInt128: Out << "_M"; break; - case BuiltinType::Bool: Out << "_N"; break; - case BuiltinType::WChar_S: - case BuiltinType::WChar_U: Out << "_W"; break; - - case BuiltinType::Overload: - case BuiltinType::Dependent: - assert(false && - "Overloaded and dependent types shouldn't get to name mangling"); - break; - case BuiltinType::UndeducedAuto: - assert(0 && "Should not see undeduced auto here"); - break; - case BuiltinType::ObjCId: Out << "PAUobjc_object@@"; break; - case BuiltinType::ObjCClass: Out << "PAUobjc_class@@"; break; - case BuiltinType::ObjCSel: Out << "PAUobjc_selector@@"; break; - - case BuiltinType::Char16: - case BuiltinType::Char32: - case BuiltinType::NullPtr: - assert(false && "Don't know how to mangle this type"); - break; - } -} - -// <type> ::= <function-type> -void MicrosoftCXXNameMangler::mangleType(const FunctionProtoType *T) { - // Structors only appear in decls, so at this point we know it's not a - // structor type. - // I'll probably have mangleType(MemberPointerType) call the mangleType() - // method directly. - mangleType(T, NULL, false, false); -} -void MicrosoftCXXNameMangler::mangleType(const FunctionNoProtoType *T) { - llvm_unreachable("Can't mangle K&R function prototypes"); -} - -void MicrosoftCXXNameMangler::mangleType(const FunctionType *T, - const FunctionDecl *D, - bool IsStructor, - bool IsInstMethod) { - // <function-type> ::= <this-cvr-qualifiers> <calling-convention> - // <return-type> <argument-list> <throw-spec> - const FunctionProtoType *Proto = cast<FunctionProtoType>(T); - - // If this is a C++ instance method, mangle the CVR qualifiers for the - // this pointer. - if (IsInstMethod) - mangleQualifiers(Qualifiers::fromCVRMask(Proto->getTypeQuals()), false); - - mangleCallingConvention(T, IsInstMethod); - - // <return-type> ::= <type> - // ::= @ # structors (they have no declared return type) - if (IsStructor) - Out << '@'; - else - mangleType(Proto->getResultType()); - - // <argument-list> ::= X # void - // ::= <type>+ @ - // ::= <type>* Z # varargs - if (Proto->getNumArgs() == 0 && !Proto->isVariadic()) { - Out << 'X'; - } else { - if (D) { - // If we got a decl, use the "types-as-written" to make sure arrays - // get mangled right. - for (FunctionDecl::param_const_iterator Parm = D->param_begin(), - ParmEnd = D->param_end(); - Parm != ParmEnd; ++Parm) - mangleType((*Parm)->getTypeSourceInfo()->getType()); - } else { - for (FunctionProtoType::arg_type_iterator Arg = Proto->arg_type_begin(), - ArgEnd = Proto->arg_type_end(); - Arg != ArgEnd; ++Arg) - mangleType(*Arg); - } - // <builtin-type> ::= Z # ellipsis - if (Proto->isVariadic()) - Out << 'Z'; - else - Out << '@'; - } - - mangleThrowSpecification(Proto); -} - -void MicrosoftCXXNameMangler::mangleFunctionClass(const FunctionDecl *FD) { - // <function-class> ::= A # private: near - // ::= B # private: far - // ::= C # private: static near - // ::= D # private: static far - // ::= E # private: virtual near - // ::= F # private: virtual far - // ::= G # private: thunk near - // ::= H # private: thunk far - // ::= I # protected: near - // ::= J # protected: far - // ::= K # protected: static near - // ::= L # protected: static far - // ::= M # protected: virtual near - // ::= N # protected: virtual far - // ::= O # protected: thunk near - // ::= P # protected: thunk far - // ::= Q # public: near - // ::= R # public: far - // ::= S # public: static near - // ::= T # public: static far - // ::= U # public: virtual near - // ::= V # public: virtual far - // ::= W # public: thunk near - // ::= X # public: thunk far - // ::= Y # global near - // ::= Z # global far - if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD)) { - switch (MD->getAccess()) { - default: - case AS_private: - if (MD->isStatic()) - Out << 'C'; - else if (MD->isVirtual()) - Out << 'E'; - else - Out << 'A'; - break; - case AS_protected: - if (MD->isStatic()) - Out << 'K'; - else if (MD->isVirtual()) - Out << 'M'; - else - Out << 'I'; - break; - case AS_public: - if (MD->isStatic()) - Out << 'S'; - else if (MD->isVirtual()) - Out << 'U'; - else - Out << 'Q'; - } - } else - Out << 'Y'; -} -void MicrosoftCXXNameMangler::mangleCallingConvention(const FunctionType *T, - bool IsInstMethod) { - // <calling-convention> ::= A # __cdecl - // ::= B # __export __cdecl - // ::= C # __pascal - // ::= D # __export __pascal - // ::= E # __thiscall - // ::= F # __export __thiscall - // ::= G # __stdcall - // ::= H # __export __stdcall - // ::= I # __fastcall - // ::= J # __export __fastcall - // The 'export' calling conventions are from a bygone era - // (*cough*Win16*cough*) when functions were declared for export with - // that keyword. (It didn't actually export them, it just made them so - // that they could be in a DLL and somebody from another module could call - // them.) - CallingConv CC = T->getCallConv(); - if (CC == CC_Default) - CC = IsInstMethod ? getASTContext().getDefaultMethodCallConv() : CC_C; - switch (CC) { - case CC_Default: - case CC_C: Out << 'A'; break; - case CC_X86Pascal: Out << 'C'; break; - case CC_X86ThisCall: Out << 'E'; break; - case CC_X86StdCall: Out << 'G'; break; - case CC_X86FastCall: Out << 'I'; break; - } -} -void MicrosoftCXXNameMangler::mangleThrowSpecification( - const FunctionProtoType *FT) { - // <throw-spec> ::= Z # throw(...) (default) - // ::= @ # throw() or __declspec/__attribute__((nothrow)) - // ::= <type>+ - // NOTE: Since the Microsoft compiler ignores throw specifications, they are - // all actually mangled as 'Z'. (They're ignored because their associated - // functionality isn't implemented, and probably never will be.) - Out << 'Z'; -} - -void MicrosoftCXXNameMangler::mangleType(const UnresolvedUsingType *T) { - assert(false && "Don't know how to mangle UnresolvedUsingTypes yet!"); -} - -// <type> ::= <union-type> | <struct-type> | <class-type> | <enum-type> -// <union-type> ::= T <name> -// <struct-type> ::= U <name> -// <class-type> ::= V <name> -// <enum-type> ::= W <size> <name> -void MicrosoftCXXNameMangler::mangleType(const EnumType *T) { - mangleType(static_cast<const TagType*>(T)); -} -void MicrosoftCXXNameMangler::mangleType(const RecordType *T) { - mangleType(static_cast<const TagType*>(T)); -} -void MicrosoftCXXNameMangler::mangleType(const TagType *T) { - switch (T->getDecl()->getTagKind()) { - case TTK_Union: - Out << 'T'; - break; - case TTK_Struct: - Out << 'U'; - break; - case TTK_Class: - Out << 'V'; - break; - case TTK_Enum: - Out << 'W'; - Out << getASTContext().getTypeSizeInChars( - cast<EnumDecl>(T->getDecl())->getIntegerType()).getQuantity(); - break; - } - mangleName(T->getDecl()); -} - -// <type> ::= <array-type> -// <array-type> ::= P <cvr-qualifiers> [Y <dimension-count> <dimension>+] -// <element-type> # as global -// ::= Q <cvr-qualifiers> [Y <dimension-count> <dimension>+] -// <element-type> # as param -// It's supposed to be the other way around, but for some strange reason, it -// isn't. Today this behavior is retained for the sole purpose of backwards -// compatibility. -void MicrosoftCXXNameMangler::mangleType(const ArrayType *T, bool IsGlobal) { - // This isn't a recursive mangling, so now we have to do it all in this - // one call. - if (IsGlobal) - Out << 'P'; - else - Out << 'Q'; - mangleExtraDimensions(T->getElementType()); -} -void MicrosoftCXXNameMangler::mangleType(const ConstantArrayType *T) { - mangleType(static_cast<const ArrayType *>(T), false); -} -void MicrosoftCXXNameMangler::mangleType(const VariableArrayType *T) { - mangleType(static_cast<const ArrayType *>(T), false); -} -void MicrosoftCXXNameMangler::mangleType(const DependentSizedArrayType *T) { - mangleType(static_cast<const ArrayType *>(T), false); -} -void MicrosoftCXXNameMangler::mangleType(const IncompleteArrayType *T) { - mangleType(static_cast<const ArrayType *>(T), false); -} -void MicrosoftCXXNameMangler::mangleExtraDimensions(QualType ElementTy) { - llvm::SmallVector<llvm::APInt, 3> Dimensions; - for (;;) { - if (ElementTy->isConstantArrayType()) { - const ConstantArrayType *CAT = - static_cast<const ConstantArrayType *>(ElementTy.getTypePtr()); - Dimensions.push_back(CAT->getSize()); - ElementTy = CAT->getElementType(); - } else if (ElementTy->isVariableArrayType()) { - assert(false && "Don't know how to mangle VLAs!"); - } else if (ElementTy->isDependentSizedArrayType()) { - // The dependent expression has to be folded into a constant (TODO). - assert(false && "Don't know how to mangle dependent-sized arrays!"); - } else if (ElementTy->isIncompleteArrayType()) continue; - else break; - } - mangleQualifiers(ElementTy.getQualifiers(), false); - // If there are any additional dimensions, mangle them now. - if (Dimensions.size() > 0) { - Out << 'Y'; - // <dimension-count> ::= <number> # number of extra dimensions - mangleNumber(Dimensions.size()); - for (unsigned Dim = 0; Dim < Dimensions.size(); ++Dim) { - mangleNumber(Dimensions[Dim].getLimitedValue()); - } - } - mangleType(ElementTy.getLocalUnqualifiedType()); -} - -// <type> ::= <pointer-to-member-type> -// <pointer-to-member-type> ::= <pointer-cvr-qualifiers> <cvr-qualifiers> -// <class name> <type> -void MicrosoftCXXNameMangler::mangleType(const MemberPointerType *T) { - QualType PointeeType = T->getPointeeType(); - if (const FunctionProtoType *FPT = dyn_cast<FunctionProtoType>(PointeeType)) { - Out << '8'; - mangleName(cast<RecordType>(T->getClass())->getDecl()); - mangleType(FPT, NULL, false, true); - } else { - mangleQualifiers(PointeeType.getQualifiers(), true); - mangleName(cast<RecordType>(T->getClass())->getDecl()); - mangleType(PointeeType.getLocalUnqualifiedType()); - } -} - -void MicrosoftCXXNameMangler::mangleType(const TemplateTypeParmType *T) { - assert(false && "Don't know how to mangle TemplateTypeParmTypes yet!"); -} - -// <type> ::= <pointer-type> -// <pointer-type> ::= <pointer-cvr-qualifiers> <cvr-qualifiers> <type> -void MicrosoftCXXNameMangler::mangleType(const PointerType *T) { - QualType PointeeTy = T->getPointeeType(); - if (PointeeTy->isArrayType()) { - // Pointers to arrays are mangled like arrays. - mangleExtraDimensions(T->getPointeeType()); - } else if (PointeeTy->isFunctionType()) { - // Function pointers are special. - Out << '6'; - mangleType(static_cast<const FunctionType *>(PointeeTy.getTypePtr()), - NULL, false, false); - } else { - if (!PointeeTy.hasQualifiers()) - // Lack of qualifiers is mangled as 'A'. - Out << 'A'; - mangleType(PointeeTy); - } -} -void MicrosoftCXXNameMangler::mangleType(const ObjCObjectPointerType *T) { - // Object pointers never have qualifiers. - Out << 'A'; - mangleType(T->getPointeeType()); -} - -// <type> ::= <reference-type> -// <reference-type> ::= A <cvr-qualifiers> <type> -void MicrosoftCXXNameMangler::mangleType(const LValueReferenceType *T) { - Out << 'A'; - QualType PointeeTy = T->getPointeeType(); - if (!PointeeTy.hasQualifiers()) - // Lack of qualifiers is mangled as 'A'. - Out << 'A'; - mangleType(PointeeTy); -} - -void MicrosoftCXXNameMangler::mangleType(const RValueReferenceType *T) { - assert(false && "Don't know how to mangle RValueReferenceTypes yet!"); -} - -void MicrosoftCXXNameMangler::mangleType(const ComplexType *T) { - assert(false && "Don't know how to mangle ComplexTypes yet!"); -} - -void MicrosoftCXXNameMangler::mangleType(const VectorType *T) { - assert(false && "Don't know how to mangle VectorTypes yet!"); -} -void MicrosoftCXXNameMangler::mangleType(const ExtVectorType *T) { - assert(false && "Don't know how to mangle ExtVectorTypes yet!"); -} -void MicrosoftCXXNameMangler::mangleType(const DependentSizedExtVectorType *T) { - assert(false && "Don't know how to mangle DependentSizedExtVectorTypes yet!"); -} - -void MicrosoftCXXNameMangler::mangleType(const ObjCInterfaceType *T) { - // ObjC interfaces have structs underlying them. - Out << 'U'; - mangleName(T->getDecl()); -} - -void MicrosoftCXXNameMangler::mangleType(const ObjCObjectType *T) { - // We don't allow overloading by different protocol qualification, - // so mangling them isn't necessary. - mangleType(T->getBaseType()); -} - -void MicrosoftCXXNameMangler::mangleType(const BlockPointerType *T) { - Out << "_E"; - mangleType(T->getPointeeType()); -} - -void MicrosoftCXXNameMangler::mangleType(const InjectedClassNameType *T) { - assert(false && "Don't know how to mangle InjectedClassNameTypes yet!"); -} - -void MicrosoftCXXNameMangler::mangleType(const TemplateSpecializationType *T) { - assert(false && "Don't know how to mangle TemplateSpecializationTypes yet!"); -} - -void MicrosoftCXXNameMangler::mangleType(const DependentNameType *T) { - assert(false && "Don't know how to mangle DependentNameTypes yet!"); -} - -void MicrosoftCXXNameMangler::mangleType( - const DependentTemplateSpecializationType *T) { - assert(false && - "Don't know how to mangle DependentTemplateSpecializationTypes yet!"); -} - -void MicrosoftCXXNameMangler::mangleType(const PackExpansionType *T) { - assert(false && "Don't know how to mangle PackExpansionTypes yet!"); -} - -void MicrosoftCXXNameMangler::mangleType(const TypeOfType *T) { - assert(false && "Don't know how to mangle TypeOfTypes yet!"); -} - -void MicrosoftCXXNameMangler::mangleType(const TypeOfExprType *T) { - assert(false && "Don't know how to mangle TypeOfExprTypes yet!"); -} - -void MicrosoftCXXNameMangler::mangleType(const DecltypeType *T) { - assert(false && "Don't know how to mangle DecltypeTypes yet!"); -} - -void MicrosoftMangleContext::mangleName(const NamedDecl *D, - llvm::SmallVectorImpl<char> &Name) { - assert((isa<FunctionDecl>(D) || isa<VarDecl>(D)) && - "Invalid mangleName() call, argument is not a variable or function!"); - assert(!isa<CXXConstructorDecl>(D) && !isa<CXXDestructorDecl>(D) && - "Invalid mangleName() call on 'structor decl!"); - - PrettyStackTraceDecl CrashInfo(D, SourceLocation(), - getASTContext().getSourceManager(), - "Mangling declaration"); - - MicrosoftCXXNameMangler Mangler(*this, Name); - return Mangler.mangle(D); -} -void MicrosoftMangleContext::mangleThunk(const CXXMethodDecl *MD, - const ThunkInfo &Thunk, - llvm::SmallVectorImpl<char> &) { - assert(false && "Can't yet mangle thunks!"); -} -void MicrosoftMangleContext::mangleCXXDtorThunk(const CXXDestructorDecl *DD, - CXXDtorType Type, - const ThisAdjustment &, - llvm::SmallVectorImpl<char> &) { - assert(false && "Can't yet mangle destructor thunks!"); -} -void MicrosoftMangleContext::mangleCXXVTable(const CXXRecordDecl *RD, - llvm::SmallVectorImpl<char> &) { - assert(false && "Can't yet mangle virtual tables!"); -} -void MicrosoftMangleContext::mangleCXXVTT(const CXXRecordDecl *RD, - llvm::SmallVectorImpl<char> &) { - llvm_unreachable("The MS C++ ABI does not have virtual table tables!"); -} -void MicrosoftMangleContext::mangleCXXCtorVTable(const CXXRecordDecl *RD, - int64_t Offset, - const CXXRecordDecl *Type, - llvm::SmallVectorImpl<char> &) { - llvm_unreachable("The MS C++ ABI does not have constructor vtables!"); -} -void MicrosoftMangleContext::mangleCXXRTTI(QualType T, - llvm::SmallVectorImpl<char> &) { - assert(false && "Can't yet mangle RTTI!"); -} -void MicrosoftMangleContext::mangleCXXRTTIName(QualType T, - llvm::SmallVectorImpl<char> &) { - assert(false && "Can't yet mangle RTTI names!"); -} -void MicrosoftMangleContext::mangleCXXCtor(const CXXConstructorDecl *D, - CXXCtorType Type, - llvm::SmallVectorImpl<char> &) { - assert(false && "Can't yet mangle constructors!"); -} -void MicrosoftMangleContext::mangleCXXDtor(const CXXDestructorDecl *D, - CXXDtorType Type, - llvm::SmallVectorImpl<char> &) { - assert(false && "Can't yet mangle destructors!"); -} - CGCXXABI *clang::CodeGen::CreateMicrosoftCXXABI(CodeGenModule &CGM) { return new MicrosoftCXXABI(CGM); } |