blob: a0bb6c74c33a15e5a4182152d43d972c41dd923b [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 McCallf85e1932011-06-15 23:02:42 +000024#include "clang/AST/ExprObjC.h"
John McCallfb44de92011-05-01 22:35:37 +000025#include "clang/AST/TypeLoc.h"
Peter Collingbourne14110472011-01-13 18:57:25 +000026#include "clang/Basic/ABI.h"
Douglas Gregor6ec36682009-02-18 23:53:56 +000027#include "clang/Basic/SourceManager.h"
Rafael Espindola4e274e92011-02-15 22:23:51 +000028#include "clang/Basic/TargetInfo.h"
Anders Carlssonc4355b62009-10-07 01:45:02 +000029#include "llvm/ADT/StringExtras.h"
Douglas Gregor5f2bfd42009-02-13 00:10:09 +000030#include "llvm/Support/raw_ostream.h"
John McCallefe6aee2009-09-05 07:56:18 +000031#include "llvm/Support/ErrorHandling.h"
Anders Carlssonf98574b2010-02-05 07:31:37 +000032
33#define MANGLE_CHECKER 0
34
35#if MANGLE_CHECKER
36#include <cxxabi.h>
37#endif
38
Douglas Gregor5f2bfd42009-02-13 00:10:09 +000039using namespace clang;
Charles Davis685b1d92010-05-26 18:25:27 +000040
Douglas Gregor5f2bfd42009-02-13 00:10:09 +000041namespace {
Fariborz Jahanian57058532010-03-03 19:41:08 +000042
John McCall82b7d7b2010-10-18 21:28:44 +000043static const CXXRecordDecl *GetLocalClassDecl(const NamedDecl *ND) {
44 const DeclContext *DC = dyn_cast<DeclContext>(ND);
45 if (!DC)
46 DC = ND->getDeclContext();
47 while (!DC->isNamespace() && !DC->isTranslationUnit()) {
48 if (isa<FunctionDecl>(DC->getParent()))
49 return dyn_cast<CXXRecordDecl>(DC);
50 DC = DC->getParent();
Fariborz Jahanian57058532010-03-03 19:41:08 +000051 }
52 return 0;
53}
54
John McCallfb44de92011-05-01 22:35:37 +000055static const FunctionDecl *getStructor(const FunctionDecl *fn) {
56 if (const FunctionTemplateDecl *ftd = fn->getPrimaryTemplate())
57 return ftd->getTemplatedDecl();
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +000058
John McCallfb44de92011-05-01 22:35:37 +000059 return fn;
60}
Anders Carlsson7e120032009-11-24 05:36:32 +000061
John McCallfb44de92011-05-01 22:35:37 +000062static const NamedDecl *getStructor(const NamedDecl *decl) {
63 const FunctionDecl *fn = dyn_cast_or_null<FunctionDecl>(decl);
64 return (fn ? getStructor(fn) : decl);
Anders Carlsson7e120032009-11-24 05:36:32 +000065}
John McCall1dd73832010-02-04 01:42:13 +000066
67static const unsigned UnknownArity = ~0U;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +000068
Peter Collingbourne14110472011-01-13 18:57:25 +000069class ItaniumMangleContext : public MangleContext {
70 llvm::DenseMap<const TagDecl *, uint64_t> AnonStructIds;
71 unsigned Discriminator;
72 llvm::DenseMap<const NamedDecl*, unsigned> Uniquifier;
73
74public:
75 explicit ItaniumMangleContext(ASTContext &Context,
David Blaikied6471f72011-09-25 23:23:43 +000076 DiagnosticsEngine &Diags)
Peter Collingbourne14110472011-01-13 18:57:25 +000077 : MangleContext(Context, Diags) { }
78
79 uint64_t getAnonymousStructId(const TagDecl *TD) {
80 std::pair<llvm::DenseMap<const TagDecl *,
81 uint64_t>::iterator, bool> Result =
82 AnonStructIds.insert(std::make_pair(TD, AnonStructIds.size()));
83 return Result.first->second;
84 }
85
86 void startNewFunction() {
87 MangleContext::startNewFunction();
88 mangleInitDiscriminator();
89 }
90
91 /// @name Mangler Entry Points
92 /// @{
93
94 bool shouldMangleDeclName(const NamedDecl *D);
Chris Lattner5f9e2722011-07-23 10:55:15 +000095 void mangleName(const NamedDecl *D, raw_ostream &);
Peter Collingbourne14110472011-01-13 18:57:25 +000096 void mangleThunk(const CXXMethodDecl *MD,
97 const ThunkInfo &Thunk,
Chris Lattner5f9e2722011-07-23 10:55:15 +000098 raw_ostream &);
Peter Collingbourne14110472011-01-13 18:57:25 +000099 void mangleCXXDtorThunk(const CXXDestructorDecl *DD, CXXDtorType Type,
100 const ThisAdjustment &ThisAdjustment,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000101 raw_ostream &);
Peter Collingbourne14110472011-01-13 18:57:25 +0000102 void mangleReferenceTemporary(const VarDecl *D,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000103 raw_ostream &);
Peter Collingbourne14110472011-01-13 18:57:25 +0000104 void mangleCXXVTable(const CXXRecordDecl *RD,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000105 raw_ostream &);
Peter Collingbourne14110472011-01-13 18:57:25 +0000106 void mangleCXXVTT(const CXXRecordDecl *RD,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000107 raw_ostream &);
Peter Collingbourne14110472011-01-13 18:57:25 +0000108 void mangleCXXCtorVTable(const CXXRecordDecl *RD, int64_t Offset,
109 const CXXRecordDecl *Type,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000110 raw_ostream &);
111 void mangleCXXRTTI(QualType T, raw_ostream &);
112 void mangleCXXRTTIName(QualType T, raw_ostream &);
Peter Collingbourne14110472011-01-13 18:57:25 +0000113 void mangleCXXCtor(const CXXConstructorDecl *D, CXXCtorType Type,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000114 raw_ostream &);
Peter Collingbourne14110472011-01-13 18:57:25 +0000115 void mangleCXXDtor(const CXXDestructorDecl *D, CXXDtorType Type,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000116 raw_ostream &);
Peter Collingbourne14110472011-01-13 18:57:25 +0000117
Chris Lattner5f9e2722011-07-23 10:55:15 +0000118 void mangleItaniumGuardVariable(const VarDecl *D, raw_ostream &);
Peter Collingbourne14110472011-01-13 18:57:25 +0000119
120 void mangleInitDiscriminator() {
121 Discriminator = 0;
122 }
123
124 bool getNextDiscriminator(const NamedDecl *ND, unsigned &disc) {
Douglas Gregor9e8c92a2012-02-20 19:44:39 +0000125 // Lambda closure types with external linkage (indicated by a
126 // non-zero lambda mangling number) have their own numbering scheme, so
127 // they do not need a discriminator.
128 if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(ND))
129 if (RD->isLambda() && RD->getLambdaManglingNumber() > 0)
130 return false;
131
Peter Collingbourne14110472011-01-13 18:57:25 +0000132 unsigned &discriminator = Uniquifier[ND];
133 if (!discriminator)
134 discriminator = ++Discriminator;
135 if (discriminator == 1)
136 return false;
137 disc = discriminator-2;
138 return true;
139 }
140 /// @}
141};
142
Daniel Dunbar1b077112009-11-21 09:06:10 +0000143/// CXXNameMangler - Manage the mangling of a single name.
Daniel Dunbarc0747712009-11-21 09:12:13 +0000144class CXXNameMangler {
Peter Collingbourne14110472011-01-13 18:57:25 +0000145 ItaniumMangleContext &Context;
Chris Lattner5f9e2722011-07-23 10:55:15 +0000146 raw_ostream &Out;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000147
John McCallfb44de92011-05-01 22:35:37 +0000148 /// The "structor" is the top-level declaration being mangled, if
149 /// that's not a template specialization; otherwise it's the pattern
150 /// for that specialization.
151 const NamedDecl *Structor;
Daniel Dunbar1b077112009-11-21 09:06:10 +0000152 unsigned StructorType;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000153
Anders Carlsson9d85b722010-06-02 04:29:50 +0000154 /// SeqID - The next subsitution sequence number.
155 unsigned SeqID;
156
John McCallfb44de92011-05-01 22:35:37 +0000157 class FunctionTypeDepthState {
158 unsigned Bits;
159
160 enum { InResultTypeMask = 1 };
161
162 public:
163 FunctionTypeDepthState() : Bits(0) {}
164
165 /// The number of function types we're inside.
166 unsigned getDepth() const {
167 return Bits >> 1;
168 }
169
170 /// True if we're in the return type of the innermost function type.
171 bool isInResultType() const {
172 return Bits & InResultTypeMask;
173 }
174
175 FunctionTypeDepthState push() {
176 FunctionTypeDepthState tmp = *this;
177 Bits = (Bits & ~InResultTypeMask) + 2;
178 return tmp;
179 }
180
181 void enterResultType() {
182 Bits |= InResultTypeMask;
183 }
184
185 void leaveResultType() {
186 Bits &= ~InResultTypeMask;
187 }
188
189 void pop(FunctionTypeDepthState saved) {
190 assert(getDepth() == saved.getDepth() + 1);
191 Bits = saved.Bits;
192 }
193
194 } FunctionTypeDepth;
195
Daniel Dunbar1b077112009-11-21 09:06:10 +0000196 llvm::DenseMap<uintptr_t, unsigned> Substitutions;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000197
John McCall1dd73832010-02-04 01:42:13 +0000198 ASTContext &getASTContext() const { return Context.getASTContext(); }
199
Daniel Dunbarc0747712009-11-21 09:12:13 +0000200public:
Chris Lattner5f9e2722011-07-23 10:55:15 +0000201 CXXNameMangler(ItaniumMangleContext &C, raw_ostream &Out_,
John McCallfb44de92011-05-01 22:35:37 +0000202 const NamedDecl *D = 0)
203 : Context(C), Out(Out_), Structor(getStructor(D)), StructorType(0),
204 SeqID(0) {
205 // These can't be mangled without a ctor type or dtor type.
206 assert(!D || (!isa<CXXDestructorDecl>(D) &&
207 !isa<CXXConstructorDecl>(D)));
208 }
Chris Lattner5f9e2722011-07-23 10:55:15 +0000209 CXXNameMangler(ItaniumMangleContext &C, raw_ostream &Out_,
Daniel Dunbar77939c92009-11-21 09:06:31 +0000210 const CXXConstructorDecl *D, CXXCtorType Type)
Rafael Espindolac4850c22011-02-10 23:59:36 +0000211 : Context(C), Out(Out_), Structor(getStructor(D)), StructorType(Type),
John McCallfb44de92011-05-01 22:35:37 +0000212 SeqID(0) { }
Chris Lattner5f9e2722011-07-23 10:55:15 +0000213 CXXNameMangler(ItaniumMangleContext &C, raw_ostream &Out_,
Daniel Dunbar77939c92009-11-21 09:06:31 +0000214 const CXXDestructorDecl *D, CXXDtorType Type)
Rafael Espindolac4850c22011-02-10 23:59:36 +0000215 : Context(C), Out(Out_), Structor(getStructor(D)), StructorType(Type),
John McCallfb44de92011-05-01 22:35:37 +0000216 SeqID(0) { }
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000217
Anders Carlssonf98574b2010-02-05 07:31:37 +0000218#if MANGLE_CHECKER
219 ~CXXNameMangler() {
220 if (Out.str()[0] == '\01')
221 return;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000222
Anders Carlssonf98574b2010-02-05 07:31:37 +0000223 int status = 0;
224 char *result = abi::__cxa_demangle(Out.str().str().c_str(), 0, 0, &status);
225 assert(status == 0 && "Could not demangle mangled name!");
226 free(result);
227 }
228#endif
Chris Lattner5f9e2722011-07-23 10:55:15 +0000229 raw_ostream &getStream() { return Out; }
Daniel Dunbarc0747712009-11-21 09:12:13 +0000230
Chris Lattner5f9e2722011-07-23 10:55:15 +0000231 void mangle(const NamedDecl *D, StringRef Prefix = "_Z");
Anders Carlsson19879c92010-03-23 17:17:29 +0000232 void mangleCallOffset(int64_t NonVirtual, int64_t Virtual);
John McCall0512e482010-07-14 04:20:34 +0000233 void mangleNumber(const llvm::APSInt &I);
Anders Carlssona94822e2009-11-26 02:32:05 +0000234 void mangleNumber(int64_t Number);
John McCall0512e482010-07-14 04:20:34 +0000235 void mangleFloat(const llvm::APFloat &F);
Daniel Dunbarc0747712009-11-21 09:12:13 +0000236 void mangleFunctionEncoding(const FunctionDecl *FD);
237 void mangleName(const NamedDecl *ND);
238 void mangleType(QualType T);
Douglas Gregor1b12a3b2010-05-26 05:11:13 +0000239 void mangleNameOrStandardSubstitution(const NamedDecl *ND);
240
Daniel Dunbarc0747712009-11-21 09:12:13 +0000241private:
Daniel Dunbar1b077112009-11-21 09:06:10 +0000242 bool mangleSubstitution(const NamedDecl *ND);
243 bool mangleSubstitution(QualType T);
Douglas Gregor1e9268e2010-04-28 05:58:56 +0000244 bool mangleSubstitution(TemplateName Template);
Daniel Dunbar1b077112009-11-21 09:06:10 +0000245 bool mangleSubstitution(uintptr_t Ptr);
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000246
John McCall68a51a72011-07-01 00:04:39 +0000247 void mangleExistingSubstitution(QualType type);
248 void mangleExistingSubstitution(TemplateName name);
249
Daniel Dunbar1b077112009-11-21 09:06:10 +0000250 bool mangleStandardSubstitution(const NamedDecl *ND);
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000251
Daniel Dunbar1b077112009-11-21 09:06:10 +0000252 void addSubstitution(const NamedDecl *ND) {
253 ND = cast<NamedDecl>(ND->getCanonicalDecl());
Anders Carlsson433d1372009-11-07 04:26:04 +0000254
Daniel Dunbar1b077112009-11-21 09:06:10 +0000255 addSubstitution(reinterpret_cast<uintptr_t>(ND));
256 }
257 void addSubstitution(QualType T);
Douglas Gregor1e9268e2010-04-28 05:58:56 +0000258 void addSubstitution(TemplateName Template);
Daniel Dunbar1b077112009-11-21 09:06:10 +0000259 void addSubstitution(uintptr_t Ptr);
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000260
John McCalla0ce15c2011-04-24 08:23:24 +0000261 void mangleUnresolvedPrefix(NestedNameSpecifier *qualifier,
262 NamedDecl *firstQualifierLookup,
263 bool recursive = false);
264 void mangleUnresolvedName(NestedNameSpecifier *qualifier,
265 NamedDecl *firstQualifierLookup,
266 DeclarationName name,
John McCall1dd73832010-02-04 01:42:13 +0000267 unsigned KnownArity = UnknownArity);
268
Daniel Dunbar1b077112009-11-21 09:06:10 +0000269 void mangleName(const TemplateDecl *TD,
270 const TemplateArgument *TemplateArgs,
271 unsigned NumTemplateArgs);
John McCall1dd73832010-02-04 01:42:13 +0000272 void mangleUnqualifiedName(const NamedDecl *ND) {
273 mangleUnqualifiedName(ND, ND->getDeclName(), UnknownArity);
274 }
275 void mangleUnqualifiedName(const NamedDecl *ND, DeclarationName Name,
276 unsigned KnownArity);
Daniel Dunbar1b077112009-11-21 09:06:10 +0000277 void mangleUnscopedName(const NamedDecl *ND);
278 void mangleUnscopedTemplateName(const TemplateDecl *ND);
Douglas Gregor1e9268e2010-04-28 05:58:56 +0000279 void mangleUnscopedTemplateName(TemplateName);
Daniel Dunbar1b077112009-11-21 09:06:10 +0000280 void mangleSourceName(const IdentifierInfo *II);
281 void mangleLocalName(const NamedDecl *ND);
Fariborz Jahanian57058532010-03-03 19:41:08 +0000282 void mangleNestedName(const NamedDecl *ND, const DeclContext *DC,
283 bool NoFunction=false);
Daniel Dunbar1b077112009-11-21 09:06:10 +0000284 void mangleNestedName(const TemplateDecl *TD,
285 const TemplateArgument *TemplateArgs,
286 unsigned NumTemplateArgs);
John McCalla0ce15c2011-04-24 08:23:24 +0000287 void manglePrefix(NestedNameSpecifier *qualifier);
Fariborz Jahanian57058532010-03-03 19:41:08 +0000288 void manglePrefix(const DeclContext *DC, bool NoFunction=false);
John McCall4f4e4132011-05-04 01:45:19 +0000289 void manglePrefix(QualType type);
Daniel Dunbar1b077112009-11-21 09:06:10 +0000290 void mangleTemplatePrefix(const TemplateDecl *ND);
Douglas Gregor20f0cc72010-04-23 03:10:43 +0000291 void mangleTemplatePrefix(TemplateName Template);
Daniel Dunbar1b077112009-11-21 09:06:10 +0000292 void mangleOperatorName(OverloadedOperatorKind OO, unsigned Arity);
293 void mangleQualifiers(Qualifiers Quals);
Douglas Gregor0a9a6d62011-01-26 17:36:28 +0000294 void mangleRefQualifier(RefQualifierKind RefQualifier);
John McCallefe6aee2009-09-05 07:56:18 +0000295
Anders Carlsson7b06f6c2009-12-10 03:14:39 +0000296 void mangleObjCMethodName(const ObjCMethodDecl *MD);
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000297
Daniel Dunbar1b077112009-11-21 09:06:10 +0000298 // Declare manglers for every type class.
John McCallefe6aee2009-09-05 07:56:18 +0000299#define ABSTRACT_TYPE(CLASS, PARENT)
300#define NON_CANONICAL_TYPE(CLASS, PARENT)
301#define TYPE(CLASS, PARENT) void mangleType(const CLASS##Type *T);
302#include "clang/AST/TypeNodes.def"
303
Daniel Dunbar1b077112009-11-21 09:06:10 +0000304 void mangleType(const TagType*);
John McCallb6f532e2010-07-14 06:43:17 +0000305 void mangleType(TemplateName);
Daniel Dunbar1b077112009-11-21 09:06:10 +0000306 void mangleBareFunctionType(const FunctionType *T,
307 bool MangleReturnType);
Bob Wilson57147a82010-11-16 00:32:18 +0000308 void mangleNeonVectorType(const VectorType *T);
Anders Carlssone170ba72009-12-14 01:45:37 +0000309
310 void mangleIntegerLiteral(QualType T, const llvm::APSInt &Value);
John McCalla0ce15c2011-04-24 08:23:24 +0000311 void mangleMemberExpr(const Expr *base, bool isArrow,
312 NestedNameSpecifier *qualifier,
313 NamedDecl *firstQualifierLookup,
314 DeclarationName name,
315 unsigned knownArity);
John McCall5e1e89b2010-08-18 19:18:59 +0000316 void mangleExpression(const Expr *E, unsigned Arity = UnknownArity);
Daniel Dunbar1b077112009-11-21 09:06:10 +0000317 void mangleCXXCtorType(CXXCtorType T);
318 void mangleCXXDtorType(CXXDtorType T);
Mike Stump1eb44332009-09-09 15:08:12 +0000319
Argyrios Kyrtzidisb0c3e092011-09-22 20:07:03 +0000320 void mangleTemplateArgs(const ASTTemplateArgumentListInfo &TemplateArgs);
Douglas Gregor20f0cc72010-04-23 03:10:43 +0000321 void mangleTemplateArgs(TemplateName Template,
322 const TemplateArgument *TemplateArgs,
Sean Huntc3021132010-05-05 15:23:54 +0000323 unsigned NumTemplateArgs);
Rafael Espindolad9800722010-03-11 14:07:00 +0000324 void mangleTemplateArgs(const TemplateParameterList &PL,
325 const TemplateArgument *TemplateArgs,
Daniel Dunbar1b077112009-11-21 09:06:10 +0000326 unsigned NumTemplateArgs);
Rafael Espindolad9800722010-03-11 14:07:00 +0000327 void mangleTemplateArgs(const TemplateParameterList &PL,
328 const TemplateArgumentList &AL);
Douglas Gregorf1588662011-07-12 15:18:55 +0000329 void mangleTemplateArg(const NamedDecl *P, TemplateArgument A);
John McCall4f4e4132011-05-04 01:45:19 +0000330 void mangleUnresolvedTemplateArgs(const TemplateArgument *args,
331 unsigned numArgs);
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000332
Daniel Dunbar1b077112009-11-21 09:06:10 +0000333 void mangleTemplateParameter(unsigned Index);
John McCallfb44de92011-05-01 22:35:37 +0000334
335 void mangleFunctionParam(const ParmVarDecl *parm);
Daniel Dunbar1b077112009-11-21 09:06:10 +0000336};
Peter Collingbourne14110472011-01-13 18:57:25 +0000337
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000338}
339
Anders Carlsson43f17402009-04-02 15:51:53 +0000340static bool isInCLinkageSpecification(const Decl *D) {
Douglas Gregor457e2812009-10-28 16:31:34 +0000341 D = D->getCanonicalDecl();
Mike Stump1eb44332009-09-09 15:08:12 +0000342 for (const DeclContext *DC = D->getDeclContext();
Anders Carlsson43f17402009-04-02 15:51:53 +0000343 !DC->isTranslationUnit(); DC = DC->getParent()) {
Mike Stump1eb44332009-09-09 15:08:12 +0000344 if (const LinkageSpecDecl *Linkage = dyn_cast<LinkageSpecDecl>(DC))
Anders Carlsson43f17402009-04-02 15:51:53 +0000345 return Linkage->getLanguage() == LinkageSpecDecl::lang_c;
346 }
Mike Stump1eb44332009-09-09 15:08:12 +0000347
Anders Carlsson43f17402009-04-02 15:51:53 +0000348 return false;
349}
350
Peter Collingbourne14110472011-01-13 18:57:25 +0000351bool ItaniumMangleContext::shouldMangleDeclName(const NamedDecl *D) {
Daniel Dunbarf981bf82009-11-21 09:14:52 +0000352 // In C, functions with no attributes never need to be mangled. Fastpath them.
353 if (!getASTContext().getLangOptions().CPlusPlus && !D->hasAttrs())
354 return false;
355
356 // Any decl can be declared with __asm("foo") on it, and this takes precedence
357 // over all other naming in the .o file.
358 if (D->hasAttr<AsmLabelAttr>())
359 return true;
360
Mike Stump141c5af2009-09-02 00:25:38 +0000361 // Clang's "overloadable" attribute extension to C/C++ implies name mangling
Anders Carlssona1e16222009-11-07 07:15:03 +0000362 // (always) as does passing a C++ member function and a function
363 // whose name is not a simple identifier.
Daniel Dunbarf981bf82009-11-21 09:14:52 +0000364 const FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
365 if (FD && (FD->hasAttr<OverloadableAttr>() || isa<CXXMethodDecl>(FD) ||
366 !FD->getDeclName().isIdentifier()))
367 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000368
Daniel Dunbarf981bf82009-11-21 09:14:52 +0000369 // Otherwise, no mangling is done outside C++ mode.
370 if (!getASTContext().getLangOptions().CPlusPlus)
371 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000372
Sean Hunt31455252010-01-24 03:04:27 +0000373 // Variables at global scope with non-internal linkage are not mangled
Eli Friedman7facf842009-12-02 20:32:49 +0000374 if (!FD) {
375 const DeclContext *DC = D->getDeclContext();
376 // Check for extern variable declared locally.
Fariborz Jahaniane81c5612010-06-30 18:57:21 +0000377 if (DC->isFunctionOrMethod() && D->hasLinkage())
Eli Friedman7facf842009-12-02 20:32:49 +0000378 while (!DC->isNamespace() && !DC->isTranslationUnit())
379 DC = DC->getParent();
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000380 if (DC->isTranslationUnit() && D->getLinkage() != InternalLinkage)
Eli Friedman7facf842009-12-02 20:32:49 +0000381 return false;
382 }
383
Eli Friedmanc00cb642010-07-18 20:49:59 +0000384 // Class members are always mangled.
385 if (D->getDeclContext()->isRecord())
386 return true;
387
Eli Friedman7facf842009-12-02 20:32:49 +0000388 // C functions and "main" are not mangled.
389 if ((FD && FD->isMain()) || isInCLinkageSpecification(D))
Daniel Dunbarf981bf82009-11-21 09:14:52 +0000390 return false;
391
Anders Carlsson43f17402009-04-02 15:51:53 +0000392 return true;
393}
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000394
Chris Lattner5f9e2722011-07-23 10:55:15 +0000395void CXXNameMangler::mangle(const NamedDecl *D, StringRef Prefix) {
Mike Stump141c5af2009-09-02 00:25:38 +0000396 // Any decl can be declared with __asm("foo") on it, and this takes precedence
397 // over all other naming in the .o file.
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000398 if (const AsmLabelAttr *ALA = D->getAttr<AsmLabelAttr>()) {
Chris Lattnerca3f25c2009-03-21 08:24:40 +0000399 // If we have an asm name, then we use it as the mangling.
Rafael Espindola4e274e92011-02-15 22:23:51 +0000400
401 // Adding the prefix can cause problems when one file has a "foo" and
402 // another has a "\01foo". That is known to happen on ELF with the
403 // tricks normally used for producing aliases (PR9177). Fortunately the
404 // llvm mangler on ELF is a nop, so we can just avoid adding the \01
Peter Collingbourne69317432011-04-06 12:29:09 +0000405 // marker. We also avoid adding the marker if this is an alias for an
406 // LLVM intrinsic.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000407 StringRef UserLabelPrefix =
Douglas Gregorbcfd1f52011-09-02 00:18:52 +0000408 getASTContext().getTargetInfo().getUserLabelPrefix();
Peter Collingbourne69317432011-04-06 12:29:09 +0000409 if (!UserLabelPrefix.empty() && !ALA->getLabel().startswith("llvm."))
Rafael Espindola4e274e92011-02-15 22:23:51 +0000410 Out << '\01'; // LLVM IR Marker for __asm("foo")
411
Chris Lattnerca3f25c2009-03-21 08:24:40 +0000412 Out << ALA->getLabel();
Daniel Dunbarf981bf82009-11-21 09:14:52 +0000413 return;
Chris Lattnerca3f25c2009-03-21 08:24:40 +0000414 }
Mike Stump1eb44332009-09-09 15:08:12 +0000415
Sean Hunt31455252010-01-24 03:04:27 +0000416 // <mangled-name> ::= _Z <encoding>
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000417 // ::= <data name>
418 // ::= <special-name>
Daniel Dunbar7e0c1952009-11-21 09:17:15 +0000419 Out << Prefix;
420 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
Daniel Dunbarf981bf82009-11-21 09:14:52 +0000421 mangleFunctionEncoding(FD);
Rafael Espindolad9800722010-03-11 14:07:00 +0000422 else if (const VarDecl *VD = dyn_cast<VarDecl>(D))
423 mangleName(VD);
Daniel Dunbar7e0c1952009-11-21 09:17:15 +0000424 else
Rafael Espindolad9800722010-03-11 14:07:00 +0000425 mangleName(cast<FieldDecl>(D));
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000426}
427
428void CXXNameMangler::mangleFunctionEncoding(const FunctionDecl *FD) {
429 // <encoding> ::= <function name> <bare-function-type>
430 mangleName(FD);
Mike Stump1eb44332009-09-09 15:08:12 +0000431
Daniel Dunbar7e0c1952009-11-21 09:17:15 +0000432 // Don't mangle in the type if this isn't a decl we should typically mangle.
433 if (!Context.shouldMangleDeclName(FD))
434 return;
435
Mike Stump141c5af2009-09-02 00:25:38 +0000436 // Whether the mangling of a function type includes the return type depends on
437 // the context and the nature of the function. The rules for deciding whether
438 // the return type is included are:
Mike Stump1eb44332009-09-09 15:08:12 +0000439 //
Douglas Gregor1fd2dd12009-06-29 22:39:32 +0000440 // 1. Template functions (names or types) have return types encoded, with
441 // the exceptions listed below.
Mike Stump1eb44332009-09-09 15:08:12 +0000442 // 2. Function types not appearing as part of a function name mangling,
Douglas Gregor1fd2dd12009-06-29 22:39:32 +0000443 // e.g. parameters, pointer types, etc., have return type encoded, with the
444 // exceptions listed below.
445 // 3. Non-template function names do not have return types encoded.
446 //
Mike Stump141c5af2009-09-02 00:25:38 +0000447 // The exceptions mentioned in (1) and (2) above, for which the return type is
448 // never included, are
Douglas Gregor1fd2dd12009-06-29 22:39:32 +0000449 // 1. Constructors.
450 // 2. Destructors.
451 // 3. Conversion operator functions, e.g. operator int.
452 bool MangleReturnType = false;
Anders Carlsson9234b7f2009-09-17 03:46:43 +0000453 if (FunctionTemplateDecl *PrimaryTemplate = FD->getPrimaryTemplate()) {
454 if (!(isa<CXXConstructorDecl>(FD) || isa<CXXDestructorDecl>(FD) ||
455 isa<CXXConversionDecl>(FD)))
456 MangleReturnType = true;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000457
Anders Carlsson9234b7f2009-09-17 03:46:43 +0000458 // Mangle the type of the primary template.
459 FD = PrimaryTemplate->getTemplatedDecl();
460 }
461
Douglas Gregor79e6bd32011-07-12 04:42:08 +0000462 mangleBareFunctionType(FD->getType()->getAs<FunctionType>(),
463 MangleReturnType);
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000464}
465
Anders Carlsson47846d22009-12-04 06:23:23 +0000466static const DeclContext *IgnoreLinkageSpecDecls(const DeclContext *DC) {
467 while (isa<LinkageSpecDecl>(DC)) {
Anders Carlsson47846d22009-12-04 06:23:23 +0000468 DC = DC->getParent();
469 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000470
Anders Carlsson47846d22009-12-04 06:23:23 +0000471 return DC;
472}
473
Anders Carlssonc820f902010-06-02 15:58:27 +0000474/// isStd - Return whether a given namespace is the 'std' namespace.
475static bool isStd(const NamespaceDecl *NS) {
476 if (!IgnoreLinkageSpecDecls(NS->getParent())->isTranslationUnit())
477 return false;
478
479 const IdentifierInfo *II = NS->getOriginalNamespace()->getIdentifier();
480 return II && II->isStr("std");
481}
482
Anders Carlsson47846d22009-12-04 06:23:23 +0000483// isStdNamespace - Return whether a given decl context is a toplevel 'std'
484// namespace.
Daniel Dunbar1308af92009-11-21 09:11:45 +0000485static bool isStdNamespace(const DeclContext *DC) {
Anders Carlsson47846d22009-12-04 06:23:23 +0000486 if (!DC->isNamespace())
487 return false;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000488
Anders Carlsson47846d22009-12-04 06:23:23 +0000489 return isStd(cast<NamespaceDecl>(DC));
Daniel Dunbar1308af92009-11-21 09:11:45 +0000490}
491
Anders Carlssonbb36ba42009-09-26 03:24:57 +0000492static const TemplateDecl *
493isTemplate(const NamedDecl *ND, const TemplateArgumentList *&TemplateArgs) {
Anders Carlsson2744a062009-09-18 19:00:18 +0000494 // Check if we have a function template.
495 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)){
Anders Carlssonbb36ba42009-09-26 03:24:57 +0000496 if (const TemplateDecl *TD = FD->getPrimaryTemplate()) {
Anders Carlsson2744a062009-09-18 19:00:18 +0000497 TemplateArgs = FD->getTemplateSpecializationArgs();
Anders Carlssonbb36ba42009-09-26 03:24:57 +0000498 return TD;
Anders Carlsson2744a062009-09-18 19:00:18 +0000499 }
500 }
501
Anders Carlssoneafc6dc2009-09-18 19:44:50 +0000502 // Check if we have a class template.
503 if (const ClassTemplateSpecializationDecl *Spec =
504 dyn_cast<ClassTemplateSpecializationDecl>(ND)) {
505 TemplateArgs = &Spec->getTemplateArgs();
Anders Carlssonbb36ba42009-09-26 03:24:57 +0000506 return Spec->getSpecializedTemplate();
Anders Carlssoneafc6dc2009-09-18 19:44:50 +0000507 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000508
Anders Carlsson2744a062009-09-18 19:00:18 +0000509 return 0;
510}
511
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000512void CXXNameMangler::mangleName(const NamedDecl *ND) {
513 // <name> ::= <nested-name>
514 // ::= <unscoped-name>
515 // ::= <unscoped-template-name> <template-args>
Anders Carlsson201ce742009-09-17 03:17:01 +0000516 // ::= <local-name>
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000517 //
Anders Carlssond58d6f72009-09-17 16:12:20 +0000518 const DeclContext *DC = ND->getDeclContext();
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000519
Eli Friedman7facf842009-12-02 20:32:49 +0000520 // If this is an extern variable declared locally, the relevant DeclContext
521 // is that of the containing namespace, or the translation unit.
522 if (isa<FunctionDecl>(DC) && ND->hasLinkage())
523 while (!DC->isNamespace() && !DC->isTranslationUnit())
524 DC = DC->getParent();
John McCall82b7d7b2010-10-18 21:28:44 +0000525 else if (GetLocalClassDecl(ND)) {
526 mangleLocalName(ND);
527 return;
528 }
Eli Friedman7facf842009-12-02 20:32:49 +0000529
Anders Carlsson5cc58c62009-09-22 17:23:30 +0000530 while (isa<LinkageSpecDecl>(DC))
Anders Carlssond58d6f72009-09-17 16:12:20 +0000531 DC = DC->getParent();
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000532
Anders Carlssond58d6f72009-09-17 16:12:20 +0000533 if (DC->isTranslationUnit() || isStdNamespace(DC)) {
Anders Carlsson2744a062009-09-18 19:00:18 +0000534 // Check if we have a template.
535 const TemplateArgumentList *TemplateArgs = 0;
Anders Carlsson0fa6df42009-09-26 19:45:45 +0000536 if (const TemplateDecl *TD = isTemplate(ND, TemplateArgs)) {
Anders Carlsson2744a062009-09-18 19:00:18 +0000537 mangleUnscopedTemplateName(TD);
Rafael Espindolad9800722010-03-11 14:07:00 +0000538 TemplateParameterList *TemplateParameters = TD->getTemplateParameters();
539 mangleTemplateArgs(*TemplateParameters, *TemplateArgs);
Anders Carlsson2744a062009-09-18 19:00:18 +0000540 return;
Anders Carlsson7482e242009-09-18 04:29:09 +0000541 }
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000542
Anders Carlsson7482e242009-09-18 04:29:09 +0000543 mangleUnscopedName(ND);
544 return;
545 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000546
Anders Carlsson7b06f6c2009-12-10 03:14:39 +0000547 if (isa<FunctionDecl>(DC) || isa<ObjCMethodDecl>(DC)) {
Anders Carlsson7482e242009-09-18 04:29:09 +0000548 mangleLocalName(ND);
549 return;
550 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000551
Eli Friedman7facf842009-12-02 20:32:49 +0000552 mangleNestedName(ND, DC);
Anders Carlsson7482e242009-09-18 04:29:09 +0000553}
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000554void CXXNameMangler::mangleName(const TemplateDecl *TD,
Anders Carlsson7624f212009-09-18 02:42:01 +0000555 const TemplateArgument *TemplateArgs,
556 unsigned NumTemplateArgs) {
Anders Carlsson47846d22009-12-04 06:23:23 +0000557 const DeclContext *DC = IgnoreLinkageSpecDecls(TD->getDeclContext());
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000558
Anders Carlsson7624f212009-09-18 02:42:01 +0000559 if (DC->isTranslationUnit() || isStdNamespace(DC)) {
Anders Carlsson0fa6df42009-09-26 19:45:45 +0000560 mangleUnscopedTemplateName(TD);
Rafael Espindolad9800722010-03-11 14:07:00 +0000561 TemplateParameterList *TemplateParameters = TD->getTemplateParameters();
562 mangleTemplateArgs(*TemplateParameters, TemplateArgs, NumTemplateArgs);
Anders Carlsson7624f212009-09-18 02:42:01 +0000563 } else {
564 mangleNestedName(TD, TemplateArgs, NumTemplateArgs);
565 }
566}
567
Anders Carlsson201ce742009-09-17 03:17:01 +0000568void CXXNameMangler::mangleUnscopedName(const NamedDecl *ND) {
569 // <unscoped-name> ::= <unqualified-name>
570 // ::= St <unqualified-name> # ::std::
571 if (isStdNamespace(ND->getDeclContext()))
572 Out << "St";
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000573
Anders Carlsson201ce742009-09-17 03:17:01 +0000574 mangleUnqualifiedName(ND);
575}
576
Anders Carlsson0fa6df42009-09-26 19:45:45 +0000577void CXXNameMangler::mangleUnscopedTemplateName(const TemplateDecl *ND) {
Anders Carlsson201ce742009-09-17 03:17:01 +0000578 // <unscoped-template-name> ::= <unscoped-name>
579 // ::= <substitution>
Anders Carlsson7624f212009-09-18 02:42:01 +0000580 if (mangleSubstitution(ND))
Anders Carlsson03c9d532009-09-17 04:02:31 +0000581 return;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000582
Douglas Gregor32fb4e12010-02-05 20:45:00 +0000583 // <template-template-param> ::= <template-param>
584 if (const TemplateTemplateParmDecl *TTP
585 = dyn_cast<TemplateTemplateParmDecl>(ND)) {
586 mangleTemplateParameter(TTP->getIndex());
587 return;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000588 }
Douglas Gregor32fb4e12010-02-05 20:45:00 +0000589
Anders Carlsson1668f202009-09-26 20:13:56 +0000590 mangleUnscopedName(ND->getTemplatedDecl());
Anders Carlsson7624f212009-09-18 02:42:01 +0000591 addSubstitution(ND);
Anders Carlsson201ce742009-09-17 03:17:01 +0000592}
593
Douglas Gregor1e9268e2010-04-28 05:58:56 +0000594void CXXNameMangler::mangleUnscopedTemplateName(TemplateName Template) {
595 // <unscoped-template-name> ::= <unscoped-name>
596 // ::= <substitution>
597 if (TemplateDecl *TD = Template.getAsTemplateDecl())
598 return mangleUnscopedTemplateName(TD);
Sean Huntc3021132010-05-05 15:23:54 +0000599
Douglas Gregor1e9268e2010-04-28 05:58:56 +0000600 if (mangleSubstitution(Template))
601 return;
602
Douglas Gregor1e9268e2010-04-28 05:58:56 +0000603 DependentTemplateName *Dependent = Template.getAsDependentTemplateName();
604 assert(Dependent && "Not a dependent template name?");
Douglas Gregor19617912011-07-12 05:06:05 +0000605 if (const IdentifierInfo *Id = Dependent->getIdentifier())
606 mangleSourceName(Id);
607 else
608 mangleOperatorName(Dependent->getOperator(), UnknownArity);
Sean Huntc3021132010-05-05 15:23:54 +0000609
Douglas Gregor1e9268e2010-04-28 05:58:56 +0000610 addSubstitution(Template);
611}
612
John McCall1b600522011-04-24 03:07:16 +0000613void CXXNameMangler::mangleFloat(const llvm::APFloat &f) {
614 // ABI:
615 // Floating-point literals are encoded using a fixed-length
616 // lowercase hexadecimal string corresponding to the internal
617 // representation (IEEE on Itanium), high-order bytes first,
618 // without leading zeroes. For example: "Lf bf800000 E" is -1.0f
619 // on Itanium.
John McCall0c8731a2012-01-30 18:36:31 +0000620 // The 'without leading zeroes' thing seems to be an editorial
621 // mistake; see the discussion on cxx-abi-dev beginning on
622 // 2012-01-16.
John McCall1b600522011-04-24 03:07:16 +0000623
John McCall0c8731a2012-01-30 18:36:31 +0000624 // Our requirements here are just barely wierd enough to justify
625 // using a custom algorithm instead of post-processing APInt::toString().
John McCall1b600522011-04-24 03:07:16 +0000626
John McCall0c8731a2012-01-30 18:36:31 +0000627 llvm::APInt valueBits = f.bitcastToAPInt();
628 unsigned numCharacters = (valueBits.getBitWidth() + 3) / 4;
629 assert(numCharacters != 0);
630
631 // Allocate a buffer of the right number of characters.
632 llvm::SmallVector<char, 20> buffer;
633 buffer.set_size(numCharacters);
634
635 // Fill the buffer left-to-right.
636 for (unsigned stringIndex = 0; stringIndex != numCharacters; ++stringIndex) {
637 // The bit-index of the next hex digit.
638 unsigned digitBitIndex = 4 * (numCharacters - stringIndex - 1);
639
640 // Project out 4 bits starting at 'digitIndex'.
641 llvm::integerPart hexDigit
642 = valueBits.getRawData()[digitBitIndex / llvm::integerPartWidth];
643 hexDigit >>= (digitBitIndex % llvm::integerPartWidth);
644 hexDigit &= 0xF;
645
646 // Map that over to a lowercase hex digit.
647 static const char charForHex[16] = {
648 '0', '1', '2', '3', '4', '5', '6', '7',
649 '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'
650 };
651 buffer[stringIndex] = charForHex[hexDigit];
652 }
653
654 Out.write(buffer.data(), numCharacters);
John McCall0512e482010-07-14 04:20:34 +0000655}
656
657void CXXNameMangler::mangleNumber(const llvm::APSInt &Value) {
658 if (Value.isSigned() && Value.isNegative()) {
659 Out << 'n';
660 Value.abs().print(Out, true);
661 } else
662 Value.print(Out, Value.isSigned());
663}
664
Anders Carlssona94822e2009-11-26 02:32:05 +0000665void CXXNameMangler::mangleNumber(int64_t Number) {
666 // <number> ::= [n] <non-negative decimal integer>
667 if (Number < 0) {
668 Out << 'n';
669 Number = -Number;
670 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000671
Anders Carlssona94822e2009-11-26 02:32:05 +0000672 Out << Number;
673}
674
Anders Carlsson19879c92010-03-23 17:17:29 +0000675void CXXNameMangler::mangleCallOffset(int64_t NonVirtual, int64_t Virtual) {
Mike Stump141c5af2009-09-02 00:25:38 +0000676 // <call-offset> ::= h <nv-offset> _
677 // ::= v <v-offset> _
678 // <nv-offset> ::= <offset number> # non-virtual base override
Anders Carlssona94822e2009-11-26 02:32:05 +0000679 // <v-offset> ::= <offset number> _ <virtual offset number>
Mike Stump141c5af2009-09-02 00:25:38 +0000680 // # virtual base override, with vcall offset
Anders Carlsson19879c92010-03-23 17:17:29 +0000681 if (!Virtual) {
Anders Carlssona94822e2009-11-26 02:32:05 +0000682 Out << 'h';
Anders Carlsson19879c92010-03-23 17:17:29 +0000683 mangleNumber(NonVirtual);
Anders Carlssona94822e2009-11-26 02:32:05 +0000684 Out << '_';
685 return;
Mike Stump141c5af2009-09-02 00:25:38 +0000686 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000687
Anders Carlssona94822e2009-11-26 02:32:05 +0000688 Out << 'v';
Anders Carlsson19879c92010-03-23 17:17:29 +0000689 mangleNumber(NonVirtual);
Anders Carlssona94822e2009-11-26 02:32:05 +0000690 Out << '_';
Anders Carlsson19879c92010-03-23 17:17:29 +0000691 mangleNumber(Virtual);
Anders Carlssona94822e2009-11-26 02:32:05 +0000692 Out << '_';
Mike Stump9124bcc2009-09-02 00:56:18 +0000693}
694
John McCall4f4e4132011-05-04 01:45:19 +0000695void CXXNameMangler::manglePrefix(QualType type) {
John McCalla0ce15c2011-04-24 08:23:24 +0000696 if (const TemplateSpecializationType *TST =
697 type->getAs<TemplateSpecializationType>()) {
698 if (!mangleSubstitution(QualType(TST, 0))) {
699 mangleTemplatePrefix(TST->getTemplateName());
Sean Huntc3021132010-05-05 15:23:54 +0000700
Douglas Gregoraa2187d2011-02-28 00:04:36 +0000701 // FIXME: GCC does not appear to mangle the template arguments when
702 // the template in question is a dependent template name. Should we
703 // emulate that badness?
John McCalla0ce15c2011-04-24 08:23:24 +0000704 mangleTemplateArgs(TST->getTemplateName(), TST->getArgs(),
705 TST->getNumArgs());
706 addSubstitution(QualType(TST, 0));
Rafael Espindola9b35b252010-03-17 04:28:11 +0000707 }
John McCalla0ce15c2011-04-24 08:23:24 +0000708 } else if (const DependentTemplateSpecializationType *DTST
709 = type->getAs<DependentTemplateSpecializationType>()) {
710 TemplateName Template
711 = getASTContext().getDependentTemplateName(DTST->getQualifier(),
712 DTST->getIdentifier());
713 mangleTemplatePrefix(Template);
714
715 // FIXME: GCC does not appear to mangle the template arguments when
716 // the template in question is a dependent template name. Should we
717 // emulate that badness?
718 mangleTemplateArgs(Template, DTST->getArgs(), DTST->getNumArgs());
719 } else {
720 // We use the QualType mangle type variant here because it handles
721 // substitutions.
722 mangleType(type);
John McCall1dd73832010-02-04 01:42:13 +0000723 }
724}
725
John McCalla0ce15c2011-04-24 08:23:24 +0000726/// Mangle everything prior to the base-unresolved-name in an unresolved-name.
727///
728/// \param firstQualifierLookup - the entity found by unqualified lookup
729/// for the first name in the qualifier, if this is for a member expression
730/// \param recursive - true if this is being called recursively,
731/// i.e. if there is more prefix "to the right".
732void CXXNameMangler::mangleUnresolvedPrefix(NestedNameSpecifier *qualifier,
733 NamedDecl *firstQualifierLookup,
734 bool recursive) {
John McCall1dd73832010-02-04 01:42:13 +0000735
John McCalla0ce15c2011-04-24 08:23:24 +0000736 // x, ::x
737 // <unresolved-name> ::= [gs] <base-unresolved-name>
738
739 // T::x / decltype(p)::x
740 // <unresolved-name> ::= sr <unresolved-type> <base-unresolved-name>
741
742 // T::N::x /decltype(p)::N::x
743 // <unresolved-name> ::= srN <unresolved-type> <unresolved-qualifier-level>+ E
744 // <base-unresolved-name>
745
746 // A::x, N::y, A<T>::z; "gs" means leading "::"
747 // <unresolved-name> ::= [gs] sr <unresolved-qualifier-level>+ E
748 // <base-unresolved-name>
749
750 switch (qualifier->getKind()) {
751 case NestedNameSpecifier::Global:
752 Out << "gs";
753
754 // We want an 'sr' unless this is the entire NNS.
755 if (recursive)
756 Out << "sr";
757
758 // We never want an 'E' here.
759 return;
760
761 case NestedNameSpecifier::Namespace:
762 if (qualifier->getPrefix())
763 mangleUnresolvedPrefix(qualifier->getPrefix(), firstQualifierLookup,
764 /*recursive*/ true);
765 else
766 Out << "sr";
767 mangleSourceName(qualifier->getAsNamespace()->getIdentifier());
768 break;
769 case NestedNameSpecifier::NamespaceAlias:
770 if (qualifier->getPrefix())
771 mangleUnresolvedPrefix(qualifier->getPrefix(), firstQualifierLookup,
772 /*recursive*/ true);
773 else
774 Out << "sr";
775 mangleSourceName(qualifier->getAsNamespaceAlias()->getIdentifier());
776 break;
777
778 case NestedNameSpecifier::TypeSpec:
779 case NestedNameSpecifier::TypeSpecWithTemplate: {
John McCall4f4e4132011-05-04 01:45:19 +0000780 const Type *type = qualifier->getAsType();
John McCalla0ce15c2011-04-24 08:23:24 +0000781
John McCall4f4e4132011-05-04 01:45:19 +0000782 // We only want to use an unresolved-type encoding if this is one of:
783 // - a decltype
784 // - a template type parameter
785 // - a template template parameter with arguments
786 // In all of these cases, we should have no prefix.
787 if (qualifier->getPrefix()) {
788 mangleUnresolvedPrefix(qualifier->getPrefix(), firstQualifierLookup,
789 /*recursive*/ true);
790 } else {
791 // Otherwise, all the cases want this.
792 Out << "sr";
John McCall4f4e4132011-05-04 01:45:19 +0000793 }
794
John McCall4f4e4132011-05-04 01:45:19 +0000795 // Only certain other types are valid as prefixes; enumerate them.
John McCalld3d49bb2011-06-28 16:49:23 +0000796 switch (type->getTypeClass()) {
797 case Type::Builtin:
798 case Type::Complex:
799 case Type::Pointer:
800 case Type::BlockPointer:
801 case Type::LValueReference:
802 case Type::RValueReference:
803 case Type::MemberPointer:
804 case Type::ConstantArray:
805 case Type::IncompleteArray:
806 case Type::VariableArray:
807 case Type::DependentSizedArray:
808 case Type::DependentSizedExtVector:
809 case Type::Vector:
810 case Type::ExtVector:
811 case Type::FunctionProto:
812 case Type::FunctionNoProto:
813 case Type::Enum:
814 case Type::Paren:
815 case Type::Elaborated:
816 case Type::Attributed:
817 case Type::Auto:
818 case Type::PackExpansion:
John McCalld3d49bb2011-06-28 16:49:23 +0000819 case Type::ObjCObject:
820 case Type::ObjCInterface:
821 case Type::ObjCObjectPointer:
Eli Friedmanb001de72011-10-06 23:00:33 +0000822 case Type::Atomic:
John McCalld3d49bb2011-06-28 16:49:23 +0000823 llvm_unreachable("type is illegal as a nested name specifier");
824
John McCall68a51a72011-07-01 00:04:39 +0000825 case Type::SubstTemplateTypeParmPack:
826 // FIXME: not clear how to mangle this!
827 // template <class T...> class A {
828 // template <class U...> void foo(decltype(T::foo(U())) x...);
829 // };
830 Out << "_SUBSTPACK_";
831 break;
832
John McCalld3d49bb2011-06-28 16:49:23 +0000833 // <unresolved-type> ::= <template-param>
834 // ::= <decltype>
835 // ::= <template-template-param> <template-args>
836 // (this last is not official yet)
837 case Type::TypeOfExpr:
838 case Type::TypeOf:
839 case Type::Decltype:
840 case Type::TemplateTypeParm:
841 case Type::UnaryTransform:
John McCall35ee32e2011-07-01 02:19:08 +0000842 case Type::SubstTemplateTypeParm:
John McCalld3d49bb2011-06-28 16:49:23 +0000843 unresolvedType:
844 assert(!qualifier->getPrefix());
845
846 // We only get here recursively if we're followed by identifiers.
847 if (recursive) Out << 'N';
848
John McCall35ee32e2011-07-01 02:19:08 +0000849 // This seems to do everything we want. It's not really
850 // sanctioned for a substituted template parameter, though.
John McCalld3d49bb2011-06-28 16:49:23 +0000851 mangleType(QualType(type, 0));
852
853 // We never want to print 'E' directly after an unresolved-type,
854 // so we return directly.
855 return;
856
John McCalld3d49bb2011-06-28 16:49:23 +0000857 case Type::Typedef:
858 mangleSourceName(cast<TypedefType>(type)->getDecl()->getIdentifier());
859 break;
860
861 case Type::UnresolvedUsing:
862 mangleSourceName(cast<UnresolvedUsingType>(type)->getDecl()
863 ->getIdentifier());
864 break;
865
866 case Type::Record:
867 mangleSourceName(cast<RecordType>(type)->getDecl()->getIdentifier());
868 break;
869
870 case Type::TemplateSpecialization: {
871 const TemplateSpecializationType *tst
872 = cast<TemplateSpecializationType>(type);
John McCall68a51a72011-07-01 00:04:39 +0000873 TemplateName name = tst->getTemplateName();
874 switch (name.getKind()) {
875 case TemplateName::Template:
876 case TemplateName::QualifiedTemplate: {
877 TemplateDecl *temp = name.getAsTemplateDecl();
John McCalld3d49bb2011-06-28 16:49:23 +0000878
John McCall68a51a72011-07-01 00:04:39 +0000879 // If the base is a template template parameter, this is an
880 // unresolved type.
881 assert(temp && "no template for template specialization type");
882 if (isa<TemplateTemplateParmDecl>(temp)) goto unresolvedType;
John McCalld3d49bb2011-06-28 16:49:23 +0000883
John McCall68a51a72011-07-01 00:04:39 +0000884 mangleSourceName(temp->getIdentifier());
885 break;
886 }
887
888 case TemplateName::OverloadedTemplate:
889 case TemplateName::DependentTemplate:
890 llvm_unreachable("invalid base for a template specialization type");
891
892 case TemplateName::SubstTemplateTemplateParm: {
893 SubstTemplateTemplateParmStorage *subst
894 = name.getAsSubstTemplateTemplateParm();
895 mangleExistingSubstitution(subst->getReplacement());
896 break;
897 }
898
899 case TemplateName::SubstTemplateTemplateParmPack: {
900 // FIXME: not clear how to mangle this!
901 // template <template <class U> class T...> class A {
902 // template <class U...> void foo(decltype(T<U>::foo) x...);
903 // };
904 Out << "_SUBSTPACK_";
905 break;
906 }
907 }
908
John McCall4f4e4132011-05-04 01:45:19 +0000909 mangleUnresolvedTemplateArgs(tst->getArgs(), tst->getNumArgs());
John McCalld3d49bb2011-06-28 16:49:23 +0000910 break;
911 }
912
913 case Type::InjectedClassName:
914 mangleSourceName(cast<InjectedClassNameType>(type)->getDecl()
915 ->getIdentifier());
916 break;
917
918 case Type::DependentName:
919 mangleSourceName(cast<DependentNameType>(type)->getIdentifier());
920 break;
921
922 case Type::DependentTemplateSpecialization: {
923 const DependentTemplateSpecializationType *tst
924 = cast<DependentTemplateSpecializationType>(type);
John McCall4f4e4132011-05-04 01:45:19 +0000925 mangleSourceName(tst->getIdentifier());
926 mangleUnresolvedTemplateArgs(tst->getArgs(), tst->getNumArgs());
John McCalld3d49bb2011-06-28 16:49:23 +0000927 break;
928 }
John McCall4f4e4132011-05-04 01:45:19 +0000929 }
930 break;
John McCalla0ce15c2011-04-24 08:23:24 +0000931 }
932
933 case NestedNameSpecifier::Identifier:
934 // Member expressions can have these without prefixes.
935 if (qualifier->getPrefix()) {
936 mangleUnresolvedPrefix(qualifier->getPrefix(), firstQualifierLookup,
937 /*recursive*/ true);
938 } else if (firstQualifierLookup) {
939
940 // Try to make a proper qualifier out of the lookup result, and
941 // then just recurse on that.
942 NestedNameSpecifier *newQualifier;
943 if (TypeDecl *typeDecl = dyn_cast<TypeDecl>(firstQualifierLookup)) {
944 QualType type = getASTContext().getTypeDeclType(typeDecl);
945
946 // Pretend we had a different nested name specifier.
947 newQualifier = NestedNameSpecifier::Create(getASTContext(),
948 /*prefix*/ 0,
949 /*template*/ false,
950 type.getTypePtr());
951 } else if (NamespaceDecl *nspace =
952 dyn_cast<NamespaceDecl>(firstQualifierLookup)) {
953 newQualifier = NestedNameSpecifier::Create(getASTContext(),
954 /*prefix*/ 0,
955 nspace);
956 } else if (NamespaceAliasDecl *alias =
957 dyn_cast<NamespaceAliasDecl>(firstQualifierLookup)) {
958 newQualifier = NestedNameSpecifier::Create(getASTContext(),
959 /*prefix*/ 0,
960 alias);
961 } else {
962 // No sensible mangling to do here.
963 newQualifier = 0;
964 }
965
966 if (newQualifier)
967 return mangleUnresolvedPrefix(newQualifier, /*lookup*/ 0, recursive);
968
969 } else {
970 Out << "sr";
971 }
972
973 mangleSourceName(qualifier->getAsIdentifier());
974 break;
975 }
976
977 // If this was the innermost part of the NNS, and we fell out to
978 // here, append an 'E'.
979 if (!recursive)
980 Out << 'E';
981}
982
983/// Mangle an unresolved-name, which is generally used for names which
984/// weren't resolved to specific entities.
985void CXXNameMangler::mangleUnresolvedName(NestedNameSpecifier *qualifier,
986 NamedDecl *firstQualifierLookup,
987 DeclarationName name,
988 unsigned knownArity) {
989 if (qualifier) mangleUnresolvedPrefix(qualifier, firstQualifierLookup);
990 mangleUnqualifiedName(0, name, knownArity);
John McCall1dd73832010-02-04 01:42:13 +0000991}
992
Anders Carlsson6f7e2f42010-06-08 14:49:03 +0000993static const FieldDecl *FindFirstNamedDataMember(const RecordDecl *RD) {
994 assert(RD->isAnonymousStructOrUnion() &&
995 "Expected anonymous struct or union!");
996
997 for (RecordDecl::field_iterator I = RD->field_begin(), E = RD->field_end();
998 I != E; ++I) {
999 const FieldDecl *FD = *I;
1000
1001 if (FD->getIdentifier())
1002 return FD;
1003
1004 if (const RecordType *RT = FD->getType()->getAs<RecordType>()) {
1005 if (const FieldDecl *NamedDataMember =
1006 FindFirstNamedDataMember(RT->getDecl()))
1007 return NamedDataMember;
1008 }
1009 }
1010
1011 // We didn't find a named data member.
1012 return 0;
1013}
1014
John McCall1dd73832010-02-04 01:42:13 +00001015void CXXNameMangler::mangleUnqualifiedName(const NamedDecl *ND,
1016 DeclarationName Name,
1017 unsigned KnownArity) {
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001018 // <unqualified-name> ::= <operator-name>
Mike Stump1eb44332009-09-09 15:08:12 +00001019 // ::= <ctor-dtor-name>
1020 // ::= <source-name>
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001021 switch (Name.getNameKind()) {
Anders Carlssonc4355b62009-10-07 01:45:02 +00001022 case DeclarationName::Identifier: {
Anders Carlssonc4355b62009-10-07 01:45:02 +00001023 if (const IdentifierInfo *II = Name.getAsIdentifierInfo()) {
Sean Hunt31455252010-01-24 03:04:27 +00001024 // We must avoid conflicts between internally- and externally-
John McCall74990f42011-03-22 06:34:45 +00001025 // linked variable and function declaration names in the same TU:
1026 // void test() { extern void foo(); }
1027 // static void foo();
1028 // This naming convention is the same as that followed by GCC,
1029 // though it shouldn't actually matter.
1030 if (ND && ND->getLinkage() == InternalLinkage &&
Sean Hunt31455252010-01-24 03:04:27 +00001031 ND->getDeclContext()->isFileContext())
1032 Out << 'L';
1033
Anders Carlssonc4355b62009-10-07 01:45:02 +00001034 mangleSourceName(II);
1035 break;
1036 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00001037
John McCall1dd73832010-02-04 01:42:13 +00001038 // Otherwise, an anonymous entity. We must have a declaration.
1039 assert(ND && "mangling empty name without declaration");
1040
1041 if (const NamespaceDecl *NS = dyn_cast<NamespaceDecl>(ND)) {
1042 if (NS->isAnonymousNamespace()) {
1043 // This is how gcc mangles these names.
1044 Out << "12_GLOBAL__N_1";
1045 break;
1046 }
1047 }
1048
Anders Carlsson6f7e2f42010-06-08 14:49:03 +00001049 if (const VarDecl *VD = dyn_cast<VarDecl>(ND)) {
1050 // We must have an anonymous union or struct declaration.
1051 const RecordDecl *RD =
1052 cast<RecordDecl>(VD->getType()->getAs<RecordType>()->getDecl());
1053
1054 // Itanium C++ ABI 5.1.2:
1055 //
1056 // For the purposes of mangling, the name of an anonymous union is
1057 // considered to be the name of the first named data member found by a
1058 // pre-order, depth-first, declaration-order walk of the data members of
1059 // the anonymous union. If there is no such data member (i.e., if all of
1060 // the data members in the union are unnamed), then there is no way for
1061 // a program to refer to the anonymous union, and there is therefore no
1062 // need to mangle its name.
1063 const FieldDecl *FD = FindFirstNamedDataMember(RD);
John McCall7121c8f2010-08-05 22:02:13 +00001064
1065 // It's actually possible for various reasons for us to get here
1066 // with an empty anonymous struct / union. Fortunately, it
1067 // doesn't really matter what name we generate.
1068 if (!FD) break;
Anders Carlsson6f7e2f42010-06-08 14:49:03 +00001069 assert(FD->getIdentifier() && "Data member name isn't an identifier!");
1070
1071 mangleSourceName(FD->getIdentifier());
1072 break;
1073 }
1074
Anders Carlssonc4355b62009-10-07 01:45:02 +00001075 // We must have an anonymous struct.
1076 const TagDecl *TD = cast<TagDecl>(ND);
Richard Smith162e1c12011-04-15 14:24:37 +00001077 if (const TypedefNameDecl *D = TD->getTypedefNameForAnonDecl()) {
Anders Carlssonc4355b62009-10-07 01:45:02 +00001078 assert(TD->getDeclContext() == D->getDeclContext() &&
1079 "Typedef should not be in another decl context!");
1080 assert(D->getDeclName().getAsIdentifierInfo() &&
1081 "Typedef was not named!");
1082 mangleSourceName(D->getDeclName().getAsIdentifierInfo());
1083 break;
1084 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00001085
Douglas Gregor9e8c92a2012-02-20 19:44:39 +00001086 // <unnamed-type-name> ::= <closure-type-name>
1087 //
1088 // <closure-type-name> ::= Ul <lambda-sig> E [ <nonnegative number> ] _
1089 // <lambda-sig> ::= <parameter-type>+ # Parameter types or 'v' for 'void'.
1090 if (const CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(TD)) {
1091 if (Record->isLambda()) {
1092 // FIXME: Figure out if we're in a function body, default argument,
1093 // or initializer for a class member.
1094
1095 Out << "Ul";
1096 DeclarationName Name
1097 = getASTContext().DeclarationNames.getCXXOperatorName(OO_Call);
1098 const FunctionProtoType *Proto
1099 = cast<CXXMethodDecl>(*Record->lookup(Name).first)->getType()->
1100 getAs<FunctionProtoType>();
1101 mangleBareFunctionType(Proto, /*MangleReturnType=*/false);
1102 Out << "E";
1103
1104 // The number is omitted for the first closure type with a given
1105 // <lambda-sig> in a given context; it is n-2 for the nth closure type
1106 // (in lexical order) with that same <lambda-sig> and context.
1107 //
1108 // The AST keeps track of the number for us.
1109 if (unsigned Number = Record->getLambdaManglingNumber()) {
1110 if (Number > 1)
1111 mangleNumber(Number - 2);
1112 }
1113 Out << '_';
1114 break;
1115 }
1116 }
1117
Anders Carlssonc4355b62009-10-07 01:45:02 +00001118 // Get a unique id for the anonymous struct.
1119 uint64_t AnonStructId = Context.getAnonymousStructId(TD);
1120
1121 // Mangle it as a source name in the form
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00001122 // [n] $_<id>
Anders Carlssonc4355b62009-10-07 01:45:02 +00001123 // where n is the length of the string.
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00001124 SmallString<8> Str;
Anders Carlssonc4355b62009-10-07 01:45:02 +00001125 Str += "$_";
1126 Str += llvm::utostr(AnonStructId);
1127
1128 Out << Str.size();
1129 Out << Str.str();
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001130 break;
Anders Carlssonc4355b62009-10-07 01:45:02 +00001131 }
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001132
1133 case DeclarationName::ObjCZeroArgSelector:
1134 case DeclarationName::ObjCOneArgSelector:
1135 case DeclarationName::ObjCMultiArgSelector:
David Blaikieb219cfc2011-09-23 05:06:16 +00001136 llvm_unreachable("Can't mangle Objective-C selector names here!");
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001137
1138 case DeclarationName::CXXConstructorName:
Anders Carlsson27ae5362009-04-17 01:58:57 +00001139 if (ND == Structor)
Mike Stump141c5af2009-09-02 00:25:38 +00001140 // If the named decl is the C++ constructor we're mangling, use the type
1141 // we were given.
Anders Carlsson27ae5362009-04-17 01:58:57 +00001142 mangleCXXCtorType(static_cast<CXXCtorType>(StructorType));
Anders Carlsson3ac86b52009-04-15 05:36:58 +00001143 else
1144 // Otherwise, use the complete constructor name. This is relevant if a
1145 // class with a constructor is declared within a constructor.
1146 mangleCXXCtorType(Ctor_Complete);
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001147 break;
1148
1149 case DeclarationName::CXXDestructorName:
Anders Carlsson27ae5362009-04-17 01:58:57 +00001150 if (ND == Structor)
Mike Stump141c5af2009-09-02 00:25:38 +00001151 // If the named decl is the C++ destructor we're mangling, use the type we
1152 // were given.
Anders Carlsson27ae5362009-04-17 01:58:57 +00001153 mangleCXXDtorType(static_cast<CXXDtorType>(StructorType));
1154 else
1155 // Otherwise, use the complete destructor name. This is relevant if a
1156 // class with a destructor is declared within a destructor.
1157 mangleCXXDtorType(Dtor_Complete);
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001158 break;
1159
1160 case DeclarationName::CXXConversionFunctionName:
Mike Stump1eb44332009-09-09 15:08:12 +00001161 // <operator-name> ::= cv <type> # (cast)
Douglas Gregor219cc612009-02-13 01:28:03 +00001162 Out << "cv";
Douglas Gregor79e6bd32011-07-12 04:42:08 +00001163 mangleType(Name.getCXXNameType());
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001164 break;
1165
Anders Carlsson8257d412009-12-22 06:36:32 +00001166 case DeclarationName::CXXOperatorName: {
John McCall1dd73832010-02-04 01:42:13 +00001167 unsigned Arity;
1168 if (ND) {
1169 Arity = cast<FunctionDecl>(ND)->getNumParams();
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001170
John McCall1dd73832010-02-04 01:42:13 +00001171 // If we have a C++ member function, we need to include the 'this' pointer.
1172 // FIXME: This does not make sense for operators that are static, but their
1173 // names stay the same regardless of the arity (operator new for instance).
1174 if (isa<CXXMethodDecl>(ND))
1175 Arity++;
1176 } else
1177 Arity = KnownArity;
1178
Anders Carlsson8257d412009-12-22 06:36:32 +00001179 mangleOperatorName(Name.getCXXOverloadedOperator(), Arity);
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001180 break;
Anders Carlsson8257d412009-12-22 06:36:32 +00001181 }
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001182
Sean Hunt3e518bd2009-11-29 07:34:05 +00001183 case DeclarationName::CXXLiteralOperatorName:
Sean Hunt5dd6b392009-12-04 21:11:13 +00001184 // FIXME: This mangling is not yet official.
Sean Hunt2421f662009-12-04 21:01:37 +00001185 Out << "li";
Sean Hunt3e518bd2009-11-29 07:34:05 +00001186 mangleSourceName(Name.getCXXLiteralIdentifier());
1187 break;
1188
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001189 case DeclarationName::CXXUsingDirective:
David Blaikieb219cfc2011-09-23 05:06:16 +00001190 llvm_unreachable("Can't mangle a using directive name!");
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001191 }
1192}
1193
1194void CXXNameMangler::mangleSourceName(const IdentifierInfo *II) {
1195 // <source-name> ::= <positive length number> <identifier>
1196 // <number> ::= [n] <non-negative decimal integer>
1197 // <identifier> ::= <unqualified source code identifier>
1198 Out << II->getLength() << II->getName();
1199}
1200
Eli Friedman7facf842009-12-02 20:32:49 +00001201void CXXNameMangler::mangleNestedName(const NamedDecl *ND,
Fariborz Jahanian57058532010-03-03 19:41:08 +00001202 const DeclContext *DC,
1203 bool NoFunction) {
Douglas Gregor0a9a6d62011-01-26 17:36:28 +00001204 // <nested-name>
1205 // ::= N [<CV-qualifiers>] [<ref-qualifier>] <prefix> <unqualified-name> E
1206 // ::= N [<CV-qualifiers>] [<ref-qualifier>] <template-prefix>
1207 // <template-args> E
Anders Carlssond99edc42009-09-26 03:55:37 +00001208
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001209 Out << 'N';
Douglas Gregor0a9a6d62011-01-26 17:36:28 +00001210 if (const CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(ND)) {
John McCall0953e762009-09-24 19:53:00 +00001211 mangleQualifiers(Qualifiers::fromCVRMask(Method->getTypeQualifiers()));
Douglas Gregor0a9a6d62011-01-26 17:36:28 +00001212 mangleRefQualifier(Method->getRefQualifier());
1213 }
1214
Anders Carlsson2744a062009-09-18 19:00:18 +00001215 // Check if we have a template.
1216 const TemplateArgumentList *TemplateArgs = 0;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00001217 if (const TemplateDecl *TD = isTemplate(ND, TemplateArgs)) {
Anders Carlsson2744a062009-09-18 19:00:18 +00001218 mangleTemplatePrefix(TD);
Rafael Espindolad9800722010-03-11 14:07:00 +00001219 TemplateParameterList *TemplateParameters = TD->getTemplateParameters();
1220 mangleTemplateArgs(*TemplateParameters, *TemplateArgs);
Fariborz Jahanian57058532010-03-03 19:41:08 +00001221 }
1222 else {
1223 manglePrefix(DC, NoFunction);
Anders Carlsson7482e242009-09-18 04:29:09 +00001224 mangleUnqualifiedName(ND);
1225 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00001226
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001227 Out << 'E';
1228}
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00001229void CXXNameMangler::mangleNestedName(const TemplateDecl *TD,
Anders Carlsson7624f212009-09-18 02:42:01 +00001230 const TemplateArgument *TemplateArgs,
1231 unsigned NumTemplateArgs) {
Anders Carlssone45117b2009-09-27 19:53:49 +00001232 // <nested-name> ::= N [<CV-qualifiers>] <template-prefix> <template-args> E
1233
Anders Carlsson7624f212009-09-18 02:42:01 +00001234 Out << 'N';
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00001235
Anders Carlssone45117b2009-09-27 19:53:49 +00001236 mangleTemplatePrefix(TD);
Rafael Espindolad9800722010-03-11 14:07:00 +00001237 TemplateParameterList *TemplateParameters = TD->getTemplateParameters();
1238 mangleTemplateArgs(*TemplateParameters, TemplateArgs, NumTemplateArgs);
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00001239
Anders Carlsson7624f212009-09-18 02:42:01 +00001240 Out << 'E';
1241}
1242
Anders Carlsson1b42c792009-04-02 16:24:45 +00001243void CXXNameMangler::mangleLocalName(const NamedDecl *ND) {
1244 // <local-name> := Z <function encoding> E <entity name> [<discriminator>]
1245 // := Z <function encoding> E s [<discriminator>]
Mike Stump1eb44332009-09-09 15:08:12 +00001246 // <discriminator> := _ <non-negative number>
Fariborz Jahanian57058532010-03-03 19:41:08 +00001247 const DeclContext *DC = ND->getDeclContext();
Fariborz Jahanian8805fe82011-06-09 19:25:01 +00001248 if (isa<ObjCMethodDecl>(DC) && isa<FunctionDecl>(ND)) {
1249 // Don't add objc method name mangling to locally declared function
1250 mangleUnqualifiedName(ND);
1251 return;
1252 }
1253
Anders Carlsson1b42c792009-04-02 16:24:45 +00001254 Out << 'Z';
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001255
Charles Davis685b1d92010-05-26 18:25:27 +00001256 if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(DC)) {
1257 mangleObjCMethodName(MD);
John McCall82b7d7b2010-10-18 21:28:44 +00001258 } else if (const CXXRecordDecl *RD = GetLocalClassDecl(ND)) {
1259 mangleFunctionEncoding(cast<FunctionDecl>(RD->getDeclContext()));
Fariborz Jahanian57058532010-03-03 19:41:08 +00001260 Out << 'E';
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001261
John McCall82b7d7b2010-10-18 21:28:44 +00001262 // Mangle the name relative to the closest enclosing function.
1263 if (ND == RD) // equality ok because RD derived from ND above
1264 mangleUnqualifiedName(ND);
1265 else
1266 mangleNestedName(ND, DC, true /*NoFunction*/);
1267
Fariborz Jahanian4819ac42010-03-04 01:02:03 +00001268 unsigned disc;
John McCall82b7d7b2010-10-18 21:28:44 +00001269 if (Context.getNextDiscriminator(RD, disc)) {
Fariborz Jahanian4819ac42010-03-04 01:02:03 +00001270 if (disc < 10)
1271 Out << '_' << disc;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001272 else
Fariborz Jahanian4819ac42010-03-04 01:02:03 +00001273 Out << "__" << disc << '_';
1274 }
Fariborz Jahanian57058532010-03-03 19:41:08 +00001275
1276 return;
1277 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001278 else
Fariborz Jahanian57058532010-03-03 19:41:08 +00001279 mangleFunctionEncoding(cast<FunctionDecl>(DC));
Anders Carlsson7b06f6c2009-12-10 03:14:39 +00001280
Anders Carlsson1b42c792009-04-02 16:24:45 +00001281 Out << 'E';
Eli Friedman6f9f25d2009-12-11 20:21:38 +00001282 mangleUnqualifiedName(ND);
Anders Carlsson1b42c792009-04-02 16:24:45 +00001283}
1284
John McCalla0ce15c2011-04-24 08:23:24 +00001285void CXXNameMangler::manglePrefix(NestedNameSpecifier *qualifier) {
1286 switch (qualifier->getKind()) {
1287 case NestedNameSpecifier::Global:
1288 // nothing
1289 return;
1290
1291 case NestedNameSpecifier::Namespace:
1292 mangleName(qualifier->getAsNamespace());
1293 return;
1294
1295 case NestedNameSpecifier::NamespaceAlias:
1296 mangleName(qualifier->getAsNamespaceAlias()->getNamespace());
1297 return;
1298
1299 case NestedNameSpecifier::TypeSpec:
1300 case NestedNameSpecifier::TypeSpecWithTemplate:
John McCall4f4e4132011-05-04 01:45:19 +00001301 manglePrefix(QualType(qualifier->getAsType(), 0));
John McCalla0ce15c2011-04-24 08:23:24 +00001302 return;
1303
1304 case NestedNameSpecifier::Identifier:
1305 // Member expressions can have these without prefixes, but that
1306 // should end up in mangleUnresolvedPrefix instead.
1307 assert(qualifier->getPrefix());
1308 manglePrefix(qualifier->getPrefix());
1309
1310 mangleSourceName(qualifier->getAsIdentifier());
1311 return;
1312 }
1313
1314 llvm_unreachable("unexpected nested name specifier");
1315}
1316
Fariborz Jahanian57058532010-03-03 19:41:08 +00001317void CXXNameMangler::manglePrefix(const DeclContext *DC, bool NoFunction) {
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001318 // <prefix> ::= <prefix> <unqualified-name>
1319 // ::= <template-prefix> <template-args>
1320 // ::= <template-param>
1321 // ::= # empty
1322 // ::= <substitution>
Anders Carlsson6862fc72009-09-17 04:16:28 +00001323
Anders Carlssonadd28822009-09-22 20:33:31 +00001324 while (isa<LinkageSpecDecl>(DC))
1325 DC = DC->getParent();
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00001326
Anders Carlsson9263e912009-09-18 18:39:58 +00001327 if (DC->isTranslationUnit())
1328 return;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00001329
Douglas Gregor35415f52010-05-25 17:04:15 +00001330 if (const BlockDecl *Block = dyn_cast<BlockDecl>(DC)) {
1331 manglePrefix(DC->getParent(), NoFunction);
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00001332 SmallString<64> Name;
Rafael Espindolac4850c22011-02-10 23:59:36 +00001333 llvm::raw_svector_ostream NameStream(Name);
1334 Context.mangleBlock(Block, NameStream);
1335 NameStream.flush();
Douglas Gregor35415f52010-05-25 17:04:15 +00001336 Out << Name.size() << Name;
1337 return;
1338 }
1339
Anders Carlsson6862fc72009-09-17 04:16:28 +00001340 if (mangleSubstitution(cast<NamedDecl>(DC)))
1341 return;
Anders Carlsson7482e242009-09-18 04:29:09 +00001342
Anders Carlsson2ee3fca2009-09-18 20:11:09 +00001343 // Check if we have a template.
1344 const TemplateArgumentList *TemplateArgs = 0;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00001345 if (const TemplateDecl *TD = isTemplate(cast<NamedDecl>(DC), TemplateArgs)) {
Anders Carlsson2ee3fca2009-09-18 20:11:09 +00001346 mangleTemplatePrefix(TD);
Rafael Espindolad9800722010-03-11 14:07:00 +00001347 TemplateParameterList *TemplateParameters = TD->getTemplateParameters();
1348 mangleTemplateArgs(*TemplateParameters, *TemplateArgs);
Fariborz Jahanian57058532010-03-03 19:41:08 +00001349 }
Douglas Gregor35415f52010-05-25 17:04:15 +00001350 else if(NoFunction && (isa<FunctionDecl>(DC) || isa<ObjCMethodDecl>(DC)))
Fariborz Jahanian57058532010-03-03 19:41:08 +00001351 return;
Douglas Gregor35415f52010-05-25 17:04:15 +00001352 else if (const ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(DC))
1353 mangleObjCMethodName(Method);
Fariborz Jahanian57058532010-03-03 19:41:08 +00001354 else {
1355 manglePrefix(DC->getParent(), NoFunction);
Anders Carlsson2ee3fca2009-09-18 20:11:09 +00001356 mangleUnqualifiedName(cast<NamedDecl>(DC));
1357 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00001358
Anders Carlsson6862fc72009-09-17 04:16:28 +00001359 addSubstitution(cast<NamedDecl>(DC));
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001360}
1361
Douglas Gregor20f0cc72010-04-23 03:10:43 +00001362void CXXNameMangler::mangleTemplatePrefix(TemplateName Template) {
1363 // <template-prefix> ::= <prefix> <template unqualified-name>
1364 // ::= <template-param>
1365 // ::= <substitution>
1366 if (TemplateDecl *TD = Template.getAsTemplateDecl())
1367 return mangleTemplatePrefix(TD);
1368
1369 if (QualifiedTemplateName *Qualified = Template.getAsQualifiedTemplateName())
John McCalla0ce15c2011-04-24 08:23:24 +00001370 manglePrefix(Qualified->getQualifier());
Sean Huntc3021132010-05-05 15:23:54 +00001371
Douglas Gregor20f0cc72010-04-23 03:10:43 +00001372 if (OverloadedTemplateStorage *Overloaded
1373 = Template.getAsOverloadedTemplate()) {
Sean Huntc3021132010-05-05 15:23:54 +00001374 mangleUnqualifiedName(0, (*Overloaded->begin())->getDeclName(),
Douglas Gregor20f0cc72010-04-23 03:10:43 +00001375 UnknownArity);
1376 return;
1377 }
Sean Huntc3021132010-05-05 15:23:54 +00001378
Douglas Gregor20f0cc72010-04-23 03:10:43 +00001379 DependentTemplateName *Dependent = Template.getAsDependentTemplateName();
1380 assert(Dependent && "Unknown template name kind?");
John McCalla0ce15c2011-04-24 08:23:24 +00001381 manglePrefix(Dependent->getQualifier());
Douglas Gregor1e9268e2010-04-28 05:58:56 +00001382 mangleUnscopedTemplateName(Template);
Douglas Gregor20f0cc72010-04-23 03:10:43 +00001383}
1384
Anders Carlsson0fa6df42009-09-26 19:45:45 +00001385void CXXNameMangler::mangleTemplatePrefix(const TemplateDecl *ND) {
Anders Carlsson7482e242009-09-18 04:29:09 +00001386 // <template-prefix> ::= <prefix> <template unqualified-name>
1387 // ::= <template-param>
1388 // ::= <substitution>
Douglas Gregor32fb4e12010-02-05 20:45:00 +00001389 // <template-template-param> ::= <template-param>
1390 // <substitution>
Anders Carlsson7482e242009-09-18 04:29:09 +00001391
Anders Carlssonaeb85372009-09-26 22:18:22 +00001392 if (mangleSubstitution(ND))
1393 return;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00001394
Douglas Gregor32fb4e12010-02-05 20:45:00 +00001395 // <template-template-param> ::= <template-param>
1396 if (const TemplateTemplateParmDecl *TTP
1397 = dyn_cast<TemplateTemplateParmDecl>(ND)) {
1398 mangleTemplateParameter(TTP->getIndex());
1399 return;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001400 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00001401
Anders Carlssonaa73ab12009-09-18 18:47:07 +00001402 manglePrefix(ND->getDeclContext());
Anders Carlsson1668f202009-09-26 20:13:56 +00001403 mangleUnqualifiedName(ND->getTemplatedDecl());
Anders Carlssonaeb85372009-09-26 22:18:22 +00001404 addSubstitution(ND);
Anders Carlsson7482e242009-09-18 04:29:09 +00001405}
1406
John McCallb6f532e2010-07-14 06:43:17 +00001407/// Mangles a template name under the production <type>. Required for
1408/// template template arguments.
1409/// <type> ::= <class-enum-type>
1410/// ::= <template-param>
1411/// ::= <substitution>
1412void CXXNameMangler::mangleType(TemplateName TN) {
1413 if (mangleSubstitution(TN))
1414 return;
1415
1416 TemplateDecl *TD = 0;
1417
1418 switch (TN.getKind()) {
1419 case TemplateName::QualifiedTemplate:
1420 TD = TN.getAsQualifiedTemplateName()->getTemplateDecl();
1421 goto HaveDecl;
1422
1423 case TemplateName::Template:
1424 TD = TN.getAsTemplateDecl();
1425 goto HaveDecl;
1426
1427 HaveDecl:
1428 if (isa<TemplateTemplateParmDecl>(TD))
1429 mangleTemplateParameter(cast<TemplateTemplateParmDecl>(TD)->getIndex());
1430 else
1431 mangleName(TD);
1432 break;
1433
1434 case TemplateName::OverloadedTemplate:
1435 llvm_unreachable("can't mangle an overloaded template name as a <type>");
John McCallb6f532e2010-07-14 06:43:17 +00001436
1437 case TemplateName::DependentTemplate: {
1438 const DependentTemplateName *Dependent = TN.getAsDependentTemplateName();
1439 assert(Dependent->isIdentifier());
1440
1441 // <class-enum-type> ::= <name>
1442 // <name> ::= <nested-name>
John McCalla0ce15c2011-04-24 08:23:24 +00001443 mangleUnresolvedPrefix(Dependent->getQualifier(), 0);
John McCallb6f532e2010-07-14 06:43:17 +00001444 mangleSourceName(Dependent->getIdentifier());
1445 break;
1446 }
1447
John McCallb44e0cf2011-06-30 21:59:02 +00001448 case TemplateName::SubstTemplateTemplateParm: {
1449 // Substituted template parameters are mangled as the substituted
1450 // template. This will check for the substitution twice, which is
1451 // fine, but we have to return early so that we don't try to *add*
1452 // the substitution twice.
1453 SubstTemplateTemplateParmStorage *subst
1454 = TN.getAsSubstTemplateTemplateParm();
1455 mangleType(subst->getReplacement());
1456 return;
1457 }
John McCall14606042011-06-30 08:33:18 +00001458
Douglas Gregor1aee05d2011-01-15 06:45:20 +00001459 case TemplateName::SubstTemplateTemplateParmPack: {
John McCall68a51a72011-07-01 00:04:39 +00001460 // FIXME: not clear how to mangle this!
1461 // template <template <class> class T...> class A {
1462 // template <template <class> class U...> void foo(B<T,U> x...);
1463 // };
1464 Out << "_SUBSTPACK_";
Douglas Gregor1aee05d2011-01-15 06:45:20 +00001465 break;
1466 }
John McCallb6f532e2010-07-14 06:43:17 +00001467 }
1468
1469 addSubstitution(TN);
1470}
1471
Mike Stump1eb44332009-09-09 15:08:12 +00001472void
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001473CXXNameMangler::mangleOperatorName(OverloadedOperatorKind OO, unsigned Arity) {
1474 switch (OO) {
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001475 // <operator-name> ::= nw # new
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001476 case OO_New: Out << "nw"; break;
1477 // ::= na # new[]
1478 case OO_Array_New: Out << "na"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001479 // ::= dl # delete
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001480 case OO_Delete: Out << "dl"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001481 // ::= da # delete[]
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001482 case OO_Array_Delete: Out << "da"; break;
1483 // ::= ps # + (unary)
John McCall5e1e89b2010-08-18 19:18:59 +00001484 // ::= pl # + (binary or unknown)
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001485 case OO_Plus:
Anders Carlsson8257d412009-12-22 06:36:32 +00001486 Out << (Arity == 1? "ps" : "pl"); break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001487 // ::= ng # - (unary)
John McCall5e1e89b2010-08-18 19:18:59 +00001488 // ::= mi # - (binary or unknown)
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001489 case OO_Minus:
Anders Carlsson8257d412009-12-22 06:36:32 +00001490 Out << (Arity == 1? "ng" : "mi"); break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001491 // ::= ad # & (unary)
John McCall5e1e89b2010-08-18 19:18:59 +00001492 // ::= an # & (binary or unknown)
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001493 case OO_Amp:
Anders Carlsson8257d412009-12-22 06:36:32 +00001494 Out << (Arity == 1? "ad" : "an"); break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001495 // ::= de # * (unary)
John McCall5e1e89b2010-08-18 19:18:59 +00001496 // ::= ml # * (binary or unknown)
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001497 case OO_Star:
John McCall5e1e89b2010-08-18 19:18:59 +00001498 // Use binary when unknown.
Anders Carlsson8257d412009-12-22 06:36:32 +00001499 Out << (Arity == 1? "de" : "ml"); break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001500 // ::= co # ~
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001501 case OO_Tilde: Out << "co"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001502 // ::= dv # /
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001503 case OO_Slash: Out << "dv"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001504 // ::= rm # %
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001505 case OO_Percent: Out << "rm"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001506 // ::= or # |
1507 case OO_Pipe: Out << "or"; break;
1508 // ::= eo # ^
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001509 case OO_Caret: Out << "eo"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001510 // ::= aS # =
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001511 case OO_Equal: Out << "aS"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001512 // ::= pL # +=
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001513 case OO_PlusEqual: Out << "pL"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001514 // ::= mI # -=
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001515 case OO_MinusEqual: Out << "mI"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001516 // ::= mL # *=
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001517 case OO_StarEqual: Out << "mL"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001518 // ::= dV # /=
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001519 case OO_SlashEqual: Out << "dV"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001520 // ::= rM # %=
1521 case OO_PercentEqual: Out << "rM"; break;
1522 // ::= aN # &=
1523 case OO_AmpEqual: Out << "aN"; break;
1524 // ::= oR # |=
1525 case OO_PipeEqual: Out << "oR"; break;
1526 // ::= eO # ^=
1527 case OO_CaretEqual: Out << "eO"; break;
1528 // ::= ls # <<
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001529 case OO_LessLess: Out << "ls"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001530 // ::= rs # >>
1531 case OO_GreaterGreater: Out << "rs"; break;
1532 // ::= lS # <<=
1533 case OO_LessLessEqual: Out << "lS"; break;
1534 // ::= rS # >>=
1535 case OO_GreaterGreaterEqual: Out << "rS"; break;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001536 // ::= eq # ==
1537 case OO_EqualEqual: Out << "eq"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001538 // ::= ne # !=
1539 case OO_ExclaimEqual: Out << "ne"; break;
1540 // ::= lt # <
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001541 case OO_Less: Out << "lt"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001542 // ::= gt # >
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001543 case OO_Greater: Out << "gt"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001544 // ::= le # <=
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001545 case OO_LessEqual: Out << "le"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001546 // ::= ge # >=
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001547 case OO_GreaterEqual: Out << "ge"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001548 // ::= nt # !
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001549 case OO_Exclaim: Out << "nt"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001550 // ::= aa # &&
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001551 case OO_AmpAmp: Out << "aa"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001552 // ::= oo # ||
1553 case OO_PipePipe: Out << "oo"; break;
1554 // ::= pp # ++
1555 case OO_PlusPlus: Out << "pp"; break;
1556 // ::= mm # --
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001557 case OO_MinusMinus: Out << "mm"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001558 // ::= cm # ,
1559 case OO_Comma: Out << "cm"; break;
1560 // ::= pm # ->*
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001561 case OO_ArrowStar: Out << "pm"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001562 // ::= pt # ->
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001563 case OO_Arrow: Out << "pt"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001564 // ::= cl # ()
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001565 case OO_Call: Out << "cl"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001566 // ::= ix # []
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001567 case OO_Subscript: Out << "ix"; break;
Anders Carlssone170ba72009-12-14 01:45:37 +00001568
1569 // ::= qu # ?
1570 // The conditional operator can't be overloaded, but we still handle it when
1571 // mangling expressions.
1572 case OO_Conditional: Out << "qu"; break;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001573
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001574 case OO_None:
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001575 case NUM_OVERLOADED_OPERATORS:
David Blaikieb219cfc2011-09-23 05:06:16 +00001576 llvm_unreachable("Not an overloaded operator");
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001577 }
1578}
1579
John McCall0953e762009-09-24 19:53:00 +00001580void CXXNameMangler::mangleQualifiers(Qualifiers Quals) {
Mike Stump1eb44332009-09-09 15:08:12 +00001581 // <CV-qualifiers> ::= [r] [V] [K] # restrict (C99), volatile, const
John McCall0953e762009-09-24 19:53:00 +00001582 if (Quals.hasRestrict())
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001583 Out << 'r';
John McCall0953e762009-09-24 19:53:00 +00001584 if (Quals.hasVolatile())
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001585 Out << 'V';
John McCall0953e762009-09-24 19:53:00 +00001586 if (Quals.hasConst())
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001587 Out << 'K';
John McCall0953e762009-09-24 19:53:00 +00001588
Douglas Gregor56079f72010-06-14 23:15:08 +00001589 if (Quals.hasAddressSpace()) {
1590 // Extension:
1591 //
1592 // <type> ::= U <address-space-number>
1593 //
1594 // where <address-space-number> is a source name consisting of 'AS'
1595 // followed by the address space <number>.
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00001596 SmallString<64> ASString;
Douglas Gregor56079f72010-06-14 23:15:08 +00001597 ASString = "AS" + llvm::utostr_32(Quals.getAddressSpace());
1598 Out << 'U' << ASString.size() << ASString;
1599 }
1600
Chris Lattner5f9e2722011-07-23 10:55:15 +00001601 StringRef LifetimeName;
John McCallf85e1932011-06-15 23:02:42 +00001602 switch (Quals.getObjCLifetime()) {
1603 // Objective-C ARC Extension:
1604 //
1605 // <type> ::= U "__strong"
1606 // <type> ::= U "__weak"
1607 // <type> ::= U "__autoreleasing"
John McCallf85e1932011-06-15 23:02:42 +00001608 case Qualifiers::OCL_None:
1609 break;
1610
1611 case Qualifiers::OCL_Weak:
1612 LifetimeName = "__weak";
1613 break;
1614
1615 case Qualifiers::OCL_Strong:
1616 LifetimeName = "__strong";
1617 break;
1618
1619 case Qualifiers::OCL_Autoreleasing:
1620 LifetimeName = "__autoreleasing";
1621 break;
1622
1623 case Qualifiers::OCL_ExplicitNone:
Douglas Gregorc22d6992011-06-17 22:26:49 +00001624 // The __unsafe_unretained qualifier is *not* mangled, so that
1625 // __unsafe_unretained types in ARC produce the same manglings as the
1626 // equivalent (but, naturally, unqualified) types in non-ARC, providing
1627 // better ABI compatibility.
1628 //
1629 // It's safe to do this because unqualified 'id' won't show up
1630 // in any type signatures that need to be mangled.
John McCallf85e1932011-06-15 23:02:42 +00001631 break;
1632 }
1633 if (!LifetimeName.empty())
1634 Out << 'U' << LifetimeName.size() << LifetimeName;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001635}
1636
Douglas Gregor0a9a6d62011-01-26 17:36:28 +00001637void CXXNameMangler::mangleRefQualifier(RefQualifierKind RefQualifier) {
1638 // <ref-qualifier> ::= R # lvalue reference
1639 // ::= O # rvalue-reference
1640 // Proposal to Itanium C++ ABI list on 1/26/11
1641 switch (RefQualifier) {
1642 case RQ_None:
1643 break;
1644
1645 case RQ_LValue:
1646 Out << 'R';
1647 break;
1648
1649 case RQ_RValue:
1650 Out << 'O';
1651 break;
1652 }
1653}
1654
Anders Carlsson7b06f6c2009-12-10 03:14:39 +00001655void CXXNameMangler::mangleObjCMethodName(const ObjCMethodDecl *MD) {
Rafael Espindolaf0be9792011-02-11 02:52:17 +00001656 Context.mangleObjCMethodName(MD, Out);
Anders Carlsson7b06f6c2009-12-10 03:14:39 +00001657}
1658
Douglas Gregorf1588662011-07-12 15:18:55 +00001659void CXXNameMangler::mangleType(QualType T) {
1660 // If our type is instantiation-dependent but not dependent, we mangle
1661 // it as it was written in the source, removing any top-level sugar.
1662 // Otherwise, use the canonical type.
1663 //
1664 // FIXME: This is an approximation of the instantiation-dependent name
1665 // mangling rules, since we should really be using the type as written and
1666 // augmented via semantic analysis (i.e., with implicit conversions and
1667 // default template arguments) for any instantiation-dependent type.
1668 // Unfortunately, that requires several changes to our AST:
1669 // - Instantiation-dependent TemplateSpecializationTypes will need to be
1670 // uniqued, so that we can handle substitutions properly
1671 // - Default template arguments will need to be represented in the
1672 // TemplateSpecializationType, since they need to be mangled even though
1673 // they aren't written.
1674 // - Conversions on non-type template arguments need to be expressed, since
1675 // they can affect the mangling of sizeof/alignof.
1676 if (!T->isInstantiationDependentType() || T->isDependentType())
1677 T = T.getCanonicalType();
1678 else {
1679 // Desugar any types that are purely sugar.
1680 do {
1681 // Don't desugar through template specialization types that aren't
1682 // type aliases. We need to mangle the template arguments as written.
1683 if (const TemplateSpecializationType *TST
1684 = dyn_cast<TemplateSpecializationType>(T))
1685 if (!TST->isTypeAlias())
1686 break;
Anders Carlsson4843e582009-03-10 17:07:44 +00001687
Douglas Gregorf1588662011-07-12 15:18:55 +00001688 QualType Desugared
1689 = T.getSingleStepDesugaredType(Context.getASTContext());
1690 if (Desugared == T)
1691 break;
1692
1693 T = Desugared;
1694 } while (true);
1695 }
1696 SplitQualType split = T.split();
John McCall200fa532012-02-08 00:46:36 +00001697 Qualifiers quals = split.Quals;
1698 const Type *ty = split.Ty;
John McCallb47f7482011-01-26 20:05:40 +00001699
Douglas Gregorf1588662011-07-12 15:18:55 +00001700 bool isSubstitutable = quals || !isa<BuiltinType>(T);
1701 if (isSubstitutable && mangleSubstitution(T))
Anders Carlsson76967372009-09-17 00:43:46 +00001702 return;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001703
John McCallb47f7482011-01-26 20:05:40 +00001704 // If we're mangling a qualified array type, push the qualifiers to
1705 // the element type.
Douglas Gregorf1588662011-07-12 15:18:55 +00001706 if (quals && isa<ArrayType>(T)) {
1707 ty = Context.getASTContext().getAsArrayType(T);
John McCallb47f7482011-01-26 20:05:40 +00001708 quals = Qualifiers();
1709
Douglas Gregorf1588662011-07-12 15:18:55 +00001710 // Note that we don't update T: we want to add the
1711 // substitution at the original type.
John McCallb47f7482011-01-26 20:05:40 +00001712 }
1713
1714 if (quals) {
1715 mangleQualifiers(quals);
John McCall0953e762009-09-24 19:53:00 +00001716 // Recurse: even if the qualified type isn't yet substitutable,
1717 // the unqualified type might be.
John McCallb47f7482011-01-26 20:05:40 +00001718 mangleType(QualType(ty, 0));
Anders Carlsson76967372009-09-17 00:43:46 +00001719 } else {
John McCallb47f7482011-01-26 20:05:40 +00001720 switch (ty->getTypeClass()) {
John McCallefe6aee2009-09-05 07:56:18 +00001721#define ABSTRACT_TYPE(CLASS, PARENT)
1722#define NON_CANONICAL_TYPE(CLASS, PARENT) \
Anders Carlsson76967372009-09-17 00:43:46 +00001723 case Type::CLASS: \
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +00001724 llvm_unreachable("can't mangle non-canonical type " #CLASS "Type"); \
Anders Carlsson76967372009-09-17 00:43:46 +00001725 return;
John McCallefe6aee2009-09-05 07:56:18 +00001726#define TYPE(CLASS, PARENT) \
Anders Carlsson76967372009-09-17 00:43:46 +00001727 case Type::CLASS: \
John McCallb47f7482011-01-26 20:05:40 +00001728 mangleType(static_cast<const CLASS##Type*>(ty)); \
Anders Carlsson76967372009-09-17 00:43:46 +00001729 break;
John McCallefe6aee2009-09-05 07:56:18 +00001730#include "clang/AST/TypeNodes.def"
Anders Carlsson76967372009-09-17 00:43:46 +00001731 }
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001732 }
Anders Carlsson76967372009-09-17 00:43:46 +00001733
1734 // Add the substitution.
John McCallb47f7482011-01-26 20:05:40 +00001735 if (isSubstitutable)
Douglas Gregorf1588662011-07-12 15:18:55 +00001736 addSubstitution(T);
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001737}
1738
Douglas Gregor1b12a3b2010-05-26 05:11:13 +00001739void CXXNameMangler::mangleNameOrStandardSubstitution(const NamedDecl *ND) {
1740 if (!mangleStandardSubstitution(ND))
1741 mangleName(ND);
1742}
1743
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001744void CXXNameMangler::mangleType(const BuiltinType *T) {
John McCallefe6aee2009-09-05 07:56:18 +00001745 // <type> ::= <builtin-type>
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001746 // <builtin-type> ::= v # void
1747 // ::= w # wchar_t
1748 // ::= b # bool
1749 // ::= c # char
1750 // ::= a # signed char
1751 // ::= h # unsigned char
1752 // ::= s # short
1753 // ::= t # unsigned short
1754 // ::= i # int
1755 // ::= j # unsigned int
1756 // ::= l # long
1757 // ::= m # unsigned long
1758 // ::= x # long long, __int64
1759 // ::= y # unsigned long long, __int64
1760 // ::= n # __int128
1761 // UNSUPPORTED: ::= o # unsigned __int128
1762 // ::= f # float
1763 // ::= d # double
1764 // ::= e # long double, __float80
1765 // UNSUPPORTED: ::= g # __float128
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001766 // UNSUPPORTED: ::= Dd # IEEE 754r decimal floating point (64 bits)
1767 // UNSUPPORTED: ::= De # IEEE 754r decimal floating point (128 bits)
1768 // UNSUPPORTED: ::= Df # IEEE 754r decimal floating point (32 bits)
Anton Korobeynikovaa4a99b2011-10-14 23:23:15 +00001769 // ::= Dh # IEEE 754r half-precision floating point (16 bits)
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001770 // ::= Di # char32_t
1771 // ::= Ds # char16_t
Anders Carlssone2923682010-11-04 04:31:32 +00001772 // ::= Dn # std::nullptr_t (i.e., decltype(nullptr))
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001773 // ::= u <source-name> # vendor extended type
1774 switch (T->getKind()) {
1775 case BuiltinType::Void: Out << 'v'; break;
1776 case BuiltinType::Bool: Out << 'b'; break;
1777 case BuiltinType::Char_U: case BuiltinType::Char_S: Out << 'c'; break;
1778 case BuiltinType::UChar: Out << 'h'; break;
1779 case BuiltinType::UShort: Out << 't'; break;
1780 case BuiltinType::UInt: Out << 'j'; break;
1781 case BuiltinType::ULong: Out << 'm'; break;
1782 case BuiltinType::ULongLong: Out << 'y'; break;
Chris Lattner2df9ced2009-04-30 02:43:43 +00001783 case BuiltinType::UInt128: Out << 'o'; break;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001784 case BuiltinType::SChar: Out << 'a'; break;
Chris Lattner3f59c972010-12-25 23:25:43 +00001785 case BuiltinType::WChar_S:
1786 case BuiltinType::WChar_U: Out << 'w'; break;
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001787 case BuiltinType::Char16: Out << "Ds"; break;
1788 case BuiltinType::Char32: Out << "Di"; break;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001789 case BuiltinType::Short: Out << 's'; break;
1790 case BuiltinType::Int: Out << 'i'; break;
1791 case BuiltinType::Long: Out << 'l'; break;
1792 case BuiltinType::LongLong: Out << 'x'; break;
Chris Lattner2df9ced2009-04-30 02:43:43 +00001793 case BuiltinType::Int128: Out << 'n'; break;
Anton Korobeynikovaa4a99b2011-10-14 23:23:15 +00001794 case BuiltinType::Half: Out << "Dh"; break;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001795 case BuiltinType::Float: Out << 'f'; break;
1796 case BuiltinType::Double: Out << 'd'; break;
1797 case BuiltinType::LongDouble: Out << 'e'; break;
Anders Carlssone2923682010-11-04 04:31:32 +00001798 case BuiltinType::NullPtr: Out << "Dn"; break;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001799
John McCalle0a22d02011-10-18 21:02:43 +00001800#define BUILTIN_TYPE(Id, SingletonId)
1801#define PLACEHOLDER_TYPE(Id, SingletonId) \
1802 case BuiltinType::Id:
1803#include "clang/AST/BuiltinTypes.def"
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001804 case BuiltinType::Dependent:
John McCallfb44de92011-05-01 22:35:37 +00001805 llvm_unreachable("mangling a placeholder type");
Steve Naroff9533a7f2009-07-22 17:14:51 +00001806 case BuiltinType::ObjCId: Out << "11objc_object"; break;
1807 case BuiltinType::ObjCClass: Out << "10objc_class"; break;
Fariborz Jahanian13dcd002009-11-21 19:53:08 +00001808 case BuiltinType::ObjCSel: Out << "13objc_selector"; break;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001809 }
1810}
1811
John McCallefe6aee2009-09-05 07:56:18 +00001812// <type> ::= <function-type>
1813// <function-type> ::= F [Y] <bare-function-type> E
1814void CXXNameMangler::mangleType(const FunctionProtoType *T) {
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001815 Out << 'F';
Mike Stumpf5408fe2009-05-16 07:57:57 +00001816 // FIXME: We don't have enough information in the AST to produce the 'Y'
1817 // encoding for extern "C" function types.
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001818 mangleBareFunctionType(T, /*MangleReturnType=*/true);
1819 Out << 'E';
1820}
John McCallefe6aee2009-09-05 07:56:18 +00001821void CXXNameMangler::mangleType(const FunctionNoProtoType *T) {
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +00001822 llvm_unreachable("Can't mangle K&R function prototypes");
John McCallefe6aee2009-09-05 07:56:18 +00001823}
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001824void CXXNameMangler::mangleBareFunctionType(const FunctionType *T,
1825 bool MangleReturnType) {
John McCallefe6aee2009-09-05 07:56:18 +00001826 // We should never be mangling something without a prototype.
1827 const FunctionProtoType *Proto = cast<FunctionProtoType>(T);
1828
John McCallfb44de92011-05-01 22:35:37 +00001829 // Record that we're in a function type. See mangleFunctionParam
1830 // for details on what we're trying to achieve here.
1831 FunctionTypeDepthState saved = FunctionTypeDepth.push();
1832
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001833 // <bare-function-type> ::= <signature type>+
John McCallfb44de92011-05-01 22:35:37 +00001834 if (MangleReturnType) {
1835 FunctionTypeDepth.enterResultType();
John McCallefe6aee2009-09-05 07:56:18 +00001836 mangleType(Proto->getResultType());
John McCallfb44de92011-05-01 22:35:37 +00001837 FunctionTypeDepth.leaveResultType();
1838 }
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001839
Anders Carlsson93296682010-06-02 04:40:13 +00001840 if (Proto->getNumArgs() == 0 && !Proto->isVariadic()) {
Eli Friedmana7e68452010-08-22 01:00:03 +00001841 // <builtin-type> ::= v # void
Anders Carlssonc6c91bc2009-04-01 00:15:23 +00001842 Out << 'v';
John McCallfb44de92011-05-01 22:35:37 +00001843
1844 FunctionTypeDepth.pop(saved);
Anders Carlssonc6c91bc2009-04-01 00:15:23 +00001845 return;
1846 }
Mike Stump1eb44332009-09-09 15:08:12 +00001847
Douglas Gregor72564e72009-02-26 23:50:07 +00001848 for (FunctionProtoType::arg_type_iterator Arg = Proto->arg_type_begin(),
Mike Stump1eb44332009-09-09 15:08:12 +00001849 ArgEnd = Proto->arg_type_end();
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001850 Arg != ArgEnd; ++Arg)
Douglas Gregor79e6bd32011-07-12 04:42:08 +00001851 mangleType(Context.getASTContext().getSignatureParameterType(*Arg));
Douglas Gregor219cc612009-02-13 01:28:03 +00001852
John McCallfb44de92011-05-01 22:35:37 +00001853 FunctionTypeDepth.pop(saved);
1854
Douglas Gregor219cc612009-02-13 01:28:03 +00001855 // <builtin-type> ::= z # ellipsis
1856 if (Proto->isVariadic())
1857 Out << 'z';
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001858}
1859
John McCallefe6aee2009-09-05 07:56:18 +00001860// <type> ::= <class-enum-type>
Mike Stump1eb44332009-09-09 15:08:12 +00001861// <class-enum-type> ::= <name>
John McCalled976492009-12-04 22:46:56 +00001862void CXXNameMangler::mangleType(const UnresolvedUsingType *T) {
1863 mangleName(T->getDecl());
1864}
1865
1866// <type> ::= <class-enum-type>
1867// <class-enum-type> ::= <name>
John McCallefe6aee2009-09-05 07:56:18 +00001868void CXXNameMangler::mangleType(const EnumType *T) {
1869 mangleType(static_cast<const TagType*>(T));
1870}
1871void CXXNameMangler::mangleType(const RecordType *T) {
1872 mangleType(static_cast<const TagType*>(T));
1873}
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001874void CXXNameMangler::mangleType(const TagType *T) {
Eli Friedmanecb7e932009-12-11 18:00:57 +00001875 mangleName(T->getDecl());
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001876}
1877
John McCallefe6aee2009-09-05 07:56:18 +00001878// <type> ::= <array-type>
1879// <array-type> ::= A <positive dimension number> _ <element type>
1880// ::= A [<dimension expression>] _ <element type>
1881void CXXNameMangler::mangleType(const ConstantArrayType *T) {
1882 Out << 'A' << T->getSize() << '_';
1883 mangleType(T->getElementType());
1884}
1885void CXXNameMangler::mangleType(const VariableArrayType *T) {
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001886 Out << 'A';
Fariborz Jahanian7281d1f2010-11-02 16:54:00 +00001887 // decayed vla types (size 0) will just be skipped.
1888 if (T->getSizeExpr())
1889 mangleExpression(T->getSizeExpr());
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001890 Out << '_';
1891 mangleType(T->getElementType());
1892}
John McCallefe6aee2009-09-05 07:56:18 +00001893void CXXNameMangler::mangleType(const DependentSizedArrayType *T) {
1894 Out << 'A';
1895 mangleExpression(T->getSizeExpr());
1896 Out << '_';
1897 mangleType(T->getElementType());
1898}
1899void CXXNameMangler::mangleType(const IncompleteArrayType *T) {
Nick Lewycky271b6652010-09-05 03:40:33 +00001900 Out << "A_";
John McCallefe6aee2009-09-05 07:56:18 +00001901 mangleType(T->getElementType());
1902}
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001903
John McCallefe6aee2009-09-05 07:56:18 +00001904// <type> ::= <pointer-to-member-type>
1905// <pointer-to-member-type> ::= M <class type> <member type>
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001906void CXXNameMangler::mangleType(const MemberPointerType *T) {
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001907 Out << 'M';
1908 mangleType(QualType(T->getClass(), 0));
Anders Carlsson0e650012009-05-17 17:41:20 +00001909 QualType PointeeType = T->getPointeeType();
1910 if (const FunctionProtoType *FPT = dyn_cast<FunctionProtoType>(PointeeType)) {
John McCall0953e762009-09-24 19:53:00 +00001911 mangleQualifiers(Qualifiers::fromCVRMask(FPT->getTypeQuals()));
Douglas Gregor0a9a6d62011-01-26 17:36:28 +00001912 mangleRefQualifier(FPT->getRefQualifier());
Anders Carlsson0e650012009-05-17 17:41:20 +00001913 mangleType(FPT);
Anders Carlsson9d85b722010-06-02 04:29:50 +00001914
1915 // Itanium C++ ABI 5.1.8:
1916 //
1917 // The type of a non-static member function is considered to be different,
1918 // for the purposes of substitution, from the type of a namespace-scope or
1919 // static member function whose type appears similar. The types of two
1920 // non-static member functions are considered to be different, for the
1921 // purposes of substitution, if the functions are members of different
1922 // classes. In other words, for the purposes of substitution, the class of
1923 // which the function is a member is considered part of the type of
1924 // function.
1925
1926 // We increment the SeqID here to emulate adding an entry to the
1927 // substitution table. We can't actually add it because we don't want this
1928 // particular function type to be substituted.
1929 ++SeqID;
Mike Stump1eb44332009-09-09 15:08:12 +00001930 } else
Anders Carlsson0e650012009-05-17 17:41:20 +00001931 mangleType(PointeeType);
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001932}
1933
John McCallefe6aee2009-09-05 07:56:18 +00001934// <type> ::= <template-param>
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001935void CXXNameMangler::mangleType(const TemplateTypeParmType *T) {
Anders Carlsson0ccdf8d2009-09-27 00:38:53 +00001936 mangleTemplateParameter(T->getIndex());
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001937}
1938
Douglas Gregorc3069d62011-01-14 02:55:32 +00001939// <type> ::= <template-param>
1940void CXXNameMangler::mangleType(const SubstTemplateTypeParmPackType *T) {
John McCall68a51a72011-07-01 00:04:39 +00001941 // FIXME: not clear how to mangle this!
1942 // template <class T...> class A {
1943 // template <class U...> void foo(T(*)(U) x...);
1944 // };
1945 Out << "_SUBSTPACK_";
Douglas Gregorc3069d62011-01-14 02:55:32 +00001946}
1947
John McCallefe6aee2009-09-05 07:56:18 +00001948// <type> ::= P <type> # pointer-to
1949void CXXNameMangler::mangleType(const PointerType *T) {
1950 Out << 'P';
1951 mangleType(T->getPointeeType());
1952}
1953void CXXNameMangler::mangleType(const ObjCObjectPointerType *T) {
1954 Out << 'P';
1955 mangleType(T->getPointeeType());
1956}
1957
1958// <type> ::= R <type> # reference-to
1959void CXXNameMangler::mangleType(const LValueReferenceType *T) {
1960 Out << 'R';
1961 mangleType(T->getPointeeType());
1962}
1963
1964// <type> ::= O <type> # rvalue reference-to (C++0x)
1965void CXXNameMangler::mangleType(const RValueReferenceType *T) {
1966 Out << 'O';
1967 mangleType(T->getPointeeType());
1968}
1969
1970// <type> ::= C <type> # complex pair (C 2000)
1971void CXXNameMangler::mangleType(const ComplexType *T) {
1972 Out << 'C';
1973 mangleType(T->getElementType());
1974}
1975
Bob Wilsonc7df92d2010-11-12 17:24:43 +00001976// ARM's ABI for Neon vector types specifies that they should be mangled as
Bob Wilson57147a82010-11-16 00:32:18 +00001977// if they are structs (to match ARM's initial implementation). The
1978// vector type must be one of the special types predefined by ARM.
1979void CXXNameMangler::mangleNeonVectorType(const VectorType *T) {
Bob Wilsonc7df92d2010-11-12 17:24:43 +00001980 QualType EltType = T->getElementType();
Bob Wilson57147a82010-11-16 00:32:18 +00001981 assert(EltType->isBuiltinType() && "Neon vector element not a BuiltinType");
Bob Wilsonc7df92d2010-11-12 17:24:43 +00001982 const char *EltName = 0;
Bob Wilson491328c2010-11-12 17:24:46 +00001983 if (T->getVectorKind() == VectorType::NeonPolyVector) {
1984 switch (cast<BuiltinType>(EltType)->getKind()) {
Bob Wilson4cfaa5d2010-11-12 17:24:49 +00001985 case BuiltinType::SChar: EltName = "poly8_t"; break;
1986 case BuiltinType::Short: EltName = "poly16_t"; break;
Bob Wilson57147a82010-11-16 00:32:18 +00001987 default: llvm_unreachable("unexpected Neon polynomial vector element type");
Bob Wilson491328c2010-11-12 17:24:46 +00001988 }
1989 } else {
1990 switch (cast<BuiltinType>(EltType)->getKind()) {
Bob Wilson4cfaa5d2010-11-12 17:24:49 +00001991 case BuiltinType::SChar: EltName = "int8_t"; break;
1992 case BuiltinType::UChar: EltName = "uint8_t"; break;
1993 case BuiltinType::Short: EltName = "int16_t"; break;
1994 case BuiltinType::UShort: EltName = "uint16_t"; break;
1995 case BuiltinType::Int: EltName = "int32_t"; break;
1996 case BuiltinType::UInt: EltName = "uint32_t"; break;
1997 case BuiltinType::LongLong: EltName = "int64_t"; break;
1998 case BuiltinType::ULongLong: EltName = "uint64_t"; break;
1999 case BuiltinType::Float: EltName = "float32_t"; break;
Bob Wilson57147a82010-11-16 00:32:18 +00002000 default: llvm_unreachable("unexpected Neon vector element type");
Bob Wilson491328c2010-11-12 17:24:46 +00002001 }
Bob Wilsonc7df92d2010-11-12 17:24:43 +00002002 }
2003 const char *BaseName = 0;
Bob Wilson4cfaa5d2010-11-12 17:24:49 +00002004 unsigned BitSize = (T->getNumElements() *
Bob Wilson3a723022010-11-16 00:32:12 +00002005 getASTContext().getTypeSize(EltType));
Bob Wilsonc7df92d2010-11-12 17:24:43 +00002006 if (BitSize == 64)
2007 BaseName = "__simd64_";
Bob Wilson57147a82010-11-16 00:32:18 +00002008 else {
2009 assert(BitSize == 128 && "Neon vector type not 64 or 128 bits");
Bob Wilsonc7df92d2010-11-12 17:24:43 +00002010 BaseName = "__simd128_";
Bob Wilson57147a82010-11-16 00:32:18 +00002011 }
Bob Wilsonc7df92d2010-11-12 17:24:43 +00002012 Out << strlen(BaseName) + strlen(EltName);
2013 Out << BaseName << EltName;
Bob Wilsonc7df92d2010-11-12 17:24:43 +00002014}
2015
John McCallefe6aee2009-09-05 07:56:18 +00002016// GNU extension: vector types
Chris Lattner788b0fd2010-06-23 06:00:24 +00002017// <type> ::= <vector-type>
2018// <vector-type> ::= Dv <positive dimension number> _
2019// <extended element type>
2020// ::= Dv [<dimension expression>] _ <element type>
2021// <extended element type> ::= <element type>
2022// ::= p # AltiVec vector pixel
John McCallefe6aee2009-09-05 07:56:18 +00002023void CXXNameMangler::mangleType(const VectorType *T) {
Bob Wilson491328c2010-11-12 17:24:46 +00002024 if ((T->getVectorKind() == VectorType::NeonVector ||
Bob Wilson57147a82010-11-16 00:32:18 +00002025 T->getVectorKind() == VectorType::NeonPolyVector)) {
2026 mangleNeonVectorType(T);
Bob Wilsonc7df92d2010-11-12 17:24:43 +00002027 return;
Bob Wilson57147a82010-11-16 00:32:18 +00002028 }
Nick Lewycky0e5f0672010-03-26 07:18:04 +00002029 Out << "Dv" << T->getNumElements() << '_';
Bob Wilsone86d78c2010-11-10 21:56:12 +00002030 if (T->getVectorKind() == VectorType::AltiVecPixel)
Chris Lattner788b0fd2010-06-23 06:00:24 +00002031 Out << 'p';
Bob Wilsone86d78c2010-11-10 21:56:12 +00002032 else if (T->getVectorKind() == VectorType::AltiVecBool)
Chris Lattner788b0fd2010-06-23 06:00:24 +00002033 Out << 'b';
2034 else
2035 mangleType(T->getElementType());
John McCallefe6aee2009-09-05 07:56:18 +00002036}
2037void CXXNameMangler::mangleType(const ExtVectorType *T) {
2038 mangleType(static_cast<const VectorType*>(T));
2039}
2040void CXXNameMangler::mangleType(const DependentSizedExtVectorType *T) {
Nick Lewycky0e5f0672010-03-26 07:18:04 +00002041 Out << "Dv";
2042 mangleExpression(T->getSizeExpr());
2043 Out << '_';
John McCallefe6aee2009-09-05 07:56:18 +00002044 mangleType(T->getElementType());
2045}
2046
Douglas Gregor7536dd52010-12-20 02:24:11 +00002047void CXXNameMangler::mangleType(const PackExpansionType *T) {
Douglas Gregor4fc48662011-01-13 16:39:34 +00002048 // <type> ::= Dp <type> # pack expansion (C++0x)
Douglas Gregor255c2692011-01-13 17:44:36 +00002049 Out << "Dp";
Douglas Gregor7536dd52010-12-20 02:24:11 +00002050 mangleType(T->getPattern());
2051}
2052
Anders Carlssona40c5e42009-03-07 22:03:21 +00002053void CXXNameMangler::mangleType(const ObjCInterfaceType *T) {
2054 mangleSourceName(T->getDecl()->getIdentifier());
2055}
2056
John McCallc12c5bb2010-05-15 11:32:37 +00002057void CXXNameMangler::mangleType(const ObjCObjectType *T) {
John McCallc00c1f62010-05-15 17:06:29 +00002058 // We don't allow overloading by different protocol qualification,
2059 // so mangling them isn't necessary.
John McCallc12c5bb2010-05-15 11:32:37 +00002060 mangleType(T->getBaseType());
2061}
2062
John McCallefe6aee2009-09-05 07:56:18 +00002063void CXXNameMangler::mangleType(const BlockPointerType *T) {
Anders Carlssonf28c6872009-12-23 22:31:44 +00002064 Out << "U13block_pointer";
2065 mangleType(T->getPointeeType());
John McCallefe6aee2009-09-05 07:56:18 +00002066}
2067
John McCall31f17ec2010-04-27 00:57:59 +00002068void CXXNameMangler::mangleType(const InjectedClassNameType *T) {
2069 // Mangle injected class name types as if the user had written the
2070 // specialization out fully. It may not actually be possible to see
2071 // this mangling, though.
2072 mangleType(T->getInjectedSpecializationType());
2073}
2074
John McCallefe6aee2009-09-05 07:56:18 +00002075void CXXNameMangler::mangleType(const TemplateSpecializationType *T) {
Douglas Gregor1e9268e2010-04-28 05:58:56 +00002076 if (TemplateDecl *TD = T->getTemplateName().getAsTemplateDecl()) {
2077 mangleName(TD, T->getArgs(), T->getNumArgs());
2078 } else {
2079 if (mangleSubstitution(QualType(T, 0)))
2080 return;
Sean Huntc3021132010-05-05 15:23:54 +00002081
Douglas Gregor1e9268e2010-04-28 05:58:56 +00002082 mangleTemplatePrefix(T->getTemplateName());
Sean Huntc3021132010-05-05 15:23:54 +00002083
Douglas Gregor1e9268e2010-04-28 05:58:56 +00002084 // FIXME: GCC does not appear to mangle the template arguments when
2085 // the template in question is a dependent template name. Should we
2086 // emulate that badness?
2087 mangleTemplateArgs(T->getTemplateName(), T->getArgs(), T->getNumArgs());
2088 addSubstitution(QualType(T, 0));
2089 }
John McCallefe6aee2009-09-05 07:56:18 +00002090}
2091
Douglas Gregor4714c122010-03-31 17:34:00 +00002092void CXXNameMangler::mangleType(const DependentNameType *T) {
Anders Carlssonae352482009-09-26 02:26:02 +00002093 // Typename types are always nested
2094 Out << 'N';
John McCalla0ce15c2011-04-24 08:23:24 +00002095 manglePrefix(T->getQualifier());
John McCall33500952010-06-11 00:33:02 +00002096 mangleSourceName(T->getIdentifier());
2097 Out << 'E';
2098}
John McCall6ab30e02010-06-09 07:26:17 +00002099
John McCall33500952010-06-11 00:33:02 +00002100void CXXNameMangler::mangleType(const DependentTemplateSpecializationType *T) {
Douglas Gregoraa2187d2011-02-28 00:04:36 +00002101 // Dependently-scoped template types are nested if they have a prefix.
John McCall33500952010-06-11 00:33:02 +00002102 Out << 'N';
2103
2104 // TODO: avoid making this TemplateName.
2105 TemplateName Prefix =
2106 getASTContext().getDependentTemplateName(T->getQualifier(),
2107 T->getIdentifier());
2108 mangleTemplatePrefix(Prefix);
2109
2110 // FIXME: GCC does not appear to mangle the template arguments when
2111 // the template in question is a dependent template name. Should we
2112 // emulate that badness?
2113 mangleTemplateArgs(Prefix, T->getArgs(), T->getNumArgs());
Anders Carlssonae352482009-09-26 02:26:02 +00002114 Out << 'E';
John McCallefe6aee2009-09-05 07:56:18 +00002115}
2116
John McCallad5e7382010-03-01 23:49:17 +00002117void CXXNameMangler::mangleType(const TypeOfType *T) {
2118 // FIXME: this is pretty unsatisfactory, but there isn't an obvious
2119 // "extension with parameters" mangling.
2120 Out << "u6typeof";
2121}
2122
2123void CXXNameMangler::mangleType(const TypeOfExprType *T) {
2124 // FIXME: this is pretty unsatisfactory, but there isn't an obvious
2125 // "extension with parameters" mangling.
2126 Out << "u6typeof";
2127}
2128
2129void CXXNameMangler::mangleType(const DecltypeType *T) {
2130 Expr *E = T->getUnderlyingExpr();
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002131
John McCallad5e7382010-03-01 23:49:17 +00002132 // type ::= Dt <expression> E # decltype of an id-expression
2133 // # or class member access
2134 // ::= DT <expression> E # decltype of an expression
2135
2136 // This purports to be an exhaustive list of id-expressions and
2137 // class member accesses. Note that we do not ignore parentheses;
2138 // parentheses change the semantics of decltype for these
2139 // expressions (and cause the mangler to use the other form).
2140 if (isa<DeclRefExpr>(E) ||
2141 isa<MemberExpr>(E) ||
2142 isa<UnresolvedLookupExpr>(E) ||
2143 isa<DependentScopeDeclRefExpr>(E) ||
2144 isa<CXXDependentScopeMemberExpr>(E) ||
2145 isa<UnresolvedMemberExpr>(E))
2146 Out << "Dt";
2147 else
2148 Out << "DT";
2149 mangleExpression(E);
2150 Out << 'E';
2151}
2152
Sean Huntca63c202011-05-24 22:41:36 +00002153void CXXNameMangler::mangleType(const UnaryTransformType *T) {
2154 // If this is dependent, we need to record that. If not, we simply
2155 // mangle it as the underlying type since they are equivalent.
2156 if (T->isDependentType()) {
2157 Out << 'U';
2158
2159 switch (T->getUTTKind()) {
2160 case UnaryTransformType::EnumUnderlyingType:
2161 Out << "3eut";
2162 break;
2163 }
2164 }
2165
2166 mangleType(T->getUnderlyingType());
2167}
2168
Richard Smith34b41d92011-02-20 03:19:35 +00002169void CXXNameMangler::mangleType(const AutoType *T) {
2170 QualType D = T->getDeducedType();
Richard Smith967ecd32011-02-21 20:10:02 +00002171 // <builtin-type> ::= Da # dependent auto
2172 if (D.isNull())
2173 Out << "Da";
2174 else
2175 mangleType(D);
Richard Smith34b41d92011-02-20 03:19:35 +00002176}
2177
Eli Friedmanb001de72011-10-06 23:00:33 +00002178void CXXNameMangler::mangleType(const AtomicType *T) {
2179 // <type> ::= U <source-name> <type> # vendor extended type qualifier
2180 // (Until there's a standardized mangling...)
2181 Out << "U7_Atomic";
2182 mangleType(T->getValueType());
2183}
2184
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002185void CXXNameMangler::mangleIntegerLiteral(QualType T,
Anders Carlssone170ba72009-12-14 01:45:37 +00002186 const llvm::APSInt &Value) {
2187 // <expr-primary> ::= L <type> <value number> E # integer literal
2188 Out << 'L';
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002189
Anders Carlssone170ba72009-12-14 01:45:37 +00002190 mangleType(T);
2191 if (T->isBooleanType()) {
2192 // Boolean values are encoded as 0/1.
2193 Out << (Value.getBoolValue() ? '1' : '0');
2194 } else {
John McCall0512e482010-07-14 04:20:34 +00002195 mangleNumber(Value);
Anders Carlssone170ba72009-12-14 01:45:37 +00002196 }
2197 Out << 'E';
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002198
Anders Carlssone170ba72009-12-14 01:45:37 +00002199}
2200
John McCall2f27bf82010-02-04 02:56:29 +00002201/// Mangles a member expression. Implicit accesses are not handled,
2202/// but that should be okay, because you shouldn't be able to
2203/// make an implicit access in a function template declaration.
John McCalla0ce15c2011-04-24 08:23:24 +00002204void CXXNameMangler::mangleMemberExpr(const Expr *base,
2205 bool isArrow,
2206 NestedNameSpecifier *qualifier,
2207 NamedDecl *firstQualifierLookup,
2208 DeclarationName member,
2209 unsigned arity) {
2210 // <expression> ::= dt <expression> <unresolved-name>
2211 // ::= pt <expression> <unresolved-name>
2212 Out << (isArrow ? "pt" : "dt");
2213 mangleExpression(base);
2214 mangleUnresolvedName(qualifier, firstQualifierLookup, member, arity);
John McCall2f27bf82010-02-04 02:56:29 +00002215}
2216
John McCall5a7e6f72011-04-28 02:52:03 +00002217/// Look at the callee of the given call expression and determine if
2218/// it's a parenthesized id-expression which would have triggered ADL
2219/// otherwise.
2220static bool isParenthesizedADLCallee(const CallExpr *call) {
2221 const Expr *callee = call->getCallee();
2222 const Expr *fn = callee->IgnoreParens();
2223
2224 // Must be parenthesized. IgnoreParens() skips __extension__ nodes,
2225 // too, but for those to appear in the callee, it would have to be
2226 // parenthesized.
2227 if (callee == fn) return false;
2228
2229 // Must be an unresolved lookup.
2230 const UnresolvedLookupExpr *lookup = dyn_cast<UnresolvedLookupExpr>(fn);
2231 if (!lookup) return false;
2232
2233 assert(!lookup->requiresADL());
2234
2235 // Must be an unqualified lookup.
2236 if (lookup->getQualifier()) return false;
2237
2238 // Must not have found a class member. Note that if one is a class
2239 // member, they're all class members.
2240 if (lookup->getNumDecls() > 0 &&
2241 (*lookup->decls_begin())->isCXXClassMember())
2242 return false;
2243
2244 // Otherwise, ADL would have been triggered.
2245 return true;
2246}
2247
John McCall5e1e89b2010-08-18 19:18:59 +00002248void CXXNameMangler::mangleExpression(const Expr *E, unsigned Arity) {
Anders Carlssond553f8c2009-09-21 01:21:10 +00002249 // <expression> ::= <unary operator-name> <expression>
John McCall09cc1412010-02-03 00:55:45 +00002250 // ::= <binary operator-name> <expression> <expression>
2251 // ::= <trinary operator-name> <expression> <expression> <expression>
Anders Carlssond553f8c2009-09-21 01:21:10 +00002252 // ::= cv <type> expression # conversion with one argument
2253 // ::= cv <type> _ <expression>* E # conversion with a different number of arguments
Eli Friedmana7e68452010-08-22 01:00:03 +00002254 // ::= st <type> # sizeof (a type)
Anders Carlssond553f8c2009-09-21 01:21:10 +00002255 // ::= at <type> # alignof (a type)
2256 // ::= <template-param>
2257 // ::= <function-param>
2258 // ::= sr <type> <unqualified-name> # dependent name
2259 // ::= sr <type> <unqualified-name> <template-args> # dependent template-id
Douglas Gregor63f62df2011-06-05 05:27:58 +00002260 // ::= ds <expression> <expression> # expr.*expr
Anders Carlssond553f8c2009-09-21 01:21:10 +00002261 // ::= sZ <template-param> # size of a parameter pack
Douglas Gregor4fc48662011-01-13 16:39:34 +00002262 // ::= sZ <function-param> # size of a function parameter pack
John McCall09cc1412010-02-03 00:55:45 +00002263 // ::= <expr-primary>
John McCall1dd73832010-02-04 01:42:13 +00002264 // <expr-primary> ::= L <type> <value number> E # integer literal
2265 // ::= L <type <value float> E # floating literal
2266 // ::= L <mangled-name> E # external name
Douglas Gregoredee94b2011-07-12 04:47:20 +00002267 QualType ImplicitlyConvertedToType;
2268
2269recurse:
Anders Carlssond553f8c2009-09-21 01:21:10 +00002270 switch (E->getStmtClass()) {
John McCall6ae1f352010-04-09 22:26:14 +00002271 case Expr::NoStmtClass:
John McCall63c00d72011-02-09 08:16:59 +00002272#define ABSTRACT_STMT(Type)
John McCall6ae1f352010-04-09 22:26:14 +00002273#define EXPR(Type, Base)
2274#define STMT(Type, Base) \
2275 case Expr::Type##Class:
Sean Hunt4bfe1962010-05-05 15:24:00 +00002276#include "clang/AST/StmtNodes.inc"
John McCall0512e482010-07-14 04:20:34 +00002277 // fallthrough
2278
2279 // These all can only appear in local or variable-initialization
2280 // contexts and so should never appear in a mangling.
2281 case Expr::AddrLabelExprClass:
2282 case Expr::BlockDeclRefExprClass:
2283 case Expr::CXXThisExprClass:
2284 case Expr::DesignatedInitExprClass:
2285 case Expr::ImplicitValueInitExprClass:
2286 case Expr::InitListExprClass:
2287 case Expr::ParenListExprClass:
Douglas Gregor01d08012012-02-07 10:09:13 +00002288 case Expr::LambdaExprClass:
John McCall09cc1412010-02-03 00:55:45 +00002289 llvm_unreachable("unexpected statement kind");
John McCall09cc1412010-02-03 00:55:45 +00002290
John McCall0512e482010-07-14 04:20:34 +00002291 // FIXME: invent manglings for all these.
2292 case Expr::BlockExprClass:
2293 case Expr::CXXPseudoDestructorExprClass:
2294 case Expr::ChooseExprClass:
2295 case Expr::CompoundLiteralExprClass:
2296 case Expr::ExtVectorElementExprClass:
Peter Collingbournef111d932011-04-15 00:35:48 +00002297 case Expr::GenericSelectionExprClass:
John McCall0512e482010-07-14 04:20:34 +00002298 case Expr::ObjCEncodeExprClass:
John McCall0512e482010-07-14 04:20:34 +00002299 case Expr::ObjCIsaExprClass:
2300 case Expr::ObjCIvarRefExprClass:
2301 case Expr::ObjCMessageExprClass:
2302 case Expr::ObjCPropertyRefExprClass:
2303 case Expr::ObjCProtocolExprClass:
2304 case Expr::ObjCSelectorExprClass:
2305 case Expr::ObjCStringLiteralClass:
John McCallf85e1932011-06-15 23:02:42 +00002306 case Expr::ObjCIndirectCopyRestoreExprClass:
John McCall0512e482010-07-14 04:20:34 +00002307 case Expr::OffsetOfExprClass:
2308 case Expr::PredefinedExprClass:
2309 case Expr::ShuffleVectorExprClass:
2310 case Expr::StmtExprClass:
John McCall0512e482010-07-14 04:20:34 +00002311 case Expr::UnaryTypeTraitExprClass:
Francois Pichet6ad6f282010-12-07 00:08:36 +00002312 case Expr::BinaryTypeTraitExprClass:
John Wiegley21ff2e52011-04-28 00:16:57 +00002313 case Expr::ArrayTypeTraitExprClass:
John Wiegley55262202011-04-25 06:54:41 +00002314 case Expr::ExpressionTraitExprClass:
Francois Pichet9be88402010-09-08 23:47:05 +00002315 case Expr::VAArgExprClass:
Sebastian Redl2e156222010-09-10 20:55:43 +00002316 case Expr::CXXUuidofExprClass:
Peter Collingbournee08ce652011-02-09 21:07:24 +00002317 case Expr::CXXNoexceptExprClass:
Tanya Lattner61eee0c2011-06-04 00:47:47 +00002318 case Expr::CUDAKernelCallExprClass:
2319 case Expr::AsTypeExprClass:
John McCall4b9c2d22011-11-06 09:01:30 +00002320 case Expr::PseudoObjectExprClass:
Eli Friedman276b0612011-10-11 02:20:01 +00002321 case Expr::AtomicExprClass:
Tanya Lattner61eee0c2011-06-04 00:47:47 +00002322 {
John McCall6ae1f352010-04-09 22:26:14 +00002323 // As bad as this diagnostic is, it's better than crashing.
David Blaikied6471f72011-09-25 23:23:43 +00002324 DiagnosticsEngine &Diags = Context.getDiags();
2325 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
John McCall6ae1f352010-04-09 22:26:14 +00002326 "cannot yet mangle expression type %0");
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +00002327 Diags.Report(E->getExprLoc(), DiagID)
John McCall739bf092010-04-10 09:39:25 +00002328 << E->getStmtClassName() << E->getSourceRange();
John McCall6ae1f352010-04-09 22:26:14 +00002329 break;
2330 }
2331
John McCall56ca35d2011-02-17 10:25:35 +00002332 // Even gcc-4.5 doesn't mangle this.
2333 case Expr::BinaryConditionalOperatorClass: {
David Blaikied6471f72011-09-25 23:23:43 +00002334 DiagnosticsEngine &Diags = Context.getDiags();
John McCall56ca35d2011-02-17 10:25:35 +00002335 unsigned DiagID =
David Blaikied6471f72011-09-25 23:23:43 +00002336 Diags.getCustomDiagID(DiagnosticsEngine::Error,
John McCall56ca35d2011-02-17 10:25:35 +00002337 "?: operator with omitted middle operand cannot be mangled");
2338 Diags.Report(E->getExprLoc(), DiagID)
2339 << E->getStmtClassName() << E->getSourceRange();
2340 break;
2341 }
2342
2343 // These are used for internal purposes and cannot be meaningfully mangled.
John McCall7cd7d1a2010-11-15 23:31:06 +00002344 case Expr::OpaqueValueExprClass:
2345 llvm_unreachable("cannot mangle opaque value; mangling wrong thing?");
2346
John McCall0512e482010-07-14 04:20:34 +00002347 case Expr::CXXDefaultArgExprClass:
John McCall5e1e89b2010-08-18 19:18:59 +00002348 mangleExpression(cast<CXXDefaultArgExpr>(E)->getExpr(), Arity);
John McCall0512e482010-07-14 04:20:34 +00002349 break;
2350
John McCall91a57552011-07-15 05:09:51 +00002351 case Expr::SubstNonTypeTemplateParmExprClass:
2352 mangleExpression(cast<SubstNonTypeTemplateParmExpr>(E)->getReplacement(),
2353 Arity);
2354 break;
2355
John McCall0512e482010-07-14 04:20:34 +00002356 case Expr::CXXMemberCallExprClass: // fallthrough
John McCall1dd73832010-02-04 01:42:13 +00002357 case Expr::CallExprClass: {
2358 const CallExpr *CE = cast<CallExpr>(E);
John McCall5a7e6f72011-04-28 02:52:03 +00002359
2360 // <expression> ::= cp <simple-id> <expression>* E
2361 // We use this mangling only when the call would use ADL except
2362 // for being parenthesized. Per discussion with David
2363 // Vandervoorde, 2011.04.25.
2364 if (isParenthesizedADLCallee(CE)) {
2365 Out << "cp";
2366 // The callee here is a parenthesized UnresolvedLookupExpr with
2367 // no qualifier and should always get mangled as a <simple-id>
2368 // anyway.
2369
2370 // <expression> ::= cl <expression>* E
2371 } else {
2372 Out << "cl";
2373 }
2374
John McCall5e1e89b2010-08-18 19:18:59 +00002375 mangleExpression(CE->getCallee(), CE->getNumArgs());
John McCall1dd73832010-02-04 01:42:13 +00002376 for (unsigned I = 0, N = CE->getNumArgs(); I != N; ++I)
2377 mangleExpression(CE->getArg(I));
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002378 Out << 'E';
John McCall09cc1412010-02-03 00:55:45 +00002379 break;
John McCall1dd73832010-02-04 01:42:13 +00002380 }
John McCall09cc1412010-02-03 00:55:45 +00002381
John McCall0512e482010-07-14 04:20:34 +00002382 case Expr::CXXNewExprClass: {
2383 // Proposal from David Vandervoorde, 2010.06.30
2384 const CXXNewExpr *New = cast<CXXNewExpr>(E);
2385 if (New->isGlobalNew()) Out << "gs";
2386 Out << (New->isArray() ? "na" : "nw");
2387 for (CXXNewExpr::const_arg_iterator I = New->placement_arg_begin(),
2388 E = New->placement_arg_end(); I != E; ++I)
2389 mangleExpression(*I);
2390 Out << '_';
2391 mangleType(New->getAllocatedType());
2392 if (New->hasInitializer()) {
Sebastian Redl2aed8b82012-02-16 12:22:20 +00002393 // FIXME: Does this mean "parenthesized initializer"?
John McCall0512e482010-07-14 04:20:34 +00002394 Out << "pi";
Sebastian Redl2aed8b82012-02-16 12:22:20 +00002395 const Expr *Init = New->getInitializer();
2396 if (const CXXConstructExpr *CCE = dyn_cast<CXXConstructExpr>(Init)) {
2397 // Directly inline the initializers.
2398 for (CXXConstructExpr::const_arg_iterator I = CCE->arg_begin(),
2399 E = CCE->arg_end();
2400 I != E; ++I)
2401 mangleExpression(*I);
2402 } else if (const ParenListExpr *PLE = dyn_cast<ParenListExpr>(Init)) {
2403 for (unsigned i = 0, e = PLE->getNumExprs(); i != e; ++i)
2404 mangleExpression(PLE->getExpr(i));
2405 } else
2406 mangleExpression(Init);
John McCall0512e482010-07-14 04:20:34 +00002407 }
2408 Out << 'E';
2409 break;
2410 }
2411
John McCall2f27bf82010-02-04 02:56:29 +00002412 case Expr::MemberExprClass: {
2413 const MemberExpr *ME = cast<MemberExpr>(E);
2414 mangleMemberExpr(ME->getBase(), ME->isArrow(),
John McCalla0ce15c2011-04-24 08:23:24 +00002415 ME->getQualifier(), 0, ME->getMemberDecl()->getDeclName(),
John McCall5e1e89b2010-08-18 19:18:59 +00002416 Arity);
John McCall2f27bf82010-02-04 02:56:29 +00002417 break;
2418 }
2419
2420 case Expr::UnresolvedMemberExprClass: {
2421 const UnresolvedMemberExpr *ME = cast<UnresolvedMemberExpr>(E);
2422 mangleMemberExpr(ME->getBase(), ME->isArrow(),
John McCalla0ce15c2011-04-24 08:23:24 +00002423 ME->getQualifier(), 0, ME->getMemberName(),
John McCall5e1e89b2010-08-18 19:18:59 +00002424 Arity);
John McCall6dbce192010-08-20 00:17:19 +00002425 if (ME->hasExplicitTemplateArgs())
2426 mangleTemplateArgs(ME->getExplicitTemplateArgs());
John McCall2f27bf82010-02-04 02:56:29 +00002427 break;
2428 }
2429
2430 case Expr::CXXDependentScopeMemberExprClass: {
2431 const CXXDependentScopeMemberExpr *ME
2432 = cast<CXXDependentScopeMemberExpr>(E);
2433 mangleMemberExpr(ME->getBase(), ME->isArrow(),
John McCalla0ce15c2011-04-24 08:23:24 +00002434 ME->getQualifier(), ME->getFirstQualifierFoundInScope(),
2435 ME->getMember(), Arity);
John McCall6dbce192010-08-20 00:17:19 +00002436 if (ME->hasExplicitTemplateArgs())
2437 mangleTemplateArgs(ME->getExplicitTemplateArgs());
John McCall2f27bf82010-02-04 02:56:29 +00002438 break;
2439 }
2440
John McCall1dd73832010-02-04 01:42:13 +00002441 case Expr::UnresolvedLookupExprClass: {
2442 const UnresolvedLookupExpr *ULE = cast<UnresolvedLookupExpr>(E);
John McCalla0ce15c2011-04-24 08:23:24 +00002443 mangleUnresolvedName(ULE->getQualifier(), 0, ULE->getName(), Arity);
John McCall26a6ec72011-06-21 22:12:46 +00002444
2445 // All the <unresolved-name> productions end in a
2446 // base-unresolved-name, where <template-args> are just tacked
2447 // onto the end.
John McCall6dbce192010-08-20 00:17:19 +00002448 if (ULE->hasExplicitTemplateArgs())
2449 mangleTemplateArgs(ULE->getExplicitTemplateArgs());
John McCall1dd73832010-02-04 01:42:13 +00002450 break;
2451 }
2452
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002453 case Expr::CXXUnresolvedConstructExprClass: {
John McCall1dd73832010-02-04 01:42:13 +00002454 const CXXUnresolvedConstructExpr *CE = cast<CXXUnresolvedConstructExpr>(E);
2455 unsigned N = CE->arg_size();
2456
2457 Out << "cv";
2458 mangleType(CE->getType());
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002459 if (N != 1) Out << '_';
John McCall1dd73832010-02-04 01:42:13 +00002460 for (unsigned I = 0; I != N; ++I) mangleExpression(CE->getArg(I));
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002461 if (N != 1) Out << 'E';
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002462 break;
John McCall1dd73832010-02-04 01:42:13 +00002463 }
John McCall09cc1412010-02-03 00:55:45 +00002464
John McCall1dd73832010-02-04 01:42:13 +00002465 case Expr::CXXTemporaryObjectExprClass:
2466 case Expr::CXXConstructExprClass: {
2467 const CXXConstructExpr *CE = cast<CXXConstructExpr>(E);
2468 unsigned N = CE->getNumArgs();
2469
2470 Out << "cv";
2471 mangleType(CE->getType());
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002472 if (N != 1) Out << '_';
John McCall1dd73832010-02-04 01:42:13 +00002473 for (unsigned I = 0; I != N; ++I) mangleExpression(CE->getArg(I));
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002474 if (N != 1) Out << 'E';
John McCall09cc1412010-02-03 00:55:45 +00002475 break;
John McCall1dd73832010-02-04 01:42:13 +00002476 }
2477
Richard Smith41576d42012-02-06 02:54:51 +00002478 case Expr::CXXScalarValueInitExprClass:
2479 Out <<"cv";
2480 mangleType(E->getType());
2481 Out <<"_E";
2482 break;
2483
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +00002484 case Expr::UnaryExprOrTypeTraitExprClass: {
2485 const UnaryExprOrTypeTraitExpr *SAE = cast<UnaryExprOrTypeTraitExpr>(E);
Douglas Gregoredee94b2011-07-12 04:47:20 +00002486
2487 if (!SAE->isInstantiationDependent()) {
2488 // Itanium C++ ABI:
2489 // If the operand of a sizeof or alignof operator is not
2490 // instantiation-dependent it is encoded as an integer literal
2491 // reflecting the result of the operator.
2492 //
2493 // If the result of the operator is implicitly converted to a known
2494 // integer type, that type is used for the literal; otherwise, the type
2495 // of std::size_t or std::ptrdiff_t is used.
2496 QualType T = (ImplicitlyConvertedToType.isNull() ||
2497 !ImplicitlyConvertedToType->isIntegerType())? SAE->getType()
2498 : ImplicitlyConvertedToType;
Richard Smitha6b8b2c2011-10-10 18:28:20 +00002499 llvm::APSInt V = SAE->EvaluateKnownConstInt(Context.getASTContext());
2500 mangleIntegerLiteral(T, V);
Douglas Gregoredee94b2011-07-12 04:47:20 +00002501 break;
2502 }
2503
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +00002504 switch(SAE->getKind()) {
2505 case UETT_SizeOf:
2506 Out << 's';
2507 break;
2508 case UETT_AlignOf:
2509 Out << 'a';
2510 break;
2511 case UETT_VecStep:
David Blaikied6471f72011-09-25 23:23:43 +00002512 DiagnosticsEngine &Diags = Context.getDiags();
2513 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +00002514 "cannot yet mangle vec_step expression");
2515 Diags.Report(DiagID);
2516 return;
2517 }
John McCall1dd73832010-02-04 01:42:13 +00002518 if (SAE->isArgumentType()) {
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002519 Out << 't';
John McCall1dd73832010-02-04 01:42:13 +00002520 mangleType(SAE->getArgumentType());
2521 } else {
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002522 Out << 'z';
John McCall1dd73832010-02-04 01:42:13 +00002523 mangleExpression(SAE->getArgumentExpr());
2524 }
2525 break;
2526 }
Anders Carlssona7694082009-11-06 02:50:19 +00002527
John McCall0512e482010-07-14 04:20:34 +00002528 case Expr::CXXThrowExprClass: {
2529 const CXXThrowExpr *TE = cast<CXXThrowExpr>(E);
2530
2531 // Proposal from David Vandervoorde, 2010.06.30
2532 if (TE->getSubExpr()) {
2533 Out << "tw";
2534 mangleExpression(TE->getSubExpr());
2535 } else {
2536 Out << "tr";
2537 }
2538 break;
2539 }
2540
2541 case Expr::CXXTypeidExprClass: {
2542 const CXXTypeidExpr *TIE = cast<CXXTypeidExpr>(E);
2543
2544 // Proposal from David Vandervoorde, 2010.06.30
2545 if (TIE->isTypeOperand()) {
2546 Out << "ti";
2547 mangleType(TIE->getTypeOperand());
2548 } else {
2549 Out << "te";
2550 mangleExpression(TIE->getExprOperand());
2551 }
2552 break;
2553 }
2554
2555 case Expr::CXXDeleteExprClass: {
2556 const CXXDeleteExpr *DE = cast<CXXDeleteExpr>(E);
2557
2558 // Proposal from David Vandervoorde, 2010.06.30
2559 if (DE->isGlobalDelete()) Out << "gs";
2560 Out << (DE->isArrayForm() ? "da" : "dl");
2561 mangleExpression(DE->getArgument());
2562 break;
2563 }
2564
Anders Carlssone170ba72009-12-14 01:45:37 +00002565 case Expr::UnaryOperatorClass: {
2566 const UnaryOperator *UO = cast<UnaryOperator>(E);
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002567 mangleOperatorName(UnaryOperator::getOverloadedOperator(UO->getOpcode()),
Anders Carlssone170ba72009-12-14 01:45:37 +00002568 /*Arity=*/1);
2569 mangleExpression(UO->getSubExpr());
2570 break;
2571 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002572
John McCall0512e482010-07-14 04:20:34 +00002573 case Expr::ArraySubscriptExprClass: {
2574 const ArraySubscriptExpr *AE = cast<ArraySubscriptExpr>(E);
2575
Chris Lattnerfc8f0e12011-04-15 05:22:18 +00002576 // Array subscript is treated as a syntactically weird form of
John McCall0512e482010-07-14 04:20:34 +00002577 // binary operator.
2578 Out << "ix";
2579 mangleExpression(AE->getLHS());
2580 mangleExpression(AE->getRHS());
2581 break;
2582 }
2583
2584 case Expr::CompoundAssignOperatorClass: // fallthrough
Anders Carlssone170ba72009-12-14 01:45:37 +00002585 case Expr::BinaryOperatorClass: {
2586 const BinaryOperator *BO = cast<BinaryOperator>(E);
Douglas Gregor63f62df2011-06-05 05:27:58 +00002587 if (BO->getOpcode() == BO_PtrMemD)
2588 Out << "ds";
2589 else
2590 mangleOperatorName(BinaryOperator::getOverloadedOperator(BO->getOpcode()),
2591 /*Arity=*/2);
Anders Carlssone170ba72009-12-14 01:45:37 +00002592 mangleExpression(BO->getLHS());
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002593 mangleExpression(BO->getRHS());
Anders Carlssone170ba72009-12-14 01:45:37 +00002594 break;
John McCall2f27bf82010-02-04 02:56:29 +00002595 }
Anders Carlssone170ba72009-12-14 01:45:37 +00002596
2597 case Expr::ConditionalOperatorClass: {
2598 const ConditionalOperator *CO = cast<ConditionalOperator>(E);
2599 mangleOperatorName(OO_Conditional, /*Arity=*/3);
2600 mangleExpression(CO->getCond());
John McCall5e1e89b2010-08-18 19:18:59 +00002601 mangleExpression(CO->getLHS(), Arity);
2602 mangleExpression(CO->getRHS(), Arity);
Anders Carlssone170ba72009-12-14 01:45:37 +00002603 break;
2604 }
2605
Douglas Gregor46287c72010-01-29 16:37:09 +00002606 case Expr::ImplicitCastExprClass: {
Douglas Gregoredee94b2011-07-12 04:47:20 +00002607 ImplicitlyConvertedToType = E->getType();
2608 E = cast<ImplicitCastExpr>(E)->getSubExpr();
2609 goto recurse;
Douglas Gregor46287c72010-01-29 16:37:09 +00002610 }
John McCallf85e1932011-06-15 23:02:42 +00002611
2612 case Expr::ObjCBridgedCastExprClass: {
2613 // Mangle ownership casts as a vendor extended operator __bridge,
2614 // __bridge_transfer, or __bridge_retain.
Chris Lattner5f9e2722011-07-23 10:55:15 +00002615 StringRef Kind = cast<ObjCBridgedCastExpr>(E)->getBridgeKindName();
John McCallf85e1932011-06-15 23:02:42 +00002616 Out << "v1U" << Kind.size() << Kind;
2617 }
2618 // Fall through to mangle the cast itself.
2619
Douglas Gregor46287c72010-01-29 16:37:09 +00002620 case Expr::CStyleCastExprClass:
2621 case Expr::CXXStaticCastExprClass:
2622 case Expr::CXXDynamicCastExprClass:
2623 case Expr::CXXReinterpretCastExprClass:
2624 case Expr::CXXConstCastExprClass:
2625 case Expr::CXXFunctionalCastExprClass: {
2626 const ExplicitCastExpr *ECE = cast<ExplicitCastExpr>(E);
2627 Out << "cv";
2628 mangleType(ECE->getType());
2629 mangleExpression(ECE->getSubExpr());
2630 break;
2631 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002632
Anders Carlsson58040a52009-12-16 05:48:46 +00002633 case Expr::CXXOperatorCallExprClass: {
2634 const CXXOperatorCallExpr *CE = cast<CXXOperatorCallExpr>(E);
2635 unsigned NumArgs = CE->getNumArgs();
2636 mangleOperatorName(CE->getOperator(), /*Arity=*/NumArgs);
2637 // Mangle the arguments.
2638 for (unsigned i = 0; i != NumArgs; ++i)
2639 mangleExpression(CE->getArg(i));
2640 break;
2641 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002642
Anders Carlssona7694082009-11-06 02:50:19 +00002643 case Expr::ParenExprClass:
John McCall5e1e89b2010-08-18 19:18:59 +00002644 mangleExpression(cast<ParenExpr>(E)->getSubExpr(), Arity);
Anders Carlssona7694082009-11-06 02:50:19 +00002645 break;
2646
Anders Carlssond553f8c2009-09-21 01:21:10 +00002647 case Expr::DeclRefExprClass: {
Douglas Gregor5ed1bc32010-02-28 21:40:32 +00002648 const NamedDecl *D = cast<DeclRefExpr>(E)->getDecl();
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002649
Anders Carlssond553f8c2009-09-21 01:21:10 +00002650 switch (D->getKind()) {
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002651 default:
Douglas Gregor5ed1bc32010-02-28 21:40:32 +00002652 // <expr-primary> ::= L <mangled-name> E # external name
2653 Out << 'L';
2654 mangle(D, "_Z");
2655 Out << 'E';
2656 break;
2657
John McCallfb44de92011-05-01 22:35:37 +00002658 case Decl::ParmVar:
2659 mangleFunctionParam(cast<ParmVarDecl>(D));
2660 break;
2661
John McCall3dc7e7b2010-07-24 01:17:35 +00002662 case Decl::EnumConstant: {
2663 const EnumConstantDecl *ED = cast<EnumConstantDecl>(D);
2664 mangleIntegerLiteral(ED->getType(), ED->getInitVal());
2665 break;
2666 }
2667
Anders Carlssond553f8c2009-09-21 01:21:10 +00002668 case Decl::NonTypeTemplateParm: {
2669 const NonTypeTemplateParmDecl *PD = cast<NonTypeTemplateParmDecl>(D);
Anders Carlsson0ccdf8d2009-09-27 00:38:53 +00002670 mangleTemplateParameter(PD->getIndex());
Anders Carlssond553f8c2009-09-21 01:21:10 +00002671 break;
2672 }
2673
2674 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002675
Anders Carlsson50755b02009-09-27 20:11:34 +00002676 break;
Anders Carlssond553f8c2009-09-21 01:21:10 +00002677 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002678
Douglas Gregorc7793c72011-01-15 01:15:58 +00002679 case Expr::SubstNonTypeTemplateParmPackExprClass:
John McCall68a51a72011-07-01 00:04:39 +00002680 // FIXME: not clear how to mangle this!
2681 // template <unsigned N...> class A {
2682 // template <class U...> void foo(U (&x)[N]...);
2683 // };
2684 Out << "_SUBSTPACK_";
Douglas Gregorc7793c72011-01-15 01:15:58 +00002685 break;
2686
John McCall865d4472009-11-19 22:55:06 +00002687 case Expr::DependentScopeDeclRefExprClass: {
2688 const DependentScopeDeclRefExpr *DRE = cast<DependentScopeDeclRefExpr>(E);
John McCall26a6ec72011-06-21 22:12:46 +00002689 mangleUnresolvedName(DRE->getQualifier(), 0, DRE->getDeclName(), Arity);
Douglas Gregor4b2ccfc2010-02-28 22:05:49 +00002690
John McCall26a6ec72011-06-21 22:12:46 +00002691 // All the <unresolved-name> productions end in a
2692 // base-unresolved-name, where <template-args> are just tacked
2693 // onto the end.
John McCall6dbce192010-08-20 00:17:19 +00002694 if (DRE->hasExplicitTemplateArgs())
2695 mangleTemplateArgs(DRE->getExplicitTemplateArgs());
Anders Carlsson50755b02009-09-27 20:11:34 +00002696 break;
2697 }
2698
John McCalld9307602010-04-09 22:54:09 +00002699 case Expr::CXXBindTemporaryExprClass:
2700 mangleExpression(cast<CXXBindTemporaryExpr>(E)->getSubExpr());
2701 break;
2702
John McCall4765fa02010-12-06 08:20:24 +00002703 case Expr::ExprWithCleanupsClass:
2704 mangleExpression(cast<ExprWithCleanups>(E)->getSubExpr(), Arity);
John McCalld9307602010-04-09 22:54:09 +00002705 break;
2706
John McCall1dd73832010-02-04 01:42:13 +00002707 case Expr::FloatingLiteralClass: {
2708 const FloatingLiteral *FL = cast<FloatingLiteral>(E);
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002709 Out << 'L';
John McCall1dd73832010-02-04 01:42:13 +00002710 mangleType(FL->getType());
John McCall0512e482010-07-14 04:20:34 +00002711 mangleFloat(FL->getValue());
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002712 Out << 'E';
John McCall1dd73832010-02-04 01:42:13 +00002713 break;
2714 }
2715
John McCallde810632010-04-09 21:48:08 +00002716 case Expr::CharacterLiteralClass:
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002717 Out << 'L';
John McCallde810632010-04-09 21:48:08 +00002718 mangleType(E->getType());
2719 Out << cast<CharacterLiteral>(E)->getValue();
2720 Out << 'E';
2721 break;
2722
2723 case Expr::CXXBoolLiteralExprClass:
2724 Out << "Lb";
2725 Out << (cast<CXXBoolLiteralExpr>(E)->getValue() ? '1' : '0');
2726 Out << 'E';
2727 break;
2728
John McCall0512e482010-07-14 04:20:34 +00002729 case Expr::IntegerLiteralClass: {
2730 llvm::APSInt Value(cast<IntegerLiteral>(E)->getValue());
2731 if (E->getType()->isSignedIntegerType())
2732 Value.setIsSigned(true);
2733 mangleIntegerLiteral(E->getType(), Value);
Anders Carlssone170ba72009-12-14 01:45:37 +00002734 break;
John McCall0512e482010-07-14 04:20:34 +00002735 }
2736
2737 case Expr::ImaginaryLiteralClass: {
2738 const ImaginaryLiteral *IE = cast<ImaginaryLiteral>(E);
2739 // Mangle as if a complex literal.
Nick Lewycky271b6652010-09-05 03:40:33 +00002740 // Proposal from David Vandevoorde, 2010.06.30.
John McCall0512e482010-07-14 04:20:34 +00002741 Out << 'L';
2742 mangleType(E->getType());
2743 if (const FloatingLiteral *Imag =
2744 dyn_cast<FloatingLiteral>(IE->getSubExpr())) {
2745 // Mangle a floating-point zero of the appropriate type.
2746 mangleFloat(llvm::APFloat(Imag->getValue().getSemantics()));
2747 Out << '_';
2748 mangleFloat(Imag->getValue());
2749 } else {
Nick Lewycky271b6652010-09-05 03:40:33 +00002750 Out << "0_";
John McCall0512e482010-07-14 04:20:34 +00002751 llvm::APSInt Value(cast<IntegerLiteral>(IE->getSubExpr())->getValue());
2752 if (IE->getSubExpr()->getType()->isSignedIntegerType())
2753 Value.setIsSigned(true);
2754 mangleNumber(Value);
2755 }
2756 Out << 'E';
2757 break;
2758 }
2759
2760 case Expr::StringLiteralClass: {
John McCall1658c392010-07-15 21:53:03 +00002761 // Revised proposal from David Vandervoorde, 2010.07.15.
John McCall0512e482010-07-14 04:20:34 +00002762 Out << 'L';
John McCall1658c392010-07-15 21:53:03 +00002763 assert(isa<ConstantArrayType>(E->getType()));
2764 mangleType(E->getType());
John McCall0512e482010-07-14 04:20:34 +00002765 Out << 'E';
2766 break;
2767 }
2768
2769 case Expr::GNUNullExprClass:
2770 // FIXME: should this really be mangled the same as nullptr?
2771 // fallthrough
2772
2773 case Expr::CXXNullPtrLiteralExprClass: {
2774 // Proposal from David Vandervoorde, 2010.06.30, as
2775 // modified by ABI list discussion.
2776 Out << "LDnE";
2777 break;
2778 }
Douglas Gregorbe230c32011-01-03 17:17:50 +00002779
2780 case Expr::PackExpansionExprClass:
2781 Out << "sp";
2782 mangleExpression(cast<PackExpansionExpr>(E)->getPattern());
2783 break;
Douglas Gregor2e774c42011-01-04 18:56:13 +00002784
2785 case Expr::SizeOfPackExprClass: {
Douglas Gregor2e774c42011-01-04 18:56:13 +00002786 Out << "sZ";
2787 const NamedDecl *Pack = cast<SizeOfPackExpr>(E)->getPack();
2788 if (const TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(Pack))
2789 mangleTemplateParameter(TTP->getIndex());
2790 else if (const NonTypeTemplateParmDecl *NTTP
2791 = dyn_cast<NonTypeTemplateParmDecl>(Pack))
2792 mangleTemplateParameter(NTTP->getIndex());
2793 else if (const TemplateTemplateParmDecl *TempTP
2794 = dyn_cast<TemplateTemplateParmDecl>(Pack))
2795 mangleTemplateParameter(TempTP->getIndex());
Douglas Gregor91832362011-07-12 07:03:48 +00002796 else
2797 mangleFunctionParam(cast<ParmVarDecl>(Pack));
Douglas Gregordfbbcf92011-03-03 02:20:19 +00002798 break;
Douglas Gregor2e774c42011-01-04 18:56:13 +00002799 }
Douglas Gregor03e80032011-06-21 17:03:29 +00002800
2801 case Expr::MaterializeTemporaryExprClass: {
2802 mangleExpression(cast<MaterializeTemporaryExpr>(E)->GetTemporaryExpr());
2803 break;
2804 }
Anders Carlssond553f8c2009-09-21 01:21:10 +00002805 }
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00002806}
2807
John McCallfb44de92011-05-01 22:35:37 +00002808/// Mangle an expression which refers to a parameter variable.
2809///
2810/// <expression> ::= <function-param>
2811/// <function-param> ::= fp <top-level CV-qualifiers> _ # L == 0, I == 0
2812/// <function-param> ::= fp <top-level CV-qualifiers>
2813/// <parameter-2 non-negative number> _ # L == 0, I > 0
2814/// <function-param> ::= fL <L-1 non-negative number>
2815/// p <top-level CV-qualifiers> _ # L > 0, I == 0
2816/// <function-param> ::= fL <L-1 non-negative number>
2817/// p <top-level CV-qualifiers>
2818/// <I-1 non-negative number> _ # L > 0, I > 0
2819///
2820/// L is the nesting depth of the parameter, defined as 1 if the
2821/// parameter comes from the innermost function prototype scope
2822/// enclosing the current context, 2 if from the next enclosing
2823/// function prototype scope, and so on, with one special case: if
2824/// we've processed the full parameter clause for the innermost
2825/// function type, then L is one less. This definition conveniently
2826/// makes it irrelevant whether a function's result type was written
2827/// trailing or leading, but is otherwise overly complicated; the
2828/// numbering was first designed without considering references to
2829/// parameter in locations other than return types, and then the
2830/// mangling had to be generalized without changing the existing
2831/// manglings.
2832///
2833/// I is the zero-based index of the parameter within its parameter
2834/// declaration clause. Note that the original ABI document describes
2835/// this using 1-based ordinals.
2836void CXXNameMangler::mangleFunctionParam(const ParmVarDecl *parm) {
2837 unsigned parmDepth = parm->getFunctionScopeDepth();
2838 unsigned parmIndex = parm->getFunctionScopeIndex();
2839
2840 // Compute 'L'.
2841 // parmDepth does not include the declaring function prototype.
2842 // FunctionTypeDepth does account for that.
2843 assert(parmDepth < FunctionTypeDepth.getDepth());
2844 unsigned nestingDepth = FunctionTypeDepth.getDepth() - parmDepth;
2845 if (FunctionTypeDepth.isInResultType())
2846 nestingDepth--;
2847
2848 if (nestingDepth == 0) {
2849 Out << "fp";
2850 } else {
2851 Out << "fL" << (nestingDepth - 1) << 'p';
2852 }
2853
2854 // Top-level qualifiers. We don't have to worry about arrays here,
2855 // because parameters declared as arrays should already have been
2856 // tranformed to have pointer type. FIXME: apparently these don't
2857 // get mangled if used as an rvalue of a known non-class type?
2858 assert(!parm->getType()->isArrayType()
2859 && "parameter's type is still an array type?");
2860 mangleQualifiers(parm->getType().getQualifiers());
2861
2862 // Parameter index.
2863 if (parmIndex != 0) {
2864 Out << (parmIndex - 1);
2865 }
2866 Out << '_';
2867}
2868
Anders Carlsson3ac86b52009-04-15 05:36:58 +00002869void CXXNameMangler::mangleCXXCtorType(CXXCtorType T) {
2870 // <ctor-dtor-name> ::= C1 # complete object constructor
2871 // ::= C2 # base object constructor
2872 // ::= C3 # complete object allocating constructor
2873 //
2874 switch (T) {
2875 case Ctor_Complete:
2876 Out << "C1";
2877 break;
2878 case Ctor_Base:
2879 Out << "C2";
2880 break;
2881 case Ctor_CompleteAllocating:
2882 Out << "C3";
2883 break;
2884 }
2885}
2886
Anders Carlsson27ae5362009-04-17 01:58:57 +00002887void CXXNameMangler::mangleCXXDtorType(CXXDtorType T) {
2888 // <ctor-dtor-name> ::= D0 # deleting destructor
2889 // ::= D1 # complete object destructor
2890 // ::= D2 # base object destructor
2891 //
2892 switch (T) {
2893 case Dtor_Deleting:
2894 Out << "D0";
2895 break;
2896 case Dtor_Complete:
2897 Out << "D1";
2898 break;
2899 case Dtor_Base:
2900 Out << "D2";
2901 break;
2902 }
2903}
2904
John McCall6dbce192010-08-20 00:17:19 +00002905void CXXNameMangler::mangleTemplateArgs(
Argyrios Kyrtzidisb0c3e092011-09-22 20:07:03 +00002906 const ASTTemplateArgumentListInfo &TemplateArgs) {
John McCall6dbce192010-08-20 00:17:19 +00002907 // <template-args> ::= I <template-arg>+ E
2908 Out << 'I';
John McCall4f4e4132011-05-04 01:45:19 +00002909 for (unsigned i = 0, e = TemplateArgs.NumTemplateArgs; i != e; ++i)
2910 mangleTemplateArg(0, TemplateArgs.getTemplateArgs()[i].getArgument());
John McCall6dbce192010-08-20 00:17:19 +00002911 Out << 'E';
2912}
2913
Douglas Gregor20f0cc72010-04-23 03:10:43 +00002914void CXXNameMangler::mangleTemplateArgs(TemplateName Template,
2915 const TemplateArgument *TemplateArgs,
2916 unsigned NumTemplateArgs) {
2917 if (TemplateDecl *TD = Template.getAsTemplateDecl())
2918 return mangleTemplateArgs(*TD->getTemplateParameters(), TemplateArgs,
2919 NumTemplateArgs);
Sean Huntc3021132010-05-05 15:23:54 +00002920
John McCall4f4e4132011-05-04 01:45:19 +00002921 mangleUnresolvedTemplateArgs(TemplateArgs, NumTemplateArgs);
2922}
2923
2924void CXXNameMangler::mangleUnresolvedTemplateArgs(const TemplateArgument *args,
2925 unsigned numArgs) {
Douglas Gregor20f0cc72010-04-23 03:10:43 +00002926 // <template-args> ::= I <template-arg>+ E
2927 Out << 'I';
John McCall4f4e4132011-05-04 01:45:19 +00002928 for (unsigned i = 0; i != numArgs; ++i)
2929 mangleTemplateArg(0, args[i]);
Douglas Gregor20f0cc72010-04-23 03:10:43 +00002930 Out << 'E';
2931}
2932
Rafael Espindolad9800722010-03-11 14:07:00 +00002933void CXXNameMangler::mangleTemplateArgs(const TemplateParameterList &PL,
2934 const TemplateArgumentList &AL) {
Anders Carlsson7a0ba872009-05-15 16:09:15 +00002935 // <template-args> ::= I <template-arg>+ E
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002936 Out << 'I';
Rafael Espindolad9800722010-03-11 14:07:00 +00002937 for (unsigned i = 0, e = AL.size(); i != e; ++i)
2938 mangleTemplateArg(PL.getParam(i), AL[i]);
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002939 Out << 'E';
Anders Carlsson7a0ba872009-05-15 16:09:15 +00002940}
2941
Rafael Espindolad9800722010-03-11 14:07:00 +00002942void CXXNameMangler::mangleTemplateArgs(const TemplateParameterList &PL,
2943 const TemplateArgument *TemplateArgs,
Anders Carlsson7624f212009-09-18 02:42:01 +00002944 unsigned NumTemplateArgs) {
2945 // <template-args> ::= I <template-arg>+ E
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002946 Out << 'I';
Daniel Dunbar7e0c1952009-11-21 09:17:15 +00002947 for (unsigned i = 0; i != NumTemplateArgs; ++i)
Rafael Espindolad9800722010-03-11 14:07:00 +00002948 mangleTemplateArg(PL.getParam(i), TemplateArgs[i]);
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002949 Out << 'E';
Anders Carlsson7624f212009-09-18 02:42:01 +00002950}
2951
Rafael Espindolad9800722010-03-11 14:07:00 +00002952void CXXNameMangler::mangleTemplateArg(const NamedDecl *P,
Douglas Gregorf1588662011-07-12 15:18:55 +00002953 TemplateArgument A) {
Mike Stump1eb44332009-09-09 15:08:12 +00002954 // <template-arg> ::= <type> # type or template
Anders Carlsson7a0ba872009-05-15 16:09:15 +00002955 // ::= X <expression> E # expression
2956 // ::= <expr-primary> # simple expressions
Douglas Gregor4fc48662011-01-13 16:39:34 +00002957 // ::= J <template-arg>* E # argument pack
Douglas Gregorf1588662011-07-12 15:18:55 +00002958 // ::= sp <expression> # pack expansion of (C++0x)
2959 if (!A.isInstantiationDependent() || A.isDependent())
2960 A = Context.getASTContext().getCanonicalTemplateArgument(A);
2961
Anders Carlsson7a0ba872009-05-15 16:09:15 +00002962 switch (A.getKind()) {
Douglas Gregorf90b27a2011-01-03 22:36:02 +00002963 case TemplateArgument::Null:
2964 llvm_unreachable("Cannot mangle NULL template argument");
2965
Anders Carlsson7a0ba872009-05-15 16:09:15 +00002966 case TemplateArgument::Type:
2967 mangleType(A.getAsType());
2968 break;
Anders Carlsson9e85c742009-12-23 19:30:55 +00002969 case TemplateArgument::Template:
John McCallb6f532e2010-07-14 06:43:17 +00002970 // This is mangled as <type>.
2971 mangleType(A.getAsTemplate());
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002972 break;
Douglas Gregora7fc9012011-01-05 18:58:31 +00002973 case TemplateArgument::TemplateExpansion:
Douglas Gregor4fc48662011-01-13 16:39:34 +00002974 // <type> ::= Dp <type> # pack expansion (C++0x)
Douglas Gregora7fc9012011-01-05 18:58:31 +00002975 Out << "Dp";
2976 mangleType(A.getAsTemplateOrTemplatePattern());
2977 break;
John McCall092beef2012-01-06 05:06:35 +00002978 case TemplateArgument::Expression: {
2979 // It's possible to end up with a DeclRefExpr here in certain
2980 // dependent cases, in which case we should mangle as a
2981 // declaration.
2982 const Expr *E = A.getAsExpr()->IgnoreParens();
2983 if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E)) {
2984 const ValueDecl *D = DRE->getDecl();
2985 if (isa<VarDecl>(D) || isa<FunctionDecl>(D)) {
2986 Out << "L";
2987 mangle(D, "_Z");
2988 Out << 'E';
2989 break;
2990 }
2991 }
2992
Anders Carlssond553f8c2009-09-21 01:21:10 +00002993 Out << 'X';
John McCall092beef2012-01-06 05:06:35 +00002994 mangleExpression(E);
Anders Carlssond553f8c2009-09-21 01:21:10 +00002995 Out << 'E';
2996 break;
John McCall092beef2012-01-06 05:06:35 +00002997 }
Anders Carlssone170ba72009-12-14 01:45:37 +00002998 case TemplateArgument::Integral:
2999 mangleIntegerLiteral(A.getIntegralType(), *A.getAsIntegral());
Anders Carlsson7a0ba872009-05-15 16:09:15 +00003000 break;
Daniel Dunbar7e0c1952009-11-21 09:17:15 +00003001 case TemplateArgument::Declaration: {
Douglas Gregor20f0cc72010-04-23 03:10:43 +00003002 assert(P && "Missing template parameter for declaration argument");
Daniel Dunbar7e0c1952009-11-21 09:17:15 +00003003 // <expr-primary> ::= L <mangled-name> E # external name
3004
Rafael Espindolad9800722010-03-11 14:07:00 +00003005 // Clang produces AST's where pointer-to-member-function expressions
Daniel Dunbar7e0c1952009-11-21 09:17:15 +00003006 // and pointer-to-function expressions are represented as a declaration not
Rafael Espindolad9800722010-03-11 14:07:00 +00003007 // an expression. We compensate for it here to produce the correct mangling.
3008 NamedDecl *D = cast<NamedDecl>(A.getAsDecl());
3009 const NonTypeTemplateParmDecl *Parameter = cast<NonTypeTemplateParmDecl>(P);
John McCallc0a45592011-04-24 08:43:07 +00003010 bool compensateMangling = !Parameter->getType()->isReferenceType();
Rafael Espindolad9800722010-03-11 14:07:00 +00003011 if (compensateMangling) {
3012 Out << 'X';
3013 mangleOperatorName(OO_Amp, 1);
3014 }
3015
Daniel Dunbar7e0c1952009-11-21 09:17:15 +00003016 Out << 'L';
3017 // References to external entities use the mangled name; if the name would
3018 // not normally be manged then mangle it as unqualified.
3019 //
3020 // FIXME: The ABI specifies that external names here should have _Z, but
3021 // gcc leaves this off.
Rafael Espindolad9800722010-03-11 14:07:00 +00003022 if (compensateMangling)
3023 mangle(D, "_Z");
3024 else
3025 mangle(D, "Z");
Daniel Dunbar7e0c1952009-11-21 09:17:15 +00003026 Out << 'E';
Rafael Espindolad9800722010-03-11 14:07:00 +00003027
3028 if (compensateMangling)
3029 Out << 'E';
3030
Daniel Dunbar7e0c1952009-11-21 09:17:15 +00003031 break;
3032 }
Douglas Gregorf90b27a2011-01-03 22:36:02 +00003033
3034 case TemplateArgument::Pack: {
3035 // Note: proposal by Mike Herrick on 12/20/10
3036 Out << 'J';
3037 for (TemplateArgument::pack_iterator PA = A.pack_begin(),
3038 PAEnd = A.pack_end();
3039 PA != PAEnd; ++PA)
3040 mangleTemplateArg(P, *PA);
3041 Out << 'E';
3042 }
Daniel Dunbar7e0c1952009-11-21 09:17:15 +00003043 }
Anders Carlsson7a0ba872009-05-15 16:09:15 +00003044}
3045
Anders Carlsson0ccdf8d2009-09-27 00:38:53 +00003046void CXXNameMangler::mangleTemplateParameter(unsigned Index) {
3047 // <template-param> ::= T_ # first template parameter
3048 // ::= T <parameter-2 non-negative number> _
3049 if (Index == 0)
3050 Out << "T_";
3051 else
3052 Out << 'T' << (Index - 1) << '_';
3053}
3054
John McCall68a51a72011-07-01 00:04:39 +00003055void CXXNameMangler::mangleExistingSubstitution(QualType type) {
3056 bool result = mangleSubstitution(type);
3057 assert(result && "no existing substitution for type");
3058 (void) result;
3059}
3060
3061void CXXNameMangler::mangleExistingSubstitution(TemplateName tname) {
3062 bool result = mangleSubstitution(tname);
3063 assert(result && "no existing substitution for template name");
3064 (void) result;
3065}
3066
Anders Carlsson76967372009-09-17 00:43:46 +00003067// <substitution> ::= S <seq-id> _
3068// ::= S_
Anders Carlsson6862fc72009-09-17 04:16:28 +00003069bool CXXNameMangler::mangleSubstitution(const NamedDecl *ND) {
Anders Carlssone7c8cb62009-09-26 20:53:44 +00003070 // Try one of the standard substitutions first.
3071 if (mangleStandardSubstitution(ND))
3072 return true;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00003073
Anders Carlsson433d1372009-11-07 04:26:04 +00003074 ND = cast<NamedDecl>(ND->getCanonicalDecl());
Anders Carlsson6862fc72009-09-17 04:16:28 +00003075 return mangleSubstitution(reinterpret_cast<uintptr_t>(ND));
3076}
3077
Douglas Gregor14795c82011-12-03 18:24:43 +00003078/// \brief Determine whether the given type has any qualifiers that are
3079/// relevant for substitutions.
3080static bool hasMangledSubstitutionQualifiers(QualType T) {
3081 Qualifiers Qs = T.getQualifiers();
3082 return Qs.getCVRQualifiers() || Qs.hasAddressSpace();
3083}
3084
Anders Carlsson76967372009-09-17 00:43:46 +00003085bool CXXNameMangler::mangleSubstitution(QualType T) {
Douglas Gregor14795c82011-12-03 18:24:43 +00003086 if (!hasMangledSubstitutionQualifiers(T)) {
Anders Carlssond99edc42009-09-26 03:55:37 +00003087 if (const RecordType *RT = T->getAs<RecordType>())
3088 return mangleSubstitution(RT->getDecl());
3089 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00003090
Anders Carlsson76967372009-09-17 00:43:46 +00003091 uintptr_t TypePtr = reinterpret_cast<uintptr_t>(T.getAsOpaquePtr());
3092
Anders Carlssond3a932a2009-09-17 03:53:28 +00003093 return mangleSubstitution(TypePtr);
3094}
3095
Douglas Gregor1e9268e2010-04-28 05:58:56 +00003096bool CXXNameMangler::mangleSubstitution(TemplateName Template) {
3097 if (TemplateDecl *TD = Template.getAsTemplateDecl())
3098 return mangleSubstitution(TD);
Sean Huntc3021132010-05-05 15:23:54 +00003099
Douglas Gregor1e9268e2010-04-28 05:58:56 +00003100 Template = Context.getASTContext().getCanonicalTemplateName(Template);
3101 return mangleSubstitution(
3102 reinterpret_cast<uintptr_t>(Template.getAsVoidPointer()));
3103}
3104
Anders Carlssond3a932a2009-09-17 03:53:28 +00003105bool CXXNameMangler::mangleSubstitution(uintptr_t Ptr) {
Benjamin Kramer35f59b62010-04-10 16:03:31 +00003106 llvm::DenseMap<uintptr_t, unsigned>::iterator I = Substitutions.find(Ptr);
Anders Carlsson76967372009-09-17 00:43:46 +00003107 if (I == Substitutions.end())
3108 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00003109
Anders Carlsson76967372009-09-17 00:43:46 +00003110 unsigned SeqID = I->second;
3111 if (SeqID == 0)
3112 Out << "S_";
3113 else {
3114 SeqID--;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00003115
Anders Carlsson76967372009-09-17 00:43:46 +00003116 // <seq-id> is encoded in base-36, using digits and upper case letters.
3117 char Buffer[10];
Benjamin Kramer35f59b62010-04-10 16:03:31 +00003118 char *BufferPtr = llvm::array_endof(Buffer);
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00003119
Anders Carlsson76967372009-09-17 00:43:46 +00003120 if (SeqID == 0) *--BufferPtr = '0';
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00003121
Anders Carlsson76967372009-09-17 00:43:46 +00003122 while (SeqID) {
3123 assert(BufferPtr > Buffer && "Buffer overflow!");
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00003124
John McCall6ab30e02010-06-09 07:26:17 +00003125 char c = static_cast<char>(SeqID % 36);
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00003126
Anders Carlsson76967372009-09-17 00:43:46 +00003127 *--BufferPtr = (c < 10 ? '0' + c : 'A' + c - 10);
3128 SeqID /= 36;
3129 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00003130
Benjamin Kramer35f59b62010-04-10 16:03:31 +00003131 Out << 'S'
Chris Lattner5f9e2722011-07-23 10:55:15 +00003132 << StringRef(BufferPtr, llvm::array_endof(Buffer)-BufferPtr)
Benjamin Kramer35f59b62010-04-10 16:03:31 +00003133 << '_';
Anders Carlsson76967372009-09-17 00:43:46 +00003134 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00003135
Anders Carlsson76967372009-09-17 00:43:46 +00003136 return true;
3137}
3138
Anders Carlssonf514b542009-09-27 00:12:57 +00003139static bool isCharType(QualType T) {
3140 if (T.isNull())
3141 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00003142
Anders Carlssonf514b542009-09-27 00:12:57 +00003143 return T->isSpecificBuiltinType(BuiltinType::Char_S) ||
3144 T->isSpecificBuiltinType(BuiltinType::Char_U);
3145}
3146
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00003147/// isCharSpecialization - Returns whether a given type is a template
Anders Carlssonf514b542009-09-27 00:12:57 +00003148/// specialization of a given name with a single argument of type char.
3149static bool isCharSpecialization(QualType T, const char *Name) {
3150 if (T.isNull())
3151 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00003152
Anders Carlssonf514b542009-09-27 00:12:57 +00003153 const RecordType *RT = T->getAs<RecordType>();
3154 if (!RT)
3155 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00003156
3157 const ClassTemplateSpecializationDecl *SD =
Anders Carlssonf514b542009-09-27 00:12:57 +00003158 dyn_cast<ClassTemplateSpecializationDecl>(RT->getDecl());
3159 if (!SD)
3160 return false;
3161
3162 if (!isStdNamespace(SD->getDeclContext()))
3163 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00003164
Anders Carlssonf514b542009-09-27 00:12:57 +00003165 const TemplateArgumentList &TemplateArgs = SD->getTemplateArgs();
3166 if (TemplateArgs.size() != 1)
3167 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00003168
Anders Carlssonf514b542009-09-27 00:12:57 +00003169 if (!isCharType(TemplateArgs[0].getAsType()))
3170 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00003171
Daniel Dunbar01eb9b92009-10-18 21:17:35 +00003172 return SD->getIdentifier()->getName() == Name;
Anders Carlssonf514b542009-09-27 00:12:57 +00003173}
3174
Anders Carlsson91f88602009-12-07 19:56:42 +00003175template <std::size_t StrLen>
Benjamin Kramer54353f42010-11-25 18:29:30 +00003176static bool isStreamCharSpecialization(const ClassTemplateSpecializationDecl*SD,
3177 const char (&Str)[StrLen]) {
Anders Carlsson91f88602009-12-07 19:56:42 +00003178 if (!SD->getIdentifier()->isStr(Str))
3179 return false;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00003180
Anders Carlsson91f88602009-12-07 19:56:42 +00003181 const TemplateArgumentList &TemplateArgs = SD->getTemplateArgs();
3182 if (TemplateArgs.size() != 2)
3183 return false;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00003184
Anders Carlsson91f88602009-12-07 19:56:42 +00003185 if (!isCharType(TemplateArgs[0].getAsType()))
3186 return false;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00003187
Anders Carlsson91f88602009-12-07 19:56:42 +00003188 if (!isCharSpecialization(TemplateArgs[1].getAsType(), "char_traits"))
3189 return false;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00003190
Anders Carlsson91f88602009-12-07 19:56:42 +00003191 return true;
3192}
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00003193
Anders Carlssone7c8cb62009-09-26 20:53:44 +00003194bool CXXNameMangler::mangleStandardSubstitution(const NamedDecl *ND) {
3195 // <substitution> ::= St # ::std::
Anders Carlsson8c031552009-09-26 23:10:05 +00003196 if (const NamespaceDecl *NS = dyn_cast<NamespaceDecl>(ND)) {
Anders Carlsson47846d22009-12-04 06:23:23 +00003197 if (isStd(NS)) {
Anders Carlsson8c031552009-09-26 23:10:05 +00003198 Out << "St";
3199 return true;
3200 }
3201 }
3202
3203 if (const ClassTemplateDecl *TD = dyn_cast<ClassTemplateDecl>(ND)) {
3204 if (!isStdNamespace(TD->getDeclContext()))
3205 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00003206
Anders Carlsson8c031552009-09-26 23:10:05 +00003207 // <substitution> ::= Sa # ::std::allocator
3208 if (TD->getIdentifier()->isStr("allocator")) {
3209 Out << "Sa";
3210 return true;
3211 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00003212
Anders Carlsson189d59c2009-09-26 23:14:39 +00003213 // <<substitution> ::= Sb # ::std::basic_string
3214 if (TD->getIdentifier()->isStr("basic_string")) {
3215 Out << "Sb";
3216 return true;
3217 }
Anders Carlsson8c031552009-09-26 23:10:05 +00003218 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00003219
3220 if (const ClassTemplateSpecializationDecl *SD =
Anders Carlssonf514b542009-09-27 00:12:57 +00003221 dyn_cast<ClassTemplateSpecializationDecl>(ND)) {
Eli Friedman5370ee22010-02-23 18:25:09 +00003222 if (!isStdNamespace(SD->getDeclContext()))
3223 return false;
3224
Anders Carlssonf514b542009-09-27 00:12:57 +00003225 // <substitution> ::= Ss # ::std::basic_string<char,
3226 // ::std::char_traits<char>,
3227 // ::std::allocator<char> >
3228 if (SD->getIdentifier()->isStr("basic_string")) {
3229 const TemplateArgumentList &TemplateArgs = SD->getTemplateArgs();
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00003230
Anders Carlssonf514b542009-09-27 00:12:57 +00003231 if (TemplateArgs.size() != 3)
3232 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00003233
Anders Carlssonf514b542009-09-27 00:12:57 +00003234 if (!isCharType(TemplateArgs[0].getAsType()))
3235 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00003236
Anders Carlssonf514b542009-09-27 00:12:57 +00003237 if (!isCharSpecialization(TemplateArgs[1].getAsType(), "char_traits"))
3238 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00003239
Anders Carlssonf514b542009-09-27 00:12:57 +00003240 if (!isCharSpecialization(TemplateArgs[2].getAsType(), "allocator"))
3241 return false;
3242
3243 Out << "Ss";
3244 return true;
3245 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00003246
Anders Carlsson91f88602009-12-07 19:56:42 +00003247 // <substitution> ::= Si # ::std::basic_istream<char,
3248 // ::std::char_traits<char> >
3249 if (isStreamCharSpecialization(SD, "basic_istream")) {
3250 Out << "Si";
3251 return true;
3252 }
3253
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00003254 // <substitution> ::= So # ::std::basic_ostream<char,
Anders Carlsson8f8fd8e2009-10-08 17:20:26 +00003255 // ::std::char_traits<char> >
Anders Carlsson91f88602009-12-07 19:56:42 +00003256 if (isStreamCharSpecialization(SD, "basic_ostream")) {
Anders Carlsson8f8fd8e2009-10-08 17:20:26 +00003257 Out << "So";
3258 return true;
3259 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00003260
Anders Carlsson91f88602009-12-07 19:56:42 +00003261 // <substitution> ::= Sd # ::std::basic_iostream<char,
3262 // ::std::char_traits<char> >
3263 if (isStreamCharSpecialization(SD, "basic_iostream")) {
3264 Out << "Sd";
3265 return true;
3266 }
Anders Carlssonf514b542009-09-27 00:12:57 +00003267 }
Anders Carlsson8c031552009-09-26 23:10:05 +00003268 return false;
Anders Carlssone7c8cb62009-09-26 20:53:44 +00003269}
3270
Anders Carlsson76967372009-09-17 00:43:46 +00003271void CXXNameMangler::addSubstitution(QualType T) {
Douglas Gregor14795c82011-12-03 18:24:43 +00003272 if (!hasMangledSubstitutionQualifiers(T)) {
Anders Carlssond99edc42009-09-26 03:55:37 +00003273 if (const RecordType *RT = T->getAs<RecordType>()) {
3274 addSubstitution(RT->getDecl());
3275 return;
3276 }
3277 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00003278
Anders Carlsson76967372009-09-17 00:43:46 +00003279 uintptr_t TypePtr = reinterpret_cast<uintptr_t>(T.getAsOpaquePtr());
Anders Carlssond3a932a2009-09-17 03:53:28 +00003280 addSubstitution(TypePtr);
3281}
3282
Douglas Gregor1e9268e2010-04-28 05:58:56 +00003283void CXXNameMangler::addSubstitution(TemplateName Template) {
3284 if (TemplateDecl *TD = Template.getAsTemplateDecl())
3285 return addSubstitution(TD);
Sean Huntc3021132010-05-05 15:23:54 +00003286
Douglas Gregor1e9268e2010-04-28 05:58:56 +00003287 Template = Context.getASTContext().getCanonicalTemplateName(Template);
3288 addSubstitution(reinterpret_cast<uintptr_t>(Template.getAsVoidPointer()));
3289}
3290
Anders Carlssond3a932a2009-09-17 03:53:28 +00003291void CXXNameMangler::addSubstitution(uintptr_t Ptr) {
Anders Carlssond3a932a2009-09-17 03:53:28 +00003292 assert(!Substitutions.count(Ptr) && "Substitution already exists!");
Anders Carlsson9d85b722010-06-02 04:29:50 +00003293 Substitutions[Ptr] = SeqID++;
Anders Carlsson76967372009-09-17 00:43:46 +00003294}
3295
Daniel Dunbar1b077112009-11-21 09:06:10 +00003296//
Mike Stump1eb44332009-09-09 15:08:12 +00003297
Daniel Dunbar1b077112009-11-21 09:06:10 +00003298/// \brief Mangles the name of the declaration D and emits that name to the
3299/// given output stream.
3300///
3301/// If the declaration D requires a mangled name, this routine will emit that
3302/// mangled name to \p os and return true. Otherwise, \p os will be unchanged
3303/// and this routine will return false. In this case, the caller should just
3304/// emit the identifier of the declaration (\c D->getIdentifier()) as its
3305/// name.
Peter Collingbourne14110472011-01-13 18:57:25 +00003306void ItaniumMangleContext::mangleName(const NamedDecl *D,
Chris Lattner5f9e2722011-07-23 10:55:15 +00003307 raw_ostream &Out) {
Daniel Dunbarc02ab4c2009-11-21 09:14:44 +00003308 assert((isa<FunctionDecl>(D) || isa<VarDecl>(D)) &&
3309 "Invalid mangleName() call, argument is not a variable or function!");
3310 assert(!isa<CXXConstructorDecl>(D) && !isa<CXXDestructorDecl>(D) &&
3311 "Invalid mangleName() call on 'structor decl!");
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00003312
Daniel Dunbar1b077112009-11-21 09:06:10 +00003313 PrettyStackTraceDecl CrashInfo(D, SourceLocation(),
3314 getASTContext().getSourceManager(),
3315 "Mangling declaration");
Mike Stump1eb44332009-09-09 15:08:12 +00003316
John McCallfb44de92011-05-01 22:35:37 +00003317 CXXNameMangler Mangler(*this, Out, D);
Daniel Dunbar94fd26d2009-11-21 09:06:22 +00003318 return Mangler.mangle(D);
Daniel Dunbar1b077112009-11-21 09:06:10 +00003319}
Mike Stump1eb44332009-09-09 15:08:12 +00003320
Peter Collingbourne14110472011-01-13 18:57:25 +00003321void ItaniumMangleContext::mangleCXXCtor(const CXXConstructorDecl *D,
3322 CXXCtorType Type,
Chris Lattner5f9e2722011-07-23 10:55:15 +00003323 raw_ostream &Out) {
Rafael Espindolac4850c22011-02-10 23:59:36 +00003324 CXXNameMangler Mangler(*this, Out, D, Type);
Daniel Dunbar77939c92009-11-21 09:06:31 +00003325 Mangler.mangle(D);
Daniel Dunbar1b077112009-11-21 09:06:10 +00003326}
Mike Stump1eb44332009-09-09 15:08:12 +00003327
Peter Collingbourne14110472011-01-13 18:57:25 +00003328void ItaniumMangleContext::mangleCXXDtor(const CXXDestructorDecl *D,
3329 CXXDtorType Type,
Chris Lattner5f9e2722011-07-23 10:55:15 +00003330 raw_ostream &Out) {
Rafael Espindolac4850c22011-02-10 23:59:36 +00003331 CXXNameMangler Mangler(*this, Out, D, Type);
Daniel Dunbar77939c92009-11-21 09:06:31 +00003332 Mangler.mangle(D);
Daniel Dunbar1b077112009-11-21 09:06:10 +00003333}
Mike Stumpf1216772009-07-31 18:25:34 +00003334
Peter Collingbourne14110472011-01-13 18:57:25 +00003335void ItaniumMangleContext::mangleThunk(const CXXMethodDecl *MD,
3336 const ThunkInfo &Thunk,
Chris Lattner5f9e2722011-07-23 10:55:15 +00003337 raw_ostream &Out) {
Anders Carlsson19879c92010-03-23 17:17:29 +00003338 // <special-name> ::= T <call-offset> <base encoding>
3339 // # base is the nominal target function of thunk
3340 // <special-name> ::= Tc <call-offset> <call-offset> <base encoding>
3341 // # base is the nominal target function of thunk
3342 // # first call-offset is 'this' adjustment
3343 // # second call-offset is result adjustment
Sean Huntc3021132010-05-05 15:23:54 +00003344
Anders Carlsson19879c92010-03-23 17:17:29 +00003345 assert(!isa<CXXDestructorDecl>(MD) &&
3346 "Use mangleCXXDtor for destructor decls!");
Rafael Espindolac4850c22011-02-10 23:59:36 +00003347 CXXNameMangler Mangler(*this, Out);
Anders Carlsson19879c92010-03-23 17:17:29 +00003348 Mangler.getStream() << "_ZT";
3349 if (!Thunk.Return.isEmpty())
3350 Mangler.getStream() << 'c';
Sean Huntc3021132010-05-05 15:23:54 +00003351
Anders Carlsson19879c92010-03-23 17:17:29 +00003352 // Mangle the 'this' pointer adjustment.
3353 Mangler.mangleCallOffset(Thunk.This.NonVirtual, Thunk.This.VCallOffsetOffset);
Sean Huntc3021132010-05-05 15:23:54 +00003354
Anders Carlsson19879c92010-03-23 17:17:29 +00003355 // Mangle the return pointer adjustment if there is one.
3356 if (!Thunk.Return.isEmpty())
3357 Mangler.mangleCallOffset(Thunk.Return.NonVirtual,
3358 Thunk.Return.VBaseOffsetOffset);
Sean Huntc3021132010-05-05 15:23:54 +00003359
Anders Carlsson19879c92010-03-23 17:17:29 +00003360 Mangler.mangleFunctionEncoding(MD);
3361}
3362
Sean Huntc3021132010-05-05 15:23:54 +00003363void
Peter Collingbourne14110472011-01-13 18:57:25 +00003364ItaniumMangleContext::mangleCXXDtorThunk(const CXXDestructorDecl *DD,
3365 CXXDtorType Type,
3366 const ThisAdjustment &ThisAdjustment,
Chris Lattner5f9e2722011-07-23 10:55:15 +00003367 raw_ostream &Out) {
Anders Carlsson19879c92010-03-23 17:17:29 +00003368 // <special-name> ::= T <call-offset> <base encoding>
3369 // # base is the nominal target function of thunk
Rafael Espindolac4850c22011-02-10 23:59:36 +00003370 CXXNameMangler Mangler(*this, Out, DD, Type);
Anders Carlsson19879c92010-03-23 17:17:29 +00003371 Mangler.getStream() << "_ZT";
3372
3373 // Mangle the 'this' pointer adjustment.
Sean Huntc3021132010-05-05 15:23:54 +00003374 Mangler.mangleCallOffset(ThisAdjustment.NonVirtual,
Anders Carlsson19879c92010-03-23 17:17:29 +00003375 ThisAdjustment.VCallOffsetOffset);
3376
3377 Mangler.mangleFunctionEncoding(DD);
3378}
3379
Daniel Dunbarc0747712009-11-21 09:12:13 +00003380/// mangleGuardVariable - Returns the mangled name for a guard variable
3381/// for the passed in VarDecl.
Peter Collingbourne14110472011-01-13 18:57:25 +00003382void ItaniumMangleContext::mangleItaniumGuardVariable(const VarDecl *D,
Chris Lattner5f9e2722011-07-23 10:55:15 +00003383 raw_ostream &Out) {
Daniel Dunbarc0747712009-11-21 09:12:13 +00003384 // <special-name> ::= GV <object name> # Guard variable for one-time
3385 // # initialization
Rafael Espindolac4850c22011-02-10 23:59:36 +00003386 CXXNameMangler Mangler(*this, Out);
Daniel Dunbarc0747712009-11-21 09:12:13 +00003387 Mangler.getStream() << "_ZGV";
3388 Mangler.mangleName(D);
3389}
3390
Peter Collingbourne14110472011-01-13 18:57:25 +00003391void ItaniumMangleContext::mangleReferenceTemporary(const VarDecl *D,
Chris Lattner5f9e2722011-07-23 10:55:15 +00003392 raw_ostream &Out) {
Anders Carlsson715edf22010-06-26 16:09:40 +00003393 // We match the GCC mangling here.
3394 // <special-name> ::= GR <object name>
Rafael Espindolac4850c22011-02-10 23:59:36 +00003395 CXXNameMangler Mangler(*this, Out);
Anders Carlsson715edf22010-06-26 16:09:40 +00003396 Mangler.getStream() << "_ZGR";
3397 Mangler.mangleName(D);
3398}
3399
Peter Collingbourne14110472011-01-13 18:57:25 +00003400void ItaniumMangleContext::mangleCXXVTable(const CXXRecordDecl *RD,
Chris Lattner5f9e2722011-07-23 10:55:15 +00003401 raw_ostream &Out) {
Daniel Dunbarc0747712009-11-21 09:12:13 +00003402 // <special-name> ::= TV <type> # virtual table
Rafael Espindolac4850c22011-02-10 23:59:36 +00003403 CXXNameMangler Mangler(*this, Out);
Daniel Dunbarc0747712009-11-21 09:12:13 +00003404 Mangler.getStream() << "_ZTV";
Douglas Gregor1b12a3b2010-05-26 05:11:13 +00003405 Mangler.mangleNameOrStandardSubstitution(RD);
Daniel Dunbar1b077112009-11-21 09:06:10 +00003406}
Mike Stump82d75b02009-11-10 01:58:37 +00003407
Peter Collingbourne14110472011-01-13 18:57:25 +00003408void ItaniumMangleContext::mangleCXXVTT(const CXXRecordDecl *RD,
Chris Lattner5f9e2722011-07-23 10:55:15 +00003409 raw_ostream &Out) {
Daniel Dunbarc0747712009-11-21 09:12:13 +00003410 // <special-name> ::= TT <type> # VTT structure
Rafael Espindolac4850c22011-02-10 23:59:36 +00003411 CXXNameMangler Mangler(*this, Out);
Daniel Dunbarc0747712009-11-21 09:12:13 +00003412 Mangler.getStream() << "_ZTT";
Douglas Gregor1b12a3b2010-05-26 05:11:13 +00003413 Mangler.mangleNameOrStandardSubstitution(RD);
Daniel Dunbar1b077112009-11-21 09:06:10 +00003414}
Mike Stumpab3f7e92009-11-10 01:41:59 +00003415
Peter Collingbourne14110472011-01-13 18:57:25 +00003416void ItaniumMangleContext::mangleCXXCtorVTable(const CXXRecordDecl *RD,
3417 int64_t Offset,
3418 const CXXRecordDecl *Type,
Chris Lattner5f9e2722011-07-23 10:55:15 +00003419 raw_ostream &Out) {
Daniel Dunbarc0747712009-11-21 09:12:13 +00003420 // <special-name> ::= TC <type> <offset number> _ <base type>
Rafael Espindolac4850c22011-02-10 23:59:36 +00003421 CXXNameMangler Mangler(*this, Out);
Daniel Dunbarc0747712009-11-21 09:12:13 +00003422 Mangler.getStream() << "_ZTC";
Douglas Gregor1b12a3b2010-05-26 05:11:13 +00003423 Mangler.mangleNameOrStandardSubstitution(RD);
Daniel Dunbarc0747712009-11-21 09:12:13 +00003424 Mangler.getStream() << Offset;
Benjamin Kramer35f59b62010-04-10 16:03:31 +00003425 Mangler.getStream() << '_';
Douglas Gregor1b12a3b2010-05-26 05:11:13 +00003426 Mangler.mangleNameOrStandardSubstitution(Type);
Daniel Dunbar1b077112009-11-21 09:06:10 +00003427}
Mike Stump738f8c22009-07-31 23:15:31 +00003428
Peter Collingbourne14110472011-01-13 18:57:25 +00003429void ItaniumMangleContext::mangleCXXRTTI(QualType Ty,
Chris Lattner5f9e2722011-07-23 10:55:15 +00003430 raw_ostream &Out) {
Daniel Dunbarc0747712009-11-21 09:12:13 +00003431 // <special-name> ::= TI <type> # typeinfo structure
Douglas Gregor154fe982009-12-23 22:04:40 +00003432 assert(!Ty.hasQualifiers() && "RTTI info cannot have top-level qualifiers");
Rafael Espindolac4850c22011-02-10 23:59:36 +00003433 CXXNameMangler Mangler(*this, Out);
Daniel Dunbarc0747712009-11-21 09:12:13 +00003434 Mangler.getStream() << "_ZTI";
3435 Mangler.mangleType(Ty);
Daniel Dunbar1b077112009-11-21 09:06:10 +00003436}
Mike Stump67795982009-11-14 00:14:13 +00003437
Peter Collingbourne14110472011-01-13 18:57:25 +00003438void ItaniumMangleContext::mangleCXXRTTIName(QualType Ty,
Chris Lattner5f9e2722011-07-23 10:55:15 +00003439 raw_ostream &Out) {
Daniel Dunbarc0747712009-11-21 09:12:13 +00003440 // <special-name> ::= TS <type> # typeinfo name (null terminated byte string)
Rafael Espindolac4850c22011-02-10 23:59:36 +00003441 CXXNameMangler Mangler(*this, Out);
Daniel Dunbarc0747712009-11-21 09:12:13 +00003442 Mangler.getStream() << "_ZTS";
3443 Mangler.mangleType(Ty);
Mike Stumpf1216772009-07-31 18:25:34 +00003444}
Peter Collingbourne14110472011-01-13 18:57:25 +00003445
3446MangleContext *clang::createItaniumMangleContext(ASTContext &Context,
David Blaikied6471f72011-09-25 23:23:43 +00003447 DiagnosticsEngine &Diags) {
Peter Collingbourne14110472011-01-13 18:57:25 +00003448 return new ItaniumMangleContext(Context, Diags);
3449}