blob: d1e3a98789e703e7f4051a26d2b3749011f06ed6 [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
Anders Carlssone2923682010-11-04 04:31:32 +00001222 // ::= Dn # std::nullptr_t (i.e., decltype(nullptr))
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001223 // ::= u <source-name> # vendor extended type
1224 switch (T->getKind()) {
1225 case BuiltinType::Void: Out << 'v'; break;
1226 case BuiltinType::Bool: Out << 'b'; break;
1227 case BuiltinType::Char_U: case BuiltinType::Char_S: Out << 'c'; break;
1228 case BuiltinType::UChar: Out << 'h'; break;
1229 case BuiltinType::UShort: Out << 't'; break;
1230 case BuiltinType::UInt: Out << 'j'; break;
1231 case BuiltinType::ULong: Out << 'm'; break;
1232 case BuiltinType::ULongLong: Out << 'y'; break;
Chris Lattner2df9ced2009-04-30 02:43:43 +00001233 case BuiltinType::UInt128: Out << 'o'; break;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001234 case BuiltinType::SChar: Out << 'a'; break;
1235 case BuiltinType::WChar: Out << 'w'; break;
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001236 case BuiltinType::Char16: Out << "Ds"; break;
1237 case BuiltinType::Char32: Out << "Di"; break;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001238 case BuiltinType::Short: Out << 's'; break;
1239 case BuiltinType::Int: Out << 'i'; break;
1240 case BuiltinType::Long: Out << 'l'; break;
1241 case BuiltinType::LongLong: Out << 'x'; break;
Chris Lattner2df9ced2009-04-30 02:43:43 +00001242 case BuiltinType::Int128: Out << 'n'; break;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001243 case BuiltinType::Float: Out << 'f'; break;
1244 case BuiltinType::Double: Out << 'd'; break;
1245 case BuiltinType::LongDouble: Out << 'e'; break;
Anders Carlssone2923682010-11-04 04:31:32 +00001246 case BuiltinType::NullPtr: Out << "Dn"; break;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001247
1248 case BuiltinType::Overload:
1249 case BuiltinType::Dependent:
Mike Stump1eb44332009-09-09 15:08:12 +00001250 assert(false &&
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001251 "Overloaded and dependent types shouldn't get to name mangling");
1252 break;
Anders Carlssone89d1592009-06-26 18:41:36 +00001253 case BuiltinType::UndeducedAuto:
1254 assert(0 && "Should not see undeduced auto here");
1255 break;
Steve Naroff9533a7f2009-07-22 17:14:51 +00001256 case BuiltinType::ObjCId: Out << "11objc_object"; break;
1257 case BuiltinType::ObjCClass: Out << "10objc_class"; break;
Fariborz Jahanian13dcd002009-11-21 19:53:08 +00001258 case BuiltinType::ObjCSel: Out << "13objc_selector"; break;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001259 }
1260}
1261
John McCallefe6aee2009-09-05 07:56:18 +00001262// <type> ::= <function-type>
1263// <function-type> ::= F [Y] <bare-function-type> E
1264void CXXNameMangler::mangleType(const FunctionProtoType *T) {
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001265 Out << 'F';
Mike Stumpf5408fe2009-05-16 07:57:57 +00001266 // FIXME: We don't have enough information in the AST to produce the 'Y'
1267 // encoding for extern "C" function types.
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001268 mangleBareFunctionType(T, /*MangleReturnType=*/true);
1269 Out << 'E';
1270}
John McCallefe6aee2009-09-05 07:56:18 +00001271void CXXNameMangler::mangleType(const FunctionNoProtoType *T) {
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +00001272 llvm_unreachable("Can't mangle K&R function prototypes");
John McCallefe6aee2009-09-05 07:56:18 +00001273}
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001274void CXXNameMangler::mangleBareFunctionType(const FunctionType *T,
1275 bool MangleReturnType) {
John McCallefe6aee2009-09-05 07:56:18 +00001276 // We should never be mangling something without a prototype.
1277 const FunctionProtoType *Proto = cast<FunctionProtoType>(T);
1278
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001279 // <bare-function-type> ::= <signature type>+
1280 if (MangleReturnType)
John McCallefe6aee2009-09-05 07:56:18 +00001281 mangleType(Proto->getResultType());
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001282
Anders Carlsson93296682010-06-02 04:40:13 +00001283 if (Proto->getNumArgs() == 0 && !Proto->isVariadic()) {
Eli Friedmana7e68452010-08-22 01:00:03 +00001284 // <builtin-type> ::= v # void
Anders Carlssonc6c91bc2009-04-01 00:15:23 +00001285 Out << 'v';
1286 return;
1287 }
Mike Stump1eb44332009-09-09 15:08:12 +00001288
Douglas Gregor72564e72009-02-26 23:50:07 +00001289 for (FunctionProtoType::arg_type_iterator Arg = Proto->arg_type_begin(),
Mike Stump1eb44332009-09-09 15:08:12 +00001290 ArgEnd = Proto->arg_type_end();
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001291 Arg != ArgEnd; ++Arg)
1292 mangleType(*Arg);
Douglas Gregor219cc612009-02-13 01:28:03 +00001293
1294 // <builtin-type> ::= z # ellipsis
1295 if (Proto->isVariadic())
1296 Out << 'z';
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001297}
1298
John McCallefe6aee2009-09-05 07:56:18 +00001299// <type> ::= <class-enum-type>
Mike Stump1eb44332009-09-09 15:08:12 +00001300// <class-enum-type> ::= <name>
John McCalled976492009-12-04 22:46:56 +00001301void CXXNameMangler::mangleType(const UnresolvedUsingType *T) {
1302 mangleName(T->getDecl());
1303}
1304
1305// <type> ::= <class-enum-type>
1306// <class-enum-type> ::= <name>
John McCallefe6aee2009-09-05 07:56:18 +00001307void CXXNameMangler::mangleType(const EnumType *T) {
1308 mangleType(static_cast<const TagType*>(T));
1309}
1310void CXXNameMangler::mangleType(const RecordType *T) {
1311 mangleType(static_cast<const TagType*>(T));
1312}
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001313void CXXNameMangler::mangleType(const TagType *T) {
Eli Friedmanecb7e932009-12-11 18:00:57 +00001314 mangleName(T->getDecl());
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001315}
1316
John McCallefe6aee2009-09-05 07:56:18 +00001317// <type> ::= <array-type>
1318// <array-type> ::= A <positive dimension number> _ <element type>
1319// ::= A [<dimension expression>] _ <element type>
1320void CXXNameMangler::mangleType(const ConstantArrayType *T) {
1321 Out << 'A' << T->getSize() << '_';
1322 mangleType(T->getElementType());
1323}
1324void CXXNameMangler::mangleType(const VariableArrayType *T) {
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001325 Out << 'A';
Fariborz Jahanian7281d1f2010-11-02 16:54:00 +00001326 // decayed vla types (size 0) will just be skipped.
1327 if (T->getSizeExpr())
1328 mangleExpression(T->getSizeExpr());
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001329 Out << '_';
1330 mangleType(T->getElementType());
1331}
John McCallefe6aee2009-09-05 07:56:18 +00001332void CXXNameMangler::mangleType(const DependentSizedArrayType *T) {
1333 Out << 'A';
1334 mangleExpression(T->getSizeExpr());
1335 Out << '_';
1336 mangleType(T->getElementType());
1337}
1338void CXXNameMangler::mangleType(const IncompleteArrayType *T) {
Nick Lewycky271b6652010-09-05 03:40:33 +00001339 Out << "A_";
John McCallefe6aee2009-09-05 07:56:18 +00001340 mangleType(T->getElementType());
1341}
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001342
John McCallefe6aee2009-09-05 07:56:18 +00001343// <type> ::= <pointer-to-member-type>
1344// <pointer-to-member-type> ::= M <class type> <member type>
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001345void CXXNameMangler::mangleType(const MemberPointerType *T) {
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001346 Out << 'M';
1347 mangleType(QualType(T->getClass(), 0));
Anders Carlsson0e650012009-05-17 17:41:20 +00001348 QualType PointeeType = T->getPointeeType();
1349 if (const FunctionProtoType *FPT = dyn_cast<FunctionProtoType>(PointeeType)) {
John McCall0953e762009-09-24 19:53:00 +00001350 mangleQualifiers(Qualifiers::fromCVRMask(FPT->getTypeQuals()));
Anders Carlsson0e650012009-05-17 17:41:20 +00001351 mangleType(FPT);
Anders Carlsson9d85b722010-06-02 04:29:50 +00001352
1353 // Itanium C++ ABI 5.1.8:
1354 //
1355 // The type of a non-static member function is considered to be different,
1356 // for the purposes of substitution, from the type of a namespace-scope or
1357 // static member function whose type appears similar. The types of two
1358 // non-static member functions are considered to be different, for the
1359 // purposes of substitution, if the functions are members of different
1360 // classes. In other words, for the purposes of substitution, the class of
1361 // which the function is a member is considered part of the type of
1362 // function.
1363
1364 // We increment the SeqID here to emulate adding an entry to the
1365 // substitution table. We can't actually add it because we don't want this
1366 // particular function type to be substituted.
1367 ++SeqID;
Mike Stump1eb44332009-09-09 15:08:12 +00001368 } else
Anders Carlsson0e650012009-05-17 17:41:20 +00001369 mangleType(PointeeType);
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001370}
1371
John McCallefe6aee2009-09-05 07:56:18 +00001372// <type> ::= <template-param>
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001373void CXXNameMangler::mangleType(const TemplateTypeParmType *T) {
Anders Carlsson0ccdf8d2009-09-27 00:38:53 +00001374 mangleTemplateParameter(T->getIndex());
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001375}
1376
John McCallefe6aee2009-09-05 07:56:18 +00001377// <type> ::= P <type> # pointer-to
1378void CXXNameMangler::mangleType(const PointerType *T) {
1379 Out << 'P';
1380 mangleType(T->getPointeeType());
1381}
1382void CXXNameMangler::mangleType(const ObjCObjectPointerType *T) {
1383 Out << 'P';
1384 mangleType(T->getPointeeType());
1385}
1386
1387// <type> ::= R <type> # reference-to
1388void CXXNameMangler::mangleType(const LValueReferenceType *T) {
1389 Out << 'R';
1390 mangleType(T->getPointeeType());
1391}
1392
1393// <type> ::= O <type> # rvalue reference-to (C++0x)
1394void CXXNameMangler::mangleType(const RValueReferenceType *T) {
1395 Out << 'O';
1396 mangleType(T->getPointeeType());
1397}
1398
1399// <type> ::= C <type> # complex pair (C 2000)
1400void CXXNameMangler::mangleType(const ComplexType *T) {
1401 Out << 'C';
1402 mangleType(T->getElementType());
1403}
1404
1405// GNU extension: vector types
Chris Lattner788b0fd2010-06-23 06:00:24 +00001406// <type> ::= <vector-type>
1407// <vector-type> ::= Dv <positive dimension number> _
1408// <extended element type>
1409// ::= Dv [<dimension expression>] _ <element type>
1410// <extended element type> ::= <element type>
1411// ::= p # AltiVec vector pixel
John McCallefe6aee2009-09-05 07:56:18 +00001412void CXXNameMangler::mangleType(const VectorType *T) {
Nick Lewycky0e5f0672010-03-26 07:18:04 +00001413 Out << "Dv" << T->getNumElements() << '_';
Chris Lattner788b0fd2010-06-23 06:00:24 +00001414 if (T->getAltiVecSpecific() == VectorType::Pixel)
1415 Out << 'p';
1416 else if (T->getAltiVecSpecific() == VectorType::Bool)
1417 Out << 'b';
1418 else
1419 mangleType(T->getElementType());
John McCallefe6aee2009-09-05 07:56:18 +00001420}
1421void CXXNameMangler::mangleType(const ExtVectorType *T) {
1422 mangleType(static_cast<const VectorType*>(T));
1423}
1424void CXXNameMangler::mangleType(const DependentSizedExtVectorType *T) {
Nick Lewycky0e5f0672010-03-26 07:18:04 +00001425 Out << "Dv";
1426 mangleExpression(T->getSizeExpr());
1427 Out << '_';
John McCallefe6aee2009-09-05 07:56:18 +00001428 mangleType(T->getElementType());
1429}
1430
Anders Carlssona40c5e42009-03-07 22:03:21 +00001431void CXXNameMangler::mangleType(const ObjCInterfaceType *T) {
1432 mangleSourceName(T->getDecl()->getIdentifier());
1433}
1434
John McCallc12c5bb2010-05-15 11:32:37 +00001435void CXXNameMangler::mangleType(const ObjCObjectType *T) {
John McCallc00c1f62010-05-15 17:06:29 +00001436 // We don't allow overloading by different protocol qualification,
1437 // so mangling them isn't necessary.
John McCallc12c5bb2010-05-15 11:32:37 +00001438 mangleType(T->getBaseType());
1439}
1440
John McCallefe6aee2009-09-05 07:56:18 +00001441void CXXNameMangler::mangleType(const BlockPointerType *T) {
Anders Carlssonf28c6872009-12-23 22:31:44 +00001442 Out << "U13block_pointer";
1443 mangleType(T->getPointeeType());
John McCallefe6aee2009-09-05 07:56:18 +00001444}
1445
John McCall31f17ec2010-04-27 00:57:59 +00001446void CXXNameMangler::mangleType(const InjectedClassNameType *T) {
1447 // Mangle injected class name types as if the user had written the
1448 // specialization out fully. It may not actually be possible to see
1449 // this mangling, though.
1450 mangleType(T->getInjectedSpecializationType());
1451}
1452
John McCallefe6aee2009-09-05 07:56:18 +00001453void CXXNameMangler::mangleType(const TemplateSpecializationType *T) {
Douglas Gregor1e9268e2010-04-28 05:58:56 +00001454 if (TemplateDecl *TD = T->getTemplateName().getAsTemplateDecl()) {
1455 mangleName(TD, T->getArgs(), T->getNumArgs());
1456 } else {
1457 if (mangleSubstitution(QualType(T, 0)))
1458 return;
Sean Huntc3021132010-05-05 15:23:54 +00001459
Douglas Gregor1e9268e2010-04-28 05:58:56 +00001460 mangleTemplatePrefix(T->getTemplateName());
Sean Huntc3021132010-05-05 15:23:54 +00001461
Douglas Gregor1e9268e2010-04-28 05:58:56 +00001462 // FIXME: GCC does not appear to mangle the template arguments when
1463 // the template in question is a dependent template name. Should we
1464 // emulate that badness?
1465 mangleTemplateArgs(T->getTemplateName(), T->getArgs(), T->getNumArgs());
1466 addSubstitution(QualType(T, 0));
1467 }
John McCallefe6aee2009-09-05 07:56:18 +00001468}
1469
Douglas Gregor4714c122010-03-31 17:34:00 +00001470void CXXNameMangler::mangleType(const DependentNameType *T) {
Anders Carlssonae352482009-09-26 02:26:02 +00001471 // Typename types are always nested
1472 Out << 'N';
John McCall33500952010-06-11 00:33:02 +00001473 mangleUnresolvedScope(T->getQualifier());
1474 mangleSourceName(T->getIdentifier());
1475 Out << 'E';
1476}
John McCall6ab30e02010-06-09 07:26:17 +00001477
John McCall33500952010-06-11 00:33:02 +00001478void CXXNameMangler::mangleType(const DependentTemplateSpecializationType *T) {
1479 // Dependently-scoped template types are always nested
1480 Out << 'N';
1481
1482 // TODO: avoid making this TemplateName.
1483 TemplateName Prefix =
1484 getASTContext().getDependentTemplateName(T->getQualifier(),
1485 T->getIdentifier());
1486 mangleTemplatePrefix(Prefix);
1487
1488 // FIXME: GCC does not appear to mangle the template arguments when
1489 // the template in question is a dependent template name. Should we
1490 // emulate that badness?
1491 mangleTemplateArgs(Prefix, T->getArgs(), T->getNumArgs());
Anders Carlssonae352482009-09-26 02:26:02 +00001492 Out << 'E';
John McCallefe6aee2009-09-05 07:56:18 +00001493}
1494
John McCallad5e7382010-03-01 23:49:17 +00001495void CXXNameMangler::mangleType(const TypeOfType *T) {
1496 // FIXME: this is pretty unsatisfactory, but there isn't an obvious
1497 // "extension with parameters" mangling.
1498 Out << "u6typeof";
1499}
1500
1501void CXXNameMangler::mangleType(const TypeOfExprType *T) {
1502 // FIXME: this is pretty unsatisfactory, but there isn't an obvious
1503 // "extension with parameters" mangling.
1504 Out << "u6typeof";
1505}
1506
1507void CXXNameMangler::mangleType(const DecltypeType *T) {
1508 Expr *E = T->getUnderlyingExpr();
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001509
John McCallad5e7382010-03-01 23:49:17 +00001510 // type ::= Dt <expression> E # decltype of an id-expression
1511 // # or class member access
1512 // ::= DT <expression> E # decltype of an expression
1513
1514 // This purports to be an exhaustive list of id-expressions and
1515 // class member accesses. Note that we do not ignore parentheses;
1516 // parentheses change the semantics of decltype for these
1517 // expressions (and cause the mangler to use the other form).
1518 if (isa<DeclRefExpr>(E) ||
1519 isa<MemberExpr>(E) ||
1520 isa<UnresolvedLookupExpr>(E) ||
1521 isa<DependentScopeDeclRefExpr>(E) ||
1522 isa<CXXDependentScopeMemberExpr>(E) ||
1523 isa<UnresolvedMemberExpr>(E))
1524 Out << "Dt";
1525 else
1526 Out << "DT";
1527 mangleExpression(E);
1528 Out << 'E';
1529}
1530
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001531void CXXNameMangler::mangleIntegerLiteral(QualType T,
Anders Carlssone170ba72009-12-14 01:45:37 +00001532 const llvm::APSInt &Value) {
1533 // <expr-primary> ::= L <type> <value number> E # integer literal
1534 Out << 'L';
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001535
Anders Carlssone170ba72009-12-14 01:45:37 +00001536 mangleType(T);
1537 if (T->isBooleanType()) {
1538 // Boolean values are encoded as 0/1.
1539 Out << (Value.getBoolValue() ? '1' : '0');
1540 } else {
John McCall0512e482010-07-14 04:20:34 +00001541 mangleNumber(Value);
Anders Carlssone170ba72009-12-14 01:45:37 +00001542 }
1543 Out << 'E';
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001544
Anders Carlssone170ba72009-12-14 01:45:37 +00001545}
1546
John McCall2f27bf82010-02-04 02:56:29 +00001547/// Mangles a member expression. Implicit accesses are not handled,
1548/// but that should be okay, because you shouldn't be able to
1549/// make an implicit access in a function template declaration.
John McCall2f27bf82010-02-04 02:56:29 +00001550void CXXNameMangler::mangleMemberExpr(const Expr *Base,
1551 bool IsArrow,
1552 NestedNameSpecifier *Qualifier,
1553 DeclarationName Member,
1554 unsigned Arity) {
John McCalle1e342f2010-03-01 19:12:25 +00001555 // gcc-4.4 uses 'dt' for dot expressions, which is reasonable.
1556 // OTOH, gcc also mangles the name as an expression.
1557 Out << (IsArrow ? "pt" : "dt");
John McCall2f27bf82010-02-04 02:56:29 +00001558 mangleExpression(Base);
1559 mangleUnresolvedName(Qualifier, Member, Arity);
1560}
1561
John McCall5e1e89b2010-08-18 19:18:59 +00001562void CXXNameMangler::mangleExpression(const Expr *E, unsigned Arity) {
Anders Carlssond553f8c2009-09-21 01:21:10 +00001563 // <expression> ::= <unary operator-name> <expression>
John McCall09cc1412010-02-03 00:55:45 +00001564 // ::= <binary operator-name> <expression> <expression>
1565 // ::= <trinary operator-name> <expression> <expression> <expression>
Eli Friedmana7e68452010-08-22 01:00:03 +00001566 // ::= cl <expression>* E # call
Anders Carlssond553f8c2009-09-21 01:21:10 +00001567 // ::= cv <type> expression # conversion with one argument
1568 // ::= cv <type> _ <expression>* E # conversion with a different number of arguments
Eli Friedmana7e68452010-08-22 01:00:03 +00001569 // ::= st <type> # sizeof (a type)
Anders Carlssond553f8c2009-09-21 01:21:10 +00001570 // ::= at <type> # alignof (a type)
1571 // ::= <template-param>
1572 // ::= <function-param>
1573 // ::= sr <type> <unqualified-name> # dependent name
1574 // ::= sr <type> <unqualified-name> <template-args> # dependent template-id
1575 // ::= sZ <template-param> # size of a parameter pack
John McCall09cc1412010-02-03 00:55:45 +00001576 // ::= <expr-primary>
John McCall1dd73832010-02-04 01:42:13 +00001577 // <expr-primary> ::= L <type> <value number> E # integer literal
1578 // ::= L <type <value float> E # floating literal
1579 // ::= L <mangled-name> E # external name
Anders Carlssond553f8c2009-09-21 01:21:10 +00001580 switch (E->getStmtClass()) {
John McCall6ae1f352010-04-09 22:26:14 +00001581 case Expr::NoStmtClass:
1582#define EXPR(Type, Base)
1583#define STMT(Type, Base) \
1584 case Expr::Type##Class:
Sean Hunt4bfe1962010-05-05 15:24:00 +00001585#include "clang/AST/StmtNodes.inc"
John McCall0512e482010-07-14 04:20:34 +00001586 // fallthrough
1587
1588 // These all can only appear in local or variable-initialization
1589 // contexts and so should never appear in a mangling.
1590 case Expr::AddrLabelExprClass:
1591 case Expr::BlockDeclRefExprClass:
1592 case Expr::CXXThisExprClass:
1593 case Expr::DesignatedInitExprClass:
1594 case Expr::ImplicitValueInitExprClass:
1595 case Expr::InitListExprClass:
1596 case Expr::ParenListExprClass:
1597 case Expr::CXXScalarValueInitExprClass:
John McCall09cc1412010-02-03 00:55:45 +00001598 llvm_unreachable("unexpected statement kind");
1599 break;
1600
John McCall0512e482010-07-14 04:20:34 +00001601 // FIXME: invent manglings for all these.
1602 case Expr::BlockExprClass:
1603 case Expr::CXXPseudoDestructorExprClass:
1604 case Expr::ChooseExprClass:
1605 case Expr::CompoundLiteralExprClass:
1606 case Expr::ExtVectorElementExprClass:
1607 case Expr::ObjCEncodeExprClass:
1608 case Expr::ObjCImplicitSetterGetterRefExprClass:
1609 case Expr::ObjCIsaExprClass:
1610 case Expr::ObjCIvarRefExprClass:
1611 case Expr::ObjCMessageExprClass:
1612 case Expr::ObjCPropertyRefExprClass:
1613 case Expr::ObjCProtocolExprClass:
1614 case Expr::ObjCSelectorExprClass:
1615 case Expr::ObjCStringLiteralClass:
John McCall0512e482010-07-14 04:20:34 +00001616 case Expr::OffsetOfExprClass:
1617 case Expr::PredefinedExprClass:
1618 case Expr::ShuffleVectorExprClass:
1619 case Expr::StmtExprClass:
1620 case Expr::TypesCompatibleExprClass:
1621 case Expr::UnaryTypeTraitExprClass:
Francois Pichet9be88402010-09-08 23:47:05 +00001622 case Expr::VAArgExprClass:
Sebastian Redl2e156222010-09-10 20:55:43 +00001623 case Expr::CXXUuidofExprClass:
1624 case Expr::CXXNoexceptExprClass: {
John McCall6ae1f352010-04-09 22:26:14 +00001625 // As bad as this diagnostic is, it's better than crashing.
1626 Diagnostic &Diags = Context.getDiags();
1627 unsigned DiagID = Diags.getCustomDiagID(Diagnostic::Error,
1628 "cannot yet mangle expression type %0");
John McCall739bf092010-04-10 09:39:25 +00001629 Diags.Report(FullSourceLoc(E->getExprLoc(),
1630 getASTContext().getSourceManager()),
1631 DiagID)
1632 << E->getStmtClassName() << E->getSourceRange();
John McCall6ae1f352010-04-09 22:26:14 +00001633 break;
1634 }
1635
John McCall0512e482010-07-14 04:20:34 +00001636 case Expr::CXXDefaultArgExprClass:
John McCall5e1e89b2010-08-18 19:18:59 +00001637 mangleExpression(cast<CXXDefaultArgExpr>(E)->getExpr(), Arity);
John McCall0512e482010-07-14 04:20:34 +00001638 break;
1639
1640 case Expr::CXXMemberCallExprClass: // fallthrough
John McCall1dd73832010-02-04 01:42:13 +00001641 case Expr::CallExprClass: {
1642 const CallExpr *CE = cast<CallExpr>(E);
1643 Out << "cl";
John McCall5e1e89b2010-08-18 19:18:59 +00001644 mangleExpression(CE->getCallee(), CE->getNumArgs());
John McCall1dd73832010-02-04 01:42:13 +00001645 for (unsigned I = 0, N = CE->getNumArgs(); I != N; ++I)
1646 mangleExpression(CE->getArg(I));
Benjamin Kramer35f59b62010-04-10 16:03:31 +00001647 Out << 'E';
John McCall09cc1412010-02-03 00:55:45 +00001648 break;
John McCall1dd73832010-02-04 01:42:13 +00001649 }
John McCall09cc1412010-02-03 00:55:45 +00001650
John McCall0512e482010-07-14 04:20:34 +00001651 case Expr::CXXNewExprClass: {
1652 // Proposal from David Vandervoorde, 2010.06.30
1653 const CXXNewExpr *New = cast<CXXNewExpr>(E);
1654 if (New->isGlobalNew()) Out << "gs";
1655 Out << (New->isArray() ? "na" : "nw");
1656 for (CXXNewExpr::const_arg_iterator I = New->placement_arg_begin(),
1657 E = New->placement_arg_end(); I != E; ++I)
1658 mangleExpression(*I);
1659 Out << '_';
1660 mangleType(New->getAllocatedType());
1661 if (New->hasInitializer()) {
1662 Out << "pi";
1663 for (CXXNewExpr::const_arg_iterator I = New->constructor_arg_begin(),
1664 E = New->constructor_arg_end(); I != E; ++I)
1665 mangleExpression(*I);
1666 }
1667 Out << 'E';
1668 break;
1669 }
1670
John McCall2f27bf82010-02-04 02:56:29 +00001671 case Expr::MemberExprClass: {
1672 const MemberExpr *ME = cast<MemberExpr>(E);
1673 mangleMemberExpr(ME->getBase(), ME->isArrow(),
1674 ME->getQualifier(), ME->getMemberDecl()->getDeclName(),
John McCall5e1e89b2010-08-18 19:18:59 +00001675 Arity);
John McCall2f27bf82010-02-04 02:56:29 +00001676 break;
1677 }
1678
1679 case Expr::UnresolvedMemberExprClass: {
1680 const UnresolvedMemberExpr *ME = cast<UnresolvedMemberExpr>(E);
1681 mangleMemberExpr(ME->getBase(), ME->isArrow(),
1682 ME->getQualifier(), ME->getMemberName(),
John McCall5e1e89b2010-08-18 19:18:59 +00001683 Arity);
John McCall6dbce192010-08-20 00:17:19 +00001684 if (ME->hasExplicitTemplateArgs())
1685 mangleTemplateArgs(ME->getExplicitTemplateArgs());
John McCall2f27bf82010-02-04 02:56:29 +00001686 break;
1687 }
1688
1689 case Expr::CXXDependentScopeMemberExprClass: {
1690 const CXXDependentScopeMemberExpr *ME
1691 = cast<CXXDependentScopeMemberExpr>(E);
1692 mangleMemberExpr(ME->getBase(), ME->isArrow(),
1693 ME->getQualifier(), ME->getMember(),
John McCall5e1e89b2010-08-18 19:18:59 +00001694 Arity);
John McCall6dbce192010-08-20 00:17:19 +00001695 if (ME->hasExplicitTemplateArgs())
1696 mangleTemplateArgs(ME->getExplicitTemplateArgs());
John McCall2f27bf82010-02-04 02:56:29 +00001697 break;
1698 }
1699
John McCall1dd73832010-02-04 01:42:13 +00001700 case Expr::UnresolvedLookupExprClass: {
John McCalla3218e72010-02-04 01:48:38 +00001701 // The ABI doesn't cover how to mangle overload sets, so we mangle
1702 // using something as close as possible to the original lookup
1703 // expression.
John McCall1dd73832010-02-04 01:42:13 +00001704 const UnresolvedLookupExpr *ULE = cast<UnresolvedLookupExpr>(E);
John McCall5e1e89b2010-08-18 19:18:59 +00001705 mangleUnresolvedName(ULE->getQualifier(), ULE->getName(), Arity);
John McCall6dbce192010-08-20 00:17:19 +00001706 if (ULE->hasExplicitTemplateArgs())
1707 mangleTemplateArgs(ULE->getExplicitTemplateArgs());
John McCall1dd73832010-02-04 01:42:13 +00001708 break;
1709 }
1710
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001711 case Expr::CXXUnresolvedConstructExprClass: {
John McCall1dd73832010-02-04 01:42:13 +00001712 const CXXUnresolvedConstructExpr *CE = cast<CXXUnresolvedConstructExpr>(E);
1713 unsigned N = CE->arg_size();
1714
1715 Out << "cv";
1716 mangleType(CE->getType());
Benjamin Kramer35f59b62010-04-10 16:03:31 +00001717 if (N != 1) Out << '_';
John McCall1dd73832010-02-04 01:42:13 +00001718 for (unsigned I = 0; I != N; ++I) mangleExpression(CE->getArg(I));
Benjamin Kramer35f59b62010-04-10 16:03:31 +00001719 if (N != 1) Out << 'E';
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001720 break;
John McCall1dd73832010-02-04 01:42:13 +00001721 }
John McCall09cc1412010-02-03 00:55:45 +00001722
John McCall1dd73832010-02-04 01:42:13 +00001723 case Expr::CXXTemporaryObjectExprClass:
1724 case Expr::CXXConstructExprClass: {
1725 const CXXConstructExpr *CE = cast<CXXConstructExpr>(E);
1726 unsigned N = CE->getNumArgs();
1727
1728 Out << "cv";
1729 mangleType(CE->getType());
Benjamin Kramer35f59b62010-04-10 16:03:31 +00001730 if (N != 1) Out << '_';
John McCall1dd73832010-02-04 01:42:13 +00001731 for (unsigned I = 0; I != N; ++I) mangleExpression(CE->getArg(I));
Benjamin Kramer35f59b62010-04-10 16:03:31 +00001732 if (N != 1) Out << 'E';
John McCall09cc1412010-02-03 00:55:45 +00001733 break;
John McCall1dd73832010-02-04 01:42:13 +00001734 }
1735
1736 case Expr::SizeOfAlignOfExprClass: {
1737 const SizeOfAlignOfExpr *SAE = cast<SizeOfAlignOfExpr>(E);
Benjamin Kramer35f59b62010-04-10 16:03:31 +00001738 if (SAE->isSizeOf()) Out << 's';
1739 else Out << 'a';
John McCall1dd73832010-02-04 01:42:13 +00001740 if (SAE->isArgumentType()) {
Benjamin Kramer35f59b62010-04-10 16:03:31 +00001741 Out << 't';
John McCall1dd73832010-02-04 01:42:13 +00001742 mangleType(SAE->getArgumentType());
1743 } else {
Benjamin Kramer35f59b62010-04-10 16:03:31 +00001744 Out << 'z';
John McCall1dd73832010-02-04 01:42:13 +00001745 mangleExpression(SAE->getArgumentExpr());
1746 }
1747 break;
1748 }
Anders Carlssona7694082009-11-06 02:50:19 +00001749
John McCall0512e482010-07-14 04:20:34 +00001750 case Expr::CXXThrowExprClass: {
1751 const CXXThrowExpr *TE = cast<CXXThrowExpr>(E);
1752
1753 // Proposal from David Vandervoorde, 2010.06.30
1754 if (TE->getSubExpr()) {
1755 Out << "tw";
1756 mangleExpression(TE->getSubExpr());
1757 } else {
1758 Out << "tr";
1759 }
1760 break;
1761 }
1762
1763 case Expr::CXXTypeidExprClass: {
1764 const CXXTypeidExpr *TIE = cast<CXXTypeidExpr>(E);
1765
1766 // Proposal from David Vandervoorde, 2010.06.30
1767 if (TIE->isTypeOperand()) {
1768 Out << "ti";
1769 mangleType(TIE->getTypeOperand());
1770 } else {
1771 Out << "te";
1772 mangleExpression(TIE->getExprOperand());
1773 }
1774 break;
1775 }
1776
1777 case Expr::CXXDeleteExprClass: {
1778 const CXXDeleteExpr *DE = cast<CXXDeleteExpr>(E);
1779
1780 // Proposal from David Vandervoorde, 2010.06.30
1781 if (DE->isGlobalDelete()) Out << "gs";
1782 Out << (DE->isArrayForm() ? "da" : "dl");
1783 mangleExpression(DE->getArgument());
1784 break;
1785 }
1786
Anders Carlssone170ba72009-12-14 01:45:37 +00001787 case Expr::UnaryOperatorClass: {
1788 const UnaryOperator *UO = cast<UnaryOperator>(E);
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001789 mangleOperatorName(UnaryOperator::getOverloadedOperator(UO->getOpcode()),
Anders Carlssone170ba72009-12-14 01:45:37 +00001790 /*Arity=*/1);
1791 mangleExpression(UO->getSubExpr());
1792 break;
1793 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001794
John McCall0512e482010-07-14 04:20:34 +00001795 case Expr::ArraySubscriptExprClass: {
1796 const ArraySubscriptExpr *AE = cast<ArraySubscriptExpr>(E);
1797
1798 // Array subscript is treated as a syntactically wierd form of
1799 // binary operator.
1800 Out << "ix";
1801 mangleExpression(AE->getLHS());
1802 mangleExpression(AE->getRHS());
1803 break;
1804 }
1805
1806 case Expr::CompoundAssignOperatorClass: // fallthrough
Anders Carlssone170ba72009-12-14 01:45:37 +00001807 case Expr::BinaryOperatorClass: {
1808 const BinaryOperator *BO = cast<BinaryOperator>(E);
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001809 mangleOperatorName(BinaryOperator::getOverloadedOperator(BO->getOpcode()),
Anders Carlssone170ba72009-12-14 01:45:37 +00001810 /*Arity=*/2);
1811 mangleExpression(BO->getLHS());
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001812 mangleExpression(BO->getRHS());
Anders Carlssone170ba72009-12-14 01:45:37 +00001813 break;
John McCall2f27bf82010-02-04 02:56:29 +00001814 }
Anders Carlssone170ba72009-12-14 01:45:37 +00001815
1816 case Expr::ConditionalOperatorClass: {
1817 const ConditionalOperator *CO = cast<ConditionalOperator>(E);
1818 mangleOperatorName(OO_Conditional, /*Arity=*/3);
1819 mangleExpression(CO->getCond());
John McCall5e1e89b2010-08-18 19:18:59 +00001820 mangleExpression(CO->getLHS(), Arity);
1821 mangleExpression(CO->getRHS(), Arity);
Anders Carlssone170ba72009-12-14 01:45:37 +00001822 break;
1823 }
1824
Douglas Gregor46287c72010-01-29 16:37:09 +00001825 case Expr::ImplicitCastExprClass: {
John McCall5e1e89b2010-08-18 19:18:59 +00001826 mangleExpression(cast<ImplicitCastExpr>(E)->getSubExpr(), Arity);
Douglas Gregor46287c72010-01-29 16:37:09 +00001827 break;
1828 }
1829
1830 case Expr::CStyleCastExprClass:
1831 case Expr::CXXStaticCastExprClass:
1832 case Expr::CXXDynamicCastExprClass:
1833 case Expr::CXXReinterpretCastExprClass:
1834 case Expr::CXXConstCastExprClass:
1835 case Expr::CXXFunctionalCastExprClass: {
1836 const ExplicitCastExpr *ECE = cast<ExplicitCastExpr>(E);
1837 Out << "cv";
1838 mangleType(ECE->getType());
1839 mangleExpression(ECE->getSubExpr());
1840 break;
1841 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001842
Anders Carlsson58040a52009-12-16 05:48:46 +00001843 case Expr::CXXOperatorCallExprClass: {
1844 const CXXOperatorCallExpr *CE = cast<CXXOperatorCallExpr>(E);
1845 unsigned NumArgs = CE->getNumArgs();
1846 mangleOperatorName(CE->getOperator(), /*Arity=*/NumArgs);
1847 // Mangle the arguments.
1848 for (unsigned i = 0; i != NumArgs; ++i)
1849 mangleExpression(CE->getArg(i));
1850 break;
1851 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001852
Anders Carlssona7694082009-11-06 02:50:19 +00001853 case Expr::ParenExprClass:
John McCall5e1e89b2010-08-18 19:18:59 +00001854 mangleExpression(cast<ParenExpr>(E)->getSubExpr(), Arity);
Anders Carlssona7694082009-11-06 02:50:19 +00001855 break;
1856
Anders Carlssond553f8c2009-09-21 01:21:10 +00001857 case Expr::DeclRefExprClass: {
Douglas Gregor5ed1bc32010-02-28 21:40:32 +00001858 const NamedDecl *D = cast<DeclRefExpr>(E)->getDecl();
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00001859
Anders Carlssond553f8c2009-09-21 01:21:10 +00001860 switch (D->getKind()) {
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001861 default:
Douglas Gregor5ed1bc32010-02-28 21:40:32 +00001862 // <expr-primary> ::= L <mangled-name> E # external name
1863 Out << 'L';
1864 mangle(D, "_Z");
1865 Out << 'E';
1866 break;
1867
John McCall3dc7e7b2010-07-24 01:17:35 +00001868 case Decl::EnumConstant: {
1869 const EnumConstantDecl *ED = cast<EnumConstantDecl>(D);
1870 mangleIntegerLiteral(ED->getType(), ED->getInitVal());
1871 break;
1872 }
1873
Anders Carlssond553f8c2009-09-21 01:21:10 +00001874 case Decl::NonTypeTemplateParm: {
1875 const NonTypeTemplateParmDecl *PD = cast<NonTypeTemplateParmDecl>(D);
Anders Carlsson0ccdf8d2009-09-27 00:38:53 +00001876 mangleTemplateParameter(PD->getIndex());
Anders Carlssond553f8c2009-09-21 01:21:10 +00001877 break;
1878 }
1879
1880 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00001881
Anders Carlsson50755b02009-09-27 20:11:34 +00001882 break;
Anders Carlssond553f8c2009-09-21 01:21:10 +00001883 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00001884
John McCall865d4472009-11-19 22:55:06 +00001885 case Expr::DependentScopeDeclRefExprClass: {
1886 const DependentScopeDeclRefExpr *DRE = cast<DependentScopeDeclRefExpr>(E);
Douglas Gregor4b2ccfc2010-02-28 22:05:49 +00001887 NestedNameSpecifier *NNS = DRE->getQualifier();
1888 const Type *QTy = NNS->getAsType();
1889
1890 // When we're dealing with a nested-name-specifier that has just a
1891 // dependent identifier in it, mangle that as a typename. FIXME:
1892 // It isn't clear that we ever actually want to have such a
1893 // nested-name-specifier; why not just represent it as a typename type?
1894 if (!QTy && NNS->getAsIdentifier() && NNS->getPrefix()) {
Douglas Gregor4a2023f2010-03-31 20:19:30 +00001895 QTy = getASTContext().getDependentNameType(ETK_Typename,
1896 NNS->getPrefix(),
1897 NNS->getAsIdentifier())
Douglas Gregor4b2ccfc2010-02-28 22:05:49 +00001898 .getTypePtr();
1899 }
Anders Carlsson50755b02009-09-27 20:11:34 +00001900 assert(QTy && "Qualifier was not type!");
1901
John McCall6dbce192010-08-20 00:17:19 +00001902 // ::= sr <type> <unqualified-name> # dependent name
1903 // ::= sr <type> <unqualified-name> <template-args> # dependent template-id
Anders Carlsson50755b02009-09-27 20:11:34 +00001904 Out << "sr";
1905 mangleType(QualType(QTy, 0));
John McCall5e1e89b2010-08-18 19:18:59 +00001906 mangleUnqualifiedName(0, DRE->getDeclName(), Arity);
John McCall6dbce192010-08-20 00:17:19 +00001907 if (DRE->hasExplicitTemplateArgs())
1908 mangleTemplateArgs(DRE->getExplicitTemplateArgs());
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00001909
Anders Carlsson50755b02009-09-27 20:11:34 +00001910 break;
1911 }
1912
John McCalld9307602010-04-09 22:54:09 +00001913 case Expr::CXXBindTemporaryExprClass:
1914 mangleExpression(cast<CXXBindTemporaryExpr>(E)->getSubExpr());
1915 break;
1916
1917 case Expr::CXXExprWithTemporariesClass:
John McCall5e1e89b2010-08-18 19:18:59 +00001918 mangleExpression(cast<CXXExprWithTemporaries>(E)->getSubExpr(), Arity);
John McCalld9307602010-04-09 22:54:09 +00001919 break;
1920
John McCall1dd73832010-02-04 01:42:13 +00001921 case Expr::FloatingLiteralClass: {
1922 const FloatingLiteral *FL = cast<FloatingLiteral>(E);
Benjamin Kramer35f59b62010-04-10 16:03:31 +00001923 Out << 'L';
John McCall1dd73832010-02-04 01:42:13 +00001924 mangleType(FL->getType());
John McCall0512e482010-07-14 04:20:34 +00001925 mangleFloat(FL->getValue());
Benjamin Kramer35f59b62010-04-10 16:03:31 +00001926 Out << 'E';
John McCall1dd73832010-02-04 01:42:13 +00001927 break;
1928 }
1929
John McCallde810632010-04-09 21:48:08 +00001930 case Expr::CharacterLiteralClass:
Benjamin Kramer35f59b62010-04-10 16:03:31 +00001931 Out << 'L';
John McCallde810632010-04-09 21:48:08 +00001932 mangleType(E->getType());
1933 Out << cast<CharacterLiteral>(E)->getValue();
1934 Out << 'E';
1935 break;
1936
1937 case Expr::CXXBoolLiteralExprClass:
1938 Out << "Lb";
1939 Out << (cast<CXXBoolLiteralExpr>(E)->getValue() ? '1' : '0');
1940 Out << 'E';
1941 break;
1942
John McCall0512e482010-07-14 04:20:34 +00001943 case Expr::IntegerLiteralClass: {
1944 llvm::APSInt Value(cast<IntegerLiteral>(E)->getValue());
1945 if (E->getType()->isSignedIntegerType())
1946 Value.setIsSigned(true);
1947 mangleIntegerLiteral(E->getType(), Value);
Anders Carlssone170ba72009-12-14 01:45:37 +00001948 break;
John McCall0512e482010-07-14 04:20:34 +00001949 }
1950
1951 case Expr::ImaginaryLiteralClass: {
1952 const ImaginaryLiteral *IE = cast<ImaginaryLiteral>(E);
1953 // Mangle as if a complex literal.
Nick Lewycky271b6652010-09-05 03:40:33 +00001954 // Proposal from David Vandevoorde, 2010.06.30.
John McCall0512e482010-07-14 04:20:34 +00001955 Out << 'L';
1956 mangleType(E->getType());
1957 if (const FloatingLiteral *Imag =
1958 dyn_cast<FloatingLiteral>(IE->getSubExpr())) {
1959 // Mangle a floating-point zero of the appropriate type.
1960 mangleFloat(llvm::APFloat(Imag->getValue().getSemantics()));
1961 Out << '_';
1962 mangleFloat(Imag->getValue());
1963 } else {
Nick Lewycky271b6652010-09-05 03:40:33 +00001964 Out << "0_";
John McCall0512e482010-07-14 04:20:34 +00001965 llvm::APSInt Value(cast<IntegerLiteral>(IE->getSubExpr())->getValue());
1966 if (IE->getSubExpr()->getType()->isSignedIntegerType())
1967 Value.setIsSigned(true);
1968 mangleNumber(Value);
1969 }
1970 Out << 'E';
1971 break;
1972 }
1973
1974 case Expr::StringLiteralClass: {
John McCall1658c392010-07-15 21:53:03 +00001975 // Revised proposal from David Vandervoorde, 2010.07.15.
John McCall0512e482010-07-14 04:20:34 +00001976 Out << 'L';
John McCall1658c392010-07-15 21:53:03 +00001977 assert(isa<ConstantArrayType>(E->getType()));
1978 mangleType(E->getType());
John McCall0512e482010-07-14 04:20:34 +00001979 Out << 'E';
1980 break;
1981 }
1982
1983 case Expr::GNUNullExprClass:
1984 // FIXME: should this really be mangled the same as nullptr?
1985 // fallthrough
1986
1987 case Expr::CXXNullPtrLiteralExprClass: {
1988 // Proposal from David Vandervoorde, 2010.06.30, as
1989 // modified by ABI list discussion.
1990 Out << "LDnE";
1991 break;
1992 }
Anders Carlssone170ba72009-12-14 01:45:37 +00001993
Anders Carlssond553f8c2009-09-21 01:21:10 +00001994 }
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001995}
1996
Anders Carlsson3ac86b52009-04-15 05:36:58 +00001997void CXXNameMangler::mangleCXXCtorType(CXXCtorType T) {
1998 // <ctor-dtor-name> ::= C1 # complete object constructor
1999 // ::= C2 # base object constructor
2000 // ::= C3 # complete object allocating constructor
2001 //
2002 switch (T) {
2003 case Ctor_Complete:
2004 Out << "C1";
2005 break;
2006 case Ctor_Base:
2007 Out << "C2";
2008 break;
2009 case Ctor_CompleteAllocating:
2010 Out << "C3";
2011 break;
2012 }
2013}
2014
Anders Carlsson27ae5362009-04-17 01:58:57 +00002015void CXXNameMangler::mangleCXXDtorType(CXXDtorType T) {
2016 // <ctor-dtor-name> ::= D0 # deleting destructor
2017 // ::= D1 # complete object destructor
2018 // ::= D2 # base object destructor
2019 //
2020 switch (T) {
2021 case Dtor_Deleting:
2022 Out << "D0";
2023 break;
2024 case Dtor_Complete:
2025 Out << "D1";
2026 break;
2027 case Dtor_Base:
2028 Out << "D2";
2029 break;
2030 }
2031}
2032
John McCall6dbce192010-08-20 00:17:19 +00002033void CXXNameMangler::mangleTemplateArgs(
2034 const ExplicitTemplateArgumentList &TemplateArgs) {
2035 // <template-args> ::= I <template-arg>+ E
2036 Out << 'I';
2037 for (unsigned I = 0, E = TemplateArgs.NumTemplateArgs; I != E; ++I)
2038 mangleTemplateArg(0, TemplateArgs.getTemplateArgs()[I].getArgument());
2039 Out << 'E';
2040}
2041
Douglas Gregor20f0cc72010-04-23 03:10:43 +00002042void CXXNameMangler::mangleTemplateArgs(TemplateName Template,
2043 const TemplateArgument *TemplateArgs,
2044 unsigned NumTemplateArgs) {
2045 if (TemplateDecl *TD = Template.getAsTemplateDecl())
2046 return mangleTemplateArgs(*TD->getTemplateParameters(), TemplateArgs,
2047 NumTemplateArgs);
Sean Huntc3021132010-05-05 15:23:54 +00002048
Douglas Gregor20f0cc72010-04-23 03:10:43 +00002049 // <template-args> ::= I <template-arg>+ E
2050 Out << 'I';
2051 for (unsigned i = 0; i != NumTemplateArgs; ++i)
2052 mangleTemplateArg(0, TemplateArgs[i]);
2053 Out << 'E';
2054}
2055
Rafael Espindolad9800722010-03-11 14:07:00 +00002056void CXXNameMangler::mangleTemplateArgs(const TemplateParameterList &PL,
2057 const TemplateArgumentList &AL) {
Anders Carlsson7a0ba872009-05-15 16:09:15 +00002058 // <template-args> ::= I <template-arg>+ E
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002059 Out << 'I';
Rafael Espindolad9800722010-03-11 14:07:00 +00002060 for (unsigned i = 0, e = AL.size(); i != e; ++i)
2061 mangleTemplateArg(PL.getParam(i), AL[i]);
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002062 Out << 'E';
Anders Carlsson7a0ba872009-05-15 16:09:15 +00002063}
2064
Rafael Espindolad9800722010-03-11 14:07:00 +00002065void CXXNameMangler::mangleTemplateArgs(const TemplateParameterList &PL,
2066 const TemplateArgument *TemplateArgs,
Anders Carlsson7624f212009-09-18 02:42:01 +00002067 unsigned NumTemplateArgs) {
2068 // <template-args> ::= I <template-arg>+ E
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002069 Out << 'I';
Daniel Dunbar7e0c1952009-11-21 09:17:15 +00002070 for (unsigned i = 0; i != NumTemplateArgs; ++i)
Rafael Espindolad9800722010-03-11 14:07:00 +00002071 mangleTemplateArg(PL.getParam(i), TemplateArgs[i]);
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002072 Out << 'E';
Anders Carlsson7624f212009-09-18 02:42:01 +00002073}
2074
Rafael Espindolad9800722010-03-11 14:07:00 +00002075void CXXNameMangler::mangleTemplateArg(const NamedDecl *P,
2076 const TemplateArgument &A) {
Mike Stump1eb44332009-09-09 15:08:12 +00002077 // <template-arg> ::= <type> # type or template
Anders Carlsson7a0ba872009-05-15 16:09:15 +00002078 // ::= X <expression> E # expression
2079 // ::= <expr-primary> # simple expressions
2080 // ::= I <template-arg>* E # argument pack
2081 // ::= sp <expression> # pack expansion of (C++0x)
2082 switch (A.getKind()) {
2083 default:
2084 assert(0 && "Unknown template argument kind!");
2085 case TemplateArgument::Type:
2086 mangleType(A.getAsType());
2087 break;
Anders Carlsson9e85c742009-12-23 19:30:55 +00002088 case TemplateArgument::Template:
John McCallb6f532e2010-07-14 06:43:17 +00002089 // This is mangled as <type>.
2090 mangleType(A.getAsTemplate());
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002091 break;
Anders Carlssond553f8c2009-09-21 01:21:10 +00002092 case TemplateArgument::Expression:
2093 Out << 'X';
2094 mangleExpression(A.getAsExpr());
2095 Out << 'E';
2096 break;
Anders Carlssone170ba72009-12-14 01:45:37 +00002097 case TemplateArgument::Integral:
2098 mangleIntegerLiteral(A.getIntegralType(), *A.getAsIntegral());
Anders Carlsson7a0ba872009-05-15 16:09:15 +00002099 break;
Daniel Dunbar7e0c1952009-11-21 09:17:15 +00002100 case TemplateArgument::Declaration: {
Douglas Gregor20f0cc72010-04-23 03:10:43 +00002101 assert(P && "Missing template parameter for declaration argument");
Daniel Dunbar7e0c1952009-11-21 09:17:15 +00002102 // <expr-primary> ::= L <mangled-name> E # external name
2103
Rafael Espindolad9800722010-03-11 14:07:00 +00002104 // Clang produces AST's where pointer-to-member-function expressions
Daniel Dunbar7e0c1952009-11-21 09:17:15 +00002105 // and pointer-to-function expressions are represented as a declaration not
Rafael Espindolad9800722010-03-11 14:07:00 +00002106 // an expression. We compensate for it here to produce the correct mangling.
2107 NamedDecl *D = cast<NamedDecl>(A.getAsDecl());
2108 const NonTypeTemplateParmDecl *Parameter = cast<NonTypeTemplateParmDecl>(P);
2109 bool compensateMangling = D->isCXXClassMember() &&
2110 !Parameter->getType()->isReferenceType();
2111 if (compensateMangling) {
2112 Out << 'X';
2113 mangleOperatorName(OO_Amp, 1);
2114 }
2115
Daniel Dunbar7e0c1952009-11-21 09:17:15 +00002116 Out << 'L';
2117 // References to external entities use the mangled name; if the name would
2118 // not normally be manged then mangle it as unqualified.
2119 //
2120 // FIXME: The ABI specifies that external names here should have _Z, but
2121 // gcc leaves this off.
Rafael Espindolad9800722010-03-11 14:07:00 +00002122 if (compensateMangling)
2123 mangle(D, "_Z");
2124 else
2125 mangle(D, "Z");
Daniel Dunbar7e0c1952009-11-21 09:17:15 +00002126 Out << 'E';
Rafael Espindolad9800722010-03-11 14:07:00 +00002127
2128 if (compensateMangling)
2129 Out << 'E';
2130
Daniel Dunbar7e0c1952009-11-21 09:17:15 +00002131 break;
2132 }
2133 }
Anders Carlsson7a0ba872009-05-15 16:09:15 +00002134}
2135
Anders Carlsson0ccdf8d2009-09-27 00:38:53 +00002136void CXXNameMangler::mangleTemplateParameter(unsigned Index) {
2137 // <template-param> ::= T_ # first template parameter
2138 // ::= T <parameter-2 non-negative number> _
2139 if (Index == 0)
2140 Out << "T_";
2141 else
2142 Out << 'T' << (Index - 1) << '_';
2143}
2144
Anders Carlsson76967372009-09-17 00:43:46 +00002145// <substitution> ::= S <seq-id> _
2146// ::= S_
Anders Carlsson6862fc72009-09-17 04:16:28 +00002147bool CXXNameMangler::mangleSubstitution(const NamedDecl *ND) {
Anders Carlssone7c8cb62009-09-26 20:53:44 +00002148 // Try one of the standard substitutions first.
2149 if (mangleStandardSubstitution(ND))
2150 return true;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002151
Anders Carlsson433d1372009-11-07 04:26:04 +00002152 ND = cast<NamedDecl>(ND->getCanonicalDecl());
Anders Carlsson6862fc72009-09-17 04:16:28 +00002153 return mangleSubstitution(reinterpret_cast<uintptr_t>(ND));
2154}
2155
Anders Carlsson76967372009-09-17 00:43:46 +00002156bool CXXNameMangler::mangleSubstitution(QualType T) {
Anders Carlssond99edc42009-09-26 03:55:37 +00002157 if (!T.getCVRQualifiers()) {
2158 if (const RecordType *RT = T->getAs<RecordType>())
2159 return mangleSubstitution(RT->getDecl());
2160 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002161
Anders Carlsson76967372009-09-17 00:43:46 +00002162 uintptr_t TypePtr = reinterpret_cast<uintptr_t>(T.getAsOpaquePtr());
2163
Anders Carlssond3a932a2009-09-17 03:53:28 +00002164 return mangleSubstitution(TypePtr);
2165}
2166
Douglas Gregor1e9268e2010-04-28 05:58:56 +00002167bool CXXNameMangler::mangleSubstitution(TemplateName Template) {
2168 if (TemplateDecl *TD = Template.getAsTemplateDecl())
2169 return mangleSubstitution(TD);
Sean Huntc3021132010-05-05 15:23:54 +00002170
Douglas Gregor1e9268e2010-04-28 05:58:56 +00002171 Template = Context.getASTContext().getCanonicalTemplateName(Template);
2172 return mangleSubstitution(
2173 reinterpret_cast<uintptr_t>(Template.getAsVoidPointer()));
2174}
2175
Anders Carlssond3a932a2009-09-17 03:53:28 +00002176bool CXXNameMangler::mangleSubstitution(uintptr_t Ptr) {
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002177 llvm::DenseMap<uintptr_t, unsigned>::iterator I = Substitutions.find(Ptr);
Anders Carlsson76967372009-09-17 00:43:46 +00002178 if (I == Substitutions.end())
2179 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002180
Anders Carlsson76967372009-09-17 00:43:46 +00002181 unsigned SeqID = I->second;
2182 if (SeqID == 0)
2183 Out << "S_";
2184 else {
2185 SeqID--;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002186
Anders Carlsson76967372009-09-17 00:43:46 +00002187 // <seq-id> is encoded in base-36, using digits and upper case letters.
2188 char Buffer[10];
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002189 char *BufferPtr = llvm::array_endof(Buffer);
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002190
Anders Carlsson76967372009-09-17 00:43:46 +00002191 if (SeqID == 0) *--BufferPtr = '0';
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002192
Anders Carlsson76967372009-09-17 00:43:46 +00002193 while (SeqID) {
2194 assert(BufferPtr > Buffer && "Buffer overflow!");
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002195
John McCall6ab30e02010-06-09 07:26:17 +00002196 char c = static_cast<char>(SeqID % 36);
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002197
Anders Carlsson76967372009-09-17 00:43:46 +00002198 *--BufferPtr = (c < 10 ? '0' + c : 'A' + c - 10);
2199 SeqID /= 36;
2200 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002201
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002202 Out << 'S'
2203 << llvm::StringRef(BufferPtr, llvm::array_endof(Buffer)-BufferPtr)
2204 << '_';
Anders Carlsson76967372009-09-17 00:43:46 +00002205 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002206
Anders Carlsson76967372009-09-17 00:43:46 +00002207 return true;
2208}
2209
Anders Carlssonf514b542009-09-27 00:12:57 +00002210static bool isCharType(QualType T) {
2211 if (T.isNull())
2212 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002213
Anders Carlssonf514b542009-09-27 00:12:57 +00002214 return T->isSpecificBuiltinType(BuiltinType::Char_S) ||
2215 T->isSpecificBuiltinType(BuiltinType::Char_U);
2216}
2217
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002218/// isCharSpecialization - Returns whether a given type is a template
Anders Carlssonf514b542009-09-27 00:12:57 +00002219/// specialization of a given name with a single argument of type char.
2220static bool isCharSpecialization(QualType T, const char *Name) {
2221 if (T.isNull())
2222 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002223
Anders Carlssonf514b542009-09-27 00:12:57 +00002224 const RecordType *RT = T->getAs<RecordType>();
2225 if (!RT)
2226 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002227
2228 const ClassTemplateSpecializationDecl *SD =
Anders Carlssonf514b542009-09-27 00:12:57 +00002229 dyn_cast<ClassTemplateSpecializationDecl>(RT->getDecl());
2230 if (!SD)
2231 return false;
2232
2233 if (!isStdNamespace(SD->getDeclContext()))
2234 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002235
Anders Carlssonf514b542009-09-27 00:12:57 +00002236 const TemplateArgumentList &TemplateArgs = SD->getTemplateArgs();
2237 if (TemplateArgs.size() != 1)
2238 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002239
Anders Carlssonf514b542009-09-27 00:12:57 +00002240 if (!isCharType(TemplateArgs[0].getAsType()))
2241 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002242
Daniel Dunbar01eb9b92009-10-18 21:17:35 +00002243 return SD->getIdentifier()->getName() == Name;
Anders Carlssonf514b542009-09-27 00:12:57 +00002244}
2245
Anders Carlsson91f88602009-12-07 19:56:42 +00002246template <std::size_t StrLen>
2247bool isStreamCharSpecialization(const ClassTemplateSpecializationDecl *SD,
2248 const char (&Str)[StrLen]) {
2249 if (!SD->getIdentifier()->isStr(Str))
2250 return false;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002251
Anders Carlsson91f88602009-12-07 19:56:42 +00002252 const TemplateArgumentList &TemplateArgs = SD->getTemplateArgs();
2253 if (TemplateArgs.size() != 2)
2254 return false;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002255
Anders Carlsson91f88602009-12-07 19:56:42 +00002256 if (!isCharType(TemplateArgs[0].getAsType()))
2257 return false;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002258
Anders Carlsson91f88602009-12-07 19:56:42 +00002259 if (!isCharSpecialization(TemplateArgs[1].getAsType(), "char_traits"))
2260 return false;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002261
Anders Carlsson91f88602009-12-07 19:56:42 +00002262 return true;
2263}
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002264
Anders Carlssone7c8cb62009-09-26 20:53:44 +00002265bool CXXNameMangler::mangleStandardSubstitution(const NamedDecl *ND) {
2266 // <substitution> ::= St # ::std::
Anders Carlsson8c031552009-09-26 23:10:05 +00002267 if (const NamespaceDecl *NS = dyn_cast<NamespaceDecl>(ND)) {
Anders Carlsson47846d22009-12-04 06:23:23 +00002268 if (isStd(NS)) {
Anders Carlsson8c031552009-09-26 23:10:05 +00002269 Out << "St";
2270 return true;
2271 }
2272 }
2273
2274 if (const ClassTemplateDecl *TD = dyn_cast<ClassTemplateDecl>(ND)) {
2275 if (!isStdNamespace(TD->getDeclContext()))
2276 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002277
Anders Carlsson8c031552009-09-26 23:10:05 +00002278 // <substitution> ::= Sa # ::std::allocator
2279 if (TD->getIdentifier()->isStr("allocator")) {
2280 Out << "Sa";
2281 return true;
2282 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002283
Anders Carlsson189d59c2009-09-26 23:14:39 +00002284 // <<substitution> ::= Sb # ::std::basic_string
2285 if (TD->getIdentifier()->isStr("basic_string")) {
2286 Out << "Sb";
2287 return true;
2288 }
Anders Carlsson8c031552009-09-26 23:10:05 +00002289 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002290
2291 if (const ClassTemplateSpecializationDecl *SD =
Anders Carlssonf514b542009-09-27 00:12:57 +00002292 dyn_cast<ClassTemplateSpecializationDecl>(ND)) {
Eli Friedman5370ee22010-02-23 18:25:09 +00002293 if (!isStdNamespace(SD->getDeclContext()))
2294 return false;
2295
Anders Carlssonf514b542009-09-27 00:12:57 +00002296 // <substitution> ::= Ss # ::std::basic_string<char,
2297 // ::std::char_traits<char>,
2298 // ::std::allocator<char> >
2299 if (SD->getIdentifier()->isStr("basic_string")) {
2300 const TemplateArgumentList &TemplateArgs = SD->getTemplateArgs();
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002301
Anders Carlssonf514b542009-09-27 00:12:57 +00002302 if (TemplateArgs.size() != 3)
2303 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002304
Anders Carlssonf514b542009-09-27 00:12:57 +00002305 if (!isCharType(TemplateArgs[0].getAsType()))
2306 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002307
Anders Carlssonf514b542009-09-27 00:12:57 +00002308 if (!isCharSpecialization(TemplateArgs[1].getAsType(), "char_traits"))
2309 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002310
Anders Carlssonf514b542009-09-27 00:12:57 +00002311 if (!isCharSpecialization(TemplateArgs[2].getAsType(), "allocator"))
2312 return false;
2313
2314 Out << "Ss";
2315 return true;
2316 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002317
Anders Carlsson91f88602009-12-07 19:56:42 +00002318 // <substitution> ::= Si # ::std::basic_istream<char,
2319 // ::std::char_traits<char> >
2320 if (isStreamCharSpecialization(SD, "basic_istream")) {
2321 Out << "Si";
2322 return true;
2323 }
2324
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002325 // <substitution> ::= So # ::std::basic_ostream<char,
Anders Carlsson8f8fd8e2009-10-08 17:20:26 +00002326 // ::std::char_traits<char> >
Anders Carlsson91f88602009-12-07 19:56:42 +00002327 if (isStreamCharSpecialization(SD, "basic_ostream")) {
Anders Carlsson8f8fd8e2009-10-08 17:20:26 +00002328 Out << "So";
2329 return true;
2330 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002331
Anders Carlsson91f88602009-12-07 19:56:42 +00002332 // <substitution> ::= Sd # ::std::basic_iostream<char,
2333 // ::std::char_traits<char> >
2334 if (isStreamCharSpecialization(SD, "basic_iostream")) {
2335 Out << "Sd";
2336 return true;
2337 }
Anders Carlssonf514b542009-09-27 00:12:57 +00002338 }
Anders Carlsson8c031552009-09-26 23:10:05 +00002339 return false;
Anders Carlssone7c8cb62009-09-26 20:53:44 +00002340}
2341
Anders Carlsson76967372009-09-17 00:43:46 +00002342void CXXNameMangler::addSubstitution(QualType T) {
Anders Carlssond99edc42009-09-26 03:55:37 +00002343 if (!T.getCVRQualifiers()) {
2344 if (const RecordType *RT = T->getAs<RecordType>()) {
2345 addSubstitution(RT->getDecl());
2346 return;
2347 }
2348 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002349
Anders Carlsson76967372009-09-17 00:43:46 +00002350 uintptr_t TypePtr = reinterpret_cast<uintptr_t>(T.getAsOpaquePtr());
Anders Carlssond3a932a2009-09-17 03:53:28 +00002351 addSubstitution(TypePtr);
2352}
2353
Douglas Gregor1e9268e2010-04-28 05:58:56 +00002354void CXXNameMangler::addSubstitution(TemplateName Template) {
2355 if (TemplateDecl *TD = Template.getAsTemplateDecl())
2356 return addSubstitution(TD);
Sean Huntc3021132010-05-05 15:23:54 +00002357
Douglas Gregor1e9268e2010-04-28 05:58:56 +00002358 Template = Context.getASTContext().getCanonicalTemplateName(Template);
2359 addSubstitution(reinterpret_cast<uintptr_t>(Template.getAsVoidPointer()));
2360}
2361
Anders Carlssond3a932a2009-09-17 03:53:28 +00002362void CXXNameMangler::addSubstitution(uintptr_t Ptr) {
Anders Carlssond3a932a2009-09-17 03:53:28 +00002363 assert(!Substitutions.count(Ptr) && "Substitution already exists!");
Anders Carlsson9d85b722010-06-02 04:29:50 +00002364 Substitutions[Ptr] = SeqID++;
Anders Carlsson76967372009-09-17 00:43:46 +00002365}
2366
Daniel Dunbar1b077112009-11-21 09:06:10 +00002367//
Mike Stump1eb44332009-09-09 15:08:12 +00002368
Daniel Dunbar1b077112009-11-21 09:06:10 +00002369/// \brief Mangles the name of the declaration D and emits that name to the
2370/// given output stream.
2371///
2372/// If the declaration D requires a mangled name, this routine will emit that
2373/// mangled name to \p os and return true. Otherwise, \p os will be unchanged
2374/// and this routine will return false. In this case, the caller should just
2375/// emit the identifier of the declaration (\c D->getIdentifier()) as its
2376/// name.
Daniel Dunbarf981bf82009-11-21 09:14:52 +00002377void MangleContext::mangleName(const NamedDecl *D,
Daniel Dunbar94fd26d2009-11-21 09:06:22 +00002378 llvm::SmallVectorImpl<char> &Res) {
Daniel Dunbarc02ab4c2009-11-21 09:14:44 +00002379 assert((isa<FunctionDecl>(D) || isa<VarDecl>(D)) &&
2380 "Invalid mangleName() call, argument is not a variable or function!");
2381 assert(!isa<CXXConstructorDecl>(D) && !isa<CXXDestructorDecl>(D) &&
2382 "Invalid mangleName() call on 'structor decl!");
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002383
Daniel Dunbar1b077112009-11-21 09:06:10 +00002384 PrettyStackTraceDecl CrashInfo(D, SourceLocation(),
2385 getASTContext().getSourceManager(),
2386 "Mangling declaration");
Mike Stump1eb44332009-09-09 15:08:12 +00002387
Daniel Dunbar94fd26d2009-11-21 09:06:22 +00002388 CXXNameMangler Mangler(*this, Res);
2389 return Mangler.mangle(D);
Daniel Dunbar1b077112009-11-21 09:06:10 +00002390}
Mike Stump1eb44332009-09-09 15:08:12 +00002391
Daniel Dunbar1b077112009-11-21 09:06:10 +00002392void MangleContext::mangleCXXCtor(const CXXConstructorDecl *D, CXXCtorType Type,
Daniel Dunbar94fd26d2009-11-21 09:06:22 +00002393 llvm::SmallVectorImpl<char> &Res) {
Daniel Dunbar77939c92009-11-21 09:06:31 +00002394 CXXNameMangler Mangler(*this, Res, D, Type);
2395 Mangler.mangle(D);
Daniel Dunbar1b077112009-11-21 09:06:10 +00002396}
Mike Stump1eb44332009-09-09 15:08:12 +00002397
Daniel Dunbar1b077112009-11-21 09:06:10 +00002398void MangleContext::mangleCXXDtor(const CXXDestructorDecl *D, CXXDtorType Type,
Daniel Dunbar94fd26d2009-11-21 09:06:22 +00002399 llvm::SmallVectorImpl<char> &Res) {
Daniel Dunbar77939c92009-11-21 09:06:31 +00002400 CXXNameMangler Mangler(*this, Res, D, Type);
2401 Mangler.mangle(D);
Daniel Dunbar1b077112009-11-21 09:06:10 +00002402}
Mike Stumpf1216772009-07-31 18:25:34 +00002403
Fariborz Jahanian564360b2010-06-24 00:08:06 +00002404void MangleContext::mangleBlock(GlobalDecl GD, const BlockDecl *BD,
Douglas Gregor35415f52010-05-25 17:04:15 +00002405 llvm::SmallVectorImpl<char> &Res) {
Charles Davis685b1d92010-05-26 18:25:27 +00002406 MiscNameMangler Mangler(*this, Res);
Fariborz Jahanian564360b2010-06-24 00:08:06 +00002407 Mangler.mangleBlock(GD, BD);
Douglas Gregor35415f52010-05-25 17:04:15 +00002408}
2409
Anders Carlsson19879c92010-03-23 17:17:29 +00002410void MangleContext::mangleThunk(const CXXMethodDecl *MD,
2411 const ThunkInfo &Thunk,
2412 llvm::SmallVectorImpl<char> &Res) {
2413 // <special-name> ::= T <call-offset> <base encoding>
2414 // # base is the nominal target function of thunk
2415 // <special-name> ::= Tc <call-offset> <call-offset> <base encoding>
2416 // # base is the nominal target function of thunk
2417 // # first call-offset is 'this' adjustment
2418 // # second call-offset is result adjustment
Sean Huntc3021132010-05-05 15:23:54 +00002419
Anders Carlsson19879c92010-03-23 17:17:29 +00002420 assert(!isa<CXXDestructorDecl>(MD) &&
2421 "Use mangleCXXDtor for destructor decls!");
Sean Huntc3021132010-05-05 15:23:54 +00002422
Anders Carlsson19879c92010-03-23 17:17:29 +00002423 CXXNameMangler Mangler(*this, Res);
2424 Mangler.getStream() << "_ZT";
2425 if (!Thunk.Return.isEmpty())
2426 Mangler.getStream() << 'c';
Sean Huntc3021132010-05-05 15:23:54 +00002427
Anders Carlsson19879c92010-03-23 17:17:29 +00002428 // Mangle the 'this' pointer adjustment.
2429 Mangler.mangleCallOffset(Thunk.This.NonVirtual, Thunk.This.VCallOffsetOffset);
Sean Huntc3021132010-05-05 15:23:54 +00002430
Anders Carlsson19879c92010-03-23 17:17:29 +00002431 // Mangle the return pointer adjustment if there is one.
2432 if (!Thunk.Return.isEmpty())
2433 Mangler.mangleCallOffset(Thunk.Return.NonVirtual,
2434 Thunk.Return.VBaseOffsetOffset);
Sean Huntc3021132010-05-05 15:23:54 +00002435
Anders Carlsson19879c92010-03-23 17:17:29 +00002436 Mangler.mangleFunctionEncoding(MD);
2437}
2438
Sean Huntc3021132010-05-05 15:23:54 +00002439void
Anders Carlsson19879c92010-03-23 17:17:29 +00002440MangleContext::mangleCXXDtorThunk(const CXXDestructorDecl *DD, CXXDtorType Type,
2441 const ThisAdjustment &ThisAdjustment,
2442 llvm::SmallVectorImpl<char> &Res) {
2443 // <special-name> ::= T <call-offset> <base encoding>
2444 // # base is the nominal target function of thunk
Sean Huntc3021132010-05-05 15:23:54 +00002445
Anders Carlsson19879c92010-03-23 17:17:29 +00002446 CXXNameMangler Mangler(*this, Res, DD, Type);
2447 Mangler.getStream() << "_ZT";
2448
2449 // Mangle the 'this' pointer adjustment.
Sean Huntc3021132010-05-05 15:23:54 +00002450 Mangler.mangleCallOffset(ThisAdjustment.NonVirtual,
Anders Carlsson19879c92010-03-23 17:17:29 +00002451 ThisAdjustment.VCallOffsetOffset);
2452
2453 Mangler.mangleFunctionEncoding(DD);
2454}
2455
Daniel Dunbarc0747712009-11-21 09:12:13 +00002456/// mangleGuardVariable - Returns the mangled name for a guard variable
2457/// for the passed in VarDecl.
John McCall5cd91b52010-09-08 01:44:27 +00002458void MangleContext::mangleItaniumGuardVariable(const VarDecl *D,
2459 llvm::SmallVectorImpl<char> &Res) {
Daniel Dunbarc0747712009-11-21 09:12:13 +00002460 // <special-name> ::= GV <object name> # Guard variable for one-time
2461 // # initialization
2462 CXXNameMangler Mangler(*this, Res);
2463 Mangler.getStream() << "_ZGV";
2464 Mangler.mangleName(D);
2465}
2466
Anders Carlsson715edf22010-06-26 16:09:40 +00002467void MangleContext::mangleReferenceTemporary(const VarDecl *D,
2468 llvm::SmallVectorImpl<char> &Res) {
2469 // We match the GCC mangling here.
2470 // <special-name> ::= GR <object name>
2471 CXXNameMangler Mangler(*this, Res);
2472 Mangler.getStream() << "_ZGR";
2473 Mangler.mangleName(D);
2474}
2475
Anders Carlsson046c2942010-04-17 20:15:18 +00002476void MangleContext::mangleCXXVTable(const CXXRecordDecl *RD,
Daniel Dunbar94fd26d2009-11-21 09:06:22 +00002477 llvm::SmallVectorImpl<char> &Res) {
Daniel Dunbarc0747712009-11-21 09:12:13 +00002478 // <special-name> ::= TV <type> # virtual table
Daniel Dunbar94fd26d2009-11-21 09:06:22 +00002479 CXXNameMangler Mangler(*this, Res);
Daniel Dunbarc0747712009-11-21 09:12:13 +00002480 Mangler.getStream() << "_ZTV";
Douglas Gregor1b12a3b2010-05-26 05:11:13 +00002481 Mangler.mangleNameOrStandardSubstitution(RD);
Daniel Dunbar1b077112009-11-21 09:06:10 +00002482}
Mike Stump82d75b02009-11-10 01:58:37 +00002483
Daniel Dunbar1b077112009-11-21 09:06:10 +00002484void MangleContext::mangleCXXVTT(const CXXRecordDecl *RD,
Daniel Dunbar94fd26d2009-11-21 09:06:22 +00002485 llvm::SmallVectorImpl<char> &Res) {
Daniel Dunbarc0747712009-11-21 09:12:13 +00002486 // <special-name> ::= TT <type> # VTT structure
Daniel Dunbar94fd26d2009-11-21 09:06:22 +00002487 CXXNameMangler Mangler(*this, Res);
Daniel Dunbarc0747712009-11-21 09:12:13 +00002488 Mangler.getStream() << "_ZTT";
Douglas Gregor1b12a3b2010-05-26 05:11:13 +00002489 Mangler.mangleNameOrStandardSubstitution(RD);
Daniel Dunbar1b077112009-11-21 09:06:10 +00002490}
Mike Stumpab3f7e92009-11-10 01:41:59 +00002491
Anders Carlsson046c2942010-04-17 20:15:18 +00002492void MangleContext::mangleCXXCtorVTable(const CXXRecordDecl *RD, int64_t Offset,
Daniel Dunbar1b077112009-11-21 09:06:10 +00002493 const CXXRecordDecl *Type,
Daniel Dunbar94fd26d2009-11-21 09:06:22 +00002494 llvm::SmallVectorImpl<char> &Res) {
Daniel Dunbarc0747712009-11-21 09:12:13 +00002495 // <special-name> ::= TC <type> <offset number> _ <base type>
Daniel Dunbar94fd26d2009-11-21 09:06:22 +00002496 CXXNameMangler Mangler(*this, Res);
Daniel Dunbarc0747712009-11-21 09:12:13 +00002497 Mangler.getStream() << "_ZTC";
Douglas Gregor1b12a3b2010-05-26 05:11:13 +00002498 Mangler.mangleNameOrStandardSubstitution(RD);
Daniel Dunbarc0747712009-11-21 09:12:13 +00002499 Mangler.getStream() << Offset;
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002500 Mangler.getStream() << '_';
Douglas Gregor1b12a3b2010-05-26 05:11:13 +00002501 Mangler.mangleNameOrStandardSubstitution(Type);
Daniel Dunbar1b077112009-11-21 09:06:10 +00002502}
Mike Stump738f8c22009-07-31 23:15:31 +00002503
Mike Stumpde050572009-12-02 18:57:08 +00002504void MangleContext::mangleCXXRTTI(QualType Ty,
Daniel Dunbar94fd26d2009-11-21 09:06:22 +00002505 llvm::SmallVectorImpl<char> &Res) {
Daniel Dunbarc0747712009-11-21 09:12:13 +00002506 // <special-name> ::= TI <type> # typeinfo structure
Douglas Gregor154fe982009-12-23 22:04:40 +00002507 assert(!Ty.hasQualifiers() && "RTTI info cannot have top-level qualifiers");
Daniel Dunbar94fd26d2009-11-21 09:06:22 +00002508 CXXNameMangler Mangler(*this, Res);
Daniel Dunbarc0747712009-11-21 09:12:13 +00002509 Mangler.getStream() << "_ZTI";
2510 Mangler.mangleType(Ty);
Daniel Dunbar1b077112009-11-21 09:06:10 +00002511}
Mike Stump67795982009-11-14 00:14:13 +00002512
Mike Stumpde050572009-12-02 18:57:08 +00002513void MangleContext::mangleCXXRTTIName(QualType Ty,
Daniel Dunbar94fd26d2009-11-21 09:06:22 +00002514 llvm::SmallVectorImpl<char> &Res) {
Daniel Dunbarc0747712009-11-21 09:12:13 +00002515 // <special-name> ::= TS <type> # typeinfo name (null terminated byte string)
Daniel Dunbar94fd26d2009-11-21 09:06:22 +00002516 CXXNameMangler Mangler(*this, Res);
Daniel Dunbarc0747712009-11-21 09:12:13 +00002517 Mangler.getStream() << "_ZTS";
2518 Mangler.mangleType(Ty);
Mike Stumpf1216772009-07-31 18:25:34 +00002519}