blob: 01838f98d450fb97da566e227b8ad19373cea3fa [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
43void MiscNameMangler::mangleBlock(const BlockDecl *BD) {
44 // 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();
58 else {
59 // FIXME: We were doing a mangleUnqualifiedName() before, but that's
60 // a private member of a class that will soon itself be private to the
61 // Itanium C++ ABI object. What should we do now? Right now, I'm just
62 // calling the mangleName() method on the MangleContext; is there a
63 // better way?
64 llvm::SmallString<64> Buffer;
65 Context.mangleName(ND, Buffer);
66 Out << Buffer;
67 }
68 }
69 Out << "_block_invoke_" << Context.getBlockId(BD, true);
70 } else {
71 Out << "__block_global_" << Context.getBlockId(BD, false);
72 }
73}
74
75void MiscNameMangler::mangleObjCMethodName(const ObjCMethodDecl *MD) {
76 llvm::SmallString<64> Name;
77 llvm::raw_svector_ostream OS(Name);
78
79 const ObjCContainerDecl *CD =
80 dyn_cast<ObjCContainerDecl>(MD->getDeclContext());
81 assert (CD && "Missing container decl in GetNameForMethod");
82 OS << (MD->isInstanceMethod() ? '-' : '+') << '[' << CD->getName();
83 if (const ObjCCategoryImplDecl *CID = dyn_cast<ObjCCategoryImplDecl>(CD))
84 OS << '(' << CID << ')';
85 OS << ' ' << MD->getSelector().getAsString() << ']';
86
87 Out << OS.str().size() << OS.str();
88}
89
Douglas Gregor5f2bfd42009-02-13 00:10:09 +000090namespace {
Fariborz Jahanian57058532010-03-03 19:41:08 +000091
92static const DeclContext *GetLocalClassFunctionDeclContext(
93 const DeclContext *DC) {
94 if (isa<CXXRecordDecl>(DC)) {
95 while (!DC->isNamespace() && !DC->isTranslationUnit() &&
96 !isa<FunctionDecl>(DC))
97 DC = DC->getParent();
98 if (isa<FunctionDecl>(DC))
99 return DC;
100 }
101 return 0;
102}
103
Anders Carlsson7e120032009-11-24 05:36:32 +0000104static const CXXMethodDecl *getStructor(const CXXMethodDecl *MD) {
105 assert((isa<CXXConstructorDecl>(MD) || isa<CXXDestructorDecl>(MD)) &&
106 "Passed in decl is not a ctor or dtor!");
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000107
Anders Carlsson7e120032009-11-24 05:36:32 +0000108 if (const TemplateDecl *TD = MD->getPrimaryTemplate()) {
109 MD = cast<CXXMethodDecl>(TD->getTemplatedDecl());
110
111 assert((isa<CXXConstructorDecl>(MD) || isa<CXXDestructorDecl>(MD)) &&
112 "Templated decl is not a ctor or dtor!");
113 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000114
Anders Carlsson7e120032009-11-24 05:36:32 +0000115 return MD;
116}
John McCall1dd73832010-02-04 01:42:13 +0000117
118static const unsigned UnknownArity = ~0U;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000119
Daniel Dunbar1b077112009-11-21 09:06:10 +0000120/// CXXNameMangler - Manage the mangling of a single name.
Daniel Dunbarc0747712009-11-21 09:12:13 +0000121class CXXNameMangler {
Daniel Dunbar1b077112009-11-21 09:06:10 +0000122 MangleContext &Context;
Daniel Dunbar94fd26d2009-11-21 09:06:22 +0000123 llvm::raw_svector_ostream Out;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000124
Daniel Dunbar1b077112009-11-21 09:06:10 +0000125 const CXXMethodDecl *Structor;
126 unsigned StructorType;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000127
Anders Carlsson9d85b722010-06-02 04:29:50 +0000128 /// SeqID - The next subsitution sequence number.
129 unsigned SeqID;
130
Daniel Dunbar1b077112009-11-21 09:06:10 +0000131 llvm::DenseMap<uintptr_t, unsigned> Substitutions;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000132
John McCall1dd73832010-02-04 01:42:13 +0000133 ASTContext &getASTContext() const { return Context.getASTContext(); }
134
Daniel Dunbarc0747712009-11-21 09:12:13 +0000135public:
Daniel Dunbar94fd26d2009-11-21 09:06:22 +0000136 CXXNameMangler(MangleContext &C, llvm::SmallVectorImpl<char> &Res)
Anders Carlsson9d85b722010-06-02 04:29:50 +0000137 : Context(C), Out(Res), Structor(0), StructorType(0), SeqID(0) { }
Daniel Dunbar77939c92009-11-21 09:06:31 +0000138 CXXNameMangler(MangleContext &C, llvm::SmallVectorImpl<char> &Res,
139 const CXXConstructorDecl *D, CXXCtorType Type)
Anders Carlsson9d85b722010-06-02 04:29:50 +0000140 : Context(C), Out(Res), Structor(getStructor(D)), StructorType(Type),
141 SeqID(0) { }
Daniel Dunbar77939c92009-11-21 09:06:31 +0000142 CXXNameMangler(MangleContext &C, llvm::SmallVectorImpl<char> &Res,
143 const CXXDestructorDecl *D, CXXDtorType Type)
Anders Carlsson9d85b722010-06-02 04:29:50 +0000144 : Context(C), Out(Res), Structor(getStructor(D)), StructorType(Type),
145 SeqID(0) { }
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000146
Anders Carlssonf98574b2010-02-05 07:31:37 +0000147#if MANGLE_CHECKER
148 ~CXXNameMangler() {
149 if (Out.str()[0] == '\01')
150 return;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000151
Anders Carlssonf98574b2010-02-05 07:31:37 +0000152 int status = 0;
153 char *result = abi::__cxa_demangle(Out.str().str().c_str(), 0, 0, &status);
154 assert(status == 0 && "Could not demangle mangled name!");
155 free(result);
156 }
157#endif
Daniel Dunbarc0747712009-11-21 09:12:13 +0000158 llvm::raw_svector_ostream &getStream() { return Out; }
159
Daniel Dunbar7e0c1952009-11-21 09:17:15 +0000160 void mangle(const NamedDecl *D, llvm::StringRef Prefix = "_Z");
Anders Carlsson19879c92010-03-23 17:17:29 +0000161 void mangleCallOffset(int64_t NonVirtual, int64_t Virtual);
Anders Carlssona94822e2009-11-26 02:32:05 +0000162 void mangleNumber(int64_t Number);
Daniel Dunbarc0747712009-11-21 09:12:13 +0000163 void mangleFunctionEncoding(const FunctionDecl *FD);
164 void mangleName(const NamedDecl *ND);
165 void mangleType(QualType T);
Douglas Gregor1b12a3b2010-05-26 05:11:13 +0000166 void mangleNameOrStandardSubstitution(const NamedDecl *ND);
167
Daniel Dunbarc0747712009-11-21 09:12:13 +0000168private:
Daniel Dunbar1b077112009-11-21 09:06:10 +0000169 bool mangleSubstitution(const NamedDecl *ND);
170 bool mangleSubstitution(QualType T);
Douglas Gregor1e9268e2010-04-28 05:58:56 +0000171 bool mangleSubstitution(TemplateName Template);
Daniel Dunbar1b077112009-11-21 09:06:10 +0000172 bool mangleSubstitution(uintptr_t Ptr);
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000173
Daniel Dunbar1b077112009-11-21 09:06:10 +0000174 bool mangleStandardSubstitution(const NamedDecl *ND);
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000175
Daniel Dunbar1b077112009-11-21 09:06:10 +0000176 void addSubstitution(const NamedDecl *ND) {
177 ND = cast<NamedDecl>(ND->getCanonicalDecl());
Anders Carlsson433d1372009-11-07 04:26:04 +0000178
Daniel Dunbar1b077112009-11-21 09:06:10 +0000179 addSubstitution(reinterpret_cast<uintptr_t>(ND));
180 }
181 void addSubstitution(QualType T);
Douglas Gregor1e9268e2010-04-28 05:58:56 +0000182 void addSubstitution(TemplateName Template);
Daniel Dunbar1b077112009-11-21 09:06:10 +0000183 void addSubstitution(uintptr_t Ptr);
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000184
John McCall1dd73832010-02-04 01:42:13 +0000185 void mangleUnresolvedScope(NestedNameSpecifier *Qualifier);
186 void mangleUnresolvedName(NestedNameSpecifier *Qualifier,
187 DeclarationName Name,
188 unsigned KnownArity = UnknownArity);
189
Daniel Dunbar1b077112009-11-21 09:06:10 +0000190 void mangleName(const TemplateDecl *TD,
191 const TemplateArgument *TemplateArgs,
192 unsigned NumTemplateArgs);
John McCall1dd73832010-02-04 01:42:13 +0000193 void mangleUnqualifiedName(const NamedDecl *ND) {
194 mangleUnqualifiedName(ND, ND->getDeclName(), UnknownArity);
195 }
196 void mangleUnqualifiedName(const NamedDecl *ND, DeclarationName Name,
197 unsigned KnownArity);
Daniel Dunbar1b077112009-11-21 09:06:10 +0000198 void mangleUnscopedName(const NamedDecl *ND);
199 void mangleUnscopedTemplateName(const TemplateDecl *ND);
Douglas Gregor1e9268e2010-04-28 05:58:56 +0000200 void mangleUnscopedTemplateName(TemplateName);
Daniel Dunbar1b077112009-11-21 09:06:10 +0000201 void mangleSourceName(const IdentifierInfo *II);
202 void mangleLocalName(const NamedDecl *ND);
Fariborz Jahanian57058532010-03-03 19:41:08 +0000203 void mangleNestedName(const NamedDecl *ND, const DeclContext *DC,
204 bool NoFunction=false);
Daniel Dunbar1b077112009-11-21 09:06:10 +0000205 void mangleNestedName(const TemplateDecl *TD,
206 const TemplateArgument *TemplateArgs,
207 unsigned NumTemplateArgs);
Fariborz Jahanian57058532010-03-03 19:41:08 +0000208 void manglePrefix(const DeclContext *DC, bool NoFunction=false);
Daniel Dunbar1b077112009-11-21 09:06:10 +0000209 void mangleTemplatePrefix(const TemplateDecl *ND);
Douglas Gregor20f0cc72010-04-23 03:10:43 +0000210 void mangleTemplatePrefix(TemplateName Template);
Daniel Dunbar1b077112009-11-21 09:06:10 +0000211 void mangleOperatorName(OverloadedOperatorKind OO, unsigned Arity);
212 void mangleQualifiers(Qualifiers Quals);
John McCallefe6aee2009-09-05 07:56:18 +0000213
Anders Carlsson7b06f6c2009-12-10 03:14:39 +0000214 void mangleObjCMethodName(const ObjCMethodDecl *MD);
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000215
Daniel Dunbar1b077112009-11-21 09:06:10 +0000216 // Declare manglers for every type class.
John McCallefe6aee2009-09-05 07:56:18 +0000217#define ABSTRACT_TYPE(CLASS, PARENT)
218#define NON_CANONICAL_TYPE(CLASS, PARENT)
219#define TYPE(CLASS, PARENT) void mangleType(const CLASS##Type *T);
220#include "clang/AST/TypeNodes.def"
221
Daniel Dunbar1b077112009-11-21 09:06:10 +0000222 void mangleType(const TagType*);
223 void mangleBareFunctionType(const FunctionType *T,
224 bool MangleReturnType);
Anders Carlssone170ba72009-12-14 01:45:37 +0000225
226 void mangleIntegerLiteral(QualType T, const llvm::APSInt &Value);
John McCall2f27bf82010-02-04 02:56:29 +0000227 void mangleMemberExpr(const Expr *Base, bool IsArrow,
228 NestedNameSpecifier *Qualifier,
229 DeclarationName Name,
230 unsigned KnownArity);
John McCall1dd73832010-02-04 01:42:13 +0000231 void mangleCalledExpression(const Expr *E, unsigned KnownArity);
Daniel Dunbar1b077112009-11-21 09:06:10 +0000232 void mangleExpression(const Expr *E);
233 void mangleCXXCtorType(CXXCtorType T);
234 void mangleCXXDtorType(CXXDtorType T);
Mike Stump1eb44332009-09-09 15:08:12 +0000235
Douglas Gregor20f0cc72010-04-23 03:10:43 +0000236 void mangleTemplateArgs(TemplateName Template,
237 const TemplateArgument *TemplateArgs,
Sean Huntc3021132010-05-05 15:23:54 +0000238 unsigned NumTemplateArgs);
Rafael Espindolad9800722010-03-11 14:07:00 +0000239 void mangleTemplateArgs(const TemplateParameterList &PL,
240 const TemplateArgument *TemplateArgs,
Daniel Dunbar1b077112009-11-21 09:06:10 +0000241 unsigned NumTemplateArgs);
Rafael Espindolad9800722010-03-11 14:07:00 +0000242 void mangleTemplateArgs(const TemplateParameterList &PL,
243 const TemplateArgumentList &AL);
244 void mangleTemplateArg(const NamedDecl *P, const TemplateArgument &A);
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000245
Daniel Dunbar1b077112009-11-21 09:06:10 +0000246 void mangleTemplateParameter(unsigned Index);
247};
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000248}
249
Anders Carlsson43f17402009-04-02 15:51:53 +0000250static bool isInCLinkageSpecification(const Decl *D) {
Douglas Gregor457e2812009-10-28 16:31:34 +0000251 D = D->getCanonicalDecl();
Mike Stump1eb44332009-09-09 15:08:12 +0000252 for (const DeclContext *DC = D->getDeclContext();
Anders Carlsson43f17402009-04-02 15:51:53 +0000253 !DC->isTranslationUnit(); DC = DC->getParent()) {
Mike Stump1eb44332009-09-09 15:08:12 +0000254 if (const LinkageSpecDecl *Linkage = dyn_cast<LinkageSpecDecl>(DC))
Anders Carlsson43f17402009-04-02 15:51:53 +0000255 return Linkage->getLanguage() == LinkageSpecDecl::lang_c;
256 }
Mike Stump1eb44332009-09-09 15:08:12 +0000257
Anders Carlsson43f17402009-04-02 15:51:53 +0000258 return false;
259}
260
Daniel Dunbarf981bf82009-11-21 09:14:52 +0000261bool MangleContext::shouldMangleDeclName(const NamedDecl *D) {
262 // In C, functions with no attributes never need to be mangled. Fastpath them.
263 if (!getASTContext().getLangOptions().CPlusPlus && !D->hasAttrs())
264 return false;
265
266 // Any decl can be declared with __asm("foo") on it, and this takes precedence
267 // over all other naming in the .o file.
268 if (D->hasAttr<AsmLabelAttr>())
269 return true;
270
Mike Stump141c5af2009-09-02 00:25:38 +0000271 // Clang's "overloadable" attribute extension to C/C++ implies name mangling
Anders Carlssona1e16222009-11-07 07:15:03 +0000272 // (always) as does passing a C++ member function and a function
273 // whose name is not a simple identifier.
Daniel Dunbarf981bf82009-11-21 09:14:52 +0000274 const FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
275 if (FD && (FD->hasAttr<OverloadableAttr>() || isa<CXXMethodDecl>(FD) ||
276 !FD->getDeclName().isIdentifier()))
277 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000278
Daniel Dunbarf981bf82009-11-21 09:14:52 +0000279 // Otherwise, no mangling is done outside C++ mode.
280 if (!getASTContext().getLangOptions().CPlusPlus)
281 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000282
Sean Hunt31455252010-01-24 03:04:27 +0000283 // Variables at global scope with non-internal linkage are not mangled
Eli Friedman7facf842009-12-02 20:32:49 +0000284 if (!FD) {
285 const DeclContext *DC = D->getDeclContext();
286 // Check for extern variable declared locally.
287 if (isa<FunctionDecl>(DC) && D->hasLinkage())
288 while (!DC->isNamespace() && !DC->isTranslationUnit())
289 DC = DC->getParent();
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000290 if (DC->isTranslationUnit() && D->getLinkage() != InternalLinkage)
Eli Friedman7facf842009-12-02 20:32:49 +0000291 return false;
292 }
293
294 // C functions and "main" are not mangled.
295 if ((FD && FD->isMain()) || isInCLinkageSpecification(D))
Daniel Dunbarf981bf82009-11-21 09:14:52 +0000296 return false;
297
Anders Carlsson43f17402009-04-02 15:51:53 +0000298 return true;
299}
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000300
Daniel Dunbar7e0c1952009-11-21 09:17:15 +0000301void CXXNameMangler::mangle(const NamedDecl *D, llvm::StringRef Prefix) {
Mike Stump141c5af2009-09-02 00:25:38 +0000302 // Any decl can be declared with __asm("foo") on it, and this takes precedence
303 // over all other naming in the .o file.
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000304 if (const AsmLabelAttr *ALA = D->getAttr<AsmLabelAttr>()) {
Chris Lattnerca3f25c2009-03-21 08:24:40 +0000305 // If we have an asm name, then we use it as the mangling.
306 Out << '\01'; // LLVM IR Marker for __asm("foo")
307 Out << ALA->getLabel();
Daniel Dunbarf981bf82009-11-21 09:14:52 +0000308 return;
Chris Lattnerca3f25c2009-03-21 08:24:40 +0000309 }
Mike Stump1eb44332009-09-09 15:08:12 +0000310
Sean Hunt31455252010-01-24 03:04:27 +0000311 // <mangled-name> ::= _Z <encoding>
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000312 // ::= <data name>
313 // ::= <special-name>
Daniel Dunbar7e0c1952009-11-21 09:17:15 +0000314 Out << Prefix;
315 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
Daniel Dunbarf981bf82009-11-21 09:14:52 +0000316 mangleFunctionEncoding(FD);
Rafael Espindolad9800722010-03-11 14:07:00 +0000317 else if (const VarDecl *VD = dyn_cast<VarDecl>(D))
318 mangleName(VD);
Daniel Dunbar7e0c1952009-11-21 09:17:15 +0000319 else
Rafael Espindolad9800722010-03-11 14:07:00 +0000320 mangleName(cast<FieldDecl>(D));
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000321}
322
323void CXXNameMangler::mangleFunctionEncoding(const FunctionDecl *FD) {
324 // <encoding> ::= <function name> <bare-function-type>
325 mangleName(FD);
Mike Stump1eb44332009-09-09 15:08:12 +0000326
Daniel Dunbar7e0c1952009-11-21 09:17:15 +0000327 // Don't mangle in the type if this isn't a decl we should typically mangle.
328 if (!Context.shouldMangleDeclName(FD))
329 return;
330
Mike Stump141c5af2009-09-02 00:25:38 +0000331 // Whether the mangling of a function type includes the return type depends on
332 // the context and the nature of the function. The rules for deciding whether
333 // the return type is included are:
Mike Stump1eb44332009-09-09 15:08:12 +0000334 //
Douglas Gregor1fd2dd12009-06-29 22:39:32 +0000335 // 1. Template functions (names or types) have return types encoded, with
336 // the exceptions listed below.
Mike Stump1eb44332009-09-09 15:08:12 +0000337 // 2. Function types not appearing as part of a function name mangling,
Douglas Gregor1fd2dd12009-06-29 22:39:32 +0000338 // e.g. parameters, pointer types, etc., have return type encoded, with the
339 // exceptions listed below.
340 // 3. Non-template function names do not have return types encoded.
341 //
Mike Stump141c5af2009-09-02 00:25:38 +0000342 // The exceptions mentioned in (1) and (2) above, for which the return type is
343 // never included, are
Douglas Gregor1fd2dd12009-06-29 22:39:32 +0000344 // 1. Constructors.
345 // 2. Destructors.
346 // 3. Conversion operator functions, e.g. operator int.
347 bool MangleReturnType = false;
Anders Carlsson9234b7f2009-09-17 03:46:43 +0000348 if (FunctionTemplateDecl *PrimaryTemplate = FD->getPrimaryTemplate()) {
349 if (!(isa<CXXConstructorDecl>(FD) || isa<CXXDestructorDecl>(FD) ||
350 isa<CXXConversionDecl>(FD)))
351 MangleReturnType = true;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000352
Anders Carlsson9234b7f2009-09-17 03:46:43 +0000353 // Mangle the type of the primary template.
354 FD = PrimaryTemplate->getTemplatedDecl();
355 }
356
John McCall54e14c42009-10-22 22:37:11 +0000357 // Do the canonicalization out here because parameter types can
358 // undergo additional canonicalization (e.g. array decay).
359 FunctionType *FT = cast<FunctionType>(Context.getASTContext()
360 .getCanonicalType(FD->getType()));
361
362 mangleBareFunctionType(FT, MangleReturnType);
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000363}
364
Anders Carlsson47846d22009-12-04 06:23:23 +0000365static const DeclContext *IgnoreLinkageSpecDecls(const DeclContext *DC) {
366 while (isa<LinkageSpecDecl>(DC)) {
Anders Carlsson47846d22009-12-04 06:23:23 +0000367 DC = DC->getParent();
368 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000369
Anders Carlsson47846d22009-12-04 06:23:23 +0000370 return DC;
371}
372
Anders Carlssonc820f902010-06-02 15:58:27 +0000373/// isStd - Return whether a given namespace is the 'std' namespace.
374static bool isStd(const NamespaceDecl *NS) {
375 if (!IgnoreLinkageSpecDecls(NS->getParent())->isTranslationUnit())
376 return false;
377
378 const IdentifierInfo *II = NS->getOriginalNamespace()->getIdentifier();
379 return II && II->isStr("std");
380}
381
Anders Carlsson47846d22009-12-04 06:23:23 +0000382// isStdNamespace - Return whether a given decl context is a toplevel 'std'
383// namespace.
Daniel Dunbar1308af92009-11-21 09:11:45 +0000384static bool isStdNamespace(const DeclContext *DC) {
Anders Carlsson47846d22009-12-04 06:23:23 +0000385 if (!DC->isNamespace())
386 return false;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000387
Anders Carlsson47846d22009-12-04 06:23:23 +0000388 return isStd(cast<NamespaceDecl>(DC));
Daniel Dunbar1308af92009-11-21 09:11:45 +0000389}
390
Anders Carlssonbb36ba42009-09-26 03:24:57 +0000391static const TemplateDecl *
392isTemplate(const NamedDecl *ND, const TemplateArgumentList *&TemplateArgs) {
Anders Carlsson2744a062009-09-18 19:00:18 +0000393 // Check if we have a function template.
394 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)){
Anders Carlssonbb36ba42009-09-26 03:24:57 +0000395 if (const TemplateDecl *TD = FD->getPrimaryTemplate()) {
Anders Carlsson2744a062009-09-18 19:00:18 +0000396 TemplateArgs = FD->getTemplateSpecializationArgs();
Anders Carlssonbb36ba42009-09-26 03:24:57 +0000397 return TD;
Anders Carlsson2744a062009-09-18 19:00:18 +0000398 }
399 }
400
Anders Carlssoneafc6dc2009-09-18 19:44:50 +0000401 // Check if we have a class template.
402 if (const ClassTemplateSpecializationDecl *Spec =
403 dyn_cast<ClassTemplateSpecializationDecl>(ND)) {
404 TemplateArgs = &Spec->getTemplateArgs();
Anders Carlssonbb36ba42009-09-26 03:24:57 +0000405 return Spec->getSpecializedTemplate();
Anders Carlssoneafc6dc2009-09-18 19:44:50 +0000406 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000407
Anders Carlsson2744a062009-09-18 19:00:18 +0000408 return 0;
409}
410
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000411void CXXNameMangler::mangleName(const NamedDecl *ND) {
412 // <name> ::= <nested-name>
413 // ::= <unscoped-name>
414 // ::= <unscoped-template-name> <template-args>
Anders Carlsson201ce742009-09-17 03:17:01 +0000415 // ::= <local-name>
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000416 //
Anders Carlssond58d6f72009-09-17 16:12:20 +0000417 const DeclContext *DC = ND->getDeclContext();
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000418
Fariborz Jahanian57058532010-03-03 19:41:08 +0000419 if (GetLocalClassFunctionDeclContext(DC)) {
420 mangleLocalName(ND);
421 return;
422 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000423
Eli Friedman7facf842009-12-02 20:32:49 +0000424 // If this is an extern variable declared locally, the relevant DeclContext
425 // is that of the containing namespace, or the translation unit.
426 if (isa<FunctionDecl>(DC) && ND->hasLinkage())
427 while (!DC->isNamespace() && !DC->isTranslationUnit())
428 DC = DC->getParent();
429
Anders Carlsson5cc58c62009-09-22 17:23:30 +0000430 while (isa<LinkageSpecDecl>(DC))
Anders Carlssond58d6f72009-09-17 16:12:20 +0000431 DC = DC->getParent();
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000432
Anders Carlssond58d6f72009-09-17 16:12:20 +0000433 if (DC->isTranslationUnit() || isStdNamespace(DC)) {
Anders Carlsson2744a062009-09-18 19:00:18 +0000434 // Check if we have a template.
435 const TemplateArgumentList *TemplateArgs = 0;
Anders Carlsson0fa6df42009-09-26 19:45:45 +0000436 if (const TemplateDecl *TD = isTemplate(ND, TemplateArgs)) {
Anders Carlsson2744a062009-09-18 19:00:18 +0000437 mangleUnscopedTemplateName(TD);
Rafael Espindolad9800722010-03-11 14:07:00 +0000438 TemplateParameterList *TemplateParameters = TD->getTemplateParameters();
439 mangleTemplateArgs(*TemplateParameters, *TemplateArgs);
Anders Carlsson2744a062009-09-18 19:00:18 +0000440 return;
Anders Carlsson7482e242009-09-18 04:29:09 +0000441 }
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000442
Anders Carlsson7482e242009-09-18 04:29:09 +0000443 mangleUnscopedName(ND);
444 return;
445 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000446
Anders Carlsson7b06f6c2009-12-10 03:14:39 +0000447 if (isa<FunctionDecl>(DC) || isa<ObjCMethodDecl>(DC)) {
Anders Carlsson7482e242009-09-18 04:29:09 +0000448 mangleLocalName(ND);
449 return;
450 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000451
Eli Friedman7facf842009-12-02 20:32:49 +0000452 mangleNestedName(ND, DC);
Anders Carlsson7482e242009-09-18 04:29:09 +0000453}
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000454void CXXNameMangler::mangleName(const TemplateDecl *TD,
Anders Carlsson7624f212009-09-18 02:42:01 +0000455 const TemplateArgument *TemplateArgs,
456 unsigned NumTemplateArgs) {
Anders Carlsson47846d22009-12-04 06:23:23 +0000457 const DeclContext *DC = IgnoreLinkageSpecDecls(TD->getDeclContext());
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000458
Anders Carlsson7624f212009-09-18 02:42:01 +0000459 if (DC->isTranslationUnit() || isStdNamespace(DC)) {
Anders Carlsson0fa6df42009-09-26 19:45:45 +0000460 mangleUnscopedTemplateName(TD);
Rafael Espindolad9800722010-03-11 14:07:00 +0000461 TemplateParameterList *TemplateParameters = TD->getTemplateParameters();
462 mangleTemplateArgs(*TemplateParameters, TemplateArgs, NumTemplateArgs);
Anders Carlsson7624f212009-09-18 02:42:01 +0000463 } else {
464 mangleNestedName(TD, TemplateArgs, NumTemplateArgs);
465 }
466}
467
Anders Carlsson201ce742009-09-17 03:17:01 +0000468void CXXNameMangler::mangleUnscopedName(const NamedDecl *ND) {
469 // <unscoped-name> ::= <unqualified-name>
470 // ::= St <unqualified-name> # ::std::
471 if (isStdNamespace(ND->getDeclContext()))
472 Out << "St";
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000473
Anders Carlsson201ce742009-09-17 03:17:01 +0000474 mangleUnqualifiedName(ND);
475}
476
Anders Carlsson0fa6df42009-09-26 19:45:45 +0000477void CXXNameMangler::mangleUnscopedTemplateName(const TemplateDecl *ND) {
Anders Carlsson201ce742009-09-17 03:17:01 +0000478 // <unscoped-template-name> ::= <unscoped-name>
479 // ::= <substitution>
Anders Carlsson7624f212009-09-18 02:42:01 +0000480 if (mangleSubstitution(ND))
Anders Carlsson03c9d532009-09-17 04:02:31 +0000481 return;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000482
Douglas Gregor32fb4e12010-02-05 20:45:00 +0000483 // <template-template-param> ::= <template-param>
484 if (const TemplateTemplateParmDecl *TTP
485 = dyn_cast<TemplateTemplateParmDecl>(ND)) {
486 mangleTemplateParameter(TTP->getIndex());
487 return;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000488 }
Douglas Gregor32fb4e12010-02-05 20:45:00 +0000489
Anders Carlsson1668f202009-09-26 20:13:56 +0000490 mangleUnscopedName(ND->getTemplatedDecl());
Anders Carlsson7624f212009-09-18 02:42:01 +0000491 addSubstitution(ND);
Anders Carlsson201ce742009-09-17 03:17:01 +0000492}
493
Douglas Gregor1e9268e2010-04-28 05:58:56 +0000494void CXXNameMangler::mangleUnscopedTemplateName(TemplateName Template) {
495 // <unscoped-template-name> ::= <unscoped-name>
496 // ::= <substitution>
497 if (TemplateDecl *TD = Template.getAsTemplateDecl())
498 return mangleUnscopedTemplateName(TD);
Sean Huntc3021132010-05-05 15:23:54 +0000499
Douglas Gregor1e9268e2010-04-28 05:58:56 +0000500 if (mangleSubstitution(Template))
501 return;
502
503 // FIXME: How to cope with operators here?
504 DependentTemplateName *Dependent = Template.getAsDependentTemplateName();
505 assert(Dependent && "Not a dependent template name?");
506 if (!Dependent->isIdentifier()) {
507 // FIXME: We can't possibly know the arity of the operator here!
508 Diagnostic &Diags = Context.getDiags();
509 unsigned DiagID = Diags.getCustomDiagID(Diagnostic::Error,
510 "cannot mangle dependent operator name");
511 Diags.Report(FullSourceLoc(), DiagID);
512 return;
513 }
Sean Huntc3021132010-05-05 15:23:54 +0000514
Douglas Gregor1e9268e2010-04-28 05:58:56 +0000515 mangleSourceName(Dependent->getIdentifier());
516 addSubstitution(Template);
517}
518
Anders Carlssona94822e2009-11-26 02:32:05 +0000519void CXXNameMangler::mangleNumber(int64_t Number) {
520 // <number> ::= [n] <non-negative decimal integer>
521 if (Number < 0) {
522 Out << 'n';
523 Number = -Number;
524 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000525
Anders Carlssona94822e2009-11-26 02:32:05 +0000526 Out << Number;
527}
528
Anders Carlsson19879c92010-03-23 17:17:29 +0000529void CXXNameMangler::mangleCallOffset(int64_t NonVirtual, int64_t Virtual) {
Mike Stump141c5af2009-09-02 00:25:38 +0000530 // <call-offset> ::= h <nv-offset> _
531 // ::= v <v-offset> _
532 // <nv-offset> ::= <offset number> # non-virtual base override
Anders Carlssona94822e2009-11-26 02:32:05 +0000533 // <v-offset> ::= <offset number> _ <virtual offset number>
Mike Stump141c5af2009-09-02 00:25:38 +0000534 // # virtual base override, with vcall offset
Anders Carlsson19879c92010-03-23 17:17:29 +0000535 if (!Virtual) {
Anders Carlssona94822e2009-11-26 02:32:05 +0000536 Out << 'h';
Anders Carlsson19879c92010-03-23 17:17:29 +0000537 mangleNumber(NonVirtual);
Anders Carlssona94822e2009-11-26 02:32:05 +0000538 Out << '_';
539 return;
Mike Stump141c5af2009-09-02 00:25:38 +0000540 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000541
Anders Carlssona94822e2009-11-26 02:32:05 +0000542 Out << 'v';
Anders Carlsson19879c92010-03-23 17:17:29 +0000543 mangleNumber(NonVirtual);
Anders Carlssona94822e2009-11-26 02:32:05 +0000544 Out << '_';
Anders Carlsson19879c92010-03-23 17:17:29 +0000545 mangleNumber(Virtual);
Anders Carlssona94822e2009-11-26 02:32:05 +0000546 Out << '_';
Mike Stump9124bcc2009-09-02 00:56:18 +0000547}
548
John McCall1dd73832010-02-04 01:42:13 +0000549void CXXNameMangler::mangleUnresolvedScope(NestedNameSpecifier *Qualifier) {
550 Qualifier = getASTContext().getCanonicalNestedNameSpecifier(Qualifier);
551 switch (Qualifier->getKind()) {
552 case NestedNameSpecifier::Global:
553 // nothing
554 break;
555 case NestedNameSpecifier::Namespace:
556 mangleName(Qualifier->getAsNamespace());
557 break;
558 case NestedNameSpecifier::TypeSpec:
Rafael Espindola9b35b252010-03-17 04:28:11 +0000559 case NestedNameSpecifier::TypeSpecWithTemplate: {
560 const Type *QTy = Qualifier->getAsType();
561
562 if (const TemplateSpecializationType *TST =
563 dyn_cast<TemplateSpecializationType>(QTy)) {
564 if (!mangleSubstitution(QualType(TST, 0))) {
Douglas Gregor20f0cc72010-04-23 03:10:43 +0000565 mangleTemplatePrefix(TST->getTemplateName());
Sean Huntc3021132010-05-05 15:23:54 +0000566
Douglas Gregor20f0cc72010-04-23 03:10:43 +0000567 // FIXME: GCC does not appear to mangle the template arguments when
568 // the template in question is a dependent template name. Should we
569 // emulate that badness?
570 mangleTemplateArgs(TST->getTemplateName(), TST->getArgs(),
Rafael Espindola9b35b252010-03-17 04:28:11 +0000571 TST->getNumArgs());
572 addSubstitution(QualType(TST, 0));
573 }
574 } else {
575 // We use the QualType mangle type variant here because it handles
576 // substitutions.
577 mangleType(QualType(QTy, 0));
578 }
579 }
John McCall1dd73832010-02-04 01:42:13 +0000580 break;
581 case NestedNameSpecifier::Identifier:
John McCallad5e7382010-03-01 23:49:17 +0000582 // Member expressions can have these without prefixes.
583 if (Qualifier->getPrefix())
584 mangleUnresolvedScope(Qualifier->getPrefix());
John McCall1dd73832010-02-04 01:42:13 +0000585 mangleSourceName(Qualifier->getAsIdentifier());
586 break;
587 }
588}
589
590/// Mangles a name which was not resolved to a specific entity.
591void CXXNameMangler::mangleUnresolvedName(NestedNameSpecifier *Qualifier,
592 DeclarationName Name,
593 unsigned KnownArity) {
594 if (Qualifier)
595 mangleUnresolvedScope(Qualifier);
596 // FIXME: ambiguity of unqualified lookup with ::
597
598 mangleUnqualifiedName(0, Name, KnownArity);
599}
600
Anders Carlsson6f7e2f42010-06-08 14:49:03 +0000601static const FieldDecl *FindFirstNamedDataMember(const RecordDecl *RD) {
602 assert(RD->isAnonymousStructOrUnion() &&
603 "Expected anonymous struct or union!");
604
605 for (RecordDecl::field_iterator I = RD->field_begin(), E = RD->field_end();
606 I != E; ++I) {
607 const FieldDecl *FD = *I;
608
609 if (FD->getIdentifier())
610 return FD;
611
612 if (const RecordType *RT = FD->getType()->getAs<RecordType>()) {
613 if (const FieldDecl *NamedDataMember =
614 FindFirstNamedDataMember(RT->getDecl()))
615 return NamedDataMember;
616 }
617 }
618
619 // We didn't find a named data member.
620 return 0;
621}
622
John McCall1dd73832010-02-04 01:42:13 +0000623void CXXNameMangler::mangleUnqualifiedName(const NamedDecl *ND,
624 DeclarationName Name,
625 unsigned KnownArity) {
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000626 // <unqualified-name> ::= <operator-name>
Mike Stump1eb44332009-09-09 15:08:12 +0000627 // ::= <ctor-dtor-name>
628 // ::= <source-name>
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000629 switch (Name.getNameKind()) {
Anders Carlssonc4355b62009-10-07 01:45:02 +0000630 case DeclarationName::Identifier: {
Anders Carlssonc4355b62009-10-07 01:45:02 +0000631 if (const IdentifierInfo *II = Name.getAsIdentifierInfo()) {
Sean Hunt31455252010-01-24 03:04:27 +0000632 // We must avoid conflicts between internally- and externally-
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000633 // linked variable declaration names in the same TU.
Anders Carlssonaec25232010-02-06 04:52:27 +0000634 // This naming convention is the same as that followed by GCC, though it
635 // shouldn't actually matter.
636 if (ND && isa<VarDecl>(ND) && ND->getLinkage() == InternalLinkage &&
Sean Hunt31455252010-01-24 03:04:27 +0000637 ND->getDeclContext()->isFileContext())
638 Out << 'L';
639
Anders Carlssonc4355b62009-10-07 01:45:02 +0000640 mangleSourceName(II);
641 break;
642 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000643
John McCall1dd73832010-02-04 01:42:13 +0000644 // Otherwise, an anonymous entity. We must have a declaration.
645 assert(ND && "mangling empty name without declaration");
646
647 if (const NamespaceDecl *NS = dyn_cast<NamespaceDecl>(ND)) {
648 if (NS->isAnonymousNamespace()) {
649 // This is how gcc mangles these names.
650 Out << "12_GLOBAL__N_1";
651 break;
652 }
653 }
654
Anders Carlsson6f7e2f42010-06-08 14:49:03 +0000655 if (const VarDecl *VD = dyn_cast<VarDecl>(ND)) {
656 // We must have an anonymous union or struct declaration.
657 const RecordDecl *RD =
658 cast<RecordDecl>(VD->getType()->getAs<RecordType>()->getDecl());
659
660 // Itanium C++ ABI 5.1.2:
661 //
662 // For the purposes of mangling, the name of an anonymous union is
663 // considered to be the name of the first named data member found by a
664 // pre-order, depth-first, declaration-order walk of the data members of
665 // the anonymous union. If there is no such data member (i.e., if all of
666 // the data members in the union are unnamed), then there is no way for
667 // a program to refer to the anonymous union, and there is therefore no
668 // need to mangle its name.
669 const FieldDecl *FD = FindFirstNamedDataMember(RD);
670 assert(FD && "Didn't find a named data member!");
671 assert(FD->getIdentifier() && "Data member name isn't an identifier!");
672
673 mangleSourceName(FD->getIdentifier());
674 break;
675 }
676
Anders Carlssonc4355b62009-10-07 01:45:02 +0000677 // We must have an anonymous struct.
678 const TagDecl *TD = cast<TagDecl>(ND);
679 if (const TypedefDecl *D = TD->getTypedefForAnonDecl()) {
680 assert(TD->getDeclContext() == D->getDeclContext() &&
681 "Typedef should not be in another decl context!");
682 assert(D->getDeclName().getAsIdentifierInfo() &&
683 "Typedef was not named!");
684 mangleSourceName(D->getDeclName().getAsIdentifierInfo());
685 break;
686 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000687
Anders Carlssonc4355b62009-10-07 01:45:02 +0000688 // Get a unique id for the anonymous struct.
689 uint64_t AnonStructId = Context.getAnonymousStructId(TD);
690
691 // Mangle it as a source name in the form
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000692 // [n] $_<id>
Anders Carlssonc4355b62009-10-07 01:45:02 +0000693 // where n is the length of the string.
694 llvm::SmallString<8> Str;
695 Str += "$_";
696 Str += llvm::utostr(AnonStructId);
697
698 Out << Str.size();
699 Out << Str.str();
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000700 break;
Anders Carlssonc4355b62009-10-07 01:45:02 +0000701 }
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000702
703 case DeclarationName::ObjCZeroArgSelector:
704 case DeclarationName::ObjCOneArgSelector:
705 case DeclarationName::ObjCMultiArgSelector:
706 assert(false && "Can't mangle Objective-C selector names here!");
707 break;
708
709 case DeclarationName::CXXConstructorName:
Anders Carlsson27ae5362009-04-17 01:58:57 +0000710 if (ND == Structor)
Mike Stump141c5af2009-09-02 00:25:38 +0000711 // If the named decl is the C++ constructor we're mangling, use the type
712 // we were given.
Anders Carlsson27ae5362009-04-17 01:58:57 +0000713 mangleCXXCtorType(static_cast<CXXCtorType>(StructorType));
Anders Carlsson3ac86b52009-04-15 05:36:58 +0000714 else
715 // Otherwise, use the complete constructor name. This is relevant if a
716 // class with a constructor is declared within a constructor.
717 mangleCXXCtorType(Ctor_Complete);
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000718 break;
719
720 case DeclarationName::CXXDestructorName:
Anders Carlsson27ae5362009-04-17 01:58:57 +0000721 if (ND == Structor)
Mike Stump141c5af2009-09-02 00:25:38 +0000722 // If the named decl is the C++ destructor we're mangling, use the type we
723 // were given.
Anders Carlsson27ae5362009-04-17 01:58:57 +0000724 mangleCXXDtorType(static_cast<CXXDtorType>(StructorType));
725 else
726 // Otherwise, use the complete destructor name. This is relevant if a
727 // class with a destructor is declared within a destructor.
728 mangleCXXDtorType(Dtor_Complete);
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000729 break;
730
731 case DeclarationName::CXXConversionFunctionName:
Mike Stump1eb44332009-09-09 15:08:12 +0000732 // <operator-name> ::= cv <type> # (cast)
Douglas Gregor219cc612009-02-13 01:28:03 +0000733 Out << "cv";
Anders Carlssonb5404912009-10-07 01:06:45 +0000734 mangleType(Context.getASTContext().getCanonicalType(Name.getCXXNameType()));
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000735 break;
736
Anders Carlsson8257d412009-12-22 06:36:32 +0000737 case DeclarationName::CXXOperatorName: {
John McCall1dd73832010-02-04 01:42:13 +0000738 unsigned Arity;
739 if (ND) {
740 Arity = cast<FunctionDecl>(ND)->getNumParams();
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000741
John McCall1dd73832010-02-04 01:42:13 +0000742 // If we have a C++ member function, we need to include the 'this' pointer.
743 // FIXME: This does not make sense for operators that are static, but their
744 // names stay the same regardless of the arity (operator new for instance).
745 if (isa<CXXMethodDecl>(ND))
746 Arity++;
747 } else
748 Arity = KnownArity;
749
Anders Carlsson8257d412009-12-22 06:36:32 +0000750 mangleOperatorName(Name.getCXXOverloadedOperator(), Arity);
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000751 break;
Anders Carlsson8257d412009-12-22 06:36:32 +0000752 }
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000753
Sean Hunt3e518bd2009-11-29 07:34:05 +0000754 case DeclarationName::CXXLiteralOperatorName:
Sean Hunt5dd6b392009-12-04 21:11:13 +0000755 // FIXME: This mangling is not yet official.
Sean Hunt2421f662009-12-04 21:01:37 +0000756 Out << "li";
Sean Hunt3e518bd2009-11-29 07:34:05 +0000757 mangleSourceName(Name.getCXXLiteralIdentifier());
758 break;
759
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000760 case DeclarationName::CXXUsingDirective:
761 assert(false && "Can't mangle a using directive name!");
Douglas Gregor219cc612009-02-13 01:28:03 +0000762 break;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000763 }
764}
765
766void CXXNameMangler::mangleSourceName(const IdentifierInfo *II) {
767 // <source-name> ::= <positive length number> <identifier>
768 // <number> ::= [n] <non-negative decimal integer>
769 // <identifier> ::= <unqualified source code identifier>
770 Out << II->getLength() << II->getName();
771}
772
Eli Friedman7facf842009-12-02 20:32:49 +0000773void CXXNameMangler::mangleNestedName(const NamedDecl *ND,
Fariborz Jahanian57058532010-03-03 19:41:08 +0000774 const DeclContext *DC,
775 bool NoFunction) {
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000776 // <nested-name> ::= N [<CV-qualifiers>] <prefix> <unqualified-name> E
777 // ::= N [<CV-qualifiers>] <template-prefix> <template-args> E
Anders Carlssond99edc42009-09-26 03:55:37 +0000778
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000779 Out << 'N';
780 if (const CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(ND))
John McCall0953e762009-09-24 19:53:00 +0000781 mangleQualifiers(Qualifiers::fromCVRMask(Method->getTypeQualifiers()));
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000782
Anders Carlsson2744a062009-09-18 19:00:18 +0000783 // Check if we have a template.
784 const TemplateArgumentList *TemplateArgs = 0;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000785 if (const TemplateDecl *TD = isTemplate(ND, TemplateArgs)) {
Anders Carlsson2744a062009-09-18 19:00:18 +0000786 mangleTemplatePrefix(TD);
Rafael Espindolad9800722010-03-11 14:07:00 +0000787 TemplateParameterList *TemplateParameters = TD->getTemplateParameters();
788 mangleTemplateArgs(*TemplateParameters, *TemplateArgs);
Fariborz Jahanian57058532010-03-03 19:41:08 +0000789 }
790 else {
791 manglePrefix(DC, NoFunction);
Anders Carlsson7482e242009-09-18 04:29:09 +0000792 mangleUnqualifiedName(ND);
793 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000794
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000795 Out << 'E';
796}
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000797void CXXNameMangler::mangleNestedName(const TemplateDecl *TD,
Anders Carlsson7624f212009-09-18 02:42:01 +0000798 const TemplateArgument *TemplateArgs,
799 unsigned NumTemplateArgs) {
Anders Carlssone45117b2009-09-27 19:53:49 +0000800 // <nested-name> ::= N [<CV-qualifiers>] <template-prefix> <template-args> E
801
Anders Carlsson7624f212009-09-18 02:42:01 +0000802 Out << 'N';
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000803
Anders Carlssone45117b2009-09-27 19:53:49 +0000804 mangleTemplatePrefix(TD);
Rafael Espindolad9800722010-03-11 14:07:00 +0000805 TemplateParameterList *TemplateParameters = TD->getTemplateParameters();
806 mangleTemplateArgs(*TemplateParameters, TemplateArgs, NumTemplateArgs);
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000807
Anders Carlsson7624f212009-09-18 02:42:01 +0000808 Out << 'E';
809}
810
Anders Carlsson1b42c792009-04-02 16:24:45 +0000811void CXXNameMangler::mangleLocalName(const NamedDecl *ND) {
812 // <local-name> := Z <function encoding> E <entity name> [<discriminator>]
813 // := Z <function encoding> E s [<discriminator>]
Mike Stump1eb44332009-09-09 15:08:12 +0000814 // <discriminator> := _ <non-negative number>
Fariborz Jahanian57058532010-03-03 19:41:08 +0000815 const DeclContext *DC = ND->getDeclContext();
Anders Carlsson1b42c792009-04-02 16:24:45 +0000816 Out << 'Z';
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000817
Charles Davis685b1d92010-05-26 18:25:27 +0000818 if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(DC)) {
819 mangleObjCMethodName(MD);
820 }
Fariborz Jahanian57058532010-03-03 19:41:08 +0000821 else if (const DeclContext *CDC = GetLocalClassFunctionDeclContext(DC)) {
822 mangleFunctionEncoding(cast<FunctionDecl>(CDC));
823 Out << 'E';
824 mangleNestedName(ND, DC, true /*NoFunction*/);
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000825
Fariborz Jahanian4819ac42010-03-04 01:02:03 +0000826 // FIXME. This still does not cover all cases.
827 unsigned disc;
828 if (Context.getNextDiscriminator(ND, disc)) {
829 if (disc < 10)
830 Out << '_' << disc;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000831 else
Fariborz Jahanian4819ac42010-03-04 01:02:03 +0000832 Out << "__" << disc << '_';
833 }
Fariborz Jahanian57058532010-03-03 19:41:08 +0000834
835 return;
836 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000837 else
Fariborz Jahanian57058532010-03-03 19:41:08 +0000838 mangleFunctionEncoding(cast<FunctionDecl>(DC));
Anders Carlsson7b06f6c2009-12-10 03:14:39 +0000839
Anders Carlsson1b42c792009-04-02 16:24:45 +0000840 Out << 'E';
Eli Friedman6f9f25d2009-12-11 20:21:38 +0000841 mangleUnqualifiedName(ND);
Anders Carlsson1b42c792009-04-02 16:24:45 +0000842}
843
Fariborz Jahanian57058532010-03-03 19:41:08 +0000844void CXXNameMangler::manglePrefix(const DeclContext *DC, bool NoFunction) {
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000845 // <prefix> ::= <prefix> <unqualified-name>
846 // ::= <template-prefix> <template-args>
847 // ::= <template-param>
848 // ::= # empty
849 // ::= <substitution>
Anders Carlsson6862fc72009-09-17 04:16:28 +0000850
Anders Carlssonadd28822009-09-22 20:33:31 +0000851 while (isa<LinkageSpecDecl>(DC))
852 DC = DC->getParent();
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000853
Anders Carlsson9263e912009-09-18 18:39:58 +0000854 if (DC->isTranslationUnit())
855 return;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000856
Douglas Gregor35415f52010-05-25 17:04:15 +0000857 if (const BlockDecl *Block = dyn_cast<BlockDecl>(DC)) {
858 manglePrefix(DC->getParent(), NoFunction);
859 llvm::SmallString<64> Name;
860 Context.mangleBlock(Block, Name);
861 Out << Name.size() << Name;
862 return;
863 }
864
Anders Carlsson6862fc72009-09-17 04:16:28 +0000865 if (mangleSubstitution(cast<NamedDecl>(DC)))
866 return;
Anders Carlsson7482e242009-09-18 04:29:09 +0000867
Anders Carlsson2ee3fca2009-09-18 20:11:09 +0000868 // Check if we have a template.
869 const TemplateArgumentList *TemplateArgs = 0;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000870 if (const TemplateDecl *TD = isTemplate(cast<NamedDecl>(DC), TemplateArgs)) {
Anders Carlsson2ee3fca2009-09-18 20:11:09 +0000871 mangleTemplatePrefix(TD);
Rafael Espindolad9800722010-03-11 14:07:00 +0000872 TemplateParameterList *TemplateParameters = TD->getTemplateParameters();
873 mangleTemplateArgs(*TemplateParameters, *TemplateArgs);
Fariborz Jahanian57058532010-03-03 19:41:08 +0000874 }
Douglas Gregor35415f52010-05-25 17:04:15 +0000875 else if(NoFunction && (isa<FunctionDecl>(DC) || isa<ObjCMethodDecl>(DC)))
Fariborz Jahanian57058532010-03-03 19:41:08 +0000876 return;
Douglas Gregor35415f52010-05-25 17:04:15 +0000877 else if (const ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(DC))
878 mangleObjCMethodName(Method);
Fariborz Jahanian57058532010-03-03 19:41:08 +0000879 else {
880 manglePrefix(DC->getParent(), NoFunction);
Anders Carlsson2ee3fca2009-09-18 20:11:09 +0000881 mangleUnqualifiedName(cast<NamedDecl>(DC));
882 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000883
Anders Carlsson6862fc72009-09-17 04:16:28 +0000884 addSubstitution(cast<NamedDecl>(DC));
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000885}
886
Douglas Gregor20f0cc72010-04-23 03:10:43 +0000887void CXXNameMangler::mangleTemplatePrefix(TemplateName Template) {
888 // <template-prefix> ::= <prefix> <template unqualified-name>
889 // ::= <template-param>
890 // ::= <substitution>
891 if (TemplateDecl *TD = Template.getAsTemplateDecl())
892 return mangleTemplatePrefix(TD);
893
894 if (QualifiedTemplateName *Qualified = Template.getAsQualifiedTemplateName())
895 mangleUnresolvedScope(Qualified->getQualifier());
Sean Huntc3021132010-05-05 15:23:54 +0000896
Douglas Gregor20f0cc72010-04-23 03:10:43 +0000897 if (OverloadedTemplateStorage *Overloaded
898 = Template.getAsOverloadedTemplate()) {
Sean Huntc3021132010-05-05 15:23:54 +0000899 mangleUnqualifiedName(0, (*Overloaded->begin())->getDeclName(),
Douglas Gregor20f0cc72010-04-23 03:10:43 +0000900 UnknownArity);
901 return;
902 }
Sean Huntc3021132010-05-05 15:23:54 +0000903
Douglas Gregor20f0cc72010-04-23 03:10:43 +0000904 DependentTemplateName *Dependent = Template.getAsDependentTemplateName();
905 assert(Dependent && "Unknown template name kind?");
906 mangleUnresolvedScope(Dependent->getQualifier());
Douglas Gregor1e9268e2010-04-28 05:58:56 +0000907 mangleUnscopedTemplateName(Template);
Douglas Gregor20f0cc72010-04-23 03:10:43 +0000908}
909
Anders Carlsson0fa6df42009-09-26 19:45:45 +0000910void CXXNameMangler::mangleTemplatePrefix(const TemplateDecl *ND) {
Anders Carlsson7482e242009-09-18 04:29:09 +0000911 // <template-prefix> ::= <prefix> <template unqualified-name>
912 // ::= <template-param>
913 // ::= <substitution>
Douglas Gregor32fb4e12010-02-05 20:45:00 +0000914 // <template-template-param> ::= <template-param>
915 // <substitution>
Anders Carlsson7482e242009-09-18 04:29:09 +0000916
Anders Carlssonaeb85372009-09-26 22:18:22 +0000917 if (mangleSubstitution(ND))
918 return;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000919
Douglas Gregor32fb4e12010-02-05 20:45:00 +0000920 // <template-template-param> ::= <template-param>
921 if (const TemplateTemplateParmDecl *TTP
922 = dyn_cast<TemplateTemplateParmDecl>(ND)) {
923 mangleTemplateParameter(TTP->getIndex());
924 return;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000925 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000926
Anders Carlssonaa73ab12009-09-18 18:47:07 +0000927 manglePrefix(ND->getDeclContext());
Anders Carlsson1668f202009-09-26 20:13:56 +0000928 mangleUnqualifiedName(ND->getTemplatedDecl());
Anders Carlssonaeb85372009-09-26 22:18:22 +0000929 addSubstitution(ND);
Anders Carlsson7482e242009-09-18 04:29:09 +0000930}
931
Mike Stump1eb44332009-09-09 15:08:12 +0000932void
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000933CXXNameMangler::mangleOperatorName(OverloadedOperatorKind OO, unsigned Arity) {
934 switch (OO) {
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000935 // <operator-name> ::= nw # new
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000936 case OO_New: Out << "nw"; break;
937 // ::= na # new[]
938 case OO_Array_New: Out << "na"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000939 // ::= dl # delete
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000940 case OO_Delete: Out << "dl"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000941 // ::= da # delete[]
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000942 case OO_Array_Delete: Out << "da"; break;
943 // ::= ps # + (unary)
944 // ::= pl # +
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000945 case OO_Plus:
Anders Carlsson8257d412009-12-22 06:36:32 +0000946 assert((Arity == 1 || Arity == 2) && "Invalid arity!");
947 Out << (Arity == 1? "ps" : "pl"); break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000948 // ::= ng # - (unary)
949 // ::= mi # -
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000950 case OO_Minus:
Anders Carlsson8257d412009-12-22 06:36:32 +0000951 assert((Arity == 1 || Arity == 2) && "Invalid arity!");
952 Out << (Arity == 1? "ng" : "mi"); break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000953 // ::= ad # & (unary)
954 // ::= an # &
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000955 case OO_Amp:
Anders Carlsson8257d412009-12-22 06:36:32 +0000956 assert((Arity == 1 || Arity == 2) && "Invalid arity!");
957 Out << (Arity == 1? "ad" : "an"); break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000958 // ::= de # * (unary)
959 // ::= ml # *
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000960 case OO_Star:
Anders Carlsson8257d412009-12-22 06:36:32 +0000961 assert((Arity == 1 || Arity == 2) && "Invalid arity!");
962 Out << (Arity == 1? "de" : "ml"); break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000963 // ::= co # ~
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000964 case OO_Tilde: Out << "co"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000965 // ::= dv # /
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000966 case OO_Slash: Out << "dv"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000967 // ::= rm # %
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000968 case OO_Percent: Out << "rm"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000969 // ::= or # |
970 case OO_Pipe: Out << "or"; break;
971 // ::= eo # ^
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000972 case OO_Caret: Out << "eo"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000973 // ::= aS # =
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000974 case OO_Equal: Out << "aS"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000975 // ::= pL # +=
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000976 case OO_PlusEqual: Out << "pL"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000977 // ::= mI # -=
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000978 case OO_MinusEqual: Out << "mI"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000979 // ::= mL # *=
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000980 case OO_StarEqual: Out << "mL"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000981 // ::= dV # /=
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000982 case OO_SlashEqual: Out << "dV"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000983 // ::= rM # %=
984 case OO_PercentEqual: Out << "rM"; break;
985 // ::= aN # &=
986 case OO_AmpEqual: Out << "aN"; break;
987 // ::= oR # |=
988 case OO_PipeEqual: Out << "oR"; break;
989 // ::= eO # ^=
990 case OO_CaretEqual: Out << "eO"; break;
991 // ::= ls # <<
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000992 case OO_LessLess: Out << "ls"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000993 // ::= rs # >>
994 case OO_GreaterGreater: Out << "rs"; break;
995 // ::= lS # <<=
996 case OO_LessLessEqual: Out << "lS"; break;
997 // ::= rS # >>=
998 case OO_GreaterGreaterEqual: Out << "rS"; break;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000999 // ::= eq # ==
1000 case OO_EqualEqual: Out << "eq"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001001 // ::= ne # !=
1002 case OO_ExclaimEqual: Out << "ne"; break;
1003 // ::= lt # <
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001004 case OO_Less: Out << "lt"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001005 // ::= gt # >
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001006 case OO_Greater: Out << "gt"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001007 // ::= le # <=
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001008 case OO_LessEqual: Out << "le"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001009 // ::= ge # >=
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001010 case OO_GreaterEqual: Out << "ge"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001011 // ::= nt # !
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001012 case OO_Exclaim: Out << "nt"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001013 // ::= aa # &&
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001014 case OO_AmpAmp: Out << "aa"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001015 // ::= oo # ||
1016 case OO_PipePipe: Out << "oo"; break;
1017 // ::= pp # ++
1018 case OO_PlusPlus: Out << "pp"; break;
1019 // ::= mm # --
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001020 case OO_MinusMinus: Out << "mm"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001021 // ::= cm # ,
1022 case OO_Comma: Out << "cm"; break;
1023 // ::= pm # ->*
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001024 case OO_ArrowStar: Out << "pm"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001025 // ::= pt # ->
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001026 case OO_Arrow: Out << "pt"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001027 // ::= cl # ()
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001028 case OO_Call: Out << "cl"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001029 // ::= ix # []
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001030 case OO_Subscript: Out << "ix"; break;
Anders Carlssone170ba72009-12-14 01:45:37 +00001031
1032 // ::= qu # ?
1033 // The conditional operator can't be overloaded, but we still handle it when
1034 // mangling expressions.
1035 case OO_Conditional: Out << "qu"; break;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001036
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001037 case OO_None:
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001038 case NUM_OVERLOADED_OPERATORS:
Mike Stump1eb44332009-09-09 15:08:12 +00001039 assert(false && "Not an overloaded operator");
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001040 break;
1041 }
1042}
1043
John McCall0953e762009-09-24 19:53:00 +00001044void CXXNameMangler::mangleQualifiers(Qualifiers Quals) {
Mike Stump1eb44332009-09-09 15:08:12 +00001045 // <CV-qualifiers> ::= [r] [V] [K] # restrict (C99), volatile, const
John McCall0953e762009-09-24 19:53:00 +00001046 if (Quals.hasRestrict())
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001047 Out << 'r';
John McCall0953e762009-09-24 19:53:00 +00001048 if (Quals.hasVolatile())
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001049 Out << 'V';
John McCall0953e762009-09-24 19:53:00 +00001050 if (Quals.hasConst())
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001051 Out << 'K';
John McCall0953e762009-09-24 19:53:00 +00001052
Douglas Gregor56079f72010-06-14 23:15:08 +00001053 if (Quals.hasAddressSpace()) {
1054 // Extension:
1055 //
1056 // <type> ::= U <address-space-number>
1057 //
1058 // where <address-space-number> is a source name consisting of 'AS'
1059 // followed by the address space <number>.
1060 llvm::SmallString<64> ASString;
1061 ASString = "AS" + llvm::utostr_32(Quals.getAddressSpace());
1062 Out << 'U' << ASString.size() << ASString;
1063 }
1064
John McCall0953e762009-09-24 19:53:00 +00001065 // FIXME: For now, just drop all extension qualifiers on the floor.
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001066}
1067
Anders Carlsson7b06f6c2009-12-10 03:14:39 +00001068void CXXNameMangler::mangleObjCMethodName(const ObjCMethodDecl *MD) {
Charles Davis685b1d92010-05-26 18:25:27 +00001069 llvm::SmallString<64> Buffer;
1070 MiscNameMangler(Context, Buffer).mangleObjCMethodName(MD);
1071 Out << Buffer;
Anders Carlsson7b06f6c2009-12-10 03:14:39 +00001072}
1073
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001074void CXXNameMangler::mangleType(QualType T) {
Anders Carlsson4843e582009-03-10 17:07:44 +00001075 // Only operate on the canonical type!
Anders Carlssonb5404912009-10-07 01:06:45 +00001076 T = Context.getASTContext().getCanonicalType(T);
Anders Carlsson4843e582009-03-10 17:07:44 +00001077
Douglas Gregora4923eb2009-11-16 21:35:15 +00001078 bool IsSubstitutable = T.hasLocalQualifiers() || !isa<BuiltinType>(T);
Anders Carlsson76967372009-09-17 00:43:46 +00001079 if (IsSubstitutable && mangleSubstitution(T))
1080 return;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001081
Douglas Gregora4923eb2009-11-16 21:35:15 +00001082 if (Qualifiers Quals = T.getLocalQualifiers()) {
John McCall0953e762009-09-24 19:53:00 +00001083 mangleQualifiers(Quals);
1084 // Recurse: even if the qualified type isn't yet substitutable,
1085 // the unqualified type might be.
Douglas Gregora4923eb2009-11-16 21:35:15 +00001086 mangleType(T.getLocalUnqualifiedType());
Anders Carlsson76967372009-09-17 00:43:46 +00001087 } else {
1088 switch (T->getTypeClass()) {
John McCallefe6aee2009-09-05 07:56:18 +00001089#define ABSTRACT_TYPE(CLASS, PARENT)
1090#define NON_CANONICAL_TYPE(CLASS, PARENT) \
Anders Carlsson76967372009-09-17 00:43:46 +00001091 case Type::CLASS: \
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +00001092 llvm_unreachable("can't mangle non-canonical type " #CLASS "Type"); \
Anders Carlsson76967372009-09-17 00:43:46 +00001093 return;
John McCallefe6aee2009-09-05 07:56:18 +00001094#define TYPE(CLASS, PARENT) \
Anders Carlsson76967372009-09-17 00:43:46 +00001095 case Type::CLASS: \
John McCall0953e762009-09-24 19:53:00 +00001096 mangleType(static_cast<const CLASS##Type*>(T.getTypePtr())); \
Anders Carlsson76967372009-09-17 00:43:46 +00001097 break;
John McCallefe6aee2009-09-05 07:56:18 +00001098#include "clang/AST/TypeNodes.def"
Anders Carlsson76967372009-09-17 00:43:46 +00001099 }
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001100 }
Anders Carlsson76967372009-09-17 00:43:46 +00001101
1102 // Add the substitution.
1103 if (IsSubstitutable)
1104 addSubstitution(T);
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001105}
1106
Douglas Gregor1b12a3b2010-05-26 05:11:13 +00001107void CXXNameMangler::mangleNameOrStandardSubstitution(const NamedDecl *ND) {
1108 if (!mangleStandardSubstitution(ND))
1109 mangleName(ND);
1110}
1111
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001112void CXXNameMangler::mangleType(const BuiltinType *T) {
John McCallefe6aee2009-09-05 07:56:18 +00001113 // <type> ::= <builtin-type>
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001114 // <builtin-type> ::= v # void
1115 // ::= w # wchar_t
1116 // ::= b # bool
1117 // ::= c # char
1118 // ::= a # signed char
1119 // ::= h # unsigned char
1120 // ::= s # short
1121 // ::= t # unsigned short
1122 // ::= i # int
1123 // ::= j # unsigned int
1124 // ::= l # long
1125 // ::= m # unsigned long
1126 // ::= x # long long, __int64
1127 // ::= y # unsigned long long, __int64
1128 // ::= n # __int128
1129 // UNSUPPORTED: ::= o # unsigned __int128
1130 // ::= f # float
1131 // ::= d # double
1132 // ::= e # long double, __float80
1133 // UNSUPPORTED: ::= g # __float128
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001134 // UNSUPPORTED: ::= Dd # IEEE 754r decimal floating point (64 bits)
1135 // UNSUPPORTED: ::= De # IEEE 754r decimal floating point (128 bits)
1136 // UNSUPPORTED: ::= Df # IEEE 754r decimal floating point (32 bits)
1137 // UNSUPPORTED: ::= Dh # IEEE 754r half-precision floating point (16 bits)
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001138 // ::= Di # char32_t
1139 // ::= Ds # char16_t
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001140 // ::= u <source-name> # vendor extended type
Sebastian Redl6e8ed162009-05-10 18:38:11 +00001141 // From our point of view, std::nullptr_t is a builtin, but as far as mangling
1142 // is concerned, it's a type called std::nullptr_t.
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001143 switch (T->getKind()) {
1144 case BuiltinType::Void: Out << 'v'; break;
1145 case BuiltinType::Bool: Out << 'b'; break;
1146 case BuiltinType::Char_U: case BuiltinType::Char_S: Out << 'c'; break;
1147 case BuiltinType::UChar: Out << 'h'; break;
1148 case BuiltinType::UShort: Out << 't'; break;
1149 case BuiltinType::UInt: Out << 'j'; break;
1150 case BuiltinType::ULong: Out << 'm'; break;
1151 case BuiltinType::ULongLong: Out << 'y'; break;
Chris Lattner2df9ced2009-04-30 02:43:43 +00001152 case BuiltinType::UInt128: Out << 'o'; break;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001153 case BuiltinType::SChar: Out << 'a'; break;
1154 case BuiltinType::WChar: Out << 'w'; break;
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001155 case BuiltinType::Char16: Out << "Ds"; break;
1156 case BuiltinType::Char32: Out << "Di"; break;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001157 case BuiltinType::Short: Out << 's'; break;
1158 case BuiltinType::Int: Out << 'i'; break;
1159 case BuiltinType::Long: Out << 'l'; break;
1160 case BuiltinType::LongLong: Out << 'x'; break;
Chris Lattner2df9ced2009-04-30 02:43:43 +00001161 case BuiltinType::Int128: Out << 'n'; break;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001162 case BuiltinType::Float: Out << 'f'; break;
1163 case BuiltinType::Double: Out << 'd'; break;
1164 case BuiltinType::LongDouble: Out << 'e'; break;
Sebastian Redl6e8ed162009-05-10 18:38:11 +00001165 case BuiltinType::NullPtr: Out << "St9nullptr_t"; break;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001166
1167 case BuiltinType::Overload:
1168 case BuiltinType::Dependent:
Mike Stump1eb44332009-09-09 15:08:12 +00001169 assert(false &&
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001170 "Overloaded and dependent types shouldn't get to name mangling");
1171 break;
Anders Carlssone89d1592009-06-26 18:41:36 +00001172 case BuiltinType::UndeducedAuto:
1173 assert(0 && "Should not see undeduced auto here");
1174 break;
Steve Naroff9533a7f2009-07-22 17:14:51 +00001175 case BuiltinType::ObjCId: Out << "11objc_object"; break;
1176 case BuiltinType::ObjCClass: Out << "10objc_class"; break;
Fariborz Jahanian13dcd002009-11-21 19:53:08 +00001177 case BuiltinType::ObjCSel: Out << "13objc_selector"; break;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001178 }
1179}
1180
John McCallefe6aee2009-09-05 07:56:18 +00001181// <type> ::= <function-type>
1182// <function-type> ::= F [Y] <bare-function-type> E
1183void CXXNameMangler::mangleType(const FunctionProtoType *T) {
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001184 Out << 'F';
Mike Stumpf5408fe2009-05-16 07:57:57 +00001185 // FIXME: We don't have enough information in the AST to produce the 'Y'
1186 // encoding for extern "C" function types.
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001187 mangleBareFunctionType(T, /*MangleReturnType=*/true);
1188 Out << 'E';
1189}
John McCallefe6aee2009-09-05 07:56:18 +00001190void CXXNameMangler::mangleType(const FunctionNoProtoType *T) {
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +00001191 llvm_unreachable("Can't mangle K&R function prototypes");
John McCallefe6aee2009-09-05 07:56:18 +00001192}
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001193void CXXNameMangler::mangleBareFunctionType(const FunctionType *T,
1194 bool MangleReturnType) {
John McCallefe6aee2009-09-05 07:56:18 +00001195 // We should never be mangling something without a prototype.
1196 const FunctionProtoType *Proto = cast<FunctionProtoType>(T);
1197
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001198 // <bare-function-type> ::= <signature type>+
1199 if (MangleReturnType)
John McCallefe6aee2009-09-05 07:56:18 +00001200 mangleType(Proto->getResultType());
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001201
Anders Carlsson93296682010-06-02 04:40:13 +00001202 if (Proto->getNumArgs() == 0 && !Proto->isVariadic()) {
1203 // <builtin-type> ::= v # void
Anders Carlssonc6c91bc2009-04-01 00:15:23 +00001204 Out << 'v';
1205 return;
1206 }
Mike Stump1eb44332009-09-09 15:08:12 +00001207
Douglas Gregor72564e72009-02-26 23:50:07 +00001208 for (FunctionProtoType::arg_type_iterator Arg = Proto->arg_type_begin(),
Mike Stump1eb44332009-09-09 15:08:12 +00001209 ArgEnd = Proto->arg_type_end();
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001210 Arg != ArgEnd; ++Arg)
1211 mangleType(*Arg);
Douglas Gregor219cc612009-02-13 01:28:03 +00001212
1213 // <builtin-type> ::= z # ellipsis
1214 if (Proto->isVariadic())
1215 Out << 'z';
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001216}
1217
John McCallefe6aee2009-09-05 07:56:18 +00001218// <type> ::= <class-enum-type>
Mike Stump1eb44332009-09-09 15:08:12 +00001219// <class-enum-type> ::= <name>
John McCalled976492009-12-04 22:46:56 +00001220void CXXNameMangler::mangleType(const UnresolvedUsingType *T) {
1221 mangleName(T->getDecl());
1222}
1223
1224// <type> ::= <class-enum-type>
1225// <class-enum-type> ::= <name>
John McCallefe6aee2009-09-05 07:56:18 +00001226void CXXNameMangler::mangleType(const EnumType *T) {
1227 mangleType(static_cast<const TagType*>(T));
1228}
1229void CXXNameMangler::mangleType(const RecordType *T) {
1230 mangleType(static_cast<const TagType*>(T));
1231}
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001232void CXXNameMangler::mangleType(const TagType *T) {
Eli Friedmanecb7e932009-12-11 18:00:57 +00001233 mangleName(T->getDecl());
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001234}
1235
John McCallefe6aee2009-09-05 07:56:18 +00001236// <type> ::= <array-type>
1237// <array-type> ::= A <positive dimension number> _ <element type>
1238// ::= A [<dimension expression>] _ <element type>
1239void CXXNameMangler::mangleType(const ConstantArrayType *T) {
1240 Out << 'A' << T->getSize() << '_';
1241 mangleType(T->getElementType());
1242}
1243void CXXNameMangler::mangleType(const VariableArrayType *T) {
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001244 Out << 'A';
John McCallefe6aee2009-09-05 07:56:18 +00001245 mangleExpression(T->getSizeExpr());
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001246 Out << '_';
1247 mangleType(T->getElementType());
1248}
John McCallefe6aee2009-09-05 07:56:18 +00001249void CXXNameMangler::mangleType(const DependentSizedArrayType *T) {
1250 Out << 'A';
1251 mangleExpression(T->getSizeExpr());
1252 Out << '_';
1253 mangleType(T->getElementType());
1254}
1255void CXXNameMangler::mangleType(const IncompleteArrayType *T) {
1256 Out << 'A' << '_';
1257 mangleType(T->getElementType());
1258}
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001259
John McCallefe6aee2009-09-05 07:56:18 +00001260// <type> ::= <pointer-to-member-type>
1261// <pointer-to-member-type> ::= M <class type> <member type>
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001262void CXXNameMangler::mangleType(const MemberPointerType *T) {
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001263 Out << 'M';
1264 mangleType(QualType(T->getClass(), 0));
Anders Carlsson0e650012009-05-17 17:41:20 +00001265 QualType PointeeType = T->getPointeeType();
1266 if (const FunctionProtoType *FPT = dyn_cast<FunctionProtoType>(PointeeType)) {
John McCall0953e762009-09-24 19:53:00 +00001267 mangleQualifiers(Qualifiers::fromCVRMask(FPT->getTypeQuals()));
Anders Carlsson0e650012009-05-17 17:41:20 +00001268 mangleType(FPT);
Anders Carlsson9d85b722010-06-02 04:29:50 +00001269
1270 // Itanium C++ ABI 5.1.8:
1271 //
1272 // The type of a non-static member function is considered to be different,
1273 // for the purposes of substitution, from the type of a namespace-scope or
1274 // static member function whose type appears similar. The types of two
1275 // non-static member functions are considered to be different, for the
1276 // purposes of substitution, if the functions are members of different
1277 // classes. In other words, for the purposes of substitution, the class of
1278 // which the function is a member is considered part of the type of
1279 // function.
1280
1281 // We increment the SeqID here to emulate adding an entry to the
1282 // substitution table. We can't actually add it because we don't want this
1283 // particular function type to be substituted.
1284 ++SeqID;
Mike Stump1eb44332009-09-09 15:08:12 +00001285 } else
Anders Carlsson0e650012009-05-17 17:41:20 +00001286 mangleType(PointeeType);
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001287}
1288
John McCallefe6aee2009-09-05 07:56:18 +00001289// <type> ::= <template-param>
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001290void CXXNameMangler::mangleType(const TemplateTypeParmType *T) {
Anders Carlsson0ccdf8d2009-09-27 00:38:53 +00001291 mangleTemplateParameter(T->getIndex());
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001292}
1293
John McCallefe6aee2009-09-05 07:56:18 +00001294// FIXME: <type> ::= <template-template-param> <template-args>
John McCallefe6aee2009-09-05 07:56:18 +00001295
1296// <type> ::= P <type> # pointer-to
1297void CXXNameMangler::mangleType(const PointerType *T) {
1298 Out << 'P';
1299 mangleType(T->getPointeeType());
1300}
1301void CXXNameMangler::mangleType(const ObjCObjectPointerType *T) {
1302 Out << 'P';
1303 mangleType(T->getPointeeType());
1304}
1305
1306// <type> ::= R <type> # reference-to
1307void CXXNameMangler::mangleType(const LValueReferenceType *T) {
1308 Out << 'R';
1309 mangleType(T->getPointeeType());
1310}
1311
1312// <type> ::= O <type> # rvalue reference-to (C++0x)
1313void CXXNameMangler::mangleType(const RValueReferenceType *T) {
1314 Out << 'O';
1315 mangleType(T->getPointeeType());
1316}
1317
1318// <type> ::= C <type> # complex pair (C 2000)
1319void CXXNameMangler::mangleType(const ComplexType *T) {
1320 Out << 'C';
1321 mangleType(T->getElementType());
1322}
1323
1324// GNU extension: vector types
Chris Lattner788b0fd2010-06-23 06:00:24 +00001325// <type> ::= <vector-type>
1326// <vector-type> ::= Dv <positive dimension number> _
1327// <extended element type>
1328// ::= Dv [<dimension expression>] _ <element type>
1329// <extended element type> ::= <element type>
1330// ::= p # AltiVec vector pixel
John McCallefe6aee2009-09-05 07:56:18 +00001331void CXXNameMangler::mangleType(const VectorType *T) {
Nick Lewycky0e5f0672010-03-26 07:18:04 +00001332 Out << "Dv" << T->getNumElements() << '_';
Chris Lattner788b0fd2010-06-23 06:00:24 +00001333 if (T->getAltiVecSpecific() == VectorType::Pixel)
1334 Out << 'p';
1335 else if (T->getAltiVecSpecific() == VectorType::Bool)
1336 Out << 'b';
1337 else
1338 mangleType(T->getElementType());
John McCallefe6aee2009-09-05 07:56:18 +00001339}
1340void CXXNameMangler::mangleType(const ExtVectorType *T) {
1341 mangleType(static_cast<const VectorType*>(T));
1342}
1343void CXXNameMangler::mangleType(const DependentSizedExtVectorType *T) {
Nick Lewycky0e5f0672010-03-26 07:18:04 +00001344 Out << "Dv";
1345 mangleExpression(T->getSizeExpr());
1346 Out << '_';
John McCallefe6aee2009-09-05 07:56:18 +00001347 mangleType(T->getElementType());
1348}
1349
Anders Carlssona40c5e42009-03-07 22:03:21 +00001350void CXXNameMangler::mangleType(const ObjCInterfaceType *T) {
1351 mangleSourceName(T->getDecl()->getIdentifier());
1352}
1353
John McCallc12c5bb2010-05-15 11:32:37 +00001354void CXXNameMangler::mangleType(const ObjCObjectType *T) {
John McCallc00c1f62010-05-15 17:06:29 +00001355 // We don't allow overloading by different protocol qualification,
1356 // so mangling them isn't necessary.
John McCallc12c5bb2010-05-15 11:32:37 +00001357 mangleType(T->getBaseType());
1358}
1359
John McCallefe6aee2009-09-05 07:56:18 +00001360void CXXNameMangler::mangleType(const BlockPointerType *T) {
Anders Carlssonf28c6872009-12-23 22:31:44 +00001361 Out << "U13block_pointer";
1362 mangleType(T->getPointeeType());
John McCallefe6aee2009-09-05 07:56:18 +00001363}
1364
John McCall31f17ec2010-04-27 00:57:59 +00001365void CXXNameMangler::mangleType(const InjectedClassNameType *T) {
1366 // Mangle injected class name types as if the user had written the
1367 // specialization out fully. It may not actually be possible to see
1368 // this mangling, though.
1369 mangleType(T->getInjectedSpecializationType());
1370}
1371
John McCallefe6aee2009-09-05 07:56:18 +00001372void CXXNameMangler::mangleType(const TemplateSpecializationType *T) {
Douglas Gregor1e9268e2010-04-28 05:58:56 +00001373 if (TemplateDecl *TD = T->getTemplateName().getAsTemplateDecl()) {
1374 mangleName(TD, T->getArgs(), T->getNumArgs());
1375 } else {
1376 if (mangleSubstitution(QualType(T, 0)))
1377 return;
Sean Huntc3021132010-05-05 15:23:54 +00001378
Douglas Gregor1e9268e2010-04-28 05:58:56 +00001379 mangleTemplatePrefix(T->getTemplateName());
Sean Huntc3021132010-05-05 15:23:54 +00001380
Douglas Gregor1e9268e2010-04-28 05:58:56 +00001381 // FIXME: GCC does not appear to mangle the template arguments when
1382 // the template in question is a dependent template name. Should we
1383 // emulate that badness?
1384 mangleTemplateArgs(T->getTemplateName(), T->getArgs(), T->getNumArgs());
1385 addSubstitution(QualType(T, 0));
1386 }
John McCallefe6aee2009-09-05 07:56:18 +00001387}
1388
Douglas Gregor4714c122010-03-31 17:34:00 +00001389void CXXNameMangler::mangleType(const DependentNameType *T) {
Anders Carlssonae352482009-09-26 02:26:02 +00001390 // Typename types are always nested
1391 Out << 'N';
John McCall33500952010-06-11 00:33:02 +00001392 mangleUnresolvedScope(T->getQualifier());
1393 mangleSourceName(T->getIdentifier());
1394 Out << 'E';
1395}
John McCall6ab30e02010-06-09 07:26:17 +00001396
John McCall33500952010-06-11 00:33:02 +00001397void CXXNameMangler::mangleType(const DependentTemplateSpecializationType *T) {
1398 // Dependently-scoped template types are always nested
1399 Out << 'N';
1400
1401 // TODO: avoid making this TemplateName.
1402 TemplateName Prefix =
1403 getASTContext().getDependentTemplateName(T->getQualifier(),
1404 T->getIdentifier());
1405 mangleTemplatePrefix(Prefix);
1406
1407 // FIXME: GCC does not appear to mangle the template arguments when
1408 // the template in question is a dependent template name. Should we
1409 // emulate that badness?
1410 mangleTemplateArgs(Prefix, T->getArgs(), T->getNumArgs());
Anders Carlssonae352482009-09-26 02:26:02 +00001411 Out << 'E';
John McCallefe6aee2009-09-05 07:56:18 +00001412}
1413
John McCallad5e7382010-03-01 23:49:17 +00001414void CXXNameMangler::mangleType(const TypeOfType *T) {
1415 // FIXME: this is pretty unsatisfactory, but there isn't an obvious
1416 // "extension with parameters" mangling.
1417 Out << "u6typeof";
1418}
1419
1420void CXXNameMangler::mangleType(const TypeOfExprType *T) {
1421 // FIXME: this is pretty unsatisfactory, but there isn't an obvious
1422 // "extension with parameters" mangling.
1423 Out << "u6typeof";
1424}
1425
1426void CXXNameMangler::mangleType(const DecltypeType *T) {
1427 Expr *E = T->getUnderlyingExpr();
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001428
John McCallad5e7382010-03-01 23:49:17 +00001429 // type ::= Dt <expression> E # decltype of an id-expression
1430 // # or class member access
1431 // ::= DT <expression> E # decltype of an expression
1432
1433 // This purports to be an exhaustive list of id-expressions and
1434 // class member accesses. Note that we do not ignore parentheses;
1435 // parentheses change the semantics of decltype for these
1436 // expressions (and cause the mangler to use the other form).
1437 if (isa<DeclRefExpr>(E) ||
1438 isa<MemberExpr>(E) ||
1439 isa<UnresolvedLookupExpr>(E) ||
1440 isa<DependentScopeDeclRefExpr>(E) ||
1441 isa<CXXDependentScopeMemberExpr>(E) ||
1442 isa<UnresolvedMemberExpr>(E))
1443 Out << "Dt";
1444 else
1445 Out << "DT";
1446 mangleExpression(E);
1447 Out << 'E';
1448}
1449
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001450void CXXNameMangler::mangleIntegerLiteral(QualType T,
Anders Carlssone170ba72009-12-14 01:45:37 +00001451 const llvm::APSInt &Value) {
1452 // <expr-primary> ::= L <type> <value number> E # integer literal
1453 Out << 'L';
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001454
Anders Carlssone170ba72009-12-14 01:45:37 +00001455 mangleType(T);
1456 if (T->isBooleanType()) {
1457 // Boolean values are encoded as 0/1.
1458 Out << (Value.getBoolValue() ? '1' : '0');
1459 } else {
Anders Carlssondfc0d1f2010-06-02 05:07:26 +00001460 if (Value.isSigned() && Value.isNegative()) {
Anders Carlssone170ba72009-12-14 01:45:37 +00001461 Out << 'n';
Anders Carlssondfc0d1f2010-06-02 05:07:26 +00001462 Value.abs().print(Out, true);
1463 } else
1464 Value.print(Out, Value.isSigned());
Anders Carlssone170ba72009-12-14 01:45:37 +00001465 }
1466 Out << 'E';
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001467
Anders Carlssone170ba72009-12-14 01:45:37 +00001468}
1469
John McCall1dd73832010-02-04 01:42:13 +00001470void CXXNameMangler::mangleCalledExpression(const Expr *E, unsigned Arity) {
1471 if (E->getType() != getASTContext().OverloadTy)
1472 mangleExpression(E);
John McCall2f27bf82010-02-04 02:56:29 +00001473 // propagate arity to dependent overloads?
John McCall1dd73832010-02-04 01:42:13 +00001474
1475 llvm::PointerIntPair<OverloadExpr*,1> R
1476 = OverloadExpr::find(const_cast<Expr*>(E));
1477 if (R.getInt())
1478 Out << "an"; // &
1479 const OverloadExpr *Ovl = R.getPointer();
John McCall2f27bf82010-02-04 02:56:29 +00001480 if (const UnresolvedMemberExpr *ME = dyn_cast<UnresolvedMemberExpr>(Ovl)) {
1481 mangleMemberExpr(ME->getBase(), ME->isArrow(), ME->getQualifier(),
1482 ME->getMemberName(), Arity);
1483 return;
1484 }
John McCall1dd73832010-02-04 01:42:13 +00001485
1486 mangleUnresolvedName(Ovl->getQualifier(), Ovl->getName(), Arity);
1487}
1488
John McCall2f27bf82010-02-04 02:56:29 +00001489/// Mangles a member expression. Implicit accesses are not handled,
1490/// but that should be okay, because you shouldn't be able to
1491/// make an implicit access in a function template declaration.
John McCall2f27bf82010-02-04 02:56:29 +00001492void CXXNameMangler::mangleMemberExpr(const Expr *Base,
1493 bool IsArrow,
1494 NestedNameSpecifier *Qualifier,
1495 DeclarationName Member,
1496 unsigned Arity) {
John McCalle1e342f2010-03-01 19:12:25 +00001497 // gcc-4.4 uses 'dt' for dot expressions, which is reasonable.
1498 // OTOH, gcc also mangles the name as an expression.
1499 Out << (IsArrow ? "pt" : "dt");
John McCall2f27bf82010-02-04 02:56:29 +00001500 mangleExpression(Base);
1501 mangleUnresolvedName(Qualifier, Member, Arity);
1502}
1503
Anders Carlssond553f8c2009-09-21 01:21:10 +00001504void CXXNameMangler::mangleExpression(const Expr *E) {
1505 // <expression> ::= <unary operator-name> <expression>
John McCall09cc1412010-02-03 00:55:45 +00001506 // ::= <binary operator-name> <expression> <expression>
1507 // ::= <trinary operator-name> <expression> <expression> <expression>
1508 // ::= cl <expression>* E # call
Anders Carlssond553f8c2009-09-21 01:21:10 +00001509 // ::= cv <type> expression # conversion with one argument
1510 // ::= cv <type> _ <expression>* E # conversion with a different number of arguments
John McCall09cc1412010-02-03 00:55:45 +00001511 // ::= st <type> # sizeof (a type)
Anders Carlssond553f8c2009-09-21 01:21:10 +00001512 // ::= at <type> # alignof (a type)
1513 // ::= <template-param>
1514 // ::= <function-param>
1515 // ::= sr <type> <unqualified-name> # dependent name
1516 // ::= sr <type> <unqualified-name> <template-args> # dependent template-id
1517 // ::= sZ <template-param> # size of a parameter pack
John McCall09cc1412010-02-03 00:55:45 +00001518 // ::= <expr-primary>
John McCall1dd73832010-02-04 01:42:13 +00001519 // <expr-primary> ::= L <type> <value number> E # integer literal
1520 // ::= L <type <value float> E # floating literal
1521 // ::= L <mangled-name> E # external name
Anders Carlssond553f8c2009-09-21 01:21:10 +00001522 switch (E->getStmtClass()) {
John McCall6ae1f352010-04-09 22:26:14 +00001523 case Expr::NoStmtClass:
1524#define EXPR(Type, Base)
1525#define STMT(Type, Base) \
1526 case Expr::Type##Class:
Sean Hunt4bfe1962010-05-05 15:24:00 +00001527#include "clang/AST/StmtNodes.inc"
John McCall09cc1412010-02-03 00:55:45 +00001528 llvm_unreachable("unexpected statement kind");
1529 break;
1530
John McCall6ae1f352010-04-09 22:26:14 +00001531 default: {
1532 // As bad as this diagnostic is, it's better than crashing.
1533 Diagnostic &Diags = Context.getDiags();
1534 unsigned DiagID = Diags.getCustomDiagID(Diagnostic::Error,
1535 "cannot yet mangle expression type %0");
John McCall739bf092010-04-10 09:39:25 +00001536 Diags.Report(FullSourceLoc(E->getExprLoc(),
1537 getASTContext().getSourceManager()),
1538 DiagID)
1539 << E->getStmtClassName() << E->getSourceRange();
John McCall6ae1f352010-04-09 22:26:14 +00001540 break;
1541 }
1542
John McCall1dd73832010-02-04 01:42:13 +00001543 case Expr::CallExprClass: {
1544 const CallExpr *CE = cast<CallExpr>(E);
1545 Out << "cl";
1546 mangleCalledExpression(CE->getCallee(), CE->getNumArgs());
1547 for (unsigned I = 0, N = CE->getNumArgs(); I != N; ++I)
1548 mangleExpression(CE->getArg(I));
Benjamin Kramer35f59b62010-04-10 16:03:31 +00001549 Out << 'E';
John McCall09cc1412010-02-03 00:55:45 +00001550 break;
John McCall1dd73832010-02-04 01:42:13 +00001551 }
John McCall09cc1412010-02-03 00:55:45 +00001552
John McCall2f27bf82010-02-04 02:56:29 +00001553 case Expr::MemberExprClass: {
1554 const MemberExpr *ME = cast<MemberExpr>(E);
1555 mangleMemberExpr(ME->getBase(), ME->isArrow(),
1556 ME->getQualifier(), ME->getMemberDecl()->getDeclName(),
1557 UnknownArity);
1558 break;
1559 }
1560
1561 case Expr::UnresolvedMemberExprClass: {
1562 const UnresolvedMemberExpr *ME = cast<UnresolvedMemberExpr>(E);
1563 mangleMemberExpr(ME->getBase(), ME->isArrow(),
1564 ME->getQualifier(), ME->getMemberName(),
1565 UnknownArity);
1566 break;
1567 }
1568
1569 case Expr::CXXDependentScopeMemberExprClass: {
1570 const CXXDependentScopeMemberExpr *ME
1571 = cast<CXXDependentScopeMemberExpr>(E);
1572 mangleMemberExpr(ME->getBase(), ME->isArrow(),
1573 ME->getQualifier(), ME->getMember(),
1574 UnknownArity);
1575 break;
1576 }
1577
John McCall1dd73832010-02-04 01:42:13 +00001578 case Expr::UnresolvedLookupExprClass: {
John McCalla3218e72010-02-04 01:48:38 +00001579 // The ABI doesn't cover how to mangle overload sets, so we mangle
1580 // using something as close as possible to the original lookup
1581 // expression.
John McCall1dd73832010-02-04 01:42:13 +00001582 const UnresolvedLookupExpr *ULE = cast<UnresolvedLookupExpr>(E);
1583 mangleUnresolvedName(ULE->getQualifier(), ULE->getName(), UnknownArity);
1584 break;
1585 }
1586
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001587 case Expr::CXXUnresolvedConstructExprClass: {
John McCall1dd73832010-02-04 01:42:13 +00001588 const CXXUnresolvedConstructExpr *CE = cast<CXXUnresolvedConstructExpr>(E);
1589 unsigned N = CE->arg_size();
1590
1591 Out << "cv";
1592 mangleType(CE->getType());
Benjamin Kramer35f59b62010-04-10 16:03:31 +00001593 if (N != 1) Out << '_';
John McCall1dd73832010-02-04 01:42:13 +00001594 for (unsigned I = 0; I != N; ++I) mangleExpression(CE->getArg(I));
Benjamin Kramer35f59b62010-04-10 16:03:31 +00001595 if (N != 1) Out << 'E';
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001596 break;
John McCall1dd73832010-02-04 01:42:13 +00001597 }
John McCall09cc1412010-02-03 00:55:45 +00001598
John McCall1dd73832010-02-04 01:42:13 +00001599 case Expr::CXXTemporaryObjectExprClass:
1600 case Expr::CXXConstructExprClass: {
1601 const CXXConstructExpr *CE = cast<CXXConstructExpr>(E);
1602 unsigned N = CE->getNumArgs();
1603
1604 Out << "cv";
1605 mangleType(CE->getType());
Benjamin Kramer35f59b62010-04-10 16:03:31 +00001606 if (N != 1) Out << '_';
John McCall1dd73832010-02-04 01:42:13 +00001607 for (unsigned I = 0; I != N; ++I) mangleExpression(CE->getArg(I));
Benjamin Kramer35f59b62010-04-10 16:03:31 +00001608 if (N != 1) Out << 'E';
John McCall09cc1412010-02-03 00:55:45 +00001609 break;
John McCall1dd73832010-02-04 01:42:13 +00001610 }
1611
1612 case Expr::SizeOfAlignOfExprClass: {
1613 const SizeOfAlignOfExpr *SAE = cast<SizeOfAlignOfExpr>(E);
Benjamin Kramer35f59b62010-04-10 16:03:31 +00001614 if (SAE->isSizeOf()) Out << 's';
1615 else Out << 'a';
John McCall1dd73832010-02-04 01:42:13 +00001616 if (SAE->isArgumentType()) {
Benjamin Kramer35f59b62010-04-10 16:03:31 +00001617 Out << 't';
John McCall1dd73832010-02-04 01:42:13 +00001618 mangleType(SAE->getArgumentType());
1619 } else {
Benjamin Kramer35f59b62010-04-10 16:03:31 +00001620 Out << 'z';
John McCall1dd73832010-02-04 01:42:13 +00001621 mangleExpression(SAE->getArgumentExpr());
1622 }
1623 break;
1624 }
Anders Carlssona7694082009-11-06 02:50:19 +00001625
Anders Carlssone170ba72009-12-14 01:45:37 +00001626 case Expr::UnaryOperatorClass: {
1627 const UnaryOperator *UO = cast<UnaryOperator>(E);
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001628 mangleOperatorName(UnaryOperator::getOverloadedOperator(UO->getOpcode()),
Anders Carlssone170ba72009-12-14 01:45:37 +00001629 /*Arity=*/1);
1630 mangleExpression(UO->getSubExpr());
1631 break;
1632 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001633
Anders Carlssone170ba72009-12-14 01:45:37 +00001634 case Expr::BinaryOperatorClass: {
1635 const BinaryOperator *BO = cast<BinaryOperator>(E);
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001636 mangleOperatorName(BinaryOperator::getOverloadedOperator(BO->getOpcode()),
Anders Carlssone170ba72009-12-14 01:45:37 +00001637 /*Arity=*/2);
1638 mangleExpression(BO->getLHS());
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001639 mangleExpression(BO->getRHS());
Anders Carlssone170ba72009-12-14 01:45:37 +00001640 break;
John McCall2f27bf82010-02-04 02:56:29 +00001641 }
Anders Carlssone170ba72009-12-14 01:45:37 +00001642
1643 case Expr::ConditionalOperatorClass: {
1644 const ConditionalOperator *CO = cast<ConditionalOperator>(E);
1645 mangleOperatorName(OO_Conditional, /*Arity=*/3);
1646 mangleExpression(CO->getCond());
1647 mangleExpression(CO->getLHS());
1648 mangleExpression(CO->getRHS());
1649 break;
1650 }
1651
Douglas Gregor46287c72010-01-29 16:37:09 +00001652 case Expr::ImplicitCastExprClass: {
1653 mangleExpression(cast<ImplicitCastExpr>(E)->getSubExpr());
1654 break;
1655 }
1656
1657 case Expr::CStyleCastExprClass:
1658 case Expr::CXXStaticCastExprClass:
1659 case Expr::CXXDynamicCastExprClass:
1660 case Expr::CXXReinterpretCastExprClass:
1661 case Expr::CXXConstCastExprClass:
1662 case Expr::CXXFunctionalCastExprClass: {
1663 const ExplicitCastExpr *ECE = cast<ExplicitCastExpr>(E);
1664 Out << "cv";
1665 mangleType(ECE->getType());
1666 mangleExpression(ECE->getSubExpr());
1667 break;
1668 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001669
Anders Carlsson58040a52009-12-16 05:48:46 +00001670 case Expr::CXXOperatorCallExprClass: {
1671 const CXXOperatorCallExpr *CE = cast<CXXOperatorCallExpr>(E);
1672 unsigned NumArgs = CE->getNumArgs();
1673 mangleOperatorName(CE->getOperator(), /*Arity=*/NumArgs);
1674 // Mangle the arguments.
1675 for (unsigned i = 0; i != NumArgs; ++i)
1676 mangleExpression(CE->getArg(i));
1677 break;
1678 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001679
Anders Carlssona7694082009-11-06 02:50:19 +00001680 case Expr::ParenExprClass:
1681 mangleExpression(cast<ParenExpr>(E)->getSubExpr());
1682 break;
1683
Anders Carlssond553f8c2009-09-21 01:21:10 +00001684 case Expr::DeclRefExprClass: {
Douglas Gregor5ed1bc32010-02-28 21:40:32 +00001685 const NamedDecl *D = cast<DeclRefExpr>(E)->getDecl();
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00001686
Anders Carlssond553f8c2009-09-21 01:21:10 +00001687 switch (D->getKind()) {
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001688 default:
Douglas Gregor5ed1bc32010-02-28 21:40:32 +00001689 // <expr-primary> ::= L <mangled-name> E # external name
1690 Out << 'L';
1691 mangle(D, "_Z");
1692 Out << 'E';
1693 break;
1694
Anders Carlssond553f8c2009-09-21 01:21:10 +00001695 case Decl::NonTypeTemplateParm: {
1696 const NonTypeTemplateParmDecl *PD = cast<NonTypeTemplateParmDecl>(D);
Anders Carlsson0ccdf8d2009-09-27 00:38:53 +00001697 mangleTemplateParameter(PD->getIndex());
Anders Carlssond553f8c2009-09-21 01:21:10 +00001698 break;
1699 }
1700
1701 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00001702
Anders Carlsson50755b02009-09-27 20:11:34 +00001703 break;
Anders Carlssond553f8c2009-09-21 01:21:10 +00001704 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00001705
John McCall865d4472009-11-19 22:55:06 +00001706 case Expr::DependentScopeDeclRefExprClass: {
1707 const DependentScopeDeclRefExpr *DRE = cast<DependentScopeDeclRefExpr>(E);
Douglas Gregor4b2ccfc2010-02-28 22:05:49 +00001708 NestedNameSpecifier *NNS = DRE->getQualifier();
1709 const Type *QTy = NNS->getAsType();
1710
1711 // When we're dealing with a nested-name-specifier that has just a
1712 // dependent identifier in it, mangle that as a typename. FIXME:
1713 // It isn't clear that we ever actually want to have such a
1714 // nested-name-specifier; why not just represent it as a typename type?
1715 if (!QTy && NNS->getAsIdentifier() && NNS->getPrefix()) {
Douglas Gregor4a2023f2010-03-31 20:19:30 +00001716 QTy = getASTContext().getDependentNameType(ETK_Typename,
1717 NNS->getPrefix(),
1718 NNS->getAsIdentifier())
Douglas Gregor4b2ccfc2010-02-28 22:05:49 +00001719 .getTypePtr();
1720 }
Anders Carlsson50755b02009-09-27 20:11:34 +00001721 assert(QTy && "Qualifier was not type!");
1722
1723 // ::= sr <type> <unqualified-name> # dependent name
1724 Out << "sr";
1725 mangleType(QualType(QTy, 0));
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00001726
Anders Carlsson50755b02009-09-27 20:11:34 +00001727 assert(DRE->getDeclName().getNameKind() == DeclarationName::Identifier &&
1728 "Unhandled decl name kind!");
1729 mangleSourceName(DRE->getDeclName().getAsIdentifierInfo());
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00001730
Anders Carlsson50755b02009-09-27 20:11:34 +00001731 break;
1732 }
1733
John McCalld9307602010-04-09 22:54:09 +00001734 case Expr::CXXBindReferenceExprClass:
1735 mangleExpression(cast<CXXBindReferenceExpr>(E)->getSubExpr());
1736 break;
1737
1738 case Expr::CXXBindTemporaryExprClass:
1739 mangleExpression(cast<CXXBindTemporaryExpr>(E)->getSubExpr());
1740 break;
1741
1742 case Expr::CXXExprWithTemporariesClass:
1743 mangleExpression(cast<CXXExprWithTemporaries>(E)->getSubExpr());
1744 break;
1745
John McCall1dd73832010-02-04 01:42:13 +00001746 case Expr::FloatingLiteralClass: {
1747 const FloatingLiteral *FL = cast<FloatingLiteral>(E);
Benjamin Kramer35f59b62010-04-10 16:03:31 +00001748 Out << 'L';
John McCall1dd73832010-02-04 01:42:13 +00001749 mangleType(FL->getType());
1750
1751 // TODO: avoid this copy with careful stream management.
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001752 llvm::SmallString<20> Buffer;
John McCall1dd73832010-02-04 01:42:13 +00001753 FL->getValue().bitcastToAPInt().toString(Buffer, 16, false);
1754 Out.write(Buffer.data(), Buffer.size());
1755
Benjamin Kramer35f59b62010-04-10 16:03:31 +00001756 Out << 'E';
John McCall1dd73832010-02-04 01:42:13 +00001757 break;
1758 }
1759
John McCallde810632010-04-09 21:48:08 +00001760 case Expr::CharacterLiteralClass:
Benjamin Kramer35f59b62010-04-10 16:03:31 +00001761 Out << 'L';
John McCallde810632010-04-09 21:48:08 +00001762 mangleType(E->getType());
1763 Out << cast<CharacterLiteral>(E)->getValue();
1764 Out << 'E';
1765 break;
1766
1767 case Expr::CXXBoolLiteralExprClass:
1768 Out << "Lb";
1769 Out << (cast<CXXBoolLiteralExpr>(E)->getValue() ? '1' : '0');
1770 Out << 'E';
1771 break;
1772
Anders Carlssone170ba72009-12-14 01:45:37 +00001773 case Expr::IntegerLiteralClass:
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001774 mangleIntegerLiteral(E->getType(),
Anders Carlssone170ba72009-12-14 01:45:37 +00001775 llvm::APSInt(cast<IntegerLiteral>(E)->getValue()));
1776 break;
1777
Anders Carlssond553f8c2009-09-21 01:21:10 +00001778 }
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001779}
1780
John McCallefe6aee2009-09-05 07:56:18 +00001781// FIXME: <type> ::= G <type> # imaginary (C 2000)
1782// FIXME: <type> ::= U <source-name> <type> # vendor extended type qualifier
1783
Anders Carlsson3ac86b52009-04-15 05:36:58 +00001784void CXXNameMangler::mangleCXXCtorType(CXXCtorType T) {
1785 // <ctor-dtor-name> ::= C1 # complete object constructor
1786 // ::= C2 # base object constructor
1787 // ::= C3 # complete object allocating constructor
1788 //
1789 switch (T) {
1790 case Ctor_Complete:
1791 Out << "C1";
1792 break;
1793 case Ctor_Base:
1794 Out << "C2";
1795 break;
1796 case Ctor_CompleteAllocating:
1797 Out << "C3";
1798 break;
1799 }
1800}
1801
Anders Carlsson27ae5362009-04-17 01:58:57 +00001802void CXXNameMangler::mangleCXXDtorType(CXXDtorType T) {
1803 // <ctor-dtor-name> ::= D0 # deleting destructor
1804 // ::= D1 # complete object destructor
1805 // ::= D2 # base object destructor
1806 //
1807 switch (T) {
1808 case Dtor_Deleting:
1809 Out << "D0";
1810 break;
1811 case Dtor_Complete:
1812 Out << "D1";
1813 break;
1814 case Dtor_Base:
1815 Out << "D2";
1816 break;
1817 }
1818}
1819
Douglas Gregor20f0cc72010-04-23 03:10:43 +00001820void CXXNameMangler::mangleTemplateArgs(TemplateName Template,
1821 const TemplateArgument *TemplateArgs,
1822 unsigned NumTemplateArgs) {
1823 if (TemplateDecl *TD = Template.getAsTemplateDecl())
1824 return mangleTemplateArgs(*TD->getTemplateParameters(), TemplateArgs,
1825 NumTemplateArgs);
Sean Huntc3021132010-05-05 15:23:54 +00001826
Douglas Gregor20f0cc72010-04-23 03:10:43 +00001827 // <template-args> ::= I <template-arg>+ E
1828 Out << 'I';
1829 for (unsigned i = 0; i != NumTemplateArgs; ++i)
1830 mangleTemplateArg(0, TemplateArgs[i]);
1831 Out << 'E';
1832}
1833
Rafael Espindolad9800722010-03-11 14:07:00 +00001834void CXXNameMangler::mangleTemplateArgs(const TemplateParameterList &PL,
1835 const TemplateArgumentList &AL) {
Anders Carlsson7a0ba872009-05-15 16:09:15 +00001836 // <template-args> ::= I <template-arg>+ E
Benjamin Kramer35f59b62010-04-10 16:03:31 +00001837 Out << 'I';
Rafael Espindolad9800722010-03-11 14:07:00 +00001838 for (unsigned i = 0, e = AL.size(); i != e; ++i)
1839 mangleTemplateArg(PL.getParam(i), AL[i]);
Benjamin Kramer35f59b62010-04-10 16:03:31 +00001840 Out << 'E';
Anders Carlsson7a0ba872009-05-15 16:09:15 +00001841}
1842
Rafael Espindolad9800722010-03-11 14:07:00 +00001843void CXXNameMangler::mangleTemplateArgs(const TemplateParameterList &PL,
1844 const TemplateArgument *TemplateArgs,
Anders Carlsson7624f212009-09-18 02:42:01 +00001845 unsigned NumTemplateArgs) {
1846 // <template-args> ::= I <template-arg>+ E
Benjamin Kramer35f59b62010-04-10 16:03:31 +00001847 Out << 'I';
Daniel Dunbar7e0c1952009-11-21 09:17:15 +00001848 for (unsigned i = 0; i != NumTemplateArgs; ++i)
Rafael Espindolad9800722010-03-11 14:07:00 +00001849 mangleTemplateArg(PL.getParam(i), TemplateArgs[i]);
Benjamin Kramer35f59b62010-04-10 16:03:31 +00001850 Out << 'E';
Anders Carlsson7624f212009-09-18 02:42:01 +00001851}
1852
Rafael Espindolad9800722010-03-11 14:07:00 +00001853void CXXNameMangler::mangleTemplateArg(const NamedDecl *P,
1854 const TemplateArgument &A) {
Mike Stump1eb44332009-09-09 15:08:12 +00001855 // <template-arg> ::= <type> # type or template
Anders Carlsson7a0ba872009-05-15 16:09:15 +00001856 // ::= X <expression> E # expression
1857 // ::= <expr-primary> # simple expressions
1858 // ::= I <template-arg>* E # argument pack
1859 // ::= sp <expression> # pack expansion of (C++0x)
1860 switch (A.getKind()) {
1861 default:
1862 assert(0 && "Unknown template argument kind!");
1863 case TemplateArgument::Type:
1864 mangleType(A.getAsType());
1865 break;
Anders Carlsson9e85c742009-12-23 19:30:55 +00001866 case TemplateArgument::Template:
Douglas Gregor2d565b32010-02-06 01:09:36 +00001867 assert(A.getAsTemplate().getAsTemplateDecl() &&
Douglas Gregor20f0cc72010-04-23 03:10:43 +00001868 "Can't get dependent template names here");
Anders Carlsson9e85c742009-12-23 19:30:55 +00001869 mangleName(A.getAsTemplate().getAsTemplateDecl());
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001870 break;
Anders Carlssond553f8c2009-09-21 01:21:10 +00001871 case TemplateArgument::Expression:
1872 Out << 'X';
1873 mangleExpression(A.getAsExpr());
1874 Out << 'E';
1875 break;
Anders Carlssone170ba72009-12-14 01:45:37 +00001876 case TemplateArgument::Integral:
1877 mangleIntegerLiteral(A.getIntegralType(), *A.getAsIntegral());
Anders Carlsson7a0ba872009-05-15 16:09:15 +00001878 break;
Daniel Dunbar7e0c1952009-11-21 09:17:15 +00001879 case TemplateArgument::Declaration: {
Douglas Gregor20f0cc72010-04-23 03:10:43 +00001880 assert(P && "Missing template parameter for declaration argument");
Daniel Dunbar7e0c1952009-11-21 09:17:15 +00001881 // <expr-primary> ::= L <mangled-name> E # external name
1882
Rafael Espindolad9800722010-03-11 14:07:00 +00001883 // Clang produces AST's where pointer-to-member-function expressions
Daniel Dunbar7e0c1952009-11-21 09:17:15 +00001884 // and pointer-to-function expressions are represented as a declaration not
Rafael Espindolad9800722010-03-11 14:07:00 +00001885 // an expression. We compensate for it here to produce the correct mangling.
1886 NamedDecl *D = cast<NamedDecl>(A.getAsDecl());
1887 const NonTypeTemplateParmDecl *Parameter = cast<NonTypeTemplateParmDecl>(P);
1888 bool compensateMangling = D->isCXXClassMember() &&
1889 !Parameter->getType()->isReferenceType();
1890 if (compensateMangling) {
1891 Out << 'X';
1892 mangleOperatorName(OO_Amp, 1);
1893 }
1894
Daniel Dunbar7e0c1952009-11-21 09:17:15 +00001895 Out << 'L';
1896 // References to external entities use the mangled name; if the name would
1897 // not normally be manged then mangle it as unqualified.
1898 //
1899 // FIXME: The ABI specifies that external names here should have _Z, but
1900 // gcc leaves this off.
Rafael Espindolad9800722010-03-11 14:07:00 +00001901 if (compensateMangling)
1902 mangle(D, "_Z");
1903 else
1904 mangle(D, "Z");
Daniel Dunbar7e0c1952009-11-21 09:17:15 +00001905 Out << 'E';
Rafael Espindolad9800722010-03-11 14:07:00 +00001906
1907 if (compensateMangling)
1908 Out << 'E';
1909
Daniel Dunbar7e0c1952009-11-21 09:17:15 +00001910 break;
1911 }
1912 }
Anders Carlsson7a0ba872009-05-15 16:09:15 +00001913}
1914
Anders Carlsson0ccdf8d2009-09-27 00:38:53 +00001915void CXXNameMangler::mangleTemplateParameter(unsigned Index) {
1916 // <template-param> ::= T_ # first template parameter
1917 // ::= T <parameter-2 non-negative number> _
1918 if (Index == 0)
1919 Out << "T_";
1920 else
1921 Out << 'T' << (Index - 1) << '_';
1922}
1923
Anders Carlsson76967372009-09-17 00:43:46 +00001924// <substitution> ::= S <seq-id> _
1925// ::= S_
Anders Carlsson6862fc72009-09-17 04:16:28 +00001926bool CXXNameMangler::mangleSubstitution(const NamedDecl *ND) {
Anders Carlssone7c8cb62009-09-26 20:53:44 +00001927 // Try one of the standard substitutions first.
1928 if (mangleStandardSubstitution(ND))
1929 return true;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00001930
Anders Carlsson433d1372009-11-07 04:26:04 +00001931 ND = cast<NamedDecl>(ND->getCanonicalDecl());
Anders Carlsson6862fc72009-09-17 04:16:28 +00001932 return mangleSubstitution(reinterpret_cast<uintptr_t>(ND));
1933}
1934
Anders Carlsson76967372009-09-17 00:43:46 +00001935bool CXXNameMangler::mangleSubstitution(QualType T) {
Anders Carlssond99edc42009-09-26 03:55:37 +00001936 if (!T.getCVRQualifiers()) {
1937 if (const RecordType *RT = T->getAs<RecordType>())
1938 return mangleSubstitution(RT->getDecl());
1939 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00001940
Anders Carlsson76967372009-09-17 00:43:46 +00001941 uintptr_t TypePtr = reinterpret_cast<uintptr_t>(T.getAsOpaquePtr());
1942
Anders Carlssond3a932a2009-09-17 03:53:28 +00001943 return mangleSubstitution(TypePtr);
1944}
1945
Douglas Gregor1e9268e2010-04-28 05:58:56 +00001946bool CXXNameMangler::mangleSubstitution(TemplateName Template) {
1947 if (TemplateDecl *TD = Template.getAsTemplateDecl())
1948 return mangleSubstitution(TD);
Sean Huntc3021132010-05-05 15:23:54 +00001949
Douglas Gregor1e9268e2010-04-28 05:58:56 +00001950 Template = Context.getASTContext().getCanonicalTemplateName(Template);
1951 return mangleSubstitution(
1952 reinterpret_cast<uintptr_t>(Template.getAsVoidPointer()));
1953}
1954
Anders Carlssond3a932a2009-09-17 03:53:28 +00001955bool CXXNameMangler::mangleSubstitution(uintptr_t Ptr) {
Benjamin Kramer35f59b62010-04-10 16:03:31 +00001956 llvm::DenseMap<uintptr_t, unsigned>::iterator I = Substitutions.find(Ptr);
Anders Carlsson76967372009-09-17 00:43:46 +00001957 if (I == Substitutions.end())
1958 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00001959
Anders Carlsson76967372009-09-17 00:43:46 +00001960 unsigned SeqID = I->second;
1961 if (SeqID == 0)
1962 Out << "S_";
1963 else {
1964 SeqID--;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00001965
Anders Carlsson76967372009-09-17 00:43:46 +00001966 // <seq-id> is encoded in base-36, using digits and upper case letters.
1967 char Buffer[10];
Benjamin Kramer35f59b62010-04-10 16:03:31 +00001968 char *BufferPtr = llvm::array_endof(Buffer);
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00001969
Anders Carlsson76967372009-09-17 00:43:46 +00001970 if (SeqID == 0) *--BufferPtr = '0';
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00001971
Anders Carlsson76967372009-09-17 00:43:46 +00001972 while (SeqID) {
1973 assert(BufferPtr > Buffer && "Buffer overflow!");
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00001974
John McCall6ab30e02010-06-09 07:26:17 +00001975 char c = static_cast<char>(SeqID % 36);
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00001976
Anders Carlsson76967372009-09-17 00:43:46 +00001977 *--BufferPtr = (c < 10 ? '0' + c : 'A' + c - 10);
1978 SeqID /= 36;
1979 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00001980
Benjamin Kramer35f59b62010-04-10 16:03:31 +00001981 Out << 'S'
1982 << llvm::StringRef(BufferPtr, llvm::array_endof(Buffer)-BufferPtr)
1983 << '_';
Anders Carlsson76967372009-09-17 00:43:46 +00001984 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00001985
Anders Carlsson76967372009-09-17 00:43:46 +00001986 return true;
1987}
1988
Anders Carlssonf514b542009-09-27 00:12:57 +00001989static bool isCharType(QualType T) {
1990 if (T.isNull())
1991 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00001992
Anders Carlssonf514b542009-09-27 00:12:57 +00001993 return T->isSpecificBuiltinType(BuiltinType::Char_S) ||
1994 T->isSpecificBuiltinType(BuiltinType::Char_U);
1995}
1996
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00001997/// isCharSpecialization - Returns whether a given type is a template
Anders Carlssonf514b542009-09-27 00:12:57 +00001998/// specialization of a given name with a single argument of type char.
1999static bool isCharSpecialization(QualType T, const char *Name) {
2000 if (T.isNull())
2001 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002002
Anders Carlssonf514b542009-09-27 00:12:57 +00002003 const RecordType *RT = T->getAs<RecordType>();
2004 if (!RT)
2005 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002006
2007 const ClassTemplateSpecializationDecl *SD =
Anders Carlssonf514b542009-09-27 00:12:57 +00002008 dyn_cast<ClassTemplateSpecializationDecl>(RT->getDecl());
2009 if (!SD)
2010 return false;
2011
2012 if (!isStdNamespace(SD->getDeclContext()))
2013 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002014
Anders Carlssonf514b542009-09-27 00:12:57 +00002015 const TemplateArgumentList &TemplateArgs = SD->getTemplateArgs();
2016 if (TemplateArgs.size() != 1)
2017 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002018
Anders Carlssonf514b542009-09-27 00:12:57 +00002019 if (!isCharType(TemplateArgs[0].getAsType()))
2020 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002021
Daniel Dunbar01eb9b92009-10-18 21:17:35 +00002022 return SD->getIdentifier()->getName() == Name;
Anders Carlssonf514b542009-09-27 00:12:57 +00002023}
2024
Anders Carlsson91f88602009-12-07 19:56:42 +00002025template <std::size_t StrLen>
2026bool isStreamCharSpecialization(const ClassTemplateSpecializationDecl *SD,
2027 const char (&Str)[StrLen]) {
2028 if (!SD->getIdentifier()->isStr(Str))
2029 return false;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002030
Anders Carlsson91f88602009-12-07 19:56:42 +00002031 const TemplateArgumentList &TemplateArgs = SD->getTemplateArgs();
2032 if (TemplateArgs.size() != 2)
2033 return false;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002034
Anders Carlsson91f88602009-12-07 19:56:42 +00002035 if (!isCharType(TemplateArgs[0].getAsType()))
2036 return false;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002037
Anders Carlsson91f88602009-12-07 19:56:42 +00002038 if (!isCharSpecialization(TemplateArgs[1].getAsType(), "char_traits"))
2039 return false;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002040
Anders Carlsson91f88602009-12-07 19:56:42 +00002041 return true;
2042}
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002043
Anders Carlssone7c8cb62009-09-26 20:53:44 +00002044bool CXXNameMangler::mangleStandardSubstitution(const NamedDecl *ND) {
2045 // <substitution> ::= St # ::std::
Anders Carlsson8c031552009-09-26 23:10:05 +00002046 if (const NamespaceDecl *NS = dyn_cast<NamespaceDecl>(ND)) {
Anders Carlsson47846d22009-12-04 06:23:23 +00002047 if (isStd(NS)) {
Anders Carlsson8c031552009-09-26 23:10:05 +00002048 Out << "St";
2049 return true;
2050 }
2051 }
2052
2053 if (const ClassTemplateDecl *TD = dyn_cast<ClassTemplateDecl>(ND)) {
2054 if (!isStdNamespace(TD->getDeclContext()))
2055 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002056
Anders Carlsson8c031552009-09-26 23:10:05 +00002057 // <substitution> ::= Sa # ::std::allocator
2058 if (TD->getIdentifier()->isStr("allocator")) {
2059 Out << "Sa";
2060 return true;
2061 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002062
Anders Carlsson189d59c2009-09-26 23:14:39 +00002063 // <<substitution> ::= Sb # ::std::basic_string
2064 if (TD->getIdentifier()->isStr("basic_string")) {
2065 Out << "Sb";
2066 return true;
2067 }
Anders Carlsson8c031552009-09-26 23:10:05 +00002068 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002069
2070 if (const ClassTemplateSpecializationDecl *SD =
Anders Carlssonf514b542009-09-27 00:12:57 +00002071 dyn_cast<ClassTemplateSpecializationDecl>(ND)) {
Eli Friedman5370ee22010-02-23 18:25:09 +00002072 if (!isStdNamespace(SD->getDeclContext()))
2073 return false;
2074
Anders Carlssonf514b542009-09-27 00:12:57 +00002075 // <substitution> ::= Ss # ::std::basic_string<char,
2076 // ::std::char_traits<char>,
2077 // ::std::allocator<char> >
2078 if (SD->getIdentifier()->isStr("basic_string")) {
2079 const TemplateArgumentList &TemplateArgs = SD->getTemplateArgs();
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002080
Anders Carlssonf514b542009-09-27 00:12:57 +00002081 if (TemplateArgs.size() != 3)
2082 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002083
Anders Carlssonf514b542009-09-27 00:12:57 +00002084 if (!isCharType(TemplateArgs[0].getAsType()))
2085 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002086
Anders Carlssonf514b542009-09-27 00:12:57 +00002087 if (!isCharSpecialization(TemplateArgs[1].getAsType(), "char_traits"))
2088 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002089
Anders Carlssonf514b542009-09-27 00:12:57 +00002090 if (!isCharSpecialization(TemplateArgs[2].getAsType(), "allocator"))
2091 return false;
2092
2093 Out << "Ss";
2094 return true;
2095 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002096
Anders Carlsson91f88602009-12-07 19:56:42 +00002097 // <substitution> ::= Si # ::std::basic_istream<char,
2098 // ::std::char_traits<char> >
2099 if (isStreamCharSpecialization(SD, "basic_istream")) {
2100 Out << "Si";
2101 return true;
2102 }
2103
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002104 // <substitution> ::= So # ::std::basic_ostream<char,
Anders Carlsson8f8fd8e2009-10-08 17:20:26 +00002105 // ::std::char_traits<char> >
Anders Carlsson91f88602009-12-07 19:56:42 +00002106 if (isStreamCharSpecialization(SD, "basic_ostream")) {
Anders Carlsson8f8fd8e2009-10-08 17:20:26 +00002107 Out << "So";
2108 return true;
2109 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002110
Anders Carlsson91f88602009-12-07 19:56:42 +00002111 // <substitution> ::= Sd # ::std::basic_iostream<char,
2112 // ::std::char_traits<char> >
2113 if (isStreamCharSpecialization(SD, "basic_iostream")) {
2114 Out << "Sd";
2115 return true;
2116 }
Anders Carlssonf514b542009-09-27 00:12:57 +00002117 }
Anders Carlsson8c031552009-09-26 23:10:05 +00002118 return false;
Anders Carlssone7c8cb62009-09-26 20:53:44 +00002119}
2120
Anders Carlsson76967372009-09-17 00:43:46 +00002121void CXXNameMangler::addSubstitution(QualType T) {
Anders Carlssond99edc42009-09-26 03:55:37 +00002122 if (!T.getCVRQualifiers()) {
2123 if (const RecordType *RT = T->getAs<RecordType>()) {
2124 addSubstitution(RT->getDecl());
2125 return;
2126 }
2127 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002128
Anders Carlsson76967372009-09-17 00:43:46 +00002129 uintptr_t TypePtr = reinterpret_cast<uintptr_t>(T.getAsOpaquePtr());
Anders Carlssond3a932a2009-09-17 03:53:28 +00002130 addSubstitution(TypePtr);
2131}
2132
Douglas Gregor1e9268e2010-04-28 05:58:56 +00002133void CXXNameMangler::addSubstitution(TemplateName Template) {
2134 if (TemplateDecl *TD = Template.getAsTemplateDecl())
2135 return addSubstitution(TD);
Sean Huntc3021132010-05-05 15:23:54 +00002136
Douglas Gregor1e9268e2010-04-28 05:58:56 +00002137 Template = Context.getASTContext().getCanonicalTemplateName(Template);
2138 addSubstitution(reinterpret_cast<uintptr_t>(Template.getAsVoidPointer()));
2139}
2140
Anders Carlssond3a932a2009-09-17 03:53:28 +00002141void CXXNameMangler::addSubstitution(uintptr_t Ptr) {
Anders Carlssond3a932a2009-09-17 03:53:28 +00002142 assert(!Substitutions.count(Ptr) && "Substitution already exists!");
Anders Carlsson9d85b722010-06-02 04:29:50 +00002143 Substitutions[Ptr] = SeqID++;
Anders Carlsson76967372009-09-17 00:43:46 +00002144}
2145
Daniel Dunbar1b077112009-11-21 09:06:10 +00002146//
Mike Stump1eb44332009-09-09 15:08:12 +00002147
Daniel Dunbar1b077112009-11-21 09:06:10 +00002148/// \brief Mangles the name of the declaration D and emits that name to the
2149/// given output stream.
2150///
2151/// If the declaration D requires a mangled name, this routine will emit that
2152/// mangled name to \p os and return true. Otherwise, \p os will be unchanged
2153/// and this routine will return false. In this case, the caller should just
2154/// emit the identifier of the declaration (\c D->getIdentifier()) as its
2155/// name.
Daniel Dunbarf981bf82009-11-21 09:14:52 +00002156void MangleContext::mangleName(const NamedDecl *D,
Daniel Dunbar94fd26d2009-11-21 09:06:22 +00002157 llvm::SmallVectorImpl<char> &Res) {
Daniel Dunbarc02ab4c2009-11-21 09:14:44 +00002158 assert((isa<FunctionDecl>(D) || isa<VarDecl>(D)) &&
2159 "Invalid mangleName() call, argument is not a variable or function!");
2160 assert(!isa<CXXConstructorDecl>(D) && !isa<CXXDestructorDecl>(D) &&
2161 "Invalid mangleName() call on 'structor decl!");
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002162
Daniel Dunbar1b077112009-11-21 09:06:10 +00002163 PrettyStackTraceDecl CrashInfo(D, SourceLocation(),
2164 getASTContext().getSourceManager(),
2165 "Mangling declaration");
Mike Stump1eb44332009-09-09 15:08:12 +00002166
Daniel Dunbar94fd26d2009-11-21 09:06:22 +00002167 CXXNameMangler Mangler(*this, Res);
2168 return Mangler.mangle(D);
Daniel Dunbar1b077112009-11-21 09:06:10 +00002169}
Mike Stump1eb44332009-09-09 15:08:12 +00002170
Daniel Dunbar1b077112009-11-21 09:06:10 +00002171void MangleContext::mangleCXXCtor(const CXXConstructorDecl *D, CXXCtorType Type,
Daniel Dunbar94fd26d2009-11-21 09:06:22 +00002172 llvm::SmallVectorImpl<char> &Res) {
Daniel Dunbar77939c92009-11-21 09:06:31 +00002173 CXXNameMangler Mangler(*this, Res, D, Type);
2174 Mangler.mangle(D);
Daniel Dunbar1b077112009-11-21 09:06:10 +00002175}
Mike Stump1eb44332009-09-09 15:08:12 +00002176
Daniel Dunbar1b077112009-11-21 09:06:10 +00002177void MangleContext::mangleCXXDtor(const CXXDestructorDecl *D, CXXDtorType Type,
Daniel Dunbar94fd26d2009-11-21 09:06:22 +00002178 llvm::SmallVectorImpl<char> &Res) {
Daniel Dunbar77939c92009-11-21 09:06:31 +00002179 CXXNameMangler Mangler(*this, Res, D, Type);
2180 Mangler.mangle(D);
Daniel Dunbar1b077112009-11-21 09:06:10 +00002181}
Mike Stumpf1216772009-07-31 18:25:34 +00002182
Douglas Gregor35415f52010-05-25 17:04:15 +00002183void MangleContext::mangleBlock(const BlockDecl *BD,
2184 llvm::SmallVectorImpl<char> &Res) {
Charles Davis685b1d92010-05-26 18:25:27 +00002185 MiscNameMangler Mangler(*this, Res);
Douglas Gregor35415f52010-05-25 17:04:15 +00002186 Mangler.mangleBlock(BD);
2187}
2188
Anders Carlsson19879c92010-03-23 17:17:29 +00002189void MangleContext::mangleThunk(const CXXMethodDecl *MD,
2190 const ThunkInfo &Thunk,
2191 llvm::SmallVectorImpl<char> &Res) {
2192 // <special-name> ::= T <call-offset> <base encoding>
2193 // # base is the nominal target function of thunk
2194 // <special-name> ::= Tc <call-offset> <call-offset> <base encoding>
2195 // # base is the nominal target function of thunk
2196 // # first call-offset is 'this' adjustment
2197 // # second call-offset is result adjustment
Sean Huntc3021132010-05-05 15:23:54 +00002198
Anders Carlsson19879c92010-03-23 17:17:29 +00002199 assert(!isa<CXXDestructorDecl>(MD) &&
2200 "Use mangleCXXDtor for destructor decls!");
Sean Huntc3021132010-05-05 15:23:54 +00002201
Anders Carlsson19879c92010-03-23 17:17:29 +00002202 CXXNameMangler Mangler(*this, Res);
2203 Mangler.getStream() << "_ZT";
2204 if (!Thunk.Return.isEmpty())
2205 Mangler.getStream() << 'c';
Sean Huntc3021132010-05-05 15:23:54 +00002206
Anders Carlsson19879c92010-03-23 17:17:29 +00002207 // Mangle the 'this' pointer adjustment.
2208 Mangler.mangleCallOffset(Thunk.This.NonVirtual, Thunk.This.VCallOffsetOffset);
Sean Huntc3021132010-05-05 15:23:54 +00002209
Anders Carlsson19879c92010-03-23 17:17:29 +00002210 // Mangle the return pointer adjustment if there is one.
2211 if (!Thunk.Return.isEmpty())
2212 Mangler.mangleCallOffset(Thunk.Return.NonVirtual,
2213 Thunk.Return.VBaseOffsetOffset);
Sean Huntc3021132010-05-05 15:23:54 +00002214
Anders Carlsson19879c92010-03-23 17:17:29 +00002215 Mangler.mangleFunctionEncoding(MD);
2216}
2217
Sean Huntc3021132010-05-05 15:23:54 +00002218void
Anders Carlsson19879c92010-03-23 17:17:29 +00002219MangleContext::mangleCXXDtorThunk(const CXXDestructorDecl *DD, CXXDtorType Type,
2220 const ThisAdjustment &ThisAdjustment,
2221 llvm::SmallVectorImpl<char> &Res) {
2222 // <special-name> ::= T <call-offset> <base encoding>
2223 // # base is the nominal target function of thunk
Sean Huntc3021132010-05-05 15:23:54 +00002224
Anders Carlsson19879c92010-03-23 17:17:29 +00002225 CXXNameMangler Mangler(*this, Res, DD, Type);
2226 Mangler.getStream() << "_ZT";
2227
2228 // Mangle the 'this' pointer adjustment.
Sean Huntc3021132010-05-05 15:23:54 +00002229 Mangler.mangleCallOffset(ThisAdjustment.NonVirtual,
Anders Carlsson19879c92010-03-23 17:17:29 +00002230 ThisAdjustment.VCallOffsetOffset);
2231
2232 Mangler.mangleFunctionEncoding(DD);
2233}
2234
Daniel Dunbarc0747712009-11-21 09:12:13 +00002235/// mangleGuardVariable - Returns the mangled name for a guard variable
2236/// for the passed in VarDecl.
2237void MangleContext::mangleGuardVariable(const VarDecl *D,
2238 llvm::SmallVectorImpl<char> &Res) {
2239 // <special-name> ::= GV <object name> # Guard variable for one-time
2240 // # initialization
2241 CXXNameMangler Mangler(*this, Res);
2242 Mangler.getStream() << "_ZGV";
2243 Mangler.mangleName(D);
2244}
2245
Anders Carlsson046c2942010-04-17 20:15:18 +00002246void MangleContext::mangleCXXVTable(const CXXRecordDecl *RD,
Daniel Dunbar94fd26d2009-11-21 09:06:22 +00002247 llvm::SmallVectorImpl<char> &Res) {
Daniel Dunbarc0747712009-11-21 09:12:13 +00002248 // <special-name> ::= TV <type> # virtual table
Daniel Dunbar94fd26d2009-11-21 09:06:22 +00002249 CXXNameMangler Mangler(*this, Res);
Daniel Dunbarc0747712009-11-21 09:12:13 +00002250 Mangler.getStream() << "_ZTV";
Douglas Gregor1b12a3b2010-05-26 05:11:13 +00002251 Mangler.mangleNameOrStandardSubstitution(RD);
Daniel Dunbar1b077112009-11-21 09:06:10 +00002252}
Mike Stump82d75b02009-11-10 01:58:37 +00002253
Daniel Dunbar1b077112009-11-21 09:06:10 +00002254void MangleContext::mangleCXXVTT(const CXXRecordDecl *RD,
Daniel Dunbar94fd26d2009-11-21 09:06:22 +00002255 llvm::SmallVectorImpl<char> &Res) {
Daniel Dunbarc0747712009-11-21 09:12:13 +00002256 // <special-name> ::= TT <type> # VTT structure
Daniel Dunbar94fd26d2009-11-21 09:06:22 +00002257 CXXNameMangler Mangler(*this, Res);
Daniel Dunbarc0747712009-11-21 09:12:13 +00002258 Mangler.getStream() << "_ZTT";
Douglas Gregor1b12a3b2010-05-26 05:11:13 +00002259 Mangler.mangleNameOrStandardSubstitution(RD);
Daniel Dunbar1b077112009-11-21 09:06:10 +00002260}
Mike Stumpab3f7e92009-11-10 01:41:59 +00002261
Anders Carlsson046c2942010-04-17 20:15:18 +00002262void MangleContext::mangleCXXCtorVTable(const CXXRecordDecl *RD, int64_t Offset,
Daniel Dunbar1b077112009-11-21 09:06:10 +00002263 const CXXRecordDecl *Type,
Daniel Dunbar94fd26d2009-11-21 09:06:22 +00002264 llvm::SmallVectorImpl<char> &Res) {
Daniel Dunbarc0747712009-11-21 09:12:13 +00002265 // <special-name> ::= TC <type> <offset number> _ <base type>
Daniel Dunbar94fd26d2009-11-21 09:06:22 +00002266 CXXNameMangler Mangler(*this, Res);
Daniel Dunbarc0747712009-11-21 09:12:13 +00002267 Mangler.getStream() << "_ZTC";
Douglas Gregor1b12a3b2010-05-26 05:11:13 +00002268 Mangler.mangleNameOrStandardSubstitution(RD);
Daniel Dunbarc0747712009-11-21 09:12:13 +00002269 Mangler.getStream() << Offset;
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002270 Mangler.getStream() << '_';
Douglas Gregor1b12a3b2010-05-26 05:11:13 +00002271 Mangler.mangleNameOrStandardSubstitution(Type);
Daniel Dunbar1b077112009-11-21 09:06:10 +00002272}
Mike Stump738f8c22009-07-31 23:15:31 +00002273
Mike Stumpde050572009-12-02 18:57:08 +00002274void MangleContext::mangleCXXRTTI(QualType Ty,
Daniel Dunbar94fd26d2009-11-21 09:06:22 +00002275 llvm::SmallVectorImpl<char> &Res) {
Daniel Dunbarc0747712009-11-21 09:12:13 +00002276 // <special-name> ::= TI <type> # typeinfo structure
Douglas Gregor154fe982009-12-23 22:04:40 +00002277 assert(!Ty.hasQualifiers() && "RTTI info cannot have top-level qualifiers");
Daniel Dunbar94fd26d2009-11-21 09:06:22 +00002278 CXXNameMangler Mangler(*this, Res);
Daniel Dunbarc0747712009-11-21 09:12:13 +00002279 Mangler.getStream() << "_ZTI";
2280 Mangler.mangleType(Ty);
Daniel Dunbar1b077112009-11-21 09:06:10 +00002281}
Mike Stump67795982009-11-14 00:14:13 +00002282
Mike Stumpde050572009-12-02 18:57:08 +00002283void MangleContext::mangleCXXRTTIName(QualType Ty,
Daniel Dunbar94fd26d2009-11-21 09:06:22 +00002284 llvm::SmallVectorImpl<char> &Res) {
Daniel Dunbarc0747712009-11-21 09:12:13 +00002285 // <special-name> ::= TS <type> # typeinfo name (null terminated byte string)
Daniel Dunbar94fd26d2009-11-21 09:06:22 +00002286 CXXNameMangler Mangler(*this, Res);
Daniel Dunbarc0747712009-11-21 09:12:13 +00002287 Mangler.getStream() << "_ZTS";
2288 Mangler.mangleType(Ty);
Mike Stumpf1216772009-07-31 18:25:34 +00002289}