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