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