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