Douglas Gregor | 6ec3668 | 2009-02-18 23:53:56 +0000 | [diff] [blame] | 1 | //===--- Mangle.cpp - Mangle C++ Names --------------------------*- C++ -*-===// |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // Implements C++ name mangling according to the Itanium C++ ABI, |
| 11 | // which is used in GCC 3.2 and newer (and many compilers that are |
| 12 | // ABI-compatible with GCC): |
| 13 | // |
| 14 | // http://www.codesourcery.com/public/cxx-abi/abi.html |
| 15 | // |
| 16 | //===----------------------------------------------------------------------===// |
| 17 | #include "Mangle.h" |
| 18 | #include "clang/AST/ASTContext.h" |
| 19 | #include "clang/AST/Decl.h" |
| 20 | #include "clang/AST/DeclCXX.h" |
Anders Carlsson | a40c5e4 | 2009-03-07 22:03:21 +0000 | [diff] [blame] | 21 | #include "clang/AST/DeclObjC.h" |
Anders Carlsson | 7a0ba87 | 2009-05-15 16:09:15 +0000 | [diff] [blame] | 22 | #include "clang/AST/DeclTemplate.h" |
Anders Carlsson | 50755b0 | 2009-09-27 20:11:34 +0000 | [diff] [blame] | 23 | #include "clang/AST/ExprCXX.h" |
Douglas Gregor | 6ec3668 | 2009-02-18 23:53:56 +0000 | [diff] [blame] | 24 | #include "clang/Basic/SourceManager.h" |
Anders Carlsson | c4355b6 | 2009-10-07 01:45:02 +0000 | [diff] [blame] | 25 | #include "llvm/ADT/StringExtras.h" |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 26 | #include "llvm/Support/raw_ostream.h" |
John McCall | efe6aee | 2009-09-05 07:56:18 +0000 | [diff] [blame] | 27 | #include "llvm/Support/ErrorHandling.h" |
Anders Carlsson | b73a5be | 2009-11-26 02:49:32 +0000 | [diff] [blame] | 28 | #include "CGVtable.h" |
Anders Carlsson | f98574b | 2010-02-05 07:31:37 +0000 | [diff] [blame] | 29 | |
| 30 | #define MANGLE_CHECKER 0 |
| 31 | |
| 32 | #if MANGLE_CHECKER |
| 33 | #include <cxxabi.h> |
| 34 | #endif |
| 35 | |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 36 | using namespace clang; |
Anders Carlsson | b73a5be | 2009-11-26 02:49:32 +0000 | [diff] [blame] | 37 | using namespace CodeGen; |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 38 | |
| 39 | namespace { |
Anders Carlsson | 7e12003 | 2009-11-24 05:36:32 +0000 | [diff] [blame] | 40 | |
| 41 | static const CXXMethodDecl *getStructor(const CXXMethodDecl *MD) { |
| 42 | assert((isa<CXXConstructorDecl>(MD) || isa<CXXDestructorDecl>(MD)) && |
| 43 | "Passed in decl is not a ctor or dtor!"); |
| 44 | |
| 45 | if (const TemplateDecl *TD = MD->getPrimaryTemplate()) { |
| 46 | MD = cast<CXXMethodDecl>(TD->getTemplatedDecl()); |
| 47 | |
| 48 | assert((isa<CXXConstructorDecl>(MD) || isa<CXXDestructorDecl>(MD)) && |
| 49 | "Templated decl is not a ctor or dtor!"); |
| 50 | } |
| 51 | |
| 52 | return MD; |
| 53 | } |
John McCall | 1dd7383 | 2010-02-04 01:42:13 +0000 | [diff] [blame] | 54 | |
| 55 | static const unsigned UnknownArity = ~0U; |
Anders Carlsson | 7e12003 | 2009-11-24 05:36:32 +0000 | [diff] [blame] | 56 | |
Daniel Dunbar | 1b07711 | 2009-11-21 09:06:10 +0000 | [diff] [blame] | 57 | /// CXXNameMangler - Manage the mangling of a single name. |
Daniel Dunbar | c074771 | 2009-11-21 09:12:13 +0000 | [diff] [blame] | 58 | class CXXNameMangler { |
Daniel Dunbar | 1b07711 | 2009-11-21 09:06:10 +0000 | [diff] [blame] | 59 | MangleContext &Context; |
Daniel Dunbar | 94fd26d | 2009-11-21 09:06:22 +0000 | [diff] [blame] | 60 | llvm::raw_svector_ostream Out; |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 61 | |
Daniel Dunbar | 1b07711 | 2009-11-21 09:06:10 +0000 | [diff] [blame] | 62 | const CXXMethodDecl *Structor; |
| 63 | unsigned StructorType; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 64 | |
Daniel Dunbar | 1b07711 | 2009-11-21 09:06:10 +0000 | [diff] [blame] | 65 | llvm::DenseMap<uintptr_t, unsigned> Substitutions; |
Daniel Dunbar | 3c9e463 | 2009-11-21 09:05:47 +0000 | [diff] [blame] | 66 | |
John McCall | 1dd7383 | 2010-02-04 01:42:13 +0000 | [diff] [blame] | 67 | ASTContext &getASTContext() const { return Context.getASTContext(); } |
| 68 | |
Daniel Dunbar | c074771 | 2009-11-21 09:12:13 +0000 | [diff] [blame] | 69 | public: |
Daniel Dunbar | 94fd26d | 2009-11-21 09:06:22 +0000 | [diff] [blame] | 70 | CXXNameMangler(MangleContext &C, llvm::SmallVectorImpl<char> &Res) |
| 71 | : Context(C), Out(Res), Structor(0), StructorType(0) { } |
Daniel Dunbar | 77939c9 | 2009-11-21 09:06:31 +0000 | [diff] [blame] | 72 | CXXNameMangler(MangleContext &C, llvm::SmallVectorImpl<char> &Res, |
| 73 | const CXXConstructorDecl *D, CXXCtorType Type) |
Anders Carlsson | 7e12003 | 2009-11-24 05:36:32 +0000 | [diff] [blame] | 74 | : Context(C), Out(Res), Structor(getStructor(D)), StructorType(Type) { } |
Daniel Dunbar | 77939c9 | 2009-11-21 09:06:31 +0000 | [diff] [blame] | 75 | CXXNameMangler(MangleContext &C, llvm::SmallVectorImpl<char> &Res, |
| 76 | const CXXDestructorDecl *D, CXXDtorType Type) |
Anders Carlsson | 7e12003 | 2009-11-24 05:36:32 +0000 | [diff] [blame] | 77 | : Context(C), Out(Res), Structor(getStructor(D)), StructorType(Type) { } |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 78 | |
Anders Carlsson | f98574b | 2010-02-05 07:31:37 +0000 | [diff] [blame] | 79 | #if MANGLE_CHECKER |
| 80 | ~CXXNameMangler() { |
| 81 | if (Out.str()[0] == '\01') |
| 82 | return; |
| 83 | |
| 84 | int status = 0; |
| 85 | char *result = abi::__cxa_demangle(Out.str().str().c_str(), 0, 0, &status); |
| 86 | assert(status == 0 && "Could not demangle mangled name!"); |
| 87 | free(result); |
| 88 | } |
| 89 | #endif |
Daniel Dunbar | c074771 | 2009-11-21 09:12:13 +0000 | [diff] [blame] | 90 | llvm::raw_svector_ostream &getStream() { return Out; } |
| 91 | |
Daniel Dunbar | 7e0c195 | 2009-11-21 09:17:15 +0000 | [diff] [blame] | 92 | void mangle(const NamedDecl *D, llvm::StringRef Prefix = "_Z"); |
Anders Carlsson | b73a5be | 2009-11-26 02:49:32 +0000 | [diff] [blame] | 93 | void mangleCallOffset(const ThunkAdjustment &Adjustment); |
Anders Carlsson | a94822e | 2009-11-26 02:32:05 +0000 | [diff] [blame] | 94 | void mangleNumber(int64_t Number); |
Daniel Dunbar | c074771 | 2009-11-21 09:12:13 +0000 | [diff] [blame] | 95 | void mangleFunctionEncoding(const FunctionDecl *FD); |
| 96 | void mangleName(const NamedDecl *ND); |
| 97 | void mangleType(QualType T); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 98 | |
Daniel Dunbar | c074771 | 2009-11-21 09:12:13 +0000 | [diff] [blame] | 99 | private: |
Daniel Dunbar | 1b07711 | 2009-11-21 09:06:10 +0000 | [diff] [blame] | 100 | bool mangleSubstitution(const NamedDecl *ND); |
| 101 | bool mangleSubstitution(QualType T); |
| 102 | bool mangleSubstitution(uintptr_t Ptr); |
Daniel Dunbar | 3c9e463 | 2009-11-21 09:05:47 +0000 | [diff] [blame] | 103 | |
Daniel Dunbar | 1b07711 | 2009-11-21 09:06:10 +0000 | [diff] [blame] | 104 | bool mangleStandardSubstitution(const NamedDecl *ND); |
Daniel Dunbar | 3c9e463 | 2009-11-21 09:05:47 +0000 | [diff] [blame] | 105 | |
Daniel Dunbar | 1b07711 | 2009-11-21 09:06:10 +0000 | [diff] [blame] | 106 | void addSubstitution(const NamedDecl *ND) { |
| 107 | ND = cast<NamedDecl>(ND->getCanonicalDecl()); |
Anders Carlsson | 433d137 | 2009-11-07 04:26:04 +0000 | [diff] [blame] | 108 | |
Daniel Dunbar | 1b07711 | 2009-11-21 09:06:10 +0000 | [diff] [blame] | 109 | addSubstitution(reinterpret_cast<uintptr_t>(ND)); |
| 110 | } |
| 111 | void addSubstitution(QualType T); |
| 112 | void addSubstitution(uintptr_t Ptr); |
Daniel Dunbar | 3c9e463 | 2009-11-21 09:05:47 +0000 | [diff] [blame] | 113 | |
John McCall | 1dd7383 | 2010-02-04 01:42:13 +0000 | [diff] [blame] | 114 | void mangleUnresolvedScope(NestedNameSpecifier *Qualifier); |
| 115 | void mangleUnresolvedName(NestedNameSpecifier *Qualifier, |
| 116 | DeclarationName Name, |
| 117 | unsigned KnownArity = UnknownArity); |
| 118 | |
Daniel Dunbar | 1b07711 | 2009-11-21 09:06:10 +0000 | [diff] [blame] | 119 | void mangleName(const TemplateDecl *TD, |
| 120 | const TemplateArgument *TemplateArgs, |
| 121 | unsigned NumTemplateArgs); |
John McCall | 1dd7383 | 2010-02-04 01:42:13 +0000 | [diff] [blame] | 122 | void mangleUnqualifiedName(const NamedDecl *ND) { |
| 123 | mangleUnqualifiedName(ND, ND->getDeclName(), UnknownArity); |
| 124 | } |
| 125 | void mangleUnqualifiedName(const NamedDecl *ND, DeclarationName Name, |
| 126 | unsigned KnownArity); |
Daniel Dunbar | 1b07711 | 2009-11-21 09:06:10 +0000 | [diff] [blame] | 127 | void mangleUnscopedName(const NamedDecl *ND); |
| 128 | void mangleUnscopedTemplateName(const TemplateDecl *ND); |
| 129 | void mangleSourceName(const IdentifierInfo *II); |
| 130 | void mangleLocalName(const NamedDecl *ND); |
Eli Friedman | 7facf84 | 2009-12-02 20:32:49 +0000 | [diff] [blame] | 131 | void mangleNestedName(const NamedDecl *ND, const DeclContext *DC); |
Daniel Dunbar | 1b07711 | 2009-11-21 09:06:10 +0000 | [diff] [blame] | 132 | void mangleNestedName(const TemplateDecl *TD, |
| 133 | const TemplateArgument *TemplateArgs, |
| 134 | unsigned NumTemplateArgs); |
| 135 | void manglePrefix(const DeclContext *DC); |
| 136 | void mangleTemplatePrefix(const TemplateDecl *ND); |
| 137 | void mangleOperatorName(OverloadedOperatorKind OO, unsigned Arity); |
| 138 | void mangleQualifiers(Qualifiers Quals); |
John McCall | efe6aee | 2009-09-05 07:56:18 +0000 | [diff] [blame] | 139 | |
Anders Carlsson | 7b06f6c | 2009-12-10 03:14:39 +0000 | [diff] [blame] | 140 | void mangleObjCMethodName(const ObjCMethodDecl *MD); |
| 141 | |
Daniel Dunbar | 1b07711 | 2009-11-21 09:06:10 +0000 | [diff] [blame] | 142 | // Declare manglers for every type class. |
John McCall | efe6aee | 2009-09-05 07:56:18 +0000 | [diff] [blame] | 143 | #define ABSTRACT_TYPE(CLASS, PARENT) |
| 144 | #define NON_CANONICAL_TYPE(CLASS, PARENT) |
| 145 | #define TYPE(CLASS, PARENT) void mangleType(const CLASS##Type *T); |
| 146 | #include "clang/AST/TypeNodes.def" |
| 147 | |
Daniel Dunbar | 1b07711 | 2009-11-21 09:06:10 +0000 | [diff] [blame] | 148 | void mangleType(const TagType*); |
| 149 | void mangleBareFunctionType(const FunctionType *T, |
| 150 | bool MangleReturnType); |
Anders Carlsson | e170ba7 | 2009-12-14 01:45:37 +0000 | [diff] [blame] | 151 | |
| 152 | void mangleIntegerLiteral(QualType T, const llvm::APSInt &Value); |
John McCall | 2f27bf8 | 2010-02-04 02:56:29 +0000 | [diff] [blame] | 153 | void mangleMemberExpr(const Expr *Base, bool IsArrow, |
| 154 | NestedNameSpecifier *Qualifier, |
| 155 | DeclarationName Name, |
| 156 | unsigned KnownArity); |
John McCall | 1dd7383 | 2010-02-04 01:42:13 +0000 | [diff] [blame] | 157 | void mangleCalledExpression(const Expr *E, unsigned KnownArity); |
Daniel Dunbar | 1b07711 | 2009-11-21 09:06:10 +0000 | [diff] [blame] | 158 | void mangleExpression(const Expr *E); |
| 159 | void mangleCXXCtorType(CXXCtorType T); |
| 160 | void mangleCXXDtorType(CXXDtorType T); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 161 | |
Daniel Dunbar | 1b07711 | 2009-11-21 09:06:10 +0000 | [diff] [blame] | 162 | void mangleTemplateArgs(const TemplateArgument *TemplateArgs, |
| 163 | unsigned NumTemplateArgs); |
Anders Carlsson | 9e85c74 | 2009-12-23 19:30:55 +0000 | [diff] [blame] | 164 | void mangleTemplateArgs(const TemplateArgumentList &L); |
| 165 | void mangleTemplateArg(const TemplateArgument &A); |
Daniel Dunbar | 3c9e463 | 2009-11-21 09:05:47 +0000 | [diff] [blame] | 166 | |
Daniel Dunbar | 1b07711 | 2009-11-21 09:06:10 +0000 | [diff] [blame] | 167 | void mangleTemplateParameter(unsigned Index); |
| 168 | }; |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 169 | } |
| 170 | |
Anders Carlsson | 43f1740 | 2009-04-02 15:51:53 +0000 | [diff] [blame] | 171 | static bool isInCLinkageSpecification(const Decl *D) { |
Douglas Gregor | 457e281 | 2009-10-28 16:31:34 +0000 | [diff] [blame] | 172 | D = D->getCanonicalDecl(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 173 | for (const DeclContext *DC = D->getDeclContext(); |
Anders Carlsson | 43f1740 | 2009-04-02 15:51:53 +0000 | [diff] [blame] | 174 | !DC->isTranslationUnit(); DC = DC->getParent()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 175 | if (const LinkageSpecDecl *Linkage = dyn_cast<LinkageSpecDecl>(DC)) |
Anders Carlsson | 43f1740 | 2009-04-02 15:51:53 +0000 | [diff] [blame] | 176 | return Linkage->getLanguage() == LinkageSpecDecl::lang_c; |
| 177 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 178 | |
Anders Carlsson | 43f1740 | 2009-04-02 15:51:53 +0000 | [diff] [blame] | 179 | return false; |
| 180 | } |
| 181 | |
Daniel Dunbar | f981bf8 | 2009-11-21 09:14:52 +0000 | [diff] [blame] | 182 | bool MangleContext::shouldMangleDeclName(const NamedDecl *D) { |
| 183 | // In C, functions with no attributes never need to be mangled. Fastpath them. |
| 184 | if (!getASTContext().getLangOptions().CPlusPlus && !D->hasAttrs()) |
| 185 | return false; |
| 186 | |
| 187 | // Any decl can be declared with __asm("foo") on it, and this takes precedence |
| 188 | // over all other naming in the .o file. |
| 189 | if (D->hasAttr<AsmLabelAttr>()) |
| 190 | return true; |
| 191 | |
Mike Stump | 141c5af | 2009-09-02 00:25:38 +0000 | [diff] [blame] | 192 | // Clang's "overloadable" attribute extension to C/C++ implies name mangling |
Anders Carlsson | a1e1622 | 2009-11-07 07:15:03 +0000 | [diff] [blame] | 193 | // (always) as does passing a C++ member function and a function |
| 194 | // whose name is not a simple identifier. |
Daniel Dunbar | f981bf8 | 2009-11-21 09:14:52 +0000 | [diff] [blame] | 195 | const FunctionDecl *FD = dyn_cast<FunctionDecl>(D); |
| 196 | if (FD && (FD->hasAttr<OverloadableAttr>() || isa<CXXMethodDecl>(FD) || |
| 197 | !FD->getDeclName().isIdentifier())) |
| 198 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 199 | |
Daniel Dunbar | f981bf8 | 2009-11-21 09:14:52 +0000 | [diff] [blame] | 200 | // Otherwise, no mangling is done outside C++ mode. |
| 201 | if (!getASTContext().getLangOptions().CPlusPlus) |
| 202 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 203 | |
Daniel Dunbar | f981bf8 | 2009-11-21 09:14:52 +0000 | [diff] [blame] | 204 | // No mangling in an "implicit extern C" header. |
| 205 | if (D->getLocation().isValid() && |
| 206 | getASTContext().getSourceManager(). |
| 207 | isInExternCSystemHeader(D->getLocation())) |
| 208 | return false; |
Anders Carlsson | 43f1740 | 2009-04-02 15:51:53 +0000 | [diff] [blame] | 209 | |
Sean Hunt | 3145525 | 2010-01-24 03:04:27 +0000 | [diff] [blame] | 210 | // Variables at global scope with non-internal linkage are not mangled |
Eli Friedman | 7facf84 | 2009-12-02 20:32:49 +0000 | [diff] [blame] | 211 | if (!FD) { |
| 212 | const DeclContext *DC = D->getDeclContext(); |
| 213 | // Check for extern variable declared locally. |
| 214 | if (isa<FunctionDecl>(DC) && D->hasLinkage()) |
| 215 | while (!DC->isNamespace() && !DC->isTranslationUnit()) |
| 216 | DC = DC->getParent(); |
Douglas Gregor | 0b6bc8b | 2010-02-03 09:33:45 +0000 | [diff] [blame] | 217 | if (DC->isTranslationUnit() && D->getLinkage() != InternalLinkage) |
Eli Friedman | 7facf84 | 2009-12-02 20:32:49 +0000 | [diff] [blame] | 218 | return false; |
| 219 | } |
| 220 | |
| 221 | // C functions and "main" are not mangled. |
| 222 | if ((FD && FD->isMain()) || isInCLinkageSpecification(D)) |
Daniel Dunbar | f981bf8 | 2009-11-21 09:14:52 +0000 | [diff] [blame] | 223 | return false; |
| 224 | |
Anders Carlsson | 43f1740 | 2009-04-02 15:51:53 +0000 | [diff] [blame] | 225 | return true; |
| 226 | } |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 227 | |
Daniel Dunbar | 7e0c195 | 2009-11-21 09:17:15 +0000 | [diff] [blame] | 228 | void CXXNameMangler::mangle(const NamedDecl *D, llvm::StringRef Prefix) { |
Mike Stump | 141c5af | 2009-09-02 00:25:38 +0000 | [diff] [blame] | 229 | // Any decl can be declared with __asm("foo") on it, and this takes precedence |
| 230 | // over all other naming in the .o file. |
Argyrios Kyrtzidis | 40b598e | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 231 | if (const AsmLabelAttr *ALA = D->getAttr<AsmLabelAttr>()) { |
Chris Lattner | ca3f25c | 2009-03-21 08:24:40 +0000 | [diff] [blame] | 232 | // If we have an asm name, then we use it as the mangling. |
| 233 | Out << '\01'; // LLVM IR Marker for __asm("foo") |
| 234 | Out << ALA->getLabel(); |
Daniel Dunbar | f981bf8 | 2009-11-21 09:14:52 +0000 | [diff] [blame] | 235 | return; |
Chris Lattner | ca3f25c | 2009-03-21 08:24:40 +0000 | [diff] [blame] | 236 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 237 | |
Sean Hunt | 3145525 | 2010-01-24 03:04:27 +0000 | [diff] [blame] | 238 | // <mangled-name> ::= _Z <encoding> |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 239 | // ::= <data name> |
| 240 | // ::= <special-name> |
Daniel Dunbar | 7e0c195 | 2009-11-21 09:17:15 +0000 | [diff] [blame] | 241 | Out << Prefix; |
| 242 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) |
Daniel Dunbar | f981bf8 | 2009-11-21 09:14:52 +0000 | [diff] [blame] | 243 | mangleFunctionEncoding(FD); |
Daniel Dunbar | 7e0c195 | 2009-11-21 09:17:15 +0000 | [diff] [blame] | 244 | else |
| 245 | mangleName(cast<VarDecl>(D)); |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 246 | } |
| 247 | |
| 248 | void CXXNameMangler::mangleFunctionEncoding(const FunctionDecl *FD) { |
| 249 | // <encoding> ::= <function name> <bare-function-type> |
| 250 | mangleName(FD); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 251 | |
Daniel Dunbar | 7e0c195 | 2009-11-21 09:17:15 +0000 | [diff] [blame] | 252 | // Don't mangle in the type if this isn't a decl we should typically mangle. |
| 253 | if (!Context.shouldMangleDeclName(FD)) |
| 254 | return; |
| 255 | |
Mike Stump | 141c5af | 2009-09-02 00:25:38 +0000 | [diff] [blame] | 256 | // Whether the mangling of a function type includes the return type depends on |
| 257 | // the context and the nature of the function. The rules for deciding whether |
| 258 | // the return type is included are: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 259 | // |
Douglas Gregor | 1fd2dd1 | 2009-06-29 22:39:32 +0000 | [diff] [blame] | 260 | // 1. Template functions (names or types) have return types encoded, with |
| 261 | // the exceptions listed below. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 262 | // 2. Function types not appearing as part of a function name mangling, |
Douglas Gregor | 1fd2dd1 | 2009-06-29 22:39:32 +0000 | [diff] [blame] | 263 | // e.g. parameters, pointer types, etc., have return type encoded, with the |
| 264 | // exceptions listed below. |
| 265 | // 3. Non-template function names do not have return types encoded. |
| 266 | // |
Mike Stump | 141c5af | 2009-09-02 00:25:38 +0000 | [diff] [blame] | 267 | // The exceptions mentioned in (1) and (2) above, for which the return type is |
| 268 | // never included, are |
Douglas Gregor | 1fd2dd1 | 2009-06-29 22:39:32 +0000 | [diff] [blame] | 269 | // 1. Constructors. |
| 270 | // 2. Destructors. |
| 271 | // 3. Conversion operator functions, e.g. operator int. |
| 272 | bool MangleReturnType = false; |
Anders Carlsson | 9234b7f | 2009-09-17 03:46:43 +0000 | [diff] [blame] | 273 | if (FunctionTemplateDecl *PrimaryTemplate = FD->getPrimaryTemplate()) { |
| 274 | if (!(isa<CXXConstructorDecl>(FD) || isa<CXXDestructorDecl>(FD) || |
| 275 | isa<CXXConversionDecl>(FD))) |
| 276 | MangleReturnType = true; |
Daniel Dunbar | 3c9e463 | 2009-11-21 09:05:47 +0000 | [diff] [blame] | 277 | |
Anders Carlsson | 9234b7f | 2009-09-17 03:46:43 +0000 | [diff] [blame] | 278 | // Mangle the type of the primary template. |
| 279 | FD = PrimaryTemplate->getTemplatedDecl(); |
| 280 | } |
| 281 | |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 282 | // Do the canonicalization out here because parameter types can |
| 283 | // undergo additional canonicalization (e.g. array decay). |
| 284 | FunctionType *FT = cast<FunctionType>(Context.getASTContext() |
| 285 | .getCanonicalType(FD->getType())); |
| 286 | |
| 287 | mangleBareFunctionType(FT, MangleReturnType); |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 288 | } |
| 289 | |
Anders Carlsson | 47846d2 | 2009-12-04 06:23:23 +0000 | [diff] [blame] | 290 | /// isStd - Return whether a given namespace is the 'std' namespace. |
| 291 | static bool isStd(const NamespaceDecl *NS) { |
John McCall | 9aeed32 | 2009-10-01 00:25:31 +0000 | [diff] [blame] | 292 | const IdentifierInfo *II = NS->getOriginalNamespace()->getIdentifier(); |
| 293 | return II && II->isStr("std"); |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 294 | } |
| 295 | |
Anders Carlsson | 47846d2 | 2009-12-04 06:23:23 +0000 | [diff] [blame] | 296 | static const DeclContext *IgnoreLinkageSpecDecls(const DeclContext *DC) { |
| 297 | while (isa<LinkageSpecDecl>(DC)) { |
| 298 | assert(cast<LinkageSpecDecl>(DC)->getLanguage() == |
| 299 | LinkageSpecDecl::lang_cxx && "Unexpected linkage decl!"); |
| 300 | DC = DC->getParent(); |
| 301 | } |
| 302 | |
| 303 | return DC; |
| 304 | } |
| 305 | |
| 306 | // isStdNamespace - Return whether a given decl context is a toplevel 'std' |
| 307 | // namespace. |
Daniel Dunbar | 1308af9 | 2009-11-21 09:11:45 +0000 | [diff] [blame] | 308 | static bool isStdNamespace(const DeclContext *DC) { |
Anders Carlsson | 47846d2 | 2009-12-04 06:23:23 +0000 | [diff] [blame] | 309 | if (!DC->isNamespace()) |
| 310 | return false; |
| 311 | |
| 312 | if (!IgnoreLinkageSpecDecls(DC->getParent())->isTranslationUnit()) |
| 313 | return false; |
| 314 | |
| 315 | return isStd(cast<NamespaceDecl>(DC)); |
Daniel Dunbar | 1308af9 | 2009-11-21 09:11:45 +0000 | [diff] [blame] | 316 | } |
| 317 | |
Anders Carlsson | bb36ba4 | 2009-09-26 03:24:57 +0000 | [diff] [blame] | 318 | static const TemplateDecl * |
| 319 | isTemplate(const NamedDecl *ND, const TemplateArgumentList *&TemplateArgs) { |
Anders Carlsson | 2744a06 | 2009-09-18 19:00:18 +0000 | [diff] [blame] | 320 | // Check if we have a function template. |
| 321 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)){ |
Anders Carlsson | bb36ba4 | 2009-09-26 03:24:57 +0000 | [diff] [blame] | 322 | if (const TemplateDecl *TD = FD->getPrimaryTemplate()) { |
Anders Carlsson | 2744a06 | 2009-09-18 19:00:18 +0000 | [diff] [blame] | 323 | TemplateArgs = FD->getTemplateSpecializationArgs(); |
Anders Carlsson | bb36ba4 | 2009-09-26 03:24:57 +0000 | [diff] [blame] | 324 | return TD; |
Anders Carlsson | 2744a06 | 2009-09-18 19:00:18 +0000 | [diff] [blame] | 325 | } |
| 326 | } |
| 327 | |
Anders Carlsson | eafc6dc | 2009-09-18 19:44:50 +0000 | [diff] [blame] | 328 | // Check if we have a class template. |
| 329 | if (const ClassTemplateSpecializationDecl *Spec = |
| 330 | dyn_cast<ClassTemplateSpecializationDecl>(ND)) { |
| 331 | TemplateArgs = &Spec->getTemplateArgs(); |
Anders Carlsson | bb36ba4 | 2009-09-26 03:24:57 +0000 | [diff] [blame] | 332 | return Spec->getSpecializedTemplate(); |
Anders Carlsson | eafc6dc | 2009-09-18 19:44:50 +0000 | [diff] [blame] | 333 | } |
Daniel Dunbar | 3c9e463 | 2009-11-21 09:05:47 +0000 | [diff] [blame] | 334 | |
Anders Carlsson | 2744a06 | 2009-09-18 19:00:18 +0000 | [diff] [blame] | 335 | return 0; |
| 336 | } |
| 337 | |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 338 | void CXXNameMangler::mangleName(const NamedDecl *ND) { |
| 339 | // <name> ::= <nested-name> |
| 340 | // ::= <unscoped-name> |
| 341 | // ::= <unscoped-template-name> <template-args> |
Anders Carlsson | 201ce74 | 2009-09-17 03:17:01 +0000 | [diff] [blame] | 342 | // ::= <local-name> |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 343 | // |
Anders Carlsson | d58d6f7 | 2009-09-17 16:12:20 +0000 | [diff] [blame] | 344 | const DeclContext *DC = ND->getDeclContext(); |
Eli Friedman | 7facf84 | 2009-12-02 20:32:49 +0000 | [diff] [blame] | 345 | |
| 346 | // If this is an extern variable declared locally, the relevant DeclContext |
| 347 | // is that of the containing namespace, or the translation unit. |
| 348 | if (isa<FunctionDecl>(DC) && ND->hasLinkage()) |
| 349 | while (!DC->isNamespace() && !DC->isTranslationUnit()) |
| 350 | DC = DC->getParent(); |
| 351 | |
Anders Carlsson | 5cc58c6 | 2009-09-22 17:23:30 +0000 | [diff] [blame] | 352 | while (isa<LinkageSpecDecl>(DC)) |
Anders Carlsson | d58d6f7 | 2009-09-17 16:12:20 +0000 | [diff] [blame] | 353 | DC = DC->getParent(); |
Daniel Dunbar | 3c9e463 | 2009-11-21 09:05:47 +0000 | [diff] [blame] | 354 | |
Anders Carlsson | d58d6f7 | 2009-09-17 16:12:20 +0000 | [diff] [blame] | 355 | if (DC->isTranslationUnit() || isStdNamespace(DC)) { |
Anders Carlsson | 2744a06 | 2009-09-18 19:00:18 +0000 | [diff] [blame] | 356 | // Check if we have a template. |
| 357 | const TemplateArgumentList *TemplateArgs = 0; |
Anders Carlsson | 0fa6df4 | 2009-09-26 19:45:45 +0000 | [diff] [blame] | 358 | if (const TemplateDecl *TD = isTemplate(ND, TemplateArgs)) { |
Anders Carlsson | 2744a06 | 2009-09-18 19:00:18 +0000 | [diff] [blame] | 359 | mangleUnscopedTemplateName(TD); |
Anders Carlsson | 9e85c74 | 2009-12-23 19:30:55 +0000 | [diff] [blame] | 360 | mangleTemplateArgs(*TemplateArgs); |
Anders Carlsson | 2744a06 | 2009-09-18 19:00:18 +0000 | [diff] [blame] | 361 | return; |
Anders Carlsson | 7482e24 | 2009-09-18 04:29:09 +0000 | [diff] [blame] | 362 | } |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 363 | |
Anders Carlsson | 7482e24 | 2009-09-18 04:29:09 +0000 | [diff] [blame] | 364 | mangleUnscopedName(ND); |
| 365 | return; |
| 366 | } |
Daniel Dunbar | 3c9e463 | 2009-11-21 09:05:47 +0000 | [diff] [blame] | 367 | |
Anders Carlsson | 7b06f6c | 2009-12-10 03:14:39 +0000 | [diff] [blame] | 368 | if (isa<FunctionDecl>(DC) || isa<ObjCMethodDecl>(DC)) { |
Anders Carlsson | 7482e24 | 2009-09-18 04:29:09 +0000 | [diff] [blame] | 369 | mangleLocalName(ND); |
| 370 | return; |
| 371 | } |
Daniel Dunbar | 3c9e463 | 2009-11-21 09:05:47 +0000 | [diff] [blame] | 372 | |
Eli Friedman | 7facf84 | 2009-12-02 20:32:49 +0000 | [diff] [blame] | 373 | mangleNestedName(ND, DC); |
Anders Carlsson | 7482e24 | 2009-09-18 04:29:09 +0000 | [diff] [blame] | 374 | } |
Daniel Dunbar | 3c9e463 | 2009-11-21 09:05:47 +0000 | [diff] [blame] | 375 | void CXXNameMangler::mangleName(const TemplateDecl *TD, |
Anders Carlsson | 7624f21 | 2009-09-18 02:42:01 +0000 | [diff] [blame] | 376 | const TemplateArgument *TemplateArgs, |
| 377 | unsigned NumTemplateArgs) { |
Anders Carlsson | 47846d2 | 2009-12-04 06:23:23 +0000 | [diff] [blame] | 378 | const DeclContext *DC = IgnoreLinkageSpecDecls(TD->getDeclContext()); |
Daniel Dunbar | 3c9e463 | 2009-11-21 09:05:47 +0000 | [diff] [blame] | 379 | |
Anders Carlsson | 7624f21 | 2009-09-18 02:42:01 +0000 | [diff] [blame] | 380 | if (DC->isTranslationUnit() || isStdNamespace(DC)) { |
Anders Carlsson | 0fa6df4 | 2009-09-26 19:45:45 +0000 | [diff] [blame] | 381 | mangleUnscopedTemplateName(TD); |
Anders Carlsson | 7624f21 | 2009-09-18 02:42:01 +0000 | [diff] [blame] | 382 | mangleTemplateArgs(TemplateArgs, NumTemplateArgs); |
| 383 | } else { |
| 384 | mangleNestedName(TD, TemplateArgs, NumTemplateArgs); |
| 385 | } |
| 386 | } |
| 387 | |
Anders Carlsson | 201ce74 | 2009-09-17 03:17:01 +0000 | [diff] [blame] | 388 | void CXXNameMangler::mangleUnscopedName(const NamedDecl *ND) { |
| 389 | // <unscoped-name> ::= <unqualified-name> |
| 390 | // ::= St <unqualified-name> # ::std:: |
| 391 | if (isStdNamespace(ND->getDeclContext())) |
| 392 | Out << "St"; |
Daniel Dunbar | 3c9e463 | 2009-11-21 09:05:47 +0000 | [diff] [blame] | 393 | |
Anders Carlsson | 201ce74 | 2009-09-17 03:17:01 +0000 | [diff] [blame] | 394 | mangleUnqualifiedName(ND); |
| 395 | } |
| 396 | |
Anders Carlsson | 0fa6df4 | 2009-09-26 19:45:45 +0000 | [diff] [blame] | 397 | void CXXNameMangler::mangleUnscopedTemplateName(const TemplateDecl *ND) { |
Anders Carlsson | 201ce74 | 2009-09-17 03:17:01 +0000 | [diff] [blame] | 398 | // <unscoped-template-name> ::= <unscoped-name> |
| 399 | // ::= <substitution> |
Anders Carlsson | 7624f21 | 2009-09-18 02:42:01 +0000 | [diff] [blame] | 400 | if (mangleSubstitution(ND)) |
Anders Carlsson | 03c9d53 | 2009-09-17 04:02:31 +0000 | [diff] [blame] | 401 | return; |
Daniel Dunbar | 3c9e463 | 2009-11-21 09:05:47 +0000 | [diff] [blame] | 402 | |
Douglas Gregor | 32fb4e1 | 2010-02-05 20:45:00 +0000 | [diff] [blame] | 403 | // <template-template-param> ::= <template-param> |
| 404 | if (const TemplateTemplateParmDecl *TTP |
| 405 | = dyn_cast<TemplateTemplateParmDecl>(ND)) { |
| 406 | mangleTemplateParameter(TTP->getIndex()); |
| 407 | return; |
| 408 | } |
| 409 | |
Anders Carlsson | 1668f20 | 2009-09-26 20:13:56 +0000 | [diff] [blame] | 410 | mangleUnscopedName(ND->getTemplatedDecl()); |
Anders Carlsson | 7624f21 | 2009-09-18 02:42:01 +0000 | [diff] [blame] | 411 | addSubstitution(ND); |
Anders Carlsson | 201ce74 | 2009-09-17 03:17:01 +0000 | [diff] [blame] | 412 | } |
| 413 | |
Anders Carlsson | a94822e | 2009-11-26 02:32:05 +0000 | [diff] [blame] | 414 | void CXXNameMangler::mangleNumber(int64_t Number) { |
| 415 | // <number> ::= [n] <non-negative decimal integer> |
| 416 | if (Number < 0) { |
| 417 | Out << 'n'; |
| 418 | Number = -Number; |
| 419 | } |
| 420 | |
| 421 | Out << Number; |
| 422 | } |
| 423 | |
Anders Carlsson | b73a5be | 2009-11-26 02:49:32 +0000 | [diff] [blame] | 424 | void CXXNameMangler::mangleCallOffset(const ThunkAdjustment &Adjustment) { |
Mike Stump | 141c5af | 2009-09-02 00:25:38 +0000 | [diff] [blame] | 425 | // <call-offset> ::= h <nv-offset> _ |
| 426 | // ::= v <v-offset> _ |
| 427 | // <nv-offset> ::= <offset number> # non-virtual base override |
Anders Carlsson | a94822e | 2009-11-26 02:32:05 +0000 | [diff] [blame] | 428 | // <v-offset> ::= <offset number> _ <virtual offset number> |
Mike Stump | 141c5af | 2009-09-02 00:25:38 +0000 | [diff] [blame] | 429 | // # virtual base override, with vcall offset |
Anders Carlsson | b73a5be | 2009-11-26 02:49:32 +0000 | [diff] [blame] | 430 | if (!Adjustment.Virtual) { |
Anders Carlsson | a94822e | 2009-11-26 02:32:05 +0000 | [diff] [blame] | 431 | Out << 'h'; |
Anders Carlsson | b73a5be | 2009-11-26 02:49:32 +0000 | [diff] [blame] | 432 | mangleNumber(Adjustment.NonVirtual); |
Anders Carlsson | a94822e | 2009-11-26 02:32:05 +0000 | [diff] [blame] | 433 | Out << '_'; |
| 434 | return; |
Mike Stump | 141c5af | 2009-09-02 00:25:38 +0000 | [diff] [blame] | 435 | } |
Anders Carlsson | a94822e | 2009-11-26 02:32:05 +0000 | [diff] [blame] | 436 | |
| 437 | Out << 'v'; |
Anders Carlsson | b73a5be | 2009-11-26 02:49:32 +0000 | [diff] [blame] | 438 | mangleNumber(Adjustment.NonVirtual); |
Anders Carlsson | a94822e | 2009-11-26 02:32:05 +0000 | [diff] [blame] | 439 | Out << '_'; |
Anders Carlsson | b73a5be | 2009-11-26 02:49:32 +0000 | [diff] [blame] | 440 | mangleNumber(Adjustment.Virtual); |
Anders Carlsson | a94822e | 2009-11-26 02:32:05 +0000 | [diff] [blame] | 441 | Out << '_'; |
Mike Stump | 9124bcc | 2009-09-02 00:56:18 +0000 | [diff] [blame] | 442 | } |
| 443 | |
John McCall | 1dd7383 | 2010-02-04 01:42:13 +0000 | [diff] [blame] | 444 | void CXXNameMangler::mangleUnresolvedScope(NestedNameSpecifier *Qualifier) { |
| 445 | Qualifier = getASTContext().getCanonicalNestedNameSpecifier(Qualifier); |
| 446 | switch (Qualifier->getKind()) { |
| 447 | case NestedNameSpecifier::Global: |
| 448 | // nothing |
| 449 | break; |
| 450 | case NestedNameSpecifier::Namespace: |
| 451 | mangleName(Qualifier->getAsNamespace()); |
| 452 | break; |
| 453 | case NestedNameSpecifier::TypeSpec: |
| 454 | case NestedNameSpecifier::TypeSpecWithTemplate: |
| 455 | mangleType(QualType(Qualifier->getAsType(), 0)); |
| 456 | break; |
| 457 | case NestedNameSpecifier::Identifier: |
| 458 | mangleUnresolvedScope(Qualifier->getPrefix()); |
| 459 | mangleSourceName(Qualifier->getAsIdentifier()); |
| 460 | break; |
| 461 | } |
| 462 | } |
| 463 | |
| 464 | /// Mangles a name which was not resolved to a specific entity. |
| 465 | void CXXNameMangler::mangleUnresolvedName(NestedNameSpecifier *Qualifier, |
| 466 | DeclarationName Name, |
| 467 | unsigned KnownArity) { |
| 468 | if (Qualifier) |
| 469 | mangleUnresolvedScope(Qualifier); |
| 470 | // FIXME: ambiguity of unqualified lookup with :: |
| 471 | |
| 472 | mangleUnqualifiedName(0, Name, KnownArity); |
| 473 | } |
| 474 | |
| 475 | void CXXNameMangler::mangleUnqualifiedName(const NamedDecl *ND, |
| 476 | DeclarationName Name, |
| 477 | unsigned KnownArity) { |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 478 | // <unqualified-name> ::= <operator-name> |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 479 | // ::= <ctor-dtor-name> |
| 480 | // ::= <source-name> |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 481 | switch (Name.getNameKind()) { |
Anders Carlsson | c4355b6 | 2009-10-07 01:45:02 +0000 | [diff] [blame] | 482 | case DeclarationName::Identifier: { |
Anders Carlsson | c4355b6 | 2009-10-07 01:45:02 +0000 | [diff] [blame] | 483 | if (const IdentifierInfo *II = Name.getAsIdentifierInfo()) { |
Sean Hunt | 3145525 | 2010-01-24 03:04:27 +0000 | [diff] [blame] | 484 | // We must avoid conflicts between internally- and externally- |
Anders Carlsson | aec2523 | 2010-02-06 04:52:27 +0000 | [diff] [blame] | 485 | // linked variable declaration names in the same TU. |
| 486 | // This naming convention is the same as that followed by GCC, though it |
| 487 | // shouldn't actually matter. |
| 488 | if (ND && isa<VarDecl>(ND) && ND->getLinkage() == InternalLinkage && |
Sean Hunt | 3145525 | 2010-01-24 03:04:27 +0000 | [diff] [blame] | 489 | ND->getDeclContext()->isFileContext()) |
| 490 | Out << 'L'; |
| 491 | |
Anders Carlsson | c4355b6 | 2009-10-07 01:45:02 +0000 | [diff] [blame] | 492 | mangleSourceName(II); |
| 493 | break; |
| 494 | } |
Daniel Dunbar | 3c9e463 | 2009-11-21 09:05:47 +0000 | [diff] [blame] | 495 | |
John McCall | 1dd7383 | 2010-02-04 01:42:13 +0000 | [diff] [blame] | 496 | // Otherwise, an anonymous entity. We must have a declaration. |
| 497 | assert(ND && "mangling empty name without declaration"); |
| 498 | |
| 499 | if (const NamespaceDecl *NS = dyn_cast<NamespaceDecl>(ND)) { |
| 500 | if (NS->isAnonymousNamespace()) { |
| 501 | // This is how gcc mangles these names. |
| 502 | Out << "12_GLOBAL__N_1"; |
| 503 | break; |
| 504 | } |
| 505 | } |
| 506 | |
Anders Carlsson | c4355b6 | 2009-10-07 01:45:02 +0000 | [diff] [blame] | 507 | // We must have an anonymous struct. |
| 508 | const TagDecl *TD = cast<TagDecl>(ND); |
| 509 | if (const TypedefDecl *D = TD->getTypedefForAnonDecl()) { |
| 510 | assert(TD->getDeclContext() == D->getDeclContext() && |
| 511 | "Typedef should not be in another decl context!"); |
| 512 | assert(D->getDeclName().getAsIdentifierInfo() && |
| 513 | "Typedef was not named!"); |
| 514 | mangleSourceName(D->getDeclName().getAsIdentifierInfo()); |
| 515 | break; |
| 516 | } |
Daniel Dunbar | 3c9e463 | 2009-11-21 09:05:47 +0000 | [diff] [blame] | 517 | |
Anders Carlsson | c4355b6 | 2009-10-07 01:45:02 +0000 | [diff] [blame] | 518 | // Get a unique id for the anonymous struct. |
| 519 | uint64_t AnonStructId = Context.getAnonymousStructId(TD); |
| 520 | |
| 521 | // Mangle it as a source name in the form |
Daniel Dunbar | 3c9e463 | 2009-11-21 09:05:47 +0000 | [diff] [blame] | 522 | // [n] $_<id> |
Anders Carlsson | c4355b6 | 2009-10-07 01:45:02 +0000 | [diff] [blame] | 523 | // where n is the length of the string. |
| 524 | llvm::SmallString<8> Str; |
| 525 | Str += "$_"; |
| 526 | Str += llvm::utostr(AnonStructId); |
| 527 | |
| 528 | Out << Str.size(); |
| 529 | Out << Str.str(); |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 530 | break; |
Anders Carlsson | c4355b6 | 2009-10-07 01:45:02 +0000 | [diff] [blame] | 531 | } |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 532 | |
| 533 | case DeclarationName::ObjCZeroArgSelector: |
| 534 | case DeclarationName::ObjCOneArgSelector: |
| 535 | case DeclarationName::ObjCMultiArgSelector: |
| 536 | assert(false && "Can't mangle Objective-C selector names here!"); |
| 537 | break; |
| 538 | |
| 539 | case DeclarationName::CXXConstructorName: |
Anders Carlsson | 27ae536 | 2009-04-17 01:58:57 +0000 | [diff] [blame] | 540 | if (ND == Structor) |
Mike Stump | 141c5af | 2009-09-02 00:25:38 +0000 | [diff] [blame] | 541 | // If the named decl is the C++ constructor we're mangling, use the type |
| 542 | // we were given. |
Anders Carlsson | 27ae536 | 2009-04-17 01:58:57 +0000 | [diff] [blame] | 543 | mangleCXXCtorType(static_cast<CXXCtorType>(StructorType)); |
Anders Carlsson | 3ac86b5 | 2009-04-15 05:36:58 +0000 | [diff] [blame] | 544 | else |
| 545 | // Otherwise, use the complete constructor name. This is relevant if a |
| 546 | // class with a constructor is declared within a constructor. |
| 547 | mangleCXXCtorType(Ctor_Complete); |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 548 | break; |
| 549 | |
| 550 | case DeclarationName::CXXDestructorName: |
Anders Carlsson | 27ae536 | 2009-04-17 01:58:57 +0000 | [diff] [blame] | 551 | if (ND == Structor) |
Mike Stump | 141c5af | 2009-09-02 00:25:38 +0000 | [diff] [blame] | 552 | // If the named decl is the C++ destructor we're mangling, use the type we |
| 553 | // were given. |
Anders Carlsson | 27ae536 | 2009-04-17 01:58:57 +0000 | [diff] [blame] | 554 | mangleCXXDtorType(static_cast<CXXDtorType>(StructorType)); |
| 555 | else |
| 556 | // Otherwise, use the complete destructor name. This is relevant if a |
| 557 | // class with a destructor is declared within a destructor. |
| 558 | mangleCXXDtorType(Dtor_Complete); |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 559 | break; |
| 560 | |
| 561 | case DeclarationName::CXXConversionFunctionName: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 562 | // <operator-name> ::= cv <type> # (cast) |
Douglas Gregor | 219cc61 | 2009-02-13 01:28:03 +0000 | [diff] [blame] | 563 | Out << "cv"; |
Anders Carlsson | b540491 | 2009-10-07 01:06:45 +0000 | [diff] [blame] | 564 | mangleType(Context.getASTContext().getCanonicalType(Name.getCXXNameType())); |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 565 | break; |
| 566 | |
Anders Carlsson | 8257d41 | 2009-12-22 06:36:32 +0000 | [diff] [blame] | 567 | case DeclarationName::CXXOperatorName: { |
John McCall | 1dd7383 | 2010-02-04 01:42:13 +0000 | [diff] [blame] | 568 | unsigned Arity; |
| 569 | if (ND) { |
| 570 | Arity = cast<FunctionDecl>(ND)->getNumParams(); |
Anders Carlsson | 8257d41 | 2009-12-22 06:36:32 +0000 | [diff] [blame] | 571 | |
John McCall | 1dd7383 | 2010-02-04 01:42:13 +0000 | [diff] [blame] | 572 | // If we have a C++ member function, we need to include the 'this' pointer. |
| 573 | // FIXME: This does not make sense for operators that are static, but their |
| 574 | // names stay the same regardless of the arity (operator new for instance). |
| 575 | if (isa<CXXMethodDecl>(ND)) |
| 576 | Arity++; |
| 577 | } else |
| 578 | Arity = KnownArity; |
| 579 | |
Anders Carlsson | 8257d41 | 2009-12-22 06:36:32 +0000 | [diff] [blame] | 580 | mangleOperatorName(Name.getCXXOverloadedOperator(), Arity); |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 581 | break; |
Anders Carlsson | 8257d41 | 2009-12-22 06:36:32 +0000 | [diff] [blame] | 582 | } |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 583 | |
Sean Hunt | 3e518bd | 2009-11-29 07:34:05 +0000 | [diff] [blame] | 584 | case DeclarationName::CXXLiteralOperatorName: |
Sean Hunt | 5dd6b39 | 2009-12-04 21:11:13 +0000 | [diff] [blame] | 585 | // FIXME: This mangling is not yet official. |
Sean Hunt | 2421f66 | 2009-12-04 21:01:37 +0000 | [diff] [blame] | 586 | Out << "li"; |
Sean Hunt | 3e518bd | 2009-11-29 07:34:05 +0000 | [diff] [blame] | 587 | mangleSourceName(Name.getCXXLiteralIdentifier()); |
| 588 | break; |
| 589 | |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 590 | case DeclarationName::CXXUsingDirective: |
| 591 | assert(false && "Can't mangle a using directive name!"); |
Douglas Gregor | 219cc61 | 2009-02-13 01:28:03 +0000 | [diff] [blame] | 592 | break; |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 593 | } |
| 594 | } |
| 595 | |
| 596 | void CXXNameMangler::mangleSourceName(const IdentifierInfo *II) { |
| 597 | // <source-name> ::= <positive length number> <identifier> |
| 598 | // <number> ::= [n] <non-negative decimal integer> |
| 599 | // <identifier> ::= <unqualified source code identifier> |
| 600 | Out << II->getLength() << II->getName(); |
| 601 | } |
| 602 | |
Eli Friedman | 7facf84 | 2009-12-02 20:32:49 +0000 | [diff] [blame] | 603 | void CXXNameMangler::mangleNestedName(const NamedDecl *ND, |
| 604 | const DeclContext *DC) { |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 605 | // <nested-name> ::= N [<CV-qualifiers>] <prefix> <unqualified-name> E |
| 606 | // ::= N [<CV-qualifiers>] <template-prefix> <template-args> E |
Anders Carlsson | d99edc4 | 2009-09-26 03:55:37 +0000 | [diff] [blame] | 607 | |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 608 | Out << 'N'; |
| 609 | if (const CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(ND)) |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 610 | mangleQualifiers(Qualifiers::fromCVRMask(Method->getTypeQualifiers())); |
Daniel Dunbar | 3c9e463 | 2009-11-21 09:05:47 +0000 | [diff] [blame] | 611 | |
Anders Carlsson | 2744a06 | 2009-09-18 19:00:18 +0000 | [diff] [blame] | 612 | // Check if we have a template. |
| 613 | const TemplateArgumentList *TemplateArgs = 0; |
Daniel Dunbar | 3c9e463 | 2009-11-21 09:05:47 +0000 | [diff] [blame] | 614 | if (const TemplateDecl *TD = isTemplate(ND, TemplateArgs)) { |
Anders Carlsson | 2744a06 | 2009-09-18 19:00:18 +0000 | [diff] [blame] | 615 | mangleTemplatePrefix(TD); |
Anders Carlsson | 9e85c74 | 2009-12-23 19:30:55 +0000 | [diff] [blame] | 616 | mangleTemplateArgs(*TemplateArgs); |
Anders Carlsson | 7482e24 | 2009-09-18 04:29:09 +0000 | [diff] [blame] | 617 | } else { |
Eli Friedman | 7facf84 | 2009-12-02 20:32:49 +0000 | [diff] [blame] | 618 | manglePrefix(DC); |
Anders Carlsson | 7482e24 | 2009-09-18 04:29:09 +0000 | [diff] [blame] | 619 | mangleUnqualifiedName(ND); |
| 620 | } |
Daniel Dunbar | 3c9e463 | 2009-11-21 09:05:47 +0000 | [diff] [blame] | 621 | |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 622 | Out << 'E'; |
| 623 | } |
Daniel Dunbar | 3c9e463 | 2009-11-21 09:05:47 +0000 | [diff] [blame] | 624 | void CXXNameMangler::mangleNestedName(const TemplateDecl *TD, |
Anders Carlsson | 7624f21 | 2009-09-18 02:42:01 +0000 | [diff] [blame] | 625 | const TemplateArgument *TemplateArgs, |
| 626 | unsigned NumTemplateArgs) { |
Anders Carlsson | e45117b | 2009-09-27 19:53:49 +0000 | [diff] [blame] | 627 | // <nested-name> ::= N [<CV-qualifiers>] <template-prefix> <template-args> E |
| 628 | |
Anders Carlsson | 7624f21 | 2009-09-18 02:42:01 +0000 | [diff] [blame] | 629 | Out << 'N'; |
Daniel Dunbar | 3c9e463 | 2009-11-21 09:05:47 +0000 | [diff] [blame] | 630 | |
Anders Carlsson | e45117b | 2009-09-27 19:53:49 +0000 | [diff] [blame] | 631 | mangleTemplatePrefix(TD); |
Anders Carlsson | 7624f21 | 2009-09-18 02:42:01 +0000 | [diff] [blame] | 632 | mangleTemplateArgs(TemplateArgs, NumTemplateArgs); |
Daniel Dunbar | 3c9e463 | 2009-11-21 09:05:47 +0000 | [diff] [blame] | 633 | |
Anders Carlsson | 7624f21 | 2009-09-18 02:42:01 +0000 | [diff] [blame] | 634 | Out << 'E'; |
| 635 | } |
| 636 | |
Anders Carlsson | 1b42c79 | 2009-04-02 16:24:45 +0000 | [diff] [blame] | 637 | void CXXNameMangler::mangleLocalName(const NamedDecl *ND) { |
| 638 | // <local-name> := Z <function encoding> E <entity name> [<discriminator>] |
| 639 | // := Z <function encoding> E s [<discriminator>] |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 640 | // <discriminator> := _ <non-negative number> |
Anders Carlsson | 1b42c79 | 2009-04-02 16:24:45 +0000 | [diff] [blame] | 641 | Out << 'Z'; |
Anders Carlsson | 7b06f6c | 2009-12-10 03:14:39 +0000 | [diff] [blame] | 642 | |
| 643 | if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(ND->getDeclContext())) |
| 644 | mangleObjCMethodName(MD); |
| 645 | else |
| 646 | mangleFunctionEncoding(cast<FunctionDecl>(ND->getDeclContext())); |
| 647 | |
Anders Carlsson | 1b42c79 | 2009-04-02 16:24:45 +0000 | [diff] [blame] | 648 | Out << 'E'; |
Eli Friedman | 6f9f25d | 2009-12-11 20:21:38 +0000 | [diff] [blame] | 649 | mangleUnqualifiedName(ND); |
Anders Carlsson | 1b42c79 | 2009-04-02 16:24:45 +0000 | [diff] [blame] | 650 | } |
| 651 | |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 652 | void CXXNameMangler::manglePrefix(const DeclContext *DC) { |
| 653 | // <prefix> ::= <prefix> <unqualified-name> |
| 654 | // ::= <template-prefix> <template-args> |
| 655 | // ::= <template-param> |
| 656 | // ::= # empty |
| 657 | // ::= <substitution> |
Anders Carlsson | 6862fc7 | 2009-09-17 04:16:28 +0000 | [diff] [blame] | 658 | |
Anders Carlsson | add2882 | 2009-09-22 20:33:31 +0000 | [diff] [blame] | 659 | while (isa<LinkageSpecDecl>(DC)) |
| 660 | DC = DC->getParent(); |
Daniel Dunbar | 3c9e463 | 2009-11-21 09:05:47 +0000 | [diff] [blame] | 661 | |
Anders Carlsson | 9263e91 | 2009-09-18 18:39:58 +0000 | [diff] [blame] | 662 | if (DC->isTranslationUnit()) |
| 663 | return; |
Daniel Dunbar | 3c9e463 | 2009-11-21 09:05:47 +0000 | [diff] [blame] | 664 | |
Anders Carlsson | 6862fc7 | 2009-09-17 04:16:28 +0000 | [diff] [blame] | 665 | if (mangleSubstitution(cast<NamedDecl>(DC))) |
| 666 | return; |
Anders Carlsson | 7482e24 | 2009-09-18 04:29:09 +0000 | [diff] [blame] | 667 | |
Anders Carlsson | 2ee3fca | 2009-09-18 20:11:09 +0000 | [diff] [blame] | 668 | // Check if we have a template. |
| 669 | const TemplateArgumentList *TemplateArgs = 0; |
Daniel Dunbar | 3c9e463 | 2009-11-21 09:05:47 +0000 | [diff] [blame] | 670 | if (const TemplateDecl *TD = isTemplate(cast<NamedDecl>(DC), TemplateArgs)) { |
Anders Carlsson | 2ee3fca | 2009-09-18 20:11:09 +0000 | [diff] [blame] | 671 | mangleTemplatePrefix(TD); |
Anders Carlsson | 9e85c74 | 2009-12-23 19:30:55 +0000 | [diff] [blame] | 672 | mangleTemplateArgs(*TemplateArgs); |
Anders Carlsson | 2ee3fca | 2009-09-18 20:11:09 +0000 | [diff] [blame] | 673 | } else { |
| 674 | manglePrefix(DC->getParent()); |
| 675 | mangleUnqualifiedName(cast<NamedDecl>(DC)); |
| 676 | } |
Daniel Dunbar | 3c9e463 | 2009-11-21 09:05:47 +0000 | [diff] [blame] | 677 | |
Anders Carlsson | 6862fc7 | 2009-09-17 04:16:28 +0000 | [diff] [blame] | 678 | addSubstitution(cast<NamedDecl>(DC)); |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 679 | } |
| 680 | |
Anders Carlsson | 0fa6df4 | 2009-09-26 19:45:45 +0000 | [diff] [blame] | 681 | void CXXNameMangler::mangleTemplatePrefix(const TemplateDecl *ND) { |
Anders Carlsson | 7482e24 | 2009-09-18 04:29:09 +0000 | [diff] [blame] | 682 | // <template-prefix> ::= <prefix> <template unqualified-name> |
| 683 | // ::= <template-param> |
| 684 | // ::= <substitution> |
Douglas Gregor | 32fb4e1 | 2010-02-05 20:45:00 +0000 | [diff] [blame] | 685 | // <template-template-param> ::= <template-param> |
| 686 | // <substitution> |
Anders Carlsson | 7482e24 | 2009-09-18 04:29:09 +0000 | [diff] [blame] | 687 | |
Anders Carlsson | aeb8537 | 2009-09-26 22:18:22 +0000 | [diff] [blame] | 688 | if (mangleSubstitution(ND)) |
| 689 | return; |
Daniel Dunbar | 3c9e463 | 2009-11-21 09:05:47 +0000 | [diff] [blame] | 690 | |
Douglas Gregor | 32fb4e1 | 2010-02-05 20:45:00 +0000 | [diff] [blame] | 691 | // <template-template-param> ::= <template-param> |
| 692 | if (const TemplateTemplateParmDecl *TTP |
| 693 | = dyn_cast<TemplateTemplateParmDecl>(ND)) { |
| 694 | mangleTemplateParameter(TTP->getIndex()); |
| 695 | return; |
| 696 | } |
Daniel Dunbar | 3c9e463 | 2009-11-21 09:05:47 +0000 | [diff] [blame] | 697 | |
Anders Carlsson | aa73ab1 | 2009-09-18 18:47:07 +0000 | [diff] [blame] | 698 | manglePrefix(ND->getDeclContext()); |
Anders Carlsson | 1668f20 | 2009-09-26 20:13:56 +0000 | [diff] [blame] | 699 | mangleUnqualifiedName(ND->getTemplatedDecl()); |
Anders Carlsson | aeb8537 | 2009-09-26 22:18:22 +0000 | [diff] [blame] | 700 | addSubstitution(ND); |
Anders Carlsson | 7482e24 | 2009-09-18 04:29:09 +0000 | [diff] [blame] | 701 | } |
| 702 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 703 | void |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 704 | CXXNameMangler::mangleOperatorName(OverloadedOperatorKind OO, unsigned Arity) { |
| 705 | switch (OO) { |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 706 | // <operator-name> ::= nw # new |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 707 | case OO_New: Out << "nw"; break; |
| 708 | // ::= na # new[] |
| 709 | case OO_Array_New: Out << "na"; break; |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 710 | // ::= dl # delete |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 711 | case OO_Delete: Out << "dl"; break; |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 712 | // ::= da # delete[] |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 713 | case OO_Array_Delete: Out << "da"; break; |
| 714 | // ::= ps # + (unary) |
| 715 | // ::= pl # + |
Anders Carlsson | 8257d41 | 2009-12-22 06:36:32 +0000 | [diff] [blame] | 716 | case OO_Plus: |
| 717 | assert((Arity == 1 || Arity == 2) && "Invalid arity!"); |
| 718 | Out << (Arity == 1? "ps" : "pl"); break; |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 719 | // ::= ng # - (unary) |
| 720 | // ::= mi # - |
Anders Carlsson | 8257d41 | 2009-12-22 06:36:32 +0000 | [diff] [blame] | 721 | case OO_Minus: |
| 722 | assert((Arity == 1 || Arity == 2) && "Invalid arity!"); |
| 723 | Out << (Arity == 1? "ng" : "mi"); break; |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 724 | // ::= ad # & (unary) |
| 725 | // ::= an # & |
Anders Carlsson | 8257d41 | 2009-12-22 06:36:32 +0000 | [diff] [blame] | 726 | case OO_Amp: |
| 727 | assert((Arity == 1 || Arity == 2) && "Invalid arity!"); |
| 728 | Out << (Arity == 1? "ad" : "an"); break; |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 729 | // ::= de # * (unary) |
| 730 | // ::= ml # * |
Anders Carlsson | 8257d41 | 2009-12-22 06:36:32 +0000 | [diff] [blame] | 731 | case OO_Star: |
| 732 | assert((Arity == 1 || Arity == 2) && "Invalid arity!"); |
| 733 | Out << (Arity == 1? "de" : "ml"); break; |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 734 | // ::= co # ~ |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 735 | case OO_Tilde: Out << "co"; break; |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 736 | // ::= dv # / |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 737 | case OO_Slash: Out << "dv"; break; |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 738 | // ::= rm # % |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 739 | case OO_Percent: Out << "rm"; break; |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 740 | // ::= or # | |
| 741 | case OO_Pipe: Out << "or"; break; |
| 742 | // ::= eo # ^ |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 743 | case OO_Caret: Out << "eo"; break; |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 744 | // ::= aS # = |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 745 | case OO_Equal: Out << "aS"; break; |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 746 | // ::= pL # += |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 747 | case OO_PlusEqual: Out << "pL"; break; |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 748 | // ::= mI # -= |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 749 | case OO_MinusEqual: Out << "mI"; break; |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 750 | // ::= mL # *= |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 751 | case OO_StarEqual: Out << "mL"; break; |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 752 | // ::= dV # /= |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 753 | case OO_SlashEqual: Out << "dV"; break; |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 754 | // ::= rM # %= |
| 755 | case OO_PercentEqual: Out << "rM"; break; |
| 756 | // ::= aN # &= |
| 757 | case OO_AmpEqual: Out << "aN"; break; |
| 758 | // ::= oR # |= |
| 759 | case OO_PipeEqual: Out << "oR"; break; |
| 760 | // ::= eO # ^= |
| 761 | case OO_CaretEqual: Out << "eO"; break; |
| 762 | // ::= ls # << |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 763 | case OO_LessLess: Out << "ls"; break; |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 764 | // ::= rs # >> |
| 765 | case OO_GreaterGreater: Out << "rs"; break; |
| 766 | // ::= lS # <<= |
| 767 | case OO_LessLessEqual: Out << "lS"; break; |
| 768 | // ::= rS # >>= |
| 769 | case OO_GreaterGreaterEqual: Out << "rS"; break; |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 770 | // ::= eq # == |
| 771 | case OO_EqualEqual: Out << "eq"; break; |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 772 | // ::= ne # != |
| 773 | case OO_ExclaimEqual: Out << "ne"; break; |
| 774 | // ::= lt # < |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 775 | case OO_Less: Out << "lt"; break; |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 776 | // ::= gt # > |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 777 | case OO_Greater: Out << "gt"; break; |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 778 | // ::= le # <= |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 779 | case OO_LessEqual: Out << "le"; break; |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 780 | // ::= ge # >= |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 781 | case OO_GreaterEqual: Out << "ge"; break; |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 782 | // ::= nt # ! |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 783 | case OO_Exclaim: Out << "nt"; break; |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 784 | // ::= aa # && |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 785 | case OO_AmpAmp: Out << "aa"; break; |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 786 | // ::= oo # || |
| 787 | case OO_PipePipe: Out << "oo"; break; |
| 788 | // ::= pp # ++ |
| 789 | case OO_PlusPlus: Out << "pp"; break; |
| 790 | // ::= mm # -- |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 791 | case OO_MinusMinus: Out << "mm"; break; |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 792 | // ::= cm # , |
| 793 | case OO_Comma: Out << "cm"; break; |
| 794 | // ::= pm # ->* |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 795 | case OO_ArrowStar: Out << "pm"; break; |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 796 | // ::= pt # -> |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 797 | case OO_Arrow: Out << "pt"; break; |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 798 | // ::= cl # () |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 799 | case OO_Call: Out << "cl"; break; |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 800 | // ::= ix # [] |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 801 | case OO_Subscript: Out << "ix"; break; |
Anders Carlsson | e170ba7 | 2009-12-14 01:45:37 +0000 | [diff] [blame] | 802 | |
| 803 | // ::= qu # ? |
| 804 | // The conditional operator can't be overloaded, but we still handle it when |
| 805 | // mangling expressions. |
| 806 | case OO_Conditional: Out << "qu"; break; |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 807 | |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 808 | case OO_None: |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 809 | case NUM_OVERLOADED_OPERATORS: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 810 | assert(false && "Not an overloaded operator"); |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 811 | break; |
| 812 | } |
| 813 | } |
| 814 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 815 | void CXXNameMangler::mangleQualifiers(Qualifiers Quals) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 816 | // <CV-qualifiers> ::= [r] [V] [K] # restrict (C99), volatile, const |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 817 | if (Quals.hasRestrict()) |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 818 | Out << 'r'; |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 819 | if (Quals.hasVolatile()) |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 820 | Out << 'V'; |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 821 | if (Quals.hasConst()) |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 822 | Out << 'K'; |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 823 | |
| 824 | // FIXME: For now, just drop all extension qualifiers on the floor. |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 825 | } |
| 826 | |
Anders Carlsson | 7b06f6c | 2009-12-10 03:14:39 +0000 | [diff] [blame] | 827 | void CXXNameMangler::mangleObjCMethodName(const ObjCMethodDecl *MD) { |
| 828 | llvm::SmallString<64> Name; |
| 829 | llvm::raw_svector_ostream OS(Name); |
| 830 | |
| 831 | const ObjCContainerDecl *CD = |
| 832 | dyn_cast<ObjCContainerDecl>(MD->getDeclContext()); |
| 833 | assert (CD && "Missing container decl in GetNameForMethod"); |
| 834 | OS << (MD->isInstanceMethod() ? '-' : '+') << '[' << CD->getName(); |
| 835 | if (const ObjCCategoryImplDecl *CID = dyn_cast<ObjCCategoryImplDecl>(CD)) |
| 836 | OS << '(' << CID->getNameAsString() << ')'; |
| 837 | OS << ' ' << MD->getSelector().getAsString() << ']'; |
| 838 | |
| 839 | Out << OS.str().size() << OS.str(); |
| 840 | } |
| 841 | |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 842 | void CXXNameMangler::mangleType(QualType T) { |
Anders Carlsson | 4843e58 | 2009-03-10 17:07:44 +0000 | [diff] [blame] | 843 | // Only operate on the canonical type! |
Anders Carlsson | b540491 | 2009-10-07 01:06:45 +0000 | [diff] [blame] | 844 | T = Context.getASTContext().getCanonicalType(T); |
Anders Carlsson | 4843e58 | 2009-03-10 17:07:44 +0000 | [diff] [blame] | 845 | |
Douglas Gregor | a4923eb | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 846 | bool IsSubstitutable = T.hasLocalQualifiers() || !isa<BuiltinType>(T); |
Anders Carlsson | 7696737 | 2009-09-17 00:43:46 +0000 | [diff] [blame] | 847 | if (IsSubstitutable && mangleSubstitution(T)) |
| 848 | return; |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 849 | |
Douglas Gregor | a4923eb | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 850 | if (Qualifiers Quals = T.getLocalQualifiers()) { |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 851 | mangleQualifiers(Quals); |
| 852 | // Recurse: even if the qualified type isn't yet substitutable, |
| 853 | // the unqualified type might be. |
Douglas Gregor | a4923eb | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 854 | mangleType(T.getLocalUnqualifiedType()); |
Anders Carlsson | 7696737 | 2009-09-17 00:43:46 +0000 | [diff] [blame] | 855 | } else { |
| 856 | switch (T->getTypeClass()) { |
John McCall | efe6aee | 2009-09-05 07:56:18 +0000 | [diff] [blame] | 857 | #define ABSTRACT_TYPE(CLASS, PARENT) |
| 858 | #define NON_CANONICAL_TYPE(CLASS, PARENT) \ |
Anders Carlsson | 7696737 | 2009-09-17 00:43:46 +0000 | [diff] [blame] | 859 | case Type::CLASS: \ |
Jeffrey Yasskin | 9f61aa9 | 2009-12-12 05:05:38 +0000 | [diff] [blame] | 860 | llvm_unreachable("can't mangle non-canonical type " #CLASS "Type"); \ |
Anders Carlsson | 7696737 | 2009-09-17 00:43:46 +0000 | [diff] [blame] | 861 | return; |
John McCall | efe6aee | 2009-09-05 07:56:18 +0000 | [diff] [blame] | 862 | #define TYPE(CLASS, PARENT) \ |
Anders Carlsson | 7696737 | 2009-09-17 00:43:46 +0000 | [diff] [blame] | 863 | case Type::CLASS: \ |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 864 | mangleType(static_cast<const CLASS##Type*>(T.getTypePtr())); \ |
Anders Carlsson | 7696737 | 2009-09-17 00:43:46 +0000 | [diff] [blame] | 865 | break; |
John McCall | efe6aee | 2009-09-05 07:56:18 +0000 | [diff] [blame] | 866 | #include "clang/AST/TypeNodes.def" |
Anders Carlsson | 7696737 | 2009-09-17 00:43:46 +0000 | [diff] [blame] | 867 | } |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 868 | } |
Anders Carlsson | 7696737 | 2009-09-17 00:43:46 +0000 | [diff] [blame] | 869 | |
| 870 | // Add the substitution. |
| 871 | if (IsSubstitutable) |
| 872 | addSubstitution(T); |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 873 | } |
| 874 | |
| 875 | void CXXNameMangler::mangleType(const BuiltinType *T) { |
John McCall | efe6aee | 2009-09-05 07:56:18 +0000 | [diff] [blame] | 876 | // <type> ::= <builtin-type> |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 877 | // <builtin-type> ::= v # void |
| 878 | // ::= w # wchar_t |
| 879 | // ::= b # bool |
| 880 | // ::= c # char |
| 881 | // ::= a # signed char |
| 882 | // ::= h # unsigned char |
| 883 | // ::= s # short |
| 884 | // ::= t # unsigned short |
| 885 | // ::= i # int |
| 886 | // ::= j # unsigned int |
| 887 | // ::= l # long |
| 888 | // ::= m # unsigned long |
| 889 | // ::= x # long long, __int64 |
| 890 | // ::= y # unsigned long long, __int64 |
| 891 | // ::= n # __int128 |
| 892 | // UNSUPPORTED: ::= o # unsigned __int128 |
| 893 | // ::= f # float |
| 894 | // ::= d # double |
| 895 | // ::= e # long double, __float80 |
| 896 | // UNSUPPORTED: ::= g # __float128 |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 897 | // UNSUPPORTED: ::= Dd # IEEE 754r decimal floating point (64 bits) |
| 898 | // UNSUPPORTED: ::= De # IEEE 754r decimal floating point (128 bits) |
| 899 | // UNSUPPORTED: ::= Df # IEEE 754r decimal floating point (32 bits) |
| 900 | // UNSUPPORTED: ::= Dh # IEEE 754r half-precision floating point (16 bits) |
Alisdair Meredith | f5c209d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 901 | // ::= Di # char32_t |
| 902 | // ::= Ds # char16_t |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 903 | // ::= u <source-name> # vendor extended type |
Sebastian Redl | 6e8ed16 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 904 | // From our point of view, std::nullptr_t is a builtin, but as far as mangling |
| 905 | // is concerned, it's a type called std::nullptr_t. |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 906 | switch (T->getKind()) { |
| 907 | case BuiltinType::Void: Out << 'v'; break; |
| 908 | case BuiltinType::Bool: Out << 'b'; break; |
| 909 | case BuiltinType::Char_U: case BuiltinType::Char_S: Out << 'c'; break; |
| 910 | case BuiltinType::UChar: Out << 'h'; break; |
| 911 | case BuiltinType::UShort: Out << 't'; break; |
| 912 | case BuiltinType::UInt: Out << 'j'; break; |
| 913 | case BuiltinType::ULong: Out << 'm'; break; |
| 914 | case BuiltinType::ULongLong: Out << 'y'; break; |
Chris Lattner | 2df9ced | 2009-04-30 02:43:43 +0000 | [diff] [blame] | 915 | case BuiltinType::UInt128: Out << 'o'; break; |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 916 | case BuiltinType::SChar: Out << 'a'; break; |
| 917 | case BuiltinType::WChar: Out << 'w'; break; |
Alisdair Meredith | f5c209d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 918 | case BuiltinType::Char16: Out << "Ds"; break; |
| 919 | case BuiltinType::Char32: Out << "Di"; break; |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 920 | case BuiltinType::Short: Out << 's'; break; |
| 921 | case BuiltinType::Int: Out << 'i'; break; |
| 922 | case BuiltinType::Long: Out << 'l'; break; |
| 923 | case BuiltinType::LongLong: Out << 'x'; break; |
Chris Lattner | 2df9ced | 2009-04-30 02:43:43 +0000 | [diff] [blame] | 924 | case BuiltinType::Int128: Out << 'n'; break; |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 925 | case BuiltinType::Float: Out << 'f'; break; |
| 926 | case BuiltinType::Double: Out << 'd'; break; |
| 927 | case BuiltinType::LongDouble: Out << 'e'; break; |
Sebastian Redl | 6e8ed16 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 928 | case BuiltinType::NullPtr: Out << "St9nullptr_t"; break; |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 929 | |
| 930 | case BuiltinType::Overload: |
| 931 | case BuiltinType::Dependent: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 932 | assert(false && |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 933 | "Overloaded and dependent types shouldn't get to name mangling"); |
| 934 | break; |
Anders Carlsson | e89d159 | 2009-06-26 18:41:36 +0000 | [diff] [blame] | 935 | case BuiltinType::UndeducedAuto: |
| 936 | assert(0 && "Should not see undeduced auto here"); |
| 937 | break; |
Steve Naroff | 9533a7f | 2009-07-22 17:14:51 +0000 | [diff] [blame] | 938 | case BuiltinType::ObjCId: Out << "11objc_object"; break; |
| 939 | case BuiltinType::ObjCClass: Out << "10objc_class"; break; |
Fariborz Jahanian | 13dcd00 | 2009-11-21 19:53:08 +0000 | [diff] [blame] | 940 | case BuiltinType::ObjCSel: Out << "13objc_selector"; break; |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 941 | } |
| 942 | } |
| 943 | |
John McCall | efe6aee | 2009-09-05 07:56:18 +0000 | [diff] [blame] | 944 | // <type> ::= <function-type> |
| 945 | // <function-type> ::= F [Y] <bare-function-type> E |
| 946 | void CXXNameMangler::mangleType(const FunctionProtoType *T) { |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 947 | Out << 'F'; |
Mike Stump | f5408fe | 2009-05-16 07:57:57 +0000 | [diff] [blame] | 948 | // FIXME: We don't have enough information in the AST to produce the 'Y' |
| 949 | // encoding for extern "C" function types. |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 950 | mangleBareFunctionType(T, /*MangleReturnType=*/true); |
| 951 | Out << 'E'; |
| 952 | } |
John McCall | efe6aee | 2009-09-05 07:56:18 +0000 | [diff] [blame] | 953 | void CXXNameMangler::mangleType(const FunctionNoProtoType *T) { |
Jeffrey Yasskin | 9f61aa9 | 2009-12-12 05:05:38 +0000 | [diff] [blame] | 954 | llvm_unreachable("Can't mangle K&R function prototypes"); |
John McCall | efe6aee | 2009-09-05 07:56:18 +0000 | [diff] [blame] | 955 | } |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 956 | void CXXNameMangler::mangleBareFunctionType(const FunctionType *T, |
| 957 | bool MangleReturnType) { |
John McCall | efe6aee | 2009-09-05 07:56:18 +0000 | [diff] [blame] | 958 | // We should never be mangling something without a prototype. |
| 959 | const FunctionProtoType *Proto = cast<FunctionProtoType>(T); |
| 960 | |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 961 | // <bare-function-type> ::= <signature type>+ |
| 962 | if (MangleReturnType) |
John McCall | efe6aee | 2009-09-05 07:56:18 +0000 | [diff] [blame] | 963 | mangleType(Proto->getResultType()); |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 964 | |
Anders Carlsson | c6c91bc | 2009-04-01 00:15:23 +0000 | [diff] [blame] | 965 | if (Proto->getNumArgs() == 0) { |
| 966 | Out << 'v'; |
| 967 | return; |
| 968 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 969 | |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 970 | for (FunctionProtoType::arg_type_iterator Arg = Proto->arg_type_begin(), |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 971 | ArgEnd = Proto->arg_type_end(); |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 972 | Arg != ArgEnd; ++Arg) |
| 973 | mangleType(*Arg); |
Douglas Gregor | 219cc61 | 2009-02-13 01:28:03 +0000 | [diff] [blame] | 974 | |
| 975 | // <builtin-type> ::= z # ellipsis |
| 976 | if (Proto->isVariadic()) |
| 977 | Out << 'z'; |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 978 | } |
| 979 | |
John McCall | efe6aee | 2009-09-05 07:56:18 +0000 | [diff] [blame] | 980 | // <type> ::= <class-enum-type> |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 981 | // <class-enum-type> ::= <name> |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 982 | void CXXNameMangler::mangleType(const UnresolvedUsingType *T) { |
| 983 | mangleName(T->getDecl()); |
| 984 | } |
| 985 | |
| 986 | // <type> ::= <class-enum-type> |
| 987 | // <class-enum-type> ::= <name> |
John McCall | efe6aee | 2009-09-05 07:56:18 +0000 | [diff] [blame] | 988 | void CXXNameMangler::mangleType(const EnumType *T) { |
| 989 | mangleType(static_cast<const TagType*>(T)); |
| 990 | } |
| 991 | void CXXNameMangler::mangleType(const RecordType *T) { |
| 992 | mangleType(static_cast<const TagType*>(T)); |
| 993 | } |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 994 | void CXXNameMangler::mangleType(const TagType *T) { |
Eli Friedman | ecb7e93 | 2009-12-11 18:00:57 +0000 | [diff] [blame] | 995 | mangleName(T->getDecl()); |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 996 | } |
| 997 | |
John McCall | efe6aee | 2009-09-05 07:56:18 +0000 | [diff] [blame] | 998 | // <type> ::= <array-type> |
| 999 | // <array-type> ::= A <positive dimension number> _ <element type> |
| 1000 | // ::= A [<dimension expression>] _ <element type> |
| 1001 | void CXXNameMangler::mangleType(const ConstantArrayType *T) { |
| 1002 | Out << 'A' << T->getSize() << '_'; |
| 1003 | mangleType(T->getElementType()); |
| 1004 | } |
| 1005 | void CXXNameMangler::mangleType(const VariableArrayType *T) { |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 1006 | Out << 'A'; |
John McCall | efe6aee | 2009-09-05 07:56:18 +0000 | [diff] [blame] | 1007 | mangleExpression(T->getSizeExpr()); |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 1008 | Out << '_'; |
| 1009 | mangleType(T->getElementType()); |
| 1010 | } |
John McCall | efe6aee | 2009-09-05 07:56:18 +0000 | [diff] [blame] | 1011 | void CXXNameMangler::mangleType(const DependentSizedArrayType *T) { |
| 1012 | Out << 'A'; |
| 1013 | mangleExpression(T->getSizeExpr()); |
| 1014 | Out << '_'; |
| 1015 | mangleType(T->getElementType()); |
| 1016 | } |
| 1017 | void CXXNameMangler::mangleType(const IncompleteArrayType *T) { |
| 1018 | Out << 'A' << '_'; |
| 1019 | mangleType(T->getElementType()); |
| 1020 | } |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 1021 | |
John McCall | efe6aee | 2009-09-05 07:56:18 +0000 | [diff] [blame] | 1022 | // <type> ::= <pointer-to-member-type> |
| 1023 | // <pointer-to-member-type> ::= M <class type> <member type> |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 1024 | void CXXNameMangler::mangleType(const MemberPointerType *T) { |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 1025 | Out << 'M'; |
| 1026 | mangleType(QualType(T->getClass(), 0)); |
Anders Carlsson | 0e65001 | 2009-05-17 17:41:20 +0000 | [diff] [blame] | 1027 | QualType PointeeType = T->getPointeeType(); |
| 1028 | if (const FunctionProtoType *FPT = dyn_cast<FunctionProtoType>(PointeeType)) { |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1029 | mangleQualifiers(Qualifiers::fromCVRMask(FPT->getTypeQuals())); |
Anders Carlsson | 0e65001 | 2009-05-17 17:41:20 +0000 | [diff] [blame] | 1030 | mangleType(FPT); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1031 | } else |
Anders Carlsson | 0e65001 | 2009-05-17 17:41:20 +0000 | [diff] [blame] | 1032 | mangleType(PointeeType); |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 1033 | } |
| 1034 | |
John McCall | efe6aee | 2009-09-05 07:56:18 +0000 | [diff] [blame] | 1035 | // <type> ::= <template-param> |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 1036 | void CXXNameMangler::mangleType(const TemplateTypeParmType *T) { |
Anders Carlsson | 0ccdf8d | 2009-09-27 00:38:53 +0000 | [diff] [blame] | 1037 | mangleTemplateParameter(T->getIndex()); |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 1038 | } |
| 1039 | |
John McCall | efe6aee | 2009-09-05 07:56:18 +0000 | [diff] [blame] | 1040 | // FIXME: <type> ::= <template-template-param> <template-args> |
John McCall | efe6aee | 2009-09-05 07:56:18 +0000 | [diff] [blame] | 1041 | |
| 1042 | // <type> ::= P <type> # pointer-to |
| 1043 | void CXXNameMangler::mangleType(const PointerType *T) { |
| 1044 | Out << 'P'; |
| 1045 | mangleType(T->getPointeeType()); |
| 1046 | } |
| 1047 | void CXXNameMangler::mangleType(const ObjCObjectPointerType *T) { |
| 1048 | Out << 'P'; |
| 1049 | mangleType(T->getPointeeType()); |
| 1050 | } |
| 1051 | |
| 1052 | // <type> ::= R <type> # reference-to |
| 1053 | void CXXNameMangler::mangleType(const LValueReferenceType *T) { |
| 1054 | Out << 'R'; |
| 1055 | mangleType(T->getPointeeType()); |
| 1056 | } |
| 1057 | |
| 1058 | // <type> ::= O <type> # rvalue reference-to (C++0x) |
| 1059 | void CXXNameMangler::mangleType(const RValueReferenceType *T) { |
| 1060 | Out << 'O'; |
| 1061 | mangleType(T->getPointeeType()); |
| 1062 | } |
| 1063 | |
| 1064 | // <type> ::= C <type> # complex pair (C 2000) |
| 1065 | void CXXNameMangler::mangleType(const ComplexType *T) { |
| 1066 | Out << 'C'; |
| 1067 | mangleType(T->getElementType()); |
| 1068 | } |
| 1069 | |
| 1070 | // GNU extension: vector types |
| 1071 | void CXXNameMangler::mangleType(const VectorType *T) { |
| 1072 | Out << "U8__vector"; |
| 1073 | mangleType(T->getElementType()); |
| 1074 | } |
| 1075 | void CXXNameMangler::mangleType(const ExtVectorType *T) { |
| 1076 | mangleType(static_cast<const VectorType*>(T)); |
| 1077 | } |
| 1078 | void CXXNameMangler::mangleType(const DependentSizedExtVectorType *T) { |
| 1079 | Out << "U8__vector"; |
| 1080 | mangleType(T->getElementType()); |
| 1081 | } |
| 1082 | |
Anders Carlsson | a40c5e4 | 2009-03-07 22:03:21 +0000 | [diff] [blame] | 1083 | void CXXNameMangler::mangleType(const ObjCInterfaceType *T) { |
| 1084 | mangleSourceName(T->getDecl()->getIdentifier()); |
| 1085 | } |
| 1086 | |
John McCall | efe6aee | 2009-09-05 07:56:18 +0000 | [diff] [blame] | 1087 | void CXXNameMangler::mangleType(const BlockPointerType *T) { |
Anders Carlsson | f28c687 | 2009-12-23 22:31:44 +0000 | [diff] [blame] | 1088 | Out << "U13block_pointer"; |
| 1089 | mangleType(T->getPointeeType()); |
John McCall | efe6aee | 2009-09-05 07:56:18 +0000 | [diff] [blame] | 1090 | } |
| 1091 | |
John McCall | efe6aee | 2009-09-05 07:56:18 +0000 | [diff] [blame] | 1092 | void CXXNameMangler::mangleType(const TemplateSpecializationType *T) { |
Anders Carlsson | 7624f21 | 2009-09-18 02:42:01 +0000 | [diff] [blame] | 1093 | TemplateDecl *TD = T->getTemplateName().getAsTemplateDecl(); |
| 1094 | assert(TD && "FIXME: Support dependent template names!"); |
Daniel Dunbar | 3c9e463 | 2009-11-21 09:05:47 +0000 | [diff] [blame] | 1095 | |
Anders Carlsson | 7624f21 | 2009-09-18 02:42:01 +0000 | [diff] [blame] | 1096 | mangleName(TD, T->getArgs(), T->getNumArgs()); |
John McCall | efe6aee | 2009-09-05 07:56:18 +0000 | [diff] [blame] | 1097 | } |
| 1098 | |
| 1099 | void CXXNameMangler::mangleType(const TypenameType *T) { |
Anders Carlsson | ae35248 | 2009-09-26 02:26:02 +0000 | [diff] [blame] | 1100 | // Typename types are always nested |
| 1101 | Out << 'N'; |
| 1102 | |
| 1103 | const Type *QTy = T->getQualifier()->getAsType(); |
Daniel Dunbar | 3c9e463 | 2009-11-21 09:05:47 +0000 | [diff] [blame] | 1104 | if (const TemplateSpecializationType *TST = |
Anders Carlsson | ae35248 | 2009-09-26 02:26:02 +0000 | [diff] [blame] | 1105 | dyn_cast<TemplateSpecializationType>(QTy)) { |
Anders Carlsson | 8859917 | 2009-09-27 01:06:07 +0000 | [diff] [blame] | 1106 | if (!mangleSubstitution(QualType(TST, 0))) { |
| 1107 | TemplateDecl *TD = TST->getTemplateName().getAsTemplateDecl(); |
Douglas Gregor | 2d565b3 | 2010-02-06 01:09:36 +0000 | [diff] [blame] | 1108 | assert(TD && "FIXME: Support dependent template names"); |
Anders Carlsson | 8859917 | 2009-09-27 01:06:07 +0000 | [diff] [blame] | 1109 | mangleTemplatePrefix(TD); |
| 1110 | mangleTemplateArgs(TST->getArgs(), TST->getNumArgs()); |
| 1111 | addSubstitution(QualType(TST, 0)); |
| 1112 | } |
Daniel Dunbar | 3c9e463 | 2009-11-21 09:05:47 +0000 | [diff] [blame] | 1113 | } else if (const TemplateTypeParmType *TTPT = |
Anders Carlsson | 0ccdf8d | 2009-09-27 00:38:53 +0000 | [diff] [blame] | 1114 | dyn_cast<TemplateTypeParmType>(QTy)) { |
| 1115 | // We use the QualType mangle type variant here because it handles |
| 1116 | // substitutions. |
| 1117 | mangleType(QualType(TTPT, 0)); |
Anders Carlsson | ae35248 | 2009-09-26 02:26:02 +0000 | [diff] [blame] | 1118 | } else |
| 1119 | assert(false && "Unhandled type!"); |
| 1120 | |
| 1121 | mangleSourceName(T->getIdentifier()); |
Daniel Dunbar | 3c9e463 | 2009-11-21 09:05:47 +0000 | [diff] [blame] | 1122 | |
Anders Carlsson | ae35248 | 2009-09-26 02:26:02 +0000 | [diff] [blame] | 1123 | Out << 'E'; |
John McCall | efe6aee | 2009-09-05 07:56:18 +0000 | [diff] [blame] | 1124 | } |
| 1125 | |
Anders Carlsson | e170ba7 | 2009-12-14 01:45:37 +0000 | [diff] [blame] | 1126 | void CXXNameMangler::mangleIntegerLiteral(QualType T, |
| 1127 | const llvm::APSInt &Value) { |
| 1128 | // <expr-primary> ::= L <type> <value number> E # integer literal |
| 1129 | Out << 'L'; |
| 1130 | |
| 1131 | mangleType(T); |
| 1132 | if (T->isBooleanType()) { |
| 1133 | // Boolean values are encoded as 0/1. |
| 1134 | Out << (Value.getBoolValue() ? '1' : '0'); |
| 1135 | } else { |
| 1136 | if (Value.isNegative()) |
| 1137 | Out << 'n'; |
| 1138 | Value.abs().print(Out, false); |
| 1139 | } |
| 1140 | Out << 'E'; |
| 1141 | |
| 1142 | } |
| 1143 | |
John McCall | 1dd7383 | 2010-02-04 01:42:13 +0000 | [diff] [blame] | 1144 | void CXXNameMangler::mangleCalledExpression(const Expr *E, unsigned Arity) { |
| 1145 | if (E->getType() != getASTContext().OverloadTy) |
| 1146 | mangleExpression(E); |
John McCall | 2f27bf8 | 2010-02-04 02:56:29 +0000 | [diff] [blame] | 1147 | // propagate arity to dependent overloads? |
John McCall | 1dd7383 | 2010-02-04 01:42:13 +0000 | [diff] [blame] | 1148 | |
| 1149 | llvm::PointerIntPair<OverloadExpr*,1> R |
| 1150 | = OverloadExpr::find(const_cast<Expr*>(E)); |
| 1151 | if (R.getInt()) |
| 1152 | Out << "an"; // & |
| 1153 | const OverloadExpr *Ovl = R.getPointer(); |
John McCall | 2f27bf8 | 2010-02-04 02:56:29 +0000 | [diff] [blame] | 1154 | if (const UnresolvedMemberExpr *ME = dyn_cast<UnresolvedMemberExpr>(Ovl)) { |
| 1155 | mangleMemberExpr(ME->getBase(), ME->isArrow(), ME->getQualifier(), |
| 1156 | ME->getMemberName(), Arity); |
| 1157 | return; |
| 1158 | } |
John McCall | 1dd7383 | 2010-02-04 01:42:13 +0000 | [diff] [blame] | 1159 | |
| 1160 | mangleUnresolvedName(Ovl->getQualifier(), Ovl->getName(), Arity); |
| 1161 | } |
| 1162 | |
John McCall | 2f27bf8 | 2010-02-04 02:56:29 +0000 | [diff] [blame] | 1163 | /// Mangles a member expression. Implicit accesses are not handled, |
| 1164 | /// but that should be okay, because you shouldn't be able to |
| 1165 | /// make an implicit access in a function template declaration. |
| 1166 | /// |
| 1167 | /// The standard ABI does not describe how member expressions should |
| 1168 | /// be mangled, so this is very unstandardized. We mangle as if it |
| 1169 | /// were a binary operator, except that the RHS is mangled as an |
| 1170 | /// abstract name. |
| 1171 | /// |
| 1172 | /// The standard ABI also does not assign a mangling to the dot |
| 1173 | /// operator, so we arbitrarily select 'me'. |
| 1174 | void CXXNameMangler::mangleMemberExpr(const Expr *Base, |
| 1175 | bool IsArrow, |
| 1176 | NestedNameSpecifier *Qualifier, |
| 1177 | DeclarationName Member, |
| 1178 | unsigned Arity) { |
| 1179 | Out << (IsArrow ? "pt" : "me"); |
| 1180 | mangleExpression(Base); |
| 1181 | mangleUnresolvedName(Qualifier, Member, Arity); |
| 1182 | } |
| 1183 | |
Anders Carlsson | d553f8c | 2009-09-21 01:21:10 +0000 | [diff] [blame] | 1184 | void CXXNameMangler::mangleExpression(const Expr *E) { |
| 1185 | // <expression> ::= <unary operator-name> <expression> |
John McCall | 09cc141 | 2010-02-03 00:55:45 +0000 | [diff] [blame] | 1186 | // ::= <binary operator-name> <expression> <expression> |
| 1187 | // ::= <trinary operator-name> <expression> <expression> <expression> |
| 1188 | // ::= cl <expression>* E # call |
Anders Carlsson | d553f8c | 2009-09-21 01:21:10 +0000 | [diff] [blame] | 1189 | // ::= cv <type> expression # conversion with one argument |
| 1190 | // ::= cv <type> _ <expression>* E # conversion with a different number of arguments |
John McCall | 09cc141 | 2010-02-03 00:55:45 +0000 | [diff] [blame] | 1191 | // ::= st <type> # sizeof (a type) |
Anders Carlsson | d553f8c | 2009-09-21 01:21:10 +0000 | [diff] [blame] | 1192 | // ::= at <type> # alignof (a type) |
| 1193 | // ::= <template-param> |
| 1194 | // ::= <function-param> |
| 1195 | // ::= sr <type> <unqualified-name> # dependent name |
| 1196 | // ::= sr <type> <unqualified-name> <template-args> # dependent template-id |
| 1197 | // ::= sZ <template-param> # size of a parameter pack |
John McCall | 09cc141 | 2010-02-03 00:55:45 +0000 | [diff] [blame] | 1198 | // ::= <expr-primary> |
John McCall | 1dd7383 | 2010-02-04 01:42:13 +0000 | [diff] [blame] | 1199 | // <expr-primary> ::= L <type> <value number> E # integer literal |
| 1200 | // ::= L <type <value float> E # floating literal |
| 1201 | // ::= L <mangled-name> E # external name |
Anders Carlsson | d553f8c | 2009-09-21 01:21:10 +0000 | [diff] [blame] | 1202 | switch (E->getStmtClass()) { |
John McCall | 09cc141 | 2010-02-03 00:55:45 +0000 | [diff] [blame] | 1203 | default: |
| 1204 | llvm_unreachable("unexpected statement kind"); |
| 1205 | break; |
| 1206 | |
John McCall | 1dd7383 | 2010-02-04 01:42:13 +0000 | [diff] [blame] | 1207 | case Expr::CallExprClass: { |
| 1208 | const CallExpr *CE = cast<CallExpr>(E); |
| 1209 | Out << "cl"; |
| 1210 | mangleCalledExpression(CE->getCallee(), CE->getNumArgs()); |
| 1211 | for (unsigned I = 0, N = CE->getNumArgs(); I != N; ++I) |
| 1212 | mangleExpression(CE->getArg(I)); |
| 1213 | Out << "E"; |
John McCall | 09cc141 | 2010-02-03 00:55:45 +0000 | [diff] [blame] | 1214 | break; |
John McCall | 1dd7383 | 2010-02-04 01:42:13 +0000 | [diff] [blame] | 1215 | } |
John McCall | 09cc141 | 2010-02-03 00:55:45 +0000 | [diff] [blame] | 1216 | |
John McCall | 2f27bf8 | 2010-02-04 02:56:29 +0000 | [diff] [blame] | 1217 | case Expr::MemberExprClass: { |
| 1218 | const MemberExpr *ME = cast<MemberExpr>(E); |
| 1219 | mangleMemberExpr(ME->getBase(), ME->isArrow(), |
| 1220 | ME->getQualifier(), ME->getMemberDecl()->getDeclName(), |
| 1221 | UnknownArity); |
| 1222 | break; |
| 1223 | } |
| 1224 | |
| 1225 | case Expr::UnresolvedMemberExprClass: { |
| 1226 | const UnresolvedMemberExpr *ME = cast<UnresolvedMemberExpr>(E); |
| 1227 | mangleMemberExpr(ME->getBase(), ME->isArrow(), |
| 1228 | ME->getQualifier(), ME->getMemberName(), |
| 1229 | UnknownArity); |
| 1230 | break; |
| 1231 | } |
| 1232 | |
| 1233 | case Expr::CXXDependentScopeMemberExprClass: { |
| 1234 | const CXXDependentScopeMemberExpr *ME |
| 1235 | = cast<CXXDependentScopeMemberExpr>(E); |
| 1236 | mangleMemberExpr(ME->getBase(), ME->isArrow(), |
| 1237 | ME->getQualifier(), ME->getMember(), |
| 1238 | UnknownArity); |
| 1239 | break; |
| 1240 | } |
| 1241 | |
John McCall | 1dd7383 | 2010-02-04 01:42:13 +0000 | [diff] [blame] | 1242 | case Expr::UnresolvedLookupExprClass: { |
John McCall | a3218e7 | 2010-02-04 01:48:38 +0000 | [diff] [blame] | 1243 | // The ABI doesn't cover how to mangle overload sets, so we mangle |
| 1244 | // using something as close as possible to the original lookup |
| 1245 | // expression. |
John McCall | 1dd7383 | 2010-02-04 01:42:13 +0000 | [diff] [blame] | 1246 | const UnresolvedLookupExpr *ULE = cast<UnresolvedLookupExpr>(E); |
| 1247 | mangleUnresolvedName(ULE->getQualifier(), ULE->getName(), UnknownArity); |
| 1248 | break; |
| 1249 | } |
| 1250 | |
| 1251 | case Expr::CXXUnresolvedConstructExprClass: { |
| 1252 | const CXXUnresolvedConstructExpr *CE = cast<CXXUnresolvedConstructExpr>(E); |
| 1253 | unsigned N = CE->arg_size(); |
| 1254 | |
| 1255 | Out << "cv"; |
| 1256 | mangleType(CE->getType()); |
| 1257 | if (N != 1) Out << "_"; |
| 1258 | for (unsigned I = 0; I != N; ++I) mangleExpression(CE->getArg(I)); |
| 1259 | if (N != 1) Out << "E"; |
John McCall | 09cc141 | 2010-02-03 00:55:45 +0000 | [diff] [blame] | 1260 | break; |
John McCall | 1dd7383 | 2010-02-04 01:42:13 +0000 | [diff] [blame] | 1261 | } |
John McCall | 09cc141 | 2010-02-03 00:55:45 +0000 | [diff] [blame] | 1262 | |
John McCall | 1dd7383 | 2010-02-04 01:42:13 +0000 | [diff] [blame] | 1263 | case Expr::CXXTemporaryObjectExprClass: |
| 1264 | case Expr::CXXConstructExprClass: { |
| 1265 | const CXXConstructExpr *CE = cast<CXXConstructExpr>(E); |
| 1266 | unsigned N = CE->getNumArgs(); |
| 1267 | |
| 1268 | Out << "cv"; |
| 1269 | mangleType(CE->getType()); |
| 1270 | if (N != 1) Out << "_"; |
| 1271 | for (unsigned I = 0; I != N; ++I) mangleExpression(CE->getArg(I)); |
| 1272 | if (N != 1) Out << "E"; |
John McCall | 09cc141 | 2010-02-03 00:55:45 +0000 | [diff] [blame] | 1273 | break; |
John McCall | 1dd7383 | 2010-02-04 01:42:13 +0000 | [diff] [blame] | 1274 | } |
| 1275 | |
| 1276 | case Expr::SizeOfAlignOfExprClass: { |
| 1277 | const SizeOfAlignOfExpr *SAE = cast<SizeOfAlignOfExpr>(E); |
| 1278 | if (SAE->isSizeOf()) Out << "s"; |
| 1279 | else Out << "a"; |
| 1280 | if (SAE->isArgumentType()) { |
| 1281 | Out << "t"; |
| 1282 | mangleType(SAE->getArgumentType()); |
| 1283 | } else { |
| 1284 | Out << "z"; |
| 1285 | mangleExpression(SAE->getArgumentExpr()); |
| 1286 | } |
| 1287 | break; |
| 1288 | } |
Anders Carlsson | a769408 | 2009-11-06 02:50:19 +0000 | [diff] [blame] | 1289 | |
Anders Carlsson | e170ba7 | 2009-12-14 01:45:37 +0000 | [diff] [blame] | 1290 | case Expr::UnaryOperatorClass: { |
| 1291 | const UnaryOperator *UO = cast<UnaryOperator>(E); |
| 1292 | mangleOperatorName(UnaryOperator::getOverloadedOperator(UO->getOpcode()), |
| 1293 | /*Arity=*/1); |
| 1294 | mangleExpression(UO->getSubExpr()); |
| 1295 | break; |
| 1296 | } |
| 1297 | |
| 1298 | case Expr::BinaryOperatorClass: { |
| 1299 | const BinaryOperator *BO = cast<BinaryOperator>(E); |
| 1300 | mangleOperatorName(BinaryOperator::getOverloadedOperator(BO->getOpcode()), |
| 1301 | /*Arity=*/2); |
| 1302 | mangleExpression(BO->getLHS()); |
| 1303 | mangleExpression(BO->getRHS()); |
| 1304 | break; |
John McCall | 2f27bf8 | 2010-02-04 02:56:29 +0000 | [diff] [blame] | 1305 | } |
Anders Carlsson | e170ba7 | 2009-12-14 01:45:37 +0000 | [diff] [blame] | 1306 | |
| 1307 | case Expr::ConditionalOperatorClass: { |
| 1308 | const ConditionalOperator *CO = cast<ConditionalOperator>(E); |
| 1309 | mangleOperatorName(OO_Conditional, /*Arity=*/3); |
| 1310 | mangleExpression(CO->getCond()); |
| 1311 | mangleExpression(CO->getLHS()); |
| 1312 | mangleExpression(CO->getRHS()); |
| 1313 | break; |
| 1314 | } |
| 1315 | |
Douglas Gregor | 46287c7 | 2010-01-29 16:37:09 +0000 | [diff] [blame] | 1316 | case Expr::ImplicitCastExprClass: { |
| 1317 | mangleExpression(cast<ImplicitCastExpr>(E)->getSubExpr()); |
| 1318 | break; |
| 1319 | } |
| 1320 | |
| 1321 | case Expr::CStyleCastExprClass: |
| 1322 | case Expr::CXXStaticCastExprClass: |
| 1323 | case Expr::CXXDynamicCastExprClass: |
| 1324 | case Expr::CXXReinterpretCastExprClass: |
| 1325 | case Expr::CXXConstCastExprClass: |
| 1326 | case Expr::CXXFunctionalCastExprClass: { |
| 1327 | const ExplicitCastExpr *ECE = cast<ExplicitCastExpr>(E); |
| 1328 | Out << "cv"; |
| 1329 | mangleType(ECE->getType()); |
| 1330 | mangleExpression(ECE->getSubExpr()); |
| 1331 | break; |
| 1332 | } |
| 1333 | |
Anders Carlsson | 58040a5 | 2009-12-16 05:48:46 +0000 | [diff] [blame] | 1334 | case Expr::CXXOperatorCallExprClass: { |
| 1335 | const CXXOperatorCallExpr *CE = cast<CXXOperatorCallExpr>(E); |
| 1336 | unsigned NumArgs = CE->getNumArgs(); |
| 1337 | mangleOperatorName(CE->getOperator(), /*Arity=*/NumArgs); |
| 1338 | // Mangle the arguments. |
| 1339 | for (unsigned i = 0; i != NumArgs; ++i) |
| 1340 | mangleExpression(CE->getArg(i)); |
| 1341 | break; |
| 1342 | } |
| 1343 | |
Anders Carlsson | a769408 | 2009-11-06 02:50:19 +0000 | [diff] [blame] | 1344 | case Expr::ParenExprClass: |
| 1345 | mangleExpression(cast<ParenExpr>(E)->getSubExpr()); |
| 1346 | break; |
| 1347 | |
Anders Carlsson | d553f8c | 2009-09-21 01:21:10 +0000 | [diff] [blame] | 1348 | case Expr::DeclRefExprClass: { |
Douglas Gregor | 5ed1bc3 | 2010-02-28 21:40:32 +0000 | [diff] [blame] | 1349 | const NamedDecl *D = cast<DeclRefExpr>(E)->getDecl(); |
Daniel Dunbar | 3c9e463 | 2009-11-21 09:05:47 +0000 | [diff] [blame] | 1350 | |
Anders Carlsson | d553f8c | 2009-09-21 01:21:10 +0000 | [diff] [blame] | 1351 | switch (D->getKind()) { |
Douglas Gregor | 5ed1bc3 | 2010-02-28 21:40:32 +0000 | [diff] [blame] | 1352 | default: |
| 1353 | // <expr-primary> ::= L <mangled-name> E # external name |
| 1354 | Out << 'L'; |
| 1355 | mangle(D, "_Z"); |
| 1356 | Out << 'E'; |
| 1357 | break; |
| 1358 | |
Anders Carlsson | d553f8c | 2009-09-21 01:21:10 +0000 | [diff] [blame] | 1359 | case Decl::NonTypeTemplateParm: { |
| 1360 | const NonTypeTemplateParmDecl *PD = cast<NonTypeTemplateParmDecl>(D); |
Anders Carlsson | 0ccdf8d | 2009-09-27 00:38:53 +0000 | [diff] [blame] | 1361 | mangleTemplateParameter(PD->getIndex()); |
Anders Carlsson | d553f8c | 2009-09-21 01:21:10 +0000 | [diff] [blame] | 1362 | break; |
| 1363 | } |
| 1364 | |
| 1365 | } |
Daniel Dunbar | 3c9e463 | 2009-11-21 09:05:47 +0000 | [diff] [blame] | 1366 | |
Anders Carlsson | 50755b0 | 2009-09-27 20:11:34 +0000 | [diff] [blame] | 1367 | break; |
Anders Carlsson | d553f8c | 2009-09-21 01:21:10 +0000 | [diff] [blame] | 1368 | } |
Daniel Dunbar | 3c9e463 | 2009-11-21 09:05:47 +0000 | [diff] [blame] | 1369 | |
John McCall | 865d447 | 2009-11-19 22:55:06 +0000 | [diff] [blame] | 1370 | case Expr::DependentScopeDeclRefExprClass: { |
| 1371 | const DependentScopeDeclRefExpr *DRE = cast<DependentScopeDeclRefExpr>(E); |
Douglas Gregor | 4b2ccfc | 2010-02-28 22:05:49 +0000 | [diff] [blame^] | 1372 | NestedNameSpecifier *NNS = DRE->getQualifier(); |
| 1373 | const Type *QTy = NNS->getAsType(); |
| 1374 | |
| 1375 | // When we're dealing with a nested-name-specifier that has just a |
| 1376 | // dependent identifier in it, mangle that as a typename. FIXME: |
| 1377 | // It isn't clear that we ever actually want to have such a |
| 1378 | // nested-name-specifier; why not just represent it as a typename type? |
| 1379 | if (!QTy && NNS->getAsIdentifier() && NNS->getPrefix()) { |
| 1380 | QTy = getASTContext().getTypenameType(NNS->getPrefix(), |
| 1381 | NNS->getAsIdentifier()) |
| 1382 | .getTypePtr(); |
| 1383 | } |
Anders Carlsson | 50755b0 | 2009-09-27 20:11:34 +0000 | [diff] [blame] | 1384 | assert(QTy && "Qualifier was not type!"); |
| 1385 | |
| 1386 | // ::= sr <type> <unqualified-name> # dependent name |
| 1387 | Out << "sr"; |
| 1388 | mangleType(QualType(QTy, 0)); |
Daniel Dunbar | 3c9e463 | 2009-11-21 09:05:47 +0000 | [diff] [blame] | 1389 | |
Anders Carlsson | 50755b0 | 2009-09-27 20:11:34 +0000 | [diff] [blame] | 1390 | assert(DRE->getDeclName().getNameKind() == DeclarationName::Identifier && |
| 1391 | "Unhandled decl name kind!"); |
| 1392 | mangleSourceName(DRE->getDeclName().getAsIdentifierInfo()); |
Daniel Dunbar | 3c9e463 | 2009-11-21 09:05:47 +0000 | [diff] [blame] | 1393 | |
Anders Carlsson | 50755b0 | 2009-09-27 20:11:34 +0000 | [diff] [blame] | 1394 | break; |
| 1395 | } |
| 1396 | |
John McCall | 1dd7383 | 2010-02-04 01:42:13 +0000 | [diff] [blame] | 1397 | case Expr::FloatingLiteralClass: { |
| 1398 | const FloatingLiteral *FL = cast<FloatingLiteral>(E); |
| 1399 | Out << "L"; |
| 1400 | mangleType(FL->getType()); |
| 1401 | |
| 1402 | // TODO: avoid this copy with careful stream management. |
| 1403 | llvm::SmallVector<char,20> Buffer; |
| 1404 | FL->getValue().bitcastToAPInt().toString(Buffer, 16, false); |
| 1405 | Out.write(Buffer.data(), Buffer.size()); |
| 1406 | |
| 1407 | Out << "E"; |
| 1408 | break; |
| 1409 | } |
| 1410 | |
Anders Carlsson | e170ba7 | 2009-12-14 01:45:37 +0000 | [diff] [blame] | 1411 | case Expr::IntegerLiteralClass: |
| 1412 | mangleIntegerLiteral(E->getType(), |
| 1413 | llvm::APSInt(cast<IntegerLiteral>(E)->getValue())); |
| 1414 | break; |
| 1415 | |
Anders Carlsson | d553f8c | 2009-09-21 01:21:10 +0000 | [diff] [blame] | 1416 | } |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 1417 | } |
| 1418 | |
John McCall | efe6aee | 2009-09-05 07:56:18 +0000 | [diff] [blame] | 1419 | // FIXME: <type> ::= G <type> # imaginary (C 2000) |
| 1420 | // FIXME: <type> ::= U <source-name> <type> # vendor extended type qualifier |
| 1421 | |
Anders Carlsson | 3ac86b5 | 2009-04-15 05:36:58 +0000 | [diff] [blame] | 1422 | void CXXNameMangler::mangleCXXCtorType(CXXCtorType T) { |
| 1423 | // <ctor-dtor-name> ::= C1 # complete object constructor |
| 1424 | // ::= C2 # base object constructor |
| 1425 | // ::= C3 # complete object allocating constructor |
| 1426 | // |
| 1427 | switch (T) { |
| 1428 | case Ctor_Complete: |
| 1429 | Out << "C1"; |
| 1430 | break; |
| 1431 | case Ctor_Base: |
| 1432 | Out << "C2"; |
| 1433 | break; |
| 1434 | case Ctor_CompleteAllocating: |
| 1435 | Out << "C3"; |
| 1436 | break; |
| 1437 | } |
| 1438 | } |
| 1439 | |
Anders Carlsson | 27ae536 | 2009-04-17 01:58:57 +0000 | [diff] [blame] | 1440 | void CXXNameMangler::mangleCXXDtorType(CXXDtorType T) { |
| 1441 | // <ctor-dtor-name> ::= D0 # deleting destructor |
| 1442 | // ::= D1 # complete object destructor |
| 1443 | // ::= D2 # base object destructor |
| 1444 | // |
| 1445 | switch (T) { |
| 1446 | case Dtor_Deleting: |
| 1447 | Out << "D0"; |
| 1448 | break; |
| 1449 | case Dtor_Complete: |
| 1450 | Out << "D1"; |
| 1451 | break; |
| 1452 | case Dtor_Base: |
| 1453 | Out << "D2"; |
| 1454 | break; |
| 1455 | } |
| 1456 | } |
| 1457 | |
Anders Carlsson | 9e85c74 | 2009-12-23 19:30:55 +0000 | [diff] [blame] | 1458 | void CXXNameMangler::mangleTemplateArgs(const TemplateArgumentList &L) { |
Anders Carlsson | 7a0ba87 | 2009-05-15 16:09:15 +0000 | [diff] [blame] | 1459 | // <template-args> ::= I <template-arg>+ E |
| 1460 | Out << "I"; |
Daniel Dunbar | 7e0c195 | 2009-11-21 09:17:15 +0000 | [diff] [blame] | 1461 | for (unsigned i = 0, e = L.size(); i != e; ++i) |
Anders Carlsson | 9e85c74 | 2009-12-23 19:30:55 +0000 | [diff] [blame] | 1462 | mangleTemplateArg(L[i]); |
Anders Carlsson | 7a0ba87 | 2009-05-15 16:09:15 +0000 | [diff] [blame] | 1463 | Out << "E"; |
| 1464 | } |
| 1465 | |
Anders Carlsson | 7624f21 | 2009-09-18 02:42:01 +0000 | [diff] [blame] | 1466 | void CXXNameMangler::mangleTemplateArgs(const TemplateArgument *TemplateArgs, |
| 1467 | unsigned NumTemplateArgs) { |
| 1468 | // <template-args> ::= I <template-arg>+ E |
| 1469 | Out << "I"; |
Daniel Dunbar | 7e0c195 | 2009-11-21 09:17:15 +0000 | [diff] [blame] | 1470 | for (unsigned i = 0; i != NumTemplateArgs; ++i) |
Anders Carlsson | 9e85c74 | 2009-12-23 19:30:55 +0000 | [diff] [blame] | 1471 | mangleTemplateArg(TemplateArgs[i]); |
Anders Carlsson | 7624f21 | 2009-09-18 02:42:01 +0000 | [diff] [blame] | 1472 | Out << "E"; |
| 1473 | } |
| 1474 | |
Anders Carlsson | 9e85c74 | 2009-12-23 19:30:55 +0000 | [diff] [blame] | 1475 | void CXXNameMangler::mangleTemplateArg(const TemplateArgument &A) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1476 | // <template-arg> ::= <type> # type or template |
Anders Carlsson | 7a0ba87 | 2009-05-15 16:09:15 +0000 | [diff] [blame] | 1477 | // ::= X <expression> E # expression |
| 1478 | // ::= <expr-primary> # simple expressions |
| 1479 | // ::= I <template-arg>* E # argument pack |
| 1480 | // ::= sp <expression> # pack expansion of (C++0x) |
| 1481 | switch (A.getKind()) { |
| 1482 | default: |
| 1483 | assert(0 && "Unknown template argument kind!"); |
| 1484 | case TemplateArgument::Type: |
| 1485 | mangleType(A.getAsType()); |
| 1486 | break; |
Anders Carlsson | 9e85c74 | 2009-12-23 19:30:55 +0000 | [diff] [blame] | 1487 | case TemplateArgument::Template: |
Douglas Gregor | 2d565b3 | 2010-02-06 01:09:36 +0000 | [diff] [blame] | 1488 | assert(A.getAsTemplate().getAsTemplateDecl() && |
| 1489 | "FIXME: Support dependent template names"); |
Anders Carlsson | 9e85c74 | 2009-12-23 19:30:55 +0000 | [diff] [blame] | 1490 | mangleName(A.getAsTemplate().getAsTemplateDecl()); |
| 1491 | break; |
Anders Carlsson | d553f8c | 2009-09-21 01:21:10 +0000 | [diff] [blame] | 1492 | case TemplateArgument::Expression: |
| 1493 | Out << 'X'; |
| 1494 | mangleExpression(A.getAsExpr()); |
| 1495 | Out << 'E'; |
| 1496 | break; |
Anders Carlsson | e170ba7 | 2009-12-14 01:45:37 +0000 | [diff] [blame] | 1497 | case TemplateArgument::Integral: |
| 1498 | mangleIntegerLiteral(A.getIntegralType(), *A.getAsIntegral()); |
Anders Carlsson | 7a0ba87 | 2009-05-15 16:09:15 +0000 | [diff] [blame] | 1499 | break; |
Daniel Dunbar | 7e0c195 | 2009-11-21 09:17:15 +0000 | [diff] [blame] | 1500 | case TemplateArgument::Declaration: { |
| 1501 | // <expr-primary> ::= L <mangled-name> E # external name |
| 1502 | |
| 1503 | // FIXME: Clang produces AST's where pointer-to-member-function expressions |
| 1504 | // and pointer-to-function expressions are represented as a declaration not |
| 1505 | // an expression; this is not how gcc represents them and this changes the |
| 1506 | // mangling. |
| 1507 | Out << 'L'; |
| 1508 | // References to external entities use the mangled name; if the name would |
| 1509 | // not normally be manged then mangle it as unqualified. |
| 1510 | // |
| 1511 | // FIXME: The ABI specifies that external names here should have _Z, but |
| 1512 | // gcc leaves this off. |
| 1513 | mangle(cast<NamedDecl>(A.getAsDecl()), "Z"); |
| 1514 | Out << 'E'; |
| 1515 | break; |
| 1516 | } |
| 1517 | } |
Anders Carlsson | 7a0ba87 | 2009-05-15 16:09:15 +0000 | [diff] [blame] | 1518 | } |
| 1519 | |
Anders Carlsson | 0ccdf8d | 2009-09-27 00:38:53 +0000 | [diff] [blame] | 1520 | void CXXNameMangler::mangleTemplateParameter(unsigned Index) { |
| 1521 | // <template-param> ::= T_ # first template parameter |
| 1522 | // ::= T <parameter-2 non-negative number> _ |
| 1523 | if (Index == 0) |
| 1524 | Out << "T_"; |
| 1525 | else |
| 1526 | Out << 'T' << (Index - 1) << '_'; |
| 1527 | } |
| 1528 | |
Anders Carlsson | 7696737 | 2009-09-17 00:43:46 +0000 | [diff] [blame] | 1529 | // <substitution> ::= S <seq-id> _ |
| 1530 | // ::= S_ |
Anders Carlsson | 6862fc7 | 2009-09-17 04:16:28 +0000 | [diff] [blame] | 1531 | bool CXXNameMangler::mangleSubstitution(const NamedDecl *ND) { |
Anders Carlsson | e7c8cb6 | 2009-09-26 20:53:44 +0000 | [diff] [blame] | 1532 | // Try one of the standard substitutions first. |
| 1533 | if (mangleStandardSubstitution(ND)) |
| 1534 | return true; |
Daniel Dunbar | 3c9e463 | 2009-11-21 09:05:47 +0000 | [diff] [blame] | 1535 | |
Anders Carlsson | 433d137 | 2009-11-07 04:26:04 +0000 | [diff] [blame] | 1536 | ND = cast<NamedDecl>(ND->getCanonicalDecl()); |
Anders Carlsson | 6862fc7 | 2009-09-17 04:16:28 +0000 | [diff] [blame] | 1537 | return mangleSubstitution(reinterpret_cast<uintptr_t>(ND)); |
| 1538 | } |
| 1539 | |
Anders Carlsson | 7696737 | 2009-09-17 00:43:46 +0000 | [diff] [blame] | 1540 | bool CXXNameMangler::mangleSubstitution(QualType T) { |
Anders Carlsson | d99edc4 | 2009-09-26 03:55:37 +0000 | [diff] [blame] | 1541 | if (!T.getCVRQualifiers()) { |
| 1542 | if (const RecordType *RT = T->getAs<RecordType>()) |
| 1543 | return mangleSubstitution(RT->getDecl()); |
| 1544 | } |
Daniel Dunbar | 3c9e463 | 2009-11-21 09:05:47 +0000 | [diff] [blame] | 1545 | |
Anders Carlsson | 7696737 | 2009-09-17 00:43:46 +0000 | [diff] [blame] | 1546 | uintptr_t TypePtr = reinterpret_cast<uintptr_t>(T.getAsOpaquePtr()); |
| 1547 | |
Anders Carlsson | d3a932a | 2009-09-17 03:53:28 +0000 | [diff] [blame] | 1548 | return mangleSubstitution(TypePtr); |
| 1549 | } |
| 1550 | |
| 1551 | bool CXXNameMangler::mangleSubstitution(uintptr_t Ptr) { |
Daniel Dunbar | 3c9e463 | 2009-11-21 09:05:47 +0000 | [diff] [blame] | 1552 | llvm::DenseMap<uintptr_t, unsigned>::iterator I = |
Anders Carlsson | d3a932a | 2009-09-17 03:53:28 +0000 | [diff] [blame] | 1553 | Substitutions.find(Ptr); |
Anders Carlsson | 7696737 | 2009-09-17 00:43:46 +0000 | [diff] [blame] | 1554 | if (I == Substitutions.end()) |
| 1555 | return false; |
Daniel Dunbar | 3c9e463 | 2009-11-21 09:05:47 +0000 | [diff] [blame] | 1556 | |
Anders Carlsson | 7696737 | 2009-09-17 00:43:46 +0000 | [diff] [blame] | 1557 | unsigned SeqID = I->second; |
| 1558 | if (SeqID == 0) |
| 1559 | Out << "S_"; |
| 1560 | else { |
| 1561 | SeqID--; |
Daniel Dunbar | 3c9e463 | 2009-11-21 09:05:47 +0000 | [diff] [blame] | 1562 | |
Anders Carlsson | 7696737 | 2009-09-17 00:43:46 +0000 | [diff] [blame] | 1563 | // <seq-id> is encoded in base-36, using digits and upper case letters. |
| 1564 | char Buffer[10]; |
| 1565 | char *BufferPtr = Buffer + 9; |
Daniel Dunbar | 3c9e463 | 2009-11-21 09:05:47 +0000 | [diff] [blame] | 1566 | |
Anders Carlsson | 7696737 | 2009-09-17 00:43:46 +0000 | [diff] [blame] | 1567 | *BufferPtr = 0; |
| 1568 | if (SeqID == 0) *--BufferPtr = '0'; |
Daniel Dunbar | 3c9e463 | 2009-11-21 09:05:47 +0000 | [diff] [blame] | 1569 | |
Anders Carlsson | 7696737 | 2009-09-17 00:43:46 +0000 | [diff] [blame] | 1570 | while (SeqID) { |
| 1571 | assert(BufferPtr > Buffer && "Buffer overflow!"); |
Daniel Dunbar | 3c9e463 | 2009-11-21 09:05:47 +0000 | [diff] [blame] | 1572 | |
Anders Carlsson | 7696737 | 2009-09-17 00:43:46 +0000 | [diff] [blame] | 1573 | unsigned char c = static_cast<unsigned char>(SeqID) % 36; |
Daniel Dunbar | 3c9e463 | 2009-11-21 09:05:47 +0000 | [diff] [blame] | 1574 | |
Anders Carlsson | 7696737 | 2009-09-17 00:43:46 +0000 | [diff] [blame] | 1575 | *--BufferPtr = (c < 10 ? '0' + c : 'A' + c - 10); |
| 1576 | SeqID /= 36; |
| 1577 | } |
Daniel Dunbar | 3c9e463 | 2009-11-21 09:05:47 +0000 | [diff] [blame] | 1578 | |
Anders Carlsson | 7696737 | 2009-09-17 00:43:46 +0000 | [diff] [blame] | 1579 | Out << 'S' << BufferPtr << '_'; |
| 1580 | } |
Daniel Dunbar | 3c9e463 | 2009-11-21 09:05:47 +0000 | [diff] [blame] | 1581 | |
Anders Carlsson | 7696737 | 2009-09-17 00:43:46 +0000 | [diff] [blame] | 1582 | return true; |
| 1583 | } |
| 1584 | |
Anders Carlsson | f514b54 | 2009-09-27 00:12:57 +0000 | [diff] [blame] | 1585 | static bool isCharType(QualType T) { |
| 1586 | if (T.isNull()) |
| 1587 | return false; |
Daniel Dunbar | 3c9e463 | 2009-11-21 09:05:47 +0000 | [diff] [blame] | 1588 | |
Anders Carlsson | f514b54 | 2009-09-27 00:12:57 +0000 | [diff] [blame] | 1589 | return T->isSpecificBuiltinType(BuiltinType::Char_S) || |
| 1590 | T->isSpecificBuiltinType(BuiltinType::Char_U); |
| 1591 | } |
| 1592 | |
Daniel Dunbar | 3c9e463 | 2009-11-21 09:05:47 +0000 | [diff] [blame] | 1593 | /// isCharSpecialization - Returns whether a given type is a template |
Anders Carlsson | f514b54 | 2009-09-27 00:12:57 +0000 | [diff] [blame] | 1594 | /// specialization of a given name with a single argument of type char. |
| 1595 | static bool isCharSpecialization(QualType T, const char *Name) { |
| 1596 | if (T.isNull()) |
| 1597 | return false; |
Daniel Dunbar | 3c9e463 | 2009-11-21 09:05:47 +0000 | [diff] [blame] | 1598 | |
Anders Carlsson | f514b54 | 2009-09-27 00:12:57 +0000 | [diff] [blame] | 1599 | const RecordType *RT = T->getAs<RecordType>(); |
| 1600 | if (!RT) |
| 1601 | return false; |
Daniel Dunbar | 3c9e463 | 2009-11-21 09:05:47 +0000 | [diff] [blame] | 1602 | |
| 1603 | const ClassTemplateSpecializationDecl *SD = |
Anders Carlsson | f514b54 | 2009-09-27 00:12:57 +0000 | [diff] [blame] | 1604 | dyn_cast<ClassTemplateSpecializationDecl>(RT->getDecl()); |
| 1605 | if (!SD) |
| 1606 | return false; |
| 1607 | |
| 1608 | if (!isStdNamespace(SD->getDeclContext())) |
| 1609 | return false; |
Daniel Dunbar | 3c9e463 | 2009-11-21 09:05:47 +0000 | [diff] [blame] | 1610 | |
Anders Carlsson | f514b54 | 2009-09-27 00:12:57 +0000 | [diff] [blame] | 1611 | const TemplateArgumentList &TemplateArgs = SD->getTemplateArgs(); |
| 1612 | if (TemplateArgs.size() != 1) |
| 1613 | return false; |
Daniel Dunbar | 3c9e463 | 2009-11-21 09:05:47 +0000 | [diff] [blame] | 1614 | |
Anders Carlsson | f514b54 | 2009-09-27 00:12:57 +0000 | [diff] [blame] | 1615 | if (!isCharType(TemplateArgs[0].getAsType())) |
| 1616 | return false; |
Daniel Dunbar | 3c9e463 | 2009-11-21 09:05:47 +0000 | [diff] [blame] | 1617 | |
Daniel Dunbar | 01eb9b9 | 2009-10-18 21:17:35 +0000 | [diff] [blame] | 1618 | return SD->getIdentifier()->getName() == Name; |
Anders Carlsson | f514b54 | 2009-09-27 00:12:57 +0000 | [diff] [blame] | 1619 | } |
| 1620 | |
Anders Carlsson | 91f8860 | 2009-12-07 19:56:42 +0000 | [diff] [blame] | 1621 | template <std::size_t StrLen> |
| 1622 | bool isStreamCharSpecialization(const ClassTemplateSpecializationDecl *SD, |
| 1623 | const char (&Str)[StrLen]) { |
| 1624 | if (!SD->getIdentifier()->isStr(Str)) |
| 1625 | return false; |
| 1626 | |
| 1627 | const TemplateArgumentList &TemplateArgs = SD->getTemplateArgs(); |
| 1628 | if (TemplateArgs.size() != 2) |
| 1629 | return false; |
| 1630 | |
| 1631 | if (!isCharType(TemplateArgs[0].getAsType())) |
| 1632 | return false; |
| 1633 | |
| 1634 | if (!isCharSpecialization(TemplateArgs[1].getAsType(), "char_traits")) |
| 1635 | return false; |
| 1636 | |
| 1637 | return true; |
| 1638 | } |
| 1639 | |
Anders Carlsson | e7c8cb6 | 2009-09-26 20:53:44 +0000 | [diff] [blame] | 1640 | bool CXXNameMangler::mangleStandardSubstitution(const NamedDecl *ND) { |
| 1641 | // <substitution> ::= St # ::std:: |
Anders Carlsson | 8c03155 | 2009-09-26 23:10:05 +0000 | [diff] [blame] | 1642 | if (const NamespaceDecl *NS = dyn_cast<NamespaceDecl>(ND)) { |
Anders Carlsson | 47846d2 | 2009-12-04 06:23:23 +0000 | [diff] [blame] | 1643 | if (isStd(NS)) { |
Anders Carlsson | 8c03155 | 2009-09-26 23:10:05 +0000 | [diff] [blame] | 1644 | Out << "St"; |
| 1645 | return true; |
| 1646 | } |
| 1647 | } |
| 1648 | |
| 1649 | if (const ClassTemplateDecl *TD = dyn_cast<ClassTemplateDecl>(ND)) { |
| 1650 | if (!isStdNamespace(TD->getDeclContext())) |
| 1651 | return false; |
Daniel Dunbar | 3c9e463 | 2009-11-21 09:05:47 +0000 | [diff] [blame] | 1652 | |
Anders Carlsson | 8c03155 | 2009-09-26 23:10:05 +0000 | [diff] [blame] | 1653 | // <substitution> ::= Sa # ::std::allocator |
| 1654 | if (TD->getIdentifier()->isStr("allocator")) { |
| 1655 | Out << "Sa"; |
| 1656 | return true; |
| 1657 | } |
Daniel Dunbar | 3c9e463 | 2009-11-21 09:05:47 +0000 | [diff] [blame] | 1658 | |
Anders Carlsson | 189d59c | 2009-09-26 23:14:39 +0000 | [diff] [blame] | 1659 | // <<substitution> ::= Sb # ::std::basic_string |
| 1660 | if (TD->getIdentifier()->isStr("basic_string")) { |
| 1661 | Out << "Sb"; |
| 1662 | return true; |
| 1663 | } |
Anders Carlsson | 8c03155 | 2009-09-26 23:10:05 +0000 | [diff] [blame] | 1664 | } |
Daniel Dunbar | 3c9e463 | 2009-11-21 09:05:47 +0000 | [diff] [blame] | 1665 | |
| 1666 | if (const ClassTemplateSpecializationDecl *SD = |
Anders Carlsson | f514b54 | 2009-09-27 00:12:57 +0000 | [diff] [blame] | 1667 | dyn_cast<ClassTemplateSpecializationDecl>(ND)) { |
Eli Friedman | 5370ee2 | 2010-02-23 18:25:09 +0000 | [diff] [blame] | 1668 | if (!isStdNamespace(SD->getDeclContext())) |
| 1669 | return false; |
| 1670 | |
Anders Carlsson | f514b54 | 2009-09-27 00:12:57 +0000 | [diff] [blame] | 1671 | // <substitution> ::= Ss # ::std::basic_string<char, |
| 1672 | // ::std::char_traits<char>, |
| 1673 | // ::std::allocator<char> > |
| 1674 | if (SD->getIdentifier()->isStr("basic_string")) { |
| 1675 | const TemplateArgumentList &TemplateArgs = SD->getTemplateArgs(); |
Daniel Dunbar | 3c9e463 | 2009-11-21 09:05:47 +0000 | [diff] [blame] | 1676 | |
Anders Carlsson | f514b54 | 2009-09-27 00:12:57 +0000 | [diff] [blame] | 1677 | if (TemplateArgs.size() != 3) |
| 1678 | return false; |
Daniel Dunbar | 3c9e463 | 2009-11-21 09:05:47 +0000 | [diff] [blame] | 1679 | |
Anders Carlsson | f514b54 | 2009-09-27 00:12:57 +0000 | [diff] [blame] | 1680 | if (!isCharType(TemplateArgs[0].getAsType())) |
| 1681 | return false; |
Daniel Dunbar | 3c9e463 | 2009-11-21 09:05:47 +0000 | [diff] [blame] | 1682 | |
Anders Carlsson | f514b54 | 2009-09-27 00:12:57 +0000 | [diff] [blame] | 1683 | if (!isCharSpecialization(TemplateArgs[1].getAsType(), "char_traits")) |
| 1684 | return false; |
Daniel Dunbar | 3c9e463 | 2009-11-21 09:05:47 +0000 | [diff] [blame] | 1685 | |
Anders Carlsson | f514b54 | 2009-09-27 00:12:57 +0000 | [diff] [blame] | 1686 | if (!isCharSpecialization(TemplateArgs[2].getAsType(), "allocator")) |
| 1687 | return false; |
| 1688 | |
| 1689 | Out << "Ss"; |
| 1690 | return true; |
| 1691 | } |
Daniel Dunbar | 3c9e463 | 2009-11-21 09:05:47 +0000 | [diff] [blame] | 1692 | |
Anders Carlsson | 91f8860 | 2009-12-07 19:56:42 +0000 | [diff] [blame] | 1693 | // <substitution> ::= Si # ::std::basic_istream<char, |
| 1694 | // ::std::char_traits<char> > |
| 1695 | if (isStreamCharSpecialization(SD, "basic_istream")) { |
| 1696 | Out << "Si"; |
| 1697 | return true; |
| 1698 | } |
| 1699 | |
Daniel Dunbar | 3c9e463 | 2009-11-21 09:05:47 +0000 | [diff] [blame] | 1700 | // <substitution> ::= So # ::std::basic_ostream<char, |
Anders Carlsson | 8f8fd8e | 2009-10-08 17:20:26 +0000 | [diff] [blame] | 1701 | // ::std::char_traits<char> > |
Anders Carlsson | 91f8860 | 2009-12-07 19:56:42 +0000 | [diff] [blame] | 1702 | if (isStreamCharSpecialization(SD, "basic_ostream")) { |
Anders Carlsson | 8f8fd8e | 2009-10-08 17:20:26 +0000 | [diff] [blame] | 1703 | Out << "So"; |
| 1704 | return true; |
| 1705 | } |
Anders Carlsson | 91f8860 | 2009-12-07 19:56:42 +0000 | [diff] [blame] | 1706 | |
| 1707 | // <substitution> ::= Sd # ::std::basic_iostream<char, |
| 1708 | // ::std::char_traits<char> > |
| 1709 | if (isStreamCharSpecialization(SD, "basic_iostream")) { |
| 1710 | Out << "Sd"; |
| 1711 | return true; |
| 1712 | } |
Anders Carlsson | f514b54 | 2009-09-27 00:12:57 +0000 | [diff] [blame] | 1713 | } |
Anders Carlsson | 8c03155 | 2009-09-26 23:10:05 +0000 | [diff] [blame] | 1714 | return false; |
Anders Carlsson | e7c8cb6 | 2009-09-26 20:53:44 +0000 | [diff] [blame] | 1715 | } |
| 1716 | |
Anders Carlsson | 7696737 | 2009-09-17 00:43:46 +0000 | [diff] [blame] | 1717 | void CXXNameMangler::addSubstitution(QualType T) { |
Anders Carlsson | d99edc4 | 2009-09-26 03:55:37 +0000 | [diff] [blame] | 1718 | if (!T.getCVRQualifiers()) { |
| 1719 | if (const RecordType *RT = T->getAs<RecordType>()) { |
| 1720 | addSubstitution(RT->getDecl()); |
| 1721 | return; |
| 1722 | } |
| 1723 | } |
Daniel Dunbar | 3c9e463 | 2009-11-21 09:05:47 +0000 | [diff] [blame] | 1724 | |
Anders Carlsson | 7696737 | 2009-09-17 00:43:46 +0000 | [diff] [blame] | 1725 | uintptr_t TypePtr = reinterpret_cast<uintptr_t>(T.getAsOpaquePtr()); |
Anders Carlsson | d3a932a | 2009-09-17 03:53:28 +0000 | [diff] [blame] | 1726 | addSubstitution(TypePtr); |
| 1727 | } |
| 1728 | |
| 1729 | void CXXNameMangler::addSubstitution(uintptr_t Ptr) { |
Anders Carlsson | 7696737 | 2009-09-17 00:43:46 +0000 | [diff] [blame] | 1730 | unsigned SeqID = Substitutions.size(); |
Daniel Dunbar | 3c9e463 | 2009-11-21 09:05:47 +0000 | [diff] [blame] | 1731 | |
Anders Carlsson | d3a932a | 2009-09-17 03:53:28 +0000 | [diff] [blame] | 1732 | assert(!Substitutions.count(Ptr) && "Substitution already exists!"); |
Daniel Dunbar | 3c9e463 | 2009-11-21 09:05:47 +0000 | [diff] [blame] | 1733 | Substitutions[Ptr] = SeqID; |
Anders Carlsson | 7696737 | 2009-09-17 00:43:46 +0000 | [diff] [blame] | 1734 | } |
| 1735 | |
Daniel Dunbar | 1b07711 | 2009-11-21 09:06:10 +0000 | [diff] [blame] | 1736 | // |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1737 | |
Daniel Dunbar | 1b07711 | 2009-11-21 09:06:10 +0000 | [diff] [blame] | 1738 | /// \brief Mangles the name of the declaration D and emits that name to the |
| 1739 | /// given output stream. |
| 1740 | /// |
| 1741 | /// If the declaration D requires a mangled name, this routine will emit that |
| 1742 | /// mangled name to \p os and return true. Otherwise, \p os will be unchanged |
| 1743 | /// and this routine will return false. In this case, the caller should just |
| 1744 | /// emit the identifier of the declaration (\c D->getIdentifier()) as its |
| 1745 | /// name. |
Daniel Dunbar | f981bf8 | 2009-11-21 09:14:52 +0000 | [diff] [blame] | 1746 | void MangleContext::mangleName(const NamedDecl *D, |
Daniel Dunbar | 94fd26d | 2009-11-21 09:06:22 +0000 | [diff] [blame] | 1747 | llvm::SmallVectorImpl<char> &Res) { |
Daniel Dunbar | c02ab4c | 2009-11-21 09:14:44 +0000 | [diff] [blame] | 1748 | assert((isa<FunctionDecl>(D) || isa<VarDecl>(D)) && |
| 1749 | "Invalid mangleName() call, argument is not a variable or function!"); |
| 1750 | assert(!isa<CXXConstructorDecl>(D) && !isa<CXXDestructorDecl>(D) && |
| 1751 | "Invalid mangleName() call on 'structor decl!"); |
Daniel Dunbar | 3c9e463 | 2009-11-21 09:05:47 +0000 | [diff] [blame] | 1752 | |
Daniel Dunbar | 1b07711 | 2009-11-21 09:06:10 +0000 | [diff] [blame] | 1753 | PrettyStackTraceDecl CrashInfo(D, SourceLocation(), |
| 1754 | getASTContext().getSourceManager(), |
| 1755 | "Mangling declaration"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1756 | |
Daniel Dunbar | 94fd26d | 2009-11-21 09:06:22 +0000 | [diff] [blame] | 1757 | CXXNameMangler Mangler(*this, Res); |
| 1758 | return Mangler.mangle(D); |
Daniel Dunbar | 1b07711 | 2009-11-21 09:06:10 +0000 | [diff] [blame] | 1759 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1760 | |
Daniel Dunbar | 1b07711 | 2009-11-21 09:06:10 +0000 | [diff] [blame] | 1761 | void MangleContext::mangleCXXCtor(const CXXConstructorDecl *D, CXXCtorType Type, |
Daniel Dunbar | 94fd26d | 2009-11-21 09:06:22 +0000 | [diff] [blame] | 1762 | llvm::SmallVectorImpl<char> &Res) { |
Daniel Dunbar | 77939c9 | 2009-11-21 09:06:31 +0000 | [diff] [blame] | 1763 | CXXNameMangler Mangler(*this, Res, D, Type); |
| 1764 | Mangler.mangle(D); |
Daniel Dunbar | 1b07711 | 2009-11-21 09:06:10 +0000 | [diff] [blame] | 1765 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1766 | |
Daniel Dunbar | 1b07711 | 2009-11-21 09:06:10 +0000 | [diff] [blame] | 1767 | void MangleContext::mangleCXXDtor(const CXXDestructorDecl *D, CXXDtorType Type, |
Daniel Dunbar | 94fd26d | 2009-11-21 09:06:22 +0000 | [diff] [blame] | 1768 | llvm::SmallVectorImpl<char> &Res) { |
Daniel Dunbar | 77939c9 | 2009-11-21 09:06:31 +0000 | [diff] [blame] | 1769 | CXXNameMangler Mangler(*this, Res, D, Type); |
| 1770 | Mangler.mangle(D); |
Daniel Dunbar | 1b07711 | 2009-11-21 09:06:10 +0000 | [diff] [blame] | 1771 | } |
Mike Stump | f121677 | 2009-07-31 18:25:34 +0000 | [diff] [blame] | 1772 | |
Daniel Dunbar | c074771 | 2009-11-21 09:12:13 +0000 | [diff] [blame] | 1773 | /// \brief Mangles the a thunk with the offset n for the declaration D and |
| 1774 | /// emits that name to the given output stream. |
Anders Carlsson | a94822e | 2009-11-26 02:32:05 +0000 | [diff] [blame] | 1775 | void MangleContext::mangleThunk(const FunctionDecl *FD, |
Anders Carlsson | b73a5be | 2009-11-26 02:49:32 +0000 | [diff] [blame] | 1776 | const ThunkAdjustment &ThisAdjustment, |
Daniel Dunbar | c074771 | 2009-11-21 09:12:13 +0000 | [diff] [blame] | 1777 | llvm::SmallVectorImpl<char> &Res) { |
Daniel Dunbar | c074771 | 2009-11-21 09:12:13 +0000 | [diff] [blame] | 1778 | assert(!isa<CXXDestructorDecl>(FD) && |
| 1779 | "Use mangleCXXDtor for destructor decls!"); |
| 1780 | |
| 1781 | // <special-name> ::= T <call-offset> <base encoding> |
| 1782 | // # base is the nominal target function of thunk |
| 1783 | CXXNameMangler Mangler(*this, Res); |
| 1784 | Mangler.getStream() << "_ZT"; |
Anders Carlsson | b73a5be | 2009-11-26 02:49:32 +0000 | [diff] [blame] | 1785 | Mangler.mangleCallOffset(ThisAdjustment); |
Daniel Dunbar | c074771 | 2009-11-21 09:12:13 +0000 | [diff] [blame] | 1786 | Mangler.mangleFunctionEncoding(FD); |
| 1787 | } |
| 1788 | |
Eli Friedman | 61d89b6 | 2009-12-03 00:03:05 +0000 | [diff] [blame] | 1789 | void MangleContext::mangleCXXDtorThunk(const CXXDestructorDecl *D, |
| 1790 | CXXDtorType Type, |
| 1791 | const ThunkAdjustment &ThisAdjustment, |
| 1792 | llvm::SmallVectorImpl<char> &Res) { |
| 1793 | // <special-name> ::= T <call-offset> <base encoding> |
| 1794 | // # base is the nominal target function of thunk |
| 1795 | CXXNameMangler Mangler(*this, Res, D, Type); |
| 1796 | Mangler.getStream() << "_ZT"; |
| 1797 | Mangler.mangleCallOffset(ThisAdjustment); |
| 1798 | Mangler.mangleFunctionEncoding(D); |
| 1799 | } |
| 1800 | |
Daniel Dunbar | c074771 | 2009-11-21 09:12:13 +0000 | [diff] [blame] | 1801 | /// \brief Mangles the a covariant thunk for the declaration D and emits that |
| 1802 | /// name to the given output stream. |
Anders Carlsson | 7622cd3 | 2009-11-26 03:09:37 +0000 | [diff] [blame] | 1803 | void |
| 1804 | MangleContext::mangleCovariantThunk(const FunctionDecl *FD, |
| 1805 | const CovariantThunkAdjustment& Adjustment, |
| 1806 | llvm::SmallVectorImpl<char> &Res) { |
Daniel Dunbar | c074771 | 2009-11-21 09:12:13 +0000 | [diff] [blame] | 1807 | assert(!isa<CXXDestructorDecl>(FD) && |
Eli Friedman | 61d89b6 | 2009-12-03 00:03:05 +0000 | [diff] [blame] | 1808 | "No such thing as a covariant thunk for a destructor!"); |
Daniel Dunbar | c074771 | 2009-11-21 09:12:13 +0000 | [diff] [blame] | 1809 | |
| 1810 | // <special-name> ::= Tc <call-offset> <call-offset> <base encoding> |
| 1811 | // # base is the nominal target function of thunk |
| 1812 | // # first call-offset is 'this' adjustment |
| 1813 | // # second call-offset is result adjustment |
| 1814 | CXXNameMangler Mangler(*this, Res); |
| 1815 | Mangler.getStream() << "_ZTc"; |
Anders Carlsson | 7622cd3 | 2009-11-26 03:09:37 +0000 | [diff] [blame] | 1816 | Mangler.mangleCallOffset(Adjustment.ThisAdjustment); |
| 1817 | Mangler.mangleCallOffset(Adjustment.ReturnAdjustment); |
Daniel Dunbar | c074771 | 2009-11-21 09:12:13 +0000 | [diff] [blame] | 1818 | Mangler.mangleFunctionEncoding(FD); |
| 1819 | } |
| 1820 | |
| 1821 | /// mangleGuardVariable - Returns the mangled name for a guard variable |
| 1822 | /// for the passed in VarDecl. |
| 1823 | void MangleContext::mangleGuardVariable(const VarDecl *D, |
| 1824 | llvm::SmallVectorImpl<char> &Res) { |
| 1825 | // <special-name> ::= GV <object name> # Guard variable for one-time |
| 1826 | // # initialization |
| 1827 | CXXNameMangler Mangler(*this, Res); |
| 1828 | Mangler.getStream() << "_ZGV"; |
| 1829 | Mangler.mangleName(D); |
| 1830 | } |
| 1831 | |
Daniel Dunbar | 1b07711 | 2009-11-21 09:06:10 +0000 | [diff] [blame] | 1832 | void MangleContext::mangleCXXVtable(const CXXRecordDecl *RD, |
Daniel Dunbar | 94fd26d | 2009-11-21 09:06:22 +0000 | [diff] [blame] | 1833 | llvm::SmallVectorImpl<char> &Res) { |
Daniel Dunbar | c074771 | 2009-11-21 09:12:13 +0000 | [diff] [blame] | 1834 | // <special-name> ::= TV <type> # virtual table |
Daniel Dunbar | 94fd26d | 2009-11-21 09:06:22 +0000 | [diff] [blame] | 1835 | CXXNameMangler Mangler(*this, Res); |
Daniel Dunbar | c074771 | 2009-11-21 09:12:13 +0000 | [diff] [blame] | 1836 | Mangler.getStream() << "_ZTV"; |
| 1837 | Mangler.mangleName(RD); |
Daniel Dunbar | 1b07711 | 2009-11-21 09:06:10 +0000 | [diff] [blame] | 1838 | } |
Mike Stump | 82d75b0 | 2009-11-10 01:58:37 +0000 | [diff] [blame] | 1839 | |
Daniel Dunbar | 1b07711 | 2009-11-21 09:06:10 +0000 | [diff] [blame] | 1840 | void MangleContext::mangleCXXVTT(const CXXRecordDecl *RD, |
Daniel Dunbar | 94fd26d | 2009-11-21 09:06:22 +0000 | [diff] [blame] | 1841 | llvm::SmallVectorImpl<char> &Res) { |
Daniel Dunbar | c074771 | 2009-11-21 09:12:13 +0000 | [diff] [blame] | 1842 | // <special-name> ::= TT <type> # VTT structure |
Daniel Dunbar | 94fd26d | 2009-11-21 09:06:22 +0000 | [diff] [blame] | 1843 | CXXNameMangler Mangler(*this, Res); |
Daniel Dunbar | c074771 | 2009-11-21 09:12:13 +0000 | [diff] [blame] | 1844 | Mangler.getStream() << "_ZTT"; |
| 1845 | Mangler.mangleName(RD); |
Daniel Dunbar | 1b07711 | 2009-11-21 09:06:10 +0000 | [diff] [blame] | 1846 | } |
Mike Stump | ab3f7e9 | 2009-11-10 01:41:59 +0000 | [diff] [blame] | 1847 | |
Daniel Dunbar | 1b07711 | 2009-11-21 09:06:10 +0000 | [diff] [blame] | 1848 | void MangleContext::mangleCXXCtorVtable(const CXXRecordDecl *RD, int64_t Offset, |
| 1849 | const CXXRecordDecl *Type, |
Daniel Dunbar | 94fd26d | 2009-11-21 09:06:22 +0000 | [diff] [blame] | 1850 | llvm::SmallVectorImpl<char> &Res) { |
Daniel Dunbar | c074771 | 2009-11-21 09:12:13 +0000 | [diff] [blame] | 1851 | // <special-name> ::= TC <type> <offset number> _ <base type> |
Daniel Dunbar | 94fd26d | 2009-11-21 09:06:22 +0000 | [diff] [blame] | 1852 | CXXNameMangler Mangler(*this, Res); |
Daniel Dunbar | c074771 | 2009-11-21 09:12:13 +0000 | [diff] [blame] | 1853 | Mangler.getStream() << "_ZTC"; |
| 1854 | Mangler.mangleName(RD); |
| 1855 | Mangler.getStream() << Offset; |
| 1856 | Mangler.getStream() << "_"; |
| 1857 | Mangler.mangleName(Type); |
Daniel Dunbar | 1b07711 | 2009-11-21 09:06:10 +0000 | [diff] [blame] | 1858 | } |
Mike Stump | 738f8c2 | 2009-07-31 23:15:31 +0000 | [diff] [blame] | 1859 | |
Mike Stump | de05057 | 2009-12-02 18:57:08 +0000 | [diff] [blame] | 1860 | void MangleContext::mangleCXXRTTI(QualType Ty, |
Daniel Dunbar | 94fd26d | 2009-11-21 09:06:22 +0000 | [diff] [blame] | 1861 | llvm::SmallVectorImpl<char> &Res) { |
Daniel Dunbar | c074771 | 2009-11-21 09:12:13 +0000 | [diff] [blame] | 1862 | // <special-name> ::= TI <type> # typeinfo structure |
Douglas Gregor | 154fe98 | 2009-12-23 22:04:40 +0000 | [diff] [blame] | 1863 | assert(!Ty.hasQualifiers() && "RTTI info cannot have top-level qualifiers"); |
Daniel Dunbar | 94fd26d | 2009-11-21 09:06:22 +0000 | [diff] [blame] | 1864 | CXXNameMangler Mangler(*this, Res); |
Daniel Dunbar | c074771 | 2009-11-21 09:12:13 +0000 | [diff] [blame] | 1865 | Mangler.getStream() << "_ZTI"; |
| 1866 | Mangler.mangleType(Ty); |
Daniel Dunbar | 1b07711 | 2009-11-21 09:06:10 +0000 | [diff] [blame] | 1867 | } |
Mike Stump | 6779598 | 2009-11-14 00:14:13 +0000 | [diff] [blame] | 1868 | |
Mike Stump | de05057 | 2009-12-02 18:57:08 +0000 | [diff] [blame] | 1869 | void MangleContext::mangleCXXRTTIName(QualType Ty, |
Daniel Dunbar | 94fd26d | 2009-11-21 09:06:22 +0000 | [diff] [blame] | 1870 | llvm::SmallVectorImpl<char> &Res) { |
Daniel Dunbar | c074771 | 2009-11-21 09:12:13 +0000 | [diff] [blame] | 1871 | // <special-name> ::= TS <type> # typeinfo name (null terminated byte string) |
Daniel Dunbar | 94fd26d | 2009-11-21 09:06:22 +0000 | [diff] [blame] | 1872 | CXXNameMangler Mangler(*this, Res); |
Daniel Dunbar | c074771 | 2009-11-21 09:12:13 +0000 | [diff] [blame] | 1873 | Mangler.getStream() << "_ZTS"; |
| 1874 | Mangler.mangleType(Ty); |
Mike Stump | f121677 | 2009-07-31 18:25:34 +0000 | [diff] [blame] | 1875 | } |