blob: c460929c461d754f2964da63e0bd4688126b58eb [file] [log] [blame]
Peter Collingbourne14110472011-01-13 18:57:25 +00001//===--- ItaniumMangle.cpp - Itanium C++ Name Mangling ----------*- C++ -*-===//
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00002//
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 Collingbourne14110472011-01-13 18:57:25 +000017#include "clang/AST/Mangle.h"
Douglas Gregor5f2bfd42009-02-13 00:10:09 +000018#include "clang/AST/ASTContext.h"
19#include "clang/AST/Decl.h"
20#include "clang/AST/DeclCXX.h"
Anders Carlssona40c5e42009-03-07 22:03:21 +000021#include "clang/AST/DeclObjC.h"
Anders Carlsson7a0ba872009-05-15 16:09:15 +000022#include "clang/AST/DeclTemplate.h"
Anders Carlsson50755b02009-09-27 20:11:34 +000023#include "clang/AST/ExprCXX.h"
John McCallfb44de92011-05-01 22:35:37 +000024#include "clang/AST/TypeLoc.h"
Peter Collingbourne14110472011-01-13 18:57:25 +000025#include "clang/Basic/ABI.h"
Douglas Gregor6ec36682009-02-18 23:53:56 +000026#include "clang/Basic/SourceManager.h"
Rafael Espindola4e274e92011-02-15 22:23:51 +000027#include "clang/Basic/TargetInfo.h"
Anders Carlssonc4355b62009-10-07 01:45:02 +000028#include "llvm/ADT/StringExtras.h"
Douglas Gregor5f2bfd42009-02-13 00:10:09 +000029#include "llvm/Support/raw_ostream.h"
John McCallefe6aee2009-09-05 07:56:18 +000030#include "llvm/Support/ErrorHandling.h"
Anders Carlssonf98574b2010-02-05 07:31:37 +000031
32#define MANGLE_CHECKER 0
33
34#if MANGLE_CHECKER
35#include <cxxabi.h>
36#endif
37
Douglas Gregor5f2bfd42009-02-13 00:10:09 +000038using namespace clang;
Charles Davis685b1d92010-05-26 18:25:27 +000039
Douglas Gregor5f2bfd42009-02-13 00:10:09 +000040namespace {
Fariborz Jahanian57058532010-03-03 19:41:08 +000041
John McCall82b7d7b2010-10-18 21:28:44 +000042static 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 Jahanian57058532010-03-03 19:41:08 +000050 }
51 return 0;
52}
53
John McCallfb44de92011-05-01 22:35:37 +000054static const FunctionDecl *getStructor(const FunctionDecl *fn) {
55 if (const FunctionTemplateDecl *ftd = fn->getPrimaryTemplate())
56 return ftd->getTemplatedDecl();
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +000057
John McCallfb44de92011-05-01 22:35:37 +000058 return fn;
59}
Anders Carlsson7e120032009-11-24 05:36:32 +000060
John McCallfb44de92011-05-01 22:35:37 +000061static const NamedDecl *getStructor(const NamedDecl *decl) {
62 const FunctionDecl *fn = dyn_cast_or_null<FunctionDecl>(decl);
63 return (fn ? getStructor(fn) : decl);
Anders Carlsson7e120032009-11-24 05:36:32 +000064}
John McCall1dd73832010-02-04 01:42:13 +000065
66static const unsigned UnknownArity = ~0U;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +000067
Peter Collingbourne14110472011-01-13 18:57:25 +000068class ItaniumMangleContext : public MangleContext {
69 llvm::DenseMap<const TagDecl *, uint64_t> AnonStructIds;
70 unsigned Discriminator;
71 llvm::DenseMap<const NamedDecl*, unsigned> Uniquifier;
72
73public:
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 Espindola0e376a02011-02-11 01:41:00 +000094 void mangleName(const NamedDecl *D, llvm::raw_ostream &);
Peter Collingbourne14110472011-01-13 18:57:25 +000095 void mangleThunk(const CXXMethodDecl *MD,
96 const ThunkInfo &Thunk,
Rafael Espindolaf0be9792011-02-11 02:52:17 +000097 llvm::raw_ostream &);
Peter Collingbourne14110472011-01-13 18:57:25 +000098 void mangleCXXDtorThunk(const CXXDestructorDecl *DD, CXXDtorType Type,
99 const ThisAdjustment &ThisAdjustment,
Rafael Espindolaf0be9792011-02-11 02:52:17 +0000100 llvm::raw_ostream &);
Peter Collingbourne14110472011-01-13 18:57:25 +0000101 void mangleReferenceTemporary(const VarDecl *D,
Rafael Espindolaf0be9792011-02-11 02:52:17 +0000102 llvm::raw_ostream &);
Peter Collingbourne14110472011-01-13 18:57:25 +0000103 void mangleCXXVTable(const CXXRecordDecl *RD,
Rafael Espindolaf0be9792011-02-11 02:52:17 +0000104 llvm::raw_ostream &);
Peter Collingbourne14110472011-01-13 18:57:25 +0000105 void mangleCXXVTT(const CXXRecordDecl *RD,
Rafael Espindolaf0be9792011-02-11 02:52:17 +0000106 llvm::raw_ostream &);
Peter Collingbourne14110472011-01-13 18:57:25 +0000107 void mangleCXXCtorVTable(const CXXRecordDecl *RD, int64_t Offset,
108 const CXXRecordDecl *Type,
Rafael Espindolaf0be9792011-02-11 02:52:17 +0000109 llvm::raw_ostream &);
110 void mangleCXXRTTI(QualType T, llvm::raw_ostream &);
111 void mangleCXXRTTIName(QualType T, llvm::raw_ostream &);
Peter Collingbourne14110472011-01-13 18:57:25 +0000112 void mangleCXXCtor(const CXXConstructorDecl *D, CXXCtorType Type,
Rafael Espindola0e376a02011-02-11 01:41:00 +0000113 llvm::raw_ostream &);
Peter Collingbourne14110472011-01-13 18:57:25 +0000114 void mangleCXXDtor(const CXXDestructorDecl *D, CXXDtorType Type,
Rafael Espindola0e376a02011-02-11 01:41:00 +0000115 llvm::raw_ostream &);
Peter Collingbourne14110472011-01-13 18:57:25 +0000116
Rafael Espindolaf0be9792011-02-11 02:52:17 +0000117 void mangleItaniumGuardVariable(const VarDecl *D, llvm::raw_ostream &);
Peter Collingbourne14110472011-01-13 18:57:25 +0000118
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 Dunbar1b077112009-11-21 09:06:10 +0000135/// CXXNameMangler - Manage the mangling of a single name.
Daniel Dunbarc0747712009-11-21 09:12:13 +0000136class CXXNameMangler {
Peter Collingbourne14110472011-01-13 18:57:25 +0000137 ItaniumMangleContext &Context;
Rafael Espindola0e376a02011-02-11 01:41:00 +0000138 llvm::raw_ostream &Out;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000139
John McCallfb44de92011-05-01 22:35:37 +0000140 /// 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 Dunbar1b077112009-11-21 09:06:10 +0000144 unsigned StructorType;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000145
Anders Carlsson9d85b722010-06-02 04:29:50 +0000146 /// SeqID - The next subsitution sequence number.
147 unsigned SeqID;
148
John McCallfb44de92011-05-01 22:35:37 +0000149 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 Dunbar1b077112009-11-21 09:06:10 +0000188 llvm::DenseMap<uintptr_t, unsigned> Substitutions;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000189
John McCall1dd73832010-02-04 01:42:13 +0000190 ASTContext &getASTContext() const { return Context.getASTContext(); }
191
Daniel Dunbarc0747712009-11-21 09:12:13 +0000192public:
John McCallfb44de92011-05-01 22:35:37 +0000193 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 Espindola0e376a02011-02-11 01:41:00 +0000201 CXXNameMangler(ItaniumMangleContext &C, llvm::raw_ostream &Out_,
Daniel Dunbar77939c92009-11-21 09:06:31 +0000202 const CXXConstructorDecl *D, CXXCtorType Type)
Rafael Espindolac4850c22011-02-10 23:59:36 +0000203 : Context(C), Out(Out_), Structor(getStructor(D)), StructorType(Type),
John McCallfb44de92011-05-01 22:35:37 +0000204 SeqID(0) { }
Rafael Espindola0e376a02011-02-11 01:41:00 +0000205 CXXNameMangler(ItaniumMangleContext &C, llvm::raw_ostream &Out_,
Daniel Dunbar77939c92009-11-21 09:06:31 +0000206 const CXXDestructorDecl *D, CXXDtorType Type)
Rafael Espindolac4850c22011-02-10 23:59:36 +0000207 : Context(C), Out(Out_), Structor(getStructor(D)), StructorType(Type),
John McCallfb44de92011-05-01 22:35:37 +0000208 SeqID(0) { }
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000209
Anders Carlssonf98574b2010-02-05 07:31:37 +0000210#if MANGLE_CHECKER
211 ~CXXNameMangler() {
212 if (Out.str()[0] == '\01')
213 return;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000214
Anders Carlssonf98574b2010-02-05 07:31:37 +0000215 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 Espindola0e376a02011-02-11 01:41:00 +0000221 llvm::raw_ostream &getStream() { return Out; }
Daniel Dunbarc0747712009-11-21 09:12:13 +0000222
Daniel Dunbar7e0c1952009-11-21 09:17:15 +0000223 void mangle(const NamedDecl *D, llvm::StringRef Prefix = "_Z");
Anders Carlsson19879c92010-03-23 17:17:29 +0000224 void mangleCallOffset(int64_t NonVirtual, int64_t Virtual);
John McCall0512e482010-07-14 04:20:34 +0000225 void mangleNumber(const llvm::APSInt &I);
Anders Carlssona94822e2009-11-26 02:32:05 +0000226 void mangleNumber(int64_t Number);
John McCall0512e482010-07-14 04:20:34 +0000227 void mangleFloat(const llvm::APFloat &F);
Daniel Dunbarc0747712009-11-21 09:12:13 +0000228 void mangleFunctionEncoding(const FunctionDecl *FD);
229 void mangleName(const NamedDecl *ND);
230 void mangleType(QualType T);
Douglas Gregor1b12a3b2010-05-26 05:11:13 +0000231 void mangleNameOrStandardSubstitution(const NamedDecl *ND);
232
Daniel Dunbarc0747712009-11-21 09:12:13 +0000233private:
Daniel Dunbar1b077112009-11-21 09:06:10 +0000234 bool mangleSubstitution(const NamedDecl *ND);
235 bool mangleSubstitution(QualType T);
Douglas Gregor1e9268e2010-04-28 05:58:56 +0000236 bool mangleSubstitution(TemplateName Template);
Daniel Dunbar1b077112009-11-21 09:06:10 +0000237 bool mangleSubstitution(uintptr_t Ptr);
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000238
Daniel Dunbar1b077112009-11-21 09:06:10 +0000239 bool mangleStandardSubstitution(const NamedDecl *ND);
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000240
Daniel Dunbar1b077112009-11-21 09:06:10 +0000241 void addSubstitution(const NamedDecl *ND) {
242 ND = cast<NamedDecl>(ND->getCanonicalDecl());
Anders Carlsson433d1372009-11-07 04:26:04 +0000243
Daniel Dunbar1b077112009-11-21 09:06:10 +0000244 addSubstitution(reinterpret_cast<uintptr_t>(ND));
245 }
246 void addSubstitution(QualType T);
Douglas Gregor1e9268e2010-04-28 05:58:56 +0000247 void addSubstitution(TemplateName Template);
Daniel Dunbar1b077112009-11-21 09:06:10 +0000248 void addSubstitution(uintptr_t Ptr);
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000249
John McCalla0ce15c2011-04-24 08:23:24 +0000250 void mangleUnresolvedPrefix(NestedNameSpecifier *qualifier,
251 NamedDecl *firstQualifierLookup,
252 bool recursive = false);
253 void mangleUnresolvedName(NestedNameSpecifier *qualifier,
254 NamedDecl *firstQualifierLookup,
255 DeclarationName name,
John McCall1dd73832010-02-04 01:42:13 +0000256 unsigned KnownArity = UnknownArity);
257
John McCalla0ce15c2011-04-24 08:23:24 +0000258 void mangleUnresolvedType(QualType type);
259
Daniel Dunbar1b077112009-11-21 09:06:10 +0000260 void mangleName(const TemplateDecl *TD,
261 const TemplateArgument *TemplateArgs,
262 unsigned NumTemplateArgs);
John McCall1dd73832010-02-04 01:42:13 +0000263 void mangleUnqualifiedName(const NamedDecl *ND) {
264 mangleUnqualifiedName(ND, ND->getDeclName(), UnknownArity);
265 }
266 void mangleUnqualifiedName(const NamedDecl *ND, DeclarationName Name,
267 unsigned KnownArity);
Daniel Dunbar1b077112009-11-21 09:06:10 +0000268 void mangleUnscopedName(const NamedDecl *ND);
269 void mangleUnscopedTemplateName(const TemplateDecl *ND);
Douglas Gregor1e9268e2010-04-28 05:58:56 +0000270 void mangleUnscopedTemplateName(TemplateName);
Daniel Dunbar1b077112009-11-21 09:06:10 +0000271 void mangleSourceName(const IdentifierInfo *II);
272 void mangleLocalName(const NamedDecl *ND);
Fariborz Jahanian57058532010-03-03 19:41:08 +0000273 void mangleNestedName(const NamedDecl *ND, const DeclContext *DC,
274 bool NoFunction=false);
Daniel Dunbar1b077112009-11-21 09:06:10 +0000275 void mangleNestedName(const TemplateDecl *TD,
276 const TemplateArgument *TemplateArgs,
277 unsigned NumTemplateArgs);
John McCalla0ce15c2011-04-24 08:23:24 +0000278 void manglePrefix(NestedNameSpecifier *qualifier);
Fariborz Jahanian57058532010-03-03 19:41:08 +0000279 void manglePrefix(const DeclContext *DC, bool NoFunction=false);
Daniel Dunbar1b077112009-11-21 09:06:10 +0000280 void mangleTemplatePrefix(const TemplateDecl *ND);
Douglas Gregor20f0cc72010-04-23 03:10:43 +0000281 void mangleTemplatePrefix(TemplateName Template);
Daniel Dunbar1b077112009-11-21 09:06:10 +0000282 void mangleOperatorName(OverloadedOperatorKind OO, unsigned Arity);
283 void mangleQualifiers(Qualifiers Quals);
Douglas Gregor0a9a6d62011-01-26 17:36:28 +0000284 void mangleRefQualifier(RefQualifierKind RefQualifier);
John McCallefe6aee2009-09-05 07:56:18 +0000285
Anders Carlsson7b06f6c2009-12-10 03:14:39 +0000286 void mangleObjCMethodName(const ObjCMethodDecl *MD);
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000287
Daniel Dunbar1b077112009-11-21 09:06:10 +0000288 // Declare manglers for every type class.
John McCallefe6aee2009-09-05 07:56:18 +0000289#define ABSTRACT_TYPE(CLASS, PARENT)
290#define NON_CANONICAL_TYPE(CLASS, PARENT)
291#define TYPE(CLASS, PARENT) void mangleType(const CLASS##Type *T);
292#include "clang/AST/TypeNodes.def"
293
Daniel Dunbar1b077112009-11-21 09:06:10 +0000294 void mangleType(const TagType*);
John McCallb6f532e2010-07-14 06:43:17 +0000295 void mangleType(TemplateName);
Daniel Dunbar1b077112009-11-21 09:06:10 +0000296 void mangleBareFunctionType(const FunctionType *T,
297 bool MangleReturnType);
Bob Wilson57147a82010-11-16 00:32:18 +0000298 void mangleNeonVectorType(const VectorType *T);
Anders Carlssone170ba72009-12-14 01:45:37 +0000299
300 void mangleIntegerLiteral(QualType T, const llvm::APSInt &Value);
John McCalla0ce15c2011-04-24 08:23:24 +0000301 void mangleMemberExpr(const Expr *base, bool isArrow,
302 NestedNameSpecifier *qualifier,
303 NamedDecl *firstQualifierLookup,
304 DeclarationName name,
305 unsigned knownArity);
John McCall5e1e89b2010-08-18 19:18:59 +0000306 void mangleExpression(const Expr *E, unsigned Arity = UnknownArity);
Daniel Dunbar1b077112009-11-21 09:06:10 +0000307 void mangleCXXCtorType(CXXCtorType T);
308 void mangleCXXDtorType(CXXDtorType T);
Mike Stump1eb44332009-09-09 15:08:12 +0000309
John McCall6dbce192010-08-20 00:17:19 +0000310 void mangleTemplateArgs(const ExplicitTemplateArgumentList &TemplateArgs);
Douglas Gregor20f0cc72010-04-23 03:10:43 +0000311 void mangleTemplateArgs(TemplateName Template,
312 const TemplateArgument *TemplateArgs,
Sean Huntc3021132010-05-05 15:23:54 +0000313 unsigned NumTemplateArgs);
Rafael Espindolad9800722010-03-11 14:07:00 +0000314 void mangleTemplateArgs(const TemplateParameterList &PL,
315 const TemplateArgument *TemplateArgs,
Daniel Dunbar1b077112009-11-21 09:06:10 +0000316 unsigned NumTemplateArgs);
Rafael Espindolad9800722010-03-11 14:07:00 +0000317 void mangleTemplateArgs(const TemplateParameterList &PL,
318 const TemplateArgumentList &AL);
319 void mangleTemplateArg(const NamedDecl *P, const TemplateArgument &A);
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000320
Daniel Dunbar1b077112009-11-21 09:06:10 +0000321 void mangleTemplateParameter(unsigned Index);
John McCallfb44de92011-05-01 22:35:37 +0000322
323 void mangleFunctionParam(const ParmVarDecl *parm);
Daniel Dunbar1b077112009-11-21 09:06:10 +0000324};
Peter Collingbourne14110472011-01-13 18:57:25 +0000325
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000326}
327
Anders Carlsson43f17402009-04-02 15:51:53 +0000328static bool isInCLinkageSpecification(const Decl *D) {
Douglas Gregor457e2812009-10-28 16:31:34 +0000329 D = D->getCanonicalDecl();
Mike Stump1eb44332009-09-09 15:08:12 +0000330 for (const DeclContext *DC = D->getDeclContext();
Anders Carlsson43f17402009-04-02 15:51:53 +0000331 !DC->isTranslationUnit(); DC = DC->getParent()) {
Mike Stump1eb44332009-09-09 15:08:12 +0000332 if (const LinkageSpecDecl *Linkage = dyn_cast<LinkageSpecDecl>(DC))
Anders Carlsson43f17402009-04-02 15:51:53 +0000333 return Linkage->getLanguage() == LinkageSpecDecl::lang_c;
334 }
Mike Stump1eb44332009-09-09 15:08:12 +0000335
Anders Carlsson43f17402009-04-02 15:51:53 +0000336 return false;
337}
338
Peter Collingbourne14110472011-01-13 18:57:25 +0000339bool ItaniumMangleContext::shouldMangleDeclName(const NamedDecl *D) {
Daniel Dunbarf981bf82009-11-21 09:14:52 +0000340 // In C, functions with no attributes never need to be mangled. Fastpath them.
341 if (!getASTContext().getLangOptions().CPlusPlus && !D->hasAttrs())
342 return false;
343
344 // Any decl can be declared with __asm("foo") on it, and this takes precedence
345 // over all other naming in the .o file.
346 if (D->hasAttr<AsmLabelAttr>())
347 return true;
348
Mike Stump141c5af2009-09-02 00:25:38 +0000349 // Clang's "overloadable" attribute extension to C/C++ implies name mangling
Anders Carlssona1e16222009-11-07 07:15:03 +0000350 // (always) as does passing a C++ member function and a function
351 // whose name is not a simple identifier.
Daniel Dunbarf981bf82009-11-21 09:14:52 +0000352 const FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
353 if (FD && (FD->hasAttr<OverloadableAttr>() || isa<CXXMethodDecl>(FD) ||
354 !FD->getDeclName().isIdentifier()))
355 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000356
Daniel Dunbarf981bf82009-11-21 09:14:52 +0000357 // Otherwise, no mangling is done outside C++ mode.
358 if (!getASTContext().getLangOptions().CPlusPlus)
359 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000360
Sean Hunt31455252010-01-24 03:04:27 +0000361 // Variables at global scope with non-internal linkage are not mangled
Eli Friedman7facf842009-12-02 20:32:49 +0000362 if (!FD) {
363 const DeclContext *DC = D->getDeclContext();
364 // Check for extern variable declared locally.
Fariborz Jahaniane81c5612010-06-30 18:57:21 +0000365 if (DC->isFunctionOrMethod() && D->hasLinkage())
Eli Friedman7facf842009-12-02 20:32:49 +0000366 while (!DC->isNamespace() && !DC->isTranslationUnit())
367 DC = DC->getParent();
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000368 if (DC->isTranslationUnit() && D->getLinkage() != InternalLinkage)
Eli Friedman7facf842009-12-02 20:32:49 +0000369 return false;
370 }
371
Eli Friedmanc00cb642010-07-18 20:49:59 +0000372 // Class members are always mangled.
373 if (D->getDeclContext()->isRecord())
374 return true;
375
Eli Friedman7facf842009-12-02 20:32:49 +0000376 // C functions and "main" are not mangled.
377 if ((FD && FD->isMain()) || isInCLinkageSpecification(D))
Daniel Dunbarf981bf82009-11-21 09:14:52 +0000378 return false;
379
Anders Carlsson43f17402009-04-02 15:51:53 +0000380 return true;
381}
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000382
Daniel Dunbar7e0c1952009-11-21 09:17:15 +0000383void CXXNameMangler::mangle(const NamedDecl *D, llvm::StringRef Prefix) {
Mike Stump141c5af2009-09-02 00:25:38 +0000384 // Any decl can be declared with __asm("foo") on it, and this takes precedence
385 // over all other naming in the .o file.
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000386 if (const AsmLabelAttr *ALA = D->getAttr<AsmLabelAttr>()) {
Chris Lattnerca3f25c2009-03-21 08:24:40 +0000387 // If we have an asm name, then we use it as the mangling.
Rafael Espindola4e274e92011-02-15 22:23:51 +0000388
389 // Adding the prefix can cause problems when one file has a "foo" and
390 // another has a "\01foo". That is known to happen on ELF with the
391 // tricks normally used for producing aliases (PR9177). Fortunately the
392 // llvm mangler on ELF is a nop, so we can just avoid adding the \01
Peter Collingbourne69317432011-04-06 12:29:09 +0000393 // marker. We also avoid adding the marker if this is an alias for an
394 // LLVM intrinsic.
Rafael Espindola4e274e92011-02-15 22:23:51 +0000395 llvm::StringRef UserLabelPrefix =
396 getASTContext().Target.getUserLabelPrefix();
Peter Collingbourne69317432011-04-06 12:29:09 +0000397 if (!UserLabelPrefix.empty() && !ALA->getLabel().startswith("llvm."))
Rafael Espindola4e274e92011-02-15 22:23:51 +0000398 Out << '\01'; // LLVM IR Marker for __asm("foo")
399
Chris Lattnerca3f25c2009-03-21 08:24:40 +0000400 Out << ALA->getLabel();
Daniel Dunbarf981bf82009-11-21 09:14:52 +0000401 return;
Chris Lattnerca3f25c2009-03-21 08:24:40 +0000402 }
Mike Stump1eb44332009-09-09 15:08:12 +0000403
Sean Hunt31455252010-01-24 03:04:27 +0000404 // <mangled-name> ::= _Z <encoding>
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000405 // ::= <data name>
406 // ::= <special-name>
Daniel Dunbar7e0c1952009-11-21 09:17:15 +0000407 Out << Prefix;
408 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
Daniel Dunbarf981bf82009-11-21 09:14:52 +0000409 mangleFunctionEncoding(FD);
Rafael Espindolad9800722010-03-11 14:07:00 +0000410 else if (const VarDecl *VD = dyn_cast<VarDecl>(D))
411 mangleName(VD);
Daniel Dunbar7e0c1952009-11-21 09:17:15 +0000412 else
Rafael Espindolad9800722010-03-11 14:07:00 +0000413 mangleName(cast<FieldDecl>(D));
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000414}
415
416void CXXNameMangler::mangleFunctionEncoding(const FunctionDecl *FD) {
417 // <encoding> ::= <function name> <bare-function-type>
418 mangleName(FD);
Mike Stump1eb44332009-09-09 15:08:12 +0000419
Daniel Dunbar7e0c1952009-11-21 09:17:15 +0000420 // Don't mangle in the type if this isn't a decl we should typically mangle.
421 if (!Context.shouldMangleDeclName(FD))
422 return;
423
Mike Stump141c5af2009-09-02 00:25:38 +0000424 // Whether the mangling of a function type includes the return type depends on
425 // the context and the nature of the function. The rules for deciding whether
426 // the return type is included are:
Mike Stump1eb44332009-09-09 15:08:12 +0000427 //
Douglas Gregor1fd2dd12009-06-29 22:39:32 +0000428 // 1. Template functions (names or types) have return types encoded, with
429 // the exceptions listed below.
Mike Stump1eb44332009-09-09 15:08:12 +0000430 // 2. Function types not appearing as part of a function name mangling,
Douglas Gregor1fd2dd12009-06-29 22:39:32 +0000431 // e.g. parameters, pointer types, etc., have return type encoded, with the
432 // exceptions listed below.
433 // 3. Non-template function names do not have return types encoded.
434 //
Mike Stump141c5af2009-09-02 00:25:38 +0000435 // The exceptions mentioned in (1) and (2) above, for which the return type is
436 // never included, are
Douglas Gregor1fd2dd12009-06-29 22:39:32 +0000437 // 1. Constructors.
438 // 2. Destructors.
439 // 3. Conversion operator functions, e.g. operator int.
440 bool MangleReturnType = false;
Anders Carlsson9234b7f2009-09-17 03:46:43 +0000441 if (FunctionTemplateDecl *PrimaryTemplate = FD->getPrimaryTemplate()) {
442 if (!(isa<CXXConstructorDecl>(FD) || isa<CXXDestructorDecl>(FD) ||
443 isa<CXXConversionDecl>(FD)))
444 MangleReturnType = true;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000445
Anders Carlsson9234b7f2009-09-17 03:46:43 +0000446 // Mangle the type of the primary template.
447 FD = PrimaryTemplate->getTemplatedDecl();
448 }
449
John McCall54e14c42009-10-22 22:37:11 +0000450 // Do the canonicalization out here because parameter types can
451 // undergo additional canonicalization (e.g. array decay).
John McCallf4c73712011-01-19 06:33:43 +0000452 const FunctionType *FT
453 = cast<FunctionType>(Context.getASTContext()
John McCall54e14c42009-10-22 22:37:11 +0000454 .getCanonicalType(FD->getType()));
455
456 mangleBareFunctionType(FT, MangleReturnType);
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000457}
458
Anders Carlsson47846d22009-12-04 06:23:23 +0000459static const DeclContext *IgnoreLinkageSpecDecls(const DeclContext *DC) {
460 while (isa<LinkageSpecDecl>(DC)) {
Anders Carlsson47846d22009-12-04 06:23:23 +0000461 DC = DC->getParent();
462 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000463
Anders Carlsson47846d22009-12-04 06:23:23 +0000464 return DC;
465}
466
Anders Carlssonc820f902010-06-02 15:58:27 +0000467/// isStd - Return whether a given namespace is the 'std' namespace.
468static bool isStd(const NamespaceDecl *NS) {
469 if (!IgnoreLinkageSpecDecls(NS->getParent())->isTranslationUnit())
470 return false;
471
472 const IdentifierInfo *II = NS->getOriginalNamespace()->getIdentifier();
473 return II && II->isStr("std");
474}
475
Anders Carlsson47846d22009-12-04 06:23:23 +0000476// isStdNamespace - Return whether a given decl context is a toplevel 'std'
477// namespace.
Daniel Dunbar1308af92009-11-21 09:11:45 +0000478static bool isStdNamespace(const DeclContext *DC) {
Anders Carlsson47846d22009-12-04 06:23:23 +0000479 if (!DC->isNamespace())
480 return false;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000481
Anders Carlsson47846d22009-12-04 06:23:23 +0000482 return isStd(cast<NamespaceDecl>(DC));
Daniel Dunbar1308af92009-11-21 09:11:45 +0000483}
484
Anders Carlssonbb36ba42009-09-26 03:24:57 +0000485static const TemplateDecl *
486isTemplate(const NamedDecl *ND, const TemplateArgumentList *&TemplateArgs) {
Anders Carlsson2744a062009-09-18 19:00:18 +0000487 // Check if we have a function template.
488 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)){
Anders Carlssonbb36ba42009-09-26 03:24:57 +0000489 if (const TemplateDecl *TD = FD->getPrimaryTemplate()) {
Anders Carlsson2744a062009-09-18 19:00:18 +0000490 TemplateArgs = FD->getTemplateSpecializationArgs();
Anders Carlssonbb36ba42009-09-26 03:24:57 +0000491 return TD;
Anders Carlsson2744a062009-09-18 19:00:18 +0000492 }
493 }
494
Anders Carlssoneafc6dc2009-09-18 19:44:50 +0000495 // Check if we have a class template.
496 if (const ClassTemplateSpecializationDecl *Spec =
497 dyn_cast<ClassTemplateSpecializationDecl>(ND)) {
498 TemplateArgs = &Spec->getTemplateArgs();
Anders Carlssonbb36ba42009-09-26 03:24:57 +0000499 return Spec->getSpecializedTemplate();
Anders Carlssoneafc6dc2009-09-18 19:44:50 +0000500 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000501
Anders Carlsson2744a062009-09-18 19:00:18 +0000502 return 0;
503}
504
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000505void CXXNameMangler::mangleName(const NamedDecl *ND) {
506 // <name> ::= <nested-name>
507 // ::= <unscoped-name>
508 // ::= <unscoped-template-name> <template-args>
Anders Carlsson201ce742009-09-17 03:17:01 +0000509 // ::= <local-name>
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000510 //
Anders Carlssond58d6f72009-09-17 16:12:20 +0000511 const DeclContext *DC = ND->getDeclContext();
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000512
Eli Friedman7facf842009-12-02 20:32:49 +0000513 // If this is an extern variable declared locally, the relevant DeclContext
514 // is that of the containing namespace, or the translation unit.
515 if (isa<FunctionDecl>(DC) && ND->hasLinkage())
516 while (!DC->isNamespace() && !DC->isTranslationUnit())
517 DC = DC->getParent();
John McCall82b7d7b2010-10-18 21:28:44 +0000518 else if (GetLocalClassDecl(ND)) {
519 mangleLocalName(ND);
520 return;
521 }
Eli Friedman7facf842009-12-02 20:32:49 +0000522
Anders Carlsson5cc58c62009-09-22 17:23:30 +0000523 while (isa<LinkageSpecDecl>(DC))
Anders Carlssond58d6f72009-09-17 16:12:20 +0000524 DC = DC->getParent();
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000525
Anders Carlssond58d6f72009-09-17 16:12:20 +0000526 if (DC->isTranslationUnit() || isStdNamespace(DC)) {
Anders Carlsson2744a062009-09-18 19:00:18 +0000527 // Check if we have a template.
528 const TemplateArgumentList *TemplateArgs = 0;
Anders Carlsson0fa6df42009-09-26 19:45:45 +0000529 if (const TemplateDecl *TD = isTemplate(ND, TemplateArgs)) {
Anders Carlsson2744a062009-09-18 19:00:18 +0000530 mangleUnscopedTemplateName(TD);
Rafael Espindolad9800722010-03-11 14:07:00 +0000531 TemplateParameterList *TemplateParameters = TD->getTemplateParameters();
532 mangleTemplateArgs(*TemplateParameters, *TemplateArgs);
Anders Carlsson2744a062009-09-18 19:00:18 +0000533 return;
Anders Carlsson7482e242009-09-18 04:29:09 +0000534 }
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000535
Anders Carlsson7482e242009-09-18 04:29:09 +0000536 mangleUnscopedName(ND);
537 return;
538 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000539
Anders Carlsson7b06f6c2009-12-10 03:14:39 +0000540 if (isa<FunctionDecl>(DC) || isa<ObjCMethodDecl>(DC)) {
Anders Carlsson7482e242009-09-18 04:29:09 +0000541 mangleLocalName(ND);
542 return;
543 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000544
Eli Friedman7facf842009-12-02 20:32:49 +0000545 mangleNestedName(ND, DC);
Anders Carlsson7482e242009-09-18 04:29:09 +0000546}
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000547void CXXNameMangler::mangleName(const TemplateDecl *TD,
Anders Carlsson7624f212009-09-18 02:42:01 +0000548 const TemplateArgument *TemplateArgs,
549 unsigned NumTemplateArgs) {
Anders Carlsson47846d22009-12-04 06:23:23 +0000550 const DeclContext *DC = IgnoreLinkageSpecDecls(TD->getDeclContext());
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000551
Anders Carlsson7624f212009-09-18 02:42:01 +0000552 if (DC->isTranslationUnit() || isStdNamespace(DC)) {
Anders Carlsson0fa6df42009-09-26 19:45:45 +0000553 mangleUnscopedTemplateName(TD);
Rafael Espindolad9800722010-03-11 14:07:00 +0000554 TemplateParameterList *TemplateParameters = TD->getTemplateParameters();
555 mangleTemplateArgs(*TemplateParameters, TemplateArgs, NumTemplateArgs);
Anders Carlsson7624f212009-09-18 02:42:01 +0000556 } else {
557 mangleNestedName(TD, TemplateArgs, NumTemplateArgs);
558 }
559}
560
Anders Carlsson201ce742009-09-17 03:17:01 +0000561void CXXNameMangler::mangleUnscopedName(const NamedDecl *ND) {
562 // <unscoped-name> ::= <unqualified-name>
563 // ::= St <unqualified-name> # ::std::
564 if (isStdNamespace(ND->getDeclContext()))
565 Out << "St";
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000566
Anders Carlsson201ce742009-09-17 03:17:01 +0000567 mangleUnqualifiedName(ND);
568}
569
Anders Carlsson0fa6df42009-09-26 19:45:45 +0000570void CXXNameMangler::mangleUnscopedTemplateName(const TemplateDecl *ND) {
Anders Carlsson201ce742009-09-17 03:17:01 +0000571 // <unscoped-template-name> ::= <unscoped-name>
572 // ::= <substitution>
Anders Carlsson7624f212009-09-18 02:42:01 +0000573 if (mangleSubstitution(ND))
Anders Carlsson03c9d532009-09-17 04:02:31 +0000574 return;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000575
Douglas Gregor32fb4e12010-02-05 20:45:00 +0000576 // <template-template-param> ::= <template-param>
577 if (const TemplateTemplateParmDecl *TTP
578 = dyn_cast<TemplateTemplateParmDecl>(ND)) {
579 mangleTemplateParameter(TTP->getIndex());
580 return;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000581 }
Douglas Gregor32fb4e12010-02-05 20:45:00 +0000582
Anders Carlsson1668f202009-09-26 20:13:56 +0000583 mangleUnscopedName(ND->getTemplatedDecl());
Anders Carlsson7624f212009-09-18 02:42:01 +0000584 addSubstitution(ND);
Anders Carlsson201ce742009-09-17 03:17:01 +0000585}
586
Douglas Gregor1e9268e2010-04-28 05:58:56 +0000587void CXXNameMangler::mangleUnscopedTemplateName(TemplateName Template) {
588 // <unscoped-template-name> ::= <unscoped-name>
589 // ::= <substitution>
590 if (TemplateDecl *TD = Template.getAsTemplateDecl())
591 return mangleUnscopedTemplateName(TD);
Sean Huntc3021132010-05-05 15:23:54 +0000592
Douglas Gregor1e9268e2010-04-28 05:58:56 +0000593 if (mangleSubstitution(Template))
594 return;
595
596 // FIXME: How to cope with operators here?
597 DependentTemplateName *Dependent = Template.getAsDependentTemplateName();
598 assert(Dependent && "Not a dependent template name?");
599 if (!Dependent->isIdentifier()) {
600 // FIXME: We can't possibly know the arity of the operator here!
601 Diagnostic &Diags = Context.getDiags();
602 unsigned DiagID = Diags.getCustomDiagID(Diagnostic::Error,
603 "cannot mangle dependent operator name");
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000604 Diags.Report(DiagID);
Douglas Gregor1e9268e2010-04-28 05:58:56 +0000605 return;
606 }
Sean Huntc3021132010-05-05 15:23:54 +0000607
Douglas Gregor1e9268e2010-04-28 05:58:56 +0000608 mangleSourceName(Dependent->getIdentifier());
609 addSubstitution(Template);
610}
611
John McCall1b600522011-04-24 03:07:16 +0000612void CXXNameMangler::mangleFloat(const llvm::APFloat &f) {
613 // ABI:
614 // Floating-point literals are encoded using a fixed-length
615 // lowercase hexadecimal string corresponding to the internal
616 // representation (IEEE on Itanium), high-order bytes first,
617 // without leading zeroes. For example: "Lf bf800000 E" is -1.0f
618 // on Itanium.
619 // APInt::toString uses uppercase hexadecimal, and it's not really
620 // worth embellishing that interface for this use case, so we just
621 // do a second pass to lowercase things.
622 typedef llvm::SmallString<20> buffer_t;
623 buffer_t buffer;
624 f.bitcastToAPInt().toString(buffer, 16, false);
625
626 for (buffer_t::iterator i = buffer.begin(), e = buffer.end(); i != e; ++i)
627 if (isupper(*i)) *i = tolower(*i);
628
629 Out.write(buffer.data(), buffer.size());
John McCall0512e482010-07-14 04:20:34 +0000630}
631
632void CXXNameMangler::mangleNumber(const llvm::APSInt &Value) {
633 if (Value.isSigned() && Value.isNegative()) {
634 Out << 'n';
635 Value.abs().print(Out, true);
636 } else
637 Value.print(Out, Value.isSigned());
638}
639
Anders Carlssona94822e2009-11-26 02:32:05 +0000640void CXXNameMangler::mangleNumber(int64_t Number) {
641 // <number> ::= [n] <non-negative decimal integer>
642 if (Number < 0) {
643 Out << 'n';
644 Number = -Number;
645 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000646
Anders Carlssona94822e2009-11-26 02:32:05 +0000647 Out << Number;
648}
649
Anders Carlsson19879c92010-03-23 17:17:29 +0000650void CXXNameMangler::mangleCallOffset(int64_t NonVirtual, int64_t Virtual) {
Mike Stump141c5af2009-09-02 00:25:38 +0000651 // <call-offset> ::= h <nv-offset> _
652 // ::= v <v-offset> _
653 // <nv-offset> ::= <offset number> # non-virtual base override
Anders Carlssona94822e2009-11-26 02:32:05 +0000654 // <v-offset> ::= <offset number> _ <virtual offset number>
Mike Stump141c5af2009-09-02 00:25:38 +0000655 // # virtual base override, with vcall offset
Anders Carlsson19879c92010-03-23 17:17:29 +0000656 if (!Virtual) {
Anders Carlssona94822e2009-11-26 02:32:05 +0000657 Out << 'h';
Anders Carlsson19879c92010-03-23 17:17:29 +0000658 mangleNumber(NonVirtual);
Anders Carlssona94822e2009-11-26 02:32:05 +0000659 Out << '_';
660 return;
Mike Stump141c5af2009-09-02 00:25:38 +0000661 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000662
Anders Carlssona94822e2009-11-26 02:32:05 +0000663 Out << 'v';
Anders Carlsson19879c92010-03-23 17:17:29 +0000664 mangleNumber(NonVirtual);
Anders Carlssona94822e2009-11-26 02:32:05 +0000665 Out << '_';
Anders Carlsson19879c92010-03-23 17:17:29 +0000666 mangleNumber(Virtual);
Anders Carlssona94822e2009-11-26 02:32:05 +0000667 Out << '_';
Mike Stump9124bcc2009-09-02 00:56:18 +0000668}
669
John McCalla0ce15c2011-04-24 08:23:24 +0000670void CXXNameMangler::mangleUnresolvedType(QualType type) {
671 if (const TemplateSpecializationType *TST =
672 type->getAs<TemplateSpecializationType>()) {
673 if (!mangleSubstitution(QualType(TST, 0))) {
674 mangleTemplatePrefix(TST->getTemplateName());
Sean Huntc3021132010-05-05 15:23:54 +0000675
Douglas Gregoraa2187d2011-02-28 00:04:36 +0000676 // FIXME: GCC does not appear to mangle the template arguments when
677 // the template in question is a dependent template name. Should we
678 // emulate that badness?
John McCalla0ce15c2011-04-24 08:23:24 +0000679 mangleTemplateArgs(TST->getTemplateName(), TST->getArgs(),
680 TST->getNumArgs());
681 addSubstitution(QualType(TST, 0));
Rafael Espindola9b35b252010-03-17 04:28:11 +0000682 }
John McCalla0ce15c2011-04-24 08:23:24 +0000683 } else if (const DependentTemplateSpecializationType *DTST
684 = type->getAs<DependentTemplateSpecializationType>()) {
685 TemplateName Template
686 = getASTContext().getDependentTemplateName(DTST->getQualifier(),
687 DTST->getIdentifier());
688 mangleTemplatePrefix(Template);
689
690 // FIXME: GCC does not appear to mangle the template arguments when
691 // the template in question is a dependent template name. Should we
692 // emulate that badness?
693 mangleTemplateArgs(Template, DTST->getArgs(), DTST->getNumArgs());
694 } else {
695 // We use the QualType mangle type variant here because it handles
696 // substitutions.
697 mangleType(type);
John McCall1dd73832010-02-04 01:42:13 +0000698 }
699}
700
John McCalla0ce15c2011-04-24 08:23:24 +0000701/// Mangle everything prior to the base-unresolved-name in an unresolved-name.
702///
703/// \param firstQualifierLookup - the entity found by unqualified lookup
704/// for the first name in the qualifier, if this is for a member expression
705/// \param recursive - true if this is being called recursively,
706/// i.e. if there is more prefix "to the right".
707void CXXNameMangler::mangleUnresolvedPrefix(NestedNameSpecifier *qualifier,
708 NamedDecl *firstQualifierLookup,
709 bool recursive) {
John McCall1dd73832010-02-04 01:42:13 +0000710
John McCalla0ce15c2011-04-24 08:23:24 +0000711 // x, ::x
712 // <unresolved-name> ::= [gs] <base-unresolved-name>
713
714 // T::x / decltype(p)::x
715 // <unresolved-name> ::= sr <unresolved-type> <base-unresolved-name>
716
717 // T::N::x /decltype(p)::N::x
718 // <unresolved-name> ::= srN <unresolved-type> <unresolved-qualifier-level>+ E
719 // <base-unresolved-name>
720
721 // A::x, N::y, A<T>::z; "gs" means leading "::"
722 // <unresolved-name> ::= [gs] sr <unresolved-qualifier-level>+ E
723 // <base-unresolved-name>
724
725 switch (qualifier->getKind()) {
726 case NestedNameSpecifier::Global:
727 Out << "gs";
728
729 // We want an 'sr' unless this is the entire NNS.
730 if (recursive)
731 Out << "sr";
732
733 // We never want an 'E' here.
734 return;
735
736 case NestedNameSpecifier::Namespace:
737 if (qualifier->getPrefix())
738 mangleUnresolvedPrefix(qualifier->getPrefix(), firstQualifierLookup,
739 /*recursive*/ true);
740 else
741 Out << "sr";
742 mangleSourceName(qualifier->getAsNamespace()->getIdentifier());
743 break;
744 case NestedNameSpecifier::NamespaceAlias:
745 if (qualifier->getPrefix())
746 mangleUnresolvedPrefix(qualifier->getPrefix(), firstQualifierLookup,
747 /*recursive*/ true);
748 else
749 Out << "sr";
750 mangleSourceName(qualifier->getAsNamespaceAlias()->getIdentifier());
751 break;
752
753 case NestedNameSpecifier::TypeSpec:
754 case NestedNameSpecifier::TypeSpecWithTemplate: {
755 // Both cases want this.
756 Out << "sr";
757
758 // We only get here recursively if we're followed by identifiers.
759 if (recursive) Out << 'N';
760
761 mangleUnresolvedType(QualType(qualifier->getAsType(), 0));
762
763 // We never want to print 'E' directly after an unresolved-type,
764 // so we return directly.
765 return;
766 }
767
768 case NestedNameSpecifier::Identifier:
769 // Member expressions can have these without prefixes.
770 if (qualifier->getPrefix()) {
771 mangleUnresolvedPrefix(qualifier->getPrefix(), firstQualifierLookup,
772 /*recursive*/ true);
773 } else if (firstQualifierLookup) {
774
775 // Try to make a proper qualifier out of the lookup result, and
776 // then just recurse on that.
777 NestedNameSpecifier *newQualifier;
778 if (TypeDecl *typeDecl = dyn_cast<TypeDecl>(firstQualifierLookup)) {
779 QualType type = getASTContext().getTypeDeclType(typeDecl);
780
781 // Pretend we had a different nested name specifier.
782 newQualifier = NestedNameSpecifier::Create(getASTContext(),
783 /*prefix*/ 0,
784 /*template*/ false,
785 type.getTypePtr());
786 } else if (NamespaceDecl *nspace =
787 dyn_cast<NamespaceDecl>(firstQualifierLookup)) {
788 newQualifier = NestedNameSpecifier::Create(getASTContext(),
789 /*prefix*/ 0,
790 nspace);
791 } else if (NamespaceAliasDecl *alias =
792 dyn_cast<NamespaceAliasDecl>(firstQualifierLookup)) {
793 newQualifier = NestedNameSpecifier::Create(getASTContext(),
794 /*prefix*/ 0,
795 alias);
796 } else {
797 // No sensible mangling to do here.
798 newQualifier = 0;
799 }
800
801 if (newQualifier)
802 return mangleUnresolvedPrefix(newQualifier, /*lookup*/ 0, recursive);
803
804 } else {
805 Out << "sr";
806 }
807
808 mangleSourceName(qualifier->getAsIdentifier());
809 break;
810 }
811
812 // If this was the innermost part of the NNS, and we fell out to
813 // here, append an 'E'.
814 if (!recursive)
815 Out << 'E';
816}
817
818/// Mangle an unresolved-name, which is generally used for names which
819/// weren't resolved to specific entities.
820void CXXNameMangler::mangleUnresolvedName(NestedNameSpecifier *qualifier,
821 NamedDecl *firstQualifierLookup,
822 DeclarationName name,
823 unsigned knownArity) {
824 if (qualifier) mangleUnresolvedPrefix(qualifier, firstQualifierLookup);
825 mangleUnqualifiedName(0, name, knownArity);
John McCall1dd73832010-02-04 01:42:13 +0000826}
827
Anders Carlsson6f7e2f42010-06-08 14:49:03 +0000828static const FieldDecl *FindFirstNamedDataMember(const RecordDecl *RD) {
829 assert(RD->isAnonymousStructOrUnion() &&
830 "Expected anonymous struct or union!");
831
832 for (RecordDecl::field_iterator I = RD->field_begin(), E = RD->field_end();
833 I != E; ++I) {
834 const FieldDecl *FD = *I;
835
836 if (FD->getIdentifier())
837 return FD;
838
839 if (const RecordType *RT = FD->getType()->getAs<RecordType>()) {
840 if (const FieldDecl *NamedDataMember =
841 FindFirstNamedDataMember(RT->getDecl()))
842 return NamedDataMember;
843 }
844 }
845
846 // We didn't find a named data member.
847 return 0;
848}
849
John McCall1dd73832010-02-04 01:42:13 +0000850void CXXNameMangler::mangleUnqualifiedName(const NamedDecl *ND,
851 DeclarationName Name,
852 unsigned KnownArity) {
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000853 // <unqualified-name> ::= <operator-name>
Mike Stump1eb44332009-09-09 15:08:12 +0000854 // ::= <ctor-dtor-name>
855 // ::= <source-name>
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000856 switch (Name.getNameKind()) {
Anders Carlssonc4355b62009-10-07 01:45:02 +0000857 case DeclarationName::Identifier: {
Anders Carlssonc4355b62009-10-07 01:45:02 +0000858 if (const IdentifierInfo *II = Name.getAsIdentifierInfo()) {
Sean Hunt31455252010-01-24 03:04:27 +0000859 // We must avoid conflicts between internally- and externally-
John McCall74990f42011-03-22 06:34:45 +0000860 // linked variable and function declaration names in the same TU:
861 // void test() { extern void foo(); }
862 // static void foo();
863 // This naming convention is the same as that followed by GCC,
864 // though it shouldn't actually matter.
865 if (ND && ND->getLinkage() == InternalLinkage &&
Sean Hunt31455252010-01-24 03:04:27 +0000866 ND->getDeclContext()->isFileContext())
867 Out << 'L';
868
Anders Carlssonc4355b62009-10-07 01:45:02 +0000869 mangleSourceName(II);
870 break;
871 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000872
John McCall1dd73832010-02-04 01:42:13 +0000873 // Otherwise, an anonymous entity. We must have a declaration.
874 assert(ND && "mangling empty name without declaration");
875
876 if (const NamespaceDecl *NS = dyn_cast<NamespaceDecl>(ND)) {
877 if (NS->isAnonymousNamespace()) {
878 // This is how gcc mangles these names.
879 Out << "12_GLOBAL__N_1";
880 break;
881 }
882 }
883
Anders Carlsson6f7e2f42010-06-08 14:49:03 +0000884 if (const VarDecl *VD = dyn_cast<VarDecl>(ND)) {
885 // We must have an anonymous union or struct declaration.
886 const RecordDecl *RD =
887 cast<RecordDecl>(VD->getType()->getAs<RecordType>()->getDecl());
888
889 // Itanium C++ ABI 5.1.2:
890 //
891 // For the purposes of mangling, the name of an anonymous union is
892 // considered to be the name of the first named data member found by a
893 // pre-order, depth-first, declaration-order walk of the data members of
894 // the anonymous union. If there is no such data member (i.e., if all of
895 // the data members in the union are unnamed), then there is no way for
896 // a program to refer to the anonymous union, and there is therefore no
897 // need to mangle its name.
898 const FieldDecl *FD = FindFirstNamedDataMember(RD);
John McCall7121c8f2010-08-05 22:02:13 +0000899
900 // It's actually possible for various reasons for us to get here
901 // with an empty anonymous struct / union. Fortunately, it
902 // doesn't really matter what name we generate.
903 if (!FD) break;
Anders Carlsson6f7e2f42010-06-08 14:49:03 +0000904 assert(FD->getIdentifier() && "Data member name isn't an identifier!");
905
906 mangleSourceName(FD->getIdentifier());
907 break;
908 }
909
Anders Carlssonc4355b62009-10-07 01:45:02 +0000910 // We must have an anonymous struct.
911 const TagDecl *TD = cast<TagDecl>(ND);
Richard Smith162e1c12011-04-15 14:24:37 +0000912 if (const TypedefNameDecl *D = TD->getTypedefNameForAnonDecl()) {
Anders Carlssonc4355b62009-10-07 01:45:02 +0000913 assert(TD->getDeclContext() == D->getDeclContext() &&
914 "Typedef should not be in another decl context!");
915 assert(D->getDeclName().getAsIdentifierInfo() &&
916 "Typedef was not named!");
917 mangleSourceName(D->getDeclName().getAsIdentifierInfo());
918 break;
919 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000920
Anders Carlssonc4355b62009-10-07 01:45:02 +0000921 // Get a unique id for the anonymous struct.
922 uint64_t AnonStructId = Context.getAnonymousStructId(TD);
923
924 // Mangle it as a source name in the form
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000925 // [n] $_<id>
Anders Carlssonc4355b62009-10-07 01:45:02 +0000926 // where n is the length of the string.
927 llvm::SmallString<8> Str;
928 Str += "$_";
929 Str += llvm::utostr(AnonStructId);
930
931 Out << Str.size();
932 Out << Str.str();
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000933 break;
Anders Carlssonc4355b62009-10-07 01:45:02 +0000934 }
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000935
936 case DeclarationName::ObjCZeroArgSelector:
937 case DeclarationName::ObjCOneArgSelector:
938 case DeclarationName::ObjCMultiArgSelector:
939 assert(false && "Can't mangle Objective-C selector names here!");
940 break;
941
942 case DeclarationName::CXXConstructorName:
Anders Carlsson27ae5362009-04-17 01:58:57 +0000943 if (ND == Structor)
Mike Stump141c5af2009-09-02 00:25:38 +0000944 // If the named decl is the C++ constructor we're mangling, use the type
945 // we were given.
Anders Carlsson27ae5362009-04-17 01:58:57 +0000946 mangleCXXCtorType(static_cast<CXXCtorType>(StructorType));
Anders Carlsson3ac86b52009-04-15 05:36:58 +0000947 else
948 // Otherwise, use the complete constructor name. This is relevant if a
949 // class with a constructor is declared within a constructor.
950 mangleCXXCtorType(Ctor_Complete);
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000951 break;
952
953 case DeclarationName::CXXDestructorName:
Anders Carlsson27ae5362009-04-17 01:58:57 +0000954 if (ND == Structor)
Mike Stump141c5af2009-09-02 00:25:38 +0000955 // If the named decl is the C++ destructor we're mangling, use the type we
956 // were given.
Anders Carlsson27ae5362009-04-17 01:58:57 +0000957 mangleCXXDtorType(static_cast<CXXDtorType>(StructorType));
958 else
959 // Otherwise, use the complete destructor name. This is relevant if a
960 // class with a destructor is declared within a destructor.
961 mangleCXXDtorType(Dtor_Complete);
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000962 break;
963
964 case DeclarationName::CXXConversionFunctionName:
Mike Stump1eb44332009-09-09 15:08:12 +0000965 // <operator-name> ::= cv <type> # (cast)
Douglas Gregor219cc612009-02-13 01:28:03 +0000966 Out << "cv";
Anders Carlssonb5404912009-10-07 01:06:45 +0000967 mangleType(Context.getASTContext().getCanonicalType(Name.getCXXNameType()));
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000968 break;
969
Anders Carlsson8257d412009-12-22 06:36:32 +0000970 case DeclarationName::CXXOperatorName: {
John McCall1dd73832010-02-04 01:42:13 +0000971 unsigned Arity;
972 if (ND) {
973 Arity = cast<FunctionDecl>(ND)->getNumParams();
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000974
John McCall1dd73832010-02-04 01:42:13 +0000975 // If we have a C++ member function, we need to include the 'this' pointer.
976 // FIXME: This does not make sense for operators that are static, but their
977 // names stay the same regardless of the arity (operator new for instance).
978 if (isa<CXXMethodDecl>(ND))
979 Arity++;
980 } else
981 Arity = KnownArity;
982
Anders Carlsson8257d412009-12-22 06:36:32 +0000983 mangleOperatorName(Name.getCXXOverloadedOperator(), Arity);
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000984 break;
Anders Carlsson8257d412009-12-22 06:36:32 +0000985 }
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000986
Sean Hunt3e518bd2009-11-29 07:34:05 +0000987 case DeclarationName::CXXLiteralOperatorName:
Sean Hunt5dd6b392009-12-04 21:11:13 +0000988 // FIXME: This mangling is not yet official.
Sean Hunt2421f662009-12-04 21:01:37 +0000989 Out << "li";
Sean Hunt3e518bd2009-11-29 07:34:05 +0000990 mangleSourceName(Name.getCXXLiteralIdentifier());
991 break;
992
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000993 case DeclarationName::CXXUsingDirective:
994 assert(false && "Can't mangle a using directive name!");
Douglas Gregor219cc612009-02-13 01:28:03 +0000995 break;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000996 }
997}
998
999void CXXNameMangler::mangleSourceName(const IdentifierInfo *II) {
1000 // <source-name> ::= <positive length number> <identifier>
1001 // <number> ::= [n] <non-negative decimal integer>
1002 // <identifier> ::= <unqualified source code identifier>
1003 Out << II->getLength() << II->getName();
1004}
1005
Eli Friedman7facf842009-12-02 20:32:49 +00001006void CXXNameMangler::mangleNestedName(const NamedDecl *ND,
Fariborz Jahanian57058532010-03-03 19:41:08 +00001007 const DeclContext *DC,
1008 bool NoFunction) {
Douglas Gregor0a9a6d62011-01-26 17:36:28 +00001009 // <nested-name>
1010 // ::= N [<CV-qualifiers>] [<ref-qualifier>] <prefix> <unqualified-name> E
1011 // ::= N [<CV-qualifiers>] [<ref-qualifier>] <template-prefix>
1012 // <template-args> E
Anders Carlssond99edc42009-09-26 03:55:37 +00001013
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001014 Out << 'N';
Douglas Gregor0a9a6d62011-01-26 17:36:28 +00001015 if (const CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(ND)) {
John McCall0953e762009-09-24 19:53:00 +00001016 mangleQualifiers(Qualifiers::fromCVRMask(Method->getTypeQualifiers()));
Douglas Gregor0a9a6d62011-01-26 17:36:28 +00001017 mangleRefQualifier(Method->getRefQualifier());
1018 }
1019
Anders Carlsson2744a062009-09-18 19:00:18 +00001020 // Check if we have a template.
1021 const TemplateArgumentList *TemplateArgs = 0;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00001022 if (const TemplateDecl *TD = isTemplate(ND, TemplateArgs)) {
Anders Carlsson2744a062009-09-18 19:00:18 +00001023 mangleTemplatePrefix(TD);
Rafael Espindolad9800722010-03-11 14:07:00 +00001024 TemplateParameterList *TemplateParameters = TD->getTemplateParameters();
1025 mangleTemplateArgs(*TemplateParameters, *TemplateArgs);
Fariborz Jahanian57058532010-03-03 19:41:08 +00001026 }
1027 else {
1028 manglePrefix(DC, NoFunction);
Anders Carlsson7482e242009-09-18 04:29:09 +00001029 mangleUnqualifiedName(ND);
1030 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00001031
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001032 Out << 'E';
1033}
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00001034void CXXNameMangler::mangleNestedName(const TemplateDecl *TD,
Anders Carlsson7624f212009-09-18 02:42:01 +00001035 const TemplateArgument *TemplateArgs,
1036 unsigned NumTemplateArgs) {
Anders Carlssone45117b2009-09-27 19:53:49 +00001037 // <nested-name> ::= N [<CV-qualifiers>] <template-prefix> <template-args> E
1038
Anders Carlsson7624f212009-09-18 02:42:01 +00001039 Out << 'N';
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00001040
Anders Carlssone45117b2009-09-27 19:53:49 +00001041 mangleTemplatePrefix(TD);
Rafael Espindolad9800722010-03-11 14:07:00 +00001042 TemplateParameterList *TemplateParameters = TD->getTemplateParameters();
1043 mangleTemplateArgs(*TemplateParameters, TemplateArgs, NumTemplateArgs);
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00001044
Anders Carlsson7624f212009-09-18 02:42:01 +00001045 Out << 'E';
1046}
1047
Anders Carlsson1b42c792009-04-02 16:24:45 +00001048void CXXNameMangler::mangleLocalName(const NamedDecl *ND) {
1049 // <local-name> := Z <function encoding> E <entity name> [<discriminator>]
1050 // := Z <function encoding> E s [<discriminator>]
Mike Stump1eb44332009-09-09 15:08:12 +00001051 // <discriminator> := _ <non-negative number>
Fariborz Jahanian57058532010-03-03 19:41:08 +00001052 const DeclContext *DC = ND->getDeclContext();
Anders Carlsson1b42c792009-04-02 16:24:45 +00001053 Out << 'Z';
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001054
Charles Davis685b1d92010-05-26 18:25:27 +00001055 if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(DC)) {
1056 mangleObjCMethodName(MD);
John McCall82b7d7b2010-10-18 21:28:44 +00001057 } else if (const CXXRecordDecl *RD = GetLocalClassDecl(ND)) {
1058 mangleFunctionEncoding(cast<FunctionDecl>(RD->getDeclContext()));
Fariborz Jahanian57058532010-03-03 19:41:08 +00001059 Out << 'E';
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001060
John McCall82b7d7b2010-10-18 21:28:44 +00001061 // Mangle the name relative to the closest enclosing function.
1062 if (ND == RD) // equality ok because RD derived from ND above
1063 mangleUnqualifiedName(ND);
1064 else
1065 mangleNestedName(ND, DC, true /*NoFunction*/);
1066
Fariborz Jahanian4819ac42010-03-04 01:02:03 +00001067 unsigned disc;
John McCall82b7d7b2010-10-18 21:28:44 +00001068 if (Context.getNextDiscriminator(RD, disc)) {
Fariborz Jahanian4819ac42010-03-04 01:02:03 +00001069 if (disc < 10)
1070 Out << '_' << disc;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001071 else
Fariborz Jahanian4819ac42010-03-04 01:02:03 +00001072 Out << "__" << disc << '_';
1073 }
Fariborz Jahanian57058532010-03-03 19:41:08 +00001074
1075 return;
1076 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001077 else
Fariborz Jahanian57058532010-03-03 19:41:08 +00001078 mangleFunctionEncoding(cast<FunctionDecl>(DC));
Anders Carlsson7b06f6c2009-12-10 03:14:39 +00001079
Anders Carlsson1b42c792009-04-02 16:24:45 +00001080 Out << 'E';
Eli Friedman6f9f25d2009-12-11 20:21:38 +00001081 mangleUnqualifiedName(ND);
Anders Carlsson1b42c792009-04-02 16:24:45 +00001082}
1083
John McCalla0ce15c2011-04-24 08:23:24 +00001084void CXXNameMangler::manglePrefix(NestedNameSpecifier *qualifier) {
1085 switch (qualifier->getKind()) {
1086 case NestedNameSpecifier::Global:
1087 // nothing
1088 return;
1089
1090 case NestedNameSpecifier::Namespace:
1091 mangleName(qualifier->getAsNamespace());
1092 return;
1093
1094 case NestedNameSpecifier::NamespaceAlias:
1095 mangleName(qualifier->getAsNamespaceAlias()->getNamespace());
1096 return;
1097
1098 case NestedNameSpecifier::TypeSpec:
1099 case NestedNameSpecifier::TypeSpecWithTemplate:
1100 mangleUnresolvedType(QualType(qualifier->getAsType(), 0));
1101 return;
1102
1103 case NestedNameSpecifier::Identifier:
1104 // Member expressions can have these without prefixes, but that
1105 // should end up in mangleUnresolvedPrefix instead.
1106 assert(qualifier->getPrefix());
1107 manglePrefix(qualifier->getPrefix());
1108
1109 mangleSourceName(qualifier->getAsIdentifier());
1110 return;
1111 }
1112
1113 llvm_unreachable("unexpected nested name specifier");
1114}
1115
Fariborz Jahanian57058532010-03-03 19:41:08 +00001116void CXXNameMangler::manglePrefix(const DeclContext *DC, bool NoFunction) {
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001117 // <prefix> ::= <prefix> <unqualified-name>
1118 // ::= <template-prefix> <template-args>
1119 // ::= <template-param>
1120 // ::= # empty
1121 // ::= <substitution>
Anders Carlsson6862fc72009-09-17 04:16:28 +00001122
Anders Carlssonadd28822009-09-22 20:33:31 +00001123 while (isa<LinkageSpecDecl>(DC))
1124 DC = DC->getParent();
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00001125
Anders Carlsson9263e912009-09-18 18:39:58 +00001126 if (DC->isTranslationUnit())
1127 return;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00001128
Douglas Gregor35415f52010-05-25 17:04:15 +00001129 if (const BlockDecl *Block = dyn_cast<BlockDecl>(DC)) {
1130 manglePrefix(DC->getParent(), NoFunction);
1131 llvm::SmallString<64> Name;
Rafael Espindolac4850c22011-02-10 23:59:36 +00001132 llvm::raw_svector_ostream NameStream(Name);
1133 Context.mangleBlock(Block, NameStream);
1134 NameStream.flush();
Douglas Gregor35415f52010-05-25 17:04:15 +00001135 Out << Name.size() << Name;
1136 return;
1137 }
1138
Anders Carlsson6862fc72009-09-17 04:16:28 +00001139 if (mangleSubstitution(cast<NamedDecl>(DC)))
1140 return;
Anders Carlsson7482e242009-09-18 04:29:09 +00001141
Anders Carlsson2ee3fca2009-09-18 20:11:09 +00001142 // Check if we have a template.
1143 const TemplateArgumentList *TemplateArgs = 0;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00001144 if (const TemplateDecl *TD = isTemplate(cast<NamedDecl>(DC), TemplateArgs)) {
Anders Carlsson2ee3fca2009-09-18 20:11:09 +00001145 mangleTemplatePrefix(TD);
Rafael Espindolad9800722010-03-11 14:07:00 +00001146 TemplateParameterList *TemplateParameters = TD->getTemplateParameters();
1147 mangleTemplateArgs(*TemplateParameters, *TemplateArgs);
Fariborz Jahanian57058532010-03-03 19:41:08 +00001148 }
Douglas Gregor35415f52010-05-25 17:04:15 +00001149 else if(NoFunction && (isa<FunctionDecl>(DC) || isa<ObjCMethodDecl>(DC)))
Fariborz Jahanian57058532010-03-03 19:41:08 +00001150 return;
Douglas Gregor35415f52010-05-25 17:04:15 +00001151 else if (const ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(DC))
1152 mangleObjCMethodName(Method);
Fariborz Jahanian57058532010-03-03 19:41:08 +00001153 else {
1154 manglePrefix(DC->getParent(), NoFunction);
Anders Carlsson2ee3fca2009-09-18 20:11:09 +00001155 mangleUnqualifiedName(cast<NamedDecl>(DC));
1156 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00001157
Anders Carlsson6862fc72009-09-17 04:16:28 +00001158 addSubstitution(cast<NamedDecl>(DC));
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001159}
1160
Douglas Gregor20f0cc72010-04-23 03:10:43 +00001161void CXXNameMangler::mangleTemplatePrefix(TemplateName Template) {
1162 // <template-prefix> ::= <prefix> <template unqualified-name>
1163 // ::= <template-param>
1164 // ::= <substitution>
1165 if (TemplateDecl *TD = Template.getAsTemplateDecl())
1166 return mangleTemplatePrefix(TD);
1167
1168 if (QualifiedTemplateName *Qualified = Template.getAsQualifiedTemplateName())
John McCalla0ce15c2011-04-24 08:23:24 +00001169 manglePrefix(Qualified->getQualifier());
Sean Huntc3021132010-05-05 15:23:54 +00001170
Douglas Gregor20f0cc72010-04-23 03:10:43 +00001171 if (OverloadedTemplateStorage *Overloaded
1172 = Template.getAsOverloadedTemplate()) {
Sean Huntc3021132010-05-05 15:23:54 +00001173 mangleUnqualifiedName(0, (*Overloaded->begin())->getDeclName(),
Douglas Gregor20f0cc72010-04-23 03:10:43 +00001174 UnknownArity);
1175 return;
1176 }
Sean Huntc3021132010-05-05 15:23:54 +00001177
Douglas Gregor20f0cc72010-04-23 03:10:43 +00001178 DependentTemplateName *Dependent = Template.getAsDependentTemplateName();
1179 assert(Dependent && "Unknown template name kind?");
John McCalla0ce15c2011-04-24 08:23:24 +00001180 manglePrefix(Dependent->getQualifier());
Douglas Gregor1e9268e2010-04-28 05:58:56 +00001181 mangleUnscopedTemplateName(Template);
Douglas Gregor20f0cc72010-04-23 03:10:43 +00001182}
1183
Anders Carlsson0fa6df42009-09-26 19:45:45 +00001184void CXXNameMangler::mangleTemplatePrefix(const TemplateDecl *ND) {
Anders Carlsson7482e242009-09-18 04:29:09 +00001185 // <template-prefix> ::= <prefix> <template unqualified-name>
1186 // ::= <template-param>
1187 // ::= <substitution>
Douglas Gregor32fb4e12010-02-05 20:45:00 +00001188 // <template-template-param> ::= <template-param>
1189 // <substitution>
Anders Carlsson7482e242009-09-18 04:29:09 +00001190
Anders Carlssonaeb85372009-09-26 22:18:22 +00001191 if (mangleSubstitution(ND))
1192 return;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00001193
Douglas Gregor32fb4e12010-02-05 20:45:00 +00001194 // <template-template-param> ::= <template-param>
1195 if (const TemplateTemplateParmDecl *TTP
1196 = dyn_cast<TemplateTemplateParmDecl>(ND)) {
1197 mangleTemplateParameter(TTP->getIndex());
1198 return;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001199 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00001200
Anders Carlssonaa73ab12009-09-18 18:47:07 +00001201 manglePrefix(ND->getDeclContext());
Anders Carlsson1668f202009-09-26 20:13:56 +00001202 mangleUnqualifiedName(ND->getTemplatedDecl());
Anders Carlssonaeb85372009-09-26 22:18:22 +00001203 addSubstitution(ND);
Anders Carlsson7482e242009-09-18 04:29:09 +00001204}
1205
John McCallb6f532e2010-07-14 06:43:17 +00001206/// Mangles a template name under the production <type>. Required for
1207/// template template arguments.
1208/// <type> ::= <class-enum-type>
1209/// ::= <template-param>
1210/// ::= <substitution>
1211void CXXNameMangler::mangleType(TemplateName TN) {
1212 if (mangleSubstitution(TN))
1213 return;
1214
1215 TemplateDecl *TD = 0;
1216
1217 switch (TN.getKind()) {
1218 case TemplateName::QualifiedTemplate:
1219 TD = TN.getAsQualifiedTemplateName()->getTemplateDecl();
1220 goto HaveDecl;
1221
1222 case TemplateName::Template:
1223 TD = TN.getAsTemplateDecl();
1224 goto HaveDecl;
1225
1226 HaveDecl:
1227 if (isa<TemplateTemplateParmDecl>(TD))
1228 mangleTemplateParameter(cast<TemplateTemplateParmDecl>(TD)->getIndex());
1229 else
1230 mangleName(TD);
1231 break;
1232
1233 case TemplateName::OverloadedTemplate:
1234 llvm_unreachable("can't mangle an overloaded template name as a <type>");
1235 break;
1236
1237 case TemplateName::DependentTemplate: {
1238 const DependentTemplateName *Dependent = TN.getAsDependentTemplateName();
1239 assert(Dependent->isIdentifier());
1240
1241 // <class-enum-type> ::= <name>
1242 // <name> ::= <nested-name>
John McCalla0ce15c2011-04-24 08:23:24 +00001243 mangleUnresolvedPrefix(Dependent->getQualifier(), 0);
John McCallb6f532e2010-07-14 06:43:17 +00001244 mangleSourceName(Dependent->getIdentifier());
1245 break;
1246 }
1247
Douglas Gregor1aee05d2011-01-15 06:45:20 +00001248 case TemplateName::SubstTemplateTemplateParmPack: {
1249 SubstTemplateTemplateParmPackStorage *SubstPack
1250 = TN.getAsSubstTemplateTemplateParmPack();
1251 mangleTemplateParameter(SubstPack->getParameterPack()->getIndex());
1252 break;
1253 }
John McCallb6f532e2010-07-14 06:43:17 +00001254 }
1255
1256 addSubstitution(TN);
1257}
1258
Mike Stump1eb44332009-09-09 15:08:12 +00001259void
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001260CXXNameMangler::mangleOperatorName(OverloadedOperatorKind OO, unsigned Arity) {
1261 switch (OO) {
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001262 // <operator-name> ::= nw # new
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001263 case OO_New: Out << "nw"; break;
1264 // ::= na # new[]
1265 case OO_Array_New: Out << "na"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001266 // ::= dl # delete
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001267 case OO_Delete: Out << "dl"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001268 // ::= da # delete[]
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001269 case OO_Array_Delete: Out << "da"; break;
1270 // ::= ps # + (unary)
John McCall5e1e89b2010-08-18 19:18:59 +00001271 // ::= pl # + (binary or unknown)
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001272 case OO_Plus:
Anders Carlsson8257d412009-12-22 06:36:32 +00001273 Out << (Arity == 1? "ps" : "pl"); break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001274 // ::= ng # - (unary)
John McCall5e1e89b2010-08-18 19:18:59 +00001275 // ::= mi # - (binary or unknown)
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001276 case OO_Minus:
Anders Carlsson8257d412009-12-22 06:36:32 +00001277 Out << (Arity == 1? "ng" : "mi"); break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001278 // ::= ad # & (unary)
John McCall5e1e89b2010-08-18 19:18:59 +00001279 // ::= an # & (binary or unknown)
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001280 case OO_Amp:
Anders Carlsson8257d412009-12-22 06:36:32 +00001281 Out << (Arity == 1? "ad" : "an"); break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001282 // ::= de # * (unary)
John McCall5e1e89b2010-08-18 19:18:59 +00001283 // ::= ml # * (binary or unknown)
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001284 case OO_Star:
John McCall5e1e89b2010-08-18 19:18:59 +00001285 // Use binary when unknown.
Anders Carlsson8257d412009-12-22 06:36:32 +00001286 Out << (Arity == 1? "de" : "ml"); break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001287 // ::= co # ~
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001288 case OO_Tilde: Out << "co"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001289 // ::= dv # /
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001290 case OO_Slash: Out << "dv"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001291 // ::= rm # %
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001292 case OO_Percent: Out << "rm"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001293 // ::= or # |
1294 case OO_Pipe: Out << "or"; break;
1295 // ::= eo # ^
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001296 case OO_Caret: Out << "eo"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001297 // ::= aS # =
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001298 case OO_Equal: Out << "aS"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001299 // ::= pL # +=
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001300 case OO_PlusEqual: Out << "pL"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001301 // ::= mI # -=
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001302 case OO_MinusEqual: Out << "mI"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001303 // ::= mL # *=
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001304 case OO_StarEqual: Out << "mL"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001305 // ::= dV # /=
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001306 case OO_SlashEqual: Out << "dV"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001307 // ::= rM # %=
1308 case OO_PercentEqual: Out << "rM"; break;
1309 // ::= aN # &=
1310 case OO_AmpEqual: Out << "aN"; break;
1311 // ::= oR # |=
1312 case OO_PipeEqual: Out << "oR"; break;
1313 // ::= eO # ^=
1314 case OO_CaretEqual: Out << "eO"; break;
1315 // ::= ls # <<
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001316 case OO_LessLess: Out << "ls"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001317 // ::= rs # >>
1318 case OO_GreaterGreater: Out << "rs"; break;
1319 // ::= lS # <<=
1320 case OO_LessLessEqual: Out << "lS"; break;
1321 // ::= rS # >>=
1322 case OO_GreaterGreaterEqual: Out << "rS"; break;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001323 // ::= eq # ==
1324 case OO_EqualEqual: Out << "eq"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001325 // ::= ne # !=
1326 case OO_ExclaimEqual: Out << "ne"; break;
1327 // ::= lt # <
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001328 case OO_Less: Out << "lt"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001329 // ::= gt # >
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001330 case OO_Greater: Out << "gt"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001331 // ::= le # <=
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001332 case OO_LessEqual: Out << "le"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001333 // ::= ge # >=
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001334 case OO_GreaterEqual: Out << "ge"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001335 // ::= nt # !
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001336 case OO_Exclaim: Out << "nt"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001337 // ::= aa # &&
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001338 case OO_AmpAmp: Out << "aa"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001339 // ::= oo # ||
1340 case OO_PipePipe: Out << "oo"; break;
1341 // ::= pp # ++
1342 case OO_PlusPlus: Out << "pp"; break;
1343 // ::= mm # --
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001344 case OO_MinusMinus: Out << "mm"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001345 // ::= cm # ,
1346 case OO_Comma: Out << "cm"; break;
1347 // ::= pm # ->*
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001348 case OO_ArrowStar: Out << "pm"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001349 // ::= pt # ->
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001350 case OO_Arrow: Out << "pt"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001351 // ::= cl # ()
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001352 case OO_Call: Out << "cl"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001353 // ::= ix # []
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001354 case OO_Subscript: Out << "ix"; break;
Anders Carlssone170ba72009-12-14 01:45:37 +00001355
1356 // ::= qu # ?
1357 // The conditional operator can't be overloaded, but we still handle it when
1358 // mangling expressions.
1359 case OO_Conditional: Out << "qu"; break;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001360
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001361 case OO_None:
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001362 case NUM_OVERLOADED_OPERATORS:
Mike Stump1eb44332009-09-09 15:08:12 +00001363 assert(false && "Not an overloaded operator");
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001364 break;
1365 }
1366}
1367
John McCall0953e762009-09-24 19:53:00 +00001368void CXXNameMangler::mangleQualifiers(Qualifiers Quals) {
Mike Stump1eb44332009-09-09 15:08:12 +00001369 // <CV-qualifiers> ::= [r] [V] [K] # restrict (C99), volatile, const
John McCall0953e762009-09-24 19:53:00 +00001370 if (Quals.hasRestrict())
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001371 Out << 'r';
John McCall0953e762009-09-24 19:53:00 +00001372 if (Quals.hasVolatile())
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001373 Out << 'V';
John McCall0953e762009-09-24 19:53:00 +00001374 if (Quals.hasConst())
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001375 Out << 'K';
John McCall0953e762009-09-24 19:53:00 +00001376
Douglas Gregor56079f72010-06-14 23:15:08 +00001377 if (Quals.hasAddressSpace()) {
1378 // Extension:
1379 //
1380 // <type> ::= U <address-space-number>
1381 //
1382 // where <address-space-number> is a source name consisting of 'AS'
1383 // followed by the address space <number>.
1384 llvm::SmallString<64> ASString;
1385 ASString = "AS" + llvm::utostr_32(Quals.getAddressSpace());
1386 Out << 'U' << ASString.size() << ASString;
1387 }
1388
John McCall0953e762009-09-24 19:53:00 +00001389 // FIXME: For now, just drop all extension qualifiers on the floor.
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001390}
1391
Douglas Gregor0a9a6d62011-01-26 17:36:28 +00001392void CXXNameMangler::mangleRefQualifier(RefQualifierKind RefQualifier) {
1393 // <ref-qualifier> ::= R # lvalue reference
1394 // ::= O # rvalue-reference
1395 // Proposal to Itanium C++ ABI list on 1/26/11
1396 switch (RefQualifier) {
1397 case RQ_None:
1398 break;
1399
1400 case RQ_LValue:
1401 Out << 'R';
1402 break;
1403
1404 case RQ_RValue:
1405 Out << 'O';
1406 break;
1407 }
1408}
1409
Anders Carlsson7b06f6c2009-12-10 03:14:39 +00001410void CXXNameMangler::mangleObjCMethodName(const ObjCMethodDecl *MD) {
Rafael Espindolaf0be9792011-02-11 02:52:17 +00001411 Context.mangleObjCMethodName(MD, Out);
Anders Carlsson7b06f6c2009-12-10 03:14:39 +00001412}
1413
John McCallb47f7482011-01-26 20:05:40 +00001414void CXXNameMangler::mangleType(QualType nonCanon) {
Anders Carlsson4843e582009-03-10 17:07:44 +00001415 // Only operate on the canonical type!
John McCallb47f7482011-01-26 20:05:40 +00001416 QualType canon = nonCanon.getCanonicalType();
Anders Carlsson4843e582009-03-10 17:07:44 +00001417
John McCallb47f7482011-01-26 20:05:40 +00001418 SplitQualType split = canon.split();
1419 Qualifiers quals = split.second;
1420 const Type *ty = split.first;
1421
1422 bool isSubstitutable = quals || !isa<BuiltinType>(ty);
1423 if (isSubstitutable && mangleSubstitution(canon))
Anders Carlsson76967372009-09-17 00:43:46 +00001424 return;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001425
John McCallb47f7482011-01-26 20:05:40 +00001426 // If we're mangling a qualified array type, push the qualifiers to
1427 // the element type.
1428 if (quals && isa<ArrayType>(ty)) {
1429 ty = Context.getASTContext().getAsArrayType(canon);
1430 quals = Qualifiers();
1431
1432 // Note that we don't update canon: we want to add the
1433 // substitution at the canonical type.
1434 }
1435
1436 if (quals) {
1437 mangleQualifiers(quals);
John McCall0953e762009-09-24 19:53:00 +00001438 // Recurse: even if the qualified type isn't yet substitutable,
1439 // the unqualified type might be.
John McCallb47f7482011-01-26 20:05:40 +00001440 mangleType(QualType(ty, 0));
Anders Carlsson76967372009-09-17 00:43:46 +00001441 } else {
John McCallb47f7482011-01-26 20:05:40 +00001442 switch (ty->getTypeClass()) {
John McCallefe6aee2009-09-05 07:56:18 +00001443#define ABSTRACT_TYPE(CLASS, PARENT)
1444#define NON_CANONICAL_TYPE(CLASS, PARENT) \
Anders Carlsson76967372009-09-17 00:43:46 +00001445 case Type::CLASS: \
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +00001446 llvm_unreachable("can't mangle non-canonical type " #CLASS "Type"); \
Anders Carlsson76967372009-09-17 00:43:46 +00001447 return;
John McCallefe6aee2009-09-05 07:56:18 +00001448#define TYPE(CLASS, PARENT) \
Anders Carlsson76967372009-09-17 00:43:46 +00001449 case Type::CLASS: \
John McCallb47f7482011-01-26 20:05:40 +00001450 mangleType(static_cast<const CLASS##Type*>(ty)); \
Anders Carlsson76967372009-09-17 00:43:46 +00001451 break;
John McCallefe6aee2009-09-05 07:56:18 +00001452#include "clang/AST/TypeNodes.def"
Anders Carlsson76967372009-09-17 00:43:46 +00001453 }
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001454 }
Anders Carlsson76967372009-09-17 00:43:46 +00001455
1456 // Add the substitution.
John McCallb47f7482011-01-26 20:05:40 +00001457 if (isSubstitutable)
1458 addSubstitution(canon);
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001459}
1460
Douglas Gregor1b12a3b2010-05-26 05:11:13 +00001461void CXXNameMangler::mangleNameOrStandardSubstitution(const NamedDecl *ND) {
1462 if (!mangleStandardSubstitution(ND))
1463 mangleName(ND);
1464}
1465
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001466void CXXNameMangler::mangleType(const BuiltinType *T) {
John McCallefe6aee2009-09-05 07:56:18 +00001467 // <type> ::= <builtin-type>
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001468 // <builtin-type> ::= v # void
1469 // ::= w # wchar_t
1470 // ::= b # bool
1471 // ::= c # char
1472 // ::= a # signed char
1473 // ::= h # unsigned char
1474 // ::= s # short
1475 // ::= t # unsigned short
1476 // ::= i # int
1477 // ::= j # unsigned int
1478 // ::= l # long
1479 // ::= m # unsigned long
1480 // ::= x # long long, __int64
1481 // ::= y # unsigned long long, __int64
1482 // ::= n # __int128
1483 // UNSUPPORTED: ::= o # unsigned __int128
1484 // ::= f # float
1485 // ::= d # double
1486 // ::= e # long double, __float80
1487 // UNSUPPORTED: ::= g # __float128
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001488 // UNSUPPORTED: ::= Dd # IEEE 754r decimal floating point (64 bits)
1489 // UNSUPPORTED: ::= De # IEEE 754r decimal floating point (128 bits)
1490 // UNSUPPORTED: ::= Df # IEEE 754r decimal floating point (32 bits)
1491 // UNSUPPORTED: ::= Dh # IEEE 754r half-precision floating point (16 bits)
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001492 // ::= Di # char32_t
1493 // ::= Ds # char16_t
Anders Carlssone2923682010-11-04 04:31:32 +00001494 // ::= Dn # std::nullptr_t (i.e., decltype(nullptr))
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001495 // ::= u <source-name> # vendor extended type
1496 switch (T->getKind()) {
1497 case BuiltinType::Void: Out << 'v'; break;
1498 case BuiltinType::Bool: Out << 'b'; break;
1499 case BuiltinType::Char_U: case BuiltinType::Char_S: Out << 'c'; break;
1500 case BuiltinType::UChar: Out << 'h'; break;
1501 case BuiltinType::UShort: Out << 't'; break;
1502 case BuiltinType::UInt: Out << 'j'; break;
1503 case BuiltinType::ULong: Out << 'm'; break;
1504 case BuiltinType::ULongLong: Out << 'y'; break;
Chris Lattner2df9ced2009-04-30 02:43:43 +00001505 case BuiltinType::UInt128: Out << 'o'; break;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001506 case BuiltinType::SChar: Out << 'a'; break;
Chris Lattner3f59c972010-12-25 23:25:43 +00001507 case BuiltinType::WChar_S:
1508 case BuiltinType::WChar_U: Out << 'w'; break;
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001509 case BuiltinType::Char16: Out << "Ds"; break;
1510 case BuiltinType::Char32: Out << "Di"; break;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001511 case BuiltinType::Short: Out << 's'; break;
1512 case BuiltinType::Int: Out << 'i'; break;
1513 case BuiltinType::Long: Out << 'l'; break;
1514 case BuiltinType::LongLong: Out << 'x'; break;
Chris Lattner2df9ced2009-04-30 02:43:43 +00001515 case BuiltinType::Int128: Out << 'n'; break;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001516 case BuiltinType::Float: Out << 'f'; break;
1517 case BuiltinType::Double: Out << 'd'; break;
1518 case BuiltinType::LongDouble: Out << 'e'; break;
Anders Carlssone2923682010-11-04 04:31:32 +00001519 case BuiltinType::NullPtr: Out << "Dn"; break;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001520
1521 case BuiltinType::Overload:
1522 case BuiltinType::Dependent:
John McCall864c0412011-04-26 20:42:42 +00001523 case BuiltinType::BoundMember:
John McCall1de4d4e2011-04-07 08:22:57 +00001524 case BuiltinType::UnknownAny:
John McCallfb44de92011-05-01 22:35:37 +00001525 llvm_unreachable("mangling a placeholder type");
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001526 break;
Steve Naroff9533a7f2009-07-22 17:14:51 +00001527 case BuiltinType::ObjCId: Out << "11objc_object"; break;
1528 case BuiltinType::ObjCClass: Out << "10objc_class"; break;
Fariborz Jahanian13dcd002009-11-21 19:53:08 +00001529 case BuiltinType::ObjCSel: Out << "13objc_selector"; break;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001530 }
1531}
1532
John McCallefe6aee2009-09-05 07:56:18 +00001533// <type> ::= <function-type>
1534// <function-type> ::= F [Y] <bare-function-type> E
1535void CXXNameMangler::mangleType(const FunctionProtoType *T) {
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001536 Out << 'F';
Mike Stumpf5408fe2009-05-16 07:57:57 +00001537 // FIXME: We don't have enough information in the AST to produce the 'Y'
1538 // encoding for extern "C" function types.
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001539 mangleBareFunctionType(T, /*MangleReturnType=*/true);
1540 Out << 'E';
1541}
John McCallefe6aee2009-09-05 07:56:18 +00001542void CXXNameMangler::mangleType(const FunctionNoProtoType *T) {
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +00001543 llvm_unreachable("Can't mangle K&R function prototypes");
John McCallefe6aee2009-09-05 07:56:18 +00001544}
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001545void CXXNameMangler::mangleBareFunctionType(const FunctionType *T,
1546 bool MangleReturnType) {
John McCallefe6aee2009-09-05 07:56:18 +00001547 // We should never be mangling something without a prototype.
1548 const FunctionProtoType *Proto = cast<FunctionProtoType>(T);
1549
John McCallfb44de92011-05-01 22:35:37 +00001550 // Record that we're in a function type. See mangleFunctionParam
1551 // for details on what we're trying to achieve here.
1552 FunctionTypeDepthState saved = FunctionTypeDepth.push();
1553
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001554 // <bare-function-type> ::= <signature type>+
John McCallfb44de92011-05-01 22:35:37 +00001555 if (MangleReturnType) {
1556 FunctionTypeDepth.enterResultType();
John McCallefe6aee2009-09-05 07:56:18 +00001557 mangleType(Proto->getResultType());
John McCallfb44de92011-05-01 22:35:37 +00001558 FunctionTypeDepth.leaveResultType();
1559 }
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001560
Anders Carlsson93296682010-06-02 04:40:13 +00001561 if (Proto->getNumArgs() == 0 && !Proto->isVariadic()) {
Eli Friedmana7e68452010-08-22 01:00:03 +00001562 // <builtin-type> ::= v # void
Anders Carlssonc6c91bc2009-04-01 00:15:23 +00001563 Out << 'v';
John McCallfb44de92011-05-01 22:35:37 +00001564
1565 FunctionTypeDepth.pop(saved);
Anders Carlssonc6c91bc2009-04-01 00:15:23 +00001566 return;
1567 }
Mike Stump1eb44332009-09-09 15:08:12 +00001568
Douglas Gregor72564e72009-02-26 23:50:07 +00001569 for (FunctionProtoType::arg_type_iterator Arg = Proto->arg_type_begin(),
Mike Stump1eb44332009-09-09 15:08:12 +00001570 ArgEnd = Proto->arg_type_end();
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001571 Arg != ArgEnd; ++Arg)
1572 mangleType(*Arg);
Douglas Gregor219cc612009-02-13 01:28:03 +00001573
John McCallfb44de92011-05-01 22:35:37 +00001574 FunctionTypeDepth.pop(saved);
1575
Douglas Gregor219cc612009-02-13 01:28:03 +00001576 // <builtin-type> ::= z # ellipsis
1577 if (Proto->isVariadic())
1578 Out << 'z';
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001579}
1580
John McCallefe6aee2009-09-05 07:56:18 +00001581// <type> ::= <class-enum-type>
Mike Stump1eb44332009-09-09 15:08:12 +00001582// <class-enum-type> ::= <name>
John McCalled976492009-12-04 22:46:56 +00001583void CXXNameMangler::mangleType(const UnresolvedUsingType *T) {
1584 mangleName(T->getDecl());
1585}
1586
1587// <type> ::= <class-enum-type>
1588// <class-enum-type> ::= <name>
John McCallefe6aee2009-09-05 07:56:18 +00001589void CXXNameMangler::mangleType(const EnumType *T) {
1590 mangleType(static_cast<const TagType*>(T));
1591}
1592void CXXNameMangler::mangleType(const RecordType *T) {
1593 mangleType(static_cast<const TagType*>(T));
1594}
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001595void CXXNameMangler::mangleType(const TagType *T) {
Eli Friedmanecb7e932009-12-11 18:00:57 +00001596 mangleName(T->getDecl());
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001597}
1598
John McCallefe6aee2009-09-05 07:56:18 +00001599// <type> ::= <array-type>
1600// <array-type> ::= A <positive dimension number> _ <element type>
1601// ::= A [<dimension expression>] _ <element type>
1602void CXXNameMangler::mangleType(const ConstantArrayType *T) {
1603 Out << 'A' << T->getSize() << '_';
1604 mangleType(T->getElementType());
1605}
1606void CXXNameMangler::mangleType(const VariableArrayType *T) {
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001607 Out << 'A';
Fariborz Jahanian7281d1f2010-11-02 16:54:00 +00001608 // decayed vla types (size 0) will just be skipped.
1609 if (T->getSizeExpr())
1610 mangleExpression(T->getSizeExpr());
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001611 Out << '_';
1612 mangleType(T->getElementType());
1613}
John McCallefe6aee2009-09-05 07:56:18 +00001614void CXXNameMangler::mangleType(const DependentSizedArrayType *T) {
1615 Out << 'A';
1616 mangleExpression(T->getSizeExpr());
1617 Out << '_';
1618 mangleType(T->getElementType());
1619}
1620void CXXNameMangler::mangleType(const IncompleteArrayType *T) {
Nick Lewycky271b6652010-09-05 03:40:33 +00001621 Out << "A_";
John McCallefe6aee2009-09-05 07:56:18 +00001622 mangleType(T->getElementType());
1623}
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001624
John McCallefe6aee2009-09-05 07:56:18 +00001625// <type> ::= <pointer-to-member-type>
1626// <pointer-to-member-type> ::= M <class type> <member type>
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001627void CXXNameMangler::mangleType(const MemberPointerType *T) {
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001628 Out << 'M';
1629 mangleType(QualType(T->getClass(), 0));
Anders Carlsson0e650012009-05-17 17:41:20 +00001630 QualType PointeeType = T->getPointeeType();
1631 if (const FunctionProtoType *FPT = dyn_cast<FunctionProtoType>(PointeeType)) {
John McCall0953e762009-09-24 19:53:00 +00001632 mangleQualifiers(Qualifiers::fromCVRMask(FPT->getTypeQuals()));
Douglas Gregor0a9a6d62011-01-26 17:36:28 +00001633 mangleRefQualifier(FPT->getRefQualifier());
Anders Carlsson0e650012009-05-17 17:41:20 +00001634 mangleType(FPT);
Anders Carlsson9d85b722010-06-02 04:29:50 +00001635
1636 // Itanium C++ ABI 5.1.8:
1637 //
1638 // The type of a non-static member function is considered to be different,
1639 // for the purposes of substitution, from the type of a namespace-scope or
1640 // static member function whose type appears similar. The types of two
1641 // non-static member functions are considered to be different, for the
1642 // purposes of substitution, if the functions are members of different
1643 // classes. In other words, for the purposes of substitution, the class of
1644 // which the function is a member is considered part of the type of
1645 // function.
1646
1647 // We increment the SeqID here to emulate adding an entry to the
1648 // substitution table. We can't actually add it because we don't want this
1649 // particular function type to be substituted.
1650 ++SeqID;
Mike Stump1eb44332009-09-09 15:08:12 +00001651 } else
Anders Carlsson0e650012009-05-17 17:41:20 +00001652 mangleType(PointeeType);
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001653}
1654
John McCallefe6aee2009-09-05 07:56:18 +00001655// <type> ::= <template-param>
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001656void CXXNameMangler::mangleType(const TemplateTypeParmType *T) {
Anders Carlsson0ccdf8d2009-09-27 00:38:53 +00001657 mangleTemplateParameter(T->getIndex());
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001658}
1659
Douglas Gregorc3069d62011-01-14 02:55:32 +00001660// <type> ::= <template-param>
1661void CXXNameMangler::mangleType(const SubstTemplateTypeParmPackType *T) {
1662 mangleTemplateParameter(T->getReplacedParameter()->getIndex());
1663}
1664
John McCallefe6aee2009-09-05 07:56:18 +00001665// <type> ::= P <type> # pointer-to
1666void CXXNameMangler::mangleType(const PointerType *T) {
1667 Out << 'P';
1668 mangleType(T->getPointeeType());
1669}
1670void CXXNameMangler::mangleType(const ObjCObjectPointerType *T) {
1671 Out << 'P';
1672 mangleType(T->getPointeeType());
1673}
1674
1675// <type> ::= R <type> # reference-to
1676void CXXNameMangler::mangleType(const LValueReferenceType *T) {
1677 Out << 'R';
1678 mangleType(T->getPointeeType());
1679}
1680
1681// <type> ::= O <type> # rvalue reference-to (C++0x)
1682void CXXNameMangler::mangleType(const RValueReferenceType *T) {
1683 Out << 'O';
1684 mangleType(T->getPointeeType());
1685}
1686
1687// <type> ::= C <type> # complex pair (C 2000)
1688void CXXNameMangler::mangleType(const ComplexType *T) {
1689 Out << 'C';
1690 mangleType(T->getElementType());
1691}
1692
Bob Wilsonc7df92d2010-11-12 17:24:43 +00001693// ARM's ABI for Neon vector types specifies that they should be mangled as
Bob Wilson57147a82010-11-16 00:32:18 +00001694// if they are structs (to match ARM's initial implementation). The
1695// vector type must be one of the special types predefined by ARM.
1696void CXXNameMangler::mangleNeonVectorType(const VectorType *T) {
Bob Wilsonc7df92d2010-11-12 17:24:43 +00001697 QualType EltType = T->getElementType();
Bob Wilson57147a82010-11-16 00:32:18 +00001698 assert(EltType->isBuiltinType() && "Neon vector element not a BuiltinType");
Bob Wilsonc7df92d2010-11-12 17:24:43 +00001699 const char *EltName = 0;
Bob Wilson491328c2010-11-12 17:24:46 +00001700 if (T->getVectorKind() == VectorType::NeonPolyVector) {
1701 switch (cast<BuiltinType>(EltType)->getKind()) {
Bob Wilson4cfaa5d2010-11-12 17:24:49 +00001702 case BuiltinType::SChar: EltName = "poly8_t"; break;
1703 case BuiltinType::Short: EltName = "poly16_t"; break;
Bob Wilson57147a82010-11-16 00:32:18 +00001704 default: llvm_unreachable("unexpected Neon polynomial vector element type");
Bob Wilson491328c2010-11-12 17:24:46 +00001705 }
1706 } else {
1707 switch (cast<BuiltinType>(EltType)->getKind()) {
Bob Wilson4cfaa5d2010-11-12 17:24:49 +00001708 case BuiltinType::SChar: EltName = "int8_t"; break;
1709 case BuiltinType::UChar: EltName = "uint8_t"; break;
1710 case BuiltinType::Short: EltName = "int16_t"; break;
1711 case BuiltinType::UShort: EltName = "uint16_t"; break;
1712 case BuiltinType::Int: EltName = "int32_t"; break;
1713 case BuiltinType::UInt: EltName = "uint32_t"; break;
1714 case BuiltinType::LongLong: EltName = "int64_t"; break;
1715 case BuiltinType::ULongLong: EltName = "uint64_t"; break;
1716 case BuiltinType::Float: EltName = "float32_t"; break;
Bob Wilson57147a82010-11-16 00:32:18 +00001717 default: llvm_unreachable("unexpected Neon vector element type");
Bob Wilson491328c2010-11-12 17:24:46 +00001718 }
Bob Wilsonc7df92d2010-11-12 17:24:43 +00001719 }
1720 const char *BaseName = 0;
Bob Wilson4cfaa5d2010-11-12 17:24:49 +00001721 unsigned BitSize = (T->getNumElements() *
Bob Wilson3a723022010-11-16 00:32:12 +00001722 getASTContext().getTypeSize(EltType));
Bob Wilsonc7df92d2010-11-12 17:24:43 +00001723 if (BitSize == 64)
1724 BaseName = "__simd64_";
Bob Wilson57147a82010-11-16 00:32:18 +00001725 else {
1726 assert(BitSize == 128 && "Neon vector type not 64 or 128 bits");
Bob Wilsonc7df92d2010-11-12 17:24:43 +00001727 BaseName = "__simd128_";
Bob Wilson57147a82010-11-16 00:32:18 +00001728 }
Bob Wilsonc7df92d2010-11-12 17:24:43 +00001729 Out << strlen(BaseName) + strlen(EltName);
1730 Out << BaseName << EltName;
Bob Wilsonc7df92d2010-11-12 17:24:43 +00001731}
1732
John McCallefe6aee2009-09-05 07:56:18 +00001733// GNU extension: vector types
Chris Lattner788b0fd2010-06-23 06:00:24 +00001734// <type> ::= <vector-type>
1735// <vector-type> ::= Dv <positive dimension number> _
1736// <extended element type>
1737// ::= Dv [<dimension expression>] _ <element type>
1738// <extended element type> ::= <element type>
1739// ::= p # AltiVec vector pixel
John McCallefe6aee2009-09-05 07:56:18 +00001740void CXXNameMangler::mangleType(const VectorType *T) {
Bob Wilson491328c2010-11-12 17:24:46 +00001741 if ((T->getVectorKind() == VectorType::NeonVector ||
Bob Wilson57147a82010-11-16 00:32:18 +00001742 T->getVectorKind() == VectorType::NeonPolyVector)) {
1743 mangleNeonVectorType(T);
Bob Wilsonc7df92d2010-11-12 17:24:43 +00001744 return;
Bob Wilson57147a82010-11-16 00:32:18 +00001745 }
Nick Lewycky0e5f0672010-03-26 07:18:04 +00001746 Out << "Dv" << T->getNumElements() << '_';
Bob Wilsone86d78c2010-11-10 21:56:12 +00001747 if (T->getVectorKind() == VectorType::AltiVecPixel)
Chris Lattner788b0fd2010-06-23 06:00:24 +00001748 Out << 'p';
Bob Wilsone86d78c2010-11-10 21:56:12 +00001749 else if (T->getVectorKind() == VectorType::AltiVecBool)
Chris Lattner788b0fd2010-06-23 06:00:24 +00001750 Out << 'b';
1751 else
1752 mangleType(T->getElementType());
John McCallefe6aee2009-09-05 07:56:18 +00001753}
1754void CXXNameMangler::mangleType(const ExtVectorType *T) {
1755 mangleType(static_cast<const VectorType*>(T));
1756}
1757void CXXNameMangler::mangleType(const DependentSizedExtVectorType *T) {
Nick Lewycky0e5f0672010-03-26 07:18:04 +00001758 Out << "Dv";
1759 mangleExpression(T->getSizeExpr());
1760 Out << '_';
John McCallefe6aee2009-09-05 07:56:18 +00001761 mangleType(T->getElementType());
1762}
1763
Douglas Gregor7536dd52010-12-20 02:24:11 +00001764void CXXNameMangler::mangleType(const PackExpansionType *T) {
Douglas Gregor4fc48662011-01-13 16:39:34 +00001765 // <type> ::= Dp <type> # pack expansion (C++0x)
Douglas Gregor255c2692011-01-13 17:44:36 +00001766 Out << "Dp";
Douglas Gregor7536dd52010-12-20 02:24:11 +00001767 mangleType(T->getPattern());
1768}
1769
Anders Carlssona40c5e42009-03-07 22:03:21 +00001770void CXXNameMangler::mangleType(const ObjCInterfaceType *T) {
1771 mangleSourceName(T->getDecl()->getIdentifier());
1772}
1773
John McCallc12c5bb2010-05-15 11:32:37 +00001774void CXXNameMangler::mangleType(const ObjCObjectType *T) {
John McCallc00c1f62010-05-15 17:06:29 +00001775 // We don't allow overloading by different protocol qualification,
1776 // so mangling them isn't necessary.
John McCallc12c5bb2010-05-15 11:32:37 +00001777 mangleType(T->getBaseType());
1778}
1779
John McCallefe6aee2009-09-05 07:56:18 +00001780void CXXNameMangler::mangleType(const BlockPointerType *T) {
Anders Carlssonf28c6872009-12-23 22:31:44 +00001781 Out << "U13block_pointer";
1782 mangleType(T->getPointeeType());
John McCallefe6aee2009-09-05 07:56:18 +00001783}
1784
John McCall31f17ec2010-04-27 00:57:59 +00001785void CXXNameMangler::mangleType(const InjectedClassNameType *T) {
1786 // Mangle injected class name types as if the user had written the
1787 // specialization out fully. It may not actually be possible to see
1788 // this mangling, though.
1789 mangleType(T->getInjectedSpecializationType());
1790}
1791
John McCallefe6aee2009-09-05 07:56:18 +00001792void CXXNameMangler::mangleType(const TemplateSpecializationType *T) {
Douglas Gregor1e9268e2010-04-28 05:58:56 +00001793 if (TemplateDecl *TD = T->getTemplateName().getAsTemplateDecl()) {
1794 mangleName(TD, T->getArgs(), T->getNumArgs());
1795 } else {
1796 if (mangleSubstitution(QualType(T, 0)))
1797 return;
Sean Huntc3021132010-05-05 15:23:54 +00001798
Douglas Gregor1e9268e2010-04-28 05:58:56 +00001799 mangleTemplatePrefix(T->getTemplateName());
Sean Huntc3021132010-05-05 15:23:54 +00001800
Douglas Gregor1e9268e2010-04-28 05:58:56 +00001801 // FIXME: GCC does not appear to mangle the template arguments when
1802 // the template in question is a dependent template name. Should we
1803 // emulate that badness?
1804 mangleTemplateArgs(T->getTemplateName(), T->getArgs(), T->getNumArgs());
1805 addSubstitution(QualType(T, 0));
1806 }
John McCallefe6aee2009-09-05 07:56:18 +00001807}
1808
Douglas Gregor4714c122010-03-31 17:34:00 +00001809void CXXNameMangler::mangleType(const DependentNameType *T) {
Anders Carlssonae352482009-09-26 02:26:02 +00001810 // Typename types are always nested
1811 Out << 'N';
John McCalla0ce15c2011-04-24 08:23:24 +00001812 manglePrefix(T->getQualifier());
John McCall33500952010-06-11 00:33:02 +00001813 mangleSourceName(T->getIdentifier());
1814 Out << 'E';
1815}
John McCall6ab30e02010-06-09 07:26:17 +00001816
John McCall33500952010-06-11 00:33:02 +00001817void CXXNameMangler::mangleType(const DependentTemplateSpecializationType *T) {
Douglas Gregoraa2187d2011-02-28 00:04:36 +00001818 // Dependently-scoped template types are nested if they have a prefix.
John McCall33500952010-06-11 00:33:02 +00001819 Out << 'N';
1820
1821 // TODO: avoid making this TemplateName.
1822 TemplateName Prefix =
1823 getASTContext().getDependentTemplateName(T->getQualifier(),
1824 T->getIdentifier());
1825 mangleTemplatePrefix(Prefix);
1826
1827 // FIXME: GCC does not appear to mangle the template arguments when
1828 // the template in question is a dependent template name. Should we
1829 // emulate that badness?
1830 mangleTemplateArgs(Prefix, T->getArgs(), T->getNumArgs());
Anders Carlssonae352482009-09-26 02:26:02 +00001831 Out << 'E';
John McCallefe6aee2009-09-05 07:56:18 +00001832}
1833
John McCallad5e7382010-03-01 23:49:17 +00001834void CXXNameMangler::mangleType(const TypeOfType *T) {
1835 // FIXME: this is pretty unsatisfactory, but there isn't an obvious
1836 // "extension with parameters" mangling.
1837 Out << "u6typeof";
1838}
1839
1840void CXXNameMangler::mangleType(const TypeOfExprType *T) {
1841 // FIXME: this is pretty unsatisfactory, but there isn't an obvious
1842 // "extension with parameters" mangling.
1843 Out << "u6typeof";
1844}
1845
1846void CXXNameMangler::mangleType(const DecltypeType *T) {
1847 Expr *E = T->getUnderlyingExpr();
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001848
John McCallad5e7382010-03-01 23:49:17 +00001849 // type ::= Dt <expression> E # decltype of an id-expression
1850 // # or class member access
1851 // ::= DT <expression> E # decltype of an expression
1852
1853 // This purports to be an exhaustive list of id-expressions and
1854 // class member accesses. Note that we do not ignore parentheses;
1855 // parentheses change the semantics of decltype for these
1856 // expressions (and cause the mangler to use the other form).
1857 if (isa<DeclRefExpr>(E) ||
1858 isa<MemberExpr>(E) ||
1859 isa<UnresolvedLookupExpr>(E) ||
1860 isa<DependentScopeDeclRefExpr>(E) ||
1861 isa<CXXDependentScopeMemberExpr>(E) ||
1862 isa<UnresolvedMemberExpr>(E))
1863 Out << "Dt";
1864 else
1865 Out << "DT";
1866 mangleExpression(E);
1867 Out << 'E';
1868}
1869
Richard Smith34b41d92011-02-20 03:19:35 +00001870void CXXNameMangler::mangleType(const AutoType *T) {
1871 QualType D = T->getDeducedType();
Richard Smith967ecd32011-02-21 20:10:02 +00001872 // <builtin-type> ::= Da # dependent auto
1873 if (D.isNull())
1874 Out << "Da";
1875 else
1876 mangleType(D);
Richard Smith34b41d92011-02-20 03:19:35 +00001877}
1878
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001879void CXXNameMangler::mangleIntegerLiteral(QualType T,
Anders Carlssone170ba72009-12-14 01:45:37 +00001880 const llvm::APSInt &Value) {
1881 // <expr-primary> ::= L <type> <value number> E # integer literal
1882 Out << 'L';
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001883
Anders Carlssone170ba72009-12-14 01:45:37 +00001884 mangleType(T);
1885 if (T->isBooleanType()) {
1886 // Boolean values are encoded as 0/1.
1887 Out << (Value.getBoolValue() ? '1' : '0');
1888 } else {
John McCall0512e482010-07-14 04:20:34 +00001889 mangleNumber(Value);
Anders Carlssone170ba72009-12-14 01:45:37 +00001890 }
1891 Out << 'E';
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001892
Anders Carlssone170ba72009-12-14 01:45:37 +00001893}
1894
John McCall2f27bf82010-02-04 02:56:29 +00001895/// Mangles a member expression. Implicit accesses are not handled,
1896/// but that should be okay, because you shouldn't be able to
1897/// make an implicit access in a function template declaration.
John McCalla0ce15c2011-04-24 08:23:24 +00001898void CXXNameMangler::mangleMemberExpr(const Expr *base,
1899 bool isArrow,
1900 NestedNameSpecifier *qualifier,
1901 NamedDecl *firstQualifierLookup,
1902 DeclarationName member,
1903 unsigned arity) {
1904 // <expression> ::= dt <expression> <unresolved-name>
1905 // ::= pt <expression> <unresolved-name>
1906 Out << (isArrow ? "pt" : "dt");
1907 mangleExpression(base);
1908 mangleUnresolvedName(qualifier, firstQualifierLookup, member, arity);
John McCall2f27bf82010-02-04 02:56:29 +00001909}
1910
John McCall5a7e6f72011-04-28 02:52:03 +00001911/// Look at the callee of the given call expression and determine if
1912/// it's a parenthesized id-expression which would have triggered ADL
1913/// otherwise.
1914static bool isParenthesizedADLCallee(const CallExpr *call) {
1915 const Expr *callee = call->getCallee();
1916 const Expr *fn = callee->IgnoreParens();
1917
1918 // Must be parenthesized. IgnoreParens() skips __extension__ nodes,
1919 // too, but for those to appear in the callee, it would have to be
1920 // parenthesized.
1921 if (callee == fn) return false;
1922
1923 // Must be an unresolved lookup.
1924 const UnresolvedLookupExpr *lookup = dyn_cast<UnresolvedLookupExpr>(fn);
1925 if (!lookup) return false;
1926
1927 assert(!lookup->requiresADL());
1928
1929 // Must be an unqualified lookup.
1930 if (lookup->getQualifier()) return false;
1931
1932 // Must not have found a class member. Note that if one is a class
1933 // member, they're all class members.
1934 if (lookup->getNumDecls() > 0 &&
1935 (*lookup->decls_begin())->isCXXClassMember())
1936 return false;
1937
1938 // Otherwise, ADL would have been triggered.
1939 return true;
1940}
1941
John McCall5e1e89b2010-08-18 19:18:59 +00001942void CXXNameMangler::mangleExpression(const Expr *E, unsigned Arity) {
Anders Carlssond553f8c2009-09-21 01:21:10 +00001943 // <expression> ::= <unary operator-name> <expression>
John McCall09cc1412010-02-03 00:55:45 +00001944 // ::= <binary operator-name> <expression> <expression>
1945 // ::= <trinary operator-name> <expression> <expression> <expression>
Anders Carlssond553f8c2009-09-21 01:21:10 +00001946 // ::= cv <type> expression # conversion with one argument
1947 // ::= cv <type> _ <expression>* E # conversion with a different number of arguments
Eli Friedmana7e68452010-08-22 01:00:03 +00001948 // ::= st <type> # sizeof (a type)
Anders Carlssond553f8c2009-09-21 01:21:10 +00001949 // ::= at <type> # alignof (a type)
1950 // ::= <template-param>
1951 // ::= <function-param>
1952 // ::= sr <type> <unqualified-name> # dependent name
1953 // ::= sr <type> <unqualified-name> <template-args> # dependent template-id
1954 // ::= sZ <template-param> # size of a parameter pack
Douglas Gregor4fc48662011-01-13 16:39:34 +00001955 // ::= sZ <function-param> # size of a function parameter pack
John McCall09cc1412010-02-03 00:55:45 +00001956 // ::= <expr-primary>
John McCall1dd73832010-02-04 01:42:13 +00001957 // <expr-primary> ::= L <type> <value number> E # integer literal
1958 // ::= L <type <value float> E # floating literal
1959 // ::= L <mangled-name> E # external name
Anders Carlssond553f8c2009-09-21 01:21:10 +00001960 switch (E->getStmtClass()) {
John McCall6ae1f352010-04-09 22:26:14 +00001961 case Expr::NoStmtClass:
John McCall63c00d72011-02-09 08:16:59 +00001962#define ABSTRACT_STMT(Type)
John McCall6ae1f352010-04-09 22:26:14 +00001963#define EXPR(Type, Base)
1964#define STMT(Type, Base) \
1965 case Expr::Type##Class:
Sean Hunt4bfe1962010-05-05 15:24:00 +00001966#include "clang/AST/StmtNodes.inc"
John McCall0512e482010-07-14 04:20:34 +00001967 // fallthrough
1968
1969 // These all can only appear in local or variable-initialization
1970 // contexts and so should never appear in a mangling.
1971 case Expr::AddrLabelExprClass:
1972 case Expr::BlockDeclRefExprClass:
1973 case Expr::CXXThisExprClass:
1974 case Expr::DesignatedInitExprClass:
1975 case Expr::ImplicitValueInitExprClass:
1976 case Expr::InitListExprClass:
1977 case Expr::ParenListExprClass:
1978 case Expr::CXXScalarValueInitExprClass:
John McCall09cc1412010-02-03 00:55:45 +00001979 llvm_unreachable("unexpected statement kind");
1980 break;
1981
John McCall0512e482010-07-14 04:20:34 +00001982 // FIXME: invent manglings for all these.
1983 case Expr::BlockExprClass:
1984 case Expr::CXXPseudoDestructorExprClass:
1985 case Expr::ChooseExprClass:
1986 case Expr::CompoundLiteralExprClass:
1987 case Expr::ExtVectorElementExprClass:
Peter Collingbournef111d932011-04-15 00:35:48 +00001988 case Expr::GenericSelectionExprClass:
John McCall0512e482010-07-14 04:20:34 +00001989 case Expr::ObjCEncodeExprClass:
John McCall0512e482010-07-14 04:20:34 +00001990 case Expr::ObjCIsaExprClass:
1991 case Expr::ObjCIvarRefExprClass:
1992 case Expr::ObjCMessageExprClass:
1993 case Expr::ObjCPropertyRefExprClass:
1994 case Expr::ObjCProtocolExprClass:
1995 case Expr::ObjCSelectorExprClass:
1996 case Expr::ObjCStringLiteralClass:
John McCall0512e482010-07-14 04:20:34 +00001997 case Expr::OffsetOfExprClass:
1998 case Expr::PredefinedExprClass:
1999 case Expr::ShuffleVectorExprClass:
2000 case Expr::StmtExprClass:
John McCall0512e482010-07-14 04:20:34 +00002001 case Expr::UnaryTypeTraitExprClass:
Francois Pichet6ad6f282010-12-07 00:08:36 +00002002 case Expr::BinaryTypeTraitExprClass:
John Wiegley21ff2e52011-04-28 00:16:57 +00002003 case Expr::ArrayTypeTraitExprClass:
John Wiegley55262202011-04-25 06:54:41 +00002004 case Expr::ExpressionTraitExprClass:
Francois Pichet9be88402010-09-08 23:47:05 +00002005 case Expr::VAArgExprClass:
Sebastian Redl2e156222010-09-10 20:55:43 +00002006 case Expr::CXXUuidofExprClass:
Peter Collingbournee08ce652011-02-09 21:07:24 +00002007 case Expr::CXXNoexceptExprClass:
2008 case Expr::CUDAKernelCallExprClass: {
John McCall6ae1f352010-04-09 22:26:14 +00002009 // As bad as this diagnostic is, it's better than crashing.
2010 Diagnostic &Diags = Context.getDiags();
2011 unsigned DiagID = Diags.getCustomDiagID(Diagnostic::Error,
2012 "cannot yet mangle expression type %0");
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +00002013 Diags.Report(E->getExprLoc(), DiagID)
John McCall739bf092010-04-10 09:39:25 +00002014 << E->getStmtClassName() << E->getSourceRange();
John McCall6ae1f352010-04-09 22:26:14 +00002015 break;
2016 }
2017
John McCall56ca35d2011-02-17 10:25:35 +00002018 // Even gcc-4.5 doesn't mangle this.
2019 case Expr::BinaryConditionalOperatorClass: {
2020 Diagnostic &Diags = Context.getDiags();
2021 unsigned DiagID =
2022 Diags.getCustomDiagID(Diagnostic::Error,
2023 "?: operator with omitted middle operand cannot be mangled");
2024 Diags.Report(E->getExprLoc(), DiagID)
2025 << E->getStmtClassName() << E->getSourceRange();
2026 break;
2027 }
2028
2029 // These are used for internal purposes and cannot be meaningfully mangled.
John McCall7cd7d1a2010-11-15 23:31:06 +00002030 case Expr::OpaqueValueExprClass:
2031 llvm_unreachable("cannot mangle opaque value; mangling wrong thing?");
2032
John McCall0512e482010-07-14 04:20:34 +00002033 case Expr::CXXDefaultArgExprClass:
John McCall5e1e89b2010-08-18 19:18:59 +00002034 mangleExpression(cast<CXXDefaultArgExpr>(E)->getExpr(), Arity);
John McCall0512e482010-07-14 04:20:34 +00002035 break;
2036
2037 case Expr::CXXMemberCallExprClass: // fallthrough
John McCall1dd73832010-02-04 01:42:13 +00002038 case Expr::CallExprClass: {
2039 const CallExpr *CE = cast<CallExpr>(E);
John McCall5a7e6f72011-04-28 02:52:03 +00002040
2041 // <expression> ::= cp <simple-id> <expression>* E
2042 // We use this mangling only when the call would use ADL except
2043 // for being parenthesized. Per discussion with David
2044 // Vandervoorde, 2011.04.25.
2045 if (isParenthesizedADLCallee(CE)) {
2046 Out << "cp";
2047 // The callee here is a parenthesized UnresolvedLookupExpr with
2048 // no qualifier and should always get mangled as a <simple-id>
2049 // anyway.
2050
2051 // <expression> ::= cl <expression>* E
2052 } else {
2053 Out << "cl";
2054 }
2055
John McCall5e1e89b2010-08-18 19:18:59 +00002056 mangleExpression(CE->getCallee(), CE->getNumArgs());
John McCall1dd73832010-02-04 01:42:13 +00002057 for (unsigned I = 0, N = CE->getNumArgs(); I != N; ++I)
2058 mangleExpression(CE->getArg(I));
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002059 Out << 'E';
John McCall09cc1412010-02-03 00:55:45 +00002060 break;
John McCall1dd73832010-02-04 01:42:13 +00002061 }
John McCall09cc1412010-02-03 00:55:45 +00002062
John McCall0512e482010-07-14 04:20:34 +00002063 case Expr::CXXNewExprClass: {
2064 // Proposal from David Vandervoorde, 2010.06.30
2065 const CXXNewExpr *New = cast<CXXNewExpr>(E);
2066 if (New->isGlobalNew()) Out << "gs";
2067 Out << (New->isArray() ? "na" : "nw");
2068 for (CXXNewExpr::const_arg_iterator I = New->placement_arg_begin(),
2069 E = New->placement_arg_end(); I != E; ++I)
2070 mangleExpression(*I);
2071 Out << '_';
2072 mangleType(New->getAllocatedType());
2073 if (New->hasInitializer()) {
2074 Out << "pi";
2075 for (CXXNewExpr::const_arg_iterator I = New->constructor_arg_begin(),
2076 E = New->constructor_arg_end(); I != E; ++I)
2077 mangleExpression(*I);
2078 }
2079 Out << 'E';
2080 break;
2081 }
2082
John McCall2f27bf82010-02-04 02:56:29 +00002083 case Expr::MemberExprClass: {
2084 const MemberExpr *ME = cast<MemberExpr>(E);
2085 mangleMemberExpr(ME->getBase(), ME->isArrow(),
John McCalla0ce15c2011-04-24 08:23:24 +00002086 ME->getQualifier(), 0, ME->getMemberDecl()->getDeclName(),
John McCall5e1e89b2010-08-18 19:18:59 +00002087 Arity);
John McCall2f27bf82010-02-04 02:56:29 +00002088 break;
2089 }
2090
2091 case Expr::UnresolvedMemberExprClass: {
2092 const UnresolvedMemberExpr *ME = cast<UnresolvedMemberExpr>(E);
2093 mangleMemberExpr(ME->getBase(), ME->isArrow(),
John McCalla0ce15c2011-04-24 08:23:24 +00002094 ME->getQualifier(), 0, ME->getMemberName(),
John McCall5e1e89b2010-08-18 19:18:59 +00002095 Arity);
John McCall6dbce192010-08-20 00:17:19 +00002096 if (ME->hasExplicitTemplateArgs())
2097 mangleTemplateArgs(ME->getExplicitTemplateArgs());
John McCall2f27bf82010-02-04 02:56:29 +00002098 break;
2099 }
2100
2101 case Expr::CXXDependentScopeMemberExprClass: {
2102 const CXXDependentScopeMemberExpr *ME
2103 = cast<CXXDependentScopeMemberExpr>(E);
2104 mangleMemberExpr(ME->getBase(), ME->isArrow(),
John McCalla0ce15c2011-04-24 08:23:24 +00002105 ME->getQualifier(), ME->getFirstQualifierFoundInScope(),
2106 ME->getMember(), Arity);
John McCall6dbce192010-08-20 00:17:19 +00002107 if (ME->hasExplicitTemplateArgs())
2108 mangleTemplateArgs(ME->getExplicitTemplateArgs());
John McCall2f27bf82010-02-04 02:56:29 +00002109 break;
2110 }
2111
John McCall1dd73832010-02-04 01:42:13 +00002112 case Expr::UnresolvedLookupExprClass: {
2113 const UnresolvedLookupExpr *ULE = cast<UnresolvedLookupExpr>(E);
John McCalla0ce15c2011-04-24 08:23:24 +00002114 mangleUnresolvedName(ULE->getQualifier(), 0, ULE->getName(), Arity);
John McCall6dbce192010-08-20 00:17:19 +00002115 if (ULE->hasExplicitTemplateArgs())
2116 mangleTemplateArgs(ULE->getExplicitTemplateArgs());
John McCall1dd73832010-02-04 01:42:13 +00002117 break;
2118 }
2119
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002120 case Expr::CXXUnresolvedConstructExprClass: {
John McCall1dd73832010-02-04 01:42:13 +00002121 const CXXUnresolvedConstructExpr *CE = cast<CXXUnresolvedConstructExpr>(E);
2122 unsigned N = CE->arg_size();
2123
2124 Out << "cv";
2125 mangleType(CE->getType());
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002126 if (N != 1) Out << '_';
John McCall1dd73832010-02-04 01:42:13 +00002127 for (unsigned I = 0; I != N; ++I) mangleExpression(CE->getArg(I));
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002128 if (N != 1) Out << 'E';
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002129 break;
John McCall1dd73832010-02-04 01:42:13 +00002130 }
John McCall09cc1412010-02-03 00:55:45 +00002131
John McCall1dd73832010-02-04 01:42:13 +00002132 case Expr::CXXTemporaryObjectExprClass:
2133 case Expr::CXXConstructExprClass: {
2134 const CXXConstructExpr *CE = cast<CXXConstructExpr>(E);
2135 unsigned N = CE->getNumArgs();
2136
2137 Out << "cv";
2138 mangleType(CE->getType());
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002139 if (N != 1) Out << '_';
John McCall1dd73832010-02-04 01:42:13 +00002140 for (unsigned I = 0; I != N; ++I) mangleExpression(CE->getArg(I));
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002141 if (N != 1) Out << 'E';
John McCall09cc1412010-02-03 00:55:45 +00002142 break;
John McCall1dd73832010-02-04 01:42:13 +00002143 }
2144
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +00002145 case Expr::UnaryExprOrTypeTraitExprClass: {
2146 const UnaryExprOrTypeTraitExpr *SAE = cast<UnaryExprOrTypeTraitExpr>(E);
2147 switch(SAE->getKind()) {
2148 case UETT_SizeOf:
2149 Out << 's';
2150 break;
2151 case UETT_AlignOf:
2152 Out << 'a';
2153 break;
2154 case UETT_VecStep:
2155 Diagnostic &Diags = Context.getDiags();
2156 unsigned DiagID = Diags.getCustomDiagID(Diagnostic::Error,
2157 "cannot yet mangle vec_step expression");
2158 Diags.Report(DiagID);
2159 return;
2160 }
John McCall1dd73832010-02-04 01:42:13 +00002161 if (SAE->isArgumentType()) {
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002162 Out << 't';
John McCall1dd73832010-02-04 01:42:13 +00002163 mangleType(SAE->getArgumentType());
2164 } else {
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002165 Out << 'z';
John McCall1dd73832010-02-04 01:42:13 +00002166 mangleExpression(SAE->getArgumentExpr());
2167 }
2168 break;
2169 }
Anders Carlssona7694082009-11-06 02:50:19 +00002170
John McCall0512e482010-07-14 04:20:34 +00002171 case Expr::CXXThrowExprClass: {
2172 const CXXThrowExpr *TE = cast<CXXThrowExpr>(E);
2173
2174 // Proposal from David Vandervoorde, 2010.06.30
2175 if (TE->getSubExpr()) {
2176 Out << "tw";
2177 mangleExpression(TE->getSubExpr());
2178 } else {
2179 Out << "tr";
2180 }
2181 break;
2182 }
2183
2184 case Expr::CXXTypeidExprClass: {
2185 const CXXTypeidExpr *TIE = cast<CXXTypeidExpr>(E);
2186
2187 // Proposal from David Vandervoorde, 2010.06.30
2188 if (TIE->isTypeOperand()) {
2189 Out << "ti";
2190 mangleType(TIE->getTypeOperand());
2191 } else {
2192 Out << "te";
2193 mangleExpression(TIE->getExprOperand());
2194 }
2195 break;
2196 }
2197
2198 case Expr::CXXDeleteExprClass: {
2199 const CXXDeleteExpr *DE = cast<CXXDeleteExpr>(E);
2200
2201 // Proposal from David Vandervoorde, 2010.06.30
2202 if (DE->isGlobalDelete()) Out << "gs";
2203 Out << (DE->isArrayForm() ? "da" : "dl");
2204 mangleExpression(DE->getArgument());
2205 break;
2206 }
2207
Anders Carlssone170ba72009-12-14 01:45:37 +00002208 case Expr::UnaryOperatorClass: {
2209 const UnaryOperator *UO = cast<UnaryOperator>(E);
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002210 mangleOperatorName(UnaryOperator::getOverloadedOperator(UO->getOpcode()),
Anders Carlssone170ba72009-12-14 01:45:37 +00002211 /*Arity=*/1);
2212 mangleExpression(UO->getSubExpr());
2213 break;
2214 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002215
John McCall0512e482010-07-14 04:20:34 +00002216 case Expr::ArraySubscriptExprClass: {
2217 const ArraySubscriptExpr *AE = cast<ArraySubscriptExpr>(E);
2218
Chris Lattnerfc8f0e12011-04-15 05:22:18 +00002219 // Array subscript is treated as a syntactically weird form of
John McCall0512e482010-07-14 04:20:34 +00002220 // binary operator.
2221 Out << "ix";
2222 mangleExpression(AE->getLHS());
2223 mangleExpression(AE->getRHS());
2224 break;
2225 }
2226
2227 case Expr::CompoundAssignOperatorClass: // fallthrough
Anders Carlssone170ba72009-12-14 01:45:37 +00002228 case Expr::BinaryOperatorClass: {
2229 const BinaryOperator *BO = cast<BinaryOperator>(E);
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002230 mangleOperatorName(BinaryOperator::getOverloadedOperator(BO->getOpcode()),
Anders Carlssone170ba72009-12-14 01:45:37 +00002231 /*Arity=*/2);
2232 mangleExpression(BO->getLHS());
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002233 mangleExpression(BO->getRHS());
Anders Carlssone170ba72009-12-14 01:45:37 +00002234 break;
John McCall2f27bf82010-02-04 02:56:29 +00002235 }
Anders Carlssone170ba72009-12-14 01:45:37 +00002236
2237 case Expr::ConditionalOperatorClass: {
2238 const ConditionalOperator *CO = cast<ConditionalOperator>(E);
2239 mangleOperatorName(OO_Conditional, /*Arity=*/3);
2240 mangleExpression(CO->getCond());
John McCall5e1e89b2010-08-18 19:18:59 +00002241 mangleExpression(CO->getLHS(), Arity);
2242 mangleExpression(CO->getRHS(), Arity);
Anders Carlssone170ba72009-12-14 01:45:37 +00002243 break;
2244 }
2245
Douglas Gregor46287c72010-01-29 16:37:09 +00002246 case Expr::ImplicitCastExprClass: {
John McCall5e1e89b2010-08-18 19:18:59 +00002247 mangleExpression(cast<ImplicitCastExpr>(E)->getSubExpr(), Arity);
Douglas Gregor46287c72010-01-29 16:37:09 +00002248 break;
2249 }
2250
2251 case Expr::CStyleCastExprClass:
2252 case Expr::CXXStaticCastExprClass:
2253 case Expr::CXXDynamicCastExprClass:
2254 case Expr::CXXReinterpretCastExprClass:
2255 case Expr::CXXConstCastExprClass:
2256 case Expr::CXXFunctionalCastExprClass: {
2257 const ExplicitCastExpr *ECE = cast<ExplicitCastExpr>(E);
2258 Out << "cv";
2259 mangleType(ECE->getType());
2260 mangleExpression(ECE->getSubExpr());
2261 break;
2262 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002263
Anders Carlsson58040a52009-12-16 05:48:46 +00002264 case Expr::CXXOperatorCallExprClass: {
2265 const CXXOperatorCallExpr *CE = cast<CXXOperatorCallExpr>(E);
2266 unsigned NumArgs = CE->getNumArgs();
2267 mangleOperatorName(CE->getOperator(), /*Arity=*/NumArgs);
2268 // Mangle the arguments.
2269 for (unsigned i = 0; i != NumArgs; ++i)
2270 mangleExpression(CE->getArg(i));
2271 break;
2272 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002273
Anders Carlssona7694082009-11-06 02:50:19 +00002274 case Expr::ParenExprClass:
John McCall5e1e89b2010-08-18 19:18:59 +00002275 mangleExpression(cast<ParenExpr>(E)->getSubExpr(), Arity);
Anders Carlssona7694082009-11-06 02:50:19 +00002276 break;
2277
Anders Carlssond553f8c2009-09-21 01:21:10 +00002278 case Expr::DeclRefExprClass: {
Douglas Gregor5ed1bc32010-02-28 21:40:32 +00002279 const NamedDecl *D = cast<DeclRefExpr>(E)->getDecl();
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002280
Anders Carlssond553f8c2009-09-21 01:21:10 +00002281 switch (D->getKind()) {
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002282 default:
Douglas Gregor5ed1bc32010-02-28 21:40:32 +00002283 // <expr-primary> ::= L <mangled-name> E # external name
2284 Out << 'L';
2285 mangle(D, "_Z");
2286 Out << 'E';
2287 break;
2288
John McCallfb44de92011-05-01 22:35:37 +00002289 case Decl::ParmVar:
2290 mangleFunctionParam(cast<ParmVarDecl>(D));
2291 break;
2292
John McCall3dc7e7b2010-07-24 01:17:35 +00002293 case Decl::EnumConstant: {
2294 const EnumConstantDecl *ED = cast<EnumConstantDecl>(D);
2295 mangleIntegerLiteral(ED->getType(), ED->getInitVal());
2296 break;
2297 }
2298
Anders Carlssond553f8c2009-09-21 01:21:10 +00002299 case Decl::NonTypeTemplateParm: {
2300 const NonTypeTemplateParmDecl *PD = cast<NonTypeTemplateParmDecl>(D);
Anders Carlsson0ccdf8d2009-09-27 00:38:53 +00002301 mangleTemplateParameter(PD->getIndex());
Anders Carlssond553f8c2009-09-21 01:21:10 +00002302 break;
2303 }
2304
2305 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002306
Anders Carlsson50755b02009-09-27 20:11:34 +00002307 break;
Anders Carlssond553f8c2009-09-21 01:21:10 +00002308 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002309
Douglas Gregorc7793c72011-01-15 01:15:58 +00002310 case Expr::SubstNonTypeTemplateParmPackExprClass:
2311 mangleTemplateParameter(
2312 cast<SubstNonTypeTemplateParmPackExpr>(E)->getParameterPack()->getIndex());
2313 break;
2314
John McCall865d4472009-11-19 22:55:06 +00002315 case Expr::DependentScopeDeclRefExprClass: {
2316 const DependentScopeDeclRefExpr *DRE = cast<DependentScopeDeclRefExpr>(E);
Douglas Gregor4b2ccfc2010-02-28 22:05:49 +00002317 NestedNameSpecifier *NNS = DRE->getQualifier();
2318 const Type *QTy = NNS->getAsType();
2319
2320 // When we're dealing with a nested-name-specifier that has just a
2321 // dependent identifier in it, mangle that as a typename. FIXME:
2322 // It isn't clear that we ever actually want to have such a
2323 // nested-name-specifier; why not just represent it as a typename type?
2324 if (!QTy && NNS->getAsIdentifier() && NNS->getPrefix()) {
Douglas Gregor4a2023f2010-03-31 20:19:30 +00002325 QTy = getASTContext().getDependentNameType(ETK_Typename,
2326 NNS->getPrefix(),
2327 NNS->getAsIdentifier())
Douglas Gregor4b2ccfc2010-02-28 22:05:49 +00002328 .getTypePtr();
2329 }
Anders Carlsson50755b02009-09-27 20:11:34 +00002330 assert(QTy && "Qualifier was not type!");
2331
John McCall6dbce192010-08-20 00:17:19 +00002332 // ::= sr <type> <unqualified-name> # dependent name
2333 // ::= sr <type> <unqualified-name> <template-args> # dependent template-id
Anders Carlsson50755b02009-09-27 20:11:34 +00002334 Out << "sr";
2335 mangleType(QualType(QTy, 0));
John McCall5e1e89b2010-08-18 19:18:59 +00002336 mangleUnqualifiedName(0, DRE->getDeclName(), Arity);
John McCall6dbce192010-08-20 00:17:19 +00002337 if (DRE->hasExplicitTemplateArgs())
2338 mangleTemplateArgs(DRE->getExplicitTemplateArgs());
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002339
Anders Carlsson50755b02009-09-27 20:11:34 +00002340 break;
2341 }
2342
John McCalld9307602010-04-09 22:54:09 +00002343 case Expr::CXXBindTemporaryExprClass:
2344 mangleExpression(cast<CXXBindTemporaryExpr>(E)->getSubExpr());
2345 break;
2346
John McCall4765fa02010-12-06 08:20:24 +00002347 case Expr::ExprWithCleanupsClass:
2348 mangleExpression(cast<ExprWithCleanups>(E)->getSubExpr(), Arity);
John McCalld9307602010-04-09 22:54:09 +00002349 break;
2350
John McCall1dd73832010-02-04 01:42:13 +00002351 case Expr::FloatingLiteralClass: {
2352 const FloatingLiteral *FL = cast<FloatingLiteral>(E);
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002353 Out << 'L';
John McCall1dd73832010-02-04 01:42:13 +00002354 mangleType(FL->getType());
John McCall0512e482010-07-14 04:20:34 +00002355 mangleFloat(FL->getValue());
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002356 Out << 'E';
John McCall1dd73832010-02-04 01:42:13 +00002357 break;
2358 }
2359
John McCallde810632010-04-09 21:48:08 +00002360 case Expr::CharacterLiteralClass:
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002361 Out << 'L';
John McCallde810632010-04-09 21:48:08 +00002362 mangleType(E->getType());
2363 Out << cast<CharacterLiteral>(E)->getValue();
2364 Out << 'E';
2365 break;
2366
2367 case Expr::CXXBoolLiteralExprClass:
2368 Out << "Lb";
2369 Out << (cast<CXXBoolLiteralExpr>(E)->getValue() ? '1' : '0');
2370 Out << 'E';
2371 break;
2372
John McCall0512e482010-07-14 04:20:34 +00002373 case Expr::IntegerLiteralClass: {
2374 llvm::APSInt Value(cast<IntegerLiteral>(E)->getValue());
2375 if (E->getType()->isSignedIntegerType())
2376 Value.setIsSigned(true);
2377 mangleIntegerLiteral(E->getType(), Value);
Anders Carlssone170ba72009-12-14 01:45:37 +00002378 break;
John McCall0512e482010-07-14 04:20:34 +00002379 }
2380
2381 case Expr::ImaginaryLiteralClass: {
2382 const ImaginaryLiteral *IE = cast<ImaginaryLiteral>(E);
2383 // Mangle as if a complex literal.
Nick Lewycky271b6652010-09-05 03:40:33 +00002384 // Proposal from David Vandevoorde, 2010.06.30.
John McCall0512e482010-07-14 04:20:34 +00002385 Out << 'L';
2386 mangleType(E->getType());
2387 if (const FloatingLiteral *Imag =
2388 dyn_cast<FloatingLiteral>(IE->getSubExpr())) {
2389 // Mangle a floating-point zero of the appropriate type.
2390 mangleFloat(llvm::APFloat(Imag->getValue().getSemantics()));
2391 Out << '_';
2392 mangleFloat(Imag->getValue());
2393 } else {
Nick Lewycky271b6652010-09-05 03:40:33 +00002394 Out << "0_";
John McCall0512e482010-07-14 04:20:34 +00002395 llvm::APSInt Value(cast<IntegerLiteral>(IE->getSubExpr())->getValue());
2396 if (IE->getSubExpr()->getType()->isSignedIntegerType())
2397 Value.setIsSigned(true);
2398 mangleNumber(Value);
2399 }
2400 Out << 'E';
2401 break;
2402 }
2403
2404 case Expr::StringLiteralClass: {
John McCall1658c392010-07-15 21:53:03 +00002405 // Revised proposal from David Vandervoorde, 2010.07.15.
John McCall0512e482010-07-14 04:20:34 +00002406 Out << 'L';
John McCall1658c392010-07-15 21:53:03 +00002407 assert(isa<ConstantArrayType>(E->getType()));
2408 mangleType(E->getType());
John McCall0512e482010-07-14 04:20:34 +00002409 Out << 'E';
2410 break;
2411 }
2412
2413 case Expr::GNUNullExprClass:
2414 // FIXME: should this really be mangled the same as nullptr?
2415 // fallthrough
2416
2417 case Expr::CXXNullPtrLiteralExprClass: {
2418 // Proposal from David Vandervoorde, 2010.06.30, as
2419 // modified by ABI list discussion.
2420 Out << "LDnE";
2421 break;
2422 }
Douglas Gregorbe230c32011-01-03 17:17:50 +00002423
2424 case Expr::PackExpansionExprClass:
2425 Out << "sp";
2426 mangleExpression(cast<PackExpansionExpr>(E)->getPattern());
2427 break;
Douglas Gregor2e774c42011-01-04 18:56:13 +00002428
2429 case Expr::SizeOfPackExprClass: {
Douglas Gregor2e774c42011-01-04 18:56:13 +00002430 Out << "sZ";
2431 const NamedDecl *Pack = cast<SizeOfPackExpr>(E)->getPack();
2432 if (const TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(Pack))
2433 mangleTemplateParameter(TTP->getIndex());
2434 else if (const NonTypeTemplateParmDecl *NTTP
2435 = dyn_cast<NonTypeTemplateParmDecl>(Pack))
2436 mangleTemplateParameter(NTTP->getIndex());
2437 else if (const TemplateTemplateParmDecl *TempTP
2438 = dyn_cast<TemplateTemplateParmDecl>(Pack))
2439 mangleTemplateParameter(TempTP->getIndex());
2440 else {
Douglas Gregor4fc48662011-01-13 16:39:34 +00002441 // Note: proposed by Mike Herrick on 11/30/10
2442 // <expression> ::= sZ <function-param> # size of function parameter pack
Douglas Gregor2e774c42011-01-04 18:56:13 +00002443 Diagnostic &Diags = Context.getDiags();
2444 unsigned DiagID = Diags.getCustomDiagID(Diagnostic::Error,
2445 "cannot mangle sizeof...(function parameter pack)");
2446 Diags.Report(DiagID);
2447 return;
2448 }
Douglas Gregordfbbcf92011-03-03 02:20:19 +00002449 break;
Douglas Gregor2e774c42011-01-04 18:56:13 +00002450 }
Anders Carlssond553f8c2009-09-21 01:21:10 +00002451 }
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00002452}
2453
John McCallfb44de92011-05-01 22:35:37 +00002454/// Mangle an expression which refers to a parameter variable.
2455///
2456/// <expression> ::= <function-param>
2457/// <function-param> ::= fp <top-level CV-qualifiers> _ # L == 0, I == 0
2458/// <function-param> ::= fp <top-level CV-qualifiers>
2459/// <parameter-2 non-negative number> _ # L == 0, I > 0
2460/// <function-param> ::= fL <L-1 non-negative number>
2461/// p <top-level CV-qualifiers> _ # L > 0, I == 0
2462/// <function-param> ::= fL <L-1 non-negative number>
2463/// p <top-level CV-qualifiers>
2464/// <I-1 non-negative number> _ # L > 0, I > 0
2465///
2466/// L is the nesting depth of the parameter, defined as 1 if the
2467/// parameter comes from the innermost function prototype scope
2468/// enclosing the current context, 2 if from the next enclosing
2469/// function prototype scope, and so on, with one special case: if
2470/// we've processed the full parameter clause for the innermost
2471/// function type, then L is one less. This definition conveniently
2472/// makes it irrelevant whether a function's result type was written
2473/// trailing or leading, but is otherwise overly complicated; the
2474/// numbering was first designed without considering references to
2475/// parameter in locations other than return types, and then the
2476/// mangling had to be generalized without changing the existing
2477/// manglings.
2478///
2479/// I is the zero-based index of the parameter within its parameter
2480/// declaration clause. Note that the original ABI document describes
2481/// this using 1-based ordinals.
2482void CXXNameMangler::mangleFunctionParam(const ParmVarDecl *parm) {
2483 unsigned parmDepth = parm->getFunctionScopeDepth();
2484 unsigned parmIndex = parm->getFunctionScopeIndex();
2485
2486 // Compute 'L'.
2487 // parmDepth does not include the declaring function prototype.
2488 // FunctionTypeDepth does account for that.
2489 assert(parmDepth < FunctionTypeDepth.getDepth());
2490 unsigned nestingDepth = FunctionTypeDepth.getDepth() - parmDepth;
2491 if (FunctionTypeDepth.isInResultType())
2492 nestingDepth--;
2493
2494 if (nestingDepth == 0) {
2495 Out << "fp";
2496 } else {
2497 Out << "fL" << (nestingDepth - 1) << 'p';
2498 }
2499
2500 // Top-level qualifiers. We don't have to worry about arrays here,
2501 // because parameters declared as arrays should already have been
2502 // tranformed to have pointer type. FIXME: apparently these don't
2503 // get mangled if used as an rvalue of a known non-class type?
2504 assert(!parm->getType()->isArrayType()
2505 && "parameter's type is still an array type?");
2506 mangleQualifiers(parm->getType().getQualifiers());
2507
2508 // Parameter index.
2509 if (parmIndex != 0) {
2510 Out << (parmIndex - 1);
2511 }
2512 Out << '_';
2513}
2514
Anders Carlsson3ac86b52009-04-15 05:36:58 +00002515void CXXNameMangler::mangleCXXCtorType(CXXCtorType T) {
2516 // <ctor-dtor-name> ::= C1 # complete object constructor
2517 // ::= C2 # base object constructor
2518 // ::= C3 # complete object allocating constructor
2519 //
2520 switch (T) {
2521 case Ctor_Complete:
2522 Out << "C1";
2523 break;
2524 case Ctor_Base:
2525 Out << "C2";
2526 break;
2527 case Ctor_CompleteAllocating:
2528 Out << "C3";
2529 break;
2530 }
2531}
2532
Anders Carlsson27ae5362009-04-17 01:58:57 +00002533void CXXNameMangler::mangleCXXDtorType(CXXDtorType T) {
2534 // <ctor-dtor-name> ::= D0 # deleting destructor
2535 // ::= D1 # complete object destructor
2536 // ::= D2 # base object destructor
2537 //
2538 switch (T) {
2539 case Dtor_Deleting:
2540 Out << "D0";
2541 break;
2542 case Dtor_Complete:
2543 Out << "D1";
2544 break;
2545 case Dtor_Base:
2546 Out << "D2";
2547 break;
2548 }
2549}
2550
John McCall6dbce192010-08-20 00:17:19 +00002551void CXXNameMangler::mangleTemplateArgs(
2552 const ExplicitTemplateArgumentList &TemplateArgs) {
2553 // <template-args> ::= I <template-arg>+ E
2554 Out << 'I';
2555 for (unsigned I = 0, E = TemplateArgs.NumTemplateArgs; I != E; ++I)
2556 mangleTemplateArg(0, TemplateArgs.getTemplateArgs()[I].getArgument());
2557 Out << 'E';
2558}
2559
Douglas Gregor20f0cc72010-04-23 03:10:43 +00002560void CXXNameMangler::mangleTemplateArgs(TemplateName Template,
2561 const TemplateArgument *TemplateArgs,
2562 unsigned NumTemplateArgs) {
2563 if (TemplateDecl *TD = Template.getAsTemplateDecl())
2564 return mangleTemplateArgs(*TD->getTemplateParameters(), TemplateArgs,
2565 NumTemplateArgs);
Sean Huntc3021132010-05-05 15:23:54 +00002566
Douglas Gregor20f0cc72010-04-23 03:10:43 +00002567 // <template-args> ::= I <template-arg>+ E
2568 Out << 'I';
2569 for (unsigned i = 0; i != NumTemplateArgs; ++i)
2570 mangleTemplateArg(0, TemplateArgs[i]);
2571 Out << 'E';
2572}
2573
Rafael Espindolad9800722010-03-11 14:07:00 +00002574void CXXNameMangler::mangleTemplateArgs(const TemplateParameterList &PL,
2575 const TemplateArgumentList &AL) {
Anders Carlsson7a0ba872009-05-15 16:09:15 +00002576 // <template-args> ::= I <template-arg>+ E
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002577 Out << 'I';
Rafael Espindolad9800722010-03-11 14:07:00 +00002578 for (unsigned i = 0, e = AL.size(); i != e; ++i)
2579 mangleTemplateArg(PL.getParam(i), AL[i]);
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002580 Out << 'E';
Anders Carlsson7a0ba872009-05-15 16:09:15 +00002581}
2582
Rafael Espindolad9800722010-03-11 14:07:00 +00002583void CXXNameMangler::mangleTemplateArgs(const TemplateParameterList &PL,
2584 const TemplateArgument *TemplateArgs,
Anders Carlsson7624f212009-09-18 02:42:01 +00002585 unsigned NumTemplateArgs) {
2586 // <template-args> ::= I <template-arg>+ E
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002587 Out << 'I';
Daniel Dunbar7e0c1952009-11-21 09:17:15 +00002588 for (unsigned i = 0; i != NumTemplateArgs; ++i)
Rafael Espindolad9800722010-03-11 14:07:00 +00002589 mangleTemplateArg(PL.getParam(i), TemplateArgs[i]);
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002590 Out << 'E';
Anders Carlsson7624f212009-09-18 02:42:01 +00002591}
2592
Rafael Espindolad9800722010-03-11 14:07:00 +00002593void CXXNameMangler::mangleTemplateArg(const NamedDecl *P,
2594 const TemplateArgument &A) {
Mike Stump1eb44332009-09-09 15:08:12 +00002595 // <template-arg> ::= <type> # type or template
Anders Carlsson7a0ba872009-05-15 16:09:15 +00002596 // ::= X <expression> E # expression
2597 // ::= <expr-primary> # simple expressions
Douglas Gregor4fc48662011-01-13 16:39:34 +00002598 // ::= J <template-arg>* E # argument pack
Anders Carlsson7a0ba872009-05-15 16:09:15 +00002599 // ::= sp <expression> # pack expansion of (C++0x)
2600 switch (A.getKind()) {
Douglas Gregorf90b27a2011-01-03 22:36:02 +00002601 case TemplateArgument::Null:
2602 llvm_unreachable("Cannot mangle NULL template argument");
2603
Anders Carlsson7a0ba872009-05-15 16:09:15 +00002604 case TemplateArgument::Type:
2605 mangleType(A.getAsType());
2606 break;
Anders Carlsson9e85c742009-12-23 19:30:55 +00002607 case TemplateArgument::Template:
John McCallb6f532e2010-07-14 06:43:17 +00002608 // This is mangled as <type>.
2609 mangleType(A.getAsTemplate());
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002610 break;
Douglas Gregora7fc9012011-01-05 18:58:31 +00002611 case TemplateArgument::TemplateExpansion:
Douglas Gregor4fc48662011-01-13 16:39:34 +00002612 // <type> ::= Dp <type> # pack expansion (C++0x)
Douglas Gregora7fc9012011-01-05 18:58:31 +00002613 Out << "Dp";
2614 mangleType(A.getAsTemplateOrTemplatePattern());
2615 break;
Anders Carlssond553f8c2009-09-21 01:21:10 +00002616 case TemplateArgument::Expression:
2617 Out << 'X';
2618 mangleExpression(A.getAsExpr());
2619 Out << 'E';
2620 break;
Anders Carlssone170ba72009-12-14 01:45:37 +00002621 case TemplateArgument::Integral:
2622 mangleIntegerLiteral(A.getIntegralType(), *A.getAsIntegral());
Anders Carlsson7a0ba872009-05-15 16:09:15 +00002623 break;
Daniel Dunbar7e0c1952009-11-21 09:17:15 +00002624 case TemplateArgument::Declaration: {
Douglas Gregor20f0cc72010-04-23 03:10:43 +00002625 assert(P && "Missing template parameter for declaration argument");
Daniel Dunbar7e0c1952009-11-21 09:17:15 +00002626 // <expr-primary> ::= L <mangled-name> E # external name
2627
Rafael Espindolad9800722010-03-11 14:07:00 +00002628 // Clang produces AST's where pointer-to-member-function expressions
Daniel Dunbar7e0c1952009-11-21 09:17:15 +00002629 // and pointer-to-function expressions are represented as a declaration not
Rafael Espindolad9800722010-03-11 14:07:00 +00002630 // an expression. We compensate for it here to produce the correct mangling.
2631 NamedDecl *D = cast<NamedDecl>(A.getAsDecl());
2632 const NonTypeTemplateParmDecl *Parameter = cast<NonTypeTemplateParmDecl>(P);
John McCallc0a45592011-04-24 08:43:07 +00002633 bool compensateMangling = !Parameter->getType()->isReferenceType();
Rafael Espindolad9800722010-03-11 14:07:00 +00002634 if (compensateMangling) {
2635 Out << 'X';
2636 mangleOperatorName(OO_Amp, 1);
2637 }
2638
Daniel Dunbar7e0c1952009-11-21 09:17:15 +00002639 Out << 'L';
2640 // References to external entities use the mangled name; if the name would
2641 // not normally be manged then mangle it as unqualified.
2642 //
2643 // FIXME: The ABI specifies that external names here should have _Z, but
2644 // gcc leaves this off.
Rafael Espindolad9800722010-03-11 14:07:00 +00002645 if (compensateMangling)
2646 mangle(D, "_Z");
2647 else
2648 mangle(D, "Z");
Daniel Dunbar7e0c1952009-11-21 09:17:15 +00002649 Out << 'E';
Rafael Espindolad9800722010-03-11 14:07:00 +00002650
2651 if (compensateMangling)
2652 Out << 'E';
2653
Daniel Dunbar7e0c1952009-11-21 09:17:15 +00002654 break;
2655 }
Douglas Gregorf90b27a2011-01-03 22:36:02 +00002656
2657 case TemplateArgument::Pack: {
2658 // Note: proposal by Mike Herrick on 12/20/10
2659 Out << 'J';
2660 for (TemplateArgument::pack_iterator PA = A.pack_begin(),
2661 PAEnd = A.pack_end();
2662 PA != PAEnd; ++PA)
2663 mangleTemplateArg(P, *PA);
2664 Out << 'E';
2665 }
Daniel Dunbar7e0c1952009-11-21 09:17:15 +00002666 }
Anders Carlsson7a0ba872009-05-15 16:09:15 +00002667}
2668
Anders Carlsson0ccdf8d2009-09-27 00:38:53 +00002669void CXXNameMangler::mangleTemplateParameter(unsigned Index) {
2670 // <template-param> ::= T_ # first template parameter
2671 // ::= T <parameter-2 non-negative number> _
2672 if (Index == 0)
2673 Out << "T_";
2674 else
2675 Out << 'T' << (Index - 1) << '_';
2676}
2677
Anders Carlsson76967372009-09-17 00:43:46 +00002678// <substitution> ::= S <seq-id> _
2679// ::= S_
Anders Carlsson6862fc72009-09-17 04:16:28 +00002680bool CXXNameMangler::mangleSubstitution(const NamedDecl *ND) {
Anders Carlssone7c8cb62009-09-26 20:53:44 +00002681 // Try one of the standard substitutions first.
2682 if (mangleStandardSubstitution(ND))
2683 return true;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002684
Anders Carlsson433d1372009-11-07 04:26:04 +00002685 ND = cast<NamedDecl>(ND->getCanonicalDecl());
Anders Carlsson6862fc72009-09-17 04:16:28 +00002686 return mangleSubstitution(reinterpret_cast<uintptr_t>(ND));
2687}
2688
Anders Carlsson76967372009-09-17 00:43:46 +00002689bool CXXNameMangler::mangleSubstitution(QualType T) {
Anders Carlssond99edc42009-09-26 03:55:37 +00002690 if (!T.getCVRQualifiers()) {
2691 if (const RecordType *RT = T->getAs<RecordType>())
2692 return mangleSubstitution(RT->getDecl());
2693 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002694
Anders Carlsson76967372009-09-17 00:43:46 +00002695 uintptr_t TypePtr = reinterpret_cast<uintptr_t>(T.getAsOpaquePtr());
2696
Anders Carlssond3a932a2009-09-17 03:53:28 +00002697 return mangleSubstitution(TypePtr);
2698}
2699
Douglas Gregor1e9268e2010-04-28 05:58:56 +00002700bool CXXNameMangler::mangleSubstitution(TemplateName Template) {
2701 if (TemplateDecl *TD = Template.getAsTemplateDecl())
2702 return mangleSubstitution(TD);
Sean Huntc3021132010-05-05 15:23:54 +00002703
Douglas Gregor1e9268e2010-04-28 05:58:56 +00002704 Template = Context.getASTContext().getCanonicalTemplateName(Template);
2705 return mangleSubstitution(
2706 reinterpret_cast<uintptr_t>(Template.getAsVoidPointer()));
2707}
2708
Anders Carlssond3a932a2009-09-17 03:53:28 +00002709bool CXXNameMangler::mangleSubstitution(uintptr_t Ptr) {
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002710 llvm::DenseMap<uintptr_t, unsigned>::iterator I = Substitutions.find(Ptr);
Anders Carlsson76967372009-09-17 00:43:46 +00002711 if (I == Substitutions.end())
2712 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002713
Anders Carlsson76967372009-09-17 00:43:46 +00002714 unsigned SeqID = I->second;
2715 if (SeqID == 0)
2716 Out << "S_";
2717 else {
2718 SeqID--;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002719
Anders Carlsson76967372009-09-17 00:43:46 +00002720 // <seq-id> is encoded in base-36, using digits and upper case letters.
2721 char Buffer[10];
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002722 char *BufferPtr = llvm::array_endof(Buffer);
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002723
Anders Carlsson76967372009-09-17 00:43:46 +00002724 if (SeqID == 0) *--BufferPtr = '0';
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002725
Anders Carlsson76967372009-09-17 00:43:46 +00002726 while (SeqID) {
2727 assert(BufferPtr > Buffer && "Buffer overflow!");
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002728
John McCall6ab30e02010-06-09 07:26:17 +00002729 char c = static_cast<char>(SeqID % 36);
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002730
Anders Carlsson76967372009-09-17 00:43:46 +00002731 *--BufferPtr = (c < 10 ? '0' + c : 'A' + c - 10);
2732 SeqID /= 36;
2733 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002734
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002735 Out << 'S'
2736 << llvm::StringRef(BufferPtr, llvm::array_endof(Buffer)-BufferPtr)
2737 << '_';
Anders Carlsson76967372009-09-17 00:43:46 +00002738 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002739
Anders Carlsson76967372009-09-17 00:43:46 +00002740 return true;
2741}
2742
Anders Carlssonf514b542009-09-27 00:12:57 +00002743static bool isCharType(QualType T) {
2744 if (T.isNull())
2745 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002746
Anders Carlssonf514b542009-09-27 00:12:57 +00002747 return T->isSpecificBuiltinType(BuiltinType::Char_S) ||
2748 T->isSpecificBuiltinType(BuiltinType::Char_U);
2749}
2750
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002751/// isCharSpecialization - Returns whether a given type is a template
Anders Carlssonf514b542009-09-27 00:12:57 +00002752/// specialization of a given name with a single argument of type char.
2753static bool isCharSpecialization(QualType T, const char *Name) {
2754 if (T.isNull())
2755 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002756
Anders Carlssonf514b542009-09-27 00:12:57 +00002757 const RecordType *RT = T->getAs<RecordType>();
2758 if (!RT)
2759 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002760
2761 const ClassTemplateSpecializationDecl *SD =
Anders Carlssonf514b542009-09-27 00:12:57 +00002762 dyn_cast<ClassTemplateSpecializationDecl>(RT->getDecl());
2763 if (!SD)
2764 return false;
2765
2766 if (!isStdNamespace(SD->getDeclContext()))
2767 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002768
Anders Carlssonf514b542009-09-27 00:12:57 +00002769 const TemplateArgumentList &TemplateArgs = SD->getTemplateArgs();
2770 if (TemplateArgs.size() != 1)
2771 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002772
Anders Carlssonf514b542009-09-27 00:12:57 +00002773 if (!isCharType(TemplateArgs[0].getAsType()))
2774 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002775
Daniel Dunbar01eb9b92009-10-18 21:17:35 +00002776 return SD->getIdentifier()->getName() == Name;
Anders Carlssonf514b542009-09-27 00:12:57 +00002777}
2778
Anders Carlsson91f88602009-12-07 19:56:42 +00002779template <std::size_t StrLen>
Benjamin Kramer54353f42010-11-25 18:29:30 +00002780static bool isStreamCharSpecialization(const ClassTemplateSpecializationDecl*SD,
2781 const char (&Str)[StrLen]) {
Anders Carlsson91f88602009-12-07 19:56:42 +00002782 if (!SD->getIdentifier()->isStr(Str))
2783 return false;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002784
Anders Carlsson91f88602009-12-07 19:56:42 +00002785 const TemplateArgumentList &TemplateArgs = SD->getTemplateArgs();
2786 if (TemplateArgs.size() != 2)
2787 return false;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002788
Anders Carlsson91f88602009-12-07 19:56:42 +00002789 if (!isCharType(TemplateArgs[0].getAsType()))
2790 return false;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002791
Anders Carlsson91f88602009-12-07 19:56:42 +00002792 if (!isCharSpecialization(TemplateArgs[1].getAsType(), "char_traits"))
2793 return false;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002794
Anders Carlsson91f88602009-12-07 19:56:42 +00002795 return true;
2796}
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002797
Anders Carlssone7c8cb62009-09-26 20:53:44 +00002798bool CXXNameMangler::mangleStandardSubstitution(const NamedDecl *ND) {
2799 // <substitution> ::= St # ::std::
Anders Carlsson8c031552009-09-26 23:10:05 +00002800 if (const NamespaceDecl *NS = dyn_cast<NamespaceDecl>(ND)) {
Anders Carlsson47846d22009-12-04 06:23:23 +00002801 if (isStd(NS)) {
Anders Carlsson8c031552009-09-26 23:10:05 +00002802 Out << "St";
2803 return true;
2804 }
2805 }
2806
2807 if (const ClassTemplateDecl *TD = dyn_cast<ClassTemplateDecl>(ND)) {
2808 if (!isStdNamespace(TD->getDeclContext()))
2809 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002810
Anders Carlsson8c031552009-09-26 23:10:05 +00002811 // <substitution> ::= Sa # ::std::allocator
2812 if (TD->getIdentifier()->isStr("allocator")) {
2813 Out << "Sa";
2814 return true;
2815 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002816
Anders Carlsson189d59c2009-09-26 23:14:39 +00002817 // <<substitution> ::= Sb # ::std::basic_string
2818 if (TD->getIdentifier()->isStr("basic_string")) {
2819 Out << "Sb";
2820 return true;
2821 }
Anders Carlsson8c031552009-09-26 23:10:05 +00002822 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002823
2824 if (const ClassTemplateSpecializationDecl *SD =
Anders Carlssonf514b542009-09-27 00:12:57 +00002825 dyn_cast<ClassTemplateSpecializationDecl>(ND)) {
Eli Friedman5370ee22010-02-23 18:25:09 +00002826 if (!isStdNamespace(SD->getDeclContext()))
2827 return false;
2828
Anders Carlssonf514b542009-09-27 00:12:57 +00002829 // <substitution> ::= Ss # ::std::basic_string<char,
2830 // ::std::char_traits<char>,
2831 // ::std::allocator<char> >
2832 if (SD->getIdentifier()->isStr("basic_string")) {
2833 const TemplateArgumentList &TemplateArgs = SD->getTemplateArgs();
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002834
Anders Carlssonf514b542009-09-27 00:12:57 +00002835 if (TemplateArgs.size() != 3)
2836 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002837
Anders Carlssonf514b542009-09-27 00:12:57 +00002838 if (!isCharType(TemplateArgs[0].getAsType()))
2839 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002840
Anders Carlssonf514b542009-09-27 00:12:57 +00002841 if (!isCharSpecialization(TemplateArgs[1].getAsType(), "char_traits"))
2842 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002843
Anders Carlssonf514b542009-09-27 00:12:57 +00002844 if (!isCharSpecialization(TemplateArgs[2].getAsType(), "allocator"))
2845 return false;
2846
2847 Out << "Ss";
2848 return true;
2849 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002850
Anders Carlsson91f88602009-12-07 19:56:42 +00002851 // <substitution> ::= Si # ::std::basic_istream<char,
2852 // ::std::char_traits<char> >
2853 if (isStreamCharSpecialization(SD, "basic_istream")) {
2854 Out << "Si";
2855 return true;
2856 }
2857
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002858 // <substitution> ::= So # ::std::basic_ostream<char,
Anders Carlsson8f8fd8e2009-10-08 17:20:26 +00002859 // ::std::char_traits<char> >
Anders Carlsson91f88602009-12-07 19:56:42 +00002860 if (isStreamCharSpecialization(SD, "basic_ostream")) {
Anders Carlsson8f8fd8e2009-10-08 17:20:26 +00002861 Out << "So";
2862 return true;
2863 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002864
Anders Carlsson91f88602009-12-07 19:56:42 +00002865 // <substitution> ::= Sd # ::std::basic_iostream<char,
2866 // ::std::char_traits<char> >
2867 if (isStreamCharSpecialization(SD, "basic_iostream")) {
2868 Out << "Sd";
2869 return true;
2870 }
Anders Carlssonf514b542009-09-27 00:12:57 +00002871 }
Anders Carlsson8c031552009-09-26 23:10:05 +00002872 return false;
Anders Carlssone7c8cb62009-09-26 20:53:44 +00002873}
2874
Anders Carlsson76967372009-09-17 00:43:46 +00002875void CXXNameMangler::addSubstitution(QualType T) {
Anders Carlssond99edc42009-09-26 03:55:37 +00002876 if (!T.getCVRQualifiers()) {
2877 if (const RecordType *RT = T->getAs<RecordType>()) {
2878 addSubstitution(RT->getDecl());
2879 return;
2880 }
2881 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002882
Anders Carlsson76967372009-09-17 00:43:46 +00002883 uintptr_t TypePtr = reinterpret_cast<uintptr_t>(T.getAsOpaquePtr());
Anders Carlssond3a932a2009-09-17 03:53:28 +00002884 addSubstitution(TypePtr);
2885}
2886
Douglas Gregor1e9268e2010-04-28 05:58:56 +00002887void CXXNameMangler::addSubstitution(TemplateName Template) {
2888 if (TemplateDecl *TD = Template.getAsTemplateDecl())
2889 return addSubstitution(TD);
Sean Huntc3021132010-05-05 15:23:54 +00002890
Douglas Gregor1e9268e2010-04-28 05:58:56 +00002891 Template = Context.getASTContext().getCanonicalTemplateName(Template);
2892 addSubstitution(reinterpret_cast<uintptr_t>(Template.getAsVoidPointer()));
2893}
2894
Anders Carlssond3a932a2009-09-17 03:53:28 +00002895void CXXNameMangler::addSubstitution(uintptr_t Ptr) {
Anders Carlssond3a932a2009-09-17 03:53:28 +00002896 assert(!Substitutions.count(Ptr) && "Substitution already exists!");
Anders Carlsson9d85b722010-06-02 04:29:50 +00002897 Substitutions[Ptr] = SeqID++;
Anders Carlsson76967372009-09-17 00:43:46 +00002898}
2899
Daniel Dunbar1b077112009-11-21 09:06:10 +00002900//
Mike Stump1eb44332009-09-09 15:08:12 +00002901
Daniel Dunbar1b077112009-11-21 09:06:10 +00002902/// \brief Mangles the name of the declaration D and emits that name to the
2903/// given output stream.
2904///
2905/// If the declaration D requires a mangled name, this routine will emit that
2906/// mangled name to \p os and return true. Otherwise, \p os will be unchanged
2907/// and this routine will return false. In this case, the caller should just
2908/// emit the identifier of the declaration (\c D->getIdentifier()) as its
2909/// name.
Peter Collingbourne14110472011-01-13 18:57:25 +00002910void ItaniumMangleContext::mangleName(const NamedDecl *D,
Rafael Espindola0e376a02011-02-11 01:41:00 +00002911 llvm::raw_ostream &Out) {
Daniel Dunbarc02ab4c2009-11-21 09:14:44 +00002912 assert((isa<FunctionDecl>(D) || isa<VarDecl>(D)) &&
2913 "Invalid mangleName() call, argument is not a variable or function!");
2914 assert(!isa<CXXConstructorDecl>(D) && !isa<CXXDestructorDecl>(D) &&
2915 "Invalid mangleName() call on 'structor decl!");
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002916
Daniel Dunbar1b077112009-11-21 09:06:10 +00002917 PrettyStackTraceDecl CrashInfo(D, SourceLocation(),
2918 getASTContext().getSourceManager(),
2919 "Mangling declaration");
Mike Stump1eb44332009-09-09 15:08:12 +00002920
John McCallfb44de92011-05-01 22:35:37 +00002921 CXXNameMangler Mangler(*this, Out, D);
Daniel Dunbar94fd26d2009-11-21 09:06:22 +00002922 return Mangler.mangle(D);
Daniel Dunbar1b077112009-11-21 09:06:10 +00002923}
Mike Stump1eb44332009-09-09 15:08:12 +00002924
Peter Collingbourne14110472011-01-13 18:57:25 +00002925void ItaniumMangleContext::mangleCXXCtor(const CXXConstructorDecl *D,
2926 CXXCtorType Type,
Rafael Espindola0e376a02011-02-11 01:41:00 +00002927 llvm::raw_ostream &Out) {
Rafael Espindolac4850c22011-02-10 23:59:36 +00002928 CXXNameMangler Mangler(*this, Out, D, Type);
Daniel Dunbar77939c92009-11-21 09:06:31 +00002929 Mangler.mangle(D);
Daniel Dunbar1b077112009-11-21 09:06:10 +00002930}
Mike Stump1eb44332009-09-09 15:08:12 +00002931
Peter Collingbourne14110472011-01-13 18:57:25 +00002932void ItaniumMangleContext::mangleCXXDtor(const CXXDestructorDecl *D,
2933 CXXDtorType Type,
Rafael Espindola0e376a02011-02-11 01:41:00 +00002934 llvm::raw_ostream &Out) {
Rafael Espindolac4850c22011-02-10 23:59:36 +00002935 CXXNameMangler Mangler(*this, Out, D, Type);
Daniel Dunbar77939c92009-11-21 09:06:31 +00002936 Mangler.mangle(D);
Daniel Dunbar1b077112009-11-21 09:06:10 +00002937}
Mike Stumpf1216772009-07-31 18:25:34 +00002938
Peter Collingbourne14110472011-01-13 18:57:25 +00002939void ItaniumMangleContext::mangleThunk(const CXXMethodDecl *MD,
2940 const ThunkInfo &Thunk,
Rafael Espindolaf0be9792011-02-11 02:52:17 +00002941 llvm::raw_ostream &Out) {
Anders Carlsson19879c92010-03-23 17:17:29 +00002942 // <special-name> ::= T <call-offset> <base encoding>
2943 // # base is the nominal target function of thunk
2944 // <special-name> ::= Tc <call-offset> <call-offset> <base encoding>
2945 // # base is the nominal target function of thunk
2946 // # first call-offset is 'this' adjustment
2947 // # second call-offset is result adjustment
Sean Huntc3021132010-05-05 15:23:54 +00002948
Anders Carlsson19879c92010-03-23 17:17:29 +00002949 assert(!isa<CXXDestructorDecl>(MD) &&
2950 "Use mangleCXXDtor for destructor decls!");
Rafael Espindolac4850c22011-02-10 23:59:36 +00002951 CXXNameMangler Mangler(*this, Out);
Anders Carlsson19879c92010-03-23 17:17:29 +00002952 Mangler.getStream() << "_ZT";
2953 if (!Thunk.Return.isEmpty())
2954 Mangler.getStream() << 'c';
Sean Huntc3021132010-05-05 15:23:54 +00002955
Anders Carlsson19879c92010-03-23 17:17:29 +00002956 // Mangle the 'this' pointer adjustment.
2957 Mangler.mangleCallOffset(Thunk.This.NonVirtual, Thunk.This.VCallOffsetOffset);
Sean Huntc3021132010-05-05 15:23:54 +00002958
Anders Carlsson19879c92010-03-23 17:17:29 +00002959 // Mangle the return pointer adjustment if there is one.
2960 if (!Thunk.Return.isEmpty())
2961 Mangler.mangleCallOffset(Thunk.Return.NonVirtual,
2962 Thunk.Return.VBaseOffsetOffset);
Sean Huntc3021132010-05-05 15:23:54 +00002963
Anders Carlsson19879c92010-03-23 17:17:29 +00002964 Mangler.mangleFunctionEncoding(MD);
2965}
2966
Sean Huntc3021132010-05-05 15:23:54 +00002967void
Peter Collingbourne14110472011-01-13 18:57:25 +00002968ItaniumMangleContext::mangleCXXDtorThunk(const CXXDestructorDecl *DD,
2969 CXXDtorType Type,
2970 const ThisAdjustment &ThisAdjustment,
Rafael Espindolaf0be9792011-02-11 02:52:17 +00002971 llvm::raw_ostream &Out) {
Anders Carlsson19879c92010-03-23 17:17:29 +00002972 // <special-name> ::= T <call-offset> <base encoding>
2973 // # base is the nominal target function of thunk
Rafael Espindolac4850c22011-02-10 23:59:36 +00002974 CXXNameMangler Mangler(*this, Out, DD, Type);
Anders Carlsson19879c92010-03-23 17:17:29 +00002975 Mangler.getStream() << "_ZT";
2976
2977 // Mangle the 'this' pointer adjustment.
Sean Huntc3021132010-05-05 15:23:54 +00002978 Mangler.mangleCallOffset(ThisAdjustment.NonVirtual,
Anders Carlsson19879c92010-03-23 17:17:29 +00002979 ThisAdjustment.VCallOffsetOffset);
2980
2981 Mangler.mangleFunctionEncoding(DD);
2982}
2983
Daniel Dunbarc0747712009-11-21 09:12:13 +00002984/// mangleGuardVariable - Returns the mangled name for a guard variable
2985/// for the passed in VarDecl.
Peter Collingbourne14110472011-01-13 18:57:25 +00002986void ItaniumMangleContext::mangleItaniumGuardVariable(const VarDecl *D,
Rafael Espindolaf0be9792011-02-11 02:52:17 +00002987 llvm::raw_ostream &Out) {
Daniel Dunbarc0747712009-11-21 09:12:13 +00002988 // <special-name> ::= GV <object name> # Guard variable for one-time
2989 // # initialization
Rafael Espindolac4850c22011-02-10 23:59:36 +00002990 CXXNameMangler Mangler(*this, Out);
Daniel Dunbarc0747712009-11-21 09:12:13 +00002991 Mangler.getStream() << "_ZGV";
2992 Mangler.mangleName(D);
2993}
2994
Peter Collingbourne14110472011-01-13 18:57:25 +00002995void ItaniumMangleContext::mangleReferenceTemporary(const VarDecl *D,
Rafael Espindolaf0be9792011-02-11 02:52:17 +00002996 llvm::raw_ostream &Out) {
Anders Carlsson715edf22010-06-26 16:09:40 +00002997 // We match the GCC mangling here.
2998 // <special-name> ::= GR <object name>
Rafael Espindolac4850c22011-02-10 23:59:36 +00002999 CXXNameMangler Mangler(*this, Out);
Anders Carlsson715edf22010-06-26 16:09:40 +00003000 Mangler.getStream() << "_ZGR";
3001 Mangler.mangleName(D);
3002}
3003
Peter Collingbourne14110472011-01-13 18:57:25 +00003004void ItaniumMangleContext::mangleCXXVTable(const CXXRecordDecl *RD,
Rafael Espindolaf0be9792011-02-11 02:52:17 +00003005 llvm::raw_ostream &Out) {
Daniel Dunbarc0747712009-11-21 09:12:13 +00003006 // <special-name> ::= TV <type> # virtual table
Rafael Espindolac4850c22011-02-10 23:59:36 +00003007 CXXNameMangler Mangler(*this, Out);
Daniel Dunbarc0747712009-11-21 09:12:13 +00003008 Mangler.getStream() << "_ZTV";
Douglas Gregor1b12a3b2010-05-26 05:11:13 +00003009 Mangler.mangleNameOrStandardSubstitution(RD);
Daniel Dunbar1b077112009-11-21 09:06:10 +00003010}
Mike Stump82d75b02009-11-10 01:58:37 +00003011
Peter Collingbourne14110472011-01-13 18:57:25 +00003012void ItaniumMangleContext::mangleCXXVTT(const CXXRecordDecl *RD,
Rafael Espindolaf0be9792011-02-11 02:52:17 +00003013 llvm::raw_ostream &Out) {
Daniel Dunbarc0747712009-11-21 09:12:13 +00003014 // <special-name> ::= TT <type> # VTT structure
Rafael Espindolac4850c22011-02-10 23:59:36 +00003015 CXXNameMangler Mangler(*this, Out);
Daniel Dunbarc0747712009-11-21 09:12:13 +00003016 Mangler.getStream() << "_ZTT";
Douglas Gregor1b12a3b2010-05-26 05:11:13 +00003017 Mangler.mangleNameOrStandardSubstitution(RD);
Daniel Dunbar1b077112009-11-21 09:06:10 +00003018}
Mike Stumpab3f7e92009-11-10 01:41:59 +00003019
Peter Collingbourne14110472011-01-13 18:57:25 +00003020void ItaniumMangleContext::mangleCXXCtorVTable(const CXXRecordDecl *RD,
3021 int64_t Offset,
3022 const CXXRecordDecl *Type,
Rafael Espindolaf0be9792011-02-11 02:52:17 +00003023 llvm::raw_ostream &Out) {
Daniel Dunbarc0747712009-11-21 09:12:13 +00003024 // <special-name> ::= TC <type> <offset number> _ <base type>
Rafael Espindolac4850c22011-02-10 23:59:36 +00003025 CXXNameMangler Mangler(*this, Out);
Daniel Dunbarc0747712009-11-21 09:12:13 +00003026 Mangler.getStream() << "_ZTC";
Douglas Gregor1b12a3b2010-05-26 05:11:13 +00003027 Mangler.mangleNameOrStandardSubstitution(RD);
Daniel Dunbarc0747712009-11-21 09:12:13 +00003028 Mangler.getStream() << Offset;
Benjamin Kramer35f59b62010-04-10 16:03:31 +00003029 Mangler.getStream() << '_';
Douglas Gregor1b12a3b2010-05-26 05:11:13 +00003030 Mangler.mangleNameOrStandardSubstitution(Type);
Daniel Dunbar1b077112009-11-21 09:06:10 +00003031}
Mike Stump738f8c22009-07-31 23:15:31 +00003032
Peter Collingbourne14110472011-01-13 18:57:25 +00003033void ItaniumMangleContext::mangleCXXRTTI(QualType Ty,
Rafael Espindolaf0be9792011-02-11 02:52:17 +00003034 llvm::raw_ostream &Out) {
Daniel Dunbarc0747712009-11-21 09:12:13 +00003035 // <special-name> ::= TI <type> # typeinfo structure
Douglas Gregor154fe982009-12-23 22:04:40 +00003036 assert(!Ty.hasQualifiers() && "RTTI info cannot have top-level qualifiers");
Rafael Espindolac4850c22011-02-10 23:59:36 +00003037 CXXNameMangler Mangler(*this, Out);
Daniel Dunbarc0747712009-11-21 09:12:13 +00003038 Mangler.getStream() << "_ZTI";
3039 Mangler.mangleType(Ty);
Daniel Dunbar1b077112009-11-21 09:06:10 +00003040}
Mike Stump67795982009-11-14 00:14:13 +00003041
Peter Collingbourne14110472011-01-13 18:57:25 +00003042void ItaniumMangleContext::mangleCXXRTTIName(QualType Ty,
Rafael Espindolaf0be9792011-02-11 02:52:17 +00003043 llvm::raw_ostream &Out) {
Daniel Dunbarc0747712009-11-21 09:12:13 +00003044 // <special-name> ::= TS <type> # typeinfo name (null terminated byte string)
Rafael Espindolac4850c22011-02-10 23:59:36 +00003045 CXXNameMangler Mangler(*this, Out);
Daniel Dunbarc0747712009-11-21 09:12:13 +00003046 Mangler.getStream() << "_ZTS";
3047 Mangler.mangleType(Ty);
Mike Stumpf1216772009-07-31 18:25:34 +00003048}
Peter Collingbourne14110472011-01-13 18:57:25 +00003049
3050MangleContext *clang::createItaniumMangleContext(ASTContext &Context,
3051 Diagnostic &Diags) {
3052 return new ItaniumMangleContext(Context, Diags);
3053}