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