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