blob: 3663ed705ebae8a8af07bd398cb94e795bdc0dbb [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
Peter Collingbourne69317432011-04-06 12:29:09 +0000337 // marker. We also avoid adding the marker if this is an alias for an
338 // LLVM intrinsic.
Rafael Espindola4e274e92011-02-15 22:23:51 +0000339 llvm::StringRef UserLabelPrefix =
340 getASTContext().Target.getUserLabelPrefix();
Peter Collingbourne69317432011-04-06 12:29:09 +0000341 if (!UserLabelPrefix.empty() && !ALA->getLabel().startswith("llvm."))
Rafael Espindola4e274e92011-02-15 22:23:51 +0000342 Out << '\01'; // LLVM IR Marker for __asm("foo")
343
Chris Lattnerca3f25c2009-03-21 08:24:40 +0000344 Out << ALA->getLabel();
Daniel Dunbarf981bf82009-11-21 09:14:52 +0000345 return;
Chris Lattnerca3f25c2009-03-21 08:24:40 +0000346 }
Mike Stump1eb44332009-09-09 15:08:12 +0000347
Sean Hunt31455252010-01-24 03:04:27 +0000348 // <mangled-name> ::= _Z <encoding>
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000349 // ::= <data name>
350 // ::= <special-name>
Daniel Dunbar7e0c1952009-11-21 09:17:15 +0000351 Out << Prefix;
352 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
Daniel Dunbarf981bf82009-11-21 09:14:52 +0000353 mangleFunctionEncoding(FD);
Rafael Espindolad9800722010-03-11 14:07:00 +0000354 else if (const VarDecl *VD = dyn_cast<VarDecl>(D))
355 mangleName(VD);
Daniel Dunbar7e0c1952009-11-21 09:17:15 +0000356 else
Rafael Espindolad9800722010-03-11 14:07:00 +0000357 mangleName(cast<FieldDecl>(D));
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000358}
359
360void CXXNameMangler::mangleFunctionEncoding(const FunctionDecl *FD) {
361 // <encoding> ::= <function name> <bare-function-type>
362 mangleName(FD);
Mike Stump1eb44332009-09-09 15:08:12 +0000363
Daniel Dunbar7e0c1952009-11-21 09:17:15 +0000364 // Don't mangle in the type if this isn't a decl we should typically mangle.
365 if (!Context.shouldMangleDeclName(FD))
366 return;
367
Mike Stump141c5af2009-09-02 00:25:38 +0000368 // Whether the mangling of a function type includes the return type depends on
369 // the context and the nature of the function. The rules for deciding whether
370 // the return type is included are:
Mike Stump1eb44332009-09-09 15:08:12 +0000371 //
Douglas Gregor1fd2dd12009-06-29 22:39:32 +0000372 // 1. Template functions (names or types) have return types encoded, with
373 // the exceptions listed below.
Mike Stump1eb44332009-09-09 15:08:12 +0000374 // 2. Function types not appearing as part of a function name mangling,
Douglas Gregor1fd2dd12009-06-29 22:39:32 +0000375 // e.g. parameters, pointer types, etc., have return type encoded, with the
376 // exceptions listed below.
377 // 3. Non-template function names do not have return types encoded.
378 //
Mike Stump141c5af2009-09-02 00:25:38 +0000379 // The exceptions mentioned in (1) and (2) above, for which the return type is
380 // never included, are
Douglas Gregor1fd2dd12009-06-29 22:39:32 +0000381 // 1. Constructors.
382 // 2. Destructors.
383 // 3. Conversion operator functions, e.g. operator int.
384 bool MangleReturnType = false;
Anders Carlsson9234b7f2009-09-17 03:46:43 +0000385 if (FunctionTemplateDecl *PrimaryTemplate = FD->getPrimaryTemplate()) {
386 if (!(isa<CXXConstructorDecl>(FD) || isa<CXXDestructorDecl>(FD) ||
387 isa<CXXConversionDecl>(FD)))
388 MangleReturnType = true;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000389
Anders Carlsson9234b7f2009-09-17 03:46:43 +0000390 // Mangle the type of the primary template.
391 FD = PrimaryTemplate->getTemplatedDecl();
392 }
393
John McCall54e14c42009-10-22 22:37:11 +0000394 // Do the canonicalization out here because parameter types can
395 // undergo additional canonicalization (e.g. array decay).
John McCallf4c73712011-01-19 06:33:43 +0000396 const FunctionType *FT
397 = cast<FunctionType>(Context.getASTContext()
John McCall54e14c42009-10-22 22:37:11 +0000398 .getCanonicalType(FD->getType()));
399
400 mangleBareFunctionType(FT, MangleReturnType);
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000401}
402
Anders Carlsson47846d22009-12-04 06:23:23 +0000403static const DeclContext *IgnoreLinkageSpecDecls(const DeclContext *DC) {
404 while (isa<LinkageSpecDecl>(DC)) {
Anders Carlsson47846d22009-12-04 06:23:23 +0000405 DC = DC->getParent();
406 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000407
Anders Carlsson47846d22009-12-04 06:23:23 +0000408 return DC;
409}
410
Anders Carlssonc820f902010-06-02 15:58:27 +0000411/// isStd - Return whether a given namespace is the 'std' namespace.
412static bool isStd(const NamespaceDecl *NS) {
413 if (!IgnoreLinkageSpecDecls(NS->getParent())->isTranslationUnit())
414 return false;
415
416 const IdentifierInfo *II = NS->getOriginalNamespace()->getIdentifier();
417 return II && II->isStr("std");
418}
419
Anders Carlsson47846d22009-12-04 06:23:23 +0000420// isStdNamespace - Return whether a given decl context is a toplevel 'std'
421// namespace.
Daniel Dunbar1308af92009-11-21 09:11:45 +0000422static bool isStdNamespace(const DeclContext *DC) {
Anders Carlsson47846d22009-12-04 06:23:23 +0000423 if (!DC->isNamespace())
424 return false;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000425
Anders Carlsson47846d22009-12-04 06:23:23 +0000426 return isStd(cast<NamespaceDecl>(DC));
Daniel Dunbar1308af92009-11-21 09:11:45 +0000427}
428
Anders Carlssonbb36ba42009-09-26 03:24:57 +0000429static const TemplateDecl *
430isTemplate(const NamedDecl *ND, const TemplateArgumentList *&TemplateArgs) {
Anders Carlsson2744a062009-09-18 19:00:18 +0000431 // Check if we have a function template.
432 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)){
Anders Carlssonbb36ba42009-09-26 03:24:57 +0000433 if (const TemplateDecl *TD = FD->getPrimaryTemplate()) {
Anders Carlsson2744a062009-09-18 19:00:18 +0000434 TemplateArgs = FD->getTemplateSpecializationArgs();
Anders Carlssonbb36ba42009-09-26 03:24:57 +0000435 return TD;
Anders Carlsson2744a062009-09-18 19:00:18 +0000436 }
437 }
438
Anders Carlssoneafc6dc2009-09-18 19:44:50 +0000439 // Check if we have a class template.
440 if (const ClassTemplateSpecializationDecl *Spec =
441 dyn_cast<ClassTemplateSpecializationDecl>(ND)) {
442 TemplateArgs = &Spec->getTemplateArgs();
Anders Carlssonbb36ba42009-09-26 03:24:57 +0000443 return Spec->getSpecializedTemplate();
Anders Carlssoneafc6dc2009-09-18 19:44:50 +0000444 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000445
Anders Carlsson2744a062009-09-18 19:00:18 +0000446 return 0;
447}
448
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000449void CXXNameMangler::mangleName(const NamedDecl *ND) {
450 // <name> ::= <nested-name>
451 // ::= <unscoped-name>
452 // ::= <unscoped-template-name> <template-args>
Anders Carlsson201ce742009-09-17 03:17:01 +0000453 // ::= <local-name>
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000454 //
Anders Carlssond58d6f72009-09-17 16:12:20 +0000455 const DeclContext *DC = ND->getDeclContext();
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000456
Eli Friedman7facf842009-12-02 20:32:49 +0000457 // If this is an extern variable declared locally, the relevant DeclContext
458 // is that of the containing namespace, or the translation unit.
459 if (isa<FunctionDecl>(DC) && ND->hasLinkage())
460 while (!DC->isNamespace() && !DC->isTranslationUnit())
461 DC = DC->getParent();
John McCall82b7d7b2010-10-18 21:28:44 +0000462 else if (GetLocalClassDecl(ND)) {
463 mangleLocalName(ND);
464 return;
465 }
Eli Friedman7facf842009-12-02 20:32:49 +0000466
Anders Carlsson5cc58c62009-09-22 17:23:30 +0000467 while (isa<LinkageSpecDecl>(DC))
Anders Carlssond58d6f72009-09-17 16:12:20 +0000468 DC = DC->getParent();
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000469
Anders Carlssond58d6f72009-09-17 16:12:20 +0000470 if (DC->isTranslationUnit() || isStdNamespace(DC)) {
Anders Carlsson2744a062009-09-18 19:00:18 +0000471 // Check if we have a template.
472 const TemplateArgumentList *TemplateArgs = 0;
Anders Carlsson0fa6df42009-09-26 19:45:45 +0000473 if (const TemplateDecl *TD = isTemplate(ND, TemplateArgs)) {
Anders Carlsson2744a062009-09-18 19:00:18 +0000474 mangleUnscopedTemplateName(TD);
Rafael Espindolad9800722010-03-11 14:07:00 +0000475 TemplateParameterList *TemplateParameters = TD->getTemplateParameters();
476 mangleTemplateArgs(*TemplateParameters, *TemplateArgs);
Anders Carlsson2744a062009-09-18 19:00:18 +0000477 return;
Anders Carlsson7482e242009-09-18 04:29:09 +0000478 }
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000479
Anders Carlsson7482e242009-09-18 04:29:09 +0000480 mangleUnscopedName(ND);
481 return;
482 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000483
Anders Carlsson7b06f6c2009-12-10 03:14:39 +0000484 if (isa<FunctionDecl>(DC) || isa<ObjCMethodDecl>(DC)) {
Anders Carlsson7482e242009-09-18 04:29:09 +0000485 mangleLocalName(ND);
486 return;
487 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000488
Eli Friedman7facf842009-12-02 20:32:49 +0000489 mangleNestedName(ND, DC);
Anders Carlsson7482e242009-09-18 04:29:09 +0000490}
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000491void CXXNameMangler::mangleName(const TemplateDecl *TD,
Anders Carlsson7624f212009-09-18 02:42:01 +0000492 const TemplateArgument *TemplateArgs,
493 unsigned NumTemplateArgs) {
Anders Carlsson47846d22009-12-04 06:23:23 +0000494 const DeclContext *DC = IgnoreLinkageSpecDecls(TD->getDeclContext());
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000495
Anders Carlsson7624f212009-09-18 02:42:01 +0000496 if (DC->isTranslationUnit() || isStdNamespace(DC)) {
Anders Carlsson0fa6df42009-09-26 19:45:45 +0000497 mangleUnscopedTemplateName(TD);
Rafael Espindolad9800722010-03-11 14:07:00 +0000498 TemplateParameterList *TemplateParameters = TD->getTemplateParameters();
499 mangleTemplateArgs(*TemplateParameters, TemplateArgs, NumTemplateArgs);
Anders Carlsson7624f212009-09-18 02:42:01 +0000500 } else {
501 mangleNestedName(TD, TemplateArgs, NumTemplateArgs);
502 }
503}
504
Anders Carlsson201ce742009-09-17 03:17:01 +0000505void CXXNameMangler::mangleUnscopedName(const NamedDecl *ND) {
506 // <unscoped-name> ::= <unqualified-name>
507 // ::= St <unqualified-name> # ::std::
508 if (isStdNamespace(ND->getDeclContext()))
509 Out << "St";
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000510
Anders Carlsson201ce742009-09-17 03:17:01 +0000511 mangleUnqualifiedName(ND);
512}
513
Anders Carlsson0fa6df42009-09-26 19:45:45 +0000514void CXXNameMangler::mangleUnscopedTemplateName(const TemplateDecl *ND) {
Anders Carlsson201ce742009-09-17 03:17:01 +0000515 // <unscoped-template-name> ::= <unscoped-name>
516 // ::= <substitution>
Anders Carlsson7624f212009-09-18 02:42:01 +0000517 if (mangleSubstitution(ND))
Anders Carlsson03c9d532009-09-17 04:02:31 +0000518 return;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000519
Douglas Gregor32fb4e12010-02-05 20:45:00 +0000520 // <template-template-param> ::= <template-param>
521 if (const TemplateTemplateParmDecl *TTP
522 = dyn_cast<TemplateTemplateParmDecl>(ND)) {
523 mangleTemplateParameter(TTP->getIndex());
524 return;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000525 }
Douglas Gregor32fb4e12010-02-05 20:45:00 +0000526
Anders Carlsson1668f202009-09-26 20:13:56 +0000527 mangleUnscopedName(ND->getTemplatedDecl());
Anders Carlsson7624f212009-09-18 02:42:01 +0000528 addSubstitution(ND);
Anders Carlsson201ce742009-09-17 03:17:01 +0000529}
530
Douglas Gregor1e9268e2010-04-28 05:58:56 +0000531void CXXNameMangler::mangleUnscopedTemplateName(TemplateName Template) {
532 // <unscoped-template-name> ::= <unscoped-name>
533 // ::= <substitution>
534 if (TemplateDecl *TD = Template.getAsTemplateDecl())
535 return mangleUnscopedTemplateName(TD);
Sean Huntc3021132010-05-05 15:23:54 +0000536
Douglas Gregor1e9268e2010-04-28 05:58:56 +0000537 if (mangleSubstitution(Template))
538 return;
539
540 // FIXME: How to cope with operators here?
541 DependentTemplateName *Dependent = Template.getAsDependentTemplateName();
542 assert(Dependent && "Not a dependent template name?");
543 if (!Dependent->isIdentifier()) {
544 // FIXME: We can't possibly know the arity of the operator here!
545 Diagnostic &Diags = Context.getDiags();
546 unsigned DiagID = Diags.getCustomDiagID(Diagnostic::Error,
547 "cannot mangle dependent operator name");
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000548 Diags.Report(DiagID);
Douglas Gregor1e9268e2010-04-28 05:58:56 +0000549 return;
550 }
Sean Huntc3021132010-05-05 15:23:54 +0000551
Douglas Gregor1e9268e2010-04-28 05:58:56 +0000552 mangleSourceName(Dependent->getIdentifier());
553 addSubstitution(Template);
554}
555
John McCall1b600522011-04-24 03:07:16 +0000556void CXXNameMangler::mangleFloat(const llvm::APFloat &f) {
557 // ABI:
558 // Floating-point literals are encoded using a fixed-length
559 // lowercase hexadecimal string corresponding to the internal
560 // representation (IEEE on Itanium), high-order bytes first,
561 // without leading zeroes. For example: "Lf bf800000 E" is -1.0f
562 // on Itanium.
563 // APInt::toString uses uppercase hexadecimal, and it's not really
564 // worth embellishing that interface for this use case, so we just
565 // do a second pass to lowercase things.
566 typedef llvm::SmallString<20> buffer_t;
567 buffer_t buffer;
568 f.bitcastToAPInt().toString(buffer, 16, false);
569
570 for (buffer_t::iterator i = buffer.begin(), e = buffer.end(); i != e; ++i)
571 if (isupper(*i)) *i = tolower(*i);
572
573 Out.write(buffer.data(), buffer.size());
John McCall0512e482010-07-14 04:20:34 +0000574}
575
576void CXXNameMangler::mangleNumber(const llvm::APSInt &Value) {
577 if (Value.isSigned() && Value.isNegative()) {
578 Out << 'n';
579 Value.abs().print(Out, true);
580 } else
581 Value.print(Out, Value.isSigned());
582}
583
Anders Carlssona94822e2009-11-26 02:32:05 +0000584void CXXNameMangler::mangleNumber(int64_t Number) {
585 // <number> ::= [n] <non-negative decimal integer>
586 if (Number < 0) {
587 Out << 'n';
588 Number = -Number;
589 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000590
Anders Carlssona94822e2009-11-26 02:32:05 +0000591 Out << Number;
592}
593
Anders Carlsson19879c92010-03-23 17:17:29 +0000594void CXXNameMangler::mangleCallOffset(int64_t NonVirtual, int64_t Virtual) {
Mike Stump141c5af2009-09-02 00:25:38 +0000595 // <call-offset> ::= h <nv-offset> _
596 // ::= v <v-offset> _
597 // <nv-offset> ::= <offset number> # non-virtual base override
Anders Carlssona94822e2009-11-26 02:32:05 +0000598 // <v-offset> ::= <offset number> _ <virtual offset number>
Mike Stump141c5af2009-09-02 00:25:38 +0000599 // # virtual base override, with vcall offset
Anders Carlsson19879c92010-03-23 17:17:29 +0000600 if (!Virtual) {
Anders Carlssona94822e2009-11-26 02:32:05 +0000601 Out << 'h';
Anders Carlsson19879c92010-03-23 17:17:29 +0000602 mangleNumber(NonVirtual);
Anders Carlssona94822e2009-11-26 02:32:05 +0000603 Out << '_';
604 return;
Mike Stump141c5af2009-09-02 00:25:38 +0000605 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000606
Anders Carlssona94822e2009-11-26 02:32:05 +0000607 Out << 'v';
Anders Carlsson19879c92010-03-23 17:17:29 +0000608 mangleNumber(NonVirtual);
Anders Carlssona94822e2009-11-26 02:32:05 +0000609 Out << '_';
Anders Carlsson19879c92010-03-23 17:17:29 +0000610 mangleNumber(Virtual);
Anders Carlssona94822e2009-11-26 02:32:05 +0000611 Out << '_';
Mike Stump9124bcc2009-09-02 00:56:18 +0000612}
613
John McCall1dd73832010-02-04 01:42:13 +0000614void CXXNameMangler::mangleUnresolvedScope(NestedNameSpecifier *Qualifier) {
615 Qualifier = getASTContext().getCanonicalNestedNameSpecifier(Qualifier);
616 switch (Qualifier->getKind()) {
617 case NestedNameSpecifier::Global:
618 // nothing
619 break;
620 case NestedNameSpecifier::Namespace:
621 mangleName(Qualifier->getAsNamespace());
622 break;
Douglas Gregor14aba762011-02-24 02:36:08 +0000623 case NestedNameSpecifier::NamespaceAlias:
624 mangleName(Qualifier->getAsNamespaceAlias()->getNamespace());
625 break;
John McCall1dd73832010-02-04 01:42:13 +0000626 case NestedNameSpecifier::TypeSpec:
Rafael Espindola9b35b252010-03-17 04:28:11 +0000627 case NestedNameSpecifier::TypeSpecWithTemplate: {
628 const Type *QTy = Qualifier->getAsType();
629
630 if (const TemplateSpecializationType *TST =
631 dyn_cast<TemplateSpecializationType>(QTy)) {
632 if (!mangleSubstitution(QualType(TST, 0))) {
Douglas Gregor20f0cc72010-04-23 03:10:43 +0000633 mangleTemplatePrefix(TST->getTemplateName());
Sean Huntc3021132010-05-05 15:23:54 +0000634
Douglas Gregor20f0cc72010-04-23 03:10:43 +0000635 // FIXME: GCC does not appear to mangle the template arguments when
636 // the template in question is a dependent template name. Should we
637 // emulate that badness?
638 mangleTemplateArgs(TST->getTemplateName(), TST->getArgs(),
Rafael Espindola9b35b252010-03-17 04:28:11 +0000639 TST->getNumArgs());
640 addSubstitution(QualType(TST, 0));
641 }
Douglas Gregoraa2187d2011-02-28 00:04:36 +0000642 } else if (const DependentTemplateSpecializationType *DTST
643 = dyn_cast<DependentTemplateSpecializationType>(QTy)) {
644 TemplateName Template
645 = getASTContext().getDependentTemplateName(DTST->getQualifier(),
646 DTST->getIdentifier());
647 mangleTemplatePrefix(Template);
648
649 // FIXME: GCC does not appear to mangle the template arguments when
650 // the template in question is a dependent template name. Should we
651 // emulate that badness?
652 mangleTemplateArgs(Template, DTST->getArgs(), DTST->getNumArgs());
Rafael Espindola9b35b252010-03-17 04:28:11 +0000653 } else {
654 // We use the QualType mangle type variant here because it handles
655 // substitutions.
656 mangleType(QualType(QTy, 0));
657 }
658 }
John McCall1dd73832010-02-04 01:42:13 +0000659 break;
660 case NestedNameSpecifier::Identifier:
John McCallad5e7382010-03-01 23:49:17 +0000661 // Member expressions can have these without prefixes.
662 if (Qualifier->getPrefix())
663 mangleUnresolvedScope(Qualifier->getPrefix());
John McCall1dd73832010-02-04 01:42:13 +0000664 mangleSourceName(Qualifier->getAsIdentifier());
665 break;
666 }
667}
668
669/// Mangles a name which was not resolved to a specific entity.
670void CXXNameMangler::mangleUnresolvedName(NestedNameSpecifier *Qualifier,
671 DeclarationName Name,
672 unsigned KnownArity) {
673 if (Qualifier)
674 mangleUnresolvedScope(Qualifier);
675 // FIXME: ambiguity of unqualified lookup with ::
676
677 mangleUnqualifiedName(0, Name, KnownArity);
678}
679
Anders Carlsson6f7e2f42010-06-08 14:49:03 +0000680static const FieldDecl *FindFirstNamedDataMember(const RecordDecl *RD) {
681 assert(RD->isAnonymousStructOrUnion() &&
682 "Expected anonymous struct or union!");
683
684 for (RecordDecl::field_iterator I = RD->field_begin(), E = RD->field_end();
685 I != E; ++I) {
686 const FieldDecl *FD = *I;
687
688 if (FD->getIdentifier())
689 return FD;
690
691 if (const RecordType *RT = FD->getType()->getAs<RecordType>()) {
692 if (const FieldDecl *NamedDataMember =
693 FindFirstNamedDataMember(RT->getDecl()))
694 return NamedDataMember;
695 }
696 }
697
698 // We didn't find a named data member.
699 return 0;
700}
701
John McCall1dd73832010-02-04 01:42:13 +0000702void CXXNameMangler::mangleUnqualifiedName(const NamedDecl *ND,
703 DeclarationName Name,
704 unsigned KnownArity) {
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000705 // <unqualified-name> ::= <operator-name>
Mike Stump1eb44332009-09-09 15:08:12 +0000706 // ::= <ctor-dtor-name>
707 // ::= <source-name>
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000708 switch (Name.getNameKind()) {
Anders Carlssonc4355b62009-10-07 01:45:02 +0000709 case DeclarationName::Identifier: {
Anders Carlssonc4355b62009-10-07 01:45:02 +0000710 if (const IdentifierInfo *II = Name.getAsIdentifierInfo()) {
Sean Hunt31455252010-01-24 03:04:27 +0000711 // We must avoid conflicts between internally- and externally-
John McCall74990f42011-03-22 06:34:45 +0000712 // linked variable and function declaration names in the same TU:
713 // void test() { extern void foo(); }
714 // static void foo();
715 // This naming convention is the same as that followed by GCC,
716 // though it shouldn't actually matter.
717 if (ND && ND->getLinkage() == InternalLinkage &&
Sean Hunt31455252010-01-24 03:04:27 +0000718 ND->getDeclContext()->isFileContext())
719 Out << 'L';
720
Anders Carlssonc4355b62009-10-07 01:45:02 +0000721 mangleSourceName(II);
722 break;
723 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000724
John McCall1dd73832010-02-04 01:42:13 +0000725 // Otherwise, an anonymous entity. We must have a declaration.
726 assert(ND && "mangling empty name without declaration");
727
728 if (const NamespaceDecl *NS = dyn_cast<NamespaceDecl>(ND)) {
729 if (NS->isAnonymousNamespace()) {
730 // This is how gcc mangles these names.
731 Out << "12_GLOBAL__N_1";
732 break;
733 }
734 }
735
Anders Carlsson6f7e2f42010-06-08 14:49:03 +0000736 if (const VarDecl *VD = dyn_cast<VarDecl>(ND)) {
737 // We must have an anonymous union or struct declaration.
738 const RecordDecl *RD =
739 cast<RecordDecl>(VD->getType()->getAs<RecordType>()->getDecl());
740
741 // Itanium C++ ABI 5.1.2:
742 //
743 // For the purposes of mangling, the name of an anonymous union is
744 // considered to be the name of the first named data member found by a
745 // pre-order, depth-first, declaration-order walk of the data members of
746 // the anonymous union. If there is no such data member (i.e., if all of
747 // the data members in the union are unnamed), then there is no way for
748 // a program to refer to the anonymous union, and there is therefore no
749 // need to mangle its name.
750 const FieldDecl *FD = FindFirstNamedDataMember(RD);
John McCall7121c8f2010-08-05 22:02:13 +0000751
752 // It's actually possible for various reasons for us to get here
753 // with an empty anonymous struct / union. Fortunately, it
754 // doesn't really matter what name we generate.
755 if (!FD) break;
Anders Carlsson6f7e2f42010-06-08 14:49:03 +0000756 assert(FD->getIdentifier() && "Data member name isn't an identifier!");
757
758 mangleSourceName(FD->getIdentifier());
759 break;
760 }
761
Anders Carlssonc4355b62009-10-07 01:45:02 +0000762 // We must have an anonymous struct.
763 const TagDecl *TD = cast<TagDecl>(ND);
Richard Smith162e1c12011-04-15 14:24:37 +0000764 if (const TypedefNameDecl *D = TD->getTypedefNameForAnonDecl()) {
Anders Carlssonc4355b62009-10-07 01:45:02 +0000765 assert(TD->getDeclContext() == D->getDeclContext() &&
766 "Typedef should not be in another decl context!");
767 assert(D->getDeclName().getAsIdentifierInfo() &&
768 "Typedef was not named!");
769 mangleSourceName(D->getDeclName().getAsIdentifierInfo());
770 break;
771 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000772
Anders Carlssonc4355b62009-10-07 01:45:02 +0000773 // Get a unique id for the anonymous struct.
774 uint64_t AnonStructId = Context.getAnonymousStructId(TD);
775
776 // Mangle it as a source name in the form
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000777 // [n] $_<id>
Anders Carlssonc4355b62009-10-07 01:45:02 +0000778 // where n is the length of the string.
779 llvm::SmallString<8> Str;
780 Str += "$_";
781 Str += llvm::utostr(AnonStructId);
782
783 Out << Str.size();
784 Out << Str.str();
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000785 break;
Anders Carlssonc4355b62009-10-07 01:45:02 +0000786 }
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000787
788 case DeclarationName::ObjCZeroArgSelector:
789 case DeclarationName::ObjCOneArgSelector:
790 case DeclarationName::ObjCMultiArgSelector:
791 assert(false && "Can't mangle Objective-C selector names here!");
792 break;
793
794 case DeclarationName::CXXConstructorName:
Anders Carlsson27ae5362009-04-17 01:58:57 +0000795 if (ND == Structor)
Mike Stump141c5af2009-09-02 00:25:38 +0000796 // If the named decl is the C++ constructor we're mangling, use the type
797 // we were given.
Anders Carlsson27ae5362009-04-17 01:58:57 +0000798 mangleCXXCtorType(static_cast<CXXCtorType>(StructorType));
Anders Carlsson3ac86b52009-04-15 05:36:58 +0000799 else
800 // Otherwise, use the complete constructor name. This is relevant if a
801 // class with a constructor is declared within a constructor.
802 mangleCXXCtorType(Ctor_Complete);
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000803 break;
804
805 case DeclarationName::CXXDestructorName:
Anders Carlsson27ae5362009-04-17 01:58:57 +0000806 if (ND == Structor)
Mike Stump141c5af2009-09-02 00:25:38 +0000807 // If the named decl is the C++ destructor we're mangling, use the type we
808 // were given.
Anders Carlsson27ae5362009-04-17 01:58:57 +0000809 mangleCXXDtorType(static_cast<CXXDtorType>(StructorType));
810 else
811 // Otherwise, use the complete destructor name. This is relevant if a
812 // class with a destructor is declared within a destructor.
813 mangleCXXDtorType(Dtor_Complete);
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000814 break;
815
816 case DeclarationName::CXXConversionFunctionName:
Mike Stump1eb44332009-09-09 15:08:12 +0000817 // <operator-name> ::= cv <type> # (cast)
Douglas Gregor219cc612009-02-13 01:28:03 +0000818 Out << "cv";
Anders Carlssonb5404912009-10-07 01:06:45 +0000819 mangleType(Context.getASTContext().getCanonicalType(Name.getCXXNameType()));
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000820 break;
821
Anders Carlsson8257d412009-12-22 06:36:32 +0000822 case DeclarationName::CXXOperatorName: {
John McCall1dd73832010-02-04 01:42:13 +0000823 unsigned Arity;
824 if (ND) {
825 Arity = cast<FunctionDecl>(ND)->getNumParams();
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000826
John McCall1dd73832010-02-04 01:42:13 +0000827 // If we have a C++ member function, we need to include the 'this' pointer.
828 // FIXME: This does not make sense for operators that are static, but their
829 // names stay the same regardless of the arity (operator new for instance).
830 if (isa<CXXMethodDecl>(ND))
831 Arity++;
832 } else
833 Arity = KnownArity;
834
Anders Carlsson8257d412009-12-22 06:36:32 +0000835 mangleOperatorName(Name.getCXXOverloadedOperator(), Arity);
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000836 break;
Anders Carlsson8257d412009-12-22 06:36:32 +0000837 }
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000838
Sean Hunt3e518bd2009-11-29 07:34:05 +0000839 case DeclarationName::CXXLiteralOperatorName:
Sean Hunt5dd6b392009-12-04 21:11:13 +0000840 // FIXME: This mangling is not yet official.
Sean Hunt2421f662009-12-04 21:01:37 +0000841 Out << "li";
Sean Hunt3e518bd2009-11-29 07:34:05 +0000842 mangleSourceName(Name.getCXXLiteralIdentifier());
843 break;
844
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000845 case DeclarationName::CXXUsingDirective:
846 assert(false && "Can't mangle a using directive name!");
Douglas Gregor219cc612009-02-13 01:28:03 +0000847 break;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000848 }
849}
850
851void CXXNameMangler::mangleSourceName(const IdentifierInfo *II) {
852 // <source-name> ::= <positive length number> <identifier>
853 // <number> ::= [n] <non-negative decimal integer>
854 // <identifier> ::= <unqualified source code identifier>
855 Out << II->getLength() << II->getName();
856}
857
Eli Friedman7facf842009-12-02 20:32:49 +0000858void CXXNameMangler::mangleNestedName(const NamedDecl *ND,
Fariborz Jahanian57058532010-03-03 19:41:08 +0000859 const DeclContext *DC,
860 bool NoFunction) {
Douglas Gregor0a9a6d62011-01-26 17:36:28 +0000861 // <nested-name>
862 // ::= N [<CV-qualifiers>] [<ref-qualifier>] <prefix> <unqualified-name> E
863 // ::= N [<CV-qualifiers>] [<ref-qualifier>] <template-prefix>
864 // <template-args> E
Anders Carlssond99edc42009-09-26 03:55:37 +0000865
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000866 Out << 'N';
Douglas Gregor0a9a6d62011-01-26 17:36:28 +0000867 if (const CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(ND)) {
John McCall0953e762009-09-24 19:53:00 +0000868 mangleQualifiers(Qualifiers::fromCVRMask(Method->getTypeQualifiers()));
Douglas Gregor0a9a6d62011-01-26 17:36:28 +0000869 mangleRefQualifier(Method->getRefQualifier());
870 }
871
Anders Carlsson2744a062009-09-18 19:00:18 +0000872 // Check if we have a template.
873 const TemplateArgumentList *TemplateArgs = 0;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000874 if (const TemplateDecl *TD = isTemplate(ND, TemplateArgs)) {
Anders Carlsson2744a062009-09-18 19:00:18 +0000875 mangleTemplatePrefix(TD);
Rafael Espindolad9800722010-03-11 14:07:00 +0000876 TemplateParameterList *TemplateParameters = TD->getTemplateParameters();
877 mangleTemplateArgs(*TemplateParameters, *TemplateArgs);
Fariborz Jahanian57058532010-03-03 19:41:08 +0000878 }
879 else {
880 manglePrefix(DC, NoFunction);
Anders Carlsson7482e242009-09-18 04:29:09 +0000881 mangleUnqualifiedName(ND);
882 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000883
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000884 Out << 'E';
885}
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000886void CXXNameMangler::mangleNestedName(const TemplateDecl *TD,
Anders Carlsson7624f212009-09-18 02:42:01 +0000887 const TemplateArgument *TemplateArgs,
888 unsigned NumTemplateArgs) {
Anders Carlssone45117b2009-09-27 19:53:49 +0000889 // <nested-name> ::= N [<CV-qualifiers>] <template-prefix> <template-args> E
890
Anders Carlsson7624f212009-09-18 02:42:01 +0000891 Out << 'N';
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000892
Anders Carlssone45117b2009-09-27 19:53:49 +0000893 mangleTemplatePrefix(TD);
Rafael Espindolad9800722010-03-11 14:07:00 +0000894 TemplateParameterList *TemplateParameters = TD->getTemplateParameters();
895 mangleTemplateArgs(*TemplateParameters, TemplateArgs, NumTemplateArgs);
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000896
Anders Carlsson7624f212009-09-18 02:42:01 +0000897 Out << 'E';
898}
899
Anders Carlsson1b42c792009-04-02 16:24:45 +0000900void CXXNameMangler::mangleLocalName(const NamedDecl *ND) {
901 // <local-name> := Z <function encoding> E <entity name> [<discriminator>]
902 // := Z <function encoding> E s [<discriminator>]
Mike Stump1eb44332009-09-09 15:08:12 +0000903 // <discriminator> := _ <non-negative number>
Fariborz Jahanian57058532010-03-03 19:41:08 +0000904 const DeclContext *DC = ND->getDeclContext();
Anders Carlsson1b42c792009-04-02 16:24:45 +0000905 Out << 'Z';
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000906
Charles Davis685b1d92010-05-26 18:25:27 +0000907 if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(DC)) {
908 mangleObjCMethodName(MD);
John McCall82b7d7b2010-10-18 21:28:44 +0000909 } else if (const CXXRecordDecl *RD = GetLocalClassDecl(ND)) {
910 mangleFunctionEncoding(cast<FunctionDecl>(RD->getDeclContext()));
Fariborz Jahanian57058532010-03-03 19:41:08 +0000911 Out << 'E';
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000912
John McCall82b7d7b2010-10-18 21:28:44 +0000913 // Mangle the name relative to the closest enclosing function.
914 if (ND == RD) // equality ok because RD derived from ND above
915 mangleUnqualifiedName(ND);
916 else
917 mangleNestedName(ND, DC, true /*NoFunction*/);
918
Fariborz Jahanian4819ac42010-03-04 01:02:03 +0000919 unsigned disc;
John McCall82b7d7b2010-10-18 21:28:44 +0000920 if (Context.getNextDiscriminator(RD, disc)) {
Fariborz Jahanian4819ac42010-03-04 01:02:03 +0000921 if (disc < 10)
922 Out << '_' << disc;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000923 else
Fariborz Jahanian4819ac42010-03-04 01:02:03 +0000924 Out << "__" << disc << '_';
925 }
Fariborz Jahanian57058532010-03-03 19:41:08 +0000926
927 return;
928 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000929 else
Fariborz Jahanian57058532010-03-03 19:41:08 +0000930 mangleFunctionEncoding(cast<FunctionDecl>(DC));
Anders Carlsson7b06f6c2009-12-10 03:14:39 +0000931
Anders Carlsson1b42c792009-04-02 16:24:45 +0000932 Out << 'E';
Eli Friedman6f9f25d2009-12-11 20:21:38 +0000933 mangleUnqualifiedName(ND);
Anders Carlsson1b42c792009-04-02 16:24:45 +0000934}
935
Fariborz Jahanian57058532010-03-03 19:41:08 +0000936void CXXNameMangler::manglePrefix(const DeclContext *DC, bool NoFunction) {
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000937 // <prefix> ::= <prefix> <unqualified-name>
938 // ::= <template-prefix> <template-args>
939 // ::= <template-param>
940 // ::= # empty
941 // ::= <substitution>
Anders Carlsson6862fc72009-09-17 04:16:28 +0000942
Anders Carlssonadd28822009-09-22 20:33:31 +0000943 while (isa<LinkageSpecDecl>(DC))
944 DC = DC->getParent();
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000945
Anders Carlsson9263e912009-09-18 18:39:58 +0000946 if (DC->isTranslationUnit())
947 return;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000948
Douglas Gregor35415f52010-05-25 17:04:15 +0000949 if (const BlockDecl *Block = dyn_cast<BlockDecl>(DC)) {
950 manglePrefix(DC->getParent(), NoFunction);
951 llvm::SmallString<64> Name;
Rafael Espindolac4850c22011-02-10 23:59:36 +0000952 llvm::raw_svector_ostream NameStream(Name);
953 Context.mangleBlock(Block, NameStream);
954 NameStream.flush();
Douglas Gregor35415f52010-05-25 17:04:15 +0000955 Out << Name.size() << Name;
956 return;
957 }
958
Anders Carlsson6862fc72009-09-17 04:16:28 +0000959 if (mangleSubstitution(cast<NamedDecl>(DC)))
960 return;
Anders Carlsson7482e242009-09-18 04:29:09 +0000961
Anders Carlsson2ee3fca2009-09-18 20:11:09 +0000962 // Check if we have a template.
963 const TemplateArgumentList *TemplateArgs = 0;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000964 if (const TemplateDecl *TD = isTemplate(cast<NamedDecl>(DC), TemplateArgs)) {
Anders Carlsson2ee3fca2009-09-18 20:11:09 +0000965 mangleTemplatePrefix(TD);
Rafael Espindolad9800722010-03-11 14:07:00 +0000966 TemplateParameterList *TemplateParameters = TD->getTemplateParameters();
967 mangleTemplateArgs(*TemplateParameters, *TemplateArgs);
Fariborz Jahanian57058532010-03-03 19:41:08 +0000968 }
Douglas Gregor35415f52010-05-25 17:04:15 +0000969 else if(NoFunction && (isa<FunctionDecl>(DC) || isa<ObjCMethodDecl>(DC)))
Fariborz Jahanian57058532010-03-03 19:41:08 +0000970 return;
Douglas Gregor35415f52010-05-25 17:04:15 +0000971 else if (const ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(DC))
972 mangleObjCMethodName(Method);
Fariborz Jahanian57058532010-03-03 19:41:08 +0000973 else {
974 manglePrefix(DC->getParent(), NoFunction);
Anders Carlsson2ee3fca2009-09-18 20:11:09 +0000975 mangleUnqualifiedName(cast<NamedDecl>(DC));
976 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000977
Anders Carlsson6862fc72009-09-17 04:16:28 +0000978 addSubstitution(cast<NamedDecl>(DC));
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000979}
980
Douglas Gregor20f0cc72010-04-23 03:10:43 +0000981void CXXNameMangler::mangleTemplatePrefix(TemplateName Template) {
982 // <template-prefix> ::= <prefix> <template unqualified-name>
983 // ::= <template-param>
984 // ::= <substitution>
985 if (TemplateDecl *TD = Template.getAsTemplateDecl())
986 return mangleTemplatePrefix(TD);
987
988 if (QualifiedTemplateName *Qualified = Template.getAsQualifiedTemplateName())
989 mangleUnresolvedScope(Qualified->getQualifier());
Sean Huntc3021132010-05-05 15:23:54 +0000990
Douglas Gregor20f0cc72010-04-23 03:10:43 +0000991 if (OverloadedTemplateStorage *Overloaded
992 = Template.getAsOverloadedTemplate()) {
Sean Huntc3021132010-05-05 15:23:54 +0000993 mangleUnqualifiedName(0, (*Overloaded->begin())->getDeclName(),
Douglas Gregor20f0cc72010-04-23 03:10:43 +0000994 UnknownArity);
995 return;
996 }
Sean Huntc3021132010-05-05 15:23:54 +0000997
Douglas Gregor20f0cc72010-04-23 03:10:43 +0000998 DependentTemplateName *Dependent = Template.getAsDependentTemplateName();
999 assert(Dependent && "Unknown template name kind?");
1000 mangleUnresolvedScope(Dependent->getQualifier());
Douglas Gregor1e9268e2010-04-28 05:58:56 +00001001 mangleUnscopedTemplateName(Template);
Douglas Gregor20f0cc72010-04-23 03:10:43 +00001002}
1003
Anders Carlsson0fa6df42009-09-26 19:45:45 +00001004void CXXNameMangler::mangleTemplatePrefix(const TemplateDecl *ND) {
Anders Carlsson7482e242009-09-18 04:29:09 +00001005 // <template-prefix> ::= <prefix> <template unqualified-name>
1006 // ::= <template-param>
1007 // ::= <substitution>
Douglas Gregor32fb4e12010-02-05 20:45:00 +00001008 // <template-template-param> ::= <template-param>
1009 // <substitution>
Anders Carlsson7482e242009-09-18 04:29:09 +00001010
Anders Carlssonaeb85372009-09-26 22:18:22 +00001011 if (mangleSubstitution(ND))
1012 return;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00001013
Douglas Gregor32fb4e12010-02-05 20:45:00 +00001014 // <template-template-param> ::= <template-param>
1015 if (const TemplateTemplateParmDecl *TTP
1016 = dyn_cast<TemplateTemplateParmDecl>(ND)) {
1017 mangleTemplateParameter(TTP->getIndex());
1018 return;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001019 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00001020
Anders Carlssonaa73ab12009-09-18 18:47:07 +00001021 manglePrefix(ND->getDeclContext());
Anders Carlsson1668f202009-09-26 20:13:56 +00001022 mangleUnqualifiedName(ND->getTemplatedDecl());
Anders Carlssonaeb85372009-09-26 22:18:22 +00001023 addSubstitution(ND);
Anders Carlsson7482e242009-09-18 04:29:09 +00001024}
1025
John McCallb6f532e2010-07-14 06:43:17 +00001026/// Mangles a template name under the production <type>. Required for
1027/// template template arguments.
1028/// <type> ::= <class-enum-type>
1029/// ::= <template-param>
1030/// ::= <substitution>
1031void CXXNameMangler::mangleType(TemplateName TN) {
1032 if (mangleSubstitution(TN))
1033 return;
1034
1035 TemplateDecl *TD = 0;
1036
1037 switch (TN.getKind()) {
1038 case TemplateName::QualifiedTemplate:
1039 TD = TN.getAsQualifiedTemplateName()->getTemplateDecl();
1040 goto HaveDecl;
1041
1042 case TemplateName::Template:
1043 TD = TN.getAsTemplateDecl();
1044 goto HaveDecl;
1045
1046 HaveDecl:
1047 if (isa<TemplateTemplateParmDecl>(TD))
1048 mangleTemplateParameter(cast<TemplateTemplateParmDecl>(TD)->getIndex());
1049 else
1050 mangleName(TD);
1051 break;
1052
1053 case TemplateName::OverloadedTemplate:
1054 llvm_unreachable("can't mangle an overloaded template name as a <type>");
1055 break;
1056
1057 case TemplateName::DependentTemplate: {
1058 const DependentTemplateName *Dependent = TN.getAsDependentTemplateName();
1059 assert(Dependent->isIdentifier());
1060
1061 // <class-enum-type> ::= <name>
1062 // <name> ::= <nested-name>
1063 mangleUnresolvedScope(Dependent->getQualifier());
1064 mangleSourceName(Dependent->getIdentifier());
1065 break;
1066 }
1067
Douglas Gregor1aee05d2011-01-15 06:45:20 +00001068 case TemplateName::SubstTemplateTemplateParmPack: {
1069 SubstTemplateTemplateParmPackStorage *SubstPack
1070 = TN.getAsSubstTemplateTemplateParmPack();
1071 mangleTemplateParameter(SubstPack->getParameterPack()->getIndex());
1072 break;
1073 }
John McCallb6f532e2010-07-14 06:43:17 +00001074 }
1075
1076 addSubstitution(TN);
1077}
1078
Mike Stump1eb44332009-09-09 15:08:12 +00001079void
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001080CXXNameMangler::mangleOperatorName(OverloadedOperatorKind OO, unsigned Arity) {
1081 switch (OO) {
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001082 // <operator-name> ::= nw # new
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001083 case OO_New: Out << "nw"; break;
1084 // ::= na # new[]
1085 case OO_Array_New: Out << "na"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001086 // ::= dl # delete
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001087 case OO_Delete: Out << "dl"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001088 // ::= da # delete[]
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001089 case OO_Array_Delete: Out << "da"; break;
1090 // ::= ps # + (unary)
John McCall5e1e89b2010-08-18 19:18:59 +00001091 // ::= pl # + (binary or unknown)
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001092 case OO_Plus:
Anders Carlsson8257d412009-12-22 06:36:32 +00001093 Out << (Arity == 1? "ps" : "pl"); break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001094 // ::= ng # - (unary)
John McCall5e1e89b2010-08-18 19:18:59 +00001095 // ::= mi # - (binary or unknown)
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001096 case OO_Minus:
Anders Carlsson8257d412009-12-22 06:36:32 +00001097 Out << (Arity == 1? "ng" : "mi"); break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001098 // ::= ad # & (unary)
John McCall5e1e89b2010-08-18 19:18:59 +00001099 // ::= an # & (binary or unknown)
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001100 case OO_Amp:
Anders Carlsson8257d412009-12-22 06:36:32 +00001101 Out << (Arity == 1? "ad" : "an"); break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001102 // ::= de # * (unary)
John McCall5e1e89b2010-08-18 19:18:59 +00001103 // ::= ml # * (binary or unknown)
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001104 case OO_Star:
John McCall5e1e89b2010-08-18 19:18:59 +00001105 // Use binary when unknown.
Anders Carlsson8257d412009-12-22 06:36:32 +00001106 Out << (Arity == 1? "de" : "ml"); break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001107 // ::= co # ~
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001108 case OO_Tilde: Out << "co"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001109 // ::= dv # /
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001110 case OO_Slash: Out << "dv"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001111 // ::= rm # %
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001112 case OO_Percent: Out << "rm"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001113 // ::= or # |
1114 case OO_Pipe: Out << "or"; break;
1115 // ::= eo # ^
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001116 case OO_Caret: Out << "eo"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001117 // ::= aS # =
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001118 case OO_Equal: Out << "aS"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001119 // ::= pL # +=
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001120 case OO_PlusEqual: Out << "pL"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001121 // ::= mI # -=
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001122 case OO_MinusEqual: Out << "mI"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001123 // ::= mL # *=
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001124 case OO_StarEqual: Out << "mL"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001125 // ::= dV # /=
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001126 case OO_SlashEqual: Out << "dV"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001127 // ::= rM # %=
1128 case OO_PercentEqual: Out << "rM"; break;
1129 // ::= aN # &=
1130 case OO_AmpEqual: Out << "aN"; break;
1131 // ::= oR # |=
1132 case OO_PipeEqual: Out << "oR"; break;
1133 // ::= eO # ^=
1134 case OO_CaretEqual: Out << "eO"; break;
1135 // ::= ls # <<
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001136 case OO_LessLess: Out << "ls"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001137 // ::= rs # >>
1138 case OO_GreaterGreater: Out << "rs"; break;
1139 // ::= lS # <<=
1140 case OO_LessLessEqual: Out << "lS"; break;
1141 // ::= rS # >>=
1142 case OO_GreaterGreaterEqual: Out << "rS"; break;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001143 // ::= eq # ==
1144 case OO_EqualEqual: Out << "eq"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001145 // ::= ne # !=
1146 case OO_ExclaimEqual: Out << "ne"; break;
1147 // ::= lt # <
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001148 case OO_Less: Out << "lt"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001149 // ::= gt # >
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001150 case OO_Greater: Out << "gt"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001151 // ::= le # <=
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001152 case OO_LessEqual: Out << "le"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001153 // ::= ge # >=
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001154 case OO_GreaterEqual: Out << "ge"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001155 // ::= nt # !
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001156 case OO_Exclaim: Out << "nt"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001157 // ::= aa # &&
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001158 case OO_AmpAmp: Out << "aa"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001159 // ::= oo # ||
1160 case OO_PipePipe: Out << "oo"; break;
1161 // ::= pp # ++
1162 case OO_PlusPlus: Out << "pp"; break;
1163 // ::= mm # --
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001164 case OO_MinusMinus: Out << "mm"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001165 // ::= cm # ,
1166 case OO_Comma: Out << "cm"; break;
1167 // ::= pm # ->*
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001168 case OO_ArrowStar: Out << "pm"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001169 // ::= pt # ->
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001170 case OO_Arrow: Out << "pt"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001171 // ::= cl # ()
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001172 case OO_Call: Out << "cl"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001173 // ::= ix # []
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001174 case OO_Subscript: Out << "ix"; break;
Anders Carlssone170ba72009-12-14 01:45:37 +00001175
1176 // ::= qu # ?
1177 // The conditional operator can't be overloaded, but we still handle it when
1178 // mangling expressions.
1179 case OO_Conditional: Out << "qu"; break;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001180
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001181 case OO_None:
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001182 case NUM_OVERLOADED_OPERATORS:
Mike Stump1eb44332009-09-09 15:08:12 +00001183 assert(false && "Not an overloaded operator");
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001184 break;
1185 }
1186}
1187
John McCall0953e762009-09-24 19:53:00 +00001188void CXXNameMangler::mangleQualifiers(Qualifiers Quals) {
Mike Stump1eb44332009-09-09 15:08:12 +00001189 // <CV-qualifiers> ::= [r] [V] [K] # restrict (C99), volatile, const
John McCall0953e762009-09-24 19:53:00 +00001190 if (Quals.hasRestrict())
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001191 Out << 'r';
John McCall0953e762009-09-24 19:53:00 +00001192 if (Quals.hasVolatile())
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001193 Out << 'V';
John McCall0953e762009-09-24 19:53:00 +00001194 if (Quals.hasConst())
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001195 Out << 'K';
John McCall0953e762009-09-24 19:53:00 +00001196
Douglas Gregor56079f72010-06-14 23:15:08 +00001197 if (Quals.hasAddressSpace()) {
1198 // Extension:
1199 //
1200 // <type> ::= U <address-space-number>
1201 //
1202 // where <address-space-number> is a source name consisting of 'AS'
1203 // followed by the address space <number>.
1204 llvm::SmallString<64> ASString;
1205 ASString = "AS" + llvm::utostr_32(Quals.getAddressSpace());
1206 Out << 'U' << ASString.size() << ASString;
1207 }
1208
John McCall0953e762009-09-24 19:53:00 +00001209 // FIXME: For now, just drop all extension qualifiers on the floor.
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001210}
1211
Douglas Gregor0a9a6d62011-01-26 17:36:28 +00001212void CXXNameMangler::mangleRefQualifier(RefQualifierKind RefQualifier) {
1213 // <ref-qualifier> ::= R # lvalue reference
1214 // ::= O # rvalue-reference
1215 // Proposal to Itanium C++ ABI list on 1/26/11
1216 switch (RefQualifier) {
1217 case RQ_None:
1218 break;
1219
1220 case RQ_LValue:
1221 Out << 'R';
1222 break;
1223
1224 case RQ_RValue:
1225 Out << 'O';
1226 break;
1227 }
1228}
1229
Anders Carlsson7b06f6c2009-12-10 03:14:39 +00001230void CXXNameMangler::mangleObjCMethodName(const ObjCMethodDecl *MD) {
Rafael Espindolaf0be9792011-02-11 02:52:17 +00001231 Context.mangleObjCMethodName(MD, Out);
Anders Carlsson7b06f6c2009-12-10 03:14:39 +00001232}
1233
John McCallb47f7482011-01-26 20:05:40 +00001234void CXXNameMangler::mangleType(QualType nonCanon) {
Anders Carlsson4843e582009-03-10 17:07:44 +00001235 // Only operate on the canonical type!
John McCallb47f7482011-01-26 20:05:40 +00001236 QualType canon = nonCanon.getCanonicalType();
Anders Carlsson4843e582009-03-10 17:07:44 +00001237
John McCallb47f7482011-01-26 20:05:40 +00001238 SplitQualType split = canon.split();
1239 Qualifiers quals = split.second;
1240 const Type *ty = split.first;
1241
1242 bool isSubstitutable = quals || !isa<BuiltinType>(ty);
1243 if (isSubstitutable && mangleSubstitution(canon))
Anders Carlsson76967372009-09-17 00:43:46 +00001244 return;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001245
John McCallb47f7482011-01-26 20:05:40 +00001246 // If we're mangling a qualified array type, push the qualifiers to
1247 // the element type.
1248 if (quals && isa<ArrayType>(ty)) {
1249 ty = Context.getASTContext().getAsArrayType(canon);
1250 quals = Qualifiers();
1251
1252 // Note that we don't update canon: we want to add the
1253 // substitution at the canonical type.
1254 }
1255
1256 if (quals) {
1257 mangleQualifiers(quals);
John McCall0953e762009-09-24 19:53:00 +00001258 // Recurse: even if the qualified type isn't yet substitutable,
1259 // the unqualified type might be.
John McCallb47f7482011-01-26 20:05:40 +00001260 mangleType(QualType(ty, 0));
Anders Carlsson76967372009-09-17 00:43:46 +00001261 } else {
John McCallb47f7482011-01-26 20:05:40 +00001262 switch (ty->getTypeClass()) {
John McCallefe6aee2009-09-05 07:56:18 +00001263#define ABSTRACT_TYPE(CLASS, PARENT)
1264#define NON_CANONICAL_TYPE(CLASS, PARENT) \
Anders Carlsson76967372009-09-17 00:43:46 +00001265 case Type::CLASS: \
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +00001266 llvm_unreachable("can't mangle non-canonical type " #CLASS "Type"); \
Anders Carlsson76967372009-09-17 00:43:46 +00001267 return;
John McCallefe6aee2009-09-05 07:56:18 +00001268#define TYPE(CLASS, PARENT) \
Anders Carlsson76967372009-09-17 00:43:46 +00001269 case Type::CLASS: \
John McCallb47f7482011-01-26 20:05:40 +00001270 mangleType(static_cast<const CLASS##Type*>(ty)); \
Anders Carlsson76967372009-09-17 00:43:46 +00001271 break;
John McCallefe6aee2009-09-05 07:56:18 +00001272#include "clang/AST/TypeNodes.def"
Anders Carlsson76967372009-09-17 00:43:46 +00001273 }
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001274 }
Anders Carlsson76967372009-09-17 00:43:46 +00001275
1276 // Add the substitution.
John McCallb47f7482011-01-26 20:05:40 +00001277 if (isSubstitutable)
1278 addSubstitution(canon);
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001279}
1280
Douglas Gregor1b12a3b2010-05-26 05:11:13 +00001281void CXXNameMangler::mangleNameOrStandardSubstitution(const NamedDecl *ND) {
1282 if (!mangleStandardSubstitution(ND))
1283 mangleName(ND);
1284}
1285
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001286void CXXNameMangler::mangleType(const BuiltinType *T) {
John McCallefe6aee2009-09-05 07:56:18 +00001287 // <type> ::= <builtin-type>
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001288 // <builtin-type> ::= v # void
1289 // ::= w # wchar_t
1290 // ::= b # bool
1291 // ::= c # char
1292 // ::= a # signed char
1293 // ::= h # unsigned char
1294 // ::= s # short
1295 // ::= t # unsigned short
1296 // ::= i # int
1297 // ::= j # unsigned int
1298 // ::= l # long
1299 // ::= m # unsigned long
1300 // ::= x # long long, __int64
1301 // ::= y # unsigned long long, __int64
1302 // ::= n # __int128
1303 // UNSUPPORTED: ::= o # unsigned __int128
1304 // ::= f # float
1305 // ::= d # double
1306 // ::= e # long double, __float80
1307 // UNSUPPORTED: ::= g # __float128
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001308 // UNSUPPORTED: ::= Dd # IEEE 754r decimal floating point (64 bits)
1309 // UNSUPPORTED: ::= De # IEEE 754r decimal floating point (128 bits)
1310 // UNSUPPORTED: ::= Df # IEEE 754r decimal floating point (32 bits)
1311 // UNSUPPORTED: ::= Dh # IEEE 754r half-precision floating point (16 bits)
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001312 // ::= Di # char32_t
1313 // ::= Ds # char16_t
Anders Carlssone2923682010-11-04 04:31:32 +00001314 // ::= Dn # std::nullptr_t (i.e., decltype(nullptr))
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001315 // ::= u <source-name> # vendor extended type
1316 switch (T->getKind()) {
1317 case BuiltinType::Void: Out << 'v'; break;
1318 case BuiltinType::Bool: Out << 'b'; break;
1319 case BuiltinType::Char_U: case BuiltinType::Char_S: Out << 'c'; break;
1320 case BuiltinType::UChar: Out << 'h'; break;
1321 case BuiltinType::UShort: Out << 't'; break;
1322 case BuiltinType::UInt: Out << 'j'; break;
1323 case BuiltinType::ULong: Out << 'm'; break;
1324 case BuiltinType::ULongLong: Out << 'y'; break;
Chris Lattner2df9ced2009-04-30 02:43:43 +00001325 case BuiltinType::UInt128: Out << 'o'; break;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001326 case BuiltinType::SChar: Out << 'a'; break;
Chris Lattner3f59c972010-12-25 23:25:43 +00001327 case BuiltinType::WChar_S:
1328 case BuiltinType::WChar_U: Out << 'w'; break;
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001329 case BuiltinType::Char16: Out << "Ds"; break;
1330 case BuiltinType::Char32: Out << "Di"; break;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001331 case BuiltinType::Short: Out << 's'; break;
1332 case BuiltinType::Int: Out << 'i'; break;
1333 case BuiltinType::Long: Out << 'l'; break;
1334 case BuiltinType::LongLong: Out << 'x'; break;
Chris Lattner2df9ced2009-04-30 02:43:43 +00001335 case BuiltinType::Int128: Out << 'n'; break;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001336 case BuiltinType::Float: Out << 'f'; break;
1337 case BuiltinType::Double: Out << 'd'; break;
1338 case BuiltinType::LongDouble: Out << 'e'; break;
Anders Carlssone2923682010-11-04 04:31:32 +00001339 case BuiltinType::NullPtr: Out << "Dn"; break;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001340
1341 case BuiltinType::Overload:
1342 case BuiltinType::Dependent:
John McCall1de4d4e2011-04-07 08:22:57 +00001343 case BuiltinType::UnknownAny:
Mike Stump1eb44332009-09-09 15:08:12 +00001344 assert(false &&
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001345 "Overloaded and dependent types shouldn't get to name mangling");
1346 break;
Steve Naroff9533a7f2009-07-22 17:14:51 +00001347 case BuiltinType::ObjCId: Out << "11objc_object"; break;
1348 case BuiltinType::ObjCClass: Out << "10objc_class"; break;
Fariborz Jahanian13dcd002009-11-21 19:53:08 +00001349 case BuiltinType::ObjCSel: Out << "13objc_selector"; break;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001350 }
1351}
1352
John McCallefe6aee2009-09-05 07:56:18 +00001353// <type> ::= <function-type>
1354// <function-type> ::= F [Y] <bare-function-type> E
1355void CXXNameMangler::mangleType(const FunctionProtoType *T) {
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001356 Out << 'F';
Mike Stumpf5408fe2009-05-16 07:57:57 +00001357 // FIXME: We don't have enough information in the AST to produce the 'Y'
1358 // encoding for extern "C" function types.
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001359 mangleBareFunctionType(T, /*MangleReturnType=*/true);
1360 Out << 'E';
1361}
John McCallefe6aee2009-09-05 07:56:18 +00001362void CXXNameMangler::mangleType(const FunctionNoProtoType *T) {
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +00001363 llvm_unreachable("Can't mangle K&R function prototypes");
John McCallefe6aee2009-09-05 07:56:18 +00001364}
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001365void CXXNameMangler::mangleBareFunctionType(const FunctionType *T,
1366 bool MangleReturnType) {
John McCallefe6aee2009-09-05 07:56:18 +00001367 // We should never be mangling something without a prototype.
1368 const FunctionProtoType *Proto = cast<FunctionProtoType>(T);
1369
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001370 // <bare-function-type> ::= <signature type>+
1371 if (MangleReturnType)
John McCallefe6aee2009-09-05 07:56:18 +00001372 mangleType(Proto->getResultType());
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001373
Anders Carlsson93296682010-06-02 04:40:13 +00001374 if (Proto->getNumArgs() == 0 && !Proto->isVariadic()) {
Eli Friedmana7e68452010-08-22 01:00:03 +00001375 // <builtin-type> ::= v # void
Anders Carlssonc6c91bc2009-04-01 00:15:23 +00001376 Out << 'v';
1377 return;
1378 }
Mike Stump1eb44332009-09-09 15:08:12 +00001379
Douglas Gregor72564e72009-02-26 23:50:07 +00001380 for (FunctionProtoType::arg_type_iterator Arg = Proto->arg_type_begin(),
Mike Stump1eb44332009-09-09 15:08:12 +00001381 ArgEnd = Proto->arg_type_end();
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001382 Arg != ArgEnd; ++Arg)
1383 mangleType(*Arg);
Douglas Gregor219cc612009-02-13 01:28:03 +00001384
1385 // <builtin-type> ::= z # ellipsis
1386 if (Proto->isVariadic())
1387 Out << 'z';
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001388}
1389
John McCallefe6aee2009-09-05 07:56:18 +00001390// <type> ::= <class-enum-type>
Mike Stump1eb44332009-09-09 15:08:12 +00001391// <class-enum-type> ::= <name>
John McCalled976492009-12-04 22:46:56 +00001392void CXXNameMangler::mangleType(const UnresolvedUsingType *T) {
1393 mangleName(T->getDecl());
1394}
1395
1396// <type> ::= <class-enum-type>
1397// <class-enum-type> ::= <name>
John McCallefe6aee2009-09-05 07:56:18 +00001398void CXXNameMangler::mangleType(const EnumType *T) {
1399 mangleType(static_cast<const TagType*>(T));
1400}
1401void CXXNameMangler::mangleType(const RecordType *T) {
1402 mangleType(static_cast<const TagType*>(T));
1403}
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001404void CXXNameMangler::mangleType(const TagType *T) {
Eli Friedmanecb7e932009-12-11 18:00:57 +00001405 mangleName(T->getDecl());
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001406}
1407
John McCallefe6aee2009-09-05 07:56:18 +00001408// <type> ::= <array-type>
1409// <array-type> ::= A <positive dimension number> _ <element type>
1410// ::= A [<dimension expression>] _ <element type>
1411void CXXNameMangler::mangleType(const ConstantArrayType *T) {
1412 Out << 'A' << T->getSize() << '_';
1413 mangleType(T->getElementType());
1414}
1415void CXXNameMangler::mangleType(const VariableArrayType *T) {
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001416 Out << 'A';
Fariborz Jahanian7281d1f2010-11-02 16:54:00 +00001417 // decayed vla types (size 0) will just be skipped.
1418 if (T->getSizeExpr())
1419 mangleExpression(T->getSizeExpr());
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001420 Out << '_';
1421 mangleType(T->getElementType());
1422}
John McCallefe6aee2009-09-05 07:56:18 +00001423void CXXNameMangler::mangleType(const DependentSizedArrayType *T) {
1424 Out << 'A';
1425 mangleExpression(T->getSizeExpr());
1426 Out << '_';
1427 mangleType(T->getElementType());
1428}
1429void CXXNameMangler::mangleType(const IncompleteArrayType *T) {
Nick Lewycky271b6652010-09-05 03:40:33 +00001430 Out << "A_";
John McCallefe6aee2009-09-05 07:56:18 +00001431 mangleType(T->getElementType());
1432}
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001433
John McCallefe6aee2009-09-05 07:56:18 +00001434// <type> ::= <pointer-to-member-type>
1435// <pointer-to-member-type> ::= M <class type> <member type>
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001436void CXXNameMangler::mangleType(const MemberPointerType *T) {
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001437 Out << 'M';
1438 mangleType(QualType(T->getClass(), 0));
Anders Carlsson0e650012009-05-17 17:41:20 +00001439 QualType PointeeType = T->getPointeeType();
1440 if (const FunctionProtoType *FPT = dyn_cast<FunctionProtoType>(PointeeType)) {
John McCall0953e762009-09-24 19:53:00 +00001441 mangleQualifiers(Qualifiers::fromCVRMask(FPT->getTypeQuals()));
Douglas Gregor0a9a6d62011-01-26 17:36:28 +00001442 mangleRefQualifier(FPT->getRefQualifier());
Anders Carlsson0e650012009-05-17 17:41:20 +00001443 mangleType(FPT);
Anders Carlsson9d85b722010-06-02 04:29:50 +00001444
1445 // Itanium C++ ABI 5.1.8:
1446 //
1447 // The type of a non-static member function is considered to be different,
1448 // for the purposes of substitution, from the type of a namespace-scope or
1449 // static member function whose type appears similar. The types of two
1450 // non-static member functions are considered to be different, for the
1451 // purposes of substitution, if the functions are members of different
1452 // classes. In other words, for the purposes of substitution, the class of
1453 // which the function is a member is considered part of the type of
1454 // function.
1455
1456 // We increment the SeqID here to emulate adding an entry to the
1457 // substitution table. We can't actually add it because we don't want this
1458 // particular function type to be substituted.
1459 ++SeqID;
Mike Stump1eb44332009-09-09 15:08:12 +00001460 } else
Anders Carlsson0e650012009-05-17 17:41:20 +00001461 mangleType(PointeeType);
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001462}
1463
John McCallefe6aee2009-09-05 07:56:18 +00001464// <type> ::= <template-param>
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001465void CXXNameMangler::mangleType(const TemplateTypeParmType *T) {
Anders Carlsson0ccdf8d2009-09-27 00:38:53 +00001466 mangleTemplateParameter(T->getIndex());
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001467}
1468
Douglas Gregorc3069d62011-01-14 02:55:32 +00001469// <type> ::= <template-param>
1470void CXXNameMangler::mangleType(const SubstTemplateTypeParmPackType *T) {
1471 mangleTemplateParameter(T->getReplacedParameter()->getIndex());
1472}
1473
John McCallefe6aee2009-09-05 07:56:18 +00001474// <type> ::= P <type> # pointer-to
1475void CXXNameMangler::mangleType(const PointerType *T) {
1476 Out << 'P';
1477 mangleType(T->getPointeeType());
1478}
1479void CXXNameMangler::mangleType(const ObjCObjectPointerType *T) {
1480 Out << 'P';
1481 mangleType(T->getPointeeType());
1482}
1483
1484// <type> ::= R <type> # reference-to
1485void CXXNameMangler::mangleType(const LValueReferenceType *T) {
1486 Out << 'R';
1487 mangleType(T->getPointeeType());
1488}
1489
1490// <type> ::= O <type> # rvalue reference-to (C++0x)
1491void CXXNameMangler::mangleType(const RValueReferenceType *T) {
1492 Out << 'O';
1493 mangleType(T->getPointeeType());
1494}
1495
1496// <type> ::= C <type> # complex pair (C 2000)
1497void CXXNameMangler::mangleType(const ComplexType *T) {
1498 Out << 'C';
1499 mangleType(T->getElementType());
1500}
1501
Bob Wilsonc7df92d2010-11-12 17:24:43 +00001502// ARM's ABI for Neon vector types specifies that they should be mangled as
Bob Wilson57147a82010-11-16 00:32:18 +00001503// if they are structs (to match ARM's initial implementation). The
1504// vector type must be one of the special types predefined by ARM.
1505void CXXNameMangler::mangleNeonVectorType(const VectorType *T) {
Bob Wilsonc7df92d2010-11-12 17:24:43 +00001506 QualType EltType = T->getElementType();
Bob Wilson57147a82010-11-16 00:32:18 +00001507 assert(EltType->isBuiltinType() && "Neon vector element not a BuiltinType");
Bob Wilsonc7df92d2010-11-12 17:24:43 +00001508 const char *EltName = 0;
Bob Wilson491328c2010-11-12 17:24:46 +00001509 if (T->getVectorKind() == VectorType::NeonPolyVector) {
1510 switch (cast<BuiltinType>(EltType)->getKind()) {
Bob Wilson4cfaa5d2010-11-12 17:24:49 +00001511 case BuiltinType::SChar: EltName = "poly8_t"; break;
1512 case BuiltinType::Short: EltName = "poly16_t"; break;
Bob Wilson57147a82010-11-16 00:32:18 +00001513 default: llvm_unreachable("unexpected Neon polynomial vector element type");
Bob Wilson491328c2010-11-12 17:24:46 +00001514 }
1515 } else {
1516 switch (cast<BuiltinType>(EltType)->getKind()) {
Bob Wilson4cfaa5d2010-11-12 17:24:49 +00001517 case BuiltinType::SChar: EltName = "int8_t"; break;
1518 case BuiltinType::UChar: EltName = "uint8_t"; break;
1519 case BuiltinType::Short: EltName = "int16_t"; break;
1520 case BuiltinType::UShort: EltName = "uint16_t"; break;
1521 case BuiltinType::Int: EltName = "int32_t"; break;
1522 case BuiltinType::UInt: EltName = "uint32_t"; break;
1523 case BuiltinType::LongLong: EltName = "int64_t"; break;
1524 case BuiltinType::ULongLong: EltName = "uint64_t"; break;
1525 case BuiltinType::Float: EltName = "float32_t"; break;
Bob Wilson57147a82010-11-16 00:32:18 +00001526 default: llvm_unreachable("unexpected Neon vector element type");
Bob Wilson491328c2010-11-12 17:24:46 +00001527 }
Bob Wilsonc7df92d2010-11-12 17:24:43 +00001528 }
1529 const char *BaseName = 0;
Bob Wilson4cfaa5d2010-11-12 17:24:49 +00001530 unsigned BitSize = (T->getNumElements() *
Bob Wilson3a723022010-11-16 00:32:12 +00001531 getASTContext().getTypeSize(EltType));
Bob Wilsonc7df92d2010-11-12 17:24:43 +00001532 if (BitSize == 64)
1533 BaseName = "__simd64_";
Bob Wilson57147a82010-11-16 00:32:18 +00001534 else {
1535 assert(BitSize == 128 && "Neon vector type not 64 or 128 bits");
Bob Wilsonc7df92d2010-11-12 17:24:43 +00001536 BaseName = "__simd128_";
Bob Wilson57147a82010-11-16 00:32:18 +00001537 }
Bob Wilsonc7df92d2010-11-12 17:24:43 +00001538 Out << strlen(BaseName) + strlen(EltName);
1539 Out << BaseName << EltName;
Bob Wilsonc7df92d2010-11-12 17:24:43 +00001540}
1541
John McCallefe6aee2009-09-05 07:56:18 +00001542// GNU extension: vector types
Chris Lattner788b0fd2010-06-23 06:00:24 +00001543// <type> ::= <vector-type>
1544// <vector-type> ::= Dv <positive dimension number> _
1545// <extended element type>
1546// ::= Dv [<dimension expression>] _ <element type>
1547// <extended element type> ::= <element type>
1548// ::= p # AltiVec vector pixel
John McCallefe6aee2009-09-05 07:56:18 +00001549void CXXNameMangler::mangleType(const VectorType *T) {
Bob Wilson491328c2010-11-12 17:24:46 +00001550 if ((T->getVectorKind() == VectorType::NeonVector ||
Bob Wilson57147a82010-11-16 00:32:18 +00001551 T->getVectorKind() == VectorType::NeonPolyVector)) {
1552 mangleNeonVectorType(T);
Bob Wilsonc7df92d2010-11-12 17:24:43 +00001553 return;
Bob Wilson57147a82010-11-16 00:32:18 +00001554 }
Nick Lewycky0e5f0672010-03-26 07:18:04 +00001555 Out << "Dv" << T->getNumElements() << '_';
Bob Wilsone86d78c2010-11-10 21:56:12 +00001556 if (T->getVectorKind() == VectorType::AltiVecPixel)
Chris Lattner788b0fd2010-06-23 06:00:24 +00001557 Out << 'p';
Bob Wilsone86d78c2010-11-10 21:56:12 +00001558 else if (T->getVectorKind() == VectorType::AltiVecBool)
Chris Lattner788b0fd2010-06-23 06:00:24 +00001559 Out << 'b';
1560 else
1561 mangleType(T->getElementType());
John McCallefe6aee2009-09-05 07:56:18 +00001562}
1563void CXXNameMangler::mangleType(const ExtVectorType *T) {
1564 mangleType(static_cast<const VectorType*>(T));
1565}
1566void CXXNameMangler::mangleType(const DependentSizedExtVectorType *T) {
Nick Lewycky0e5f0672010-03-26 07:18:04 +00001567 Out << "Dv";
1568 mangleExpression(T->getSizeExpr());
1569 Out << '_';
John McCallefe6aee2009-09-05 07:56:18 +00001570 mangleType(T->getElementType());
1571}
1572
Douglas Gregor7536dd52010-12-20 02:24:11 +00001573void CXXNameMangler::mangleType(const PackExpansionType *T) {
Douglas Gregor4fc48662011-01-13 16:39:34 +00001574 // <type> ::= Dp <type> # pack expansion (C++0x)
Douglas Gregor255c2692011-01-13 17:44:36 +00001575 Out << "Dp";
Douglas Gregor7536dd52010-12-20 02:24:11 +00001576 mangleType(T->getPattern());
1577}
1578
Anders Carlssona40c5e42009-03-07 22:03:21 +00001579void CXXNameMangler::mangleType(const ObjCInterfaceType *T) {
1580 mangleSourceName(T->getDecl()->getIdentifier());
1581}
1582
John McCallc12c5bb2010-05-15 11:32:37 +00001583void CXXNameMangler::mangleType(const ObjCObjectType *T) {
John McCallc00c1f62010-05-15 17:06:29 +00001584 // We don't allow overloading by different protocol qualification,
1585 // so mangling them isn't necessary.
John McCallc12c5bb2010-05-15 11:32:37 +00001586 mangleType(T->getBaseType());
1587}
1588
John McCallefe6aee2009-09-05 07:56:18 +00001589void CXXNameMangler::mangleType(const BlockPointerType *T) {
Anders Carlssonf28c6872009-12-23 22:31:44 +00001590 Out << "U13block_pointer";
1591 mangleType(T->getPointeeType());
John McCallefe6aee2009-09-05 07:56:18 +00001592}
1593
John McCall31f17ec2010-04-27 00:57:59 +00001594void CXXNameMangler::mangleType(const InjectedClassNameType *T) {
1595 // Mangle injected class name types as if the user had written the
1596 // specialization out fully. It may not actually be possible to see
1597 // this mangling, though.
1598 mangleType(T->getInjectedSpecializationType());
1599}
1600
John McCallefe6aee2009-09-05 07:56:18 +00001601void CXXNameMangler::mangleType(const TemplateSpecializationType *T) {
Douglas Gregor1e9268e2010-04-28 05:58:56 +00001602 if (TemplateDecl *TD = T->getTemplateName().getAsTemplateDecl()) {
1603 mangleName(TD, T->getArgs(), T->getNumArgs());
1604 } else {
1605 if (mangleSubstitution(QualType(T, 0)))
1606 return;
Sean Huntc3021132010-05-05 15:23:54 +00001607
Douglas Gregor1e9268e2010-04-28 05:58:56 +00001608 mangleTemplatePrefix(T->getTemplateName());
Sean Huntc3021132010-05-05 15:23:54 +00001609
Douglas Gregor1e9268e2010-04-28 05:58:56 +00001610 // FIXME: GCC does not appear to mangle the template arguments when
1611 // the template in question is a dependent template name. Should we
1612 // emulate that badness?
1613 mangleTemplateArgs(T->getTemplateName(), T->getArgs(), T->getNumArgs());
1614 addSubstitution(QualType(T, 0));
1615 }
John McCallefe6aee2009-09-05 07:56:18 +00001616}
1617
Douglas Gregor4714c122010-03-31 17:34:00 +00001618void CXXNameMangler::mangleType(const DependentNameType *T) {
Anders Carlssonae352482009-09-26 02:26:02 +00001619 // Typename types are always nested
1620 Out << 'N';
John McCall33500952010-06-11 00:33:02 +00001621 mangleUnresolvedScope(T->getQualifier());
1622 mangleSourceName(T->getIdentifier());
1623 Out << 'E';
1624}
John McCall6ab30e02010-06-09 07:26:17 +00001625
John McCall33500952010-06-11 00:33:02 +00001626void CXXNameMangler::mangleType(const DependentTemplateSpecializationType *T) {
Douglas Gregoraa2187d2011-02-28 00:04:36 +00001627 // Dependently-scoped template types are nested if they have a prefix.
John McCall33500952010-06-11 00:33:02 +00001628 Out << 'N';
1629
1630 // TODO: avoid making this TemplateName.
1631 TemplateName Prefix =
1632 getASTContext().getDependentTemplateName(T->getQualifier(),
1633 T->getIdentifier());
1634 mangleTemplatePrefix(Prefix);
1635
1636 // FIXME: GCC does not appear to mangle the template arguments when
1637 // the template in question is a dependent template name. Should we
1638 // emulate that badness?
1639 mangleTemplateArgs(Prefix, T->getArgs(), T->getNumArgs());
Anders Carlssonae352482009-09-26 02:26:02 +00001640 Out << 'E';
John McCallefe6aee2009-09-05 07:56:18 +00001641}
1642
John McCallad5e7382010-03-01 23:49:17 +00001643void CXXNameMangler::mangleType(const TypeOfType *T) {
1644 // FIXME: this is pretty unsatisfactory, but there isn't an obvious
1645 // "extension with parameters" mangling.
1646 Out << "u6typeof";
1647}
1648
1649void CXXNameMangler::mangleType(const TypeOfExprType *T) {
1650 // FIXME: this is pretty unsatisfactory, but there isn't an obvious
1651 // "extension with parameters" mangling.
1652 Out << "u6typeof";
1653}
1654
1655void CXXNameMangler::mangleType(const DecltypeType *T) {
1656 Expr *E = T->getUnderlyingExpr();
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001657
John McCallad5e7382010-03-01 23:49:17 +00001658 // type ::= Dt <expression> E # decltype of an id-expression
1659 // # or class member access
1660 // ::= DT <expression> E # decltype of an expression
1661
1662 // This purports to be an exhaustive list of id-expressions and
1663 // class member accesses. Note that we do not ignore parentheses;
1664 // parentheses change the semantics of decltype for these
1665 // expressions (and cause the mangler to use the other form).
1666 if (isa<DeclRefExpr>(E) ||
1667 isa<MemberExpr>(E) ||
1668 isa<UnresolvedLookupExpr>(E) ||
1669 isa<DependentScopeDeclRefExpr>(E) ||
1670 isa<CXXDependentScopeMemberExpr>(E) ||
1671 isa<UnresolvedMemberExpr>(E))
1672 Out << "Dt";
1673 else
1674 Out << "DT";
1675 mangleExpression(E);
1676 Out << 'E';
1677}
1678
Richard Smith34b41d92011-02-20 03:19:35 +00001679void CXXNameMangler::mangleType(const AutoType *T) {
1680 QualType D = T->getDeducedType();
Richard Smith967ecd32011-02-21 20:10:02 +00001681 // <builtin-type> ::= Da # dependent auto
1682 if (D.isNull())
1683 Out << "Da";
1684 else
1685 mangleType(D);
Richard Smith34b41d92011-02-20 03:19:35 +00001686}
1687
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001688void CXXNameMangler::mangleIntegerLiteral(QualType T,
Anders Carlssone170ba72009-12-14 01:45:37 +00001689 const llvm::APSInt &Value) {
1690 // <expr-primary> ::= L <type> <value number> E # integer literal
1691 Out << 'L';
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001692
Anders Carlssone170ba72009-12-14 01:45:37 +00001693 mangleType(T);
1694 if (T->isBooleanType()) {
1695 // Boolean values are encoded as 0/1.
1696 Out << (Value.getBoolValue() ? '1' : '0');
1697 } else {
John McCall0512e482010-07-14 04:20:34 +00001698 mangleNumber(Value);
Anders Carlssone170ba72009-12-14 01:45:37 +00001699 }
1700 Out << 'E';
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001701
Anders Carlssone170ba72009-12-14 01:45:37 +00001702}
1703
John McCall2f27bf82010-02-04 02:56:29 +00001704/// Mangles a member expression. Implicit accesses are not handled,
1705/// but that should be okay, because you shouldn't be able to
1706/// make an implicit access in a function template declaration.
John McCall2f27bf82010-02-04 02:56:29 +00001707void CXXNameMangler::mangleMemberExpr(const Expr *Base,
1708 bool IsArrow,
1709 NestedNameSpecifier *Qualifier,
1710 DeclarationName Member,
1711 unsigned Arity) {
John McCalle1e342f2010-03-01 19:12:25 +00001712 // gcc-4.4 uses 'dt' for dot expressions, which is reasonable.
1713 // OTOH, gcc also mangles the name as an expression.
1714 Out << (IsArrow ? "pt" : "dt");
John McCall2f27bf82010-02-04 02:56:29 +00001715 mangleExpression(Base);
1716 mangleUnresolvedName(Qualifier, Member, Arity);
1717}
1718
John McCall5e1e89b2010-08-18 19:18:59 +00001719void CXXNameMangler::mangleExpression(const Expr *E, unsigned Arity) {
Anders Carlssond553f8c2009-09-21 01:21:10 +00001720 // <expression> ::= <unary operator-name> <expression>
John McCall09cc1412010-02-03 00:55:45 +00001721 // ::= <binary operator-name> <expression> <expression>
1722 // ::= <trinary operator-name> <expression> <expression> <expression>
Eli Friedmana7e68452010-08-22 01:00:03 +00001723 // ::= cl <expression>* E # call
Anders Carlssond553f8c2009-09-21 01:21:10 +00001724 // ::= cv <type> expression # conversion with one argument
1725 // ::= cv <type> _ <expression>* E # conversion with a different number of arguments
Eli Friedmana7e68452010-08-22 01:00:03 +00001726 // ::= st <type> # sizeof (a type)
Anders Carlssond553f8c2009-09-21 01:21:10 +00001727 // ::= at <type> # alignof (a type)
1728 // ::= <template-param>
1729 // ::= <function-param>
1730 // ::= sr <type> <unqualified-name> # dependent name
1731 // ::= sr <type> <unqualified-name> <template-args> # dependent template-id
1732 // ::= sZ <template-param> # size of a parameter pack
Douglas Gregor4fc48662011-01-13 16:39:34 +00001733 // ::= sZ <function-param> # size of a function parameter pack
John McCall09cc1412010-02-03 00:55:45 +00001734 // ::= <expr-primary>
John McCall1dd73832010-02-04 01:42:13 +00001735 // <expr-primary> ::= L <type> <value number> E # integer literal
1736 // ::= L <type <value float> E # floating literal
1737 // ::= L <mangled-name> E # external name
Anders Carlssond553f8c2009-09-21 01:21:10 +00001738 switch (E->getStmtClass()) {
John McCall6ae1f352010-04-09 22:26:14 +00001739 case Expr::NoStmtClass:
John McCall63c00d72011-02-09 08:16:59 +00001740#define ABSTRACT_STMT(Type)
John McCall6ae1f352010-04-09 22:26:14 +00001741#define EXPR(Type, Base)
1742#define STMT(Type, Base) \
1743 case Expr::Type##Class:
Sean Hunt4bfe1962010-05-05 15:24:00 +00001744#include "clang/AST/StmtNodes.inc"
John McCall0512e482010-07-14 04:20:34 +00001745 // fallthrough
1746
1747 // These all can only appear in local or variable-initialization
1748 // contexts and so should never appear in a mangling.
1749 case Expr::AddrLabelExprClass:
1750 case Expr::BlockDeclRefExprClass:
1751 case Expr::CXXThisExprClass:
1752 case Expr::DesignatedInitExprClass:
1753 case Expr::ImplicitValueInitExprClass:
1754 case Expr::InitListExprClass:
1755 case Expr::ParenListExprClass:
1756 case Expr::CXXScalarValueInitExprClass:
John McCall09cc1412010-02-03 00:55:45 +00001757 llvm_unreachable("unexpected statement kind");
1758 break;
1759
John McCall0512e482010-07-14 04:20:34 +00001760 // FIXME: invent manglings for all these.
1761 case Expr::BlockExprClass:
1762 case Expr::CXXPseudoDestructorExprClass:
1763 case Expr::ChooseExprClass:
1764 case Expr::CompoundLiteralExprClass:
1765 case Expr::ExtVectorElementExprClass:
Peter Collingbournef111d932011-04-15 00:35:48 +00001766 case Expr::GenericSelectionExprClass:
John McCall0512e482010-07-14 04:20:34 +00001767 case Expr::ObjCEncodeExprClass:
John McCall0512e482010-07-14 04:20:34 +00001768 case Expr::ObjCIsaExprClass:
1769 case Expr::ObjCIvarRefExprClass:
1770 case Expr::ObjCMessageExprClass:
1771 case Expr::ObjCPropertyRefExprClass:
1772 case Expr::ObjCProtocolExprClass:
1773 case Expr::ObjCSelectorExprClass:
1774 case Expr::ObjCStringLiteralClass:
John McCall0512e482010-07-14 04:20:34 +00001775 case Expr::OffsetOfExprClass:
1776 case Expr::PredefinedExprClass:
1777 case Expr::ShuffleVectorExprClass:
1778 case Expr::StmtExprClass:
John McCall0512e482010-07-14 04:20:34 +00001779 case Expr::UnaryTypeTraitExprClass:
Francois Pichet6ad6f282010-12-07 00:08:36 +00001780 case Expr::BinaryTypeTraitExprClass:
Francois Pichet9be88402010-09-08 23:47:05 +00001781 case Expr::VAArgExprClass:
Sebastian Redl2e156222010-09-10 20:55:43 +00001782 case Expr::CXXUuidofExprClass:
Peter Collingbournee08ce652011-02-09 21:07:24 +00001783 case Expr::CXXNoexceptExprClass:
1784 case Expr::CUDAKernelCallExprClass: {
John McCall6ae1f352010-04-09 22:26:14 +00001785 // As bad as this diagnostic is, it's better than crashing.
1786 Diagnostic &Diags = Context.getDiags();
1787 unsigned DiagID = Diags.getCustomDiagID(Diagnostic::Error,
1788 "cannot yet mangle expression type %0");
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +00001789 Diags.Report(E->getExprLoc(), DiagID)
John McCall739bf092010-04-10 09:39:25 +00001790 << E->getStmtClassName() << E->getSourceRange();
John McCall6ae1f352010-04-09 22:26:14 +00001791 break;
1792 }
1793
John McCall56ca35d2011-02-17 10:25:35 +00001794 // Even gcc-4.5 doesn't mangle this.
1795 case Expr::BinaryConditionalOperatorClass: {
1796 Diagnostic &Diags = Context.getDiags();
1797 unsigned DiagID =
1798 Diags.getCustomDiagID(Diagnostic::Error,
1799 "?: operator with omitted middle operand cannot be mangled");
1800 Diags.Report(E->getExprLoc(), DiagID)
1801 << E->getStmtClassName() << E->getSourceRange();
1802 break;
1803 }
1804
1805 // These are used for internal purposes and cannot be meaningfully mangled.
John McCall7cd7d1a2010-11-15 23:31:06 +00001806 case Expr::OpaqueValueExprClass:
1807 llvm_unreachable("cannot mangle opaque value; mangling wrong thing?");
1808
John McCall0512e482010-07-14 04:20:34 +00001809 case Expr::CXXDefaultArgExprClass:
John McCall5e1e89b2010-08-18 19:18:59 +00001810 mangleExpression(cast<CXXDefaultArgExpr>(E)->getExpr(), Arity);
John McCall0512e482010-07-14 04:20:34 +00001811 break;
1812
1813 case Expr::CXXMemberCallExprClass: // fallthrough
John McCall1dd73832010-02-04 01:42:13 +00001814 case Expr::CallExprClass: {
1815 const CallExpr *CE = cast<CallExpr>(E);
1816 Out << "cl";
John McCall5e1e89b2010-08-18 19:18:59 +00001817 mangleExpression(CE->getCallee(), CE->getNumArgs());
John McCall1dd73832010-02-04 01:42:13 +00001818 for (unsigned I = 0, N = CE->getNumArgs(); I != N; ++I)
1819 mangleExpression(CE->getArg(I));
Benjamin Kramer35f59b62010-04-10 16:03:31 +00001820 Out << 'E';
John McCall09cc1412010-02-03 00:55:45 +00001821 break;
John McCall1dd73832010-02-04 01:42:13 +00001822 }
John McCall09cc1412010-02-03 00:55:45 +00001823
John McCall0512e482010-07-14 04:20:34 +00001824 case Expr::CXXNewExprClass: {
1825 // Proposal from David Vandervoorde, 2010.06.30
1826 const CXXNewExpr *New = cast<CXXNewExpr>(E);
1827 if (New->isGlobalNew()) Out << "gs";
1828 Out << (New->isArray() ? "na" : "nw");
1829 for (CXXNewExpr::const_arg_iterator I = New->placement_arg_begin(),
1830 E = New->placement_arg_end(); I != E; ++I)
1831 mangleExpression(*I);
1832 Out << '_';
1833 mangleType(New->getAllocatedType());
1834 if (New->hasInitializer()) {
1835 Out << "pi";
1836 for (CXXNewExpr::const_arg_iterator I = New->constructor_arg_begin(),
1837 E = New->constructor_arg_end(); I != E; ++I)
1838 mangleExpression(*I);
1839 }
1840 Out << 'E';
1841 break;
1842 }
1843
John McCall2f27bf82010-02-04 02:56:29 +00001844 case Expr::MemberExprClass: {
1845 const MemberExpr *ME = cast<MemberExpr>(E);
1846 mangleMemberExpr(ME->getBase(), ME->isArrow(),
1847 ME->getQualifier(), ME->getMemberDecl()->getDeclName(),
John McCall5e1e89b2010-08-18 19:18:59 +00001848 Arity);
John McCall2f27bf82010-02-04 02:56:29 +00001849 break;
1850 }
1851
1852 case Expr::UnresolvedMemberExprClass: {
1853 const UnresolvedMemberExpr *ME = cast<UnresolvedMemberExpr>(E);
1854 mangleMemberExpr(ME->getBase(), ME->isArrow(),
1855 ME->getQualifier(), ME->getMemberName(),
John McCall5e1e89b2010-08-18 19:18:59 +00001856 Arity);
John McCall6dbce192010-08-20 00:17:19 +00001857 if (ME->hasExplicitTemplateArgs())
1858 mangleTemplateArgs(ME->getExplicitTemplateArgs());
John McCall2f27bf82010-02-04 02:56:29 +00001859 break;
1860 }
1861
1862 case Expr::CXXDependentScopeMemberExprClass: {
1863 const CXXDependentScopeMemberExpr *ME
1864 = cast<CXXDependentScopeMemberExpr>(E);
1865 mangleMemberExpr(ME->getBase(), ME->isArrow(),
1866 ME->getQualifier(), ME->getMember(),
John McCall5e1e89b2010-08-18 19:18:59 +00001867 Arity);
John McCall6dbce192010-08-20 00:17:19 +00001868 if (ME->hasExplicitTemplateArgs())
1869 mangleTemplateArgs(ME->getExplicitTemplateArgs());
John McCall2f27bf82010-02-04 02:56:29 +00001870 break;
1871 }
1872
John McCall1dd73832010-02-04 01:42:13 +00001873 case Expr::UnresolvedLookupExprClass: {
John McCalla3218e72010-02-04 01:48:38 +00001874 // The ABI doesn't cover how to mangle overload sets, so we mangle
1875 // using something as close as possible to the original lookup
1876 // expression.
John McCall1dd73832010-02-04 01:42:13 +00001877 const UnresolvedLookupExpr *ULE = cast<UnresolvedLookupExpr>(E);
John McCall5e1e89b2010-08-18 19:18:59 +00001878 mangleUnresolvedName(ULE->getQualifier(), ULE->getName(), Arity);
John McCall6dbce192010-08-20 00:17:19 +00001879 if (ULE->hasExplicitTemplateArgs())
1880 mangleTemplateArgs(ULE->getExplicitTemplateArgs());
John McCall1dd73832010-02-04 01:42:13 +00001881 break;
1882 }
1883
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001884 case Expr::CXXUnresolvedConstructExprClass: {
John McCall1dd73832010-02-04 01:42:13 +00001885 const CXXUnresolvedConstructExpr *CE = cast<CXXUnresolvedConstructExpr>(E);
1886 unsigned N = CE->arg_size();
1887
1888 Out << "cv";
1889 mangleType(CE->getType());
Benjamin Kramer35f59b62010-04-10 16:03:31 +00001890 if (N != 1) Out << '_';
John McCall1dd73832010-02-04 01:42:13 +00001891 for (unsigned I = 0; I != N; ++I) mangleExpression(CE->getArg(I));
Benjamin Kramer35f59b62010-04-10 16:03:31 +00001892 if (N != 1) Out << 'E';
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001893 break;
John McCall1dd73832010-02-04 01:42:13 +00001894 }
John McCall09cc1412010-02-03 00:55:45 +00001895
John McCall1dd73832010-02-04 01:42:13 +00001896 case Expr::CXXTemporaryObjectExprClass:
1897 case Expr::CXXConstructExprClass: {
1898 const CXXConstructExpr *CE = cast<CXXConstructExpr>(E);
1899 unsigned N = CE->getNumArgs();
1900
1901 Out << "cv";
1902 mangleType(CE->getType());
Benjamin Kramer35f59b62010-04-10 16:03:31 +00001903 if (N != 1) Out << '_';
John McCall1dd73832010-02-04 01:42:13 +00001904 for (unsigned I = 0; I != N; ++I) mangleExpression(CE->getArg(I));
Benjamin Kramer35f59b62010-04-10 16:03:31 +00001905 if (N != 1) Out << 'E';
John McCall09cc1412010-02-03 00:55:45 +00001906 break;
John McCall1dd73832010-02-04 01:42:13 +00001907 }
1908
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +00001909 case Expr::UnaryExprOrTypeTraitExprClass: {
1910 const UnaryExprOrTypeTraitExpr *SAE = cast<UnaryExprOrTypeTraitExpr>(E);
1911 switch(SAE->getKind()) {
1912 case UETT_SizeOf:
1913 Out << 's';
1914 break;
1915 case UETT_AlignOf:
1916 Out << 'a';
1917 break;
1918 case UETT_VecStep:
1919 Diagnostic &Diags = Context.getDiags();
1920 unsigned DiagID = Diags.getCustomDiagID(Diagnostic::Error,
1921 "cannot yet mangle vec_step expression");
1922 Diags.Report(DiagID);
1923 return;
1924 }
John McCall1dd73832010-02-04 01:42:13 +00001925 if (SAE->isArgumentType()) {
Benjamin Kramer35f59b62010-04-10 16:03:31 +00001926 Out << 't';
John McCall1dd73832010-02-04 01:42:13 +00001927 mangleType(SAE->getArgumentType());
1928 } else {
Benjamin Kramer35f59b62010-04-10 16:03:31 +00001929 Out << 'z';
John McCall1dd73832010-02-04 01:42:13 +00001930 mangleExpression(SAE->getArgumentExpr());
1931 }
1932 break;
1933 }
Anders Carlssona7694082009-11-06 02:50:19 +00001934
John McCall0512e482010-07-14 04:20:34 +00001935 case Expr::CXXThrowExprClass: {
1936 const CXXThrowExpr *TE = cast<CXXThrowExpr>(E);
1937
1938 // Proposal from David Vandervoorde, 2010.06.30
1939 if (TE->getSubExpr()) {
1940 Out << "tw";
1941 mangleExpression(TE->getSubExpr());
1942 } else {
1943 Out << "tr";
1944 }
1945 break;
1946 }
1947
1948 case Expr::CXXTypeidExprClass: {
1949 const CXXTypeidExpr *TIE = cast<CXXTypeidExpr>(E);
1950
1951 // Proposal from David Vandervoorde, 2010.06.30
1952 if (TIE->isTypeOperand()) {
1953 Out << "ti";
1954 mangleType(TIE->getTypeOperand());
1955 } else {
1956 Out << "te";
1957 mangleExpression(TIE->getExprOperand());
1958 }
1959 break;
1960 }
1961
1962 case Expr::CXXDeleteExprClass: {
1963 const CXXDeleteExpr *DE = cast<CXXDeleteExpr>(E);
1964
1965 // Proposal from David Vandervoorde, 2010.06.30
1966 if (DE->isGlobalDelete()) Out << "gs";
1967 Out << (DE->isArrayForm() ? "da" : "dl");
1968 mangleExpression(DE->getArgument());
1969 break;
1970 }
1971
Anders Carlssone170ba72009-12-14 01:45:37 +00001972 case Expr::UnaryOperatorClass: {
1973 const UnaryOperator *UO = cast<UnaryOperator>(E);
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001974 mangleOperatorName(UnaryOperator::getOverloadedOperator(UO->getOpcode()),
Anders Carlssone170ba72009-12-14 01:45:37 +00001975 /*Arity=*/1);
1976 mangleExpression(UO->getSubExpr());
1977 break;
1978 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001979
John McCall0512e482010-07-14 04:20:34 +00001980 case Expr::ArraySubscriptExprClass: {
1981 const ArraySubscriptExpr *AE = cast<ArraySubscriptExpr>(E);
1982
Chris Lattnerfc8f0e12011-04-15 05:22:18 +00001983 // Array subscript is treated as a syntactically weird form of
John McCall0512e482010-07-14 04:20:34 +00001984 // binary operator.
1985 Out << "ix";
1986 mangleExpression(AE->getLHS());
1987 mangleExpression(AE->getRHS());
1988 break;
1989 }
1990
1991 case Expr::CompoundAssignOperatorClass: // fallthrough
Anders Carlssone170ba72009-12-14 01:45:37 +00001992 case Expr::BinaryOperatorClass: {
1993 const BinaryOperator *BO = cast<BinaryOperator>(E);
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001994 mangleOperatorName(BinaryOperator::getOverloadedOperator(BO->getOpcode()),
Anders Carlssone170ba72009-12-14 01:45:37 +00001995 /*Arity=*/2);
1996 mangleExpression(BO->getLHS());
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001997 mangleExpression(BO->getRHS());
Anders Carlssone170ba72009-12-14 01:45:37 +00001998 break;
John McCall2f27bf82010-02-04 02:56:29 +00001999 }
Anders Carlssone170ba72009-12-14 01:45:37 +00002000
2001 case Expr::ConditionalOperatorClass: {
2002 const ConditionalOperator *CO = cast<ConditionalOperator>(E);
2003 mangleOperatorName(OO_Conditional, /*Arity=*/3);
2004 mangleExpression(CO->getCond());
John McCall5e1e89b2010-08-18 19:18:59 +00002005 mangleExpression(CO->getLHS(), Arity);
2006 mangleExpression(CO->getRHS(), Arity);
Anders Carlssone170ba72009-12-14 01:45:37 +00002007 break;
2008 }
2009
Douglas Gregor46287c72010-01-29 16:37:09 +00002010 case Expr::ImplicitCastExprClass: {
John McCall5e1e89b2010-08-18 19:18:59 +00002011 mangleExpression(cast<ImplicitCastExpr>(E)->getSubExpr(), Arity);
Douglas Gregor46287c72010-01-29 16:37:09 +00002012 break;
2013 }
2014
2015 case Expr::CStyleCastExprClass:
2016 case Expr::CXXStaticCastExprClass:
2017 case Expr::CXXDynamicCastExprClass:
2018 case Expr::CXXReinterpretCastExprClass:
2019 case Expr::CXXConstCastExprClass:
2020 case Expr::CXXFunctionalCastExprClass: {
2021 const ExplicitCastExpr *ECE = cast<ExplicitCastExpr>(E);
2022 Out << "cv";
2023 mangleType(ECE->getType());
2024 mangleExpression(ECE->getSubExpr());
2025 break;
2026 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002027
Anders Carlsson58040a52009-12-16 05:48:46 +00002028 case Expr::CXXOperatorCallExprClass: {
2029 const CXXOperatorCallExpr *CE = cast<CXXOperatorCallExpr>(E);
2030 unsigned NumArgs = CE->getNumArgs();
2031 mangleOperatorName(CE->getOperator(), /*Arity=*/NumArgs);
2032 // Mangle the arguments.
2033 for (unsigned i = 0; i != NumArgs; ++i)
2034 mangleExpression(CE->getArg(i));
2035 break;
2036 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002037
Anders Carlssona7694082009-11-06 02:50:19 +00002038 case Expr::ParenExprClass:
John McCall5e1e89b2010-08-18 19:18:59 +00002039 mangleExpression(cast<ParenExpr>(E)->getSubExpr(), Arity);
Anders Carlssona7694082009-11-06 02:50:19 +00002040 break;
2041
Anders Carlssond553f8c2009-09-21 01:21:10 +00002042 case Expr::DeclRefExprClass: {
Douglas Gregor5ed1bc32010-02-28 21:40:32 +00002043 const NamedDecl *D = cast<DeclRefExpr>(E)->getDecl();
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002044
Anders Carlssond553f8c2009-09-21 01:21:10 +00002045 switch (D->getKind()) {
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002046 default:
Douglas Gregor5ed1bc32010-02-28 21:40:32 +00002047 // <expr-primary> ::= L <mangled-name> E # external name
2048 Out << 'L';
2049 mangle(D, "_Z");
2050 Out << 'E';
2051 break;
2052
John McCall3dc7e7b2010-07-24 01:17:35 +00002053 case Decl::EnumConstant: {
2054 const EnumConstantDecl *ED = cast<EnumConstantDecl>(D);
2055 mangleIntegerLiteral(ED->getType(), ED->getInitVal());
2056 break;
2057 }
2058
Anders Carlssond553f8c2009-09-21 01:21:10 +00002059 case Decl::NonTypeTemplateParm: {
2060 const NonTypeTemplateParmDecl *PD = cast<NonTypeTemplateParmDecl>(D);
Anders Carlsson0ccdf8d2009-09-27 00:38:53 +00002061 mangleTemplateParameter(PD->getIndex());
Anders Carlssond553f8c2009-09-21 01:21:10 +00002062 break;
2063 }
2064
2065 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002066
Anders Carlsson50755b02009-09-27 20:11:34 +00002067 break;
Anders Carlssond553f8c2009-09-21 01:21:10 +00002068 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002069
Douglas Gregorc7793c72011-01-15 01:15:58 +00002070 case Expr::SubstNonTypeTemplateParmPackExprClass:
2071 mangleTemplateParameter(
2072 cast<SubstNonTypeTemplateParmPackExpr>(E)->getParameterPack()->getIndex());
2073 break;
2074
John McCall865d4472009-11-19 22:55:06 +00002075 case Expr::DependentScopeDeclRefExprClass: {
2076 const DependentScopeDeclRefExpr *DRE = cast<DependentScopeDeclRefExpr>(E);
Douglas Gregor4b2ccfc2010-02-28 22:05:49 +00002077 NestedNameSpecifier *NNS = DRE->getQualifier();
2078 const Type *QTy = NNS->getAsType();
2079
2080 // When we're dealing with a nested-name-specifier that has just a
2081 // dependent identifier in it, mangle that as a typename. FIXME:
2082 // It isn't clear that we ever actually want to have such a
2083 // nested-name-specifier; why not just represent it as a typename type?
2084 if (!QTy && NNS->getAsIdentifier() && NNS->getPrefix()) {
Douglas Gregor4a2023f2010-03-31 20:19:30 +00002085 QTy = getASTContext().getDependentNameType(ETK_Typename,
2086 NNS->getPrefix(),
2087 NNS->getAsIdentifier())
Douglas Gregor4b2ccfc2010-02-28 22:05:49 +00002088 .getTypePtr();
2089 }
Anders Carlsson50755b02009-09-27 20:11:34 +00002090 assert(QTy && "Qualifier was not type!");
2091
John McCall6dbce192010-08-20 00:17:19 +00002092 // ::= sr <type> <unqualified-name> # dependent name
2093 // ::= sr <type> <unqualified-name> <template-args> # dependent template-id
Anders Carlsson50755b02009-09-27 20:11:34 +00002094 Out << "sr";
2095 mangleType(QualType(QTy, 0));
John McCall5e1e89b2010-08-18 19:18:59 +00002096 mangleUnqualifiedName(0, DRE->getDeclName(), Arity);
John McCall6dbce192010-08-20 00:17:19 +00002097 if (DRE->hasExplicitTemplateArgs())
2098 mangleTemplateArgs(DRE->getExplicitTemplateArgs());
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002099
Anders Carlsson50755b02009-09-27 20:11:34 +00002100 break;
2101 }
2102
John McCalld9307602010-04-09 22:54:09 +00002103 case Expr::CXXBindTemporaryExprClass:
2104 mangleExpression(cast<CXXBindTemporaryExpr>(E)->getSubExpr());
2105 break;
2106
John McCall4765fa02010-12-06 08:20:24 +00002107 case Expr::ExprWithCleanupsClass:
2108 mangleExpression(cast<ExprWithCleanups>(E)->getSubExpr(), Arity);
John McCalld9307602010-04-09 22:54:09 +00002109 break;
2110
John McCall1dd73832010-02-04 01:42:13 +00002111 case Expr::FloatingLiteralClass: {
2112 const FloatingLiteral *FL = cast<FloatingLiteral>(E);
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002113 Out << 'L';
John McCall1dd73832010-02-04 01:42:13 +00002114 mangleType(FL->getType());
John McCall0512e482010-07-14 04:20:34 +00002115 mangleFloat(FL->getValue());
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002116 Out << 'E';
John McCall1dd73832010-02-04 01:42:13 +00002117 break;
2118 }
2119
John McCallde810632010-04-09 21:48:08 +00002120 case Expr::CharacterLiteralClass:
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002121 Out << 'L';
John McCallde810632010-04-09 21:48:08 +00002122 mangleType(E->getType());
2123 Out << cast<CharacterLiteral>(E)->getValue();
2124 Out << 'E';
2125 break;
2126
2127 case Expr::CXXBoolLiteralExprClass:
2128 Out << "Lb";
2129 Out << (cast<CXXBoolLiteralExpr>(E)->getValue() ? '1' : '0');
2130 Out << 'E';
2131 break;
2132
John McCall0512e482010-07-14 04:20:34 +00002133 case Expr::IntegerLiteralClass: {
2134 llvm::APSInt Value(cast<IntegerLiteral>(E)->getValue());
2135 if (E->getType()->isSignedIntegerType())
2136 Value.setIsSigned(true);
2137 mangleIntegerLiteral(E->getType(), Value);
Anders Carlssone170ba72009-12-14 01:45:37 +00002138 break;
John McCall0512e482010-07-14 04:20:34 +00002139 }
2140
2141 case Expr::ImaginaryLiteralClass: {
2142 const ImaginaryLiteral *IE = cast<ImaginaryLiteral>(E);
2143 // Mangle as if a complex literal.
Nick Lewycky271b6652010-09-05 03:40:33 +00002144 // Proposal from David Vandevoorde, 2010.06.30.
John McCall0512e482010-07-14 04:20:34 +00002145 Out << 'L';
2146 mangleType(E->getType());
2147 if (const FloatingLiteral *Imag =
2148 dyn_cast<FloatingLiteral>(IE->getSubExpr())) {
2149 // Mangle a floating-point zero of the appropriate type.
2150 mangleFloat(llvm::APFloat(Imag->getValue().getSemantics()));
2151 Out << '_';
2152 mangleFloat(Imag->getValue());
2153 } else {
Nick Lewycky271b6652010-09-05 03:40:33 +00002154 Out << "0_";
John McCall0512e482010-07-14 04:20:34 +00002155 llvm::APSInt Value(cast<IntegerLiteral>(IE->getSubExpr())->getValue());
2156 if (IE->getSubExpr()->getType()->isSignedIntegerType())
2157 Value.setIsSigned(true);
2158 mangleNumber(Value);
2159 }
2160 Out << 'E';
2161 break;
2162 }
2163
2164 case Expr::StringLiteralClass: {
John McCall1658c392010-07-15 21:53:03 +00002165 // Revised proposal from David Vandervoorde, 2010.07.15.
John McCall0512e482010-07-14 04:20:34 +00002166 Out << 'L';
John McCall1658c392010-07-15 21:53:03 +00002167 assert(isa<ConstantArrayType>(E->getType()));
2168 mangleType(E->getType());
John McCall0512e482010-07-14 04:20:34 +00002169 Out << 'E';
2170 break;
2171 }
2172
2173 case Expr::GNUNullExprClass:
2174 // FIXME: should this really be mangled the same as nullptr?
2175 // fallthrough
2176
2177 case Expr::CXXNullPtrLiteralExprClass: {
2178 // Proposal from David Vandervoorde, 2010.06.30, as
2179 // modified by ABI list discussion.
2180 Out << "LDnE";
2181 break;
2182 }
Douglas Gregorbe230c32011-01-03 17:17:50 +00002183
2184 case Expr::PackExpansionExprClass:
2185 Out << "sp";
2186 mangleExpression(cast<PackExpansionExpr>(E)->getPattern());
2187 break;
Douglas Gregor2e774c42011-01-04 18:56:13 +00002188
2189 case Expr::SizeOfPackExprClass: {
Douglas Gregor2e774c42011-01-04 18:56:13 +00002190 Out << "sZ";
2191 const NamedDecl *Pack = cast<SizeOfPackExpr>(E)->getPack();
2192 if (const TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(Pack))
2193 mangleTemplateParameter(TTP->getIndex());
2194 else if (const NonTypeTemplateParmDecl *NTTP
2195 = dyn_cast<NonTypeTemplateParmDecl>(Pack))
2196 mangleTemplateParameter(NTTP->getIndex());
2197 else if (const TemplateTemplateParmDecl *TempTP
2198 = dyn_cast<TemplateTemplateParmDecl>(Pack))
2199 mangleTemplateParameter(TempTP->getIndex());
2200 else {
Douglas Gregor4fc48662011-01-13 16:39:34 +00002201 // Note: proposed by Mike Herrick on 11/30/10
2202 // <expression> ::= sZ <function-param> # size of function parameter pack
Douglas Gregor2e774c42011-01-04 18:56:13 +00002203 Diagnostic &Diags = Context.getDiags();
2204 unsigned DiagID = Diags.getCustomDiagID(Diagnostic::Error,
2205 "cannot mangle sizeof...(function parameter pack)");
2206 Diags.Report(DiagID);
2207 return;
2208 }
Douglas Gregordfbbcf92011-03-03 02:20:19 +00002209 break;
Douglas Gregor2e774c42011-01-04 18:56:13 +00002210 }
Anders Carlssond553f8c2009-09-21 01:21:10 +00002211 }
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00002212}
2213
Anders Carlsson3ac86b52009-04-15 05:36:58 +00002214void CXXNameMangler::mangleCXXCtorType(CXXCtorType T) {
2215 // <ctor-dtor-name> ::= C1 # complete object constructor
2216 // ::= C2 # base object constructor
2217 // ::= C3 # complete object allocating constructor
2218 //
2219 switch (T) {
2220 case Ctor_Complete:
2221 Out << "C1";
2222 break;
2223 case Ctor_Base:
2224 Out << "C2";
2225 break;
2226 case Ctor_CompleteAllocating:
2227 Out << "C3";
2228 break;
2229 }
2230}
2231
Anders Carlsson27ae5362009-04-17 01:58:57 +00002232void CXXNameMangler::mangleCXXDtorType(CXXDtorType T) {
2233 // <ctor-dtor-name> ::= D0 # deleting destructor
2234 // ::= D1 # complete object destructor
2235 // ::= D2 # base object destructor
2236 //
2237 switch (T) {
2238 case Dtor_Deleting:
2239 Out << "D0";
2240 break;
2241 case Dtor_Complete:
2242 Out << "D1";
2243 break;
2244 case Dtor_Base:
2245 Out << "D2";
2246 break;
2247 }
2248}
2249
John McCall6dbce192010-08-20 00:17:19 +00002250void CXXNameMangler::mangleTemplateArgs(
2251 const ExplicitTemplateArgumentList &TemplateArgs) {
2252 // <template-args> ::= I <template-arg>+ E
2253 Out << 'I';
2254 for (unsigned I = 0, E = TemplateArgs.NumTemplateArgs; I != E; ++I)
2255 mangleTemplateArg(0, TemplateArgs.getTemplateArgs()[I].getArgument());
2256 Out << 'E';
2257}
2258
Douglas Gregor20f0cc72010-04-23 03:10:43 +00002259void CXXNameMangler::mangleTemplateArgs(TemplateName Template,
2260 const TemplateArgument *TemplateArgs,
2261 unsigned NumTemplateArgs) {
2262 if (TemplateDecl *TD = Template.getAsTemplateDecl())
2263 return mangleTemplateArgs(*TD->getTemplateParameters(), TemplateArgs,
2264 NumTemplateArgs);
Sean Huntc3021132010-05-05 15:23:54 +00002265
Douglas Gregor20f0cc72010-04-23 03:10:43 +00002266 // <template-args> ::= I <template-arg>+ E
2267 Out << 'I';
2268 for (unsigned i = 0; i != NumTemplateArgs; ++i)
2269 mangleTemplateArg(0, TemplateArgs[i]);
2270 Out << 'E';
2271}
2272
Rafael Espindolad9800722010-03-11 14:07:00 +00002273void CXXNameMangler::mangleTemplateArgs(const TemplateParameterList &PL,
2274 const TemplateArgumentList &AL) {
Anders Carlsson7a0ba872009-05-15 16:09:15 +00002275 // <template-args> ::= I <template-arg>+ E
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002276 Out << 'I';
Rafael Espindolad9800722010-03-11 14:07:00 +00002277 for (unsigned i = 0, e = AL.size(); i != e; ++i)
2278 mangleTemplateArg(PL.getParam(i), AL[i]);
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002279 Out << 'E';
Anders Carlsson7a0ba872009-05-15 16:09:15 +00002280}
2281
Rafael Espindolad9800722010-03-11 14:07:00 +00002282void CXXNameMangler::mangleTemplateArgs(const TemplateParameterList &PL,
2283 const TemplateArgument *TemplateArgs,
Anders Carlsson7624f212009-09-18 02:42:01 +00002284 unsigned NumTemplateArgs) {
2285 // <template-args> ::= I <template-arg>+ E
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002286 Out << 'I';
Daniel Dunbar7e0c1952009-11-21 09:17:15 +00002287 for (unsigned i = 0; i != NumTemplateArgs; ++i)
Rafael Espindolad9800722010-03-11 14:07:00 +00002288 mangleTemplateArg(PL.getParam(i), TemplateArgs[i]);
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002289 Out << 'E';
Anders Carlsson7624f212009-09-18 02:42:01 +00002290}
2291
Rafael Espindolad9800722010-03-11 14:07:00 +00002292void CXXNameMangler::mangleTemplateArg(const NamedDecl *P,
2293 const TemplateArgument &A) {
Mike Stump1eb44332009-09-09 15:08:12 +00002294 // <template-arg> ::= <type> # type or template
Anders Carlsson7a0ba872009-05-15 16:09:15 +00002295 // ::= X <expression> E # expression
2296 // ::= <expr-primary> # simple expressions
Douglas Gregor4fc48662011-01-13 16:39:34 +00002297 // ::= J <template-arg>* E # argument pack
Anders Carlsson7a0ba872009-05-15 16:09:15 +00002298 // ::= sp <expression> # pack expansion of (C++0x)
2299 switch (A.getKind()) {
Douglas Gregorf90b27a2011-01-03 22:36:02 +00002300 case TemplateArgument::Null:
2301 llvm_unreachable("Cannot mangle NULL template argument");
2302
Anders Carlsson7a0ba872009-05-15 16:09:15 +00002303 case TemplateArgument::Type:
2304 mangleType(A.getAsType());
2305 break;
Anders Carlsson9e85c742009-12-23 19:30:55 +00002306 case TemplateArgument::Template:
John McCallb6f532e2010-07-14 06:43:17 +00002307 // This is mangled as <type>.
2308 mangleType(A.getAsTemplate());
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002309 break;
Douglas Gregora7fc9012011-01-05 18:58:31 +00002310 case TemplateArgument::TemplateExpansion:
Douglas Gregor4fc48662011-01-13 16:39:34 +00002311 // <type> ::= Dp <type> # pack expansion (C++0x)
Douglas Gregora7fc9012011-01-05 18:58:31 +00002312 Out << "Dp";
2313 mangleType(A.getAsTemplateOrTemplatePattern());
2314 break;
Anders Carlssond553f8c2009-09-21 01:21:10 +00002315 case TemplateArgument::Expression:
2316 Out << 'X';
2317 mangleExpression(A.getAsExpr());
2318 Out << 'E';
2319 break;
Anders Carlssone170ba72009-12-14 01:45:37 +00002320 case TemplateArgument::Integral:
2321 mangleIntegerLiteral(A.getIntegralType(), *A.getAsIntegral());
Anders Carlsson7a0ba872009-05-15 16:09:15 +00002322 break;
Daniel Dunbar7e0c1952009-11-21 09:17:15 +00002323 case TemplateArgument::Declaration: {
Douglas Gregor20f0cc72010-04-23 03:10:43 +00002324 assert(P && "Missing template parameter for declaration argument");
Daniel Dunbar7e0c1952009-11-21 09:17:15 +00002325 // <expr-primary> ::= L <mangled-name> E # external name
2326
Rafael Espindolad9800722010-03-11 14:07:00 +00002327 // Clang produces AST's where pointer-to-member-function expressions
Daniel Dunbar7e0c1952009-11-21 09:17:15 +00002328 // and pointer-to-function expressions are represented as a declaration not
Rafael Espindolad9800722010-03-11 14:07:00 +00002329 // an expression. We compensate for it here to produce the correct mangling.
2330 NamedDecl *D = cast<NamedDecl>(A.getAsDecl());
2331 const NonTypeTemplateParmDecl *Parameter = cast<NonTypeTemplateParmDecl>(P);
2332 bool compensateMangling = D->isCXXClassMember() &&
2333 !Parameter->getType()->isReferenceType();
2334 if (compensateMangling) {
2335 Out << 'X';
2336 mangleOperatorName(OO_Amp, 1);
2337 }
2338
Daniel Dunbar7e0c1952009-11-21 09:17:15 +00002339 Out << 'L';
2340 // References to external entities use the mangled name; if the name would
2341 // not normally be manged then mangle it as unqualified.
2342 //
2343 // FIXME: The ABI specifies that external names here should have _Z, but
2344 // gcc leaves this off.
Rafael Espindolad9800722010-03-11 14:07:00 +00002345 if (compensateMangling)
2346 mangle(D, "_Z");
2347 else
2348 mangle(D, "Z");
Daniel Dunbar7e0c1952009-11-21 09:17:15 +00002349 Out << 'E';
Rafael Espindolad9800722010-03-11 14:07:00 +00002350
2351 if (compensateMangling)
2352 Out << 'E';
2353
Daniel Dunbar7e0c1952009-11-21 09:17:15 +00002354 break;
2355 }
Douglas Gregorf90b27a2011-01-03 22:36:02 +00002356
2357 case TemplateArgument::Pack: {
2358 // Note: proposal by Mike Herrick on 12/20/10
2359 Out << 'J';
2360 for (TemplateArgument::pack_iterator PA = A.pack_begin(),
2361 PAEnd = A.pack_end();
2362 PA != PAEnd; ++PA)
2363 mangleTemplateArg(P, *PA);
2364 Out << 'E';
2365 }
Daniel Dunbar7e0c1952009-11-21 09:17:15 +00002366 }
Anders Carlsson7a0ba872009-05-15 16:09:15 +00002367}
2368
Anders Carlsson0ccdf8d2009-09-27 00:38:53 +00002369void CXXNameMangler::mangleTemplateParameter(unsigned Index) {
2370 // <template-param> ::= T_ # first template parameter
2371 // ::= T <parameter-2 non-negative number> _
2372 if (Index == 0)
2373 Out << "T_";
2374 else
2375 Out << 'T' << (Index - 1) << '_';
2376}
2377
Anders Carlsson76967372009-09-17 00:43:46 +00002378// <substitution> ::= S <seq-id> _
2379// ::= S_
Anders Carlsson6862fc72009-09-17 04:16:28 +00002380bool CXXNameMangler::mangleSubstitution(const NamedDecl *ND) {
Anders Carlssone7c8cb62009-09-26 20:53:44 +00002381 // Try one of the standard substitutions first.
2382 if (mangleStandardSubstitution(ND))
2383 return true;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002384
Anders Carlsson433d1372009-11-07 04:26:04 +00002385 ND = cast<NamedDecl>(ND->getCanonicalDecl());
Anders Carlsson6862fc72009-09-17 04:16:28 +00002386 return mangleSubstitution(reinterpret_cast<uintptr_t>(ND));
2387}
2388
Anders Carlsson76967372009-09-17 00:43:46 +00002389bool CXXNameMangler::mangleSubstitution(QualType T) {
Anders Carlssond99edc42009-09-26 03:55:37 +00002390 if (!T.getCVRQualifiers()) {
2391 if (const RecordType *RT = T->getAs<RecordType>())
2392 return mangleSubstitution(RT->getDecl());
2393 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002394
Anders Carlsson76967372009-09-17 00:43:46 +00002395 uintptr_t TypePtr = reinterpret_cast<uintptr_t>(T.getAsOpaquePtr());
2396
Anders Carlssond3a932a2009-09-17 03:53:28 +00002397 return mangleSubstitution(TypePtr);
2398}
2399
Douglas Gregor1e9268e2010-04-28 05:58:56 +00002400bool CXXNameMangler::mangleSubstitution(TemplateName Template) {
2401 if (TemplateDecl *TD = Template.getAsTemplateDecl())
2402 return mangleSubstitution(TD);
Sean Huntc3021132010-05-05 15:23:54 +00002403
Douglas Gregor1e9268e2010-04-28 05:58:56 +00002404 Template = Context.getASTContext().getCanonicalTemplateName(Template);
2405 return mangleSubstitution(
2406 reinterpret_cast<uintptr_t>(Template.getAsVoidPointer()));
2407}
2408
Anders Carlssond3a932a2009-09-17 03:53:28 +00002409bool CXXNameMangler::mangleSubstitution(uintptr_t Ptr) {
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002410 llvm::DenseMap<uintptr_t, unsigned>::iterator I = Substitutions.find(Ptr);
Anders Carlsson76967372009-09-17 00:43:46 +00002411 if (I == Substitutions.end())
2412 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002413
Anders Carlsson76967372009-09-17 00:43:46 +00002414 unsigned SeqID = I->second;
2415 if (SeqID == 0)
2416 Out << "S_";
2417 else {
2418 SeqID--;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002419
Anders Carlsson76967372009-09-17 00:43:46 +00002420 // <seq-id> is encoded in base-36, using digits and upper case letters.
2421 char Buffer[10];
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002422 char *BufferPtr = llvm::array_endof(Buffer);
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002423
Anders Carlsson76967372009-09-17 00:43:46 +00002424 if (SeqID == 0) *--BufferPtr = '0';
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002425
Anders Carlsson76967372009-09-17 00:43:46 +00002426 while (SeqID) {
2427 assert(BufferPtr > Buffer && "Buffer overflow!");
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002428
John McCall6ab30e02010-06-09 07:26:17 +00002429 char c = static_cast<char>(SeqID % 36);
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002430
Anders Carlsson76967372009-09-17 00:43:46 +00002431 *--BufferPtr = (c < 10 ? '0' + c : 'A' + c - 10);
2432 SeqID /= 36;
2433 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002434
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002435 Out << 'S'
2436 << llvm::StringRef(BufferPtr, llvm::array_endof(Buffer)-BufferPtr)
2437 << '_';
Anders Carlsson76967372009-09-17 00:43:46 +00002438 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002439
Anders Carlsson76967372009-09-17 00:43:46 +00002440 return true;
2441}
2442
Anders Carlssonf514b542009-09-27 00:12:57 +00002443static bool isCharType(QualType T) {
2444 if (T.isNull())
2445 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002446
Anders Carlssonf514b542009-09-27 00:12:57 +00002447 return T->isSpecificBuiltinType(BuiltinType::Char_S) ||
2448 T->isSpecificBuiltinType(BuiltinType::Char_U);
2449}
2450
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002451/// isCharSpecialization - Returns whether a given type is a template
Anders Carlssonf514b542009-09-27 00:12:57 +00002452/// specialization of a given name with a single argument of type char.
2453static bool isCharSpecialization(QualType T, const char *Name) {
2454 if (T.isNull())
2455 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002456
Anders Carlssonf514b542009-09-27 00:12:57 +00002457 const RecordType *RT = T->getAs<RecordType>();
2458 if (!RT)
2459 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002460
2461 const ClassTemplateSpecializationDecl *SD =
Anders Carlssonf514b542009-09-27 00:12:57 +00002462 dyn_cast<ClassTemplateSpecializationDecl>(RT->getDecl());
2463 if (!SD)
2464 return false;
2465
2466 if (!isStdNamespace(SD->getDeclContext()))
2467 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002468
Anders Carlssonf514b542009-09-27 00:12:57 +00002469 const TemplateArgumentList &TemplateArgs = SD->getTemplateArgs();
2470 if (TemplateArgs.size() != 1)
2471 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002472
Anders Carlssonf514b542009-09-27 00:12:57 +00002473 if (!isCharType(TemplateArgs[0].getAsType()))
2474 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002475
Daniel Dunbar01eb9b92009-10-18 21:17:35 +00002476 return SD->getIdentifier()->getName() == Name;
Anders Carlssonf514b542009-09-27 00:12:57 +00002477}
2478
Anders Carlsson91f88602009-12-07 19:56:42 +00002479template <std::size_t StrLen>
Benjamin Kramer54353f42010-11-25 18:29:30 +00002480static bool isStreamCharSpecialization(const ClassTemplateSpecializationDecl*SD,
2481 const char (&Str)[StrLen]) {
Anders Carlsson91f88602009-12-07 19:56:42 +00002482 if (!SD->getIdentifier()->isStr(Str))
2483 return false;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002484
Anders Carlsson91f88602009-12-07 19:56:42 +00002485 const TemplateArgumentList &TemplateArgs = SD->getTemplateArgs();
2486 if (TemplateArgs.size() != 2)
2487 return false;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002488
Anders Carlsson91f88602009-12-07 19:56:42 +00002489 if (!isCharType(TemplateArgs[0].getAsType()))
2490 return false;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002491
Anders Carlsson91f88602009-12-07 19:56:42 +00002492 if (!isCharSpecialization(TemplateArgs[1].getAsType(), "char_traits"))
2493 return false;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002494
Anders Carlsson91f88602009-12-07 19:56:42 +00002495 return true;
2496}
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002497
Anders Carlssone7c8cb62009-09-26 20:53:44 +00002498bool CXXNameMangler::mangleStandardSubstitution(const NamedDecl *ND) {
2499 // <substitution> ::= St # ::std::
Anders Carlsson8c031552009-09-26 23:10:05 +00002500 if (const NamespaceDecl *NS = dyn_cast<NamespaceDecl>(ND)) {
Anders Carlsson47846d22009-12-04 06:23:23 +00002501 if (isStd(NS)) {
Anders Carlsson8c031552009-09-26 23:10:05 +00002502 Out << "St";
2503 return true;
2504 }
2505 }
2506
2507 if (const ClassTemplateDecl *TD = dyn_cast<ClassTemplateDecl>(ND)) {
2508 if (!isStdNamespace(TD->getDeclContext()))
2509 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002510
Anders Carlsson8c031552009-09-26 23:10:05 +00002511 // <substitution> ::= Sa # ::std::allocator
2512 if (TD->getIdentifier()->isStr("allocator")) {
2513 Out << "Sa";
2514 return true;
2515 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002516
Anders Carlsson189d59c2009-09-26 23:14:39 +00002517 // <<substitution> ::= Sb # ::std::basic_string
2518 if (TD->getIdentifier()->isStr("basic_string")) {
2519 Out << "Sb";
2520 return true;
2521 }
Anders Carlsson8c031552009-09-26 23:10:05 +00002522 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002523
2524 if (const ClassTemplateSpecializationDecl *SD =
Anders Carlssonf514b542009-09-27 00:12:57 +00002525 dyn_cast<ClassTemplateSpecializationDecl>(ND)) {
Eli Friedman5370ee22010-02-23 18:25:09 +00002526 if (!isStdNamespace(SD->getDeclContext()))
2527 return false;
2528
Anders Carlssonf514b542009-09-27 00:12:57 +00002529 // <substitution> ::= Ss # ::std::basic_string<char,
2530 // ::std::char_traits<char>,
2531 // ::std::allocator<char> >
2532 if (SD->getIdentifier()->isStr("basic_string")) {
2533 const TemplateArgumentList &TemplateArgs = SD->getTemplateArgs();
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002534
Anders Carlssonf514b542009-09-27 00:12:57 +00002535 if (TemplateArgs.size() != 3)
2536 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002537
Anders Carlssonf514b542009-09-27 00:12:57 +00002538 if (!isCharType(TemplateArgs[0].getAsType()))
2539 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002540
Anders Carlssonf514b542009-09-27 00:12:57 +00002541 if (!isCharSpecialization(TemplateArgs[1].getAsType(), "char_traits"))
2542 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002543
Anders Carlssonf514b542009-09-27 00:12:57 +00002544 if (!isCharSpecialization(TemplateArgs[2].getAsType(), "allocator"))
2545 return false;
2546
2547 Out << "Ss";
2548 return true;
2549 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002550
Anders Carlsson91f88602009-12-07 19:56:42 +00002551 // <substitution> ::= Si # ::std::basic_istream<char,
2552 // ::std::char_traits<char> >
2553 if (isStreamCharSpecialization(SD, "basic_istream")) {
2554 Out << "Si";
2555 return true;
2556 }
2557
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002558 // <substitution> ::= So # ::std::basic_ostream<char,
Anders Carlsson8f8fd8e2009-10-08 17:20:26 +00002559 // ::std::char_traits<char> >
Anders Carlsson91f88602009-12-07 19:56:42 +00002560 if (isStreamCharSpecialization(SD, "basic_ostream")) {
Anders Carlsson8f8fd8e2009-10-08 17:20:26 +00002561 Out << "So";
2562 return true;
2563 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002564
Anders Carlsson91f88602009-12-07 19:56:42 +00002565 // <substitution> ::= Sd # ::std::basic_iostream<char,
2566 // ::std::char_traits<char> >
2567 if (isStreamCharSpecialization(SD, "basic_iostream")) {
2568 Out << "Sd";
2569 return true;
2570 }
Anders Carlssonf514b542009-09-27 00:12:57 +00002571 }
Anders Carlsson8c031552009-09-26 23:10:05 +00002572 return false;
Anders Carlssone7c8cb62009-09-26 20:53:44 +00002573}
2574
Anders Carlsson76967372009-09-17 00:43:46 +00002575void CXXNameMangler::addSubstitution(QualType T) {
Anders Carlssond99edc42009-09-26 03:55:37 +00002576 if (!T.getCVRQualifiers()) {
2577 if (const RecordType *RT = T->getAs<RecordType>()) {
2578 addSubstitution(RT->getDecl());
2579 return;
2580 }
2581 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002582
Anders Carlsson76967372009-09-17 00:43:46 +00002583 uintptr_t TypePtr = reinterpret_cast<uintptr_t>(T.getAsOpaquePtr());
Anders Carlssond3a932a2009-09-17 03:53:28 +00002584 addSubstitution(TypePtr);
2585}
2586
Douglas Gregor1e9268e2010-04-28 05:58:56 +00002587void CXXNameMangler::addSubstitution(TemplateName Template) {
2588 if (TemplateDecl *TD = Template.getAsTemplateDecl())
2589 return addSubstitution(TD);
Sean Huntc3021132010-05-05 15:23:54 +00002590
Douglas Gregor1e9268e2010-04-28 05:58:56 +00002591 Template = Context.getASTContext().getCanonicalTemplateName(Template);
2592 addSubstitution(reinterpret_cast<uintptr_t>(Template.getAsVoidPointer()));
2593}
2594
Anders Carlssond3a932a2009-09-17 03:53:28 +00002595void CXXNameMangler::addSubstitution(uintptr_t Ptr) {
Anders Carlssond3a932a2009-09-17 03:53:28 +00002596 assert(!Substitutions.count(Ptr) && "Substitution already exists!");
Anders Carlsson9d85b722010-06-02 04:29:50 +00002597 Substitutions[Ptr] = SeqID++;
Anders Carlsson76967372009-09-17 00:43:46 +00002598}
2599
Daniel Dunbar1b077112009-11-21 09:06:10 +00002600//
Mike Stump1eb44332009-09-09 15:08:12 +00002601
Daniel Dunbar1b077112009-11-21 09:06:10 +00002602/// \brief Mangles the name of the declaration D and emits that name to the
2603/// given output stream.
2604///
2605/// If the declaration D requires a mangled name, this routine will emit that
2606/// mangled name to \p os and return true. Otherwise, \p os will be unchanged
2607/// and this routine will return false. In this case, the caller should just
2608/// emit the identifier of the declaration (\c D->getIdentifier()) as its
2609/// name.
Peter Collingbourne14110472011-01-13 18:57:25 +00002610void ItaniumMangleContext::mangleName(const NamedDecl *D,
Rafael Espindola0e376a02011-02-11 01:41:00 +00002611 llvm::raw_ostream &Out) {
Daniel Dunbarc02ab4c2009-11-21 09:14:44 +00002612 assert((isa<FunctionDecl>(D) || isa<VarDecl>(D)) &&
2613 "Invalid mangleName() call, argument is not a variable or function!");
2614 assert(!isa<CXXConstructorDecl>(D) && !isa<CXXDestructorDecl>(D) &&
2615 "Invalid mangleName() call on 'structor decl!");
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002616
Daniel Dunbar1b077112009-11-21 09:06:10 +00002617 PrettyStackTraceDecl CrashInfo(D, SourceLocation(),
2618 getASTContext().getSourceManager(),
2619 "Mangling declaration");
Mike Stump1eb44332009-09-09 15:08:12 +00002620
Rafael Espindolac4850c22011-02-10 23:59:36 +00002621 CXXNameMangler Mangler(*this, Out);
Daniel Dunbar94fd26d2009-11-21 09:06:22 +00002622 return Mangler.mangle(D);
Daniel Dunbar1b077112009-11-21 09:06:10 +00002623}
Mike Stump1eb44332009-09-09 15:08:12 +00002624
Peter Collingbourne14110472011-01-13 18:57:25 +00002625void ItaniumMangleContext::mangleCXXCtor(const CXXConstructorDecl *D,
2626 CXXCtorType Type,
Rafael Espindola0e376a02011-02-11 01:41:00 +00002627 llvm::raw_ostream &Out) {
Rafael Espindolac4850c22011-02-10 23:59:36 +00002628 CXXNameMangler Mangler(*this, Out, D, Type);
Daniel Dunbar77939c92009-11-21 09:06:31 +00002629 Mangler.mangle(D);
Daniel Dunbar1b077112009-11-21 09:06:10 +00002630}
Mike Stump1eb44332009-09-09 15:08:12 +00002631
Peter Collingbourne14110472011-01-13 18:57:25 +00002632void ItaniumMangleContext::mangleCXXDtor(const CXXDestructorDecl *D,
2633 CXXDtorType Type,
Rafael Espindola0e376a02011-02-11 01:41:00 +00002634 llvm::raw_ostream &Out) {
Rafael Espindolac4850c22011-02-10 23:59:36 +00002635 CXXNameMangler Mangler(*this, Out, D, Type);
Daniel Dunbar77939c92009-11-21 09:06:31 +00002636 Mangler.mangle(D);
Daniel Dunbar1b077112009-11-21 09:06:10 +00002637}
Mike Stumpf1216772009-07-31 18:25:34 +00002638
Peter Collingbourne14110472011-01-13 18:57:25 +00002639void ItaniumMangleContext::mangleThunk(const CXXMethodDecl *MD,
2640 const ThunkInfo &Thunk,
Rafael Espindolaf0be9792011-02-11 02:52:17 +00002641 llvm::raw_ostream &Out) {
Anders Carlsson19879c92010-03-23 17:17:29 +00002642 // <special-name> ::= T <call-offset> <base encoding>
2643 // # base is the nominal target function of thunk
2644 // <special-name> ::= Tc <call-offset> <call-offset> <base encoding>
2645 // # base is the nominal target function of thunk
2646 // # first call-offset is 'this' adjustment
2647 // # second call-offset is result adjustment
Sean Huntc3021132010-05-05 15:23:54 +00002648
Anders Carlsson19879c92010-03-23 17:17:29 +00002649 assert(!isa<CXXDestructorDecl>(MD) &&
2650 "Use mangleCXXDtor for destructor decls!");
Rafael Espindolac4850c22011-02-10 23:59:36 +00002651 CXXNameMangler Mangler(*this, Out);
Anders Carlsson19879c92010-03-23 17:17:29 +00002652 Mangler.getStream() << "_ZT";
2653 if (!Thunk.Return.isEmpty())
2654 Mangler.getStream() << 'c';
Sean Huntc3021132010-05-05 15:23:54 +00002655
Anders Carlsson19879c92010-03-23 17:17:29 +00002656 // Mangle the 'this' pointer adjustment.
2657 Mangler.mangleCallOffset(Thunk.This.NonVirtual, Thunk.This.VCallOffsetOffset);
Sean Huntc3021132010-05-05 15:23:54 +00002658
Anders Carlsson19879c92010-03-23 17:17:29 +00002659 // Mangle the return pointer adjustment if there is one.
2660 if (!Thunk.Return.isEmpty())
2661 Mangler.mangleCallOffset(Thunk.Return.NonVirtual,
2662 Thunk.Return.VBaseOffsetOffset);
Sean Huntc3021132010-05-05 15:23:54 +00002663
Anders Carlsson19879c92010-03-23 17:17:29 +00002664 Mangler.mangleFunctionEncoding(MD);
2665}
2666
Sean Huntc3021132010-05-05 15:23:54 +00002667void
Peter Collingbourne14110472011-01-13 18:57:25 +00002668ItaniumMangleContext::mangleCXXDtorThunk(const CXXDestructorDecl *DD,
2669 CXXDtorType Type,
2670 const ThisAdjustment &ThisAdjustment,
Rafael Espindolaf0be9792011-02-11 02:52:17 +00002671 llvm::raw_ostream &Out) {
Anders Carlsson19879c92010-03-23 17:17:29 +00002672 // <special-name> ::= T <call-offset> <base encoding>
2673 // # base is the nominal target function of thunk
Rafael Espindolac4850c22011-02-10 23:59:36 +00002674 CXXNameMangler Mangler(*this, Out, DD, Type);
Anders Carlsson19879c92010-03-23 17:17:29 +00002675 Mangler.getStream() << "_ZT";
2676
2677 // Mangle the 'this' pointer adjustment.
Sean Huntc3021132010-05-05 15:23:54 +00002678 Mangler.mangleCallOffset(ThisAdjustment.NonVirtual,
Anders Carlsson19879c92010-03-23 17:17:29 +00002679 ThisAdjustment.VCallOffsetOffset);
2680
2681 Mangler.mangleFunctionEncoding(DD);
2682}
2683
Daniel Dunbarc0747712009-11-21 09:12:13 +00002684/// mangleGuardVariable - Returns the mangled name for a guard variable
2685/// for the passed in VarDecl.
Peter Collingbourne14110472011-01-13 18:57:25 +00002686void ItaniumMangleContext::mangleItaniumGuardVariable(const VarDecl *D,
Rafael Espindolaf0be9792011-02-11 02:52:17 +00002687 llvm::raw_ostream &Out) {
Daniel Dunbarc0747712009-11-21 09:12:13 +00002688 // <special-name> ::= GV <object name> # Guard variable for one-time
2689 // # initialization
Rafael Espindolac4850c22011-02-10 23:59:36 +00002690 CXXNameMangler Mangler(*this, Out);
Daniel Dunbarc0747712009-11-21 09:12:13 +00002691 Mangler.getStream() << "_ZGV";
2692 Mangler.mangleName(D);
2693}
2694
Peter Collingbourne14110472011-01-13 18:57:25 +00002695void ItaniumMangleContext::mangleReferenceTemporary(const VarDecl *D,
Rafael Espindolaf0be9792011-02-11 02:52:17 +00002696 llvm::raw_ostream &Out) {
Anders Carlsson715edf22010-06-26 16:09:40 +00002697 // We match the GCC mangling here.
2698 // <special-name> ::= GR <object name>
Rafael Espindolac4850c22011-02-10 23:59:36 +00002699 CXXNameMangler Mangler(*this, Out);
Anders Carlsson715edf22010-06-26 16:09:40 +00002700 Mangler.getStream() << "_ZGR";
2701 Mangler.mangleName(D);
2702}
2703
Peter Collingbourne14110472011-01-13 18:57:25 +00002704void ItaniumMangleContext::mangleCXXVTable(const CXXRecordDecl *RD,
Rafael Espindolaf0be9792011-02-11 02:52:17 +00002705 llvm::raw_ostream &Out) {
Daniel Dunbarc0747712009-11-21 09:12:13 +00002706 // <special-name> ::= TV <type> # virtual table
Rafael Espindolac4850c22011-02-10 23:59:36 +00002707 CXXNameMangler Mangler(*this, Out);
Daniel Dunbarc0747712009-11-21 09:12:13 +00002708 Mangler.getStream() << "_ZTV";
Douglas Gregor1b12a3b2010-05-26 05:11:13 +00002709 Mangler.mangleNameOrStandardSubstitution(RD);
Daniel Dunbar1b077112009-11-21 09:06:10 +00002710}
Mike Stump82d75b02009-11-10 01:58:37 +00002711
Peter Collingbourne14110472011-01-13 18:57:25 +00002712void ItaniumMangleContext::mangleCXXVTT(const CXXRecordDecl *RD,
Rafael Espindolaf0be9792011-02-11 02:52:17 +00002713 llvm::raw_ostream &Out) {
Daniel Dunbarc0747712009-11-21 09:12:13 +00002714 // <special-name> ::= TT <type> # VTT structure
Rafael Espindolac4850c22011-02-10 23:59:36 +00002715 CXXNameMangler Mangler(*this, Out);
Daniel Dunbarc0747712009-11-21 09:12:13 +00002716 Mangler.getStream() << "_ZTT";
Douglas Gregor1b12a3b2010-05-26 05:11:13 +00002717 Mangler.mangleNameOrStandardSubstitution(RD);
Daniel Dunbar1b077112009-11-21 09:06:10 +00002718}
Mike Stumpab3f7e92009-11-10 01:41:59 +00002719
Peter Collingbourne14110472011-01-13 18:57:25 +00002720void ItaniumMangleContext::mangleCXXCtorVTable(const CXXRecordDecl *RD,
2721 int64_t Offset,
2722 const CXXRecordDecl *Type,
Rafael Espindolaf0be9792011-02-11 02:52:17 +00002723 llvm::raw_ostream &Out) {
Daniel Dunbarc0747712009-11-21 09:12:13 +00002724 // <special-name> ::= TC <type> <offset number> _ <base type>
Rafael Espindolac4850c22011-02-10 23:59:36 +00002725 CXXNameMangler Mangler(*this, Out);
Daniel Dunbarc0747712009-11-21 09:12:13 +00002726 Mangler.getStream() << "_ZTC";
Douglas Gregor1b12a3b2010-05-26 05:11:13 +00002727 Mangler.mangleNameOrStandardSubstitution(RD);
Daniel Dunbarc0747712009-11-21 09:12:13 +00002728 Mangler.getStream() << Offset;
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002729 Mangler.getStream() << '_';
Douglas Gregor1b12a3b2010-05-26 05:11:13 +00002730 Mangler.mangleNameOrStandardSubstitution(Type);
Daniel Dunbar1b077112009-11-21 09:06:10 +00002731}
Mike Stump738f8c22009-07-31 23:15:31 +00002732
Peter Collingbourne14110472011-01-13 18:57:25 +00002733void ItaniumMangleContext::mangleCXXRTTI(QualType Ty,
Rafael Espindolaf0be9792011-02-11 02:52:17 +00002734 llvm::raw_ostream &Out) {
Daniel Dunbarc0747712009-11-21 09:12:13 +00002735 // <special-name> ::= TI <type> # typeinfo structure
Douglas Gregor154fe982009-12-23 22:04:40 +00002736 assert(!Ty.hasQualifiers() && "RTTI info cannot have top-level qualifiers");
Rafael Espindolac4850c22011-02-10 23:59:36 +00002737 CXXNameMangler Mangler(*this, Out);
Daniel Dunbarc0747712009-11-21 09:12:13 +00002738 Mangler.getStream() << "_ZTI";
2739 Mangler.mangleType(Ty);
Daniel Dunbar1b077112009-11-21 09:06:10 +00002740}
Mike Stump67795982009-11-14 00:14:13 +00002741
Peter Collingbourne14110472011-01-13 18:57:25 +00002742void ItaniumMangleContext::mangleCXXRTTIName(QualType Ty,
Rafael Espindolaf0be9792011-02-11 02:52:17 +00002743 llvm::raw_ostream &Out) {
Daniel Dunbarc0747712009-11-21 09:12:13 +00002744 // <special-name> ::= TS <type> # typeinfo name (null terminated byte string)
Rafael Espindolac4850c22011-02-10 23:59:36 +00002745 CXXNameMangler Mangler(*this, Out);
Daniel Dunbarc0747712009-11-21 09:12:13 +00002746 Mangler.getStream() << "_ZTS";
2747 Mangler.mangleType(Ty);
Mike Stumpf1216772009-07-31 18:25:34 +00002748}
Peter Collingbourne14110472011-01-13 18:57:25 +00002749
2750MangleContext *clang::createItaniumMangleContext(ASTContext &Context,
2751 Diagnostic &Diags) {
2752 return new ItaniumMangleContext(Context, Diags);
2753}