blob: 939ca7a924aa03eb9c432ea4d077333ee18a5115 [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"
Rafael Espindola4e274e92011-02-15 22:23:51 +000026#include "clang/Basic/TargetInfo.h"
Anders Carlssonc4355b62009-10-07 01:45:02 +000027#include "llvm/ADT/StringExtras.h"
Douglas Gregor5f2bfd42009-02-13 00:10:09 +000028#include "llvm/Support/raw_ostream.h"
John McCallefe6aee2009-09-05 07:56:18 +000029#include "llvm/Support/ErrorHandling.h"
Anders Carlssonf98574b2010-02-05 07:31:37 +000030
31#define MANGLE_CHECKER 0
32
33#if MANGLE_CHECKER
34#include <cxxabi.h>
35#endif
36
Douglas Gregor5f2bfd42009-02-13 00:10:09 +000037using namespace clang;
Charles Davis685b1d92010-05-26 18:25:27 +000038
Douglas Gregor5f2bfd42009-02-13 00:10:09 +000039namespace {
Fariborz Jahanian57058532010-03-03 19:41:08 +000040
John McCall82b7d7b2010-10-18 21:28:44 +000041static const CXXRecordDecl *GetLocalClassDecl(const NamedDecl *ND) {
42 const DeclContext *DC = dyn_cast<DeclContext>(ND);
43 if (!DC)
44 DC = ND->getDeclContext();
45 while (!DC->isNamespace() && !DC->isTranslationUnit()) {
46 if (isa<FunctionDecl>(DC->getParent()))
47 return dyn_cast<CXXRecordDecl>(DC);
48 DC = DC->getParent();
Fariborz Jahanian57058532010-03-03 19:41:08 +000049 }
50 return 0;
51}
52
Anders Carlsson7e120032009-11-24 05:36:32 +000053static const CXXMethodDecl *getStructor(const CXXMethodDecl *MD) {
54 assert((isa<CXXConstructorDecl>(MD) || isa<CXXDestructorDecl>(MD)) &&
55 "Passed in decl is not a ctor or dtor!");
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +000056
Anders Carlsson7e120032009-11-24 05:36:32 +000057 if (const TemplateDecl *TD = MD->getPrimaryTemplate()) {
58 MD = cast<CXXMethodDecl>(TD->getTemplatedDecl());
59
60 assert((isa<CXXConstructorDecl>(MD) || isa<CXXDestructorDecl>(MD)) &&
61 "Templated decl is not a ctor or dtor!");
62 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +000063
Anders Carlsson7e120032009-11-24 05:36:32 +000064 return MD;
65}
John McCall1dd73832010-02-04 01:42:13 +000066
67static const unsigned UnknownArity = ~0U;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +000068
Peter Collingbourne14110472011-01-13 18:57:25 +000069class ItaniumMangleContext : public MangleContext {
70 llvm::DenseMap<const TagDecl *, uint64_t> AnonStructIds;
71 unsigned Discriminator;
72 llvm::DenseMap<const NamedDecl*, unsigned> Uniquifier;
73
74public:
75 explicit ItaniumMangleContext(ASTContext &Context,
76 Diagnostic &Diags)
77 : MangleContext(Context, Diags) { }
78
79 uint64_t getAnonymousStructId(const TagDecl *TD) {
80 std::pair<llvm::DenseMap<const TagDecl *,
81 uint64_t>::iterator, bool> Result =
82 AnonStructIds.insert(std::make_pair(TD, AnonStructIds.size()));
83 return Result.first->second;
84 }
85
86 void startNewFunction() {
87 MangleContext::startNewFunction();
88 mangleInitDiscriminator();
89 }
90
91 /// @name Mangler Entry Points
92 /// @{
93
94 bool shouldMangleDeclName(const NamedDecl *D);
Rafael Espindola0e376a02011-02-11 01:41:00 +000095 void mangleName(const NamedDecl *D, llvm::raw_ostream &);
Peter Collingbourne14110472011-01-13 18:57:25 +000096 void mangleThunk(const CXXMethodDecl *MD,
97 const ThunkInfo &Thunk,
Rafael Espindolaf0be9792011-02-11 02:52:17 +000098 llvm::raw_ostream &);
Peter Collingbourne14110472011-01-13 18:57:25 +000099 void mangleCXXDtorThunk(const CXXDestructorDecl *DD, CXXDtorType Type,
100 const ThisAdjustment &ThisAdjustment,
Rafael Espindolaf0be9792011-02-11 02:52:17 +0000101 llvm::raw_ostream &);
Peter Collingbourne14110472011-01-13 18:57:25 +0000102 void mangleReferenceTemporary(const VarDecl *D,
Rafael Espindolaf0be9792011-02-11 02:52:17 +0000103 llvm::raw_ostream &);
Peter Collingbourne14110472011-01-13 18:57:25 +0000104 void mangleCXXVTable(const CXXRecordDecl *RD,
Rafael Espindolaf0be9792011-02-11 02:52:17 +0000105 llvm::raw_ostream &);
Peter Collingbourne14110472011-01-13 18:57:25 +0000106 void mangleCXXVTT(const CXXRecordDecl *RD,
Rafael Espindolaf0be9792011-02-11 02:52:17 +0000107 llvm::raw_ostream &);
Peter Collingbourne14110472011-01-13 18:57:25 +0000108 void mangleCXXCtorVTable(const CXXRecordDecl *RD, int64_t Offset,
109 const CXXRecordDecl *Type,
Rafael Espindolaf0be9792011-02-11 02:52:17 +0000110 llvm::raw_ostream &);
111 void mangleCXXRTTI(QualType T, llvm::raw_ostream &);
112 void mangleCXXRTTIName(QualType T, llvm::raw_ostream &);
Peter Collingbourne14110472011-01-13 18:57:25 +0000113 void mangleCXXCtor(const CXXConstructorDecl *D, CXXCtorType Type,
Rafael Espindola0e376a02011-02-11 01:41:00 +0000114 llvm::raw_ostream &);
Peter Collingbourne14110472011-01-13 18:57:25 +0000115 void mangleCXXDtor(const CXXDestructorDecl *D, CXXDtorType Type,
Rafael Espindola0e376a02011-02-11 01:41:00 +0000116 llvm::raw_ostream &);
Peter Collingbourne14110472011-01-13 18:57:25 +0000117
Rafael Espindolaf0be9792011-02-11 02:52:17 +0000118 void mangleItaniumGuardVariable(const VarDecl *D, llvm::raw_ostream &);
Peter Collingbourne14110472011-01-13 18:57:25 +0000119
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;
Rafael Espindola0e376a02011-02-11 01:41:00 +0000139 llvm::raw_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:
Rafael Espindola0e376a02011-02-11 01:41:00 +0000152 CXXNameMangler(ItaniumMangleContext &C, llvm::raw_ostream &Out_)
Rafael Espindolac4850c22011-02-10 23:59:36 +0000153 : Context(C), Out(Out_), Structor(0), StructorType(0), SeqID(0) { }
Rafael Espindola0e376a02011-02-11 01:41:00 +0000154 CXXNameMangler(ItaniumMangleContext &C, llvm::raw_ostream &Out_,
Daniel Dunbar77939c92009-11-21 09:06:31 +0000155 const CXXConstructorDecl *D, CXXCtorType Type)
Rafael Espindolac4850c22011-02-10 23:59:36 +0000156 : Context(C), Out(Out_), Structor(getStructor(D)), StructorType(Type),
Anders Carlsson9d85b722010-06-02 04:29:50 +0000157 SeqID(0) { }
Rafael Espindola0e376a02011-02-11 01:41:00 +0000158 CXXNameMangler(ItaniumMangleContext &C, llvm::raw_ostream &Out_,
Daniel Dunbar77939c92009-11-21 09:06:31 +0000159 const CXXDestructorDecl *D, CXXDtorType Type)
Rafael Espindolac4850c22011-02-10 23:59:36 +0000160 : Context(C), Out(Out_), Structor(getStructor(D)), StructorType(Type),
Anders Carlsson9d85b722010-06-02 04:29:50 +0000161 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
Rafael Espindola0e376a02011-02-11 01:41:00 +0000174 llvm::raw_ostream &getStream() { return Out; }
Daniel Dunbarc0747712009-11-21 09:12:13 +0000175
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);
Douglas Gregor0a9a6d62011-01-26 17:36:28 +0000231 void mangleRefQualifier(RefQualifierKind RefQualifier);
John McCallefe6aee2009-09-05 07:56:18 +0000232
Anders Carlsson7b06f6c2009-12-10 03:14:39 +0000233 void mangleObjCMethodName(const ObjCMethodDecl *MD);
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000234
Daniel Dunbar1b077112009-11-21 09:06:10 +0000235 // Declare manglers for every type class.
John McCallefe6aee2009-09-05 07:56:18 +0000236#define ABSTRACT_TYPE(CLASS, PARENT)
237#define NON_CANONICAL_TYPE(CLASS, PARENT)
238#define TYPE(CLASS, PARENT) void mangleType(const CLASS##Type *T);
239#include "clang/AST/TypeNodes.def"
240
Daniel Dunbar1b077112009-11-21 09:06:10 +0000241 void mangleType(const TagType*);
John McCallb6f532e2010-07-14 06:43:17 +0000242 void mangleType(TemplateName);
Daniel Dunbar1b077112009-11-21 09:06:10 +0000243 void mangleBareFunctionType(const FunctionType *T,
244 bool MangleReturnType);
Bob Wilson57147a82010-11-16 00:32:18 +0000245 void mangleNeonVectorType(const VectorType *T);
Anders Carlssone170ba72009-12-14 01:45:37 +0000246
247 void mangleIntegerLiteral(QualType T, const llvm::APSInt &Value);
John McCall2f27bf82010-02-04 02:56:29 +0000248 void mangleMemberExpr(const Expr *Base, bool IsArrow,
249 NestedNameSpecifier *Qualifier,
250 DeclarationName Name,
251 unsigned KnownArity);
John McCall5e1e89b2010-08-18 19:18:59 +0000252 void mangleExpression(const Expr *E, unsigned Arity = UnknownArity);
Daniel Dunbar1b077112009-11-21 09:06:10 +0000253 void mangleCXXCtorType(CXXCtorType T);
254 void mangleCXXDtorType(CXXDtorType T);
Mike Stump1eb44332009-09-09 15:08:12 +0000255
John McCall6dbce192010-08-20 00:17:19 +0000256 void mangleTemplateArgs(const ExplicitTemplateArgumentList &TemplateArgs);
Douglas Gregor20f0cc72010-04-23 03:10:43 +0000257 void mangleTemplateArgs(TemplateName Template,
258 const TemplateArgument *TemplateArgs,
Sean Huntc3021132010-05-05 15:23:54 +0000259 unsigned NumTemplateArgs);
Rafael Espindolad9800722010-03-11 14:07:00 +0000260 void mangleTemplateArgs(const TemplateParameterList &PL,
261 const TemplateArgument *TemplateArgs,
Daniel Dunbar1b077112009-11-21 09:06:10 +0000262 unsigned NumTemplateArgs);
Rafael Espindolad9800722010-03-11 14:07:00 +0000263 void mangleTemplateArgs(const TemplateParameterList &PL,
264 const TemplateArgumentList &AL);
265 void mangleTemplateArg(const NamedDecl *P, const TemplateArgument &A);
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000266
Daniel Dunbar1b077112009-11-21 09:06:10 +0000267 void mangleTemplateParameter(unsigned Index);
268};
Peter Collingbourne14110472011-01-13 18:57:25 +0000269
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000270}
271
Anders Carlsson43f17402009-04-02 15:51:53 +0000272static bool isInCLinkageSpecification(const Decl *D) {
Douglas Gregor457e2812009-10-28 16:31:34 +0000273 D = D->getCanonicalDecl();
Mike Stump1eb44332009-09-09 15:08:12 +0000274 for (const DeclContext *DC = D->getDeclContext();
Anders Carlsson43f17402009-04-02 15:51:53 +0000275 !DC->isTranslationUnit(); DC = DC->getParent()) {
Mike Stump1eb44332009-09-09 15:08:12 +0000276 if (const LinkageSpecDecl *Linkage = dyn_cast<LinkageSpecDecl>(DC))
Anders Carlsson43f17402009-04-02 15:51:53 +0000277 return Linkage->getLanguage() == LinkageSpecDecl::lang_c;
278 }
Mike Stump1eb44332009-09-09 15:08:12 +0000279
Anders Carlsson43f17402009-04-02 15:51:53 +0000280 return false;
281}
282
Peter Collingbourne14110472011-01-13 18:57:25 +0000283bool ItaniumMangleContext::shouldMangleDeclName(const NamedDecl *D) {
Daniel Dunbarf981bf82009-11-21 09:14:52 +0000284 // In C, functions with no attributes never need to be mangled. Fastpath them.
285 if (!getASTContext().getLangOptions().CPlusPlus && !D->hasAttrs())
286 return false;
287
288 // Any decl can be declared with __asm("foo") on it, and this takes precedence
289 // over all other naming in the .o file.
290 if (D->hasAttr<AsmLabelAttr>())
291 return true;
292
Mike Stump141c5af2009-09-02 00:25:38 +0000293 // Clang's "overloadable" attribute extension to C/C++ implies name mangling
Anders Carlssona1e16222009-11-07 07:15:03 +0000294 // (always) as does passing a C++ member function and a function
295 // whose name is not a simple identifier.
Daniel Dunbarf981bf82009-11-21 09:14:52 +0000296 const FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
297 if (FD && (FD->hasAttr<OverloadableAttr>() || isa<CXXMethodDecl>(FD) ||
298 !FD->getDeclName().isIdentifier()))
299 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000300
Daniel Dunbarf981bf82009-11-21 09:14:52 +0000301 // Otherwise, no mangling is done outside C++ mode.
302 if (!getASTContext().getLangOptions().CPlusPlus)
303 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000304
Sean Hunt31455252010-01-24 03:04:27 +0000305 // Variables at global scope with non-internal linkage are not mangled
Eli Friedman7facf842009-12-02 20:32:49 +0000306 if (!FD) {
307 const DeclContext *DC = D->getDeclContext();
308 // Check for extern variable declared locally.
Fariborz Jahaniane81c5612010-06-30 18:57:21 +0000309 if (DC->isFunctionOrMethod() && D->hasLinkage())
Eli Friedman7facf842009-12-02 20:32:49 +0000310 while (!DC->isNamespace() && !DC->isTranslationUnit())
311 DC = DC->getParent();
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000312 if (DC->isTranslationUnit() && D->getLinkage() != InternalLinkage)
Eli Friedman7facf842009-12-02 20:32:49 +0000313 return false;
314 }
315
Eli Friedmanc00cb642010-07-18 20:49:59 +0000316 // Class members are always mangled.
317 if (D->getDeclContext()->isRecord())
318 return true;
319
Eli Friedman7facf842009-12-02 20:32:49 +0000320 // C functions and "main" are not mangled.
321 if ((FD && FD->isMain()) || isInCLinkageSpecification(D))
Daniel Dunbarf981bf82009-11-21 09:14:52 +0000322 return false;
323
Anders Carlsson43f17402009-04-02 15:51:53 +0000324 return true;
325}
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000326
Daniel Dunbar7e0c1952009-11-21 09:17:15 +0000327void CXXNameMangler::mangle(const NamedDecl *D, llvm::StringRef Prefix) {
Mike Stump141c5af2009-09-02 00:25:38 +0000328 // Any decl can be declared with __asm("foo") on it, and this takes precedence
329 // over all other naming in the .o file.
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000330 if (const AsmLabelAttr *ALA = D->getAttr<AsmLabelAttr>()) {
Chris Lattnerca3f25c2009-03-21 08:24:40 +0000331 // If we have an asm name, then we use it as the mangling.
Rafael Espindola4e274e92011-02-15 22:23:51 +0000332
333 // Adding the prefix can cause problems when one file has a "foo" and
334 // another has a "\01foo". That is known to happen on ELF with the
335 // tricks normally used for producing aliases (PR9177). Fortunately the
336 // llvm mangler on ELF is a nop, so we can just avoid adding the \01
337 // marker.
338 llvm::StringRef UserLabelPrefix =
339 getASTContext().Target.getUserLabelPrefix();
340 if (!UserLabelPrefix.empty())
341 Out << '\01'; // LLVM IR Marker for __asm("foo")
342
Chris Lattnerca3f25c2009-03-21 08:24:40 +0000343 Out << ALA->getLabel();
Daniel Dunbarf981bf82009-11-21 09:14:52 +0000344 return;
Chris Lattnerca3f25c2009-03-21 08:24:40 +0000345 }
Mike Stump1eb44332009-09-09 15:08:12 +0000346
Sean Hunt31455252010-01-24 03:04:27 +0000347 // <mangled-name> ::= _Z <encoding>
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000348 // ::= <data name>
349 // ::= <special-name>
Daniel Dunbar7e0c1952009-11-21 09:17:15 +0000350 Out << Prefix;
351 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
Daniel Dunbarf981bf82009-11-21 09:14:52 +0000352 mangleFunctionEncoding(FD);
Rafael Espindolad9800722010-03-11 14:07:00 +0000353 else if (const VarDecl *VD = dyn_cast<VarDecl>(D))
354 mangleName(VD);
Daniel Dunbar7e0c1952009-11-21 09:17:15 +0000355 else
Rafael Espindolad9800722010-03-11 14:07:00 +0000356 mangleName(cast<FieldDecl>(D));
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000357}
358
359void CXXNameMangler::mangleFunctionEncoding(const FunctionDecl *FD) {
360 // <encoding> ::= <function name> <bare-function-type>
361 mangleName(FD);
Mike Stump1eb44332009-09-09 15:08:12 +0000362
Daniel Dunbar7e0c1952009-11-21 09:17:15 +0000363 // Don't mangle in the type if this isn't a decl we should typically mangle.
364 if (!Context.shouldMangleDeclName(FD))
365 return;
366
Mike Stump141c5af2009-09-02 00:25:38 +0000367 // Whether the mangling of a function type includes the return type depends on
368 // the context and the nature of the function. The rules for deciding whether
369 // the return type is included are:
Mike Stump1eb44332009-09-09 15:08:12 +0000370 //
Douglas Gregor1fd2dd12009-06-29 22:39:32 +0000371 // 1. Template functions (names or types) have return types encoded, with
372 // the exceptions listed below.
Mike Stump1eb44332009-09-09 15:08:12 +0000373 // 2. Function types not appearing as part of a function name mangling,
Douglas Gregor1fd2dd12009-06-29 22:39:32 +0000374 // e.g. parameters, pointer types, etc., have return type encoded, with the
375 // exceptions listed below.
376 // 3. Non-template function names do not have return types encoded.
377 //
Mike Stump141c5af2009-09-02 00:25:38 +0000378 // The exceptions mentioned in (1) and (2) above, for which the return type is
379 // never included, are
Douglas Gregor1fd2dd12009-06-29 22:39:32 +0000380 // 1. Constructors.
381 // 2. Destructors.
382 // 3. Conversion operator functions, e.g. operator int.
383 bool MangleReturnType = false;
Anders Carlsson9234b7f2009-09-17 03:46:43 +0000384 if (FunctionTemplateDecl *PrimaryTemplate = FD->getPrimaryTemplate()) {
385 if (!(isa<CXXConstructorDecl>(FD) || isa<CXXDestructorDecl>(FD) ||
386 isa<CXXConversionDecl>(FD)))
387 MangleReturnType = true;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000388
Anders Carlsson9234b7f2009-09-17 03:46:43 +0000389 // Mangle the type of the primary template.
390 FD = PrimaryTemplate->getTemplatedDecl();
391 }
392
John McCall54e14c42009-10-22 22:37:11 +0000393 // Do the canonicalization out here because parameter types can
394 // undergo additional canonicalization (e.g. array decay).
John McCallf4c73712011-01-19 06:33:43 +0000395 const FunctionType *FT
396 = cast<FunctionType>(Context.getASTContext()
John McCall54e14c42009-10-22 22:37:11 +0000397 .getCanonicalType(FD->getType()));
398
399 mangleBareFunctionType(FT, MangleReturnType);
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000400}
401
Anders Carlsson47846d22009-12-04 06:23:23 +0000402static const DeclContext *IgnoreLinkageSpecDecls(const DeclContext *DC) {
403 while (isa<LinkageSpecDecl>(DC)) {
Anders Carlsson47846d22009-12-04 06:23:23 +0000404 DC = DC->getParent();
405 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000406
Anders Carlsson47846d22009-12-04 06:23:23 +0000407 return DC;
408}
409
Anders Carlssonc820f902010-06-02 15:58:27 +0000410/// isStd - Return whether a given namespace is the 'std' namespace.
411static bool isStd(const NamespaceDecl *NS) {
412 if (!IgnoreLinkageSpecDecls(NS->getParent())->isTranslationUnit())
413 return false;
414
415 const IdentifierInfo *II = NS->getOriginalNamespace()->getIdentifier();
416 return II && II->isStr("std");
417}
418
Anders Carlsson47846d22009-12-04 06:23:23 +0000419// isStdNamespace - Return whether a given decl context is a toplevel 'std'
420// namespace.
Daniel Dunbar1308af92009-11-21 09:11:45 +0000421static bool isStdNamespace(const DeclContext *DC) {
Anders Carlsson47846d22009-12-04 06:23:23 +0000422 if (!DC->isNamespace())
423 return false;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000424
Anders Carlsson47846d22009-12-04 06:23:23 +0000425 return isStd(cast<NamespaceDecl>(DC));
Daniel Dunbar1308af92009-11-21 09:11:45 +0000426}
427
Anders Carlssonbb36ba42009-09-26 03:24:57 +0000428static const TemplateDecl *
429isTemplate(const NamedDecl *ND, const TemplateArgumentList *&TemplateArgs) {
Anders Carlsson2744a062009-09-18 19:00:18 +0000430 // Check if we have a function template.
431 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)){
Anders Carlssonbb36ba42009-09-26 03:24:57 +0000432 if (const TemplateDecl *TD = FD->getPrimaryTemplate()) {
Anders Carlsson2744a062009-09-18 19:00:18 +0000433 TemplateArgs = FD->getTemplateSpecializationArgs();
Anders Carlssonbb36ba42009-09-26 03:24:57 +0000434 return TD;
Anders Carlsson2744a062009-09-18 19:00:18 +0000435 }
436 }
437
Anders Carlssoneafc6dc2009-09-18 19:44:50 +0000438 // Check if we have a class template.
439 if (const ClassTemplateSpecializationDecl *Spec =
440 dyn_cast<ClassTemplateSpecializationDecl>(ND)) {
441 TemplateArgs = &Spec->getTemplateArgs();
Anders Carlssonbb36ba42009-09-26 03:24:57 +0000442 return Spec->getSpecializedTemplate();
Anders Carlssoneafc6dc2009-09-18 19:44:50 +0000443 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000444
Anders Carlsson2744a062009-09-18 19:00:18 +0000445 return 0;
446}
447
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000448void CXXNameMangler::mangleName(const NamedDecl *ND) {
449 // <name> ::= <nested-name>
450 // ::= <unscoped-name>
451 // ::= <unscoped-template-name> <template-args>
Anders Carlsson201ce742009-09-17 03:17:01 +0000452 // ::= <local-name>
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000453 //
Anders Carlssond58d6f72009-09-17 16:12:20 +0000454 const DeclContext *DC = ND->getDeclContext();
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000455
Eli Friedman7facf842009-12-02 20:32:49 +0000456 // If this is an extern variable declared locally, the relevant DeclContext
457 // is that of the containing namespace, or the translation unit.
458 if (isa<FunctionDecl>(DC) && ND->hasLinkage())
459 while (!DC->isNamespace() && !DC->isTranslationUnit())
460 DC = DC->getParent();
John McCall82b7d7b2010-10-18 21:28:44 +0000461 else if (GetLocalClassDecl(ND)) {
462 mangleLocalName(ND);
463 return;
464 }
Eli Friedman7facf842009-12-02 20:32:49 +0000465
Anders Carlsson5cc58c62009-09-22 17:23:30 +0000466 while (isa<LinkageSpecDecl>(DC))
Anders Carlssond58d6f72009-09-17 16:12:20 +0000467 DC = DC->getParent();
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000468
Anders Carlssond58d6f72009-09-17 16:12:20 +0000469 if (DC->isTranslationUnit() || isStdNamespace(DC)) {
Anders Carlsson2744a062009-09-18 19:00:18 +0000470 // Check if we have a template.
471 const TemplateArgumentList *TemplateArgs = 0;
Anders Carlsson0fa6df42009-09-26 19:45:45 +0000472 if (const TemplateDecl *TD = isTemplate(ND, TemplateArgs)) {
Anders Carlsson2744a062009-09-18 19:00:18 +0000473 mangleUnscopedTemplateName(TD);
Rafael Espindolad9800722010-03-11 14:07:00 +0000474 TemplateParameterList *TemplateParameters = TD->getTemplateParameters();
475 mangleTemplateArgs(*TemplateParameters, *TemplateArgs);
Anders Carlsson2744a062009-09-18 19:00:18 +0000476 return;
Anders Carlsson7482e242009-09-18 04:29:09 +0000477 }
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000478
Anders Carlsson7482e242009-09-18 04:29:09 +0000479 mangleUnscopedName(ND);
480 return;
481 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000482
Anders Carlsson7b06f6c2009-12-10 03:14:39 +0000483 if (isa<FunctionDecl>(DC) || isa<ObjCMethodDecl>(DC)) {
Anders Carlsson7482e242009-09-18 04:29:09 +0000484 mangleLocalName(ND);
485 return;
486 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000487
Eli Friedman7facf842009-12-02 20:32:49 +0000488 mangleNestedName(ND, DC);
Anders Carlsson7482e242009-09-18 04:29:09 +0000489}
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000490void CXXNameMangler::mangleName(const TemplateDecl *TD,
Anders Carlsson7624f212009-09-18 02:42:01 +0000491 const TemplateArgument *TemplateArgs,
492 unsigned NumTemplateArgs) {
Anders Carlsson47846d22009-12-04 06:23:23 +0000493 const DeclContext *DC = IgnoreLinkageSpecDecls(TD->getDeclContext());
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000494
Anders Carlsson7624f212009-09-18 02:42:01 +0000495 if (DC->isTranslationUnit() || isStdNamespace(DC)) {
Anders Carlsson0fa6df42009-09-26 19:45:45 +0000496 mangleUnscopedTemplateName(TD);
Rafael Espindolad9800722010-03-11 14:07:00 +0000497 TemplateParameterList *TemplateParameters = TD->getTemplateParameters();
498 mangleTemplateArgs(*TemplateParameters, TemplateArgs, NumTemplateArgs);
Anders Carlsson7624f212009-09-18 02:42:01 +0000499 } else {
500 mangleNestedName(TD, TemplateArgs, NumTemplateArgs);
501 }
502}
503
Anders Carlsson201ce742009-09-17 03:17:01 +0000504void CXXNameMangler::mangleUnscopedName(const NamedDecl *ND) {
505 // <unscoped-name> ::= <unqualified-name>
506 // ::= St <unqualified-name> # ::std::
507 if (isStdNamespace(ND->getDeclContext()))
508 Out << "St";
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000509
Anders Carlsson201ce742009-09-17 03:17:01 +0000510 mangleUnqualifiedName(ND);
511}
512
Anders Carlsson0fa6df42009-09-26 19:45:45 +0000513void CXXNameMangler::mangleUnscopedTemplateName(const TemplateDecl *ND) {
Anders Carlsson201ce742009-09-17 03:17:01 +0000514 // <unscoped-template-name> ::= <unscoped-name>
515 // ::= <substitution>
Anders Carlsson7624f212009-09-18 02:42:01 +0000516 if (mangleSubstitution(ND))
Anders Carlsson03c9d532009-09-17 04:02:31 +0000517 return;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000518
Douglas Gregor32fb4e12010-02-05 20:45:00 +0000519 // <template-template-param> ::= <template-param>
520 if (const TemplateTemplateParmDecl *TTP
521 = dyn_cast<TemplateTemplateParmDecl>(ND)) {
522 mangleTemplateParameter(TTP->getIndex());
523 return;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000524 }
Douglas Gregor32fb4e12010-02-05 20:45:00 +0000525
Anders Carlsson1668f202009-09-26 20:13:56 +0000526 mangleUnscopedName(ND->getTemplatedDecl());
Anders Carlsson7624f212009-09-18 02:42:01 +0000527 addSubstitution(ND);
Anders Carlsson201ce742009-09-17 03:17:01 +0000528}
529
Douglas Gregor1e9268e2010-04-28 05:58:56 +0000530void CXXNameMangler::mangleUnscopedTemplateName(TemplateName Template) {
531 // <unscoped-template-name> ::= <unscoped-name>
532 // ::= <substitution>
533 if (TemplateDecl *TD = Template.getAsTemplateDecl())
534 return mangleUnscopedTemplateName(TD);
Sean Huntc3021132010-05-05 15:23:54 +0000535
Douglas Gregor1e9268e2010-04-28 05:58:56 +0000536 if (mangleSubstitution(Template))
537 return;
538
539 // FIXME: How to cope with operators here?
540 DependentTemplateName *Dependent = Template.getAsDependentTemplateName();
541 assert(Dependent && "Not a dependent template name?");
542 if (!Dependent->isIdentifier()) {
543 // FIXME: We can't possibly know the arity of the operator here!
544 Diagnostic &Diags = Context.getDiags();
545 unsigned DiagID = Diags.getCustomDiagID(Diagnostic::Error,
546 "cannot mangle dependent operator name");
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000547 Diags.Report(DiagID);
Douglas Gregor1e9268e2010-04-28 05:58:56 +0000548 return;
549 }
Sean Huntc3021132010-05-05 15:23:54 +0000550
Douglas Gregor1e9268e2010-04-28 05:58:56 +0000551 mangleSourceName(Dependent->getIdentifier());
552 addSubstitution(Template);
553}
554
John McCall0512e482010-07-14 04:20:34 +0000555void CXXNameMangler::mangleFloat(const llvm::APFloat &F) {
556 // TODO: avoid this copy with careful stream management.
557 llvm::SmallString<20> Buffer;
558 F.bitcastToAPInt().toString(Buffer, 16, false);
559 Out.write(Buffer.data(), Buffer.size());
560}
561
562void CXXNameMangler::mangleNumber(const llvm::APSInt &Value) {
563 if (Value.isSigned() && Value.isNegative()) {
564 Out << 'n';
565 Value.abs().print(Out, true);
566 } else
567 Value.print(Out, Value.isSigned());
568}
569
Anders Carlssona94822e2009-11-26 02:32:05 +0000570void CXXNameMangler::mangleNumber(int64_t Number) {
571 // <number> ::= [n] <non-negative decimal integer>
572 if (Number < 0) {
573 Out << 'n';
574 Number = -Number;
575 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000576
Anders Carlssona94822e2009-11-26 02:32:05 +0000577 Out << Number;
578}
579
Anders Carlsson19879c92010-03-23 17:17:29 +0000580void CXXNameMangler::mangleCallOffset(int64_t NonVirtual, int64_t Virtual) {
Mike Stump141c5af2009-09-02 00:25:38 +0000581 // <call-offset> ::= h <nv-offset> _
582 // ::= v <v-offset> _
583 // <nv-offset> ::= <offset number> # non-virtual base override
Anders Carlssona94822e2009-11-26 02:32:05 +0000584 // <v-offset> ::= <offset number> _ <virtual offset number>
Mike Stump141c5af2009-09-02 00:25:38 +0000585 // # virtual base override, with vcall offset
Anders Carlsson19879c92010-03-23 17:17:29 +0000586 if (!Virtual) {
Anders Carlssona94822e2009-11-26 02:32:05 +0000587 Out << 'h';
Anders Carlsson19879c92010-03-23 17:17:29 +0000588 mangleNumber(NonVirtual);
Anders Carlssona94822e2009-11-26 02:32:05 +0000589 Out << '_';
590 return;
Mike Stump141c5af2009-09-02 00:25:38 +0000591 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000592
Anders Carlssona94822e2009-11-26 02:32:05 +0000593 Out << 'v';
Anders Carlsson19879c92010-03-23 17:17:29 +0000594 mangleNumber(NonVirtual);
Anders Carlssona94822e2009-11-26 02:32:05 +0000595 Out << '_';
Anders Carlsson19879c92010-03-23 17:17:29 +0000596 mangleNumber(Virtual);
Anders Carlssona94822e2009-11-26 02:32:05 +0000597 Out << '_';
Mike Stump9124bcc2009-09-02 00:56:18 +0000598}
599
John McCall1dd73832010-02-04 01:42:13 +0000600void CXXNameMangler::mangleUnresolvedScope(NestedNameSpecifier *Qualifier) {
601 Qualifier = getASTContext().getCanonicalNestedNameSpecifier(Qualifier);
602 switch (Qualifier->getKind()) {
603 case NestedNameSpecifier::Global:
604 // nothing
605 break;
606 case NestedNameSpecifier::Namespace:
607 mangleName(Qualifier->getAsNamespace());
608 break;
Douglas Gregor14aba762011-02-24 02:36:08 +0000609 case NestedNameSpecifier::NamespaceAlias:
610 mangleName(Qualifier->getAsNamespaceAlias()->getNamespace());
611 break;
John McCall1dd73832010-02-04 01:42:13 +0000612 case NestedNameSpecifier::TypeSpec:
Rafael Espindola9b35b252010-03-17 04:28:11 +0000613 case NestedNameSpecifier::TypeSpecWithTemplate: {
614 const Type *QTy = Qualifier->getAsType();
615
616 if (const TemplateSpecializationType *TST =
617 dyn_cast<TemplateSpecializationType>(QTy)) {
618 if (!mangleSubstitution(QualType(TST, 0))) {
Douglas Gregor20f0cc72010-04-23 03:10:43 +0000619 mangleTemplatePrefix(TST->getTemplateName());
Sean Huntc3021132010-05-05 15:23:54 +0000620
Douglas Gregor20f0cc72010-04-23 03:10:43 +0000621 // FIXME: GCC does not appear to mangle the template arguments when
622 // the template in question is a dependent template name. Should we
623 // emulate that badness?
624 mangleTemplateArgs(TST->getTemplateName(), TST->getArgs(),
Rafael Espindola9b35b252010-03-17 04:28:11 +0000625 TST->getNumArgs());
626 addSubstitution(QualType(TST, 0));
627 }
628 } else {
629 // We use the QualType mangle type variant here because it handles
630 // substitutions.
631 mangleType(QualType(QTy, 0));
632 }
633 }
John McCall1dd73832010-02-04 01:42:13 +0000634 break;
635 case NestedNameSpecifier::Identifier:
John McCallad5e7382010-03-01 23:49:17 +0000636 // Member expressions can have these without prefixes.
637 if (Qualifier->getPrefix())
638 mangleUnresolvedScope(Qualifier->getPrefix());
John McCall1dd73832010-02-04 01:42:13 +0000639 mangleSourceName(Qualifier->getAsIdentifier());
640 break;
641 }
642}
643
644/// Mangles a name which was not resolved to a specific entity.
645void CXXNameMangler::mangleUnresolvedName(NestedNameSpecifier *Qualifier,
646 DeclarationName Name,
647 unsigned KnownArity) {
648 if (Qualifier)
649 mangleUnresolvedScope(Qualifier);
650 // FIXME: ambiguity of unqualified lookup with ::
651
652 mangleUnqualifiedName(0, Name, KnownArity);
653}
654
Anders Carlsson6f7e2f42010-06-08 14:49:03 +0000655static const FieldDecl *FindFirstNamedDataMember(const RecordDecl *RD) {
656 assert(RD->isAnonymousStructOrUnion() &&
657 "Expected anonymous struct or union!");
658
659 for (RecordDecl::field_iterator I = RD->field_begin(), E = RD->field_end();
660 I != E; ++I) {
661 const FieldDecl *FD = *I;
662
663 if (FD->getIdentifier())
664 return FD;
665
666 if (const RecordType *RT = FD->getType()->getAs<RecordType>()) {
667 if (const FieldDecl *NamedDataMember =
668 FindFirstNamedDataMember(RT->getDecl()))
669 return NamedDataMember;
670 }
671 }
672
673 // We didn't find a named data member.
674 return 0;
675}
676
John McCall1dd73832010-02-04 01:42:13 +0000677void CXXNameMangler::mangleUnqualifiedName(const NamedDecl *ND,
678 DeclarationName Name,
679 unsigned KnownArity) {
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000680 // <unqualified-name> ::= <operator-name>
Mike Stump1eb44332009-09-09 15:08:12 +0000681 // ::= <ctor-dtor-name>
682 // ::= <source-name>
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000683 switch (Name.getNameKind()) {
Anders Carlssonc4355b62009-10-07 01:45:02 +0000684 case DeclarationName::Identifier: {
Anders Carlssonc4355b62009-10-07 01:45:02 +0000685 if (const IdentifierInfo *II = Name.getAsIdentifierInfo()) {
Sean Hunt31455252010-01-24 03:04:27 +0000686 // We must avoid conflicts between internally- and externally-
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000687 // linked variable declaration names in the same TU.
Anders Carlssonaec25232010-02-06 04:52:27 +0000688 // This naming convention is the same as that followed by GCC, though it
689 // shouldn't actually matter.
690 if (ND && isa<VarDecl>(ND) && ND->getLinkage() == InternalLinkage &&
Sean Hunt31455252010-01-24 03:04:27 +0000691 ND->getDeclContext()->isFileContext())
692 Out << 'L';
693
Anders Carlssonc4355b62009-10-07 01:45:02 +0000694 mangleSourceName(II);
695 break;
696 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000697
John McCall1dd73832010-02-04 01:42:13 +0000698 // Otherwise, an anonymous entity. We must have a declaration.
699 assert(ND && "mangling empty name without declaration");
700
701 if (const NamespaceDecl *NS = dyn_cast<NamespaceDecl>(ND)) {
702 if (NS->isAnonymousNamespace()) {
703 // This is how gcc mangles these names.
704 Out << "12_GLOBAL__N_1";
705 break;
706 }
707 }
708
Anders Carlsson6f7e2f42010-06-08 14:49:03 +0000709 if (const VarDecl *VD = dyn_cast<VarDecl>(ND)) {
710 // We must have an anonymous union or struct declaration.
711 const RecordDecl *RD =
712 cast<RecordDecl>(VD->getType()->getAs<RecordType>()->getDecl());
713
714 // Itanium C++ ABI 5.1.2:
715 //
716 // For the purposes of mangling, the name of an anonymous union is
717 // considered to be the name of the first named data member found by a
718 // pre-order, depth-first, declaration-order walk of the data members of
719 // the anonymous union. If there is no such data member (i.e., if all of
720 // the data members in the union are unnamed), then there is no way for
721 // a program to refer to the anonymous union, and there is therefore no
722 // need to mangle its name.
723 const FieldDecl *FD = FindFirstNamedDataMember(RD);
John McCall7121c8f2010-08-05 22:02:13 +0000724
725 // It's actually possible for various reasons for us to get here
726 // with an empty anonymous struct / union. Fortunately, it
727 // doesn't really matter what name we generate.
728 if (!FD) break;
Anders Carlsson6f7e2f42010-06-08 14:49:03 +0000729 assert(FD->getIdentifier() && "Data member name isn't an identifier!");
730
731 mangleSourceName(FD->getIdentifier());
732 break;
733 }
734
Anders Carlssonc4355b62009-10-07 01:45:02 +0000735 // We must have an anonymous struct.
736 const TagDecl *TD = cast<TagDecl>(ND);
737 if (const TypedefDecl *D = TD->getTypedefForAnonDecl()) {
738 assert(TD->getDeclContext() == D->getDeclContext() &&
739 "Typedef should not be in another decl context!");
740 assert(D->getDeclName().getAsIdentifierInfo() &&
741 "Typedef was not named!");
742 mangleSourceName(D->getDeclName().getAsIdentifierInfo());
743 break;
744 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000745
Anders Carlssonc4355b62009-10-07 01:45:02 +0000746 // Get a unique id for the anonymous struct.
747 uint64_t AnonStructId = Context.getAnonymousStructId(TD);
748
749 // Mangle it as a source name in the form
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000750 // [n] $_<id>
Anders Carlssonc4355b62009-10-07 01:45:02 +0000751 // where n is the length of the string.
752 llvm::SmallString<8> Str;
753 Str += "$_";
754 Str += llvm::utostr(AnonStructId);
755
756 Out << Str.size();
757 Out << Str.str();
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000758 break;
Anders Carlssonc4355b62009-10-07 01:45:02 +0000759 }
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000760
761 case DeclarationName::ObjCZeroArgSelector:
762 case DeclarationName::ObjCOneArgSelector:
763 case DeclarationName::ObjCMultiArgSelector:
764 assert(false && "Can't mangle Objective-C selector names here!");
765 break;
766
767 case DeclarationName::CXXConstructorName:
Anders Carlsson27ae5362009-04-17 01:58:57 +0000768 if (ND == Structor)
Mike Stump141c5af2009-09-02 00:25:38 +0000769 // If the named decl is the C++ constructor we're mangling, use the type
770 // we were given.
Anders Carlsson27ae5362009-04-17 01:58:57 +0000771 mangleCXXCtorType(static_cast<CXXCtorType>(StructorType));
Anders Carlsson3ac86b52009-04-15 05:36:58 +0000772 else
773 // Otherwise, use the complete constructor name. This is relevant if a
774 // class with a constructor is declared within a constructor.
775 mangleCXXCtorType(Ctor_Complete);
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000776 break;
777
778 case DeclarationName::CXXDestructorName:
Anders Carlsson27ae5362009-04-17 01:58:57 +0000779 if (ND == Structor)
Mike Stump141c5af2009-09-02 00:25:38 +0000780 // If the named decl is the C++ destructor we're mangling, use the type we
781 // were given.
Anders Carlsson27ae5362009-04-17 01:58:57 +0000782 mangleCXXDtorType(static_cast<CXXDtorType>(StructorType));
783 else
784 // Otherwise, use the complete destructor name. This is relevant if a
785 // class with a destructor is declared within a destructor.
786 mangleCXXDtorType(Dtor_Complete);
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000787 break;
788
789 case DeclarationName::CXXConversionFunctionName:
Mike Stump1eb44332009-09-09 15:08:12 +0000790 // <operator-name> ::= cv <type> # (cast)
Douglas Gregor219cc612009-02-13 01:28:03 +0000791 Out << "cv";
Anders Carlssonb5404912009-10-07 01:06:45 +0000792 mangleType(Context.getASTContext().getCanonicalType(Name.getCXXNameType()));
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000793 break;
794
Anders Carlsson8257d412009-12-22 06:36:32 +0000795 case DeclarationName::CXXOperatorName: {
John McCall1dd73832010-02-04 01:42:13 +0000796 unsigned Arity;
797 if (ND) {
798 Arity = cast<FunctionDecl>(ND)->getNumParams();
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000799
John McCall1dd73832010-02-04 01:42:13 +0000800 // If we have a C++ member function, we need to include the 'this' pointer.
801 // FIXME: This does not make sense for operators that are static, but their
802 // names stay the same regardless of the arity (operator new for instance).
803 if (isa<CXXMethodDecl>(ND))
804 Arity++;
805 } else
806 Arity = KnownArity;
807
Anders Carlsson8257d412009-12-22 06:36:32 +0000808 mangleOperatorName(Name.getCXXOverloadedOperator(), Arity);
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000809 break;
Anders Carlsson8257d412009-12-22 06:36:32 +0000810 }
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000811
Sean Hunt3e518bd2009-11-29 07:34:05 +0000812 case DeclarationName::CXXLiteralOperatorName:
Sean Hunt5dd6b392009-12-04 21:11:13 +0000813 // FIXME: This mangling is not yet official.
Sean Hunt2421f662009-12-04 21:01:37 +0000814 Out << "li";
Sean Hunt3e518bd2009-11-29 07:34:05 +0000815 mangleSourceName(Name.getCXXLiteralIdentifier());
816 break;
817
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000818 case DeclarationName::CXXUsingDirective:
819 assert(false && "Can't mangle a using directive name!");
Douglas Gregor219cc612009-02-13 01:28:03 +0000820 break;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000821 }
822}
823
824void CXXNameMangler::mangleSourceName(const IdentifierInfo *II) {
825 // <source-name> ::= <positive length number> <identifier>
826 // <number> ::= [n] <non-negative decimal integer>
827 // <identifier> ::= <unqualified source code identifier>
828 Out << II->getLength() << II->getName();
829}
830
Eli Friedman7facf842009-12-02 20:32:49 +0000831void CXXNameMangler::mangleNestedName(const NamedDecl *ND,
Fariborz Jahanian57058532010-03-03 19:41:08 +0000832 const DeclContext *DC,
833 bool NoFunction) {
Douglas Gregor0a9a6d62011-01-26 17:36:28 +0000834 // <nested-name>
835 // ::= N [<CV-qualifiers>] [<ref-qualifier>] <prefix> <unqualified-name> E
836 // ::= N [<CV-qualifiers>] [<ref-qualifier>] <template-prefix>
837 // <template-args> E
Anders Carlssond99edc42009-09-26 03:55:37 +0000838
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000839 Out << 'N';
Douglas Gregor0a9a6d62011-01-26 17:36:28 +0000840 if (const CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(ND)) {
John McCall0953e762009-09-24 19:53:00 +0000841 mangleQualifiers(Qualifiers::fromCVRMask(Method->getTypeQualifiers()));
Douglas Gregor0a9a6d62011-01-26 17:36:28 +0000842 mangleRefQualifier(Method->getRefQualifier());
843 }
844
Anders Carlsson2744a062009-09-18 19:00:18 +0000845 // Check if we have a template.
846 const TemplateArgumentList *TemplateArgs = 0;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000847 if (const TemplateDecl *TD = isTemplate(ND, TemplateArgs)) {
Anders Carlsson2744a062009-09-18 19:00:18 +0000848 mangleTemplatePrefix(TD);
Rafael Espindolad9800722010-03-11 14:07:00 +0000849 TemplateParameterList *TemplateParameters = TD->getTemplateParameters();
850 mangleTemplateArgs(*TemplateParameters, *TemplateArgs);
Fariborz Jahanian57058532010-03-03 19:41:08 +0000851 }
852 else {
853 manglePrefix(DC, NoFunction);
Anders Carlsson7482e242009-09-18 04:29:09 +0000854 mangleUnqualifiedName(ND);
855 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000856
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000857 Out << 'E';
858}
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000859void CXXNameMangler::mangleNestedName(const TemplateDecl *TD,
Anders Carlsson7624f212009-09-18 02:42:01 +0000860 const TemplateArgument *TemplateArgs,
861 unsigned NumTemplateArgs) {
Anders Carlssone45117b2009-09-27 19:53:49 +0000862 // <nested-name> ::= N [<CV-qualifiers>] <template-prefix> <template-args> E
863
Anders Carlsson7624f212009-09-18 02:42:01 +0000864 Out << 'N';
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000865
Anders Carlssone45117b2009-09-27 19:53:49 +0000866 mangleTemplatePrefix(TD);
Rafael Espindolad9800722010-03-11 14:07:00 +0000867 TemplateParameterList *TemplateParameters = TD->getTemplateParameters();
868 mangleTemplateArgs(*TemplateParameters, TemplateArgs, NumTemplateArgs);
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000869
Anders Carlsson7624f212009-09-18 02:42:01 +0000870 Out << 'E';
871}
872
Anders Carlsson1b42c792009-04-02 16:24:45 +0000873void CXXNameMangler::mangleLocalName(const NamedDecl *ND) {
874 // <local-name> := Z <function encoding> E <entity name> [<discriminator>]
875 // := Z <function encoding> E s [<discriminator>]
Mike Stump1eb44332009-09-09 15:08:12 +0000876 // <discriminator> := _ <non-negative number>
Fariborz Jahanian57058532010-03-03 19:41:08 +0000877 const DeclContext *DC = ND->getDeclContext();
Anders Carlsson1b42c792009-04-02 16:24:45 +0000878 Out << 'Z';
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000879
Charles Davis685b1d92010-05-26 18:25:27 +0000880 if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(DC)) {
881 mangleObjCMethodName(MD);
John McCall82b7d7b2010-10-18 21:28:44 +0000882 } else if (const CXXRecordDecl *RD = GetLocalClassDecl(ND)) {
883 mangleFunctionEncoding(cast<FunctionDecl>(RD->getDeclContext()));
Fariborz Jahanian57058532010-03-03 19:41:08 +0000884 Out << 'E';
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000885
John McCall82b7d7b2010-10-18 21:28:44 +0000886 // Mangle the name relative to the closest enclosing function.
887 if (ND == RD) // equality ok because RD derived from ND above
888 mangleUnqualifiedName(ND);
889 else
890 mangleNestedName(ND, DC, true /*NoFunction*/);
891
Fariborz Jahanian4819ac42010-03-04 01:02:03 +0000892 unsigned disc;
John McCall82b7d7b2010-10-18 21:28:44 +0000893 if (Context.getNextDiscriminator(RD, disc)) {
Fariborz Jahanian4819ac42010-03-04 01:02:03 +0000894 if (disc < 10)
895 Out << '_' << disc;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000896 else
Fariborz Jahanian4819ac42010-03-04 01:02:03 +0000897 Out << "__" << disc << '_';
898 }
Fariborz Jahanian57058532010-03-03 19:41:08 +0000899
900 return;
901 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000902 else
Fariborz Jahanian57058532010-03-03 19:41:08 +0000903 mangleFunctionEncoding(cast<FunctionDecl>(DC));
Anders Carlsson7b06f6c2009-12-10 03:14:39 +0000904
Anders Carlsson1b42c792009-04-02 16:24:45 +0000905 Out << 'E';
Eli Friedman6f9f25d2009-12-11 20:21:38 +0000906 mangleUnqualifiedName(ND);
Anders Carlsson1b42c792009-04-02 16:24:45 +0000907}
908
Fariborz Jahanian57058532010-03-03 19:41:08 +0000909void CXXNameMangler::manglePrefix(const DeclContext *DC, bool NoFunction) {
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000910 // <prefix> ::= <prefix> <unqualified-name>
911 // ::= <template-prefix> <template-args>
912 // ::= <template-param>
913 // ::= # empty
914 // ::= <substitution>
Anders Carlsson6862fc72009-09-17 04:16:28 +0000915
Anders Carlssonadd28822009-09-22 20:33:31 +0000916 while (isa<LinkageSpecDecl>(DC))
917 DC = DC->getParent();
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000918
Anders Carlsson9263e912009-09-18 18:39:58 +0000919 if (DC->isTranslationUnit())
920 return;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000921
Douglas Gregor35415f52010-05-25 17:04:15 +0000922 if (const BlockDecl *Block = dyn_cast<BlockDecl>(DC)) {
923 manglePrefix(DC->getParent(), NoFunction);
924 llvm::SmallString<64> Name;
Rafael Espindolac4850c22011-02-10 23:59:36 +0000925 llvm::raw_svector_ostream NameStream(Name);
926 Context.mangleBlock(Block, NameStream);
927 NameStream.flush();
Douglas Gregor35415f52010-05-25 17:04:15 +0000928 Out << Name.size() << Name;
929 return;
930 }
931
Anders Carlsson6862fc72009-09-17 04:16:28 +0000932 if (mangleSubstitution(cast<NamedDecl>(DC)))
933 return;
Anders Carlsson7482e242009-09-18 04:29:09 +0000934
Anders Carlsson2ee3fca2009-09-18 20:11:09 +0000935 // Check if we have a template.
936 const TemplateArgumentList *TemplateArgs = 0;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000937 if (const TemplateDecl *TD = isTemplate(cast<NamedDecl>(DC), TemplateArgs)) {
Anders Carlsson2ee3fca2009-09-18 20:11:09 +0000938 mangleTemplatePrefix(TD);
Rafael Espindolad9800722010-03-11 14:07:00 +0000939 TemplateParameterList *TemplateParameters = TD->getTemplateParameters();
940 mangleTemplateArgs(*TemplateParameters, *TemplateArgs);
Fariborz Jahanian57058532010-03-03 19:41:08 +0000941 }
Douglas Gregor35415f52010-05-25 17:04:15 +0000942 else if(NoFunction && (isa<FunctionDecl>(DC) || isa<ObjCMethodDecl>(DC)))
Fariborz Jahanian57058532010-03-03 19:41:08 +0000943 return;
Douglas Gregor35415f52010-05-25 17:04:15 +0000944 else if (const ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(DC))
945 mangleObjCMethodName(Method);
Fariborz Jahanian57058532010-03-03 19:41:08 +0000946 else {
947 manglePrefix(DC->getParent(), NoFunction);
Anders Carlsson2ee3fca2009-09-18 20:11:09 +0000948 mangleUnqualifiedName(cast<NamedDecl>(DC));
949 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000950
Anders Carlsson6862fc72009-09-17 04:16:28 +0000951 addSubstitution(cast<NamedDecl>(DC));
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000952}
953
Douglas Gregor20f0cc72010-04-23 03:10:43 +0000954void CXXNameMangler::mangleTemplatePrefix(TemplateName Template) {
955 // <template-prefix> ::= <prefix> <template unqualified-name>
956 // ::= <template-param>
957 // ::= <substitution>
958 if (TemplateDecl *TD = Template.getAsTemplateDecl())
959 return mangleTemplatePrefix(TD);
960
961 if (QualifiedTemplateName *Qualified = Template.getAsQualifiedTemplateName())
962 mangleUnresolvedScope(Qualified->getQualifier());
Sean Huntc3021132010-05-05 15:23:54 +0000963
Douglas Gregor20f0cc72010-04-23 03:10:43 +0000964 if (OverloadedTemplateStorage *Overloaded
965 = Template.getAsOverloadedTemplate()) {
Sean Huntc3021132010-05-05 15:23:54 +0000966 mangleUnqualifiedName(0, (*Overloaded->begin())->getDeclName(),
Douglas Gregor20f0cc72010-04-23 03:10:43 +0000967 UnknownArity);
968 return;
969 }
Sean Huntc3021132010-05-05 15:23:54 +0000970
Douglas Gregor20f0cc72010-04-23 03:10:43 +0000971 DependentTemplateName *Dependent = Template.getAsDependentTemplateName();
972 assert(Dependent && "Unknown template name kind?");
973 mangleUnresolvedScope(Dependent->getQualifier());
Douglas Gregor1e9268e2010-04-28 05:58:56 +0000974 mangleUnscopedTemplateName(Template);
Douglas Gregor20f0cc72010-04-23 03:10:43 +0000975}
976
Anders Carlsson0fa6df42009-09-26 19:45:45 +0000977void CXXNameMangler::mangleTemplatePrefix(const TemplateDecl *ND) {
Anders Carlsson7482e242009-09-18 04:29:09 +0000978 // <template-prefix> ::= <prefix> <template unqualified-name>
979 // ::= <template-param>
980 // ::= <substitution>
Douglas Gregor32fb4e12010-02-05 20:45:00 +0000981 // <template-template-param> ::= <template-param>
982 // <substitution>
Anders Carlsson7482e242009-09-18 04:29:09 +0000983
Anders Carlssonaeb85372009-09-26 22:18:22 +0000984 if (mangleSubstitution(ND))
985 return;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000986
Douglas Gregor32fb4e12010-02-05 20:45:00 +0000987 // <template-template-param> ::= <template-param>
988 if (const TemplateTemplateParmDecl *TTP
989 = dyn_cast<TemplateTemplateParmDecl>(ND)) {
990 mangleTemplateParameter(TTP->getIndex());
991 return;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000992 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000993
Anders Carlssonaa73ab12009-09-18 18:47:07 +0000994 manglePrefix(ND->getDeclContext());
Anders Carlsson1668f202009-09-26 20:13:56 +0000995 mangleUnqualifiedName(ND->getTemplatedDecl());
Anders Carlssonaeb85372009-09-26 22:18:22 +0000996 addSubstitution(ND);
Anders Carlsson7482e242009-09-18 04:29:09 +0000997}
998
John McCallb6f532e2010-07-14 06:43:17 +0000999/// Mangles a template name under the production <type>. Required for
1000/// template template arguments.
1001/// <type> ::= <class-enum-type>
1002/// ::= <template-param>
1003/// ::= <substitution>
1004void CXXNameMangler::mangleType(TemplateName TN) {
1005 if (mangleSubstitution(TN))
1006 return;
1007
1008 TemplateDecl *TD = 0;
1009
1010 switch (TN.getKind()) {
1011 case TemplateName::QualifiedTemplate:
1012 TD = TN.getAsQualifiedTemplateName()->getTemplateDecl();
1013 goto HaveDecl;
1014
1015 case TemplateName::Template:
1016 TD = TN.getAsTemplateDecl();
1017 goto HaveDecl;
1018
1019 HaveDecl:
1020 if (isa<TemplateTemplateParmDecl>(TD))
1021 mangleTemplateParameter(cast<TemplateTemplateParmDecl>(TD)->getIndex());
1022 else
1023 mangleName(TD);
1024 break;
1025
1026 case TemplateName::OverloadedTemplate:
1027 llvm_unreachable("can't mangle an overloaded template name as a <type>");
1028 break;
1029
1030 case TemplateName::DependentTemplate: {
1031 const DependentTemplateName *Dependent = TN.getAsDependentTemplateName();
1032 assert(Dependent->isIdentifier());
1033
1034 // <class-enum-type> ::= <name>
1035 // <name> ::= <nested-name>
1036 mangleUnresolvedScope(Dependent->getQualifier());
1037 mangleSourceName(Dependent->getIdentifier());
1038 break;
1039 }
1040
Douglas Gregor1aee05d2011-01-15 06:45:20 +00001041 case TemplateName::SubstTemplateTemplateParmPack: {
1042 SubstTemplateTemplateParmPackStorage *SubstPack
1043 = TN.getAsSubstTemplateTemplateParmPack();
1044 mangleTemplateParameter(SubstPack->getParameterPack()->getIndex());
1045 break;
1046 }
John McCallb6f532e2010-07-14 06:43:17 +00001047 }
1048
1049 addSubstitution(TN);
1050}
1051
Mike Stump1eb44332009-09-09 15:08:12 +00001052void
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001053CXXNameMangler::mangleOperatorName(OverloadedOperatorKind OO, unsigned Arity) {
1054 switch (OO) {
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001055 // <operator-name> ::= nw # new
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001056 case OO_New: Out << "nw"; break;
1057 // ::= na # new[]
1058 case OO_Array_New: Out << "na"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001059 // ::= dl # delete
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001060 case OO_Delete: Out << "dl"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001061 // ::= da # delete[]
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001062 case OO_Array_Delete: Out << "da"; break;
1063 // ::= ps # + (unary)
John McCall5e1e89b2010-08-18 19:18:59 +00001064 // ::= pl # + (binary or unknown)
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001065 case OO_Plus:
Anders Carlsson8257d412009-12-22 06:36:32 +00001066 Out << (Arity == 1? "ps" : "pl"); break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001067 // ::= ng # - (unary)
John McCall5e1e89b2010-08-18 19:18:59 +00001068 // ::= mi # - (binary or unknown)
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001069 case OO_Minus:
Anders Carlsson8257d412009-12-22 06:36:32 +00001070 Out << (Arity == 1? "ng" : "mi"); break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001071 // ::= ad # & (unary)
John McCall5e1e89b2010-08-18 19:18:59 +00001072 // ::= an # & (binary or unknown)
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001073 case OO_Amp:
Anders Carlsson8257d412009-12-22 06:36:32 +00001074 Out << (Arity == 1? "ad" : "an"); break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001075 // ::= de # * (unary)
John McCall5e1e89b2010-08-18 19:18:59 +00001076 // ::= ml # * (binary or unknown)
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001077 case OO_Star:
John McCall5e1e89b2010-08-18 19:18:59 +00001078 // Use binary when unknown.
Anders Carlsson8257d412009-12-22 06:36:32 +00001079 Out << (Arity == 1? "de" : "ml"); break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001080 // ::= co # ~
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001081 case OO_Tilde: Out << "co"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001082 // ::= dv # /
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001083 case OO_Slash: Out << "dv"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001084 // ::= rm # %
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001085 case OO_Percent: Out << "rm"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001086 // ::= or # |
1087 case OO_Pipe: Out << "or"; break;
1088 // ::= eo # ^
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001089 case OO_Caret: Out << "eo"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001090 // ::= aS # =
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001091 case OO_Equal: Out << "aS"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001092 // ::= pL # +=
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001093 case OO_PlusEqual: Out << "pL"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001094 // ::= mI # -=
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001095 case OO_MinusEqual: Out << "mI"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001096 // ::= mL # *=
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001097 case OO_StarEqual: Out << "mL"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001098 // ::= dV # /=
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001099 case OO_SlashEqual: Out << "dV"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001100 // ::= rM # %=
1101 case OO_PercentEqual: Out << "rM"; break;
1102 // ::= aN # &=
1103 case OO_AmpEqual: Out << "aN"; break;
1104 // ::= oR # |=
1105 case OO_PipeEqual: Out << "oR"; break;
1106 // ::= eO # ^=
1107 case OO_CaretEqual: Out << "eO"; break;
1108 // ::= ls # <<
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001109 case OO_LessLess: Out << "ls"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001110 // ::= rs # >>
1111 case OO_GreaterGreater: Out << "rs"; break;
1112 // ::= lS # <<=
1113 case OO_LessLessEqual: Out << "lS"; break;
1114 // ::= rS # >>=
1115 case OO_GreaterGreaterEqual: Out << "rS"; break;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001116 // ::= eq # ==
1117 case OO_EqualEqual: Out << "eq"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001118 // ::= ne # !=
1119 case OO_ExclaimEqual: Out << "ne"; break;
1120 // ::= lt # <
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001121 case OO_Less: Out << "lt"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001122 // ::= gt # >
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001123 case OO_Greater: Out << "gt"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001124 // ::= le # <=
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001125 case OO_LessEqual: Out << "le"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001126 // ::= ge # >=
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001127 case OO_GreaterEqual: Out << "ge"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001128 // ::= nt # !
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001129 case OO_Exclaim: Out << "nt"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001130 // ::= aa # &&
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001131 case OO_AmpAmp: Out << "aa"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001132 // ::= oo # ||
1133 case OO_PipePipe: Out << "oo"; break;
1134 // ::= pp # ++
1135 case OO_PlusPlus: Out << "pp"; break;
1136 // ::= mm # --
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001137 case OO_MinusMinus: Out << "mm"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001138 // ::= cm # ,
1139 case OO_Comma: Out << "cm"; break;
1140 // ::= pm # ->*
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001141 case OO_ArrowStar: Out << "pm"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001142 // ::= pt # ->
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001143 case OO_Arrow: Out << "pt"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001144 // ::= cl # ()
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001145 case OO_Call: Out << "cl"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001146 // ::= ix # []
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001147 case OO_Subscript: Out << "ix"; break;
Anders Carlssone170ba72009-12-14 01:45:37 +00001148
1149 // ::= qu # ?
1150 // The conditional operator can't be overloaded, but we still handle it when
1151 // mangling expressions.
1152 case OO_Conditional: Out << "qu"; break;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001153
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001154 case OO_None:
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001155 case NUM_OVERLOADED_OPERATORS:
Mike Stump1eb44332009-09-09 15:08:12 +00001156 assert(false && "Not an overloaded operator");
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001157 break;
1158 }
1159}
1160
John McCall0953e762009-09-24 19:53:00 +00001161void CXXNameMangler::mangleQualifiers(Qualifiers Quals) {
Mike Stump1eb44332009-09-09 15:08:12 +00001162 // <CV-qualifiers> ::= [r] [V] [K] # restrict (C99), volatile, const
John McCall0953e762009-09-24 19:53:00 +00001163 if (Quals.hasRestrict())
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001164 Out << 'r';
John McCall0953e762009-09-24 19:53:00 +00001165 if (Quals.hasVolatile())
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001166 Out << 'V';
John McCall0953e762009-09-24 19:53:00 +00001167 if (Quals.hasConst())
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001168 Out << 'K';
John McCall0953e762009-09-24 19:53:00 +00001169
Douglas Gregor56079f72010-06-14 23:15:08 +00001170 if (Quals.hasAddressSpace()) {
1171 // Extension:
1172 //
1173 // <type> ::= U <address-space-number>
1174 //
1175 // where <address-space-number> is a source name consisting of 'AS'
1176 // followed by the address space <number>.
1177 llvm::SmallString<64> ASString;
1178 ASString = "AS" + llvm::utostr_32(Quals.getAddressSpace());
1179 Out << 'U' << ASString.size() << ASString;
1180 }
1181
John McCall0953e762009-09-24 19:53:00 +00001182 // FIXME: For now, just drop all extension qualifiers on the floor.
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001183}
1184
Douglas Gregor0a9a6d62011-01-26 17:36:28 +00001185void CXXNameMangler::mangleRefQualifier(RefQualifierKind RefQualifier) {
1186 // <ref-qualifier> ::= R # lvalue reference
1187 // ::= O # rvalue-reference
1188 // Proposal to Itanium C++ ABI list on 1/26/11
1189 switch (RefQualifier) {
1190 case RQ_None:
1191 break;
1192
1193 case RQ_LValue:
1194 Out << 'R';
1195 break;
1196
1197 case RQ_RValue:
1198 Out << 'O';
1199 break;
1200 }
1201}
1202
Anders Carlsson7b06f6c2009-12-10 03:14:39 +00001203void CXXNameMangler::mangleObjCMethodName(const ObjCMethodDecl *MD) {
Rafael Espindolaf0be9792011-02-11 02:52:17 +00001204 Context.mangleObjCMethodName(MD, Out);
Anders Carlsson7b06f6c2009-12-10 03:14:39 +00001205}
1206
John McCallb47f7482011-01-26 20:05:40 +00001207void CXXNameMangler::mangleType(QualType nonCanon) {
Anders Carlsson4843e582009-03-10 17:07:44 +00001208 // Only operate on the canonical type!
John McCallb47f7482011-01-26 20:05:40 +00001209 QualType canon = nonCanon.getCanonicalType();
Anders Carlsson4843e582009-03-10 17:07:44 +00001210
John McCallb47f7482011-01-26 20:05:40 +00001211 SplitQualType split = canon.split();
1212 Qualifiers quals = split.second;
1213 const Type *ty = split.first;
1214
1215 bool isSubstitutable = quals || !isa<BuiltinType>(ty);
1216 if (isSubstitutable && mangleSubstitution(canon))
Anders Carlsson76967372009-09-17 00:43:46 +00001217 return;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001218
John McCallb47f7482011-01-26 20:05:40 +00001219 // If we're mangling a qualified array type, push the qualifiers to
1220 // the element type.
1221 if (quals && isa<ArrayType>(ty)) {
1222 ty = Context.getASTContext().getAsArrayType(canon);
1223 quals = Qualifiers();
1224
1225 // Note that we don't update canon: we want to add the
1226 // substitution at the canonical type.
1227 }
1228
1229 if (quals) {
1230 mangleQualifiers(quals);
John McCall0953e762009-09-24 19:53:00 +00001231 // Recurse: even if the qualified type isn't yet substitutable,
1232 // the unqualified type might be.
John McCallb47f7482011-01-26 20:05:40 +00001233 mangleType(QualType(ty, 0));
Anders Carlsson76967372009-09-17 00:43:46 +00001234 } else {
John McCallb47f7482011-01-26 20:05:40 +00001235 switch (ty->getTypeClass()) {
John McCallefe6aee2009-09-05 07:56:18 +00001236#define ABSTRACT_TYPE(CLASS, PARENT)
1237#define NON_CANONICAL_TYPE(CLASS, PARENT) \
Anders Carlsson76967372009-09-17 00:43:46 +00001238 case Type::CLASS: \
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +00001239 llvm_unreachable("can't mangle non-canonical type " #CLASS "Type"); \
Anders Carlsson76967372009-09-17 00:43:46 +00001240 return;
John McCallefe6aee2009-09-05 07:56:18 +00001241#define TYPE(CLASS, PARENT) \
Anders Carlsson76967372009-09-17 00:43:46 +00001242 case Type::CLASS: \
John McCallb47f7482011-01-26 20:05:40 +00001243 mangleType(static_cast<const CLASS##Type*>(ty)); \
Anders Carlsson76967372009-09-17 00:43:46 +00001244 break;
John McCallefe6aee2009-09-05 07:56:18 +00001245#include "clang/AST/TypeNodes.def"
Anders Carlsson76967372009-09-17 00:43:46 +00001246 }
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001247 }
Anders Carlsson76967372009-09-17 00:43:46 +00001248
1249 // Add the substitution.
John McCallb47f7482011-01-26 20:05:40 +00001250 if (isSubstitutable)
1251 addSubstitution(canon);
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001252}
1253
Douglas Gregor1b12a3b2010-05-26 05:11:13 +00001254void CXXNameMangler::mangleNameOrStandardSubstitution(const NamedDecl *ND) {
1255 if (!mangleStandardSubstitution(ND))
1256 mangleName(ND);
1257}
1258
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001259void CXXNameMangler::mangleType(const BuiltinType *T) {
John McCallefe6aee2009-09-05 07:56:18 +00001260 // <type> ::= <builtin-type>
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001261 // <builtin-type> ::= v # void
1262 // ::= w # wchar_t
1263 // ::= b # bool
1264 // ::= c # char
1265 // ::= a # signed char
1266 // ::= h # unsigned char
1267 // ::= s # short
1268 // ::= t # unsigned short
1269 // ::= i # int
1270 // ::= j # unsigned int
1271 // ::= l # long
1272 // ::= m # unsigned long
1273 // ::= x # long long, __int64
1274 // ::= y # unsigned long long, __int64
1275 // ::= n # __int128
1276 // UNSUPPORTED: ::= o # unsigned __int128
1277 // ::= f # float
1278 // ::= d # double
1279 // ::= e # long double, __float80
1280 // UNSUPPORTED: ::= g # __float128
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001281 // UNSUPPORTED: ::= Dd # IEEE 754r decimal floating point (64 bits)
1282 // UNSUPPORTED: ::= De # IEEE 754r decimal floating point (128 bits)
1283 // UNSUPPORTED: ::= Df # IEEE 754r decimal floating point (32 bits)
1284 // UNSUPPORTED: ::= Dh # IEEE 754r half-precision floating point (16 bits)
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001285 // ::= Di # char32_t
1286 // ::= Ds # char16_t
Anders Carlssone2923682010-11-04 04:31:32 +00001287 // ::= Dn # std::nullptr_t (i.e., decltype(nullptr))
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001288 // ::= u <source-name> # vendor extended type
1289 switch (T->getKind()) {
1290 case BuiltinType::Void: Out << 'v'; break;
1291 case BuiltinType::Bool: Out << 'b'; break;
1292 case BuiltinType::Char_U: case BuiltinType::Char_S: Out << 'c'; break;
1293 case BuiltinType::UChar: Out << 'h'; break;
1294 case BuiltinType::UShort: Out << 't'; break;
1295 case BuiltinType::UInt: Out << 'j'; break;
1296 case BuiltinType::ULong: Out << 'm'; break;
1297 case BuiltinType::ULongLong: Out << 'y'; break;
Chris Lattner2df9ced2009-04-30 02:43:43 +00001298 case BuiltinType::UInt128: Out << 'o'; break;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001299 case BuiltinType::SChar: Out << 'a'; break;
Chris Lattner3f59c972010-12-25 23:25:43 +00001300 case BuiltinType::WChar_S:
1301 case BuiltinType::WChar_U: Out << 'w'; break;
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001302 case BuiltinType::Char16: Out << "Ds"; break;
1303 case BuiltinType::Char32: Out << "Di"; break;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001304 case BuiltinType::Short: Out << 's'; break;
1305 case BuiltinType::Int: Out << 'i'; break;
1306 case BuiltinType::Long: Out << 'l'; break;
1307 case BuiltinType::LongLong: Out << 'x'; break;
Chris Lattner2df9ced2009-04-30 02:43:43 +00001308 case BuiltinType::Int128: Out << 'n'; break;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001309 case BuiltinType::Float: Out << 'f'; break;
1310 case BuiltinType::Double: Out << 'd'; break;
1311 case BuiltinType::LongDouble: Out << 'e'; break;
Anders Carlssone2923682010-11-04 04:31:32 +00001312 case BuiltinType::NullPtr: Out << "Dn"; break;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001313
1314 case BuiltinType::Overload:
1315 case BuiltinType::Dependent:
Mike Stump1eb44332009-09-09 15:08:12 +00001316 assert(false &&
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001317 "Overloaded and dependent types shouldn't get to name mangling");
1318 break;
Steve Naroff9533a7f2009-07-22 17:14:51 +00001319 case BuiltinType::ObjCId: Out << "11objc_object"; break;
1320 case BuiltinType::ObjCClass: Out << "10objc_class"; break;
Fariborz Jahanian13dcd002009-11-21 19:53:08 +00001321 case BuiltinType::ObjCSel: Out << "13objc_selector"; break;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001322 }
1323}
1324
John McCallefe6aee2009-09-05 07:56:18 +00001325// <type> ::= <function-type>
1326// <function-type> ::= F [Y] <bare-function-type> E
1327void CXXNameMangler::mangleType(const FunctionProtoType *T) {
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001328 Out << 'F';
Mike Stumpf5408fe2009-05-16 07:57:57 +00001329 // FIXME: We don't have enough information in the AST to produce the 'Y'
1330 // encoding for extern "C" function types.
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001331 mangleBareFunctionType(T, /*MangleReturnType=*/true);
1332 Out << 'E';
1333}
John McCallefe6aee2009-09-05 07:56:18 +00001334void CXXNameMangler::mangleType(const FunctionNoProtoType *T) {
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +00001335 llvm_unreachable("Can't mangle K&R function prototypes");
John McCallefe6aee2009-09-05 07:56:18 +00001336}
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001337void CXXNameMangler::mangleBareFunctionType(const FunctionType *T,
1338 bool MangleReturnType) {
John McCallefe6aee2009-09-05 07:56:18 +00001339 // We should never be mangling something without a prototype.
1340 const FunctionProtoType *Proto = cast<FunctionProtoType>(T);
1341
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001342 // <bare-function-type> ::= <signature type>+
1343 if (MangleReturnType)
John McCallefe6aee2009-09-05 07:56:18 +00001344 mangleType(Proto->getResultType());
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001345
Anders Carlsson93296682010-06-02 04:40:13 +00001346 if (Proto->getNumArgs() == 0 && !Proto->isVariadic()) {
Eli Friedmana7e68452010-08-22 01:00:03 +00001347 // <builtin-type> ::= v # void
Anders Carlssonc6c91bc2009-04-01 00:15:23 +00001348 Out << 'v';
1349 return;
1350 }
Mike Stump1eb44332009-09-09 15:08:12 +00001351
Douglas Gregor72564e72009-02-26 23:50:07 +00001352 for (FunctionProtoType::arg_type_iterator Arg = Proto->arg_type_begin(),
Mike Stump1eb44332009-09-09 15:08:12 +00001353 ArgEnd = Proto->arg_type_end();
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001354 Arg != ArgEnd; ++Arg)
1355 mangleType(*Arg);
Douglas Gregor219cc612009-02-13 01:28:03 +00001356
1357 // <builtin-type> ::= z # ellipsis
1358 if (Proto->isVariadic())
1359 Out << 'z';
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001360}
1361
John McCallefe6aee2009-09-05 07:56:18 +00001362// <type> ::= <class-enum-type>
Mike Stump1eb44332009-09-09 15:08:12 +00001363// <class-enum-type> ::= <name>
John McCalled976492009-12-04 22:46:56 +00001364void CXXNameMangler::mangleType(const UnresolvedUsingType *T) {
1365 mangleName(T->getDecl());
1366}
1367
1368// <type> ::= <class-enum-type>
1369// <class-enum-type> ::= <name>
John McCallefe6aee2009-09-05 07:56:18 +00001370void CXXNameMangler::mangleType(const EnumType *T) {
1371 mangleType(static_cast<const TagType*>(T));
1372}
1373void CXXNameMangler::mangleType(const RecordType *T) {
1374 mangleType(static_cast<const TagType*>(T));
1375}
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001376void CXXNameMangler::mangleType(const TagType *T) {
Eli Friedmanecb7e932009-12-11 18:00:57 +00001377 mangleName(T->getDecl());
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001378}
1379
John McCallefe6aee2009-09-05 07:56:18 +00001380// <type> ::= <array-type>
1381// <array-type> ::= A <positive dimension number> _ <element type>
1382// ::= A [<dimension expression>] _ <element type>
1383void CXXNameMangler::mangleType(const ConstantArrayType *T) {
1384 Out << 'A' << T->getSize() << '_';
1385 mangleType(T->getElementType());
1386}
1387void CXXNameMangler::mangleType(const VariableArrayType *T) {
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001388 Out << 'A';
Fariborz Jahanian7281d1f2010-11-02 16:54:00 +00001389 // decayed vla types (size 0) will just be skipped.
1390 if (T->getSizeExpr())
1391 mangleExpression(T->getSizeExpr());
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001392 Out << '_';
1393 mangleType(T->getElementType());
1394}
John McCallefe6aee2009-09-05 07:56:18 +00001395void CXXNameMangler::mangleType(const DependentSizedArrayType *T) {
1396 Out << 'A';
1397 mangleExpression(T->getSizeExpr());
1398 Out << '_';
1399 mangleType(T->getElementType());
1400}
1401void CXXNameMangler::mangleType(const IncompleteArrayType *T) {
Nick Lewycky271b6652010-09-05 03:40:33 +00001402 Out << "A_";
John McCallefe6aee2009-09-05 07:56:18 +00001403 mangleType(T->getElementType());
1404}
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001405
John McCallefe6aee2009-09-05 07:56:18 +00001406// <type> ::= <pointer-to-member-type>
1407// <pointer-to-member-type> ::= M <class type> <member type>
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001408void CXXNameMangler::mangleType(const MemberPointerType *T) {
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001409 Out << 'M';
1410 mangleType(QualType(T->getClass(), 0));
Anders Carlsson0e650012009-05-17 17:41:20 +00001411 QualType PointeeType = T->getPointeeType();
1412 if (const FunctionProtoType *FPT = dyn_cast<FunctionProtoType>(PointeeType)) {
John McCall0953e762009-09-24 19:53:00 +00001413 mangleQualifiers(Qualifiers::fromCVRMask(FPT->getTypeQuals()));
Douglas Gregor0a9a6d62011-01-26 17:36:28 +00001414 mangleRefQualifier(FPT->getRefQualifier());
Anders Carlsson0e650012009-05-17 17:41:20 +00001415 mangleType(FPT);
Anders Carlsson9d85b722010-06-02 04:29:50 +00001416
1417 // Itanium C++ ABI 5.1.8:
1418 //
1419 // The type of a non-static member function is considered to be different,
1420 // for the purposes of substitution, from the type of a namespace-scope or
1421 // static member function whose type appears similar. The types of two
1422 // non-static member functions are considered to be different, for the
1423 // purposes of substitution, if the functions are members of different
1424 // classes. In other words, for the purposes of substitution, the class of
1425 // which the function is a member is considered part of the type of
1426 // function.
1427
1428 // We increment the SeqID here to emulate adding an entry to the
1429 // substitution table. We can't actually add it because we don't want this
1430 // particular function type to be substituted.
1431 ++SeqID;
Mike Stump1eb44332009-09-09 15:08:12 +00001432 } else
Anders Carlsson0e650012009-05-17 17:41:20 +00001433 mangleType(PointeeType);
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001434}
1435
John McCallefe6aee2009-09-05 07:56:18 +00001436// <type> ::= <template-param>
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001437void CXXNameMangler::mangleType(const TemplateTypeParmType *T) {
Anders Carlsson0ccdf8d2009-09-27 00:38:53 +00001438 mangleTemplateParameter(T->getIndex());
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001439}
1440
Douglas Gregorc3069d62011-01-14 02:55:32 +00001441// <type> ::= <template-param>
1442void CXXNameMangler::mangleType(const SubstTemplateTypeParmPackType *T) {
1443 mangleTemplateParameter(T->getReplacedParameter()->getIndex());
1444}
1445
John McCallefe6aee2009-09-05 07:56:18 +00001446// <type> ::= P <type> # pointer-to
1447void CXXNameMangler::mangleType(const PointerType *T) {
1448 Out << 'P';
1449 mangleType(T->getPointeeType());
1450}
1451void CXXNameMangler::mangleType(const ObjCObjectPointerType *T) {
1452 Out << 'P';
1453 mangleType(T->getPointeeType());
1454}
1455
1456// <type> ::= R <type> # reference-to
1457void CXXNameMangler::mangleType(const LValueReferenceType *T) {
1458 Out << 'R';
1459 mangleType(T->getPointeeType());
1460}
1461
1462// <type> ::= O <type> # rvalue reference-to (C++0x)
1463void CXXNameMangler::mangleType(const RValueReferenceType *T) {
1464 Out << 'O';
1465 mangleType(T->getPointeeType());
1466}
1467
1468// <type> ::= C <type> # complex pair (C 2000)
1469void CXXNameMangler::mangleType(const ComplexType *T) {
1470 Out << 'C';
1471 mangleType(T->getElementType());
1472}
1473
Bob Wilsonc7df92d2010-11-12 17:24:43 +00001474// ARM's ABI for Neon vector types specifies that they should be mangled as
Bob Wilson57147a82010-11-16 00:32:18 +00001475// if they are structs (to match ARM's initial implementation). The
1476// vector type must be one of the special types predefined by ARM.
1477void CXXNameMangler::mangleNeonVectorType(const VectorType *T) {
Bob Wilsonc7df92d2010-11-12 17:24:43 +00001478 QualType EltType = T->getElementType();
Bob Wilson57147a82010-11-16 00:32:18 +00001479 assert(EltType->isBuiltinType() && "Neon vector element not a BuiltinType");
Bob Wilsonc7df92d2010-11-12 17:24:43 +00001480 const char *EltName = 0;
Bob Wilson491328c2010-11-12 17:24:46 +00001481 if (T->getVectorKind() == VectorType::NeonPolyVector) {
1482 switch (cast<BuiltinType>(EltType)->getKind()) {
Bob Wilson4cfaa5d2010-11-12 17:24:49 +00001483 case BuiltinType::SChar: EltName = "poly8_t"; break;
1484 case BuiltinType::Short: EltName = "poly16_t"; break;
Bob Wilson57147a82010-11-16 00:32:18 +00001485 default: llvm_unreachable("unexpected Neon polynomial vector element type");
Bob Wilson491328c2010-11-12 17:24:46 +00001486 }
1487 } else {
1488 switch (cast<BuiltinType>(EltType)->getKind()) {
Bob Wilson4cfaa5d2010-11-12 17:24:49 +00001489 case BuiltinType::SChar: EltName = "int8_t"; break;
1490 case BuiltinType::UChar: EltName = "uint8_t"; break;
1491 case BuiltinType::Short: EltName = "int16_t"; break;
1492 case BuiltinType::UShort: EltName = "uint16_t"; break;
1493 case BuiltinType::Int: EltName = "int32_t"; break;
1494 case BuiltinType::UInt: EltName = "uint32_t"; break;
1495 case BuiltinType::LongLong: EltName = "int64_t"; break;
1496 case BuiltinType::ULongLong: EltName = "uint64_t"; break;
1497 case BuiltinType::Float: EltName = "float32_t"; break;
Bob Wilson57147a82010-11-16 00:32:18 +00001498 default: llvm_unreachable("unexpected Neon vector element type");
Bob Wilson491328c2010-11-12 17:24:46 +00001499 }
Bob Wilsonc7df92d2010-11-12 17:24:43 +00001500 }
1501 const char *BaseName = 0;
Bob Wilson4cfaa5d2010-11-12 17:24:49 +00001502 unsigned BitSize = (T->getNumElements() *
Bob Wilson3a723022010-11-16 00:32:12 +00001503 getASTContext().getTypeSize(EltType));
Bob Wilsonc7df92d2010-11-12 17:24:43 +00001504 if (BitSize == 64)
1505 BaseName = "__simd64_";
Bob Wilson57147a82010-11-16 00:32:18 +00001506 else {
1507 assert(BitSize == 128 && "Neon vector type not 64 or 128 bits");
Bob Wilsonc7df92d2010-11-12 17:24:43 +00001508 BaseName = "__simd128_";
Bob Wilson57147a82010-11-16 00:32:18 +00001509 }
Bob Wilsonc7df92d2010-11-12 17:24:43 +00001510 Out << strlen(BaseName) + strlen(EltName);
1511 Out << BaseName << EltName;
Bob Wilsonc7df92d2010-11-12 17:24:43 +00001512}
1513
John McCallefe6aee2009-09-05 07:56:18 +00001514// GNU extension: vector types
Chris Lattner788b0fd2010-06-23 06:00:24 +00001515// <type> ::= <vector-type>
1516// <vector-type> ::= Dv <positive dimension number> _
1517// <extended element type>
1518// ::= Dv [<dimension expression>] _ <element type>
1519// <extended element type> ::= <element type>
1520// ::= p # AltiVec vector pixel
John McCallefe6aee2009-09-05 07:56:18 +00001521void CXXNameMangler::mangleType(const VectorType *T) {
Bob Wilson491328c2010-11-12 17:24:46 +00001522 if ((T->getVectorKind() == VectorType::NeonVector ||
Bob Wilson57147a82010-11-16 00:32:18 +00001523 T->getVectorKind() == VectorType::NeonPolyVector)) {
1524 mangleNeonVectorType(T);
Bob Wilsonc7df92d2010-11-12 17:24:43 +00001525 return;
Bob Wilson57147a82010-11-16 00:32:18 +00001526 }
Nick Lewycky0e5f0672010-03-26 07:18:04 +00001527 Out << "Dv" << T->getNumElements() << '_';
Bob Wilsone86d78c2010-11-10 21:56:12 +00001528 if (T->getVectorKind() == VectorType::AltiVecPixel)
Chris Lattner788b0fd2010-06-23 06:00:24 +00001529 Out << 'p';
Bob Wilsone86d78c2010-11-10 21:56:12 +00001530 else if (T->getVectorKind() == VectorType::AltiVecBool)
Chris Lattner788b0fd2010-06-23 06:00:24 +00001531 Out << 'b';
1532 else
1533 mangleType(T->getElementType());
John McCallefe6aee2009-09-05 07:56:18 +00001534}
1535void CXXNameMangler::mangleType(const ExtVectorType *T) {
1536 mangleType(static_cast<const VectorType*>(T));
1537}
1538void CXXNameMangler::mangleType(const DependentSizedExtVectorType *T) {
Nick Lewycky0e5f0672010-03-26 07:18:04 +00001539 Out << "Dv";
1540 mangleExpression(T->getSizeExpr());
1541 Out << '_';
John McCallefe6aee2009-09-05 07:56:18 +00001542 mangleType(T->getElementType());
1543}
1544
Douglas Gregor7536dd52010-12-20 02:24:11 +00001545void CXXNameMangler::mangleType(const PackExpansionType *T) {
Douglas Gregor4fc48662011-01-13 16:39:34 +00001546 // <type> ::= Dp <type> # pack expansion (C++0x)
Douglas Gregor255c2692011-01-13 17:44:36 +00001547 Out << "Dp";
Douglas Gregor7536dd52010-12-20 02:24:11 +00001548 mangleType(T->getPattern());
1549}
1550
Anders Carlssona40c5e42009-03-07 22:03:21 +00001551void CXXNameMangler::mangleType(const ObjCInterfaceType *T) {
1552 mangleSourceName(T->getDecl()->getIdentifier());
1553}
1554
John McCallc12c5bb2010-05-15 11:32:37 +00001555void CXXNameMangler::mangleType(const ObjCObjectType *T) {
John McCallc00c1f62010-05-15 17:06:29 +00001556 // We don't allow overloading by different protocol qualification,
1557 // so mangling them isn't necessary.
John McCallc12c5bb2010-05-15 11:32:37 +00001558 mangleType(T->getBaseType());
1559}
1560
John McCallefe6aee2009-09-05 07:56:18 +00001561void CXXNameMangler::mangleType(const BlockPointerType *T) {
Anders Carlssonf28c6872009-12-23 22:31:44 +00001562 Out << "U13block_pointer";
1563 mangleType(T->getPointeeType());
John McCallefe6aee2009-09-05 07:56:18 +00001564}
1565
John McCall31f17ec2010-04-27 00:57:59 +00001566void CXXNameMangler::mangleType(const InjectedClassNameType *T) {
1567 // Mangle injected class name types as if the user had written the
1568 // specialization out fully. It may not actually be possible to see
1569 // this mangling, though.
1570 mangleType(T->getInjectedSpecializationType());
1571}
1572
John McCallefe6aee2009-09-05 07:56:18 +00001573void CXXNameMangler::mangleType(const TemplateSpecializationType *T) {
Douglas Gregor1e9268e2010-04-28 05:58:56 +00001574 if (TemplateDecl *TD = T->getTemplateName().getAsTemplateDecl()) {
1575 mangleName(TD, T->getArgs(), T->getNumArgs());
1576 } else {
1577 if (mangleSubstitution(QualType(T, 0)))
1578 return;
Sean Huntc3021132010-05-05 15:23:54 +00001579
Douglas Gregor1e9268e2010-04-28 05:58:56 +00001580 mangleTemplatePrefix(T->getTemplateName());
Sean Huntc3021132010-05-05 15:23:54 +00001581
Douglas Gregor1e9268e2010-04-28 05:58:56 +00001582 // FIXME: GCC does not appear to mangle the template arguments when
1583 // the template in question is a dependent template name. Should we
1584 // emulate that badness?
1585 mangleTemplateArgs(T->getTemplateName(), T->getArgs(), T->getNumArgs());
1586 addSubstitution(QualType(T, 0));
1587 }
John McCallefe6aee2009-09-05 07:56:18 +00001588}
1589
Douglas Gregor4714c122010-03-31 17:34:00 +00001590void CXXNameMangler::mangleType(const DependentNameType *T) {
Anders Carlssonae352482009-09-26 02:26:02 +00001591 // Typename types are always nested
1592 Out << 'N';
John McCall33500952010-06-11 00:33:02 +00001593 mangleUnresolvedScope(T->getQualifier());
1594 mangleSourceName(T->getIdentifier());
1595 Out << 'E';
1596}
John McCall6ab30e02010-06-09 07:26:17 +00001597
John McCall33500952010-06-11 00:33:02 +00001598void CXXNameMangler::mangleType(const DependentTemplateSpecializationType *T) {
1599 // Dependently-scoped template types are always nested
1600 Out << 'N';
1601
1602 // TODO: avoid making this TemplateName.
1603 TemplateName Prefix =
1604 getASTContext().getDependentTemplateName(T->getQualifier(),
1605 T->getIdentifier());
1606 mangleTemplatePrefix(Prefix);
1607
1608 // FIXME: GCC does not appear to mangle the template arguments when
1609 // the template in question is a dependent template name. Should we
1610 // emulate that badness?
1611 mangleTemplateArgs(Prefix, T->getArgs(), T->getNumArgs());
Anders Carlssonae352482009-09-26 02:26:02 +00001612 Out << 'E';
John McCallefe6aee2009-09-05 07:56:18 +00001613}
1614
John McCallad5e7382010-03-01 23:49:17 +00001615void CXXNameMangler::mangleType(const TypeOfType *T) {
1616 // FIXME: this is pretty unsatisfactory, but there isn't an obvious
1617 // "extension with parameters" mangling.
1618 Out << "u6typeof";
1619}
1620
1621void CXXNameMangler::mangleType(const TypeOfExprType *T) {
1622 // FIXME: this is pretty unsatisfactory, but there isn't an obvious
1623 // "extension with parameters" mangling.
1624 Out << "u6typeof";
1625}
1626
1627void CXXNameMangler::mangleType(const DecltypeType *T) {
1628 Expr *E = T->getUnderlyingExpr();
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001629
John McCallad5e7382010-03-01 23:49:17 +00001630 // type ::= Dt <expression> E # decltype of an id-expression
1631 // # or class member access
1632 // ::= DT <expression> E # decltype of an expression
1633
1634 // This purports to be an exhaustive list of id-expressions and
1635 // class member accesses. Note that we do not ignore parentheses;
1636 // parentheses change the semantics of decltype for these
1637 // expressions (and cause the mangler to use the other form).
1638 if (isa<DeclRefExpr>(E) ||
1639 isa<MemberExpr>(E) ||
1640 isa<UnresolvedLookupExpr>(E) ||
1641 isa<DependentScopeDeclRefExpr>(E) ||
1642 isa<CXXDependentScopeMemberExpr>(E) ||
1643 isa<UnresolvedMemberExpr>(E))
1644 Out << "Dt";
1645 else
1646 Out << "DT";
1647 mangleExpression(E);
1648 Out << 'E';
1649}
1650
Richard Smith34b41d92011-02-20 03:19:35 +00001651void CXXNameMangler::mangleType(const AutoType *T) {
1652 QualType D = T->getDeducedType();
Richard Smith967ecd32011-02-21 20:10:02 +00001653 // <builtin-type> ::= Da # dependent auto
1654 if (D.isNull())
1655 Out << "Da";
1656 else
1657 mangleType(D);
Richard Smith34b41d92011-02-20 03:19:35 +00001658}
1659
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001660void CXXNameMangler::mangleIntegerLiteral(QualType T,
Anders Carlssone170ba72009-12-14 01:45:37 +00001661 const llvm::APSInt &Value) {
1662 // <expr-primary> ::= L <type> <value number> E # integer literal
1663 Out << 'L';
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001664
Anders Carlssone170ba72009-12-14 01:45:37 +00001665 mangleType(T);
1666 if (T->isBooleanType()) {
1667 // Boolean values are encoded as 0/1.
1668 Out << (Value.getBoolValue() ? '1' : '0');
1669 } else {
John McCall0512e482010-07-14 04:20:34 +00001670 mangleNumber(Value);
Anders Carlssone170ba72009-12-14 01:45:37 +00001671 }
1672 Out << 'E';
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001673
Anders Carlssone170ba72009-12-14 01:45:37 +00001674}
1675
John McCall2f27bf82010-02-04 02:56:29 +00001676/// Mangles a member expression. Implicit accesses are not handled,
1677/// but that should be okay, because you shouldn't be able to
1678/// make an implicit access in a function template declaration.
John McCall2f27bf82010-02-04 02:56:29 +00001679void CXXNameMangler::mangleMemberExpr(const Expr *Base,
1680 bool IsArrow,
1681 NestedNameSpecifier *Qualifier,
1682 DeclarationName Member,
1683 unsigned Arity) {
John McCalle1e342f2010-03-01 19:12:25 +00001684 // gcc-4.4 uses 'dt' for dot expressions, which is reasonable.
1685 // OTOH, gcc also mangles the name as an expression.
1686 Out << (IsArrow ? "pt" : "dt");
John McCall2f27bf82010-02-04 02:56:29 +00001687 mangleExpression(Base);
1688 mangleUnresolvedName(Qualifier, Member, Arity);
1689}
1690
John McCall5e1e89b2010-08-18 19:18:59 +00001691void CXXNameMangler::mangleExpression(const Expr *E, unsigned Arity) {
Anders Carlssond553f8c2009-09-21 01:21:10 +00001692 // <expression> ::= <unary operator-name> <expression>
John McCall09cc1412010-02-03 00:55:45 +00001693 // ::= <binary operator-name> <expression> <expression>
1694 // ::= <trinary operator-name> <expression> <expression> <expression>
Eli Friedmana7e68452010-08-22 01:00:03 +00001695 // ::= cl <expression>* E # call
Anders Carlssond553f8c2009-09-21 01:21:10 +00001696 // ::= cv <type> expression # conversion with one argument
1697 // ::= cv <type> _ <expression>* E # conversion with a different number of arguments
Eli Friedmana7e68452010-08-22 01:00:03 +00001698 // ::= st <type> # sizeof (a type)
Anders Carlssond553f8c2009-09-21 01:21:10 +00001699 // ::= at <type> # alignof (a type)
1700 // ::= <template-param>
1701 // ::= <function-param>
1702 // ::= sr <type> <unqualified-name> # dependent name
1703 // ::= sr <type> <unqualified-name> <template-args> # dependent template-id
1704 // ::= sZ <template-param> # size of a parameter pack
Douglas Gregor4fc48662011-01-13 16:39:34 +00001705 // ::= sZ <function-param> # size of a function parameter pack
John McCall09cc1412010-02-03 00:55:45 +00001706 // ::= <expr-primary>
John McCall1dd73832010-02-04 01:42:13 +00001707 // <expr-primary> ::= L <type> <value number> E # integer literal
1708 // ::= L <type <value float> E # floating literal
1709 // ::= L <mangled-name> E # external name
Anders Carlssond553f8c2009-09-21 01:21:10 +00001710 switch (E->getStmtClass()) {
John McCall6ae1f352010-04-09 22:26:14 +00001711 case Expr::NoStmtClass:
John McCall63c00d72011-02-09 08:16:59 +00001712#define ABSTRACT_STMT(Type)
John McCall6ae1f352010-04-09 22:26:14 +00001713#define EXPR(Type, Base)
1714#define STMT(Type, Base) \
1715 case Expr::Type##Class:
Sean Hunt4bfe1962010-05-05 15:24:00 +00001716#include "clang/AST/StmtNodes.inc"
John McCall0512e482010-07-14 04:20:34 +00001717 // fallthrough
1718
1719 // These all can only appear in local or variable-initialization
1720 // contexts and so should never appear in a mangling.
1721 case Expr::AddrLabelExprClass:
1722 case Expr::BlockDeclRefExprClass:
1723 case Expr::CXXThisExprClass:
1724 case Expr::DesignatedInitExprClass:
1725 case Expr::ImplicitValueInitExprClass:
1726 case Expr::InitListExprClass:
1727 case Expr::ParenListExprClass:
1728 case Expr::CXXScalarValueInitExprClass:
John McCall09cc1412010-02-03 00:55:45 +00001729 llvm_unreachable("unexpected statement kind");
1730 break;
1731
John McCall0512e482010-07-14 04:20:34 +00001732 // FIXME: invent manglings for all these.
1733 case Expr::BlockExprClass:
1734 case Expr::CXXPseudoDestructorExprClass:
1735 case Expr::ChooseExprClass:
1736 case Expr::CompoundLiteralExprClass:
1737 case Expr::ExtVectorElementExprClass:
1738 case Expr::ObjCEncodeExprClass:
John McCall0512e482010-07-14 04:20:34 +00001739 case Expr::ObjCIsaExprClass:
1740 case Expr::ObjCIvarRefExprClass:
1741 case Expr::ObjCMessageExprClass:
1742 case Expr::ObjCPropertyRefExprClass:
1743 case Expr::ObjCProtocolExprClass:
1744 case Expr::ObjCSelectorExprClass:
1745 case Expr::ObjCStringLiteralClass:
John McCall0512e482010-07-14 04:20:34 +00001746 case Expr::OffsetOfExprClass:
1747 case Expr::PredefinedExprClass:
1748 case Expr::ShuffleVectorExprClass:
1749 case Expr::StmtExprClass:
John McCall0512e482010-07-14 04:20:34 +00001750 case Expr::UnaryTypeTraitExprClass:
Francois Pichet6ad6f282010-12-07 00:08:36 +00001751 case Expr::BinaryTypeTraitExprClass:
Francois Pichet9be88402010-09-08 23:47:05 +00001752 case Expr::VAArgExprClass:
Sebastian Redl2e156222010-09-10 20:55:43 +00001753 case Expr::CXXUuidofExprClass:
Peter Collingbournee08ce652011-02-09 21:07:24 +00001754 case Expr::CXXNoexceptExprClass:
1755 case Expr::CUDAKernelCallExprClass: {
John McCall6ae1f352010-04-09 22:26:14 +00001756 // As bad as this diagnostic is, it's better than crashing.
1757 Diagnostic &Diags = Context.getDiags();
1758 unsigned DiagID = Diags.getCustomDiagID(Diagnostic::Error,
1759 "cannot yet mangle expression type %0");
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +00001760 Diags.Report(E->getExprLoc(), DiagID)
John McCall739bf092010-04-10 09:39:25 +00001761 << E->getStmtClassName() << E->getSourceRange();
John McCall6ae1f352010-04-09 22:26:14 +00001762 break;
1763 }
1764
John McCall56ca35d2011-02-17 10:25:35 +00001765 // Even gcc-4.5 doesn't mangle this.
1766 case Expr::BinaryConditionalOperatorClass: {
1767 Diagnostic &Diags = Context.getDiags();
1768 unsigned DiagID =
1769 Diags.getCustomDiagID(Diagnostic::Error,
1770 "?: operator with omitted middle operand cannot be mangled");
1771 Diags.Report(E->getExprLoc(), DiagID)
1772 << E->getStmtClassName() << E->getSourceRange();
1773 break;
1774 }
1775
1776 // These are used for internal purposes and cannot be meaningfully mangled.
John McCall7cd7d1a2010-11-15 23:31:06 +00001777 case Expr::OpaqueValueExprClass:
1778 llvm_unreachable("cannot mangle opaque value; mangling wrong thing?");
1779
John McCall0512e482010-07-14 04:20:34 +00001780 case Expr::CXXDefaultArgExprClass:
John McCall5e1e89b2010-08-18 19:18:59 +00001781 mangleExpression(cast<CXXDefaultArgExpr>(E)->getExpr(), Arity);
John McCall0512e482010-07-14 04:20:34 +00001782 break;
1783
1784 case Expr::CXXMemberCallExprClass: // fallthrough
John McCall1dd73832010-02-04 01:42:13 +00001785 case Expr::CallExprClass: {
1786 const CallExpr *CE = cast<CallExpr>(E);
1787 Out << "cl";
John McCall5e1e89b2010-08-18 19:18:59 +00001788 mangleExpression(CE->getCallee(), CE->getNumArgs());
John McCall1dd73832010-02-04 01:42:13 +00001789 for (unsigned I = 0, N = CE->getNumArgs(); I != N; ++I)
1790 mangleExpression(CE->getArg(I));
Benjamin Kramer35f59b62010-04-10 16:03:31 +00001791 Out << 'E';
John McCall09cc1412010-02-03 00:55:45 +00001792 break;
John McCall1dd73832010-02-04 01:42:13 +00001793 }
John McCall09cc1412010-02-03 00:55:45 +00001794
John McCall0512e482010-07-14 04:20:34 +00001795 case Expr::CXXNewExprClass: {
1796 // Proposal from David Vandervoorde, 2010.06.30
1797 const CXXNewExpr *New = cast<CXXNewExpr>(E);
1798 if (New->isGlobalNew()) Out << "gs";
1799 Out << (New->isArray() ? "na" : "nw");
1800 for (CXXNewExpr::const_arg_iterator I = New->placement_arg_begin(),
1801 E = New->placement_arg_end(); I != E; ++I)
1802 mangleExpression(*I);
1803 Out << '_';
1804 mangleType(New->getAllocatedType());
1805 if (New->hasInitializer()) {
1806 Out << "pi";
1807 for (CXXNewExpr::const_arg_iterator I = New->constructor_arg_begin(),
1808 E = New->constructor_arg_end(); I != E; ++I)
1809 mangleExpression(*I);
1810 }
1811 Out << 'E';
1812 break;
1813 }
1814
John McCall2f27bf82010-02-04 02:56:29 +00001815 case Expr::MemberExprClass: {
1816 const MemberExpr *ME = cast<MemberExpr>(E);
1817 mangleMemberExpr(ME->getBase(), ME->isArrow(),
1818 ME->getQualifier(), ME->getMemberDecl()->getDeclName(),
John McCall5e1e89b2010-08-18 19:18:59 +00001819 Arity);
John McCall2f27bf82010-02-04 02:56:29 +00001820 break;
1821 }
1822
1823 case Expr::UnresolvedMemberExprClass: {
1824 const UnresolvedMemberExpr *ME = cast<UnresolvedMemberExpr>(E);
1825 mangleMemberExpr(ME->getBase(), ME->isArrow(),
1826 ME->getQualifier(), ME->getMemberName(),
John McCall5e1e89b2010-08-18 19:18:59 +00001827 Arity);
John McCall6dbce192010-08-20 00:17:19 +00001828 if (ME->hasExplicitTemplateArgs())
1829 mangleTemplateArgs(ME->getExplicitTemplateArgs());
John McCall2f27bf82010-02-04 02:56:29 +00001830 break;
1831 }
1832
1833 case Expr::CXXDependentScopeMemberExprClass: {
1834 const CXXDependentScopeMemberExpr *ME
1835 = cast<CXXDependentScopeMemberExpr>(E);
1836 mangleMemberExpr(ME->getBase(), ME->isArrow(),
1837 ME->getQualifier(), ME->getMember(),
John McCall5e1e89b2010-08-18 19:18:59 +00001838 Arity);
John McCall6dbce192010-08-20 00:17:19 +00001839 if (ME->hasExplicitTemplateArgs())
1840 mangleTemplateArgs(ME->getExplicitTemplateArgs());
John McCall2f27bf82010-02-04 02:56:29 +00001841 break;
1842 }
1843
John McCall1dd73832010-02-04 01:42:13 +00001844 case Expr::UnresolvedLookupExprClass: {
John McCalla3218e72010-02-04 01:48:38 +00001845 // The ABI doesn't cover how to mangle overload sets, so we mangle
1846 // using something as close as possible to the original lookup
1847 // expression.
John McCall1dd73832010-02-04 01:42:13 +00001848 const UnresolvedLookupExpr *ULE = cast<UnresolvedLookupExpr>(E);
John McCall5e1e89b2010-08-18 19:18:59 +00001849 mangleUnresolvedName(ULE->getQualifier(), ULE->getName(), Arity);
John McCall6dbce192010-08-20 00:17:19 +00001850 if (ULE->hasExplicitTemplateArgs())
1851 mangleTemplateArgs(ULE->getExplicitTemplateArgs());
John McCall1dd73832010-02-04 01:42:13 +00001852 break;
1853 }
1854
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001855 case Expr::CXXUnresolvedConstructExprClass: {
John McCall1dd73832010-02-04 01:42:13 +00001856 const CXXUnresolvedConstructExpr *CE = cast<CXXUnresolvedConstructExpr>(E);
1857 unsigned N = CE->arg_size();
1858
1859 Out << "cv";
1860 mangleType(CE->getType());
Benjamin Kramer35f59b62010-04-10 16:03:31 +00001861 if (N != 1) Out << '_';
John McCall1dd73832010-02-04 01:42:13 +00001862 for (unsigned I = 0; I != N; ++I) mangleExpression(CE->getArg(I));
Benjamin Kramer35f59b62010-04-10 16:03:31 +00001863 if (N != 1) Out << 'E';
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001864 break;
John McCall1dd73832010-02-04 01:42:13 +00001865 }
John McCall09cc1412010-02-03 00:55:45 +00001866
John McCall1dd73832010-02-04 01:42:13 +00001867 case Expr::CXXTemporaryObjectExprClass:
1868 case Expr::CXXConstructExprClass: {
1869 const CXXConstructExpr *CE = cast<CXXConstructExpr>(E);
1870 unsigned N = CE->getNumArgs();
1871
1872 Out << "cv";
1873 mangleType(CE->getType());
Benjamin Kramer35f59b62010-04-10 16:03:31 +00001874 if (N != 1) Out << '_';
John McCall1dd73832010-02-04 01:42:13 +00001875 for (unsigned I = 0; I != N; ++I) mangleExpression(CE->getArg(I));
Benjamin Kramer35f59b62010-04-10 16:03:31 +00001876 if (N != 1) Out << 'E';
John McCall09cc1412010-02-03 00:55:45 +00001877 break;
John McCall1dd73832010-02-04 01:42:13 +00001878 }
1879
1880 case Expr::SizeOfAlignOfExprClass: {
1881 const SizeOfAlignOfExpr *SAE = cast<SizeOfAlignOfExpr>(E);
Benjamin Kramer35f59b62010-04-10 16:03:31 +00001882 if (SAE->isSizeOf()) Out << 's';
1883 else Out << 'a';
John McCall1dd73832010-02-04 01:42:13 +00001884 if (SAE->isArgumentType()) {
Benjamin Kramer35f59b62010-04-10 16:03:31 +00001885 Out << 't';
John McCall1dd73832010-02-04 01:42:13 +00001886 mangleType(SAE->getArgumentType());
1887 } else {
Benjamin Kramer35f59b62010-04-10 16:03:31 +00001888 Out << 'z';
John McCall1dd73832010-02-04 01:42:13 +00001889 mangleExpression(SAE->getArgumentExpr());
1890 }
1891 break;
1892 }
Anders Carlssona7694082009-11-06 02:50:19 +00001893
John McCall0512e482010-07-14 04:20:34 +00001894 case Expr::CXXThrowExprClass: {
1895 const CXXThrowExpr *TE = cast<CXXThrowExpr>(E);
1896
1897 // Proposal from David Vandervoorde, 2010.06.30
1898 if (TE->getSubExpr()) {
1899 Out << "tw";
1900 mangleExpression(TE->getSubExpr());
1901 } else {
1902 Out << "tr";
1903 }
1904 break;
1905 }
1906
1907 case Expr::CXXTypeidExprClass: {
1908 const CXXTypeidExpr *TIE = cast<CXXTypeidExpr>(E);
1909
1910 // Proposal from David Vandervoorde, 2010.06.30
1911 if (TIE->isTypeOperand()) {
1912 Out << "ti";
1913 mangleType(TIE->getTypeOperand());
1914 } else {
1915 Out << "te";
1916 mangleExpression(TIE->getExprOperand());
1917 }
1918 break;
1919 }
1920
1921 case Expr::CXXDeleteExprClass: {
1922 const CXXDeleteExpr *DE = cast<CXXDeleteExpr>(E);
1923
1924 // Proposal from David Vandervoorde, 2010.06.30
1925 if (DE->isGlobalDelete()) Out << "gs";
1926 Out << (DE->isArrayForm() ? "da" : "dl");
1927 mangleExpression(DE->getArgument());
1928 break;
1929 }
1930
Anders Carlssone170ba72009-12-14 01:45:37 +00001931 case Expr::UnaryOperatorClass: {
1932 const UnaryOperator *UO = cast<UnaryOperator>(E);
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001933 mangleOperatorName(UnaryOperator::getOverloadedOperator(UO->getOpcode()),
Anders Carlssone170ba72009-12-14 01:45:37 +00001934 /*Arity=*/1);
1935 mangleExpression(UO->getSubExpr());
1936 break;
1937 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001938
John McCall0512e482010-07-14 04:20:34 +00001939 case Expr::ArraySubscriptExprClass: {
1940 const ArraySubscriptExpr *AE = cast<ArraySubscriptExpr>(E);
1941
1942 // Array subscript is treated as a syntactically wierd form of
1943 // binary operator.
1944 Out << "ix";
1945 mangleExpression(AE->getLHS());
1946 mangleExpression(AE->getRHS());
1947 break;
1948 }
1949
1950 case Expr::CompoundAssignOperatorClass: // fallthrough
Anders Carlssone170ba72009-12-14 01:45:37 +00001951 case Expr::BinaryOperatorClass: {
1952 const BinaryOperator *BO = cast<BinaryOperator>(E);
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001953 mangleOperatorName(BinaryOperator::getOverloadedOperator(BO->getOpcode()),
Anders Carlssone170ba72009-12-14 01:45:37 +00001954 /*Arity=*/2);
1955 mangleExpression(BO->getLHS());
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001956 mangleExpression(BO->getRHS());
Anders Carlssone170ba72009-12-14 01:45:37 +00001957 break;
John McCall2f27bf82010-02-04 02:56:29 +00001958 }
Anders Carlssone170ba72009-12-14 01:45:37 +00001959
1960 case Expr::ConditionalOperatorClass: {
1961 const ConditionalOperator *CO = cast<ConditionalOperator>(E);
1962 mangleOperatorName(OO_Conditional, /*Arity=*/3);
1963 mangleExpression(CO->getCond());
John McCall5e1e89b2010-08-18 19:18:59 +00001964 mangleExpression(CO->getLHS(), Arity);
1965 mangleExpression(CO->getRHS(), Arity);
Anders Carlssone170ba72009-12-14 01:45:37 +00001966 break;
1967 }
1968
Douglas Gregor46287c72010-01-29 16:37:09 +00001969 case Expr::ImplicitCastExprClass: {
John McCall5e1e89b2010-08-18 19:18:59 +00001970 mangleExpression(cast<ImplicitCastExpr>(E)->getSubExpr(), Arity);
Douglas Gregor46287c72010-01-29 16:37:09 +00001971 break;
1972 }
1973
1974 case Expr::CStyleCastExprClass:
1975 case Expr::CXXStaticCastExprClass:
1976 case Expr::CXXDynamicCastExprClass:
1977 case Expr::CXXReinterpretCastExprClass:
1978 case Expr::CXXConstCastExprClass:
1979 case Expr::CXXFunctionalCastExprClass: {
1980 const ExplicitCastExpr *ECE = cast<ExplicitCastExpr>(E);
1981 Out << "cv";
1982 mangleType(ECE->getType());
1983 mangleExpression(ECE->getSubExpr());
1984 break;
1985 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001986
Anders Carlsson58040a52009-12-16 05:48:46 +00001987 case Expr::CXXOperatorCallExprClass: {
1988 const CXXOperatorCallExpr *CE = cast<CXXOperatorCallExpr>(E);
1989 unsigned NumArgs = CE->getNumArgs();
1990 mangleOperatorName(CE->getOperator(), /*Arity=*/NumArgs);
1991 // Mangle the arguments.
1992 for (unsigned i = 0; i != NumArgs; ++i)
1993 mangleExpression(CE->getArg(i));
1994 break;
1995 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001996
Anders Carlssona7694082009-11-06 02:50:19 +00001997 case Expr::ParenExprClass:
John McCall5e1e89b2010-08-18 19:18:59 +00001998 mangleExpression(cast<ParenExpr>(E)->getSubExpr(), Arity);
Anders Carlssona7694082009-11-06 02:50:19 +00001999 break;
2000
Anders Carlssond553f8c2009-09-21 01:21:10 +00002001 case Expr::DeclRefExprClass: {
Douglas Gregor5ed1bc32010-02-28 21:40:32 +00002002 const NamedDecl *D = cast<DeclRefExpr>(E)->getDecl();
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002003
Anders Carlssond553f8c2009-09-21 01:21:10 +00002004 switch (D->getKind()) {
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002005 default:
Douglas Gregor5ed1bc32010-02-28 21:40:32 +00002006 // <expr-primary> ::= L <mangled-name> E # external name
2007 Out << 'L';
2008 mangle(D, "_Z");
2009 Out << 'E';
2010 break;
2011
John McCall3dc7e7b2010-07-24 01:17:35 +00002012 case Decl::EnumConstant: {
2013 const EnumConstantDecl *ED = cast<EnumConstantDecl>(D);
2014 mangleIntegerLiteral(ED->getType(), ED->getInitVal());
2015 break;
2016 }
2017
Anders Carlssond553f8c2009-09-21 01:21:10 +00002018 case Decl::NonTypeTemplateParm: {
2019 const NonTypeTemplateParmDecl *PD = cast<NonTypeTemplateParmDecl>(D);
Anders Carlsson0ccdf8d2009-09-27 00:38:53 +00002020 mangleTemplateParameter(PD->getIndex());
Anders Carlssond553f8c2009-09-21 01:21:10 +00002021 break;
2022 }
2023
2024 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002025
Anders Carlsson50755b02009-09-27 20:11:34 +00002026 break;
Anders Carlssond553f8c2009-09-21 01:21:10 +00002027 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002028
Douglas Gregorc7793c72011-01-15 01:15:58 +00002029 case Expr::SubstNonTypeTemplateParmPackExprClass:
2030 mangleTemplateParameter(
2031 cast<SubstNonTypeTemplateParmPackExpr>(E)->getParameterPack()->getIndex());
2032 break;
2033
John McCall865d4472009-11-19 22:55:06 +00002034 case Expr::DependentScopeDeclRefExprClass: {
2035 const DependentScopeDeclRefExpr *DRE = cast<DependentScopeDeclRefExpr>(E);
Douglas Gregor4b2ccfc2010-02-28 22:05:49 +00002036 NestedNameSpecifier *NNS = DRE->getQualifier();
2037 const Type *QTy = NNS->getAsType();
2038
2039 // When we're dealing with a nested-name-specifier that has just a
2040 // dependent identifier in it, mangle that as a typename. FIXME:
2041 // It isn't clear that we ever actually want to have such a
2042 // nested-name-specifier; why not just represent it as a typename type?
2043 if (!QTy && NNS->getAsIdentifier() && NNS->getPrefix()) {
Douglas Gregor4a2023f2010-03-31 20:19:30 +00002044 QTy = getASTContext().getDependentNameType(ETK_Typename,
2045 NNS->getPrefix(),
2046 NNS->getAsIdentifier())
Douglas Gregor4b2ccfc2010-02-28 22:05:49 +00002047 .getTypePtr();
2048 }
Anders Carlsson50755b02009-09-27 20:11:34 +00002049 assert(QTy && "Qualifier was not type!");
2050
John McCall6dbce192010-08-20 00:17:19 +00002051 // ::= sr <type> <unqualified-name> # dependent name
2052 // ::= sr <type> <unqualified-name> <template-args> # dependent template-id
Anders Carlsson50755b02009-09-27 20:11:34 +00002053 Out << "sr";
2054 mangleType(QualType(QTy, 0));
John McCall5e1e89b2010-08-18 19:18:59 +00002055 mangleUnqualifiedName(0, DRE->getDeclName(), Arity);
John McCall6dbce192010-08-20 00:17:19 +00002056 if (DRE->hasExplicitTemplateArgs())
2057 mangleTemplateArgs(DRE->getExplicitTemplateArgs());
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002058
Anders Carlsson50755b02009-09-27 20:11:34 +00002059 break;
2060 }
2061
John McCalld9307602010-04-09 22:54:09 +00002062 case Expr::CXXBindTemporaryExprClass:
2063 mangleExpression(cast<CXXBindTemporaryExpr>(E)->getSubExpr());
2064 break;
2065
John McCall4765fa02010-12-06 08:20:24 +00002066 case Expr::ExprWithCleanupsClass:
2067 mangleExpression(cast<ExprWithCleanups>(E)->getSubExpr(), Arity);
John McCalld9307602010-04-09 22:54:09 +00002068 break;
2069
John McCall1dd73832010-02-04 01:42:13 +00002070 case Expr::FloatingLiteralClass: {
2071 const FloatingLiteral *FL = cast<FloatingLiteral>(E);
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002072 Out << 'L';
John McCall1dd73832010-02-04 01:42:13 +00002073 mangleType(FL->getType());
John McCall0512e482010-07-14 04:20:34 +00002074 mangleFloat(FL->getValue());
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002075 Out << 'E';
John McCall1dd73832010-02-04 01:42:13 +00002076 break;
2077 }
2078
John McCallde810632010-04-09 21:48:08 +00002079 case Expr::CharacterLiteralClass:
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002080 Out << 'L';
John McCallde810632010-04-09 21:48:08 +00002081 mangleType(E->getType());
2082 Out << cast<CharacterLiteral>(E)->getValue();
2083 Out << 'E';
2084 break;
2085
2086 case Expr::CXXBoolLiteralExprClass:
2087 Out << "Lb";
2088 Out << (cast<CXXBoolLiteralExpr>(E)->getValue() ? '1' : '0');
2089 Out << 'E';
2090 break;
2091
John McCall0512e482010-07-14 04:20:34 +00002092 case Expr::IntegerLiteralClass: {
2093 llvm::APSInt Value(cast<IntegerLiteral>(E)->getValue());
2094 if (E->getType()->isSignedIntegerType())
2095 Value.setIsSigned(true);
2096 mangleIntegerLiteral(E->getType(), Value);
Anders Carlssone170ba72009-12-14 01:45:37 +00002097 break;
John McCall0512e482010-07-14 04:20:34 +00002098 }
2099
2100 case Expr::ImaginaryLiteralClass: {
2101 const ImaginaryLiteral *IE = cast<ImaginaryLiteral>(E);
2102 // Mangle as if a complex literal.
Nick Lewycky271b6652010-09-05 03:40:33 +00002103 // Proposal from David Vandevoorde, 2010.06.30.
John McCall0512e482010-07-14 04:20:34 +00002104 Out << 'L';
2105 mangleType(E->getType());
2106 if (const FloatingLiteral *Imag =
2107 dyn_cast<FloatingLiteral>(IE->getSubExpr())) {
2108 // Mangle a floating-point zero of the appropriate type.
2109 mangleFloat(llvm::APFloat(Imag->getValue().getSemantics()));
2110 Out << '_';
2111 mangleFloat(Imag->getValue());
2112 } else {
Nick Lewycky271b6652010-09-05 03:40:33 +00002113 Out << "0_";
John McCall0512e482010-07-14 04:20:34 +00002114 llvm::APSInt Value(cast<IntegerLiteral>(IE->getSubExpr())->getValue());
2115 if (IE->getSubExpr()->getType()->isSignedIntegerType())
2116 Value.setIsSigned(true);
2117 mangleNumber(Value);
2118 }
2119 Out << 'E';
2120 break;
2121 }
2122
2123 case Expr::StringLiteralClass: {
John McCall1658c392010-07-15 21:53:03 +00002124 // Revised proposal from David Vandervoorde, 2010.07.15.
John McCall0512e482010-07-14 04:20:34 +00002125 Out << 'L';
John McCall1658c392010-07-15 21:53:03 +00002126 assert(isa<ConstantArrayType>(E->getType()));
2127 mangleType(E->getType());
John McCall0512e482010-07-14 04:20:34 +00002128 Out << 'E';
2129 break;
2130 }
2131
2132 case Expr::GNUNullExprClass:
2133 // FIXME: should this really be mangled the same as nullptr?
2134 // fallthrough
2135
2136 case Expr::CXXNullPtrLiteralExprClass: {
2137 // Proposal from David Vandervoorde, 2010.06.30, as
2138 // modified by ABI list discussion.
2139 Out << "LDnE";
2140 break;
2141 }
Douglas Gregorbe230c32011-01-03 17:17:50 +00002142
2143 case Expr::PackExpansionExprClass:
2144 Out << "sp";
2145 mangleExpression(cast<PackExpansionExpr>(E)->getPattern());
2146 break;
Douglas Gregor2e774c42011-01-04 18:56:13 +00002147
2148 case Expr::SizeOfPackExprClass: {
Douglas Gregor2e774c42011-01-04 18:56:13 +00002149 Out << "sZ";
2150 const NamedDecl *Pack = cast<SizeOfPackExpr>(E)->getPack();
2151 if (const TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(Pack))
2152 mangleTemplateParameter(TTP->getIndex());
2153 else if (const NonTypeTemplateParmDecl *NTTP
2154 = dyn_cast<NonTypeTemplateParmDecl>(Pack))
2155 mangleTemplateParameter(NTTP->getIndex());
2156 else if (const TemplateTemplateParmDecl *TempTP
2157 = dyn_cast<TemplateTemplateParmDecl>(Pack))
2158 mangleTemplateParameter(TempTP->getIndex());
2159 else {
Douglas Gregor4fc48662011-01-13 16:39:34 +00002160 // Note: proposed by Mike Herrick on 11/30/10
2161 // <expression> ::= sZ <function-param> # size of function parameter pack
Douglas Gregor2e774c42011-01-04 18:56:13 +00002162 Diagnostic &Diags = Context.getDiags();
2163 unsigned DiagID = Diags.getCustomDiagID(Diagnostic::Error,
2164 "cannot mangle sizeof...(function parameter pack)");
2165 Diags.Report(DiagID);
2166 return;
2167 }
2168 }
Anders Carlssond553f8c2009-09-21 01:21:10 +00002169 }
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00002170}
2171
Anders Carlsson3ac86b52009-04-15 05:36:58 +00002172void CXXNameMangler::mangleCXXCtorType(CXXCtorType T) {
2173 // <ctor-dtor-name> ::= C1 # complete object constructor
2174 // ::= C2 # base object constructor
2175 // ::= C3 # complete object allocating constructor
2176 //
2177 switch (T) {
2178 case Ctor_Complete:
2179 Out << "C1";
2180 break;
2181 case Ctor_Base:
2182 Out << "C2";
2183 break;
2184 case Ctor_CompleteAllocating:
2185 Out << "C3";
2186 break;
2187 }
2188}
2189
Anders Carlsson27ae5362009-04-17 01:58:57 +00002190void CXXNameMangler::mangleCXXDtorType(CXXDtorType T) {
2191 // <ctor-dtor-name> ::= D0 # deleting destructor
2192 // ::= D1 # complete object destructor
2193 // ::= D2 # base object destructor
2194 //
2195 switch (T) {
2196 case Dtor_Deleting:
2197 Out << "D0";
2198 break;
2199 case Dtor_Complete:
2200 Out << "D1";
2201 break;
2202 case Dtor_Base:
2203 Out << "D2";
2204 break;
2205 }
2206}
2207
John McCall6dbce192010-08-20 00:17:19 +00002208void CXXNameMangler::mangleTemplateArgs(
2209 const ExplicitTemplateArgumentList &TemplateArgs) {
2210 // <template-args> ::= I <template-arg>+ E
2211 Out << 'I';
2212 for (unsigned I = 0, E = TemplateArgs.NumTemplateArgs; I != E; ++I)
2213 mangleTemplateArg(0, TemplateArgs.getTemplateArgs()[I].getArgument());
2214 Out << 'E';
2215}
2216
Douglas Gregor20f0cc72010-04-23 03:10:43 +00002217void CXXNameMangler::mangleTemplateArgs(TemplateName Template,
2218 const TemplateArgument *TemplateArgs,
2219 unsigned NumTemplateArgs) {
2220 if (TemplateDecl *TD = Template.getAsTemplateDecl())
2221 return mangleTemplateArgs(*TD->getTemplateParameters(), TemplateArgs,
2222 NumTemplateArgs);
Sean Huntc3021132010-05-05 15:23:54 +00002223
Douglas Gregor20f0cc72010-04-23 03:10:43 +00002224 // <template-args> ::= I <template-arg>+ E
2225 Out << 'I';
2226 for (unsigned i = 0; i != NumTemplateArgs; ++i)
2227 mangleTemplateArg(0, TemplateArgs[i]);
2228 Out << 'E';
2229}
2230
Rafael Espindolad9800722010-03-11 14:07:00 +00002231void CXXNameMangler::mangleTemplateArgs(const TemplateParameterList &PL,
2232 const TemplateArgumentList &AL) {
Anders Carlsson7a0ba872009-05-15 16:09:15 +00002233 // <template-args> ::= I <template-arg>+ E
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002234 Out << 'I';
Rafael Espindolad9800722010-03-11 14:07:00 +00002235 for (unsigned i = 0, e = AL.size(); i != e; ++i)
2236 mangleTemplateArg(PL.getParam(i), AL[i]);
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002237 Out << 'E';
Anders Carlsson7a0ba872009-05-15 16:09:15 +00002238}
2239
Rafael Espindolad9800722010-03-11 14:07:00 +00002240void CXXNameMangler::mangleTemplateArgs(const TemplateParameterList &PL,
2241 const TemplateArgument *TemplateArgs,
Anders Carlsson7624f212009-09-18 02:42:01 +00002242 unsigned NumTemplateArgs) {
2243 // <template-args> ::= I <template-arg>+ E
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002244 Out << 'I';
Daniel Dunbar7e0c1952009-11-21 09:17:15 +00002245 for (unsigned i = 0; i != NumTemplateArgs; ++i)
Rafael Espindolad9800722010-03-11 14:07:00 +00002246 mangleTemplateArg(PL.getParam(i), TemplateArgs[i]);
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002247 Out << 'E';
Anders Carlsson7624f212009-09-18 02:42:01 +00002248}
2249
Rafael Espindolad9800722010-03-11 14:07:00 +00002250void CXXNameMangler::mangleTemplateArg(const NamedDecl *P,
2251 const TemplateArgument &A) {
Mike Stump1eb44332009-09-09 15:08:12 +00002252 // <template-arg> ::= <type> # type or template
Anders Carlsson7a0ba872009-05-15 16:09:15 +00002253 // ::= X <expression> E # expression
2254 // ::= <expr-primary> # simple expressions
Douglas Gregor4fc48662011-01-13 16:39:34 +00002255 // ::= J <template-arg>* E # argument pack
Anders Carlsson7a0ba872009-05-15 16:09:15 +00002256 // ::= sp <expression> # pack expansion of (C++0x)
2257 switch (A.getKind()) {
Douglas Gregorf90b27a2011-01-03 22:36:02 +00002258 case TemplateArgument::Null:
2259 llvm_unreachable("Cannot mangle NULL template argument");
2260
Anders Carlsson7a0ba872009-05-15 16:09:15 +00002261 case TemplateArgument::Type:
2262 mangleType(A.getAsType());
2263 break;
Anders Carlsson9e85c742009-12-23 19:30:55 +00002264 case TemplateArgument::Template:
John McCallb6f532e2010-07-14 06:43:17 +00002265 // This is mangled as <type>.
2266 mangleType(A.getAsTemplate());
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002267 break;
Douglas Gregora7fc9012011-01-05 18:58:31 +00002268 case TemplateArgument::TemplateExpansion:
Douglas Gregor4fc48662011-01-13 16:39:34 +00002269 // <type> ::= Dp <type> # pack expansion (C++0x)
Douglas Gregora7fc9012011-01-05 18:58:31 +00002270 Out << "Dp";
2271 mangleType(A.getAsTemplateOrTemplatePattern());
2272 break;
Anders Carlssond553f8c2009-09-21 01:21:10 +00002273 case TemplateArgument::Expression:
2274 Out << 'X';
2275 mangleExpression(A.getAsExpr());
2276 Out << 'E';
2277 break;
Anders Carlssone170ba72009-12-14 01:45:37 +00002278 case TemplateArgument::Integral:
2279 mangleIntegerLiteral(A.getIntegralType(), *A.getAsIntegral());
Anders Carlsson7a0ba872009-05-15 16:09:15 +00002280 break;
Daniel Dunbar7e0c1952009-11-21 09:17:15 +00002281 case TemplateArgument::Declaration: {
Douglas Gregor20f0cc72010-04-23 03:10:43 +00002282 assert(P && "Missing template parameter for declaration argument");
Daniel Dunbar7e0c1952009-11-21 09:17:15 +00002283 // <expr-primary> ::= L <mangled-name> E # external name
2284
Rafael Espindolad9800722010-03-11 14:07:00 +00002285 // Clang produces AST's where pointer-to-member-function expressions
Daniel Dunbar7e0c1952009-11-21 09:17:15 +00002286 // and pointer-to-function expressions are represented as a declaration not
Rafael Espindolad9800722010-03-11 14:07:00 +00002287 // an expression. We compensate for it here to produce the correct mangling.
2288 NamedDecl *D = cast<NamedDecl>(A.getAsDecl());
2289 const NonTypeTemplateParmDecl *Parameter = cast<NonTypeTemplateParmDecl>(P);
2290 bool compensateMangling = D->isCXXClassMember() &&
2291 !Parameter->getType()->isReferenceType();
2292 if (compensateMangling) {
2293 Out << 'X';
2294 mangleOperatorName(OO_Amp, 1);
2295 }
2296
Daniel Dunbar7e0c1952009-11-21 09:17:15 +00002297 Out << 'L';
2298 // References to external entities use the mangled name; if the name would
2299 // not normally be manged then mangle it as unqualified.
2300 //
2301 // FIXME: The ABI specifies that external names here should have _Z, but
2302 // gcc leaves this off.
Rafael Espindolad9800722010-03-11 14:07:00 +00002303 if (compensateMangling)
2304 mangle(D, "_Z");
2305 else
2306 mangle(D, "Z");
Daniel Dunbar7e0c1952009-11-21 09:17:15 +00002307 Out << 'E';
Rafael Espindolad9800722010-03-11 14:07:00 +00002308
2309 if (compensateMangling)
2310 Out << 'E';
2311
Daniel Dunbar7e0c1952009-11-21 09:17:15 +00002312 break;
2313 }
Douglas Gregorf90b27a2011-01-03 22:36:02 +00002314
2315 case TemplateArgument::Pack: {
2316 // Note: proposal by Mike Herrick on 12/20/10
2317 Out << 'J';
2318 for (TemplateArgument::pack_iterator PA = A.pack_begin(),
2319 PAEnd = A.pack_end();
2320 PA != PAEnd; ++PA)
2321 mangleTemplateArg(P, *PA);
2322 Out << 'E';
2323 }
Daniel Dunbar7e0c1952009-11-21 09:17:15 +00002324 }
Anders Carlsson7a0ba872009-05-15 16:09:15 +00002325}
2326
Anders Carlsson0ccdf8d2009-09-27 00:38:53 +00002327void CXXNameMangler::mangleTemplateParameter(unsigned Index) {
2328 // <template-param> ::= T_ # first template parameter
2329 // ::= T <parameter-2 non-negative number> _
2330 if (Index == 0)
2331 Out << "T_";
2332 else
2333 Out << 'T' << (Index - 1) << '_';
2334}
2335
Anders Carlsson76967372009-09-17 00:43:46 +00002336// <substitution> ::= S <seq-id> _
2337// ::= S_
Anders Carlsson6862fc72009-09-17 04:16:28 +00002338bool CXXNameMangler::mangleSubstitution(const NamedDecl *ND) {
Anders Carlssone7c8cb62009-09-26 20:53:44 +00002339 // Try one of the standard substitutions first.
2340 if (mangleStandardSubstitution(ND))
2341 return true;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002342
Anders Carlsson433d1372009-11-07 04:26:04 +00002343 ND = cast<NamedDecl>(ND->getCanonicalDecl());
Anders Carlsson6862fc72009-09-17 04:16:28 +00002344 return mangleSubstitution(reinterpret_cast<uintptr_t>(ND));
2345}
2346
Anders Carlsson76967372009-09-17 00:43:46 +00002347bool CXXNameMangler::mangleSubstitution(QualType T) {
Anders Carlssond99edc42009-09-26 03:55:37 +00002348 if (!T.getCVRQualifiers()) {
2349 if (const RecordType *RT = T->getAs<RecordType>())
2350 return mangleSubstitution(RT->getDecl());
2351 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002352
Anders Carlsson76967372009-09-17 00:43:46 +00002353 uintptr_t TypePtr = reinterpret_cast<uintptr_t>(T.getAsOpaquePtr());
2354
Anders Carlssond3a932a2009-09-17 03:53:28 +00002355 return mangleSubstitution(TypePtr);
2356}
2357
Douglas Gregor1e9268e2010-04-28 05:58:56 +00002358bool CXXNameMangler::mangleSubstitution(TemplateName Template) {
2359 if (TemplateDecl *TD = Template.getAsTemplateDecl())
2360 return mangleSubstitution(TD);
Sean Huntc3021132010-05-05 15:23:54 +00002361
Douglas Gregor1e9268e2010-04-28 05:58:56 +00002362 Template = Context.getASTContext().getCanonicalTemplateName(Template);
2363 return mangleSubstitution(
2364 reinterpret_cast<uintptr_t>(Template.getAsVoidPointer()));
2365}
2366
Anders Carlssond3a932a2009-09-17 03:53:28 +00002367bool CXXNameMangler::mangleSubstitution(uintptr_t Ptr) {
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002368 llvm::DenseMap<uintptr_t, unsigned>::iterator I = Substitutions.find(Ptr);
Anders Carlsson76967372009-09-17 00:43:46 +00002369 if (I == Substitutions.end())
2370 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002371
Anders Carlsson76967372009-09-17 00:43:46 +00002372 unsigned SeqID = I->second;
2373 if (SeqID == 0)
2374 Out << "S_";
2375 else {
2376 SeqID--;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002377
Anders Carlsson76967372009-09-17 00:43:46 +00002378 // <seq-id> is encoded in base-36, using digits and upper case letters.
2379 char Buffer[10];
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002380 char *BufferPtr = llvm::array_endof(Buffer);
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002381
Anders Carlsson76967372009-09-17 00:43:46 +00002382 if (SeqID == 0) *--BufferPtr = '0';
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002383
Anders Carlsson76967372009-09-17 00:43:46 +00002384 while (SeqID) {
2385 assert(BufferPtr > Buffer && "Buffer overflow!");
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002386
John McCall6ab30e02010-06-09 07:26:17 +00002387 char c = static_cast<char>(SeqID % 36);
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002388
Anders Carlsson76967372009-09-17 00:43:46 +00002389 *--BufferPtr = (c < 10 ? '0' + c : 'A' + c - 10);
2390 SeqID /= 36;
2391 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002392
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002393 Out << 'S'
2394 << llvm::StringRef(BufferPtr, llvm::array_endof(Buffer)-BufferPtr)
2395 << '_';
Anders Carlsson76967372009-09-17 00:43:46 +00002396 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002397
Anders Carlsson76967372009-09-17 00:43:46 +00002398 return true;
2399}
2400
Anders Carlssonf514b542009-09-27 00:12:57 +00002401static bool isCharType(QualType T) {
2402 if (T.isNull())
2403 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002404
Anders Carlssonf514b542009-09-27 00:12:57 +00002405 return T->isSpecificBuiltinType(BuiltinType::Char_S) ||
2406 T->isSpecificBuiltinType(BuiltinType::Char_U);
2407}
2408
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002409/// isCharSpecialization - Returns whether a given type is a template
Anders Carlssonf514b542009-09-27 00:12:57 +00002410/// specialization of a given name with a single argument of type char.
2411static bool isCharSpecialization(QualType T, const char *Name) {
2412 if (T.isNull())
2413 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002414
Anders Carlssonf514b542009-09-27 00:12:57 +00002415 const RecordType *RT = T->getAs<RecordType>();
2416 if (!RT)
2417 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002418
2419 const ClassTemplateSpecializationDecl *SD =
Anders Carlssonf514b542009-09-27 00:12:57 +00002420 dyn_cast<ClassTemplateSpecializationDecl>(RT->getDecl());
2421 if (!SD)
2422 return false;
2423
2424 if (!isStdNamespace(SD->getDeclContext()))
2425 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002426
Anders Carlssonf514b542009-09-27 00:12:57 +00002427 const TemplateArgumentList &TemplateArgs = SD->getTemplateArgs();
2428 if (TemplateArgs.size() != 1)
2429 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002430
Anders Carlssonf514b542009-09-27 00:12:57 +00002431 if (!isCharType(TemplateArgs[0].getAsType()))
2432 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002433
Daniel Dunbar01eb9b92009-10-18 21:17:35 +00002434 return SD->getIdentifier()->getName() == Name;
Anders Carlssonf514b542009-09-27 00:12:57 +00002435}
2436
Anders Carlsson91f88602009-12-07 19:56:42 +00002437template <std::size_t StrLen>
Benjamin Kramer54353f42010-11-25 18:29:30 +00002438static bool isStreamCharSpecialization(const ClassTemplateSpecializationDecl*SD,
2439 const char (&Str)[StrLen]) {
Anders Carlsson91f88602009-12-07 19:56:42 +00002440 if (!SD->getIdentifier()->isStr(Str))
2441 return false;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002442
Anders Carlsson91f88602009-12-07 19:56:42 +00002443 const TemplateArgumentList &TemplateArgs = SD->getTemplateArgs();
2444 if (TemplateArgs.size() != 2)
2445 return false;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002446
Anders Carlsson91f88602009-12-07 19:56:42 +00002447 if (!isCharType(TemplateArgs[0].getAsType()))
2448 return false;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002449
Anders Carlsson91f88602009-12-07 19:56:42 +00002450 if (!isCharSpecialization(TemplateArgs[1].getAsType(), "char_traits"))
2451 return false;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002452
Anders Carlsson91f88602009-12-07 19:56:42 +00002453 return true;
2454}
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002455
Anders Carlssone7c8cb62009-09-26 20:53:44 +00002456bool CXXNameMangler::mangleStandardSubstitution(const NamedDecl *ND) {
2457 // <substitution> ::= St # ::std::
Anders Carlsson8c031552009-09-26 23:10:05 +00002458 if (const NamespaceDecl *NS = dyn_cast<NamespaceDecl>(ND)) {
Anders Carlsson47846d22009-12-04 06:23:23 +00002459 if (isStd(NS)) {
Anders Carlsson8c031552009-09-26 23:10:05 +00002460 Out << "St";
2461 return true;
2462 }
2463 }
2464
2465 if (const ClassTemplateDecl *TD = dyn_cast<ClassTemplateDecl>(ND)) {
2466 if (!isStdNamespace(TD->getDeclContext()))
2467 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002468
Anders Carlsson8c031552009-09-26 23:10:05 +00002469 // <substitution> ::= Sa # ::std::allocator
2470 if (TD->getIdentifier()->isStr("allocator")) {
2471 Out << "Sa";
2472 return true;
2473 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002474
Anders Carlsson189d59c2009-09-26 23:14:39 +00002475 // <<substitution> ::= Sb # ::std::basic_string
2476 if (TD->getIdentifier()->isStr("basic_string")) {
2477 Out << "Sb";
2478 return true;
2479 }
Anders Carlsson8c031552009-09-26 23:10:05 +00002480 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002481
2482 if (const ClassTemplateSpecializationDecl *SD =
Anders Carlssonf514b542009-09-27 00:12:57 +00002483 dyn_cast<ClassTemplateSpecializationDecl>(ND)) {
Eli Friedman5370ee22010-02-23 18:25:09 +00002484 if (!isStdNamespace(SD->getDeclContext()))
2485 return false;
2486
Anders Carlssonf514b542009-09-27 00:12:57 +00002487 // <substitution> ::= Ss # ::std::basic_string<char,
2488 // ::std::char_traits<char>,
2489 // ::std::allocator<char> >
2490 if (SD->getIdentifier()->isStr("basic_string")) {
2491 const TemplateArgumentList &TemplateArgs = SD->getTemplateArgs();
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002492
Anders Carlssonf514b542009-09-27 00:12:57 +00002493 if (TemplateArgs.size() != 3)
2494 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002495
Anders Carlssonf514b542009-09-27 00:12:57 +00002496 if (!isCharType(TemplateArgs[0].getAsType()))
2497 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002498
Anders Carlssonf514b542009-09-27 00:12:57 +00002499 if (!isCharSpecialization(TemplateArgs[1].getAsType(), "char_traits"))
2500 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002501
Anders Carlssonf514b542009-09-27 00:12:57 +00002502 if (!isCharSpecialization(TemplateArgs[2].getAsType(), "allocator"))
2503 return false;
2504
2505 Out << "Ss";
2506 return true;
2507 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002508
Anders Carlsson91f88602009-12-07 19:56:42 +00002509 // <substitution> ::= Si # ::std::basic_istream<char,
2510 // ::std::char_traits<char> >
2511 if (isStreamCharSpecialization(SD, "basic_istream")) {
2512 Out << "Si";
2513 return true;
2514 }
2515
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002516 // <substitution> ::= So # ::std::basic_ostream<char,
Anders Carlsson8f8fd8e2009-10-08 17:20:26 +00002517 // ::std::char_traits<char> >
Anders Carlsson91f88602009-12-07 19:56:42 +00002518 if (isStreamCharSpecialization(SD, "basic_ostream")) {
Anders Carlsson8f8fd8e2009-10-08 17:20:26 +00002519 Out << "So";
2520 return true;
2521 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002522
Anders Carlsson91f88602009-12-07 19:56:42 +00002523 // <substitution> ::= Sd # ::std::basic_iostream<char,
2524 // ::std::char_traits<char> >
2525 if (isStreamCharSpecialization(SD, "basic_iostream")) {
2526 Out << "Sd";
2527 return true;
2528 }
Anders Carlssonf514b542009-09-27 00:12:57 +00002529 }
Anders Carlsson8c031552009-09-26 23:10:05 +00002530 return false;
Anders Carlssone7c8cb62009-09-26 20:53:44 +00002531}
2532
Anders Carlsson76967372009-09-17 00:43:46 +00002533void CXXNameMangler::addSubstitution(QualType T) {
Anders Carlssond99edc42009-09-26 03:55:37 +00002534 if (!T.getCVRQualifiers()) {
2535 if (const RecordType *RT = T->getAs<RecordType>()) {
2536 addSubstitution(RT->getDecl());
2537 return;
2538 }
2539 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002540
Anders Carlsson76967372009-09-17 00:43:46 +00002541 uintptr_t TypePtr = reinterpret_cast<uintptr_t>(T.getAsOpaquePtr());
Anders Carlssond3a932a2009-09-17 03:53:28 +00002542 addSubstitution(TypePtr);
2543}
2544
Douglas Gregor1e9268e2010-04-28 05:58:56 +00002545void CXXNameMangler::addSubstitution(TemplateName Template) {
2546 if (TemplateDecl *TD = Template.getAsTemplateDecl())
2547 return addSubstitution(TD);
Sean Huntc3021132010-05-05 15:23:54 +00002548
Douglas Gregor1e9268e2010-04-28 05:58:56 +00002549 Template = Context.getASTContext().getCanonicalTemplateName(Template);
2550 addSubstitution(reinterpret_cast<uintptr_t>(Template.getAsVoidPointer()));
2551}
2552
Anders Carlssond3a932a2009-09-17 03:53:28 +00002553void CXXNameMangler::addSubstitution(uintptr_t Ptr) {
Anders Carlssond3a932a2009-09-17 03:53:28 +00002554 assert(!Substitutions.count(Ptr) && "Substitution already exists!");
Anders Carlsson9d85b722010-06-02 04:29:50 +00002555 Substitutions[Ptr] = SeqID++;
Anders Carlsson76967372009-09-17 00:43:46 +00002556}
2557
Daniel Dunbar1b077112009-11-21 09:06:10 +00002558//
Mike Stump1eb44332009-09-09 15:08:12 +00002559
Daniel Dunbar1b077112009-11-21 09:06:10 +00002560/// \brief Mangles the name of the declaration D and emits that name to the
2561/// given output stream.
2562///
2563/// If the declaration D requires a mangled name, this routine will emit that
2564/// mangled name to \p os and return true. Otherwise, \p os will be unchanged
2565/// and this routine will return false. In this case, the caller should just
2566/// emit the identifier of the declaration (\c D->getIdentifier()) as its
2567/// name.
Peter Collingbourne14110472011-01-13 18:57:25 +00002568void ItaniumMangleContext::mangleName(const NamedDecl *D,
Rafael Espindola0e376a02011-02-11 01:41:00 +00002569 llvm::raw_ostream &Out) {
Daniel Dunbarc02ab4c2009-11-21 09:14:44 +00002570 assert((isa<FunctionDecl>(D) || isa<VarDecl>(D)) &&
2571 "Invalid mangleName() call, argument is not a variable or function!");
2572 assert(!isa<CXXConstructorDecl>(D) && !isa<CXXDestructorDecl>(D) &&
2573 "Invalid mangleName() call on 'structor decl!");
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002574
Daniel Dunbar1b077112009-11-21 09:06:10 +00002575 PrettyStackTraceDecl CrashInfo(D, SourceLocation(),
2576 getASTContext().getSourceManager(),
2577 "Mangling declaration");
Mike Stump1eb44332009-09-09 15:08:12 +00002578
Rafael Espindolac4850c22011-02-10 23:59:36 +00002579 CXXNameMangler Mangler(*this, Out);
Daniel Dunbar94fd26d2009-11-21 09:06:22 +00002580 return Mangler.mangle(D);
Daniel Dunbar1b077112009-11-21 09:06:10 +00002581}
Mike Stump1eb44332009-09-09 15:08:12 +00002582
Peter Collingbourne14110472011-01-13 18:57:25 +00002583void ItaniumMangleContext::mangleCXXCtor(const CXXConstructorDecl *D,
2584 CXXCtorType Type,
Rafael Espindola0e376a02011-02-11 01:41:00 +00002585 llvm::raw_ostream &Out) {
Rafael Espindolac4850c22011-02-10 23:59:36 +00002586 CXXNameMangler Mangler(*this, Out, D, Type);
Daniel Dunbar77939c92009-11-21 09:06:31 +00002587 Mangler.mangle(D);
Daniel Dunbar1b077112009-11-21 09:06:10 +00002588}
Mike Stump1eb44332009-09-09 15:08:12 +00002589
Peter Collingbourne14110472011-01-13 18:57:25 +00002590void ItaniumMangleContext::mangleCXXDtor(const CXXDestructorDecl *D,
2591 CXXDtorType Type,
Rafael Espindola0e376a02011-02-11 01:41:00 +00002592 llvm::raw_ostream &Out) {
Rafael Espindolac4850c22011-02-10 23:59:36 +00002593 CXXNameMangler Mangler(*this, Out, D, Type);
Daniel Dunbar77939c92009-11-21 09:06:31 +00002594 Mangler.mangle(D);
Daniel Dunbar1b077112009-11-21 09:06:10 +00002595}
Mike Stumpf1216772009-07-31 18:25:34 +00002596
Peter Collingbourne14110472011-01-13 18:57:25 +00002597void ItaniumMangleContext::mangleThunk(const CXXMethodDecl *MD,
2598 const ThunkInfo &Thunk,
Rafael Espindolaf0be9792011-02-11 02:52:17 +00002599 llvm::raw_ostream &Out) {
Anders Carlsson19879c92010-03-23 17:17:29 +00002600 // <special-name> ::= T <call-offset> <base encoding>
2601 // # base is the nominal target function of thunk
2602 // <special-name> ::= Tc <call-offset> <call-offset> <base encoding>
2603 // # base is the nominal target function of thunk
2604 // # first call-offset is 'this' adjustment
2605 // # second call-offset is result adjustment
Sean Huntc3021132010-05-05 15:23:54 +00002606
Anders Carlsson19879c92010-03-23 17:17:29 +00002607 assert(!isa<CXXDestructorDecl>(MD) &&
2608 "Use mangleCXXDtor for destructor decls!");
Rafael Espindolac4850c22011-02-10 23:59:36 +00002609 CXXNameMangler Mangler(*this, Out);
Anders Carlsson19879c92010-03-23 17:17:29 +00002610 Mangler.getStream() << "_ZT";
2611 if (!Thunk.Return.isEmpty())
2612 Mangler.getStream() << 'c';
Sean Huntc3021132010-05-05 15:23:54 +00002613
Anders Carlsson19879c92010-03-23 17:17:29 +00002614 // Mangle the 'this' pointer adjustment.
2615 Mangler.mangleCallOffset(Thunk.This.NonVirtual, Thunk.This.VCallOffsetOffset);
Sean Huntc3021132010-05-05 15:23:54 +00002616
Anders Carlsson19879c92010-03-23 17:17:29 +00002617 // Mangle the return pointer adjustment if there is one.
2618 if (!Thunk.Return.isEmpty())
2619 Mangler.mangleCallOffset(Thunk.Return.NonVirtual,
2620 Thunk.Return.VBaseOffsetOffset);
Sean Huntc3021132010-05-05 15:23:54 +00002621
Anders Carlsson19879c92010-03-23 17:17:29 +00002622 Mangler.mangleFunctionEncoding(MD);
2623}
2624
Sean Huntc3021132010-05-05 15:23:54 +00002625void
Peter Collingbourne14110472011-01-13 18:57:25 +00002626ItaniumMangleContext::mangleCXXDtorThunk(const CXXDestructorDecl *DD,
2627 CXXDtorType Type,
2628 const ThisAdjustment &ThisAdjustment,
Rafael Espindolaf0be9792011-02-11 02:52:17 +00002629 llvm::raw_ostream &Out) {
Anders Carlsson19879c92010-03-23 17:17:29 +00002630 // <special-name> ::= T <call-offset> <base encoding>
2631 // # base is the nominal target function of thunk
Rafael Espindolac4850c22011-02-10 23:59:36 +00002632 CXXNameMangler Mangler(*this, Out, DD, Type);
Anders Carlsson19879c92010-03-23 17:17:29 +00002633 Mangler.getStream() << "_ZT";
2634
2635 // Mangle the 'this' pointer adjustment.
Sean Huntc3021132010-05-05 15:23:54 +00002636 Mangler.mangleCallOffset(ThisAdjustment.NonVirtual,
Anders Carlsson19879c92010-03-23 17:17:29 +00002637 ThisAdjustment.VCallOffsetOffset);
2638
2639 Mangler.mangleFunctionEncoding(DD);
2640}
2641
Daniel Dunbarc0747712009-11-21 09:12:13 +00002642/// mangleGuardVariable - Returns the mangled name for a guard variable
2643/// for the passed in VarDecl.
Peter Collingbourne14110472011-01-13 18:57:25 +00002644void ItaniumMangleContext::mangleItaniumGuardVariable(const VarDecl *D,
Rafael Espindolaf0be9792011-02-11 02:52:17 +00002645 llvm::raw_ostream &Out) {
Daniel Dunbarc0747712009-11-21 09:12:13 +00002646 // <special-name> ::= GV <object name> # Guard variable for one-time
2647 // # initialization
Rafael Espindolac4850c22011-02-10 23:59:36 +00002648 CXXNameMangler Mangler(*this, Out);
Daniel Dunbarc0747712009-11-21 09:12:13 +00002649 Mangler.getStream() << "_ZGV";
2650 Mangler.mangleName(D);
2651}
2652
Peter Collingbourne14110472011-01-13 18:57:25 +00002653void ItaniumMangleContext::mangleReferenceTemporary(const VarDecl *D,
Rafael Espindolaf0be9792011-02-11 02:52:17 +00002654 llvm::raw_ostream &Out) {
Anders Carlsson715edf22010-06-26 16:09:40 +00002655 // We match the GCC mangling here.
2656 // <special-name> ::= GR <object name>
Rafael Espindolac4850c22011-02-10 23:59:36 +00002657 CXXNameMangler Mangler(*this, Out);
Anders Carlsson715edf22010-06-26 16:09:40 +00002658 Mangler.getStream() << "_ZGR";
2659 Mangler.mangleName(D);
2660}
2661
Peter Collingbourne14110472011-01-13 18:57:25 +00002662void ItaniumMangleContext::mangleCXXVTable(const CXXRecordDecl *RD,
Rafael Espindolaf0be9792011-02-11 02:52:17 +00002663 llvm::raw_ostream &Out) {
Daniel Dunbarc0747712009-11-21 09:12:13 +00002664 // <special-name> ::= TV <type> # virtual table
Rafael Espindolac4850c22011-02-10 23:59:36 +00002665 CXXNameMangler Mangler(*this, Out);
Daniel Dunbarc0747712009-11-21 09:12:13 +00002666 Mangler.getStream() << "_ZTV";
Douglas Gregor1b12a3b2010-05-26 05:11:13 +00002667 Mangler.mangleNameOrStandardSubstitution(RD);
Daniel Dunbar1b077112009-11-21 09:06:10 +00002668}
Mike Stump82d75b02009-11-10 01:58:37 +00002669
Peter Collingbourne14110472011-01-13 18:57:25 +00002670void ItaniumMangleContext::mangleCXXVTT(const CXXRecordDecl *RD,
Rafael Espindolaf0be9792011-02-11 02:52:17 +00002671 llvm::raw_ostream &Out) {
Daniel Dunbarc0747712009-11-21 09:12:13 +00002672 // <special-name> ::= TT <type> # VTT structure
Rafael Espindolac4850c22011-02-10 23:59:36 +00002673 CXXNameMangler Mangler(*this, Out);
Daniel Dunbarc0747712009-11-21 09:12:13 +00002674 Mangler.getStream() << "_ZTT";
Douglas Gregor1b12a3b2010-05-26 05:11:13 +00002675 Mangler.mangleNameOrStandardSubstitution(RD);
Daniel Dunbar1b077112009-11-21 09:06:10 +00002676}
Mike Stumpab3f7e92009-11-10 01:41:59 +00002677
Peter Collingbourne14110472011-01-13 18:57:25 +00002678void ItaniumMangleContext::mangleCXXCtorVTable(const CXXRecordDecl *RD,
2679 int64_t Offset,
2680 const CXXRecordDecl *Type,
Rafael Espindolaf0be9792011-02-11 02:52:17 +00002681 llvm::raw_ostream &Out) {
Daniel Dunbarc0747712009-11-21 09:12:13 +00002682 // <special-name> ::= TC <type> <offset number> _ <base type>
Rafael Espindolac4850c22011-02-10 23:59:36 +00002683 CXXNameMangler Mangler(*this, Out);
Daniel Dunbarc0747712009-11-21 09:12:13 +00002684 Mangler.getStream() << "_ZTC";
Douglas Gregor1b12a3b2010-05-26 05:11:13 +00002685 Mangler.mangleNameOrStandardSubstitution(RD);
Daniel Dunbarc0747712009-11-21 09:12:13 +00002686 Mangler.getStream() << Offset;
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002687 Mangler.getStream() << '_';
Douglas Gregor1b12a3b2010-05-26 05:11:13 +00002688 Mangler.mangleNameOrStandardSubstitution(Type);
Daniel Dunbar1b077112009-11-21 09:06:10 +00002689}
Mike Stump738f8c22009-07-31 23:15:31 +00002690
Peter Collingbourne14110472011-01-13 18:57:25 +00002691void ItaniumMangleContext::mangleCXXRTTI(QualType Ty,
Rafael Espindolaf0be9792011-02-11 02:52:17 +00002692 llvm::raw_ostream &Out) {
Daniel Dunbarc0747712009-11-21 09:12:13 +00002693 // <special-name> ::= TI <type> # typeinfo structure
Douglas Gregor154fe982009-12-23 22:04:40 +00002694 assert(!Ty.hasQualifiers() && "RTTI info cannot have top-level qualifiers");
Rafael Espindolac4850c22011-02-10 23:59:36 +00002695 CXXNameMangler Mangler(*this, Out);
Daniel Dunbarc0747712009-11-21 09:12:13 +00002696 Mangler.getStream() << "_ZTI";
2697 Mangler.mangleType(Ty);
Daniel Dunbar1b077112009-11-21 09:06:10 +00002698}
Mike Stump67795982009-11-14 00:14:13 +00002699
Peter Collingbourne14110472011-01-13 18:57:25 +00002700void ItaniumMangleContext::mangleCXXRTTIName(QualType Ty,
Rafael Espindolaf0be9792011-02-11 02:52:17 +00002701 llvm::raw_ostream &Out) {
Daniel Dunbarc0747712009-11-21 09:12:13 +00002702 // <special-name> ::= TS <type> # typeinfo name (null terminated byte string)
Rafael Espindolac4850c22011-02-10 23:59:36 +00002703 CXXNameMangler Mangler(*this, Out);
Daniel Dunbarc0747712009-11-21 09:12:13 +00002704 Mangler.getStream() << "_ZTS";
2705 Mangler.mangleType(Ty);
Mike Stumpf1216772009-07-31 18:25:34 +00002706}
Peter Collingbourne14110472011-01-13 18:57:25 +00002707
2708MangleContext *clang::createItaniumMangleContext(ASTContext &Context,
2709 Diagnostic &Diags) {
2710 return new ItaniumMangleContext(Context, Diags);
2711}