blob: d66c374cbec098eff0b23898de13a2c0064bf8b8 [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"
Rafael Espindola4e274e92011-02-15 22:23:51 +000026#include "clang/Basic/TargetInfo.h"
Anders Carlssonc4355b62009-10-07 01:45:02 +000027#include "llvm/ADT/StringExtras.h"
Douglas Gregor5f2bfd42009-02-13 00:10:09 +000028#include "llvm/Support/raw_ostream.h"
John McCallefe6aee2009-09-05 07:56:18 +000029#include "llvm/Support/ErrorHandling.h"
Anders Carlssonf98574b2010-02-05 07:31:37 +000030
31#define MANGLE_CHECKER 0
32
33#if MANGLE_CHECKER
34#include <cxxabi.h>
35#endif
36
Douglas Gregor5f2bfd42009-02-13 00:10:09 +000037using namespace clang;
Charles Davis685b1d92010-05-26 18:25:27 +000038
Douglas Gregor5f2bfd42009-02-13 00:10:09 +000039namespace {
Fariborz Jahanian57058532010-03-03 19:41:08 +000040
John McCall82b7d7b2010-10-18 21:28:44 +000041static const CXXRecordDecl *GetLocalClassDecl(const NamedDecl *ND) {
42 const DeclContext *DC = dyn_cast<DeclContext>(ND);
43 if (!DC)
44 DC = ND->getDeclContext();
45 while (!DC->isNamespace() && !DC->isTranslationUnit()) {
46 if (isa<FunctionDecl>(DC->getParent()))
47 return dyn_cast<CXXRecordDecl>(DC);
48 DC = DC->getParent();
Fariborz Jahanian57058532010-03-03 19:41:08 +000049 }
50 return 0;
51}
52
Anders Carlsson7e120032009-11-24 05:36:32 +000053static const CXXMethodDecl *getStructor(const CXXMethodDecl *MD) {
54 assert((isa<CXXConstructorDecl>(MD) || isa<CXXDestructorDecl>(MD)) &&
55 "Passed in decl is not a ctor or dtor!");
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +000056
Anders Carlsson7e120032009-11-24 05:36:32 +000057 if (const TemplateDecl *TD = MD->getPrimaryTemplate()) {
58 MD = cast<CXXMethodDecl>(TD->getTemplatedDecl());
59
60 assert((isa<CXXConstructorDecl>(MD) || isa<CXXDestructorDecl>(MD)) &&
61 "Templated decl is not a ctor or dtor!");
62 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +000063
Anders Carlsson7e120032009-11-24 05:36:32 +000064 return MD;
65}
John McCall1dd73832010-02-04 01:42:13 +000066
67static const unsigned UnknownArity = ~0U;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +000068
Peter Collingbourne14110472011-01-13 18:57:25 +000069class ItaniumMangleContext : public MangleContext {
70 llvm::DenseMap<const TagDecl *, uint64_t> AnonStructIds;
71 unsigned Discriminator;
72 llvm::DenseMap<const NamedDecl*, unsigned> Uniquifier;
73
74public:
75 explicit ItaniumMangleContext(ASTContext &Context,
76 Diagnostic &Diags)
77 : MangleContext(Context, Diags) { }
78
79 uint64_t getAnonymousStructId(const TagDecl *TD) {
80 std::pair<llvm::DenseMap<const TagDecl *,
81 uint64_t>::iterator, bool> Result =
82 AnonStructIds.insert(std::make_pair(TD, AnonStructIds.size()));
83 return Result.first->second;
84 }
85
86 void startNewFunction() {
87 MangleContext::startNewFunction();
88 mangleInitDiscriminator();
89 }
90
91 /// @name Mangler Entry Points
92 /// @{
93
94 bool shouldMangleDeclName(const NamedDecl *D);
Rafael Espindola0e376a02011-02-11 01:41:00 +000095 void mangleName(const NamedDecl *D, llvm::raw_ostream &);
Peter Collingbourne14110472011-01-13 18:57:25 +000096 void mangleThunk(const CXXMethodDecl *MD,
97 const ThunkInfo &Thunk,
Rafael Espindolaf0be9792011-02-11 02:52:17 +000098 llvm::raw_ostream &);
Peter Collingbourne14110472011-01-13 18:57:25 +000099 void mangleCXXDtorThunk(const CXXDestructorDecl *DD, CXXDtorType Type,
100 const ThisAdjustment &ThisAdjustment,
Rafael Espindolaf0be9792011-02-11 02:52:17 +0000101 llvm::raw_ostream &);
Peter Collingbourne14110472011-01-13 18:57:25 +0000102 void mangleReferenceTemporary(const VarDecl *D,
Rafael Espindolaf0be9792011-02-11 02:52:17 +0000103 llvm::raw_ostream &);
Peter Collingbourne14110472011-01-13 18:57:25 +0000104 void mangleCXXVTable(const CXXRecordDecl *RD,
Rafael Espindolaf0be9792011-02-11 02:52:17 +0000105 llvm::raw_ostream &);
Peter Collingbourne14110472011-01-13 18:57:25 +0000106 void mangleCXXVTT(const CXXRecordDecl *RD,
Rafael Espindolaf0be9792011-02-11 02:52:17 +0000107 llvm::raw_ostream &);
Peter Collingbourne14110472011-01-13 18:57:25 +0000108 void mangleCXXCtorVTable(const CXXRecordDecl *RD, int64_t Offset,
109 const CXXRecordDecl *Type,
Rafael Espindolaf0be9792011-02-11 02:52:17 +0000110 llvm::raw_ostream &);
111 void mangleCXXRTTI(QualType T, llvm::raw_ostream &);
112 void mangleCXXRTTIName(QualType T, llvm::raw_ostream &);
Peter Collingbourne14110472011-01-13 18:57:25 +0000113 void mangleCXXCtor(const CXXConstructorDecl *D, CXXCtorType Type,
Rafael Espindola0e376a02011-02-11 01:41:00 +0000114 llvm::raw_ostream &);
Peter Collingbourne14110472011-01-13 18:57:25 +0000115 void mangleCXXDtor(const CXXDestructorDecl *D, CXXDtorType Type,
Rafael Espindola0e376a02011-02-11 01:41:00 +0000116 llvm::raw_ostream &);
Peter Collingbourne14110472011-01-13 18:57:25 +0000117
Rafael Espindolaf0be9792011-02-11 02:52:17 +0000118 void mangleItaniumGuardVariable(const VarDecl *D, llvm::raw_ostream &);
Peter Collingbourne14110472011-01-13 18:57:25 +0000119
120 void mangleInitDiscriminator() {
121 Discriminator = 0;
122 }
123
124 bool getNextDiscriminator(const NamedDecl *ND, unsigned &disc) {
125 unsigned &discriminator = Uniquifier[ND];
126 if (!discriminator)
127 discriminator = ++Discriminator;
128 if (discriminator == 1)
129 return false;
130 disc = discriminator-2;
131 return true;
132 }
133 /// @}
134};
135
Daniel Dunbar1b077112009-11-21 09:06:10 +0000136/// CXXNameMangler - Manage the mangling of a single name.
Daniel Dunbarc0747712009-11-21 09:12:13 +0000137class CXXNameMangler {
Peter Collingbourne14110472011-01-13 18:57:25 +0000138 ItaniumMangleContext &Context;
Rafael Espindola0e376a02011-02-11 01:41:00 +0000139 llvm::raw_ostream &Out;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000140
Daniel Dunbar1b077112009-11-21 09:06:10 +0000141 const CXXMethodDecl *Structor;
142 unsigned StructorType;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000143
Anders Carlsson9d85b722010-06-02 04:29:50 +0000144 /// SeqID - The next subsitution sequence number.
145 unsigned SeqID;
146
Daniel Dunbar1b077112009-11-21 09:06:10 +0000147 llvm::DenseMap<uintptr_t, unsigned> Substitutions;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000148
John McCall1dd73832010-02-04 01:42:13 +0000149 ASTContext &getASTContext() const { return Context.getASTContext(); }
150
Daniel Dunbarc0747712009-11-21 09:12:13 +0000151public:
Rafael Espindola0e376a02011-02-11 01:41:00 +0000152 CXXNameMangler(ItaniumMangleContext &C, llvm::raw_ostream &Out_)
Rafael Espindolac4850c22011-02-10 23:59:36 +0000153 : Context(C), Out(Out_), Structor(0), StructorType(0), SeqID(0) { }
Rafael Espindola0e376a02011-02-11 01:41:00 +0000154 CXXNameMangler(ItaniumMangleContext &C, llvm::raw_ostream &Out_,
Daniel Dunbar77939c92009-11-21 09:06:31 +0000155 const CXXConstructorDecl *D, CXXCtorType Type)
Rafael Espindolac4850c22011-02-10 23:59:36 +0000156 : Context(C), Out(Out_), Structor(getStructor(D)), StructorType(Type),
Anders Carlsson9d85b722010-06-02 04:29:50 +0000157 SeqID(0) { }
Rafael Espindola0e376a02011-02-11 01:41:00 +0000158 CXXNameMangler(ItaniumMangleContext &C, llvm::raw_ostream &Out_,
Daniel Dunbar77939c92009-11-21 09:06:31 +0000159 const CXXDestructorDecl *D, CXXDtorType Type)
Rafael Espindolac4850c22011-02-10 23:59:36 +0000160 : Context(C), Out(Out_), Structor(getStructor(D)), StructorType(Type),
Anders Carlsson9d85b722010-06-02 04:29:50 +0000161 SeqID(0) { }
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000162
Anders Carlssonf98574b2010-02-05 07:31:37 +0000163#if MANGLE_CHECKER
164 ~CXXNameMangler() {
165 if (Out.str()[0] == '\01')
166 return;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000167
Anders Carlssonf98574b2010-02-05 07:31:37 +0000168 int status = 0;
169 char *result = abi::__cxa_demangle(Out.str().str().c_str(), 0, 0, &status);
170 assert(status == 0 && "Could not demangle mangled name!");
171 free(result);
172 }
173#endif
Rafael Espindola0e376a02011-02-11 01:41:00 +0000174 llvm::raw_ostream &getStream() { return Out; }
Daniel Dunbarc0747712009-11-21 09:12:13 +0000175
Daniel Dunbar7e0c1952009-11-21 09:17:15 +0000176 void mangle(const NamedDecl *D, llvm::StringRef Prefix = "_Z");
Anders Carlsson19879c92010-03-23 17:17:29 +0000177 void mangleCallOffset(int64_t NonVirtual, int64_t Virtual);
John McCall0512e482010-07-14 04:20:34 +0000178 void mangleNumber(const llvm::APSInt &I);
Anders Carlssona94822e2009-11-26 02:32:05 +0000179 void mangleNumber(int64_t Number);
John McCall0512e482010-07-14 04:20:34 +0000180 void mangleFloat(const llvm::APFloat &F);
Daniel Dunbarc0747712009-11-21 09:12:13 +0000181 void mangleFunctionEncoding(const FunctionDecl *FD);
182 void mangleName(const NamedDecl *ND);
183 void mangleType(QualType T);
Douglas Gregor1b12a3b2010-05-26 05:11:13 +0000184 void mangleNameOrStandardSubstitution(const NamedDecl *ND);
185
Daniel Dunbarc0747712009-11-21 09:12:13 +0000186private:
Daniel Dunbar1b077112009-11-21 09:06:10 +0000187 bool mangleSubstitution(const NamedDecl *ND);
188 bool mangleSubstitution(QualType T);
Douglas Gregor1e9268e2010-04-28 05:58:56 +0000189 bool mangleSubstitution(TemplateName Template);
Daniel Dunbar1b077112009-11-21 09:06:10 +0000190 bool mangleSubstitution(uintptr_t Ptr);
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000191
Daniel Dunbar1b077112009-11-21 09:06:10 +0000192 bool mangleStandardSubstitution(const NamedDecl *ND);
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000193
Daniel Dunbar1b077112009-11-21 09:06:10 +0000194 void addSubstitution(const NamedDecl *ND) {
195 ND = cast<NamedDecl>(ND->getCanonicalDecl());
Anders Carlsson433d1372009-11-07 04:26:04 +0000196
Daniel Dunbar1b077112009-11-21 09:06:10 +0000197 addSubstitution(reinterpret_cast<uintptr_t>(ND));
198 }
199 void addSubstitution(QualType T);
Douglas Gregor1e9268e2010-04-28 05:58:56 +0000200 void addSubstitution(TemplateName Template);
Daniel Dunbar1b077112009-11-21 09:06:10 +0000201 void addSubstitution(uintptr_t Ptr);
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000202
John McCall1dd73832010-02-04 01:42:13 +0000203 void mangleUnresolvedScope(NestedNameSpecifier *Qualifier);
204 void mangleUnresolvedName(NestedNameSpecifier *Qualifier,
205 DeclarationName Name,
206 unsigned KnownArity = UnknownArity);
207
Daniel Dunbar1b077112009-11-21 09:06:10 +0000208 void mangleName(const TemplateDecl *TD,
209 const TemplateArgument *TemplateArgs,
210 unsigned NumTemplateArgs);
John McCall1dd73832010-02-04 01:42:13 +0000211 void mangleUnqualifiedName(const NamedDecl *ND) {
212 mangleUnqualifiedName(ND, ND->getDeclName(), UnknownArity);
213 }
214 void mangleUnqualifiedName(const NamedDecl *ND, DeclarationName Name,
215 unsigned KnownArity);
Daniel Dunbar1b077112009-11-21 09:06:10 +0000216 void mangleUnscopedName(const NamedDecl *ND);
217 void mangleUnscopedTemplateName(const TemplateDecl *ND);
Douglas Gregor1e9268e2010-04-28 05:58:56 +0000218 void mangleUnscopedTemplateName(TemplateName);
Daniel Dunbar1b077112009-11-21 09:06:10 +0000219 void mangleSourceName(const IdentifierInfo *II);
220 void mangleLocalName(const NamedDecl *ND);
Fariborz Jahanian57058532010-03-03 19:41:08 +0000221 void mangleNestedName(const NamedDecl *ND, const DeclContext *DC,
222 bool NoFunction=false);
Daniel Dunbar1b077112009-11-21 09:06:10 +0000223 void mangleNestedName(const TemplateDecl *TD,
224 const TemplateArgument *TemplateArgs,
225 unsigned NumTemplateArgs);
Fariborz Jahanian57058532010-03-03 19:41:08 +0000226 void manglePrefix(const DeclContext *DC, bool NoFunction=false);
Daniel Dunbar1b077112009-11-21 09:06:10 +0000227 void mangleTemplatePrefix(const TemplateDecl *ND);
Douglas Gregor20f0cc72010-04-23 03:10:43 +0000228 void mangleTemplatePrefix(TemplateName Template);
Daniel Dunbar1b077112009-11-21 09:06:10 +0000229 void mangleOperatorName(OverloadedOperatorKind OO, unsigned Arity);
230 void mangleQualifiers(Qualifiers Quals);
Douglas Gregor0a9a6d62011-01-26 17:36:28 +0000231 void mangleRefQualifier(RefQualifierKind RefQualifier);
John McCallefe6aee2009-09-05 07:56:18 +0000232
Anders Carlsson7b06f6c2009-12-10 03:14:39 +0000233 void mangleObjCMethodName(const ObjCMethodDecl *MD);
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000234
Daniel Dunbar1b077112009-11-21 09:06:10 +0000235 // Declare manglers for every type class.
John McCallefe6aee2009-09-05 07:56:18 +0000236#define ABSTRACT_TYPE(CLASS, PARENT)
237#define NON_CANONICAL_TYPE(CLASS, PARENT)
238#define TYPE(CLASS, PARENT) void mangleType(const CLASS##Type *T);
239#include "clang/AST/TypeNodes.def"
240
Daniel Dunbar1b077112009-11-21 09:06:10 +0000241 void mangleType(const TagType*);
John McCallb6f532e2010-07-14 06:43:17 +0000242 void mangleType(TemplateName);
Daniel Dunbar1b077112009-11-21 09:06:10 +0000243 void mangleBareFunctionType(const FunctionType *T,
244 bool MangleReturnType);
Bob Wilson57147a82010-11-16 00:32:18 +0000245 void mangleNeonVectorType(const VectorType *T);
Anders Carlssone170ba72009-12-14 01:45:37 +0000246
247 void mangleIntegerLiteral(QualType T, const llvm::APSInt &Value);
John McCall2f27bf82010-02-04 02:56:29 +0000248 void mangleMemberExpr(const Expr *Base, bool IsArrow,
249 NestedNameSpecifier *Qualifier,
250 DeclarationName Name,
251 unsigned KnownArity);
John McCall5e1e89b2010-08-18 19:18:59 +0000252 void mangleExpression(const Expr *E, unsigned Arity = UnknownArity);
Daniel Dunbar1b077112009-11-21 09:06:10 +0000253 void mangleCXXCtorType(CXXCtorType T);
254 void mangleCXXDtorType(CXXDtorType T);
Mike Stump1eb44332009-09-09 15:08:12 +0000255
John McCall6dbce192010-08-20 00:17:19 +0000256 void mangleTemplateArgs(const ExplicitTemplateArgumentList &TemplateArgs);
Douglas Gregor20f0cc72010-04-23 03:10:43 +0000257 void mangleTemplateArgs(TemplateName Template,
258 const TemplateArgument *TemplateArgs,
Sean Huntc3021132010-05-05 15:23:54 +0000259 unsigned NumTemplateArgs);
Rafael Espindolad9800722010-03-11 14:07:00 +0000260 void mangleTemplateArgs(const TemplateParameterList &PL,
261 const TemplateArgument *TemplateArgs,
Daniel Dunbar1b077112009-11-21 09:06:10 +0000262 unsigned NumTemplateArgs);
Rafael Espindolad9800722010-03-11 14:07:00 +0000263 void mangleTemplateArgs(const TemplateParameterList &PL,
264 const TemplateArgumentList &AL);
265 void mangleTemplateArg(const NamedDecl *P, const TemplateArgument &A);
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000266
Daniel Dunbar1b077112009-11-21 09:06:10 +0000267 void mangleTemplateParameter(unsigned Index);
268};
Peter Collingbourne14110472011-01-13 18:57:25 +0000269
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000270}
271
Anders Carlsson43f17402009-04-02 15:51:53 +0000272static bool isInCLinkageSpecification(const Decl *D) {
Douglas Gregor457e2812009-10-28 16:31:34 +0000273 D = D->getCanonicalDecl();
Mike Stump1eb44332009-09-09 15:08:12 +0000274 for (const DeclContext *DC = D->getDeclContext();
Anders Carlsson43f17402009-04-02 15:51:53 +0000275 !DC->isTranslationUnit(); DC = DC->getParent()) {
Mike Stump1eb44332009-09-09 15:08:12 +0000276 if (const LinkageSpecDecl *Linkage = dyn_cast<LinkageSpecDecl>(DC))
Anders Carlsson43f17402009-04-02 15:51:53 +0000277 return Linkage->getLanguage() == LinkageSpecDecl::lang_c;
278 }
Mike Stump1eb44332009-09-09 15:08:12 +0000279
Anders Carlsson43f17402009-04-02 15:51:53 +0000280 return false;
281}
282
Peter Collingbourne14110472011-01-13 18:57:25 +0000283bool ItaniumMangleContext::shouldMangleDeclName(const NamedDecl *D) {
Daniel Dunbarf981bf82009-11-21 09:14:52 +0000284 // In C, functions with no attributes never need to be mangled. Fastpath them.
285 if (!getASTContext().getLangOptions().CPlusPlus && !D->hasAttrs())
286 return false;
287
288 // Any decl can be declared with __asm("foo") on it, and this takes precedence
289 // over all other naming in the .o file.
290 if (D->hasAttr<AsmLabelAttr>())
291 return true;
292
Mike Stump141c5af2009-09-02 00:25:38 +0000293 // Clang's "overloadable" attribute extension to C/C++ implies name mangling
Anders Carlssona1e16222009-11-07 07:15:03 +0000294 // (always) as does passing a C++ member function and a function
295 // whose name is not a simple identifier.
Daniel Dunbarf981bf82009-11-21 09:14:52 +0000296 const FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
297 if (FD && (FD->hasAttr<OverloadableAttr>() || isa<CXXMethodDecl>(FD) ||
298 !FD->getDeclName().isIdentifier()))
299 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000300
Daniel Dunbarf981bf82009-11-21 09:14:52 +0000301 // Otherwise, no mangling is done outside C++ mode.
302 if (!getASTContext().getLangOptions().CPlusPlus)
303 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000304
Sean Hunt31455252010-01-24 03:04:27 +0000305 // Variables at global scope with non-internal linkage are not mangled
Eli Friedman7facf842009-12-02 20:32:49 +0000306 if (!FD) {
307 const DeclContext *DC = D->getDeclContext();
308 // Check for extern variable declared locally.
Fariborz Jahaniane81c5612010-06-30 18:57:21 +0000309 if (DC->isFunctionOrMethod() && D->hasLinkage())
Eli Friedman7facf842009-12-02 20:32:49 +0000310 while (!DC->isNamespace() && !DC->isTranslationUnit())
311 DC = DC->getParent();
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000312 if (DC->isTranslationUnit() && D->getLinkage() != InternalLinkage)
Eli Friedman7facf842009-12-02 20:32:49 +0000313 return false;
314 }
315
Eli Friedmanc00cb642010-07-18 20:49:59 +0000316 // Class members are always mangled.
317 if (D->getDeclContext()->isRecord())
318 return true;
319
Eli Friedman7facf842009-12-02 20:32:49 +0000320 // C functions and "main" are not mangled.
321 if ((FD && FD->isMain()) || isInCLinkageSpecification(D))
Daniel Dunbarf981bf82009-11-21 09:14:52 +0000322 return false;
323
Anders Carlsson43f17402009-04-02 15:51:53 +0000324 return true;
325}
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000326
Daniel Dunbar7e0c1952009-11-21 09:17:15 +0000327void CXXNameMangler::mangle(const NamedDecl *D, llvm::StringRef Prefix) {
Mike Stump141c5af2009-09-02 00:25:38 +0000328 // Any decl can be declared with __asm("foo") on it, and this takes precedence
329 // over all other naming in the .o file.
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000330 if (const AsmLabelAttr *ALA = D->getAttr<AsmLabelAttr>()) {
Chris Lattnerca3f25c2009-03-21 08:24:40 +0000331 // If we have an asm name, then we use it as the mangling.
Rafael Espindola4e274e92011-02-15 22:23:51 +0000332
333 // Adding the prefix can cause problems when one file has a "foo" and
334 // another has a "\01foo". That is known to happen on ELF with the
335 // tricks normally used for producing aliases (PR9177). Fortunately the
336 // llvm mangler on ELF is a nop, so we can just avoid adding the \01
337 // marker.
338 llvm::StringRef UserLabelPrefix =
339 getASTContext().Target.getUserLabelPrefix();
340 if (!UserLabelPrefix.empty())
341 Out << '\01'; // LLVM IR Marker for __asm("foo")
342
Chris Lattnerca3f25c2009-03-21 08:24:40 +0000343 Out << ALA->getLabel();
Daniel Dunbarf981bf82009-11-21 09:14:52 +0000344 return;
Chris Lattnerca3f25c2009-03-21 08:24:40 +0000345 }
Mike Stump1eb44332009-09-09 15:08:12 +0000346
Sean Hunt31455252010-01-24 03:04:27 +0000347 // <mangled-name> ::= _Z <encoding>
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000348 // ::= <data name>
349 // ::= <special-name>
Daniel Dunbar7e0c1952009-11-21 09:17:15 +0000350 Out << Prefix;
351 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
Daniel Dunbarf981bf82009-11-21 09:14:52 +0000352 mangleFunctionEncoding(FD);
Rafael Espindolad9800722010-03-11 14:07:00 +0000353 else if (const VarDecl *VD = dyn_cast<VarDecl>(D))
354 mangleName(VD);
Daniel Dunbar7e0c1952009-11-21 09:17:15 +0000355 else
Rafael Espindolad9800722010-03-11 14:07:00 +0000356 mangleName(cast<FieldDecl>(D));
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000357}
358
359void CXXNameMangler::mangleFunctionEncoding(const FunctionDecl *FD) {
360 // <encoding> ::= <function name> <bare-function-type>
361 mangleName(FD);
Mike Stump1eb44332009-09-09 15:08:12 +0000362
Daniel Dunbar7e0c1952009-11-21 09:17:15 +0000363 // Don't mangle in the type if this isn't a decl we should typically mangle.
364 if (!Context.shouldMangleDeclName(FD))
365 return;
366
Mike Stump141c5af2009-09-02 00:25:38 +0000367 // Whether the mangling of a function type includes the return type depends on
368 // the context and the nature of the function. The rules for deciding whether
369 // the return type is included are:
Mike Stump1eb44332009-09-09 15:08:12 +0000370 //
Douglas Gregor1fd2dd12009-06-29 22:39:32 +0000371 // 1. Template functions (names or types) have return types encoded, with
372 // the exceptions listed below.
Mike Stump1eb44332009-09-09 15:08:12 +0000373 // 2. Function types not appearing as part of a function name mangling,
Douglas Gregor1fd2dd12009-06-29 22:39:32 +0000374 // e.g. parameters, pointer types, etc., have return type encoded, with the
375 // exceptions listed below.
376 // 3. Non-template function names do not have return types encoded.
377 //
Mike Stump141c5af2009-09-02 00:25:38 +0000378 // The exceptions mentioned in (1) and (2) above, for which the return type is
379 // never included, are
Douglas Gregor1fd2dd12009-06-29 22:39:32 +0000380 // 1. Constructors.
381 // 2. Destructors.
382 // 3. Conversion operator functions, e.g. operator int.
383 bool MangleReturnType = false;
Anders Carlsson9234b7f2009-09-17 03:46:43 +0000384 if (FunctionTemplateDecl *PrimaryTemplate = FD->getPrimaryTemplate()) {
385 if (!(isa<CXXConstructorDecl>(FD) || isa<CXXDestructorDecl>(FD) ||
386 isa<CXXConversionDecl>(FD)))
387 MangleReturnType = true;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000388
Anders Carlsson9234b7f2009-09-17 03:46:43 +0000389 // Mangle the type of the primary template.
390 FD = PrimaryTemplate->getTemplatedDecl();
391 }
392
John McCall54e14c42009-10-22 22:37:11 +0000393 // Do the canonicalization out here because parameter types can
394 // undergo additional canonicalization (e.g. array decay).
John McCallf4c73712011-01-19 06:33:43 +0000395 const FunctionType *FT
396 = cast<FunctionType>(Context.getASTContext()
John McCall54e14c42009-10-22 22:37:11 +0000397 .getCanonicalType(FD->getType()));
398
399 mangleBareFunctionType(FT, MangleReturnType);
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000400}
401
Anders Carlsson47846d22009-12-04 06:23:23 +0000402static const DeclContext *IgnoreLinkageSpecDecls(const DeclContext *DC) {
403 while (isa<LinkageSpecDecl>(DC)) {
Anders Carlsson47846d22009-12-04 06:23:23 +0000404 DC = DC->getParent();
405 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000406
Anders Carlsson47846d22009-12-04 06:23:23 +0000407 return DC;
408}
409
Anders Carlssonc820f902010-06-02 15:58:27 +0000410/// isStd - Return whether a given namespace is the 'std' namespace.
411static bool isStd(const NamespaceDecl *NS) {
412 if (!IgnoreLinkageSpecDecls(NS->getParent())->isTranslationUnit())
413 return false;
414
415 const IdentifierInfo *II = NS->getOriginalNamespace()->getIdentifier();
416 return II && II->isStr("std");
417}
418
Anders Carlsson47846d22009-12-04 06:23:23 +0000419// isStdNamespace - Return whether a given decl context is a toplevel 'std'
420// namespace.
Daniel Dunbar1308af92009-11-21 09:11:45 +0000421static bool isStdNamespace(const DeclContext *DC) {
Anders Carlsson47846d22009-12-04 06:23:23 +0000422 if (!DC->isNamespace())
423 return false;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000424
Anders Carlsson47846d22009-12-04 06:23:23 +0000425 return isStd(cast<NamespaceDecl>(DC));
Daniel Dunbar1308af92009-11-21 09:11:45 +0000426}
427
Anders Carlssonbb36ba42009-09-26 03:24:57 +0000428static const TemplateDecl *
429isTemplate(const NamedDecl *ND, const TemplateArgumentList *&TemplateArgs) {
Anders Carlsson2744a062009-09-18 19:00:18 +0000430 // Check if we have a function template.
431 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)){
Anders Carlssonbb36ba42009-09-26 03:24:57 +0000432 if (const TemplateDecl *TD = FD->getPrimaryTemplate()) {
Anders Carlsson2744a062009-09-18 19:00:18 +0000433 TemplateArgs = FD->getTemplateSpecializationArgs();
Anders Carlssonbb36ba42009-09-26 03:24:57 +0000434 return TD;
Anders Carlsson2744a062009-09-18 19:00:18 +0000435 }
436 }
437
Anders Carlssoneafc6dc2009-09-18 19:44:50 +0000438 // Check if we have a class template.
439 if (const ClassTemplateSpecializationDecl *Spec =
440 dyn_cast<ClassTemplateSpecializationDecl>(ND)) {
441 TemplateArgs = &Spec->getTemplateArgs();
Anders Carlssonbb36ba42009-09-26 03:24:57 +0000442 return Spec->getSpecializedTemplate();
Anders Carlssoneafc6dc2009-09-18 19:44:50 +0000443 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000444
Anders Carlsson2744a062009-09-18 19:00:18 +0000445 return 0;
446}
447
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000448void CXXNameMangler::mangleName(const NamedDecl *ND) {
449 // <name> ::= <nested-name>
450 // ::= <unscoped-name>
451 // ::= <unscoped-template-name> <template-args>
Anders Carlsson201ce742009-09-17 03:17:01 +0000452 // ::= <local-name>
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000453 //
Anders Carlssond58d6f72009-09-17 16:12:20 +0000454 const DeclContext *DC = ND->getDeclContext();
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000455
Eli Friedman7facf842009-12-02 20:32:49 +0000456 // If this is an extern variable declared locally, the relevant DeclContext
457 // is that of the containing namespace, or the translation unit.
458 if (isa<FunctionDecl>(DC) && ND->hasLinkage())
459 while (!DC->isNamespace() && !DC->isTranslationUnit())
460 DC = DC->getParent();
John McCall82b7d7b2010-10-18 21:28:44 +0000461 else if (GetLocalClassDecl(ND)) {
462 mangleLocalName(ND);
463 return;
464 }
Eli Friedman7facf842009-12-02 20:32:49 +0000465
Anders Carlsson5cc58c62009-09-22 17:23:30 +0000466 while (isa<LinkageSpecDecl>(DC))
Anders Carlssond58d6f72009-09-17 16:12:20 +0000467 DC = DC->getParent();
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000468
Anders Carlssond58d6f72009-09-17 16:12:20 +0000469 if (DC->isTranslationUnit() || isStdNamespace(DC)) {
Anders Carlsson2744a062009-09-18 19:00:18 +0000470 // Check if we have a template.
471 const TemplateArgumentList *TemplateArgs = 0;
Anders Carlsson0fa6df42009-09-26 19:45:45 +0000472 if (const TemplateDecl *TD = isTemplate(ND, TemplateArgs)) {
Anders Carlsson2744a062009-09-18 19:00:18 +0000473 mangleUnscopedTemplateName(TD);
Rafael Espindolad9800722010-03-11 14:07:00 +0000474 TemplateParameterList *TemplateParameters = TD->getTemplateParameters();
475 mangleTemplateArgs(*TemplateParameters, *TemplateArgs);
Anders Carlsson2744a062009-09-18 19:00:18 +0000476 return;
Anders Carlsson7482e242009-09-18 04:29:09 +0000477 }
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000478
Anders Carlsson7482e242009-09-18 04:29:09 +0000479 mangleUnscopedName(ND);
480 return;
481 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000482
Anders Carlsson7b06f6c2009-12-10 03:14:39 +0000483 if (isa<FunctionDecl>(DC) || isa<ObjCMethodDecl>(DC)) {
Anders Carlsson7482e242009-09-18 04:29:09 +0000484 mangleLocalName(ND);
485 return;
486 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000487
Eli Friedman7facf842009-12-02 20:32:49 +0000488 mangleNestedName(ND, DC);
Anders Carlsson7482e242009-09-18 04:29:09 +0000489}
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000490void CXXNameMangler::mangleName(const TemplateDecl *TD,
Anders Carlsson7624f212009-09-18 02:42:01 +0000491 const TemplateArgument *TemplateArgs,
492 unsigned NumTemplateArgs) {
Anders Carlsson47846d22009-12-04 06:23:23 +0000493 const DeclContext *DC = IgnoreLinkageSpecDecls(TD->getDeclContext());
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000494
Anders Carlsson7624f212009-09-18 02:42:01 +0000495 if (DC->isTranslationUnit() || isStdNamespace(DC)) {
Anders Carlsson0fa6df42009-09-26 19:45:45 +0000496 mangleUnscopedTemplateName(TD);
Rafael Espindolad9800722010-03-11 14:07:00 +0000497 TemplateParameterList *TemplateParameters = TD->getTemplateParameters();
498 mangleTemplateArgs(*TemplateParameters, TemplateArgs, NumTemplateArgs);
Anders Carlsson7624f212009-09-18 02:42:01 +0000499 } else {
500 mangleNestedName(TD, TemplateArgs, NumTemplateArgs);
501 }
502}
503
Anders Carlsson201ce742009-09-17 03:17:01 +0000504void CXXNameMangler::mangleUnscopedName(const NamedDecl *ND) {
505 // <unscoped-name> ::= <unqualified-name>
506 // ::= St <unqualified-name> # ::std::
507 if (isStdNamespace(ND->getDeclContext()))
508 Out << "St";
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000509
Anders Carlsson201ce742009-09-17 03:17:01 +0000510 mangleUnqualifiedName(ND);
511}
512
Anders Carlsson0fa6df42009-09-26 19:45:45 +0000513void CXXNameMangler::mangleUnscopedTemplateName(const TemplateDecl *ND) {
Anders Carlsson201ce742009-09-17 03:17:01 +0000514 // <unscoped-template-name> ::= <unscoped-name>
515 // ::= <substitution>
Anders Carlsson7624f212009-09-18 02:42:01 +0000516 if (mangleSubstitution(ND))
Anders Carlsson03c9d532009-09-17 04:02:31 +0000517 return;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000518
Douglas Gregor32fb4e12010-02-05 20:45:00 +0000519 // <template-template-param> ::= <template-param>
520 if (const TemplateTemplateParmDecl *TTP
521 = dyn_cast<TemplateTemplateParmDecl>(ND)) {
522 mangleTemplateParameter(TTP->getIndex());
523 return;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000524 }
Douglas Gregor32fb4e12010-02-05 20:45:00 +0000525
Anders Carlsson1668f202009-09-26 20:13:56 +0000526 mangleUnscopedName(ND->getTemplatedDecl());
Anders Carlsson7624f212009-09-18 02:42:01 +0000527 addSubstitution(ND);
Anders Carlsson201ce742009-09-17 03:17:01 +0000528}
529
Douglas Gregor1e9268e2010-04-28 05:58:56 +0000530void CXXNameMangler::mangleUnscopedTemplateName(TemplateName Template) {
531 // <unscoped-template-name> ::= <unscoped-name>
532 // ::= <substitution>
533 if (TemplateDecl *TD = Template.getAsTemplateDecl())
534 return mangleUnscopedTemplateName(TD);
Sean Huntc3021132010-05-05 15:23:54 +0000535
Douglas Gregor1e9268e2010-04-28 05:58:56 +0000536 if (mangleSubstitution(Template))
537 return;
538
539 // FIXME: How to cope with operators here?
540 DependentTemplateName *Dependent = Template.getAsDependentTemplateName();
541 assert(Dependent && "Not a dependent template name?");
542 if (!Dependent->isIdentifier()) {
543 // FIXME: We can't possibly know the arity of the operator here!
544 Diagnostic &Diags = Context.getDiags();
545 unsigned DiagID = Diags.getCustomDiagID(Diagnostic::Error,
546 "cannot mangle dependent operator name");
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000547 Diags.Report(DiagID);
Douglas Gregor1e9268e2010-04-28 05:58:56 +0000548 return;
549 }
Sean Huntc3021132010-05-05 15:23:54 +0000550
Douglas Gregor1e9268e2010-04-28 05:58:56 +0000551 mangleSourceName(Dependent->getIdentifier());
552 addSubstitution(Template);
553}
554
John McCall0512e482010-07-14 04:20:34 +0000555void CXXNameMangler::mangleFloat(const llvm::APFloat &F) {
556 // TODO: avoid this copy with careful stream management.
557 llvm::SmallString<20> Buffer;
558 F.bitcastToAPInt().toString(Buffer, 16, false);
559 Out.write(Buffer.data(), Buffer.size());
560}
561
562void CXXNameMangler::mangleNumber(const llvm::APSInt &Value) {
563 if (Value.isSigned() && Value.isNegative()) {
564 Out << 'n';
565 Value.abs().print(Out, true);
566 } else
567 Value.print(Out, Value.isSigned());
568}
569
Anders Carlssona94822e2009-11-26 02:32:05 +0000570void CXXNameMangler::mangleNumber(int64_t Number) {
571 // <number> ::= [n] <non-negative decimal integer>
572 if (Number < 0) {
573 Out << 'n';
574 Number = -Number;
575 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000576
Anders Carlssona94822e2009-11-26 02:32:05 +0000577 Out << Number;
578}
579
Anders Carlsson19879c92010-03-23 17:17:29 +0000580void CXXNameMangler::mangleCallOffset(int64_t NonVirtual, int64_t Virtual) {
Mike Stump141c5af2009-09-02 00:25:38 +0000581 // <call-offset> ::= h <nv-offset> _
582 // ::= v <v-offset> _
583 // <nv-offset> ::= <offset number> # non-virtual base override
Anders Carlssona94822e2009-11-26 02:32:05 +0000584 // <v-offset> ::= <offset number> _ <virtual offset number>
Mike Stump141c5af2009-09-02 00:25:38 +0000585 // # virtual base override, with vcall offset
Anders Carlsson19879c92010-03-23 17:17:29 +0000586 if (!Virtual) {
Anders Carlssona94822e2009-11-26 02:32:05 +0000587 Out << 'h';
Anders Carlsson19879c92010-03-23 17:17:29 +0000588 mangleNumber(NonVirtual);
Anders Carlssona94822e2009-11-26 02:32:05 +0000589 Out << '_';
590 return;
Mike Stump141c5af2009-09-02 00:25:38 +0000591 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000592
Anders Carlssona94822e2009-11-26 02:32:05 +0000593 Out << 'v';
Anders Carlsson19879c92010-03-23 17:17:29 +0000594 mangleNumber(NonVirtual);
Anders Carlssona94822e2009-11-26 02:32:05 +0000595 Out << '_';
Anders Carlsson19879c92010-03-23 17:17:29 +0000596 mangleNumber(Virtual);
Anders Carlssona94822e2009-11-26 02:32:05 +0000597 Out << '_';
Mike Stump9124bcc2009-09-02 00:56:18 +0000598}
599
John McCall1dd73832010-02-04 01:42:13 +0000600void CXXNameMangler::mangleUnresolvedScope(NestedNameSpecifier *Qualifier) {
601 Qualifier = getASTContext().getCanonicalNestedNameSpecifier(Qualifier);
602 switch (Qualifier->getKind()) {
603 case NestedNameSpecifier::Global:
604 // nothing
605 break;
606 case NestedNameSpecifier::Namespace:
607 mangleName(Qualifier->getAsNamespace());
608 break;
609 case NestedNameSpecifier::TypeSpec:
Rafael Espindola9b35b252010-03-17 04:28:11 +0000610 case NestedNameSpecifier::TypeSpecWithTemplate: {
611 const Type *QTy = Qualifier->getAsType();
612
613 if (const TemplateSpecializationType *TST =
614 dyn_cast<TemplateSpecializationType>(QTy)) {
615 if (!mangleSubstitution(QualType(TST, 0))) {
Douglas Gregor20f0cc72010-04-23 03:10:43 +0000616 mangleTemplatePrefix(TST->getTemplateName());
Sean Huntc3021132010-05-05 15:23:54 +0000617
Douglas Gregor20f0cc72010-04-23 03:10:43 +0000618 // FIXME: GCC does not appear to mangle the template arguments when
619 // the template in question is a dependent template name. Should we
620 // emulate that badness?
621 mangleTemplateArgs(TST->getTemplateName(), TST->getArgs(),
Rafael Espindola9b35b252010-03-17 04:28:11 +0000622 TST->getNumArgs());
623 addSubstitution(QualType(TST, 0));
624 }
625 } else {
626 // We use the QualType mangle type variant here because it handles
627 // substitutions.
628 mangleType(QualType(QTy, 0));
629 }
630 }
John McCall1dd73832010-02-04 01:42:13 +0000631 break;
632 case NestedNameSpecifier::Identifier:
John McCallad5e7382010-03-01 23:49:17 +0000633 // Member expressions can have these without prefixes.
634 if (Qualifier->getPrefix())
635 mangleUnresolvedScope(Qualifier->getPrefix());
John McCall1dd73832010-02-04 01:42:13 +0000636 mangleSourceName(Qualifier->getAsIdentifier());
637 break;
638 }
639}
640
641/// Mangles a name which was not resolved to a specific entity.
642void CXXNameMangler::mangleUnresolvedName(NestedNameSpecifier *Qualifier,
643 DeclarationName Name,
644 unsigned KnownArity) {
645 if (Qualifier)
646 mangleUnresolvedScope(Qualifier);
647 // FIXME: ambiguity of unqualified lookup with ::
648
649 mangleUnqualifiedName(0, Name, KnownArity);
650}
651
Anders Carlsson6f7e2f42010-06-08 14:49:03 +0000652static const FieldDecl *FindFirstNamedDataMember(const RecordDecl *RD) {
653 assert(RD->isAnonymousStructOrUnion() &&
654 "Expected anonymous struct or union!");
655
656 for (RecordDecl::field_iterator I = RD->field_begin(), E = RD->field_end();
657 I != E; ++I) {
658 const FieldDecl *FD = *I;
659
660 if (FD->getIdentifier())
661 return FD;
662
663 if (const RecordType *RT = FD->getType()->getAs<RecordType>()) {
664 if (const FieldDecl *NamedDataMember =
665 FindFirstNamedDataMember(RT->getDecl()))
666 return NamedDataMember;
667 }
668 }
669
670 // We didn't find a named data member.
671 return 0;
672}
673
John McCall1dd73832010-02-04 01:42:13 +0000674void CXXNameMangler::mangleUnqualifiedName(const NamedDecl *ND,
675 DeclarationName Name,
676 unsigned KnownArity) {
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000677 // <unqualified-name> ::= <operator-name>
Mike Stump1eb44332009-09-09 15:08:12 +0000678 // ::= <ctor-dtor-name>
679 // ::= <source-name>
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000680 switch (Name.getNameKind()) {
Anders Carlssonc4355b62009-10-07 01:45:02 +0000681 case DeclarationName::Identifier: {
Anders Carlssonc4355b62009-10-07 01:45:02 +0000682 if (const IdentifierInfo *II = Name.getAsIdentifierInfo()) {
Sean Hunt31455252010-01-24 03:04:27 +0000683 // We must avoid conflicts between internally- and externally-
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000684 // linked variable declaration names in the same TU.
Anders Carlssonaec25232010-02-06 04:52:27 +0000685 // This naming convention is the same as that followed by GCC, though it
686 // shouldn't actually matter.
687 if (ND && isa<VarDecl>(ND) && ND->getLinkage() == InternalLinkage &&
Sean Hunt31455252010-01-24 03:04:27 +0000688 ND->getDeclContext()->isFileContext())
689 Out << 'L';
690
Anders Carlssonc4355b62009-10-07 01:45:02 +0000691 mangleSourceName(II);
692 break;
693 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000694
John McCall1dd73832010-02-04 01:42:13 +0000695 // Otherwise, an anonymous entity. We must have a declaration.
696 assert(ND && "mangling empty name without declaration");
697
698 if (const NamespaceDecl *NS = dyn_cast<NamespaceDecl>(ND)) {
699 if (NS->isAnonymousNamespace()) {
700 // This is how gcc mangles these names.
701 Out << "12_GLOBAL__N_1";
702 break;
703 }
704 }
705
Anders Carlsson6f7e2f42010-06-08 14:49:03 +0000706 if (const VarDecl *VD = dyn_cast<VarDecl>(ND)) {
707 // We must have an anonymous union or struct declaration.
708 const RecordDecl *RD =
709 cast<RecordDecl>(VD->getType()->getAs<RecordType>()->getDecl());
710
711 // Itanium C++ ABI 5.1.2:
712 //
713 // For the purposes of mangling, the name of an anonymous union is
714 // considered to be the name of the first named data member found by a
715 // pre-order, depth-first, declaration-order walk of the data members of
716 // the anonymous union. If there is no such data member (i.e., if all of
717 // the data members in the union are unnamed), then there is no way for
718 // a program to refer to the anonymous union, and there is therefore no
719 // need to mangle its name.
720 const FieldDecl *FD = FindFirstNamedDataMember(RD);
John McCall7121c8f2010-08-05 22:02:13 +0000721
722 // It's actually possible for various reasons for us to get here
723 // with an empty anonymous struct / union. Fortunately, it
724 // doesn't really matter what name we generate.
725 if (!FD) break;
Anders Carlsson6f7e2f42010-06-08 14:49:03 +0000726 assert(FD->getIdentifier() && "Data member name isn't an identifier!");
727
728 mangleSourceName(FD->getIdentifier());
729 break;
730 }
731
Anders Carlssonc4355b62009-10-07 01:45:02 +0000732 // We must have an anonymous struct.
733 const TagDecl *TD = cast<TagDecl>(ND);
734 if (const TypedefDecl *D = TD->getTypedefForAnonDecl()) {
735 assert(TD->getDeclContext() == D->getDeclContext() &&
736 "Typedef should not be in another decl context!");
737 assert(D->getDeclName().getAsIdentifierInfo() &&
738 "Typedef was not named!");
739 mangleSourceName(D->getDeclName().getAsIdentifierInfo());
740 break;
741 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000742
Anders Carlssonc4355b62009-10-07 01:45:02 +0000743 // Get a unique id for the anonymous struct.
744 uint64_t AnonStructId = Context.getAnonymousStructId(TD);
745
746 // Mangle it as a source name in the form
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000747 // [n] $_<id>
Anders Carlssonc4355b62009-10-07 01:45:02 +0000748 // where n is the length of the string.
749 llvm::SmallString<8> Str;
750 Str += "$_";
751 Str += llvm::utostr(AnonStructId);
752
753 Out << Str.size();
754 Out << Str.str();
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000755 break;
Anders Carlssonc4355b62009-10-07 01:45:02 +0000756 }
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000757
758 case DeclarationName::ObjCZeroArgSelector:
759 case DeclarationName::ObjCOneArgSelector:
760 case DeclarationName::ObjCMultiArgSelector:
761 assert(false && "Can't mangle Objective-C selector names here!");
762 break;
763
764 case DeclarationName::CXXConstructorName:
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++ constructor we're mangling, use the type
767 // we were given.
Anders Carlsson27ae5362009-04-17 01:58:57 +0000768 mangleCXXCtorType(static_cast<CXXCtorType>(StructorType));
Anders Carlsson3ac86b52009-04-15 05:36:58 +0000769 else
770 // Otherwise, use the complete constructor name. This is relevant if a
771 // class with a constructor is declared within a constructor.
772 mangleCXXCtorType(Ctor_Complete);
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000773 break;
774
775 case DeclarationName::CXXDestructorName:
Anders Carlsson27ae5362009-04-17 01:58:57 +0000776 if (ND == Structor)
Mike Stump141c5af2009-09-02 00:25:38 +0000777 // If the named decl is the C++ destructor we're mangling, use the type we
778 // were given.
Anders Carlsson27ae5362009-04-17 01:58:57 +0000779 mangleCXXDtorType(static_cast<CXXDtorType>(StructorType));
780 else
781 // Otherwise, use the complete destructor name. This is relevant if a
782 // class with a destructor is declared within a destructor.
783 mangleCXXDtorType(Dtor_Complete);
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000784 break;
785
786 case DeclarationName::CXXConversionFunctionName:
Mike Stump1eb44332009-09-09 15:08:12 +0000787 // <operator-name> ::= cv <type> # (cast)
Douglas Gregor219cc612009-02-13 01:28:03 +0000788 Out << "cv";
Anders Carlssonb5404912009-10-07 01:06:45 +0000789 mangleType(Context.getASTContext().getCanonicalType(Name.getCXXNameType()));
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000790 break;
791
Anders Carlsson8257d412009-12-22 06:36:32 +0000792 case DeclarationName::CXXOperatorName: {
John McCall1dd73832010-02-04 01:42:13 +0000793 unsigned Arity;
794 if (ND) {
795 Arity = cast<FunctionDecl>(ND)->getNumParams();
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000796
John McCall1dd73832010-02-04 01:42:13 +0000797 // If we have a C++ member function, we need to include the 'this' pointer.
798 // FIXME: This does not make sense for operators that are static, but their
799 // names stay the same regardless of the arity (operator new for instance).
800 if (isa<CXXMethodDecl>(ND))
801 Arity++;
802 } else
803 Arity = KnownArity;
804
Anders Carlsson8257d412009-12-22 06:36:32 +0000805 mangleOperatorName(Name.getCXXOverloadedOperator(), Arity);
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000806 break;
Anders Carlsson8257d412009-12-22 06:36:32 +0000807 }
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000808
Sean Hunt3e518bd2009-11-29 07:34:05 +0000809 case DeclarationName::CXXLiteralOperatorName:
Sean Hunt5dd6b392009-12-04 21:11:13 +0000810 // FIXME: This mangling is not yet official.
Sean Hunt2421f662009-12-04 21:01:37 +0000811 Out << "li";
Sean Hunt3e518bd2009-11-29 07:34:05 +0000812 mangleSourceName(Name.getCXXLiteralIdentifier());
813 break;
814
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000815 case DeclarationName::CXXUsingDirective:
816 assert(false && "Can't mangle a using directive name!");
Douglas Gregor219cc612009-02-13 01:28:03 +0000817 break;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000818 }
819}
820
821void CXXNameMangler::mangleSourceName(const IdentifierInfo *II) {
822 // <source-name> ::= <positive length number> <identifier>
823 // <number> ::= [n] <non-negative decimal integer>
824 // <identifier> ::= <unqualified source code identifier>
825 Out << II->getLength() << II->getName();
826}
827
Eli Friedman7facf842009-12-02 20:32:49 +0000828void CXXNameMangler::mangleNestedName(const NamedDecl *ND,
Fariborz Jahanian57058532010-03-03 19:41:08 +0000829 const DeclContext *DC,
830 bool NoFunction) {
Douglas Gregor0a9a6d62011-01-26 17:36:28 +0000831 // <nested-name>
832 // ::= N [<CV-qualifiers>] [<ref-qualifier>] <prefix> <unqualified-name> E
833 // ::= N [<CV-qualifiers>] [<ref-qualifier>] <template-prefix>
834 // <template-args> E
Anders Carlssond99edc42009-09-26 03:55:37 +0000835
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000836 Out << 'N';
Douglas Gregor0a9a6d62011-01-26 17:36:28 +0000837 if (const CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(ND)) {
John McCall0953e762009-09-24 19:53:00 +0000838 mangleQualifiers(Qualifiers::fromCVRMask(Method->getTypeQualifiers()));
Douglas Gregor0a9a6d62011-01-26 17:36:28 +0000839 mangleRefQualifier(Method->getRefQualifier());
840 }
841
Anders Carlsson2744a062009-09-18 19:00:18 +0000842 // Check if we have a template.
843 const TemplateArgumentList *TemplateArgs = 0;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000844 if (const TemplateDecl *TD = isTemplate(ND, TemplateArgs)) {
Anders Carlsson2744a062009-09-18 19:00:18 +0000845 mangleTemplatePrefix(TD);
Rafael Espindolad9800722010-03-11 14:07:00 +0000846 TemplateParameterList *TemplateParameters = TD->getTemplateParameters();
847 mangleTemplateArgs(*TemplateParameters, *TemplateArgs);
Fariborz Jahanian57058532010-03-03 19:41:08 +0000848 }
849 else {
850 manglePrefix(DC, NoFunction);
Anders Carlsson7482e242009-09-18 04:29:09 +0000851 mangleUnqualifiedName(ND);
852 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000853
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000854 Out << 'E';
855}
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000856void CXXNameMangler::mangleNestedName(const TemplateDecl *TD,
Anders Carlsson7624f212009-09-18 02:42:01 +0000857 const TemplateArgument *TemplateArgs,
858 unsigned NumTemplateArgs) {
Anders Carlssone45117b2009-09-27 19:53:49 +0000859 // <nested-name> ::= N [<CV-qualifiers>] <template-prefix> <template-args> E
860
Anders Carlsson7624f212009-09-18 02:42:01 +0000861 Out << 'N';
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000862
Anders Carlssone45117b2009-09-27 19:53:49 +0000863 mangleTemplatePrefix(TD);
Rafael Espindolad9800722010-03-11 14:07:00 +0000864 TemplateParameterList *TemplateParameters = TD->getTemplateParameters();
865 mangleTemplateArgs(*TemplateParameters, TemplateArgs, NumTemplateArgs);
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000866
Anders Carlsson7624f212009-09-18 02:42:01 +0000867 Out << 'E';
868}
869
Anders Carlsson1b42c792009-04-02 16:24:45 +0000870void CXXNameMangler::mangleLocalName(const NamedDecl *ND) {
871 // <local-name> := Z <function encoding> E <entity name> [<discriminator>]
872 // := Z <function encoding> E s [<discriminator>]
Mike Stump1eb44332009-09-09 15:08:12 +0000873 // <discriminator> := _ <non-negative number>
Fariborz Jahanian57058532010-03-03 19:41:08 +0000874 const DeclContext *DC = ND->getDeclContext();
Anders Carlsson1b42c792009-04-02 16:24:45 +0000875 Out << 'Z';
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000876
Charles Davis685b1d92010-05-26 18:25:27 +0000877 if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(DC)) {
878 mangleObjCMethodName(MD);
John McCall82b7d7b2010-10-18 21:28:44 +0000879 } else if (const CXXRecordDecl *RD = GetLocalClassDecl(ND)) {
880 mangleFunctionEncoding(cast<FunctionDecl>(RD->getDeclContext()));
Fariborz Jahanian57058532010-03-03 19:41:08 +0000881 Out << 'E';
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000882
John McCall82b7d7b2010-10-18 21:28:44 +0000883 // Mangle the name relative to the closest enclosing function.
884 if (ND == RD) // equality ok because RD derived from ND above
885 mangleUnqualifiedName(ND);
886 else
887 mangleNestedName(ND, DC, true /*NoFunction*/);
888
Fariborz Jahanian4819ac42010-03-04 01:02:03 +0000889 unsigned disc;
John McCall82b7d7b2010-10-18 21:28:44 +0000890 if (Context.getNextDiscriminator(RD, disc)) {
Fariborz Jahanian4819ac42010-03-04 01:02:03 +0000891 if (disc < 10)
892 Out << '_' << disc;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000893 else
Fariborz Jahanian4819ac42010-03-04 01:02:03 +0000894 Out << "__" << disc << '_';
895 }
Fariborz Jahanian57058532010-03-03 19:41:08 +0000896
897 return;
898 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000899 else
Fariborz Jahanian57058532010-03-03 19:41:08 +0000900 mangleFunctionEncoding(cast<FunctionDecl>(DC));
Anders Carlsson7b06f6c2009-12-10 03:14:39 +0000901
Anders Carlsson1b42c792009-04-02 16:24:45 +0000902 Out << 'E';
Eli Friedman6f9f25d2009-12-11 20:21:38 +0000903 mangleUnqualifiedName(ND);
Anders Carlsson1b42c792009-04-02 16:24:45 +0000904}
905
Fariborz Jahanian57058532010-03-03 19:41:08 +0000906void CXXNameMangler::manglePrefix(const DeclContext *DC, bool NoFunction) {
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000907 // <prefix> ::= <prefix> <unqualified-name>
908 // ::= <template-prefix> <template-args>
909 // ::= <template-param>
910 // ::= # empty
911 // ::= <substitution>
Anders Carlsson6862fc72009-09-17 04:16:28 +0000912
Anders Carlssonadd28822009-09-22 20:33:31 +0000913 while (isa<LinkageSpecDecl>(DC))
914 DC = DC->getParent();
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000915
Anders Carlsson9263e912009-09-18 18:39:58 +0000916 if (DC->isTranslationUnit())
917 return;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000918
Douglas Gregor35415f52010-05-25 17:04:15 +0000919 if (const BlockDecl *Block = dyn_cast<BlockDecl>(DC)) {
920 manglePrefix(DC->getParent(), NoFunction);
921 llvm::SmallString<64> Name;
Rafael Espindolac4850c22011-02-10 23:59:36 +0000922 llvm::raw_svector_ostream NameStream(Name);
923 Context.mangleBlock(Block, NameStream);
924 NameStream.flush();
Douglas Gregor35415f52010-05-25 17:04:15 +0000925 Out << Name.size() << Name;
926 return;
927 }
928
Anders Carlsson6862fc72009-09-17 04:16:28 +0000929 if (mangleSubstitution(cast<NamedDecl>(DC)))
930 return;
Anders Carlsson7482e242009-09-18 04:29:09 +0000931
Anders Carlsson2ee3fca2009-09-18 20:11:09 +0000932 // Check if we have a template.
933 const TemplateArgumentList *TemplateArgs = 0;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000934 if (const TemplateDecl *TD = isTemplate(cast<NamedDecl>(DC), TemplateArgs)) {
Anders Carlsson2ee3fca2009-09-18 20:11:09 +0000935 mangleTemplatePrefix(TD);
Rafael Espindolad9800722010-03-11 14:07:00 +0000936 TemplateParameterList *TemplateParameters = TD->getTemplateParameters();
937 mangleTemplateArgs(*TemplateParameters, *TemplateArgs);
Fariborz Jahanian57058532010-03-03 19:41:08 +0000938 }
Douglas Gregor35415f52010-05-25 17:04:15 +0000939 else if(NoFunction && (isa<FunctionDecl>(DC) || isa<ObjCMethodDecl>(DC)))
Fariborz Jahanian57058532010-03-03 19:41:08 +0000940 return;
Douglas Gregor35415f52010-05-25 17:04:15 +0000941 else if (const ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(DC))
942 mangleObjCMethodName(Method);
Fariborz Jahanian57058532010-03-03 19:41:08 +0000943 else {
944 manglePrefix(DC->getParent(), NoFunction);
Anders Carlsson2ee3fca2009-09-18 20:11:09 +0000945 mangleUnqualifiedName(cast<NamedDecl>(DC));
946 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000947
Anders Carlsson6862fc72009-09-17 04:16:28 +0000948 addSubstitution(cast<NamedDecl>(DC));
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000949}
950
Douglas Gregor20f0cc72010-04-23 03:10:43 +0000951void CXXNameMangler::mangleTemplatePrefix(TemplateName Template) {
952 // <template-prefix> ::= <prefix> <template unqualified-name>
953 // ::= <template-param>
954 // ::= <substitution>
955 if (TemplateDecl *TD = Template.getAsTemplateDecl())
956 return mangleTemplatePrefix(TD);
957
958 if (QualifiedTemplateName *Qualified = Template.getAsQualifiedTemplateName())
959 mangleUnresolvedScope(Qualified->getQualifier());
Sean Huntc3021132010-05-05 15:23:54 +0000960
Douglas Gregor20f0cc72010-04-23 03:10:43 +0000961 if (OverloadedTemplateStorage *Overloaded
962 = Template.getAsOverloadedTemplate()) {
Sean Huntc3021132010-05-05 15:23:54 +0000963 mangleUnqualifiedName(0, (*Overloaded->begin())->getDeclName(),
Douglas Gregor20f0cc72010-04-23 03:10:43 +0000964 UnknownArity);
965 return;
966 }
Sean Huntc3021132010-05-05 15:23:54 +0000967
Douglas Gregor20f0cc72010-04-23 03:10:43 +0000968 DependentTemplateName *Dependent = Template.getAsDependentTemplateName();
969 assert(Dependent && "Unknown template name kind?");
970 mangleUnresolvedScope(Dependent->getQualifier());
Douglas Gregor1e9268e2010-04-28 05:58:56 +0000971 mangleUnscopedTemplateName(Template);
Douglas Gregor20f0cc72010-04-23 03:10:43 +0000972}
973
Anders Carlsson0fa6df42009-09-26 19:45:45 +0000974void CXXNameMangler::mangleTemplatePrefix(const TemplateDecl *ND) {
Anders Carlsson7482e242009-09-18 04:29:09 +0000975 // <template-prefix> ::= <prefix> <template unqualified-name>
976 // ::= <template-param>
977 // ::= <substitution>
Douglas Gregor32fb4e12010-02-05 20:45:00 +0000978 // <template-template-param> ::= <template-param>
979 // <substitution>
Anders Carlsson7482e242009-09-18 04:29:09 +0000980
Anders Carlssonaeb85372009-09-26 22:18:22 +0000981 if (mangleSubstitution(ND))
982 return;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000983
Douglas Gregor32fb4e12010-02-05 20:45:00 +0000984 // <template-template-param> ::= <template-param>
985 if (const TemplateTemplateParmDecl *TTP
986 = dyn_cast<TemplateTemplateParmDecl>(ND)) {
987 mangleTemplateParameter(TTP->getIndex());
988 return;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000989 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000990
Anders Carlssonaa73ab12009-09-18 18:47:07 +0000991 manglePrefix(ND->getDeclContext());
Anders Carlsson1668f202009-09-26 20:13:56 +0000992 mangleUnqualifiedName(ND->getTemplatedDecl());
Anders Carlssonaeb85372009-09-26 22:18:22 +0000993 addSubstitution(ND);
Anders Carlsson7482e242009-09-18 04:29:09 +0000994}
995
John McCallb6f532e2010-07-14 06:43:17 +0000996/// Mangles a template name under the production <type>. Required for
997/// template template arguments.
998/// <type> ::= <class-enum-type>
999/// ::= <template-param>
1000/// ::= <substitution>
1001void CXXNameMangler::mangleType(TemplateName TN) {
1002 if (mangleSubstitution(TN))
1003 return;
1004
1005 TemplateDecl *TD = 0;
1006
1007 switch (TN.getKind()) {
1008 case TemplateName::QualifiedTemplate:
1009 TD = TN.getAsQualifiedTemplateName()->getTemplateDecl();
1010 goto HaveDecl;
1011
1012 case TemplateName::Template:
1013 TD = TN.getAsTemplateDecl();
1014 goto HaveDecl;
1015
1016 HaveDecl:
1017 if (isa<TemplateTemplateParmDecl>(TD))
1018 mangleTemplateParameter(cast<TemplateTemplateParmDecl>(TD)->getIndex());
1019 else
1020 mangleName(TD);
1021 break;
1022
1023 case TemplateName::OverloadedTemplate:
1024 llvm_unreachable("can't mangle an overloaded template name as a <type>");
1025 break;
1026
1027 case TemplateName::DependentTemplate: {
1028 const DependentTemplateName *Dependent = TN.getAsDependentTemplateName();
1029 assert(Dependent->isIdentifier());
1030
1031 // <class-enum-type> ::= <name>
1032 // <name> ::= <nested-name>
1033 mangleUnresolvedScope(Dependent->getQualifier());
1034 mangleSourceName(Dependent->getIdentifier());
1035 break;
1036 }
1037
Douglas Gregor1aee05d2011-01-15 06:45:20 +00001038 case TemplateName::SubstTemplateTemplateParmPack: {
1039 SubstTemplateTemplateParmPackStorage *SubstPack
1040 = TN.getAsSubstTemplateTemplateParmPack();
1041 mangleTemplateParameter(SubstPack->getParameterPack()->getIndex());
1042 break;
1043 }
John McCallb6f532e2010-07-14 06:43:17 +00001044 }
1045
1046 addSubstitution(TN);
1047}
1048
Mike Stump1eb44332009-09-09 15:08:12 +00001049void
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001050CXXNameMangler::mangleOperatorName(OverloadedOperatorKind OO, unsigned Arity) {
1051 switch (OO) {
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001052 // <operator-name> ::= nw # new
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001053 case OO_New: Out << "nw"; break;
1054 // ::= na # new[]
1055 case OO_Array_New: Out << "na"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001056 // ::= dl # delete
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001057 case OO_Delete: Out << "dl"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001058 // ::= da # delete[]
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001059 case OO_Array_Delete: Out << "da"; break;
1060 // ::= ps # + (unary)
John McCall5e1e89b2010-08-18 19:18:59 +00001061 // ::= pl # + (binary or unknown)
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001062 case OO_Plus:
Anders Carlsson8257d412009-12-22 06:36:32 +00001063 Out << (Arity == 1? "ps" : "pl"); break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001064 // ::= ng # - (unary)
John McCall5e1e89b2010-08-18 19:18:59 +00001065 // ::= mi # - (binary or unknown)
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001066 case OO_Minus:
Anders Carlsson8257d412009-12-22 06:36:32 +00001067 Out << (Arity == 1? "ng" : "mi"); break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001068 // ::= ad # & (unary)
John McCall5e1e89b2010-08-18 19:18:59 +00001069 // ::= an # & (binary or unknown)
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001070 case OO_Amp:
Anders Carlsson8257d412009-12-22 06:36:32 +00001071 Out << (Arity == 1? "ad" : "an"); break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001072 // ::= de # * (unary)
John McCall5e1e89b2010-08-18 19:18:59 +00001073 // ::= ml # * (binary or unknown)
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001074 case OO_Star:
John McCall5e1e89b2010-08-18 19:18:59 +00001075 // Use binary when unknown.
Anders Carlsson8257d412009-12-22 06:36:32 +00001076 Out << (Arity == 1? "de" : "ml"); break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001077 // ::= co # ~
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001078 case OO_Tilde: Out << "co"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001079 // ::= dv # /
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001080 case OO_Slash: Out << "dv"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001081 // ::= rm # %
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001082 case OO_Percent: Out << "rm"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001083 // ::= or # |
1084 case OO_Pipe: Out << "or"; break;
1085 // ::= eo # ^
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001086 case OO_Caret: Out << "eo"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001087 // ::= aS # =
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001088 case OO_Equal: Out << "aS"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001089 // ::= pL # +=
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001090 case OO_PlusEqual: Out << "pL"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001091 // ::= mI # -=
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001092 case OO_MinusEqual: Out << "mI"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001093 // ::= mL # *=
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001094 case OO_StarEqual: Out << "mL"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001095 // ::= dV # /=
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001096 case OO_SlashEqual: Out << "dV"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001097 // ::= rM # %=
1098 case OO_PercentEqual: Out << "rM"; break;
1099 // ::= aN # &=
1100 case OO_AmpEqual: Out << "aN"; break;
1101 // ::= oR # |=
1102 case OO_PipeEqual: Out << "oR"; break;
1103 // ::= eO # ^=
1104 case OO_CaretEqual: Out << "eO"; break;
1105 // ::= ls # <<
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001106 case OO_LessLess: Out << "ls"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001107 // ::= rs # >>
1108 case OO_GreaterGreater: Out << "rs"; break;
1109 // ::= lS # <<=
1110 case OO_LessLessEqual: Out << "lS"; break;
1111 // ::= rS # >>=
1112 case OO_GreaterGreaterEqual: Out << "rS"; break;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001113 // ::= eq # ==
1114 case OO_EqualEqual: Out << "eq"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001115 // ::= ne # !=
1116 case OO_ExclaimEqual: Out << "ne"; break;
1117 // ::= lt # <
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001118 case OO_Less: Out << "lt"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001119 // ::= gt # >
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001120 case OO_Greater: Out << "gt"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001121 // ::= le # <=
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001122 case OO_LessEqual: Out << "le"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001123 // ::= ge # >=
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001124 case OO_GreaterEqual: Out << "ge"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001125 // ::= nt # !
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001126 case OO_Exclaim: Out << "nt"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001127 // ::= aa # &&
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001128 case OO_AmpAmp: Out << "aa"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001129 // ::= oo # ||
1130 case OO_PipePipe: Out << "oo"; break;
1131 // ::= pp # ++
1132 case OO_PlusPlus: Out << "pp"; break;
1133 // ::= mm # --
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001134 case OO_MinusMinus: Out << "mm"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001135 // ::= cm # ,
1136 case OO_Comma: Out << "cm"; break;
1137 // ::= pm # ->*
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001138 case OO_ArrowStar: Out << "pm"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001139 // ::= pt # ->
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001140 case OO_Arrow: Out << "pt"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001141 // ::= cl # ()
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001142 case OO_Call: Out << "cl"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001143 // ::= ix # []
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001144 case OO_Subscript: Out << "ix"; break;
Anders Carlssone170ba72009-12-14 01:45:37 +00001145
1146 // ::= qu # ?
1147 // The conditional operator can't be overloaded, but we still handle it when
1148 // mangling expressions.
1149 case OO_Conditional: Out << "qu"; break;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001150
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001151 case OO_None:
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001152 case NUM_OVERLOADED_OPERATORS:
Mike Stump1eb44332009-09-09 15:08:12 +00001153 assert(false && "Not an overloaded operator");
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001154 break;
1155 }
1156}
1157
John McCall0953e762009-09-24 19:53:00 +00001158void CXXNameMangler::mangleQualifiers(Qualifiers Quals) {
Mike Stump1eb44332009-09-09 15:08:12 +00001159 // <CV-qualifiers> ::= [r] [V] [K] # restrict (C99), volatile, const
John McCall0953e762009-09-24 19:53:00 +00001160 if (Quals.hasRestrict())
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001161 Out << 'r';
John McCall0953e762009-09-24 19:53:00 +00001162 if (Quals.hasVolatile())
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001163 Out << 'V';
John McCall0953e762009-09-24 19:53:00 +00001164 if (Quals.hasConst())
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001165 Out << 'K';
John McCall0953e762009-09-24 19:53:00 +00001166
Douglas Gregor56079f72010-06-14 23:15:08 +00001167 if (Quals.hasAddressSpace()) {
1168 // Extension:
1169 //
1170 // <type> ::= U <address-space-number>
1171 //
1172 // where <address-space-number> is a source name consisting of 'AS'
1173 // followed by the address space <number>.
1174 llvm::SmallString<64> ASString;
1175 ASString = "AS" + llvm::utostr_32(Quals.getAddressSpace());
1176 Out << 'U' << ASString.size() << ASString;
1177 }
1178
John McCall0953e762009-09-24 19:53:00 +00001179 // FIXME: For now, just drop all extension qualifiers on the floor.
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001180}
1181
Douglas Gregor0a9a6d62011-01-26 17:36:28 +00001182void CXXNameMangler::mangleRefQualifier(RefQualifierKind RefQualifier) {
1183 // <ref-qualifier> ::= R # lvalue reference
1184 // ::= O # rvalue-reference
1185 // Proposal to Itanium C++ ABI list on 1/26/11
1186 switch (RefQualifier) {
1187 case RQ_None:
1188 break;
1189
1190 case RQ_LValue:
1191 Out << 'R';
1192 break;
1193
1194 case RQ_RValue:
1195 Out << 'O';
1196 break;
1197 }
1198}
1199
Anders Carlsson7b06f6c2009-12-10 03:14:39 +00001200void CXXNameMangler::mangleObjCMethodName(const ObjCMethodDecl *MD) {
Rafael Espindolaf0be9792011-02-11 02:52:17 +00001201 Context.mangleObjCMethodName(MD, Out);
Anders Carlsson7b06f6c2009-12-10 03:14:39 +00001202}
1203
John McCallb47f7482011-01-26 20:05:40 +00001204void CXXNameMangler::mangleType(QualType nonCanon) {
Anders Carlsson4843e582009-03-10 17:07:44 +00001205 // Only operate on the canonical type!
John McCallb47f7482011-01-26 20:05:40 +00001206 QualType canon = nonCanon.getCanonicalType();
Anders Carlsson4843e582009-03-10 17:07:44 +00001207
John McCallb47f7482011-01-26 20:05:40 +00001208 SplitQualType split = canon.split();
1209 Qualifiers quals = split.second;
1210 const Type *ty = split.first;
1211
1212 bool isSubstitutable = quals || !isa<BuiltinType>(ty);
1213 if (isSubstitutable && mangleSubstitution(canon))
Anders Carlsson76967372009-09-17 00:43:46 +00001214 return;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001215
John McCallb47f7482011-01-26 20:05:40 +00001216 // If we're mangling a qualified array type, push the qualifiers to
1217 // the element type.
1218 if (quals && isa<ArrayType>(ty)) {
1219 ty = Context.getASTContext().getAsArrayType(canon);
1220 quals = Qualifiers();
1221
1222 // Note that we don't update canon: we want to add the
1223 // substitution at the canonical type.
1224 }
1225
1226 if (quals) {
1227 mangleQualifiers(quals);
John McCall0953e762009-09-24 19:53:00 +00001228 // Recurse: even if the qualified type isn't yet substitutable,
1229 // the unqualified type might be.
John McCallb47f7482011-01-26 20:05:40 +00001230 mangleType(QualType(ty, 0));
Anders Carlsson76967372009-09-17 00:43:46 +00001231 } else {
John McCallb47f7482011-01-26 20:05:40 +00001232 switch (ty->getTypeClass()) {
John McCallefe6aee2009-09-05 07:56:18 +00001233#define ABSTRACT_TYPE(CLASS, PARENT)
1234#define NON_CANONICAL_TYPE(CLASS, PARENT) \
Anders Carlsson76967372009-09-17 00:43:46 +00001235 case Type::CLASS: \
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +00001236 llvm_unreachable("can't mangle non-canonical type " #CLASS "Type"); \
Anders Carlsson76967372009-09-17 00:43:46 +00001237 return;
John McCallefe6aee2009-09-05 07:56:18 +00001238#define TYPE(CLASS, PARENT) \
Anders Carlsson76967372009-09-17 00:43:46 +00001239 case Type::CLASS: \
John McCallb47f7482011-01-26 20:05:40 +00001240 mangleType(static_cast<const CLASS##Type*>(ty)); \
Anders Carlsson76967372009-09-17 00:43:46 +00001241 break;
John McCallefe6aee2009-09-05 07:56:18 +00001242#include "clang/AST/TypeNodes.def"
Anders Carlsson76967372009-09-17 00:43:46 +00001243 }
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001244 }
Anders Carlsson76967372009-09-17 00:43:46 +00001245
1246 // Add the substitution.
John McCallb47f7482011-01-26 20:05:40 +00001247 if (isSubstitutable)
1248 addSubstitution(canon);
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001249}
1250
Douglas Gregor1b12a3b2010-05-26 05:11:13 +00001251void CXXNameMangler::mangleNameOrStandardSubstitution(const NamedDecl *ND) {
1252 if (!mangleStandardSubstitution(ND))
1253 mangleName(ND);
1254}
1255
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001256void CXXNameMangler::mangleType(const BuiltinType *T) {
John McCallefe6aee2009-09-05 07:56:18 +00001257 // <type> ::= <builtin-type>
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001258 // <builtin-type> ::= v # void
1259 // ::= w # wchar_t
1260 // ::= b # bool
1261 // ::= c # char
1262 // ::= a # signed char
1263 // ::= h # unsigned char
1264 // ::= s # short
1265 // ::= t # unsigned short
1266 // ::= i # int
1267 // ::= j # unsigned int
1268 // ::= l # long
1269 // ::= m # unsigned long
1270 // ::= x # long long, __int64
1271 // ::= y # unsigned long long, __int64
1272 // ::= n # __int128
1273 // UNSUPPORTED: ::= o # unsigned __int128
1274 // ::= f # float
1275 // ::= d # double
1276 // ::= e # long double, __float80
1277 // UNSUPPORTED: ::= g # __float128
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001278 // UNSUPPORTED: ::= Dd # IEEE 754r decimal floating point (64 bits)
1279 // UNSUPPORTED: ::= De # IEEE 754r decimal floating point (128 bits)
1280 // UNSUPPORTED: ::= Df # IEEE 754r decimal floating point (32 bits)
1281 // UNSUPPORTED: ::= Dh # IEEE 754r half-precision floating point (16 bits)
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001282 // ::= Di # char32_t
1283 // ::= Ds # char16_t
Anders Carlssone2923682010-11-04 04:31:32 +00001284 // ::= Dn # std::nullptr_t (i.e., decltype(nullptr))
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001285 // ::= u <source-name> # vendor extended type
1286 switch (T->getKind()) {
1287 case BuiltinType::Void: Out << 'v'; break;
1288 case BuiltinType::Bool: Out << 'b'; break;
1289 case BuiltinType::Char_U: case BuiltinType::Char_S: Out << 'c'; break;
1290 case BuiltinType::UChar: Out << 'h'; break;
1291 case BuiltinType::UShort: Out << 't'; break;
1292 case BuiltinType::UInt: Out << 'j'; break;
1293 case BuiltinType::ULong: Out << 'm'; break;
1294 case BuiltinType::ULongLong: Out << 'y'; break;
Chris Lattner2df9ced2009-04-30 02:43:43 +00001295 case BuiltinType::UInt128: Out << 'o'; break;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001296 case BuiltinType::SChar: Out << 'a'; break;
Chris Lattner3f59c972010-12-25 23:25:43 +00001297 case BuiltinType::WChar_S:
1298 case BuiltinType::WChar_U: Out << 'w'; break;
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001299 case BuiltinType::Char16: Out << "Ds"; break;
1300 case BuiltinType::Char32: Out << "Di"; break;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001301 case BuiltinType::Short: Out << 's'; break;
1302 case BuiltinType::Int: Out << 'i'; break;
1303 case BuiltinType::Long: Out << 'l'; break;
1304 case BuiltinType::LongLong: Out << 'x'; break;
Chris Lattner2df9ced2009-04-30 02:43:43 +00001305 case BuiltinType::Int128: Out << 'n'; break;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001306 case BuiltinType::Float: Out << 'f'; break;
1307 case BuiltinType::Double: Out << 'd'; break;
1308 case BuiltinType::LongDouble: Out << 'e'; break;
Anders Carlssone2923682010-11-04 04:31:32 +00001309 case BuiltinType::NullPtr: Out << "Dn"; break;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001310
1311 case BuiltinType::Overload:
1312 case BuiltinType::Dependent:
Mike Stump1eb44332009-09-09 15:08:12 +00001313 assert(false &&
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001314 "Overloaded and dependent types shouldn't get to name mangling");
1315 break;
Steve Naroff9533a7f2009-07-22 17:14:51 +00001316 case BuiltinType::ObjCId: Out << "11objc_object"; break;
1317 case BuiltinType::ObjCClass: Out << "10objc_class"; break;
Fariborz Jahanian13dcd002009-11-21 19:53:08 +00001318 case BuiltinType::ObjCSel: Out << "13objc_selector"; break;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001319 }
1320}
1321
John McCallefe6aee2009-09-05 07:56:18 +00001322// <type> ::= <function-type>
1323// <function-type> ::= F [Y] <bare-function-type> E
1324void CXXNameMangler::mangleType(const FunctionProtoType *T) {
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001325 Out << 'F';
Mike Stumpf5408fe2009-05-16 07:57:57 +00001326 // FIXME: We don't have enough information in the AST to produce the 'Y'
1327 // encoding for extern "C" function types.
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001328 mangleBareFunctionType(T, /*MangleReturnType=*/true);
1329 Out << 'E';
1330}
John McCallefe6aee2009-09-05 07:56:18 +00001331void CXXNameMangler::mangleType(const FunctionNoProtoType *T) {
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +00001332 llvm_unreachable("Can't mangle K&R function prototypes");
John McCallefe6aee2009-09-05 07:56:18 +00001333}
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001334void CXXNameMangler::mangleBareFunctionType(const FunctionType *T,
1335 bool MangleReturnType) {
John McCallefe6aee2009-09-05 07:56:18 +00001336 // We should never be mangling something without a prototype.
1337 const FunctionProtoType *Proto = cast<FunctionProtoType>(T);
1338
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001339 // <bare-function-type> ::= <signature type>+
1340 if (MangleReturnType)
John McCallefe6aee2009-09-05 07:56:18 +00001341 mangleType(Proto->getResultType());
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001342
Anders Carlsson93296682010-06-02 04:40:13 +00001343 if (Proto->getNumArgs() == 0 && !Proto->isVariadic()) {
Eli Friedmana7e68452010-08-22 01:00:03 +00001344 // <builtin-type> ::= v # void
Anders Carlssonc6c91bc2009-04-01 00:15:23 +00001345 Out << 'v';
1346 return;
1347 }
Mike Stump1eb44332009-09-09 15:08:12 +00001348
Douglas Gregor72564e72009-02-26 23:50:07 +00001349 for (FunctionProtoType::arg_type_iterator Arg = Proto->arg_type_begin(),
Mike Stump1eb44332009-09-09 15:08:12 +00001350 ArgEnd = Proto->arg_type_end();
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001351 Arg != ArgEnd; ++Arg)
1352 mangleType(*Arg);
Douglas Gregor219cc612009-02-13 01:28:03 +00001353
1354 // <builtin-type> ::= z # ellipsis
1355 if (Proto->isVariadic())
1356 Out << 'z';
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001357}
1358
John McCallefe6aee2009-09-05 07:56:18 +00001359// <type> ::= <class-enum-type>
Mike Stump1eb44332009-09-09 15:08:12 +00001360// <class-enum-type> ::= <name>
John McCalled976492009-12-04 22:46:56 +00001361void CXXNameMangler::mangleType(const UnresolvedUsingType *T) {
1362 mangleName(T->getDecl());
1363}
1364
1365// <type> ::= <class-enum-type>
1366// <class-enum-type> ::= <name>
John McCallefe6aee2009-09-05 07:56:18 +00001367void CXXNameMangler::mangleType(const EnumType *T) {
1368 mangleType(static_cast<const TagType*>(T));
1369}
1370void CXXNameMangler::mangleType(const RecordType *T) {
1371 mangleType(static_cast<const TagType*>(T));
1372}
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001373void CXXNameMangler::mangleType(const TagType *T) {
Eli Friedmanecb7e932009-12-11 18:00:57 +00001374 mangleName(T->getDecl());
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001375}
1376
John McCallefe6aee2009-09-05 07:56:18 +00001377// <type> ::= <array-type>
1378// <array-type> ::= A <positive dimension number> _ <element type>
1379// ::= A [<dimension expression>] _ <element type>
1380void CXXNameMangler::mangleType(const ConstantArrayType *T) {
1381 Out << 'A' << T->getSize() << '_';
1382 mangleType(T->getElementType());
1383}
1384void CXXNameMangler::mangleType(const VariableArrayType *T) {
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001385 Out << 'A';
Fariborz Jahanian7281d1f2010-11-02 16:54:00 +00001386 // decayed vla types (size 0) will just be skipped.
1387 if (T->getSizeExpr())
1388 mangleExpression(T->getSizeExpr());
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001389 Out << '_';
1390 mangleType(T->getElementType());
1391}
John McCallefe6aee2009-09-05 07:56:18 +00001392void CXXNameMangler::mangleType(const DependentSizedArrayType *T) {
1393 Out << 'A';
1394 mangleExpression(T->getSizeExpr());
1395 Out << '_';
1396 mangleType(T->getElementType());
1397}
1398void CXXNameMangler::mangleType(const IncompleteArrayType *T) {
Nick Lewycky271b6652010-09-05 03:40:33 +00001399 Out << "A_";
John McCallefe6aee2009-09-05 07:56:18 +00001400 mangleType(T->getElementType());
1401}
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001402
John McCallefe6aee2009-09-05 07:56:18 +00001403// <type> ::= <pointer-to-member-type>
1404// <pointer-to-member-type> ::= M <class type> <member type>
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001405void CXXNameMangler::mangleType(const MemberPointerType *T) {
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001406 Out << 'M';
1407 mangleType(QualType(T->getClass(), 0));
Anders Carlsson0e650012009-05-17 17:41:20 +00001408 QualType PointeeType = T->getPointeeType();
1409 if (const FunctionProtoType *FPT = dyn_cast<FunctionProtoType>(PointeeType)) {
John McCall0953e762009-09-24 19:53:00 +00001410 mangleQualifiers(Qualifiers::fromCVRMask(FPT->getTypeQuals()));
Douglas Gregor0a9a6d62011-01-26 17:36:28 +00001411 mangleRefQualifier(FPT->getRefQualifier());
Anders Carlsson0e650012009-05-17 17:41:20 +00001412 mangleType(FPT);
Anders Carlsson9d85b722010-06-02 04:29:50 +00001413
1414 // Itanium C++ ABI 5.1.8:
1415 //
1416 // The type of a non-static member function is considered to be different,
1417 // for the purposes of substitution, from the type of a namespace-scope or
1418 // static member function whose type appears similar. The types of two
1419 // non-static member functions are considered to be different, for the
1420 // purposes of substitution, if the functions are members of different
1421 // classes. In other words, for the purposes of substitution, the class of
1422 // which the function is a member is considered part of the type of
1423 // function.
1424
1425 // We increment the SeqID here to emulate adding an entry to the
1426 // substitution table. We can't actually add it because we don't want this
1427 // particular function type to be substituted.
1428 ++SeqID;
Mike Stump1eb44332009-09-09 15:08:12 +00001429 } else
Anders Carlsson0e650012009-05-17 17:41:20 +00001430 mangleType(PointeeType);
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001431}
1432
John McCallefe6aee2009-09-05 07:56:18 +00001433// <type> ::= <template-param>
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001434void CXXNameMangler::mangleType(const TemplateTypeParmType *T) {
Anders Carlsson0ccdf8d2009-09-27 00:38:53 +00001435 mangleTemplateParameter(T->getIndex());
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001436}
1437
Douglas Gregorc3069d62011-01-14 02:55:32 +00001438// <type> ::= <template-param>
1439void CXXNameMangler::mangleType(const SubstTemplateTypeParmPackType *T) {
1440 mangleTemplateParameter(T->getReplacedParameter()->getIndex());
1441}
1442
John McCallefe6aee2009-09-05 07:56:18 +00001443// <type> ::= P <type> # pointer-to
1444void CXXNameMangler::mangleType(const PointerType *T) {
1445 Out << 'P';
1446 mangleType(T->getPointeeType());
1447}
1448void CXXNameMangler::mangleType(const ObjCObjectPointerType *T) {
1449 Out << 'P';
1450 mangleType(T->getPointeeType());
1451}
1452
1453// <type> ::= R <type> # reference-to
1454void CXXNameMangler::mangleType(const LValueReferenceType *T) {
1455 Out << 'R';
1456 mangleType(T->getPointeeType());
1457}
1458
1459// <type> ::= O <type> # rvalue reference-to (C++0x)
1460void CXXNameMangler::mangleType(const RValueReferenceType *T) {
1461 Out << 'O';
1462 mangleType(T->getPointeeType());
1463}
1464
1465// <type> ::= C <type> # complex pair (C 2000)
1466void CXXNameMangler::mangleType(const ComplexType *T) {
1467 Out << 'C';
1468 mangleType(T->getElementType());
1469}
1470
Bob Wilsonc7df92d2010-11-12 17:24:43 +00001471// ARM's ABI for Neon vector types specifies that they should be mangled as
Bob Wilson57147a82010-11-16 00:32:18 +00001472// if they are structs (to match ARM's initial implementation). The
1473// vector type must be one of the special types predefined by ARM.
1474void CXXNameMangler::mangleNeonVectorType(const VectorType *T) {
Bob Wilsonc7df92d2010-11-12 17:24:43 +00001475 QualType EltType = T->getElementType();
Bob Wilson57147a82010-11-16 00:32:18 +00001476 assert(EltType->isBuiltinType() && "Neon vector element not a BuiltinType");
Bob Wilsonc7df92d2010-11-12 17:24:43 +00001477 const char *EltName = 0;
Bob Wilson491328c2010-11-12 17:24:46 +00001478 if (T->getVectorKind() == VectorType::NeonPolyVector) {
1479 switch (cast<BuiltinType>(EltType)->getKind()) {
Bob Wilson4cfaa5d2010-11-12 17:24:49 +00001480 case BuiltinType::SChar: EltName = "poly8_t"; break;
1481 case BuiltinType::Short: EltName = "poly16_t"; break;
Bob Wilson57147a82010-11-16 00:32:18 +00001482 default: llvm_unreachable("unexpected Neon polynomial vector element type");
Bob Wilson491328c2010-11-12 17:24:46 +00001483 }
1484 } else {
1485 switch (cast<BuiltinType>(EltType)->getKind()) {
Bob Wilson4cfaa5d2010-11-12 17:24:49 +00001486 case BuiltinType::SChar: EltName = "int8_t"; break;
1487 case BuiltinType::UChar: EltName = "uint8_t"; break;
1488 case BuiltinType::Short: EltName = "int16_t"; break;
1489 case BuiltinType::UShort: EltName = "uint16_t"; break;
1490 case BuiltinType::Int: EltName = "int32_t"; break;
1491 case BuiltinType::UInt: EltName = "uint32_t"; break;
1492 case BuiltinType::LongLong: EltName = "int64_t"; break;
1493 case BuiltinType::ULongLong: EltName = "uint64_t"; break;
1494 case BuiltinType::Float: EltName = "float32_t"; break;
Bob Wilson57147a82010-11-16 00:32:18 +00001495 default: llvm_unreachable("unexpected Neon vector element type");
Bob Wilson491328c2010-11-12 17:24:46 +00001496 }
Bob Wilsonc7df92d2010-11-12 17:24:43 +00001497 }
1498 const char *BaseName = 0;
Bob Wilson4cfaa5d2010-11-12 17:24:49 +00001499 unsigned BitSize = (T->getNumElements() *
Bob Wilson3a723022010-11-16 00:32:12 +00001500 getASTContext().getTypeSize(EltType));
Bob Wilsonc7df92d2010-11-12 17:24:43 +00001501 if (BitSize == 64)
1502 BaseName = "__simd64_";
Bob Wilson57147a82010-11-16 00:32:18 +00001503 else {
1504 assert(BitSize == 128 && "Neon vector type not 64 or 128 bits");
Bob Wilsonc7df92d2010-11-12 17:24:43 +00001505 BaseName = "__simd128_";
Bob Wilson57147a82010-11-16 00:32:18 +00001506 }
Bob Wilsonc7df92d2010-11-12 17:24:43 +00001507 Out << strlen(BaseName) + strlen(EltName);
1508 Out << BaseName << EltName;
Bob Wilsonc7df92d2010-11-12 17:24:43 +00001509}
1510
John McCallefe6aee2009-09-05 07:56:18 +00001511// GNU extension: vector types
Chris Lattner788b0fd2010-06-23 06:00:24 +00001512// <type> ::= <vector-type>
1513// <vector-type> ::= Dv <positive dimension number> _
1514// <extended element type>
1515// ::= Dv [<dimension expression>] _ <element type>
1516// <extended element type> ::= <element type>
1517// ::= p # AltiVec vector pixel
John McCallefe6aee2009-09-05 07:56:18 +00001518void CXXNameMangler::mangleType(const VectorType *T) {
Bob Wilson491328c2010-11-12 17:24:46 +00001519 if ((T->getVectorKind() == VectorType::NeonVector ||
Bob Wilson57147a82010-11-16 00:32:18 +00001520 T->getVectorKind() == VectorType::NeonPolyVector)) {
1521 mangleNeonVectorType(T);
Bob Wilsonc7df92d2010-11-12 17:24:43 +00001522 return;
Bob Wilson57147a82010-11-16 00:32:18 +00001523 }
Nick Lewycky0e5f0672010-03-26 07:18:04 +00001524 Out << "Dv" << T->getNumElements() << '_';
Bob Wilsone86d78c2010-11-10 21:56:12 +00001525 if (T->getVectorKind() == VectorType::AltiVecPixel)
Chris Lattner788b0fd2010-06-23 06:00:24 +00001526 Out << 'p';
Bob Wilsone86d78c2010-11-10 21:56:12 +00001527 else if (T->getVectorKind() == VectorType::AltiVecBool)
Chris Lattner788b0fd2010-06-23 06:00:24 +00001528 Out << 'b';
1529 else
1530 mangleType(T->getElementType());
John McCallefe6aee2009-09-05 07:56:18 +00001531}
1532void CXXNameMangler::mangleType(const ExtVectorType *T) {
1533 mangleType(static_cast<const VectorType*>(T));
1534}
1535void CXXNameMangler::mangleType(const DependentSizedExtVectorType *T) {
Nick Lewycky0e5f0672010-03-26 07:18:04 +00001536 Out << "Dv";
1537 mangleExpression(T->getSizeExpr());
1538 Out << '_';
John McCallefe6aee2009-09-05 07:56:18 +00001539 mangleType(T->getElementType());
1540}
1541
Douglas Gregor7536dd52010-12-20 02:24:11 +00001542void CXXNameMangler::mangleType(const PackExpansionType *T) {
Douglas Gregor4fc48662011-01-13 16:39:34 +00001543 // <type> ::= Dp <type> # pack expansion (C++0x)
Douglas Gregor255c2692011-01-13 17:44:36 +00001544 Out << "Dp";
Douglas Gregor7536dd52010-12-20 02:24:11 +00001545 mangleType(T->getPattern());
1546}
1547
Anders Carlssona40c5e42009-03-07 22:03:21 +00001548void CXXNameMangler::mangleType(const ObjCInterfaceType *T) {
1549 mangleSourceName(T->getDecl()->getIdentifier());
1550}
1551
John McCallc12c5bb2010-05-15 11:32:37 +00001552void CXXNameMangler::mangleType(const ObjCObjectType *T) {
John McCallc00c1f62010-05-15 17:06:29 +00001553 // We don't allow overloading by different protocol qualification,
1554 // so mangling them isn't necessary.
John McCallc12c5bb2010-05-15 11:32:37 +00001555 mangleType(T->getBaseType());
1556}
1557
John McCallefe6aee2009-09-05 07:56:18 +00001558void CXXNameMangler::mangleType(const BlockPointerType *T) {
Anders Carlssonf28c6872009-12-23 22:31:44 +00001559 Out << "U13block_pointer";
1560 mangleType(T->getPointeeType());
John McCallefe6aee2009-09-05 07:56:18 +00001561}
1562
John McCall31f17ec2010-04-27 00:57:59 +00001563void CXXNameMangler::mangleType(const InjectedClassNameType *T) {
1564 // Mangle injected class name types as if the user had written the
1565 // specialization out fully. It may not actually be possible to see
1566 // this mangling, though.
1567 mangleType(T->getInjectedSpecializationType());
1568}
1569
John McCallefe6aee2009-09-05 07:56:18 +00001570void CXXNameMangler::mangleType(const TemplateSpecializationType *T) {
Douglas Gregor1e9268e2010-04-28 05:58:56 +00001571 if (TemplateDecl *TD = T->getTemplateName().getAsTemplateDecl()) {
1572 mangleName(TD, T->getArgs(), T->getNumArgs());
1573 } else {
1574 if (mangleSubstitution(QualType(T, 0)))
1575 return;
Sean Huntc3021132010-05-05 15:23:54 +00001576
Douglas Gregor1e9268e2010-04-28 05:58:56 +00001577 mangleTemplatePrefix(T->getTemplateName());
Sean Huntc3021132010-05-05 15:23:54 +00001578
Douglas Gregor1e9268e2010-04-28 05:58:56 +00001579 // FIXME: GCC does not appear to mangle the template arguments when
1580 // the template in question is a dependent template name. Should we
1581 // emulate that badness?
1582 mangleTemplateArgs(T->getTemplateName(), T->getArgs(), T->getNumArgs());
1583 addSubstitution(QualType(T, 0));
1584 }
John McCallefe6aee2009-09-05 07:56:18 +00001585}
1586
Douglas Gregor4714c122010-03-31 17:34:00 +00001587void CXXNameMangler::mangleType(const DependentNameType *T) {
Anders Carlssonae352482009-09-26 02:26:02 +00001588 // Typename types are always nested
1589 Out << 'N';
John McCall33500952010-06-11 00:33:02 +00001590 mangleUnresolvedScope(T->getQualifier());
1591 mangleSourceName(T->getIdentifier());
1592 Out << 'E';
1593}
John McCall6ab30e02010-06-09 07:26:17 +00001594
John McCall33500952010-06-11 00:33:02 +00001595void CXXNameMangler::mangleType(const DependentTemplateSpecializationType *T) {
1596 // Dependently-scoped template types are always nested
1597 Out << 'N';
1598
1599 // TODO: avoid making this TemplateName.
1600 TemplateName Prefix =
1601 getASTContext().getDependentTemplateName(T->getQualifier(),
1602 T->getIdentifier());
1603 mangleTemplatePrefix(Prefix);
1604
1605 // FIXME: GCC does not appear to mangle the template arguments when
1606 // the template in question is a dependent template name. Should we
1607 // emulate that badness?
1608 mangleTemplateArgs(Prefix, T->getArgs(), T->getNumArgs());
Anders Carlssonae352482009-09-26 02:26:02 +00001609 Out << 'E';
John McCallefe6aee2009-09-05 07:56:18 +00001610}
1611
John McCallad5e7382010-03-01 23:49:17 +00001612void CXXNameMangler::mangleType(const TypeOfType *T) {
1613 // FIXME: this is pretty unsatisfactory, but there isn't an obvious
1614 // "extension with parameters" mangling.
1615 Out << "u6typeof";
1616}
1617
1618void CXXNameMangler::mangleType(const TypeOfExprType *T) {
1619 // FIXME: this is pretty unsatisfactory, but there isn't an obvious
1620 // "extension with parameters" mangling.
1621 Out << "u6typeof";
1622}
1623
1624void CXXNameMangler::mangleType(const DecltypeType *T) {
1625 Expr *E = T->getUnderlyingExpr();
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001626
John McCallad5e7382010-03-01 23:49:17 +00001627 // type ::= Dt <expression> E # decltype of an id-expression
1628 // # or class member access
1629 // ::= DT <expression> E # decltype of an expression
1630
1631 // This purports to be an exhaustive list of id-expressions and
1632 // class member accesses. Note that we do not ignore parentheses;
1633 // parentheses change the semantics of decltype for these
1634 // expressions (and cause the mangler to use the other form).
1635 if (isa<DeclRefExpr>(E) ||
1636 isa<MemberExpr>(E) ||
1637 isa<UnresolvedLookupExpr>(E) ||
1638 isa<DependentScopeDeclRefExpr>(E) ||
1639 isa<CXXDependentScopeMemberExpr>(E) ||
1640 isa<UnresolvedMemberExpr>(E))
1641 Out << "Dt";
1642 else
1643 Out << "DT";
1644 mangleExpression(E);
1645 Out << 'E';
1646}
1647
Richard Smith34b41d92011-02-20 03:19:35 +00001648void CXXNameMangler::mangleType(const AutoType *T) {
1649 QualType D = T->getDeducedType();
1650 assert(!D.isNull() && "can't mangle undeduced auto type");
1651 mangleType(D);
1652}
1653
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001654void CXXNameMangler::mangleIntegerLiteral(QualType T,
Anders Carlssone170ba72009-12-14 01:45:37 +00001655 const llvm::APSInt &Value) {
1656 // <expr-primary> ::= L <type> <value number> E # integer literal
1657 Out << 'L';
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001658
Anders Carlssone170ba72009-12-14 01:45:37 +00001659 mangleType(T);
1660 if (T->isBooleanType()) {
1661 // Boolean values are encoded as 0/1.
1662 Out << (Value.getBoolValue() ? '1' : '0');
1663 } else {
John McCall0512e482010-07-14 04:20:34 +00001664 mangleNumber(Value);
Anders Carlssone170ba72009-12-14 01:45:37 +00001665 }
1666 Out << 'E';
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001667
Anders Carlssone170ba72009-12-14 01:45:37 +00001668}
1669
John McCall2f27bf82010-02-04 02:56:29 +00001670/// Mangles a member expression. Implicit accesses are not handled,
1671/// but that should be okay, because you shouldn't be able to
1672/// make an implicit access in a function template declaration.
John McCall2f27bf82010-02-04 02:56:29 +00001673void CXXNameMangler::mangleMemberExpr(const Expr *Base,
1674 bool IsArrow,
1675 NestedNameSpecifier *Qualifier,
1676 DeclarationName Member,
1677 unsigned Arity) {
John McCalle1e342f2010-03-01 19:12:25 +00001678 // gcc-4.4 uses 'dt' for dot expressions, which is reasonable.
1679 // OTOH, gcc also mangles the name as an expression.
1680 Out << (IsArrow ? "pt" : "dt");
John McCall2f27bf82010-02-04 02:56:29 +00001681 mangleExpression(Base);
1682 mangleUnresolvedName(Qualifier, Member, Arity);
1683}
1684
John McCall5e1e89b2010-08-18 19:18:59 +00001685void CXXNameMangler::mangleExpression(const Expr *E, unsigned Arity) {
Anders Carlssond553f8c2009-09-21 01:21:10 +00001686 // <expression> ::= <unary operator-name> <expression>
John McCall09cc1412010-02-03 00:55:45 +00001687 // ::= <binary operator-name> <expression> <expression>
1688 // ::= <trinary operator-name> <expression> <expression> <expression>
Eli Friedmana7e68452010-08-22 01:00:03 +00001689 // ::= cl <expression>* E # call
Anders Carlssond553f8c2009-09-21 01:21:10 +00001690 // ::= cv <type> expression # conversion with one argument
1691 // ::= cv <type> _ <expression>* E # conversion with a different number of arguments
Eli Friedmana7e68452010-08-22 01:00:03 +00001692 // ::= st <type> # sizeof (a type)
Anders Carlssond553f8c2009-09-21 01:21:10 +00001693 // ::= at <type> # alignof (a type)
1694 // ::= <template-param>
1695 // ::= <function-param>
1696 // ::= sr <type> <unqualified-name> # dependent name
1697 // ::= sr <type> <unqualified-name> <template-args> # dependent template-id
1698 // ::= sZ <template-param> # size of a parameter pack
Douglas Gregor4fc48662011-01-13 16:39:34 +00001699 // ::= sZ <function-param> # size of a function parameter pack
John McCall09cc1412010-02-03 00:55:45 +00001700 // ::= <expr-primary>
John McCall1dd73832010-02-04 01:42:13 +00001701 // <expr-primary> ::= L <type> <value number> E # integer literal
1702 // ::= L <type <value float> E # floating literal
1703 // ::= L <mangled-name> E # external name
Anders Carlssond553f8c2009-09-21 01:21:10 +00001704 switch (E->getStmtClass()) {
John McCall6ae1f352010-04-09 22:26:14 +00001705 case Expr::NoStmtClass:
John McCall63c00d72011-02-09 08:16:59 +00001706#define ABSTRACT_STMT(Type)
John McCall6ae1f352010-04-09 22:26:14 +00001707#define EXPR(Type, Base)
1708#define STMT(Type, Base) \
1709 case Expr::Type##Class:
Sean Hunt4bfe1962010-05-05 15:24:00 +00001710#include "clang/AST/StmtNodes.inc"
John McCall0512e482010-07-14 04:20:34 +00001711 // fallthrough
1712
1713 // These all can only appear in local or variable-initialization
1714 // contexts and so should never appear in a mangling.
1715 case Expr::AddrLabelExprClass:
1716 case Expr::BlockDeclRefExprClass:
1717 case Expr::CXXThisExprClass:
1718 case Expr::DesignatedInitExprClass:
1719 case Expr::ImplicitValueInitExprClass:
1720 case Expr::InitListExprClass:
1721 case Expr::ParenListExprClass:
1722 case Expr::CXXScalarValueInitExprClass:
John McCall09cc1412010-02-03 00:55:45 +00001723 llvm_unreachable("unexpected statement kind");
1724 break;
1725
John McCall0512e482010-07-14 04:20:34 +00001726 // FIXME: invent manglings for all these.
1727 case Expr::BlockExprClass:
1728 case Expr::CXXPseudoDestructorExprClass:
1729 case Expr::ChooseExprClass:
1730 case Expr::CompoundLiteralExprClass:
1731 case Expr::ExtVectorElementExprClass:
1732 case Expr::ObjCEncodeExprClass:
John McCall0512e482010-07-14 04:20:34 +00001733 case Expr::ObjCIsaExprClass:
1734 case Expr::ObjCIvarRefExprClass:
1735 case Expr::ObjCMessageExprClass:
1736 case Expr::ObjCPropertyRefExprClass:
1737 case Expr::ObjCProtocolExprClass:
1738 case Expr::ObjCSelectorExprClass:
1739 case Expr::ObjCStringLiteralClass:
John McCall0512e482010-07-14 04:20:34 +00001740 case Expr::OffsetOfExprClass:
1741 case Expr::PredefinedExprClass:
1742 case Expr::ShuffleVectorExprClass:
1743 case Expr::StmtExprClass:
John McCall0512e482010-07-14 04:20:34 +00001744 case Expr::UnaryTypeTraitExprClass:
Francois Pichet6ad6f282010-12-07 00:08:36 +00001745 case Expr::BinaryTypeTraitExprClass:
Francois Pichet9be88402010-09-08 23:47:05 +00001746 case Expr::VAArgExprClass:
Sebastian Redl2e156222010-09-10 20:55:43 +00001747 case Expr::CXXUuidofExprClass:
Peter Collingbournee08ce652011-02-09 21:07:24 +00001748 case Expr::CXXNoexceptExprClass:
1749 case Expr::CUDAKernelCallExprClass: {
John McCall6ae1f352010-04-09 22:26:14 +00001750 // As bad as this diagnostic is, it's better than crashing.
1751 Diagnostic &Diags = Context.getDiags();
1752 unsigned DiagID = Diags.getCustomDiagID(Diagnostic::Error,
1753 "cannot yet mangle expression type %0");
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +00001754 Diags.Report(E->getExprLoc(), DiagID)
John McCall739bf092010-04-10 09:39:25 +00001755 << E->getStmtClassName() << E->getSourceRange();
John McCall6ae1f352010-04-09 22:26:14 +00001756 break;
1757 }
1758
John McCall56ca35d2011-02-17 10:25:35 +00001759 // Even gcc-4.5 doesn't mangle this.
1760 case Expr::BinaryConditionalOperatorClass: {
1761 Diagnostic &Diags = Context.getDiags();
1762 unsigned DiagID =
1763 Diags.getCustomDiagID(Diagnostic::Error,
1764 "?: operator with omitted middle operand cannot be mangled");
1765 Diags.Report(E->getExprLoc(), DiagID)
1766 << E->getStmtClassName() << E->getSourceRange();
1767 break;
1768 }
1769
1770 // These are used for internal purposes and cannot be meaningfully mangled.
John McCall7cd7d1a2010-11-15 23:31:06 +00001771 case Expr::OpaqueValueExprClass:
1772 llvm_unreachable("cannot mangle opaque value; mangling wrong thing?");
1773
John McCall0512e482010-07-14 04:20:34 +00001774 case Expr::CXXDefaultArgExprClass:
John McCall5e1e89b2010-08-18 19:18:59 +00001775 mangleExpression(cast<CXXDefaultArgExpr>(E)->getExpr(), Arity);
John McCall0512e482010-07-14 04:20:34 +00001776 break;
1777
1778 case Expr::CXXMemberCallExprClass: // fallthrough
John McCall1dd73832010-02-04 01:42:13 +00001779 case Expr::CallExprClass: {
1780 const CallExpr *CE = cast<CallExpr>(E);
1781 Out << "cl";
John McCall5e1e89b2010-08-18 19:18:59 +00001782 mangleExpression(CE->getCallee(), CE->getNumArgs());
John McCall1dd73832010-02-04 01:42:13 +00001783 for (unsigned I = 0, N = CE->getNumArgs(); I != N; ++I)
1784 mangleExpression(CE->getArg(I));
Benjamin Kramer35f59b62010-04-10 16:03:31 +00001785 Out << 'E';
John McCall09cc1412010-02-03 00:55:45 +00001786 break;
John McCall1dd73832010-02-04 01:42:13 +00001787 }
John McCall09cc1412010-02-03 00:55:45 +00001788
John McCall0512e482010-07-14 04:20:34 +00001789 case Expr::CXXNewExprClass: {
1790 // Proposal from David Vandervoorde, 2010.06.30
1791 const CXXNewExpr *New = cast<CXXNewExpr>(E);
1792 if (New->isGlobalNew()) Out << "gs";
1793 Out << (New->isArray() ? "na" : "nw");
1794 for (CXXNewExpr::const_arg_iterator I = New->placement_arg_begin(),
1795 E = New->placement_arg_end(); I != E; ++I)
1796 mangleExpression(*I);
1797 Out << '_';
1798 mangleType(New->getAllocatedType());
1799 if (New->hasInitializer()) {
1800 Out << "pi";
1801 for (CXXNewExpr::const_arg_iterator I = New->constructor_arg_begin(),
1802 E = New->constructor_arg_end(); I != E; ++I)
1803 mangleExpression(*I);
1804 }
1805 Out << 'E';
1806 break;
1807 }
1808
John McCall2f27bf82010-02-04 02:56:29 +00001809 case Expr::MemberExprClass: {
1810 const MemberExpr *ME = cast<MemberExpr>(E);
1811 mangleMemberExpr(ME->getBase(), ME->isArrow(),
1812 ME->getQualifier(), ME->getMemberDecl()->getDeclName(),
John McCall5e1e89b2010-08-18 19:18:59 +00001813 Arity);
John McCall2f27bf82010-02-04 02:56:29 +00001814 break;
1815 }
1816
1817 case Expr::UnresolvedMemberExprClass: {
1818 const UnresolvedMemberExpr *ME = cast<UnresolvedMemberExpr>(E);
1819 mangleMemberExpr(ME->getBase(), ME->isArrow(),
1820 ME->getQualifier(), ME->getMemberName(),
John McCall5e1e89b2010-08-18 19:18:59 +00001821 Arity);
John McCall6dbce192010-08-20 00:17:19 +00001822 if (ME->hasExplicitTemplateArgs())
1823 mangleTemplateArgs(ME->getExplicitTemplateArgs());
John McCall2f27bf82010-02-04 02:56:29 +00001824 break;
1825 }
1826
1827 case Expr::CXXDependentScopeMemberExprClass: {
1828 const CXXDependentScopeMemberExpr *ME
1829 = cast<CXXDependentScopeMemberExpr>(E);
1830 mangleMemberExpr(ME->getBase(), ME->isArrow(),
1831 ME->getQualifier(), ME->getMember(),
John McCall5e1e89b2010-08-18 19:18:59 +00001832 Arity);
John McCall6dbce192010-08-20 00:17:19 +00001833 if (ME->hasExplicitTemplateArgs())
1834 mangleTemplateArgs(ME->getExplicitTemplateArgs());
John McCall2f27bf82010-02-04 02:56:29 +00001835 break;
1836 }
1837
John McCall1dd73832010-02-04 01:42:13 +00001838 case Expr::UnresolvedLookupExprClass: {
John McCalla3218e72010-02-04 01:48:38 +00001839 // The ABI doesn't cover how to mangle overload sets, so we mangle
1840 // using something as close as possible to the original lookup
1841 // expression.
John McCall1dd73832010-02-04 01:42:13 +00001842 const UnresolvedLookupExpr *ULE = cast<UnresolvedLookupExpr>(E);
John McCall5e1e89b2010-08-18 19:18:59 +00001843 mangleUnresolvedName(ULE->getQualifier(), ULE->getName(), Arity);
John McCall6dbce192010-08-20 00:17:19 +00001844 if (ULE->hasExplicitTemplateArgs())
1845 mangleTemplateArgs(ULE->getExplicitTemplateArgs());
John McCall1dd73832010-02-04 01:42:13 +00001846 break;
1847 }
1848
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001849 case Expr::CXXUnresolvedConstructExprClass: {
John McCall1dd73832010-02-04 01:42:13 +00001850 const CXXUnresolvedConstructExpr *CE = cast<CXXUnresolvedConstructExpr>(E);
1851 unsigned N = CE->arg_size();
1852
1853 Out << "cv";
1854 mangleType(CE->getType());
Benjamin Kramer35f59b62010-04-10 16:03:31 +00001855 if (N != 1) Out << '_';
John McCall1dd73832010-02-04 01:42:13 +00001856 for (unsigned I = 0; I != N; ++I) mangleExpression(CE->getArg(I));
Benjamin Kramer35f59b62010-04-10 16:03:31 +00001857 if (N != 1) Out << 'E';
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001858 break;
John McCall1dd73832010-02-04 01:42:13 +00001859 }
John McCall09cc1412010-02-03 00:55:45 +00001860
John McCall1dd73832010-02-04 01:42:13 +00001861 case Expr::CXXTemporaryObjectExprClass:
1862 case Expr::CXXConstructExprClass: {
1863 const CXXConstructExpr *CE = cast<CXXConstructExpr>(E);
1864 unsigned N = CE->getNumArgs();
1865
1866 Out << "cv";
1867 mangleType(CE->getType());
Benjamin Kramer35f59b62010-04-10 16:03:31 +00001868 if (N != 1) Out << '_';
John McCall1dd73832010-02-04 01:42:13 +00001869 for (unsigned I = 0; I != N; ++I) mangleExpression(CE->getArg(I));
Benjamin Kramer35f59b62010-04-10 16:03:31 +00001870 if (N != 1) Out << 'E';
John McCall09cc1412010-02-03 00:55:45 +00001871 break;
John McCall1dd73832010-02-04 01:42:13 +00001872 }
1873
1874 case Expr::SizeOfAlignOfExprClass: {
1875 const SizeOfAlignOfExpr *SAE = cast<SizeOfAlignOfExpr>(E);
Benjamin Kramer35f59b62010-04-10 16:03:31 +00001876 if (SAE->isSizeOf()) Out << 's';
1877 else Out << 'a';
John McCall1dd73832010-02-04 01:42:13 +00001878 if (SAE->isArgumentType()) {
Benjamin Kramer35f59b62010-04-10 16:03:31 +00001879 Out << 't';
John McCall1dd73832010-02-04 01:42:13 +00001880 mangleType(SAE->getArgumentType());
1881 } else {
Benjamin Kramer35f59b62010-04-10 16:03:31 +00001882 Out << 'z';
John McCall1dd73832010-02-04 01:42:13 +00001883 mangleExpression(SAE->getArgumentExpr());
1884 }
1885 break;
1886 }
Anders Carlssona7694082009-11-06 02:50:19 +00001887
John McCall0512e482010-07-14 04:20:34 +00001888 case Expr::CXXThrowExprClass: {
1889 const CXXThrowExpr *TE = cast<CXXThrowExpr>(E);
1890
1891 // Proposal from David Vandervoorde, 2010.06.30
1892 if (TE->getSubExpr()) {
1893 Out << "tw";
1894 mangleExpression(TE->getSubExpr());
1895 } else {
1896 Out << "tr";
1897 }
1898 break;
1899 }
1900
1901 case Expr::CXXTypeidExprClass: {
1902 const CXXTypeidExpr *TIE = cast<CXXTypeidExpr>(E);
1903
1904 // Proposal from David Vandervoorde, 2010.06.30
1905 if (TIE->isTypeOperand()) {
1906 Out << "ti";
1907 mangleType(TIE->getTypeOperand());
1908 } else {
1909 Out << "te";
1910 mangleExpression(TIE->getExprOperand());
1911 }
1912 break;
1913 }
1914
1915 case Expr::CXXDeleteExprClass: {
1916 const CXXDeleteExpr *DE = cast<CXXDeleteExpr>(E);
1917
1918 // Proposal from David Vandervoorde, 2010.06.30
1919 if (DE->isGlobalDelete()) Out << "gs";
1920 Out << (DE->isArrayForm() ? "da" : "dl");
1921 mangleExpression(DE->getArgument());
1922 break;
1923 }
1924
Anders Carlssone170ba72009-12-14 01:45:37 +00001925 case Expr::UnaryOperatorClass: {
1926 const UnaryOperator *UO = cast<UnaryOperator>(E);
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001927 mangleOperatorName(UnaryOperator::getOverloadedOperator(UO->getOpcode()),
Anders Carlssone170ba72009-12-14 01:45:37 +00001928 /*Arity=*/1);
1929 mangleExpression(UO->getSubExpr());
1930 break;
1931 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001932
John McCall0512e482010-07-14 04:20:34 +00001933 case Expr::ArraySubscriptExprClass: {
1934 const ArraySubscriptExpr *AE = cast<ArraySubscriptExpr>(E);
1935
1936 // Array subscript is treated as a syntactically wierd form of
1937 // binary operator.
1938 Out << "ix";
1939 mangleExpression(AE->getLHS());
1940 mangleExpression(AE->getRHS());
1941 break;
1942 }
1943
1944 case Expr::CompoundAssignOperatorClass: // fallthrough
Anders Carlssone170ba72009-12-14 01:45:37 +00001945 case Expr::BinaryOperatorClass: {
1946 const BinaryOperator *BO = cast<BinaryOperator>(E);
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001947 mangleOperatorName(BinaryOperator::getOverloadedOperator(BO->getOpcode()),
Anders Carlssone170ba72009-12-14 01:45:37 +00001948 /*Arity=*/2);
1949 mangleExpression(BO->getLHS());
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001950 mangleExpression(BO->getRHS());
Anders Carlssone170ba72009-12-14 01:45:37 +00001951 break;
John McCall2f27bf82010-02-04 02:56:29 +00001952 }
Anders Carlssone170ba72009-12-14 01:45:37 +00001953
1954 case Expr::ConditionalOperatorClass: {
1955 const ConditionalOperator *CO = cast<ConditionalOperator>(E);
1956 mangleOperatorName(OO_Conditional, /*Arity=*/3);
1957 mangleExpression(CO->getCond());
John McCall5e1e89b2010-08-18 19:18:59 +00001958 mangleExpression(CO->getLHS(), Arity);
1959 mangleExpression(CO->getRHS(), Arity);
Anders Carlssone170ba72009-12-14 01:45:37 +00001960 break;
1961 }
1962
Douglas Gregor46287c72010-01-29 16:37:09 +00001963 case Expr::ImplicitCastExprClass: {
John McCall5e1e89b2010-08-18 19:18:59 +00001964 mangleExpression(cast<ImplicitCastExpr>(E)->getSubExpr(), Arity);
Douglas Gregor46287c72010-01-29 16:37:09 +00001965 break;
1966 }
1967
1968 case Expr::CStyleCastExprClass:
1969 case Expr::CXXStaticCastExprClass:
1970 case Expr::CXXDynamicCastExprClass:
1971 case Expr::CXXReinterpretCastExprClass:
1972 case Expr::CXXConstCastExprClass:
1973 case Expr::CXXFunctionalCastExprClass: {
1974 const ExplicitCastExpr *ECE = cast<ExplicitCastExpr>(E);
1975 Out << "cv";
1976 mangleType(ECE->getType());
1977 mangleExpression(ECE->getSubExpr());
1978 break;
1979 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001980
Anders Carlsson58040a52009-12-16 05:48:46 +00001981 case Expr::CXXOperatorCallExprClass: {
1982 const CXXOperatorCallExpr *CE = cast<CXXOperatorCallExpr>(E);
1983 unsigned NumArgs = CE->getNumArgs();
1984 mangleOperatorName(CE->getOperator(), /*Arity=*/NumArgs);
1985 // Mangle the arguments.
1986 for (unsigned i = 0; i != NumArgs; ++i)
1987 mangleExpression(CE->getArg(i));
1988 break;
1989 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001990
Anders Carlssona7694082009-11-06 02:50:19 +00001991 case Expr::ParenExprClass:
John McCall5e1e89b2010-08-18 19:18:59 +00001992 mangleExpression(cast<ParenExpr>(E)->getSubExpr(), Arity);
Anders Carlssona7694082009-11-06 02:50:19 +00001993 break;
1994
Anders Carlssond553f8c2009-09-21 01:21:10 +00001995 case Expr::DeclRefExprClass: {
Douglas Gregor5ed1bc32010-02-28 21:40:32 +00001996 const NamedDecl *D = cast<DeclRefExpr>(E)->getDecl();
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00001997
Anders Carlssond553f8c2009-09-21 01:21:10 +00001998 switch (D->getKind()) {
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001999 default:
Douglas Gregor5ed1bc32010-02-28 21:40:32 +00002000 // <expr-primary> ::= L <mangled-name> E # external name
2001 Out << 'L';
2002 mangle(D, "_Z");
2003 Out << 'E';
2004 break;
2005
John McCall3dc7e7b2010-07-24 01:17:35 +00002006 case Decl::EnumConstant: {
2007 const EnumConstantDecl *ED = cast<EnumConstantDecl>(D);
2008 mangleIntegerLiteral(ED->getType(), ED->getInitVal());
2009 break;
2010 }
2011
Anders Carlssond553f8c2009-09-21 01:21:10 +00002012 case Decl::NonTypeTemplateParm: {
2013 const NonTypeTemplateParmDecl *PD = cast<NonTypeTemplateParmDecl>(D);
Anders Carlsson0ccdf8d2009-09-27 00:38:53 +00002014 mangleTemplateParameter(PD->getIndex());
Anders Carlssond553f8c2009-09-21 01:21:10 +00002015 break;
2016 }
2017
2018 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002019
Anders Carlsson50755b02009-09-27 20:11:34 +00002020 break;
Anders Carlssond553f8c2009-09-21 01:21:10 +00002021 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002022
Douglas Gregorc7793c72011-01-15 01:15:58 +00002023 case Expr::SubstNonTypeTemplateParmPackExprClass:
2024 mangleTemplateParameter(
2025 cast<SubstNonTypeTemplateParmPackExpr>(E)->getParameterPack()->getIndex());
2026 break;
2027
John McCall865d4472009-11-19 22:55:06 +00002028 case Expr::DependentScopeDeclRefExprClass: {
2029 const DependentScopeDeclRefExpr *DRE = cast<DependentScopeDeclRefExpr>(E);
Douglas Gregor4b2ccfc2010-02-28 22:05:49 +00002030 NestedNameSpecifier *NNS = DRE->getQualifier();
2031 const Type *QTy = NNS->getAsType();
2032
2033 // When we're dealing with a nested-name-specifier that has just a
2034 // dependent identifier in it, mangle that as a typename. FIXME:
2035 // It isn't clear that we ever actually want to have such a
2036 // nested-name-specifier; why not just represent it as a typename type?
2037 if (!QTy && NNS->getAsIdentifier() && NNS->getPrefix()) {
Douglas Gregor4a2023f2010-03-31 20:19:30 +00002038 QTy = getASTContext().getDependentNameType(ETK_Typename,
2039 NNS->getPrefix(),
2040 NNS->getAsIdentifier())
Douglas Gregor4b2ccfc2010-02-28 22:05:49 +00002041 .getTypePtr();
2042 }
Anders Carlsson50755b02009-09-27 20:11:34 +00002043 assert(QTy && "Qualifier was not type!");
2044
John McCall6dbce192010-08-20 00:17:19 +00002045 // ::= sr <type> <unqualified-name> # dependent name
2046 // ::= sr <type> <unqualified-name> <template-args> # dependent template-id
Anders Carlsson50755b02009-09-27 20:11:34 +00002047 Out << "sr";
2048 mangleType(QualType(QTy, 0));
John McCall5e1e89b2010-08-18 19:18:59 +00002049 mangleUnqualifiedName(0, DRE->getDeclName(), Arity);
John McCall6dbce192010-08-20 00:17:19 +00002050 if (DRE->hasExplicitTemplateArgs())
2051 mangleTemplateArgs(DRE->getExplicitTemplateArgs());
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002052
Anders Carlsson50755b02009-09-27 20:11:34 +00002053 break;
2054 }
2055
John McCalld9307602010-04-09 22:54:09 +00002056 case Expr::CXXBindTemporaryExprClass:
2057 mangleExpression(cast<CXXBindTemporaryExpr>(E)->getSubExpr());
2058 break;
2059
John McCall4765fa02010-12-06 08:20:24 +00002060 case Expr::ExprWithCleanupsClass:
2061 mangleExpression(cast<ExprWithCleanups>(E)->getSubExpr(), Arity);
John McCalld9307602010-04-09 22:54:09 +00002062 break;
2063
John McCall1dd73832010-02-04 01:42:13 +00002064 case Expr::FloatingLiteralClass: {
2065 const FloatingLiteral *FL = cast<FloatingLiteral>(E);
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002066 Out << 'L';
John McCall1dd73832010-02-04 01:42:13 +00002067 mangleType(FL->getType());
John McCall0512e482010-07-14 04:20:34 +00002068 mangleFloat(FL->getValue());
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002069 Out << 'E';
John McCall1dd73832010-02-04 01:42:13 +00002070 break;
2071 }
2072
John McCallde810632010-04-09 21:48:08 +00002073 case Expr::CharacterLiteralClass:
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002074 Out << 'L';
John McCallde810632010-04-09 21:48:08 +00002075 mangleType(E->getType());
2076 Out << cast<CharacterLiteral>(E)->getValue();
2077 Out << 'E';
2078 break;
2079
2080 case Expr::CXXBoolLiteralExprClass:
2081 Out << "Lb";
2082 Out << (cast<CXXBoolLiteralExpr>(E)->getValue() ? '1' : '0');
2083 Out << 'E';
2084 break;
2085
John McCall0512e482010-07-14 04:20:34 +00002086 case Expr::IntegerLiteralClass: {
2087 llvm::APSInt Value(cast<IntegerLiteral>(E)->getValue());
2088 if (E->getType()->isSignedIntegerType())
2089 Value.setIsSigned(true);
2090 mangleIntegerLiteral(E->getType(), Value);
Anders Carlssone170ba72009-12-14 01:45:37 +00002091 break;
John McCall0512e482010-07-14 04:20:34 +00002092 }
2093
2094 case Expr::ImaginaryLiteralClass: {
2095 const ImaginaryLiteral *IE = cast<ImaginaryLiteral>(E);
2096 // Mangle as if a complex literal.
Nick Lewycky271b6652010-09-05 03:40:33 +00002097 // Proposal from David Vandevoorde, 2010.06.30.
John McCall0512e482010-07-14 04:20:34 +00002098 Out << 'L';
2099 mangleType(E->getType());
2100 if (const FloatingLiteral *Imag =
2101 dyn_cast<FloatingLiteral>(IE->getSubExpr())) {
2102 // Mangle a floating-point zero of the appropriate type.
2103 mangleFloat(llvm::APFloat(Imag->getValue().getSemantics()));
2104 Out << '_';
2105 mangleFloat(Imag->getValue());
2106 } else {
Nick Lewycky271b6652010-09-05 03:40:33 +00002107 Out << "0_";
John McCall0512e482010-07-14 04:20:34 +00002108 llvm::APSInt Value(cast<IntegerLiteral>(IE->getSubExpr())->getValue());
2109 if (IE->getSubExpr()->getType()->isSignedIntegerType())
2110 Value.setIsSigned(true);
2111 mangleNumber(Value);
2112 }
2113 Out << 'E';
2114 break;
2115 }
2116
2117 case Expr::StringLiteralClass: {
John McCall1658c392010-07-15 21:53:03 +00002118 // Revised proposal from David Vandervoorde, 2010.07.15.
John McCall0512e482010-07-14 04:20:34 +00002119 Out << 'L';
John McCall1658c392010-07-15 21:53:03 +00002120 assert(isa<ConstantArrayType>(E->getType()));
2121 mangleType(E->getType());
John McCall0512e482010-07-14 04:20:34 +00002122 Out << 'E';
2123 break;
2124 }
2125
2126 case Expr::GNUNullExprClass:
2127 // FIXME: should this really be mangled the same as nullptr?
2128 // fallthrough
2129
2130 case Expr::CXXNullPtrLiteralExprClass: {
2131 // Proposal from David Vandervoorde, 2010.06.30, as
2132 // modified by ABI list discussion.
2133 Out << "LDnE";
2134 break;
2135 }
Douglas Gregorbe230c32011-01-03 17:17:50 +00002136
2137 case Expr::PackExpansionExprClass:
2138 Out << "sp";
2139 mangleExpression(cast<PackExpansionExpr>(E)->getPattern());
2140 break;
Douglas Gregor2e774c42011-01-04 18:56:13 +00002141
2142 case Expr::SizeOfPackExprClass: {
Douglas Gregor2e774c42011-01-04 18:56:13 +00002143 Out << "sZ";
2144 const NamedDecl *Pack = cast<SizeOfPackExpr>(E)->getPack();
2145 if (const TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(Pack))
2146 mangleTemplateParameter(TTP->getIndex());
2147 else if (const NonTypeTemplateParmDecl *NTTP
2148 = dyn_cast<NonTypeTemplateParmDecl>(Pack))
2149 mangleTemplateParameter(NTTP->getIndex());
2150 else if (const TemplateTemplateParmDecl *TempTP
2151 = dyn_cast<TemplateTemplateParmDecl>(Pack))
2152 mangleTemplateParameter(TempTP->getIndex());
2153 else {
Douglas Gregor4fc48662011-01-13 16:39:34 +00002154 // Note: proposed by Mike Herrick on 11/30/10
2155 // <expression> ::= sZ <function-param> # size of function parameter pack
Douglas Gregor2e774c42011-01-04 18:56:13 +00002156 Diagnostic &Diags = Context.getDiags();
2157 unsigned DiagID = Diags.getCustomDiagID(Diagnostic::Error,
2158 "cannot mangle sizeof...(function parameter pack)");
2159 Diags.Report(DiagID);
2160 return;
2161 }
2162 }
Anders Carlssond553f8c2009-09-21 01:21:10 +00002163 }
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00002164}
2165
Anders Carlsson3ac86b52009-04-15 05:36:58 +00002166void CXXNameMangler::mangleCXXCtorType(CXXCtorType T) {
2167 // <ctor-dtor-name> ::= C1 # complete object constructor
2168 // ::= C2 # base object constructor
2169 // ::= C3 # complete object allocating constructor
2170 //
2171 switch (T) {
2172 case Ctor_Complete:
2173 Out << "C1";
2174 break;
2175 case Ctor_Base:
2176 Out << "C2";
2177 break;
2178 case Ctor_CompleteAllocating:
2179 Out << "C3";
2180 break;
2181 }
2182}
2183
Anders Carlsson27ae5362009-04-17 01:58:57 +00002184void CXXNameMangler::mangleCXXDtorType(CXXDtorType T) {
2185 // <ctor-dtor-name> ::= D0 # deleting destructor
2186 // ::= D1 # complete object destructor
2187 // ::= D2 # base object destructor
2188 //
2189 switch (T) {
2190 case Dtor_Deleting:
2191 Out << "D0";
2192 break;
2193 case Dtor_Complete:
2194 Out << "D1";
2195 break;
2196 case Dtor_Base:
2197 Out << "D2";
2198 break;
2199 }
2200}
2201
John McCall6dbce192010-08-20 00:17:19 +00002202void CXXNameMangler::mangleTemplateArgs(
2203 const ExplicitTemplateArgumentList &TemplateArgs) {
2204 // <template-args> ::= I <template-arg>+ E
2205 Out << 'I';
2206 for (unsigned I = 0, E = TemplateArgs.NumTemplateArgs; I != E; ++I)
2207 mangleTemplateArg(0, TemplateArgs.getTemplateArgs()[I].getArgument());
2208 Out << 'E';
2209}
2210
Douglas Gregor20f0cc72010-04-23 03:10:43 +00002211void CXXNameMangler::mangleTemplateArgs(TemplateName Template,
2212 const TemplateArgument *TemplateArgs,
2213 unsigned NumTemplateArgs) {
2214 if (TemplateDecl *TD = Template.getAsTemplateDecl())
2215 return mangleTemplateArgs(*TD->getTemplateParameters(), TemplateArgs,
2216 NumTemplateArgs);
Sean Huntc3021132010-05-05 15:23:54 +00002217
Douglas Gregor20f0cc72010-04-23 03:10:43 +00002218 // <template-args> ::= I <template-arg>+ E
2219 Out << 'I';
2220 for (unsigned i = 0; i != NumTemplateArgs; ++i)
2221 mangleTemplateArg(0, TemplateArgs[i]);
2222 Out << 'E';
2223}
2224
Rafael Espindolad9800722010-03-11 14:07:00 +00002225void CXXNameMangler::mangleTemplateArgs(const TemplateParameterList &PL,
2226 const TemplateArgumentList &AL) {
Anders Carlsson7a0ba872009-05-15 16:09:15 +00002227 // <template-args> ::= I <template-arg>+ E
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002228 Out << 'I';
Rafael Espindolad9800722010-03-11 14:07:00 +00002229 for (unsigned i = 0, e = AL.size(); i != e; ++i)
2230 mangleTemplateArg(PL.getParam(i), AL[i]);
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002231 Out << 'E';
Anders Carlsson7a0ba872009-05-15 16:09:15 +00002232}
2233
Rafael Espindolad9800722010-03-11 14:07:00 +00002234void CXXNameMangler::mangleTemplateArgs(const TemplateParameterList &PL,
2235 const TemplateArgument *TemplateArgs,
Anders Carlsson7624f212009-09-18 02:42:01 +00002236 unsigned NumTemplateArgs) {
2237 // <template-args> ::= I <template-arg>+ E
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002238 Out << 'I';
Daniel Dunbar7e0c1952009-11-21 09:17:15 +00002239 for (unsigned i = 0; i != NumTemplateArgs; ++i)
Rafael Espindolad9800722010-03-11 14:07:00 +00002240 mangleTemplateArg(PL.getParam(i), TemplateArgs[i]);
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002241 Out << 'E';
Anders Carlsson7624f212009-09-18 02:42:01 +00002242}
2243
Rafael Espindolad9800722010-03-11 14:07:00 +00002244void CXXNameMangler::mangleTemplateArg(const NamedDecl *P,
2245 const TemplateArgument &A) {
Mike Stump1eb44332009-09-09 15:08:12 +00002246 // <template-arg> ::= <type> # type or template
Anders Carlsson7a0ba872009-05-15 16:09:15 +00002247 // ::= X <expression> E # expression
2248 // ::= <expr-primary> # simple expressions
Douglas Gregor4fc48662011-01-13 16:39:34 +00002249 // ::= J <template-arg>* E # argument pack
Anders Carlsson7a0ba872009-05-15 16:09:15 +00002250 // ::= sp <expression> # pack expansion of (C++0x)
2251 switch (A.getKind()) {
Douglas Gregorf90b27a2011-01-03 22:36:02 +00002252 case TemplateArgument::Null:
2253 llvm_unreachable("Cannot mangle NULL template argument");
2254
Anders Carlsson7a0ba872009-05-15 16:09:15 +00002255 case TemplateArgument::Type:
2256 mangleType(A.getAsType());
2257 break;
Anders Carlsson9e85c742009-12-23 19:30:55 +00002258 case TemplateArgument::Template:
John McCallb6f532e2010-07-14 06:43:17 +00002259 // This is mangled as <type>.
2260 mangleType(A.getAsTemplate());
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002261 break;
Douglas Gregora7fc9012011-01-05 18:58:31 +00002262 case TemplateArgument::TemplateExpansion:
Douglas Gregor4fc48662011-01-13 16:39:34 +00002263 // <type> ::= Dp <type> # pack expansion (C++0x)
Douglas Gregora7fc9012011-01-05 18:58:31 +00002264 Out << "Dp";
2265 mangleType(A.getAsTemplateOrTemplatePattern());
2266 break;
Anders Carlssond553f8c2009-09-21 01:21:10 +00002267 case TemplateArgument::Expression:
2268 Out << 'X';
2269 mangleExpression(A.getAsExpr());
2270 Out << 'E';
2271 break;
Anders Carlssone170ba72009-12-14 01:45:37 +00002272 case TemplateArgument::Integral:
2273 mangleIntegerLiteral(A.getIntegralType(), *A.getAsIntegral());
Anders Carlsson7a0ba872009-05-15 16:09:15 +00002274 break;
Daniel Dunbar7e0c1952009-11-21 09:17:15 +00002275 case TemplateArgument::Declaration: {
Douglas Gregor20f0cc72010-04-23 03:10:43 +00002276 assert(P && "Missing template parameter for declaration argument");
Daniel Dunbar7e0c1952009-11-21 09:17:15 +00002277 // <expr-primary> ::= L <mangled-name> E # external name
2278
Rafael Espindolad9800722010-03-11 14:07:00 +00002279 // Clang produces AST's where pointer-to-member-function expressions
Daniel Dunbar7e0c1952009-11-21 09:17:15 +00002280 // and pointer-to-function expressions are represented as a declaration not
Rafael Espindolad9800722010-03-11 14:07:00 +00002281 // an expression. We compensate for it here to produce the correct mangling.
2282 NamedDecl *D = cast<NamedDecl>(A.getAsDecl());
2283 const NonTypeTemplateParmDecl *Parameter = cast<NonTypeTemplateParmDecl>(P);
2284 bool compensateMangling = D->isCXXClassMember() &&
2285 !Parameter->getType()->isReferenceType();
2286 if (compensateMangling) {
2287 Out << 'X';
2288 mangleOperatorName(OO_Amp, 1);
2289 }
2290
Daniel Dunbar7e0c1952009-11-21 09:17:15 +00002291 Out << 'L';
2292 // References to external entities use the mangled name; if the name would
2293 // not normally be manged then mangle it as unqualified.
2294 //
2295 // FIXME: The ABI specifies that external names here should have _Z, but
2296 // gcc leaves this off.
Rafael Espindolad9800722010-03-11 14:07:00 +00002297 if (compensateMangling)
2298 mangle(D, "_Z");
2299 else
2300 mangle(D, "Z");
Daniel Dunbar7e0c1952009-11-21 09:17:15 +00002301 Out << 'E';
Rafael Espindolad9800722010-03-11 14:07:00 +00002302
2303 if (compensateMangling)
2304 Out << 'E';
2305
Daniel Dunbar7e0c1952009-11-21 09:17:15 +00002306 break;
2307 }
Douglas Gregorf90b27a2011-01-03 22:36:02 +00002308
2309 case TemplateArgument::Pack: {
2310 // Note: proposal by Mike Herrick on 12/20/10
2311 Out << 'J';
2312 for (TemplateArgument::pack_iterator PA = A.pack_begin(),
2313 PAEnd = A.pack_end();
2314 PA != PAEnd; ++PA)
2315 mangleTemplateArg(P, *PA);
2316 Out << 'E';
2317 }
Daniel Dunbar7e0c1952009-11-21 09:17:15 +00002318 }
Anders Carlsson7a0ba872009-05-15 16:09:15 +00002319}
2320
Anders Carlsson0ccdf8d2009-09-27 00:38:53 +00002321void CXXNameMangler::mangleTemplateParameter(unsigned Index) {
2322 // <template-param> ::= T_ # first template parameter
2323 // ::= T <parameter-2 non-negative number> _
2324 if (Index == 0)
2325 Out << "T_";
2326 else
2327 Out << 'T' << (Index - 1) << '_';
2328}
2329
Anders Carlsson76967372009-09-17 00:43:46 +00002330// <substitution> ::= S <seq-id> _
2331// ::= S_
Anders Carlsson6862fc72009-09-17 04:16:28 +00002332bool CXXNameMangler::mangleSubstitution(const NamedDecl *ND) {
Anders Carlssone7c8cb62009-09-26 20:53:44 +00002333 // Try one of the standard substitutions first.
2334 if (mangleStandardSubstitution(ND))
2335 return true;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002336
Anders Carlsson433d1372009-11-07 04:26:04 +00002337 ND = cast<NamedDecl>(ND->getCanonicalDecl());
Anders Carlsson6862fc72009-09-17 04:16:28 +00002338 return mangleSubstitution(reinterpret_cast<uintptr_t>(ND));
2339}
2340
Anders Carlsson76967372009-09-17 00:43:46 +00002341bool CXXNameMangler::mangleSubstitution(QualType T) {
Anders Carlssond99edc42009-09-26 03:55:37 +00002342 if (!T.getCVRQualifiers()) {
2343 if (const RecordType *RT = T->getAs<RecordType>())
2344 return mangleSubstitution(RT->getDecl());
2345 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002346
Anders Carlsson76967372009-09-17 00:43:46 +00002347 uintptr_t TypePtr = reinterpret_cast<uintptr_t>(T.getAsOpaquePtr());
2348
Anders Carlssond3a932a2009-09-17 03:53:28 +00002349 return mangleSubstitution(TypePtr);
2350}
2351
Douglas Gregor1e9268e2010-04-28 05:58:56 +00002352bool CXXNameMangler::mangleSubstitution(TemplateName Template) {
2353 if (TemplateDecl *TD = Template.getAsTemplateDecl())
2354 return mangleSubstitution(TD);
Sean Huntc3021132010-05-05 15:23:54 +00002355
Douglas Gregor1e9268e2010-04-28 05:58:56 +00002356 Template = Context.getASTContext().getCanonicalTemplateName(Template);
2357 return mangleSubstitution(
2358 reinterpret_cast<uintptr_t>(Template.getAsVoidPointer()));
2359}
2360
Anders Carlssond3a932a2009-09-17 03:53:28 +00002361bool CXXNameMangler::mangleSubstitution(uintptr_t Ptr) {
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002362 llvm::DenseMap<uintptr_t, unsigned>::iterator I = Substitutions.find(Ptr);
Anders Carlsson76967372009-09-17 00:43:46 +00002363 if (I == Substitutions.end())
2364 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002365
Anders Carlsson76967372009-09-17 00:43:46 +00002366 unsigned SeqID = I->second;
2367 if (SeqID == 0)
2368 Out << "S_";
2369 else {
2370 SeqID--;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002371
Anders Carlsson76967372009-09-17 00:43:46 +00002372 // <seq-id> is encoded in base-36, using digits and upper case letters.
2373 char Buffer[10];
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002374 char *BufferPtr = llvm::array_endof(Buffer);
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002375
Anders Carlsson76967372009-09-17 00:43:46 +00002376 if (SeqID == 0) *--BufferPtr = '0';
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002377
Anders Carlsson76967372009-09-17 00:43:46 +00002378 while (SeqID) {
2379 assert(BufferPtr > Buffer && "Buffer overflow!");
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002380
John McCall6ab30e02010-06-09 07:26:17 +00002381 char c = static_cast<char>(SeqID % 36);
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002382
Anders Carlsson76967372009-09-17 00:43:46 +00002383 *--BufferPtr = (c < 10 ? '0' + c : 'A' + c - 10);
2384 SeqID /= 36;
2385 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002386
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002387 Out << 'S'
2388 << llvm::StringRef(BufferPtr, llvm::array_endof(Buffer)-BufferPtr)
2389 << '_';
Anders Carlsson76967372009-09-17 00:43:46 +00002390 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002391
Anders Carlsson76967372009-09-17 00:43:46 +00002392 return true;
2393}
2394
Anders Carlssonf514b542009-09-27 00:12:57 +00002395static bool isCharType(QualType T) {
2396 if (T.isNull())
2397 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002398
Anders Carlssonf514b542009-09-27 00:12:57 +00002399 return T->isSpecificBuiltinType(BuiltinType::Char_S) ||
2400 T->isSpecificBuiltinType(BuiltinType::Char_U);
2401}
2402
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002403/// isCharSpecialization - Returns whether a given type is a template
Anders Carlssonf514b542009-09-27 00:12:57 +00002404/// specialization of a given name with a single argument of type char.
2405static bool isCharSpecialization(QualType T, const char *Name) {
2406 if (T.isNull())
2407 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002408
Anders Carlssonf514b542009-09-27 00:12:57 +00002409 const RecordType *RT = T->getAs<RecordType>();
2410 if (!RT)
2411 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002412
2413 const ClassTemplateSpecializationDecl *SD =
Anders Carlssonf514b542009-09-27 00:12:57 +00002414 dyn_cast<ClassTemplateSpecializationDecl>(RT->getDecl());
2415 if (!SD)
2416 return false;
2417
2418 if (!isStdNamespace(SD->getDeclContext()))
2419 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002420
Anders Carlssonf514b542009-09-27 00:12:57 +00002421 const TemplateArgumentList &TemplateArgs = SD->getTemplateArgs();
2422 if (TemplateArgs.size() != 1)
2423 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002424
Anders Carlssonf514b542009-09-27 00:12:57 +00002425 if (!isCharType(TemplateArgs[0].getAsType()))
2426 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002427
Daniel Dunbar01eb9b92009-10-18 21:17:35 +00002428 return SD->getIdentifier()->getName() == Name;
Anders Carlssonf514b542009-09-27 00:12:57 +00002429}
2430
Anders Carlsson91f88602009-12-07 19:56:42 +00002431template <std::size_t StrLen>
Benjamin Kramer54353f42010-11-25 18:29:30 +00002432static bool isStreamCharSpecialization(const ClassTemplateSpecializationDecl*SD,
2433 const char (&Str)[StrLen]) {
Anders Carlsson91f88602009-12-07 19:56:42 +00002434 if (!SD->getIdentifier()->isStr(Str))
2435 return false;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002436
Anders Carlsson91f88602009-12-07 19:56:42 +00002437 const TemplateArgumentList &TemplateArgs = SD->getTemplateArgs();
2438 if (TemplateArgs.size() != 2)
2439 return false;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002440
Anders Carlsson91f88602009-12-07 19:56:42 +00002441 if (!isCharType(TemplateArgs[0].getAsType()))
2442 return false;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002443
Anders Carlsson91f88602009-12-07 19:56:42 +00002444 if (!isCharSpecialization(TemplateArgs[1].getAsType(), "char_traits"))
2445 return false;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002446
Anders Carlsson91f88602009-12-07 19:56:42 +00002447 return true;
2448}
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002449
Anders Carlssone7c8cb62009-09-26 20:53:44 +00002450bool CXXNameMangler::mangleStandardSubstitution(const NamedDecl *ND) {
2451 // <substitution> ::= St # ::std::
Anders Carlsson8c031552009-09-26 23:10:05 +00002452 if (const NamespaceDecl *NS = dyn_cast<NamespaceDecl>(ND)) {
Anders Carlsson47846d22009-12-04 06:23:23 +00002453 if (isStd(NS)) {
Anders Carlsson8c031552009-09-26 23:10:05 +00002454 Out << "St";
2455 return true;
2456 }
2457 }
2458
2459 if (const ClassTemplateDecl *TD = dyn_cast<ClassTemplateDecl>(ND)) {
2460 if (!isStdNamespace(TD->getDeclContext()))
2461 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002462
Anders Carlsson8c031552009-09-26 23:10:05 +00002463 // <substitution> ::= Sa # ::std::allocator
2464 if (TD->getIdentifier()->isStr("allocator")) {
2465 Out << "Sa";
2466 return true;
2467 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002468
Anders Carlsson189d59c2009-09-26 23:14:39 +00002469 // <<substitution> ::= Sb # ::std::basic_string
2470 if (TD->getIdentifier()->isStr("basic_string")) {
2471 Out << "Sb";
2472 return true;
2473 }
Anders Carlsson8c031552009-09-26 23:10:05 +00002474 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002475
2476 if (const ClassTemplateSpecializationDecl *SD =
Anders Carlssonf514b542009-09-27 00:12:57 +00002477 dyn_cast<ClassTemplateSpecializationDecl>(ND)) {
Eli Friedman5370ee22010-02-23 18:25:09 +00002478 if (!isStdNamespace(SD->getDeclContext()))
2479 return false;
2480
Anders Carlssonf514b542009-09-27 00:12:57 +00002481 // <substitution> ::= Ss # ::std::basic_string<char,
2482 // ::std::char_traits<char>,
2483 // ::std::allocator<char> >
2484 if (SD->getIdentifier()->isStr("basic_string")) {
2485 const TemplateArgumentList &TemplateArgs = SD->getTemplateArgs();
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002486
Anders Carlssonf514b542009-09-27 00:12:57 +00002487 if (TemplateArgs.size() != 3)
2488 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002489
Anders Carlssonf514b542009-09-27 00:12:57 +00002490 if (!isCharType(TemplateArgs[0].getAsType()))
2491 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002492
Anders Carlssonf514b542009-09-27 00:12:57 +00002493 if (!isCharSpecialization(TemplateArgs[1].getAsType(), "char_traits"))
2494 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002495
Anders Carlssonf514b542009-09-27 00:12:57 +00002496 if (!isCharSpecialization(TemplateArgs[2].getAsType(), "allocator"))
2497 return false;
2498
2499 Out << "Ss";
2500 return true;
2501 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002502
Anders Carlsson91f88602009-12-07 19:56:42 +00002503 // <substitution> ::= Si # ::std::basic_istream<char,
2504 // ::std::char_traits<char> >
2505 if (isStreamCharSpecialization(SD, "basic_istream")) {
2506 Out << "Si";
2507 return true;
2508 }
2509
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002510 // <substitution> ::= So # ::std::basic_ostream<char,
Anders Carlsson8f8fd8e2009-10-08 17:20:26 +00002511 // ::std::char_traits<char> >
Anders Carlsson91f88602009-12-07 19:56:42 +00002512 if (isStreamCharSpecialization(SD, "basic_ostream")) {
Anders Carlsson8f8fd8e2009-10-08 17:20:26 +00002513 Out << "So";
2514 return true;
2515 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002516
Anders Carlsson91f88602009-12-07 19:56:42 +00002517 // <substitution> ::= Sd # ::std::basic_iostream<char,
2518 // ::std::char_traits<char> >
2519 if (isStreamCharSpecialization(SD, "basic_iostream")) {
2520 Out << "Sd";
2521 return true;
2522 }
Anders Carlssonf514b542009-09-27 00:12:57 +00002523 }
Anders Carlsson8c031552009-09-26 23:10:05 +00002524 return false;
Anders Carlssone7c8cb62009-09-26 20:53:44 +00002525}
2526
Anders Carlsson76967372009-09-17 00:43:46 +00002527void CXXNameMangler::addSubstitution(QualType T) {
Anders Carlssond99edc42009-09-26 03:55:37 +00002528 if (!T.getCVRQualifiers()) {
2529 if (const RecordType *RT = T->getAs<RecordType>()) {
2530 addSubstitution(RT->getDecl());
2531 return;
2532 }
2533 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002534
Anders Carlsson76967372009-09-17 00:43:46 +00002535 uintptr_t TypePtr = reinterpret_cast<uintptr_t>(T.getAsOpaquePtr());
Anders Carlssond3a932a2009-09-17 03:53:28 +00002536 addSubstitution(TypePtr);
2537}
2538
Douglas Gregor1e9268e2010-04-28 05:58:56 +00002539void CXXNameMangler::addSubstitution(TemplateName Template) {
2540 if (TemplateDecl *TD = Template.getAsTemplateDecl())
2541 return addSubstitution(TD);
Sean Huntc3021132010-05-05 15:23:54 +00002542
Douglas Gregor1e9268e2010-04-28 05:58:56 +00002543 Template = Context.getASTContext().getCanonicalTemplateName(Template);
2544 addSubstitution(reinterpret_cast<uintptr_t>(Template.getAsVoidPointer()));
2545}
2546
Anders Carlssond3a932a2009-09-17 03:53:28 +00002547void CXXNameMangler::addSubstitution(uintptr_t Ptr) {
Anders Carlssond3a932a2009-09-17 03:53:28 +00002548 assert(!Substitutions.count(Ptr) && "Substitution already exists!");
Anders Carlsson9d85b722010-06-02 04:29:50 +00002549 Substitutions[Ptr] = SeqID++;
Anders Carlsson76967372009-09-17 00:43:46 +00002550}
2551
Daniel Dunbar1b077112009-11-21 09:06:10 +00002552//
Mike Stump1eb44332009-09-09 15:08:12 +00002553
Daniel Dunbar1b077112009-11-21 09:06:10 +00002554/// \brief Mangles the name of the declaration D and emits that name to the
2555/// given output stream.
2556///
2557/// If the declaration D requires a mangled name, this routine will emit that
2558/// mangled name to \p os and return true. Otherwise, \p os will be unchanged
2559/// and this routine will return false. In this case, the caller should just
2560/// emit the identifier of the declaration (\c D->getIdentifier()) as its
2561/// name.
Peter Collingbourne14110472011-01-13 18:57:25 +00002562void ItaniumMangleContext::mangleName(const NamedDecl *D,
Rafael Espindola0e376a02011-02-11 01:41:00 +00002563 llvm::raw_ostream &Out) {
Daniel Dunbarc02ab4c2009-11-21 09:14:44 +00002564 assert((isa<FunctionDecl>(D) || isa<VarDecl>(D)) &&
2565 "Invalid mangleName() call, argument is not a variable or function!");
2566 assert(!isa<CXXConstructorDecl>(D) && !isa<CXXDestructorDecl>(D) &&
2567 "Invalid mangleName() call on 'structor decl!");
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002568
Daniel Dunbar1b077112009-11-21 09:06:10 +00002569 PrettyStackTraceDecl CrashInfo(D, SourceLocation(),
2570 getASTContext().getSourceManager(),
2571 "Mangling declaration");
Mike Stump1eb44332009-09-09 15:08:12 +00002572
Rafael Espindolac4850c22011-02-10 23:59:36 +00002573 CXXNameMangler Mangler(*this, Out);
Daniel Dunbar94fd26d2009-11-21 09:06:22 +00002574 return Mangler.mangle(D);
Daniel Dunbar1b077112009-11-21 09:06:10 +00002575}
Mike Stump1eb44332009-09-09 15:08:12 +00002576
Peter Collingbourne14110472011-01-13 18:57:25 +00002577void ItaniumMangleContext::mangleCXXCtor(const CXXConstructorDecl *D,
2578 CXXCtorType Type,
Rafael Espindola0e376a02011-02-11 01:41:00 +00002579 llvm::raw_ostream &Out) {
Rafael Espindolac4850c22011-02-10 23:59:36 +00002580 CXXNameMangler Mangler(*this, Out, D, Type);
Daniel Dunbar77939c92009-11-21 09:06:31 +00002581 Mangler.mangle(D);
Daniel Dunbar1b077112009-11-21 09:06:10 +00002582}
Mike Stump1eb44332009-09-09 15:08:12 +00002583
Peter Collingbourne14110472011-01-13 18:57:25 +00002584void ItaniumMangleContext::mangleCXXDtor(const CXXDestructorDecl *D,
2585 CXXDtorType Type,
Rafael Espindola0e376a02011-02-11 01:41:00 +00002586 llvm::raw_ostream &Out) {
Rafael Espindolac4850c22011-02-10 23:59:36 +00002587 CXXNameMangler Mangler(*this, Out, D, Type);
Daniel Dunbar77939c92009-11-21 09:06:31 +00002588 Mangler.mangle(D);
Daniel Dunbar1b077112009-11-21 09:06:10 +00002589}
Mike Stumpf1216772009-07-31 18:25:34 +00002590
Peter Collingbourne14110472011-01-13 18:57:25 +00002591void ItaniumMangleContext::mangleThunk(const CXXMethodDecl *MD,
2592 const ThunkInfo &Thunk,
Rafael Espindolaf0be9792011-02-11 02:52:17 +00002593 llvm::raw_ostream &Out) {
Anders Carlsson19879c92010-03-23 17:17:29 +00002594 // <special-name> ::= T <call-offset> <base encoding>
2595 // # base is the nominal target function of thunk
2596 // <special-name> ::= Tc <call-offset> <call-offset> <base encoding>
2597 // # base is the nominal target function of thunk
2598 // # first call-offset is 'this' adjustment
2599 // # second call-offset is result adjustment
Sean Huntc3021132010-05-05 15:23:54 +00002600
Anders Carlsson19879c92010-03-23 17:17:29 +00002601 assert(!isa<CXXDestructorDecl>(MD) &&
2602 "Use mangleCXXDtor for destructor decls!");
Rafael Espindolac4850c22011-02-10 23:59:36 +00002603 CXXNameMangler Mangler(*this, Out);
Anders Carlsson19879c92010-03-23 17:17:29 +00002604 Mangler.getStream() << "_ZT";
2605 if (!Thunk.Return.isEmpty())
2606 Mangler.getStream() << 'c';
Sean Huntc3021132010-05-05 15:23:54 +00002607
Anders Carlsson19879c92010-03-23 17:17:29 +00002608 // Mangle the 'this' pointer adjustment.
2609 Mangler.mangleCallOffset(Thunk.This.NonVirtual, Thunk.This.VCallOffsetOffset);
Sean Huntc3021132010-05-05 15:23:54 +00002610
Anders Carlsson19879c92010-03-23 17:17:29 +00002611 // Mangle the return pointer adjustment if there is one.
2612 if (!Thunk.Return.isEmpty())
2613 Mangler.mangleCallOffset(Thunk.Return.NonVirtual,
2614 Thunk.Return.VBaseOffsetOffset);
Sean Huntc3021132010-05-05 15:23:54 +00002615
Anders Carlsson19879c92010-03-23 17:17:29 +00002616 Mangler.mangleFunctionEncoding(MD);
2617}
2618
Sean Huntc3021132010-05-05 15:23:54 +00002619void
Peter Collingbourne14110472011-01-13 18:57:25 +00002620ItaniumMangleContext::mangleCXXDtorThunk(const CXXDestructorDecl *DD,
2621 CXXDtorType Type,
2622 const ThisAdjustment &ThisAdjustment,
Rafael Espindolaf0be9792011-02-11 02:52:17 +00002623 llvm::raw_ostream &Out) {
Anders Carlsson19879c92010-03-23 17:17:29 +00002624 // <special-name> ::= T <call-offset> <base encoding>
2625 // # base is the nominal target function of thunk
Rafael Espindolac4850c22011-02-10 23:59:36 +00002626 CXXNameMangler Mangler(*this, Out, DD, Type);
Anders Carlsson19879c92010-03-23 17:17:29 +00002627 Mangler.getStream() << "_ZT";
2628
2629 // Mangle the 'this' pointer adjustment.
Sean Huntc3021132010-05-05 15:23:54 +00002630 Mangler.mangleCallOffset(ThisAdjustment.NonVirtual,
Anders Carlsson19879c92010-03-23 17:17:29 +00002631 ThisAdjustment.VCallOffsetOffset);
2632
2633 Mangler.mangleFunctionEncoding(DD);
2634}
2635
Daniel Dunbarc0747712009-11-21 09:12:13 +00002636/// mangleGuardVariable - Returns the mangled name for a guard variable
2637/// for the passed in VarDecl.
Peter Collingbourne14110472011-01-13 18:57:25 +00002638void ItaniumMangleContext::mangleItaniumGuardVariable(const VarDecl *D,
Rafael Espindolaf0be9792011-02-11 02:52:17 +00002639 llvm::raw_ostream &Out) {
Daniel Dunbarc0747712009-11-21 09:12:13 +00002640 // <special-name> ::= GV <object name> # Guard variable for one-time
2641 // # initialization
Rafael Espindolac4850c22011-02-10 23:59:36 +00002642 CXXNameMangler Mangler(*this, Out);
Daniel Dunbarc0747712009-11-21 09:12:13 +00002643 Mangler.getStream() << "_ZGV";
2644 Mangler.mangleName(D);
2645}
2646
Peter Collingbourne14110472011-01-13 18:57:25 +00002647void ItaniumMangleContext::mangleReferenceTemporary(const VarDecl *D,
Rafael Espindolaf0be9792011-02-11 02:52:17 +00002648 llvm::raw_ostream &Out) {
Anders Carlsson715edf22010-06-26 16:09:40 +00002649 // We match the GCC mangling here.
2650 // <special-name> ::= GR <object name>
Rafael Espindolac4850c22011-02-10 23:59:36 +00002651 CXXNameMangler Mangler(*this, Out);
Anders Carlsson715edf22010-06-26 16:09:40 +00002652 Mangler.getStream() << "_ZGR";
2653 Mangler.mangleName(D);
2654}
2655
Peter Collingbourne14110472011-01-13 18:57:25 +00002656void ItaniumMangleContext::mangleCXXVTable(const CXXRecordDecl *RD,
Rafael Espindolaf0be9792011-02-11 02:52:17 +00002657 llvm::raw_ostream &Out) {
Daniel Dunbarc0747712009-11-21 09:12:13 +00002658 // <special-name> ::= TV <type> # virtual table
Rafael Espindolac4850c22011-02-10 23:59:36 +00002659 CXXNameMangler Mangler(*this, Out);
Daniel Dunbarc0747712009-11-21 09:12:13 +00002660 Mangler.getStream() << "_ZTV";
Douglas Gregor1b12a3b2010-05-26 05:11:13 +00002661 Mangler.mangleNameOrStandardSubstitution(RD);
Daniel Dunbar1b077112009-11-21 09:06:10 +00002662}
Mike Stump82d75b02009-11-10 01:58:37 +00002663
Peter Collingbourne14110472011-01-13 18:57:25 +00002664void ItaniumMangleContext::mangleCXXVTT(const CXXRecordDecl *RD,
Rafael Espindolaf0be9792011-02-11 02:52:17 +00002665 llvm::raw_ostream &Out) {
Daniel Dunbarc0747712009-11-21 09:12:13 +00002666 // <special-name> ::= TT <type> # VTT structure
Rafael Espindolac4850c22011-02-10 23:59:36 +00002667 CXXNameMangler Mangler(*this, Out);
Daniel Dunbarc0747712009-11-21 09:12:13 +00002668 Mangler.getStream() << "_ZTT";
Douglas Gregor1b12a3b2010-05-26 05:11:13 +00002669 Mangler.mangleNameOrStandardSubstitution(RD);
Daniel Dunbar1b077112009-11-21 09:06:10 +00002670}
Mike Stumpab3f7e92009-11-10 01:41:59 +00002671
Peter Collingbourne14110472011-01-13 18:57:25 +00002672void ItaniumMangleContext::mangleCXXCtorVTable(const CXXRecordDecl *RD,
2673 int64_t Offset,
2674 const CXXRecordDecl *Type,
Rafael Espindolaf0be9792011-02-11 02:52:17 +00002675 llvm::raw_ostream &Out) {
Daniel Dunbarc0747712009-11-21 09:12:13 +00002676 // <special-name> ::= TC <type> <offset number> _ <base type>
Rafael Espindolac4850c22011-02-10 23:59:36 +00002677 CXXNameMangler Mangler(*this, Out);
Daniel Dunbarc0747712009-11-21 09:12:13 +00002678 Mangler.getStream() << "_ZTC";
Douglas Gregor1b12a3b2010-05-26 05:11:13 +00002679 Mangler.mangleNameOrStandardSubstitution(RD);
Daniel Dunbarc0747712009-11-21 09:12:13 +00002680 Mangler.getStream() << Offset;
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002681 Mangler.getStream() << '_';
Douglas Gregor1b12a3b2010-05-26 05:11:13 +00002682 Mangler.mangleNameOrStandardSubstitution(Type);
Daniel Dunbar1b077112009-11-21 09:06:10 +00002683}
Mike Stump738f8c22009-07-31 23:15:31 +00002684
Peter Collingbourne14110472011-01-13 18:57:25 +00002685void ItaniumMangleContext::mangleCXXRTTI(QualType Ty,
Rafael Espindolaf0be9792011-02-11 02:52:17 +00002686 llvm::raw_ostream &Out) {
Daniel Dunbarc0747712009-11-21 09:12:13 +00002687 // <special-name> ::= TI <type> # typeinfo structure
Douglas Gregor154fe982009-12-23 22:04:40 +00002688 assert(!Ty.hasQualifiers() && "RTTI info cannot have top-level qualifiers");
Rafael Espindolac4850c22011-02-10 23:59:36 +00002689 CXXNameMangler Mangler(*this, Out);
Daniel Dunbarc0747712009-11-21 09:12:13 +00002690 Mangler.getStream() << "_ZTI";
2691 Mangler.mangleType(Ty);
Daniel Dunbar1b077112009-11-21 09:06:10 +00002692}
Mike Stump67795982009-11-14 00:14:13 +00002693
Peter Collingbourne14110472011-01-13 18:57:25 +00002694void ItaniumMangleContext::mangleCXXRTTIName(QualType Ty,
Rafael Espindolaf0be9792011-02-11 02:52:17 +00002695 llvm::raw_ostream &Out) {
Daniel Dunbarc0747712009-11-21 09:12:13 +00002696 // <special-name> ::= TS <type> # typeinfo name (null terminated byte string)
Rafael Espindolac4850c22011-02-10 23:59:36 +00002697 CXXNameMangler Mangler(*this, Out);
Daniel Dunbarc0747712009-11-21 09:12:13 +00002698 Mangler.getStream() << "_ZTS";
2699 Mangler.mangleType(Ty);
Mike Stumpf1216772009-07-31 18:25:34 +00002700}
Peter Collingbourne14110472011-01-13 18:57:25 +00002701
2702MangleContext *clang::createItaniumMangleContext(ASTContext &Context,
2703 Diagnostic &Diags) {
2704 return new ItaniumMangleContext(Context, Diags);
2705}