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