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