blob: 2f8be294f2c56fa58256d90436beaff8e88ad851 [file] [log] [blame]
Peter Collingbourne14110472011-01-13 18:57:25 +00001//===--- ItaniumMangle.cpp - Itanium C++ Name Mangling ----------*- 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//===----------------------------------------------------------------------===//
Peter Collingbourne14110472011-01-13 18:57:25 +000017#include "clang/AST/Mangle.h"
Douglas Gregor5f2bfd42009-02-13 00:10:09 +000018#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"
Peter Collingbourne14110472011-01-13 18:57:25 +000024#include "clang/Basic/ABI.h"
Douglas Gregor6ec36682009-02-18 23:53:56 +000025#include "clang/Basic/SourceManager.h"
Anders Carlssonc4355b62009-10-07 01:45:02 +000026#include "llvm/ADT/StringExtras.h"
Douglas Gregor5f2bfd42009-02-13 00:10:09 +000027#include "llvm/Support/raw_ostream.h"
John McCallefe6aee2009-09-05 07:56:18 +000028#include "llvm/Support/ErrorHandling.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;
Charles Davis685b1d92010-05-26 18:25:27 +000037
Douglas Gregor5f2bfd42009-02-13 00:10:09 +000038namespace {
Fariborz Jahanian57058532010-03-03 19:41:08 +000039
John McCall82b7d7b2010-10-18 21:28:44 +000040static const CXXRecordDecl *GetLocalClassDecl(const NamedDecl *ND) {
41 const DeclContext *DC = dyn_cast<DeclContext>(ND);
42 if (!DC)
43 DC = ND->getDeclContext();
44 while (!DC->isNamespace() && !DC->isTranslationUnit()) {
45 if (isa<FunctionDecl>(DC->getParent()))
46 return dyn_cast<CXXRecordDecl>(DC);
47 DC = DC->getParent();
Fariborz Jahanian57058532010-03-03 19:41:08 +000048 }
49 return 0;
50}
51
Anders Carlsson7e120032009-11-24 05:36:32 +000052static const CXXMethodDecl *getStructor(const CXXMethodDecl *MD) {
53 assert((isa<CXXConstructorDecl>(MD) || isa<CXXDestructorDecl>(MD)) &&
54 "Passed in decl is not a ctor or dtor!");
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +000055
Anders Carlsson7e120032009-11-24 05:36:32 +000056 if (const TemplateDecl *TD = MD->getPrimaryTemplate()) {
57 MD = cast<CXXMethodDecl>(TD->getTemplatedDecl());
58
59 assert((isa<CXXConstructorDecl>(MD) || isa<CXXDestructorDecl>(MD)) &&
60 "Templated decl is not a ctor or dtor!");
61 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +000062
Anders Carlsson7e120032009-11-24 05:36:32 +000063 return MD;
64}
John McCall1dd73832010-02-04 01:42:13 +000065
66static const unsigned UnknownArity = ~0U;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +000067
Peter Collingbourne14110472011-01-13 18:57:25 +000068class ItaniumMangleContext : public MangleContext {
69 llvm::DenseMap<const TagDecl *, uint64_t> AnonStructIds;
70 unsigned Discriminator;
71 llvm::DenseMap<const NamedDecl*, unsigned> Uniquifier;
72
73public:
74 explicit ItaniumMangleContext(ASTContext &Context,
75 Diagnostic &Diags)
76 : MangleContext(Context, Diags) { }
77
78 uint64_t getAnonymousStructId(const TagDecl *TD) {
79 std::pair<llvm::DenseMap<const TagDecl *,
80 uint64_t>::iterator, bool> Result =
81 AnonStructIds.insert(std::make_pair(TD, AnonStructIds.size()));
82 return Result.first->second;
83 }
84
85 void startNewFunction() {
86 MangleContext::startNewFunction();
87 mangleInitDiscriminator();
88 }
89
90 /// @name Mangler Entry Points
91 /// @{
92
93 bool shouldMangleDeclName(const NamedDecl *D);
94 void mangleName(const NamedDecl *D, llvm::SmallVectorImpl<char> &);
95 void mangleThunk(const CXXMethodDecl *MD,
96 const ThunkInfo &Thunk,
97 llvm::SmallVectorImpl<char> &);
98 void mangleCXXDtorThunk(const CXXDestructorDecl *DD, CXXDtorType Type,
99 const ThisAdjustment &ThisAdjustment,
100 llvm::SmallVectorImpl<char> &);
101 void mangleReferenceTemporary(const VarDecl *D,
102 llvm::SmallVectorImpl<char> &);
103 void mangleCXXVTable(const CXXRecordDecl *RD,
104 llvm::SmallVectorImpl<char> &);
105 void mangleCXXVTT(const CXXRecordDecl *RD,
106 llvm::SmallVectorImpl<char> &);
107 void mangleCXXCtorVTable(const CXXRecordDecl *RD, int64_t Offset,
108 const CXXRecordDecl *Type,
109 llvm::SmallVectorImpl<char> &);
110 void mangleCXXRTTI(QualType T, llvm::SmallVectorImpl<char> &);
111 void mangleCXXRTTIName(QualType T, llvm::SmallVectorImpl<char> &);
112 void mangleCXXCtor(const CXXConstructorDecl *D, CXXCtorType Type,
113 llvm::SmallVectorImpl<char> &);
114 void mangleCXXDtor(const CXXDestructorDecl *D, CXXDtorType Type,
115 llvm::SmallVectorImpl<char> &);
116
117 void mangleItaniumGuardVariable(const VarDecl *D,
118 llvm::SmallVectorImpl<char> &);
119
120 void mangleInitDiscriminator() {
121 Discriminator = 0;
122 }
123
124 bool getNextDiscriminator(const NamedDecl *ND, unsigned &disc) {
125 unsigned &discriminator = Uniquifier[ND];
126 if (!discriminator)
127 discriminator = ++Discriminator;
128 if (discriminator == 1)
129 return false;
130 disc = discriminator-2;
131 return true;
132 }
133 /// @}
134};
135
Daniel Dunbar1b077112009-11-21 09:06:10 +0000136/// CXXNameMangler - Manage the mangling of a single name.
Daniel Dunbarc0747712009-11-21 09:12:13 +0000137class CXXNameMangler {
Peter Collingbourne14110472011-01-13 18:57:25 +0000138 ItaniumMangleContext &Context;
Daniel Dunbar94fd26d2009-11-21 09:06:22 +0000139 llvm::raw_svector_ostream Out;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000140
Daniel Dunbar1b077112009-11-21 09:06:10 +0000141 const CXXMethodDecl *Structor;
142 unsigned StructorType;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000143
Anders Carlsson9d85b722010-06-02 04:29:50 +0000144 /// SeqID - The next subsitution sequence number.
145 unsigned SeqID;
146
Daniel Dunbar1b077112009-11-21 09:06:10 +0000147 llvm::DenseMap<uintptr_t, unsigned> Substitutions;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000148
John McCall1dd73832010-02-04 01:42:13 +0000149 ASTContext &getASTContext() const { return Context.getASTContext(); }
150
Daniel Dunbarc0747712009-11-21 09:12:13 +0000151public:
Peter Collingbourne14110472011-01-13 18:57:25 +0000152 CXXNameMangler(ItaniumMangleContext &C, llvm::SmallVectorImpl<char> &Res)
Anders Carlsson9d85b722010-06-02 04:29:50 +0000153 : Context(C), Out(Res), Structor(0), StructorType(0), SeqID(0) { }
Peter Collingbourne14110472011-01-13 18:57:25 +0000154 CXXNameMangler(ItaniumMangleContext &C, llvm::SmallVectorImpl<char> &Res,
Daniel Dunbar77939c92009-11-21 09:06:31 +0000155 const CXXConstructorDecl *D, CXXCtorType Type)
Anders Carlsson9d85b722010-06-02 04:29:50 +0000156 : Context(C), Out(Res), Structor(getStructor(D)), StructorType(Type),
157 SeqID(0) { }
Peter Collingbourne14110472011-01-13 18:57:25 +0000158 CXXNameMangler(ItaniumMangleContext &C, llvm::SmallVectorImpl<char> &Res,
Daniel Dunbar77939c92009-11-21 09:06:31 +0000159 const CXXDestructorDecl *D, CXXDtorType Type)
Anders Carlsson9d85b722010-06-02 04:29:50 +0000160 : Context(C), Out(Res), Structor(getStructor(D)), StructorType(Type),
161 SeqID(0) { }
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000162
Anders Carlssonf98574b2010-02-05 07:31:37 +0000163#if MANGLE_CHECKER
164 ~CXXNameMangler() {
165 if (Out.str()[0] == '\01')
166 return;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000167
Anders Carlssonf98574b2010-02-05 07:31:37 +0000168 int status = 0;
169 char *result = abi::__cxa_demangle(Out.str().str().c_str(), 0, 0, &status);
170 assert(status == 0 && "Could not demangle mangled name!");
171 free(result);
172 }
173#endif
Daniel Dunbarc0747712009-11-21 09:12:13 +0000174 llvm::raw_svector_ostream &getStream() { return Out; }
175
Daniel Dunbar7e0c1952009-11-21 09:17:15 +0000176 void mangle(const NamedDecl *D, llvm::StringRef Prefix = "_Z");
Anders Carlsson19879c92010-03-23 17:17:29 +0000177 void mangleCallOffset(int64_t NonVirtual, int64_t Virtual);
John McCall0512e482010-07-14 04:20:34 +0000178 void mangleNumber(const llvm::APSInt &I);
Anders Carlssona94822e2009-11-26 02:32:05 +0000179 void mangleNumber(int64_t Number);
John McCall0512e482010-07-14 04:20:34 +0000180 void mangleFloat(const llvm::APFloat &F);
Daniel Dunbarc0747712009-11-21 09:12:13 +0000181 void mangleFunctionEncoding(const FunctionDecl *FD);
182 void mangleName(const NamedDecl *ND);
183 void mangleType(QualType T);
Douglas Gregor1b12a3b2010-05-26 05:11:13 +0000184 void mangleNameOrStandardSubstitution(const NamedDecl *ND);
185
Daniel Dunbarc0747712009-11-21 09:12:13 +0000186private:
Daniel Dunbar1b077112009-11-21 09:06:10 +0000187 bool mangleSubstitution(const NamedDecl *ND);
188 bool mangleSubstitution(QualType T);
Douglas Gregor1e9268e2010-04-28 05:58:56 +0000189 bool mangleSubstitution(TemplateName Template);
Daniel Dunbar1b077112009-11-21 09:06:10 +0000190 bool mangleSubstitution(uintptr_t Ptr);
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000191
Daniel Dunbar1b077112009-11-21 09:06:10 +0000192 bool mangleStandardSubstitution(const NamedDecl *ND);
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000193
Daniel Dunbar1b077112009-11-21 09:06:10 +0000194 void addSubstitution(const NamedDecl *ND) {
195 ND = cast<NamedDecl>(ND->getCanonicalDecl());
Anders Carlsson433d1372009-11-07 04:26:04 +0000196
Daniel Dunbar1b077112009-11-21 09:06:10 +0000197 addSubstitution(reinterpret_cast<uintptr_t>(ND));
198 }
199 void addSubstitution(QualType T);
Douglas Gregor1e9268e2010-04-28 05:58:56 +0000200 void addSubstitution(TemplateName Template);
Daniel Dunbar1b077112009-11-21 09:06:10 +0000201 void addSubstitution(uintptr_t Ptr);
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000202
John McCall1dd73832010-02-04 01:42:13 +0000203 void mangleUnresolvedScope(NestedNameSpecifier *Qualifier);
204 void mangleUnresolvedName(NestedNameSpecifier *Qualifier,
205 DeclarationName Name,
206 unsigned KnownArity = UnknownArity);
207
Daniel Dunbar1b077112009-11-21 09:06:10 +0000208 void mangleName(const TemplateDecl *TD,
209 const TemplateArgument *TemplateArgs,
210 unsigned NumTemplateArgs);
John McCall1dd73832010-02-04 01:42:13 +0000211 void mangleUnqualifiedName(const NamedDecl *ND) {
212 mangleUnqualifiedName(ND, ND->getDeclName(), UnknownArity);
213 }
214 void mangleUnqualifiedName(const NamedDecl *ND, DeclarationName Name,
215 unsigned KnownArity);
Daniel Dunbar1b077112009-11-21 09:06:10 +0000216 void mangleUnscopedName(const NamedDecl *ND);
217 void mangleUnscopedTemplateName(const TemplateDecl *ND);
Douglas Gregor1e9268e2010-04-28 05:58:56 +0000218 void mangleUnscopedTemplateName(TemplateName);
Daniel Dunbar1b077112009-11-21 09:06:10 +0000219 void mangleSourceName(const IdentifierInfo *II);
220 void mangleLocalName(const NamedDecl *ND);
Fariborz Jahanian57058532010-03-03 19:41:08 +0000221 void mangleNestedName(const NamedDecl *ND, const DeclContext *DC,
222 bool NoFunction=false);
Daniel Dunbar1b077112009-11-21 09:06:10 +0000223 void mangleNestedName(const TemplateDecl *TD,
224 const TemplateArgument *TemplateArgs,
225 unsigned NumTemplateArgs);
Fariborz Jahanian57058532010-03-03 19:41:08 +0000226 void manglePrefix(const DeclContext *DC, bool NoFunction=false);
Daniel Dunbar1b077112009-11-21 09:06:10 +0000227 void mangleTemplatePrefix(const TemplateDecl *ND);
Douglas Gregor20f0cc72010-04-23 03:10:43 +0000228 void mangleTemplatePrefix(TemplateName Template);
Daniel Dunbar1b077112009-11-21 09:06:10 +0000229 void mangleOperatorName(OverloadedOperatorKind OO, unsigned Arity);
230 void mangleQualifiers(Qualifiers Quals);
John McCallefe6aee2009-09-05 07:56:18 +0000231
Anders Carlsson7b06f6c2009-12-10 03:14:39 +0000232 void mangleObjCMethodName(const ObjCMethodDecl *MD);
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000233
Daniel Dunbar1b077112009-11-21 09:06:10 +0000234 // Declare manglers for every type class.
John McCallefe6aee2009-09-05 07:56:18 +0000235#define ABSTRACT_TYPE(CLASS, PARENT)
236#define NON_CANONICAL_TYPE(CLASS, PARENT)
237#define TYPE(CLASS, PARENT) void mangleType(const CLASS##Type *T);
238#include "clang/AST/TypeNodes.def"
239
Daniel Dunbar1b077112009-11-21 09:06:10 +0000240 void mangleType(const TagType*);
John McCallb6f532e2010-07-14 06:43:17 +0000241 void mangleType(TemplateName);
Daniel Dunbar1b077112009-11-21 09:06:10 +0000242 void mangleBareFunctionType(const FunctionType *T,
243 bool MangleReturnType);
Bob Wilson57147a82010-11-16 00:32:18 +0000244 void mangleNeonVectorType(const VectorType *T);
Anders Carlssone170ba72009-12-14 01:45:37 +0000245
246 void mangleIntegerLiteral(QualType T, const llvm::APSInt &Value);
John McCall2f27bf82010-02-04 02:56:29 +0000247 void mangleMemberExpr(const Expr *Base, bool IsArrow,
248 NestedNameSpecifier *Qualifier,
249 DeclarationName Name,
250 unsigned KnownArity);
John McCall5e1e89b2010-08-18 19:18:59 +0000251 void mangleExpression(const Expr *E, unsigned Arity = UnknownArity);
Daniel Dunbar1b077112009-11-21 09:06:10 +0000252 void mangleCXXCtorType(CXXCtorType T);
253 void mangleCXXDtorType(CXXDtorType T);
Mike Stump1eb44332009-09-09 15:08:12 +0000254
John McCall6dbce192010-08-20 00:17:19 +0000255 void mangleTemplateArgs(const ExplicitTemplateArgumentList &TemplateArgs);
Douglas Gregor20f0cc72010-04-23 03:10:43 +0000256 void mangleTemplateArgs(TemplateName Template,
257 const TemplateArgument *TemplateArgs,
Sean Huntc3021132010-05-05 15:23:54 +0000258 unsigned NumTemplateArgs);
Rafael Espindolad9800722010-03-11 14:07:00 +0000259 void mangleTemplateArgs(const TemplateParameterList &PL,
260 const TemplateArgument *TemplateArgs,
Daniel Dunbar1b077112009-11-21 09:06:10 +0000261 unsigned NumTemplateArgs);
Rafael Espindolad9800722010-03-11 14:07:00 +0000262 void mangleTemplateArgs(const TemplateParameterList &PL,
263 const TemplateArgumentList &AL);
264 void mangleTemplateArg(const NamedDecl *P, const TemplateArgument &A);
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000265
Daniel Dunbar1b077112009-11-21 09:06:10 +0000266 void mangleTemplateParameter(unsigned Index);
267};
Peter Collingbourne14110472011-01-13 18:57:25 +0000268
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000269}
270
Anders Carlsson43f17402009-04-02 15:51:53 +0000271static bool isInCLinkageSpecification(const Decl *D) {
Douglas Gregor457e2812009-10-28 16:31:34 +0000272 D = D->getCanonicalDecl();
Mike Stump1eb44332009-09-09 15:08:12 +0000273 for (const DeclContext *DC = D->getDeclContext();
Anders Carlsson43f17402009-04-02 15:51:53 +0000274 !DC->isTranslationUnit(); DC = DC->getParent()) {
Mike Stump1eb44332009-09-09 15:08:12 +0000275 if (const LinkageSpecDecl *Linkage = dyn_cast<LinkageSpecDecl>(DC))
Anders Carlsson43f17402009-04-02 15:51:53 +0000276 return Linkage->getLanguage() == LinkageSpecDecl::lang_c;
277 }
Mike Stump1eb44332009-09-09 15:08:12 +0000278
Anders Carlsson43f17402009-04-02 15:51:53 +0000279 return false;
280}
281
Peter Collingbourne14110472011-01-13 18:57:25 +0000282bool ItaniumMangleContext::shouldMangleDeclName(const NamedDecl *D) {
Daniel Dunbarf981bf82009-11-21 09:14:52 +0000283 // In C, functions with no attributes never need to be mangled. Fastpath them.
284 if (!getASTContext().getLangOptions().CPlusPlus && !D->hasAttrs())
285 return false;
286
287 // Any decl can be declared with __asm("foo") on it, and this takes precedence
288 // over all other naming in the .o file.
289 if (D->hasAttr<AsmLabelAttr>())
290 return true;
291
Mike Stump141c5af2009-09-02 00:25:38 +0000292 // Clang's "overloadable" attribute extension to C/C++ implies name mangling
Anders Carlssona1e16222009-11-07 07:15:03 +0000293 // (always) as does passing a C++ member function and a function
294 // whose name is not a simple identifier.
Daniel Dunbarf981bf82009-11-21 09:14:52 +0000295 const FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
296 if (FD && (FD->hasAttr<OverloadableAttr>() || isa<CXXMethodDecl>(FD) ||
297 !FD->getDeclName().isIdentifier()))
298 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000299
Daniel Dunbarf981bf82009-11-21 09:14:52 +0000300 // Otherwise, no mangling is done outside C++ mode.
301 if (!getASTContext().getLangOptions().CPlusPlus)
302 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000303
Sean Hunt31455252010-01-24 03:04:27 +0000304 // Variables at global scope with non-internal linkage are not mangled
Eli Friedman7facf842009-12-02 20:32:49 +0000305 if (!FD) {
306 const DeclContext *DC = D->getDeclContext();
307 // Check for extern variable declared locally.
Fariborz Jahaniane81c5612010-06-30 18:57:21 +0000308 if (DC->isFunctionOrMethod() && D->hasLinkage())
Eli Friedman7facf842009-12-02 20:32:49 +0000309 while (!DC->isNamespace() && !DC->isTranslationUnit())
310 DC = DC->getParent();
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000311 if (DC->isTranslationUnit() && D->getLinkage() != InternalLinkage)
Eli Friedman7facf842009-12-02 20:32:49 +0000312 return false;
313 }
314
Eli Friedmanc00cb642010-07-18 20:49:59 +0000315 // Class members are always mangled.
316 if (D->getDeclContext()->isRecord())
317 return true;
318
Eli Friedman7facf842009-12-02 20:32:49 +0000319 // C functions and "main" are not mangled.
320 if ((FD && FD->isMain()) || isInCLinkageSpecification(D))
Daniel Dunbarf981bf82009-11-21 09:14:52 +0000321 return false;
322
Anders Carlsson43f17402009-04-02 15:51:53 +0000323 return true;
324}
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000325
Daniel Dunbar7e0c1952009-11-21 09:17:15 +0000326void CXXNameMangler::mangle(const NamedDecl *D, llvm::StringRef Prefix) {
Mike Stump141c5af2009-09-02 00:25:38 +0000327 // Any decl can be declared with __asm("foo") on it, and this takes precedence
328 // over all other naming in the .o file.
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000329 if (const AsmLabelAttr *ALA = D->getAttr<AsmLabelAttr>()) {
Chris Lattnerca3f25c2009-03-21 08:24:40 +0000330 // If we have an asm name, then we use it as the mangling.
331 Out << '\01'; // LLVM IR Marker for __asm("foo")
332 Out << ALA->getLabel();
Daniel Dunbarf981bf82009-11-21 09:14:52 +0000333 return;
Chris Lattnerca3f25c2009-03-21 08:24:40 +0000334 }
Mike Stump1eb44332009-09-09 15:08:12 +0000335
Sean Hunt31455252010-01-24 03:04:27 +0000336 // <mangled-name> ::= _Z <encoding>
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000337 // ::= <data name>
338 // ::= <special-name>
Daniel Dunbar7e0c1952009-11-21 09:17:15 +0000339 Out << Prefix;
340 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
Daniel Dunbarf981bf82009-11-21 09:14:52 +0000341 mangleFunctionEncoding(FD);
Rafael Espindolad9800722010-03-11 14:07:00 +0000342 else if (const VarDecl *VD = dyn_cast<VarDecl>(D))
343 mangleName(VD);
Daniel Dunbar7e0c1952009-11-21 09:17:15 +0000344 else
Rafael Espindolad9800722010-03-11 14:07:00 +0000345 mangleName(cast<FieldDecl>(D));
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000346}
347
348void CXXNameMangler::mangleFunctionEncoding(const FunctionDecl *FD) {
349 // <encoding> ::= <function name> <bare-function-type>
350 mangleName(FD);
Mike Stump1eb44332009-09-09 15:08:12 +0000351
Daniel Dunbar7e0c1952009-11-21 09:17:15 +0000352 // Don't mangle in the type if this isn't a decl we should typically mangle.
353 if (!Context.shouldMangleDeclName(FD))
354 return;
355
Mike Stump141c5af2009-09-02 00:25:38 +0000356 // Whether the mangling of a function type includes the return type depends on
357 // the context and the nature of the function. The rules for deciding whether
358 // the return type is included are:
Mike Stump1eb44332009-09-09 15:08:12 +0000359 //
Douglas Gregor1fd2dd12009-06-29 22:39:32 +0000360 // 1. Template functions (names or types) have return types encoded, with
361 // the exceptions listed below.
Mike Stump1eb44332009-09-09 15:08:12 +0000362 // 2. Function types not appearing as part of a function name mangling,
Douglas Gregor1fd2dd12009-06-29 22:39:32 +0000363 // e.g. parameters, pointer types, etc., have return type encoded, with the
364 // exceptions listed below.
365 // 3. Non-template function names do not have return types encoded.
366 //
Mike Stump141c5af2009-09-02 00:25:38 +0000367 // The exceptions mentioned in (1) and (2) above, for which the return type is
368 // never included, are
Douglas Gregor1fd2dd12009-06-29 22:39:32 +0000369 // 1. Constructors.
370 // 2. Destructors.
371 // 3. Conversion operator functions, e.g. operator int.
372 bool MangleReturnType = false;
Anders Carlsson9234b7f2009-09-17 03:46:43 +0000373 if (FunctionTemplateDecl *PrimaryTemplate = FD->getPrimaryTemplate()) {
374 if (!(isa<CXXConstructorDecl>(FD) || isa<CXXDestructorDecl>(FD) ||
375 isa<CXXConversionDecl>(FD)))
376 MangleReturnType = true;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000377
Anders Carlsson9234b7f2009-09-17 03:46:43 +0000378 // Mangle the type of the primary template.
379 FD = PrimaryTemplate->getTemplatedDecl();
380 }
381
John McCall54e14c42009-10-22 22:37:11 +0000382 // Do the canonicalization out here because parameter types can
383 // undergo additional canonicalization (e.g. array decay).
384 FunctionType *FT = cast<FunctionType>(Context.getASTContext()
385 .getCanonicalType(FD->getType()));
386
387 mangleBareFunctionType(FT, MangleReturnType);
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000388}
389
Anders Carlsson47846d22009-12-04 06:23:23 +0000390static const DeclContext *IgnoreLinkageSpecDecls(const DeclContext *DC) {
391 while (isa<LinkageSpecDecl>(DC)) {
Anders Carlsson47846d22009-12-04 06:23:23 +0000392 DC = DC->getParent();
393 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000394
Anders Carlsson47846d22009-12-04 06:23:23 +0000395 return DC;
396}
397
Anders Carlssonc820f902010-06-02 15:58:27 +0000398/// isStd - Return whether a given namespace is the 'std' namespace.
399static bool isStd(const NamespaceDecl *NS) {
400 if (!IgnoreLinkageSpecDecls(NS->getParent())->isTranslationUnit())
401 return false;
402
403 const IdentifierInfo *II = NS->getOriginalNamespace()->getIdentifier();
404 return II && II->isStr("std");
405}
406
Anders Carlsson47846d22009-12-04 06:23:23 +0000407// isStdNamespace - Return whether a given decl context is a toplevel 'std'
408// namespace.
Daniel Dunbar1308af92009-11-21 09:11:45 +0000409static bool isStdNamespace(const DeclContext *DC) {
Anders Carlsson47846d22009-12-04 06:23:23 +0000410 if (!DC->isNamespace())
411 return false;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000412
Anders Carlsson47846d22009-12-04 06:23:23 +0000413 return isStd(cast<NamespaceDecl>(DC));
Daniel Dunbar1308af92009-11-21 09:11:45 +0000414}
415
Anders Carlssonbb36ba42009-09-26 03:24:57 +0000416static const TemplateDecl *
417isTemplate(const NamedDecl *ND, const TemplateArgumentList *&TemplateArgs) {
Anders Carlsson2744a062009-09-18 19:00:18 +0000418 // Check if we have a function template.
419 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)){
Anders Carlssonbb36ba42009-09-26 03:24:57 +0000420 if (const TemplateDecl *TD = FD->getPrimaryTemplate()) {
Anders Carlsson2744a062009-09-18 19:00:18 +0000421 TemplateArgs = FD->getTemplateSpecializationArgs();
Anders Carlssonbb36ba42009-09-26 03:24:57 +0000422 return TD;
Anders Carlsson2744a062009-09-18 19:00:18 +0000423 }
424 }
425
Anders Carlssoneafc6dc2009-09-18 19:44:50 +0000426 // Check if we have a class template.
427 if (const ClassTemplateSpecializationDecl *Spec =
428 dyn_cast<ClassTemplateSpecializationDecl>(ND)) {
429 TemplateArgs = &Spec->getTemplateArgs();
Anders Carlssonbb36ba42009-09-26 03:24:57 +0000430 return Spec->getSpecializedTemplate();
Anders Carlssoneafc6dc2009-09-18 19:44:50 +0000431 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000432
Anders Carlsson2744a062009-09-18 19:00:18 +0000433 return 0;
434}
435
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000436void CXXNameMangler::mangleName(const NamedDecl *ND) {
437 // <name> ::= <nested-name>
438 // ::= <unscoped-name>
439 // ::= <unscoped-template-name> <template-args>
Anders Carlsson201ce742009-09-17 03:17:01 +0000440 // ::= <local-name>
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000441 //
Anders Carlssond58d6f72009-09-17 16:12:20 +0000442 const DeclContext *DC = ND->getDeclContext();
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000443
Eli Friedman7facf842009-12-02 20:32:49 +0000444 // If this is an extern variable declared locally, the relevant DeclContext
445 // is that of the containing namespace, or the translation unit.
446 if (isa<FunctionDecl>(DC) && ND->hasLinkage())
447 while (!DC->isNamespace() && !DC->isTranslationUnit())
448 DC = DC->getParent();
John McCall82b7d7b2010-10-18 21:28:44 +0000449 else if (GetLocalClassDecl(ND)) {
450 mangleLocalName(ND);
451 return;
452 }
Eli Friedman7facf842009-12-02 20:32:49 +0000453
Anders Carlsson5cc58c62009-09-22 17:23:30 +0000454 while (isa<LinkageSpecDecl>(DC))
Anders Carlssond58d6f72009-09-17 16:12:20 +0000455 DC = DC->getParent();
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000456
Anders Carlssond58d6f72009-09-17 16:12:20 +0000457 if (DC->isTranslationUnit() || isStdNamespace(DC)) {
Anders Carlsson2744a062009-09-18 19:00:18 +0000458 // Check if we have a template.
459 const TemplateArgumentList *TemplateArgs = 0;
Anders Carlsson0fa6df42009-09-26 19:45:45 +0000460 if (const TemplateDecl *TD = isTemplate(ND, TemplateArgs)) {
Anders Carlsson2744a062009-09-18 19:00:18 +0000461 mangleUnscopedTemplateName(TD);
Rafael Espindolad9800722010-03-11 14:07:00 +0000462 TemplateParameterList *TemplateParameters = TD->getTemplateParameters();
463 mangleTemplateArgs(*TemplateParameters, *TemplateArgs);
Anders Carlsson2744a062009-09-18 19:00:18 +0000464 return;
Anders Carlsson7482e242009-09-18 04:29:09 +0000465 }
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000466
Anders Carlsson7482e242009-09-18 04:29:09 +0000467 mangleUnscopedName(ND);
468 return;
469 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000470
Anders Carlsson7b06f6c2009-12-10 03:14:39 +0000471 if (isa<FunctionDecl>(DC) || isa<ObjCMethodDecl>(DC)) {
Anders Carlsson7482e242009-09-18 04:29:09 +0000472 mangleLocalName(ND);
473 return;
474 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000475
Eli Friedman7facf842009-12-02 20:32:49 +0000476 mangleNestedName(ND, DC);
Anders Carlsson7482e242009-09-18 04:29:09 +0000477}
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000478void CXXNameMangler::mangleName(const TemplateDecl *TD,
Anders Carlsson7624f212009-09-18 02:42:01 +0000479 const TemplateArgument *TemplateArgs,
480 unsigned NumTemplateArgs) {
Anders Carlsson47846d22009-12-04 06:23:23 +0000481 const DeclContext *DC = IgnoreLinkageSpecDecls(TD->getDeclContext());
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000482
Anders Carlsson7624f212009-09-18 02:42:01 +0000483 if (DC->isTranslationUnit() || isStdNamespace(DC)) {
Anders Carlsson0fa6df42009-09-26 19:45:45 +0000484 mangleUnscopedTemplateName(TD);
Rafael Espindolad9800722010-03-11 14:07:00 +0000485 TemplateParameterList *TemplateParameters = TD->getTemplateParameters();
486 mangleTemplateArgs(*TemplateParameters, TemplateArgs, NumTemplateArgs);
Anders Carlsson7624f212009-09-18 02:42:01 +0000487 } else {
488 mangleNestedName(TD, TemplateArgs, NumTemplateArgs);
489 }
490}
491
Anders Carlsson201ce742009-09-17 03:17:01 +0000492void CXXNameMangler::mangleUnscopedName(const NamedDecl *ND) {
493 // <unscoped-name> ::= <unqualified-name>
494 // ::= St <unqualified-name> # ::std::
495 if (isStdNamespace(ND->getDeclContext()))
496 Out << "St";
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000497
Anders Carlsson201ce742009-09-17 03:17:01 +0000498 mangleUnqualifiedName(ND);
499}
500
Anders Carlsson0fa6df42009-09-26 19:45:45 +0000501void CXXNameMangler::mangleUnscopedTemplateName(const TemplateDecl *ND) {
Anders Carlsson201ce742009-09-17 03:17:01 +0000502 // <unscoped-template-name> ::= <unscoped-name>
503 // ::= <substitution>
Anders Carlsson7624f212009-09-18 02:42:01 +0000504 if (mangleSubstitution(ND))
Anders Carlsson03c9d532009-09-17 04:02:31 +0000505 return;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000506
Douglas Gregor32fb4e12010-02-05 20:45:00 +0000507 // <template-template-param> ::= <template-param>
508 if (const TemplateTemplateParmDecl *TTP
509 = dyn_cast<TemplateTemplateParmDecl>(ND)) {
510 mangleTemplateParameter(TTP->getIndex());
511 return;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000512 }
Douglas Gregor32fb4e12010-02-05 20:45:00 +0000513
Anders Carlsson1668f202009-09-26 20:13:56 +0000514 mangleUnscopedName(ND->getTemplatedDecl());
Anders Carlsson7624f212009-09-18 02:42:01 +0000515 addSubstitution(ND);
Anders Carlsson201ce742009-09-17 03:17:01 +0000516}
517
Douglas Gregor1e9268e2010-04-28 05:58:56 +0000518void CXXNameMangler::mangleUnscopedTemplateName(TemplateName Template) {
519 // <unscoped-template-name> ::= <unscoped-name>
520 // ::= <substitution>
521 if (TemplateDecl *TD = Template.getAsTemplateDecl())
522 return mangleUnscopedTemplateName(TD);
Sean Huntc3021132010-05-05 15:23:54 +0000523
Douglas Gregor1e9268e2010-04-28 05:58:56 +0000524 if (mangleSubstitution(Template))
525 return;
526
527 // FIXME: How to cope with operators here?
528 DependentTemplateName *Dependent = Template.getAsDependentTemplateName();
529 assert(Dependent && "Not a dependent template name?");
530 if (!Dependent->isIdentifier()) {
531 // FIXME: We can't possibly know the arity of the operator here!
532 Diagnostic &Diags = Context.getDiags();
533 unsigned DiagID = Diags.getCustomDiagID(Diagnostic::Error,
534 "cannot mangle dependent operator name");
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000535 Diags.Report(DiagID);
Douglas Gregor1e9268e2010-04-28 05:58:56 +0000536 return;
537 }
Sean Huntc3021132010-05-05 15:23:54 +0000538
Douglas Gregor1e9268e2010-04-28 05:58:56 +0000539 mangleSourceName(Dependent->getIdentifier());
540 addSubstitution(Template);
541}
542
John McCall0512e482010-07-14 04:20:34 +0000543void CXXNameMangler::mangleFloat(const llvm::APFloat &F) {
544 // TODO: avoid this copy with careful stream management.
545 llvm::SmallString<20> Buffer;
546 F.bitcastToAPInt().toString(Buffer, 16, false);
547 Out.write(Buffer.data(), Buffer.size());
548}
549
550void CXXNameMangler::mangleNumber(const llvm::APSInt &Value) {
551 if (Value.isSigned() && Value.isNegative()) {
552 Out << 'n';
553 Value.abs().print(Out, true);
554 } else
555 Value.print(Out, Value.isSigned());
556}
557
Anders Carlssona94822e2009-11-26 02:32:05 +0000558void CXXNameMangler::mangleNumber(int64_t Number) {
559 // <number> ::= [n] <non-negative decimal integer>
560 if (Number < 0) {
561 Out << 'n';
562 Number = -Number;
563 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000564
Anders Carlssona94822e2009-11-26 02:32:05 +0000565 Out << Number;
566}
567
Anders Carlsson19879c92010-03-23 17:17:29 +0000568void CXXNameMangler::mangleCallOffset(int64_t NonVirtual, int64_t Virtual) {
Mike Stump141c5af2009-09-02 00:25:38 +0000569 // <call-offset> ::= h <nv-offset> _
570 // ::= v <v-offset> _
571 // <nv-offset> ::= <offset number> # non-virtual base override
Anders Carlssona94822e2009-11-26 02:32:05 +0000572 // <v-offset> ::= <offset number> _ <virtual offset number>
Mike Stump141c5af2009-09-02 00:25:38 +0000573 // # virtual base override, with vcall offset
Anders Carlsson19879c92010-03-23 17:17:29 +0000574 if (!Virtual) {
Anders Carlssona94822e2009-11-26 02:32:05 +0000575 Out << 'h';
Anders Carlsson19879c92010-03-23 17:17:29 +0000576 mangleNumber(NonVirtual);
Anders Carlssona94822e2009-11-26 02:32:05 +0000577 Out << '_';
578 return;
Mike Stump141c5af2009-09-02 00:25:38 +0000579 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000580
Anders Carlssona94822e2009-11-26 02:32:05 +0000581 Out << 'v';
Anders Carlsson19879c92010-03-23 17:17:29 +0000582 mangleNumber(NonVirtual);
Anders Carlssona94822e2009-11-26 02:32:05 +0000583 Out << '_';
Anders Carlsson19879c92010-03-23 17:17:29 +0000584 mangleNumber(Virtual);
Anders Carlssona94822e2009-11-26 02:32:05 +0000585 Out << '_';
Mike Stump9124bcc2009-09-02 00:56:18 +0000586}
587
John McCall1dd73832010-02-04 01:42:13 +0000588void CXXNameMangler::mangleUnresolvedScope(NestedNameSpecifier *Qualifier) {
589 Qualifier = getASTContext().getCanonicalNestedNameSpecifier(Qualifier);
590 switch (Qualifier->getKind()) {
591 case NestedNameSpecifier::Global:
592 // nothing
593 break;
594 case NestedNameSpecifier::Namespace:
595 mangleName(Qualifier->getAsNamespace());
596 break;
597 case NestedNameSpecifier::TypeSpec:
Rafael Espindola9b35b252010-03-17 04:28:11 +0000598 case NestedNameSpecifier::TypeSpecWithTemplate: {
599 const Type *QTy = Qualifier->getAsType();
600
601 if (const TemplateSpecializationType *TST =
602 dyn_cast<TemplateSpecializationType>(QTy)) {
603 if (!mangleSubstitution(QualType(TST, 0))) {
Douglas Gregor20f0cc72010-04-23 03:10:43 +0000604 mangleTemplatePrefix(TST->getTemplateName());
Sean Huntc3021132010-05-05 15:23:54 +0000605
Douglas Gregor20f0cc72010-04-23 03:10:43 +0000606 // FIXME: GCC does not appear to mangle the template arguments when
607 // the template in question is a dependent template name. Should we
608 // emulate that badness?
609 mangleTemplateArgs(TST->getTemplateName(), TST->getArgs(),
Rafael Espindola9b35b252010-03-17 04:28:11 +0000610 TST->getNumArgs());
611 addSubstitution(QualType(TST, 0));
612 }
613 } else {
614 // We use the QualType mangle type variant here because it handles
615 // substitutions.
616 mangleType(QualType(QTy, 0));
617 }
618 }
John McCall1dd73832010-02-04 01:42:13 +0000619 break;
620 case NestedNameSpecifier::Identifier:
John McCallad5e7382010-03-01 23:49:17 +0000621 // Member expressions can have these without prefixes.
622 if (Qualifier->getPrefix())
623 mangleUnresolvedScope(Qualifier->getPrefix());
John McCall1dd73832010-02-04 01:42:13 +0000624 mangleSourceName(Qualifier->getAsIdentifier());
625 break;
626 }
627}
628
629/// Mangles a name which was not resolved to a specific entity.
630void CXXNameMangler::mangleUnresolvedName(NestedNameSpecifier *Qualifier,
631 DeclarationName Name,
632 unsigned KnownArity) {
633 if (Qualifier)
634 mangleUnresolvedScope(Qualifier);
635 // FIXME: ambiguity of unqualified lookup with ::
636
637 mangleUnqualifiedName(0, Name, KnownArity);
638}
639
Anders Carlsson6f7e2f42010-06-08 14:49:03 +0000640static const FieldDecl *FindFirstNamedDataMember(const RecordDecl *RD) {
641 assert(RD->isAnonymousStructOrUnion() &&
642 "Expected anonymous struct or union!");
643
644 for (RecordDecl::field_iterator I = RD->field_begin(), E = RD->field_end();
645 I != E; ++I) {
646 const FieldDecl *FD = *I;
647
648 if (FD->getIdentifier())
649 return FD;
650
651 if (const RecordType *RT = FD->getType()->getAs<RecordType>()) {
652 if (const FieldDecl *NamedDataMember =
653 FindFirstNamedDataMember(RT->getDecl()))
654 return NamedDataMember;
655 }
656 }
657
658 // We didn't find a named data member.
659 return 0;
660}
661
John McCall1dd73832010-02-04 01:42:13 +0000662void CXXNameMangler::mangleUnqualifiedName(const NamedDecl *ND,
663 DeclarationName Name,
664 unsigned KnownArity) {
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000665 // <unqualified-name> ::= <operator-name>
Mike Stump1eb44332009-09-09 15:08:12 +0000666 // ::= <ctor-dtor-name>
667 // ::= <source-name>
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000668 switch (Name.getNameKind()) {
Anders Carlssonc4355b62009-10-07 01:45:02 +0000669 case DeclarationName::Identifier: {
Anders Carlssonc4355b62009-10-07 01:45:02 +0000670 if (const IdentifierInfo *II = Name.getAsIdentifierInfo()) {
Sean Hunt31455252010-01-24 03:04:27 +0000671 // We must avoid conflicts between internally- and externally-
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000672 // linked variable declaration names in the same TU.
Anders Carlssonaec25232010-02-06 04:52:27 +0000673 // This naming convention is the same as that followed by GCC, though it
674 // shouldn't actually matter.
675 if (ND && isa<VarDecl>(ND) && ND->getLinkage() == InternalLinkage &&
Sean Hunt31455252010-01-24 03:04:27 +0000676 ND->getDeclContext()->isFileContext())
677 Out << 'L';
678
Anders Carlssonc4355b62009-10-07 01:45:02 +0000679 mangleSourceName(II);
680 break;
681 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000682
John McCall1dd73832010-02-04 01:42:13 +0000683 // Otherwise, an anonymous entity. We must have a declaration.
684 assert(ND && "mangling empty name without declaration");
685
686 if (const NamespaceDecl *NS = dyn_cast<NamespaceDecl>(ND)) {
687 if (NS->isAnonymousNamespace()) {
688 // This is how gcc mangles these names.
689 Out << "12_GLOBAL__N_1";
690 break;
691 }
692 }
693
Anders Carlsson6f7e2f42010-06-08 14:49:03 +0000694 if (const VarDecl *VD = dyn_cast<VarDecl>(ND)) {
695 // We must have an anonymous union or struct declaration.
696 const RecordDecl *RD =
697 cast<RecordDecl>(VD->getType()->getAs<RecordType>()->getDecl());
698
699 // Itanium C++ ABI 5.1.2:
700 //
701 // For the purposes of mangling, the name of an anonymous union is
702 // considered to be the name of the first named data member found by a
703 // pre-order, depth-first, declaration-order walk of the data members of
704 // the anonymous union. If there is no such data member (i.e., if all of
705 // the data members in the union are unnamed), then there is no way for
706 // a program to refer to the anonymous union, and there is therefore no
707 // need to mangle its name.
708 const FieldDecl *FD = FindFirstNamedDataMember(RD);
John McCall7121c8f2010-08-05 22:02:13 +0000709
710 // It's actually possible for various reasons for us to get here
711 // with an empty anonymous struct / union. Fortunately, it
712 // doesn't really matter what name we generate.
713 if (!FD) break;
Anders Carlsson6f7e2f42010-06-08 14:49:03 +0000714 assert(FD->getIdentifier() && "Data member name isn't an identifier!");
715
716 mangleSourceName(FD->getIdentifier());
717 break;
718 }
719
Anders Carlssonc4355b62009-10-07 01:45:02 +0000720 // We must have an anonymous struct.
721 const TagDecl *TD = cast<TagDecl>(ND);
722 if (const TypedefDecl *D = TD->getTypedefForAnonDecl()) {
723 assert(TD->getDeclContext() == D->getDeclContext() &&
724 "Typedef should not be in another decl context!");
725 assert(D->getDeclName().getAsIdentifierInfo() &&
726 "Typedef was not named!");
727 mangleSourceName(D->getDeclName().getAsIdentifierInfo());
728 break;
729 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000730
Anders Carlssonc4355b62009-10-07 01:45:02 +0000731 // Get a unique id for the anonymous struct.
732 uint64_t AnonStructId = Context.getAnonymousStructId(TD);
733
734 // Mangle it as a source name in the form
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000735 // [n] $_<id>
Anders Carlssonc4355b62009-10-07 01:45:02 +0000736 // where n is the length of the string.
737 llvm::SmallString<8> Str;
738 Str += "$_";
739 Str += llvm::utostr(AnonStructId);
740
741 Out << Str.size();
742 Out << Str.str();
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000743 break;
Anders Carlssonc4355b62009-10-07 01:45:02 +0000744 }
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000745
746 case DeclarationName::ObjCZeroArgSelector:
747 case DeclarationName::ObjCOneArgSelector:
748 case DeclarationName::ObjCMultiArgSelector:
749 assert(false && "Can't mangle Objective-C selector names here!");
750 break;
751
752 case DeclarationName::CXXConstructorName:
Anders Carlsson27ae5362009-04-17 01:58:57 +0000753 if (ND == Structor)
Mike Stump141c5af2009-09-02 00:25:38 +0000754 // If the named decl is the C++ constructor we're mangling, use the type
755 // we were given.
Anders Carlsson27ae5362009-04-17 01:58:57 +0000756 mangleCXXCtorType(static_cast<CXXCtorType>(StructorType));
Anders Carlsson3ac86b52009-04-15 05:36:58 +0000757 else
758 // Otherwise, use the complete constructor name. This is relevant if a
759 // class with a constructor is declared within a constructor.
760 mangleCXXCtorType(Ctor_Complete);
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000761 break;
762
763 case DeclarationName::CXXDestructorName:
Anders Carlsson27ae5362009-04-17 01:58:57 +0000764 if (ND == Structor)
Mike Stump141c5af2009-09-02 00:25:38 +0000765 // If the named decl is the C++ destructor we're mangling, use the type we
766 // were given.
Anders Carlsson27ae5362009-04-17 01:58:57 +0000767 mangleCXXDtorType(static_cast<CXXDtorType>(StructorType));
768 else
769 // Otherwise, use the complete destructor name. This is relevant if a
770 // class with a destructor is declared within a destructor.
771 mangleCXXDtorType(Dtor_Complete);
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000772 break;
773
774 case DeclarationName::CXXConversionFunctionName:
Mike Stump1eb44332009-09-09 15:08:12 +0000775 // <operator-name> ::= cv <type> # (cast)
Douglas Gregor219cc612009-02-13 01:28:03 +0000776 Out << "cv";
Anders Carlssonb5404912009-10-07 01:06:45 +0000777 mangleType(Context.getASTContext().getCanonicalType(Name.getCXXNameType()));
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000778 break;
779
Anders Carlsson8257d412009-12-22 06:36:32 +0000780 case DeclarationName::CXXOperatorName: {
John McCall1dd73832010-02-04 01:42:13 +0000781 unsigned Arity;
782 if (ND) {
783 Arity = cast<FunctionDecl>(ND)->getNumParams();
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000784
John McCall1dd73832010-02-04 01:42:13 +0000785 // If we have a C++ member function, we need to include the 'this' pointer.
786 // FIXME: This does not make sense for operators that are static, but their
787 // names stay the same regardless of the arity (operator new for instance).
788 if (isa<CXXMethodDecl>(ND))
789 Arity++;
790 } else
791 Arity = KnownArity;
792
Anders Carlsson8257d412009-12-22 06:36:32 +0000793 mangleOperatorName(Name.getCXXOverloadedOperator(), Arity);
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000794 break;
Anders Carlsson8257d412009-12-22 06:36:32 +0000795 }
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000796
Sean Hunt3e518bd2009-11-29 07:34:05 +0000797 case DeclarationName::CXXLiteralOperatorName:
Sean Hunt5dd6b392009-12-04 21:11:13 +0000798 // FIXME: This mangling is not yet official.
Sean Hunt2421f662009-12-04 21:01:37 +0000799 Out << "li";
Sean Hunt3e518bd2009-11-29 07:34:05 +0000800 mangleSourceName(Name.getCXXLiteralIdentifier());
801 break;
802
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000803 case DeclarationName::CXXUsingDirective:
804 assert(false && "Can't mangle a using directive name!");
Douglas Gregor219cc612009-02-13 01:28:03 +0000805 break;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000806 }
807}
808
809void CXXNameMangler::mangleSourceName(const IdentifierInfo *II) {
810 // <source-name> ::= <positive length number> <identifier>
811 // <number> ::= [n] <non-negative decimal integer>
812 // <identifier> ::= <unqualified source code identifier>
813 Out << II->getLength() << II->getName();
814}
815
Eli Friedman7facf842009-12-02 20:32:49 +0000816void CXXNameMangler::mangleNestedName(const NamedDecl *ND,
Fariborz Jahanian57058532010-03-03 19:41:08 +0000817 const DeclContext *DC,
818 bool NoFunction) {
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000819 // <nested-name> ::= N [<CV-qualifiers>] <prefix> <unqualified-name> E
820 // ::= N [<CV-qualifiers>] <template-prefix> <template-args> E
Anders Carlssond99edc42009-09-26 03:55:37 +0000821
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000822 Out << 'N';
823 if (const CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(ND))
John McCall0953e762009-09-24 19:53:00 +0000824 mangleQualifiers(Qualifiers::fromCVRMask(Method->getTypeQualifiers()));
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000825
Anders Carlsson2744a062009-09-18 19:00:18 +0000826 // Check if we have a template.
827 const TemplateArgumentList *TemplateArgs = 0;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000828 if (const TemplateDecl *TD = isTemplate(ND, TemplateArgs)) {
Anders Carlsson2744a062009-09-18 19:00:18 +0000829 mangleTemplatePrefix(TD);
Rafael Espindolad9800722010-03-11 14:07:00 +0000830 TemplateParameterList *TemplateParameters = TD->getTemplateParameters();
831 mangleTemplateArgs(*TemplateParameters, *TemplateArgs);
Fariborz Jahanian57058532010-03-03 19:41:08 +0000832 }
833 else {
834 manglePrefix(DC, NoFunction);
Anders Carlsson7482e242009-09-18 04:29:09 +0000835 mangleUnqualifiedName(ND);
836 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000837
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000838 Out << 'E';
839}
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000840void CXXNameMangler::mangleNestedName(const TemplateDecl *TD,
Anders Carlsson7624f212009-09-18 02:42:01 +0000841 const TemplateArgument *TemplateArgs,
842 unsigned NumTemplateArgs) {
Anders Carlssone45117b2009-09-27 19:53:49 +0000843 // <nested-name> ::= N [<CV-qualifiers>] <template-prefix> <template-args> E
844
Anders Carlsson7624f212009-09-18 02:42:01 +0000845 Out << 'N';
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000846
Anders Carlssone45117b2009-09-27 19:53:49 +0000847 mangleTemplatePrefix(TD);
Rafael Espindolad9800722010-03-11 14:07:00 +0000848 TemplateParameterList *TemplateParameters = TD->getTemplateParameters();
849 mangleTemplateArgs(*TemplateParameters, TemplateArgs, NumTemplateArgs);
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000850
Anders Carlsson7624f212009-09-18 02:42:01 +0000851 Out << 'E';
852}
853
Anders Carlsson1b42c792009-04-02 16:24:45 +0000854void CXXNameMangler::mangleLocalName(const NamedDecl *ND) {
855 // <local-name> := Z <function encoding> E <entity name> [<discriminator>]
856 // := Z <function encoding> E s [<discriminator>]
Mike Stump1eb44332009-09-09 15:08:12 +0000857 // <discriminator> := _ <non-negative number>
Fariborz Jahanian57058532010-03-03 19:41:08 +0000858 const DeclContext *DC = ND->getDeclContext();
Anders Carlsson1b42c792009-04-02 16:24:45 +0000859 Out << 'Z';
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000860
Charles Davis685b1d92010-05-26 18:25:27 +0000861 if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(DC)) {
862 mangleObjCMethodName(MD);
John McCall82b7d7b2010-10-18 21:28:44 +0000863 } else if (const CXXRecordDecl *RD = GetLocalClassDecl(ND)) {
864 mangleFunctionEncoding(cast<FunctionDecl>(RD->getDeclContext()));
Fariborz Jahanian57058532010-03-03 19:41:08 +0000865 Out << 'E';
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000866
John McCall82b7d7b2010-10-18 21:28:44 +0000867 // Mangle the name relative to the closest enclosing function.
868 if (ND == RD) // equality ok because RD derived from ND above
869 mangleUnqualifiedName(ND);
870 else
871 mangleNestedName(ND, DC, true /*NoFunction*/);
872
Fariborz Jahanian4819ac42010-03-04 01:02:03 +0000873 unsigned disc;
John McCall82b7d7b2010-10-18 21:28:44 +0000874 if (Context.getNextDiscriminator(RD, disc)) {
Fariborz Jahanian4819ac42010-03-04 01:02:03 +0000875 if (disc < 10)
876 Out << '_' << disc;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000877 else
Fariborz Jahanian4819ac42010-03-04 01:02:03 +0000878 Out << "__" << disc << '_';
879 }
Fariborz Jahanian57058532010-03-03 19:41:08 +0000880
881 return;
882 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000883 else
Fariborz Jahanian57058532010-03-03 19:41:08 +0000884 mangleFunctionEncoding(cast<FunctionDecl>(DC));
Anders Carlsson7b06f6c2009-12-10 03:14:39 +0000885
Anders Carlsson1b42c792009-04-02 16:24:45 +0000886 Out << 'E';
Eli Friedman6f9f25d2009-12-11 20:21:38 +0000887 mangleUnqualifiedName(ND);
Anders Carlsson1b42c792009-04-02 16:24:45 +0000888}
889
Fariborz Jahanian57058532010-03-03 19:41:08 +0000890void CXXNameMangler::manglePrefix(const DeclContext *DC, bool NoFunction) {
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000891 // <prefix> ::= <prefix> <unqualified-name>
892 // ::= <template-prefix> <template-args>
893 // ::= <template-param>
894 // ::= # empty
895 // ::= <substitution>
Anders Carlsson6862fc72009-09-17 04:16:28 +0000896
Anders Carlssonadd28822009-09-22 20:33:31 +0000897 while (isa<LinkageSpecDecl>(DC))
898 DC = DC->getParent();
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000899
Anders Carlsson9263e912009-09-18 18:39:58 +0000900 if (DC->isTranslationUnit())
901 return;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000902
Douglas Gregor35415f52010-05-25 17:04:15 +0000903 if (const BlockDecl *Block = dyn_cast<BlockDecl>(DC)) {
904 manglePrefix(DC->getParent(), NoFunction);
905 llvm::SmallString<64> Name;
Peter Collingbourne14110472011-01-13 18:57:25 +0000906 Context.mangleBlock(Block, Name);
Douglas Gregor35415f52010-05-25 17:04:15 +0000907 Out << Name.size() << Name;
908 return;
909 }
910
Anders Carlsson6862fc72009-09-17 04:16:28 +0000911 if (mangleSubstitution(cast<NamedDecl>(DC)))
912 return;
Anders Carlsson7482e242009-09-18 04:29:09 +0000913
Anders Carlsson2ee3fca2009-09-18 20:11:09 +0000914 // Check if we have a template.
915 const TemplateArgumentList *TemplateArgs = 0;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000916 if (const TemplateDecl *TD = isTemplate(cast<NamedDecl>(DC), TemplateArgs)) {
Anders Carlsson2ee3fca2009-09-18 20:11:09 +0000917 mangleTemplatePrefix(TD);
Rafael Espindolad9800722010-03-11 14:07:00 +0000918 TemplateParameterList *TemplateParameters = TD->getTemplateParameters();
919 mangleTemplateArgs(*TemplateParameters, *TemplateArgs);
Fariborz Jahanian57058532010-03-03 19:41:08 +0000920 }
Douglas Gregor35415f52010-05-25 17:04:15 +0000921 else if(NoFunction && (isa<FunctionDecl>(DC) || isa<ObjCMethodDecl>(DC)))
Fariborz Jahanian57058532010-03-03 19:41:08 +0000922 return;
Douglas Gregor35415f52010-05-25 17:04:15 +0000923 else if (const ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(DC))
924 mangleObjCMethodName(Method);
Fariborz Jahanian57058532010-03-03 19:41:08 +0000925 else {
926 manglePrefix(DC->getParent(), NoFunction);
Anders Carlsson2ee3fca2009-09-18 20:11:09 +0000927 mangleUnqualifiedName(cast<NamedDecl>(DC));
928 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000929
Anders Carlsson6862fc72009-09-17 04:16:28 +0000930 addSubstitution(cast<NamedDecl>(DC));
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000931}
932
Douglas Gregor20f0cc72010-04-23 03:10:43 +0000933void CXXNameMangler::mangleTemplatePrefix(TemplateName Template) {
934 // <template-prefix> ::= <prefix> <template unqualified-name>
935 // ::= <template-param>
936 // ::= <substitution>
937 if (TemplateDecl *TD = Template.getAsTemplateDecl())
938 return mangleTemplatePrefix(TD);
939
940 if (QualifiedTemplateName *Qualified = Template.getAsQualifiedTemplateName())
941 mangleUnresolvedScope(Qualified->getQualifier());
Sean Huntc3021132010-05-05 15:23:54 +0000942
Douglas Gregor20f0cc72010-04-23 03:10:43 +0000943 if (OverloadedTemplateStorage *Overloaded
944 = Template.getAsOverloadedTemplate()) {
Sean Huntc3021132010-05-05 15:23:54 +0000945 mangleUnqualifiedName(0, (*Overloaded->begin())->getDeclName(),
Douglas Gregor20f0cc72010-04-23 03:10:43 +0000946 UnknownArity);
947 return;
948 }
Sean Huntc3021132010-05-05 15:23:54 +0000949
Douglas Gregor20f0cc72010-04-23 03:10:43 +0000950 DependentTemplateName *Dependent = Template.getAsDependentTemplateName();
951 assert(Dependent && "Unknown template name kind?");
952 mangleUnresolvedScope(Dependent->getQualifier());
Douglas Gregor1e9268e2010-04-28 05:58:56 +0000953 mangleUnscopedTemplateName(Template);
Douglas Gregor20f0cc72010-04-23 03:10:43 +0000954}
955
Anders Carlsson0fa6df42009-09-26 19:45:45 +0000956void CXXNameMangler::mangleTemplatePrefix(const TemplateDecl *ND) {
Anders Carlsson7482e242009-09-18 04:29:09 +0000957 // <template-prefix> ::= <prefix> <template unqualified-name>
958 // ::= <template-param>
959 // ::= <substitution>
Douglas Gregor32fb4e12010-02-05 20:45:00 +0000960 // <template-template-param> ::= <template-param>
961 // <substitution>
Anders Carlsson7482e242009-09-18 04:29:09 +0000962
Anders Carlssonaeb85372009-09-26 22:18:22 +0000963 if (mangleSubstitution(ND))
964 return;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000965
Douglas Gregor32fb4e12010-02-05 20:45:00 +0000966 // <template-template-param> ::= <template-param>
967 if (const TemplateTemplateParmDecl *TTP
968 = dyn_cast<TemplateTemplateParmDecl>(ND)) {
969 mangleTemplateParameter(TTP->getIndex());
970 return;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000971 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000972
Anders Carlssonaa73ab12009-09-18 18:47:07 +0000973 manglePrefix(ND->getDeclContext());
Anders Carlsson1668f202009-09-26 20:13:56 +0000974 mangleUnqualifiedName(ND->getTemplatedDecl());
Anders Carlssonaeb85372009-09-26 22:18:22 +0000975 addSubstitution(ND);
Anders Carlsson7482e242009-09-18 04:29:09 +0000976}
977
John McCallb6f532e2010-07-14 06:43:17 +0000978/// Mangles a template name under the production <type>. Required for
979/// template template arguments.
980/// <type> ::= <class-enum-type>
981/// ::= <template-param>
982/// ::= <substitution>
983void CXXNameMangler::mangleType(TemplateName TN) {
984 if (mangleSubstitution(TN))
985 return;
986
987 TemplateDecl *TD = 0;
988
989 switch (TN.getKind()) {
990 case TemplateName::QualifiedTemplate:
991 TD = TN.getAsQualifiedTemplateName()->getTemplateDecl();
992 goto HaveDecl;
993
994 case TemplateName::Template:
995 TD = TN.getAsTemplateDecl();
996 goto HaveDecl;
997
998 HaveDecl:
999 if (isa<TemplateTemplateParmDecl>(TD))
1000 mangleTemplateParameter(cast<TemplateTemplateParmDecl>(TD)->getIndex());
1001 else
1002 mangleName(TD);
1003 break;
1004
1005 case TemplateName::OverloadedTemplate:
1006 llvm_unreachable("can't mangle an overloaded template name as a <type>");
1007 break;
1008
1009 case TemplateName::DependentTemplate: {
1010 const DependentTemplateName *Dependent = TN.getAsDependentTemplateName();
1011 assert(Dependent->isIdentifier());
1012
1013 // <class-enum-type> ::= <name>
1014 // <name> ::= <nested-name>
1015 mangleUnresolvedScope(Dependent->getQualifier());
1016 mangleSourceName(Dependent->getIdentifier());
1017 break;
1018 }
1019
1020 }
1021
1022 addSubstitution(TN);
1023}
1024
Mike Stump1eb44332009-09-09 15:08:12 +00001025void
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001026CXXNameMangler::mangleOperatorName(OverloadedOperatorKind OO, unsigned Arity) {
1027 switch (OO) {
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001028 // <operator-name> ::= nw # new
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001029 case OO_New: Out << "nw"; break;
1030 // ::= na # new[]
1031 case OO_Array_New: Out << "na"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001032 // ::= dl # delete
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001033 case OO_Delete: Out << "dl"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001034 // ::= da # delete[]
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001035 case OO_Array_Delete: Out << "da"; break;
1036 // ::= ps # + (unary)
John McCall5e1e89b2010-08-18 19:18:59 +00001037 // ::= pl # + (binary or unknown)
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001038 case OO_Plus:
Anders Carlsson8257d412009-12-22 06:36:32 +00001039 Out << (Arity == 1? "ps" : "pl"); break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001040 // ::= ng # - (unary)
John McCall5e1e89b2010-08-18 19:18:59 +00001041 // ::= mi # - (binary or unknown)
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001042 case OO_Minus:
Anders Carlsson8257d412009-12-22 06:36:32 +00001043 Out << (Arity == 1? "ng" : "mi"); break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001044 // ::= ad # & (unary)
John McCall5e1e89b2010-08-18 19:18:59 +00001045 // ::= an # & (binary or unknown)
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001046 case OO_Amp:
Anders Carlsson8257d412009-12-22 06:36:32 +00001047 Out << (Arity == 1? "ad" : "an"); break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001048 // ::= de # * (unary)
John McCall5e1e89b2010-08-18 19:18:59 +00001049 // ::= ml # * (binary or unknown)
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001050 case OO_Star:
John McCall5e1e89b2010-08-18 19:18:59 +00001051 // Use binary when unknown.
Anders Carlsson8257d412009-12-22 06:36:32 +00001052 Out << (Arity == 1? "de" : "ml"); break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001053 // ::= co # ~
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001054 case OO_Tilde: Out << "co"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001055 // ::= dv # /
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001056 case OO_Slash: Out << "dv"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001057 // ::= rm # %
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001058 case OO_Percent: Out << "rm"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001059 // ::= or # |
1060 case OO_Pipe: Out << "or"; break;
1061 // ::= eo # ^
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001062 case OO_Caret: Out << "eo"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001063 // ::= aS # =
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001064 case OO_Equal: Out << "aS"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001065 // ::= pL # +=
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001066 case OO_PlusEqual: Out << "pL"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001067 // ::= mI # -=
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001068 case OO_MinusEqual: Out << "mI"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001069 // ::= mL # *=
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001070 case OO_StarEqual: Out << "mL"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001071 // ::= dV # /=
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001072 case OO_SlashEqual: Out << "dV"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001073 // ::= rM # %=
1074 case OO_PercentEqual: Out << "rM"; break;
1075 // ::= aN # &=
1076 case OO_AmpEqual: Out << "aN"; break;
1077 // ::= oR # |=
1078 case OO_PipeEqual: Out << "oR"; break;
1079 // ::= eO # ^=
1080 case OO_CaretEqual: Out << "eO"; break;
1081 // ::= ls # <<
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001082 case OO_LessLess: Out << "ls"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001083 // ::= rs # >>
1084 case OO_GreaterGreater: Out << "rs"; break;
1085 // ::= lS # <<=
1086 case OO_LessLessEqual: Out << "lS"; break;
1087 // ::= rS # >>=
1088 case OO_GreaterGreaterEqual: Out << "rS"; break;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001089 // ::= eq # ==
1090 case OO_EqualEqual: Out << "eq"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001091 // ::= ne # !=
1092 case OO_ExclaimEqual: Out << "ne"; break;
1093 // ::= lt # <
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001094 case OO_Less: Out << "lt"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001095 // ::= gt # >
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001096 case OO_Greater: Out << "gt"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001097 // ::= le # <=
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001098 case OO_LessEqual: Out << "le"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001099 // ::= ge # >=
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001100 case OO_GreaterEqual: Out << "ge"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001101 // ::= nt # !
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001102 case OO_Exclaim: Out << "nt"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001103 // ::= aa # &&
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001104 case OO_AmpAmp: Out << "aa"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001105 // ::= oo # ||
1106 case OO_PipePipe: Out << "oo"; break;
1107 // ::= pp # ++
1108 case OO_PlusPlus: Out << "pp"; break;
1109 // ::= mm # --
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001110 case OO_MinusMinus: Out << "mm"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001111 // ::= cm # ,
1112 case OO_Comma: Out << "cm"; break;
1113 // ::= pm # ->*
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001114 case OO_ArrowStar: Out << "pm"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001115 // ::= pt # ->
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001116 case OO_Arrow: Out << "pt"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001117 // ::= cl # ()
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001118 case OO_Call: Out << "cl"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001119 // ::= ix # []
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001120 case OO_Subscript: Out << "ix"; break;
Anders Carlssone170ba72009-12-14 01:45:37 +00001121
1122 // ::= qu # ?
1123 // The conditional operator can't be overloaded, but we still handle it when
1124 // mangling expressions.
1125 case OO_Conditional: Out << "qu"; break;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001126
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001127 case OO_None:
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001128 case NUM_OVERLOADED_OPERATORS:
Mike Stump1eb44332009-09-09 15:08:12 +00001129 assert(false && "Not an overloaded operator");
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001130 break;
1131 }
1132}
1133
John McCall0953e762009-09-24 19:53:00 +00001134void CXXNameMangler::mangleQualifiers(Qualifiers Quals) {
Mike Stump1eb44332009-09-09 15:08:12 +00001135 // <CV-qualifiers> ::= [r] [V] [K] # restrict (C99), volatile, const
John McCall0953e762009-09-24 19:53:00 +00001136 if (Quals.hasRestrict())
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001137 Out << 'r';
John McCall0953e762009-09-24 19:53:00 +00001138 if (Quals.hasVolatile())
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001139 Out << 'V';
John McCall0953e762009-09-24 19:53:00 +00001140 if (Quals.hasConst())
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001141 Out << 'K';
John McCall0953e762009-09-24 19:53:00 +00001142
Douglas Gregor56079f72010-06-14 23:15:08 +00001143 if (Quals.hasAddressSpace()) {
1144 // Extension:
1145 //
1146 // <type> ::= U <address-space-number>
1147 //
1148 // where <address-space-number> is a source name consisting of 'AS'
1149 // followed by the address space <number>.
1150 llvm::SmallString<64> ASString;
1151 ASString = "AS" + llvm::utostr_32(Quals.getAddressSpace());
1152 Out << 'U' << ASString.size() << ASString;
1153 }
1154
John McCall0953e762009-09-24 19:53:00 +00001155 // FIXME: For now, just drop all extension qualifiers on the floor.
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001156}
1157
Anders Carlsson7b06f6c2009-12-10 03:14:39 +00001158void CXXNameMangler::mangleObjCMethodName(const ObjCMethodDecl *MD) {
Charles Davis685b1d92010-05-26 18:25:27 +00001159 llvm::SmallString<64> Buffer;
Peter Collingbourne14110472011-01-13 18:57:25 +00001160 Context.mangleObjCMethodName(MD, Buffer);
Charles Davis685b1d92010-05-26 18:25:27 +00001161 Out << Buffer;
Anders Carlsson7b06f6c2009-12-10 03:14:39 +00001162}
1163
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001164void CXXNameMangler::mangleType(QualType T) {
Anders Carlsson4843e582009-03-10 17:07:44 +00001165 // Only operate on the canonical type!
Anders Carlssonb5404912009-10-07 01:06:45 +00001166 T = Context.getASTContext().getCanonicalType(T);
Anders Carlsson4843e582009-03-10 17:07:44 +00001167
Douglas Gregora4923eb2009-11-16 21:35:15 +00001168 bool IsSubstitutable = T.hasLocalQualifiers() || !isa<BuiltinType>(T);
Anders Carlsson76967372009-09-17 00:43:46 +00001169 if (IsSubstitutable && mangleSubstitution(T))
1170 return;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001171
Douglas Gregora4923eb2009-11-16 21:35:15 +00001172 if (Qualifiers Quals = T.getLocalQualifiers()) {
John McCall0953e762009-09-24 19:53:00 +00001173 mangleQualifiers(Quals);
1174 // Recurse: even if the qualified type isn't yet substitutable,
1175 // the unqualified type might be.
Douglas Gregora4923eb2009-11-16 21:35:15 +00001176 mangleType(T.getLocalUnqualifiedType());
Anders Carlsson76967372009-09-17 00:43:46 +00001177 } else {
1178 switch (T->getTypeClass()) {
John McCallefe6aee2009-09-05 07:56:18 +00001179#define ABSTRACT_TYPE(CLASS, PARENT)
1180#define NON_CANONICAL_TYPE(CLASS, PARENT) \
Anders Carlsson76967372009-09-17 00:43:46 +00001181 case Type::CLASS: \
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +00001182 llvm_unreachable("can't mangle non-canonical type " #CLASS "Type"); \
Anders Carlsson76967372009-09-17 00:43:46 +00001183 return;
John McCallefe6aee2009-09-05 07:56:18 +00001184#define TYPE(CLASS, PARENT) \
Anders Carlsson76967372009-09-17 00:43:46 +00001185 case Type::CLASS: \
John McCall0953e762009-09-24 19:53:00 +00001186 mangleType(static_cast<const CLASS##Type*>(T.getTypePtr())); \
Anders Carlsson76967372009-09-17 00:43:46 +00001187 break;
John McCallefe6aee2009-09-05 07:56:18 +00001188#include "clang/AST/TypeNodes.def"
Anders Carlsson76967372009-09-17 00:43:46 +00001189 }
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001190 }
Anders Carlsson76967372009-09-17 00:43:46 +00001191
1192 // Add the substitution.
1193 if (IsSubstitutable)
1194 addSubstitution(T);
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001195}
1196
Douglas Gregor1b12a3b2010-05-26 05:11:13 +00001197void CXXNameMangler::mangleNameOrStandardSubstitution(const NamedDecl *ND) {
1198 if (!mangleStandardSubstitution(ND))
1199 mangleName(ND);
1200}
1201
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001202void CXXNameMangler::mangleType(const BuiltinType *T) {
John McCallefe6aee2009-09-05 07:56:18 +00001203 // <type> ::= <builtin-type>
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001204 // <builtin-type> ::= v # void
1205 // ::= w # wchar_t
1206 // ::= b # bool
1207 // ::= c # char
1208 // ::= a # signed char
1209 // ::= h # unsigned char
1210 // ::= s # short
1211 // ::= t # unsigned short
1212 // ::= i # int
1213 // ::= j # unsigned int
1214 // ::= l # long
1215 // ::= m # unsigned long
1216 // ::= x # long long, __int64
1217 // ::= y # unsigned long long, __int64
1218 // ::= n # __int128
1219 // UNSUPPORTED: ::= o # unsigned __int128
1220 // ::= f # float
1221 // ::= d # double
1222 // ::= e # long double, __float80
1223 // UNSUPPORTED: ::= g # __float128
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001224 // UNSUPPORTED: ::= Dd # IEEE 754r decimal floating point (64 bits)
1225 // UNSUPPORTED: ::= De # IEEE 754r decimal floating point (128 bits)
1226 // UNSUPPORTED: ::= Df # IEEE 754r decimal floating point (32 bits)
1227 // UNSUPPORTED: ::= Dh # IEEE 754r half-precision floating point (16 bits)
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001228 // ::= Di # char32_t
1229 // ::= Ds # char16_t
Anders Carlssone2923682010-11-04 04:31:32 +00001230 // ::= Dn # std::nullptr_t (i.e., decltype(nullptr))
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001231 // ::= u <source-name> # vendor extended type
1232 switch (T->getKind()) {
1233 case BuiltinType::Void: Out << 'v'; break;
1234 case BuiltinType::Bool: Out << 'b'; break;
1235 case BuiltinType::Char_U: case BuiltinType::Char_S: Out << 'c'; break;
1236 case BuiltinType::UChar: Out << 'h'; break;
1237 case BuiltinType::UShort: Out << 't'; break;
1238 case BuiltinType::UInt: Out << 'j'; break;
1239 case BuiltinType::ULong: Out << 'm'; break;
1240 case BuiltinType::ULongLong: Out << 'y'; break;
Chris Lattner2df9ced2009-04-30 02:43:43 +00001241 case BuiltinType::UInt128: Out << 'o'; break;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001242 case BuiltinType::SChar: Out << 'a'; break;
Chris Lattner3f59c972010-12-25 23:25:43 +00001243 case BuiltinType::WChar_S:
1244 case BuiltinType::WChar_U: Out << 'w'; break;
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001245 case BuiltinType::Char16: Out << "Ds"; break;
1246 case BuiltinType::Char32: Out << "Di"; break;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001247 case BuiltinType::Short: Out << 's'; break;
1248 case BuiltinType::Int: Out << 'i'; break;
1249 case BuiltinType::Long: Out << 'l'; break;
1250 case BuiltinType::LongLong: Out << 'x'; break;
Chris Lattner2df9ced2009-04-30 02:43:43 +00001251 case BuiltinType::Int128: Out << 'n'; break;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001252 case BuiltinType::Float: Out << 'f'; break;
1253 case BuiltinType::Double: Out << 'd'; break;
1254 case BuiltinType::LongDouble: Out << 'e'; break;
Anders Carlssone2923682010-11-04 04:31:32 +00001255 case BuiltinType::NullPtr: Out << "Dn"; break;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001256
1257 case BuiltinType::Overload:
1258 case BuiltinType::Dependent:
Mike Stump1eb44332009-09-09 15:08:12 +00001259 assert(false &&
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001260 "Overloaded and dependent types shouldn't get to name mangling");
1261 break;
Anders Carlssone89d1592009-06-26 18:41:36 +00001262 case BuiltinType::UndeducedAuto:
1263 assert(0 && "Should not see undeduced auto here");
1264 break;
Steve Naroff9533a7f2009-07-22 17:14:51 +00001265 case BuiltinType::ObjCId: Out << "11objc_object"; break;
1266 case BuiltinType::ObjCClass: Out << "10objc_class"; break;
Fariborz Jahanian13dcd002009-11-21 19:53:08 +00001267 case BuiltinType::ObjCSel: Out << "13objc_selector"; break;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001268 }
1269}
1270
John McCallefe6aee2009-09-05 07:56:18 +00001271// <type> ::= <function-type>
1272// <function-type> ::= F [Y] <bare-function-type> E
1273void CXXNameMangler::mangleType(const FunctionProtoType *T) {
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001274 Out << 'F';
Mike Stumpf5408fe2009-05-16 07:57:57 +00001275 // FIXME: We don't have enough information in the AST to produce the 'Y'
1276 // encoding for extern "C" function types.
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001277 mangleBareFunctionType(T, /*MangleReturnType=*/true);
1278 Out << 'E';
1279}
John McCallefe6aee2009-09-05 07:56:18 +00001280void CXXNameMangler::mangleType(const FunctionNoProtoType *T) {
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +00001281 llvm_unreachable("Can't mangle K&R function prototypes");
John McCallefe6aee2009-09-05 07:56:18 +00001282}
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001283void CXXNameMangler::mangleBareFunctionType(const FunctionType *T,
1284 bool MangleReturnType) {
John McCallefe6aee2009-09-05 07:56:18 +00001285 // We should never be mangling something without a prototype.
1286 const FunctionProtoType *Proto = cast<FunctionProtoType>(T);
1287
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001288 // <bare-function-type> ::= <signature type>+
1289 if (MangleReturnType)
John McCallefe6aee2009-09-05 07:56:18 +00001290 mangleType(Proto->getResultType());
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001291
Anders Carlsson93296682010-06-02 04:40:13 +00001292 if (Proto->getNumArgs() == 0 && !Proto->isVariadic()) {
Eli Friedmana7e68452010-08-22 01:00:03 +00001293 // <builtin-type> ::= v # void
Anders Carlssonc6c91bc2009-04-01 00:15:23 +00001294 Out << 'v';
1295 return;
1296 }
Mike Stump1eb44332009-09-09 15:08:12 +00001297
Douglas Gregor72564e72009-02-26 23:50:07 +00001298 for (FunctionProtoType::arg_type_iterator Arg = Proto->arg_type_begin(),
Mike Stump1eb44332009-09-09 15:08:12 +00001299 ArgEnd = Proto->arg_type_end();
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001300 Arg != ArgEnd; ++Arg)
1301 mangleType(*Arg);
Douglas Gregor219cc612009-02-13 01:28:03 +00001302
1303 // <builtin-type> ::= z # ellipsis
1304 if (Proto->isVariadic())
1305 Out << 'z';
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001306}
1307
John McCallefe6aee2009-09-05 07:56:18 +00001308// <type> ::= <class-enum-type>
Mike Stump1eb44332009-09-09 15:08:12 +00001309// <class-enum-type> ::= <name>
John McCalled976492009-12-04 22:46:56 +00001310void CXXNameMangler::mangleType(const UnresolvedUsingType *T) {
1311 mangleName(T->getDecl());
1312}
1313
1314// <type> ::= <class-enum-type>
1315// <class-enum-type> ::= <name>
John McCallefe6aee2009-09-05 07:56:18 +00001316void CXXNameMangler::mangleType(const EnumType *T) {
1317 mangleType(static_cast<const TagType*>(T));
1318}
1319void CXXNameMangler::mangleType(const RecordType *T) {
1320 mangleType(static_cast<const TagType*>(T));
1321}
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001322void CXXNameMangler::mangleType(const TagType *T) {
Eli Friedmanecb7e932009-12-11 18:00:57 +00001323 mangleName(T->getDecl());
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001324}
1325
John McCallefe6aee2009-09-05 07:56:18 +00001326// <type> ::= <array-type>
1327// <array-type> ::= A <positive dimension number> _ <element type>
1328// ::= A [<dimension expression>] _ <element type>
1329void CXXNameMangler::mangleType(const ConstantArrayType *T) {
1330 Out << 'A' << T->getSize() << '_';
1331 mangleType(T->getElementType());
1332}
1333void CXXNameMangler::mangleType(const VariableArrayType *T) {
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001334 Out << 'A';
Fariborz Jahanian7281d1f2010-11-02 16:54:00 +00001335 // decayed vla types (size 0) will just be skipped.
1336 if (T->getSizeExpr())
1337 mangleExpression(T->getSizeExpr());
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001338 Out << '_';
1339 mangleType(T->getElementType());
1340}
John McCallefe6aee2009-09-05 07:56:18 +00001341void CXXNameMangler::mangleType(const DependentSizedArrayType *T) {
1342 Out << 'A';
1343 mangleExpression(T->getSizeExpr());
1344 Out << '_';
1345 mangleType(T->getElementType());
1346}
1347void CXXNameMangler::mangleType(const IncompleteArrayType *T) {
Nick Lewycky271b6652010-09-05 03:40:33 +00001348 Out << "A_";
John McCallefe6aee2009-09-05 07:56:18 +00001349 mangleType(T->getElementType());
1350}
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001351
John McCallefe6aee2009-09-05 07:56:18 +00001352// <type> ::= <pointer-to-member-type>
1353// <pointer-to-member-type> ::= M <class type> <member type>
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001354void CXXNameMangler::mangleType(const MemberPointerType *T) {
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001355 Out << 'M';
1356 mangleType(QualType(T->getClass(), 0));
Anders Carlsson0e650012009-05-17 17:41:20 +00001357 QualType PointeeType = T->getPointeeType();
1358 if (const FunctionProtoType *FPT = dyn_cast<FunctionProtoType>(PointeeType)) {
John McCall0953e762009-09-24 19:53:00 +00001359 mangleQualifiers(Qualifiers::fromCVRMask(FPT->getTypeQuals()));
Anders Carlsson0e650012009-05-17 17:41:20 +00001360 mangleType(FPT);
Anders Carlsson9d85b722010-06-02 04:29:50 +00001361
1362 // Itanium C++ ABI 5.1.8:
1363 //
1364 // The type of a non-static member function is considered to be different,
1365 // for the purposes of substitution, from the type of a namespace-scope or
1366 // static member function whose type appears similar. The types of two
1367 // non-static member functions are considered to be different, for the
1368 // purposes of substitution, if the functions are members of different
1369 // classes. In other words, for the purposes of substitution, the class of
1370 // which the function is a member is considered part of the type of
1371 // function.
1372
1373 // We increment the SeqID here to emulate adding an entry to the
1374 // substitution table. We can't actually add it because we don't want this
1375 // particular function type to be substituted.
1376 ++SeqID;
Mike Stump1eb44332009-09-09 15:08:12 +00001377 } else
Anders Carlsson0e650012009-05-17 17:41:20 +00001378 mangleType(PointeeType);
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001379}
1380
John McCallefe6aee2009-09-05 07:56:18 +00001381// <type> ::= <template-param>
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001382void CXXNameMangler::mangleType(const TemplateTypeParmType *T) {
Anders Carlsson0ccdf8d2009-09-27 00:38:53 +00001383 mangleTemplateParameter(T->getIndex());
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001384}
1385
Douglas Gregorc3069d62011-01-14 02:55:32 +00001386// <type> ::= <template-param>
1387void CXXNameMangler::mangleType(const SubstTemplateTypeParmPackType *T) {
1388 mangleTemplateParameter(T->getReplacedParameter()->getIndex());
1389}
1390
John McCallefe6aee2009-09-05 07:56:18 +00001391// <type> ::= P <type> # pointer-to
1392void CXXNameMangler::mangleType(const PointerType *T) {
1393 Out << 'P';
1394 mangleType(T->getPointeeType());
1395}
1396void CXXNameMangler::mangleType(const ObjCObjectPointerType *T) {
1397 Out << 'P';
1398 mangleType(T->getPointeeType());
1399}
1400
1401// <type> ::= R <type> # reference-to
1402void CXXNameMangler::mangleType(const LValueReferenceType *T) {
1403 Out << 'R';
1404 mangleType(T->getPointeeType());
1405}
1406
1407// <type> ::= O <type> # rvalue reference-to (C++0x)
1408void CXXNameMangler::mangleType(const RValueReferenceType *T) {
1409 Out << 'O';
1410 mangleType(T->getPointeeType());
1411}
1412
1413// <type> ::= C <type> # complex pair (C 2000)
1414void CXXNameMangler::mangleType(const ComplexType *T) {
1415 Out << 'C';
1416 mangleType(T->getElementType());
1417}
1418
Bob Wilsonc7df92d2010-11-12 17:24:43 +00001419// ARM's ABI for Neon vector types specifies that they should be mangled as
Bob Wilson57147a82010-11-16 00:32:18 +00001420// if they are structs (to match ARM's initial implementation). The
1421// vector type must be one of the special types predefined by ARM.
1422void CXXNameMangler::mangleNeonVectorType(const VectorType *T) {
Bob Wilsonc7df92d2010-11-12 17:24:43 +00001423 QualType EltType = T->getElementType();
Bob Wilson57147a82010-11-16 00:32:18 +00001424 assert(EltType->isBuiltinType() && "Neon vector element not a BuiltinType");
Bob Wilsonc7df92d2010-11-12 17:24:43 +00001425 const char *EltName = 0;
Bob Wilson491328c2010-11-12 17:24:46 +00001426 if (T->getVectorKind() == VectorType::NeonPolyVector) {
1427 switch (cast<BuiltinType>(EltType)->getKind()) {
Bob Wilson4cfaa5d2010-11-12 17:24:49 +00001428 case BuiltinType::SChar: EltName = "poly8_t"; break;
1429 case BuiltinType::Short: EltName = "poly16_t"; break;
Bob Wilson57147a82010-11-16 00:32:18 +00001430 default: llvm_unreachable("unexpected Neon polynomial vector element type");
Bob Wilson491328c2010-11-12 17:24:46 +00001431 }
1432 } else {
1433 switch (cast<BuiltinType>(EltType)->getKind()) {
Bob Wilson4cfaa5d2010-11-12 17:24:49 +00001434 case BuiltinType::SChar: EltName = "int8_t"; break;
1435 case BuiltinType::UChar: EltName = "uint8_t"; break;
1436 case BuiltinType::Short: EltName = "int16_t"; break;
1437 case BuiltinType::UShort: EltName = "uint16_t"; break;
1438 case BuiltinType::Int: EltName = "int32_t"; break;
1439 case BuiltinType::UInt: EltName = "uint32_t"; break;
1440 case BuiltinType::LongLong: EltName = "int64_t"; break;
1441 case BuiltinType::ULongLong: EltName = "uint64_t"; break;
1442 case BuiltinType::Float: EltName = "float32_t"; break;
Bob Wilson57147a82010-11-16 00:32:18 +00001443 default: llvm_unreachable("unexpected Neon vector element type");
Bob Wilson491328c2010-11-12 17:24:46 +00001444 }
Bob Wilsonc7df92d2010-11-12 17:24:43 +00001445 }
1446 const char *BaseName = 0;
Bob Wilson4cfaa5d2010-11-12 17:24:49 +00001447 unsigned BitSize = (T->getNumElements() *
Bob Wilson3a723022010-11-16 00:32:12 +00001448 getASTContext().getTypeSize(EltType));
Bob Wilsonc7df92d2010-11-12 17:24:43 +00001449 if (BitSize == 64)
1450 BaseName = "__simd64_";
Bob Wilson57147a82010-11-16 00:32:18 +00001451 else {
1452 assert(BitSize == 128 && "Neon vector type not 64 or 128 bits");
Bob Wilsonc7df92d2010-11-12 17:24:43 +00001453 BaseName = "__simd128_";
Bob Wilson57147a82010-11-16 00:32:18 +00001454 }
Bob Wilsonc7df92d2010-11-12 17:24:43 +00001455 Out << strlen(BaseName) + strlen(EltName);
1456 Out << BaseName << EltName;
Bob Wilsonc7df92d2010-11-12 17:24:43 +00001457}
1458
John McCallefe6aee2009-09-05 07:56:18 +00001459// GNU extension: vector types
Chris Lattner788b0fd2010-06-23 06:00:24 +00001460// <type> ::= <vector-type>
1461// <vector-type> ::= Dv <positive dimension number> _
1462// <extended element type>
1463// ::= Dv [<dimension expression>] _ <element type>
1464// <extended element type> ::= <element type>
1465// ::= p # AltiVec vector pixel
John McCallefe6aee2009-09-05 07:56:18 +00001466void CXXNameMangler::mangleType(const VectorType *T) {
Bob Wilson491328c2010-11-12 17:24:46 +00001467 if ((T->getVectorKind() == VectorType::NeonVector ||
Bob Wilson57147a82010-11-16 00:32:18 +00001468 T->getVectorKind() == VectorType::NeonPolyVector)) {
1469 mangleNeonVectorType(T);
Bob Wilsonc7df92d2010-11-12 17:24:43 +00001470 return;
Bob Wilson57147a82010-11-16 00:32:18 +00001471 }
Nick Lewycky0e5f0672010-03-26 07:18:04 +00001472 Out << "Dv" << T->getNumElements() << '_';
Bob Wilsone86d78c2010-11-10 21:56:12 +00001473 if (T->getVectorKind() == VectorType::AltiVecPixel)
Chris Lattner788b0fd2010-06-23 06:00:24 +00001474 Out << 'p';
Bob Wilsone86d78c2010-11-10 21:56:12 +00001475 else if (T->getVectorKind() == VectorType::AltiVecBool)
Chris Lattner788b0fd2010-06-23 06:00:24 +00001476 Out << 'b';
1477 else
1478 mangleType(T->getElementType());
John McCallefe6aee2009-09-05 07:56:18 +00001479}
1480void CXXNameMangler::mangleType(const ExtVectorType *T) {
1481 mangleType(static_cast<const VectorType*>(T));
1482}
1483void CXXNameMangler::mangleType(const DependentSizedExtVectorType *T) {
Nick Lewycky0e5f0672010-03-26 07:18:04 +00001484 Out << "Dv";
1485 mangleExpression(T->getSizeExpr());
1486 Out << '_';
John McCallefe6aee2009-09-05 07:56:18 +00001487 mangleType(T->getElementType());
1488}
1489
Douglas Gregor7536dd52010-12-20 02:24:11 +00001490void CXXNameMangler::mangleType(const PackExpansionType *T) {
Douglas Gregor4fc48662011-01-13 16:39:34 +00001491 // <type> ::= Dp <type> # pack expansion (C++0x)
Douglas Gregor255c2692011-01-13 17:44:36 +00001492 Out << "Dp";
Douglas Gregor7536dd52010-12-20 02:24:11 +00001493 mangleType(T->getPattern());
1494}
1495
Anders Carlssona40c5e42009-03-07 22:03:21 +00001496void CXXNameMangler::mangleType(const ObjCInterfaceType *T) {
1497 mangleSourceName(T->getDecl()->getIdentifier());
1498}
1499
John McCallc12c5bb2010-05-15 11:32:37 +00001500void CXXNameMangler::mangleType(const ObjCObjectType *T) {
John McCallc00c1f62010-05-15 17:06:29 +00001501 // We don't allow overloading by different protocol qualification,
1502 // so mangling them isn't necessary.
John McCallc12c5bb2010-05-15 11:32:37 +00001503 mangleType(T->getBaseType());
1504}
1505
John McCallefe6aee2009-09-05 07:56:18 +00001506void CXXNameMangler::mangleType(const BlockPointerType *T) {
Anders Carlssonf28c6872009-12-23 22:31:44 +00001507 Out << "U13block_pointer";
1508 mangleType(T->getPointeeType());
John McCallefe6aee2009-09-05 07:56:18 +00001509}
1510
John McCall31f17ec2010-04-27 00:57:59 +00001511void CXXNameMangler::mangleType(const InjectedClassNameType *T) {
1512 // Mangle injected class name types as if the user had written the
1513 // specialization out fully. It may not actually be possible to see
1514 // this mangling, though.
1515 mangleType(T->getInjectedSpecializationType());
1516}
1517
John McCallefe6aee2009-09-05 07:56:18 +00001518void CXXNameMangler::mangleType(const TemplateSpecializationType *T) {
Douglas Gregor1e9268e2010-04-28 05:58:56 +00001519 if (TemplateDecl *TD = T->getTemplateName().getAsTemplateDecl()) {
1520 mangleName(TD, T->getArgs(), T->getNumArgs());
1521 } else {
1522 if (mangleSubstitution(QualType(T, 0)))
1523 return;
Sean Huntc3021132010-05-05 15:23:54 +00001524
Douglas Gregor1e9268e2010-04-28 05:58:56 +00001525 mangleTemplatePrefix(T->getTemplateName());
Sean Huntc3021132010-05-05 15:23:54 +00001526
Douglas Gregor1e9268e2010-04-28 05:58:56 +00001527 // FIXME: GCC does not appear to mangle the template arguments when
1528 // the template in question is a dependent template name. Should we
1529 // emulate that badness?
1530 mangleTemplateArgs(T->getTemplateName(), T->getArgs(), T->getNumArgs());
1531 addSubstitution(QualType(T, 0));
1532 }
John McCallefe6aee2009-09-05 07:56:18 +00001533}
1534
Douglas Gregor4714c122010-03-31 17:34:00 +00001535void CXXNameMangler::mangleType(const DependentNameType *T) {
Anders Carlssonae352482009-09-26 02:26:02 +00001536 // Typename types are always nested
1537 Out << 'N';
John McCall33500952010-06-11 00:33:02 +00001538 mangleUnresolvedScope(T->getQualifier());
1539 mangleSourceName(T->getIdentifier());
1540 Out << 'E';
1541}
John McCall6ab30e02010-06-09 07:26:17 +00001542
John McCall33500952010-06-11 00:33:02 +00001543void CXXNameMangler::mangleType(const DependentTemplateSpecializationType *T) {
1544 // Dependently-scoped template types are always nested
1545 Out << 'N';
1546
1547 // TODO: avoid making this TemplateName.
1548 TemplateName Prefix =
1549 getASTContext().getDependentTemplateName(T->getQualifier(),
1550 T->getIdentifier());
1551 mangleTemplatePrefix(Prefix);
1552
1553 // FIXME: GCC does not appear to mangle the template arguments when
1554 // the template in question is a dependent template name. Should we
1555 // emulate that badness?
1556 mangleTemplateArgs(Prefix, T->getArgs(), T->getNumArgs());
Anders Carlssonae352482009-09-26 02:26:02 +00001557 Out << 'E';
John McCallefe6aee2009-09-05 07:56:18 +00001558}
1559
John McCallad5e7382010-03-01 23:49:17 +00001560void CXXNameMangler::mangleType(const TypeOfType *T) {
1561 // FIXME: this is pretty unsatisfactory, but there isn't an obvious
1562 // "extension with parameters" mangling.
1563 Out << "u6typeof";
1564}
1565
1566void CXXNameMangler::mangleType(const TypeOfExprType *T) {
1567 // FIXME: this is pretty unsatisfactory, but there isn't an obvious
1568 // "extension with parameters" mangling.
1569 Out << "u6typeof";
1570}
1571
1572void CXXNameMangler::mangleType(const DecltypeType *T) {
1573 Expr *E = T->getUnderlyingExpr();
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001574
John McCallad5e7382010-03-01 23:49:17 +00001575 // type ::= Dt <expression> E # decltype of an id-expression
1576 // # or class member access
1577 // ::= DT <expression> E # decltype of an expression
1578
1579 // This purports to be an exhaustive list of id-expressions and
1580 // class member accesses. Note that we do not ignore parentheses;
1581 // parentheses change the semantics of decltype for these
1582 // expressions (and cause the mangler to use the other form).
1583 if (isa<DeclRefExpr>(E) ||
1584 isa<MemberExpr>(E) ||
1585 isa<UnresolvedLookupExpr>(E) ||
1586 isa<DependentScopeDeclRefExpr>(E) ||
1587 isa<CXXDependentScopeMemberExpr>(E) ||
1588 isa<UnresolvedMemberExpr>(E))
1589 Out << "Dt";
1590 else
1591 Out << "DT";
1592 mangleExpression(E);
1593 Out << 'E';
1594}
1595
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001596void CXXNameMangler::mangleIntegerLiteral(QualType T,
Anders Carlssone170ba72009-12-14 01:45:37 +00001597 const llvm::APSInt &Value) {
1598 // <expr-primary> ::= L <type> <value number> E # integer literal
1599 Out << 'L';
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001600
Anders Carlssone170ba72009-12-14 01:45:37 +00001601 mangleType(T);
1602 if (T->isBooleanType()) {
1603 // Boolean values are encoded as 0/1.
1604 Out << (Value.getBoolValue() ? '1' : '0');
1605 } else {
John McCall0512e482010-07-14 04:20:34 +00001606 mangleNumber(Value);
Anders Carlssone170ba72009-12-14 01:45:37 +00001607 }
1608 Out << 'E';
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001609
Anders Carlssone170ba72009-12-14 01:45:37 +00001610}
1611
John McCall2f27bf82010-02-04 02:56:29 +00001612/// Mangles a member expression. Implicit accesses are not handled,
1613/// but that should be okay, because you shouldn't be able to
1614/// make an implicit access in a function template declaration.
John McCall2f27bf82010-02-04 02:56:29 +00001615void CXXNameMangler::mangleMemberExpr(const Expr *Base,
1616 bool IsArrow,
1617 NestedNameSpecifier *Qualifier,
1618 DeclarationName Member,
1619 unsigned Arity) {
John McCalle1e342f2010-03-01 19:12:25 +00001620 // gcc-4.4 uses 'dt' for dot expressions, which is reasonable.
1621 // OTOH, gcc also mangles the name as an expression.
1622 Out << (IsArrow ? "pt" : "dt");
John McCall2f27bf82010-02-04 02:56:29 +00001623 mangleExpression(Base);
1624 mangleUnresolvedName(Qualifier, Member, Arity);
1625}
1626
John McCall5e1e89b2010-08-18 19:18:59 +00001627void CXXNameMangler::mangleExpression(const Expr *E, unsigned Arity) {
Anders Carlssond553f8c2009-09-21 01:21:10 +00001628 // <expression> ::= <unary operator-name> <expression>
John McCall09cc1412010-02-03 00:55:45 +00001629 // ::= <binary operator-name> <expression> <expression>
1630 // ::= <trinary operator-name> <expression> <expression> <expression>
Eli Friedmana7e68452010-08-22 01:00:03 +00001631 // ::= cl <expression>* E # call
Anders Carlssond553f8c2009-09-21 01:21:10 +00001632 // ::= cv <type> expression # conversion with one argument
1633 // ::= cv <type> _ <expression>* E # conversion with a different number of arguments
Eli Friedmana7e68452010-08-22 01:00:03 +00001634 // ::= st <type> # sizeof (a type)
Anders Carlssond553f8c2009-09-21 01:21:10 +00001635 // ::= at <type> # alignof (a type)
1636 // ::= <template-param>
1637 // ::= <function-param>
1638 // ::= sr <type> <unqualified-name> # dependent name
1639 // ::= sr <type> <unqualified-name> <template-args> # dependent template-id
1640 // ::= sZ <template-param> # size of a parameter pack
Douglas Gregor4fc48662011-01-13 16:39:34 +00001641 // ::= sZ <function-param> # size of a function parameter pack
John McCall09cc1412010-02-03 00:55:45 +00001642 // ::= <expr-primary>
John McCall1dd73832010-02-04 01:42:13 +00001643 // <expr-primary> ::= L <type> <value number> E # integer literal
1644 // ::= L <type <value float> E # floating literal
1645 // ::= L <mangled-name> E # external name
Anders Carlssond553f8c2009-09-21 01:21:10 +00001646 switch (E->getStmtClass()) {
John McCall6ae1f352010-04-09 22:26:14 +00001647 case Expr::NoStmtClass:
1648#define EXPR(Type, Base)
1649#define STMT(Type, Base) \
1650 case Expr::Type##Class:
Sean Hunt4bfe1962010-05-05 15:24:00 +00001651#include "clang/AST/StmtNodes.inc"
John McCall0512e482010-07-14 04:20:34 +00001652 // fallthrough
1653
1654 // These all can only appear in local or variable-initialization
1655 // contexts and so should never appear in a mangling.
1656 case Expr::AddrLabelExprClass:
1657 case Expr::BlockDeclRefExprClass:
1658 case Expr::CXXThisExprClass:
1659 case Expr::DesignatedInitExprClass:
1660 case Expr::ImplicitValueInitExprClass:
1661 case Expr::InitListExprClass:
1662 case Expr::ParenListExprClass:
1663 case Expr::CXXScalarValueInitExprClass:
John McCall09cc1412010-02-03 00:55:45 +00001664 llvm_unreachable("unexpected statement kind");
1665 break;
1666
John McCall0512e482010-07-14 04:20:34 +00001667 // FIXME: invent manglings for all these.
1668 case Expr::BlockExprClass:
1669 case Expr::CXXPseudoDestructorExprClass:
1670 case Expr::ChooseExprClass:
1671 case Expr::CompoundLiteralExprClass:
1672 case Expr::ExtVectorElementExprClass:
1673 case Expr::ObjCEncodeExprClass:
John McCall0512e482010-07-14 04:20:34 +00001674 case Expr::ObjCIsaExprClass:
1675 case Expr::ObjCIvarRefExprClass:
1676 case Expr::ObjCMessageExprClass:
1677 case Expr::ObjCPropertyRefExprClass:
1678 case Expr::ObjCProtocolExprClass:
1679 case Expr::ObjCSelectorExprClass:
1680 case Expr::ObjCStringLiteralClass:
John McCall0512e482010-07-14 04:20:34 +00001681 case Expr::OffsetOfExprClass:
1682 case Expr::PredefinedExprClass:
1683 case Expr::ShuffleVectorExprClass:
1684 case Expr::StmtExprClass:
John McCall0512e482010-07-14 04:20:34 +00001685 case Expr::UnaryTypeTraitExprClass:
Francois Pichet6ad6f282010-12-07 00:08:36 +00001686 case Expr::BinaryTypeTraitExprClass:
Francois Pichet9be88402010-09-08 23:47:05 +00001687 case Expr::VAArgExprClass:
Sebastian Redl2e156222010-09-10 20:55:43 +00001688 case Expr::CXXUuidofExprClass:
1689 case Expr::CXXNoexceptExprClass: {
John McCall6ae1f352010-04-09 22:26:14 +00001690 // As bad as this diagnostic is, it's better than crashing.
1691 Diagnostic &Diags = Context.getDiags();
1692 unsigned DiagID = Diags.getCustomDiagID(Diagnostic::Error,
1693 "cannot yet mangle expression type %0");
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +00001694 Diags.Report(E->getExprLoc(), DiagID)
John McCall739bf092010-04-10 09:39:25 +00001695 << E->getStmtClassName() << E->getSourceRange();
John McCall6ae1f352010-04-09 22:26:14 +00001696 break;
1697 }
1698
John McCall7cd7d1a2010-11-15 23:31:06 +00001699 case Expr::OpaqueValueExprClass:
1700 llvm_unreachable("cannot mangle opaque value; mangling wrong thing?");
1701
John McCall0512e482010-07-14 04:20:34 +00001702 case Expr::CXXDefaultArgExprClass:
John McCall5e1e89b2010-08-18 19:18:59 +00001703 mangleExpression(cast<CXXDefaultArgExpr>(E)->getExpr(), Arity);
John McCall0512e482010-07-14 04:20:34 +00001704 break;
1705
1706 case Expr::CXXMemberCallExprClass: // fallthrough
John McCall1dd73832010-02-04 01:42:13 +00001707 case Expr::CallExprClass: {
1708 const CallExpr *CE = cast<CallExpr>(E);
1709 Out << "cl";
John McCall5e1e89b2010-08-18 19:18:59 +00001710 mangleExpression(CE->getCallee(), CE->getNumArgs());
John McCall1dd73832010-02-04 01:42:13 +00001711 for (unsigned I = 0, N = CE->getNumArgs(); I != N; ++I)
1712 mangleExpression(CE->getArg(I));
Benjamin Kramer35f59b62010-04-10 16:03:31 +00001713 Out << 'E';
John McCall09cc1412010-02-03 00:55:45 +00001714 break;
John McCall1dd73832010-02-04 01:42:13 +00001715 }
John McCall09cc1412010-02-03 00:55:45 +00001716
John McCall0512e482010-07-14 04:20:34 +00001717 case Expr::CXXNewExprClass: {
1718 // Proposal from David Vandervoorde, 2010.06.30
1719 const CXXNewExpr *New = cast<CXXNewExpr>(E);
1720 if (New->isGlobalNew()) Out << "gs";
1721 Out << (New->isArray() ? "na" : "nw");
1722 for (CXXNewExpr::const_arg_iterator I = New->placement_arg_begin(),
1723 E = New->placement_arg_end(); I != E; ++I)
1724 mangleExpression(*I);
1725 Out << '_';
1726 mangleType(New->getAllocatedType());
1727 if (New->hasInitializer()) {
1728 Out << "pi";
1729 for (CXXNewExpr::const_arg_iterator I = New->constructor_arg_begin(),
1730 E = New->constructor_arg_end(); I != E; ++I)
1731 mangleExpression(*I);
1732 }
1733 Out << 'E';
1734 break;
1735 }
1736
John McCall2f27bf82010-02-04 02:56:29 +00001737 case Expr::MemberExprClass: {
1738 const MemberExpr *ME = cast<MemberExpr>(E);
1739 mangleMemberExpr(ME->getBase(), ME->isArrow(),
1740 ME->getQualifier(), ME->getMemberDecl()->getDeclName(),
John McCall5e1e89b2010-08-18 19:18:59 +00001741 Arity);
John McCall2f27bf82010-02-04 02:56:29 +00001742 break;
1743 }
1744
1745 case Expr::UnresolvedMemberExprClass: {
1746 const UnresolvedMemberExpr *ME = cast<UnresolvedMemberExpr>(E);
1747 mangleMemberExpr(ME->getBase(), ME->isArrow(),
1748 ME->getQualifier(), ME->getMemberName(),
John McCall5e1e89b2010-08-18 19:18:59 +00001749 Arity);
John McCall6dbce192010-08-20 00:17:19 +00001750 if (ME->hasExplicitTemplateArgs())
1751 mangleTemplateArgs(ME->getExplicitTemplateArgs());
John McCall2f27bf82010-02-04 02:56:29 +00001752 break;
1753 }
1754
1755 case Expr::CXXDependentScopeMemberExprClass: {
1756 const CXXDependentScopeMemberExpr *ME
1757 = cast<CXXDependentScopeMemberExpr>(E);
1758 mangleMemberExpr(ME->getBase(), ME->isArrow(),
1759 ME->getQualifier(), ME->getMember(),
John McCall5e1e89b2010-08-18 19:18:59 +00001760 Arity);
John McCall6dbce192010-08-20 00:17:19 +00001761 if (ME->hasExplicitTemplateArgs())
1762 mangleTemplateArgs(ME->getExplicitTemplateArgs());
John McCall2f27bf82010-02-04 02:56:29 +00001763 break;
1764 }
1765
John McCall1dd73832010-02-04 01:42:13 +00001766 case Expr::UnresolvedLookupExprClass: {
John McCalla3218e72010-02-04 01:48:38 +00001767 // The ABI doesn't cover how to mangle overload sets, so we mangle
1768 // using something as close as possible to the original lookup
1769 // expression.
John McCall1dd73832010-02-04 01:42:13 +00001770 const UnresolvedLookupExpr *ULE = cast<UnresolvedLookupExpr>(E);
John McCall5e1e89b2010-08-18 19:18:59 +00001771 mangleUnresolvedName(ULE->getQualifier(), ULE->getName(), Arity);
John McCall6dbce192010-08-20 00:17:19 +00001772 if (ULE->hasExplicitTemplateArgs())
1773 mangleTemplateArgs(ULE->getExplicitTemplateArgs());
John McCall1dd73832010-02-04 01:42:13 +00001774 break;
1775 }
1776
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001777 case Expr::CXXUnresolvedConstructExprClass: {
John McCall1dd73832010-02-04 01:42:13 +00001778 const CXXUnresolvedConstructExpr *CE = cast<CXXUnresolvedConstructExpr>(E);
1779 unsigned N = CE->arg_size();
1780
1781 Out << "cv";
1782 mangleType(CE->getType());
Benjamin Kramer35f59b62010-04-10 16:03:31 +00001783 if (N != 1) Out << '_';
John McCall1dd73832010-02-04 01:42:13 +00001784 for (unsigned I = 0; I != N; ++I) mangleExpression(CE->getArg(I));
Benjamin Kramer35f59b62010-04-10 16:03:31 +00001785 if (N != 1) Out << 'E';
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001786 break;
John McCall1dd73832010-02-04 01:42:13 +00001787 }
John McCall09cc1412010-02-03 00:55:45 +00001788
John McCall1dd73832010-02-04 01:42:13 +00001789 case Expr::CXXTemporaryObjectExprClass:
1790 case Expr::CXXConstructExprClass: {
1791 const CXXConstructExpr *CE = cast<CXXConstructExpr>(E);
1792 unsigned N = CE->getNumArgs();
1793
1794 Out << "cv";
1795 mangleType(CE->getType());
Benjamin Kramer35f59b62010-04-10 16:03:31 +00001796 if (N != 1) Out << '_';
John McCall1dd73832010-02-04 01:42:13 +00001797 for (unsigned I = 0; I != N; ++I) mangleExpression(CE->getArg(I));
Benjamin Kramer35f59b62010-04-10 16:03:31 +00001798 if (N != 1) Out << 'E';
John McCall09cc1412010-02-03 00:55:45 +00001799 break;
John McCall1dd73832010-02-04 01:42:13 +00001800 }
1801
1802 case Expr::SizeOfAlignOfExprClass: {
1803 const SizeOfAlignOfExpr *SAE = cast<SizeOfAlignOfExpr>(E);
Benjamin Kramer35f59b62010-04-10 16:03:31 +00001804 if (SAE->isSizeOf()) Out << 's';
1805 else Out << 'a';
John McCall1dd73832010-02-04 01:42:13 +00001806 if (SAE->isArgumentType()) {
Benjamin Kramer35f59b62010-04-10 16:03:31 +00001807 Out << 't';
John McCall1dd73832010-02-04 01:42:13 +00001808 mangleType(SAE->getArgumentType());
1809 } else {
Benjamin Kramer35f59b62010-04-10 16:03:31 +00001810 Out << 'z';
John McCall1dd73832010-02-04 01:42:13 +00001811 mangleExpression(SAE->getArgumentExpr());
1812 }
1813 break;
1814 }
Anders Carlssona7694082009-11-06 02:50:19 +00001815
John McCall0512e482010-07-14 04:20:34 +00001816 case Expr::CXXThrowExprClass: {
1817 const CXXThrowExpr *TE = cast<CXXThrowExpr>(E);
1818
1819 // Proposal from David Vandervoorde, 2010.06.30
1820 if (TE->getSubExpr()) {
1821 Out << "tw";
1822 mangleExpression(TE->getSubExpr());
1823 } else {
1824 Out << "tr";
1825 }
1826 break;
1827 }
1828
1829 case Expr::CXXTypeidExprClass: {
1830 const CXXTypeidExpr *TIE = cast<CXXTypeidExpr>(E);
1831
1832 // Proposal from David Vandervoorde, 2010.06.30
1833 if (TIE->isTypeOperand()) {
1834 Out << "ti";
1835 mangleType(TIE->getTypeOperand());
1836 } else {
1837 Out << "te";
1838 mangleExpression(TIE->getExprOperand());
1839 }
1840 break;
1841 }
1842
1843 case Expr::CXXDeleteExprClass: {
1844 const CXXDeleteExpr *DE = cast<CXXDeleteExpr>(E);
1845
1846 // Proposal from David Vandervoorde, 2010.06.30
1847 if (DE->isGlobalDelete()) Out << "gs";
1848 Out << (DE->isArrayForm() ? "da" : "dl");
1849 mangleExpression(DE->getArgument());
1850 break;
1851 }
1852
Anders Carlssone170ba72009-12-14 01:45:37 +00001853 case Expr::UnaryOperatorClass: {
1854 const UnaryOperator *UO = cast<UnaryOperator>(E);
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001855 mangleOperatorName(UnaryOperator::getOverloadedOperator(UO->getOpcode()),
Anders Carlssone170ba72009-12-14 01:45:37 +00001856 /*Arity=*/1);
1857 mangleExpression(UO->getSubExpr());
1858 break;
1859 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001860
John McCall0512e482010-07-14 04:20:34 +00001861 case Expr::ArraySubscriptExprClass: {
1862 const ArraySubscriptExpr *AE = cast<ArraySubscriptExpr>(E);
1863
1864 // Array subscript is treated as a syntactically wierd form of
1865 // binary operator.
1866 Out << "ix";
1867 mangleExpression(AE->getLHS());
1868 mangleExpression(AE->getRHS());
1869 break;
1870 }
1871
1872 case Expr::CompoundAssignOperatorClass: // fallthrough
Anders Carlssone170ba72009-12-14 01:45:37 +00001873 case Expr::BinaryOperatorClass: {
1874 const BinaryOperator *BO = cast<BinaryOperator>(E);
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001875 mangleOperatorName(BinaryOperator::getOverloadedOperator(BO->getOpcode()),
Anders Carlssone170ba72009-12-14 01:45:37 +00001876 /*Arity=*/2);
1877 mangleExpression(BO->getLHS());
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001878 mangleExpression(BO->getRHS());
Anders Carlssone170ba72009-12-14 01:45:37 +00001879 break;
John McCall2f27bf82010-02-04 02:56:29 +00001880 }
Anders Carlssone170ba72009-12-14 01:45:37 +00001881
1882 case Expr::ConditionalOperatorClass: {
1883 const ConditionalOperator *CO = cast<ConditionalOperator>(E);
1884 mangleOperatorName(OO_Conditional, /*Arity=*/3);
1885 mangleExpression(CO->getCond());
John McCall5e1e89b2010-08-18 19:18:59 +00001886 mangleExpression(CO->getLHS(), Arity);
1887 mangleExpression(CO->getRHS(), Arity);
Anders Carlssone170ba72009-12-14 01:45:37 +00001888 break;
1889 }
1890
Douglas Gregor46287c72010-01-29 16:37:09 +00001891 case Expr::ImplicitCastExprClass: {
John McCall5e1e89b2010-08-18 19:18:59 +00001892 mangleExpression(cast<ImplicitCastExpr>(E)->getSubExpr(), Arity);
Douglas Gregor46287c72010-01-29 16:37:09 +00001893 break;
1894 }
1895
1896 case Expr::CStyleCastExprClass:
1897 case Expr::CXXStaticCastExprClass:
1898 case Expr::CXXDynamicCastExprClass:
1899 case Expr::CXXReinterpretCastExprClass:
1900 case Expr::CXXConstCastExprClass:
1901 case Expr::CXXFunctionalCastExprClass: {
1902 const ExplicitCastExpr *ECE = cast<ExplicitCastExpr>(E);
1903 Out << "cv";
1904 mangleType(ECE->getType());
1905 mangleExpression(ECE->getSubExpr());
1906 break;
1907 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001908
Anders Carlsson58040a52009-12-16 05:48:46 +00001909 case Expr::CXXOperatorCallExprClass: {
1910 const CXXOperatorCallExpr *CE = cast<CXXOperatorCallExpr>(E);
1911 unsigned NumArgs = CE->getNumArgs();
1912 mangleOperatorName(CE->getOperator(), /*Arity=*/NumArgs);
1913 // Mangle the arguments.
1914 for (unsigned i = 0; i != NumArgs; ++i)
1915 mangleExpression(CE->getArg(i));
1916 break;
1917 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001918
Anders Carlssona7694082009-11-06 02:50:19 +00001919 case Expr::ParenExprClass:
John McCall5e1e89b2010-08-18 19:18:59 +00001920 mangleExpression(cast<ParenExpr>(E)->getSubExpr(), Arity);
Anders Carlssona7694082009-11-06 02:50:19 +00001921 break;
1922
Anders Carlssond553f8c2009-09-21 01:21:10 +00001923 case Expr::DeclRefExprClass: {
Douglas Gregor5ed1bc32010-02-28 21:40:32 +00001924 const NamedDecl *D = cast<DeclRefExpr>(E)->getDecl();
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00001925
Anders Carlssond553f8c2009-09-21 01:21:10 +00001926 switch (D->getKind()) {
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001927 default:
Douglas Gregor5ed1bc32010-02-28 21:40:32 +00001928 // <expr-primary> ::= L <mangled-name> E # external name
1929 Out << 'L';
1930 mangle(D, "_Z");
1931 Out << 'E';
1932 break;
1933
John McCall3dc7e7b2010-07-24 01:17:35 +00001934 case Decl::EnumConstant: {
1935 const EnumConstantDecl *ED = cast<EnumConstantDecl>(D);
1936 mangleIntegerLiteral(ED->getType(), ED->getInitVal());
1937 break;
1938 }
1939
Anders Carlssond553f8c2009-09-21 01:21:10 +00001940 case Decl::NonTypeTemplateParm: {
1941 const NonTypeTemplateParmDecl *PD = cast<NonTypeTemplateParmDecl>(D);
Anders Carlsson0ccdf8d2009-09-27 00:38:53 +00001942 mangleTemplateParameter(PD->getIndex());
Anders Carlssond553f8c2009-09-21 01:21:10 +00001943 break;
1944 }
1945
1946 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00001947
Anders Carlsson50755b02009-09-27 20:11:34 +00001948 break;
Anders Carlssond553f8c2009-09-21 01:21:10 +00001949 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00001950
Douglas Gregorc7793c72011-01-15 01:15:58 +00001951 case Expr::SubstNonTypeTemplateParmPackExprClass:
1952 mangleTemplateParameter(
1953 cast<SubstNonTypeTemplateParmPackExpr>(E)->getParameterPack()->getIndex());
1954 break;
1955
John McCall865d4472009-11-19 22:55:06 +00001956 case Expr::DependentScopeDeclRefExprClass: {
1957 const DependentScopeDeclRefExpr *DRE = cast<DependentScopeDeclRefExpr>(E);
Douglas Gregor4b2ccfc2010-02-28 22:05:49 +00001958 NestedNameSpecifier *NNS = DRE->getQualifier();
1959 const Type *QTy = NNS->getAsType();
1960
1961 // When we're dealing with a nested-name-specifier that has just a
1962 // dependent identifier in it, mangle that as a typename. FIXME:
1963 // It isn't clear that we ever actually want to have such a
1964 // nested-name-specifier; why not just represent it as a typename type?
1965 if (!QTy && NNS->getAsIdentifier() && NNS->getPrefix()) {
Douglas Gregor4a2023f2010-03-31 20:19:30 +00001966 QTy = getASTContext().getDependentNameType(ETK_Typename,
1967 NNS->getPrefix(),
1968 NNS->getAsIdentifier())
Douglas Gregor4b2ccfc2010-02-28 22:05:49 +00001969 .getTypePtr();
1970 }
Anders Carlsson50755b02009-09-27 20:11:34 +00001971 assert(QTy && "Qualifier was not type!");
1972
John McCall6dbce192010-08-20 00:17:19 +00001973 // ::= sr <type> <unqualified-name> # dependent name
1974 // ::= sr <type> <unqualified-name> <template-args> # dependent template-id
Anders Carlsson50755b02009-09-27 20:11:34 +00001975 Out << "sr";
1976 mangleType(QualType(QTy, 0));
John McCall5e1e89b2010-08-18 19:18:59 +00001977 mangleUnqualifiedName(0, DRE->getDeclName(), Arity);
John McCall6dbce192010-08-20 00:17:19 +00001978 if (DRE->hasExplicitTemplateArgs())
1979 mangleTemplateArgs(DRE->getExplicitTemplateArgs());
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00001980
Anders Carlsson50755b02009-09-27 20:11:34 +00001981 break;
1982 }
1983
John McCalld9307602010-04-09 22:54:09 +00001984 case Expr::CXXBindTemporaryExprClass:
1985 mangleExpression(cast<CXXBindTemporaryExpr>(E)->getSubExpr());
1986 break;
1987
John McCall4765fa02010-12-06 08:20:24 +00001988 case Expr::ExprWithCleanupsClass:
1989 mangleExpression(cast<ExprWithCleanups>(E)->getSubExpr(), Arity);
John McCalld9307602010-04-09 22:54:09 +00001990 break;
1991
John McCall1dd73832010-02-04 01:42:13 +00001992 case Expr::FloatingLiteralClass: {
1993 const FloatingLiteral *FL = cast<FloatingLiteral>(E);
Benjamin Kramer35f59b62010-04-10 16:03:31 +00001994 Out << 'L';
John McCall1dd73832010-02-04 01:42:13 +00001995 mangleType(FL->getType());
John McCall0512e482010-07-14 04:20:34 +00001996 mangleFloat(FL->getValue());
Benjamin Kramer35f59b62010-04-10 16:03:31 +00001997 Out << 'E';
John McCall1dd73832010-02-04 01:42:13 +00001998 break;
1999 }
2000
John McCallde810632010-04-09 21:48:08 +00002001 case Expr::CharacterLiteralClass:
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002002 Out << 'L';
John McCallde810632010-04-09 21:48:08 +00002003 mangleType(E->getType());
2004 Out << cast<CharacterLiteral>(E)->getValue();
2005 Out << 'E';
2006 break;
2007
2008 case Expr::CXXBoolLiteralExprClass:
2009 Out << "Lb";
2010 Out << (cast<CXXBoolLiteralExpr>(E)->getValue() ? '1' : '0');
2011 Out << 'E';
2012 break;
2013
John McCall0512e482010-07-14 04:20:34 +00002014 case Expr::IntegerLiteralClass: {
2015 llvm::APSInt Value(cast<IntegerLiteral>(E)->getValue());
2016 if (E->getType()->isSignedIntegerType())
2017 Value.setIsSigned(true);
2018 mangleIntegerLiteral(E->getType(), Value);
Anders Carlssone170ba72009-12-14 01:45:37 +00002019 break;
John McCall0512e482010-07-14 04:20:34 +00002020 }
2021
2022 case Expr::ImaginaryLiteralClass: {
2023 const ImaginaryLiteral *IE = cast<ImaginaryLiteral>(E);
2024 // Mangle as if a complex literal.
Nick Lewycky271b6652010-09-05 03:40:33 +00002025 // Proposal from David Vandevoorde, 2010.06.30.
John McCall0512e482010-07-14 04:20:34 +00002026 Out << 'L';
2027 mangleType(E->getType());
2028 if (const FloatingLiteral *Imag =
2029 dyn_cast<FloatingLiteral>(IE->getSubExpr())) {
2030 // Mangle a floating-point zero of the appropriate type.
2031 mangleFloat(llvm::APFloat(Imag->getValue().getSemantics()));
2032 Out << '_';
2033 mangleFloat(Imag->getValue());
2034 } else {
Nick Lewycky271b6652010-09-05 03:40:33 +00002035 Out << "0_";
John McCall0512e482010-07-14 04:20:34 +00002036 llvm::APSInt Value(cast<IntegerLiteral>(IE->getSubExpr())->getValue());
2037 if (IE->getSubExpr()->getType()->isSignedIntegerType())
2038 Value.setIsSigned(true);
2039 mangleNumber(Value);
2040 }
2041 Out << 'E';
2042 break;
2043 }
2044
2045 case Expr::StringLiteralClass: {
John McCall1658c392010-07-15 21:53:03 +00002046 // Revised proposal from David Vandervoorde, 2010.07.15.
John McCall0512e482010-07-14 04:20:34 +00002047 Out << 'L';
John McCall1658c392010-07-15 21:53:03 +00002048 assert(isa<ConstantArrayType>(E->getType()));
2049 mangleType(E->getType());
John McCall0512e482010-07-14 04:20:34 +00002050 Out << 'E';
2051 break;
2052 }
2053
2054 case Expr::GNUNullExprClass:
2055 // FIXME: should this really be mangled the same as nullptr?
2056 // fallthrough
2057
2058 case Expr::CXXNullPtrLiteralExprClass: {
2059 // Proposal from David Vandervoorde, 2010.06.30, as
2060 // modified by ABI list discussion.
2061 Out << "LDnE";
2062 break;
2063 }
Douglas Gregorbe230c32011-01-03 17:17:50 +00002064
2065 case Expr::PackExpansionExprClass:
2066 Out << "sp";
2067 mangleExpression(cast<PackExpansionExpr>(E)->getPattern());
2068 break;
Douglas Gregor2e774c42011-01-04 18:56:13 +00002069
2070 case Expr::SizeOfPackExprClass: {
Douglas Gregor2e774c42011-01-04 18:56:13 +00002071 Out << "sZ";
2072 const NamedDecl *Pack = cast<SizeOfPackExpr>(E)->getPack();
2073 if (const TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(Pack))
2074 mangleTemplateParameter(TTP->getIndex());
2075 else if (const NonTypeTemplateParmDecl *NTTP
2076 = dyn_cast<NonTypeTemplateParmDecl>(Pack))
2077 mangleTemplateParameter(NTTP->getIndex());
2078 else if (const TemplateTemplateParmDecl *TempTP
2079 = dyn_cast<TemplateTemplateParmDecl>(Pack))
2080 mangleTemplateParameter(TempTP->getIndex());
2081 else {
Douglas Gregor4fc48662011-01-13 16:39:34 +00002082 // Note: proposed by Mike Herrick on 11/30/10
2083 // <expression> ::= sZ <function-param> # size of function parameter pack
Douglas Gregor2e774c42011-01-04 18:56:13 +00002084 Diagnostic &Diags = Context.getDiags();
2085 unsigned DiagID = Diags.getCustomDiagID(Diagnostic::Error,
2086 "cannot mangle sizeof...(function parameter pack)");
2087 Diags.Report(DiagID);
2088 return;
2089 }
2090 }
Anders Carlssond553f8c2009-09-21 01:21:10 +00002091 }
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00002092}
2093
Anders Carlsson3ac86b52009-04-15 05:36:58 +00002094void CXXNameMangler::mangleCXXCtorType(CXXCtorType T) {
2095 // <ctor-dtor-name> ::= C1 # complete object constructor
2096 // ::= C2 # base object constructor
2097 // ::= C3 # complete object allocating constructor
2098 //
2099 switch (T) {
2100 case Ctor_Complete:
2101 Out << "C1";
2102 break;
2103 case Ctor_Base:
2104 Out << "C2";
2105 break;
2106 case Ctor_CompleteAllocating:
2107 Out << "C3";
2108 break;
2109 }
2110}
2111
Anders Carlsson27ae5362009-04-17 01:58:57 +00002112void CXXNameMangler::mangleCXXDtorType(CXXDtorType T) {
2113 // <ctor-dtor-name> ::= D0 # deleting destructor
2114 // ::= D1 # complete object destructor
2115 // ::= D2 # base object destructor
2116 //
2117 switch (T) {
2118 case Dtor_Deleting:
2119 Out << "D0";
2120 break;
2121 case Dtor_Complete:
2122 Out << "D1";
2123 break;
2124 case Dtor_Base:
2125 Out << "D2";
2126 break;
2127 }
2128}
2129
John McCall6dbce192010-08-20 00:17:19 +00002130void CXXNameMangler::mangleTemplateArgs(
2131 const ExplicitTemplateArgumentList &TemplateArgs) {
2132 // <template-args> ::= I <template-arg>+ E
2133 Out << 'I';
2134 for (unsigned I = 0, E = TemplateArgs.NumTemplateArgs; I != E; ++I)
2135 mangleTemplateArg(0, TemplateArgs.getTemplateArgs()[I].getArgument());
2136 Out << 'E';
2137}
2138
Douglas Gregor20f0cc72010-04-23 03:10:43 +00002139void CXXNameMangler::mangleTemplateArgs(TemplateName Template,
2140 const TemplateArgument *TemplateArgs,
2141 unsigned NumTemplateArgs) {
2142 if (TemplateDecl *TD = Template.getAsTemplateDecl())
2143 return mangleTemplateArgs(*TD->getTemplateParameters(), TemplateArgs,
2144 NumTemplateArgs);
Sean Huntc3021132010-05-05 15:23:54 +00002145
Douglas Gregor20f0cc72010-04-23 03:10:43 +00002146 // <template-args> ::= I <template-arg>+ E
2147 Out << 'I';
2148 for (unsigned i = 0; i != NumTemplateArgs; ++i)
2149 mangleTemplateArg(0, TemplateArgs[i]);
2150 Out << 'E';
2151}
2152
Rafael Espindolad9800722010-03-11 14:07:00 +00002153void CXXNameMangler::mangleTemplateArgs(const TemplateParameterList &PL,
2154 const TemplateArgumentList &AL) {
Anders Carlsson7a0ba872009-05-15 16:09:15 +00002155 // <template-args> ::= I <template-arg>+ E
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002156 Out << 'I';
Rafael Espindolad9800722010-03-11 14:07:00 +00002157 for (unsigned i = 0, e = AL.size(); i != e; ++i)
2158 mangleTemplateArg(PL.getParam(i), AL[i]);
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002159 Out << 'E';
Anders Carlsson7a0ba872009-05-15 16:09:15 +00002160}
2161
Rafael Espindolad9800722010-03-11 14:07:00 +00002162void CXXNameMangler::mangleTemplateArgs(const TemplateParameterList &PL,
2163 const TemplateArgument *TemplateArgs,
Anders Carlsson7624f212009-09-18 02:42:01 +00002164 unsigned NumTemplateArgs) {
2165 // <template-args> ::= I <template-arg>+ E
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002166 Out << 'I';
Daniel Dunbar7e0c1952009-11-21 09:17:15 +00002167 for (unsigned i = 0; i != NumTemplateArgs; ++i)
Rafael Espindolad9800722010-03-11 14:07:00 +00002168 mangleTemplateArg(PL.getParam(i), TemplateArgs[i]);
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002169 Out << 'E';
Anders Carlsson7624f212009-09-18 02:42:01 +00002170}
2171
Rafael Espindolad9800722010-03-11 14:07:00 +00002172void CXXNameMangler::mangleTemplateArg(const NamedDecl *P,
2173 const TemplateArgument &A) {
Mike Stump1eb44332009-09-09 15:08:12 +00002174 // <template-arg> ::= <type> # type or template
Anders Carlsson7a0ba872009-05-15 16:09:15 +00002175 // ::= X <expression> E # expression
2176 // ::= <expr-primary> # simple expressions
Douglas Gregor4fc48662011-01-13 16:39:34 +00002177 // ::= J <template-arg>* E # argument pack
Anders Carlsson7a0ba872009-05-15 16:09:15 +00002178 // ::= sp <expression> # pack expansion of (C++0x)
2179 switch (A.getKind()) {
Douglas Gregorf90b27a2011-01-03 22:36:02 +00002180 case TemplateArgument::Null:
2181 llvm_unreachable("Cannot mangle NULL template argument");
2182
Anders Carlsson7a0ba872009-05-15 16:09:15 +00002183 case TemplateArgument::Type:
2184 mangleType(A.getAsType());
2185 break;
Anders Carlsson9e85c742009-12-23 19:30:55 +00002186 case TemplateArgument::Template:
John McCallb6f532e2010-07-14 06:43:17 +00002187 // This is mangled as <type>.
2188 mangleType(A.getAsTemplate());
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002189 break;
Douglas Gregora7fc9012011-01-05 18:58:31 +00002190 case TemplateArgument::TemplateExpansion:
Douglas Gregor4fc48662011-01-13 16:39:34 +00002191 // <type> ::= Dp <type> # pack expansion (C++0x)
Douglas Gregora7fc9012011-01-05 18:58:31 +00002192 Out << "Dp";
2193 mangleType(A.getAsTemplateOrTemplatePattern());
2194 break;
Anders Carlssond553f8c2009-09-21 01:21:10 +00002195 case TemplateArgument::Expression:
2196 Out << 'X';
2197 mangleExpression(A.getAsExpr());
2198 Out << 'E';
2199 break;
Anders Carlssone170ba72009-12-14 01:45:37 +00002200 case TemplateArgument::Integral:
2201 mangleIntegerLiteral(A.getIntegralType(), *A.getAsIntegral());
Anders Carlsson7a0ba872009-05-15 16:09:15 +00002202 break;
Daniel Dunbar7e0c1952009-11-21 09:17:15 +00002203 case TemplateArgument::Declaration: {
Douglas Gregor20f0cc72010-04-23 03:10:43 +00002204 assert(P && "Missing template parameter for declaration argument");
Daniel Dunbar7e0c1952009-11-21 09:17:15 +00002205 // <expr-primary> ::= L <mangled-name> E # external name
2206
Rafael Espindolad9800722010-03-11 14:07:00 +00002207 // Clang produces AST's where pointer-to-member-function expressions
Daniel Dunbar7e0c1952009-11-21 09:17:15 +00002208 // and pointer-to-function expressions are represented as a declaration not
Rafael Espindolad9800722010-03-11 14:07:00 +00002209 // an expression. We compensate for it here to produce the correct mangling.
2210 NamedDecl *D = cast<NamedDecl>(A.getAsDecl());
2211 const NonTypeTemplateParmDecl *Parameter = cast<NonTypeTemplateParmDecl>(P);
2212 bool compensateMangling = D->isCXXClassMember() &&
2213 !Parameter->getType()->isReferenceType();
2214 if (compensateMangling) {
2215 Out << 'X';
2216 mangleOperatorName(OO_Amp, 1);
2217 }
2218
Daniel Dunbar7e0c1952009-11-21 09:17:15 +00002219 Out << 'L';
2220 // References to external entities use the mangled name; if the name would
2221 // not normally be manged then mangle it as unqualified.
2222 //
2223 // FIXME: The ABI specifies that external names here should have _Z, but
2224 // gcc leaves this off.
Rafael Espindolad9800722010-03-11 14:07:00 +00002225 if (compensateMangling)
2226 mangle(D, "_Z");
2227 else
2228 mangle(D, "Z");
Daniel Dunbar7e0c1952009-11-21 09:17:15 +00002229 Out << 'E';
Rafael Espindolad9800722010-03-11 14:07:00 +00002230
2231 if (compensateMangling)
2232 Out << 'E';
2233
Daniel Dunbar7e0c1952009-11-21 09:17:15 +00002234 break;
2235 }
Douglas Gregorf90b27a2011-01-03 22:36:02 +00002236
2237 case TemplateArgument::Pack: {
2238 // Note: proposal by Mike Herrick on 12/20/10
2239 Out << 'J';
2240 for (TemplateArgument::pack_iterator PA = A.pack_begin(),
2241 PAEnd = A.pack_end();
2242 PA != PAEnd; ++PA)
2243 mangleTemplateArg(P, *PA);
2244 Out << 'E';
2245 }
Daniel Dunbar7e0c1952009-11-21 09:17:15 +00002246 }
Anders Carlsson7a0ba872009-05-15 16:09:15 +00002247}
2248
Anders Carlsson0ccdf8d2009-09-27 00:38:53 +00002249void CXXNameMangler::mangleTemplateParameter(unsigned Index) {
2250 // <template-param> ::= T_ # first template parameter
2251 // ::= T <parameter-2 non-negative number> _
2252 if (Index == 0)
2253 Out << "T_";
2254 else
2255 Out << 'T' << (Index - 1) << '_';
2256}
2257
Anders Carlsson76967372009-09-17 00:43:46 +00002258// <substitution> ::= S <seq-id> _
2259// ::= S_
Anders Carlsson6862fc72009-09-17 04:16:28 +00002260bool CXXNameMangler::mangleSubstitution(const NamedDecl *ND) {
Anders Carlssone7c8cb62009-09-26 20:53:44 +00002261 // Try one of the standard substitutions first.
2262 if (mangleStandardSubstitution(ND))
2263 return true;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002264
Anders Carlsson433d1372009-11-07 04:26:04 +00002265 ND = cast<NamedDecl>(ND->getCanonicalDecl());
Anders Carlsson6862fc72009-09-17 04:16:28 +00002266 return mangleSubstitution(reinterpret_cast<uintptr_t>(ND));
2267}
2268
Anders Carlsson76967372009-09-17 00:43:46 +00002269bool CXXNameMangler::mangleSubstitution(QualType T) {
Anders Carlssond99edc42009-09-26 03:55:37 +00002270 if (!T.getCVRQualifiers()) {
2271 if (const RecordType *RT = T->getAs<RecordType>())
2272 return mangleSubstitution(RT->getDecl());
2273 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002274
Anders Carlsson76967372009-09-17 00:43:46 +00002275 uintptr_t TypePtr = reinterpret_cast<uintptr_t>(T.getAsOpaquePtr());
2276
Anders Carlssond3a932a2009-09-17 03:53:28 +00002277 return mangleSubstitution(TypePtr);
2278}
2279
Douglas Gregor1e9268e2010-04-28 05:58:56 +00002280bool CXXNameMangler::mangleSubstitution(TemplateName Template) {
2281 if (TemplateDecl *TD = Template.getAsTemplateDecl())
2282 return mangleSubstitution(TD);
Sean Huntc3021132010-05-05 15:23:54 +00002283
Douglas Gregor1e9268e2010-04-28 05:58:56 +00002284 Template = Context.getASTContext().getCanonicalTemplateName(Template);
2285 return mangleSubstitution(
2286 reinterpret_cast<uintptr_t>(Template.getAsVoidPointer()));
2287}
2288
Anders Carlssond3a932a2009-09-17 03:53:28 +00002289bool CXXNameMangler::mangleSubstitution(uintptr_t Ptr) {
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002290 llvm::DenseMap<uintptr_t, unsigned>::iterator I = Substitutions.find(Ptr);
Anders Carlsson76967372009-09-17 00:43:46 +00002291 if (I == Substitutions.end())
2292 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002293
Anders Carlsson76967372009-09-17 00:43:46 +00002294 unsigned SeqID = I->second;
2295 if (SeqID == 0)
2296 Out << "S_";
2297 else {
2298 SeqID--;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002299
Anders Carlsson76967372009-09-17 00:43:46 +00002300 // <seq-id> is encoded in base-36, using digits and upper case letters.
2301 char Buffer[10];
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002302 char *BufferPtr = llvm::array_endof(Buffer);
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002303
Anders Carlsson76967372009-09-17 00:43:46 +00002304 if (SeqID == 0) *--BufferPtr = '0';
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002305
Anders Carlsson76967372009-09-17 00:43:46 +00002306 while (SeqID) {
2307 assert(BufferPtr > Buffer && "Buffer overflow!");
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002308
John McCall6ab30e02010-06-09 07:26:17 +00002309 char c = static_cast<char>(SeqID % 36);
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002310
Anders Carlsson76967372009-09-17 00:43:46 +00002311 *--BufferPtr = (c < 10 ? '0' + c : 'A' + c - 10);
2312 SeqID /= 36;
2313 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002314
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002315 Out << 'S'
2316 << llvm::StringRef(BufferPtr, llvm::array_endof(Buffer)-BufferPtr)
2317 << '_';
Anders Carlsson76967372009-09-17 00:43:46 +00002318 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002319
Anders Carlsson76967372009-09-17 00:43:46 +00002320 return true;
2321}
2322
Anders Carlssonf514b542009-09-27 00:12:57 +00002323static bool isCharType(QualType T) {
2324 if (T.isNull())
2325 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002326
Anders Carlssonf514b542009-09-27 00:12:57 +00002327 return T->isSpecificBuiltinType(BuiltinType::Char_S) ||
2328 T->isSpecificBuiltinType(BuiltinType::Char_U);
2329}
2330
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002331/// isCharSpecialization - Returns whether a given type is a template
Anders Carlssonf514b542009-09-27 00:12:57 +00002332/// specialization of a given name with a single argument of type char.
2333static bool isCharSpecialization(QualType T, const char *Name) {
2334 if (T.isNull())
2335 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002336
Anders Carlssonf514b542009-09-27 00:12:57 +00002337 const RecordType *RT = T->getAs<RecordType>();
2338 if (!RT)
2339 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002340
2341 const ClassTemplateSpecializationDecl *SD =
Anders Carlssonf514b542009-09-27 00:12:57 +00002342 dyn_cast<ClassTemplateSpecializationDecl>(RT->getDecl());
2343 if (!SD)
2344 return false;
2345
2346 if (!isStdNamespace(SD->getDeclContext()))
2347 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002348
Anders Carlssonf514b542009-09-27 00:12:57 +00002349 const TemplateArgumentList &TemplateArgs = SD->getTemplateArgs();
2350 if (TemplateArgs.size() != 1)
2351 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002352
Anders Carlssonf514b542009-09-27 00:12:57 +00002353 if (!isCharType(TemplateArgs[0].getAsType()))
2354 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002355
Daniel Dunbar01eb9b92009-10-18 21:17:35 +00002356 return SD->getIdentifier()->getName() == Name;
Anders Carlssonf514b542009-09-27 00:12:57 +00002357}
2358
Anders Carlsson91f88602009-12-07 19:56:42 +00002359template <std::size_t StrLen>
Benjamin Kramer54353f42010-11-25 18:29:30 +00002360static bool isStreamCharSpecialization(const ClassTemplateSpecializationDecl*SD,
2361 const char (&Str)[StrLen]) {
Anders Carlsson91f88602009-12-07 19:56:42 +00002362 if (!SD->getIdentifier()->isStr(Str))
2363 return false;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002364
Anders Carlsson91f88602009-12-07 19:56:42 +00002365 const TemplateArgumentList &TemplateArgs = SD->getTemplateArgs();
2366 if (TemplateArgs.size() != 2)
2367 return false;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002368
Anders Carlsson91f88602009-12-07 19:56:42 +00002369 if (!isCharType(TemplateArgs[0].getAsType()))
2370 return false;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002371
Anders Carlsson91f88602009-12-07 19:56:42 +00002372 if (!isCharSpecialization(TemplateArgs[1].getAsType(), "char_traits"))
2373 return false;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002374
Anders Carlsson91f88602009-12-07 19:56:42 +00002375 return true;
2376}
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002377
Anders Carlssone7c8cb62009-09-26 20:53:44 +00002378bool CXXNameMangler::mangleStandardSubstitution(const NamedDecl *ND) {
2379 // <substitution> ::= St # ::std::
Anders Carlsson8c031552009-09-26 23:10:05 +00002380 if (const NamespaceDecl *NS = dyn_cast<NamespaceDecl>(ND)) {
Anders Carlsson47846d22009-12-04 06:23:23 +00002381 if (isStd(NS)) {
Anders Carlsson8c031552009-09-26 23:10:05 +00002382 Out << "St";
2383 return true;
2384 }
2385 }
2386
2387 if (const ClassTemplateDecl *TD = dyn_cast<ClassTemplateDecl>(ND)) {
2388 if (!isStdNamespace(TD->getDeclContext()))
2389 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002390
Anders Carlsson8c031552009-09-26 23:10:05 +00002391 // <substitution> ::= Sa # ::std::allocator
2392 if (TD->getIdentifier()->isStr("allocator")) {
2393 Out << "Sa";
2394 return true;
2395 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002396
Anders Carlsson189d59c2009-09-26 23:14:39 +00002397 // <<substitution> ::= Sb # ::std::basic_string
2398 if (TD->getIdentifier()->isStr("basic_string")) {
2399 Out << "Sb";
2400 return true;
2401 }
Anders Carlsson8c031552009-09-26 23:10:05 +00002402 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002403
2404 if (const ClassTemplateSpecializationDecl *SD =
Anders Carlssonf514b542009-09-27 00:12:57 +00002405 dyn_cast<ClassTemplateSpecializationDecl>(ND)) {
Eli Friedman5370ee22010-02-23 18:25:09 +00002406 if (!isStdNamespace(SD->getDeclContext()))
2407 return false;
2408
Anders Carlssonf514b542009-09-27 00:12:57 +00002409 // <substitution> ::= Ss # ::std::basic_string<char,
2410 // ::std::char_traits<char>,
2411 // ::std::allocator<char> >
2412 if (SD->getIdentifier()->isStr("basic_string")) {
2413 const TemplateArgumentList &TemplateArgs = SD->getTemplateArgs();
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002414
Anders Carlssonf514b542009-09-27 00:12:57 +00002415 if (TemplateArgs.size() != 3)
2416 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002417
Anders Carlssonf514b542009-09-27 00:12:57 +00002418 if (!isCharType(TemplateArgs[0].getAsType()))
2419 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002420
Anders Carlssonf514b542009-09-27 00:12:57 +00002421 if (!isCharSpecialization(TemplateArgs[1].getAsType(), "char_traits"))
2422 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002423
Anders Carlssonf514b542009-09-27 00:12:57 +00002424 if (!isCharSpecialization(TemplateArgs[2].getAsType(), "allocator"))
2425 return false;
2426
2427 Out << "Ss";
2428 return true;
2429 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002430
Anders Carlsson91f88602009-12-07 19:56:42 +00002431 // <substitution> ::= Si # ::std::basic_istream<char,
2432 // ::std::char_traits<char> >
2433 if (isStreamCharSpecialization(SD, "basic_istream")) {
2434 Out << "Si";
2435 return true;
2436 }
2437
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002438 // <substitution> ::= So # ::std::basic_ostream<char,
Anders Carlsson8f8fd8e2009-10-08 17:20:26 +00002439 // ::std::char_traits<char> >
Anders Carlsson91f88602009-12-07 19:56:42 +00002440 if (isStreamCharSpecialization(SD, "basic_ostream")) {
Anders Carlsson8f8fd8e2009-10-08 17:20:26 +00002441 Out << "So";
2442 return true;
2443 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002444
Anders Carlsson91f88602009-12-07 19:56:42 +00002445 // <substitution> ::= Sd # ::std::basic_iostream<char,
2446 // ::std::char_traits<char> >
2447 if (isStreamCharSpecialization(SD, "basic_iostream")) {
2448 Out << "Sd";
2449 return true;
2450 }
Anders Carlssonf514b542009-09-27 00:12:57 +00002451 }
Anders Carlsson8c031552009-09-26 23:10:05 +00002452 return false;
Anders Carlssone7c8cb62009-09-26 20:53:44 +00002453}
2454
Anders Carlsson76967372009-09-17 00:43:46 +00002455void CXXNameMangler::addSubstitution(QualType T) {
Anders Carlssond99edc42009-09-26 03:55:37 +00002456 if (!T.getCVRQualifiers()) {
2457 if (const RecordType *RT = T->getAs<RecordType>()) {
2458 addSubstitution(RT->getDecl());
2459 return;
2460 }
2461 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002462
Anders Carlsson76967372009-09-17 00:43:46 +00002463 uintptr_t TypePtr = reinterpret_cast<uintptr_t>(T.getAsOpaquePtr());
Anders Carlssond3a932a2009-09-17 03:53:28 +00002464 addSubstitution(TypePtr);
2465}
2466
Douglas Gregor1e9268e2010-04-28 05:58:56 +00002467void CXXNameMangler::addSubstitution(TemplateName Template) {
2468 if (TemplateDecl *TD = Template.getAsTemplateDecl())
2469 return addSubstitution(TD);
Sean Huntc3021132010-05-05 15:23:54 +00002470
Douglas Gregor1e9268e2010-04-28 05:58:56 +00002471 Template = Context.getASTContext().getCanonicalTemplateName(Template);
2472 addSubstitution(reinterpret_cast<uintptr_t>(Template.getAsVoidPointer()));
2473}
2474
Anders Carlssond3a932a2009-09-17 03:53:28 +00002475void CXXNameMangler::addSubstitution(uintptr_t Ptr) {
Anders Carlssond3a932a2009-09-17 03:53:28 +00002476 assert(!Substitutions.count(Ptr) && "Substitution already exists!");
Anders Carlsson9d85b722010-06-02 04:29:50 +00002477 Substitutions[Ptr] = SeqID++;
Anders Carlsson76967372009-09-17 00:43:46 +00002478}
2479
Daniel Dunbar1b077112009-11-21 09:06:10 +00002480//
Mike Stump1eb44332009-09-09 15:08:12 +00002481
Daniel Dunbar1b077112009-11-21 09:06:10 +00002482/// \brief Mangles the name of the declaration D and emits that name to the
2483/// given output stream.
2484///
2485/// If the declaration D requires a mangled name, this routine will emit that
2486/// mangled name to \p os and return true. Otherwise, \p os will be unchanged
2487/// and this routine will return false. In this case, the caller should just
2488/// emit the identifier of the declaration (\c D->getIdentifier()) as its
2489/// name.
Peter Collingbourne14110472011-01-13 18:57:25 +00002490void ItaniumMangleContext::mangleName(const NamedDecl *D,
2491 llvm::SmallVectorImpl<char> &Res) {
Daniel Dunbarc02ab4c2009-11-21 09:14:44 +00002492 assert((isa<FunctionDecl>(D) || isa<VarDecl>(D)) &&
2493 "Invalid mangleName() call, argument is not a variable or function!");
2494 assert(!isa<CXXConstructorDecl>(D) && !isa<CXXDestructorDecl>(D) &&
2495 "Invalid mangleName() call on 'structor decl!");
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002496
Daniel Dunbar1b077112009-11-21 09:06:10 +00002497 PrettyStackTraceDecl CrashInfo(D, SourceLocation(),
2498 getASTContext().getSourceManager(),
2499 "Mangling declaration");
Mike Stump1eb44332009-09-09 15:08:12 +00002500
Daniel Dunbar94fd26d2009-11-21 09:06:22 +00002501 CXXNameMangler Mangler(*this, Res);
2502 return Mangler.mangle(D);
Daniel Dunbar1b077112009-11-21 09:06:10 +00002503}
Mike Stump1eb44332009-09-09 15:08:12 +00002504
Peter Collingbourne14110472011-01-13 18:57:25 +00002505void ItaniumMangleContext::mangleCXXCtor(const CXXConstructorDecl *D,
2506 CXXCtorType Type,
2507 llvm::SmallVectorImpl<char> &Res) {
Daniel Dunbar77939c92009-11-21 09:06:31 +00002508 CXXNameMangler Mangler(*this, Res, D, Type);
2509 Mangler.mangle(D);
Daniel Dunbar1b077112009-11-21 09:06:10 +00002510}
Mike Stump1eb44332009-09-09 15:08:12 +00002511
Peter Collingbourne14110472011-01-13 18:57:25 +00002512void ItaniumMangleContext::mangleCXXDtor(const CXXDestructorDecl *D,
2513 CXXDtorType Type,
2514 llvm::SmallVectorImpl<char> &Res) {
Daniel Dunbar77939c92009-11-21 09:06:31 +00002515 CXXNameMangler Mangler(*this, Res, D, Type);
2516 Mangler.mangle(D);
Daniel Dunbar1b077112009-11-21 09:06:10 +00002517}
Mike Stumpf1216772009-07-31 18:25:34 +00002518
Peter Collingbourne14110472011-01-13 18:57:25 +00002519void ItaniumMangleContext::mangleThunk(const CXXMethodDecl *MD,
2520 const ThunkInfo &Thunk,
2521 llvm::SmallVectorImpl<char> &Res) {
Anders Carlsson19879c92010-03-23 17:17:29 +00002522 // <special-name> ::= T <call-offset> <base encoding>
2523 // # base is the nominal target function of thunk
2524 // <special-name> ::= Tc <call-offset> <call-offset> <base encoding>
2525 // # base is the nominal target function of thunk
2526 // # first call-offset is 'this' adjustment
2527 // # second call-offset is result adjustment
Sean Huntc3021132010-05-05 15:23:54 +00002528
Anders Carlsson19879c92010-03-23 17:17:29 +00002529 assert(!isa<CXXDestructorDecl>(MD) &&
2530 "Use mangleCXXDtor for destructor decls!");
Sean Huntc3021132010-05-05 15:23:54 +00002531
Anders Carlsson19879c92010-03-23 17:17:29 +00002532 CXXNameMangler Mangler(*this, Res);
2533 Mangler.getStream() << "_ZT";
2534 if (!Thunk.Return.isEmpty())
2535 Mangler.getStream() << 'c';
Sean Huntc3021132010-05-05 15:23:54 +00002536
Anders Carlsson19879c92010-03-23 17:17:29 +00002537 // Mangle the 'this' pointer adjustment.
2538 Mangler.mangleCallOffset(Thunk.This.NonVirtual, Thunk.This.VCallOffsetOffset);
Sean Huntc3021132010-05-05 15:23:54 +00002539
Anders Carlsson19879c92010-03-23 17:17:29 +00002540 // Mangle the return pointer adjustment if there is one.
2541 if (!Thunk.Return.isEmpty())
2542 Mangler.mangleCallOffset(Thunk.Return.NonVirtual,
2543 Thunk.Return.VBaseOffsetOffset);
Sean Huntc3021132010-05-05 15:23:54 +00002544
Anders Carlsson19879c92010-03-23 17:17:29 +00002545 Mangler.mangleFunctionEncoding(MD);
2546}
2547
Sean Huntc3021132010-05-05 15:23:54 +00002548void
Peter Collingbourne14110472011-01-13 18:57:25 +00002549ItaniumMangleContext::mangleCXXDtorThunk(const CXXDestructorDecl *DD,
2550 CXXDtorType Type,
2551 const ThisAdjustment &ThisAdjustment,
2552 llvm::SmallVectorImpl<char> &Res) {
Anders Carlsson19879c92010-03-23 17:17:29 +00002553 // <special-name> ::= T <call-offset> <base encoding>
2554 // # base is the nominal target function of thunk
Sean Huntc3021132010-05-05 15:23:54 +00002555
Anders Carlsson19879c92010-03-23 17:17:29 +00002556 CXXNameMangler Mangler(*this, Res, DD, Type);
2557 Mangler.getStream() << "_ZT";
2558
2559 // Mangle the 'this' pointer adjustment.
Sean Huntc3021132010-05-05 15:23:54 +00002560 Mangler.mangleCallOffset(ThisAdjustment.NonVirtual,
Anders Carlsson19879c92010-03-23 17:17:29 +00002561 ThisAdjustment.VCallOffsetOffset);
2562
2563 Mangler.mangleFunctionEncoding(DD);
2564}
2565
Daniel Dunbarc0747712009-11-21 09:12:13 +00002566/// mangleGuardVariable - Returns the mangled name for a guard variable
2567/// for the passed in VarDecl.
Peter Collingbourne14110472011-01-13 18:57:25 +00002568void ItaniumMangleContext::mangleItaniumGuardVariable(const VarDecl *D,
2569 llvm::SmallVectorImpl<char> &Res) {
Daniel Dunbarc0747712009-11-21 09:12:13 +00002570 // <special-name> ::= GV <object name> # Guard variable for one-time
2571 // # initialization
2572 CXXNameMangler Mangler(*this, Res);
2573 Mangler.getStream() << "_ZGV";
2574 Mangler.mangleName(D);
2575}
2576
Peter Collingbourne14110472011-01-13 18:57:25 +00002577void ItaniumMangleContext::mangleReferenceTemporary(const VarDecl *D,
Anders Carlsson715edf22010-06-26 16:09:40 +00002578 llvm::SmallVectorImpl<char> &Res) {
2579 // We match the GCC mangling here.
2580 // <special-name> ::= GR <object name>
2581 CXXNameMangler Mangler(*this, Res);
2582 Mangler.getStream() << "_ZGR";
2583 Mangler.mangleName(D);
2584}
2585
Peter Collingbourne14110472011-01-13 18:57:25 +00002586void ItaniumMangleContext::mangleCXXVTable(const CXXRecordDecl *RD,
2587 llvm::SmallVectorImpl<char> &Res) {
Daniel Dunbarc0747712009-11-21 09:12:13 +00002588 // <special-name> ::= TV <type> # virtual table
Daniel Dunbar94fd26d2009-11-21 09:06:22 +00002589 CXXNameMangler Mangler(*this, Res);
Daniel Dunbarc0747712009-11-21 09:12:13 +00002590 Mangler.getStream() << "_ZTV";
Douglas Gregor1b12a3b2010-05-26 05:11:13 +00002591 Mangler.mangleNameOrStandardSubstitution(RD);
Daniel Dunbar1b077112009-11-21 09:06:10 +00002592}
Mike Stump82d75b02009-11-10 01:58:37 +00002593
Peter Collingbourne14110472011-01-13 18:57:25 +00002594void ItaniumMangleContext::mangleCXXVTT(const CXXRecordDecl *RD,
2595 llvm::SmallVectorImpl<char> &Res) {
Daniel Dunbarc0747712009-11-21 09:12:13 +00002596 // <special-name> ::= TT <type> # VTT structure
Daniel Dunbar94fd26d2009-11-21 09:06:22 +00002597 CXXNameMangler Mangler(*this, Res);
Daniel Dunbarc0747712009-11-21 09:12:13 +00002598 Mangler.getStream() << "_ZTT";
Douglas Gregor1b12a3b2010-05-26 05:11:13 +00002599 Mangler.mangleNameOrStandardSubstitution(RD);
Daniel Dunbar1b077112009-11-21 09:06:10 +00002600}
Mike Stumpab3f7e92009-11-10 01:41:59 +00002601
Peter Collingbourne14110472011-01-13 18:57:25 +00002602void ItaniumMangleContext::mangleCXXCtorVTable(const CXXRecordDecl *RD,
2603 int64_t Offset,
2604 const CXXRecordDecl *Type,
2605 llvm::SmallVectorImpl<char> &Res) {
Daniel Dunbarc0747712009-11-21 09:12:13 +00002606 // <special-name> ::= TC <type> <offset number> _ <base type>
Daniel Dunbar94fd26d2009-11-21 09:06:22 +00002607 CXXNameMangler Mangler(*this, Res);
Daniel Dunbarc0747712009-11-21 09:12:13 +00002608 Mangler.getStream() << "_ZTC";
Douglas Gregor1b12a3b2010-05-26 05:11:13 +00002609 Mangler.mangleNameOrStandardSubstitution(RD);
Daniel Dunbarc0747712009-11-21 09:12:13 +00002610 Mangler.getStream() << Offset;
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002611 Mangler.getStream() << '_';
Douglas Gregor1b12a3b2010-05-26 05:11:13 +00002612 Mangler.mangleNameOrStandardSubstitution(Type);
Daniel Dunbar1b077112009-11-21 09:06:10 +00002613}
Mike Stump738f8c22009-07-31 23:15:31 +00002614
Peter Collingbourne14110472011-01-13 18:57:25 +00002615void ItaniumMangleContext::mangleCXXRTTI(QualType Ty,
2616 llvm::SmallVectorImpl<char> &Res) {
Daniel Dunbarc0747712009-11-21 09:12:13 +00002617 // <special-name> ::= TI <type> # typeinfo structure
Douglas Gregor154fe982009-12-23 22:04:40 +00002618 assert(!Ty.hasQualifiers() && "RTTI info cannot have top-level qualifiers");
Daniel Dunbar94fd26d2009-11-21 09:06:22 +00002619 CXXNameMangler Mangler(*this, Res);
Daniel Dunbarc0747712009-11-21 09:12:13 +00002620 Mangler.getStream() << "_ZTI";
2621 Mangler.mangleType(Ty);
Daniel Dunbar1b077112009-11-21 09:06:10 +00002622}
Mike Stump67795982009-11-14 00:14:13 +00002623
Peter Collingbourne14110472011-01-13 18:57:25 +00002624void ItaniumMangleContext::mangleCXXRTTIName(QualType Ty,
2625 llvm::SmallVectorImpl<char> &Res) {
Daniel Dunbarc0747712009-11-21 09:12:13 +00002626 // <special-name> ::= TS <type> # typeinfo name (null terminated byte string)
Daniel Dunbar94fd26d2009-11-21 09:06:22 +00002627 CXXNameMangler Mangler(*this, Res);
Daniel Dunbarc0747712009-11-21 09:12:13 +00002628 Mangler.getStream() << "_ZTS";
2629 Mangler.mangleType(Ty);
Mike Stumpf1216772009-07-31 18:25:34 +00002630}
Peter Collingbourne14110472011-01-13 18:57:25 +00002631
2632MangleContext *clang::createItaniumMangleContext(ASTContext &Context,
2633 Diagnostic &Diags) {
2634 return new ItaniumMangleContext(Context, Diags);
2635}