blob: e47d9cb43a6222794d65cb68a4c5d14bbe55620a [file] [log] [blame]
Douglas Gregor6ec36682009-02-18 23:53:56 +00001//===--- Mangle.cpp - Mangle C++ Names --------------------------*- 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//===----------------------------------------------------------------------===//
17#include "Mangle.h"
18#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"
Douglas Gregor6ec36682009-02-18 23:53:56 +000024#include "clang/Basic/SourceManager.h"
Anders Carlssonc4355b62009-10-07 01:45:02 +000025#include "llvm/ADT/StringExtras.h"
Douglas Gregor5f2bfd42009-02-13 00:10:09 +000026#include "llvm/Support/raw_ostream.h"
John McCallefe6aee2009-09-05 07:56:18 +000027#include "llvm/Support/ErrorHandling.h"
Anders Carlsson461e3262010-04-08 16:30:25 +000028#include "CGVTables.h"
Anders Carlssonf98574b2010-02-05 07:31:37 +000029
30#define MANGLE_CHECKER 0
31
32#if MANGLE_CHECKER
33#include <cxxabi.h>
34#endif
35
Douglas Gregor5f2bfd42009-02-13 00:10:09 +000036using namespace clang;
Anders Carlssonb73a5be2009-11-26 02:49:32 +000037using namespace CodeGen;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +000038
Charles Davis685b1d92010-05-26 18:25:27 +000039MiscNameMangler::MiscNameMangler(MangleContext &C,
40 llvm::SmallVectorImpl<char> &Res)
41 : Context(C), Out(Res) { }
42
Fariborz Jahanian564360b2010-06-24 00:08:06 +000043void MiscNameMangler::mangleBlock(GlobalDecl GD, const BlockDecl *BD) {
Charles Davis685b1d92010-05-26 18:25:27 +000044 // Mangle the context of the block.
45 // FIXME: We currently mimic GCC's mangling scheme, which leaves much to be
46 // desired. Come up with a better mangling scheme.
47 const DeclContext *DC = BD->getDeclContext();
48 while (isa<BlockDecl>(DC) || isa<EnumDecl>(DC))
49 DC = DC->getParent();
50 if (DC->isFunctionOrMethod()) {
51 Out << "__";
52 if (const ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(DC))
53 mangleObjCMethodName(Method);
54 else {
55 const NamedDecl *ND = cast<NamedDecl>(DC);
56 if (IdentifierInfo *II = ND->getIdentifier())
57 Out << II->getName();
Fariborz Jahanian564360b2010-06-24 00:08:06 +000058 else if (const CXXDestructorDecl *D = dyn_cast<CXXDestructorDecl>(ND)) {
59 llvm::SmallString<64> Buffer;
60 Context.mangleCXXDtor(D, GD.getDtorType(), Buffer);
61 Out << Buffer;
62 }
63 else if (const CXXConstructorDecl *D = dyn_cast<CXXConstructorDecl>(ND)) {
64 llvm::SmallString<64> Buffer;
65 Context.mangleCXXCtor(D, GD.getCtorType(), Buffer);
66 Out << Buffer;
67 }
Charles Davis685b1d92010-05-26 18:25:27 +000068 else {
69 // FIXME: We were doing a mangleUnqualifiedName() before, but that's
70 // a private member of a class that will soon itself be private to the
71 // Itanium C++ ABI object. What should we do now? Right now, I'm just
72 // calling the mangleName() method on the MangleContext; is there a
73 // better way?
74 llvm::SmallString<64> Buffer;
75 Context.mangleName(ND, Buffer);
76 Out << Buffer;
77 }
78 }
79 Out << "_block_invoke_" << Context.getBlockId(BD, true);
80 } else {
81 Out << "__block_global_" << Context.getBlockId(BD, false);
82 }
83}
84
85void MiscNameMangler::mangleObjCMethodName(const ObjCMethodDecl *MD) {
86 llvm::SmallString<64> Name;
87 llvm::raw_svector_ostream OS(Name);
88
89 const ObjCContainerDecl *CD =
90 dyn_cast<ObjCContainerDecl>(MD->getDeclContext());
91 assert (CD && "Missing container decl in GetNameForMethod");
92 OS << (MD->isInstanceMethod() ? '-' : '+') << '[' << CD->getName();
93 if (const ObjCCategoryImplDecl *CID = dyn_cast<ObjCCategoryImplDecl>(CD))
94 OS << '(' << CID << ')';
95 OS << ' ' << MD->getSelector().getAsString() << ']';
96
97 Out << OS.str().size() << OS.str();
98}
99
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000100namespace {
Fariborz Jahanian57058532010-03-03 19:41:08 +0000101
John McCall82b7d7b2010-10-18 21:28:44 +0000102static const CXXRecordDecl *GetLocalClassDecl(const NamedDecl *ND) {
103 const DeclContext *DC = dyn_cast<DeclContext>(ND);
104 if (!DC)
105 DC = ND->getDeclContext();
106 while (!DC->isNamespace() && !DC->isTranslationUnit()) {
107 if (isa<FunctionDecl>(DC->getParent()))
108 return dyn_cast<CXXRecordDecl>(DC);
109 DC = DC->getParent();
Fariborz Jahanian57058532010-03-03 19:41:08 +0000110 }
111 return 0;
112}
113
Anders Carlsson7e120032009-11-24 05:36:32 +0000114static const CXXMethodDecl *getStructor(const CXXMethodDecl *MD) {
115 assert((isa<CXXConstructorDecl>(MD) || isa<CXXDestructorDecl>(MD)) &&
116 "Passed in decl is not a ctor or dtor!");
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000117
Anders Carlsson7e120032009-11-24 05:36:32 +0000118 if (const TemplateDecl *TD = MD->getPrimaryTemplate()) {
119 MD = cast<CXXMethodDecl>(TD->getTemplatedDecl());
120
121 assert((isa<CXXConstructorDecl>(MD) || isa<CXXDestructorDecl>(MD)) &&
122 "Templated decl is not a ctor or dtor!");
123 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000124
Anders Carlsson7e120032009-11-24 05:36:32 +0000125 return MD;
126}
John McCall1dd73832010-02-04 01:42:13 +0000127
128static const unsigned UnknownArity = ~0U;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000129
Daniel Dunbar1b077112009-11-21 09:06:10 +0000130/// CXXNameMangler - Manage the mangling of a single name.
Daniel Dunbarc0747712009-11-21 09:12:13 +0000131class CXXNameMangler {
Daniel Dunbar1b077112009-11-21 09:06:10 +0000132 MangleContext &Context;
Daniel Dunbar94fd26d2009-11-21 09:06:22 +0000133 llvm::raw_svector_ostream Out;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000134
Daniel Dunbar1b077112009-11-21 09:06:10 +0000135 const CXXMethodDecl *Structor;
136 unsigned StructorType;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000137
Anders Carlsson9d85b722010-06-02 04:29:50 +0000138 /// SeqID - The next subsitution sequence number.
139 unsigned SeqID;
140
Daniel Dunbar1b077112009-11-21 09:06:10 +0000141 llvm::DenseMap<uintptr_t, unsigned> Substitutions;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000142
John McCall1dd73832010-02-04 01:42:13 +0000143 ASTContext &getASTContext() const { return Context.getASTContext(); }
144
Daniel Dunbarc0747712009-11-21 09:12:13 +0000145public:
Daniel Dunbar94fd26d2009-11-21 09:06:22 +0000146 CXXNameMangler(MangleContext &C, llvm::SmallVectorImpl<char> &Res)
Anders Carlsson9d85b722010-06-02 04:29:50 +0000147 : Context(C), Out(Res), Structor(0), StructorType(0), SeqID(0) { }
Daniel Dunbar77939c92009-11-21 09:06:31 +0000148 CXXNameMangler(MangleContext &C, llvm::SmallVectorImpl<char> &Res,
149 const CXXConstructorDecl *D, CXXCtorType Type)
Anders Carlsson9d85b722010-06-02 04:29:50 +0000150 : Context(C), Out(Res), Structor(getStructor(D)), StructorType(Type),
151 SeqID(0) { }
Daniel Dunbar77939c92009-11-21 09:06:31 +0000152 CXXNameMangler(MangleContext &C, llvm::SmallVectorImpl<char> &Res,
153 const CXXDestructorDecl *D, CXXDtorType Type)
Anders Carlsson9d85b722010-06-02 04:29:50 +0000154 : Context(C), Out(Res), Structor(getStructor(D)), StructorType(Type),
155 SeqID(0) { }
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000156
Anders Carlssonf98574b2010-02-05 07:31:37 +0000157#if MANGLE_CHECKER
158 ~CXXNameMangler() {
159 if (Out.str()[0] == '\01')
160 return;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000161
Anders Carlssonf98574b2010-02-05 07:31:37 +0000162 int status = 0;
163 char *result = abi::__cxa_demangle(Out.str().str().c_str(), 0, 0, &status);
164 assert(status == 0 && "Could not demangle mangled name!");
165 free(result);
166 }
167#endif
Daniel Dunbarc0747712009-11-21 09:12:13 +0000168 llvm::raw_svector_ostream &getStream() { return Out; }
169
Daniel Dunbar7e0c1952009-11-21 09:17:15 +0000170 void mangle(const NamedDecl *D, llvm::StringRef Prefix = "_Z");
Anders Carlsson19879c92010-03-23 17:17:29 +0000171 void mangleCallOffset(int64_t NonVirtual, int64_t Virtual);
John McCall0512e482010-07-14 04:20:34 +0000172 void mangleNumber(const llvm::APSInt &I);
Anders Carlssona94822e2009-11-26 02:32:05 +0000173 void mangleNumber(int64_t Number);
John McCall0512e482010-07-14 04:20:34 +0000174 void mangleFloat(const llvm::APFloat &F);
Daniel Dunbarc0747712009-11-21 09:12:13 +0000175 void mangleFunctionEncoding(const FunctionDecl *FD);
176 void mangleName(const NamedDecl *ND);
177 void mangleType(QualType T);
Douglas Gregor1b12a3b2010-05-26 05:11:13 +0000178 void mangleNameOrStandardSubstitution(const NamedDecl *ND);
179
Daniel Dunbarc0747712009-11-21 09:12:13 +0000180private:
Daniel Dunbar1b077112009-11-21 09:06:10 +0000181 bool mangleSubstitution(const NamedDecl *ND);
182 bool mangleSubstitution(QualType T);
Douglas Gregor1e9268e2010-04-28 05:58:56 +0000183 bool mangleSubstitution(TemplateName Template);
Daniel Dunbar1b077112009-11-21 09:06:10 +0000184 bool mangleSubstitution(uintptr_t Ptr);
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000185
Daniel Dunbar1b077112009-11-21 09:06:10 +0000186 bool mangleStandardSubstitution(const NamedDecl *ND);
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000187
Daniel Dunbar1b077112009-11-21 09:06:10 +0000188 void addSubstitution(const NamedDecl *ND) {
189 ND = cast<NamedDecl>(ND->getCanonicalDecl());
Anders Carlsson433d1372009-11-07 04:26:04 +0000190
Daniel Dunbar1b077112009-11-21 09:06:10 +0000191 addSubstitution(reinterpret_cast<uintptr_t>(ND));
192 }
193 void addSubstitution(QualType T);
Douglas Gregor1e9268e2010-04-28 05:58:56 +0000194 void addSubstitution(TemplateName Template);
Daniel Dunbar1b077112009-11-21 09:06:10 +0000195 void addSubstitution(uintptr_t Ptr);
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000196
John McCall1dd73832010-02-04 01:42:13 +0000197 void mangleUnresolvedScope(NestedNameSpecifier *Qualifier);
198 void mangleUnresolvedName(NestedNameSpecifier *Qualifier,
199 DeclarationName Name,
200 unsigned KnownArity = UnknownArity);
201
Daniel Dunbar1b077112009-11-21 09:06:10 +0000202 void mangleName(const TemplateDecl *TD,
203 const TemplateArgument *TemplateArgs,
204 unsigned NumTemplateArgs);
John McCall1dd73832010-02-04 01:42:13 +0000205 void mangleUnqualifiedName(const NamedDecl *ND) {
206 mangleUnqualifiedName(ND, ND->getDeclName(), UnknownArity);
207 }
208 void mangleUnqualifiedName(const NamedDecl *ND, DeclarationName Name,
209 unsigned KnownArity);
Daniel Dunbar1b077112009-11-21 09:06:10 +0000210 void mangleUnscopedName(const NamedDecl *ND);
211 void mangleUnscopedTemplateName(const TemplateDecl *ND);
Douglas Gregor1e9268e2010-04-28 05:58:56 +0000212 void mangleUnscopedTemplateName(TemplateName);
Daniel Dunbar1b077112009-11-21 09:06:10 +0000213 void mangleSourceName(const IdentifierInfo *II);
214 void mangleLocalName(const NamedDecl *ND);
Fariborz Jahanian57058532010-03-03 19:41:08 +0000215 void mangleNestedName(const NamedDecl *ND, const DeclContext *DC,
216 bool NoFunction=false);
Daniel Dunbar1b077112009-11-21 09:06:10 +0000217 void mangleNestedName(const TemplateDecl *TD,
218 const TemplateArgument *TemplateArgs,
219 unsigned NumTemplateArgs);
Fariborz Jahanian57058532010-03-03 19:41:08 +0000220 void manglePrefix(const DeclContext *DC, bool NoFunction=false);
Daniel Dunbar1b077112009-11-21 09:06:10 +0000221 void mangleTemplatePrefix(const TemplateDecl *ND);
Douglas Gregor20f0cc72010-04-23 03:10:43 +0000222 void mangleTemplatePrefix(TemplateName Template);
Daniel Dunbar1b077112009-11-21 09:06:10 +0000223 void mangleOperatorName(OverloadedOperatorKind OO, unsigned Arity);
224 void mangleQualifiers(Qualifiers Quals);
John McCallefe6aee2009-09-05 07:56:18 +0000225
Anders Carlsson7b06f6c2009-12-10 03:14:39 +0000226 void mangleObjCMethodName(const ObjCMethodDecl *MD);
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000227
Daniel Dunbar1b077112009-11-21 09:06:10 +0000228 // Declare manglers for every type class.
John McCallefe6aee2009-09-05 07:56:18 +0000229#define ABSTRACT_TYPE(CLASS, PARENT)
230#define NON_CANONICAL_TYPE(CLASS, PARENT)
231#define TYPE(CLASS, PARENT) void mangleType(const CLASS##Type *T);
232#include "clang/AST/TypeNodes.def"
233
Daniel Dunbar1b077112009-11-21 09:06:10 +0000234 void mangleType(const TagType*);
John McCallb6f532e2010-07-14 06:43:17 +0000235 void mangleType(TemplateName);
Daniel Dunbar1b077112009-11-21 09:06:10 +0000236 void mangleBareFunctionType(const FunctionType *T,
237 bool MangleReturnType);
Anders Carlssone170ba72009-12-14 01:45:37 +0000238
239 void mangleIntegerLiteral(QualType T, const llvm::APSInt &Value);
John McCall2f27bf82010-02-04 02:56:29 +0000240 void mangleMemberExpr(const Expr *Base, bool IsArrow,
241 NestedNameSpecifier *Qualifier,
242 DeclarationName Name,
243 unsigned KnownArity);
John McCall5e1e89b2010-08-18 19:18:59 +0000244 void mangleExpression(const Expr *E, unsigned Arity = UnknownArity);
Daniel Dunbar1b077112009-11-21 09:06:10 +0000245 void mangleCXXCtorType(CXXCtorType T);
246 void mangleCXXDtorType(CXXDtorType T);
Mike Stump1eb44332009-09-09 15:08:12 +0000247
John McCall6dbce192010-08-20 00:17:19 +0000248 void mangleTemplateArgs(const ExplicitTemplateArgumentList &TemplateArgs);
Douglas Gregor20f0cc72010-04-23 03:10:43 +0000249 void mangleTemplateArgs(TemplateName Template,
250 const TemplateArgument *TemplateArgs,
Sean Huntc3021132010-05-05 15:23:54 +0000251 unsigned NumTemplateArgs);
Rafael Espindolad9800722010-03-11 14:07:00 +0000252 void mangleTemplateArgs(const TemplateParameterList &PL,
253 const TemplateArgument *TemplateArgs,
Daniel Dunbar1b077112009-11-21 09:06:10 +0000254 unsigned NumTemplateArgs);
Rafael Espindolad9800722010-03-11 14:07:00 +0000255 void mangleTemplateArgs(const TemplateParameterList &PL,
256 const TemplateArgumentList &AL);
257 void mangleTemplateArg(const NamedDecl *P, const TemplateArgument &A);
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000258
Daniel Dunbar1b077112009-11-21 09:06:10 +0000259 void mangleTemplateParameter(unsigned Index);
260};
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000261}
262
Anders Carlsson43f17402009-04-02 15:51:53 +0000263static bool isInCLinkageSpecification(const Decl *D) {
Douglas Gregor457e2812009-10-28 16:31:34 +0000264 D = D->getCanonicalDecl();
Mike Stump1eb44332009-09-09 15:08:12 +0000265 for (const DeclContext *DC = D->getDeclContext();
Anders Carlsson43f17402009-04-02 15:51:53 +0000266 !DC->isTranslationUnit(); DC = DC->getParent()) {
Mike Stump1eb44332009-09-09 15:08:12 +0000267 if (const LinkageSpecDecl *Linkage = dyn_cast<LinkageSpecDecl>(DC))
Anders Carlsson43f17402009-04-02 15:51:53 +0000268 return Linkage->getLanguage() == LinkageSpecDecl::lang_c;
269 }
Mike Stump1eb44332009-09-09 15:08:12 +0000270
Anders Carlsson43f17402009-04-02 15:51:53 +0000271 return false;
272}
273
Daniel Dunbarf981bf82009-11-21 09:14:52 +0000274bool MangleContext::shouldMangleDeclName(const NamedDecl *D) {
275 // In C, functions with no attributes never need to be mangled. Fastpath them.
276 if (!getASTContext().getLangOptions().CPlusPlus && !D->hasAttrs())
277 return false;
278
279 // Any decl can be declared with __asm("foo") on it, and this takes precedence
280 // over all other naming in the .o file.
281 if (D->hasAttr<AsmLabelAttr>())
282 return true;
283
Mike Stump141c5af2009-09-02 00:25:38 +0000284 // Clang's "overloadable" attribute extension to C/C++ implies name mangling
Anders Carlssona1e16222009-11-07 07:15:03 +0000285 // (always) as does passing a C++ member function and a function
286 // whose name is not a simple identifier.
Daniel Dunbarf981bf82009-11-21 09:14:52 +0000287 const FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
288 if (FD && (FD->hasAttr<OverloadableAttr>() || isa<CXXMethodDecl>(FD) ||
289 !FD->getDeclName().isIdentifier()))
290 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000291
Daniel Dunbarf981bf82009-11-21 09:14:52 +0000292 // Otherwise, no mangling is done outside C++ mode.
293 if (!getASTContext().getLangOptions().CPlusPlus)
294 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000295
Sean Hunt31455252010-01-24 03:04:27 +0000296 // Variables at global scope with non-internal linkage are not mangled
Eli Friedman7facf842009-12-02 20:32:49 +0000297 if (!FD) {
298 const DeclContext *DC = D->getDeclContext();
299 // Check for extern variable declared locally.
Fariborz Jahaniane81c5612010-06-30 18:57:21 +0000300 if (DC->isFunctionOrMethod() && D->hasLinkage())
Eli Friedman7facf842009-12-02 20:32:49 +0000301 while (!DC->isNamespace() && !DC->isTranslationUnit())
302 DC = DC->getParent();
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000303 if (DC->isTranslationUnit() && D->getLinkage() != InternalLinkage)
Eli Friedman7facf842009-12-02 20:32:49 +0000304 return false;
305 }
306
Eli Friedmanc00cb642010-07-18 20:49:59 +0000307 // Class members are always mangled.
308 if (D->getDeclContext()->isRecord())
309 return true;
310
Eli Friedman7facf842009-12-02 20:32:49 +0000311 // C functions and "main" are not mangled.
312 if ((FD && FD->isMain()) || isInCLinkageSpecification(D))
Daniel Dunbarf981bf82009-11-21 09:14:52 +0000313 return false;
314
Anders Carlsson43f17402009-04-02 15:51:53 +0000315 return true;
316}
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000317
Daniel Dunbar7e0c1952009-11-21 09:17:15 +0000318void CXXNameMangler::mangle(const NamedDecl *D, llvm::StringRef Prefix) {
Mike Stump141c5af2009-09-02 00:25:38 +0000319 // Any decl can be declared with __asm("foo") on it, and this takes precedence
320 // over all other naming in the .o file.
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000321 if (const AsmLabelAttr *ALA = D->getAttr<AsmLabelAttr>()) {
Chris Lattnerca3f25c2009-03-21 08:24:40 +0000322 // If we have an asm name, then we use it as the mangling.
323 Out << '\01'; // LLVM IR Marker for __asm("foo")
324 Out << ALA->getLabel();
Daniel Dunbarf981bf82009-11-21 09:14:52 +0000325 return;
Chris Lattnerca3f25c2009-03-21 08:24:40 +0000326 }
Mike Stump1eb44332009-09-09 15:08:12 +0000327
Sean Hunt31455252010-01-24 03:04:27 +0000328 // <mangled-name> ::= _Z <encoding>
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000329 // ::= <data name>
330 // ::= <special-name>
Daniel Dunbar7e0c1952009-11-21 09:17:15 +0000331 Out << Prefix;
332 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
Daniel Dunbarf981bf82009-11-21 09:14:52 +0000333 mangleFunctionEncoding(FD);
Rafael Espindolad9800722010-03-11 14:07:00 +0000334 else if (const VarDecl *VD = dyn_cast<VarDecl>(D))
335 mangleName(VD);
Daniel Dunbar7e0c1952009-11-21 09:17:15 +0000336 else
Rafael Espindolad9800722010-03-11 14:07:00 +0000337 mangleName(cast<FieldDecl>(D));
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000338}
339
340void CXXNameMangler::mangleFunctionEncoding(const FunctionDecl *FD) {
341 // <encoding> ::= <function name> <bare-function-type>
342 mangleName(FD);
Mike Stump1eb44332009-09-09 15:08:12 +0000343
Daniel Dunbar7e0c1952009-11-21 09:17:15 +0000344 // Don't mangle in the type if this isn't a decl we should typically mangle.
345 if (!Context.shouldMangleDeclName(FD))
346 return;
347
Mike Stump141c5af2009-09-02 00:25:38 +0000348 // Whether the mangling of a function type includes the return type depends on
349 // the context and the nature of the function. The rules for deciding whether
350 // the return type is included are:
Mike Stump1eb44332009-09-09 15:08:12 +0000351 //
Douglas Gregor1fd2dd12009-06-29 22:39:32 +0000352 // 1. Template functions (names or types) have return types encoded, with
353 // the exceptions listed below.
Mike Stump1eb44332009-09-09 15:08:12 +0000354 // 2. Function types not appearing as part of a function name mangling,
Douglas Gregor1fd2dd12009-06-29 22:39:32 +0000355 // e.g. parameters, pointer types, etc., have return type encoded, with the
356 // exceptions listed below.
357 // 3. Non-template function names do not have return types encoded.
358 //
Mike Stump141c5af2009-09-02 00:25:38 +0000359 // The exceptions mentioned in (1) and (2) above, for which the return type is
360 // never included, are
Douglas Gregor1fd2dd12009-06-29 22:39:32 +0000361 // 1. Constructors.
362 // 2. Destructors.
363 // 3. Conversion operator functions, e.g. operator int.
364 bool MangleReturnType = false;
Anders Carlsson9234b7f2009-09-17 03:46:43 +0000365 if (FunctionTemplateDecl *PrimaryTemplate = FD->getPrimaryTemplate()) {
366 if (!(isa<CXXConstructorDecl>(FD) || isa<CXXDestructorDecl>(FD) ||
367 isa<CXXConversionDecl>(FD)))
368 MangleReturnType = true;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000369
Anders Carlsson9234b7f2009-09-17 03:46:43 +0000370 // Mangle the type of the primary template.
371 FD = PrimaryTemplate->getTemplatedDecl();
372 }
373
John McCall54e14c42009-10-22 22:37:11 +0000374 // Do the canonicalization out here because parameter types can
375 // undergo additional canonicalization (e.g. array decay).
376 FunctionType *FT = cast<FunctionType>(Context.getASTContext()
377 .getCanonicalType(FD->getType()));
378
379 mangleBareFunctionType(FT, MangleReturnType);
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000380}
381
Anders Carlsson47846d22009-12-04 06:23:23 +0000382static const DeclContext *IgnoreLinkageSpecDecls(const DeclContext *DC) {
383 while (isa<LinkageSpecDecl>(DC)) {
Anders Carlsson47846d22009-12-04 06:23:23 +0000384 DC = DC->getParent();
385 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000386
Anders Carlsson47846d22009-12-04 06:23:23 +0000387 return DC;
388}
389
Anders Carlssonc820f902010-06-02 15:58:27 +0000390/// isStd - Return whether a given namespace is the 'std' namespace.
391static bool isStd(const NamespaceDecl *NS) {
392 if (!IgnoreLinkageSpecDecls(NS->getParent())->isTranslationUnit())
393 return false;
394
395 const IdentifierInfo *II = NS->getOriginalNamespace()->getIdentifier();
396 return II && II->isStr("std");
397}
398
Anders Carlsson47846d22009-12-04 06:23:23 +0000399// isStdNamespace - Return whether a given decl context is a toplevel 'std'
400// namespace.
Daniel Dunbar1308af92009-11-21 09:11:45 +0000401static bool isStdNamespace(const DeclContext *DC) {
Anders Carlsson47846d22009-12-04 06:23:23 +0000402 if (!DC->isNamespace())
403 return false;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000404
Anders Carlsson47846d22009-12-04 06:23:23 +0000405 return isStd(cast<NamespaceDecl>(DC));
Daniel Dunbar1308af92009-11-21 09:11:45 +0000406}
407
Anders Carlssonbb36ba42009-09-26 03:24:57 +0000408static const TemplateDecl *
409isTemplate(const NamedDecl *ND, const TemplateArgumentList *&TemplateArgs) {
Anders Carlsson2744a062009-09-18 19:00:18 +0000410 // Check if we have a function template.
411 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)){
Anders Carlssonbb36ba42009-09-26 03:24:57 +0000412 if (const TemplateDecl *TD = FD->getPrimaryTemplate()) {
Anders Carlsson2744a062009-09-18 19:00:18 +0000413 TemplateArgs = FD->getTemplateSpecializationArgs();
Anders Carlssonbb36ba42009-09-26 03:24:57 +0000414 return TD;
Anders Carlsson2744a062009-09-18 19:00:18 +0000415 }
416 }
417
Anders Carlssoneafc6dc2009-09-18 19:44:50 +0000418 // Check if we have a class template.
419 if (const ClassTemplateSpecializationDecl *Spec =
420 dyn_cast<ClassTemplateSpecializationDecl>(ND)) {
421 TemplateArgs = &Spec->getTemplateArgs();
Anders Carlssonbb36ba42009-09-26 03:24:57 +0000422 return Spec->getSpecializedTemplate();
Anders Carlssoneafc6dc2009-09-18 19:44:50 +0000423 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000424
Anders Carlsson2744a062009-09-18 19:00:18 +0000425 return 0;
426}
427
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000428void CXXNameMangler::mangleName(const NamedDecl *ND) {
429 // <name> ::= <nested-name>
430 // ::= <unscoped-name>
431 // ::= <unscoped-template-name> <template-args>
Anders Carlsson201ce742009-09-17 03:17:01 +0000432 // ::= <local-name>
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000433 //
Anders Carlssond58d6f72009-09-17 16:12:20 +0000434 const DeclContext *DC = ND->getDeclContext();
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000435
Eli Friedman7facf842009-12-02 20:32:49 +0000436 // If this is an extern variable declared locally, the relevant DeclContext
437 // is that of the containing namespace, or the translation unit.
438 if (isa<FunctionDecl>(DC) && ND->hasLinkage())
439 while (!DC->isNamespace() && !DC->isTranslationUnit())
440 DC = DC->getParent();
John McCall82b7d7b2010-10-18 21:28:44 +0000441 else if (GetLocalClassDecl(ND)) {
442 mangleLocalName(ND);
443 return;
444 }
Eli Friedman7facf842009-12-02 20:32:49 +0000445
Anders Carlsson5cc58c62009-09-22 17:23:30 +0000446 while (isa<LinkageSpecDecl>(DC))
Anders Carlssond58d6f72009-09-17 16:12:20 +0000447 DC = DC->getParent();
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000448
Anders Carlssond58d6f72009-09-17 16:12:20 +0000449 if (DC->isTranslationUnit() || isStdNamespace(DC)) {
Anders Carlsson2744a062009-09-18 19:00:18 +0000450 // Check if we have a template.
451 const TemplateArgumentList *TemplateArgs = 0;
Anders Carlsson0fa6df42009-09-26 19:45:45 +0000452 if (const TemplateDecl *TD = isTemplate(ND, TemplateArgs)) {
Anders Carlsson2744a062009-09-18 19:00:18 +0000453 mangleUnscopedTemplateName(TD);
Rafael Espindolad9800722010-03-11 14:07:00 +0000454 TemplateParameterList *TemplateParameters = TD->getTemplateParameters();
455 mangleTemplateArgs(*TemplateParameters, *TemplateArgs);
Anders Carlsson2744a062009-09-18 19:00:18 +0000456 return;
Anders Carlsson7482e242009-09-18 04:29:09 +0000457 }
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000458
Anders Carlsson7482e242009-09-18 04:29:09 +0000459 mangleUnscopedName(ND);
460 return;
461 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000462
Anders Carlsson7b06f6c2009-12-10 03:14:39 +0000463 if (isa<FunctionDecl>(DC) || isa<ObjCMethodDecl>(DC)) {
Anders Carlsson7482e242009-09-18 04:29:09 +0000464 mangleLocalName(ND);
465 return;
466 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000467
Eli Friedman7facf842009-12-02 20:32:49 +0000468 mangleNestedName(ND, DC);
Anders Carlsson7482e242009-09-18 04:29:09 +0000469}
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000470void CXXNameMangler::mangleName(const TemplateDecl *TD,
Anders Carlsson7624f212009-09-18 02:42:01 +0000471 const TemplateArgument *TemplateArgs,
472 unsigned NumTemplateArgs) {
Anders Carlsson47846d22009-12-04 06:23:23 +0000473 const DeclContext *DC = IgnoreLinkageSpecDecls(TD->getDeclContext());
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000474
Anders Carlsson7624f212009-09-18 02:42:01 +0000475 if (DC->isTranslationUnit() || isStdNamespace(DC)) {
Anders Carlsson0fa6df42009-09-26 19:45:45 +0000476 mangleUnscopedTemplateName(TD);
Rafael Espindolad9800722010-03-11 14:07:00 +0000477 TemplateParameterList *TemplateParameters = TD->getTemplateParameters();
478 mangleTemplateArgs(*TemplateParameters, TemplateArgs, NumTemplateArgs);
Anders Carlsson7624f212009-09-18 02:42:01 +0000479 } else {
480 mangleNestedName(TD, TemplateArgs, NumTemplateArgs);
481 }
482}
483
Anders Carlsson201ce742009-09-17 03:17:01 +0000484void CXXNameMangler::mangleUnscopedName(const NamedDecl *ND) {
485 // <unscoped-name> ::= <unqualified-name>
486 // ::= St <unqualified-name> # ::std::
487 if (isStdNamespace(ND->getDeclContext()))
488 Out << "St";
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000489
Anders Carlsson201ce742009-09-17 03:17:01 +0000490 mangleUnqualifiedName(ND);
491}
492
Anders Carlsson0fa6df42009-09-26 19:45:45 +0000493void CXXNameMangler::mangleUnscopedTemplateName(const TemplateDecl *ND) {
Anders Carlsson201ce742009-09-17 03:17:01 +0000494 // <unscoped-template-name> ::= <unscoped-name>
495 // ::= <substitution>
Anders Carlsson7624f212009-09-18 02:42:01 +0000496 if (mangleSubstitution(ND))
Anders Carlsson03c9d532009-09-17 04:02:31 +0000497 return;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000498
Douglas Gregor32fb4e12010-02-05 20:45:00 +0000499 // <template-template-param> ::= <template-param>
500 if (const TemplateTemplateParmDecl *TTP
501 = dyn_cast<TemplateTemplateParmDecl>(ND)) {
502 mangleTemplateParameter(TTP->getIndex());
503 return;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000504 }
Douglas Gregor32fb4e12010-02-05 20:45:00 +0000505
Anders Carlsson1668f202009-09-26 20:13:56 +0000506 mangleUnscopedName(ND->getTemplatedDecl());
Anders Carlsson7624f212009-09-18 02:42:01 +0000507 addSubstitution(ND);
Anders Carlsson201ce742009-09-17 03:17:01 +0000508}
509
Douglas Gregor1e9268e2010-04-28 05:58:56 +0000510void CXXNameMangler::mangleUnscopedTemplateName(TemplateName Template) {
511 // <unscoped-template-name> ::= <unscoped-name>
512 // ::= <substitution>
513 if (TemplateDecl *TD = Template.getAsTemplateDecl())
514 return mangleUnscopedTemplateName(TD);
Sean Huntc3021132010-05-05 15:23:54 +0000515
Douglas Gregor1e9268e2010-04-28 05:58:56 +0000516 if (mangleSubstitution(Template))
517 return;
518
519 // FIXME: How to cope with operators here?
520 DependentTemplateName *Dependent = Template.getAsDependentTemplateName();
521 assert(Dependent && "Not a dependent template name?");
522 if (!Dependent->isIdentifier()) {
523 // FIXME: We can't possibly know the arity of the operator here!
524 Diagnostic &Diags = Context.getDiags();
525 unsigned DiagID = Diags.getCustomDiagID(Diagnostic::Error,
526 "cannot mangle dependent operator name");
527 Diags.Report(FullSourceLoc(), DiagID);
528 return;
529 }
Sean Huntc3021132010-05-05 15:23:54 +0000530
Douglas Gregor1e9268e2010-04-28 05:58:56 +0000531 mangleSourceName(Dependent->getIdentifier());
532 addSubstitution(Template);
533}
534
John McCall0512e482010-07-14 04:20:34 +0000535void CXXNameMangler::mangleFloat(const llvm::APFloat &F) {
536 // TODO: avoid this copy with careful stream management.
537 llvm::SmallString<20> Buffer;
538 F.bitcastToAPInt().toString(Buffer, 16, false);
539 Out.write(Buffer.data(), Buffer.size());
540}
541
542void CXXNameMangler::mangleNumber(const llvm::APSInt &Value) {
543 if (Value.isSigned() && Value.isNegative()) {
544 Out << 'n';
545 Value.abs().print(Out, true);
546 } else
547 Value.print(Out, Value.isSigned());
548}
549
Anders Carlssona94822e2009-11-26 02:32:05 +0000550void CXXNameMangler::mangleNumber(int64_t Number) {
551 // <number> ::= [n] <non-negative decimal integer>
552 if (Number < 0) {
553 Out << 'n';
554 Number = -Number;
555 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000556
Anders Carlssona94822e2009-11-26 02:32:05 +0000557 Out << Number;
558}
559
Anders Carlsson19879c92010-03-23 17:17:29 +0000560void CXXNameMangler::mangleCallOffset(int64_t NonVirtual, int64_t Virtual) {
Mike Stump141c5af2009-09-02 00:25:38 +0000561 // <call-offset> ::= h <nv-offset> _
562 // ::= v <v-offset> _
563 // <nv-offset> ::= <offset number> # non-virtual base override
Anders Carlssona94822e2009-11-26 02:32:05 +0000564 // <v-offset> ::= <offset number> _ <virtual offset number>
Mike Stump141c5af2009-09-02 00:25:38 +0000565 // # virtual base override, with vcall offset
Anders Carlsson19879c92010-03-23 17:17:29 +0000566 if (!Virtual) {
Anders Carlssona94822e2009-11-26 02:32:05 +0000567 Out << 'h';
Anders Carlsson19879c92010-03-23 17:17:29 +0000568 mangleNumber(NonVirtual);
Anders Carlssona94822e2009-11-26 02:32:05 +0000569 Out << '_';
570 return;
Mike Stump141c5af2009-09-02 00:25:38 +0000571 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000572
Anders Carlssona94822e2009-11-26 02:32:05 +0000573 Out << 'v';
Anders Carlsson19879c92010-03-23 17:17:29 +0000574 mangleNumber(NonVirtual);
Anders Carlssona94822e2009-11-26 02:32:05 +0000575 Out << '_';
Anders Carlsson19879c92010-03-23 17:17:29 +0000576 mangleNumber(Virtual);
Anders Carlssona94822e2009-11-26 02:32:05 +0000577 Out << '_';
Mike Stump9124bcc2009-09-02 00:56:18 +0000578}
579
John McCall1dd73832010-02-04 01:42:13 +0000580void CXXNameMangler::mangleUnresolvedScope(NestedNameSpecifier *Qualifier) {
581 Qualifier = getASTContext().getCanonicalNestedNameSpecifier(Qualifier);
582 switch (Qualifier->getKind()) {
583 case NestedNameSpecifier::Global:
584 // nothing
585 break;
586 case NestedNameSpecifier::Namespace:
587 mangleName(Qualifier->getAsNamespace());
588 break;
589 case NestedNameSpecifier::TypeSpec:
Rafael Espindola9b35b252010-03-17 04:28:11 +0000590 case NestedNameSpecifier::TypeSpecWithTemplate: {
591 const Type *QTy = Qualifier->getAsType();
592
593 if (const TemplateSpecializationType *TST =
594 dyn_cast<TemplateSpecializationType>(QTy)) {
595 if (!mangleSubstitution(QualType(TST, 0))) {
Douglas Gregor20f0cc72010-04-23 03:10:43 +0000596 mangleTemplatePrefix(TST->getTemplateName());
Sean Huntc3021132010-05-05 15:23:54 +0000597
Douglas Gregor20f0cc72010-04-23 03:10:43 +0000598 // FIXME: GCC does not appear to mangle the template arguments when
599 // the template in question is a dependent template name. Should we
600 // emulate that badness?
601 mangleTemplateArgs(TST->getTemplateName(), TST->getArgs(),
Rafael Espindola9b35b252010-03-17 04:28:11 +0000602 TST->getNumArgs());
603 addSubstitution(QualType(TST, 0));
604 }
605 } else {
606 // We use the QualType mangle type variant here because it handles
607 // substitutions.
608 mangleType(QualType(QTy, 0));
609 }
610 }
John McCall1dd73832010-02-04 01:42:13 +0000611 break;
612 case NestedNameSpecifier::Identifier:
John McCallad5e7382010-03-01 23:49:17 +0000613 // Member expressions can have these without prefixes.
614 if (Qualifier->getPrefix())
615 mangleUnresolvedScope(Qualifier->getPrefix());
John McCall1dd73832010-02-04 01:42:13 +0000616 mangleSourceName(Qualifier->getAsIdentifier());
617 break;
618 }
619}
620
621/// Mangles a name which was not resolved to a specific entity.
622void CXXNameMangler::mangleUnresolvedName(NestedNameSpecifier *Qualifier,
623 DeclarationName Name,
624 unsigned KnownArity) {
625 if (Qualifier)
626 mangleUnresolvedScope(Qualifier);
627 // FIXME: ambiguity of unqualified lookup with ::
628
629 mangleUnqualifiedName(0, Name, KnownArity);
630}
631
Anders Carlsson6f7e2f42010-06-08 14:49:03 +0000632static const FieldDecl *FindFirstNamedDataMember(const RecordDecl *RD) {
633 assert(RD->isAnonymousStructOrUnion() &&
634 "Expected anonymous struct or union!");
635
636 for (RecordDecl::field_iterator I = RD->field_begin(), E = RD->field_end();
637 I != E; ++I) {
638 const FieldDecl *FD = *I;
639
640 if (FD->getIdentifier())
641 return FD;
642
643 if (const RecordType *RT = FD->getType()->getAs<RecordType>()) {
644 if (const FieldDecl *NamedDataMember =
645 FindFirstNamedDataMember(RT->getDecl()))
646 return NamedDataMember;
647 }
648 }
649
650 // We didn't find a named data member.
651 return 0;
652}
653
John McCall1dd73832010-02-04 01:42:13 +0000654void CXXNameMangler::mangleUnqualifiedName(const NamedDecl *ND,
655 DeclarationName Name,
656 unsigned KnownArity) {
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000657 // <unqualified-name> ::= <operator-name>
Mike Stump1eb44332009-09-09 15:08:12 +0000658 // ::= <ctor-dtor-name>
659 // ::= <source-name>
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000660 switch (Name.getNameKind()) {
Anders Carlssonc4355b62009-10-07 01:45:02 +0000661 case DeclarationName::Identifier: {
Anders Carlssonc4355b62009-10-07 01:45:02 +0000662 if (const IdentifierInfo *II = Name.getAsIdentifierInfo()) {
Sean Hunt31455252010-01-24 03:04:27 +0000663 // We must avoid conflicts between internally- and externally-
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000664 // linked variable declaration names in the same TU.
Anders Carlssonaec25232010-02-06 04:52:27 +0000665 // This naming convention is the same as that followed by GCC, though it
666 // shouldn't actually matter.
667 if (ND && isa<VarDecl>(ND) && ND->getLinkage() == InternalLinkage &&
Sean Hunt31455252010-01-24 03:04:27 +0000668 ND->getDeclContext()->isFileContext())
669 Out << 'L';
670
Anders Carlssonc4355b62009-10-07 01:45:02 +0000671 mangleSourceName(II);
672 break;
673 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000674
John McCall1dd73832010-02-04 01:42:13 +0000675 // Otherwise, an anonymous entity. We must have a declaration.
676 assert(ND && "mangling empty name without declaration");
677
678 if (const NamespaceDecl *NS = dyn_cast<NamespaceDecl>(ND)) {
679 if (NS->isAnonymousNamespace()) {
680 // This is how gcc mangles these names.
681 Out << "12_GLOBAL__N_1";
682 break;
683 }
684 }
685
Anders Carlsson6f7e2f42010-06-08 14:49:03 +0000686 if (const VarDecl *VD = dyn_cast<VarDecl>(ND)) {
687 // We must have an anonymous union or struct declaration.
688 const RecordDecl *RD =
689 cast<RecordDecl>(VD->getType()->getAs<RecordType>()->getDecl());
690
691 // Itanium C++ ABI 5.1.2:
692 //
693 // For the purposes of mangling, the name of an anonymous union is
694 // considered to be the name of the first named data member found by a
695 // pre-order, depth-first, declaration-order walk of the data members of
696 // the anonymous union. If there is no such data member (i.e., if all of
697 // the data members in the union are unnamed), then there is no way for
698 // a program to refer to the anonymous union, and there is therefore no
699 // need to mangle its name.
700 const FieldDecl *FD = FindFirstNamedDataMember(RD);
John McCall7121c8f2010-08-05 22:02:13 +0000701
702 // It's actually possible for various reasons for us to get here
703 // with an empty anonymous struct / union. Fortunately, it
704 // doesn't really matter what name we generate.
705 if (!FD) break;
Anders Carlsson6f7e2f42010-06-08 14:49:03 +0000706 assert(FD->getIdentifier() && "Data member name isn't an identifier!");
707
708 mangleSourceName(FD->getIdentifier());
709 break;
710 }
711
Anders Carlssonc4355b62009-10-07 01:45:02 +0000712 // We must have an anonymous struct.
713 const TagDecl *TD = cast<TagDecl>(ND);
714 if (const TypedefDecl *D = TD->getTypedefForAnonDecl()) {
715 assert(TD->getDeclContext() == D->getDeclContext() &&
716 "Typedef should not be in another decl context!");
717 assert(D->getDeclName().getAsIdentifierInfo() &&
718 "Typedef was not named!");
719 mangleSourceName(D->getDeclName().getAsIdentifierInfo());
720 break;
721 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000722
Anders Carlssonc4355b62009-10-07 01:45:02 +0000723 // Get a unique id for the anonymous struct.
724 uint64_t AnonStructId = Context.getAnonymousStructId(TD);
725
726 // Mangle it as a source name in the form
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000727 // [n] $_<id>
Anders Carlssonc4355b62009-10-07 01:45:02 +0000728 // where n is the length of the string.
729 llvm::SmallString<8> Str;
730 Str += "$_";
731 Str += llvm::utostr(AnonStructId);
732
733 Out << Str.size();
734 Out << Str.str();
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000735 break;
Anders Carlssonc4355b62009-10-07 01:45:02 +0000736 }
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000737
738 case DeclarationName::ObjCZeroArgSelector:
739 case DeclarationName::ObjCOneArgSelector:
740 case DeclarationName::ObjCMultiArgSelector:
741 assert(false && "Can't mangle Objective-C selector names here!");
742 break;
743
744 case DeclarationName::CXXConstructorName:
Anders Carlsson27ae5362009-04-17 01:58:57 +0000745 if (ND == Structor)
Mike Stump141c5af2009-09-02 00:25:38 +0000746 // If the named decl is the C++ constructor we're mangling, use the type
747 // we were given.
Anders Carlsson27ae5362009-04-17 01:58:57 +0000748 mangleCXXCtorType(static_cast<CXXCtorType>(StructorType));
Anders Carlsson3ac86b52009-04-15 05:36:58 +0000749 else
750 // Otherwise, use the complete constructor name. This is relevant if a
751 // class with a constructor is declared within a constructor.
752 mangleCXXCtorType(Ctor_Complete);
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000753 break;
754
755 case DeclarationName::CXXDestructorName:
Anders Carlsson27ae5362009-04-17 01:58:57 +0000756 if (ND == Structor)
Mike Stump141c5af2009-09-02 00:25:38 +0000757 // If the named decl is the C++ destructor we're mangling, use the type we
758 // were given.
Anders Carlsson27ae5362009-04-17 01:58:57 +0000759 mangleCXXDtorType(static_cast<CXXDtorType>(StructorType));
760 else
761 // Otherwise, use the complete destructor name. This is relevant if a
762 // class with a destructor is declared within a destructor.
763 mangleCXXDtorType(Dtor_Complete);
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000764 break;
765
766 case DeclarationName::CXXConversionFunctionName:
Mike Stump1eb44332009-09-09 15:08:12 +0000767 // <operator-name> ::= cv <type> # (cast)
Douglas Gregor219cc612009-02-13 01:28:03 +0000768 Out << "cv";
Anders Carlssonb5404912009-10-07 01:06:45 +0000769 mangleType(Context.getASTContext().getCanonicalType(Name.getCXXNameType()));
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000770 break;
771
Anders Carlsson8257d412009-12-22 06:36:32 +0000772 case DeclarationName::CXXOperatorName: {
John McCall1dd73832010-02-04 01:42:13 +0000773 unsigned Arity;
774 if (ND) {
775 Arity = cast<FunctionDecl>(ND)->getNumParams();
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000776
John McCall1dd73832010-02-04 01:42:13 +0000777 // If we have a C++ member function, we need to include the 'this' pointer.
778 // FIXME: This does not make sense for operators that are static, but their
779 // names stay the same regardless of the arity (operator new for instance).
780 if (isa<CXXMethodDecl>(ND))
781 Arity++;
782 } else
783 Arity = KnownArity;
784
Anders Carlsson8257d412009-12-22 06:36:32 +0000785 mangleOperatorName(Name.getCXXOverloadedOperator(), Arity);
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000786 break;
Anders Carlsson8257d412009-12-22 06:36:32 +0000787 }
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000788
Sean Hunt3e518bd2009-11-29 07:34:05 +0000789 case DeclarationName::CXXLiteralOperatorName:
Sean Hunt5dd6b392009-12-04 21:11:13 +0000790 // FIXME: This mangling is not yet official.
Sean Hunt2421f662009-12-04 21:01:37 +0000791 Out << "li";
Sean Hunt3e518bd2009-11-29 07:34:05 +0000792 mangleSourceName(Name.getCXXLiteralIdentifier());
793 break;
794
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000795 case DeclarationName::CXXUsingDirective:
796 assert(false && "Can't mangle a using directive name!");
Douglas Gregor219cc612009-02-13 01:28:03 +0000797 break;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000798 }
799}
800
801void CXXNameMangler::mangleSourceName(const IdentifierInfo *II) {
802 // <source-name> ::= <positive length number> <identifier>
803 // <number> ::= [n] <non-negative decimal integer>
804 // <identifier> ::= <unqualified source code identifier>
805 Out << II->getLength() << II->getName();
806}
807
Eli Friedman7facf842009-12-02 20:32:49 +0000808void CXXNameMangler::mangleNestedName(const NamedDecl *ND,
Fariborz Jahanian57058532010-03-03 19:41:08 +0000809 const DeclContext *DC,
810 bool NoFunction) {
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000811 // <nested-name> ::= N [<CV-qualifiers>] <prefix> <unqualified-name> E
812 // ::= N [<CV-qualifiers>] <template-prefix> <template-args> E
Anders Carlssond99edc42009-09-26 03:55:37 +0000813
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000814 Out << 'N';
815 if (const CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(ND))
John McCall0953e762009-09-24 19:53:00 +0000816 mangleQualifiers(Qualifiers::fromCVRMask(Method->getTypeQualifiers()));
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000817
Anders Carlsson2744a062009-09-18 19:00:18 +0000818 // Check if we have a template.
819 const TemplateArgumentList *TemplateArgs = 0;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000820 if (const TemplateDecl *TD = isTemplate(ND, TemplateArgs)) {
Anders Carlsson2744a062009-09-18 19:00:18 +0000821 mangleTemplatePrefix(TD);
Rafael Espindolad9800722010-03-11 14:07:00 +0000822 TemplateParameterList *TemplateParameters = TD->getTemplateParameters();
823 mangleTemplateArgs(*TemplateParameters, *TemplateArgs);
Fariborz Jahanian57058532010-03-03 19:41:08 +0000824 }
825 else {
826 manglePrefix(DC, NoFunction);
Anders Carlsson7482e242009-09-18 04:29:09 +0000827 mangleUnqualifiedName(ND);
828 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000829
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000830 Out << 'E';
831}
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000832void CXXNameMangler::mangleNestedName(const TemplateDecl *TD,
Anders Carlsson7624f212009-09-18 02:42:01 +0000833 const TemplateArgument *TemplateArgs,
834 unsigned NumTemplateArgs) {
Anders Carlssone45117b2009-09-27 19:53:49 +0000835 // <nested-name> ::= N [<CV-qualifiers>] <template-prefix> <template-args> E
836
Anders Carlsson7624f212009-09-18 02:42:01 +0000837 Out << 'N';
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000838
Anders Carlssone45117b2009-09-27 19:53:49 +0000839 mangleTemplatePrefix(TD);
Rafael Espindolad9800722010-03-11 14:07:00 +0000840 TemplateParameterList *TemplateParameters = TD->getTemplateParameters();
841 mangleTemplateArgs(*TemplateParameters, TemplateArgs, NumTemplateArgs);
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000842
Anders Carlsson7624f212009-09-18 02:42:01 +0000843 Out << 'E';
844}
845
Anders Carlsson1b42c792009-04-02 16:24:45 +0000846void CXXNameMangler::mangleLocalName(const NamedDecl *ND) {
847 // <local-name> := Z <function encoding> E <entity name> [<discriminator>]
848 // := Z <function encoding> E s [<discriminator>]
Mike Stump1eb44332009-09-09 15:08:12 +0000849 // <discriminator> := _ <non-negative number>
Fariborz Jahanian57058532010-03-03 19:41:08 +0000850 const DeclContext *DC = ND->getDeclContext();
Anders Carlsson1b42c792009-04-02 16:24:45 +0000851 Out << 'Z';
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000852
Charles Davis685b1d92010-05-26 18:25:27 +0000853 if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(DC)) {
854 mangleObjCMethodName(MD);
John McCall82b7d7b2010-10-18 21:28:44 +0000855 } else if (const CXXRecordDecl *RD = GetLocalClassDecl(ND)) {
856 mangleFunctionEncoding(cast<FunctionDecl>(RD->getDeclContext()));
Fariborz Jahanian57058532010-03-03 19:41:08 +0000857 Out << 'E';
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000858
John McCall82b7d7b2010-10-18 21:28:44 +0000859 // Mangle the name relative to the closest enclosing function.
860 if (ND == RD) // equality ok because RD derived from ND above
861 mangleUnqualifiedName(ND);
862 else
863 mangleNestedName(ND, DC, true /*NoFunction*/);
864
Fariborz Jahanian4819ac42010-03-04 01:02:03 +0000865 unsigned disc;
John McCall82b7d7b2010-10-18 21:28:44 +0000866 if (Context.getNextDiscriminator(RD, disc)) {
Fariborz Jahanian4819ac42010-03-04 01:02:03 +0000867 if (disc < 10)
868 Out << '_' << disc;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000869 else
Fariborz Jahanian4819ac42010-03-04 01:02:03 +0000870 Out << "__" << disc << '_';
871 }
Fariborz Jahanian57058532010-03-03 19:41:08 +0000872
873 return;
874 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000875 else
Fariborz Jahanian57058532010-03-03 19:41:08 +0000876 mangleFunctionEncoding(cast<FunctionDecl>(DC));
Anders Carlsson7b06f6c2009-12-10 03:14:39 +0000877
Anders Carlsson1b42c792009-04-02 16:24:45 +0000878 Out << 'E';
Eli Friedman6f9f25d2009-12-11 20:21:38 +0000879 mangleUnqualifiedName(ND);
Anders Carlsson1b42c792009-04-02 16:24:45 +0000880}
881
Fariborz Jahanian57058532010-03-03 19:41:08 +0000882void CXXNameMangler::manglePrefix(const DeclContext *DC, bool NoFunction) {
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000883 // <prefix> ::= <prefix> <unqualified-name>
884 // ::= <template-prefix> <template-args>
885 // ::= <template-param>
886 // ::= # empty
887 // ::= <substitution>
Anders Carlsson6862fc72009-09-17 04:16:28 +0000888
Anders Carlssonadd28822009-09-22 20:33:31 +0000889 while (isa<LinkageSpecDecl>(DC))
890 DC = DC->getParent();
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000891
Anders Carlsson9263e912009-09-18 18:39:58 +0000892 if (DC->isTranslationUnit())
893 return;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000894
Douglas Gregor35415f52010-05-25 17:04:15 +0000895 if (const BlockDecl *Block = dyn_cast<BlockDecl>(DC)) {
896 manglePrefix(DC->getParent(), NoFunction);
897 llvm::SmallString<64> Name;
Fariborz Jahanian564360b2010-06-24 00:08:06 +0000898 Context.mangleBlock(GlobalDecl(), Block, Name);
Douglas Gregor35415f52010-05-25 17:04:15 +0000899 Out << Name.size() << Name;
900 return;
901 }
902
Anders Carlsson6862fc72009-09-17 04:16:28 +0000903 if (mangleSubstitution(cast<NamedDecl>(DC)))
904 return;
Anders Carlsson7482e242009-09-18 04:29:09 +0000905
Anders Carlsson2ee3fca2009-09-18 20:11:09 +0000906 // Check if we have a template.
907 const TemplateArgumentList *TemplateArgs = 0;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000908 if (const TemplateDecl *TD = isTemplate(cast<NamedDecl>(DC), TemplateArgs)) {
Anders Carlsson2ee3fca2009-09-18 20:11:09 +0000909 mangleTemplatePrefix(TD);
Rafael Espindolad9800722010-03-11 14:07:00 +0000910 TemplateParameterList *TemplateParameters = TD->getTemplateParameters();
911 mangleTemplateArgs(*TemplateParameters, *TemplateArgs);
Fariborz Jahanian57058532010-03-03 19:41:08 +0000912 }
Douglas Gregor35415f52010-05-25 17:04:15 +0000913 else if(NoFunction && (isa<FunctionDecl>(DC) || isa<ObjCMethodDecl>(DC)))
Fariborz Jahanian57058532010-03-03 19:41:08 +0000914 return;
Douglas Gregor35415f52010-05-25 17:04:15 +0000915 else if (const ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(DC))
916 mangleObjCMethodName(Method);
Fariborz Jahanian57058532010-03-03 19:41:08 +0000917 else {
918 manglePrefix(DC->getParent(), NoFunction);
Anders Carlsson2ee3fca2009-09-18 20:11:09 +0000919 mangleUnqualifiedName(cast<NamedDecl>(DC));
920 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000921
Anders Carlsson6862fc72009-09-17 04:16:28 +0000922 addSubstitution(cast<NamedDecl>(DC));
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000923}
924
Douglas Gregor20f0cc72010-04-23 03:10:43 +0000925void CXXNameMangler::mangleTemplatePrefix(TemplateName Template) {
926 // <template-prefix> ::= <prefix> <template unqualified-name>
927 // ::= <template-param>
928 // ::= <substitution>
929 if (TemplateDecl *TD = Template.getAsTemplateDecl())
930 return mangleTemplatePrefix(TD);
931
932 if (QualifiedTemplateName *Qualified = Template.getAsQualifiedTemplateName())
933 mangleUnresolvedScope(Qualified->getQualifier());
Sean Huntc3021132010-05-05 15:23:54 +0000934
Douglas Gregor20f0cc72010-04-23 03:10:43 +0000935 if (OverloadedTemplateStorage *Overloaded
936 = Template.getAsOverloadedTemplate()) {
Sean Huntc3021132010-05-05 15:23:54 +0000937 mangleUnqualifiedName(0, (*Overloaded->begin())->getDeclName(),
Douglas Gregor20f0cc72010-04-23 03:10:43 +0000938 UnknownArity);
939 return;
940 }
Sean Huntc3021132010-05-05 15:23:54 +0000941
Douglas Gregor20f0cc72010-04-23 03:10:43 +0000942 DependentTemplateName *Dependent = Template.getAsDependentTemplateName();
943 assert(Dependent && "Unknown template name kind?");
944 mangleUnresolvedScope(Dependent->getQualifier());
Douglas Gregor1e9268e2010-04-28 05:58:56 +0000945 mangleUnscopedTemplateName(Template);
Douglas Gregor20f0cc72010-04-23 03:10:43 +0000946}
947
Anders Carlsson0fa6df42009-09-26 19:45:45 +0000948void CXXNameMangler::mangleTemplatePrefix(const TemplateDecl *ND) {
Anders Carlsson7482e242009-09-18 04:29:09 +0000949 // <template-prefix> ::= <prefix> <template unqualified-name>
950 // ::= <template-param>
951 // ::= <substitution>
Douglas Gregor32fb4e12010-02-05 20:45:00 +0000952 // <template-template-param> ::= <template-param>
953 // <substitution>
Anders Carlsson7482e242009-09-18 04:29:09 +0000954
Anders Carlssonaeb85372009-09-26 22:18:22 +0000955 if (mangleSubstitution(ND))
956 return;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000957
Douglas Gregor32fb4e12010-02-05 20:45:00 +0000958 // <template-template-param> ::= <template-param>
959 if (const TemplateTemplateParmDecl *TTP
960 = dyn_cast<TemplateTemplateParmDecl>(ND)) {
961 mangleTemplateParameter(TTP->getIndex());
962 return;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000963 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000964
Anders Carlssonaa73ab12009-09-18 18:47:07 +0000965 manglePrefix(ND->getDeclContext());
Anders Carlsson1668f202009-09-26 20:13:56 +0000966 mangleUnqualifiedName(ND->getTemplatedDecl());
Anders Carlssonaeb85372009-09-26 22:18:22 +0000967 addSubstitution(ND);
Anders Carlsson7482e242009-09-18 04:29:09 +0000968}
969
John McCallb6f532e2010-07-14 06:43:17 +0000970/// Mangles a template name under the production <type>. Required for
971/// template template arguments.
972/// <type> ::= <class-enum-type>
973/// ::= <template-param>
974/// ::= <substitution>
975void CXXNameMangler::mangleType(TemplateName TN) {
976 if (mangleSubstitution(TN))
977 return;
978
979 TemplateDecl *TD = 0;
980
981 switch (TN.getKind()) {
982 case TemplateName::QualifiedTemplate:
983 TD = TN.getAsQualifiedTemplateName()->getTemplateDecl();
984 goto HaveDecl;
985
986 case TemplateName::Template:
987 TD = TN.getAsTemplateDecl();
988 goto HaveDecl;
989
990 HaveDecl:
991 if (isa<TemplateTemplateParmDecl>(TD))
992 mangleTemplateParameter(cast<TemplateTemplateParmDecl>(TD)->getIndex());
993 else
994 mangleName(TD);
995 break;
996
997 case TemplateName::OverloadedTemplate:
998 llvm_unreachable("can't mangle an overloaded template name as a <type>");
999 break;
1000
1001 case TemplateName::DependentTemplate: {
1002 const DependentTemplateName *Dependent = TN.getAsDependentTemplateName();
1003 assert(Dependent->isIdentifier());
1004
1005 // <class-enum-type> ::= <name>
1006 // <name> ::= <nested-name>
1007 mangleUnresolvedScope(Dependent->getQualifier());
1008 mangleSourceName(Dependent->getIdentifier());
1009 break;
1010 }
1011
1012 }
1013
1014 addSubstitution(TN);
1015}
1016
Mike Stump1eb44332009-09-09 15:08:12 +00001017void
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001018CXXNameMangler::mangleOperatorName(OverloadedOperatorKind OO, unsigned Arity) {
1019 switch (OO) {
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001020 // <operator-name> ::= nw # new
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001021 case OO_New: Out << "nw"; break;
1022 // ::= na # new[]
1023 case OO_Array_New: Out << "na"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001024 // ::= dl # delete
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001025 case OO_Delete: Out << "dl"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001026 // ::= da # delete[]
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001027 case OO_Array_Delete: Out << "da"; break;
1028 // ::= ps # + (unary)
John McCall5e1e89b2010-08-18 19:18:59 +00001029 // ::= pl # + (binary or unknown)
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001030 case OO_Plus:
Anders Carlsson8257d412009-12-22 06:36:32 +00001031 Out << (Arity == 1? "ps" : "pl"); break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001032 // ::= ng # - (unary)
John McCall5e1e89b2010-08-18 19:18:59 +00001033 // ::= mi # - (binary or unknown)
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001034 case OO_Minus:
Anders Carlsson8257d412009-12-22 06:36:32 +00001035 Out << (Arity == 1? "ng" : "mi"); break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001036 // ::= ad # & (unary)
John McCall5e1e89b2010-08-18 19:18:59 +00001037 // ::= an # & (binary or unknown)
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001038 case OO_Amp:
Anders Carlsson8257d412009-12-22 06:36:32 +00001039 Out << (Arity == 1? "ad" : "an"); break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001040 // ::= de # * (unary)
John McCall5e1e89b2010-08-18 19:18:59 +00001041 // ::= ml # * (binary or unknown)
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001042 case OO_Star:
John McCall5e1e89b2010-08-18 19:18:59 +00001043 // Use binary when unknown.
Anders Carlsson8257d412009-12-22 06:36:32 +00001044 Out << (Arity == 1? "de" : "ml"); break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001045 // ::= co # ~
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001046 case OO_Tilde: Out << "co"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001047 // ::= dv # /
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001048 case OO_Slash: Out << "dv"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001049 // ::= rm # %
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001050 case OO_Percent: Out << "rm"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001051 // ::= or # |
1052 case OO_Pipe: Out << "or"; break;
1053 // ::= eo # ^
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001054 case OO_Caret: Out << "eo"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001055 // ::= aS # =
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001056 case OO_Equal: Out << "aS"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001057 // ::= pL # +=
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001058 case OO_PlusEqual: Out << "pL"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001059 // ::= mI # -=
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001060 case OO_MinusEqual: Out << "mI"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001061 // ::= mL # *=
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001062 case OO_StarEqual: Out << "mL"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001063 // ::= dV # /=
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001064 case OO_SlashEqual: Out << "dV"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001065 // ::= rM # %=
1066 case OO_PercentEqual: Out << "rM"; break;
1067 // ::= aN # &=
1068 case OO_AmpEqual: Out << "aN"; break;
1069 // ::= oR # |=
1070 case OO_PipeEqual: Out << "oR"; break;
1071 // ::= eO # ^=
1072 case OO_CaretEqual: Out << "eO"; break;
1073 // ::= ls # <<
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001074 case OO_LessLess: Out << "ls"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001075 // ::= rs # >>
1076 case OO_GreaterGreater: Out << "rs"; break;
1077 // ::= lS # <<=
1078 case OO_LessLessEqual: Out << "lS"; break;
1079 // ::= rS # >>=
1080 case OO_GreaterGreaterEqual: Out << "rS"; break;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001081 // ::= eq # ==
1082 case OO_EqualEqual: Out << "eq"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001083 // ::= ne # !=
1084 case OO_ExclaimEqual: Out << "ne"; break;
1085 // ::= lt # <
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001086 case OO_Less: Out << "lt"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001087 // ::= gt # >
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001088 case OO_Greater: Out << "gt"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001089 // ::= le # <=
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001090 case OO_LessEqual: Out << "le"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001091 // ::= ge # >=
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001092 case OO_GreaterEqual: Out << "ge"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001093 // ::= nt # !
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001094 case OO_Exclaim: Out << "nt"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001095 // ::= aa # &&
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001096 case OO_AmpAmp: Out << "aa"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001097 // ::= oo # ||
1098 case OO_PipePipe: Out << "oo"; break;
1099 // ::= pp # ++
1100 case OO_PlusPlus: Out << "pp"; break;
1101 // ::= mm # --
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001102 case OO_MinusMinus: Out << "mm"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001103 // ::= cm # ,
1104 case OO_Comma: Out << "cm"; break;
1105 // ::= pm # ->*
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001106 case OO_ArrowStar: Out << "pm"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001107 // ::= pt # ->
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001108 case OO_Arrow: Out << "pt"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001109 // ::= cl # ()
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001110 case OO_Call: Out << "cl"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001111 // ::= ix # []
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001112 case OO_Subscript: Out << "ix"; break;
Anders Carlssone170ba72009-12-14 01:45:37 +00001113
1114 // ::= qu # ?
1115 // The conditional operator can't be overloaded, but we still handle it when
1116 // mangling expressions.
1117 case OO_Conditional: Out << "qu"; break;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001118
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001119 case OO_None:
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001120 case NUM_OVERLOADED_OPERATORS:
Mike Stump1eb44332009-09-09 15:08:12 +00001121 assert(false && "Not an overloaded operator");
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001122 break;
1123 }
1124}
1125
John McCall0953e762009-09-24 19:53:00 +00001126void CXXNameMangler::mangleQualifiers(Qualifiers Quals) {
Mike Stump1eb44332009-09-09 15:08:12 +00001127 // <CV-qualifiers> ::= [r] [V] [K] # restrict (C99), volatile, const
John McCall0953e762009-09-24 19:53:00 +00001128 if (Quals.hasRestrict())
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001129 Out << 'r';
John McCall0953e762009-09-24 19:53:00 +00001130 if (Quals.hasVolatile())
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001131 Out << 'V';
John McCall0953e762009-09-24 19:53:00 +00001132 if (Quals.hasConst())
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001133 Out << 'K';
John McCall0953e762009-09-24 19:53:00 +00001134
Douglas Gregor56079f72010-06-14 23:15:08 +00001135 if (Quals.hasAddressSpace()) {
1136 // Extension:
1137 //
1138 // <type> ::= U <address-space-number>
1139 //
1140 // where <address-space-number> is a source name consisting of 'AS'
1141 // followed by the address space <number>.
1142 llvm::SmallString<64> ASString;
1143 ASString = "AS" + llvm::utostr_32(Quals.getAddressSpace());
1144 Out << 'U' << ASString.size() << ASString;
1145 }
1146
John McCall0953e762009-09-24 19:53:00 +00001147 // FIXME: For now, just drop all extension qualifiers on the floor.
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001148}
1149
Anders Carlsson7b06f6c2009-12-10 03:14:39 +00001150void CXXNameMangler::mangleObjCMethodName(const ObjCMethodDecl *MD) {
Charles Davis685b1d92010-05-26 18:25:27 +00001151 llvm::SmallString<64> Buffer;
1152 MiscNameMangler(Context, Buffer).mangleObjCMethodName(MD);
1153 Out << Buffer;
Anders Carlsson7b06f6c2009-12-10 03:14:39 +00001154}
1155
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001156void CXXNameMangler::mangleType(QualType T) {
Anders Carlsson4843e582009-03-10 17:07:44 +00001157 // Only operate on the canonical type!
Anders Carlssonb5404912009-10-07 01:06:45 +00001158 T = Context.getASTContext().getCanonicalType(T);
Anders Carlsson4843e582009-03-10 17:07:44 +00001159
Douglas Gregora4923eb2009-11-16 21:35:15 +00001160 bool IsSubstitutable = T.hasLocalQualifiers() || !isa<BuiltinType>(T);
Anders Carlsson76967372009-09-17 00:43:46 +00001161 if (IsSubstitutable && mangleSubstitution(T))
1162 return;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001163
Douglas Gregora4923eb2009-11-16 21:35:15 +00001164 if (Qualifiers Quals = T.getLocalQualifiers()) {
John McCall0953e762009-09-24 19:53:00 +00001165 mangleQualifiers(Quals);
1166 // Recurse: even if the qualified type isn't yet substitutable,
1167 // the unqualified type might be.
Douglas Gregora4923eb2009-11-16 21:35:15 +00001168 mangleType(T.getLocalUnqualifiedType());
Anders Carlsson76967372009-09-17 00:43:46 +00001169 } else {
1170 switch (T->getTypeClass()) {
John McCallefe6aee2009-09-05 07:56:18 +00001171#define ABSTRACT_TYPE(CLASS, PARENT)
1172#define NON_CANONICAL_TYPE(CLASS, PARENT) \
Anders Carlsson76967372009-09-17 00:43:46 +00001173 case Type::CLASS: \
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +00001174 llvm_unreachable("can't mangle non-canonical type " #CLASS "Type"); \
Anders Carlsson76967372009-09-17 00:43:46 +00001175 return;
John McCallefe6aee2009-09-05 07:56:18 +00001176#define TYPE(CLASS, PARENT) \
Anders Carlsson76967372009-09-17 00:43:46 +00001177 case Type::CLASS: \
John McCall0953e762009-09-24 19:53:00 +00001178 mangleType(static_cast<const CLASS##Type*>(T.getTypePtr())); \
Anders Carlsson76967372009-09-17 00:43:46 +00001179 break;
John McCallefe6aee2009-09-05 07:56:18 +00001180#include "clang/AST/TypeNodes.def"
Anders Carlsson76967372009-09-17 00:43:46 +00001181 }
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001182 }
Anders Carlsson76967372009-09-17 00:43:46 +00001183
1184 // Add the substitution.
1185 if (IsSubstitutable)
1186 addSubstitution(T);
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001187}
1188
Douglas Gregor1b12a3b2010-05-26 05:11:13 +00001189void CXXNameMangler::mangleNameOrStandardSubstitution(const NamedDecl *ND) {
1190 if (!mangleStandardSubstitution(ND))
1191 mangleName(ND);
1192}
1193
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001194void CXXNameMangler::mangleType(const BuiltinType *T) {
John McCallefe6aee2009-09-05 07:56:18 +00001195 // <type> ::= <builtin-type>
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001196 // <builtin-type> ::= v # void
1197 // ::= w # wchar_t
1198 // ::= b # bool
1199 // ::= c # char
1200 // ::= a # signed char
1201 // ::= h # unsigned char
1202 // ::= s # short
1203 // ::= t # unsigned short
1204 // ::= i # int
1205 // ::= j # unsigned int
1206 // ::= l # long
1207 // ::= m # unsigned long
1208 // ::= x # long long, __int64
1209 // ::= y # unsigned long long, __int64
1210 // ::= n # __int128
1211 // UNSUPPORTED: ::= o # unsigned __int128
1212 // ::= f # float
1213 // ::= d # double
1214 // ::= e # long double, __float80
1215 // UNSUPPORTED: ::= g # __float128
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001216 // UNSUPPORTED: ::= Dd # IEEE 754r decimal floating point (64 bits)
1217 // UNSUPPORTED: ::= De # IEEE 754r decimal floating point (128 bits)
1218 // UNSUPPORTED: ::= Df # IEEE 754r decimal floating point (32 bits)
1219 // UNSUPPORTED: ::= Dh # IEEE 754r half-precision floating point (16 bits)
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001220 // ::= Di # char32_t
1221 // ::= Ds # char16_t
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001222 // ::= u <source-name> # vendor extended type
Sebastian Redl6e8ed162009-05-10 18:38:11 +00001223 // From our point of view, std::nullptr_t is a builtin, but as far as mangling
1224 // is concerned, it's a type called std::nullptr_t.
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001225 switch (T->getKind()) {
1226 case BuiltinType::Void: Out << 'v'; break;
1227 case BuiltinType::Bool: Out << 'b'; break;
1228 case BuiltinType::Char_U: case BuiltinType::Char_S: Out << 'c'; break;
1229 case BuiltinType::UChar: Out << 'h'; break;
1230 case BuiltinType::UShort: Out << 't'; break;
1231 case BuiltinType::UInt: Out << 'j'; break;
1232 case BuiltinType::ULong: Out << 'm'; break;
1233 case BuiltinType::ULongLong: Out << 'y'; break;
Chris Lattner2df9ced2009-04-30 02:43:43 +00001234 case BuiltinType::UInt128: Out << 'o'; break;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001235 case BuiltinType::SChar: Out << 'a'; break;
1236 case BuiltinType::WChar: Out << 'w'; break;
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001237 case BuiltinType::Char16: Out << "Ds"; break;
1238 case BuiltinType::Char32: Out << "Di"; break;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001239 case BuiltinType::Short: Out << 's'; break;
1240 case BuiltinType::Int: Out << 'i'; break;
1241 case BuiltinType::Long: Out << 'l'; break;
1242 case BuiltinType::LongLong: Out << 'x'; break;
Chris Lattner2df9ced2009-04-30 02:43:43 +00001243 case BuiltinType::Int128: Out << 'n'; break;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001244 case BuiltinType::Float: Out << 'f'; break;
1245 case BuiltinType::Double: Out << 'd'; break;
1246 case BuiltinType::LongDouble: Out << 'e'; break;
Sebastian Redl6e8ed162009-05-10 18:38:11 +00001247 case BuiltinType::NullPtr: Out << "St9nullptr_t"; break;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001248
1249 case BuiltinType::Overload:
1250 case BuiltinType::Dependent:
Mike Stump1eb44332009-09-09 15:08:12 +00001251 assert(false &&
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001252 "Overloaded and dependent types shouldn't get to name mangling");
1253 break;
Anders Carlssone89d1592009-06-26 18:41:36 +00001254 case BuiltinType::UndeducedAuto:
1255 assert(0 && "Should not see undeduced auto here");
1256 break;
Steve Naroff9533a7f2009-07-22 17:14:51 +00001257 case BuiltinType::ObjCId: Out << "11objc_object"; break;
1258 case BuiltinType::ObjCClass: Out << "10objc_class"; break;
Fariborz Jahanian13dcd002009-11-21 19:53:08 +00001259 case BuiltinType::ObjCSel: Out << "13objc_selector"; break;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001260 }
1261}
1262
John McCallefe6aee2009-09-05 07:56:18 +00001263// <type> ::= <function-type>
1264// <function-type> ::= F [Y] <bare-function-type> E
1265void CXXNameMangler::mangleType(const FunctionProtoType *T) {
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001266 Out << 'F';
Mike Stumpf5408fe2009-05-16 07:57:57 +00001267 // FIXME: We don't have enough information in the AST to produce the 'Y'
1268 // encoding for extern "C" function types.
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001269 mangleBareFunctionType(T, /*MangleReturnType=*/true);
1270 Out << 'E';
1271}
John McCallefe6aee2009-09-05 07:56:18 +00001272void CXXNameMangler::mangleType(const FunctionNoProtoType *T) {
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +00001273 llvm_unreachable("Can't mangle K&R function prototypes");
John McCallefe6aee2009-09-05 07:56:18 +00001274}
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001275void CXXNameMangler::mangleBareFunctionType(const FunctionType *T,
1276 bool MangleReturnType) {
John McCallefe6aee2009-09-05 07:56:18 +00001277 // We should never be mangling something without a prototype.
1278 const FunctionProtoType *Proto = cast<FunctionProtoType>(T);
1279
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001280 // <bare-function-type> ::= <signature type>+
1281 if (MangleReturnType)
John McCallefe6aee2009-09-05 07:56:18 +00001282 mangleType(Proto->getResultType());
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001283
Anders Carlsson93296682010-06-02 04:40:13 +00001284 if (Proto->getNumArgs() == 0 && !Proto->isVariadic()) {
Eli Friedmana7e68452010-08-22 01:00:03 +00001285 // <builtin-type> ::= v # void
Anders Carlssonc6c91bc2009-04-01 00:15:23 +00001286 Out << 'v';
1287 return;
1288 }
Mike Stump1eb44332009-09-09 15:08:12 +00001289
Douglas Gregor72564e72009-02-26 23:50:07 +00001290 for (FunctionProtoType::arg_type_iterator Arg = Proto->arg_type_begin(),
Mike Stump1eb44332009-09-09 15:08:12 +00001291 ArgEnd = Proto->arg_type_end();
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001292 Arg != ArgEnd; ++Arg)
1293 mangleType(*Arg);
Douglas Gregor219cc612009-02-13 01:28:03 +00001294
1295 // <builtin-type> ::= z # ellipsis
1296 if (Proto->isVariadic())
1297 Out << 'z';
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001298}
1299
John McCallefe6aee2009-09-05 07:56:18 +00001300// <type> ::= <class-enum-type>
Mike Stump1eb44332009-09-09 15:08:12 +00001301// <class-enum-type> ::= <name>
John McCalled976492009-12-04 22:46:56 +00001302void CXXNameMangler::mangleType(const UnresolvedUsingType *T) {
1303 mangleName(T->getDecl());
1304}
1305
1306// <type> ::= <class-enum-type>
1307// <class-enum-type> ::= <name>
John McCallefe6aee2009-09-05 07:56:18 +00001308void CXXNameMangler::mangleType(const EnumType *T) {
1309 mangleType(static_cast<const TagType*>(T));
1310}
1311void CXXNameMangler::mangleType(const RecordType *T) {
1312 mangleType(static_cast<const TagType*>(T));
1313}
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001314void CXXNameMangler::mangleType(const TagType *T) {
Eli Friedmanecb7e932009-12-11 18:00:57 +00001315 mangleName(T->getDecl());
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001316}
1317
John McCallefe6aee2009-09-05 07:56:18 +00001318// <type> ::= <array-type>
1319// <array-type> ::= A <positive dimension number> _ <element type>
1320// ::= A [<dimension expression>] _ <element type>
1321void CXXNameMangler::mangleType(const ConstantArrayType *T) {
1322 Out << 'A' << T->getSize() << '_';
1323 mangleType(T->getElementType());
1324}
1325void CXXNameMangler::mangleType(const VariableArrayType *T) {
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001326 Out << 'A';
John McCallefe6aee2009-09-05 07:56:18 +00001327 mangleExpression(T->getSizeExpr());
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001328 Out << '_';
1329 mangleType(T->getElementType());
1330}
John McCallefe6aee2009-09-05 07:56:18 +00001331void CXXNameMangler::mangleType(const DependentSizedArrayType *T) {
1332 Out << 'A';
1333 mangleExpression(T->getSizeExpr());
1334 Out << '_';
1335 mangleType(T->getElementType());
1336}
1337void CXXNameMangler::mangleType(const IncompleteArrayType *T) {
Nick Lewycky271b6652010-09-05 03:40:33 +00001338 Out << "A_";
John McCallefe6aee2009-09-05 07:56:18 +00001339 mangleType(T->getElementType());
1340}
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001341
John McCallefe6aee2009-09-05 07:56:18 +00001342// <type> ::= <pointer-to-member-type>
1343// <pointer-to-member-type> ::= M <class type> <member type>
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001344void CXXNameMangler::mangleType(const MemberPointerType *T) {
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001345 Out << 'M';
1346 mangleType(QualType(T->getClass(), 0));
Anders Carlsson0e650012009-05-17 17:41:20 +00001347 QualType PointeeType = T->getPointeeType();
1348 if (const FunctionProtoType *FPT = dyn_cast<FunctionProtoType>(PointeeType)) {
John McCall0953e762009-09-24 19:53:00 +00001349 mangleQualifiers(Qualifiers::fromCVRMask(FPT->getTypeQuals()));
Anders Carlsson0e650012009-05-17 17:41:20 +00001350 mangleType(FPT);
Anders Carlsson9d85b722010-06-02 04:29:50 +00001351
1352 // Itanium C++ ABI 5.1.8:
1353 //
1354 // The type of a non-static member function is considered to be different,
1355 // for the purposes of substitution, from the type of a namespace-scope or
1356 // static member function whose type appears similar. The types of two
1357 // non-static member functions are considered to be different, for the
1358 // purposes of substitution, if the functions are members of different
1359 // classes. In other words, for the purposes of substitution, the class of
1360 // which the function is a member is considered part of the type of
1361 // function.
1362
1363 // We increment the SeqID here to emulate adding an entry to the
1364 // substitution table. We can't actually add it because we don't want this
1365 // particular function type to be substituted.
1366 ++SeqID;
Mike Stump1eb44332009-09-09 15:08:12 +00001367 } else
Anders Carlsson0e650012009-05-17 17:41:20 +00001368 mangleType(PointeeType);
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001369}
1370
John McCallefe6aee2009-09-05 07:56:18 +00001371// <type> ::= <template-param>
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001372void CXXNameMangler::mangleType(const TemplateTypeParmType *T) {
Anders Carlsson0ccdf8d2009-09-27 00:38:53 +00001373 mangleTemplateParameter(T->getIndex());
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001374}
1375
John McCallefe6aee2009-09-05 07:56:18 +00001376// <type> ::= P <type> # pointer-to
1377void CXXNameMangler::mangleType(const PointerType *T) {
1378 Out << 'P';
1379 mangleType(T->getPointeeType());
1380}
1381void CXXNameMangler::mangleType(const ObjCObjectPointerType *T) {
1382 Out << 'P';
1383 mangleType(T->getPointeeType());
1384}
1385
1386// <type> ::= R <type> # reference-to
1387void CXXNameMangler::mangleType(const LValueReferenceType *T) {
1388 Out << 'R';
1389 mangleType(T->getPointeeType());
1390}
1391
1392// <type> ::= O <type> # rvalue reference-to (C++0x)
1393void CXXNameMangler::mangleType(const RValueReferenceType *T) {
1394 Out << 'O';
1395 mangleType(T->getPointeeType());
1396}
1397
1398// <type> ::= C <type> # complex pair (C 2000)
1399void CXXNameMangler::mangleType(const ComplexType *T) {
1400 Out << 'C';
1401 mangleType(T->getElementType());
1402}
1403
1404// GNU extension: vector types
Chris Lattner788b0fd2010-06-23 06:00:24 +00001405// <type> ::= <vector-type>
1406// <vector-type> ::= Dv <positive dimension number> _
1407// <extended element type>
1408// ::= Dv [<dimension expression>] _ <element type>
1409// <extended element type> ::= <element type>
1410// ::= p # AltiVec vector pixel
John McCallefe6aee2009-09-05 07:56:18 +00001411void CXXNameMangler::mangleType(const VectorType *T) {
Nick Lewycky0e5f0672010-03-26 07:18:04 +00001412 Out << "Dv" << T->getNumElements() << '_';
Chris Lattner788b0fd2010-06-23 06:00:24 +00001413 if (T->getAltiVecSpecific() == VectorType::Pixel)
1414 Out << 'p';
1415 else if (T->getAltiVecSpecific() == VectorType::Bool)
1416 Out << 'b';
1417 else
1418 mangleType(T->getElementType());
John McCallefe6aee2009-09-05 07:56:18 +00001419}
1420void CXXNameMangler::mangleType(const ExtVectorType *T) {
1421 mangleType(static_cast<const VectorType*>(T));
1422}
1423void CXXNameMangler::mangleType(const DependentSizedExtVectorType *T) {
Nick Lewycky0e5f0672010-03-26 07:18:04 +00001424 Out << "Dv";
1425 mangleExpression(T->getSizeExpr());
1426 Out << '_';
John McCallefe6aee2009-09-05 07:56:18 +00001427 mangleType(T->getElementType());
1428}
1429
Anders Carlssona40c5e42009-03-07 22:03:21 +00001430void CXXNameMangler::mangleType(const ObjCInterfaceType *T) {
1431 mangleSourceName(T->getDecl()->getIdentifier());
1432}
1433
John McCallc12c5bb2010-05-15 11:32:37 +00001434void CXXNameMangler::mangleType(const ObjCObjectType *T) {
John McCallc00c1f62010-05-15 17:06:29 +00001435 // We don't allow overloading by different protocol qualification,
1436 // so mangling them isn't necessary.
John McCallc12c5bb2010-05-15 11:32:37 +00001437 mangleType(T->getBaseType());
1438}
1439
John McCallefe6aee2009-09-05 07:56:18 +00001440void CXXNameMangler::mangleType(const BlockPointerType *T) {
Anders Carlssonf28c6872009-12-23 22:31:44 +00001441 Out << "U13block_pointer";
1442 mangleType(T->getPointeeType());
John McCallefe6aee2009-09-05 07:56:18 +00001443}
1444
John McCall31f17ec2010-04-27 00:57:59 +00001445void CXXNameMangler::mangleType(const InjectedClassNameType *T) {
1446 // Mangle injected class name types as if the user had written the
1447 // specialization out fully. It may not actually be possible to see
1448 // this mangling, though.
1449 mangleType(T->getInjectedSpecializationType());
1450}
1451
John McCallefe6aee2009-09-05 07:56:18 +00001452void CXXNameMangler::mangleType(const TemplateSpecializationType *T) {
Douglas Gregor1e9268e2010-04-28 05:58:56 +00001453 if (TemplateDecl *TD = T->getTemplateName().getAsTemplateDecl()) {
1454 mangleName(TD, T->getArgs(), T->getNumArgs());
1455 } else {
1456 if (mangleSubstitution(QualType(T, 0)))
1457 return;
Sean Huntc3021132010-05-05 15:23:54 +00001458
Douglas Gregor1e9268e2010-04-28 05:58:56 +00001459 mangleTemplatePrefix(T->getTemplateName());
Sean Huntc3021132010-05-05 15:23:54 +00001460
Douglas Gregor1e9268e2010-04-28 05:58:56 +00001461 // FIXME: GCC does not appear to mangle the template arguments when
1462 // the template in question is a dependent template name. Should we
1463 // emulate that badness?
1464 mangleTemplateArgs(T->getTemplateName(), T->getArgs(), T->getNumArgs());
1465 addSubstitution(QualType(T, 0));
1466 }
John McCallefe6aee2009-09-05 07:56:18 +00001467}
1468
Douglas Gregor4714c122010-03-31 17:34:00 +00001469void CXXNameMangler::mangleType(const DependentNameType *T) {
Anders Carlssonae352482009-09-26 02:26:02 +00001470 // Typename types are always nested
1471 Out << 'N';
John McCall33500952010-06-11 00:33:02 +00001472 mangleUnresolvedScope(T->getQualifier());
1473 mangleSourceName(T->getIdentifier());
1474 Out << 'E';
1475}
John McCall6ab30e02010-06-09 07:26:17 +00001476
John McCall33500952010-06-11 00:33:02 +00001477void CXXNameMangler::mangleType(const DependentTemplateSpecializationType *T) {
1478 // Dependently-scoped template types are always nested
1479 Out << 'N';
1480
1481 // TODO: avoid making this TemplateName.
1482 TemplateName Prefix =
1483 getASTContext().getDependentTemplateName(T->getQualifier(),
1484 T->getIdentifier());
1485 mangleTemplatePrefix(Prefix);
1486
1487 // FIXME: GCC does not appear to mangle the template arguments when
1488 // the template in question is a dependent template name. Should we
1489 // emulate that badness?
1490 mangleTemplateArgs(Prefix, T->getArgs(), T->getNumArgs());
Anders Carlssonae352482009-09-26 02:26:02 +00001491 Out << 'E';
John McCallefe6aee2009-09-05 07:56:18 +00001492}
1493
John McCallad5e7382010-03-01 23:49:17 +00001494void CXXNameMangler::mangleType(const TypeOfType *T) {
1495 // FIXME: this is pretty unsatisfactory, but there isn't an obvious
1496 // "extension with parameters" mangling.
1497 Out << "u6typeof";
1498}
1499
1500void CXXNameMangler::mangleType(const TypeOfExprType *T) {
1501 // FIXME: this is pretty unsatisfactory, but there isn't an obvious
1502 // "extension with parameters" mangling.
1503 Out << "u6typeof";
1504}
1505
1506void CXXNameMangler::mangleType(const DecltypeType *T) {
1507 Expr *E = T->getUnderlyingExpr();
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001508
John McCallad5e7382010-03-01 23:49:17 +00001509 // type ::= Dt <expression> E # decltype of an id-expression
1510 // # or class member access
1511 // ::= DT <expression> E # decltype of an expression
1512
1513 // This purports to be an exhaustive list of id-expressions and
1514 // class member accesses. Note that we do not ignore parentheses;
1515 // parentheses change the semantics of decltype for these
1516 // expressions (and cause the mangler to use the other form).
1517 if (isa<DeclRefExpr>(E) ||
1518 isa<MemberExpr>(E) ||
1519 isa<UnresolvedLookupExpr>(E) ||
1520 isa<DependentScopeDeclRefExpr>(E) ||
1521 isa<CXXDependentScopeMemberExpr>(E) ||
1522 isa<UnresolvedMemberExpr>(E))
1523 Out << "Dt";
1524 else
1525 Out << "DT";
1526 mangleExpression(E);
1527 Out << 'E';
1528}
1529
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001530void CXXNameMangler::mangleIntegerLiteral(QualType T,
Anders Carlssone170ba72009-12-14 01:45:37 +00001531 const llvm::APSInt &Value) {
1532 // <expr-primary> ::= L <type> <value number> E # integer literal
1533 Out << 'L';
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001534
Anders Carlssone170ba72009-12-14 01:45:37 +00001535 mangleType(T);
1536 if (T->isBooleanType()) {
1537 // Boolean values are encoded as 0/1.
1538 Out << (Value.getBoolValue() ? '1' : '0');
1539 } else {
John McCall0512e482010-07-14 04:20:34 +00001540 mangleNumber(Value);
Anders Carlssone170ba72009-12-14 01:45:37 +00001541 }
1542 Out << 'E';
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001543
Anders Carlssone170ba72009-12-14 01:45:37 +00001544}
1545
John McCall2f27bf82010-02-04 02:56:29 +00001546/// Mangles a member expression. Implicit accesses are not handled,
1547/// but that should be okay, because you shouldn't be able to
1548/// make an implicit access in a function template declaration.
John McCall2f27bf82010-02-04 02:56:29 +00001549void CXXNameMangler::mangleMemberExpr(const Expr *Base,
1550 bool IsArrow,
1551 NestedNameSpecifier *Qualifier,
1552 DeclarationName Member,
1553 unsigned Arity) {
John McCalle1e342f2010-03-01 19:12:25 +00001554 // gcc-4.4 uses 'dt' for dot expressions, which is reasonable.
1555 // OTOH, gcc also mangles the name as an expression.
1556 Out << (IsArrow ? "pt" : "dt");
John McCall2f27bf82010-02-04 02:56:29 +00001557 mangleExpression(Base);
1558 mangleUnresolvedName(Qualifier, Member, Arity);
1559}
1560
John McCall5e1e89b2010-08-18 19:18:59 +00001561void CXXNameMangler::mangleExpression(const Expr *E, unsigned Arity) {
Anders Carlssond553f8c2009-09-21 01:21:10 +00001562 // <expression> ::= <unary operator-name> <expression>
John McCall09cc1412010-02-03 00:55:45 +00001563 // ::= <binary operator-name> <expression> <expression>
1564 // ::= <trinary operator-name> <expression> <expression> <expression>
Eli Friedmana7e68452010-08-22 01:00:03 +00001565 // ::= cl <expression>* E # call
Anders Carlssond553f8c2009-09-21 01:21:10 +00001566 // ::= cv <type> expression # conversion with one argument
1567 // ::= cv <type> _ <expression>* E # conversion with a different number of arguments
Eli Friedmana7e68452010-08-22 01:00:03 +00001568 // ::= st <type> # sizeof (a type)
Anders Carlssond553f8c2009-09-21 01:21:10 +00001569 // ::= at <type> # alignof (a type)
1570 // ::= <template-param>
1571 // ::= <function-param>
1572 // ::= sr <type> <unqualified-name> # dependent name
1573 // ::= sr <type> <unqualified-name> <template-args> # dependent template-id
1574 // ::= sZ <template-param> # size of a parameter pack
John McCall09cc1412010-02-03 00:55:45 +00001575 // ::= <expr-primary>
John McCall1dd73832010-02-04 01:42:13 +00001576 // <expr-primary> ::= L <type> <value number> E # integer literal
1577 // ::= L <type <value float> E # floating literal
1578 // ::= L <mangled-name> E # external name
Anders Carlssond553f8c2009-09-21 01:21:10 +00001579 switch (E->getStmtClass()) {
John McCall6ae1f352010-04-09 22:26:14 +00001580 case Expr::NoStmtClass:
1581#define EXPR(Type, Base)
1582#define STMT(Type, Base) \
1583 case Expr::Type##Class:
Sean Hunt4bfe1962010-05-05 15:24:00 +00001584#include "clang/AST/StmtNodes.inc"
John McCall0512e482010-07-14 04:20:34 +00001585 // fallthrough
1586
1587 // These all can only appear in local or variable-initialization
1588 // contexts and so should never appear in a mangling.
1589 case Expr::AddrLabelExprClass:
1590 case Expr::BlockDeclRefExprClass:
1591 case Expr::CXXThisExprClass:
1592 case Expr::DesignatedInitExprClass:
1593 case Expr::ImplicitValueInitExprClass:
1594 case Expr::InitListExprClass:
1595 case Expr::ParenListExprClass:
1596 case Expr::CXXScalarValueInitExprClass:
John McCall09cc1412010-02-03 00:55:45 +00001597 llvm_unreachable("unexpected statement kind");
1598 break;
1599
John McCall0512e482010-07-14 04:20:34 +00001600 // FIXME: invent manglings for all these.
1601 case Expr::BlockExprClass:
1602 case Expr::CXXPseudoDestructorExprClass:
1603 case Expr::ChooseExprClass:
1604 case Expr::CompoundLiteralExprClass:
1605 case Expr::ExtVectorElementExprClass:
1606 case Expr::ObjCEncodeExprClass:
1607 case Expr::ObjCImplicitSetterGetterRefExprClass:
1608 case Expr::ObjCIsaExprClass:
1609 case Expr::ObjCIvarRefExprClass:
1610 case Expr::ObjCMessageExprClass:
1611 case Expr::ObjCPropertyRefExprClass:
1612 case Expr::ObjCProtocolExprClass:
1613 case Expr::ObjCSelectorExprClass:
1614 case Expr::ObjCStringLiteralClass:
John McCall0512e482010-07-14 04:20:34 +00001615 case Expr::OffsetOfExprClass:
1616 case Expr::PredefinedExprClass:
1617 case Expr::ShuffleVectorExprClass:
1618 case Expr::StmtExprClass:
1619 case Expr::TypesCompatibleExprClass:
1620 case Expr::UnaryTypeTraitExprClass:
Francois Pichet9be88402010-09-08 23:47:05 +00001621 case Expr::VAArgExprClass:
Sebastian Redl2e156222010-09-10 20:55:43 +00001622 case Expr::CXXUuidofExprClass:
1623 case Expr::CXXNoexceptExprClass: {
John McCall6ae1f352010-04-09 22:26:14 +00001624 // As bad as this diagnostic is, it's better than crashing.
1625 Diagnostic &Diags = Context.getDiags();
1626 unsigned DiagID = Diags.getCustomDiagID(Diagnostic::Error,
1627 "cannot yet mangle expression type %0");
John McCall739bf092010-04-10 09:39:25 +00001628 Diags.Report(FullSourceLoc(E->getExprLoc(),
1629 getASTContext().getSourceManager()),
1630 DiagID)
1631 << E->getStmtClassName() << E->getSourceRange();
John McCall6ae1f352010-04-09 22:26:14 +00001632 break;
1633 }
1634
John McCall0512e482010-07-14 04:20:34 +00001635 case Expr::CXXDefaultArgExprClass:
John McCall5e1e89b2010-08-18 19:18:59 +00001636 mangleExpression(cast<CXXDefaultArgExpr>(E)->getExpr(), Arity);
John McCall0512e482010-07-14 04:20:34 +00001637 break;
1638
1639 case Expr::CXXMemberCallExprClass: // fallthrough
John McCall1dd73832010-02-04 01:42:13 +00001640 case Expr::CallExprClass: {
1641 const CallExpr *CE = cast<CallExpr>(E);
1642 Out << "cl";
John McCall5e1e89b2010-08-18 19:18:59 +00001643 mangleExpression(CE->getCallee(), CE->getNumArgs());
John McCall1dd73832010-02-04 01:42:13 +00001644 for (unsigned I = 0, N = CE->getNumArgs(); I != N; ++I)
1645 mangleExpression(CE->getArg(I));
Benjamin Kramer35f59b62010-04-10 16:03:31 +00001646 Out << 'E';
John McCall09cc1412010-02-03 00:55:45 +00001647 break;
John McCall1dd73832010-02-04 01:42:13 +00001648 }
John McCall09cc1412010-02-03 00:55:45 +00001649
John McCall0512e482010-07-14 04:20:34 +00001650 case Expr::CXXNewExprClass: {
1651 // Proposal from David Vandervoorde, 2010.06.30
1652 const CXXNewExpr *New = cast<CXXNewExpr>(E);
1653 if (New->isGlobalNew()) Out << "gs";
1654 Out << (New->isArray() ? "na" : "nw");
1655 for (CXXNewExpr::const_arg_iterator I = New->placement_arg_begin(),
1656 E = New->placement_arg_end(); I != E; ++I)
1657 mangleExpression(*I);
1658 Out << '_';
1659 mangleType(New->getAllocatedType());
1660 if (New->hasInitializer()) {
1661 Out << "pi";
1662 for (CXXNewExpr::const_arg_iterator I = New->constructor_arg_begin(),
1663 E = New->constructor_arg_end(); I != E; ++I)
1664 mangleExpression(*I);
1665 }
1666 Out << 'E';
1667 break;
1668 }
1669
John McCall2f27bf82010-02-04 02:56:29 +00001670 case Expr::MemberExprClass: {
1671 const MemberExpr *ME = cast<MemberExpr>(E);
1672 mangleMemberExpr(ME->getBase(), ME->isArrow(),
1673 ME->getQualifier(), ME->getMemberDecl()->getDeclName(),
John McCall5e1e89b2010-08-18 19:18:59 +00001674 Arity);
John McCall2f27bf82010-02-04 02:56:29 +00001675 break;
1676 }
1677
1678 case Expr::UnresolvedMemberExprClass: {
1679 const UnresolvedMemberExpr *ME = cast<UnresolvedMemberExpr>(E);
1680 mangleMemberExpr(ME->getBase(), ME->isArrow(),
1681 ME->getQualifier(), ME->getMemberName(),
John McCall5e1e89b2010-08-18 19:18:59 +00001682 Arity);
John McCall6dbce192010-08-20 00:17:19 +00001683 if (ME->hasExplicitTemplateArgs())
1684 mangleTemplateArgs(ME->getExplicitTemplateArgs());
John McCall2f27bf82010-02-04 02:56:29 +00001685 break;
1686 }
1687
1688 case Expr::CXXDependentScopeMemberExprClass: {
1689 const CXXDependentScopeMemberExpr *ME
1690 = cast<CXXDependentScopeMemberExpr>(E);
1691 mangleMemberExpr(ME->getBase(), ME->isArrow(),
1692 ME->getQualifier(), ME->getMember(),
John McCall5e1e89b2010-08-18 19:18:59 +00001693 Arity);
John McCall6dbce192010-08-20 00:17:19 +00001694 if (ME->hasExplicitTemplateArgs())
1695 mangleTemplateArgs(ME->getExplicitTemplateArgs());
John McCall2f27bf82010-02-04 02:56:29 +00001696 break;
1697 }
1698
John McCall1dd73832010-02-04 01:42:13 +00001699 case Expr::UnresolvedLookupExprClass: {
John McCalla3218e72010-02-04 01:48:38 +00001700 // The ABI doesn't cover how to mangle overload sets, so we mangle
1701 // using something as close as possible to the original lookup
1702 // expression.
John McCall1dd73832010-02-04 01:42:13 +00001703 const UnresolvedLookupExpr *ULE = cast<UnresolvedLookupExpr>(E);
John McCall5e1e89b2010-08-18 19:18:59 +00001704 mangleUnresolvedName(ULE->getQualifier(), ULE->getName(), Arity);
John McCall6dbce192010-08-20 00:17:19 +00001705 if (ULE->hasExplicitTemplateArgs())
1706 mangleTemplateArgs(ULE->getExplicitTemplateArgs());
John McCall1dd73832010-02-04 01:42:13 +00001707 break;
1708 }
1709
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001710 case Expr::CXXUnresolvedConstructExprClass: {
John McCall1dd73832010-02-04 01:42:13 +00001711 const CXXUnresolvedConstructExpr *CE = cast<CXXUnresolvedConstructExpr>(E);
1712 unsigned N = CE->arg_size();
1713
1714 Out << "cv";
1715 mangleType(CE->getType());
Benjamin Kramer35f59b62010-04-10 16:03:31 +00001716 if (N != 1) Out << '_';
John McCall1dd73832010-02-04 01:42:13 +00001717 for (unsigned I = 0; I != N; ++I) mangleExpression(CE->getArg(I));
Benjamin Kramer35f59b62010-04-10 16:03:31 +00001718 if (N != 1) Out << 'E';
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001719 break;
John McCall1dd73832010-02-04 01:42:13 +00001720 }
John McCall09cc1412010-02-03 00:55:45 +00001721
John McCall1dd73832010-02-04 01:42:13 +00001722 case Expr::CXXTemporaryObjectExprClass:
1723 case Expr::CXXConstructExprClass: {
1724 const CXXConstructExpr *CE = cast<CXXConstructExpr>(E);
1725 unsigned N = CE->getNumArgs();
1726
1727 Out << "cv";
1728 mangleType(CE->getType());
Benjamin Kramer35f59b62010-04-10 16:03:31 +00001729 if (N != 1) Out << '_';
John McCall1dd73832010-02-04 01:42:13 +00001730 for (unsigned I = 0; I != N; ++I) mangleExpression(CE->getArg(I));
Benjamin Kramer35f59b62010-04-10 16:03:31 +00001731 if (N != 1) Out << 'E';
John McCall09cc1412010-02-03 00:55:45 +00001732 break;
John McCall1dd73832010-02-04 01:42:13 +00001733 }
1734
1735 case Expr::SizeOfAlignOfExprClass: {
1736 const SizeOfAlignOfExpr *SAE = cast<SizeOfAlignOfExpr>(E);
Benjamin Kramer35f59b62010-04-10 16:03:31 +00001737 if (SAE->isSizeOf()) Out << 's';
1738 else Out << 'a';
John McCall1dd73832010-02-04 01:42:13 +00001739 if (SAE->isArgumentType()) {
Benjamin Kramer35f59b62010-04-10 16:03:31 +00001740 Out << 't';
John McCall1dd73832010-02-04 01:42:13 +00001741 mangleType(SAE->getArgumentType());
1742 } else {
Benjamin Kramer35f59b62010-04-10 16:03:31 +00001743 Out << 'z';
John McCall1dd73832010-02-04 01:42:13 +00001744 mangleExpression(SAE->getArgumentExpr());
1745 }
1746 break;
1747 }
Anders Carlssona7694082009-11-06 02:50:19 +00001748
John McCall0512e482010-07-14 04:20:34 +00001749 case Expr::CXXThrowExprClass: {
1750 const CXXThrowExpr *TE = cast<CXXThrowExpr>(E);
1751
1752 // Proposal from David Vandervoorde, 2010.06.30
1753 if (TE->getSubExpr()) {
1754 Out << "tw";
1755 mangleExpression(TE->getSubExpr());
1756 } else {
1757 Out << "tr";
1758 }
1759 break;
1760 }
1761
1762 case Expr::CXXTypeidExprClass: {
1763 const CXXTypeidExpr *TIE = cast<CXXTypeidExpr>(E);
1764
1765 // Proposal from David Vandervoorde, 2010.06.30
1766 if (TIE->isTypeOperand()) {
1767 Out << "ti";
1768 mangleType(TIE->getTypeOperand());
1769 } else {
1770 Out << "te";
1771 mangleExpression(TIE->getExprOperand());
1772 }
1773 break;
1774 }
1775
1776 case Expr::CXXDeleteExprClass: {
1777 const CXXDeleteExpr *DE = cast<CXXDeleteExpr>(E);
1778
1779 // Proposal from David Vandervoorde, 2010.06.30
1780 if (DE->isGlobalDelete()) Out << "gs";
1781 Out << (DE->isArrayForm() ? "da" : "dl");
1782 mangleExpression(DE->getArgument());
1783 break;
1784 }
1785
Anders Carlssone170ba72009-12-14 01:45:37 +00001786 case Expr::UnaryOperatorClass: {
1787 const UnaryOperator *UO = cast<UnaryOperator>(E);
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001788 mangleOperatorName(UnaryOperator::getOverloadedOperator(UO->getOpcode()),
Anders Carlssone170ba72009-12-14 01:45:37 +00001789 /*Arity=*/1);
1790 mangleExpression(UO->getSubExpr());
1791 break;
1792 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001793
John McCall0512e482010-07-14 04:20:34 +00001794 case Expr::ArraySubscriptExprClass: {
1795 const ArraySubscriptExpr *AE = cast<ArraySubscriptExpr>(E);
1796
1797 // Array subscript is treated as a syntactically wierd form of
1798 // binary operator.
1799 Out << "ix";
1800 mangleExpression(AE->getLHS());
1801 mangleExpression(AE->getRHS());
1802 break;
1803 }
1804
1805 case Expr::CompoundAssignOperatorClass: // fallthrough
Anders Carlssone170ba72009-12-14 01:45:37 +00001806 case Expr::BinaryOperatorClass: {
1807 const BinaryOperator *BO = cast<BinaryOperator>(E);
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001808 mangleOperatorName(BinaryOperator::getOverloadedOperator(BO->getOpcode()),
Anders Carlssone170ba72009-12-14 01:45:37 +00001809 /*Arity=*/2);
1810 mangleExpression(BO->getLHS());
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001811 mangleExpression(BO->getRHS());
Anders Carlssone170ba72009-12-14 01:45:37 +00001812 break;
John McCall2f27bf82010-02-04 02:56:29 +00001813 }
Anders Carlssone170ba72009-12-14 01:45:37 +00001814
1815 case Expr::ConditionalOperatorClass: {
1816 const ConditionalOperator *CO = cast<ConditionalOperator>(E);
1817 mangleOperatorName(OO_Conditional, /*Arity=*/3);
1818 mangleExpression(CO->getCond());
John McCall5e1e89b2010-08-18 19:18:59 +00001819 mangleExpression(CO->getLHS(), Arity);
1820 mangleExpression(CO->getRHS(), Arity);
Anders Carlssone170ba72009-12-14 01:45:37 +00001821 break;
1822 }
1823
Douglas Gregor46287c72010-01-29 16:37:09 +00001824 case Expr::ImplicitCastExprClass: {
John McCall5e1e89b2010-08-18 19:18:59 +00001825 mangleExpression(cast<ImplicitCastExpr>(E)->getSubExpr(), Arity);
Douglas Gregor46287c72010-01-29 16:37:09 +00001826 break;
1827 }
1828
1829 case Expr::CStyleCastExprClass:
1830 case Expr::CXXStaticCastExprClass:
1831 case Expr::CXXDynamicCastExprClass:
1832 case Expr::CXXReinterpretCastExprClass:
1833 case Expr::CXXConstCastExprClass:
1834 case Expr::CXXFunctionalCastExprClass: {
1835 const ExplicitCastExpr *ECE = cast<ExplicitCastExpr>(E);
1836 Out << "cv";
1837 mangleType(ECE->getType());
1838 mangleExpression(ECE->getSubExpr());
1839 break;
1840 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001841
Anders Carlsson58040a52009-12-16 05:48:46 +00001842 case Expr::CXXOperatorCallExprClass: {
1843 const CXXOperatorCallExpr *CE = cast<CXXOperatorCallExpr>(E);
1844 unsigned NumArgs = CE->getNumArgs();
1845 mangleOperatorName(CE->getOperator(), /*Arity=*/NumArgs);
1846 // Mangle the arguments.
1847 for (unsigned i = 0; i != NumArgs; ++i)
1848 mangleExpression(CE->getArg(i));
1849 break;
1850 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001851
Anders Carlssona7694082009-11-06 02:50:19 +00001852 case Expr::ParenExprClass:
John McCall5e1e89b2010-08-18 19:18:59 +00001853 mangleExpression(cast<ParenExpr>(E)->getSubExpr(), Arity);
Anders Carlssona7694082009-11-06 02:50:19 +00001854 break;
1855
Anders Carlssond553f8c2009-09-21 01:21:10 +00001856 case Expr::DeclRefExprClass: {
Douglas Gregor5ed1bc32010-02-28 21:40:32 +00001857 const NamedDecl *D = cast<DeclRefExpr>(E)->getDecl();
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00001858
Anders Carlssond553f8c2009-09-21 01:21:10 +00001859 switch (D->getKind()) {
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001860 default:
Douglas Gregor5ed1bc32010-02-28 21:40:32 +00001861 // <expr-primary> ::= L <mangled-name> E # external name
1862 Out << 'L';
1863 mangle(D, "_Z");
1864 Out << 'E';
1865 break;
1866
John McCall3dc7e7b2010-07-24 01:17:35 +00001867 case Decl::EnumConstant: {
1868 const EnumConstantDecl *ED = cast<EnumConstantDecl>(D);
1869 mangleIntegerLiteral(ED->getType(), ED->getInitVal());
1870 break;
1871 }
1872
Anders Carlssond553f8c2009-09-21 01:21:10 +00001873 case Decl::NonTypeTemplateParm: {
1874 const NonTypeTemplateParmDecl *PD = cast<NonTypeTemplateParmDecl>(D);
Anders Carlsson0ccdf8d2009-09-27 00:38:53 +00001875 mangleTemplateParameter(PD->getIndex());
Anders Carlssond553f8c2009-09-21 01:21:10 +00001876 break;
1877 }
1878
1879 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00001880
Anders Carlsson50755b02009-09-27 20:11:34 +00001881 break;
Anders Carlssond553f8c2009-09-21 01:21:10 +00001882 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00001883
John McCall865d4472009-11-19 22:55:06 +00001884 case Expr::DependentScopeDeclRefExprClass: {
1885 const DependentScopeDeclRefExpr *DRE = cast<DependentScopeDeclRefExpr>(E);
Douglas Gregor4b2ccfc2010-02-28 22:05:49 +00001886 NestedNameSpecifier *NNS = DRE->getQualifier();
1887 const Type *QTy = NNS->getAsType();
1888
1889 // When we're dealing with a nested-name-specifier that has just a
1890 // dependent identifier in it, mangle that as a typename. FIXME:
1891 // It isn't clear that we ever actually want to have such a
1892 // nested-name-specifier; why not just represent it as a typename type?
1893 if (!QTy && NNS->getAsIdentifier() && NNS->getPrefix()) {
Douglas Gregor4a2023f2010-03-31 20:19:30 +00001894 QTy = getASTContext().getDependentNameType(ETK_Typename,
1895 NNS->getPrefix(),
1896 NNS->getAsIdentifier())
Douglas Gregor4b2ccfc2010-02-28 22:05:49 +00001897 .getTypePtr();
1898 }
Anders Carlsson50755b02009-09-27 20:11:34 +00001899 assert(QTy && "Qualifier was not type!");
1900
John McCall6dbce192010-08-20 00:17:19 +00001901 // ::= sr <type> <unqualified-name> # dependent name
1902 // ::= sr <type> <unqualified-name> <template-args> # dependent template-id
Anders Carlsson50755b02009-09-27 20:11:34 +00001903 Out << "sr";
1904 mangleType(QualType(QTy, 0));
John McCall5e1e89b2010-08-18 19:18:59 +00001905 mangleUnqualifiedName(0, DRE->getDeclName(), Arity);
John McCall6dbce192010-08-20 00:17:19 +00001906 if (DRE->hasExplicitTemplateArgs())
1907 mangleTemplateArgs(DRE->getExplicitTemplateArgs());
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00001908
Anders Carlsson50755b02009-09-27 20:11:34 +00001909 break;
1910 }
1911
John McCalld9307602010-04-09 22:54:09 +00001912 case Expr::CXXBindTemporaryExprClass:
1913 mangleExpression(cast<CXXBindTemporaryExpr>(E)->getSubExpr());
1914 break;
1915
1916 case Expr::CXXExprWithTemporariesClass:
John McCall5e1e89b2010-08-18 19:18:59 +00001917 mangleExpression(cast<CXXExprWithTemporaries>(E)->getSubExpr(), Arity);
John McCalld9307602010-04-09 22:54:09 +00001918 break;
1919
John McCall1dd73832010-02-04 01:42:13 +00001920 case Expr::FloatingLiteralClass: {
1921 const FloatingLiteral *FL = cast<FloatingLiteral>(E);
Benjamin Kramer35f59b62010-04-10 16:03:31 +00001922 Out << 'L';
John McCall1dd73832010-02-04 01:42:13 +00001923 mangleType(FL->getType());
John McCall0512e482010-07-14 04:20:34 +00001924 mangleFloat(FL->getValue());
Benjamin Kramer35f59b62010-04-10 16:03:31 +00001925 Out << 'E';
John McCall1dd73832010-02-04 01:42:13 +00001926 break;
1927 }
1928
John McCallde810632010-04-09 21:48:08 +00001929 case Expr::CharacterLiteralClass:
Benjamin Kramer35f59b62010-04-10 16:03:31 +00001930 Out << 'L';
John McCallde810632010-04-09 21:48:08 +00001931 mangleType(E->getType());
1932 Out << cast<CharacterLiteral>(E)->getValue();
1933 Out << 'E';
1934 break;
1935
1936 case Expr::CXXBoolLiteralExprClass:
1937 Out << "Lb";
1938 Out << (cast<CXXBoolLiteralExpr>(E)->getValue() ? '1' : '0');
1939 Out << 'E';
1940 break;
1941
John McCall0512e482010-07-14 04:20:34 +00001942 case Expr::IntegerLiteralClass: {
1943 llvm::APSInt Value(cast<IntegerLiteral>(E)->getValue());
1944 if (E->getType()->isSignedIntegerType())
1945 Value.setIsSigned(true);
1946 mangleIntegerLiteral(E->getType(), Value);
Anders Carlssone170ba72009-12-14 01:45:37 +00001947 break;
John McCall0512e482010-07-14 04:20:34 +00001948 }
1949
1950 case Expr::ImaginaryLiteralClass: {
1951 const ImaginaryLiteral *IE = cast<ImaginaryLiteral>(E);
1952 // Mangle as if a complex literal.
Nick Lewycky271b6652010-09-05 03:40:33 +00001953 // Proposal from David Vandevoorde, 2010.06.30.
John McCall0512e482010-07-14 04:20:34 +00001954 Out << 'L';
1955 mangleType(E->getType());
1956 if (const FloatingLiteral *Imag =
1957 dyn_cast<FloatingLiteral>(IE->getSubExpr())) {
1958 // Mangle a floating-point zero of the appropriate type.
1959 mangleFloat(llvm::APFloat(Imag->getValue().getSemantics()));
1960 Out << '_';
1961 mangleFloat(Imag->getValue());
1962 } else {
Nick Lewycky271b6652010-09-05 03:40:33 +00001963 Out << "0_";
John McCall0512e482010-07-14 04:20:34 +00001964 llvm::APSInt Value(cast<IntegerLiteral>(IE->getSubExpr())->getValue());
1965 if (IE->getSubExpr()->getType()->isSignedIntegerType())
1966 Value.setIsSigned(true);
1967 mangleNumber(Value);
1968 }
1969 Out << 'E';
1970 break;
1971 }
1972
1973 case Expr::StringLiteralClass: {
John McCall1658c392010-07-15 21:53:03 +00001974 // Revised proposal from David Vandervoorde, 2010.07.15.
John McCall0512e482010-07-14 04:20:34 +00001975 Out << 'L';
John McCall1658c392010-07-15 21:53:03 +00001976 assert(isa<ConstantArrayType>(E->getType()));
1977 mangleType(E->getType());
John McCall0512e482010-07-14 04:20:34 +00001978 Out << 'E';
1979 break;
1980 }
1981
1982 case Expr::GNUNullExprClass:
1983 // FIXME: should this really be mangled the same as nullptr?
1984 // fallthrough
1985
1986 case Expr::CXXNullPtrLiteralExprClass: {
1987 // Proposal from David Vandervoorde, 2010.06.30, as
1988 // modified by ABI list discussion.
1989 Out << "LDnE";
1990 break;
1991 }
Anders Carlssone170ba72009-12-14 01:45:37 +00001992
Anders Carlssond553f8c2009-09-21 01:21:10 +00001993 }
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001994}
1995
Anders Carlsson3ac86b52009-04-15 05:36:58 +00001996void CXXNameMangler::mangleCXXCtorType(CXXCtorType T) {
1997 // <ctor-dtor-name> ::= C1 # complete object constructor
1998 // ::= C2 # base object constructor
1999 // ::= C3 # complete object allocating constructor
2000 //
2001 switch (T) {
2002 case Ctor_Complete:
2003 Out << "C1";
2004 break;
2005 case Ctor_Base:
2006 Out << "C2";
2007 break;
2008 case Ctor_CompleteAllocating:
2009 Out << "C3";
2010 break;
2011 }
2012}
2013
Anders Carlsson27ae5362009-04-17 01:58:57 +00002014void CXXNameMangler::mangleCXXDtorType(CXXDtorType T) {
2015 // <ctor-dtor-name> ::= D0 # deleting destructor
2016 // ::= D1 # complete object destructor
2017 // ::= D2 # base object destructor
2018 //
2019 switch (T) {
2020 case Dtor_Deleting:
2021 Out << "D0";
2022 break;
2023 case Dtor_Complete:
2024 Out << "D1";
2025 break;
2026 case Dtor_Base:
2027 Out << "D2";
2028 break;
2029 }
2030}
2031
John McCall6dbce192010-08-20 00:17:19 +00002032void CXXNameMangler::mangleTemplateArgs(
2033 const ExplicitTemplateArgumentList &TemplateArgs) {
2034 // <template-args> ::= I <template-arg>+ E
2035 Out << 'I';
2036 for (unsigned I = 0, E = TemplateArgs.NumTemplateArgs; I != E; ++I)
2037 mangleTemplateArg(0, TemplateArgs.getTemplateArgs()[I].getArgument());
2038 Out << 'E';
2039}
2040
Douglas Gregor20f0cc72010-04-23 03:10:43 +00002041void CXXNameMangler::mangleTemplateArgs(TemplateName Template,
2042 const TemplateArgument *TemplateArgs,
2043 unsigned NumTemplateArgs) {
2044 if (TemplateDecl *TD = Template.getAsTemplateDecl())
2045 return mangleTemplateArgs(*TD->getTemplateParameters(), TemplateArgs,
2046 NumTemplateArgs);
Sean Huntc3021132010-05-05 15:23:54 +00002047
Douglas Gregor20f0cc72010-04-23 03:10:43 +00002048 // <template-args> ::= I <template-arg>+ E
2049 Out << 'I';
2050 for (unsigned i = 0; i != NumTemplateArgs; ++i)
2051 mangleTemplateArg(0, TemplateArgs[i]);
2052 Out << 'E';
2053}
2054
Rafael Espindolad9800722010-03-11 14:07:00 +00002055void CXXNameMangler::mangleTemplateArgs(const TemplateParameterList &PL,
2056 const TemplateArgumentList &AL) {
Anders Carlsson7a0ba872009-05-15 16:09:15 +00002057 // <template-args> ::= I <template-arg>+ E
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002058 Out << 'I';
Rafael Espindolad9800722010-03-11 14:07:00 +00002059 for (unsigned i = 0, e = AL.size(); i != e; ++i)
2060 mangleTemplateArg(PL.getParam(i), AL[i]);
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002061 Out << 'E';
Anders Carlsson7a0ba872009-05-15 16:09:15 +00002062}
2063
Rafael Espindolad9800722010-03-11 14:07:00 +00002064void CXXNameMangler::mangleTemplateArgs(const TemplateParameterList &PL,
2065 const TemplateArgument *TemplateArgs,
Anders Carlsson7624f212009-09-18 02:42:01 +00002066 unsigned NumTemplateArgs) {
2067 // <template-args> ::= I <template-arg>+ E
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002068 Out << 'I';
Daniel Dunbar7e0c1952009-11-21 09:17:15 +00002069 for (unsigned i = 0; i != NumTemplateArgs; ++i)
Rafael Espindolad9800722010-03-11 14:07:00 +00002070 mangleTemplateArg(PL.getParam(i), TemplateArgs[i]);
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002071 Out << 'E';
Anders Carlsson7624f212009-09-18 02:42:01 +00002072}
2073
Rafael Espindolad9800722010-03-11 14:07:00 +00002074void CXXNameMangler::mangleTemplateArg(const NamedDecl *P,
2075 const TemplateArgument &A) {
Mike Stump1eb44332009-09-09 15:08:12 +00002076 // <template-arg> ::= <type> # type or template
Anders Carlsson7a0ba872009-05-15 16:09:15 +00002077 // ::= X <expression> E # expression
2078 // ::= <expr-primary> # simple expressions
2079 // ::= I <template-arg>* E # argument pack
2080 // ::= sp <expression> # pack expansion of (C++0x)
2081 switch (A.getKind()) {
2082 default:
2083 assert(0 && "Unknown template argument kind!");
2084 case TemplateArgument::Type:
2085 mangleType(A.getAsType());
2086 break;
Anders Carlsson9e85c742009-12-23 19:30:55 +00002087 case TemplateArgument::Template:
John McCallb6f532e2010-07-14 06:43:17 +00002088 // This is mangled as <type>.
2089 mangleType(A.getAsTemplate());
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002090 break;
Anders Carlssond553f8c2009-09-21 01:21:10 +00002091 case TemplateArgument::Expression:
2092 Out << 'X';
2093 mangleExpression(A.getAsExpr());
2094 Out << 'E';
2095 break;
Anders Carlssone170ba72009-12-14 01:45:37 +00002096 case TemplateArgument::Integral:
2097 mangleIntegerLiteral(A.getIntegralType(), *A.getAsIntegral());
Anders Carlsson7a0ba872009-05-15 16:09:15 +00002098 break;
Daniel Dunbar7e0c1952009-11-21 09:17:15 +00002099 case TemplateArgument::Declaration: {
Douglas Gregor20f0cc72010-04-23 03:10:43 +00002100 assert(P && "Missing template parameter for declaration argument");
Daniel Dunbar7e0c1952009-11-21 09:17:15 +00002101 // <expr-primary> ::= L <mangled-name> E # external name
2102
Rafael Espindolad9800722010-03-11 14:07:00 +00002103 // Clang produces AST's where pointer-to-member-function expressions
Daniel Dunbar7e0c1952009-11-21 09:17:15 +00002104 // and pointer-to-function expressions are represented as a declaration not
Rafael Espindolad9800722010-03-11 14:07:00 +00002105 // an expression. We compensate for it here to produce the correct mangling.
2106 NamedDecl *D = cast<NamedDecl>(A.getAsDecl());
2107 const NonTypeTemplateParmDecl *Parameter = cast<NonTypeTemplateParmDecl>(P);
2108 bool compensateMangling = D->isCXXClassMember() &&
2109 !Parameter->getType()->isReferenceType();
2110 if (compensateMangling) {
2111 Out << 'X';
2112 mangleOperatorName(OO_Amp, 1);
2113 }
2114
Daniel Dunbar7e0c1952009-11-21 09:17:15 +00002115 Out << 'L';
2116 // References to external entities use the mangled name; if the name would
2117 // not normally be manged then mangle it as unqualified.
2118 //
2119 // FIXME: The ABI specifies that external names here should have _Z, but
2120 // gcc leaves this off.
Rafael Espindolad9800722010-03-11 14:07:00 +00002121 if (compensateMangling)
2122 mangle(D, "_Z");
2123 else
2124 mangle(D, "Z");
Daniel Dunbar7e0c1952009-11-21 09:17:15 +00002125 Out << 'E';
Rafael Espindolad9800722010-03-11 14:07:00 +00002126
2127 if (compensateMangling)
2128 Out << 'E';
2129
Daniel Dunbar7e0c1952009-11-21 09:17:15 +00002130 break;
2131 }
2132 }
Anders Carlsson7a0ba872009-05-15 16:09:15 +00002133}
2134
Anders Carlsson0ccdf8d2009-09-27 00:38:53 +00002135void CXXNameMangler::mangleTemplateParameter(unsigned Index) {
2136 // <template-param> ::= T_ # first template parameter
2137 // ::= T <parameter-2 non-negative number> _
2138 if (Index == 0)
2139 Out << "T_";
2140 else
2141 Out << 'T' << (Index - 1) << '_';
2142}
2143
Anders Carlsson76967372009-09-17 00:43:46 +00002144// <substitution> ::= S <seq-id> _
2145// ::= S_
Anders Carlsson6862fc72009-09-17 04:16:28 +00002146bool CXXNameMangler::mangleSubstitution(const NamedDecl *ND) {
Anders Carlssone7c8cb62009-09-26 20:53:44 +00002147 // Try one of the standard substitutions first.
2148 if (mangleStandardSubstitution(ND))
2149 return true;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002150
Anders Carlsson433d1372009-11-07 04:26:04 +00002151 ND = cast<NamedDecl>(ND->getCanonicalDecl());
Anders Carlsson6862fc72009-09-17 04:16:28 +00002152 return mangleSubstitution(reinterpret_cast<uintptr_t>(ND));
2153}
2154
Anders Carlsson76967372009-09-17 00:43:46 +00002155bool CXXNameMangler::mangleSubstitution(QualType T) {
Anders Carlssond99edc42009-09-26 03:55:37 +00002156 if (!T.getCVRQualifiers()) {
2157 if (const RecordType *RT = T->getAs<RecordType>())
2158 return mangleSubstitution(RT->getDecl());
2159 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002160
Anders Carlsson76967372009-09-17 00:43:46 +00002161 uintptr_t TypePtr = reinterpret_cast<uintptr_t>(T.getAsOpaquePtr());
2162
Anders Carlssond3a932a2009-09-17 03:53:28 +00002163 return mangleSubstitution(TypePtr);
2164}
2165
Douglas Gregor1e9268e2010-04-28 05:58:56 +00002166bool CXXNameMangler::mangleSubstitution(TemplateName Template) {
2167 if (TemplateDecl *TD = Template.getAsTemplateDecl())
2168 return mangleSubstitution(TD);
Sean Huntc3021132010-05-05 15:23:54 +00002169
Douglas Gregor1e9268e2010-04-28 05:58:56 +00002170 Template = Context.getASTContext().getCanonicalTemplateName(Template);
2171 return mangleSubstitution(
2172 reinterpret_cast<uintptr_t>(Template.getAsVoidPointer()));
2173}
2174
Anders Carlssond3a932a2009-09-17 03:53:28 +00002175bool CXXNameMangler::mangleSubstitution(uintptr_t Ptr) {
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002176 llvm::DenseMap<uintptr_t, unsigned>::iterator I = Substitutions.find(Ptr);
Anders Carlsson76967372009-09-17 00:43:46 +00002177 if (I == Substitutions.end())
2178 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002179
Anders Carlsson76967372009-09-17 00:43:46 +00002180 unsigned SeqID = I->second;
2181 if (SeqID == 0)
2182 Out << "S_";
2183 else {
2184 SeqID--;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002185
Anders Carlsson76967372009-09-17 00:43:46 +00002186 // <seq-id> is encoded in base-36, using digits and upper case letters.
2187 char Buffer[10];
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002188 char *BufferPtr = llvm::array_endof(Buffer);
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002189
Anders Carlsson76967372009-09-17 00:43:46 +00002190 if (SeqID == 0) *--BufferPtr = '0';
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002191
Anders Carlsson76967372009-09-17 00:43:46 +00002192 while (SeqID) {
2193 assert(BufferPtr > Buffer && "Buffer overflow!");
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002194
John McCall6ab30e02010-06-09 07:26:17 +00002195 char c = static_cast<char>(SeqID % 36);
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002196
Anders Carlsson76967372009-09-17 00:43:46 +00002197 *--BufferPtr = (c < 10 ? '0' + c : 'A' + c - 10);
2198 SeqID /= 36;
2199 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002200
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002201 Out << 'S'
2202 << llvm::StringRef(BufferPtr, llvm::array_endof(Buffer)-BufferPtr)
2203 << '_';
Anders Carlsson76967372009-09-17 00:43:46 +00002204 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002205
Anders Carlsson76967372009-09-17 00:43:46 +00002206 return true;
2207}
2208
Anders Carlssonf514b542009-09-27 00:12:57 +00002209static bool isCharType(QualType T) {
2210 if (T.isNull())
2211 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002212
Anders Carlssonf514b542009-09-27 00:12:57 +00002213 return T->isSpecificBuiltinType(BuiltinType::Char_S) ||
2214 T->isSpecificBuiltinType(BuiltinType::Char_U);
2215}
2216
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002217/// isCharSpecialization - Returns whether a given type is a template
Anders Carlssonf514b542009-09-27 00:12:57 +00002218/// specialization of a given name with a single argument of type char.
2219static bool isCharSpecialization(QualType T, const char *Name) {
2220 if (T.isNull())
2221 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002222
Anders Carlssonf514b542009-09-27 00:12:57 +00002223 const RecordType *RT = T->getAs<RecordType>();
2224 if (!RT)
2225 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002226
2227 const ClassTemplateSpecializationDecl *SD =
Anders Carlssonf514b542009-09-27 00:12:57 +00002228 dyn_cast<ClassTemplateSpecializationDecl>(RT->getDecl());
2229 if (!SD)
2230 return false;
2231
2232 if (!isStdNamespace(SD->getDeclContext()))
2233 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002234
Anders Carlssonf514b542009-09-27 00:12:57 +00002235 const TemplateArgumentList &TemplateArgs = SD->getTemplateArgs();
2236 if (TemplateArgs.size() != 1)
2237 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002238
Anders Carlssonf514b542009-09-27 00:12:57 +00002239 if (!isCharType(TemplateArgs[0].getAsType()))
2240 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002241
Daniel Dunbar01eb9b92009-10-18 21:17:35 +00002242 return SD->getIdentifier()->getName() == Name;
Anders Carlssonf514b542009-09-27 00:12:57 +00002243}
2244
Anders Carlsson91f88602009-12-07 19:56:42 +00002245template <std::size_t StrLen>
2246bool isStreamCharSpecialization(const ClassTemplateSpecializationDecl *SD,
2247 const char (&Str)[StrLen]) {
2248 if (!SD->getIdentifier()->isStr(Str))
2249 return false;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002250
Anders Carlsson91f88602009-12-07 19:56:42 +00002251 const TemplateArgumentList &TemplateArgs = SD->getTemplateArgs();
2252 if (TemplateArgs.size() != 2)
2253 return false;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002254
Anders Carlsson91f88602009-12-07 19:56:42 +00002255 if (!isCharType(TemplateArgs[0].getAsType()))
2256 return false;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002257
Anders Carlsson91f88602009-12-07 19:56:42 +00002258 if (!isCharSpecialization(TemplateArgs[1].getAsType(), "char_traits"))
2259 return false;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002260
Anders Carlsson91f88602009-12-07 19:56:42 +00002261 return true;
2262}
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002263
Anders Carlssone7c8cb62009-09-26 20:53:44 +00002264bool CXXNameMangler::mangleStandardSubstitution(const NamedDecl *ND) {
2265 // <substitution> ::= St # ::std::
Anders Carlsson8c031552009-09-26 23:10:05 +00002266 if (const NamespaceDecl *NS = dyn_cast<NamespaceDecl>(ND)) {
Anders Carlsson47846d22009-12-04 06:23:23 +00002267 if (isStd(NS)) {
Anders Carlsson8c031552009-09-26 23:10:05 +00002268 Out << "St";
2269 return true;
2270 }
2271 }
2272
2273 if (const ClassTemplateDecl *TD = dyn_cast<ClassTemplateDecl>(ND)) {
2274 if (!isStdNamespace(TD->getDeclContext()))
2275 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002276
Anders Carlsson8c031552009-09-26 23:10:05 +00002277 // <substitution> ::= Sa # ::std::allocator
2278 if (TD->getIdentifier()->isStr("allocator")) {
2279 Out << "Sa";
2280 return true;
2281 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002282
Anders Carlsson189d59c2009-09-26 23:14:39 +00002283 // <<substitution> ::= Sb # ::std::basic_string
2284 if (TD->getIdentifier()->isStr("basic_string")) {
2285 Out << "Sb";
2286 return true;
2287 }
Anders Carlsson8c031552009-09-26 23:10:05 +00002288 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002289
2290 if (const ClassTemplateSpecializationDecl *SD =
Anders Carlssonf514b542009-09-27 00:12:57 +00002291 dyn_cast<ClassTemplateSpecializationDecl>(ND)) {
Eli Friedman5370ee22010-02-23 18:25:09 +00002292 if (!isStdNamespace(SD->getDeclContext()))
2293 return false;
2294
Anders Carlssonf514b542009-09-27 00:12:57 +00002295 // <substitution> ::= Ss # ::std::basic_string<char,
2296 // ::std::char_traits<char>,
2297 // ::std::allocator<char> >
2298 if (SD->getIdentifier()->isStr("basic_string")) {
2299 const TemplateArgumentList &TemplateArgs = SD->getTemplateArgs();
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002300
Anders Carlssonf514b542009-09-27 00:12:57 +00002301 if (TemplateArgs.size() != 3)
2302 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002303
Anders Carlssonf514b542009-09-27 00:12:57 +00002304 if (!isCharType(TemplateArgs[0].getAsType()))
2305 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002306
Anders Carlssonf514b542009-09-27 00:12:57 +00002307 if (!isCharSpecialization(TemplateArgs[1].getAsType(), "char_traits"))
2308 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002309
Anders Carlssonf514b542009-09-27 00:12:57 +00002310 if (!isCharSpecialization(TemplateArgs[2].getAsType(), "allocator"))
2311 return false;
2312
2313 Out << "Ss";
2314 return true;
2315 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002316
Anders Carlsson91f88602009-12-07 19:56:42 +00002317 // <substitution> ::= Si # ::std::basic_istream<char,
2318 // ::std::char_traits<char> >
2319 if (isStreamCharSpecialization(SD, "basic_istream")) {
2320 Out << "Si";
2321 return true;
2322 }
2323
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002324 // <substitution> ::= So # ::std::basic_ostream<char,
Anders Carlsson8f8fd8e2009-10-08 17:20:26 +00002325 // ::std::char_traits<char> >
Anders Carlsson91f88602009-12-07 19:56:42 +00002326 if (isStreamCharSpecialization(SD, "basic_ostream")) {
Anders Carlsson8f8fd8e2009-10-08 17:20:26 +00002327 Out << "So";
2328 return true;
2329 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002330
Anders Carlsson91f88602009-12-07 19:56:42 +00002331 // <substitution> ::= Sd # ::std::basic_iostream<char,
2332 // ::std::char_traits<char> >
2333 if (isStreamCharSpecialization(SD, "basic_iostream")) {
2334 Out << "Sd";
2335 return true;
2336 }
Anders Carlssonf514b542009-09-27 00:12:57 +00002337 }
Anders Carlsson8c031552009-09-26 23:10:05 +00002338 return false;
Anders Carlssone7c8cb62009-09-26 20:53:44 +00002339}
2340
Anders Carlsson76967372009-09-17 00:43:46 +00002341void CXXNameMangler::addSubstitution(QualType T) {
Anders Carlssond99edc42009-09-26 03:55:37 +00002342 if (!T.getCVRQualifiers()) {
2343 if (const RecordType *RT = T->getAs<RecordType>()) {
2344 addSubstitution(RT->getDecl());
2345 return;
2346 }
2347 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002348
Anders Carlsson76967372009-09-17 00:43:46 +00002349 uintptr_t TypePtr = reinterpret_cast<uintptr_t>(T.getAsOpaquePtr());
Anders Carlssond3a932a2009-09-17 03:53:28 +00002350 addSubstitution(TypePtr);
2351}
2352
Douglas Gregor1e9268e2010-04-28 05:58:56 +00002353void CXXNameMangler::addSubstitution(TemplateName Template) {
2354 if (TemplateDecl *TD = Template.getAsTemplateDecl())
2355 return addSubstitution(TD);
Sean Huntc3021132010-05-05 15:23:54 +00002356
Douglas Gregor1e9268e2010-04-28 05:58:56 +00002357 Template = Context.getASTContext().getCanonicalTemplateName(Template);
2358 addSubstitution(reinterpret_cast<uintptr_t>(Template.getAsVoidPointer()));
2359}
2360
Anders Carlssond3a932a2009-09-17 03:53:28 +00002361void CXXNameMangler::addSubstitution(uintptr_t Ptr) {
Anders Carlssond3a932a2009-09-17 03:53:28 +00002362 assert(!Substitutions.count(Ptr) && "Substitution already exists!");
Anders Carlsson9d85b722010-06-02 04:29:50 +00002363 Substitutions[Ptr] = SeqID++;
Anders Carlsson76967372009-09-17 00:43:46 +00002364}
2365
Daniel Dunbar1b077112009-11-21 09:06:10 +00002366//
Mike Stump1eb44332009-09-09 15:08:12 +00002367
Daniel Dunbar1b077112009-11-21 09:06:10 +00002368/// \brief Mangles the name of the declaration D and emits that name to the
2369/// given output stream.
2370///
2371/// If the declaration D requires a mangled name, this routine will emit that
2372/// mangled name to \p os and return true. Otherwise, \p os will be unchanged
2373/// and this routine will return false. In this case, the caller should just
2374/// emit the identifier of the declaration (\c D->getIdentifier()) as its
2375/// name.
Daniel Dunbarf981bf82009-11-21 09:14:52 +00002376void MangleContext::mangleName(const NamedDecl *D,
Daniel Dunbar94fd26d2009-11-21 09:06:22 +00002377 llvm::SmallVectorImpl<char> &Res) {
Daniel Dunbarc02ab4c2009-11-21 09:14:44 +00002378 assert((isa<FunctionDecl>(D) || isa<VarDecl>(D)) &&
2379 "Invalid mangleName() call, argument is not a variable or function!");
2380 assert(!isa<CXXConstructorDecl>(D) && !isa<CXXDestructorDecl>(D) &&
2381 "Invalid mangleName() call on 'structor decl!");
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002382
Daniel Dunbar1b077112009-11-21 09:06:10 +00002383 PrettyStackTraceDecl CrashInfo(D, SourceLocation(),
2384 getASTContext().getSourceManager(),
2385 "Mangling declaration");
Mike Stump1eb44332009-09-09 15:08:12 +00002386
Daniel Dunbar94fd26d2009-11-21 09:06:22 +00002387 CXXNameMangler Mangler(*this, Res);
2388 return Mangler.mangle(D);
Daniel Dunbar1b077112009-11-21 09:06:10 +00002389}
Mike Stump1eb44332009-09-09 15:08:12 +00002390
Daniel Dunbar1b077112009-11-21 09:06:10 +00002391void MangleContext::mangleCXXCtor(const CXXConstructorDecl *D, CXXCtorType Type,
Daniel Dunbar94fd26d2009-11-21 09:06:22 +00002392 llvm::SmallVectorImpl<char> &Res) {
Daniel Dunbar77939c92009-11-21 09:06:31 +00002393 CXXNameMangler Mangler(*this, Res, D, Type);
2394 Mangler.mangle(D);
Daniel Dunbar1b077112009-11-21 09:06:10 +00002395}
Mike Stump1eb44332009-09-09 15:08:12 +00002396
Daniel Dunbar1b077112009-11-21 09:06:10 +00002397void MangleContext::mangleCXXDtor(const CXXDestructorDecl *D, CXXDtorType Type,
Daniel Dunbar94fd26d2009-11-21 09:06:22 +00002398 llvm::SmallVectorImpl<char> &Res) {
Daniel Dunbar77939c92009-11-21 09:06:31 +00002399 CXXNameMangler Mangler(*this, Res, D, Type);
2400 Mangler.mangle(D);
Daniel Dunbar1b077112009-11-21 09:06:10 +00002401}
Mike Stumpf1216772009-07-31 18:25:34 +00002402
Fariborz Jahanian564360b2010-06-24 00:08:06 +00002403void MangleContext::mangleBlock(GlobalDecl GD, const BlockDecl *BD,
Douglas Gregor35415f52010-05-25 17:04:15 +00002404 llvm::SmallVectorImpl<char> &Res) {
Charles Davis685b1d92010-05-26 18:25:27 +00002405 MiscNameMangler Mangler(*this, Res);
Fariborz Jahanian564360b2010-06-24 00:08:06 +00002406 Mangler.mangleBlock(GD, BD);
Douglas Gregor35415f52010-05-25 17:04:15 +00002407}
2408
Anders Carlsson19879c92010-03-23 17:17:29 +00002409void MangleContext::mangleThunk(const CXXMethodDecl *MD,
2410 const ThunkInfo &Thunk,
2411 llvm::SmallVectorImpl<char> &Res) {
2412 // <special-name> ::= T <call-offset> <base encoding>
2413 // # base is the nominal target function of thunk
2414 // <special-name> ::= Tc <call-offset> <call-offset> <base encoding>
2415 // # base is the nominal target function of thunk
2416 // # first call-offset is 'this' adjustment
2417 // # second call-offset is result adjustment
Sean Huntc3021132010-05-05 15:23:54 +00002418
Anders Carlsson19879c92010-03-23 17:17:29 +00002419 assert(!isa<CXXDestructorDecl>(MD) &&
2420 "Use mangleCXXDtor for destructor decls!");
Sean Huntc3021132010-05-05 15:23:54 +00002421
Anders Carlsson19879c92010-03-23 17:17:29 +00002422 CXXNameMangler Mangler(*this, Res);
2423 Mangler.getStream() << "_ZT";
2424 if (!Thunk.Return.isEmpty())
2425 Mangler.getStream() << 'c';
Sean Huntc3021132010-05-05 15:23:54 +00002426
Anders Carlsson19879c92010-03-23 17:17:29 +00002427 // Mangle the 'this' pointer adjustment.
2428 Mangler.mangleCallOffset(Thunk.This.NonVirtual, Thunk.This.VCallOffsetOffset);
Sean Huntc3021132010-05-05 15:23:54 +00002429
Anders Carlsson19879c92010-03-23 17:17:29 +00002430 // Mangle the return pointer adjustment if there is one.
2431 if (!Thunk.Return.isEmpty())
2432 Mangler.mangleCallOffset(Thunk.Return.NonVirtual,
2433 Thunk.Return.VBaseOffsetOffset);
Sean Huntc3021132010-05-05 15:23:54 +00002434
Anders Carlsson19879c92010-03-23 17:17:29 +00002435 Mangler.mangleFunctionEncoding(MD);
2436}
2437
Sean Huntc3021132010-05-05 15:23:54 +00002438void
Anders Carlsson19879c92010-03-23 17:17:29 +00002439MangleContext::mangleCXXDtorThunk(const CXXDestructorDecl *DD, CXXDtorType Type,
2440 const ThisAdjustment &ThisAdjustment,
2441 llvm::SmallVectorImpl<char> &Res) {
2442 // <special-name> ::= T <call-offset> <base encoding>
2443 // # base is the nominal target function of thunk
Sean Huntc3021132010-05-05 15:23:54 +00002444
Anders Carlsson19879c92010-03-23 17:17:29 +00002445 CXXNameMangler Mangler(*this, Res, DD, Type);
2446 Mangler.getStream() << "_ZT";
2447
2448 // Mangle the 'this' pointer adjustment.
Sean Huntc3021132010-05-05 15:23:54 +00002449 Mangler.mangleCallOffset(ThisAdjustment.NonVirtual,
Anders Carlsson19879c92010-03-23 17:17:29 +00002450 ThisAdjustment.VCallOffsetOffset);
2451
2452 Mangler.mangleFunctionEncoding(DD);
2453}
2454
Daniel Dunbarc0747712009-11-21 09:12:13 +00002455/// mangleGuardVariable - Returns the mangled name for a guard variable
2456/// for the passed in VarDecl.
John McCall5cd91b52010-09-08 01:44:27 +00002457void MangleContext::mangleItaniumGuardVariable(const VarDecl *D,
2458 llvm::SmallVectorImpl<char> &Res) {
Daniel Dunbarc0747712009-11-21 09:12:13 +00002459 // <special-name> ::= GV <object name> # Guard variable for one-time
2460 // # initialization
2461 CXXNameMangler Mangler(*this, Res);
2462 Mangler.getStream() << "_ZGV";
2463 Mangler.mangleName(D);
2464}
2465
Anders Carlsson715edf22010-06-26 16:09:40 +00002466void MangleContext::mangleReferenceTemporary(const VarDecl *D,
2467 llvm::SmallVectorImpl<char> &Res) {
2468 // We match the GCC mangling here.
2469 // <special-name> ::= GR <object name>
2470 CXXNameMangler Mangler(*this, Res);
2471 Mangler.getStream() << "_ZGR";
2472 Mangler.mangleName(D);
2473}
2474
Anders Carlsson046c2942010-04-17 20:15:18 +00002475void MangleContext::mangleCXXVTable(const CXXRecordDecl *RD,
Daniel Dunbar94fd26d2009-11-21 09:06:22 +00002476 llvm::SmallVectorImpl<char> &Res) {
Daniel Dunbarc0747712009-11-21 09:12:13 +00002477 // <special-name> ::= TV <type> # virtual table
Daniel Dunbar94fd26d2009-11-21 09:06:22 +00002478 CXXNameMangler Mangler(*this, Res);
Daniel Dunbarc0747712009-11-21 09:12:13 +00002479 Mangler.getStream() << "_ZTV";
Douglas Gregor1b12a3b2010-05-26 05:11:13 +00002480 Mangler.mangleNameOrStandardSubstitution(RD);
Daniel Dunbar1b077112009-11-21 09:06:10 +00002481}
Mike Stump82d75b02009-11-10 01:58:37 +00002482
Daniel Dunbar1b077112009-11-21 09:06:10 +00002483void MangleContext::mangleCXXVTT(const CXXRecordDecl *RD,
Daniel Dunbar94fd26d2009-11-21 09:06:22 +00002484 llvm::SmallVectorImpl<char> &Res) {
Daniel Dunbarc0747712009-11-21 09:12:13 +00002485 // <special-name> ::= TT <type> # VTT structure
Daniel Dunbar94fd26d2009-11-21 09:06:22 +00002486 CXXNameMangler Mangler(*this, Res);
Daniel Dunbarc0747712009-11-21 09:12:13 +00002487 Mangler.getStream() << "_ZTT";
Douglas Gregor1b12a3b2010-05-26 05:11:13 +00002488 Mangler.mangleNameOrStandardSubstitution(RD);
Daniel Dunbar1b077112009-11-21 09:06:10 +00002489}
Mike Stumpab3f7e92009-11-10 01:41:59 +00002490
Anders Carlsson046c2942010-04-17 20:15:18 +00002491void MangleContext::mangleCXXCtorVTable(const CXXRecordDecl *RD, int64_t Offset,
Daniel Dunbar1b077112009-11-21 09:06:10 +00002492 const CXXRecordDecl *Type,
Daniel Dunbar94fd26d2009-11-21 09:06:22 +00002493 llvm::SmallVectorImpl<char> &Res) {
Daniel Dunbarc0747712009-11-21 09:12:13 +00002494 // <special-name> ::= TC <type> <offset number> _ <base type>
Daniel Dunbar94fd26d2009-11-21 09:06:22 +00002495 CXXNameMangler Mangler(*this, Res);
Daniel Dunbarc0747712009-11-21 09:12:13 +00002496 Mangler.getStream() << "_ZTC";
Douglas Gregor1b12a3b2010-05-26 05:11:13 +00002497 Mangler.mangleNameOrStandardSubstitution(RD);
Daniel Dunbarc0747712009-11-21 09:12:13 +00002498 Mangler.getStream() << Offset;
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002499 Mangler.getStream() << '_';
Douglas Gregor1b12a3b2010-05-26 05:11:13 +00002500 Mangler.mangleNameOrStandardSubstitution(Type);
Daniel Dunbar1b077112009-11-21 09:06:10 +00002501}
Mike Stump738f8c22009-07-31 23:15:31 +00002502
Mike Stumpde050572009-12-02 18:57:08 +00002503void MangleContext::mangleCXXRTTI(QualType Ty,
Daniel Dunbar94fd26d2009-11-21 09:06:22 +00002504 llvm::SmallVectorImpl<char> &Res) {
Daniel Dunbarc0747712009-11-21 09:12:13 +00002505 // <special-name> ::= TI <type> # typeinfo structure
Douglas Gregor154fe982009-12-23 22:04:40 +00002506 assert(!Ty.hasQualifiers() && "RTTI info cannot have top-level qualifiers");
Daniel Dunbar94fd26d2009-11-21 09:06:22 +00002507 CXXNameMangler Mangler(*this, Res);
Daniel Dunbarc0747712009-11-21 09:12:13 +00002508 Mangler.getStream() << "_ZTI";
2509 Mangler.mangleType(Ty);
Daniel Dunbar1b077112009-11-21 09:06:10 +00002510}
Mike Stump67795982009-11-14 00:14:13 +00002511
Mike Stumpde050572009-12-02 18:57:08 +00002512void MangleContext::mangleCXXRTTIName(QualType Ty,
Daniel Dunbar94fd26d2009-11-21 09:06:22 +00002513 llvm::SmallVectorImpl<char> &Res) {
Daniel Dunbarc0747712009-11-21 09:12:13 +00002514 // <special-name> ::= TS <type> # typeinfo name (null terminated byte string)
Daniel Dunbar94fd26d2009-11-21 09:06:22 +00002515 CXXNameMangler Mangler(*this, Res);
Daniel Dunbarc0747712009-11-21 09:12:13 +00002516 Mangler.getStream() << "_ZTS";
2517 Mangler.mangleType(Ty);
Mike Stumpf1216772009-07-31 18:25:34 +00002518}