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