blob: 30fd668b0c28200561f104cebff0342ca0f025c5 [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
102static const DeclContext *GetLocalClassFunctionDeclContext(
103 const DeclContext *DC) {
104 if (isa<CXXRecordDecl>(DC)) {
105 while (!DC->isNamespace() && !DC->isTranslationUnit() &&
106 !isa<FunctionDecl>(DC))
107 DC = DC->getParent();
108 if (isa<FunctionDecl>(DC))
109 return DC;
110 }
111 return 0;
112}
113
Anders Carlsson7e120032009-11-24 05:36:32 +0000114static const CXXMethodDecl *getStructor(const CXXMethodDecl *MD) {
115 assert((isa<CXXConstructorDecl>(MD) || isa<CXXDestructorDecl>(MD)) &&
116 "Passed in decl is not a ctor or dtor!");
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000117
Anders Carlsson7e120032009-11-24 05:36:32 +0000118 if (const TemplateDecl *TD = MD->getPrimaryTemplate()) {
119 MD = cast<CXXMethodDecl>(TD->getTemplatedDecl());
120
121 assert((isa<CXXConstructorDecl>(MD) || isa<CXXDestructorDecl>(MD)) &&
122 "Templated decl is not a ctor or dtor!");
123 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000124
Anders Carlsson7e120032009-11-24 05:36:32 +0000125 return MD;
126}
John McCall1dd73832010-02-04 01:42:13 +0000127
128static const unsigned UnknownArity = ~0U;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000129
Daniel Dunbar1b077112009-11-21 09:06:10 +0000130/// CXXNameMangler - Manage the mangling of a single name.
Daniel Dunbarc0747712009-11-21 09:12:13 +0000131class CXXNameMangler {
Daniel Dunbar1b077112009-11-21 09:06:10 +0000132 MangleContext &Context;
Daniel Dunbar94fd26d2009-11-21 09:06:22 +0000133 llvm::raw_svector_ostream Out;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000134
Daniel Dunbar1b077112009-11-21 09:06:10 +0000135 const CXXMethodDecl *Structor;
136 unsigned StructorType;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000137
Anders Carlsson9d85b722010-06-02 04:29:50 +0000138 /// SeqID - The next subsitution sequence number.
139 unsigned SeqID;
140
Daniel Dunbar1b077112009-11-21 09:06:10 +0000141 llvm::DenseMap<uintptr_t, unsigned> Substitutions;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000142
John McCall1dd73832010-02-04 01:42:13 +0000143 ASTContext &getASTContext() const { return Context.getASTContext(); }
144
Daniel Dunbarc0747712009-11-21 09:12:13 +0000145public:
Daniel Dunbar94fd26d2009-11-21 09:06:22 +0000146 CXXNameMangler(MangleContext &C, llvm::SmallVectorImpl<char> &Res)
Anders Carlsson9d85b722010-06-02 04:29:50 +0000147 : Context(C), Out(Res), Structor(0), StructorType(0), SeqID(0) { }
Daniel Dunbar77939c92009-11-21 09:06:31 +0000148 CXXNameMangler(MangleContext &C, llvm::SmallVectorImpl<char> &Res,
149 const CXXConstructorDecl *D, CXXCtorType Type)
Anders Carlsson9d85b722010-06-02 04:29:50 +0000150 : Context(C), Out(Res), Structor(getStructor(D)), StructorType(Type),
151 SeqID(0) { }
Daniel Dunbar77939c92009-11-21 09:06:31 +0000152 CXXNameMangler(MangleContext &C, llvm::SmallVectorImpl<char> &Res,
153 const CXXDestructorDecl *D, CXXDtorType Type)
Anders Carlsson9d85b722010-06-02 04:29:50 +0000154 : Context(C), Out(Res), Structor(getStructor(D)), StructorType(Type),
155 SeqID(0) { }
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000156
Anders Carlssonf98574b2010-02-05 07:31:37 +0000157#if MANGLE_CHECKER
158 ~CXXNameMangler() {
159 if (Out.str()[0] == '\01')
160 return;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000161
Anders Carlssonf98574b2010-02-05 07:31:37 +0000162 int status = 0;
163 char *result = abi::__cxa_demangle(Out.str().str().c_str(), 0, 0, &status);
164 assert(status == 0 && "Could not demangle mangled name!");
165 free(result);
166 }
167#endif
Daniel Dunbarc0747712009-11-21 09:12:13 +0000168 llvm::raw_svector_ostream &getStream() { return Out; }
169
Daniel Dunbar7e0c1952009-11-21 09:17:15 +0000170 void mangle(const NamedDecl *D, llvm::StringRef Prefix = "_Z");
Anders Carlsson19879c92010-03-23 17:17:29 +0000171 void mangleCallOffset(int64_t NonVirtual, int64_t Virtual);
John McCall0512e482010-07-14 04:20:34 +0000172 void mangleNumber(const llvm::APSInt &I);
Anders Carlssona94822e2009-11-26 02:32:05 +0000173 void mangleNumber(int64_t Number);
John McCall0512e482010-07-14 04:20:34 +0000174 void mangleFloat(const llvm::APFloat &F);
Daniel Dunbarc0747712009-11-21 09:12:13 +0000175 void mangleFunctionEncoding(const FunctionDecl *FD);
176 void mangleName(const NamedDecl *ND);
177 void mangleType(QualType T);
Douglas Gregor1b12a3b2010-05-26 05:11:13 +0000178 void mangleNameOrStandardSubstitution(const NamedDecl *ND);
179
Daniel Dunbarc0747712009-11-21 09:12:13 +0000180private:
Daniel Dunbar1b077112009-11-21 09:06:10 +0000181 bool mangleSubstitution(const NamedDecl *ND);
182 bool mangleSubstitution(QualType T);
Douglas Gregor1e9268e2010-04-28 05:58:56 +0000183 bool mangleSubstitution(TemplateName Template);
Daniel Dunbar1b077112009-11-21 09:06:10 +0000184 bool mangleSubstitution(uintptr_t Ptr);
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000185
Daniel Dunbar1b077112009-11-21 09:06:10 +0000186 bool mangleStandardSubstitution(const NamedDecl *ND);
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000187
Daniel Dunbar1b077112009-11-21 09:06:10 +0000188 void addSubstitution(const NamedDecl *ND) {
189 ND = cast<NamedDecl>(ND->getCanonicalDecl());
Anders Carlsson433d1372009-11-07 04:26:04 +0000190
Daniel Dunbar1b077112009-11-21 09:06:10 +0000191 addSubstitution(reinterpret_cast<uintptr_t>(ND));
192 }
193 void addSubstitution(QualType T);
Douglas Gregor1e9268e2010-04-28 05:58:56 +0000194 void addSubstitution(TemplateName Template);
Daniel Dunbar1b077112009-11-21 09:06:10 +0000195 void addSubstitution(uintptr_t Ptr);
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000196
John McCall1dd73832010-02-04 01:42:13 +0000197 void mangleUnresolvedScope(NestedNameSpecifier *Qualifier);
198 void mangleUnresolvedName(NestedNameSpecifier *Qualifier,
199 DeclarationName Name,
200 unsigned KnownArity = UnknownArity);
201
Daniel Dunbar1b077112009-11-21 09:06:10 +0000202 void mangleName(const TemplateDecl *TD,
203 const TemplateArgument *TemplateArgs,
204 unsigned NumTemplateArgs);
John McCall1dd73832010-02-04 01:42:13 +0000205 void mangleUnqualifiedName(const NamedDecl *ND) {
206 mangleUnqualifiedName(ND, ND->getDeclName(), UnknownArity);
207 }
208 void mangleUnqualifiedName(const NamedDecl *ND, DeclarationName Name,
209 unsigned KnownArity);
Daniel Dunbar1b077112009-11-21 09:06:10 +0000210 void mangleUnscopedName(const NamedDecl *ND);
211 void mangleUnscopedTemplateName(const TemplateDecl *ND);
Douglas Gregor1e9268e2010-04-28 05:58:56 +0000212 void mangleUnscopedTemplateName(TemplateName);
Daniel Dunbar1b077112009-11-21 09:06:10 +0000213 void mangleSourceName(const IdentifierInfo *II);
214 void mangleLocalName(const NamedDecl *ND);
Fariborz Jahanian57058532010-03-03 19:41:08 +0000215 void mangleNestedName(const NamedDecl *ND, const DeclContext *DC,
216 bool NoFunction=false);
Daniel Dunbar1b077112009-11-21 09:06:10 +0000217 void mangleNestedName(const TemplateDecl *TD,
218 const TemplateArgument *TemplateArgs,
219 unsigned NumTemplateArgs);
Fariborz Jahanian57058532010-03-03 19:41:08 +0000220 void manglePrefix(const DeclContext *DC, bool NoFunction=false);
Daniel Dunbar1b077112009-11-21 09:06:10 +0000221 void mangleTemplatePrefix(const TemplateDecl *ND);
Douglas Gregor20f0cc72010-04-23 03:10:43 +0000222 void mangleTemplatePrefix(TemplateName Template);
Daniel Dunbar1b077112009-11-21 09:06:10 +0000223 void mangleOperatorName(OverloadedOperatorKind OO, unsigned Arity);
224 void mangleQualifiers(Qualifiers Quals);
John McCallefe6aee2009-09-05 07:56:18 +0000225
Anders Carlsson7b06f6c2009-12-10 03:14:39 +0000226 void mangleObjCMethodName(const ObjCMethodDecl *MD);
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000227
Daniel Dunbar1b077112009-11-21 09:06:10 +0000228 // Declare manglers for every type class.
John McCallefe6aee2009-09-05 07:56:18 +0000229#define ABSTRACT_TYPE(CLASS, PARENT)
230#define NON_CANONICAL_TYPE(CLASS, PARENT)
231#define TYPE(CLASS, PARENT) void mangleType(const CLASS##Type *T);
232#include "clang/AST/TypeNodes.def"
233
Daniel Dunbar1b077112009-11-21 09:06:10 +0000234 void mangleType(const TagType*);
John McCallb6f532e2010-07-14 06:43:17 +0000235 void mangleType(TemplateName);
Daniel Dunbar1b077112009-11-21 09:06:10 +0000236 void mangleBareFunctionType(const FunctionType *T,
237 bool MangleReturnType);
Anders Carlssone170ba72009-12-14 01:45:37 +0000238
239 void mangleIntegerLiteral(QualType T, const llvm::APSInt &Value);
John McCall2f27bf82010-02-04 02:56:29 +0000240 void mangleMemberExpr(const Expr *Base, bool IsArrow,
241 NestedNameSpecifier *Qualifier,
242 DeclarationName Name,
243 unsigned KnownArity);
John McCall5e1e89b2010-08-18 19:18:59 +0000244 void mangleExpression(const Expr *E, unsigned Arity = UnknownArity);
Daniel Dunbar1b077112009-11-21 09:06:10 +0000245 void mangleCXXCtorType(CXXCtorType T);
246 void mangleCXXDtorType(CXXDtorType T);
Mike Stump1eb44332009-09-09 15:08:12 +0000247
Douglas Gregor20f0cc72010-04-23 03:10:43 +0000248 void mangleTemplateArgs(TemplateName Template,
249 const TemplateArgument *TemplateArgs,
Sean Huntc3021132010-05-05 15:23:54 +0000250 unsigned NumTemplateArgs);
Rafael Espindolad9800722010-03-11 14:07:00 +0000251 void mangleTemplateArgs(const TemplateParameterList &PL,
252 const TemplateArgument *TemplateArgs,
Daniel Dunbar1b077112009-11-21 09:06:10 +0000253 unsigned NumTemplateArgs);
Rafael Espindolad9800722010-03-11 14:07:00 +0000254 void mangleTemplateArgs(const TemplateParameterList &PL,
255 const TemplateArgumentList &AL);
256 void mangleTemplateArg(const NamedDecl *P, const TemplateArgument &A);
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000257
Daniel Dunbar1b077112009-11-21 09:06:10 +0000258 void mangleTemplateParameter(unsigned Index);
259};
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000260}
261
Anders Carlsson43f17402009-04-02 15:51:53 +0000262static bool isInCLinkageSpecification(const Decl *D) {
Douglas Gregor457e2812009-10-28 16:31:34 +0000263 D = D->getCanonicalDecl();
Mike Stump1eb44332009-09-09 15:08:12 +0000264 for (const DeclContext *DC = D->getDeclContext();
Anders Carlsson43f17402009-04-02 15:51:53 +0000265 !DC->isTranslationUnit(); DC = DC->getParent()) {
Mike Stump1eb44332009-09-09 15:08:12 +0000266 if (const LinkageSpecDecl *Linkage = dyn_cast<LinkageSpecDecl>(DC))
Anders Carlsson43f17402009-04-02 15:51:53 +0000267 return Linkage->getLanguage() == LinkageSpecDecl::lang_c;
268 }
Mike Stump1eb44332009-09-09 15:08:12 +0000269
Anders Carlsson43f17402009-04-02 15:51:53 +0000270 return false;
271}
272
Daniel Dunbarf981bf82009-11-21 09:14:52 +0000273bool MangleContext::shouldMangleDeclName(const NamedDecl *D) {
274 // In C, functions with no attributes never need to be mangled. Fastpath them.
275 if (!getASTContext().getLangOptions().CPlusPlus && !D->hasAttrs())
276 return false;
277
278 // Any decl can be declared with __asm("foo") on it, and this takes precedence
279 // over all other naming in the .o file.
280 if (D->hasAttr<AsmLabelAttr>())
281 return true;
282
Mike Stump141c5af2009-09-02 00:25:38 +0000283 // Clang's "overloadable" attribute extension to C/C++ implies name mangling
Anders Carlssona1e16222009-11-07 07:15:03 +0000284 // (always) as does passing a C++ member function and a function
285 // whose name is not a simple identifier.
Daniel Dunbarf981bf82009-11-21 09:14:52 +0000286 const FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
287 if (FD && (FD->hasAttr<OverloadableAttr>() || isa<CXXMethodDecl>(FD) ||
288 !FD->getDeclName().isIdentifier()))
289 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000290
Daniel Dunbarf981bf82009-11-21 09:14:52 +0000291 // Otherwise, no mangling is done outside C++ mode.
292 if (!getASTContext().getLangOptions().CPlusPlus)
293 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000294
Sean Hunt31455252010-01-24 03:04:27 +0000295 // Variables at global scope with non-internal linkage are not mangled
Eli Friedman7facf842009-12-02 20:32:49 +0000296 if (!FD) {
297 const DeclContext *DC = D->getDeclContext();
298 // Check for extern variable declared locally.
Fariborz Jahaniane81c5612010-06-30 18:57:21 +0000299 if (DC->isFunctionOrMethod() && D->hasLinkage())
Eli Friedman7facf842009-12-02 20:32:49 +0000300 while (!DC->isNamespace() && !DC->isTranslationUnit())
301 DC = DC->getParent();
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000302 if (DC->isTranslationUnit() && D->getLinkage() != InternalLinkage)
Eli Friedman7facf842009-12-02 20:32:49 +0000303 return false;
304 }
305
Eli Friedmanc00cb642010-07-18 20:49:59 +0000306 // Class members are always mangled.
307 if (D->getDeclContext()->isRecord())
308 return true;
309
Eli Friedman7facf842009-12-02 20:32:49 +0000310 // C functions and "main" are not mangled.
311 if ((FD && FD->isMain()) || isInCLinkageSpecification(D))
Daniel Dunbarf981bf82009-11-21 09:14:52 +0000312 return false;
313
Anders Carlsson43f17402009-04-02 15:51:53 +0000314 return true;
315}
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000316
Daniel Dunbar7e0c1952009-11-21 09:17:15 +0000317void CXXNameMangler::mangle(const NamedDecl *D, llvm::StringRef Prefix) {
Mike Stump141c5af2009-09-02 00:25:38 +0000318 // Any decl can be declared with __asm("foo") on it, and this takes precedence
319 // over all other naming in the .o file.
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000320 if (const AsmLabelAttr *ALA = D->getAttr<AsmLabelAttr>()) {
Chris Lattnerca3f25c2009-03-21 08:24:40 +0000321 // If we have an asm name, then we use it as the mangling.
322 Out << '\01'; // LLVM IR Marker for __asm("foo")
323 Out << ALA->getLabel();
Daniel Dunbarf981bf82009-11-21 09:14:52 +0000324 return;
Chris Lattnerca3f25c2009-03-21 08:24:40 +0000325 }
Mike Stump1eb44332009-09-09 15:08:12 +0000326
Sean Hunt31455252010-01-24 03:04:27 +0000327 // <mangled-name> ::= _Z <encoding>
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000328 // ::= <data name>
329 // ::= <special-name>
Daniel Dunbar7e0c1952009-11-21 09:17:15 +0000330 Out << Prefix;
331 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
Daniel Dunbarf981bf82009-11-21 09:14:52 +0000332 mangleFunctionEncoding(FD);
Rafael Espindolad9800722010-03-11 14:07:00 +0000333 else if (const VarDecl *VD = dyn_cast<VarDecl>(D))
334 mangleName(VD);
Daniel Dunbar7e0c1952009-11-21 09:17:15 +0000335 else
Rafael Espindolad9800722010-03-11 14:07:00 +0000336 mangleName(cast<FieldDecl>(D));
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000337}
338
339void CXXNameMangler::mangleFunctionEncoding(const FunctionDecl *FD) {
340 // <encoding> ::= <function name> <bare-function-type>
341 mangleName(FD);
Mike Stump1eb44332009-09-09 15:08:12 +0000342
Daniel Dunbar7e0c1952009-11-21 09:17:15 +0000343 // Don't mangle in the type if this isn't a decl we should typically mangle.
344 if (!Context.shouldMangleDeclName(FD))
345 return;
346
Mike Stump141c5af2009-09-02 00:25:38 +0000347 // Whether the mangling of a function type includes the return type depends on
348 // the context and the nature of the function. The rules for deciding whether
349 // the return type is included are:
Mike Stump1eb44332009-09-09 15:08:12 +0000350 //
Douglas Gregor1fd2dd12009-06-29 22:39:32 +0000351 // 1. Template functions (names or types) have return types encoded, with
352 // the exceptions listed below.
Mike Stump1eb44332009-09-09 15:08:12 +0000353 // 2. Function types not appearing as part of a function name mangling,
Douglas Gregor1fd2dd12009-06-29 22:39:32 +0000354 // e.g. parameters, pointer types, etc., have return type encoded, with the
355 // exceptions listed below.
356 // 3. Non-template function names do not have return types encoded.
357 //
Mike Stump141c5af2009-09-02 00:25:38 +0000358 // The exceptions mentioned in (1) and (2) above, for which the return type is
359 // never included, are
Douglas Gregor1fd2dd12009-06-29 22:39:32 +0000360 // 1. Constructors.
361 // 2. Destructors.
362 // 3. Conversion operator functions, e.g. operator int.
363 bool MangleReturnType = false;
Anders Carlsson9234b7f2009-09-17 03:46:43 +0000364 if (FunctionTemplateDecl *PrimaryTemplate = FD->getPrimaryTemplate()) {
365 if (!(isa<CXXConstructorDecl>(FD) || isa<CXXDestructorDecl>(FD) ||
366 isa<CXXConversionDecl>(FD)))
367 MangleReturnType = true;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000368
Anders Carlsson9234b7f2009-09-17 03:46:43 +0000369 // Mangle the type of the primary template.
370 FD = PrimaryTemplate->getTemplatedDecl();
371 }
372
John McCall54e14c42009-10-22 22:37:11 +0000373 // Do the canonicalization out here because parameter types can
374 // undergo additional canonicalization (e.g. array decay).
375 FunctionType *FT = cast<FunctionType>(Context.getASTContext()
376 .getCanonicalType(FD->getType()));
377
378 mangleBareFunctionType(FT, MangleReturnType);
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000379}
380
Anders Carlsson47846d22009-12-04 06:23:23 +0000381static const DeclContext *IgnoreLinkageSpecDecls(const DeclContext *DC) {
382 while (isa<LinkageSpecDecl>(DC)) {
Anders Carlsson47846d22009-12-04 06:23:23 +0000383 DC = DC->getParent();
384 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000385
Anders Carlsson47846d22009-12-04 06:23:23 +0000386 return DC;
387}
388
Anders Carlssonc820f902010-06-02 15:58:27 +0000389/// isStd - Return whether a given namespace is the 'std' namespace.
390static bool isStd(const NamespaceDecl *NS) {
391 if (!IgnoreLinkageSpecDecls(NS->getParent())->isTranslationUnit())
392 return false;
393
394 const IdentifierInfo *II = NS->getOriginalNamespace()->getIdentifier();
395 return II && II->isStr("std");
396}
397
Anders Carlsson47846d22009-12-04 06:23:23 +0000398// isStdNamespace - Return whether a given decl context is a toplevel 'std'
399// namespace.
Daniel Dunbar1308af92009-11-21 09:11:45 +0000400static bool isStdNamespace(const DeclContext *DC) {
Anders Carlsson47846d22009-12-04 06:23:23 +0000401 if (!DC->isNamespace())
402 return false;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000403
Anders Carlsson47846d22009-12-04 06:23:23 +0000404 return isStd(cast<NamespaceDecl>(DC));
Daniel Dunbar1308af92009-11-21 09:11:45 +0000405}
406
Anders Carlssonbb36ba42009-09-26 03:24:57 +0000407static const TemplateDecl *
408isTemplate(const NamedDecl *ND, const TemplateArgumentList *&TemplateArgs) {
Anders Carlsson2744a062009-09-18 19:00:18 +0000409 // Check if we have a function template.
410 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)){
Anders Carlssonbb36ba42009-09-26 03:24:57 +0000411 if (const TemplateDecl *TD = FD->getPrimaryTemplate()) {
Anders Carlsson2744a062009-09-18 19:00:18 +0000412 TemplateArgs = FD->getTemplateSpecializationArgs();
Anders Carlssonbb36ba42009-09-26 03:24:57 +0000413 return TD;
Anders Carlsson2744a062009-09-18 19:00:18 +0000414 }
415 }
416
Anders Carlssoneafc6dc2009-09-18 19:44:50 +0000417 // Check if we have a class template.
418 if (const ClassTemplateSpecializationDecl *Spec =
419 dyn_cast<ClassTemplateSpecializationDecl>(ND)) {
420 TemplateArgs = &Spec->getTemplateArgs();
Anders Carlssonbb36ba42009-09-26 03:24:57 +0000421 return Spec->getSpecializedTemplate();
Anders Carlssoneafc6dc2009-09-18 19:44:50 +0000422 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000423
Anders Carlsson2744a062009-09-18 19:00:18 +0000424 return 0;
425}
426
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000427void CXXNameMangler::mangleName(const NamedDecl *ND) {
428 // <name> ::= <nested-name>
429 // ::= <unscoped-name>
430 // ::= <unscoped-template-name> <template-args>
Anders Carlsson201ce742009-09-17 03:17:01 +0000431 // ::= <local-name>
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000432 //
Anders Carlssond58d6f72009-09-17 16:12:20 +0000433 const DeclContext *DC = ND->getDeclContext();
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000434
Fariborz Jahanian57058532010-03-03 19:41:08 +0000435 if (GetLocalClassFunctionDeclContext(DC)) {
436 mangleLocalName(ND);
437 return;
438 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000439
Eli Friedman7facf842009-12-02 20:32:49 +0000440 // If this is an extern variable declared locally, the relevant DeclContext
441 // is that of the containing namespace, or the translation unit.
442 if (isa<FunctionDecl>(DC) && ND->hasLinkage())
443 while (!DC->isNamespace() && !DC->isTranslationUnit())
444 DC = DC->getParent();
445
Anders Carlsson5cc58c62009-09-22 17:23:30 +0000446 while (isa<LinkageSpecDecl>(DC))
Anders Carlssond58d6f72009-09-17 16:12:20 +0000447 DC = DC->getParent();
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000448
Anders Carlssond58d6f72009-09-17 16:12:20 +0000449 if (DC->isTranslationUnit() || isStdNamespace(DC)) {
Anders Carlsson2744a062009-09-18 19:00:18 +0000450 // Check if we have a template.
451 const TemplateArgumentList *TemplateArgs = 0;
Anders Carlsson0fa6df42009-09-26 19:45:45 +0000452 if (const TemplateDecl *TD = isTemplate(ND, TemplateArgs)) {
Anders Carlsson2744a062009-09-18 19:00:18 +0000453 mangleUnscopedTemplateName(TD);
Rafael Espindolad9800722010-03-11 14:07:00 +0000454 TemplateParameterList *TemplateParameters = TD->getTemplateParameters();
455 mangleTemplateArgs(*TemplateParameters, *TemplateArgs);
Anders Carlsson2744a062009-09-18 19:00:18 +0000456 return;
Anders Carlsson7482e242009-09-18 04:29:09 +0000457 }
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000458
Anders Carlsson7482e242009-09-18 04:29:09 +0000459 mangleUnscopedName(ND);
460 return;
461 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000462
Anders Carlsson7b06f6c2009-12-10 03:14:39 +0000463 if (isa<FunctionDecl>(DC) || isa<ObjCMethodDecl>(DC)) {
Anders Carlsson7482e242009-09-18 04:29:09 +0000464 mangleLocalName(ND);
465 return;
466 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000467
Eli Friedman7facf842009-12-02 20:32:49 +0000468 mangleNestedName(ND, DC);
Anders Carlsson7482e242009-09-18 04:29:09 +0000469}
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000470void CXXNameMangler::mangleName(const TemplateDecl *TD,
Anders Carlsson7624f212009-09-18 02:42:01 +0000471 const TemplateArgument *TemplateArgs,
472 unsigned NumTemplateArgs) {
Anders Carlsson47846d22009-12-04 06:23:23 +0000473 const DeclContext *DC = IgnoreLinkageSpecDecls(TD->getDeclContext());
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000474
Anders Carlsson7624f212009-09-18 02:42:01 +0000475 if (DC->isTranslationUnit() || isStdNamespace(DC)) {
Anders Carlsson0fa6df42009-09-26 19:45:45 +0000476 mangleUnscopedTemplateName(TD);
Rafael Espindolad9800722010-03-11 14:07:00 +0000477 TemplateParameterList *TemplateParameters = TD->getTemplateParameters();
478 mangleTemplateArgs(*TemplateParameters, TemplateArgs, NumTemplateArgs);
Anders Carlsson7624f212009-09-18 02:42:01 +0000479 } else {
480 mangleNestedName(TD, TemplateArgs, NumTemplateArgs);
481 }
482}
483
Anders Carlsson201ce742009-09-17 03:17:01 +0000484void CXXNameMangler::mangleUnscopedName(const NamedDecl *ND) {
485 // <unscoped-name> ::= <unqualified-name>
486 // ::= St <unqualified-name> # ::std::
487 if (isStdNamespace(ND->getDeclContext()))
488 Out << "St";
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000489
Anders Carlsson201ce742009-09-17 03:17:01 +0000490 mangleUnqualifiedName(ND);
491}
492
Anders Carlsson0fa6df42009-09-26 19:45:45 +0000493void CXXNameMangler::mangleUnscopedTemplateName(const TemplateDecl *ND) {
Anders Carlsson201ce742009-09-17 03:17:01 +0000494 // <unscoped-template-name> ::= <unscoped-name>
495 // ::= <substitution>
Anders Carlsson7624f212009-09-18 02:42:01 +0000496 if (mangleSubstitution(ND))
Anders Carlsson03c9d532009-09-17 04:02:31 +0000497 return;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000498
Douglas Gregor32fb4e12010-02-05 20:45:00 +0000499 // <template-template-param> ::= <template-param>
500 if (const TemplateTemplateParmDecl *TTP
501 = dyn_cast<TemplateTemplateParmDecl>(ND)) {
502 mangleTemplateParameter(TTP->getIndex());
503 return;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000504 }
Douglas Gregor32fb4e12010-02-05 20:45:00 +0000505
Anders Carlsson1668f202009-09-26 20:13:56 +0000506 mangleUnscopedName(ND->getTemplatedDecl());
Anders Carlsson7624f212009-09-18 02:42:01 +0000507 addSubstitution(ND);
Anders Carlsson201ce742009-09-17 03:17:01 +0000508}
509
Douglas Gregor1e9268e2010-04-28 05:58:56 +0000510void CXXNameMangler::mangleUnscopedTemplateName(TemplateName Template) {
511 // <unscoped-template-name> ::= <unscoped-name>
512 // ::= <substitution>
513 if (TemplateDecl *TD = Template.getAsTemplateDecl())
514 return mangleUnscopedTemplateName(TD);
Sean Huntc3021132010-05-05 15:23:54 +0000515
Douglas Gregor1e9268e2010-04-28 05:58:56 +0000516 if (mangleSubstitution(Template))
517 return;
518
519 // FIXME: How to cope with operators here?
520 DependentTemplateName *Dependent = Template.getAsDependentTemplateName();
521 assert(Dependent && "Not a dependent template name?");
522 if (!Dependent->isIdentifier()) {
523 // FIXME: We can't possibly know the arity of the operator here!
524 Diagnostic &Diags = Context.getDiags();
525 unsigned DiagID = Diags.getCustomDiagID(Diagnostic::Error,
526 "cannot mangle dependent operator name");
527 Diags.Report(FullSourceLoc(), DiagID);
528 return;
529 }
Sean Huntc3021132010-05-05 15:23:54 +0000530
Douglas Gregor1e9268e2010-04-28 05:58:56 +0000531 mangleSourceName(Dependent->getIdentifier());
532 addSubstitution(Template);
533}
534
John McCall0512e482010-07-14 04:20:34 +0000535void CXXNameMangler::mangleFloat(const llvm::APFloat &F) {
536 // TODO: avoid this copy with careful stream management.
537 llvm::SmallString<20> Buffer;
538 F.bitcastToAPInt().toString(Buffer, 16, false);
539 Out.write(Buffer.data(), Buffer.size());
540}
541
542void CXXNameMangler::mangleNumber(const llvm::APSInt &Value) {
543 if (Value.isSigned() && Value.isNegative()) {
544 Out << 'n';
545 Value.abs().print(Out, true);
546 } else
547 Value.print(Out, Value.isSigned());
548}
549
Anders Carlssona94822e2009-11-26 02:32:05 +0000550void CXXNameMangler::mangleNumber(int64_t Number) {
551 // <number> ::= [n] <non-negative decimal integer>
552 if (Number < 0) {
553 Out << 'n';
554 Number = -Number;
555 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000556
Anders Carlssona94822e2009-11-26 02:32:05 +0000557 Out << Number;
558}
559
Anders Carlsson19879c92010-03-23 17:17:29 +0000560void CXXNameMangler::mangleCallOffset(int64_t NonVirtual, int64_t Virtual) {
Mike Stump141c5af2009-09-02 00:25:38 +0000561 // <call-offset> ::= h <nv-offset> _
562 // ::= v <v-offset> _
563 // <nv-offset> ::= <offset number> # non-virtual base override
Anders Carlssona94822e2009-11-26 02:32:05 +0000564 // <v-offset> ::= <offset number> _ <virtual offset number>
Mike Stump141c5af2009-09-02 00:25:38 +0000565 // # virtual base override, with vcall offset
Anders Carlsson19879c92010-03-23 17:17:29 +0000566 if (!Virtual) {
Anders Carlssona94822e2009-11-26 02:32:05 +0000567 Out << 'h';
Anders Carlsson19879c92010-03-23 17:17:29 +0000568 mangleNumber(NonVirtual);
Anders Carlssona94822e2009-11-26 02:32:05 +0000569 Out << '_';
570 return;
Mike Stump141c5af2009-09-02 00:25:38 +0000571 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000572
Anders Carlssona94822e2009-11-26 02:32:05 +0000573 Out << 'v';
Anders Carlsson19879c92010-03-23 17:17:29 +0000574 mangleNumber(NonVirtual);
Anders Carlssona94822e2009-11-26 02:32:05 +0000575 Out << '_';
Anders Carlsson19879c92010-03-23 17:17:29 +0000576 mangleNumber(Virtual);
Anders Carlssona94822e2009-11-26 02:32:05 +0000577 Out << '_';
Mike Stump9124bcc2009-09-02 00:56:18 +0000578}
579
John McCall1dd73832010-02-04 01:42:13 +0000580void CXXNameMangler::mangleUnresolvedScope(NestedNameSpecifier *Qualifier) {
581 Qualifier = getASTContext().getCanonicalNestedNameSpecifier(Qualifier);
582 switch (Qualifier->getKind()) {
583 case NestedNameSpecifier::Global:
584 // nothing
585 break;
586 case NestedNameSpecifier::Namespace:
587 mangleName(Qualifier->getAsNamespace());
588 break;
589 case NestedNameSpecifier::TypeSpec:
Rafael Espindola9b35b252010-03-17 04:28:11 +0000590 case NestedNameSpecifier::TypeSpecWithTemplate: {
591 const Type *QTy = Qualifier->getAsType();
592
593 if (const TemplateSpecializationType *TST =
594 dyn_cast<TemplateSpecializationType>(QTy)) {
595 if (!mangleSubstitution(QualType(TST, 0))) {
Douglas Gregor20f0cc72010-04-23 03:10:43 +0000596 mangleTemplatePrefix(TST->getTemplateName());
Sean Huntc3021132010-05-05 15:23:54 +0000597
Douglas Gregor20f0cc72010-04-23 03:10:43 +0000598 // FIXME: GCC does not appear to mangle the template arguments when
599 // the template in question is a dependent template name. Should we
600 // emulate that badness?
601 mangleTemplateArgs(TST->getTemplateName(), TST->getArgs(),
Rafael Espindola9b35b252010-03-17 04:28:11 +0000602 TST->getNumArgs());
603 addSubstitution(QualType(TST, 0));
604 }
605 } else {
606 // We use the QualType mangle type variant here because it handles
607 // substitutions.
608 mangleType(QualType(QTy, 0));
609 }
610 }
John McCall1dd73832010-02-04 01:42:13 +0000611 break;
612 case NestedNameSpecifier::Identifier:
John McCallad5e7382010-03-01 23:49:17 +0000613 // Member expressions can have these without prefixes.
614 if (Qualifier->getPrefix())
615 mangleUnresolvedScope(Qualifier->getPrefix());
John McCall1dd73832010-02-04 01:42:13 +0000616 mangleSourceName(Qualifier->getAsIdentifier());
617 break;
618 }
619}
620
621/// Mangles a name which was not resolved to a specific entity.
622void CXXNameMangler::mangleUnresolvedName(NestedNameSpecifier *Qualifier,
623 DeclarationName Name,
624 unsigned KnownArity) {
625 if (Qualifier)
626 mangleUnresolvedScope(Qualifier);
627 // FIXME: ambiguity of unqualified lookup with ::
628
629 mangleUnqualifiedName(0, Name, KnownArity);
630}
631
Anders Carlsson6f7e2f42010-06-08 14:49:03 +0000632static const FieldDecl *FindFirstNamedDataMember(const RecordDecl *RD) {
633 assert(RD->isAnonymousStructOrUnion() &&
634 "Expected anonymous struct or union!");
635
636 for (RecordDecl::field_iterator I = RD->field_begin(), E = RD->field_end();
637 I != E; ++I) {
638 const FieldDecl *FD = *I;
639
640 if (FD->getIdentifier())
641 return FD;
642
643 if (const RecordType *RT = FD->getType()->getAs<RecordType>()) {
644 if (const FieldDecl *NamedDataMember =
645 FindFirstNamedDataMember(RT->getDecl()))
646 return NamedDataMember;
647 }
648 }
649
650 // We didn't find a named data member.
651 return 0;
652}
653
John McCall1dd73832010-02-04 01:42:13 +0000654void CXXNameMangler::mangleUnqualifiedName(const NamedDecl *ND,
655 DeclarationName Name,
656 unsigned KnownArity) {
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000657 // <unqualified-name> ::= <operator-name>
Mike Stump1eb44332009-09-09 15:08:12 +0000658 // ::= <ctor-dtor-name>
659 // ::= <source-name>
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000660 switch (Name.getNameKind()) {
Anders Carlssonc4355b62009-10-07 01:45:02 +0000661 case DeclarationName::Identifier: {
Anders Carlssonc4355b62009-10-07 01:45:02 +0000662 if (const IdentifierInfo *II = Name.getAsIdentifierInfo()) {
Sean Hunt31455252010-01-24 03:04:27 +0000663 // We must avoid conflicts between internally- and externally-
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000664 // linked variable declaration names in the same TU.
Anders Carlssonaec25232010-02-06 04:52:27 +0000665 // This naming convention is the same as that followed by GCC, though it
666 // shouldn't actually matter.
667 if (ND && isa<VarDecl>(ND) && ND->getLinkage() == InternalLinkage &&
Sean Hunt31455252010-01-24 03:04:27 +0000668 ND->getDeclContext()->isFileContext())
669 Out << 'L';
670
Anders Carlssonc4355b62009-10-07 01:45:02 +0000671 mangleSourceName(II);
672 break;
673 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000674
John McCall1dd73832010-02-04 01:42:13 +0000675 // Otherwise, an anonymous entity. We must have a declaration.
676 assert(ND && "mangling empty name without declaration");
677
678 if (const NamespaceDecl *NS = dyn_cast<NamespaceDecl>(ND)) {
679 if (NS->isAnonymousNamespace()) {
680 // This is how gcc mangles these names.
681 Out << "12_GLOBAL__N_1";
682 break;
683 }
684 }
685
Anders Carlsson6f7e2f42010-06-08 14:49:03 +0000686 if (const VarDecl *VD = dyn_cast<VarDecl>(ND)) {
687 // We must have an anonymous union or struct declaration.
688 const RecordDecl *RD =
689 cast<RecordDecl>(VD->getType()->getAs<RecordType>()->getDecl());
690
691 // Itanium C++ ABI 5.1.2:
692 //
693 // For the purposes of mangling, the name of an anonymous union is
694 // considered to be the name of the first named data member found by a
695 // pre-order, depth-first, declaration-order walk of the data members of
696 // the anonymous union. If there is no such data member (i.e., if all of
697 // the data members in the union are unnamed), then there is no way for
698 // a program to refer to the anonymous union, and there is therefore no
699 // need to mangle its name.
700 const FieldDecl *FD = FindFirstNamedDataMember(RD);
John McCall7121c8f2010-08-05 22:02:13 +0000701
702 // It's actually possible for various reasons for us to get here
703 // with an empty anonymous struct / union. Fortunately, it
704 // doesn't really matter what name we generate.
705 if (!FD) break;
Anders Carlsson6f7e2f42010-06-08 14:49:03 +0000706 assert(FD->getIdentifier() && "Data member name isn't an identifier!");
707
708 mangleSourceName(FD->getIdentifier());
709 break;
710 }
711
Anders Carlssonc4355b62009-10-07 01:45:02 +0000712 // We must have an anonymous struct.
713 const TagDecl *TD = cast<TagDecl>(ND);
714 if (const TypedefDecl *D = TD->getTypedefForAnonDecl()) {
715 assert(TD->getDeclContext() == D->getDeclContext() &&
716 "Typedef should not be in another decl context!");
717 assert(D->getDeclName().getAsIdentifierInfo() &&
718 "Typedef was not named!");
719 mangleSourceName(D->getDeclName().getAsIdentifierInfo());
720 break;
721 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000722
Anders Carlssonc4355b62009-10-07 01:45:02 +0000723 // Get a unique id for the anonymous struct.
724 uint64_t AnonStructId = Context.getAnonymousStructId(TD);
725
726 // Mangle it as a source name in the form
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000727 // [n] $_<id>
Anders Carlssonc4355b62009-10-07 01:45:02 +0000728 // where n is the length of the string.
729 llvm::SmallString<8> Str;
730 Str += "$_";
731 Str += llvm::utostr(AnonStructId);
732
733 Out << Str.size();
734 Out << Str.str();
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000735 break;
Anders Carlssonc4355b62009-10-07 01:45:02 +0000736 }
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000737
738 case DeclarationName::ObjCZeroArgSelector:
739 case DeclarationName::ObjCOneArgSelector:
740 case DeclarationName::ObjCMultiArgSelector:
741 assert(false && "Can't mangle Objective-C selector names here!");
742 break;
743
744 case DeclarationName::CXXConstructorName:
Anders Carlsson27ae5362009-04-17 01:58:57 +0000745 if (ND == Structor)
Mike Stump141c5af2009-09-02 00:25:38 +0000746 // If the named decl is the C++ constructor we're mangling, use the type
747 // we were given.
Anders Carlsson27ae5362009-04-17 01:58:57 +0000748 mangleCXXCtorType(static_cast<CXXCtorType>(StructorType));
Anders Carlsson3ac86b52009-04-15 05:36:58 +0000749 else
750 // Otherwise, use the complete constructor name. This is relevant if a
751 // class with a constructor is declared within a constructor.
752 mangleCXXCtorType(Ctor_Complete);
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000753 break;
754
755 case DeclarationName::CXXDestructorName:
Anders Carlsson27ae5362009-04-17 01:58:57 +0000756 if (ND == Structor)
Mike Stump141c5af2009-09-02 00:25:38 +0000757 // If the named decl is the C++ destructor we're mangling, use the type we
758 // were given.
Anders Carlsson27ae5362009-04-17 01:58:57 +0000759 mangleCXXDtorType(static_cast<CXXDtorType>(StructorType));
760 else
761 // Otherwise, use the complete destructor name. This is relevant if a
762 // class with a destructor is declared within a destructor.
763 mangleCXXDtorType(Dtor_Complete);
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000764 break;
765
766 case DeclarationName::CXXConversionFunctionName:
Mike Stump1eb44332009-09-09 15:08:12 +0000767 // <operator-name> ::= cv <type> # (cast)
Douglas Gregor219cc612009-02-13 01:28:03 +0000768 Out << "cv";
Anders Carlssonb5404912009-10-07 01:06:45 +0000769 mangleType(Context.getASTContext().getCanonicalType(Name.getCXXNameType()));
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000770 break;
771
Anders Carlsson8257d412009-12-22 06:36:32 +0000772 case DeclarationName::CXXOperatorName: {
John McCall1dd73832010-02-04 01:42:13 +0000773 unsigned Arity;
774 if (ND) {
775 Arity = cast<FunctionDecl>(ND)->getNumParams();
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000776
John McCall1dd73832010-02-04 01:42:13 +0000777 // If we have a C++ member function, we need to include the 'this' pointer.
778 // FIXME: This does not make sense for operators that are static, but their
779 // names stay the same regardless of the arity (operator new for instance).
780 if (isa<CXXMethodDecl>(ND))
781 Arity++;
782 } else
783 Arity = KnownArity;
784
Anders Carlsson8257d412009-12-22 06:36:32 +0000785 mangleOperatorName(Name.getCXXOverloadedOperator(), Arity);
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000786 break;
Anders Carlsson8257d412009-12-22 06:36:32 +0000787 }
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000788
Sean Hunt3e518bd2009-11-29 07:34:05 +0000789 case DeclarationName::CXXLiteralOperatorName:
Sean Hunt5dd6b392009-12-04 21:11:13 +0000790 // FIXME: This mangling is not yet official.
Sean Hunt2421f662009-12-04 21:01:37 +0000791 Out << "li";
Sean Hunt3e518bd2009-11-29 07:34:05 +0000792 mangleSourceName(Name.getCXXLiteralIdentifier());
793 break;
794
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000795 case DeclarationName::CXXUsingDirective:
796 assert(false && "Can't mangle a using directive name!");
Douglas Gregor219cc612009-02-13 01:28:03 +0000797 break;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000798 }
799}
800
801void CXXNameMangler::mangleSourceName(const IdentifierInfo *II) {
802 // <source-name> ::= <positive length number> <identifier>
803 // <number> ::= [n] <non-negative decimal integer>
804 // <identifier> ::= <unqualified source code identifier>
805 Out << II->getLength() << II->getName();
806}
807
Eli Friedman7facf842009-12-02 20:32:49 +0000808void CXXNameMangler::mangleNestedName(const NamedDecl *ND,
Fariborz Jahanian57058532010-03-03 19:41:08 +0000809 const DeclContext *DC,
810 bool NoFunction) {
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000811 // <nested-name> ::= N [<CV-qualifiers>] <prefix> <unqualified-name> E
812 // ::= N [<CV-qualifiers>] <template-prefix> <template-args> E
Anders Carlssond99edc42009-09-26 03:55:37 +0000813
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000814 Out << 'N';
815 if (const CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(ND))
John McCall0953e762009-09-24 19:53:00 +0000816 mangleQualifiers(Qualifiers::fromCVRMask(Method->getTypeQualifiers()));
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000817
Anders Carlsson2744a062009-09-18 19:00:18 +0000818 // Check if we have a template.
819 const TemplateArgumentList *TemplateArgs = 0;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000820 if (const TemplateDecl *TD = isTemplate(ND, TemplateArgs)) {
Anders Carlsson2744a062009-09-18 19:00:18 +0000821 mangleTemplatePrefix(TD);
Rafael Espindolad9800722010-03-11 14:07:00 +0000822 TemplateParameterList *TemplateParameters = TD->getTemplateParameters();
823 mangleTemplateArgs(*TemplateParameters, *TemplateArgs);
Fariborz Jahanian57058532010-03-03 19:41:08 +0000824 }
825 else {
826 manglePrefix(DC, NoFunction);
Anders Carlsson7482e242009-09-18 04:29:09 +0000827 mangleUnqualifiedName(ND);
828 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000829
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000830 Out << 'E';
831}
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000832void CXXNameMangler::mangleNestedName(const TemplateDecl *TD,
Anders Carlsson7624f212009-09-18 02:42:01 +0000833 const TemplateArgument *TemplateArgs,
834 unsigned NumTemplateArgs) {
Anders Carlssone45117b2009-09-27 19:53:49 +0000835 // <nested-name> ::= N [<CV-qualifiers>] <template-prefix> <template-args> E
836
Anders Carlsson7624f212009-09-18 02:42:01 +0000837 Out << 'N';
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000838
Anders Carlssone45117b2009-09-27 19:53:49 +0000839 mangleTemplatePrefix(TD);
Rafael Espindolad9800722010-03-11 14:07:00 +0000840 TemplateParameterList *TemplateParameters = TD->getTemplateParameters();
841 mangleTemplateArgs(*TemplateParameters, TemplateArgs, NumTemplateArgs);
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000842
Anders Carlsson7624f212009-09-18 02:42:01 +0000843 Out << 'E';
844}
845
Anders Carlsson1b42c792009-04-02 16:24:45 +0000846void CXXNameMangler::mangleLocalName(const NamedDecl *ND) {
847 // <local-name> := Z <function encoding> E <entity name> [<discriminator>]
848 // := Z <function encoding> E s [<discriminator>]
Mike Stump1eb44332009-09-09 15:08:12 +0000849 // <discriminator> := _ <non-negative number>
Fariborz Jahanian57058532010-03-03 19:41:08 +0000850 const DeclContext *DC = ND->getDeclContext();
Anders Carlsson1b42c792009-04-02 16:24:45 +0000851 Out << 'Z';
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000852
Charles Davis685b1d92010-05-26 18:25:27 +0000853 if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(DC)) {
854 mangleObjCMethodName(MD);
855 }
Fariborz Jahanian57058532010-03-03 19:41:08 +0000856 else if (const DeclContext *CDC = GetLocalClassFunctionDeclContext(DC)) {
857 mangleFunctionEncoding(cast<FunctionDecl>(CDC));
858 Out << 'E';
859 mangleNestedName(ND, DC, true /*NoFunction*/);
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000860
Fariborz Jahanian4819ac42010-03-04 01:02:03 +0000861 // FIXME. This still does not cover all cases.
862 unsigned disc;
863 if (Context.getNextDiscriminator(ND, disc)) {
864 if (disc < 10)
865 Out << '_' << disc;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000866 else
Fariborz Jahanian4819ac42010-03-04 01:02:03 +0000867 Out << "__" << disc << '_';
868 }
Fariborz Jahanian57058532010-03-03 19:41:08 +0000869
870 return;
871 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000872 else
Fariborz Jahanian57058532010-03-03 19:41:08 +0000873 mangleFunctionEncoding(cast<FunctionDecl>(DC));
Anders Carlsson7b06f6c2009-12-10 03:14:39 +0000874
Anders Carlsson1b42c792009-04-02 16:24:45 +0000875 Out << 'E';
Eli Friedman6f9f25d2009-12-11 20:21:38 +0000876 mangleUnqualifiedName(ND);
Anders Carlsson1b42c792009-04-02 16:24:45 +0000877}
878
Fariborz Jahanian57058532010-03-03 19:41:08 +0000879void CXXNameMangler::manglePrefix(const DeclContext *DC, bool NoFunction) {
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000880 // <prefix> ::= <prefix> <unqualified-name>
881 // ::= <template-prefix> <template-args>
882 // ::= <template-param>
883 // ::= # empty
884 // ::= <substitution>
Anders Carlsson6862fc72009-09-17 04:16:28 +0000885
Anders Carlssonadd28822009-09-22 20:33:31 +0000886 while (isa<LinkageSpecDecl>(DC))
887 DC = DC->getParent();
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000888
Anders Carlsson9263e912009-09-18 18:39:58 +0000889 if (DC->isTranslationUnit())
890 return;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000891
Douglas Gregor35415f52010-05-25 17:04:15 +0000892 if (const BlockDecl *Block = dyn_cast<BlockDecl>(DC)) {
893 manglePrefix(DC->getParent(), NoFunction);
894 llvm::SmallString<64> Name;
Fariborz Jahanian564360b2010-06-24 00:08:06 +0000895 Context.mangleBlock(GlobalDecl(), Block, Name);
Douglas Gregor35415f52010-05-25 17:04:15 +0000896 Out << Name.size() << Name;
897 return;
898 }
899
Anders Carlsson6862fc72009-09-17 04:16:28 +0000900 if (mangleSubstitution(cast<NamedDecl>(DC)))
901 return;
Anders Carlsson7482e242009-09-18 04:29:09 +0000902
Anders Carlsson2ee3fca2009-09-18 20:11:09 +0000903 // Check if we have a template.
904 const TemplateArgumentList *TemplateArgs = 0;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000905 if (const TemplateDecl *TD = isTemplate(cast<NamedDecl>(DC), TemplateArgs)) {
Anders Carlsson2ee3fca2009-09-18 20:11:09 +0000906 mangleTemplatePrefix(TD);
Rafael Espindolad9800722010-03-11 14:07:00 +0000907 TemplateParameterList *TemplateParameters = TD->getTemplateParameters();
908 mangleTemplateArgs(*TemplateParameters, *TemplateArgs);
Fariborz Jahanian57058532010-03-03 19:41:08 +0000909 }
Douglas Gregor35415f52010-05-25 17:04:15 +0000910 else if(NoFunction && (isa<FunctionDecl>(DC) || isa<ObjCMethodDecl>(DC)))
Fariborz Jahanian57058532010-03-03 19:41:08 +0000911 return;
Douglas Gregor35415f52010-05-25 17:04:15 +0000912 else if (const ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(DC))
913 mangleObjCMethodName(Method);
Fariborz Jahanian57058532010-03-03 19:41:08 +0000914 else {
915 manglePrefix(DC->getParent(), NoFunction);
Anders Carlsson2ee3fca2009-09-18 20:11:09 +0000916 mangleUnqualifiedName(cast<NamedDecl>(DC));
917 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000918
Anders Carlsson6862fc72009-09-17 04:16:28 +0000919 addSubstitution(cast<NamedDecl>(DC));
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000920}
921
Douglas Gregor20f0cc72010-04-23 03:10:43 +0000922void CXXNameMangler::mangleTemplatePrefix(TemplateName Template) {
923 // <template-prefix> ::= <prefix> <template unqualified-name>
924 // ::= <template-param>
925 // ::= <substitution>
926 if (TemplateDecl *TD = Template.getAsTemplateDecl())
927 return mangleTemplatePrefix(TD);
928
929 if (QualifiedTemplateName *Qualified = Template.getAsQualifiedTemplateName())
930 mangleUnresolvedScope(Qualified->getQualifier());
Sean Huntc3021132010-05-05 15:23:54 +0000931
Douglas Gregor20f0cc72010-04-23 03:10:43 +0000932 if (OverloadedTemplateStorage *Overloaded
933 = Template.getAsOverloadedTemplate()) {
Sean Huntc3021132010-05-05 15:23:54 +0000934 mangleUnqualifiedName(0, (*Overloaded->begin())->getDeclName(),
Douglas Gregor20f0cc72010-04-23 03:10:43 +0000935 UnknownArity);
936 return;
937 }
Sean Huntc3021132010-05-05 15:23:54 +0000938
Douglas Gregor20f0cc72010-04-23 03:10:43 +0000939 DependentTemplateName *Dependent = Template.getAsDependentTemplateName();
940 assert(Dependent && "Unknown template name kind?");
941 mangleUnresolvedScope(Dependent->getQualifier());
Douglas Gregor1e9268e2010-04-28 05:58:56 +0000942 mangleUnscopedTemplateName(Template);
Douglas Gregor20f0cc72010-04-23 03:10:43 +0000943}
944
Anders Carlsson0fa6df42009-09-26 19:45:45 +0000945void CXXNameMangler::mangleTemplatePrefix(const TemplateDecl *ND) {
Anders Carlsson7482e242009-09-18 04:29:09 +0000946 // <template-prefix> ::= <prefix> <template unqualified-name>
947 // ::= <template-param>
948 // ::= <substitution>
Douglas Gregor32fb4e12010-02-05 20:45:00 +0000949 // <template-template-param> ::= <template-param>
950 // <substitution>
Anders Carlsson7482e242009-09-18 04:29:09 +0000951
Anders Carlssonaeb85372009-09-26 22:18:22 +0000952 if (mangleSubstitution(ND))
953 return;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000954
Douglas Gregor32fb4e12010-02-05 20:45:00 +0000955 // <template-template-param> ::= <template-param>
956 if (const TemplateTemplateParmDecl *TTP
957 = dyn_cast<TemplateTemplateParmDecl>(ND)) {
958 mangleTemplateParameter(TTP->getIndex());
959 return;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000960 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000961
Anders Carlssonaa73ab12009-09-18 18:47:07 +0000962 manglePrefix(ND->getDeclContext());
Anders Carlsson1668f202009-09-26 20:13:56 +0000963 mangleUnqualifiedName(ND->getTemplatedDecl());
Anders Carlssonaeb85372009-09-26 22:18:22 +0000964 addSubstitution(ND);
Anders Carlsson7482e242009-09-18 04:29:09 +0000965}
966
John McCallb6f532e2010-07-14 06:43:17 +0000967/// Mangles a template name under the production <type>. Required for
968/// template template arguments.
969/// <type> ::= <class-enum-type>
970/// ::= <template-param>
971/// ::= <substitution>
972void CXXNameMangler::mangleType(TemplateName TN) {
973 if (mangleSubstitution(TN))
974 return;
975
976 TemplateDecl *TD = 0;
977
978 switch (TN.getKind()) {
979 case TemplateName::QualifiedTemplate:
980 TD = TN.getAsQualifiedTemplateName()->getTemplateDecl();
981 goto HaveDecl;
982
983 case TemplateName::Template:
984 TD = TN.getAsTemplateDecl();
985 goto HaveDecl;
986
987 HaveDecl:
988 if (isa<TemplateTemplateParmDecl>(TD))
989 mangleTemplateParameter(cast<TemplateTemplateParmDecl>(TD)->getIndex());
990 else
991 mangleName(TD);
992 break;
993
994 case TemplateName::OverloadedTemplate:
995 llvm_unreachable("can't mangle an overloaded template name as a <type>");
996 break;
997
998 case TemplateName::DependentTemplate: {
999 const DependentTemplateName *Dependent = TN.getAsDependentTemplateName();
1000 assert(Dependent->isIdentifier());
1001
1002 // <class-enum-type> ::= <name>
1003 // <name> ::= <nested-name>
1004 mangleUnresolvedScope(Dependent->getQualifier());
1005 mangleSourceName(Dependent->getIdentifier());
1006 break;
1007 }
1008
1009 }
1010
1011 addSubstitution(TN);
1012}
1013
Mike Stump1eb44332009-09-09 15:08:12 +00001014void
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001015CXXNameMangler::mangleOperatorName(OverloadedOperatorKind OO, unsigned Arity) {
1016 switch (OO) {
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001017 // <operator-name> ::= nw # new
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001018 case OO_New: Out << "nw"; break;
1019 // ::= na # new[]
1020 case OO_Array_New: Out << "na"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001021 // ::= dl # delete
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001022 case OO_Delete: Out << "dl"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001023 // ::= da # delete[]
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001024 case OO_Array_Delete: Out << "da"; break;
1025 // ::= ps # + (unary)
John McCall5e1e89b2010-08-18 19:18:59 +00001026 // ::= pl # + (binary or unknown)
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001027 case OO_Plus:
Anders Carlsson8257d412009-12-22 06:36:32 +00001028 Out << (Arity == 1? "ps" : "pl"); break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001029 // ::= ng # - (unary)
John McCall5e1e89b2010-08-18 19:18:59 +00001030 // ::= mi # - (binary or unknown)
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001031 case OO_Minus:
Anders Carlsson8257d412009-12-22 06:36:32 +00001032 Out << (Arity == 1? "ng" : "mi"); break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001033 // ::= ad # & (unary)
John McCall5e1e89b2010-08-18 19:18:59 +00001034 // ::= an # & (binary or unknown)
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001035 case OO_Amp:
Anders Carlsson8257d412009-12-22 06:36:32 +00001036 Out << (Arity == 1? "ad" : "an"); break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001037 // ::= de # * (unary)
John McCall5e1e89b2010-08-18 19:18:59 +00001038 // ::= ml # * (binary or unknown)
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001039 case OO_Star:
John McCall5e1e89b2010-08-18 19:18:59 +00001040 // Use binary when unknown.
Anders Carlsson8257d412009-12-22 06:36:32 +00001041 Out << (Arity == 1? "de" : "ml"); break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001042 // ::= co # ~
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001043 case OO_Tilde: Out << "co"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001044 // ::= dv # /
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001045 case OO_Slash: Out << "dv"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001046 // ::= rm # %
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001047 case OO_Percent: Out << "rm"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001048 // ::= or # |
1049 case OO_Pipe: Out << "or"; break;
1050 // ::= eo # ^
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001051 case OO_Caret: Out << "eo"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001052 // ::= aS # =
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001053 case OO_Equal: Out << "aS"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001054 // ::= pL # +=
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001055 case OO_PlusEqual: Out << "pL"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001056 // ::= mI # -=
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001057 case OO_MinusEqual: Out << "mI"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001058 // ::= mL # *=
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001059 case OO_StarEqual: Out << "mL"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001060 // ::= dV # /=
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001061 case OO_SlashEqual: Out << "dV"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001062 // ::= rM # %=
1063 case OO_PercentEqual: Out << "rM"; break;
1064 // ::= aN # &=
1065 case OO_AmpEqual: Out << "aN"; break;
1066 // ::= oR # |=
1067 case OO_PipeEqual: Out << "oR"; break;
1068 // ::= eO # ^=
1069 case OO_CaretEqual: Out << "eO"; break;
1070 // ::= ls # <<
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001071 case OO_LessLess: Out << "ls"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001072 // ::= rs # >>
1073 case OO_GreaterGreater: Out << "rs"; break;
1074 // ::= lS # <<=
1075 case OO_LessLessEqual: Out << "lS"; break;
1076 // ::= rS # >>=
1077 case OO_GreaterGreaterEqual: Out << "rS"; break;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001078 // ::= eq # ==
1079 case OO_EqualEqual: Out << "eq"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001080 // ::= ne # !=
1081 case OO_ExclaimEqual: Out << "ne"; break;
1082 // ::= lt # <
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001083 case OO_Less: Out << "lt"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001084 // ::= gt # >
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001085 case OO_Greater: Out << "gt"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001086 // ::= le # <=
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001087 case OO_LessEqual: Out << "le"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001088 // ::= ge # >=
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001089 case OO_GreaterEqual: Out << "ge"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001090 // ::= nt # !
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001091 case OO_Exclaim: Out << "nt"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001092 // ::= aa # &&
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001093 case OO_AmpAmp: Out << "aa"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001094 // ::= oo # ||
1095 case OO_PipePipe: Out << "oo"; break;
1096 // ::= pp # ++
1097 case OO_PlusPlus: Out << "pp"; break;
1098 // ::= mm # --
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001099 case OO_MinusMinus: Out << "mm"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001100 // ::= cm # ,
1101 case OO_Comma: Out << "cm"; break;
1102 // ::= pm # ->*
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001103 case OO_ArrowStar: Out << "pm"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001104 // ::= pt # ->
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001105 case OO_Arrow: Out << "pt"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001106 // ::= cl # ()
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001107 case OO_Call: Out << "cl"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001108 // ::= ix # []
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001109 case OO_Subscript: Out << "ix"; break;
Anders Carlssone170ba72009-12-14 01:45:37 +00001110
1111 // ::= qu # ?
1112 // The conditional operator can't be overloaded, but we still handle it when
1113 // mangling expressions.
1114 case OO_Conditional: Out << "qu"; break;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001115
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001116 case OO_None:
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001117 case NUM_OVERLOADED_OPERATORS:
Mike Stump1eb44332009-09-09 15:08:12 +00001118 assert(false && "Not an overloaded operator");
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001119 break;
1120 }
1121}
1122
John McCall0953e762009-09-24 19:53:00 +00001123void CXXNameMangler::mangleQualifiers(Qualifiers Quals) {
Mike Stump1eb44332009-09-09 15:08:12 +00001124 // <CV-qualifiers> ::= [r] [V] [K] # restrict (C99), volatile, const
John McCall0953e762009-09-24 19:53:00 +00001125 if (Quals.hasRestrict())
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001126 Out << 'r';
John McCall0953e762009-09-24 19:53:00 +00001127 if (Quals.hasVolatile())
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001128 Out << 'V';
John McCall0953e762009-09-24 19:53:00 +00001129 if (Quals.hasConst())
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001130 Out << 'K';
John McCall0953e762009-09-24 19:53:00 +00001131
Douglas Gregor56079f72010-06-14 23:15:08 +00001132 if (Quals.hasAddressSpace()) {
1133 // Extension:
1134 //
1135 // <type> ::= U <address-space-number>
1136 //
1137 // where <address-space-number> is a source name consisting of 'AS'
1138 // followed by the address space <number>.
1139 llvm::SmallString<64> ASString;
1140 ASString = "AS" + llvm::utostr_32(Quals.getAddressSpace());
1141 Out << 'U' << ASString.size() << ASString;
1142 }
1143
John McCall0953e762009-09-24 19:53:00 +00001144 // FIXME: For now, just drop all extension qualifiers on the floor.
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001145}
1146
Anders Carlsson7b06f6c2009-12-10 03:14:39 +00001147void CXXNameMangler::mangleObjCMethodName(const ObjCMethodDecl *MD) {
Charles Davis685b1d92010-05-26 18:25:27 +00001148 llvm::SmallString<64> Buffer;
1149 MiscNameMangler(Context, Buffer).mangleObjCMethodName(MD);
1150 Out << Buffer;
Anders Carlsson7b06f6c2009-12-10 03:14:39 +00001151}
1152
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001153void CXXNameMangler::mangleType(QualType T) {
Anders Carlsson4843e582009-03-10 17:07:44 +00001154 // Only operate on the canonical type!
Anders Carlssonb5404912009-10-07 01:06:45 +00001155 T = Context.getASTContext().getCanonicalType(T);
Anders Carlsson4843e582009-03-10 17:07:44 +00001156
Douglas Gregora4923eb2009-11-16 21:35:15 +00001157 bool IsSubstitutable = T.hasLocalQualifiers() || !isa<BuiltinType>(T);
Anders Carlsson76967372009-09-17 00:43:46 +00001158 if (IsSubstitutable && mangleSubstitution(T))
1159 return;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001160
Douglas Gregora4923eb2009-11-16 21:35:15 +00001161 if (Qualifiers Quals = T.getLocalQualifiers()) {
John McCall0953e762009-09-24 19:53:00 +00001162 mangleQualifiers(Quals);
1163 // Recurse: even if the qualified type isn't yet substitutable,
1164 // the unqualified type might be.
Douglas Gregora4923eb2009-11-16 21:35:15 +00001165 mangleType(T.getLocalUnqualifiedType());
Anders Carlsson76967372009-09-17 00:43:46 +00001166 } else {
1167 switch (T->getTypeClass()) {
John McCallefe6aee2009-09-05 07:56:18 +00001168#define ABSTRACT_TYPE(CLASS, PARENT)
1169#define NON_CANONICAL_TYPE(CLASS, PARENT) \
Anders Carlsson76967372009-09-17 00:43:46 +00001170 case Type::CLASS: \
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +00001171 llvm_unreachable("can't mangle non-canonical type " #CLASS "Type"); \
Anders Carlsson76967372009-09-17 00:43:46 +00001172 return;
John McCallefe6aee2009-09-05 07:56:18 +00001173#define TYPE(CLASS, PARENT) \
Anders Carlsson76967372009-09-17 00:43:46 +00001174 case Type::CLASS: \
John McCall0953e762009-09-24 19:53:00 +00001175 mangleType(static_cast<const CLASS##Type*>(T.getTypePtr())); \
Anders Carlsson76967372009-09-17 00:43:46 +00001176 break;
John McCallefe6aee2009-09-05 07:56:18 +00001177#include "clang/AST/TypeNodes.def"
Anders Carlsson76967372009-09-17 00:43:46 +00001178 }
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001179 }
Anders Carlsson76967372009-09-17 00:43:46 +00001180
1181 // Add the substitution.
1182 if (IsSubstitutable)
1183 addSubstitution(T);
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001184}
1185
Douglas Gregor1b12a3b2010-05-26 05:11:13 +00001186void CXXNameMangler::mangleNameOrStandardSubstitution(const NamedDecl *ND) {
1187 if (!mangleStandardSubstitution(ND))
1188 mangleName(ND);
1189}
1190
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001191void CXXNameMangler::mangleType(const BuiltinType *T) {
John McCallefe6aee2009-09-05 07:56:18 +00001192 // <type> ::= <builtin-type>
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001193 // <builtin-type> ::= v # void
1194 // ::= w # wchar_t
1195 // ::= b # bool
1196 // ::= c # char
1197 // ::= a # signed char
1198 // ::= h # unsigned char
1199 // ::= s # short
1200 // ::= t # unsigned short
1201 // ::= i # int
1202 // ::= j # unsigned int
1203 // ::= l # long
1204 // ::= m # unsigned long
1205 // ::= x # long long, __int64
1206 // ::= y # unsigned long long, __int64
1207 // ::= n # __int128
1208 // UNSUPPORTED: ::= o # unsigned __int128
1209 // ::= f # float
1210 // ::= d # double
1211 // ::= e # long double, __float80
1212 // UNSUPPORTED: ::= g # __float128
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001213 // UNSUPPORTED: ::= Dd # IEEE 754r decimal floating point (64 bits)
1214 // UNSUPPORTED: ::= De # IEEE 754r decimal floating point (128 bits)
1215 // UNSUPPORTED: ::= Df # IEEE 754r decimal floating point (32 bits)
1216 // UNSUPPORTED: ::= Dh # IEEE 754r half-precision floating point (16 bits)
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001217 // ::= Di # char32_t
1218 // ::= Ds # char16_t
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001219 // ::= u <source-name> # vendor extended type
Sebastian Redl6e8ed162009-05-10 18:38:11 +00001220 // From our point of view, std::nullptr_t is a builtin, but as far as mangling
1221 // is concerned, it's a type called std::nullptr_t.
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001222 switch (T->getKind()) {
1223 case BuiltinType::Void: Out << 'v'; break;
1224 case BuiltinType::Bool: Out << 'b'; break;
1225 case BuiltinType::Char_U: case BuiltinType::Char_S: Out << 'c'; break;
1226 case BuiltinType::UChar: Out << 'h'; break;
1227 case BuiltinType::UShort: Out << 't'; break;
1228 case BuiltinType::UInt: Out << 'j'; break;
1229 case BuiltinType::ULong: Out << 'm'; break;
1230 case BuiltinType::ULongLong: Out << 'y'; break;
Chris Lattner2df9ced2009-04-30 02:43:43 +00001231 case BuiltinType::UInt128: Out << 'o'; break;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001232 case BuiltinType::SChar: Out << 'a'; break;
1233 case BuiltinType::WChar: Out << 'w'; break;
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001234 case BuiltinType::Char16: Out << "Ds"; break;
1235 case BuiltinType::Char32: Out << "Di"; break;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001236 case BuiltinType::Short: Out << 's'; break;
1237 case BuiltinType::Int: Out << 'i'; break;
1238 case BuiltinType::Long: Out << 'l'; break;
1239 case BuiltinType::LongLong: Out << 'x'; break;
Chris Lattner2df9ced2009-04-30 02:43:43 +00001240 case BuiltinType::Int128: Out << 'n'; break;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001241 case BuiltinType::Float: Out << 'f'; break;
1242 case BuiltinType::Double: Out << 'd'; break;
1243 case BuiltinType::LongDouble: Out << 'e'; break;
Sebastian Redl6e8ed162009-05-10 18:38:11 +00001244 case BuiltinType::NullPtr: Out << "St9nullptr_t"; break;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001245
1246 case BuiltinType::Overload:
1247 case BuiltinType::Dependent:
Mike Stump1eb44332009-09-09 15:08:12 +00001248 assert(false &&
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001249 "Overloaded and dependent types shouldn't get to name mangling");
1250 break;
Anders Carlssone89d1592009-06-26 18:41:36 +00001251 case BuiltinType::UndeducedAuto:
1252 assert(0 && "Should not see undeduced auto here");
1253 break;
Steve Naroff9533a7f2009-07-22 17:14:51 +00001254 case BuiltinType::ObjCId: Out << "11objc_object"; break;
1255 case BuiltinType::ObjCClass: Out << "10objc_class"; break;
Fariborz Jahanian13dcd002009-11-21 19:53:08 +00001256 case BuiltinType::ObjCSel: Out << "13objc_selector"; break;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001257 }
1258}
1259
John McCallefe6aee2009-09-05 07:56:18 +00001260// <type> ::= <function-type>
1261// <function-type> ::= F [Y] <bare-function-type> E
1262void CXXNameMangler::mangleType(const FunctionProtoType *T) {
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001263 Out << 'F';
Mike Stumpf5408fe2009-05-16 07:57:57 +00001264 // FIXME: We don't have enough information in the AST to produce the 'Y'
1265 // encoding for extern "C" function types.
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001266 mangleBareFunctionType(T, /*MangleReturnType=*/true);
1267 Out << 'E';
1268}
John McCallefe6aee2009-09-05 07:56:18 +00001269void CXXNameMangler::mangleType(const FunctionNoProtoType *T) {
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +00001270 llvm_unreachable("Can't mangle K&R function prototypes");
John McCallefe6aee2009-09-05 07:56:18 +00001271}
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001272void CXXNameMangler::mangleBareFunctionType(const FunctionType *T,
1273 bool MangleReturnType) {
John McCallefe6aee2009-09-05 07:56:18 +00001274 // We should never be mangling something without a prototype.
1275 const FunctionProtoType *Proto = cast<FunctionProtoType>(T);
1276
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001277 // <bare-function-type> ::= <signature type>+
1278 if (MangleReturnType)
John McCallefe6aee2009-09-05 07:56:18 +00001279 mangleType(Proto->getResultType());
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001280
Anders Carlsson93296682010-06-02 04:40:13 +00001281 if (Proto->getNumArgs() == 0 && !Proto->isVariadic()) {
1282 // <builtin-type> ::= v # void
Anders Carlssonc6c91bc2009-04-01 00:15:23 +00001283 Out << 'v';
1284 return;
1285 }
Mike Stump1eb44332009-09-09 15:08:12 +00001286
Douglas Gregor72564e72009-02-26 23:50:07 +00001287 for (FunctionProtoType::arg_type_iterator Arg = Proto->arg_type_begin(),
Mike Stump1eb44332009-09-09 15:08:12 +00001288 ArgEnd = Proto->arg_type_end();
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001289 Arg != ArgEnd; ++Arg)
1290 mangleType(*Arg);
Douglas Gregor219cc612009-02-13 01:28:03 +00001291
1292 // <builtin-type> ::= z # ellipsis
1293 if (Proto->isVariadic())
1294 Out << 'z';
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001295}
1296
John McCallefe6aee2009-09-05 07:56:18 +00001297// <type> ::= <class-enum-type>
Mike Stump1eb44332009-09-09 15:08:12 +00001298// <class-enum-type> ::= <name>
John McCalled976492009-12-04 22:46:56 +00001299void CXXNameMangler::mangleType(const UnresolvedUsingType *T) {
1300 mangleName(T->getDecl());
1301}
1302
1303// <type> ::= <class-enum-type>
1304// <class-enum-type> ::= <name>
John McCallefe6aee2009-09-05 07:56:18 +00001305void CXXNameMangler::mangleType(const EnumType *T) {
1306 mangleType(static_cast<const TagType*>(T));
1307}
1308void CXXNameMangler::mangleType(const RecordType *T) {
1309 mangleType(static_cast<const TagType*>(T));
1310}
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001311void CXXNameMangler::mangleType(const TagType *T) {
Eli Friedmanecb7e932009-12-11 18:00:57 +00001312 mangleName(T->getDecl());
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001313}
1314
John McCallefe6aee2009-09-05 07:56:18 +00001315// <type> ::= <array-type>
1316// <array-type> ::= A <positive dimension number> _ <element type>
1317// ::= A [<dimension expression>] _ <element type>
1318void CXXNameMangler::mangleType(const ConstantArrayType *T) {
1319 Out << 'A' << T->getSize() << '_';
1320 mangleType(T->getElementType());
1321}
1322void CXXNameMangler::mangleType(const VariableArrayType *T) {
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001323 Out << 'A';
John McCallefe6aee2009-09-05 07:56:18 +00001324 mangleExpression(T->getSizeExpr());
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001325 Out << '_';
1326 mangleType(T->getElementType());
1327}
John McCallefe6aee2009-09-05 07:56:18 +00001328void CXXNameMangler::mangleType(const DependentSizedArrayType *T) {
1329 Out << 'A';
1330 mangleExpression(T->getSizeExpr());
1331 Out << '_';
1332 mangleType(T->getElementType());
1333}
1334void CXXNameMangler::mangleType(const IncompleteArrayType *T) {
1335 Out << 'A' << '_';
1336 mangleType(T->getElementType());
1337}
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001338
John McCallefe6aee2009-09-05 07:56:18 +00001339// <type> ::= <pointer-to-member-type>
1340// <pointer-to-member-type> ::= M <class type> <member type>
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001341void CXXNameMangler::mangleType(const MemberPointerType *T) {
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001342 Out << 'M';
1343 mangleType(QualType(T->getClass(), 0));
Anders Carlsson0e650012009-05-17 17:41:20 +00001344 QualType PointeeType = T->getPointeeType();
1345 if (const FunctionProtoType *FPT = dyn_cast<FunctionProtoType>(PointeeType)) {
John McCall0953e762009-09-24 19:53:00 +00001346 mangleQualifiers(Qualifiers::fromCVRMask(FPT->getTypeQuals()));
Anders Carlsson0e650012009-05-17 17:41:20 +00001347 mangleType(FPT);
Anders Carlsson9d85b722010-06-02 04:29:50 +00001348
1349 // Itanium C++ ABI 5.1.8:
1350 //
1351 // The type of a non-static member function is considered to be different,
1352 // for the purposes of substitution, from the type of a namespace-scope or
1353 // static member function whose type appears similar. The types of two
1354 // non-static member functions are considered to be different, for the
1355 // purposes of substitution, if the functions are members of different
1356 // classes. In other words, for the purposes of substitution, the class of
1357 // which the function is a member is considered part of the type of
1358 // function.
1359
1360 // We increment the SeqID here to emulate adding an entry to the
1361 // substitution table. We can't actually add it because we don't want this
1362 // particular function type to be substituted.
1363 ++SeqID;
Mike Stump1eb44332009-09-09 15:08:12 +00001364 } else
Anders Carlsson0e650012009-05-17 17:41:20 +00001365 mangleType(PointeeType);
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001366}
1367
John McCallefe6aee2009-09-05 07:56:18 +00001368// <type> ::= <template-param>
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001369void CXXNameMangler::mangleType(const TemplateTypeParmType *T) {
Anders Carlsson0ccdf8d2009-09-27 00:38:53 +00001370 mangleTemplateParameter(T->getIndex());
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001371}
1372
John McCallefe6aee2009-09-05 07:56:18 +00001373// <type> ::= P <type> # pointer-to
1374void CXXNameMangler::mangleType(const PointerType *T) {
1375 Out << 'P';
1376 mangleType(T->getPointeeType());
1377}
1378void CXXNameMangler::mangleType(const ObjCObjectPointerType *T) {
1379 Out << 'P';
1380 mangleType(T->getPointeeType());
1381}
1382
1383// <type> ::= R <type> # reference-to
1384void CXXNameMangler::mangleType(const LValueReferenceType *T) {
1385 Out << 'R';
1386 mangleType(T->getPointeeType());
1387}
1388
1389// <type> ::= O <type> # rvalue reference-to (C++0x)
1390void CXXNameMangler::mangleType(const RValueReferenceType *T) {
1391 Out << 'O';
1392 mangleType(T->getPointeeType());
1393}
1394
1395// <type> ::= C <type> # complex pair (C 2000)
1396void CXXNameMangler::mangleType(const ComplexType *T) {
1397 Out << 'C';
1398 mangleType(T->getElementType());
1399}
1400
1401// GNU extension: vector types
Chris Lattner788b0fd2010-06-23 06:00:24 +00001402// <type> ::= <vector-type>
1403// <vector-type> ::= Dv <positive dimension number> _
1404// <extended element type>
1405// ::= Dv [<dimension expression>] _ <element type>
1406// <extended element type> ::= <element type>
1407// ::= p # AltiVec vector pixel
John McCallefe6aee2009-09-05 07:56:18 +00001408void CXXNameMangler::mangleType(const VectorType *T) {
Nick Lewycky0e5f0672010-03-26 07:18:04 +00001409 Out << "Dv" << T->getNumElements() << '_';
Chris Lattner788b0fd2010-06-23 06:00:24 +00001410 if (T->getAltiVecSpecific() == VectorType::Pixel)
1411 Out << 'p';
1412 else if (T->getAltiVecSpecific() == VectorType::Bool)
1413 Out << 'b';
1414 else
1415 mangleType(T->getElementType());
John McCallefe6aee2009-09-05 07:56:18 +00001416}
1417void CXXNameMangler::mangleType(const ExtVectorType *T) {
1418 mangleType(static_cast<const VectorType*>(T));
1419}
1420void CXXNameMangler::mangleType(const DependentSizedExtVectorType *T) {
Nick Lewycky0e5f0672010-03-26 07:18:04 +00001421 Out << "Dv";
1422 mangleExpression(T->getSizeExpr());
1423 Out << '_';
John McCallefe6aee2009-09-05 07:56:18 +00001424 mangleType(T->getElementType());
1425}
1426
Anders Carlssona40c5e42009-03-07 22:03:21 +00001427void CXXNameMangler::mangleType(const ObjCInterfaceType *T) {
1428 mangleSourceName(T->getDecl()->getIdentifier());
1429}
1430
John McCallc12c5bb2010-05-15 11:32:37 +00001431void CXXNameMangler::mangleType(const ObjCObjectType *T) {
John McCallc00c1f62010-05-15 17:06:29 +00001432 // We don't allow overloading by different protocol qualification,
1433 // so mangling them isn't necessary.
John McCallc12c5bb2010-05-15 11:32:37 +00001434 mangleType(T->getBaseType());
1435}
1436
John McCallefe6aee2009-09-05 07:56:18 +00001437void CXXNameMangler::mangleType(const BlockPointerType *T) {
Anders Carlssonf28c6872009-12-23 22:31:44 +00001438 Out << "U13block_pointer";
1439 mangleType(T->getPointeeType());
John McCallefe6aee2009-09-05 07:56:18 +00001440}
1441
John McCall31f17ec2010-04-27 00:57:59 +00001442void CXXNameMangler::mangleType(const InjectedClassNameType *T) {
1443 // Mangle injected class name types as if the user had written the
1444 // specialization out fully. It may not actually be possible to see
1445 // this mangling, though.
1446 mangleType(T->getInjectedSpecializationType());
1447}
1448
John McCallefe6aee2009-09-05 07:56:18 +00001449void CXXNameMangler::mangleType(const TemplateSpecializationType *T) {
Douglas Gregor1e9268e2010-04-28 05:58:56 +00001450 if (TemplateDecl *TD = T->getTemplateName().getAsTemplateDecl()) {
1451 mangleName(TD, T->getArgs(), T->getNumArgs());
1452 } else {
1453 if (mangleSubstitution(QualType(T, 0)))
1454 return;
Sean Huntc3021132010-05-05 15:23:54 +00001455
Douglas Gregor1e9268e2010-04-28 05:58:56 +00001456 mangleTemplatePrefix(T->getTemplateName());
Sean Huntc3021132010-05-05 15:23:54 +00001457
Douglas Gregor1e9268e2010-04-28 05:58:56 +00001458 // FIXME: GCC does not appear to mangle the template arguments when
1459 // the template in question is a dependent template name. Should we
1460 // emulate that badness?
1461 mangleTemplateArgs(T->getTemplateName(), T->getArgs(), T->getNumArgs());
1462 addSubstitution(QualType(T, 0));
1463 }
John McCallefe6aee2009-09-05 07:56:18 +00001464}
1465
Douglas Gregor4714c122010-03-31 17:34:00 +00001466void CXXNameMangler::mangleType(const DependentNameType *T) {
Anders Carlssonae352482009-09-26 02:26:02 +00001467 // Typename types are always nested
1468 Out << 'N';
John McCall33500952010-06-11 00:33:02 +00001469 mangleUnresolvedScope(T->getQualifier());
1470 mangleSourceName(T->getIdentifier());
1471 Out << 'E';
1472}
John McCall6ab30e02010-06-09 07:26:17 +00001473
John McCall33500952010-06-11 00:33:02 +00001474void CXXNameMangler::mangleType(const DependentTemplateSpecializationType *T) {
1475 // Dependently-scoped template types are always nested
1476 Out << 'N';
1477
1478 // TODO: avoid making this TemplateName.
1479 TemplateName Prefix =
1480 getASTContext().getDependentTemplateName(T->getQualifier(),
1481 T->getIdentifier());
1482 mangleTemplatePrefix(Prefix);
1483
1484 // FIXME: GCC does not appear to mangle the template arguments when
1485 // the template in question is a dependent template name. Should we
1486 // emulate that badness?
1487 mangleTemplateArgs(Prefix, T->getArgs(), T->getNumArgs());
Anders Carlssonae352482009-09-26 02:26:02 +00001488 Out << 'E';
John McCallefe6aee2009-09-05 07:56:18 +00001489}
1490
John McCallad5e7382010-03-01 23:49:17 +00001491void CXXNameMangler::mangleType(const TypeOfType *T) {
1492 // FIXME: this is pretty unsatisfactory, but there isn't an obvious
1493 // "extension with parameters" mangling.
1494 Out << "u6typeof";
1495}
1496
1497void CXXNameMangler::mangleType(const TypeOfExprType *T) {
1498 // FIXME: this is pretty unsatisfactory, but there isn't an obvious
1499 // "extension with parameters" mangling.
1500 Out << "u6typeof";
1501}
1502
1503void CXXNameMangler::mangleType(const DecltypeType *T) {
1504 Expr *E = T->getUnderlyingExpr();
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001505
John McCallad5e7382010-03-01 23:49:17 +00001506 // type ::= Dt <expression> E # decltype of an id-expression
1507 // # or class member access
1508 // ::= DT <expression> E # decltype of an expression
1509
1510 // This purports to be an exhaustive list of id-expressions and
1511 // class member accesses. Note that we do not ignore parentheses;
1512 // parentheses change the semantics of decltype for these
1513 // expressions (and cause the mangler to use the other form).
1514 if (isa<DeclRefExpr>(E) ||
1515 isa<MemberExpr>(E) ||
1516 isa<UnresolvedLookupExpr>(E) ||
1517 isa<DependentScopeDeclRefExpr>(E) ||
1518 isa<CXXDependentScopeMemberExpr>(E) ||
1519 isa<UnresolvedMemberExpr>(E))
1520 Out << "Dt";
1521 else
1522 Out << "DT";
1523 mangleExpression(E);
1524 Out << 'E';
1525}
1526
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001527void CXXNameMangler::mangleIntegerLiteral(QualType T,
Anders Carlssone170ba72009-12-14 01:45:37 +00001528 const llvm::APSInt &Value) {
1529 // <expr-primary> ::= L <type> <value number> E # integer literal
1530 Out << 'L';
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001531
Anders Carlssone170ba72009-12-14 01:45:37 +00001532 mangleType(T);
1533 if (T->isBooleanType()) {
1534 // Boolean values are encoded as 0/1.
1535 Out << (Value.getBoolValue() ? '1' : '0');
1536 } else {
John McCall0512e482010-07-14 04:20:34 +00001537 mangleNumber(Value);
Anders Carlssone170ba72009-12-14 01:45:37 +00001538 }
1539 Out << 'E';
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001540
Anders Carlssone170ba72009-12-14 01:45:37 +00001541}
1542
John McCall2f27bf82010-02-04 02:56:29 +00001543/// Mangles a member expression. Implicit accesses are not handled,
1544/// but that should be okay, because you shouldn't be able to
1545/// make an implicit access in a function template declaration.
John McCall2f27bf82010-02-04 02:56:29 +00001546void CXXNameMangler::mangleMemberExpr(const Expr *Base,
1547 bool IsArrow,
1548 NestedNameSpecifier *Qualifier,
1549 DeclarationName Member,
1550 unsigned Arity) {
John McCalle1e342f2010-03-01 19:12:25 +00001551 // gcc-4.4 uses 'dt' for dot expressions, which is reasonable.
1552 // OTOH, gcc also mangles the name as an expression.
1553 Out << (IsArrow ? "pt" : "dt");
John McCall2f27bf82010-02-04 02:56:29 +00001554 mangleExpression(Base);
1555 mangleUnresolvedName(Qualifier, Member, Arity);
1556}
1557
John McCall5e1e89b2010-08-18 19:18:59 +00001558void CXXNameMangler::mangleExpression(const Expr *E, unsigned Arity) {
Anders Carlssond553f8c2009-09-21 01:21:10 +00001559 // <expression> ::= <unary operator-name> <expression>
John McCall09cc1412010-02-03 00:55:45 +00001560 // ::= <binary operator-name> <expression> <expression>
1561 // ::= <trinary operator-name> <expression> <expression> <expression>
1562 // ::= cl <expression>* E # call
Anders Carlssond553f8c2009-09-21 01:21:10 +00001563 // ::= cv <type> expression # conversion with one argument
1564 // ::= cv <type> _ <expression>* E # conversion with a different number of arguments
John McCall09cc1412010-02-03 00:55:45 +00001565 // ::= st <type> # sizeof (a type)
Anders Carlssond553f8c2009-09-21 01:21:10 +00001566 // ::= at <type> # alignof (a type)
1567 // ::= <template-param>
1568 // ::= <function-param>
1569 // ::= sr <type> <unqualified-name> # dependent name
1570 // ::= sr <type> <unqualified-name> <template-args> # dependent template-id
1571 // ::= sZ <template-param> # size of a parameter pack
John McCall09cc1412010-02-03 00:55:45 +00001572 // ::= <expr-primary>
John McCall1dd73832010-02-04 01:42:13 +00001573 // <expr-primary> ::= L <type> <value number> E # integer literal
1574 // ::= L <type <value float> E # floating literal
1575 // ::= L <mangled-name> E # external name
Anders Carlssond553f8c2009-09-21 01:21:10 +00001576 switch (E->getStmtClass()) {
John McCall6ae1f352010-04-09 22:26:14 +00001577 case Expr::NoStmtClass:
1578#define EXPR(Type, Base)
1579#define STMT(Type, Base) \
1580 case Expr::Type##Class:
Sean Hunt4bfe1962010-05-05 15:24:00 +00001581#include "clang/AST/StmtNodes.inc"
John McCall0512e482010-07-14 04:20:34 +00001582 // fallthrough
1583
1584 // These all can only appear in local or variable-initialization
1585 // contexts and so should never appear in a mangling.
1586 case Expr::AddrLabelExprClass:
1587 case Expr::BlockDeclRefExprClass:
1588 case Expr::CXXThisExprClass:
1589 case Expr::DesignatedInitExprClass:
1590 case Expr::ImplicitValueInitExprClass:
1591 case Expr::InitListExprClass:
1592 case Expr::ParenListExprClass:
1593 case Expr::CXXScalarValueInitExprClass:
John McCall09cc1412010-02-03 00:55:45 +00001594 llvm_unreachable("unexpected statement kind");
1595 break;
1596
John McCall0512e482010-07-14 04:20:34 +00001597 // FIXME: invent manglings for all these.
1598 case Expr::BlockExprClass:
1599 case Expr::CXXPseudoDestructorExprClass:
1600 case Expr::ChooseExprClass:
1601 case Expr::CompoundLiteralExprClass:
1602 case Expr::ExtVectorElementExprClass:
1603 case Expr::ObjCEncodeExprClass:
1604 case Expr::ObjCImplicitSetterGetterRefExprClass:
1605 case Expr::ObjCIsaExprClass:
1606 case Expr::ObjCIvarRefExprClass:
1607 case Expr::ObjCMessageExprClass:
1608 case Expr::ObjCPropertyRefExprClass:
1609 case Expr::ObjCProtocolExprClass:
1610 case Expr::ObjCSelectorExprClass:
1611 case Expr::ObjCStringLiteralClass:
1612 case Expr::ObjCSuperExprClass:
1613 case Expr::OffsetOfExprClass:
1614 case Expr::PredefinedExprClass:
1615 case Expr::ShuffleVectorExprClass:
1616 case Expr::StmtExprClass:
1617 case Expr::TypesCompatibleExprClass:
1618 case Expr::UnaryTypeTraitExprClass:
1619 case Expr::VAArgExprClass: {
John McCall6ae1f352010-04-09 22:26:14 +00001620 // As bad as this diagnostic is, it's better than crashing.
1621 Diagnostic &Diags = Context.getDiags();
1622 unsigned DiagID = Diags.getCustomDiagID(Diagnostic::Error,
1623 "cannot yet mangle expression type %0");
John McCall739bf092010-04-10 09:39:25 +00001624 Diags.Report(FullSourceLoc(E->getExprLoc(),
1625 getASTContext().getSourceManager()),
1626 DiagID)
1627 << E->getStmtClassName() << E->getSourceRange();
John McCall6ae1f352010-04-09 22:26:14 +00001628 break;
1629 }
1630
John McCall0512e482010-07-14 04:20:34 +00001631 case Expr::CXXDefaultArgExprClass:
John McCall5e1e89b2010-08-18 19:18:59 +00001632 mangleExpression(cast<CXXDefaultArgExpr>(E)->getExpr(), Arity);
John McCall0512e482010-07-14 04:20:34 +00001633 break;
1634
1635 case Expr::CXXMemberCallExprClass: // fallthrough
John McCall1dd73832010-02-04 01:42:13 +00001636 case Expr::CallExprClass: {
1637 const CallExpr *CE = cast<CallExpr>(E);
1638 Out << "cl";
John McCall5e1e89b2010-08-18 19:18:59 +00001639 mangleExpression(CE->getCallee(), CE->getNumArgs());
John McCall1dd73832010-02-04 01:42:13 +00001640 for (unsigned I = 0, N = CE->getNumArgs(); I != N; ++I)
1641 mangleExpression(CE->getArg(I));
Benjamin Kramer35f59b62010-04-10 16:03:31 +00001642 Out << 'E';
John McCall09cc1412010-02-03 00:55:45 +00001643 break;
John McCall1dd73832010-02-04 01:42:13 +00001644 }
John McCall09cc1412010-02-03 00:55:45 +00001645
John McCall0512e482010-07-14 04:20:34 +00001646 case Expr::CXXNewExprClass: {
1647 // Proposal from David Vandervoorde, 2010.06.30
1648 const CXXNewExpr *New = cast<CXXNewExpr>(E);
1649 if (New->isGlobalNew()) Out << "gs";
1650 Out << (New->isArray() ? "na" : "nw");
1651 for (CXXNewExpr::const_arg_iterator I = New->placement_arg_begin(),
1652 E = New->placement_arg_end(); I != E; ++I)
1653 mangleExpression(*I);
1654 Out << '_';
1655 mangleType(New->getAllocatedType());
1656 if (New->hasInitializer()) {
1657 Out << "pi";
1658 for (CXXNewExpr::const_arg_iterator I = New->constructor_arg_begin(),
1659 E = New->constructor_arg_end(); I != E; ++I)
1660 mangleExpression(*I);
1661 }
1662 Out << 'E';
1663 break;
1664 }
1665
John McCall2f27bf82010-02-04 02:56:29 +00001666 case Expr::MemberExprClass: {
1667 const MemberExpr *ME = cast<MemberExpr>(E);
1668 mangleMemberExpr(ME->getBase(), ME->isArrow(),
1669 ME->getQualifier(), ME->getMemberDecl()->getDeclName(),
John McCall5e1e89b2010-08-18 19:18:59 +00001670 Arity);
John McCall2f27bf82010-02-04 02:56:29 +00001671 break;
1672 }
1673
1674 case Expr::UnresolvedMemberExprClass: {
1675 const UnresolvedMemberExpr *ME = cast<UnresolvedMemberExpr>(E);
1676 mangleMemberExpr(ME->getBase(), ME->isArrow(),
1677 ME->getQualifier(), ME->getMemberName(),
John McCall5e1e89b2010-08-18 19:18:59 +00001678 Arity);
John McCall2f27bf82010-02-04 02:56:29 +00001679 break;
1680 }
1681
1682 case Expr::CXXDependentScopeMemberExprClass: {
1683 const CXXDependentScopeMemberExpr *ME
1684 = cast<CXXDependentScopeMemberExpr>(E);
1685 mangleMemberExpr(ME->getBase(), ME->isArrow(),
1686 ME->getQualifier(), ME->getMember(),
John McCall5e1e89b2010-08-18 19:18:59 +00001687 Arity);
John McCall2f27bf82010-02-04 02:56:29 +00001688 break;
1689 }
1690
John McCall1dd73832010-02-04 01:42:13 +00001691 case Expr::UnresolvedLookupExprClass: {
John McCalla3218e72010-02-04 01:48:38 +00001692 // The ABI doesn't cover how to mangle overload sets, so we mangle
1693 // using something as close as possible to the original lookup
1694 // expression.
John McCall1dd73832010-02-04 01:42:13 +00001695 const UnresolvedLookupExpr *ULE = cast<UnresolvedLookupExpr>(E);
John McCall5e1e89b2010-08-18 19:18:59 +00001696 mangleUnresolvedName(ULE->getQualifier(), ULE->getName(), Arity);
John McCall1dd73832010-02-04 01:42:13 +00001697 break;
1698 }
1699
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001700 case Expr::CXXUnresolvedConstructExprClass: {
John McCall1dd73832010-02-04 01:42:13 +00001701 const CXXUnresolvedConstructExpr *CE = cast<CXXUnresolvedConstructExpr>(E);
1702 unsigned N = CE->arg_size();
1703
1704 Out << "cv";
1705 mangleType(CE->getType());
Benjamin Kramer35f59b62010-04-10 16:03:31 +00001706 if (N != 1) Out << '_';
John McCall1dd73832010-02-04 01:42:13 +00001707 for (unsigned I = 0; I != N; ++I) mangleExpression(CE->getArg(I));
Benjamin Kramer35f59b62010-04-10 16:03:31 +00001708 if (N != 1) Out << 'E';
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001709 break;
John McCall1dd73832010-02-04 01:42:13 +00001710 }
John McCall09cc1412010-02-03 00:55:45 +00001711
John McCall1dd73832010-02-04 01:42:13 +00001712 case Expr::CXXTemporaryObjectExprClass:
1713 case Expr::CXXConstructExprClass: {
1714 const CXXConstructExpr *CE = cast<CXXConstructExpr>(E);
1715 unsigned N = CE->getNumArgs();
1716
1717 Out << "cv";
1718 mangleType(CE->getType());
Benjamin Kramer35f59b62010-04-10 16:03:31 +00001719 if (N != 1) Out << '_';
John McCall1dd73832010-02-04 01:42:13 +00001720 for (unsigned I = 0; I != N; ++I) mangleExpression(CE->getArg(I));
Benjamin Kramer35f59b62010-04-10 16:03:31 +00001721 if (N != 1) Out << 'E';
John McCall09cc1412010-02-03 00:55:45 +00001722 break;
John McCall1dd73832010-02-04 01:42:13 +00001723 }
1724
1725 case Expr::SizeOfAlignOfExprClass: {
1726 const SizeOfAlignOfExpr *SAE = cast<SizeOfAlignOfExpr>(E);
Benjamin Kramer35f59b62010-04-10 16:03:31 +00001727 if (SAE->isSizeOf()) Out << 's';
1728 else Out << 'a';
John McCall1dd73832010-02-04 01:42:13 +00001729 if (SAE->isArgumentType()) {
Benjamin Kramer35f59b62010-04-10 16:03:31 +00001730 Out << 't';
John McCall1dd73832010-02-04 01:42:13 +00001731 mangleType(SAE->getArgumentType());
1732 } else {
Benjamin Kramer35f59b62010-04-10 16:03:31 +00001733 Out << 'z';
John McCall1dd73832010-02-04 01:42:13 +00001734 mangleExpression(SAE->getArgumentExpr());
1735 }
1736 break;
1737 }
Anders Carlssona7694082009-11-06 02:50:19 +00001738
John McCall0512e482010-07-14 04:20:34 +00001739 case Expr::CXXThrowExprClass: {
1740 const CXXThrowExpr *TE = cast<CXXThrowExpr>(E);
1741
1742 // Proposal from David Vandervoorde, 2010.06.30
1743 if (TE->getSubExpr()) {
1744 Out << "tw";
1745 mangleExpression(TE->getSubExpr());
1746 } else {
1747 Out << "tr";
1748 }
1749 break;
1750 }
1751
1752 case Expr::CXXTypeidExprClass: {
1753 const CXXTypeidExpr *TIE = cast<CXXTypeidExpr>(E);
1754
1755 // Proposal from David Vandervoorde, 2010.06.30
1756 if (TIE->isTypeOperand()) {
1757 Out << "ti";
1758 mangleType(TIE->getTypeOperand());
1759 } else {
1760 Out << "te";
1761 mangleExpression(TIE->getExprOperand());
1762 }
1763 break;
1764 }
1765
1766 case Expr::CXXDeleteExprClass: {
1767 const CXXDeleteExpr *DE = cast<CXXDeleteExpr>(E);
1768
1769 // Proposal from David Vandervoorde, 2010.06.30
1770 if (DE->isGlobalDelete()) Out << "gs";
1771 Out << (DE->isArrayForm() ? "da" : "dl");
1772 mangleExpression(DE->getArgument());
1773 break;
1774 }
1775
Anders Carlssone170ba72009-12-14 01:45:37 +00001776 case Expr::UnaryOperatorClass: {
1777 const UnaryOperator *UO = cast<UnaryOperator>(E);
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001778 mangleOperatorName(UnaryOperator::getOverloadedOperator(UO->getOpcode()),
Anders Carlssone170ba72009-12-14 01:45:37 +00001779 /*Arity=*/1);
1780 mangleExpression(UO->getSubExpr());
1781 break;
1782 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001783
John McCall0512e482010-07-14 04:20:34 +00001784 case Expr::ArraySubscriptExprClass: {
1785 const ArraySubscriptExpr *AE = cast<ArraySubscriptExpr>(E);
1786
1787 // Array subscript is treated as a syntactically wierd form of
1788 // binary operator.
1789 Out << "ix";
1790 mangleExpression(AE->getLHS());
1791 mangleExpression(AE->getRHS());
1792 break;
1793 }
1794
1795 case Expr::CompoundAssignOperatorClass: // fallthrough
Anders Carlssone170ba72009-12-14 01:45:37 +00001796 case Expr::BinaryOperatorClass: {
1797 const BinaryOperator *BO = cast<BinaryOperator>(E);
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001798 mangleOperatorName(BinaryOperator::getOverloadedOperator(BO->getOpcode()),
Anders Carlssone170ba72009-12-14 01:45:37 +00001799 /*Arity=*/2);
1800 mangleExpression(BO->getLHS());
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001801 mangleExpression(BO->getRHS());
Anders Carlssone170ba72009-12-14 01:45:37 +00001802 break;
John McCall2f27bf82010-02-04 02:56:29 +00001803 }
Anders Carlssone170ba72009-12-14 01:45:37 +00001804
1805 case Expr::ConditionalOperatorClass: {
1806 const ConditionalOperator *CO = cast<ConditionalOperator>(E);
1807 mangleOperatorName(OO_Conditional, /*Arity=*/3);
1808 mangleExpression(CO->getCond());
John McCall5e1e89b2010-08-18 19:18:59 +00001809 mangleExpression(CO->getLHS(), Arity);
1810 mangleExpression(CO->getRHS(), Arity);
Anders Carlssone170ba72009-12-14 01:45:37 +00001811 break;
1812 }
1813
Douglas Gregor46287c72010-01-29 16:37:09 +00001814 case Expr::ImplicitCastExprClass: {
John McCall5e1e89b2010-08-18 19:18:59 +00001815 mangleExpression(cast<ImplicitCastExpr>(E)->getSubExpr(), Arity);
Douglas Gregor46287c72010-01-29 16:37:09 +00001816 break;
1817 }
1818
1819 case Expr::CStyleCastExprClass:
1820 case Expr::CXXStaticCastExprClass:
1821 case Expr::CXXDynamicCastExprClass:
1822 case Expr::CXXReinterpretCastExprClass:
1823 case Expr::CXXConstCastExprClass:
1824 case Expr::CXXFunctionalCastExprClass: {
1825 const ExplicitCastExpr *ECE = cast<ExplicitCastExpr>(E);
1826 Out << "cv";
1827 mangleType(ECE->getType());
1828 mangleExpression(ECE->getSubExpr());
1829 break;
1830 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001831
Anders Carlsson58040a52009-12-16 05:48:46 +00001832 case Expr::CXXOperatorCallExprClass: {
1833 const CXXOperatorCallExpr *CE = cast<CXXOperatorCallExpr>(E);
1834 unsigned NumArgs = CE->getNumArgs();
1835 mangleOperatorName(CE->getOperator(), /*Arity=*/NumArgs);
1836 // Mangle the arguments.
1837 for (unsigned i = 0; i != NumArgs; ++i)
1838 mangleExpression(CE->getArg(i));
1839 break;
1840 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001841
Anders Carlssona7694082009-11-06 02:50:19 +00001842 case Expr::ParenExprClass:
John McCall5e1e89b2010-08-18 19:18:59 +00001843 mangleExpression(cast<ParenExpr>(E)->getSubExpr(), Arity);
Anders Carlssona7694082009-11-06 02:50:19 +00001844 break;
1845
Anders Carlssond553f8c2009-09-21 01:21:10 +00001846 case Expr::DeclRefExprClass: {
Douglas Gregor5ed1bc32010-02-28 21:40:32 +00001847 const NamedDecl *D = cast<DeclRefExpr>(E)->getDecl();
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00001848
Anders Carlssond553f8c2009-09-21 01:21:10 +00001849 switch (D->getKind()) {
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001850 default:
Douglas Gregor5ed1bc32010-02-28 21:40:32 +00001851 // <expr-primary> ::= L <mangled-name> E # external name
1852 Out << 'L';
1853 mangle(D, "_Z");
1854 Out << 'E';
1855 break;
1856
John McCall3dc7e7b2010-07-24 01:17:35 +00001857 case Decl::EnumConstant: {
1858 const EnumConstantDecl *ED = cast<EnumConstantDecl>(D);
1859 mangleIntegerLiteral(ED->getType(), ED->getInitVal());
1860 break;
1861 }
1862
Anders Carlssond553f8c2009-09-21 01:21:10 +00001863 case Decl::NonTypeTemplateParm: {
1864 const NonTypeTemplateParmDecl *PD = cast<NonTypeTemplateParmDecl>(D);
Anders Carlsson0ccdf8d2009-09-27 00:38:53 +00001865 mangleTemplateParameter(PD->getIndex());
Anders Carlssond553f8c2009-09-21 01:21:10 +00001866 break;
1867 }
1868
1869 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00001870
Anders Carlsson50755b02009-09-27 20:11:34 +00001871 break;
Anders Carlssond553f8c2009-09-21 01:21:10 +00001872 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00001873
John McCall865d4472009-11-19 22:55:06 +00001874 case Expr::DependentScopeDeclRefExprClass: {
1875 const DependentScopeDeclRefExpr *DRE = cast<DependentScopeDeclRefExpr>(E);
Douglas Gregor4b2ccfc2010-02-28 22:05:49 +00001876 NestedNameSpecifier *NNS = DRE->getQualifier();
1877 const Type *QTy = NNS->getAsType();
1878
1879 // When we're dealing with a nested-name-specifier that has just a
1880 // dependent identifier in it, mangle that as a typename. FIXME:
1881 // It isn't clear that we ever actually want to have such a
1882 // nested-name-specifier; why not just represent it as a typename type?
1883 if (!QTy && NNS->getAsIdentifier() && NNS->getPrefix()) {
Douglas Gregor4a2023f2010-03-31 20:19:30 +00001884 QTy = getASTContext().getDependentNameType(ETK_Typename,
1885 NNS->getPrefix(),
1886 NNS->getAsIdentifier())
Douglas Gregor4b2ccfc2010-02-28 22:05:49 +00001887 .getTypePtr();
1888 }
Anders Carlsson50755b02009-09-27 20:11:34 +00001889 assert(QTy && "Qualifier was not type!");
1890
1891 // ::= sr <type> <unqualified-name> # dependent name
1892 Out << "sr";
1893 mangleType(QualType(QTy, 0));
John McCall5e1e89b2010-08-18 19:18:59 +00001894 mangleUnqualifiedName(0, DRE->getDeclName(), Arity);
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00001895
Anders Carlsson50755b02009-09-27 20:11:34 +00001896 break;
1897 }
1898
John McCalld9307602010-04-09 22:54:09 +00001899 case Expr::CXXBindReferenceExprClass:
1900 mangleExpression(cast<CXXBindReferenceExpr>(E)->getSubExpr());
1901 break;
1902
1903 case Expr::CXXBindTemporaryExprClass:
1904 mangleExpression(cast<CXXBindTemporaryExpr>(E)->getSubExpr());
1905 break;
1906
1907 case Expr::CXXExprWithTemporariesClass:
John McCall5e1e89b2010-08-18 19:18:59 +00001908 mangleExpression(cast<CXXExprWithTemporaries>(E)->getSubExpr(), Arity);
John McCalld9307602010-04-09 22:54:09 +00001909 break;
1910
John McCall1dd73832010-02-04 01:42:13 +00001911 case Expr::FloatingLiteralClass: {
1912 const FloatingLiteral *FL = cast<FloatingLiteral>(E);
Benjamin Kramer35f59b62010-04-10 16:03:31 +00001913 Out << 'L';
John McCall1dd73832010-02-04 01:42:13 +00001914 mangleType(FL->getType());
John McCall0512e482010-07-14 04:20:34 +00001915 mangleFloat(FL->getValue());
Benjamin Kramer35f59b62010-04-10 16:03:31 +00001916 Out << 'E';
John McCall1dd73832010-02-04 01:42:13 +00001917 break;
1918 }
1919
John McCallde810632010-04-09 21:48:08 +00001920 case Expr::CharacterLiteralClass:
Benjamin Kramer35f59b62010-04-10 16:03:31 +00001921 Out << 'L';
John McCallde810632010-04-09 21:48:08 +00001922 mangleType(E->getType());
1923 Out << cast<CharacterLiteral>(E)->getValue();
1924 Out << 'E';
1925 break;
1926
1927 case Expr::CXXBoolLiteralExprClass:
1928 Out << "Lb";
1929 Out << (cast<CXXBoolLiteralExpr>(E)->getValue() ? '1' : '0');
1930 Out << 'E';
1931 break;
1932
John McCall0512e482010-07-14 04:20:34 +00001933 case Expr::IntegerLiteralClass: {
1934 llvm::APSInt Value(cast<IntegerLiteral>(E)->getValue());
1935 if (E->getType()->isSignedIntegerType())
1936 Value.setIsSigned(true);
1937 mangleIntegerLiteral(E->getType(), Value);
Anders Carlssone170ba72009-12-14 01:45:37 +00001938 break;
John McCall0512e482010-07-14 04:20:34 +00001939 }
1940
1941 case Expr::ImaginaryLiteralClass: {
1942 const ImaginaryLiteral *IE = cast<ImaginaryLiteral>(E);
1943 // Mangle as if a complex literal.
1944 // Proposal from David Vandervoorde, 2010.06.30.
1945 Out << 'L';
1946 mangleType(E->getType());
1947 if (const FloatingLiteral *Imag =
1948 dyn_cast<FloatingLiteral>(IE->getSubExpr())) {
1949 // Mangle a floating-point zero of the appropriate type.
1950 mangleFloat(llvm::APFloat(Imag->getValue().getSemantics()));
1951 Out << '_';
1952 mangleFloat(Imag->getValue());
1953 } else {
1954 Out << '0' << '_';
1955 llvm::APSInt Value(cast<IntegerLiteral>(IE->getSubExpr())->getValue());
1956 if (IE->getSubExpr()->getType()->isSignedIntegerType())
1957 Value.setIsSigned(true);
1958 mangleNumber(Value);
1959 }
1960 Out << 'E';
1961 break;
1962 }
1963
1964 case Expr::StringLiteralClass: {
John McCall1658c392010-07-15 21:53:03 +00001965 // Revised proposal from David Vandervoorde, 2010.07.15.
John McCall0512e482010-07-14 04:20:34 +00001966 Out << 'L';
John McCall1658c392010-07-15 21:53:03 +00001967 assert(isa<ConstantArrayType>(E->getType()));
1968 mangleType(E->getType());
John McCall0512e482010-07-14 04:20:34 +00001969 Out << 'E';
1970 break;
1971 }
1972
1973 case Expr::GNUNullExprClass:
1974 // FIXME: should this really be mangled the same as nullptr?
1975 // fallthrough
1976
1977 case Expr::CXXNullPtrLiteralExprClass: {
1978 // Proposal from David Vandervoorde, 2010.06.30, as
1979 // modified by ABI list discussion.
1980 Out << "LDnE";
1981 break;
1982 }
Anders Carlssone170ba72009-12-14 01:45:37 +00001983
Anders Carlssond553f8c2009-09-21 01:21:10 +00001984 }
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001985}
1986
Anders Carlsson3ac86b52009-04-15 05:36:58 +00001987void CXXNameMangler::mangleCXXCtorType(CXXCtorType T) {
1988 // <ctor-dtor-name> ::= C1 # complete object constructor
1989 // ::= C2 # base object constructor
1990 // ::= C3 # complete object allocating constructor
1991 //
1992 switch (T) {
1993 case Ctor_Complete:
1994 Out << "C1";
1995 break;
1996 case Ctor_Base:
1997 Out << "C2";
1998 break;
1999 case Ctor_CompleteAllocating:
2000 Out << "C3";
2001 break;
2002 }
2003}
2004
Anders Carlsson27ae5362009-04-17 01:58:57 +00002005void CXXNameMangler::mangleCXXDtorType(CXXDtorType T) {
2006 // <ctor-dtor-name> ::= D0 # deleting destructor
2007 // ::= D1 # complete object destructor
2008 // ::= D2 # base object destructor
2009 //
2010 switch (T) {
2011 case Dtor_Deleting:
2012 Out << "D0";
2013 break;
2014 case Dtor_Complete:
2015 Out << "D1";
2016 break;
2017 case Dtor_Base:
2018 Out << "D2";
2019 break;
2020 }
2021}
2022
Douglas Gregor20f0cc72010-04-23 03:10:43 +00002023void CXXNameMangler::mangleTemplateArgs(TemplateName Template,
2024 const TemplateArgument *TemplateArgs,
2025 unsigned NumTemplateArgs) {
2026 if (TemplateDecl *TD = Template.getAsTemplateDecl())
2027 return mangleTemplateArgs(*TD->getTemplateParameters(), TemplateArgs,
2028 NumTemplateArgs);
Sean Huntc3021132010-05-05 15:23:54 +00002029
Douglas Gregor20f0cc72010-04-23 03:10:43 +00002030 // <template-args> ::= I <template-arg>+ E
2031 Out << 'I';
2032 for (unsigned i = 0; i != NumTemplateArgs; ++i)
2033 mangleTemplateArg(0, TemplateArgs[i]);
2034 Out << 'E';
2035}
2036
Rafael Espindolad9800722010-03-11 14:07:00 +00002037void CXXNameMangler::mangleTemplateArgs(const TemplateParameterList &PL,
2038 const TemplateArgumentList &AL) {
Anders Carlsson7a0ba872009-05-15 16:09:15 +00002039 // <template-args> ::= I <template-arg>+ E
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002040 Out << 'I';
Rafael Espindolad9800722010-03-11 14:07:00 +00002041 for (unsigned i = 0, e = AL.size(); i != e; ++i)
2042 mangleTemplateArg(PL.getParam(i), AL[i]);
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002043 Out << 'E';
Anders Carlsson7a0ba872009-05-15 16:09:15 +00002044}
2045
Rafael Espindolad9800722010-03-11 14:07:00 +00002046void CXXNameMangler::mangleTemplateArgs(const TemplateParameterList &PL,
2047 const TemplateArgument *TemplateArgs,
Anders Carlsson7624f212009-09-18 02:42:01 +00002048 unsigned NumTemplateArgs) {
2049 // <template-args> ::= I <template-arg>+ E
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002050 Out << 'I';
Daniel Dunbar7e0c1952009-11-21 09:17:15 +00002051 for (unsigned i = 0; i != NumTemplateArgs; ++i)
Rafael Espindolad9800722010-03-11 14:07:00 +00002052 mangleTemplateArg(PL.getParam(i), TemplateArgs[i]);
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002053 Out << 'E';
Anders Carlsson7624f212009-09-18 02:42:01 +00002054}
2055
Rafael Espindolad9800722010-03-11 14:07:00 +00002056void CXXNameMangler::mangleTemplateArg(const NamedDecl *P,
2057 const TemplateArgument &A) {
Mike Stump1eb44332009-09-09 15:08:12 +00002058 // <template-arg> ::= <type> # type or template
Anders Carlsson7a0ba872009-05-15 16:09:15 +00002059 // ::= X <expression> E # expression
2060 // ::= <expr-primary> # simple expressions
2061 // ::= I <template-arg>* E # argument pack
2062 // ::= sp <expression> # pack expansion of (C++0x)
2063 switch (A.getKind()) {
2064 default:
2065 assert(0 && "Unknown template argument kind!");
2066 case TemplateArgument::Type:
2067 mangleType(A.getAsType());
2068 break;
Anders Carlsson9e85c742009-12-23 19:30:55 +00002069 case TemplateArgument::Template:
John McCallb6f532e2010-07-14 06:43:17 +00002070 // This is mangled as <type>.
2071 mangleType(A.getAsTemplate());
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002072 break;
Anders Carlssond553f8c2009-09-21 01:21:10 +00002073 case TemplateArgument::Expression:
2074 Out << 'X';
2075 mangleExpression(A.getAsExpr());
2076 Out << 'E';
2077 break;
Anders Carlssone170ba72009-12-14 01:45:37 +00002078 case TemplateArgument::Integral:
2079 mangleIntegerLiteral(A.getIntegralType(), *A.getAsIntegral());
Anders Carlsson7a0ba872009-05-15 16:09:15 +00002080 break;
Daniel Dunbar7e0c1952009-11-21 09:17:15 +00002081 case TemplateArgument::Declaration: {
Douglas Gregor20f0cc72010-04-23 03:10:43 +00002082 assert(P && "Missing template parameter for declaration argument");
Daniel Dunbar7e0c1952009-11-21 09:17:15 +00002083 // <expr-primary> ::= L <mangled-name> E # external name
2084
Rafael Espindolad9800722010-03-11 14:07:00 +00002085 // Clang produces AST's where pointer-to-member-function expressions
Daniel Dunbar7e0c1952009-11-21 09:17:15 +00002086 // and pointer-to-function expressions are represented as a declaration not
Rafael Espindolad9800722010-03-11 14:07:00 +00002087 // an expression. We compensate for it here to produce the correct mangling.
2088 NamedDecl *D = cast<NamedDecl>(A.getAsDecl());
2089 const NonTypeTemplateParmDecl *Parameter = cast<NonTypeTemplateParmDecl>(P);
2090 bool compensateMangling = D->isCXXClassMember() &&
2091 !Parameter->getType()->isReferenceType();
2092 if (compensateMangling) {
2093 Out << 'X';
2094 mangleOperatorName(OO_Amp, 1);
2095 }
2096
Daniel Dunbar7e0c1952009-11-21 09:17:15 +00002097 Out << 'L';
2098 // References to external entities use the mangled name; if the name would
2099 // not normally be manged then mangle it as unqualified.
2100 //
2101 // FIXME: The ABI specifies that external names here should have _Z, but
2102 // gcc leaves this off.
Rafael Espindolad9800722010-03-11 14:07:00 +00002103 if (compensateMangling)
2104 mangle(D, "_Z");
2105 else
2106 mangle(D, "Z");
Daniel Dunbar7e0c1952009-11-21 09:17:15 +00002107 Out << 'E';
Rafael Espindolad9800722010-03-11 14:07:00 +00002108
2109 if (compensateMangling)
2110 Out << 'E';
2111
Daniel Dunbar7e0c1952009-11-21 09:17:15 +00002112 break;
2113 }
2114 }
Anders Carlsson7a0ba872009-05-15 16:09:15 +00002115}
2116
Anders Carlsson0ccdf8d2009-09-27 00:38:53 +00002117void CXXNameMangler::mangleTemplateParameter(unsigned Index) {
2118 // <template-param> ::= T_ # first template parameter
2119 // ::= T <parameter-2 non-negative number> _
2120 if (Index == 0)
2121 Out << "T_";
2122 else
2123 Out << 'T' << (Index - 1) << '_';
2124}
2125
Anders Carlsson76967372009-09-17 00:43:46 +00002126// <substitution> ::= S <seq-id> _
2127// ::= S_
Anders Carlsson6862fc72009-09-17 04:16:28 +00002128bool CXXNameMangler::mangleSubstitution(const NamedDecl *ND) {
Anders Carlssone7c8cb62009-09-26 20:53:44 +00002129 // Try one of the standard substitutions first.
2130 if (mangleStandardSubstitution(ND))
2131 return true;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002132
Anders Carlsson433d1372009-11-07 04:26:04 +00002133 ND = cast<NamedDecl>(ND->getCanonicalDecl());
Anders Carlsson6862fc72009-09-17 04:16:28 +00002134 return mangleSubstitution(reinterpret_cast<uintptr_t>(ND));
2135}
2136
Anders Carlsson76967372009-09-17 00:43:46 +00002137bool CXXNameMangler::mangleSubstitution(QualType T) {
Anders Carlssond99edc42009-09-26 03:55:37 +00002138 if (!T.getCVRQualifiers()) {
2139 if (const RecordType *RT = T->getAs<RecordType>())
2140 return mangleSubstitution(RT->getDecl());
2141 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002142
Anders Carlsson76967372009-09-17 00:43:46 +00002143 uintptr_t TypePtr = reinterpret_cast<uintptr_t>(T.getAsOpaquePtr());
2144
Anders Carlssond3a932a2009-09-17 03:53:28 +00002145 return mangleSubstitution(TypePtr);
2146}
2147
Douglas Gregor1e9268e2010-04-28 05:58:56 +00002148bool CXXNameMangler::mangleSubstitution(TemplateName Template) {
2149 if (TemplateDecl *TD = Template.getAsTemplateDecl())
2150 return mangleSubstitution(TD);
Sean Huntc3021132010-05-05 15:23:54 +00002151
Douglas Gregor1e9268e2010-04-28 05:58:56 +00002152 Template = Context.getASTContext().getCanonicalTemplateName(Template);
2153 return mangleSubstitution(
2154 reinterpret_cast<uintptr_t>(Template.getAsVoidPointer()));
2155}
2156
Anders Carlssond3a932a2009-09-17 03:53:28 +00002157bool CXXNameMangler::mangleSubstitution(uintptr_t Ptr) {
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002158 llvm::DenseMap<uintptr_t, unsigned>::iterator I = Substitutions.find(Ptr);
Anders Carlsson76967372009-09-17 00:43:46 +00002159 if (I == Substitutions.end())
2160 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002161
Anders Carlsson76967372009-09-17 00:43:46 +00002162 unsigned SeqID = I->second;
2163 if (SeqID == 0)
2164 Out << "S_";
2165 else {
2166 SeqID--;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002167
Anders Carlsson76967372009-09-17 00:43:46 +00002168 // <seq-id> is encoded in base-36, using digits and upper case letters.
2169 char Buffer[10];
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002170 char *BufferPtr = llvm::array_endof(Buffer);
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002171
Anders Carlsson76967372009-09-17 00:43:46 +00002172 if (SeqID == 0) *--BufferPtr = '0';
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002173
Anders Carlsson76967372009-09-17 00:43:46 +00002174 while (SeqID) {
2175 assert(BufferPtr > Buffer && "Buffer overflow!");
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002176
John McCall6ab30e02010-06-09 07:26:17 +00002177 char c = static_cast<char>(SeqID % 36);
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002178
Anders Carlsson76967372009-09-17 00:43:46 +00002179 *--BufferPtr = (c < 10 ? '0' + c : 'A' + c - 10);
2180 SeqID /= 36;
2181 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002182
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002183 Out << 'S'
2184 << llvm::StringRef(BufferPtr, llvm::array_endof(Buffer)-BufferPtr)
2185 << '_';
Anders Carlsson76967372009-09-17 00:43:46 +00002186 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002187
Anders Carlsson76967372009-09-17 00:43:46 +00002188 return true;
2189}
2190
Anders Carlssonf514b542009-09-27 00:12:57 +00002191static bool isCharType(QualType T) {
2192 if (T.isNull())
2193 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002194
Anders Carlssonf514b542009-09-27 00:12:57 +00002195 return T->isSpecificBuiltinType(BuiltinType::Char_S) ||
2196 T->isSpecificBuiltinType(BuiltinType::Char_U);
2197}
2198
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002199/// isCharSpecialization - Returns whether a given type is a template
Anders Carlssonf514b542009-09-27 00:12:57 +00002200/// specialization of a given name with a single argument of type char.
2201static bool isCharSpecialization(QualType T, const char *Name) {
2202 if (T.isNull())
2203 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002204
Anders Carlssonf514b542009-09-27 00:12:57 +00002205 const RecordType *RT = T->getAs<RecordType>();
2206 if (!RT)
2207 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002208
2209 const ClassTemplateSpecializationDecl *SD =
Anders Carlssonf514b542009-09-27 00:12:57 +00002210 dyn_cast<ClassTemplateSpecializationDecl>(RT->getDecl());
2211 if (!SD)
2212 return false;
2213
2214 if (!isStdNamespace(SD->getDeclContext()))
2215 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002216
Anders Carlssonf514b542009-09-27 00:12:57 +00002217 const TemplateArgumentList &TemplateArgs = SD->getTemplateArgs();
2218 if (TemplateArgs.size() != 1)
2219 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002220
Anders Carlssonf514b542009-09-27 00:12:57 +00002221 if (!isCharType(TemplateArgs[0].getAsType()))
2222 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002223
Daniel Dunbar01eb9b92009-10-18 21:17:35 +00002224 return SD->getIdentifier()->getName() == Name;
Anders Carlssonf514b542009-09-27 00:12:57 +00002225}
2226
Anders Carlsson91f88602009-12-07 19:56:42 +00002227template <std::size_t StrLen>
2228bool isStreamCharSpecialization(const ClassTemplateSpecializationDecl *SD,
2229 const char (&Str)[StrLen]) {
2230 if (!SD->getIdentifier()->isStr(Str))
2231 return false;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002232
Anders Carlsson91f88602009-12-07 19:56:42 +00002233 const TemplateArgumentList &TemplateArgs = SD->getTemplateArgs();
2234 if (TemplateArgs.size() != 2)
2235 return false;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002236
Anders Carlsson91f88602009-12-07 19:56:42 +00002237 if (!isCharType(TemplateArgs[0].getAsType()))
2238 return false;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002239
Anders Carlsson91f88602009-12-07 19:56:42 +00002240 if (!isCharSpecialization(TemplateArgs[1].getAsType(), "char_traits"))
2241 return false;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002242
Anders Carlsson91f88602009-12-07 19:56:42 +00002243 return true;
2244}
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002245
Anders Carlssone7c8cb62009-09-26 20:53:44 +00002246bool CXXNameMangler::mangleStandardSubstitution(const NamedDecl *ND) {
2247 // <substitution> ::= St # ::std::
Anders Carlsson8c031552009-09-26 23:10:05 +00002248 if (const NamespaceDecl *NS = dyn_cast<NamespaceDecl>(ND)) {
Anders Carlsson47846d22009-12-04 06:23:23 +00002249 if (isStd(NS)) {
Anders Carlsson8c031552009-09-26 23:10:05 +00002250 Out << "St";
2251 return true;
2252 }
2253 }
2254
2255 if (const ClassTemplateDecl *TD = dyn_cast<ClassTemplateDecl>(ND)) {
2256 if (!isStdNamespace(TD->getDeclContext()))
2257 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002258
Anders Carlsson8c031552009-09-26 23:10:05 +00002259 // <substitution> ::= Sa # ::std::allocator
2260 if (TD->getIdentifier()->isStr("allocator")) {
2261 Out << "Sa";
2262 return true;
2263 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002264
Anders Carlsson189d59c2009-09-26 23:14:39 +00002265 // <<substitution> ::= Sb # ::std::basic_string
2266 if (TD->getIdentifier()->isStr("basic_string")) {
2267 Out << "Sb";
2268 return true;
2269 }
Anders Carlsson8c031552009-09-26 23:10:05 +00002270 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002271
2272 if (const ClassTemplateSpecializationDecl *SD =
Anders Carlssonf514b542009-09-27 00:12:57 +00002273 dyn_cast<ClassTemplateSpecializationDecl>(ND)) {
Eli Friedman5370ee22010-02-23 18:25:09 +00002274 if (!isStdNamespace(SD->getDeclContext()))
2275 return false;
2276
Anders Carlssonf514b542009-09-27 00:12:57 +00002277 // <substitution> ::= Ss # ::std::basic_string<char,
2278 // ::std::char_traits<char>,
2279 // ::std::allocator<char> >
2280 if (SD->getIdentifier()->isStr("basic_string")) {
2281 const TemplateArgumentList &TemplateArgs = SD->getTemplateArgs();
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002282
Anders Carlssonf514b542009-09-27 00:12:57 +00002283 if (TemplateArgs.size() != 3)
2284 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002285
Anders Carlssonf514b542009-09-27 00:12:57 +00002286 if (!isCharType(TemplateArgs[0].getAsType()))
2287 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002288
Anders Carlssonf514b542009-09-27 00:12:57 +00002289 if (!isCharSpecialization(TemplateArgs[1].getAsType(), "char_traits"))
2290 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002291
Anders Carlssonf514b542009-09-27 00:12:57 +00002292 if (!isCharSpecialization(TemplateArgs[2].getAsType(), "allocator"))
2293 return false;
2294
2295 Out << "Ss";
2296 return true;
2297 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002298
Anders Carlsson91f88602009-12-07 19:56:42 +00002299 // <substitution> ::= Si # ::std::basic_istream<char,
2300 // ::std::char_traits<char> >
2301 if (isStreamCharSpecialization(SD, "basic_istream")) {
2302 Out << "Si";
2303 return true;
2304 }
2305
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002306 // <substitution> ::= So # ::std::basic_ostream<char,
Anders Carlsson8f8fd8e2009-10-08 17:20:26 +00002307 // ::std::char_traits<char> >
Anders Carlsson91f88602009-12-07 19:56:42 +00002308 if (isStreamCharSpecialization(SD, "basic_ostream")) {
Anders Carlsson8f8fd8e2009-10-08 17:20:26 +00002309 Out << "So";
2310 return true;
2311 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002312
Anders Carlsson91f88602009-12-07 19:56:42 +00002313 // <substitution> ::= Sd # ::std::basic_iostream<char,
2314 // ::std::char_traits<char> >
2315 if (isStreamCharSpecialization(SD, "basic_iostream")) {
2316 Out << "Sd";
2317 return true;
2318 }
Anders Carlssonf514b542009-09-27 00:12:57 +00002319 }
Anders Carlsson8c031552009-09-26 23:10:05 +00002320 return false;
Anders Carlssone7c8cb62009-09-26 20:53:44 +00002321}
2322
Anders Carlsson76967372009-09-17 00:43:46 +00002323void CXXNameMangler::addSubstitution(QualType T) {
Anders Carlssond99edc42009-09-26 03:55:37 +00002324 if (!T.getCVRQualifiers()) {
2325 if (const RecordType *RT = T->getAs<RecordType>()) {
2326 addSubstitution(RT->getDecl());
2327 return;
2328 }
2329 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002330
Anders Carlsson76967372009-09-17 00:43:46 +00002331 uintptr_t TypePtr = reinterpret_cast<uintptr_t>(T.getAsOpaquePtr());
Anders Carlssond3a932a2009-09-17 03:53:28 +00002332 addSubstitution(TypePtr);
2333}
2334
Douglas Gregor1e9268e2010-04-28 05:58:56 +00002335void CXXNameMangler::addSubstitution(TemplateName Template) {
2336 if (TemplateDecl *TD = Template.getAsTemplateDecl())
2337 return addSubstitution(TD);
Sean Huntc3021132010-05-05 15:23:54 +00002338
Douglas Gregor1e9268e2010-04-28 05:58:56 +00002339 Template = Context.getASTContext().getCanonicalTemplateName(Template);
2340 addSubstitution(reinterpret_cast<uintptr_t>(Template.getAsVoidPointer()));
2341}
2342
Anders Carlssond3a932a2009-09-17 03:53:28 +00002343void CXXNameMangler::addSubstitution(uintptr_t Ptr) {
Anders Carlssond3a932a2009-09-17 03:53:28 +00002344 assert(!Substitutions.count(Ptr) && "Substitution already exists!");
Anders Carlsson9d85b722010-06-02 04:29:50 +00002345 Substitutions[Ptr] = SeqID++;
Anders Carlsson76967372009-09-17 00:43:46 +00002346}
2347
Daniel Dunbar1b077112009-11-21 09:06:10 +00002348//
Mike Stump1eb44332009-09-09 15:08:12 +00002349
Daniel Dunbar1b077112009-11-21 09:06:10 +00002350/// \brief Mangles the name of the declaration D and emits that name to the
2351/// given output stream.
2352///
2353/// If the declaration D requires a mangled name, this routine will emit that
2354/// mangled name to \p os and return true. Otherwise, \p os will be unchanged
2355/// and this routine will return false. In this case, the caller should just
2356/// emit the identifier of the declaration (\c D->getIdentifier()) as its
2357/// name.
Daniel Dunbarf981bf82009-11-21 09:14:52 +00002358void MangleContext::mangleName(const NamedDecl *D,
Daniel Dunbar94fd26d2009-11-21 09:06:22 +00002359 llvm::SmallVectorImpl<char> &Res) {
Daniel Dunbarc02ab4c2009-11-21 09:14:44 +00002360 assert((isa<FunctionDecl>(D) || isa<VarDecl>(D)) &&
2361 "Invalid mangleName() call, argument is not a variable or function!");
2362 assert(!isa<CXXConstructorDecl>(D) && !isa<CXXDestructorDecl>(D) &&
2363 "Invalid mangleName() call on 'structor decl!");
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002364
Daniel Dunbar1b077112009-11-21 09:06:10 +00002365 PrettyStackTraceDecl CrashInfo(D, SourceLocation(),
2366 getASTContext().getSourceManager(),
2367 "Mangling declaration");
Mike Stump1eb44332009-09-09 15:08:12 +00002368
Daniel Dunbar94fd26d2009-11-21 09:06:22 +00002369 CXXNameMangler Mangler(*this, Res);
2370 return Mangler.mangle(D);
Daniel Dunbar1b077112009-11-21 09:06:10 +00002371}
Mike Stump1eb44332009-09-09 15:08:12 +00002372
Daniel Dunbar1b077112009-11-21 09:06:10 +00002373void MangleContext::mangleCXXCtor(const CXXConstructorDecl *D, CXXCtorType Type,
Daniel Dunbar94fd26d2009-11-21 09:06:22 +00002374 llvm::SmallVectorImpl<char> &Res) {
Daniel Dunbar77939c92009-11-21 09:06:31 +00002375 CXXNameMangler Mangler(*this, Res, D, Type);
2376 Mangler.mangle(D);
Daniel Dunbar1b077112009-11-21 09:06:10 +00002377}
Mike Stump1eb44332009-09-09 15:08:12 +00002378
Daniel Dunbar1b077112009-11-21 09:06:10 +00002379void MangleContext::mangleCXXDtor(const CXXDestructorDecl *D, CXXDtorType Type,
Daniel Dunbar94fd26d2009-11-21 09:06:22 +00002380 llvm::SmallVectorImpl<char> &Res) {
Daniel Dunbar77939c92009-11-21 09:06:31 +00002381 CXXNameMangler Mangler(*this, Res, D, Type);
2382 Mangler.mangle(D);
Daniel Dunbar1b077112009-11-21 09:06:10 +00002383}
Mike Stumpf1216772009-07-31 18:25:34 +00002384
Fariborz Jahanian564360b2010-06-24 00:08:06 +00002385void MangleContext::mangleBlock(GlobalDecl GD, const BlockDecl *BD,
Douglas Gregor35415f52010-05-25 17:04:15 +00002386 llvm::SmallVectorImpl<char> &Res) {
Charles Davis685b1d92010-05-26 18:25:27 +00002387 MiscNameMangler Mangler(*this, Res);
Fariborz Jahanian564360b2010-06-24 00:08:06 +00002388 Mangler.mangleBlock(GD, BD);
Douglas Gregor35415f52010-05-25 17:04:15 +00002389}
2390
Anders Carlsson19879c92010-03-23 17:17:29 +00002391void MangleContext::mangleThunk(const CXXMethodDecl *MD,
2392 const ThunkInfo &Thunk,
2393 llvm::SmallVectorImpl<char> &Res) {
2394 // <special-name> ::= T <call-offset> <base encoding>
2395 // # base is the nominal target function of thunk
2396 // <special-name> ::= Tc <call-offset> <call-offset> <base encoding>
2397 // # base is the nominal target function of thunk
2398 // # first call-offset is 'this' adjustment
2399 // # second call-offset is result adjustment
Sean Huntc3021132010-05-05 15:23:54 +00002400
Anders Carlsson19879c92010-03-23 17:17:29 +00002401 assert(!isa<CXXDestructorDecl>(MD) &&
2402 "Use mangleCXXDtor for destructor decls!");
Sean Huntc3021132010-05-05 15:23:54 +00002403
Anders Carlsson19879c92010-03-23 17:17:29 +00002404 CXXNameMangler Mangler(*this, Res);
2405 Mangler.getStream() << "_ZT";
2406 if (!Thunk.Return.isEmpty())
2407 Mangler.getStream() << 'c';
Sean Huntc3021132010-05-05 15:23:54 +00002408
Anders Carlsson19879c92010-03-23 17:17:29 +00002409 // Mangle the 'this' pointer adjustment.
2410 Mangler.mangleCallOffset(Thunk.This.NonVirtual, Thunk.This.VCallOffsetOffset);
Sean Huntc3021132010-05-05 15:23:54 +00002411
Anders Carlsson19879c92010-03-23 17:17:29 +00002412 // Mangle the return pointer adjustment if there is one.
2413 if (!Thunk.Return.isEmpty())
2414 Mangler.mangleCallOffset(Thunk.Return.NonVirtual,
2415 Thunk.Return.VBaseOffsetOffset);
Sean Huntc3021132010-05-05 15:23:54 +00002416
Anders Carlsson19879c92010-03-23 17:17:29 +00002417 Mangler.mangleFunctionEncoding(MD);
2418}
2419
Sean Huntc3021132010-05-05 15:23:54 +00002420void
Anders Carlsson19879c92010-03-23 17:17:29 +00002421MangleContext::mangleCXXDtorThunk(const CXXDestructorDecl *DD, CXXDtorType Type,
2422 const ThisAdjustment &ThisAdjustment,
2423 llvm::SmallVectorImpl<char> &Res) {
2424 // <special-name> ::= T <call-offset> <base encoding>
2425 // # base is the nominal target function of thunk
Sean Huntc3021132010-05-05 15:23:54 +00002426
Anders Carlsson19879c92010-03-23 17:17:29 +00002427 CXXNameMangler Mangler(*this, Res, DD, Type);
2428 Mangler.getStream() << "_ZT";
2429
2430 // Mangle the 'this' pointer adjustment.
Sean Huntc3021132010-05-05 15:23:54 +00002431 Mangler.mangleCallOffset(ThisAdjustment.NonVirtual,
Anders Carlsson19879c92010-03-23 17:17:29 +00002432 ThisAdjustment.VCallOffsetOffset);
2433
2434 Mangler.mangleFunctionEncoding(DD);
2435}
2436
Daniel Dunbarc0747712009-11-21 09:12:13 +00002437/// mangleGuardVariable - Returns the mangled name for a guard variable
2438/// for the passed in VarDecl.
2439void MangleContext::mangleGuardVariable(const VarDecl *D,
2440 llvm::SmallVectorImpl<char> &Res) {
2441 // <special-name> ::= GV <object name> # Guard variable for one-time
2442 // # initialization
2443 CXXNameMangler Mangler(*this, Res);
2444 Mangler.getStream() << "_ZGV";
2445 Mangler.mangleName(D);
2446}
2447
Anders Carlsson715edf22010-06-26 16:09:40 +00002448void MangleContext::mangleReferenceTemporary(const VarDecl *D,
2449 llvm::SmallVectorImpl<char> &Res) {
2450 // We match the GCC mangling here.
2451 // <special-name> ::= GR <object name>
2452 CXXNameMangler Mangler(*this, Res);
2453 Mangler.getStream() << "_ZGR";
2454 Mangler.mangleName(D);
2455}
2456
Anders Carlsson046c2942010-04-17 20:15:18 +00002457void MangleContext::mangleCXXVTable(const CXXRecordDecl *RD,
Daniel Dunbar94fd26d2009-11-21 09:06:22 +00002458 llvm::SmallVectorImpl<char> &Res) {
Daniel Dunbarc0747712009-11-21 09:12:13 +00002459 // <special-name> ::= TV <type> # virtual table
Daniel Dunbar94fd26d2009-11-21 09:06:22 +00002460 CXXNameMangler Mangler(*this, Res);
Daniel Dunbarc0747712009-11-21 09:12:13 +00002461 Mangler.getStream() << "_ZTV";
Douglas Gregor1b12a3b2010-05-26 05:11:13 +00002462 Mangler.mangleNameOrStandardSubstitution(RD);
Daniel Dunbar1b077112009-11-21 09:06:10 +00002463}
Mike Stump82d75b02009-11-10 01:58:37 +00002464
Daniel Dunbar1b077112009-11-21 09:06:10 +00002465void MangleContext::mangleCXXVTT(const CXXRecordDecl *RD,
Daniel Dunbar94fd26d2009-11-21 09:06:22 +00002466 llvm::SmallVectorImpl<char> &Res) {
Daniel Dunbarc0747712009-11-21 09:12:13 +00002467 // <special-name> ::= TT <type> # VTT structure
Daniel Dunbar94fd26d2009-11-21 09:06:22 +00002468 CXXNameMangler Mangler(*this, Res);
Daniel Dunbarc0747712009-11-21 09:12:13 +00002469 Mangler.getStream() << "_ZTT";
Douglas Gregor1b12a3b2010-05-26 05:11:13 +00002470 Mangler.mangleNameOrStandardSubstitution(RD);
Daniel Dunbar1b077112009-11-21 09:06:10 +00002471}
Mike Stumpab3f7e92009-11-10 01:41:59 +00002472
Anders Carlsson046c2942010-04-17 20:15:18 +00002473void MangleContext::mangleCXXCtorVTable(const CXXRecordDecl *RD, int64_t Offset,
Daniel Dunbar1b077112009-11-21 09:06:10 +00002474 const CXXRecordDecl *Type,
Daniel Dunbar94fd26d2009-11-21 09:06:22 +00002475 llvm::SmallVectorImpl<char> &Res) {
Daniel Dunbarc0747712009-11-21 09:12:13 +00002476 // <special-name> ::= TC <type> <offset number> _ <base type>
Daniel Dunbar94fd26d2009-11-21 09:06:22 +00002477 CXXNameMangler Mangler(*this, Res);
Daniel Dunbarc0747712009-11-21 09:12:13 +00002478 Mangler.getStream() << "_ZTC";
Douglas Gregor1b12a3b2010-05-26 05:11:13 +00002479 Mangler.mangleNameOrStandardSubstitution(RD);
Daniel Dunbarc0747712009-11-21 09:12:13 +00002480 Mangler.getStream() << Offset;
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002481 Mangler.getStream() << '_';
Douglas Gregor1b12a3b2010-05-26 05:11:13 +00002482 Mangler.mangleNameOrStandardSubstitution(Type);
Daniel Dunbar1b077112009-11-21 09:06:10 +00002483}
Mike Stump738f8c22009-07-31 23:15:31 +00002484
Mike Stumpde050572009-12-02 18:57:08 +00002485void MangleContext::mangleCXXRTTI(QualType Ty,
Daniel Dunbar94fd26d2009-11-21 09:06:22 +00002486 llvm::SmallVectorImpl<char> &Res) {
Daniel Dunbarc0747712009-11-21 09:12:13 +00002487 // <special-name> ::= TI <type> # typeinfo structure
Douglas Gregor154fe982009-12-23 22:04:40 +00002488 assert(!Ty.hasQualifiers() && "RTTI info cannot have top-level qualifiers");
Daniel Dunbar94fd26d2009-11-21 09:06:22 +00002489 CXXNameMangler Mangler(*this, Res);
Daniel Dunbarc0747712009-11-21 09:12:13 +00002490 Mangler.getStream() << "_ZTI";
2491 Mangler.mangleType(Ty);
Daniel Dunbar1b077112009-11-21 09:06:10 +00002492}
Mike Stump67795982009-11-14 00:14:13 +00002493
Mike Stumpde050572009-12-02 18:57:08 +00002494void MangleContext::mangleCXXRTTIName(QualType Ty,
Daniel Dunbar94fd26d2009-11-21 09:06:22 +00002495 llvm::SmallVectorImpl<char> &Res) {
Daniel Dunbarc0747712009-11-21 09:12:13 +00002496 // <special-name> ::= TS <type> # typeinfo name (null terminated byte string)
Daniel Dunbar94fd26d2009-11-21 09:06:22 +00002497 CXXNameMangler Mangler(*this, Res);
Daniel Dunbarc0747712009-11-21 09:12:13 +00002498 Mangler.getStream() << "_ZTS";
2499 Mangler.mangleType(Ty);
Mike Stumpf1216772009-07-31 18:25:34 +00002500}