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