blob: 30ee541c00fa03780f34a869396af9c7d431bc6d [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
307 // C functions and "main" are not mangled.
308 if ((FD && FD->isMain()) || isInCLinkageSpecification(D))
Daniel Dunbarf981bf82009-11-21 09:14:52 +0000309 return false;
310
Anders Carlsson43f17402009-04-02 15:51:53 +0000311 return true;
312}
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000313
Daniel Dunbar7e0c1952009-11-21 09:17:15 +0000314void CXXNameMangler::mangle(const NamedDecl *D, llvm::StringRef Prefix) {
Mike Stump141c5af2009-09-02 00:25:38 +0000315 // Any decl can be declared with __asm("foo") on it, and this takes precedence
316 // over all other naming in the .o file.
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000317 if (const AsmLabelAttr *ALA = D->getAttr<AsmLabelAttr>()) {
Chris Lattnerca3f25c2009-03-21 08:24:40 +0000318 // If we have an asm name, then we use it as the mangling.
319 Out << '\01'; // LLVM IR Marker for __asm("foo")
320 Out << ALA->getLabel();
Daniel Dunbarf981bf82009-11-21 09:14:52 +0000321 return;
Chris Lattnerca3f25c2009-03-21 08:24:40 +0000322 }
Mike Stump1eb44332009-09-09 15:08:12 +0000323
Sean Hunt31455252010-01-24 03:04:27 +0000324 // <mangled-name> ::= _Z <encoding>
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000325 // ::= <data name>
326 // ::= <special-name>
Daniel Dunbar7e0c1952009-11-21 09:17:15 +0000327 Out << Prefix;
328 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
Daniel Dunbarf981bf82009-11-21 09:14:52 +0000329 mangleFunctionEncoding(FD);
Rafael Espindolad9800722010-03-11 14:07:00 +0000330 else if (const VarDecl *VD = dyn_cast<VarDecl>(D))
331 mangleName(VD);
Daniel Dunbar7e0c1952009-11-21 09:17:15 +0000332 else
Rafael Espindolad9800722010-03-11 14:07:00 +0000333 mangleName(cast<FieldDecl>(D));
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000334}
335
336void CXXNameMangler::mangleFunctionEncoding(const FunctionDecl *FD) {
337 // <encoding> ::= <function name> <bare-function-type>
338 mangleName(FD);
Mike Stump1eb44332009-09-09 15:08:12 +0000339
Daniel Dunbar7e0c1952009-11-21 09:17:15 +0000340 // Don't mangle in the type if this isn't a decl we should typically mangle.
341 if (!Context.shouldMangleDeclName(FD))
342 return;
343
Mike Stump141c5af2009-09-02 00:25:38 +0000344 // Whether the mangling of a function type includes the return type depends on
345 // the context and the nature of the function. The rules for deciding whether
346 // the return type is included are:
Mike Stump1eb44332009-09-09 15:08:12 +0000347 //
Douglas Gregor1fd2dd12009-06-29 22:39:32 +0000348 // 1. Template functions (names or types) have return types encoded, with
349 // the exceptions listed below.
Mike Stump1eb44332009-09-09 15:08:12 +0000350 // 2. Function types not appearing as part of a function name mangling,
Douglas Gregor1fd2dd12009-06-29 22:39:32 +0000351 // e.g. parameters, pointer types, etc., have return type encoded, with the
352 // exceptions listed below.
353 // 3. Non-template function names do not have return types encoded.
354 //
Mike Stump141c5af2009-09-02 00:25:38 +0000355 // The exceptions mentioned in (1) and (2) above, for which the return type is
356 // never included, are
Douglas Gregor1fd2dd12009-06-29 22:39:32 +0000357 // 1. Constructors.
358 // 2. Destructors.
359 // 3. Conversion operator functions, e.g. operator int.
360 bool MangleReturnType = false;
Anders Carlsson9234b7f2009-09-17 03:46:43 +0000361 if (FunctionTemplateDecl *PrimaryTemplate = FD->getPrimaryTemplate()) {
362 if (!(isa<CXXConstructorDecl>(FD) || isa<CXXDestructorDecl>(FD) ||
363 isa<CXXConversionDecl>(FD)))
364 MangleReturnType = true;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000365
Anders Carlsson9234b7f2009-09-17 03:46:43 +0000366 // Mangle the type of the primary template.
367 FD = PrimaryTemplate->getTemplatedDecl();
368 }
369
John McCall54e14c42009-10-22 22:37:11 +0000370 // Do the canonicalization out here because parameter types can
371 // undergo additional canonicalization (e.g. array decay).
372 FunctionType *FT = cast<FunctionType>(Context.getASTContext()
373 .getCanonicalType(FD->getType()));
374
375 mangleBareFunctionType(FT, MangleReturnType);
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000376}
377
Anders Carlsson47846d22009-12-04 06:23:23 +0000378static const DeclContext *IgnoreLinkageSpecDecls(const DeclContext *DC) {
379 while (isa<LinkageSpecDecl>(DC)) {
Anders Carlsson47846d22009-12-04 06:23:23 +0000380 DC = DC->getParent();
381 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000382
Anders Carlsson47846d22009-12-04 06:23:23 +0000383 return DC;
384}
385
Anders Carlssonc820f902010-06-02 15:58:27 +0000386/// isStd - Return whether a given namespace is the 'std' namespace.
387static bool isStd(const NamespaceDecl *NS) {
388 if (!IgnoreLinkageSpecDecls(NS->getParent())->isTranslationUnit())
389 return false;
390
391 const IdentifierInfo *II = NS->getOriginalNamespace()->getIdentifier();
392 return II && II->isStr("std");
393}
394
Anders Carlsson47846d22009-12-04 06:23:23 +0000395// isStdNamespace - Return whether a given decl context is a toplevel 'std'
396// namespace.
Daniel Dunbar1308af92009-11-21 09:11:45 +0000397static bool isStdNamespace(const DeclContext *DC) {
Anders Carlsson47846d22009-12-04 06:23:23 +0000398 if (!DC->isNamespace())
399 return false;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000400
Anders Carlsson47846d22009-12-04 06:23:23 +0000401 return isStd(cast<NamespaceDecl>(DC));
Daniel Dunbar1308af92009-11-21 09:11:45 +0000402}
403
Anders Carlssonbb36ba42009-09-26 03:24:57 +0000404static const TemplateDecl *
405isTemplate(const NamedDecl *ND, const TemplateArgumentList *&TemplateArgs) {
Anders Carlsson2744a062009-09-18 19:00:18 +0000406 // Check if we have a function template.
407 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)){
Anders Carlssonbb36ba42009-09-26 03:24:57 +0000408 if (const TemplateDecl *TD = FD->getPrimaryTemplate()) {
Anders Carlsson2744a062009-09-18 19:00:18 +0000409 TemplateArgs = FD->getTemplateSpecializationArgs();
Anders Carlssonbb36ba42009-09-26 03:24:57 +0000410 return TD;
Anders Carlsson2744a062009-09-18 19:00:18 +0000411 }
412 }
413
Anders Carlssoneafc6dc2009-09-18 19:44:50 +0000414 // Check if we have a class template.
415 if (const ClassTemplateSpecializationDecl *Spec =
416 dyn_cast<ClassTemplateSpecializationDecl>(ND)) {
417 TemplateArgs = &Spec->getTemplateArgs();
Anders Carlssonbb36ba42009-09-26 03:24:57 +0000418 return Spec->getSpecializedTemplate();
Anders Carlssoneafc6dc2009-09-18 19:44:50 +0000419 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000420
Anders Carlsson2744a062009-09-18 19:00:18 +0000421 return 0;
422}
423
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000424void CXXNameMangler::mangleName(const NamedDecl *ND) {
425 // <name> ::= <nested-name>
426 // ::= <unscoped-name>
427 // ::= <unscoped-template-name> <template-args>
Anders Carlsson201ce742009-09-17 03:17:01 +0000428 // ::= <local-name>
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000429 //
Anders Carlssond58d6f72009-09-17 16:12:20 +0000430 const DeclContext *DC = ND->getDeclContext();
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000431
Fariborz Jahanian57058532010-03-03 19:41:08 +0000432 if (GetLocalClassFunctionDeclContext(DC)) {
433 mangleLocalName(ND);
434 return;
435 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000436
Eli Friedman7facf842009-12-02 20:32:49 +0000437 // If this is an extern variable declared locally, the relevant DeclContext
438 // is that of the containing namespace, or the translation unit.
439 if (isa<FunctionDecl>(DC) && ND->hasLinkage())
440 while (!DC->isNamespace() && !DC->isTranslationUnit())
441 DC = DC->getParent();
442
Anders Carlsson5cc58c62009-09-22 17:23:30 +0000443 while (isa<LinkageSpecDecl>(DC))
Anders Carlssond58d6f72009-09-17 16:12:20 +0000444 DC = DC->getParent();
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000445
Anders Carlssond58d6f72009-09-17 16:12:20 +0000446 if (DC->isTranslationUnit() || isStdNamespace(DC)) {
Anders Carlsson2744a062009-09-18 19:00:18 +0000447 // Check if we have a template.
448 const TemplateArgumentList *TemplateArgs = 0;
Anders Carlsson0fa6df42009-09-26 19:45:45 +0000449 if (const TemplateDecl *TD = isTemplate(ND, TemplateArgs)) {
Anders Carlsson2744a062009-09-18 19:00:18 +0000450 mangleUnscopedTemplateName(TD);
Rafael Espindolad9800722010-03-11 14:07:00 +0000451 TemplateParameterList *TemplateParameters = TD->getTemplateParameters();
452 mangleTemplateArgs(*TemplateParameters, *TemplateArgs);
Anders Carlsson2744a062009-09-18 19:00:18 +0000453 return;
Anders Carlsson7482e242009-09-18 04:29:09 +0000454 }
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000455
Anders Carlsson7482e242009-09-18 04:29:09 +0000456 mangleUnscopedName(ND);
457 return;
458 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000459
Anders Carlsson7b06f6c2009-12-10 03:14:39 +0000460 if (isa<FunctionDecl>(DC) || isa<ObjCMethodDecl>(DC)) {
Anders Carlsson7482e242009-09-18 04:29:09 +0000461 mangleLocalName(ND);
462 return;
463 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000464
Eli Friedman7facf842009-12-02 20:32:49 +0000465 mangleNestedName(ND, DC);
Anders Carlsson7482e242009-09-18 04:29:09 +0000466}
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000467void CXXNameMangler::mangleName(const TemplateDecl *TD,
Anders Carlsson7624f212009-09-18 02:42:01 +0000468 const TemplateArgument *TemplateArgs,
469 unsigned NumTemplateArgs) {
Anders Carlsson47846d22009-12-04 06:23:23 +0000470 const DeclContext *DC = IgnoreLinkageSpecDecls(TD->getDeclContext());
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000471
Anders Carlsson7624f212009-09-18 02:42:01 +0000472 if (DC->isTranslationUnit() || isStdNamespace(DC)) {
Anders Carlsson0fa6df42009-09-26 19:45:45 +0000473 mangleUnscopedTemplateName(TD);
Rafael Espindolad9800722010-03-11 14:07:00 +0000474 TemplateParameterList *TemplateParameters = TD->getTemplateParameters();
475 mangleTemplateArgs(*TemplateParameters, TemplateArgs, NumTemplateArgs);
Anders Carlsson7624f212009-09-18 02:42:01 +0000476 } else {
477 mangleNestedName(TD, TemplateArgs, NumTemplateArgs);
478 }
479}
480
Anders Carlsson201ce742009-09-17 03:17:01 +0000481void CXXNameMangler::mangleUnscopedName(const NamedDecl *ND) {
482 // <unscoped-name> ::= <unqualified-name>
483 // ::= St <unqualified-name> # ::std::
484 if (isStdNamespace(ND->getDeclContext()))
485 Out << "St";
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000486
Anders Carlsson201ce742009-09-17 03:17:01 +0000487 mangleUnqualifiedName(ND);
488}
489
Anders Carlsson0fa6df42009-09-26 19:45:45 +0000490void CXXNameMangler::mangleUnscopedTemplateName(const TemplateDecl *ND) {
Anders Carlsson201ce742009-09-17 03:17:01 +0000491 // <unscoped-template-name> ::= <unscoped-name>
492 // ::= <substitution>
Anders Carlsson7624f212009-09-18 02:42:01 +0000493 if (mangleSubstitution(ND))
Anders Carlsson03c9d532009-09-17 04:02:31 +0000494 return;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000495
Douglas Gregor32fb4e12010-02-05 20:45:00 +0000496 // <template-template-param> ::= <template-param>
497 if (const TemplateTemplateParmDecl *TTP
498 = dyn_cast<TemplateTemplateParmDecl>(ND)) {
499 mangleTemplateParameter(TTP->getIndex());
500 return;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000501 }
Douglas Gregor32fb4e12010-02-05 20:45:00 +0000502
Anders Carlsson1668f202009-09-26 20:13:56 +0000503 mangleUnscopedName(ND->getTemplatedDecl());
Anders Carlsson7624f212009-09-18 02:42:01 +0000504 addSubstitution(ND);
Anders Carlsson201ce742009-09-17 03:17:01 +0000505}
506
Douglas Gregor1e9268e2010-04-28 05:58:56 +0000507void CXXNameMangler::mangleUnscopedTemplateName(TemplateName Template) {
508 // <unscoped-template-name> ::= <unscoped-name>
509 // ::= <substitution>
510 if (TemplateDecl *TD = Template.getAsTemplateDecl())
511 return mangleUnscopedTemplateName(TD);
Sean Huntc3021132010-05-05 15:23:54 +0000512
Douglas Gregor1e9268e2010-04-28 05:58:56 +0000513 if (mangleSubstitution(Template))
514 return;
515
516 // FIXME: How to cope with operators here?
517 DependentTemplateName *Dependent = Template.getAsDependentTemplateName();
518 assert(Dependent && "Not a dependent template name?");
519 if (!Dependent->isIdentifier()) {
520 // FIXME: We can't possibly know the arity of the operator here!
521 Diagnostic &Diags = Context.getDiags();
522 unsigned DiagID = Diags.getCustomDiagID(Diagnostic::Error,
523 "cannot mangle dependent operator name");
524 Diags.Report(FullSourceLoc(), DiagID);
525 return;
526 }
Sean Huntc3021132010-05-05 15:23:54 +0000527
Douglas Gregor1e9268e2010-04-28 05:58:56 +0000528 mangleSourceName(Dependent->getIdentifier());
529 addSubstitution(Template);
530}
531
John McCall0512e482010-07-14 04:20:34 +0000532void CXXNameMangler::mangleFloat(const llvm::APFloat &F) {
533 // TODO: avoid this copy with careful stream management.
534 llvm::SmallString<20> Buffer;
535 F.bitcastToAPInt().toString(Buffer, 16, false);
536 Out.write(Buffer.data(), Buffer.size());
537}
538
539void CXXNameMangler::mangleNumber(const llvm::APSInt &Value) {
540 if (Value.isSigned() && Value.isNegative()) {
541 Out << 'n';
542 Value.abs().print(Out, true);
543 } else
544 Value.print(Out, Value.isSigned());
545}
546
Anders Carlssona94822e2009-11-26 02:32:05 +0000547void CXXNameMangler::mangleNumber(int64_t Number) {
548 // <number> ::= [n] <non-negative decimal integer>
549 if (Number < 0) {
550 Out << 'n';
551 Number = -Number;
552 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000553
Anders Carlssona94822e2009-11-26 02:32:05 +0000554 Out << Number;
555}
556
Anders Carlsson19879c92010-03-23 17:17:29 +0000557void CXXNameMangler::mangleCallOffset(int64_t NonVirtual, int64_t Virtual) {
Mike Stump141c5af2009-09-02 00:25:38 +0000558 // <call-offset> ::= h <nv-offset> _
559 // ::= v <v-offset> _
560 // <nv-offset> ::= <offset number> # non-virtual base override
Anders Carlssona94822e2009-11-26 02:32:05 +0000561 // <v-offset> ::= <offset number> _ <virtual offset number>
Mike Stump141c5af2009-09-02 00:25:38 +0000562 // # virtual base override, with vcall offset
Anders Carlsson19879c92010-03-23 17:17:29 +0000563 if (!Virtual) {
Anders Carlssona94822e2009-11-26 02:32:05 +0000564 Out << 'h';
Anders Carlsson19879c92010-03-23 17:17:29 +0000565 mangleNumber(NonVirtual);
Anders Carlssona94822e2009-11-26 02:32:05 +0000566 Out << '_';
567 return;
Mike Stump141c5af2009-09-02 00:25:38 +0000568 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000569
Anders Carlssona94822e2009-11-26 02:32:05 +0000570 Out << 'v';
Anders Carlsson19879c92010-03-23 17:17:29 +0000571 mangleNumber(NonVirtual);
Anders Carlssona94822e2009-11-26 02:32:05 +0000572 Out << '_';
Anders Carlsson19879c92010-03-23 17:17:29 +0000573 mangleNumber(Virtual);
Anders Carlssona94822e2009-11-26 02:32:05 +0000574 Out << '_';
Mike Stump9124bcc2009-09-02 00:56:18 +0000575}
576
John McCall1dd73832010-02-04 01:42:13 +0000577void CXXNameMangler::mangleUnresolvedScope(NestedNameSpecifier *Qualifier) {
578 Qualifier = getASTContext().getCanonicalNestedNameSpecifier(Qualifier);
579 switch (Qualifier->getKind()) {
580 case NestedNameSpecifier::Global:
581 // nothing
582 break;
583 case NestedNameSpecifier::Namespace:
584 mangleName(Qualifier->getAsNamespace());
585 break;
586 case NestedNameSpecifier::TypeSpec:
Rafael Espindola9b35b252010-03-17 04:28:11 +0000587 case NestedNameSpecifier::TypeSpecWithTemplate: {
588 const Type *QTy = Qualifier->getAsType();
589
590 if (const TemplateSpecializationType *TST =
591 dyn_cast<TemplateSpecializationType>(QTy)) {
592 if (!mangleSubstitution(QualType(TST, 0))) {
Douglas Gregor20f0cc72010-04-23 03:10:43 +0000593 mangleTemplatePrefix(TST->getTemplateName());
Sean Huntc3021132010-05-05 15:23:54 +0000594
Douglas Gregor20f0cc72010-04-23 03:10:43 +0000595 // FIXME: GCC does not appear to mangle the template arguments when
596 // the template in question is a dependent template name. Should we
597 // emulate that badness?
598 mangleTemplateArgs(TST->getTemplateName(), TST->getArgs(),
Rafael Espindola9b35b252010-03-17 04:28:11 +0000599 TST->getNumArgs());
600 addSubstitution(QualType(TST, 0));
601 }
602 } else {
603 // We use the QualType mangle type variant here because it handles
604 // substitutions.
605 mangleType(QualType(QTy, 0));
606 }
607 }
John McCall1dd73832010-02-04 01:42:13 +0000608 break;
609 case NestedNameSpecifier::Identifier:
John McCallad5e7382010-03-01 23:49:17 +0000610 // Member expressions can have these without prefixes.
611 if (Qualifier->getPrefix())
612 mangleUnresolvedScope(Qualifier->getPrefix());
John McCall1dd73832010-02-04 01:42:13 +0000613 mangleSourceName(Qualifier->getAsIdentifier());
614 break;
615 }
616}
617
618/// Mangles a name which was not resolved to a specific entity.
619void CXXNameMangler::mangleUnresolvedName(NestedNameSpecifier *Qualifier,
620 DeclarationName Name,
621 unsigned KnownArity) {
622 if (Qualifier)
623 mangleUnresolvedScope(Qualifier);
624 // FIXME: ambiguity of unqualified lookup with ::
625
626 mangleUnqualifiedName(0, Name, KnownArity);
627}
628
Anders Carlsson6f7e2f42010-06-08 14:49:03 +0000629static const FieldDecl *FindFirstNamedDataMember(const RecordDecl *RD) {
630 assert(RD->isAnonymousStructOrUnion() &&
631 "Expected anonymous struct or union!");
632
633 for (RecordDecl::field_iterator I = RD->field_begin(), E = RD->field_end();
634 I != E; ++I) {
635 const FieldDecl *FD = *I;
636
637 if (FD->getIdentifier())
638 return FD;
639
640 if (const RecordType *RT = FD->getType()->getAs<RecordType>()) {
641 if (const FieldDecl *NamedDataMember =
642 FindFirstNamedDataMember(RT->getDecl()))
643 return NamedDataMember;
644 }
645 }
646
647 // We didn't find a named data member.
648 return 0;
649}
650
John McCall1dd73832010-02-04 01:42:13 +0000651void CXXNameMangler::mangleUnqualifiedName(const NamedDecl *ND,
652 DeclarationName Name,
653 unsigned KnownArity) {
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000654 // <unqualified-name> ::= <operator-name>
Mike Stump1eb44332009-09-09 15:08:12 +0000655 // ::= <ctor-dtor-name>
656 // ::= <source-name>
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000657 switch (Name.getNameKind()) {
Anders Carlssonc4355b62009-10-07 01:45:02 +0000658 case DeclarationName::Identifier: {
Anders Carlssonc4355b62009-10-07 01:45:02 +0000659 if (const IdentifierInfo *II = Name.getAsIdentifierInfo()) {
Sean Hunt31455252010-01-24 03:04:27 +0000660 // We must avoid conflicts between internally- and externally-
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000661 // linked variable declaration names in the same TU.
Anders Carlssonaec25232010-02-06 04:52:27 +0000662 // This naming convention is the same as that followed by GCC, though it
663 // shouldn't actually matter.
664 if (ND && isa<VarDecl>(ND) && ND->getLinkage() == InternalLinkage &&
Sean Hunt31455252010-01-24 03:04:27 +0000665 ND->getDeclContext()->isFileContext())
666 Out << 'L';
667
Anders Carlssonc4355b62009-10-07 01:45:02 +0000668 mangleSourceName(II);
669 break;
670 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000671
John McCall1dd73832010-02-04 01:42:13 +0000672 // Otherwise, an anonymous entity. We must have a declaration.
673 assert(ND && "mangling empty name without declaration");
674
675 if (const NamespaceDecl *NS = dyn_cast<NamespaceDecl>(ND)) {
676 if (NS->isAnonymousNamespace()) {
677 // This is how gcc mangles these names.
678 Out << "12_GLOBAL__N_1";
679 break;
680 }
681 }
682
Anders Carlsson6f7e2f42010-06-08 14:49:03 +0000683 if (const VarDecl *VD = dyn_cast<VarDecl>(ND)) {
684 // We must have an anonymous union or struct declaration.
685 const RecordDecl *RD =
686 cast<RecordDecl>(VD->getType()->getAs<RecordType>()->getDecl());
687
688 // Itanium C++ ABI 5.1.2:
689 //
690 // For the purposes of mangling, the name of an anonymous union is
691 // considered to be the name of the first named data member found by a
692 // pre-order, depth-first, declaration-order walk of the data members of
693 // the anonymous union. If there is no such data member (i.e., if all of
694 // the data members in the union are unnamed), then there is no way for
695 // a program to refer to the anonymous union, and there is therefore no
696 // need to mangle its name.
697 const FieldDecl *FD = FindFirstNamedDataMember(RD);
698 assert(FD && "Didn't find a named data member!");
699 assert(FD->getIdentifier() && "Data member name isn't an identifier!");
700
701 mangleSourceName(FD->getIdentifier());
702 break;
703 }
704
Anders Carlssonc4355b62009-10-07 01:45:02 +0000705 // We must have an anonymous struct.
706 const TagDecl *TD = cast<TagDecl>(ND);
707 if (const TypedefDecl *D = TD->getTypedefForAnonDecl()) {
708 assert(TD->getDeclContext() == D->getDeclContext() &&
709 "Typedef should not be in another decl context!");
710 assert(D->getDeclName().getAsIdentifierInfo() &&
711 "Typedef was not named!");
712 mangleSourceName(D->getDeclName().getAsIdentifierInfo());
713 break;
714 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000715
Anders Carlssonc4355b62009-10-07 01:45:02 +0000716 // Get a unique id for the anonymous struct.
717 uint64_t AnonStructId = Context.getAnonymousStructId(TD);
718
719 // Mangle it as a source name in the form
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000720 // [n] $_<id>
Anders Carlssonc4355b62009-10-07 01:45:02 +0000721 // where n is the length of the string.
722 llvm::SmallString<8> Str;
723 Str += "$_";
724 Str += llvm::utostr(AnonStructId);
725
726 Out << Str.size();
727 Out << Str.str();
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000728 break;
Anders Carlssonc4355b62009-10-07 01:45:02 +0000729 }
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000730
731 case DeclarationName::ObjCZeroArgSelector:
732 case DeclarationName::ObjCOneArgSelector:
733 case DeclarationName::ObjCMultiArgSelector:
734 assert(false && "Can't mangle Objective-C selector names here!");
735 break;
736
737 case DeclarationName::CXXConstructorName:
Anders Carlsson27ae5362009-04-17 01:58:57 +0000738 if (ND == Structor)
Mike Stump141c5af2009-09-02 00:25:38 +0000739 // If the named decl is the C++ constructor we're mangling, use the type
740 // we were given.
Anders Carlsson27ae5362009-04-17 01:58:57 +0000741 mangleCXXCtorType(static_cast<CXXCtorType>(StructorType));
Anders Carlsson3ac86b52009-04-15 05:36:58 +0000742 else
743 // Otherwise, use the complete constructor name. This is relevant if a
744 // class with a constructor is declared within a constructor.
745 mangleCXXCtorType(Ctor_Complete);
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000746 break;
747
748 case DeclarationName::CXXDestructorName:
Anders Carlsson27ae5362009-04-17 01:58:57 +0000749 if (ND == Structor)
Mike Stump141c5af2009-09-02 00:25:38 +0000750 // If the named decl is the C++ destructor we're mangling, use the type we
751 // were given.
Anders Carlsson27ae5362009-04-17 01:58:57 +0000752 mangleCXXDtorType(static_cast<CXXDtorType>(StructorType));
753 else
754 // Otherwise, use the complete destructor name. This is relevant if a
755 // class with a destructor is declared within a destructor.
756 mangleCXXDtorType(Dtor_Complete);
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000757 break;
758
759 case DeclarationName::CXXConversionFunctionName:
Mike Stump1eb44332009-09-09 15:08:12 +0000760 // <operator-name> ::= cv <type> # (cast)
Douglas Gregor219cc612009-02-13 01:28:03 +0000761 Out << "cv";
Anders Carlssonb5404912009-10-07 01:06:45 +0000762 mangleType(Context.getASTContext().getCanonicalType(Name.getCXXNameType()));
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000763 break;
764
Anders Carlsson8257d412009-12-22 06:36:32 +0000765 case DeclarationName::CXXOperatorName: {
John McCall1dd73832010-02-04 01:42:13 +0000766 unsigned Arity;
767 if (ND) {
768 Arity = cast<FunctionDecl>(ND)->getNumParams();
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000769
John McCall1dd73832010-02-04 01:42:13 +0000770 // If we have a C++ member function, we need to include the 'this' pointer.
771 // FIXME: This does not make sense for operators that are static, but their
772 // names stay the same regardless of the arity (operator new for instance).
773 if (isa<CXXMethodDecl>(ND))
774 Arity++;
775 } else
776 Arity = KnownArity;
777
Anders Carlsson8257d412009-12-22 06:36:32 +0000778 mangleOperatorName(Name.getCXXOverloadedOperator(), Arity);
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000779 break;
Anders Carlsson8257d412009-12-22 06:36:32 +0000780 }
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000781
Sean Hunt3e518bd2009-11-29 07:34:05 +0000782 case DeclarationName::CXXLiteralOperatorName:
Sean Hunt5dd6b392009-12-04 21:11:13 +0000783 // FIXME: This mangling is not yet official.
Sean Hunt2421f662009-12-04 21:01:37 +0000784 Out << "li";
Sean Hunt3e518bd2009-11-29 07:34:05 +0000785 mangleSourceName(Name.getCXXLiteralIdentifier());
786 break;
787
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000788 case DeclarationName::CXXUsingDirective:
789 assert(false && "Can't mangle a using directive name!");
Douglas Gregor219cc612009-02-13 01:28:03 +0000790 break;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000791 }
792}
793
794void CXXNameMangler::mangleSourceName(const IdentifierInfo *II) {
795 // <source-name> ::= <positive length number> <identifier>
796 // <number> ::= [n] <non-negative decimal integer>
797 // <identifier> ::= <unqualified source code identifier>
798 Out << II->getLength() << II->getName();
799}
800
Eli Friedman7facf842009-12-02 20:32:49 +0000801void CXXNameMangler::mangleNestedName(const NamedDecl *ND,
Fariborz Jahanian57058532010-03-03 19:41:08 +0000802 const DeclContext *DC,
803 bool NoFunction) {
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000804 // <nested-name> ::= N [<CV-qualifiers>] <prefix> <unqualified-name> E
805 // ::= N [<CV-qualifiers>] <template-prefix> <template-args> E
Anders Carlssond99edc42009-09-26 03:55:37 +0000806
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000807 Out << 'N';
808 if (const CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(ND))
John McCall0953e762009-09-24 19:53:00 +0000809 mangleQualifiers(Qualifiers::fromCVRMask(Method->getTypeQualifiers()));
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000810
Anders Carlsson2744a062009-09-18 19:00:18 +0000811 // Check if we have a template.
812 const TemplateArgumentList *TemplateArgs = 0;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000813 if (const TemplateDecl *TD = isTemplate(ND, TemplateArgs)) {
Anders Carlsson2744a062009-09-18 19:00:18 +0000814 mangleTemplatePrefix(TD);
Rafael Espindolad9800722010-03-11 14:07:00 +0000815 TemplateParameterList *TemplateParameters = TD->getTemplateParameters();
816 mangleTemplateArgs(*TemplateParameters, *TemplateArgs);
Fariborz Jahanian57058532010-03-03 19:41:08 +0000817 }
818 else {
819 manglePrefix(DC, NoFunction);
Anders Carlsson7482e242009-09-18 04:29:09 +0000820 mangleUnqualifiedName(ND);
821 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000822
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000823 Out << 'E';
824}
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000825void CXXNameMangler::mangleNestedName(const TemplateDecl *TD,
Anders Carlsson7624f212009-09-18 02:42:01 +0000826 const TemplateArgument *TemplateArgs,
827 unsigned NumTemplateArgs) {
Anders Carlssone45117b2009-09-27 19:53:49 +0000828 // <nested-name> ::= N [<CV-qualifiers>] <template-prefix> <template-args> E
829
Anders Carlsson7624f212009-09-18 02:42:01 +0000830 Out << 'N';
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000831
Anders Carlssone45117b2009-09-27 19:53:49 +0000832 mangleTemplatePrefix(TD);
Rafael Espindolad9800722010-03-11 14:07:00 +0000833 TemplateParameterList *TemplateParameters = TD->getTemplateParameters();
834 mangleTemplateArgs(*TemplateParameters, TemplateArgs, NumTemplateArgs);
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000835
Anders Carlsson7624f212009-09-18 02:42:01 +0000836 Out << 'E';
837}
838
Anders Carlsson1b42c792009-04-02 16:24:45 +0000839void CXXNameMangler::mangleLocalName(const NamedDecl *ND) {
840 // <local-name> := Z <function encoding> E <entity name> [<discriminator>]
841 // := Z <function encoding> E s [<discriminator>]
Mike Stump1eb44332009-09-09 15:08:12 +0000842 // <discriminator> := _ <non-negative number>
Fariborz Jahanian57058532010-03-03 19:41:08 +0000843 const DeclContext *DC = ND->getDeclContext();
Anders Carlsson1b42c792009-04-02 16:24:45 +0000844 Out << 'Z';
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000845
Charles Davis685b1d92010-05-26 18:25:27 +0000846 if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(DC)) {
847 mangleObjCMethodName(MD);
848 }
Fariborz Jahanian57058532010-03-03 19:41:08 +0000849 else if (const DeclContext *CDC = GetLocalClassFunctionDeclContext(DC)) {
850 mangleFunctionEncoding(cast<FunctionDecl>(CDC));
851 Out << 'E';
852 mangleNestedName(ND, DC, true /*NoFunction*/);
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000853
Fariborz Jahanian4819ac42010-03-04 01:02:03 +0000854 // FIXME. This still does not cover all cases.
855 unsigned disc;
856 if (Context.getNextDiscriminator(ND, disc)) {
857 if (disc < 10)
858 Out << '_' << disc;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000859 else
Fariborz Jahanian4819ac42010-03-04 01:02:03 +0000860 Out << "__" << disc << '_';
861 }
Fariborz Jahanian57058532010-03-03 19:41:08 +0000862
863 return;
864 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000865 else
Fariborz Jahanian57058532010-03-03 19:41:08 +0000866 mangleFunctionEncoding(cast<FunctionDecl>(DC));
Anders Carlsson7b06f6c2009-12-10 03:14:39 +0000867
Anders Carlsson1b42c792009-04-02 16:24:45 +0000868 Out << 'E';
Eli Friedman6f9f25d2009-12-11 20:21:38 +0000869 mangleUnqualifiedName(ND);
Anders Carlsson1b42c792009-04-02 16:24:45 +0000870}
871
Fariborz Jahanian57058532010-03-03 19:41:08 +0000872void CXXNameMangler::manglePrefix(const DeclContext *DC, bool NoFunction) {
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000873 // <prefix> ::= <prefix> <unqualified-name>
874 // ::= <template-prefix> <template-args>
875 // ::= <template-param>
876 // ::= # empty
877 // ::= <substitution>
Anders Carlsson6862fc72009-09-17 04:16:28 +0000878
Anders Carlssonadd28822009-09-22 20:33:31 +0000879 while (isa<LinkageSpecDecl>(DC))
880 DC = DC->getParent();
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000881
Anders Carlsson9263e912009-09-18 18:39:58 +0000882 if (DC->isTranslationUnit())
883 return;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000884
Douglas Gregor35415f52010-05-25 17:04:15 +0000885 if (const BlockDecl *Block = dyn_cast<BlockDecl>(DC)) {
886 manglePrefix(DC->getParent(), NoFunction);
887 llvm::SmallString<64> Name;
Fariborz Jahanian564360b2010-06-24 00:08:06 +0000888 Context.mangleBlock(GlobalDecl(), Block, Name);
Douglas Gregor35415f52010-05-25 17:04:15 +0000889 Out << Name.size() << Name;
890 return;
891 }
892
Anders Carlsson6862fc72009-09-17 04:16:28 +0000893 if (mangleSubstitution(cast<NamedDecl>(DC)))
894 return;
Anders Carlsson7482e242009-09-18 04:29:09 +0000895
Anders Carlsson2ee3fca2009-09-18 20:11:09 +0000896 // Check if we have a template.
897 const TemplateArgumentList *TemplateArgs = 0;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000898 if (const TemplateDecl *TD = isTemplate(cast<NamedDecl>(DC), TemplateArgs)) {
Anders Carlsson2ee3fca2009-09-18 20:11:09 +0000899 mangleTemplatePrefix(TD);
Rafael Espindolad9800722010-03-11 14:07:00 +0000900 TemplateParameterList *TemplateParameters = TD->getTemplateParameters();
901 mangleTemplateArgs(*TemplateParameters, *TemplateArgs);
Fariborz Jahanian57058532010-03-03 19:41:08 +0000902 }
Douglas Gregor35415f52010-05-25 17:04:15 +0000903 else if(NoFunction && (isa<FunctionDecl>(DC) || isa<ObjCMethodDecl>(DC)))
Fariborz Jahanian57058532010-03-03 19:41:08 +0000904 return;
Douglas Gregor35415f52010-05-25 17:04:15 +0000905 else if (const ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(DC))
906 mangleObjCMethodName(Method);
Fariborz Jahanian57058532010-03-03 19:41:08 +0000907 else {
908 manglePrefix(DC->getParent(), NoFunction);
Anders Carlsson2ee3fca2009-09-18 20:11:09 +0000909 mangleUnqualifiedName(cast<NamedDecl>(DC));
910 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000911
Anders Carlsson6862fc72009-09-17 04:16:28 +0000912 addSubstitution(cast<NamedDecl>(DC));
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000913}
914
Douglas Gregor20f0cc72010-04-23 03:10:43 +0000915void CXXNameMangler::mangleTemplatePrefix(TemplateName Template) {
916 // <template-prefix> ::= <prefix> <template unqualified-name>
917 // ::= <template-param>
918 // ::= <substitution>
919 if (TemplateDecl *TD = Template.getAsTemplateDecl())
920 return mangleTemplatePrefix(TD);
921
922 if (QualifiedTemplateName *Qualified = Template.getAsQualifiedTemplateName())
923 mangleUnresolvedScope(Qualified->getQualifier());
Sean Huntc3021132010-05-05 15:23:54 +0000924
Douglas Gregor20f0cc72010-04-23 03:10:43 +0000925 if (OverloadedTemplateStorage *Overloaded
926 = Template.getAsOverloadedTemplate()) {
Sean Huntc3021132010-05-05 15:23:54 +0000927 mangleUnqualifiedName(0, (*Overloaded->begin())->getDeclName(),
Douglas Gregor20f0cc72010-04-23 03:10:43 +0000928 UnknownArity);
929 return;
930 }
Sean Huntc3021132010-05-05 15:23:54 +0000931
Douglas Gregor20f0cc72010-04-23 03:10:43 +0000932 DependentTemplateName *Dependent = Template.getAsDependentTemplateName();
933 assert(Dependent && "Unknown template name kind?");
934 mangleUnresolvedScope(Dependent->getQualifier());
Douglas Gregor1e9268e2010-04-28 05:58:56 +0000935 mangleUnscopedTemplateName(Template);
Douglas Gregor20f0cc72010-04-23 03:10:43 +0000936}
937
Anders Carlsson0fa6df42009-09-26 19:45:45 +0000938void CXXNameMangler::mangleTemplatePrefix(const TemplateDecl *ND) {
Anders Carlsson7482e242009-09-18 04:29:09 +0000939 // <template-prefix> ::= <prefix> <template unqualified-name>
940 // ::= <template-param>
941 // ::= <substitution>
Douglas Gregor32fb4e12010-02-05 20:45:00 +0000942 // <template-template-param> ::= <template-param>
943 // <substitution>
Anders Carlsson7482e242009-09-18 04:29:09 +0000944
Anders Carlssonaeb85372009-09-26 22:18:22 +0000945 if (mangleSubstitution(ND))
946 return;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000947
Douglas Gregor32fb4e12010-02-05 20:45:00 +0000948 // <template-template-param> ::= <template-param>
949 if (const TemplateTemplateParmDecl *TTP
950 = dyn_cast<TemplateTemplateParmDecl>(ND)) {
951 mangleTemplateParameter(TTP->getIndex());
952 return;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000953 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000954
Anders Carlssonaa73ab12009-09-18 18:47:07 +0000955 manglePrefix(ND->getDeclContext());
Anders Carlsson1668f202009-09-26 20:13:56 +0000956 mangleUnqualifiedName(ND->getTemplatedDecl());
Anders Carlssonaeb85372009-09-26 22:18:22 +0000957 addSubstitution(ND);
Anders Carlsson7482e242009-09-18 04:29:09 +0000958}
959
John McCallb6f532e2010-07-14 06:43:17 +0000960/// Mangles a template name under the production <type>. Required for
961/// template template arguments.
962/// <type> ::= <class-enum-type>
963/// ::= <template-param>
964/// ::= <substitution>
965void CXXNameMangler::mangleType(TemplateName TN) {
966 if (mangleSubstitution(TN))
967 return;
968
969 TemplateDecl *TD = 0;
970
971 switch (TN.getKind()) {
972 case TemplateName::QualifiedTemplate:
973 TD = TN.getAsQualifiedTemplateName()->getTemplateDecl();
974 goto HaveDecl;
975
976 case TemplateName::Template:
977 TD = TN.getAsTemplateDecl();
978 goto HaveDecl;
979
980 HaveDecl:
981 if (isa<TemplateTemplateParmDecl>(TD))
982 mangleTemplateParameter(cast<TemplateTemplateParmDecl>(TD)->getIndex());
983 else
984 mangleName(TD);
985 break;
986
987 case TemplateName::OverloadedTemplate:
988 llvm_unreachable("can't mangle an overloaded template name as a <type>");
989 break;
990
991 case TemplateName::DependentTemplate: {
992 const DependentTemplateName *Dependent = TN.getAsDependentTemplateName();
993 assert(Dependent->isIdentifier());
994
995 // <class-enum-type> ::= <name>
996 // <name> ::= <nested-name>
997 mangleUnresolvedScope(Dependent->getQualifier());
998 mangleSourceName(Dependent->getIdentifier());
999 break;
1000 }
1001
1002 }
1003
1004 addSubstitution(TN);
1005}
1006
Mike Stump1eb44332009-09-09 15:08:12 +00001007void
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001008CXXNameMangler::mangleOperatorName(OverloadedOperatorKind OO, unsigned Arity) {
1009 switch (OO) {
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001010 // <operator-name> ::= nw # new
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001011 case OO_New: Out << "nw"; break;
1012 // ::= na # new[]
1013 case OO_Array_New: Out << "na"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001014 // ::= dl # delete
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001015 case OO_Delete: Out << "dl"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001016 // ::= da # delete[]
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001017 case OO_Array_Delete: Out << "da"; break;
1018 // ::= ps # + (unary)
1019 // ::= pl # +
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001020 case OO_Plus:
Anders Carlsson8257d412009-12-22 06:36:32 +00001021 assert((Arity == 1 || Arity == 2) && "Invalid arity!");
1022 Out << (Arity == 1? "ps" : "pl"); break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001023 // ::= ng # - (unary)
1024 // ::= mi # -
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001025 case OO_Minus:
Anders Carlsson8257d412009-12-22 06:36:32 +00001026 assert((Arity == 1 || Arity == 2) && "Invalid arity!");
1027 Out << (Arity == 1? "ng" : "mi"); break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001028 // ::= ad # & (unary)
1029 // ::= an # &
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001030 case OO_Amp:
Anders Carlsson8257d412009-12-22 06:36:32 +00001031 assert((Arity == 1 || Arity == 2) && "Invalid arity!");
1032 Out << (Arity == 1? "ad" : "an"); break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001033 // ::= de # * (unary)
1034 // ::= ml # *
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001035 case OO_Star:
Anders Carlsson8257d412009-12-22 06:36:32 +00001036 assert((Arity == 1 || Arity == 2) && "Invalid arity!");
1037 Out << (Arity == 1? "de" : "ml"); break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001038 // ::= co # ~
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001039 case OO_Tilde: Out << "co"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001040 // ::= dv # /
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001041 case OO_Slash: Out << "dv"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001042 // ::= rm # %
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001043 case OO_Percent: Out << "rm"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001044 // ::= or # |
1045 case OO_Pipe: Out << "or"; break;
1046 // ::= eo # ^
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001047 case OO_Caret: Out << "eo"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001048 // ::= aS # =
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001049 case OO_Equal: Out << "aS"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001050 // ::= pL # +=
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001051 case OO_PlusEqual: Out << "pL"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001052 // ::= mI # -=
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001053 case OO_MinusEqual: Out << "mI"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001054 // ::= mL # *=
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001055 case OO_StarEqual: Out << "mL"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001056 // ::= dV # /=
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001057 case OO_SlashEqual: Out << "dV"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001058 // ::= rM # %=
1059 case OO_PercentEqual: Out << "rM"; break;
1060 // ::= aN # &=
1061 case OO_AmpEqual: Out << "aN"; break;
1062 // ::= oR # |=
1063 case OO_PipeEqual: Out << "oR"; break;
1064 // ::= eO # ^=
1065 case OO_CaretEqual: Out << "eO"; break;
1066 // ::= ls # <<
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001067 case OO_LessLess: Out << "ls"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001068 // ::= rs # >>
1069 case OO_GreaterGreater: Out << "rs"; break;
1070 // ::= lS # <<=
1071 case OO_LessLessEqual: Out << "lS"; break;
1072 // ::= rS # >>=
1073 case OO_GreaterGreaterEqual: Out << "rS"; break;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001074 // ::= eq # ==
1075 case OO_EqualEqual: Out << "eq"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001076 // ::= ne # !=
1077 case OO_ExclaimEqual: Out << "ne"; break;
1078 // ::= lt # <
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001079 case OO_Less: Out << "lt"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001080 // ::= gt # >
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001081 case OO_Greater: Out << "gt"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001082 // ::= le # <=
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001083 case OO_LessEqual: Out << "le"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001084 // ::= ge # >=
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001085 case OO_GreaterEqual: Out << "ge"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001086 // ::= nt # !
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001087 case OO_Exclaim: Out << "nt"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001088 // ::= aa # &&
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001089 case OO_AmpAmp: Out << "aa"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001090 // ::= oo # ||
1091 case OO_PipePipe: Out << "oo"; break;
1092 // ::= pp # ++
1093 case OO_PlusPlus: Out << "pp"; break;
1094 // ::= mm # --
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001095 case OO_MinusMinus: Out << "mm"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001096 // ::= cm # ,
1097 case OO_Comma: Out << "cm"; break;
1098 // ::= pm # ->*
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001099 case OO_ArrowStar: Out << "pm"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001100 // ::= pt # ->
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001101 case OO_Arrow: Out << "pt"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001102 // ::= cl # ()
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001103 case OO_Call: Out << "cl"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001104 // ::= ix # []
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001105 case OO_Subscript: Out << "ix"; break;
Anders Carlssone170ba72009-12-14 01:45:37 +00001106
1107 // ::= qu # ?
1108 // The conditional operator can't be overloaded, but we still handle it when
1109 // mangling expressions.
1110 case OO_Conditional: Out << "qu"; break;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001111
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001112 case OO_None:
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001113 case NUM_OVERLOADED_OPERATORS:
Mike Stump1eb44332009-09-09 15:08:12 +00001114 assert(false && "Not an overloaded operator");
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001115 break;
1116 }
1117}
1118
John McCall0953e762009-09-24 19:53:00 +00001119void CXXNameMangler::mangleQualifiers(Qualifiers Quals) {
Mike Stump1eb44332009-09-09 15:08:12 +00001120 // <CV-qualifiers> ::= [r] [V] [K] # restrict (C99), volatile, const
John McCall0953e762009-09-24 19:53:00 +00001121 if (Quals.hasRestrict())
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001122 Out << 'r';
John McCall0953e762009-09-24 19:53:00 +00001123 if (Quals.hasVolatile())
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001124 Out << 'V';
John McCall0953e762009-09-24 19:53:00 +00001125 if (Quals.hasConst())
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001126 Out << 'K';
John McCall0953e762009-09-24 19:53:00 +00001127
Douglas Gregor56079f72010-06-14 23:15:08 +00001128 if (Quals.hasAddressSpace()) {
1129 // Extension:
1130 //
1131 // <type> ::= U <address-space-number>
1132 //
1133 // where <address-space-number> is a source name consisting of 'AS'
1134 // followed by the address space <number>.
1135 llvm::SmallString<64> ASString;
1136 ASString = "AS" + llvm::utostr_32(Quals.getAddressSpace());
1137 Out << 'U' << ASString.size() << ASString;
1138 }
1139
John McCall0953e762009-09-24 19:53:00 +00001140 // FIXME: For now, just drop all extension qualifiers on the floor.
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001141}
1142
Anders Carlsson7b06f6c2009-12-10 03:14:39 +00001143void CXXNameMangler::mangleObjCMethodName(const ObjCMethodDecl *MD) {
Charles Davis685b1d92010-05-26 18:25:27 +00001144 llvm::SmallString<64> Buffer;
1145 MiscNameMangler(Context, Buffer).mangleObjCMethodName(MD);
1146 Out << Buffer;
Anders Carlsson7b06f6c2009-12-10 03:14:39 +00001147}
1148
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001149void CXXNameMangler::mangleType(QualType T) {
Anders Carlsson4843e582009-03-10 17:07:44 +00001150 // Only operate on the canonical type!
Anders Carlssonb5404912009-10-07 01:06:45 +00001151 T = Context.getASTContext().getCanonicalType(T);
Anders Carlsson4843e582009-03-10 17:07:44 +00001152
Douglas Gregora4923eb2009-11-16 21:35:15 +00001153 bool IsSubstitutable = T.hasLocalQualifiers() || !isa<BuiltinType>(T);
Anders Carlsson76967372009-09-17 00:43:46 +00001154 if (IsSubstitutable && mangleSubstitution(T))
1155 return;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001156
Douglas Gregora4923eb2009-11-16 21:35:15 +00001157 if (Qualifiers Quals = T.getLocalQualifiers()) {
John McCall0953e762009-09-24 19:53:00 +00001158 mangleQualifiers(Quals);
1159 // Recurse: even if the qualified type isn't yet substitutable,
1160 // the unqualified type might be.
Douglas Gregora4923eb2009-11-16 21:35:15 +00001161 mangleType(T.getLocalUnqualifiedType());
Anders Carlsson76967372009-09-17 00:43:46 +00001162 } else {
1163 switch (T->getTypeClass()) {
John McCallefe6aee2009-09-05 07:56:18 +00001164#define ABSTRACT_TYPE(CLASS, PARENT)
1165#define NON_CANONICAL_TYPE(CLASS, PARENT) \
Anders Carlsson76967372009-09-17 00:43:46 +00001166 case Type::CLASS: \
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +00001167 llvm_unreachable("can't mangle non-canonical type " #CLASS "Type"); \
Anders Carlsson76967372009-09-17 00:43:46 +00001168 return;
John McCallefe6aee2009-09-05 07:56:18 +00001169#define TYPE(CLASS, PARENT) \
Anders Carlsson76967372009-09-17 00:43:46 +00001170 case Type::CLASS: \
John McCall0953e762009-09-24 19:53:00 +00001171 mangleType(static_cast<const CLASS##Type*>(T.getTypePtr())); \
Anders Carlsson76967372009-09-17 00:43:46 +00001172 break;
John McCallefe6aee2009-09-05 07:56:18 +00001173#include "clang/AST/TypeNodes.def"
Anders Carlsson76967372009-09-17 00:43:46 +00001174 }
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001175 }
Anders Carlsson76967372009-09-17 00:43:46 +00001176
1177 // Add the substitution.
1178 if (IsSubstitutable)
1179 addSubstitution(T);
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001180}
1181
Douglas Gregor1b12a3b2010-05-26 05:11:13 +00001182void CXXNameMangler::mangleNameOrStandardSubstitution(const NamedDecl *ND) {
1183 if (!mangleStandardSubstitution(ND))
1184 mangleName(ND);
1185}
1186
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001187void CXXNameMangler::mangleType(const BuiltinType *T) {
John McCallefe6aee2009-09-05 07:56:18 +00001188 // <type> ::= <builtin-type>
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001189 // <builtin-type> ::= v # void
1190 // ::= w # wchar_t
1191 // ::= b # bool
1192 // ::= c # char
1193 // ::= a # signed char
1194 // ::= h # unsigned char
1195 // ::= s # short
1196 // ::= t # unsigned short
1197 // ::= i # int
1198 // ::= j # unsigned int
1199 // ::= l # long
1200 // ::= m # unsigned long
1201 // ::= x # long long, __int64
1202 // ::= y # unsigned long long, __int64
1203 // ::= n # __int128
1204 // UNSUPPORTED: ::= o # unsigned __int128
1205 // ::= f # float
1206 // ::= d # double
1207 // ::= e # long double, __float80
1208 // UNSUPPORTED: ::= g # __float128
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001209 // UNSUPPORTED: ::= Dd # IEEE 754r decimal floating point (64 bits)
1210 // UNSUPPORTED: ::= De # IEEE 754r decimal floating point (128 bits)
1211 // UNSUPPORTED: ::= Df # IEEE 754r decimal floating point (32 bits)
1212 // UNSUPPORTED: ::= Dh # IEEE 754r half-precision floating point (16 bits)
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001213 // ::= Di # char32_t
1214 // ::= Ds # char16_t
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001215 // ::= u <source-name> # vendor extended type
Sebastian Redl6e8ed162009-05-10 18:38:11 +00001216 // From our point of view, std::nullptr_t is a builtin, but as far as mangling
1217 // is concerned, it's a type called std::nullptr_t.
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001218 switch (T->getKind()) {
1219 case BuiltinType::Void: Out << 'v'; break;
1220 case BuiltinType::Bool: Out << 'b'; break;
1221 case BuiltinType::Char_U: case BuiltinType::Char_S: Out << 'c'; break;
1222 case BuiltinType::UChar: Out << 'h'; break;
1223 case BuiltinType::UShort: Out << 't'; break;
1224 case BuiltinType::UInt: Out << 'j'; break;
1225 case BuiltinType::ULong: Out << 'm'; break;
1226 case BuiltinType::ULongLong: Out << 'y'; break;
Chris Lattner2df9ced2009-04-30 02:43:43 +00001227 case BuiltinType::UInt128: Out << 'o'; break;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001228 case BuiltinType::SChar: Out << 'a'; break;
1229 case BuiltinType::WChar: Out << 'w'; break;
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001230 case BuiltinType::Char16: Out << "Ds"; break;
1231 case BuiltinType::Char32: Out << "Di"; break;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001232 case BuiltinType::Short: Out << 's'; break;
1233 case BuiltinType::Int: Out << 'i'; break;
1234 case BuiltinType::Long: Out << 'l'; break;
1235 case BuiltinType::LongLong: Out << 'x'; break;
Chris Lattner2df9ced2009-04-30 02:43:43 +00001236 case BuiltinType::Int128: Out << 'n'; break;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001237 case BuiltinType::Float: Out << 'f'; break;
1238 case BuiltinType::Double: Out << 'd'; break;
1239 case BuiltinType::LongDouble: Out << 'e'; break;
Sebastian Redl6e8ed162009-05-10 18:38:11 +00001240 case BuiltinType::NullPtr: Out << "St9nullptr_t"; break;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001241
1242 case BuiltinType::Overload:
1243 case BuiltinType::Dependent:
Mike Stump1eb44332009-09-09 15:08:12 +00001244 assert(false &&
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001245 "Overloaded and dependent types shouldn't get to name mangling");
1246 break;
Anders Carlssone89d1592009-06-26 18:41:36 +00001247 case BuiltinType::UndeducedAuto:
1248 assert(0 && "Should not see undeduced auto here");
1249 break;
Steve Naroff9533a7f2009-07-22 17:14:51 +00001250 case BuiltinType::ObjCId: Out << "11objc_object"; break;
1251 case BuiltinType::ObjCClass: Out << "10objc_class"; break;
Fariborz Jahanian13dcd002009-11-21 19:53:08 +00001252 case BuiltinType::ObjCSel: Out << "13objc_selector"; break;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001253 }
1254}
1255
John McCallefe6aee2009-09-05 07:56:18 +00001256// <type> ::= <function-type>
1257// <function-type> ::= F [Y] <bare-function-type> E
1258void CXXNameMangler::mangleType(const FunctionProtoType *T) {
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001259 Out << 'F';
Mike Stumpf5408fe2009-05-16 07:57:57 +00001260 // FIXME: We don't have enough information in the AST to produce the 'Y'
1261 // encoding for extern "C" function types.
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001262 mangleBareFunctionType(T, /*MangleReturnType=*/true);
1263 Out << 'E';
1264}
John McCallefe6aee2009-09-05 07:56:18 +00001265void CXXNameMangler::mangleType(const FunctionNoProtoType *T) {
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +00001266 llvm_unreachable("Can't mangle K&R function prototypes");
John McCallefe6aee2009-09-05 07:56:18 +00001267}
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001268void CXXNameMangler::mangleBareFunctionType(const FunctionType *T,
1269 bool MangleReturnType) {
John McCallefe6aee2009-09-05 07:56:18 +00001270 // We should never be mangling something without a prototype.
1271 const FunctionProtoType *Proto = cast<FunctionProtoType>(T);
1272
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001273 // <bare-function-type> ::= <signature type>+
1274 if (MangleReturnType)
John McCallefe6aee2009-09-05 07:56:18 +00001275 mangleType(Proto->getResultType());
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001276
Anders Carlsson93296682010-06-02 04:40:13 +00001277 if (Proto->getNumArgs() == 0 && !Proto->isVariadic()) {
1278 // <builtin-type> ::= v # void
Anders Carlssonc6c91bc2009-04-01 00:15:23 +00001279 Out << 'v';
1280 return;
1281 }
Mike Stump1eb44332009-09-09 15:08:12 +00001282
Douglas Gregor72564e72009-02-26 23:50:07 +00001283 for (FunctionProtoType::arg_type_iterator Arg = Proto->arg_type_begin(),
Mike Stump1eb44332009-09-09 15:08:12 +00001284 ArgEnd = Proto->arg_type_end();
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001285 Arg != ArgEnd; ++Arg)
1286 mangleType(*Arg);
Douglas Gregor219cc612009-02-13 01:28:03 +00001287
1288 // <builtin-type> ::= z # ellipsis
1289 if (Proto->isVariadic())
1290 Out << 'z';
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001291}
1292
John McCallefe6aee2009-09-05 07:56:18 +00001293// <type> ::= <class-enum-type>
Mike Stump1eb44332009-09-09 15:08:12 +00001294// <class-enum-type> ::= <name>
John McCalled976492009-12-04 22:46:56 +00001295void CXXNameMangler::mangleType(const UnresolvedUsingType *T) {
1296 mangleName(T->getDecl());
1297}
1298
1299// <type> ::= <class-enum-type>
1300// <class-enum-type> ::= <name>
John McCallefe6aee2009-09-05 07:56:18 +00001301void CXXNameMangler::mangleType(const EnumType *T) {
1302 mangleType(static_cast<const TagType*>(T));
1303}
1304void CXXNameMangler::mangleType(const RecordType *T) {
1305 mangleType(static_cast<const TagType*>(T));
1306}
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001307void CXXNameMangler::mangleType(const TagType *T) {
Eli Friedmanecb7e932009-12-11 18:00:57 +00001308 mangleName(T->getDecl());
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001309}
1310
John McCallefe6aee2009-09-05 07:56:18 +00001311// <type> ::= <array-type>
1312// <array-type> ::= A <positive dimension number> _ <element type>
1313// ::= A [<dimension expression>] _ <element type>
1314void CXXNameMangler::mangleType(const ConstantArrayType *T) {
1315 Out << 'A' << T->getSize() << '_';
1316 mangleType(T->getElementType());
1317}
1318void CXXNameMangler::mangleType(const VariableArrayType *T) {
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001319 Out << 'A';
John McCallefe6aee2009-09-05 07:56:18 +00001320 mangleExpression(T->getSizeExpr());
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001321 Out << '_';
1322 mangleType(T->getElementType());
1323}
John McCallefe6aee2009-09-05 07:56:18 +00001324void CXXNameMangler::mangleType(const DependentSizedArrayType *T) {
1325 Out << 'A';
1326 mangleExpression(T->getSizeExpr());
1327 Out << '_';
1328 mangleType(T->getElementType());
1329}
1330void CXXNameMangler::mangleType(const IncompleteArrayType *T) {
1331 Out << 'A' << '_';
1332 mangleType(T->getElementType());
1333}
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001334
John McCallefe6aee2009-09-05 07:56:18 +00001335// <type> ::= <pointer-to-member-type>
1336// <pointer-to-member-type> ::= M <class type> <member type>
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001337void CXXNameMangler::mangleType(const MemberPointerType *T) {
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001338 Out << 'M';
1339 mangleType(QualType(T->getClass(), 0));
Anders Carlsson0e650012009-05-17 17:41:20 +00001340 QualType PointeeType = T->getPointeeType();
1341 if (const FunctionProtoType *FPT = dyn_cast<FunctionProtoType>(PointeeType)) {
John McCall0953e762009-09-24 19:53:00 +00001342 mangleQualifiers(Qualifiers::fromCVRMask(FPT->getTypeQuals()));
Anders Carlsson0e650012009-05-17 17:41:20 +00001343 mangleType(FPT);
Anders Carlsson9d85b722010-06-02 04:29:50 +00001344
1345 // Itanium C++ ABI 5.1.8:
1346 //
1347 // The type of a non-static member function is considered to be different,
1348 // for the purposes of substitution, from the type of a namespace-scope or
1349 // static member function whose type appears similar. The types of two
1350 // non-static member functions are considered to be different, for the
1351 // purposes of substitution, if the functions are members of different
1352 // classes. In other words, for the purposes of substitution, the class of
1353 // which the function is a member is considered part of the type of
1354 // function.
1355
1356 // We increment the SeqID here to emulate adding an entry to the
1357 // substitution table. We can't actually add it because we don't want this
1358 // particular function type to be substituted.
1359 ++SeqID;
Mike Stump1eb44332009-09-09 15:08:12 +00001360 } else
Anders Carlsson0e650012009-05-17 17:41:20 +00001361 mangleType(PointeeType);
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001362}
1363
John McCallefe6aee2009-09-05 07:56:18 +00001364// <type> ::= <template-param>
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001365void CXXNameMangler::mangleType(const TemplateTypeParmType *T) {
Anders Carlsson0ccdf8d2009-09-27 00:38:53 +00001366 mangleTemplateParameter(T->getIndex());
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001367}
1368
John McCallefe6aee2009-09-05 07:56:18 +00001369// <type> ::= P <type> # pointer-to
1370void CXXNameMangler::mangleType(const PointerType *T) {
1371 Out << 'P';
1372 mangleType(T->getPointeeType());
1373}
1374void CXXNameMangler::mangleType(const ObjCObjectPointerType *T) {
1375 Out << 'P';
1376 mangleType(T->getPointeeType());
1377}
1378
1379// <type> ::= R <type> # reference-to
1380void CXXNameMangler::mangleType(const LValueReferenceType *T) {
1381 Out << 'R';
1382 mangleType(T->getPointeeType());
1383}
1384
1385// <type> ::= O <type> # rvalue reference-to (C++0x)
1386void CXXNameMangler::mangleType(const RValueReferenceType *T) {
1387 Out << 'O';
1388 mangleType(T->getPointeeType());
1389}
1390
1391// <type> ::= C <type> # complex pair (C 2000)
1392void CXXNameMangler::mangleType(const ComplexType *T) {
1393 Out << 'C';
1394 mangleType(T->getElementType());
1395}
1396
1397// GNU extension: vector types
Chris Lattner788b0fd2010-06-23 06:00:24 +00001398// <type> ::= <vector-type>
1399// <vector-type> ::= Dv <positive dimension number> _
1400// <extended element type>
1401// ::= Dv [<dimension expression>] _ <element type>
1402// <extended element type> ::= <element type>
1403// ::= p # AltiVec vector pixel
John McCallefe6aee2009-09-05 07:56:18 +00001404void CXXNameMangler::mangleType(const VectorType *T) {
Nick Lewycky0e5f0672010-03-26 07:18:04 +00001405 Out << "Dv" << T->getNumElements() << '_';
Chris Lattner788b0fd2010-06-23 06:00:24 +00001406 if (T->getAltiVecSpecific() == VectorType::Pixel)
1407 Out << 'p';
1408 else if (T->getAltiVecSpecific() == VectorType::Bool)
1409 Out << 'b';
1410 else
1411 mangleType(T->getElementType());
John McCallefe6aee2009-09-05 07:56:18 +00001412}
1413void CXXNameMangler::mangleType(const ExtVectorType *T) {
1414 mangleType(static_cast<const VectorType*>(T));
1415}
1416void CXXNameMangler::mangleType(const DependentSizedExtVectorType *T) {
Nick Lewycky0e5f0672010-03-26 07:18:04 +00001417 Out << "Dv";
1418 mangleExpression(T->getSizeExpr());
1419 Out << '_';
John McCallefe6aee2009-09-05 07:56:18 +00001420 mangleType(T->getElementType());
1421}
1422
Anders Carlssona40c5e42009-03-07 22:03:21 +00001423void CXXNameMangler::mangleType(const ObjCInterfaceType *T) {
1424 mangleSourceName(T->getDecl()->getIdentifier());
1425}
1426
John McCallc12c5bb2010-05-15 11:32:37 +00001427void CXXNameMangler::mangleType(const ObjCObjectType *T) {
John McCallc00c1f62010-05-15 17:06:29 +00001428 // We don't allow overloading by different protocol qualification,
1429 // so mangling them isn't necessary.
John McCallc12c5bb2010-05-15 11:32:37 +00001430 mangleType(T->getBaseType());
1431}
1432
John McCallefe6aee2009-09-05 07:56:18 +00001433void CXXNameMangler::mangleType(const BlockPointerType *T) {
Anders Carlssonf28c6872009-12-23 22:31:44 +00001434 Out << "U13block_pointer";
1435 mangleType(T->getPointeeType());
John McCallefe6aee2009-09-05 07:56:18 +00001436}
1437
John McCall31f17ec2010-04-27 00:57:59 +00001438void CXXNameMangler::mangleType(const InjectedClassNameType *T) {
1439 // Mangle injected class name types as if the user had written the
1440 // specialization out fully. It may not actually be possible to see
1441 // this mangling, though.
1442 mangleType(T->getInjectedSpecializationType());
1443}
1444
John McCallefe6aee2009-09-05 07:56:18 +00001445void CXXNameMangler::mangleType(const TemplateSpecializationType *T) {
Douglas Gregor1e9268e2010-04-28 05:58:56 +00001446 if (TemplateDecl *TD = T->getTemplateName().getAsTemplateDecl()) {
1447 mangleName(TD, T->getArgs(), T->getNumArgs());
1448 } else {
1449 if (mangleSubstitution(QualType(T, 0)))
1450 return;
Sean Huntc3021132010-05-05 15:23:54 +00001451
Douglas Gregor1e9268e2010-04-28 05:58:56 +00001452 mangleTemplatePrefix(T->getTemplateName());
Sean Huntc3021132010-05-05 15:23:54 +00001453
Douglas Gregor1e9268e2010-04-28 05:58:56 +00001454 // FIXME: GCC does not appear to mangle the template arguments when
1455 // the template in question is a dependent template name. Should we
1456 // emulate that badness?
1457 mangleTemplateArgs(T->getTemplateName(), T->getArgs(), T->getNumArgs());
1458 addSubstitution(QualType(T, 0));
1459 }
John McCallefe6aee2009-09-05 07:56:18 +00001460}
1461
Douglas Gregor4714c122010-03-31 17:34:00 +00001462void CXXNameMangler::mangleType(const DependentNameType *T) {
Anders Carlssonae352482009-09-26 02:26:02 +00001463 // Typename types are always nested
1464 Out << 'N';
John McCall33500952010-06-11 00:33:02 +00001465 mangleUnresolvedScope(T->getQualifier());
1466 mangleSourceName(T->getIdentifier());
1467 Out << 'E';
1468}
John McCall6ab30e02010-06-09 07:26:17 +00001469
John McCall33500952010-06-11 00:33:02 +00001470void CXXNameMangler::mangleType(const DependentTemplateSpecializationType *T) {
1471 // Dependently-scoped template types are always nested
1472 Out << 'N';
1473
1474 // TODO: avoid making this TemplateName.
1475 TemplateName Prefix =
1476 getASTContext().getDependentTemplateName(T->getQualifier(),
1477 T->getIdentifier());
1478 mangleTemplatePrefix(Prefix);
1479
1480 // FIXME: GCC does not appear to mangle the template arguments when
1481 // the template in question is a dependent template name. Should we
1482 // emulate that badness?
1483 mangleTemplateArgs(Prefix, T->getArgs(), T->getNumArgs());
Anders Carlssonae352482009-09-26 02:26:02 +00001484 Out << 'E';
John McCallefe6aee2009-09-05 07:56:18 +00001485}
1486
John McCallad5e7382010-03-01 23:49:17 +00001487void CXXNameMangler::mangleType(const TypeOfType *T) {
1488 // FIXME: this is pretty unsatisfactory, but there isn't an obvious
1489 // "extension with parameters" mangling.
1490 Out << "u6typeof";
1491}
1492
1493void CXXNameMangler::mangleType(const TypeOfExprType *T) {
1494 // FIXME: this is pretty unsatisfactory, but there isn't an obvious
1495 // "extension with parameters" mangling.
1496 Out << "u6typeof";
1497}
1498
1499void CXXNameMangler::mangleType(const DecltypeType *T) {
1500 Expr *E = T->getUnderlyingExpr();
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001501
John McCallad5e7382010-03-01 23:49:17 +00001502 // type ::= Dt <expression> E # decltype of an id-expression
1503 // # or class member access
1504 // ::= DT <expression> E # decltype of an expression
1505
1506 // This purports to be an exhaustive list of id-expressions and
1507 // class member accesses. Note that we do not ignore parentheses;
1508 // parentheses change the semantics of decltype for these
1509 // expressions (and cause the mangler to use the other form).
1510 if (isa<DeclRefExpr>(E) ||
1511 isa<MemberExpr>(E) ||
1512 isa<UnresolvedLookupExpr>(E) ||
1513 isa<DependentScopeDeclRefExpr>(E) ||
1514 isa<CXXDependentScopeMemberExpr>(E) ||
1515 isa<UnresolvedMemberExpr>(E))
1516 Out << "Dt";
1517 else
1518 Out << "DT";
1519 mangleExpression(E);
1520 Out << 'E';
1521}
1522
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001523void CXXNameMangler::mangleIntegerLiteral(QualType T,
Anders Carlssone170ba72009-12-14 01:45:37 +00001524 const llvm::APSInt &Value) {
1525 // <expr-primary> ::= L <type> <value number> E # integer literal
1526 Out << 'L';
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001527
Anders Carlssone170ba72009-12-14 01:45:37 +00001528 mangleType(T);
1529 if (T->isBooleanType()) {
1530 // Boolean values are encoded as 0/1.
1531 Out << (Value.getBoolValue() ? '1' : '0');
1532 } else {
John McCall0512e482010-07-14 04:20:34 +00001533 mangleNumber(Value);
Anders Carlssone170ba72009-12-14 01:45:37 +00001534 }
1535 Out << 'E';
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001536
Anders Carlssone170ba72009-12-14 01:45:37 +00001537}
1538
John McCall1dd73832010-02-04 01:42:13 +00001539void CXXNameMangler::mangleCalledExpression(const Expr *E, unsigned Arity) {
1540 if (E->getType() != getASTContext().OverloadTy)
1541 mangleExpression(E);
John McCall2f27bf82010-02-04 02:56:29 +00001542 // propagate arity to dependent overloads?
John McCall1dd73832010-02-04 01:42:13 +00001543
1544 llvm::PointerIntPair<OverloadExpr*,1> R
1545 = OverloadExpr::find(const_cast<Expr*>(E));
1546 if (R.getInt())
1547 Out << "an"; // &
1548 const OverloadExpr *Ovl = R.getPointer();
John McCall2f27bf82010-02-04 02:56:29 +00001549 if (const UnresolvedMemberExpr *ME = dyn_cast<UnresolvedMemberExpr>(Ovl)) {
1550 mangleMemberExpr(ME->getBase(), ME->isArrow(), ME->getQualifier(),
1551 ME->getMemberName(), Arity);
1552 return;
1553 }
John McCall1dd73832010-02-04 01:42:13 +00001554
1555 mangleUnresolvedName(Ovl->getQualifier(), Ovl->getName(), Arity);
1556}
1557
John McCall2f27bf82010-02-04 02:56:29 +00001558/// Mangles a member expression. Implicit accesses are not handled,
1559/// but that should be okay, because you shouldn't be able to
1560/// make an implicit access in a function template declaration.
John McCall2f27bf82010-02-04 02:56:29 +00001561void CXXNameMangler::mangleMemberExpr(const Expr *Base,
1562 bool IsArrow,
1563 NestedNameSpecifier *Qualifier,
1564 DeclarationName Member,
1565 unsigned Arity) {
John McCalle1e342f2010-03-01 19:12:25 +00001566 // gcc-4.4 uses 'dt' for dot expressions, which is reasonable.
1567 // OTOH, gcc also mangles the name as an expression.
1568 Out << (IsArrow ? "pt" : "dt");
John McCall2f27bf82010-02-04 02:56:29 +00001569 mangleExpression(Base);
1570 mangleUnresolvedName(Qualifier, Member, Arity);
1571}
1572
Anders Carlssond553f8c2009-09-21 01:21:10 +00001573void CXXNameMangler::mangleExpression(const Expr *E) {
1574 // <expression> ::= <unary operator-name> <expression>
John McCall09cc1412010-02-03 00:55:45 +00001575 // ::= <binary operator-name> <expression> <expression>
1576 // ::= <trinary operator-name> <expression> <expression> <expression>
1577 // ::= cl <expression>* E # call
Anders Carlssond553f8c2009-09-21 01:21:10 +00001578 // ::= cv <type> expression # conversion with one argument
1579 // ::= cv <type> _ <expression>* E # conversion with a different number of arguments
John McCall09cc1412010-02-03 00:55:45 +00001580 // ::= st <type> # sizeof (a type)
Anders Carlssond553f8c2009-09-21 01:21:10 +00001581 // ::= at <type> # alignof (a type)
1582 // ::= <template-param>
1583 // ::= <function-param>
1584 // ::= sr <type> <unqualified-name> # dependent name
1585 // ::= sr <type> <unqualified-name> <template-args> # dependent template-id
1586 // ::= sZ <template-param> # size of a parameter pack
John McCall09cc1412010-02-03 00:55:45 +00001587 // ::= <expr-primary>
John McCall1dd73832010-02-04 01:42:13 +00001588 // <expr-primary> ::= L <type> <value number> E # integer literal
1589 // ::= L <type <value float> E # floating literal
1590 // ::= L <mangled-name> E # external name
Anders Carlssond553f8c2009-09-21 01:21:10 +00001591 switch (E->getStmtClass()) {
John McCall6ae1f352010-04-09 22:26:14 +00001592 case Expr::NoStmtClass:
1593#define EXPR(Type, Base)
1594#define STMT(Type, Base) \
1595 case Expr::Type##Class:
Sean Hunt4bfe1962010-05-05 15:24:00 +00001596#include "clang/AST/StmtNodes.inc"
John McCall0512e482010-07-14 04:20:34 +00001597 // fallthrough
1598
1599 // These all can only appear in local or variable-initialization
1600 // contexts and so should never appear in a mangling.
1601 case Expr::AddrLabelExprClass:
1602 case Expr::BlockDeclRefExprClass:
1603 case Expr::CXXThisExprClass:
1604 case Expr::DesignatedInitExprClass:
1605 case Expr::ImplicitValueInitExprClass:
1606 case Expr::InitListExprClass:
1607 case Expr::ParenListExprClass:
1608 case Expr::CXXScalarValueInitExprClass:
John McCall09cc1412010-02-03 00:55:45 +00001609 llvm_unreachable("unexpected statement kind");
1610 break;
1611
John McCall0512e482010-07-14 04:20:34 +00001612 // FIXME: invent manglings for all these.
1613 case Expr::BlockExprClass:
1614 case Expr::CXXPseudoDestructorExprClass:
1615 case Expr::ChooseExprClass:
1616 case Expr::CompoundLiteralExprClass:
1617 case Expr::ExtVectorElementExprClass:
1618 case Expr::ObjCEncodeExprClass:
1619 case Expr::ObjCImplicitSetterGetterRefExprClass:
1620 case Expr::ObjCIsaExprClass:
1621 case Expr::ObjCIvarRefExprClass:
1622 case Expr::ObjCMessageExprClass:
1623 case Expr::ObjCPropertyRefExprClass:
1624 case Expr::ObjCProtocolExprClass:
1625 case Expr::ObjCSelectorExprClass:
1626 case Expr::ObjCStringLiteralClass:
1627 case Expr::ObjCSuperExprClass:
1628 case Expr::OffsetOfExprClass:
1629 case Expr::PredefinedExprClass:
1630 case Expr::ShuffleVectorExprClass:
1631 case Expr::StmtExprClass:
1632 case Expr::TypesCompatibleExprClass:
1633 case Expr::UnaryTypeTraitExprClass:
1634 case Expr::VAArgExprClass: {
John McCall6ae1f352010-04-09 22:26:14 +00001635 // As bad as this diagnostic is, it's better than crashing.
1636 Diagnostic &Diags = Context.getDiags();
1637 unsigned DiagID = Diags.getCustomDiagID(Diagnostic::Error,
1638 "cannot yet mangle expression type %0");
John McCall739bf092010-04-10 09:39:25 +00001639 Diags.Report(FullSourceLoc(E->getExprLoc(),
1640 getASTContext().getSourceManager()),
1641 DiagID)
1642 << E->getStmtClassName() << E->getSourceRange();
John McCall6ae1f352010-04-09 22:26:14 +00001643 break;
1644 }
1645
John McCall0512e482010-07-14 04:20:34 +00001646 case Expr::CXXDefaultArgExprClass:
1647 mangleExpression(cast<CXXDefaultArgExpr>(E)->getExpr());
1648 break;
1649
1650 case Expr::CXXMemberCallExprClass: // fallthrough
John McCall1dd73832010-02-04 01:42:13 +00001651 case Expr::CallExprClass: {
1652 const CallExpr *CE = cast<CallExpr>(E);
1653 Out << "cl";
1654 mangleCalledExpression(CE->getCallee(), CE->getNumArgs());
1655 for (unsigned I = 0, N = CE->getNumArgs(); I != N; ++I)
1656 mangleExpression(CE->getArg(I));
Benjamin Kramer35f59b62010-04-10 16:03:31 +00001657 Out << 'E';
John McCall09cc1412010-02-03 00:55:45 +00001658 break;
John McCall1dd73832010-02-04 01:42:13 +00001659 }
John McCall09cc1412010-02-03 00:55:45 +00001660
John McCall0512e482010-07-14 04:20:34 +00001661 case Expr::CXXNewExprClass: {
1662 // Proposal from David Vandervoorde, 2010.06.30
1663 const CXXNewExpr *New = cast<CXXNewExpr>(E);
1664 if (New->isGlobalNew()) Out << "gs";
1665 Out << (New->isArray() ? "na" : "nw");
1666 for (CXXNewExpr::const_arg_iterator I = New->placement_arg_begin(),
1667 E = New->placement_arg_end(); I != E; ++I)
1668 mangleExpression(*I);
1669 Out << '_';
1670 mangleType(New->getAllocatedType());
1671 if (New->hasInitializer()) {
1672 Out << "pi";
1673 for (CXXNewExpr::const_arg_iterator I = New->constructor_arg_begin(),
1674 E = New->constructor_arg_end(); I != E; ++I)
1675 mangleExpression(*I);
1676 }
1677 Out << 'E';
1678 break;
1679 }
1680
John McCall2f27bf82010-02-04 02:56:29 +00001681 case Expr::MemberExprClass: {
1682 const MemberExpr *ME = cast<MemberExpr>(E);
1683 mangleMemberExpr(ME->getBase(), ME->isArrow(),
1684 ME->getQualifier(), ME->getMemberDecl()->getDeclName(),
1685 UnknownArity);
1686 break;
1687 }
1688
1689 case Expr::UnresolvedMemberExprClass: {
1690 const UnresolvedMemberExpr *ME = cast<UnresolvedMemberExpr>(E);
1691 mangleMemberExpr(ME->getBase(), ME->isArrow(),
1692 ME->getQualifier(), ME->getMemberName(),
1693 UnknownArity);
1694 break;
1695 }
1696
1697 case Expr::CXXDependentScopeMemberExprClass: {
1698 const CXXDependentScopeMemberExpr *ME
1699 = cast<CXXDependentScopeMemberExpr>(E);
1700 mangleMemberExpr(ME->getBase(), ME->isArrow(),
1701 ME->getQualifier(), ME->getMember(),
1702 UnknownArity);
1703 break;
1704 }
1705
John McCall1dd73832010-02-04 01:42:13 +00001706 case Expr::UnresolvedLookupExprClass: {
John McCalla3218e72010-02-04 01:48:38 +00001707 // The ABI doesn't cover how to mangle overload sets, so we mangle
1708 // using something as close as possible to the original lookup
1709 // expression.
John McCall1dd73832010-02-04 01:42:13 +00001710 const UnresolvedLookupExpr *ULE = cast<UnresolvedLookupExpr>(E);
1711 mangleUnresolvedName(ULE->getQualifier(), ULE->getName(), UnknownArity);
1712 break;
1713 }
1714
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001715 case Expr::CXXUnresolvedConstructExprClass: {
John McCall1dd73832010-02-04 01:42:13 +00001716 const CXXUnresolvedConstructExpr *CE = cast<CXXUnresolvedConstructExpr>(E);
1717 unsigned N = CE->arg_size();
1718
1719 Out << "cv";
1720 mangleType(CE->getType());
Benjamin Kramer35f59b62010-04-10 16:03:31 +00001721 if (N != 1) Out << '_';
John McCall1dd73832010-02-04 01:42:13 +00001722 for (unsigned I = 0; I != N; ++I) mangleExpression(CE->getArg(I));
Benjamin Kramer35f59b62010-04-10 16:03:31 +00001723 if (N != 1) Out << 'E';
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001724 break;
John McCall1dd73832010-02-04 01:42:13 +00001725 }
John McCall09cc1412010-02-03 00:55:45 +00001726
John McCall1dd73832010-02-04 01:42:13 +00001727 case Expr::CXXTemporaryObjectExprClass:
1728 case Expr::CXXConstructExprClass: {
1729 const CXXConstructExpr *CE = cast<CXXConstructExpr>(E);
1730 unsigned N = CE->getNumArgs();
1731
1732 Out << "cv";
1733 mangleType(CE->getType());
Benjamin Kramer35f59b62010-04-10 16:03:31 +00001734 if (N != 1) Out << '_';
John McCall1dd73832010-02-04 01:42:13 +00001735 for (unsigned I = 0; I != N; ++I) mangleExpression(CE->getArg(I));
Benjamin Kramer35f59b62010-04-10 16:03:31 +00001736 if (N != 1) Out << 'E';
John McCall09cc1412010-02-03 00:55:45 +00001737 break;
John McCall1dd73832010-02-04 01:42:13 +00001738 }
1739
1740 case Expr::SizeOfAlignOfExprClass: {
1741 const SizeOfAlignOfExpr *SAE = cast<SizeOfAlignOfExpr>(E);
Benjamin Kramer35f59b62010-04-10 16:03:31 +00001742 if (SAE->isSizeOf()) Out << 's';
1743 else Out << 'a';
John McCall1dd73832010-02-04 01:42:13 +00001744 if (SAE->isArgumentType()) {
Benjamin Kramer35f59b62010-04-10 16:03:31 +00001745 Out << 't';
John McCall1dd73832010-02-04 01:42:13 +00001746 mangleType(SAE->getArgumentType());
1747 } else {
Benjamin Kramer35f59b62010-04-10 16:03:31 +00001748 Out << 'z';
John McCall1dd73832010-02-04 01:42:13 +00001749 mangleExpression(SAE->getArgumentExpr());
1750 }
1751 break;
1752 }
Anders Carlssona7694082009-11-06 02:50:19 +00001753
John McCall0512e482010-07-14 04:20:34 +00001754 case Expr::CXXThrowExprClass: {
1755 const CXXThrowExpr *TE = cast<CXXThrowExpr>(E);
1756
1757 // Proposal from David Vandervoorde, 2010.06.30
1758 if (TE->getSubExpr()) {
1759 Out << "tw";
1760 mangleExpression(TE->getSubExpr());
1761 } else {
1762 Out << "tr";
1763 }
1764 break;
1765 }
1766
1767 case Expr::CXXTypeidExprClass: {
1768 const CXXTypeidExpr *TIE = cast<CXXTypeidExpr>(E);
1769
1770 // Proposal from David Vandervoorde, 2010.06.30
1771 if (TIE->isTypeOperand()) {
1772 Out << "ti";
1773 mangleType(TIE->getTypeOperand());
1774 } else {
1775 Out << "te";
1776 mangleExpression(TIE->getExprOperand());
1777 }
1778 break;
1779 }
1780
1781 case Expr::CXXDeleteExprClass: {
1782 const CXXDeleteExpr *DE = cast<CXXDeleteExpr>(E);
1783
1784 // Proposal from David Vandervoorde, 2010.06.30
1785 if (DE->isGlobalDelete()) Out << "gs";
1786 Out << (DE->isArrayForm() ? "da" : "dl");
1787 mangleExpression(DE->getArgument());
1788 break;
1789 }
1790
Anders Carlssone170ba72009-12-14 01:45:37 +00001791 case Expr::UnaryOperatorClass: {
1792 const UnaryOperator *UO = cast<UnaryOperator>(E);
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001793 mangleOperatorName(UnaryOperator::getOverloadedOperator(UO->getOpcode()),
Anders Carlssone170ba72009-12-14 01:45:37 +00001794 /*Arity=*/1);
1795 mangleExpression(UO->getSubExpr());
1796 break;
1797 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001798
John McCall0512e482010-07-14 04:20:34 +00001799 case Expr::ArraySubscriptExprClass: {
1800 const ArraySubscriptExpr *AE = cast<ArraySubscriptExpr>(E);
1801
1802 // Array subscript is treated as a syntactically wierd form of
1803 // binary operator.
1804 Out << "ix";
1805 mangleExpression(AE->getLHS());
1806 mangleExpression(AE->getRHS());
1807 break;
1808 }
1809
1810 case Expr::CompoundAssignOperatorClass: // fallthrough
Anders Carlssone170ba72009-12-14 01:45:37 +00001811 case Expr::BinaryOperatorClass: {
1812 const BinaryOperator *BO = cast<BinaryOperator>(E);
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001813 mangleOperatorName(BinaryOperator::getOverloadedOperator(BO->getOpcode()),
Anders Carlssone170ba72009-12-14 01:45:37 +00001814 /*Arity=*/2);
1815 mangleExpression(BO->getLHS());
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001816 mangleExpression(BO->getRHS());
Anders Carlssone170ba72009-12-14 01:45:37 +00001817 break;
John McCall2f27bf82010-02-04 02:56:29 +00001818 }
Anders Carlssone170ba72009-12-14 01:45:37 +00001819
1820 case Expr::ConditionalOperatorClass: {
1821 const ConditionalOperator *CO = cast<ConditionalOperator>(E);
1822 mangleOperatorName(OO_Conditional, /*Arity=*/3);
1823 mangleExpression(CO->getCond());
1824 mangleExpression(CO->getLHS());
1825 mangleExpression(CO->getRHS());
1826 break;
1827 }
1828
Douglas Gregor46287c72010-01-29 16:37:09 +00001829 case Expr::ImplicitCastExprClass: {
1830 mangleExpression(cast<ImplicitCastExpr>(E)->getSubExpr());
1831 break;
1832 }
1833
1834 case Expr::CStyleCastExprClass:
1835 case Expr::CXXStaticCastExprClass:
1836 case Expr::CXXDynamicCastExprClass:
1837 case Expr::CXXReinterpretCastExprClass:
1838 case Expr::CXXConstCastExprClass:
1839 case Expr::CXXFunctionalCastExprClass: {
1840 const ExplicitCastExpr *ECE = cast<ExplicitCastExpr>(E);
1841 Out << "cv";
1842 mangleType(ECE->getType());
1843 mangleExpression(ECE->getSubExpr());
1844 break;
1845 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001846
Anders Carlsson58040a52009-12-16 05:48:46 +00001847 case Expr::CXXOperatorCallExprClass: {
1848 const CXXOperatorCallExpr *CE = cast<CXXOperatorCallExpr>(E);
1849 unsigned NumArgs = CE->getNumArgs();
1850 mangleOperatorName(CE->getOperator(), /*Arity=*/NumArgs);
1851 // Mangle the arguments.
1852 for (unsigned i = 0; i != NumArgs; ++i)
1853 mangleExpression(CE->getArg(i));
1854 break;
1855 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001856
Anders Carlssona7694082009-11-06 02:50:19 +00001857 case Expr::ParenExprClass:
1858 mangleExpression(cast<ParenExpr>(E)->getSubExpr());
1859 break;
1860
Anders Carlssond553f8c2009-09-21 01:21:10 +00001861 case Expr::DeclRefExprClass: {
Douglas Gregor5ed1bc32010-02-28 21:40:32 +00001862 const NamedDecl *D = cast<DeclRefExpr>(E)->getDecl();
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00001863
Anders Carlssond553f8c2009-09-21 01:21:10 +00001864 switch (D->getKind()) {
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001865 default:
Douglas Gregor5ed1bc32010-02-28 21:40:32 +00001866 // <expr-primary> ::= L <mangled-name> E # external name
1867 Out << 'L';
1868 mangle(D, "_Z");
1869 Out << 'E';
1870 break;
1871
Anders Carlssond553f8c2009-09-21 01:21:10 +00001872 case Decl::NonTypeTemplateParm: {
1873 const NonTypeTemplateParmDecl *PD = cast<NonTypeTemplateParmDecl>(D);
Anders Carlsson0ccdf8d2009-09-27 00:38:53 +00001874 mangleTemplateParameter(PD->getIndex());
Anders Carlssond553f8c2009-09-21 01:21:10 +00001875 break;
1876 }
1877
1878 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00001879
Anders Carlsson50755b02009-09-27 20:11:34 +00001880 break;
Anders Carlssond553f8c2009-09-21 01:21:10 +00001881 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00001882
John McCall865d4472009-11-19 22:55:06 +00001883 case Expr::DependentScopeDeclRefExprClass: {
1884 const DependentScopeDeclRefExpr *DRE = cast<DependentScopeDeclRefExpr>(E);
Douglas Gregor4b2ccfc2010-02-28 22:05:49 +00001885 NestedNameSpecifier *NNS = DRE->getQualifier();
1886 const Type *QTy = NNS->getAsType();
1887
1888 // When we're dealing with a nested-name-specifier that has just a
1889 // dependent identifier in it, mangle that as a typename. FIXME:
1890 // It isn't clear that we ever actually want to have such a
1891 // nested-name-specifier; why not just represent it as a typename type?
1892 if (!QTy && NNS->getAsIdentifier() && NNS->getPrefix()) {
Douglas Gregor4a2023f2010-03-31 20:19:30 +00001893 QTy = getASTContext().getDependentNameType(ETK_Typename,
1894 NNS->getPrefix(),
1895 NNS->getAsIdentifier())
Douglas Gregor4b2ccfc2010-02-28 22:05:49 +00001896 .getTypePtr();
1897 }
Anders Carlsson50755b02009-09-27 20:11:34 +00001898 assert(QTy && "Qualifier was not type!");
1899
1900 // ::= sr <type> <unqualified-name> # dependent name
1901 Out << "sr";
1902 mangleType(QualType(QTy, 0));
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00001903
Anders Carlsson50755b02009-09-27 20:11:34 +00001904 assert(DRE->getDeclName().getNameKind() == DeclarationName::Identifier &&
1905 "Unhandled decl name kind!");
1906 mangleSourceName(DRE->getDeclName().getAsIdentifierInfo());
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00001907
Anders Carlsson50755b02009-09-27 20:11:34 +00001908 break;
1909 }
1910
John McCalld9307602010-04-09 22:54:09 +00001911 case Expr::CXXBindReferenceExprClass:
1912 mangleExpression(cast<CXXBindReferenceExpr>(E)->getSubExpr());
1913 break;
1914
1915 case Expr::CXXBindTemporaryExprClass:
1916 mangleExpression(cast<CXXBindTemporaryExpr>(E)->getSubExpr());
1917 break;
1918
1919 case Expr::CXXExprWithTemporariesClass:
1920 mangleExpression(cast<CXXExprWithTemporaries>(E)->getSubExpr());
1921 break;
1922
John McCall1dd73832010-02-04 01:42:13 +00001923 case Expr::FloatingLiteralClass: {
1924 const FloatingLiteral *FL = cast<FloatingLiteral>(E);
Benjamin Kramer35f59b62010-04-10 16:03:31 +00001925 Out << 'L';
John McCall1dd73832010-02-04 01:42:13 +00001926 mangleType(FL->getType());
John McCall0512e482010-07-14 04:20:34 +00001927 mangleFloat(FL->getValue());
Benjamin Kramer35f59b62010-04-10 16:03:31 +00001928 Out << 'E';
John McCall1dd73832010-02-04 01:42:13 +00001929 break;
1930 }
1931
John McCallde810632010-04-09 21:48:08 +00001932 case Expr::CharacterLiteralClass:
Benjamin Kramer35f59b62010-04-10 16:03:31 +00001933 Out << 'L';
John McCallde810632010-04-09 21:48:08 +00001934 mangleType(E->getType());
1935 Out << cast<CharacterLiteral>(E)->getValue();
1936 Out << 'E';
1937 break;
1938
1939 case Expr::CXXBoolLiteralExprClass:
1940 Out << "Lb";
1941 Out << (cast<CXXBoolLiteralExpr>(E)->getValue() ? '1' : '0');
1942 Out << 'E';
1943 break;
1944
John McCall0512e482010-07-14 04:20:34 +00001945 case Expr::IntegerLiteralClass: {
1946 llvm::APSInt Value(cast<IntegerLiteral>(E)->getValue());
1947 if (E->getType()->isSignedIntegerType())
1948 Value.setIsSigned(true);
1949 mangleIntegerLiteral(E->getType(), Value);
Anders Carlssone170ba72009-12-14 01:45:37 +00001950 break;
John McCall0512e482010-07-14 04:20:34 +00001951 }
1952
1953 case Expr::ImaginaryLiteralClass: {
1954 const ImaginaryLiteral *IE = cast<ImaginaryLiteral>(E);
1955 // Mangle as if a complex literal.
1956 // Proposal from David Vandervoorde, 2010.06.30.
1957 Out << 'L';
1958 mangleType(E->getType());
1959 if (const FloatingLiteral *Imag =
1960 dyn_cast<FloatingLiteral>(IE->getSubExpr())) {
1961 // Mangle a floating-point zero of the appropriate type.
1962 mangleFloat(llvm::APFloat(Imag->getValue().getSemantics()));
1963 Out << '_';
1964 mangleFloat(Imag->getValue());
1965 } else {
1966 Out << '0' << '_';
1967 llvm::APSInt Value(cast<IntegerLiteral>(IE->getSubExpr())->getValue());
1968 if (IE->getSubExpr()->getType()->isSignedIntegerType())
1969 Value.setIsSigned(true);
1970 mangleNumber(Value);
1971 }
1972 Out << 'E';
1973 break;
1974 }
1975
1976 case Expr::StringLiteralClass: {
1977 // Proposal from David Vandervoorde, 2010.06.30.
1978 // I've sent a comment off asking whether this needs to also
1979 // represent the length of the string.
1980 Out << 'L';
1981 const ConstantArrayType *T = cast<ConstantArrayType>(E->getType());
1982 QualType CharTy = T->getElementType().getUnqualifiedType();
1983 mangleType(CharTy);
1984 Out << 'E';
1985 break;
1986 }
1987
1988 case Expr::GNUNullExprClass:
1989 // FIXME: should this really be mangled the same as nullptr?
1990 // fallthrough
1991
1992 case Expr::CXXNullPtrLiteralExprClass: {
1993 // Proposal from David Vandervoorde, 2010.06.30, as
1994 // modified by ABI list discussion.
1995 Out << "LDnE";
1996 break;
1997 }
Anders Carlssone170ba72009-12-14 01:45:37 +00001998
Anders Carlssond553f8c2009-09-21 01:21:10 +00001999 }
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00002000}
2001
Anders Carlsson3ac86b52009-04-15 05:36:58 +00002002void CXXNameMangler::mangleCXXCtorType(CXXCtorType T) {
2003 // <ctor-dtor-name> ::= C1 # complete object constructor
2004 // ::= C2 # base object constructor
2005 // ::= C3 # complete object allocating constructor
2006 //
2007 switch (T) {
2008 case Ctor_Complete:
2009 Out << "C1";
2010 break;
2011 case Ctor_Base:
2012 Out << "C2";
2013 break;
2014 case Ctor_CompleteAllocating:
2015 Out << "C3";
2016 break;
2017 }
2018}
2019
Anders Carlsson27ae5362009-04-17 01:58:57 +00002020void CXXNameMangler::mangleCXXDtorType(CXXDtorType T) {
2021 // <ctor-dtor-name> ::= D0 # deleting destructor
2022 // ::= D1 # complete object destructor
2023 // ::= D2 # base object destructor
2024 //
2025 switch (T) {
2026 case Dtor_Deleting:
2027 Out << "D0";
2028 break;
2029 case Dtor_Complete:
2030 Out << "D1";
2031 break;
2032 case Dtor_Base:
2033 Out << "D2";
2034 break;
2035 }
2036}
2037
Douglas Gregor20f0cc72010-04-23 03:10:43 +00002038void CXXNameMangler::mangleTemplateArgs(TemplateName Template,
2039 const TemplateArgument *TemplateArgs,
2040 unsigned NumTemplateArgs) {
2041 if (TemplateDecl *TD = Template.getAsTemplateDecl())
2042 return mangleTemplateArgs(*TD->getTemplateParameters(), TemplateArgs,
2043 NumTemplateArgs);
Sean Huntc3021132010-05-05 15:23:54 +00002044
Douglas Gregor20f0cc72010-04-23 03:10:43 +00002045 // <template-args> ::= I <template-arg>+ E
2046 Out << 'I';
2047 for (unsigned i = 0; i != NumTemplateArgs; ++i)
2048 mangleTemplateArg(0, TemplateArgs[i]);
2049 Out << 'E';
2050}
2051
Rafael Espindolad9800722010-03-11 14:07:00 +00002052void CXXNameMangler::mangleTemplateArgs(const TemplateParameterList &PL,
2053 const TemplateArgumentList &AL) {
Anders Carlsson7a0ba872009-05-15 16:09:15 +00002054 // <template-args> ::= I <template-arg>+ E
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002055 Out << 'I';
Rafael Espindolad9800722010-03-11 14:07:00 +00002056 for (unsigned i = 0, e = AL.size(); i != e; ++i)
2057 mangleTemplateArg(PL.getParam(i), AL[i]);
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002058 Out << 'E';
Anders Carlsson7a0ba872009-05-15 16:09:15 +00002059}
2060
Rafael Espindolad9800722010-03-11 14:07:00 +00002061void CXXNameMangler::mangleTemplateArgs(const TemplateParameterList &PL,
2062 const TemplateArgument *TemplateArgs,
Anders Carlsson7624f212009-09-18 02:42:01 +00002063 unsigned NumTemplateArgs) {
2064 // <template-args> ::= I <template-arg>+ E
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002065 Out << 'I';
Daniel Dunbar7e0c1952009-11-21 09:17:15 +00002066 for (unsigned i = 0; i != NumTemplateArgs; ++i)
Rafael Espindolad9800722010-03-11 14:07:00 +00002067 mangleTemplateArg(PL.getParam(i), TemplateArgs[i]);
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002068 Out << 'E';
Anders Carlsson7624f212009-09-18 02:42:01 +00002069}
2070
Rafael Espindolad9800722010-03-11 14:07:00 +00002071void CXXNameMangler::mangleTemplateArg(const NamedDecl *P,
2072 const TemplateArgument &A) {
Mike Stump1eb44332009-09-09 15:08:12 +00002073 // <template-arg> ::= <type> # type or template
Anders Carlsson7a0ba872009-05-15 16:09:15 +00002074 // ::= X <expression> E # expression
2075 // ::= <expr-primary> # simple expressions
2076 // ::= I <template-arg>* E # argument pack
2077 // ::= sp <expression> # pack expansion of (C++0x)
2078 switch (A.getKind()) {
2079 default:
2080 assert(0 && "Unknown template argument kind!");
2081 case TemplateArgument::Type:
2082 mangleType(A.getAsType());
2083 break;
Anders Carlsson9e85c742009-12-23 19:30:55 +00002084 case TemplateArgument::Template:
John McCallb6f532e2010-07-14 06:43:17 +00002085 // This is mangled as <type>.
2086 mangleType(A.getAsTemplate());
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002087 break;
Anders Carlssond553f8c2009-09-21 01:21:10 +00002088 case TemplateArgument::Expression:
2089 Out << 'X';
2090 mangleExpression(A.getAsExpr());
2091 Out << 'E';
2092 break;
Anders Carlssone170ba72009-12-14 01:45:37 +00002093 case TemplateArgument::Integral:
2094 mangleIntegerLiteral(A.getIntegralType(), *A.getAsIntegral());
Anders Carlsson7a0ba872009-05-15 16:09:15 +00002095 break;
Daniel Dunbar7e0c1952009-11-21 09:17:15 +00002096 case TemplateArgument::Declaration: {
Douglas Gregor20f0cc72010-04-23 03:10:43 +00002097 assert(P && "Missing template parameter for declaration argument");
Daniel Dunbar7e0c1952009-11-21 09:17:15 +00002098 // <expr-primary> ::= L <mangled-name> E # external name
2099
Rafael Espindolad9800722010-03-11 14:07:00 +00002100 // Clang produces AST's where pointer-to-member-function expressions
Daniel Dunbar7e0c1952009-11-21 09:17:15 +00002101 // and pointer-to-function expressions are represented as a declaration not
Rafael Espindolad9800722010-03-11 14:07:00 +00002102 // an expression. We compensate for it here to produce the correct mangling.
2103 NamedDecl *D = cast<NamedDecl>(A.getAsDecl());
2104 const NonTypeTemplateParmDecl *Parameter = cast<NonTypeTemplateParmDecl>(P);
2105 bool compensateMangling = D->isCXXClassMember() &&
2106 !Parameter->getType()->isReferenceType();
2107 if (compensateMangling) {
2108 Out << 'X';
2109 mangleOperatorName(OO_Amp, 1);
2110 }
2111
Daniel Dunbar7e0c1952009-11-21 09:17:15 +00002112 Out << 'L';
2113 // References to external entities use the mangled name; if the name would
2114 // not normally be manged then mangle it as unqualified.
2115 //
2116 // FIXME: The ABI specifies that external names here should have _Z, but
2117 // gcc leaves this off.
Rafael Espindolad9800722010-03-11 14:07:00 +00002118 if (compensateMangling)
2119 mangle(D, "_Z");
2120 else
2121 mangle(D, "Z");
Daniel Dunbar7e0c1952009-11-21 09:17:15 +00002122 Out << 'E';
Rafael Espindolad9800722010-03-11 14:07:00 +00002123
2124 if (compensateMangling)
2125 Out << 'E';
2126
Daniel Dunbar7e0c1952009-11-21 09:17:15 +00002127 break;
2128 }
2129 }
Anders Carlsson7a0ba872009-05-15 16:09:15 +00002130}
2131
Anders Carlsson0ccdf8d2009-09-27 00:38:53 +00002132void CXXNameMangler::mangleTemplateParameter(unsigned Index) {
2133 // <template-param> ::= T_ # first template parameter
2134 // ::= T <parameter-2 non-negative number> _
2135 if (Index == 0)
2136 Out << "T_";
2137 else
2138 Out << 'T' << (Index - 1) << '_';
2139}
2140
Anders Carlsson76967372009-09-17 00:43:46 +00002141// <substitution> ::= S <seq-id> _
2142// ::= S_
Anders Carlsson6862fc72009-09-17 04:16:28 +00002143bool CXXNameMangler::mangleSubstitution(const NamedDecl *ND) {
Anders Carlssone7c8cb62009-09-26 20:53:44 +00002144 // Try one of the standard substitutions first.
2145 if (mangleStandardSubstitution(ND))
2146 return true;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002147
Anders Carlsson433d1372009-11-07 04:26:04 +00002148 ND = cast<NamedDecl>(ND->getCanonicalDecl());
Anders Carlsson6862fc72009-09-17 04:16:28 +00002149 return mangleSubstitution(reinterpret_cast<uintptr_t>(ND));
2150}
2151
Anders Carlsson76967372009-09-17 00:43:46 +00002152bool CXXNameMangler::mangleSubstitution(QualType T) {
Anders Carlssond99edc42009-09-26 03:55:37 +00002153 if (!T.getCVRQualifiers()) {
2154 if (const RecordType *RT = T->getAs<RecordType>())
2155 return mangleSubstitution(RT->getDecl());
2156 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002157
Anders Carlsson76967372009-09-17 00:43:46 +00002158 uintptr_t TypePtr = reinterpret_cast<uintptr_t>(T.getAsOpaquePtr());
2159
Anders Carlssond3a932a2009-09-17 03:53:28 +00002160 return mangleSubstitution(TypePtr);
2161}
2162
Douglas Gregor1e9268e2010-04-28 05:58:56 +00002163bool CXXNameMangler::mangleSubstitution(TemplateName Template) {
2164 if (TemplateDecl *TD = Template.getAsTemplateDecl())
2165 return mangleSubstitution(TD);
Sean Huntc3021132010-05-05 15:23:54 +00002166
Douglas Gregor1e9268e2010-04-28 05:58:56 +00002167 Template = Context.getASTContext().getCanonicalTemplateName(Template);
2168 return mangleSubstitution(
2169 reinterpret_cast<uintptr_t>(Template.getAsVoidPointer()));
2170}
2171
Anders Carlssond3a932a2009-09-17 03:53:28 +00002172bool CXXNameMangler::mangleSubstitution(uintptr_t Ptr) {
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002173 llvm::DenseMap<uintptr_t, unsigned>::iterator I = Substitutions.find(Ptr);
Anders Carlsson76967372009-09-17 00:43:46 +00002174 if (I == Substitutions.end())
2175 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002176
Anders Carlsson76967372009-09-17 00:43:46 +00002177 unsigned SeqID = I->second;
2178 if (SeqID == 0)
2179 Out << "S_";
2180 else {
2181 SeqID--;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002182
Anders Carlsson76967372009-09-17 00:43:46 +00002183 // <seq-id> is encoded in base-36, using digits and upper case letters.
2184 char Buffer[10];
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002185 char *BufferPtr = llvm::array_endof(Buffer);
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002186
Anders Carlsson76967372009-09-17 00:43:46 +00002187 if (SeqID == 0) *--BufferPtr = '0';
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002188
Anders Carlsson76967372009-09-17 00:43:46 +00002189 while (SeqID) {
2190 assert(BufferPtr > Buffer && "Buffer overflow!");
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002191
John McCall6ab30e02010-06-09 07:26:17 +00002192 char c = static_cast<char>(SeqID % 36);
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002193
Anders Carlsson76967372009-09-17 00:43:46 +00002194 *--BufferPtr = (c < 10 ? '0' + c : 'A' + c - 10);
2195 SeqID /= 36;
2196 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002197
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002198 Out << 'S'
2199 << llvm::StringRef(BufferPtr, llvm::array_endof(Buffer)-BufferPtr)
2200 << '_';
Anders Carlsson76967372009-09-17 00:43:46 +00002201 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002202
Anders Carlsson76967372009-09-17 00:43:46 +00002203 return true;
2204}
2205
Anders Carlssonf514b542009-09-27 00:12:57 +00002206static bool isCharType(QualType T) {
2207 if (T.isNull())
2208 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002209
Anders Carlssonf514b542009-09-27 00:12:57 +00002210 return T->isSpecificBuiltinType(BuiltinType::Char_S) ||
2211 T->isSpecificBuiltinType(BuiltinType::Char_U);
2212}
2213
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002214/// isCharSpecialization - Returns whether a given type is a template
Anders Carlssonf514b542009-09-27 00:12:57 +00002215/// specialization of a given name with a single argument of type char.
2216static bool isCharSpecialization(QualType T, const char *Name) {
2217 if (T.isNull())
2218 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002219
Anders Carlssonf514b542009-09-27 00:12:57 +00002220 const RecordType *RT = T->getAs<RecordType>();
2221 if (!RT)
2222 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002223
2224 const ClassTemplateSpecializationDecl *SD =
Anders Carlssonf514b542009-09-27 00:12:57 +00002225 dyn_cast<ClassTemplateSpecializationDecl>(RT->getDecl());
2226 if (!SD)
2227 return false;
2228
2229 if (!isStdNamespace(SD->getDeclContext()))
2230 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002231
Anders Carlssonf514b542009-09-27 00:12:57 +00002232 const TemplateArgumentList &TemplateArgs = SD->getTemplateArgs();
2233 if (TemplateArgs.size() != 1)
2234 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002235
Anders Carlssonf514b542009-09-27 00:12:57 +00002236 if (!isCharType(TemplateArgs[0].getAsType()))
2237 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002238
Daniel Dunbar01eb9b92009-10-18 21:17:35 +00002239 return SD->getIdentifier()->getName() == Name;
Anders Carlssonf514b542009-09-27 00:12:57 +00002240}
2241
Anders Carlsson91f88602009-12-07 19:56:42 +00002242template <std::size_t StrLen>
2243bool isStreamCharSpecialization(const ClassTemplateSpecializationDecl *SD,
2244 const char (&Str)[StrLen]) {
2245 if (!SD->getIdentifier()->isStr(Str))
2246 return false;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002247
Anders Carlsson91f88602009-12-07 19:56:42 +00002248 const TemplateArgumentList &TemplateArgs = SD->getTemplateArgs();
2249 if (TemplateArgs.size() != 2)
2250 return false;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002251
Anders Carlsson91f88602009-12-07 19:56:42 +00002252 if (!isCharType(TemplateArgs[0].getAsType()))
2253 return false;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002254
Anders Carlsson91f88602009-12-07 19:56:42 +00002255 if (!isCharSpecialization(TemplateArgs[1].getAsType(), "char_traits"))
2256 return false;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002257
Anders Carlsson91f88602009-12-07 19:56:42 +00002258 return true;
2259}
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002260
Anders Carlssone7c8cb62009-09-26 20:53:44 +00002261bool CXXNameMangler::mangleStandardSubstitution(const NamedDecl *ND) {
2262 // <substitution> ::= St # ::std::
Anders Carlsson8c031552009-09-26 23:10:05 +00002263 if (const NamespaceDecl *NS = dyn_cast<NamespaceDecl>(ND)) {
Anders Carlsson47846d22009-12-04 06:23:23 +00002264 if (isStd(NS)) {
Anders Carlsson8c031552009-09-26 23:10:05 +00002265 Out << "St";
2266 return true;
2267 }
2268 }
2269
2270 if (const ClassTemplateDecl *TD = dyn_cast<ClassTemplateDecl>(ND)) {
2271 if (!isStdNamespace(TD->getDeclContext()))
2272 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002273
Anders Carlsson8c031552009-09-26 23:10:05 +00002274 // <substitution> ::= Sa # ::std::allocator
2275 if (TD->getIdentifier()->isStr("allocator")) {
2276 Out << "Sa";
2277 return true;
2278 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002279
Anders Carlsson189d59c2009-09-26 23:14:39 +00002280 // <<substitution> ::= Sb # ::std::basic_string
2281 if (TD->getIdentifier()->isStr("basic_string")) {
2282 Out << "Sb";
2283 return true;
2284 }
Anders Carlsson8c031552009-09-26 23:10:05 +00002285 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002286
2287 if (const ClassTemplateSpecializationDecl *SD =
Anders Carlssonf514b542009-09-27 00:12:57 +00002288 dyn_cast<ClassTemplateSpecializationDecl>(ND)) {
Eli Friedman5370ee22010-02-23 18:25:09 +00002289 if (!isStdNamespace(SD->getDeclContext()))
2290 return false;
2291
Anders Carlssonf514b542009-09-27 00:12:57 +00002292 // <substitution> ::= Ss # ::std::basic_string<char,
2293 // ::std::char_traits<char>,
2294 // ::std::allocator<char> >
2295 if (SD->getIdentifier()->isStr("basic_string")) {
2296 const TemplateArgumentList &TemplateArgs = SD->getTemplateArgs();
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002297
Anders Carlssonf514b542009-09-27 00:12:57 +00002298 if (TemplateArgs.size() != 3)
2299 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002300
Anders Carlssonf514b542009-09-27 00:12:57 +00002301 if (!isCharType(TemplateArgs[0].getAsType()))
2302 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002303
Anders Carlssonf514b542009-09-27 00:12:57 +00002304 if (!isCharSpecialization(TemplateArgs[1].getAsType(), "char_traits"))
2305 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002306
Anders Carlssonf514b542009-09-27 00:12:57 +00002307 if (!isCharSpecialization(TemplateArgs[2].getAsType(), "allocator"))
2308 return false;
2309
2310 Out << "Ss";
2311 return true;
2312 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002313
Anders Carlsson91f88602009-12-07 19:56:42 +00002314 // <substitution> ::= Si # ::std::basic_istream<char,
2315 // ::std::char_traits<char> >
2316 if (isStreamCharSpecialization(SD, "basic_istream")) {
2317 Out << "Si";
2318 return true;
2319 }
2320
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002321 // <substitution> ::= So # ::std::basic_ostream<char,
Anders Carlsson8f8fd8e2009-10-08 17:20:26 +00002322 // ::std::char_traits<char> >
Anders Carlsson91f88602009-12-07 19:56:42 +00002323 if (isStreamCharSpecialization(SD, "basic_ostream")) {
Anders Carlsson8f8fd8e2009-10-08 17:20:26 +00002324 Out << "So";
2325 return true;
2326 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002327
Anders Carlsson91f88602009-12-07 19:56:42 +00002328 // <substitution> ::= Sd # ::std::basic_iostream<char,
2329 // ::std::char_traits<char> >
2330 if (isStreamCharSpecialization(SD, "basic_iostream")) {
2331 Out << "Sd";
2332 return true;
2333 }
Anders Carlssonf514b542009-09-27 00:12:57 +00002334 }
Anders Carlsson8c031552009-09-26 23:10:05 +00002335 return false;
Anders Carlssone7c8cb62009-09-26 20:53:44 +00002336}
2337
Anders Carlsson76967372009-09-17 00:43:46 +00002338void CXXNameMangler::addSubstitution(QualType T) {
Anders Carlssond99edc42009-09-26 03:55:37 +00002339 if (!T.getCVRQualifiers()) {
2340 if (const RecordType *RT = T->getAs<RecordType>()) {
2341 addSubstitution(RT->getDecl());
2342 return;
2343 }
2344 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002345
Anders Carlsson76967372009-09-17 00:43:46 +00002346 uintptr_t TypePtr = reinterpret_cast<uintptr_t>(T.getAsOpaquePtr());
Anders Carlssond3a932a2009-09-17 03:53:28 +00002347 addSubstitution(TypePtr);
2348}
2349
Douglas Gregor1e9268e2010-04-28 05:58:56 +00002350void CXXNameMangler::addSubstitution(TemplateName Template) {
2351 if (TemplateDecl *TD = Template.getAsTemplateDecl())
2352 return addSubstitution(TD);
Sean Huntc3021132010-05-05 15:23:54 +00002353
Douglas Gregor1e9268e2010-04-28 05:58:56 +00002354 Template = Context.getASTContext().getCanonicalTemplateName(Template);
2355 addSubstitution(reinterpret_cast<uintptr_t>(Template.getAsVoidPointer()));
2356}
2357
Anders Carlssond3a932a2009-09-17 03:53:28 +00002358void CXXNameMangler::addSubstitution(uintptr_t Ptr) {
Anders Carlssond3a932a2009-09-17 03:53:28 +00002359 assert(!Substitutions.count(Ptr) && "Substitution already exists!");
Anders Carlsson9d85b722010-06-02 04:29:50 +00002360 Substitutions[Ptr] = SeqID++;
Anders Carlsson76967372009-09-17 00:43:46 +00002361}
2362
Daniel Dunbar1b077112009-11-21 09:06:10 +00002363//
Mike Stump1eb44332009-09-09 15:08:12 +00002364
Daniel Dunbar1b077112009-11-21 09:06:10 +00002365/// \brief Mangles the name of the declaration D and emits that name to the
2366/// given output stream.
2367///
2368/// If the declaration D requires a mangled name, this routine will emit that
2369/// mangled name to \p os and return true. Otherwise, \p os will be unchanged
2370/// and this routine will return false. In this case, the caller should just
2371/// emit the identifier of the declaration (\c D->getIdentifier()) as its
2372/// name.
Daniel Dunbarf981bf82009-11-21 09:14:52 +00002373void MangleContext::mangleName(const NamedDecl *D,
Daniel Dunbar94fd26d2009-11-21 09:06:22 +00002374 llvm::SmallVectorImpl<char> &Res) {
Daniel Dunbarc02ab4c2009-11-21 09:14:44 +00002375 assert((isa<FunctionDecl>(D) || isa<VarDecl>(D)) &&
2376 "Invalid mangleName() call, argument is not a variable or function!");
2377 assert(!isa<CXXConstructorDecl>(D) && !isa<CXXDestructorDecl>(D) &&
2378 "Invalid mangleName() call on 'structor decl!");
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002379
Daniel Dunbar1b077112009-11-21 09:06:10 +00002380 PrettyStackTraceDecl CrashInfo(D, SourceLocation(),
2381 getASTContext().getSourceManager(),
2382 "Mangling declaration");
Mike Stump1eb44332009-09-09 15:08:12 +00002383
Daniel Dunbar94fd26d2009-11-21 09:06:22 +00002384 CXXNameMangler Mangler(*this, Res);
2385 return Mangler.mangle(D);
Daniel Dunbar1b077112009-11-21 09:06:10 +00002386}
Mike Stump1eb44332009-09-09 15:08:12 +00002387
Daniel Dunbar1b077112009-11-21 09:06:10 +00002388void MangleContext::mangleCXXCtor(const CXXConstructorDecl *D, CXXCtorType Type,
Daniel Dunbar94fd26d2009-11-21 09:06:22 +00002389 llvm::SmallVectorImpl<char> &Res) {
Daniel Dunbar77939c92009-11-21 09:06:31 +00002390 CXXNameMangler Mangler(*this, Res, D, Type);
2391 Mangler.mangle(D);
Daniel Dunbar1b077112009-11-21 09:06:10 +00002392}
Mike Stump1eb44332009-09-09 15:08:12 +00002393
Daniel Dunbar1b077112009-11-21 09:06:10 +00002394void MangleContext::mangleCXXDtor(const CXXDestructorDecl *D, CXXDtorType Type,
Daniel Dunbar94fd26d2009-11-21 09:06:22 +00002395 llvm::SmallVectorImpl<char> &Res) {
Daniel Dunbar77939c92009-11-21 09:06:31 +00002396 CXXNameMangler Mangler(*this, Res, D, Type);
2397 Mangler.mangle(D);
Daniel Dunbar1b077112009-11-21 09:06:10 +00002398}
Mike Stumpf1216772009-07-31 18:25:34 +00002399
Fariborz Jahanian564360b2010-06-24 00:08:06 +00002400void MangleContext::mangleBlock(GlobalDecl GD, const BlockDecl *BD,
Douglas Gregor35415f52010-05-25 17:04:15 +00002401 llvm::SmallVectorImpl<char> &Res) {
Charles Davis685b1d92010-05-26 18:25:27 +00002402 MiscNameMangler Mangler(*this, Res);
Fariborz Jahanian564360b2010-06-24 00:08:06 +00002403 Mangler.mangleBlock(GD, BD);
Douglas Gregor35415f52010-05-25 17:04:15 +00002404}
2405
Anders Carlsson19879c92010-03-23 17:17:29 +00002406void MangleContext::mangleThunk(const CXXMethodDecl *MD,
2407 const ThunkInfo &Thunk,
2408 llvm::SmallVectorImpl<char> &Res) {
2409 // <special-name> ::= T <call-offset> <base encoding>
2410 // # base is the nominal target function of thunk
2411 // <special-name> ::= Tc <call-offset> <call-offset> <base encoding>
2412 // # base is the nominal target function of thunk
2413 // # first call-offset is 'this' adjustment
2414 // # second call-offset is result adjustment
Sean Huntc3021132010-05-05 15:23:54 +00002415
Anders Carlsson19879c92010-03-23 17:17:29 +00002416 assert(!isa<CXXDestructorDecl>(MD) &&
2417 "Use mangleCXXDtor for destructor decls!");
Sean Huntc3021132010-05-05 15:23:54 +00002418
Anders Carlsson19879c92010-03-23 17:17:29 +00002419 CXXNameMangler Mangler(*this, Res);
2420 Mangler.getStream() << "_ZT";
2421 if (!Thunk.Return.isEmpty())
2422 Mangler.getStream() << 'c';
Sean Huntc3021132010-05-05 15:23:54 +00002423
Anders Carlsson19879c92010-03-23 17:17:29 +00002424 // Mangle the 'this' pointer adjustment.
2425 Mangler.mangleCallOffset(Thunk.This.NonVirtual, Thunk.This.VCallOffsetOffset);
Sean Huntc3021132010-05-05 15:23:54 +00002426
Anders Carlsson19879c92010-03-23 17:17:29 +00002427 // Mangle the return pointer adjustment if there is one.
2428 if (!Thunk.Return.isEmpty())
2429 Mangler.mangleCallOffset(Thunk.Return.NonVirtual,
2430 Thunk.Return.VBaseOffsetOffset);
Sean Huntc3021132010-05-05 15:23:54 +00002431
Anders Carlsson19879c92010-03-23 17:17:29 +00002432 Mangler.mangleFunctionEncoding(MD);
2433}
2434
Sean Huntc3021132010-05-05 15:23:54 +00002435void
Anders Carlsson19879c92010-03-23 17:17:29 +00002436MangleContext::mangleCXXDtorThunk(const CXXDestructorDecl *DD, CXXDtorType Type,
2437 const ThisAdjustment &ThisAdjustment,
2438 llvm::SmallVectorImpl<char> &Res) {
2439 // <special-name> ::= T <call-offset> <base encoding>
2440 // # base is the nominal target function of thunk
Sean Huntc3021132010-05-05 15:23:54 +00002441
Anders Carlsson19879c92010-03-23 17:17:29 +00002442 CXXNameMangler Mangler(*this, Res, DD, Type);
2443 Mangler.getStream() << "_ZT";
2444
2445 // Mangle the 'this' pointer adjustment.
Sean Huntc3021132010-05-05 15:23:54 +00002446 Mangler.mangleCallOffset(ThisAdjustment.NonVirtual,
Anders Carlsson19879c92010-03-23 17:17:29 +00002447 ThisAdjustment.VCallOffsetOffset);
2448
2449 Mangler.mangleFunctionEncoding(DD);
2450}
2451
Daniel Dunbarc0747712009-11-21 09:12:13 +00002452/// mangleGuardVariable - Returns the mangled name for a guard variable
2453/// for the passed in VarDecl.
2454void MangleContext::mangleGuardVariable(const VarDecl *D,
2455 llvm::SmallVectorImpl<char> &Res) {
2456 // <special-name> ::= GV <object name> # Guard variable for one-time
2457 // # initialization
2458 CXXNameMangler Mangler(*this, Res);
2459 Mangler.getStream() << "_ZGV";
2460 Mangler.mangleName(D);
2461}
2462
Anders Carlsson715edf22010-06-26 16:09:40 +00002463void MangleContext::mangleReferenceTemporary(const VarDecl *D,
2464 llvm::SmallVectorImpl<char> &Res) {
2465 // We match the GCC mangling here.
2466 // <special-name> ::= GR <object name>
2467 CXXNameMangler Mangler(*this, Res);
2468 Mangler.getStream() << "_ZGR";
2469 Mangler.mangleName(D);
2470}
2471
Anders Carlsson046c2942010-04-17 20:15:18 +00002472void MangleContext::mangleCXXVTable(const CXXRecordDecl *RD,
Daniel Dunbar94fd26d2009-11-21 09:06:22 +00002473 llvm::SmallVectorImpl<char> &Res) {
Daniel Dunbarc0747712009-11-21 09:12:13 +00002474 // <special-name> ::= TV <type> # virtual table
Daniel Dunbar94fd26d2009-11-21 09:06:22 +00002475 CXXNameMangler Mangler(*this, Res);
Daniel Dunbarc0747712009-11-21 09:12:13 +00002476 Mangler.getStream() << "_ZTV";
Douglas Gregor1b12a3b2010-05-26 05:11:13 +00002477 Mangler.mangleNameOrStandardSubstitution(RD);
Daniel Dunbar1b077112009-11-21 09:06:10 +00002478}
Mike Stump82d75b02009-11-10 01:58:37 +00002479
Daniel Dunbar1b077112009-11-21 09:06:10 +00002480void MangleContext::mangleCXXVTT(const CXXRecordDecl *RD,
Daniel Dunbar94fd26d2009-11-21 09:06:22 +00002481 llvm::SmallVectorImpl<char> &Res) {
Daniel Dunbarc0747712009-11-21 09:12:13 +00002482 // <special-name> ::= TT <type> # VTT structure
Daniel Dunbar94fd26d2009-11-21 09:06:22 +00002483 CXXNameMangler Mangler(*this, Res);
Daniel Dunbarc0747712009-11-21 09:12:13 +00002484 Mangler.getStream() << "_ZTT";
Douglas Gregor1b12a3b2010-05-26 05:11:13 +00002485 Mangler.mangleNameOrStandardSubstitution(RD);
Daniel Dunbar1b077112009-11-21 09:06:10 +00002486}
Mike Stumpab3f7e92009-11-10 01:41:59 +00002487
Anders Carlsson046c2942010-04-17 20:15:18 +00002488void MangleContext::mangleCXXCtorVTable(const CXXRecordDecl *RD, int64_t Offset,
Daniel Dunbar1b077112009-11-21 09:06:10 +00002489 const CXXRecordDecl *Type,
Daniel Dunbar94fd26d2009-11-21 09:06:22 +00002490 llvm::SmallVectorImpl<char> &Res) {
Daniel Dunbarc0747712009-11-21 09:12:13 +00002491 // <special-name> ::= TC <type> <offset number> _ <base type>
Daniel Dunbar94fd26d2009-11-21 09:06:22 +00002492 CXXNameMangler Mangler(*this, Res);
Daniel Dunbarc0747712009-11-21 09:12:13 +00002493 Mangler.getStream() << "_ZTC";
Douglas Gregor1b12a3b2010-05-26 05:11:13 +00002494 Mangler.mangleNameOrStandardSubstitution(RD);
Daniel Dunbarc0747712009-11-21 09:12:13 +00002495 Mangler.getStream() << Offset;
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002496 Mangler.getStream() << '_';
Douglas Gregor1b12a3b2010-05-26 05:11:13 +00002497 Mangler.mangleNameOrStandardSubstitution(Type);
Daniel Dunbar1b077112009-11-21 09:06:10 +00002498}
Mike Stump738f8c22009-07-31 23:15:31 +00002499
Mike Stumpde050572009-12-02 18:57:08 +00002500void MangleContext::mangleCXXRTTI(QualType Ty,
Daniel Dunbar94fd26d2009-11-21 09:06:22 +00002501 llvm::SmallVectorImpl<char> &Res) {
Daniel Dunbarc0747712009-11-21 09:12:13 +00002502 // <special-name> ::= TI <type> # typeinfo structure
Douglas Gregor154fe982009-12-23 22:04:40 +00002503 assert(!Ty.hasQualifiers() && "RTTI info cannot have top-level qualifiers");
Daniel Dunbar94fd26d2009-11-21 09:06:22 +00002504 CXXNameMangler Mangler(*this, Res);
Daniel Dunbarc0747712009-11-21 09:12:13 +00002505 Mangler.getStream() << "_ZTI";
2506 Mangler.mangleType(Ty);
Daniel Dunbar1b077112009-11-21 09:06:10 +00002507}
Mike Stump67795982009-11-14 00:14:13 +00002508
Mike Stumpde050572009-12-02 18:57:08 +00002509void MangleContext::mangleCXXRTTIName(QualType Ty,
Daniel Dunbar94fd26d2009-11-21 09:06:22 +00002510 llvm::SmallVectorImpl<char> &Res) {
Daniel Dunbarc0747712009-11-21 09:12:13 +00002511 // <special-name> ::= TS <type> # typeinfo name (null terminated byte string)
Daniel Dunbar94fd26d2009-11-21 09:06:22 +00002512 CXXNameMangler Mangler(*this, Res);
Daniel Dunbarc0747712009-11-21 09:12:13 +00002513 Mangler.getStream() << "_ZTS";
2514 Mangler.mangleType(Ty);
Mike Stumpf1216772009-07-31 18:25:34 +00002515}