| 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" |
| Douglas Gregor | 6ec3668 | 2009-02-18 23:53:56 +0000 | [diff] [blame] | 23 | #include "clang/Basic/SourceManager.h" |
| Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 24 | #include "llvm/Support/Compiler.h" |
| 25 | #include "llvm/Support/raw_ostream.h" |
| John McCall | efe6aee | 2009-09-05 07:56:18 +0000 | [diff] [blame] | 26 | #include "llvm/Support/ErrorHandling.h" |
| Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 27 | using namespace clang; |
| 28 | |
| 29 | namespace { |
| 30 | class VISIBILITY_HIDDEN CXXNameMangler { |
| 31 | ASTContext &Context; |
| 32 | llvm::raw_ostream &Out; |
| 33 | |
| Anders Carlsson | 27ae536 | 2009-04-17 01:58:57 +0000 | [diff] [blame] | 34 | const CXXMethodDecl *Structor; |
| 35 | unsigned StructorType; |
| Anders Carlsson | 3ac86b5 | 2009-04-15 05:36:58 +0000 | [diff] [blame] | 36 | CXXCtorType CtorType; |
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 37 | |
| Anders Carlsson | 7696737 | 2009-09-17 00:43:46 +0000 | [diff] [blame] | 38 | llvm::DenseMap<uintptr_t, unsigned> Substitutions; |
| 39 | |
| Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 40 | public: |
| 41 | CXXNameMangler(ASTContext &C, llvm::raw_ostream &os) |
| Anders Carlsson | 27ae536 | 2009-04-17 01:58:57 +0000 | [diff] [blame] | 42 | : Context(C), Out(os), Structor(0), StructorType(0) { } |
| Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 43 | |
| 44 | bool mangle(const NamedDecl *D); |
| Mike Stump | 77ca8f6 | 2009-09-05 07:20:32 +0000 | [diff] [blame] | 45 | void mangleCalloffset(int64_t nv, int64_t v); |
| Mike Stump | dec025b | 2009-09-07 04:27:52 +0000 | [diff] [blame] | 46 | void mangleThunk(const FunctionDecl *FD, int64_t nv, int64_t v); |
| 47 | void mangleCovariantThunk(const FunctionDecl *FD, |
| Mike Stump | 77ca8f6 | 2009-09-05 07:20:32 +0000 | [diff] [blame] | 48 | int64_t nv_t, int64_t v_t, |
| Mike Stump | 9124bcc | 2009-09-02 00:56:18 +0000 | [diff] [blame] | 49 | int64_t nv_r, int64_t v_r); |
| Anders Carlsson | 41aa8c1 | 2009-04-13 18:02:10 +0000 | [diff] [blame] | 50 | void mangleGuardVariable(const VarDecl *D); |
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 51 | |
| Mike Stump | f121677 | 2009-07-31 18:25:34 +0000 | [diff] [blame] | 52 | void mangleCXXVtable(QualType Type); |
| Mike Stump | 738f8c2 | 2009-07-31 23:15:31 +0000 | [diff] [blame] | 53 | void mangleCXXRtti(QualType Type); |
| Anders Carlsson | 3ac86b5 | 2009-04-15 05:36:58 +0000 | [diff] [blame] | 54 | void mangleCXXCtor(const CXXConstructorDecl *D, CXXCtorType Type); |
| Anders Carlsson | 27ae536 | 2009-04-17 01:58:57 +0000 | [diff] [blame] | 55 | void mangleCXXDtor(const CXXDestructorDecl *D, CXXDtorType Type); |
| Anders Carlsson | 3ac86b5 | 2009-04-15 05:36:58 +0000 | [diff] [blame] | 56 | |
| Anders Carlsson | 43f1740 | 2009-04-02 15:51:53 +0000 | [diff] [blame] | 57 | private: |
| Anders Carlsson | 7696737 | 2009-09-17 00:43:46 +0000 | [diff] [blame] | 58 | bool mangleSubstitution(QualType T); |
| 59 | void addSubstitution(QualType T); |
| 60 | |
| Anders Carlsson | 43f1740 | 2009-04-02 15:51:53 +0000 | [diff] [blame] | 61 | bool mangleFunctionDecl(const FunctionDecl *FD); |
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 62 | |
| Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 63 | void mangleFunctionEncoding(const FunctionDecl *FD); |
| 64 | void mangleName(const NamedDecl *ND); |
| 65 | void mangleUnqualifiedName(const NamedDecl *ND); |
| Anders Carlsson | 201ce74 | 2009-09-17 03:17:01 +0000 | [diff] [blame^] | 66 | void mangleUnscopedName(const NamedDecl *ND); |
| 67 | void mangleUnscopedTemplateName(const FunctionDecl *ND); |
| Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 68 | void mangleSourceName(const IdentifierInfo *II); |
| Anders Carlsson | 1b42c79 | 2009-04-02 16:24:45 +0000 | [diff] [blame] | 69 | void mangleLocalName(const NamedDecl *ND); |
| Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 70 | void mangleNestedName(const NamedDecl *ND); |
| 71 | void manglePrefix(const DeclContext *DC); |
| 72 | void mangleOperatorName(OverloadedOperatorKind OO, unsigned Arity); |
| 73 | void mangleCVQualifiers(unsigned Quals); |
| 74 | void mangleType(QualType T); |
| John McCall | efe6aee | 2009-09-05 07:56:18 +0000 | [diff] [blame] | 75 | |
| 76 | // Declare manglers for every type class. |
| 77 | #define ABSTRACT_TYPE(CLASS, PARENT) |
| 78 | #define NON_CANONICAL_TYPE(CLASS, PARENT) |
| 79 | #define TYPE(CLASS, PARENT) void mangleType(const CLASS##Type *T); |
| 80 | #include "clang/AST/TypeNodes.def" |
| 81 | |
| 82 | void mangleType(const TagType*); |
| 83 | void mangleBareFunctionType(const FunctionType *T, |
| 84 | bool MangleReturnType); |
| Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 85 | void mangleExpression(Expr *E); |
| Anders Carlsson | 3ac86b5 | 2009-04-15 05:36:58 +0000 | [diff] [blame] | 86 | void mangleCXXCtorType(CXXCtorType T); |
| Anders Carlsson | 27ae536 | 2009-04-17 01:58:57 +0000 | [diff] [blame] | 87 | void mangleCXXDtorType(CXXDtorType T); |
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 88 | |
| Anders Carlsson | 7a0ba87 | 2009-05-15 16:09:15 +0000 | [diff] [blame] | 89 | void mangleTemplateArgumentList(const TemplateArgumentList &L); |
| 90 | void mangleTemplateArgument(const TemplateArgument &A); |
| Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 91 | }; |
| 92 | } |
| 93 | |
| Anders Carlsson | 43f1740 | 2009-04-02 15:51:53 +0000 | [diff] [blame] | 94 | static bool isInCLinkageSpecification(const Decl *D) { |
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 95 | for (const DeclContext *DC = D->getDeclContext(); |
| Anders Carlsson | 43f1740 | 2009-04-02 15:51:53 +0000 | [diff] [blame] | 96 | !DC->isTranslationUnit(); DC = DC->getParent()) { |
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 97 | if (const LinkageSpecDecl *Linkage = dyn_cast<LinkageSpecDecl>(DC)) |
| Anders Carlsson | 43f1740 | 2009-04-02 15:51:53 +0000 | [diff] [blame] | 98 | return Linkage->getLanguage() == LinkageSpecDecl::lang_c; |
| 99 | } |
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 100 | |
| Anders Carlsson | 43f1740 | 2009-04-02 15:51:53 +0000 | [diff] [blame] | 101 | return false; |
| 102 | } |
| 103 | |
| 104 | bool CXXNameMangler::mangleFunctionDecl(const FunctionDecl *FD) { |
| Mike Stump | 141c5af | 2009-09-02 00:25:38 +0000 | [diff] [blame] | 105 | // Clang's "overloadable" attribute extension to C/C++ implies name mangling |
| 106 | // (always). |
| Argyrios Kyrtzidis | 40b598e | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 107 | if (!FD->hasAttr<OverloadableAttr>()) { |
| Chris Lattner | 783601d | 2009-06-13 23:34:16 +0000 | [diff] [blame] | 108 | // C functions are not mangled, and "main" is never mangled. |
| Douglas Gregor | 48a83b5 | 2009-09-12 00:17:51 +0000 | [diff] [blame] | 109 | if (!Context.getLangOptions().CPlusPlus || FD->isMain()) |
| Chris Lattner | 783601d | 2009-06-13 23:34:16 +0000 | [diff] [blame] | 110 | return false; |
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 111 | |
| 112 | // No mangling in an "implicit extern C" header. |
| Chris Lattner | 783601d | 2009-06-13 23:34:16 +0000 | [diff] [blame] | 113 | if (FD->getLocation().isValid() && |
| 114 | Context.getSourceManager().isInExternCSystemHeader(FD->getLocation())) |
| 115 | return false; |
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 116 | |
| Chris Lattner | 783601d | 2009-06-13 23:34:16 +0000 | [diff] [blame] | 117 | // No name mangling in a C linkage specification. |
| 118 | if (isInCLinkageSpecification(FD)) |
| 119 | return false; |
| 120 | } |
| Anders Carlsson | 43f1740 | 2009-04-02 15:51:53 +0000 | [diff] [blame] | 121 | |
| 122 | // If we get here, mangle the decl name! |
| 123 | Out << "_Z"; |
| 124 | mangleFunctionEncoding(FD); |
| 125 | return true; |
| 126 | } |
| Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 127 | |
| 128 | bool CXXNameMangler::mangle(const NamedDecl *D) { |
| Mike Stump | 141c5af | 2009-09-02 00:25:38 +0000 | [diff] [blame] | 129 | // Any decl can be declared with __asm("foo") on it, and this takes precedence |
| 130 | // over all other naming in the .o file. |
| Argyrios Kyrtzidis | 40b598e | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 131 | if (const AsmLabelAttr *ALA = D->getAttr<AsmLabelAttr>()) { |
| Chris Lattner | ca3f25c | 2009-03-21 08:24:40 +0000 | [diff] [blame] | 132 | // If we have an asm name, then we use it as the mangling. |
| 133 | Out << '\01'; // LLVM IR Marker for __asm("foo") |
| 134 | Out << ALA->getLabel(); |
| 135 | return true; |
| 136 | } |
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 137 | |
| Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 138 | // <mangled-name> ::= _Z <encoding> |
| 139 | // ::= <data name> |
| 140 | // ::= <special-name> |
| 141 | |
| 142 | // FIXME: Actually use a visitor to decode these? |
| Anders Carlsson | 43f1740 | 2009-04-02 15:51:53 +0000 | [diff] [blame] | 143 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) |
| 144 | return mangleFunctionDecl(FD); |
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 145 | |
| Anders Carlsson | 329749c | 2009-04-02 16:05:20 +0000 | [diff] [blame] | 146 | if (const VarDecl *VD = dyn_cast<VarDecl>(D)) { |
| 147 | if (!Context.getLangOptions().CPlusPlus || |
| Anders Carlsson | 9ccb065 | 2009-04-11 01:19:45 +0000 | [diff] [blame] | 148 | isInCLinkageSpecification(D) || |
| 149 | D->getDeclContext()->isTranslationUnit()) |
| Anders Carlsson | 329749c | 2009-04-02 16:05:20 +0000 | [diff] [blame] | 150 | return false; |
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 151 | |
| Anders Carlsson | 329749c | 2009-04-02 16:05:20 +0000 | [diff] [blame] | 152 | Out << "_Z"; |
| 153 | mangleName(VD); |
| 154 | return true; |
| 155 | } |
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 156 | |
| Anders Carlsson | 43f1740 | 2009-04-02 15:51:53 +0000 | [diff] [blame] | 157 | return false; |
| Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 158 | } |
| 159 | |
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 160 | void CXXNameMangler::mangleCXXCtor(const CXXConstructorDecl *D, |
| Anders Carlsson | 3ac86b5 | 2009-04-15 05:36:58 +0000 | [diff] [blame] | 161 | CXXCtorType Type) { |
| Anders Carlsson | 27ae536 | 2009-04-17 01:58:57 +0000 | [diff] [blame] | 162 | assert(!Structor && "Structor already set!"); |
| 163 | Structor = D; |
| 164 | StructorType = Type; |
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 165 | |
| Anders Carlsson | 27ae536 | 2009-04-17 01:58:57 +0000 | [diff] [blame] | 166 | mangle(D); |
| 167 | } |
| 168 | |
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 169 | void CXXNameMangler::mangleCXXDtor(const CXXDestructorDecl *D, |
| Anders Carlsson | 27ae536 | 2009-04-17 01:58:57 +0000 | [diff] [blame] | 170 | CXXDtorType Type) { |
| 171 | assert(!Structor && "Structor already set!"); |
| 172 | Structor = D; |
| 173 | StructorType = Type; |
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 174 | |
| Anders Carlsson | 3ac86b5 | 2009-04-15 05:36:58 +0000 | [diff] [blame] | 175 | mangle(D); |
| 176 | } |
| 177 | |
| Mike Stump | f121677 | 2009-07-31 18:25:34 +0000 | [diff] [blame] | 178 | void CXXNameMangler::mangleCXXVtable(QualType T) { |
| 179 | // <special-name> ::= TV <type> # virtual table |
| 180 | Out << "_ZTV"; |
| 181 | mangleType(T); |
| 182 | } |
| 183 | |
| Mike Stump | 738f8c2 | 2009-07-31 23:15:31 +0000 | [diff] [blame] | 184 | void CXXNameMangler::mangleCXXRtti(QualType T) { |
| 185 | // <special-name> ::= TI <type> # typeinfo structure |
| 186 | Out << "_ZTI"; |
| 187 | mangleType(T); |
| 188 | } |
| 189 | |
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 190 | void CXXNameMangler::mangleGuardVariable(const VarDecl *D) { |
| 191 | // <special-name> ::= GV <object name> # Guard variable for one-time |
| Mike Stump | f121677 | 2009-07-31 18:25:34 +0000 | [diff] [blame] | 192 | // # initialization |
| Anders Carlsson | 41aa8c1 | 2009-04-13 18:02:10 +0000 | [diff] [blame] | 193 | |
| 194 | Out << "_ZGV"; |
| 195 | mangleName(D); |
| 196 | } |
| 197 | |
| Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 198 | void CXXNameMangler::mangleFunctionEncoding(const FunctionDecl *FD) { |
| 199 | // <encoding> ::= <function name> <bare-function-type> |
| 200 | mangleName(FD); |
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 201 | |
| Mike Stump | 141c5af | 2009-09-02 00:25:38 +0000 | [diff] [blame] | 202 | // Whether the mangling of a function type includes the return type depends on |
| 203 | // the context and the nature of the function. The rules for deciding whether |
| 204 | // the return type is included are: |
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 205 | // |
| Douglas Gregor | 1fd2dd1 | 2009-06-29 22:39:32 +0000 | [diff] [blame] | 206 | // 1. Template functions (names or types) have return types encoded, with |
| 207 | // the exceptions listed below. |
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 208 | // 2. Function types not appearing as part of a function name mangling, |
| Douglas Gregor | 1fd2dd1 | 2009-06-29 22:39:32 +0000 | [diff] [blame] | 209 | // e.g. parameters, pointer types, etc., have return type encoded, with the |
| 210 | // exceptions listed below. |
| 211 | // 3. Non-template function names do not have return types encoded. |
| 212 | // |
| Mike Stump | 141c5af | 2009-09-02 00:25:38 +0000 | [diff] [blame] | 213 | // The exceptions mentioned in (1) and (2) above, for which the return type is |
| 214 | // never included, are |
| Douglas Gregor | 1fd2dd1 | 2009-06-29 22:39:32 +0000 | [diff] [blame] | 215 | // 1. Constructors. |
| 216 | // 2. Destructors. |
| 217 | // 3. Conversion operator functions, e.g. operator int. |
| 218 | bool MangleReturnType = false; |
| 219 | if (FD->getPrimaryTemplate() && |
| 220 | !(isa<CXXConstructorDecl>(FD) || isa<CXXDestructorDecl>(FD) || |
| 221 | isa<CXXConversionDecl>(FD))) |
| 222 | MangleReturnType = true; |
| 223 | mangleBareFunctionType(FD->getType()->getAsFunctionType(), MangleReturnType); |
| Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 224 | } |
| 225 | |
| 226 | static bool isStdNamespace(const DeclContext *DC) { |
| 227 | if (!DC->isNamespace() || !DC->getParent()->isTranslationUnit()) |
| 228 | return false; |
| 229 | |
| 230 | const NamespaceDecl *NS = cast<NamespaceDecl>(DC); |
| Douglas Gregor | 6ec3668 | 2009-02-18 23:53:56 +0000 | [diff] [blame] | 231 | return NS->getOriginalNamespace()->getIdentifier()->isStr("std"); |
| Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 232 | } |
| 233 | |
| 234 | void CXXNameMangler::mangleName(const NamedDecl *ND) { |
| 235 | // <name> ::= <nested-name> |
| 236 | // ::= <unscoped-name> |
| 237 | // ::= <unscoped-template-name> <template-args> |
| Anders Carlsson | 201ce74 | 2009-09-17 03:17:01 +0000 | [diff] [blame^] | 238 | // ::= <local-name> |
| Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 239 | // |
| Anders Carlsson | 201ce74 | 2009-09-17 03:17:01 +0000 | [diff] [blame^] | 240 | if (ND->getDeclContext()->isTranslationUnit() || |
| 241 | isStdNamespace(ND->getDeclContext())) { |
| 242 | const FunctionDecl *FD = dyn_cast<FunctionDecl>(ND); |
| 243 | if (FD && FD->getPrimaryTemplate()) |
| 244 | mangleUnscopedTemplateName(FD); |
| 245 | else |
| 246 | mangleUnscopedName(ND); |
| Anders Carlsson | 1b42c79 | 2009-04-02 16:24:45 +0000 | [diff] [blame] | 247 | } else if (isa<FunctionDecl>(ND->getDeclContext())) |
| 248 | mangleLocalName(ND); |
| 249 | else |
| Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 250 | mangleNestedName(ND); |
| Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 251 | } |
| 252 | |
| Anders Carlsson | 201ce74 | 2009-09-17 03:17:01 +0000 | [diff] [blame^] | 253 | void CXXNameMangler::mangleUnscopedName(const NamedDecl *ND) { |
| 254 | // <unscoped-name> ::= <unqualified-name> |
| 255 | // ::= St <unqualified-name> # ::std:: |
| 256 | if (isStdNamespace(ND->getDeclContext())) |
| 257 | Out << "St"; |
| 258 | |
| 259 | mangleUnqualifiedName(ND); |
| 260 | } |
| 261 | |
| 262 | void CXXNameMangler::mangleUnscopedTemplateName(const FunctionDecl *FD) { |
| 263 | // <unscoped-template-name> ::= <unscoped-name> |
| 264 | // ::= <substitution> |
| 265 | mangleUnscopedName(FD); |
| 266 | } |
| 267 | |
| Mike Stump | 77ca8f6 | 2009-09-05 07:20:32 +0000 | [diff] [blame] | 268 | void CXXNameMangler::mangleCalloffset(int64_t nv, int64_t v) { |
| Mike Stump | 141c5af | 2009-09-02 00:25:38 +0000 | [diff] [blame] | 269 | // <call-offset> ::= h <nv-offset> _ |
| 270 | // ::= v <v-offset> _ |
| 271 | // <nv-offset> ::= <offset number> # non-virtual base override |
| 272 | // <v-offset> ::= <offset nubmer> _ <virtual offset number> |
| 273 | // # virtual base override, with vcall offset |
| Mike Stump | 77ca8f6 | 2009-09-05 07:20:32 +0000 | [diff] [blame] | 274 | if (v == 0) { |
| Mike Stump | 9124bcc | 2009-09-02 00:56:18 +0000 | [diff] [blame] | 275 | Out << "h"; |
| Mike Stump | 141c5af | 2009-09-02 00:25:38 +0000 | [diff] [blame] | 276 | if (nv < 0) { |
| 277 | Out << "n"; |
| 278 | nv = -nv; |
| 279 | } |
| 280 | Out << nv; |
| 281 | } else { |
| Mike Stump | 9124bcc | 2009-09-02 00:56:18 +0000 | [diff] [blame] | 282 | Out << "v"; |
| Mike Stump | 141c5af | 2009-09-02 00:25:38 +0000 | [diff] [blame] | 283 | if (nv < 0) { |
| 284 | Out << "n"; |
| 285 | nv = -nv; |
| 286 | } |
| 287 | Out << nv; |
| 288 | Out << "_"; |
| 289 | if (v < 0) { |
| 290 | Out << "n"; |
| 291 | v = -v; |
| 292 | } |
| 293 | Out << v; |
| 294 | } |
| 295 | Out << "_"; |
| Mike Stump | 9124bcc | 2009-09-02 00:56:18 +0000 | [diff] [blame] | 296 | } |
| 297 | |
| Mike Stump | dec025b | 2009-09-07 04:27:52 +0000 | [diff] [blame] | 298 | void CXXNameMangler::mangleThunk(const FunctionDecl *FD, int64_t nv, |
| 299 | int64_t v) { |
| Mike Stump | 9124bcc | 2009-09-02 00:56:18 +0000 | [diff] [blame] | 300 | // <special-name> ::= T <call-offset> <base encoding> |
| 301 | // # base is the nominal target function of thunk |
| Mike Stump | dec025b | 2009-09-07 04:27:52 +0000 | [diff] [blame] | 302 | Out << "_ZT"; |
| Mike Stump | 77ca8f6 | 2009-09-05 07:20:32 +0000 | [diff] [blame] | 303 | mangleCalloffset(nv, v); |
| Mike Stump | dec025b | 2009-09-07 04:27:52 +0000 | [diff] [blame] | 304 | mangleFunctionEncoding(FD); |
| Mike Stump | 9124bcc | 2009-09-02 00:56:18 +0000 | [diff] [blame] | 305 | } |
| 306 | |
| Mike Stump | dec025b | 2009-09-07 04:27:52 +0000 | [diff] [blame] | 307 | void CXXNameMangler::mangleCovariantThunk(const FunctionDecl *FD, |
| Mike Stump | 77ca8f6 | 2009-09-05 07:20:32 +0000 | [diff] [blame] | 308 | int64_t nv_t, int64_t v_t, |
| Mike Stump | 9124bcc | 2009-09-02 00:56:18 +0000 | [diff] [blame] | 309 | int64_t nv_r, int64_t v_r) { |
| 310 | // <special-name> ::= Tc <call-offset> <call-offset> <base encoding> |
| 311 | // # base is the nominal target function of thunk |
| 312 | // # first call-offset is 'this' adjustment |
| 313 | // # second call-offset is result adjustment |
| Mike Stump | dec025b | 2009-09-07 04:27:52 +0000 | [diff] [blame] | 314 | Out << "_ZTc"; |
| Mike Stump | 77ca8f6 | 2009-09-05 07:20:32 +0000 | [diff] [blame] | 315 | mangleCalloffset(nv_t, v_t); |
| 316 | mangleCalloffset(nv_r, v_r); |
| Mike Stump | dec025b | 2009-09-07 04:27:52 +0000 | [diff] [blame] | 317 | mangleFunctionEncoding(FD); |
| Mike Stump | 141c5af | 2009-09-02 00:25:38 +0000 | [diff] [blame] | 318 | } |
| 319 | |
| Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 320 | void CXXNameMangler::mangleUnqualifiedName(const NamedDecl *ND) { |
| 321 | // <unqualified-name> ::= <operator-name> |
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 322 | // ::= <ctor-dtor-name> |
| 323 | // ::= <source-name> |
| Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 324 | DeclarationName Name = ND->getDeclName(); |
| 325 | switch (Name.getNameKind()) { |
| 326 | case DeclarationName::Identifier: |
| 327 | mangleSourceName(Name.getAsIdentifierInfo()); |
| 328 | break; |
| 329 | |
| 330 | case DeclarationName::ObjCZeroArgSelector: |
| 331 | case DeclarationName::ObjCOneArgSelector: |
| 332 | case DeclarationName::ObjCMultiArgSelector: |
| 333 | assert(false && "Can't mangle Objective-C selector names here!"); |
| 334 | break; |
| 335 | |
| 336 | case DeclarationName::CXXConstructorName: |
| Anders Carlsson | 27ae536 | 2009-04-17 01:58:57 +0000 | [diff] [blame] | 337 | if (ND == Structor) |
| Mike Stump | 141c5af | 2009-09-02 00:25:38 +0000 | [diff] [blame] | 338 | // If the named decl is the C++ constructor we're mangling, use the type |
| 339 | // we were given. |
| Anders Carlsson | 27ae536 | 2009-04-17 01:58:57 +0000 | [diff] [blame] | 340 | mangleCXXCtorType(static_cast<CXXCtorType>(StructorType)); |
| Anders Carlsson | 3ac86b5 | 2009-04-15 05:36:58 +0000 | [diff] [blame] | 341 | else |
| 342 | // Otherwise, use the complete constructor name. This is relevant if a |
| 343 | // class with a constructor is declared within a constructor. |
| 344 | mangleCXXCtorType(Ctor_Complete); |
| Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 345 | break; |
| 346 | |
| 347 | case DeclarationName::CXXDestructorName: |
| Anders Carlsson | 27ae536 | 2009-04-17 01:58:57 +0000 | [diff] [blame] | 348 | if (ND == Structor) |
| Mike Stump | 141c5af | 2009-09-02 00:25:38 +0000 | [diff] [blame] | 349 | // If the named decl is the C++ destructor we're mangling, use the type we |
| 350 | // were given. |
| Anders Carlsson | 27ae536 | 2009-04-17 01:58:57 +0000 | [diff] [blame] | 351 | mangleCXXDtorType(static_cast<CXXDtorType>(StructorType)); |
| 352 | else |
| 353 | // Otherwise, use the complete destructor name. This is relevant if a |
| 354 | // class with a destructor is declared within a destructor. |
| 355 | mangleCXXDtorType(Dtor_Complete); |
| Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 356 | break; |
| 357 | |
| 358 | case DeclarationName::CXXConversionFunctionName: |
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 359 | // <operator-name> ::= cv <type> # (cast) |
| Douglas Gregor | 219cc61 | 2009-02-13 01:28:03 +0000 | [diff] [blame] | 360 | Out << "cv"; |
| 361 | mangleType(Context.getCanonicalType(Name.getCXXNameType())); |
| Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 362 | break; |
| 363 | |
| 364 | case DeclarationName::CXXOperatorName: |
| 365 | mangleOperatorName(Name.getCXXOverloadedOperator(), |
| 366 | cast<FunctionDecl>(ND)->getNumParams()); |
| 367 | break; |
| 368 | |
| 369 | case DeclarationName::CXXUsingDirective: |
| 370 | assert(false && "Can't mangle a using directive name!"); |
| Douglas Gregor | 219cc61 | 2009-02-13 01:28:03 +0000 | [diff] [blame] | 371 | break; |
| Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 372 | } |
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 373 | |
| Douglas Gregor | 1fd2dd1 | 2009-06-29 22:39:32 +0000 | [diff] [blame] | 374 | if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(ND)) { |
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 375 | if (const TemplateArgumentList *TemplateArgs |
| Douglas Gregor | 1fd2dd1 | 2009-06-29 22:39:32 +0000 | [diff] [blame] | 376 | = Function->getTemplateSpecializationArgs()) |
| 377 | mangleTemplateArgumentList(*TemplateArgs); |
| 378 | } |
| Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 379 | } |
| 380 | |
| 381 | void CXXNameMangler::mangleSourceName(const IdentifierInfo *II) { |
| 382 | // <source-name> ::= <positive length number> <identifier> |
| 383 | // <number> ::= [n] <non-negative decimal integer> |
| 384 | // <identifier> ::= <unqualified source code identifier> |
| 385 | Out << II->getLength() << II->getName(); |
| 386 | } |
| 387 | |
| 388 | void CXXNameMangler::mangleNestedName(const NamedDecl *ND) { |
| 389 | // <nested-name> ::= N [<CV-qualifiers>] <prefix> <unqualified-name> E |
| 390 | // ::= N [<CV-qualifiers>] <template-prefix> <template-args> E |
| 391 | // FIXME: no template support |
| 392 | Out << 'N'; |
| 393 | if (const CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(ND)) |
| 394 | mangleCVQualifiers(Method->getTypeQualifiers()); |
| 395 | manglePrefix(ND->getDeclContext()); |
| 396 | mangleUnqualifiedName(ND); |
| 397 | Out << 'E'; |
| 398 | } |
| 399 | |
| Anders Carlsson | 1b42c79 | 2009-04-02 16:24:45 +0000 | [diff] [blame] | 400 | void CXXNameMangler::mangleLocalName(const NamedDecl *ND) { |
| 401 | // <local-name> := Z <function encoding> E <entity name> [<discriminator>] |
| 402 | // := Z <function encoding> E s [<discriminator>] |
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 403 | // <discriminator> := _ <non-negative number> |
| Anders Carlsson | 1b42c79 | 2009-04-02 16:24:45 +0000 | [diff] [blame] | 404 | Out << 'Z'; |
| 405 | mangleFunctionEncoding(cast<FunctionDecl>(ND->getDeclContext())); |
| 406 | Out << 'E'; |
| 407 | mangleSourceName(ND->getIdentifier()); |
| 408 | } |
| 409 | |
| Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 410 | void CXXNameMangler::manglePrefix(const DeclContext *DC) { |
| 411 | // <prefix> ::= <prefix> <unqualified-name> |
| 412 | // ::= <template-prefix> <template-args> |
| 413 | // ::= <template-param> |
| 414 | // ::= # empty |
| 415 | // ::= <substitution> |
| 416 | // FIXME: We only handle mangling of namespaces and classes at the moment. |
| Anders Carlsson | c8dee9c | 2009-04-01 00:42:16 +0000 | [diff] [blame] | 417 | if (!DC->getParent()->isTranslationUnit()) |
| 418 | manglePrefix(DC->getParent()); |
| Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 419 | |
| 420 | if (const NamespaceDecl *Namespace = dyn_cast<NamespaceDecl>(DC)) |
| 421 | mangleSourceName(Namespace->getIdentifier()); |
| Anders Carlsson | 7a0ba87 | 2009-05-15 16:09:15 +0000 | [diff] [blame] | 422 | else if (const RecordDecl *Record = dyn_cast<RecordDecl>(DC)) { |
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 423 | if (const ClassTemplateSpecializationDecl *D = |
| Anders Carlsson | 7a0ba87 | 2009-05-15 16:09:15 +0000 | [diff] [blame] | 424 | dyn_cast<ClassTemplateSpecializationDecl>(Record)) { |
| 425 | mangleType(QualType(D->getTypeForDecl(), 0)); |
| 426 | } else |
| 427 | mangleSourceName(Record->getIdentifier()); |
| 428 | } |
| Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 429 | } |
| 430 | |
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 431 | void |
| Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 432 | CXXNameMangler::mangleOperatorName(OverloadedOperatorKind OO, unsigned Arity) { |
| 433 | switch (OO) { |
| Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 434 | // <operator-name> ::= nw # new |
| Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 435 | case OO_New: Out << "nw"; break; |
| 436 | // ::= na # new[] |
| 437 | case OO_Array_New: Out << "na"; break; |
| Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 438 | // ::= dl # delete |
| Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 439 | case OO_Delete: Out << "dl"; break; |
| Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 440 | // ::= da # delete[] |
| Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 441 | case OO_Array_Delete: Out << "da"; break; |
| 442 | // ::= ps # + (unary) |
| 443 | // ::= pl # + |
| 444 | case OO_Plus: Out << (Arity == 1? "ps" : "pl"); break; |
| Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 445 | // ::= ng # - (unary) |
| 446 | // ::= mi # - |
| Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 447 | case OO_Minus: Out << (Arity == 1? "ng" : "mi"); break; |
| Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 448 | // ::= ad # & (unary) |
| 449 | // ::= an # & |
| Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 450 | case OO_Amp: Out << (Arity == 1? "ad" : "an"); break; |
| Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 451 | // ::= de # * (unary) |
| 452 | // ::= ml # * |
| Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 453 | case OO_Star: Out << (Arity == 1? "de" : "ml"); break; |
| Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 454 | // ::= co # ~ |
| Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 455 | case OO_Tilde: Out << "co"; break; |
| Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 456 | // ::= dv # / |
| Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 457 | case OO_Slash: Out << "dv"; break; |
| Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 458 | // ::= rm # % |
| Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 459 | case OO_Percent: Out << "rm"; break; |
| Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 460 | // ::= or # | |
| 461 | case OO_Pipe: Out << "or"; break; |
| 462 | // ::= eo # ^ |
| Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 463 | case OO_Caret: Out << "eo"; break; |
| Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 464 | // ::= aS # = |
| Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 465 | case OO_Equal: Out << "aS"; break; |
| Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 466 | // ::= pL # += |
| Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 467 | case OO_PlusEqual: Out << "pL"; break; |
| Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 468 | // ::= mI # -= |
| Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 469 | case OO_MinusEqual: Out << "mI"; break; |
| Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 470 | // ::= mL # *= |
| Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 471 | case OO_StarEqual: Out << "mL"; break; |
| Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 472 | // ::= dV # /= |
| Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 473 | case OO_SlashEqual: Out << "dV"; break; |
| Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 474 | // ::= rM # %= |
| 475 | case OO_PercentEqual: Out << "rM"; break; |
| 476 | // ::= aN # &= |
| 477 | case OO_AmpEqual: Out << "aN"; break; |
| 478 | // ::= oR # |= |
| 479 | case OO_PipeEqual: Out << "oR"; break; |
| 480 | // ::= eO # ^= |
| 481 | case OO_CaretEqual: Out << "eO"; break; |
| 482 | // ::= ls # << |
| Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 483 | case OO_LessLess: Out << "ls"; break; |
| Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 484 | // ::= rs # >> |
| 485 | case OO_GreaterGreater: Out << "rs"; break; |
| 486 | // ::= lS # <<= |
| 487 | case OO_LessLessEqual: Out << "lS"; break; |
| 488 | // ::= rS # >>= |
| 489 | case OO_GreaterGreaterEqual: Out << "rS"; break; |
| Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 490 | // ::= eq # == |
| 491 | case OO_EqualEqual: Out << "eq"; break; |
| Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 492 | // ::= ne # != |
| 493 | case OO_ExclaimEqual: Out << "ne"; break; |
| 494 | // ::= lt # < |
| Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 495 | case OO_Less: Out << "lt"; break; |
| Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 496 | // ::= gt # > |
| Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 497 | case OO_Greater: Out << "gt"; break; |
| Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 498 | // ::= le # <= |
| Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 499 | case OO_LessEqual: Out << "le"; break; |
| Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 500 | // ::= ge # >= |
| Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 501 | case OO_GreaterEqual: Out << "ge"; break; |
| Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 502 | // ::= nt # ! |
| Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 503 | case OO_Exclaim: Out << "nt"; break; |
| Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 504 | // ::= aa # && |
| Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 505 | case OO_AmpAmp: Out << "aa"; break; |
| Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 506 | // ::= oo # || |
| 507 | case OO_PipePipe: Out << "oo"; break; |
| 508 | // ::= pp # ++ |
| 509 | case OO_PlusPlus: Out << "pp"; break; |
| 510 | // ::= mm # -- |
| Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 511 | case OO_MinusMinus: Out << "mm"; break; |
| Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 512 | // ::= cm # , |
| 513 | case OO_Comma: Out << "cm"; break; |
| 514 | // ::= pm # ->* |
| Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 515 | case OO_ArrowStar: Out << "pm"; break; |
| Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 516 | // ::= pt # -> |
| Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 517 | case OO_Arrow: Out << "pt"; break; |
| Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 518 | // ::= cl # () |
| Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 519 | case OO_Call: Out << "cl"; break; |
| Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 520 | // ::= ix # [] |
| Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 521 | case OO_Subscript: Out << "ix"; break; |
| 522 | // UNSUPPORTED: ::= qu # ? |
| 523 | |
| Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 524 | case OO_None: |
| 525 | case OO_Conditional: |
| Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 526 | case NUM_OVERLOADED_OPERATORS: |
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 527 | assert(false && "Not an overloaded operator"); |
| Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 528 | break; |
| 529 | } |
| 530 | } |
| 531 | |
| 532 | void CXXNameMangler::mangleCVQualifiers(unsigned Quals) { |
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 533 | // <CV-qualifiers> ::= [r] [V] [K] # restrict (C99), volatile, const |
| Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 534 | if (Quals & QualType::Restrict) |
| 535 | Out << 'r'; |
| 536 | if (Quals & QualType::Volatile) |
| 537 | Out << 'V'; |
| 538 | if (Quals & QualType::Const) |
| 539 | Out << 'K'; |
| 540 | } |
| 541 | |
| 542 | void CXXNameMangler::mangleType(QualType T) { |
| Anders Carlsson | 4843e58 | 2009-03-10 17:07:44 +0000 | [diff] [blame] | 543 | // Only operate on the canonical type! |
| 544 | T = Context.getCanonicalType(T); |
| 545 | |
| Anders Carlsson | 7696737 | 2009-09-17 00:43:46 +0000 | [diff] [blame] | 546 | bool IsSubstitutable = !isa<BuiltinType>(T); |
| 547 | if (IsSubstitutable && mangleSubstitution(T)) |
| 548 | return; |
| Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 549 | |
| Anders Carlsson | 7696737 | 2009-09-17 00:43:46 +0000 | [diff] [blame] | 550 | if (unsigned CVRQualifiers = T.getCVRQualifiers()) { |
| 551 | // <type> ::= <CV-qualifiers> <type> |
| 552 | mangleCVQualifiers(CVRQualifiers); |
| 553 | |
| 554 | mangleType(T.getUnqualifiedType()); |
| 555 | } else { |
| 556 | switch (T->getTypeClass()) { |
| John McCall | efe6aee | 2009-09-05 07:56:18 +0000 | [diff] [blame] | 557 | #define ABSTRACT_TYPE(CLASS, PARENT) |
| 558 | #define NON_CANONICAL_TYPE(CLASS, PARENT) \ |
| Anders Carlsson | 7696737 | 2009-09-17 00:43:46 +0000 | [diff] [blame] | 559 | case Type::CLASS: \ |
| 560 | llvm::llvm_unreachable("can't mangle non-canonical type " #CLASS "Type"); \ |
| 561 | return; |
| John McCall | efe6aee | 2009-09-05 07:56:18 +0000 | [diff] [blame] | 562 | #define TYPE(CLASS, PARENT) \ |
| Anders Carlsson | 7696737 | 2009-09-17 00:43:46 +0000 | [diff] [blame] | 563 | case Type::CLASS: \ |
| 564 | mangleType(static_cast<CLASS##Type*>(T.getTypePtr())); \ |
| 565 | break; |
| John McCall | efe6aee | 2009-09-05 07:56:18 +0000 | [diff] [blame] | 566 | #include "clang/AST/TypeNodes.def" |
| Anders Carlsson | 7696737 | 2009-09-17 00:43:46 +0000 | [diff] [blame] | 567 | } |
| Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 568 | } |
| Anders Carlsson | 7696737 | 2009-09-17 00:43:46 +0000 | [diff] [blame] | 569 | |
| 570 | // Add the substitution. |
| 571 | if (IsSubstitutable) |
| 572 | addSubstitution(T); |
| Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 573 | } |
| 574 | |
| 575 | void CXXNameMangler::mangleType(const BuiltinType *T) { |
| John McCall | efe6aee | 2009-09-05 07:56:18 +0000 | [diff] [blame] | 576 | // <type> ::= <builtin-type> |
| Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 577 | // <builtin-type> ::= v # void |
| 578 | // ::= w # wchar_t |
| 579 | // ::= b # bool |
| 580 | // ::= c # char |
| 581 | // ::= a # signed char |
| 582 | // ::= h # unsigned char |
| 583 | // ::= s # short |
| 584 | // ::= t # unsigned short |
| 585 | // ::= i # int |
| 586 | // ::= j # unsigned int |
| 587 | // ::= l # long |
| 588 | // ::= m # unsigned long |
| 589 | // ::= x # long long, __int64 |
| 590 | // ::= y # unsigned long long, __int64 |
| 591 | // ::= n # __int128 |
| 592 | // UNSUPPORTED: ::= o # unsigned __int128 |
| 593 | // ::= f # float |
| 594 | // ::= d # double |
| 595 | // ::= e # long double, __float80 |
| 596 | // UNSUPPORTED: ::= g # __float128 |
| Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 597 | // UNSUPPORTED: ::= Dd # IEEE 754r decimal floating point (64 bits) |
| 598 | // UNSUPPORTED: ::= De # IEEE 754r decimal floating point (128 bits) |
| 599 | // UNSUPPORTED: ::= Df # IEEE 754r decimal floating point (32 bits) |
| 600 | // UNSUPPORTED: ::= Dh # IEEE 754r half-precision floating point (16 bits) |
| Alisdair Meredith | f5c209d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 601 | // ::= Di # char32_t |
| 602 | // ::= Ds # char16_t |
| Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 603 | // ::= u <source-name> # vendor extended type |
| Sebastian Redl | 6e8ed16 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 604 | // From our point of view, std::nullptr_t is a builtin, but as far as mangling |
| 605 | // is concerned, it's a type called std::nullptr_t. |
| Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 606 | switch (T->getKind()) { |
| 607 | case BuiltinType::Void: Out << 'v'; break; |
| 608 | case BuiltinType::Bool: Out << 'b'; break; |
| 609 | case BuiltinType::Char_U: case BuiltinType::Char_S: Out << 'c'; break; |
| 610 | case BuiltinType::UChar: Out << 'h'; break; |
| 611 | case BuiltinType::UShort: Out << 't'; break; |
| 612 | case BuiltinType::UInt: Out << 'j'; break; |
| 613 | case BuiltinType::ULong: Out << 'm'; break; |
| 614 | case BuiltinType::ULongLong: Out << 'y'; break; |
| Chris Lattner | 2df9ced | 2009-04-30 02:43:43 +0000 | [diff] [blame] | 615 | case BuiltinType::UInt128: Out << 'o'; break; |
| Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 616 | case BuiltinType::SChar: Out << 'a'; break; |
| 617 | case BuiltinType::WChar: Out << 'w'; break; |
| Alisdair Meredith | f5c209d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 618 | case BuiltinType::Char16: Out << "Ds"; break; |
| 619 | case BuiltinType::Char32: Out << "Di"; break; |
| Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 620 | case BuiltinType::Short: Out << 's'; break; |
| 621 | case BuiltinType::Int: Out << 'i'; break; |
| 622 | case BuiltinType::Long: Out << 'l'; break; |
| 623 | case BuiltinType::LongLong: Out << 'x'; break; |
| Chris Lattner | 2df9ced | 2009-04-30 02:43:43 +0000 | [diff] [blame] | 624 | case BuiltinType::Int128: Out << 'n'; break; |
| Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 625 | case BuiltinType::Float: Out << 'f'; break; |
| 626 | case BuiltinType::Double: Out << 'd'; break; |
| 627 | case BuiltinType::LongDouble: Out << 'e'; break; |
| Sebastian Redl | 6e8ed16 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 628 | case BuiltinType::NullPtr: Out << "St9nullptr_t"; break; |
| Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 629 | |
| 630 | case BuiltinType::Overload: |
| 631 | case BuiltinType::Dependent: |
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 632 | assert(false && |
| Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 633 | "Overloaded and dependent types shouldn't get to name mangling"); |
| 634 | break; |
| Anders Carlsson | e89d159 | 2009-06-26 18:41:36 +0000 | [diff] [blame] | 635 | case BuiltinType::UndeducedAuto: |
| 636 | assert(0 && "Should not see undeduced auto here"); |
| 637 | break; |
| Steve Naroff | 9533a7f | 2009-07-22 17:14:51 +0000 | [diff] [blame] | 638 | case BuiltinType::ObjCId: Out << "11objc_object"; break; |
| 639 | case BuiltinType::ObjCClass: Out << "10objc_class"; break; |
| Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 640 | } |
| 641 | } |
| 642 | |
| John McCall | efe6aee | 2009-09-05 07:56:18 +0000 | [diff] [blame] | 643 | // <type> ::= <function-type> |
| 644 | // <function-type> ::= F [Y] <bare-function-type> E |
| 645 | void CXXNameMangler::mangleType(const FunctionProtoType *T) { |
| Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 646 | Out << 'F'; |
| Mike Stump | f5408fe | 2009-05-16 07:57:57 +0000 | [diff] [blame] | 647 | // FIXME: We don't have enough information in the AST to produce the 'Y' |
| 648 | // encoding for extern "C" function types. |
| Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 649 | mangleBareFunctionType(T, /*MangleReturnType=*/true); |
| 650 | Out << 'E'; |
| 651 | } |
| John McCall | efe6aee | 2009-09-05 07:56:18 +0000 | [diff] [blame] | 652 | void CXXNameMangler::mangleType(const FunctionNoProtoType *T) { |
| 653 | llvm::llvm_unreachable("Can't mangle K&R function prototypes"); |
| 654 | } |
| Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 655 | void CXXNameMangler::mangleBareFunctionType(const FunctionType *T, |
| 656 | bool MangleReturnType) { |
| John McCall | efe6aee | 2009-09-05 07:56:18 +0000 | [diff] [blame] | 657 | // We should never be mangling something without a prototype. |
| 658 | const FunctionProtoType *Proto = cast<FunctionProtoType>(T); |
| 659 | |
| Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 660 | // <bare-function-type> ::= <signature type>+ |
| 661 | if (MangleReturnType) |
| John McCall | efe6aee | 2009-09-05 07:56:18 +0000 | [diff] [blame] | 662 | mangleType(Proto->getResultType()); |
| Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 663 | |
| Anders Carlsson | c6c91bc | 2009-04-01 00:15:23 +0000 | [diff] [blame] | 664 | if (Proto->getNumArgs() == 0) { |
| 665 | Out << 'v'; |
| 666 | return; |
| 667 | } |
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 668 | |
| Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 669 | for (FunctionProtoType::arg_type_iterator Arg = Proto->arg_type_begin(), |
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 670 | ArgEnd = Proto->arg_type_end(); |
| Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 671 | Arg != ArgEnd; ++Arg) |
| 672 | mangleType(*Arg); |
| Douglas Gregor | 219cc61 | 2009-02-13 01:28:03 +0000 | [diff] [blame] | 673 | |
| 674 | // <builtin-type> ::= z # ellipsis |
| 675 | if (Proto->isVariadic()) |
| 676 | Out << 'z'; |
| Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 677 | } |
| 678 | |
| John McCall | efe6aee | 2009-09-05 07:56:18 +0000 | [diff] [blame] | 679 | // <type> ::= <class-enum-type> |
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 680 | // <class-enum-type> ::= <name> |
| John McCall | efe6aee | 2009-09-05 07:56:18 +0000 | [diff] [blame] | 681 | void CXXNameMangler::mangleType(const EnumType *T) { |
| 682 | mangleType(static_cast<const TagType*>(T)); |
| 683 | } |
| 684 | void CXXNameMangler::mangleType(const RecordType *T) { |
| 685 | mangleType(static_cast<const TagType*>(T)); |
| 686 | } |
| Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 687 | void CXXNameMangler::mangleType(const TagType *T) { |
| Anders Carlsson | 4843e58 | 2009-03-10 17:07:44 +0000 | [diff] [blame] | 688 | if (!T->getDecl()->getIdentifier()) |
| 689 | mangleName(T->getDecl()->getTypedefForAnonDecl()); |
| 690 | else |
| 691 | mangleName(T->getDecl()); |
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 692 | |
| Mike Stump | 141c5af | 2009-09-02 00:25:38 +0000 | [diff] [blame] | 693 | // If this is a class template specialization, mangle the template arguments. |
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 694 | if (ClassTemplateSpecializationDecl *Spec |
| Anders Carlsson | 7a0ba87 | 2009-05-15 16:09:15 +0000 | [diff] [blame] | 695 | = dyn_cast<ClassTemplateSpecializationDecl>(T->getDecl())) |
| 696 | mangleTemplateArgumentList(Spec->getTemplateArgs()); |
| Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 697 | } |
| 698 | |
| John McCall | efe6aee | 2009-09-05 07:56:18 +0000 | [diff] [blame] | 699 | // <type> ::= <array-type> |
| 700 | // <array-type> ::= A <positive dimension number> _ <element type> |
| 701 | // ::= A [<dimension expression>] _ <element type> |
| 702 | void CXXNameMangler::mangleType(const ConstantArrayType *T) { |
| 703 | Out << 'A' << T->getSize() << '_'; |
| 704 | mangleType(T->getElementType()); |
| 705 | } |
| 706 | void CXXNameMangler::mangleType(const VariableArrayType *T) { |
| Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 707 | Out << 'A'; |
| John McCall | efe6aee | 2009-09-05 07:56:18 +0000 | [diff] [blame] | 708 | mangleExpression(T->getSizeExpr()); |
| Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 709 | Out << '_'; |
| 710 | mangleType(T->getElementType()); |
| 711 | } |
| John McCall | efe6aee | 2009-09-05 07:56:18 +0000 | [diff] [blame] | 712 | void CXXNameMangler::mangleType(const DependentSizedArrayType *T) { |
| 713 | Out << 'A'; |
| 714 | mangleExpression(T->getSizeExpr()); |
| 715 | Out << '_'; |
| 716 | mangleType(T->getElementType()); |
| 717 | } |
| 718 | void CXXNameMangler::mangleType(const IncompleteArrayType *T) { |
| 719 | Out << 'A' << '_'; |
| 720 | mangleType(T->getElementType()); |
| 721 | } |
| Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 722 | |
| John McCall | efe6aee | 2009-09-05 07:56:18 +0000 | [diff] [blame] | 723 | // <type> ::= <pointer-to-member-type> |
| 724 | // <pointer-to-member-type> ::= M <class type> <member type> |
| Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 725 | void CXXNameMangler::mangleType(const MemberPointerType *T) { |
| Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 726 | Out << 'M'; |
| 727 | mangleType(QualType(T->getClass(), 0)); |
| Anders Carlsson | 0e65001 | 2009-05-17 17:41:20 +0000 | [diff] [blame] | 728 | QualType PointeeType = T->getPointeeType(); |
| 729 | if (const FunctionProtoType *FPT = dyn_cast<FunctionProtoType>(PointeeType)) { |
| 730 | mangleCVQualifiers(FPT->getTypeQuals()); |
| 731 | mangleType(FPT); |
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 732 | } else |
| Anders Carlsson | 0e65001 | 2009-05-17 17:41:20 +0000 | [diff] [blame] | 733 | mangleType(PointeeType); |
| Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 734 | } |
| 735 | |
| John McCall | efe6aee | 2009-09-05 07:56:18 +0000 | [diff] [blame] | 736 | // <type> ::= <template-param> |
| 737 | // <template-param> ::= T_ # first template parameter |
| 738 | // ::= T <parameter-2 non-negative number> _ |
| Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 739 | void CXXNameMangler::mangleType(const TemplateTypeParmType *T) { |
| Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 740 | if (T->getIndex() == 0) |
| 741 | Out << "T_"; |
| 742 | else |
| 743 | Out << 'T' << (T->getIndex() - 1) << '_'; |
| 744 | } |
| 745 | |
| John McCall | efe6aee | 2009-09-05 07:56:18 +0000 | [diff] [blame] | 746 | // FIXME: <type> ::= <template-template-param> <template-args> |
| 747 | // FIXME: <type> ::= <substitution> # See Compression below |
| 748 | |
| 749 | // <type> ::= P <type> # pointer-to |
| 750 | void CXXNameMangler::mangleType(const PointerType *T) { |
| 751 | Out << 'P'; |
| 752 | mangleType(T->getPointeeType()); |
| 753 | } |
| 754 | void CXXNameMangler::mangleType(const ObjCObjectPointerType *T) { |
| 755 | Out << 'P'; |
| 756 | mangleType(T->getPointeeType()); |
| 757 | } |
| 758 | |
| 759 | // <type> ::= R <type> # reference-to |
| 760 | void CXXNameMangler::mangleType(const LValueReferenceType *T) { |
| 761 | Out << 'R'; |
| 762 | mangleType(T->getPointeeType()); |
| 763 | } |
| 764 | |
| 765 | // <type> ::= O <type> # rvalue reference-to (C++0x) |
| 766 | void CXXNameMangler::mangleType(const RValueReferenceType *T) { |
| 767 | Out << 'O'; |
| 768 | mangleType(T->getPointeeType()); |
| 769 | } |
| 770 | |
| 771 | // <type> ::= C <type> # complex pair (C 2000) |
| 772 | void CXXNameMangler::mangleType(const ComplexType *T) { |
| 773 | Out << 'C'; |
| 774 | mangleType(T->getElementType()); |
| 775 | } |
| 776 | |
| 777 | // GNU extension: vector types |
| 778 | void CXXNameMangler::mangleType(const VectorType *T) { |
| 779 | Out << "U8__vector"; |
| 780 | mangleType(T->getElementType()); |
| 781 | } |
| 782 | void CXXNameMangler::mangleType(const ExtVectorType *T) { |
| 783 | mangleType(static_cast<const VectorType*>(T)); |
| 784 | } |
| 785 | void CXXNameMangler::mangleType(const DependentSizedExtVectorType *T) { |
| 786 | Out << "U8__vector"; |
| 787 | mangleType(T->getElementType()); |
| 788 | } |
| 789 | |
| Anders Carlsson | a40c5e4 | 2009-03-07 22:03:21 +0000 | [diff] [blame] | 790 | void CXXNameMangler::mangleType(const ObjCInterfaceType *T) { |
| 791 | mangleSourceName(T->getDecl()->getIdentifier()); |
| 792 | } |
| 793 | |
| John McCall | efe6aee | 2009-09-05 07:56:18 +0000 | [diff] [blame] | 794 | void CXXNameMangler::mangleType(const BlockPointerType *T) { |
| 795 | assert(false && "can't mangle block pointer types yet"); |
| 796 | } |
| 797 | |
| 798 | void CXXNameMangler::mangleType(const FixedWidthIntType *T) { |
| 799 | assert(false && "can't mangle arbitary-precision integer type yet"); |
| 800 | } |
| 801 | |
| 802 | void CXXNameMangler::mangleType(const TemplateSpecializationType *T) { |
| 803 | // TSTs are never canonical unless they're dependent. |
| 804 | assert(false && "can't mangle dependent template specializations yet"); |
| 805 | } |
| 806 | |
| 807 | void CXXNameMangler::mangleType(const TypenameType *T) { |
| 808 | assert(false && "can't mangle dependent typenames yet"); |
| 809 | } |
| 810 | |
| 811 | // FIXME: For now, just drop all extension qualifiers on the floor. |
| 812 | void CXXNameMangler::mangleType(const ExtQualType *T) { |
| 813 | mangleType(QualType(T->getBaseType(), 0)); |
| 814 | } |
| 815 | |
| Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 816 | void CXXNameMangler::mangleExpression(Expr *E) { |
| 817 | assert(false && "Cannot mangle expressions yet"); |
| 818 | } |
| 819 | |
| John McCall | efe6aee | 2009-09-05 07:56:18 +0000 | [diff] [blame] | 820 | // FIXME: <type> ::= G <type> # imaginary (C 2000) |
| 821 | // FIXME: <type> ::= U <source-name> <type> # vendor extended type qualifier |
| 822 | |
| Anders Carlsson | 3ac86b5 | 2009-04-15 05:36:58 +0000 | [diff] [blame] | 823 | void CXXNameMangler::mangleCXXCtorType(CXXCtorType T) { |
| 824 | // <ctor-dtor-name> ::= C1 # complete object constructor |
| 825 | // ::= C2 # base object constructor |
| 826 | // ::= C3 # complete object allocating constructor |
| 827 | // |
| 828 | switch (T) { |
| 829 | case Ctor_Complete: |
| 830 | Out << "C1"; |
| 831 | break; |
| 832 | case Ctor_Base: |
| 833 | Out << "C2"; |
| 834 | break; |
| 835 | case Ctor_CompleteAllocating: |
| 836 | Out << "C3"; |
| 837 | break; |
| 838 | } |
| 839 | } |
| 840 | |
| Anders Carlsson | 27ae536 | 2009-04-17 01:58:57 +0000 | [diff] [blame] | 841 | void CXXNameMangler::mangleCXXDtorType(CXXDtorType T) { |
| 842 | // <ctor-dtor-name> ::= D0 # deleting destructor |
| 843 | // ::= D1 # complete object destructor |
| 844 | // ::= D2 # base object destructor |
| 845 | // |
| 846 | switch (T) { |
| 847 | case Dtor_Deleting: |
| 848 | Out << "D0"; |
| 849 | break; |
| 850 | case Dtor_Complete: |
| 851 | Out << "D1"; |
| 852 | break; |
| 853 | case Dtor_Base: |
| 854 | Out << "D2"; |
| 855 | break; |
| 856 | } |
| 857 | } |
| 858 | |
| Anders Carlsson | 7a0ba87 | 2009-05-15 16:09:15 +0000 | [diff] [blame] | 859 | void CXXNameMangler::mangleTemplateArgumentList(const TemplateArgumentList &L) { |
| 860 | // <template-args> ::= I <template-arg>+ E |
| 861 | Out << "I"; |
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 862 | |
| Anders Carlsson | 7a0ba87 | 2009-05-15 16:09:15 +0000 | [diff] [blame] | 863 | for (unsigned i = 0, e = L.size(); i != e; ++i) { |
| 864 | const TemplateArgument &A = L[i]; |
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 865 | |
| Anders Carlsson | 7a0ba87 | 2009-05-15 16:09:15 +0000 | [diff] [blame] | 866 | mangleTemplateArgument(A); |
| 867 | } |
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 868 | |
| Anders Carlsson | 7a0ba87 | 2009-05-15 16:09:15 +0000 | [diff] [blame] | 869 | Out << "E"; |
| 870 | } |
| 871 | |
| 872 | void CXXNameMangler::mangleTemplateArgument(const TemplateArgument &A) { |
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 873 | // <template-arg> ::= <type> # type or template |
| Anders Carlsson | 7a0ba87 | 2009-05-15 16:09:15 +0000 | [diff] [blame] | 874 | // ::= X <expression> E # expression |
| 875 | // ::= <expr-primary> # simple expressions |
| 876 | // ::= I <template-arg>* E # argument pack |
| 877 | // ::= sp <expression> # pack expansion of (C++0x) |
| 878 | switch (A.getKind()) { |
| 879 | default: |
| 880 | assert(0 && "Unknown template argument kind!"); |
| 881 | case TemplateArgument::Type: |
| 882 | mangleType(A.getAsType()); |
| 883 | break; |
| 884 | case TemplateArgument::Integral: |
| 885 | // <expr-primary> ::= L <type> <value number> E # integer literal |
| 886 | |
| 887 | Out << 'L'; |
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 888 | |
| Anders Carlsson | 7a0ba87 | 2009-05-15 16:09:15 +0000 | [diff] [blame] | 889 | mangleType(A.getIntegralType()); |
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 890 | |
| Anders Carlsson | 7a0ba87 | 2009-05-15 16:09:15 +0000 | [diff] [blame] | 891 | const llvm::APSInt *Integral = A.getAsIntegral(); |
| 892 | if (A.getIntegralType()->isBooleanType()) { |
| 893 | // Boolean values are encoded as 0/1. |
| 894 | Out << (Integral->getBoolValue() ? '1' : '0'); |
| 895 | } else { |
| 896 | if (Integral->isNegative()) |
| 897 | Out << 'n'; |
| 898 | Integral->abs().print(Out, false); |
| 899 | } |
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 900 | |
| Anders Carlsson | 7a0ba87 | 2009-05-15 16:09:15 +0000 | [diff] [blame] | 901 | Out << 'E'; |
| 902 | break; |
| 903 | } |
| 904 | } |
| 905 | |
| Anders Carlsson | 7696737 | 2009-09-17 00:43:46 +0000 | [diff] [blame] | 906 | // <substitution> ::= S <seq-id> _ |
| 907 | // ::= S_ |
| 908 | bool CXXNameMangler::mangleSubstitution(QualType T) { |
| 909 | uintptr_t TypePtr = reinterpret_cast<uintptr_t>(T.getAsOpaquePtr()); |
| 910 | |
| 911 | llvm::DenseMap<uintptr_t, unsigned>::iterator I = |
| 912 | Substitutions.find(TypePtr); |
| 913 | if (I == Substitutions.end()) |
| 914 | return false; |
| 915 | |
| 916 | unsigned SeqID = I->second; |
| 917 | if (SeqID == 0) |
| 918 | Out << "S_"; |
| 919 | else { |
| 920 | SeqID--; |
| 921 | |
| 922 | // <seq-id> is encoded in base-36, using digits and upper case letters. |
| 923 | char Buffer[10]; |
| 924 | char *BufferPtr = Buffer + 9; |
| 925 | |
| 926 | *BufferPtr = 0; |
| 927 | if (SeqID == 0) *--BufferPtr = '0'; |
| 928 | |
| 929 | while (SeqID) { |
| 930 | assert(BufferPtr > Buffer && "Buffer overflow!"); |
| 931 | |
| 932 | unsigned char c = static_cast<unsigned char>(SeqID) % 36; |
| 933 | |
| 934 | *--BufferPtr = (c < 10 ? '0' + c : 'A' + c - 10); |
| 935 | SeqID /= 36; |
| 936 | } |
| 937 | |
| 938 | Out << 'S' << BufferPtr << '_'; |
| 939 | } |
| 940 | |
| 941 | return true; |
| 942 | } |
| 943 | |
| 944 | void CXXNameMangler::addSubstitution(QualType T) { |
| 945 | uintptr_t TypePtr = reinterpret_cast<uintptr_t>(T.getAsOpaquePtr()); |
| 946 | unsigned SeqID = Substitutions.size(); |
| 947 | |
| 948 | assert(!Substitutions.count(TypePtr) && "Substitution already exists!"); |
| 949 | Substitutions[TypePtr] = SeqID; |
| 950 | } |
| 951 | |
| Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 952 | namespace clang { |
| Mike Stump | 141c5af | 2009-09-02 00:25:38 +0000 | [diff] [blame] | 953 | /// \brief Mangles the name of the declaration D and emits that name to the |
| 954 | /// given output stream. |
| Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 955 | /// |
| Mike Stump | 141c5af | 2009-09-02 00:25:38 +0000 | [diff] [blame] | 956 | /// If the declaration D requires a mangled name, this routine will emit that |
| 957 | /// mangled name to \p os and return true. Otherwise, \p os will be unchanged |
| 958 | /// and this routine will return false. In this case, the caller should just |
| 959 | /// emit the identifier of the declaration (\c D->getIdentifier()) as its |
| 960 | /// name. |
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 961 | bool mangleName(const NamedDecl *D, ASTContext &Context, |
| Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 962 | llvm::raw_ostream &os) { |
| Anders Carlsson | 578aa64 | 2009-05-03 16:51:04 +0000 | [diff] [blame] | 963 | assert(!isa<CXXConstructorDecl>(D) && |
| 964 | "Use mangleCXXCtor for constructor decls!"); |
| 965 | assert(!isa<CXXDestructorDecl>(D) && |
| 966 | "Use mangleCXXDtor for destructor decls!"); |
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 967 | |
| Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 968 | CXXNameMangler Mangler(Context, os); |
| Douglas Gregor | 6ec3668 | 2009-02-18 23:53:56 +0000 | [diff] [blame] | 969 | if (!Mangler.mangle(D)) |
| 970 | return false; |
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 971 | |
| Douglas Gregor | 6ec3668 | 2009-02-18 23:53:56 +0000 | [diff] [blame] | 972 | os.flush(); |
| 973 | return true; |
| Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 974 | } |
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 975 | |
| Mike Stump | 141c5af | 2009-09-02 00:25:38 +0000 | [diff] [blame] | 976 | /// \brief Mangles the a thunk with the offset n for the declaration D and |
| 977 | /// emits that name to the given output stream. |
| Mike Stump | dec025b | 2009-09-07 04:27:52 +0000 | [diff] [blame] | 978 | void mangleThunk(const FunctionDecl *FD, int64_t nv, int64_t v, |
| Mike Stump | 883f127 | 2009-09-02 00:28:47 +0000 | [diff] [blame] | 979 | ASTContext &Context, llvm::raw_ostream &os) { |
| Mike Stump | 141c5af | 2009-09-02 00:25:38 +0000 | [diff] [blame] | 980 | // FIXME: Hum, we might have to thunk these, fix. |
| Mike Stump | dec025b | 2009-09-07 04:27:52 +0000 | [diff] [blame] | 981 | assert(!isa<CXXDestructorDecl>(FD) && |
| Mike Stump | 141c5af | 2009-09-02 00:25:38 +0000 | [diff] [blame] | 982 | "Use mangleCXXDtor for destructor decls!"); |
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 983 | |
| Mike Stump | 141c5af | 2009-09-02 00:25:38 +0000 | [diff] [blame] | 984 | CXXNameMangler Mangler(Context, os); |
| Mike Stump | dec025b | 2009-09-07 04:27:52 +0000 | [diff] [blame] | 985 | Mangler.mangleThunk(FD, nv, v); |
| Mike Stump | 141c5af | 2009-09-02 00:25:38 +0000 | [diff] [blame] | 986 | os.flush(); |
| 987 | } |
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 988 | |
| Mike Stump | 9124bcc | 2009-09-02 00:56:18 +0000 | [diff] [blame] | 989 | /// \brief Mangles the a covariant thunk for the declaration D and emits that |
| 990 | /// name to the given output stream. |
| Mike Stump | dec025b | 2009-09-07 04:27:52 +0000 | [diff] [blame] | 991 | void mangleCovariantThunk(const FunctionDecl *FD, int64_t nv_t, int64_t v_t, |
| Mike Stump | 77ca8f6 | 2009-09-05 07:20:32 +0000 | [diff] [blame] | 992 | int64_t nv_r, int64_t v_r, ASTContext &Context, |
| Mike Stump | 9124bcc | 2009-09-02 00:56:18 +0000 | [diff] [blame] | 993 | llvm::raw_ostream &os) { |
| 994 | // FIXME: Hum, we might have to thunk these, fix. |
| Mike Stump | dec025b | 2009-09-07 04:27:52 +0000 | [diff] [blame] | 995 | assert(!isa<CXXDestructorDecl>(FD) && |
| Mike Stump | 9124bcc | 2009-09-02 00:56:18 +0000 | [diff] [blame] | 996 | "Use mangleCXXDtor for destructor decls!"); |
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 997 | |
| Mike Stump | 9124bcc | 2009-09-02 00:56:18 +0000 | [diff] [blame] | 998 | CXXNameMangler Mangler(Context, os); |
| Mike Stump | dec025b | 2009-09-07 04:27:52 +0000 | [diff] [blame] | 999 | Mangler.mangleCovariantThunk(FD, nv_t, v_t, nv_r, v_r); |
| Mike Stump | 9124bcc | 2009-09-02 00:56:18 +0000 | [diff] [blame] | 1000 | os.flush(); |
| 1001 | } |
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1002 | |
| Anders Carlsson | 3ac86b5 | 2009-04-15 05:36:58 +0000 | [diff] [blame] | 1003 | /// mangleGuardVariable - Returns the mangled name for a guard variable |
| 1004 | /// for the passed in VarDecl. |
| Anders Carlsson | 41aa8c1 | 2009-04-13 18:02:10 +0000 | [diff] [blame] | 1005 | void mangleGuardVariable(const VarDecl *D, ASTContext &Context, |
| 1006 | llvm::raw_ostream &os) { |
| 1007 | CXXNameMangler Mangler(Context, os); |
| 1008 | Mangler.mangleGuardVariable(D); |
| 1009 | |
| 1010 | os.flush(); |
| 1011 | } |
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1012 | |
| Anders Carlsson | 3ac86b5 | 2009-04-15 05:36:58 +0000 | [diff] [blame] | 1013 | void mangleCXXCtor(const CXXConstructorDecl *D, CXXCtorType Type, |
| 1014 | ASTContext &Context, llvm::raw_ostream &os) { |
| 1015 | CXXNameMangler Mangler(Context, os); |
| 1016 | Mangler.mangleCXXCtor(D, Type); |
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1017 | |
| Anders Carlsson | 3ac86b5 | 2009-04-15 05:36:58 +0000 | [diff] [blame] | 1018 | os.flush(); |
| 1019 | } |
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1020 | |
| Anders Carlsson | 27ae536 | 2009-04-17 01:58:57 +0000 | [diff] [blame] | 1021 | void mangleCXXDtor(const CXXDestructorDecl *D, CXXDtorType Type, |
| 1022 | ASTContext &Context, llvm::raw_ostream &os) { |
| 1023 | CXXNameMangler Mangler(Context, os); |
| 1024 | Mangler.mangleCXXDtor(D, Type); |
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1025 | |
| Anders Carlsson | 27ae536 | 2009-04-17 01:58:57 +0000 | [diff] [blame] | 1026 | os.flush(); |
| 1027 | } |
| Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 1028 | |
| Mike Stump | f121677 | 2009-07-31 18:25:34 +0000 | [diff] [blame] | 1029 | void mangleCXXVtable(QualType Type, ASTContext &Context, |
| 1030 | llvm::raw_ostream &os) { |
| 1031 | CXXNameMangler Mangler(Context, os); |
| 1032 | Mangler.mangleCXXVtable(Type); |
| 1033 | |
| 1034 | os.flush(); |
| 1035 | } |
| Mike Stump | 738f8c2 | 2009-07-31 23:15:31 +0000 | [diff] [blame] | 1036 | |
| 1037 | void mangleCXXRtti(QualType Type, ASTContext &Context, |
| 1038 | llvm::raw_ostream &os) { |
| 1039 | CXXNameMangler Mangler(Context, os); |
| 1040 | Mangler.mangleCXXRtti(Type); |
| 1041 | |
| 1042 | os.flush(); |
| 1043 | } |
| Mike Stump | f121677 | 2009-07-31 18:25:34 +0000 | [diff] [blame] | 1044 | } |