Douglas Gregor | 6ec3668 | 2009-02-18 23:53:56 +0000 | [diff] [blame] | 1 | //===--- Mangle.cpp - Mangle C++ Names --------------------------*- C++ -*-===// |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // Implements C++ name mangling according to the Itanium C++ ABI, |
| 11 | // which is used in GCC 3.2 and newer (and many compilers that are |
| 12 | // ABI-compatible with GCC): |
| 13 | // |
| 14 | // http://www.codesourcery.com/public/cxx-abi/abi.html |
| 15 | // |
| 16 | //===----------------------------------------------------------------------===// |
| 17 | #include "Mangle.h" |
| 18 | #include "clang/AST/ASTContext.h" |
| 19 | #include "clang/AST/Decl.h" |
| 20 | #include "clang/AST/DeclCXX.h" |
Anders Carlsson | a40c5e4 | 2009-03-07 22:03:21 +0000 | [diff] [blame] | 21 | #include "clang/AST/DeclObjC.h" |
Anders Carlsson | 7a0ba87 | 2009-05-15 16:09:15 +0000 | [diff] [blame] | 22 | #include "clang/AST/DeclTemplate.h" |
Anders Carlsson | 50755b0 | 2009-09-27 20:11:34 +0000 | [diff] [blame] | 23 | #include "clang/AST/ExprCXX.h" |
Douglas Gregor | 6ec3668 | 2009-02-18 23:53:56 +0000 | [diff] [blame] | 24 | #include "clang/Basic/SourceManager.h" |
Anders Carlsson | c4355b6 | 2009-10-07 01:45:02 +0000 | [diff] [blame] | 25 | #include "llvm/ADT/StringExtras.h" |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 26 | #include "llvm/Support/raw_ostream.h" |
John McCall | efe6aee | 2009-09-05 07:56:18 +0000 | [diff] [blame] | 27 | #include "llvm/Support/ErrorHandling.h" |
Anders Carlsson | b73a5be | 2009-11-26 02:49:32 +0000 | [diff] [blame] | 28 | #include "CGVtable.h" |
Anders Carlsson | f98574b | 2010-02-05 07:31:37 +0000 | [diff] [blame] | 29 | |
| 30 | #define MANGLE_CHECKER 0 |
| 31 | |
| 32 | #if MANGLE_CHECKER |
| 33 | #include <cxxabi.h> |
| 34 | #endif |
| 35 | |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 36 | using namespace clang; |
Anders Carlsson | b73a5be | 2009-11-26 02:49:32 +0000 | [diff] [blame] | 37 | using namespace CodeGen; |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 38 | |
| 39 | namespace { |
Fariborz Jahanian | 5705853 | 2010-03-03 19:41:08 +0000 | [diff] [blame] | 40 | |
| 41 | static const DeclContext *GetLocalClassFunctionDeclContext( |
| 42 | const DeclContext *DC) { |
| 43 | if (isa<CXXRecordDecl>(DC)) { |
| 44 | while (!DC->isNamespace() && !DC->isTranslationUnit() && |
| 45 | !isa<FunctionDecl>(DC)) |
| 46 | DC = DC->getParent(); |
| 47 | if (isa<FunctionDecl>(DC)) |
| 48 | return DC; |
| 49 | } |
| 50 | return 0; |
| 51 | } |
| 52 | |
Anders Carlsson | 7e12003 | 2009-11-24 05:36:32 +0000 | [diff] [blame] | 53 | static const CXXMethodDecl *getStructor(const CXXMethodDecl *MD) { |
| 54 | assert((isa<CXXConstructorDecl>(MD) || isa<CXXDestructorDecl>(MD)) && |
| 55 | "Passed in decl is not a ctor or dtor!"); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 56 | |
Anders Carlsson | 7e12003 | 2009-11-24 05:36:32 +0000 | [diff] [blame] | 57 | if (const TemplateDecl *TD = MD->getPrimaryTemplate()) { |
| 58 | MD = cast<CXXMethodDecl>(TD->getTemplatedDecl()); |
| 59 | |
| 60 | assert((isa<CXXConstructorDecl>(MD) || isa<CXXDestructorDecl>(MD)) && |
| 61 | "Templated decl is not a ctor or dtor!"); |
| 62 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 63 | |
Anders Carlsson | 7e12003 | 2009-11-24 05:36:32 +0000 | [diff] [blame] | 64 | return MD; |
| 65 | } |
John McCall | 1dd7383 | 2010-02-04 01:42:13 +0000 | [diff] [blame] | 66 | |
| 67 | static const unsigned UnknownArity = ~0U; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 68 | |
Daniel Dunbar | 1b07711 | 2009-11-21 09:06:10 +0000 | [diff] [blame] | 69 | /// CXXNameMangler - Manage the mangling of a single name. |
Daniel Dunbar | c074771 | 2009-11-21 09:12:13 +0000 | [diff] [blame] | 70 | class CXXNameMangler { |
Daniel Dunbar | 1b07711 | 2009-11-21 09:06:10 +0000 | [diff] [blame] | 71 | MangleContext &Context; |
Daniel Dunbar | 94fd26d | 2009-11-21 09:06:22 +0000 | [diff] [blame] | 72 | llvm::raw_svector_ostream Out; |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 73 | |
Daniel Dunbar | 1b07711 | 2009-11-21 09:06:10 +0000 | [diff] [blame] | 74 | const CXXMethodDecl *Structor; |
| 75 | unsigned StructorType; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 76 | |
Daniel Dunbar | 1b07711 | 2009-11-21 09:06:10 +0000 | [diff] [blame] | 77 | llvm::DenseMap<uintptr_t, unsigned> Substitutions; |
Daniel Dunbar | 3c9e463 | 2009-11-21 09:05:47 +0000 | [diff] [blame] | 78 | |
John McCall | 1dd7383 | 2010-02-04 01:42:13 +0000 | [diff] [blame] | 79 | ASTContext &getASTContext() const { return Context.getASTContext(); } |
| 80 | |
Daniel Dunbar | c074771 | 2009-11-21 09:12:13 +0000 | [diff] [blame] | 81 | public: |
Daniel Dunbar | 94fd26d | 2009-11-21 09:06:22 +0000 | [diff] [blame] | 82 | CXXNameMangler(MangleContext &C, llvm::SmallVectorImpl<char> &Res) |
| 83 | : Context(C), Out(Res), Structor(0), StructorType(0) { } |
Daniel Dunbar | 77939c9 | 2009-11-21 09:06:31 +0000 | [diff] [blame] | 84 | CXXNameMangler(MangleContext &C, llvm::SmallVectorImpl<char> &Res, |
| 85 | const CXXConstructorDecl *D, CXXCtorType Type) |
Anders Carlsson | 7e12003 | 2009-11-24 05:36:32 +0000 | [diff] [blame] | 86 | : Context(C), Out(Res), Structor(getStructor(D)), StructorType(Type) { } |
Daniel Dunbar | 77939c9 | 2009-11-21 09:06:31 +0000 | [diff] [blame] | 87 | CXXNameMangler(MangleContext &C, llvm::SmallVectorImpl<char> &Res, |
| 88 | const CXXDestructorDecl *D, CXXDtorType Type) |
Anders Carlsson | 7e12003 | 2009-11-24 05:36:32 +0000 | [diff] [blame] | 89 | : Context(C), Out(Res), Structor(getStructor(D)), StructorType(Type) { } |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 90 | |
Anders Carlsson | f98574b | 2010-02-05 07:31:37 +0000 | [diff] [blame] | 91 | #if MANGLE_CHECKER |
| 92 | ~CXXNameMangler() { |
| 93 | if (Out.str()[0] == '\01') |
| 94 | return; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 95 | |
Anders Carlsson | f98574b | 2010-02-05 07:31:37 +0000 | [diff] [blame] | 96 | int status = 0; |
| 97 | char *result = abi::__cxa_demangle(Out.str().str().c_str(), 0, 0, &status); |
| 98 | assert(status == 0 && "Could not demangle mangled name!"); |
| 99 | free(result); |
| 100 | } |
| 101 | #endif |
Daniel Dunbar | c074771 | 2009-11-21 09:12:13 +0000 | [diff] [blame] | 102 | llvm::raw_svector_ostream &getStream() { return Out; } |
| 103 | |
Daniel Dunbar | 7e0c195 | 2009-11-21 09:17:15 +0000 | [diff] [blame] | 104 | void mangle(const NamedDecl *D, llvm::StringRef Prefix = "_Z"); |
Anders Carlsson | 19879c9 | 2010-03-23 17:17:29 +0000 | [diff] [blame] | 105 | void mangleCallOffset(int64_t NonVirtual, int64_t Virtual); |
Anders Carlsson | a94822e | 2009-11-26 02:32:05 +0000 | [diff] [blame] | 106 | void mangleNumber(int64_t Number); |
Daniel Dunbar | c074771 | 2009-11-21 09:12:13 +0000 | [diff] [blame] | 107 | void mangleFunctionEncoding(const FunctionDecl *FD); |
| 108 | void mangleName(const NamedDecl *ND); |
| 109 | void mangleType(QualType T); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 110 | |
Daniel Dunbar | c074771 | 2009-11-21 09:12:13 +0000 | [diff] [blame] | 111 | private: |
Daniel Dunbar | 1b07711 | 2009-11-21 09:06:10 +0000 | [diff] [blame] | 112 | bool mangleSubstitution(const NamedDecl *ND); |
| 113 | bool mangleSubstitution(QualType T); |
| 114 | bool mangleSubstitution(uintptr_t Ptr); |
Daniel Dunbar | 3c9e463 | 2009-11-21 09:05:47 +0000 | [diff] [blame] | 115 | |
Daniel Dunbar | 1b07711 | 2009-11-21 09:06:10 +0000 | [diff] [blame] | 116 | bool mangleStandardSubstitution(const NamedDecl *ND); |
Daniel Dunbar | 3c9e463 | 2009-11-21 09:05:47 +0000 | [diff] [blame] | 117 | |
Daniel Dunbar | 1b07711 | 2009-11-21 09:06:10 +0000 | [diff] [blame] | 118 | void addSubstitution(const NamedDecl *ND) { |
| 119 | ND = cast<NamedDecl>(ND->getCanonicalDecl()); |
Anders Carlsson | 433d137 | 2009-11-07 04:26:04 +0000 | [diff] [blame] | 120 | |
Daniel Dunbar | 1b07711 | 2009-11-21 09:06:10 +0000 | [diff] [blame] | 121 | addSubstitution(reinterpret_cast<uintptr_t>(ND)); |
| 122 | } |
| 123 | void addSubstitution(QualType T); |
| 124 | void addSubstitution(uintptr_t Ptr); |
Daniel Dunbar | 3c9e463 | 2009-11-21 09:05:47 +0000 | [diff] [blame] | 125 | |
John McCall | 1dd7383 | 2010-02-04 01:42:13 +0000 | [diff] [blame] | 126 | void mangleUnresolvedScope(NestedNameSpecifier *Qualifier); |
| 127 | void mangleUnresolvedName(NestedNameSpecifier *Qualifier, |
| 128 | DeclarationName Name, |
| 129 | unsigned KnownArity = UnknownArity); |
| 130 | |
Daniel Dunbar | 1b07711 | 2009-11-21 09:06:10 +0000 | [diff] [blame] | 131 | void mangleName(const TemplateDecl *TD, |
| 132 | const TemplateArgument *TemplateArgs, |
| 133 | unsigned NumTemplateArgs); |
John McCall | 1dd7383 | 2010-02-04 01:42:13 +0000 | [diff] [blame] | 134 | void mangleUnqualifiedName(const NamedDecl *ND) { |
| 135 | mangleUnqualifiedName(ND, ND->getDeclName(), UnknownArity); |
| 136 | } |
| 137 | void mangleUnqualifiedName(const NamedDecl *ND, DeclarationName Name, |
| 138 | unsigned KnownArity); |
Daniel Dunbar | 1b07711 | 2009-11-21 09:06:10 +0000 | [diff] [blame] | 139 | void mangleUnscopedName(const NamedDecl *ND); |
| 140 | void mangleUnscopedTemplateName(const TemplateDecl *ND); |
| 141 | void mangleSourceName(const IdentifierInfo *II); |
| 142 | void mangleLocalName(const NamedDecl *ND); |
Fariborz Jahanian | 5705853 | 2010-03-03 19:41:08 +0000 | [diff] [blame] | 143 | void mangleNestedName(const NamedDecl *ND, const DeclContext *DC, |
| 144 | bool NoFunction=false); |
Daniel Dunbar | 1b07711 | 2009-11-21 09:06:10 +0000 | [diff] [blame] | 145 | void mangleNestedName(const TemplateDecl *TD, |
| 146 | const TemplateArgument *TemplateArgs, |
| 147 | unsigned NumTemplateArgs); |
Fariborz Jahanian | 5705853 | 2010-03-03 19:41:08 +0000 | [diff] [blame] | 148 | void manglePrefix(const DeclContext *DC, bool NoFunction=false); |
Daniel Dunbar | 1b07711 | 2009-11-21 09:06:10 +0000 | [diff] [blame] | 149 | void mangleTemplatePrefix(const TemplateDecl *ND); |
| 150 | void mangleOperatorName(OverloadedOperatorKind OO, unsigned Arity); |
| 151 | void mangleQualifiers(Qualifiers Quals); |
John McCall | efe6aee | 2009-09-05 07:56:18 +0000 | [diff] [blame] | 152 | |
Anders Carlsson | 7b06f6c | 2009-12-10 03:14:39 +0000 | [diff] [blame] | 153 | void mangleObjCMethodName(const ObjCMethodDecl *MD); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 154 | |
Daniel Dunbar | 1b07711 | 2009-11-21 09:06:10 +0000 | [diff] [blame] | 155 | // Declare manglers for every type class. |
John McCall | efe6aee | 2009-09-05 07:56:18 +0000 | [diff] [blame] | 156 | #define ABSTRACT_TYPE(CLASS, PARENT) |
| 157 | #define NON_CANONICAL_TYPE(CLASS, PARENT) |
| 158 | #define TYPE(CLASS, PARENT) void mangleType(const CLASS##Type *T); |
| 159 | #include "clang/AST/TypeNodes.def" |
| 160 | |
Daniel Dunbar | 1b07711 | 2009-11-21 09:06:10 +0000 | [diff] [blame] | 161 | void mangleType(const TagType*); |
| 162 | void mangleBareFunctionType(const FunctionType *T, |
| 163 | bool MangleReturnType); |
Anders Carlsson | e170ba7 | 2009-12-14 01:45:37 +0000 | [diff] [blame] | 164 | |
| 165 | void mangleIntegerLiteral(QualType T, const llvm::APSInt &Value); |
John McCall | 2f27bf8 | 2010-02-04 02:56:29 +0000 | [diff] [blame] | 166 | void mangleMemberExpr(const Expr *Base, bool IsArrow, |
| 167 | NestedNameSpecifier *Qualifier, |
| 168 | DeclarationName Name, |
| 169 | unsigned KnownArity); |
John McCall | 1dd7383 | 2010-02-04 01:42:13 +0000 | [diff] [blame] | 170 | void mangleCalledExpression(const Expr *E, unsigned KnownArity); |
Daniel Dunbar | 1b07711 | 2009-11-21 09:06:10 +0000 | [diff] [blame] | 171 | void mangleExpression(const Expr *E); |
| 172 | void mangleCXXCtorType(CXXCtorType T); |
| 173 | void mangleCXXDtorType(CXXDtorType T); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 174 | |
Rafael Espindola | d980072 | 2010-03-11 14:07:00 +0000 | [diff] [blame] | 175 | void mangleTemplateArgs(const TemplateParameterList &PL, |
| 176 | const TemplateArgument *TemplateArgs, |
Daniel Dunbar | 1b07711 | 2009-11-21 09:06:10 +0000 | [diff] [blame] | 177 | unsigned NumTemplateArgs); |
Rafael Espindola | d980072 | 2010-03-11 14:07:00 +0000 | [diff] [blame] | 178 | void mangleTemplateArgs(const TemplateParameterList &PL, |
| 179 | const TemplateArgumentList &AL); |
| 180 | void mangleTemplateArg(const NamedDecl *P, const TemplateArgument &A); |
Daniel Dunbar | 3c9e463 | 2009-11-21 09:05:47 +0000 | [diff] [blame] | 181 | |
Daniel Dunbar | 1b07711 | 2009-11-21 09:06:10 +0000 | [diff] [blame] | 182 | void mangleTemplateParameter(unsigned Index); |
| 183 | }; |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 184 | } |
| 185 | |
Anders Carlsson | 43f1740 | 2009-04-02 15:51:53 +0000 | [diff] [blame] | 186 | static bool isInCLinkageSpecification(const Decl *D) { |
Douglas Gregor | 457e281 | 2009-10-28 16:31:34 +0000 | [diff] [blame] | 187 | D = D->getCanonicalDecl(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 188 | for (const DeclContext *DC = D->getDeclContext(); |
Anders Carlsson | 43f1740 | 2009-04-02 15:51:53 +0000 | [diff] [blame] | 189 | !DC->isTranslationUnit(); DC = DC->getParent()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 190 | if (const LinkageSpecDecl *Linkage = dyn_cast<LinkageSpecDecl>(DC)) |
Anders Carlsson | 43f1740 | 2009-04-02 15:51:53 +0000 | [diff] [blame] | 191 | return Linkage->getLanguage() == LinkageSpecDecl::lang_c; |
| 192 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 193 | |
Anders Carlsson | 43f1740 | 2009-04-02 15:51:53 +0000 | [diff] [blame] | 194 | return false; |
| 195 | } |
| 196 | |
Daniel Dunbar | f981bf8 | 2009-11-21 09:14:52 +0000 | [diff] [blame] | 197 | bool MangleContext::shouldMangleDeclName(const NamedDecl *D) { |
| 198 | // In C, functions with no attributes never need to be mangled. Fastpath them. |
| 199 | if (!getASTContext().getLangOptions().CPlusPlus && !D->hasAttrs()) |
| 200 | return false; |
| 201 | |
| 202 | // Any decl can be declared with __asm("foo") on it, and this takes precedence |
| 203 | // over all other naming in the .o file. |
| 204 | if (D->hasAttr<AsmLabelAttr>()) |
| 205 | return true; |
| 206 | |
Mike Stump | 141c5af | 2009-09-02 00:25:38 +0000 | [diff] [blame] | 207 | // Clang's "overloadable" attribute extension to C/C++ implies name mangling |
Anders Carlsson | a1e1622 | 2009-11-07 07:15:03 +0000 | [diff] [blame] | 208 | // (always) as does passing a C++ member function and a function |
| 209 | // whose name is not a simple identifier. |
Daniel Dunbar | f981bf8 | 2009-11-21 09:14:52 +0000 | [diff] [blame] | 210 | const FunctionDecl *FD = dyn_cast<FunctionDecl>(D); |
| 211 | if (FD && (FD->hasAttr<OverloadableAttr>() || isa<CXXMethodDecl>(FD) || |
| 212 | !FD->getDeclName().isIdentifier())) |
| 213 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 214 | |
Daniel Dunbar | f981bf8 | 2009-11-21 09:14:52 +0000 | [diff] [blame] | 215 | // Otherwise, no mangling is done outside C++ mode. |
| 216 | if (!getASTContext().getLangOptions().CPlusPlus) |
| 217 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 218 | |
Sean Hunt | 3145525 | 2010-01-24 03:04:27 +0000 | [diff] [blame] | 219 | // Variables at global scope with non-internal linkage are not mangled |
Eli Friedman | 7facf84 | 2009-12-02 20:32:49 +0000 | [diff] [blame] | 220 | if (!FD) { |
| 221 | const DeclContext *DC = D->getDeclContext(); |
| 222 | // Check for extern variable declared locally. |
| 223 | if (isa<FunctionDecl>(DC) && D->hasLinkage()) |
| 224 | while (!DC->isNamespace() && !DC->isTranslationUnit()) |
| 225 | DC = DC->getParent(); |
Douglas Gregor | 0b6bc8b | 2010-02-03 09:33:45 +0000 | [diff] [blame] | 226 | if (DC->isTranslationUnit() && D->getLinkage() != InternalLinkage) |
Eli Friedman | 7facf84 | 2009-12-02 20:32:49 +0000 | [diff] [blame] | 227 | return false; |
| 228 | } |
| 229 | |
| 230 | // C functions and "main" are not mangled. |
| 231 | if ((FD && FD->isMain()) || isInCLinkageSpecification(D)) |
Daniel Dunbar | f981bf8 | 2009-11-21 09:14:52 +0000 | [diff] [blame] | 232 | return false; |
| 233 | |
Anders Carlsson | 43f1740 | 2009-04-02 15:51:53 +0000 | [diff] [blame] | 234 | return true; |
| 235 | } |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 236 | |
Daniel Dunbar | 7e0c195 | 2009-11-21 09:17:15 +0000 | [diff] [blame] | 237 | void CXXNameMangler::mangle(const NamedDecl *D, llvm::StringRef Prefix) { |
Mike Stump | 141c5af | 2009-09-02 00:25:38 +0000 | [diff] [blame] | 238 | // Any decl can be declared with __asm("foo") on it, and this takes precedence |
| 239 | // over all other naming in the .o file. |
Argyrios Kyrtzidis | 40b598e | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 240 | if (const AsmLabelAttr *ALA = D->getAttr<AsmLabelAttr>()) { |
Chris Lattner | ca3f25c | 2009-03-21 08:24:40 +0000 | [diff] [blame] | 241 | // If we have an asm name, then we use it as the mangling. |
| 242 | Out << '\01'; // LLVM IR Marker for __asm("foo") |
| 243 | Out << ALA->getLabel(); |
Daniel Dunbar | f981bf8 | 2009-11-21 09:14:52 +0000 | [diff] [blame] | 244 | return; |
Chris Lattner | ca3f25c | 2009-03-21 08:24:40 +0000 | [diff] [blame] | 245 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 246 | |
Sean Hunt | 3145525 | 2010-01-24 03:04:27 +0000 | [diff] [blame] | 247 | // <mangled-name> ::= _Z <encoding> |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 248 | // ::= <data name> |
| 249 | // ::= <special-name> |
Daniel Dunbar | 7e0c195 | 2009-11-21 09:17:15 +0000 | [diff] [blame] | 250 | Out << Prefix; |
| 251 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) |
Daniel Dunbar | f981bf8 | 2009-11-21 09:14:52 +0000 | [diff] [blame] | 252 | mangleFunctionEncoding(FD); |
Rafael Espindola | d980072 | 2010-03-11 14:07:00 +0000 | [diff] [blame] | 253 | else if (const VarDecl *VD = dyn_cast<VarDecl>(D)) |
| 254 | mangleName(VD); |
Daniel Dunbar | 7e0c195 | 2009-11-21 09:17:15 +0000 | [diff] [blame] | 255 | else |
Rafael Espindola | d980072 | 2010-03-11 14:07:00 +0000 | [diff] [blame] | 256 | mangleName(cast<FieldDecl>(D)); |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 257 | } |
| 258 | |
| 259 | void CXXNameMangler::mangleFunctionEncoding(const FunctionDecl *FD) { |
| 260 | // <encoding> ::= <function name> <bare-function-type> |
| 261 | mangleName(FD); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 262 | |
Daniel Dunbar | 7e0c195 | 2009-11-21 09:17:15 +0000 | [diff] [blame] | 263 | // Don't mangle in the type if this isn't a decl we should typically mangle. |
| 264 | if (!Context.shouldMangleDeclName(FD)) |
| 265 | return; |
| 266 | |
Mike Stump | 141c5af | 2009-09-02 00:25:38 +0000 | [diff] [blame] | 267 | // Whether the mangling of a function type includes the return type depends on |
| 268 | // the context and the nature of the function. The rules for deciding whether |
| 269 | // the return type is included are: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 270 | // |
Douglas Gregor | 1fd2dd1 | 2009-06-29 22:39:32 +0000 | [diff] [blame] | 271 | // 1. Template functions (names or types) have return types encoded, with |
| 272 | // the exceptions listed below. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 273 | // 2. Function types not appearing as part of a function name mangling, |
Douglas Gregor | 1fd2dd1 | 2009-06-29 22:39:32 +0000 | [diff] [blame] | 274 | // e.g. parameters, pointer types, etc., have return type encoded, with the |
| 275 | // exceptions listed below. |
| 276 | // 3. Non-template function names do not have return types encoded. |
| 277 | // |
Mike Stump | 141c5af | 2009-09-02 00:25:38 +0000 | [diff] [blame] | 278 | // The exceptions mentioned in (1) and (2) above, for which the return type is |
| 279 | // never included, are |
Douglas Gregor | 1fd2dd1 | 2009-06-29 22:39:32 +0000 | [diff] [blame] | 280 | // 1. Constructors. |
| 281 | // 2. Destructors. |
| 282 | // 3. Conversion operator functions, e.g. operator int. |
| 283 | bool MangleReturnType = false; |
Anders Carlsson | 9234b7f | 2009-09-17 03:46:43 +0000 | [diff] [blame] | 284 | if (FunctionTemplateDecl *PrimaryTemplate = FD->getPrimaryTemplate()) { |
| 285 | if (!(isa<CXXConstructorDecl>(FD) || isa<CXXDestructorDecl>(FD) || |
| 286 | isa<CXXConversionDecl>(FD))) |
| 287 | MangleReturnType = true; |
Daniel Dunbar | 3c9e463 | 2009-11-21 09:05:47 +0000 | [diff] [blame] | 288 | |
Anders Carlsson | 9234b7f | 2009-09-17 03:46:43 +0000 | [diff] [blame] | 289 | // Mangle the type of the primary template. |
| 290 | FD = PrimaryTemplate->getTemplatedDecl(); |
| 291 | } |
| 292 | |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 293 | // Do the canonicalization out here because parameter types can |
| 294 | // undergo additional canonicalization (e.g. array decay). |
| 295 | FunctionType *FT = cast<FunctionType>(Context.getASTContext() |
| 296 | .getCanonicalType(FD->getType())); |
| 297 | |
| 298 | mangleBareFunctionType(FT, MangleReturnType); |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 299 | } |
| 300 | |
Anders Carlsson | 47846d2 | 2009-12-04 06:23:23 +0000 | [diff] [blame] | 301 | /// isStd - Return whether a given namespace is the 'std' namespace. |
| 302 | static bool isStd(const NamespaceDecl *NS) { |
John McCall | 9aeed32 | 2009-10-01 00:25:31 +0000 | [diff] [blame] | 303 | const IdentifierInfo *II = NS->getOriginalNamespace()->getIdentifier(); |
| 304 | return II && II->isStr("std"); |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 305 | } |
| 306 | |
Anders Carlsson | 47846d2 | 2009-12-04 06:23:23 +0000 | [diff] [blame] | 307 | static const DeclContext *IgnoreLinkageSpecDecls(const DeclContext *DC) { |
| 308 | while (isa<LinkageSpecDecl>(DC)) { |
| 309 | assert(cast<LinkageSpecDecl>(DC)->getLanguage() == |
| 310 | LinkageSpecDecl::lang_cxx && "Unexpected linkage decl!"); |
| 311 | DC = DC->getParent(); |
| 312 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 313 | |
Anders Carlsson | 47846d2 | 2009-12-04 06:23:23 +0000 | [diff] [blame] | 314 | return DC; |
| 315 | } |
| 316 | |
| 317 | // isStdNamespace - Return whether a given decl context is a toplevel 'std' |
| 318 | // namespace. |
Daniel Dunbar | 1308af9 | 2009-11-21 09:11:45 +0000 | [diff] [blame] | 319 | static bool isStdNamespace(const DeclContext *DC) { |
Anders Carlsson | 47846d2 | 2009-12-04 06:23:23 +0000 | [diff] [blame] | 320 | if (!DC->isNamespace()) |
| 321 | return false; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 322 | |
Anders Carlsson | 47846d2 | 2009-12-04 06:23:23 +0000 | [diff] [blame] | 323 | if (!IgnoreLinkageSpecDecls(DC->getParent())->isTranslationUnit()) |
| 324 | return false; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 325 | |
Anders Carlsson | 47846d2 | 2009-12-04 06:23:23 +0000 | [diff] [blame] | 326 | return isStd(cast<NamespaceDecl>(DC)); |
Daniel Dunbar | 1308af9 | 2009-11-21 09:11:45 +0000 | [diff] [blame] | 327 | } |
| 328 | |
Anders Carlsson | bb36ba4 | 2009-09-26 03:24:57 +0000 | [diff] [blame] | 329 | static const TemplateDecl * |
| 330 | isTemplate(const NamedDecl *ND, const TemplateArgumentList *&TemplateArgs) { |
Anders Carlsson | 2744a06 | 2009-09-18 19:00:18 +0000 | [diff] [blame] | 331 | // Check if we have a function template. |
| 332 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)){ |
Anders Carlsson | bb36ba4 | 2009-09-26 03:24:57 +0000 | [diff] [blame] | 333 | if (const TemplateDecl *TD = FD->getPrimaryTemplate()) { |
Anders Carlsson | 2744a06 | 2009-09-18 19:00:18 +0000 | [diff] [blame] | 334 | TemplateArgs = FD->getTemplateSpecializationArgs(); |
Anders Carlsson | bb36ba4 | 2009-09-26 03:24:57 +0000 | [diff] [blame] | 335 | return TD; |
Anders Carlsson | 2744a06 | 2009-09-18 19:00:18 +0000 | [diff] [blame] | 336 | } |
| 337 | } |
| 338 | |
Anders Carlsson | eafc6dc | 2009-09-18 19:44:50 +0000 | [diff] [blame] | 339 | // Check if we have a class template. |
| 340 | if (const ClassTemplateSpecializationDecl *Spec = |
| 341 | dyn_cast<ClassTemplateSpecializationDecl>(ND)) { |
| 342 | TemplateArgs = &Spec->getTemplateArgs(); |
Anders Carlsson | bb36ba4 | 2009-09-26 03:24:57 +0000 | [diff] [blame] | 343 | return Spec->getSpecializedTemplate(); |
Anders Carlsson | eafc6dc | 2009-09-18 19:44:50 +0000 | [diff] [blame] | 344 | } |
Daniel Dunbar | 3c9e463 | 2009-11-21 09:05:47 +0000 | [diff] [blame] | 345 | |
Anders Carlsson | 2744a06 | 2009-09-18 19:00:18 +0000 | [diff] [blame] | 346 | return 0; |
| 347 | } |
| 348 | |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 349 | void CXXNameMangler::mangleName(const NamedDecl *ND) { |
| 350 | // <name> ::= <nested-name> |
| 351 | // ::= <unscoped-name> |
| 352 | // ::= <unscoped-template-name> <template-args> |
Anders Carlsson | 201ce74 | 2009-09-17 03:17:01 +0000 | [diff] [blame] | 353 | // ::= <local-name> |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 354 | // |
Anders Carlsson | d58d6f7 | 2009-09-17 16:12:20 +0000 | [diff] [blame] | 355 | const DeclContext *DC = ND->getDeclContext(); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 356 | |
Fariborz Jahanian | 5705853 | 2010-03-03 19:41:08 +0000 | [diff] [blame] | 357 | if (GetLocalClassFunctionDeclContext(DC)) { |
| 358 | mangleLocalName(ND); |
| 359 | return; |
| 360 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 361 | |
Eli Friedman | 7facf84 | 2009-12-02 20:32:49 +0000 | [diff] [blame] | 362 | // If this is an extern variable declared locally, the relevant DeclContext |
| 363 | // is that of the containing namespace, or the translation unit. |
| 364 | if (isa<FunctionDecl>(DC) && ND->hasLinkage()) |
| 365 | while (!DC->isNamespace() && !DC->isTranslationUnit()) |
| 366 | DC = DC->getParent(); |
| 367 | |
Anders Carlsson | 5cc58c6 | 2009-09-22 17:23:30 +0000 | [diff] [blame] | 368 | while (isa<LinkageSpecDecl>(DC)) |
Anders Carlsson | d58d6f7 | 2009-09-17 16:12:20 +0000 | [diff] [blame] | 369 | DC = DC->getParent(); |
Daniel Dunbar | 3c9e463 | 2009-11-21 09:05:47 +0000 | [diff] [blame] | 370 | |
Anders Carlsson | d58d6f7 | 2009-09-17 16:12:20 +0000 | [diff] [blame] | 371 | if (DC->isTranslationUnit() || isStdNamespace(DC)) { |
Anders Carlsson | 2744a06 | 2009-09-18 19:00:18 +0000 | [diff] [blame] | 372 | // Check if we have a template. |
| 373 | const TemplateArgumentList *TemplateArgs = 0; |
Anders Carlsson | 0fa6df4 | 2009-09-26 19:45:45 +0000 | [diff] [blame] | 374 | if (const TemplateDecl *TD = isTemplate(ND, TemplateArgs)) { |
Anders Carlsson | 2744a06 | 2009-09-18 19:00:18 +0000 | [diff] [blame] | 375 | mangleUnscopedTemplateName(TD); |
Rafael Espindola | d980072 | 2010-03-11 14:07:00 +0000 | [diff] [blame] | 376 | TemplateParameterList *TemplateParameters = TD->getTemplateParameters(); |
| 377 | mangleTemplateArgs(*TemplateParameters, *TemplateArgs); |
Anders Carlsson | 2744a06 | 2009-09-18 19:00:18 +0000 | [diff] [blame] | 378 | return; |
Anders Carlsson | 7482e24 | 2009-09-18 04:29:09 +0000 | [diff] [blame] | 379 | } |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 380 | |
Anders Carlsson | 7482e24 | 2009-09-18 04:29:09 +0000 | [diff] [blame] | 381 | mangleUnscopedName(ND); |
| 382 | return; |
| 383 | } |
Daniel Dunbar | 3c9e463 | 2009-11-21 09:05:47 +0000 | [diff] [blame] | 384 | |
Anders Carlsson | 7b06f6c | 2009-12-10 03:14:39 +0000 | [diff] [blame] | 385 | if (isa<FunctionDecl>(DC) || isa<ObjCMethodDecl>(DC)) { |
Anders Carlsson | 7482e24 | 2009-09-18 04:29:09 +0000 | [diff] [blame] | 386 | mangleLocalName(ND); |
| 387 | return; |
| 388 | } |
Daniel Dunbar | 3c9e463 | 2009-11-21 09:05:47 +0000 | [diff] [blame] | 389 | |
Eli Friedman | 7facf84 | 2009-12-02 20:32:49 +0000 | [diff] [blame] | 390 | mangleNestedName(ND, DC); |
Anders Carlsson | 7482e24 | 2009-09-18 04:29:09 +0000 | [diff] [blame] | 391 | } |
Daniel Dunbar | 3c9e463 | 2009-11-21 09:05:47 +0000 | [diff] [blame] | 392 | void CXXNameMangler::mangleName(const TemplateDecl *TD, |
Anders Carlsson | 7624f21 | 2009-09-18 02:42:01 +0000 | [diff] [blame] | 393 | const TemplateArgument *TemplateArgs, |
| 394 | unsigned NumTemplateArgs) { |
Anders Carlsson | 47846d2 | 2009-12-04 06:23:23 +0000 | [diff] [blame] | 395 | const DeclContext *DC = IgnoreLinkageSpecDecls(TD->getDeclContext()); |
Daniel Dunbar | 3c9e463 | 2009-11-21 09:05:47 +0000 | [diff] [blame] | 396 | |
Anders Carlsson | 7624f21 | 2009-09-18 02:42:01 +0000 | [diff] [blame] | 397 | if (DC->isTranslationUnit() || isStdNamespace(DC)) { |
Anders Carlsson | 0fa6df4 | 2009-09-26 19:45:45 +0000 | [diff] [blame] | 398 | mangleUnscopedTemplateName(TD); |
Rafael Espindola | d980072 | 2010-03-11 14:07:00 +0000 | [diff] [blame] | 399 | TemplateParameterList *TemplateParameters = TD->getTemplateParameters(); |
| 400 | mangleTemplateArgs(*TemplateParameters, TemplateArgs, NumTemplateArgs); |
Anders Carlsson | 7624f21 | 2009-09-18 02:42:01 +0000 | [diff] [blame] | 401 | } else { |
| 402 | mangleNestedName(TD, TemplateArgs, NumTemplateArgs); |
| 403 | } |
| 404 | } |
| 405 | |
Anders Carlsson | 201ce74 | 2009-09-17 03:17:01 +0000 | [diff] [blame] | 406 | void CXXNameMangler::mangleUnscopedName(const NamedDecl *ND) { |
| 407 | // <unscoped-name> ::= <unqualified-name> |
| 408 | // ::= St <unqualified-name> # ::std:: |
| 409 | if (isStdNamespace(ND->getDeclContext())) |
| 410 | Out << "St"; |
Daniel Dunbar | 3c9e463 | 2009-11-21 09:05:47 +0000 | [diff] [blame] | 411 | |
Anders Carlsson | 201ce74 | 2009-09-17 03:17:01 +0000 | [diff] [blame] | 412 | mangleUnqualifiedName(ND); |
| 413 | } |
| 414 | |
Anders Carlsson | 0fa6df4 | 2009-09-26 19:45:45 +0000 | [diff] [blame] | 415 | void CXXNameMangler::mangleUnscopedTemplateName(const TemplateDecl *ND) { |
Anders Carlsson | 201ce74 | 2009-09-17 03:17:01 +0000 | [diff] [blame] | 416 | // <unscoped-template-name> ::= <unscoped-name> |
| 417 | // ::= <substitution> |
Anders Carlsson | 7624f21 | 2009-09-18 02:42:01 +0000 | [diff] [blame] | 418 | if (mangleSubstitution(ND)) |
Anders Carlsson | 03c9d53 | 2009-09-17 04:02:31 +0000 | [diff] [blame] | 419 | return; |
Daniel Dunbar | 3c9e463 | 2009-11-21 09:05:47 +0000 | [diff] [blame] | 420 | |
Douglas Gregor | 32fb4e1 | 2010-02-05 20:45:00 +0000 | [diff] [blame] | 421 | // <template-template-param> ::= <template-param> |
| 422 | if (const TemplateTemplateParmDecl *TTP |
| 423 | = dyn_cast<TemplateTemplateParmDecl>(ND)) { |
| 424 | mangleTemplateParameter(TTP->getIndex()); |
| 425 | return; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 426 | } |
Douglas Gregor | 32fb4e1 | 2010-02-05 20:45:00 +0000 | [diff] [blame] | 427 | |
Anders Carlsson | 1668f20 | 2009-09-26 20:13:56 +0000 | [diff] [blame] | 428 | mangleUnscopedName(ND->getTemplatedDecl()); |
Anders Carlsson | 7624f21 | 2009-09-18 02:42:01 +0000 | [diff] [blame] | 429 | addSubstitution(ND); |
Anders Carlsson | 201ce74 | 2009-09-17 03:17:01 +0000 | [diff] [blame] | 430 | } |
| 431 | |
Anders Carlsson | a94822e | 2009-11-26 02:32:05 +0000 | [diff] [blame] | 432 | void CXXNameMangler::mangleNumber(int64_t Number) { |
| 433 | // <number> ::= [n] <non-negative decimal integer> |
| 434 | if (Number < 0) { |
| 435 | Out << 'n'; |
| 436 | Number = -Number; |
| 437 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 438 | |
Anders Carlsson | a94822e | 2009-11-26 02:32:05 +0000 | [diff] [blame] | 439 | Out << Number; |
| 440 | } |
| 441 | |
Anders Carlsson | 19879c9 | 2010-03-23 17:17:29 +0000 | [diff] [blame] | 442 | void CXXNameMangler::mangleCallOffset(int64_t NonVirtual, int64_t Virtual) { |
Mike Stump | 141c5af | 2009-09-02 00:25:38 +0000 | [diff] [blame] | 443 | // <call-offset> ::= h <nv-offset> _ |
| 444 | // ::= v <v-offset> _ |
| 445 | // <nv-offset> ::= <offset number> # non-virtual base override |
Anders Carlsson | a94822e | 2009-11-26 02:32:05 +0000 | [diff] [blame] | 446 | // <v-offset> ::= <offset number> _ <virtual offset number> |
Mike Stump | 141c5af | 2009-09-02 00:25:38 +0000 | [diff] [blame] | 447 | // # virtual base override, with vcall offset |
Anders Carlsson | 19879c9 | 2010-03-23 17:17:29 +0000 | [diff] [blame] | 448 | if (!Virtual) { |
Anders Carlsson | a94822e | 2009-11-26 02:32:05 +0000 | [diff] [blame] | 449 | Out << 'h'; |
Anders Carlsson | 19879c9 | 2010-03-23 17:17:29 +0000 | [diff] [blame] | 450 | mangleNumber(NonVirtual); |
Anders Carlsson | a94822e | 2009-11-26 02:32:05 +0000 | [diff] [blame] | 451 | Out << '_'; |
| 452 | return; |
Mike Stump | 141c5af | 2009-09-02 00:25:38 +0000 | [diff] [blame] | 453 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 454 | |
Anders Carlsson | a94822e | 2009-11-26 02:32:05 +0000 | [diff] [blame] | 455 | Out << 'v'; |
Anders Carlsson | 19879c9 | 2010-03-23 17:17:29 +0000 | [diff] [blame] | 456 | mangleNumber(NonVirtual); |
Anders Carlsson | a94822e | 2009-11-26 02:32:05 +0000 | [diff] [blame] | 457 | Out << '_'; |
Anders Carlsson | 19879c9 | 2010-03-23 17:17:29 +0000 | [diff] [blame] | 458 | mangleNumber(Virtual); |
Anders Carlsson | a94822e | 2009-11-26 02:32:05 +0000 | [diff] [blame] | 459 | Out << '_'; |
Mike Stump | 9124bcc | 2009-09-02 00:56:18 +0000 | [diff] [blame] | 460 | } |
| 461 | |
John McCall | 1dd7383 | 2010-02-04 01:42:13 +0000 | [diff] [blame] | 462 | void CXXNameMangler::mangleUnresolvedScope(NestedNameSpecifier *Qualifier) { |
| 463 | Qualifier = getASTContext().getCanonicalNestedNameSpecifier(Qualifier); |
| 464 | switch (Qualifier->getKind()) { |
| 465 | case NestedNameSpecifier::Global: |
| 466 | // nothing |
| 467 | break; |
| 468 | case NestedNameSpecifier::Namespace: |
| 469 | mangleName(Qualifier->getAsNamespace()); |
| 470 | break; |
| 471 | case NestedNameSpecifier::TypeSpec: |
Rafael Espindola | 9b35b25 | 2010-03-17 04:28:11 +0000 | [diff] [blame] | 472 | case NestedNameSpecifier::TypeSpecWithTemplate: { |
| 473 | const Type *QTy = Qualifier->getAsType(); |
| 474 | |
| 475 | if (const TemplateSpecializationType *TST = |
| 476 | dyn_cast<TemplateSpecializationType>(QTy)) { |
| 477 | if (!mangleSubstitution(QualType(TST, 0))) { |
| 478 | TemplateDecl *TD = TST->getTemplateName().getAsTemplateDecl(); |
| 479 | assert(TD && "FIXME: Support dependent template names"); |
| 480 | mangleTemplatePrefix(TD); |
| 481 | TemplateParameterList *TemplateParameters = TD->getTemplateParameters(); |
| 482 | mangleTemplateArgs(*TemplateParameters, TST->getArgs(), |
| 483 | TST->getNumArgs()); |
| 484 | addSubstitution(QualType(TST, 0)); |
| 485 | } |
| 486 | } else { |
| 487 | // We use the QualType mangle type variant here because it handles |
| 488 | // substitutions. |
| 489 | mangleType(QualType(QTy, 0)); |
| 490 | } |
| 491 | } |
John McCall | 1dd7383 | 2010-02-04 01:42:13 +0000 | [diff] [blame] | 492 | break; |
| 493 | case NestedNameSpecifier::Identifier: |
John McCall | ad5e738 | 2010-03-01 23:49:17 +0000 | [diff] [blame] | 494 | // Member expressions can have these without prefixes. |
| 495 | if (Qualifier->getPrefix()) |
| 496 | mangleUnresolvedScope(Qualifier->getPrefix()); |
John McCall | 1dd7383 | 2010-02-04 01:42:13 +0000 | [diff] [blame] | 497 | mangleSourceName(Qualifier->getAsIdentifier()); |
| 498 | break; |
| 499 | } |
| 500 | } |
| 501 | |
| 502 | /// Mangles a name which was not resolved to a specific entity. |
| 503 | void CXXNameMangler::mangleUnresolvedName(NestedNameSpecifier *Qualifier, |
| 504 | DeclarationName Name, |
| 505 | unsigned KnownArity) { |
| 506 | if (Qualifier) |
| 507 | mangleUnresolvedScope(Qualifier); |
| 508 | // FIXME: ambiguity of unqualified lookup with :: |
| 509 | |
| 510 | mangleUnqualifiedName(0, Name, KnownArity); |
| 511 | } |
| 512 | |
| 513 | void CXXNameMangler::mangleUnqualifiedName(const NamedDecl *ND, |
| 514 | DeclarationName Name, |
| 515 | unsigned KnownArity) { |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 516 | // <unqualified-name> ::= <operator-name> |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 517 | // ::= <ctor-dtor-name> |
| 518 | // ::= <source-name> |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 519 | switch (Name.getNameKind()) { |
Anders Carlsson | c4355b6 | 2009-10-07 01:45:02 +0000 | [diff] [blame] | 520 | case DeclarationName::Identifier: { |
Anders Carlsson | c4355b6 | 2009-10-07 01:45:02 +0000 | [diff] [blame] | 521 | if (const IdentifierInfo *II = Name.getAsIdentifierInfo()) { |
Sean Hunt | 3145525 | 2010-01-24 03:04:27 +0000 | [diff] [blame] | 522 | // We must avoid conflicts between internally- and externally- |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 523 | // linked variable declaration names in the same TU. |
Anders Carlsson | aec2523 | 2010-02-06 04:52:27 +0000 | [diff] [blame] | 524 | // This naming convention is the same as that followed by GCC, though it |
| 525 | // shouldn't actually matter. |
| 526 | if (ND && isa<VarDecl>(ND) && ND->getLinkage() == InternalLinkage && |
Sean Hunt | 3145525 | 2010-01-24 03:04:27 +0000 | [diff] [blame] | 527 | ND->getDeclContext()->isFileContext()) |
| 528 | Out << 'L'; |
| 529 | |
Anders Carlsson | c4355b6 | 2009-10-07 01:45:02 +0000 | [diff] [blame] | 530 | mangleSourceName(II); |
| 531 | break; |
| 532 | } |
Daniel Dunbar | 3c9e463 | 2009-11-21 09:05:47 +0000 | [diff] [blame] | 533 | |
John McCall | 1dd7383 | 2010-02-04 01:42:13 +0000 | [diff] [blame] | 534 | // Otherwise, an anonymous entity. We must have a declaration. |
| 535 | assert(ND && "mangling empty name without declaration"); |
| 536 | |
| 537 | if (const NamespaceDecl *NS = dyn_cast<NamespaceDecl>(ND)) { |
| 538 | if (NS->isAnonymousNamespace()) { |
| 539 | // This is how gcc mangles these names. |
| 540 | Out << "12_GLOBAL__N_1"; |
| 541 | break; |
| 542 | } |
| 543 | } |
| 544 | |
Anders Carlsson | c4355b6 | 2009-10-07 01:45:02 +0000 | [diff] [blame] | 545 | // We must have an anonymous struct. |
| 546 | const TagDecl *TD = cast<TagDecl>(ND); |
| 547 | if (const TypedefDecl *D = TD->getTypedefForAnonDecl()) { |
| 548 | assert(TD->getDeclContext() == D->getDeclContext() && |
| 549 | "Typedef should not be in another decl context!"); |
| 550 | assert(D->getDeclName().getAsIdentifierInfo() && |
| 551 | "Typedef was not named!"); |
| 552 | mangleSourceName(D->getDeclName().getAsIdentifierInfo()); |
| 553 | break; |
| 554 | } |
Daniel Dunbar | 3c9e463 | 2009-11-21 09:05:47 +0000 | [diff] [blame] | 555 | |
Anders Carlsson | c4355b6 | 2009-10-07 01:45:02 +0000 | [diff] [blame] | 556 | // Get a unique id for the anonymous struct. |
| 557 | uint64_t AnonStructId = Context.getAnonymousStructId(TD); |
| 558 | |
| 559 | // Mangle it as a source name in the form |
Daniel Dunbar | 3c9e463 | 2009-11-21 09:05:47 +0000 | [diff] [blame] | 560 | // [n] $_<id> |
Anders Carlsson | c4355b6 | 2009-10-07 01:45:02 +0000 | [diff] [blame] | 561 | // where n is the length of the string. |
| 562 | llvm::SmallString<8> Str; |
| 563 | Str += "$_"; |
| 564 | Str += llvm::utostr(AnonStructId); |
| 565 | |
| 566 | Out << Str.size(); |
| 567 | Out << Str.str(); |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 568 | break; |
Anders Carlsson | c4355b6 | 2009-10-07 01:45:02 +0000 | [diff] [blame] | 569 | } |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 570 | |
| 571 | case DeclarationName::ObjCZeroArgSelector: |
| 572 | case DeclarationName::ObjCOneArgSelector: |
| 573 | case DeclarationName::ObjCMultiArgSelector: |
| 574 | assert(false && "Can't mangle Objective-C selector names here!"); |
| 575 | break; |
| 576 | |
| 577 | case DeclarationName::CXXConstructorName: |
Anders Carlsson | 27ae536 | 2009-04-17 01:58:57 +0000 | [diff] [blame] | 578 | if (ND == Structor) |
Mike Stump | 141c5af | 2009-09-02 00:25:38 +0000 | [diff] [blame] | 579 | // If the named decl is the C++ constructor we're mangling, use the type |
| 580 | // we were given. |
Anders Carlsson | 27ae536 | 2009-04-17 01:58:57 +0000 | [diff] [blame] | 581 | mangleCXXCtorType(static_cast<CXXCtorType>(StructorType)); |
Anders Carlsson | 3ac86b5 | 2009-04-15 05:36:58 +0000 | [diff] [blame] | 582 | else |
| 583 | // Otherwise, use the complete constructor name. This is relevant if a |
| 584 | // class with a constructor is declared within a constructor. |
| 585 | mangleCXXCtorType(Ctor_Complete); |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 586 | break; |
| 587 | |
| 588 | case DeclarationName::CXXDestructorName: |
Anders Carlsson | 27ae536 | 2009-04-17 01:58:57 +0000 | [diff] [blame] | 589 | if (ND == Structor) |
Mike Stump | 141c5af | 2009-09-02 00:25:38 +0000 | [diff] [blame] | 590 | // If the named decl is the C++ destructor we're mangling, use the type we |
| 591 | // were given. |
Anders Carlsson | 27ae536 | 2009-04-17 01:58:57 +0000 | [diff] [blame] | 592 | mangleCXXDtorType(static_cast<CXXDtorType>(StructorType)); |
| 593 | else |
| 594 | // Otherwise, use the complete destructor name. This is relevant if a |
| 595 | // class with a destructor is declared within a destructor. |
| 596 | mangleCXXDtorType(Dtor_Complete); |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 597 | break; |
| 598 | |
| 599 | case DeclarationName::CXXConversionFunctionName: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 600 | // <operator-name> ::= cv <type> # (cast) |
Douglas Gregor | 219cc61 | 2009-02-13 01:28:03 +0000 | [diff] [blame] | 601 | Out << "cv"; |
Anders Carlsson | b540491 | 2009-10-07 01:06:45 +0000 | [diff] [blame] | 602 | mangleType(Context.getASTContext().getCanonicalType(Name.getCXXNameType())); |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 603 | break; |
| 604 | |
Anders Carlsson | 8257d41 | 2009-12-22 06:36:32 +0000 | [diff] [blame] | 605 | case DeclarationName::CXXOperatorName: { |
John McCall | 1dd7383 | 2010-02-04 01:42:13 +0000 | [diff] [blame] | 606 | unsigned Arity; |
| 607 | if (ND) { |
| 608 | Arity = cast<FunctionDecl>(ND)->getNumParams(); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 609 | |
John McCall | 1dd7383 | 2010-02-04 01:42:13 +0000 | [diff] [blame] | 610 | // If we have a C++ member function, we need to include the 'this' pointer. |
| 611 | // FIXME: This does not make sense for operators that are static, but their |
| 612 | // names stay the same regardless of the arity (operator new for instance). |
| 613 | if (isa<CXXMethodDecl>(ND)) |
| 614 | Arity++; |
| 615 | } else |
| 616 | Arity = KnownArity; |
| 617 | |
Anders Carlsson | 8257d41 | 2009-12-22 06:36:32 +0000 | [diff] [blame] | 618 | mangleOperatorName(Name.getCXXOverloadedOperator(), Arity); |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 619 | break; |
Anders Carlsson | 8257d41 | 2009-12-22 06:36:32 +0000 | [diff] [blame] | 620 | } |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 621 | |
Sean Hunt | 3e518bd | 2009-11-29 07:34:05 +0000 | [diff] [blame] | 622 | case DeclarationName::CXXLiteralOperatorName: |
Sean Hunt | 5dd6b39 | 2009-12-04 21:11:13 +0000 | [diff] [blame] | 623 | // FIXME: This mangling is not yet official. |
Sean Hunt | 2421f66 | 2009-12-04 21:01:37 +0000 | [diff] [blame] | 624 | Out << "li"; |
Sean Hunt | 3e518bd | 2009-11-29 07:34:05 +0000 | [diff] [blame] | 625 | mangleSourceName(Name.getCXXLiteralIdentifier()); |
| 626 | break; |
| 627 | |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 628 | case DeclarationName::CXXUsingDirective: |
| 629 | assert(false && "Can't mangle a using directive name!"); |
Douglas Gregor | 219cc61 | 2009-02-13 01:28:03 +0000 | [diff] [blame] | 630 | break; |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 631 | } |
| 632 | } |
| 633 | |
| 634 | void CXXNameMangler::mangleSourceName(const IdentifierInfo *II) { |
| 635 | // <source-name> ::= <positive length number> <identifier> |
| 636 | // <number> ::= [n] <non-negative decimal integer> |
| 637 | // <identifier> ::= <unqualified source code identifier> |
| 638 | Out << II->getLength() << II->getName(); |
| 639 | } |
| 640 | |
Eli Friedman | 7facf84 | 2009-12-02 20:32:49 +0000 | [diff] [blame] | 641 | void CXXNameMangler::mangleNestedName(const NamedDecl *ND, |
Fariborz Jahanian | 5705853 | 2010-03-03 19:41:08 +0000 | [diff] [blame] | 642 | const DeclContext *DC, |
| 643 | bool NoFunction) { |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 644 | // <nested-name> ::= N [<CV-qualifiers>] <prefix> <unqualified-name> E |
| 645 | // ::= N [<CV-qualifiers>] <template-prefix> <template-args> E |
Anders Carlsson | d99edc4 | 2009-09-26 03:55:37 +0000 | [diff] [blame] | 646 | |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 647 | Out << 'N'; |
| 648 | if (const CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(ND)) |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 649 | mangleQualifiers(Qualifiers::fromCVRMask(Method->getTypeQualifiers())); |
Daniel Dunbar | 3c9e463 | 2009-11-21 09:05:47 +0000 | [diff] [blame] | 650 | |
Anders Carlsson | 2744a06 | 2009-09-18 19:00:18 +0000 | [diff] [blame] | 651 | // Check if we have a template. |
| 652 | const TemplateArgumentList *TemplateArgs = 0; |
Daniel Dunbar | 3c9e463 | 2009-11-21 09:05:47 +0000 | [diff] [blame] | 653 | if (const TemplateDecl *TD = isTemplate(ND, TemplateArgs)) { |
Anders Carlsson | 2744a06 | 2009-09-18 19:00:18 +0000 | [diff] [blame] | 654 | mangleTemplatePrefix(TD); |
Rafael Espindola | d980072 | 2010-03-11 14:07:00 +0000 | [diff] [blame] | 655 | TemplateParameterList *TemplateParameters = TD->getTemplateParameters(); |
| 656 | mangleTemplateArgs(*TemplateParameters, *TemplateArgs); |
Fariborz Jahanian | 5705853 | 2010-03-03 19:41:08 +0000 | [diff] [blame] | 657 | } |
| 658 | else { |
| 659 | manglePrefix(DC, NoFunction); |
Anders Carlsson | 7482e24 | 2009-09-18 04:29:09 +0000 | [diff] [blame] | 660 | mangleUnqualifiedName(ND); |
| 661 | } |
Daniel Dunbar | 3c9e463 | 2009-11-21 09:05:47 +0000 | [diff] [blame] | 662 | |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 663 | Out << 'E'; |
| 664 | } |
Daniel Dunbar | 3c9e463 | 2009-11-21 09:05:47 +0000 | [diff] [blame] | 665 | void CXXNameMangler::mangleNestedName(const TemplateDecl *TD, |
Anders Carlsson | 7624f21 | 2009-09-18 02:42:01 +0000 | [diff] [blame] | 666 | const TemplateArgument *TemplateArgs, |
| 667 | unsigned NumTemplateArgs) { |
Anders Carlsson | e45117b | 2009-09-27 19:53:49 +0000 | [diff] [blame] | 668 | // <nested-name> ::= N [<CV-qualifiers>] <template-prefix> <template-args> E |
| 669 | |
Anders Carlsson | 7624f21 | 2009-09-18 02:42:01 +0000 | [diff] [blame] | 670 | Out << 'N'; |
Daniel Dunbar | 3c9e463 | 2009-11-21 09:05:47 +0000 | [diff] [blame] | 671 | |
Anders Carlsson | e45117b | 2009-09-27 19:53:49 +0000 | [diff] [blame] | 672 | mangleTemplatePrefix(TD); |
Rafael Espindola | d980072 | 2010-03-11 14:07:00 +0000 | [diff] [blame] | 673 | TemplateParameterList *TemplateParameters = TD->getTemplateParameters(); |
| 674 | mangleTemplateArgs(*TemplateParameters, TemplateArgs, NumTemplateArgs); |
Daniel Dunbar | 3c9e463 | 2009-11-21 09:05:47 +0000 | [diff] [blame] | 675 | |
Anders Carlsson | 7624f21 | 2009-09-18 02:42:01 +0000 | [diff] [blame] | 676 | Out << 'E'; |
| 677 | } |
| 678 | |
Anders Carlsson | 1b42c79 | 2009-04-02 16:24:45 +0000 | [diff] [blame] | 679 | void CXXNameMangler::mangleLocalName(const NamedDecl *ND) { |
| 680 | // <local-name> := Z <function encoding> E <entity name> [<discriminator>] |
| 681 | // := Z <function encoding> E s [<discriminator>] |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 682 | // <discriminator> := _ <non-negative number> |
Fariborz Jahanian | 5705853 | 2010-03-03 19:41:08 +0000 | [diff] [blame] | 683 | const DeclContext *DC = ND->getDeclContext(); |
Anders Carlsson | 1b42c79 | 2009-04-02 16:24:45 +0000 | [diff] [blame] | 684 | Out << 'Z'; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 685 | |
Fariborz Jahanian | 5705853 | 2010-03-03 19:41:08 +0000 | [diff] [blame] | 686 | if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(DC)) |
Anders Carlsson | 7b06f6c | 2009-12-10 03:14:39 +0000 | [diff] [blame] | 687 | mangleObjCMethodName(MD); |
Fariborz Jahanian | 5705853 | 2010-03-03 19:41:08 +0000 | [diff] [blame] | 688 | else if (const DeclContext *CDC = GetLocalClassFunctionDeclContext(DC)) { |
| 689 | mangleFunctionEncoding(cast<FunctionDecl>(CDC)); |
| 690 | Out << 'E'; |
| 691 | mangleNestedName(ND, DC, true /*NoFunction*/); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 692 | |
Fariborz Jahanian | 4819ac4 | 2010-03-04 01:02:03 +0000 | [diff] [blame] | 693 | // FIXME. This still does not cover all cases. |
| 694 | unsigned disc; |
| 695 | if (Context.getNextDiscriminator(ND, disc)) { |
| 696 | if (disc < 10) |
| 697 | Out << '_' << disc; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 698 | else |
Fariborz Jahanian | 4819ac4 | 2010-03-04 01:02:03 +0000 | [diff] [blame] | 699 | Out << "__" << disc << '_'; |
| 700 | } |
Fariborz Jahanian | 5705853 | 2010-03-03 19:41:08 +0000 | [diff] [blame] | 701 | |
| 702 | return; |
| 703 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 704 | else |
Fariborz Jahanian | 5705853 | 2010-03-03 19:41:08 +0000 | [diff] [blame] | 705 | mangleFunctionEncoding(cast<FunctionDecl>(DC)); |
Anders Carlsson | 7b06f6c | 2009-12-10 03:14:39 +0000 | [diff] [blame] | 706 | |
Anders Carlsson | 1b42c79 | 2009-04-02 16:24:45 +0000 | [diff] [blame] | 707 | Out << 'E'; |
Eli Friedman | 6f9f25d | 2009-12-11 20:21:38 +0000 | [diff] [blame] | 708 | mangleUnqualifiedName(ND); |
Anders Carlsson | 1b42c79 | 2009-04-02 16:24:45 +0000 | [diff] [blame] | 709 | } |
| 710 | |
Fariborz Jahanian | 5705853 | 2010-03-03 19:41:08 +0000 | [diff] [blame] | 711 | void CXXNameMangler::manglePrefix(const DeclContext *DC, bool NoFunction) { |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 712 | // <prefix> ::= <prefix> <unqualified-name> |
| 713 | // ::= <template-prefix> <template-args> |
| 714 | // ::= <template-param> |
| 715 | // ::= # empty |
| 716 | // ::= <substitution> |
Anders Carlsson | 6862fc7 | 2009-09-17 04:16:28 +0000 | [diff] [blame] | 717 | |
Anders Carlsson | add2882 | 2009-09-22 20:33:31 +0000 | [diff] [blame] | 718 | while (isa<LinkageSpecDecl>(DC)) |
| 719 | DC = DC->getParent(); |
Daniel Dunbar | 3c9e463 | 2009-11-21 09:05:47 +0000 | [diff] [blame] | 720 | |
Anders Carlsson | 9263e91 | 2009-09-18 18:39:58 +0000 | [diff] [blame] | 721 | if (DC->isTranslationUnit()) |
| 722 | return; |
Daniel Dunbar | 3c9e463 | 2009-11-21 09:05:47 +0000 | [diff] [blame] | 723 | |
Anders Carlsson | 6862fc7 | 2009-09-17 04:16:28 +0000 | [diff] [blame] | 724 | if (mangleSubstitution(cast<NamedDecl>(DC))) |
| 725 | return; |
Anders Carlsson | 7482e24 | 2009-09-18 04:29:09 +0000 | [diff] [blame] | 726 | |
Anders Carlsson | 2ee3fca | 2009-09-18 20:11:09 +0000 | [diff] [blame] | 727 | // Check if we have a template. |
| 728 | const TemplateArgumentList *TemplateArgs = 0; |
Daniel Dunbar | 3c9e463 | 2009-11-21 09:05:47 +0000 | [diff] [blame] | 729 | if (const TemplateDecl *TD = isTemplate(cast<NamedDecl>(DC), TemplateArgs)) { |
Anders Carlsson | 2ee3fca | 2009-09-18 20:11:09 +0000 | [diff] [blame] | 730 | mangleTemplatePrefix(TD); |
Rafael Espindola | d980072 | 2010-03-11 14:07:00 +0000 | [diff] [blame] | 731 | TemplateParameterList *TemplateParameters = TD->getTemplateParameters(); |
| 732 | mangleTemplateArgs(*TemplateParameters, *TemplateArgs); |
Fariborz Jahanian | 5705853 | 2010-03-03 19:41:08 +0000 | [diff] [blame] | 733 | } |
| 734 | else if(NoFunction && isa<FunctionDecl>(DC)) |
| 735 | return; |
| 736 | else { |
| 737 | manglePrefix(DC->getParent(), NoFunction); |
Anders Carlsson | 2ee3fca | 2009-09-18 20:11:09 +0000 | [diff] [blame] | 738 | mangleUnqualifiedName(cast<NamedDecl>(DC)); |
| 739 | } |
Daniel Dunbar | 3c9e463 | 2009-11-21 09:05:47 +0000 | [diff] [blame] | 740 | |
Anders Carlsson | 6862fc7 | 2009-09-17 04:16:28 +0000 | [diff] [blame] | 741 | addSubstitution(cast<NamedDecl>(DC)); |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 742 | } |
| 743 | |
Anders Carlsson | 0fa6df4 | 2009-09-26 19:45:45 +0000 | [diff] [blame] | 744 | void CXXNameMangler::mangleTemplatePrefix(const TemplateDecl *ND) { |
Anders Carlsson | 7482e24 | 2009-09-18 04:29:09 +0000 | [diff] [blame] | 745 | // <template-prefix> ::= <prefix> <template unqualified-name> |
| 746 | // ::= <template-param> |
| 747 | // ::= <substitution> |
Douglas Gregor | 32fb4e1 | 2010-02-05 20:45:00 +0000 | [diff] [blame] | 748 | // <template-template-param> ::= <template-param> |
| 749 | // <substitution> |
Anders Carlsson | 7482e24 | 2009-09-18 04:29:09 +0000 | [diff] [blame] | 750 | |
Anders Carlsson | aeb8537 | 2009-09-26 22:18:22 +0000 | [diff] [blame] | 751 | if (mangleSubstitution(ND)) |
| 752 | return; |
Daniel Dunbar | 3c9e463 | 2009-11-21 09:05:47 +0000 | [diff] [blame] | 753 | |
Douglas Gregor | 32fb4e1 | 2010-02-05 20:45:00 +0000 | [diff] [blame] | 754 | // <template-template-param> ::= <template-param> |
| 755 | if (const TemplateTemplateParmDecl *TTP |
| 756 | = dyn_cast<TemplateTemplateParmDecl>(ND)) { |
| 757 | mangleTemplateParameter(TTP->getIndex()); |
| 758 | return; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 759 | } |
Daniel Dunbar | 3c9e463 | 2009-11-21 09:05:47 +0000 | [diff] [blame] | 760 | |
Anders Carlsson | aa73ab1 | 2009-09-18 18:47:07 +0000 | [diff] [blame] | 761 | manglePrefix(ND->getDeclContext()); |
Anders Carlsson | 1668f20 | 2009-09-26 20:13:56 +0000 | [diff] [blame] | 762 | mangleUnqualifiedName(ND->getTemplatedDecl()); |
Anders Carlsson | aeb8537 | 2009-09-26 22:18:22 +0000 | [diff] [blame] | 763 | addSubstitution(ND); |
Anders Carlsson | 7482e24 | 2009-09-18 04:29:09 +0000 | [diff] [blame] | 764 | } |
| 765 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 766 | void |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 767 | CXXNameMangler::mangleOperatorName(OverloadedOperatorKind OO, unsigned Arity) { |
| 768 | switch (OO) { |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 769 | // <operator-name> ::= nw # new |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 770 | case OO_New: Out << "nw"; break; |
| 771 | // ::= na # new[] |
| 772 | case OO_Array_New: Out << "na"; break; |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 773 | // ::= dl # delete |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 774 | case OO_Delete: Out << "dl"; break; |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 775 | // ::= da # delete[] |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 776 | case OO_Array_Delete: Out << "da"; break; |
| 777 | // ::= ps # + (unary) |
| 778 | // ::= pl # + |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 779 | case OO_Plus: |
Anders Carlsson | 8257d41 | 2009-12-22 06:36:32 +0000 | [diff] [blame] | 780 | assert((Arity == 1 || Arity == 2) && "Invalid arity!"); |
| 781 | Out << (Arity == 1? "ps" : "pl"); break; |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 782 | // ::= ng # - (unary) |
| 783 | // ::= mi # - |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 784 | case OO_Minus: |
Anders Carlsson | 8257d41 | 2009-12-22 06:36:32 +0000 | [diff] [blame] | 785 | assert((Arity == 1 || Arity == 2) && "Invalid arity!"); |
| 786 | Out << (Arity == 1? "ng" : "mi"); break; |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 787 | // ::= ad # & (unary) |
| 788 | // ::= an # & |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 789 | case OO_Amp: |
Anders Carlsson | 8257d41 | 2009-12-22 06:36:32 +0000 | [diff] [blame] | 790 | assert((Arity == 1 || Arity == 2) && "Invalid arity!"); |
| 791 | Out << (Arity == 1? "ad" : "an"); break; |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 792 | // ::= de # * (unary) |
| 793 | // ::= ml # * |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 794 | case OO_Star: |
Anders Carlsson | 8257d41 | 2009-12-22 06:36:32 +0000 | [diff] [blame] | 795 | assert((Arity == 1 || Arity == 2) && "Invalid arity!"); |
| 796 | Out << (Arity == 1? "de" : "ml"); break; |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 797 | // ::= co # ~ |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 798 | case OO_Tilde: Out << "co"; break; |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 799 | // ::= dv # / |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 800 | case OO_Slash: Out << "dv"; break; |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 801 | // ::= rm # % |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 802 | case OO_Percent: Out << "rm"; break; |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 803 | // ::= or # | |
| 804 | case OO_Pipe: Out << "or"; break; |
| 805 | // ::= eo # ^ |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 806 | case OO_Caret: Out << "eo"; break; |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 807 | // ::= aS # = |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 808 | case OO_Equal: Out << "aS"; break; |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 809 | // ::= pL # += |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 810 | case OO_PlusEqual: Out << "pL"; break; |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 811 | // ::= mI # -= |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 812 | case OO_MinusEqual: Out << "mI"; break; |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 813 | // ::= mL # *= |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 814 | case OO_StarEqual: Out << "mL"; break; |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 815 | // ::= dV # /= |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 816 | case OO_SlashEqual: Out << "dV"; break; |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 817 | // ::= rM # %= |
| 818 | case OO_PercentEqual: Out << "rM"; break; |
| 819 | // ::= aN # &= |
| 820 | case OO_AmpEqual: Out << "aN"; break; |
| 821 | // ::= oR # |= |
| 822 | case OO_PipeEqual: Out << "oR"; break; |
| 823 | // ::= eO # ^= |
| 824 | case OO_CaretEqual: Out << "eO"; break; |
| 825 | // ::= ls # << |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 826 | case OO_LessLess: Out << "ls"; break; |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 827 | // ::= rs # >> |
| 828 | case OO_GreaterGreater: Out << "rs"; break; |
| 829 | // ::= lS # <<= |
| 830 | case OO_LessLessEqual: Out << "lS"; break; |
| 831 | // ::= rS # >>= |
| 832 | case OO_GreaterGreaterEqual: Out << "rS"; break; |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 833 | // ::= eq # == |
| 834 | case OO_EqualEqual: Out << "eq"; break; |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 835 | // ::= ne # != |
| 836 | case OO_ExclaimEqual: Out << "ne"; break; |
| 837 | // ::= lt # < |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 838 | case OO_Less: Out << "lt"; break; |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 839 | // ::= gt # > |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 840 | case OO_Greater: Out << "gt"; break; |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 841 | // ::= le # <= |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 842 | case OO_LessEqual: Out << "le"; break; |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 843 | // ::= ge # >= |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 844 | case OO_GreaterEqual: Out << "ge"; break; |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 845 | // ::= nt # ! |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 846 | case OO_Exclaim: Out << "nt"; break; |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 847 | // ::= aa # && |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 848 | case OO_AmpAmp: Out << "aa"; break; |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 849 | // ::= oo # || |
| 850 | case OO_PipePipe: Out << "oo"; break; |
| 851 | // ::= pp # ++ |
| 852 | case OO_PlusPlus: Out << "pp"; break; |
| 853 | // ::= mm # -- |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 854 | case OO_MinusMinus: Out << "mm"; break; |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 855 | // ::= cm # , |
| 856 | case OO_Comma: Out << "cm"; break; |
| 857 | // ::= pm # ->* |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 858 | case OO_ArrowStar: Out << "pm"; break; |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 859 | // ::= pt # -> |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 860 | case OO_Arrow: Out << "pt"; break; |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 861 | // ::= cl # () |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 862 | case OO_Call: Out << "cl"; break; |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 863 | // ::= ix # [] |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 864 | case OO_Subscript: Out << "ix"; break; |
Anders Carlsson | e170ba7 | 2009-12-14 01:45:37 +0000 | [diff] [blame] | 865 | |
| 866 | // ::= qu # ? |
| 867 | // The conditional operator can't be overloaded, but we still handle it when |
| 868 | // mangling expressions. |
| 869 | case OO_Conditional: Out << "qu"; break; |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 870 | |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 871 | case OO_None: |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 872 | case NUM_OVERLOADED_OPERATORS: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 873 | assert(false && "Not an overloaded operator"); |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 874 | break; |
| 875 | } |
| 876 | } |
| 877 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 878 | void CXXNameMangler::mangleQualifiers(Qualifiers Quals) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 879 | // <CV-qualifiers> ::= [r] [V] [K] # restrict (C99), volatile, const |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 880 | if (Quals.hasRestrict()) |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 881 | Out << 'r'; |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 882 | if (Quals.hasVolatile()) |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 883 | Out << 'V'; |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 884 | if (Quals.hasConst()) |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 885 | Out << 'K'; |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 886 | |
| 887 | // FIXME: For now, just drop all extension qualifiers on the floor. |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 888 | } |
| 889 | |
Anders Carlsson | 7b06f6c | 2009-12-10 03:14:39 +0000 | [diff] [blame] | 890 | void CXXNameMangler::mangleObjCMethodName(const ObjCMethodDecl *MD) { |
| 891 | llvm::SmallString<64> Name; |
| 892 | llvm::raw_svector_ostream OS(Name); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 893 | |
| 894 | const ObjCContainerDecl *CD = |
Anders Carlsson | 7b06f6c | 2009-12-10 03:14:39 +0000 | [diff] [blame] | 895 | dyn_cast<ObjCContainerDecl>(MD->getDeclContext()); |
| 896 | assert (CD && "Missing container decl in GetNameForMethod"); |
| 897 | OS << (MD->isInstanceMethod() ? '-' : '+') << '[' << CD->getName(); |
| 898 | if (const ObjCCategoryImplDecl *CID = dyn_cast<ObjCCategoryImplDecl>(CD)) |
| 899 | OS << '(' << CID->getNameAsString() << ')'; |
| 900 | OS << ' ' << MD->getSelector().getAsString() << ']'; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 901 | |
Anders Carlsson | 7b06f6c | 2009-12-10 03:14:39 +0000 | [diff] [blame] | 902 | Out << OS.str().size() << OS.str(); |
| 903 | } |
| 904 | |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 905 | void CXXNameMangler::mangleType(QualType T) { |
Anders Carlsson | 4843e58 | 2009-03-10 17:07:44 +0000 | [diff] [blame] | 906 | // Only operate on the canonical type! |
Anders Carlsson | b540491 | 2009-10-07 01:06:45 +0000 | [diff] [blame] | 907 | T = Context.getASTContext().getCanonicalType(T); |
Anders Carlsson | 4843e58 | 2009-03-10 17:07:44 +0000 | [diff] [blame] | 908 | |
Douglas Gregor | a4923eb | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 909 | bool IsSubstitutable = T.hasLocalQualifiers() || !isa<BuiltinType>(T); |
Anders Carlsson | 7696737 | 2009-09-17 00:43:46 +0000 | [diff] [blame] | 910 | if (IsSubstitutable && mangleSubstitution(T)) |
| 911 | return; |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 912 | |
Douglas Gregor | a4923eb | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 913 | if (Qualifiers Quals = T.getLocalQualifiers()) { |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 914 | mangleQualifiers(Quals); |
| 915 | // Recurse: even if the qualified type isn't yet substitutable, |
| 916 | // the unqualified type might be. |
Douglas Gregor | a4923eb | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 917 | mangleType(T.getLocalUnqualifiedType()); |
Anders Carlsson | 7696737 | 2009-09-17 00:43:46 +0000 | [diff] [blame] | 918 | } else { |
| 919 | switch (T->getTypeClass()) { |
John McCall | efe6aee | 2009-09-05 07:56:18 +0000 | [diff] [blame] | 920 | #define ABSTRACT_TYPE(CLASS, PARENT) |
| 921 | #define NON_CANONICAL_TYPE(CLASS, PARENT) \ |
Anders Carlsson | 7696737 | 2009-09-17 00:43:46 +0000 | [diff] [blame] | 922 | case Type::CLASS: \ |
Jeffrey Yasskin | 9f61aa9 | 2009-12-12 05:05:38 +0000 | [diff] [blame] | 923 | llvm_unreachable("can't mangle non-canonical type " #CLASS "Type"); \ |
Anders Carlsson | 7696737 | 2009-09-17 00:43:46 +0000 | [diff] [blame] | 924 | return; |
John McCall | efe6aee | 2009-09-05 07:56:18 +0000 | [diff] [blame] | 925 | #define TYPE(CLASS, PARENT) \ |
Anders Carlsson | 7696737 | 2009-09-17 00:43:46 +0000 | [diff] [blame] | 926 | case Type::CLASS: \ |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 927 | mangleType(static_cast<const CLASS##Type*>(T.getTypePtr())); \ |
Anders Carlsson | 7696737 | 2009-09-17 00:43:46 +0000 | [diff] [blame] | 928 | break; |
John McCall | efe6aee | 2009-09-05 07:56:18 +0000 | [diff] [blame] | 929 | #include "clang/AST/TypeNodes.def" |
Anders Carlsson | 7696737 | 2009-09-17 00:43:46 +0000 | [diff] [blame] | 930 | } |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 931 | } |
Anders Carlsson | 7696737 | 2009-09-17 00:43:46 +0000 | [diff] [blame] | 932 | |
| 933 | // Add the substitution. |
| 934 | if (IsSubstitutable) |
| 935 | addSubstitution(T); |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 936 | } |
| 937 | |
| 938 | void CXXNameMangler::mangleType(const BuiltinType *T) { |
John McCall | efe6aee | 2009-09-05 07:56:18 +0000 | [diff] [blame] | 939 | // <type> ::= <builtin-type> |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 940 | // <builtin-type> ::= v # void |
| 941 | // ::= w # wchar_t |
| 942 | // ::= b # bool |
| 943 | // ::= c # char |
| 944 | // ::= a # signed char |
| 945 | // ::= h # unsigned char |
| 946 | // ::= s # short |
| 947 | // ::= t # unsigned short |
| 948 | // ::= i # int |
| 949 | // ::= j # unsigned int |
| 950 | // ::= l # long |
| 951 | // ::= m # unsigned long |
| 952 | // ::= x # long long, __int64 |
| 953 | // ::= y # unsigned long long, __int64 |
| 954 | // ::= n # __int128 |
| 955 | // UNSUPPORTED: ::= o # unsigned __int128 |
| 956 | // ::= f # float |
| 957 | // ::= d # double |
| 958 | // ::= e # long double, __float80 |
| 959 | // UNSUPPORTED: ::= g # __float128 |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 960 | // UNSUPPORTED: ::= Dd # IEEE 754r decimal floating point (64 bits) |
| 961 | // UNSUPPORTED: ::= De # IEEE 754r decimal floating point (128 bits) |
| 962 | // UNSUPPORTED: ::= Df # IEEE 754r decimal floating point (32 bits) |
| 963 | // UNSUPPORTED: ::= Dh # IEEE 754r half-precision floating point (16 bits) |
Alisdair Meredith | f5c209d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 964 | // ::= Di # char32_t |
| 965 | // ::= Ds # char16_t |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 966 | // ::= u <source-name> # vendor extended type |
Sebastian Redl | 6e8ed16 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 967 | // From our point of view, std::nullptr_t is a builtin, but as far as mangling |
| 968 | // is concerned, it's a type called std::nullptr_t. |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 969 | switch (T->getKind()) { |
| 970 | case BuiltinType::Void: Out << 'v'; break; |
| 971 | case BuiltinType::Bool: Out << 'b'; break; |
| 972 | case BuiltinType::Char_U: case BuiltinType::Char_S: Out << 'c'; break; |
| 973 | case BuiltinType::UChar: Out << 'h'; break; |
| 974 | case BuiltinType::UShort: Out << 't'; break; |
| 975 | case BuiltinType::UInt: Out << 'j'; break; |
| 976 | case BuiltinType::ULong: Out << 'm'; break; |
| 977 | case BuiltinType::ULongLong: Out << 'y'; break; |
Chris Lattner | 2df9ced | 2009-04-30 02:43:43 +0000 | [diff] [blame] | 978 | case BuiltinType::UInt128: Out << 'o'; break; |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 979 | case BuiltinType::SChar: Out << 'a'; break; |
| 980 | case BuiltinType::WChar: Out << 'w'; break; |
Alisdair Meredith | f5c209d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 981 | case BuiltinType::Char16: Out << "Ds"; break; |
| 982 | case BuiltinType::Char32: Out << "Di"; break; |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 983 | case BuiltinType::Short: Out << 's'; break; |
| 984 | case BuiltinType::Int: Out << 'i'; break; |
| 985 | case BuiltinType::Long: Out << 'l'; break; |
| 986 | case BuiltinType::LongLong: Out << 'x'; break; |
Chris Lattner | 2df9ced | 2009-04-30 02:43:43 +0000 | [diff] [blame] | 987 | case BuiltinType::Int128: Out << 'n'; break; |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 988 | case BuiltinType::Float: Out << 'f'; break; |
| 989 | case BuiltinType::Double: Out << 'd'; break; |
| 990 | case BuiltinType::LongDouble: Out << 'e'; break; |
Sebastian Redl | 6e8ed16 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 991 | case BuiltinType::NullPtr: Out << "St9nullptr_t"; break; |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 992 | |
| 993 | case BuiltinType::Overload: |
| 994 | case BuiltinType::Dependent: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 995 | assert(false && |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 996 | "Overloaded and dependent types shouldn't get to name mangling"); |
| 997 | break; |
Anders Carlsson | e89d159 | 2009-06-26 18:41:36 +0000 | [diff] [blame] | 998 | case BuiltinType::UndeducedAuto: |
| 999 | assert(0 && "Should not see undeduced auto here"); |
| 1000 | break; |
Steve Naroff | 9533a7f | 2009-07-22 17:14:51 +0000 | [diff] [blame] | 1001 | case BuiltinType::ObjCId: Out << "11objc_object"; break; |
| 1002 | case BuiltinType::ObjCClass: Out << "10objc_class"; break; |
Fariborz Jahanian | 13dcd00 | 2009-11-21 19:53:08 +0000 | [diff] [blame] | 1003 | case BuiltinType::ObjCSel: Out << "13objc_selector"; break; |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 1004 | } |
| 1005 | } |
| 1006 | |
John McCall | efe6aee | 2009-09-05 07:56:18 +0000 | [diff] [blame] | 1007 | // <type> ::= <function-type> |
| 1008 | // <function-type> ::= F [Y] <bare-function-type> E |
| 1009 | void CXXNameMangler::mangleType(const FunctionProtoType *T) { |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 1010 | Out << 'F'; |
Mike Stump | f5408fe | 2009-05-16 07:57:57 +0000 | [diff] [blame] | 1011 | // FIXME: We don't have enough information in the AST to produce the 'Y' |
| 1012 | // encoding for extern "C" function types. |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 1013 | mangleBareFunctionType(T, /*MangleReturnType=*/true); |
| 1014 | Out << 'E'; |
| 1015 | } |
John McCall | efe6aee | 2009-09-05 07:56:18 +0000 | [diff] [blame] | 1016 | void CXXNameMangler::mangleType(const FunctionNoProtoType *T) { |
Jeffrey Yasskin | 9f61aa9 | 2009-12-12 05:05:38 +0000 | [diff] [blame] | 1017 | llvm_unreachable("Can't mangle K&R function prototypes"); |
John McCall | efe6aee | 2009-09-05 07:56:18 +0000 | [diff] [blame] | 1018 | } |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 1019 | void CXXNameMangler::mangleBareFunctionType(const FunctionType *T, |
| 1020 | bool MangleReturnType) { |
John McCall | efe6aee | 2009-09-05 07:56:18 +0000 | [diff] [blame] | 1021 | // We should never be mangling something without a prototype. |
| 1022 | const FunctionProtoType *Proto = cast<FunctionProtoType>(T); |
| 1023 | |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 1024 | // <bare-function-type> ::= <signature type>+ |
| 1025 | if (MangleReturnType) |
John McCall | efe6aee | 2009-09-05 07:56:18 +0000 | [diff] [blame] | 1026 | mangleType(Proto->getResultType()); |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 1027 | |
Anders Carlsson | c6c91bc | 2009-04-01 00:15:23 +0000 | [diff] [blame] | 1028 | if (Proto->getNumArgs() == 0) { |
| 1029 | Out << 'v'; |
| 1030 | return; |
| 1031 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1032 | |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1033 | for (FunctionProtoType::arg_type_iterator Arg = Proto->arg_type_begin(), |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1034 | ArgEnd = Proto->arg_type_end(); |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 1035 | Arg != ArgEnd; ++Arg) |
| 1036 | mangleType(*Arg); |
Douglas Gregor | 219cc61 | 2009-02-13 01:28:03 +0000 | [diff] [blame] | 1037 | |
| 1038 | // <builtin-type> ::= z # ellipsis |
| 1039 | if (Proto->isVariadic()) |
| 1040 | Out << 'z'; |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 1041 | } |
| 1042 | |
John McCall | efe6aee | 2009-09-05 07:56:18 +0000 | [diff] [blame] | 1043 | // <type> ::= <class-enum-type> |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1044 | // <class-enum-type> ::= <name> |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 1045 | void CXXNameMangler::mangleType(const UnresolvedUsingType *T) { |
| 1046 | mangleName(T->getDecl()); |
| 1047 | } |
| 1048 | |
| 1049 | // <type> ::= <class-enum-type> |
| 1050 | // <class-enum-type> ::= <name> |
John McCall | efe6aee | 2009-09-05 07:56:18 +0000 | [diff] [blame] | 1051 | void CXXNameMangler::mangleType(const EnumType *T) { |
| 1052 | mangleType(static_cast<const TagType*>(T)); |
| 1053 | } |
| 1054 | void CXXNameMangler::mangleType(const RecordType *T) { |
| 1055 | mangleType(static_cast<const TagType*>(T)); |
| 1056 | } |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 1057 | void CXXNameMangler::mangleType(const TagType *T) { |
Eli Friedman | ecb7e93 | 2009-12-11 18:00:57 +0000 | [diff] [blame] | 1058 | mangleName(T->getDecl()); |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 1059 | } |
| 1060 | |
John McCall | efe6aee | 2009-09-05 07:56:18 +0000 | [diff] [blame] | 1061 | // <type> ::= <array-type> |
| 1062 | // <array-type> ::= A <positive dimension number> _ <element type> |
| 1063 | // ::= A [<dimension expression>] _ <element type> |
| 1064 | void CXXNameMangler::mangleType(const ConstantArrayType *T) { |
| 1065 | Out << 'A' << T->getSize() << '_'; |
| 1066 | mangleType(T->getElementType()); |
| 1067 | } |
| 1068 | void CXXNameMangler::mangleType(const VariableArrayType *T) { |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 1069 | Out << 'A'; |
John McCall | efe6aee | 2009-09-05 07:56:18 +0000 | [diff] [blame] | 1070 | mangleExpression(T->getSizeExpr()); |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 1071 | Out << '_'; |
| 1072 | mangleType(T->getElementType()); |
| 1073 | } |
John McCall | efe6aee | 2009-09-05 07:56:18 +0000 | [diff] [blame] | 1074 | void CXXNameMangler::mangleType(const DependentSizedArrayType *T) { |
| 1075 | Out << 'A'; |
| 1076 | mangleExpression(T->getSizeExpr()); |
| 1077 | Out << '_'; |
| 1078 | mangleType(T->getElementType()); |
| 1079 | } |
| 1080 | void CXXNameMangler::mangleType(const IncompleteArrayType *T) { |
| 1081 | Out << 'A' << '_'; |
| 1082 | mangleType(T->getElementType()); |
| 1083 | } |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 1084 | |
John McCall | efe6aee | 2009-09-05 07:56:18 +0000 | [diff] [blame] | 1085 | // <type> ::= <pointer-to-member-type> |
| 1086 | // <pointer-to-member-type> ::= M <class type> <member type> |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 1087 | void CXXNameMangler::mangleType(const MemberPointerType *T) { |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 1088 | Out << 'M'; |
| 1089 | mangleType(QualType(T->getClass(), 0)); |
Anders Carlsson | 0e65001 | 2009-05-17 17:41:20 +0000 | [diff] [blame] | 1090 | QualType PointeeType = T->getPointeeType(); |
| 1091 | if (const FunctionProtoType *FPT = dyn_cast<FunctionProtoType>(PointeeType)) { |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1092 | mangleQualifiers(Qualifiers::fromCVRMask(FPT->getTypeQuals())); |
Anders Carlsson | 0e65001 | 2009-05-17 17:41:20 +0000 | [diff] [blame] | 1093 | mangleType(FPT); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1094 | } else |
Anders Carlsson | 0e65001 | 2009-05-17 17:41:20 +0000 | [diff] [blame] | 1095 | mangleType(PointeeType); |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 1096 | } |
| 1097 | |
John McCall | efe6aee | 2009-09-05 07:56:18 +0000 | [diff] [blame] | 1098 | // <type> ::= <template-param> |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 1099 | void CXXNameMangler::mangleType(const TemplateTypeParmType *T) { |
Anders Carlsson | 0ccdf8d | 2009-09-27 00:38:53 +0000 | [diff] [blame] | 1100 | mangleTemplateParameter(T->getIndex()); |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 1101 | } |
| 1102 | |
John McCall | efe6aee | 2009-09-05 07:56:18 +0000 | [diff] [blame] | 1103 | // FIXME: <type> ::= <template-template-param> <template-args> |
John McCall | efe6aee | 2009-09-05 07:56:18 +0000 | [diff] [blame] | 1104 | |
| 1105 | // <type> ::= P <type> # pointer-to |
| 1106 | void CXXNameMangler::mangleType(const PointerType *T) { |
| 1107 | Out << 'P'; |
| 1108 | mangleType(T->getPointeeType()); |
| 1109 | } |
| 1110 | void CXXNameMangler::mangleType(const ObjCObjectPointerType *T) { |
| 1111 | Out << 'P'; |
| 1112 | mangleType(T->getPointeeType()); |
| 1113 | } |
| 1114 | |
| 1115 | // <type> ::= R <type> # reference-to |
| 1116 | void CXXNameMangler::mangleType(const LValueReferenceType *T) { |
| 1117 | Out << 'R'; |
| 1118 | mangleType(T->getPointeeType()); |
| 1119 | } |
| 1120 | |
| 1121 | // <type> ::= O <type> # rvalue reference-to (C++0x) |
| 1122 | void CXXNameMangler::mangleType(const RValueReferenceType *T) { |
| 1123 | Out << 'O'; |
| 1124 | mangleType(T->getPointeeType()); |
| 1125 | } |
| 1126 | |
| 1127 | // <type> ::= C <type> # complex pair (C 2000) |
| 1128 | void CXXNameMangler::mangleType(const ComplexType *T) { |
| 1129 | Out << 'C'; |
| 1130 | mangleType(T->getElementType()); |
| 1131 | } |
| 1132 | |
| 1133 | // GNU extension: vector types |
Nick Lewycky | 0e5f067 | 2010-03-26 07:18:04 +0000 | [diff] [blame] | 1134 | // <type> ::= <vector-type> |
| 1135 | // <vector-type> ::= Dv <positive dimension number> _ <element type> |
| 1136 | // ::= Dv [<dimension expression>] _ <element type> |
John McCall | efe6aee | 2009-09-05 07:56:18 +0000 | [diff] [blame] | 1137 | void CXXNameMangler::mangleType(const VectorType *T) { |
Nick Lewycky | 0e5f067 | 2010-03-26 07:18:04 +0000 | [diff] [blame] | 1138 | Out << "Dv" << T->getNumElements() << '_'; |
John McCall | efe6aee | 2009-09-05 07:56:18 +0000 | [diff] [blame] | 1139 | mangleType(T->getElementType()); |
| 1140 | } |
| 1141 | void CXXNameMangler::mangleType(const ExtVectorType *T) { |
| 1142 | mangleType(static_cast<const VectorType*>(T)); |
| 1143 | } |
| 1144 | void CXXNameMangler::mangleType(const DependentSizedExtVectorType *T) { |
Nick Lewycky | 0e5f067 | 2010-03-26 07:18:04 +0000 | [diff] [blame] | 1145 | Out << "Dv"; |
| 1146 | mangleExpression(T->getSizeExpr()); |
| 1147 | Out << '_'; |
John McCall | efe6aee | 2009-09-05 07:56:18 +0000 | [diff] [blame] | 1148 | mangleType(T->getElementType()); |
| 1149 | } |
| 1150 | |
Anders Carlsson | a40c5e4 | 2009-03-07 22:03:21 +0000 | [diff] [blame] | 1151 | void CXXNameMangler::mangleType(const ObjCInterfaceType *T) { |
| 1152 | mangleSourceName(T->getDecl()->getIdentifier()); |
| 1153 | } |
| 1154 | |
John McCall | efe6aee | 2009-09-05 07:56:18 +0000 | [diff] [blame] | 1155 | void CXXNameMangler::mangleType(const BlockPointerType *T) { |
Anders Carlsson | f28c687 | 2009-12-23 22:31:44 +0000 | [diff] [blame] | 1156 | Out << "U13block_pointer"; |
| 1157 | mangleType(T->getPointeeType()); |
John McCall | efe6aee | 2009-09-05 07:56:18 +0000 | [diff] [blame] | 1158 | } |
| 1159 | |
John McCall | efe6aee | 2009-09-05 07:56:18 +0000 | [diff] [blame] | 1160 | void CXXNameMangler::mangleType(const TemplateSpecializationType *T) { |
Anders Carlsson | 7624f21 | 2009-09-18 02:42:01 +0000 | [diff] [blame] | 1161 | TemplateDecl *TD = T->getTemplateName().getAsTemplateDecl(); |
| 1162 | assert(TD && "FIXME: Support dependent template names!"); |
Daniel Dunbar | 3c9e463 | 2009-11-21 09:05:47 +0000 | [diff] [blame] | 1163 | |
Anders Carlsson | 7624f21 | 2009-09-18 02:42:01 +0000 | [diff] [blame] | 1164 | mangleName(TD, T->getArgs(), T->getNumArgs()); |
John McCall | efe6aee | 2009-09-05 07:56:18 +0000 | [diff] [blame] | 1165 | } |
| 1166 | |
Douglas Gregor | 4714c12 | 2010-03-31 17:34:00 +0000 | [diff] [blame^] | 1167 | void CXXNameMangler::mangleType(const DependentNameType *T) { |
Anders Carlsson | ae35248 | 2009-09-26 02:26:02 +0000 | [diff] [blame] | 1168 | // Typename types are always nested |
| 1169 | Out << 'N'; |
Rafael Espindola | 9b35b25 | 2010-03-17 04:28:11 +0000 | [diff] [blame] | 1170 | mangleUnresolvedScope(T->getQualifier()); |
Anders Carlsson | ae35248 | 2009-09-26 02:26:02 +0000 | [diff] [blame] | 1171 | mangleSourceName(T->getIdentifier()); |
Anders Carlsson | ae35248 | 2009-09-26 02:26:02 +0000 | [diff] [blame] | 1172 | Out << 'E'; |
John McCall | efe6aee | 2009-09-05 07:56:18 +0000 | [diff] [blame] | 1173 | } |
| 1174 | |
John McCall | ad5e738 | 2010-03-01 23:49:17 +0000 | [diff] [blame] | 1175 | void CXXNameMangler::mangleType(const TypeOfType *T) { |
| 1176 | // FIXME: this is pretty unsatisfactory, but there isn't an obvious |
| 1177 | // "extension with parameters" mangling. |
| 1178 | Out << "u6typeof"; |
| 1179 | } |
| 1180 | |
| 1181 | void CXXNameMangler::mangleType(const TypeOfExprType *T) { |
| 1182 | // FIXME: this is pretty unsatisfactory, but there isn't an obvious |
| 1183 | // "extension with parameters" mangling. |
| 1184 | Out << "u6typeof"; |
| 1185 | } |
| 1186 | |
| 1187 | void CXXNameMangler::mangleType(const DecltypeType *T) { |
| 1188 | Expr *E = T->getUnderlyingExpr(); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 1189 | |
John McCall | ad5e738 | 2010-03-01 23:49:17 +0000 | [diff] [blame] | 1190 | // type ::= Dt <expression> E # decltype of an id-expression |
| 1191 | // # or class member access |
| 1192 | // ::= DT <expression> E # decltype of an expression |
| 1193 | |
| 1194 | // This purports to be an exhaustive list of id-expressions and |
| 1195 | // class member accesses. Note that we do not ignore parentheses; |
| 1196 | // parentheses change the semantics of decltype for these |
| 1197 | // expressions (and cause the mangler to use the other form). |
| 1198 | if (isa<DeclRefExpr>(E) || |
| 1199 | isa<MemberExpr>(E) || |
| 1200 | isa<UnresolvedLookupExpr>(E) || |
| 1201 | isa<DependentScopeDeclRefExpr>(E) || |
| 1202 | isa<CXXDependentScopeMemberExpr>(E) || |
| 1203 | isa<UnresolvedMemberExpr>(E)) |
| 1204 | Out << "Dt"; |
| 1205 | else |
| 1206 | Out << "DT"; |
| 1207 | mangleExpression(E); |
| 1208 | Out << 'E'; |
| 1209 | } |
| 1210 | |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 1211 | void CXXNameMangler::mangleIntegerLiteral(QualType T, |
Anders Carlsson | e170ba7 | 2009-12-14 01:45:37 +0000 | [diff] [blame] | 1212 | const llvm::APSInt &Value) { |
| 1213 | // <expr-primary> ::= L <type> <value number> E # integer literal |
| 1214 | Out << 'L'; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 1215 | |
Anders Carlsson | e170ba7 | 2009-12-14 01:45:37 +0000 | [diff] [blame] | 1216 | mangleType(T); |
| 1217 | if (T->isBooleanType()) { |
| 1218 | // Boolean values are encoded as 0/1. |
| 1219 | Out << (Value.getBoolValue() ? '1' : '0'); |
| 1220 | } else { |
| 1221 | if (Value.isNegative()) |
| 1222 | Out << 'n'; |
| 1223 | Value.abs().print(Out, false); |
| 1224 | } |
| 1225 | Out << 'E'; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 1226 | |
Anders Carlsson | e170ba7 | 2009-12-14 01:45:37 +0000 | [diff] [blame] | 1227 | } |
| 1228 | |
John McCall | 1dd7383 | 2010-02-04 01:42:13 +0000 | [diff] [blame] | 1229 | void CXXNameMangler::mangleCalledExpression(const Expr *E, unsigned Arity) { |
| 1230 | if (E->getType() != getASTContext().OverloadTy) |
| 1231 | mangleExpression(E); |
John McCall | 2f27bf8 | 2010-02-04 02:56:29 +0000 | [diff] [blame] | 1232 | // propagate arity to dependent overloads? |
John McCall | 1dd7383 | 2010-02-04 01:42:13 +0000 | [diff] [blame] | 1233 | |
| 1234 | llvm::PointerIntPair<OverloadExpr*,1> R |
| 1235 | = OverloadExpr::find(const_cast<Expr*>(E)); |
| 1236 | if (R.getInt()) |
| 1237 | Out << "an"; // & |
| 1238 | const OverloadExpr *Ovl = R.getPointer(); |
John McCall | 2f27bf8 | 2010-02-04 02:56:29 +0000 | [diff] [blame] | 1239 | if (const UnresolvedMemberExpr *ME = dyn_cast<UnresolvedMemberExpr>(Ovl)) { |
| 1240 | mangleMemberExpr(ME->getBase(), ME->isArrow(), ME->getQualifier(), |
| 1241 | ME->getMemberName(), Arity); |
| 1242 | return; |
| 1243 | } |
John McCall | 1dd7383 | 2010-02-04 01:42:13 +0000 | [diff] [blame] | 1244 | |
| 1245 | mangleUnresolvedName(Ovl->getQualifier(), Ovl->getName(), Arity); |
| 1246 | } |
| 1247 | |
John McCall | 2f27bf8 | 2010-02-04 02:56:29 +0000 | [diff] [blame] | 1248 | /// Mangles a member expression. Implicit accesses are not handled, |
| 1249 | /// but that should be okay, because you shouldn't be able to |
| 1250 | /// make an implicit access in a function template declaration. |
John McCall | 2f27bf8 | 2010-02-04 02:56:29 +0000 | [diff] [blame] | 1251 | void CXXNameMangler::mangleMemberExpr(const Expr *Base, |
| 1252 | bool IsArrow, |
| 1253 | NestedNameSpecifier *Qualifier, |
| 1254 | DeclarationName Member, |
| 1255 | unsigned Arity) { |
John McCall | e1e342f | 2010-03-01 19:12:25 +0000 | [diff] [blame] | 1256 | // gcc-4.4 uses 'dt' for dot expressions, which is reasonable. |
| 1257 | // OTOH, gcc also mangles the name as an expression. |
| 1258 | Out << (IsArrow ? "pt" : "dt"); |
John McCall | 2f27bf8 | 2010-02-04 02:56:29 +0000 | [diff] [blame] | 1259 | mangleExpression(Base); |
| 1260 | mangleUnresolvedName(Qualifier, Member, Arity); |
| 1261 | } |
| 1262 | |
Anders Carlsson | d553f8c | 2009-09-21 01:21:10 +0000 | [diff] [blame] | 1263 | void CXXNameMangler::mangleExpression(const Expr *E) { |
| 1264 | // <expression> ::= <unary operator-name> <expression> |
John McCall | 09cc141 | 2010-02-03 00:55:45 +0000 | [diff] [blame] | 1265 | // ::= <binary operator-name> <expression> <expression> |
| 1266 | // ::= <trinary operator-name> <expression> <expression> <expression> |
| 1267 | // ::= cl <expression>* E # call |
Anders Carlsson | d553f8c | 2009-09-21 01:21:10 +0000 | [diff] [blame] | 1268 | // ::= cv <type> expression # conversion with one argument |
| 1269 | // ::= cv <type> _ <expression>* E # conversion with a different number of arguments |
John McCall | 09cc141 | 2010-02-03 00:55:45 +0000 | [diff] [blame] | 1270 | // ::= st <type> # sizeof (a type) |
Anders Carlsson | d553f8c | 2009-09-21 01:21:10 +0000 | [diff] [blame] | 1271 | // ::= at <type> # alignof (a type) |
| 1272 | // ::= <template-param> |
| 1273 | // ::= <function-param> |
| 1274 | // ::= sr <type> <unqualified-name> # dependent name |
| 1275 | // ::= sr <type> <unqualified-name> <template-args> # dependent template-id |
| 1276 | // ::= sZ <template-param> # size of a parameter pack |
John McCall | 09cc141 | 2010-02-03 00:55:45 +0000 | [diff] [blame] | 1277 | // ::= <expr-primary> |
John McCall | 1dd7383 | 2010-02-04 01:42:13 +0000 | [diff] [blame] | 1278 | // <expr-primary> ::= L <type> <value number> E # integer literal |
| 1279 | // ::= L <type <value float> E # floating literal |
| 1280 | // ::= L <mangled-name> E # external name |
Anders Carlsson | d553f8c | 2009-09-21 01:21:10 +0000 | [diff] [blame] | 1281 | switch (E->getStmtClass()) { |
John McCall | 09cc141 | 2010-02-03 00:55:45 +0000 | [diff] [blame] | 1282 | default: |
| 1283 | llvm_unreachable("unexpected statement kind"); |
| 1284 | break; |
| 1285 | |
John McCall | 1dd7383 | 2010-02-04 01:42:13 +0000 | [diff] [blame] | 1286 | case Expr::CallExprClass: { |
| 1287 | const CallExpr *CE = cast<CallExpr>(E); |
| 1288 | Out << "cl"; |
| 1289 | mangleCalledExpression(CE->getCallee(), CE->getNumArgs()); |
| 1290 | for (unsigned I = 0, N = CE->getNumArgs(); I != N; ++I) |
| 1291 | mangleExpression(CE->getArg(I)); |
| 1292 | Out << "E"; |
John McCall | 09cc141 | 2010-02-03 00:55:45 +0000 | [diff] [blame] | 1293 | break; |
John McCall | 1dd7383 | 2010-02-04 01:42:13 +0000 | [diff] [blame] | 1294 | } |
John McCall | 09cc141 | 2010-02-03 00:55:45 +0000 | [diff] [blame] | 1295 | |
John McCall | 2f27bf8 | 2010-02-04 02:56:29 +0000 | [diff] [blame] | 1296 | case Expr::MemberExprClass: { |
| 1297 | const MemberExpr *ME = cast<MemberExpr>(E); |
| 1298 | mangleMemberExpr(ME->getBase(), ME->isArrow(), |
| 1299 | ME->getQualifier(), ME->getMemberDecl()->getDeclName(), |
| 1300 | UnknownArity); |
| 1301 | break; |
| 1302 | } |
| 1303 | |
| 1304 | case Expr::UnresolvedMemberExprClass: { |
| 1305 | const UnresolvedMemberExpr *ME = cast<UnresolvedMemberExpr>(E); |
| 1306 | mangleMemberExpr(ME->getBase(), ME->isArrow(), |
| 1307 | ME->getQualifier(), ME->getMemberName(), |
| 1308 | UnknownArity); |
| 1309 | break; |
| 1310 | } |
| 1311 | |
| 1312 | case Expr::CXXDependentScopeMemberExprClass: { |
| 1313 | const CXXDependentScopeMemberExpr *ME |
| 1314 | = cast<CXXDependentScopeMemberExpr>(E); |
| 1315 | mangleMemberExpr(ME->getBase(), ME->isArrow(), |
| 1316 | ME->getQualifier(), ME->getMember(), |
| 1317 | UnknownArity); |
| 1318 | break; |
| 1319 | } |
| 1320 | |
John McCall | 1dd7383 | 2010-02-04 01:42:13 +0000 | [diff] [blame] | 1321 | case Expr::UnresolvedLookupExprClass: { |
John McCall | a3218e7 | 2010-02-04 01:48:38 +0000 | [diff] [blame] | 1322 | // The ABI doesn't cover how to mangle overload sets, so we mangle |
| 1323 | // using something as close as possible to the original lookup |
| 1324 | // expression. |
John McCall | 1dd7383 | 2010-02-04 01:42:13 +0000 | [diff] [blame] | 1325 | const UnresolvedLookupExpr *ULE = cast<UnresolvedLookupExpr>(E); |
| 1326 | mangleUnresolvedName(ULE->getQualifier(), ULE->getName(), UnknownArity); |
| 1327 | break; |
| 1328 | } |
| 1329 | |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 1330 | case Expr::CXXUnresolvedConstructExprClass: { |
John McCall | 1dd7383 | 2010-02-04 01:42:13 +0000 | [diff] [blame] | 1331 | const CXXUnresolvedConstructExpr *CE = cast<CXXUnresolvedConstructExpr>(E); |
| 1332 | unsigned N = CE->arg_size(); |
| 1333 | |
| 1334 | Out << "cv"; |
| 1335 | mangleType(CE->getType()); |
| 1336 | if (N != 1) Out << "_"; |
| 1337 | for (unsigned I = 0; I != N; ++I) mangleExpression(CE->getArg(I)); |
| 1338 | if (N != 1) Out << "E"; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 1339 | break; |
John McCall | 1dd7383 | 2010-02-04 01:42:13 +0000 | [diff] [blame] | 1340 | } |
John McCall | 09cc141 | 2010-02-03 00:55:45 +0000 | [diff] [blame] | 1341 | |
John McCall | 1dd7383 | 2010-02-04 01:42:13 +0000 | [diff] [blame] | 1342 | case Expr::CXXTemporaryObjectExprClass: |
| 1343 | case Expr::CXXConstructExprClass: { |
| 1344 | const CXXConstructExpr *CE = cast<CXXConstructExpr>(E); |
| 1345 | unsigned N = CE->getNumArgs(); |
| 1346 | |
| 1347 | Out << "cv"; |
| 1348 | mangleType(CE->getType()); |
| 1349 | if (N != 1) Out << "_"; |
| 1350 | for (unsigned I = 0; I != N; ++I) mangleExpression(CE->getArg(I)); |
| 1351 | if (N != 1) Out << "E"; |
John McCall | 09cc141 | 2010-02-03 00:55:45 +0000 | [diff] [blame] | 1352 | break; |
John McCall | 1dd7383 | 2010-02-04 01:42:13 +0000 | [diff] [blame] | 1353 | } |
| 1354 | |
| 1355 | case Expr::SizeOfAlignOfExprClass: { |
| 1356 | const SizeOfAlignOfExpr *SAE = cast<SizeOfAlignOfExpr>(E); |
| 1357 | if (SAE->isSizeOf()) Out << "s"; |
| 1358 | else Out << "a"; |
| 1359 | if (SAE->isArgumentType()) { |
| 1360 | Out << "t"; |
| 1361 | mangleType(SAE->getArgumentType()); |
| 1362 | } else { |
| 1363 | Out << "z"; |
| 1364 | mangleExpression(SAE->getArgumentExpr()); |
| 1365 | } |
| 1366 | break; |
| 1367 | } |
Anders Carlsson | a769408 | 2009-11-06 02:50:19 +0000 | [diff] [blame] | 1368 | |
Anders Carlsson | e170ba7 | 2009-12-14 01:45:37 +0000 | [diff] [blame] | 1369 | case Expr::UnaryOperatorClass: { |
| 1370 | const UnaryOperator *UO = cast<UnaryOperator>(E); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 1371 | mangleOperatorName(UnaryOperator::getOverloadedOperator(UO->getOpcode()), |
Anders Carlsson | e170ba7 | 2009-12-14 01:45:37 +0000 | [diff] [blame] | 1372 | /*Arity=*/1); |
| 1373 | mangleExpression(UO->getSubExpr()); |
| 1374 | break; |
| 1375 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 1376 | |
Anders Carlsson | e170ba7 | 2009-12-14 01:45:37 +0000 | [diff] [blame] | 1377 | case Expr::BinaryOperatorClass: { |
| 1378 | const BinaryOperator *BO = cast<BinaryOperator>(E); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 1379 | mangleOperatorName(BinaryOperator::getOverloadedOperator(BO->getOpcode()), |
Anders Carlsson | e170ba7 | 2009-12-14 01:45:37 +0000 | [diff] [blame] | 1380 | /*Arity=*/2); |
| 1381 | mangleExpression(BO->getLHS()); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 1382 | mangleExpression(BO->getRHS()); |
Anders Carlsson | e170ba7 | 2009-12-14 01:45:37 +0000 | [diff] [blame] | 1383 | break; |
John McCall | 2f27bf8 | 2010-02-04 02:56:29 +0000 | [diff] [blame] | 1384 | } |
Anders Carlsson | e170ba7 | 2009-12-14 01:45:37 +0000 | [diff] [blame] | 1385 | |
| 1386 | case Expr::ConditionalOperatorClass: { |
| 1387 | const ConditionalOperator *CO = cast<ConditionalOperator>(E); |
| 1388 | mangleOperatorName(OO_Conditional, /*Arity=*/3); |
| 1389 | mangleExpression(CO->getCond()); |
| 1390 | mangleExpression(CO->getLHS()); |
| 1391 | mangleExpression(CO->getRHS()); |
| 1392 | break; |
| 1393 | } |
| 1394 | |
Douglas Gregor | 46287c7 | 2010-01-29 16:37:09 +0000 | [diff] [blame] | 1395 | case Expr::ImplicitCastExprClass: { |
| 1396 | mangleExpression(cast<ImplicitCastExpr>(E)->getSubExpr()); |
| 1397 | break; |
| 1398 | } |
| 1399 | |
| 1400 | case Expr::CStyleCastExprClass: |
| 1401 | case Expr::CXXStaticCastExprClass: |
| 1402 | case Expr::CXXDynamicCastExprClass: |
| 1403 | case Expr::CXXReinterpretCastExprClass: |
| 1404 | case Expr::CXXConstCastExprClass: |
| 1405 | case Expr::CXXFunctionalCastExprClass: { |
| 1406 | const ExplicitCastExpr *ECE = cast<ExplicitCastExpr>(E); |
| 1407 | Out << "cv"; |
| 1408 | mangleType(ECE->getType()); |
| 1409 | mangleExpression(ECE->getSubExpr()); |
| 1410 | break; |
| 1411 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 1412 | |
Anders Carlsson | 58040a5 | 2009-12-16 05:48:46 +0000 | [diff] [blame] | 1413 | case Expr::CXXOperatorCallExprClass: { |
| 1414 | const CXXOperatorCallExpr *CE = cast<CXXOperatorCallExpr>(E); |
| 1415 | unsigned NumArgs = CE->getNumArgs(); |
| 1416 | mangleOperatorName(CE->getOperator(), /*Arity=*/NumArgs); |
| 1417 | // Mangle the arguments. |
| 1418 | for (unsigned i = 0; i != NumArgs; ++i) |
| 1419 | mangleExpression(CE->getArg(i)); |
| 1420 | break; |
| 1421 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 1422 | |
Anders Carlsson | a769408 | 2009-11-06 02:50:19 +0000 | [diff] [blame] | 1423 | case Expr::ParenExprClass: |
| 1424 | mangleExpression(cast<ParenExpr>(E)->getSubExpr()); |
| 1425 | break; |
| 1426 | |
Anders Carlsson | d553f8c | 2009-09-21 01:21:10 +0000 | [diff] [blame] | 1427 | case Expr::DeclRefExprClass: { |
Douglas Gregor | 5ed1bc3 | 2010-02-28 21:40:32 +0000 | [diff] [blame] | 1428 | const NamedDecl *D = cast<DeclRefExpr>(E)->getDecl(); |
Daniel Dunbar | 3c9e463 | 2009-11-21 09:05:47 +0000 | [diff] [blame] | 1429 | |
Anders Carlsson | d553f8c | 2009-09-21 01:21:10 +0000 | [diff] [blame] | 1430 | switch (D->getKind()) { |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 1431 | default: |
Douglas Gregor | 5ed1bc3 | 2010-02-28 21:40:32 +0000 | [diff] [blame] | 1432 | // <expr-primary> ::= L <mangled-name> E # external name |
| 1433 | Out << 'L'; |
| 1434 | mangle(D, "_Z"); |
| 1435 | Out << 'E'; |
| 1436 | break; |
| 1437 | |
Anders Carlsson | d553f8c | 2009-09-21 01:21:10 +0000 | [diff] [blame] | 1438 | case Decl::NonTypeTemplateParm: { |
| 1439 | const NonTypeTemplateParmDecl *PD = cast<NonTypeTemplateParmDecl>(D); |
Anders Carlsson | 0ccdf8d | 2009-09-27 00:38:53 +0000 | [diff] [blame] | 1440 | mangleTemplateParameter(PD->getIndex()); |
Anders Carlsson | d553f8c | 2009-09-21 01:21:10 +0000 | [diff] [blame] | 1441 | break; |
| 1442 | } |
| 1443 | |
| 1444 | } |
Daniel Dunbar | 3c9e463 | 2009-11-21 09:05:47 +0000 | [diff] [blame] | 1445 | |
Anders Carlsson | 50755b0 | 2009-09-27 20:11:34 +0000 | [diff] [blame] | 1446 | break; |
Anders Carlsson | d553f8c | 2009-09-21 01:21:10 +0000 | [diff] [blame] | 1447 | } |
Daniel Dunbar | 3c9e463 | 2009-11-21 09:05:47 +0000 | [diff] [blame] | 1448 | |
John McCall | 865d447 | 2009-11-19 22:55:06 +0000 | [diff] [blame] | 1449 | case Expr::DependentScopeDeclRefExprClass: { |
| 1450 | const DependentScopeDeclRefExpr *DRE = cast<DependentScopeDeclRefExpr>(E); |
Douglas Gregor | 4b2ccfc | 2010-02-28 22:05:49 +0000 | [diff] [blame] | 1451 | NestedNameSpecifier *NNS = DRE->getQualifier(); |
| 1452 | const Type *QTy = NNS->getAsType(); |
| 1453 | |
| 1454 | // When we're dealing with a nested-name-specifier that has just a |
| 1455 | // dependent identifier in it, mangle that as a typename. FIXME: |
| 1456 | // It isn't clear that we ever actually want to have such a |
| 1457 | // nested-name-specifier; why not just represent it as a typename type? |
| 1458 | if (!QTy && NNS->getAsIdentifier() && NNS->getPrefix()) { |
Douglas Gregor | 4714c12 | 2010-03-31 17:34:00 +0000 | [diff] [blame^] | 1459 | QTy = getASTContext().getDependentNameType(NNS->getPrefix(), |
Douglas Gregor | 4b2ccfc | 2010-02-28 22:05:49 +0000 | [diff] [blame] | 1460 | NNS->getAsIdentifier()) |
| 1461 | .getTypePtr(); |
| 1462 | } |
Anders Carlsson | 50755b0 | 2009-09-27 20:11:34 +0000 | [diff] [blame] | 1463 | assert(QTy && "Qualifier was not type!"); |
| 1464 | |
| 1465 | // ::= sr <type> <unqualified-name> # dependent name |
| 1466 | Out << "sr"; |
| 1467 | mangleType(QualType(QTy, 0)); |
Daniel Dunbar | 3c9e463 | 2009-11-21 09:05:47 +0000 | [diff] [blame] | 1468 | |
Anders Carlsson | 50755b0 | 2009-09-27 20:11:34 +0000 | [diff] [blame] | 1469 | assert(DRE->getDeclName().getNameKind() == DeclarationName::Identifier && |
| 1470 | "Unhandled decl name kind!"); |
| 1471 | mangleSourceName(DRE->getDeclName().getAsIdentifierInfo()); |
Daniel Dunbar | 3c9e463 | 2009-11-21 09:05:47 +0000 | [diff] [blame] | 1472 | |
Anders Carlsson | 50755b0 | 2009-09-27 20:11:34 +0000 | [diff] [blame] | 1473 | break; |
| 1474 | } |
| 1475 | |
John McCall | 1dd7383 | 2010-02-04 01:42:13 +0000 | [diff] [blame] | 1476 | case Expr::FloatingLiteralClass: { |
| 1477 | const FloatingLiteral *FL = cast<FloatingLiteral>(E); |
| 1478 | Out << "L"; |
| 1479 | mangleType(FL->getType()); |
| 1480 | |
| 1481 | // TODO: avoid this copy with careful stream management. |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 1482 | llvm::SmallString<20> Buffer; |
John McCall | 1dd7383 | 2010-02-04 01:42:13 +0000 | [diff] [blame] | 1483 | FL->getValue().bitcastToAPInt().toString(Buffer, 16, false); |
| 1484 | Out.write(Buffer.data(), Buffer.size()); |
| 1485 | |
| 1486 | Out << "E"; |
| 1487 | break; |
| 1488 | } |
| 1489 | |
Anders Carlsson | e170ba7 | 2009-12-14 01:45:37 +0000 | [diff] [blame] | 1490 | case Expr::IntegerLiteralClass: |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 1491 | mangleIntegerLiteral(E->getType(), |
Anders Carlsson | e170ba7 | 2009-12-14 01:45:37 +0000 | [diff] [blame] | 1492 | llvm::APSInt(cast<IntegerLiteral>(E)->getValue())); |
| 1493 | break; |
| 1494 | |
Anders Carlsson | d553f8c | 2009-09-21 01:21:10 +0000 | [diff] [blame] | 1495 | } |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 1496 | } |
| 1497 | |
John McCall | efe6aee | 2009-09-05 07:56:18 +0000 | [diff] [blame] | 1498 | // FIXME: <type> ::= G <type> # imaginary (C 2000) |
| 1499 | // FIXME: <type> ::= U <source-name> <type> # vendor extended type qualifier |
| 1500 | |
Anders Carlsson | 3ac86b5 | 2009-04-15 05:36:58 +0000 | [diff] [blame] | 1501 | void CXXNameMangler::mangleCXXCtorType(CXXCtorType T) { |
| 1502 | // <ctor-dtor-name> ::= C1 # complete object constructor |
| 1503 | // ::= C2 # base object constructor |
| 1504 | // ::= C3 # complete object allocating constructor |
| 1505 | // |
| 1506 | switch (T) { |
| 1507 | case Ctor_Complete: |
| 1508 | Out << "C1"; |
| 1509 | break; |
| 1510 | case Ctor_Base: |
| 1511 | Out << "C2"; |
| 1512 | break; |
| 1513 | case Ctor_CompleteAllocating: |
| 1514 | Out << "C3"; |
| 1515 | break; |
| 1516 | } |
| 1517 | } |
| 1518 | |
Anders Carlsson | 27ae536 | 2009-04-17 01:58:57 +0000 | [diff] [blame] | 1519 | void CXXNameMangler::mangleCXXDtorType(CXXDtorType T) { |
| 1520 | // <ctor-dtor-name> ::= D0 # deleting destructor |
| 1521 | // ::= D1 # complete object destructor |
| 1522 | // ::= D2 # base object destructor |
| 1523 | // |
| 1524 | switch (T) { |
| 1525 | case Dtor_Deleting: |
| 1526 | Out << "D0"; |
| 1527 | break; |
| 1528 | case Dtor_Complete: |
| 1529 | Out << "D1"; |
| 1530 | break; |
| 1531 | case Dtor_Base: |
| 1532 | Out << "D2"; |
| 1533 | break; |
| 1534 | } |
| 1535 | } |
| 1536 | |
Rafael Espindola | d980072 | 2010-03-11 14:07:00 +0000 | [diff] [blame] | 1537 | void CXXNameMangler::mangleTemplateArgs(const TemplateParameterList &PL, |
| 1538 | const TemplateArgumentList &AL) { |
Anders Carlsson | 7a0ba87 | 2009-05-15 16:09:15 +0000 | [diff] [blame] | 1539 | // <template-args> ::= I <template-arg>+ E |
| 1540 | Out << "I"; |
Rafael Espindola | d980072 | 2010-03-11 14:07:00 +0000 | [diff] [blame] | 1541 | for (unsigned i = 0, e = AL.size(); i != e; ++i) |
| 1542 | mangleTemplateArg(PL.getParam(i), AL[i]); |
Anders Carlsson | 7a0ba87 | 2009-05-15 16:09:15 +0000 | [diff] [blame] | 1543 | Out << "E"; |
| 1544 | } |
| 1545 | |
Rafael Espindola | d980072 | 2010-03-11 14:07:00 +0000 | [diff] [blame] | 1546 | void CXXNameMangler::mangleTemplateArgs(const TemplateParameterList &PL, |
| 1547 | const TemplateArgument *TemplateArgs, |
Anders Carlsson | 7624f21 | 2009-09-18 02:42:01 +0000 | [diff] [blame] | 1548 | unsigned NumTemplateArgs) { |
| 1549 | // <template-args> ::= I <template-arg>+ E |
| 1550 | Out << "I"; |
Daniel Dunbar | 7e0c195 | 2009-11-21 09:17:15 +0000 | [diff] [blame] | 1551 | for (unsigned i = 0; i != NumTemplateArgs; ++i) |
Rafael Espindola | d980072 | 2010-03-11 14:07:00 +0000 | [diff] [blame] | 1552 | mangleTemplateArg(PL.getParam(i), TemplateArgs[i]); |
Anders Carlsson | 7624f21 | 2009-09-18 02:42:01 +0000 | [diff] [blame] | 1553 | Out << "E"; |
| 1554 | } |
| 1555 | |
Rafael Espindola | d980072 | 2010-03-11 14:07:00 +0000 | [diff] [blame] | 1556 | void CXXNameMangler::mangleTemplateArg(const NamedDecl *P, |
| 1557 | const TemplateArgument &A) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1558 | // <template-arg> ::= <type> # type or template |
Anders Carlsson | 7a0ba87 | 2009-05-15 16:09:15 +0000 | [diff] [blame] | 1559 | // ::= X <expression> E # expression |
| 1560 | // ::= <expr-primary> # simple expressions |
| 1561 | // ::= I <template-arg>* E # argument pack |
| 1562 | // ::= sp <expression> # pack expansion of (C++0x) |
| 1563 | switch (A.getKind()) { |
| 1564 | default: |
| 1565 | assert(0 && "Unknown template argument kind!"); |
| 1566 | case TemplateArgument::Type: |
| 1567 | mangleType(A.getAsType()); |
| 1568 | break; |
Anders Carlsson | 9e85c74 | 2009-12-23 19:30:55 +0000 | [diff] [blame] | 1569 | case TemplateArgument::Template: |
Douglas Gregor | 2d565b3 | 2010-02-06 01:09:36 +0000 | [diff] [blame] | 1570 | assert(A.getAsTemplate().getAsTemplateDecl() && |
| 1571 | "FIXME: Support dependent template names"); |
Anders Carlsson | 9e85c74 | 2009-12-23 19:30:55 +0000 | [diff] [blame] | 1572 | mangleName(A.getAsTemplate().getAsTemplateDecl()); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 1573 | break; |
Anders Carlsson | d553f8c | 2009-09-21 01:21:10 +0000 | [diff] [blame] | 1574 | case TemplateArgument::Expression: |
| 1575 | Out << 'X'; |
| 1576 | mangleExpression(A.getAsExpr()); |
| 1577 | Out << 'E'; |
| 1578 | break; |
Anders Carlsson | e170ba7 | 2009-12-14 01:45:37 +0000 | [diff] [blame] | 1579 | case TemplateArgument::Integral: |
| 1580 | mangleIntegerLiteral(A.getIntegralType(), *A.getAsIntegral()); |
Anders Carlsson | 7a0ba87 | 2009-05-15 16:09:15 +0000 | [diff] [blame] | 1581 | break; |
Daniel Dunbar | 7e0c195 | 2009-11-21 09:17:15 +0000 | [diff] [blame] | 1582 | case TemplateArgument::Declaration: { |
| 1583 | // <expr-primary> ::= L <mangled-name> E # external name |
| 1584 | |
Rafael Espindola | d980072 | 2010-03-11 14:07:00 +0000 | [diff] [blame] | 1585 | // Clang produces AST's where pointer-to-member-function expressions |
Daniel Dunbar | 7e0c195 | 2009-11-21 09:17:15 +0000 | [diff] [blame] | 1586 | // and pointer-to-function expressions are represented as a declaration not |
Rafael Espindola | d980072 | 2010-03-11 14:07:00 +0000 | [diff] [blame] | 1587 | // an expression. We compensate for it here to produce the correct mangling. |
| 1588 | NamedDecl *D = cast<NamedDecl>(A.getAsDecl()); |
| 1589 | const NonTypeTemplateParmDecl *Parameter = cast<NonTypeTemplateParmDecl>(P); |
| 1590 | bool compensateMangling = D->isCXXClassMember() && |
| 1591 | !Parameter->getType()->isReferenceType(); |
| 1592 | if (compensateMangling) { |
| 1593 | Out << 'X'; |
| 1594 | mangleOperatorName(OO_Amp, 1); |
| 1595 | } |
| 1596 | |
Daniel Dunbar | 7e0c195 | 2009-11-21 09:17:15 +0000 | [diff] [blame] | 1597 | Out << 'L'; |
| 1598 | // References to external entities use the mangled name; if the name would |
| 1599 | // not normally be manged then mangle it as unqualified. |
| 1600 | // |
| 1601 | // FIXME: The ABI specifies that external names here should have _Z, but |
| 1602 | // gcc leaves this off. |
Rafael Espindola | d980072 | 2010-03-11 14:07:00 +0000 | [diff] [blame] | 1603 | if (compensateMangling) |
| 1604 | mangle(D, "_Z"); |
| 1605 | else |
| 1606 | mangle(D, "Z"); |
Daniel Dunbar | 7e0c195 | 2009-11-21 09:17:15 +0000 | [diff] [blame] | 1607 | Out << 'E'; |
Rafael Espindola | d980072 | 2010-03-11 14:07:00 +0000 | [diff] [blame] | 1608 | |
| 1609 | if (compensateMangling) |
| 1610 | Out << 'E'; |
| 1611 | |
Daniel Dunbar | 7e0c195 | 2009-11-21 09:17:15 +0000 | [diff] [blame] | 1612 | break; |
| 1613 | } |
| 1614 | } |
Anders Carlsson | 7a0ba87 | 2009-05-15 16:09:15 +0000 | [diff] [blame] | 1615 | } |
| 1616 | |
Anders Carlsson | 0ccdf8d | 2009-09-27 00:38:53 +0000 | [diff] [blame] | 1617 | void CXXNameMangler::mangleTemplateParameter(unsigned Index) { |
| 1618 | // <template-param> ::= T_ # first template parameter |
| 1619 | // ::= T <parameter-2 non-negative number> _ |
| 1620 | if (Index == 0) |
| 1621 | Out << "T_"; |
| 1622 | else |
| 1623 | Out << 'T' << (Index - 1) << '_'; |
| 1624 | } |
| 1625 | |
Anders Carlsson | 7696737 | 2009-09-17 00:43:46 +0000 | [diff] [blame] | 1626 | // <substitution> ::= S <seq-id> _ |
| 1627 | // ::= S_ |
Anders Carlsson | 6862fc7 | 2009-09-17 04:16:28 +0000 | [diff] [blame] | 1628 | bool CXXNameMangler::mangleSubstitution(const NamedDecl *ND) { |
Anders Carlsson | e7c8cb6 | 2009-09-26 20:53:44 +0000 | [diff] [blame] | 1629 | // Try one of the standard substitutions first. |
| 1630 | if (mangleStandardSubstitution(ND)) |
| 1631 | return true; |
Daniel Dunbar | 3c9e463 | 2009-11-21 09:05:47 +0000 | [diff] [blame] | 1632 | |
Anders Carlsson | 433d137 | 2009-11-07 04:26:04 +0000 | [diff] [blame] | 1633 | ND = cast<NamedDecl>(ND->getCanonicalDecl()); |
Anders Carlsson | 6862fc7 | 2009-09-17 04:16:28 +0000 | [diff] [blame] | 1634 | return mangleSubstitution(reinterpret_cast<uintptr_t>(ND)); |
| 1635 | } |
| 1636 | |
Anders Carlsson | 7696737 | 2009-09-17 00:43:46 +0000 | [diff] [blame] | 1637 | bool CXXNameMangler::mangleSubstitution(QualType T) { |
Anders Carlsson | d99edc4 | 2009-09-26 03:55:37 +0000 | [diff] [blame] | 1638 | if (!T.getCVRQualifiers()) { |
| 1639 | if (const RecordType *RT = T->getAs<RecordType>()) |
| 1640 | return mangleSubstitution(RT->getDecl()); |
| 1641 | } |
Daniel Dunbar | 3c9e463 | 2009-11-21 09:05:47 +0000 | [diff] [blame] | 1642 | |
Anders Carlsson | 7696737 | 2009-09-17 00:43:46 +0000 | [diff] [blame] | 1643 | uintptr_t TypePtr = reinterpret_cast<uintptr_t>(T.getAsOpaquePtr()); |
| 1644 | |
Anders Carlsson | d3a932a | 2009-09-17 03:53:28 +0000 | [diff] [blame] | 1645 | return mangleSubstitution(TypePtr); |
| 1646 | } |
| 1647 | |
| 1648 | bool CXXNameMangler::mangleSubstitution(uintptr_t Ptr) { |
Daniel Dunbar | 3c9e463 | 2009-11-21 09:05:47 +0000 | [diff] [blame] | 1649 | llvm::DenseMap<uintptr_t, unsigned>::iterator I = |
Anders Carlsson | d3a932a | 2009-09-17 03:53:28 +0000 | [diff] [blame] | 1650 | Substitutions.find(Ptr); |
Anders Carlsson | 7696737 | 2009-09-17 00:43:46 +0000 | [diff] [blame] | 1651 | if (I == Substitutions.end()) |
| 1652 | return false; |
Daniel Dunbar | 3c9e463 | 2009-11-21 09:05:47 +0000 | [diff] [blame] | 1653 | |
Anders Carlsson | 7696737 | 2009-09-17 00:43:46 +0000 | [diff] [blame] | 1654 | unsigned SeqID = I->second; |
| 1655 | if (SeqID == 0) |
| 1656 | Out << "S_"; |
| 1657 | else { |
| 1658 | SeqID--; |
Daniel Dunbar | 3c9e463 | 2009-11-21 09:05:47 +0000 | [diff] [blame] | 1659 | |
Anders Carlsson | 7696737 | 2009-09-17 00:43:46 +0000 | [diff] [blame] | 1660 | // <seq-id> is encoded in base-36, using digits and upper case letters. |
| 1661 | char Buffer[10]; |
| 1662 | char *BufferPtr = Buffer + 9; |
Daniel Dunbar | 3c9e463 | 2009-11-21 09:05:47 +0000 | [diff] [blame] | 1663 | |
Anders Carlsson | 7696737 | 2009-09-17 00:43:46 +0000 | [diff] [blame] | 1664 | *BufferPtr = 0; |
| 1665 | if (SeqID == 0) *--BufferPtr = '0'; |
Daniel Dunbar | 3c9e463 | 2009-11-21 09:05:47 +0000 | [diff] [blame] | 1666 | |
Anders Carlsson | 7696737 | 2009-09-17 00:43:46 +0000 | [diff] [blame] | 1667 | while (SeqID) { |
| 1668 | assert(BufferPtr > Buffer && "Buffer overflow!"); |
Daniel Dunbar | 3c9e463 | 2009-11-21 09:05:47 +0000 | [diff] [blame] | 1669 | |
Anders Carlsson | 7696737 | 2009-09-17 00:43:46 +0000 | [diff] [blame] | 1670 | unsigned char c = static_cast<unsigned char>(SeqID) % 36; |
Daniel Dunbar | 3c9e463 | 2009-11-21 09:05:47 +0000 | [diff] [blame] | 1671 | |
Anders Carlsson | 7696737 | 2009-09-17 00:43:46 +0000 | [diff] [blame] | 1672 | *--BufferPtr = (c < 10 ? '0' + c : 'A' + c - 10); |
| 1673 | SeqID /= 36; |
| 1674 | } |
Daniel Dunbar | 3c9e463 | 2009-11-21 09:05:47 +0000 | [diff] [blame] | 1675 | |
Anders Carlsson | 7696737 | 2009-09-17 00:43:46 +0000 | [diff] [blame] | 1676 | Out << 'S' << BufferPtr << '_'; |
| 1677 | } |
Daniel Dunbar | 3c9e463 | 2009-11-21 09:05:47 +0000 | [diff] [blame] | 1678 | |
Anders Carlsson | 7696737 | 2009-09-17 00:43:46 +0000 | [diff] [blame] | 1679 | return true; |
| 1680 | } |
| 1681 | |
Anders Carlsson | f514b54 | 2009-09-27 00:12:57 +0000 | [diff] [blame] | 1682 | static bool isCharType(QualType T) { |
| 1683 | if (T.isNull()) |
| 1684 | return false; |
Daniel Dunbar | 3c9e463 | 2009-11-21 09:05:47 +0000 | [diff] [blame] | 1685 | |
Anders Carlsson | f514b54 | 2009-09-27 00:12:57 +0000 | [diff] [blame] | 1686 | return T->isSpecificBuiltinType(BuiltinType::Char_S) || |
| 1687 | T->isSpecificBuiltinType(BuiltinType::Char_U); |
| 1688 | } |
| 1689 | |
Daniel Dunbar | 3c9e463 | 2009-11-21 09:05:47 +0000 | [diff] [blame] | 1690 | /// isCharSpecialization - Returns whether a given type is a template |
Anders Carlsson | f514b54 | 2009-09-27 00:12:57 +0000 | [diff] [blame] | 1691 | /// specialization of a given name with a single argument of type char. |
| 1692 | static bool isCharSpecialization(QualType T, const char *Name) { |
| 1693 | if (T.isNull()) |
| 1694 | return false; |
Daniel Dunbar | 3c9e463 | 2009-11-21 09:05:47 +0000 | [diff] [blame] | 1695 | |
Anders Carlsson | f514b54 | 2009-09-27 00:12:57 +0000 | [diff] [blame] | 1696 | const RecordType *RT = T->getAs<RecordType>(); |
| 1697 | if (!RT) |
| 1698 | return false; |
Daniel Dunbar | 3c9e463 | 2009-11-21 09:05:47 +0000 | [diff] [blame] | 1699 | |
| 1700 | const ClassTemplateSpecializationDecl *SD = |
Anders Carlsson | f514b54 | 2009-09-27 00:12:57 +0000 | [diff] [blame] | 1701 | dyn_cast<ClassTemplateSpecializationDecl>(RT->getDecl()); |
| 1702 | if (!SD) |
| 1703 | return false; |
| 1704 | |
| 1705 | if (!isStdNamespace(SD->getDeclContext())) |
| 1706 | return false; |
Daniel Dunbar | 3c9e463 | 2009-11-21 09:05:47 +0000 | [diff] [blame] | 1707 | |
Anders Carlsson | f514b54 | 2009-09-27 00:12:57 +0000 | [diff] [blame] | 1708 | const TemplateArgumentList &TemplateArgs = SD->getTemplateArgs(); |
| 1709 | if (TemplateArgs.size() != 1) |
| 1710 | return false; |
Daniel Dunbar | 3c9e463 | 2009-11-21 09:05:47 +0000 | [diff] [blame] | 1711 | |
Anders Carlsson | f514b54 | 2009-09-27 00:12:57 +0000 | [diff] [blame] | 1712 | if (!isCharType(TemplateArgs[0].getAsType())) |
| 1713 | return false; |
Daniel Dunbar | 3c9e463 | 2009-11-21 09:05:47 +0000 | [diff] [blame] | 1714 | |
Daniel Dunbar | 01eb9b9 | 2009-10-18 21:17:35 +0000 | [diff] [blame] | 1715 | return SD->getIdentifier()->getName() == Name; |
Anders Carlsson | f514b54 | 2009-09-27 00:12:57 +0000 | [diff] [blame] | 1716 | } |
| 1717 | |
Anders Carlsson | 91f8860 | 2009-12-07 19:56:42 +0000 | [diff] [blame] | 1718 | template <std::size_t StrLen> |
| 1719 | bool isStreamCharSpecialization(const ClassTemplateSpecializationDecl *SD, |
| 1720 | const char (&Str)[StrLen]) { |
| 1721 | if (!SD->getIdentifier()->isStr(Str)) |
| 1722 | return false; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 1723 | |
Anders Carlsson | 91f8860 | 2009-12-07 19:56:42 +0000 | [diff] [blame] | 1724 | const TemplateArgumentList &TemplateArgs = SD->getTemplateArgs(); |
| 1725 | if (TemplateArgs.size() != 2) |
| 1726 | return false; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 1727 | |
Anders Carlsson | 91f8860 | 2009-12-07 19:56:42 +0000 | [diff] [blame] | 1728 | if (!isCharType(TemplateArgs[0].getAsType())) |
| 1729 | return false; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 1730 | |
Anders Carlsson | 91f8860 | 2009-12-07 19:56:42 +0000 | [diff] [blame] | 1731 | if (!isCharSpecialization(TemplateArgs[1].getAsType(), "char_traits")) |
| 1732 | return false; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 1733 | |
Anders Carlsson | 91f8860 | 2009-12-07 19:56:42 +0000 | [diff] [blame] | 1734 | return true; |
| 1735 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 1736 | |
Anders Carlsson | e7c8cb6 | 2009-09-26 20:53:44 +0000 | [diff] [blame] | 1737 | bool CXXNameMangler::mangleStandardSubstitution(const NamedDecl *ND) { |
| 1738 | // <substitution> ::= St # ::std:: |
Anders Carlsson | 8c03155 | 2009-09-26 23:10:05 +0000 | [diff] [blame] | 1739 | if (const NamespaceDecl *NS = dyn_cast<NamespaceDecl>(ND)) { |
Anders Carlsson | 47846d2 | 2009-12-04 06:23:23 +0000 | [diff] [blame] | 1740 | if (isStd(NS)) { |
Anders Carlsson | 8c03155 | 2009-09-26 23:10:05 +0000 | [diff] [blame] | 1741 | Out << "St"; |
| 1742 | return true; |
| 1743 | } |
| 1744 | } |
| 1745 | |
| 1746 | if (const ClassTemplateDecl *TD = dyn_cast<ClassTemplateDecl>(ND)) { |
| 1747 | if (!isStdNamespace(TD->getDeclContext())) |
| 1748 | return false; |
Daniel Dunbar | 3c9e463 | 2009-11-21 09:05:47 +0000 | [diff] [blame] | 1749 | |
Anders Carlsson | 8c03155 | 2009-09-26 23:10:05 +0000 | [diff] [blame] | 1750 | // <substitution> ::= Sa # ::std::allocator |
| 1751 | if (TD->getIdentifier()->isStr("allocator")) { |
| 1752 | Out << "Sa"; |
| 1753 | return true; |
| 1754 | } |
Daniel Dunbar | 3c9e463 | 2009-11-21 09:05:47 +0000 | [diff] [blame] | 1755 | |
Anders Carlsson | 189d59c | 2009-09-26 23:14:39 +0000 | [diff] [blame] | 1756 | // <<substitution> ::= Sb # ::std::basic_string |
| 1757 | if (TD->getIdentifier()->isStr("basic_string")) { |
| 1758 | Out << "Sb"; |
| 1759 | return true; |
| 1760 | } |
Anders Carlsson | 8c03155 | 2009-09-26 23:10:05 +0000 | [diff] [blame] | 1761 | } |
Daniel Dunbar | 3c9e463 | 2009-11-21 09:05:47 +0000 | [diff] [blame] | 1762 | |
| 1763 | if (const ClassTemplateSpecializationDecl *SD = |
Anders Carlsson | f514b54 | 2009-09-27 00:12:57 +0000 | [diff] [blame] | 1764 | dyn_cast<ClassTemplateSpecializationDecl>(ND)) { |
Eli Friedman | 5370ee2 | 2010-02-23 18:25:09 +0000 | [diff] [blame] | 1765 | if (!isStdNamespace(SD->getDeclContext())) |
| 1766 | return false; |
| 1767 | |
Anders Carlsson | f514b54 | 2009-09-27 00:12:57 +0000 | [diff] [blame] | 1768 | // <substitution> ::= Ss # ::std::basic_string<char, |
| 1769 | // ::std::char_traits<char>, |
| 1770 | // ::std::allocator<char> > |
| 1771 | if (SD->getIdentifier()->isStr("basic_string")) { |
| 1772 | const TemplateArgumentList &TemplateArgs = SD->getTemplateArgs(); |
Daniel Dunbar | 3c9e463 | 2009-11-21 09:05:47 +0000 | [diff] [blame] | 1773 | |
Anders Carlsson | f514b54 | 2009-09-27 00:12:57 +0000 | [diff] [blame] | 1774 | if (TemplateArgs.size() != 3) |
| 1775 | return false; |
Daniel Dunbar | 3c9e463 | 2009-11-21 09:05:47 +0000 | [diff] [blame] | 1776 | |
Anders Carlsson | f514b54 | 2009-09-27 00:12:57 +0000 | [diff] [blame] | 1777 | if (!isCharType(TemplateArgs[0].getAsType())) |
| 1778 | return false; |
Daniel Dunbar | 3c9e463 | 2009-11-21 09:05:47 +0000 | [diff] [blame] | 1779 | |
Anders Carlsson | f514b54 | 2009-09-27 00:12:57 +0000 | [diff] [blame] | 1780 | if (!isCharSpecialization(TemplateArgs[1].getAsType(), "char_traits")) |
| 1781 | return false; |
Daniel Dunbar | 3c9e463 | 2009-11-21 09:05:47 +0000 | [diff] [blame] | 1782 | |
Anders Carlsson | f514b54 | 2009-09-27 00:12:57 +0000 | [diff] [blame] | 1783 | if (!isCharSpecialization(TemplateArgs[2].getAsType(), "allocator")) |
| 1784 | return false; |
| 1785 | |
| 1786 | Out << "Ss"; |
| 1787 | return true; |
| 1788 | } |
Daniel Dunbar | 3c9e463 | 2009-11-21 09:05:47 +0000 | [diff] [blame] | 1789 | |
Anders Carlsson | 91f8860 | 2009-12-07 19:56:42 +0000 | [diff] [blame] | 1790 | // <substitution> ::= Si # ::std::basic_istream<char, |
| 1791 | // ::std::char_traits<char> > |
| 1792 | if (isStreamCharSpecialization(SD, "basic_istream")) { |
| 1793 | Out << "Si"; |
| 1794 | return true; |
| 1795 | } |
| 1796 | |
Daniel Dunbar | 3c9e463 | 2009-11-21 09:05:47 +0000 | [diff] [blame] | 1797 | // <substitution> ::= So # ::std::basic_ostream<char, |
Anders Carlsson | 8f8fd8e | 2009-10-08 17:20:26 +0000 | [diff] [blame] | 1798 | // ::std::char_traits<char> > |
Anders Carlsson | 91f8860 | 2009-12-07 19:56:42 +0000 | [diff] [blame] | 1799 | if (isStreamCharSpecialization(SD, "basic_ostream")) { |
Anders Carlsson | 8f8fd8e | 2009-10-08 17:20:26 +0000 | [diff] [blame] | 1800 | Out << "So"; |
| 1801 | return true; |
| 1802 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 1803 | |
Anders Carlsson | 91f8860 | 2009-12-07 19:56:42 +0000 | [diff] [blame] | 1804 | // <substitution> ::= Sd # ::std::basic_iostream<char, |
| 1805 | // ::std::char_traits<char> > |
| 1806 | if (isStreamCharSpecialization(SD, "basic_iostream")) { |
| 1807 | Out << "Sd"; |
| 1808 | return true; |
| 1809 | } |
Anders Carlsson | f514b54 | 2009-09-27 00:12:57 +0000 | [diff] [blame] | 1810 | } |
Anders Carlsson | 8c03155 | 2009-09-26 23:10:05 +0000 | [diff] [blame] | 1811 | return false; |
Anders Carlsson | e7c8cb6 | 2009-09-26 20:53:44 +0000 | [diff] [blame] | 1812 | } |
| 1813 | |
Anders Carlsson | 7696737 | 2009-09-17 00:43:46 +0000 | [diff] [blame] | 1814 | void CXXNameMangler::addSubstitution(QualType T) { |
Anders Carlsson | d99edc4 | 2009-09-26 03:55:37 +0000 | [diff] [blame] | 1815 | if (!T.getCVRQualifiers()) { |
| 1816 | if (const RecordType *RT = T->getAs<RecordType>()) { |
| 1817 | addSubstitution(RT->getDecl()); |
| 1818 | return; |
| 1819 | } |
| 1820 | } |
Daniel Dunbar | 3c9e463 | 2009-11-21 09:05:47 +0000 | [diff] [blame] | 1821 | |
Anders Carlsson | 7696737 | 2009-09-17 00:43:46 +0000 | [diff] [blame] | 1822 | uintptr_t TypePtr = reinterpret_cast<uintptr_t>(T.getAsOpaquePtr()); |
Anders Carlsson | d3a932a | 2009-09-17 03:53:28 +0000 | [diff] [blame] | 1823 | addSubstitution(TypePtr); |
| 1824 | } |
| 1825 | |
| 1826 | void CXXNameMangler::addSubstitution(uintptr_t Ptr) { |
Anders Carlsson | 7696737 | 2009-09-17 00:43:46 +0000 | [diff] [blame] | 1827 | unsigned SeqID = Substitutions.size(); |
Daniel Dunbar | 3c9e463 | 2009-11-21 09:05:47 +0000 | [diff] [blame] | 1828 | |
Anders Carlsson | d3a932a | 2009-09-17 03:53:28 +0000 | [diff] [blame] | 1829 | assert(!Substitutions.count(Ptr) && "Substitution already exists!"); |
Daniel Dunbar | 3c9e463 | 2009-11-21 09:05:47 +0000 | [diff] [blame] | 1830 | Substitutions[Ptr] = SeqID; |
Anders Carlsson | 7696737 | 2009-09-17 00:43:46 +0000 | [diff] [blame] | 1831 | } |
| 1832 | |
Daniel Dunbar | 1b07711 | 2009-11-21 09:06:10 +0000 | [diff] [blame] | 1833 | // |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1834 | |
Daniel Dunbar | 1b07711 | 2009-11-21 09:06:10 +0000 | [diff] [blame] | 1835 | /// \brief Mangles the name of the declaration D and emits that name to the |
| 1836 | /// given output stream. |
| 1837 | /// |
| 1838 | /// If the declaration D requires a mangled name, this routine will emit that |
| 1839 | /// mangled name to \p os and return true. Otherwise, \p os will be unchanged |
| 1840 | /// and this routine will return false. In this case, the caller should just |
| 1841 | /// emit the identifier of the declaration (\c D->getIdentifier()) as its |
| 1842 | /// name. |
Daniel Dunbar | f981bf8 | 2009-11-21 09:14:52 +0000 | [diff] [blame] | 1843 | void MangleContext::mangleName(const NamedDecl *D, |
Daniel Dunbar | 94fd26d | 2009-11-21 09:06:22 +0000 | [diff] [blame] | 1844 | llvm::SmallVectorImpl<char> &Res) { |
Daniel Dunbar | c02ab4c | 2009-11-21 09:14:44 +0000 | [diff] [blame] | 1845 | assert((isa<FunctionDecl>(D) || isa<VarDecl>(D)) && |
| 1846 | "Invalid mangleName() call, argument is not a variable or function!"); |
| 1847 | assert(!isa<CXXConstructorDecl>(D) && !isa<CXXDestructorDecl>(D) && |
| 1848 | "Invalid mangleName() call on 'structor decl!"); |
Daniel Dunbar | 3c9e463 | 2009-11-21 09:05:47 +0000 | [diff] [blame] | 1849 | |
Daniel Dunbar | 1b07711 | 2009-11-21 09:06:10 +0000 | [diff] [blame] | 1850 | PrettyStackTraceDecl CrashInfo(D, SourceLocation(), |
| 1851 | getASTContext().getSourceManager(), |
| 1852 | "Mangling declaration"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1853 | |
Daniel Dunbar | 94fd26d | 2009-11-21 09:06:22 +0000 | [diff] [blame] | 1854 | CXXNameMangler Mangler(*this, Res); |
| 1855 | return Mangler.mangle(D); |
Daniel Dunbar | 1b07711 | 2009-11-21 09:06:10 +0000 | [diff] [blame] | 1856 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1857 | |
Daniel Dunbar | 1b07711 | 2009-11-21 09:06:10 +0000 | [diff] [blame] | 1858 | void MangleContext::mangleCXXCtor(const CXXConstructorDecl *D, CXXCtorType Type, |
Daniel Dunbar | 94fd26d | 2009-11-21 09:06:22 +0000 | [diff] [blame] | 1859 | llvm::SmallVectorImpl<char> &Res) { |
Daniel Dunbar | 77939c9 | 2009-11-21 09:06:31 +0000 | [diff] [blame] | 1860 | CXXNameMangler Mangler(*this, Res, D, Type); |
| 1861 | Mangler.mangle(D); |
Daniel Dunbar | 1b07711 | 2009-11-21 09:06:10 +0000 | [diff] [blame] | 1862 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1863 | |
Daniel Dunbar | 1b07711 | 2009-11-21 09:06:10 +0000 | [diff] [blame] | 1864 | void MangleContext::mangleCXXDtor(const CXXDestructorDecl *D, CXXDtorType Type, |
Daniel Dunbar | 94fd26d | 2009-11-21 09:06:22 +0000 | [diff] [blame] | 1865 | llvm::SmallVectorImpl<char> &Res) { |
Daniel Dunbar | 77939c9 | 2009-11-21 09:06:31 +0000 | [diff] [blame] | 1866 | CXXNameMangler Mangler(*this, Res, D, Type); |
| 1867 | Mangler.mangle(D); |
Daniel Dunbar | 1b07711 | 2009-11-21 09:06:10 +0000 | [diff] [blame] | 1868 | } |
Mike Stump | f121677 | 2009-07-31 18:25:34 +0000 | [diff] [blame] | 1869 | |
Anders Carlsson | 19879c9 | 2010-03-23 17:17:29 +0000 | [diff] [blame] | 1870 | void MangleContext::mangleThunk(const CXXMethodDecl *MD, |
| 1871 | const ThunkInfo &Thunk, |
| 1872 | llvm::SmallVectorImpl<char> &Res) { |
| 1873 | // <special-name> ::= T <call-offset> <base encoding> |
| 1874 | // # base is the nominal target function of thunk |
| 1875 | // <special-name> ::= Tc <call-offset> <call-offset> <base encoding> |
| 1876 | // # base is the nominal target function of thunk |
| 1877 | // # first call-offset is 'this' adjustment |
| 1878 | // # second call-offset is result adjustment |
| 1879 | |
| 1880 | assert(!isa<CXXDestructorDecl>(MD) && |
| 1881 | "Use mangleCXXDtor for destructor decls!"); |
| 1882 | |
| 1883 | CXXNameMangler Mangler(*this, Res); |
| 1884 | Mangler.getStream() << "_ZT"; |
| 1885 | if (!Thunk.Return.isEmpty()) |
| 1886 | Mangler.getStream() << 'c'; |
| 1887 | |
| 1888 | // Mangle the 'this' pointer adjustment. |
| 1889 | Mangler.mangleCallOffset(Thunk.This.NonVirtual, Thunk.This.VCallOffsetOffset); |
| 1890 | |
| 1891 | // Mangle the return pointer adjustment if there is one. |
| 1892 | if (!Thunk.Return.isEmpty()) |
| 1893 | Mangler.mangleCallOffset(Thunk.Return.NonVirtual, |
| 1894 | Thunk.Return.VBaseOffsetOffset); |
| 1895 | |
| 1896 | Mangler.mangleFunctionEncoding(MD); |
| 1897 | } |
| 1898 | |
| 1899 | void |
| 1900 | MangleContext::mangleCXXDtorThunk(const CXXDestructorDecl *DD, CXXDtorType Type, |
| 1901 | const ThisAdjustment &ThisAdjustment, |
| 1902 | llvm::SmallVectorImpl<char> &Res) { |
| 1903 | // <special-name> ::= T <call-offset> <base encoding> |
| 1904 | // # base is the nominal target function of thunk |
| 1905 | |
| 1906 | CXXNameMangler Mangler(*this, Res, DD, Type); |
| 1907 | Mangler.getStream() << "_ZT"; |
| 1908 | |
| 1909 | // Mangle the 'this' pointer adjustment. |
| 1910 | Mangler.mangleCallOffset(ThisAdjustment.NonVirtual, |
| 1911 | ThisAdjustment.VCallOffsetOffset); |
| 1912 | |
| 1913 | Mangler.mangleFunctionEncoding(DD); |
| 1914 | } |
| 1915 | |
Daniel Dunbar | c074771 | 2009-11-21 09:12:13 +0000 | [diff] [blame] | 1916 | /// mangleGuardVariable - Returns the mangled name for a guard variable |
| 1917 | /// for the passed in VarDecl. |
| 1918 | void MangleContext::mangleGuardVariable(const VarDecl *D, |
| 1919 | llvm::SmallVectorImpl<char> &Res) { |
| 1920 | // <special-name> ::= GV <object name> # Guard variable for one-time |
| 1921 | // # initialization |
| 1922 | CXXNameMangler Mangler(*this, Res); |
| 1923 | Mangler.getStream() << "_ZGV"; |
| 1924 | Mangler.mangleName(D); |
| 1925 | } |
| 1926 | |
Daniel Dunbar | 1b07711 | 2009-11-21 09:06:10 +0000 | [diff] [blame] | 1927 | void MangleContext::mangleCXXVtable(const CXXRecordDecl *RD, |
Daniel Dunbar | 94fd26d | 2009-11-21 09:06:22 +0000 | [diff] [blame] | 1928 | llvm::SmallVectorImpl<char> &Res) { |
Daniel Dunbar | c074771 | 2009-11-21 09:12:13 +0000 | [diff] [blame] | 1929 | // <special-name> ::= TV <type> # virtual table |
Daniel Dunbar | 94fd26d | 2009-11-21 09:06:22 +0000 | [diff] [blame] | 1930 | CXXNameMangler Mangler(*this, Res); |
Daniel Dunbar | c074771 | 2009-11-21 09:12:13 +0000 | [diff] [blame] | 1931 | Mangler.getStream() << "_ZTV"; |
| 1932 | Mangler.mangleName(RD); |
Daniel Dunbar | 1b07711 | 2009-11-21 09:06:10 +0000 | [diff] [blame] | 1933 | } |
Mike Stump | 82d75b0 | 2009-11-10 01:58:37 +0000 | [diff] [blame] | 1934 | |
Daniel Dunbar | 1b07711 | 2009-11-21 09:06:10 +0000 | [diff] [blame] | 1935 | void MangleContext::mangleCXXVTT(const CXXRecordDecl *RD, |
Daniel Dunbar | 94fd26d | 2009-11-21 09:06:22 +0000 | [diff] [blame] | 1936 | llvm::SmallVectorImpl<char> &Res) { |
Daniel Dunbar | c074771 | 2009-11-21 09:12:13 +0000 | [diff] [blame] | 1937 | // <special-name> ::= TT <type> # VTT structure |
Daniel Dunbar | 94fd26d | 2009-11-21 09:06:22 +0000 | [diff] [blame] | 1938 | CXXNameMangler Mangler(*this, Res); |
Daniel Dunbar | c074771 | 2009-11-21 09:12:13 +0000 | [diff] [blame] | 1939 | Mangler.getStream() << "_ZTT"; |
| 1940 | Mangler.mangleName(RD); |
Daniel Dunbar | 1b07711 | 2009-11-21 09:06:10 +0000 | [diff] [blame] | 1941 | } |
Mike Stump | ab3f7e9 | 2009-11-10 01:41:59 +0000 | [diff] [blame] | 1942 | |
Daniel Dunbar | 1b07711 | 2009-11-21 09:06:10 +0000 | [diff] [blame] | 1943 | void MangleContext::mangleCXXCtorVtable(const CXXRecordDecl *RD, int64_t Offset, |
| 1944 | const CXXRecordDecl *Type, |
Daniel Dunbar | 94fd26d | 2009-11-21 09:06:22 +0000 | [diff] [blame] | 1945 | llvm::SmallVectorImpl<char> &Res) { |
Daniel Dunbar | c074771 | 2009-11-21 09:12:13 +0000 | [diff] [blame] | 1946 | // <special-name> ::= TC <type> <offset number> _ <base type> |
Daniel Dunbar | 94fd26d | 2009-11-21 09:06:22 +0000 | [diff] [blame] | 1947 | CXXNameMangler Mangler(*this, Res); |
Daniel Dunbar | c074771 | 2009-11-21 09:12:13 +0000 | [diff] [blame] | 1948 | Mangler.getStream() << "_ZTC"; |
| 1949 | Mangler.mangleName(RD); |
| 1950 | Mangler.getStream() << Offset; |
| 1951 | Mangler.getStream() << "_"; |
| 1952 | Mangler.mangleName(Type); |
Daniel Dunbar | 1b07711 | 2009-11-21 09:06:10 +0000 | [diff] [blame] | 1953 | } |
Mike Stump | 738f8c2 | 2009-07-31 23:15:31 +0000 | [diff] [blame] | 1954 | |
Mike Stump | de05057 | 2009-12-02 18:57:08 +0000 | [diff] [blame] | 1955 | void MangleContext::mangleCXXRTTI(QualType Ty, |
Daniel Dunbar | 94fd26d | 2009-11-21 09:06:22 +0000 | [diff] [blame] | 1956 | llvm::SmallVectorImpl<char> &Res) { |
Daniel Dunbar | c074771 | 2009-11-21 09:12:13 +0000 | [diff] [blame] | 1957 | // <special-name> ::= TI <type> # typeinfo structure |
Douglas Gregor | 154fe98 | 2009-12-23 22:04:40 +0000 | [diff] [blame] | 1958 | assert(!Ty.hasQualifiers() && "RTTI info cannot have top-level qualifiers"); |
Daniel Dunbar | 94fd26d | 2009-11-21 09:06:22 +0000 | [diff] [blame] | 1959 | CXXNameMangler Mangler(*this, Res); |
Daniel Dunbar | c074771 | 2009-11-21 09:12:13 +0000 | [diff] [blame] | 1960 | Mangler.getStream() << "_ZTI"; |
| 1961 | Mangler.mangleType(Ty); |
Daniel Dunbar | 1b07711 | 2009-11-21 09:06:10 +0000 | [diff] [blame] | 1962 | } |
Mike Stump | 6779598 | 2009-11-14 00:14:13 +0000 | [diff] [blame] | 1963 | |
Mike Stump | de05057 | 2009-12-02 18:57:08 +0000 | [diff] [blame] | 1964 | void MangleContext::mangleCXXRTTIName(QualType Ty, |
Daniel Dunbar | 94fd26d | 2009-11-21 09:06:22 +0000 | [diff] [blame] | 1965 | llvm::SmallVectorImpl<char> &Res) { |
Daniel Dunbar | c074771 | 2009-11-21 09:12:13 +0000 | [diff] [blame] | 1966 | // <special-name> ::= TS <type> # typeinfo name (null terminated byte string) |
Daniel Dunbar | 94fd26d | 2009-11-21 09:06:22 +0000 | [diff] [blame] | 1967 | CXXNameMangler Mangler(*this, Res); |
Daniel Dunbar | c074771 | 2009-11-21 09:12:13 +0000 | [diff] [blame] | 1968 | Mangler.getStream() << "_ZTS"; |
| 1969 | Mangler.mangleType(Ty); |
Mike Stump | f121677 | 2009-07-31 18:25:34 +0000 | [diff] [blame] | 1970 | } |