blob: af9e35bbfcd251509dc0de9df11ce24f9a09d459 [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).
John McCallf4c73712011-01-19 06:33:43 +0000384 const FunctionType *FT
385 = cast<FunctionType>(Context.getASTContext()
John McCall54e14c42009-10-22 22:37:11 +0000386 .getCanonicalType(FD->getType()));
387
388 mangleBareFunctionType(FT, MangleReturnType);
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000389}
390
Anders Carlsson47846d22009-12-04 06:23:23 +0000391static const DeclContext *IgnoreLinkageSpecDecls(const DeclContext *DC) {
392 while (isa<LinkageSpecDecl>(DC)) {
Anders Carlsson47846d22009-12-04 06:23:23 +0000393 DC = DC->getParent();
394 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000395
Anders Carlsson47846d22009-12-04 06:23:23 +0000396 return DC;
397}
398
Anders Carlssonc820f902010-06-02 15:58:27 +0000399/// isStd - Return whether a given namespace is the 'std' namespace.
400static bool isStd(const NamespaceDecl *NS) {
401 if (!IgnoreLinkageSpecDecls(NS->getParent())->isTranslationUnit())
402 return false;
403
404 const IdentifierInfo *II = NS->getOriginalNamespace()->getIdentifier();
405 return II && II->isStr("std");
406}
407
Anders Carlsson47846d22009-12-04 06:23:23 +0000408// isStdNamespace - Return whether a given decl context is a toplevel 'std'
409// namespace.
Daniel Dunbar1308af92009-11-21 09:11:45 +0000410static bool isStdNamespace(const DeclContext *DC) {
Anders Carlsson47846d22009-12-04 06:23:23 +0000411 if (!DC->isNamespace())
412 return false;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000413
Anders Carlsson47846d22009-12-04 06:23:23 +0000414 return isStd(cast<NamespaceDecl>(DC));
Daniel Dunbar1308af92009-11-21 09:11:45 +0000415}
416
Anders Carlssonbb36ba42009-09-26 03:24:57 +0000417static const TemplateDecl *
418isTemplate(const NamedDecl *ND, const TemplateArgumentList *&TemplateArgs) {
Anders Carlsson2744a062009-09-18 19:00:18 +0000419 // Check if we have a function template.
420 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)){
Anders Carlssonbb36ba42009-09-26 03:24:57 +0000421 if (const TemplateDecl *TD = FD->getPrimaryTemplate()) {
Anders Carlsson2744a062009-09-18 19:00:18 +0000422 TemplateArgs = FD->getTemplateSpecializationArgs();
Anders Carlssonbb36ba42009-09-26 03:24:57 +0000423 return TD;
Anders Carlsson2744a062009-09-18 19:00:18 +0000424 }
425 }
426
Anders Carlssoneafc6dc2009-09-18 19:44:50 +0000427 // Check if we have a class template.
428 if (const ClassTemplateSpecializationDecl *Spec =
429 dyn_cast<ClassTemplateSpecializationDecl>(ND)) {
430 TemplateArgs = &Spec->getTemplateArgs();
Anders Carlssonbb36ba42009-09-26 03:24:57 +0000431 return Spec->getSpecializedTemplate();
Anders Carlssoneafc6dc2009-09-18 19:44:50 +0000432 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000433
Anders Carlsson2744a062009-09-18 19:00:18 +0000434 return 0;
435}
436
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000437void CXXNameMangler::mangleName(const NamedDecl *ND) {
438 // <name> ::= <nested-name>
439 // ::= <unscoped-name>
440 // ::= <unscoped-template-name> <template-args>
Anders Carlsson201ce742009-09-17 03:17:01 +0000441 // ::= <local-name>
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000442 //
Anders Carlssond58d6f72009-09-17 16:12:20 +0000443 const DeclContext *DC = ND->getDeclContext();
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000444
Eli Friedman7facf842009-12-02 20:32:49 +0000445 // If this is an extern variable declared locally, the relevant DeclContext
446 // is that of the containing namespace, or the translation unit.
447 if (isa<FunctionDecl>(DC) && ND->hasLinkage())
448 while (!DC->isNamespace() && !DC->isTranslationUnit())
449 DC = DC->getParent();
John McCall82b7d7b2010-10-18 21:28:44 +0000450 else if (GetLocalClassDecl(ND)) {
451 mangleLocalName(ND);
452 return;
453 }
Eli Friedman7facf842009-12-02 20:32:49 +0000454
Anders Carlsson5cc58c62009-09-22 17:23:30 +0000455 while (isa<LinkageSpecDecl>(DC))
Anders Carlssond58d6f72009-09-17 16:12:20 +0000456 DC = DC->getParent();
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000457
Anders Carlssond58d6f72009-09-17 16:12:20 +0000458 if (DC->isTranslationUnit() || isStdNamespace(DC)) {
Anders Carlsson2744a062009-09-18 19:00:18 +0000459 // Check if we have a template.
460 const TemplateArgumentList *TemplateArgs = 0;
Anders Carlsson0fa6df42009-09-26 19:45:45 +0000461 if (const TemplateDecl *TD = isTemplate(ND, TemplateArgs)) {
Anders Carlsson2744a062009-09-18 19:00:18 +0000462 mangleUnscopedTemplateName(TD);
Rafael Espindolad9800722010-03-11 14:07:00 +0000463 TemplateParameterList *TemplateParameters = TD->getTemplateParameters();
464 mangleTemplateArgs(*TemplateParameters, *TemplateArgs);
Anders Carlsson2744a062009-09-18 19:00:18 +0000465 return;
Anders Carlsson7482e242009-09-18 04:29:09 +0000466 }
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000467
Anders Carlsson7482e242009-09-18 04:29:09 +0000468 mangleUnscopedName(ND);
469 return;
470 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000471
Anders Carlsson7b06f6c2009-12-10 03:14:39 +0000472 if (isa<FunctionDecl>(DC) || isa<ObjCMethodDecl>(DC)) {
Anders Carlsson7482e242009-09-18 04:29:09 +0000473 mangleLocalName(ND);
474 return;
475 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000476
Eli Friedman7facf842009-12-02 20:32:49 +0000477 mangleNestedName(ND, DC);
Anders Carlsson7482e242009-09-18 04:29:09 +0000478}
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000479void CXXNameMangler::mangleName(const TemplateDecl *TD,
Anders Carlsson7624f212009-09-18 02:42:01 +0000480 const TemplateArgument *TemplateArgs,
481 unsigned NumTemplateArgs) {
Anders Carlsson47846d22009-12-04 06:23:23 +0000482 const DeclContext *DC = IgnoreLinkageSpecDecls(TD->getDeclContext());
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000483
Anders Carlsson7624f212009-09-18 02:42:01 +0000484 if (DC->isTranslationUnit() || isStdNamespace(DC)) {
Anders Carlsson0fa6df42009-09-26 19:45:45 +0000485 mangleUnscopedTemplateName(TD);
Rafael Espindolad9800722010-03-11 14:07:00 +0000486 TemplateParameterList *TemplateParameters = TD->getTemplateParameters();
487 mangleTemplateArgs(*TemplateParameters, TemplateArgs, NumTemplateArgs);
Anders Carlsson7624f212009-09-18 02:42:01 +0000488 } else {
489 mangleNestedName(TD, TemplateArgs, NumTemplateArgs);
490 }
491}
492
Anders Carlsson201ce742009-09-17 03:17:01 +0000493void CXXNameMangler::mangleUnscopedName(const NamedDecl *ND) {
494 // <unscoped-name> ::= <unqualified-name>
495 // ::= St <unqualified-name> # ::std::
496 if (isStdNamespace(ND->getDeclContext()))
497 Out << "St";
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000498
Anders Carlsson201ce742009-09-17 03:17:01 +0000499 mangleUnqualifiedName(ND);
500}
501
Anders Carlsson0fa6df42009-09-26 19:45:45 +0000502void CXXNameMangler::mangleUnscopedTemplateName(const TemplateDecl *ND) {
Anders Carlsson201ce742009-09-17 03:17:01 +0000503 // <unscoped-template-name> ::= <unscoped-name>
504 // ::= <substitution>
Anders Carlsson7624f212009-09-18 02:42:01 +0000505 if (mangleSubstitution(ND))
Anders Carlsson03c9d532009-09-17 04:02:31 +0000506 return;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000507
Douglas Gregor32fb4e12010-02-05 20:45:00 +0000508 // <template-template-param> ::= <template-param>
509 if (const TemplateTemplateParmDecl *TTP
510 = dyn_cast<TemplateTemplateParmDecl>(ND)) {
511 mangleTemplateParameter(TTP->getIndex());
512 return;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000513 }
Douglas Gregor32fb4e12010-02-05 20:45:00 +0000514
Anders Carlsson1668f202009-09-26 20:13:56 +0000515 mangleUnscopedName(ND->getTemplatedDecl());
Anders Carlsson7624f212009-09-18 02:42:01 +0000516 addSubstitution(ND);
Anders Carlsson201ce742009-09-17 03:17:01 +0000517}
518
Douglas Gregor1e9268e2010-04-28 05:58:56 +0000519void CXXNameMangler::mangleUnscopedTemplateName(TemplateName Template) {
520 // <unscoped-template-name> ::= <unscoped-name>
521 // ::= <substitution>
522 if (TemplateDecl *TD = Template.getAsTemplateDecl())
523 return mangleUnscopedTemplateName(TD);
Sean Huntc3021132010-05-05 15:23:54 +0000524
Douglas Gregor1e9268e2010-04-28 05:58:56 +0000525 if (mangleSubstitution(Template))
526 return;
527
528 // FIXME: How to cope with operators here?
529 DependentTemplateName *Dependent = Template.getAsDependentTemplateName();
530 assert(Dependent && "Not a dependent template name?");
531 if (!Dependent->isIdentifier()) {
532 // FIXME: We can't possibly know the arity of the operator here!
533 Diagnostic &Diags = Context.getDiags();
534 unsigned DiagID = Diags.getCustomDiagID(Diagnostic::Error,
535 "cannot mangle dependent operator name");
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000536 Diags.Report(DiagID);
Douglas Gregor1e9268e2010-04-28 05:58:56 +0000537 return;
538 }
Sean Huntc3021132010-05-05 15:23:54 +0000539
Douglas Gregor1e9268e2010-04-28 05:58:56 +0000540 mangleSourceName(Dependent->getIdentifier());
541 addSubstitution(Template);
542}
543
John McCall0512e482010-07-14 04:20:34 +0000544void CXXNameMangler::mangleFloat(const llvm::APFloat &F) {
545 // TODO: avoid this copy with careful stream management.
546 llvm::SmallString<20> Buffer;
547 F.bitcastToAPInt().toString(Buffer, 16, false);
548 Out.write(Buffer.data(), Buffer.size());
549}
550
551void CXXNameMangler::mangleNumber(const llvm::APSInt &Value) {
552 if (Value.isSigned() && Value.isNegative()) {
553 Out << 'n';
554 Value.abs().print(Out, true);
555 } else
556 Value.print(Out, Value.isSigned());
557}
558
Anders Carlssona94822e2009-11-26 02:32:05 +0000559void CXXNameMangler::mangleNumber(int64_t Number) {
560 // <number> ::= [n] <non-negative decimal integer>
561 if (Number < 0) {
562 Out << 'n';
563 Number = -Number;
564 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000565
Anders Carlssona94822e2009-11-26 02:32:05 +0000566 Out << Number;
567}
568
Anders Carlsson19879c92010-03-23 17:17:29 +0000569void CXXNameMangler::mangleCallOffset(int64_t NonVirtual, int64_t Virtual) {
Mike Stump141c5af2009-09-02 00:25:38 +0000570 // <call-offset> ::= h <nv-offset> _
571 // ::= v <v-offset> _
572 // <nv-offset> ::= <offset number> # non-virtual base override
Anders Carlssona94822e2009-11-26 02:32:05 +0000573 // <v-offset> ::= <offset number> _ <virtual offset number>
Mike Stump141c5af2009-09-02 00:25:38 +0000574 // # virtual base override, with vcall offset
Anders Carlsson19879c92010-03-23 17:17:29 +0000575 if (!Virtual) {
Anders Carlssona94822e2009-11-26 02:32:05 +0000576 Out << 'h';
Anders Carlsson19879c92010-03-23 17:17:29 +0000577 mangleNumber(NonVirtual);
Anders Carlssona94822e2009-11-26 02:32:05 +0000578 Out << '_';
579 return;
Mike Stump141c5af2009-09-02 00:25:38 +0000580 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000581
Anders Carlssona94822e2009-11-26 02:32:05 +0000582 Out << 'v';
Anders Carlsson19879c92010-03-23 17:17:29 +0000583 mangleNumber(NonVirtual);
Anders Carlssona94822e2009-11-26 02:32:05 +0000584 Out << '_';
Anders Carlsson19879c92010-03-23 17:17:29 +0000585 mangleNumber(Virtual);
Anders Carlssona94822e2009-11-26 02:32:05 +0000586 Out << '_';
Mike Stump9124bcc2009-09-02 00:56:18 +0000587}
588
John McCall1dd73832010-02-04 01:42:13 +0000589void CXXNameMangler::mangleUnresolvedScope(NestedNameSpecifier *Qualifier) {
590 Qualifier = getASTContext().getCanonicalNestedNameSpecifier(Qualifier);
591 switch (Qualifier->getKind()) {
592 case NestedNameSpecifier::Global:
593 // nothing
594 break;
595 case NestedNameSpecifier::Namespace:
596 mangleName(Qualifier->getAsNamespace());
597 break;
598 case NestedNameSpecifier::TypeSpec:
Rafael Espindola9b35b252010-03-17 04:28:11 +0000599 case NestedNameSpecifier::TypeSpecWithTemplate: {
600 const Type *QTy = Qualifier->getAsType();
601
602 if (const TemplateSpecializationType *TST =
603 dyn_cast<TemplateSpecializationType>(QTy)) {
604 if (!mangleSubstitution(QualType(TST, 0))) {
Douglas Gregor20f0cc72010-04-23 03:10:43 +0000605 mangleTemplatePrefix(TST->getTemplateName());
Sean Huntc3021132010-05-05 15:23:54 +0000606
Douglas Gregor20f0cc72010-04-23 03:10:43 +0000607 // FIXME: GCC does not appear to mangle the template arguments when
608 // the template in question is a dependent template name. Should we
609 // emulate that badness?
610 mangleTemplateArgs(TST->getTemplateName(), TST->getArgs(),
Rafael Espindola9b35b252010-03-17 04:28:11 +0000611 TST->getNumArgs());
612 addSubstitution(QualType(TST, 0));
613 }
614 } else {
615 // We use the QualType mangle type variant here because it handles
616 // substitutions.
617 mangleType(QualType(QTy, 0));
618 }
619 }
John McCall1dd73832010-02-04 01:42:13 +0000620 break;
621 case NestedNameSpecifier::Identifier:
John McCallad5e7382010-03-01 23:49:17 +0000622 // Member expressions can have these without prefixes.
623 if (Qualifier->getPrefix())
624 mangleUnresolvedScope(Qualifier->getPrefix());
John McCall1dd73832010-02-04 01:42:13 +0000625 mangleSourceName(Qualifier->getAsIdentifier());
626 break;
627 }
628}
629
630/// Mangles a name which was not resolved to a specific entity.
631void CXXNameMangler::mangleUnresolvedName(NestedNameSpecifier *Qualifier,
632 DeclarationName Name,
633 unsigned KnownArity) {
634 if (Qualifier)
635 mangleUnresolvedScope(Qualifier);
636 // FIXME: ambiguity of unqualified lookup with ::
637
638 mangleUnqualifiedName(0, Name, KnownArity);
639}
640
Anders Carlsson6f7e2f42010-06-08 14:49:03 +0000641static const FieldDecl *FindFirstNamedDataMember(const RecordDecl *RD) {
642 assert(RD->isAnonymousStructOrUnion() &&
643 "Expected anonymous struct or union!");
644
645 for (RecordDecl::field_iterator I = RD->field_begin(), E = RD->field_end();
646 I != E; ++I) {
647 const FieldDecl *FD = *I;
648
649 if (FD->getIdentifier())
650 return FD;
651
652 if (const RecordType *RT = FD->getType()->getAs<RecordType>()) {
653 if (const FieldDecl *NamedDataMember =
654 FindFirstNamedDataMember(RT->getDecl()))
655 return NamedDataMember;
656 }
657 }
658
659 // We didn't find a named data member.
660 return 0;
661}
662
John McCall1dd73832010-02-04 01:42:13 +0000663void CXXNameMangler::mangleUnqualifiedName(const NamedDecl *ND,
664 DeclarationName Name,
665 unsigned KnownArity) {
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000666 // <unqualified-name> ::= <operator-name>
Mike Stump1eb44332009-09-09 15:08:12 +0000667 // ::= <ctor-dtor-name>
668 // ::= <source-name>
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000669 switch (Name.getNameKind()) {
Anders Carlssonc4355b62009-10-07 01:45:02 +0000670 case DeclarationName::Identifier: {
Anders Carlssonc4355b62009-10-07 01:45:02 +0000671 if (const IdentifierInfo *II = Name.getAsIdentifierInfo()) {
Sean Hunt31455252010-01-24 03:04:27 +0000672 // We must avoid conflicts between internally- and externally-
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000673 // linked variable declaration names in the same TU.
Anders Carlssonaec25232010-02-06 04:52:27 +0000674 // This naming convention is the same as that followed by GCC, though it
675 // shouldn't actually matter.
676 if (ND && isa<VarDecl>(ND) && ND->getLinkage() == InternalLinkage &&
Sean Hunt31455252010-01-24 03:04:27 +0000677 ND->getDeclContext()->isFileContext())
678 Out << 'L';
679
Anders Carlssonc4355b62009-10-07 01:45:02 +0000680 mangleSourceName(II);
681 break;
682 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000683
John McCall1dd73832010-02-04 01:42:13 +0000684 // Otherwise, an anonymous entity. We must have a declaration.
685 assert(ND && "mangling empty name without declaration");
686
687 if (const NamespaceDecl *NS = dyn_cast<NamespaceDecl>(ND)) {
688 if (NS->isAnonymousNamespace()) {
689 // This is how gcc mangles these names.
690 Out << "12_GLOBAL__N_1";
691 break;
692 }
693 }
694
Anders Carlsson6f7e2f42010-06-08 14:49:03 +0000695 if (const VarDecl *VD = dyn_cast<VarDecl>(ND)) {
696 // We must have an anonymous union or struct declaration.
697 const RecordDecl *RD =
698 cast<RecordDecl>(VD->getType()->getAs<RecordType>()->getDecl());
699
700 // Itanium C++ ABI 5.1.2:
701 //
702 // For the purposes of mangling, the name of an anonymous union is
703 // considered to be the name of the first named data member found by a
704 // pre-order, depth-first, declaration-order walk of the data members of
705 // the anonymous union. If there is no such data member (i.e., if all of
706 // the data members in the union are unnamed), then there is no way for
707 // a program to refer to the anonymous union, and there is therefore no
708 // need to mangle its name.
709 const FieldDecl *FD = FindFirstNamedDataMember(RD);
John McCall7121c8f2010-08-05 22:02:13 +0000710
711 // It's actually possible for various reasons for us to get here
712 // with an empty anonymous struct / union. Fortunately, it
713 // doesn't really matter what name we generate.
714 if (!FD) break;
Anders Carlsson6f7e2f42010-06-08 14:49:03 +0000715 assert(FD->getIdentifier() && "Data member name isn't an identifier!");
716
717 mangleSourceName(FD->getIdentifier());
718 break;
719 }
720
Anders Carlssonc4355b62009-10-07 01:45:02 +0000721 // We must have an anonymous struct.
722 const TagDecl *TD = cast<TagDecl>(ND);
723 if (const TypedefDecl *D = TD->getTypedefForAnonDecl()) {
724 assert(TD->getDeclContext() == D->getDeclContext() &&
725 "Typedef should not be in another decl context!");
726 assert(D->getDeclName().getAsIdentifierInfo() &&
727 "Typedef was not named!");
728 mangleSourceName(D->getDeclName().getAsIdentifierInfo());
729 break;
730 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000731
Anders Carlssonc4355b62009-10-07 01:45:02 +0000732 // Get a unique id for the anonymous struct.
733 uint64_t AnonStructId = Context.getAnonymousStructId(TD);
734
735 // Mangle it as a source name in the form
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000736 // [n] $_<id>
Anders Carlssonc4355b62009-10-07 01:45:02 +0000737 // where n is the length of the string.
738 llvm::SmallString<8> Str;
739 Str += "$_";
740 Str += llvm::utostr(AnonStructId);
741
742 Out << Str.size();
743 Out << Str.str();
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000744 break;
Anders Carlssonc4355b62009-10-07 01:45:02 +0000745 }
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000746
747 case DeclarationName::ObjCZeroArgSelector:
748 case DeclarationName::ObjCOneArgSelector:
749 case DeclarationName::ObjCMultiArgSelector:
750 assert(false && "Can't mangle Objective-C selector names here!");
751 break;
752
753 case DeclarationName::CXXConstructorName:
Anders Carlsson27ae5362009-04-17 01:58:57 +0000754 if (ND == Structor)
Mike Stump141c5af2009-09-02 00:25:38 +0000755 // If the named decl is the C++ constructor we're mangling, use the type
756 // we were given.
Anders Carlsson27ae5362009-04-17 01:58:57 +0000757 mangleCXXCtorType(static_cast<CXXCtorType>(StructorType));
Anders Carlsson3ac86b52009-04-15 05:36:58 +0000758 else
759 // Otherwise, use the complete constructor name. This is relevant if a
760 // class with a constructor is declared within a constructor.
761 mangleCXXCtorType(Ctor_Complete);
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000762 break;
763
764 case DeclarationName::CXXDestructorName:
Anders Carlsson27ae5362009-04-17 01:58:57 +0000765 if (ND == Structor)
Mike Stump141c5af2009-09-02 00:25:38 +0000766 // If the named decl is the C++ destructor we're mangling, use the type we
767 // were given.
Anders Carlsson27ae5362009-04-17 01:58:57 +0000768 mangleCXXDtorType(static_cast<CXXDtorType>(StructorType));
769 else
770 // Otherwise, use the complete destructor name. This is relevant if a
771 // class with a destructor is declared within a destructor.
772 mangleCXXDtorType(Dtor_Complete);
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000773 break;
774
775 case DeclarationName::CXXConversionFunctionName:
Mike Stump1eb44332009-09-09 15:08:12 +0000776 // <operator-name> ::= cv <type> # (cast)
Douglas Gregor219cc612009-02-13 01:28:03 +0000777 Out << "cv";
Anders Carlssonb5404912009-10-07 01:06:45 +0000778 mangleType(Context.getASTContext().getCanonicalType(Name.getCXXNameType()));
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000779 break;
780
Anders Carlsson8257d412009-12-22 06:36:32 +0000781 case DeclarationName::CXXOperatorName: {
John McCall1dd73832010-02-04 01:42:13 +0000782 unsigned Arity;
783 if (ND) {
784 Arity = cast<FunctionDecl>(ND)->getNumParams();
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000785
John McCall1dd73832010-02-04 01:42:13 +0000786 // If we have a C++ member function, we need to include the 'this' pointer.
787 // FIXME: This does not make sense for operators that are static, but their
788 // names stay the same regardless of the arity (operator new for instance).
789 if (isa<CXXMethodDecl>(ND))
790 Arity++;
791 } else
792 Arity = KnownArity;
793
Anders Carlsson8257d412009-12-22 06:36:32 +0000794 mangleOperatorName(Name.getCXXOverloadedOperator(), Arity);
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000795 break;
Anders Carlsson8257d412009-12-22 06:36:32 +0000796 }
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000797
Sean Hunt3e518bd2009-11-29 07:34:05 +0000798 case DeclarationName::CXXLiteralOperatorName:
Sean Hunt5dd6b392009-12-04 21:11:13 +0000799 // FIXME: This mangling is not yet official.
Sean Hunt2421f662009-12-04 21:01:37 +0000800 Out << "li";
Sean Hunt3e518bd2009-11-29 07:34:05 +0000801 mangleSourceName(Name.getCXXLiteralIdentifier());
802 break;
803
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000804 case DeclarationName::CXXUsingDirective:
805 assert(false && "Can't mangle a using directive name!");
Douglas Gregor219cc612009-02-13 01:28:03 +0000806 break;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000807 }
808}
809
810void CXXNameMangler::mangleSourceName(const IdentifierInfo *II) {
811 // <source-name> ::= <positive length number> <identifier>
812 // <number> ::= [n] <non-negative decimal integer>
813 // <identifier> ::= <unqualified source code identifier>
814 Out << II->getLength() << II->getName();
815}
816
Eli Friedman7facf842009-12-02 20:32:49 +0000817void CXXNameMangler::mangleNestedName(const NamedDecl *ND,
Fariborz Jahanian57058532010-03-03 19:41:08 +0000818 const DeclContext *DC,
819 bool NoFunction) {
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000820 // <nested-name> ::= N [<CV-qualifiers>] <prefix> <unqualified-name> E
821 // ::= N [<CV-qualifiers>] <template-prefix> <template-args> E
Anders Carlssond99edc42009-09-26 03:55:37 +0000822
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000823 Out << 'N';
824 if (const CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(ND))
John McCall0953e762009-09-24 19:53:00 +0000825 mangleQualifiers(Qualifiers::fromCVRMask(Method->getTypeQualifiers()));
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000826
Anders Carlsson2744a062009-09-18 19:00:18 +0000827 // Check if we have a template.
828 const TemplateArgumentList *TemplateArgs = 0;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000829 if (const TemplateDecl *TD = isTemplate(ND, TemplateArgs)) {
Anders Carlsson2744a062009-09-18 19:00:18 +0000830 mangleTemplatePrefix(TD);
Rafael Espindolad9800722010-03-11 14:07:00 +0000831 TemplateParameterList *TemplateParameters = TD->getTemplateParameters();
832 mangleTemplateArgs(*TemplateParameters, *TemplateArgs);
Fariborz Jahanian57058532010-03-03 19:41:08 +0000833 }
834 else {
835 manglePrefix(DC, NoFunction);
Anders Carlsson7482e242009-09-18 04:29:09 +0000836 mangleUnqualifiedName(ND);
837 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000838
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000839 Out << 'E';
840}
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000841void CXXNameMangler::mangleNestedName(const TemplateDecl *TD,
Anders Carlsson7624f212009-09-18 02:42:01 +0000842 const TemplateArgument *TemplateArgs,
843 unsigned NumTemplateArgs) {
Anders Carlssone45117b2009-09-27 19:53:49 +0000844 // <nested-name> ::= N [<CV-qualifiers>] <template-prefix> <template-args> E
845
Anders Carlsson7624f212009-09-18 02:42:01 +0000846 Out << 'N';
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000847
Anders Carlssone45117b2009-09-27 19:53:49 +0000848 mangleTemplatePrefix(TD);
Rafael Espindolad9800722010-03-11 14:07:00 +0000849 TemplateParameterList *TemplateParameters = TD->getTemplateParameters();
850 mangleTemplateArgs(*TemplateParameters, TemplateArgs, NumTemplateArgs);
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000851
Anders Carlsson7624f212009-09-18 02:42:01 +0000852 Out << 'E';
853}
854
Anders Carlsson1b42c792009-04-02 16:24:45 +0000855void CXXNameMangler::mangleLocalName(const NamedDecl *ND) {
856 // <local-name> := Z <function encoding> E <entity name> [<discriminator>]
857 // := Z <function encoding> E s [<discriminator>]
Mike Stump1eb44332009-09-09 15:08:12 +0000858 // <discriminator> := _ <non-negative number>
Fariborz Jahanian57058532010-03-03 19:41:08 +0000859 const DeclContext *DC = ND->getDeclContext();
Anders Carlsson1b42c792009-04-02 16:24:45 +0000860 Out << 'Z';
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000861
Charles Davis685b1d92010-05-26 18:25:27 +0000862 if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(DC)) {
863 mangleObjCMethodName(MD);
John McCall82b7d7b2010-10-18 21:28:44 +0000864 } else if (const CXXRecordDecl *RD = GetLocalClassDecl(ND)) {
865 mangleFunctionEncoding(cast<FunctionDecl>(RD->getDeclContext()));
Fariborz Jahanian57058532010-03-03 19:41:08 +0000866 Out << 'E';
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000867
John McCall82b7d7b2010-10-18 21:28:44 +0000868 // Mangle the name relative to the closest enclosing function.
869 if (ND == RD) // equality ok because RD derived from ND above
870 mangleUnqualifiedName(ND);
871 else
872 mangleNestedName(ND, DC, true /*NoFunction*/);
873
Fariborz Jahanian4819ac42010-03-04 01:02:03 +0000874 unsigned disc;
John McCall82b7d7b2010-10-18 21:28:44 +0000875 if (Context.getNextDiscriminator(RD, disc)) {
Fariborz Jahanian4819ac42010-03-04 01:02:03 +0000876 if (disc < 10)
877 Out << '_' << disc;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000878 else
Fariborz Jahanian4819ac42010-03-04 01:02:03 +0000879 Out << "__" << disc << '_';
880 }
Fariborz Jahanian57058532010-03-03 19:41:08 +0000881
882 return;
883 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000884 else
Fariborz Jahanian57058532010-03-03 19:41:08 +0000885 mangleFunctionEncoding(cast<FunctionDecl>(DC));
Anders Carlsson7b06f6c2009-12-10 03:14:39 +0000886
Anders Carlsson1b42c792009-04-02 16:24:45 +0000887 Out << 'E';
Eli Friedman6f9f25d2009-12-11 20:21:38 +0000888 mangleUnqualifiedName(ND);
Anders Carlsson1b42c792009-04-02 16:24:45 +0000889}
890
Fariborz Jahanian57058532010-03-03 19:41:08 +0000891void CXXNameMangler::manglePrefix(const DeclContext *DC, bool NoFunction) {
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000892 // <prefix> ::= <prefix> <unqualified-name>
893 // ::= <template-prefix> <template-args>
894 // ::= <template-param>
895 // ::= # empty
896 // ::= <substitution>
Anders Carlsson6862fc72009-09-17 04:16:28 +0000897
Anders Carlssonadd28822009-09-22 20:33:31 +0000898 while (isa<LinkageSpecDecl>(DC))
899 DC = DC->getParent();
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000900
Anders Carlsson9263e912009-09-18 18:39:58 +0000901 if (DC->isTranslationUnit())
902 return;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000903
Douglas Gregor35415f52010-05-25 17:04:15 +0000904 if (const BlockDecl *Block = dyn_cast<BlockDecl>(DC)) {
905 manglePrefix(DC->getParent(), NoFunction);
906 llvm::SmallString<64> Name;
Peter Collingbourne14110472011-01-13 18:57:25 +0000907 Context.mangleBlock(Block, Name);
Douglas Gregor35415f52010-05-25 17:04:15 +0000908 Out << Name.size() << Name;
909 return;
910 }
911
Anders Carlsson6862fc72009-09-17 04:16:28 +0000912 if (mangleSubstitution(cast<NamedDecl>(DC)))
913 return;
Anders Carlsson7482e242009-09-18 04:29:09 +0000914
Anders Carlsson2ee3fca2009-09-18 20:11:09 +0000915 // Check if we have a template.
916 const TemplateArgumentList *TemplateArgs = 0;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000917 if (const TemplateDecl *TD = isTemplate(cast<NamedDecl>(DC), TemplateArgs)) {
Anders Carlsson2ee3fca2009-09-18 20:11:09 +0000918 mangleTemplatePrefix(TD);
Rafael Espindolad9800722010-03-11 14:07:00 +0000919 TemplateParameterList *TemplateParameters = TD->getTemplateParameters();
920 mangleTemplateArgs(*TemplateParameters, *TemplateArgs);
Fariborz Jahanian57058532010-03-03 19:41:08 +0000921 }
Douglas Gregor35415f52010-05-25 17:04:15 +0000922 else if(NoFunction && (isa<FunctionDecl>(DC) || isa<ObjCMethodDecl>(DC)))
Fariborz Jahanian57058532010-03-03 19:41:08 +0000923 return;
Douglas Gregor35415f52010-05-25 17:04:15 +0000924 else if (const ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(DC))
925 mangleObjCMethodName(Method);
Fariborz Jahanian57058532010-03-03 19:41:08 +0000926 else {
927 manglePrefix(DC->getParent(), NoFunction);
Anders Carlsson2ee3fca2009-09-18 20:11:09 +0000928 mangleUnqualifiedName(cast<NamedDecl>(DC));
929 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000930
Anders Carlsson6862fc72009-09-17 04:16:28 +0000931 addSubstitution(cast<NamedDecl>(DC));
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000932}
933
Douglas Gregor20f0cc72010-04-23 03:10:43 +0000934void CXXNameMangler::mangleTemplatePrefix(TemplateName Template) {
935 // <template-prefix> ::= <prefix> <template unqualified-name>
936 // ::= <template-param>
937 // ::= <substitution>
938 if (TemplateDecl *TD = Template.getAsTemplateDecl())
939 return mangleTemplatePrefix(TD);
940
941 if (QualifiedTemplateName *Qualified = Template.getAsQualifiedTemplateName())
942 mangleUnresolvedScope(Qualified->getQualifier());
Sean Huntc3021132010-05-05 15:23:54 +0000943
Douglas Gregor20f0cc72010-04-23 03:10:43 +0000944 if (OverloadedTemplateStorage *Overloaded
945 = Template.getAsOverloadedTemplate()) {
Sean Huntc3021132010-05-05 15:23:54 +0000946 mangleUnqualifiedName(0, (*Overloaded->begin())->getDeclName(),
Douglas Gregor20f0cc72010-04-23 03:10:43 +0000947 UnknownArity);
948 return;
949 }
Sean Huntc3021132010-05-05 15:23:54 +0000950
Douglas Gregor20f0cc72010-04-23 03:10:43 +0000951 DependentTemplateName *Dependent = Template.getAsDependentTemplateName();
952 assert(Dependent && "Unknown template name kind?");
953 mangleUnresolvedScope(Dependent->getQualifier());
Douglas Gregor1e9268e2010-04-28 05:58:56 +0000954 mangleUnscopedTemplateName(Template);
Douglas Gregor20f0cc72010-04-23 03:10:43 +0000955}
956
Anders Carlsson0fa6df42009-09-26 19:45:45 +0000957void CXXNameMangler::mangleTemplatePrefix(const TemplateDecl *ND) {
Anders Carlsson7482e242009-09-18 04:29:09 +0000958 // <template-prefix> ::= <prefix> <template unqualified-name>
959 // ::= <template-param>
960 // ::= <substitution>
Douglas Gregor32fb4e12010-02-05 20:45:00 +0000961 // <template-template-param> ::= <template-param>
962 // <substitution>
Anders Carlsson7482e242009-09-18 04:29:09 +0000963
Anders Carlssonaeb85372009-09-26 22:18:22 +0000964 if (mangleSubstitution(ND))
965 return;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000966
Douglas Gregor32fb4e12010-02-05 20:45:00 +0000967 // <template-template-param> ::= <template-param>
968 if (const TemplateTemplateParmDecl *TTP
969 = dyn_cast<TemplateTemplateParmDecl>(ND)) {
970 mangleTemplateParameter(TTP->getIndex());
971 return;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000972 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000973
Anders Carlssonaa73ab12009-09-18 18:47:07 +0000974 manglePrefix(ND->getDeclContext());
Anders Carlsson1668f202009-09-26 20:13:56 +0000975 mangleUnqualifiedName(ND->getTemplatedDecl());
Anders Carlssonaeb85372009-09-26 22:18:22 +0000976 addSubstitution(ND);
Anders Carlsson7482e242009-09-18 04:29:09 +0000977}
978
John McCallb6f532e2010-07-14 06:43:17 +0000979/// Mangles a template name under the production <type>. Required for
980/// template template arguments.
981/// <type> ::= <class-enum-type>
982/// ::= <template-param>
983/// ::= <substitution>
984void CXXNameMangler::mangleType(TemplateName TN) {
985 if (mangleSubstitution(TN))
986 return;
987
988 TemplateDecl *TD = 0;
989
990 switch (TN.getKind()) {
991 case TemplateName::QualifiedTemplate:
992 TD = TN.getAsQualifiedTemplateName()->getTemplateDecl();
993 goto HaveDecl;
994
995 case TemplateName::Template:
996 TD = TN.getAsTemplateDecl();
997 goto HaveDecl;
998
999 HaveDecl:
1000 if (isa<TemplateTemplateParmDecl>(TD))
1001 mangleTemplateParameter(cast<TemplateTemplateParmDecl>(TD)->getIndex());
1002 else
1003 mangleName(TD);
1004 break;
1005
1006 case TemplateName::OverloadedTemplate:
1007 llvm_unreachable("can't mangle an overloaded template name as a <type>");
1008 break;
1009
1010 case TemplateName::DependentTemplate: {
1011 const DependentTemplateName *Dependent = TN.getAsDependentTemplateName();
1012 assert(Dependent->isIdentifier());
1013
1014 // <class-enum-type> ::= <name>
1015 // <name> ::= <nested-name>
1016 mangleUnresolvedScope(Dependent->getQualifier());
1017 mangleSourceName(Dependent->getIdentifier());
1018 break;
1019 }
1020
Douglas Gregor1aee05d2011-01-15 06:45:20 +00001021 case TemplateName::SubstTemplateTemplateParmPack: {
1022 SubstTemplateTemplateParmPackStorage *SubstPack
1023 = TN.getAsSubstTemplateTemplateParmPack();
1024 mangleTemplateParameter(SubstPack->getParameterPack()->getIndex());
1025 break;
1026 }
John McCallb6f532e2010-07-14 06:43:17 +00001027 }
1028
1029 addSubstitution(TN);
1030}
1031
Mike Stump1eb44332009-09-09 15:08:12 +00001032void
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001033CXXNameMangler::mangleOperatorName(OverloadedOperatorKind OO, unsigned Arity) {
1034 switch (OO) {
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001035 // <operator-name> ::= nw # new
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001036 case OO_New: Out << "nw"; break;
1037 // ::= na # new[]
1038 case OO_Array_New: Out << "na"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001039 // ::= dl # delete
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001040 case OO_Delete: Out << "dl"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001041 // ::= da # delete[]
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001042 case OO_Array_Delete: Out << "da"; break;
1043 // ::= ps # + (unary)
John McCall5e1e89b2010-08-18 19:18:59 +00001044 // ::= pl # + (binary or unknown)
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001045 case OO_Plus:
Anders Carlsson8257d412009-12-22 06:36:32 +00001046 Out << (Arity == 1? "ps" : "pl"); break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001047 // ::= ng # - (unary)
John McCall5e1e89b2010-08-18 19:18:59 +00001048 // ::= mi # - (binary or unknown)
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001049 case OO_Minus:
Anders Carlsson8257d412009-12-22 06:36:32 +00001050 Out << (Arity == 1? "ng" : "mi"); break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001051 // ::= ad # & (unary)
John McCall5e1e89b2010-08-18 19:18:59 +00001052 // ::= an # & (binary or unknown)
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001053 case OO_Amp:
Anders Carlsson8257d412009-12-22 06:36:32 +00001054 Out << (Arity == 1? "ad" : "an"); break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001055 // ::= de # * (unary)
John McCall5e1e89b2010-08-18 19:18:59 +00001056 // ::= ml # * (binary or unknown)
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001057 case OO_Star:
John McCall5e1e89b2010-08-18 19:18:59 +00001058 // Use binary when unknown.
Anders Carlsson8257d412009-12-22 06:36:32 +00001059 Out << (Arity == 1? "de" : "ml"); break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001060 // ::= co # ~
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001061 case OO_Tilde: Out << "co"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001062 // ::= dv # /
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001063 case OO_Slash: Out << "dv"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001064 // ::= rm # %
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001065 case OO_Percent: Out << "rm"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001066 // ::= or # |
1067 case OO_Pipe: Out << "or"; break;
1068 // ::= eo # ^
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001069 case OO_Caret: Out << "eo"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001070 // ::= aS # =
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001071 case OO_Equal: Out << "aS"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001072 // ::= pL # +=
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001073 case OO_PlusEqual: Out << "pL"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001074 // ::= mI # -=
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001075 case OO_MinusEqual: Out << "mI"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001076 // ::= mL # *=
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001077 case OO_StarEqual: Out << "mL"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001078 // ::= dV # /=
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001079 case OO_SlashEqual: Out << "dV"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001080 // ::= rM # %=
1081 case OO_PercentEqual: Out << "rM"; break;
1082 // ::= aN # &=
1083 case OO_AmpEqual: Out << "aN"; break;
1084 // ::= oR # |=
1085 case OO_PipeEqual: Out << "oR"; break;
1086 // ::= eO # ^=
1087 case OO_CaretEqual: Out << "eO"; break;
1088 // ::= ls # <<
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001089 case OO_LessLess: Out << "ls"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001090 // ::= rs # >>
1091 case OO_GreaterGreater: Out << "rs"; break;
1092 // ::= lS # <<=
1093 case OO_LessLessEqual: Out << "lS"; break;
1094 // ::= rS # >>=
1095 case OO_GreaterGreaterEqual: Out << "rS"; break;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001096 // ::= eq # ==
1097 case OO_EqualEqual: Out << "eq"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001098 // ::= ne # !=
1099 case OO_ExclaimEqual: Out << "ne"; break;
1100 // ::= lt # <
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001101 case OO_Less: Out << "lt"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001102 // ::= gt # >
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001103 case OO_Greater: Out << "gt"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001104 // ::= le # <=
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001105 case OO_LessEqual: Out << "le"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001106 // ::= ge # >=
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001107 case OO_GreaterEqual: Out << "ge"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001108 // ::= nt # !
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001109 case OO_Exclaim: Out << "nt"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001110 // ::= aa # &&
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001111 case OO_AmpAmp: Out << "aa"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001112 // ::= oo # ||
1113 case OO_PipePipe: Out << "oo"; break;
1114 // ::= pp # ++
1115 case OO_PlusPlus: Out << "pp"; break;
1116 // ::= mm # --
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001117 case OO_MinusMinus: Out << "mm"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001118 // ::= cm # ,
1119 case OO_Comma: Out << "cm"; break;
1120 // ::= pm # ->*
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001121 case OO_ArrowStar: Out << "pm"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001122 // ::= pt # ->
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001123 case OO_Arrow: Out << "pt"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001124 // ::= cl # ()
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001125 case OO_Call: Out << "cl"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001126 // ::= ix # []
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001127 case OO_Subscript: Out << "ix"; break;
Anders Carlssone170ba72009-12-14 01:45:37 +00001128
1129 // ::= qu # ?
1130 // The conditional operator can't be overloaded, but we still handle it when
1131 // mangling expressions.
1132 case OO_Conditional: Out << "qu"; break;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001133
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001134 case OO_None:
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001135 case NUM_OVERLOADED_OPERATORS:
Mike Stump1eb44332009-09-09 15:08:12 +00001136 assert(false && "Not an overloaded operator");
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001137 break;
1138 }
1139}
1140
John McCall0953e762009-09-24 19:53:00 +00001141void CXXNameMangler::mangleQualifiers(Qualifiers Quals) {
Mike Stump1eb44332009-09-09 15:08:12 +00001142 // <CV-qualifiers> ::= [r] [V] [K] # restrict (C99), volatile, const
John McCall0953e762009-09-24 19:53:00 +00001143 if (Quals.hasRestrict())
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001144 Out << 'r';
John McCall0953e762009-09-24 19:53:00 +00001145 if (Quals.hasVolatile())
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001146 Out << 'V';
John McCall0953e762009-09-24 19:53:00 +00001147 if (Quals.hasConst())
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001148 Out << 'K';
John McCall0953e762009-09-24 19:53:00 +00001149
Douglas Gregor56079f72010-06-14 23:15:08 +00001150 if (Quals.hasAddressSpace()) {
1151 // Extension:
1152 //
1153 // <type> ::= U <address-space-number>
1154 //
1155 // where <address-space-number> is a source name consisting of 'AS'
1156 // followed by the address space <number>.
1157 llvm::SmallString<64> ASString;
1158 ASString = "AS" + llvm::utostr_32(Quals.getAddressSpace());
1159 Out << 'U' << ASString.size() << ASString;
1160 }
1161
John McCall0953e762009-09-24 19:53:00 +00001162 // FIXME: For now, just drop all extension qualifiers on the floor.
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001163}
1164
Anders Carlsson7b06f6c2009-12-10 03:14:39 +00001165void CXXNameMangler::mangleObjCMethodName(const ObjCMethodDecl *MD) {
Charles Davis685b1d92010-05-26 18:25:27 +00001166 llvm::SmallString<64> Buffer;
Peter Collingbourne14110472011-01-13 18:57:25 +00001167 Context.mangleObjCMethodName(MD, Buffer);
Charles Davis685b1d92010-05-26 18:25:27 +00001168 Out << Buffer;
Anders Carlsson7b06f6c2009-12-10 03:14:39 +00001169}
1170
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001171void CXXNameMangler::mangleType(QualType T) {
Anders Carlsson4843e582009-03-10 17:07:44 +00001172 // Only operate on the canonical type!
Anders Carlssonb5404912009-10-07 01:06:45 +00001173 T = Context.getASTContext().getCanonicalType(T);
Anders Carlsson4843e582009-03-10 17:07:44 +00001174
Douglas Gregora4923eb2009-11-16 21:35:15 +00001175 bool IsSubstitutable = T.hasLocalQualifiers() || !isa<BuiltinType>(T);
Anders Carlsson76967372009-09-17 00:43:46 +00001176 if (IsSubstitutable && mangleSubstitution(T))
1177 return;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001178
Douglas Gregora4923eb2009-11-16 21:35:15 +00001179 if (Qualifiers Quals = T.getLocalQualifiers()) {
John McCall0953e762009-09-24 19:53:00 +00001180 mangleQualifiers(Quals);
1181 // Recurse: even if the qualified type isn't yet substitutable,
1182 // the unqualified type might be.
Douglas Gregora4923eb2009-11-16 21:35:15 +00001183 mangleType(T.getLocalUnqualifiedType());
Anders Carlsson76967372009-09-17 00:43:46 +00001184 } else {
1185 switch (T->getTypeClass()) {
John McCallefe6aee2009-09-05 07:56:18 +00001186#define ABSTRACT_TYPE(CLASS, PARENT)
1187#define NON_CANONICAL_TYPE(CLASS, PARENT) \
Anders Carlsson76967372009-09-17 00:43:46 +00001188 case Type::CLASS: \
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +00001189 llvm_unreachable("can't mangle non-canonical type " #CLASS "Type"); \
Anders Carlsson76967372009-09-17 00:43:46 +00001190 return;
John McCallefe6aee2009-09-05 07:56:18 +00001191#define TYPE(CLASS, PARENT) \
Anders Carlsson76967372009-09-17 00:43:46 +00001192 case Type::CLASS: \
John McCall0953e762009-09-24 19:53:00 +00001193 mangleType(static_cast<const CLASS##Type*>(T.getTypePtr())); \
Anders Carlsson76967372009-09-17 00:43:46 +00001194 break;
John McCallefe6aee2009-09-05 07:56:18 +00001195#include "clang/AST/TypeNodes.def"
Anders Carlsson76967372009-09-17 00:43:46 +00001196 }
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001197 }
Anders Carlsson76967372009-09-17 00:43:46 +00001198
1199 // Add the substitution.
1200 if (IsSubstitutable)
1201 addSubstitution(T);
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001202}
1203
Douglas Gregor1b12a3b2010-05-26 05:11:13 +00001204void CXXNameMangler::mangleNameOrStandardSubstitution(const NamedDecl *ND) {
1205 if (!mangleStandardSubstitution(ND))
1206 mangleName(ND);
1207}
1208
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001209void CXXNameMangler::mangleType(const BuiltinType *T) {
John McCallefe6aee2009-09-05 07:56:18 +00001210 // <type> ::= <builtin-type>
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001211 // <builtin-type> ::= v # void
1212 // ::= w # wchar_t
1213 // ::= b # bool
1214 // ::= c # char
1215 // ::= a # signed char
1216 // ::= h # unsigned char
1217 // ::= s # short
1218 // ::= t # unsigned short
1219 // ::= i # int
1220 // ::= j # unsigned int
1221 // ::= l # long
1222 // ::= m # unsigned long
1223 // ::= x # long long, __int64
1224 // ::= y # unsigned long long, __int64
1225 // ::= n # __int128
1226 // UNSUPPORTED: ::= o # unsigned __int128
1227 // ::= f # float
1228 // ::= d # double
1229 // ::= e # long double, __float80
1230 // UNSUPPORTED: ::= g # __float128
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001231 // UNSUPPORTED: ::= Dd # IEEE 754r decimal floating point (64 bits)
1232 // UNSUPPORTED: ::= De # IEEE 754r decimal floating point (128 bits)
1233 // UNSUPPORTED: ::= Df # IEEE 754r decimal floating point (32 bits)
1234 // UNSUPPORTED: ::= Dh # IEEE 754r half-precision floating point (16 bits)
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001235 // ::= Di # char32_t
1236 // ::= Ds # char16_t
Anders Carlssone2923682010-11-04 04:31:32 +00001237 // ::= Dn # std::nullptr_t (i.e., decltype(nullptr))
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001238 // ::= u <source-name> # vendor extended type
1239 switch (T->getKind()) {
1240 case BuiltinType::Void: Out << 'v'; break;
1241 case BuiltinType::Bool: Out << 'b'; break;
1242 case BuiltinType::Char_U: case BuiltinType::Char_S: Out << 'c'; break;
1243 case BuiltinType::UChar: Out << 'h'; break;
1244 case BuiltinType::UShort: Out << 't'; break;
1245 case BuiltinType::UInt: Out << 'j'; break;
1246 case BuiltinType::ULong: Out << 'm'; break;
1247 case BuiltinType::ULongLong: Out << 'y'; break;
Chris Lattner2df9ced2009-04-30 02:43:43 +00001248 case BuiltinType::UInt128: Out << 'o'; break;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001249 case BuiltinType::SChar: Out << 'a'; break;
Chris Lattner3f59c972010-12-25 23:25:43 +00001250 case BuiltinType::WChar_S:
1251 case BuiltinType::WChar_U: Out << 'w'; break;
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001252 case BuiltinType::Char16: Out << "Ds"; break;
1253 case BuiltinType::Char32: Out << "Di"; break;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001254 case BuiltinType::Short: Out << 's'; break;
1255 case BuiltinType::Int: Out << 'i'; break;
1256 case BuiltinType::Long: Out << 'l'; break;
1257 case BuiltinType::LongLong: Out << 'x'; break;
Chris Lattner2df9ced2009-04-30 02:43:43 +00001258 case BuiltinType::Int128: Out << 'n'; break;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001259 case BuiltinType::Float: Out << 'f'; break;
1260 case BuiltinType::Double: Out << 'd'; break;
1261 case BuiltinType::LongDouble: Out << 'e'; break;
Anders Carlssone2923682010-11-04 04:31:32 +00001262 case BuiltinType::NullPtr: Out << "Dn"; break;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001263
1264 case BuiltinType::Overload:
1265 case BuiltinType::Dependent:
Mike Stump1eb44332009-09-09 15:08:12 +00001266 assert(false &&
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001267 "Overloaded and dependent types shouldn't get to name mangling");
1268 break;
Anders Carlssone89d1592009-06-26 18:41:36 +00001269 case BuiltinType::UndeducedAuto:
1270 assert(0 && "Should not see undeduced auto here");
1271 break;
Steve Naroff9533a7f2009-07-22 17:14:51 +00001272 case BuiltinType::ObjCId: Out << "11objc_object"; break;
1273 case BuiltinType::ObjCClass: Out << "10objc_class"; break;
Fariborz Jahanian13dcd002009-11-21 19:53:08 +00001274 case BuiltinType::ObjCSel: Out << "13objc_selector"; break;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001275 }
1276}
1277
John McCallefe6aee2009-09-05 07:56:18 +00001278// <type> ::= <function-type>
1279// <function-type> ::= F [Y] <bare-function-type> E
1280void CXXNameMangler::mangleType(const FunctionProtoType *T) {
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001281 Out << 'F';
Mike Stumpf5408fe2009-05-16 07:57:57 +00001282 // FIXME: We don't have enough information in the AST to produce the 'Y'
1283 // encoding for extern "C" function types.
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001284 mangleBareFunctionType(T, /*MangleReturnType=*/true);
1285 Out << 'E';
1286}
John McCallefe6aee2009-09-05 07:56:18 +00001287void CXXNameMangler::mangleType(const FunctionNoProtoType *T) {
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +00001288 llvm_unreachable("Can't mangle K&R function prototypes");
John McCallefe6aee2009-09-05 07:56:18 +00001289}
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001290void CXXNameMangler::mangleBareFunctionType(const FunctionType *T,
1291 bool MangleReturnType) {
John McCallefe6aee2009-09-05 07:56:18 +00001292 // We should never be mangling something without a prototype.
1293 const FunctionProtoType *Proto = cast<FunctionProtoType>(T);
1294
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001295 // <bare-function-type> ::= <signature type>+
1296 if (MangleReturnType)
John McCallefe6aee2009-09-05 07:56:18 +00001297 mangleType(Proto->getResultType());
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001298
Anders Carlsson93296682010-06-02 04:40:13 +00001299 if (Proto->getNumArgs() == 0 && !Proto->isVariadic()) {
Eli Friedmana7e68452010-08-22 01:00:03 +00001300 // <builtin-type> ::= v # void
Anders Carlssonc6c91bc2009-04-01 00:15:23 +00001301 Out << 'v';
1302 return;
1303 }
Mike Stump1eb44332009-09-09 15:08:12 +00001304
Douglas Gregor72564e72009-02-26 23:50:07 +00001305 for (FunctionProtoType::arg_type_iterator Arg = Proto->arg_type_begin(),
Mike Stump1eb44332009-09-09 15:08:12 +00001306 ArgEnd = Proto->arg_type_end();
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001307 Arg != ArgEnd; ++Arg)
1308 mangleType(*Arg);
Douglas Gregor219cc612009-02-13 01:28:03 +00001309
1310 // <builtin-type> ::= z # ellipsis
1311 if (Proto->isVariadic())
1312 Out << 'z';
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001313}
1314
John McCallefe6aee2009-09-05 07:56:18 +00001315// <type> ::= <class-enum-type>
Mike Stump1eb44332009-09-09 15:08:12 +00001316// <class-enum-type> ::= <name>
John McCalled976492009-12-04 22:46:56 +00001317void CXXNameMangler::mangleType(const UnresolvedUsingType *T) {
1318 mangleName(T->getDecl());
1319}
1320
1321// <type> ::= <class-enum-type>
1322// <class-enum-type> ::= <name>
John McCallefe6aee2009-09-05 07:56:18 +00001323void CXXNameMangler::mangleType(const EnumType *T) {
1324 mangleType(static_cast<const TagType*>(T));
1325}
1326void CXXNameMangler::mangleType(const RecordType *T) {
1327 mangleType(static_cast<const TagType*>(T));
1328}
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001329void CXXNameMangler::mangleType(const TagType *T) {
Eli Friedmanecb7e932009-12-11 18:00:57 +00001330 mangleName(T->getDecl());
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001331}
1332
John McCallefe6aee2009-09-05 07:56:18 +00001333// <type> ::= <array-type>
1334// <array-type> ::= A <positive dimension number> _ <element type>
1335// ::= A [<dimension expression>] _ <element type>
1336void CXXNameMangler::mangleType(const ConstantArrayType *T) {
1337 Out << 'A' << T->getSize() << '_';
1338 mangleType(T->getElementType());
1339}
1340void CXXNameMangler::mangleType(const VariableArrayType *T) {
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001341 Out << 'A';
Fariborz Jahanian7281d1f2010-11-02 16:54:00 +00001342 // decayed vla types (size 0) will just be skipped.
1343 if (T->getSizeExpr())
1344 mangleExpression(T->getSizeExpr());
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001345 Out << '_';
1346 mangleType(T->getElementType());
1347}
John McCallefe6aee2009-09-05 07:56:18 +00001348void CXXNameMangler::mangleType(const DependentSizedArrayType *T) {
1349 Out << 'A';
1350 mangleExpression(T->getSizeExpr());
1351 Out << '_';
1352 mangleType(T->getElementType());
1353}
1354void CXXNameMangler::mangleType(const IncompleteArrayType *T) {
Nick Lewycky271b6652010-09-05 03:40:33 +00001355 Out << "A_";
John McCallefe6aee2009-09-05 07:56:18 +00001356 mangleType(T->getElementType());
1357}
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001358
John McCallefe6aee2009-09-05 07:56:18 +00001359// <type> ::= <pointer-to-member-type>
1360// <pointer-to-member-type> ::= M <class type> <member type>
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001361void CXXNameMangler::mangleType(const MemberPointerType *T) {
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001362 Out << 'M';
1363 mangleType(QualType(T->getClass(), 0));
Anders Carlsson0e650012009-05-17 17:41:20 +00001364 QualType PointeeType = T->getPointeeType();
1365 if (const FunctionProtoType *FPT = dyn_cast<FunctionProtoType>(PointeeType)) {
John McCall0953e762009-09-24 19:53:00 +00001366 mangleQualifiers(Qualifiers::fromCVRMask(FPT->getTypeQuals()));
Anders Carlsson0e650012009-05-17 17:41:20 +00001367 mangleType(FPT);
Anders Carlsson9d85b722010-06-02 04:29:50 +00001368
1369 // Itanium C++ ABI 5.1.8:
1370 //
1371 // The type of a non-static member function is considered to be different,
1372 // for the purposes of substitution, from the type of a namespace-scope or
1373 // static member function whose type appears similar. The types of two
1374 // non-static member functions are considered to be different, for the
1375 // purposes of substitution, if the functions are members of different
1376 // classes. In other words, for the purposes of substitution, the class of
1377 // which the function is a member is considered part of the type of
1378 // function.
1379
1380 // We increment the SeqID here to emulate adding an entry to the
1381 // substitution table. We can't actually add it because we don't want this
1382 // particular function type to be substituted.
1383 ++SeqID;
Mike Stump1eb44332009-09-09 15:08:12 +00001384 } else
Anders Carlsson0e650012009-05-17 17:41:20 +00001385 mangleType(PointeeType);
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001386}
1387
John McCallefe6aee2009-09-05 07:56:18 +00001388// <type> ::= <template-param>
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001389void CXXNameMangler::mangleType(const TemplateTypeParmType *T) {
Anders Carlsson0ccdf8d2009-09-27 00:38:53 +00001390 mangleTemplateParameter(T->getIndex());
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001391}
1392
Douglas Gregorc3069d62011-01-14 02:55:32 +00001393// <type> ::= <template-param>
1394void CXXNameMangler::mangleType(const SubstTemplateTypeParmPackType *T) {
1395 mangleTemplateParameter(T->getReplacedParameter()->getIndex());
1396}
1397
John McCallefe6aee2009-09-05 07:56:18 +00001398// <type> ::= P <type> # pointer-to
1399void CXXNameMangler::mangleType(const PointerType *T) {
1400 Out << 'P';
1401 mangleType(T->getPointeeType());
1402}
1403void CXXNameMangler::mangleType(const ObjCObjectPointerType *T) {
1404 Out << 'P';
1405 mangleType(T->getPointeeType());
1406}
1407
1408// <type> ::= R <type> # reference-to
1409void CXXNameMangler::mangleType(const LValueReferenceType *T) {
1410 Out << 'R';
1411 mangleType(T->getPointeeType());
1412}
1413
1414// <type> ::= O <type> # rvalue reference-to (C++0x)
1415void CXXNameMangler::mangleType(const RValueReferenceType *T) {
1416 Out << 'O';
1417 mangleType(T->getPointeeType());
1418}
1419
1420// <type> ::= C <type> # complex pair (C 2000)
1421void CXXNameMangler::mangleType(const ComplexType *T) {
1422 Out << 'C';
1423 mangleType(T->getElementType());
1424}
1425
Bob Wilsonc7df92d2010-11-12 17:24:43 +00001426// ARM's ABI for Neon vector types specifies that they should be mangled as
Bob Wilson57147a82010-11-16 00:32:18 +00001427// if they are structs (to match ARM's initial implementation). The
1428// vector type must be one of the special types predefined by ARM.
1429void CXXNameMangler::mangleNeonVectorType(const VectorType *T) {
Bob Wilsonc7df92d2010-11-12 17:24:43 +00001430 QualType EltType = T->getElementType();
Bob Wilson57147a82010-11-16 00:32:18 +00001431 assert(EltType->isBuiltinType() && "Neon vector element not a BuiltinType");
Bob Wilsonc7df92d2010-11-12 17:24:43 +00001432 const char *EltName = 0;
Bob Wilson491328c2010-11-12 17:24:46 +00001433 if (T->getVectorKind() == VectorType::NeonPolyVector) {
1434 switch (cast<BuiltinType>(EltType)->getKind()) {
Bob Wilson4cfaa5d2010-11-12 17:24:49 +00001435 case BuiltinType::SChar: EltName = "poly8_t"; break;
1436 case BuiltinType::Short: EltName = "poly16_t"; break;
Bob Wilson57147a82010-11-16 00:32:18 +00001437 default: llvm_unreachable("unexpected Neon polynomial vector element type");
Bob Wilson491328c2010-11-12 17:24:46 +00001438 }
1439 } else {
1440 switch (cast<BuiltinType>(EltType)->getKind()) {
Bob Wilson4cfaa5d2010-11-12 17:24:49 +00001441 case BuiltinType::SChar: EltName = "int8_t"; break;
1442 case BuiltinType::UChar: EltName = "uint8_t"; break;
1443 case BuiltinType::Short: EltName = "int16_t"; break;
1444 case BuiltinType::UShort: EltName = "uint16_t"; break;
1445 case BuiltinType::Int: EltName = "int32_t"; break;
1446 case BuiltinType::UInt: EltName = "uint32_t"; break;
1447 case BuiltinType::LongLong: EltName = "int64_t"; break;
1448 case BuiltinType::ULongLong: EltName = "uint64_t"; break;
1449 case BuiltinType::Float: EltName = "float32_t"; break;
Bob Wilson57147a82010-11-16 00:32:18 +00001450 default: llvm_unreachable("unexpected Neon vector element type");
Bob Wilson491328c2010-11-12 17:24:46 +00001451 }
Bob Wilsonc7df92d2010-11-12 17:24:43 +00001452 }
1453 const char *BaseName = 0;
Bob Wilson4cfaa5d2010-11-12 17:24:49 +00001454 unsigned BitSize = (T->getNumElements() *
Bob Wilson3a723022010-11-16 00:32:12 +00001455 getASTContext().getTypeSize(EltType));
Bob Wilsonc7df92d2010-11-12 17:24:43 +00001456 if (BitSize == 64)
1457 BaseName = "__simd64_";
Bob Wilson57147a82010-11-16 00:32:18 +00001458 else {
1459 assert(BitSize == 128 && "Neon vector type not 64 or 128 bits");
Bob Wilsonc7df92d2010-11-12 17:24:43 +00001460 BaseName = "__simd128_";
Bob Wilson57147a82010-11-16 00:32:18 +00001461 }
Bob Wilsonc7df92d2010-11-12 17:24:43 +00001462 Out << strlen(BaseName) + strlen(EltName);
1463 Out << BaseName << EltName;
Bob Wilsonc7df92d2010-11-12 17:24:43 +00001464}
1465
John McCallefe6aee2009-09-05 07:56:18 +00001466// GNU extension: vector types
Chris Lattner788b0fd2010-06-23 06:00:24 +00001467// <type> ::= <vector-type>
1468// <vector-type> ::= Dv <positive dimension number> _
1469// <extended element type>
1470// ::= Dv [<dimension expression>] _ <element type>
1471// <extended element type> ::= <element type>
1472// ::= p # AltiVec vector pixel
John McCallefe6aee2009-09-05 07:56:18 +00001473void CXXNameMangler::mangleType(const VectorType *T) {
Bob Wilson491328c2010-11-12 17:24:46 +00001474 if ((T->getVectorKind() == VectorType::NeonVector ||
Bob Wilson57147a82010-11-16 00:32:18 +00001475 T->getVectorKind() == VectorType::NeonPolyVector)) {
1476 mangleNeonVectorType(T);
Bob Wilsonc7df92d2010-11-12 17:24:43 +00001477 return;
Bob Wilson57147a82010-11-16 00:32:18 +00001478 }
Nick Lewycky0e5f0672010-03-26 07:18:04 +00001479 Out << "Dv" << T->getNumElements() << '_';
Bob Wilsone86d78c2010-11-10 21:56:12 +00001480 if (T->getVectorKind() == VectorType::AltiVecPixel)
Chris Lattner788b0fd2010-06-23 06:00:24 +00001481 Out << 'p';
Bob Wilsone86d78c2010-11-10 21:56:12 +00001482 else if (T->getVectorKind() == VectorType::AltiVecBool)
Chris Lattner788b0fd2010-06-23 06:00:24 +00001483 Out << 'b';
1484 else
1485 mangleType(T->getElementType());
John McCallefe6aee2009-09-05 07:56:18 +00001486}
1487void CXXNameMangler::mangleType(const ExtVectorType *T) {
1488 mangleType(static_cast<const VectorType*>(T));
1489}
1490void CXXNameMangler::mangleType(const DependentSizedExtVectorType *T) {
Nick Lewycky0e5f0672010-03-26 07:18:04 +00001491 Out << "Dv";
1492 mangleExpression(T->getSizeExpr());
1493 Out << '_';
John McCallefe6aee2009-09-05 07:56:18 +00001494 mangleType(T->getElementType());
1495}
1496
Douglas Gregor7536dd52010-12-20 02:24:11 +00001497void CXXNameMangler::mangleType(const PackExpansionType *T) {
Douglas Gregor4fc48662011-01-13 16:39:34 +00001498 // <type> ::= Dp <type> # pack expansion (C++0x)
Douglas Gregor255c2692011-01-13 17:44:36 +00001499 Out << "Dp";
Douglas Gregor7536dd52010-12-20 02:24:11 +00001500 mangleType(T->getPattern());
1501}
1502
Anders Carlssona40c5e42009-03-07 22:03:21 +00001503void CXXNameMangler::mangleType(const ObjCInterfaceType *T) {
1504 mangleSourceName(T->getDecl()->getIdentifier());
1505}
1506
John McCallc12c5bb2010-05-15 11:32:37 +00001507void CXXNameMangler::mangleType(const ObjCObjectType *T) {
John McCallc00c1f62010-05-15 17:06:29 +00001508 // We don't allow overloading by different protocol qualification,
1509 // so mangling them isn't necessary.
John McCallc12c5bb2010-05-15 11:32:37 +00001510 mangleType(T->getBaseType());
1511}
1512
John McCallefe6aee2009-09-05 07:56:18 +00001513void CXXNameMangler::mangleType(const BlockPointerType *T) {
Anders Carlssonf28c6872009-12-23 22:31:44 +00001514 Out << "U13block_pointer";
1515 mangleType(T->getPointeeType());
John McCallefe6aee2009-09-05 07:56:18 +00001516}
1517
John McCall31f17ec2010-04-27 00:57:59 +00001518void CXXNameMangler::mangleType(const InjectedClassNameType *T) {
1519 // Mangle injected class name types as if the user had written the
1520 // specialization out fully. It may not actually be possible to see
1521 // this mangling, though.
1522 mangleType(T->getInjectedSpecializationType());
1523}
1524
John McCallefe6aee2009-09-05 07:56:18 +00001525void CXXNameMangler::mangleType(const TemplateSpecializationType *T) {
Douglas Gregor1e9268e2010-04-28 05:58:56 +00001526 if (TemplateDecl *TD = T->getTemplateName().getAsTemplateDecl()) {
1527 mangleName(TD, T->getArgs(), T->getNumArgs());
1528 } else {
1529 if (mangleSubstitution(QualType(T, 0)))
1530 return;
Sean Huntc3021132010-05-05 15:23:54 +00001531
Douglas Gregor1e9268e2010-04-28 05:58:56 +00001532 mangleTemplatePrefix(T->getTemplateName());
Sean Huntc3021132010-05-05 15:23:54 +00001533
Douglas Gregor1e9268e2010-04-28 05:58:56 +00001534 // FIXME: GCC does not appear to mangle the template arguments when
1535 // the template in question is a dependent template name. Should we
1536 // emulate that badness?
1537 mangleTemplateArgs(T->getTemplateName(), T->getArgs(), T->getNumArgs());
1538 addSubstitution(QualType(T, 0));
1539 }
John McCallefe6aee2009-09-05 07:56:18 +00001540}
1541
Douglas Gregor4714c122010-03-31 17:34:00 +00001542void CXXNameMangler::mangleType(const DependentNameType *T) {
Anders Carlssonae352482009-09-26 02:26:02 +00001543 // Typename types are always nested
1544 Out << 'N';
John McCall33500952010-06-11 00:33:02 +00001545 mangleUnresolvedScope(T->getQualifier());
1546 mangleSourceName(T->getIdentifier());
1547 Out << 'E';
1548}
John McCall6ab30e02010-06-09 07:26:17 +00001549
John McCall33500952010-06-11 00:33:02 +00001550void CXXNameMangler::mangleType(const DependentTemplateSpecializationType *T) {
1551 // Dependently-scoped template types are always nested
1552 Out << 'N';
1553
1554 // TODO: avoid making this TemplateName.
1555 TemplateName Prefix =
1556 getASTContext().getDependentTemplateName(T->getQualifier(),
1557 T->getIdentifier());
1558 mangleTemplatePrefix(Prefix);
1559
1560 // FIXME: GCC does not appear to mangle the template arguments when
1561 // the template in question is a dependent template name. Should we
1562 // emulate that badness?
1563 mangleTemplateArgs(Prefix, T->getArgs(), T->getNumArgs());
Anders Carlssonae352482009-09-26 02:26:02 +00001564 Out << 'E';
John McCallefe6aee2009-09-05 07:56:18 +00001565}
1566
John McCallad5e7382010-03-01 23:49:17 +00001567void CXXNameMangler::mangleType(const TypeOfType *T) {
1568 // FIXME: this is pretty unsatisfactory, but there isn't an obvious
1569 // "extension with parameters" mangling.
1570 Out << "u6typeof";
1571}
1572
1573void CXXNameMangler::mangleType(const TypeOfExprType *T) {
1574 // FIXME: this is pretty unsatisfactory, but there isn't an obvious
1575 // "extension with parameters" mangling.
1576 Out << "u6typeof";
1577}
1578
1579void CXXNameMangler::mangleType(const DecltypeType *T) {
1580 Expr *E = T->getUnderlyingExpr();
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001581
John McCallad5e7382010-03-01 23:49:17 +00001582 // type ::= Dt <expression> E # decltype of an id-expression
1583 // # or class member access
1584 // ::= DT <expression> E # decltype of an expression
1585
1586 // This purports to be an exhaustive list of id-expressions and
1587 // class member accesses. Note that we do not ignore parentheses;
1588 // parentheses change the semantics of decltype for these
1589 // expressions (and cause the mangler to use the other form).
1590 if (isa<DeclRefExpr>(E) ||
1591 isa<MemberExpr>(E) ||
1592 isa<UnresolvedLookupExpr>(E) ||
1593 isa<DependentScopeDeclRefExpr>(E) ||
1594 isa<CXXDependentScopeMemberExpr>(E) ||
1595 isa<UnresolvedMemberExpr>(E))
1596 Out << "Dt";
1597 else
1598 Out << "DT";
1599 mangleExpression(E);
1600 Out << 'E';
1601}
1602
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001603void CXXNameMangler::mangleIntegerLiteral(QualType T,
Anders Carlssone170ba72009-12-14 01:45:37 +00001604 const llvm::APSInt &Value) {
1605 // <expr-primary> ::= L <type> <value number> E # integer literal
1606 Out << 'L';
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001607
Anders Carlssone170ba72009-12-14 01:45:37 +00001608 mangleType(T);
1609 if (T->isBooleanType()) {
1610 // Boolean values are encoded as 0/1.
1611 Out << (Value.getBoolValue() ? '1' : '0');
1612 } else {
John McCall0512e482010-07-14 04:20:34 +00001613 mangleNumber(Value);
Anders Carlssone170ba72009-12-14 01:45:37 +00001614 }
1615 Out << 'E';
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001616
Anders Carlssone170ba72009-12-14 01:45:37 +00001617}
1618
John McCall2f27bf82010-02-04 02:56:29 +00001619/// Mangles a member expression. Implicit accesses are not handled,
1620/// but that should be okay, because you shouldn't be able to
1621/// make an implicit access in a function template declaration.
John McCall2f27bf82010-02-04 02:56:29 +00001622void CXXNameMangler::mangleMemberExpr(const Expr *Base,
1623 bool IsArrow,
1624 NestedNameSpecifier *Qualifier,
1625 DeclarationName Member,
1626 unsigned Arity) {
John McCalle1e342f2010-03-01 19:12:25 +00001627 // gcc-4.4 uses 'dt' for dot expressions, which is reasonable.
1628 // OTOH, gcc also mangles the name as an expression.
1629 Out << (IsArrow ? "pt" : "dt");
John McCall2f27bf82010-02-04 02:56:29 +00001630 mangleExpression(Base);
1631 mangleUnresolvedName(Qualifier, Member, Arity);
1632}
1633
John McCall5e1e89b2010-08-18 19:18:59 +00001634void CXXNameMangler::mangleExpression(const Expr *E, unsigned Arity) {
Anders Carlssond553f8c2009-09-21 01:21:10 +00001635 // <expression> ::= <unary operator-name> <expression>
John McCall09cc1412010-02-03 00:55:45 +00001636 // ::= <binary operator-name> <expression> <expression>
1637 // ::= <trinary operator-name> <expression> <expression> <expression>
Eli Friedmana7e68452010-08-22 01:00:03 +00001638 // ::= cl <expression>* E # call
Anders Carlssond553f8c2009-09-21 01:21:10 +00001639 // ::= cv <type> expression # conversion with one argument
1640 // ::= cv <type> _ <expression>* E # conversion with a different number of arguments
Eli Friedmana7e68452010-08-22 01:00:03 +00001641 // ::= st <type> # sizeof (a type)
Anders Carlssond553f8c2009-09-21 01:21:10 +00001642 // ::= at <type> # alignof (a type)
1643 // ::= <template-param>
1644 // ::= <function-param>
1645 // ::= sr <type> <unqualified-name> # dependent name
1646 // ::= sr <type> <unqualified-name> <template-args> # dependent template-id
1647 // ::= sZ <template-param> # size of a parameter pack
Douglas Gregor4fc48662011-01-13 16:39:34 +00001648 // ::= sZ <function-param> # size of a function parameter pack
John McCall09cc1412010-02-03 00:55:45 +00001649 // ::= <expr-primary>
John McCall1dd73832010-02-04 01:42:13 +00001650 // <expr-primary> ::= L <type> <value number> E # integer literal
1651 // ::= L <type <value float> E # floating literal
1652 // ::= L <mangled-name> E # external name
Anders Carlssond553f8c2009-09-21 01:21:10 +00001653 switch (E->getStmtClass()) {
John McCall6ae1f352010-04-09 22:26:14 +00001654 case Expr::NoStmtClass:
1655#define EXPR(Type, Base)
1656#define STMT(Type, Base) \
1657 case Expr::Type##Class:
Sean Hunt4bfe1962010-05-05 15:24:00 +00001658#include "clang/AST/StmtNodes.inc"
John McCall0512e482010-07-14 04:20:34 +00001659 // fallthrough
1660
1661 // These all can only appear in local or variable-initialization
1662 // contexts and so should never appear in a mangling.
1663 case Expr::AddrLabelExprClass:
1664 case Expr::BlockDeclRefExprClass:
1665 case Expr::CXXThisExprClass:
1666 case Expr::DesignatedInitExprClass:
1667 case Expr::ImplicitValueInitExprClass:
1668 case Expr::InitListExprClass:
1669 case Expr::ParenListExprClass:
1670 case Expr::CXXScalarValueInitExprClass:
John McCall09cc1412010-02-03 00:55:45 +00001671 llvm_unreachable("unexpected statement kind");
1672 break;
1673
John McCall0512e482010-07-14 04:20:34 +00001674 // FIXME: invent manglings for all these.
1675 case Expr::BlockExprClass:
1676 case Expr::CXXPseudoDestructorExprClass:
1677 case Expr::ChooseExprClass:
1678 case Expr::CompoundLiteralExprClass:
1679 case Expr::ExtVectorElementExprClass:
1680 case Expr::ObjCEncodeExprClass:
John McCall0512e482010-07-14 04:20:34 +00001681 case Expr::ObjCIsaExprClass:
1682 case Expr::ObjCIvarRefExprClass:
1683 case Expr::ObjCMessageExprClass:
1684 case Expr::ObjCPropertyRefExprClass:
1685 case Expr::ObjCProtocolExprClass:
1686 case Expr::ObjCSelectorExprClass:
1687 case Expr::ObjCStringLiteralClass:
John McCall0512e482010-07-14 04:20:34 +00001688 case Expr::OffsetOfExprClass:
1689 case Expr::PredefinedExprClass:
1690 case Expr::ShuffleVectorExprClass:
1691 case Expr::StmtExprClass:
John McCall0512e482010-07-14 04:20:34 +00001692 case Expr::UnaryTypeTraitExprClass:
Francois Pichet6ad6f282010-12-07 00:08:36 +00001693 case Expr::BinaryTypeTraitExprClass:
Francois Pichet9be88402010-09-08 23:47:05 +00001694 case Expr::VAArgExprClass:
Sebastian Redl2e156222010-09-10 20:55:43 +00001695 case Expr::CXXUuidofExprClass:
1696 case Expr::CXXNoexceptExprClass: {
John McCall6ae1f352010-04-09 22:26:14 +00001697 // As bad as this diagnostic is, it's better than crashing.
1698 Diagnostic &Diags = Context.getDiags();
1699 unsigned DiagID = Diags.getCustomDiagID(Diagnostic::Error,
1700 "cannot yet mangle expression type %0");
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +00001701 Diags.Report(E->getExprLoc(), DiagID)
John McCall739bf092010-04-10 09:39:25 +00001702 << E->getStmtClassName() << E->getSourceRange();
John McCall6ae1f352010-04-09 22:26:14 +00001703 break;
1704 }
1705
John McCall7cd7d1a2010-11-15 23:31:06 +00001706 case Expr::OpaqueValueExprClass:
1707 llvm_unreachable("cannot mangle opaque value; mangling wrong thing?");
1708
John McCall0512e482010-07-14 04:20:34 +00001709 case Expr::CXXDefaultArgExprClass:
John McCall5e1e89b2010-08-18 19:18:59 +00001710 mangleExpression(cast<CXXDefaultArgExpr>(E)->getExpr(), Arity);
John McCall0512e482010-07-14 04:20:34 +00001711 break;
1712
1713 case Expr::CXXMemberCallExprClass: // fallthrough
John McCall1dd73832010-02-04 01:42:13 +00001714 case Expr::CallExprClass: {
1715 const CallExpr *CE = cast<CallExpr>(E);
1716 Out << "cl";
John McCall5e1e89b2010-08-18 19:18:59 +00001717 mangleExpression(CE->getCallee(), CE->getNumArgs());
John McCall1dd73832010-02-04 01:42:13 +00001718 for (unsigned I = 0, N = CE->getNumArgs(); I != N; ++I)
1719 mangleExpression(CE->getArg(I));
Benjamin Kramer35f59b62010-04-10 16:03:31 +00001720 Out << 'E';
John McCall09cc1412010-02-03 00:55:45 +00001721 break;
John McCall1dd73832010-02-04 01:42:13 +00001722 }
John McCall09cc1412010-02-03 00:55:45 +00001723
John McCall0512e482010-07-14 04:20:34 +00001724 case Expr::CXXNewExprClass: {
1725 // Proposal from David Vandervoorde, 2010.06.30
1726 const CXXNewExpr *New = cast<CXXNewExpr>(E);
1727 if (New->isGlobalNew()) Out << "gs";
1728 Out << (New->isArray() ? "na" : "nw");
1729 for (CXXNewExpr::const_arg_iterator I = New->placement_arg_begin(),
1730 E = New->placement_arg_end(); I != E; ++I)
1731 mangleExpression(*I);
1732 Out << '_';
1733 mangleType(New->getAllocatedType());
1734 if (New->hasInitializer()) {
1735 Out << "pi";
1736 for (CXXNewExpr::const_arg_iterator I = New->constructor_arg_begin(),
1737 E = New->constructor_arg_end(); I != E; ++I)
1738 mangleExpression(*I);
1739 }
1740 Out << 'E';
1741 break;
1742 }
1743
John McCall2f27bf82010-02-04 02:56:29 +00001744 case Expr::MemberExprClass: {
1745 const MemberExpr *ME = cast<MemberExpr>(E);
1746 mangleMemberExpr(ME->getBase(), ME->isArrow(),
1747 ME->getQualifier(), ME->getMemberDecl()->getDeclName(),
John McCall5e1e89b2010-08-18 19:18:59 +00001748 Arity);
John McCall2f27bf82010-02-04 02:56:29 +00001749 break;
1750 }
1751
1752 case Expr::UnresolvedMemberExprClass: {
1753 const UnresolvedMemberExpr *ME = cast<UnresolvedMemberExpr>(E);
1754 mangleMemberExpr(ME->getBase(), ME->isArrow(),
1755 ME->getQualifier(), ME->getMemberName(),
John McCall5e1e89b2010-08-18 19:18:59 +00001756 Arity);
John McCall6dbce192010-08-20 00:17:19 +00001757 if (ME->hasExplicitTemplateArgs())
1758 mangleTemplateArgs(ME->getExplicitTemplateArgs());
John McCall2f27bf82010-02-04 02:56:29 +00001759 break;
1760 }
1761
1762 case Expr::CXXDependentScopeMemberExprClass: {
1763 const CXXDependentScopeMemberExpr *ME
1764 = cast<CXXDependentScopeMemberExpr>(E);
1765 mangleMemberExpr(ME->getBase(), ME->isArrow(),
1766 ME->getQualifier(), ME->getMember(),
John McCall5e1e89b2010-08-18 19:18:59 +00001767 Arity);
John McCall6dbce192010-08-20 00:17:19 +00001768 if (ME->hasExplicitTemplateArgs())
1769 mangleTemplateArgs(ME->getExplicitTemplateArgs());
John McCall2f27bf82010-02-04 02:56:29 +00001770 break;
1771 }
1772
John McCall1dd73832010-02-04 01:42:13 +00001773 case Expr::UnresolvedLookupExprClass: {
John McCalla3218e72010-02-04 01:48:38 +00001774 // The ABI doesn't cover how to mangle overload sets, so we mangle
1775 // using something as close as possible to the original lookup
1776 // expression.
John McCall1dd73832010-02-04 01:42:13 +00001777 const UnresolvedLookupExpr *ULE = cast<UnresolvedLookupExpr>(E);
John McCall5e1e89b2010-08-18 19:18:59 +00001778 mangleUnresolvedName(ULE->getQualifier(), ULE->getName(), Arity);
John McCall6dbce192010-08-20 00:17:19 +00001779 if (ULE->hasExplicitTemplateArgs())
1780 mangleTemplateArgs(ULE->getExplicitTemplateArgs());
John McCall1dd73832010-02-04 01:42:13 +00001781 break;
1782 }
1783
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001784 case Expr::CXXUnresolvedConstructExprClass: {
John McCall1dd73832010-02-04 01:42:13 +00001785 const CXXUnresolvedConstructExpr *CE = cast<CXXUnresolvedConstructExpr>(E);
1786 unsigned N = CE->arg_size();
1787
1788 Out << "cv";
1789 mangleType(CE->getType());
Benjamin Kramer35f59b62010-04-10 16:03:31 +00001790 if (N != 1) Out << '_';
John McCall1dd73832010-02-04 01:42:13 +00001791 for (unsigned I = 0; I != N; ++I) mangleExpression(CE->getArg(I));
Benjamin Kramer35f59b62010-04-10 16:03:31 +00001792 if (N != 1) Out << 'E';
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001793 break;
John McCall1dd73832010-02-04 01:42:13 +00001794 }
John McCall09cc1412010-02-03 00:55:45 +00001795
John McCall1dd73832010-02-04 01:42:13 +00001796 case Expr::CXXTemporaryObjectExprClass:
1797 case Expr::CXXConstructExprClass: {
1798 const CXXConstructExpr *CE = cast<CXXConstructExpr>(E);
1799 unsigned N = CE->getNumArgs();
1800
1801 Out << "cv";
1802 mangleType(CE->getType());
Benjamin Kramer35f59b62010-04-10 16:03:31 +00001803 if (N != 1) Out << '_';
John McCall1dd73832010-02-04 01:42:13 +00001804 for (unsigned I = 0; I != N; ++I) mangleExpression(CE->getArg(I));
Benjamin Kramer35f59b62010-04-10 16:03:31 +00001805 if (N != 1) Out << 'E';
John McCall09cc1412010-02-03 00:55:45 +00001806 break;
John McCall1dd73832010-02-04 01:42:13 +00001807 }
1808
1809 case Expr::SizeOfAlignOfExprClass: {
1810 const SizeOfAlignOfExpr *SAE = cast<SizeOfAlignOfExpr>(E);
Benjamin Kramer35f59b62010-04-10 16:03:31 +00001811 if (SAE->isSizeOf()) Out << 's';
1812 else Out << 'a';
John McCall1dd73832010-02-04 01:42:13 +00001813 if (SAE->isArgumentType()) {
Benjamin Kramer35f59b62010-04-10 16:03:31 +00001814 Out << 't';
John McCall1dd73832010-02-04 01:42:13 +00001815 mangleType(SAE->getArgumentType());
1816 } else {
Benjamin Kramer35f59b62010-04-10 16:03:31 +00001817 Out << 'z';
John McCall1dd73832010-02-04 01:42:13 +00001818 mangleExpression(SAE->getArgumentExpr());
1819 }
1820 break;
1821 }
Anders Carlssona7694082009-11-06 02:50:19 +00001822
John McCall0512e482010-07-14 04:20:34 +00001823 case Expr::CXXThrowExprClass: {
1824 const CXXThrowExpr *TE = cast<CXXThrowExpr>(E);
1825
1826 // Proposal from David Vandervoorde, 2010.06.30
1827 if (TE->getSubExpr()) {
1828 Out << "tw";
1829 mangleExpression(TE->getSubExpr());
1830 } else {
1831 Out << "tr";
1832 }
1833 break;
1834 }
1835
1836 case Expr::CXXTypeidExprClass: {
1837 const CXXTypeidExpr *TIE = cast<CXXTypeidExpr>(E);
1838
1839 // Proposal from David Vandervoorde, 2010.06.30
1840 if (TIE->isTypeOperand()) {
1841 Out << "ti";
1842 mangleType(TIE->getTypeOperand());
1843 } else {
1844 Out << "te";
1845 mangleExpression(TIE->getExprOperand());
1846 }
1847 break;
1848 }
1849
1850 case Expr::CXXDeleteExprClass: {
1851 const CXXDeleteExpr *DE = cast<CXXDeleteExpr>(E);
1852
1853 // Proposal from David Vandervoorde, 2010.06.30
1854 if (DE->isGlobalDelete()) Out << "gs";
1855 Out << (DE->isArrayForm() ? "da" : "dl");
1856 mangleExpression(DE->getArgument());
1857 break;
1858 }
1859
Anders Carlssone170ba72009-12-14 01:45:37 +00001860 case Expr::UnaryOperatorClass: {
1861 const UnaryOperator *UO = cast<UnaryOperator>(E);
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001862 mangleOperatorName(UnaryOperator::getOverloadedOperator(UO->getOpcode()),
Anders Carlssone170ba72009-12-14 01:45:37 +00001863 /*Arity=*/1);
1864 mangleExpression(UO->getSubExpr());
1865 break;
1866 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001867
John McCall0512e482010-07-14 04:20:34 +00001868 case Expr::ArraySubscriptExprClass: {
1869 const ArraySubscriptExpr *AE = cast<ArraySubscriptExpr>(E);
1870
1871 // Array subscript is treated as a syntactically wierd form of
1872 // binary operator.
1873 Out << "ix";
1874 mangleExpression(AE->getLHS());
1875 mangleExpression(AE->getRHS());
1876 break;
1877 }
1878
1879 case Expr::CompoundAssignOperatorClass: // fallthrough
Anders Carlssone170ba72009-12-14 01:45:37 +00001880 case Expr::BinaryOperatorClass: {
1881 const BinaryOperator *BO = cast<BinaryOperator>(E);
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001882 mangleOperatorName(BinaryOperator::getOverloadedOperator(BO->getOpcode()),
Anders Carlssone170ba72009-12-14 01:45:37 +00001883 /*Arity=*/2);
1884 mangleExpression(BO->getLHS());
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001885 mangleExpression(BO->getRHS());
Anders Carlssone170ba72009-12-14 01:45:37 +00001886 break;
John McCall2f27bf82010-02-04 02:56:29 +00001887 }
Anders Carlssone170ba72009-12-14 01:45:37 +00001888
1889 case Expr::ConditionalOperatorClass: {
1890 const ConditionalOperator *CO = cast<ConditionalOperator>(E);
1891 mangleOperatorName(OO_Conditional, /*Arity=*/3);
1892 mangleExpression(CO->getCond());
John McCall5e1e89b2010-08-18 19:18:59 +00001893 mangleExpression(CO->getLHS(), Arity);
1894 mangleExpression(CO->getRHS(), Arity);
Anders Carlssone170ba72009-12-14 01:45:37 +00001895 break;
1896 }
1897
Douglas Gregor46287c72010-01-29 16:37:09 +00001898 case Expr::ImplicitCastExprClass: {
John McCall5e1e89b2010-08-18 19:18:59 +00001899 mangleExpression(cast<ImplicitCastExpr>(E)->getSubExpr(), Arity);
Douglas Gregor46287c72010-01-29 16:37:09 +00001900 break;
1901 }
1902
1903 case Expr::CStyleCastExprClass:
1904 case Expr::CXXStaticCastExprClass:
1905 case Expr::CXXDynamicCastExprClass:
1906 case Expr::CXXReinterpretCastExprClass:
1907 case Expr::CXXConstCastExprClass:
1908 case Expr::CXXFunctionalCastExprClass: {
1909 const ExplicitCastExpr *ECE = cast<ExplicitCastExpr>(E);
1910 Out << "cv";
1911 mangleType(ECE->getType());
1912 mangleExpression(ECE->getSubExpr());
1913 break;
1914 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001915
Anders Carlsson58040a52009-12-16 05:48:46 +00001916 case Expr::CXXOperatorCallExprClass: {
1917 const CXXOperatorCallExpr *CE = cast<CXXOperatorCallExpr>(E);
1918 unsigned NumArgs = CE->getNumArgs();
1919 mangleOperatorName(CE->getOperator(), /*Arity=*/NumArgs);
1920 // Mangle the arguments.
1921 for (unsigned i = 0; i != NumArgs; ++i)
1922 mangleExpression(CE->getArg(i));
1923 break;
1924 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001925
Anders Carlssona7694082009-11-06 02:50:19 +00001926 case Expr::ParenExprClass:
John McCall5e1e89b2010-08-18 19:18:59 +00001927 mangleExpression(cast<ParenExpr>(E)->getSubExpr(), Arity);
Anders Carlssona7694082009-11-06 02:50:19 +00001928 break;
1929
Anders Carlssond553f8c2009-09-21 01:21:10 +00001930 case Expr::DeclRefExprClass: {
Douglas Gregor5ed1bc32010-02-28 21:40:32 +00001931 const NamedDecl *D = cast<DeclRefExpr>(E)->getDecl();
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00001932
Anders Carlssond553f8c2009-09-21 01:21:10 +00001933 switch (D->getKind()) {
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001934 default:
Douglas Gregor5ed1bc32010-02-28 21:40:32 +00001935 // <expr-primary> ::= L <mangled-name> E # external name
1936 Out << 'L';
1937 mangle(D, "_Z");
1938 Out << 'E';
1939 break;
1940
John McCall3dc7e7b2010-07-24 01:17:35 +00001941 case Decl::EnumConstant: {
1942 const EnumConstantDecl *ED = cast<EnumConstantDecl>(D);
1943 mangleIntegerLiteral(ED->getType(), ED->getInitVal());
1944 break;
1945 }
1946
Anders Carlssond553f8c2009-09-21 01:21:10 +00001947 case Decl::NonTypeTemplateParm: {
1948 const NonTypeTemplateParmDecl *PD = cast<NonTypeTemplateParmDecl>(D);
Anders Carlsson0ccdf8d2009-09-27 00:38:53 +00001949 mangleTemplateParameter(PD->getIndex());
Anders Carlssond553f8c2009-09-21 01:21:10 +00001950 break;
1951 }
1952
1953 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00001954
Anders Carlsson50755b02009-09-27 20:11:34 +00001955 break;
Anders Carlssond553f8c2009-09-21 01:21:10 +00001956 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00001957
Douglas Gregorc7793c72011-01-15 01:15:58 +00001958 case Expr::SubstNonTypeTemplateParmPackExprClass:
1959 mangleTemplateParameter(
1960 cast<SubstNonTypeTemplateParmPackExpr>(E)->getParameterPack()->getIndex());
1961 break;
1962
John McCall865d4472009-11-19 22:55:06 +00001963 case Expr::DependentScopeDeclRefExprClass: {
1964 const DependentScopeDeclRefExpr *DRE = cast<DependentScopeDeclRefExpr>(E);
Douglas Gregor4b2ccfc2010-02-28 22:05:49 +00001965 NestedNameSpecifier *NNS = DRE->getQualifier();
1966 const Type *QTy = NNS->getAsType();
1967
1968 // When we're dealing with a nested-name-specifier that has just a
1969 // dependent identifier in it, mangle that as a typename. FIXME:
1970 // It isn't clear that we ever actually want to have such a
1971 // nested-name-specifier; why not just represent it as a typename type?
1972 if (!QTy && NNS->getAsIdentifier() && NNS->getPrefix()) {
Douglas Gregor4a2023f2010-03-31 20:19:30 +00001973 QTy = getASTContext().getDependentNameType(ETK_Typename,
1974 NNS->getPrefix(),
1975 NNS->getAsIdentifier())
Douglas Gregor4b2ccfc2010-02-28 22:05:49 +00001976 .getTypePtr();
1977 }
Anders Carlsson50755b02009-09-27 20:11:34 +00001978 assert(QTy && "Qualifier was not type!");
1979
John McCall6dbce192010-08-20 00:17:19 +00001980 // ::= sr <type> <unqualified-name> # dependent name
1981 // ::= sr <type> <unqualified-name> <template-args> # dependent template-id
Anders Carlsson50755b02009-09-27 20:11:34 +00001982 Out << "sr";
1983 mangleType(QualType(QTy, 0));
John McCall5e1e89b2010-08-18 19:18:59 +00001984 mangleUnqualifiedName(0, DRE->getDeclName(), Arity);
John McCall6dbce192010-08-20 00:17:19 +00001985 if (DRE->hasExplicitTemplateArgs())
1986 mangleTemplateArgs(DRE->getExplicitTemplateArgs());
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00001987
Anders Carlsson50755b02009-09-27 20:11:34 +00001988 break;
1989 }
1990
John McCalld9307602010-04-09 22:54:09 +00001991 case Expr::CXXBindTemporaryExprClass:
1992 mangleExpression(cast<CXXBindTemporaryExpr>(E)->getSubExpr());
1993 break;
1994
John McCall4765fa02010-12-06 08:20:24 +00001995 case Expr::ExprWithCleanupsClass:
1996 mangleExpression(cast<ExprWithCleanups>(E)->getSubExpr(), Arity);
John McCalld9307602010-04-09 22:54:09 +00001997 break;
1998
John McCall1dd73832010-02-04 01:42:13 +00001999 case Expr::FloatingLiteralClass: {
2000 const FloatingLiteral *FL = cast<FloatingLiteral>(E);
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002001 Out << 'L';
John McCall1dd73832010-02-04 01:42:13 +00002002 mangleType(FL->getType());
John McCall0512e482010-07-14 04:20:34 +00002003 mangleFloat(FL->getValue());
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002004 Out << 'E';
John McCall1dd73832010-02-04 01:42:13 +00002005 break;
2006 }
2007
John McCallde810632010-04-09 21:48:08 +00002008 case Expr::CharacterLiteralClass:
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002009 Out << 'L';
John McCallde810632010-04-09 21:48:08 +00002010 mangleType(E->getType());
2011 Out << cast<CharacterLiteral>(E)->getValue();
2012 Out << 'E';
2013 break;
2014
2015 case Expr::CXXBoolLiteralExprClass:
2016 Out << "Lb";
2017 Out << (cast<CXXBoolLiteralExpr>(E)->getValue() ? '1' : '0');
2018 Out << 'E';
2019 break;
2020
John McCall0512e482010-07-14 04:20:34 +00002021 case Expr::IntegerLiteralClass: {
2022 llvm::APSInt Value(cast<IntegerLiteral>(E)->getValue());
2023 if (E->getType()->isSignedIntegerType())
2024 Value.setIsSigned(true);
2025 mangleIntegerLiteral(E->getType(), Value);
Anders Carlssone170ba72009-12-14 01:45:37 +00002026 break;
John McCall0512e482010-07-14 04:20:34 +00002027 }
2028
2029 case Expr::ImaginaryLiteralClass: {
2030 const ImaginaryLiteral *IE = cast<ImaginaryLiteral>(E);
2031 // Mangle as if a complex literal.
Nick Lewycky271b6652010-09-05 03:40:33 +00002032 // Proposal from David Vandevoorde, 2010.06.30.
John McCall0512e482010-07-14 04:20:34 +00002033 Out << 'L';
2034 mangleType(E->getType());
2035 if (const FloatingLiteral *Imag =
2036 dyn_cast<FloatingLiteral>(IE->getSubExpr())) {
2037 // Mangle a floating-point zero of the appropriate type.
2038 mangleFloat(llvm::APFloat(Imag->getValue().getSemantics()));
2039 Out << '_';
2040 mangleFloat(Imag->getValue());
2041 } else {
Nick Lewycky271b6652010-09-05 03:40:33 +00002042 Out << "0_";
John McCall0512e482010-07-14 04:20:34 +00002043 llvm::APSInt Value(cast<IntegerLiteral>(IE->getSubExpr())->getValue());
2044 if (IE->getSubExpr()->getType()->isSignedIntegerType())
2045 Value.setIsSigned(true);
2046 mangleNumber(Value);
2047 }
2048 Out << 'E';
2049 break;
2050 }
2051
2052 case Expr::StringLiteralClass: {
John McCall1658c392010-07-15 21:53:03 +00002053 // Revised proposal from David Vandervoorde, 2010.07.15.
John McCall0512e482010-07-14 04:20:34 +00002054 Out << 'L';
John McCall1658c392010-07-15 21:53:03 +00002055 assert(isa<ConstantArrayType>(E->getType()));
2056 mangleType(E->getType());
John McCall0512e482010-07-14 04:20:34 +00002057 Out << 'E';
2058 break;
2059 }
2060
2061 case Expr::GNUNullExprClass:
2062 // FIXME: should this really be mangled the same as nullptr?
2063 // fallthrough
2064
2065 case Expr::CXXNullPtrLiteralExprClass: {
2066 // Proposal from David Vandervoorde, 2010.06.30, as
2067 // modified by ABI list discussion.
2068 Out << "LDnE";
2069 break;
2070 }
Douglas Gregorbe230c32011-01-03 17:17:50 +00002071
2072 case Expr::PackExpansionExprClass:
2073 Out << "sp";
2074 mangleExpression(cast<PackExpansionExpr>(E)->getPattern());
2075 break;
Douglas Gregor2e774c42011-01-04 18:56:13 +00002076
2077 case Expr::SizeOfPackExprClass: {
Douglas Gregor2e774c42011-01-04 18:56:13 +00002078 Out << "sZ";
2079 const NamedDecl *Pack = cast<SizeOfPackExpr>(E)->getPack();
2080 if (const TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(Pack))
2081 mangleTemplateParameter(TTP->getIndex());
2082 else if (const NonTypeTemplateParmDecl *NTTP
2083 = dyn_cast<NonTypeTemplateParmDecl>(Pack))
2084 mangleTemplateParameter(NTTP->getIndex());
2085 else if (const TemplateTemplateParmDecl *TempTP
2086 = dyn_cast<TemplateTemplateParmDecl>(Pack))
2087 mangleTemplateParameter(TempTP->getIndex());
2088 else {
Douglas Gregor4fc48662011-01-13 16:39:34 +00002089 // Note: proposed by Mike Herrick on 11/30/10
2090 // <expression> ::= sZ <function-param> # size of function parameter pack
Douglas Gregor2e774c42011-01-04 18:56:13 +00002091 Diagnostic &Diags = Context.getDiags();
2092 unsigned DiagID = Diags.getCustomDiagID(Diagnostic::Error,
2093 "cannot mangle sizeof...(function parameter pack)");
2094 Diags.Report(DiagID);
2095 return;
2096 }
2097 }
Anders Carlssond553f8c2009-09-21 01:21:10 +00002098 }
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00002099}
2100
Anders Carlsson3ac86b52009-04-15 05:36:58 +00002101void CXXNameMangler::mangleCXXCtorType(CXXCtorType T) {
2102 // <ctor-dtor-name> ::= C1 # complete object constructor
2103 // ::= C2 # base object constructor
2104 // ::= C3 # complete object allocating constructor
2105 //
2106 switch (T) {
2107 case Ctor_Complete:
2108 Out << "C1";
2109 break;
2110 case Ctor_Base:
2111 Out << "C2";
2112 break;
2113 case Ctor_CompleteAllocating:
2114 Out << "C3";
2115 break;
2116 }
2117}
2118
Anders Carlsson27ae5362009-04-17 01:58:57 +00002119void CXXNameMangler::mangleCXXDtorType(CXXDtorType T) {
2120 // <ctor-dtor-name> ::= D0 # deleting destructor
2121 // ::= D1 # complete object destructor
2122 // ::= D2 # base object destructor
2123 //
2124 switch (T) {
2125 case Dtor_Deleting:
2126 Out << "D0";
2127 break;
2128 case Dtor_Complete:
2129 Out << "D1";
2130 break;
2131 case Dtor_Base:
2132 Out << "D2";
2133 break;
2134 }
2135}
2136
John McCall6dbce192010-08-20 00:17:19 +00002137void CXXNameMangler::mangleTemplateArgs(
2138 const ExplicitTemplateArgumentList &TemplateArgs) {
2139 // <template-args> ::= I <template-arg>+ E
2140 Out << 'I';
2141 for (unsigned I = 0, E = TemplateArgs.NumTemplateArgs; I != E; ++I)
2142 mangleTemplateArg(0, TemplateArgs.getTemplateArgs()[I].getArgument());
2143 Out << 'E';
2144}
2145
Douglas Gregor20f0cc72010-04-23 03:10:43 +00002146void CXXNameMangler::mangleTemplateArgs(TemplateName Template,
2147 const TemplateArgument *TemplateArgs,
2148 unsigned NumTemplateArgs) {
2149 if (TemplateDecl *TD = Template.getAsTemplateDecl())
2150 return mangleTemplateArgs(*TD->getTemplateParameters(), TemplateArgs,
2151 NumTemplateArgs);
Sean Huntc3021132010-05-05 15:23:54 +00002152
Douglas Gregor20f0cc72010-04-23 03:10:43 +00002153 // <template-args> ::= I <template-arg>+ E
2154 Out << 'I';
2155 for (unsigned i = 0; i != NumTemplateArgs; ++i)
2156 mangleTemplateArg(0, TemplateArgs[i]);
2157 Out << 'E';
2158}
2159
Rafael Espindolad9800722010-03-11 14:07:00 +00002160void CXXNameMangler::mangleTemplateArgs(const TemplateParameterList &PL,
2161 const TemplateArgumentList &AL) {
Anders Carlsson7a0ba872009-05-15 16:09:15 +00002162 // <template-args> ::= I <template-arg>+ E
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002163 Out << 'I';
Rafael Espindolad9800722010-03-11 14:07:00 +00002164 for (unsigned i = 0, e = AL.size(); i != e; ++i)
2165 mangleTemplateArg(PL.getParam(i), AL[i]);
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002166 Out << 'E';
Anders Carlsson7a0ba872009-05-15 16:09:15 +00002167}
2168
Rafael Espindolad9800722010-03-11 14:07:00 +00002169void CXXNameMangler::mangleTemplateArgs(const TemplateParameterList &PL,
2170 const TemplateArgument *TemplateArgs,
Anders Carlsson7624f212009-09-18 02:42:01 +00002171 unsigned NumTemplateArgs) {
2172 // <template-args> ::= I <template-arg>+ E
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002173 Out << 'I';
Daniel Dunbar7e0c1952009-11-21 09:17:15 +00002174 for (unsigned i = 0; i != NumTemplateArgs; ++i)
Rafael Espindolad9800722010-03-11 14:07:00 +00002175 mangleTemplateArg(PL.getParam(i), TemplateArgs[i]);
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002176 Out << 'E';
Anders Carlsson7624f212009-09-18 02:42:01 +00002177}
2178
Rafael Espindolad9800722010-03-11 14:07:00 +00002179void CXXNameMangler::mangleTemplateArg(const NamedDecl *P,
2180 const TemplateArgument &A) {
Mike Stump1eb44332009-09-09 15:08:12 +00002181 // <template-arg> ::= <type> # type or template
Anders Carlsson7a0ba872009-05-15 16:09:15 +00002182 // ::= X <expression> E # expression
2183 // ::= <expr-primary> # simple expressions
Douglas Gregor4fc48662011-01-13 16:39:34 +00002184 // ::= J <template-arg>* E # argument pack
Anders Carlsson7a0ba872009-05-15 16:09:15 +00002185 // ::= sp <expression> # pack expansion of (C++0x)
2186 switch (A.getKind()) {
Douglas Gregorf90b27a2011-01-03 22:36:02 +00002187 case TemplateArgument::Null:
2188 llvm_unreachable("Cannot mangle NULL template argument");
2189
Anders Carlsson7a0ba872009-05-15 16:09:15 +00002190 case TemplateArgument::Type:
2191 mangleType(A.getAsType());
2192 break;
Anders Carlsson9e85c742009-12-23 19:30:55 +00002193 case TemplateArgument::Template:
John McCallb6f532e2010-07-14 06:43:17 +00002194 // This is mangled as <type>.
2195 mangleType(A.getAsTemplate());
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002196 break;
Douglas Gregora7fc9012011-01-05 18:58:31 +00002197 case TemplateArgument::TemplateExpansion:
Douglas Gregor4fc48662011-01-13 16:39:34 +00002198 // <type> ::= Dp <type> # pack expansion (C++0x)
Douglas Gregora7fc9012011-01-05 18:58:31 +00002199 Out << "Dp";
2200 mangleType(A.getAsTemplateOrTemplatePattern());
2201 break;
Anders Carlssond553f8c2009-09-21 01:21:10 +00002202 case TemplateArgument::Expression:
2203 Out << 'X';
2204 mangleExpression(A.getAsExpr());
2205 Out << 'E';
2206 break;
Anders Carlssone170ba72009-12-14 01:45:37 +00002207 case TemplateArgument::Integral:
2208 mangleIntegerLiteral(A.getIntegralType(), *A.getAsIntegral());
Anders Carlsson7a0ba872009-05-15 16:09:15 +00002209 break;
Daniel Dunbar7e0c1952009-11-21 09:17:15 +00002210 case TemplateArgument::Declaration: {
Douglas Gregor20f0cc72010-04-23 03:10:43 +00002211 assert(P && "Missing template parameter for declaration argument");
Daniel Dunbar7e0c1952009-11-21 09:17:15 +00002212 // <expr-primary> ::= L <mangled-name> E # external name
2213
Rafael Espindolad9800722010-03-11 14:07:00 +00002214 // Clang produces AST's where pointer-to-member-function expressions
Daniel Dunbar7e0c1952009-11-21 09:17:15 +00002215 // and pointer-to-function expressions are represented as a declaration not
Rafael Espindolad9800722010-03-11 14:07:00 +00002216 // an expression. We compensate for it here to produce the correct mangling.
2217 NamedDecl *D = cast<NamedDecl>(A.getAsDecl());
2218 const NonTypeTemplateParmDecl *Parameter = cast<NonTypeTemplateParmDecl>(P);
2219 bool compensateMangling = D->isCXXClassMember() &&
2220 !Parameter->getType()->isReferenceType();
2221 if (compensateMangling) {
2222 Out << 'X';
2223 mangleOperatorName(OO_Amp, 1);
2224 }
2225
Daniel Dunbar7e0c1952009-11-21 09:17:15 +00002226 Out << 'L';
2227 // References to external entities use the mangled name; if the name would
2228 // not normally be manged then mangle it as unqualified.
2229 //
2230 // FIXME: The ABI specifies that external names here should have _Z, but
2231 // gcc leaves this off.
Rafael Espindolad9800722010-03-11 14:07:00 +00002232 if (compensateMangling)
2233 mangle(D, "_Z");
2234 else
2235 mangle(D, "Z");
Daniel Dunbar7e0c1952009-11-21 09:17:15 +00002236 Out << 'E';
Rafael Espindolad9800722010-03-11 14:07:00 +00002237
2238 if (compensateMangling)
2239 Out << 'E';
2240
Daniel Dunbar7e0c1952009-11-21 09:17:15 +00002241 break;
2242 }
Douglas Gregorf90b27a2011-01-03 22:36:02 +00002243
2244 case TemplateArgument::Pack: {
2245 // Note: proposal by Mike Herrick on 12/20/10
2246 Out << 'J';
2247 for (TemplateArgument::pack_iterator PA = A.pack_begin(),
2248 PAEnd = A.pack_end();
2249 PA != PAEnd; ++PA)
2250 mangleTemplateArg(P, *PA);
2251 Out << 'E';
2252 }
Daniel Dunbar7e0c1952009-11-21 09:17:15 +00002253 }
Anders Carlsson7a0ba872009-05-15 16:09:15 +00002254}
2255
Anders Carlsson0ccdf8d2009-09-27 00:38:53 +00002256void CXXNameMangler::mangleTemplateParameter(unsigned Index) {
2257 // <template-param> ::= T_ # first template parameter
2258 // ::= T <parameter-2 non-negative number> _
2259 if (Index == 0)
2260 Out << "T_";
2261 else
2262 Out << 'T' << (Index - 1) << '_';
2263}
2264
Anders Carlsson76967372009-09-17 00:43:46 +00002265// <substitution> ::= S <seq-id> _
2266// ::= S_
Anders Carlsson6862fc72009-09-17 04:16:28 +00002267bool CXXNameMangler::mangleSubstitution(const NamedDecl *ND) {
Anders Carlssone7c8cb62009-09-26 20:53:44 +00002268 // Try one of the standard substitutions first.
2269 if (mangleStandardSubstitution(ND))
2270 return true;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002271
Anders Carlsson433d1372009-11-07 04:26:04 +00002272 ND = cast<NamedDecl>(ND->getCanonicalDecl());
Anders Carlsson6862fc72009-09-17 04:16:28 +00002273 return mangleSubstitution(reinterpret_cast<uintptr_t>(ND));
2274}
2275
Anders Carlsson76967372009-09-17 00:43:46 +00002276bool CXXNameMangler::mangleSubstitution(QualType T) {
Anders Carlssond99edc42009-09-26 03:55:37 +00002277 if (!T.getCVRQualifiers()) {
2278 if (const RecordType *RT = T->getAs<RecordType>())
2279 return mangleSubstitution(RT->getDecl());
2280 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002281
Anders Carlsson76967372009-09-17 00:43:46 +00002282 uintptr_t TypePtr = reinterpret_cast<uintptr_t>(T.getAsOpaquePtr());
2283
Anders Carlssond3a932a2009-09-17 03:53:28 +00002284 return mangleSubstitution(TypePtr);
2285}
2286
Douglas Gregor1e9268e2010-04-28 05:58:56 +00002287bool CXXNameMangler::mangleSubstitution(TemplateName Template) {
2288 if (TemplateDecl *TD = Template.getAsTemplateDecl())
2289 return mangleSubstitution(TD);
Sean Huntc3021132010-05-05 15:23:54 +00002290
Douglas Gregor1e9268e2010-04-28 05:58:56 +00002291 Template = Context.getASTContext().getCanonicalTemplateName(Template);
2292 return mangleSubstitution(
2293 reinterpret_cast<uintptr_t>(Template.getAsVoidPointer()));
2294}
2295
Anders Carlssond3a932a2009-09-17 03:53:28 +00002296bool CXXNameMangler::mangleSubstitution(uintptr_t Ptr) {
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002297 llvm::DenseMap<uintptr_t, unsigned>::iterator I = Substitutions.find(Ptr);
Anders Carlsson76967372009-09-17 00:43:46 +00002298 if (I == Substitutions.end())
2299 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002300
Anders Carlsson76967372009-09-17 00:43:46 +00002301 unsigned SeqID = I->second;
2302 if (SeqID == 0)
2303 Out << "S_";
2304 else {
2305 SeqID--;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002306
Anders Carlsson76967372009-09-17 00:43:46 +00002307 // <seq-id> is encoded in base-36, using digits and upper case letters.
2308 char Buffer[10];
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002309 char *BufferPtr = llvm::array_endof(Buffer);
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002310
Anders Carlsson76967372009-09-17 00:43:46 +00002311 if (SeqID == 0) *--BufferPtr = '0';
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002312
Anders Carlsson76967372009-09-17 00:43:46 +00002313 while (SeqID) {
2314 assert(BufferPtr > Buffer && "Buffer overflow!");
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002315
John McCall6ab30e02010-06-09 07:26:17 +00002316 char c = static_cast<char>(SeqID % 36);
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002317
Anders Carlsson76967372009-09-17 00:43:46 +00002318 *--BufferPtr = (c < 10 ? '0' + c : 'A' + c - 10);
2319 SeqID /= 36;
2320 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002321
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002322 Out << 'S'
2323 << llvm::StringRef(BufferPtr, llvm::array_endof(Buffer)-BufferPtr)
2324 << '_';
Anders Carlsson76967372009-09-17 00:43:46 +00002325 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002326
Anders Carlsson76967372009-09-17 00:43:46 +00002327 return true;
2328}
2329
Anders Carlssonf514b542009-09-27 00:12:57 +00002330static bool isCharType(QualType T) {
2331 if (T.isNull())
2332 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002333
Anders Carlssonf514b542009-09-27 00:12:57 +00002334 return T->isSpecificBuiltinType(BuiltinType::Char_S) ||
2335 T->isSpecificBuiltinType(BuiltinType::Char_U);
2336}
2337
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002338/// isCharSpecialization - Returns whether a given type is a template
Anders Carlssonf514b542009-09-27 00:12:57 +00002339/// specialization of a given name with a single argument of type char.
2340static bool isCharSpecialization(QualType T, const char *Name) {
2341 if (T.isNull())
2342 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002343
Anders Carlssonf514b542009-09-27 00:12:57 +00002344 const RecordType *RT = T->getAs<RecordType>();
2345 if (!RT)
2346 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002347
2348 const ClassTemplateSpecializationDecl *SD =
Anders Carlssonf514b542009-09-27 00:12:57 +00002349 dyn_cast<ClassTemplateSpecializationDecl>(RT->getDecl());
2350 if (!SD)
2351 return false;
2352
2353 if (!isStdNamespace(SD->getDeclContext()))
2354 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002355
Anders Carlssonf514b542009-09-27 00:12:57 +00002356 const TemplateArgumentList &TemplateArgs = SD->getTemplateArgs();
2357 if (TemplateArgs.size() != 1)
2358 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002359
Anders Carlssonf514b542009-09-27 00:12:57 +00002360 if (!isCharType(TemplateArgs[0].getAsType()))
2361 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002362
Daniel Dunbar01eb9b92009-10-18 21:17:35 +00002363 return SD->getIdentifier()->getName() == Name;
Anders Carlssonf514b542009-09-27 00:12:57 +00002364}
2365
Anders Carlsson91f88602009-12-07 19:56:42 +00002366template <std::size_t StrLen>
Benjamin Kramer54353f42010-11-25 18:29:30 +00002367static bool isStreamCharSpecialization(const ClassTemplateSpecializationDecl*SD,
2368 const char (&Str)[StrLen]) {
Anders Carlsson91f88602009-12-07 19:56:42 +00002369 if (!SD->getIdentifier()->isStr(Str))
2370 return false;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002371
Anders Carlsson91f88602009-12-07 19:56:42 +00002372 const TemplateArgumentList &TemplateArgs = SD->getTemplateArgs();
2373 if (TemplateArgs.size() != 2)
2374 return false;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002375
Anders Carlsson91f88602009-12-07 19:56:42 +00002376 if (!isCharType(TemplateArgs[0].getAsType()))
2377 return false;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002378
Anders Carlsson91f88602009-12-07 19:56:42 +00002379 if (!isCharSpecialization(TemplateArgs[1].getAsType(), "char_traits"))
2380 return false;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002381
Anders Carlsson91f88602009-12-07 19:56:42 +00002382 return true;
2383}
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002384
Anders Carlssone7c8cb62009-09-26 20:53:44 +00002385bool CXXNameMangler::mangleStandardSubstitution(const NamedDecl *ND) {
2386 // <substitution> ::= St # ::std::
Anders Carlsson8c031552009-09-26 23:10:05 +00002387 if (const NamespaceDecl *NS = dyn_cast<NamespaceDecl>(ND)) {
Anders Carlsson47846d22009-12-04 06:23:23 +00002388 if (isStd(NS)) {
Anders Carlsson8c031552009-09-26 23:10:05 +00002389 Out << "St";
2390 return true;
2391 }
2392 }
2393
2394 if (const ClassTemplateDecl *TD = dyn_cast<ClassTemplateDecl>(ND)) {
2395 if (!isStdNamespace(TD->getDeclContext()))
2396 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002397
Anders Carlsson8c031552009-09-26 23:10:05 +00002398 // <substitution> ::= Sa # ::std::allocator
2399 if (TD->getIdentifier()->isStr("allocator")) {
2400 Out << "Sa";
2401 return true;
2402 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002403
Anders Carlsson189d59c2009-09-26 23:14:39 +00002404 // <<substitution> ::= Sb # ::std::basic_string
2405 if (TD->getIdentifier()->isStr("basic_string")) {
2406 Out << "Sb";
2407 return true;
2408 }
Anders Carlsson8c031552009-09-26 23:10:05 +00002409 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002410
2411 if (const ClassTemplateSpecializationDecl *SD =
Anders Carlssonf514b542009-09-27 00:12:57 +00002412 dyn_cast<ClassTemplateSpecializationDecl>(ND)) {
Eli Friedman5370ee22010-02-23 18:25:09 +00002413 if (!isStdNamespace(SD->getDeclContext()))
2414 return false;
2415
Anders Carlssonf514b542009-09-27 00:12:57 +00002416 // <substitution> ::= Ss # ::std::basic_string<char,
2417 // ::std::char_traits<char>,
2418 // ::std::allocator<char> >
2419 if (SD->getIdentifier()->isStr("basic_string")) {
2420 const TemplateArgumentList &TemplateArgs = SD->getTemplateArgs();
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002421
Anders Carlssonf514b542009-09-27 00:12:57 +00002422 if (TemplateArgs.size() != 3)
2423 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002424
Anders Carlssonf514b542009-09-27 00:12:57 +00002425 if (!isCharType(TemplateArgs[0].getAsType()))
2426 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002427
Anders Carlssonf514b542009-09-27 00:12:57 +00002428 if (!isCharSpecialization(TemplateArgs[1].getAsType(), "char_traits"))
2429 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002430
Anders Carlssonf514b542009-09-27 00:12:57 +00002431 if (!isCharSpecialization(TemplateArgs[2].getAsType(), "allocator"))
2432 return false;
2433
2434 Out << "Ss";
2435 return true;
2436 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002437
Anders Carlsson91f88602009-12-07 19:56:42 +00002438 // <substitution> ::= Si # ::std::basic_istream<char,
2439 // ::std::char_traits<char> >
2440 if (isStreamCharSpecialization(SD, "basic_istream")) {
2441 Out << "Si";
2442 return true;
2443 }
2444
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002445 // <substitution> ::= So # ::std::basic_ostream<char,
Anders Carlsson8f8fd8e2009-10-08 17:20:26 +00002446 // ::std::char_traits<char> >
Anders Carlsson91f88602009-12-07 19:56:42 +00002447 if (isStreamCharSpecialization(SD, "basic_ostream")) {
Anders Carlsson8f8fd8e2009-10-08 17:20:26 +00002448 Out << "So";
2449 return true;
2450 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002451
Anders Carlsson91f88602009-12-07 19:56:42 +00002452 // <substitution> ::= Sd # ::std::basic_iostream<char,
2453 // ::std::char_traits<char> >
2454 if (isStreamCharSpecialization(SD, "basic_iostream")) {
2455 Out << "Sd";
2456 return true;
2457 }
Anders Carlssonf514b542009-09-27 00:12:57 +00002458 }
Anders Carlsson8c031552009-09-26 23:10:05 +00002459 return false;
Anders Carlssone7c8cb62009-09-26 20:53:44 +00002460}
2461
Anders Carlsson76967372009-09-17 00:43:46 +00002462void CXXNameMangler::addSubstitution(QualType T) {
Anders Carlssond99edc42009-09-26 03:55:37 +00002463 if (!T.getCVRQualifiers()) {
2464 if (const RecordType *RT = T->getAs<RecordType>()) {
2465 addSubstitution(RT->getDecl());
2466 return;
2467 }
2468 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002469
Anders Carlsson76967372009-09-17 00:43:46 +00002470 uintptr_t TypePtr = reinterpret_cast<uintptr_t>(T.getAsOpaquePtr());
Anders Carlssond3a932a2009-09-17 03:53:28 +00002471 addSubstitution(TypePtr);
2472}
2473
Douglas Gregor1e9268e2010-04-28 05:58:56 +00002474void CXXNameMangler::addSubstitution(TemplateName Template) {
2475 if (TemplateDecl *TD = Template.getAsTemplateDecl())
2476 return addSubstitution(TD);
Sean Huntc3021132010-05-05 15:23:54 +00002477
Douglas Gregor1e9268e2010-04-28 05:58:56 +00002478 Template = Context.getASTContext().getCanonicalTemplateName(Template);
2479 addSubstitution(reinterpret_cast<uintptr_t>(Template.getAsVoidPointer()));
2480}
2481
Anders Carlssond3a932a2009-09-17 03:53:28 +00002482void CXXNameMangler::addSubstitution(uintptr_t Ptr) {
Anders Carlssond3a932a2009-09-17 03:53:28 +00002483 assert(!Substitutions.count(Ptr) && "Substitution already exists!");
Anders Carlsson9d85b722010-06-02 04:29:50 +00002484 Substitutions[Ptr] = SeqID++;
Anders Carlsson76967372009-09-17 00:43:46 +00002485}
2486
Daniel Dunbar1b077112009-11-21 09:06:10 +00002487//
Mike Stump1eb44332009-09-09 15:08:12 +00002488
Daniel Dunbar1b077112009-11-21 09:06:10 +00002489/// \brief Mangles the name of the declaration D and emits that name to the
2490/// given output stream.
2491///
2492/// If the declaration D requires a mangled name, this routine will emit that
2493/// mangled name to \p os and return true. Otherwise, \p os will be unchanged
2494/// and this routine will return false. In this case, the caller should just
2495/// emit the identifier of the declaration (\c D->getIdentifier()) as its
2496/// name.
Peter Collingbourne14110472011-01-13 18:57:25 +00002497void ItaniumMangleContext::mangleName(const NamedDecl *D,
2498 llvm::SmallVectorImpl<char> &Res) {
Daniel Dunbarc02ab4c2009-11-21 09:14:44 +00002499 assert((isa<FunctionDecl>(D) || isa<VarDecl>(D)) &&
2500 "Invalid mangleName() call, argument is not a variable or function!");
2501 assert(!isa<CXXConstructorDecl>(D) && !isa<CXXDestructorDecl>(D) &&
2502 "Invalid mangleName() call on 'structor decl!");
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002503
Daniel Dunbar1b077112009-11-21 09:06:10 +00002504 PrettyStackTraceDecl CrashInfo(D, SourceLocation(),
2505 getASTContext().getSourceManager(),
2506 "Mangling declaration");
Mike Stump1eb44332009-09-09 15:08:12 +00002507
Daniel Dunbar94fd26d2009-11-21 09:06:22 +00002508 CXXNameMangler Mangler(*this, Res);
2509 return Mangler.mangle(D);
Daniel Dunbar1b077112009-11-21 09:06:10 +00002510}
Mike Stump1eb44332009-09-09 15:08:12 +00002511
Peter Collingbourne14110472011-01-13 18:57:25 +00002512void ItaniumMangleContext::mangleCXXCtor(const CXXConstructorDecl *D,
2513 CXXCtorType Type,
2514 llvm::SmallVectorImpl<char> &Res) {
Daniel Dunbar77939c92009-11-21 09:06:31 +00002515 CXXNameMangler Mangler(*this, Res, D, Type);
2516 Mangler.mangle(D);
Daniel Dunbar1b077112009-11-21 09:06:10 +00002517}
Mike Stump1eb44332009-09-09 15:08:12 +00002518
Peter Collingbourne14110472011-01-13 18:57:25 +00002519void ItaniumMangleContext::mangleCXXDtor(const CXXDestructorDecl *D,
2520 CXXDtorType Type,
2521 llvm::SmallVectorImpl<char> &Res) {
Daniel Dunbar77939c92009-11-21 09:06:31 +00002522 CXXNameMangler Mangler(*this, Res, D, Type);
2523 Mangler.mangle(D);
Daniel Dunbar1b077112009-11-21 09:06:10 +00002524}
Mike Stumpf1216772009-07-31 18:25:34 +00002525
Peter Collingbourne14110472011-01-13 18:57:25 +00002526void ItaniumMangleContext::mangleThunk(const CXXMethodDecl *MD,
2527 const ThunkInfo &Thunk,
2528 llvm::SmallVectorImpl<char> &Res) {
Anders Carlsson19879c92010-03-23 17:17:29 +00002529 // <special-name> ::= T <call-offset> <base encoding>
2530 // # base is the nominal target function of thunk
2531 // <special-name> ::= Tc <call-offset> <call-offset> <base encoding>
2532 // # base is the nominal target function of thunk
2533 // # first call-offset is 'this' adjustment
2534 // # second call-offset is result adjustment
Sean Huntc3021132010-05-05 15:23:54 +00002535
Anders Carlsson19879c92010-03-23 17:17:29 +00002536 assert(!isa<CXXDestructorDecl>(MD) &&
2537 "Use mangleCXXDtor for destructor decls!");
Sean Huntc3021132010-05-05 15:23:54 +00002538
Anders Carlsson19879c92010-03-23 17:17:29 +00002539 CXXNameMangler Mangler(*this, Res);
2540 Mangler.getStream() << "_ZT";
2541 if (!Thunk.Return.isEmpty())
2542 Mangler.getStream() << 'c';
Sean Huntc3021132010-05-05 15:23:54 +00002543
Anders Carlsson19879c92010-03-23 17:17:29 +00002544 // Mangle the 'this' pointer adjustment.
2545 Mangler.mangleCallOffset(Thunk.This.NonVirtual, Thunk.This.VCallOffsetOffset);
Sean Huntc3021132010-05-05 15:23:54 +00002546
Anders Carlsson19879c92010-03-23 17:17:29 +00002547 // Mangle the return pointer adjustment if there is one.
2548 if (!Thunk.Return.isEmpty())
2549 Mangler.mangleCallOffset(Thunk.Return.NonVirtual,
2550 Thunk.Return.VBaseOffsetOffset);
Sean Huntc3021132010-05-05 15:23:54 +00002551
Anders Carlsson19879c92010-03-23 17:17:29 +00002552 Mangler.mangleFunctionEncoding(MD);
2553}
2554
Sean Huntc3021132010-05-05 15:23:54 +00002555void
Peter Collingbourne14110472011-01-13 18:57:25 +00002556ItaniumMangleContext::mangleCXXDtorThunk(const CXXDestructorDecl *DD,
2557 CXXDtorType Type,
2558 const ThisAdjustment &ThisAdjustment,
2559 llvm::SmallVectorImpl<char> &Res) {
Anders Carlsson19879c92010-03-23 17:17:29 +00002560 // <special-name> ::= T <call-offset> <base encoding>
2561 // # base is the nominal target function of thunk
Sean Huntc3021132010-05-05 15:23:54 +00002562
Anders Carlsson19879c92010-03-23 17:17:29 +00002563 CXXNameMangler Mangler(*this, Res, DD, Type);
2564 Mangler.getStream() << "_ZT";
2565
2566 // Mangle the 'this' pointer adjustment.
Sean Huntc3021132010-05-05 15:23:54 +00002567 Mangler.mangleCallOffset(ThisAdjustment.NonVirtual,
Anders Carlsson19879c92010-03-23 17:17:29 +00002568 ThisAdjustment.VCallOffsetOffset);
2569
2570 Mangler.mangleFunctionEncoding(DD);
2571}
2572
Daniel Dunbarc0747712009-11-21 09:12:13 +00002573/// mangleGuardVariable - Returns the mangled name for a guard variable
2574/// for the passed in VarDecl.
Peter Collingbourne14110472011-01-13 18:57:25 +00002575void ItaniumMangleContext::mangleItaniumGuardVariable(const VarDecl *D,
2576 llvm::SmallVectorImpl<char> &Res) {
Daniel Dunbarc0747712009-11-21 09:12:13 +00002577 // <special-name> ::= GV <object name> # Guard variable for one-time
2578 // # initialization
2579 CXXNameMangler Mangler(*this, Res);
2580 Mangler.getStream() << "_ZGV";
2581 Mangler.mangleName(D);
2582}
2583
Peter Collingbourne14110472011-01-13 18:57:25 +00002584void ItaniumMangleContext::mangleReferenceTemporary(const VarDecl *D,
Anders Carlsson715edf22010-06-26 16:09:40 +00002585 llvm::SmallVectorImpl<char> &Res) {
2586 // We match the GCC mangling here.
2587 // <special-name> ::= GR <object name>
2588 CXXNameMangler Mangler(*this, Res);
2589 Mangler.getStream() << "_ZGR";
2590 Mangler.mangleName(D);
2591}
2592
Peter Collingbourne14110472011-01-13 18:57:25 +00002593void ItaniumMangleContext::mangleCXXVTable(const CXXRecordDecl *RD,
2594 llvm::SmallVectorImpl<char> &Res) {
Daniel Dunbarc0747712009-11-21 09:12:13 +00002595 // <special-name> ::= TV <type> # virtual table
Daniel Dunbar94fd26d2009-11-21 09:06:22 +00002596 CXXNameMangler Mangler(*this, Res);
Daniel Dunbarc0747712009-11-21 09:12:13 +00002597 Mangler.getStream() << "_ZTV";
Douglas Gregor1b12a3b2010-05-26 05:11:13 +00002598 Mangler.mangleNameOrStandardSubstitution(RD);
Daniel Dunbar1b077112009-11-21 09:06:10 +00002599}
Mike Stump82d75b02009-11-10 01:58:37 +00002600
Peter Collingbourne14110472011-01-13 18:57:25 +00002601void ItaniumMangleContext::mangleCXXVTT(const CXXRecordDecl *RD,
2602 llvm::SmallVectorImpl<char> &Res) {
Daniel Dunbarc0747712009-11-21 09:12:13 +00002603 // <special-name> ::= TT <type> # VTT structure
Daniel Dunbar94fd26d2009-11-21 09:06:22 +00002604 CXXNameMangler Mangler(*this, Res);
Daniel Dunbarc0747712009-11-21 09:12:13 +00002605 Mangler.getStream() << "_ZTT";
Douglas Gregor1b12a3b2010-05-26 05:11:13 +00002606 Mangler.mangleNameOrStandardSubstitution(RD);
Daniel Dunbar1b077112009-11-21 09:06:10 +00002607}
Mike Stumpab3f7e92009-11-10 01:41:59 +00002608
Peter Collingbourne14110472011-01-13 18:57:25 +00002609void ItaniumMangleContext::mangleCXXCtorVTable(const CXXRecordDecl *RD,
2610 int64_t Offset,
2611 const CXXRecordDecl *Type,
2612 llvm::SmallVectorImpl<char> &Res) {
Daniel Dunbarc0747712009-11-21 09:12:13 +00002613 // <special-name> ::= TC <type> <offset number> _ <base type>
Daniel Dunbar94fd26d2009-11-21 09:06:22 +00002614 CXXNameMangler Mangler(*this, Res);
Daniel Dunbarc0747712009-11-21 09:12:13 +00002615 Mangler.getStream() << "_ZTC";
Douglas Gregor1b12a3b2010-05-26 05:11:13 +00002616 Mangler.mangleNameOrStandardSubstitution(RD);
Daniel Dunbarc0747712009-11-21 09:12:13 +00002617 Mangler.getStream() << Offset;
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002618 Mangler.getStream() << '_';
Douglas Gregor1b12a3b2010-05-26 05:11:13 +00002619 Mangler.mangleNameOrStandardSubstitution(Type);
Daniel Dunbar1b077112009-11-21 09:06:10 +00002620}
Mike Stump738f8c22009-07-31 23:15:31 +00002621
Peter Collingbourne14110472011-01-13 18:57:25 +00002622void ItaniumMangleContext::mangleCXXRTTI(QualType Ty,
2623 llvm::SmallVectorImpl<char> &Res) {
Daniel Dunbarc0747712009-11-21 09:12:13 +00002624 // <special-name> ::= TI <type> # typeinfo structure
Douglas Gregor154fe982009-12-23 22:04:40 +00002625 assert(!Ty.hasQualifiers() && "RTTI info cannot have top-level qualifiers");
Daniel Dunbar94fd26d2009-11-21 09:06:22 +00002626 CXXNameMangler Mangler(*this, Res);
Daniel Dunbarc0747712009-11-21 09:12:13 +00002627 Mangler.getStream() << "_ZTI";
2628 Mangler.mangleType(Ty);
Daniel Dunbar1b077112009-11-21 09:06:10 +00002629}
Mike Stump67795982009-11-14 00:14:13 +00002630
Peter Collingbourne14110472011-01-13 18:57:25 +00002631void ItaniumMangleContext::mangleCXXRTTIName(QualType Ty,
2632 llvm::SmallVectorImpl<char> &Res) {
Daniel Dunbarc0747712009-11-21 09:12:13 +00002633 // <special-name> ::= TS <type> # typeinfo name (null terminated byte string)
Daniel Dunbar94fd26d2009-11-21 09:06:22 +00002634 CXXNameMangler Mangler(*this, Res);
Daniel Dunbarc0747712009-11-21 09:12:13 +00002635 Mangler.getStream() << "_ZTS";
2636 Mangler.mangleType(Ty);
Mike Stumpf1216772009-07-31 18:25:34 +00002637}
Peter Collingbourne14110472011-01-13 18:57:25 +00002638
2639MangleContext *clang::createItaniumMangleContext(ASTContext &Context,
2640 Diagnostic &Diags) {
2641 return new ItaniumMangleContext(Context, Diags);
2642}