Douglas Gregor | 6ec3668 | 2009-02-18 23:53:56 +0000 | [diff] [blame] | 1 | //===--- Mangle.cpp - Mangle C++ Names --------------------------*- C++ -*-===// |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // Implements C++ name mangling according to the Itanium C++ ABI, |
| 11 | // which is used in GCC 3.2 and newer (and many compilers that are |
| 12 | // ABI-compatible with GCC): |
| 13 | // |
| 14 | // http://www.codesourcery.com/public/cxx-abi/abi.html |
| 15 | // |
| 16 | //===----------------------------------------------------------------------===// |
| 17 | #include "Mangle.h" |
| 18 | #include "clang/AST/ASTContext.h" |
| 19 | #include "clang/AST/Decl.h" |
| 20 | #include "clang/AST/DeclCXX.h" |
Anders Carlsson | a40c5e4 | 2009-03-07 22:03:21 +0000 | [diff] [blame] | 21 | #include "clang/AST/DeclObjC.h" |
Anders Carlsson | 7a0ba87 | 2009-05-15 16:09:15 +0000 | [diff] [blame] | 22 | #include "clang/AST/DeclTemplate.h" |
Anders Carlsson | 50755b0 | 2009-09-27 20:11:34 +0000 | [diff] [blame] | 23 | #include "clang/AST/ExprCXX.h" |
Douglas Gregor | 6ec3668 | 2009-02-18 23:53:56 +0000 | [diff] [blame] | 24 | #include "clang/Basic/SourceManager.h" |
Anders Carlsson | c4355b6 | 2009-10-07 01:45:02 +0000 | [diff] [blame] | 25 | #include "llvm/ADT/StringExtras.h" |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 26 | #include "llvm/Support/raw_ostream.h" |
John McCall | efe6aee | 2009-09-05 07:56:18 +0000 | [diff] [blame] | 27 | #include "llvm/Support/ErrorHandling.h" |
Anders Carlsson | 461e326 | 2010-04-08 16:30:25 +0000 | [diff] [blame] | 28 | #include "CGVTables.h" |
Anders Carlsson | f98574b | 2010-02-05 07:31:37 +0000 | [diff] [blame] | 29 | |
| 30 | #define MANGLE_CHECKER 0 |
| 31 | |
| 32 | #if MANGLE_CHECKER |
| 33 | #include <cxxabi.h> |
| 34 | #endif |
| 35 | |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 36 | using namespace clang; |
Anders Carlsson | b73a5be | 2009-11-26 02:49:32 +0000 | [diff] [blame] | 37 | using namespace CodeGen; |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 38 | |
Charles Davis | 685b1d9 | 2010-05-26 18:25:27 +0000 | [diff] [blame] | 39 | MiscNameMangler::MiscNameMangler(MangleContext &C, |
| 40 | llvm::SmallVectorImpl<char> &Res) |
| 41 | : Context(C), Out(Res) { } |
| 42 | |
Fariborz Jahanian | 564360b | 2010-06-24 00:08:06 +0000 | [diff] [blame] | 43 | void MiscNameMangler::mangleBlock(GlobalDecl GD, const BlockDecl *BD) { |
Charles Davis | 685b1d9 | 2010-05-26 18:25:27 +0000 | [diff] [blame] | 44 | // Mangle the context of the block. |
| 45 | // FIXME: We currently mimic GCC's mangling scheme, which leaves much to be |
| 46 | // desired. Come up with a better mangling scheme. |
| 47 | const DeclContext *DC = BD->getDeclContext(); |
| 48 | while (isa<BlockDecl>(DC) || isa<EnumDecl>(DC)) |
| 49 | DC = DC->getParent(); |
| 50 | if (DC->isFunctionOrMethod()) { |
| 51 | Out << "__"; |
| 52 | if (const ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(DC)) |
| 53 | mangleObjCMethodName(Method); |
| 54 | else { |
| 55 | const NamedDecl *ND = cast<NamedDecl>(DC); |
| 56 | if (IdentifierInfo *II = ND->getIdentifier()) |
| 57 | Out << II->getName(); |
Fariborz Jahanian | 564360b | 2010-06-24 00:08:06 +0000 | [diff] [blame] | 58 | else if (const CXXDestructorDecl *D = dyn_cast<CXXDestructorDecl>(ND)) { |
| 59 | llvm::SmallString<64> Buffer; |
| 60 | Context.mangleCXXDtor(D, GD.getDtorType(), Buffer); |
| 61 | Out << Buffer; |
| 62 | } |
| 63 | else if (const CXXConstructorDecl *D = dyn_cast<CXXConstructorDecl>(ND)) { |
| 64 | llvm::SmallString<64> Buffer; |
| 65 | Context.mangleCXXCtor(D, GD.getCtorType(), Buffer); |
| 66 | Out << Buffer; |
| 67 | } |
Charles Davis | 685b1d9 | 2010-05-26 18:25:27 +0000 | [diff] [blame] | 68 | else { |
| 69 | // FIXME: We were doing a mangleUnqualifiedName() before, but that's |
| 70 | // a private member of a class that will soon itself be private to the |
| 71 | // Itanium C++ ABI object. What should we do now? Right now, I'm just |
| 72 | // calling the mangleName() method on the MangleContext; is there a |
| 73 | // better way? |
| 74 | llvm::SmallString<64> Buffer; |
| 75 | Context.mangleName(ND, Buffer); |
| 76 | Out << Buffer; |
| 77 | } |
| 78 | } |
| 79 | Out << "_block_invoke_" << Context.getBlockId(BD, true); |
| 80 | } else { |
| 81 | Out << "__block_global_" << Context.getBlockId(BD, false); |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | void MiscNameMangler::mangleObjCMethodName(const ObjCMethodDecl *MD) { |
| 86 | llvm::SmallString<64> Name; |
| 87 | llvm::raw_svector_ostream OS(Name); |
| 88 | |
| 89 | const ObjCContainerDecl *CD = |
| 90 | dyn_cast<ObjCContainerDecl>(MD->getDeclContext()); |
| 91 | assert (CD && "Missing container decl in GetNameForMethod"); |
| 92 | OS << (MD->isInstanceMethod() ? '-' : '+') << '[' << CD->getName(); |
| 93 | if (const ObjCCategoryImplDecl *CID = dyn_cast<ObjCCategoryImplDecl>(CD)) |
| 94 | OS << '(' << CID << ')'; |
| 95 | OS << ' ' << MD->getSelector().getAsString() << ']'; |
| 96 | |
| 97 | Out << OS.str().size() << OS.str(); |
| 98 | } |
| 99 | |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 100 | namespace { |
Fariborz Jahanian | 5705853 | 2010-03-03 19:41:08 +0000 | [diff] [blame] | 101 | |
| 102 | static const DeclContext *GetLocalClassFunctionDeclContext( |
| 103 | const DeclContext *DC) { |
| 104 | if (isa<CXXRecordDecl>(DC)) { |
| 105 | while (!DC->isNamespace() && !DC->isTranslationUnit() && |
| 106 | !isa<FunctionDecl>(DC)) |
| 107 | DC = DC->getParent(); |
| 108 | if (isa<FunctionDecl>(DC)) |
| 109 | return DC; |
| 110 | } |
| 111 | return 0; |
| 112 | } |
| 113 | |
Anders Carlsson | 7e12003 | 2009-11-24 05:36:32 +0000 | [diff] [blame] | 114 | static const CXXMethodDecl *getStructor(const CXXMethodDecl *MD) { |
| 115 | assert((isa<CXXConstructorDecl>(MD) || isa<CXXDestructorDecl>(MD)) && |
| 116 | "Passed in decl is not a ctor or dtor!"); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 117 | |
Anders Carlsson | 7e12003 | 2009-11-24 05:36:32 +0000 | [diff] [blame] | 118 | if (const TemplateDecl *TD = MD->getPrimaryTemplate()) { |
| 119 | MD = cast<CXXMethodDecl>(TD->getTemplatedDecl()); |
| 120 | |
| 121 | assert((isa<CXXConstructorDecl>(MD) || isa<CXXDestructorDecl>(MD)) && |
| 122 | "Templated decl is not a ctor or dtor!"); |
| 123 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 124 | |
Anders Carlsson | 7e12003 | 2009-11-24 05:36:32 +0000 | [diff] [blame] | 125 | return MD; |
| 126 | } |
John McCall | 1dd7383 | 2010-02-04 01:42:13 +0000 | [diff] [blame] | 127 | |
| 128 | static const unsigned UnknownArity = ~0U; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 129 | |
Daniel Dunbar | 1b07711 | 2009-11-21 09:06:10 +0000 | [diff] [blame] | 130 | /// CXXNameMangler - Manage the mangling of a single name. |
Daniel Dunbar | c074771 | 2009-11-21 09:12:13 +0000 | [diff] [blame] | 131 | class CXXNameMangler { |
Daniel Dunbar | 1b07711 | 2009-11-21 09:06:10 +0000 | [diff] [blame] | 132 | MangleContext &Context; |
Daniel Dunbar | 94fd26d | 2009-11-21 09:06:22 +0000 | [diff] [blame] | 133 | llvm::raw_svector_ostream Out; |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 134 | |
Daniel Dunbar | 1b07711 | 2009-11-21 09:06:10 +0000 | [diff] [blame] | 135 | const CXXMethodDecl *Structor; |
| 136 | unsigned StructorType; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 137 | |
Anders Carlsson | 9d85b72 | 2010-06-02 04:29:50 +0000 | [diff] [blame] | 138 | /// SeqID - The next subsitution sequence number. |
| 139 | unsigned SeqID; |
| 140 | |
Daniel Dunbar | 1b07711 | 2009-11-21 09:06:10 +0000 | [diff] [blame] | 141 | llvm::DenseMap<uintptr_t, unsigned> Substitutions; |
Daniel Dunbar | 3c9e463 | 2009-11-21 09:05:47 +0000 | [diff] [blame] | 142 | |
John McCall | 1dd7383 | 2010-02-04 01:42:13 +0000 | [diff] [blame] | 143 | ASTContext &getASTContext() const { return Context.getASTContext(); } |
| 144 | |
Daniel Dunbar | c074771 | 2009-11-21 09:12:13 +0000 | [diff] [blame] | 145 | public: |
Daniel Dunbar | 94fd26d | 2009-11-21 09:06:22 +0000 | [diff] [blame] | 146 | CXXNameMangler(MangleContext &C, llvm::SmallVectorImpl<char> &Res) |
Anders Carlsson | 9d85b72 | 2010-06-02 04:29:50 +0000 | [diff] [blame] | 147 | : Context(C), Out(Res), Structor(0), StructorType(0), SeqID(0) { } |
Daniel Dunbar | 77939c9 | 2009-11-21 09:06:31 +0000 | [diff] [blame] | 148 | CXXNameMangler(MangleContext &C, llvm::SmallVectorImpl<char> &Res, |
| 149 | const CXXConstructorDecl *D, CXXCtorType Type) |
Anders Carlsson | 9d85b72 | 2010-06-02 04:29:50 +0000 | [diff] [blame] | 150 | : Context(C), Out(Res), Structor(getStructor(D)), StructorType(Type), |
| 151 | SeqID(0) { } |
Daniel Dunbar | 77939c9 | 2009-11-21 09:06:31 +0000 | [diff] [blame] | 152 | CXXNameMangler(MangleContext &C, llvm::SmallVectorImpl<char> &Res, |
| 153 | const CXXDestructorDecl *D, CXXDtorType Type) |
Anders Carlsson | 9d85b72 | 2010-06-02 04:29:50 +0000 | [diff] [blame] | 154 | : Context(C), Out(Res), Structor(getStructor(D)), StructorType(Type), |
| 155 | SeqID(0) { } |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 156 | |
Anders Carlsson | f98574b | 2010-02-05 07:31:37 +0000 | [diff] [blame] | 157 | #if MANGLE_CHECKER |
| 158 | ~CXXNameMangler() { |
| 159 | if (Out.str()[0] == '\01') |
| 160 | return; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 161 | |
Anders Carlsson | f98574b | 2010-02-05 07:31:37 +0000 | [diff] [blame] | 162 | int status = 0; |
| 163 | char *result = abi::__cxa_demangle(Out.str().str().c_str(), 0, 0, &status); |
| 164 | assert(status == 0 && "Could not demangle mangled name!"); |
| 165 | free(result); |
| 166 | } |
| 167 | #endif |
Daniel Dunbar | c074771 | 2009-11-21 09:12:13 +0000 | [diff] [blame] | 168 | llvm::raw_svector_ostream &getStream() { return Out; } |
| 169 | |
Daniel Dunbar | 7e0c195 | 2009-11-21 09:17:15 +0000 | [diff] [blame] | 170 | void mangle(const NamedDecl *D, llvm::StringRef Prefix = "_Z"); |
Anders Carlsson | 19879c9 | 2010-03-23 17:17:29 +0000 | [diff] [blame] | 171 | void mangleCallOffset(int64_t NonVirtual, int64_t Virtual); |
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); |
Anders Carlsson | e170ba7 | 2009-12-14 01:45:37 +0000 | [diff] [blame] | 238 | |
| 239 | void mangleIntegerLiteral(QualType T, const llvm::APSInt &Value); |
John McCall | 2f27bf8 | 2010-02-04 02:56:29 +0000 | [diff] [blame] | 240 | void mangleMemberExpr(const Expr *Base, bool IsArrow, |
| 241 | NestedNameSpecifier *Qualifier, |
| 242 | DeclarationName Name, |
| 243 | unsigned KnownArity); |
John McCall | 5e1e89b | 2010-08-18 19:18:59 +0000 | [diff] [blame] | 244 | void mangleExpression(const Expr *E, unsigned Arity = UnknownArity); |
Daniel Dunbar | 1b07711 | 2009-11-21 09:06:10 +0000 | [diff] [blame] | 245 | void mangleCXXCtorType(CXXCtorType T); |
| 246 | void mangleCXXDtorType(CXXDtorType T); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 247 | |
John McCall | 6dbce19 | 2010-08-20 00:17:19 +0000 | [diff] [blame] | 248 | void mangleTemplateArgs(const ExplicitTemplateArgumentList &TemplateArgs); |
Douglas Gregor | 20f0cc7 | 2010-04-23 03:10:43 +0000 | [diff] [blame] | 249 | void mangleTemplateArgs(TemplateName Template, |
| 250 | const TemplateArgument *TemplateArgs, |
Sean Hunt | c302113 | 2010-05-05 15:23:54 +0000 | [diff] [blame] | 251 | unsigned NumTemplateArgs); |
Rafael Espindola | d980072 | 2010-03-11 14:07:00 +0000 | [diff] [blame] | 252 | void mangleTemplateArgs(const TemplateParameterList &PL, |
| 253 | const TemplateArgument *TemplateArgs, |
Daniel Dunbar | 1b07711 | 2009-11-21 09:06:10 +0000 | [diff] [blame] | 254 | unsigned NumTemplateArgs); |
Rafael Espindola | d980072 | 2010-03-11 14:07:00 +0000 | [diff] [blame] | 255 | void mangleTemplateArgs(const TemplateParameterList &PL, |
| 256 | const TemplateArgumentList &AL); |
| 257 | void mangleTemplateArg(const NamedDecl *P, const TemplateArgument &A); |
Daniel Dunbar | 3c9e463 | 2009-11-21 09:05:47 +0000 | [diff] [blame] | 258 | |
Daniel Dunbar | 1b07711 | 2009-11-21 09:06:10 +0000 | [diff] [blame] | 259 | void mangleTemplateParameter(unsigned Index); |
| 260 | }; |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 261 | } |
| 262 | |
Anders Carlsson | 43f1740 | 2009-04-02 15:51:53 +0000 | [diff] [blame] | 263 | static bool isInCLinkageSpecification(const Decl *D) { |
Douglas Gregor | 457e281 | 2009-10-28 16:31:34 +0000 | [diff] [blame] | 264 | D = D->getCanonicalDecl(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 265 | for (const DeclContext *DC = D->getDeclContext(); |
Anders Carlsson | 43f1740 | 2009-04-02 15:51:53 +0000 | [diff] [blame] | 266 | !DC->isTranslationUnit(); DC = DC->getParent()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 267 | if (const LinkageSpecDecl *Linkage = dyn_cast<LinkageSpecDecl>(DC)) |
Anders Carlsson | 43f1740 | 2009-04-02 15:51:53 +0000 | [diff] [blame] | 268 | return Linkage->getLanguage() == LinkageSpecDecl::lang_c; |
| 269 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 270 | |
Anders Carlsson | 43f1740 | 2009-04-02 15:51:53 +0000 | [diff] [blame] | 271 | return false; |
| 272 | } |
| 273 | |
Daniel Dunbar | f981bf8 | 2009-11-21 09:14:52 +0000 | [diff] [blame] | 274 | bool MangleContext::shouldMangleDeclName(const NamedDecl *D) { |
| 275 | // In C, functions with no attributes never need to be mangled. Fastpath them. |
| 276 | if (!getASTContext().getLangOptions().CPlusPlus && !D->hasAttrs()) |
| 277 | return false; |
| 278 | |
| 279 | // Any decl can be declared with __asm("foo") on it, and this takes precedence |
| 280 | // over all other naming in the .o file. |
| 281 | if (D->hasAttr<AsmLabelAttr>()) |
| 282 | return true; |
| 283 | |
Mike Stump | 141c5af | 2009-09-02 00:25:38 +0000 | [diff] [blame] | 284 | // Clang's "overloadable" attribute extension to C/C++ implies name mangling |
Anders Carlsson | a1e1622 | 2009-11-07 07:15:03 +0000 | [diff] [blame] | 285 | // (always) as does passing a C++ member function and a function |
| 286 | // whose name is not a simple identifier. |
Daniel Dunbar | f981bf8 | 2009-11-21 09:14:52 +0000 | [diff] [blame] | 287 | const FunctionDecl *FD = dyn_cast<FunctionDecl>(D); |
| 288 | if (FD && (FD->hasAttr<OverloadableAttr>() || isa<CXXMethodDecl>(FD) || |
| 289 | !FD->getDeclName().isIdentifier())) |
| 290 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 291 | |
Daniel Dunbar | f981bf8 | 2009-11-21 09:14:52 +0000 | [diff] [blame] | 292 | // Otherwise, no mangling is done outside C++ mode. |
| 293 | if (!getASTContext().getLangOptions().CPlusPlus) |
| 294 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 295 | |
Sean Hunt | 3145525 | 2010-01-24 03:04:27 +0000 | [diff] [blame] | 296 | // Variables at global scope with non-internal linkage are not mangled |
Eli Friedman | 7facf84 | 2009-12-02 20:32:49 +0000 | [diff] [blame] | 297 | if (!FD) { |
| 298 | const DeclContext *DC = D->getDeclContext(); |
| 299 | // Check for extern variable declared locally. |
Fariborz Jahanian | e81c561 | 2010-06-30 18:57:21 +0000 | [diff] [blame] | 300 | if (DC->isFunctionOrMethod() && D->hasLinkage()) |
Eli Friedman | 7facf84 | 2009-12-02 20:32:49 +0000 | [diff] [blame] | 301 | while (!DC->isNamespace() && !DC->isTranslationUnit()) |
| 302 | DC = DC->getParent(); |
Douglas Gregor | 0b6bc8b | 2010-02-03 09:33:45 +0000 | [diff] [blame] | 303 | if (DC->isTranslationUnit() && D->getLinkage() != InternalLinkage) |
Eli Friedman | 7facf84 | 2009-12-02 20:32:49 +0000 | [diff] [blame] | 304 | return false; |
| 305 | } |
| 306 | |
Eli Friedman | c00cb64 | 2010-07-18 20:49:59 +0000 | [diff] [blame] | 307 | // Class members are always mangled. |
| 308 | if (D->getDeclContext()->isRecord()) |
| 309 | return true; |
| 310 | |
Eli Friedman | 7facf84 | 2009-12-02 20:32:49 +0000 | [diff] [blame] | 311 | // C functions and "main" are not mangled. |
| 312 | if ((FD && FD->isMain()) || isInCLinkageSpecification(D)) |
Daniel Dunbar | f981bf8 | 2009-11-21 09:14:52 +0000 | [diff] [blame] | 313 | return false; |
| 314 | |
Anders Carlsson | 43f1740 | 2009-04-02 15:51:53 +0000 | [diff] [blame] | 315 | return true; |
| 316 | } |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 317 | |
Daniel Dunbar | 7e0c195 | 2009-11-21 09:17:15 +0000 | [diff] [blame] | 318 | void CXXNameMangler::mangle(const NamedDecl *D, llvm::StringRef Prefix) { |
Mike Stump | 141c5af | 2009-09-02 00:25:38 +0000 | [diff] [blame] | 319 | // Any decl can be declared with __asm("foo") on it, and this takes precedence |
| 320 | // over all other naming in the .o file. |
Argyrios Kyrtzidis | 40b598e | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 321 | if (const AsmLabelAttr *ALA = D->getAttr<AsmLabelAttr>()) { |
Chris Lattner | ca3f25c | 2009-03-21 08:24:40 +0000 | [diff] [blame] | 322 | // If we have an asm name, then we use it as the mangling. |
| 323 | Out << '\01'; // LLVM IR Marker for __asm("foo") |
| 324 | Out << ALA->getLabel(); |
Daniel Dunbar | f981bf8 | 2009-11-21 09:14:52 +0000 | [diff] [blame] | 325 | return; |
Chris Lattner | ca3f25c | 2009-03-21 08:24:40 +0000 | [diff] [blame] | 326 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 327 | |
Sean Hunt | 3145525 | 2010-01-24 03:04:27 +0000 | [diff] [blame] | 328 | // <mangled-name> ::= _Z <encoding> |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 329 | // ::= <data name> |
| 330 | // ::= <special-name> |
Daniel Dunbar | 7e0c195 | 2009-11-21 09:17:15 +0000 | [diff] [blame] | 331 | Out << Prefix; |
| 332 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) |
Daniel Dunbar | f981bf8 | 2009-11-21 09:14:52 +0000 | [diff] [blame] | 333 | mangleFunctionEncoding(FD); |
Rafael Espindola | d980072 | 2010-03-11 14:07:00 +0000 | [diff] [blame] | 334 | else if (const VarDecl *VD = dyn_cast<VarDecl>(D)) |
| 335 | mangleName(VD); |
Daniel Dunbar | 7e0c195 | 2009-11-21 09:17:15 +0000 | [diff] [blame] | 336 | else |
Rafael Espindola | d980072 | 2010-03-11 14:07:00 +0000 | [diff] [blame] | 337 | mangleName(cast<FieldDecl>(D)); |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 338 | } |
| 339 | |
| 340 | void CXXNameMangler::mangleFunctionEncoding(const FunctionDecl *FD) { |
| 341 | // <encoding> ::= <function name> <bare-function-type> |
| 342 | mangleName(FD); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 343 | |
Daniel Dunbar | 7e0c195 | 2009-11-21 09:17:15 +0000 | [diff] [blame] | 344 | // Don't mangle in the type if this isn't a decl we should typically mangle. |
| 345 | if (!Context.shouldMangleDeclName(FD)) |
| 346 | return; |
| 347 | |
Mike Stump | 141c5af | 2009-09-02 00:25:38 +0000 | [diff] [blame] | 348 | // Whether the mangling of a function type includes the return type depends on |
| 349 | // the context and the nature of the function. The rules for deciding whether |
| 350 | // the return type is included are: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 351 | // |
Douglas Gregor | 1fd2dd1 | 2009-06-29 22:39:32 +0000 | [diff] [blame] | 352 | // 1. Template functions (names or types) have return types encoded, with |
| 353 | // the exceptions listed below. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 354 | // 2. Function types not appearing as part of a function name mangling, |
Douglas Gregor | 1fd2dd1 | 2009-06-29 22:39:32 +0000 | [diff] [blame] | 355 | // e.g. parameters, pointer types, etc., have return type encoded, with the |
| 356 | // exceptions listed below. |
| 357 | // 3. Non-template function names do not have return types encoded. |
| 358 | // |
Mike Stump | 141c5af | 2009-09-02 00:25:38 +0000 | [diff] [blame] | 359 | // The exceptions mentioned in (1) and (2) above, for which the return type is |
| 360 | // never included, are |
Douglas Gregor | 1fd2dd1 | 2009-06-29 22:39:32 +0000 | [diff] [blame] | 361 | // 1. Constructors. |
| 362 | // 2. Destructors. |
| 363 | // 3. Conversion operator functions, e.g. operator int. |
| 364 | bool MangleReturnType = false; |
Anders Carlsson | 9234b7f | 2009-09-17 03:46:43 +0000 | [diff] [blame] | 365 | if (FunctionTemplateDecl *PrimaryTemplate = FD->getPrimaryTemplate()) { |
| 366 | if (!(isa<CXXConstructorDecl>(FD) || isa<CXXDestructorDecl>(FD) || |
| 367 | isa<CXXConversionDecl>(FD))) |
| 368 | MangleReturnType = true; |
Daniel Dunbar | 3c9e463 | 2009-11-21 09:05:47 +0000 | [diff] [blame] | 369 | |
Anders Carlsson | 9234b7f | 2009-09-17 03:46:43 +0000 | [diff] [blame] | 370 | // Mangle the type of the primary template. |
| 371 | FD = PrimaryTemplate->getTemplatedDecl(); |
| 372 | } |
| 373 | |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 374 | // Do the canonicalization out here because parameter types can |
| 375 | // undergo additional canonicalization (e.g. array decay). |
| 376 | FunctionType *FT = cast<FunctionType>(Context.getASTContext() |
| 377 | .getCanonicalType(FD->getType())); |
| 378 | |
| 379 | mangleBareFunctionType(FT, MangleReturnType); |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 380 | } |
| 381 | |
Anders Carlsson | 47846d2 | 2009-12-04 06:23:23 +0000 | [diff] [blame] | 382 | static const DeclContext *IgnoreLinkageSpecDecls(const DeclContext *DC) { |
| 383 | while (isa<LinkageSpecDecl>(DC)) { |
Anders Carlsson | 47846d2 | 2009-12-04 06:23:23 +0000 | [diff] [blame] | 384 | DC = DC->getParent(); |
| 385 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 386 | |
Anders Carlsson | 47846d2 | 2009-12-04 06:23:23 +0000 | [diff] [blame] | 387 | return DC; |
| 388 | } |
| 389 | |
Anders Carlsson | c820f90 | 2010-06-02 15:58:27 +0000 | [diff] [blame] | 390 | /// isStd - Return whether a given namespace is the 'std' namespace. |
| 391 | static bool isStd(const NamespaceDecl *NS) { |
| 392 | if (!IgnoreLinkageSpecDecls(NS->getParent())->isTranslationUnit()) |
| 393 | return false; |
| 394 | |
| 395 | const IdentifierInfo *II = NS->getOriginalNamespace()->getIdentifier(); |
| 396 | return II && II->isStr("std"); |
| 397 | } |
| 398 | |
Anders Carlsson | 47846d2 | 2009-12-04 06:23:23 +0000 | [diff] [blame] | 399 | // isStdNamespace - Return whether a given decl context is a toplevel 'std' |
| 400 | // namespace. |
Daniel Dunbar | 1308af9 | 2009-11-21 09:11:45 +0000 | [diff] [blame] | 401 | static bool isStdNamespace(const DeclContext *DC) { |
Anders Carlsson | 47846d2 | 2009-12-04 06:23:23 +0000 | [diff] [blame] | 402 | if (!DC->isNamespace()) |
| 403 | return false; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 404 | |
Anders Carlsson | 47846d2 | 2009-12-04 06:23:23 +0000 | [diff] [blame] | 405 | return isStd(cast<NamespaceDecl>(DC)); |
Daniel Dunbar | 1308af9 | 2009-11-21 09:11:45 +0000 | [diff] [blame] | 406 | } |
| 407 | |
Anders Carlsson | bb36ba4 | 2009-09-26 03:24:57 +0000 | [diff] [blame] | 408 | static const TemplateDecl * |
| 409 | isTemplate(const NamedDecl *ND, const TemplateArgumentList *&TemplateArgs) { |
Anders Carlsson | 2744a06 | 2009-09-18 19:00:18 +0000 | [diff] [blame] | 410 | // Check if we have a function template. |
| 411 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)){ |
Anders Carlsson | bb36ba4 | 2009-09-26 03:24:57 +0000 | [diff] [blame] | 412 | if (const TemplateDecl *TD = FD->getPrimaryTemplate()) { |
Anders Carlsson | 2744a06 | 2009-09-18 19:00:18 +0000 | [diff] [blame] | 413 | TemplateArgs = FD->getTemplateSpecializationArgs(); |
Anders Carlsson | bb36ba4 | 2009-09-26 03:24:57 +0000 | [diff] [blame] | 414 | return TD; |
Anders Carlsson | 2744a06 | 2009-09-18 19:00:18 +0000 | [diff] [blame] | 415 | } |
| 416 | } |
| 417 | |
Anders Carlsson | eafc6dc | 2009-09-18 19:44:50 +0000 | [diff] [blame] | 418 | // Check if we have a class template. |
| 419 | if (const ClassTemplateSpecializationDecl *Spec = |
| 420 | dyn_cast<ClassTemplateSpecializationDecl>(ND)) { |
| 421 | TemplateArgs = &Spec->getTemplateArgs(); |
Anders Carlsson | bb36ba4 | 2009-09-26 03:24:57 +0000 | [diff] [blame] | 422 | return Spec->getSpecializedTemplate(); |
Anders Carlsson | eafc6dc | 2009-09-18 19:44:50 +0000 | [diff] [blame] | 423 | } |
Daniel Dunbar | 3c9e463 | 2009-11-21 09:05:47 +0000 | [diff] [blame] | 424 | |
Anders Carlsson | 2744a06 | 2009-09-18 19:00:18 +0000 | [diff] [blame] | 425 | return 0; |
| 426 | } |
| 427 | |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 428 | void CXXNameMangler::mangleName(const NamedDecl *ND) { |
| 429 | // <name> ::= <nested-name> |
| 430 | // ::= <unscoped-name> |
| 431 | // ::= <unscoped-template-name> <template-args> |
Anders Carlsson | 201ce74 | 2009-09-17 03:17:01 +0000 | [diff] [blame] | 432 | // ::= <local-name> |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 433 | // |
Anders Carlsson | d58d6f7 | 2009-09-17 16:12:20 +0000 | [diff] [blame] | 434 | const DeclContext *DC = ND->getDeclContext(); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 435 | |
Fariborz Jahanian | 5705853 | 2010-03-03 19:41:08 +0000 | [diff] [blame] | 436 | if (GetLocalClassFunctionDeclContext(DC)) { |
| 437 | mangleLocalName(ND); |
| 438 | return; |
| 439 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 440 | |
Eli Friedman | 7facf84 | 2009-12-02 20:32:49 +0000 | [diff] [blame] | 441 | // If this is an extern variable declared locally, the relevant DeclContext |
| 442 | // is that of the containing namespace, or the translation unit. |
| 443 | if (isa<FunctionDecl>(DC) && ND->hasLinkage()) |
| 444 | while (!DC->isNamespace() && !DC->isTranslationUnit()) |
| 445 | DC = DC->getParent(); |
| 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"); |
| 528 | Diags.Report(FullSourceLoc(), DiagID); |
| 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); |
| 856 | } |
Fariborz Jahanian | 5705853 | 2010-03-03 19:41:08 +0000 | [diff] [blame] | 857 | else if (const DeclContext *CDC = GetLocalClassFunctionDeclContext(DC)) { |
| 858 | mangleFunctionEncoding(cast<FunctionDecl>(CDC)); |
| 859 | Out << 'E'; |
| 860 | mangleNestedName(ND, DC, true /*NoFunction*/); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 861 | |
Fariborz Jahanian | 4819ac4 | 2010-03-04 01:02:03 +0000 | [diff] [blame] | 862 | // FIXME. This still does not cover all cases. |
| 863 | unsigned disc; |
| 864 | if (Context.getNextDiscriminator(ND, disc)) { |
| 865 | if (disc < 10) |
| 866 | Out << '_' << disc; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 867 | else |
Fariborz Jahanian | 4819ac4 | 2010-03-04 01:02:03 +0000 | [diff] [blame] | 868 | Out << "__" << disc << '_'; |
| 869 | } |
Fariborz Jahanian | 5705853 | 2010-03-03 19:41:08 +0000 | [diff] [blame] | 870 | |
| 871 | return; |
| 872 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 873 | else |
Fariborz Jahanian | 5705853 | 2010-03-03 19:41:08 +0000 | [diff] [blame] | 874 | mangleFunctionEncoding(cast<FunctionDecl>(DC)); |
Anders Carlsson | 7b06f6c | 2009-12-10 03:14:39 +0000 | [diff] [blame] | 875 | |
Anders Carlsson | 1b42c79 | 2009-04-02 16:24:45 +0000 | [diff] [blame] | 876 | Out << 'E'; |
Eli Friedman | 6f9f25d | 2009-12-11 20:21:38 +0000 | [diff] [blame] | 877 | mangleUnqualifiedName(ND); |
Anders Carlsson | 1b42c79 | 2009-04-02 16:24:45 +0000 | [diff] [blame] | 878 | } |
| 879 | |
Fariborz Jahanian | 5705853 | 2010-03-03 19:41:08 +0000 | [diff] [blame] | 880 | void CXXNameMangler::manglePrefix(const DeclContext *DC, bool NoFunction) { |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 881 | // <prefix> ::= <prefix> <unqualified-name> |
| 882 | // ::= <template-prefix> <template-args> |
| 883 | // ::= <template-param> |
| 884 | // ::= # empty |
| 885 | // ::= <substitution> |
Anders Carlsson | 6862fc7 | 2009-09-17 04:16:28 +0000 | [diff] [blame] | 886 | |
Anders Carlsson | add2882 | 2009-09-22 20:33:31 +0000 | [diff] [blame] | 887 | while (isa<LinkageSpecDecl>(DC)) |
| 888 | DC = DC->getParent(); |
Daniel Dunbar | 3c9e463 | 2009-11-21 09:05:47 +0000 | [diff] [blame] | 889 | |
Anders Carlsson | 9263e91 | 2009-09-18 18:39:58 +0000 | [diff] [blame] | 890 | if (DC->isTranslationUnit()) |
| 891 | return; |
Daniel Dunbar | 3c9e463 | 2009-11-21 09:05:47 +0000 | [diff] [blame] | 892 | |
Douglas Gregor | 35415f5 | 2010-05-25 17:04:15 +0000 | [diff] [blame] | 893 | if (const BlockDecl *Block = dyn_cast<BlockDecl>(DC)) { |
| 894 | manglePrefix(DC->getParent(), NoFunction); |
| 895 | llvm::SmallString<64> Name; |
Fariborz Jahanian | 564360b | 2010-06-24 00:08:06 +0000 | [diff] [blame] | 896 | Context.mangleBlock(GlobalDecl(), Block, Name); |
Douglas Gregor | 35415f5 | 2010-05-25 17:04:15 +0000 | [diff] [blame] | 897 | Out << Name.size() << Name; |
| 898 | return; |
| 899 | } |
| 900 | |
Anders Carlsson | 6862fc7 | 2009-09-17 04:16:28 +0000 | [diff] [blame] | 901 | if (mangleSubstitution(cast<NamedDecl>(DC))) |
| 902 | return; |
Anders Carlsson | 7482e24 | 2009-09-18 04:29:09 +0000 | [diff] [blame] | 903 | |
Anders Carlsson | 2ee3fca | 2009-09-18 20:11:09 +0000 | [diff] [blame] | 904 | // Check if we have a template. |
| 905 | const TemplateArgumentList *TemplateArgs = 0; |
Daniel Dunbar | 3c9e463 | 2009-11-21 09:05:47 +0000 | [diff] [blame] | 906 | if (const TemplateDecl *TD = isTemplate(cast<NamedDecl>(DC), TemplateArgs)) { |
Anders Carlsson | 2ee3fca | 2009-09-18 20:11:09 +0000 | [diff] [blame] | 907 | mangleTemplatePrefix(TD); |
Rafael Espindola | d980072 | 2010-03-11 14:07:00 +0000 | [diff] [blame] | 908 | TemplateParameterList *TemplateParameters = TD->getTemplateParameters(); |
| 909 | mangleTemplateArgs(*TemplateParameters, *TemplateArgs); |
Fariborz Jahanian | 5705853 | 2010-03-03 19:41:08 +0000 | [diff] [blame] | 910 | } |
Douglas Gregor | 35415f5 | 2010-05-25 17:04:15 +0000 | [diff] [blame] | 911 | else if(NoFunction && (isa<FunctionDecl>(DC) || isa<ObjCMethodDecl>(DC))) |
Fariborz Jahanian | 5705853 | 2010-03-03 19:41:08 +0000 | [diff] [blame] | 912 | return; |
Douglas Gregor | 35415f5 | 2010-05-25 17:04:15 +0000 | [diff] [blame] | 913 | else if (const ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(DC)) |
| 914 | mangleObjCMethodName(Method); |
Fariborz Jahanian | 5705853 | 2010-03-03 19:41:08 +0000 | [diff] [blame] | 915 | else { |
| 916 | manglePrefix(DC->getParent(), NoFunction); |
Anders Carlsson | 2ee3fca | 2009-09-18 20:11:09 +0000 | [diff] [blame] | 917 | mangleUnqualifiedName(cast<NamedDecl>(DC)); |
| 918 | } |
Daniel Dunbar | 3c9e463 | 2009-11-21 09:05:47 +0000 | [diff] [blame] | 919 | |
Anders Carlsson | 6862fc7 | 2009-09-17 04:16:28 +0000 | [diff] [blame] | 920 | addSubstitution(cast<NamedDecl>(DC)); |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 921 | } |
| 922 | |
Douglas Gregor | 20f0cc7 | 2010-04-23 03:10:43 +0000 | [diff] [blame] | 923 | void CXXNameMangler::mangleTemplatePrefix(TemplateName Template) { |
| 924 | // <template-prefix> ::= <prefix> <template unqualified-name> |
| 925 | // ::= <template-param> |
| 926 | // ::= <substitution> |
| 927 | if (TemplateDecl *TD = Template.getAsTemplateDecl()) |
| 928 | return mangleTemplatePrefix(TD); |
| 929 | |
| 930 | if (QualifiedTemplateName *Qualified = Template.getAsQualifiedTemplateName()) |
| 931 | mangleUnresolvedScope(Qualified->getQualifier()); |
Sean Hunt | c302113 | 2010-05-05 15:23:54 +0000 | [diff] [blame] | 932 | |
Douglas Gregor | 20f0cc7 | 2010-04-23 03:10:43 +0000 | [diff] [blame] | 933 | if (OverloadedTemplateStorage *Overloaded |
| 934 | = Template.getAsOverloadedTemplate()) { |
Sean Hunt | c302113 | 2010-05-05 15:23:54 +0000 | [diff] [blame] | 935 | mangleUnqualifiedName(0, (*Overloaded->begin())->getDeclName(), |
Douglas Gregor | 20f0cc7 | 2010-04-23 03:10:43 +0000 | [diff] [blame] | 936 | UnknownArity); |
| 937 | return; |
| 938 | } |
Sean Hunt | c302113 | 2010-05-05 15:23:54 +0000 | [diff] [blame] | 939 | |
Douglas Gregor | 20f0cc7 | 2010-04-23 03:10:43 +0000 | [diff] [blame] | 940 | DependentTemplateName *Dependent = Template.getAsDependentTemplateName(); |
| 941 | assert(Dependent && "Unknown template name kind?"); |
| 942 | mangleUnresolvedScope(Dependent->getQualifier()); |
Douglas Gregor | 1e9268e | 2010-04-28 05:58:56 +0000 | [diff] [blame] | 943 | mangleUnscopedTemplateName(Template); |
Douglas Gregor | 20f0cc7 | 2010-04-23 03:10:43 +0000 | [diff] [blame] | 944 | } |
| 945 | |
Anders Carlsson | 0fa6df4 | 2009-09-26 19:45:45 +0000 | [diff] [blame] | 946 | void CXXNameMangler::mangleTemplatePrefix(const TemplateDecl *ND) { |
Anders Carlsson | 7482e24 | 2009-09-18 04:29:09 +0000 | [diff] [blame] | 947 | // <template-prefix> ::= <prefix> <template unqualified-name> |
| 948 | // ::= <template-param> |
| 949 | // ::= <substitution> |
Douglas Gregor | 32fb4e1 | 2010-02-05 20:45:00 +0000 | [diff] [blame] | 950 | // <template-template-param> ::= <template-param> |
| 951 | // <substitution> |
Anders Carlsson | 7482e24 | 2009-09-18 04:29:09 +0000 | [diff] [blame] | 952 | |
Anders Carlsson | aeb8537 | 2009-09-26 22:18:22 +0000 | [diff] [blame] | 953 | if (mangleSubstitution(ND)) |
| 954 | return; |
Daniel Dunbar | 3c9e463 | 2009-11-21 09:05:47 +0000 | [diff] [blame] | 955 | |
Douglas Gregor | 32fb4e1 | 2010-02-05 20:45:00 +0000 | [diff] [blame] | 956 | // <template-template-param> ::= <template-param> |
| 957 | if (const TemplateTemplateParmDecl *TTP |
| 958 | = dyn_cast<TemplateTemplateParmDecl>(ND)) { |
| 959 | mangleTemplateParameter(TTP->getIndex()); |
| 960 | return; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 961 | } |
Daniel Dunbar | 3c9e463 | 2009-11-21 09:05:47 +0000 | [diff] [blame] | 962 | |
Anders Carlsson | aa73ab1 | 2009-09-18 18:47:07 +0000 | [diff] [blame] | 963 | manglePrefix(ND->getDeclContext()); |
Anders Carlsson | 1668f20 | 2009-09-26 20:13:56 +0000 | [diff] [blame] | 964 | mangleUnqualifiedName(ND->getTemplatedDecl()); |
Anders Carlsson | aeb8537 | 2009-09-26 22:18:22 +0000 | [diff] [blame] | 965 | addSubstitution(ND); |
Anders Carlsson | 7482e24 | 2009-09-18 04:29:09 +0000 | [diff] [blame] | 966 | } |
| 967 | |
John McCall | b6f532e | 2010-07-14 06:43:17 +0000 | [diff] [blame] | 968 | /// Mangles a template name under the production <type>. Required for |
| 969 | /// template template arguments. |
| 970 | /// <type> ::= <class-enum-type> |
| 971 | /// ::= <template-param> |
| 972 | /// ::= <substitution> |
| 973 | void CXXNameMangler::mangleType(TemplateName TN) { |
| 974 | if (mangleSubstitution(TN)) |
| 975 | return; |
| 976 | |
| 977 | TemplateDecl *TD = 0; |
| 978 | |
| 979 | switch (TN.getKind()) { |
| 980 | case TemplateName::QualifiedTemplate: |
| 981 | TD = TN.getAsQualifiedTemplateName()->getTemplateDecl(); |
| 982 | goto HaveDecl; |
| 983 | |
| 984 | case TemplateName::Template: |
| 985 | TD = TN.getAsTemplateDecl(); |
| 986 | goto HaveDecl; |
| 987 | |
| 988 | HaveDecl: |
| 989 | if (isa<TemplateTemplateParmDecl>(TD)) |
| 990 | mangleTemplateParameter(cast<TemplateTemplateParmDecl>(TD)->getIndex()); |
| 991 | else |
| 992 | mangleName(TD); |
| 993 | break; |
| 994 | |
| 995 | case TemplateName::OverloadedTemplate: |
| 996 | llvm_unreachable("can't mangle an overloaded template name as a <type>"); |
| 997 | break; |
| 998 | |
| 999 | case TemplateName::DependentTemplate: { |
| 1000 | const DependentTemplateName *Dependent = TN.getAsDependentTemplateName(); |
| 1001 | assert(Dependent->isIdentifier()); |
| 1002 | |
| 1003 | // <class-enum-type> ::= <name> |
| 1004 | // <name> ::= <nested-name> |
| 1005 | mangleUnresolvedScope(Dependent->getQualifier()); |
| 1006 | mangleSourceName(Dependent->getIdentifier()); |
| 1007 | break; |
| 1008 | } |
| 1009 | |
| 1010 | } |
| 1011 | |
| 1012 | addSubstitution(TN); |
| 1013 | } |
| 1014 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1015 | void |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 1016 | CXXNameMangler::mangleOperatorName(OverloadedOperatorKind OO, unsigned Arity) { |
| 1017 | switch (OO) { |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 1018 | // <operator-name> ::= nw # new |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 1019 | case OO_New: Out << "nw"; break; |
| 1020 | // ::= na # new[] |
| 1021 | case OO_Array_New: Out << "na"; break; |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 1022 | // ::= dl # delete |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 1023 | case OO_Delete: Out << "dl"; break; |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 1024 | // ::= da # delete[] |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 1025 | case OO_Array_Delete: Out << "da"; break; |
| 1026 | // ::= ps # + (unary) |
John McCall | 5e1e89b | 2010-08-18 19:18:59 +0000 | [diff] [blame] | 1027 | // ::= pl # + (binary or unknown) |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 1028 | case OO_Plus: |
Anders Carlsson | 8257d41 | 2009-12-22 06:36:32 +0000 | [diff] [blame] | 1029 | Out << (Arity == 1? "ps" : "pl"); break; |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 1030 | // ::= ng # - (unary) |
John McCall | 5e1e89b | 2010-08-18 19:18:59 +0000 | [diff] [blame] | 1031 | // ::= mi # - (binary or unknown) |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 1032 | case OO_Minus: |
Anders Carlsson | 8257d41 | 2009-12-22 06:36:32 +0000 | [diff] [blame] | 1033 | Out << (Arity == 1? "ng" : "mi"); break; |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 1034 | // ::= ad # & (unary) |
John McCall | 5e1e89b | 2010-08-18 19:18:59 +0000 | [diff] [blame] | 1035 | // ::= an # & (binary or unknown) |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 1036 | case OO_Amp: |
Anders Carlsson | 8257d41 | 2009-12-22 06:36:32 +0000 | [diff] [blame] | 1037 | Out << (Arity == 1? "ad" : "an"); break; |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 1038 | // ::= de # * (unary) |
John McCall | 5e1e89b | 2010-08-18 19:18:59 +0000 | [diff] [blame] | 1039 | // ::= ml # * (binary or unknown) |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 1040 | case OO_Star: |
John McCall | 5e1e89b | 2010-08-18 19:18:59 +0000 | [diff] [blame] | 1041 | // Use binary when unknown. |
Anders Carlsson | 8257d41 | 2009-12-22 06:36:32 +0000 | [diff] [blame] | 1042 | Out << (Arity == 1? "de" : "ml"); break; |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 1043 | // ::= co # ~ |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 1044 | case OO_Tilde: Out << "co"; break; |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 1045 | // ::= dv # / |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 1046 | case OO_Slash: Out << "dv"; break; |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 1047 | // ::= rm # % |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 1048 | case OO_Percent: Out << "rm"; break; |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 1049 | // ::= or # | |
| 1050 | case OO_Pipe: Out << "or"; break; |
| 1051 | // ::= eo # ^ |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 1052 | case OO_Caret: Out << "eo"; break; |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 1053 | // ::= aS # = |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 1054 | case OO_Equal: Out << "aS"; break; |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 1055 | // ::= pL # += |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 1056 | case OO_PlusEqual: Out << "pL"; break; |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 1057 | // ::= mI # -= |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 1058 | case OO_MinusEqual: Out << "mI"; break; |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 1059 | // ::= mL # *= |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 1060 | case OO_StarEqual: Out << "mL"; break; |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 1061 | // ::= dV # /= |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 1062 | case OO_SlashEqual: Out << "dV"; break; |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 1063 | // ::= rM # %= |
| 1064 | case OO_PercentEqual: Out << "rM"; break; |
| 1065 | // ::= aN # &= |
| 1066 | case OO_AmpEqual: Out << "aN"; break; |
| 1067 | // ::= oR # |= |
| 1068 | case OO_PipeEqual: Out << "oR"; break; |
| 1069 | // ::= eO # ^= |
| 1070 | case OO_CaretEqual: Out << "eO"; break; |
| 1071 | // ::= ls # << |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 1072 | case OO_LessLess: Out << "ls"; break; |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 1073 | // ::= rs # >> |
| 1074 | case OO_GreaterGreater: Out << "rs"; break; |
| 1075 | // ::= lS # <<= |
| 1076 | case OO_LessLessEqual: Out << "lS"; break; |
| 1077 | // ::= rS # >>= |
| 1078 | case OO_GreaterGreaterEqual: Out << "rS"; break; |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 1079 | // ::= eq # == |
| 1080 | case OO_EqualEqual: Out << "eq"; break; |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 1081 | // ::= ne # != |
| 1082 | case OO_ExclaimEqual: Out << "ne"; break; |
| 1083 | // ::= lt # < |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 1084 | case OO_Less: Out << "lt"; break; |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 1085 | // ::= gt # > |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 1086 | case OO_Greater: Out << "gt"; break; |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 1087 | // ::= le # <= |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 1088 | case OO_LessEqual: Out << "le"; break; |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 1089 | // ::= ge # >= |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 1090 | case OO_GreaterEqual: Out << "ge"; break; |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 1091 | // ::= nt # ! |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 1092 | case OO_Exclaim: Out << "nt"; break; |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 1093 | // ::= aa # && |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 1094 | case OO_AmpAmp: Out << "aa"; break; |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 1095 | // ::= oo # || |
| 1096 | case OO_PipePipe: Out << "oo"; break; |
| 1097 | // ::= pp # ++ |
| 1098 | case OO_PlusPlus: Out << "pp"; break; |
| 1099 | // ::= mm # -- |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 1100 | case OO_MinusMinus: Out << "mm"; break; |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 1101 | // ::= cm # , |
| 1102 | case OO_Comma: Out << "cm"; break; |
| 1103 | // ::= pm # ->* |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 1104 | case OO_ArrowStar: Out << "pm"; break; |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 1105 | // ::= pt # -> |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 1106 | case OO_Arrow: Out << "pt"; break; |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 1107 | // ::= cl # () |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 1108 | case OO_Call: Out << "cl"; break; |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 1109 | // ::= ix # [] |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 1110 | case OO_Subscript: Out << "ix"; break; |
Anders Carlsson | e170ba7 | 2009-12-14 01:45:37 +0000 | [diff] [blame] | 1111 | |
| 1112 | // ::= qu # ? |
| 1113 | // The conditional operator can't be overloaded, but we still handle it when |
| 1114 | // mangling expressions. |
| 1115 | case OO_Conditional: Out << "qu"; break; |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 1116 | |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 1117 | case OO_None: |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 1118 | case NUM_OVERLOADED_OPERATORS: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1119 | assert(false && "Not an overloaded operator"); |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 1120 | break; |
| 1121 | } |
| 1122 | } |
| 1123 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1124 | void CXXNameMangler::mangleQualifiers(Qualifiers Quals) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1125 | // <CV-qualifiers> ::= [r] [V] [K] # restrict (C99), volatile, const |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1126 | if (Quals.hasRestrict()) |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 1127 | Out << 'r'; |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1128 | if (Quals.hasVolatile()) |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 1129 | Out << 'V'; |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1130 | if (Quals.hasConst()) |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 1131 | Out << 'K'; |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1132 | |
Douglas Gregor | 56079f7 | 2010-06-14 23:15:08 +0000 | [diff] [blame] | 1133 | if (Quals.hasAddressSpace()) { |
| 1134 | // Extension: |
| 1135 | // |
| 1136 | // <type> ::= U <address-space-number> |
| 1137 | // |
| 1138 | // where <address-space-number> is a source name consisting of 'AS' |
| 1139 | // followed by the address space <number>. |
| 1140 | llvm::SmallString<64> ASString; |
| 1141 | ASString = "AS" + llvm::utostr_32(Quals.getAddressSpace()); |
| 1142 | Out << 'U' << ASString.size() << ASString; |
| 1143 | } |
| 1144 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1145 | // FIXME: For now, just drop all extension qualifiers on the floor. |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 1146 | } |
| 1147 | |
Anders Carlsson | 7b06f6c | 2009-12-10 03:14:39 +0000 | [diff] [blame] | 1148 | void CXXNameMangler::mangleObjCMethodName(const ObjCMethodDecl *MD) { |
Charles Davis | 685b1d9 | 2010-05-26 18:25:27 +0000 | [diff] [blame] | 1149 | llvm::SmallString<64> Buffer; |
| 1150 | MiscNameMangler(Context, Buffer).mangleObjCMethodName(MD); |
| 1151 | Out << Buffer; |
Anders Carlsson | 7b06f6c | 2009-12-10 03:14:39 +0000 | [diff] [blame] | 1152 | } |
| 1153 | |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 1154 | void CXXNameMangler::mangleType(QualType T) { |
Anders Carlsson | 4843e58 | 2009-03-10 17:07:44 +0000 | [diff] [blame] | 1155 | // Only operate on the canonical type! |
Anders Carlsson | b540491 | 2009-10-07 01:06:45 +0000 | [diff] [blame] | 1156 | T = Context.getASTContext().getCanonicalType(T); |
Anders Carlsson | 4843e58 | 2009-03-10 17:07:44 +0000 | [diff] [blame] | 1157 | |
Douglas Gregor | a4923eb | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 1158 | bool IsSubstitutable = T.hasLocalQualifiers() || !isa<BuiltinType>(T); |
Anders Carlsson | 7696737 | 2009-09-17 00:43:46 +0000 | [diff] [blame] | 1159 | if (IsSubstitutable && mangleSubstitution(T)) |
| 1160 | return; |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 1161 | |
Douglas Gregor | a4923eb | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 1162 | if (Qualifiers Quals = T.getLocalQualifiers()) { |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1163 | mangleQualifiers(Quals); |
| 1164 | // Recurse: even if the qualified type isn't yet substitutable, |
| 1165 | // the unqualified type might be. |
Douglas Gregor | a4923eb | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 1166 | mangleType(T.getLocalUnqualifiedType()); |
Anders Carlsson | 7696737 | 2009-09-17 00:43:46 +0000 | [diff] [blame] | 1167 | } else { |
| 1168 | switch (T->getTypeClass()) { |
John McCall | efe6aee | 2009-09-05 07:56:18 +0000 | [diff] [blame] | 1169 | #define ABSTRACT_TYPE(CLASS, PARENT) |
| 1170 | #define NON_CANONICAL_TYPE(CLASS, PARENT) \ |
Anders Carlsson | 7696737 | 2009-09-17 00:43:46 +0000 | [diff] [blame] | 1171 | case Type::CLASS: \ |
Jeffrey Yasskin | 9f61aa9 | 2009-12-12 05:05:38 +0000 | [diff] [blame] | 1172 | llvm_unreachable("can't mangle non-canonical type " #CLASS "Type"); \ |
Anders Carlsson | 7696737 | 2009-09-17 00:43:46 +0000 | [diff] [blame] | 1173 | return; |
John McCall | efe6aee | 2009-09-05 07:56:18 +0000 | [diff] [blame] | 1174 | #define TYPE(CLASS, PARENT) \ |
Anders Carlsson | 7696737 | 2009-09-17 00:43:46 +0000 | [diff] [blame] | 1175 | case Type::CLASS: \ |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1176 | mangleType(static_cast<const CLASS##Type*>(T.getTypePtr())); \ |
Anders Carlsson | 7696737 | 2009-09-17 00:43:46 +0000 | [diff] [blame] | 1177 | break; |
John McCall | efe6aee | 2009-09-05 07:56:18 +0000 | [diff] [blame] | 1178 | #include "clang/AST/TypeNodes.def" |
Anders Carlsson | 7696737 | 2009-09-17 00:43:46 +0000 | [diff] [blame] | 1179 | } |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 1180 | } |
Anders Carlsson | 7696737 | 2009-09-17 00:43:46 +0000 | [diff] [blame] | 1181 | |
| 1182 | // Add the substitution. |
| 1183 | if (IsSubstitutable) |
| 1184 | addSubstitution(T); |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 1185 | } |
| 1186 | |
Douglas Gregor | 1b12a3b | 2010-05-26 05:11:13 +0000 | [diff] [blame] | 1187 | void CXXNameMangler::mangleNameOrStandardSubstitution(const NamedDecl *ND) { |
| 1188 | if (!mangleStandardSubstitution(ND)) |
| 1189 | mangleName(ND); |
| 1190 | } |
| 1191 | |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 1192 | void CXXNameMangler::mangleType(const BuiltinType *T) { |
John McCall | efe6aee | 2009-09-05 07:56:18 +0000 | [diff] [blame] | 1193 | // <type> ::= <builtin-type> |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 1194 | // <builtin-type> ::= v # void |
| 1195 | // ::= w # wchar_t |
| 1196 | // ::= b # bool |
| 1197 | // ::= c # char |
| 1198 | // ::= a # signed char |
| 1199 | // ::= h # unsigned char |
| 1200 | // ::= s # short |
| 1201 | // ::= t # unsigned short |
| 1202 | // ::= i # int |
| 1203 | // ::= j # unsigned int |
| 1204 | // ::= l # long |
| 1205 | // ::= m # unsigned long |
| 1206 | // ::= x # long long, __int64 |
| 1207 | // ::= y # unsigned long long, __int64 |
| 1208 | // ::= n # __int128 |
| 1209 | // UNSUPPORTED: ::= o # unsigned __int128 |
| 1210 | // ::= f # float |
| 1211 | // ::= d # double |
| 1212 | // ::= e # long double, __float80 |
| 1213 | // UNSUPPORTED: ::= g # __float128 |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 1214 | // UNSUPPORTED: ::= Dd # IEEE 754r decimal floating point (64 bits) |
| 1215 | // UNSUPPORTED: ::= De # IEEE 754r decimal floating point (128 bits) |
| 1216 | // UNSUPPORTED: ::= Df # IEEE 754r decimal floating point (32 bits) |
| 1217 | // UNSUPPORTED: ::= Dh # IEEE 754r half-precision floating point (16 bits) |
Alisdair Meredith | f5c209d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 1218 | // ::= Di # char32_t |
| 1219 | // ::= Ds # char16_t |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 1220 | // ::= u <source-name> # vendor extended type |
Sebastian Redl | 6e8ed16 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 1221 | // From our point of view, std::nullptr_t is a builtin, but as far as mangling |
| 1222 | // is concerned, it's a type called std::nullptr_t. |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 1223 | switch (T->getKind()) { |
| 1224 | case BuiltinType::Void: Out << 'v'; break; |
| 1225 | case BuiltinType::Bool: Out << 'b'; break; |
| 1226 | case BuiltinType::Char_U: case BuiltinType::Char_S: Out << 'c'; break; |
| 1227 | case BuiltinType::UChar: Out << 'h'; break; |
| 1228 | case BuiltinType::UShort: Out << 't'; break; |
| 1229 | case BuiltinType::UInt: Out << 'j'; break; |
| 1230 | case BuiltinType::ULong: Out << 'm'; break; |
| 1231 | case BuiltinType::ULongLong: Out << 'y'; break; |
Chris Lattner | 2df9ced | 2009-04-30 02:43:43 +0000 | [diff] [blame] | 1232 | case BuiltinType::UInt128: Out << 'o'; break; |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 1233 | case BuiltinType::SChar: Out << 'a'; break; |
| 1234 | case BuiltinType::WChar: Out << 'w'; break; |
Alisdair Meredith | f5c209d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 1235 | case BuiltinType::Char16: Out << "Ds"; break; |
| 1236 | case BuiltinType::Char32: Out << "Di"; break; |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 1237 | case BuiltinType::Short: Out << 's'; break; |
| 1238 | case BuiltinType::Int: Out << 'i'; break; |
| 1239 | case BuiltinType::Long: Out << 'l'; break; |
| 1240 | case BuiltinType::LongLong: Out << 'x'; break; |
Chris Lattner | 2df9ced | 2009-04-30 02:43:43 +0000 | [diff] [blame] | 1241 | case BuiltinType::Int128: Out << 'n'; break; |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 1242 | case BuiltinType::Float: Out << 'f'; break; |
| 1243 | case BuiltinType::Double: Out << 'd'; break; |
| 1244 | case BuiltinType::LongDouble: Out << 'e'; break; |
Sebastian Redl | 6e8ed16 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 1245 | case BuiltinType::NullPtr: Out << "St9nullptr_t"; break; |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 1246 | |
| 1247 | case BuiltinType::Overload: |
| 1248 | case BuiltinType::Dependent: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1249 | assert(false && |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 1250 | "Overloaded and dependent types shouldn't get to name mangling"); |
| 1251 | break; |
Anders Carlsson | e89d159 | 2009-06-26 18:41:36 +0000 | [diff] [blame] | 1252 | case BuiltinType::UndeducedAuto: |
| 1253 | assert(0 && "Should not see undeduced auto here"); |
| 1254 | break; |
Steve Naroff | 9533a7f | 2009-07-22 17:14:51 +0000 | [diff] [blame] | 1255 | case BuiltinType::ObjCId: Out << "11objc_object"; break; |
| 1256 | case BuiltinType::ObjCClass: Out << "10objc_class"; break; |
Fariborz Jahanian | 13dcd00 | 2009-11-21 19:53:08 +0000 | [diff] [blame] | 1257 | case BuiltinType::ObjCSel: Out << "13objc_selector"; break; |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 1258 | } |
| 1259 | } |
| 1260 | |
John McCall | efe6aee | 2009-09-05 07:56:18 +0000 | [diff] [blame] | 1261 | // <type> ::= <function-type> |
| 1262 | // <function-type> ::= F [Y] <bare-function-type> E |
| 1263 | void CXXNameMangler::mangleType(const FunctionProtoType *T) { |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 1264 | Out << 'F'; |
Mike Stump | f5408fe | 2009-05-16 07:57:57 +0000 | [diff] [blame] | 1265 | // FIXME: We don't have enough information in the AST to produce the 'Y' |
| 1266 | // encoding for extern "C" function types. |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 1267 | mangleBareFunctionType(T, /*MangleReturnType=*/true); |
| 1268 | Out << 'E'; |
| 1269 | } |
John McCall | efe6aee | 2009-09-05 07:56:18 +0000 | [diff] [blame] | 1270 | void CXXNameMangler::mangleType(const FunctionNoProtoType *T) { |
Jeffrey Yasskin | 9f61aa9 | 2009-12-12 05:05:38 +0000 | [diff] [blame] | 1271 | llvm_unreachable("Can't mangle K&R function prototypes"); |
John McCall | efe6aee | 2009-09-05 07:56:18 +0000 | [diff] [blame] | 1272 | } |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 1273 | void CXXNameMangler::mangleBareFunctionType(const FunctionType *T, |
| 1274 | bool MangleReturnType) { |
John McCall | efe6aee | 2009-09-05 07:56:18 +0000 | [diff] [blame] | 1275 | // We should never be mangling something without a prototype. |
| 1276 | const FunctionProtoType *Proto = cast<FunctionProtoType>(T); |
| 1277 | |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 1278 | // <bare-function-type> ::= <signature type>+ |
| 1279 | if (MangleReturnType) |
John McCall | efe6aee | 2009-09-05 07:56:18 +0000 | [diff] [blame] | 1280 | mangleType(Proto->getResultType()); |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 1281 | |
Anders Carlsson | 9329668 | 2010-06-02 04:40:13 +0000 | [diff] [blame] | 1282 | if (Proto->getNumArgs() == 0 && !Proto->isVariadic()) { |
Eli Friedman | a7e6845 | 2010-08-22 01:00:03 +0000 | [diff] [blame^] | 1283 | // <builtin-type> ::= v # void |
Anders Carlsson | c6c91bc | 2009-04-01 00:15:23 +0000 | [diff] [blame] | 1284 | Out << 'v'; |
| 1285 | return; |
| 1286 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1287 | |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1288 | for (FunctionProtoType::arg_type_iterator Arg = Proto->arg_type_begin(), |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1289 | ArgEnd = Proto->arg_type_end(); |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 1290 | Arg != ArgEnd; ++Arg) |
| 1291 | mangleType(*Arg); |
Douglas Gregor | 219cc61 | 2009-02-13 01:28:03 +0000 | [diff] [blame] | 1292 | |
| 1293 | // <builtin-type> ::= z # ellipsis |
| 1294 | if (Proto->isVariadic()) |
| 1295 | Out << 'z'; |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 1296 | } |
| 1297 | |
John McCall | efe6aee | 2009-09-05 07:56:18 +0000 | [diff] [blame] | 1298 | // <type> ::= <class-enum-type> |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1299 | // <class-enum-type> ::= <name> |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 1300 | void CXXNameMangler::mangleType(const UnresolvedUsingType *T) { |
| 1301 | mangleName(T->getDecl()); |
| 1302 | } |
| 1303 | |
| 1304 | // <type> ::= <class-enum-type> |
| 1305 | // <class-enum-type> ::= <name> |
John McCall | efe6aee | 2009-09-05 07:56:18 +0000 | [diff] [blame] | 1306 | void CXXNameMangler::mangleType(const EnumType *T) { |
| 1307 | mangleType(static_cast<const TagType*>(T)); |
| 1308 | } |
| 1309 | void CXXNameMangler::mangleType(const RecordType *T) { |
| 1310 | mangleType(static_cast<const TagType*>(T)); |
| 1311 | } |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 1312 | void CXXNameMangler::mangleType(const TagType *T) { |
Eli Friedman | ecb7e93 | 2009-12-11 18:00:57 +0000 | [diff] [blame] | 1313 | mangleName(T->getDecl()); |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 1314 | } |
| 1315 | |
John McCall | efe6aee | 2009-09-05 07:56:18 +0000 | [diff] [blame] | 1316 | // <type> ::= <array-type> |
| 1317 | // <array-type> ::= A <positive dimension number> _ <element type> |
| 1318 | // ::= A [<dimension expression>] _ <element type> |
| 1319 | void CXXNameMangler::mangleType(const ConstantArrayType *T) { |
| 1320 | Out << 'A' << T->getSize() << '_'; |
| 1321 | mangleType(T->getElementType()); |
| 1322 | } |
| 1323 | void CXXNameMangler::mangleType(const VariableArrayType *T) { |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 1324 | Out << 'A'; |
John McCall | efe6aee | 2009-09-05 07:56:18 +0000 | [diff] [blame] | 1325 | mangleExpression(T->getSizeExpr()); |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 1326 | Out << '_'; |
| 1327 | mangleType(T->getElementType()); |
| 1328 | } |
John McCall | efe6aee | 2009-09-05 07:56:18 +0000 | [diff] [blame] | 1329 | void CXXNameMangler::mangleType(const DependentSizedArrayType *T) { |
| 1330 | Out << 'A'; |
| 1331 | mangleExpression(T->getSizeExpr()); |
| 1332 | Out << '_'; |
| 1333 | mangleType(T->getElementType()); |
| 1334 | } |
| 1335 | void CXXNameMangler::mangleType(const IncompleteArrayType *T) { |
| 1336 | Out << 'A' << '_'; |
| 1337 | mangleType(T->getElementType()); |
| 1338 | } |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 1339 | |
John McCall | efe6aee | 2009-09-05 07:56:18 +0000 | [diff] [blame] | 1340 | // <type> ::= <pointer-to-member-type> |
| 1341 | // <pointer-to-member-type> ::= M <class type> <member type> |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 1342 | void CXXNameMangler::mangleType(const MemberPointerType *T) { |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 1343 | Out << 'M'; |
| 1344 | mangleType(QualType(T->getClass(), 0)); |
Anders Carlsson | 0e65001 | 2009-05-17 17:41:20 +0000 | [diff] [blame] | 1345 | QualType PointeeType = T->getPointeeType(); |
| 1346 | if (const FunctionProtoType *FPT = dyn_cast<FunctionProtoType>(PointeeType)) { |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1347 | mangleQualifiers(Qualifiers::fromCVRMask(FPT->getTypeQuals())); |
Anders Carlsson | 0e65001 | 2009-05-17 17:41:20 +0000 | [diff] [blame] | 1348 | mangleType(FPT); |
Anders Carlsson | 9d85b72 | 2010-06-02 04:29:50 +0000 | [diff] [blame] | 1349 | |
| 1350 | // Itanium C++ ABI 5.1.8: |
| 1351 | // |
| 1352 | // The type of a non-static member function is considered to be different, |
| 1353 | // for the purposes of substitution, from the type of a namespace-scope or |
| 1354 | // static member function whose type appears similar. The types of two |
| 1355 | // non-static member functions are considered to be different, for the |
| 1356 | // purposes of substitution, if the functions are members of different |
| 1357 | // classes. In other words, for the purposes of substitution, the class of |
| 1358 | // which the function is a member is considered part of the type of |
| 1359 | // function. |
| 1360 | |
| 1361 | // We increment the SeqID here to emulate adding an entry to the |
| 1362 | // substitution table. We can't actually add it because we don't want this |
| 1363 | // particular function type to be substituted. |
| 1364 | ++SeqID; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1365 | } else |
Anders Carlsson | 0e65001 | 2009-05-17 17:41:20 +0000 | [diff] [blame] | 1366 | mangleType(PointeeType); |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 1367 | } |
| 1368 | |
John McCall | efe6aee | 2009-09-05 07:56:18 +0000 | [diff] [blame] | 1369 | // <type> ::= <template-param> |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 1370 | void CXXNameMangler::mangleType(const TemplateTypeParmType *T) { |
Anders Carlsson | 0ccdf8d | 2009-09-27 00:38:53 +0000 | [diff] [blame] | 1371 | mangleTemplateParameter(T->getIndex()); |
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> ::= P <type> # pointer-to |
| 1375 | void CXXNameMangler::mangleType(const PointerType *T) { |
| 1376 | Out << 'P'; |
| 1377 | mangleType(T->getPointeeType()); |
| 1378 | } |
| 1379 | void CXXNameMangler::mangleType(const ObjCObjectPointerType *T) { |
| 1380 | Out << 'P'; |
| 1381 | mangleType(T->getPointeeType()); |
| 1382 | } |
| 1383 | |
| 1384 | // <type> ::= R <type> # reference-to |
| 1385 | void CXXNameMangler::mangleType(const LValueReferenceType *T) { |
| 1386 | Out << 'R'; |
| 1387 | mangleType(T->getPointeeType()); |
| 1388 | } |
| 1389 | |
| 1390 | // <type> ::= O <type> # rvalue reference-to (C++0x) |
| 1391 | void CXXNameMangler::mangleType(const RValueReferenceType *T) { |
| 1392 | Out << 'O'; |
| 1393 | mangleType(T->getPointeeType()); |
| 1394 | } |
| 1395 | |
| 1396 | // <type> ::= C <type> # complex pair (C 2000) |
| 1397 | void CXXNameMangler::mangleType(const ComplexType *T) { |
| 1398 | Out << 'C'; |
| 1399 | mangleType(T->getElementType()); |
| 1400 | } |
| 1401 | |
| 1402 | // GNU extension: vector types |
Chris Lattner | 788b0fd | 2010-06-23 06:00:24 +0000 | [diff] [blame] | 1403 | // <type> ::= <vector-type> |
| 1404 | // <vector-type> ::= Dv <positive dimension number> _ |
| 1405 | // <extended element type> |
| 1406 | // ::= Dv [<dimension expression>] _ <element type> |
| 1407 | // <extended element type> ::= <element type> |
| 1408 | // ::= p # AltiVec vector pixel |
John McCall | efe6aee | 2009-09-05 07:56:18 +0000 | [diff] [blame] | 1409 | void CXXNameMangler::mangleType(const VectorType *T) { |
Nick Lewycky | 0e5f067 | 2010-03-26 07:18:04 +0000 | [diff] [blame] | 1410 | Out << "Dv" << T->getNumElements() << '_'; |
Chris Lattner | 788b0fd | 2010-06-23 06:00:24 +0000 | [diff] [blame] | 1411 | if (T->getAltiVecSpecific() == VectorType::Pixel) |
| 1412 | Out << 'p'; |
| 1413 | else if (T->getAltiVecSpecific() == VectorType::Bool) |
| 1414 | Out << 'b'; |
| 1415 | else |
| 1416 | mangleType(T->getElementType()); |
John McCall | efe6aee | 2009-09-05 07:56:18 +0000 | [diff] [blame] | 1417 | } |
| 1418 | void CXXNameMangler::mangleType(const ExtVectorType *T) { |
| 1419 | mangleType(static_cast<const VectorType*>(T)); |
| 1420 | } |
| 1421 | void CXXNameMangler::mangleType(const DependentSizedExtVectorType *T) { |
Nick Lewycky | 0e5f067 | 2010-03-26 07:18:04 +0000 | [diff] [blame] | 1422 | Out << "Dv"; |
| 1423 | mangleExpression(T->getSizeExpr()); |
| 1424 | Out << '_'; |
John McCall | efe6aee | 2009-09-05 07:56:18 +0000 | [diff] [blame] | 1425 | mangleType(T->getElementType()); |
| 1426 | } |
| 1427 | |
Anders Carlsson | a40c5e4 | 2009-03-07 22:03:21 +0000 | [diff] [blame] | 1428 | void CXXNameMangler::mangleType(const ObjCInterfaceType *T) { |
| 1429 | mangleSourceName(T->getDecl()->getIdentifier()); |
| 1430 | } |
| 1431 | |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 1432 | void CXXNameMangler::mangleType(const ObjCObjectType *T) { |
John McCall | c00c1f6 | 2010-05-15 17:06:29 +0000 | [diff] [blame] | 1433 | // We don't allow overloading by different protocol qualification, |
| 1434 | // so mangling them isn't necessary. |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 1435 | mangleType(T->getBaseType()); |
| 1436 | } |
| 1437 | |
John McCall | efe6aee | 2009-09-05 07:56:18 +0000 | [diff] [blame] | 1438 | void CXXNameMangler::mangleType(const BlockPointerType *T) { |
Anders Carlsson | f28c687 | 2009-12-23 22:31:44 +0000 | [diff] [blame] | 1439 | Out << "U13block_pointer"; |
| 1440 | mangleType(T->getPointeeType()); |
John McCall | efe6aee | 2009-09-05 07:56:18 +0000 | [diff] [blame] | 1441 | } |
| 1442 | |
John McCall | 31f17ec | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 1443 | void CXXNameMangler::mangleType(const InjectedClassNameType *T) { |
| 1444 | // Mangle injected class name types as if the user had written the |
| 1445 | // specialization out fully. It may not actually be possible to see |
| 1446 | // this mangling, though. |
| 1447 | mangleType(T->getInjectedSpecializationType()); |
| 1448 | } |
| 1449 | |
John McCall | efe6aee | 2009-09-05 07:56:18 +0000 | [diff] [blame] | 1450 | void CXXNameMangler::mangleType(const TemplateSpecializationType *T) { |
Douglas Gregor | 1e9268e | 2010-04-28 05:58:56 +0000 | [diff] [blame] | 1451 | if (TemplateDecl *TD = T->getTemplateName().getAsTemplateDecl()) { |
| 1452 | mangleName(TD, T->getArgs(), T->getNumArgs()); |
| 1453 | } else { |
| 1454 | if (mangleSubstitution(QualType(T, 0))) |
| 1455 | return; |
Sean Hunt | c302113 | 2010-05-05 15:23:54 +0000 | [diff] [blame] | 1456 | |
Douglas Gregor | 1e9268e | 2010-04-28 05:58:56 +0000 | [diff] [blame] | 1457 | mangleTemplatePrefix(T->getTemplateName()); |
Sean Hunt | c302113 | 2010-05-05 15:23:54 +0000 | [diff] [blame] | 1458 | |
Douglas Gregor | 1e9268e | 2010-04-28 05:58:56 +0000 | [diff] [blame] | 1459 | // FIXME: GCC does not appear to mangle the template arguments when |
| 1460 | // the template in question is a dependent template name. Should we |
| 1461 | // emulate that badness? |
| 1462 | mangleTemplateArgs(T->getTemplateName(), T->getArgs(), T->getNumArgs()); |
| 1463 | addSubstitution(QualType(T, 0)); |
| 1464 | } |
John McCall | efe6aee | 2009-09-05 07:56:18 +0000 | [diff] [blame] | 1465 | } |
| 1466 | |
Douglas Gregor | 4714c12 | 2010-03-31 17:34:00 +0000 | [diff] [blame] | 1467 | void CXXNameMangler::mangleType(const DependentNameType *T) { |
Anders Carlsson | ae35248 | 2009-09-26 02:26:02 +0000 | [diff] [blame] | 1468 | // Typename types are always nested |
| 1469 | Out << 'N'; |
John McCall | 3350095 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 1470 | mangleUnresolvedScope(T->getQualifier()); |
| 1471 | mangleSourceName(T->getIdentifier()); |
| 1472 | Out << 'E'; |
| 1473 | } |
John McCall | 6ab30e0 | 2010-06-09 07:26:17 +0000 | [diff] [blame] | 1474 | |
John McCall | 3350095 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 1475 | void CXXNameMangler::mangleType(const DependentTemplateSpecializationType *T) { |
| 1476 | // Dependently-scoped template types are always nested |
| 1477 | Out << 'N'; |
| 1478 | |
| 1479 | // TODO: avoid making this TemplateName. |
| 1480 | TemplateName Prefix = |
| 1481 | getASTContext().getDependentTemplateName(T->getQualifier(), |
| 1482 | T->getIdentifier()); |
| 1483 | mangleTemplatePrefix(Prefix); |
| 1484 | |
| 1485 | // FIXME: GCC does not appear to mangle the template arguments when |
| 1486 | // the template in question is a dependent template name. Should we |
| 1487 | // emulate that badness? |
| 1488 | mangleTemplateArgs(Prefix, T->getArgs(), T->getNumArgs()); |
Anders Carlsson | ae35248 | 2009-09-26 02:26:02 +0000 | [diff] [blame] | 1489 | Out << 'E'; |
John McCall | efe6aee | 2009-09-05 07:56:18 +0000 | [diff] [blame] | 1490 | } |
| 1491 | |
John McCall | ad5e738 | 2010-03-01 23:49:17 +0000 | [diff] [blame] | 1492 | void CXXNameMangler::mangleType(const TypeOfType *T) { |
| 1493 | // FIXME: this is pretty unsatisfactory, but there isn't an obvious |
| 1494 | // "extension with parameters" mangling. |
| 1495 | Out << "u6typeof"; |
| 1496 | } |
| 1497 | |
| 1498 | void CXXNameMangler::mangleType(const TypeOfExprType *T) { |
| 1499 | // FIXME: this is pretty unsatisfactory, but there isn't an obvious |
| 1500 | // "extension with parameters" mangling. |
| 1501 | Out << "u6typeof"; |
| 1502 | } |
| 1503 | |
| 1504 | void CXXNameMangler::mangleType(const DecltypeType *T) { |
| 1505 | Expr *E = T->getUnderlyingExpr(); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 1506 | |
John McCall | ad5e738 | 2010-03-01 23:49:17 +0000 | [diff] [blame] | 1507 | // type ::= Dt <expression> E # decltype of an id-expression |
| 1508 | // # or class member access |
| 1509 | // ::= DT <expression> E # decltype of an expression |
| 1510 | |
| 1511 | // This purports to be an exhaustive list of id-expressions and |
| 1512 | // class member accesses. Note that we do not ignore parentheses; |
| 1513 | // parentheses change the semantics of decltype for these |
| 1514 | // expressions (and cause the mangler to use the other form). |
| 1515 | if (isa<DeclRefExpr>(E) || |
| 1516 | isa<MemberExpr>(E) || |
| 1517 | isa<UnresolvedLookupExpr>(E) || |
| 1518 | isa<DependentScopeDeclRefExpr>(E) || |
| 1519 | isa<CXXDependentScopeMemberExpr>(E) || |
| 1520 | isa<UnresolvedMemberExpr>(E)) |
| 1521 | Out << "Dt"; |
| 1522 | else |
| 1523 | Out << "DT"; |
| 1524 | mangleExpression(E); |
| 1525 | Out << 'E'; |
| 1526 | } |
| 1527 | |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 1528 | void CXXNameMangler::mangleIntegerLiteral(QualType T, |
Anders Carlsson | e170ba7 | 2009-12-14 01:45:37 +0000 | [diff] [blame] | 1529 | const llvm::APSInt &Value) { |
| 1530 | // <expr-primary> ::= L <type> <value number> E # integer literal |
| 1531 | Out << 'L'; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 1532 | |
Anders Carlsson | e170ba7 | 2009-12-14 01:45:37 +0000 | [diff] [blame] | 1533 | mangleType(T); |
| 1534 | if (T->isBooleanType()) { |
| 1535 | // Boolean values are encoded as 0/1. |
| 1536 | Out << (Value.getBoolValue() ? '1' : '0'); |
| 1537 | } else { |
John McCall | 0512e48 | 2010-07-14 04:20:34 +0000 | [diff] [blame] | 1538 | mangleNumber(Value); |
Anders Carlsson | e170ba7 | 2009-12-14 01:45:37 +0000 | [diff] [blame] | 1539 | } |
| 1540 | Out << 'E'; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 1541 | |
Anders Carlsson | e170ba7 | 2009-12-14 01:45:37 +0000 | [diff] [blame] | 1542 | } |
| 1543 | |
John McCall | 2f27bf8 | 2010-02-04 02:56:29 +0000 | [diff] [blame] | 1544 | /// Mangles a member expression. Implicit accesses are not handled, |
| 1545 | /// but that should be okay, because you shouldn't be able to |
| 1546 | /// make an implicit access in a function template declaration. |
John McCall | 2f27bf8 | 2010-02-04 02:56:29 +0000 | [diff] [blame] | 1547 | void CXXNameMangler::mangleMemberExpr(const Expr *Base, |
| 1548 | bool IsArrow, |
| 1549 | NestedNameSpecifier *Qualifier, |
| 1550 | DeclarationName Member, |
| 1551 | unsigned Arity) { |
John McCall | e1e342f | 2010-03-01 19:12:25 +0000 | [diff] [blame] | 1552 | // gcc-4.4 uses 'dt' for dot expressions, which is reasonable. |
| 1553 | // OTOH, gcc also mangles the name as an expression. |
| 1554 | Out << (IsArrow ? "pt" : "dt"); |
John McCall | 2f27bf8 | 2010-02-04 02:56:29 +0000 | [diff] [blame] | 1555 | mangleExpression(Base); |
| 1556 | mangleUnresolvedName(Qualifier, Member, Arity); |
| 1557 | } |
| 1558 | |
John McCall | 5e1e89b | 2010-08-18 19:18:59 +0000 | [diff] [blame] | 1559 | void CXXNameMangler::mangleExpression(const Expr *E, unsigned Arity) { |
Anders Carlsson | d553f8c | 2009-09-21 01:21:10 +0000 | [diff] [blame] | 1560 | // <expression> ::= <unary operator-name> <expression> |
John McCall | 09cc141 | 2010-02-03 00:55:45 +0000 | [diff] [blame] | 1561 | // ::= <binary operator-name> <expression> <expression> |
| 1562 | // ::= <trinary operator-name> <expression> <expression> <expression> |
Eli Friedman | a7e6845 | 2010-08-22 01:00:03 +0000 | [diff] [blame^] | 1563 | // ::= cl <expression>* E # call |
Anders Carlsson | d553f8c | 2009-09-21 01:21:10 +0000 | [diff] [blame] | 1564 | // ::= cv <type> expression # conversion with one argument |
| 1565 | // ::= cv <type> _ <expression>* E # conversion with a different number of arguments |
Eli Friedman | a7e6845 | 2010-08-22 01:00:03 +0000 | [diff] [blame^] | 1566 | // ::= st <type> # sizeof (a type) |
Anders Carlsson | d553f8c | 2009-09-21 01:21:10 +0000 | [diff] [blame] | 1567 | // ::= at <type> # alignof (a type) |
| 1568 | // ::= <template-param> |
| 1569 | // ::= <function-param> |
| 1570 | // ::= sr <type> <unqualified-name> # dependent name |
| 1571 | // ::= sr <type> <unqualified-name> <template-args> # dependent template-id |
| 1572 | // ::= sZ <template-param> # size of a parameter pack |
John McCall | 09cc141 | 2010-02-03 00:55:45 +0000 | [diff] [blame] | 1573 | // ::= <expr-primary> |
John McCall | 1dd7383 | 2010-02-04 01:42:13 +0000 | [diff] [blame] | 1574 | // <expr-primary> ::= L <type> <value number> E # integer literal |
| 1575 | // ::= L <type <value float> E # floating literal |
| 1576 | // ::= L <mangled-name> E # external name |
Anders Carlsson | d553f8c | 2009-09-21 01:21:10 +0000 | [diff] [blame] | 1577 | switch (E->getStmtClass()) { |
John McCall | 6ae1f35 | 2010-04-09 22:26:14 +0000 | [diff] [blame] | 1578 | case Expr::NoStmtClass: |
| 1579 | #define EXPR(Type, Base) |
| 1580 | #define STMT(Type, Base) \ |
| 1581 | case Expr::Type##Class: |
Sean Hunt | 4bfe196 | 2010-05-05 15:24:00 +0000 | [diff] [blame] | 1582 | #include "clang/AST/StmtNodes.inc" |
John McCall | 0512e48 | 2010-07-14 04:20:34 +0000 | [diff] [blame] | 1583 | // fallthrough |
| 1584 | |
| 1585 | // These all can only appear in local or variable-initialization |
| 1586 | // contexts and so should never appear in a mangling. |
| 1587 | case Expr::AddrLabelExprClass: |
| 1588 | case Expr::BlockDeclRefExprClass: |
| 1589 | case Expr::CXXThisExprClass: |
| 1590 | case Expr::DesignatedInitExprClass: |
| 1591 | case Expr::ImplicitValueInitExprClass: |
| 1592 | case Expr::InitListExprClass: |
| 1593 | case Expr::ParenListExprClass: |
| 1594 | case Expr::CXXScalarValueInitExprClass: |
John McCall | 09cc141 | 2010-02-03 00:55:45 +0000 | [diff] [blame] | 1595 | llvm_unreachable("unexpected statement kind"); |
| 1596 | break; |
| 1597 | |
John McCall | 0512e48 | 2010-07-14 04:20:34 +0000 | [diff] [blame] | 1598 | // FIXME: invent manglings for all these. |
| 1599 | case Expr::BlockExprClass: |
| 1600 | case Expr::CXXPseudoDestructorExprClass: |
| 1601 | case Expr::ChooseExprClass: |
| 1602 | case Expr::CompoundLiteralExprClass: |
| 1603 | case Expr::ExtVectorElementExprClass: |
| 1604 | case Expr::ObjCEncodeExprClass: |
| 1605 | case Expr::ObjCImplicitSetterGetterRefExprClass: |
| 1606 | case Expr::ObjCIsaExprClass: |
| 1607 | case Expr::ObjCIvarRefExprClass: |
| 1608 | case Expr::ObjCMessageExprClass: |
| 1609 | case Expr::ObjCPropertyRefExprClass: |
| 1610 | case Expr::ObjCProtocolExprClass: |
| 1611 | case Expr::ObjCSelectorExprClass: |
| 1612 | case Expr::ObjCStringLiteralClass: |
| 1613 | case Expr::ObjCSuperExprClass: |
| 1614 | case Expr::OffsetOfExprClass: |
| 1615 | case Expr::PredefinedExprClass: |
| 1616 | case Expr::ShuffleVectorExprClass: |
| 1617 | case Expr::StmtExprClass: |
| 1618 | case Expr::TypesCompatibleExprClass: |
| 1619 | case Expr::UnaryTypeTraitExprClass: |
| 1620 | case Expr::VAArgExprClass: { |
John McCall | 6ae1f35 | 2010-04-09 22:26:14 +0000 | [diff] [blame] | 1621 | // As bad as this diagnostic is, it's better than crashing. |
| 1622 | Diagnostic &Diags = Context.getDiags(); |
| 1623 | unsigned DiagID = Diags.getCustomDiagID(Diagnostic::Error, |
| 1624 | "cannot yet mangle expression type %0"); |
John McCall | 739bf09 | 2010-04-10 09:39:25 +0000 | [diff] [blame] | 1625 | Diags.Report(FullSourceLoc(E->getExprLoc(), |
| 1626 | getASTContext().getSourceManager()), |
| 1627 | DiagID) |
| 1628 | << E->getStmtClassName() << E->getSourceRange(); |
John McCall | 6ae1f35 | 2010-04-09 22:26:14 +0000 | [diff] [blame] | 1629 | break; |
| 1630 | } |
| 1631 | |
John McCall | 0512e48 | 2010-07-14 04:20:34 +0000 | [diff] [blame] | 1632 | case Expr::CXXDefaultArgExprClass: |
John McCall | 5e1e89b | 2010-08-18 19:18:59 +0000 | [diff] [blame] | 1633 | mangleExpression(cast<CXXDefaultArgExpr>(E)->getExpr(), Arity); |
John McCall | 0512e48 | 2010-07-14 04:20:34 +0000 | [diff] [blame] | 1634 | break; |
| 1635 | |
| 1636 | case Expr::CXXMemberCallExprClass: // fallthrough |
John McCall | 1dd7383 | 2010-02-04 01:42:13 +0000 | [diff] [blame] | 1637 | case Expr::CallExprClass: { |
| 1638 | const CallExpr *CE = cast<CallExpr>(E); |
| 1639 | Out << "cl"; |
John McCall | 5e1e89b | 2010-08-18 19:18:59 +0000 | [diff] [blame] | 1640 | mangleExpression(CE->getCallee(), CE->getNumArgs()); |
John McCall | 1dd7383 | 2010-02-04 01:42:13 +0000 | [diff] [blame] | 1641 | for (unsigned I = 0, N = CE->getNumArgs(); I != N; ++I) |
| 1642 | mangleExpression(CE->getArg(I)); |
Benjamin Kramer | 35f59b6 | 2010-04-10 16:03:31 +0000 | [diff] [blame] | 1643 | Out << 'E'; |
John McCall | 09cc141 | 2010-02-03 00:55:45 +0000 | [diff] [blame] | 1644 | break; |
John McCall | 1dd7383 | 2010-02-04 01:42:13 +0000 | [diff] [blame] | 1645 | } |
John McCall | 09cc141 | 2010-02-03 00:55:45 +0000 | [diff] [blame] | 1646 | |
John McCall | 0512e48 | 2010-07-14 04:20:34 +0000 | [diff] [blame] | 1647 | case Expr::CXXNewExprClass: { |
| 1648 | // Proposal from David Vandervoorde, 2010.06.30 |
| 1649 | const CXXNewExpr *New = cast<CXXNewExpr>(E); |
| 1650 | if (New->isGlobalNew()) Out << "gs"; |
| 1651 | Out << (New->isArray() ? "na" : "nw"); |
| 1652 | for (CXXNewExpr::const_arg_iterator I = New->placement_arg_begin(), |
| 1653 | E = New->placement_arg_end(); I != E; ++I) |
| 1654 | mangleExpression(*I); |
| 1655 | Out << '_'; |
| 1656 | mangleType(New->getAllocatedType()); |
| 1657 | if (New->hasInitializer()) { |
| 1658 | Out << "pi"; |
| 1659 | for (CXXNewExpr::const_arg_iterator I = New->constructor_arg_begin(), |
| 1660 | E = New->constructor_arg_end(); I != E; ++I) |
| 1661 | mangleExpression(*I); |
| 1662 | } |
| 1663 | Out << 'E'; |
| 1664 | break; |
| 1665 | } |
| 1666 | |
John McCall | 2f27bf8 | 2010-02-04 02:56:29 +0000 | [diff] [blame] | 1667 | case Expr::MemberExprClass: { |
| 1668 | const MemberExpr *ME = cast<MemberExpr>(E); |
| 1669 | mangleMemberExpr(ME->getBase(), ME->isArrow(), |
| 1670 | ME->getQualifier(), ME->getMemberDecl()->getDeclName(), |
John McCall | 5e1e89b | 2010-08-18 19:18:59 +0000 | [diff] [blame] | 1671 | Arity); |
John McCall | 2f27bf8 | 2010-02-04 02:56:29 +0000 | [diff] [blame] | 1672 | break; |
| 1673 | } |
| 1674 | |
| 1675 | case Expr::UnresolvedMemberExprClass: { |
| 1676 | const UnresolvedMemberExpr *ME = cast<UnresolvedMemberExpr>(E); |
| 1677 | mangleMemberExpr(ME->getBase(), ME->isArrow(), |
| 1678 | ME->getQualifier(), ME->getMemberName(), |
John McCall | 5e1e89b | 2010-08-18 19:18:59 +0000 | [diff] [blame] | 1679 | Arity); |
John McCall | 6dbce19 | 2010-08-20 00:17:19 +0000 | [diff] [blame] | 1680 | if (ME->hasExplicitTemplateArgs()) |
| 1681 | mangleTemplateArgs(ME->getExplicitTemplateArgs()); |
John McCall | 2f27bf8 | 2010-02-04 02:56:29 +0000 | [diff] [blame] | 1682 | break; |
| 1683 | } |
| 1684 | |
| 1685 | case Expr::CXXDependentScopeMemberExprClass: { |
| 1686 | const CXXDependentScopeMemberExpr *ME |
| 1687 | = cast<CXXDependentScopeMemberExpr>(E); |
| 1688 | mangleMemberExpr(ME->getBase(), ME->isArrow(), |
| 1689 | ME->getQualifier(), ME->getMember(), |
John McCall | 5e1e89b | 2010-08-18 19:18:59 +0000 | [diff] [blame] | 1690 | Arity); |
John McCall | 6dbce19 | 2010-08-20 00:17:19 +0000 | [diff] [blame] | 1691 | if (ME->hasExplicitTemplateArgs()) |
| 1692 | mangleTemplateArgs(ME->getExplicitTemplateArgs()); |
John McCall | 2f27bf8 | 2010-02-04 02:56:29 +0000 | [diff] [blame] | 1693 | break; |
| 1694 | } |
| 1695 | |
John McCall | 1dd7383 | 2010-02-04 01:42:13 +0000 | [diff] [blame] | 1696 | case Expr::UnresolvedLookupExprClass: { |
John McCall | a3218e7 | 2010-02-04 01:48:38 +0000 | [diff] [blame] | 1697 | // The ABI doesn't cover how to mangle overload sets, so we mangle |
| 1698 | // using something as close as possible to the original lookup |
| 1699 | // expression. |
John McCall | 1dd7383 | 2010-02-04 01:42:13 +0000 | [diff] [blame] | 1700 | const UnresolvedLookupExpr *ULE = cast<UnresolvedLookupExpr>(E); |
John McCall | 5e1e89b | 2010-08-18 19:18:59 +0000 | [diff] [blame] | 1701 | mangleUnresolvedName(ULE->getQualifier(), ULE->getName(), Arity); |
John McCall | 6dbce19 | 2010-08-20 00:17:19 +0000 | [diff] [blame] | 1702 | if (ULE->hasExplicitTemplateArgs()) |
| 1703 | mangleTemplateArgs(ULE->getExplicitTemplateArgs()); |
John McCall | 1dd7383 | 2010-02-04 01:42:13 +0000 | [diff] [blame] | 1704 | break; |
| 1705 | } |
| 1706 | |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 1707 | case Expr::CXXUnresolvedConstructExprClass: { |
John McCall | 1dd7383 | 2010-02-04 01:42:13 +0000 | [diff] [blame] | 1708 | const CXXUnresolvedConstructExpr *CE = cast<CXXUnresolvedConstructExpr>(E); |
| 1709 | unsigned N = CE->arg_size(); |
| 1710 | |
| 1711 | Out << "cv"; |
| 1712 | mangleType(CE->getType()); |
Benjamin Kramer | 35f59b6 | 2010-04-10 16:03:31 +0000 | [diff] [blame] | 1713 | if (N != 1) Out << '_'; |
John McCall | 1dd7383 | 2010-02-04 01:42:13 +0000 | [diff] [blame] | 1714 | for (unsigned I = 0; I != N; ++I) mangleExpression(CE->getArg(I)); |
Benjamin Kramer | 35f59b6 | 2010-04-10 16:03:31 +0000 | [diff] [blame] | 1715 | if (N != 1) Out << 'E'; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 1716 | break; |
John McCall | 1dd7383 | 2010-02-04 01:42:13 +0000 | [diff] [blame] | 1717 | } |
John McCall | 09cc141 | 2010-02-03 00:55:45 +0000 | [diff] [blame] | 1718 | |
John McCall | 1dd7383 | 2010-02-04 01:42:13 +0000 | [diff] [blame] | 1719 | case Expr::CXXTemporaryObjectExprClass: |
| 1720 | case Expr::CXXConstructExprClass: { |
| 1721 | const CXXConstructExpr *CE = cast<CXXConstructExpr>(E); |
| 1722 | unsigned N = CE->getNumArgs(); |
| 1723 | |
| 1724 | Out << "cv"; |
| 1725 | mangleType(CE->getType()); |
Benjamin Kramer | 35f59b6 | 2010-04-10 16:03:31 +0000 | [diff] [blame] | 1726 | if (N != 1) Out << '_'; |
John McCall | 1dd7383 | 2010-02-04 01:42:13 +0000 | [diff] [blame] | 1727 | for (unsigned I = 0; I != N; ++I) mangleExpression(CE->getArg(I)); |
Benjamin Kramer | 35f59b6 | 2010-04-10 16:03:31 +0000 | [diff] [blame] | 1728 | if (N != 1) Out << 'E'; |
John McCall | 09cc141 | 2010-02-03 00:55:45 +0000 | [diff] [blame] | 1729 | break; |
John McCall | 1dd7383 | 2010-02-04 01:42:13 +0000 | [diff] [blame] | 1730 | } |
| 1731 | |
| 1732 | case Expr::SizeOfAlignOfExprClass: { |
| 1733 | const SizeOfAlignOfExpr *SAE = cast<SizeOfAlignOfExpr>(E); |
Benjamin Kramer | 35f59b6 | 2010-04-10 16:03:31 +0000 | [diff] [blame] | 1734 | if (SAE->isSizeOf()) Out << 's'; |
| 1735 | else Out << 'a'; |
John McCall | 1dd7383 | 2010-02-04 01:42:13 +0000 | [diff] [blame] | 1736 | if (SAE->isArgumentType()) { |
Benjamin Kramer | 35f59b6 | 2010-04-10 16:03:31 +0000 | [diff] [blame] | 1737 | Out << 't'; |
John McCall | 1dd7383 | 2010-02-04 01:42:13 +0000 | [diff] [blame] | 1738 | mangleType(SAE->getArgumentType()); |
| 1739 | } else { |
Benjamin Kramer | 35f59b6 | 2010-04-10 16:03:31 +0000 | [diff] [blame] | 1740 | Out << 'z'; |
John McCall | 1dd7383 | 2010-02-04 01:42:13 +0000 | [diff] [blame] | 1741 | mangleExpression(SAE->getArgumentExpr()); |
| 1742 | } |
| 1743 | break; |
| 1744 | } |
Anders Carlsson | a769408 | 2009-11-06 02:50:19 +0000 | [diff] [blame] | 1745 | |
John McCall | 0512e48 | 2010-07-14 04:20:34 +0000 | [diff] [blame] | 1746 | case Expr::CXXThrowExprClass: { |
| 1747 | const CXXThrowExpr *TE = cast<CXXThrowExpr>(E); |
| 1748 | |
| 1749 | // Proposal from David Vandervoorde, 2010.06.30 |
| 1750 | if (TE->getSubExpr()) { |
| 1751 | Out << "tw"; |
| 1752 | mangleExpression(TE->getSubExpr()); |
| 1753 | } else { |
| 1754 | Out << "tr"; |
| 1755 | } |
| 1756 | break; |
| 1757 | } |
| 1758 | |
| 1759 | case Expr::CXXTypeidExprClass: { |
| 1760 | const CXXTypeidExpr *TIE = cast<CXXTypeidExpr>(E); |
| 1761 | |
| 1762 | // Proposal from David Vandervoorde, 2010.06.30 |
| 1763 | if (TIE->isTypeOperand()) { |
| 1764 | Out << "ti"; |
| 1765 | mangleType(TIE->getTypeOperand()); |
| 1766 | } else { |
| 1767 | Out << "te"; |
| 1768 | mangleExpression(TIE->getExprOperand()); |
| 1769 | } |
| 1770 | break; |
| 1771 | } |
| 1772 | |
| 1773 | case Expr::CXXDeleteExprClass: { |
| 1774 | const CXXDeleteExpr *DE = cast<CXXDeleteExpr>(E); |
| 1775 | |
| 1776 | // Proposal from David Vandervoorde, 2010.06.30 |
| 1777 | if (DE->isGlobalDelete()) Out << "gs"; |
| 1778 | Out << (DE->isArrayForm() ? "da" : "dl"); |
| 1779 | mangleExpression(DE->getArgument()); |
| 1780 | break; |
| 1781 | } |
| 1782 | |
Anders Carlsson | e170ba7 | 2009-12-14 01:45:37 +0000 | [diff] [blame] | 1783 | case Expr::UnaryOperatorClass: { |
| 1784 | const UnaryOperator *UO = cast<UnaryOperator>(E); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 1785 | mangleOperatorName(UnaryOperator::getOverloadedOperator(UO->getOpcode()), |
Anders Carlsson | e170ba7 | 2009-12-14 01:45:37 +0000 | [diff] [blame] | 1786 | /*Arity=*/1); |
| 1787 | mangleExpression(UO->getSubExpr()); |
| 1788 | break; |
| 1789 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 1790 | |
John McCall | 0512e48 | 2010-07-14 04:20:34 +0000 | [diff] [blame] | 1791 | case Expr::ArraySubscriptExprClass: { |
| 1792 | const ArraySubscriptExpr *AE = cast<ArraySubscriptExpr>(E); |
| 1793 | |
| 1794 | // Array subscript is treated as a syntactically wierd form of |
| 1795 | // binary operator. |
| 1796 | Out << "ix"; |
| 1797 | mangleExpression(AE->getLHS()); |
| 1798 | mangleExpression(AE->getRHS()); |
| 1799 | break; |
| 1800 | } |
| 1801 | |
| 1802 | case Expr::CompoundAssignOperatorClass: // fallthrough |
Anders Carlsson | e170ba7 | 2009-12-14 01:45:37 +0000 | [diff] [blame] | 1803 | case Expr::BinaryOperatorClass: { |
| 1804 | const BinaryOperator *BO = cast<BinaryOperator>(E); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 1805 | mangleOperatorName(BinaryOperator::getOverloadedOperator(BO->getOpcode()), |
Anders Carlsson | e170ba7 | 2009-12-14 01:45:37 +0000 | [diff] [blame] | 1806 | /*Arity=*/2); |
| 1807 | mangleExpression(BO->getLHS()); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 1808 | mangleExpression(BO->getRHS()); |
Anders Carlsson | e170ba7 | 2009-12-14 01:45:37 +0000 | [diff] [blame] | 1809 | break; |
John McCall | 2f27bf8 | 2010-02-04 02:56:29 +0000 | [diff] [blame] | 1810 | } |
Anders Carlsson | e170ba7 | 2009-12-14 01:45:37 +0000 | [diff] [blame] | 1811 | |
| 1812 | case Expr::ConditionalOperatorClass: { |
| 1813 | const ConditionalOperator *CO = cast<ConditionalOperator>(E); |
| 1814 | mangleOperatorName(OO_Conditional, /*Arity=*/3); |
| 1815 | mangleExpression(CO->getCond()); |
John McCall | 5e1e89b | 2010-08-18 19:18:59 +0000 | [diff] [blame] | 1816 | mangleExpression(CO->getLHS(), Arity); |
| 1817 | mangleExpression(CO->getRHS(), Arity); |
Anders Carlsson | e170ba7 | 2009-12-14 01:45:37 +0000 | [diff] [blame] | 1818 | break; |
| 1819 | } |
| 1820 | |
Douglas Gregor | 46287c7 | 2010-01-29 16:37:09 +0000 | [diff] [blame] | 1821 | case Expr::ImplicitCastExprClass: { |
John McCall | 5e1e89b | 2010-08-18 19:18:59 +0000 | [diff] [blame] | 1822 | mangleExpression(cast<ImplicitCastExpr>(E)->getSubExpr(), Arity); |
Douglas Gregor | 46287c7 | 2010-01-29 16:37:09 +0000 | [diff] [blame] | 1823 | break; |
| 1824 | } |
| 1825 | |
| 1826 | case Expr::CStyleCastExprClass: |
| 1827 | case Expr::CXXStaticCastExprClass: |
| 1828 | case Expr::CXXDynamicCastExprClass: |
| 1829 | case Expr::CXXReinterpretCastExprClass: |
| 1830 | case Expr::CXXConstCastExprClass: |
| 1831 | case Expr::CXXFunctionalCastExprClass: { |
| 1832 | const ExplicitCastExpr *ECE = cast<ExplicitCastExpr>(E); |
| 1833 | Out << "cv"; |
| 1834 | mangleType(ECE->getType()); |
| 1835 | mangleExpression(ECE->getSubExpr()); |
| 1836 | break; |
| 1837 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 1838 | |
Anders Carlsson | 58040a5 | 2009-12-16 05:48:46 +0000 | [diff] [blame] | 1839 | case Expr::CXXOperatorCallExprClass: { |
| 1840 | const CXXOperatorCallExpr *CE = cast<CXXOperatorCallExpr>(E); |
| 1841 | unsigned NumArgs = CE->getNumArgs(); |
| 1842 | mangleOperatorName(CE->getOperator(), /*Arity=*/NumArgs); |
| 1843 | // Mangle the arguments. |
| 1844 | for (unsigned i = 0; i != NumArgs; ++i) |
| 1845 | mangleExpression(CE->getArg(i)); |
| 1846 | break; |
| 1847 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 1848 | |
Anders Carlsson | a769408 | 2009-11-06 02:50:19 +0000 | [diff] [blame] | 1849 | case Expr::ParenExprClass: |
John McCall | 5e1e89b | 2010-08-18 19:18:59 +0000 | [diff] [blame] | 1850 | mangleExpression(cast<ParenExpr>(E)->getSubExpr(), Arity); |
Anders Carlsson | a769408 | 2009-11-06 02:50:19 +0000 | [diff] [blame] | 1851 | break; |
| 1852 | |
Anders Carlsson | d553f8c | 2009-09-21 01:21:10 +0000 | [diff] [blame] | 1853 | case Expr::DeclRefExprClass: { |
Douglas Gregor | 5ed1bc3 | 2010-02-28 21:40:32 +0000 | [diff] [blame] | 1854 | const NamedDecl *D = cast<DeclRefExpr>(E)->getDecl(); |
Daniel Dunbar | 3c9e463 | 2009-11-21 09:05:47 +0000 | [diff] [blame] | 1855 | |
Anders Carlsson | d553f8c | 2009-09-21 01:21:10 +0000 | [diff] [blame] | 1856 | switch (D->getKind()) { |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 1857 | default: |
Douglas Gregor | 5ed1bc3 | 2010-02-28 21:40:32 +0000 | [diff] [blame] | 1858 | // <expr-primary> ::= L <mangled-name> E # external name |
| 1859 | Out << 'L'; |
| 1860 | mangle(D, "_Z"); |
| 1861 | Out << 'E'; |
| 1862 | break; |
| 1863 | |
John McCall | 3dc7e7b | 2010-07-24 01:17:35 +0000 | [diff] [blame] | 1864 | case Decl::EnumConstant: { |
| 1865 | const EnumConstantDecl *ED = cast<EnumConstantDecl>(D); |
| 1866 | mangleIntegerLiteral(ED->getType(), ED->getInitVal()); |
| 1867 | break; |
| 1868 | } |
| 1869 | |
Anders Carlsson | d553f8c | 2009-09-21 01:21:10 +0000 | [diff] [blame] | 1870 | case Decl::NonTypeTemplateParm: { |
| 1871 | const NonTypeTemplateParmDecl *PD = cast<NonTypeTemplateParmDecl>(D); |
Anders Carlsson | 0ccdf8d | 2009-09-27 00:38:53 +0000 | [diff] [blame] | 1872 | mangleTemplateParameter(PD->getIndex()); |
Anders Carlsson | d553f8c | 2009-09-21 01:21:10 +0000 | [diff] [blame] | 1873 | break; |
| 1874 | } |
| 1875 | |
| 1876 | } |
Daniel Dunbar | 3c9e463 | 2009-11-21 09:05:47 +0000 | [diff] [blame] | 1877 | |
Anders Carlsson | 50755b0 | 2009-09-27 20:11:34 +0000 | [diff] [blame] | 1878 | break; |
Anders Carlsson | d553f8c | 2009-09-21 01:21:10 +0000 | [diff] [blame] | 1879 | } |
Daniel Dunbar | 3c9e463 | 2009-11-21 09:05:47 +0000 | [diff] [blame] | 1880 | |
John McCall | 865d447 | 2009-11-19 22:55:06 +0000 | [diff] [blame] | 1881 | case Expr::DependentScopeDeclRefExprClass: { |
| 1882 | const DependentScopeDeclRefExpr *DRE = cast<DependentScopeDeclRefExpr>(E); |
Douglas Gregor | 4b2ccfc | 2010-02-28 22:05:49 +0000 | [diff] [blame] | 1883 | NestedNameSpecifier *NNS = DRE->getQualifier(); |
| 1884 | const Type *QTy = NNS->getAsType(); |
| 1885 | |
| 1886 | // When we're dealing with a nested-name-specifier that has just a |
| 1887 | // dependent identifier in it, mangle that as a typename. FIXME: |
| 1888 | // It isn't clear that we ever actually want to have such a |
| 1889 | // nested-name-specifier; why not just represent it as a typename type? |
| 1890 | if (!QTy && NNS->getAsIdentifier() && NNS->getPrefix()) { |
Douglas Gregor | 4a2023f | 2010-03-31 20:19:30 +0000 | [diff] [blame] | 1891 | QTy = getASTContext().getDependentNameType(ETK_Typename, |
| 1892 | NNS->getPrefix(), |
| 1893 | NNS->getAsIdentifier()) |
Douglas Gregor | 4b2ccfc | 2010-02-28 22:05:49 +0000 | [diff] [blame] | 1894 | .getTypePtr(); |
| 1895 | } |
Anders Carlsson | 50755b0 | 2009-09-27 20:11:34 +0000 | [diff] [blame] | 1896 | assert(QTy && "Qualifier was not type!"); |
| 1897 | |
John McCall | 6dbce19 | 2010-08-20 00:17:19 +0000 | [diff] [blame] | 1898 | // ::= sr <type> <unqualified-name> # dependent name |
| 1899 | // ::= sr <type> <unqualified-name> <template-args> # dependent template-id |
Anders Carlsson | 50755b0 | 2009-09-27 20:11:34 +0000 | [diff] [blame] | 1900 | Out << "sr"; |
| 1901 | mangleType(QualType(QTy, 0)); |
John McCall | 5e1e89b | 2010-08-18 19:18:59 +0000 | [diff] [blame] | 1902 | mangleUnqualifiedName(0, DRE->getDeclName(), Arity); |
John McCall | 6dbce19 | 2010-08-20 00:17:19 +0000 | [diff] [blame] | 1903 | if (DRE->hasExplicitTemplateArgs()) |
| 1904 | mangleTemplateArgs(DRE->getExplicitTemplateArgs()); |
Daniel Dunbar | 3c9e463 | 2009-11-21 09:05:47 +0000 | [diff] [blame] | 1905 | |
Anders Carlsson | 50755b0 | 2009-09-27 20:11:34 +0000 | [diff] [blame] | 1906 | break; |
| 1907 | } |
| 1908 | |
John McCall | d930760 | 2010-04-09 22:54:09 +0000 | [diff] [blame] | 1909 | case Expr::CXXBindReferenceExprClass: |
| 1910 | mangleExpression(cast<CXXBindReferenceExpr>(E)->getSubExpr()); |
| 1911 | break; |
| 1912 | |
| 1913 | case Expr::CXXBindTemporaryExprClass: |
| 1914 | mangleExpression(cast<CXXBindTemporaryExpr>(E)->getSubExpr()); |
| 1915 | break; |
| 1916 | |
| 1917 | case Expr::CXXExprWithTemporariesClass: |
John McCall | 5e1e89b | 2010-08-18 19:18:59 +0000 | [diff] [blame] | 1918 | mangleExpression(cast<CXXExprWithTemporaries>(E)->getSubExpr(), Arity); |
John McCall | d930760 | 2010-04-09 22:54:09 +0000 | [diff] [blame] | 1919 | break; |
| 1920 | |
John McCall | 1dd7383 | 2010-02-04 01:42:13 +0000 | [diff] [blame] | 1921 | case Expr::FloatingLiteralClass: { |
| 1922 | const FloatingLiteral *FL = cast<FloatingLiteral>(E); |
Benjamin Kramer | 35f59b6 | 2010-04-10 16:03:31 +0000 | [diff] [blame] | 1923 | Out << 'L'; |
John McCall | 1dd7383 | 2010-02-04 01:42:13 +0000 | [diff] [blame] | 1924 | mangleType(FL->getType()); |
John McCall | 0512e48 | 2010-07-14 04:20:34 +0000 | [diff] [blame] | 1925 | mangleFloat(FL->getValue()); |
Benjamin Kramer | 35f59b6 | 2010-04-10 16:03:31 +0000 | [diff] [blame] | 1926 | Out << 'E'; |
John McCall | 1dd7383 | 2010-02-04 01:42:13 +0000 | [diff] [blame] | 1927 | break; |
| 1928 | } |
| 1929 | |
John McCall | de81063 | 2010-04-09 21:48:08 +0000 | [diff] [blame] | 1930 | case Expr::CharacterLiteralClass: |
Benjamin Kramer | 35f59b6 | 2010-04-10 16:03:31 +0000 | [diff] [blame] | 1931 | Out << 'L'; |
John McCall | de81063 | 2010-04-09 21:48:08 +0000 | [diff] [blame] | 1932 | mangleType(E->getType()); |
| 1933 | Out << cast<CharacterLiteral>(E)->getValue(); |
| 1934 | Out << 'E'; |
| 1935 | break; |
| 1936 | |
| 1937 | case Expr::CXXBoolLiteralExprClass: |
| 1938 | Out << "Lb"; |
| 1939 | Out << (cast<CXXBoolLiteralExpr>(E)->getValue() ? '1' : '0'); |
| 1940 | Out << 'E'; |
| 1941 | break; |
| 1942 | |
John McCall | 0512e48 | 2010-07-14 04:20:34 +0000 | [diff] [blame] | 1943 | case Expr::IntegerLiteralClass: { |
| 1944 | llvm::APSInt Value(cast<IntegerLiteral>(E)->getValue()); |
| 1945 | if (E->getType()->isSignedIntegerType()) |
| 1946 | Value.setIsSigned(true); |
| 1947 | mangleIntegerLiteral(E->getType(), Value); |
Anders Carlsson | e170ba7 | 2009-12-14 01:45:37 +0000 | [diff] [blame] | 1948 | break; |
John McCall | 0512e48 | 2010-07-14 04:20:34 +0000 | [diff] [blame] | 1949 | } |
| 1950 | |
| 1951 | case Expr::ImaginaryLiteralClass: { |
| 1952 | const ImaginaryLiteral *IE = cast<ImaginaryLiteral>(E); |
| 1953 | // Mangle as if a complex literal. |
| 1954 | // Proposal from David Vandervoorde, 2010.06.30. |
| 1955 | Out << 'L'; |
| 1956 | mangleType(E->getType()); |
| 1957 | if (const FloatingLiteral *Imag = |
| 1958 | dyn_cast<FloatingLiteral>(IE->getSubExpr())) { |
| 1959 | // Mangle a floating-point zero of the appropriate type. |
| 1960 | mangleFloat(llvm::APFloat(Imag->getValue().getSemantics())); |
| 1961 | Out << '_'; |
| 1962 | mangleFloat(Imag->getValue()); |
| 1963 | } else { |
| 1964 | Out << '0' << '_'; |
| 1965 | llvm::APSInt Value(cast<IntegerLiteral>(IE->getSubExpr())->getValue()); |
| 1966 | if (IE->getSubExpr()->getType()->isSignedIntegerType()) |
| 1967 | Value.setIsSigned(true); |
| 1968 | mangleNumber(Value); |
| 1969 | } |
| 1970 | Out << 'E'; |
| 1971 | break; |
| 1972 | } |
| 1973 | |
| 1974 | case Expr::StringLiteralClass: { |
John McCall | 1658c39 | 2010-07-15 21:53:03 +0000 | [diff] [blame] | 1975 | // Revised proposal from David Vandervoorde, 2010.07.15. |
John McCall | 0512e48 | 2010-07-14 04:20:34 +0000 | [diff] [blame] | 1976 | Out << 'L'; |
John McCall | 1658c39 | 2010-07-15 21:53:03 +0000 | [diff] [blame] | 1977 | assert(isa<ConstantArrayType>(E->getType())); |
| 1978 | mangleType(E->getType()); |
John McCall | 0512e48 | 2010-07-14 04:20:34 +0000 | [diff] [blame] | 1979 | Out << 'E'; |
| 1980 | break; |
| 1981 | } |
| 1982 | |
| 1983 | case Expr::GNUNullExprClass: |
| 1984 | // FIXME: should this really be mangled the same as nullptr? |
| 1985 | // fallthrough |
| 1986 | |
| 1987 | case Expr::CXXNullPtrLiteralExprClass: { |
| 1988 | // Proposal from David Vandervoorde, 2010.06.30, as |
| 1989 | // modified by ABI list discussion. |
| 1990 | Out << "LDnE"; |
| 1991 | break; |
| 1992 | } |
Anders Carlsson | e170ba7 | 2009-12-14 01:45:37 +0000 | [diff] [blame] | 1993 | |
Anders Carlsson | d553f8c | 2009-09-21 01:21:10 +0000 | [diff] [blame] | 1994 | } |
Douglas Gregor | 5f2bfd4 | 2009-02-13 00:10:09 +0000 | [diff] [blame] | 1995 | } |
| 1996 | |
Anders Carlsson | 3ac86b5 | 2009-04-15 05:36:58 +0000 | [diff] [blame] | 1997 | void CXXNameMangler::mangleCXXCtorType(CXXCtorType T) { |
| 1998 | // <ctor-dtor-name> ::= C1 # complete object constructor |
| 1999 | // ::= C2 # base object constructor |
| 2000 | // ::= C3 # complete object allocating constructor |
| 2001 | // |
| 2002 | switch (T) { |
| 2003 | case Ctor_Complete: |
| 2004 | Out << "C1"; |
| 2005 | break; |
| 2006 | case Ctor_Base: |
| 2007 | Out << "C2"; |
| 2008 | break; |
| 2009 | case Ctor_CompleteAllocating: |
| 2010 | Out << "C3"; |
| 2011 | break; |
| 2012 | } |
| 2013 | } |
| 2014 | |
Anders Carlsson | 27ae536 | 2009-04-17 01:58:57 +0000 | [diff] [blame] | 2015 | void CXXNameMangler::mangleCXXDtorType(CXXDtorType T) { |
| 2016 | // <ctor-dtor-name> ::= D0 # deleting destructor |
| 2017 | // ::= D1 # complete object destructor |
| 2018 | // ::= D2 # base object destructor |
| 2019 | // |
| 2020 | switch (T) { |
| 2021 | case Dtor_Deleting: |
| 2022 | Out << "D0"; |
| 2023 | break; |
| 2024 | case Dtor_Complete: |
| 2025 | Out << "D1"; |
| 2026 | break; |
| 2027 | case Dtor_Base: |
| 2028 | Out << "D2"; |
| 2029 | break; |
| 2030 | } |
| 2031 | } |
| 2032 | |
John McCall | 6dbce19 | 2010-08-20 00:17:19 +0000 | [diff] [blame] | 2033 | void CXXNameMangler::mangleTemplateArgs( |
| 2034 | const ExplicitTemplateArgumentList &TemplateArgs) { |
| 2035 | // <template-args> ::= I <template-arg>+ E |
| 2036 | Out << 'I'; |
| 2037 | for (unsigned I = 0, E = TemplateArgs.NumTemplateArgs; I != E; ++I) |
| 2038 | mangleTemplateArg(0, TemplateArgs.getTemplateArgs()[I].getArgument()); |
| 2039 | Out << 'E'; |
| 2040 | } |
| 2041 | |
Douglas Gregor | 20f0cc7 | 2010-04-23 03:10:43 +0000 | [diff] [blame] | 2042 | void CXXNameMangler::mangleTemplateArgs(TemplateName Template, |
| 2043 | const TemplateArgument *TemplateArgs, |
| 2044 | unsigned NumTemplateArgs) { |
| 2045 | if (TemplateDecl *TD = Template.getAsTemplateDecl()) |
| 2046 | return mangleTemplateArgs(*TD->getTemplateParameters(), TemplateArgs, |
| 2047 | NumTemplateArgs); |
Sean Hunt | c302113 | 2010-05-05 15:23:54 +0000 | [diff] [blame] | 2048 | |
Douglas Gregor | 20f0cc7 | 2010-04-23 03:10:43 +0000 | [diff] [blame] | 2049 | // <template-args> ::= I <template-arg>+ E |
| 2050 | Out << 'I'; |
| 2051 | for (unsigned i = 0; i != NumTemplateArgs; ++i) |
| 2052 | mangleTemplateArg(0, TemplateArgs[i]); |
| 2053 | Out << 'E'; |
| 2054 | } |
| 2055 | |
Rafael Espindola | d980072 | 2010-03-11 14:07:00 +0000 | [diff] [blame] | 2056 | void CXXNameMangler::mangleTemplateArgs(const TemplateParameterList &PL, |
| 2057 | const TemplateArgumentList &AL) { |
Anders Carlsson | 7a0ba87 | 2009-05-15 16:09:15 +0000 | [diff] [blame] | 2058 | // <template-args> ::= I <template-arg>+ E |
Benjamin Kramer | 35f59b6 | 2010-04-10 16:03:31 +0000 | [diff] [blame] | 2059 | Out << 'I'; |
Rafael Espindola | d980072 | 2010-03-11 14:07:00 +0000 | [diff] [blame] | 2060 | for (unsigned i = 0, e = AL.size(); i != e; ++i) |
| 2061 | mangleTemplateArg(PL.getParam(i), AL[i]); |
Benjamin Kramer | 35f59b6 | 2010-04-10 16:03:31 +0000 | [diff] [blame] | 2062 | Out << 'E'; |
Anders Carlsson | 7a0ba87 | 2009-05-15 16:09:15 +0000 | [diff] [blame] | 2063 | } |
| 2064 | |
Rafael Espindola | d980072 | 2010-03-11 14:07:00 +0000 | [diff] [blame] | 2065 | void CXXNameMangler::mangleTemplateArgs(const TemplateParameterList &PL, |
| 2066 | const TemplateArgument *TemplateArgs, |
Anders Carlsson | 7624f21 | 2009-09-18 02:42:01 +0000 | [diff] [blame] | 2067 | unsigned NumTemplateArgs) { |
| 2068 | // <template-args> ::= I <template-arg>+ E |
Benjamin Kramer | 35f59b6 | 2010-04-10 16:03:31 +0000 | [diff] [blame] | 2069 | Out << 'I'; |
Daniel Dunbar | 7e0c195 | 2009-11-21 09:17:15 +0000 | [diff] [blame] | 2070 | for (unsigned i = 0; i != NumTemplateArgs; ++i) |
Rafael Espindola | d980072 | 2010-03-11 14:07:00 +0000 | [diff] [blame] | 2071 | mangleTemplateArg(PL.getParam(i), TemplateArgs[i]); |
Benjamin Kramer | 35f59b6 | 2010-04-10 16:03:31 +0000 | [diff] [blame] | 2072 | Out << 'E'; |
Anders Carlsson | 7624f21 | 2009-09-18 02:42:01 +0000 | [diff] [blame] | 2073 | } |
| 2074 | |
Rafael Espindola | d980072 | 2010-03-11 14:07:00 +0000 | [diff] [blame] | 2075 | void CXXNameMangler::mangleTemplateArg(const NamedDecl *P, |
| 2076 | const TemplateArgument &A) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2077 | // <template-arg> ::= <type> # type or template |
Anders Carlsson | 7a0ba87 | 2009-05-15 16:09:15 +0000 | [diff] [blame] | 2078 | // ::= X <expression> E # expression |
| 2079 | // ::= <expr-primary> # simple expressions |
| 2080 | // ::= I <template-arg>* E # argument pack |
| 2081 | // ::= sp <expression> # pack expansion of (C++0x) |
| 2082 | switch (A.getKind()) { |
| 2083 | default: |
| 2084 | assert(0 && "Unknown template argument kind!"); |
| 2085 | case TemplateArgument::Type: |
| 2086 | mangleType(A.getAsType()); |
| 2087 | break; |
Anders Carlsson | 9e85c74 | 2009-12-23 19:30:55 +0000 | [diff] [blame] | 2088 | case TemplateArgument::Template: |
John McCall | b6f532e | 2010-07-14 06:43:17 +0000 | [diff] [blame] | 2089 | // This is mangled as <type>. |
| 2090 | mangleType(A.getAsTemplate()); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 2091 | break; |
Anders Carlsson | d553f8c | 2009-09-21 01:21:10 +0000 | [diff] [blame] | 2092 | case TemplateArgument::Expression: |
| 2093 | Out << 'X'; |
| 2094 | mangleExpression(A.getAsExpr()); |
| 2095 | Out << 'E'; |
| 2096 | break; |
Anders Carlsson | e170ba7 | 2009-12-14 01:45:37 +0000 | [diff] [blame] | 2097 | case TemplateArgument::Integral: |
| 2098 | mangleIntegerLiteral(A.getIntegralType(), *A.getAsIntegral()); |
Anders Carlsson | 7a0ba87 | 2009-05-15 16:09:15 +0000 | [diff] [blame] | 2099 | break; |
Daniel Dunbar | 7e0c195 | 2009-11-21 09:17:15 +0000 | [diff] [blame] | 2100 | case TemplateArgument::Declaration: { |
Douglas Gregor | 20f0cc7 | 2010-04-23 03:10:43 +0000 | [diff] [blame] | 2101 | assert(P && "Missing template parameter for declaration argument"); |
Daniel Dunbar | 7e0c195 | 2009-11-21 09:17:15 +0000 | [diff] [blame] | 2102 | // <expr-primary> ::= L <mangled-name> E # external name |
| 2103 | |
Rafael Espindola | d980072 | 2010-03-11 14:07:00 +0000 | [diff] [blame] | 2104 | // Clang produces AST's where pointer-to-member-function expressions |
Daniel Dunbar | 7e0c195 | 2009-11-21 09:17:15 +0000 | [diff] [blame] | 2105 | // and pointer-to-function expressions are represented as a declaration not |
Rafael Espindola | d980072 | 2010-03-11 14:07:00 +0000 | [diff] [blame] | 2106 | // an expression. We compensate for it here to produce the correct mangling. |
| 2107 | NamedDecl *D = cast<NamedDecl>(A.getAsDecl()); |
| 2108 | const NonTypeTemplateParmDecl *Parameter = cast<NonTypeTemplateParmDecl>(P); |
| 2109 | bool compensateMangling = D->isCXXClassMember() && |
| 2110 | !Parameter->getType()->isReferenceType(); |
| 2111 | if (compensateMangling) { |
| 2112 | Out << 'X'; |
| 2113 | mangleOperatorName(OO_Amp, 1); |
| 2114 | } |
| 2115 | |
Daniel Dunbar | 7e0c195 | 2009-11-21 09:17:15 +0000 | [diff] [blame] | 2116 | Out << 'L'; |
| 2117 | // References to external entities use the mangled name; if the name would |
| 2118 | // not normally be manged then mangle it as unqualified. |
| 2119 | // |
| 2120 | // FIXME: The ABI specifies that external names here should have _Z, but |
| 2121 | // gcc leaves this off. |
Rafael Espindola | d980072 | 2010-03-11 14:07:00 +0000 | [diff] [blame] | 2122 | if (compensateMangling) |
| 2123 | mangle(D, "_Z"); |
| 2124 | else |
| 2125 | mangle(D, "Z"); |
Daniel Dunbar | 7e0c195 | 2009-11-21 09:17:15 +0000 | [diff] [blame] | 2126 | Out << 'E'; |
Rafael Espindola | d980072 | 2010-03-11 14:07:00 +0000 | [diff] [blame] | 2127 | |
| 2128 | if (compensateMangling) |
| 2129 | Out << 'E'; |
| 2130 | |
Daniel Dunbar | 7e0c195 | 2009-11-21 09:17:15 +0000 | [diff] [blame] | 2131 | break; |
| 2132 | } |
| 2133 | } |
Anders Carlsson | 7a0ba87 | 2009-05-15 16:09:15 +0000 | [diff] [blame] | 2134 | } |
| 2135 | |
Anders Carlsson | 0ccdf8d | 2009-09-27 00:38:53 +0000 | [diff] [blame] | 2136 | void CXXNameMangler::mangleTemplateParameter(unsigned Index) { |
| 2137 | // <template-param> ::= T_ # first template parameter |
| 2138 | // ::= T <parameter-2 non-negative number> _ |
| 2139 | if (Index == 0) |
| 2140 | Out << "T_"; |
| 2141 | else |
| 2142 | Out << 'T' << (Index - 1) << '_'; |
| 2143 | } |
| 2144 | |
Anders Carlsson | 7696737 | 2009-09-17 00:43:46 +0000 | [diff] [blame] | 2145 | // <substitution> ::= S <seq-id> _ |
| 2146 | // ::= S_ |
Anders Carlsson | 6862fc7 | 2009-09-17 04:16:28 +0000 | [diff] [blame] | 2147 | bool CXXNameMangler::mangleSubstitution(const NamedDecl *ND) { |
Anders Carlsson | e7c8cb6 | 2009-09-26 20:53:44 +0000 | [diff] [blame] | 2148 | // Try one of the standard substitutions first. |
| 2149 | if (mangleStandardSubstitution(ND)) |
| 2150 | return true; |
Daniel Dunbar | 3c9e463 | 2009-11-21 09:05:47 +0000 | [diff] [blame] | 2151 | |
Anders Carlsson | 433d137 | 2009-11-07 04:26:04 +0000 | [diff] [blame] | 2152 | ND = cast<NamedDecl>(ND->getCanonicalDecl()); |
Anders Carlsson | 6862fc7 | 2009-09-17 04:16:28 +0000 | [diff] [blame] | 2153 | return mangleSubstitution(reinterpret_cast<uintptr_t>(ND)); |
| 2154 | } |
| 2155 | |
Anders Carlsson | 7696737 | 2009-09-17 00:43:46 +0000 | [diff] [blame] | 2156 | bool CXXNameMangler::mangleSubstitution(QualType T) { |
Anders Carlsson | d99edc4 | 2009-09-26 03:55:37 +0000 | [diff] [blame] | 2157 | if (!T.getCVRQualifiers()) { |
| 2158 | if (const RecordType *RT = T->getAs<RecordType>()) |
| 2159 | return mangleSubstitution(RT->getDecl()); |
| 2160 | } |
Daniel Dunbar | 3c9e463 | 2009-11-21 09:05:47 +0000 | [diff] [blame] | 2161 | |
Anders Carlsson | 7696737 | 2009-09-17 00:43:46 +0000 | [diff] [blame] | 2162 | uintptr_t TypePtr = reinterpret_cast<uintptr_t>(T.getAsOpaquePtr()); |
| 2163 | |
Anders Carlsson | d3a932a | 2009-09-17 03:53:28 +0000 | [diff] [blame] | 2164 | return mangleSubstitution(TypePtr); |
| 2165 | } |
| 2166 | |
Douglas Gregor | 1e9268e | 2010-04-28 05:58:56 +0000 | [diff] [blame] | 2167 | bool CXXNameMangler::mangleSubstitution(TemplateName Template) { |
| 2168 | if (TemplateDecl *TD = Template.getAsTemplateDecl()) |
| 2169 | return mangleSubstitution(TD); |
Sean Hunt | c302113 | 2010-05-05 15:23:54 +0000 | [diff] [blame] | 2170 | |
Douglas Gregor | 1e9268e | 2010-04-28 05:58:56 +0000 | [diff] [blame] | 2171 | Template = Context.getASTContext().getCanonicalTemplateName(Template); |
| 2172 | return mangleSubstitution( |
| 2173 | reinterpret_cast<uintptr_t>(Template.getAsVoidPointer())); |
| 2174 | } |
| 2175 | |
Anders Carlsson | d3a932a | 2009-09-17 03:53:28 +0000 | [diff] [blame] | 2176 | bool CXXNameMangler::mangleSubstitution(uintptr_t Ptr) { |
Benjamin Kramer | 35f59b6 | 2010-04-10 16:03:31 +0000 | [diff] [blame] | 2177 | llvm::DenseMap<uintptr_t, unsigned>::iterator I = Substitutions.find(Ptr); |
Anders Carlsson | 7696737 | 2009-09-17 00:43:46 +0000 | [diff] [blame] | 2178 | if (I == Substitutions.end()) |
| 2179 | return false; |
Daniel Dunbar | 3c9e463 | 2009-11-21 09:05:47 +0000 | [diff] [blame] | 2180 | |
Anders Carlsson | 7696737 | 2009-09-17 00:43:46 +0000 | [diff] [blame] | 2181 | unsigned SeqID = I->second; |
| 2182 | if (SeqID == 0) |
| 2183 | Out << "S_"; |
| 2184 | else { |
| 2185 | SeqID--; |
Daniel Dunbar | 3c9e463 | 2009-11-21 09:05:47 +0000 | [diff] [blame] | 2186 | |
Anders Carlsson | 7696737 | 2009-09-17 00:43:46 +0000 | [diff] [blame] | 2187 | // <seq-id> is encoded in base-36, using digits and upper case letters. |
| 2188 | char Buffer[10]; |
Benjamin Kramer | 35f59b6 | 2010-04-10 16:03:31 +0000 | [diff] [blame] | 2189 | char *BufferPtr = llvm::array_endof(Buffer); |
Daniel Dunbar | 3c9e463 | 2009-11-21 09:05:47 +0000 | [diff] [blame] | 2190 | |
Anders Carlsson | 7696737 | 2009-09-17 00:43:46 +0000 | [diff] [blame] | 2191 | if (SeqID == 0) *--BufferPtr = '0'; |
Daniel Dunbar | 3c9e463 | 2009-11-21 09:05:47 +0000 | [diff] [blame] | 2192 | |
Anders Carlsson | 7696737 | 2009-09-17 00:43:46 +0000 | [diff] [blame] | 2193 | while (SeqID) { |
| 2194 | assert(BufferPtr > Buffer && "Buffer overflow!"); |
Daniel Dunbar | 3c9e463 | 2009-11-21 09:05:47 +0000 | [diff] [blame] | 2195 | |
John McCall | 6ab30e0 | 2010-06-09 07:26:17 +0000 | [diff] [blame] | 2196 | char c = static_cast<char>(SeqID % 36); |
Daniel Dunbar | 3c9e463 | 2009-11-21 09:05:47 +0000 | [diff] [blame] | 2197 | |
Anders Carlsson | 7696737 | 2009-09-17 00:43:46 +0000 | [diff] [blame] | 2198 | *--BufferPtr = (c < 10 ? '0' + c : 'A' + c - 10); |
| 2199 | SeqID /= 36; |
| 2200 | } |
Daniel Dunbar | 3c9e463 | 2009-11-21 09:05:47 +0000 | [diff] [blame] | 2201 | |
Benjamin Kramer | 35f59b6 | 2010-04-10 16:03:31 +0000 | [diff] [blame] | 2202 | Out << 'S' |
| 2203 | << llvm::StringRef(BufferPtr, llvm::array_endof(Buffer)-BufferPtr) |
| 2204 | << '_'; |
Anders Carlsson | 7696737 | 2009-09-17 00:43:46 +0000 | [diff] [blame] | 2205 | } |
Daniel Dunbar | 3c9e463 | 2009-11-21 09:05:47 +0000 | [diff] [blame] | 2206 | |
Anders Carlsson | 7696737 | 2009-09-17 00:43:46 +0000 | [diff] [blame] | 2207 | return true; |
| 2208 | } |
| 2209 | |
Anders Carlsson | f514b54 | 2009-09-27 00:12:57 +0000 | [diff] [blame] | 2210 | static bool isCharType(QualType T) { |
| 2211 | if (T.isNull()) |
| 2212 | return false; |
Daniel Dunbar | 3c9e463 | 2009-11-21 09:05:47 +0000 | [diff] [blame] | 2213 | |
Anders Carlsson | f514b54 | 2009-09-27 00:12:57 +0000 | [diff] [blame] | 2214 | return T->isSpecificBuiltinType(BuiltinType::Char_S) || |
| 2215 | T->isSpecificBuiltinType(BuiltinType::Char_U); |
| 2216 | } |
| 2217 | |
Daniel Dunbar | 3c9e463 | 2009-11-21 09:05:47 +0000 | [diff] [blame] | 2218 | /// isCharSpecialization - Returns whether a given type is a template |
Anders Carlsson | f514b54 | 2009-09-27 00:12:57 +0000 | [diff] [blame] | 2219 | /// specialization of a given name with a single argument of type char. |
| 2220 | static bool isCharSpecialization(QualType T, const char *Name) { |
| 2221 | if (T.isNull()) |
| 2222 | return false; |
Daniel Dunbar | 3c9e463 | 2009-11-21 09:05:47 +0000 | [diff] [blame] | 2223 | |
Anders Carlsson | f514b54 | 2009-09-27 00:12:57 +0000 | [diff] [blame] | 2224 | const RecordType *RT = T->getAs<RecordType>(); |
| 2225 | if (!RT) |
| 2226 | return false; |
Daniel Dunbar | 3c9e463 | 2009-11-21 09:05:47 +0000 | [diff] [blame] | 2227 | |
| 2228 | const ClassTemplateSpecializationDecl *SD = |
Anders Carlsson | f514b54 | 2009-09-27 00:12:57 +0000 | [diff] [blame] | 2229 | dyn_cast<ClassTemplateSpecializationDecl>(RT->getDecl()); |
| 2230 | if (!SD) |
| 2231 | return false; |
| 2232 | |
| 2233 | if (!isStdNamespace(SD->getDeclContext())) |
| 2234 | return false; |
Daniel Dunbar | 3c9e463 | 2009-11-21 09:05:47 +0000 | [diff] [blame] | 2235 | |
Anders Carlsson | f514b54 | 2009-09-27 00:12:57 +0000 | [diff] [blame] | 2236 | const TemplateArgumentList &TemplateArgs = SD->getTemplateArgs(); |
| 2237 | if (TemplateArgs.size() != 1) |
| 2238 | return false; |
Daniel Dunbar | 3c9e463 | 2009-11-21 09:05:47 +0000 | [diff] [blame] | 2239 | |
Anders Carlsson | f514b54 | 2009-09-27 00:12:57 +0000 | [diff] [blame] | 2240 | if (!isCharType(TemplateArgs[0].getAsType())) |
| 2241 | return false; |
Daniel Dunbar | 3c9e463 | 2009-11-21 09:05:47 +0000 | [diff] [blame] | 2242 | |
Daniel Dunbar | 01eb9b9 | 2009-10-18 21:17:35 +0000 | [diff] [blame] | 2243 | return SD->getIdentifier()->getName() == Name; |
Anders Carlsson | f514b54 | 2009-09-27 00:12:57 +0000 | [diff] [blame] | 2244 | } |
| 2245 | |
Anders Carlsson | 91f8860 | 2009-12-07 19:56:42 +0000 | [diff] [blame] | 2246 | template <std::size_t StrLen> |
| 2247 | bool isStreamCharSpecialization(const ClassTemplateSpecializationDecl *SD, |
| 2248 | const char (&Str)[StrLen]) { |
| 2249 | if (!SD->getIdentifier()->isStr(Str)) |
| 2250 | return false; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 2251 | |
Anders Carlsson | 91f8860 | 2009-12-07 19:56:42 +0000 | [diff] [blame] | 2252 | const TemplateArgumentList &TemplateArgs = SD->getTemplateArgs(); |
| 2253 | if (TemplateArgs.size() != 2) |
| 2254 | return false; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 2255 | |
Anders Carlsson | 91f8860 | 2009-12-07 19:56:42 +0000 | [diff] [blame] | 2256 | if (!isCharType(TemplateArgs[0].getAsType())) |
| 2257 | return false; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 2258 | |
Anders Carlsson | 91f8860 | 2009-12-07 19:56:42 +0000 | [diff] [blame] | 2259 | if (!isCharSpecialization(TemplateArgs[1].getAsType(), "char_traits")) |
| 2260 | return false; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 2261 | |
Anders Carlsson | 91f8860 | 2009-12-07 19:56:42 +0000 | [diff] [blame] | 2262 | return true; |
| 2263 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 2264 | |
Anders Carlsson | e7c8cb6 | 2009-09-26 20:53:44 +0000 | [diff] [blame] | 2265 | bool CXXNameMangler::mangleStandardSubstitution(const NamedDecl *ND) { |
| 2266 | // <substitution> ::= St # ::std:: |
Anders Carlsson | 8c03155 | 2009-09-26 23:10:05 +0000 | [diff] [blame] | 2267 | if (const NamespaceDecl *NS = dyn_cast<NamespaceDecl>(ND)) { |
Anders Carlsson | 47846d2 | 2009-12-04 06:23:23 +0000 | [diff] [blame] | 2268 | if (isStd(NS)) { |
Anders Carlsson | 8c03155 | 2009-09-26 23:10:05 +0000 | [diff] [blame] | 2269 | Out << "St"; |
| 2270 | return true; |
| 2271 | } |
| 2272 | } |
| 2273 | |
| 2274 | if (const ClassTemplateDecl *TD = dyn_cast<ClassTemplateDecl>(ND)) { |
| 2275 | if (!isStdNamespace(TD->getDeclContext())) |
| 2276 | return false; |
Daniel Dunbar | 3c9e463 | 2009-11-21 09:05:47 +0000 | [diff] [blame] | 2277 | |
Anders Carlsson | 8c03155 | 2009-09-26 23:10:05 +0000 | [diff] [blame] | 2278 | // <substitution> ::= Sa # ::std::allocator |
| 2279 | if (TD->getIdentifier()->isStr("allocator")) { |
| 2280 | Out << "Sa"; |
| 2281 | return true; |
| 2282 | } |
Daniel Dunbar | 3c9e463 | 2009-11-21 09:05:47 +0000 | [diff] [blame] | 2283 | |
Anders Carlsson | 189d59c | 2009-09-26 23:14:39 +0000 | [diff] [blame] | 2284 | // <<substitution> ::= Sb # ::std::basic_string |
| 2285 | if (TD->getIdentifier()->isStr("basic_string")) { |
| 2286 | Out << "Sb"; |
| 2287 | return true; |
| 2288 | } |
Anders Carlsson | 8c03155 | 2009-09-26 23:10:05 +0000 | [diff] [blame] | 2289 | } |
Daniel Dunbar | 3c9e463 | 2009-11-21 09:05:47 +0000 | [diff] [blame] | 2290 | |
| 2291 | if (const ClassTemplateSpecializationDecl *SD = |
Anders Carlsson | f514b54 | 2009-09-27 00:12:57 +0000 | [diff] [blame] | 2292 | dyn_cast<ClassTemplateSpecializationDecl>(ND)) { |
Eli Friedman | 5370ee2 | 2010-02-23 18:25:09 +0000 | [diff] [blame] | 2293 | if (!isStdNamespace(SD->getDeclContext())) |
| 2294 | return false; |
| 2295 | |
Anders Carlsson | f514b54 | 2009-09-27 00:12:57 +0000 | [diff] [blame] | 2296 | // <substitution> ::= Ss # ::std::basic_string<char, |
| 2297 | // ::std::char_traits<char>, |
| 2298 | // ::std::allocator<char> > |
| 2299 | if (SD->getIdentifier()->isStr("basic_string")) { |
| 2300 | const TemplateArgumentList &TemplateArgs = SD->getTemplateArgs(); |
Daniel Dunbar | 3c9e463 | 2009-11-21 09:05:47 +0000 | [diff] [blame] | 2301 | |
Anders Carlsson | f514b54 | 2009-09-27 00:12:57 +0000 | [diff] [blame] | 2302 | if (TemplateArgs.size() != 3) |
| 2303 | return false; |
Daniel Dunbar | 3c9e463 | 2009-11-21 09:05:47 +0000 | [diff] [blame] | 2304 | |
Anders Carlsson | f514b54 | 2009-09-27 00:12:57 +0000 | [diff] [blame] | 2305 | if (!isCharType(TemplateArgs[0].getAsType())) |
| 2306 | return false; |
Daniel Dunbar | 3c9e463 | 2009-11-21 09:05:47 +0000 | [diff] [blame] | 2307 | |
Anders Carlsson | f514b54 | 2009-09-27 00:12:57 +0000 | [diff] [blame] | 2308 | if (!isCharSpecialization(TemplateArgs[1].getAsType(), "char_traits")) |
| 2309 | return false; |
Daniel Dunbar | 3c9e463 | 2009-11-21 09:05:47 +0000 | [diff] [blame] | 2310 | |
Anders Carlsson | f514b54 | 2009-09-27 00:12:57 +0000 | [diff] [blame] | 2311 | if (!isCharSpecialization(TemplateArgs[2].getAsType(), "allocator")) |
| 2312 | return false; |
| 2313 | |
| 2314 | Out << "Ss"; |
| 2315 | return true; |
| 2316 | } |
Daniel Dunbar | 3c9e463 | 2009-11-21 09:05:47 +0000 | [diff] [blame] | 2317 | |
Anders Carlsson | 91f8860 | 2009-12-07 19:56:42 +0000 | [diff] [blame] | 2318 | // <substitution> ::= Si # ::std::basic_istream<char, |
| 2319 | // ::std::char_traits<char> > |
| 2320 | if (isStreamCharSpecialization(SD, "basic_istream")) { |
| 2321 | Out << "Si"; |
| 2322 | return true; |
| 2323 | } |
| 2324 | |
Daniel Dunbar | 3c9e463 | 2009-11-21 09:05:47 +0000 | [diff] [blame] | 2325 | // <substitution> ::= So # ::std::basic_ostream<char, |
Anders Carlsson | 8f8fd8e | 2009-10-08 17:20:26 +0000 | [diff] [blame] | 2326 | // ::std::char_traits<char> > |
Anders Carlsson | 91f8860 | 2009-12-07 19:56:42 +0000 | [diff] [blame] | 2327 | if (isStreamCharSpecialization(SD, "basic_ostream")) { |
Anders Carlsson | 8f8fd8e | 2009-10-08 17:20:26 +0000 | [diff] [blame] | 2328 | Out << "So"; |
| 2329 | return true; |
| 2330 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 2331 | |
Anders Carlsson | 91f8860 | 2009-12-07 19:56:42 +0000 | [diff] [blame] | 2332 | // <substitution> ::= Sd # ::std::basic_iostream<char, |
| 2333 | // ::std::char_traits<char> > |
| 2334 | if (isStreamCharSpecialization(SD, "basic_iostream")) { |
| 2335 | Out << "Sd"; |
| 2336 | return true; |
| 2337 | } |
Anders Carlsson | f514b54 | 2009-09-27 00:12:57 +0000 | [diff] [blame] | 2338 | } |
Anders Carlsson | 8c03155 | 2009-09-26 23:10:05 +0000 | [diff] [blame] | 2339 | return false; |
Anders Carlsson | e7c8cb6 | 2009-09-26 20:53:44 +0000 | [diff] [blame] | 2340 | } |
| 2341 | |
Anders Carlsson | 7696737 | 2009-09-17 00:43:46 +0000 | [diff] [blame] | 2342 | void CXXNameMangler::addSubstitution(QualType T) { |
Anders Carlsson | d99edc4 | 2009-09-26 03:55:37 +0000 | [diff] [blame] | 2343 | if (!T.getCVRQualifiers()) { |
| 2344 | if (const RecordType *RT = T->getAs<RecordType>()) { |
| 2345 | addSubstitution(RT->getDecl()); |
| 2346 | return; |
| 2347 | } |
| 2348 | } |
Daniel Dunbar | 3c9e463 | 2009-11-21 09:05:47 +0000 | [diff] [blame] | 2349 | |
Anders Carlsson | 7696737 | 2009-09-17 00:43:46 +0000 | [diff] [blame] | 2350 | uintptr_t TypePtr = reinterpret_cast<uintptr_t>(T.getAsOpaquePtr()); |
Anders Carlsson | d3a932a | 2009-09-17 03:53:28 +0000 | [diff] [blame] | 2351 | addSubstitution(TypePtr); |
| 2352 | } |
| 2353 | |
Douglas Gregor | 1e9268e | 2010-04-28 05:58:56 +0000 | [diff] [blame] | 2354 | void CXXNameMangler::addSubstitution(TemplateName Template) { |
| 2355 | if (TemplateDecl *TD = Template.getAsTemplateDecl()) |
| 2356 | return addSubstitution(TD); |
Sean Hunt | c302113 | 2010-05-05 15:23:54 +0000 | [diff] [blame] | 2357 | |
Douglas Gregor | 1e9268e | 2010-04-28 05:58:56 +0000 | [diff] [blame] | 2358 | Template = Context.getASTContext().getCanonicalTemplateName(Template); |
| 2359 | addSubstitution(reinterpret_cast<uintptr_t>(Template.getAsVoidPointer())); |
| 2360 | } |
| 2361 | |
Anders Carlsson | d3a932a | 2009-09-17 03:53:28 +0000 | [diff] [blame] | 2362 | void CXXNameMangler::addSubstitution(uintptr_t Ptr) { |
Anders Carlsson | d3a932a | 2009-09-17 03:53:28 +0000 | [diff] [blame] | 2363 | assert(!Substitutions.count(Ptr) && "Substitution already exists!"); |
Anders Carlsson | 9d85b72 | 2010-06-02 04:29:50 +0000 | [diff] [blame] | 2364 | Substitutions[Ptr] = SeqID++; |
Anders Carlsson | 7696737 | 2009-09-17 00:43:46 +0000 | [diff] [blame] | 2365 | } |
| 2366 | |
Daniel Dunbar | 1b07711 | 2009-11-21 09:06:10 +0000 | [diff] [blame] | 2367 | // |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2368 | |
Daniel Dunbar | 1b07711 | 2009-11-21 09:06:10 +0000 | [diff] [blame] | 2369 | /// \brief Mangles the name of the declaration D and emits that name to the |
| 2370 | /// given output stream. |
| 2371 | /// |
| 2372 | /// If the declaration D requires a mangled name, this routine will emit that |
| 2373 | /// mangled name to \p os and return true. Otherwise, \p os will be unchanged |
| 2374 | /// and this routine will return false. In this case, the caller should just |
| 2375 | /// emit the identifier of the declaration (\c D->getIdentifier()) as its |
| 2376 | /// name. |
Daniel Dunbar | f981bf8 | 2009-11-21 09:14:52 +0000 | [diff] [blame] | 2377 | void MangleContext::mangleName(const NamedDecl *D, |
Daniel Dunbar | 94fd26d | 2009-11-21 09:06:22 +0000 | [diff] [blame] | 2378 | llvm::SmallVectorImpl<char> &Res) { |
Daniel Dunbar | c02ab4c | 2009-11-21 09:14:44 +0000 | [diff] [blame] | 2379 | assert((isa<FunctionDecl>(D) || isa<VarDecl>(D)) && |
| 2380 | "Invalid mangleName() call, argument is not a variable or function!"); |
| 2381 | assert(!isa<CXXConstructorDecl>(D) && !isa<CXXDestructorDecl>(D) && |
| 2382 | "Invalid mangleName() call on 'structor decl!"); |
Daniel Dunbar | 3c9e463 | 2009-11-21 09:05:47 +0000 | [diff] [blame] | 2383 | |
Daniel Dunbar | 1b07711 | 2009-11-21 09:06:10 +0000 | [diff] [blame] | 2384 | PrettyStackTraceDecl CrashInfo(D, SourceLocation(), |
| 2385 | getASTContext().getSourceManager(), |
| 2386 | "Mangling declaration"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2387 | |
Daniel Dunbar | 94fd26d | 2009-11-21 09:06:22 +0000 | [diff] [blame] | 2388 | CXXNameMangler Mangler(*this, Res); |
| 2389 | return Mangler.mangle(D); |
Daniel Dunbar | 1b07711 | 2009-11-21 09:06:10 +0000 | [diff] [blame] | 2390 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2391 | |
Daniel Dunbar | 1b07711 | 2009-11-21 09:06:10 +0000 | [diff] [blame] | 2392 | void MangleContext::mangleCXXCtor(const CXXConstructorDecl *D, CXXCtorType Type, |
Daniel Dunbar | 94fd26d | 2009-11-21 09:06:22 +0000 | [diff] [blame] | 2393 | llvm::SmallVectorImpl<char> &Res) { |
Daniel Dunbar | 77939c9 | 2009-11-21 09:06:31 +0000 | [diff] [blame] | 2394 | CXXNameMangler Mangler(*this, Res, D, Type); |
| 2395 | Mangler.mangle(D); |
Daniel Dunbar | 1b07711 | 2009-11-21 09:06:10 +0000 | [diff] [blame] | 2396 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2397 | |
Daniel Dunbar | 1b07711 | 2009-11-21 09:06:10 +0000 | [diff] [blame] | 2398 | void MangleContext::mangleCXXDtor(const CXXDestructorDecl *D, CXXDtorType Type, |
Daniel Dunbar | 94fd26d | 2009-11-21 09:06:22 +0000 | [diff] [blame] | 2399 | llvm::SmallVectorImpl<char> &Res) { |
Daniel Dunbar | 77939c9 | 2009-11-21 09:06:31 +0000 | [diff] [blame] | 2400 | CXXNameMangler Mangler(*this, Res, D, Type); |
| 2401 | Mangler.mangle(D); |
Daniel Dunbar | 1b07711 | 2009-11-21 09:06:10 +0000 | [diff] [blame] | 2402 | } |
Mike Stump | f121677 | 2009-07-31 18:25:34 +0000 | [diff] [blame] | 2403 | |
Fariborz Jahanian | 564360b | 2010-06-24 00:08:06 +0000 | [diff] [blame] | 2404 | void MangleContext::mangleBlock(GlobalDecl GD, const BlockDecl *BD, |
Douglas Gregor | 35415f5 | 2010-05-25 17:04:15 +0000 | [diff] [blame] | 2405 | llvm::SmallVectorImpl<char> &Res) { |
Charles Davis | 685b1d9 | 2010-05-26 18:25:27 +0000 | [diff] [blame] | 2406 | MiscNameMangler Mangler(*this, Res); |
Fariborz Jahanian | 564360b | 2010-06-24 00:08:06 +0000 | [diff] [blame] | 2407 | Mangler.mangleBlock(GD, BD); |
Douglas Gregor | 35415f5 | 2010-05-25 17:04:15 +0000 | [diff] [blame] | 2408 | } |
| 2409 | |
Anders Carlsson | 19879c9 | 2010-03-23 17:17:29 +0000 | [diff] [blame] | 2410 | void MangleContext::mangleThunk(const CXXMethodDecl *MD, |
| 2411 | const ThunkInfo &Thunk, |
| 2412 | llvm::SmallVectorImpl<char> &Res) { |
| 2413 | // <special-name> ::= T <call-offset> <base encoding> |
| 2414 | // # base is the nominal target function of thunk |
| 2415 | // <special-name> ::= Tc <call-offset> <call-offset> <base encoding> |
| 2416 | // # base is the nominal target function of thunk |
| 2417 | // # first call-offset is 'this' adjustment |
| 2418 | // # second call-offset is result adjustment |
Sean Hunt | c302113 | 2010-05-05 15:23:54 +0000 | [diff] [blame] | 2419 | |
Anders Carlsson | 19879c9 | 2010-03-23 17:17:29 +0000 | [diff] [blame] | 2420 | assert(!isa<CXXDestructorDecl>(MD) && |
| 2421 | "Use mangleCXXDtor for destructor decls!"); |
Sean Hunt | c302113 | 2010-05-05 15:23:54 +0000 | [diff] [blame] | 2422 | |
Anders Carlsson | 19879c9 | 2010-03-23 17:17:29 +0000 | [diff] [blame] | 2423 | CXXNameMangler Mangler(*this, Res); |
| 2424 | Mangler.getStream() << "_ZT"; |
| 2425 | if (!Thunk.Return.isEmpty()) |
| 2426 | Mangler.getStream() << 'c'; |
Sean Hunt | c302113 | 2010-05-05 15:23:54 +0000 | [diff] [blame] | 2427 | |
Anders Carlsson | 19879c9 | 2010-03-23 17:17:29 +0000 | [diff] [blame] | 2428 | // Mangle the 'this' pointer adjustment. |
| 2429 | Mangler.mangleCallOffset(Thunk.This.NonVirtual, Thunk.This.VCallOffsetOffset); |
Sean Hunt | c302113 | 2010-05-05 15:23:54 +0000 | [diff] [blame] | 2430 | |
Anders Carlsson | 19879c9 | 2010-03-23 17:17:29 +0000 | [diff] [blame] | 2431 | // Mangle the return pointer adjustment if there is one. |
| 2432 | if (!Thunk.Return.isEmpty()) |
| 2433 | Mangler.mangleCallOffset(Thunk.Return.NonVirtual, |
| 2434 | Thunk.Return.VBaseOffsetOffset); |
Sean Hunt | c302113 | 2010-05-05 15:23:54 +0000 | [diff] [blame] | 2435 | |
Anders Carlsson | 19879c9 | 2010-03-23 17:17:29 +0000 | [diff] [blame] | 2436 | Mangler.mangleFunctionEncoding(MD); |
| 2437 | } |
| 2438 | |
Sean Hunt | c302113 | 2010-05-05 15:23:54 +0000 | [diff] [blame] | 2439 | void |
Anders Carlsson | 19879c9 | 2010-03-23 17:17:29 +0000 | [diff] [blame] | 2440 | MangleContext::mangleCXXDtorThunk(const CXXDestructorDecl *DD, CXXDtorType Type, |
| 2441 | const ThisAdjustment &ThisAdjustment, |
| 2442 | llvm::SmallVectorImpl<char> &Res) { |
| 2443 | // <special-name> ::= T <call-offset> <base encoding> |
| 2444 | // # base is the nominal target function of thunk |
Sean Hunt | c302113 | 2010-05-05 15:23:54 +0000 | [diff] [blame] | 2445 | |
Anders Carlsson | 19879c9 | 2010-03-23 17:17:29 +0000 | [diff] [blame] | 2446 | CXXNameMangler Mangler(*this, Res, DD, Type); |
| 2447 | Mangler.getStream() << "_ZT"; |
| 2448 | |
| 2449 | // Mangle the 'this' pointer adjustment. |
Sean Hunt | c302113 | 2010-05-05 15:23:54 +0000 | [diff] [blame] | 2450 | Mangler.mangleCallOffset(ThisAdjustment.NonVirtual, |
Anders Carlsson | 19879c9 | 2010-03-23 17:17:29 +0000 | [diff] [blame] | 2451 | ThisAdjustment.VCallOffsetOffset); |
| 2452 | |
| 2453 | Mangler.mangleFunctionEncoding(DD); |
| 2454 | } |
| 2455 | |
Daniel Dunbar | c074771 | 2009-11-21 09:12:13 +0000 | [diff] [blame] | 2456 | /// mangleGuardVariable - Returns the mangled name for a guard variable |
| 2457 | /// for the passed in VarDecl. |
| 2458 | void MangleContext::mangleGuardVariable(const VarDecl *D, |
| 2459 | llvm::SmallVectorImpl<char> &Res) { |
| 2460 | // <special-name> ::= GV <object name> # Guard variable for one-time |
| 2461 | // # initialization |
| 2462 | CXXNameMangler Mangler(*this, Res); |
| 2463 | Mangler.getStream() << "_ZGV"; |
| 2464 | Mangler.mangleName(D); |
| 2465 | } |
| 2466 | |
Anders Carlsson | 715edf2 | 2010-06-26 16:09:40 +0000 | [diff] [blame] | 2467 | void MangleContext::mangleReferenceTemporary(const VarDecl *D, |
| 2468 | llvm::SmallVectorImpl<char> &Res) { |
| 2469 | // We match the GCC mangling here. |
| 2470 | // <special-name> ::= GR <object name> |
| 2471 | CXXNameMangler Mangler(*this, Res); |
| 2472 | Mangler.getStream() << "_ZGR"; |
| 2473 | Mangler.mangleName(D); |
| 2474 | } |
| 2475 | |
Anders Carlsson | 046c294 | 2010-04-17 20:15:18 +0000 | [diff] [blame] | 2476 | void MangleContext::mangleCXXVTable(const CXXRecordDecl *RD, |
Daniel Dunbar | 94fd26d | 2009-11-21 09:06:22 +0000 | [diff] [blame] | 2477 | llvm::SmallVectorImpl<char> &Res) { |
Daniel Dunbar | c074771 | 2009-11-21 09:12:13 +0000 | [diff] [blame] | 2478 | // <special-name> ::= TV <type> # virtual table |
Daniel Dunbar | 94fd26d | 2009-11-21 09:06:22 +0000 | [diff] [blame] | 2479 | CXXNameMangler Mangler(*this, Res); |
Daniel Dunbar | c074771 | 2009-11-21 09:12:13 +0000 | [diff] [blame] | 2480 | Mangler.getStream() << "_ZTV"; |
Douglas Gregor | 1b12a3b | 2010-05-26 05:11:13 +0000 | [diff] [blame] | 2481 | Mangler.mangleNameOrStandardSubstitution(RD); |
Daniel Dunbar | 1b07711 | 2009-11-21 09:06:10 +0000 | [diff] [blame] | 2482 | } |
Mike Stump | 82d75b0 | 2009-11-10 01:58:37 +0000 | [diff] [blame] | 2483 | |
Daniel Dunbar | 1b07711 | 2009-11-21 09:06:10 +0000 | [diff] [blame] | 2484 | void MangleContext::mangleCXXVTT(const CXXRecordDecl *RD, |
Daniel Dunbar | 94fd26d | 2009-11-21 09:06:22 +0000 | [diff] [blame] | 2485 | llvm::SmallVectorImpl<char> &Res) { |
Daniel Dunbar | c074771 | 2009-11-21 09:12:13 +0000 | [diff] [blame] | 2486 | // <special-name> ::= TT <type> # VTT structure |
Daniel Dunbar | 94fd26d | 2009-11-21 09:06:22 +0000 | [diff] [blame] | 2487 | CXXNameMangler Mangler(*this, Res); |
Daniel Dunbar | c074771 | 2009-11-21 09:12:13 +0000 | [diff] [blame] | 2488 | Mangler.getStream() << "_ZTT"; |
Douglas Gregor | 1b12a3b | 2010-05-26 05:11:13 +0000 | [diff] [blame] | 2489 | Mangler.mangleNameOrStandardSubstitution(RD); |
Daniel Dunbar | 1b07711 | 2009-11-21 09:06:10 +0000 | [diff] [blame] | 2490 | } |
Mike Stump | ab3f7e9 | 2009-11-10 01:41:59 +0000 | [diff] [blame] | 2491 | |
Anders Carlsson | 046c294 | 2010-04-17 20:15:18 +0000 | [diff] [blame] | 2492 | void MangleContext::mangleCXXCtorVTable(const CXXRecordDecl *RD, int64_t Offset, |
Daniel Dunbar | 1b07711 | 2009-11-21 09:06:10 +0000 | [diff] [blame] | 2493 | const CXXRecordDecl *Type, |
Daniel Dunbar | 94fd26d | 2009-11-21 09:06:22 +0000 | [diff] [blame] | 2494 | llvm::SmallVectorImpl<char> &Res) { |
Daniel Dunbar | c074771 | 2009-11-21 09:12:13 +0000 | [diff] [blame] | 2495 | // <special-name> ::= TC <type> <offset number> _ <base type> |
Daniel Dunbar | 94fd26d | 2009-11-21 09:06:22 +0000 | [diff] [blame] | 2496 | CXXNameMangler Mangler(*this, Res); |
Daniel Dunbar | c074771 | 2009-11-21 09:12:13 +0000 | [diff] [blame] | 2497 | Mangler.getStream() << "_ZTC"; |
Douglas Gregor | 1b12a3b | 2010-05-26 05:11:13 +0000 | [diff] [blame] | 2498 | Mangler.mangleNameOrStandardSubstitution(RD); |
Daniel Dunbar | c074771 | 2009-11-21 09:12:13 +0000 | [diff] [blame] | 2499 | Mangler.getStream() << Offset; |
Benjamin Kramer | 35f59b6 | 2010-04-10 16:03:31 +0000 | [diff] [blame] | 2500 | Mangler.getStream() << '_'; |
Douglas Gregor | 1b12a3b | 2010-05-26 05:11:13 +0000 | [diff] [blame] | 2501 | Mangler.mangleNameOrStandardSubstitution(Type); |
Daniel Dunbar | 1b07711 | 2009-11-21 09:06:10 +0000 | [diff] [blame] | 2502 | } |
Mike Stump | 738f8c2 | 2009-07-31 23:15:31 +0000 | [diff] [blame] | 2503 | |
Mike Stump | de05057 | 2009-12-02 18:57:08 +0000 | [diff] [blame] | 2504 | void MangleContext::mangleCXXRTTI(QualType Ty, |
Daniel Dunbar | 94fd26d | 2009-11-21 09:06:22 +0000 | [diff] [blame] | 2505 | llvm::SmallVectorImpl<char> &Res) { |
Daniel Dunbar | c074771 | 2009-11-21 09:12:13 +0000 | [diff] [blame] | 2506 | // <special-name> ::= TI <type> # typeinfo structure |
Douglas Gregor | 154fe98 | 2009-12-23 22:04:40 +0000 | [diff] [blame] | 2507 | assert(!Ty.hasQualifiers() && "RTTI info cannot have top-level qualifiers"); |
Daniel Dunbar | 94fd26d | 2009-11-21 09:06:22 +0000 | [diff] [blame] | 2508 | CXXNameMangler Mangler(*this, Res); |
Daniel Dunbar | c074771 | 2009-11-21 09:12:13 +0000 | [diff] [blame] | 2509 | Mangler.getStream() << "_ZTI"; |
| 2510 | Mangler.mangleType(Ty); |
Daniel Dunbar | 1b07711 | 2009-11-21 09:06:10 +0000 | [diff] [blame] | 2511 | } |
Mike Stump | 6779598 | 2009-11-14 00:14:13 +0000 | [diff] [blame] | 2512 | |
Mike Stump | de05057 | 2009-12-02 18:57:08 +0000 | [diff] [blame] | 2513 | void MangleContext::mangleCXXRTTIName(QualType Ty, |
Daniel Dunbar | 94fd26d | 2009-11-21 09:06:22 +0000 | [diff] [blame] | 2514 | llvm::SmallVectorImpl<char> &Res) { |
Daniel Dunbar | c074771 | 2009-11-21 09:12:13 +0000 | [diff] [blame] | 2515 | // <special-name> ::= TS <type> # typeinfo name (null terminated byte string) |
Daniel Dunbar | 94fd26d | 2009-11-21 09:06:22 +0000 | [diff] [blame] | 2516 | CXXNameMangler Mangler(*this, Res); |
Daniel Dunbar | c074771 | 2009-11-21 09:12:13 +0000 | [diff] [blame] | 2517 | Mangler.getStream() << "_ZTS"; |
| 2518 | Mangler.mangleType(Ty); |
Mike Stump | f121677 | 2009-07-31 18:25:34 +0000 | [diff] [blame] | 2519 | } |