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