blob: 6608571ade7e9cc6f97718ba3a78d6a2482adad8 [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);
94 void mangleName(const NamedDecl *D, llvm::SmallVectorImpl<char> &);
95 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,
113 llvm::SmallVectorImpl<char> &);
114 void mangleCXXDtor(const CXXDestructorDecl *D, CXXDtorType Type,
115 llvm::SmallVectorImpl<char> &);
116
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;
Daniel Dunbar94fd26d2009-11-21 09:06:22 +0000139 llvm::raw_svector_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:
Peter Collingbourne14110472011-01-13 18:57:25 +0000152 CXXNameMangler(ItaniumMangleContext &C, llvm::SmallVectorImpl<char> &Res)
Anders Carlsson9d85b722010-06-02 04:29:50 +0000153 : Context(C), Out(Res), Structor(0), StructorType(0), SeqID(0) { }
Peter Collingbourne14110472011-01-13 18:57:25 +0000154 CXXNameMangler(ItaniumMangleContext &C, llvm::SmallVectorImpl<char> &Res,
Daniel Dunbar77939c92009-11-21 09:06:31 +0000155 const CXXConstructorDecl *D, CXXCtorType Type)
Anders Carlsson9d85b722010-06-02 04:29:50 +0000156 : Context(C), Out(Res), Structor(getStructor(D)), StructorType(Type),
157 SeqID(0) { }
Peter Collingbourne14110472011-01-13 18:57:25 +0000158 CXXNameMangler(ItaniumMangleContext &C, llvm::SmallVectorImpl<char> &Res,
Daniel Dunbar77939c92009-11-21 09:06:31 +0000159 const CXXDestructorDecl *D, CXXDtorType Type)
Anders Carlsson9d85b722010-06-02 04:29:50 +0000160 : Context(C), Out(Res), Structor(getStructor(D)), StructorType(Type),
161 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
Daniel Dunbarc0747712009-11-21 09:12:13 +0000174 llvm::raw_svector_ostream &getStream() { return Out; }
175
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;
Peter Collingbourne14110472011-01-13 18:57:25 +0000912 Context.mangleBlock(Block, Name);
Douglas Gregor35415f52010-05-25 17:04:15 +0000913 Out << Name.size() << Name;
914 return;
915 }
916
Anders Carlsson6862fc72009-09-17 04:16:28 +0000917 if (mangleSubstitution(cast<NamedDecl>(DC)))
918 return;
Anders Carlsson7482e242009-09-18 04:29:09 +0000919
Anders Carlsson2ee3fca2009-09-18 20:11:09 +0000920 // Check if we have a template.
921 const TemplateArgumentList *TemplateArgs = 0;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000922 if (const TemplateDecl *TD = isTemplate(cast<NamedDecl>(DC), TemplateArgs)) {
Anders Carlsson2ee3fca2009-09-18 20:11:09 +0000923 mangleTemplatePrefix(TD);
Rafael Espindolad9800722010-03-11 14:07:00 +0000924 TemplateParameterList *TemplateParameters = TD->getTemplateParameters();
925 mangleTemplateArgs(*TemplateParameters, *TemplateArgs);
Fariborz Jahanian57058532010-03-03 19:41:08 +0000926 }
Douglas Gregor35415f52010-05-25 17:04:15 +0000927 else if(NoFunction && (isa<FunctionDecl>(DC) || isa<ObjCMethodDecl>(DC)))
Fariborz Jahanian57058532010-03-03 19:41:08 +0000928 return;
Douglas Gregor35415f52010-05-25 17:04:15 +0000929 else if (const ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(DC))
930 mangleObjCMethodName(Method);
Fariborz Jahanian57058532010-03-03 19:41:08 +0000931 else {
932 manglePrefix(DC->getParent(), NoFunction);
Anders Carlsson2ee3fca2009-09-18 20:11:09 +0000933 mangleUnqualifiedName(cast<NamedDecl>(DC));
934 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000935
Anders Carlsson6862fc72009-09-17 04:16:28 +0000936 addSubstitution(cast<NamedDecl>(DC));
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000937}
938
Douglas Gregor20f0cc72010-04-23 03:10:43 +0000939void CXXNameMangler::mangleTemplatePrefix(TemplateName Template) {
940 // <template-prefix> ::= <prefix> <template unqualified-name>
941 // ::= <template-param>
942 // ::= <substitution>
943 if (TemplateDecl *TD = Template.getAsTemplateDecl())
944 return mangleTemplatePrefix(TD);
945
946 if (QualifiedTemplateName *Qualified = Template.getAsQualifiedTemplateName())
947 mangleUnresolvedScope(Qualified->getQualifier());
Sean Huntc3021132010-05-05 15:23:54 +0000948
Douglas Gregor20f0cc72010-04-23 03:10:43 +0000949 if (OverloadedTemplateStorage *Overloaded
950 = Template.getAsOverloadedTemplate()) {
Sean Huntc3021132010-05-05 15:23:54 +0000951 mangleUnqualifiedName(0, (*Overloaded->begin())->getDeclName(),
Douglas Gregor20f0cc72010-04-23 03:10:43 +0000952 UnknownArity);
953 return;
954 }
Sean Huntc3021132010-05-05 15:23:54 +0000955
Douglas Gregor20f0cc72010-04-23 03:10:43 +0000956 DependentTemplateName *Dependent = Template.getAsDependentTemplateName();
957 assert(Dependent && "Unknown template name kind?");
958 mangleUnresolvedScope(Dependent->getQualifier());
Douglas Gregor1e9268e2010-04-28 05:58:56 +0000959 mangleUnscopedTemplateName(Template);
Douglas Gregor20f0cc72010-04-23 03:10:43 +0000960}
961
Anders Carlsson0fa6df42009-09-26 19:45:45 +0000962void CXXNameMangler::mangleTemplatePrefix(const TemplateDecl *ND) {
Anders Carlsson7482e242009-09-18 04:29:09 +0000963 // <template-prefix> ::= <prefix> <template unqualified-name>
964 // ::= <template-param>
965 // ::= <substitution>
Douglas Gregor32fb4e12010-02-05 20:45:00 +0000966 // <template-template-param> ::= <template-param>
967 // <substitution>
Anders Carlsson7482e242009-09-18 04:29:09 +0000968
Anders Carlssonaeb85372009-09-26 22:18:22 +0000969 if (mangleSubstitution(ND))
970 return;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000971
Douglas Gregor32fb4e12010-02-05 20:45:00 +0000972 // <template-template-param> ::= <template-param>
973 if (const TemplateTemplateParmDecl *TTP
974 = dyn_cast<TemplateTemplateParmDecl>(ND)) {
975 mangleTemplateParameter(TTP->getIndex());
976 return;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000977 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000978
Anders Carlssonaa73ab12009-09-18 18:47:07 +0000979 manglePrefix(ND->getDeclContext());
Anders Carlsson1668f202009-09-26 20:13:56 +0000980 mangleUnqualifiedName(ND->getTemplatedDecl());
Anders Carlssonaeb85372009-09-26 22:18:22 +0000981 addSubstitution(ND);
Anders Carlsson7482e242009-09-18 04:29:09 +0000982}
983
John McCallb6f532e2010-07-14 06:43:17 +0000984/// Mangles a template name under the production <type>. Required for
985/// template template arguments.
986/// <type> ::= <class-enum-type>
987/// ::= <template-param>
988/// ::= <substitution>
989void CXXNameMangler::mangleType(TemplateName TN) {
990 if (mangleSubstitution(TN))
991 return;
992
993 TemplateDecl *TD = 0;
994
995 switch (TN.getKind()) {
996 case TemplateName::QualifiedTemplate:
997 TD = TN.getAsQualifiedTemplateName()->getTemplateDecl();
998 goto HaveDecl;
999
1000 case TemplateName::Template:
1001 TD = TN.getAsTemplateDecl();
1002 goto HaveDecl;
1003
1004 HaveDecl:
1005 if (isa<TemplateTemplateParmDecl>(TD))
1006 mangleTemplateParameter(cast<TemplateTemplateParmDecl>(TD)->getIndex());
1007 else
1008 mangleName(TD);
1009 break;
1010
1011 case TemplateName::OverloadedTemplate:
1012 llvm_unreachable("can't mangle an overloaded template name as a <type>");
1013 break;
1014
1015 case TemplateName::DependentTemplate: {
1016 const DependentTemplateName *Dependent = TN.getAsDependentTemplateName();
1017 assert(Dependent->isIdentifier());
1018
1019 // <class-enum-type> ::= <name>
1020 // <name> ::= <nested-name>
1021 mangleUnresolvedScope(Dependent->getQualifier());
1022 mangleSourceName(Dependent->getIdentifier());
1023 break;
1024 }
1025
Douglas Gregor1aee05d2011-01-15 06:45:20 +00001026 case TemplateName::SubstTemplateTemplateParmPack: {
1027 SubstTemplateTemplateParmPackStorage *SubstPack
1028 = TN.getAsSubstTemplateTemplateParmPack();
1029 mangleTemplateParameter(SubstPack->getParameterPack()->getIndex());
1030 break;
1031 }
John McCallb6f532e2010-07-14 06:43:17 +00001032 }
1033
1034 addSubstitution(TN);
1035}
1036
Mike Stump1eb44332009-09-09 15:08:12 +00001037void
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001038CXXNameMangler::mangleOperatorName(OverloadedOperatorKind OO, unsigned Arity) {
1039 switch (OO) {
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001040 // <operator-name> ::= nw # new
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001041 case OO_New: Out << "nw"; break;
1042 // ::= na # new[]
1043 case OO_Array_New: Out << "na"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001044 // ::= dl # delete
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001045 case OO_Delete: Out << "dl"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001046 // ::= da # delete[]
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001047 case OO_Array_Delete: Out << "da"; break;
1048 // ::= ps # + (unary)
John McCall5e1e89b2010-08-18 19:18:59 +00001049 // ::= pl # + (binary or unknown)
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001050 case OO_Plus:
Anders Carlsson8257d412009-12-22 06:36:32 +00001051 Out << (Arity == 1? "ps" : "pl"); break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001052 // ::= ng # - (unary)
John McCall5e1e89b2010-08-18 19:18:59 +00001053 // ::= mi # - (binary or unknown)
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001054 case OO_Minus:
Anders Carlsson8257d412009-12-22 06:36:32 +00001055 Out << (Arity == 1? "ng" : "mi"); break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001056 // ::= ad # & (unary)
John McCall5e1e89b2010-08-18 19:18:59 +00001057 // ::= an # & (binary or unknown)
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001058 case OO_Amp:
Anders Carlsson8257d412009-12-22 06:36:32 +00001059 Out << (Arity == 1? "ad" : "an"); break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001060 // ::= de # * (unary)
John McCall5e1e89b2010-08-18 19:18:59 +00001061 // ::= ml # * (binary or unknown)
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001062 case OO_Star:
John McCall5e1e89b2010-08-18 19:18:59 +00001063 // Use binary when unknown.
Anders Carlsson8257d412009-12-22 06:36:32 +00001064 Out << (Arity == 1? "de" : "ml"); break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001065 // ::= co # ~
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001066 case OO_Tilde: Out << "co"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001067 // ::= dv # /
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001068 case OO_Slash: Out << "dv"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001069 // ::= rm # %
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001070 case OO_Percent: Out << "rm"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001071 // ::= or # |
1072 case OO_Pipe: Out << "or"; break;
1073 // ::= eo # ^
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001074 case OO_Caret: Out << "eo"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001075 // ::= aS # =
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001076 case OO_Equal: Out << "aS"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001077 // ::= pL # +=
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001078 case OO_PlusEqual: Out << "pL"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001079 // ::= mI # -=
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001080 case OO_MinusEqual: Out << "mI"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001081 // ::= mL # *=
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001082 case OO_StarEqual: Out << "mL"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001083 // ::= dV # /=
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001084 case OO_SlashEqual: Out << "dV"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001085 // ::= rM # %=
1086 case OO_PercentEqual: Out << "rM"; break;
1087 // ::= aN # &=
1088 case OO_AmpEqual: Out << "aN"; break;
1089 // ::= oR # |=
1090 case OO_PipeEqual: Out << "oR"; break;
1091 // ::= eO # ^=
1092 case OO_CaretEqual: Out << "eO"; break;
1093 // ::= ls # <<
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001094 case OO_LessLess: Out << "ls"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001095 // ::= rs # >>
1096 case OO_GreaterGreater: Out << "rs"; break;
1097 // ::= lS # <<=
1098 case OO_LessLessEqual: Out << "lS"; break;
1099 // ::= rS # >>=
1100 case OO_GreaterGreaterEqual: Out << "rS"; break;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001101 // ::= eq # ==
1102 case OO_EqualEqual: Out << "eq"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001103 // ::= ne # !=
1104 case OO_ExclaimEqual: Out << "ne"; break;
1105 // ::= lt # <
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001106 case OO_Less: Out << "lt"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001107 // ::= gt # >
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001108 case OO_Greater: Out << "gt"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001109 // ::= le # <=
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001110 case OO_LessEqual: Out << "le"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001111 // ::= ge # >=
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001112 case OO_GreaterEqual: Out << "ge"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001113 // ::= nt # !
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001114 case OO_Exclaim: Out << "nt"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001115 // ::= aa # &&
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001116 case OO_AmpAmp: Out << "aa"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001117 // ::= oo # ||
1118 case OO_PipePipe: Out << "oo"; break;
1119 // ::= pp # ++
1120 case OO_PlusPlus: Out << "pp"; break;
1121 // ::= mm # --
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001122 case OO_MinusMinus: Out << "mm"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001123 // ::= cm # ,
1124 case OO_Comma: Out << "cm"; break;
1125 // ::= pm # ->*
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001126 case OO_ArrowStar: Out << "pm"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001127 // ::= pt # ->
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001128 case OO_Arrow: Out << "pt"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001129 // ::= cl # ()
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001130 case OO_Call: Out << "cl"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001131 // ::= ix # []
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001132 case OO_Subscript: Out << "ix"; break;
Anders Carlssone170ba72009-12-14 01:45:37 +00001133
1134 // ::= qu # ?
1135 // The conditional operator can't be overloaded, but we still handle it when
1136 // mangling expressions.
1137 case OO_Conditional: Out << "qu"; break;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001138
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001139 case OO_None:
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001140 case NUM_OVERLOADED_OPERATORS:
Mike Stump1eb44332009-09-09 15:08:12 +00001141 assert(false && "Not an overloaded operator");
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001142 break;
1143 }
1144}
1145
John McCall0953e762009-09-24 19:53:00 +00001146void CXXNameMangler::mangleQualifiers(Qualifiers Quals) {
Mike Stump1eb44332009-09-09 15:08:12 +00001147 // <CV-qualifiers> ::= [r] [V] [K] # restrict (C99), volatile, const
John McCall0953e762009-09-24 19:53:00 +00001148 if (Quals.hasRestrict())
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001149 Out << 'r';
John McCall0953e762009-09-24 19:53:00 +00001150 if (Quals.hasVolatile())
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001151 Out << 'V';
John McCall0953e762009-09-24 19:53:00 +00001152 if (Quals.hasConst())
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001153 Out << 'K';
John McCall0953e762009-09-24 19:53:00 +00001154
Douglas Gregor56079f72010-06-14 23:15:08 +00001155 if (Quals.hasAddressSpace()) {
1156 // Extension:
1157 //
1158 // <type> ::= U <address-space-number>
1159 //
1160 // where <address-space-number> is a source name consisting of 'AS'
1161 // followed by the address space <number>.
1162 llvm::SmallString<64> ASString;
1163 ASString = "AS" + llvm::utostr_32(Quals.getAddressSpace());
1164 Out << 'U' << ASString.size() << ASString;
1165 }
1166
John McCall0953e762009-09-24 19:53:00 +00001167 // FIXME: For now, just drop all extension qualifiers on the floor.
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001168}
1169
Douglas Gregor0a9a6d62011-01-26 17:36:28 +00001170void CXXNameMangler::mangleRefQualifier(RefQualifierKind RefQualifier) {
1171 // <ref-qualifier> ::= R # lvalue reference
1172 // ::= O # rvalue-reference
1173 // Proposal to Itanium C++ ABI list on 1/26/11
1174 switch (RefQualifier) {
1175 case RQ_None:
1176 break;
1177
1178 case RQ_LValue:
1179 Out << 'R';
1180 break;
1181
1182 case RQ_RValue:
1183 Out << 'O';
1184 break;
1185 }
1186}
1187
Anders Carlsson7b06f6c2009-12-10 03:14:39 +00001188void CXXNameMangler::mangleObjCMethodName(const ObjCMethodDecl *MD) {
Charles Davis685b1d92010-05-26 18:25:27 +00001189 llvm::SmallString<64> Buffer;
Peter Collingbourne14110472011-01-13 18:57:25 +00001190 Context.mangleObjCMethodName(MD, Buffer);
Charles Davis685b1d92010-05-26 18:25:27 +00001191 Out << Buffer;
Anders Carlsson7b06f6c2009-12-10 03:14:39 +00001192}
1193
John McCallb47f7482011-01-26 20:05:40 +00001194void CXXNameMangler::mangleType(QualType nonCanon) {
Anders Carlsson4843e582009-03-10 17:07:44 +00001195 // Only operate on the canonical type!
John McCallb47f7482011-01-26 20:05:40 +00001196 QualType canon = nonCanon.getCanonicalType();
Anders Carlsson4843e582009-03-10 17:07:44 +00001197
John McCallb47f7482011-01-26 20:05:40 +00001198 SplitQualType split = canon.split();
1199 Qualifiers quals = split.second;
1200 const Type *ty = split.first;
1201
1202 bool isSubstitutable = quals || !isa<BuiltinType>(ty);
1203 if (isSubstitutable && mangleSubstitution(canon))
Anders Carlsson76967372009-09-17 00:43:46 +00001204 return;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001205
John McCallb47f7482011-01-26 20:05:40 +00001206 // If we're mangling a qualified array type, push the qualifiers to
1207 // the element type.
1208 if (quals && isa<ArrayType>(ty)) {
1209 ty = Context.getASTContext().getAsArrayType(canon);
1210 quals = Qualifiers();
1211
1212 // Note that we don't update canon: we want to add the
1213 // substitution at the canonical type.
1214 }
1215
1216 if (quals) {
1217 mangleQualifiers(quals);
John McCall0953e762009-09-24 19:53:00 +00001218 // Recurse: even if the qualified type isn't yet substitutable,
1219 // the unqualified type might be.
John McCallb47f7482011-01-26 20:05:40 +00001220 mangleType(QualType(ty, 0));
Anders Carlsson76967372009-09-17 00:43:46 +00001221 } else {
John McCallb47f7482011-01-26 20:05:40 +00001222 switch (ty->getTypeClass()) {
John McCallefe6aee2009-09-05 07:56:18 +00001223#define ABSTRACT_TYPE(CLASS, PARENT)
1224#define NON_CANONICAL_TYPE(CLASS, PARENT) \
Anders Carlsson76967372009-09-17 00:43:46 +00001225 case Type::CLASS: \
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +00001226 llvm_unreachable("can't mangle non-canonical type " #CLASS "Type"); \
Anders Carlsson76967372009-09-17 00:43:46 +00001227 return;
John McCallefe6aee2009-09-05 07:56:18 +00001228#define TYPE(CLASS, PARENT) \
Anders Carlsson76967372009-09-17 00:43:46 +00001229 case Type::CLASS: \
John McCallb47f7482011-01-26 20:05:40 +00001230 mangleType(static_cast<const CLASS##Type*>(ty)); \
Anders Carlsson76967372009-09-17 00:43:46 +00001231 break;
John McCallefe6aee2009-09-05 07:56:18 +00001232#include "clang/AST/TypeNodes.def"
Anders Carlsson76967372009-09-17 00:43:46 +00001233 }
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001234 }
Anders Carlsson76967372009-09-17 00:43:46 +00001235
1236 // Add the substitution.
John McCallb47f7482011-01-26 20:05:40 +00001237 if (isSubstitutable)
1238 addSubstitution(canon);
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001239}
1240
Douglas Gregor1b12a3b2010-05-26 05:11:13 +00001241void CXXNameMangler::mangleNameOrStandardSubstitution(const NamedDecl *ND) {
1242 if (!mangleStandardSubstitution(ND))
1243 mangleName(ND);
1244}
1245
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001246void CXXNameMangler::mangleType(const BuiltinType *T) {
John McCallefe6aee2009-09-05 07:56:18 +00001247 // <type> ::= <builtin-type>
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001248 // <builtin-type> ::= v # void
1249 // ::= w # wchar_t
1250 // ::= b # bool
1251 // ::= c # char
1252 // ::= a # signed char
1253 // ::= h # unsigned char
1254 // ::= s # short
1255 // ::= t # unsigned short
1256 // ::= i # int
1257 // ::= j # unsigned int
1258 // ::= l # long
1259 // ::= m # unsigned long
1260 // ::= x # long long, __int64
1261 // ::= y # unsigned long long, __int64
1262 // ::= n # __int128
1263 // UNSUPPORTED: ::= o # unsigned __int128
1264 // ::= f # float
1265 // ::= d # double
1266 // ::= e # long double, __float80
1267 // UNSUPPORTED: ::= g # __float128
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001268 // UNSUPPORTED: ::= Dd # IEEE 754r decimal floating point (64 bits)
1269 // UNSUPPORTED: ::= De # IEEE 754r decimal floating point (128 bits)
1270 // UNSUPPORTED: ::= Df # IEEE 754r decimal floating point (32 bits)
1271 // UNSUPPORTED: ::= Dh # IEEE 754r half-precision floating point (16 bits)
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001272 // ::= Di # char32_t
1273 // ::= Ds # char16_t
Anders Carlssone2923682010-11-04 04:31:32 +00001274 // ::= Dn # std::nullptr_t (i.e., decltype(nullptr))
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001275 // ::= u <source-name> # vendor extended type
1276 switch (T->getKind()) {
1277 case BuiltinType::Void: Out << 'v'; break;
1278 case BuiltinType::Bool: Out << 'b'; break;
1279 case BuiltinType::Char_U: case BuiltinType::Char_S: Out << 'c'; break;
1280 case BuiltinType::UChar: Out << 'h'; break;
1281 case BuiltinType::UShort: Out << 't'; break;
1282 case BuiltinType::UInt: Out << 'j'; break;
1283 case BuiltinType::ULong: Out << 'm'; break;
1284 case BuiltinType::ULongLong: Out << 'y'; break;
Chris Lattner2df9ced2009-04-30 02:43:43 +00001285 case BuiltinType::UInt128: Out << 'o'; break;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001286 case BuiltinType::SChar: Out << 'a'; break;
Chris Lattner3f59c972010-12-25 23:25:43 +00001287 case BuiltinType::WChar_S:
1288 case BuiltinType::WChar_U: Out << 'w'; break;
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001289 case BuiltinType::Char16: Out << "Ds"; break;
1290 case BuiltinType::Char32: Out << "Di"; break;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001291 case BuiltinType::Short: Out << 's'; break;
1292 case BuiltinType::Int: Out << 'i'; break;
1293 case BuiltinType::Long: Out << 'l'; break;
1294 case BuiltinType::LongLong: Out << 'x'; break;
Chris Lattner2df9ced2009-04-30 02:43:43 +00001295 case BuiltinType::Int128: Out << 'n'; break;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001296 case BuiltinType::Float: Out << 'f'; break;
1297 case BuiltinType::Double: Out << 'd'; break;
1298 case BuiltinType::LongDouble: Out << 'e'; break;
Anders Carlssone2923682010-11-04 04:31:32 +00001299 case BuiltinType::NullPtr: Out << "Dn"; break;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001300
1301 case BuiltinType::Overload:
1302 case BuiltinType::Dependent:
Mike Stump1eb44332009-09-09 15:08:12 +00001303 assert(false &&
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001304 "Overloaded and dependent types shouldn't get to name mangling");
1305 break;
Anders Carlssone89d1592009-06-26 18:41:36 +00001306 case BuiltinType::UndeducedAuto:
1307 assert(0 && "Should not see undeduced auto here");
1308 break;
Steve Naroff9533a7f2009-07-22 17:14:51 +00001309 case BuiltinType::ObjCId: Out << "11objc_object"; break;
1310 case BuiltinType::ObjCClass: Out << "10objc_class"; break;
Fariborz Jahanian13dcd002009-11-21 19:53:08 +00001311 case BuiltinType::ObjCSel: Out << "13objc_selector"; break;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001312 }
1313}
1314
John McCallefe6aee2009-09-05 07:56:18 +00001315// <type> ::= <function-type>
1316// <function-type> ::= F [Y] <bare-function-type> E
1317void CXXNameMangler::mangleType(const FunctionProtoType *T) {
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001318 Out << 'F';
Mike Stumpf5408fe2009-05-16 07:57:57 +00001319 // FIXME: We don't have enough information in the AST to produce the 'Y'
1320 // encoding for extern "C" function types.
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001321 mangleBareFunctionType(T, /*MangleReturnType=*/true);
1322 Out << 'E';
1323}
John McCallefe6aee2009-09-05 07:56:18 +00001324void CXXNameMangler::mangleType(const FunctionNoProtoType *T) {
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +00001325 llvm_unreachable("Can't mangle K&R function prototypes");
John McCallefe6aee2009-09-05 07:56:18 +00001326}
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001327void CXXNameMangler::mangleBareFunctionType(const FunctionType *T,
1328 bool MangleReturnType) {
John McCallefe6aee2009-09-05 07:56:18 +00001329 // We should never be mangling something without a prototype.
1330 const FunctionProtoType *Proto = cast<FunctionProtoType>(T);
1331
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001332 // <bare-function-type> ::= <signature type>+
1333 if (MangleReturnType)
John McCallefe6aee2009-09-05 07:56:18 +00001334 mangleType(Proto->getResultType());
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001335
Anders Carlsson93296682010-06-02 04:40:13 +00001336 if (Proto->getNumArgs() == 0 && !Proto->isVariadic()) {
Eli Friedmana7e68452010-08-22 01:00:03 +00001337 // <builtin-type> ::= v # void
Anders Carlssonc6c91bc2009-04-01 00:15:23 +00001338 Out << 'v';
1339 return;
1340 }
Mike Stump1eb44332009-09-09 15:08:12 +00001341
Douglas Gregor72564e72009-02-26 23:50:07 +00001342 for (FunctionProtoType::arg_type_iterator Arg = Proto->arg_type_begin(),
Mike Stump1eb44332009-09-09 15:08:12 +00001343 ArgEnd = Proto->arg_type_end();
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001344 Arg != ArgEnd; ++Arg)
1345 mangleType(*Arg);
Douglas Gregor219cc612009-02-13 01:28:03 +00001346
1347 // <builtin-type> ::= z # ellipsis
1348 if (Proto->isVariadic())
1349 Out << 'z';
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001350}
1351
John McCallefe6aee2009-09-05 07:56:18 +00001352// <type> ::= <class-enum-type>
Mike Stump1eb44332009-09-09 15:08:12 +00001353// <class-enum-type> ::= <name>
John McCalled976492009-12-04 22:46:56 +00001354void CXXNameMangler::mangleType(const UnresolvedUsingType *T) {
1355 mangleName(T->getDecl());
1356}
1357
1358// <type> ::= <class-enum-type>
1359// <class-enum-type> ::= <name>
John McCallefe6aee2009-09-05 07:56:18 +00001360void CXXNameMangler::mangleType(const EnumType *T) {
1361 mangleType(static_cast<const TagType*>(T));
1362}
1363void CXXNameMangler::mangleType(const RecordType *T) {
1364 mangleType(static_cast<const TagType*>(T));
1365}
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001366void CXXNameMangler::mangleType(const TagType *T) {
Eli Friedmanecb7e932009-12-11 18:00:57 +00001367 mangleName(T->getDecl());
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001368}
1369
John McCallefe6aee2009-09-05 07:56:18 +00001370// <type> ::= <array-type>
1371// <array-type> ::= A <positive dimension number> _ <element type>
1372// ::= A [<dimension expression>] _ <element type>
1373void CXXNameMangler::mangleType(const ConstantArrayType *T) {
1374 Out << 'A' << T->getSize() << '_';
1375 mangleType(T->getElementType());
1376}
1377void CXXNameMangler::mangleType(const VariableArrayType *T) {
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001378 Out << 'A';
Fariborz Jahanian7281d1f2010-11-02 16:54:00 +00001379 // decayed vla types (size 0) will just be skipped.
1380 if (T->getSizeExpr())
1381 mangleExpression(T->getSizeExpr());
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001382 Out << '_';
1383 mangleType(T->getElementType());
1384}
John McCallefe6aee2009-09-05 07:56:18 +00001385void CXXNameMangler::mangleType(const DependentSizedArrayType *T) {
1386 Out << 'A';
1387 mangleExpression(T->getSizeExpr());
1388 Out << '_';
1389 mangleType(T->getElementType());
1390}
1391void CXXNameMangler::mangleType(const IncompleteArrayType *T) {
Nick Lewycky271b6652010-09-05 03:40:33 +00001392 Out << "A_";
John McCallefe6aee2009-09-05 07:56:18 +00001393 mangleType(T->getElementType());
1394}
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001395
John McCallefe6aee2009-09-05 07:56:18 +00001396// <type> ::= <pointer-to-member-type>
1397// <pointer-to-member-type> ::= M <class type> <member type>
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001398void CXXNameMangler::mangleType(const MemberPointerType *T) {
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001399 Out << 'M';
1400 mangleType(QualType(T->getClass(), 0));
Anders Carlsson0e650012009-05-17 17:41:20 +00001401 QualType PointeeType = T->getPointeeType();
1402 if (const FunctionProtoType *FPT = dyn_cast<FunctionProtoType>(PointeeType)) {
John McCall0953e762009-09-24 19:53:00 +00001403 mangleQualifiers(Qualifiers::fromCVRMask(FPT->getTypeQuals()));
Douglas Gregor0a9a6d62011-01-26 17:36:28 +00001404 mangleRefQualifier(FPT->getRefQualifier());
Anders Carlsson0e650012009-05-17 17:41:20 +00001405 mangleType(FPT);
Anders Carlsson9d85b722010-06-02 04:29:50 +00001406
1407 // Itanium C++ ABI 5.1.8:
1408 //
1409 // The type of a non-static member function is considered to be different,
1410 // for the purposes of substitution, from the type of a namespace-scope or
1411 // static member function whose type appears similar. The types of two
1412 // non-static member functions are considered to be different, for the
1413 // purposes of substitution, if the functions are members of different
1414 // classes. In other words, for the purposes of substitution, the class of
1415 // which the function is a member is considered part of the type of
1416 // function.
1417
1418 // We increment the SeqID here to emulate adding an entry to the
1419 // substitution table. We can't actually add it because we don't want this
1420 // particular function type to be substituted.
1421 ++SeqID;
Mike Stump1eb44332009-09-09 15:08:12 +00001422 } else
Anders Carlsson0e650012009-05-17 17:41:20 +00001423 mangleType(PointeeType);
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001424}
1425
John McCallefe6aee2009-09-05 07:56:18 +00001426// <type> ::= <template-param>
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001427void CXXNameMangler::mangleType(const TemplateTypeParmType *T) {
Anders Carlsson0ccdf8d2009-09-27 00:38:53 +00001428 mangleTemplateParameter(T->getIndex());
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001429}
1430
Douglas Gregorc3069d62011-01-14 02:55:32 +00001431// <type> ::= <template-param>
1432void CXXNameMangler::mangleType(const SubstTemplateTypeParmPackType *T) {
1433 mangleTemplateParameter(T->getReplacedParameter()->getIndex());
1434}
1435
John McCallefe6aee2009-09-05 07:56:18 +00001436// <type> ::= P <type> # pointer-to
1437void CXXNameMangler::mangleType(const PointerType *T) {
1438 Out << 'P';
1439 mangleType(T->getPointeeType());
1440}
1441void CXXNameMangler::mangleType(const ObjCObjectPointerType *T) {
1442 Out << 'P';
1443 mangleType(T->getPointeeType());
1444}
1445
1446// <type> ::= R <type> # reference-to
1447void CXXNameMangler::mangleType(const LValueReferenceType *T) {
1448 Out << 'R';
1449 mangleType(T->getPointeeType());
1450}
1451
1452// <type> ::= O <type> # rvalue reference-to (C++0x)
1453void CXXNameMangler::mangleType(const RValueReferenceType *T) {
1454 Out << 'O';
1455 mangleType(T->getPointeeType());
1456}
1457
1458// <type> ::= C <type> # complex pair (C 2000)
1459void CXXNameMangler::mangleType(const ComplexType *T) {
1460 Out << 'C';
1461 mangleType(T->getElementType());
1462}
1463
Bob Wilsonc7df92d2010-11-12 17:24:43 +00001464// ARM's ABI for Neon vector types specifies that they should be mangled as
Bob Wilson57147a82010-11-16 00:32:18 +00001465// if they are structs (to match ARM's initial implementation). The
1466// vector type must be one of the special types predefined by ARM.
1467void CXXNameMangler::mangleNeonVectorType(const VectorType *T) {
Bob Wilsonc7df92d2010-11-12 17:24:43 +00001468 QualType EltType = T->getElementType();
Bob Wilson57147a82010-11-16 00:32:18 +00001469 assert(EltType->isBuiltinType() && "Neon vector element not a BuiltinType");
Bob Wilsonc7df92d2010-11-12 17:24:43 +00001470 const char *EltName = 0;
Bob Wilson491328c2010-11-12 17:24:46 +00001471 if (T->getVectorKind() == VectorType::NeonPolyVector) {
1472 switch (cast<BuiltinType>(EltType)->getKind()) {
Bob Wilson4cfaa5d2010-11-12 17:24:49 +00001473 case BuiltinType::SChar: EltName = "poly8_t"; break;
1474 case BuiltinType::Short: EltName = "poly16_t"; break;
Bob Wilson57147a82010-11-16 00:32:18 +00001475 default: llvm_unreachable("unexpected Neon polynomial vector element type");
Bob Wilson491328c2010-11-12 17:24:46 +00001476 }
1477 } else {
1478 switch (cast<BuiltinType>(EltType)->getKind()) {
Bob Wilson4cfaa5d2010-11-12 17:24:49 +00001479 case BuiltinType::SChar: EltName = "int8_t"; break;
1480 case BuiltinType::UChar: EltName = "uint8_t"; break;
1481 case BuiltinType::Short: EltName = "int16_t"; break;
1482 case BuiltinType::UShort: EltName = "uint16_t"; break;
1483 case BuiltinType::Int: EltName = "int32_t"; break;
1484 case BuiltinType::UInt: EltName = "uint32_t"; break;
1485 case BuiltinType::LongLong: EltName = "int64_t"; break;
1486 case BuiltinType::ULongLong: EltName = "uint64_t"; break;
1487 case BuiltinType::Float: EltName = "float32_t"; break;
Bob Wilson57147a82010-11-16 00:32:18 +00001488 default: llvm_unreachable("unexpected Neon vector element type");
Bob Wilson491328c2010-11-12 17:24:46 +00001489 }
Bob Wilsonc7df92d2010-11-12 17:24:43 +00001490 }
1491 const char *BaseName = 0;
Bob Wilson4cfaa5d2010-11-12 17:24:49 +00001492 unsigned BitSize = (T->getNumElements() *
Bob Wilson3a723022010-11-16 00:32:12 +00001493 getASTContext().getTypeSize(EltType));
Bob Wilsonc7df92d2010-11-12 17:24:43 +00001494 if (BitSize == 64)
1495 BaseName = "__simd64_";
Bob Wilson57147a82010-11-16 00:32:18 +00001496 else {
1497 assert(BitSize == 128 && "Neon vector type not 64 or 128 bits");
Bob Wilsonc7df92d2010-11-12 17:24:43 +00001498 BaseName = "__simd128_";
Bob Wilson57147a82010-11-16 00:32:18 +00001499 }
Bob Wilsonc7df92d2010-11-12 17:24:43 +00001500 Out << strlen(BaseName) + strlen(EltName);
1501 Out << BaseName << EltName;
Bob Wilsonc7df92d2010-11-12 17:24:43 +00001502}
1503
John McCallefe6aee2009-09-05 07:56:18 +00001504// GNU extension: vector types
Chris Lattner788b0fd2010-06-23 06:00:24 +00001505// <type> ::= <vector-type>
1506// <vector-type> ::= Dv <positive dimension number> _
1507// <extended element type>
1508// ::= Dv [<dimension expression>] _ <element type>
1509// <extended element type> ::= <element type>
1510// ::= p # AltiVec vector pixel
John McCallefe6aee2009-09-05 07:56:18 +00001511void CXXNameMangler::mangleType(const VectorType *T) {
Bob Wilson491328c2010-11-12 17:24:46 +00001512 if ((T->getVectorKind() == VectorType::NeonVector ||
Bob Wilson57147a82010-11-16 00:32:18 +00001513 T->getVectorKind() == VectorType::NeonPolyVector)) {
1514 mangleNeonVectorType(T);
Bob Wilsonc7df92d2010-11-12 17:24:43 +00001515 return;
Bob Wilson57147a82010-11-16 00:32:18 +00001516 }
Nick Lewycky0e5f0672010-03-26 07:18:04 +00001517 Out << "Dv" << T->getNumElements() << '_';
Bob Wilsone86d78c2010-11-10 21:56:12 +00001518 if (T->getVectorKind() == VectorType::AltiVecPixel)
Chris Lattner788b0fd2010-06-23 06:00:24 +00001519 Out << 'p';
Bob Wilsone86d78c2010-11-10 21:56:12 +00001520 else if (T->getVectorKind() == VectorType::AltiVecBool)
Chris Lattner788b0fd2010-06-23 06:00:24 +00001521 Out << 'b';
1522 else
1523 mangleType(T->getElementType());
John McCallefe6aee2009-09-05 07:56:18 +00001524}
1525void CXXNameMangler::mangleType(const ExtVectorType *T) {
1526 mangleType(static_cast<const VectorType*>(T));
1527}
1528void CXXNameMangler::mangleType(const DependentSizedExtVectorType *T) {
Nick Lewycky0e5f0672010-03-26 07:18:04 +00001529 Out << "Dv";
1530 mangleExpression(T->getSizeExpr());
1531 Out << '_';
John McCallefe6aee2009-09-05 07:56:18 +00001532 mangleType(T->getElementType());
1533}
1534
Douglas Gregor7536dd52010-12-20 02:24:11 +00001535void CXXNameMangler::mangleType(const PackExpansionType *T) {
Douglas Gregor4fc48662011-01-13 16:39:34 +00001536 // <type> ::= Dp <type> # pack expansion (C++0x)
Douglas Gregor255c2692011-01-13 17:44:36 +00001537 Out << "Dp";
Douglas Gregor7536dd52010-12-20 02:24:11 +00001538 mangleType(T->getPattern());
1539}
1540
Anders Carlssona40c5e42009-03-07 22:03:21 +00001541void CXXNameMangler::mangleType(const ObjCInterfaceType *T) {
1542 mangleSourceName(T->getDecl()->getIdentifier());
1543}
1544
John McCallc12c5bb2010-05-15 11:32:37 +00001545void CXXNameMangler::mangleType(const ObjCObjectType *T) {
John McCallc00c1f62010-05-15 17:06:29 +00001546 // We don't allow overloading by different protocol qualification,
1547 // so mangling them isn't necessary.
John McCallc12c5bb2010-05-15 11:32:37 +00001548 mangleType(T->getBaseType());
1549}
1550
John McCallefe6aee2009-09-05 07:56:18 +00001551void CXXNameMangler::mangleType(const BlockPointerType *T) {
Anders Carlssonf28c6872009-12-23 22:31:44 +00001552 Out << "U13block_pointer";
1553 mangleType(T->getPointeeType());
John McCallefe6aee2009-09-05 07:56:18 +00001554}
1555
John McCall31f17ec2010-04-27 00:57:59 +00001556void CXXNameMangler::mangleType(const InjectedClassNameType *T) {
1557 // Mangle injected class name types as if the user had written the
1558 // specialization out fully. It may not actually be possible to see
1559 // this mangling, though.
1560 mangleType(T->getInjectedSpecializationType());
1561}
1562
John McCallefe6aee2009-09-05 07:56:18 +00001563void CXXNameMangler::mangleType(const TemplateSpecializationType *T) {
Douglas Gregor1e9268e2010-04-28 05:58:56 +00001564 if (TemplateDecl *TD = T->getTemplateName().getAsTemplateDecl()) {
1565 mangleName(TD, T->getArgs(), T->getNumArgs());
1566 } else {
1567 if (mangleSubstitution(QualType(T, 0)))
1568 return;
Sean Huntc3021132010-05-05 15:23:54 +00001569
Douglas Gregor1e9268e2010-04-28 05:58:56 +00001570 mangleTemplatePrefix(T->getTemplateName());
Sean Huntc3021132010-05-05 15:23:54 +00001571
Douglas Gregor1e9268e2010-04-28 05:58:56 +00001572 // FIXME: GCC does not appear to mangle the template arguments when
1573 // the template in question is a dependent template name. Should we
1574 // emulate that badness?
1575 mangleTemplateArgs(T->getTemplateName(), T->getArgs(), T->getNumArgs());
1576 addSubstitution(QualType(T, 0));
1577 }
John McCallefe6aee2009-09-05 07:56:18 +00001578}
1579
Douglas Gregor4714c122010-03-31 17:34:00 +00001580void CXXNameMangler::mangleType(const DependentNameType *T) {
Anders Carlssonae352482009-09-26 02:26:02 +00001581 // Typename types are always nested
1582 Out << 'N';
John McCall33500952010-06-11 00:33:02 +00001583 mangleUnresolvedScope(T->getQualifier());
1584 mangleSourceName(T->getIdentifier());
1585 Out << 'E';
1586}
John McCall6ab30e02010-06-09 07:26:17 +00001587
John McCall33500952010-06-11 00:33:02 +00001588void CXXNameMangler::mangleType(const DependentTemplateSpecializationType *T) {
1589 // Dependently-scoped template types are always nested
1590 Out << 'N';
1591
1592 // TODO: avoid making this TemplateName.
1593 TemplateName Prefix =
1594 getASTContext().getDependentTemplateName(T->getQualifier(),
1595 T->getIdentifier());
1596 mangleTemplatePrefix(Prefix);
1597
1598 // FIXME: GCC does not appear to mangle the template arguments when
1599 // the template in question is a dependent template name. Should we
1600 // emulate that badness?
1601 mangleTemplateArgs(Prefix, T->getArgs(), T->getNumArgs());
Anders Carlssonae352482009-09-26 02:26:02 +00001602 Out << 'E';
John McCallefe6aee2009-09-05 07:56:18 +00001603}
1604
John McCallad5e7382010-03-01 23:49:17 +00001605void CXXNameMangler::mangleType(const TypeOfType *T) {
1606 // FIXME: this is pretty unsatisfactory, but there isn't an obvious
1607 // "extension with parameters" mangling.
1608 Out << "u6typeof";
1609}
1610
1611void CXXNameMangler::mangleType(const TypeOfExprType *T) {
1612 // FIXME: this is pretty unsatisfactory, but there isn't an obvious
1613 // "extension with parameters" mangling.
1614 Out << "u6typeof";
1615}
1616
1617void CXXNameMangler::mangleType(const DecltypeType *T) {
1618 Expr *E = T->getUnderlyingExpr();
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001619
John McCallad5e7382010-03-01 23:49:17 +00001620 // type ::= Dt <expression> E # decltype of an id-expression
1621 // # or class member access
1622 // ::= DT <expression> E # decltype of an expression
1623
1624 // This purports to be an exhaustive list of id-expressions and
1625 // class member accesses. Note that we do not ignore parentheses;
1626 // parentheses change the semantics of decltype for these
1627 // expressions (and cause the mangler to use the other form).
1628 if (isa<DeclRefExpr>(E) ||
1629 isa<MemberExpr>(E) ||
1630 isa<UnresolvedLookupExpr>(E) ||
1631 isa<DependentScopeDeclRefExpr>(E) ||
1632 isa<CXXDependentScopeMemberExpr>(E) ||
1633 isa<UnresolvedMemberExpr>(E))
1634 Out << "Dt";
1635 else
1636 Out << "DT";
1637 mangleExpression(E);
1638 Out << 'E';
1639}
1640
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001641void CXXNameMangler::mangleIntegerLiteral(QualType T,
Anders Carlssone170ba72009-12-14 01:45:37 +00001642 const llvm::APSInt &Value) {
1643 // <expr-primary> ::= L <type> <value number> E # integer literal
1644 Out << 'L';
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001645
Anders Carlssone170ba72009-12-14 01:45:37 +00001646 mangleType(T);
1647 if (T->isBooleanType()) {
1648 // Boolean values are encoded as 0/1.
1649 Out << (Value.getBoolValue() ? '1' : '0');
1650 } else {
John McCall0512e482010-07-14 04:20:34 +00001651 mangleNumber(Value);
Anders Carlssone170ba72009-12-14 01:45:37 +00001652 }
1653 Out << 'E';
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001654
Anders Carlssone170ba72009-12-14 01:45:37 +00001655}
1656
John McCall2f27bf82010-02-04 02:56:29 +00001657/// Mangles a member expression. Implicit accesses are not handled,
1658/// but that should be okay, because you shouldn't be able to
1659/// make an implicit access in a function template declaration.
John McCall2f27bf82010-02-04 02:56:29 +00001660void CXXNameMangler::mangleMemberExpr(const Expr *Base,
1661 bool IsArrow,
1662 NestedNameSpecifier *Qualifier,
1663 DeclarationName Member,
1664 unsigned Arity) {
John McCalle1e342f2010-03-01 19:12:25 +00001665 // gcc-4.4 uses 'dt' for dot expressions, which is reasonable.
1666 // OTOH, gcc also mangles the name as an expression.
1667 Out << (IsArrow ? "pt" : "dt");
John McCall2f27bf82010-02-04 02:56:29 +00001668 mangleExpression(Base);
1669 mangleUnresolvedName(Qualifier, Member, Arity);
1670}
1671
John McCall5e1e89b2010-08-18 19:18:59 +00001672void CXXNameMangler::mangleExpression(const Expr *E, unsigned Arity) {
Anders Carlssond553f8c2009-09-21 01:21:10 +00001673 // <expression> ::= <unary operator-name> <expression>
John McCall09cc1412010-02-03 00:55:45 +00001674 // ::= <binary operator-name> <expression> <expression>
1675 // ::= <trinary operator-name> <expression> <expression> <expression>
Eli Friedmana7e68452010-08-22 01:00:03 +00001676 // ::= cl <expression>* E # call
Anders Carlssond553f8c2009-09-21 01:21:10 +00001677 // ::= cv <type> expression # conversion with one argument
1678 // ::= cv <type> _ <expression>* E # conversion with a different number of arguments
Eli Friedmana7e68452010-08-22 01:00:03 +00001679 // ::= st <type> # sizeof (a type)
Anders Carlssond553f8c2009-09-21 01:21:10 +00001680 // ::= at <type> # alignof (a type)
1681 // ::= <template-param>
1682 // ::= <function-param>
1683 // ::= sr <type> <unqualified-name> # dependent name
1684 // ::= sr <type> <unqualified-name> <template-args> # dependent template-id
1685 // ::= sZ <template-param> # size of a parameter pack
Douglas Gregor4fc48662011-01-13 16:39:34 +00001686 // ::= sZ <function-param> # size of a function parameter pack
John McCall09cc1412010-02-03 00:55:45 +00001687 // ::= <expr-primary>
John McCall1dd73832010-02-04 01:42:13 +00001688 // <expr-primary> ::= L <type> <value number> E # integer literal
1689 // ::= L <type <value float> E # floating literal
1690 // ::= L <mangled-name> E # external name
Anders Carlssond553f8c2009-09-21 01:21:10 +00001691 switch (E->getStmtClass()) {
John McCall6ae1f352010-04-09 22:26:14 +00001692 case Expr::NoStmtClass:
John McCall63c00d72011-02-09 08:16:59 +00001693#define ABSTRACT_STMT(Type)
John McCall6ae1f352010-04-09 22:26:14 +00001694#define EXPR(Type, Base)
1695#define STMT(Type, Base) \
1696 case Expr::Type##Class:
Sean Hunt4bfe1962010-05-05 15:24:00 +00001697#include "clang/AST/StmtNodes.inc"
John McCall0512e482010-07-14 04:20:34 +00001698 // fallthrough
1699
1700 // These all can only appear in local or variable-initialization
1701 // contexts and so should never appear in a mangling.
1702 case Expr::AddrLabelExprClass:
1703 case Expr::BlockDeclRefExprClass:
1704 case Expr::CXXThisExprClass:
1705 case Expr::DesignatedInitExprClass:
1706 case Expr::ImplicitValueInitExprClass:
1707 case Expr::InitListExprClass:
1708 case Expr::ParenListExprClass:
1709 case Expr::CXXScalarValueInitExprClass:
John McCall09cc1412010-02-03 00:55:45 +00001710 llvm_unreachable("unexpected statement kind");
1711 break;
1712
John McCall0512e482010-07-14 04:20:34 +00001713 // FIXME: invent manglings for all these.
1714 case Expr::BlockExprClass:
1715 case Expr::CXXPseudoDestructorExprClass:
1716 case Expr::ChooseExprClass:
1717 case Expr::CompoundLiteralExprClass:
1718 case Expr::ExtVectorElementExprClass:
1719 case Expr::ObjCEncodeExprClass:
John McCall0512e482010-07-14 04:20:34 +00001720 case Expr::ObjCIsaExprClass:
1721 case Expr::ObjCIvarRefExprClass:
1722 case Expr::ObjCMessageExprClass:
1723 case Expr::ObjCPropertyRefExprClass:
1724 case Expr::ObjCProtocolExprClass:
1725 case Expr::ObjCSelectorExprClass:
1726 case Expr::ObjCStringLiteralClass:
John McCall0512e482010-07-14 04:20:34 +00001727 case Expr::OffsetOfExprClass:
1728 case Expr::PredefinedExprClass:
1729 case Expr::ShuffleVectorExprClass:
1730 case Expr::StmtExprClass:
John McCall0512e482010-07-14 04:20:34 +00001731 case Expr::UnaryTypeTraitExprClass:
Francois Pichet6ad6f282010-12-07 00:08:36 +00001732 case Expr::BinaryTypeTraitExprClass:
Francois Pichet9be88402010-09-08 23:47:05 +00001733 case Expr::VAArgExprClass:
Sebastian Redl2e156222010-09-10 20:55:43 +00001734 case Expr::CXXUuidofExprClass:
Peter Collingbournee08ce652011-02-09 21:07:24 +00001735 case Expr::CXXNoexceptExprClass:
1736 case Expr::CUDAKernelCallExprClass: {
John McCall6ae1f352010-04-09 22:26:14 +00001737 // As bad as this diagnostic is, it's better than crashing.
1738 Diagnostic &Diags = Context.getDiags();
1739 unsigned DiagID = Diags.getCustomDiagID(Diagnostic::Error,
1740 "cannot yet mangle expression type %0");
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +00001741 Diags.Report(E->getExprLoc(), DiagID)
John McCall739bf092010-04-10 09:39:25 +00001742 << E->getStmtClassName() << E->getSourceRange();
John McCall6ae1f352010-04-09 22:26:14 +00001743 break;
1744 }
1745
John McCall7cd7d1a2010-11-15 23:31:06 +00001746 case Expr::OpaqueValueExprClass:
1747 llvm_unreachable("cannot mangle opaque value; mangling wrong thing?");
1748
John McCall0512e482010-07-14 04:20:34 +00001749 case Expr::CXXDefaultArgExprClass:
John McCall5e1e89b2010-08-18 19:18:59 +00001750 mangleExpression(cast<CXXDefaultArgExpr>(E)->getExpr(), Arity);
John McCall0512e482010-07-14 04:20:34 +00001751 break;
1752
1753 case Expr::CXXMemberCallExprClass: // fallthrough
John McCall1dd73832010-02-04 01:42:13 +00001754 case Expr::CallExprClass: {
1755 const CallExpr *CE = cast<CallExpr>(E);
1756 Out << "cl";
John McCall5e1e89b2010-08-18 19:18:59 +00001757 mangleExpression(CE->getCallee(), CE->getNumArgs());
John McCall1dd73832010-02-04 01:42:13 +00001758 for (unsigned I = 0, N = CE->getNumArgs(); I != N; ++I)
1759 mangleExpression(CE->getArg(I));
Benjamin Kramer35f59b62010-04-10 16:03:31 +00001760 Out << 'E';
John McCall09cc1412010-02-03 00:55:45 +00001761 break;
John McCall1dd73832010-02-04 01:42:13 +00001762 }
John McCall09cc1412010-02-03 00:55:45 +00001763
John McCall0512e482010-07-14 04:20:34 +00001764 case Expr::CXXNewExprClass: {
1765 // Proposal from David Vandervoorde, 2010.06.30
1766 const CXXNewExpr *New = cast<CXXNewExpr>(E);
1767 if (New->isGlobalNew()) Out << "gs";
1768 Out << (New->isArray() ? "na" : "nw");
1769 for (CXXNewExpr::const_arg_iterator I = New->placement_arg_begin(),
1770 E = New->placement_arg_end(); I != E; ++I)
1771 mangleExpression(*I);
1772 Out << '_';
1773 mangleType(New->getAllocatedType());
1774 if (New->hasInitializer()) {
1775 Out << "pi";
1776 for (CXXNewExpr::const_arg_iterator I = New->constructor_arg_begin(),
1777 E = New->constructor_arg_end(); I != E; ++I)
1778 mangleExpression(*I);
1779 }
1780 Out << 'E';
1781 break;
1782 }
1783
John McCall2f27bf82010-02-04 02:56:29 +00001784 case Expr::MemberExprClass: {
1785 const MemberExpr *ME = cast<MemberExpr>(E);
1786 mangleMemberExpr(ME->getBase(), ME->isArrow(),
1787 ME->getQualifier(), ME->getMemberDecl()->getDeclName(),
John McCall5e1e89b2010-08-18 19:18:59 +00001788 Arity);
John McCall2f27bf82010-02-04 02:56:29 +00001789 break;
1790 }
1791
1792 case Expr::UnresolvedMemberExprClass: {
1793 const UnresolvedMemberExpr *ME = cast<UnresolvedMemberExpr>(E);
1794 mangleMemberExpr(ME->getBase(), ME->isArrow(),
1795 ME->getQualifier(), ME->getMemberName(),
John McCall5e1e89b2010-08-18 19:18:59 +00001796 Arity);
John McCall6dbce192010-08-20 00:17:19 +00001797 if (ME->hasExplicitTemplateArgs())
1798 mangleTemplateArgs(ME->getExplicitTemplateArgs());
John McCall2f27bf82010-02-04 02:56:29 +00001799 break;
1800 }
1801
1802 case Expr::CXXDependentScopeMemberExprClass: {
1803 const CXXDependentScopeMemberExpr *ME
1804 = cast<CXXDependentScopeMemberExpr>(E);
1805 mangleMemberExpr(ME->getBase(), ME->isArrow(),
1806 ME->getQualifier(), ME->getMember(),
John McCall5e1e89b2010-08-18 19:18:59 +00001807 Arity);
John McCall6dbce192010-08-20 00:17:19 +00001808 if (ME->hasExplicitTemplateArgs())
1809 mangleTemplateArgs(ME->getExplicitTemplateArgs());
John McCall2f27bf82010-02-04 02:56:29 +00001810 break;
1811 }
1812
John McCall1dd73832010-02-04 01:42:13 +00001813 case Expr::UnresolvedLookupExprClass: {
John McCalla3218e72010-02-04 01:48:38 +00001814 // The ABI doesn't cover how to mangle overload sets, so we mangle
1815 // using something as close as possible to the original lookup
1816 // expression.
John McCall1dd73832010-02-04 01:42:13 +00001817 const UnresolvedLookupExpr *ULE = cast<UnresolvedLookupExpr>(E);
John McCall5e1e89b2010-08-18 19:18:59 +00001818 mangleUnresolvedName(ULE->getQualifier(), ULE->getName(), Arity);
John McCall6dbce192010-08-20 00:17:19 +00001819 if (ULE->hasExplicitTemplateArgs())
1820 mangleTemplateArgs(ULE->getExplicitTemplateArgs());
John McCall1dd73832010-02-04 01:42:13 +00001821 break;
1822 }
1823
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001824 case Expr::CXXUnresolvedConstructExprClass: {
John McCall1dd73832010-02-04 01:42:13 +00001825 const CXXUnresolvedConstructExpr *CE = cast<CXXUnresolvedConstructExpr>(E);
1826 unsigned N = CE->arg_size();
1827
1828 Out << "cv";
1829 mangleType(CE->getType());
Benjamin Kramer35f59b62010-04-10 16:03:31 +00001830 if (N != 1) Out << '_';
John McCall1dd73832010-02-04 01:42:13 +00001831 for (unsigned I = 0; I != N; ++I) mangleExpression(CE->getArg(I));
Benjamin Kramer35f59b62010-04-10 16:03:31 +00001832 if (N != 1) Out << 'E';
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001833 break;
John McCall1dd73832010-02-04 01:42:13 +00001834 }
John McCall09cc1412010-02-03 00:55:45 +00001835
John McCall1dd73832010-02-04 01:42:13 +00001836 case Expr::CXXTemporaryObjectExprClass:
1837 case Expr::CXXConstructExprClass: {
1838 const CXXConstructExpr *CE = cast<CXXConstructExpr>(E);
1839 unsigned N = CE->getNumArgs();
1840
1841 Out << "cv";
1842 mangleType(CE->getType());
Benjamin Kramer35f59b62010-04-10 16:03:31 +00001843 if (N != 1) Out << '_';
John McCall1dd73832010-02-04 01:42:13 +00001844 for (unsigned I = 0; I != N; ++I) mangleExpression(CE->getArg(I));
Benjamin Kramer35f59b62010-04-10 16:03:31 +00001845 if (N != 1) Out << 'E';
John McCall09cc1412010-02-03 00:55:45 +00001846 break;
John McCall1dd73832010-02-04 01:42:13 +00001847 }
1848
1849 case Expr::SizeOfAlignOfExprClass: {
1850 const SizeOfAlignOfExpr *SAE = cast<SizeOfAlignOfExpr>(E);
Benjamin Kramer35f59b62010-04-10 16:03:31 +00001851 if (SAE->isSizeOf()) Out << 's';
1852 else Out << 'a';
John McCall1dd73832010-02-04 01:42:13 +00001853 if (SAE->isArgumentType()) {
Benjamin Kramer35f59b62010-04-10 16:03:31 +00001854 Out << 't';
John McCall1dd73832010-02-04 01:42:13 +00001855 mangleType(SAE->getArgumentType());
1856 } else {
Benjamin Kramer35f59b62010-04-10 16:03:31 +00001857 Out << 'z';
John McCall1dd73832010-02-04 01:42:13 +00001858 mangleExpression(SAE->getArgumentExpr());
1859 }
1860 break;
1861 }
Anders Carlssona7694082009-11-06 02:50:19 +00001862
John McCall0512e482010-07-14 04:20:34 +00001863 case Expr::CXXThrowExprClass: {
1864 const CXXThrowExpr *TE = cast<CXXThrowExpr>(E);
1865
1866 // Proposal from David Vandervoorde, 2010.06.30
1867 if (TE->getSubExpr()) {
1868 Out << "tw";
1869 mangleExpression(TE->getSubExpr());
1870 } else {
1871 Out << "tr";
1872 }
1873 break;
1874 }
1875
1876 case Expr::CXXTypeidExprClass: {
1877 const CXXTypeidExpr *TIE = cast<CXXTypeidExpr>(E);
1878
1879 // Proposal from David Vandervoorde, 2010.06.30
1880 if (TIE->isTypeOperand()) {
1881 Out << "ti";
1882 mangleType(TIE->getTypeOperand());
1883 } else {
1884 Out << "te";
1885 mangleExpression(TIE->getExprOperand());
1886 }
1887 break;
1888 }
1889
1890 case Expr::CXXDeleteExprClass: {
1891 const CXXDeleteExpr *DE = cast<CXXDeleteExpr>(E);
1892
1893 // Proposal from David Vandervoorde, 2010.06.30
1894 if (DE->isGlobalDelete()) Out << "gs";
1895 Out << (DE->isArrayForm() ? "da" : "dl");
1896 mangleExpression(DE->getArgument());
1897 break;
1898 }
1899
Anders Carlssone170ba72009-12-14 01:45:37 +00001900 case Expr::UnaryOperatorClass: {
1901 const UnaryOperator *UO = cast<UnaryOperator>(E);
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001902 mangleOperatorName(UnaryOperator::getOverloadedOperator(UO->getOpcode()),
Anders Carlssone170ba72009-12-14 01:45:37 +00001903 /*Arity=*/1);
1904 mangleExpression(UO->getSubExpr());
1905 break;
1906 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001907
John McCall0512e482010-07-14 04:20:34 +00001908 case Expr::ArraySubscriptExprClass: {
1909 const ArraySubscriptExpr *AE = cast<ArraySubscriptExpr>(E);
1910
1911 // Array subscript is treated as a syntactically wierd form of
1912 // binary operator.
1913 Out << "ix";
1914 mangleExpression(AE->getLHS());
1915 mangleExpression(AE->getRHS());
1916 break;
1917 }
1918
1919 case Expr::CompoundAssignOperatorClass: // fallthrough
Anders Carlssone170ba72009-12-14 01:45:37 +00001920 case Expr::BinaryOperatorClass: {
1921 const BinaryOperator *BO = cast<BinaryOperator>(E);
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001922 mangleOperatorName(BinaryOperator::getOverloadedOperator(BO->getOpcode()),
Anders Carlssone170ba72009-12-14 01:45:37 +00001923 /*Arity=*/2);
1924 mangleExpression(BO->getLHS());
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001925 mangleExpression(BO->getRHS());
Anders Carlssone170ba72009-12-14 01:45:37 +00001926 break;
John McCall2f27bf82010-02-04 02:56:29 +00001927 }
Anders Carlssone170ba72009-12-14 01:45:37 +00001928
1929 case Expr::ConditionalOperatorClass: {
1930 const ConditionalOperator *CO = cast<ConditionalOperator>(E);
1931 mangleOperatorName(OO_Conditional, /*Arity=*/3);
1932 mangleExpression(CO->getCond());
John McCall5e1e89b2010-08-18 19:18:59 +00001933 mangleExpression(CO->getLHS(), Arity);
1934 mangleExpression(CO->getRHS(), Arity);
Anders Carlssone170ba72009-12-14 01:45:37 +00001935 break;
1936 }
1937
Douglas Gregor46287c72010-01-29 16:37:09 +00001938 case Expr::ImplicitCastExprClass: {
John McCall5e1e89b2010-08-18 19:18:59 +00001939 mangleExpression(cast<ImplicitCastExpr>(E)->getSubExpr(), Arity);
Douglas Gregor46287c72010-01-29 16:37:09 +00001940 break;
1941 }
1942
1943 case Expr::CStyleCastExprClass:
1944 case Expr::CXXStaticCastExprClass:
1945 case Expr::CXXDynamicCastExprClass:
1946 case Expr::CXXReinterpretCastExprClass:
1947 case Expr::CXXConstCastExprClass:
1948 case Expr::CXXFunctionalCastExprClass: {
1949 const ExplicitCastExpr *ECE = cast<ExplicitCastExpr>(E);
1950 Out << "cv";
1951 mangleType(ECE->getType());
1952 mangleExpression(ECE->getSubExpr());
1953 break;
1954 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001955
Anders Carlsson58040a52009-12-16 05:48:46 +00001956 case Expr::CXXOperatorCallExprClass: {
1957 const CXXOperatorCallExpr *CE = cast<CXXOperatorCallExpr>(E);
1958 unsigned NumArgs = CE->getNumArgs();
1959 mangleOperatorName(CE->getOperator(), /*Arity=*/NumArgs);
1960 // Mangle the arguments.
1961 for (unsigned i = 0; i != NumArgs; ++i)
1962 mangleExpression(CE->getArg(i));
1963 break;
1964 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001965
Anders Carlssona7694082009-11-06 02:50:19 +00001966 case Expr::ParenExprClass:
John McCall5e1e89b2010-08-18 19:18:59 +00001967 mangleExpression(cast<ParenExpr>(E)->getSubExpr(), Arity);
Anders Carlssona7694082009-11-06 02:50:19 +00001968 break;
1969
Anders Carlssond553f8c2009-09-21 01:21:10 +00001970 case Expr::DeclRefExprClass: {
Douglas Gregor5ed1bc32010-02-28 21:40:32 +00001971 const NamedDecl *D = cast<DeclRefExpr>(E)->getDecl();
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00001972
Anders Carlssond553f8c2009-09-21 01:21:10 +00001973 switch (D->getKind()) {
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001974 default:
Douglas Gregor5ed1bc32010-02-28 21:40:32 +00001975 // <expr-primary> ::= L <mangled-name> E # external name
1976 Out << 'L';
1977 mangle(D, "_Z");
1978 Out << 'E';
1979 break;
1980
John McCall3dc7e7b2010-07-24 01:17:35 +00001981 case Decl::EnumConstant: {
1982 const EnumConstantDecl *ED = cast<EnumConstantDecl>(D);
1983 mangleIntegerLiteral(ED->getType(), ED->getInitVal());
1984 break;
1985 }
1986
Anders Carlssond553f8c2009-09-21 01:21:10 +00001987 case Decl::NonTypeTemplateParm: {
1988 const NonTypeTemplateParmDecl *PD = cast<NonTypeTemplateParmDecl>(D);
Anders Carlsson0ccdf8d2009-09-27 00:38:53 +00001989 mangleTemplateParameter(PD->getIndex());
Anders Carlssond553f8c2009-09-21 01:21:10 +00001990 break;
1991 }
1992
1993 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00001994
Anders Carlsson50755b02009-09-27 20:11:34 +00001995 break;
Anders Carlssond553f8c2009-09-21 01:21:10 +00001996 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00001997
Douglas Gregorc7793c72011-01-15 01:15:58 +00001998 case Expr::SubstNonTypeTemplateParmPackExprClass:
1999 mangleTemplateParameter(
2000 cast<SubstNonTypeTemplateParmPackExpr>(E)->getParameterPack()->getIndex());
2001 break;
2002
John McCall865d4472009-11-19 22:55:06 +00002003 case Expr::DependentScopeDeclRefExprClass: {
2004 const DependentScopeDeclRefExpr *DRE = cast<DependentScopeDeclRefExpr>(E);
Douglas Gregor4b2ccfc2010-02-28 22:05:49 +00002005 NestedNameSpecifier *NNS = DRE->getQualifier();
2006 const Type *QTy = NNS->getAsType();
2007
2008 // When we're dealing with a nested-name-specifier that has just a
2009 // dependent identifier in it, mangle that as a typename. FIXME:
2010 // It isn't clear that we ever actually want to have such a
2011 // nested-name-specifier; why not just represent it as a typename type?
2012 if (!QTy && NNS->getAsIdentifier() && NNS->getPrefix()) {
Douglas Gregor4a2023f2010-03-31 20:19:30 +00002013 QTy = getASTContext().getDependentNameType(ETK_Typename,
2014 NNS->getPrefix(),
2015 NNS->getAsIdentifier())
Douglas Gregor4b2ccfc2010-02-28 22:05:49 +00002016 .getTypePtr();
2017 }
Anders Carlsson50755b02009-09-27 20:11:34 +00002018 assert(QTy && "Qualifier was not type!");
2019
John McCall6dbce192010-08-20 00:17:19 +00002020 // ::= sr <type> <unqualified-name> # dependent name
2021 // ::= sr <type> <unqualified-name> <template-args> # dependent template-id
Anders Carlsson50755b02009-09-27 20:11:34 +00002022 Out << "sr";
2023 mangleType(QualType(QTy, 0));
John McCall5e1e89b2010-08-18 19:18:59 +00002024 mangleUnqualifiedName(0, DRE->getDeclName(), Arity);
John McCall6dbce192010-08-20 00:17:19 +00002025 if (DRE->hasExplicitTemplateArgs())
2026 mangleTemplateArgs(DRE->getExplicitTemplateArgs());
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002027
Anders Carlsson50755b02009-09-27 20:11:34 +00002028 break;
2029 }
2030
John McCalld9307602010-04-09 22:54:09 +00002031 case Expr::CXXBindTemporaryExprClass:
2032 mangleExpression(cast<CXXBindTemporaryExpr>(E)->getSubExpr());
2033 break;
2034
John McCall4765fa02010-12-06 08:20:24 +00002035 case Expr::ExprWithCleanupsClass:
2036 mangleExpression(cast<ExprWithCleanups>(E)->getSubExpr(), Arity);
John McCalld9307602010-04-09 22:54:09 +00002037 break;
2038
John McCall1dd73832010-02-04 01:42:13 +00002039 case Expr::FloatingLiteralClass: {
2040 const FloatingLiteral *FL = cast<FloatingLiteral>(E);
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002041 Out << 'L';
John McCall1dd73832010-02-04 01:42:13 +00002042 mangleType(FL->getType());
John McCall0512e482010-07-14 04:20:34 +00002043 mangleFloat(FL->getValue());
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002044 Out << 'E';
John McCall1dd73832010-02-04 01:42:13 +00002045 break;
2046 }
2047
John McCallde810632010-04-09 21:48:08 +00002048 case Expr::CharacterLiteralClass:
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002049 Out << 'L';
John McCallde810632010-04-09 21:48:08 +00002050 mangleType(E->getType());
2051 Out << cast<CharacterLiteral>(E)->getValue();
2052 Out << 'E';
2053 break;
2054
2055 case Expr::CXXBoolLiteralExprClass:
2056 Out << "Lb";
2057 Out << (cast<CXXBoolLiteralExpr>(E)->getValue() ? '1' : '0');
2058 Out << 'E';
2059 break;
2060
John McCall0512e482010-07-14 04:20:34 +00002061 case Expr::IntegerLiteralClass: {
2062 llvm::APSInt Value(cast<IntegerLiteral>(E)->getValue());
2063 if (E->getType()->isSignedIntegerType())
2064 Value.setIsSigned(true);
2065 mangleIntegerLiteral(E->getType(), Value);
Anders Carlssone170ba72009-12-14 01:45:37 +00002066 break;
John McCall0512e482010-07-14 04:20:34 +00002067 }
2068
2069 case Expr::ImaginaryLiteralClass: {
2070 const ImaginaryLiteral *IE = cast<ImaginaryLiteral>(E);
2071 // Mangle as if a complex literal.
Nick Lewycky271b6652010-09-05 03:40:33 +00002072 // Proposal from David Vandevoorde, 2010.06.30.
John McCall0512e482010-07-14 04:20:34 +00002073 Out << 'L';
2074 mangleType(E->getType());
2075 if (const FloatingLiteral *Imag =
2076 dyn_cast<FloatingLiteral>(IE->getSubExpr())) {
2077 // Mangle a floating-point zero of the appropriate type.
2078 mangleFloat(llvm::APFloat(Imag->getValue().getSemantics()));
2079 Out << '_';
2080 mangleFloat(Imag->getValue());
2081 } else {
Nick Lewycky271b6652010-09-05 03:40:33 +00002082 Out << "0_";
John McCall0512e482010-07-14 04:20:34 +00002083 llvm::APSInt Value(cast<IntegerLiteral>(IE->getSubExpr())->getValue());
2084 if (IE->getSubExpr()->getType()->isSignedIntegerType())
2085 Value.setIsSigned(true);
2086 mangleNumber(Value);
2087 }
2088 Out << 'E';
2089 break;
2090 }
2091
2092 case Expr::StringLiteralClass: {
John McCall1658c392010-07-15 21:53:03 +00002093 // Revised proposal from David Vandervoorde, 2010.07.15.
John McCall0512e482010-07-14 04:20:34 +00002094 Out << 'L';
John McCall1658c392010-07-15 21:53:03 +00002095 assert(isa<ConstantArrayType>(E->getType()));
2096 mangleType(E->getType());
John McCall0512e482010-07-14 04:20:34 +00002097 Out << 'E';
2098 break;
2099 }
2100
2101 case Expr::GNUNullExprClass:
2102 // FIXME: should this really be mangled the same as nullptr?
2103 // fallthrough
2104
2105 case Expr::CXXNullPtrLiteralExprClass: {
2106 // Proposal from David Vandervoorde, 2010.06.30, as
2107 // modified by ABI list discussion.
2108 Out << "LDnE";
2109 break;
2110 }
Douglas Gregorbe230c32011-01-03 17:17:50 +00002111
2112 case Expr::PackExpansionExprClass:
2113 Out << "sp";
2114 mangleExpression(cast<PackExpansionExpr>(E)->getPattern());
2115 break;
Douglas Gregor2e774c42011-01-04 18:56:13 +00002116
2117 case Expr::SizeOfPackExprClass: {
Douglas Gregor2e774c42011-01-04 18:56:13 +00002118 Out << "sZ";
2119 const NamedDecl *Pack = cast<SizeOfPackExpr>(E)->getPack();
2120 if (const TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(Pack))
2121 mangleTemplateParameter(TTP->getIndex());
2122 else if (const NonTypeTemplateParmDecl *NTTP
2123 = dyn_cast<NonTypeTemplateParmDecl>(Pack))
2124 mangleTemplateParameter(NTTP->getIndex());
2125 else if (const TemplateTemplateParmDecl *TempTP
2126 = dyn_cast<TemplateTemplateParmDecl>(Pack))
2127 mangleTemplateParameter(TempTP->getIndex());
2128 else {
Douglas Gregor4fc48662011-01-13 16:39:34 +00002129 // Note: proposed by Mike Herrick on 11/30/10
2130 // <expression> ::= sZ <function-param> # size of function parameter pack
Douglas Gregor2e774c42011-01-04 18:56:13 +00002131 Diagnostic &Diags = Context.getDiags();
2132 unsigned DiagID = Diags.getCustomDiagID(Diagnostic::Error,
2133 "cannot mangle sizeof...(function parameter pack)");
2134 Diags.Report(DiagID);
2135 return;
2136 }
2137 }
Anders Carlssond553f8c2009-09-21 01:21:10 +00002138 }
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00002139}
2140
Anders Carlsson3ac86b52009-04-15 05:36:58 +00002141void CXXNameMangler::mangleCXXCtorType(CXXCtorType T) {
2142 // <ctor-dtor-name> ::= C1 # complete object constructor
2143 // ::= C2 # base object constructor
2144 // ::= C3 # complete object allocating constructor
2145 //
2146 switch (T) {
2147 case Ctor_Complete:
2148 Out << "C1";
2149 break;
2150 case Ctor_Base:
2151 Out << "C2";
2152 break;
2153 case Ctor_CompleteAllocating:
2154 Out << "C3";
2155 break;
2156 }
2157}
2158
Anders Carlsson27ae5362009-04-17 01:58:57 +00002159void CXXNameMangler::mangleCXXDtorType(CXXDtorType T) {
2160 // <ctor-dtor-name> ::= D0 # deleting destructor
2161 // ::= D1 # complete object destructor
2162 // ::= D2 # base object destructor
2163 //
2164 switch (T) {
2165 case Dtor_Deleting:
2166 Out << "D0";
2167 break;
2168 case Dtor_Complete:
2169 Out << "D1";
2170 break;
2171 case Dtor_Base:
2172 Out << "D2";
2173 break;
2174 }
2175}
2176
John McCall6dbce192010-08-20 00:17:19 +00002177void CXXNameMangler::mangleTemplateArgs(
2178 const ExplicitTemplateArgumentList &TemplateArgs) {
2179 // <template-args> ::= I <template-arg>+ E
2180 Out << 'I';
2181 for (unsigned I = 0, E = TemplateArgs.NumTemplateArgs; I != E; ++I)
2182 mangleTemplateArg(0, TemplateArgs.getTemplateArgs()[I].getArgument());
2183 Out << 'E';
2184}
2185
Douglas Gregor20f0cc72010-04-23 03:10:43 +00002186void CXXNameMangler::mangleTemplateArgs(TemplateName Template,
2187 const TemplateArgument *TemplateArgs,
2188 unsigned NumTemplateArgs) {
2189 if (TemplateDecl *TD = Template.getAsTemplateDecl())
2190 return mangleTemplateArgs(*TD->getTemplateParameters(), TemplateArgs,
2191 NumTemplateArgs);
Sean Huntc3021132010-05-05 15:23:54 +00002192
Douglas Gregor20f0cc72010-04-23 03:10:43 +00002193 // <template-args> ::= I <template-arg>+ E
2194 Out << 'I';
2195 for (unsigned i = 0; i != NumTemplateArgs; ++i)
2196 mangleTemplateArg(0, TemplateArgs[i]);
2197 Out << 'E';
2198}
2199
Rafael Espindolad9800722010-03-11 14:07:00 +00002200void CXXNameMangler::mangleTemplateArgs(const TemplateParameterList &PL,
2201 const TemplateArgumentList &AL) {
Anders Carlsson7a0ba872009-05-15 16:09:15 +00002202 // <template-args> ::= I <template-arg>+ E
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002203 Out << 'I';
Rafael Espindolad9800722010-03-11 14:07:00 +00002204 for (unsigned i = 0, e = AL.size(); i != e; ++i)
2205 mangleTemplateArg(PL.getParam(i), AL[i]);
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002206 Out << 'E';
Anders Carlsson7a0ba872009-05-15 16:09:15 +00002207}
2208
Rafael Espindolad9800722010-03-11 14:07:00 +00002209void CXXNameMangler::mangleTemplateArgs(const TemplateParameterList &PL,
2210 const TemplateArgument *TemplateArgs,
Anders Carlsson7624f212009-09-18 02:42:01 +00002211 unsigned NumTemplateArgs) {
2212 // <template-args> ::= I <template-arg>+ E
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002213 Out << 'I';
Daniel Dunbar7e0c1952009-11-21 09:17:15 +00002214 for (unsigned i = 0; i != NumTemplateArgs; ++i)
Rafael Espindolad9800722010-03-11 14:07:00 +00002215 mangleTemplateArg(PL.getParam(i), TemplateArgs[i]);
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002216 Out << 'E';
Anders Carlsson7624f212009-09-18 02:42:01 +00002217}
2218
Rafael Espindolad9800722010-03-11 14:07:00 +00002219void CXXNameMangler::mangleTemplateArg(const NamedDecl *P,
2220 const TemplateArgument &A) {
Mike Stump1eb44332009-09-09 15:08:12 +00002221 // <template-arg> ::= <type> # type or template
Anders Carlsson7a0ba872009-05-15 16:09:15 +00002222 // ::= X <expression> E # expression
2223 // ::= <expr-primary> # simple expressions
Douglas Gregor4fc48662011-01-13 16:39:34 +00002224 // ::= J <template-arg>* E # argument pack
Anders Carlsson7a0ba872009-05-15 16:09:15 +00002225 // ::= sp <expression> # pack expansion of (C++0x)
2226 switch (A.getKind()) {
Douglas Gregorf90b27a2011-01-03 22:36:02 +00002227 case TemplateArgument::Null:
2228 llvm_unreachable("Cannot mangle NULL template argument");
2229
Anders Carlsson7a0ba872009-05-15 16:09:15 +00002230 case TemplateArgument::Type:
2231 mangleType(A.getAsType());
2232 break;
Anders Carlsson9e85c742009-12-23 19:30:55 +00002233 case TemplateArgument::Template:
John McCallb6f532e2010-07-14 06:43:17 +00002234 // This is mangled as <type>.
2235 mangleType(A.getAsTemplate());
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002236 break;
Douglas Gregora7fc9012011-01-05 18:58:31 +00002237 case TemplateArgument::TemplateExpansion:
Douglas Gregor4fc48662011-01-13 16:39:34 +00002238 // <type> ::= Dp <type> # pack expansion (C++0x)
Douglas Gregora7fc9012011-01-05 18:58:31 +00002239 Out << "Dp";
2240 mangleType(A.getAsTemplateOrTemplatePattern());
2241 break;
Anders Carlssond553f8c2009-09-21 01:21:10 +00002242 case TemplateArgument::Expression:
2243 Out << 'X';
2244 mangleExpression(A.getAsExpr());
2245 Out << 'E';
2246 break;
Anders Carlssone170ba72009-12-14 01:45:37 +00002247 case TemplateArgument::Integral:
2248 mangleIntegerLiteral(A.getIntegralType(), *A.getAsIntegral());
Anders Carlsson7a0ba872009-05-15 16:09:15 +00002249 break;
Daniel Dunbar7e0c1952009-11-21 09:17:15 +00002250 case TemplateArgument::Declaration: {
Douglas Gregor20f0cc72010-04-23 03:10:43 +00002251 assert(P && "Missing template parameter for declaration argument");
Daniel Dunbar7e0c1952009-11-21 09:17:15 +00002252 // <expr-primary> ::= L <mangled-name> E # external name
2253
Rafael Espindolad9800722010-03-11 14:07:00 +00002254 // Clang produces AST's where pointer-to-member-function expressions
Daniel Dunbar7e0c1952009-11-21 09:17:15 +00002255 // and pointer-to-function expressions are represented as a declaration not
Rafael Espindolad9800722010-03-11 14:07:00 +00002256 // an expression. We compensate for it here to produce the correct mangling.
2257 NamedDecl *D = cast<NamedDecl>(A.getAsDecl());
2258 const NonTypeTemplateParmDecl *Parameter = cast<NonTypeTemplateParmDecl>(P);
2259 bool compensateMangling = D->isCXXClassMember() &&
2260 !Parameter->getType()->isReferenceType();
2261 if (compensateMangling) {
2262 Out << 'X';
2263 mangleOperatorName(OO_Amp, 1);
2264 }
2265
Daniel Dunbar7e0c1952009-11-21 09:17:15 +00002266 Out << 'L';
2267 // References to external entities use the mangled name; if the name would
2268 // not normally be manged then mangle it as unqualified.
2269 //
2270 // FIXME: The ABI specifies that external names here should have _Z, but
2271 // gcc leaves this off.
Rafael Espindolad9800722010-03-11 14:07:00 +00002272 if (compensateMangling)
2273 mangle(D, "_Z");
2274 else
2275 mangle(D, "Z");
Daniel Dunbar7e0c1952009-11-21 09:17:15 +00002276 Out << 'E';
Rafael Espindolad9800722010-03-11 14:07:00 +00002277
2278 if (compensateMangling)
2279 Out << 'E';
2280
Daniel Dunbar7e0c1952009-11-21 09:17:15 +00002281 break;
2282 }
Douglas Gregorf90b27a2011-01-03 22:36:02 +00002283
2284 case TemplateArgument::Pack: {
2285 // Note: proposal by Mike Herrick on 12/20/10
2286 Out << 'J';
2287 for (TemplateArgument::pack_iterator PA = A.pack_begin(),
2288 PAEnd = A.pack_end();
2289 PA != PAEnd; ++PA)
2290 mangleTemplateArg(P, *PA);
2291 Out << 'E';
2292 }
Daniel Dunbar7e0c1952009-11-21 09:17:15 +00002293 }
Anders Carlsson7a0ba872009-05-15 16:09:15 +00002294}
2295
Anders Carlsson0ccdf8d2009-09-27 00:38:53 +00002296void CXXNameMangler::mangleTemplateParameter(unsigned Index) {
2297 // <template-param> ::= T_ # first template parameter
2298 // ::= T <parameter-2 non-negative number> _
2299 if (Index == 0)
2300 Out << "T_";
2301 else
2302 Out << 'T' << (Index - 1) << '_';
2303}
2304
Anders Carlsson76967372009-09-17 00:43:46 +00002305// <substitution> ::= S <seq-id> _
2306// ::= S_
Anders Carlsson6862fc72009-09-17 04:16:28 +00002307bool CXXNameMangler::mangleSubstitution(const NamedDecl *ND) {
Anders Carlssone7c8cb62009-09-26 20:53:44 +00002308 // Try one of the standard substitutions first.
2309 if (mangleStandardSubstitution(ND))
2310 return true;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002311
Anders Carlsson433d1372009-11-07 04:26:04 +00002312 ND = cast<NamedDecl>(ND->getCanonicalDecl());
Anders Carlsson6862fc72009-09-17 04:16:28 +00002313 return mangleSubstitution(reinterpret_cast<uintptr_t>(ND));
2314}
2315
Anders Carlsson76967372009-09-17 00:43:46 +00002316bool CXXNameMangler::mangleSubstitution(QualType T) {
Anders Carlssond99edc42009-09-26 03:55:37 +00002317 if (!T.getCVRQualifiers()) {
2318 if (const RecordType *RT = T->getAs<RecordType>())
2319 return mangleSubstitution(RT->getDecl());
2320 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002321
Anders Carlsson76967372009-09-17 00:43:46 +00002322 uintptr_t TypePtr = reinterpret_cast<uintptr_t>(T.getAsOpaquePtr());
2323
Anders Carlssond3a932a2009-09-17 03:53:28 +00002324 return mangleSubstitution(TypePtr);
2325}
2326
Douglas Gregor1e9268e2010-04-28 05:58:56 +00002327bool CXXNameMangler::mangleSubstitution(TemplateName Template) {
2328 if (TemplateDecl *TD = Template.getAsTemplateDecl())
2329 return mangleSubstitution(TD);
Sean Huntc3021132010-05-05 15:23:54 +00002330
Douglas Gregor1e9268e2010-04-28 05:58:56 +00002331 Template = Context.getASTContext().getCanonicalTemplateName(Template);
2332 return mangleSubstitution(
2333 reinterpret_cast<uintptr_t>(Template.getAsVoidPointer()));
2334}
2335
Anders Carlssond3a932a2009-09-17 03:53:28 +00002336bool CXXNameMangler::mangleSubstitution(uintptr_t Ptr) {
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002337 llvm::DenseMap<uintptr_t, unsigned>::iterator I = Substitutions.find(Ptr);
Anders Carlsson76967372009-09-17 00:43:46 +00002338 if (I == Substitutions.end())
2339 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002340
Anders Carlsson76967372009-09-17 00:43:46 +00002341 unsigned SeqID = I->second;
2342 if (SeqID == 0)
2343 Out << "S_";
2344 else {
2345 SeqID--;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002346
Anders Carlsson76967372009-09-17 00:43:46 +00002347 // <seq-id> is encoded in base-36, using digits and upper case letters.
2348 char Buffer[10];
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002349 char *BufferPtr = llvm::array_endof(Buffer);
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002350
Anders Carlsson76967372009-09-17 00:43:46 +00002351 if (SeqID == 0) *--BufferPtr = '0';
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002352
Anders Carlsson76967372009-09-17 00:43:46 +00002353 while (SeqID) {
2354 assert(BufferPtr > Buffer && "Buffer overflow!");
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002355
John McCall6ab30e02010-06-09 07:26:17 +00002356 char c = static_cast<char>(SeqID % 36);
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002357
Anders Carlsson76967372009-09-17 00:43:46 +00002358 *--BufferPtr = (c < 10 ? '0' + c : 'A' + c - 10);
2359 SeqID /= 36;
2360 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002361
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002362 Out << 'S'
2363 << llvm::StringRef(BufferPtr, llvm::array_endof(Buffer)-BufferPtr)
2364 << '_';
Anders Carlsson76967372009-09-17 00:43:46 +00002365 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002366
Anders Carlsson76967372009-09-17 00:43:46 +00002367 return true;
2368}
2369
Anders Carlssonf514b542009-09-27 00:12:57 +00002370static bool isCharType(QualType T) {
2371 if (T.isNull())
2372 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002373
Anders Carlssonf514b542009-09-27 00:12:57 +00002374 return T->isSpecificBuiltinType(BuiltinType::Char_S) ||
2375 T->isSpecificBuiltinType(BuiltinType::Char_U);
2376}
2377
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002378/// isCharSpecialization - Returns whether a given type is a template
Anders Carlssonf514b542009-09-27 00:12:57 +00002379/// specialization of a given name with a single argument of type char.
2380static bool isCharSpecialization(QualType T, const char *Name) {
2381 if (T.isNull())
2382 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002383
Anders Carlssonf514b542009-09-27 00:12:57 +00002384 const RecordType *RT = T->getAs<RecordType>();
2385 if (!RT)
2386 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002387
2388 const ClassTemplateSpecializationDecl *SD =
Anders Carlssonf514b542009-09-27 00:12:57 +00002389 dyn_cast<ClassTemplateSpecializationDecl>(RT->getDecl());
2390 if (!SD)
2391 return false;
2392
2393 if (!isStdNamespace(SD->getDeclContext()))
2394 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002395
Anders Carlssonf514b542009-09-27 00:12:57 +00002396 const TemplateArgumentList &TemplateArgs = SD->getTemplateArgs();
2397 if (TemplateArgs.size() != 1)
2398 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002399
Anders Carlssonf514b542009-09-27 00:12:57 +00002400 if (!isCharType(TemplateArgs[0].getAsType()))
2401 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002402
Daniel Dunbar01eb9b92009-10-18 21:17:35 +00002403 return SD->getIdentifier()->getName() == Name;
Anders Carlssonf514b542009-09-27 00:12:57 +00002404}
2405
Anders Carlsson91f88602009-12-07 19:56:42 +00002406template <std::size_t StrLen>
Benjamin Kramer54353f42010-11-25 18:29:30 +00002407static bool isStreamCharSpecialization(const ClassTemplateSpecializationDecl*SD,
2408 const char (&Str)[StrLen]) {
Anders Carlsson91f88602009-12-07 19:56:42 +00002409 if (!SD->getIdentifier()->isStr(Str))
2410 return false;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002411
Anders Carlsson91f88602009-12-07 19:56:42 +00002412 const TemplateArgumentList &TemplateArgs = SD->getTemplateArgs();
2413 if (TemplateArgs.size() != 2)
2414 return false;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002415
Anders Carlsson91f88602009-12-07 19:56:42 +00002416 if (!isCharType(TemplateArgs[0].getAsType()))
2417 return false;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002418
Anders Carlsson91f88602009-12-07 19:56:42 +00002419 if (!isCharSpecialization(TemplateArgs[1].getAsType(), "char_traits"))
2420 return false;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002421
Anders Carlsson91f88602009-12-07 19:56:42 +00002422 return true;
2423}
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002424
Anders Carlssone7c8cb62009-09-26 20:53:44 +00002425bool CXXNameMangler::mangleStandardSubstitution(const NamedDecl *ND) {
2426 // <substitution> ::= St # ::std::
Anders Carlsson8c031552009-09-26 23:10:05 +00002427 if (const NamespaceDecl *NS = dyn_cast<NamespaceDecl>(ND)) {
Anders Carlsson47846d22009-12-04 06:23:23 +00002428 if (isStd(NS)) {
Anders Carlsson8c031552009-09-26 23:10:05 +00002429 Out << "St";
2430 return true;
2431 }
2432 }
2433
2434 if (const ClassTemplateDecl *TD = dyn_cast<ClassTemplateDecl>(ND)) {
2435 if (!isStdNamespace(TD->getDeclContext()))
2436 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002437
Anders Carlsson8c031552009-09-26 23:10:05 +00002438 // <substitution> ::= Sa # ::std::allocator
2439 if (TD->getIdentifier()->isStr("allocator")) {
2440 Out << "Sa";
2441 return true;
2442 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002443
Anders Carlsson189d59c2009-09-26 23:14:39 +00002444 // <<substitution> ::= Sb # ::std::basic_string
2445 if (TD->getIdentifier()->isStr("basic_string")) {
2446 Out << "Sb";
2447 return true;
2448 }
Anders Carlsson8c031552009-09-26 23:10:05 +00002449 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002450
2451 if (const ClassTemplateSpecializationDecl *SD =
Anders Carlssonf514b542009-09-27 00:12:57 +00002452 dyn_cast<ClassTemplateSpecializationDecl>(ND)) {
Eli Friedman5370ee22010-02-23 18:25:09 +00002453 if (!isStdNamespace(SD->getDeclContext()))
2454 return false;
2455
Anders Carlssonf514b542009-09-27 00:12:57 +00002456 // <substitution> ::= Ss # ::std::basic_string<char,
2457 // ::std::char_traits<char>,
2458 // ::std::allocator<char> >
2459 if (SD->getIdentifier()->isStr("basic_string")) {
2460 const TemplateArgumentList &TemplateArgs = SD->getTemplateArgs();
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002461
Anders Carlssonf514b542009-09-27 00:12:57 +00002462 if (TemplateArgs.size() != 3)
2463 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002464
Anders Carlssonf514b542009-09-27 00:12:57 +00002465 if (!isCharType(TemplateArgs[0].getAsType()))
2466 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002467
Anders Carlssonf514b542009-09-27 00:12:57 +00002468 if (!isCharSpecialization(TemplateArgs[1].getAsType(), "char_traits"))
2469 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002470
Anders Carlssonf514b542009-09-27 00:12:57 +00002471 if (!isCharSpecialization(TemplateArgs[2].getAsType(), "allocator"))
2472 return false;
2473
2474 Out << "Ss";
2475 return true;
2476 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002477
Anders Carlsson91f88602009-12-07 19:56:42 +00002478 // <substitution> ::= Si # ::std::basic_istream<char,
2479 // ::std::char_traits<char> >
2480 if (isStreamCharSpecialization(SD, "basic_istream")) {
2481 Out << "Si";
2482 return true;
2483 }
2484
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002485 // <substitution> ::= So # ::std::basic_ostream<char,
Anders Carlsson8f8fd8e2009-10-08 17:20:26 +00002486 // ::std::char_traits<char> >
Anders Carlsson91f88602009-12-07 19:56:42 +00002487 if (isStreamCharSpecialization(SD, "basic_ostream")) {
Anders Carlsson8f8fd8e2009-10-08 17:20:26 +00002488 Out << "So";
2489 return true;
2490 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002491
Anders Carlsson91f88602009-12-07 19:56:42 +00002492 // <substitution> ::= Sd # ::std::basic_iostream<char,
2493 // ::std::char_traits<char> >
2494 if (isStreamCharSpecialization(SD, "basic_iostream")) {
2495 Out << "Sd";
2496 return true;
2497 }
Anders Carlssonf514b542009-09-27 00:12:57 +00002498 }
Anders Carlsson8c031552009-09-26 23:10:05 +00002499 return false;
Anders Carlssone7c8cb62009-09-26 20:53:44 +00002500}
2501
Anders Carlsson76967372009-09-17 00:43:46 +00002502void CXXNameMangler::addSubstitution(QualType T) {
Anders Carlssond99edc42009-09-26 03:55:37 +00002503 if (!T.getCVRQualifiers()) {
2504 if (const RecordType *RT = T->getAs<RecordType>()) {
2505 addSubstitution(RT->getDecl());
2506 return;
2507 }
2508 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002509
Anders Carlsson76967372009-09-17 00:43:46 +00002510 uintptr_t TypePtr = reinterpret_cast<uintptr_t>(T.getAsOpaquePtr());
Anders Carlssond3a932a2009-09-17 03:53:28 +00002511 addSubstitution(TypePtr);
2512}
2513
Douglas Gregor1e9268e2010-04-28 05:58:56 +00002514void CXXNameMangler::addSubstitution(TemplateName Template) {
2515 if (TemplateDecl *TD = Template.getAsTemplateDecl())
2516 return addSubstitution(TD);
Sean Huntc3021132010-05-05 15:23:54 +00002517
Douglas Gregor1e9268e2010-04-28 05:58:56 +00002518 Template = Context.getASTContext().getCanonicalTemplateName(Template);
2519 addSubstitution(reinterpret_cast<uintptr_t>(Template.getAsVoidPointer()));
2520}
2521
Anders Carlssond3a932a2009-09-17 03:53:28 +00002522void CXXNameMangler::addSubstitution(uintptr_t Ptr) {
Anders Carlssond3a932a2009-09-17 03:53:28 +00002523 assert(!Substitutions.count(Ptr) && "Substitution already exists!");
Anders Carlsson9d85b722010-06-02 04:29:50 +00002524 Substitutions[Ptr] = SeqID++;
Anders Carlsson76967372009-09-17 00:43:46 +00002525}
2526
Daniel Dunbar1b077112009-11-21 09:06:10 +00002527//
Mike Stump1eb44332009-09-09 15:08:12 +00002528
Daniel Dunbar1b077112009-11-21 09:06:10 +00002529/// \brief Mangles the name of the declaration D and emits that name to the
2530/// given output stream.
2531///
2532/// If the declaration D requires a mangled name, this routine will emit that
2533/// mangled name to \p os and return true. Otherwise, \p os will be unchanged
2534/// and this routine will return false. In this case, the caller should just
2535/// emit the identifier of the declaration (\c D->getIdentifier()) as its
2536/// name.
Peter Collingbourne14110472011-01-13 18:57:25 +00002537void ItaniumMangleContext::mangleName(const NamedDecl *D,
2538 llvm::SmallVectorImpl<char> &Res) {
Daniel Dunbarc02ab4c2009-11-21 09:14:44 +00002539 assert((isa<FunctionDecl>(D) || isa<VarDecl>(D)) &&
2540 "Invalid mangleName() call, argument is not a variable or function!");
2541 assert(!isa<CXXConstructorDecl>(D) && !isa<CXXDestructorDecl>(D) &&
2542 "Invalid mangleName() call on 'structor decl!");
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002543
Daniel Dunbar1b077112009-11-21 09:06:10 +00002544 PrettyStackTraceDecl CrashInfo(D, SourceLocation(),
2545 getASTContext().getSourceManager(),
2546 "Mangling declaration");
Mike Stump1eb44332009-09-09 15:08:12 +00002547
Daniel Dunbar94fd26d2009-11-21 09:06:22 +00002548 CXXNameMangler Mangler(*this, Res);
2549 return Mangler.mangle(D);
Daniel Dunbar1b077112009-11-21 09:06:10 +00002550}
Mike Stump1eb44332009-09-09 15:08:12 +00002551
Peter Collingbourne14110472011-01-13 18:57:25 +00002552void ItaniumMangleContext::mangleCXXCtor(const CXXConstructorDecl *D,
2553 CXXCtorType Type,
2554 llvm::SmallVectorImpl<char> &Res) {
Daniel Dunbar77939c92009-11-21 09:06:31 +00002555 CXXNameMangler Mangler(*this, Res, D, Type);
2556 Mangler.mangle(D);
Daniel Dunbar1b077112009-11-21 09:06:10 +00002557}
Mike Stump1eb44332009-09-09 15:08:12 +00002558
Peter Collingbourne14110472011-01-13 18:57:25 +00002559void ItaniumMangleContext::mangleCXXDtor(const CXXDestructorDecl *D,
2560 CXXDtorType Type,
2561 llvm::SmallVectorImpl<char> &Res) {
Daniel Dunbar77939c92009-11-21 09:06:31 +00002562 CXXNameMangler Mangler(*this, Res, D, Type);
2563 Mangler.mangle(D);
Daniel Dunbar1b077112009-11-21 09:06:10 +00002564}
Mike Stumpf1216772009-07-31 18:25:34 +00002565
Peter Collingbourne14110472011-01-13 18:57:25 +00002566void ItaniumMangleContext::mangleThunk(const CXXMethodDecl *MD,
2567 const ThunkInfo &Thunk,
2568 llvm::SmallVectorImpl<char> &Res) {
Anders Carlsson19879c92010-03-23 17:17:29 +00002569 // <special-name> ::= T <call-offset> <base encoding>
2570 // # base is the nominal target function of thunk
2571 // <special-name> ::= Tc <call-offset> <call-offset> <base encoding>
2572 // # base is the nominal target function of thunk
2573 // # first call-offset is 'this' adjustment
2574 // # second call-offset is result adjustment
Sean Huntc3021132010-05-05 15:23:54 +00002575
Anders Carlsson19879c92010-03-23 17:17:29 +00002576 assert(!isa<CXXDestructorDecl>(MD) &&
2577 "Use mangleCXXDtor for destructor decls!");
Sean Huntc3021132010-05-05 15:23:54 +00002578
Anders Carlsson19879c92010-03-23 17:17:29 +00002579 CXXNameMangler Mangler(*this, Res);
2580 Mangler.getStream() << "_ZT";
2581 if (!Thunk.Return.isEmpty())
2582 Mangler.getStream() << 'c';
Sean Huntc3021132010-05-05 15:23:54 +00002583
Anders Carlsson19879c92010-03-23 17:17:29 +00002584 // Mangle the 'this' pointer adjustment.
2585 Mangler.mangleCallOffset(Thunk.This.NonVirtual, Thunk.This.VCallOffsetOffset);
Sean Huntc3021132010-05-05 15:23:54 +00002586
Anders Carlsson19879c92010-03-23 17:17:29 +00002587 // Mangle the return pointer adjustment if there is one.
2588 if (!Thunk.Return.isEmpty())
2589 Mangler.mangleCallOffset(Thunk.Return.NonVirtual,
2590 Thunk.Return.VBaseOffsetOffset);
Sean Huntc3021132010-05-05 15:23:54 +00002591
Anders Carlsson19879c92010-03-23 17:17:29 +00002592 Mangler.mangleFunctionEncoding(MD);
2593}
2594
Sean Huntc3021132010-05-05 15:23:54 +00002595void
Peter Collingbourne14110472011-01-13 18:57:25 +00002596ItaniumMangleContext::mangleCXXDtorThunk(const CXXDestructorDecl *DD,
2597 CXXDtorType Type,
2598 const ThisAdjustment &ThisAdjustment,
2599 llvm::SmallVectorImpl<char> &Res) {
Anders Carlsson19879c92010-03-23 17:17:29 +00002600 // <special-name> ::= T <call-offset> <base encoding>
2601 // # base is the nominal target function of thunk
Sean Huntc3021132010-05-05 15:23:54 +00002602
Anders Carlsson19879c92010-03-23 17:17:29 +00002603 CXXNameMangler Mangler(*this, Res, DD, Type);
2604 Mangler.getStream() << "_ZT";
2605
2606 // Mangle the 'this' pointer adjustment.
Sean Huntc3021132010-05-05 15:23:54 +00002607 Mangler.mangleCallOffset(ThisAdjustment.NonVirtual,
Anders Carlsson19879c92010-03-23 17:17:29 +00002608 ThisAdjustment.VCallOffsetOffset);
2609
2610 Mangler.mangleFunctionEncoding(DD);
2611}
2612
Daniel Dunbarc0747712009-11-21 09:12:13 +00002613/// mangleGuardVariable - Returns the mangled name for a guard variable
2614/// for the passed in VarDecl.
Peter Collingbourne14110472011-01-13 18:57:25 +00002615void ItaniumMangleContext::mangleItaniumGuardVariable(const VarDecl *D,
2616 llvm::SmallVectorImpl<char> &Res) {
Daniel Dunbarc0747712009-11-21 09:12:13 +00002617 // <special-name> ::= GV <object name> # Guard variable for one-time
2618 // # initialization
2619 CXXNameMangler Mangler(*this, Res);
2620 Mangler.getStream() << "_ZGV";
2621 Mangler.mangleName(D);
2622}
2623
Peter Collingbourne14110472011-01-13 18:57:25 +00002624void ItaniumMangleContext::mangleReferenceTemporary(const VarDecl *D,
Anders Carlsson715edf22010-06-26 16:09:40 +00002625 llvm::SmallVectorImpl<char> &Res) {
2626 // We match the GCC mangling here.
2627 // <special-name> ::= GR <object name>
2628 CXXNameMangler Mangler(*this, Res);
2629 Mangler.getStream() << "_ZGR";
2630 Mangler.mangleName(D);
2631}
2632
Peter Collingbourne14110472011-01-13 18:57:25 +00002633void ItaniumMangleContext::mangleCXXVTable(const CXXRecordDecl *RD,
2634 llvm::SmallVectorImpl<char> &Res) {
Daniel Dunbarc0747712009-11-21 09:12:13 +00002635 // <special-name> ::= TV <type> # virtual table
Daniel Dunbar94fd26d2009-11-21 09:06:22 +00002636 CXXNameMangler Mangler(*this, Res);
Daniel Dunbarc0747712009-11-21 09:12:13 +00002637 Mangler.getStream() << "_ZTV";
Douglas Gregor1b12a3b2010-05-26 05:11:13 +00002638 Mangler.mangleNameOrStandardSubstitution(RD);
Daniel Dunbar1b077112009-11-21 09:06:10 +00002639}
Mike Stump82d75b02009-11-10 01:58:37 +00002640
Peter Collingbourne14110472011-01-13 18:57:25 +00002641void ItaniumMangleContext::mangleCXXVTT(const CXXRecordDecl *RD,
2642 llvm::SmallVectorImpl<char> &Res) {
Daniel Dunbarc0747712009-11-21 09:12:13 +00002643 // <special-name> ::= TT <type> # VTT structure
Daniel Dunbar94fd26d2009-11-21 09:06:22 +00002644 CXXNameMangler Mangler(*this, Res);
Daniel Dunbarc0747712009-11-21 09:12:13 +00002645 Mangler.getStream() << "_ZTT";
Douglas Gregor1b12a3b2010-05-26 05:11:13 +00002646 Mangler.mangleNameOrStandardSubstitution(RD);
Daniel Dunbar1b077112009-11-21 09:06:10 +00002647}
Mike Stumpab3f7e92009-11-10 01:41:59 +00002648
Peter Collingbourne14110472011-01-13 18:57:25 +00002649void ItaniumMangleContext::mangleCXXCtorVTable(const CXXRecordDecl *RD,
2650 int64_t Offset,
2651 const CXXRecordDecl *Type,
2652 llvm::SmallVectorImpl<char> &Res) {
Daniel Dunbarc0747712009-11-21 09:12:13 +00002653 // <special-name> ::= TC <type> <offset number> _ <base type>
Daniel Dunbar94fd26d2009-11-21 09:06:22 +00002654 CXXNameMangler Mangler(*this, Res);
Daniel Dunbarc0747712009-11-21 09:12:13 +00002655 Mangler.getStream() << "_ZTC";
Douglas Gregor1b12a3b2010-05-26 05:11:13 +00002656 Mangler.mangleNameOrStandardSubstitution(RD);
Daniel Dunbarc0747712009-11-21 09:12:13 +00002657 Mangler.getStream() << Offset;
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002658 Mangler.getStream() << '_';
Douglas Gregor1b12a3b2010-05-26 05:11:13 +00002659 Mangler.mangleNameOrStandardSubstitution(Type);
Daniel Dunbar1b077112009-11-21 09:06:10 +00002660}
Mike Stump738f8c22009-07-31 23:15:31 +00002661
Peter Collingbourne14110472011-01-13 18:57:25 +00002662void ItaniumMangleContext::mangleCXXRTTI(QualType Ty,
2663 llvm::SmallVectorImpl<char> &Res) {
Daniel Dunbarc0747712009-11-21 09:12:13 +00002664 // <special-name> ::= TI <type> # typeinfo structure
Douglas Gregor154fe982009-12-23 22:04:40 +00002665 assert(!Ty.hasQualifiers() && "RTTI info cannot have top-level qualifiers");
Daniel Dunbar94fd26d2009-11-21 09:06:22 +00002666 CXXNameMangler Mangler(*this, Res);
Daniel Dunbarc0747712009-11-21 09:12:13 +00002667 Mangler.getStream() << "_ZTI";
2668 Mangler.mangleType(Ty);
Daniel Dunbar1b077112009-11-21 09:06:10 +00002669}
Mike Stump67795982009-11-14 00:14:13 +00002670
Peter Collingbourne14110472011-01-13 18:57:25 +00002671void ItaniumMangleContext::mangleCXXRTTIName(QualType Ty,
2672 llvm::SmallVectorImpl<char> &Res) {
Daniel Dunbarc0747712009-11-21 09:12:13 +00002673 // <special-name> ::= TS <type> # typeinfo name (null terminated byte string)
Daniel Dunbar94fd26d2009-11-21 09:06:22 +00002674 CXXNameMangler Mangler(*this, Res);
Daniel Dunbarc0747712009-11-21 09:12:13 +00002675 Mangler.getStream() << "_ZTS";
2676 Mangler.mangleType(Ty);
Mike Stumpf1216772009-07-31 18:25:34 +00002677}
Peter Collingbourne14110472011-01-13 18:57:25 +00002678
2679MangleContext *clang::createItaniumMangleContext(ASTContext &Context,
2680 Diagnostic &Diags) {
2681 return new ItaniumMangleContext(Context, Diags);
2682}