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