blob: b5f629aeb5461efdb023d2f7890aa0c923714050 [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 McCall1dd73832010-02-04 01:42:13 +0000244 void mangleCalledExpression(const Expr *E, unsigned KnownArity);
Daniel Dunbar1b077112009-11-21 09:06:10 +0000245 void mangleExpression(const Expr *E);
246 void mangleCXXCtorType(CXXCtorType T);
247 void mangleCXXDtorType(CXXDtorType T);
Mike Stump1eb44332009-09-09 15:08:12 +0000248
Douglas Gregor20f0cc72010-04-23 03:10:43 +0000249 void mangleTemplateArgs(TemplateName Template,
250 const TemplateArgument *TemplateArgs,
Sean Huntc3021132010-05-05 15:23:54 +0000251 unsigned NumTemplateArgs);
Rafael Espindolad9800722010-03-11 14:07:00 +0000252 void mangleTemplateArgs(const TemplateParameterList &PL,
253 const TemplateArgument *TemplateArgs,
Daniel Dunbar1b077112009-11-21 09:06:10 +0000254 unsigned NumTemplateArgs);
Rafael Espindolad9800722010-03-11 14:07:00 +0000255 void mangleTemplateArgs(const TemplateParameterList &PL,
256 const TemplateArgumentList &AL);
257 void mangleTemplateArg(const NamedDecl *P, const TemplateArgument &A);
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000258
Daniel Dunbar1b077112009-11-21 09:06:10 +0000259 void mangleTemplateParameter(unsigned Index);
260};
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000261}
262
Anders Carlsson43f17402009-04-02 15:51:53 +0000263static bool isInCLinkageSpecification(const Decl *D) {
Douglas Gregor457e2812009-10-28 16:31:34 +0000264 D = D->getCanonicalDecl();
Mike Stump1eb44332009-09-09 15:08:12 +0000265 for (const DeclContext *DC = D->getDeclContext();
Anders Carlsson43f17402009-04-02 15:51:53 +0000266 !DC->isTranslationUnit(); DC = DC->getParent()) {
Mike Stump1eb44332009-09-09 15:08:12 +0000267 if (const LinkageSpecDecl *Linkage = dyn_cast<LinkageSpecDecl>(DC))
Anders Carlsson43f17402009-04-02 15:51:53 +0000268 return Linkage->getLanguage() == LinkageSpecDecl::lang_c;
269 }
Mike Stump1eb44332009-09-09 15:08:12 +0000270
Anders Carlsson43f17402009-04-02 15:51:53 +0000271 return false;
272}
273
Daniel Dunbarf981bf82009-11-21 09:14:52 +0000274bool MangleContext::shouldMangleDeclName(const NamedDecl *D) {
275 // In C, functions with no attributes never need to be mangled. Fastpath them.
276 if (!getASTContext().getLangOptions().CPlusPlus && !D->hasAttrs())
277 return false;
278
279 // Any decl can be declared with __asm("foo") on it, and this takes precedence
280 // over all other naming in the .o file.
281 if (D->hasAttr<AsmLabelAttr>())
282 return true;
283
Mike Stump141c5af2009-09-02 00:25:38 +0000284 // Clang's "overloadable" attribute extension to C/C++ implies name mangling
Anders Carlssona1e16222009-11-07 07:15:03 +0000285 // (always) as does passing a C++ member function and a function
286 // whose name is not a simple identifier.
Daniel Dunbarf981bf82009-11-21 09:14:52 +0000287 const FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
288 if (FD && (FD->hasAttr<OverloadableAttr>() || isa<CXXMethodDecl>(FD) ||
289 !FD->getDeclName().isIdentifier()))
290 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000291
Daniel Dunbarf981bf82009-11-21 09:14:52 +0000292 // Otherwise, no mangling is done outside C++ mode.
293 if (!getASTContext().getLangOptions().CPlusPlus)
294 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000295
Sean Hunt31455252010-01-24 03:04:27 +0000296 // Variables at global scope with non-internal linkage are not mangled
Eli Friedman7facf842009-12-02 20:32:49 +0000297 if (!FD) {
298 const DeclContext *DC = D->getDeclContext();
299 // Check for extern variable declared locally.
Fariborz Jahaniane81c5612010-06-30 18:57:21 +0000300 if (DC->isFunctionOrMethod() && D->hasLinkage())
Eli Friedman7facf842009-12-02 20:32:49 +0000301 while (!DC->isNamespace() && !DC->isTranslationUnit())
302 DC = DC->getParent();
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000303 if (DC->isTranslationUnit() && D->getLinkage() != InternalLinkage)
Eli Friedman7facf842009-12-02 20:32:49 +0000304 return false;
305 }
306
Eli Friedmanc00cb642010-07-18 20:49:59 +0000307 // Class members are always mangled.
308 if (D->getDeclContext()->isRecord())
309 return true;
310
Eli Friedman7facf842009-12-02 20:32:49 +0000311 // C functions and "main" are not mangled.
312 if ((FD && FD->isMain()) || isInCLinkageSpecification(D))
Daniel Dunbarf981bf82009-11-21 09:14:52 +0000313 return false;
314
Anders Carlsson43f17402009-04-02 15:51:53 +0000315 return true;
316}
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000317
Daniel Dunbar7e0c1952009-11-21 09:17:15 +0000318void CXXNameMangler::mangle(const NamedDecl *D, llvm::StringRef Prefix) {
Mike Stump141c5af2009-09-02 00:25:38 +0000319 // Any decl can be declared with __asm("foo") on it, and this takes precedence
320 // over all other naming in the .o file.
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000321 if (const AsmLabelAttr *ALA = D->getAttr<AsmLabelAttr>()) {
Chris Lattnerca3f25c2009-03-21 08:24:40 +0000322 // If we have an asm name, then we use it as the mangling.
323 Out << '\01'; // LLVM IR Marker for __asm("foo")
324 Out << ALA->getLabel();
Daniel Dunbarf981bf82009-11-21 09:14:52 +0000325 return;
Chris Lattnerca3f25c2009-03-21 08:24:40 +0000326 }
Mike Stump1eb44332009-09-09 15:08:12 +0000327
Sean Hunt31455252010-01-24 03:04:27 +0000328 // <mangled-name> ::= _Z <encoding>
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000329 // ::= <data name>
330 // ::= <special-name>
Daniel Dunbar7e0c1952009-11-21 09:17:15 +0000331 Out << Prefix;
332 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
Daniel Dunbarf981bf82009-11-21 09:14:52 +0000333 mangleFunctionEncoding(FD);
Rafael Espindolad9800722010-03-11 14:07:00 +0000334 else if (const VarDecl *VD = dyn_cast<VarDecl>(D))
335 mangleName(VD);
Daniel Dunbar7e0c1952009-11-21 09:17:15 +0000336 else
Rafael Espindolad9800722010-03-11 14:07:00 +0000337 mangleName(cast<FieldDecl>(D));
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000338}
339
340void CXXNameMangler::mangleFunctionEncoding(const FunctionDecl *FD) {
341 // <encoding> ::= <function name> <bare-function-type>
342 mangleName(FD);
Mike Stump1eb44332009-09-09 15:08:12 +0000343
Daniel Dunbar7e0c1952009-11-21 09:17:15 +0000344 // Don't mangle in the type if this isn't a decl we should typically mangle.
345 if (!Context.shouldMangleDeclName(FD))
346 return;
347
Mike Stump141c5af2009-09-02 00:25:38 +0000348 // Whether the mangling of a function type includes the return type depends on
349 // the context and the nature of the function. The rules for deciding whether
350 // the return type is included are:
Mike Stump1eb44332009-09-09 15:08:12 +0000351 //
Douglas Gregor1fd2dd12009-06-29 22:39:32 +0000352 // 1. Template functions (names or types) have return types encoded, with
353 // the exceptions listed below.
Mike Stump1eb44332009-09-09 15:08:12 +0000354 // 2. Function types not appearing as part of a function name mangling,
Douglas Gregor1fd2dd12009-06-29 22:39:32 +0000355 // e.g. parameters, pointer types, etc., have return type encoded, with the
356 // exceptions listed below.
357 // 3. Non-template function names do not have return types encoded.
358 //
Mike Stump141c5af2009-09-02 00:25:38 +0000359 // The exceptions mentioned in (1) and (2) above, for which the return type is
360 // never included, are
Douglas Gregor1fd2dd12009-06-29 22:39:32 +0000361 // 1. Constructors.
362 // 2. Destructors.
363 // 3. Conversion operator functions, e.g. operator int.
364 bool MangleReturnType = false;
Anders Carlsson9234b7f2009-09-17 03:46:43 +0000365 if (FunctionTemplateDecl *PrimaryTemplate = FD->getPrimaryTemplate()) {
366 if (!(isa<CXXConstructorDecl>(FD) || isa<CXXDestructorDecl>(FD) ||
367 isa<CXXConversionDecl>(FD)))
368 MangleReturnType = true;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000369
Anders Carlsson9234b7f2009-09-17 03:46:43 +0000370 // Mangle the type of the primary template.
371 FD = PrimaryTemplate->getTemplatedDecl();
372 }
373
John McCall54e14c42009-10-22 22:37:11 +0000374 // Do the canonicalization out here because parameter types can
375 // undergo additional canonicalization (e.g. array decay).
376 FunctionType *FT = cast<FunctionType>(Context.getASTContext()
377 .getCanonicalType(FD->getType()));
378
379 mangleBareFunctionType(FT, MangleReturnType);
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000380}
381
Anders Carlsson47846d22009-12-04 06:23:23 +0000382static const DeclContext *IgnoreLinkageSpecDecls(const DeclContext *DC) {
383 while (isa<LinkageSpecDecl>(DC)) {
Anders Carlsson47846d22009-12-04 06:23:23 +0000384 DC = DC->getParent();
385 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000386
Anders Carlsson47846d22009-12-04 06:23:23 +0000387 return DC;
388}
389
Anders Carlssonc820f902010-06-02 15:58:27 +0000390/// isStd - Return whether a given namespace is the 'std' namespace.
391static bool isStd(const NamespaceDecl *NS) {
392 if (!IgnoreLinkageSpecDecls(NS->getParent())->isTranslationUnit())
393 return false;
394
395 const IdentifierInfo *II = NS->getOriginalNamespace()->getIdentifier();
396 return II && II->isStr("std");
397}
398
Anders Carlsson47846d22009-12-04 06:23:23 +0000399// isStdNamespace - Return whether a given decl context is a toplevel 'std'
400// namespace.
Daniel Dunbar1308af92009-11-21 09:11:45 +0000401static bool isStdNamespace(const DeclContext *DC) {
Anders Carlsson47846d22009-12-04 06:23:23 +0000402 if (!DC->isNamespace())
403 return false;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000404
Anders Carlsson47846d22009-12-04 06:23:23 +0000405 return isStd(cast<NamespaceDecl>(DC));
Daniel Dunbar1308af92009-11-21 09:11:45 +0000406}
407
Anders Carlssonbb36ba42009-09-26 03:24:57 +0000408static const TemplateDecl *
409isTemplate(const NamedDecl *ND, const TemplateArgumentList *&TemplateArgs) {
Anders Carlsson2744a062009-09-18 19:00:18 +0000410 // Check if we have a function template.
411 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)){
Anders Carlssonbb36ba42009-09-26 03:24:57 +0000412 if (const TemplateDecl *TD = FD->getPrimaryTemplate()) {
Anders Carlsson2744a062009-09-18 19:00:18 +0000413 TemplateArgs = FD->getTemplateSpecializationArgs();
Anders Carlssonbb36ba42009-09-26 03:24:57 +0000414 return TD;
Anders Carlsson2744a062009-09-18 19:00:18 +0000415 }
416 }
417
Anders Carlssoneafc6dc2009-09-18 19:44:50 +0000418 // Check if we have a class template.
419 if (const ClassTemplateSpecializationDecl *Spec =
420 dyn_cast<ClassTemplateSpecializationDecl>(ND)) {
421 TemplateArgs = &Spec->getTemplateArgs();
Anders Carlssonbb36ba42009-09-26 03:24:57 +0000422 return Spec->getSpecializedTemplate();
Anders Carlssoneafc6dc2009-09-18 19:44:50 +0000423 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000424
Anders Carlsson2744a062009-09-18 19:00:18 +0000425 return 0;
426}
427
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000428void CXXNameMangler::mangleName(const NamedDecl *ND) {
429 // <name> ::= <nested-name>
430 // ::= <unscoped-name>
431 // ::= <unscoped-template-name> <template-args>
Anders Carlsson201ce742009-09-17 03:17:01 +0000432 // ::= <local-name>
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000433 //
Anders Carlssond58d6f72009-09-17 16:12:20 +0000434 const DeclContext *DC = ND->getDeclContext();
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000435
Fariborz Jahanian57058532010-03-03 19:41:08 +0000436 if (GetLocalClassFunctionDeclContext(DC)) {
437 mangleLocalName(ND);
438 return;
439 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000440
Eli Friedman7facf842009-12-02 20:32:49 +0000441 // If this is an extern variable declared locally, the relevant DeclContext
442 // is that of the containing namespace, or the translation unit.
443 if (isa<FunctionDecl>(DC) && ND->hasLinkage())
444 while (!DC->isNamespace() && !DC->isTranslationUnit())
445 DC = DC->getParent();
446
Anders Carlsson5cc58c62009-09-22 17:23:30 +0000447 while (isa<LinkageSpecDecl>(DC))
Anders Carlssond58d6f72009-09-17 16:12:20 +0000448 DC = DC->getParent();
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000449
Anders Carlssond58d6f72009-09-17 16:12:20 +0000450 if (DC->isTranslationUnit() || isStdNamespace(DC)) {
Anders Carlsson2744a062009-09-18 19:00:18 +0000451 // Check if we have a template.
452 const TemplateArgumentList *TemplateArgs = 0;
Anders Carlsson0fa6df42009-09-26 19:45:45 +0000453 if (const TemplateDecl *TD = isTemplate(ND, TemplateArgs)) {
Anders Carlsson2744a062009-09-18 19:00:18 +0000454 mangleUnscopedTemplateName(TD);
Rafael Espindolad9800722010-03-11 14:07:00 +0000455 TemplateParameterList *TemplateParameters = TD->getTemplateParameters();
456 mangleTemplateArgs(*TemplateParameters, *TemplateArgs);
Anders Carlsson2744a062009-09-18 19:00:18 +0000457 return;
Anders Carlsson7482e242009-09-18 04:29:09 +0000458 }
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000459
Anders Carlsson7482e242009-09-18 04:29:09 +0000460 mangleUnscopedName(ND);
461 return;
462 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000463
Anders Carlsson7b06f6c2009-12-10 03:14:39 +0000464 if (isa<FunctionDecl>(DC) || isa<ObjCMethodDecl>(DC)) {
Anders Carlsson7482e242009-09-18 04:29:09 +0000465 mangleLocalName(ND);
466 return;
467 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000468
Eli Friedman7facf842009-12-02 20:32:49 +0000469 mangleNestedName(ND, DC);
Anders Carlsson7482e242009-09-18 04:29:09 +0000470}
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000471void CXXNameMangler::mangleName(const TemplateDecl *TD,
Anders Carlsson7624f212009-09-18 02:42:01 +0000472 const TemplateArgument *TemplateArgs,
473 unsigned NumTemplateArgs) {
Anders Carlsson47846d22009-12-04 06:23:23 +0000474 const DeclContext *DC = IgnoreLinkageSpecDecls(TD->getDeclContext());
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000475
Anders Carlsson7624f212009-09-18 02:42:01 +0000476 if (DC->isTranslationUnit() || isStdNamespace(DC)) {
Anders Carlsson0fa6df42009-09-26 19:45:45 +0000477 mangleUnscopedTemplateName(TD);
Rafael Espindolad9800722010-03-11 14:07:00 +0000478 TemplateParameterList *TemplateParameters = TD->getTemplateParameters();
479 mangleTemplateArgs(*TemplateParameters, TemplateArgs, NumTemplateArgs);
Anders Carlsson7624f212009-09-18 02:42:01 +0000480 } else {
481 mangleNestedName(TD, TemplateArgs, NumTemplateArgs);
482 }
483}
484
Anders Carlsson201ce742009-09-17 03:17:01 +0000485void CXXNameMangler::mangleUnscopedName(const NamedDecl *ND) {
486 // <unscoped-name> ::= <unqualified-name>
487 // ::= St <unqualified-name> # ::std::
488 if (isStdNamespace(ND->getDeclContext()))
489 Out << "St";
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000490
Anders Carlsson201ce742009-09-17 03:17:01 +0000491 mangleUnqualifiedName(ND);
492}
493
Anders Carlsson0fa6df42009-09-26 19:45:45 +0000494void CXXNameMangler::mangleUnscopedTemplateName(const TemplateDecl *ND) {
Anders Carlsson201ce742009-09-17 03:17:01 +0000495 // <unscoped-template-name> ::= <unscoped-name>
496 // ::= <substitution>
Anders Carlsson7624f212009-09-18 02:42:01 +0000497 if (mangleSubstitution(ND))
Anders Carlsson03c9d532009-09-17 04:02:31 +0000498 return;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000499
Douglas Gregor32fb4e12010-02-05 20:45:00 +0000500 // <template-template-param> ::= <template-param>
501 if (const TemplateTemplateParmDecl *TTP
502 = dyn_cast<TemplateTemplateParmDecl>(ND)) {
503 mangleTemplateParameter(TTP->getIndex());
504 return;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000505 }
Douglas Gregor32fb4e12010-02-05 20:45:00 +0000506
Anders Carlsson1668f202009-09-26 20:13:56 +0000507 mangleUnscopedName(ND->getTemplatedDecl());
Anders Carlsson7624f212009-09-18 02:42:01 +0000508 addSubstitution(ND);
Anders Carlsson201ce742009-09-17 03:17:01 +0000509}
510
Douglas Gregor1e9268e2010-04-28 05:58:56 +0000511void CXXNameMangler::mangleUnscopedTemplateName(TemplateName Template) {
512 // <unscoped-template-name> ::= <unscoped-name>
513 // ::= <substitution>
514 if (TemplateDecl *TD = Template.getAsTemplateDecl())
515 return mangleUnscopedTemplateName(TD);
Sean Huntc3021132010-05-05 15:23:54 +0000516
Douglas Gregor1e9268e2010-04-28 05:58:56 +0000517 if (mangleSubstitution(Template))
518 return;
519
520 // FIXME: How to cope with operators here?
521 DependentTemplateName *Dependent = Template.getAsDependentTemplateName();
522 assert(Dependent && "Not a dependent template name?");
523 if (!Dependent->isIdentifier()) {
524 // FIXME: We can't possibly know the arity of the operator here!
525 Diagnostic &Diags = Context.getDiags();
526 unsigned DiagID = Diags.getCustomDiagID(Diagnostic::Error,
527 "cannot mangle dependent operator name");
528 Diags.Report(FullSourceLoc(), DiagID);
529 return;
530 }
Sean Huntc3021132010-05-05 15:23:54 +0000531
Douglas Gregor1e9268e2010-04-28 05:58:56 +0000532 mangleSourceName(Dependent->getIdentifier());
533 addSubstitution(Template);
534}
535
John McCall0512e482010-07-14 04:20:34 +0000536void CXXNameMangler::mangleFloat(const llvm::APFloat &F) {
537 // TODO: avoid this copy with careful stream management.
538 llvm::SmallString<20> Buffer;
539 F.bitcastToAPInt().toString(Buffer, 16, false);
540 Out.write(Buffer.data(), Buffer.size());
541}
542
543void CXXNameMangler::mangleNumber(const llvm::APSInt &Value) {
544 if (Value.isSigned() && Value.isNegative()) {
545 Out << 'n';
546 Value.abs().print(Out, true);
547 } else
548 Value.print(Out, Value.isSigned());
549}
550
Anders Carlssona94822e2009-11-26 02:32:05 +0000551void CXXNameMangler::mangleNumber(int64_t Number) {
552 // <number> ::= [n] <non-negative decimal integer>
553 if (Number < 0) {
554 Out << 'n';
555 Number = -Number;
556 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000557
Anders Carlssona94822e2009-11-26 02:32:05 +0000558 Out << Number;
559}
560
Anders Carlsson19879c92010-03-23 17:17:29 +0000561void CXXNameMangler::mangleCallOffset(int64_t NonVirtual, int64_t Virtual) {
Mike Stump141c5af2009-09-02 00:25:38 +0000562 // <call-offset> ::= h <nv-offset> _
563 // ::= v <v-offset> _
564 // <nv-offset> ::= <offset number> # non-virtual base override
Anders Carlssona94822e2009-11-26 02:32:05 +0000565 // <v-offset> ::= <offset number> _ <virtual offset number>
Mike Stump141c5af2009-09-02 00:25:38 +0000566 // # virtual base override, with vcall offset
Anders Carlsson19879c92010-03-23 17:17:29 +0000567 if (!Virtual) {
Anders Carlssona94822e2009-11-26 02:32:05 +0000568 Out << 'h';
Anders Carlsson19879c92010-03-23 17:17:29 +0000569 mangleNumber(NonVirtual);
Anders Carlssona94822e2009-11-26 02:32:05 +0000570 Out << '_';
571 return;
Mike Stump141c5af2009-09-02 00:25:38 +0000572 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000573
Anders Carlssona94822e2009-11-26 02:32:05 +0000574 Out << 'v';
Anders Carlsson19879c92010-03-23 17:17:29 +0000575 mangleNumber(NonVirtual);
Anders Carlssona94822e2009-11-26 02:32:05 +0000576 Out << '_';
Anders Carlsson19879c92010-03-23 17:17:29 +0000577 mangleNumber(Virtual);
Anders Carlssona94822e2009-11-26 02:32:05 +0000578 Out << '_';
Mike Stump9124bcc2009-09-02 00:56:18 +0000579}
580
John McCall1dd73832010-02-04 01:42:13 +0000581void CXXNameMangler::mangleUnresolvedScope(NestedNameSpecifier *Qualifier) {
582 Qualifier = getASTContext().getCanonicalNestedNameSpecifier(Qualifier);
583 switch (Qualifier->getKind()) {
584 case NestedNameSpecifier::Global:
585 // nothing
586 break;
587 case NestedNameSpecifier::Namespace:
588 mangleName(Qualifier->getAsNamespace());
589 break;
590 case NestedNameSpecifier::TypeSpec:
Rafael Espindola9b35b252010-03-17 04:28:11 +0000591 case NestedNameSpecifier::TypeSpecWithTemplate: {
592 const Type *QTy = Qualifier->getAsType();
593
594 if (const TemplateSpecializationType *TST =
595 dyn_cast<TemplateSpecializationType>(QTy)) {
596 if (!mangleSubstitution(QualType(TST, 0))) {
Douglas Gregor20f0cc72010-04-23 03:10:43 +0000597 mangleTemplatePrefix(TST->getTemplateName());
Sean Huntc3021132010-05-05 15:23:54 +0000598
Douglas Gregor20f0cc72010-04-23 03:10:43 +0000599 // FIXME: GCC does not appear to mangle the template arguments when
600 // the template in question is a dependent template name. Should we
601 // emulate that badness?
602 mangleTemplateArgs(TST->getTemplateName(), TST->getArgs(),
Rafael Espindola9b35b252010-03-17 04:28:11 +0000603 TST->getNumArgs());
604 addSubstitution(QualType(TST, 0));
605 }
606 } else {
607 // We use the QualType mangle type variant here because it handles
608 // substitutions.
609 mangleType(QualType(QTy, 0));
610 }
611 }
John McCall1dd73832010-02-04 01:42:13 +0000612 break;
613 case NestedNameSpecifier::Identifier:
John McCallad5e7382010-03-01 23:49:17 +0000614 // Member expressions can have these without prefixes.
615 if (Qualifier->getPrefix())
616 mangleUnresolvedScope(Qualifier->getPrefix());
John McCall1dd73832010-02-04 01:42:13 +0000617 mangleSourceName(Qualifier->getAsIdentifier());
618 break;
619 }
620}
621
622/// Mangles a name which was not resolved to a specific entity.
623void CXXNameMangler::mangleUnresolvedName(NestedNameSpecifier *Qualifier,
624 DeclarationName Name,
625 unsigned KnownArity) {
626 if (Qualifier)
627 mangleUnresolvedScope(Qualifier);
628 // FIXME: ambiguity of unqualified lookup with ::
629
630 mangleUnqualifiedName(0, Name, KnownArity);
631}
632
Anders Carlsson6f7e2f42010-06-08 14:49:03 +0000633static const FieldDecl *FindFirstNamedDataMember(const RecordDecl *RD) {
634 assert(RD->isAnonymousStructOrUnion() &&
635 "Expected anonymous struct or union!");
636
637 for (RecordDecl::field_iterator I = RD->field_begin(), E = RD->field_end();
638 I != E; ++I) {
639 const FieldDecl *FD = *I;
640
641 if (FD->getIdentifier())
642 return FD;
643
644 if (const RecordType *RT = FD->getType()->getAs<RecordType>()) {
645 if (const FieldDecl *NamedDataMember =
646 FindFirstNamedDataMember(RT->getDecl()))
647 return NamedDataMember;
648 }
649 }
650
651 // We didn't find a named data member.
652 return 0;
653}
654
John McCall1dd73832010-02-04 01:42:13 +0000655void CXXNameMangler::mangleUnqualifiedName(const NamedDecl *ND,
656 DeclarationName Name,
657 unsigned KnownArity) {
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000658 // <unqualified-name> ::= <operator-name>
Mike Stump1eb44332009-09-09 15:08:12 +0000659 // ::= <ctor-dtor-name>
660 // ::= <source-name>
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000661 switch (Name.getNameKind()) {
Anders Carlssonc4355b62009-10-07 01:45:02 +0000662 case DeclarationName::Identifier: {
Anders Carlssonc4355b62009-10-07 01:45:02 +0000663 if (const IdentifierInfo *II = Name.getAsIdentifierInfo()) {
Sean Hunt31455252010-01-24 03:04:27 +0000664 // We must avoid conflicts between internally- and externally-
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000665 // linked variable declaration names in the same TU.
Anders Carlssonaec25232010-02-06 04:52:27 +0000666 // This naming convention is the same as that followed by GCC, though it
667 // shouldn't actually matter.
668 if (ND && isa<VarDecl>(ND) && ND->getLinkage() == InternalLinkage &&
Sean Hunt31455252010-01-24 03:04:27 +0000669 ND->getDeclContext()->isFileContext())
670 Out << 'L';
671
Anders Carlssonc4355b62009-10-07 01:45:02 +0000672 mangleSourceName(II);
673 break;
674 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000675
John McCall1dd73832010-02-04 01:42:13 +0000676 // Otherwise, an anonymous entity. We must have a declaration.
677 assert(ND && "mangling empty name without declaration");
678
679 if (const NamespaceDecl *NS = dyn_cast<NamespaceDecl>(ND)) {
680 if (NS->isAnonymousNamespace()) {
681 // This is how gcc mangles these names.
682 Out << "12_GLOBAL__N_1";
683 break;
684 }
685 }
686
Anders Carlsson6f7e2f42010-06-08 14:49:03 +0000687 if (const VarDecl *VD = dyn_cast<VarDecl>(ND)) {
688 // We must have an anonymous union or struct declaration.
689 const RecordDecl *RD =
690 cast<RecordDecl>(VD->getType()->getAs<RecordType>()->getDecl());
691
692 // Itanium C++ ABI 5.1.2:
693 //
694 // For the purposes of mangling, the name of an anonymous union is
695 // considered to be the name of the first named data member found by a
696 // pre-order, depth-first, declaration-order walk of the data members of
697 // the anonymous union. If there is no such data member (i.e., if all of
698 // the data members in the union are unnamed), then there is no way for
699 // a program to refer to the anonymous union, and there is therefore no
700 // need to mangle its name.
701 const FieldDecl *FD = FindFirstNamedDataMember(RD);
702 assert(FD && "Didn't find a named data member!");
703 assert(FD->getIdentifier() && "Data member name isn't an identifier!");
704
705 mangleSourceName(FD->getIdentifier());
706 break;
707 }
708
Anders Carlssonc4355b62009-10-07 01:45:02 +0000709 // We must have an anonymous struct.
710 const TagDecl *TD = cast<TagDecl>(ND);
711 if (const TypedefDecl *D = TD->getTypedefForAnonDecl()) {
712 assert(TD->getDeclContext() == D->getDeclContext() &&
713 "Typedef should not be in another decl context!");
714 assert(D->getDeclName().getAsIdentifierInfo() &&
715 "Typedef was not named!");
716 mangleSourceName(D->getDeclName().getAsIdentifierInfo());
717 break;
718 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000719
Anders Carlssonc4355b62009-10-07 01:45:02 +0000720 // Get a unique id for the anonymous struct.
721 uint64_t AnonStructId = Context.getAnonymousStructId(TD);
722
723 // Mangle it as a source name in the form
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000724 // [n] $_<id>
Anders Carlssonc4355b62009-10-07 01:45:02 +0000725 // where n is the length of the string.
726 llvm::SmallString<8> Str;
727 Str += "$_";
728 Str += llvm::utostr(AnonStructId);
729
730 Out << Str.size();
731 Out << Str.str();
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000732 break;
Anders Carlssonc4355b62009-10-07 01:45:02 +0000733 }
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000734
735 case DeclarationName::ObjCZeroArgSelector:
736 case DeclarationName::ObjCOneArgSelector:
737 case DeclarationName::ObjCMultiArgSelector:
738 assert(false && "Can't mangle Objective-C selector names here!");
739 break;
740
741 case DeclarationName::CXXConstructorName:
Anders Carlsson27ae5362009-04-17 01:58:57 +0000742 if (ND == Structor)
Mike Stump141c5af2009-09-02 00:25:38 +0000743 // If the named decl is the C++ constructor we're mangling, use the type
744 // we were given.
Anders Carlsson27ae5362009-04-17 01:58:57 +0000745 mangleCXXCtorType(static_cast<CXXCtorType>(StructorType));
Anders Carlsson3ac86b52009-04-15 05:36:58 +0000746 else
747 // Otherwise, use the complete constructor name. This is relevant if a
748 // class with a constructor is declared within a constructor.
749 mangleCXXCtorType(Ctor_Complete);
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000750 break;
751
752 case DeclarationName::CXXDestructorName:
Anders Carlsson27ae5362009-04-17 01:58:57 +0000753 if (ND == Structor)
Mike Stump141c5af2009-09-02 00:25:38 +0000754 // If the named decl is the C++ destructor we're mangling, use the type we
755 // were given.
Anders Carlsson27ae5362009-04-17 01:58:57 +0000756 mangleCXXDtorType(static_cast<CXXDtorType>(StructorType));
757 else
758 // Otherwise, use the complete destructor name. This is relevant if a
759 // class with a destructor is declared within a destructor.
760 mangleCXXDtorType(Dtor_Complete);
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000761 break;
762
763 case DeclarationName::CXXConversionFunctionName:
Mike Stump1eb44332009-09-09 15:08:12 +0000764 // <operator-name> ::= cv <type> # (cast)
Douglas Gregor219cc612009-02-13 01:28:03 +0000765 Out << "cv";
Anders Carlssonb5404912009-10-07 01:06:45 +0000766 mangleType(Context.getASTContext().getCanonicalType(Name.getCXXNameType()));
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000767 break;
768
Anders Carlsson8257d412009-12-22 06:36:32 +0000769 case DeclarationName::CXXOperatorName: {
John McCall1dd73832010-02-04 01:42:13 +0000770 unsigned Arity;
771 if (ND) {
772 Arity = cast<FunctionDecl>(ND)->getNumParams();
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000773
John McCall1dd73832010-02-04 01:42:13 +0000774 // If we have a C++ member function, we need to include the 'this' pointer.
775 // FIXME: This does not make sense for operators that are static, but their
776 // names stay the same regardless of the arity (operator new for instance).
777 if (isa<CXXMethodDecl>(ND))
778 Arity++;
779 } else
780 Arity = KnownArity;
781
Anders Carlsson8257d412009-12-22 06:36:32 +0000782 mangleOperatorName(Name.getCXXOverloadedOperator(), Arity);
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000783 break;
Anders Carlsson8257d412009-12-22 06:36:32 +0000784 }
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000785
Sean Hunt3e518bd2009-11-29 07:34:05 +0000786 case DeclarationName::CXXLiteralOperatorName:
Sean Hunt5dd6b392009-12-04 21:11:13 +0000787 // FIXME: This mangling is not yet official.
Sean Hunt2421f662009-12-04 21:01:37 +0000788 Out << "li";
Sean Hunt3e518bd2009-11-29 07:34:05 +0000789 mangleSourceName(Name.getCXXLiteralIdentifier());
790 break;
791
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000792 case DeclarationName::CXXUsingDirective:
793 assert(false && "Can't mangle a using directive name!");
Douglas Gregor219cc612009-02-13 01:28:03 +0000794 break;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000795 }
796}
797
798void CXXNameMangler::mangleSourceName(const IdentifierInfo *II) {
799 // <source-name> ::= <positive length number> <identifier>
800 // <number> ::= [n] <non-negative decimal integer>
801 // <identifier> ::= <unqualified source code identifier>
802 Out << II->getLength() << II->getName();
803}
804
Eli Friedman7facf842009-12-02 20:32:49 +0000805void CXXNameMangler::mangleNestedName(const NamedDecl *ND,
Fariborz Jahanian57058532010-03-03 19:41:08 +0000806 const DeclContext *DC,
807 bool NoFunction) {
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000808 // <nested-name> ::= N [<CV-qualifiers>] <prefix> <unqualified-name> E
809 // ::= N [<CV-qualifiers>] <template-prefix> <template-args> E
Anders Carlssond99edc42009-09-26 03:55:37 +0000810
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000811 Out << 'N';
812 if (const CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(ND))
John McCall0953e762009-09-24 19:53:00 +0000813 mangleQualifiers(Qualifiers::fromCVRMask(Method->getTypeQualifiers()));
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000814
Anders Carlsson2744a062009-09-18 19:00:18 +0000815 // Check if we have a template.
816 const TemplateArgumentList *TemplateArgs = 0;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000817 if (const TemplateDecl *TD = isTemplate(ND, TemplateArgs)) {
Anders Carlsson2744a062009-09-18 19:00:18 +0000818 mangleTemplatePrefix(TD);
Rafael Espindolad9800722010-03-11 14:07:00 +0000819 TemplateParameterList *TemplateParameters = TD->getTemplateParameters();
820 mangleTemplateArgs(*TemplateParameters, *TemplateArgs);
Fariborz Jahanian57058532010-03-03 19:41:08 +0000821 }
822 else {
823 manglePrefix(DC, NoFunction);
Anders Carlsson7482e242009-09-18 04:29:09 +0000824 mangleUnqualifiedName(ND);
825 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000826
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000827 Out << 'E';
828}
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000829void CXXNameMangler::mangleNestedName(const TemplateDecl *TD,
Anders Carlsson7624f212009-09-18 02:42:01 +0000830 const TemplateArgument *TemplateArgs,
831 unsigned NumTemplateArgs) {
Anders Carlssone45117b2009-09-27 19:53:49 +0000832 // <nested-name> ::= N [<CV-qualifiers>] <template-prefix> <template-args> E
833
Anders Carlsson7624f212009-09-18 02:42:01 +0000834 Out << 'N';
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000835
Anders Carlssone45117b2009-09-27 19:53:49 +0000836 mangleTemplatePrefix(TD);
Rafael Espindolad9800722010-03-11 14:07:00 +0000837 TemplateParameterList *TemplateParameters = TD->getTemplateParameters();
838 mangleTemplateArgs(*TemplateParameters, TemplateArgs, NumTemplateArgs);
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000839
Anders Carlsson7624f212009-09-18 02:42:01 +0000840 Out << 'E';
841}
842
Anders Carlsson1b42c792009-04-02 16:24:45 +0000843void CXXNameMangler::mangleLocalName(const NamedDecl *ND) {
844 // <local-name> := Z <function encoding> E <entity name> [<discriminator>]
845 // := Z <function encoding> E s [<discriminator>]
Mike Stump1eb44332009-09-09 15:08:12 +0000846 // <discriminator> := _ <non-negative number>
Fariborz Jahanian57058532010-03-03 19:41:08 +0000847 const DeclContext *DC = ND->getDeclContext();
Anders Carlsson1b42c792009-04-02 16:24:45 +0000848 Out << 'Z';
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000849
Charles Davis685b1d92010-05-26 18:25:27 +0000850 if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(DC)) {
851 mangleObjCMethodName(MD);
852 }
Fariborz Jahanian57058532010-03-03 19:41:08 +0000853 else if (const DeclContext *CDC = GetLocalClassFunctionDeclContext(DC)) {
854 mangleFunctionEncoding(cast<FunctionDecl>(CDC));
855 Out << 'E';
856 mangleNestedName(ND, DC, true /*NoFunction*/);
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000857
Fariborz Jahanian4819ac42010-03-04 01:02:03 +0000858 // FIXME. This still does not cover all cases.
859 unsigned disc;
860 if (Context.getNextDiscriminator(ND, disc)) {
861 if (disc < 10)
862 Out << '_' << disc;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000863 else
Fariborz Jahanian4819ac42010-03-04 01:02:03 +0000864 Out << "__" << disc << '_';
865 }
Fariborz Jahanian57058532010-03-03 19:41:08 +0000866
867 return;
868 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000869 else
Fariborz Jahanian57058532010-03-03 19:41:08 +0000870 mangleFunctionEncoding(cast<FunctionDecl>(DC));
Anders Carlsson7b06f6c2009-12-10 03:14:39 +0000871
Anders Carlsson1b42c792009-04-02 16:24:45 +0000872 Out << 'E';
Eli Friedman6f9f25d2009-12-11 20:21:38 +0000873 mangleUnqualifiedName(ND);
Anders Carlsson1b42c792009-04-02 16:24:45 +0000874}
875
Fariborz Jahanian57058532010-03-03 19:41:08 +0000876void CXXNameMangler::manglePrefix(const DeclContext *DC, bool NoFunction) {
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000877 // <prefix> ::= <prefix> <unqualified-name>
878 // ::= <template-prefix> <template-args>
879 // ::= <template-param>
880 // ::= # empty
881 // ::= <substitution>
Anders Carlsson6862fc72009-09-17 04:16:28 +0000882
Anders Carlssonadd28822009-09-22 20:33:31 +0000883 while (isa<LinkageSpecDecl>(DC))
884 DC = DC->getParent();
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000885
Anders Carlsson9263e912009-09-18 18:39:58 +0000886 if (DC->isTranslationUnit())
887 return;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000888
Douglas Gregor35415f52010-05-25 17:04:15 +0000889 if (const BlockDecl *Block = dyn_cast<BlockDecl>(DC)) {
890 manglePrefix(DC->getParent(), NoFunction);
891 llvm::SmallString<64> Name;
Fariborz Jahanian564360b2010-06-24 00:08:06 +0000892 Context.mangleBlock(GlobalDecl(), Block, Name);
Douglas Gregor35415f52010-05-25 17:04:15 +0000893 Out << Name.size() << Name;
894 return;
895 }
896
Anders Carlsson6862fc72009-09-17 04:16:28 +0000897 if (mangleSubstitution(cast<NamedDecl>(DC)))
898 return;
Anders Carlsson7482e242009-09-18 04:29:09 +0000899
Anders Carlsson2ee3fca2009-09-18 20:11:09 +0000900 // Check if we have a template.
901 const TemplateArgumentList *TemplateArgs = 0;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000902 if (const TemplateDecl *TD = isTemplate(cast<NamedDecl>(DC), TemplateArgs)) {
Anders Carlsson2ee3fca2009-09-18 20:11:09 +0000903 mangleTemplatePrefix(TD);
Rafael Espindolad9800722010-03-11 14:07:00 +0000904 TemplateParameterList *TemplateParameters = TD->getTemplateParameters();
905 mangleTemplateArgs(*TemplateParameters, *TemplateArgs);
Fariborz Jahanian57058532010-03-03 19:41:08 +0000906 }
Douglas Gregor35415f52010-05-25 17:04:15 +0000907 else if(NoFunction && (isa<FunctionDecl>(DC) || isa<ObjCMethodDecl>(DC)))
Fariborz Jahanian57058532010-03-03 19:41:08 +0000908 return;
Douglas Gregor35415f52010-05-25 17:04:15 +0000909 else if (const ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(DC))
910 mangleObjCMethodName(Method);
Fariborz Jahanian57058532010-03-03 19:41:08 +0000911 else {
912 manglePrefix(DC->getParent(), NoFunction);
Anders Carlsson2ee3fca2009-09-18 20:11:09 +0000913 mangleUnqualifiedName(cast<NamedDecl>(DC));
914 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000915
Anders Carlsson6862fc72009-09-17 04:16:28 +0000916 addSubstitution(cast<NamedDecl>(DC));
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000917}
918
Douglas Gregor20f0cc72010-04-23 03:10:43 +0000919void CXXNameMangler::mangleTemplatePrefix(TemplateName Template) {
920 // <template-prefix> ::= <prefix> <template unqualified-name>
921 // ::= <template-param>
922 // ::= <substitution>
923 if (TemplateDecl *TD = Template.getAsTemplateDecl())
924 return mangleTemplatePrefix(TD);
925
926 if (QualifiedTemplateName *Qualified = Template.getAsQualifiedTemplateName())
927 mangleUnresolvedScope(Qualified->getQualifier());
Sean Huntc3021132010-05-05 15:23:54 +0000928
Douglas Gregor20f0cc72010-04-23 03:10:43 +0000929 if (OverloadedTemplateStorage *Overloaded
930 = Template.getAsOverloadedTemplate()) {
Sean Huntc3021132010-05-05 15:23:54 +0000931 mangleUnqualifiedName(0, (*Overloaded->begin())->getDeclName(),
Douglas Gregor20f0cc72010-04-23 03:10:43 +0000932 UnknownArity);
933 return;
934 }
Sean Huntc3021132010-05-05 15:23:54 +0000935
Douglas Gregor20f0cc72010-04-23 03:10:43 +0000936 DependentTemplateName *Dependent = Template.getAsDependentTemplateName();
937 assert(Dependent && "Unknown template name kind?");
938 mangleUnresolvedScope(Dependent->getQualifier());
Douglas Gregor1e9268e2010-04-28 05:58:56 +0000939 mangleUnscopedTemplateName(Template);
Douglas Gregor20f0cc72010-04-23 03:10:43 +0000940}
941
Anders Carlsson0fa6df42009-09-26 19:45:45 +0000942void CXXNameMangler::mangleTemplatePrefix(const TemplateDecl *ND) {
Anders Carlsson7482e242009-09-18 04:29:09 +0000943 // <template-prefix> ::= <prefix> <template unqualified-name>
944 // ::= <template-param>
945 // ::= <substitution>
Douglas Gregor32fb4e12010-02-05 20:45:00 +0000946 // <template-template-param> ::= <template-param>
947 // <substitution>
Anders Carlsson7482e242009-09-18 04:29:09 +0000948
Anders Carlssonaeb85372009-09-26 22:18:22 +0000949 if (mangleSubstitution(ND))
950 return;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000951
Douglas Gregor32fb4e12010-02-05 20:45:00 +0000952 // <template-template-param> ::= <template-param>
953 if (const TemplateTemplateParmDecl *TTP
954 = dyn_cast<TemplateTemplateParmDecl>(ND)) {
955 mangleTemplateParameter(TTP->getIndex());
956 return;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000957 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000958
Anders Carlssonaa73ab12009-09-18 18:47:07 +0000959 manglePrefix(ND->getDeclContext());
Anders Carlsson1668f202009-09-26 20:13:56 +0000960 mangleUnqualifiedName(ND->getTemplatedDecl());
Anders Carlssonaeb85372009-09-26 22:18:22 +0000961 addSubstitution(ND);
Anders Carlsson7482e242009-09-18 04:29:09 +0000962}
963
John McCallb6f532e2010-07-14 06:43:17 +0000964/// Mangles a template name under the production <type>. Required for
965/// template template arguments.
966/// <type> ::= <class-enum-type>
967/// ::= <template-param>
968/// ::= <substitution>
969void CXXNameMangler::mangleType(TemplateName TN) {
970 if (mangleSubstitution(TN))
971 return;
972
973 TemplateDecl *TD = 0;
974
975 switch (TN.getKind()) {
976 case TemplateName::QualifiedTemplate:
977 TD = TN.getAsQualifiedTemplateName()->getTemplateDecl();
978 goto HaveDecl;
979
980 case TemplateName::Template:
981 TD = TN.getAsTemplateDecl();
982 goto HaveDecl;
983
984 HaveDecl:
985 if (isa<TemplateTemplateParmDecl>(TD))
986 mangleTemplateParameter(cast<TemplateTemplateParmDecl>(TD)->getIndex());
987 else
988 mangleName(TD);
989 break;
990
991 case TemplateName::OverloadedTemplate:
992 llvm_unreachable("can't mangle an overloaded template name as a <type>");
993 break;
994
995 case TemplateName::DependentTemplate: {
996 const DependentTemplateName *Dependent = TN.getAsDependentTemplateName();
997 assert(Dependent->isIdentifier());
998
999 // <class-enum-type> ::= <name>
1000 // <name> ::= <nested-name>
1001 mangleUnresolvedScope(Dependent->getQualifier());
1002 mangleSourceName(Dependent->getIdentifier());
1003 break;
1004 }
1005
1006 }
1007
1008 addSubstitution(TN);
1009}
1010
Mike Stump1eb44332009-09-09 15:08:12 +00001011void
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001012CXXNameMangler::mangleOperatorName(OverloadedOperatorKind OO, unsigned Arity) {
1013 switch (OO) {
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001014 // <operator-name> ::= nw # new
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001015 case OO_New: Out << "nw"; break;
1016 // ::= na # new[]
1017 case OO_Array_New: Out << "na"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001018 // ::= dl # delete
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001019 case OO_Delete: Out << "dl"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001020 // ::= da # delete[]
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001021 case OO_Array_Delete: Out << "da"; break;
1022 // ::= ps # + (unary)
1023 // ::= pl # +
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001024 case OO_Plus:
Anders Carlsson8257d412009-12-22 06:36:32 +00001025 assert((Arity == 1 || Arity == 2) && "Invalid arity!");
1026 Out << (Arity == 1? "ps" : "pl"); break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001027 // ::= ng # - (unary)
1028 // ::= mi # -
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001029 case OO_Minus:
Anders Carlsson8257d412009-12-22 06:36:32 +00001030 assert((Arity == 1 || Arity == 2) && "Invalid arity!");
1031 Out << (Arity == 1? "ng" : "mi"); break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001032 // ::= ad # & (unary)
1033 // ::= an # &
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001034 case OO_Amp:
Anders Carlsson8257d412009-12-22 06:36:32 +00001035 assert((Arity == 1 || Arity == 2) && "Invalid arity!");
1036 Out << (Arity == 1? "ad" : "an"); break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001037 // ::= de # * (unary)
1038 // ::= ml # *
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001039 case OO_Star:
Anders Carlsson8257d412009-12-22 06:36:32 +00001040 assert((Arity == 1 || Arity == 2) && "Invalid arity!");
1041 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 McCall1dd73832010-02-04 01:42:13 +00001543void CXXNameMangler::mangleCalledExpression(const Expr *E, unsigned Arity) {
1544 if (E->getType() != getASTContext().OverloadTy)
1545 mangleExpression(E);
John McCall2f27bf82010-02-04 02:56:29 +00001546 // propagate arity to dependent overloads?
John McCall1dd73832010-02-04 01:42:13 +00001547
1548 llvm::PointerIntPair<OverloadExpr*,1> R
1549 = OverloadExpr::find(const_cast<Expr*>(E));
1550 if (R.getInt())
1551 Out << "an"; // &
1552 const OverloadExpr *Ovl = R.getPointer();
John McCall2f27bf82010-02-04 02:56:29 +00001553 if (const UnresolvedMemberExpr *ME = dyn_cast<UnresolvedMemberExpr>(Ovl)) {
1554 mangleMemberExpr(ME->getBase(), ME->isArrow(), ME->getQualifier(),
1555 ME->getMemberName(), Arity);
1556 return;
1557 }
John McCall1dd73832010-02-04 01:42:13 +00001558
1559 mangleUnresolvedName(Ovl->getQualifier(), Ovl->getName(), Arity);
1560}
1561
John McCall2f27bf82010-02-04 02:56:29 +00001562/// Mangles a member expression. Implicit accesses are not handled,
1563/// but that should be okay, because you shouldn't be able to
1564/// make an implicit access in a function template declaration.
John McCall2f27bf82010-02-04 02:56:29 +00001565void CXXNameMangler::mangleMemberExpr(const Expr *Base,
1566 bool IsArrow,
1567 NestedNameSpecifier *Qualifier,
1568 DeclarationName Member,
1569 unsigned Arity) {
John McCalle1e342f2010-03-01 19:12:25 +00001570 // gcc-4.4 uses 'dt' for dot expressions, which is reasonable.
1571 // OTOH, gcc also mangles the name as an expression.
1572 Out << (IsArrow ? "pt" : "dt");
John McCall2f27bf82010-02-04 02:56:29 +00001573 mangleExpression(Base);
1574 mangleUnresolvedName(Qualifier, Member, Arity);
1575}
1576
Anders Carlssond553f8c2009-09-21 01:21:10 +00001577void CXXNameMangler::mangleExpression(const Expr *E) {
1578 // <expression> ::= <unary operator-name> <expression>
John McCall09cc1412010-02-03 00:55:45 +00001579 // ::= <binary operator-name> <expression> <expression>
1580 // ::= <trinary operator-name> <expression> <expression> <expression>
1581 // ::= cl <expression>* E # call
Anders Carlssond553f8c2009-09-21 01:21:10 +00001582 // ::= cv <type> expression # conversion with one argument
1583 // ::= cv <type> _ <expression>* E # conversion with a different number of arguments
John McCall09cc1412010-02-03 00:55:45 +00001584 // ::= st <type> # sizeof (a type)
Anders Carlssond553f8c2009-09-21 01:21:10 +00001585 // ::= at <type> # alignof (a type)
1586 // ::= <template-param>
1587 // ::= <function-param>
1588 // ::= sr <type> <unqualified-name> # dependent name
1589 // ::= sr <type> <unqualified-name> <template-args> # dependent template-id
1590 // ::= sZ <template-param> # size of a parameter pack
John McCall09cc1412010-02-03 00:55:45 +00001591 // ::= <expr-primary>
John McCall1dd73832010-02-04 01:42:13 +00001592 // <expr-primary> ::= L <type> <value number> E # integer literal
1593 // ::= L <type <value float> E # floating literal
1594 // ::= L <mangled-name> E # external name
Anders Carlssond553f8c2009-09-21 01:21:10 +00001595 switch (E->getStmtClass()) {
John McCall6ae1f352010-04-09 22:26:14 +00001596 case Expr::NoStmtClass:
1597#define EXPR(Type, Base)
1598#define STMT(Type, Base) \
1599 case Expr::Type##Class:
Sean Hunt4bfe1962010-05-05 15:24:00 +00001600#include "clang/AST/StmtNodes.inc"
John McCall0512e482010-07-14 04:20:34 +00001601 // fallthrough
1602
1603 // These all can only appear in local or variable-initialization
1604 // contexts and so should never appear in a mangling.
1605 case Expr::AddrLabelExprClass:
1606 case Expr::BlockDeclRefExprClass:
1607 case Expr::CXXThisExprClass:
1608 case Expr::DesignatedInitExprClass:
1609 case Expr::ImplicitValueInitExprClass:
1610 case Expr::InitListExprClass:
1611 case Expr::ParenListExprClass:
1612 case Expr::CXXScalarValueInitExprClass:
John McCall09cc1412010-02-03 00:55:45 +00001613 llvm_unreachable("unexpected statement kind");
1614 break;
1615
John McCall0512e482010-07-14 04:20:34 +00001616 // FIXME: invent manglings for all these.
1617 case Expr::BlockExprClass:
1618 case Expr::CXXPseudoDestructorExprClass:
1619 case Expr::ChooseExprClass:
1620 case Expr::CompoundLiteralExprClass:
1621 case Expr::ExtVectorElementExprClass:
1622 case Expr::ObjCEncodeExprClass:
1623 case Expr::ObjCImplicitSetterGetterRefExprClass:
1624 case Expr::ObjCIsaExprClass:
1625 case Expr::ObjCIvarRefExprClass:
1626 case Expr::ObjCMessageExprClass:
1627 case Expr::ObjCPropertyRefExprClass:
1628 case Expr::ObjCProtocolExprClass:
1629 case Expr::ObjCSelectorExprClass:
1630 case Expr::ObjCStringLiteralClass:
1631 case Expr::ObjCSuperExprClass:
1632 case Expr::OffsetOfExprClass:
1633 case Expr::PredefinedExprClass:
1634 case Expr::ShuffleVectorExprClass:
1635 case Expr::StmtExprClass:
1636 case Expr::TypesCompatibleExprClass:
1637 case Expr::UnaryTypeTraitExprClass:
1638 case Expr::VAArgExprClass: {
John McCall6ae1f352010-04-09 22:26:14 +00001639 // As bad as this diagnostic is, it's better than crashing.
1640 Diagnostic &Diags = Context.getDiags();
1641 unsigned DiagID = Diags.getCustomDiagID(Diagnostic::Error,
1642 "cannot yet mangle expression type %0");
John McCall739bf092010-04-10 09:39:25 +00001643 Diags.Report(FullSourceLoc(E->getExprLoc(),
1644 getASTContext().getSourceManager()),
1645 DiagID)
1646 << E->getStmtClassName() << E->getSourceRange();
John McCall6ae1f352010-04-09 22:26:14 +00001647 break;
1648 }
1649
John McCall0512e482010-07-14 04:20:34 +00001650 case Expr::CXXDefaultArgExprClass:
1651 mangleExpression(cast<CXXDefaultArgExpr>(E)->getExpr());
1652 break;
1653
1654 case Expr::CXXMemberCallExprClass: // fallthrough
John McCall1dd73832010-02-04 01:42:13 +00001655 case Expr::CallExprClass: {
1656 const CallExpr *CE = cast<CallExpr>(E);
1657 Out << "cl";
1658 mangleCalledExpression(CE->getCallee(), CE->getNumArgs());
1659 for (unsigned I = 0, N = CE->getNumArgs(); I != N; ++I)
1660 mangleExpression(CE->getArg(I));
Benjamin Kramer35f59b62010-04-10 16:03:31 +00001661 Out << 'E';
John McCall09cc1412010-02-03 00:55:45 +00001662 break;
John McCall1dd73832010-02-04 01:42:13 +00001663 }
John McCall09cc1412010-02-03 00:55:45 +00001664
John McCall0512e482010-07-14 04:20:34 +00001665 case Expr::CXXNewExprClass: {
1666 // Proposal from David Vandervoorde, 2010.06.30
1667 const CXXNewExpr *New = cast<CXXNewExpr>(E);
1668 if (New->isGlobalNew()) Out << "gs";
1669 Out << (New->isArray() ? "na" : "nw");
1670 for (CXXNewExpr::const_arg_iterator I = New->placement_arg_begin(),
1671 E = New->placement_arg_end(); I != E; ++I)
1672 mangleExpression(*I);
1673 Out << '_';
1674 mangleType(New->getAllocatedType());
1675 if (New->hasInitializer()) {
1676 Out << "pi";
1677 for (CXXNewExpr::const_arg_iterator I = New->constructor_arg_begin(),
1678 E = New->constructor_arg_end(); I != E; ++I)
1679 mangleExpression(*I);
1680 }
1681 Out << 'E';
1682 break;
1683 }
1684
John McCall2f27bf82010-02-04 02:56:29 +00001685 case Expr::MemberExprClass: {
1686 const MemberExpr *ME = cast<MemberExpr>(E);
1687 mangleMemberExpr(ME->getBase(), ME->isArrow(),
1688 ME->getQualifier(), ME->getMemberDecl()->getDeclName(),
1689 UnknownArity);
1690 break;
1691 }
1692
1693 case Expr::UnresolvedMemberExprClass: {
1694 const UnresolvedMemberExpr *ME = cast<UnresolvedMemberExpr>(E);
1695 mangleMemberExpr(ME->getBase(), ME->isArrow(),
1696 ME->getQualifier(), ME->getMemberName(),
1697 UnknownArity);
1698 break;
1699 }
1700
1701 case Expr::CXXDependentScopeMemberExprClass: {
1702 const CXXDependentScopeMemberExpr *ME
1703 = cast<CXXDependentScopeMemberExpr>(E);
1704 mangleMemberExpr(ME->getBase(), ME->isArrow(),
1705 ME->getQualifier(), ME->getMember(),
1706 UnknownArity);
1707 break;
1708 }
1709
John McCall1dd73832010-02-04 01:42:13 +00001710 case Expr::UnresolvedLookupExprClass: {
John McCalla3218e72010-02-04 01:48:38 +00001711 // The ABI doesn't cover how to mangle overload sets, so we mangle
1712 // using something as close as possible to the original lookup
1713 // expression.
John McCall1dd73832010-02-04 01:42:13 +00001714 const UnresolvedLookupExpr *ULE = cast<UnresolvedLookupExpr>(E);
1715 mangleUnresolvedName(ULE->getQualifier(), ULE->getName(), UnknownArity);
1716 break;
1717 }
1718
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001719 case Expr::CXXUnresolvedConstructExprClass: {
John McCall1dd73832010-02-04 01:42:13 +00001720 const CXXUnresolvedConstructExpr *CE = cast<CXXUnresolvedConstructExpr>(E);
1721 unsigned N = CE->arg_size();
1722
1723 Out << "cv";
1724 mangleType(CE->getType());
Benjamin Kramer35f59b62010-04-10 16:03:31 +00001725 if (N != 1) Out << '_';
John McCall1dd73832010-02-04 01:42:13 +00001726 for (unsigned I = 0; I != N; ++I) mangleExpression(CE->getArg(I));
Benjamin Kramer35f59b62010-04-10 16:03:31 +00001727 if (N != 1) Out << 'E';
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001728 break;
John McCall1dd73832010-02-04 01:42:13 +00001729 }
John McCall09cc1412010-02-03 00:55:45 +00001730
John McCall1dd73832010-02-04 01:42:13 +00001731 case Expr::CXXTemporaryObjectExprClass:
1732 case Expr::CXXConstructExprClass: {
1733 const CXXConstructExpr *CE = cast<CXXConstructExpr>(E);
1734 unsigned N = CE->getNumArgs();
1735
1736 Out << "cv";
1737 mangleType(CE->getType());
Benjamin Kramer35f59b62010-04-10 16:03:31 +00001738 if (N != 1) Out << '_';
John McCall1dd73832010-02-04 01:42:13 +00001739 for (unsigned I = 0; I != N; ++I) mangleExpression(CE->getArg(I));
Benjamin Kramer35f59b62010-04-10 16:03:31 +00001740 if (N != 1) Out << 'E';
John McCall09cc1412010-02-03 00:55:45 +00001741 break;
John McCall1dd73832010-02-04 01:42:13 +00001742 }
1743
1744 case Expr::SizeOfAlignOfExprClass: {
1745 const SizeOfAlignOfExpr *SAE = cast<SizeOfAlignOfExpr>(E);
Benjamin Kramer35f59b62010-04-10 16:03:31 +00001746 if (SAE->isSizeOf()) Out << 's';
1747 else Out << 'a';
John McCall1dd73832010-02-04 01:42:13 +00001748 if (SAE->isArgumentType()) {
Benjamin Kramer35f59b62010-04-10 16:03:31 +00001749 Out << 't';
John McCall1dd73832010-02-04 01:42:13 +00001750 mangleType(SAE->getArgumentType());
1751 } else {
Benjamin Kramer35f59b62010-04-10 16:03:31 +00001752 Out << 'z';
John McCall1dd73832010-02-04 01:42:13 +00001753 mangleExpression(SAE->getArgumentExpr());
1754 }
1755 break;
1756 }
Anders Carlssona7694082009-11-06 02:50:19 +00001757
John McCall0512e482010-07-14 04:20:34 +00001758 case Expr::CXXThrowExprClass: {
1759 const CXXThrowExpr *TE = cast<CXXThrowExpr>(E);
1760
1761 // Proposal from David Vandervoorde, 2010.06.30
1762 if (TE->getSubExpr()) {
1763 Out << "tw";
1764 mangleExpression(TE->getSubExpr());
1765 } else {
1766 Out << "tr";
1767 }
1768 break;
1769 }
1770
1771 case Expr::CXXTypeidExprClass: {
1772 const CXXTypeidExpr *TIE = cast<CXXTypeidExpr>(E);
1773
1774 // Proposal from David Vandervoorde, 2010.06.30
1775 if (TIE->isTypeOperand()) {
1776 Out << "ti";
1777 mangleType(TIE->getTypeOperand());
1778 } else {
1779 Out << "te";
1780 mangleExpression(TIE->getExprOperand());
1781 }
1782 break;
1783 }
1784
1785 case Expr::CXXDeleteExprClass: {
1786 const CXXDeleteExpr *DE = cast<CXXDeleteExpr>(E);
1787
1788 // Proposal from David Vandervoorde, 2010.06.30
1789 if (DE->isGlobalDelete()) Out << "gs";
1790 Out << (DE->isArrayForm() ? "da" : "dl");
1791 mangleExpression(DE->getArgument());
1792 break;
1793 }
1794
Anders Carlssone170ba72009-12-14 01:45:37 +00001795 case Expr::UnaryOperatorClass: {
1796 const UnaryOperator *UO = cast<UnaryOperator>(E);
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001797 mangleOperatorName(UnaryOperator::getOverloadedOperator(UO->getOpcode()),
Anders Carlssone170ba72009-12-14 01:45:37 +00001798 /*Arity=*/1);
1799 mangleExpression(UO->getSubExpr());
1800 break;
1801 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001802
John McCall0512e482010-07-14 04:20:34 +00001803 case Expr::ArraySubscriptExprClass: {
1804 const ArraySubscriptExpr *AE = cast<ArraySubscriptExpr>(E);
1805
1806 // Array subscript is treated as a syntactically wierd form of
1807 // binary operator.
1808 Out << "ix";
1809 mangleExpression(AE->getLHS());
1810 mangleExpression(AE->getRHS());
1811 break;
1812 }
1813
1814 case Expr::CompoundAssignOperatorClass: // fallthrough
Anders Carlssone170ba72009-12-14 01:45:37 +00001815 case Expr::BinaryOperatorClass: {
1816 const BinaryOperator *BO = cast<BinaryOperator>(E);
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001817 mangleOperatorName(BinaryOperator::getOverloadedOperator(BO->getOpcode()),
Anders Carlssone170ba72009-12-14 01:45:37 +00001818 /*Arity=*/2);
1819 mangleExpression(BO->getLHS());
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001820 mangleExpression(BO->getRHS());
Anders Carlssone170ba72009-12-14 01:45:37 +00001821 break;
John McCall2f27bf82010-02-04 02:56:29 +00001822 }
Anders Carlssone170ba72009-12-14 01:45:37 +00001823
1824 case Expr::ConditionalOperatorClass: {
1825 const ConditionalOperator *CO = cast<ConditionalOperator>(E);
1826 mangleOperatorName(OO_Conditional, /*Arity=*/3);
1827 mangleExpression(CO->getCond());
1828 mangleExpression(CO->getLHS());
1829 mangleExpression(CO->getRHS());
1830 break;
1831 }
1832
Douglas Gregor46287c72010-01-29 16:37:09 +00001833 case Expr::ImplicitCastExprClass: {
1834 mangleExpression(cast<ImplicitCastExpr>(E)->getSubExpr());
1835 break;
1836 }
1837
1838 case Expr::CStyleCastExprClass:
1839 case Expr::CXXStaticCastExprClass:
1840 case Expr::CXXDynamicCastExprClass:
1841 case Expr::CXXReinterpretCastExprClass:
1842 case Expr::CXXConstCastExprClass:
1843 case Expr::CXXFunctionalCastExprClass: {
1844 const ExplicitCastExpr *ECE = cast<ExplicitCastExpr>(E);
1845 Out << "cv";
1846 mangleType(ECE->getType());
1847 mangleExpression(ECE->getSubExpr());
1848 break;
1849 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001850
Anders Carlsson58040a52009-12-16 05:48:46 +00001851 case Expr::CXXOperatorCallExprClass: {
1852 const CXXOperatorCallExpr *CE = cast<CXXOperatorCallExpr>(E);
1853 unsigned NumArgs = CE->getNumArgs();
1854 mangleOperatorName(CE->getOperator(), /*Arity=*/NumArgs);
1855 // Mangle the arguments.
1856 for (unsigned i = 0; i != NumArgs; ++i)
1857 mangleExpression(CE->getArg(i));
1858 break;
1859 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001860
Anders Carlssona7694082009-11-06 02:50:19 +00001861 case Expr::ParenExprClass:
1862 mangleExpression(cast<ParenExpr>(E)->getSubExpr());
1863 break;
1864
Anders Carlssond553f8c2009-09-21 01:21:10 +00001865 case Expr::DeclRefExprClass: {
Douglas Gregor5ed1bc32010-02-28 21:40:32 +00001866 const NamedDecl *D = cast<DeclRefExpr>(E)->getDecl();
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00001867
Anders Carlssond553f8c2009-09-21 01:21:10 +00001868 switch (D->getKind()) {
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001869 default:
Douglas Gregor5ed1bc32010-02-28 21:40:32 +00001870 // <expr-primary> ::= L <mangled-name> E # external name
1871 Out << 'L';
1872 mangle(D, "_Z");
1873 Out << 'E';
1874 break;
1875
John McCall3dc7e7b2010-07-24 01:17:35 +00001876 case Decl::EnumConstant: {
1877 const EnumConstantDecl *ED = cast<EnumConstantDecl>(D);
1878 mangleIntegerLiteral(ED->getType(), ED->getInitVal());
1879 break;
1880 }
1881
Anders Carlssond553f8c2009-09-21 01:21:10 +00001882 case Decl::NonTypeTemplateParm: {
1883 const NonTypeTemplateParmDecl *PD = cast<NonTypeTemplateParmDecl>(D);
Anders Carlsson0ccdf8d2009-09-27 00:38:53 +00001884 mangleTemplateParameter(PD->getIndex());
Anders Carlssond553f8c2009-09-21 01:21:10 +00001885 break;
1886 }
1887
1888 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00001889
Anders Carlsson50755b02009-09-27 20:11:34 +00001890 break;
Anders Carlssond553f8c2009-09-21 01:21:10 +00001891 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00001892
John McCall865d4472009-11-19 22:55:06 +00001893 case Expr::DependentScopeDeclRefExprClass: {
1894 const DependentScopeDeclRefExpr *DRE = cast<DependentScopeDeclRefExpr>(E);
Douglas Gregor4b2ccfc2010-02-28 22:05:49 +00001895 NestedNameSpecifier *NNS = DRE->getQualifier();
1896 const Type *QTy = NNS->getAsType();
1897
1898 // When we're dealing with a nested-name-specifier that has just a
1899 // dependent identifier in it, mangle that as a typename. FIXME:
1900 // It isn't clear that we ever actually want to have such a
1901 // nested-name-specifier; why not just represent it as a typename type?
1902 if (!QTy && NNS->getAsIdentifier() && NNS->getPrefix()) {
Douglas Gregor4a2023f2010-03-31 20:19:30 +00001903 QTy = getASTContext().getDependentNameType(ETK_Typename,
1904 NNS->getPrefix(),
1905 NNS->getAsIdentifier())
Douglas Gregor4b2ccfc2010-02-28 22:05:49 +00001906 .getTypePtr();
1907 }
Anders Carlsson50755b02009-09-27 20:11:34 +00001908 assert(QTy && "Qualifier was not type!");
1909
1910 // ::= sr <type> <unqualified-name> # dependent name
1911 Out << "sr";
1912 mangleType(QualType(QTy, 0));
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00001913
Anders Carlsson50755b02009-09-27 20:11:34 +00001914 assert(DRE->getDeclName().getNameKind() == DeclarationName::Identifier &&
1915 "Unhandled decl name kind!");
1916 mangleSourceName(DRE->getDeclName().getAsIdentifierInfo());
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00001917
Anders Carlsson50755b02009-09-27 20:11:34 +00001918 break;
1919 }
1920
John McCalld9307602010-04-09 22:54:09 +00001921 case Expr::CXXBindReferenceExprClass:
1922 mangleExpression(cast<CXXBindReferenceExpr>(E)->getSubExpr());
1923 break;
1924
1925 case Expr::CXXBindTemporaryExprClass:
1926 mangleExpression(cast<CXXBindTemporaryExpr>(E)->getSubExpr());
1927 break;
1928
1929 case Expr::CXXExprWithTemporariesClass:
1930 mangleExpression(cast<CXXExprWithTemporaries>(E)->getSubExpr());
1931 break;
1932
John McCall1dd73832010-02-04 01:42:13 +00001933 case Expr::FloatingLiteralClass: {
1934 const FloatingLiteral *FL = cast<FloatingLiteral>(E);
Benjamin Kramer35f59b62010-04-10 16:03:31 +00001935 Out << 'L';
John McCall1dd73832010-02-04 01:42:13 +00001936 mangleType(FL->getType());
John McCall0512e482010-07-14 04:20:34 +00001937 mangleFloat(FL->getValue());
Benjamin Kramer35f59b62010-04-10 16:03:31 +00001938 Out << 'E';
John McCall1dd73832010-02-04 01:42:13 +00001939 break;
1940 }
1941
John McCallde810632010-04-09 21:48:08 +00001942 case Expr::CharacterLiteralClass:
Benjamin Kramer35f59b62010-04-10 16:03:31 +00001943 Out << 'L';
John McCallde810632010-04-09 21:48:08 +00001944 mangleType(E->getType());
1945 Out << cast<CharacterLiteral>(E)->getValue();
1946 Out << 'E';
1947 break;
1948
1949 case Expr::CXXBoolLiteralExprClass:
1950 Out << "Lb";
1951 Out << (cast<CXXBoolLiteralExpr>(E)->getValue() ? '1' : '0');
1952 Out << 'E';
1953 break;
1954
John McCall0512e482010-07-14 04:20:34 +00001955 case Expr::IntegerLiteralClass: {
1956 llvm::APSInt Value(cast<IntegerLiteral>(E)->getValue());
1957 if (E->getType()->isSignedIntegerType())
1958 Value.setIsSigned(true);
1959 mangleIntegerLiteral(E->getType(), Value);
Anders Carlssone170ba72009-12-14 01:45:37 +00001960 break;
John McCall0512e482010-07-14 04:20:34 +00001961 }
1962
1963 case Expr::ImaginaryLiteralClass: {
1964 const ImaginaryLiteral *IE = cast<ImaginaryLiteral>(E);
1965 // Mangle as if a complex literal.
1966 // Proposal from David Vandervoorde, 2010.06.30.
1967 Out << 'L';
1968 mangleType(E->getType());
1969 if (const FloatingLiteral *Imag =
1970 dyn_cast<FloatingLiteral>(IE->getSubExpr())) {
1971 // Mangle a floating-point zero of the appropriate type.
1972 mangleFloat(llvm::APFloat(Imag->getValue().getSemantics()));
1973 Out << '_';
1974 mangleFloat(Imag->getValue());
1975 } else {
1976 Out << '0' << '_';
1977 llvm::APSInt Value(cast<IntegerLiteral>(IE->getSubExpr())->getValue());
1978 if (IE->getSubExpr()->getType()->isSignedIntegerType())
1979 Value.setIsSigned(true);
1980 mangleNumber(Value);
1981 }
1982 Out << 'E';
1983 break;
1984 }
1985
1986 case Expr::StringLiteralClass: {
John McCall1658c392010-07-15 21:53:03 +00001987 // Revised proposal from David Vandervoorde, 2010.07.15.
John McCall0512e482010-07-14 04:20:34 +00001988 Out << 'L';
John McCall1658c392010-07-15 21:53:03 +00001989 assert(isa<ConstantArrayType>(E->getType()));
1990 mangleType(E->getType());
John McCall0512e482010-07-14 04:20:34 +00001991 Out << 'E';
1992 break;
1993 }
1994
1995 case Expr::GNUNullExprClass:
1996 // FIXME: should this really be mangled the same as nullptr?
1997 // fallthrough
1998
1999 case Expr::CXXNullPtrLiteralExprClass: {
2000 // Proposal from David Vandervoorde, 2010.06.30, as
2001 // modified by ABI list discussion.
2002 Out << "LDnE";
2003 break;
2004 }
Anders Carlssone170ba72009-12-14 01:45:37 +00002005
Anders Carlssond553f8c2009-09-21 01:21:10 +00002006 }
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00002007}
2008
Anders Carlsson3ac86b52009-04-15 05:36:58 +00002009void CXXNameMangler::mangleCXXCtorType(CXXCtorType T) {
2010 // <ctor-dtor-name> ::= C1 # complete object constructor
2011 // ::= C2 # base object constructor
2012 // ::= C3 # complete object allocating constructor
2013 //
2014 switch (T) {
2015 case Ctor_Complete:
2016 Out << "C1";
2017 break;
2018 case Ctor_Base:
2019 Out << "C2";
2020 break;
2021 case Ctor_CompleteAllocating:
2022 Out << "C3";
2023 break;
2024 }
2025}
2026
Anders Carlsson27ae5362009-04-17 01:58:57 +00002027void CXXNameMangler::mangleCXXDtorType(CXXDtorType T) {
2028 // <ctor-dtor-name> ::= D0 # deleting destructor
2029 // ::= D1 # complete object destructor
2030 // ::= D2 # base object destructor
2031 //
2032 switch (T) {
2033 case Dtor_Deleting:
2034 Out << "D0";
2035 break;
2036 case Dtor_Complete:
2037 Out << "D1";
2038 break;
2039 case Dtor_Base:
2040 Out << "D2";
2041 break;
2042 }
2043}
2044
Douglas Gregor20f0cc72010-04-23 03:10:43 +00002045void CXXNameMangler::mangleTemplateArgs(TemplateName Template,
2046 const TemplateArgument *TemplateArgs,
2047 unsigned NumTemplateArgs) {
2048 if (TemplateDecl *TD = Template.getAsTemplateDecl())
2049 return mangleTemplateArgs(*TD->getTemplateParameters(), TemplateArgs,
2050 NumTemplateArgs);
Sean Huntc3021132010-05-05 15:23:54 +00002051
Douglas Gregor20f0cc72010-04-23 03:10:43 +00002052 // <template-args> ::= I <template-arg>+ E
2053 Out << 'I';
2054 for (unsigned i = 0; i != NumTemplateArgs; ++i)
2055 mangleTemplateArg(0, TemplateArgs[i]);
2056 Out << 'E';
2057}
2058
Rafael Espindolad9800722010-03-11 14:07:00 +00002059void CXXNameMangler::mangleTemplateArgs(const TemplateParameterList &PL,
2060 const TemplateArgumentList &AL) {
Anders Carlsson7a0ba872009-05-15 16:09:15 +00002061 // <template-args> ::= I <template-arg>+ E
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002062 Out << 'I';
Rafael Espindolad9800722010-03-11 14:07:00 +00002063 for (unsigned i = 0, e = AL.size(); i != e; ++i)
2064 mangleTemplateArg(PL.getParam(i), AL[i]);
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002065 Out << 'E';
Anders Carlsson7a0ba872009-05-15 16:09:15 +00002066}
2067
Rafael Espindolad9800722010-03-11 14:07:00 +00002068void CXXNameMangler::mangleTemplateArgs(const TemplateParameterList &PL,
2069 const TemplateArgument *TemplateArgs,
Anders Carlsson7624f212009-09-18 02:42:01 +00002070 unsigned NumTemplateArgs) {
2071 // <template-args> ::= I <template-arg>+ E
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002072 Out << 'I';
Daniel Dunbar7e0c1952009-11-21 09:17:15 +00002073 for (unsigned i = 0; i != NumTemplateArgs; ++i)
Rafael Espindolad9800722010-03-11 14:07:00 +00002074 mangleTemplateArg(PL.getParam(i), TemplateArgs[i]);
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002075 Out << 'E';
Anders Carlsson7624f212009-09-18 02:42:01 +00002076}
2077
Rafael Espindolad9800722010-03-11 14:07:00 +00002078void CXXNameMangler::mangleTemplateArg(const NamedDecl *P,
2079 const TemplateArgument &A) {
Mike Stump1eb44332009-09-09 15:08:12 +00002080 // <template-arg> ::= <type> # type or template
Anders Carlsson7a0ba872009-05-15 16:09:15 +00002081 // ::= X <expression> E # expression
2082 // ::= <expr-primary> # simple expressions
2083 // ::= I <template-arg>* E # argument pack
2084 // ::= sp <expression> # pack expansion of (C++0x)
2085 switch (A.getKind()) {
2086 default:
2087 assert(0 && "Unknown template argument kind!");
2088 case TemplateArgument::Type:
2089 mangleType(A.getAsType());
2090 break;
Anders Carlsson9e85c742009-12-23 19:30:55 +00002091 case TemplateArgument::Template:
John McCallb6f532e2010-07-14 06:43:17 +00002092 // This is mangled as <type>.
2093 mangleType(A.getAsTemplate());
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002094 break;
Anders Carlssond553f8c2009-09-21 01:21:10 +00002095 case TemplateArgument::Expression:
2096 Out << 'X';
2097 mangleExpression(A.getAsExpr());
2098 Out << 'E';
2099 break;
Anders Carlssone170ba72009-12-14 01:45:37 +00002100 case TemplateArgument::Integral:
2101 mangleIntegerLiteral(A.getIntegralType(), *A.getAsIntegral());
Anders Carlsson7a0ba872009-05-15 16:09:15 +00002102 break;
Daniel Dunbar7e0c1952009-11-21 09:17:15 +00002103 case TemplateArgument::Declaration: {
Douglas Gregor20f0cc72010-04-23 03:10:43 +00002104 assert(P && "Missing template parameter for declaration argument");
Daniel Dunbar7e0c1952009-11-21 09:17:15 +00002105 // <expr-primary> ::= L <mangled-name> E # external name
2106
Rafael Espindolad9800722010-03-11 14:07:00 +00002107 // Clang produces AST's where pointer-to-member-function expressions
Daniel Dunbar7e0c1952009-11-21 09:17:15 +00002108 // and pointer-to-function expressions are represented as a declaration not
Rafael Espindolad9800722010-03-11 14:07:00 +00002109 // an expression. We compensate for it here to produce the correct mangling.
2110 NamedDecl *D = cast<NamedDecl>(A.getAsDecl());
2111 const NonTypeTemplateParmDecl *Parameter = cast<NonTypeTemplateParmDecl>(P);
2112 bool compensateMangling = D->isCXXClassMember() &&
2113 !Parameter->getType()->isReferenceType();
2114 if (compensateMangling) {
2115 Out << 'X';
2116 mangleOperatorName(OO_Amp, 1);
2117 }
2118
Daniel Dunbar7e0c1952009-11-21 09:17:15 +00002119 Out << 'L';
2120 // References to external entities use the mangled name; if the name would
2121 // not normally be manged then mangle it as unqualified.
2122 //
2123 // FIXME: The ABI specifies that external names here should have _Z, but
2124 // gcc leaves this off.
Rafael Espindolad9800722010-03-11 14:07:00 +00002125 if (compensateMangling)
2126 mangle(D, "_Z");
2127 else
2128 mangle(D, "Z");
Daniel Dunbar7e0c1952009-11-21 09:17:15 +00002129 Out << 'E';
Rafael Espindolad9800722010-03-11 14:07:00 +00002130
2131 if (compensateMangling)
2132 Out << 'E';
2133
Daniel Dunbar7e0c1952009-11-21 09:17:15 +00002134 break;
2135 }
2136 }
Anders Carlsson7a0ba872009-05-15 16:09:15 +00002137}
2138
Anders Carlsson0ccdf8d2009-09-27 00:38:53 +00002139void CXXNameMangler::mangleTemplateParameter(unsigned Index) {
2140 // <template-param> ::= T_ # first template parameter
2141 // ::= T <parameter-2 non-negative number> _
2142 if (Index == 0)
2143 Out << "T_";
2144 else
2145 Out << 'T' << (Index - 1) << '_';
2146}
2147
Anders Carlsson76967372009-09-17 00:43:46 +00002148// <substitution> ::= S <seq-id> _
2149// ::= S_
Anders Carlsson6862fc72009-09-17 04:16:28 +00002150bool CXXNameMangler::mangleSubstitution(const NamedDecl *ND) {
Anders Carlssone7c8cb62009-09-26 20:53:44 +00002151 // Try one of the standard substitutions first.
2152 if (mangleStandardSubstitution(ND))
2153 return true;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002154
Anders Carlsson433d1372009-11-07 04:26:04 +00002155 ND = cast<NamedDecl>(ND->getCanonicalDecl());
Anders Carlsson6862fc72009-09-17 04:16:28 +00002156 return mangleSubstitution(reinterpret_cast<uintptr_t>(ND));
2157}
2158
Anders Carlsson76967372009-09-17 00:43:46 +00002159bool CXXNameMangler::mangleSubstitution(QualType T) {
Anders Carlssond99edc42009-09-26 03:55:37 +00002160 if (!T.getCVRQualifiers()) {
2161 if (const RecordType *RT = T->getAs<RecordType>())
2162 return mangleSubstitution(RT->getDecl());
2163 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002164
Anders Carlsson76967372009-09-17 00:43:46 +00002165 uintptr_t TypePtr = reinterpret_cast<uintptr_t>(T.getAsOpaquePtr());
2166
Anders Carlssond3a932a2009-09-17 03:53:28 +00002167 return mangleSubstitution(TypePtr);
2168}
2169
Douglas Gregor1e9268e2010-04-28 05:58:56 +00002170bool CXXNameMangler::mangleSubstitution(TemplateName Template) {
2171 if (TemplateDecl *TD = Template.getAsTemplateDecl())
2172 return mangleSubstitution(TD);
Sean Huntc3021132010-05-05 15:23:54 +00002173
Douglas Gregor1e9268e2010-04-28 05:58:56 +00002174 Template = Context.getASTContext().getCanonicalTemplateName(Template);
2175 return mangleSubstitution(
2176 reinterpret_cast<uintptr_t>(Template.getAsVoidPointer()));
2177}
2178
Anders Carlssond3a932a2009-09-17 03:53:28 +00002179bool CXXNameMangler::mangleSubstitution(uintptr_t Ptr) {
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002180 llvm::DenseMap<uintptr_t, unsigned>::iterator I = Substitutions.find(Ptr);
Anders Carlsson76967372009-09-17 00:43:46 +00002181 if (I == Substitutions.end())
2182 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002183
Anders Carlsson76967372009-09-17 00:43:46 +00002184 unsigned SeqID = I->second;
2185 if (SeqID == 0)
2186 Out << "S_";
2187 else {
2188 SeqID--;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002189
Anders Carlsson76967372009-09-17 00:43:46 +00002190 // <seq-id> is encoded in base-36, using digits and upper case letters.
2191 char Buffer[10];
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002192 char *BufferPtr = llvm::array_endof(Buffer);
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002193
Anders Carlsson76967372009-09-17 00:43:46 +00002194 if (SeqID == 0) *--BufferPtr = '0';
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002195
Anders Carlsson76967372009-09-17 00:43:46 +00002196 while (SeqID) {
2197 assert(BufferPtr > Buffer && "Buffer overflow!");
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002198
John McCall6ab30e02010-06-09 07:26:17 +00002199 char c = static_cast<char>(SeqID % 36);
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002200
Anders Carlsson76967372009-09-17 00:43:46 +00002201 *--BufferPtr = (c < 10 ? '0' + c : 'A' + c - 10);
2202 SeqID /= 36;
2203 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002204
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002205 Out << 'S'
2206 << llvm::StringRef(BufferPtr, llvm::array_endof(Buffer)-BufferPtr)
2207 << '_';
Anders Carlsson76967372009-09-17 00:43:46 +00002208 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002209
Anders Carlsson76967372009-09-17 00:43:46 +00002210 return true;
2211}
2212
Anders Carlssonf514b542009-09-27 00:12:57 +00002213static bool isCharType(QualType T) {
2214 if (T.isNull())
2215 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002216
Anders Carlssonf514b542009-09-27 00:12:57 +00002217 return T->isSpecificBuiltinType(BuiltinType::Char_S) ||
2218 T->isSpecificBuiltinType(BuiltinType::Char_U);
2219}
2220
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002221/// isCharSpecialization - Returns whether a given type is a template
Anders Carlssonf514b542009-09-27 00:12:57 +00002222/// specialization of a given name with a single argument of type char.
2223static bool isCharSpecialization(QualType T, const char *Name) {
2224 if (T.isNull())
2225 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002226
Anders Carlssonf514b542009-09-27 00:12:57 +00002227 const RecordType *RT = T->getAs<RecordType>();
2228 if (!RT)
2229 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002230
2231 const ClassTemplateSpecializationDecl *SD =
Anders Carlssonf514b542009-09-27 00:12:57 +00002232 dyn_cast<ClassTemplateSpecializationDecl>(RT->getDecl());
2233 if (!SD)
2234 return false;
2235
2236 if (!isStdNamespace(SD->getDeclContext()))
2237 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002238
Anders Carlssonf514b542009-09-27 00:12:57 +00002239 const TemplateArgumentList &TemplateArgs = SD->getTemplateArgs();
2240 if (TemplateArgs.size() != 1)
2241 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002242
Anders Carlssonf514b542009-09-27 00:12:57 +00002243 if (!isCharType(TemplateArgs[0].getAsType()))
2244 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002245
Daniel Dunbar01eb9b92009-10-18 21:17:35 +00002246 return SD->getIdentifier()->getName() == Name;
Anders Carlssonf514b542009-09-27 00:12:57 +00002247}
2248
Anders Carlsson91f88602009-12-07 19:56:42 +00002249template <std::size_t StrLen>
2250bool isStreamCharSpecialization(const ClassTemplateSpecializationDecl *SD,
2251 const char (&Str)[StrLen]) {
2252 if (!SD->getIdentifier()->isStr(Str))
2253 return false;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002254
Anders Carlsson91f88602009-12-07 19:56:42 +00002255 const TemplateArgumentList &TemplateArgs = SD->getTemplateArgs();
2256 if (TemplateArgs.size() != 2)
2257 return false;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002258
Anders Carlsson91f88602009-12-07 19:56:42 +00002259 if (!isCharType(TemplateArgs[0].getAsType()))
2260 return false;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002261
Anders Carlsson91f88602009-12-07 19:56:42 +00002262 if (!isCharSpecialization(TemplateArgs[1].getAsType(), "char_traits"))
2263 return false;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002264
Anders Carlsson91f88602009-12-07 19:56:42 +00002265 return true;
2266}
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002267
Anders Carlssone7c8cb62009-09-26 20:53:44 +00002268bool CXXNameMangler::mangleStandardSubstitution(const NamedDecl *ND) {
2269 // <substitution> ::= St # ::std::
Anders Carlsson8c031552009-09-26 23:10:05 +00002270 if (const NamespaceDecl *NS = dyn_cast<NamespaceDecl>(ND)) {
Anders Carlsson47846d22009-12-04 06:23:23 +00002271 if (isStd(NS)) {
Anders Carlsson8c031552009-09-26 23:10:05 +00002272 Out << "St";
2273 return true;
2274 }
2275 }
2276
2277 if (const ClassTemplateDecl *TD = dyn_cast<ClassTemplateDecl>(ND)) {
2278 if (!isStdNamespace(TD->getDeclContext()))
2279 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002280
Anders Carlsson8c031552009-09-26 23:10:05 +00002281 // <substitution> ::= Sa # ::std::allocator
2282 if (TD->getIdentifier()->isStr("allocator")) {
2283 Out << "Sa";
2284 return true;
2285 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002286
Anders Carlsson189d59c2009-09-26 23:14:39 +00002287 // <<substitution> ::= Sb # ::std::basic_string
2288 if (TD->getIdentifier()->isStr("basic_string")) {
2289 Out << "Sb";
2290 return true;
2291 }
Anders Carlsson8c031552009-09-26 23:10:05 +00002292 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002293
2294 if (const ClassTemplateSpecializationDecl *SD =
Anders Carlssonf514b542009-09-27 00:12:57 +00002295 dyn_cast<ClassTemplateSpecializationDecl>(ND)) {
Eli Friedman5370ee22010-02-23 18:25:09 +00002296 if (!isStdNamespace(SD->getDeclContext()))
2297 return false;
2298
Anders Carlssonf514b542009-09-27 00:12:57 +00002299 // <substitution> ::= Ss # ::std::basic_string<char,
2300 // ::std::char_traits<char>,
2301 // ::std::allocator<char> >
2302 if (SD->getIdentifier()->isStr("basic_string")) {
2303 const TemplateArgumentList &TemplateArgs = SD->getTemplateArgs();
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002304
Anders Carlssonf514b542009-09-27 00:12:57 +00002305 if (TemplateArgs.size() != 3)
2306 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002307
Anders Carlssonf514b542009-09-27 00:12:57 +00002308 if (!isCharType(TemplateArgs[0].getAsType()))
2309 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002310
Anders Carlssonf514b542009-09-27 00:12:57 +00002311 if (!isCharSpecialization(TemplateArgs[1].getAsType(), "char_traits"))
2312 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002313
Anders Carlssonf514b542009-09-27 00:12:57 +00002314 if (!isCharSpecialization(TemplateArgs[2].getAsType(), "allocator"))
2315 return false;
2316
2317 Out << "Ss";
2318 return true;
2319 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002320
Anders Carlsson91f88602009-12-07 19:56:42 +00002321 // <substitution> ::= Si # ::std::basic_istream<char,
2322 // ::std::char_traits<char> >
2323 if (isStreamCharSpecialization(SD, "basic_istream")) {
2324 Out << "Si";
2325 return true;
2326 }
2327
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002328 // <substitution> ::= So # ::std::basic_ostream<char,
Anders Carlsson8f8fd8e2009-10-08 17:20:26 +00002329 // ::std::char_traits<char> >
Anders Carlsson91f88602009-12-07 19:56:42 +00002330 if (isStreamCharSpecialization(SD, "basic_ostream")) {
Anders Carlsson8f8fd8e2009-10-08 17:20:26 +00002331 Out << "So";
2332 return true;
2333 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002334
Anders Carlsson91f88602009-12-07 19:56:42 +00002335 // <substitution> ::= Sd # ::std::basic_iostream<char,
2336 // ::std::char_traits<char> >
2337 if (isStreamCharSpecialization(SD, "basic_iostream")) {
2338 Out << "Sd";
2339 return true;
2340 }
Anders Carlssonf514b542009-09-27 00:12:57 +00002341 }
Anders Carlsson8c031552009-09-26 23:10:05 +00002342 return false;
Anders Carlssone7c8cb62009-09-26 20:53:44 +00002343}
2344
Anders Carlsson76967372009-09-17 00:43:46 +00002345void CXXNameMangler::addSubstitution(QualType T) {
Anders Carlssond99edc42009-09-26 03:55:37 +00002346 if (!T.getCVRQualifiers()) {
2347 if (const RecordType *RT = T->getAs<RecordType>()) {
2348 addSubstitution(RT->getDecl());
2349 return;
2350 }
2351 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002352
Anders Carlsson76967372009-09-17 00:43:46 +00002353 uintptr_t TypePtr = reinterpret_cast<uintptr_t>(T.getAsOpaquePtr());
Anders Carlssond3a932a2009-09-17 03:53:28 +00002354 addSubstitution(TypePtr);
2355}
2356
Douglas Gregor1e9268e2010-04-28 05:58:56 +00002357void CXXNameMangler::addSubstitution(TemplateName Template) {
2358 if (TemplateDecl *TD = Template.getAsTemplateDecl())
2359 return addSubstitution(TD);
Sean Huntc3021132010-05-05 15:23:54 +00002360
Douglas Gregor1e9268e2010-04-28 05:58:56 +00002361 Template = Context.getASTContext().getCanonicalTemplateName(Template);
2362 addSubstitution(reinterpret_cast<uintptr_t>(Template.getAsVoidPointer()));
2363}
2364
Anders Carlssond3a932a2009-09-17 03:53:28 +00002365void CXXNameMangler::addSubstitution(uintptr_t Ptr) {
Anders Carlssond3a932a2009-09-17 03:53:28 +00002366 assert(!Substitutions.count(Ptr) && "Substitution already exists!");
Anders Carlsson9d85b722010-06-02 04:29:50 +00002367 Substitutions[Ptr] = SeqID++;
Anders Carlsson76967372009-09-17 00:43:46 +00002368}
2369
Daniel Dunbar1b077112009-11-21 09:06:10 +00002370//
Mike Stump1eb44332009-09-09 15:08:12 +00002371
Daniel Dunbar1b077112009-11-21 09:06:10 +00002372/// \brief Mangles the name of the declaration D and emits that name to the
2373/// given output stream.
2374///
2375/// If the declaration D requires a mangled name, this routine will emit that
2376/// mangled name to \p os and return true. Otherwise, \p os will be unchanged
2377/// and this routine will return false. In this case, the caller should just
2378/// emit the identifier of the declaration (\c D->getIdentifier()) as its
2379/// name.
Daniel Dunbarf981bf82009-11-21 09:14:52 +00002380void MangleContext::mangleName(const NamedDecl *D,
Daniel Dunbar94fd26d2009-11-21 09:06:22 +00002381 llvm::SmallVectorImpl<char> &Res) {
Daniel Dunbarc02ab4c2009-11-21 09:14:44 +00002382 assert((isa<FunctionDecl>(D) || isa<VarDecl>(D)) &&
2383 "Invalid mangleName() call, argument is not a variable or function!");
2384 assert(!isa<CXXConstructorDecl>(D) && !isa<CXXDestructorDecl>(D) &&
2385 "Invalid mangleName() call on 'structor decl!");
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002386
Daniel Dunbar1b077112009-11-21 09:06:10 +00002387 PrettyStackTraceDecl CrashInfo(D, SourceLocation(),
2388 getASTContext().getSourceManager(),
2389 "Mangling declaration");
Mike Stump1eb44332009-09-09 15:08:12 +00002390
Daniel Dunbar94fd26d2009-11-21 09:06:22 +00002391 CXXNameMangler Mangler(*this, Res);
2392 return Mangler.mangle(D);
Daniel Dunbar1b077112009-11-21 09:06:10 +00002393}
Mike Stump1eb44332009-09-09 15:08:12 +00002394
Daniel Dunbar1b077112009-11-21 09:06:10 +00002395void MangleContext::mangleCXXCtor(const CXXConstructorDecl *D, CXXCtorType Type,
Daniel Dunbar94fd26d2009-11-21 09:06:22 +00002396 llvm::SmallVectorImpl<char> &Res) {
Daniel Dunbar77939c92009-11-21 09:06:31 +00002397 CXXNameMangler Mangler(*this, Res, D, Type);
2398 Mangler.mangle(D);
Daniel Dunbar1b077112009-11-21 09:06:10 +00002399}
Mike Stump1eb44332009-09-09 15:08:12 +00002400
Daniel Dunbar1b077112009-11-21 09:06:10 +00002401void MangleContext::mangleCXXDtor(const CXXDestructorDecl *D, CXXDtorType Type,
Daniel Dunbar94fd26d2009-11-21 09:06:22 +00002402 llvm::SmallVectorImpl<char> &Res) {
Daniel Dunbar77939c92009-11-21 09:06:31 +00002403 CXXNameMangler Mangler(*this, Res, D, Type);
2404 Mangler.mangle(D);
Daniel Dunbar1b077112009-11-21 09:06:10 +00002405}
Mike Stumpf1216772009-07-31 18:25:34 +00002406
Fariborz Jahanian564360b2010-06-24 00:08:06 +00002407void MangleContext::mangleBlock(GlobalDecl GD, const BlockDecl *BD,
Douglas Gregor35415f52010-05-25 17:04:15 +00002408 llvm::SmallVectorImpl<char> &Res) {
Charles Davis685b1d92010-05-26 18:25:27 +00002409 MiscNameMangler Mangler(*this, Res);
Fariborz Jahanian564360b2010-06-24 00:08:06 +00002410 Mangler.mangleBlock(GD, BD);
Douglas Gregor35415f52010-05-25 17:04:15 +00002411}
2412
Anders Carlsson19879c92010-03-23 17:17:29 +00002413void MangleContext::mangleThunk(const CXXMethodDecl *MD,
2414 const ThunkInfo &Thunk,
2415 llvm::SmallVectorImpl<char> &Res) {
2416 // <special-name> ::= T <call-offset> <base encoding>
2417 // # base is the nominal target function of thunk
2418 // <special-name> ::= Tc <call-offset> <call-offset> <base encoding>
2419 // # base is the nominal target function of thunk
2420 // # first call-offset is 'this' adjustment
2421 // # second call-offset is result adjustment
Sean Huntc3021132010-05-05 15:23:54 +00002422
Anders Carlsson19879c92010-03-23 17:17:29 +00002423 assert(!isa<CXXDestructorDecl>(MD) &&
2424 "Use mangleCXXDtor for destructor decls!");
Sean Huntc3021132010-05-05 15:23:54 +00002425
Anders Carlsson19879c92010-03-23 17:17:29 +00002426 CXXNameMangler Mangler(*this, Res);
2427 Mangler.getStream() << "_ZT";
2428 if (!Thunk.Return.isEmpty())
2429 Mangler.getStream() << 'c';
Sean Huntc3021132010-05-05 15:23:54 +00002430
Anders Carlsson19879c92010-03-23 17:17:29 +00002431 // Mangle the 'this' pointer adjustment.
2432 Mangler.mangleCallOffset(Thunk.This.NonVirtual, Thunk.This.VCallOffsetOffset);
Sean Huntc3021132010-05-05 15:23:54 +00002433
Anders Carlsson19879c92010-03-23 17:17:29 +00002434 // Mangle the return pointer adjustment if there is one.
2435 if (!Thunk.Return.isEmpty())
2436 Mangler.mangleCallOffset(Thunk.Return.NonVirtual,
2437 Thunk.Return.VBaseOffsetOffset);
Sean Huntc3021132010-05-05 15:23:54 +00002438
Anders Carlsson19879c92010-03-23 17:17:29 +00002439 Mangler.mangleFunctionEncoding(MD);
2440}
2441
Sean Huntc3021132010-05-05 15:23:54 +00002442void
Anders Carlsson19879c92010-03-23 17:17:29 +00002443MangleContext::mangleCXXDtorThunk(const CXXDestructorDecl *DD, CXXDtorType Type,
2444 const ThisAdjustment &ThisAdjustment,
2445 llvm::SmallVectorImpl<char> &Res) {
2446 // <special-name> ::= T <call-offset> <base encoding>
2447 // # base is the nominal target function of thunk
Sean Huntc3021132010-05-05 15:23:54 +00002448
Anders Carlsson19879c92010-03-23 17:17:29 +00002449 CXXNameMangler Mangler(*this, Res, DD, Type);
2450 Mangler.getStream() << "_ZT";
2451
2452 // Mangle the 'this' pointer adjustment.
Sean Huntc3021132010-05-05 15:23:54 +00002453 Mangler.mangleCallOffset(ThisAdjustment.NonVirtual,
Anders Carlsson19879c92010-03-23 17:17:29 +00002454 ThisAdjustment.VCallOffsetOffset);
2455
2456 Mangler.mangleFunctionEncoding(DD);
2457}
2458
Daniel Dunbarc0747712009-11-21 09:12:13 +00002459/// mangleGuardVariable - Returns the mangled name for a guard variable
2460/// for the passed in VarDecl.
2461void MangleContext::mangleGuardVariable(const VarDecl *D,
2462 llvm::SmallVectorImpl<char> &Res) {
2463 // <special-name> ::= GV <object name> # Guard variable for one-time
2464 // # initialization
2465 CXXNameMangler Mangler(*this, Res);
2466 Mangler.getStream() << "_ZGV";
2467 Mangler.mangleName(D);
2468}
2469
Anders Carlsson715edf22010-06-26 16:09:40 +00002470void MangleContext::mangleReferenceTemporary(const VarDecl *D,
2471 llvm::SmallVectorImpl<char> &Res) {
2472 // We match the GCC mangling here.
2473 // <special-name> ::= GR <object name>
2474 CXXNameMangler Mangler(*this, Res);
2475 Mangler.getStream() << "_ZGR";
2476 Mangler.mangleName(D);
2477}
2478
Anders Carlsson046c2942010-04-17 20:15:18 +00002479void MangleContext::mangleCXXVTable(const CXXRecordDecl *RD,
Daniel Dunbar94fd26d2009-11-21 09:06:22 +00002480 llvm::SmallVectorImpl<char> &Res) {
Daniel Dunbarc0747712009-11-21 09:12:13 +00002481 // <special-name> ::= TV <type> # virtual table
Daniel Dunbar94fd26d2009-11-21 09:06:22 +00002482 CXXNameMangler Mangler(*this, Res);
Daniel Dunbarc0747712009-11-21 09:12:13 +00002483 Mangler.getStream() << "_ZTV";
Douglas Gregor1b12a3b2010-05-26 05:11:13 +00002484 Mangler.mangleNameOrStandardSubstitution(RD);
Daniel Dunbar1b077112009-11-21 09:06:10 +00002485}
Mike Stump82d75b02009-11-10 01:58:37 +00002486
Daniel Dunbar1b077112009-11-21 09:06:10 +00002487void MangleContext::mangleCXXVTT(const CXXRecordDecl *RD,
Daniel Dunbar94fd26d2009-11-21 09:06:22 +00002488 llvm::SmallVectorImpl<char> &Res) {
Daniel Dunbarc0747712009-11-21 09:12:13 +00002489 // <special-name> ::= TT <type> # VTT structure
Daniel Dunbar94fd26d2009-11-21 09:06:22 +00002490 CXXNameMangler Mangler(*this, Res);
Daniel Dunbarc0747712009-11-21 09:12:13 +00002491 Mangler.getStream() << "_ZTT";
Douglas Gregor1b12a3b2010-05-26 05:11:13 +00002492 Mangler.mangleNameOrStandardSubstitution(RD);
Daniel Dunbar1b077112009-11-21 09:06:10 +00002493}
Mike Stumpab3f7e92009-11-10 01:41:59 +00002494
Anders Carlsson046c2942010-04-17 20:15:18 +00002495void MangleContext::mangleCXXCtorVTable(const CXXRecordDecl *RD, int64_t Offset,
Daniel Dunbar1b077112009-11-21 09:06:10 +00002496 const CXXRecordDecl *Type,
Daniel Dunbar94fd26d2009-11-21 09:06:22 +00002497 llvm::SmallVectorImpl<char> &Res) {
Daniel Dunbarc0747712009-11-21 09:12:13 +00002498 // <special-name> ::= TC <type> <offset number> _ <base type>
Daniel Dunbar94fd26d2009-11-21 09:06:22 +00002499 CXXNameMangler Mangler(*this, Res);
Daniel Dunbarc0747712009-11-21 09:12:13 +00002500 Mangler.getStream() << "_ZTC";
Douglas Gregor1b12a3b2010-05-26 05:11:13 +00002501 Mangler.mangleNameOrStandardSubstitution(RD);
Daniel Dunbarc0747712009-11-21 09:12:13 +00002502 Mangler.getStream() << Offset;
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002503 Mangler.getStream() << '_';
Douglas Gregor1b12a3b2010-05-26 05:11:13 +00002504 Mangler.mangleNameOrStandardSubstitution(Type);
Daniel Dunbar1b077112009-11-21 09:06:10 +00002505}
Mike Stump738f8c22009-07-31 23:15:31 +00002506
Mike Stumpde050572009-12-02 18:57:08 +00002507void MangleContext::mangleCXXRTTI(QualType Ty,
Daniel Dunbar94fd26d2009-11-21 09:06:22 +00002508 llvm::SmallVectorImpl<char> &Res) {
Daniel Dunbarc0747712009-11-21 09:12:13 +00002509 // <special-name> ::= TI <type> # typeinfo structure
Douglas Gregor154fe982009-12-23 22:04:40 +00002510 assert(!Ty.hasQualifiers() && "RTTI info cannot have top-level qualifiers");
Daniel Dunbar94fd26d2009-11-21 09:06:22 +00002511 CXXNameMangler Mangler(*this, Res);
Daniel Dunbarc0747712009-11-21 09:12:13 +00002512 Mangler.getStream() << "_ZTI";
2513 Mangler.mangleType(Ty);
Daniel Dunbar1b077112009-11-21 09:06:10 +00002514}
Mike Stump67795982009-11-14 00:14:13 +00002515
Mike Stumpde050572009-12-02 18:57:08 +00002516void MangleContext::mangleCXXRTTIName(QualType Ty,
Daniel Dunbar94fd26d2009-11-21 09:06:22 +00002517 llvm::SmallVectorImpl<char> &Res) {
Daniel Dunbarc0747712009-11-21 09:12:13 +00002518 // <special-name> ::= TS <type> # typeinfo name (null terminated byte string)
Daniel Dunbar94fd26d2009-11-21 09:06:22 +00002519 CXXNameMangler Mangler(*this, Res);
Daniel Dunbarc0747712009-11-21 09:12:13 +00002520 Mangler.getStream() << "_ZTS";
2521 Mangler.mangleType(Ty);
Mike Stumpf1216772009-07-31 18:25:34 +00002522}