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