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