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