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