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