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