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