blob: 8ee67e76e9082e40a0a8b5695500d33f2565864f [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);
Bob Wilsonc7df92d2010-11-12 17:24:43 +0000238 bool mangleNeonVectorType(const VectorType *T);
Anders Carlssone170ba72009-12-14 01:45:37 +0000239
240 void mangleIntegerLiteral(QualType T, const llvm::APSInt &Value);
John McCall2f27bf82010-02-04 02:56:29 +0000241 void mangleMemberExpr(const Expr *Base, bool IsArrow,
242 NestedNameSpecifier *Qualifier,
243 DeclarationName Name,
244 unsigned KnownArity);
John McCall5e1e89b2010-08-18 19:18:59 +0000245 void mangleExpression(const Expr *E, unsigned Arity = UnknownArity);
Daniel Dunbar1b077112009-11-21 09:06:10 +0000246 void mangleCXXCtorType(CXXCtorType T);
247 void mangleCXXDtorType(CXXDtorType T);
Mike Stump1eb44332009-09-09 15:08:12 +0000248
John McCall6dbce192010-08-20 00:17:19 +0000249 void mangleTemplateArgs(const ExplicitTemplateArgumentList &TemplateArgs);
Douglas Gregor20f0cc72010-04-23 03:10:43 +0000250 void mangleTemplateArgs(TemplateName Template,
251 const TemplateArgument *TemplateArgs,
Sean Huntc3021132010-05-05 15:23:54 +0000252 unsigned NumTemplateArgs);
Rafael Espindolad9800722010-03-11 14:07:00 +0000253 void mangleTemplateArgs(const TemplateParameterList &PL,
254 const TemplateArgument *TemplateArgs,
Daniel Dunbar1b077112009-11-21 09:06:10 +0000255 unsigned NumTemplateArgs);
Rafael Espindolad9800722010-03-11 14:07:00 +0000256 void mangleTemplateArgs(const TemplateParameterList &PL,
257 const TemplateArgumentList &AL);
258 void mangleTemplateArg(const NamedDecl *P, const TemplateArgument &A);
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000259
Daniel Dunbar1b077112009-11-21 09:06:10 +0000260 void mangleTemplateParameter(unsigned Index);
261};
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000262}
263
Anders Carlsson43f17402009-04-02 15:51:53 +0000264static bool isInCLinkageSpecification(const Decl *D) {
Douglas Gregor457e2812009-10-28 16:31:34 +0000265 D = D->getCanonicalDecl();
Mike Stump1eb44332009-09-09 15:08:12 +0000266 for (const DeclContext *DC = D->getDeclContext();
Anders Carlsson43f17402009-04-02 15:51:53 +0000267 !DC->isTranslationUnit(); DC = DC->getParent()) {
Mike Stump1eb44332009-09-09 15:08:12 +0000268 if (const LinkageSpecDecl *Linkage = dyn_cast<LinkageSpecDecl>(DC))
Anders Carlsson43f17402009-04-02 15:51:53 +0000269 return Linkage->getLanguage() == LinkageSpecDecl::lang_c;
270 }
Mike Stump1eb44332009-09-09 15:08:12 +0000271
Anders Carlsson43f17402009-04-02 15:51:53 +0000272 return false;
273}
274
Daniel Dunbarf981bf82009-11-21 09:14:52 +0000275bool MangleContext::shouldMangleDeclName(const NamedDecl *D) {
276 // In C, functions with no attributes never need to be mangled. Fastpath them.
277 if (!getASTContext().getLangOptions().CPlusPlus && !D->hasAttrs())
278 return false;
279
280 // Any decl can be declared with __asm("foo") on it, and this takes precedence
281 // over all other naming in the .o file.
282 if (D->hasAttr<AsmLabelAttr>())
283 return true;
284
Mike Stump141c5af2009-09-02 00:25:38 +0000285 // Clang's "overloadable" attribute extension to C/C++ implies name mangling
Anders Carlssona1e16222009-11-07 07:15:03 +0000286 // (always) as does passing a C++ member function and a function
287 // whose name is not a simple identifier.
Daniel Dunbarf981bf82009-11-21 09:14:52 +0000288 const FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
289 if (FD && (FD->hasAttr<OverloadableAttr>() || isa<CXXMethodDecl>(FD) ||
290 !FD->getDeclName().isIdentifier()))
291 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000292
Daniel Dunbarf981bf82009-11-21 09:14:52 +0000293 // Otherwise, no mangling is done outside C++ mode.
294 if (!getASTContext().getLangOptions().CPlusPlus)
295 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000296
Sean Hunt31455252010-01-24 03:04:27 +0000297 // Variables at global scope with non-internal linkage are not mangled
Eli Friedman7facf842009-12-02 20:32:49 +0000298 if (!FD) {
299 const DeclContext *DC = D->getDeclContext();
300 // Check for extern variable declared locally.
Fariborz Jahaniane81c5612010-06-30 18:57:21 +0000301 if (DC->isFunctionOrMethod() && D->hasLinkage())
Eli Friedman7facf842009-12-02 20:32:49 +0000302 while (!DC->isNamespace() && !DC->isTranslationUnit())
303 DC = DC->getParent();
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000304 if (DC->isTranslationUnit() && D->getLinkage() != InternalLinkage)
Eli Friedman7facf842009-12-02 20:32:49 +0000305 return false;
306 }
307
Eli Friedmanc00cb642010-07-18 20:49:59 +0000308 // Class members are always mangled.
309 if (D->getDeclContext()->isRecord())
310 return true;
311
Eli Friedman7facf842009-12-02 20:32:49 +0000312 // C functions and "main" are not mangled.
313 if ((FD && FD->isMain()) || isInCLinkageSpecification(D))
Daniel Dunbarf981bf82009-11-21 09:14:52 +0000314 return false;
315
Anders Carlsson43f17402009-04-02 15:51:53 +0000316 return true;
317}
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000318
Daniel Dunbar7e0c1952009-11-21 09:17:15 +0000319void CXXNameMangler::mangle(const NamedDecl *D, llvm::StringRef Prefix) {
Mike Stump141c5af2009-09-02 00:25:38 +0000320 // Any decl can be declared with __asm("foo") on it, and this takes precedence
321 // over all other naming in the .o file.
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000322 if (const AsmLabelAttr *ALA = D->getAttr<AsmLabelAttr>()) {
Chris Lattnerca3f25c2009-03-21 08:24:40 +0000323 // If we have an asm name, then we use it as the mangling.
324 Out << '\01'; // LLVM IR Marker for __asm("foo")
325 Out << ALA->getLabel();
Daniel Dunbarf981bf82009-11-21 09:14:52 +0000326 return;
Chris Lattnerca3f25c2009-03-21 08:24:40 +0000327 }
Mike Stump1eb44332009-09-09 15:08:12 +0000328
Sean Hunt31455252010-01-24 03:04:27 +0000329 // <mangled-name> ::= _Z <encoding>
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000330 // ::= <data name>
331 // ::= <special-name>
Daniel Dunbar7e0c1952009-11-21 09:17:15 +0000332 Out << Prefix;
333 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
Daniel Dunbarf981bf82009-11-21 09:14:52 +0000334 mangleFunctionEncoding(FD);
Rafael Espindolad9800722010-03-11 14:07:00 +0000335 else if (const VarDecl *VD = dyn_cast<VarDecl>(D))
336 mangleName(VD);
Daniel Dunbar7e0c1952009-11-21 09:17:15 +0000337 else
Rafael Espindolad9800722010-03-11 14:07:00 +0000338 mangleName(cast<FieldDecl>(D));
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000339}
340
341void CXXNameMangler::mangleFunctionEncoding(const FunctionDecl *FD) {
342 // <encoding> ::= <function name> <bare-function-type>
343 mangleName(FD);
Mike Stump1eb44332009-09-09 15:08:12 +0000344
Daniel Dunbar7e0c1952009-11-21 09:17:15 +0000345 // Don't mangle in the type if this isn't a decl we should typically mangle.
346 if (!Context.shouldMangleDeclName(FD))
347 return;
348
Mike Stump141c5af2009-09-02 00:25:38 +0000349 // Whether the mangling of a function type includes the return type depends on
350 // the context and the nature of the function. The rules for deciding whether
351 // the return type is included are:
Mike Stump1eb44332009-09-09 15:08:12 +0000352 //
Douglas Gregor1fd2dd12009-06-29 22:39:32 +0000353 // 1. Template functions (names or types) have return types encoded, with
354 // the exceptions listed below.
Mike Stump1eb44332009-09-09 15:08:12 +0000355 // 2. Function types not appearing as part of a function name mangling,
Douglas Gregor1fd2dd12009-06-29 22:39:32 +0000356 // e.g. parameters, pointer types, etc., have return type encoded, with the
357 // exceptions listed below.
358 // 3. Non-template function names do not have return types encoded.
359 //
Mike Stump141c5af2009-09-02 00:25:38 +0000360 // The exceptions mentioned in (1) and (2) above, for which the return type is
361 // never included, are
Douglas Gregor1fd2dd12009-06-29 22:39:32 +0000362 // 1. Constructors.
363 // 2. Destructors.
364 // 3. Conversion operator functions, e.g. operator int.
365 bool MangleReturnType = false;
Anders Carlsson9234b7f2009-09-17 03:46:43 +0000366 if (FunctionTemplateDecl *PrimaryTemplate = FD->getPrimaryTemplate()) {
367 if (!(isa<CXXConstructorDecl>(FD) || isa<CXXDestructorDecl>(FD) ||
368 isa<CXXConversionDecl>(FD)))
369 MangleReturnType = true;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000370
Anders Carlsson9234b7f2009-09-17 03:46:43 +0000371 // Mangle the type of the primary template.
372 FD = PrimaryTemplate->getTemplatedDecl();
373 }
374
John McCall54e14c42009-10-22 22:37:11 +0000375 // Do the canonicalization out here because parameter types can
376 // undergo additional canonicalization (e.g. array decay).
377 FunctionType *FT = cast<FunctionType>(Context.getASTContext()
378 .getCanonicalType(FD->getType()));
379
380 mangleBareFunctionType(FT, MangleReturnType);
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000381}
382
Anders Carlsson47846d22009-12-04 06:23:23 +0000383static const DeclContext *IgnoreLinkageSpecDecls(const DeclContext *DC) {
384 while (isa<LinkageSpecDecl>(DC)) {
Anders Carlsson47846d22009-12-04 06:23:23 +0000385 DC = DC->getParent();
386 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000387
Anders Carlsson47846d22009-12-04 06:23:23 +0000388 return DC;
389}
390
Anders Carlssonc820f902010-06-02 15:58:27 +0000391/// isStd - Return whether a given namespace is the 'std' namespace.
392static bool isStd(const NamespaceDecl *NS) {
393 if (!IgnoreLinkageSpecDecls(NS->getParent())->isTranslationUnit())
394 return false;
395
396 const IdentifierInfo *II = NS->getOriginalNamespace()->getIdentifier();
397 return II && II->isStr("std");
398}
399
Anders Carlsson47846d22009-12-04 06:23:23 +0000400// isStdNamespace - Return whether a given decl context is a toplevel 'std'
401// namespace.
Daniel Dunbar1308af92009-11-21 09:11:45 +0000402static bool isStdNamespace(const DeclContext *DC) {
Anders Carlsson47846d22009-12-04 06:23:23 +0000403 if (!DC->isNamespace())
404 return false;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000405
Anders Carlsson47846d22009-12-04 06:23:23 +0000406 return isStd(cast<NamespaceDecl>(DC));
Daniel Dunbar1308af92009-11-21 09:11:45 +0000407}
408
Anders Carlssonbb36ba42009-09-26 03:24:57 +0000409static const TemplateDecl *
410isTemplate(const NamedDecl *ND, const TemplateArgumentList *&TemplateArgs) {
Anders Carlsson2744a062009-09-18 19:00:18 +0000411 // Check if we have a function template.
412 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)){
Anders Carlssonbb36ba42009-09-26 03:24:57 +0000413 if (const TemplateDecl *TD = FD->getPrimaryTemplate()) {
Anders Carlsson2744a062009-09-18 19:00:18 +0000414 TemplateArgs = FD->getTemplateSpecializationArgs();
Anders Carlssonbb36ba42009-09-26 03:24:57 +0000415 return TD;
Anders Carlsson2744a062009-09-18 19:00:18 +0000416 }
417 }
418
Anders Carlssoneafc6dc2009-09-18 19:44:50 +0000419 // Check if we have a class template.
420 if (const ClassTemplateSpecializationDecl *Spec =
421 dyn_cast<ClassTemplateSpecializationDecl>(ND)) {
422 TemplateArgs = &Spec->getTemplateArgs();
Anders Carlssonbb36ba42009-09-26 03:24:57 +0000423 return Spec->getSpecializedTemplate();
Anders Carlssoneafc6dc2009-09-18 19:44:50 +0000424 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000425
Anders Carlsson2744a062009-09-18 19:00:18 +0000426 return 0;
427}
428
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000429void CXXNameMangler::mangleName(const NamedDecl *ND) {
430 // <name> ::= <nested-name>
431 // ::= <unscoped-name>
432 // ::= <unscoped-template-name> <template-args>
Anders Carlsson201ce742009-09-17 03:17:01 +0000433 // ::= <local-name>
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000434 //
Anders Carlssond58d6f72009-09-17 16:12:20 +0000435 const DeclContext *DC = ND->getDeclContext();
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000436
Eli Friedman7facf842009-12-02 20:32:49 +0000437 // If this is an extern variable declared locally, the relevant DeclContext
438 // is that of the containing namespace, or the translation unit.
439 if (isa<FunctionDecl>(DC) && ND->hasLinkage())
440 while (!DC->isNamespace() && !DC->isTranslationUnit())
441 DC = DC->getParent();
John McCall82b7d7b2010-10-18 21:28:44 +0000442 else if (GetLocalClassDecl(ND)) {
443 mangleLocalName(ND);
444 return;
445 }
Eli Friedman7facf842009-12-02 20:32:49 +0000446
Anders Carlsson5cc58c62009-09-22 17:23:30 +0000447 while (isa<LinkageSpecDecl>(DC))
Anders Carlssond58d6f72009-09-17 16:12:20 +0000448 DC = DC->getParent();
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000449
Anders Carlssond58d6f72009-09-17 16:12:20 +0000450 if (DC->isTranslationUnit() || isStdNamespace(DC)) {
Anders Carlsson2744a062009-09-18 19:00:18 +0000451 // Check if we have a template.
452 const TemplateArgumentList *TemplateArgs = 0;
Anders Carlsson0fa6df42009-09-26 19:45:45 +0000453 if (const TemplateDecl *TD = isTemplate(ND, TemplateArgs)) {
Anders Carlsson2744a062009-09-18 19:00:18 +0000454 mangleUnscopedTemplateName(TD);
Rafael Espindolad9800722010-03-11 14:07:00 +0000455 TemplateParameterList *TemplateParameters = TD->getTemplateParameters();
456 mangleTemplateArgs(*TemplateParameters, *TemplateArgs);
Anders Carlsson2744a062009-09-18 19:00:18 +0000457 return;
Anders Carlsson7482e242009-09-18 04:29:09 +0000458 }
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000459
Anders Carlsson7482e242009-09-18 04:29:09 +0000460 mangleUnscopedName(ND);
461 return;
462 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000463
Anders Carlsson7b06f6c2009-12-10 03:14:39 +0000464 if (isa<FunctionDecl>(DC) || isa<ObjCMethodDecl>(DC)) {
Anders Carlsson7482e242009-09-18 04:29:09 +0000465 mangleLocalName(ND);
466 return;
467 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000468
Eli Friedman7facf842009-12-02 20:32:49 +0000469 mangleNestedName(ND, DC);
Anders Carlsson7482e242009-09-18 04:29:09 +0000470}
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000471void CXXNameMangler::mangleName(const TemplateDecl *TD,
Anders Carlsson7624f212009-09-18 02:42:01 +0000472 const TemplateArgument *TemplateArgs,
473 unsigned NumTemplateArgs) {
Anders Carlsson47846d22009-12-04 06:23:23 +0000474 const DeclContext *DC = IgnoreLinkageSpecDecls(TD->getDeclContext());
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000475
Anders Carlsson7624f212009-09-18 02:42:01 +0000476 if (DC->isTranslationUnit() || isStdNamespace(DC)) {
Anders Carlsson0fa6df42009-09-26 19:45:45 +0000477 mangleUnscopedTemplateName(TD);
Rafael Espindolad9800722010-03-11 14:07:00 +0000478 TemplateParameterList *TemplateParameters = TD->getTemplateParameters();
479 mangleTemplateArgs(*TemplateParameters, TemplateArgs, NumTemplateArgs);
Anders Carlsson7624f212009-09-18 02:42:01 +0000480 } else {
481 mangleNestedName(TD, TemplateArgs, NumTemplateArgs);
482 }
483}
484
Anders Carlsson201ce742009-09-17 03:17:01 +0000485void CXXNameMangler::mangleUnscopedName(const NamedDecl *ND) {
486 // <unscoped-name> ::= <unqualified-name>
487 // ::= St <unqualified-name> # ::std::
488 if (isStdNamespace(ND->getDeclContext()))
489 Out << "St";
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000490
Anders Carlsson201ce742009-09-17 03:17:01 +0000491 mangleUnqualifiedName(ND);
492}
493
Anders Carlsson0fa6df42009-09-26 19:45:45 +0000494void CXXNameMangler::mangleUnscopedTemplateName(const TemplateDecl *ND) {
Anders Carlsson201ce742009-09-17 03:17:01 +0000495 // <unscoped-template-name> ::= <unscoped-name>
496 // ::= <substitution>
Anders Carlsson7624f212009-09-18 02:42:01 +0000497 if (mangleSubstitution(ND))
Anders Carlsson03c9d532009-09-17 04:02:31 +0000498 return;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000499
Douglas Gregor32fb4e12010-02-05 20:45:00 +0000500 // <template-template-param> ::= <template-param>
501 if (const TemplateTemplateParmDecl *TTP
502 = dyn_cast<TemplateTemplateParmDecl>(ND)) {
503 mangleTemplateParameter(TTP->getIndex());
504 return;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000505 }
Douglas Gregor32fb4e12010-02-05 20:45:00 +0000506
Anders Carlsson1668f202009-09-26 20:13:56 +0000507 mangleUnscopedName(ND->getTemplatedDecl());
Anders Carlsson7624f212009-09-18 02:42:01 +0000508 addSubstitution(ND);
Anders Carlsson201ce742009-09-17 03:17:01 +0000509}
510
Douglas Gregor1e9268e2010-04-28 05:58:56 +0000511void CXXNameMangler::mangleUnscopedTemplateName(TemplateName Template) {
512 // <unscoped-template-name> ::= <unscoped-name>
513 // ::= <substitution>
514 if (TemplateDecl *TD = Template.getAsTemplateDecl())
515 return mangleUnscopedTemplateName(TD);
Sean Huntc3021132010-05-05 15:23:54 +0000516
Douglas Gregor1e9268e2010-04-28 05:58:56 +0000517 if (mangleSubstitution(Template))
518 return;
519
520 // FIXME: How to cope with operators here?
521 DependentTemplateName *Dependent = Template.getAsDependentTemplateName();
522 assert(Dependent && "Not a dependent template name?");
523 if (!Dependent->isIdentifier()) {
524 // FIXME: We can't possibly know the arity of the operator here!
525 Diagnostic &Diags = Context.getDiags();
526 unsigned DiagID = Diags.getCustomDiagID(Diagnostic::Error,
527 "cannot mangle dependent operator name");
528 Diags.Report(FullSourceLoc(), DiagID);
529 return;
530 }
Sean Huntc3021132010-05-05 15:23:54 +0000531
Douglas Gregor1e9268e2010-04-28 05:58:56 +0000532 mangleSourceName(Dependent->getIdentifier());
533 addSubstitution(Template);
534}
535
John McCall0512e482010-07-14 04:20:34 +0000536void CXXNameMangler::mangleFloat(const llvm::APFloat &F) {
537 // TODO: avoid this copy with careful stream management.
538 llvm::SmallString<20> Buffer;
539 F.bitcastToAPInt().toString(Buffer, 16, false);
540 Out.write(Buffer.data(), Buffer.size());
541}
542
543void CXXNameMangler::mangleNumber(const llvm::APSInt &Value) {
544 if (Value.isSigned() && Value.isNegative()) {
545 Out << 'n';
546 Value.abs().print(Out, true);
547 } else
548 Value.print(Out, Value.isSigned());
549}
550
Anders Carlssona94822e2009-11-26 02:32:05 +0000551void CXXNameMangler::mangleNumber(int64_t Number) {
552 // <number> ::= [n] <non-negative decimal integer>
553 if (Number < 0) {
554 Out << 'n';
555 Number = -Number;
556 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000557
Anders Carlssona94822e2009-11-26 02:32:05 +0000558 Out << Number;
559}
560
Anders Carlsson19879c92010-03-23 17:17:29 +0000561void CXXNameMangler::mangleCallOffset(int64_t NonVirtual, int64_t Virtual) {
Mike Stump141c5af2009-09-02 00:25:38 +0000562 // <call-offset> ::= h <nv-offset> _
563 // ::= v <v-offset> _
564 // <nv-offset> ::= <offset number> # non-virtual base override
Anders Carlssona94822e2009-11-26 02:32:05 +0000565 // <v-offset> ::= <offset number> _ <virtual offset number>
Mike Stump141c5af2009-09-02 00:25:38 +0000566 // # virtual base override, with vcall offset
Anders Carlsson19879c92010-03-23 17:17:29 +0000567 if (!Virtual) {
Anders Carlssona94822e2009-11-26 02:32:05 +0000568 Out << 'h';
Anders Carlsson19879c92010-03-23 17:17:29 +0000569 mangleNumber(NonVirtual);
Anders Carlssona94822e2009-11-26 02:32:05 +0000570 Out << '_';
571 return;
Mike Stump141c5af2009-09-02 00:25:38 +0000572 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000573
Anders Carlssona94822e2009-11-26 02:32:05 +0000574 Out << 'v';
Anders Carlsson19879c92010-03-23 17:17:29 +0000575 mangleNumber(NonVirtual);
Anders Carlssona94822e2009-11-26 02:32:05 +0000576 Out << '_';
Anders Carlsson19879c92010-03-23 17:17:29 +0000577 mangleNumber(Virtual);
Anders Carlssona94822e2009-11-26 02:32:05 +0000578 Out << '_';
Mike Stump9124bcc2009-09-02 00:56:18 +0000579}
580
John McCall1dd73832010-02-04 01:42:13 +0000581void CXXNameMangler::mangleUnresolvedScope(NestedNameSpecifier *Qualifier) {
582 Qualifier = getASTContext().getCanonicalNestedNameSpecifier(Qualifier);
583 switch (Qualifier->getKind()) {
584 case NestedNameSpecifier::Global:
585 // nothing
586 break;
587 case NestedNameSpecifier::Namespace:
588 mangleName(Qualifier->getAsNamespace());
589 break;
590 case NestedNameSpecifier::TypeSpec:
Rafael Espindola9b35b252010-03-17 04:28:11 +0000591 case NestedNameSpecifier::TypeSpecWithTemplate: {
592 const Type *QTy = Qualifier->getAsType();
593
594 if (const TemplateSpecializationType *TST =
595 dyn_cast<TemplateSpecializationType>(QTy)) {
596 if (!mangleSubstitution(QualType(TST, 0))) {
Douglas Gregor20f0cc72010-04-23 03:10:43 +0000597 mangleTemplatePrefix(TST->getTemplateName());
Sean Huntc3021132010-05-05 15:23:54 +0000598
Douglas Gregor20f0cc72010-04-23 03:10:43 +0000599 // FIXME: GCC does not appear to mangle the template arguments when
600 // the template in question is a dependent template name. Should we
601 // emulate that badness?
602 mangleTemplateArgs(TST->getTemplateName(), TST->getArgs(),
Rafael Espindola9b35b252010-03-17 04:28:11 +0000603 TST->getNumArgs());
604 addSubstitution(QualType(TST, 0));
605 }
606 } else {
607 // We use the QualType mangle type variant here because it handles
608 // substitutions.
609 mangleType(QualType(QTy, 0));
610 }
611 }
John McCall1dd73832010-02-04 01:42:13 +0000612 break;
613 case NestedNameSpecifier::Identifier:
John McCallad5e7382010-03-01 23:49:17 +0000614 // Member expressions can have these without prefixes.
615 if (Qualifier->getPrefix())
616 mangleUnresolvedScope(Qualifier->getPrefix());
John McCall1dd73832010-02-04 01:42:13 +0000617 mangleSourceName(Qualifier->getAsIdentifier());
618 break;
619 }
620}
621
622/// Mangles a name which was not resolved to a specific entity.
623void CXXNameMangler::mangleUnresolvedName(NestedNameSpecifier *Qualifier,
624 DeclarationName Name,
625 unsigned KnownArity) {
626 if (Qualifier)
627 mangleUnresolvedScope(Qualifier);
628 // FIXME: ambiguity of unqualified lookup with ::
629
630 mangleUnqualifiedName(0, Name, KnownArity);
631}
632
Anders Carlsson6f7e2f42010-06-08 14:49:03 +0000633static const FieldDecl *FindFirstNamedDataMember(const RecordDecl *RD) {
634 assert(RD->isAnonymousStructOrUnion() &&
635 "Expected anonymous struct or union!");
636
637 for (RecordDecl::field_iterator I = RD->field_begin(), E = RD->field_end();
638 I != E; ++I) {
639 const FieldDecl *FD = *I;
640
641 if (FD->getIdentifier())
642 return FD;
643
644 if (const RecordType *RT = FD->getType()->getAs<RecordType>()) {
645 if (const FieldDecl *NamedDataMember =
646 FindFirstNamedDataMember(RT->getDecl()))
647 return NamedDataMember;
648 }
649 }
650
651 // We didn't find a named data member.
652 return 0;
653}
654
John McCall1dd73832010-02-04 01:42:13 +0000655void CXXNameMangler::mangleUnqualifiedName(const NamedDecl *ND,
656 DeclarationName Name,
657 unsigned KnownArity) {
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000658 // <unqualified-name> ::= <operator-name>
Mike Stump1eb44332009-09-09 15:08:12 +0000659 // ::= <ctor-dtor-name>
660 // ::= <source-name>
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000661 switch (Name.getNameKind()) {
Anders Carlssonc4355b62009-10-07 01:45:02 +0000662 case DeclarationName::Identifier: {
Anders Carlssonc4355b62009-10-07 01:45:02 +0000663 if (const IdentifierInfo *II = Name.getAsIdentifierInfo()) {
Sean Hunt31455252010-01-24 03:04:27 +0000664 // We must avoid conflicts between internally- and externally-
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000665 // linked variable declaration names in the same TU.
Anders Carlssonaec25232010-02-06 04:52:27 +0000666 // This naming convention is the same as that followed by GCC, though it
667 // shouldn't actually matter.
668 if (ND && isa<VarDecl>(ND) && ND->getLinkage() == InternalLinkage &&
Sean Hunt31455252010-01-24 03:04:27 +0000669 ND->getDeclContext()->isFileContext())
670 Out << 'L';
671
Anders Carlssonc4355b62009-10-07 01:45:02 +0000672 mangleSourceName(II);
673 break;
674 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000675
John McCall1dd73832010-02-04 01:42:13 +0000676 // Otherwise, an anonymous entity. We must have a declaration.
677 assert(ND && "mangling empty name without declaration");
678
679 if (const NamespaceDecl *NS = dyn_cast<NamespaceDecl>(ND)) {
680 if (NS->isAnonymousNamespace()) {
681 // This is how gcc mangles these names.
682 Out << "12_GLOBAL__N_1";
683 break;
684 }
685 }
686
Anders Carlsson6f7e2f42010-06-08 14:49:03 +0000687 if (const VarDecl *VD = dyn_cast<VarDecl>(ND)) {
688 // We must have an anonymous union or struct declaration.
689 const RecordDecl *RD =
690 cast<RecordDecl>(VD->getType()->getAs<RecordType>()->getDecl());
691
692 // Itanium C++ ABI 5.1.2:
693 //
694 // For the purposes of mangling, the name of an anonymous union is
695 // considered to be the name of the first named data member found by a
696 // pre-order, depth-first, declaration-order walk of the data members of
697 // the anonymous union. If there is no such data member (i.e., if all of
698 // the data members in the union are unnamed), then there is no way for
699 // a program to refer to the anonymous union, and there is therefore no
700 // need to mangle its name.
701 const FieldDecl *FD = FindFirstNamedDataMember(RD);
John McCall7121c8f2010-08-05 22:02:13 +0000702
703 // It's actually possible for various reasons for us to get here
704 // with an empty anonymous struct / union. Fortunately, it
705 // doesn't really matter what name we generate.
706 if (!FD) break;
Anders Carlsson6f7e2f42010-06-08 14:49:03 +0000707 assert(FD->getIdentifier() && "Data member name isn't an identifier!");
708
709 mangleSourceName(FD->getIdentifier());
710 break;
711 }
712
Anders Carlssonc4355b62009-10-07 01:45:02 +0000713 // We must have an anonymous struct.
714 const TagDecl *TD = cast<TagDecl>(ND);
715 if (const TypedefDecl *D = TD->getTypedefForAnonDecl()) {
716 assert(TD->getDeclContext() == D->getDeclContext() &&
717 "Typedef should not be in another decl context!");
718 assert(D->getDeclName().getAsIdentifierInfo() &&
719 "Typedef was not named!");
720 mangleSourceName(D->getDeclName().getAsIdentifierInfo());
721 break;
722 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000723
Anders Carlssonc4355b62009-10-07 01:45:02 +0000724 // Get a unique id for the anonymous struct.
725 uint64_t AnonStructId = Context.getAnonymousStructId(TD);
726
727 // Mangle it as a source name in the form
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000728 // [n] $_<id>
Anders Carlssonc4355b62009-10-07 01:45:02 +0000729 // where n is the length of the string.
730 llvm::SmallString<8> Str;
731 Str += "$_";
732 Str += llvm::utostr(AnonStructId);
733
734 Out << Str.size();
735 Out << Str.str();
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000736 break;
Anders Carlssonc4355b62009-10-07 01:45:02 +0000737 }
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000738
739 case DeclarationName::ObjCZeroArgSelector:
740 case DeclarationName::ObjCOneArgSelector:
741 case DeclarationName::ObjCMultiArgSelector:
742 assert(false && "Can't mangle Objective-C selector names here!");
743 break;
744
745 case DeclarationName::CXXConstructorName:
Anders Carlsson27ae5362009-04-17 01:58:57 +0000746 if (ND == Structor)
Mike Stump141c5af2009-09-02 00:25:38 +0000747 // If the named decl is the C++ constructor we're mangling, use the type
748 // we were given.
Anders Carlsson27ae5362009-04-17 01:58:57 +0000749 mangleCXXCtorType(static_cast<CXXCtorType>(StructorType));
Anders Carlsson3ac86b52009-04-15 05:36:58 +0000750 else
751 // Otherwise, use the complete constructor name. This is relevant if a
752 // class with a constructor is declared within a constructor.
753 mangleCXXCtorType(Ctor_Complete);
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000754 break;
755
756 case DeclarationName::CXXDestructorName:
Anders Carlsson27ae5362009-04-17 01:58:57 +0000757 if (ND == Structor)
Mike Stump141c5af2009-09-02 00:25:38 +0000758 // If the named decl is the C++ destructor we're mangling, use the type we
759 // were given.
Anders Carlsson27ae5362009-04-17 01:58:57 +0000760 mangleCXXDtorType(static_cast<CXXDtorType>(StructorType));
761 else
762 // Otherwise, use the complete destructor name. This is relevant if a
763 // class with a destructor is declared within a destructor.
764 mangleCXXDtorType(Dtor_Complete);
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000765 break;
766
767 case DeclarationName::CXXConversionFunctionName:
Mike Stump1eb44332009-09-09 15:08:12 +0000768 // <operator-name> ::= cv <type> # (cast)
Douglas Gregor219cc612009-02-13 01:28:03 +0000769 Out << "cv";
Anders Carlssonb5404912009-10-07 01:06:45 +0000770 mangleType(Context.getASTContext().getCanonicalType(Name.getCXXNameType()));
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000771 break;
772
Anders Carlsson8257d412009-12-22 06:36:32 +0000773 case DeclarationName::CXXOperatorName: {
John McCall1dd73832010-02-04 01:42:13 +0000774 unsigned Arity;
775 if (ND) {
776 Arity = cast<FunctionDecl>(ND)->getNumParams();
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000777
John McCall1dd73832010-02-04 01:42:13 +0000778 // If we have a C++ member function, we need to include the 'this' pointer.
779 // FIXME: This does not make sense for operators that are static, but their
780 // names stay the same regardless of the arity (operator new for instance).
781 if (isa<CXXMethodDecl>(ND))
782 Arity++;
783 } else
784 Arity = KnownArity;
785
Anders Carlsson8257d412009-12-22 06:36:32 +0000786 mangleOperatorName(Name.getCXXOverloadedOperator(), Arity);
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000787 break;
Anders Carlsson8257d412009-12-22 06:36:32 +0000788 }
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000789
Sean Hunt3e518bd2009-11-29 07:34:05 +0000790 case DeclarationName::CXXLiteralOperatorName:
Sean Hunt5dd6b392009-12-04 21:11:13 +0000791 // FIXME: This mangling is not yet official.
Sean Hunt2421f662009-12-04 21:01:37 +0000792 Out << "li";
Sean Hunt3e518bd2009-11-29 07:34:05 +0000793 mangleSourceName(Name.getCXXLiteralIdentifier());
794 break;
795
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000796 case DeclarationName::CXXUsingDirective:
797 assert(false && "Can't mangle a using directive name!");
Douglas Gregor219cc612009-02-13 01:28:03 +0000798 break;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000799 }
800}
801
802void CXXNameMangler::mangleSourceName(const IdentifierInfo *II) {
803 // <source-name> ::= <positive length number> <identifier>
804 // <number> ::= [n] <non-negative decimal integer>
805 // <identifier> ::= <unqualified source code identifier>
806 Out << II->getLength() << II->getName();
807}
808
Eli Friedman7facf842009-12-02 20:32:49 +0000809void CXXNameMangler::mangleNestedName(const NamedDecl *ND,
Fariborz Jahanian57058532010-03-03 19:41:08 +0000810 const DeclContext *DC,
811 bool NoFunction) {
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000812 // <nested-name> ::= N [<CV-qualifiers>] <prefix> <unqualified-name> E
813 // ::= N [<CV-qualifiers>] <template-prefix> <template-args> E
Anders Carlssond99edc42009-09-26 03:55:37 +0000814
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000815 Out << 'N';
816 if (const CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(ND))
John McCall0953e762009-09-24 19:53:00 +0000817 mangleQualifiers(Qualifiers::fromCVRMask(Method->getTypeQualifiers()));
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000818
Anders Carlsson2744a062009-09-18 19:00:18 +0000819 // Check if we have a template.
820 const TemplateArgumentList *TemplateArgs = 0;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000821 if (const TemplateDecl *TD = isTemplate(ND, TemplateArgs)) {
Anders Carlsson2744a062009-09-18 19:00:18 +0000822 mangleTemplatePrefix(TD);
Rafael Espindolad9800722010-03-11 14:07:00 +0000823 TemplateParameterList *TemplateParameters = TD->getTemplateParameters();
824 mangleTemplateArgs(*TemplateParameters, *TemplateArgs);
Fariborz Jahanian57058532010-03-03 19:41:08 +0000825 }
826 else {
827 manglePrefix(DC, NoFunction);
Anders Carlsson7482e242009-09-18 04:29:09 +0000828 mangleUnqualifiedName(ND);
829 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000830
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000831 Out << 'E';
832}
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000833void CXXNameMangler::mangleNestedName(const TemplateDecl *TD,
Anders Carlsson7624f212009-09-18 02:42:01 +0000834 const TemplateArgument *TemplateArgs,
835 unsigned NumTemplateArgs) {
Anders Carlssone45117b2009-09-27 19:53:49 +0000836 // <nested-name> ::= N [<CV-qualifiers>] <template-prefix> <template-args> E
837
Anders Carlsson7624f212009-09-18 02:42:01 +0000838 Out << 'N';
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000839
Anders Carlssone45117b2009-09-27 19:53:49 +0000840 mangleTemplatePrefix(TD);
Rafael Espindolad9800722010-03-11 14:07:00 +0000841 TemplateParameterList *TemplateParameters = TD->getTemplateParameters();
842 mangleTemplateArgs(*TemplateParameters, TemplateArgs, NumTemplateArgs);
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000843
Anders Carlsson7624f212009-09-18 02:42:01 +0000844 Out << 'E';
845}
846
Anders Carlsson1b42c792009-04-02 16:24:45 +0000847void CXXNameMangler::mangleLocalName(const NamedDecl *ND) {
848 // <local-name> := Z <function encoding> E <entity name> [<discriminator>]
849 // := Z <function encoding> E s [<discriminator>]
Mike Stump1eb44332009-09-09 15:08:12 +0000850 // <discriminator> := _ <non-negative number>
Fariborz Jahanian57058532010-03-03 19:41:08 +0000851 const DeclContext *DC = ND->getDeclContext();
Anders Carlsson1b42c792009-04-02 16:24:45 +0000852 Out << 'Z';
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000853
Charles Davis685b1d92010-05-26 18:25:27 +0000854 if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(DC)) {
855 mangleObjCMethodName(MD);
John McCall82b7d7b2010-10-18 21:28:44 +0000856 } else if (const CXXRecordDecl *RD = GetLocalClassDecl(ND)) {
857 mangleFunctionEncoding(cast<FunctionDecl>(RD->getDeclContext()));
Fariborz Jahanian57058532010-03-03 19:41:08 +0000858 Out << 'E';
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000859
John McCall82b7d7b2010-10-18 21:28:44 +0000860 // Mangle the name relative to the closest enclosing function.
861 if (ND == RD) // equality ok because RD derived from ND above
862 mangleUnqualifiedName(ND);
863 else
864 mangleNestedName(ND, DC, true /*NoFunction*/);
865
Fariborz Jahanian4819ac42010-03-04 01:02:03 +0000866 unsigned disc;
John McCall82b7d7b2010-10-18 21:28:44 +0000867 if (Context.getNextDiscriminator(RD, disc)) {
Fariborz Jahanian4819ac42010-03-04 01:02:03 +0000868 if (disc < 10)
869 Out << '_' << disc;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000870 else
Fariborz Jahanian4819ac42010-03-04 01:02:03 +0000871 Out << "__" << disc << '_';
872 }
Fariborz Jahanian57058532010-03-03 19:41:08 +0000873
874 return;
875 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000876 else
Fariborz Jahanian57058532010-03-03 19:41:08 +0000877 mangleFunctionEncoding(cast<FunctionDecl>(DC));
Anders Carlsson7b06f6c2009-12-10 03:14:39 +0000878
Anders Carlsson1b42c792009-04-02 16:24:45 +0000879 Out << 'E';
Eli Friedman6f9f25d2009-12-11 20:21:38 +0000880 mangleUnqualifiedName(ND);
Anders Carlsson1b42c792009-04-02 16:24:45 +0000881}
882
Fariborz Jahanian57058532010-03-03 19:41:08 +0000883void CXXNameMangler::manglePrefix(const DeclContext *DC, bool NoFunction) {
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000884 // <prefix> ::= <prefix> <unqualified-name>
885 // ::= <template-prefix> <template-args>
886 // ::= <template-param>
887 // ::= # empty
888 // ::= <substitution>
Anders Carlsson6862fc72009-09-17 04:16:28 +0000889
Anders Carlssonadd28822009-09-22 20:33:31 +0000890 while (isa<LinkageSpecDecl>(DC))
891 DC = DC->getParent();
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000892
Anders Carlsson9263e912009-09-18 18:39:58 +0000893 if (DC->isTranslationUnit())
894 return;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000895
Douglas Gregor35415f52010-05-25 17:04:15 +0000896 if (const BlockDecl *Block = dyn_cast<BlockDecl>(DC)) {
897 manglePrefix(DC->getParent(), NoFunction);
898 llvm::SmallString<64> Name;
Fariborz Jahanian564360b2010-06-24 00:08:06 +0000899 Context.mangleBlock(GlobalDecl(), Block, Name);
Douglas Gregor35415f52010-05-25 17:04:15 +0000900 Out << Name.size() << Name;
901 return;
902 }
903
Anders Carlsson6862fc72009-09-17 04:16:28 +0000904 if (mangleSubstitution(cast<NamedDecl>(DC)))
905 return;
Anders Carlsson7482e242009-09-18 04:29:09 +0000906
Anders Carlsson2ee3fca2009-09-18 20:11:09 +0000907 // Check if we have a template.
908 const TemplateArgumentList *TemplateArgs = 0;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000909 if (const TemplateDecl *TD = isTemplate(cast<NamedDecl>(DC), TemplateArgs)) {
Anders Carlsson2ee3fca2009-09-18 20:11:09 +0000910 mangleTemplatePrefix(TD);
Rafael Espindolad9800722010-03-11 14:07:00 +0000911 TemplateParameterList *TemplateParameters = TD->getTemplateParameters();
912 mangleTemplateArgs(*TemplateParameters, *TemplateArgs);
Fariborz Jahanian57058532010-03-03 19:41:08 +0000913 }
Douglas Gregor35415f52010-05-25 17:04:15 +0000914 else if(NoFunction && (isa<FunctionDecl>(DC) || isa<ObjCMethodDecl>(DC)))
Fariborz Jahanian57058532010-03-03 19:41:08 +0000915 return;
Douglas Gregor35415f52010-05-25 17:04:15 +0000916 else if (const ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(DC))
917 mangleObjCMethodName(Method);
Fariborz Jahanian57058532010-03-03 19:41:08 +0000918 else {
919 manglePrefix(DC->getParent(), NoFunction);
Anders Carlsson2ee3fca2009-09-18 20:11:09 +0000920 mangleUnqualifiedName(cast<NamedDecl>(DC));
921 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000922
Anders Carlsson6862fc72009-09-17 04:16:28 +0000923 addSubstitution(cast<NamedDecl>(DC));
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000924}
925
Douglas Gregor20f0cc72010-04-23 03:10:43 +0000926void CXXNameMangler::mangleTemplatePrefix(TemplateName Template) {
927 // <template-prefix> ::= <prefix> <template unqualified-name>
928 // ::= <template-param>
929 // ::= <substitution>
930 if (TemplateDecl *TD = Template.getAsTemplateDecl())
931 return mangleTemplatePrefix(TD);
932
933 if (QualifiedTemplateName *Qualified = Template.getAsQualifiedTemplateName())
934 mangleUnresolvedScope(Qualified->getQualifier());
Sean Huntc3021132010-05-05 15:23:54 +0000935
Douglas Gregor20f0cc72010-04-23 03:10:43 +0000936 if (OverloadedTemplateStorage *Overloaded
937 = Template.getAsOverloadedTemplate()) {
Sean Huntc3021132010-05-05 15:23:54 +0000938 mangleUnqualifiedName(0, (*Overloaded->begin())->getDeclName(),
Douglas Gregor20f0cc72010-04-23 03:10:43 +0000939 UnknownArity);
940 return;
941 }
Sean Huntc3021132010-05-05 15:23:54 +0000942
Douglas Gregor20f0cc72010-04-23 03:10:43 +0000943 DependentTemplateName *Dependent = Template.getAsDependentTemplateName();
944 assert(Dependent && "Unknown template name kind?");
945 mangleUnresolvedScope(Dependent->getQualifier());
Douglas Gregor1e9268e2010-04-28 05:58:56 +0000946 mangleUnscopedTemplateName(Template);
Douglas Gregor20f0cc72010-04-23 03:10:43 +0000947}
948
Anders Carlsson0fa6df42009-09-26 19:45:45 +0000949void CXXNameMangler::mangleTemplatePrefix(const TemplateDecl *ND) {
Anders Carlsson7482e242009-09-18 04:29:09 +0000950 // <template-prefix> ::= <prefix> <template unqualified-name>
951 // ::= <template-param>
952 // ::= <substitution>
Douglas Gregor32fb4e12010-02-05 20:45:00 +0000953 // <template-template-param> ::= <template-param>
954 // <substitution>
Anders Carlsson7482e242009-09-18 04:29:09 +0000955
Anders Carlssonaeb85372009-09-26 22:18:22 +0000956 if (mangleSubstitution(ND))
957 return;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000958
Douglas Gregor32fb4e12010-02-05 20:45:00 +0000959 // <template-template-param> ::= <template-param>
960 if (const TemplateTemplateParmDecl *TTP
961 = dyn_cast<TemplateTemplateParmDecl>(ND)) {
962 mangleTemplateParameter(TTP->getIndex());
963 return;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000964 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000965
Anders Carlssonaa73ab12009-09-18 18:47:07 +0000966 manglePrefix(ND->getDeclContext());
Anders Carlsson1668f202009-09-26 20:13:56 +0000967 mangleUnqualifiedName(ND->getTemplatedDecl());
Anders Carlssonaeb85372009-09-26 22:18:22 +0000968 addSubstitution(ND);
Anders Carlsson7482e242009-09-18 04:29:09 +0000969}
970
John McCallb6f532e2010-07-14 06:43:17 +0000971/// Mangles a template name under the production <type>. Required for
972/// template template arguments.
973/// <type> ::= <class-enum-type>
974/// ::= <template-param>
975/// ::= <substitution>
976void CXXNameMangler::mangleType(TemplateName TN) {
977 if (mangleSubstitution(TN))
978 return;
979
980 TemplateDecl *TD = 0;
981
982 switch (TN.getKind()) {
983 case TemplateName::QualifiedTemplate:
984 TD = TN.getAsQualifiedTemplateName()->getTemplateDecl();
985 goto HaveDecl;
986
987 case TemplateName::Template:
988 TD = TN.getAsTemplateDecl();
989 goto HaveDecl;
990
991 HaveDecl:
992 if (isa<TemplateTemplateParmDecl>(TD))
993 mangleTemplateParameter(cast<TemplateTemplateParmDecl>(TD)->getIndex());
994 else
995 mangleName(TD);
996 break;
997
998 case TemplateName::OverloadedTemplate:
999 llvm_unreachable("can't mangle an overloaded template name as a <type>");
1000 break;
1001
1002 case TemplateName::DependentTemplate: {
1003 const DependentTemplateName *Dependent = TN.getAsDependentTemplateName();
1004 assert(Dependent->isIdentifier());
1005
1006 // <class-enum-type> ::= <name>
1007 // <name> ::= <nested-name>
1008 mangleUnresolvedScope(Dependent->getQualifier());
1009 mangleSourceName(Dependent->getIdentifier());
1010 break;
1011 }
1012
1013 }
1014
1015 addSubstitution(TN);
1016}
1017
Mike Stump1eb44332009-09-09 15:08:12 +00001018void
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001019CXXNameMangler::mangleOperatorName(OverloadedOperatorKind OO, unsigned Arity) {
1020 switch (OO) {
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001021 // <operator-name> ::= nw # new
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001022 case OO_New: Out << "nw"; break;
1023 // ::= na # new[]
1024 case OO_Array_New: Out << "na"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001025 // ::= dl # delete
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001026 case OO_Delete: Out << "dl"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001027 // ::= da # delete[]
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001028 case OO_Array_Delete: Out << "da"; break;
1029 // ::= ps # + (unary)
John McCall5e1e89b2010-08-18 19:18:59 +00001030 // ::= pl # + (binary or unknown)
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001031 case OO_Plus:
Anders Carlsson8257d412009-12-22 06:36:32 +00001032 Out << (Arity == 1? "ps" : "pl"); break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001033 // ::= ng # - (unary)
John McCall5e1e89b2010-08-18 19:18:59 +00001034 // ::= mi # - (binary or unknown)
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001035 case OO_Minus:
Anders Carlsson8257d412009-12-22 06:36:32 +00001036 Out << (Arity == 1? "ng" : "mi"); break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001037 // ::= ad # & (unary)
John McCall5e1e89b2010-08-18 19:18:59 +00001038 // ::= an # & (binary or unknown)
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001039 case OO_Amp:
Anders Carlsson8257d412009-12-22 06:36:32 +00001040 Out << (Arity == 1? "ad" : "an"); break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001041 // ::= de # * (unary)
John McCall5e1e89b2010-08-18 19:18:59 +00001042 // ::= ml # * (binary or unknown)
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001043 case OO_Star:
John McCall5e1e89b2010-08-18 19:18:59 +00001044 // Use binary when unknown.
Anders Carlsson8257d412009-12-22 06:36:32 +00001045 Out << (Arity == 1? "de" : "ml"); break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001046 // ::= co # ~
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001047 case OO_Tilde: Out << "co"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001048 // ::= dv # /
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001049 case OO_Slash: Out << "dv"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001050 // ::= rm # %
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001051 case OO_Percent: Out << "rm"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001052 // ::= or # |
1053 case OO_Pipe: Out << "or"; break;
1054 // ::= eo # ^
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001055 case OO_Caret: Out << "eo"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001056 // ::= aS # =
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001057 case OO_Equal: Out << "aS"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001058 // ::= pL # +=
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001059 case OO_PlusEqual: Out << "pL"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001060 // ::= mI # -=
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001061 case OO_MinusEqual: Out << "mI"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001062 // ::= mL # *=
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001063 case OO_StarEqual: Out << "mL"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001064 // ::= dV # /=
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001065 case OO_SlashEqual: Out << "dV"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001066 // ::= rM # %=
1067 case OO_PercentEqual: Out << "rM"; break;
1068 // ::= aN # &=
1069 case OO_AmpEqual: Out << "aN"; break;
1070 // ::= oR # |=
1071 case OO_PipeEqual: Out << "oR"; break;
1072 // ::= eO # ^=
1073 case OO_CaretEqual: Out << "eO"; break;
1074 // ::= ls # <<
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001075 case OO_LessLess: Out << "ls"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001076 // ::= rs # >>
1077 case OO_GreaterGreater: Out << "rs"; break;
1078 // ::= lS # <<=
1079 case OO_LessLessEqual: Out << "lS"; break;
1080 // ::= rS # >>=
1081 case OO_GreaterGreaterEqual: Out << "rS"; break;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001082 // ::= eq # ==
1083 case OO_EqualEqual: Out << "eq"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001084 // ::= ne # !=
1085 case OO_ExclaimEqual: Out << "ne"; break;
1086 // ::= lt # <
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001087 case OO_Less: Out << "lt"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001088 // ::= gt # >
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001089 case OO_Greater: Out << "gt"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001090 // ::= le # <=
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001091 case OO_LessEqual: Out << "le"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001092 // ::= ge # >=
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001093 case OO_GreaterEqual: Out << "ge"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001094 // ::= nt # !
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001095 case OO_Exclaim: Out << "nt"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001096 // ::= aa # &&
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001097 case OO_AmpAmp: Out << "aa"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001098 // ::= oo # ||
1099 case OO_PipePipe: Out << "oo"; break;
1100 // ::= pp # ++
1101 case OO_PlusPlus: Out << "pp"; break;
1102 // ::= mm # --
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001103 case OO_MinusMinus: Out << "mm"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001104 // ::= cm # ,
1105 case OO_Comma: Out << "cm"; break;
1106 // ::= pm # ->*
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001107 case OO_ArrowStar: Out << "pm"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001108 // ::= pt # ->
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001109 case OO_Arrow: Out << "pt"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001110 // ::= cl # ()
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001111 case OO_Call: Out << "cl"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001112 // ::= ix # []
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001113 case OO_Subscript: Out << "ix"; break;
Anders Carlssone170ba72009-12-14 01:45:37 +00001114
1115 // ::= qu # ?
1116 // The conditional operator can't be overloaded, but we still handle it when
1117 // mangling expressions.
1118 case OO_Conditional: Out << "qu"; break;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001119
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001120 case OO_None:
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001121 case NUM_OVERLOADED_OPERATORS:
Mike Stump1eb44332009-09-09 15:08:12 +00001122 assert(false && "Not an overloaded operator");
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001123 break;
1124 }
1125}
1126
John McCall0953e762009-09-24 19:53:00 +00001127void CXXNameMangler::mangleQualifiers(Qualifiers Quals) {
Mike Stump1eb44332009-09-09 15:08:12 +00001128 // <CV-qualifiers> ::= [r] [V] [K] # restrict (C99), volatile, const
John McCall0953e762009-09-24 19:53:00 +00001129 if (Quals.hasRestrict())
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001130 Out << 'r';
John McCall0953e762009-09-24 19:53:00 +00001131 if (Quals.hasVolatile())
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001132 Out << 'V';
John McCall0953e762009-09-24 19:53:00 +00001133 if (Quals.hasConst())
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001134 Out << 'K';
John McCall0953e762009-09-24 19:53:00 +00001135
Douglas Gregor56079f72010-06-14 23:15:08 +00001136 if (Quals.hasAddressSpace()) {
1137 // Extension:
1138 //
1139 // <type> ::= U <address-space-number>
1140 //
1141 // where <address-space-number> is a source name consisting of 'AS'
1142 // followed by the address space <number>.
1143 llvm::SmallString<64> ASString;
1144 ASString = "AS" + llvm::utostr_32(Quals.getAddressSpace());
1145 Out << 'U' << ASString.size() << ASString;
1146 }
1147
John McCall0953e762009-09-24 19:53:00 +00001148 // FIXME: For now, just drop all extension qualifiers on the floor.
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001149}
1150
Anders Carlsson7b06f6c2009-12-10 03:14:39 +00001151void CXXNameMangler::mangleObjCMethodName(const ObjCMethodDecl *MD) {
Charles Davis685b1d92010-05-26 18:25:27 +00001152 llvm::SmallString<64> Buffer;
1153 MiscNameMangler(Context, Buffer).mangleObjCMethodName(MD);
1154 Out << Buffer;
Anders Carlsson7b06f6c2009-12-10 03:14:39 +00001155}
1156
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001157void CXXNameMangler::mangleType(QualType T) {
Anders Carlsson4843e582009-03-10 17:07:44 +00001158 // Only operate on the canonical type!
Anders Carlssonb5404912009-10-07 01:06:45 +00001159 T = Context.getASTContext().getCanonicalType(T);
Anders Carlsson4843e582009-03-10 17:07:44 +00001160
Douglas Gregora4923eb2009-11-16 21:35:15 +00001161 bool IsSubstitutable = T.hasLocalQualifiers() || !isa<BuiltinType>(T);
Anders Carlsson76967372009-09-17 00:43:46 +00001162 if (IsSubstitutable && mangleSubstitution(T))
1163 return;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001164
Douglas Gregora4923eb2009-11-16 21:35:15 +00001165 if (Qualifiers Quals = T.getLocalQualifiers()) {
John McCall0953e762009-09-24 19:53:00 +00001166 mangleQualifiers(Quals);
1167 // Recurse: even if the qualified type isn't yet substitutable,
1168 // the unqualified type might be.
Douglas Gregora4923eb2009-11-16 21:35:15 +00001169 mangleType(T.getLocalUnqualifiedType());
Anders Carlsson76967372009-09-17 00:43:46 +00001170 } else {
1171 switch (T->getTypeClass()) {
John McCallefe6aee2009-09-05 07:56:18 +00001172#define ABSTRACT_TYPE(CLASS, PARENT)
1173#define NON_CANONICAL_TYPE(CLASS, PARENT) \
Anders Carlsson76967372009-09-17 00:43:46 +00001174 case Type::CLASS: \
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +00001175 llvm_unreachable("can't mangle non-canonical type " #CLASS "Type"); \
Anders Carlsson76967372009-09-17 00:43:46 +00001176 return;
John McCallefe6aee2009-09-05 07:56:18 +00001177#define TYPE(CLASS, PARENT) \
Anders Carlsson76967372009-09-17 00:43:46 +00001178 case Type::CLASS: \
John McCall0953e762009-09-24 19:53:00 +00001179 mangleType(static_cast<const CLASS##Type*>(T.getTypePtr())); \
Anders Carlsson76967372009-09-17 00:43:46 +00001180 break;
John McCallefe6aee2009-09-05 07:56:18 +00001181#include "clang/AST/TypeNodes.def"
Anders Carlsson76967372009-09-17 00:43:46 +00001182 }
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001183 }
Anders Carlsson76967372009-09-17 00:43:46 +00001184
1185 // Add the substitution.
1186 if (IsSubstitutable)
1187 addSubstitution(T);
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001188}
1189
Douglas Gregor1b12a3b2010-05-26 05:11:13 +00001190void CXXNameMangler::mangleNameOrStandardSubstitution(const NamedDecl *ND) {
1191 if (!mangleStandardSubstitution(ND))
1192 mangleName(ND);
1193}
1194
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001195void CXXNameMangler::mangleType(const BuiltinType *T) {
John McCallefe6aee2009-09-05 07:56:18 +00001196 // <type> ::= <builtin-type>
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001197 // <builtin-type> ::= v # void
1198 // ::= w # wchar_t
1199 // ::= b # bool
1200 // ::= c # char
1201 // ::= a # signed char
1202 // ::= h # unsigned char
1203 // ::= s # short
1204 // ::= t # unsigned short
1205 // ::= i # int
1206 // ::= j # unsigned int
1207 // ::= l # long
1208 // ::= m # unsigned long
1209 // ::= x # long long, __int64
1210 // ::= y # unsigned long long, __int64
1211 // ::= n # __int128
1212 // UNSUPPORTED: ::= o # unsigned __int128
1213 // ::= f # float
1214 // ::= d # double
1215 // ::= e # long double, __float80
1216 // UNSUPPORTED: ::= g # __float128
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001217 // UNSUPPORTED: ::= Dd # IEEE 754r decimal floating point (64 bits)
1218 // UNSUPPORTED: ::= De # IEEE 754r decimal floating point (128 bits)
1219 // UNSUPPORTED: ::= Df # IEEE 754r decimal floating point (32 bits)
1220 // UNSUPPORTED: ::= Dh # IEEE 754r half-precision floating point (16 bits)
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001221 // ::= Di # char32_t
1222 // ::= Ds # char16_t
Anders Carlssone2923682010-11-04 04:31:32 +00001223 // ::= Dn # std::nullptr_t (i.e., decltype(nullptr))
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001224 // ::= u <source-name> # vendor extended type
1225 switch (T->getKind()) {
1226 case BuiltinType::Void: Out << 'v'; break;
1227 case BuiltinType::Bool: Out << 'b'; break;
1228 case BuiltinType::Char_U: case BuiltinType::Char_S: Out << 'c'; break;
1229 case BuiltinType::UChar: Out << 'h'; break;
1230 case BuiltinType::UShort: Out << 't'; break;
1231 case BuiltinType::UInt: Out << 'j'; break;
1232 case BuiltinType::ULong: Out << 'm'; break;
1233 case BuiltinType::ULongLong: Out << 'y'; break;
Chris Lattner2df9ced2009-04-30 02:43:43 +00001234 case BuiltinType::UInt128: Out << 'o'; break;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001235 case BuiltinType::SChar: Out << 'a'; break;
1236 case BuiltinType::WChar: Out << 'w'; break;
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001237 case BuiltinType::Char16: Out << "Ds"; break;
1238 case BuiltinType::Char32: Out << "Di"; break;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001239 case BuiltinType::Short: Out << 's'; break;
1240 case BuiltinType::Int: Out << 'i'; break;
1241 case BuiltinType::Long: Out << 'l'; break;
1242 case BuiltinType::LongLong: Out << 'x'; break;
Chris Lattner2df9ced2009-04-30 02:43:43 +00001243 case BuiltinType::Int128: Out << 'n'; break;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001244 case BuiltinType::Float: Out << 'f'; break;
1245 case BuiltinType::Double: Out << 'd'; break;
1246 case BuiltinType::LongDouble: Out << 'e'; break;
Anders Carlssone2923682010-11-04 04:31:32 +00001247 case BuiltinType::NullPtr: Out << "Dn"; break;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001248
1249 case BuiltinType::Overload:
1250 case BuiltinType::Dependent:
Mike Stump1eb44332009-09-09 15:08:12 +00001251 assert(false &&
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001252 "Overloaded and dependent types shouldn't get to name mangling");
1253 break;
Anders Carlssone89d1592009-06-26 18:41:36 +00001254 case BuiltinType::UndeducedAuto:
1255 assert(0 && "Should not see undeduced auto here");
1256 break;
Steve Naroff9533a7f2009-07-22 17:14:51 +00001257 case BuiltinType::ObjCId: Out << "11objc_object"; break;
1258 case BuiltinType::ObjCClass: Out << "10objc_class"; break;
Fariborz Jahanian13dcd002009-11-21 19:53:08 +00001259 case BuiltinType::ObjCSel: Out << "13objc_selector"; break;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001260 }
1261}
1262
John McCallefe6aee2009-09-05 07:56:18 +00001263// <type> ::= <function-type>
1264// <function-type> ::= F [Y] <bare-function-type> E
1265void CXXNameMangler::mangleType(const FunctionProtoType *T) {
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001266 Out << 'F';
Mike Stumpf5408fe2009-05-16 07:57:57 +00001267 // FIXME: We don't have enough information in the AST to produce the 'Y'
1268 // encoding for extern "C" function types.
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001269 mangleBareFunctionType(T, /*MangleReturnType=*/true);
1270 Out << 'E';
1271}
John McCallefe6aee2009-09-05 07:56:18 +00001272void CXXNameMangler::mangleType(const FunctionNoProtoType *T) {
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +00001273 llvm_unreachable("Can't mangle K&R function prototypes");
John McCallefe6aee2009-09-05 07:56:18 +00001274}
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001275void CXXNameMangler::mangleBareFunctionType(const FunctionType *T,
1276 bool MangleReturnType) {
John McCallefe6aee2009-09-05 07:56:18 +00001277 // We should never be mangling something without a prototype.
1278 const FunctionProtoType *Proto = cast<FunctionProtoType>(T);
1279
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001280 // <bare-function-type> ::= <signature type>+
1281 if (MangleReturnType)
John McCallefe6aee2009-09-05 07:56:18 +00001282 mangleType(Proto->getResultType());
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001283
Anders Carlsson93296682010-06-02 04:40:13 +00001284 if (Proto->getNumArgs() == 0 && !Proto->isVariadic()) {
Eli Friedmana7e68452010-08-22 01:00:03 +00001285 // <builtin-type> ::= v # void
Anders Carlssonc6c91bc2009-04-01 00:15:23 +00001286 Out << 'v';
1287 return;
1288 }
Mike Stump1eb44332009-09-09 15:08:12 +00001289
Douglas Gregor72564e72009-02-26 23:50:07 +00001290 for (FunctionProtoType::arg_type_iterator Arg = Proto->arg_type_begin(),
Mike Stump1eb44332009-09-09 15:08:12 +00001291 ArgEnd = Proto->arg_type_end();
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001292 Arg != ArgEnd; ++Arg)
1293 mangleType(*Arg);
Douglas Gregor219cc612009-02-13 01:28:03 +00001294
1295 // <builtin-type> ::= z # ellipsis
1296 if (Proto->isVariadic())
1297 Out << 'z';
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001298}
1299
John McCallefe6aee2009-09-05 07:56:18 +00001300// <type> ::= <class-enum-type>
Mike Stump1eb44332009-09-09 15:08:12 +00001301// <class-enum-type> ::= <name>
John McCalled976492009-12-04 22:46:56 +00001302void CXXNameMangler::mangleType(const UnresolvedUsingType *T) {
1303 mangleName(T->getDecl());
1304}
1305
1306// <type> ::= <class-enum-type>
1307// <class-enum-type> ::= <name>
John McCallefe6aee2009-09-05 07:56:18 +00001308void CXXNameMangler::mangleType(const EnumType *T) {
1309 mangleType(static_cast<const TagType*>(T));
1310}
1311void CXXNameMangler::mangleType(const RecordType *T) {
1312 mangleType(static_cast<const TagType*>(T));
1313}
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001314void CXXNameMangler::mangleType(const TagType *T) {
Eli Friedmanecb7e932009-12-11 18:00:57 +00001315 mangleName(T->getDecl());
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001316}
1317
John McCallefe6aee2009-09-05 07:56:18 +00001318// <type> ::= <array-type>
1319// <array-type> ::= A <positive dimension number> _ <element type>
1320// ::= A [<dimension expression>] _ <element type>
1321void CXXNameMangler::mangleType(const ConstantArrayType *T) {
1322 Out << 'A' << T->getSize() << '_';
1323 mangleType(T->getElementType());
1324}
1325void CXXNameMangler::mangleType(const VariableArrayType *T) {
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001326 Out << 'A';
Fariborz Jahanian7281d1f2010-11-02 16:54:00 +00001327 // decayed vla types (size 0) will just be skipped.
1328 if (T->getSizeExpr())
1329 mangleExpression(T->getSizeExpr());
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001330 Out << '_';
1331 mangleType(T->getElementType());
1332}
John McCallefe6aee2009-09-05 07:56:18 +00001333void CXXNameMangler::mangleType(const DependentSizedArrayType *T) {
1334 Out << 'A';
1335 mangleExpression(T->getSizeExpr());
1336 Out << '_';
1337 mangleType(T->getElementType());
1338}
1339void CXXNameMangler::mangleType(const IncompleteArrayType *T) {
Nick Lewycky271b6652010-09-05 03:40:33 +00001340 Out << "A_";
John McCallefe6aee2009-09-05 07:56:18 +00001341 mangleType(T->getElementType());
1342}
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001343
John McCallefe6aee2009-09-05 07:56:18 +00001344// <type> ::= <pointer-to-member-type>
1345// <pointer-to-member-type> ::= M <class type> <member type>
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001346void CXXNameMangler::mangleType(const MemberPointerType *T) {
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001347 Out << 'M';
1348 mangleType(QualType(T->getClass(), 0));
Anders Carlsson0e650012009-05-17 17:41:20 +00001349 QualType PointeeType = T->getPointeeType();
1350 if (const FunctionProtoType *FPT = dyn_cast<FunctionProtoType>(PointeeType)) {
John McCall0953e762009-09-24 19:53:00 +00001351 mangleQualifiers(Qualifiers::fromCVRMask(FPT->getTypeQuals()));
Anders Carlsson0e650012009-05-17 17:41:20 +00001352 mangleType(FPT);
Anders Carlsson9d85b722010-06-02 04:29:50 +00001353
1354 // Itanium C++ ABI 5.1.8:
1355 //
1356 // The type of a non-static member function is considered to be different,
1357 // for the purposes of substitution, from the type of a namespace-scope or
1358 // static member function whose type appears similar. The types of two
1359 // non-static member functions are considered to be different, for the
1360 // purposes of substitution, if the functions are members of different
1361 // classes. In other words, for the purposes of substitution, the class of
1362 // which the function is a member is considered part of the type of
1363 // function.
1364
1365 // We increment the SeqID here to emulate adding an entry to the
1366 // substitution table. We can't actually add it because we don't want this
1367 // particular function type to be substituted.
1368 ++SeqID;
Mike Stump1eb44332009-09-09 15:08:12 +00001369 } else
Anders Carlsson0e650012009-05-17 17:41:20 +00001370 mangleType(PointeeType);
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001371}
1372
John McCallefe6aee2009-09-05 07:56:18 +00001373// <type> ::= <template-param>
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001374void CXXNameMangler::mangleType(const TemplateTypeParmType *T) {
Anders Carlsson0ccdf8d2009-09-27 00:38:53 +00001375 mangleTemplateParameter(T->getIndex());
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001376}
1377
John McCallefe6aee2009-09-05 07:56:18 +00001378// <type> ::= P <type> # pointer-to
1379void CXXNameMangler::mangleType(const PointerType *T) {
1380 Out << 'P';
1381 mangleType(T->getPointeeType());
1382}
1383void CXXNameMangler::mangleType(const ObjCObjectPointerType *T) {
1384 Out << 'P';
1385 mangleType(T->getPointeeType());
1386}
1387
1388// <type> ::= R <type> # reference-to
1389void CXXNameMangler::mangleType(const LValueReferenceType *T) {
1390 Out << 'R';
1391 mangleType(T->getPointeeType());
1392}
1393
1394// <type> ::= O <type> # rvalue reference-to (C++0x)
1395void CXXNameMangler::mangleType(const RValueReferenceType *T) {
1396 Out << 'O';
1397 mangleType(T->getPointeeType());
1398}
1399
1400// <type> ::= C <type> # complex pair (C 2000)
1401void CXXNameMangler::mangleType(const ComplexType *T) {
1402 Out << 'C';
1403 mangleType(T->getElementType());
1404}
1405
Bob Wilsonc7df92d2010-11-12 17:24:43 +00001406// ARM's ABI for Neon vector types specifies that they should be mangled as
1407// if they are structs (to match ARM's initial implementation). If the
1408// vector type is not one of the special types predefined by ARM, return false
1409// so it will be mangled as an ordinary vector type.
1410bool CXXNameMangler::mangleNeonVectorType(const VectorType *T) {
1411 QualType EltType = T->getElementType();
1412 if (!EltType->isBuiltinType())
1413 return false;
1414 unsigned EltBits = 0;
1415 const char *EltName = 0;
Bob Wilson491328c2010-11-12 17:24:46 +00001416 if (T->getVectorKind() == VectorType::NeonPolyVector) {
1417 switch (cast<BuiltinType>(EltType)->getKind()) {
1418 case BuiltinType::SChar: EltBits = 8; EltName = "poly8_t"; break;
1419 case BuiltinType::Short: EltBits = 16; EltName = "poly16_t"; break;
1420 default: return false;
1421 }
1422 } else {
1423 switch (cast<BuiltinType>(EltType)->getKind()) {
1424 case BuiltinType::SChar: EltBits = 8; EltName = "int8_t"; break;
1425 case BuiltinType::UChar: EltBits = 8; EltName = "uint8_t"; break;
1426 case BuiltinType::Short: EltBits = 16; EltName = "int16_t"; break;
1427 case BuiltinType::UShort: EltBits = 16; EltName = "uint16_t"; break;
1428 case BuiltinType::Int: EltBits = 32; EltName = "int32_t"; break;
1429 case BuiltinType::UInt: EltBits = 32; EltName = "uint32_t"; break;
1430 case BuiltinType::LongLong: EltBits = 64; EltName = "int64_t"; break;
1431 case BuiltinType::ULongLong: EltBits = 64; EltName = "uint64_t"; break;
1432 case BuiltinType::Float: EltBits = 32; EltName = "float32_t"; break;
1433 default: return false;
1434 }
Bob Wilsonc7df92d2010-11-12 17:24:43 +00001435 }
1436 const char *BaseName = 0;
1437 unsigned BitSize = T->getNumElements() * EltBits;
1438 if (BitSize == 64)
1439 BaseName = "__simd64_";
1440 else if (BitSize == 128)
1441 BaseName = "__simd128_";
1442 else
1443 return false;
1444 Out << strlen(BaseName) + strlen(EltName);
1445 Out << BaseName << EltName;
1446 return true;
1447}
1448
John McCallefe6aee2009-09-05 07:56:18 +00001449// GNU extension: vector types
Chris Lattner788b0fd2010-06-23 06:00:24 +00001450// <type> ::= <vector-type>
1451// <vector-type> ::= Dv <positive dimension number> _
1452// <extended element type>
1453// ::= Dv [<dimension expression>] _ <element type>
1454// <extended element type> ::= <element type>
1455// ::= p # AltiVec vector pixel
John McCallefe6aee2009-09-05 07:56:18 +00001456void CXXNameMangler::mangleType(const VectorType *T) {
Bob Wilson491328c2010-11-12 17:24:46 +00001457 if ((T->getVectorKind() == VectorType::NeonVector ||
1458 T->getVectorKind() == VectorType::NeonPolyVector) &&
1459 mangleNeonVectorType(T))
Bob Wilsonc7df92d2010-11-12 17:24:43 +00001460 return;
Nick Lewycky0e5f0672010-03-26 07:18:04 +00001461 Out << "Dv" << T->getNumElements() << '_';
Bob Wilsone86d78c2010-11-10 21:56:12 +00001462 if (T->getVectorKind() == VectorType::AltiVecPixel)
Chris Lattner788b0fd2010-06-23 06:00:24 +00001463 Out << 'p';
Bob Wilsone86d78c2010-11-10 21:56:12 +00001464 else if (T->getVectorKind() == VectorType::AltiVecBool)
Chris Lattner788b0fd2010-06-23 06:00:24 +00001465 Out << 'b';
1466 else
1467 mangleType(T->getElementType());
John McCallefe6aee2009-09-05 07:56:18 +00001468}
1469void CXXNameMangler::mangleType(const ExtVectorType *T) {
1470 mangleType(static_cast<const VectorType*>(T));
1471}
1472void CXXNameMangler::mangleType(const DependentSizedExtVectorType *T) {
Nick Lewycky0e5f0672010-03-26 07:18:04 +00001473 Out << "Dv";
1474 mangleExpression(T->getSizeExpr());
1475 Out << '_';
John McCallefe6aee2009-09-05 07:56:18 +00001476 mangleType(T->getElementType());
1477}
1478
Anders Carlssona40c5e42009-03-07 22:03:21 +00001479void CXXNameMangler::mangleType(const ObjCInterfaceType *T) {
1480 mangleSourceName(T->getDecl()->getIdentifier());
1481}
1482
John McCallc12c5bb2010-05-15 11:32:37 +00001483void CXXNameMangler::mangleType(const ObjCObjectType *T) {
John McCallc00c1f62010-05-15 17:06:29 +00001484 // We don't allow overloading by different protocol qualification,
1485 // so mangling them isn't necessary.
John McCallc12c5bb2010-05-15 11:32:37 +00001486 mangleType(T->getBaseType());
1487}
1488
John McCallefe6aee2009-09-05 07:56:18 +00001489void CXXNameMangler::mangleType(const BlockPointerType *T) {
Anders Carlssonf28c6872009-12-23 22:31:44 +00001490 Out << "U13block_pointer";
1491 mangleType(T->getPointeeType());
John McCallefe6aee2009-09-05 07:56:18 +00001492}
1493
John McCall31f17ec2010-04-27 00:57:59 +00001494void CXXNameMangler::mangleType(const InjectedClassNameType *T) {
1495 // Mangle injected class name types as if the user had written the
1496 // specialization out fully. It may not actually be possible to see
1497 // this mangling, though.
1498 mangleType(T->getInjectedSpecializationType());
1499}
1500
John McCallefe6aee2009-09-05 07:56:18 +00001501void CXXNameMangler::mangleType(const TemplateSpecializationType *T) {
Douglas Gregor1e9268e2010-04-28 05:58:56 +00001502 if (TemplateDecl *TD = T->getTemplateName().getAsTemplateDecl()) {
1503 mangleName(TD, T->getArgs(), T->getNumArgs());
1504 } else {
1505 if (mangleSubstitution(QualType(T, 0)))
1506 return;
Sean Huntc3021132010-05-05 15:23:54 +00001507
Douglas Gregor1e9268e2010-04-28 05:58:56 +00001508 mangleTemplatePrefix(T->getTemplateName());
Sean Huntc3021132010-05-05 15:23:54 +00001509
Douglas Gregor1e9268e2010-04-28 05:58:56 +00001510 // FIXME: GCC does not appear to mangle the template arguments when
1511 // the template in question is a dependent template name. Should we
1512 // emulate that badness?
1513 mangleTemplateArgs(T->getTemplateName(), T->getArgs(), T->getNumArgs());
1514 addSubstitution(QualType(T, 0));
1515 }
John McCallefe6aee2009-09-05 07:56:18 +00001516}
1517
Douglas Gregor4714c122010-03-31 17:34:00 +00001518void CXXNameMangler::mangleType(const DependentNameType *T) {
Anders Carlssonae352482009-09-26 02:26:02 +00001519 // Typename types are always nested
1520 Out << 'N';
John McCall33500952010-06-11 00:33:02 +00001521 mangleUnresolvedScope(T->getQualifier());
1522 mangleSourceName(T->getIdentifier());
1523 Out << 'E';
1524}
John McCall6ab30e02010-06-09 07:26:17 +00001525
John McCall33500952010-06-11 00:33:02 +00001526void CXXNameMangler::mangleType(const DependentTemplateSpecializationType *T) {
1527 // Dependently-scoped template types are always nested
1528 Out << 'N';
1529
1530 // TODO: avoid making this TemplateName.
1531 TemplateName Prefix =
1532 getASTContext().getDependentTemplateName(T->getQualifier(),
1533 T->getIdentifier());
1534 mangleTemplatePrefix(Prefix);
1535
1536 // FIXME: GCC does not appear to mangle the template arguments when
1537 // the template in question is a dependent template name. Should we
1538 // emulate that badness?
1539 mangleTemplateArgs(Prefix, T->getArgs(), T->getNumArgs());
Anders Carlssonae352482009-09-26 02:26:02 +00001540 Out << 'E';
John McCallefe6aee2009-09-05 07:56:18 +00001541}
1542
John McCallad5e7382010-03-01 23:49:17 +00001543void CXXNameMangler::mangleType(const TypeOfType *T) {
1544 // FIXME: this is pretty unsatisfactory, but there isn't an obvious
1545 // "extension with parameters" mangling.
1546 Out << "u6typeof";
1547}
1548
1549void CXXNameMangler::mangleType(const TypeOfExprType *T) {
1550 // FIXME: this is pretty unsatisfactory, but there isn't an obvious
1551 // "extension with parameters" mangling.
1552 Out << "u6typeof";
1553}
1554
1555void CXXNameMangler::mangleType(const DecltypeType *T) {
1556 Expr *E = T->getUnderlyingExpr();
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001557
John McCallad5e7382010-03-01 23:49:17 +00001558 // type ::= Dt <expression> E # decltype of an id-expression
1559 // # or class member access
1560 // ::= DT <expression> E # decltype of an expression
1561
1562 // This purports to be an exhaustive list of id-expressions and
1563 // class member accesses. Note that we do not ignore parentheses;
1564 // parentheses change the semantics of decltype for these
1565 // expressions (and cause the mangler to use the other form).
1566 if (isa<DeclRefExpr>(E) ||
1567 isa<MemberExpr>(E) ||
1568 isa<UnresolvedLookupExpr>(E) ||
1569 isa<DependentScopeDeclRefExpr>(E) ||
1570 isa<CXXDependentScopeMemberExpr>(E) ||
1571 isa<UnresolvedMemberExpr>(E))
1572 Out << "Dt";
1573 else
1574 Out << "DT";
1575 mangleExpression(E);
1576 Out << 'E';
1577}
1578
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001579void CXXNameMangler::mangleIntegerLiteral(QualType T,
Anders Carlssone170ba72009-12-14 01:45:37 +00001580 const llvm::APSInt &Value) {
1581 // <expr-primary> ::= L <type> <value number> E # integer literal
1582 Out << 'L';
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001583
Anders Carlssone170ba72009-12-14 01:45:37 +00001584 mangleType(T);
1585 if (T->isBooleanType()) {
1586 // Boolean values are encoded as 0/1.
1587 Out << (Value.getBoolValue() ? '1' : '0');
1588 } else {
John McCall0512e482010-07-14 04:20:34 +00001589 mangleNumber(Value);
Anders Carlssone170ba72009-12-14 01:45:37 +00001590 }
1591 Out << 'E';
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001592
Anders Carlssone170ba72009-12-14 01:45:37 +00001593}
1594
John McCall2f27bf82010-02-04 02:56:29 +00001595/// Mangles a member expression. Implicit accesses are not handled,
1596/// but that should be okay, because you shouldn't be able to
1597/// make an implicit access in a function template declaration.
John McCall2f27bf82010-02-04 02:56:29 +00001598void CXXNameMangler::mangleMemberExpr(const Expr *Base,
1599 bool IsArrow,
1600 NestedNameSpecifier *Qualifier,
1601 DeclarationName Member,
1602 unsigned Arity) {
John McCalle1e342f2010-03-01 19:12:25 +00001603 // gcc-4.4 uses 'dt' for dot expressions, which is reasonable.
1604 // OTOH, gcc also mangles the name as an expression.
1605 Out << (IsArrow ? "pt" : "dt");
John McCall2f27bf82010-02-04 02:56:29 +00001606 mangleExpression(Base);
1607 mangleUnresolvedName(Qualifier, Member, Arity);
1608}
1609
John McCall5e1e89b2010-08-18 19:18:59 +00001610void CXXNameMangler::mangleExpression(const Expr *E, unsigned Arity) {
Anders Carlssond553f8c2009-09-21 01:21:10 +00001611 // <expression> ::= <unary operator-name> <expression>
John McCall09cc1412010-02-03 00:55:45 +00001612 // ::= <binary operator-name> <expression> <expression>
1613 // ::= <trinary operator-name> <expression> <expression> <expression>
Eli Friedmana7e68452010-08-22 01:00:03 +00001614 // ::= cl <expression>* E # call
Anders Carlssond553f8c2009-09-21 01:21:10 +00001615 // ::= cv <type> expression # conversion with one argument
1616 // ::= cv <type> _ <expression>* E # conversion with a different number of arguments
Eli Friedmana7e68452010-08-22 01:00:03 +00001617 // ::= st <type> # sizeof (a type)
Anders Carlssond553f8c2009-09-21 01:21:10 +00001618 // ::= at <type> # alignof (a type)
1619 // ::= <template-param>
1620 // ::= <function-param>
1621 // ::= sr <type> <unqualified-name> # dependent name
1622 // ::= sr <type> <unqualified-name> <template-args> # dependent template-id
1623 // ::= sZ <template-param> # size of a parameter pack
John McCall09cc1412010-02-03 00:55:45 +00001624 // ::= <expr-primary>
John McCall1dd73832010-02-04 01:42:13 +00001625 // <expr-primary> ::= L <type> <value number> E # integer literal
1626 // ::= L <type <value float> E # floating literal
1627 // ::= L <mangled-name> E # external name
Anders Carlssond553f8c2009-09-21 01:21:10 +00001628 switch (E->getStmtClass()) {
John McCall6ae1f352010-04-09 22:26:14 +00001629 case Expr::NoStmtClass:
1630#define EXPR(Type, Base)
1631#define STMT(Type, Base) \
1632 case Expr::Type##Class:
Sean Hunt4bfe1962010-05-05 15:24:00 +00001633#include "clang/AST/StmtNodes.inc"
John McCall0512e482010-07-14 04:20:34 +00001634 // fallthrough
1635
1636 // These all can only appear in local or variable-initialization
1637 // contexts and so should never appear in a mangling.
1638 case Expr::AddrLabelExprClass:
1639 case Expr::BlockDeclRefExprClass:
1640 case Expr::CXXThisExprClass:
1641 case Expr::DesignatedInitExprClass:
1642 case Expr::ImplicitValueInitExprClass:
1643 case Expr::InitListExprClass:
1644 case Expr::ParenListExprClass:
1645 case Expr::CXXScalarValueInitExprClass:
John McCall09cc1412010-02-03 00:55:45 +00001646 llvm_unreachable("unexpected statement kind");
1647 break;
1648
John McCall0512e482010-07-14 04:20:34 +00001649 // FIXME: invent manglings for all these.
1650 case Expr::BlockExprClass:
1651 case Expr::CXXPseudoDestructorExprClass:
1652 case Expr::ChooseExprClass:
1653 case Expr::CompoundLiteralExprClass:
1654 case Expr::ExtVectorElementExprClass:
1655 case Expr::ObjCEncodeExprClass:
1656 case Expr::ObjCImplicitSetterGetterRefExprClass:
1657 case Expr::ObjCIsaExprClass:
1658 case Expr::ObjCIvarRefExprClass:
1659 case Expr::ObjCMessageExprClass:
1660 case Expr::ObjCPropertyRefExprClass:
1661 case Expr::ObjCProtocolExprClass:
1662 case Expr::ObjCSelectorExprClass:
1663 case Expr::ObjCStringLiteralClass:
John McCall0512e482010-07-14 04:20:34 +00001664 case Expr::OffsetOfExprClass:
1665 case Expr::PredefinedExprClass:
1666 case Expr::ShuffleVectorExprClass:
1667 case Expr::StmtExprClass:
1668 case Expr::TypesCompatibleExprClass:
1669 case Expr::UnaryTypeTraitExprClass:
Francois Pichet9be88402010-09-08 23:47:05 +00001670 case Expr::VAArgExprClass:
Sebastian Redl2e156222010-09-10 20:55:43 +00001671 case Expr::CXXUuidofExprClass:
1672 case Expr::CXXNoexceptExprClass: {
John McCall6ae1f352010-04-09 22:26:14 +00001673 // As bad as this diagnostic is, it's better than crashing.
1674 Diagnostic &Diags = Context.getDiags();
1675 unsigned DiagID = Diags.getCustomDiagID(Diagnostic::Error,
1676 "cannot yet mangle expression type %0");
John McCall739bf092010-04-10 09:39:25 +00001677 Diags.Report(FullSourceLoc(E->getExprLoc(),
1678 getASTContext().getSourceManager()),
1679 DiagID)
1680 << E->getStmtClassName() << E->getSourceRange();
John McCall6ae1f352010-04-09 22:26:14 +00001681 break;
1682 }
1683
John McCall0512e482010-07-14 04:20:34 +00001684 case Expr::CXXDefaultArgExprClass:
John McCall5e1e89b2010-08-18 19:18:59 +00001685 mangleExpression(cast<CXXDefaultArgExpr>(E)->getExpr(), Arity);
John McCall0512e482010-07-14 04:20:34 +00001686 break;
1687
1688 case Expr::CXXMemberCallExprClass: // fallthrough
John McCall1dd73832010-02-04 01:42:13 +00001689 case Expr::CallExprClass: {
1690 const CallExpr *CE = cast<CallExpr>(E);
1691 Out << "cl";
John McCall5e1e89b2010-08-18 19:18:59 +00001692 mangleExpression(CE->getCallee(), CE->getNumArgs());
John McCall1dd73832010-02-04 01:42:13 +00001693 for (unsigned I = 0, N = CE->getNumArgs(); I != N; ++I)
1694 mangleExpression(CE->getArg(I));
Benjamin Kramer35f59b62010-04-10 16:03:31 +00001695 Out << 'E';
John McCall09cc1412010-02-03 00:55:45 +00001696 break;
John McCall1dd73832010-02-04 01:42:13 +00001697 }
John McCall09cc1412010-02-03 00:55:45 +00001698
John McCall0512e482010-07-14 04:20:34 +00001699 case Expr::CXXNewExprClass: {
1700 // Proposal from David Vandervoorde, 2010.06.30
1701 const CXXNewExpr *New = cast<CXXNewExpr>(E);
1702 if (New->isGlobalNew()) Out << "gs";
1703 Out << (New->isArray() ? "na" : "nw");
1704 for (CXXNewExpr::const_arg_iterator I = New->placement_arg_begin(),
1705 E = New->placement_arg_end(); I != E; ++I)
1706 mangleExpression(*I);
1707 Out << '_';
1708 mangleType(New->getAllocatedType());
1709 if (New->hasInitializer()) {
1710 Out << "pi";
1711 for (CXXNewExpr::const_arg_iterator I = New->constructor_arg_begin(),
1712 E = New->constructor_arg_end(); I != E; ++I)
1713 mangleExpression(*I);
1714 }
1715 Out << 'E';
1716 break;
1717 }
1718
John McCall2f27bf82010-02-04 02:56:29 +00001719 case Expr::MemberExprClass: {
1720 const MemberExpr *ME = cast<MemberExpr>(E);
1721 mangleMemberExpr(ME->getBase(), ME->isArrow(),
1722 ME->getQualifier(), ME->getMemberDecl()->getDeclName(),
John McCall5e1e89b2010-08-18 19:18:59 +00001723 Arity);
John McCall2f27bf82010-02-04 02:56:29 +00001724 break;
1725 }
1726
1727 case Expr::UnresolvedMemberExprClass: {
1728 const UnresolvedMemberExpr *ME = cast<UnresolvedMemberExpr>(E);
1729 mangleMemberExpr(ME->getBase(), ME->isArrow(),
1730 ME->getQualifier(), ME->getMemberName(),
John McCall5e1e89b2010-08-18 19:18:59 +00001731 Arity);
John McCall6dbce192010-08-20 00:17:19 +00001732 if (ME->hasExplicitTemplateArgs())
1733 mangleTemplateArgs(ME->getExplicitTemplateArgs());
John McCall2f27bf82010-02-04 02:56:29 +00001734 break;
1735 }
1736
1737 case Expr::CXXDependentScopeMemberExprClass: {
1738 const CXXDependentScopeMemberExpr *ME
1739 = cast<CXXDependentScopeMemberExpr>(E);
1740 mangleMemberExpr(ME->getBase(), ME->isArrow(),
1741 ME->getQualifier(), ME->getMember(),
John McCall5e1e89b2010-08-18 19:18:59 +00001742 Arity);
John McCall6dbce192010-08-20 00:17:19 +00001743 if (ME->hasExplicitTemplateArgs())
1744 mangleTemplateArgs(ME->getExplicitTemplateArgs());
John McCall2f27bf82010-02-04 02:56:29 +00001745 break;
1746 }
1747
John McCall1dd73832010-02-04 01:42:13 +00001748 case Expr::UnresolvedLookupExprClass: {
John McCalla3218e72010-02-04 01:48:38 +00001749 // The ABI doesn't cover how to mangle overload sets, so we mangle
1750 // using something as close as possible to the original lookup
1751 // expression.
John McCall1dd73832010-02-04 01:42:13 +00001752 const UnresolvedLookupExpr *ULE = cast<UnresolvedLookupExpr>(E);
John McCall5e1e89b2010-08-18 19:18:59 +00001753 mangleUnresolvedName(ULE->getQualifier(), ULE->getName(), Arity);
John McCall6dbce192010-08-20 00:17:19 +00001754 if (ULE->hasExplicitTemplateArgs())
1755 mangleTemplateArgs(ULE->getExplicitTemplateArgs());
John McCall1dd73832010-02-04 01:42:13 +00001756 break;
1757 }
1758
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001759 case Expr::CXXUnresolvedConstructExprClass: {
John McCall1dd73832010-02-04 01:42:13 +00001760 const CXXUnresolvedConstructExpr *CE = cast<CXXUnresolvedConstructExpr>(E);
1761 unsigned N = CE->arg_size();
1762
1763 Out << "cv";
1764 mangleType(CE->getType());
Benjamin Kramer35f59b62010-04-10 16:03:31 +00001765 if (N != 1) Out << '_';
John McCall1dd73832010-02-04 01:42:13 +00001766 for (unsigned I = 0; I != N; ++I) mangleExpression(CE->getArg(I));
Benjamin Kramer35f59b62010-04-10 16:03:31 +00001767 if (N != 1) Out << 'E';
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001768 break;
John McCall1dd73832010-02-04 01:42:13 +00001769 }
John McCall09cc1412010-02-03 00:55:45 +00001770
John McCall1dd73832010-02-04 01:42:13 +00001771 case Expr::CXXTemporaryObjectExprClass:
1772 case Expr::CXXConstructExprClass: {
1773 const CXXConstructExpr *CE = cast<CXXConstructExpr>(E);
1774 unsigned N = CE->getNumArgs();
1775
1776 Out << "cv";
1777 mangleType(CE->getType());
Benjamin Kramer35f59b62010-04-10 16:03:31 +00001778 if (N != 1) Out << '_';
John McCall1dd73832010-02-04 01:42:13 +00001779 for (unsigned I = 0; I != N; ++I) mangleExpression(CE->getArg(I));
Benjamin Kramer35f59b62010-04-10 16:03:31 +00001780 if (N != 1) Out << 'E';
John McCall09cc1412010-02-03 00:55:45 +00001781 break;
John McCall1dd73832010-02-04 01:42:13 +00001782 }
1783
1784 case Expr::SizeOfAlignOfExprClass: {
1785 const SizeOfAlignOfExpr *SAE = cast<SizeOfAlignOfExpr>(E);
Benjamin Kramer35f59b62010-04-10 16:03:31 +00001786 if (SAE->isSizeOf()) Out << 's';
1787 else Out << 'a';
John McCall1dd73832010-02-04 01:42:13 +00001788 if (SAE->isArgumentType()) {
Benjamin Kramer35f59b62010-04-10 16:03:31 +00001789 Out << 't';
John McCall1dd73832010-02-04 01:42:13 +00001790 mangleType(SAE->getArgumentType());
1791 } else {
Benjamin Kramer35f59b62010-04-10 16:03:31 +00001792 Out << 'z';
John McCall1dd73832010-02-04 01:42:13 +00001793 mangleExpression(SAE->getArgumentExpr());
1794 }
1795 break;
1796 }
Anders Carlssona7694082009-11-06 02:50:19 +00001797
John McCall0512e482010-07-14 04:20:34 +00001798 case Expr::CXXThrowExprClass: {
1799 const CXXThrowExpr *TE = cast<CXXThrowExpr>(E);
1800
1801 // Proposal from David Vandervoorde, 2010.06.30
1802 if (TE->getSubExpr()) {
1803 Out << "tw";
1804 mangleExpression(TE->getSubExpr());
1805 } else {
1806 Out << "tr";
1807 }
1808 break;
1809 }
1810
1811 case Expr::CXXTypeidExprClass: {
1812 const CXXTypeidExpr *TIE = cast<CXXTypeidExpr>(E);
1813
1814 // Proposal from David Vandervoorde, 2010.06.30
1815 if (TIE->isTypeOperand()) {
1816 Out << "ti";
1817 mangleType(TIE->getTypeOperand());
1818 } else {
1819 Out << "te";
1820 mangleExpression(TIE->getExprOperand());
1821 }
1822 break;
1823 }
1824
1825 case Expr::CXXDeleteExprClass: {
1826 const CXXDeleteExpr *DE = cast<CXXDeleteExpr>(E);
1827
1828 // Proposal from David Vandervoorde, 2010.06.30
1829 if (DE->isGlobalDelete()) Out << "gs";
1830 Out << (DE->isArrayForm() ? "da" : "dl");
1831 mangleExpression(DE->getArgument());
1832 break;
1833 }
1834
Anders Carlssone170ba72009-12-14 01:45:37 +00001835 case Expr::UnaryOperatorClass: {
1836 const UnaryOperator *UO = cast<UnaryOperator>(E);
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001837 mangleOperatorName(UnaryOperator::getOverloadedOperator(UO->getOpcode()),
Anders Carlssone170ba72009-12-14 01:45:37 +00001838 /*Arity=*/1);
1839 mangleExpression(UO->getSubExpr());
1840 break;
1841 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001842
John McCall0512e482010-07-14 04:20:34 +00001843 case Expr::ArraySubscriptExprClass: {
1844 const ArraySubscriptExpr *AE = cast<ArraySubscriptExpr>(E);
1845
1846 // Array subscript is treated as a syntactically wierd form of
1847 // binary operator.
1848 Out << "ix";
1849 mangleExpression(AE->getLHS());
1850 mangleExpression(AE->getRHS());
1851 break;
1852 }
1853
1854 case Expr::CompoundAssignOperatorClass: // fallthrough
Anders Carlssone170ba72009-12-14 01:45:37 +00001855 case Expr::BinaryOperatorClass: {
1856 const BinaryOperator *BO = cast<BinaryOperator>(E);
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001857 mangleOperatorName(BinaryOperator::getOverloadedOperator(BO->getOpcode()),
Anders Carlssone170ba72009-12-14 01:45:37 +00001858 /*Arity=*/2);
1859 mangleExpression(BO->getLHS());
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001860 mangleExpression(BO->getRHS());
Anders Carlssone170ba72009-12-14 01:45:37 +00001861 break;
John McCall2f27bf82010-02-04 02:56:29 +00001862 }
Anders Carlssone170ba72009-12-14 01:45:37 +00001863
1864 case Expr::ConditionalOperatorClass: {
1865 const ConditionalOperator *CO = cast<ConditionalOperator>(E);
1866 mangleOperatorName(OO_Conditional, /*Arity=*/3);
1867 mangleExpression(CO->getCond());
John McCall5e1e89b2010-08-18 19:18:59 +00001868 mangleExpression(CO->getLHS(), Arity);
1869 mangleExpression(CO->getRHS(), Arity);
Anders Carlssone170ba72009-12-14 01:45:37 +00001870 break;
1871 }
1872
Douglas Gregor46287c72010-01-29 16:37:09 +00001873 case Expr::ImplicitCastExprClass: {
John McCall5e1e89b2010-08-18 19:18:59 +00001874 mangleExpression(cast<ImplicitCastExpr>(E)->getSubExpr(), Arity);
Douglas Gregor46287c72010-01-29 16:37:09 +00001875 break;
1876 }
1877
1878 case Expr::CStyleCastExprClass:
1879 case Expr::CXXStaticCastExprClass:
1880 case Expr::CXXDynamicCastExprClass:
1881 case Expr::CXXReinterpretCastExprClass:
1882 case Expr::CXXConstCastExprClass:
1883 case Expr::CXXFunctionalCastExprClass: {
1884 const ExplicitCastExpr *ECE = cast<ExplicitCastExpr>(E);
1885 Out << "cv";
1886 mangleType(ECE->getType());
1887 mangleExpression(ECE->getSubExpr());
1888 break;
1889 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001890
Anders Carlsson58040a52009-12-16 05:48:46 +00001891 case Expr::CXXOperatorCallExprClass: {
1892 const CXXOperatorCallExpr *CE = cast<CXXOperatorCallExpr>(E);
1893 unsigned NumArgs = CE->getNumArgs();
1894 mangleOperatorName(CE->getOperator(), /*Arity=*/NumArgs);
1895 // Mangle the arguments.
1896 for (unsigned i = 0; i != NumArgs; ++i)
1897 mangleExpression(CE->getArg(i));
1898 break;
1899 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001900
Anders Carlssona7694082009-11-06 02:50:19 +00001901 case Expr::ParenExprClass:
John McCall5e1e89b2010-08-18 19:18:59 +00001902 mangleExpression(cast<ParenExpr>(E)->getSubExpr(), Arity);
Anders Carlssona7694082009-11-06 02:50:19 +00001903 break;
1904
Anders Carlssond553f8c2009-09-21 01:21:10 +00001905 case Expr::DeclRefExprClass: {
Douglas Gregor5ed1bc32010-02-28 21:40:32 +00001906 const NamedDecl *D = cast<DeclRefExpr>(E)->getDecl();
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00001907
Anders Carlssond553f8c2009-09-21 01:21:10 +00001908 switch (D->getKind()) {
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001909 default:
Douglas Gregor5ed1bc32010-02-28 21:40:32 +00001910 // <expr-primary> ::= L <mangled-name> E # external name
1911 Out << 'L';
1912 mangle(D, "_Z");
1913 Out << 'E';
1914 break;
1915
John McCall3dc7e7b2010-07-24 01:17:35 +00001916 case Decl::EnumConstant: {
1917 const EnumConstantDecl *ED = cast<EnumConstantDecl>(D);
1918 mangleIntegerLiteral(ED->getType(), ED->getInitVal());
1919 break;
1920 }
1921
Anders Carlssond553f8c2009-09-21 01:21:10 +00001922 case Decl::NonTypeTemplateParm: {
1923 const NonTypeTemplateParmDecl *PD = cast<NonTypeTemplateParmDecl>(D);
Anders Carlsson0ccdf8d2009-09-27 00:38:53 +00001924 mangleTemplateParameter(PD->getIndex());
Anders Carlssond553f8c2009-09-21 01:21:10 +00001925 break;
1926 }
1927
1928 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00001929
Anders Carlsson50755b02009-09-27 20:11:34 +00001930 break;
Anders Carlssond553f8c2009-09-21 01:21:10 +00001931 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00001932
John McCall865d4472009-11-19 22:55:06 +00001933 case Expr::DependentScopeDeclRefExprClass: {
1934 const DependentScopeDeclRefExpr *DRE = cast<DependentScopeDeclRefExpr>(E);
Douglas Gregor4b2ccfc2010-02-28 22:05:49 +00001935 NestedNameSpecifier *NNS = DRE->getQualifier();
1936 const Type *QTy = NNS->getAsType();
1937
1938 // When we're dealing with a nested-name-specifier that has just a
1939 // dependent identifier in it, mangle that as a typename. FIXME:
1940 // It isn't clear that we ever actually want to have such a
1941 // nested-name-specifier; why not just represent it as a typename type?
1942 if (!QTy && NNS->getAsIdentifier() && NNS->getPrefix()) {
Douglas Gregor4a2023f2010-03-31 20:19:30 +00001943 QTy = getASTContext().getDependentNameType(ETK_Typename,
1944 NNS->getPrefix(),
1945 NNS->getAsIdentifier())
Douglas Gregor4b2ccfc2010-02-28 22:05:49 +00001946 .getTypePtr();
1947 }
Anders Carlsson50755b02009-09-27 20:11:34 +00001948 assert(QTy && "Qualifier was not type!");
1949
John McCall6dbce192010-08-20 00:17:19 +00001950 // ::= sr <type> <unqualified-name> # dependent name
1951 // ::= sr <type> <unqualified-name> <template-args> # dependent template-id
Anders Carlsson50755b02009-09-27 20:11:34 +00001952 Out << "sr";
1953 mangleType(QualType(QTy, 0));
John McCall5e1e89b2010-08-18 19:18:59 +00001954 mangleUnqualifiedName(0, DRE->getDeclName(), Arity);
John McCall6dbce192010-08-20 00:17:19 +00001955 if (DRE->hasExplicitTemplateArgs())
1956 mangleTemplateArgs(DRE->getExplicitTemplateArgs());
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00001957
Anders Carlsson50755b02009-09-27 20:11:34 +00001958 break;
1959 }
1960
John McCalld9307602010-04-09 22:54:09 +00001961 case Expr::CXXBindTemporaryExprClass:
1962 mangleExpression(cast<CXXBindTemporaryExpr>(E)->getSubExpr());
1963 break;
1964
1965 case Expr::CXXExprWithTemporariesClass:
John McCall5e1e89b2010-08-18 19:18:59 +00001966 mangleExpression(cast<CXXExprWithTemporaries>(E)->getSubExpr(), Arity);
John McCalld9307602010-04-09 22:54:09 +00001967 break;
1968
John McCall1dd73832010-02-04 01:42:13 +00001969 case Expr::FloatingLiteralClass: {
1970 const FloatingLiteral *FL = cast<FloatingLiteral>(E);
Benjamin Kramer35f59b62010-04-10 16:03:31 +00001971 Out << 'L';
John McCall1dd73832010-02-04 01:42:13 +00001972 mangleType(FL->getType());
John McCall0512e482010-07-14 04:20:34 +00001973 mangleFloat(FL->getValue());
Benjamin Kramer35f59b62010-04-10 16:03:31 +00001974 Out << 'E';
John McCall1dd73832010-02-04 01:42:13 +00001975 break;
1976 }
1977
John McCallde810632010-04-09 21:48:08 +00001978 case Expr::CharacterLiteralClass:
Benjamin Kramer35f59b62010-04-10 16:03:31 +00001979 Out << 'L';
John McCallde810632010-04-09 21:48:08 +00001980 mangleType(E->getType());
1981 Out << cast<CharacterLiteral>(E)->getValue();
1982 Out << 'E';
1983 break;
1984
1985 case Expr::CXXBoolLiteralExprClass:
1986 Out << "Lb";
1987 Out << (cast<CXXBoolLiteralExpr>(E)->getValue() ? '1' : '0');
1988 Out << 'E';
1989 break;
1990
John McCall0512e482010-07-14 04:20:34 +00001991 case Expr::IntegerLiteralClass: {
1992 llvm::APSInt Value(cast<IntegerLiteral>(E)->getValue());
1993 if (E->getType()->isSignedIntegerType())
1994 Value.setIsSigned(true);
1995 mangleIntegerLiteral(E->getType(), Value);
Anders Carlssone170ba72009-12-14 01:45:37 +00001996 break;
John McCall0512e482010-07-14 04:20:34 +00001997 }
1998
1999 case Expr::ImaginaryLiteralClass: {
2000 const ImaginaryLiteral *IE = cast<ImaginaryLiteral>(E);
2001 // Mangle as if a complex literal.
Nick Lewycky271b6652010-09-05 03:40:33 +00002002 // Proposal from David Vandevoorde, 2010.06.30.
John McCall0512e482010-07-14 04:20:34 +00002003 Out << 'L';
2004 mangleType(E->getType());
2005 if (const FloatingLiteral *Imag =
2006 dyn_cast<FloatingLiteral>(IE->getSubExpr())) {
2007 // Mangle a floating-point zero of the appropriate type.
2008 mangleFloat(llvm::APFloat(Imag->getValue().getSemantics()));
2009 Out << '_';
2010 mangleFloat(Imag->getValue());
2011 } else {
Nick Lewycky271b6652010-09-05 03:40:33 +00002012 Out << "0_";
John McCall0512e482010-07-14 04:20:34 +00002013 llvm::APSInt Value(cast<IntegerLiteral>(IE->getSubExpr())->getValue());
2014 if (IE->getSubExpr()->getType()->isSignedIntegerType())
2015 Value.setIsSigned(true);
2016 mangleNumber(Value);
2017 }
2018 Out << 'E';
2019 break;
2020 }
2021
2022 case Expr::StringLiteralClass: {
John McCall1658c392010-07-15 21:53:03 +00002023 // Revised proposal from David Vandervoorde, 2010.07.15.
John McCall0512e482010-07-14 04:20:34 +00002024 Out << 'L';
John McCall1658c392010-07-15 21:53:03 +00002025 assert(isa<ConstantArrayType>(E->getType()));
2026 mangleType(E->getType());
John McCall0512e482010-07-14 04:20:34 +00002027 Out << 'E';
2028 break;
2029 }
2030
2031 case Expr::GNUNullExprClass:
2032 // FIXME: should this really be mangled the same as nullptr?
2033 // fallthrough
2034
2035 case Expr::CXXNullPtrLiteralExprClass: {
2036 // Proposal from David Vandervoorde, 2010.06.30, as
2037 // modified by ABI list discussion.
2038 Out << "LDnE";
2039 break;
2040 }
Anders Carlssone170ba72009-12-14 01:45:37 +00002041
Anders Carlssond553f8c2009-09-21 01:21:10 +00002042 }
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00002043}
2044
Anders Carlsson3ac86b52009-04-15 05:36:58 +00002045void CXXNameMangler::mangleCXXCtorType(CXXCtorType T) {
2046 // <ctor-dtor-name> ::= C1 # complete object constructor
2047 // ::= C2 # base object constructor
2048 // ::= C3 # complete object allocating constructor
2049 //
2050 switch (T) {
2051 case Ctor_Complete:
2052 Out << "C1";
2053 break;
2054 case Ctor_Base:
2055 Out << "C2";
2056 break;
2057 case Ctor_CompleteAllocating:
2058 Out << "C3";
2059 break;
2060 }
2061}
2062
Anders Carlsson27ae5362009-04-17 01:58:57 +00002063void CXXNameMangler::mangleCXXDtorType(CXXDtorType T) {
2064 // <ctor-dtor-name> ::= D0 # deleting destructor
2065 // ::= D1 # complete object destructor
2066 // ::= D2 # base object destructor
2067 //
2068 switch (T) {
2069 case Dtor_Deleting:
2070 Out << "D0";
2071 break;
2072 case Dtor_Complete:
2073 Out << "D1";
2074 break;
2075 case Dtor_Base:
2076 Out << "D2";
2077 break;
2078 }
2079}
2080
John McCall6dbce192010-08-20 00:17:19 +00002081void CXXNameMangler::mangleTemplateArgs(
2082 const ExplicitTemplateArgumentList &TemplateArgs) {
2083 // <template-args> ::= I <template-arg>+ E
2084 Out << 'I';
2085 for (unsigned I = 0, E = TemplateArgs.NumTemplateArgs; I != E; ++I)
2086 mangleTemplateArg(0, TemplateArgs.getTemplateArgs()[I].getArgument());
2087 Out << 'E';
2088}
2089
Douglas Gregor20f0cc72010-04-23 03:10:43 +00002090void CXXNameMangler::mangleTemplateArgs(TemplateName Template,
2091 const TemplateArgument *TemplateArgs,
2092 unsigned NumTemplateArgs) {
2093 if (TemplateDecl *TD = Template.getAsTemplateDecl())
2094 return mangleTemplateArgs(*TD->getTemplateParameters(), TemplateArgs,
2095 NumTemplateArgs);
Sean Huntc3021132010-05-05 15:23:54 +00002096
Douglas Gregor20f0cc72010-04-23 03:10:43 +00002097 // <template-args> ::= I <template-arg>+ E
2098 Out << 'I';
2099 for (unsigned i = 0; i != NumTemplateArgs; ++i)
2100 mangleTemplateArg(0, TemplateArgs[i]);
2101 Out << 'E';
2102}
2103
Rafael Espindolad9800722010-03-11 14:07:00 +00002104void CXXNameMangler::mangleTemplateArgs(const TemplateParameterList &PL,
2105 const TemplateArgumentList &AL) {
Anders Carlsson7a0ba872009-05-15 16:09:15 +00002106 // <template-args> ::= I <template-arg>+ E
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002107 Out << 'I';
Rafael Espindolad9800722010-03-11 14:07:00 +00002108 for (unsigned i = 0, e = AL.size(); i != e; ++i)
2109 mangleTemplateArg(PL.getParam(i), AL[i]);
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002110 Out << 'E';
Anders Carlsson7a0ba872009-05-15 16:09:15 +00002111}
2112
Rafael Espindolad9800722010-03-11 14:07:00 +00002113void CXXNameMangler::mangleTemplateArgs(const TemplateParameterList &PL,
2114 const TemplateArgument *TemplateArgs,
Anders Carlsson7624f212009-09-18 02:42:01 +00002115 unsigned NumTemplateArgs) {
2116 // <template-args> ::= I <template-arg>+ E
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002117 Out << 'I';
Daniel Dunbar7e0c1952009-11-21 09:17:15 +00002118 for (unsigned i = 0; i != NumTemplateArgs; ++i)
Rafael Espindolad9800722010-03-11 14:07:00 +00002119 mangleTemplateArg(PL.getParam(i), TemplateArgs[i]);
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002120 Out << 'E';
Anders Carlsson7624f212009-09-18 02:42:01 +00002121}
2122
Rafael Espindolad9800722010-03-11 14:07:00 +00002123void CXXNameMangler::mangleTemplateArg(const NamedDecl *P,
2124 const TemplateArgument &A) {
Mike Stump1eb44332009-09-09 15:08:12 +00002125 // <template-arg> ::= <type> # type or template
Anders Carlsson7a0ba872009-05-15 16:09:15 +00002126 // ::= X <expression> E # expression
2127 // ::= <expr-primary> # simple expressions
2128 // ::= I <template-arg>* E # argument pack
2129 // ::= sp <expression> # pack expansion of (C++0x)
2130 switch (A.getKind()) {
2131 default:
2132 assert(0 && "Unknown template argument kind!");
2133 case TemplateArgument::Type:
2134 mangleType(A.getAsType());
2135 break;
Anders Carlsson9e85c742009-12-23 19:30:55 +00002136 case TemplateArgument::Template:
John McCallb6f532e2010-07-14 06:43:17 +00002137 // This is mangled as <type>.
2138 mangleType(A.getAsTemplate());
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002139 break;
Anders Carlssond553f8c2009-09-21 01:21:10 +00002140 case TemplateArgument::Expression:
2141 Out << 'X';
2142 mangleExpression(A.getAsExpr());
2143 Out << 'E';
2144 break;
Anders Carlssone170ba72009-12-14 01:45:37 +00002145 case TemplateArgument::Integral:
2146 mangleIntegerLiteral(A.getIntegralType(), *A.getAsIntegral());
Anders Carlsson7a0ba872009-05-15 16:09:15 +00002147 break;
Daniel Dunbar7e0c1952009-11-21 09:17:15 +00002148 case TemplateArgument::Declaration: {
Douglas Gregor20f0cc72010-04-23 03:10:43 +00002149 assert(P && "Missing template parameter for declaration argument");
Daniel Dunbar7e0c1952009-11-21 09:17:15 +00002150 // <expr-primary> ::= L <mangled-name> E # external name
2151
Rafael Espindolad9800722010-03-11 14:07:00 +00002152 // Clang produces AST's where pointer-to-member-function expressions
Daniel Dunbar7e0c1952009-11-21 09:17:15 +00002153 // and pointer-to-function expressions are represented as a declaration not
Rafael Espindolad9800722010-03-11 14:07:00 +00002154 // an expression. We compensate for it here to produce the correct mangling.
2155 NamedDecl *D = cast<NamedDecl>(A.getAsDecl());
2156 const NonTypeTemplateParmDecl *Parameter = cast<NonTypeTemplateParmDecl>(P);
2157 bool compensateMangling = D->isCXXClassMember() &&
2158 !Parameter->getType()->isReferenceType();
2159 if (compensateMangling) {
2160 Out << 'X';
2161 mangleOperatorName(OO_Amp, 1);
2162 }
2163
Daniel Dunbar7e0c1952009-11-21 09:17:15 +00002164 Out << 'L';
2165 // References to external entities use the mangled name; if the name would
2166 // not normally be manged then mangle it as unqualified.
2167 //
2168 // FIXME: The ABI specifies that external names here should have _Z, but
2169 // gcc leaves this off.
Rafael Espindolad9800722010-03-11 14:07:00 +00002170 if (compensateMangling)
2171 mangle(D, "_Z");
2172 else
2173 mangle(D, "Z");
Daniel Dunbar7e0c1952009-11-21 09:17:15 +00002174 Out << 'E';
Rafael Espindolad9800722010-03-11 14:07:00 +00002175
2176 if (compensateMangling)
2177 Out << 'E';
2178
Daniel Dunbar7e0c1952009-11-21 09:17:15 +00002179 break;
2180 }
2181 }
Anders Carlsson7a0ba872009-05-15 16:09:15 +00002182}
2183
Anders Carlsson0ccdf8d2009-09-27 00:38:53 +00002184void CXXNameMangler::mangleTemplateParameter(unsigned Index) {
2185 // <template-param> ::= T_ # first template parameter
2186 // ::= T <parameter-2 non-negative number> _
2187 if (Index == 0)
2188 Out << "T_";
2189 else
2190 Out << 'T' << (Index - 1) << '_';
2191}
2192
Anders Carlsson76967372009-09-17 00:43:46 +00002193// <substitution> ::= S <seq-id> _
2194// ::= S_
Anders Carlsson6862fc72009-09-17 04:16:28 +00002195bool CXXNameMangler::mangleSubstitution(const NamedDecl *ND) {
Anders Carlssone7c8cb62009-09-26 20:53:44 +00002196 // Try one of the standard substitutions first.
2197 if (mangleStandardSubstitution(ND))
2198 return true;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002199
Anders Carlsson433d1372009-11-07 04:26:04 +00002200 ND = cast<NamedDecl>(ND->getCanonicalDecl());
Anders Carlsson6862fc72009-09-17 04:16:28 +00002201 return mangleSubstitution(reinterpret_cast<uintptr_t>(ND));
2202}
2203
Anders Carlsson76967372009-09-17 00:43:46 +00002204bool CXXNameMangler::mangleSubstitution(QualType T) {
Anders Carlssond99edc42009-09-26 03:55:37 +00002205 if (!T.getCVRQualifiers()) {
2206 if (const RecordType *RT = T->getAs<RecordType>())
2207 return mangleSubstitution(RT->getDecl());
2208 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002209
Anders Carlsson76967372009-09-17 00:43:46 +00002210 uintptr_t TypePtr = reinterpret_cast<uintptr_t>(T.getAsOpaquePtr());
2211
Anders Carlssond3a932a2009-09-17 03:53:28 +00002212 return mangleSubstitution(TypePtr);
2213}
2214
Douglas Gregor1e9268e2010-04-28 05:58:56 +00002215bool CXXNameMangler::mangleSubstitution(TemplateName Template) {
2216 if (TemplateDecl *TD = Template.getAsTemplateDecl())
2217 return mangleSubstitution(TD);
Sean Huntc3021132010-05-05 15:23:54 +00002218
Douglas Gregor1e9268e2010-04-28 05:58:56 +00002219 Template = Context.getASTContext().getCanonicalTemplateName(Template);
2220 return mangleSubstitution(
2221 reinterpret_cast<uintptr_t>(Template.getAsVoidPointer()));
2222}
2223
Anders Carlssond3a932a2009-09-17 03:53:28 +00002224bool CXXNameMangler::mangleSubstitution(uintptr_t Ptr) {
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002225 llvm::DenseMap<uintptr_t, unsigned>::iterator I = Substitutions.find(Ptr);
Anders Carlsson76967372009-09-17 00:43:46 +00002226 if (I == Substitutions.end())
2227 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002228
Anders Carlsson76967372009-09-17 00:43:46 +00002229 unsigned SeqID = I->second;
2230 if (SeqID == 0)
2231 Out << "S_";
2232 else {
2233 SeqID--;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002234
Anders Carlsson76967372009-09-17 00:43:46 +00002235 // <seq-id> is encoded in base-36, using digits and upper case letters.
2236 char Buffer[10];
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002237 char *BufferPtr = llvm::array_endof(Buffer);
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002238
Anders Carlsson76967372009-09-17 00:43:46 +00002239 if (SeqID == 0) *--BufferPtr = '0';
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002240
Anders Carlsson76967372009-09-17 00:43:46 +00002241 while (SeqID) {
2242 assert(BufferPtr > Buffer && "Buffer overflow!");
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002243
John McCall6ab30e02010-06-09 07:26:17 +00002244 char c = static_cast<char>(SeqID % 36);
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002245
Anders Carlsson76967372009-09-17 00:43:46 +00002246 *--BufferPtr = (c < 10 ? '0' + c : 'A' + c - 10);
2247 SeqID /= 36;
2248 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002249
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002250 Out << 'S'
2251 << llvm::StringRef(BufferPtr, llvm::array_endof(Buffer)-BufferPtr)
2252 << '_';
Anders Carlsson76967372009-09-17 00:43:46 +00002253 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002254
Anders Carlsson76967372009-09-17 00:43:46 +00002255 return true;
2256}
2257
Anders Carlssonf514b542009-09-27 00:12:57 +00002258static bool isCharType(QualType T) {
2259 if (T.isNull())
2260 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002261
Anders Carlssonf514b542009-09-27 00:12:57 +00002262 return T->isSpecificBuiltinType(BuiltinType::Char_S) ||
2263 T->isSpecificBuiltinType(BuiltinType::Char_U);
2264}
2265
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002266/// isCharSpecialization - Returns whether a given type is a template
Anders Carlssonf514b542009-09-27 00:12:57 +00002267/// specialization of a given name with a single argument of type char.
2268static bool isCharSpecialization(QualType T, const char *Name) {
2269 if (T.isNull())
2270 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002271
Anders Carlssonf514b542009-09-27 00:12:57 +00002272 const RecordType *RT = T->getAs<RecordType>();
2273 if (!RT)
2274 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002275
2276 const ClassTemplateSpecializationDecl *SD =
Anders Carlssonf514b542009-09-27 00:12:57 +00002277 dyn_cast<ClassTemplateSpecializationDecl>(RT->getDecl());
2278 if (!SD)
2279 return false;
2280
2281 if (!isStdNamespace(SD->getDeclContext()))
2282 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002283
Anders Carlssonf514b542009-09-27 00:12:57 +00002284 const TemplateArgumentList &TemplateArgs = SD->getTemplateArgs();
2285 if (TemplateArgs.size() != 1)
2286 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002287
Anders Carlssonf514b542009-09-27 00:12:57 +00002288 if (!isCharType(TemplateArgs[0].getAsType()))
2289 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002290
Daniel Dunbar01eb9b92009-10-18 21:17:35 +00002291 return SD->getIdentifier()->getName() == Name;
Anders Carlssonf514b542009-09-27 00:12:57 +00002292}
2293
Anders Carlsson91f88602009-12-07 19:56:42 +00002294template <std::size_t StrLen>
2295bool isStreamCharSpecialization(const ClassTemplateSpecializationDecl *SD,
2296 const char (&Str)[StrLen]) {
2297 if (!SD->getIdentifier()->isStr(Str))
2298 return false;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002299
Anders Carlsson91f88602009-12-07 19:56:42 +00002300 const TemplateArgumentList &TemplateArgs = SD->getTemplateArgs();
2301 if (TemplateArgs.size() != 2)
2302 return false;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002303
Anders Carlsson91f88602009-12-07 19:56:42 +00002304 if (!isCharType(TemplateArgs[0].getAsType()))
2305 return false;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002306
Anders Carlsson91f88602009-12-07 19:56:42 +00002307 if (!isCharSpecialization(TemplateArgs[1].getAsType(), "char_traits"))
2308 return false;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002309
Anders Carlsson91f88602009-12-07 19:56:42 +00002310 return true;
2311}
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002312
Anders Carlssone7c8cb62009-09-26 20:53:44 +00002313bool CXXNameMangler::mangleStandardSubstitution(const NamedDecl *ND) {
2314 // <substitution> ::= St # ::std::
Anders Carlsson8c031552009-09-26 23:10:05 +00002315 if (const NamespaceDecl *NS = dyn_cast<NamespaceDecl>(ND)) {
Anders Carlsson47846d22009-12-04 06:23:23 +00002316 if (isStd(NS)) {
Anders Carlsson8c031552009-09-26 23:10:05 +00002317 Out << "St";
2318 return true;
2319 }
2320 }
2321
2322 if (const ClassTemplateDecl *TD = dyn_cast<ClassTemplateDecl>(ND)) {
2323 if (!isStdNamespace(TD->getDeclContext()))
2324 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002325
Anders Carlsson8c031552009-09-26 23:10:05 +00002326 // <substitution> ::= Sa # ::std::allocator
2327 if (TD->getIdentifier()->isStr("allocator")) {
2328 Out << "Sa";
2329 return true;
2330 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002331
Anders Carlsson189d59c2009-09-26 23:14:39 +00002332 // <<substitution> ::= Sb # ::std::basic_string
2333 if (TD->getIdentifier()->isStr("basic_string")) {
2334 Out << "Sb";
2335 return true;
2336 }
Anders Carlsson8c031552009-09-26 23:10:05 +00002337 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002338
2339 if (const ClassTemplateSpecializationDecl *SD =
Anders Carlssonf514b542009-09-27 00:12:57 +00002340 dyn_cast<ClassTemplateSpecializationDecl>(ND)) {
Eli Friedman5370ee22010-02-23 18:25:09 +00002341 if (!isStdNamespace(SD->getDeclContext()))
2342 return false;
2343
Anders Carlssonf514b542009-09-27 00:12:57 +00002344 // <substitution> ::= Ss # ::std::basic_string<char,
2345 // ::std::char_traits<char>,
2346 // ::std::allocator<char> >
2347 if (SD->getIdentifier()->isStr("basic_string")) {
2348 const TemplateArgumentList &TemplateArgs = SD->getTemplateArgs();
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002349
Anders Carlssonf514b542009-09-27 00:12:57 +00002350 if (TemplateArgs.size() != 3)
2351 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002352
Anders Carlssonf514b542009-09-27 00:12:57 +00002353 if (!isCharType(TemplateArgs[0].getAsType()))
2354 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002355
Anders Carlssonf514b542009-09-27 00:12:57 +00002356 if (!isCharSpecialization(TemplateArgs[1].getAsType(), "char_traits"))
2357 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002358
Anders Carlssonf514b542009-09-27 00:12:57 +00002359 if (!isCharSpecialization(TemplateArgs[2].getAsType(), "allocator"))
2360 return false;
2361
2362 Out << "Ss";
2363 return true;
2364 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002365
Anders Carlsson91f88602009-12-07 19:56:42 +00002366 // <substitution> ::= Si # ::std::basic_istream<char,
2367 // ::std::char_traits<char> >
2368 if (isStreamCharSpecialization(SD, "basic_istream")) {
2369 Out << "Si";
2370 return true;
2371 }
2372
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002373 // <substitution> ::= So # ::std::basic_ostream<char,
Anders Carlsson8f8fd8e2009-10-08 17:20:26 +00002374 // ::std::char_traits<char> >
Anders Carlsson91f88602009-12-07 19:56:42 +00002375 if (isStreamCharSpecialization(SD, "basic_ostream")) {
Anders Carlsson8f8fd8e2009-10-08 17:20:26 +00002376 Out << "So";
2377 return true;
2378 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002379
Anders Carlsson91f88602009-12-07 19:56:42 +00002380 // <substitution> ::= Sd # ::std::basic_iostream<char,
2381 // ::std::char_traits<char> >
2382 if (isStreamCharSpecialization(SD, "basic_iostream")) {
2383 Out << "Sd";
2384 return true;
2385 }
Anders Carlssonf514b542009-09-27 00:12:57 +00002386 }
Anders Carlsson8c031552009-09-26 23:10:05 +00002387 return false;
Anders Carlssone7c8cb62009-09-26 20:53:44 +00002388}
2389
Anders Carlsson76967372009-09-17 00:43:46 +00002390void CXXNameMangler::addSubstitution(QualType T) {
Anders Carlssond99edc42009-09-26 03:55:37 +00002391 if (!T.getCVRQualifiers()) {
2392 if (const RecordType *RT = T->getAs<RecordType>()) {
2393 addSubstitution(RT->getDecl());
2394 return;
2395 }
2396 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002397
Anders Carlsson76967372009-09-17 00:43:46 +00002398 uintptr_t TypePtr = reinterpret_cast<uintptr_t>(T.getAsOpaquePtr());
Anders Carlssond3a932a2009-09-17 03:53:28 +00002399 addSubstitution(TypePtr);
2400}
2401
Douglas Gregor1e9268e2010-04-28 05:58:56 +00002402void CXXNameMangler::addSubstitution(TemplateName Template) {
2403 if (TemplateDecl *TD = Template.getAsTemplateDecl())
2404 return addSubstitution(TD);
Sean Huntc3021132010-05-05 15:23:54 +00002405
Douglas Gregor1e9268e2010-04-28 05:58:56 +00002406 Template = Context.getASTContext().getCanonicalTemplateName(Template);
2407 addSubstitution(reinterpret_cast<uintptr_t>(Template.getAsVoidPointer()));
2408}
2409
Anders Carlssond3a932a2009-09-17 03:53:28 +00002410void CXXNameMangler::addSubstitution(uintptr_t Ptr) {
Anders Carlssond3a932a2009-09-17 03:53:28 +00002411 assert(!Substitutions.count(Ptr) && "Substitution already exists!");
Anders Carlsson9d85b722010-06-02 04:29:50 +00002412 Substitutions[Ptr] = SeqID++;
Anders Carlsson76967372009-09-17 00:43:46 +00002413}
2414
Daniel Dunbar1b077112009-11-21 09:06:10 +00002415//
Mike Stump1eb44332009-09-09 15:08:12 +00002416
Daniel Dunbar1b077112009-11-21 09:06:10 +00002417/// \brief Mangles the name of the declaration D and emits that name to the
2418/// given output stream.
2419///
2420/// If the declaration D requires a mangled name, this routine will emit that
2421/// mangled name to \p os and return true. Otherwise, \p os will be unchanged
2422/// and this routine will return false. In this case, the caller should just
2423/// emit the identifier of the declaration (\c D->getIdentifier()) as its
2424/// name.
Daniel Dunbarf981bf82009-11-21 09:14:52 +00002425void MangleContext::mangleName(const NamedDecl *D,
Daniel Dunbar94fd26d2009-11-21 09:06:22 +00002426 llvm::SmallVectorImpl<char> &Res) {
Daniel Dunbarc02ab4c2009-11-21 09:14:44 +00002427 assert((isa<FunctionDecl>(D) || isa<VarDecl>(D)) &&
2428 "Invalid mangleName() call, argument is not a variable or function!");
2429 assert(!isa<CXXConstructorDecl>(D) && !isa<CXXDestructorDecl>(D) &&
2430 "Invalid mangleName() call on 'structor decl!");
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002431
Daniel Dunbar1b077112009-11-21 09:06:10 +00002432 PrettyStackTraceDecl CrashInfo(D, SourceLocation(),
2433 getASTContext().getSourceManager(),
2434 "Mangling declaration");
Mike Stump1eb44332009-09-09 15:08:12 +00002435
Daniel Dunbar94fd26d2009-11-21 09:06:22 +00002436 CXXNameMangler Mangler(*this, Res);
2437 return Mangler.mangle(D);
Daniel Dunbar1b077112009-11-21 09:06:10 +00002438}
Mike Stump1eb44332009-09-09 15:08:12 +00002439
Daniel Dunbar1b077112009-11-21 09:06:10 +00002440void MangleContext::mangleCXXCtor(const CXXConstructorDecl *D, CXXCtorType Type,
Daniel Dunbar94fd26d2009-11-21 09:06:22 +00002441 llvm::SmallVectorImpl<char> &Res) {
Daniel Dunbar77939c92009-11-21 09:06:31 +00002442 CXXNameMangler Mangler(*this, Res, D, Type);
2443 Mangler.mangle(D);
Daniel Dunbar1b077112009-11-21 09:06:10 +00002444}
Mike Stump1eb44332009-09-09 15:08:12 +00002445
Daniel Dunbar1b077112009-11-21 09:06:10 +00002446void MangleContext::mangleCXXDtor(const CXXDestructorDecl *D, CXXDtorType Type,
Daniel Dunbar94fd26d2009-11-21 09:06:22 +00002447 llvm::SmallVectorImpl<char> &Res) {
Daniel Dunbar77939c92009-11-21 09:06:31 +00002448 CXXNameMangler Mangler(*this, Res, D, Type);
2449 Mangler.mangle(D);
Daniel Dunbar1b077112009-11-21 09:06:10 +00002450}
Mike Stumpf1216772009-07-31 18:25:34 +00002451
Fariborz Jahanian564360b2010-06-24 00:08:06 +00002452void MangleContext::mangleBlock(GlobalDecl GD, const BlockDecl *BD,
Douglas Gregor35415f52010-05-25 17:04:15 +00002453 llvm::SmallVectorImpl<char> &Res) {
Charles Davis685b1d92010-05-26 18:25:27 +00002454 MiscNameMangler Mangler(*this, Res);
Fariborz Jahanian564360b2010-06-24 00:08:06 +00002455 Mangler.mangleBlock(GD, BD);
Douglas Gregor35415f52010-05-25 17:04:15 +00002456}
2457
Anders Carlsson19879c92010-03-23 17:17:29 +00002458void MangleContext::mangleThunk(const CXXMethodDecl *MD,
2459 const ThunkInfo &Thunk,
2460 llvm::SmallVectorImpl<char> &Res) {
2461 // <special-name> ::= T <call-offset> <base encoding>
2462 // # base is the nominal target function of thunk
2463 // <special-name> ::= Tc <call-offset> <call-offset> <base encoding>
2464 // # base is the nominal target function of thunk
2465 // # first call-offset is 'this' adjustment
2466 // # second call-offset is result adjustment
Sean Huntc3021132010-05-05 15:23:54 +00002467
Anders Carlsson19879c92010-03-23 17:17:29 +00002468 assert(!isa<CXXDestructorDecl>(MD) &&
2469 "Use mangleCXXDtor for destructor decls!");
Sean Huntc3021132010-05-05 15:23:54 +00002470
Anders Carlsson19879c92010-03-23 17:17:29 +00002471 CXXNameMangler Mangler(*this, Res);
2472 Mangler.getStream() << "_ZT";
2473 if (!Thunk.Return.isEmpty())
2474 Mangler.getStream() << 'c';
Sean Huntc3021132010-05-05 15:23:54 +00002475
Anders Carlsson19879c92010-03-23 17:17:29 +00002476 // Mangle the 'this' pointer adjustment.
2477 Mangler.mangleCallOffset(Thunk.This.NonVirtual, Thunk.This.VCallOffsetOffset);
Sean Huntc3021132010-05-05 15:23:54 +00002478
Anders Carlsson19879c92010-03-23 17:17:29 +00002479 // Mangle the return pointer adjustment if there is one.
2480 if (!Thunk.Return.isEmpty())
2481 Mangler.mangleCallOffset(Thunk.Return.NonVirtual,
2482 Thunk.Return.VBaseOffsetOffset);
Sean Huntc3021132010-05-05 15:23:54 +00002483
Anders Carlsson19879c92010-03-23 17:17:29 +00002484 Mangler.mangleFunctionEncoding(MD);
2485}
2486
Sean Huntc3021132010-05-05 15:23:54 +00002487void
Anders Carlsson19879c92010-03-23 17:17:29 +00002488MangleContext::mangleCXXDtorThunk(const CXXDestructorDecl *DD, CXXDtorType Type,
2489 const ThisAdjustment &ThisAdjustment,
2490 llvm::SmallVectorImpl<char> &Res) {
2491 // <special-name> ::= T <call-offset> <base encoding>
2492 // # base is the nominal target function of thunk
Sean Huntc3021132010-05-05 15:23:54 +00002493
Anders Carlsson19879c92010-03-23 17:17:29 +00002494 CXXNameMangler Mangler(*this, Res, DD, Type);
2495 Mangler.getStream() << "_ZT";
2496
2497 // Mangle the 'this' pointer adjustment.
Sean Huntc3021132010-05-05 15:23:54 +00002498 Mangler.mangleCallOffset(ThisAdjustment.NonVirtual,
Anders Carlsson19879c92010-03-23 17:17:29 +00002499 ThisAdjustment.VCallOffsetOffset);
2500
2501 Mangler.mangleFunctionEncoding(DD);
2502}
2503
Daniel Dunbarc0747712009-11-21 09:12:13 +00002504/// mangleGuardVariable - Returns the mangled name for a guard variable
2505/// for the passed in VarDecl.
John McCall5cd91b52010-09-08 01:44:27 +00002506void MangleContext::mangleItaniumGuardVariable(const VarDecl *D,
2507 llvm::SmallVectorImpl<char> &Res) {
Daniel Dunbarc0747712009-11-21 09:12:13 +00002508 // <special-name> ::= GV <object name> # Guard variable for one-time
2509 // # initialization
2510 CXXNameMangler Mangler(*this, Res);
2511 Mangler.getStream() << "_ZGV";
2512 Mangler.mangleName(D);
2513}
2514
Anders Carlsson715edf22010-06-26 16:09:40 +00002515void MangleContext::mangleReferenceTemporary(const VarDecl *D,
2516 llvm::SmallVectorImpl<char> &Res) {
2517 // We match the GCC mangling here.
2518 // <special-name> ::= GR <object name>
2519 CXXNameMangler Mangler(*this, Res);
2520 Mangler.getStream() << "_ZGR";
2521 Mangler.mangleName(D);
2522}
2523
Anders Carlsson046c2942010-04-17 20:15:18 +00002524void MangleContext::mangleCXXVTable(const CXXRecordDecl *RD,
Daniel Dunbar94fd26d2009-11-21 09:06:22 +00002525 llvm::SmallVectorImpl<char> &Res) {
Daniel Dunbarc0747712009-11-21 09:12:13 +00002526 // <special-name> ::= TV <type> # virtual table
Daniel Dunbar94fd26d2009-11-21 09:06:22 +00002527 CXXNameMangler Mangler(*this, Res);
Daniel Dunbarc0747712009-11-21 09:12:13 +00002528 Mangler.getStream() << "_ZTV";
Douglas Gregor1b12a3b2010-05-26 05:11:13 +00002529 Mangler.mangleNameOrStandardSubstitution(RD);
Daniel Dunbar1b077112009-11-21 09:06:10 +00002530}
Mike Stump82d75b02009-11-10 01:58:37 +00002531
Daniel Dunbar1b077112009-11-21 09:06:10 +00002532void MangleContext::mangleCXXVTT(const CXXRecordDecl *RD,
Daniel Dunbar94fd26d2009-11-21 09:06:22 +00002533 llvm::SmallVectorImpl<char> &Res) {
Daniel Dunbarc0747712009-11-21 09:12:13 +00002534 // <special-name> ::= TT <type> # VTT structure
Daniel Dunbar94fd26d2009-11-21 09:06:22 +00002535 CXXNameMangler Mangler(*this, Res);
Daniel Dunbarc0747712009-11-21 09:12:13 +00002536 Mangler.getStream() << "_ZTT";
Douglas Gregor1b12a3b2010-05-26 05:11:13 +00002537 Mangler.mangleNameOrStandardSubstitution(RD);
Daniel Dunbar1b077112009-11-21 09:06:10 +00002538}
Mike Stumpab3f7e92009-11-10 01:41:59 +00002539
Anders Carlsson046c2942010-04-17 20:15:18 +00002540void MangleContext::mangleCXXCtorVTable(const CXXRecordDecl *RD, int64_t Offset,
Daniel Dunbar1b077112009-11-21 09:06:10 +00002541 const CXXRecordDecl *Type,
Daniel Dunbar94fd26d2009-11-21 09:06:22 +00002542 llvm::SmallVectorImpl<char> &Res) {
Daniel Dunbarc0747712009-11-21 09:12:13 +00002543 // <special-name> ::= TC <type> <offset number> _ <base type>
Daniel Dunbar94fd26d2009-11-21 09:06:22 +00002544 CXXNameMangler Mangler(*this, Res);
Daniel Dunbarc0747712009-11-21 09:12:13 +00002545 Mangler.getStream() << "_ZTC";
Douglas Gregor1b12a3b2010-05-26 05:11:13 +00002546 Mangler.mangleNameOrStandardSubstitution(RD);
Daniel Dunbarc0747712009-11-21 09:12:13 +00002547 Mangler.getStream() << Offset;
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002548 Mangler.getStream() << '_';
Douglas Gregor1b12a3b2010-05-26 05:11:13 +00002549 Mangler.mangleNameOrStandardSubstitution(Type);
Daniel Dunbar1b077112009-11-21 09:06:10 +00002550}
Mike Stump738f8c22009-07-31 23:15:31 +00002551
Mike Stumpde050572009-12-02 18:57:08 +00002552void MangleContext::mangleCXXRTTI(QualType Ty,
Daniel Dunbar94fd26d2009-11-21 09:06:22 +00002553 llvm::SmallVectorImpl<char> &Res) {
Daniel Dunbarc0747712009-11-21 09:12:13 +00002554 // <special-name> ::= TI <type> # typeinfo structure
Douglas Gregor154fe982009-12-23 22:04:40 +00002555 assert(!Ty.hasQualifiers() && "RTTI info cannot have top-level qualifiers");
Daniel Dunbar94fd26d2009-11-21 09:06:22 +00002556 CXXNameMangler Mangler(*this, Res);
Daniel Dunbarc0747712009-11-21 09:12:13 +00002557 Mangler.getStream() << "_ZTI";
2558 Mangler.mangleType(Ty);
Daniel Dunbar1b077112009-11-21 09:06:10 +00002559}
Mike Stump67795982009-11-14 00:14:13 +00002560
Mike Stumpde050572009-12-02 18:57:08 +00002561void MangleContext::mangleCXXRTTIName(QualType Ty,
Daniel Dunbar94fd26d2009-11-21 09:06:22 +00002562 llvm::SmallVectorImpl<char> &Res) {
Daniel Dunbarc0747712009-11-21 09:12:13 +00002563 // <special-name> ::= TS <type> # typeinfo name (null terminated byte string)
Daniel Dunbar94fd26d2009-11-21 09:06:22 +00002564 CXXNameMangler Mangler(*this, Res);
Daniel Dunbarc0747712009-11-21 09:12:13 +00002565 Mangler.getStream() << "_ZTS";
2566 Mangler.mangleType(Ty);
Mike Stumpf1216772009-07-31 18:25:34 +00002567}