//===--- 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 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 // //===----------------------------------------------------------------------===// #include "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/SourceManager.h" #include "llvm/ADT/StringExtras.h" #include "llvm/Support/raw_ostream.h" #include "llvm/Support/ErrorHandling.h" #include "CGVtable.h" #define MANGLE_CHECKER 0 #if MANGLE_CHECKER #include #endif using namespace clang; using namespace CodeGen; namespace { static const DeclContext *GetLocalClassFunctionDeclContext( const DeclContext *DC) { if (isa(DC)) { while (!DC->isNamespace() && !DC->isTranslationUnit() && !isa(DC)) DC = DC->getParent(); if (isa(DC)) return DC; } return 0; } static const CXXMethodDecl *getStructor(const CXXMethodDecl *MD) { assert((isa(MD) || isa(MD)) && "Passed in decl is not a ctor or dtor!"); if (const TemplateDecl *TD = MD->getPrimaryTemplate()) { MD = cast(TD->getTemplatedDecl()); assert((isa(MD) || isa(MD)) && "Templated decl is not a ctor or dtor!"); } return MD; } static const unsigned UnknownArity = ~0U; /// CXXNameMangler - Manage the mangling of a single name. class CXXNameMangler { MangleContext &Context; llvm::raw_svector_ostream Out; const CXXMethodDecl *Structor; unsigned StructorType; llvm::DenseMap Substitutions; ASTContext &getASTContext() const { return Context.getASTContext(); } public: CXXNameMangler(MangleContext &C, llvm::SmallVectorImpl &Res) : Context(C), Out(Res), Structor(0), StructorType(0) { } CXXNameMangler(MangleContext &C, llvm::SmallVectorImpl &Res, const CXXConstructorDecl *D, CXXCtorType Type) : Context(C), Out(Res), Structor(getStructor(D)), StructorType(Type) { } CXXNameMangler(MangleContext &C, llvm::SmallVectorImpl &Res, const CXXDestructorDecl *D, CXXDtorType Type) : Context(C), Out(Res), Structor(getStructor(D)), StructorType(Type) { } #if MANGLE_CHECKER ~CXXNameMangler() { if (Out.str()[0] == '\01') return; int status = 0; char *result = abi::__cxa_demangle(Out.str().str().c_str(), 0, 0, &status); assert(status == 0 && "Could not demangle mangled name!"); free(result); } #endif llvm::raw_svector_ostream &getStream() { return Out; } void mangle(const NamedDecl *D, llvm::StringRef Prefix = "_Z"); void mangleCallOffset(const ThunkAdjustment &Adjustment); void mangleNumber(int64_t Number); void mangleFunctionEncoding(const FunctionDecl *FD); void mangleName(const NamedDecl *ND); void mangleType(QualType T); private: bool mangleSubstitution(const NamedDecl *ND); bool mangleSubstitution(QualType T); bool mangleSubstitution(uintptr_t Ptr); bool mangleStandardSubstitution(const NamedDecl *ND); void addSubstitution(const NamedDecl *ND) { ND = cast(ND->getCanonicalDecl()); addSubstitution(reinterpret_cast(ND)); } void addSubstitution(QualType T); void addSubstitution(uintptr_t Ptr); void mangleUnresolvedScope(NestedNameSpecifier *Qualifier); void mangleUnresolvedName(NestedNameSpecifier *Qualifier, DeclarationName Name, unsigned KnownArity = UnknownArity); void mangleName(const TemplateDecl *TD, const TemplateArgument *TemplateArgs, unsigned NumTemplateArgs); void mangleUnqualifiedName(const NamedDecl *ND) { mangleUnqualifiedName(ND, ND->getDeclName(), UnknownArity); } void mangleUnqualifiedName(const NamedDecl *ND, DeclarationName Name, unsigned KnownArity); void mangleUnscopedName(const NamedDecl *ND); void mangleUnscopedTemplateName(const TemplateDecl *ND); void mangleSourceName(const IdentifierInfo *II); void mangleLocalName(const NamedDecl *ND); void mangleNestedName(const NamedDecl *ND, const DeclContext *DC, bool NoFunction=false); void mangleNestedName(const TemplateDecl *TD, const TemplateArgument *TemplateArgs, unsigned NumTemplateArgs); void manglePrefix(const DeclContext *DC, bool NoFunction=false); void mangleTemplatePrefix(const TemplateDecl *ND); void mangleOperatorName(OverloadedOperatorKind OO, unsigned Arity); void mangleQualifiers(Qualifiers Quals); 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 mangleBareFunctionType(const FunctionType *T, bool MangleReturnType); void mangleIntegerLiteral(QualType T, const llvm::APSInt &Value); void mangleMemberExpr(const Expr *Base, bool IsArrow, NestedNameSpecifier *Qualifier, DeclarationName Name, unsigned KnownArity); void mangleCalledExpression(const Expr *E, unsigned KnownArity); void mangleExpression(const Expr *E); void mangleCXXCtorType(CXXCtorType T); void mangleCXXDtorType(CXXDtorType T); void mangleTemplateArgs(const TemplateArgument *TemplateArgs, unsigned NumTemplateArgs); void mangleTemplateArgs(const TemplateArgumentList &L); void mangleTemplateArg(const TemplateArgument &A); void mangleTemplateParameter(unsigned Index); }; } 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(DC)) return Linkage->getLanguage() == LinkageSpecDecl::lang_c; } return false; } bool MangleContext::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()) 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(D); if (FD && (FD->hasAttr() || isa(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 non-internal linkage are not mangled if (!FD) { const DeclContext *DC = D->getDeclContext(); // Check for extern variable declared locally. if (isa(DC) && D->hasLinkage()) while (!DC->isNamespace() && !DC->isTranslationUnit()) DC = DC->getParent(); 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 CXXNameMangler::mangle(const NamedDecl *D, llvm::StringRef Prefix) { // 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()) { // If we have an asm name, then we use it as the mangling. Out << '\01'; // LLVM IR Marker for __asm("foo") Out << ALA->getLabel(); return; } // ::= _Z // ::= // ::= Out << Prefix; if (const FunctionDecl *FD = dyn_cast(D)) mangleFunctionEncoding(FD); else mangleName(cast(D)); } void CXXNameMangler::mangleFunctionEncoding(const FunctionDecl *FD) { // ::= mangleName(FD); // Don't mangle in the type if this isn't a decl we should typically mangle. if (!Context.shouldMangleDeclName(FD)) return; // Whether the mangling of a function type includes the return type depends on // the context and the nature of the function. The rules for deciding whether // the return type is included are: // // 1. Template functions (names or types) have return types encoded, with // the exceptions listed below. // 2. Function types not appearing as part of a function name mangling, // e.g. parameters, pointer types, etc., have return type encoded, with the // exceptions listed below. // 3. Non-template function names do not have return types encoded. // // The exceptions mentioned in (1) and (2) above, for which the return type is // never included, are // 1. Constructors. // 2. Destructors. // 3. Conversion operator functions, e.g. operator int. bool MangleReturnType = false; if (FunctionTemplateDecl *PrimaryTemplate = FD->getPrimaryTemplate()) { if (!(isa(FD) || isa(FD) || isa(FD))) MangleReturnType = true; // Mangle the type of the primary template. FD = PrimaryTemplate->getTemplatedDecl(); } // Do the canonicalization out here because parameter types can // undergo additional canonicalization (e.g. array decay). FunctionType *FT = cast(Context.getASTContext() .getCanonicalType(FD->getType())); mangleBareFunctionType(FT, MangleReturnType); } /// isStd - Return whether a given namespace is the 'std' namespace. static bool isStd(const NamespaceDecl *NS) { const IdentifierInfo *II = NS->getOriginalNamespace()->getIdentifier(); return II && II->isStr("std"); } static const DeclContext *IgnoreLinkageSpecDecls(const DeclContext *DC) { while (isa(DC)) { assert(cast(DC)->getLanguage() == LinkageSpecDecl::lang_cxx && "Unexpected linkage decl!"); DC = DC->getParent(); } return DC; } // isStdNamespace - Return whether a given decl context is a toplevel 'std' // namespace. static bool isStdNamespace(const DeclContext *DC) { if (!DC->isNamespace()) return false; if (!IgnoreLinkageSpecDecls(DC->getParent())->isTranslationUnit()) return false; return isStd(cast(DC)); } static const TemplateDecl * isTemplate(const NamedDecl *ND, const TemplateArgumentList *&TemplateArgs) { // Check if we have a function template. if (const FunctionDecl *FD = dyn_cast(ND)){ if (const TemplateDecl *TD = FD->getPrimaryTemplate()) { TemplateArgs = FD->getTemplateSpecializationArgs(); return TD; } } // Check if we have a class template. if (const ClassTemplateSpecializationDecl *Spec = dyn_cast(ND)) { TemplateArgs = &Spec->getTemplateArgs(); return Spec->getSpecializedTemplate(); } return 0; } void CXXNameMangler::mangleName(const NamedDecl *ND) { // ::= // ::= // ::= // ::= // const DeclContext *DC = ND->getDeclContext(); if (GetLocalClassFunctionDeclContext(DC)) { mangleLocalName(ND); return; } // If this is an extern variable declared locally, the relevant DeclContext // is that of the containing namespace, or the translation unit. if (isa(DC) && ND->hasLinkage()) while (!DC->isNamespace() && !DC->isTranslationUnit()) DC = DC->getParent(); while (isa(DC)) DC = DC->getParent(); if (DC->isTranslationUnit() || isStdNamespace(DC)) { // Check if we have a template. const TemplateArgumentList *TemplateArgs = 0; if (const TemplateDecl *TD = isTemplate(ND, TemplateArgs)) { mangleUnscopedTemplateName(TD); mangleTemplateArgs(*TemplateArgs); return; } mangleUnscopedName(ND); return; } if (isa(DC) || isa(DC)) { mangleLocalName(ND); return; } mangleNestedName(ND, DC); } void CXXNameMangler::mangleName(const TemplateDecl *TD, const TemplateArgument *TemplateArgs, unsigned NumTemplateArgs) { const DeclContext *DC = IgnoreLinkageSpecDecls(TD->getDeclContext()); if (DC->isTranslationUnit() || isStdNamespace(DC)) { mangleUnscopedTemplateName(TD); mangleTemplateArgs(TemplateArgs, NumTemplateArgs); } else { mangleNestedName(TD, TemplateArgs, NumTemplateArgs); } } void CXXNameMangler::mangleUnscopedName(const NamedDecl *ND) { // ::= // ::= St # ::std:: if (isStdNamespace(ND->getDeclContext())) Out << "St"; mangleUnqualifiedName(ND); } void CXXNameMangler::mangleUnscopedTemplateName(const TemplateDecl *ND) { // ::= // ::= if (mangleSubstitution(ND)) return; // ::= if (const TemplateTemplateParmDecl *TTP = dyn_cast(ND)) { mangleTemplateParameter(TTP->getIndex()); return; } mangleUnscopedName(ND->getTemplatedDecl()); addSubstitution(ND); } void CXXNameMangler::mangleNumber(int64_t Number) { // ::= [n] if (Number < 0) { Out << 'n'; Number = -Number; } Out << Number; } void CXXNameMangler::mangleCallOffset(const ThunkAdjustment &Adjustment) { // ::= h _ // ::= v _ // ::= # non-virtual base override // ::= _ // # virtual base override, with vcall offset if (!Adjustment.Virtual) { Out << 'h'; mangleNumber(Adjustment.NonVirtual); Out << '_'; return; } Out << 'v'; mangleNumber(Adjustment.NonVirtual); Out << '_'; mangleNumber(Adjustment.Virtual); Out << '_'; } void CXXNameMangler::mangleUnresolvedScope(NestedNameSpecifier *Qualifier) { Qualifier = getASTContext().getCanonicalNestedNameSpecifier(Qualifier); switch (Qualifier->getKind()) { case NestedNameSpecifier::Global: // nothing break; case NestedNameSpecifier::Namespace: mangleName(Qualifier->getAsNamespace()); break; case NestedNameSpecifier::TypeSpec: case NestedNameSpecifier::TypeSpecWithTemplate: mangleType(QualType(Qualifier->getAsType(), 0)); break; case NestedNameSpecifier::Identifier: // Member expressions can have these without prefixes. if (Qualifier->getPrefix()) mangleUnresolvedScope(Qualifier->getPrefix()); mangleSourceName(Qualifier->getAsIdentifier()); break; } } /// Mangles a name which was not resolved to a specific entity. void CXXNameMangler::mangleUnresolvedName(NestedNameSpecifier *Qualifier, DeclarationName Name, unsigned KnownArity) { if (Qualifier) mangleUnresolvedScope(Qualifier); // FIXME: ambiguity of unqualified lookup with :: mangleUnqualifiedName(0, Name, KnownArity); } void CXXNameMangler::mangleUnqualifiedName(const NamedDecl *ND, DeclarationName Name, unsigned KnownArity) { // ::= // ::= // ::= switch (Name.getNameKind()) { case DeclarationName::Identifier: { if (const IdentifierInfo *II = Name.getAsIdentifierInfo()) { // We must avoid conflicts between internally- and externally- // linked variable declaration names in the same TU. // This naming convention is the same as that followed by GCC, though it // shouldn't actually matter. if (ND && isa(ND) && ND->getLinkage() == InternalLinkage && ND->getDeclContext()->isFileContext()) Out << 'L'; 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(ND)) { if (NS->isAnonymousNamespace()) { // This is how gcc mangles these names. Out << "12_GLOBAL__N_1"; break; } } // We must have an anonymous struct. const TagDecl *TD = cast(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; } // Get a unique id for the anonymous struct. uint64_t AnonStructId = Context.getAnonymousStructId(TD); // Mangle it as a source name in the form // [n] $_ // where n is the length of the string. llvm::SmallString<8> Str; Str += "$_"; Str += llvm::utostr(AnonStructId); Out << Str.size(); Out << Str.str(); break; } case DeclarationName::ObjCZeroArgSelector: case DeclarationName::ObjCOneArgSelector: case DeclarationName::ObjCMultiArgSelector: assert(false && "Can't mangle Objective-C selector names here!"); break; case DeclarationName::CXXConstructorName: if (ND == Structor) // If the named decl is the C++ constructor we're mangling, use the type // we were given. mangleCXXCtorType(static_cast(StructorType)); else // Otherwise, use the complete constructor name. This is relevant if a // class with a constructor is declared within a constructor. mangleCXXCtorType(Ctor_Complete); break; case DeclarationName::CXXDestructorName: if (ND == Structor) // If the named decl is the C++ destructor we're mangling, use the type we // were given. mangleCXXDtorType(static_cast(StructorType)); else // Otherwise, use the complete destructor name. This is relevant if a // class with a destructor is declared within a destructor. mangleCXXDtorType(Dtor_Complete); break; case DeclarationName::CXXConversionFunctionName: // ::= cv # (cast) Out << "cv"; mangleType(Context.getASTContext().getCanonicalType(Name.getCXXNameType())); break; case DeclarationName::CXXOperatorName: { unsigned Arity; if (ND) { Arity = cast(ND)->getNumParams(); // If we have a C++ member function, we need to include the 'this' pointer. // FIXME: This does not make sense for operators that are static, but their // names stay the same regardless of the arity (operator new for instance). if (isa(ND)) Arity++; } else Arity = KnownArity; mangleOperatorName(Name.getCXXOverloadedOperator(), Arity); break; } case DeclarationName::CXXLiteralOperatorName: // FIXME: This mangling is not yet official. Out << "li"; mangleSourceName(Name.getCXXLiteralIdentifier()); break; case DeclarationName::CXXUsingDirective: assert(false && "Can't mangle a using directive name!"); break; } } void CXXNameMangler::mangleSourceName(const IdentifierInfo *II) { // ::= // ::= [n] // ::= Out << II->getLength() << II->getName(); } void CXXNameMangler::mangleNestedName(const NamedDecl *ND, const DeclContext *DC, bool NoFunction) { // ::= N [] E // ::= N [] E Out << 'N'; if (const CXXMethodDecl *Method = dyn_cast(ND)) mangleQualifiers(Qualifiers::fromCVRMask(Method->getTypeQualifiers())); // Check if we have a template. const TemplateArgumentList *TemplateArgs = 0; if (const TemplateDecl *TD = isTemplate(ND, TemplateArgs)) { mangleTemplatePrefix(TD); mangleTemplateArgs(*TemplateArgs); } else { manglePrefix(DC, NoFunction); mangleUnqualifiedName(ND); } Out << 'E'; } void CXXNameMangler::mangleNestedName(const TemplateDecl *TD, const TemplateArgument *TemplateArgs, unsigned NumTemplateArgs) { // ::= N [] E Out << 'N'; mangleTemplatePrefix(TD); mangleTemplateArgs(TemplateArgs, NumTemplateArgs); Out << 'E'; } void CXXNameMangler::mangleLocalName(const NamedDecl *ND) { // := Z E [] // := Z E s [] // := _ const DeclContext *DC = ND->getDeclContext(); Out << 'Z'; if (const ObjCMethodDecl *MD = dyn_cast(DC)) mangleObjCMethodName(MD); else if (const DeclContext *CDC = GetLocalClassFunctionDeclContext(DC)) { mangleFunctionEncoding(cast(CDC)); Out << 'E'; mangleNestedName(ND, DC, true /*NoFunction*/); // FIXME. This still does not cover all cases. unsigned disc; if (Context.getNextDiscriminator(ND, disc)) { if (disc < 10) Out << '_' << disc; else Out << "__" << disc << '_'; } return; } else mangleFunctionEncoding(cast(DC)); Out << 'E'; mangleUnqualifiedName(ND); } void CXXNameMangler::manglePrefix(const DeclContext *DC, bool NoFunction) { // ::= // ::= // ::= // ::= # empty // ::= while (isa(DC)) DC = DC->getParent(); if (DC->isTranslationUnit()) return; if (mangleSubstitution(cast(DC))) return; // Check if we have a template. const TemplateArgumentList *TemplateArgs = 0; if (const TemplateDecl *TD = isTemplate(cast(DC), TemplateArgs)) { mangleTemplatePrefix(TD); mangleTemplateArgs(*TemplateArgs); } else if(NoFunction && isa(DC)) return; else { manglePrefix(DC->getParent(), NoFunction); mangleUnqualifiedName(cast(DC)); } addSubstitution(cast(DC)); } void CXXNameMangler::mangleTemplatePrefix(const TemplateDecl *ND) { // ::=