blob: 15cd7f7f8d834d735fee4292a4a5ad4498637dec [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
Douglas Gregor1aee05d2011-01-15 06:45:20 +00001020 case TemplateName::SubstTemplateTemplateParmPack: {
1021 SubstTemplateTemplateParmPackStorage *SubstPack
1022 = TN.getAsSubstTemplateTemplateParmPack();
1023 mangleTemplateParameter(SubstPack->getParameterPack()->getIndex());
1024 break;
1025 }
John McCallb6f532e2010-07-14 06:43:17 +00001026 }
1027
1028 addSubstitution(TN);
1029}
1030
Mike Stump1eb44332009-09-09 15:08:12 +00001031void
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001032CXXNameMangler::mangleOperatorName(OverloadedOperatorKind OO, unsigned Arity) {
1033 switch (OO) {
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001034 // <operator-name> ::= nw # new
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001035 case OO_New: Out << "nw"; break;
1036 // ::= na # new[]
1037 case OO_Array_New: Out << "na"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001038 // ::= dl # delete
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001039 case OO_Delete: Out << "dl"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001040 // ::= da # delete[]
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001041 case OO_Array_Delete: Out << "da"; break;
1042 // ::= ps # + (unary)
John McCall5e1e89b2010-08-18 19:18:59 +00001043 // ::= pl # + (binary or unknown)
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001044 case OO_Plus:
Anders Carlsson8257d412009-12-22 06:36:32 +00001045 Out << (Arity == 1? "ps" : "pl"); break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001046 // ::= ng # - (unary)
John McCall5e1e89b2010-08-18 19:18:59 +00001047 // ::= mi # - (binary or unknown)
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001048 case OO_Minus:
Anders Carlsson8257d412009-12-22 06:36:32 +00001049 Out << (Arity == 1? "ng" : "mi"); break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001050 // ::= ad # & (unary)
John McCall5e1e89b2010-08-18 19:18:59 +00001051 // ::= an # & (binary or unknown)
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001052 case OO_Amp:
Anders Carlsson8257d412009-12-22 06:36:32 +00001053 Out << (Arity == 1? "ad" : "an"); break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001054 // ::= de # * (unary)
John McCall5e1e89b2010-08-18 19:18:59 +00001055 // ::= ml # * (binary or unknown)
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001056 case OO_Star:
John McCall5e1e89b2010-08-18 19:18:59 +00001057 // Use binary when unknown.
Anders Carlsson8257d412009-12-22 06:36:32 +00001058 Out << (Arity == 1? "de" : "ml"); break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001059 // ::= co # ~
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001060 case OO_Tilde: Out << "co"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001061 // ::= dv # /
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001062 case OO_Slash: Out << "dv"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001063 // ::= rm # %
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001064 case OO_Percent: Out << "rm"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001065 // ::= or # |
1066 case OO_Pipe: Out << "or"; break;
1067 // ::= eo # ^
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001068 case OO_Caret: Out << "eo"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001069 // ::= aS # =
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001070 case OO_Equal: Out << "aS"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001071 // ::= pL # +=
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001072 case OO_PlusEqual: Out << "pL"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001073 // ::= mI # -=
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001074 case OO_MinusEqual: Out << "mI"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001075 // ::= mL # *=
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001076 case OO_StarEqual: Out << "mL"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001077 // ::= dV # /=
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001078 case OO_SlashEqual: Out << "dV"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001079 // ::= rM # %=
1080 case OO_PercentEqual: Out << "rM"; break;
1081 // ::= aN # &=
1082 case OO_AmpEqual: Out << "aN"; break;
1083 // ::= oR # |=
1084 case OO_PipeEqual: Out << "oR"; break;
1085 // ::= eO # ^=
1086 case OO_CaretEqual: Out << "eO"; break;
1087 // ::= ls # <<
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001088 case OO_LessLess: Out << "ls"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001089 // ::= rs # >>
1090 case OO_GreaterGreater: Out << "rs"; break;
1091 // ::= lS # <<=
1092 case OO_LessLessEqual: Out << "lS"; break;
1093 // ::= rS # >>=
1094 case OO_GreaterGreaterEqual: Out << "rS"; break;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001095 // ::= eq # ==
1096 case OO_EqualEqual: Out << "eq"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001097 // ::= ne # !=
1098 case OO_ExclaimEqual: Out << "ne"; break;
1099 // ::= lt # <
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001100 case OO_Less: Out << "lt"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001101 // ::= gt # >
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001102 case OO_Greater: Out << "gt"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001103 // ::= le # <=
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001104 case OO_LessEqual: Out << "le"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001105 // ::= ge # >=
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001106 case OO_GreaterEqual: Out << "ge"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001107 // ::= nt # !
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001108 case OO_Exclaim: Out << "nt"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001109 // ::= aa # &&
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001110 case OO_AmpAmp: Out << "aa"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001111 // ::= oo # ||
1112 case OO_PipePipe: Out << "oo"; break;
1113 // ::= pp # ++
1114 case OO_PlusPlus: Out << "pp"; break;
1115 // ::= mm # --
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001116 case OO_MinusMinus: Out << "mm"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001117 // ::= cm # ,
1118 case OO_Comma: Out << "cm"; break;
1119 // ::= pm # ->*
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001120 case OO_ArrowStar: Out << "pm"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001121 // ::= pt # ->
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001122 case OO_Arrow: Out << "pt"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001123 // ::= cl # ()
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001124 case OO_Call: Out << "cl"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001125 // ::= ix # []
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001126 case OO_Subscript: Out << "ix"; break;
Anders Carlssone170ba72009-12-14 01:45:37 +00001127
1128 // ::= qu # ?
1129 // The conditional operator can't be overloaded, but we still handle it when
1130 // mangling expressions.
1131 case OO_Conditional: Out << "qu"; break;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001132
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001133 case OO_None:
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001134 case NUM_OVERLOADED_OPERATORS:
Mike Stump1eb44332009-09-09 15:08:12 +00001135 assert(false && "Not an overloaded operator");
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001136 break;
1137 }
1138}
1139
John McCall0953e762009-09-24 19:53:00 +00001140void CXXNameMangler::mangleQualifiers(Qualifiers Quals) {
Mike Stump1eb44332009-09-09 15:08:12 +00001141 // <CV-qualifiers> ::= [r] [V] [K] # restrict (C99), volatile, const
John McCall0953e762009-09-24 19:53:00 +00001142 if (Quals.hasRestrict())
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001143 Out << 'r';
John McCall0953e762009-09-24 19:53:00 +00001144 if (Quals.hasVolatile())
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001145 Out << 'V';
John McCall0953e762009-09-24 19:53:00 +00001146 if (Quals.hasConst())
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001147 Out << 'K';
John McCall0953e762009-09-24 19:53:00 +00001148
Douglas Gregor56079f72010-06-14 23:15:08 +00001149 if (Quals.hasAddressSpace()) {
1150 // Extension:
1151 //
1152 // <type> ::= U <address-space-number>
1153 //
1154 // where <address-space-number> is a source name consisting of 'AS'
1155 // followed by the address space <number>.
1156 llvm::SmallString<64> ASString;
1157 ASString = "AS" + llvm::utostr_32(Quals.getAddressSpace());
1158 Out << 'U' << ASString.size() << ASString;
1159 }
1160
John McCall0953e762009-09-24 19:53:00 +00001161 // FIXME: For now, just drop all extension qualifiers on the floor.
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001162}
1163
Anders Carlsson7b06f6c2009-12-10 03:14:39 +00001164void CXXNameMangler::mangleObjCMethodName(const ObjCMethodDecl *MD) {
Charles Davis685b1d92010-05-26 18:25:27 +00001165 llvm::SmallString<64> Buffer;
Peter Collingbourne14110472011-01-13 18:57:25 +00001166 Context.mangleObjCMethodName(MD, Buffer);
Charles Davis685b1d92010-05-26 18:25:27 +00001167 Out << Buffer;
Anders Carlsson7b06f6c2009-12-10 03:14:39 +00001168}
1169
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001170void CXXNameMangler::mangleType(QualType T) {
Anders Carlsson4843e582009-03-10 17:07:44 +00001171 // Only operate on the canonical type!
Anders Carlssonb5404912009-10-07 01:06:45 +00001172 T = Context.getASTContext().getCanonicalType(T);
Anders Carlsson4843e582009-03-10 17:07:44 +00001173
Douglas Gregora4923eb2009-11-16 21:35:15 +00001174 bool IsSubstitutable = T.hasLocalQualifiers() || !isa<BuiltinType>(T);
Anders Carlsson76967372009-09-17 00:43:46 +00001175 if (IsSubstitutable && mangleSubstitution(T))
1176 return;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001177
Douglas Gregora4923eb2009-11-16 21:35:15 +00001178 if (Qualifiers Quals = T.getLocalQualifiers()) {
John McCall0953e762009-09-24 19:53:00 +00001179 mangleQualifiers(Quals);
1180 // Recurse: even if the qualified type isn't yet substitutable,
1181 // the unqualified type might be.
Douglas Gregora4923eb2009-11-16 21:35:15 +00001182 mangleType(T.getLocalUnqualifiedType());
Anders Carlsson76967372009-09-17 00:43:46 +00001183 } else {
1184 switch (T->getTypeClass()) {
John McCallefe6aee2009-09-05 07:56:18 +00001185#define ABSTRACT_TYPE(CLASS, PARENT)
1186#define NON_CANONICAL_TYPE(CLASS, PARENT) \
Anders Carlsson76967372009-09-17 00:43:46 +00001187 case Type::CLASS: \
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +00001188 llvm_unreachable("can't mangle non-canonical type " #CLASS "Type"); \
Anders Carlsson76967372009-09-17 00:43:46 +00001189 return;
John McCallefe6aee2009-09-05 07:56:18 +00001190#define TYPE(CLASS, PARENT) \
Anders Carlsson76967372009-09-17 00:43:46 +00001191 case Type::CLASS: \
John McCall0953e762009-09-24 19:53:00 +00001192 mangleType(static_cast<const CLASS##Type*>(T.getTypePtr())); \
Anders Carlsson76967372009-09-17 00:43:46 +00001193 break;
John McCallefe6aee2009-09-05 07:56:18 +00001194#include "clang/AST/TypeNodes.def"
Anders Carlsson76967372009-09-17 00:43:46 +00001195 }
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001196 }
Anders Carlsson76967372009-09-17 00:43:46 +00001197
1198 // Add the substitution.
1199 if (IsSubstitutable)
1200 addSubstitution(T);
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001201}
1202
Douglas Gregor1b12a3b2010-05-26 05:11:13 +00001203void CXXNameMangler::mangleNameOrStandardSubstitution(const NamedDecl *ND) {
1204 if (!mangleStandardSubstitution(ND))
1205 mangleName(ND);
1206}
1207
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001208void CXXNameMangler::mangleType(const BuiltinType *T) {
John McCallefe6aee2009-09-05 07:56:18 +00001209 // <type> ::= <builtin-type>
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001210 // <builtin-type> ::= v # void
1211 // ::= w # wchar_t
1212 // ::= b # bool
1213 // ::= c # char
1214 // ::= a # signed char
1215 // ::= h # unsigned char
1216 // ::= s # short
1217 // ::= t # unsigned short
1218 // ::= i # int
1219 // ::= j # unsigned int
1220 // ::= l # long
1221 // ::= m # unsigned long
1222 // ::= x # long long, __int64
1223 // ::= y # unsigned long long, __int64
1224 // ::= n # __int128
1225 // UNSUPPORTED: ::= o # unsigned __int128
1226 // ::= f # float
1227 // ::= d # double
1228 // ::= e # long double, __float80
1229 // UNSUPPORTED: ::= g # __float128
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001230 // UNSUPPORTED: ::= Dd # IEEE 754r decimal floating point (64 bits)
1231 // UNSUPPORTED: ::= De # IEEE 754r decimal floating point (128 bits)
1232 // UNSUPPORTED: ::= Df # IEEE 754r decimal floating point (32 bits)
1233 // UNSUPPORTED: ::= Dh # IEEE 754r half-precision floating point (16 bits)
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001234 // ::= Di # char32_t
1235 // ::= Ds # char16_t
Anders Carlssone2923682010-11-04 04:31:32 +00001236 // ::= Dn # std::nullptr_t (i.e., decltype(nullptr))
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001237 // ::= u <source-name> # vendor extended type
1238 switch (T->getKind()) {
1239 case BuiltinType::Void: Out << 'v'; break;
1240 case BuiltinType::Bool: Out << 'b'; break;
1241 case BuiltinType::Char_U: case BuiltinType::Char_S: Out << 'c'; break;
1242 case BuiltinType::UChar: Out << 'h'; break;
1243 case BuiltinType::UShort: Out << 't'; break;
1244 case BuiltinType::UInt: Out << 'j'; break;
1245 case BuiltinType::ULong: Out << 'm'; break;
1246 case BuiltinType::ULongLong: Out << 'y'; break;
Chris Lattner2df9ced2009-04-30 02:43:43 +00001247 case BuiltinType::UInt128: Out << 'o'; break;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001248 case BuiltinType::SChar: Out << 'a'; break;
Chris Lattner3f59c972010-12-25 23:25:43 +00001249 case BuiltinType::WChar_S:
1250 case BuiltinType::WChar_U: Out << 'w'; break;
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001251 case BuiltinType::Char16: Out << "Ds"; break;
1252 case BuiltinType::Char32: Out << "Di"; break;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001253 case BuiltinType::Short: Out << 's'; break;
1254 case BuiltinType::Int: Out << 'i'; break;
1255 case BuiltinType::Long: Out << 'l'; break;
1256 case BuiltinType::LongLong: Out << 'x'; break;
Chris Lattner2df9ced2009-04-30 02:43:43 +00001257 case BuiltinType::Int128: Out << 'n'; break;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001258 case BuiltinType::Float: Out << 'f'; break;
1259 case BuiltinType::Double: Out << 'd'; break;
1260 case BuiltinType::LongDouble: Out << 'e'; break;
Anders Carlssone2923682010-11-04 04:31:32 +00001261 case BuiltinType::NullPtr: Out << "Dn"; break;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001262
1263 case BuiltinType::Overload:
1264 case BuiltinType::Dependent:
Mike Stump1eb44332009-09-09 15:08:12 +00001265 assert(false &&
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001266 "Overloaded and dependent types shouldn't get to name mangling");
1267 break;
Anders Carlssone89d1592009-06-26 18:41:36 +00001268 case BuiltinType::UndeducedAuto:
1269 assert(0 && "Should not see undeduced auto here");
1270 break;
Steve Naroff9533a7f2009-07-22 17:14:51 +00001271 case BuiltinType::ObjCId: Out << "11objc_object"; break;
1272 case BuiltinType::ObjCClass: Out << "10objc_class"; break;
Fariborz Jahanian13dcd002009-11-21 19:53:08 +00001273 case BuiltinType::ObjCSel: Out << "13objc_selector"; break;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001274 }
1275}
1276
John McCallefe6aee2009-09-05 07:56:18 +00001277// <type> ::= <function-type>
1278// <function-type> ::= F [Y] <bare-function-type> E
1279void CXXNameMangler::mangleType(const FunctionProtoType *T) {
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001280 Out << 'F';
Mike Stumpf5408fe2009-05-16 07:57:57 +00001281 // FIXME: We don't have enough information in the AST to produce the 'Y'
1282 // encoding for extern "C" function types.
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001283 mangleBareFunctionType(T, /*MangleReturnType=*/true);
1284 Out << 'E';
1285}
John McCallefe6aee2009-09-05 07:56:18 +00001286void CXXNameMangler::mangleType(const FunctionNoProtoType *T) {
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +00001287 llvm_unreachable("Can't mangle K&R function prototypes");
John McCallefe6aee2009-09-05 07:56:18 +00001288}
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001289void CXXNameMangler::mangleBareFunctionType(const FunctionType *T,
1290 bool MangleReturnType) {
John McCallefe6aee2009-09-05 07:56:18 +00001291 // We should never be mangling something without a prototype.
1292 const FunctionProtoType *Proto = cast<FunctionProtoType>(T);
1293
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001294 // <bare-function-type> ::= <signature type>+
1295 if (MangleReturnType)
John McCallefe6aee2009-09-05 07:56:18 +00001296 mangleType(Proto->getResultType());
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001297
Anders Carlsson93296682010-06-02 04:40:13 +00001298 if (Proto->getNumArgs() == 0 && !Proto->isVariadic()) {
Eli Friedmana7e68452010-08-22 01:00:03 +00001299 // <builtin-type> ::= v # void
Anders Carlssonc6c91bc2009-04-01 00:15:23 +00001300 Out << 'v';
1301 return;
1302 }
Mike Stump1eb44332009-09-09 15:08:12 +00001303
Douglas Gregor72564e72009-02-26 23:50:07 +00001304 for (FunctionProtoType::arg_type_iterator Arg = Proto->arg_type_begin(),
Mike Stump1eb44332009-09-09 15:08:12 +00001305 ArgEnd = Proto->arg_type_end();
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001306 Arg != ArgEnd; ++Arg)
1307 mangleType(*Arg);
Douglas Gregor219cc612009-02-13 01:28:03 +00001308
1309 // <builtin-type> ::= z # ellipsis
1310 if (Proto->isVariadic())
1311 Out << 'z';
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001312}
1313
John McCallefe6aee2009-09-05 07:56:18 +00001314// <type> ::= <class-enum-type>
Mike Stump1eb44332009-09-09 15:08:12 +00001315// <class-enum-type> ::= <name>
John McCalled976492009-12-04 22:46:56 +00001316void CXXNameMangler::mangleType(const UnresolvedUsingType *T) {
1317 mangleName(T->getDecl());
1318}
1319
1320// <type> ::= <class-enum-type>
1321// <class-enum-type> ::= <name>
John McCallefe6aee2009-09-05 07:56:18 +00001322void CXXNameMangler::mangleType(const EnumType *T) {
1323 mangleType(static_cast<const TagType*>(T));
1324}
1325void CXXNameMangler::mangleType(const RecordType *T) {
1326 mangleType(static_cast<const TagType*>(T));
1327}
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001328void CXXNameMangler::mangleType(const TagType *T) {
Eli Friedmanecb7e932009-12-11 18:00:57 +00001329 mangleName(T->getDecl());
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001330}
1331
John McCallefe6aee2009-09-05 07:56:18 +00001332// <type> ::= <array-type>
1333// <array-type> ::= A <positive dimension number> _ <element type>
1334// ::= A [<dimension expression>] _ <element type>
1335void CXXNameMangler::mangleType(const ConstantArrayType *T) {
1336 Out << 'A' << T->getSize() << '_';
1337 mangleType(T->getElementType());
1338}
1339void CXXNameMangler::mangleType(const VariableArrayType *T) {
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001340 Out << 'A';
Fariborz Jahanian7281d1f2010-11-02 16:54:00 +00001341 // decayed vla types (size 0) will just be skipped.
1342 if (T->getSizeExpr())
1343 mangleExpression(T->getSizeExpr());
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001344 Out << '_';
1345 mangleType(T->getElementType());
1346}
John McCallefe6aee2009-09-05 07:56:18 +00001347void CXXNameMangler::mangleType(const DependentSizedArrayType *T) {
1348 Out << 'A';
1349 mangleExpression(T->getSizeExpr());
1350 Out << '_';
1351 mangleType(T->getElementType());
1352}
1353void CXXNameMangler::mangleType(const IncompleteArrayType *T) {
Nick Lewycky271b6652010-09-05 03:40:33 +00001354 Out << "A_";
John McCallefe6aee2009-09-05 07:56:18 +00001355 mangleType(T->getElementType());
1356}
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001357
John McCallefe6aee2009-09-05 07:56:18 +00001358// <type> ::= <pointer-to-member-type>
1359// <pointer-to-member-type> ::= M <class type> <member type>
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001360void CXXNameMangler::mangleType(const MemberPointerType *T) {
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001361 Out << 'M';
1362 mangleType(QualType(T->getClass(), 0));
Anders Carlsson0e650012009-05-17 17:41:20 +00001363 QualType PointeeType = T->getPointeeType();
1364 if (const FunctionProtoType *FPT = dyn_cast<FunctionProtoType>(PointeeType)) {
John McCall0953e762009-09-24 19:53:00 +00001365 mangleQualifiers(Qualifiers::fromCVRMask(FPT->getTypeQuals()));
Anders Carlsson0e650012009-05-17 17:41:20 +00001366 mangleType(FPT);
Anders Carlsson9d85b722010-06-02 04:29:50 +00001367
1368 // Itanium C++ ABI 5.1.8:
1369 //
1370 // The type of a non-static member function is considered to be different,
1371 // for the purposes of substitution, from the type of a namespace-scope or
1372 // static member function whose type appears similar. The types of two
1373 // non-static member functions are considered to be different, for the
1374 // purposes of substitution, if the functions are members of different
1375 // classes. In other words, for the purposes of substitution, the class of
1376 // which the function is a member is considered part of the type of
1377 // function.
1378
1379 // We increment the SeqID here to emulate adding an entry to the
1380 // substitution table. We can't actually add it because we don't want this
1381 // particular function type to be substituted.
1382 ++SeqID;
Mike Stump1eb44332009-09-09 15:08:12 +00001383 } else
Anders Carlsson0e650012009-05-17 17:41:20 +00001384 mangleType(PointeeType);
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001385}
1386
John McCallefe6aee2009-09-05 07:56:18 +00001387// <type> ::= <template-param>
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001388void CXXNameMangler::mangleType(const TemplateTypeParmType *T) {
Anders Carlsson0ccdf8d2009-09-27 00:38:53 +00001389 mangleTemplateParameter(T->getIndex());
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001390}
1391
Douglas Gregorc3069d62011-01-14 02:55:32 +00001392// <type> ::= <template-param>
1393void CXXNameMangler::mangleType(const SubstTemplateTypeParmPackType *T) {
1394 mangleTemplateParameter(T->getReplacedParameter()->getIndex());
1395}
1396
John McCallefe6aee2009-09-05 07:56:18 +00001397// <type> ::= P <type> # pointer-to
1398void CXXNameMangler::mangleType(const PointerType *T) {
1399 Out << 'P';
1400 mangleType(T->getPointeeType());
1401}
1402void CXXNameMangler::mangleType(const ObjCObjectPointerType *T) {
1403 Out << 'P';
1404 mangleType(T->getPointeeType());
1405}
1406
1407// <type> ::= R <type> # reference-to
1408void CXXNameMangler::mangleType(const LValueReferenceType *T) {
1409 Out << 'R';
1410 mangleType(T->getPointeeType());
1411}
1412
1413// <type> ::= O <type> # rvalue reference-to (C++0x)
1414void CXXNameMangler::mangleType(const RValueReferenceType *T) {
1415 Out << 'O';
1416 mangleType(T->getPointeeType());
1417}
1418
1419// <type> ::= C <type> # complex pair (C 2000)
1420void CXXNameMangler::mangleType(const ComplexType *T) {
1421 Out << 'C';
1422 mangleType(T->getElementType());
1423}
1424
Bob Wilsonc7df92d2010-11-12 17:24:43 +00001425// ARM's ABI for Neon vector types specifies that they should be mangled as
Bob Wilson57147a82010-11-16 00:32:18 +00001426// if they are structs (to match ARM's initial implementation). The
1427// vector type must be one of the special types predefined by ARM.
1428void CXXNameMangler::mangleNeonVectorType(const VectorType *T) {
Bob Wilsonc7df92d2010-11-12 17:24:43 +00001429 QualType EltType = T->getElementType();
Bob Wilson57147a82010-11-16 00:32:18 +00001430 assert(EltType->isBuiltinType() && "Neon vector element not a BuiltinType");
Bob Wilsonc7df92d2010-11-12 17:24:43 +00001431 const char *EltName = 0;
Bob Wilson491328c2010-11-12 17:24:46 +00001432 if (T->getVectorKind() == VectorType::NeonPolyVector) {
1433 switch (cast<BuiltinType>(EltType)->getKind()) {
Bob Wilson4cfaa5d2010-11-12 17:24:49 +00001434 case BuiltinType::SChar: EltName = "poly8_t"; break;
1435 case BuiltinType::Short: EltName = "poly16_t"; break;
Bob Wilson57147a82010-11-16 00:32:18 +00001436 default: llvm_unreachable("unexpected Neon polynomial vector element type");
Bob Wilson491328c2010-11-12 17:24:46 +00001437 }
1438 } else {
1439 switch (cast<BuiltinType>(EltType)->getKind()) {
Bob Wilson4cfaa5d2010-11-12 17:24:49 +00001440 case BuiltinType::SChar: EltName = "int8_t"; break;
1441 case BuiltinType::UChar: EltName = "uint8_t"; break;
1442 case BuiltinType::Short: EltName = "int16_t"; break;
1443 case BuiltinType::UShort: EltName = "uint16_t"; break;
1444 case BuiltinType::Int: EltName = "int32_t"; break;
1445 case BuiltinType::UInt: EltName = "uint32_t"; break;
1446 case BuiltinType::LongLong: EltName = "int64_t"; break;
1447 case BuiltinType::ULongLong: EltName = "uint64_t"; break;
1448 case BuiltinType::Float: EltName = "float32_t"; break;
Bob Wilson57147a82010-11-16 00:32:18 +00001449 default: llvm_unreachable("unexpected Neon vector element type");
Bob Wilson491328c2010-11-12 17:24:46 +00001450 }
Bob Wilsonc7df92d2010-11-12 17:24:43 +00001451 }
1452 const char *BaseName = 0;
Bob Wilson4cfaa5d2010-11-12 17:24:49 +00001453 unsigned BitSize = (T->getNumElements() *
Bob Wilson3a723022010-11-16 00:32:12 +00001454 getASTContext().getTypeSize(EltType));
Bob Wilsonc7df92d2010-11-12 17:24:43 +00001455 if (BitSize == 64)
1456 BaseName = "__simd64_";
Bob Wilson57147a82010-11-16 00:32:18 +00001457 else {
1458 assert(BitSize == 128 && "Neon vector type not 64 or 128 bits");
Bob Wilsonc7df92d2010-11-12 17:24:43 +00001459 BaseName = "__simd128_";
Bob Wilson57147a82010-11-16 00:32:18 +00001460 }
Bob Wilsonc7df92d2010-11-12 17:24:43 +00001461 Out << strlen(BaseName) + strlen(EltName);
1462 Out << BaseName << EltName;
Bob Wilsonc7df92d2010-11-12 17:24:43 +00001463}
1464
John McCallefe6aee2009-09-05 07:56:18 +00001465// GNU extension: vector types
Chris Lattner788b0fd2010-06-23 06:00:24 +00001466// <type> ::= <vector-type>
1467// <vector-type> ::= Dv <positive dimension number> _
1468// <extended element type>
1469// ::= Dv [<dimension expression>] _ <element type>
1470// <extended element type> ::= <element type>
1471// ::= p # AltiVec vector pixel
John McCallefe6aee2009-09-05 07:56:18 +00001472void CXXNameMangler::mangleType(const VectorType *T) {
Bob Wilson491328c2010-11-12 17:24:46 +00001473 if ((T->getVectorKind() == VectorType::NeonVector ||
Bob Wilson57147a82010-11-16 00:32:18 +00001474 T->getVectorKind() == VectorType::NeonPolyVector)) {
1475 mangleNeonVectorType(T);
Bob Wilsonc7df92d2010-11-12 17:24:43 +00001476 return;
Bob Wilson57147a82010-11-16 00:32:18 +00001477 }
Nick Lewycky0e5f0672010-03-26 07:18:04 +00001478 Out << "Dv" << T->getNumElements() << '_';
Bob Wilsone86d78c2010-11-10 21:56:12 +00001479 if (T->getVectorKind() == VectorType::AltiVecPixel)
Chris Lattner788b0fd2010-06-23 06:00:24 +00001480 Out << 'p';
Bob Wilsone86d78c2010-11-10 21:56:12 +00001481 else if (T->getVectorKind() == VectorType::AltiVecBool)
Chris Lattner788b0fd2010-06-23 06:00:24 +00001482 Out << 'b';
1483 else
1484 mangleType(T->getElementType());
John McCallefe6aee2009-09-05 07:56:18 +00001485}
1486void CXXNameMangler::mangleType(const ExtVectorType *T) {
1487 mangleType(static_cast<const VectorType*>(T));
1488}
1489void CXXNameMangler::mangleType(const DependentSizedExtVectorType *T) {
Nick Lewycky0e5f0672010-03-26 07:18:04 +00001490 Out << "Dv";
1491 mangleExpression(T->getSizeExpr());
1492 Out << '_';
John McCallefe6aee2009-09-05 07:56:18 +00001493 mangleType(T->getElementType());
1494}
1495
Douglas Gregor7536dd52010-12-20 02:24:11 +00001496void CXXNameMangler::mangleType(const PackExpansionType *T) {
Douglas Gregor4fc48662011-01-13 16:39:34 +00001497 // <type> ::= Dp <type> # pack expansion (C++0x)
Douglas Gregor255c2692011-01-13 17:44:36 +00001498 Out << "Dp";
Douglas Gregor7536dd52010-12-20 02:24:11 +00001499 mangleType(T->getPattern());
1500}
1501
Anders Carlssona40c5e42009-03-07 22:03:21 +00001502void CXXNameMangler::mangleType(const ObjCInterfaceType *T) {
1503 mangleSourceName(T->getDecl()->getIdentifier());
1504}
1505
John McCallc12c5bb2010-05-15 11:32:37 +00001506void CXXNameMangler::mangleType(const ObjCObjectType *T) {
John McCallc00c1f62010-05-15 17:06:29 +00001507 // We don't allow overloading by different protocol qualification,
1508 // so mangling them isn't necessary.
John McCallc12c5bb2010-05-15 11:32:37 +00001509 mangleType(T->getBaseType());
1510}
1511
John McCallefe6aee2009-09-05 07:56:18 +00001512void CXXNameMangler::mangleType(const BlockPointerType *T) {
Anders Carlssonf28c6872009-12-23 22:31:44 +00001513 Out << "U13block_pointer";
1514 mangleType(T->getPointeeType());
John McCallefe6aee2009-09-05 07:56:18 +00001515}
1516
John McCall31f17ec2010-04-27 00:57:59 +00001517void CXXNameMangler::mangleType(const InjectedClassNameType *T) {
1518 // Mangle injected class name types as if the user had written the
1519 // specialization out fully. It may not actually be possible to see
1520 // this mangling, though.
1521 mangleType(T->getInjectedSpecializationType());
1522}
1523
John McCallefe6aee2009-09-05 07:56:18 +00001524void CXXNameMangler::mangleType(const TemplateSpecializationType *T) {
Douglas Gregor1e9268e2010-04-28 05:58:56 +00001525 if (TemplateDecl *TD = T->getTemplateName().getAsTemplateDecl()) {
1526 mangleName(TD, T->getArgs(), T->getNumArgs());
1527 } else {
1528 if (mangleSubstitution(QualType(T, 0)))
1529 return;
Sean Huntc3021132010-05-05 15:23:54 +00001530
Douglas Gregor1e9268e2010-04-28 05:58:56 +00001531 mangleTemplatePrefix(T->getTemplateName());
Sean Huntc3021132010-05-05 15:23:54 +00001532
Douglas Gregor1e9268e2010-04-28 05:58:56 +00001533 // FIXME: GCC does not appear to mangle the template arguments when
1534 // the template in question is a dependent template name. Should we
1535 // emulate that badness?
1536 mangleTemplateArgs(T->getTemplateName(), T->getArgs(), T->getNumArgs());
1537 addSubstitution(QualType(T, 0));
1538 }
John McCallefe6aee2009-09-05 07:56:18 +00001539}
1540
Douglas Gregor4714c122010-03-31 17:34:00 +00001541void CXXNameMangler::mangleType(const DependentNameType *T) {
Anders Carlssonae352482009-09-26 02:26:02 +00001542 // Typename types are always nested
1543 Out << 'N';
John McCall33500952010-06-11 00:33:02 +00001544 mangleUnresolvedScope(T->getQualifier());
1545 mangleSourceName(T->getIdentifier());
1546 Out << 'E';
1547}
John McCall6ab30e02010-06-09 07:26:17 +00001548
John McCall33500952010-06-11 00:33:02 +00001549void CXXNameMangler::mangleType(const DependentTemplateSpecializationType *T) {
1550 // Dependently-scoped template types are always nested
1551 Out << 'N';
1552
1553 // TODO: avoid making this TemplateName.
1554 TemplateName Prefix =
1555 getASTContext().getDependentTemplateName(T->getQualifier(),
1556 T->getIdentifier());
1557 mangleTemplatePrefix(Prefix);
1558
1559 // FIXME: GCC does not appear to mangle the template arguments when
1560 // the template in question is a dependent template name. Should we
1561 // emulate that badness?
1562 mangleTemplateArgs(Prefix, T->getArgs(), T->getNumArgs());
Anders Carlssonae352482009-09-26 02:26:02 +00001563 Out << 'E';
John McCallefe6aee2009-09-05 07:56:18 +00001564}
1565
John McCallad5e7382010-03-01 23:49:17 +00001566void CXXNameMangler::mangleType(const TypeOfType *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 TypeOfExprType *T) {
1573 // FIXME: this is pretty unsatisfactory, but there isn't an obvious
1574 // "extension with parameters" mangling.
1575 Out << "u6typeof";
1576}
1577
1578void CXXNameMangler::mangleType(const DecltypeType *T) {
1579 Expr *E = T->getUnderlyingExpr();
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001580
John McCallad5e7382010-03-01 23:49:17 +00001581 // type ::= Dt <expression> E # decltype of an id-expression
1582 // # or class member access
1583 // ::= DT <expression> E # decltype of an expression
1584
1585 // This purports to be an exhaustive list of id-expressions and
1586 // class member accesses. Note that we do not ignore parentheses;
1587 // parentheses change the semantics of decltype for these
1588 // expressions (and cause the mangler to use the other form).
1589 if (isa<DeclRefExpr>(E) ||
1590 isa<MemberExpr>(E) ||
1591 isa<UnresolvedLookupExpr>(E) ||
1592 isa<DependentScopeDeclRefExpr>(E) ||
1593 isa<CXXDependentScopeMemberExpr>(E) ||
1594 isa<UnresolvedMemberExpr>(E))
1595 Out << "Dt";
1596 else
1597 Out << "DT";
1598 mangleExpression(E);
1599 Out << 'E';
1600}
1601
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001602void CXXNameMangler::mangleIntegerLiteral(QualType T,
Anders Carlssone170ba72009-12-14 01:45:37 +00001603 const llvm::APSInt &Value) {
1604 // <expr-primary> ::= L <type> <value number> E # integer literal
1605 Out << 'L';
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001606
Anders Carlssone170ba72009-12-14 01:45:37 +00001607 mangleType(T);
1608 if (T->isBooleanType()) {
1609 // Boolean values are encoded as 0/1.
1610 Out << (Value.getBoolValue() ? '1' : '0');
1611 } else {
John McCall0512e482010-07-14 04:20:34 +00001612 mangleNumber(Value);
Anders Carlssone170ba72009-12-14 01:45:37 +00001613 }
1614 Out << 'E';
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001615
Anders Carlssone170ba72009-12-14 01:45:37 +00001616}
1617
John McCall2f27bf82010-02-04 02:56:29 +00001618/// Mangles a member expression. Implicit accesses are not handled,
1619/// but that should be okay, because you shouldn't be able to
1620/// make an implicit access in a function template declaration.
John McCall2f27bf82010-02-04 02:56:29 +00001621void CXXNameMangler::mangleMemberExpr(const Expr *Base,
1622 bool IsArrow,
1623 NestedNameSpecifier *Qualifier,
1624 DeclarationName Member,
1625 unsigned Arity) {
John McCalle1e342f2010-03-01 19:12:25 +00001626 // gcc-4.4 uses 'dt' for dot expressions, which is reasonable.
1627 // OTOH, gcc also mangles the name as an expression.
1628 Out << (IsArrow ? "pt" : "dt");
John McCall2f27bf82010-02-04 02:56:29 +00001629 mangleExpression(Base);
1630 mangleUnresolvedName(Qualifier, Member, Arity);
1631}
1632
John McCall5e1e89b2010-08-18 19:18:59 +00001633void CXXNameMangler::mangleExpression(const Expr *E, unsigned Arity) {
Anders Carlssond553f8c2009-09-21 01:21:10 +00001634 // <expression> ::= <unary operator-name> <expression>
John McCall09cc1412010-02-03 00:55:45 +00001635 // ::= <binary operator-name> <expression> <expression>
1636 // ::= <trinary operator-name> <expression> <expression> <expression>
Eli Friedmana7e68452010-08-22 01:00:03 +00001637 // ::= cl <expression>* E # call
Anders Carlssond553f8c2009-09-21 01:21:10 +00001638 // ::= cv <type> expression # conversion with one argument
1639 // ::= cv <type> _ <expression>* E # conversion with a different number of arguments
Eli Friedmana7e68452010-08-22 01:00:03 +00001640 // ::= st <type> # sizeof (a type)
Anders Carlssond553f8c2009-09-21 01:21:10 +00001641 // ::= at <type> # alignof (a type)
1642 // ::= <template-param>
1643 // ::= <function-param>
1644 // ::= sr <type> <unqualified-name> # dependent name
1645 // ::= sr <type> <unqualified-name> <template-args> # dependent template-id
1646 // ::= sZ <template-param> # size of a parameter pack
Douglas Gregor4fc48662011-01-13 16:39:34 +00001647 // ::= sZ <function-param> # size of a function parameter pack
John McCall09cc1412010-02-03 00:55:45 +00001648 // ::= <expr-primary>
John McCall1dd73832010-02-04 01:42:13 +00001649 // <expr-primary> ::= L <type> <value number> E # integer literal
1650 // ::= L <type <value float> E # floating literal
1651 // ::= L <mangled-name> E # external name
Anders Carlssond553f8c2009-09-21 01:21:10 +00001652 switch (E->getStmtClass()) {
John McCall6ae1f352010-04-09 22:26:14 +00001653 case Expr::NoStmtClass:
1654#define EXPR(Type, Base)
1655#define STMT(Type, Base) \
1656 case Expr::Type##Class:
Sean Hunt4bfe1962010-05-05 15:24:00 +00001657#include "clang/AST/StmtNodes.inc"
John McCall0512e482010-07-14 04:20:34 +00001658 // fallthrough
1659
1660 // These all can only appear in local or variable-initialization
1661 // contexts and so should never appear in a mangling.
1662 case Expr::AddrLabelExprClass:
1663 case Expr::BlockDeclRefExprClass:
1664 case Expr::CXXThisExprClass:
1665 case Expr::DesignatedInitExprClass:
1666 case Expr::ImplicitValueInitExprClass:
1667 case Expr::InitListExprClass:
1668 case Expr::ParenListExprClass:
1669 case Expr::CXXScalarValueInitExprClass:
John McCall09cc1412010-02-03 00:55:45 +00001670 llvm_unreachable("unexpected statement kind");
1671 break;
1672
John McCall0512e482010-07-14 04:20:34 +00001673 // FIXME: invent manglings for all these.
1674 case Expr::BlockExprClass:
1675 case Expr::CXXPseudoDestructorExprClass:
1676 case Expr::ChooseExprClass:
1677 case Expr::CompoundLiteralExprClass:
1678 case Expr::ExtVectorElementExprClass:
1679 case Expr::ObjCEncodeExprClass:
John McCall0512e482010-07-14 04:20:34 +00001680 case Expr::ObjCIsaExprClass:
1681 case Expr::ObjCIvarRefExprClass:
1682 case Expr::ObjCMessageExprClass:
1683 case Expr::ObjCPropertyRefExprClass:
1684 case Expr::ObjCProtocolExprClass:
1685 case Expr::ObjCSelectorExprClass:
1686 case Expr::ObjCStringLiteralClass:
John McCall0512e482010-07-14 04:20:34 +00001687 case Expr::OffsetOfExprClass:
1688 case Expr::PredefinedExprClass:
1689 case Expr::ShuffleVectorExprClass:
1690 case Expr::StmtExprClass:
John McCall0512e482010-07-14 04:20:34 +00001691 case Expr::UnaryTypeTraitExprClass:
Francois Pichet6ad6f282010-12-07 00:08:36 +00001692 case Expr::BinaryTypeTraitExprClass:
Francois Pichet9be88402010-09-08 23:47:05 +00001693 case Expr::VAArgExprClass:
Sebastian Redl2e156222010-09-10 20:55:43 +00001694 case Expr::CXXUuidofExprClass:
1695 case Expr::CXXNoexceptExprClass: {
John McCall6ae1f352010-04-09 22:26:14 +00001696 // As bad as this diagnostic is, it's better than crashing.
1697 Diagnostic &Diags = Context.getDiags();
1698 unsigned DiagID = Diags.getCustomDiagID(Diagnostic::Error,
1699 "cannot yet mangle expression type %0");
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +00001700 Diags.Report(E->getExprLoc(), DiagID)
John McCall739bf092010-04-10 09:39:25 +00001701 << E->getStmtClassName() << E->getSourceRange();
John McCall6ae1f352010-04-09 22:26:14 +00001702 break;
1703 }
1704
John McCall7cd7d1a2010-11-15 23:31:06 +00001705 case Expr::OpaqueValueExprClass:
1706 llvm_unreachable("cannot mangle opaque value; mangling wrong thing?");
1707
John McCall0512e482010-07-14 04:20:34 +00001708 case Expr::CXXDefaultArgExprClass:
John McCall5e1e89b2010-08-18 19:18:59 +00001709 mangleExpression(cast<CXXDefaultArgExpr>(E)->getExpr(), Arity);
John McCall0512e482010-07-14 04:20:34 +00001710 break;
1711
1712 case Expr::CXXMemberCallExprClass: // fallthrough
John McCall1dd73832010-02-04 01:42:13 +00001713 case Expr::CallExprClass: {
1714 const CallExpr *CE = cast<CallExpr>(E);
1715 Out << "cl";
John McCall5e1e89b2010-08-18 19:18:59 +00001716 mangleExpression(CE->getCallee(), CE->getNumArgs());
John McCall1dd73832010-02-04 01:42:13 +00001717 for (unsigned I = 0, N = CE->getNumArgs(); I != N; ++I)
1718 mangleExpression(CE->getArg(I));
Benjamin Kramer35f59b62010-04-10 16:03:31 +00001719 Out << 'E';
John McCall09cc1412010-02-03 00:55:45 +00001720 break;
John McCall1dd73832010-02-04 01:42:13 +00001721 }
John McCall09cc1412010-02-03 00:55:45 +00001722
John McCall0512e482010-07-14 04:20:34 +00001723 case Expr::CXXNewExprClass: {
1724 // Proposal from David Vandervoorde, 2010.06.30
1725 const CXXNewExpr *New = cast<CXXNewExpr>(E);
1726 if (New->isGlobalNew()) Out << "gs";
1727 Out << (New->isArray() ? "na" : "nw");
1728 for (CXXNewExpr::const_arg_iterator I = New->placement_arg_begin(),
1729 E = New->placement_arg_end(); I != E; ++I)
1730 mangleExpression(*I);
1731 Out << '_';
1732 mangleType(New->getAllocatedType());
1733 if (New->hasInitializer()) {
1734 Out << "pi";
1735 for (CXXNewExpr::const_arg_iterator I = New->constructor_arg_begin(),
1736 E = New->constructor_arg_end(); I != E; ++I)
1737 mangleExpression(*I);
1738 }
1739 Out << 'E';
1740 break;
1741 }
1742
John McCall2f27bf82010-02-04 02:56:29 +00001743 case Expr::MemberExprClass: {
1744 const MemberExpr *ME = cast<MemberExpr>(E);
1745 mangleMemberExpr(ME->getBase(), ME->isArrow(),
1746 ME->getQualifier(), ME->getMemberDecl()->getDeclName(),
John McCall5e1e89b2010-08-18 19:18:59 +00001747 Arity);
John McCall2f27bf82010-02-04 02:56:29 +00001748 break;
1749 }
1750
1751 case Expr::UnresolvedMemberExprClass: {
1752 const UnresolvedMemberExpr *ME = cast<UnresolvedMemberExpr>(E);
1753 mangleMemberExpr(ME->getBase(), ME->isArrow(),
1754 ME->getQualifier(), ME->getMemberName(),
John McCall5e1e89b2010-08-18 19:18:59 +00001755 Arity);
John McCall6dbce192010-08-20 00:17:19 +00001756 if (ME->hasExplicitTemplateArgs())
1757 mangleTemplateArgs(ME->getExplicitTemplateArgs());
John McCall2f27bf82010-02-04 02:56:29 +00001758 break;
1759 }
1760
1761 case Expr::CXXDependentScopeMemberExprClass: {
1762 const CXXDependentScopeMemberExpr *ME
1763 = cast<CXXDependentScopeMemberExpr>(E);
1764 mangleMemberExpr(ME->getBase(), ME->isArrow(),
1765 ME->getQualifier(), ME->getMember(),
John McCall5e1e89b2010-08-18 19:18:59 +00001766 Arity);
John McCall6dbce192010-08-20 00:17:19 +00001767 if (ME->hasExplicitTemplateArgs())
1768 mangleTemplateArgs(ME->getExplicitTemplateArgs());
John McCall2f27bf82010-02-04 02:56:29 +00001769 break;
1770 }
1771
John McCall1dd73832010-02-04 01:42:13 +00001772 case Expr::UnresolvedLookupExprClass: {
John McCalla3218e72010-02-04 01:48:38 +00001773 // The ABI doesn't cover how to mangle overload sets, so we mangle
1774 // using something as close as possible to the original lookup
1775 // expression.
John McCall1dd73832010-02-04 01:42:13 +00001776 const UnresolvedLookupExpr *ULE = cast<UnresolvedLookupExpr>(E);
John McCall5e1e89b2010-08-18 19:18:59 +00001777 mangleUnresolvedName(ULE->getQualifier(), ULE->getName(), Arity);
John McCall6dbce192010-08-20 00:17:19 +00001778 if (ULE->hasExplicitTemplateArgs())
1779 mangleTemplateArgs(ULE->getExplicitTemplateArgs());
John McCall1dd73832010-02-04 01:42:13 +00001780 break;
1781 }
1782
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001783 case Expr::CXXUnresolvedConstructExprClass: {
John McCall1dd73832010-02-04 01:42:13 +00001784 const CXXUnresolvedConstructExpr *CE = cast<CXXUnresolvedConstructExpr>(E);
1785 unsigned N = CE->arg_size();
1786
1787 Out << "cv";
1788 mangleType(CE->getType());
Benjamin Kramer35f59b62010-04-10 16:03:31 +00001789 if (N != 1) Out << '_';
John McCall1dd73832010-02-04 01:42:13 +00001790 for (unsigned I = 0; I != N; ++I) mangleExpression(CE->getArg(I));
Benjamin Kramer35f59b62010-04-10 16:03:31 +00001791 if (N != 1) Out << 'E';
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001792 break;
John McCall1dd73832010-02-04 01:42:13 +00001793 }
John McCall09cc1412010-02-03 00:55:45 +00001794
John McCall1dd73832010-02-04 01:42:13 +00001795 case Expr::CXXTemporaryObjectExprClass:
1796 case Expr::CXXConstructExprClass: {
1797 const CXXConstructExpr *CE = cast<CXXConstructExpr>(E);
1798 unsigned N = CE->getNumArgs();
1799
1800 Out << "cv";
1801 mangleType(CE->getType());
Benjamin Kramer35f59b62010-04-10 16:03:31 +00001802 if (N != 1) Out << '_';
John McCall1dd73832010-02-04 01:42:13 +00001803 for (unsigned I = 0; I != N; ++I) mangleExpression(CE->getArg(I));
Benjamin Kramer35f59b62010-04-10 16:03:31 +00001804 if (N != 1) Out << 'E';
John McCall09cc1412010-02-03 00:55:45 +00001805 break;
John McCall1dd73832010-02-04 01:42:13 +00001806 }
1807
1808 case Expr::SizeOfAlignOfExprClass: {
1809 const SizeOfAlignOfExpr *SAE = cast<SizeOfAlignOfExpr>(E);
Benjamin Kramer35f59b62010-04-10 16:03:31 +00001810 if (SAE->isSizeOf()) Out << 's';
1811 else Out << 'a';
John McCall1dd73832010-02-04 01:42:13 +00001812 if (SAE->isArgumentType()) {
Benjamin Kramer35f59b62010-04-10 16:03:31 +00001813 Out << 't';
John McCall1dd73832010-02-04 01:42:13 +00001814 mangleType(SAE->getArgumentType());
1815 } else {
Benjamin Kramer35f59b62010-04-10 16:03:31 +00001816 Out << 'z';
John McCall1dd73832010-02-04 01:42:13 +00001817 mangleExpression(SAE->getArgumentExpr());
1818 }
1819 break;
1820 }
Anders Carlssona7694082009-11-06 02:50:19 +00001821
John McCall0512e482010-07-14 04:20:34 +00001822 case Expr::CXXThrowExprClass: {
1823 const CXXThrowExpr *TE = cast<CXXThrowExpr>(E);
1824
1825 // Proposal from David Vandervoorde, 2010.06.30
1826 if (TE->getSubExpr()) {
1827 Out << "tw";
1828 mangleExpression(TE->getSubExpr());
1829 } else {
1830 Out << "tr";
1831 }
1832 break;
1833 }
1834
1835 case Expr::CXXTypeidExprClass: {
1836 const CXXTypeidExpr *TIE = cast<CXXTypeidExpr>(E);
1837
1838 // Proposal from David Vandervoorde, 2010.06.30
1839 if (TIE->isTypeOperand()) {
1840 Out << "ti";
1841 mangleType(TIE->getTypeOperand());
1842 } else {
1843 Out << "te";
1844 mangleExpression(TIE->getExprOperand());
1845 }
1846 break;
1847 }
1848
1849 case Expr::CXXDeleteExprClass: {
1850 const CXXDeleteExpr *DE = cast<CXXDeleteExpr>(E);
1851
1852 // Proposal from David Vandervoorde, 2010.06.30
1853 if (DE->isGlobalDelete()) Out << "gs";
1854 Out << (DE->isArrayForm() ? "da" : "dl");
1855 mangleExpression(DE->getArgument());
1856 break;
1857 }
1858
Anders Carlssone170ba72009-12-14 01:45:37 +00001859 case Expr::UnaryOperatorClass: {
1860 const UnaryOperator *UO = cast<UnaryOperator>(E);
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001861 mangleOperatorName(UnaryOperator::getOverloadedOperator(UO->getOpcode()),
Anders Carlssone170ba72009-12-14 01:45:37 +00001862 /*Arity=*/1);
1863 mangleExpression(UO->getSubExpr());
1864 break;
1865 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001866
John McCall0512e482010-07-14 04:20:34 +00001867 case Expr::ArraySubscriptExprClass: {
1868 const ArraySubscriptExpr *AE = cast<ArraySubscriptExpr>(E);
1869
1870 // Array subscript is treated as a syntactically wierd form of
1871 // binary operator.
1872 Out << "ix";
1873 mangleExpression(AE->getLHS());
1874 mangleExpression(AE->getRHS());
1875 break;
1876 }
1877
1878 case Expr::CompoundAssignOperatorClass: // fallthrough
Anders Carlssone170ba72009-12-14 01:45:37 +00001879 case Expr::BinaryOperatorClass: {
1880 const BinaryOperator *BO = cast<BinaryOperator>(E);
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001881 mangleOperatorName(BinaryOperator::getOverloadedOperator(BO->getOpcode()),
Anders Carlssone170ba72009-12-14 01:45:37 +00001882 /*Arity=*/2);
1883 mangleExpression(BO->getLHS());
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001884 mangleExpression(BO->getRHS());
Anders Carlssone170ba72009-12-14 01:45:37 +00001885 break;
John McCall2f27bf82010-02-04 02:56:29 +00001886 }
Anders Carlssone170ba72009-12-14 01:45:37 +00001887
1888 case Expr::ConditionalOperatorClass: {
1889 const ConditionalOperator *CO = cast<ConditionalOperator>(E);
1890 mangleOperatorName(OO_Conditional, /*Arity=*/3);
1891 mangleExpression(CO->getCond());
John McCall5e1e89b2010-08-18 19:18:59 +00001892 mangleExpression(CO->getLHS(), Arity);
1893 mangleExpression(CO->getRHS(), Arity);
Anders Carlssone170ba72009-12-14 01:45:37 +00001894 break;
1895 }
1896
Douglas Gregor46287c72010-01-29 16:37:09 +00001897 case Expr::ImplicitCastExprClass: {
John McCall5e1e89b2010-08-18 19:18:59 +00001898 mangleExpression(cast<ImplicitCastExpr>(E)->getSubExpr(), Arity);
Douglas Gregor46287c72010-01-29 16:37:09 +00001899 break;
1900 }
1901
1902 case Expr::CStyleCastExprClass:
1903 case Expr::CXXStaticCastExprClass:
1904 case Expr::CXXDynamicCastExprClass:
1905 case Expr::CXXReinterpretCastExprClass:
1906 case Expr::CXXConstCastExprClass:
1907 case Expr::CXXFunctionalCastExprClass: {
1908 const ExplicitCastExpr *ECE = cast<ExplicitCastExpr>(E);
1909 Out << "cv";
1910 mangleType(ECE->getType());
1911 mangleExpression(ECE->getSubExpr());
1912 break;
1913 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001914
Anders Carlsson58040a52009-12-16 05:48:46 +00001915 case Expr::CXXOperatorCallExprClass: {
1916 const CXXOperatorCallExpr *CE = cast<CXXOperatorCallExpr>(E);
1917 unsigned NumArgs = CE->getNumArgs();
1918 mangleOperatorName(CE->getOperator(), /*Arity=*/NumArgs);
1919 // Mangle the arguments.
1920 for (unsigned i = 0; i != NumArgs; ++i)
1921 mangleExpression(CE->getArg(i));
1922 break;
1923 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001924
Anders Carlssona7694082009-11-06 02:50:19 +00001925 case Expr::ParenExprClass:
John McCall5e1e89b2010-08-18 19:18:59 +00001926 mangleExpression(cast<ParenExpr>(E)->getSubExpr(), Arity);
Anders Carlssona7694082009-11-06 02:50:19 +00001927 break;
1928
Anders Carlssond553f8c2009-09-21 01:21:10 +00001929 case Expr::DeclRefExprClass: {
Douglas Gregor5ed1bc32010-02-28 21:40:32 +00001930 const NamedDecl *D = cast<DeclRefExpr>(E)->getDecl();
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00001931
Anders Carlssond553f8c2009-09-21 01:21:10 +00001932 switch (D->getKind()) {
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001933 default:
Douglas Gregor5ed1bc32010-02-28 21:40:32 +00001934 // <expr-primary> ::= L <mangled-name> E # external name
1935 Out << 'L';
1936 mangle(D, "_Z");
1937 Out << 'E';
1938 break;
1939
John McCall3dc7e7b2010-07-24 01:17:35 +00001940 case Decl::EnumConstant: {
1941 const EnumConstantDecl *ED = cast<EnumConstantDecl>(D);
1942 mangleIntegerLiteral(ED->getType(), ED->getInitVal());
1943 break;
1944 }
1945
Anders Carlssond553f8c2009-09-21 01:21:10 +00001946 case Decl::NonTypeTemplateParm: {
1947 const NonTypeTemplateParmDecl *PD = cast<NonTypeTemplateParmDecl>(D);
Anders Carlsson0ccdf8d2009-09-27 00:38:53 +00001948 mangleTemplateParameter(PD->getIndex());
Anders Carlssond553f8c2009-09-21 01:21:10 +00001949 break;
1950 }
1951
1952 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00001953
Anders Carlsson50755b02009-09-27 20:11:34 +00001954 break;
Anders Carlssond553f8c2009-09-21 01:21:10 +00001955 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00001956
Douglas Gregorc7793c72011-01-15 01:15:58 +00001957 case Expr::SubstNonTypeTemplateParmPackExprClass:
1958 mangleTemplateParameter(
1959 cast<SubstNonTypeTemplateParmPackExpr>(E)->getParameterPack()->getIndex());
1960 break;
1961
John McCall865d4472009-11-19 22:55:06 +00001962 case Expr::DependentScopeDeclRefExprClass: {
1963 const DependentScopeDeclRefExpr *DRE = cast<DependentScopeDeclRefExpr>(E);
Douglas Gregor4b2ccfc2010-02-28 22:05:49 +00001964 NestedNameSpecifier *NNS = DRE->getQualifier();
1965 const Type *QTy = NNS->getAsType();
1966
1967 // When we're dealing with a nested-name-specifier that has just a
1968 // dependent identifier in it, mangle that as a typename. FIXME:
1969 // It isn't clear that we ever actually want to have such a
1970 // nested-name-specifier; why not just represent it as a typename type?
1971 if (!QTy && NNS->getAsIdentifier() && NNS->getPrefix()) {
Douglas Gregor4a2023f2010-03-31 20:19:30 +00001972 QTy = getASTContext().getDependentNameType(ETK_Typename,
1973 NNS->getPrefix(),
1974 NNS->getAsIdentifier())
Douglas Gregor4b2ccfc2010-02-28 22:05:49 +00001975 .getTypePtr();
1976 }
Anders Carlsson50755b02009-09-27 20:11:34 +00001977 assert(QTy && "Qualifier was not type!");
1978
John McCall6dbce192010-08-20 00:17:19 +00001979 // ::= sr <type> <unqualified-name> # dependent name
1980 // ::= sr <type> <unqualified-name> <template-args> # dependent template-id
Anders Carlsson50755b02009-09-27 20:11:34 +00001981 Out << "sr";
1982 mangleType(QualType(QTy, 0));
John McCall5e1e89b2010-08-18 19:18:59 +00001983 mangleUnqualifiedName(0, DRE->getDeclName(), Arity);
John McCall6dbce192010-08-20 00:17:19 +00001984 if (DRE->hasExplicitTemplateArgs())
1985 mangleTemplateArgs(DRE->getExplicitTemplateArgs());
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00001986
Anders Carlsson50755b02009-09-27 20:11:34 +00001987 break;
1988 }
1989
John McCalld9307602010-04-09 22:54:09 +00001990 case Expr::CXXBindTemporaryExprClass:
1991 mangleExpression(cast<CXXBindTemporaryExpr>(E)->getSubExpr());
1992 break;
1993
John McCall4765fa02010-12-06 08:20:24 +00001994 case Expr::ExprWithCleanupsClass:
1995 mangleExpression(cast<ExprWithCleanups>(E)->getSubExpr(), Arity);
John McCalld9307602010-04-09 22:54:09 +00001996 break;
1997
John McCall1dd73832010-02-04 01:42:13 +00001998 case Expr::FloatingLiteralClass: {
1999 const FloatingLiteral *FL = cast<FloatingLiteral>(E);
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002000 Out << 'L';
John McCall1dd73832010-02-04 01:42:13 +00002001 mangleType(FL->getType());
John McCall0512e482010-07-14 04:20:34 +00002002 mangleFloat(FL->getValue());
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002003 Out << 'E';
John McCall1dd73832010-02-04 01:42:13 +00002004 break;
2005 }
2006
John McCallde810632010-04-09 21:48:08 +00002007 case Expr::CharacterLiteralClass:
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002008 Out << 'L';
John McCallde810632010-04-09 21:48:08 +00002009 mangleType(E->getType());
2010 Out << cast<CharacterLiteral>(E)->getValue();
2011 Out << 'E';
2012 break;
2013
2014 case Expr::CXXBoolLiteralExprClass:
2015 Out << "Lb";
2016 Out << (cast<CXXBoolLiteralExpr>(E)->getValue() ? '1' : '0');
2017 Out << 'E';
2018 break;
2019
John McCall0512e482010-07-14 04:20:34 +00002020 case Expr::IntegerLiteralClass: {
2021 llvm::APSInt Value(cast<IntegerLiteral>(E)->getValue());
2022 if (E->getType()->isSignedIntegerType())
2023 Value.setIsSigned(true);
2024 mangleIntegerLiteral(E->getType(), Value);
Anders Carlssone170ba72009-12-14 01:45:37 +00002025 break;
John McCall0512e482010-07-14 04:20:34 +00002026 }
2027
2028 case Expr::ImaginaryLiteralClass: {
2029 const ImaginaryLiteral *IE = cast<ImaginaryLiteral>(E);
2030 // Mangle as if a complex literal.
Nick Lewycky271b6652010-09-05 03:40:33 +00002031 // Proposal from David Vandevoorde, 2010.06.30.
John McCall0512e482010-07-14 04:20:34 +00002032 Out << 'L';
2033 mangleType(E->getType());
2034 if (const FloatingLiteral *Imag =
2035 dyn_cast<FloatingLiteral>(IE->getSubExpr())) {
2036 // Mangle a floating-point zero of the appropriate type.
2037 mangleFloat(llvm::APFloat(Imag->getValue().getSemantics()));
2038 Out << '_';
2039 mangleFloat(Imag->getValue());
2040 } else {
Nick Lewycky271b6652010-09-05 03:40:33 +00002041 Out << "0_";
John McCall0512e482010-07-14 04:20:34 +00002042 llvm::APSInt Value(cast<IntegerLiteral>(IE->getSubExpr())->getValue());
2043 if (IE->getSubExpr()->getType()->isSignedIntegerType())
2044 Value.setIsSigned(true);
2045 mangleNumber(Value);
2046 }
2047 Out << 'E';
2048 break;
2049 }
2050
2051 case Expr::StringLiteralClass: {
John McCall1658c392010-07-15 21:53:03 +00002052 // Revised proposal from David Vandervoorde, 2010.07.15.
John McCall0512e482010-07-14 04:20:34 +00002053 Out << 'L';
John McCall1658c392010-07-15 21:53:03 +00002054 assert(isa<ConstantArrayType>(E->getType()));
2055 mangleType(E->getType());
John McCall0512e482010-07-14 04:20:34 +00002056 Out << 'E';
2057 break;
2058 }
2059
2060 case Expr::GNUNullExprClass:
2061 // FIXME: should this really be mangled the same as nullptr?
2062 // fallthrough
2063
2064 case Expr::CXXNullPtrLiteralExprClass: {
2065 // Proposal from David Vandervoorde, 2010.06.30, as
2066 // modified by ABI list discussion.
2067 Out << "LDnE";
2068 break;
2069 }
Douglas Gregorbe230c32011-01-03 17:17:50 +00002070
2071 case Expr::PackExpansionExprClass:
2072 Out << "sp";
2073 mangleExpression(cast<PackExpansionExpr>(E)->getPattern());
2074 break;
Douglas Gregor2e774c42011-01-04 18:56:13 +00002075
2076 case Expr::SizeOfPackExprClass: {
Douglas Gregor2e774c42011-01-04 18:56:13 +00002077 Out << "sZ";
2078 const NamedDecl *Pack = cast<SizeOfPackExpr>(E)->getPack();
2079 if (const TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(Pack))
2080 mangleTemplateParameter(TTP->getIndex());
2081 else if (const NonTypeTemplateParmDecl *NTTP
2082 = dyn_cast<NonTypeTemplateParmDecl>(Pack))
2083 mangleTemplateParameter(NTTP->getIndex());
2084 else if (const TemplateTemplateParmDecl *TempTP
2085 = dyn_cast<TemplateTemplateParmDecl>(Pack))
2086 mangleTemplateParameter(TempTP->getIndex());
2087 else {
Douglas Gregor4fc48662011-01-13 16:39:34 +00002088 // Note: proposed by Mike Herrick on 11/30/10
2089 // <expression> ::= sZ <function-param> # size of function parameter pack
Douglas Gregor2e774c42011-01-04 18:56:13 +00002090 Diagnostic &Diags = Context.getDiags();
2091 unsigned DiagID = Diags.getCustomDiagID(Diagnostic::Error,
2092 "cannot mangle sizeof...(function parameter pack)");
2093 Diags.Report(DiagID);
2094 return;
2095 }
2096 }
Anders Carlssond553f8c2009-09-21 01:21:10 +00002097 }
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00002098}
2099
Anders Carlsson3ac86b52009-04-15 05:36:58 +00002100void CXXNameMangler::mangleCXXCtorType(CXXCtorType T) {
2101 // <ctor-dtor-name> ::= C1 # complete object constructor
2102 // ::= C2 # base object constructor
2103 // ::= C3 # complete object allocating constructor
2104 //
2105 switch (T) {
2106 case Ctor_Complete:
2107 Out << "C1";
2108 break;
2109 case Ctor_Base:
2110 Out << "C2";
2111 break;
2112 case Ctor_CompleteAllocating:
2113 Out << "C3";
2114 break;
2115 }
2116}
2117
Anders Carlsson27ae5362009-04-17 01:58:57 +00002118void CXXNameMangler::mangleCXXDtorType(CXXDtorType T) {
2119 // <ctor-dtor-name> ::= D0 # deleting destructor
2120 // ::= D1 # complete object destructor
2121 // ::= D2 # base object destructor
2122 //
2123 switch (T) {
2124 case Dtor_Deleting:
2125 Out << "D0";
2126 break;
2127 case Dtor_Complete:
2128 Out << "D1";
2129 break;
2130 case Dtor_Base:
2131 Out << "D2";
2132 break;
2133 }
2134}
2135
John McCall6dbce192010-08-20 00:17:19 +00002136void CXXNameMangler::mangleTemplateArgs(
2137 const ExplicitTemplateArgumentList &TemplateArgs) {
2138 // <template-args> ::= I <template-arg>+ E
2139 Out << 'I';
2140 for (unsigned I = 0, E = TemplateArgs.NumTemplateArgs; I != E; ++I)
2141 mangleTemplateArg(0, TemplateArgs.getTemplateArgs()[I].getArgument());
2142 Out << 'E';
2143}
2144
Douglas Gregor20f0cc72010-04-23 03:10:43 +00002145void CXXNameMangler::mangleTemplateArgs(TemplateName Template,
2146 const TemplateArgument *TemplateArgs,
2147 unsigned NumTemplateArgs) {
2148 if (TemplateDecl *TD = Template.getAsTemplateDecl())
2149 return mangleTemplateArgs(*TD->getTemplateParameters(), TemplateArgs,
2150 NumTemplateArgs);
Sean Huntc3021132010-05-05 15:23:54 +00002151
Douglas Gregor20f0cc72010-04-23 03:10:43 +00002152 // <template-args> ::= I <template-arg>+ E
2153 Out << 'I';
2154 for (unsigned i = 0; i != NumTemplateArgs; ++i)
2155 mangleTemplateArg(0, TemplateArgs[i]);
2156 Out << 'E';
2157}
2158
Rafael Espindolad9800722010-03-11 14:07:00 +00002159void CXXNameMangler::mangleTemplateArgs(const TemplateParameterList &PL,
2160 const TemplateArgumentList &AL) {
Anders Carlsson7a0ba872009-05-15 16:09:15 +00002161 // <template-args> ::= I <template-arg>+ E
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002162 Out << 'I';
Rafael Espindolad9800722010-03-11 14:07:00 +00002163 for (unsigned i = 0, e = AL.size(); i != e; ++i)
2164 mangleTemplateArg(PL.getParam(i), AL[i]);
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002165 Out << 'E';
Anders Carlsson7a0ba872009-05-15 16:09:15 +00002166}
2167
Rafael Espindolad9800722010-03-11 14:07:00 +00002168void CXXNameMangler::mangleTemplateArgs(const TemplateParameterList &PL,
2169 const TemplateArgument *TemplateArgs,
Anders Carlsson7624f212009-09-18 02:42:01 +00002170 unsigned NumTemplateArgs) {
2171 // <template-args> ::= I <template-arg>+ E
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002172 Out << 'I';
Daniel Dunbar7e0c1952009-11-21 09:17:15 +00002173 for (unsigned i = 0; i != NumTemplateArgs; ++i)
Rafael Espindolad9800722010-03-11 14:07:00 +00002174 mangleTemplateArg(PL.getParam(i), TemplateArgs[i]);
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002175 Out << 'E';
Anders Carlsson7624f212009-09-18 02:42:01 +00002176}
2177
Rafael Espindolad9800722010-03-11 14:07:00 +00002178void CXXNameMangler::mangleTemplateArg(const NamedDecl *P,
2179 const TemplateArgument &A) {
Mike Stump1eb44332009-09-09 15:08:12 +00002180 // <template-arg> ::= <type> # type or template
Anders Carlsson7a0ba872009-05-15 16:09:15 +00002181 // ::= X <expression> E # expression
2182 // ::= <expr-primary> # simple expressions
Douglas Gregor4fc48662011-01-13 16:39:34 +00002183 // ::= J <template-arg>* E # argument pack
Anders Carlsson7a0ba872009-05-15 16:09:15 +00002184 // ::= sp <expression> # pack expansion of (C++0x)
2185 switch (A.getKind()) {
Douglas Gregorf90b27a2011-01-03 22:36:02 +00002186 case TemplateArgument::Null:
2187 llvm_unreachable("Cannot mangle NULL template argument");
2188
Anders Carlsson7a0ba872009-05-15 16:09:15 +00002189 case TemplateArgument::Type:
2190 mangleType(A.getAsType());
2191 break;
Anders Carlsson9e85c742009-12-23 19:30:55 +00002192 case TemplateArgument::Template:
John McCallb6f532e2010-07-14 06:43:17 +00002193 // This is mangled as <type>.
2194 mangleType(A.getAsTemplate());
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002195 break;
Douglas Gregora7fc9012011-01-05 18:58:31 +00002196 case TemplateArgument::TemplateExpansion:
Douglas Gregor4fc48662011-01-13 16:39:34 +00002197 // <type> ::= Dp <type> # pack expansion (C++0x)
Douglas Gregora7fc9012011-01-05 18:58:31 +00002198 Out << "Dp";
2199 mangleType(A.getAsTemplateOrTemplatePattern());
2200 break;
Anders Carlssond553f8c2009-09-21 01:21:10 +00002201 case TemplateArgument::Expression:
2202 Out << 'X';
2203 mangleExpression(A.getAsExpr());
2204 Out << 'E';
2205 break;
Anders Carlssone170ba72009-12-14 01:45:37 +00002206 case TemplateArgument::Integral:
2207 mangleIntegerLiteral(A.getIntegralType(), *A.getAsIntegral());
Anders Carlsson7a0ba872009-05-15 16:09:15 +00002208 break;
Daniel Dunbar7e0c1952009-11-21 09:17:15 +00002209 case TemplateArgument::Declaration: {
Douglas Gregor20f0cc72010-04-23 03:10:43 +00002210 assert(P && "Missing template parameter for declaration argument");
Daniel Dunbar7e0c1952009-11-21 09:17:15 +00002211 // <expr-primary> ::= L <mangled-name> E # external name
2212
Rafael Espindolad9800722010-03-11 14:07:00 +00002213 // Clang produces AST's where pointer-to-member-function expressions
Daniel Dunbar7e0c1952009-11-21 09:17:15 +00002214 // and pointer-to-function expressions are represented as a declaration not
Rafael Espindolad9800722010-03-11 14:07:00 +00002215 // an expression. We compensate for it here to produce the correct mangling.
2216 NamedDecl *D = cast<NamedDecl>(A.getAsDecl());
2217 const NonTypeTemplateParmDecl *Parameter = cast<NonTypeTemplateParmDecl>(P);
2218 bool compensateMangling = D->isCXXClassMember() &&
2219 !Parameter->getType()->isReferenceType();
2220 if (compensateMangling) {
2221 Out << 'X';
2222 mangleOperatorName(OO_Amp, 1);
2223 }
2224
Daniel Dunbar7e0c1952009-11-21 09:17:15 +00002225 Out << 'L';
2226 // References to external entities use the mangled name; if the name would
2227 // not normally be manged then mangle it as unqualified.
2228 //
2229 // FIXME: The ABI specifies that external names here should have _Z, but
2230 // gcc leaves this off.
Rafael Espindolad9800722010-03-11 14:07:00 +00002231 if (compensateMangling)
2232 mangle(D, "_Z");
2233 else
2234 mangle(D, "Z");
Daniel Dunbar7e0c1952009-11-21 09:17:15 +00002235 Out << 'E';
Rafael Espindolad9800722010-03-11 14:07:00 +00002236
2237 if (compensateMangling)
2238 Out << 'E';
2239
Daniel Dunbar7e0c1952009-11-21 09:17:15 +00002240 break;
2241 }
Douglas Gregorf90b27a2011-01-03 22:36:02 +00002242
2243 case TemplateArgument::Pack: {
2244 // Note: proposal by Mike Herrick on 12/20/10
2245 Out << 'J';
2246 for (TemplateArgument::pack_iterator PA = A.pack_begin(),
2247 PAEnd = A.pack_end();
2248 PA != PAEnd; ++PA)
2249 mangleTemplateArg(P, *PA);
2250 Out << 'E';
2251 }
Daniel Dunbar7e0c1952009-11-21 09:17:15 +00002252 }
Anders Carlsson7a0ba872009-05-15 16:09:15 +00002253}
2254
Anders Carlsson0ccdf8d2009-09-27 00:38:53 +00002255void CXXNameMangler::mangleTemplateParameter(unsigned Index) {
2256 // <template-param> ::= T_ # first template parameter
2257 // ::= T <parameter-2 non-negative number> _
2258 if (Index == 0)
2259 Out << "T_";
2260 else
2261 Out << 'T' << (Index - 1) << '_';
2262}
2263
Anders Carlsson76967372009-09-17 00:43:46 +00002264// <substitution> ::= S <seq-id> _
2265// ::= S_
Anders Carlsson6862fc72009-09-17 04:16:28 +00002266bool CXXNameMangler::mangleSubstitution(const NamedDecl *ND) {
Anders Carlssone7c8cb62009-09-26 20:53:44 +00002267 // Try one of the standard substitutions first.
2268 if (mangleStandardSubstitution(ND))
2269 return true;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002270
Anders Carlsson433d1372009-11-07 04:26:04 +00002271 ND = cast<NamedDecl>(ND->getCanonicalDecl());
Anders Carlsson6862fc72009-09-17 04:16:28 +00002272 return mangleSubstitution(reinterpret_cast<uintptr_t>(ND));
2273}
2274
Anders Carlsson76967372009-09-17 00:43:46 +00002275bool CXXNameMangler::mangleSubstitution(QualType T) {
Anders Carlssond99edc42009-09-26 03:55:37 +00002276 if (!T.getCVRQualifiers()) {
2277 if (const RecordType *RT = T->getAs<RecordType>())
2278 return mangleSubstitution(RT->getDecl());
2279 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002280
Anders Carlsson76967372009-09-17 00:43:46 +00002281 uintptr_t TypePtr = reinterpret_cast<uintptr_t>(T.getAsOpaquePtr());
2282
Anders Carlssond3a932a2009-09-17 03:53:28 +00002283 return mangleSubstitution(TypePtr);
2284}
2285
Douglas Gregor1e9268e2010-04-28 05:58:56 +00002286bool CXXNameMangler::mangleSubstitution(TemplateName Template) {
2287 if (TemplateDecl *TD = Template.getAsTemplateDecl())
2288 return mangleSubstitution(TD);
Sean Huntc3021132010-05-05 15:23:54 +00002289
Douglas Gregor1e9268e2010-04-28 05:58:56 +00002290 Template = Context.getASTContext().getCanonicalTemplateName(Template);
2291 return mangleSubstitution(
2292 reinterpret_cast<uintptr_t>(Template.getAsVoidPointer()));
2293}
2294
Anders Carlssond3a932a2009-09-17 03:53:28 +00002295bool CXXNameMangler::mangleSubstitution(uintptr_t Ptr) {
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002296 llvm::DenseMap<uintptr_t, unsigned>::iterator I = Substitutions.find(Ptr);
Anders Carlsson76967372009-09-17 00:43:46 +00002297 if (I == Substitutions.end())
2298 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002299
Anders Carlsson76967372009-09-17 00:43:46 +00002300 unsigned SeqID = I->second;
2301 if (SeqID == 0)
2302 Out << "S_";
2303 else {
2304 SeqID--;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002305
Anders Carlsson76967372009-09-17 00:43:46 +00002306 // <seq-id> is encoded in base-36, using digits and upper case letters.
2307 char Buffer[10];
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002308 char *BufferPtr = llvm::array_endof(Buffer);
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002309
Anders Carlsson76967372009-09-17 00:43:46 +00002310 if (SeqID == 0) *--BufferPtr = '0';
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002311
Anders Carlsson76967372009-09-17 00:43:46 +00002312 while (SeqID) {
2313 assert(BufferPtr > Buffer && "Buffer overflow!");
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002314
John McCall6ab30e02010-06-09 07:26:17 +00002315 char c = static_cast<char>(SeqID % 36);
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002316
Anders Carlsson76967372009-09-17 00:43:46 +00002317 *--BufferPtr = (c < 10 ? '0' + c : 'A' + c - 10);
2318 SeqID /= 36;
2319 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002320
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002321 Out << 'S'
2322 << llvm::StringRef(BufferPtr, llvm::array_endof(Buffer)-BufferPtr)
2323 << '_';
Anders Carlsson76967372009-09-17 00:43:46 +00002324 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002325
Anders Carlsson76967372009-09-17 00:43:46 +00002326 return true;
2327}
2328
Anders Carlssonf514b542009-09-27 00:12:57 +00002329static bool isCharType(QualType T) {
2330 if (T.isNull())
2331 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002332
Anders Carlssonf514b542009-09-27 00:12:57 +00002333 return T->isSpecificBuiltinType(BuiltinType::Char_S) ||
2334 T->isSpecificBuiltinType(BuiltinType::Char_U);
2335}
2336
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002337/// isCharSpecialization - Returns whether a given type is a template
Anders Carlssonf514b542009-09-27 00:12:57 +00002338/// specialization of a given name with a single argument of type char.
2339static bool isCharSpecialization(QualType T, const char *Name) {
2340 if (T.isNull())
2341 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002342
Anders Carlssonf514b542009-09-27 00:12:57 +00002343 const RecordType *RT = T->getAs<RecordType>();
2344 if (!RT)
2345 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002346
2347 const ClassTemplateSpecializationDecl *SD =
Anders Carlssonf514b542009-09-27 00:12:57 +00002348 dyn_cast<ClassTemplateSpecializationDecl>(RT->getDecl());
2349 if (!SD)
2350 return false;
2351
2352 if (!isStdNamespace(SD->getDeclContext()))
2353 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002354
Anders Carlssonf514b542009-09-27 00:12:57 +00002355 const TemplateArgumentList &TemplateArgs = SD->getTemplateArgs();
2356 if (TemplateArgs.size() != 1)
2357 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002358
Anders Carlssonf514b542009-09-27 00:12:57 +00002359 if (!isCharType(TemplateArgs[0].getAsType()))
2360 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002361
Daniel Dunbar01eb9b92009-10-18 21:17:35 +00002362 return SD->getIdentifier()->getName() == Name;
Anders Carlssonf514b542009-09-27 00:12:57 +00002363}
2364
Anders Carlsson91f88602009-12-07 19:56:42 +00002365template <std::size_t StrLen>
Benjamin Kramer54353f42010-11-25 18:29:30 +00002366static bool isStreamCharSpecialization(const ClassTemplateSpecializationDecl*SD,
2367 const char (&Str)[StrLen]) {
Anders Carlsson91f88602009-12-07 19:56:42 +00002368 if (!SD->getIdentifier()->isStr(Str))
2369 return false;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002370
Anders Carlsson91f88602009-12-07 19:56:42 +00002371 const TemplateArgumentList &TemplateArgs = SD->getTemplateArgs();
2372 if (TemplateArgs.size() != 2)
2373 return false;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002374
Anders Carlsson91f88602009-12-07 19:56:42 +00002375 if (!isCharType(TemplateArgs[0].getAsType()))
2376 return false;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002377
Anders Carlsson91f88602009-12-07 19:56:42 +00002378 if (!isCharSpecialization(TemplateArgs[1].getAsType(), "char_traits"))
2379 return false;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002380
Anders Carlsson91f88602009-12-07 19:56:42 +00002381 return true;
2382}
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002383
Anders Carlssone7c8cb62009-09-26 20:53:44 +00002384bool CXXNameMangler::mangleStandardSubstitution(const NamedDecl *ND) {
2385 // <substitution> ::= St # ::std::
Anders Carlsson8c031552009-09-26 23:10:05 +00002386 if (const NamespaceDecl *NS = dyn_cast<NamespaceDecl>(ND)) {
Anders Carlsson47846d22009-12-04 06:23:23 +00002387 if (isStd(NS)) {
Anders Carlsson8c031552009-09-26 23:10:05 +00002388 Out << "St";
2389 return true;
2390 }
2391 }
2392
2393 if (const ClassTemplateDecl *TD = dyn_cast<ClassTemplateDecl>(ND)) {
2394 if (!isStdNamespace(TD->getDeclContext()))
2395 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002396
Anders Carlsson8c031552009-09-26 23:10:05 +00002397 // <substitution> ::= Sa # ::std::allocator
2398 if (TD->getIdentifier()->isStr("allocator")) {
2399 Out << "Sa";
2400 return true;
2401 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002402
Anders Carlsson189d59c2009-09-26 23:14:39 +00002403 // <<substitution> ::= Sb # ::std::basic_string
2404 if (TD->getIdentifier()->isStr("basic_string")) {
2405 Out << "Sb";
2406 return true;
2407 }
Anders Carlsson8c031552009-09-26 23:10:05 +00002408 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002409
2410 if (const ClassTemplateSpecializationDecl *SD =
Anders Carlssonf514b542009-09-27 00:12:57 +00002411 dyn_cast<ClassTemplateSpecializationDecl>(ND)) {
Eli Friedman5370ee22010-02-23 18:25:09 +00002412 if (!isStdNamespace(SD->getDeclContext()))
2413 return false;
2414
Anders Carlssonf514b542009-09-27 00:12:57 +00002415 // <substitution> ::= Ss # ::std::basic_string<char,
2416 // ::std::char_traits<char>,
2417 // ::std::allocator<char> >
2418 if (SD->getIdentifier()->isStr("basic_string")) {
2419 const TemplateArgumentList &TemplateArgs = SD->getTemplateArgs();
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002420
Anders Carlssonf514b542009-09-27 00:12:57 +00002421 if (TemplateArgs.size() != 3)
2422 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002423
Anders Carlssonf514b542009-09-27 00:12:57 +00002424 if (!isCharType(TemplateArgs[0].getAsType()))
2425 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002426
Anders Carlssonf514b542009-09-27 00:12:57 +00002427 if (!isCharSpecialization(TemplateArgs[1].getAsType(), "char_traits"))
2428 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002429
Anders Carlssonf514b542009-09-27 00:12:57 +00002430 if (!isCharSpecialization(TemplateArgs[2].getAsType(), "allocator"))
2431 return false;
2432
2433 Out << "Ss";
2434 return true;
2435 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002436
Anders Carlsson91f88602009-12-07 19:56:42 +00002437 // <substitution> ::= Si # ::std::basic_istream<char,
2438 // ::std::char_traits<char> >
2439 if (isStreamCharSpecialization(SD, "basic_istream")) {
2440 Out << "Si";
2441 return true;
2442 }
2443
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002444 // <substitution> ::= So # ::std::basic_ostream<char,
Anders Carlsson8f8fd8e2009-10-08 17:20:26 +00002445 // ::std::char_traits<char> >
Anders Carlsson91f88602009-12-07 19:56:42 +00002446 if (isStreamCharSpecialization(SD, "basic_ostream")) {
Anders Carlsson8f8fd8e2009-10-08 17:20:26 +00002447 Out << "So";
2448 return true;
2449 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002450
Anders Carlsson91f88602009-12-07 19:56:42 +00002451 // <substitution> ::= Sd # ::std::basic_iostream<char,
2452 // ::std::char_traits<char> >
2453 if (isStreamCharSpecialization(SD, "basic_iostream")) {
2454 Out << "Sd";
2455 return true;
2456 }
Anders Carlssonf514b542009-09-27 00:12:57 +00002457 }
Anders Carlsson8c031552009-09-26 23:10:05 +00002458 return false;
Anders Carlssone7c8cb62009-09-26 20:53:44 +00002459}
2460
Anders Carlsson76967372009-09-17 00:43:46 +00002461void CXXNameMangler::addSubstitution(QualType T) {
Anders Carlssond99edc42009-09-26 03:55:37 +00002462 if (!T.getCVRQualifiers()) {
2463 if (const RecordType *RT = T->getAs<RecordType>()) {
2464 addSubstitution(RT->getDecl());
2465 return;
2466 }
2467 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002468
Anders Carlsson76967372009-09-17 00:43:46 +00002469 uintptr_t TypePtr = reinterpret_cast<uintptr_t>(T.getAsOpaquePtr());
Anders Carlssond3a932a2009-09-17 03:53:28 +00002470 addSubstitution(TypePtr);
2471}
2472
Douglas Gregor1e9268e2010-04-28 05:58:56 +00002473void CXXNameMangler::addSubstitution(TemplateName Template) {
2474 if (TemplateDecl *TD = Template.getAsTemplateDecl())
2475 return addSubstitution(TD);
Sean Huntc3021132010-05-05 15:23:54 +00002476
Douglas Gregor1e9268e2010-04-28 05:58:56 +00002477 Template = Context.getASTContext().getCanonicalTemplateName(Template);
2478 addSubstitution(reinterpret_cast<uintptr_t>(Template.getAsVoidPointer()));
2479}
2480
Anders Carlssond3a932a2009-09-17 03:53:28 +00002481void CXXNameMangler::addSubstitution(uintptr_t Ptr) {
Anders Carlssond3a932a2009-09-17 03:53:28 +00002482 assert(!Substitutions.count(Ptr) && "Substitution already exists!");
Anders Carlsson9d85b722010-06-02 04:29:50 +00002483 Substitutions[Ptr] = SeqID++;
Anders Carlsson76967372009-09-17 00:43:46 +00002484}
2485
Daniel Dunbar1b077112009-11-21 09:06:10 +00002486//
Mike Stump1eb44332009-09-09 15:08:12 +00002487
Daniel Dunbar1b077112009-11-21 09:06:10 +00002488/// \brief Mangles the name of the declaration D and emits that name to the
2489/// given output stream.
2490///
2491/// If the declaration D requires a mangled name, this routine will emit that
2492/// mangled name to \p os and return true. Otherwise, \p os will be unchanged
2493/// and this routine will return false. In this case, the caller should just
2494/// emit the identifier of the declaration (\c D->getIdentifier()) as its
2495/// name.
Peter Collingbourne14110472011-01-13 18:57:25 +00002496void ItaniumMangleContext::mangleName(const NamedDecl *D,
2497 llvm::SmallVectorImpl<char> &Res) {
Daniel Dunbarc02ab4c2009-11-21 09:14:44 +00002498 assert((isa<FunctionDecl>(D) || isa<VarDecl>(D)) &&
2499 "Invalid mangleName() call, argument is not a variable or function!");
2500 assert(!isa<CXXConstructorDecl>(D) && !isa<CXXDestructorDecl>(D) &&
2501 "Invalid mangleName() call on 'structor decl!");
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002502
Daniel Dunbar1b077112009-11-21 09:06:10 +00002503 PrettyStackTraceDecl CrashInfo(D, SourceLocation(),
2504 getASTContext().getSourceManager(),
2505 "Mangling declaration");
Mike Stump1eb44332009-09-09 15:08:12 +00002506
Daniel Dunbar94fd26d2009-11-21 09:06:22 +00002507 CXXNameMangler Mangler(*this, Res);
2508 return Mangler.mangle(D);
Daniel Dunbar1b077112009-11-21 09:06:10 +00002509}
Mike Stump1eb44332009-09-09 15:08:12 +00002510
Peter Collingbourne14110472011-01-13 18:57:25 +00002511void ItaniumMangleContext::mangleCXXCtor(const CXXConstructorDecl *D,
2512 CXXCtorType Type,
2513 llvm::SmallVectorImpl<char> &Res) {
Daniel Dunbar77939c92009-11-21 09:06:31 +00002514 CXXNameMangler Mangler(*this, Res, D, Type);
2515 Mangler.mangle(D);
Daniel Dunbar1b077112009-11-21 09:06:10 +00002516}
Mike Stump1eb44332009-09-09 15:08:12 +00002517
Peter Collingbourne14110472011-01-13 18:57:25 +00002518void ItaniumMangleContext::mangleCXXDtor(const CXXDestructorDecl *D,
2519 CXXDtorType Type,
2520 llvm::SmallVectorImpl<char> &Res) {
Daniel Dunbar77939c92009-11-21 09:06:31 +00002521 CXXNameMangler Mangler(*this, Res, D, Type);
2522 Mangler.mangle(D);
Daniel Dunbar1b077112009-11-21 09:06:10 +00002523}
Mike Stumpf1216772009-07-31 18:25:34 +00002524
Peter Collingbourne14110472011-01-13 18:57:25 +00002525void ItaniumMangleContext::mangleThunk(const CXXMethodDecl *MD,
2526 const ThunkInfo &Thunk,
2527 llvm::SmallVectorImpl<char> &Res) {
Anders Carlsson19879c92010-03-23 17:17:29 +00002528 // <special-name> ::= T <call-offset> <base encoding>
2529 // # base is the nominal target function of thunk
2530 // <special-name> ::= Tc <call-offset> <call-offset> <base encoding>
2531 // # base is the nominal target function of thunk
2532 // # first call-offset is 'this' adjustment
2533 // # second call-offset is result adjustment
Sean Huntc3021132010-05-05 15:23:54 +00002534
Anders Carlsson19879c92010-03-23 17:17:29 +00002535 assert(!isa<CXXDestructorDecl>(MD) &&
2536 "Use mangleCXXDtor for destructor decls!");
Sean Huntc3021132010-05-05 15:23:54 +00002537
Anders Carlsson19879c92010-03-23 17:17:29 +00002538 CXXNameMangler Mangler(*this, Res);
2539 Mangler.getStream() << "_ZT";
2540 if (!Thunk.Return.isEmpty())
2541 Mangler.getStream() << 'c';
Sean Huntc3021132010-05-05 15:23:54 +00002542
Anders Carlsson19879c92010-03-23 17:17:29 +00002543 // Mangle the 'this' pointer adjustment.
2544 Mangler.mangleCallOffset(Thunk.This.NonVirtual, Thunk.This.VCallOffsetOffset);
Sean Huntc3021132010-05-05 15:23:54 +00002545
Anders Carlsson19879c92010-03-23 17:17:29 +00002546 // Mangle the return pointer adjustment if there is one.
2547 if (!Thunk.Return.isEmpty())
2548 Mangler.mangleCallOffset(Thunk.Return.NonVirtual,
2549 Thunk.Return.VBaseOffsetOffset);
Sean Huntc3021132010-05-05 15:23:54 +00002550
Anders Carlsson19879c92010-03-23 17:17:29 +00002551 Mangler.mangleFunctionEncoding(MD);
2552}
2553
Sean Huntc3021132010-05-05 15:23:54 +00002554void
Peter Collingbourne14110472011-01-13 18:57:25 +00002555ItaniumMangleContext::mangleCXXDtorThunk(const CXXDestructorDecl *DD,
2556 CXXDtorType Type,
2557 const ThisAdjustment &ThisAdjustment,
2558 llvm::SmallVectorImpl<char> &Res) {
Anders Carlsson19879c92010-03-23 17:17:29 +00002559 // <special-name> ::= T <call-offset> <base encoding>
2560 // # base is the nominal target function of thunk
Sean Huntc3021132010-05-05 15:23:54 +00002561
Anders Carlsson19879c92010-03-23 17:17:29 +00002562 CXXNameMangler Mangler(*this, Res, DD, Type);
2563 Mangler.getStream() << "_ZT";
2564
2565 // Mangle the 'this' pointer adjustment.
Sean Huntc3021132010-05-05 15:23:54 +00002566 Mangler.mangleCallOffset(ThisAdjustment.NonVirtual,
Anders Carlsson19879c92010-03-23 17:17:29 +00002567 ThisAdjustment.VCallOffsetOffset);
2568
2569 Mangler.mangleFunctionEncoding(DD);
2570}
2571
Daniel Dunbarc0747712009-11-21 09:12:13 +00002572/// mangleGuardVariable - Returns the mangled name for a guard variable
2573/// for the passed in VarDecl.
Peter Collingbourne14110472011-01-13 18:57:25 +00002574void ItaniumMangleContext::mangleItaniumGuardVariable(const VarDecl *D,
2575 llvm::SmallVectorImpl<char> &Res) {
Daniel Dunbarc0747712009-11-21 09:12:13 +00002576 // <special-name> ::= GV <object name> # Guard variable for one-time
2577 // # initialization
2578 CXXNameMangler Mangler(*this, Res);
2579 Mangler.getStream() << "_ZGV";
2580 Mangler.mangleName(D);
2581}
2582
Peter Collingbourne14110472011-01-13 18:57:25 +00002583void ItaniumMangleContext::mangleReferenceTemporary(const VarDecl *D,
Anders Carlsson715edf22010-06-26 16:09:40 +00002584 llvm::SmallVectorImpl<char> &Res) {
2585 // We match the GCC mangling here.
2586 // <special-name> ::= GR <object name>
2587 CXXNameMangler Mangler(*this, Res);
2588 Mangler.getStream() << "_ZGR";
2589 Mangler.mangleName(D);
2590}
2591
Peter Collingbourne14110472011-01-13 18:57:25 +00002592void ItaniumMangleContext::mangleCXXVTable(const CXXRecordDecl *RD,
2593 llvm::SmallVectorImpl<char> &Res) {
Daniel Dunbarc0747712009-11-21 09:12:13 +00002594 // <special-name> ::= TV <type> # virtual table
Daniel Dunbar94fd26d2009-11-21 09:06:22 +00002595 CXXNameMangler Mangler(*this, Res);
Daniel Dunbarc0747712009-11-21 09:12:13 +00002596 Mangler.getStream() << "_ZTV";
Douglas Gregor1b12a3b2010-05-26 05:11:13 +00002597 Mangler.mangleNameOrStandardSubstitution(RD);
Daniel Dunbar1b077112009-11-21 09:06:10 +00002598}
Mike Stump82d75b02009-11-10 01:58:37 +00002599
Peter Collingbourne14110472011-01-13 18:57:25 +00002600void ItaniumMangleContext::mangleCXXVTT(const CXXRecordDecl *RD,
2601 llvm::SmallVectorImpl<char> &Res) {
Daniel Dunbarc0747712009-11-21 09:12:13 +00002602 // <special-name> ::= TT <type> # VTT structure
Daniel Dunbar94fd26d2009-11-21 09:06:22 +00002603 CXXNameMangler Mangler(*this, Res);
Daniel Dunbarc0747712009-11-21 09:12:13 +00002604 Mangler.getStream() << "_ZTT";
Douglas Gregor1b12a3b2010-05-26 05:11:13 +00002605 Mangler.mangleNameOrStandardSubstitution(RD);
Daniel Dunbar1b077112009-11-21 09:06:10 +00002606}
Mike Stumpab3f7e92009-11-10 01:41:59 +00002607
Peter Collingbourne14110472011-01-13 18:57:25 +00002608void ItaniumMangleContext::mangleCXXCtorVTable(const CXXRecordDecl *RD,
2609 int64_t Offset,
2610 const CXXRecordDecl *Type,
2611 llvm::SmallVectorImpl<char> &Res) {
Daniel Dunbarc0747712009-11-21 09:12:13 +00002612 // <special-name> ::= TC <type> <offset number> _ <base type>
Daniel Dunbar94fd26d2009-11-21 09:06:22 +00002613 CXXNameMangler Mangler(*this, Res);
Daniel Dunbarc0747712009-11-21 09:12:13 +00002614 Mangler.getStream() << "_ZTC";
Douglas Gregor1b12a3b2010-05-26 05:11:13 +00002615 Mangler.mangleNameOrStandardSubstitution(RD);
Daniel Dunbarc0747712009-11-21 09:12:13 +00002616 Mangler.getStream() << Offset;
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002617 Mangler.getStream() << '_';
Douglas Gregor1b12a3b2010-05-26 05:11:13 +00002618 Mangler.mangleNameOrStandardSubstitution(Type);
Daniel Dunbar1b077112009-11-21 09:06:10 +00002619}
Mike Stump738f8c22009-07-31 23:15:31 +00002620
Peter Collingbourne14110472011-01-13 18:57:25 +00002621void ItaniumMangleContext::mangleCXXRTTI(QualType Ty,
2622 llvm::SmallVectorImpl<char> &Res) {
Daniel Dunbarc0747712009-11-21 09:12:13 +00002623 // <special-name> ::= TI <type> # typeinfo structure
Douglas Gregor154fe982009-12-23 22:04:40 +00002624 assert(!Ty.hasQualifiers() && "RTTI info cannot have top-level qualifiers");
Daniel Dunbar94fd26d2009-11-21 09:06:22 +00002625 CXXNameMangler Mangler(*this, Res);
Daniel Dunbarc0747712009-11-21 09:12:13 +00002626 Mangler.getStream() << "_ZTI";
2627 Mangler.mangleType(Ty);
Daniel Dunbar1b077112009-11-21 09:06:10 +00002628}
Mike Stump67795982009-11-14 00:14:13 +00002629
Peter Collingbourne14110472011-01-13 18:57:25 +00002630void ItaniumMangleContext::mangleCXXRTTIName(QualType Ty,
2631 llvm::SmallVectorImpl<char> &Res) {
Daniel Dunbarc0747712009-11-21 09:12:13 +00002632 // <special-name> ::= TS <type> # typeinfo name (null terminated byte string)
Daniel Dunbar94fd26d2009-11-21 09:06:22 +00002633 CXXNameMangler Mangler(*this, Res);
Daniel Dunbarc0747712009-11-21 09:12:13 +00002634 Mangler.getStream() << "_ZTS";
2635 Mangler.mangleType(Ty);
Mike Stumpf1216772009-07-31 18:25:34 +00002636}
Peter Collingbourne14110472011-01-13 18:57:25 +00002637
2638MangleContext *clang::createItaniumMangleContext(ASTContext &Context,
2639 Diagnostic &Diags) {
2640 return new ItaniumMangleContext(Context, Diags);
2641}