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