blob: 6205988c9174892c18fdea1b0b838cae9225338c [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"
Peter Collingbourne14110472011-01-13 18:57:25 +000024#include "clang/Basic/ABI.h"
Douglas Gregor6ec36682009-02-18 23:53:56 +000025#include "clang/Basic/SourceManager.h"
Anders Carlssonc4355b62009-10-07 01:45:02 +000026#include "llvm/ADT/StringExtras.h"
Douglas Gregor5f2bfd42009-02-13 00:10:09 +000027#include "llvm/Support/raw_ostream.h"
John McCallefe6aee2009-09-05 07:56:18 +000028#include "llvm/Support/ErrorHandling.h"
Anders Carlssonf98574b2010-02-05 07:31:37 +000029
30#define MANGLE_CHECKER 0
31
32#if MANGLE_CHECKER
33#include <cxxabi.h>
34#endif
35
Douglas Gregor5f2bfd42009-02-13 00:10:09 +000036using namespace clang;
Charles Davis685b1d92010-05-26 18:25:27 +000037
Douglas Gregor5f2bfd42009-02-13 00:10:09 +000038namespace {
Fariborz Jahanian57058532010-03-03 19:41:08 +000039
John McCall82b7d7b2010-10-18 21:28:44 +000040static const CXXRecordDecl *GetLocalClassDecl(const NamedDecl *ND) {
41 const DeclContext *DC = dyn_cast<DeclContext>(ND);
42 if (!DC)
43 DC = ND->getDeclContext();
44 while (!DC->isNamespace() && !DC->isTranslationUnit()) {
45 if (isa<FunctionDecl>(DC->getParent()))
46 return dyn_cast<CXXRecordDecl>(DC);
47 DC = DC->getParent();
Fariborz Jahanian57058532010-03-03 19:41:08 +000048 }
49 return 0;
50}
51
Anders Carlsson7e120032009-11-24 05:36:32 +000052static const CXXMethodDecl *getStructor(const CXXMethodDecl *MD) {
53 assert((isa<CXXConstructorDecl>(MD) || isa<CXXDestructorDecl>(MD)) &&
54 "Passed in decl is not a ctor or dtor!");
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +000055
Anders Carlsson7e120032009-11-24 05:36:32 +000056 if (const TemplateDecl *TD = MD->getPrimaryTemplate()) {
57 MD = cast<CXXMethodDecl>(TD->getTemplatedDecl());
58
59 assert((isa<CXXConstructorDecl>(MD) || isa<CXXDestructorDecl>(MD)) &&
60 "Templated decl is not a ctor or dtor!");
61 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +000062
Anders Carlsson7e120032009-11-24 05:36:32 +000063 return MD;
64}
John McCall1dd73832010-02-04 01:42:13 +000065
66static const unsigned UnknownArity = ~0U;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +000067
Peter Collingbourne14110472011-01-13 18:57:25 +000068class ItaniumMangleContext : public MangleContext {
69 llvm::DenseMap<const TagDecl *, uint64_t> AnonStructIds;
70 unsigned Discriminator;
71 llvm::DenseMap<const NamedDecl*, unsigned> Uniquifier;
72
73public:
74 explicit ItaniumMangleContext(ASTContext &Context,
75 Diagnostic &Diags)
76 : MangleContext(Context, Diags) { }
77
78 uint64_t getAnonymousStructId(const TagDecl *TD) {
79 std::pair<llvm::DenseMap<const TagDecl *,
80 uint64_t>::iterator, bool> Result =
81 AnonStructIds.insert(std::make_pair(TD, AnonStructIds.size()));
82 return Result.first->second;
83 }
84
85 void startNewFunction() {
86 MangleContext::startNewFunction();
87 mangleInitDiscriminator();
88 }
89
90 /// @name Mangler Entry Points
91 /// @{
92
93 bool shouldMangleDeclName(const NamedDecl *D);
Rafael Espindola0e376a02011-02-11 01:41:00 +000094 void mangleName(const NamedDecl *D, llvm::raw_ostream &);
Peter Collingbourne14110472011-01-13 18:57:25 +000095 void mangleThunk(const CXXMethodDecl *MD,
96 const ThunkInfo &Thunk,
97 llvm::SmallVectorImpl<char> &);
98 void mangleCXXDtorThunk(const CXXDestructorDecl *DD, CXXDtorType Type,
99 const ThisAdjustment &ThisAdjustment,
100 llvm::SmallVectorImpl<char> &);
101 void mangleReferenceTemporary(const VarDecl *D,
102 llvm::SmallVectorImpl<char> &);
103 void mangleCXXVTable(const CXXRecordDecl *RD,
104 llvm::SmallVectorImpl<char> &);
105 void mangleCXXVTT(const CXXRecordDecl *RD,
106 llvm::SmallVectorImpl<char> &);
107 void mangleCXXCtorVTable(const CXXRecordDecl *RD, int64_t Offset,
108 const CXXRecordDecl *Type,
109 llvm::SmallVectorImpl<char> &);
110 void mangleCXXRTTI(QualType T, llvm::SmallVectorImpl<char> &);
111 void mangleCXXRTTIName(QualType T, llvm::SmallVectorImpl<char> &);
112 void mangleCXXCtor(const CXXConstructorDecl *D, CXXCtorType Type,
Rafael Espindola0e376a02011-02-11 01:41:00 +0000113 llvm::raw_ostream &);
Peter Collingbourne14110472011-01-13 18:57:25 +0000114 void mangleCXXDtor(const CXXDestructorDecl *D, CXXDtorType Type,
Rafael Espindola0e376a02011-02-11 01:41:00 +0000115 llvm::raw_ostream &);
Peter Collingbourne14110472011-01-13 18:57:25 +0000116
117 void mangleItaniumGuardVariable(const VarDecl *D,
118 llvm::SmallVectorImpl<char> &);
119
120 void mangleInitDiscriminator() {
121 Discriminator = 0;
122 }
123
124 bool getNextDiscriminator(const NamedDecl *ND, unsigned &disc) {
125 unsigned &discriminator = Uniquifier[ND];
126 if (!discriminator)
127 discriminator = ++Discriminator;
128 if (discriminator == 1)
129 return false;
130 disc = discriminator-2;
131 return true;
132 }
133 /// @}
134};
135
Daniel Dunbar1b077112009-11-21 09:06:10 +0000136/// CXXNameMangler - Manage the mangling of a single name.
Daniel Dunbarc0747712009-11-21 09:12:13 +0000137class CXXNameMangler {
Peter Collingbourne14110472011-01-13 18:57:25 +0000138 ItaniumMangleContext &Context;
Rafael Espindola0e376a02011-02-11 01:41:00 +0000139 llvm::raw_ostream &Out;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000140
Daniel Dunbar1b077112009-11-21 09:06:10 +0000141 const CXXMethodDecl *Structor;
142 unsigned StructorType;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000143
Anders Carlsson9d85b722010-06-02 04:29:50 +0000144 /// SeqID - The next subsitution sequence number.
145 unsigned SeqID;
146
Daniel Dunbar1b077112009-11-21 09:06:10 +0000147 llvm::DenseMap<uintptr_t, unsigned> Substitutions;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000148
John McCall1dd73832010-02-04 01:42:13 +0000149 ASTContext &getASTContext() const { return Context.getASTContext(); }
150
Daniel Dunbarc0747712009-11-21 09:12:13 +0000151public:
Rafael Espindola0e376a02011-02-11 01:41:00 +0000152 CXXNameMangler(ItaniumMangleContext &C, llvm::raw_ostream &Out_)
Rafael Espindolac4850c22011-02-10 23:59:36 +0000153 : Context(C), Out(Out_), Structor(0), StructorType(0), SeqID(0) { }
Rafael Espindola0e376a02011-02-11 01:41:00 +0000154 CXXNameMangler(ItaniumMangleContext &C, llvm::raw_ostream &Out_,
Daniel Dunbar77939c92009-11-21 09:06:31 +0000155 const CXXConstructorDecl *D, CXXCtorType Type)
Rafael Espindolac4850c22011-02-10 23:59:36 +0000156 : Context(C), Out(Out_), Structor(getStructor(D)), StructorType(Type),
Anders Carlsson9d85b722010-06-02 04:29:50 +0000157 SeqID(0) { }
Rafael Espindola0e376a02011-02-11 01:41:00 +0000158 CXXNameMangler(ItaniumMangleContext &C, llvm::raw_ostream &Out_,
Daniel Dunbar77939c92009-11-21 09:06:31 +0000159 const CXXDestructorDecl *D, CXXDtorType Type)
Rafael Espindolac4850c22011-02-10 23:59:36 +0000160 : Context(C), Out(Out_), Structor(getStructor(D)), StructorType(Type),
Anders Carlsson9d85b722010-06-02 04:29:50 +0000161 SeqID(0) { }
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000162
Anders Carlssonf98574b2010-02-05 07:31:37 +0000163#if MANGLE_CHECKER
164 ~CXXNameMangler() {
165 if (Out.str()[0] == '\01')
166 return;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000167
Anders Carlssonf98574b2010-02-05 07:31:37 +0000168 int status = 0;
169 char *result = abi::__cxa_demangle(Out.str().str().c_str(), 0, 0, &status);
170 assert(status == 0 && "Could not demangle mangled name!");
171 free(result);
172 }
173#endif
Rafael Espindola0e376a02011-02-11 01:41:00 +0000174 llvm::raw_ostream &getStream() { return Out; }
Daniel Dunbarc0747712009-11-21 09:12:13 +0000175
Daniel Dunbar7e0c1952009-11-21 09:17:15 +0000176 void mangle(const NamedDecl *D, llvm::StringRef Prefix = "_Z");
Anders Carlsson19879c92010-03-23 17:17:29 +0000177 void mangleCallOffset(int64_t NonVirtual, int64_t Virtual);
John McCall0512e482010-07-14 04:20:34 +0000178 void mangleNumber(const llvm::APSInt &I);
Anders Carlssona94822e2009-11-26 02:32:05 +0000179 void mangleNumber(int64_t Number);
John McCall0512e482010-07-14 04:20:34 +0000180 void mangleFloat(const llvm::APFloat &F);
Daniel Dunbarc0747712009-11-21 09:12:13 +0000181 void mangleFunctionEncoding(const FunctionDecl *FD);
182 void mangleName(const NamedDecl *ND);
183 void mangleType(QualType T);
Douglas Gregor1b12a3b2010-05-26 05:11:13 +0000184 void mangleNameOrStandardSubstitution(const NamedDecl *ND);
185
Daniel Dunbarc0747712009-11-21 09:12:13 +0000186private:
Daniel Dunbar1b077112009-11-21 09:06:10 +0000187 bool mangleSubstitution(const NamedDecl *ND);
188 bool mangleSubstitution(QualType T);
Douglas Gregor1e9268e2010-04-28 05:58:56 +0000189 bool mangleSubstitution(TemplateName Template);
Daniel Dunbar1b077112009-11-21 09:06:10 +0000190 bool mangleSubstitution(uintptr_t Ptr);
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000191
Daniel Dunbar1b077112009-11-21 09:06:10 +0000192 bool mangleStandardSubstitution(const NamedDecl *ND);
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000193
Daniel Dunbar1b077112009-11-21 09:06:10 +0000194 void addSubstitution(const NamedDecl *ND) {
195 ND = cast<NamedDecl>(ND->getCanonicalDecl());
Anders Carlsson433d1372009-11-07 04:26:04 +0000196
Daniel Dunbar1b077112009-11-21 09:06:10 +0000197 addSubstitution(reinterpret_cast<uintptr_t>(ND));
198 }
199 void addSubstitution(QualType T);
Douglas Gregor1e9268e2010-04-28 05:58:56 +0000200 void addSubstitution(TemplateName Template);
Daniel Dunbar1b077112009-11-21 09:06:10 +0000201 void addSubstitution(uintptr_t Ptr);
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000202
John McCall1dd73832010-02-04 01:42:13 +0000203 void mangleUnresolvedScope(NestedNameSpecifier *Qualifier);
204 void mangleUnresolvedName(NestedNameSpecifier *Qualifier,
205 DeclarationName Name,
206 unsigned KnownArity = UnknownArity);
207
Daniel Dunbar1b077112009-11-21 09:06:10 +0000208 void mangleName(const TemplateDecl *TD,
209 const TemplateArgument *TemplateArgs,
210 unsigned NumTemplateArgs);
John McCall1dd73832010-02-04 01:42:13 +0000211 void mangleUnqualifiedName(const NamedDecl *ND) {
212 mangleUnqualifiedName(ND, ND->getDeclName(), UnknownArity);
213 }
214 void mangleUnqualifiedName(const NamedDecl *ND, DeclarationName Name,
215 unsigned KnownArity);
Daniel Dunbar1b077112009-11-21 09:06:10 +0000216 void mangleUnscopedName(const NamedDecl *ND);
217 void mangleUnscopedTemplateName(const TemplateDecl *ND);
Douglas Gregor1e9268e2010-04-28 05:58:56 +0000218 void mangleUnscopedTemplateName(TemplateName);
Daniel Dunbar1b077112009-11-21 09:06:10 +0000219 void mangleSourceName(const IdentifierInfo *II);
220 void mangleLocalName(const NamedDecl *ND);
Fariborz Jahanian57058532010-03-03 19:41:08 +0000221 void mangleNestedName(const NamedDecl *ND, const DeclContext *DC,
222 bool NoFunction=false);
Daniel Dunbar1b077112009-11-21 09:06:10 +0000223 void mangleNestedName(const TemplateDecl *TD,
224 const TemplateArgument *TemplateArgs,
225 unsigned NumTemplateArgs);
Fariborz Jahanian57058532010-03-03 19:41:08 +0000226 void manglePrefix(const DeclContext *DC, bool NoFunction=false);
Daniel Dunbar1b077112009-11-21 09:06:10 +0000227 void mangleTemplatePrefix(const TemplateDecl *ND);
Douglas Gregor20f0cc72010-04-23 03:10:43 +0000228 void mangleTemplatePrefix(TemplateName Template);
Daniel Dunbar1b077112009-11-21 09:06:10 +0000229 void mangleOperatorName(OverloadedOperatorKind OO, unsigned Arity);
230 void mangleQualifiers(Qualifiers Quals);
Douglas Gregor0a9a6d62011-01-26 17:36:28 +0000231 void mangleRefQualifier(RefQualifierKind RefQualifier);
John McCallefe6aee2009-09-05 07:56:18 +0000232
Anders Carlsson7b06f6c2009-12-10 03:14:39 +0000233 void mangleObjCMethodName(const ObjCMethodDecl *MD);
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000234
Daniel Dunbar1b077112009-11-21 09:06:10 +0000235 // Declare manglers for every type class.
John McCallefe6aee2009-09-05 07:56:18 +0000236#define ABSTRACT_TYPE(CLASS, PARENT)
237#define NON_CANONICAL_TYPE(CLASS, PARENT)
238#define TYPE(CLASS, PARENT) void mangleType(const CLASS##Type *T);
239#include "clang/AST/TypeNodes.def"
240
Daniel Dunbar1b077112009-11-21 09:06:10 +0000241 void mangleType(const TagType*);
John McCallb6f532e2010-07-14 06:43:17 +0000242 void mangleType(TemplateName);
Daniel Dunbar1b077112009-11-21 09:06:10 +0000243 void mangleBareFunctionType(const FunctionType *T,
244 bool MangleReturnType);
Bob Wilson57147a82010-11-16 00:32:18 +0000245 void mangleNeonVectorType(const VectorType *T);
Anders Carlssone170ba72009-12-14 01:45:37 +0000246
247 void mangleIntegerLiteral(QualType T, const llvm::APSInt &Value);
John McCall2f27bf82010-02-04 02:56:29 +0000248 void mangleMemberExpr(const Expr *Base, bool IsArrow,
249 NestedNameSpecifier *Qualifier,
250 DeclarationName Name,
251 unsigned KnownArity);
John McCall5e1e89b2010-08-18 19:18:59 +0000252 void mangleExpression(const Expr *E, unsigned Arity = UnknownArity);
Daniel Dunbar1b077112009-11-21 09:06:10 +0000253 void mangleCXXCtorType(CXXCtorType T);
254 void mangleCXXDtorType(CXXDtorType T);
Mike Stump1eb44332009-09-09 15:08:12 +0000255
John McCall6dbce192010-08-20 00:17:19 +0000256 void mangleTemplateArgs(const ExplicitTemplateArgumentList &TemplateArgs);
Douglas Gregor20f0cc72010-04-23 03:10:43 +0000257 void mangleTemplateArgs(TemplateName Template,
258 const TemplateArgument *TemplateArgs,
Sean Huntc3021132010-05-05 15:23:54 +0000259 unsigned NumTemplateArgs);
Rafael Espindolad9800722010-03-11 14:07:00 +0000260 void mangleTemplateArgs(const TemplateParameterList &PL,
261 const TemplateArgument *TemplateArgs,
Daniel Dunbar1b077112009-11-21 09:06:10 +0000262 unsigned NumTemplateArgs);
Rafael Espindolad9800722010-03-11 14:07:00 +0000263 void mangleTemplateArgs(const TemplateParameterList &PL,
264 const TemplateArgumentList &AL);
265 void mangleTemplateArg(const NamedDecl *P, const TemplateArgument &A);
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000266
Daniel Dunbar1b077112009-11-21 09:06:10 +0000267 void mangleTemplateParameter(unsigned Index);
268};
Peter Collingbourne14110472011-01-13 18:57:25 +0000269
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000270}
271
Anders Carlsson43f17402009-04-02 15:51:53 +0000272static bool isInCLinkageSpecification(const Decl *D) {
Douglas Gregor457e2812009-10-28 16:31:34 +0000273 D = D->getCanonicalDecl();
Mike Stump1eb44332009-09-09 15:08:12 +0000274 for (const DeclContext *DC = D->getDeclContext();
Anders Carlsson43f17402009-04-02 15:51:53 +0000275 !DC->isTranslationUnit(); DC = DC->getParent()) {
Mike Stump1eb44332009-09-09 15:08:12 +0000276 if (const LinkageSpecDecl *Linkage = dyn_cast<LinkageSpecDecl>(DC))
Anders Carlsson43f17402009-04-02 15:51:53 +0000277 return Linkage->getLanguage() == LinkageSpecDecl::lang_c;
278 }
Mike Stump1eb44332009-09-09 15:08:12 +0000279
Anders Carlsson43f17402009-04-02 15:51:53 +0000280 return false;
281}
282
Peter Collingbourne14110472011-01-13 18:57:25 +0000283bool ItaniumMangleContext::shouldMangleDeclName(const NamedDecl *D) {
Daniel Dunbarf981bf82009-11-21 09:14:52 +0000284 // In C, functions with no attributes never need to be mangled. Fastpath them.
285 if (!getASTContext().getLangOptions().CPlusPlus && !D->hasAttrs())
286 return false;
287
288 // Any decl can be declared with __asm("foo") on it, and this takes precedence
289 // over all other naming in the .o file.
290 if (D->hasAttr<AsmLabelAttr>())
291 return true;
292
Mike Stump141c5af2009-09-02 00:25:38 +0000293 // Clang's "overloadable" attribute extension to C/C++ implies name mangling
Anders Carlssona1e16222009-11-07 07:15:03 +0000294 // (always) as does passing a C++ member function and a function
295 // whose name is not a simple identifier.
Daniel Dunbarf981bf82009-11-21 09:14:52 +0000296 const FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
297 if (FD && (FD->hasAttr<OverloadableAttr>() || isa<CXXMethodDecl>(FD) ||
298 !FD->getDeclName().isIdentifier()))
299 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000300
Daniel Dunbarf981bf82009-11-21 09:14:52 +0000301 // Otherwise, no mangling is done outside C++ mode.
302 if (!getASTContext().getLangOptions().CPlusPlus)
303 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000304
Sean Hunt31455252010-01-24 03:04:27 +0000305 // Variables at global scope with non-internal linkage are not mangled
Eli Friedman7facf842009-12-02 20:32:49 +0000306 if (!FD) {
307 const DeclContext *DC = D->getDeclContext();
308 // Check for extern variable declared locally.
Fariborz Jahaniane81c5612010-06-30 18:57:21 +0000309 if (DC->isFunctionOrMethod() && D->hasLinkage())
Eli Friedman7facf842009-12-02 20:32:49 +0000310 while (!DC->isNamespace() && !DC->isTranslationUnit())
311 DC = DC->getParent();
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000312 if (DC->isTranslationUnit() && D->getLinkage() != InternalLinkage)
Eli Friedman7facf842009-12-02 20:32:49 +0000313 return false;
314 }
315
Eli Friedmanc00cb642010-07-18 20:49:59 +0000316 // Class members are always mangled.
317 if (D->getDeclContext()->isRecord())
318 return true;
319
Eli Friedman7facf842009-12-02 20:32:49 +0000320 // C functions and "main" are not mangled.
321 if ((FD && FD->isMain()) || isInCLinkageSpecification(D))
Daniel Dunbarf981bf82009-11-21 09:14:52 +0000322 return false;
323
Anders Carlsson43f17402009-04-02 15:51:53 +0000324 return true;
325}
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000326
Daniel Dunbar7e0c1952009-11-21 09:17:15 +0000327void CXXNameMangler::mangle(const NamedDecl *D, llvm::StringRef Prefix) {
Mike Stump141c5af2009-09-02 00:25:38 +0000328 // Any decl can be declared with __asm("foo") on it, and this takes precedence
329 // over all other naming in the .o file.
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000330 if (const AsmLabelAttr *ALA = D->getAttr<AsmLabelAttr>()) {
Chris Lattnerca3f25c2009-03-21 08:24:40 +0000331 // If we have an asm name, then we use it as the mangling.
332 Out << '\01'; // LLVM IR Marker for __asm("foo")
333 Out << ALA->getLabel();
Daniel Dunbarf981bf82009-11-21 09:14:52 +0000334 return;
Chris Lattnerca3f25c2009-03-21 08:24:40 +0000335 }
Mike Stump1eb44332009-09-09 15:08:12 +0000336
Sean Hunt31455252010-01-24 03:04:27 +0000337 // <mangled-name> ::= _Z <encoding>
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000338 // ::= <data name>
339 // ::= <special-name>
Daniel Dunbar7e0c1952009-11-21 09:17:15 +0000340 Out << Prefix;
341 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
Daniel Dunbarf981bf82009-11-21 09:14:52 +0000342 mangleFunctionEncoding(FD);
Rafael Espindolad9800722010-03-11 14:07:00 +0000343 else if (const VarDecl *VD = dyn_cast<VarDecl>(D))
344 mangleName(VD);
Daniel Dunbar7e0c1952009-11-21 09:17:15 +0000345 else
Rafael Espindolad9800722010-03-11 14:07:00 +0000346 mangleName(cast<FieldDecl>(D));
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000347}
348
349void CXXNameMangler::mangleFunctionEncoding(const FunctionDecl *FD) {
350 // <encoding> ::= <function name> <bare-function-type>
351 mangleName(FD);
Mike Stump1eb44332009-09-09 15:08:12 +0000352
Daniel Dunbar7e0c1952009-11-21 09:17:15 +0000353 // Don't mangle in the type if this isn't a decl we should typically mangle.
354 if (!Context.shouldMangleDeclName(FD))
355 return;
356
Mike Stump141c5af2009-09-02 00:25:38 +0000357 // Whether the mangling of a function type includes the return type depends on
358 // the context and the nature of the function. The rules for deciding whether
359 // the return type is included are:
Mike Stump1eb44332009-09-09 15:08:12 +0000360 //
Douglas Gregor1fd2dd12009-06-29 22:39:32 +0000361 // 1. Template functions (names or types) have return types encoded, with
362 // the exceptions listed below.
Mike Stump1eb44332009-09-09 15:08:12 +0000363 // 2. Function types not appearing as part of a function name mangling,
Douglas Gregor1fd2dd12009-06-29 22:39:32 +0000364 // e.g. parameters, pointer types, etc., have return type encoded, with the
365 // exceptions listed below.
366 // 3. Non-template function names do not have return types encoded.
367 //
Mike Stump141c5af2009-09-02 00:25:38 +0000368 // The exceptions mentioned in (1) and (2) above, for which the return type is
369 // never included, are
Douglas Gregor1fd2dd12009-06-29 22:39:32 +0000370 // 1. Constructors.
371 // 2. Destructors.
372 // 3. Conversion operator functions, e.g. operator int.
373 bool MangleReturnType = false;
Anders Carlsson9234b7f2009-09-17 03:46:43 +0000374 if (FunctionTemplateDecl *PrimaryTemplate = FD->getPrimaryTemplate()) {
375 if (!(isa<CXXConstructorDecl>(FD) || isa<CXXDestructorDecl>(FD) ||
376 isa<CXXConversionDecl>(FD)))
377 MangleReturnType = true;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000378
Anders Carlsson9234b7f2009-09-17 03:46:43 +0000379 // Mangle the type of the primary template.
380 FD = PrimaryTemplate->getTemplatedDecl();
381 }
382
John McCall54e14c42009-10-22 22:37:11 +0000383 // Do the canonicalization out here because parameter types can
384 // undergo additional canonicalization (e.g. array decay).
John McCallf4c73712011-01-19 06:33:43 +0000385 const FunctionType *FT
386 = cast<FunctionType>(Context.getASTContext()
John McCall54e14c42009-10-22 22:37:11 +0000387 .getCanonicalType(FD->getType()));
388
389 mangleBareFunctionType(FT, MangleReturnType);
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000390}
391
Anders Carlsson47846d22009-12-04 06:23:23 +0000392static const DeclContext *IgnoreLinkageSpecDecls(const DeclContext *DC) {
393 while (isa<LinkageSpecDecl>(DC)) {
Anders Carlsson47846d22009-12-04 06:23:23 +0000394 DC = DC->getParent();
395 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000396
Anders Carlsson47846d22009-12-04 06:23:23 +0000397 return DC;
398}
399
Anders Carlssonc820f902010-06-02 15:58:27 +0000400/// isStd - Return whether a given namespace is the 'std' namespace.
401static bool isStd(const NamespaceDecl *NS) {
402 if (!IgnoreLinkageSpecDecls(NS->getParent())->isTranslationUnit())
403 return false;
404
405 const IdentifierInfo *II = NS->getOriginalNamespace()->getIdentifier();
406 return II && II->isStr("std");
407}
408
Anders Carlsson47846d22009-12-04 06:23:23 +0000409// isStdNamespace - Return whether a given decl context is a toplevel 'std'
410// namespace.
Daniel Dunbar1308af92009-11-21 09:11:45 +0000411static bool isStdNamespace(const DeclContext *DC) {
Anders Carlsson47846d22009-12-04 06:23:23 +0000412 if (!DC->isNamespace())
413 return false;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000414
Anders Carlsson47846d22009-12-04 06:23:23 +0000415 return isStd(cast<NamespaceDecl>(DC));
Daniel Dunbar1308af92009-11-21 09:11:45 +0000416}
417
Anders Carlssonbb36ba42009-09-26 03:24:57 +0000418static const TemplateDecl *
419isTemplate(const NamedDecl *ND, const TemplateArgumentList *&TemplateArgs) {
Anders Carlsson2744a062009-09-18 19:00:18 +0000420 // Check if we have a function template.
421 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)){
Anders Carlssonbb36ba42009-09-26 03:24:57 +0000422 if (const TemplateDecl *TD = FD->getPrimaryTemplate()) {
Anders Carlsson2744a062009-09-18 19:00:18 +0000423 TemplateArgs = FD->getTemplateSpecializationArgs();
Anders Carlssonbb36ba42009-09-26 03:24:57 +0000424 return TD;
Anders Carlsson2744a062009-09-18 19:00:18 +0000425 }
426 }
427
Anders Carlssoneafc6dc2009-09-18 19:44:50 +0000428 // Check if we have a class template.
429 if (const ClassTemplateSpecializationDecl *Spec =
430 dyn_cast<ClassTemplateSpecializationDecl>(ND)) {
431 TemplateArgs = &Spec->getTemplateArgs();
Anders Carlssonbb36ba42009-09-26 03:24:57 +0000432 return Spec->getSpecializedTemplate();
Anders Carlssoneafc6dc2009-09-18 19:44:50 +0000433 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000434
Anders Carlsson2744a062009-09-18 19:00:18 +0000435 return 0;
436}
437
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000438void CXXNameMangler::mangleName(const NamedDecl *ND) {
439 // <name> ::= <nested-name>
440 // ::= <unscoped-name>
441 // ::= <unscoped-template-name> <template-args>
Anders Carlsson201ce742009-09-17 03:17:01 +0000442 // ::= <local-name>
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000443 //
Anders Carlssond58d6f72009-09-17 16:12:20 +0000444 const DeclContext *DC = ND->getDeclContext();
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000445
Eli Friedman7facf842009-12-02 20:32:49 +0000446 // If this is an extern variable declared locally, the relevant DeclContext
447 // is that of the containing namespace, or the translation unit.
448 if (isa<FunctionDecl>(DC) && ND->hasLinkage())
449 while (!DC->isNamespace() && !DC->isTranslationUnit())
450 DC = DC->getParent();
John McCall82b7d7b2010-10-18 21:28:44 +0000451 else if (GetLocalClassDecl(ND)) {
452 mangleLocalName(ND);
453 return;
454 }
Eli Friedman7facf842009-12-02 20:32:49 +0000455
Anders Carlsson5cc58c62009-09-22 17:23:30 +0000456 while (isa<LinkageSpecDecl>(DC))
Anders Carlssond58d6f72009-09-17 16:12:20 +0000457 DC = DC->getParent();
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000458
Anders Carlssond58d6f72009-09-17 16:12:20 +0000459 if (DC->isTranslationUnit() || isStdNamespace(DC)) {
Anders Carlsson2744a062009-09-18 19:00:18 +0000460 // Check if we have a template.
461 const TemplateArgumentList *TemplateArgs = 0;
Anders Carlsson0fa6df42009-09-26 19:45:45 +0000462 if (const TemplateDecl *TD = isTemplate(ND, TemplateArgs)) {
Anders Carlsson2744a062009-09-18 19:00:18 +0000463 mangleUnscopedTemplateName(TD);
Rafael Espindolad9800722010-03-11 14:07:00 +0000464 TemplateParameterList *TemplateParameters = TD->getTemplateParameters();
465 mangleTemplateArgs(*TemplateParameters, *TemplateArgs);
Anders Carlsson2744a062009-09-18 19:00:18 +0000466 return;
Anders Carlsson7482e242009-09-18 04:29:09 +0000467 }
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000468
Anders Carlsson7482e242009-09-18 04:29:09 +0000469 mangleUnscopedName(ND);
470 return;
471 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000472
Anders Carlsson7b06f6c2009-12-10 03:14:39 +0000473 if (isa<FunctionDecl>(DC) || isa<ObjCMethodDecl>(DC)) {
Anders Carlsson7482e242009-09-18 04:29:09 +0000474 mangleLocalName(ND);
475 return;
476 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000477
Eli Friedman7facf842009-12-02 20:32:49 +0000478 mangleNestedName(ND, DC);
Anders Carlsson7482e242009-09-18 04:29:09 +0000479}
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000480void CXXNameMangler::mangleName(const TemplateDecl *TD,
Anders Carlsson7624f212009-09-18 02:42:01 +0000481 const TemplateArgument *TemplateArgs,
482 unsigned NumTemplateArgs) {
Anders Carlsson47846d22009-12-04 06:23:23 +0000483 const DeclContext *DC = IgnoreLinkageSpecDecls(TD->getDeclContext());
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000484
Anders Carlsson7624f212009-09-18 02:42:01 +0000485 if (DC->isTranslationUnit() || isStdNamespace(DC)) {
Anders Carlsson0fa6df42009-09-26 19:45:45 +0000486 mangleUnscopedTemplateName(TD);
Rafael Espindolad9800722010-03-11 14:07:00 +0000487 TemplateParameterList *TemplateParameters = TD->getTemplateParameters();
488 mangleTemplateArgs(*TemplateParameters, TemplateArgs, NumTemplateArgs);
Anders Carlsson7624f212009-09-18 02:42:01 +0000489 } else {
490 mangleNestedName(TD, TemplateArgs, NumTemplateArgs);
491 }
492}
493
Anders Carlsson201ce742009-09-17 03:17:01 +0000494void CXXNameMangler::mangleUnscopedName(const NamedDecl *ND) {
495 // <unscoped-name> ::= <unqualified-name>
496 // ::= St <unqualified-name> # ::std::
497 if (isStdNamespace(ND->getDeclContext()))
498 Out << "St";
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000499
Anders Carlsson201ce742009-09-17 03:17:01 +0000500 mangleUnqualifiedName(ND);
501}
502
Anders Carlsson0fa6df42009-09-26 19:45:45 +0000503void CXXNameMangler::mangleUnscopedTemplateName(const TemplateDecl *ND) {
Anders Carlsson201ce742009-09-17 03:17:01 +0000504 // <unscoped-template-name> ::= <unscoped-name>
505 // ::= <substitution>
Anders Carlsson7624f212009-09-18 02:42:01 +0000506 if (mangleSubstitution(ND))
Anders Carlsson03c9d532009-09-17 04:02:31 +0000507 return;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000508
Douglas Gregor32fb4e12010-02-05 20:45:00 +0000509 // <template-template-param> ::= <template-param>
510 if (const TemplateTemplateParmDecl *TTP
511 = dyn_cast<TemplateTemplateParmDecl>(ND)) {
512 mangleTemplateParameter(TTP->getIndex());
513 return;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000514 }
Douglas Gregor32fb4e12010-02-05 20:45:00 +0000515
Anders Carlsson1668f202009-09-26 20:13:56 +0000516 mangleUnscopedName(ND->getTemplatedDecl());
Anders Carlsson7624f212009-09-18 02:42:01 +0000517 addSubstitution(ND);
Anders Carlsson201ce742009-09-17 03:17:01 +0000518}
519
Douglas Gregor1e9268e2010-04-28 05:58:56 +0000520void CXXNameMangler::mangleUnscopedTemplateName(TemplateName Template) {
521 // <unscoped-template-name> ::= <unscoped-name>
522 // ::= <substitution>
523 if (TemplateDecl *TD = Template.getAsTemplateDecl())
524 return mangleUnscopedTemplateName(TD);
Sean Huntc3021132010-05-05 15:23:54 +0000525
Douglas Gregor1e9268e2010-04-28 05:58:56 +0000526 if (mangleSubstitution(Template))
527 return;
528
529 // FIXME: How to cope with operators here?
530 DependentTemplateName *Dependent = Template.getAsDependentTemplateName();
531 assert(Dependent && "Not a dependent template name?");
532 if (!Dependent->isIdentifier()) {
533 // FIXME: We can't possibly know the arity of the operator here!
534 Diagnostic &Diags = Context.getDiags();
535 unsigned DiagID = Diags.getCustomDiagID(Diagnostic::Error,
536 "cannot mangle dependent operator name");
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000537 Diags.Report(DiagID);
Douglas Gregor1e9268e2010-04-28 05:58:56 +0000538 return;
539 }
Sean Huntc3021132010-05-05 15:23:54 +0000540
Douglas Gregor1e9268e2010-04-28 05:58:56 +0000541 mangleSourceName(Dependent->getIdentifier());
542 addSubstitution(Template);
543}
544
John McCall0512e482010-07-14 04:20:34 +0000545void CXXNameMangler::mangleFloat(const llvm::APFloat &F) {
546 // TODO: avoid this copy with careful stream management.
547 llvm::SmallString<20> Buffer;
548 F.bitcastToAPInt().toString(Buffer, 16, false);
549 Out.write(Buffer.data(), Buffer.size());
550}
551
552void CXXNameMangler::mangleNumber(const llvm::APSInt &Value) {
553 if (Value.isSigned() && Value.isNegative()) {
554 Out << 'n';
555 Value.abs().print(Out, true);
556 } else
557 Value.print(Out, Value.isSigned());
558}
559
Anders Carlssona94822e2009-11-26 02:32:05 +0000560void CXXNameMangler::mangleNumber(int64_t Number) {
561 // <number> ::= [n] <non-negative decimal integer>
562 if (Number < 0) {
563 Out << 'n';
564 Number = -Number;
565 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000566
Anders Carlssona94822e2009-11-26 02:32:05 +0000567 Out << Number;
568}
569
Anders Carlsson19879c92010-03-23 17:17:29 +0000570void CXXNameMangler::mangleCallOffset(int64_t NonVirtual, int64_t Virtual) {
Mike Stump141c5af2009-09-02 00:25:38 +0000571 // <call-offset> ::= h <nv-offset> _
572 // ::= v <v-offset> _
573 // <nv-offset> ::= <offset number> # non-virtual base override
Anders Carlssona94822e2009-11-26 02:32:05 +0000574 // <v-offset> ::= <offset number> _ <virtual offset number>
Mike Stump141c5af2009-09-02 00:25:38 +0000575 // # virtual base override, with vcall offset
Anders Carlsson19879c92010-03-23 17:17:29 +0000576 if (!Virtual) {
Anders Carlssona94822e2009-11-26 02:32:05 +0000577 Out << 'h';
Anders Carlsson19879c92010-03-23 17:17:29 +0000578 mangleNumber(NonVirtual);
Anders Carlssona94822e2009-11-26 02:32:05 +0000579 Out << '_';
580 return;
Mike Stump141c5af2009-09-02 00:25:38 +0000581 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000582
Anders Carlssona94822e2009-11-26 02:32:05 +0000583 Out << 'v';
Anders Carlsson19879c92010-03-23 17:17:29 +0000584 mangleNumber(NonVirtual);
Anders Carlssona94822e2009-11-26 02:32:05 +0000585 Out << '_';
Anders Carlsson19879c92010-03-23 17:17:29 +0000586 mangleNumber(Virtual);
Anders Carlssona94822e2009-11-26 02:32:05 +0000587 Out << '_';
Mike Stump9124bcc2009-09-02 00:56:18 +0000588}
589
John McCall1dd73832010-02-04 01:42:13 +0000590void CXXNameMangler::mangleUnresolvedScope(NestedNameSpecifier *Qualifier) {
591 Qualifier = getASTContext().getCanonicalNestedNameSpecifier(Qualifier);
592 switch (Qualifier->getKind()) {
593 case NestedNameSpecifier::Global:
594 // nothing
595 break;
596 case NestedNameSpecifier::Namespace:
597 mangleName(Qualifier->getAsNamespace());
598 break;
599 case NestedNameSpecifier::TypeSpec:
Rafael Espindola9b35b252010-03-17 04:28:11 +0000600 case NestedNameSpecifier::TypeSpecWithTemplate: {
601 const Type *QTy = Qualifier->getAsType();
602
603 if (const TemplateSpecializationType *TST =
604 dyn_cast<TemplateSpecializationType>(QTy)) {
605 if (!mangleSubstitution(QualType(TST, 0))) {
Douglas Gregor20f0cc72010-04-23 03:10:43 +0000606 mangleTemplatePrefix(TST->getTemplateName());
Sean Huntc3021132010-05-05 15:23:54 +0000607
Douglas Gregor20f0cc72010-04-23 03:10:43 +0000608 // FIXME: GCC does not appear to mangle the template arguments when
609 // the template in question is a dependent template name. Should we
610 // emulate that badness?
611 mangleTemplateArgs(TST->getTemplateName(), TST->getArgs(),
Rafael Espindola9b35b252010-03-17 04:28:11 +0000612 TST->getNumArgs());
613 addSubstitution(QualType(TST, 0));
614 }
615 } else {
616 // We use the QualType mangle type variant here because it handles
617 // substitutions.
618 mangleType(QualType(QTy, 0));
619 }
620 }
John McCall1dd73832010-02-04 01:42:13 +0000621 break;
622 case NestedNameSpecifier::Identifier:
John McCallad5e7382010-03-01 23:49:17 +0000623 // Member expressions can have these without prefixes.
624 if (Qualifier->getPrefix())
625 mangleUnresolvedScope(Qualifier->getPrefix());
John McCall1dd73832010-02-04 01:42:13 +0000626 mangleSourceName(Qualifier->getAsIdentifier());
627 break;
628 }
629}
630
631/// Mangles a name which was not resolved to a specific entity.
632void CXXNameMangler::mangleUnresolvedName(NestedNameSpecifier *Qualifier,
633 DeclarationName Name,
634 unsigned KnownArity) {
635 if (Qualifier)
636 mangleUnresolvedScope(Qualifier);
637 // FIXME: ambiguity of unqualified lookup with ::
638
639 mangleUnqualifiedName(0, Name, KnownArity);
640}
641
Anders Carlsson6f7e2f42010-06-08 14:49:03 +0000642static const FieldDecl *FindFirstNamedDataMember(const RecordDecl *RD) {
643 assert(RD->isAnonymousStructOrUnion() &&
644 "Expected anonymous struct or union!");
645
646 for (RecordDecl::field_iterator I = RD->field_begin(), E = RD->field_end();
647 I != E; ++I) {
648 const FieldDecl *FD = *I;
649
650 if (FD->getIdentifier())
651 return FD;
652
653 if (const RecordType *RT = FD->getType()->getAs<RecordType>()) {
654 if (const FieldDecl *NamedDataMember =
655 FindFirstNamedDataMember(RT->getDecl()))
656 return NamedDataMember;
657 }
658 }
659
660 // We didn't find a named data member.
661 return 0;
662}
663
John McCall1dd73832010-02-04 01:42:13 +0000664void CXXNameMangler::mangleUnqualifiedName(const NamedDecl *ND,
665 DeclarationName Name,
666 unsigned KnownArity) {
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000667 // <unqualified-name> ::= <operator-name>
Mike Stump1eb44332009-09-09 15:08:12 +0000668 // ::= <ctor-dtor-name>
669 // ::= <source-name>
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000670 switch (Name.getNameKind()) {
Anders Carlssonc4355b62009-10-07 01:45:02 +0000671 case DeclarationName::Identifier: {
Anders Carlssonc4355b62009-10-07 01:45:02 +0000672 if (const IdentifierInfo *II = Name.getAsIdentifierInfo()) {
Sean Hunt31455252010-01-24 03:04:27 +0000673 // We must avoid conflicts between internally- and externally-
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000674 // linked variable declaration names in the same TU.
Anders Carlssonaec25232010-02-06 04:52:27 +0000675 // This naming convention is the same as that followed by GCC, though it
676 // shouldn't actually matter.
677 if (ND && isa<VarDecl>(ND) && ND->getLinkage() == InternalLinkage &&
Sean Hunt31455252010-01-24 03:04:27 +0000678 ND->getDeclContext()->isFileContext())
679 Out << 'L';
680
Anders Carlssonc4355b62009-10-07 01:45:02 +0000681 mangleSourceName(II);
682 break;
683 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000684
John McCall1dd73832010-02-04 01:42:13 +0000685 // Otherwise, an anonymous entity. We must have a declaration.
686 assert(ND && "mangling empty name without declaration");
687
688 if (const NamespaceDecl *NS = dyn_cast<NamespaceDecl>(ND)) {
689 if (NS->isAnonymousNamespace()) {
690 // This is how gcc mangles these names.
691 Out << "12_GLOBAL__N_1";
692 break;
693 }
694 }
695
Anders Carlsson6f7e2f42010-06-08 14:49:03 +0000696 if (const VarDecl *VD = dyn_cast<VarDecl>(ND)) {
697 // We must have an anonymous union or struct declaration.
698 const RecordDecl *RD =
699 cast<RecordDecl>(VD->getType()->getAs<RecordType>()->getDecl());
700
701 // Itanium C++ ABI 5.1.2:
702 //
703 // For the purposes of mangling, the name of an anonymous union is
704 // considered to be the name of the first named data member found by a
705 // pre-order, depth-first, declaration-order walk of the data members of
706 // the anonymous union. If there is no such data member (i.e., if all of
707 // the data members in the union are unnamed), then there is no way for
708 // a program to refer to the anonymous union, and there is therefore no
709 // need to mangle its name.
710 const FieldDecl *FD = FindFirstNamedDataMember(RD);
John McCall7121c8f2010-08-05 22:02:13 +0000711
712 // It's actually possible for various reasons for us to get here
713 // with an empty anonymous struct / union. Fortunately, it
714 // doesn't really matter what name we generate.
715 if (!FD) break;
Anders Carlsson6f7e2f42010-06-08 14:49:03 +0000716 assert(FD->getIdentifier() && "Data member name isn't an identifier!");
717
718 mangleSourceName(FD->getIdentifier());
719 break;
720 }
721
Anders Carlssonc4355b62009-10-07 01:45:02 +0000722 // We must have an anonymous struct.
723 const TagDecl *TD = cast<TagDecl>(ND);
724 if (const TypedefDecl *D = TD->getTypedefForAnonDecl()) {
725 assert(TD->getDeclContext() == D->getDeclContext() &&
726 "Typedef should not be in another decl context!");
727 assert(D->getDeclName().getAsIdentifierInfo() &&
728 "Typedef was not named!");
729 mangleSourceName(D->getDeclName().getAsIdentifierInfo());
730 break;
731 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000732
Anders Carlssonc4355b62009-10-07 01:45:02 +0000733 // Get a unique id for the anonymous struct.
734 uint64_t AnonStructId = Context.getAnonymousStructId(TD);
735
736 // Mangle it as a source name in the form
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000737 // [n] $_<id>
Anders Carlssonc4355b62009-10-07 01:45:02 +0000738 // where n is the length of the string.
739 llvm::SmallString<8> Str;
740 Str += "$_";
741 Str += llvm::utostr(AnonStructId);
742
743 Out << Str.size();
744 Out << Str.str();
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000745 break;
Anders Carlssonc4355b62009-10-07 01:45:02 +0000746 }
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000747
748 case DeclarationName::ObjCZeroArgSelector:
749 case DeclarationName::ObjCOneArgSelector:
750 case DeclarationName::ObjCMultiArgSelector:
751 assert(false && "Can't mangle Objective-C selector names here!");
752 break;
753
754 case DeclarationName::CXXConstructorName:
Anders Carlsson27ae5362009-04-17 01:58:57 +0000755 if (ND == Structor)
Mike Stump141c5af2009-09-02 00:25:38 +0000756 // If the named decl is the C++ constructor we're mangling, use the type
757 // we were given.
Anders Carlsson27ae5362009-04-17 01:58:57 +0000758 mangleCXXCtorType(static_cast<CXXCtorType>(StructorType));
Anders Carlsson3ac86b52009-04-15 05:36:58 +0000759 else
760 // Otherwise, use the complete constructor name. This is relevant if a
761 // class with a constructor is declared within a constructor.
762 mangleCXXCtorType(Ctor_Complete);
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000763 break;
764
765 case DeclarationName::CXXDestructorName:
Anders Carlsson27ae5362009-04-17 01:58:57 +0000766 if (ND == Structor)
Mike Stump141c5af2009-09-02 00:25:38 +0000767 // If the named decl is the C++ destructor we're mangling, use the type we
768 // were given.
Anders Carlsson27ae5362009-04-17 01:58:57 +0000769 mangleCXXDtorType(static_cast<CXXDtorType>(StructorType));
770 else
771 // Otherwise, use the complete destructor name. This is relevant if a
772 // class with a destructor is declared within a destructor.
773 mangleCXXDtorType(Dtor_Complete);
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000774 break;
775
776 case DeclarationName::CXXConversionFunctionName:
Mike Stump1eb44332009-09-09 15:08:12 +0000777 // <operator-name> ::= cv <type> # (cast)
Douglas Gregor219cc612009-02-13 01:28:03 +0000778 Out << "cv";
Anders Carlssonb5404912009-10-07 01:06:45 +0000779 mangleType(Context.getASTContext().getCanonicalType(Name.getCXXNameType()));
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000780 break;
781
Anders Carlsson8257d412009-12-22 06:36:32 +0000782 case DeclarationName::CXXOperatorName: {
John McCall1dd73832010-02-04 01:42:13 +0000783 unsigned Arity;
784 if (ND) {
785 Arity = cast<FunctionDecl>(ND)->getNumParams();
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000786
John McCall1dd73832010-02-04 01:42:13 +0000787 // If we have a C++ member function, we need to include the 'this' pointer.
788 // FIXME: This does not make sense for operators that are static, but their
789 // names stay the same regardless of the arity (operator new for instance).
790 if (isa<CXXMethodDecl>(ND))
791 Arity++;
792 } else
793 Arity = KnownArity;
794
Anders Carlsson8257d412009-12-22 06:36:32 +0000795 mangleOperatorName(Name.getCXXOverloadedOperator(), Arity);
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000796 break;
Anders Carlsson8257d412009-12-22 06:36:32 +0000797 }
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000798
Sean Hunt3e518bd2009-11-29 07:34:05 +0000799 case DeclarationName::CXXLiteralOperatorName:
Sean Hunt5dd6b392009-12-04 21:11:13 +0000800 // FIXME: This mangling is not yet official.
Sean Hunt2421f662009-12-04 21:01:37 +0000801 Out << "li";
Sean Hunt3e518bd2009-11-29 07:34:05 +0000802 mangleSourceName(Name.getCXXLiteralIdentifier());
803 break;
804
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000805 case DeclarationName::CXXUsingDirective:
806 assert(false && "Can't mangle a using directive name!");
Douglas Gregor219cc612009-02-13 01:28:03 +0000807 break;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000808 }
809}
810
811void CXXNameMangler::mangleSourceName(const IdentifierInfo *II) {
812 // <source-name> ::= <positive length number> <identifier>
813 // <number> ::= [n] <non-negative decimal integer>
814 // <identifier> ::= <unqualified source code identifier>
815 Out << II->getLength() << II->getName();
816}
817
Eli Friedman7facf842009-12-02 20:32:49 +0000818void CXXNameMangler::mangleNestedName(const NamedDecl *ND,
Fariborz Jahanian57058532010-03-03 19:41:08 +0000819 const DeclContext *DC,
820 bool NoFunction) {
Douglas Gregor0a9a6d62011-01-26 17:36:28 +0000821 // <nested-name>
822 // ::= N [<CV-qualifiers>] [<ref-qualifier>] <prefix> <unqualified-name> E
823 // ::= N [<CV-qualifiers>] [<ref-qualifier>] <template-prefix>
824 // <template-args> E
Anders Carlssond99edc42009-09-26 03:55:37 +0000825
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000826 Out << 'N';
Douglas Gregor0a9a6d62011-01-26 17:36:28 +0000827 if (const CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(ND)) {
John McCall0953e762009-09-24 19:53:00 +0000828 mangleQualifiers(Qualifiers::fromCVRMask(Method->getTypeQualifiers()));
Douglas Gregor0a9a6d62011-01-26 17:36:28 +0000829 mangleRefQualifier(Method->getRefQualifier());
830 }
831
Anders Carlsson2744a062009-09-18 19:00:18 +0000832 // Check if we have a template.
833 const TemplateArgumentList *TemplateArgs = 0;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000834 if (const TemplateDecl *TD = isTemplate(ND, TemplateArgs)) {
Anders Carlsson2744a062009-09-18 19:00:18 +0000835 mangleTemplatePrefix(TD);
Rafael Espindolad9800722010-03-11 14:07:00 +0000836 TemplateParameterList *TemplateParameters = TD->getTemplateParameters();
837 mangleTemplateArgs(*TemplateParameters, *TemplateArgs);
Fariborz Jahanian57058532010-03-03 19:41:08 +0000838 }
839 else {
840 manglePrefix(DC, NoFunction);
Anders Carlsson7482e242009-09-18 04:29:09 +0000841 mangleUnqualifiedName(ND);
842 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000843
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000844 Out << 'E';
845}
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000846void CXXNameMangler::mangleNestedName(const TemplateDecl *TD,
Anders Carlsson7624f212009-09-18 02:42:01 +0000847 const TemplateArgument *TemplateArgs,
848 unsigned NumTemplateArgs) {
Anders Carlssone45117b2009-09-27 19:53:49 +0000849 // <nested-name> ::= N [<CV-qualifiers>] <template-prefix> <template-args> E
850
Anders Carlsson7624f212009-09-18 02:42:01 +0000851 Out << 'N';
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000852
Anders Carlssone45117b2009-09-27 19:53:49 +0000853 mangleTemplatePrefix(TD);
Rafael Espindolad9800722010-03-11 14:07:00 +0000854 TemplateParameterList *TemplateParameters = TD->getTemplateParameters();
855 mangleTemplateArgs(*TemplateParameters, TemplateArgs, NumTemplateArgs);
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000856
Anders Carlsson7624f212009-09-18 02:42:01 +0000857 Out << 'E';
858}
859
Anders Carlsson1b42c792009-04-02 16:24:45 +0000860void CXXNameMangler::mangleLocalName(const NamedDecl *ND) {
861 // <local-name> := Z <function encoding> E <entity name> [<discriminator>]
862 // := Z <function encoding> E s [<discriminator>]
Mike Stump1eb44332009-09-09 15:08:12 +0000863 // <discriminator> := _ <non-negative number>
Fariborz Jahanian57058532010-03-03 19:41:08 +0000864 const DeclContext *DC = ND->getDeclContext();
Anders Carlsson1b42c792009-04-02 16:24:45 +0000865 Out << 'Z';
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000866
Charles Davis685b1d92010-05-26 18:25:27 +0000867 if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(DC)) {
868 mangleObjCMethodName(MD);
John McCall82b7d7b2010-10-18 21:28:44 +0000869 } else if (const CXXRecordDecl *RD = GetLocalClassDecl(ND)) {
870 mangleFunctionEncoding(cast<FunctionDecl>(RD->getDeclContext()));
Fariborz Jahanian57058532010-03-03 19:41:08 +0000871 Out << 'E';
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000872
John McCall82b7d7b2010-10-18 21:28:44 +0000873 // Mangle the name relative to the closest enclosing function.
874 if (ND == RD) // equality ok because RD derived from ND above
875 mangleUnqualifiedName(ND);
876 else
877 mangleNestedName(ND, DC, true /*NoFunction*/);
878
Fariborz Jahanian4819ac42010-03-04 01:02:03 +0000879 unsigned disc;
John McCall82b7d7b2010-10-18 21:28:44 +0000880 if (Context.getNextDiscriminator(RD, disc)) {
Fariborz Jahanian4819ac42010-03-04 01:02:03 +0000881 if (disc < 10)
882 Out << '_' << disc;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000883 else
Fariborz Jahanian4819ac42010-03-04 01:02:03 +0000884 Out << "__" << disc << '_';
885 }
Fariborz Jahanian57058532010-03-03 19:41:08 +0000886
887 return;
888 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000889 else
Fariborz Jahanian57058532010-03-03 19:41:08 +0000890 mangleFunctionEncoding(cast<FunctionDecl>(DC));
Anders Carlsson7b06f6c2009-12-10 03:14:39 +0000891
Anders Carlsson1b42c792009-04-02 16:24:45 +0000892 Out << 'E';
Eli Friedman6f9f25d2009-12-11 20:21:38 +0000893 mangleUnqualifiedName(ND);
Anders Carlsson1b42c792009-04-02 16:24:45 +0000894}
895
Fariborz Jahanian57058532010-03-03 19:41:08 +0000896void CXXNameMangler::manglePrefix(const DeclContext *DC, bool NoFunction) {
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000897 // <prefix> ::= <prefix> <unqualified-name>
898 // ::= <template-prefix> <template-args>
899 // ::= <template-param>
900 // ::= # empty
901 // ::= <substitution>
Anders Carlsson6862fc72009-09-17 04:16:28 +0000902
Anders Carlssonadd28822009-09-22 20:33:31 +0000903 while (isa<LinkageSpecDecl>(DC))
904 DC = DC->getParent();
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000905
Anders Carlsson9263e912009-09-18 18:39:58 +0000906 if (DC->isTranslationUnit())
907 return;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000908
Douglas Gregor35415f52010-05-25 17:04:15 +0000909 if (const BlockDecl *Block = dyn_cast<BlockDecl>(DC)) {
910 manglePrefix(DC->getParent(), NoFunction);
911 llvm::SmallString<64> Name;
Rafael Espindolac4850c22011-02-10 23:59:36 +0000912 llvm::raw_svector_ostream NameStream(Name);
913 Context.mangleBlock(Block, NameStream);
914 NameStream.flush();
Douglas Gregor35415f52010-05-25 17:04:15 +0000915 Out << Name.size() << Name;
916 return;
917 }
918
Anders Carlsson6862fc72009-09-17 04:16:28 +0000919 if (mangleSubstitution(cast<NamedDecl>(DC)))
920 return;
Anders Carlsson7482e242009-09-18 04:29:09 +0000921
Anders Carlsson2ee3fca2009-09-18 20:11:09 +0000922 // Check if we have a template.
923 const TemplateArgumentList *TemplateArgs = 0;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000924 if (const TemplateDecl *TD = isTemplate(cast<NamedDecl>(DC), TemplateArgs)) {
Anders Carlsson2ee3fca2009-09-18 20:11:09 +0000925 mangleTemplatePrefix(TD);
Rafael Espindolad9800722010-03-11 14:07:00 +0000926 TemplateParameterList *TemplateParameters = TD->getTemplateParameters();
927 mangleTemplateArgs(*TemplateParameters, *TemplateArgs);
Fariborz Jahanian57058532010-03-03 19:41:08 +0000928 }
Douglas Gregor35415f52010-05-25 17:04:15 +0000929 else if(NoFunction && (isa<FunctionDecl>(DC) || isa<ObjCMethodDecl>(DC)))
Fariborz Jahanian57058532010-03-03 19:41:08 +0000930 return;
Douglas Gregor35415f52010-05-25 17:04:15 +0000931 else if (const ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(DC))
932 mangleObjCMethodName(Method);
Fariborz Jahanian57058532010-03-03 19:41:08 +0000933 else {
934 manglePrefix(DC->getParent(), NoFunction);
Anders Carlsson2ee3fca2009-09-18 20:11:09 +0000935 mangleUnqualifiedName(cast<NamedDecl>(DC));
936 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000937
Anders Carlsson6862fc72009-09-17 04:16:28 +0000938 addSubstitution(cast<NamedDecl>(DC));
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000939}
940
Douglas Gregor20f0cc72010-04-23 03:10:43 +0000941void CXXNameMangler::mangleTemplatePrefix(TemplateName Template) {
942 // <template-prefix> ::= <prefix> <template unqualified-name>
943 // ::= <template-param>
944 // ::= <substitution>
945 if (TemplateDecl *TD = Template.getAsTemplateDecl())
946 return mangleTemplatePrefix(TD);
947
948 if (QualifiedTemplateName *Qualified = Template.getAsQualifiedTemplateName())
949 mangleUnresolvedScope(Qualified->getQualifier());
Sean Huntc3021132010-05-05 15:23:54 +0000950
Douglas Gregor20f0cc72010-04-23 03:10:43 +0000951 if (OverloadedTemplateStorage *Overloaded
952 = Template.getAsOverloadedTemplate()) {
Sean Huntc3021132010-05-05 15:23:54 +0000953 mangleUnqualifiedName(0, (*Overloaded->begin())->getDeclName(),
Douglas Gregor20f0cc72010-04-23 03:10:43 +0000954 UnknownArity);
955 return;
956 }
Sean Huntc3021132010-05-05 15:23:54 +0000957
Douglas Gregor20f0cc72010-04-23 03:10:43 +0000958 DependentTemplateName *Dependent = Template.getAsDependentTemplateName();
959 assert(Dependent && "Unknown template name kind?");
960 mangleUnresolvedScope(Dependent->getQualifier());
Douglas Gregor1e9268e2010-04-28 05:58:56 +0000961 mangleUnscopedTemplateName(Template);
Douglas Gregor20f0cc72010-04-23 03:10:43 +0000962}
963
Anders Carlsson0fa6df42009-09-26 19:45:45 +0000964void CXXNameMangler::mangleTemplatePrefix(const TemplateDecl *ND) {
Anders Carlsson7482e242009-09-18 04:29:09 +0000965 // <template-prefix> ::= <prefix> <template unqualified-name>
966 // ::= <template-param>
967 // ::= <substitution>
Douglas Gregor32fb4e12010-02-05 20:45:00 +0000968 // <template-template-param> ::= <template-param>
969 // <substitution>
Anders Carlsson7482e242009-09-18 04:29:09 +0000970
Anders Carlssonaeb85372009-09-26 22:18:22 +0000971 if (mangleSubstitution(ND))
972 return;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000973
Douglas Gregor32fb4e12010-02-05 20:45:00 +0000974 // <template-template-param> ::= <template-param>
975 if (const TemplateTemplateParmDecl *TTP
976 = dyn_cast<TemplateTemplateParmDecl>(ND)) {
977 mangleTemplateParameter(TTP->getIndex());
978 return;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000979 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000980
Anders Carlssonaa73ab12009-09-18 18:47:07 +0000981 manglePrefix(ND->getDeclContext());
Anders Carlsson1668f202009-09-26 20:13:56 +0000982 mangleUnqualifiedName(ND->getTemplatedDecl());
Anders Carlssonaeb85372009-09-26 22:18:22 +0000983 addSubstitution(ND);
Anders Carlsson7482e242009-09-18 04:29:09 +0000984}
985
John McCallb6f532e2010-07-14 06:43:17 +0000986/// Mangles a template name under the production <type>. Required for
987/// template template arguments.
988/// <type> ::= <class-enum-type>
989/// ::= <template-param>
990/// ::= <substitution>
991void CXXNameMangler::mangleType(TemplateName TN) {
992 if (mangleSubstitution(TN))
993 return;
994
995 TemplateDecl *TD = 0;
996
997 switch (TN.getKind()) {
998 case TemplateName::QualifiedTemplate:
999 TD = TN.getAsQualifiedTemplateName()->getTemplateDecl();
1000 goto HaveDecl;
1001
1002 case TemplateName::Template:
1003 TD = TN.getAsTemplateDecl();
1004 goto HaveDecl;
1005
1006 HaveDecl:
1007 if (isa<TemplateTemplateParmDecl>(TD))
1008 mangleTemplateParameter(cast<TemplateTemplateParmDecl>(TD)->getIndex());
1009 else
1010 mangleName(TD);
1011 break;
1012
1013 case TemplateName::OverloadedTemplate:
1014 llvm_unreachable("can't mangle an overloaded template name as a <type>");
1015 break;
1016
1017 case TemplateName::DependentTemplate: {
1018 const DependentTemplateName *Dependent = TN.getAsDependentTemplateName();
1019 assert(Dependent->isIdentifier());
1020
1021 // <class-enum-type> ::= <name>
1022 // <name> ::= <nested-name>
1023 mangleUnresolvedScope(Dependent->getQualifier());
1024 mangleSourceName(Dependent->getIdentifier());
1025 break;
1026 }
1027
Douglas Gregor1aee05d2011-01-15 06:45:20 +00001028 case TemplateName::SubstTemplateTemplateParmPack: {
1029 SubstTemplateTemplateParmPackStorage *SubstPack
1030 = TN.getAsSubstTemplateTemplateParmPack();
1031 mangleTemplateParameter(SubstPack->getParameterPack()->getIndex());
1032 break;
1033 }
John McCallb6f532e2010-07-14 06:43:17 +00001034 }
1035
1036 addSubstitution(TN);
1037}
1038
Mike Stump1eb44332009-09-09 15:08:12 +00001039void
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001040CXXNameMangler::mangleOperatorName(OverloadedOperatorKind OO, unsigned Arity) {
1041 switch (OO) {
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001042 // <operator-name> ::= nw # new
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001043 case OO_New: Out << "nw"; break;
1044 // ::= na # new[]
1045 case OO_Array_New: Out << "na"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001046 // ::= dl # delete
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001047 case OO_Delete: Out << "dl"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001048 // ::= da # delete[]
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001049 case OO_Array_Delete: Out << "da"; break;
1050 // ::= ps # + (unary)
John McCall5e1e89b2010-08-18 19:18:59 +00001051 // ::= pl # + (binary or unknown)
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001052 case OO_Plus:
Anders Carlsson8257d412009-12-22 06:36:32 +00001053 Out << (Arity == 1? "ps" : "pl"); break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001054 // ::= ng # - (unary)
John McCall5e1e89b2010-08-18 19:18:59 +00001055 // ::= mi # - (binary or unknown)
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001056 case OO_Minus:
Anders Carlsson8257d412009-12-22 06:36:32 +00001057 Out << (Arity == 1? "ng" : "mi"); break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001058 // ::= ad # & (unary)
John McCall5e1e89b2010-08-18 19:18:59 +00001059 // ::= an # & (binary or unknown)
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001060 case OO_Amp:
Anders Carlsson8257d412009-12-22 06:36:32 +00001061 Out << (Arity == 1? "ad" : "an"); break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001062 // ::= de # * (unary)
John McCall5e1e89b2010-08-18 19:18:59 +00001063 // ::= ml # * (binary or unknown)
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001064 case OO_Star:
John McCall5e1e89b2010-08-18 19:18:59 +00001065 // Use binary when unknown.
Anders Carlsson8257d412009-12-22 06:36:32 +00001066 Out << (Arity == 1? "de" : "ml"); break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001067 // ::= co # ~
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001068 case OO_Tilde: Out << "co"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001069 // ::= dv # /
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001070 case OO_Slash: Out << "dv"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001071 // ::= rm # %
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001072 case OO_Percent: Out << "rm"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001073 // ::= or # |
1074 case OO_Pipe: Out << "or"; break;
1075 // ::= eo # ^
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001076 case OO_Caret: Out << "eo"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001077 // ::= aS # =
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001078 case OO_Equal: Out << "aS"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001079 // ::= pL # +=
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001080 case OO_PlusEqual: Out << "pL"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001081 // ::= mI # -=
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001082 case OO_MinusEqual: Out << "mI"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001083 // ::= mL # *=
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001084 case OO_StarEqual: Out << "mL"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001085 // ::= dV # /=
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001086 case OO_SlashEqual: Out << "dV"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001087 // ::= rM # %=
1088 case OO_PercentEqual: Out << "rM"; break;
1089 // ::= aN # &=
1090 case OO_AmpEqual: Out << "aN"; break;
1091 // ::= oR # |=
1092 case OO_PipeEqual: Out << "oR"; break;
1093 // ::= eO # ^=
1094 case OO_CaretEqual: Out << "eO"; break;
1095 // ::= ls # <<
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001096 case OO_LessLess: Out << "ls"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001097 // ::= rs # >>
1098 case OO_GreaterGreater: Out << "rs"; break;
1099 // ::= lS # <<=
1100 case OO_LessLessEqual: Out << "lS"; break;
1101 // ::= rS # >>=
1102 case OO_GreaterGreaterEqual: Out << "rS"; break;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001103 // ::= eq # ==
1104 case OO_EqualEqual: Out << "eq"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001105 // ::= ne # !=
1106 case OO_ExclaimEqual: Out << "ne"; break;
1107 // ::= lt # <
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001108 case OO_Less: Out << "lt"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001109 // ::= gt # >
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001110 case OO_Greater: Out << "gt"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001111 // ::= le # <=
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001112 case OO_LessEqual: Out << "le"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001113 // ::= ge # >=
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001114 case OO_GreaterEqual: Out << "ge"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001115 // ::= nt # !
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001116 case OO_Exclaim: Out << "nt"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001117 // ::= aa # &&
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001118 case OO_AmpAmp: Out << "aa"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001119 // ::= oo # ||
1120 case OO_PipePipe: Out << "oo"; break;
1121 // ::= pp # ++
1122 case OO_PlusPlus: Out << "pp"; break;
1123 // ::= mm # --
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001124 case OO_MinusMinus: Out << "mm"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001125 // ::= cm # ,
1126 case OO_Comma: Out << "cm"; break;
1127 // ::= pm # ->*
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001128 case OO_ArrowStar: Out << "pm"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001129 // ::= pt # ->
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001130 case OO_Arrow: Out << "pt"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001131 // ::= cl # ()
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001132 case OO_Call: Out << "cl"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001133 // ::= ix # []
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001134 case OO_Subscript: Out << "ix"; break;
Anders Carlssone170ba72009-12-14 01:45:37 +00001135
1136 // ::= qu # ?
1137 // The conditional operator can't be overloaded, but we still handle it when
1138 // mangling expressions.
1139 case OO_Conditional: Out << "qu"; break;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001140
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001141 case OO_None:
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001142 case NUM_OVERLOADED_OPERATORS:
Mike Stump1eb44332009-09-09 15:08:12 +00001143 assert(false && "Not an overloaded operator");
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001144 break;
1145 }
1146}
1147
John McCall0953e762009-09-24 19:53:00 +00001148void CXXNameMangler::mangleQualifiers(Qualifiers Quals) {
Mike Stump1eb44332009-09-09 15:08:12 +00001149 // <CV-qualifiers> ::= [r] [V] [K] # restrict (C99), volatile, const
John McCall0953e762009-09-24 19:53:00 +00001150 if (Quals.hasRestrict())
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001151 Out << 'r';
John McCall0953e762009-09-24 19:53:00 +00001152 if (Quals.hasVolatile())
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001153 Out << 'V';
John McCall0953e762009-09-24 19:53:00 +00001154 if (Quals.hasConst())
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001155 Out << 'K';
John McCall0953e762009-09-24 19:53:00 +00001156
Douglas Gregor56079f72010-06-14 23:15:08 +00001157 if (Quals.hasAddressSpace()) {
1158 // Extension:
1159 //
1160 // <type> ::= U <address-space-number>
1161 //
1162 // where <address-space-number> is a source name consisting of 'AS'
1163 // followed by the address space <number>.
1164 llvm::SmallString<64> ASString;
1165 ASString = "AS" + llvm::utostr_32(Quals.getAddressSpace());
1166 Out << 'U' << ASString.size() << ASString;
1167 }
1168
John McCall0953e762009-09-24 19:53:00 +00001169 // FIXME: For now, just drop all extension qualifiers on the floor.
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001170}
1171
Douglas Gregor0a9a6d62011-01-26 17:36:28 +00001172void CXXNameMangler::mangleRefQualifier(RefQualifierKind RefQualifier) {
1173 // <ref-qualifier> ::= R # lvalue reference
1174 // ::= O # rvalue-reference
1175 // Proposal to Itanium C++ ABI list on 1/26/11
1176 switch (RefQualifier) {
1177 case RQ_None:
1178 break;
1179
1180 case RQ_LValue:
1181 Out << 'R';
1182 break;
1183
1184 case RQ_RValue:
1185 Out << 'O';
1186 break;
1187 }
1188}
1189
Anders Carlsson7b06f6c2009-12-10 03:14:39 +00001190void CXXNameMangler::mangleObjCMethodName(const ObjCMethodDecl *MD) {
Charles Davis685b1d92010-05-26 18:25:27 +00001191 llvm::SmallString<64> Buffer;
Peter Collingbourne14110472011-01-13 18:57:25 +00001192 Context.mangleObjCMethodName(MD, Buffer);
Charles Davis685b1d92010-05-26 18:25:27 +00001193 Out << Buffer;
Anders Carlsson7b06f6c2009-12-10 03:14:39 +00001194}
1195
John McCallb47f7482011-01-26 20:05:40 +00001196void CXXNameMangler::mangleType(QualType nonCanon) {
Anders Carlsson4843e582009-03-10 17:07:44 +00001197 // Only operate on the canonical type!
John McCallb47f7482011-01-26 20:05:40 +00001198 QualType canon = nonCanon.getCanonicalType();
Anders Carlsson4843e582009-03-10 17:07:44 +00001199
John McCallb47f7482011-01-26 20:05:40 +00001200 SplitQualType split = canon.split();
1201 Qualifiers quals = split.second;
1202 const Type *ty = split.first;
1203
1204 bool isSubstitutable = quals || !isa<BuiltinType>(ty);
1205 if (isSubstitutable && mangleSubstitution(canon))
Anders Carlsson76967372009-09-17 00:43:46 +00001206 return;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001207
John McCallb47f7482011-01-26 20:05:40 +00001208 // If we're mangling a qualified array type, push the qualifiers to
1209 // the element type.
1210 if (quals && isa<ArrayType>(ty)) {
1211 ty = Context.getASTContext().getAsArrayType(canon);
1212 quals = Qualifiers();
1213
1214 // Note that we don't update canon: we want to add the
1215 // substitution at the canonical type.
1216 }
1217
1218 if (quals) {
1219 mangleQualifiers(quals);
John McCall0953e762009-09-24 19:53:00 +00001220 // Recurse: even if the qualified type isn't yet substitutable,
1221 // the unqualified type might be.
John McCallb47f7482011-01-26 20:05:40 +00001222 mangleType(QualType(ty, 0));
Anders Carlsson76967372009-09-17 00:43:46 +00001223 } else {
John McCallb47f7482011-01-26 20:05:40 +00001224 switch (ty->getTypeClass()) {
John McCallefe6aee2009-09-05 07:56:18 +00001225#define ABSTRACT_TYPE(CLASS, PARENT)
1226#define NON_CANONICAL_TYPE(CLASS, PARENT) \
Anders Carlsson76967372009-09-17 00:43:46 +00001227 case Type::CLASS: \
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +00001228 llvm_unreachable("can't mangle non-canonical type " #CLASS "Type"); \
Anders Carlsson76967372009-09-17 00:43:46 +00001229 return;
John McCallefe6aee2009-09-05 07:56:18 +00001230#define TYPE(CLASS, PARENT) \
Anders Carlsson76967372009-09-17 00:43:46 +00001231 case Type::CLASS: \
John McCallb47f7482011-01-26 20:05:40 +00001232 mangleType(static_cast<const CLASS##Type*>(ty)); \
Anders Carlsson76967372009-09-17 00:43:46 +00001233 break;
John McCallefe6aee2009-09-05 07:56:18 +00001234#include "clang/AST/TypeNodes.def"
Anders Carlsson76967372009-09-17 00:43:46 +00001235 }
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001236 }
Anders Carlsson76967372009-09-17 00:43:46 +00001237
1238 // Add the substitution.
John McCallb47f7482011-01-26 20:05:40 +00001239 if (isSubstitutable)
1240 addSubstitution(canon);
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001241}
1242
Douglas Gregor1b12a3b2010-05-26 05:11:13 +00001243void CXXNameMangler::mangleNameOrStandardSubstitution(const NamedDecl *ND) {
1244 if (!mangleStandardSubstitution(ND))
1245 mangleName(ND);
1246}
1247
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001248void CXXNameMangler::mangleType(const BuiltinType *T) {
John McCallefe6aee2009-09-05 07:56:18 +00001249 // <type> ::= <builtin-type>
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001250 // <builtin-type> ::= v # void
1251 // ::= w # wchar_t
1252 // ::= b # bool
1253 // ::= c # char
1254 // ::= a # signed char
1255 // ::= h # unsigned char
1256 // ::= s # short
1257 // ::= t # unsigned short
1258 // ::= i # int
1259 // ::= j # unsigned int
1260 // ::= l # long
1261 // ::= m # unsigned long
1262 // ::= x # long long, __int64
1263 // ::= y # unsigned long long, __int64
1264 // ::= n # __int128
1265 // UNSUPPORTED: ::= o # unsigned __int128
1266 // ::= f # float
1267 // ::= d # double
1268 // ::= e # long double, __float80
1269 // UNSUPPORTED: ::= g # __float128
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001270 // UNSUPPORTED: ::= Dd # IEEE 754r decimal floating point (64 bits)
1271 // UNSUPPORTED: ::= De # IEEE 754r decimal floating point (128 bits)
1272 // UNSUPPORTED: ::= Df # IEEE 754r decimal floating point (32 bits)
1273 // UNSUPPORTED: ::= Dh # IEEE 754r half-precision floating point (16 bits)
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001274 // ::= Di # char32_t
1275 // ::= Ds # char16_t
Anders Carlssone2923682010-11-04 04:31:32 +00001276 // ::= Dn # std::nullptr_t (i.e., decltype(nullptr))
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001277 // ::= u <source-name> # vendor extended type
1278 switch (T->getKind()) {
1279 case BuiltinType::Void: Out << 'v'; break;
1280 case BuiltinType::Bool: Out << 'b'; break;
1281 case BuiltinType::Char_U: case BuiltinType::Char_S: Out << 'c'; break;
1282 case BuiltinType::UChar: Out << 'h'; break;
1283 case BuiltinType::UShort: Out << 't'; break;
1284 case BuiltinType::UInt: Out << 'j'; break;
1285 case BuiltinType::ULong: Out << 'm'; break;
1286 case BuiltinType::ULongLong: Out << 'y'; break;
Chris Lattner2df9ced2009-04-30 02:43:43 +00001287 case BuiltinType::UInt128: Out << 'o'; break;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001288 case BuiltinType::SChar: Out << 'a'; break;
Chris Lattner3f59c972010-12-25 23:25:43 +00001289 case BuiltinType::WChar_S:
1290 case BuiltinType::WChar_U: Out << 'w'; break;
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001291 case BuiltinType::Char16: Out << "Ds"; break;
1292 case BuiltinType::Char32: Out << "Di"; break;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001293 case BuiltinType::Short: Out << 's'; break;
1294 case BuiltinType::Int: Out << 'i'; break;
1295 case BuiltinType::Long: Out << 'l'; break;
1296 case BuiltinType::LongLong: Out << 'x'; break;
Chris Lattner2df9ced2009-04-30 02:43:43 +00001297 case BuiltinType::Int128: Out << 'n'; break;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001298 case BuiltinType::Float: Out << 'f'; break;
1299 case BuiltinType::Double: Out << 'd'; break;
1300 case BuiltinType::LongDouble: Out << 'e'; break;
Anders Carlssone2923682010-11-04 04:31:32 +00001301 case BuiltinType::NullPtr: Out << "Dn"; break;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001302
1303 case BuiltinType::Overload:
1304 case BuiltinType::Dependent:
Mike Stump1eb44332009-09-09 15:08:12 +00001305 assert(false &&
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001306 "Overloaded and dependent types shouldn't get to name mangling");
1307 break;
Anders Carlssone89d1592009-06-26 18:41:36 +00001308 case BuiltinType::UndeducedAuto:
1309 assert(0 && "Should not see undeduced auto here");
1310 break;
Steve Naroff9533a7f2009-07-22 17:14:51 +00001311 case BuiltinType::ObjCId: Out << "11objc_object"; break;
1312 case BuiltinType::ObjCClass: Out << "10objc_class"; break;
Fariborz Jahanian13dcd002009-11-21 19:53:08 +00001313 case BuiltinType::ObjCSel: Out << "13objc_selector"; break;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001314 }
1315}
1316
John McCallefe6aee2009-09-05 07:56:18 +00001317// <type> ::= <function-type>
1318// <function-type> ::= F [Y] <bare-function-type> E
1319void CXXNameMangler::mangleType(const FunctionProtoType *T) {
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001320 Out << 'F';
Mike Stumpf5408fe2009-05-16 07:57:57 +00001321 // FIXME: We don't have enough information in the AST to produce the 'Y'
1322 // encoding for extern "C" function types.
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001323 mangleBareFunctionType(T, /*MangleReturnType=*/true);
1324 Out << 'E';
1325}
John McCallefe6aee2009-09-05 07:56:18 +00001326void CXXNameMangler::mangleType(const FunctionNoProtoType *T) {
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +00001327 llvm_unreachable("Can't mangle K&R function prototypes");
John McCallefe6aee2009-09-05 07:56:18 +00001328}
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001329void CXXNameMangler::mangleBareFunctionType(const FunctionType *T,
1330 bool MangleReturnType) {
John McCallefe6aee2009-09-05 07:56:18 +00001331 // We should never be mangling something without a prototype.
1332 const FunctionProtoType *Proto = cast<FunctionProtoType>(T);
1333
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001334 // <bare-function-type> ::= <signature type>+
1335 if (MangleReturnType)
John McCallefe6aee2009-09-05 07:56:18 +00001336 mangleType(Proto->getResultType());
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001337
Anders Carlsson93296682010-06-02 04:40:13 +00001338 if (Proto->getNumArgs() == 0 && !Proto->isVariadic()) {
Eli Friedmana7e68452010-08-22 01:00:03 +00001339 // <builtin-type> ::= v # void
Anders Carlssonc6c91bc2009-04-01 00:15:23 +00001340 Out << 'v';
1341 return;
1342 }
Mike Stump1eb44332009-09-09 15:08:12 +00001343
Douglas Gregor72564e72009-02-26 23:50:07 +00001344 for (FunctionProtoType::arg_type_iterator Arg = Proto->arg_type_begin(),
Mike Stump1eb44332009-09-09 15:08:12 +00001345 ArgEnd = Proto->arg_type_end();
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001346 Arg != ArgEnd; ++Arg)
1347 mangleType(*Arg);
Douglas Gregor219cc612009-02-13 01:28:03 +00001348
1349 // <builtin-type> ::= z # ellipsis
1350 if (Proto->isVariadic())
1351 Out << 'z';
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001352}
1353
John McCallefe6aee2009-09-05 07:56:18 +00001354// <type> ::= <class-enum-type>
Mike Stump1eb44332009-09-09 15:08:12 +00001355// <class-enum-type> ::= <name>
John McCalled976492009-12-04 22:46:56 +00001356void CXXNameMangler::mangleType(const UnresolvedUsingType *T) {
1357 mangleName(T->getDecl());
1358}
1359
1360// <type> ::= <class-enum-type>
1361// <class-enum-type> ::= <name>
John McCallefe6aee2009-09-05 07:56:18 +00001362void CXXNameMangler::mangleType(const EnumType *T) {
1363 mangleType(static_cast<const TagType*>(T));
1364}
1365void CXXNameMangler::mangleType(const RecordType *T) {
1366 mangleType(static_cast<const TagType*>(T));
1367}
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001368void CXXNameMangler::mangleType(const TagType *T) {
Eli Friedmanecb7e932009-12-11 18:00:57 +00001369 mangleName(T->getDecl());
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001370}
1371
John McCallefe6aee2009-09-05 07:56:18 +00001372// <type> ::= <array-type>
1373// <array-type> ::= A <positive dimension number> _ <element type>
1374// ::= A [<dimension expression>] _ <element type>
1375void CXXNameMangler::mangleType(const ConstantArrayType *T) {
1376 Out << 'A' << T->getSize() << '_';
1377 mangleType(T->getElementType());
1378}
1379void CXXNameMangler::mangleType(const VariableArrayType *T) {
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001380 Out << 'A';
Fariborz Jahanian7281d1f2010-11-02 16:54:00 +00001381 // decayed vla types (size 0) will just be skipped.
1382 if (T->getSizeExpr())
1383 mangleExpression(T->getSizeExpr());
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001384 Out << '_';
1385 mangleType(T->getElementType());
1386}
John McCallefe6aee2009-09-05 07:56:18 +00001387void CXXNameMangler::mangleType(const DependentSizedArrayType *T) {
1388 Out << 'A';
1389 mangleExpression(T->getSizeExpr());
1390 Out << '_';
1391 mangleType(T->getElementType());
1392}
1393void CXXNameMangler::mangleType(const IncompleteArrayType *T) {
Nick Lewycky271b6652010-09-05 03:40:33 +00001394 Out << "A_";
John McCallefe6aee2009-09-05 07:56:18 +00001395 mangleType(T->getElementType());
1396}
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001397
John McCallefe6aee2009-09-05 07:56:18 +00001398// <type> ::= <pointer-to-member-type>
1399// <pointer-to-member-type> ::= M <class type> <member type>
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001400void CXXNameMangler::mangleType(const MemberPointerType *T) {
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001401 Out << 'M';
1402 mangleType(QualType(T->getClass(), 0));
Anders Carlsson0e650012009-05-17 17:41:20 +00001403 QualType PointeeType = T->getPointeeType();
1404 if (const FunctionProtoType *FPT = dyn_cast<FunctionProtoType>(PointeeType)) {
John McCall0953e762009-09-24 19:53:00 +00001405 mangleQualifiers(Qualifiers::fromCVRMask(FPT->getTypeQuals()));
Douglas Gregor0a9a6d62011-01-26 17:36:28 +00001406 mangleRefQualifier(FPT->getRefQualifier());
Anders Carlsson0e650012009-05-17 17:41:20 +00001407 mangleType(FPT);
Anders Carlsson9d85b722010-06-02 04:29:50 +00001408
1409 // Itanium C++ ABI 5.1.8:
1410 //
1411 // The type of a non-static member function is considered to be different,
1412 // for the purposes of substitution, from the type of a namespace-scope or
1413 // static member function whose type appears similar. The types of two
1414 // non-static member functions are considered to be different, for the
1415 // purposes of substitution, if the functions are members of different
1416 // classes. In other words, for the purposes of substitution, the class of
1417 // which the function is a member is considered part of the type of
1418 // function.
1419
1420 // We increment the SeqID here to emulate adding an entry to the
1421 // substitution table. We can't actually add it because we don't want this
1422 // particular function type to be substituted.
1423 ++SeqID;
Mike Stump1eb44332009-09-09 15:08:12 +00001424 } else
Anders Carlsson0e650012009-05-17 17:41:20 +00001425 mangleType(PointeeType);
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001426}
1427
John McCallefe6aee2009-09-05 07:56:18 +00001428// <type> ::= <template-param>
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001429void CXXNameMangler::mangleType(const TemplateTypeParmType *T) {
Anders Carlsson0ccdf8d2009-09-27 00:38:53 +00001430 mangleTemplateParameter(T->getIndex());
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001431}
1432
Douglas Gregorc3069d62011-01-14 02:55:32 +00001433// <type> ::= <template-param>
1434void CXXNameMangler::mangleType(const SubstTemplateTypeParmPackType *T) {
1435 mangleTemplateParameter(T->getReplacedParameter()->getIndex());
1436}
1437
John McCallefe6aee2009-09-05 07:56:18 +00001438// <type> ::= P <type> # pointer-to
1439void CXXNameMangler::mangleType(const PointerType *T) {
1440 Out << 'P';
1441 mangleType(T->getPointeeType());
1442}
1443void CXXNameMangler::mangleType(const ObjCObjectPointerType *T) {
1444 Out << 'P';
1445 mangleType(T->getPointeeType());
1446}
1447
1448// <type> ::= R <type> # reference-to
1449void CXXNameMangler::mangleType(const LValueReferenceType *T) {
1450 Out << 'R';
1451 mangleType(T->getPointeeType());
1452}
1453
1454// <type> ::= O <type> # rvalue reference-to (C++0x)
1455void CXXNameMangler::mangleType(const RValueReferenceType *T) {
1456 Out << 'O';
1457 mangleType(T->getPointeeType());
1458}
1459
1460// <type> ::= C <type> # complex pair (C 2000)
1461void CXXNameMangler::mangleType(const ComplexType *T) {
1462 Out << 'C';
1463 mangleType(T->getElementType());
1464}
1465
Bob Wilsonc7df92d2010-11-12 17:24:43 +00001466// ARM's ABI for Neon vector types specifies that they should be mangled as
Bob Wilson57147a82010-11-16 00:32:18 +00001467// if they are structs (to match ARM's initial implementation). The
1468// vector type must be one of the special types predefined by ARM.
1469void CXXNameMangler::mangleNeonVectorType(const VectorType *T) {
Bob Wilsonc7df92d2010-11-12 17:24:43 +00001470 QualType EltType = T->getElementType();
Bob Wilson57147a82010-11-16 00:32:18 +00001471 assert(EltType->isBuiltinType() && "Neon vector element not a BuiltinType");
Bob Wilsonc7df92d2010-11-12 17:24:43 +00001472 const char *EltName = 0;
Bob Wilson491328c2010-11-12 17:24:46 +00001473 if (T->getVectorKind() == VectorType::NeonPolyVector) {
1474 switch (cast<BuiltinType>(EltType)->getKind()) {
Bob Wilson4cfaa5d2010-11-12 17:24:49 +00001475 case BuiltinType::SChar: EltName = "poly8_t"; break;
1476 case BuiltinType::Short: EltName = "poly16_t"; break;
Bob Wilson57147a82010-11-16 00:32:18 +00001477 default: llvm_unreachable("unexpected Neon polynomial vector element type");
Bob Wilson491328c2010-11-12 17:24:46 +00001478 }
1479 } else {
1480 switch (cast<BuiltinType>(EltType)->getKind()) {
Bob Wilson4cfaa5d2010-11-12 17:24:49 +00001481 case BuiltinType::SChar: EltName = "int8_t"; break;
1482 case BuiltinType::UChar: EltName = "uint8_t"; break;
1483 case BuiltinType::Short: EltName = "int16_t"; break;
1484 case BuiltinType::UShort: EltName = "uint16_t"; break;
1485 case BuiltinType::Int: EltName = "int32_t"; break;
1486 case BuiltinType::UInt: EltName = "uint32_t"; break;
1487 case BuiltinType::LongLong: EltName = "int64_t"; break;
1488 case BuiltinType::ULongLong: EltName = "uint64_t"; break;
1489 case BuiltinType::Float: EltName = "float32_t"; break;
Bob Wilson57147a82010-11-16 00:32:18 +00001490 default: llvm_unreachable("unexpected Neon vector element type");
Bob Wilson491328c2010-11-12 17:24:46 +00001491 }
Bob Wilsonc7df92d2010-11-12 17:24:43 +00001492 }
1493 const char *BaseName = 0;
Bob Wilson4cfaa5d2010-11-12 17:24:49 +00001494 unsigned BitSize = (T->getNumElements() *
Bob Wilson3a723022010-11-16 00:32:12 +00001495 getASTContext().getTypeSize(EltType));
Bob Wilsonc7df92d2010-11-12 17:24:43 +00001496 if (BitSize == 64)
1497 BaseName = "__simd64_";
Bob Wilson57147a82010-11-16 00:32:18 +00001498 else {
1499 assert(BitSize == 128 && "Neon vector type not 64 or 128 bits");
Bob Wilsonc7df92d2010-11-12 17:24:43 +00001500 BaseName = "__simd128_";
Bob Wilson57147a82010-11-16 00:32:18 +00001501 }
Bob Wilsonc7df92d2010-11-12 17:24:43 +00001502 Out << strlen(BaseName) + strlen(EltName);
1503 Out << BaseName << EltName;
Bob Wilsonc7df92d2010-11-12 17:24:43 +00001504}
1505
John McCallefe6aee2009-09-05 07:56:18 +00001506// GNU extension: vector types
Chris Lattner788b0fd2010-06-23 06:00:24 +00001507// <type> ::= <vector-type>
1508// <vector-type> ::= Dv <positive dimension number> _
1509// <extended element type>
1510// ::= Dv [<dimension expression>] _ <element type>
1511// <extended element type> ::= <element type>
1512// ::= p # AltiVec vector pixel
John McCallefe6aee2009-09-05 07:56:18 +00001513void CXXNameMangler::mangleType(const VectorType *T) {
Bob Wilson491328c2010-11-12 17:24:46 +00001514 if ((T->getVectorKind() == VectorType::NeonVector ||
Bob Wilson57147a82010-11-16 00:32:18 +00001515 T->getVectorKind() == VectorType::NeonPolyVector)) {
1516 mangleNeonVectorType(T);
Bob Wilsonc7df92d2010-11-12 17:24:43 +00001517 return;
Bob Wilson57147a82010-11-16 00:32:18 +00001518 }
Nick Lewycky0e5f0672010-03-26 07:18:04 +00001519 Out << "Dv" << T->getNumElements() << '_';
Bob Wilsone86d78c2010-11-10 21:56:12 +00001520 if (T->getVectorKind() == VectorType::AltiVecPixel)
Chris Lattner788b0fd2010-06-23 06:00:24 +00001521 Out << 'p';
Bob Wilsone86d78c2010-11-10 21:56:12 +00001522 else if (T->getVectorKind() == VectorType::AltiVecBool)
Chris Lattner788b0fd2010-06-23 06:00:24 +00001523 Out << 'b';
1524 else
1525 mangleType(T->getElementType());
John McCallefe6aee2009-09-05 07:56:18 +00001526}
1527void CXXNameMangler::mangleType(const ExtVectorType *T) {
1528 mangleType(static_cast<const VectorType*>(T));
1529}
1530void CXXNameMangler::mangleType(const DependentSizedExtVectorType *T) {
Nick Lewycky0e5f0672010-03-26 07:18:04 +00001531 Out << "Dv";
1532 mangleExpression(T->getSizeExpr());
1533 Out << '_';
John McCallefe6aee2009-09-05 07:56:18 +00001534 mangleType(T->getElementType());
1535}
1536
Douglas Gregor7536dd52010-12-20 02:24:11 +00001537void CXXNameMangler::mangleType(const PackExpansionType *T) {
Douglas Gregor4fc48662011-01-13 16:39:34 +00001538 // <type> ::= Dp <type> # pack expansion (C++0x)
Douglas Gregor255c2692011-01-13 17:44:36 +00001539 Out << "Dp";
Douglas Gregor7536dd52010-12-20 02:24:11 +00001540 mangleType(T->getPattern());
1541}
1542
Anders Carlssona40c5e42009-03-07 22:03:21 +00001543void CXXNameMangler::mangleType(const ObjCInterfaceType *T) {
1544 mangleSourceName(T->getDecl()->getIdentifier());
1545}
1546
John McCallc12c5bb2010-05-15 11:32:37 +00001547void CXXNameMangler::mangleType(const ObjCObjectType *T) {
John McCallc00c1f62010-05-15 17:06:29 +00001548 // We don't allow overloading by different protocol qualification,
1549 // so mangling them isn't necessary.
John McCallc12c5bb2010-05-15 11:32:37 +00001550 mangleType(T->getBaseType());
1551}
1552
John McCallefe6aee2009-09-05 07:56:18 +00001553void CXXNameMangler::mangleType(const BlockPointerType *T) {
Anders Carlssonf28c6872009-12-23 22:31:44 +00001554 Out << "U13block_pointer";
1555 mangleType(T->getPointeeType());
John McCallefe6aee2009-09-05 07:56:18 +00001556}
1557
John McCall31f17ec2010-04-27 00:57:59 +00001558void CXXNameMangler::mangleType(const InjectedClassNameType *T) {
1559 // Mangle injected class name types as if the user had written the
1560 // specialization out fully. It may not actually be possible to see
1561 // this mangling, though.
1562 mangleType(T->getInjectedSpecializationType());
1563}
1564
John McCallefe6aee2009-09-05 07:56:18 +00001565void CXXNameMangler::mangleType(const TemplateSpecializationType *T) {
Douglas Gregor1e9268e2010-04-28 05:58:56 +00001566 if (TemplateDecl *TD = T->getTemplateName().getAsTemplateDecl()) {
1567 mangleName(TD, T->getArgs(), T->getNumArgs());
1568 } else {
1569 if (mangleSubstitution(QualType(T, 0)))
1570 return;
Sean Huntc3021132010-05-05 15:23:54 +00001571
Douglas Gregor1e9268e2010-04-28 05:58:56 +00001572 mangleTemplatePrefix(T->getTemplateName());
Sean Huntc3021132010-05-05 15:23:54 +00001573
Douglas Gregor1e9268e2010-04-28 05:58:56 +00001574 // FIXME: GCC does not appear to mangle the template arguments when
1575 // the template in question is a dependent template name. Should we
1576 // emulate that badness?
1577 mangleTemplateArgs(T->getTemplateName(), T->getArgs(), T->getNumArgs());
1578 addSubstitution(QualType(T, 0));
1579 }
John McCallefe6aee2009-09-05 07:56:18 +00001580}
1581
Douglas Gregor4714c122010-03-31 17:34:00 +00001582void CXXNameMangler::mangleType(const DependentNameType *T) {
Anders Carlssonae352482009-09-26 02:26:02 +00001583 // Typename types are always nested
1584 Out << 'N';
John McCall33500952010-06-11 00:33:02 +00001585 mangleUnresolvedScope(T->getQualifier());
1586 mangleSourceName(T->getIdentifier());
1587 Out << 'E';
1588}
John McCall6ab30e02010-06-09 07:26:17 +00001589
John McCall33500952010-06-11 00:33:02 +00001590void CXXNameMangler::mangleType(const DependentTemplateSpecializationType *T) {
1591 // Dependently-scoped template types are always nested
1592 Out << 'N';
1593
1594 // TODO: avoid making this TemplateName.
1595 TemplateName Prefix =
1596 getASTContext().getDependentTemplateName(T->getQualifier(),
1597 T->getIdentifier());
1598 mangleTemplatePrefix(Prefix);
1599
1600 // FIXME: GCC does not appear to mangle the template arguments when
1601 // the template in question is a dependent template name. Should we
1602 // emulate that badness?
1603 mangleTemplateArgs(Prefix, T->getArgs(), T->getNumArgs());
Anders Carlssonae352482009-09-26 02:26:02 +00001604 Out << 'E';
John McCallefe6aee2009-09-05 07:56:18 +00001605}
1606
John McCallad5e7382010-03-01 23:49:17 +00001607void CXXNameMangler::mangleType(const TypeOfType *T) {
1608 // FIXME: this is pretty unsatisfactory, but there isn't an obvious
1609 // "extension with parameters" mangling.
1610 Out << "u6typeof";
1611}
1612
1613void CXXNameMangler::mangleType(const TypeOfExprType *T) {
1614 // FIXME: this is pretty unsatisfactory, but there isn't an obvious
1615 // "extension with parameters" mangling.
1616 Out << "u6typeof";
1617}
1618
1619void CXXNameMangler::mangleType(const DecltypeType *T) {
1620 Expr *E = T->getUnderlyingExpr();
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001621
John McCallad5e7382010-03-01 23:49:17 +00001622 // type ::= Dt <expression> E # decltype of an id-expression
1623 // # or class member access
1624 // ::= DT <expression> E # decltype of an expression
1625
1626 // This purports to be an exhaustive list of id-expressions and
1627 // class member accesses. Note that we do not ignore parentheses;
1628 // parentheses change the semantics of decltype for these
1629 // expressions (and cause the mangler to use the other form).
1630 if (isa<DeclRefExpr>(E) ||
1631 isa<MemberExpr>(E) ||
1632 isa<UnresolvedLookupExpr>(E) ||
1633 isa<DependentScopeDeclRefExpr>(E) ||
1634 isa<CXXDependentScopeMemberExpr>(E) ||
1635 isa<UnresolvedMemberExpr>(E))
1636 Out << "Dt";
1637 else
1638 Out << "DT";
1639 mangleExpression(E);
1640 Out << 'E';
1641}
1642
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001643void CXXNameMangler::mangleIntegerLiteral(QualType T,
Anders Carlssone170ba72009-12-14 01:45:37 +00001644 const llvm::APSInt &Value) {
1645 // <expr-primary> ::= L <type> <value number> E # integer literal
1646 Out << 'L';
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001647
Anders Carlssone170ba72009-12-14 01:45:37 +00001648 mangleType(T);
1649 if (T->isBooleanType()) {
1650 // Boolean values are encoded as 0/1.
1651 Out << (Value.getBoolValue() ? '1' : '0');
1652 } else {
John McCall0512e482010-07-14 04:20:34 +00001653 mangleNumber(Value);
Anders Carlssone170ba72009-12-14 01:45:37 +00001654 }
1655 Out << 'E';
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001656
Anders Carlssone170ba72009-12-14 01:45:37 +00001657}
1658
John McCall2f27bf82010-02-04 02:56:29 +00001659/// Mangles a member expression. Implicit accesses are not handled,
1660/// but that should be okay, because you shouldn't be able to
1661/// make an implicit access in a function template declaration.
John McCall2f27bf82010-02-04 02:56:29 +00001662void CXXNameMangler::mangleMemberExpr(const Expr *Base,
1663 bool IsArrow,
1664 NestedNameSpecifier *Qualifier,
1665 DeclarationName Member,
1666 unsigned Arity) {
John McCalle1e342f2010-03-01 19:12:25 +00001667 // gcc-4.4 uses 'dt' for dot expressions, which is reasonable.
1668 // OTOH, gcc also mangles the name as an expression.
1669 Out << (IsArrow ? "pt" : "dt");
John McCall2f27bf82010-02-04 02:56:29 +00001670 mangleExpression(Base);
1671 mangleUnresolvedName(Qualifier, Member, Arity);
1672}
1673
John McCall5e1e89b2010-08-18 19:18:59 +00001674void CXXNameMangler::mangleExpression(const Expr *E, unsigned Arity) {
Anders Carlssond553f8c2009-09-21 01:21:10 +00001675 // <expression> ::= <unary operator-name> <expression>
John McCall09cc1412010-02-03 00:55:45 +00001676 // ::= <binary operator-name> <expression> <expression>
1677 // ::= <trinary operator-name> <expression> <expression> <expression>
Eli Friedmana7e68452010-08-22 01:00:03 +00001678 // ::= cl <expression>* E # call
Anders Carlssond553f8c2009-09-21 01:21:10 +00001679 // ::= cv <type> expression # conversion with one argument
1680 // ::= cv <type> _ <expression>* E # conversion with a different number of arguments
Eli Friedmana7e68452010-08-22 01:00:03 +00001681 // ::= st <type> # sizeof (a type)
Anders Carlssond553f8c2009-09-21 01:21:10 +00001682 // ::= at <type> # alignof (a type)
1683 // ::= <template-param>
1684 // ::= <function-param>
1685 // ::= sr <type> <unqualified-name> # dependent name
1686 // ::= sr <type> <unqualified-name> <template-args> # dependent template-id
1687 // ::= sZ <template-param> # size of a parameter pack
Douglas Gregor4fc48662011-01-13 16:39:34 +00001688 // ::= sZ <function-param> # size of a function parameter pack
John McCall09cc1412010-02-03 00:55:45 +00001689 // ::= <expr-primary>
John McCall1dd73832010-02-04 01:42:13 +00001690 // <expr-primary> ::= L <type> <value number> E # integer literal
1691 // ::= L <type <value float> E # floating literal
1692 // ::= L <mangled-name> E # external name
Anders Carlssond553f8c2009-09-21 01:21:10 +00001693 switch (E->getStmtClass()) {
John McCall6ae1f352010-04-09 22:26:14 +00001694 case Expr::NoStmtClass:
John McCall63c00d72011-02-09 08:16:59 +00001695#define ABSTRACT_STMT(Type)
John McCall6ae1f352010-04-09 22:26:14 +00001696#define EXPR(Type, Base)
1697#define STMT(Type, Base) \
1698 case Expr::Type##Class:
Sean Hunt4bfe1962010-05-05 15:24:00 +00001699#include "clang/AST/StmtNodes.inc"
John McCall0512e482010-07-14 04:20:34 +00001700 // fallthrough
1701
1702 // These all can only appear in local or variable-initialization
1703 // contexts and so should never appear in a mangling.
1704 case Expr::AddrLabelExprClass:
1705 case Expr::BlockDeclRefExprClass:
1706 case Expr::CXXThisExprClass:
1707 case Expr::DesignatedInitExprClass:
1708 case Expr::ImplicitValueInitExprClass:
1709 case Expr::InitListExprClass:
1710 case Expr::ParenListExprClass:
1711 case Expr::CXXScalarValueInitExprClass:
John McCall09cc1412010-02-03 00:55:45 +00001712 llvm_unreachable("unexpected statement kind");
1713 break;
1714
John McCall0512e482010-07-14 04:20:34 +00001715 // FIXME: invent manglings for all these.
1716 case Expr::BlockExprClass:
1717 case Expr::CXXPseudoDestructorExprClass:
1718 case Expr::ChooseExprClass:
1719 case Expr::CompoundLiteralExprClass:
1720 case Expr::ExtVectorElementExprClass:
1721 case Expr::ObjCEncodeExprClass:
John McCall0512e482010-07-14 04:20:34 +00001722 case Expr::ObjCIsaExprClass:
1723 case Expr::ObjCIvarRefExprClass:
1724 case Expr::ObjCMessageExprClass:
1725 case Expr::ObjCPropertyRefExprClass:
1726 case Expr::ObjCProtocolExprClass:
1727 case Expr::ObjCSelectorExprClass:
1728 case Expr::ObjCStringLiteralClass:
John McCall0512e482010-07-14 04:20:34 +00001729 case Expr::OffsetOfExprClass:
1730 case Expr::PredefinedExprClass:
1731 case Expr::ShuffleVectorExprClass:
1732 case Expr::StmtExprClass:
John McCall0512e482010-07-14 04:20:34 +00001733 case Expr::UnaryTypeTraitExprClass:
Francois Pichet6ad6f282010-12-07 00:08:36 +00001734 case Expr::BinaryTypeTraitExprClass:
Francois Pichet9be88402010-09-08 23:47:05 +00001735 case Expr::VAArgExprClass:
Sebastian Redl2e156222010-09-10 20:55:43 +00001736 case Expr::CXXUuidofExprClass:
Peter Collingbournee08ce652011-02-09 21:07:24 +00001737 case Expr::CXXNoexceptExprClass:
1738 case Expr::CUDAKernelCallExprClass: {
John McCall6ae1f352010-04-09 22:26:14 +00001739 // As bad as this diagnostic is, it's better than crashing.
1740 Diagnostic &Diags = Context.getDiags();
1741 unsigned DiagID = Diags.getCustomDiagID(Diagnostic::Error,
1742 "cannot yet mangle expression type %0");
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +00001743 Diags.Report(E->getExprLoc(), DiagID)
John McCall739bf092010-04-10 09:39:25 +00001744 << E->getStmtClassName() << E->getSourceRange();
John McCall6ae1f352010-04-09 22:26:14 +00001745 break;
1746 }
1747
John McCall7cd7d1a2010-11-15 23:31:06 +00001748 case Expr::OpaqueValueExprClass:
1749 llvm_unreachable("cannot mangle opaque value; mangling wrong thing?");
1750
John McCall0512e482010-07-14 04:20:34 +00001751 case Expr::CXXDefaultArgExprClass:
John McCall5e1e89b2010-08-18 19:18:59 +00001752 mangleExpression(cast<CXXDefaultArgExpr>(E)->getExpr(), Arity);
John McCall0512e482010-07-14 04:20:34 +00001753 break;
1754
1755 case Expr::CXXMemberCallExprClass: // fallthrough
John McCall1dd73832010-02-04 01:42:13 +00001756 case Expr::CallExprClass: {
1757 const CallExpr *CE = cast<CallExpr>(E);
1758 Out << "cl";
John McCall5e1e89b2010-08-18 19:18:59 +00001759 mangleExpression(CE->getCallee(), CE->getNumArgs());
John McCall1dd73832010-02-04 01:42:13 +00001760 for (unsigned I = 0, N = CE->getNumArgs(); I != N; ++I)
1761 mangleExpression(CE->getArg(I));
Benjamin Kramer35f59b62010-04-10 16:03:31 +00001762 Out << 'E';
John McCall09cc1412010-02-03 00:55:45 +00001763 break;
John McCall1dd73832010-02-04 01:42:13 +00001764 }
John McCall09cc1412010-02-03 00:55:45 +00001765
John McCall0512e482010-07-14 04:20:34 +00001766 case Expr::CXXNewExprClass: {
1767 // Proposal from David Vandervoorde, 2010.06.30
1768 const CXXNewExpr *New = cast<CXXNewExpr>(E);
1769 if (New->isGlobalNew()) Out << "gs";
1770 Out << (New->isArray() ? "na" : "nw");
1771 for (CXXNewExpr::const_arg_iterator I = New->placement_arg_begin(),
1772 E = New->placement_arg_end(); I != E; ++I)
1773 mangleExpression(*I);
1774 Out << '_';
1775 mangleType(New->getAllocatedType());
1776 if (New->hasInitializer()) {
1777 Out << "pi";
1778 for (CXXNewExpr::const_arg_iterator I = New->constructor_arg_begin(),
1779 E = New->constructor_arg_end(); I != E; ++I)
1780 mangleExpression(*I);
1781 }
1782 Out << 'E';
1783 break;
1784 }
1785
John McCall2f27bf82010-02-04 02:56:29 +00001786 case Expr::MemberExprClass: {
1787 const MemberExpr *ME = cast<MemberExpr>(E);
1788 mangleMemberExpr(ME->getBase(), ME->isArrow(),
1789 ME->getQualifier(), ME->getMemberDecl()->getDeclName(),
John McCall5e1e89b2010-08-18 19:18:59 +00001790 Arity);
John McCall2f27bf82010-02-04 02:56:29 +00001791 break;
1792 }
1793
1794 case Expr::UnresolvedMemberExprClass: {
1795 const UnresolvedMemberExpr *ME = cast<UnresolvedMemberExpr>(E);
1796 mangleMemberExpr(ME->getBase(), ME->isArrow(),
1797 ME->getQualifier(), ME->getMemberName(),
John McCall5e1e89b2010-08-18 19:18:59 +00001798 Arity);
John McCall6dbce192010-08-20 00:17:19 +00001799 if (ME->hasExplicitTemplateArgs())
1800 mangleTemplateArgs(ME->getExplicitTemplateArgs());
John McCall2f27bf82010-02-04 02:56:29 +00001801 break;
1802 }
1803
1804 case Expr::CXXDependentScopeMemberExprClass: {
1805 const CXXDependentScopeMemberExpr *ME
1806 = cast<CXXDependentScopeMemberExpr>(E);
1807 mangleMemberExpr(ME->getBase(), ME->isArrow(),
1808 ME->getQualifier(), ME->getMember(),
John McCall5e1e89b2010-08-18 19:18:59 +00001809 Arity);
John McCall6dbce192010-08-20 00:17:19 +00001810 if (ME->hasExplicitTemplateArgs())
1811 mangleTemplateArgs(ME->getExplicitTemplateArgs());
John McCall2f27bf82010-02-04 02:56:29 +00001812 break;
1813 }
1814
John McCall1dd73832010-02-04 01:42:13 +00001815 case Expr::UnresolvedLookupExprClass: {
John McCalla3218e72010-02-04 01:48:38 +00001816 // The ABI doesn't cover how to mangle overload sets, so we mangle
1817 // using something as close as possible to the original lookup
1818 // expression.
John McCall1dd73832010-02-04 01:42:13 +00001819 const UnresolvedLookupExpr *ULE = cast<UnresolvedLookupExpr>(E);
John McCall5e1e89b2010-08-18 19:18:59 +00001820 mangleUnresolvedName(ULE->getQualifier(), ULE->getName(), Arity);
John McCall6dbce192010-08-20 00:17:19 +00001821 if (ULE->hasExplicitTemplateArgs())
1822 mangleTemplateArgs(ULE->getExplicitTemplateArgs());
John McCall1dd73832010-02-04 01:42:13 +00001823 break;
1824 }
1825
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001826 case Expr::CXXUnresolvedConstructExprClass: {
John McCall1dd73832010-02-04 01:42:13 +00001827 const CXXUnresolvedConstructExpr *CE = cast<CXXUnresolvedConstructExpr>(E);
1828 unsigned N = CE->arg_size();
1829
1830 Out << "cv";
1831 mangleType(CE->getType());
Benjamin Kramer35f59b62010-04-10 16:03:31 +00001832 if (N != 1) Out << '_';
John McCall1dd73832010-02-04 01:42:13 +00001833 for (unsigned I = 0; I != N; ++I) mangleExpression(CE->getArg(I));
Benjamin Kramer35f59b62010-04-10 16:03:31 +00001834 if (N != 1) Out << 'E';
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001835 break;
John McCall1dd73832010-02-04 01:42:13 +00001836 }
John McCall09cc1412010-02-03 00:55:45 +00001837
John McCall1dd73832010-02-04 01:42:13 +00001838 case Expr::CXXTemporaryObjectExprClass:
1839 case Expr::CXXConstructExprClass: {
1840 const CXXConstructExpr *CE = cast<CXXConstructExpr>(E);
1841 unsigned N = CE->getNumArgs();
1842
1843 Out << "cv";
1844 mangleType(CE->getType());
Benjamin Kramer35f59b62010-04-10 16:03:31 +00001845 if (N != 1) Out << '_';
John McCall1dd73832010-02-04 01:42:13 +00001846 for (unsigned I = 0; I != N; ++I) mangleExpression(CE->getArg(I));
Benjamin Kramer35f59b62010-04-10 16:03:31 +00001847 if (N != 1) Out << 'E';
John McCall09cc1412010-02-03 00:55:45 +00001848 break;
John McCall1dd73832010-02-04 01:42:13 +00001849 }
1850
1851 case Expr::SizeOfAlignOfExprClass: {
1852 const SizeOfAlignOfExpr *SAE = cast<SizeOfAlignOfExpr>(E);
Benjamin Kramer35f59b62010-04-10 16:03:31 +00001853 if (SAE->isSizeOf()) Out << 's';
1854 else Out << 'a';
John McCall1dd73832010-02-04 01:42:13 +00001855 if (SAE->isArgumentType()) {
Benjamin Kramer35f59b62010-04-10 16:03:31 +00001856 Out << 't';
John McCall1dd73832010-02-04 01:42:13 +00001857 mangleType(SAE->getArgumentType());
1858 } else {
Benjamin Kramer35f59b62010-04-10 16:03:31 +00001859 Out << 'z';
John McCall1dd73832010-02-04 01:42:13 +00001860 mangleExpression(SAE->getArgumentExpr());
1861 }
1862 break;
1863 }
Anders Carlssona7694082009-11-06 02:50:19 +00001864
John McCall0512e482010-07-14 04:20:34 +00001865 case Expr::CXXThrowExprClass: {
1866 const CXXThrowExpr *TE = cast<CXXThrowExpr>(E);
1867
1868 // Proposal from David Vandervoorde, 2010.06.30
1869 if (TE->getSubExpr()) {
1870 Out << "tw";
1871 mangleExpression(TE->getSubExpr());
1872 } else {
1873 Out << "tr";
1874 }
1875 break;
1876 }
1877
1878 case Expr::CXXTypeidExprClass: {
1879 const CXXTypeidExpr *TIE = cast<CXXTypeidExpr>(E);
1880
1881 // Proposal from David Vandervoorde, 2010.06.30
1882 if (TIE->isTypeOperand()) {
1883 Out << "ti";
1884 mangleType(TIE->getTypeOperand());
1885 } else {
1886 Out << "te";
1887 mangleExpression(TIE->getExprOperand());
1888 }
1889 break;
1890 }
1891
1892 case Expr::CXXDeleteExprClass: {
1893 const CXXDeleteExpr *DE = cast<CXXDeleteExpr>(E);
1894
1895 // Proposal from David Vandervoorde, 2010.06.30
1896 if (DE->isGlobalDelete()) Out << "gs";
1897 Out << (DE->isArrayForm() ? "da" : "dl");
1898 mangleExpression(DE->getArgument());
1899 break;
1900 }
1901
Anders Carlssone170ba72009-12-14 01:45:37 +00001902 case Expr::UnaryOperatorClass: {
1903 const UnaryOperator *UO = cast<UnaryOperator>(E);
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001904 mangleOperatorName(UnaryOperator::getOverloadedOperator(UO->getOpcode()),
Anders Carlssone170ba72009-12-14 01:45:37 +00001905 /*Arity=*/1);
1906 mangleExpression(UO->getSubExpr());
1907 break;
1908 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001909
John McCall0512e482010-07-14 04:20:34 +00001910 case Expr::ArraySubscriptExprClass: {
1911 const ArraySubscriptExpr *AE = cast<ArraySubscriptExpr>(E);
1912
1913 // Array subscript is treated as a syntactically wierd form of
1914 // binary operator.
1915 Out << "ix";
1916 mangleExpression(AE->getLHS());
1917 mangleExpression(AE->getRHS());
1918 break;
1919 }
1920
1921 case Expr::CompoundAssignOperatorClass: // fallthrough
Anders Carlssone170ba72009-12-14 01:45:37 +00001922 case Expr::BinaryOperatorClass: {
1923 const BinaryOperator *BO = cast<BinaryOperator>(E);
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001924 mangleOperatorName(BinaryOperator::getOverloadedOperator(BO->getOpcode()),
Anders Carlssone170ba72009-12-14 01:45:37 +00001925 /*Arity=*/2);
1926 mangleExpression(BO->getLHS());
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001927 mangleExpression(BO->getRHS());
Anders Carlssone170ba72009-12-14 01:45:37 +00001928 break;
John McCall2f27bf82010-02-04 02:56:29 +00001929 }
Anders Carlssone170ba72009-12-14 01:45:37 +00001930
1931 case Expr::ConditionalOperatorClass: {
1932 const ConditionalOperator *CO = cast<ConditionalOperator>(E);
1933 mangleOperatorName(OO_Conditional, /*Arity=*/3);
1934 mangleExpression(CO->getCond());
John McCall5e1e89b2010-08-18 19:18:59 +00001935 mangleExpression(CO->getLHS(), Arity);
1936 mangleExpression(CO->getRHS(), Arity);
Anders Carlssone170ba72009-12-14 01:45:37 +00001937 break;
1938 }
1939
Douglas Gregor46287c72010-01-29 16:37:09 +00001940 case Expr::ImplicitCastExprClass: {
John McCall5e1e89b2010-08-18 19:18:59 +00001941 mangleExpression(cast<ImplicitCastExpr>(E)->getSubExpr(), Arity);
Douglas Gregor46287c72010-01-29 16:37:09 +00001942 break;
1943 }
1944
1945 case Expr::CStyleCastExprClass:
1946 case Expr::CXXStaticCastExprClass:
1947 case Expr::CXXDynamicCastExprClass:
1948 case Expr::CXXReinterpretCastExprClass:
1949 case Expr::CXXConstCastExprClass:
1950 case Expr::CXXFunctionalCastExprClass: {
1951 const ExplicitCastExpr *ECE = cast<ExplicitCastExpr>(E);
1952 Out << "cv";
1953 mangleType(ECE->getType());
1954 mangleExpression(ECE->getSubExpr());
1955 break;
1956 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001957
Anders Carlsson58040a52009-12-16 05:48:46 +00001958 case Expr::CXXOperatorCallExprClass: {
1959 const CXXOperatorCallExpr *CE = cast<CXXOperatorCallExpr>(E);
1960 unsigned NumArgs = CE->getNumArgs();
1961 mangleOperatorName(CE->getOperator(), /*Arity=*/NumArgs);
1962 // Mangle the arguments.
1963 for (unsigned i = 0; i != NumArgs; ++i)
1964 mangleExpression(CE->getArg(i));
1965 break;
1966 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001967
Anders Carlssona7694082009-11-06 02:50:19 +00001968 case Expr::ParenExprClass:
John McCall5e1e89b2010-08-18 19:18:59 +00001969 mangleExpression(cast<ParenExpr>(E)->getSubExpr(), Arity);
Anders Carlssona7694082009-11-06 02:50:19 +00001970 break;
1971
Anders Carlssond553f8c2009-09-21 01:21:10 +00001972 case Expr::DeclRefExprClass: {
Douglas Gregor5ed1bc32010-02-28 21:40:32 +00001973 const NamedDecl *D = cast<DeclRefExpr>(E)->getDecl();
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00001974
Anders Carlssond553f8c2009-09-21 01:21:10 +00001975 switch (D->getKind()) {
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001976 default:
Douglas Gregor5ed1bc32010-02-28 21:40:32 +00001977 // <expr-primary> ::= L <mangled-name> E # external name
1978 Out << 'L';
1979 mangle(D, "_Z");
1980 Out << 'E';
1981 break;
1982
John McCall3dc7e7b2010-07-24 01:17:35 +00001983 case Decl::EnumConstant: {
1984 const EnumConstantDecl *ED = cast<EnumConstantDecl>(D);
1985 mangleIntegerLiteral(ED->getType(), ED->getInitVal());
1986 break;
1987 }
1988
Anders Carlssond553f8c2009-09-21 01:21:10 +00001989 case Decl::NonTypeTemplateParm: {
1990 const NonTypeTemplateParmDecl *PD = cast<NonTypeTemplateParmDecl>(D);
Anders Carlsson0ccdf8d2009-09-27 00:38:53 +00001991 mangleTemplateParameter(PD->getIndex());
Anders Carlssond553f8c2009-09-21 01:21:10 +00001992 break;
1993 }
1994
1995 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00001996
Anders Carlsson50755b02009-09-27 20:11:34 +00001997 break;
Anders Carlssond553f8c2009-09-21 01:21:10 +00001998 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00001999
Douglas Gregorc7793c72011-01-15 01:15:58 +00002000 case Expr::SubstNonTypeTemplateParmPackExprClass:
2001 mangleTemplateParameter(
2002 cast<SubstNonTypeTemplateParmPackExpr>(E)->getParameterPack()->getIndex());
2003 break;
2004
John McCall865d4472009-11-19 22:55:06 +00002005 case Expr::DependentScopeDeclRefExprClass: {
2006 const DependentScopeDeclRefExpr *DRE = cast<DependentScopeDeclRefExpr>(E);
Douglas Gregor4b2ccfc2010-02-28 22:05:49 +00002007 NestedNameSpecifier *NNS = DRE->getQualifier();
2008 const Type *QTy = NNS->getAsType();
2009
2010 // When we're dealing with a nested-name-specifier that has just a
2011 // dependent identifier in it, mangle that as a typename. FIXME:
2012 // It isn't clear that we ever actually want to have such a
2013 // nested-name-specifier; why not just represent it as a typename type?
2014 if (!QTy && NNS->getAsIdentifier() && NNS->getPrefix()) {
Douglas Gregor4a2023f2010-03-31 20:19:30 +00002015 QTy = getASTContext().getDependentNameType(ETK_Typename,
2016 NNS->getPrefix(),
2017 NNS->getAsIdentifier())
Douglas Gregor4b2ccfc2010-02-28 22:05:49 +00002018 .getTypePtr();
2019 }
Anders Carlsson50755b02009-09-27 20:11:34 +00002020 assert(QTy && "Qualifier was not type!");
2021
John McCall6dbce192010-08-20 00:17:19 +00002022 // ::= sr <type> <unqualified-name> # dependent name
2023 // ::= sr <type> <unqualified-name> <template-args> # dependent template-id
Anders Carlsson50755b02009-09-27 20:11:34 +00002024 Out << "sr";
2025 mangleType(QualType(QTy, 0));
John McCall5e1e89b2010-08-18 19:18:59 +00002026 mangleUnqualifiedName(0, DRE->getDeclName(), Arity);
John McCall6dbce192010-08-20 00:17:19 +00002027 if (DRE->hasExplicitTemplateArgs())
2028 mangleTemplateArgs(DRE->getExplicitTemplateArgs());
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002029
Anders Carlsson50755b02009-09-27 20:11:34 +00002030 break;
2031 }
2032
John McCalld9307602010-04-09 22:54:09 +00002033 case Expr::CXXBindTemporaryExprClass:
2034 mangleExpression(cast<CXXBindTemporaryExpr>(E)->getSubExpr());
2035 break;
2036
John McCall4765fa02010-12-06 08:20:24 +00002037 case Expr::ExprWithCleanupsClass:
2038 mangleExpression(cast<ExprWithCleanups>(E)->getSubExpr(), Arity);
John McCalld9307602010-04-09 22:54:09 +00002039 break;
2040
John McCall1dd73832010-02-04 01:42:13 +00002041 case Expr::FloatingLiteralClass: {
2042 const FloatingLiteral *FL = cast<FloatingLiteral>(E);
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002043 Out << 'L';
John McCall1dd73832010-02-04 01:42:13 +00002044 mangleType(FL->getType());
John McCall0512e482010-07-14 04:20:34 +00002045 mangleFloat(FL->getValue());
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002046 Out << 'E';
John McCall1dd73832010-02-04 01:42:13 +00002047 break;
2048 }
2049
John McCallde810632010-04-09 21:48:08 +00002050 case Expr::CharacterLiteralClass:
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002051 Out << 'L';
John McCallde810632010-04-09 21:48:08 +00002052 mangleType(E->getType());
2053 Out << cast<CharacterLiteral>(E)->getValue();
2054 Out << 'E';
2055 break;
2056
2057 case Expr::CXXBoolLiteralExprClass:
2058 Out << "Lb";
2059 Out << (cast<CXXBoolLiteralExpr>(E)->getValue() ? '1' : '0');
2060 Out << 'E';
2061 break;
2062
John McCall0512e482010-07-14 04:20:34 +00002063 case Expr::IntegerLiteralClass: {
2064 llvm::APSInt Value(cast<IntegerLiteral>(E)->getValue());
2065 if (E->getType()->isSignedIntegerType())
2066 Value.setIsSigned(true);
2067 mangleIntegerLiteral(E->getType(), Value);
Anders Carlssone170ba72009-12-14 01:45:37 +00002068 break;
John McCall0512e482010-07-14 04:20:34 +00002069 }
2070
2071 case Expr::ImaginaryLiteralClass: {
2072 const ImaginaryLiteral *IE = cast<ImaginaryLiteral>(E);
2073 // Mangle as if a complex literal.
Nick Lewycky271b6652010-09-05 03:40:33 +00002074 // Proposal from David Vandevoorde, 2010.06.30.
John McCall0512e482010-07-14 04:20:34 +00002075 Out << 'L';
2076 mangleType(E->getType());
2077 if (const FloatingLiteral *Imag =
2078 dyn_cast<FloatingLiteral>(IE->getSubExpr())) {
2079 // Mangle a floating-point zero of the appropriate type.
2080 mangleFloat(llvm::APFloat(Imag->getValue().getSemantics()));
2081 Out << '_';
2082 mangleFloat(Imag->getValue());
2083 } else {
Nick Lewycky271b6652010-09-05 03:40:33 +00002084 Out << "0_";
John McCall0512e482010-07-14 04:20:34 +00002085 llvm::APSInt Value(cast<IntegerLiteral>(IE->getSubExpr())->getValue());
2086 if (IE->getSubExpr()->getType()->isSignedIntegerType())
2087 Value.setIsSigned(true);
2088 mangleNumber(Value);
2089 }
2090 Out << 'E';
2091 break;
2092 }
2093
2094 case Expr::StringLiteralClass: {
John McCall1658c392010-07-15 21:53:03 +00002095 // Revised proposal from David Vandervoorde, 2010.07.15.
John McCall0512e482010-07-14 04:20:34 +00002096 Out << 'L';
John McCall1658c392010-07-15 21:53:03 +00002097 assert(isa<ConstantArrayType>(E->getType()));
2098 mangleType(E->getType());
John McCall0512e482010-07-14 04:20:34 +00002099 Out << 'E';
2100 break;
2101 }
2102
2103 case Expr::GNUNullExprClass:
2104 // FIXME: should this really be mangled the same as nullptr?
2105 // fallthrough
2106
2107 case Expr::CXXNullPtrLiteralExprClass: {
2108 // Proposal from David Vandervoorde, 2010.06.30, as
2109 // modified by ABI list discussion.
2110 Out << "LDnE";
2111 break;
2112 }
Douglas Gregorbe230c32011-01-03 17:17:50 +00002113
2114 case Expr::PackExpansionExprClass:
2115 Out << "sp";
2116 mangleExpression(cast<PackExpansionExpr>(E)->getPattern());
2117 break;
Douglas Gregor2e774c42011-01-04 18:56:13 +00002118
2119 case Expr::SizeOfPackExprClass: {
Douglas Gregor2e774c42011-01-04 18:56:13 +00002120 Out << "sZ";
2121 const NamedDecl *Pack = cast<SizeOfPackExpr>(E)->getPack();
2122 if (const TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(Pack))
2123 mangleTemplateParameter(TTP->getIndex());
2124 else if (const NonTypeTemplateParmDecl *NTTP
2125 = dyn_cast<NonTypeTemplateParmDecl>(Pack))
2126 mangleTemplateParameter(NTTP->getIndex());
2127 else if (const TemplateTemplateParmDecl *TempTP
2128 = dyn_cast<TemplateTemplateParmDecl>(Pack))
2129 mangleTemplateParameter(TempTP->getIndex());
2130 else {
Douglas Gregor4fc48662011-01-13 16:39:34 +00002131 // Note: proposed by Mike Herrick on 11/30/10
2132 // <expression> ::= sZ <function-param> # size of function parameter pack
Douglas Gregor2e774c42011-01-04 18:56:13 +00002133 Diagnostic &Diags = Context.getDiags();
2134 unsigned DiagID = Diags.getCustomDiagID(Diagnostic::Error,
2135 "cannot mangle sizeof...(function parameter pack)");
2136 Diags.Report(DiagID);
2137 return;
2138 }
2139 }
Anders Carlssond553f8c2009-09-21 01:21:10 +00002140 }
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00002141}
2142
Anders Carlsson3ac86b52009-04-15 05:36:58 +00002143void CXXNameMangler::mangleCXXCtorType(CXXCtorType T) {
2144 // <ctor-dtor-name> ::= C1 # complete object constructor
2145 // ::= C2 # base object constructor
2146 // ::= C3 # complete object allocating constructor
2147 //
2148 switch (T) {
2149 case Ctor_Complete:
2150 Out << "C1";
2151 break;
2152 case Ctor_Base:
2153 Out << "C2";
2154 break;
2155 case Ctor_CompleteAllocating:
2156 Out << "C3";
2157 break;
2158 }
2159}
2160
Anders Carlsson27ae5362009-04-17 01:58:57 +00002161void CXXNameMangler::mangleCXXDtorType(CXXDtorType T) {
2162 // <ctor-dtor-name> ::= D0 # deleting destructor
2163 // ::= D1 # complete object destructor
2164 // ::= D2 # base object destructor
2165 //
2166 switch (T) {
2167 case Dtor_Deleting:
2168 Out << "D0";
2169 break;
2170 case Dtor_Complete:
2171 Out << "D1";
2172 break;
2173 case Dtor_Base:
2174 Out << "D2";
2175 break;
2176 }
2177}
2178
John McCall6dbce192010-08-20 00:17:19 +00002179void CXXNameMangler::mangleTemplateArgs(
2180 const ExplicitTemplateArgumentList &TemplateArgs) {
2181 // <template-args> ::= I <template-arg>+ E
2182 Out << 'I';
2183 for (unsigned I = 0, E = TemplateArgs.NumTemplateArgs; I != E; ++I)
2184 mangleTemplateArg(0, TemplateArgs.getTemplateArgs()[I].getArgument());
2185 Out << 'E';
2186}
2187
Douglas Gregor20f0cc72010-04-23 03:10:43 +00002188void CXXNameMangler::mangleTemplateArgs(TemplateName Template,
2189 const TemplateArgument *TemplateArgs,
2190 unsigned NumTemplateArgs) {
2191 if (TemplateDecl *TD = Template.getAsTemplateDecl())
2192 return mangleTemplateArgs(*TD->getTemplateParameters(), TemplateArgs,
2193 NumTemplateArgs);
Sean Huntc3021132010-05-05 15:23:54 +00002194
Douglas Gregor20f0cc72010-04-23 03:10:43 +00002195 // <template-args> ::= I <template-arg>+ E
2196 Out << 'I';
2197 for (unsigned i = 0; i != NumTemplateArgs; ++i)
2198 mangleTemplateArg(0, TemplateArgs[i]);
2199 Out << 'E';
2200}
2201
Rafael Espindolad9800722010-03-11 14:07:00 +00002202void CXXNameMangler::mangleTemplateArgs(const TemplateParameterList &PL,
2203 const TemplateArgumentList &AL) {
Anders Carlsson7a0ba872009-05-15 16:09:15 +00002204 // <template-args> ::= I <template-arg>+ E
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002205 Out << 'I';
Rafael Espindolad9800722010-03-11 14:07:00 +00002206 for (unsigned i = 0, e = AL.size(); i != e; ++i)
2207 mangleTemplateArg(PL.getParam(i), AL[i]);
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002208 Out << 'E';
Anders Carlsson7a0ba872009-05-15 16:09:15 +00002209}
2210
Rafael Espindolad9800722010-03-11 14:07:00 +00002211void CXXNameMangler::mangleTemplateArgs(const TemplateParameterList &PL,
2212 const TemplateArgument *TemplateArgs,
Anders Carlsson7624f212009-09-18 02:42:01 +00002213 unsigned NumTemplateArgs) {
2214 // <template-args> ::= I <template-arg>+ E
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002215 Out << 'I';
Daniel Dunbar7e0c1952009-11-21 09:17:15 +00002216 for (unsigned i = 0; i != NumTemplateArgs; ++i)
Rafael Espindolad9800722010-03-11 14:07:00 +00002217 mangleTemplateArg(PL.getParam(i), TemplateArgs[i]);
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002218 Out << 'E';
Anders Carlsson7624f212009-09-18 02:42:01 +00002219}
2220
Rafael Espindolad9800722010-03-11 14:07:00 +00002221void CXXNameMangler::mangleTemplateArg(const NamedDecl *P,
2222 const TemplateArgument &A) {
Mike Stump1eb44332009-09-09 15:08:12 +00002223 // <template-arg> ::= <type> # type or template
Anders Carlsson7a0ba872009-05-15 16:09:15 +00002224 // ::= X <expression> E # expression
2225 // ::= <expr-primary> # simple expressions
Douglas Gregor4fc48662011-01-13 16:39:34 +00002226 // ::= J <template-arg>* E # argument pack
Anders Carlsson7a0ba872009-05-15 16:09:15 +00002227 // ::= sp <expression> # pack expansion of (C++0x)
2228 switch (A.getKind()) {
Douglas Gregorf90b27a2011-01-03 22:36:02 +00002229 case TemplateArgument::Null:
2230 llvm_unreachable("Cannot mangle NULL template argument");
2231
Anders Carlsson7a0ba872009-05-15 16:09:15 +00002232 case TemplateArgument::Type:
2233 mangleType(A.getAsType());
2234 break;
Anders Carlsson9e85c742009-12-23 19:30:55 +00002235 case TemplateArgument::Template:
John McCallb6f532e2010-07-14 06:43:17 +00002236 // This is mangled as <type>.
2237 mangleType(A.getAsTemplate());
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002238 break;
Douglas Gregora7fc9012011-01-05 18:58:31 +00002239 case TemplateArgument::TemplateExpansion:
Douglas Gregor4fc48662011-01-13 16:39:34 +00002240 // <type> ::= Dp <type> # pack expansion (C++0x)
Douglas Gregora7fc9012011-01-05 18:58:31 +00002241 Out << "Dp";
2242 mangleType(A.getAsTemplateOrTemplatePattern());
2243 break;
Anders Carlssond553f8c2009-09-21 01:21:10 +00002244 case TemplateArgument::Expression:
2245 Out << 'X';
2246 mangleExpression(A.getAsExpr());
2247 Out << 'E';
2248 break;
Anders Carlssone170ba72009-12-14 01:45:37 +00002249 case TemplateArgument::Integral:
2250 mangleIntegerLiteral(A.getIntegralType(), *A.getAsIntegral());
Anders Carlsson7a0ba872009-05-15 16:09:15 +00002251 break;
Daniel Dunbar7e0c1952009-11-21 09:17:15 +00002252 case TemplateArgument::Declaration: {
Douglas Gregor20f0cc72010-04-23 03:10:43 +00002253 assert(P && "Missing template parameter for declaration argument");
Daniel Dunbar7e0c1952009-11-21 09:17:15 +00002254 // <expr-primary> ::= L <mangled-name> E # external name
2255
Rafael Espindolad9800722010-03-11 14:07:00 +00002256 // Clang produces AST's where pointer-to-member-function expressions
Daniel Dunbar7e0c1952009-11-21 09:17:15 +00002257 // and pointer-to-function expressions are represented as a declaration not
Rafael Espindolad9800722010-03-11 14:07:00 +00002258 // an expression. We compensate for it here to produce the correct mangling.
2259 NamedDecl *D = cast<NamedDecl>(A.getAsDecl());
2260 const NonTypeTemplateParmDecl *Parameter = cast<NonTypeTemplateParmDecl>(P);
2261 bool compensateMangling = D->isCXXClassMember() &&
2262 !Parameter->getType()->isReferenceType();
2263 if (compensateMangling) {
2264 Out << 'X';
2265 mangleOperatorName(OO_Amp, 1);
2266 }
2267
Daniel Dunbar7e0c1952009-11-21 09:17:15 +00002268 Out << 'L';
2269 // References to external entities use the mangled name; if the name would
2270 // not normally be manged then mangle it as unqualified.
2271 //
2272 // FIXME: The ABI specifies that external names here should have _Z, but
2273 // gcc leaves this off.
Rafael Espindolad9800722010-03-11 14:07:00 +00002274 if (compensateMangling)
2275 mangle(D, "_Z");
2276 else
2277 mangle(D, "Z");
Daniel Dunbar7e0c1952009-11-21 09:17:15 +00002278 Out << 'E';
Rafael Espindolad9800722010-03-11 14:07:00 +00002279
2280 if (compensateMangling)
2281 Out << 'E';
2282
Daniel Dunbar7e0c1952009-11-21 09:17:15 +00002283 break;
2284 }
Douglas Gregorf90b27a2011-01-03 22:36:02 +00002285
2286 case TemplateArgument::Pack: {
2287 // Note: proposal by Mike Herrick on 12/20/10
2288 Out << 'J';
2289 for (TemplateArgument::pack_iterator PA = A.pack_begin(),
2290 PAEnd = A.pack_end();
2291 PA != PAEnd; ++PA)
2292 mangleTemplateArg(P, *PA);
2293 Out << 'E';
2294 }
Daniel Dunbar7e0c1952009-11-21 09:17:15 +00002295 }
Anders Carlsson7a0ba872009-05-15 16:09:15 +00002296}
2297
Anders Carlsson0ccdf8d2009-09-27 00:38:53 +00002298void CXXNameMangler::mangleTemplateParameter(unsigned Index) {
2299 // <template-param> ::= T_ # first template parameter
2300 // ::= T <parameter-2 non-negative number> _
2301 if (Index == 0)
2302 Out << "T_";
2303 else
2304 Out << 'T' << (Index - 1) << '_';
2305}
2306
Anders Carlsson76967372009-09-17 00:43:46 +00002307// <substitution> ::= S <seq-id> _
2308// ::= S_
Anders Carlsson6862fc72009-09-17 04:16:28 +00002309bool CXXNameMangler::mangleSubstitution(const NamedDecl *ND) {
Anders Carlssone7c8cb62009-09-26 20:53:44 +00002310 // Try one of the standard substitutions first.
2311 if (mangleStandardSubstitution(ND))
2312 return true;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002313
Anders Carlsson433d1372009-11-07 04:26:04 +00002314 ND = cast<NamedDecl>(ND->getCanonicalDecl());
Anders Carlsson6862fc72009-09-17 04:16:28 +00002315 return mangleSubstitution(reinterpret_cast<uintptr_t>(ND));
2316}
2317
Anders Carlsson76967372009-09-17 00:43:46 +00002318bool CXXNameMangler::mangleSubstitution(QualType T) {
Anders Carlssond99edc42009-09-26 03:55:37 +00002319 if (!T.getCVRQualifiers()) {
2320 if (const RecordType *RT = T->getAs<RecordType>())
2321 return mangleSubstitution(RT->getDecl());
2322 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002323
Anders Carlsson76967372009-09-17 00:43:46 +00002324 uintptr_t TypePtr = reinterpret_cast<uintptr_t>(T.getAsOpaquePtr());
2325
Anders Carlssond3a932a2009-09-17 03:53:28 +00002326 return mangleSubstitution(TypePtr);
2327}
2328
Douglas Gregor1e9268e2010-04-28 05:58:56 +00002329bool CXXNameMangler::mangleSubstitution(TemplateName Template) {
2330 if (TemplateDecl *TD = Template.getAsTemplateDecl())
2331 return mangleSubstitution(TD);
Sean Huntc3021132010-05-05 15:23:54 +00002332
Douglas Gregor1e9268e2010-04-28 05:58:56 +00002333 Template = Context.getASTContext().getCanonicalTemplateName(Template);
2334 return mangleSubstitution(
2335 reinterpret_cast<uintptr_t>(Template.getAsVoidPointer()));
2336}
2337
Anders Carlssond3a932a2009-09-17 03:53:28 +00002338bool CXXNameMangler::mangleSubstitution(uintptr_t Ptr) {
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002339 llvm::DenseMap<uintptr_t, unsigned>::iterator I = Substitutions.find(Ptr);
Anders Carlsson76967372009-09-17 00:43:46 +00002340 if (I == Substitutions.end())
2341 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002342
Anders Carlsson76967372009-09-17 00:43:46 +00002343 unsigned SeqID = I->second;
2344 if (SeqID == 0)
2345 Out << "S_";
2346 else {
2347 SeqID--;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002348
Anders Carlsson76967372009-09-17 00:43:46 +00002349 // <seq-id> is encoded in base-36, using digits and upper case letters.
2350 char Buffer[10];
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002351 char *BufferPtr = llvm::array_endof(Buffer);
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002352
Anders Carlsson76967372009-09-17 00:43:46 +00002353 if (SeqID == 0) *--BufferPtr = '0';
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002354
Anders Carlsson76967372009-09-17 00:43:46 +00002355 while (SeqID) {
2356 assert(BufferPtr > Buffer && "Buffer overflow!");
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002357
John McCall6ab30e02010-06-09 07:26:17 +00002358 char c = static_cast<char>(SeqID % 36);
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002359
Anders Carlsson76967372009-09-17 00:43:46 +00002360 *--BufferPtr = (c < 10 ? '0' + c : 'A' + c - 10);
2361 SeqID /= 36;
2362 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002363
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002364 Out << 'S'
2365 << llvm::StringRef(BufferPtr, llvm::array_endof(Buffer)-BufferPtr)
2366 << '_';
Anders Carlsson76967372009-09-17 00:43:46 +00002367 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002368
Anders Carlsson76967372009-09-17 00:43:46 +00002369 return true;
2370}
2371
Anders Carlssonf514b542009-09-27 00:12:57 +00002372static bool isCharType(QualType T) {
2373 if (T.isNull())
2374 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002375
Anders Carlssonf514b542009-09-27 00:12:57 +00002376 return T->isSpecificBuiltinType(BuiltinType::Char_S) ||
2377 T->isSpecificBuiltinType(BuiltinType::Char_U);
2378}
2379
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002380/// isCharSpecialization - Returns whether a given type is a template
Anders Carlssonf514b542009-09-27 00:12:57 +00002381/// specialization of a given name with a single argument of type char.
2382static bool isCharSpecialization(QualType T, const char *Name) {
2383 if (T.isNull())
2384 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002385
Anders Carlssonf514b542009-09-27 00:12:57 +00002386 const RecordType *RT = T->getAs<RecordType>();
2387 if (!RT)
2388 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002389
2390 const ClassTemplateSpecializationDecl *SD =
Anders Carlssonf514b542009-09-27 00:12:57 +00002391 dyn_cast<ClassTemplateSpecializationDecl>(RT->getDecl());
2392 if (!SD)
2393 return false;
2394
2395 if (!isStdNamespace(SD->getDeclContext()))
2396 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002397
Anders Carlssonf514b542009-09-27 00:12:57 +00002398 const TemplateArgumentList &TemplateArgs = SD->getTemplateArgs();
2399 if (TemplateArgs.size() != 1)
2400 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002401
Anders Carlssonf514b542009-09-27 00:12:57 +00002402 if (!isCharType(TemplateArgs[0].getAsType()))
2403 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002404
Daniel Dunbar01eb9b92009-10-18 21:17:35 +00002405 return SD->getIdentifier()->getName() == Name;
Anders Carlssonf514b542009-09-27 00:12:57 +00002406}
2407
Anders Carlsson91f88602009-12-07 19:56:42 +00002408template <std::size_t StrLen>
Benjamin Kramer54353f42010-11-25 18:29:30 +00002409static bool isStreamCharSpecialization(const ClassTemplateSpecializationDecl*SD,
2410 const char (&Str)[StrLen]) {
Anders Carlsson91f88602009-12-07 19:56:42 +00002411 if (!SD->getIdentifier()->isStr(Str))
2412 return false;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002413
Anders Carlsson91f88602009-12-07 19:56:42 +00002414 const TemplateArgumentList &TemplateArgs = SD->getTemplateArgs();
2415 if (TemplateArgs.size() != 2)
2416 return false;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002417
Anders Carlsson91f88602009-12-07 19:56:42 +00002418 if (!isCharType(TemplateArgs[0].getAsType()))
2419 return false;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002420
Anders Carlsson91f88602009-12-07 19:56:42 +00002421 if (!isCharSpecialization(TemplateArgs[1].getAsType(), "char_traits"))
2422 return false;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002423
Anders Carlsson91f88602009-12-07 19:56:42 +00002424 return true;
2425}
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002426
Anders Carlssone7c8cb62009-09-26 20:53:44 +00002427bool CXXNameMangler::mangleStandardSubstitution(const NamedDecl *ND) {
2428 // <substitution> ::= St # ::std::
Anders Carlsson8c031552009-09-26 23:10:05 +00002429 if (const NamespaceDecl *NS = dyn_cast<NamespaceDecl>(ND)) {
Anders Carlsson47846d22009-12-04 06:23:23 +00002430 if (isStd(NS)) {
Anders Carlsson8c031552009-09-26 23:10:05 +00002431 Out << "St";
2432 return true;
2433 }
2434 }
2435
2436 if (const ClassTemplateDecl *TD = dyn_cast<ClassTemplateDecl>(ND)) {
2437 if (!isStdNamespace(TD->getDeclContext()))
2438 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002439
Anders Carlsson8c031552009-09-26 23:10:05 +00002440 // <substitution> ::= Sa # ::std::allocator
2441 if (TD->getIdentifier()->isStr("allocator")) {
2442 Out << "Sa";
2443 return true;
2444 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002445
Anders Carlsson189d59c2009-09-26 23:14:39 +00002446 // <<substitution> ::= Sb # ::std::basic_string
2447 if (TD->getIdentifier()->isStr("basic_string")) {
2448 Out << "Sb";
2449 return true;
2450 }
Anders Carlsson8c031552009-09-26 23:10:05 +00002451 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002452
2453 if (const ClassTemplateSpecializationDecl *SD =
Anders Carlssonf514b542009-09-27 00:12:57 +00002454 dyn_cast<ClassTemplateSpecializationDecl>(ND)) {
Eli Friedman5370ee22010-02-23 18:25:09 +00002455 if (!isStdNamespace(SD->getDeclContext()))
2456 return false;
2457
Anders Carlssonf514b542009-09-27 00:12:57 +00002458 // <substitution> ::= Ss # ::std::basic_string<char,
2459 // ::std::char_traits<char>,
2460 // ::std::allocator<char> >
2461 if (SD->getIdentifier()->isStr("basic_string")) {
2462 const TemplateArgumentList &TemplateArgs = SD->getTemplateArgs();
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002463
Anders Carlssonf514b542009-09-27 00:12:57 +00002464 if (TemplateArgs.size() != 3)
2465 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002466
Anders Carlssonf514b542009-09-27 00:12:57 +00002467 if (!isCharType(TemplateArgs[0].getAsType()))
2468 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002469
Anders Carlssonf514b542009-09-27 00:12:57 +00002470 if (!isCharSpecialization(TemplateArgs[1].getAsType(), "char_traits"))
2471 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002472
Anders Carlssonf514b542009-09-27 00:12:57 +00002473 if (!isCharSpecialization(TemplateArgs[2].getAsType(), "allocator"))
2474 return false;
2475
2476 Out << "Ss";
2477 return true;
2478 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002479
Anders Carlsson91f88602009-12-07 19:56:42 +00002480 // <substitution> ::= Si # ::std::basic_istream<char,
2481 // ::std::char_traits<char> >
2482 if (isStreamCharSpecialization(SD, "basic_istream")) {
2483 Out << "Si";
2484 return true;
2485 }
2486
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002487 // <substitution> ::= So # ::std::basic_ostream<char,
Anders Carlsson8f8fd8e2009-10-08 17:20:26 +00002488 // ::std::char_traits<char> >
Anders Carlsson91f88602009-12-07 19:56:42 +00002489 if (isStreamCharSpecialization(SD, "basic_ostream")) {
Anders Carlsson8f8fd8e2009-10-08 17:20:26 +00002490 Out << "So";
2491 return true;
2492 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002493
Anders Carlsson91f88602009-12-07 19:56:42 +00002494 // <substitution> ::= Sd # ::std::basic_iostream<char,
2495 // ::std::char_traits<char> >
2496 if (isStreamCharSpecialization(SD, "basic_iostream")) {
2497 Out << "Sd";
2498 return true;
2499 }
Anders Carlssonf514b542009-09-27 00:12:57 +00002500 }
Anders Carlsson8c031552009-09-26 23:10:05 +00002501 return false;
Anders Carlssone7c8cb62009-09-26 20:53:44 +00002502}
2503
Anders Carlsson76967372009-09-17 00:43:46 +00002504void CXXNameMangler::addSubstitution(QualType T) {
Anders Carlssond99edc42009-09-26 03:55:37 +00002505 if (!T.getCVRQualifiers()) {
2506 if (const RecordType *RT = T->getAs<RecordType>()) {
2507 addSubstitution(RT->getDecl());
2508 return;
2509 }
2510 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002511
Anders Carlsson76967372009-09-17 00:43:46 +00002512 uintptr_t TypePtr = reinterpret_cast<uintptr_t>(T.getAsOpaquePtr());
Anders Carlssond3a932a2009-09-17 03:53:28 +00002513 addSubstitution(TypePtr);
2514}
2515
Douglas Gregor1e9268e2010-04-28 05:58:56 +00002516void CXXNameMangler::addSubstitution(TemplateName Template) {
2517 if (TemplateDecl *TD = Template.getAsTemplateDecl())
2518 return addSubstitution(TD);
Sean Huntc3021132010-05-05 15:23:54 +00002519
Douglas Gregor1e9268e2010-04-28 05:58:56 +00002520 Template = Context.getASTContext().getCanonicalTemplateName(Template);
2521 addSubstitution(reinterpret_cast<uintptr_t>(Template.getAsVoidPointer()));
2522}
2523
Anders Carlssond3a932a2009-09-17 03:53:28 +00002524void CXXNameMangler::addSubstitution(uintptr_t Ptr) {
Anders Carlssond3a932a2009-09-17 03:53:28 +00002525 assert(!Substitutions.count(Ptr) && "Substitution already exists!");
Anders Carlsson9d85b722010-06-02 04:29:50 +00002526 Substitutions[Ptr] = SeqID++;
Anders Carlsson76967372009-09-17 00:43:46 +00002527}
2528
Daniel Dunbar1b077112009-11-21 09:06:10 +00002529//
Mike Stump1eb44332009-09-09 15:08:12 +00002530
Daniel Dunbar1b077112009-11-21 09:06:10 +00002531/// \brief Mangles the name of the declaration D and emits that name to the
2532/// given output stream.
2533///
2534/// If the declaration D requires a mangled name, this routine will emit that
2535/// mangled name to \p os and return true. Otherwise, \p os will be unchanged
2536/// and this routine will return false. In this case, the caller should just
2537/// emit the identifier of the declaration (\c D->getIdentifier()) as its
2538/// name.
Peter Collingbourne14110472011-01-13 18:57:25 +00002539void ItaniumMangleContext::mangleName(const NamedDecl *D,
Rafael Espindola0e376a02011-02-11 01:41:00 +00002540 llvm::raw_ostream &Out) {
Daniel Dunbarc02ab4c2009-11-21 09:14:44 +00002541 assert((isa<FunctionDecl>(D) || isa<VarDecl>(D)) &&
2542 "Invalid mangleName() call, argument is not a variable or function!");
2543 assert(!isa<CXXConstructorDecl>(D) && !isa<CXXDestructorDecl>(D) &&
2544 "Invalid mangleName() call on 'structor decl!");
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002545
Daniel Dunbar1b077112009-11-21 09:06:10 +00002546 PrettyStackTraceDecl CrashInfo(D, SourceLocation(),
2547 getASTContext().getSourceManager(),
2548 "Mangling declaration");
Mike Stump1eb44332009-09-09 15:08:12 +00002549
Rafael Espindolac4850c22011-02-10 23:59:36 +00002550 CXXNameMangler Mangler(*this, Out);
Daniel Dunbar94fd26d2009-11-21 09:06:22 +00002551 return Mangler.mangle(D);
Daniel Dunbar1b077112009-11-21 09:06:10 +00002552}
Mike Stump1eb44332009-09-09 15:08:12 +00002553
Peter Collingbourne14110472011-01-13 18:57:25 +00002554void ItaniumMangleContext::mangleCXXCtor(const CXXConstructorDecl *D,
2555 CXXCtorType Type,
Rafael Espindola0e376a02011-02-11 01:41:00 +00002556 llvm::raw_ostream &Out) {
Rafael Espindolac4850c22011-02-10 23:59:36 +00002557 CXXNameMangler Mangler(*this, Out, D, Type);
Daniel Dunbar77939c92009-11-21 09:06:31 +00002558 Mangler.mangle(D);
Daniel Dunbar1b077112009-11-21 09:06:10 +00002559}
Mike Stump1eb44332009-09-09 15:08:12 +00002560
Peter Collingbourne14110472011-01-13 18:57:25 +00002561void ItaniumMangleContext::mangleCXXDtor(const CXXDestructorDecl *D,
2562 CXXDtorType Type,
Rafael Espindola0e376a02011-02-11 01:41:00 +00002563 llvm::raw_ostream &Out) {
Rafael Espindolac4850c22011-02-10 23:59:36 +00002564 CXXNameMangler Mangler(*this, Out, D, Type);
Daniel Dunbar77939c92009-11-21 09:06:31 +00002565 Mangler.mangle(D);
Daniel Dunbar1b077112009-11-21 09:06:10 +00002566}
Mike Stumpf1216772009-07-31 18:25:34 +00002567
Peter Collingbourne14110472011-01-13 18:57:25 +00002568void ItaniumMangleContext::mangleThunk(const CXXMethodDecl *MD,
2569 const ThunkInfo &Thunk,
2570 llvm::SmallVectorImpl<char> &Res) {
Anders Carlsson19879c92010-03-23 17:17:29 +00002571 // <special-name> ::= T <call-offset> <base encoding>
2572 // # base is the nominal target function of thunk
2573 // <special-name> ::= Tc <call-offset> <call-offset> <base encoding>
2574 // # base is the nominal target function of thunk
2575 // # first call-offset is 'this' adjustment
2576 // # second call-offset is result adjustment
Sean Huntc3021132010-05-05 15:23:54 +00002577
Anders Carlsson19879c92010-03-23 17:17:29 +00002578 assert(!isa<CXXDestructorDecl>(MD) &&
2579 "Use mangleCXXDtor for destructor decls!");
Rafael Espindolac4850c22011-02-10 23:59:36 +00002580 llvm::raw_svector_ostream Out(Res);
2581 CXXNameMangler Mangler(*this, Out);
Anders Carlsson19879c92010-03-23 17:17:29 +00002582 Mangler.getStream() << "_ZT";
2583 if (!Thunk.Return.isEmpty())
2584 Mangler.getStream() << 'c';
Sean Huntc3021132010-05-05 15:23:54 +00002585
Anders Carlsson19879c92010-03-23 17:17:29 +00002586 // Mangle the 'this' pointer adjustment.
2587 Mangler.mangleCallOffset(Thunk.This.NonVirtual, Thunk.This.VCallOffsetOffset);
Sean Huntc3021132010-05-05 15:23:54 +00002588
Anders Carlsson19879c92010-03-23 17:17:29 +00002589 // Mangle the return pointer adjustment if there is one.
2590 if (!Thunk.Return.isEmpty())
2591 Mangler.mangleCallOffset(Thunk.Return.NonVirtual,
2592 Thunk.Return.VBaseOffsetOffset);
Sean Huntc3021132010-05-05 15:23:54 +00002593
Anders Carlsson19879c92010-03-23 17:17:29 +00002594 Mangler.mangleFunctionEncoding(MD);
2595}
2596
Sean Huntc3021132010-05-05 15:23:54 +00002597void
Peter Collingbourne14110472011-01-13 18:57:25 +00002598ItaniumMangleContext::mangleCXXDtorThunk(const CXXDestructorDecl *DD,
2599 CXXDtorType Type,
2600 const ThisAdjustment &ThisAdjustment,
2601 llvm::SmallVectorImpl<char> &Res) {
Anders Carlsson19879c92010-03-23 17:17:29 +00002602 // <special-name> ::= T <call-offset> <base encoding>
2603 // # base is the nominal target function of thunk
Sean Huntc3021132010-05-05 15:23:54 +00002604
Rafael Espindolac4850c22011-02-10 23:59:36 +00002605 llvm::raw_svector_ostream Out(Res);
2606 CXXNameMangler Mangler(*this, Out, DD, Type);
Anders Carlsson19879c92010-03-23 17:17:29 +00002607 Mangler.getStream() << "_ZT";
2608
2609 // Mangle the 'this' pointer adjustment.
Sean Huntc3021132010-05-05 15:23:54 +00002610 Mangler.mangleCallOffset(ThisAdjustment.NonVirtual,
Anders Carlsson19879c92010-03-23 17:17:29 +00002611 ThisAdjustment.VCallOffsetOffset);
2612
2613 Mangler.mangleFunctionEncoding(DD);
2614}
2615
Daniel Dunbarc0747712009-11-21 09:12:13 +00002616/// mangleGuardVariable - Returns the mangled name for a guard variable
2617/// for the passed in VarDecl.
Peter Collingbourne14110472011-01-13 18:57:25 +00002618void ItaniumMangleContext::mangleItaniumGuardVariable(const VarDecl *D,
2619 llvm::SmallVectorImpl<char> &Res) {
Daniel Dunbarc0747712009-11-21 09:12:13 +00002620 // <special-name> ::= GV <object name> # Guard variable for one-time
2621 // # initialization
Rafael Espindolac4850c22011-02-10 23:59:36 +00002622 llvm::raw_svector_ostream Out(Res);
2623 CXXNameMangler Mangler(*this, Out);
Daniel Dunbarc0747712009-11-21 09:12:13 +00002624 Mangler.getStream() << "_ZGV";
2625 Mangler.mangleName(D);
2626}
2627
Peter Collingbourne14110472011-01-13 18:57:25 +00002628void ItaniumMangleContext::mangleReferenceTemporary(const VarDecl *D,
Anders Carlsson715edf22010-06-26 16:09:40 +00002629 llvm::SmallVectorImpl<char> &Res) {
2630 // We match the GCC mangling here.
2631 // <special-name> ::= GR <object name>
Rafael Espindolac4850c22011-02-10 23:59:36 +00002632 llvm::raw_svector_ostream Out(Res);
2633 CXXNameMangler Mangler(*this, Out);
Anders Carlsson715edf22010-06-26 16:09:40 +00002634 Mangler.getStream() << "_ZGR";
2635 Mangler.mangleName(D);
2636}
2637
Peter Collingbourne14110472011-01-13 18:57:25 +00002638void ItaniumMangleContext::mangleCXXVTable(const CXXRecordDecl *RD,
2639 llvm::SmallVectorImpl<char> &Res) {
Daniel Dunbarc0747712009-11-21 09:12:13 +00002640 // <special-name> ::= TV <type> # virtual table
Rafael Espindolac4850c22011-02-10 23:59:36 +00002641 llvm::raw_svector_ostream Out(Res);
2642 CXXNameMangler Mangler(*this, Out);
Daniel Dunbarc0747712009-11-21 09:12:13 +00002643 Mangler.getStream() << "_ZTV";
Douglas Gregor1b12a3b2010-05-26 05:11:13 +00002644 Mangler.mangleNameOrStandardSubstitution(RD);
Daniel Dunbar1b077112009-11-21 09:06:10 +00002645}
Mike Stump82d75b02009-11-10 01:58:37 +00002646
Peter Collingbourne14110472011-01-13 18:57:25 +00002647void ItaniumMangleContext::mangleCXXVTT(const CXXRecordDecl *RD,
2648 llvm::SmallVectorImpl<char> &Res) {
Daniel Dunbarc0747712009-11-21 09:12:13 +00002649 // <special-name> ::= TT <type> # VTT structure
Rafael Espindolac4850c22011-02-10 23:59:36 +00002650 llvm::raw_svector_ostream Out(Res);
2651 CXXNameMangler Mangler(*this, Out);
Daniel Dunbarc0747712009-11-21 09:12:13 +00002652 Mangler.getStream() << "_ZTT";
Douglas Gregor1b12a3b2010-05-26 05:11:13 +00002653 Mangler.mangleNameOrStandardSubstitution(RD);
Daniel Dunbar1b077112009-11-21 09:06:10 +00002654}
Mike Stumpab3f7e92009-11-10 01:41:59 +00002655
Peter Collingbourne14110472011-01-13 18:57:25 +00002656void ItaniumMangleContext::mangleCXXCtorVTable(const CXXRecordDecl *RD,
2657 int64_t Offset,
2658 const CXXRecordDecl *Type,
2659 llvm::SmallVectorImpl<char> &Res) {
Daniel Dunbarc0747712009-11-21 09:12:13 +00002660 // <special-name> ::= TC <type> <offset number> _ <base type>
Rafael Espindolac4850c22011-02-10 23:59:36 +00002661 llvm::raw_svector_ostream Out(Res);
2662 CXXNameMangler Mangler(*this, Out);
Daniel Dunbarc0747712009-11-21 09:12:13 +00002663 Mangler.getStream() << "_ZTC";
Douglas Gregor1b12a3b2010-05-26 05:11:13 +00002664 Mangler.mangleNameOrStandardSubstitution(RD);
Daniel Dunbarc0747712009-11-21 09:12:13 +00002665 Mangler.getStream() << Offset;
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002666 Mangler.getStream() << '_';
Douglas Gregor1b12a3b2010-05-26 05:11:13 +00002667 Mangler.mangleNameOrStandardSubstitution(Type);
Daniel Dunbar1b077112009-11-21 09:06:10 +00002668}
Mike Stump738f8c22009-07-31 23:15:31 +00002669
Peter Collingbourne14110472011-01-13 18:57:25 +00002670void ItaniumMangleContext::mangleCXXRTTI(QualType Ty,
2671 llvm::SmallVectorImpl<char> &Res) {
Daniel Dunbarc0747712009-11-21 09:12:13 +00002672 // <special-name> ::= TI <type> # typeinfo structure
Douglas Gregor154fe982009-12-23 22:04:40 +00002673 assert(!Ty.hasQualifiers() && "RTTI info cannot have top-level qualifiers");
Rafael Espindolac4850c22011-02-10 23:59:36 +00002674 llvm::raw_svector_ostream Out(Res);
2675 CXXNameMangler Mangler(*this, Out);
Daniel Dunbarc0747712009-11-21 09:12:13 +00002676 Mangler.getStream() << "_ZTI";
2677 Mangler.mangleType(Ty);
Daniel Dunbar1b077112009-11-21 09:06:10 +00002678}
Mike Stump67795982009-11-14 00:14:13 +00002679
Peter Collingbourne14110472011-01-13 18:57:25 +00002680void ItaniumMangleContext::mangleCXXRTTIName(QualType Ty,
2681 llvm::SmallVectorImpl<char> &Res) {
Daniel Dunbarc0747712009-11-21 09:12:13 +00002682 // <special-name> ::= TS <type> # typeinfo name (null terminated byte string)
Rafael Espindolac4850c22011-02-10 23:59:36 +00002683 llvm::raw_svector_ostream Out(Res);
2684 CXXNameMangler Mangler(*this, Out);
Daniel Dunbarc0747712009-11-21 09:12:13 +00002685 Mangler.getStream() << "_ZTS";
2686 Mangler.mangleType(Ty);
Mike Stumpf1216772009-07-31 18:25:34 +00002687}
Peter Collingbourne14110472011-01-13 18:57:25 +00002688
2689MangleContext *clang::createItaniumMangleContext(ASTContext &Context,
2690 Diagnostic &Diags) {
2691 return new ItaniumMangleContext(Context, Diags);
2692}