blob: 7d641e45c1f269417b286359b50d0bf72b8abd4a [file] [log] [blame]
Peter Collingbourne14110472011-01-13 18:57:25 +00001//===--- ItaniumMangle.cpp - Itanium C++ Name Mangling ----------*- C++ -*-===//
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// Implements C++ name mangling according to the Itanium C++ ABI,
11// which is used in GCC 3.2 and newer (and many compilers that are
12// ABI-compatible with GCC):
13//
14// http://www.codesourcery.com/public/cxx-abi/abi.html
15//
16//===----------------------------------------------------------------------===//
Peter Collingbourne14110472011-01-13 18:57:25 +000017#include "clang/AST/Mangle.h"
Douglas Gregor5f2bfd42009-02-13 00:10:09 +000018#include "clang/AST/ASTContext.h"
19#include "clang/AST/Decl.h"
20#include "clang/AST/DeclCXX.h"
Anders Carlssona40c5e42009-03-07 22:03:21 +000021#include "clang/AST/DeclObjC.h"
Anders Carlsson7a0ba872009-05-15 16:09:15 +000022#include "clang/AST/DeclTemplate.h"
Anders Carlsson50755b02009-09-27 20:11:34 +000023#include "clang/AST/ExprCXX.h"
Peter Collingbourne14110472011-01-13 18:57:25 +000024#include "clang/Basic/ABI.h"
Douglas Gregor6ec36682009-02-18 23:53:56 +000025#include "clang/Basic/SourceManager.h"
Anders Carlssonc4355b62009-10-07 01:45:02 +000026#include "llvm/ADT/StringExtras.h"
Douglas Gregor5f2bfd42009-02-13 00:10:09 +000027#include "llvm/Support/raw_ostream.h"
John McCallefe6aee2009-09-05 07:56:18 +000028#include "llvm/Support/ErrorHandling.h"
Anders Carlssonf98574b2010-02-05 07:31:37 +000029
30#define MANGLE_CHECKER 0
31
32#if MANGLE_CHECKER
33#include <cxxabi.h>
34#endif
35
Douglas Gregor5f2bfd42009-02-13 00:10:09 +000036using namespace clang;
Charles Davis685b1d92010-05-26 18:25:27 +000037
Douglas Gregor5f2bfd42009-02-13 00:10:09 +000038namespace {
Fariborz Jahanian57058532010-03-03 19:41:08 +000039
John McCall82b7d7b2010-10-18 21:28:44 +000040static const CXXRecordDecl *GetLocalClassDecl(const NamedDecl *ND) {
41 const DeclContext *DC = dyn_cast<DeclContext>(ND);
42 if (!DC)
43 DC = ND->getDeclContext();
44 while (!DC->isNamespace() && !DC->isTranslationUnit()) {
45 if (isa<FunctionDecl>(DC->getParent()))
46 return dyn_cast<CXXRecordDecl>(DC);
47 DC = DC->getParent();
Fariborz Jahanian57058532010-03-03 19:41:08 +000048 }
49 return 0;
50}
51
Anders Carlsson7e120032009-11-24 05:36:32 +000052static const CXXMethodDecl *getStructor(const CXXMethodDecl *MD) {
53 assert((isa<CXXConstructorDecl>(MD) || isa<CXXDestructorDecl>(MD)) &&
54 "Passed in decl is not a ctor or dtor!");
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +000055
Anders Carlsson7e120032009-11-24 05:36:32 +000056 if (const TemplateDecl *TD = MD->getPrimaryTemplate()) {
57 MD = cast<CXXMethodDecl>(TD->getTemplatedDecl());
58
59 assert((isa<CXXConstructorDecl>(MD) || isa<CXXDestructorDecl>(MD)) &&
60 "Templated decl is not a ctor or dtor!");
61 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +000062
Anders Carlsson7e120032009-11-24 05:36:32 +000063 return MD;
64}
John McCall1dd73832010-02-04 01:42:13 +000065
66static const unsigned UnknownArity = ~0U;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +000067
Peter Collingbourne14110472011-01-13 18:57:25 +000068class ItaniumMangleContext : public MangleContext {
69 llvm::DenseMap<const TagDecl *, uint64_t> AnonStructIds;
70 unsigned Discriminator;
71 llvm::DenseMap<const NamedDecl*, unsigned> Uniquifier;
72
73public:
74 explicit ItaniumMangleContext(ASTContext &Context,
75 Diagnostic &Diags)
76 : MangleContext(Context, Diags) { }
77
78 uint64_t getAnonymousStructId(const TagDecl *TD) {
79 std::pair<llvm::DenseMap<const TagDecl *,
80 uint64_t>::iterator, bool> Result =
81 AnonStructIds.insert(std::make_pair(TD, AnonStructIds.size()));
82 return Result.first->second;
83 }
84
85 void startNewFunction() {
86 MangleContext::startNewFunction();
87 mangleInitDiscriminator();
88 }
89
90 /// @name Mangler Entry Points
91 /// @{
92
93 bool shouldMangleDeclName(const NamedDecl *D);
Rafael Espindola0e376a02011-02-11 01:41:00 +000094 void mangleName(const NamedDecl *D, llvm::raw_ostream &);
Peter Collingbourne14110472011-01-13 18:57:25 +000095 void mangleThunk(const CXXMethodDecl *MD,
96 const ThunkInfo &Thunk,
Rafael Espindolaf0be9792011-02-11 02:52:17 +000097 llvm::raw_ostream &);
Peter Collingbourne14110472011-01-13 18:57:25 +000098 void mangleCXXDtorThunk(const CXXDestructorDecl *DD, CXXDtorType Type,
99 const ThisAdjustment &ThisAdjustment,
Rafael Espindolaf0be9792011-02-11 02:52:17 +0000100 llvm::raw_ostream &);
Peter Collingbourne14110472011-01-13 18:57:25 +0000101 void mangleReferenceTemporary(const VarDecl *D,
Rafael Espindolaf0be9792011-02-11 02:52:17 +0000102 llvm::raw_ostream &);
Peter Collingbourne14110472011-01-13 18:57:25 +0000103 void mangleCXXVTable(const CXXRecordDecl *RD,
Rafael Espindolaf0be9792011-02-11 02:52:17 +0000104 llvm::raw_ostream &);
Peter Collingbourne14110472011-01-13 18:57:25 +0000105 void mangleCXXVTT(const CXXRecordDecl *RD,
Rafael Espindolaf0be9792011-02-11 02:52:17 +0000106 llvm::raw_ostream &);
Peter Collingbourne14110472011-01-13 18:57:25 +0000107 void mangleCXXCtorVTable(const CXXRecordDecl *RD, int64_t Offset,
108 const CXXRecordDecl *Type,
Rafael Espindolaf0be9792011-02-11 02:52:17 +0000109 llvm::raw_ostream &);
110 void mangleCXXRTTI(QualType T, llvm::raw_ostream &);
111 void mangleCXXRTTIName(QualType T, llvm::raw_ostream &);
Peter Collingbourne14110472011-01-13 18:57:25 +0000112 void mangleCXXCtor(const CXXConstructorDecl *D, CXXCtorType Type,
Rafael Espindola0e376a02011-02-11 01:41:00 +0000113 llvm::raw_ostream &);
Peter Collingbourne14110472011-01-13 18:57:25 +0000114 void mangleCXXDtor(const CXXDestructorDecl *D, CXXDtorType Type,
Rafael Espindola0e376a02011-02-11 01:41:00 +0000115 llvm::raw_ostream &);
Peter Collingbourne14110472011-01-13 18:57:25 +0000116
Rafael Espindolaf0be9792011-02-11 02:52:17 +0000117 void mangleItaniumGuardVariable(const VarDecl *D, llvm::raw_ostream &);
Peter Collingbourne14110472011-01-13 18:57:25 +0000118
119 void mangleInitDiscriminator() {
120 Discriminator = 0;
121 }
122
123 bool getNextDiscriminator(const NamedDecl *ND, unsigned &disc) {
124 unsigned &discriminator = Uniquifier[ND];
125 if (!discriminator)
126 discriminator = ++Discriminator;
127 if (discriminator == 1)
128 return false;
129 disc = discriminator-2;
130 return true;
131 }
132 /// @}
133};
134
Daniel Dunbar1b077112009-11-21 09:06:10 +0000135/// CXXNameMangler - Manage the mangling of a single name.
Daniel Dunbarc0747712009-11-21 09:12:13 +0000136class CXXNameMangler {
Peter Collingbourne14110472011-01-13 18:57:25 +0000137 ItaniumMangleContext &Context;
Rafael Espindola0e376a02011-02-11 01:41:00 +0000138 llvm::raw_ostream &Out;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000139
Daniel Dunbar1b077112009-11-21 09:06:10 +0000140 const CXXMethodDecl *Structor;
141 unsigned StructorType;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000142
Anders Carlsson9d85b722010-06-02 04:29:50 +0000143 /// SeqID - The next subsitution sequence number.
144 unsigned SeqID;
145
Daniel Dunbar1b077112009-11-21 09:06:10 +0000146 llvm::DenseMap<uintptr_t, unsigned> Substitutions;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000147
John McCall1dd73832010-02-04 01:42:13 +0000148 ASTContext &getASTContext() const { return Context.getASTContext(); }
149
Daniel Dunbarc0747712009-11-21 09:12:13 +0000150public:
Rafael Espindola0e376a02011-02-11 01:41:00 +0000151 CXXNameMangler(ItaniumMangleContext &C, llvm::raw_ostream &Out_)
Rafael Espindolac4850c22011-02-10 23:59:36 +0000152 : Context(C), Out(Out_), Structor(0), StructorType(0), SeqID(0) { }
Rafael Espindola0e376a02011-02-11 01:41:00 +0000153 CXXNameMangler(ItaniumMangleContext &C, llvm::raw_ostream &Out_,
Daniel Dunbar77939c92009-11-21 09:06:31 +0000154 const CXXConstructorDecl *D, CXXCtorType Type)
Rafael Espindolac4850c22011-02-10 23:59:36 +0000155 : Context(C), Out(Out_), Structor(getStructor(D)), StructorType(Type),
Anders Carlsson9d85b722010-06-02 04:29:50 +0000156 SeqID(0) { }
Rafael Espindola0e376a02011-02-11 01:41:00 +0000157 CXXNameMangler(ItaniumMangleContext &C, llvm::raw_ostream &Out_,
Daniel Dunbar77939c92009-11-21 09:06:31 +0000158 const CXXDestructorDecl *D, CXXDtorType Type)
Rafael Espindolac4850c22011-02-10 23:59:36 +0000159 : Context(C), Out(Out_), Structor(getStructor(D)), StructorType(Type),
Anders Carlsson9d85b722010-06-02 04:29:50 +0000160 SeqID(0) { }
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000161
Anders Carlssonf98574b2010-02-05 07:31:37 +0000162#if MANGLE_CHECKER
163 ~CXXNameMangler() {
164 if (Out.str()[0] == '\01')
165 return;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000166
Anders Carlssonf98574b2010-02-05 07:31:37 +0000167 int status = 0;
168 char *result = abi::__cxa_demangle(Out.str().str().c_str(), 0, 0, &status);
169 assert(status == 0 && "Could not demangle mangled name!");
170 free(result);
171 }
172#endif
Rafael Espindola0e376a02011-02-11 01:41:00 +0000173 llvm::raw_ostream &getStream() { return Out; }
Daniel Dunbarc0747712009-11-21 09:12:13 +0000174
Daniel Dunbar7e0c1952009-11-21 09:17:15 +0000175 void mangle(const NamedDecl *D, llvm::StringRef Prefix = "_Z");
Anders Carlsson19879c92010-03-23 17:17:29 +0000176 void mangleCallOffset(int64_t NonVirtual, int64_t Virtual);
John McCall0512e482010-07-14 04:20:34 +0000177 void mangleNumber(const llvm::APSInt &I);
Anders Carlssona94822e2009-11-26 02:32:05 +0000178 void mangleNumber(int64_t Number);
John McCall0512e482010-07-14 04:20:34 +0000179 void mangleFloat(const llvm::APFloat &F);
Daniel Dunbarc0747712009-11-21 09:12:13 +0000180 void mangleFunctionEncoding(const FunctionDecl *FD);
181 void mangleName(const NamedDecl *ND);
182 void mangleType(QualType T);
Douglas Gregor1b12a3b2010-05-26 05:11:13 +0000183 void mangleNameOrStandardSubstitution(const NamedDecl *ND);
184
Daniel Dunbarc0747712009-11-21 09:12:13 +0000185private:
Daniel Dunbar1b077112009-11-21 09:06:10 +0000186 bool mangleSubstitution(const NamedDecl *ND);
187 bool mangleSubstitution(QualType T);
Douglas Gregor1e9268e2010-04-28 05:58:56 +0000188 bool mangleSubstitution(TemplateName Template);
Daniel Dunbar1b077112009-11-21 09:06:10 +0000189 bool mangleSubstitution(uintptr_t Ptr);
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000190
Daniel Dunbar1b077112009-11-21 09:06:10 +0000191 bool mangleStandardSubstitution(const NamedDecl *ND);
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000192
Daniel Dunbar1b077112009-11-21 09:06:10 +0000193 void addSubstitution(const NamedDecl *ND) {
194 ND = cast<NamedDecl>(ND->getCanonicalDecl());
Anders Carlsson433d1372009-11-07 04:26:04 +0000195
Daniel Dunbar1b077112009-11-21 09:06:10 +0000196 addSubstitution(reinterpret_cast<uintptr_t>(ND));
197 }
198 void addSubstitution(QualType T);
Douglas Gregor1e9268e2010-04-28 05:58:56 +0000199 void addSubstitution(TemplateName Template);
Daniel Dunbar1b077112009-11-21 09:06:10 +0000200 void addSubstitution(uintptr_t Ptr);
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000201
John McCall1dd73832010-02-04 01:42:13 +0000202 void mangleUnresolvedScope(NestedNameSpecifier *Qualifier);
203 void mangleUnresolvedName(NestedNameSpecifier *Qualifier,
204 DeclarationName Name,
205 unsigned KnownArity = UnknownArity);
206
Daniel Dunbar1b077112009-11-21 09:06:10 +0000207 void mangleName(const TemplateDecl *TD,
208 const TemplateArgument *TemplateArgs,
209 unsigned NumTemplateArgs);
John McCall1dd73832010-02-04 01:42:13 +0000210 void mangleUnqualifiedName(const NamedDecl *ND) {
211 mangleUnqualifiedName(ND, ND->getDeclName(), UnknownArity);
212 }
213 void mangleUnqualifiedName(const NamedDecl *ND, DeclarationName Name,
214 unsigned KnownArity);
Daniel Dunbar1b077112009-11-21 09:06:10 +0000215 void mangleUnscopedName(const NamedDecl *ND);
216 void mangleUnscopedTemplateName(const TemplateDecl *ND);
Douglas Gregor1e9268e2010-04-28 05:58:56 +0000217 void mangleUnscopedTemplateName(TemplateName);
Daniel Dunbar1b077112009-11-21 09:06:10 +0000218 void mangleSourceName(const IdentifierInfo *II);
219 void mangleLocalName(const NamedDecl *ND);
Fariborz Jahanian57058532010-03-03 19:41:08 +0000220 void mangleNestedName(const NamedDecl *ND, const DeclContext *DC,
221 bool NoFunction=false);
Daniel Dunbar1b077112009-11-21 09:06:10 +0000222 void mangleNestedName(const TemplateDecl *TD,
223 const TemplateArgument *TemplateArgs,
224 unsigned NumTemplateArgs);
Fariborz Jahanian57058532010-03-03 19:41:08 +0000225 void manglePrefix(const DeclContext *DC, bool NoFunction=false);
Daniel Dunbar1b077112009-11-21 09:06:10 +0000226 void mangleTemplatePrefix(const TemplateDecl *ND);
Douglas Gregor20f0cc72010-04-23 03:10:43 +0000227 void mangleTemplatePrefix(TemplateName Template);
Daniel Dunbar1b077112009-11-21 09:06:10 +0000228 void mangleOperatorName(OverloadedOperatorKind OO, unsigned Arity);
229 void mangleQualifiers(Qualifiers Quals);
Douglas Gregor0a9a6d62011-01-26 17:36:28 +0000230 void mangleRefQualifier(RefQualifierKind RefQualifier);
John McCallefe6aee2009-09-05 07:56:18 +0000231
Anders Carlsson7b06f6c2009-12-10 03:14:39 +0000232 void mangleObjCMethodName(const ObjCMethodDecl *MD);
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000233
Daniel Dunbar1b077112009-11-21 09:06:10 +0000234 // Declare manglers for every type class.
John McCallefe6aee2009-09-05 07:56:18 +0000235#define ABSTRACT_TYPE(CLASS, PARENT)
236#define NON_CANONICAL_TYPE(CLASS, PARENT)
237#define TYPE(CLASS, PARENT) void mangleType(const CLASS##Type *T);
238#include "clang/AST/TypeNodes.def"
239
Daniel Dunbar1b077112009-11-21 09:06:10 +0000240 void mangleType(const TagType*);
John McCallb6f532e2010-07-14 06:43:17 +0000241 void mangleType(TemplateName);
Daniel Dunbar1b077112009-11-21 09:06:10 +0000242 void mangleBareFunctionType(const FunctionType *T,
243 bool MangleReturnType);
Bob Wilson57147a82010-11-16 00:32:18 +0000244 void mangleNeonVectorType(const VectorType *T);
Anders Carlssone170ba72009-12-14 01:45:37 +0000245
246 void mangleIntegerLiteral(QualType T, const llvm::APSInt &Value);
John McCall2f27bf82010-02-04 02:56:29 +0000247 void mangleMemberExpr(const Expr *Base, bool IsArrow,
248 NestedNameSpecifier *Qualifier,
249 DeclarationName Name,
250 unsigned KnownArity);
John McCall5e1e89b2010-08-18 19:18:59 +0000251 void mangleExpression(const Expr *E, unsigned Arity = UnknownArity);
Daniel Dunbar1b077112009-11-21 09:06:10 +0000252 void mangleCXXCtorType(CXXCtorType T);
253 void mangleCXXDtorType(CXXDtorType T);
Mike Stump1eb44332009-09-09 15:08:12 +0000254
John McCall6dbce192010-08-20 00:17:19 +0000255 void mangleTemplateArgs(const ExplicitTemplateArgumentList &TemplateArgs);
Douglas Gregor20f0cc72010-04-23 03:10:43 +0000256 void mangleTemplateArgs(TemplateName Template,
257 const TemplateArgument *TemplateArgs,
Sean Huntc3021132010-05-05 15:23:54 +0000258 unsigned NumTemplateArgs);
Rafael Espindolad9800722010-03-11 14:07:00 +0000259 void mangleTemplateArgs(const TemplateParameterList &PL,
260 const TemplateArgument *TemplateArgs,
Daniel Dunbar1b077112009-11-21 09:06:10 +0000261 unsigned NumTemplateArgs);
Rafael Espindolad9800722010-03-11 14:07:00 +0000262 void mangleTemplateArgs(const TemplateParameterList &PL,
263 const TemplateArgumentList &AL);
264 void mangleTemplateArg(const NamedDecl *P, const TemplateArgument &A);
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000265
Daniel Dunbar1b077112009-11-21 09:06:10 +0000266 void mangleTemplateParameter(unsigned Index);
267};
Peter Collingbourne14110472011-01-13 18:57:25 +0000268
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000269}
270
Anders Carlsson43f17402009-04-02 15:51:53 +0000271static bool isInCLinkageSpecification(const Decl *D) {
Douglas Gregor457e2812009-10-28 16:31:34 +0000272 D = D->getCanonicalDecl();
Mike Stump1eb44332009-09-09 15:08:12 +0000273 for (const DeclContext *DC = D->getDeclContext();
Anders Carlsson43f17402009-04-02 15:51:53 +0000274 !DC->isTranslationUnit(); DC = DC->getParent()) {
Mike Stump1eb44332009-09-09 15:08:12 +0000275 if (const LinkageSpecDecl *Linkage = dyn_cast<LinkageSpecDecl>(DC))
Anders Carlsson43f17402009-04-02 15:51:53 +0000276 return Linkage->getLanguage() == LinkageSpecDecl::lang_c;
277 }
Mike Stump1eb44332009-09-09 15:08:12 +0000278
Anders Carlsson43f17402009-04-02 15:51:53 +0000279 return false;
280}
281
Peter Collingbourne14110472011-01-13 18:57:25 +0000282bool ItaniumMangleContext::shouldMangleDeclName(const NamedDecl *D) {
Daniel Dunbarf981bf82009-11-21 09:14:52 +0000283 // In C, functions with no attributes never need to be mangled. Fastpath them.
284 if (!getASTContext().getLangOptions().CPlusPlus && !D->hasAttrs())
285 return false;
286
287 // Any decl can be declared with __asm("foo") on it, and this takes precedence
288 // over all other naming in the .o file.
289 if (D->hasAttr<AsmLabelAttr>())
290 return true;
291
Mike Stump141c5af2009-09-02 00:25:38 +0000292 // Clang's "overloadable" attribute extension to C/C++ implies name mangling
Anders Carlssona1e16222009-11-07 07:15:03 +0000293 // (always) as does passing a C++ member function and a function
294 // whose name is not a simple identifier.
Daniel Dunbarf981bf82009-11-21 09:14:52 +0000295 const FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
296 if (FD && (FD->hasAttr<OverloadableAttr>() || isa<CXXMethodDecl>(FD) ||
297 !FD->getDeclName().isIdentifier()))
298 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000299
Daniel Dunbarf981bf82009-11-21 09:14:52 +0000300 // Otherwise, no mangling is done outside C++ mode.
301 if (!getASTContext().getLangOptions().CPlusPlus)
302 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000303
Sean Hunt31455252010-01-24 03:04:27 +0000304 // Variables at global scope with non-internal linkage are not mangled
Eli Friedman7facf842009-12-02 20:32:49 +0000305 if (!FD) {
306 const DeclContext *DC = D->getDeclContext();
307 // Check for extern variable declared locally.
Fariborz Jahaniane81c5612010-06-30 18:57:21 +0000308 if (DC->isFunctionOrMethod() && D->hasLinkage())
Eli Friedman7facf842009-12-02 20:32:49 +0000309 while (!DC->isNamespace() && !DC->isTranslationUnit())
310 DC = DC->getParent();
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000311 if (DC->isTranslationUnit() && D->getLinkage() != InternalLinkage)
Eli Friedman7facf842009-12-02 20:32:49 +0000312 return false;
313 }
314
Eli Friedmanc00cb642010-07-18 20:49:59 +0000315 // Class members are always mangled.
316 if (D->getDeclContext()->isRecord())
317 return true;
318
Eli Friedman7facf842009-12-02 20:32:49 +0000319 // C functions and "main" are not mangled.
320 if ((FD && FD->isMain()) || isInCLinkageSpecification(D))
Daniel Dunbarf981bf82009-11-21 09:14:52 +0000321 return false;
322
Anders Carlsson43f17402009-04-02 15:51:53 +0000323 return true;
324}
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000325
Daniel Dunbar7e0c1952009-11-21 09:17:15 +0000326void CXXNameMangler::mangle(const NamedDecl *D, llvm::StringRef Prefix) {
Mike Stump141c5af2009-09-02 00:25:38 +0000327 // Any decl can be declared with __asm("foo") on it, and this takes precedence
328 // over all other naming in the .o file.
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000329 if (const AsmLabelAttr *ALA = D->getAttr<AsmLabelAttr>()) {
Chris Lattnerca3f25c2009-03-21 08:24:40 +0000330 // If we have an asm name, then we use it as the mangling.
331 Out << '\01'; // LLVM IR Marker for __asm("foo")
332 Out << ALA->getLabel();
Daniel Dunbarf981bf82009-11-21 09:14:52 +0000333 return;
Chris Lattnerca3f25c2009-03-21 08:24:40 +0000334 }
Mike Stump1eb44332009-09-09 15:08:12 +0000335
Sean Hunt31455252010-01-24 03:04:27 +0000336 // <mangled-name> ::= _Z <encoding>
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000337 // ::= <data name>
338 // ::= <special-name>
Daniel Dunbar7e0c1952009-11-21 09:17:15 +0000339 Out << Prefix;
340 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
Daniel Dunbarf981bf82009-11-21 09:14:52 +0000341 mangleFunctionEncoding(FD);
Rafael Espindolad9800722010-03-11 14:07:00 +0000342 else if (const VarDecl *VD = dyn_cast<VarDecl>(D))
343 mangleName(VD);
Daniel Dunbar7e0c1952009-11-21 09:17:15 +0000344 else
Rafael Espindolad9800722010-03-11 14:07:00 +0000345 mangleName(cast<FieldDecl>(D));
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000346}
347
348void CXXNameMangler::mangleFunctionEncoding(const FunctionDecl *FD) {
349 // <encoding> ::= <function name> <bare-function-type>
350 mangleName(FD);
Mike Stump1eb44332009-09-09 15:08:12 +0000351
Daniel Dunbar7e0c1952009-11-21 09:17:15 +0000352 // Don't mangle in the type if this isn't a decl we should typically mangle.
353 if (!Context.shouldMangleDeclName(FD))
354 return;
355
Mike Stump141c5af2009-09-02 00:25:38 +0000356 // Whether the mangling of a function type includes the return type depends on
357 // the context and the nature of the function. The rules for deciding whether
358 // the return type is included are:
Mike Stump1eb44332009-09-09 15:08:12 +0000359 //
Douglas Gregor1fd2dd12009-06-29 22:39:32 +0000360 // 1. Template functions (names or types) have return types encoded, with
361 // the exceptions listed below.
Mike Stump1eb44332009-09-09 15:08:12 +0000362 // 2. Function types not appearing as part of a function name mangling,
Douglas Gregor1fd2dd12009-06-29 22:39:32 +0000363 // e.g. parameters, pointer types, etc., have return type encoded, with the
364 // exceptions listed below.
365 // 3. Non-template function names do not have return types encoded.
366 //
Mike Stump141c5af2009-09-02 00:25:38 +0000367 // The exceptions mentioned in (1) and (2) above, for which the return type is
368 // never included, are
Douglas Gregor1fd2dd12009-06-29 22:39:32 +0000369 // 1. Constructors.
370 // 2. Destructors.
371 // 3. Conversion operator functions, e.g. operator int.
372 bool MangleReturnType = false;
Anders Carlsson9234b7f2009-09-17 03:46:43 +0000373 if (FunctionTemplateDecl *PrimaryTemplate = FD->getPrimaryTemplate()) {
374 if (!(isa<CXXConstructorDecl>(FD) || isa<CXXDestructorDecl>(FD) ||
375 isa<CXXConversionDecl>(FD)))
376 MangleReturnType = true;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000377
Anders Carlsson9234b7f2009-09-17 03:46:43 +0000378 // Mangle the type of the primary template.
379 FD = PrimaryTemplate->getTemplatedDecl();
380 }
381
John McCall54e14c42009-10-22 22:37:11 +0000382 // Do the canonicalization out here because parameter types can
383 // undergo additional canonicalization (e.g. array decay).
John McCallf4c73712011-01-19 06:33:43 +0000384 const FunctionType *FT
385 = cast<FunctionType>(Context.getASTContext()
John McCall54e14c42009-10-22 22:37:11 +0000386 .getCanonicalType(FD->getType()));
387
388 mangleBareFunctionType(FT, MangleReturnType);
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000389}
390
Anders Carlsson47846d22009-12-04 06:23:23 +0000391static const DeclContext *IgnoreLinkageSpecDecls(const DeclContext *DC) {
392 while (isa<LinkageSpecDecl>(DC)) {
Anders Carlsson47846d22009-12-04 06:23:23 +0000393 DC = DC->getParent();
394 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000395
Anders Carlsson47846d22009-12-04 06:23:23 +0000396 return DC;
397}
398
Anders Carlssonc820f902010-06-02 15:58:27 +0000399/// isStd - Return whether a given namespace is the 'std' namespace.
400static bool isStd(const NamespaceDecl *NS) {
401 if (!IgnoreLinkageSpecDecls(NS->getParent())->isTranslationUnit())
402 return false;
403
404 const IdentifierInfo *II = NS->getOriginalNamespace()->getIdentifier();
405 return II && II->isStr("std");
406}
407
Anders Carlsson47846d22009-12-04 06:23:23 +0000408// isStdNamespace - Return whether a given decl context is a toplevel 'std'
409// namespace.
Daniel Dunbar1308af92009-11-21 09:11:45 +0000410static bool isStdNamespace(const DeclContext *DC) {
Anders Carlsson47846d22009-12-04 06:23:23 +0000411 if (!DC->isNamespace())
412 return false;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000413
Anders Carlsson47846d22009-12-04 06:23:23 +0000414 return isStd(cast<NamespaceDecl>(DC));
Daniel Dunbar1308af92009-11-21 09:11:45 +0000415}
416
Anders Carlssonbb36ba42009-09-26 03:24:57 +0000417static const TemplateDecl *
418isTemplate(const NamedDecl *ND, const TemplateArgumentList *&TemplateArgs) {
Anders Carlsson2744a062009-09-18 19:00:18 +0000419 // Check if we have a function template.
420 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)){
Anders Carlssonbb36ba42009-09-26 03:24:57 +0000421 if (const TemplateDecl *TD = FD->getPrimaryTemplate()) {
Anders Carlsson2744a062009-09-18 19:00:18 +0000422 TemplateArgs = FD->getTemplateSpecializationArgs();
Anders Carlssonbb36ba42009-09-26 03:24:57 +0000423 return TD;
Anders Carlsson2744a062009-09-18 19:00:18 +0000424 }
425 }
426
Anders Carlssoneafc6dc2009-09-18 19:44:50 +0000427 // Check if we have a class template.
428 if (const ClassTemplateSpecializationDecl *Spec =
429 dyn_cast<ClassTemplateSpecializationDecl>(ND)) {
430 TemplateArgs = &Spec->getTemplateArgs();
Anders Carlssonbb36ba42009-09-26 03:24:57 +0000431 return Spec->getSpecializedTemplate();
Anders Carlssoneafc6dc2009-09-18 19:44:50 +0000432 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000433
Anders Carlsson2744a062009-09-18 19:00:18 +0000434 return 0;
435}
436
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000437void CXXNameMangler::mangleName(const NamedDecl *ND) {
438 // <name> ::= <nested-name>
439 // ::= <unscoped-name>
440 // ::= <unscoped-template-name> <template-args>
Anders Carlsson201ce742009-09-17 03:17:01 +0000441 // ::= <local-name>
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000442 //
Anders Carlssond58d6f72009-09-17 16:12:20 +0000443 const DeclContext *DC = ND->getDeclContext();
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000444
Eli Friedman7facf842009-12-02 20:32:49 +0000445 // If this is an extern variable declared locally, the relevant DeclContext
446 // is that of the containing namespace, or the translation unit.
447 if (isa<FunctionDecl>(DC) && ND->hasLinkage())
448 while (!DC->isNamespace() && !DC->isTranslationUnit())
449 DC = DC->getParent();
John McCall82b7d7b2010-10-18 21:28:44 +0000450 else if (GetLocalClassDecl(ND)) {
451 mangleLocalName(ND);
452 return;
453 }
Eli Friedman7facf842009-12-02 20:32:49 +0000454
Anders Carlsson5cc58c62009-09-22 17:23:30 +0000455 while (isa<LinkageSpecDecl>(DC))
Anders Carlssond58d6f72009-09-17 16:12:20 +0000456 DC = DC->getParent();
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000457
Anders Carlssond58d6f72009-09-17 16:12:20 +0000458 if (DC->isTranslationUnit() || isStdNamespace(DC)) {
Anders Carlsson2744a062009-09-18 19:00:18 +0000459 // Check if we have a template.
460 const TemplateArgumentList *TemplateArgs = 0;
Anders Carlsson0fa6df42009-09-26 19:45:45 +0000461 if (const TemplateDecl *TD = isTemplate(ND, TemplateArgs)) {
Anders Carlsson2744a062009-09-18 19:00:18 +0000462 mangleUnscopedTemplateName(TD);
Rafael Espindolad9800722010-03-11 14:07:00 +0000463 TemplateParameterList *TemplateParameters = TD->getTemplateParameters();
464 mangleTemplateArgs(*TemplateParameters, *TemplateArgs);
Anders Carlsson2744a062009-09-18 19:00:18 +0000465 return;
Anders Carlsson7482e242009-09-18 04:29:09 +0000466 }
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000467
Anders Carlsson7482e242009-09-18 04:29:09 +0000468 mangleUnscopedName(ND);
469 return;
470 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000471
Anders Carlsson7b06f6c2009-12-10 03:14:39 +0000472 if (isa<FunctionDecl>(DC) || isa<ObjCMethodDecl>(DC)) {
Anders Carlsson7482e242009-09-18 04:29:09 +0000473 mangleLocalName(ND);
474 return;
475 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000476
Eli Friedman7facf842009-12-02 20:32:49 +0000477 mangleNestedName(ND, DC);
Anders Carlsson7482e242009-09-18 04:29:09 +0000478}
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000479void CXXNameMangler::mangleName(const TemplateDecl *TD,
Anders Carlsson7624f212009-09-18 02:42:01 +0000480 const TemplateArgument *TemplateArgs,
481 unsigned NumTemplateArgs) {
Anders Carlsson47846d22009-12-04 06:23:23 +0000482 const DeclContext *DC = IgnoreLinkageSpecDecls(TD->getDeclContext());
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000483
Anders Carlsson7624f212009-09-18 02:42:01 +0000484 if (DC->isTranslationUnit() || isStdNamespace(DC)) {
Anders Carlsson0fa6df42009-09-26 19:45:45 +0000485 mangleUnscopedTemplateName(TD);
Rafael Espindolad9800722010-03-11 14:07:00 +0000486 TemplateParameterList *TemplateParameters = TD->getTemplateParameters();
487 mangleTemplateArgs(*TemplateParameters, TemplateArgs, NumTemplateArgs);
Anders Carlsson7624f212009-09-18 02:42:01 +0000488 } else {
489 mangleNestedName(TD, TemplateArgs, NumTemplateArgs);
490 }
491}
492
Anders Carlsson201ce742009-09-17 03:17:01 +0000493void CXXNameMangler::mangleUnscopedName(const NamedDecl *ND) {
494 // <unscoped-name> ::= <unqualified-name>
495 // ::= St <unqualified-name> # ::std::
496 if (isStdNamespace(ND->getDeclContext()))
497 Out << "St";
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000498
Anders Carlsson201ce742009-09-17 03:17:01 +0000499 mangleUnqualifiedName(ND);
500}
501
Anders Carlsson0fa6df42009-09-26 19:45:45 +0000502void CXXNameMangler::mangleUnscopedTemplateName(const TemplateDecl *ND) {
Anders Carlsson201ce742009-09-17 03:17:01 +0000503 // <unscoped-template-name> ::= <unscoped-name>
504 // ::= <substitution>
Anders Carlsson7624f212009-09-18 02:42:01 +0000505 if (mangleSubstitution(ND))
Anders Carlsson03c9d532009-09-17 04:02:31 +0000506 return;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000507
Douglas Gregor32fb4e12010-02-05 20:45:00 +0000508 // <template-template-param> ::= <template-param>
509 if (const TemplateTemplateParmDecl *TTP
510 = dyn_cast<TemplateTemplateParmDecl>(ND)) {
511 mangleTemplateParameter(TTP->getIndex());
512 return;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000513 }
Douglas Gregor32fb4e12010-02-05 20:45:00 +0000514
Anders Carlsson1668f202009-09-26 20:13:56 +0000515 mangleUnscopedName(ND->getTemplatedDecl());
Anders Carlsson7624f212009-09-18 02:42:01 +0000516 addSubstitution(ND);
Anders Carlsson201ce742009-09-17 03:17:01 +0000517}
518
Douglas Gregor1e9268e2010-04-28 05:58:56 +0000519void CXXNameMangler::mangleUnscopedTemplateName(TemplateName Template) {
520 // <unscoped-template-name> ::= <unscoped-name>
521 // ::= <substitution>
522 if (TemplateDecl *TD = Template.getAsTemplateDecl())
523 return mangleUnscopedTemplateName(TD);
Sean Huntc3021132010-05-05 15:23:54 +0000524
Douglas Gregor1e9268e2010-04-28 05:58:56 +0000525 if (mangleSubstitution(Template))
526 return;
527
528 // FIXME: How to cope with operators here?
529 DependentTemplateName *Dependent = Template.getAsDependentTemplateName();
530 assert(Dependent && "Not a dependent template name?");
531 if (!Dependent->isIdentifier()) {
532 // FIXME: We can't possibly know the arity of the operator here!
533 Diagnostic &Diags = Context.getDiags();
534 unsigned DiagID = Diags.getCustomDiagID(Diagnostic::Error,
535 "cannot mangle dependent operator name");
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000536 Diags.Report(DiagID);
Douglas Gregor1e9268e2010-04-28 05:58:56 +0000537 return;
538 }
Sean Huntc3021132010-05-05 15:23:54 +0000539
Douglas Gregor1e9268e2010-04-28 05:58:56 +0000540 mangleSourceName(Dependent->getIdentifier());
541 addSubstitution(Template);
542}
543
John McCall0512e482010-07-14 04:20:34 +0000544void CXXNameMangler::mangleFloat(const llvm::APFloat &F) {
545 // TODO: avoid this copy with careful stream management.
546 llvm::SmallString<20> Buffer;
547 F.bitcastToAPInt().toString(Buffer, 16, false);
548 Out.write(Buffer.data(), Buffer.size());
549}
550
551void CXXNameMangler::mangleNumber(const llvm::APSInt &Value) {
552 if (Value.isSigned() && Value.isNegative()) {
553 Out << 'n';
554 Value.abs().print(Out, true);
555 } else
556 Value.print(Out, Value.isSigned());
557}
558
Anders Carlssona94822e2009-11-26 02:32:05 +0000559void CXXNameMangler::mangleNumber(int64_t Number) {
560 // <number> ::= [n] <non-negative decimal integer>
561 if (Number < 0) {
562 Out << 'n';
563 Number = -Number;
564 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000565
Anders Carlssona94822e2009-11-26 02:32:05 +0000566 Out << Number;
567}
568
Anders Carlsson19879c92010-03-23 17:17:29 +0000569void CXXNameMangler::mangleCallOffset(int64_t NonVirtual, int64_t Virtual) {
Mike Stump141c5af2009-09-02 00:25:38 +0000570 // <call-offset> ::= h <nv-offset> _
571 // ::= v <v-offset> _
572 // <nv-offset> ::= <offset number> # non-virtual base override
Anders Carlssona94822e2009-11-26 02:32:05 +0000573 // <v-offset> ::= <offset number> _ <virtual offset number>
Mike Stump141c5af2009-09-02 00:25:38 +0000574 // # virtual base override, with vcall offset
Anders Carlsson19879c92010-03-23 17:17:29 +0000575 if (!Virtual) {
Anders Carlssona94822e2009-11-26 02:32:05 +0000576 Out << 'h';
Anders Carlsson19879c92010-03-23 17:17:29 +0000577 mangleNumber(NonVirtual);
Anders Carlssona94822e2009-11-26 02:32:05 +0000578 Out << '_';
579 return;
Mike Stump141c5af2009-09-02 00:25:38 +0000580 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000581
Anders Carlssona94822e2009-11-26 02:32:05 +0000582 Out << 'v';
Anders Carlsson19879c92010-03-23 17:17:29 +0000583 mangleNumber(NonVirtual);
Anders Carlssona94822e2009-11-26 02:32:05 +0000584 Out << '_';
Anders Carlsson19879c92010-03-23 17:17:29 +0000585 mangleNumber(Virtual);
Anders Carlssona94822e2009-11-26 02:32:05 +0000586 Out << '_';
Mike Stump9124bcc2009-09-02 00:56:18 +0000587}
588
John McCall1dd73832010-02-04 01:42:13 +0000589void CXXNameMangler::mangleUnresolvedScope(NestedNameSpecifier *Qualifier) {
590 Qualifier = getASTContext().getCanonicalNestedNameSpecifier(Qualifier);
591 switch (Qualifier->getKind()) {
592 case NestedNameSpecifier::Global:
593 // nothing
594 break;
595 case NestedNameSpecifier::Namespace:
596 mangleName(Qualifier->getAsNamespace());
597 break;
598 case NestedNameSpecifier::TypeSpec:
Rafael Espindola9b35b252010-03-17 04:28:11 +0000599 case NestedNameSpecifier::TypeSpecWithTemplate: {
600 const Type *QTy = Qualifier->getAsType();
601
602 if (const TemplateSpecializationType *TST =
603 dyn_cast<TemplateSpecializationType>(QTy)) {
604 if (!mangleSubstitution(QualType(TST, 0))) {
Douglas Gregor20f0cc72010-04-23 03:10:43 +0000605 mangleTemplatePrefix(TST->getTemplateName());
Sean Huntc3021132010-05-05 15:23:54 +0000606
Douglas Gregor20f0cc72010-04-23 03:10:43 +0000607 // FIXME: GCC does not appear to mangle the template arguments when
608 // the template in question is a dependent template name. Should we
609 // emulate that badness?
610 mangleTemplateArgs(TST->getTemplateName(), TST->getArgs(),
Rafael Espindola9b35b252010-03-17 04:28:11 +0000611 TST->getNumArgs());
612 addSubstitution(QualType(TST, 0));
613 }
614 } else {
615 // We use the QualType mangle type variant here because it handles
616 // substitutions.
617 mangleType(QualType(QTy, 0));
618 }
619 }
John McCall1dd73832010-02-04 01:42:13 +0000620 break;
621 case NestedNameSpecifier::Identifier:
John McCallad5e7382010-03-01 23:49:17 +0000622 // Member expressions can have these without prefixes.
623 if (Qualifier->getPrefix())
624 mangleUnresolvedScope(Qualifier->getPrefix());
John McCall1dd73832010-02-04 01:42:13 +0000625 mangleSourceName(Qualifier->getAsIdentifier());
626 break;
627 }
628}
629
630/// Mangles a name which was not resolved to a specific entity.
631void CXXNameMangler::mangleUnresolvedName(NestedNameSpecifier *Qualifier,
632 DeclarationName Name,
633 unsigned KnownArity) {
634 if (Qualifier)
635 mangleUnresolvedScope(Qualifier);
636 // FIXME: ambiguity of unqualified lookup with ::
637
638 mangleUnqualifiedName(0, Name, KnownArity);
639}
640
Anders Carlsson6f7e2f42010-06-08 14:49:03 +0000641static const FieldDecl *FindFirstNamedDataMember(const RecordDecl *RD) {
642 assert(RD->isAnonymousStructOrUnion() &&
643 "Expected anonymous struct or union!");
644
645 for (RecordDecl::field_iterator I = RD->field_begin(), E = RD->field_end();
646 I != E; ++I) {
647 const FieldDecl *FD = *I;
648
649 if (FD->getIdentifier())
650 return FD;
651
652 if (const RecordType *RT = FD->getType()->getAs<RecordType>()) {
653 if (const FieldDecl *NamedDataMember =
654 FindFirstNamedDataMember(RT->getDecl()))
655 return NamedDataMember;
656 }
657 }
658
659 // We didn't find a named data member.
660 return 0;
661}
662
John McCall1dd73832010-02-04 01:42:13 +0000663void CXXNameMangler::mangleUnqualifiedName(const NamedDecl *ND,
664 DeclarationName Name,
665 unsigned KnownArity) {
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000666 // <unqualified-name> ::= <operator-name>
Mike Stump1eb44332009-09-09 15:08:12 +0000667 // ::= <ctor-dtor-name>
668 // ::= <source-name>
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000669 switch (Name.getNameKind()) {
Anders Carlssonc4355b62009-10-07 01:45:02 +0000670 case DeclarationName::Identifier: {
Anders Carlssonc4355b62009-10-07 01:45:02 +0000671 if (const IdentifierInfo *II = Name.getAsIdentifierInfo()) {
Sean Hunt31455252010-01-24 03:04:27 +0000672 // We must avoid conflicts between internally- and externally-
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000673 // linked variable declaration names in the same TU.
Anders Carlssonaec25232010-02-06 04:52:27 +0000674 // This naming convention is the same as that followed by GCC, though it
675 // shouldn't actually matter.
676 if (ND && isa<VarDecl>(ND) && ND->getLinkage() == InternalLinkage &&
Sean Hunt31455252010-01-24 03:04:27 +0000677 ND->getDeclContext()->isFileContext())
678 Out << 'L';
679
Anders Carlssonc4355b62009-10-07 01:45:02 +0000680 mangleSourceName(II);
681 break;
682 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000683
John McCall1dd73832010-02-04 01:42:13 +0000684 // Otherwise, an anonymous entity. We must have a declaration.
685 assert(ND && "mangling empty name without declaration");
686
687 if (const NamespaceDecl *NS = dyn_cast<NamespaceDecl>(ND)) {
688 if (NS->isAnonymousNamespace()) {
689 // This is how gcc mangles these names.
690 Out << "12_GLOBAL__N_1";
691 break;
692 }
693 }
694
Anders Carlsson6f7e2f42010-06-08 14:49:03 +0000695 if (const VarDecl *VD = dyn_cast<VarDecl>(ND)) {
696 // We must have an anonymous union or struct declaration.
697 const RecordDecl *RD =
698 cast<RecordDecl>(VD->getType()->getAs<RecordType>()->getDecl());
699
700 // Itanium C++ ABI 5.1.2:
701 //
702 // For the purposes of mangling, the name of an anonymous union is
703 // considered to be the name of the first named data member found by a
704 // pre-order, depth-first, declaration-order walk of the data members of
705 // the anonymous union. If there is no such data member (i.e., if all of
706 // the data members in the union are unnamed), then there is no way for
707 // a program to refer to the anonymous union, and there is therefore no
708 // need to mangle its name.
709 const FieldDecl *FD = FindFirstNamedDataMember(RD);
John McCall7121c8f2010-08-05 22:02:13 +0000710
711 // It's actually possible for various reasons for us to get here
712 // with an empty anonymous struct / union. Fortunately, it
713 // doesn't really matter what name we generate.
714 if (!FD) break;
Anders Carlsson6f7e2f42010-06-08 14:49:03 +0000715 assert(FD->getIdentifier() && "Data member name isn't an identifier!");
716
717 mangleSourceName(FD->getIdentifier());
718 break;
719 }
720
Anders Carlssonc4355b62009-10-07 01:45:02 +0000721 // We must have an anonymous struct.
722 const TagDecl *TD = cast<TagDecl>(ND);
723 if (const TypedefDecl *D = TD->getTypedefForAnonDecl()) {
724 assert(TD->getDeclContext() == D->getDeclContext() &&
725 "Typedef should not be in another decl context!");
726 assert(D->getDeclName().getAsIdentifierInfo() &&
727 "Typedef was not named!");
728 mangleSourceName(D->getDeclName().getAsIdentifierInfo());
729 break;
730 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000731
Anders Carlssonc4355b62009-10-07 01:45:02 +0000732 // Get a unique id for the anonymous struct.
733 uint64_t AnonStructId = Context.getAnonymousStructId(TD);
734
735 // Mangle it as a source name in the form
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000736 // [n] $_<id>
Anders Carlssonc4355b62009-10-07 01:45:02 +0000737 // where n is the length of the string.
738 llvm::SmallString<8> Str;
739 Str += "$_";
740 Str += llvm::utostr(AnonStructId);
741
742 Out << Str.size();
743 Out << Str.str();
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000744 break;
Anders Carlssonc4355b62009-10-07 01:45:02 +0000745 }
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000746
747 case DeclarationName::ObjCZeroArgSelector:
748 case DeclarationName::ObjCOneArgSelector:
749 case DeclarationName::ObjCMultiArgSelector:
750 assert(false && "Can't mangle Objective-C selector names here!");
751 break;
752
753 case DeclarationName::CXXConstructorName:
Anders Carlsson27ae5362009-04-17 01:58:57 +0000754 if (ND == Structor)
Mike Stump141c5af2009-09-02 00:25:38 +0000755 // If the named decl is the C++ constructor we're mangling, use the type
756 // we were given.
Anders Carlsson27ae5362009-04-17 01:58:57 +0000757 mangleCXXCtorType(static_cast<CXXCtorType>(StructorType));
Anders Carlsson3ac86b52009-04-15 05:36:58 +0000758 else
759 // Otherwise, use the complete constructor name. This is relevant if a
760 // class with a constructor is declared within a constructor.
761 mangleCXXCtorType(Ctor_Complete);
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000762 break;
763
764 case DeclarationName::CXXDestructorName:
Anders Carlsson27ae5362009-04-17 01:58:57 +0000765 if (ND == Structor)
Mike Stump141c5af2009-09-02 00:25:38 +0000766 // If the named decl is the C++ destructor we're mangling, use the type we
767 // were given.
Anders Carlsson27ae5362009-04-17 01:58:57 +0000768 mangleCXXDtorType(static_cast<CXXDtorType>(StructorType));
769 else
770 // Otherwise, use the complete destructor name. This is relevant if a
771 // class with a destructor is declared within a destructor.
772 mangleCXXDtorType(Dtor_Complete);
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000773 break;
774
775 case DeclarationName::CXXConversionFunctionName:
Mike Stump1eb44332009-09-09 15:08:12 +0000776 // <operator-name> ::= cv <type> # (cast)
Douglas Gregor219cc612009-02-13 01:28:03 +0000777 Out << "cv";
Anders Carlssonb5404912009-10-07 01:06:45 +0000778 mangleType(Context.getASTContext().getCanonicalType(Name.getCXXNameType()));
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000779 break;
780
Anders Carlsson8257d412009-12-22 06:36:32 +0000781 case DeclarationName::CXXOperatorName: {
John McCall1dd73832010-02-04 01:42:13 +0000782 unsigned Arity;
783 if (ND) {
784 Arity = cast<FunctionDecl>(ND)->getNumParams();
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000785
John McCall1dd73832010-02-04 01:42:13 +0000786 // If we have a C++ member function, we need to include the 'this' pointer.
787 // FIXME: This does not make sense for operators that are static, but their
788 // names stay the same regardless of the arity (operator new for instance).
789 if (isa<CXXMethodDecl>(ND))
790 Arity++;
791 } else
792 Arity = KnownArity;
793
Anders Carlsson8257d412009-12-22 06:36:32 +0000794 mangleOperatorName(Name.getCXXOverloadedOperator(), Arity);
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000795 break;
Anders Carlsson8257d412009-12-22 06:36:32 +0000796 }
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000797
Sean Hunt3e518bd2009-11-29 07:34:05 +0000798 case DeclarationName::CXXLiteralOperatorName:
Sean Hunt5dd6b392009-12-04 21:11:13 +0000799 // FIXME: This mangling is not yet official.
Sean Hunt2421f662009-12-04 21:01:37 +0000800 Out << "li";
Sean Hunt3e518bd2009-11-29 07:34:05 +0000801 mangleSourceName(Name.getCXXLiteralIdentifier());
802 break;
803
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000804 case DeclarationName::CXXUsingDirective:
805 assert(false && "Can't mangle a using directive name!");
Douglas Gregor219cc612009-02-13 01:28:03 +0000806 break;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000807 }
808}
809
810void CXXNameMangler::mangleSourceName(const IdentifierInfo *II) {
811 // <source-name> ::= <positive length number> <identifier>
812 // <number> ::= [n] <non-negative decimal integer>
813 // <identifier> ::= <unqualified source code identifier>
814 Out << II->getLength() << II->getName();
815}
816
Eli Friedman7facf842009-12-02 20:32:49 +0000817void CXXNameMangler::mangleNestedName(const NamedDecl *ND,
Fariborz Jahanian57058532010-03-03 19:41:08 +0000818 const DeclContext *DC,
819 bool NoFunction) {
Douglas Gregor0a9a6d62011-01-26 17:36:28 +0000820 // <nested-name>
821 // ::= N [<CV-qualifiers>] [<ref-qualifier>] <prefix> <unqualified-name> E
822 // ::= N [<CV-qualifiers>] [<ref-qualifier>] <template-prefix>
823 // <template-args> E
Anders Carlssond99edc42009-09-26 03:55:37 +0000824
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000825 Out << 'N';
Douglas Gregor0a9a6d62011-01-26 17:36:28 +0000826 if (const CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(ND)) {
John McCall0953e762009-09-24 19:53:00 +0000827 mangleQualifiers(Qualifiers::fromCVRMask(Method->getTypeQualifiers()));
Douglas Gregor0a9a6d62011-01-26 17:36:28 +0000828 mangleRefQualifier(Method->getRefQualifier());
829 }
830
Anders Carlsson2744a062009-09-18 19:00:18 +0000831 // Check if we have a template.
832 const TemplateArgumentList *TemplateArgs = 0;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000833 if (const TemplateDecl *TD = isTemplate(ND, TemplateArgs)) {
Anders Carlsson2744a062009-09-18 19:00:18 +0000834 mangleTemplatePrefix(TD);
Rafael Espindolad9800722010-03-11 14:07:00 +0000835 TemplateParameterList *TemplateParameters = TD->getTemplateParameters();
836 mangleTemplateArgs(*TemplateParameters, *TemplateArgs);
Fariborz Jahanian57058532010-03-03 19:41:08 +0000837 }
838 else {
839 manglePrefix(DC, NoFunction);
Anders Carlsson7482e242009-09-18 04:29:09 +0000840 mangleUnqualifiedName(ND);
841 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000842
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000843 Out << 'E';
844}
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000845void CXXNameMangler::mangleNestedName(const TemplateDecl *TD,
Anders Carlsson7624f212009-09-18 02:42:01 +0000846 const TemplateArgument *TemplateArgs,
847 unsigned NumTemplateArgs) {
Anders Carlssone45117b2009-09-27 19:53:49 +0000848 // <nested-name> ::= N [<CV-qualifiers>] <template-prefix> <template-args> E
849
Anders Carlsson7624f212009-09-18 02:42:01 +0000850 Out << 'N';
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000851
Anders Carlssone45117b2009-09-27 19:53:49 +0000852 mangleTemplatePrefix(TD);
Rafael Espindolad9800722010-03-11 14:07:00 +0000853 TemplateParameterList *TemplateParameters = TD->getTemplateParameters();
854 mangleTemplateArgs(*TemplateParameters, TemplateArgs, NumTemplateArgs);
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000855
Anders Carlsson7624f212009-09-18 02:42:01 +0000856 Out << 'E';
857}
858
Anders Carlsson1b42c792009-04-02 16:24:45 +0000859void CXXNameMangler::mangleLocalName(const NamedDecl *ND) {
860 // <local-name> := Z <function encoding> E <entity name> [<discriminator>]
861 // := Z <function encoding> E s [<discriminator>]
Mike Stump1eb44332009-09-09 15:08:12 +0000862 // <discriminator> := _ <non-negative number>
Fariborz Jahanian57058532010-03-03 19:41:08 +0000863 const DeclContext *DC = ND->getDeclContext();
Anders Carlsson1b42c792009-04-02 16:24:45 +0000864 Out << 'Z';
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000865
Charles Davis685b1d92010-05-26 18:25:27 +0000866 if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(DC)) {
867 mangleObjCMethodName(MD);
John McCall82b7d7b2010-10-18 21:28:44 +0000868 } else if (const CXXRecordDecl *RD = GetLocalClassDecl(ND)) {
869 mangleFunctionEncoding(cast<FunctionDecl>(RD->getDeclContext()));
Fariborz Jahanian57058532010-03-03 19:41:08 +0000870 Out << 'E';
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000871
John McCall82b7d7b2010-10-18 21:28:44 +0000872 // Mangle the name relative to the closest enclosing function.
873 if (ND == RD) // equality ok because RD derived from ND above
874 mangleUnqualifiedName(ND);
875 else
876 mangleNestedName(ND, DC, true /*NoFunction*/);
877
Fariborz Jahanian4819ac42010-03-04 01:02:03 +0000878 unsigned disc;
John McCall82b7d7b2010-10-18 21:28:44 +0000879 if (Context.getNextDiscriminator(RD, disc)) {
Fariborz Jahanian4819ac42010-03-04 01:02:03 +0000880 if (disc < 10)
881 Out << '_' << disc;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000882 else
Fariborz Jahanian4819ac42010-03-04 01:02:03 +0000883 Out << "__" << disc << '_';
884 }
Fariborz Jahanian57058532010-03-03 19:41:08 +0000885
886 return;
887 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000888 else
Fariborz Jahanian57058532010-03-03 19:41:08 +0000889 mangleFunctionEncoding(cast<FunctionDecl>(DC));
Anders Carlsson7b06f6c2009-12-10 03:14:39 +0000890
Anders Carlsson1b42c792009-04-02 16:24:45 +0000891 Out << 'E';
Eli Friedman6f9f25d2009-12-11 20:21:38 +0000892 mangleUnqualifiedName(ND);
Anders Carlsson1b42c792009-04-02 16:24:45 +0000893}
894
Fariborz Jahanian57058532010-03-03 19:41:08 +0000895void CXXNameMangler::manglePrefix(const DeclContext *DC, bool NoFunction) {
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000896 // <prefix> ::= <prefix> <unqualified-name>
897 // ::= <template-prefix> <template-args>
898 // ::= <template-param>
899 // ::= # empty
900 // ::= <substitution>
Anders Carlsson6862fc72009-09-17 04:16:28 +0000901
Anders Carlssonadd28822009-09-22 20:33:31 +0000902 while (isa<LinkageSpecDecl>(DC))
903 DC = DC->getParent();
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000904
Anders Carlsson9263e912009-09-18 18:39:58 +0000905 if (DC->isTranslationUnit())
906 return;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000907
Douglas Gregor35415f52010-05-25 17:04:15 +0000908 if (const BlockDecl *Block = dyn_cast<BlockDecl>(DC)) {
909 manglePrefix(DC->getParent(), NoFunction);
910 llvm::SmallString<64> Name;
Rafael Espindolac4850c22011-02-10 23:59:36 +0000911 llvm::raw_svector_ostream NameStream(Name);
912 Context.mangleBlock(Block, NameStream);
913 NameStream.flush();
Douglas Gregor35415f52010-05-25 17:04:15 +0000914 Out << Name.size() << Name;
915 return;
916 }
917
Anders Carlsson6862fc72009-09-17 04:16:28 +0000918 if (mangleSubstitution(cast<NamedDecl>(DC)))
919 return;
Anders Carlsson7482e242009-09-18 04:29:09 +0000920
Anders Carlsson2ee3fca2009-09-18 20:11:09 +0000921 // Check if we have a template.
922 const TemplateArgumentList *TemplateArgs = 0;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000923 if (const TemplateDecl *TD = isTemplate(cast<NamedDecl>(DC), TemplateArgs)) {
Anders Carlsson2ee3fca2009-09-18 20:11:09 +0000924 mangleTemplatePrefix(TD);
Rafael Espindolad9800722010-03-11 14:07:00 +0000925 TemplateParameterList *TemplateParameters = TD->getTemplateParameters();
926 mangleTemplateArgs(*TemplateParameters, *TemplateArgs);
Fariborz Jahanian57058532010-03-03 19:41:08 +0000927 }
Douglas Gregor35415f52010-05-25 17:04:15 +0000928 else if(NoFunction && (isa<FunctionDecl>(DC) || isa<ObjCMethodDecl>(DC)))
Fariborz Jahanian57058532010-03-03 19:41:08 +0000929 return;
Douglas Gregor35415f52010-05-25 17:04:15 +0000930 else if (const ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(DC))
931 mangleObjCMethodName(Method);
Fariborz Jahanian57058532010-03-03 19:41:08 +0000932 else {
933 manglePrefix(DC->getParent(), NoFunction);
Anders Carlsson2ee3fca2009-09-18 20:11:09 +0000934 mangleUnqualifiedName(cast<NamedDecl>(DC));
935 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000936
Anders Carlsson6862fc72009-09-17 04:16:28 +0000937 addSubstitution(cast<NamedDecl>(DC));
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000938}
939
Douglas Gregor20f0cc72010-04-23 03:10:43 +0000940void CXXNameMangler::mangleTemplatePrefix(TemplateName Template) {
941 // <template-prefix> ::= <prefix> <template unqualified-name>
942 // ::= <template-param>
943 // ::= <substitution>
944 if (TemplateDecl *TD = Template.getAsTemplateDecl())
945 return mangleTemplatePrefix(TD);
946
947 if (QualifiedTemplateName *Qualified = Template.getAsQualifiedTemplateName())
948 mangleUnresolvedScope(Qualified->getQualifier());
Sean Huntc3021132010-05-05 15:23:54 +0000949
Douglas Gregor20f0cc72010-04-23 03:10:43 +0000950 if (OverloadedTemplateStorage *Overloaded
951 = Template.getAsOverloadedTemplate()) {
Sean Huntc3021132010-05-05 15:23:54 +0000952 mangleUnqualifiedName(0, (*Overloaded->begin())->getDeclName(),
Douglas Gregor20f0cc72010-04-23 03:10:43 +0000953 UnknownArity);
954 return;
955 }
Sean Huntc3021132010-05-05 15:23:54 +0000956
Douglas Gregor20f0cc72010-04-23 03:10:43 +0000957 DependentTemplateName *Dependent = Template.getAsDependentTemplateName();
958 assert(Dependent && "Unknown template name kind?");
959 mangleUnresolvedScope(Dependent->getQualifier());
Douglas Gregor1e9268e2010-04-28 05:58:56 +0000960 mangleUnscopedTemplateName(Template);
Douglas Gregor20f0cc72010-04-23 03:10:43 +0000961}
962
Anders Carlsson0fa6df42009-09-26 19:45:45 +0000963void CXXNameMangler::mangleTemplatePrefix(const TemplateDecl *ND) {
Anders Carlsson7482e242009-09-18 04:29:09 +0000964 // <template-prefix> ::= <prefix> <template unqualified-name>
965 // ::= <template-param>
966 // ::= <substitution>
Douglas Gregor32fb4e12010-02-05 20:45:00 +0000967 // <template-template-param> ::= <template-param>
968 // <substitution>
Anders Carlsson7482e242009-09-18 04:29:09 +0000969
Anders Carlssonaeb85372009-09-26 22:18:22 +0000970 if (mangleSubstitution(ND))
971 return;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000972
Douglas Gregor32fb4e12010-02-05 20:45:00 +0000973 // <template-template-param> ::= <template-param>
974 if (const TemplateTemplateParmDecl *TTP
975 = dyn_cast<TemplateTemplateParmDecl>(ND)) {
976 mangleTemplateParameter(TTP->getIndex());
977 return;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000978 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000979
Anders Carlssonaa73ab12009-09-18 18:47:07 +0000980 manglePrefix(ND->getDeclContext());
Anders Carlsson1668f202009-09-26 20:13:56 +0000981 mangleUnqualifiedName(ND->getTemplatedDecl());
Anders Carlssonaeb85372009-09-26 22:18:22 +0000982 addSubstitution(ND);
Anders Carlsson7482e242009-09-18 04:29:09 +0000983}
984
John McCallb6f532e2010-07-14 06:43:17 +0000985/// Mangles a template name under the production <type>. Required for
986/// template template arguments.
987/// <type> ::= <class-enum-type>
988/// ::= <template-param>
989/// ::= <substitution>
990void CXXNameMangler::mangleType(TemplateName TN) {
991 if (mangleSubstitution(TN))
992 return;
993
994 TemplateDecl *TD = 0;
995
996 switch (TN.getKind()) {
997 case TemplateName::QualifiedTemplate:
998 TD = TN.getAsQualifiedTemplateName()->getTemplateDecl();
999 goto HaveDecl;
1000
1001 case TemplateName::Template:
1002 TD = TN.getAsTemplateDecl();
1003 goto HaveDecl;
1004
1005 HaveDecl:
1006 if (isa<TemplateTemplateParmDecl>(TD))
1007 mangleTemplateParameter(cast<TemplateTemplateParmDecl>(TD)->getIndex());
1008 else
1009 mangleName(TD);
1010 break;
1011
1012 case TemplateName::OverloadedTemplate:
1013 llvm_unreachable("can't mangle an overloaded template name as a <type>");
1014 break;
1015
1016 case TemplateName::DependentTemplate: {
1017 const DependentTemplateName *Dependent = TN.getAsDependentTemplateName();
1018 assert(Dependent->isIdentifier());
1019
1020 // <class-enum-type> ::= <name>
1021 // <name> ::= <nested-name>
1022 mangleUnresolvedScope(Dependent->getQualifier());
1023 mangleSourceName(Dependent->getIdentifier());
1024 break;
1025 }
1026
Douglas Gregor1aee05d2011-01-15 06:45:20 +00001027 case TemplateName::SubstTemplateTemplateParmPack: {
1028 SubstTemplateTemplateParmPackStorage *SubstPack
1029 = TN.getAsSubstTemplateTemplateParmPack();
1030 mangleTemplateParameter(SubstPack->getParameterPack()->getIndex());
1031 break;
1032 }
John McCallb6f532e2010-07-14 06:43:17 +00001033 }
1034
1035 addSubstitution(TN);
1036}
1037
Mike Stump1eb44332009-09-09 15:08:12 +00001038void
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001039CXXNameMangler::mangleOperatorName(OverloadedOperatorKind OO, unsigned Arity) {
1040 switch (OO) {
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001041 // <operator-name> ::= nw # new
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001042 case OO_New: Out << "nw"; break;
1043 // ::= na # new[]
1044 case OO_Array_New: Out << "na"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001045 // ::= dl # delete
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001046 case OO_Delete: Out << "dl"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001047 // ::= da # delete[]
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001048 case OO_Array_Delete: Out << "da"; break;
1049 // ::= ps # + (unary)
John McCall5e1e89b2010-08-18 19:18:59 +00001050 // ::= pl # + (binary or unknown)
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001051 case OO_Plus:
Anders Carlsson8257d412009-12-22 06:36:32 +00001052 Out << (Arity == 1? "ps" : "pl"); break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001053 // ::= ng # - (unary)
John McCall5e1e89b2010-08-18 19:18:59 +00001054 // ::= mi # - (binary or unknown)
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001055 case OO_Minus:
Anders Carlsson8257d412009-12-22 06:36:32 +00001056 Out << (Arity == 1? "ng" : "mi"); break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001057 // ::= ad # & (unary)
John McCall5e1e89b2010-08-18 19:18:59 +00001058 // ::= an # & (binary or unknown)
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001059 case OO_Amp:
Anders Carlsson8257d412009-12-22 06:36:32 +00001060 Out << (Arity == 1? "ad" : "an"); break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001061 // ::= de # * (unary)
John McCall5e1e89b2010-08-18 19:18:59 +00001062 // ::= ml # * (binary or unknown)
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001063 case OO_Star:
John McCall5e1e89b2010-08-18 19:18:59 +00001064 // Use binary when unknown.
Anders Carlsson8257d412009-12-22 06:36:32 +00001065 Out << (Arity == 1? "de" : "ml"); break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001066 // ::= co # ~
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001067 case OO_Tilde: Out << "co"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001068 // ::= dv # /
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001069 case OO_Slash: Out << "dv"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001070 // ::= rm # %
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001071 case OO_Percent: Out << "rm"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001072 // ::= or # |
1073 case OO_Pipe: Out << "or"; break;
1074 // ::= eo # ^
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001075 case OO_Caret: Out << "eo"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001076 // ::= aS # =
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001077 case OO_Equal: Out << "aS"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001078 // ::= pL # +=
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001079 case OO_PlusEqual: Out << "pL"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001080 // ::= mI # -=
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001081 case OO_MinusEqual: Out << "mI"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001082 // ::= mL # *=
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001083 case OO_StarEqual: Out << "mL"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001084 // ::= dV # /=
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001085 case OO_SlashEqual: Out << "dV"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001086 // ::= rM # %=
1087 case OO_PercentEqual: Out << "rM"; break;
1088 // ::= aN # &=
1089 case OO_AmpEqual: Out << "aN"; break;
1090 // ::= oR # |=
1091 case OO_PipeEqual: Out << "oR"; break;
1092 // ::= eO # ^=
1093 case OO_CaretEqual: Out << "eO"; break;
1094 // ::= ls # <<
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001095 case OO_LessLess: Out << "ls"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001096 // ::= rs # >>
1097 case OO_GreaterGreater: Out << "rs"; break;
1098 // ::= lS # <<=
1099 case OO_LessLessEqual: Out << "lS"; break;
1100 // ::= rS # >>=
1101 case OO_GreaterGreaterEqual: Out << "rS"; break;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001102 // ::= eq # ==
1103 case OO_EqualEqual: Out << "eq"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001104 // ::= ne # !=
1105 case OO_ExclaimEqual: Out << "ne"; break;
1106 // ::= lt # <
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001107 case OO_Less: Out << "lt"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001108 // ::= gt # >
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001109 case OO_Greater: Out << "gt"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001110 // ::= le # <=
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001111 case OO_LessEqual: Out << "le"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001112 // ::= ge # >=
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001113 case OO_GreaterEqual: Out << "ge"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001114 // ::= nt # !
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001115 case OO_Exclaim: Out << "nt"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001116 // ::= aa # &&
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001117 case OO_AmpAmp: Out << "aa"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001118 // ::= oo # ||
1119 case OO_PipePipe: Out << "oo"; break;
1120 // ::= pp # ++
1121 case OO_PlusPlus: Out << "pp"; break;
1122 // ::= mm # --
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001123 case OO_MinusMinus: Out << "mm"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001124 // ::= cm # ,
1125 case OO_Comma: Out << "cm"; break;
1126 // ::= pm # ->*
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001127 case OO_ArrowStar: Out << "pm"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001128 // ::= pt # ->
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001129 case OO_Arrow: Out << "pt"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001130 // ::= cl # ()
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001131 case OO_Call: Out << "cl"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001132 // ::= ix # []
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001133 case OO_Subscript: Out << "ix"; break;
Anders Carlssone170ba72009-12-14 01:45:37 +00001134
1135 // ::= qu # ?
1136 // The conditional operator can't be overloaded, but we still handle it when
1137 // mangling expressions.
1138 case OO_Conditional: Out << "qu"; break;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001139
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001140 case OO_None:
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001141 case NUM_OVERLOADED_OPERATORS:
Mike Stump1eb44332009-09-09 15:08:12 +00001142 assert(false && "Not an overloaded operator");
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001143 break;
1144 }
1145}
1146
John McCall0953e762009-09-24 19:53:00 +00001147void CXXNameMangler::mangleQualifiers(Qualifiers Quals) {
Mike Stump1eb44332009-09-09 15:08:12 +00001148 // <CV-qualifiers> ::= [r] [V] [K] # restrict (C99), volatile, const
John McCall0953e762009-09-24 19:53:00 +00001149 if (Quals.hasRestrict())
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001150 Out << 'r';
John McCall0953e762009-09-24 19:53:00 +00001151 if (Quals.hasVolatile())
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001152 Out << 'V';
John McCall0953e762009-09-24 19:53:00 +00001153 if (Quals.hasConst())
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001154 Out << 'K';
John McCall0953e762009-09-24 19:53:00 +00001155
Douglas Gregor56079f72010-06-14 23:15:08 +00001156 if (Quals.hasAddressSpace()) {
1157 // Extension:
1158 //
1159 // <type> ::= U <address-space-number>
1160 //
1161 // where <address-space-number> is a source name consisting of 'AS'
1162 // followed by the address space <number>.
1163 llvm::SmallString<64> ASString;
1164 ASString = "AS" + llvm::utostr_32(Quals.getAddressSpace());
1165 Out << 'U' << ASString.size() << ASString;
1166 }
1167
John McCall0953e762009-09-24 19:53:00 +00001168 // FIXME: For now, just drop all extension qualifiers on the floor.
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001169}
1170
Douglas Gregor0a9a6d62011-01-26 17:36:28 +00001171void CXXNameMangler::mangleRefQualifier(RefQualifierKind RefQualifier) {
1172 // <ref-qualifier> ::= R # lvalue reference
1173 // ::= O # rvalue-reference
1174 // Proposal to Itanium C++ ABI list on 1/26/11
1175 switch (RefQualifier) {
1176 case RQ_None:
1177 break;
1178
1179 case RQ_LValue:
1180 Out << 'R';
1181 break;
1182
1183 case RQ_RValue:
1184 Out << 'O';
1185 break;
1186 }
1187}
1188
Anders Carlsson7b06f6c2009-12-10 03:14:39 +00001189void CXXNameMangler::mangleObjCMethodName(const ObjCMethodDecl *MD) {
Rafael Espindolaf0be9792011-02-11 02:52:17 +00001190 Context.mangleObjCMethodName(MD, Out);
Anders Carlsson7b06f6c2009-12-10 03:14:39 +00001191}
1192
John McCallb47f7482011-01-26 20:05:40 +00001193void CXXNameMangler::mangleType(QualType nonCanon) {
Anders Carlsson4843e582009-03-10 17:07:44 +00001194 // Only operate on the canonical type!
John McCallb47f7482011-01-26 20:05:40 +00001195 QualType canon = nonCanon.getCanonicalType();
Anders Carlsson4843e582009-03-10 17:07:44 +00001196
John McCallb47f7482011-01-26 20:05:40 +00001197 SplitQualType split = canon.split();
1198 Qualifiers quals = split.second;
1199 const Type *ty = split.first;
1200
1201 bool isSubstitutable = quals || !isa<BuiltinType>(ty);
1202 if (isSubstitutable && mangleSubstitution(canon))
Anders Carlsson76967372009-09-17 00:43:46 +00001203 return;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001204
John McCallb47f7482011-01-26 20:05:40 +00001205 // If we're mangling a qualified array type, push the qualifiers to
1206 // the element type.
1207 if (quals && isa<ArrayType>(ty)) {
1208 ty = Context.getASTContext().getAsArrayType(canon);
1209 quals = Qualifiers();
1210
1211 // Note that we don't update canon: we want to add the
1212 // substitution at the canonical type.
1213 }
1214
1215 if (quals) {
1216 mangleQualifiers(quals);
John McCall0953e762009-09-24 19:53:00 +00001217 // Recurse: even if the qualified type isn't yet substitutable,
1218 // the unqualified type might be.
John McCallb47f7482011-01-26 20:05:40 +00001219 mangleType(QualType(ty, 0));
Anders Carlsson76967372009-09-17 00:43:46 +00001220 } else {
John McCallb47f7482011-01-26 20:05:40 +00001221 switch (ty->getTypeClass()) {
John McCallefe6aee2009-09-05 07:56:18 +00001222#define ABSTRACT_TYPE(CLASS, PARENT)
1223#define NON_CANONICAL_TYPE(CLASS, PARENT) \
Anders Carlsson76967372009-09-17 00:43:46 +00001224 case Type::CLASS: \
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +00001225 llvm_unreachable("can't mangle non-canonical type " #CLASS "Type"); \
Anders Carlsson76967372009-09-17 00:43:46 +00001226 return;
John McCallefe6aee2009-09-05 07:56:18 +00001227#define TYPE(CLASS, PARENT) \
Anders Carlsson76967372009-09-17 00:43:46 +00001228 case Type::CLASS: \
John McCallb47f7482011-01-26 20:05:40 +00001229 mangleType(static_cast<const CLASS##Type*>(ty)); \
Anders Carlsson76967372009-09-17 00:43:46 +00001230 break;
John McCallefe6aee2009-09-05 07:56:18 +00001231#include "clang/AST/TypeNodes.def"
Anders Carlsson76967372009-09-17 00:43:46 +00001232 }
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001233 }
Anders Carlsson76967372009-09-17 00:43:46 +00001234
1235 // Add the substitution.
John McCallb47f7482011-01-26 20:05:40 +00001236 if (isSubstitutable)
1237 addSubstitution(canon);
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001238}
1239
Douglas Gregor1b12a3b2010-05-26 05:11:13 +00001240void CXXNameMangler::mangleNameOrStandardSubstitution(const NamedDecl *ND) {
1241 if (!mangleStandardSubstitution(ND))
1242 mangleName(ND);
1243}
1244
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001245void CXXNameMangler::mangleType(const BuiltinType *T) {
John McCallefe6aee2009-09-05 07:56:18 +00001246 // <type> ::= <builtin-type>
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001247 // <builtin-type> ::= v # void
1248 // ::= w # wchar_t
1249 // ::= b # bool
1250 // ::= c # char
1251 // ::= a # signed char
1252 // ::= h # unsigned char
1253 // ::= s # short
1254 // ::= t # unsigned short
1255 // ::= i # int
1256 // ::= j # unsigned int
1257 // ::= l # long
1258 // ::= m # unsigned long
1259 // ::= x # long long, __int64
1260 // ::= y # unsigned long long, __int64
1261 // ::= n # __int128
1262 // UNSUPPORTED: ::= o # unsigned __int128
1263 // ::= f # float
1264 // ::= d # double
1265 // ::= e # long double, __float80
1266 // UNSUPPORTED: ::= g # __float128
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001267 // UNSUPPORTED: ::= Dd # IEEE 754r decimal floating point (64 bits)
1268 // UNSUPPORTED: ::= De # IEEE 754r decimal floating point (128 bits)
1269 // UNSUPPORTED: ::= Df # IEEE 754r decimal floating point (32 bits)
1270 // UNSUPPORTED: ::= Dh # IEEE 754r half-precision floating point (16 bits)
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001271 // ::= Di # char32_t
1272 // ::= Ds # char16_t
Anders Carlssone2923682010-11-04 04:31:32 +00001273 // ::= Dn # std::nullptr_t (i.e., decltype(nullptr))
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001274 // ::= u <source-name> # vendor extended type
1275 switch (T->getKind()) {
1276 case BuiltinType::Void: Out << 'v'; break;
1277 case BuiltinType::Bool: Out << 'b'; break;
1278 case BuiltinType::Char_U: case BuiltinType::Char_S: Out << 'c'; break;
1279 case BuiltinType::UChar: Out << 'h'; break;
1280 case BuiltinType::UShort: Out << 't'; break;
1281 case BuiltinType::UInt: Out << 'j'; break;
1282 case BuiltinType::ULong: Out << 'm'; break;
1283 case BuiltinType::ULongLong: Out << 'y'; break;
Chris Lattner2df9ced2009-04-30 02:43:43 +00001284 case BuiltinType::UInt128: Out << 'o'; break;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001285 case BuiltinType::SChar: Out << 'a'; break;
Chris Lattner3f59c972010-12-25 23:25:43 +00001286 case BuiltinType::WChar_S:
1287 case BuiltinType::WChar_U: Out << 'w'; break;
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001288 case BuiltinType::Char16: Out << "Ds"; break;
1289 case BuiltinType::Char32: Out << "Di"; break;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001290 case BuiltinType::Short: Out << 's'; break;
1291 case BuiltinType::Int: Out << 'i'; break;
1292 case BuiltinType::Long: Out << 'l'; break;
1293 case BuiltinType::LongLong: Out << 'x'; break;
Chris Lattner2df9ced2009-04-30 02:43:43 +00001294 case BuiltinType::Int128: Out << 'n'; break;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001295 case BuiltinType::Float: Out << 'f'; break;
1296 case BuiltinType::Double: Out << 'd'; break;
1297 case BuiltinType::LongDouble: Out << 'e'; break;
Anders Carlssone2923682010-11-04 04:31:32 +00001298 case BuiltinType::NullPtr: Out << "Dn"; break;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001299
1300 case BuiltinType::Overload:
1301 case BuiltinType::Dependent:
Mike Stump1eb44332009-09-09 15:08:12 +00001302 assert(false &&
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001303 "Overloaded and dependent types shouldn't get to name mangling");
1304 break;
Anders Carlssone89d1592009-06-26 18:41:36 +00001305 case BuiltinType::UndeducedAuto:
1306 assert(0 && "Should not see undeduced auto here");
1307 break;
Steve Naroff9533a7f2009-07-22 17:14:51 +00001308 case BuiltinType::ObjCId: Out << "11objc_object"; break;
1309 case BuiltinType::ObjCClass: Out << "10objc_class"; break;
Fariborz Jahanian13dcd002009-11-21 19:53:08 +00001310 case BuiltinType::ObjCSel: Out << "13objc_selector"; break;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001311 }
1312}
1313
John McCallefe6aee2009-09-05 07:56:18 +00001314// <type> ::= <function-type>
1315// <function-type> ::= F [Y] <bare-function-type> E
1316void CXXNameMangler::mangleType(const FunctionProtoType *T) {
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001317 Out << 'F';
Mike Stumpf5408fe2009-05-16 07:57:57 +00001318 // FIXME: We don't have enough information in the AST to produce the 'Y'
1319 // encoding for extern "C" function types.
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001320 mangleBareFunctionType(T, /*MangleReturnType=*/true);
1321 Out << 'E';
1322}
John McCallefe6aee2009-09-05 07:56:18 +00001323void CXXNameMangler::mangleType(const FunctionNoProtoType *T) {
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +00001324 llvm_unreachable("Can't mangle K&R function prototypes");
John McCallefe6aee2009-09-05 07:56:18 +00001325}
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001326void CXXNameMangler::mangleBareFunctionType(const FunctionType *T,
1327 bool MangleReturnType) {
John McCallefe6aee2009-09-05 07:56:18 +00001328 // We should never be mangling something without a prototype.
1329 const FunctionProtoType *Proto = cast<FunctionProtoType>(T);
1330
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001331 // <bare-function-type> ::= <signature type>+
1332 if (MangleReturnType)
John McCallefe6aee2009-09-05 07:56:18 +00001333 mangleType(Proto->getResultType());
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001334
Anders Carlsson93296682010-06-02 04:40:13 +00001335 if (Proto->getNumArgs() == 0 && !Proto->isVariadic()) {
Eli Friedmana7e68452010-08-22 01:00:03 +00001336 // <builtin-type> ::= v # void
Anders Carlssonc6c91bc2009-04-01 00:15:23 +00001337 Out << 'v';
1338 return;
1339 }
Mike Stump1eb44332009-09-09 15:08:12 +00001340
Douglas Gregor72564e72009-02-26 23:50:07 +00001341 for (FunctionProtoType::arg_type_iterator Arg = Proto->arg_type_begin(),
Mike Stump1eb44332009-09-09 15:08:12 +00001342 ArgEnd = Proto->arg_type_end();
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001343 Arg != ArgEnd; ++Arg)
1344 mangleType(*Arg);
Douglas Gregor219cc612009-02-13 01:28:03 +00001345
1346 // <builtin-type> ::= z # ellipsis
1347 if (Proto->isVariadic())
1348 Out << 'z';
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001349}
1350
John McCallefe6aee2009-09-05 07:56:18 +00001351// <type> ::= <class-enum-type>
Mike Stump1eb44332009-09-09 15:08:12 +00001352// <class-enum-type> ::= <name>
John McCalled976492009-12-04 22:46:56 +00001353void CXXNameMangler::mangleType(const UnresolvedUsingType *T) {
1354 mangleName(T->getDecl());
1355}
1356
1357// <type> ::= <class-enum-type>
1358// <class-enum-type> ::= <name>
John McCallefe6aee2009-09-05 07:56:18 +00001359void CXXNameMangler::mangleType(const EnumType *T) {
1360 mangleType(static_cast<const TagType*>(T));
1361}
1362void CXXNameMangler::mangleType(const RecordType *T) {
1363 mangleType(static_cast<const TagType*>(T));
1364}
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001365void CXXNameMangler::mangleType(const TagType *T) {
Eli Friedmanecb7e932009-12-11 18:00:57 +00001366 mangleName(T->getDecl());
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001367}
1368
John McCallefe6aee2009-09-05 07:56:18 +00001369// <type> ::= <array-type>
1370// <array-type> ::= A <positive dimension number> _ <element type>
1371// ::= A [<dimension expression>] _ <element type>
1372void CXXNameMangler::mangleType(const ConstantArrayType *T) {
1373 Out << 'A' << T->getSize() << '_';
1374 mangleType(T->getElementType());
1375}
1376void CXXNameMangler::mangleType(const VariableArrayType *T) {
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001377 Out << 'A';
Fariborz Jahanian7281d1f2010-11-02 16:54:00 +00001378 // decayed vla types (size 0) will just be skipped.
1379 if (T->getSizeExpr())
1380 mangleExpression(T->getSizeExpr());
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001381 Out << '_';
1382 mangleType(T->getElementType());
1383}
John McCallefe6aee2009-09-05 07:56:18 +00001384void CXXNameMangler::mangleType(const DependentSizedArrayType *T) {
1385 Out << 'A';
1386 mangleExpression(T->getSizeExpr());
1387 Out << '_';
1388 mangleType(T->getElementType());
1389}
1390void CXXNameMangler::mangleType(const IncompleteArrayType *T) {
Nick Lewycky271b6652010-09-05 03:40:33 +00001391 Out << "A_";
John McCallefe6aee2009-09-05 07:56:18 +00001392 mangleType(T->getElementType());
1393}
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001394
John McCallefe6aee2009-09-05 07:56:18 +00001395// <type> ::= <pointer-to-member-type>
1396// <pointer-to-member-type> ::= M <class type> <member type>
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001397void CXXNameMangler::mangleType(const MemberPointerType *T) {
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001398 Out << 'M';
1399 mangleType(QualType(T->getClass(), 0));
Anders Carlsson0e650012009-05-17 17:41:20 +00001400 QualType PointeeType = T->getPointeeType();
1401 if (const FunctionProtoType *FPT = dyn_cast<FunctionProtoType>(PointeeType)) {
John McCall0953e762009-09-24 19:53:00 +00001402 mangleQualifiers(Qualifiers::fromCVRMask(FPT->getTypeQuals()));
Douglas Gregor0a9a6d62011-01-26 17:36:28 +00001403 mangleRefQualifier(FPT->getRefQualifier());
Anders Carlsson0e650012009-05-17 17:41:20 +00001404 mangleType(FPT);
Anders Carlsson9d85b722010-06-02 04:29:50 +00001405
1406 // Itanium C++ ABI 5.1.8:
1407 //
1408 // The type of a non-static member function is considered to be different,
1409 // for the purposes of substitution, from the type of a namespace-scope or
1410 // static member function whose type appears similar. The types of two
1411 // non-static member functions are considered to be different, for the
1412 // purposes of substitution, if the functions are members of different
1413 // classes. In other words, for the purposes of substitution, the class of
1414 // which the function is a member is considered part of the type of
1415 // function.
1416
1417 // We increment the SeqID here to emulate adding an entry to the
1418 // substitution table. We can't actually add it because we don't want this
1419 // particular function type to be substituted.
1420 ++SeqID;
Mike Stump1eb44332009-09-09 15:08:12 +00001421 } else
Anders Carlsson0e650012009-05-17 17:41:20 +00001422 mangleType(PointeeType);
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001423}
1424
John McCallefe6aee2009-09-05 07:56:18 +00001425// <type> ::= <template-param>
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001426void CXXNameMangler::mangleType(const TemplateTypeParmType *T) {
Anders Carlsson0ccdf8d2009-09-27 00:38:53 +00001427 mangleTemplateParameter(T->getIndex());
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001428}
1429
Douglas Gregorc3069d62011-01-14 02:55:32 +00001430// <type> ::= <template-param>
1431void CXXNameMangler::mangleType(const SubstTemplateTypeParmPackType *T) {
1432 mangleTemplateParameter(T->getReplacedParameter()->getIndex());
1433}
1434
John McCallefe6aee2009-09-05 07:56:18 +00001435// <type> ::= P <type> # pointer-to
1436void CXXNameMangler::mangleType(const PointerType *T) {
1437 Out << 'P';
1438 mangleType(T->getPointeeType());
1439}
1440void CXXNameMangler::mangleType(const ObjCObjectPointerType *T) {
1441 Out << 'P';
1442 mangleType(T->getPointeeType());
1443}
1444
1445// <type> ::= R <type> # reference-to
1446void CXXNameMangler::mangleType(const LValueReferenceType *T) {
1447 Out << 'R';
1448 mangleType(T->getPointeeType());
1449}
1450
1451// <type> ::= O <type> # rvalue reference-to (C++0x)
1452void CXXNameMangler::mangleType(const RValueReferenceType *T) {
1453 Out << 'O';
1454 mangleType(T->getPointeeType());
1455}
1456
1457// <type> ::= C <type> # complex pair (C 2000)
1458void CXXNameMangler::mangleType(const ComplexType *T) {
1459 Out << 'C';
1460 mangleType(T->getElementType());
1461}
1462
Bob Wilsonc7df92d2010-11-12 17:24:43 +00001463// ARM's ABI for Neon vector types specifies that they should be mangled as
Bob Wilson57147a82010-11-16 00:32:18 +00001464// if they are structs (to match ARM's initial implementation). The
1465// vector type must be one of the special types predefined by ARM.
1466void CXXNameMangler::mangleNeonVectorType(const VectorType *T) {
Bob Wilsonc7df92d2010-11-12 17:24:43 +00001467 QualType EltType = T->getElementType();
Bob Wilson57147a82010-11-16 00:32:18 +00001468 assert(EltType->isBuiltinType() && "Neon vector element not a BuiltinType");
Bob Wilsonc7df92d2010-11-12 17:24:43 +00001469 const char *EltName = 0;
Bob Wilson491328c2010-11-12 17:24:46 +00001470 if (T->getVectorKind() == VectorType::NeonPolyVector) {
1471 switch (cast<BuiltinType>(EltType)->getKind()) {
Bob Wilson4cfaa5d2010-11-12 17:24:49 +00001472 case BuiltinType::SChar: EltName = "poly8_t"; break;
1473 case BuiltinType::Short: EltName = "poly16_t"; break;
Bob Wilson57147a82010-11-16 00:32:18 +00001474 default: llvm_unreachable("unexpected Neon polynomial vector element type");
Bob Wilson491328c2010-11-12 17:24:46 +00001475 }
1476 } else {
1477 switch (cast<BuiltinType>(EltType)->getKind()) {
Bob Wilson4cfaa5d2010-11-12 17:24:49 +00001478 case BuiltinType::SChar: EltName = "int8_t"; break;
1479 case BuiltinType::UChar: EltName = "uint8_t"; break;
1480 case BuiltinType::Short: EltName = "int16_t"; break;
1481 case BuiltinType::UShort: EltName = "uint16_t"; break;
1482 case BuiltinType::Int: EltName = "int32_t"; break;
1483 case BuiltinType::UInt: EltName = "uint32_t"; break;
1484 case BuiltinType::LongLong: EltName = "int64_t"; break;
1485 case BuiltinType::ULongLong: EltName = "uint64_t"; break;
1486 case BuiltinType::Float: EltName = "float32_t"; break;
Bob Wilson57147a82010-11-16 00:32:18 +00001487 default: llvm_unreachable("unexpected Neon vector element type");
Bob Wilson491328c2010-11-12 17:24:46 +00001488 }
Bob Wilsonc7df92d2010-11-12 17:24:43 +00001489 }
1490 const char *BaseName = 0;
Bob Wilson4cfaa5d2010-11-12 17:24:49 +00001491 unsigned BitSize = (T->getNumElements() *
Bob Wilson3a723022010-11-16 00:32:12 +00001492 getASTContext().getTypeSize(EltType));
Bob Wilsonc7df92d2010-11-12 17:24:43 +00001493 if (BitSize == 64)
1494 BaseName = "__simd64_";
Bob Wilson57147a82010-11-16 00:32:18 +00001495 else {
1496 assert(BitSize == 128 && "Neon vector type not 64 or 128 bits");
Bob Wilsonc7df92d2010-11-12 17:24:43 +00001497 BaseName = "__simd128_";
Bob Wilson57147a82010-11-16 00:32:18 +00001498 }
Bob Wilsonc7df92d2010-11-12 17:24:43 +00001499 Out << strlen(BaseName) + strlen(EltName);
1500 Out << BaseName << EltName;
Bob Wilsonc7df92d2010-11-12 17:24:43 +00001501}
1502
John McCallefe6aee2009-09-05 07:56:18 +00001503// GNU extension: vector types
Chris Lattner788b0fd2010-06-23 06:00:24 +00001504// <type> ::= <vector-type>
1505// <vector-type> ::= Dv <positive dimension number> _
1506// <extended element type>
1507// ::= Dv [<dimension expression>] _ <element type>
1508// <extended element type> ::= <element type>
1509// ::= p # AltiVec vector pixel
John McCallefe6aee2009-09-05 07:56:18 +00001510void CXXNameMangler::mangleType(const VectorType *T) {
Bob Wilson491328c2010-11-12 17:24:46 +00001511 if ((T->getVectorKind() == VectorType::NeonVector ||
Bob Wilson57147a82010-11-16 00:32:18 +00001512 T->getVectorKind() == VectorType::NeonPolyVector)) {
1513 mangleNeonVectorType(T);
Bob Wilsonc7df92d2010-11-12 17:24:43 +00001514 return;
Bob Wilson57147a82010-11-16 00:32:18 +00001515 }
Nick Lewycky0e5f0672010-03-26 07:18:04 +00001516 Out << "Dv" << T->getNumElements() << '_';
Bob Wilsone86d78c2010-11-10 21:56:12 +00001517 if (T->getVectorKind() == VectorType::AltiVecPixel)
Chris Lattner788b0fd2010-06-23 06:00:24 +00001518 Out << 'p';
Bob Wilsone86d78c2010-11-10 21:56:12 +00001519 else if (T->getVectorKind() == VectorType::AltiVecBool)
Chris Lattner788b0fd2010-06-23 06:00:24 +00001520 Out << 'b';
1521 else
1522 mangleType(T->getElementType());
John McCallefe6aee2009-09-05 07:56:18 +00001523}
1524void CXXNameMangler::mangleType(const ExtVectorType *T) {
1525 mangleType(static_cast<const VectorType*>(T));
1526}
1527void CXXNameMangler::mangleType(const DependentSizedExtVectorType *T) {
Nick Lewycky0e5f0672010-03-26 07:18:04 +00001528 Out << "Dv";
1529 mangleExpression(T->getSizeExpr());
1530 Out << '_';
John McCallefe6aee2009-09-05 07:56:18 +00001531 mangleType(T->getElementType());
1532}
1533
Douglas Gregor7536dd52010-12-20 02:24:11 +00001534void CXXNameMangler::mangleType(const PackExpansionType *T) {
Douglas Gregor4fc48662011-01-13 16:39:34 +00001535 // <type> ::= Dp <type> # pack expansion (C++0x)
Douglas Gregor255c2692011-01-13 17:44:36 +00001536 Out << "Dp";
Douglas Gregor7536dd52010-12-20 02:24:11 +00001537 mangleType(T->getPattern());
1538}
1539
Anders Carlssona40c5e42009-03-07 22:03:21 +00001540void CXXNameMangler::mangleType(const ObjCInterfaceType *T) {
1541 mangleSourceName(T->getDecl()->getIdentifier());
1542}
1543
John McCallc12c5bb2010-05-15 11:32:37 +00001544void CXXNameMangler::mangleType(const ObjCObjectType *T) {
John McCallc00c1f62010-05-15 17:06:29 +00001545 // We don't allow overloading by different protocol qualification,
1546 // so mangling them isn't necessary.
John McCallc12c5bb2010-05-15 11:32:37 +00001547 mangleType(T->getBaseType());
1548}
1549
John McCallefe6aee2009-09-05 07:56:18 +00001550void CXXNameMangler::mangleType(const BlockPointerType *T) {
Anders Carlssonf28c6872009-12-23 22:31:44 +00001551 Out << "U13block_pointer";
1552 mangleType(T->getPointeeType());
John McCallefe6aee2009-09-05 07:56:18 +00001553}
1554
John McCall31f17ec2010-04-27 00:57:59 +00001555void CXXNameMangler::mangleType(const InjectedClassNameType *T) {
1556 // Mangle injected class name types as if the user had written the
1557 // specialization out fully. It may not actually be possible to see
1558 // this mangling, though.
1559 mangleType(T->getInjectedSpecializationType());
1560}
1561
John McCallefe6aee2009-09-05 07:56:18 +00001562void CXXNameMangler::mangleType(const TemplateSpecializationType *T) {
Douglas Gregor1e9268e2010-04-28 05:58:56 +00001563 if (TemplateDecl *TD = T->getTemplateName().getAsTemplateDecl()) {
1564 mangleName(TD, T->getArgs(), T->getNumArgs());
1565 } else {
1566 if (mangleSubstitution(QualType(T, 0)))
1567 return;
Sean Huntc3021132010-05-05 15:23:54 +00001568
Douglas Gregor1e9268e2010-04-28 05:58:56 +00001569 mangleTemplatePrefix(T->getTemplateName());
Sean Huntc3021132010-05-05 15:23:54 +00001570
Douglas Gregor1e9268e2010-04-28 05:58:56 +00001571 // FIXME: GCC does not appear to mangle the template arguments when
1572 // the template in question is a dependent template name. Should we
1573 // emulate that badness?
1574 mangleTemplateArgs(T->getTemplateName(), T->getArgs(), T->getNumArgs());
1575 addSubstitution(QualType(T, 0));
1576 }
John McCallefe6aee2009-09-05 07:56:18 +00001577}
1578
Douglas Gregor4714c122010-03-31 17:34:00 +00001579void CXXNameMangler::mangleType(const DependentNameType *T) {
Anders Carlssonae352482009-09-26 02:26:02 +00001580 // Typename types are always nested
1581 Out << 'N';
John McCall33500952010-06-11 00:33:02 +00001582 mangleUnresolvedScope(T->getQualifier());
1583 mangleSourceName(T->getIdentifier());
1584 Out << 'E';
1585}
John McCall6ab30e02010-06-09 07:26:17 +00001586
John McCall33500952010-06-11 00:33:02 +00001587void CXXNameMangler::mangleType(const DependentTemplateSpecializationType *T) {
1588 // Dependently-scoped template types are always nested
1589 Out << 'N';
1590
1591 // TODO: avoid making this TemplateName.
1592 TemplateName Prefix =
1593 getASTContext().getDependentTemplateName(T->getQualifier(),
1594 T->getIdentifier());
1595 mangleTemplatePrefix(Prefix);
1596
1597 // FIXME: GCC does not appear to mangle the template arguments when
1598 // the template in question is a dependent template name. Should we
1599 // emulate that badness?
1600 mangleTemplateArgs(Prefix, T->getArgs(), T->getNumArgs());
Anders Carlssonae352482009-09-26 02:26:02 +00001601 Out << 'E';
John McCallefe6aee2009-09-05 07:56:18 +00001602}
1603
John McCallad5e7382010-03-01 23:49:17 +00001604void CXXNameMangler::mangleType(const TypeOfType *T) {
1605 // FIXME: this is pretty unsatisfactory, but there isn't an obvious
1606 // "extension with parameters" mangling.
1607 Out << "u6typeof";
1608}
1609
1610void CXXNameMangler::mangleType(const TypeOfExprType *T) {
1611 // FIXME: this is pretty unsatisfactory, but there isn't an obvious
1612 // "extension with parameters" mangling.
1613 Out << "u6typeof";
1614}
1615
1616void CXXNameMangler::mangleType(const DecltypeType *T) {
1617 Expr *E = T->getUnderlyingExpr();
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001618
John McCallad5e7382010-03-01 23:49:17 +00001619 // type ::= Dt <expression> E # decltype of an id-expression
1620 // # or class member access
1621 // ::= DT <expression> E # decltype of an expression
1622
1623 // This purports to be an exhaustive list of id-expressions and
1624 // class member accesses. Note that we do not ignore parentheses;
1625 // parentheses change the semantics of decltype for these
1626 // expressions (and cause the mangler to use the other form).
1627 if (isa<DeclRefExpr>(E) ||
1628 isa<MemberExpr>(E) ||
1629 isa<UnresolvedLookupExpr>(E) ||
1630 isa<DependentScopeDeclRefExpr>(E) ||
1631 isa<CXXDependentScopeMemberExpr>(E) ||
1632 isa<UnresolvedMemberExpr>(E))
1633 Out << "Dt";
1634 else
1635 Out << "DT";
1636 mangleExpression(E);
1637 Out << 'E';
1638}
1639
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001640void CXXNameMangler::mangleIntegerLiteral(QualType T,
Anders Carlssone170ba72009-12-14 01:45:37 +00001641 const llvm::APSInt &Value) {
1642 // <expr-primary> ::= L <type> <value number> E # integer literal
1643 Out << 'L';
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001644
Anders Carlssone170ba72009-12-14 01:45:37 +00001645 mangleType(T);
1646 if (T->isBooleanType()) {
1647 // Boolean values are encoded as 0/1.
1648 Out << (Value.getBoolValue() ? '1' : '0');
1649 } else {
John McCall0512e482010-07-14 04:20:34 +00001650 mangleNumber(Value);
Anders Carlssone170ba72009-12-14 01:45:37 +00001651 }
1652 Out << 'E';
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001653
Anders Carlssone170ba72009-12-14 01:45:37 +00001654}
1655
John McCall2f27bf82010-02-04 02:56:29 +00001656/// Mangles a member expression. Implicit accesses are not handled,
1657/// but that should be okay, because you shouldn't be able to
1658/// make an implicit access in a function template declaration.
John McCall2f27bf82010-02-04 02:56:29 +00001659void CXXNameMangler::mangleMemberExpr(const Expr *Base,
1660 bool IsArrow,
1661 NestedNameSpecifier *Qualifier,
1662 DeclarationName Member,
1663 unsigned Arity) {
John McCalle1e342f2010-03-01 19:12:25 +00001664 // gcc-4.4 uses 'dt' for dot expressions, which is reasonable.
1665 // OTOH, gcc also mangles the name as an expression.
1666 Out << (IsArrow ? "pt" : "dt");
John McCall2f27bf82010-02-04 02:56:29 +00001667 mangleExpression(Base);
1668 mangleUnresolvedName(Qualifier, Member, Arity);
1669}
1670
John McCall5e1e89b2010-08-18 19:18:59 +00001671void CXXNameMangler::mangleExpression(const Expr *E, unsigned Arity) {
Anders Carlssond553f8c2009-09-21 01:21:10 +00001672 // <expression> ::= <unary operator-name> <expression>
John McCall09cc1412010-02-03 00:55:45 +00001673 // ::= <binary operator-name> <expression> <expression>
1674 // ::= <trinary operator-name> <expression> <expression> <expression>
Eli Friedmana7e68452010-08-22 01:00:03 +00001675 // ::= cl <expression>* E # call
Anders Carlssond553f8c2009-09-21 01:21:10 +00001676 // ::= cv <type> expression # conversion with one argument
1677 // ::= cv <type> _ <expression>* E # conversion with a different number of arguments
Eli Friedmana7e68452010-08-22 01:00:03 +00001678 // ::= st <type> # sizeof (a type)
Anders Carlssond553f8c2009-09-21 01:21:10 +00001679 // ::= at <type> # alignof (a type)
1680 // ::= <template-param>
1681 // ::= <function-param>
1682 // ::= sr <type> <unqualified-name> # dependent name
1683 // ::= sr <type> <unqualified-name> <template-args> # dependent template-id
1684 // ::= sZ <template-param> # size of a parameter pack
Douglas Gregor4fc48662011-01-13 16:39:34 +00001685 // ::= sZ <function-param> # size of a function parameter pack
John McCall09cc1412010-02-03 00:55:45 +00001686 // ::= <expr-primary>
John McCall1dd73832010-02-04 01:42:13 +00001687 // <expr-primary> ::= L <type> <value number> E # integer literal
1688 // ::= L <type <value float> E # floating literal
1689 // ::= L <mangled-name> E # external name
Anders Carlssond553f8c2009-09-21 01:21:10 +00001690 switch (E->getStmtClass()) {
John McCall6ae1f352010-04-09 22:26:14 +00001691 case Expr::NoStmtClass:
John McCall63c00d72011-02-09 08:16:59 +00001692#define ABSTRACT_STMT(Type)
John McCall6ae1f352010-04-09 22:26:14 +00001693#define EXPR(Type, Base)
1694#define STMT(Type, Base) \
1695 case Expr::Type##Class:
Sean Hunt4bfe1962010-05-05 15:24:00 +00001696#include "clang/AST/StmtNodes.inc"
John McCall0512e482010-07-14 04:20:34 +00001697 // fallthrough
1698
1699 // These all can only appear in local or variable-initialization
1700 // contexts and so should never appear in a mangling.
1701 case Expr::AddrLabelExprClass:
1702 case Expr::BlockDeclRefExprClass:
1703 case Expr::CXXThisExprClass:
1704 case Expr::DesignatedInitExprClass:
1705 case Expr::ImplicitValueInitExprClass:
1706 case Expr::InitListExprClass:
1707 case Expr::ParenListExprClass:
1708 case Expr::CXXScalarValueInitExprClass:
John McCall09cc1412010-02-03 00:55:45 +00001709 llvm_unreachable("unexpected statement kind");
1710 break;
1711
John McCall0512e482010-07-14 04:20:34 +00001712 // FIXME: invent manglings for all these.
1713 case Expr::BlockExprClass:
1714 case Expr::CXXPseudoDestructorExprClass:
1715 case Expr::ChooseExprClass:
1716 case Expr::CompoundLiteralExprClass:
1717 case Expr::ExtVectorElementExprClass:
1718 case Expr::ObjCEncodeExprClass:
John McCall0512e482010-07-14 04:20:34 +00001719 case Expr::ObjCIsaExprClass:
1720 case Expr::ObjCIvarRefExprClass:
1721 case Expr::ObjCMessageExprClass:
1722 case Expr::ObjCPropertyRefExprClass:
1723 case Expr::ObjCProtocolExprClass:
1724 case Expr::ObjCSelectorExprClass:
1725 case Expr::ObjCStringLiteralClass:
John McCall0512e482010-07-14 04:20:34 +00001726 case Expr::OffsetOfExprClass:
1727 case Expr::PredefinedExprClass:
1728 case Expr::ShuffleVectorExprClass:
1729 case Expr::StmtExprClass:
John McCall0512e482010-07-14 04:20:34 +00001730 case Expr::UnaryTypeTraitExprClass:
Francois Pichet6ad6f282010-12-07 00:08:36 +00001731 case Expr::BinaryTypeTraitExprClass:
Francois Pichet9be88402010-09-08 23:47:05 +00001732 case Expr::VAArgExprClass:
Sebastian Redl2e156222010-09-10 20:55:43 +00001733 case Expr::CXXUuidofExprClass:
Peter Collingbournee08ce652011-02-09 21:07:24 +00001734 case Expr::CXXNoexceptExprClass:
1735 case Expr::CUDAKernelCallExprClass: {
John McCall6ae1f352010-04-09 22:26:14 +00001736 // As bad as this diagnostic is, it's better than crashing.
1737 Diagnostic &Diags = Context.getDiags();
1738 unsigned DiagID = Diags.getCustomDiagID(Diagnostic::Error,
1739 "cannot yet mangle expression type %0");
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +00001740 Diags.Report(E->getExprLoc(), DiagID)
John McCall739bf092010-04-10 09:39:25 +00001741 << E->getStmtClassName() << E->getSourceRange();
John McCall6ae1f352010-04-09 22:26:14 +00001742 break;
1743 }
1744
John McCall7cd7d1a2010-11-15 23:31:06 +00001745 case Expr::OpaqueValueExprClass:
1746 llvm_unreachable("cannot mangle opaque value; mangling wrong thing?");
1747
John McCall0512e482010-07-14 04:20:34 +00001748 case Expr::CXXDefaultArgExprClass:
John McCall5e1e89b2010-08-18 19:18:59 +00001749 mangleExpression(cast<CXXDefaultArgExpr>(E)->getExpr(), Arity);
John McCall0512e482010-07-14 04:20:34 +00001750 break;
1751
1752 case Expr::CXXMemberCallExprClass: // fallthrough
John McCall1dd73832010-02-04 01:42:13 +00001753 case Expr::CallExprClass: {
1754 const CallExpr *CE = cast<CallExpr>(E);
1755 Out << "cl";
John McCall5e1e89b2010-08-18 19:18:59 +00001756 mangleExpression(CE->getCallee(), CE->getNumArgs());
John McCall1dd73832010-02-04 01:42:13 +00001757 for (unsigned I = 0, N = CE->getNumArgs(); I != N; ++I)
1758 mangleExpression(CE->getArg(I));
Benjamin Kramer35f59b62010-04-10 16:03:31 +00001759 Out << 'E';
John McCall09cc1412010-02-03 00:55:45 +00001760 break;
John McCall1dd73832010-02-04 01:42:13 +00001761 }
John McCall09cc1412010-02-03 00:55:45 +00001762
John McCall0512e482010-07-14 04:20:34 +00001763 case Expr::CXXNewExprClass: {
1764 // Proposal from David Vandervoorde, 2010.06.30
1765 const CXXNewExpr *New = cast<CXXNewExpr>(E);
1766 if (New->isGlobalNew()) Out << "gs";
1767 Out << (New->isArray() ? "na" : "nw");
1768 for (CXXNewExpr::const_arg_iterator I = New->placement_arg_begin(),
1769 E = New->placement_arg_end(); I != E; ++I)
1770 mangleExpression(*I);
1771 Out << '_';
1772 mangleType(New->getAllocatedType());
1773 if (New->hasInitializer()) {
1774 Out << "pi";
1775 for (CXXNewExpr::const_arg_iterator I = New->constructor_arg_begin(),
1776 E = New->constructor_arg_end(); I != E; ++I)
1777 mangleExpression(*I);
1778 }
1779 Out << 'E';
1780 break;
1781 }
1782
John McCall2f27bf82010-02-04 02:56:29 +00001783 case Expr::MemberExprClass: {
1784 const MemberExpr *ME = cast<MemberExpr>(E);
1785 mangleMemberExpr(ME->getBase(), ME->isArrow(),
1786 ME->getQualifier(), ME->getMemberDecl()->getDeclName(),
John McCall5e1e89b2010-08-18 19:18:59 +00001787 Arity);
John McCall2f27bf82010-02-04 02:56:29 +00001788 break;
1789 }
1790
1791 case Expr::UnresolvedMemberExprClass: {
1792 const UnresolvedMemberExpr *ME = cast<UnresolvedMemberExpr>(E);
1793 mangleMemberExpr(ME->getBase(), ME->isArrow(),
1794 ME->getQualifier(), ME->getMemberName(),
John McCall5e1e89b2010-08-18 19:18:59 +00001795 Arity);
John McCall6dbce192010-08-20 00:17:19 +00001796 if (ME->hasExplicitTemplateArgs())
1797 mangleTemplateArgs(ME->getExplicitTemplateArgs());
John McCall2f27bf82010-02-04 02:56:29 +00001798 break;
1799 }
1800
1801 case Expr::CXXDependentScopeMemberExprClass: {
1802 const CXXDependentScopeMemberExpr *ME
1803 = cast<CXXDependentScopeMemberExpr>(E);
1804 mangleMemberExpr(ME->getBase(), ME->isArrow(),
1805 ME->getQualifier(), ME->getMember(),
John McCall5e1e89b2010-08-18 19:18:59 +00001806 Arity);
John McCall6dbce192010-08-20 00:17:19 +00001807 if (ME->hasExplicitTemplateArgs())
1808 mangleTemplateArgs(ME->getExplicitTemplateArgs());
John McCall2f27bf82010-02-04 02:56:29 +00001809 break;
1810 }
1811
John McCall1dd73832010-02-04 01:42:13 +00001812 case Expr::UnresolvedLookupExprClass: {
John McCalla3218e72010-02-04 01:48:38 +00001813 // The ABI doesn't cover how to mangle overload sets, so we mangle
1814 // using something as close as possible to the original lookup
1815 // expression.
John McCall1dd73832010-02-04 01:42:13 +00001816 const UnresolvedLookupExpr *ULE = cast<UnresolvedLookupExpr>(E);
John McCall5e1e89b2010-08-18 19:18:59 +00001817 mangleUnresolvedName(ULE->getQualifier(), ULE->getName(), Arity);
John McCall6dbce192010-08-20 00:17:19 +00001818 if (ULE->hasExplicitTemplateArgs())
1819 mangleTemplateArgs(ULE->getExplicitTemplateArgs());
John McCall1dd73832010-02-04 01:42:13 +00001820 break;
1821 }
1822
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001823 case Expr::CXXUnresolvedConstructExprClass: {
John McCall1dd73832010-02-04 01:42:13 +00001824 const CXXUnresolvedConstructExpr *CE = cast<CXXUnresolvedConstructExpr>(E);
1825 unsigned N = CE->arg_size();
1826
1827 Out << "cv";
1828 mangleType(CE->getType());
Benjamin Kramer35f59b62010-04-10 16:03:31 +00001829 if (N != 1) Out << '_';
John McCall1dd73832010-02-04 01:42:13 +00001830 for (unsigned I = 0; I != N; ++I) mangleExpression(CE->getArg(I));
Benjamin Kramer35f59b62010-04-10 16:03:31 +00001831 if (N != 1) Out << 'E';
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001832 break;
John McCall1dd73832010-02-04 01:42:13 +00001833 }
John McCall09cc1412010-02-03 00:55:45 +00001834
John McCall1dd73832010-02-04 01:42:13 +00001835 case Expr::CXXTemporaryObjectExprClass:
1836 case Expr::CXXConstructExprClass: {
1837 const CXXConstructExpr *CE = cast<CXXConstructExpr>(E);
1838 unsigned N = CE->getNumArgs();
1839
1840 Out << "cv";
1841 mangleType(CE->getType());
Benjamin Kramer35f59b62010-04-10 16:03:31 +00001842 if (N != 1) Out << '_';
John McCall1dd73832010-02-04 01:42:13 +00001843 for (unsigned I = 0; I != N; ++I) mangleExpression(CE->getArg(I));
Benjamin Kramer35f59b62010-04-10 16:03:31 +00001844 if (N != 1) Out << 'E';
John McCall09cc1412010-02-03 00:55:45 +00001845 break;
John McCall1dd73832010-02-04 01:42:13 +00001846 }
1847
1848 case Expr::SizeOfAlignOfExprClass: {
1849 const SizeOfAlignOfExpr *SAE = cast<SizeOfAlignOfExpr>(E);
Benjamin Kramer35f59b62010-04-10 16:03:31 +00001850 if (SAE->isSizeOf()) Out << 's';
1851 else Out << 'a';
John McCall1dd73832010-02-04 01:42:13 +00001852 if (SAE->isArgumentType()) {
Benjamin Kramer35f59b62010-04-10 16:03:31 +00001853 Out << 't';
John McCall1dd73832010-02-04 01:42:13 +00001854 mangleType(SAE->getArgumentType());
1855 } else {
Benjamin Kramer35f59b62010-04-10 16:03:31 +00001856 Out << 'z';
John McCall1dd73832010-02-04 01:42:13 +00001857 mangleExpression(SAE->getArgumentExpr());
1858 }
1859 break;
1860 }
Anders Carlssona7694082009-11-06 02:50:19 +00001861
John McCall0512e482010-07-14 04:20:34 +00001862 case Expr::CXXThrowExprClass: {
1863 const CXXThrowExpr *TE = cast<CXXThrowExpr>(E);
1864
1865 // Proposal from David Vandervoorde, 2010.06.30
1866 if (TE->getSubExpr()) {
1867 Out << "tw";
1868 mangleExpression(TE->getSubExpr());
1869 } else {
1870 Out << "tr";
1871 }
1872 break;
1873 }
1874
1875 case Expr::CXXTypeidExprClass: {
1876 const CXXTypeidExpr *TIE = cast<CXXTypeidExpr>(E);
1877
1878 // Proposal from David Vandervoorde, 2010.06.30
1879 if (TIE->isTypeOperand()) {
1880 Out << "ti";
1881 mangleType(TIE->getTypeOperand());
1882 } else {
1883 Out << "te";
1884 mangleExpression(TIE->getExprOperand());
1885 }
1886 break;
1887 }
1888
1889 case Expr::CXXDeleteExprClass: {
1890 const CXXDeleteExpr *DE = cast<CXXDeleteExpr>(E);
1891
1892 // Proposal from David Vandervoorde, 2010.06.30
1893 if (DE->isGlobalDelete()) Out << "gs";
1894 Out << (DE->isArrayForm() ? "da" : "dl");
1895 mangleExpression(DE->getArgument());
1896 break;
1897 }
1898
Anders Carlssone170ba72009-12-14 01:45:37 +00001899 case Expr::UnaryOperatorClass: {
1900 const UnaryOperator *UO = cast<UnaryOperator>(E);
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001901 mangleOperatorName(UnaryOperator::getOverloadedOperator(UO->getOpcode()),
Anders Carlssone170ba72009-12-14 01:45:37 +00001902 /*Arity=*/1);
1903 mangleExpression(UO->getSubExpr());
1904 break;
1905 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001906
John McCall0512e482010-07-14 04:20:34 +00001907 case Expr::ArraySubscriptExprClass: {
1908 const ArraySubscriptExpr *AE = cast<ArraySubscriptExpr>(E);
1909
1910 // Array subscript is treated as a syntactically wierd form of
1911 // binary operator.
1912 Out << "ix";
1913 mangleExpression(AE->getLHS());
1914 mangleExpression(AE->getRHS());
1915 break;
1916 }
1917
1918 case Expr::CompoundAssignOperatorClass: // fallthrough
Anders Carlssone170ba72009-12-14 01:45:37 +00001919 case Expr::BinaryOperatorClass: {
1920 const BinaryOperator *BO = cast<BinaryOperator>(E);
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001921 mangleOperatorName(BinaryOperator::getOverloadedOperator(BO->getOpcode()),
Anders Carlssone170ba72009-12-14 01:45:37 +00001922 /*Arity=*/2);
1923 mangleExpression(BO->getLHS());
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001924 mangleExpression(BO->getRHS());
Anders Carlssone170ba72009-12-14 01:45:37 +00001925 break;
John McCall2f27bf82010-02-04 02:56:29 +00001926 }
Anders Carlssone170ba72009-12-14 01:45:37 +00001927
1928 case Expr::ConditionalOperatorClass: {
1929 const ConditionalOperator *CO = cast<ConditionalOperator>(E);
1930 mangleOperatorName(OO_Conditional, /*Arity=*/3);
1931 mangleExpression(CO->getCond());
John McCall5e1e89b2010-08-18 19:18:59 +00001932 mangleExpression(CO->getLHS(), Arity);
1933 mangleExpression(CO->getRHS(), Arity);
Anders Carlssone170ba72009-12-14 01:45:37 +00001934 break;
1935 }
1936
Douglas Gregor46287c72010-01-29 16:37:09 +00001937 case Expr::ImplicitCastExprClass: {
John McCall5e1e89b2010-08-18 19:18:59 +00001938 mangleExpression(cast<ImplicitCastExpr>(E)->getSubExpr(), Arity);
Douglas Gregor46287c72010-01-29 16:37:09 +00001939 break;
1940 }
1941
1942 case Expr::CStyleCastExprClass:
1943 case Expr::CXXStaticCastExprClass:
1944 case Expr::CXXDynamicCastExprClass:
1945 case Expr::CXXReinterpretCastExprClass:
1946 case Expr::CXXConstCastExprClass:
1947 case Expr::CXXFunctionalCastExprClass: {
1948 const ExplicitCastExpr *ECE = cast<ExplicitCastExpr>(E);
1949 Out << "cv";
1950 mangleType(ECE->getType());
1951 mangleExpression(ECE->getSubExpr());
1952 break;
1953 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001954
Anders Carlsson58040a52009-12-16 05:48:46 +00001955 case Expr::CXXOperatorCallExprClass: {
1956 const CXXOperatorCallExpr *CE = cast<CXXOperatorCallExpr>(E);
1957 unsigned NumArgs = CE->getNumArgs();
1958 mangleOperatorName(CE->getOperator(), /*Arity=*/NumArgs);
1959 // Mangle the arguments.
1960 for (unsigned i = 0; i != NumArgs; ++i)
1961 mangleExpression(CE->getArg(i));
1962 break;
1963 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001964
Anders Carlssona7694082009-11-06 02:50:19 +00001965 case Expr::ParenExprClass:
John McCall5e1e89b2010-08-18 19:18:59 +00001966 mangleExpression(cast<ParenExpr>(E)->getSubExpr(), Arity);
Anders Carlssona7694082009-11-06 02:50:19 +00001967 break;
1968
Anders Carlssond553f8c2009-09-21 01:21:10 +00001969 case Expr::DeclRefExprClass: {
Douglas Gregor5ed1bc32010-02-28 21:40:32 +00001970 const NamedDecl *D = cast<DeclRefExpr>(E)->getDecl();
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00001971
Anders Carlssond553f8c2009-09-21 01:21:10 +00001972 switch (D->getKind()) {
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001973 default:
Douglas Gregor5ed1bc32010-02-28 21:40:32 +00001974 // <expr-primary> ::= L <mangled-name> E # external name
1975 Out << 'L';
1976 mangle(D, "_Z");
1977 Out << 'E';
1978 break;
1979
John McCall3dc7e7b2010-07-24 01:17:35 +00001980 case Decl::EnumConstant: {
1981 const EnumConstantDecl *ED = cast<EnumConstantDecl>(D);
1982 mangleIntegerLiteral(ED->getType(), ED->getInitVal());
1983 break;
1984 }
1985
Anders Carlssond553f8c2009-09-21 01:21:10 +00001986 case Decl::NonTypeTemplateParm: {
1987 const NonTypeTemplateParmDecl *PD = cast<NonTypeTemplateParmDecl>(D);
Anders Carlsson0ccdf8d2009-09-27 00:38:53 +00001988 mangleTemplateParameter(PD->getIndex());
Anders Carlssond553f8c2009-09-21 01:21:10 +00001989 break;
1990 }
1991
1992 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00001993
Anders Carlsson50755b02009-09-27 20:11:34 +00001994 break;
Anders Carlssond553f8c2009-09-21 01:21:10 +00001995 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00001996
Douglas Gregorc7793c72011-01-15 01:15:58 +00001997 case Expr::SubstNonTypeTemplateParmPackExprClass:
1998 mangleTemplateParameter(
1999 cast<SubstNonTypeTemplateParmPackExpr>(E)->getParameterPack()->getIndex());
2000 break;
2001
John McCall865d4472009-11-19 22:55:06 +00002002 case Expr::DependentScopeDeclRefExprClass: {
2003 const DependentScopeDeclRefExpr *DRE = cast<DependentScopeDeclRefExpr>(E);
Douglas Gregor4b2ccfc2010-02-28 22:05:49 +00002004 NestedNameSpecifier *NNS = DRE->getQualifier();
2005 const Type *QTy = NNS->getAsType();
2006
2007 // When we're dealing with a nested-name-specifier that has just a
2008 // dependent identifier in it, mangle that as a typename. FIXME:
2009 // It isn't clear that we ever actually want to have such a
2010 // nested-name-specifier; why not just represent it as a typename type?
2011 if (!QTy && NNS->getAsIdentifier() && NNS->getPrefix()) {
Douglas Gregor4a2023f2010-03-31 20:19:30 +00002012 QTy = getASTContext().getDependentNameType(ETK_Typename,
2013 NNS->getPrefix(),
2014 NNS->getAsIdentifier())
Douglas Gregor4b2ccfc2010-02-28 22:05:49 +00002015 .getTypePtr();
2016 }
Anders Carlsson50755b02009-09-27 20:11:34 +00002017 assert(QTy && "Qualifier was not type!");
2018
John McCall6dbce192010-08-20 00:17:19 +00002019 // ::= sr <type> <unqualified-name> # dependent name
2020 // ::= sr <type> <unqualified-name> <template-args> # dependent template-id
Anders Carlsson50755b02009-09-27 20:11:34 +00002021 Out << "sr";
2022 mangleType(QualType(QTy, 0));
John McCall5e1e89b2010-08-18 19:18:59 +00002023 mangleUnqualifiedName(0, DRE->getDeclName(), Arity);
John McCall6dbce192010-08-20 00:17:19 +00002024 if (DRE->hasExplicitTemplateArgs())
2025 mangleTemplateArgs(DRE->getExplicitTemplateArgs());
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002026
Anders Carlsson50755b02009-09-27 20:11:34 +00002027 break;
2028 }
2029
John McCalld9307602010-04-09 22:54:09 +00002030 case Expr::CXXBindTemporaryExprClass:
2031 mangleExpression(cast<CXXBindTemporaryExpr>(E)->getSubExpr());
2032 break;
2033
John McCall4765fa02010-12-06 08:20:24 +00002034 case Expr::ExprWithCleanupsClass:
2035 mangleExpression(cast<ExprWithCleanups>(E)->getSubExpr(), Arity);
John McCalld9307602010-04-09 22:54:09 +00002036 break;
2037
John McCall1dd73832010-02-04 01:42:13 +00002038 case Expr::FloatingLiteralClass: {
2039 const FloatingLiteral *FL = cast<FloatingLiteral>(E);
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002040 Out << 'L';
John McCall1dd73832010-02-04 01:42:13 +00002041 mangleType(FL->getType());
John McCall0512e482010-07-14 04:20:34 +00002042 mangleFloat(FL->getValue());
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002043 Out << 'E';
John McCall1dd73832010-02-04 01:42:13 +00002044 break;
2045 }
2046
John McCallde810632010-04-09 21:48:08 +00002047 case Expr::CharacterLiteralClass:
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002048 Out << 'L';
John McCallde810632010-04-09 21:48:08 +00002049 mangleType(E->getType());
2050 Out << cast<CharacterLiteral>(E)->getValue();
2051 Out << 'E';
2052 break;
2053
2054 case Expr::CXXBoolLiteralExprClass:
2055 Out << "Lb";
2056 Out << (cast<CXXBoolLiteralExpr>(E)->getValue() ? '1' : '0');
2057 Out << 'E';
2058 break;
2059
John McCall0512e482010-07-14 04:20:34 +00002060 case Expr::IntegerLiteralClass: {
2061 llvm::APSInt Value(cast<IntegerLiteral>(E)->getValue());
2062 if (E->getType()->isSignedIntegerType())
2063 Value.setIsSigned(true);
2064 mangleIntegerLiteral(E->getType(), Value);
Anders Carlssone170ba72009-12-14 01:45:37 +00002065 break;
John McCall0512e482010-07-14 04:20:34 +00002066 }
2067
2068 case Expr::ImaginaryLiteralClass: {
2069 const ImaginaryLiteral *IE = cast<ImaginaryLiteral>(E);
2070 // Mangle as if a complex literal.
Nick Lewycky271b6652010-09-05 03:40:33 +00002071 // Proposal from David Vandevoorde, 2010.06.30.
John McCall0512e482010-07-14 04:20:34 +00002072 Out << 'L';
2073 mangleType(E->getType());
2074 if (const FloatingLiteral *Imag =
2075 dyn_cast<FloatingLiteral>(IE->getSubExpr())) {
2076 // Mangle a floating-point zero of the appropriate type.
2077 mangleFloat(llvm::APFloat(Imag->getValue().getSemantics()));
2078 Out << '_';
2079 mangleFloat(Imag->getValue());
2080 } else {
Nick Lewycky271b6652010-09-05 03:40:33 +00002081 Out << "0_";
John McCall0512e482010-07-14 04:20:34 +00002082 llvm::APSInt Value(cast<IntegerLiteral>(IE->getSubExpr())->getValue());
2083 if (IE->getSubExpr()->getType()->isSignedIntegerType())
2084 Value.setIsSigned(true);
2085 mangleNumber(Value);
2086 }
2087 Out << 'E';
2088 break;
2089 }
2090
2091 case Expr::StringLiteralClass: {
John McCall1658c392010-07-15 21:53:03 +00002092 // Revised proposal from David Vandervoorde, 2010.07.15.
John McCall0512e482010-07-14 04:20:34 +00002093 Out << 'L';
John McCall1658c392010-07-15 21:53:03 +00002094 assert(isa<ConstantArrayType>(E->getType()));
2095 mangleType(E->getType());
John McCall0512e482010-07-14 04:20:34 +00002096 Out << 'E';
2097 break;
2098 }
2099
2100 case Expr::GNUNullExprClass:
2101 // FIXME: should this really be mangled the same as nullptr?
2102 // fallthrough
2103
2104 case Expr::CXXNullPtrLiteralExprClass: {
2105 // Proposal from David Vandervoorde, 2010.06.30, as
2106 // modified by ABI list discussion.
2107 Out << "LDnE";
2108 break;
2109 }
Douglas Gregorbe230c32011-01-03 17:17:50 +00002110
2111 case Expr::PackExpansionExprClass:
2112 Out << "sp";
2113 mangleExpression(cast<PackExpansionExpr>(E)->getPattern());
2114 break;
Douglas Gregor2e774c42011-01-04 18:56:13 +00002115
2116 case Expr::SizeOfPackExprClass: {
Douglas Gregor2e774c42011-01-04 18:56:13 +00002117 Out << "sZ";
2118 const NamedDecl *Pack = cast<SizeOfPackExpr>(E)->getPack();
2119 if (const TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(Pack))
2120 mangleTemplateParameter(TTP->getIndex());
2121 else if (const NonTypeTemplateParmDecl *NTTP
2122 = dyn_cast<NonTypeTemplateParmDecl>(Pack))
2123 mangleTemplateParameter(NTTP->getIndex());
2124 else if (const TemplateTemplateParmDecl *TempTP
2125 = dyn_cast<TemplateTemplateParmDecl>(Pack))
2126 mangleTemplateParameter(TempTP->getIndex());
2127 else {
Douglas Gregor4fc48662011-01-13 16:39:34 +00002128 // Note: proposed by Mike Herrick on 11/30/10
2129 // <expression> ::= sZ <function-param> # size of function parameter pack
Douglas Gregor2e774c42011-01-04 18:56:13 +00002130 Diagnostic &Diags = Context.getDiags();
2131 unsigned DiagID = Diags.getCustomDiagID(Diagnostic::Error,
2132 "cannot mangle sizeof...(function parameter pack)");
2133 Diags.Report(DiagID);
2134 return;
2135 }
2136 }
Anders Carlssond553f8c2009-09-21 01:21:10 +00002137 }
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00002138}
2139
Anders Carlsson3ac86b52009-04-15 05:36:58 +00002140void CXXNameMangler::mangleCXXCtorType(CXXCtorType T) {
2141 // <ctor-dtor-name> ::= C1 # complete object constructor
2142 // ::= C2 # base object constructor
2143 // ::= C3 # complete object allocating constructor
2144 //
2145 switch (T) {
2146 case Ctor_Complete:
2147 Out << "C1";
2148 break;
2149 case Ctor_Base:
2150 Out << "C2";
2151 break;
2152 case Ctor_CompleteAllocating:
2153 Out << "C3";
2154 break;
2155 }
2156}
2157
Anders Carlsson27ae5362009-04-17 01:58:57 +00002158void CXXNameMangler::mangleCXXDtorType(CXXDtorType T) {
2159 // <ctor-dtor-name> ::= D0 # deleting destructor
2160 // ::= D1 # complete object destructor
2161 // ::= D2 # base object destructor
2162 //
2163 switch (T) {
2164 case Dtor_Deleting:
2165 Out << "D0";
2166 break;
2167 case Dtor_Complete:
2168 Out << "D1";
2169 break;
2170 case Dtor_Base:
2171 Out << "D2";
2172 break;
2173 }
2174}
2175
John McCall6dbce192010-08-20 00:17:19 +00002176void CXXNameMangler::mangleTemplateArgs(
2177 const ExplicitTemplateArgumentList &TemplateArgs) {
2178 // <template-args> ::= I <template-arg>+ E
2179 Out << 'I';
2180 for (unsigned I = 0, E = TemplateArgs.NumTemplateArgs; I != E; ++I)
2181 mangleTemplateArg(0, TemplateArgs.getTemplateArgs()[I].getArgument());
2182 Out << 'E';
2183}
2184
Douglas Gregor20f0cc72010-04-23 03:10:43 +00002185void CXXNameMangler::mangleTemplateArgs(TemplateName Template,
2186 const TemplateArgument *TemplateArgs,
2187 unsigned NumTemplateArgs) {
2188 if (TemplateDecl *TD = Template.getAsTemplateDecl())
2189 return mangleTemplateArgs(*TD->getTemplateParameters(), TemplateArgs,
2190 NumTemplateArgs);
Sean Huntc3021132010-05-05 15:23:54 +00002191
Douglas Gregor20f0cc72010-04-23 03:10:43 +00002192 // <template-args> ::= I <template-arg>+ E
2193 Out << 'I';
2194 for (unsigned i = 0; i != NumTemplateArgs; ++i)
2195 mangleTemplateArg(0, TemplateArgs[i]);
2196 Out << 'E';
2197}
2198
Rafael Espindolad9800722010-03-11 14:07:00 +00002199void CXXNameMangler::mangleTemplateArgs(const TemplateParameterList &PL,
2200 const TemplateArgumentList &AL) {
Anders Carlsson7a0ba872009-05-15 16:09:15 +00002201 // <template-args> ::= I <template-arg>+ E
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002202 Out << 'I';
Rafael Espindolad9800722010-03-11 14:07:00 +00002203 for (unsigned i = 0, e = AL.size(); i != e; ++i)
2204 mangleTemplateArg(PL.getParam(i), AL[i]);
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002205 Out << 'E';
Anders Carlsson7a0ba872009-05-15 16:09:15 +00002206}
2207
Rafael Espindolad9800722010-03-11 14:07:00 +00002208void CXXNameMangler::mangleTemplateArgs(const TemplateParameterList &PL,
2209 const TemplateArgument *TemplateArgs,
Anders Carlsson7624f212009-09-18 02:42:01 +00002210 unsigned NumTemplateArgs) {
2211 // <template-args> ::= I <template-arg>+ E
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002212 Out << 'I';
Daniel Dunbar7e0c1952009-11-21 09:17:15 +00002213 for (unsigned i = 0; i != NumTemplateArgs; ++i)
Rafael Espindolad9800722010-03-11 14:07:00 +00002214 mangleTemplateArg(PL.getParam(i), TemplateArgs[i]);
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002215 Out << 'E';
Anders Carlsson7624f212009-09-18 02:42:01 +00002216}
2217
Rafael Espindolad9800722010-03-11 14:07:00 +00002218void CXXNameMangler::mangleTemplateArg(const NamedDecl *P,
2219 const TemplateArgument &A) {
Mike Stump1eb44332009-09-09 15:08:12 +00002220 // <template-arg> ::= <type> # type or template
Anders Carlsson7a0ba872009-05-15 16:09:15 +00002221 // ::= X <expression> E # expression
2222 // ::= <expr-primary> # simple expressions
Douglas Gregor4fc48662011-01-13 16:39:34 +00002223 // ::= J <template-arg>* E # argument pack
Anders Carlsson7a0ba872009-05-15 16:09:15 +00002224 // ::= sp <expression> # pack expansion of (C++0x)
2225 switch (A.getKind()) {
Douglas Gregorf90b27a2011-01-03 22:36:02 +00002226 case TemplateArgument::Null:
2227 llvm_unreachable("Cannot mangle NULL template argument");
2228
Anders Carlsson7a0ba872009-05-15 16:09:15 +00002229 case TemplateArgument::Type:
2230 mangleType(A.getAsType());
2231 break;
Anders Carlsson9e85c742009-12-23 19:30:55 +00002232 case TemplateArgument::Template:
John McCallb6f532e2010-07-14 06:43:17 +00002233 // This is mangled as <type>.
2234 mangleType(A.getAsTemplate());
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002235 break;
Douglas Gregora7fc9012011-01-05 18:58:31 +00002236 case TemplateArgument::TemplateExpansion:
Douglas Gregor4fc48662011-01-13 16:39:34 +00002237 // <type> ::= Dp <type> # pack expansion (C++0x)
Douglas Gregora7fc9012011-01-05 18:58:31 +00002238 Out << "Dp";
2239 mangleType(A.getAsTemplateOrTemplatePattern());
2240 break;
Anders Carlssond553f8c2009-09-21 01:21:10 +00002241 case TemplateArgument::Expression:
2242 Out << 'X';
2243 mangleExpression(A.getAsExpr());
2244 Out << 'E';
2245 break;
Anders Carlssone170ba72009-12-14 01:45:37 +00002246 case TemplateArgument::Integral:
2247 mangleIntegerLiteral(A.getIntegralType(), *A.getAsIntegral());
Anders Carlsson7a0ba872009-05-15 16:09:15 +00002248 break;
Daniel Dunbar7e0c1952009-11-21 09:17:15 +00002249 case TemplateArgument::Declaration: {
Douglas Gregor20f0cc72010-04-23 03:10:43 +00002250 assert(P && "Missing template parameter for declaration argument");
Daniel Dunbar7e0c1952009-11-21 09:17:15 +00002251 // <expr-primary> ::= L <mangled-name> E # external name
2252
Rafael Espindolad9800722010-03-11 14:07:00 +00002253 // Clang produces AST's where pointer-to-member-function expressions
Daniel Dunbar7e0c1952009-11-21 09:17:15 +00002254 // and pointer-to-function expressions are represented as a declaration not
Rafael Espindolad9800722010-03-11 14:07:00 +00002255 // an expression. We compensate for it here to produce the correct mangling.
2256 NamedDecl *D = cast<NamedDecl>(A.getAsDecl());
2257 const NonTypeTemplateParmDecl *Parameter = cast<NonTypeTemplateParmDecl>(P);
2258 bool compensateMangling = D->isCXXClassMember() &&
2259 !Parameter->getType()->isReferenceType();
2260 if (compensateMangling) {
2261 Out << 'X';
2262 mangleOperatorName(OO_Amp, 1);
2263 }
2264
Daniel Dunbar7e0c1952009-11-21 09:17:15 +00002265 Out << 'L';
2266 // References to external entities use the mangled name; if the name would
2267 // not normally be manged then mangle it as unqualified.
2268 //
2269 // FIXME: The ABI specifies that external names here should have _Z, but
2270 // gcc leaves this off.
Rafael Espindolad9800722010-03-11 14:07:00 +00002271 if (compensateMangling)
2272 mangle(D, "_Z");
2273 else
2274 mangle(D, "Z");
Daniel Dunbar7e0c1952009-11-21 09:17:15 +00002275 Out << 'E';
Rafael Espindolad9800722010-03-11 14:07:00 +00002276
2277 if (compensateMangling)
2278 Out << 'E';
2279
Daniel Dunbar7e0c1952009-11-21 09:17:15 +00002280 break;
2281 }
Douglas Gregorf90b27a2011-01-03 22:36:02 +00002282
2283 case TemplateArgument::Pack: {
2284 // Note: proposal by Mike Herrick on 12/20/10
2285 Out << 'J';
2286 for (TemplateArgument::pack_iterator PA = A.pack_begin(),
2287 PAEnd = A.pack_end();
2288 PA != PAEnd; ++PA)
2289 mangleTemplateArg(P, *PA);
2290 Out << 'E';
2291 }
Daniel Dunbar7e0c1952009-11-21 09:17:15 +00002292 }
Anders Carlsson7a0ba872009-05-15 16:09:15 +00002293}
2294
Anders Carlsson0ccdf8d2009-09-27 00:38:53 +00002295void CXXNameMangler::mangleTemplateParameter(unsigned Index) {
2296 // <template-param> ::= T_ # first template parameter
2297 // ::= T <parameter-2 non-negative number> _
2298 if (Index == 0)
2299 Out << "T_";
2300 else
2301 Out << 'T' << (Index - 1) << '_';
2302}
2303
Anders Carlsson76967372009-09-17 00:43:46 +00002304// <substitution> ::= S <seq-id> _
2305// ::= S_
Anders Carlsson6862fc72009-09-17 04:16:28 +00002306bool CXXNameMangler::mangleSubstitution(const NamedDecl *ND) {
Anders Carlssone7c8cb62009-09-26 20:53:44 +00002307 // Try one of the standard substitutions first.
2308 if (mangleStandardSubstitution(ND))
2309 return true;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002310
Anders Carlsson433d1372009-11-07 04:26:04 +00002311 ND = cast<NamedDecl>(ND->getCanonicalDecl());
Anders Carlsson6862fc72009-09-17 04:16:28 +00002312 return mangleSubstitution(reinterpret_cast<uintptr_t>(ND));
2313}
2314
Anders Carlsson76967372009-09-17 00:43:46 +00002315bool CXXNameMangler::mangleSubstitution(QualType T) {
Anders Carlssond99edc42009-09-26 03:55:37 +00002316 if (!T.getCVRQualifiers()) {
2317 if (const RecordType *RT = T->getAs<RecordType>())
2318 return mangleSubstitution(RT->getDecl());
2319 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002320
Anders Carlsson76967372009-09-17 00:43:46 +00002321 uintptr_t TypePtr = reinterpret_cast<uintptr_t>(T.getAsOpaquePtr());
2322
Anders Carlssond3a932a2009-09-17 03:53:28 +00002323 return mangleSubstitution(TypePtr);
2324}
2325
Douglas Gregor1e9268e2010-04-28 05:58:56 +00002326bool CXXNameMangler::mangleSubstitution(TemplateName Template) {
2327 if (TemplateDecl *TD = Template.getAsTemplateDecl())
2328 return mangleSubstitution(TD);
Sean Huntc3021132010-05-05 15:23:54 +00002329
Douglas Gregor1e9268e2010-04-28 05:58:56 +00002330 Template = Context.getASTContext().getCanonicalTemplateName(Template);
2331 return mangleSubstitution(
2332 reinterpret_cast<uintptr_t>(Template.getAsVoidPointer()));
2333}
2334
Anders Carlssond3a932a2009-09-17 03:53:28 +00002335bool CXXNameMangler::mangleSubstitution(uintptr_t Ptr) {
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002336 llvm::DenseMap<uintptr_t, unsigned>::iterator I = Substitutions.find(Ptr);
Anders Carlsson76967372009-09-17 00:43:46 +00002337 if (I == Substitutions.end())
2338 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002339
Anders Carlsson76967372009-09-17 00:43:46 +00002340 unsigned SeqID = I->second;
2341 if (SeqID == 0)
2342 Out << "S_";
2343 else {
2344 SeqID--;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002345
Anders Carlsson76967372009-09-17 00:43:46 +00002346 // <seq-id> is encoded in base-36, using digits and upper case letters.
2347 char Buffer[10];
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002348 char *BufferPtr = llvm::array_endof(Buffer);
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002349
Anders Carlsson76967372009-09-17 00:43:46 +00002350 if (SeqID == 0) *--BufferPtr = '0';
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002351
Anders Carlsson76967372009-09-17 00:43:46 +00002352 while (SeqID) {
2353 assert(BufferPtr > Buffer && "Buffer overflow!");
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002354
John McCall6ab30e02010-06-09 07:26:17 +00002355 char c = static_cast<char>(SeqID % 36);
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002356
Anders Carlsson76967372009-09-17 00:43:46 +00002357 *--BufferPtr = (c < 10 ? '0' + c : 'A' + c - 10);
2358 SeqID /= 36;
2359 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002360
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002361 Out << 'S'
2362 << llvm::StringRef(BufferPtr, llvm::array_endof(Buffer)-BufferPtr)
2363 << '_';
Anders Carlsson76967372009-09-17 00:43:46 +00002364 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002365
Anders Carlsson76967372009-09-17 00:43:46 +00002366 return true;
2367}
2368
Anders Carlssonf514b542009-09-27 00:12:57 +00002369static bool isCharType(QualType T) {
2370 if (T.isNull())
2371 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002372
Anders Carlssonf514b542009-09-27 00:12:57 +00002373 return T->isSpecificBuiltinType(BuiltinType::Char_S) ||
2374 T->isSpecificBuiltinType(BuiltinType::Char_U);
2375}
2376
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002377/// isCharSpecialization - Returns whether a given type is a template
Anders Carlssonf514b542009-09-27 00:12:57 +00002378/// specialization of a given name with a single argument of type char.
2379static bool isCharSpecialization(QualType T, const char *Name) {
2380 if (T.isNull())
2381 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002382
Anders Carlssonf514b542009-09-27 00:12:57 +00002383 const RecordType *RT = T->getAs<RecordType>();
2384 if (!RT)
2385 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002386
2387 const ClassTemplateSpecializationDecl *SD =
Anders Carlssonf514b542009-09-27 00:12:57 +00002388 dyn_cast<ClassTemplateSpecializationDecl>(RT->getDecl());
2389 if (!SD)
2390 return false;
2391
2392 if (!isStdNamespace(SD->getDeclContext()))
2393 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002394
Anders Carlssonf514b542009-09-27 00:12:57 +00002395 const TemplateArgumentList &TemplateArgs = SD->getTemplateArgs();
2396 if (TemplateArgs.size() != 1)
2397 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002398
Anders Carlssonf514b542009-09-27 00:12:57 +00002399 if (!isCharType(TemplateArgs[0].getAsType()))
2400 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002401
Daniel Dunbar01eb9b92009-10-18 21:17:35 +00002402 return SD->getIdentifier()->getName() == Name;
Anders Carlssonf514b542009-09-27 00:12:57 +00002403}
2404
Anders Carlsson91f88602009-12-07 19:56:42 +00002405template <std::size_t StrLen>
Benjamin Kramer54353f42010-11-25 18:29:30 +00002406static bool isStreamCharSpecialization(const ClassTemplateSpecializationDecl*SD,
2407 const char (&Str)[StrLen]) {
Anders Carlsson91f88602009-12-07 19:56:42 +00002408 if (!SD->getIdentifier()->isStr(Str))
2409 return false;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002410
Anders Carlsson91f88602009-12-07 19:56:42 +00002411 const TemplateArgumentList &TemplateArgs = SD->getTemplateArgs();
2412 if (TemplateArgs.size() != 2)
2413 return false;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002414
Anders Carlsson91f88602009-12-07 19:56:42 +00002415 if (!isCharType(TemplateArgs[0].getAsType()))
2416 return false;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002417
Anders Carlsson91f88602009-12-07 19:56:42 +00002418 if (!isCharSpecialization(TemplateArgs[1].getAsType(), "char_traits"))
2419 return false;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002420
Anders Carlsson91f88602009-12-07 19:56:42 +00002421 return true;
2422}
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002423
Anders Carlssone7c8cb62009-09-26 20:53:44 +00002424bool CXXNameMangler::mangleStandardSubstitution(const NamedDecl *ND) {
2425 // <substitution> ::= St # ::std::
Anders Carlsson8c031552009-09-26 23:10:05 +00002426 if (const NamespaceDecl *NS = dyn_cast<NamespaceDecl>(ND)) {
Anders Carlsson47846d22009-12-04 06:23:23 +00002427 if (isStd(NS)) {
Anders Carlsson8c031552009-09-26 23:10:05 +00002428 Out << "St";
2429 return true;
2430 }
2431 }
2432
2433 if (const ClassTemplateDecl *TD = dyn_cast<ClassTemplateDecl>(ND)) {
2434 if (!isStdNamespace(TD->getDeclContext()))
2435 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002436
Anders Carlsson8c031552009-09-26 23:10:05 +00002437 // <substitution> ::= Sa # ::std::allocator
2438 if (TD->getIdentifier()->isStr("allocator")) {
2439 Out << "Sa";
2440 return true;
2441 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002442
Anders Carlsson189d59c2009-09-26 23:14:39 +00002443 // <<substitution> ::= Sb # ::std::basic_string
2444 if (TD->getIdentifier()->isStr("basic_string")) {
2445 Out << "Sb";
2446 return true;
2447 }
Anders Carlsson8c031552009-09-26 23:10:05 +00002448 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002449
2450 if (const ClassTemplateSpecializationDecl *SD =
Anders Carlssonf514b542009-09-27 00:12:57 +00002451 dyn_cast<ClassTemplateSpecializationDecl>(ND)) {
Eli Friedman5370ee22010-02-23 18:25:09 +00002452 if (!isStdNamespace(SD->getDeclContext()))
2453 return false;
2454
Anders Carlssonf514b542009-09-27 00:12:57 +00002455 // <substitution> ::= Ss # ::std::basic_string<char,
2456 // ::std::char_traits<char>,
2457 // ::std::allocator<char> >
2458 if (SD->getIdentifier()->isStr("basic_string")) {
2459 const TemplateArgumentList &TemplateArgs = SD->getTemplateArgs();
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002460
Anders Carlssonf514b542009-09-27 00:12:57 +00002461 if (TemplateArgs.size() != 3)
2462 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002463
Anders Carlssonf514b542009-09-27 00:12:57 +00002464 if (!isCharType(TemplateArgs[0].getAsType()))
2465 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002466
Anders Carlssonf514b542009-09-27 00:12:57 +00002467 if (!isCharSpecialization(TemplateArgs[1].getAsType(), "char_traits"))
2468 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002469
Anders Carlssonf514b542009-09-27 00:12:57 +00002470 if (!isCharSpecialization(TemplateArgs[2].getAsType(), "allocator"))
2471 return false;
2472
2473 Out << "Ss";
2474 return true;
2475 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002476
Anders Carlsson91f88602009-12-07 19:56:42 +00002477 // <substitution> ::= Si # ::std::basic_istream<char,
2478 // ::std::char_traits<char> >
2479 if (isStreamCharSpecialization(SD, "basic_istream")) {
2480 Out << "Si";
2481 return true;
2482 }
2483
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002484 // <substitution> ::= So # ::std::basic_ostream<char,
Anders Carlsson8f8fd8e2009-10-08 17:20:26 +00002485 // ::std::char_traits<char> >
Anders Carlsson91f88602009-12-07 19:56:42 +00002486 if (isStreamCharSpecialization(SD, "basic_ostream")) {
Anders Carlsson8f8fd8e2009-10-08 17:20:26 +00002487 Out << "So";
2488 return true;
2489 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002490
Anders Carlsson91f88602009-12-07 19:56:42 +00002491 // <substitution> ::= Sd # ::std::basic_iostream<char,
2492 // ::std::char_traits<char> >
2493 if (isStreamCharSpecialization(SD, "basic_iostream")) {
2494 Out << "Sd";
2495 return true;
2496 }
Anders Carlssonf514b542009-09-27 00:12:57 +00002497 }
Anders Carlsson8c031552009-09-26 23:10:05 +00002498 return false;
Anders Carlssone7c8cb62009-09-26 20:53:44 +00002499}
2500
Anders Carlsson76967372009-09-17 00:43:46 +00002501void CXXNameMangler::addSubstitution(QualType T) {
Anders Carlssond99edc42009-09-26 03:55:37 +00002502 if (!T.getCVRQualifiers()) {
2503 if (const RecordType *RT = T->getAs<RecordType>()) {
2504 addSubstitution(RT->getDecl());
2505 return;
2506 }
2507 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002508
Anders Carlsson76967372009-09-17 00:43:46 +00002509 uintptr_t TypePtr = reinterpret_cast<uintptr_t>(T.getAsOpaquePtr());
Anders Carlssond3a932a2009-09-17 03:53:28 +00002510 addSubstitution(TypePtr);
2511}
2512
Douglas Gregor1e9268e2010-04-28 05:58:56 +00002513void CXXNameMangler::addSubstitution(TemplateName Template) {
2514 if (TemplateDecl *TD = Template.getAsTemplateDecl())
2515 return addSubstitution(TD);
Sean Huntc3021132010-05-05 15:23:54 +00002516
Douglas Gregor1e9268e2010-04-28 05:58:56 +00002517 Template = Context.getASTContext().getCanonicalTemplateName(Template);
2518 addSubstitution(reinterpret_cast<uintptr_t>(Template.getAsVoidPointer()));
2519}
2520
Anders Carlssond3a932a2009-09-17 03:53:28 +00002521void CXXNameMangler::addSubstitution(uintptr_t Ptr) {
Anders Carlssond3a932a2009-09-17 03:53:28 +00002522 assert(!Substitutions.count(Ptr) && "Substitution already exists!");
Anders Carlsson9d85b722010-06-02 04:29:50 +00002523 Substitutions[Ptr] = SeqID++;
Anders Carlsson76967372009-09-17 00:43:46 +00002524}
2525
Daniel Dunbar1b077112009-11-21 09:06:10 +00002526//
Mike Stump1eb44332009-09-09 15:08:12 +00002527
Daniel Dunbar1b077112009-11-21 09:06:10 +00002528/// \brief Mangles the name of the declaration D and emits that name to the
2529/// given output stream.
2530///
2531/// If the declaration D requires a mangled name, this routine will emit that
2532/// mangled name to \p os and return true. Otherwise, \p os will be unchanged
2533/// and this routine will return false. In this case, the caller should just
2534/// emit the identifier of the declaration (\c D->getIdentifier()) as its
2535/// name.
Peter Collingbourne14110472011-01-13 18:57:25 +00002536void ItaniumMangleContext::mangleName(const NamedDecl *D,
Rafael Espindola0e376a02011-02-11 01:41:00 +00002537 llvm::raw_ostream &Out) {
Daniel Dunbarc02ab4c2009-11-21 09:14:44 +00002538 assert((isa<FunctionDecl>(D) || isa<VarDecl>(D)) &&
2539 "Invalid mangleName() call, argument is not a variable or function!");
2540 assert(!isa<CXXConstructorDecl>(D) && !isa<CXXDestructorDecl>(D) &&
2541 "Invalid mangleName() call on 'structor decl!");
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002542
Daniel Dunbar1b077112009-11-21 09:06:10 +00002543 PrettyStackTraceDecl CrashInfo(D, SourceLocation(),
2544 getASTContext().getSourceManager(),
2545 "Mangling declaration");
Mike Stump1eb44332009-09-09 15:08:12 +00002546
Rafael Espindolac4850c22011-02-10 23:59:36 +00002547 CXXNameMangler Mangler(*this, Out);
Daniel Dunbar94fd26d2009-11-21 09:06:22 +00002548 return Mangler.mangle(D);
Daniel Dunbar1b077112009-11-21 09:06:10 +00002549}
Mike Stump1eb44332009-09-09 15:08:12 +00002550
Peter Collingbourne14110472011-01-13 18:57:25 +00002551void ItaniumMangleContext::mangleCXXCtor(const CXXConstructorDecl *D,
2552 CXXCtorType Type,
Rafael Espindola0e376a02011-02-11 01:41:00 +00002553 llvm::raw_ostream &Out) {
Rafael Espindolac4850c22011-02-10 23:59:36 +00002554 CXXNameMangler Mangler(*this, Out, D, Type);
Daniel Dunbar77939c92009-11-21 09:06:31 +00002555 Mangler.mangle(D);
Daniel Dunbar1b077112009-11-21 09:06:10 +00002556}
Mike Stump1eb44332009-09-09 15:08:12 +00002557
Peter Collingbourne14110472011-01-13 18:57:25 +00002558void ItaniumMangleContext::mangleCXXDtor(const CXXDestructorDecl *D,
2559 CXXDtorType Type,
Rafael Espindola0e376a02011-02-11 01:41:00 +00002560 llvm::raw_ostream &Out) {
Rafael Espindolac4850c22011-02-10 23:59:36 +00002561 CXXNameMangler Mangler(*this, Out, D, Type);
Daniel Dunbar77939c92009-11-21 09:06:31 +00002562 Mangler.mangle(D);
Daniel Dunbar1b077112009-11-21 09:06:10 +00002563}
Mike Stumpf1216772009-07-31 18:25:34 +00002564
Peter Collingbourne14110472011-01-13 18:57:25 +00002565void ItaniumMangleContext::mangleThunk(const CXXMethodDecl *MD,
2566 const ThunkInfo &Thunk,
Rafael Espindolaf0be9792011-02-11 02:52:17 +00002567 llvm::raw_ostream &Out) {
Anders Carlsson19879c92010-03-23 17:17:29 +00002568 // <special-name> ::= T <call-offset> <base encoding>
2569 // # base is the nominal target function of thunk
2570 // <special-name> ::= Tc <call-offset> <call-offset> <base encoding>
2571 // # base is the nominal target function of thunk
2572 // # first call-offset is 'this' adjustment
2573 // # second call-offset is result adjustment
Sean Huntc3021132010-05-05 15:23:54 +00002574
Anders Carlsson19879c92010-03-23 17:17:29 +00002575 assert(!isa<CXXDestructorDecl>(MD) &&
2576 "Use mangleCXXDtor for destructor decls!");
Rafael Espindolac4850c22011-02-10 23:59:36 +00002577 CXXNameMangler Mangler(*this, Out);
Anders Carlsson19879c92010-03-23 17:17:29 +00002578 Mangler.getStream() << "_ZT";
2579 if (!Thunk.Return.isEmpty())
2580 Mangler.getStream() << 'c';
Sean Huntc3021132010-05-05 15:23:54 +00002581
Anders Carlsson19879c92010-03-23 17:17:29 +00002582 // Mangle the 'this' pointer adjustment.
2583 Mangler.mangleCallOffset(Thunk.This.NonVirtual, Thunk.This.VCallOffsetOffset);
Sean Huntc3021132010-05-05 15:23:54 +00002584
Anders Carlsson19879c92010-03-23 17:17:29 +00002585 // Mangle the return pointer adjustment if there is one.
2586 if (!Thunk.Return.isEmpty())
2587 Mangler.mangleCallOffset(Thunk.Return.NonVirtual,
2588 Thunk.Return.VBaseOffsetOffset);
Sean Huntc3021132010-05-05 15:23:54 +00002589
Anders Carlsson19879c92010-03-23 17:17:29 +00002590 Mangler.mangleFunctionEncoding(MD);
2591}
2592
Sean Huntc3021132010-05-05 15:23:54 +00002593void
Peter Collingbourne14110472011-01-13 18:57:25 +00002594ItaniumMangleContext::mangleCXXDtorThunk(const CXXDestructorDecl *DD,
2595 CXXDtorType Type,
2596 const ThisAdjustment &ThisAdjustment,
Rafael Espindolaf0be9792011-02-11 02:52:17 +00002597 llvm::raw_ostream &Out) {
Anders Carlsson19879c92010-03-23 17:17:29 +00002598 // <special-name> ::= T <call-offset> <base encoding>
2599 // # base is the nominal target function of thunk
Rafael Espindolac4850c22011-02-10 23:59:36 +00002600 CXXNameMangler Mangler(*this, Out, DD, Type);
Anders Carlsson19879c92010-03-23 17:17:29 +00002601 Mangler.getStream() << "_ZT";
2602
2603 // Mangle the 'this' pointer adjustment.
Sean Huntc3021132010-05-05 15:23:54 +00002604 Mangler.mangleCallOffset(ThisAdjustment.NonVirtual,
Anders Carlsson19879c92010-03-23 17:17:29 +00002605 ThisAdjustment.VCallOffsetOffset);
2606
2607 Mangler.mangleFunctionEncoding(DD);
2608}
2609
Daniel Dunbarc0747712009-11-21 09:12:13 +00002610/// mangleGuardVariable - Returns the mangled name for a guard variable
2611/// for the passed in VarDecl.
Peter Collingbourne14110472011-01-13 18:57:25 +00002612void ItaniumMangleContext::mangleItaniumGuardVariable(const VarDecl *D,
Rafael Espindolaf0be9792011-02-11 02:52:17 +00002613 llvm::raw_ostream &Out) {
Daniel Dunbarc0747712009-11-21 09:12:13 +00002614 // <special-name> ::= GV <object name> # Guard variable for one-time
2615 // # initialization
Rafael Espindolac4850c22011-02-10 23:59:36 +00002616 CXXNameMangler Mangler(*this, Out);
Daniel Dunbarc0747712009-11-21 09:12:13 +00002617 Mangler.getStream() << "_ZGV";
2618 Mangler.mangleName(D);
2619}
2620
Peter Collingbourne14110472011-01-13 18:57:25 +00002621void ItaniumMangleContext::mangleReferenceTemporary(const VarDecl *D,
Rafael Espindolaf0be9792011-02-11 02:52:17 +00002622 llvm::raw_ostream &Out) {
Anders Carlsson715edf22010-06-26 16:09:40 +00002623 // We match the GCC mangling here.
2624 // <special-name> ::= GR <object name>
Rafael Espindolac4850c22011-02-10 23:59:36 +00002625 CXXNameMangler Mangler(*this, Out);
Anders Carlsson715edf22010-06-26 16:09:40 +00002626 Mangler.getStream() << "_ZGR";
2627 Mangler.mangleName(D);
2628}
2629
Peter Collingbourne14110472011-01-13 18:57:25 +00002630void ItaniumMangleContext::mangleCXXVTable(const CXXRecordDecl *RD,
Rafael Espindolaf0be9792011-02-11 02:52:17 +00002631 llvm::raw_ostream &Out) {
Daniel Dunbarc0747712009-11-21 09:12:13 +00002632 // <special-name> ::= TV <type> # virtual table
Rafael Espindolac4850c22011-02-10 23:59:36 +00002633 CXXNameMangler Mangler(*this, Out);
Daniel Dunbarc0747712009-11-21 09:12:13 +00002634 Mangler.getStream() << "_ZTV";
Douglas Gregor1b12a3b2010-05-26 05:11:13 +00002635 Mangler.mangleNameOrStandardSubstitution(RD);
Daniel Dunbar1b077112009-11-21 09:06:10 +00002636}
Mike Stump82d75b02009-11-10 01:58:37 +00002637
Peter Collingbourne14110472011-01-13 18:57:25 +00002638void ItaniumMangleContext::mangleCXXVTT(const CXXRecordDecl *RD,
Rafael Espindolaf0be9792011-02-11 02:52:17 +00002639 llvm::raw_ostream &Out) {
Daniel Dunbarc0747712009-11-21 09:12:13 +00002640 // <special-name> ::= TT <type> # VTT structure
Rafael Espindolac4850c22011-02-10 23:59:36 +00002641 CXXNameMangler Mangler(*this, Out);
Daniel Dunbarc0747712009-11-21 09:12:13 +00002642 Mangler.getStream() << "_ZTT";
Douglas Gregor1b12a3b2010-05-26 05:11:13 +00002643 Mangler.mangleNameOrStandardSubstitution(RD);
Daniel Dunbar1b077112009-11-21 09:06:10 +00002644}
Mike Stumpab3f7e92009-11-10 01:41:59 +00002645
Peter Collingbourne14110472011-01-13 18:57:25 +00002646void ItaniumMangleContext::mangleCXXCtorVTable(const CXXRecordDecl *RD,
2647 int64_t Offset,
2648 const CXXRecordDecl *Type,
Rafael Espindolaf0be9792011-02-11 02:52:17 +00002649 llvm::raw_ostream &Out) {
Daniel Dunbarc0747712009-11-21 09:12:13 +00002650 // <special-name> ::= TC <type> <offset number> _ <base type>
Rafael Espindolac4850c22011-02-10 23:59:36 +00002651 CXXNameMangler Mangler(*this, Out);
Daniel Dunbarc0747712009-11-21 09:12:13 +00002652 Mangler.getStream() << "_ZTC";
Douglas Gregor1b12a3b2010-05-26 05:11:13 +00002653 Mangler.mangleNameOrStandardSubstitution(RD);
Daniel Dunbarc0747712009-11-21 09:12:13 +00002654 Mangler.getStream() << Offset;
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002655 Mangler.getStream() << '_';
Douglas Gregor1b12a3b2010-05-26 05:11:13 +00002656 Mangler.mangleNameOrStandardSubstitution(Type);
Daniel Dunbar1b077112009-11-21 09:06:10 +00002657}
Mike Stump738f8c22009-07-31 23:15:31 +00002658
Peter Collingbourne14110472011-01-13 18:57:25 +00002659void ItaniumMangleContext::mangleCXXRTTI(QualType Ty,
Rafael Espindolaf0be9792011-02-11 02:52:17 +00002660 llvm::raw_ostream &Out) {
Daniel Dunbarc0747712009-11-21 09:12:13 +00002661 // <special-name> ::= TI <type> # typeinfo structure
Douglas Gregor154fe982009-12-23 22:04:40 +00002662 assert(!Ty.hasQualifiers() && "RTTI info cannot have top-level qualifiers");
Rafael Espindolac4850c22011-02-10 23:59:36 +00002663 CXXNameMangler Mangler(*this, Out);
Daniel Dunbarc0747712009-11-21 09:12:13 +00002664 Mangler.getStream() << "_ZTI";
2665 Mangler.mangleType(Ty);
Daniel Dunbar1b077112009-11-21 09:06:10 +00002666}
Mike Stump67795982009-11-14 00:14:13 +00002667
Peter Collingbourne14110472011-01-13 18:57:25 +00002668void ItaniumMangleContext::mangleCXXRTTIName(QualType Ty,
Rafael Espindolaf0be9792011-02-11 02:52:17 +00002669 llvm::raw_ostream &Out) {
Daniel Dunbarc0747712009-11-21 09:12:13 +00002670 // <special-name> ::= TS <type> # typeinfo name (null terminated byte string)
Rafael Espindolac4850c22011-02-10 23:59:36 +00002671 CXXNameMangler Mangler(*this, Out);
Daniel Dunbarc0747712009-11-21 09:12:13 +00002672 Mangler.getStream() << "_ZTS";
2673 Mangler.mangleType(Ty);
Mike Stumpf1216772009-07-31 18:25:34 +00002674}
Peter Collingbourne14110472011-01-13 18:57:25 +00002675
2676MangleContext *clang::createItaniumMangleContext(ASTContext &Context,
2677 Diagnostic &Diags) {
2678 return new ItaniumMangleContext(Context, Diags);
2679}