blob: bceed0813789816177ec646d88a8ae52c30fcc5b [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 }
Douglas Gregoraa2187d2011-02-28 00:04:36 +0000628 } else if (const DependentTemplateSpecializationType *DTST
629 = dyn_cast<DependentTemplateSpecializationType>(QTy)) {
630 TemplateName Template
631 = getASTContext().getDependentTemplateName(DTST->getQualifier(),
632 DTST->getIdentifier());
633 mangleTemplatePrefix(Template);
634
635 // FIXME: GCC does not appear to mangle the template arguments when
636 // the template in question is a dependent template name. Should we
637 // emulate that badness?
638 mangleTemplateArgs(Template, DTST->getArgs(), DTST->getNumArgs());
Rafael Espindola9b35b252010-03-17 04:28:11 +0000639 } else {
640 // We use the QualType mangle type variant here because it handles
641 // substitutions.
642 mangleType(QualType(QTy, 0));
643 }
644 }
John McCall1dd73832010-02-04 01:42:13 +0000645 break;
646 case NestedNameSpecifier::Identifier:
John McCallad5e7382010-03-01 23:49:17 +0000647 // Member expressions can have these without prefixes.
648 if (Qualifier->getPrefix())
649 mangleUnresolvedScope(Qualifier->getPrefix());
John McCall1dd73832010-02-04 01:42:13 +0000650 mangleSourceName(Qualifier->getAsIdentifier());
651 break;
652 }
653}
654
655/// Mangles a name which was not resolved to a specific entity.
656void CXXNameMangler::mangleUnresolvedName(NestedNameSpecifier *Qualifier,
657 DeclarationName Name,
658 unsigned KnownArity) {
659 if (Qualifier)
660 mangleUnresolvedScope(Qualifier);
661 // FIXME: ambiguity of unqualified lookup with ::
662
663 mangleUnqualifiedName(0, Name, KnownArity);
664}
665
Anders Carlsson6f7e2f42010-06-08 14:49:03 +0000666static const FieldDecl *FindFirstNamedDataMember(const RecordDecl *RD) {
667 assert(RD->isAnonymousStructOrUnion() &&
668 "Expected anonymous struct or union!");
669
670 for (RecordDecl::field_iterator I = RD->field_begin(), E = RD->field_end();
671 I != E; ++I) {
672 const FieldDecl *FD = *I;
673
674 if (FD->getIdentifier())
675 return FD;
676
677 if (const RecordType *RT = FD->getType()->getAs<RecordType>()) {
678 if (const FieldDecl *NamedDataMember =
679 FindFirstNamedDataMember(RT->getDecl()))
680 return NamedDataMember;
681 }
682 }
683
684 // We didn't find a named data member.
685 return 0;
686}
687
John McCall1dd73832010-02-04 01:42:13 +0000688void CXXNameMangler::mangleUnqualifiedName(const NamedDecl *ND,
689 DeclarationName Name,
690 unsigned KnownArity) {
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000691 // <unqualified-name> ::= <operator-name>
Mike Stump1eb44332009-09-09 15:08:12 +0000692 // ::= <ctor-dtor-name>
693 // ::= <source-name>
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000694 switch (Name.getNameKind()) {
Anders Carlssonc4355b62009-10-07 01:45:02 +0000695 case DeclarationName::Identifier: {
Anders Carlssonc4355b62009-10-07 01:45:02 +0000696 if (const IdentifierInfo *II = Name.getAsIdentifierInfo()) {
Sean Hunt31455252010-01-24 03:04:27 +0000697 // We must avoid conflicts between internally- and externally-
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000698 // linked variable declaration names in the same TU.
Anders Carlssonaec25232010-02-06 04:52:27 +0000699 // This naming convention is the same as that followed by GCC, though it
700 // shouldn't actually matter.
701 if (ND && isa<VarDecl>(ND) && ND->getLinkage() == InternalLinkage &&
Sean Hunt31455252010-01-24 03:04:27 +0000702 ND->getDeclContext()->isFileContext())
703 Out << 'L';
704
Anders Carlssonc4355b62009-10-07 01:45:02 +0000705 mangleSourceName(II);
706 break;
707 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000708
John McCall1dd73832010-02-04 01:42:13 +0000709 // Otherwise, an anonymous entity. We must have a declaration.
710 assert(ND && "mangling empty name without declaration");
711
712 if (const NamespaceDecl *NS = dyn_cast<NamespaceDecl>(ND)) {
713 if (NS->isAnonymousNamespace()) {
714 // This is how gcc mangles these names.
715 Out << "12_GLOBAL__N_1";
716 break;
717 }
718 }
719
Anders Carlsson6f7e2f42010-06-08 14:49:03 +0000720 if (const VarDecl *VD = dyn_cast<VarDecl>(ND)) {
721 // We must have an anonymous union or struct declaration.
722 const RecordDecl *RD =
723 cast<RecordDecl>(VD->getType()->getAs<RecordType>()->getDecl());
724
725 // Itanium C++ ABI 5.1.2:
726 //
727 // For the purposes of mangling, the name of an anonymous union is
728 // considered to be the name of the first named data member found by a
729 // pre-order, depth-first, declaration-order walk of the data members of
730 // the anonymous union. If there is no such data member (i.e., if all of
731 // the data members in the union are unnamed), then there is no way for
732 // a program to refer to the anonymous union, and there is therefore no
733 // need to mangle its name.
734 const FieldDecl *FD = FindFirstNamedDataMember(RD);
John McCall7121c8f2010-08-05 22:02:13 +0000735
736 // It's actually possible for various reasons for us to get here
737 // with an empty anonymous struct / union. Fortunately, it
738 // doesn't really matter what name we generate.
739 if (!FD) break;
Anders Carlsson6f7e2f42010-06-08 14:49:03 +0000740 assert(FD->getIdentifier() && "Data member name isn't an identifier!");
741
742 mangleSourceName(FD->getIdentifier());
743 break;
744 }
745
Anders Carlssonc4355b62009-10-07 01:45:02 +0000746 // We must have an anonymous struct.
747 const TagDecl *TD = cast<TagDecl>(ND);
748 if (const TypedefDecl *D = TD->getTypedefForAnonDecl()) {
749 assert(TD->getDeclContext() == D->getDeclContext() &&
750 "Typedef should not be in another decl context!");
751 assert(D->getDeclName().getAsIdentifierInfo() &&
752 "Typedef was not named!");
753 mangleSourceName(D->getDeclName().getAsIdentifierInfo());
754 break;
755 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000756
Anders Carlssonc4355b62009-10-07 01:45:02 +0000757 // Get a unique id for the anonymous struct.
758 uint64_t AnonStructId = Context.getAnonymousStructId(TD);
759
760 // Mangle it as a source name in the form
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000761 // [n] $_<id>
Anders Carlssonc4355b62009-10-07 01:45:02 +0000762 // where n is the length of the string.
763 llvm::SmallString<8> Str;
764 Str += "$_";
765 Str += llvm::utostr(AnonStructId);
766
767 Out << Str.size();
768 Out << Str.str();
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000769 break;
Anders Carlssonc4355b62009-10-07 01:45:02 +0000770 }
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000771
772 case DeclarationName::ObjCZeroArgSelector:
773 case DeclarationName::ObjCOneArgSelector:
774 case DeclarationName::ObjCMultiArgSelector:
775 assert(false && "Can't mangle Objective-C selector names here!");
776 break;
777
778 case DeclarationName::CXXConstructorName:
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++ constructor we're mangling, use the type
781 // we were given.
Anders Carlsson27ae5362009-04-17 01:58:57 +0000782 mangleCXXCtorType(static_cast<CXXCtorType>(StructorType));
Anders Carlsson3ac86b52009-04-15 05:36:58 +0000783 else
784 // Otherwise, use the complete constructor name. This is relevant if a
785 // class with a constructor is declared within a constructor.
786 mangleCXXCtorType(Ctor_Complete);
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000787 break;
788
789 case DeclarationName::CXXDestructorName:
Anders Carlsson27ae5362009-04-17 01:58:57 +0000790 if (ND == Structor)
Mike Stump141c5af2009-09-02 00:25:38 +0000791 // If the named decl is the C++ destructor we're mangling, use the type we
792 // were given.
Anders Carlsson27ae5362009-04-17 01:58:57 +0000793 mangleCXXDtorType(static_cast<CXXDtorType>(StructorType));
794 else
795 // Otherwise, use the complete destructor name. This is relevant if a
796 // class with a destructor is declared within a destructor.
797 mangleCXXDtorType(Dtor_Complete);
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000798 break;
799
800 case DeclarationName::CXXConversionFunctionName:
Mike Stump1eb44332009-09-09 15:08:12 +0000801 // <operator-name> ::= cv <type> # (cast)
Douglas Gregor219cc612009-02-13 01:28:03 +0000802 Out << "cv";
Anders Carlssonb5404912009-10-07 01:06:45 +0000803 mangleType(Context.getASTContext().getCanonicalType(Name.getCXXNameType()));
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000804 break;
805
Anders Carlsson8257d412009-12-22 06:36:32 +0000806 case DeclarationName::CXXOperatorName: {
John McCall1dd73832010-02-04 01:42:13 +0000807 unsigned Arity;
808 if (ND) {
809 Arity = cast<FunctionDecl>(ND)->getNumParams();
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000810
John McCall1dd73832010-02-04 01:42:13 +0000811 // If we have a C++ member function, we need to include the 'this' pointer.
812 // FIXME: This does not make sense for operators that are static, but their
813 // names stay the same regardless of the arity (operator new for instance).
814 if (isa<CXXMethodDecl>(ND))
815 Arity++;
816 } else
817 Arity = KnownArity;
818
Anders Carlsson8257d412009-12-22 06:36:32 +0000819 mangleOperatorName(Name.getCXXOverloadedOperator(), Arity);
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000820 break;
Anders Carlsson8257d412009-12-22 06:36:32 +0000821 }
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000822
Sean Hunt3e518bd2009-11-29 07:34:05 +0000823 case DeclarationName::CXXLiteralOperatorName:
Sean Hunt5dd6b392009-12-04 21:11:13 +0000824 // FIXME: This mangling is not yet official.
Sean Hunt2421f662009-12-04 21:01:37 +0000825 Out << "li";
Sean Hunt3e518bd2009-11-29 07:34:05 +0000826 mangleSourceName(Name.getCXXLiteralIdentifier());
827 break;
828
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000829 case DeclarationName::CXXUsingDirective:
830 assert(false && "Can't mangle a using directive name!");
Douglas Gregor219cc612009-02-13 01:28:03 +0000831 break;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000832 }
833}
834
835void CXXNameMangler::mangleSourceName(const IdentifierInfo *II) {
836 // <source-name> ::= <positive length number> <identifier>
837 // <number> ::= [n] <non-negative decimal integer>
838 // <identifier> ::= <unqualified source code identifier>
839 Out << II->getLength() << II->getName();
840}
841
Eli Friedman7facf842009-12-02 20:32:49 +0000842void CXXNameMangler::mangleNestedName(const NamedDecl *ND,
Fariborz Jahanian57058532010-03-03 19:41:08 +0000843 const DeclContext *DC,
844 bool NoFunction) {
Douglas Gregor0a9a6d62011-01-26 17:36:28 +0000845 // <nested-name>
846 // ::= N [<CV-qualifiers>] [<ref-qualifier>] <prefix> <unqualified-name> E
847 // ::= N [<CV-qualifiers>] [<ref-qualifier>] <template-prefix>
848 // <template-args> E
Anders Carlssond99edc42009-09-26 03:55:37 +0000849
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000850 Out << 'N';
Douglas Gregor0a9a6d62011-01-26 17:36:28 +0000851 if (const CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(ND)) {
John McCall0953e762009-09-24 19:53:00 +0000852 mangleQualifiers(Qualifiers::fromCVRMask(Method->getTypeQualifiers()));
Douglas Gregor0a9a6d62011-01-26 17:36:28 +0000853 mangleRefQualifier(Method->getRefQualifier());
854 }
855
Anders Carlsson2744a062009-09-18 19:00:18 +0000856 // Check if we have a template.
857 const TemplateArgumentList *TemplateArgs = 0;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000858 if (const TemplateDecl *TD = isTemplate(ND, TemplateArgs)) {
Anders Carlsson2744a062009-09-18 19:00:18 +0000859 mangleTemplatePrefix(TD);
Rafael Espindolad9800722010-03-11 14:07:00 +0000860 TemplateParameterList *TemplateParameters = TD->getTemplateParameters();
861 mangleTemplateArgs(*TemplateParameters, *TemplateArgs);
Fariborz Jahanian57058532010-03-03 19:41:08 +0000862 }
863 else {
864 manglePrefix(DC, NoFunction);
Anders Carlsson7482e242009-09-18 04:29:09 +0000865 mangleUnqualifiedName(ND);
866 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000867
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000868 Out << 'E';
869}
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000870void CXXNameMangler::mangleNestedName(const TemplateDecl *TD,
Anders Carlsson7624f212009-09-18 02:42:01 +0000871 const TemplateArgument *TemplateArgs,
872 unsigned NumTemplateArgs) {
Anders Carlssone45117b2009-09-27 19:53:49 +0000873 // <nested-name> ::= N [<CV-qualifiers>] <template-prefix> <template-args> E
874
Anders Carlsson7624f212009-09-18 02:42:01 +0000875 Out << 'N';
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000876
Anders Carlssone45117b2009-09-27 19:53:49 +0000877 mangleTemplatePrefix(TD);
Rafael Espindolad9800722010-03-11 14:07:00 +0000878 TemplateParameterList *TemplateParameters = TD->getTemplateParameters();
879 mangleTemplateArgs(*TemplateParameters, TemplateArgs, NumTemplateArgs);
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000880
Anders Carlsson7624f212009-09-18 02:42:01 +0000881 Out << 'E';
882}
883
Anders Carlsson1b42c792009-04-02 16:24:45 +0000884void CXXNameMangler::mangleLocalName(const NamedDecl *ND) {
885 // <local-name> := Z <function encoding> E <entity name> [<discriminator>]
886 // := Z <function encoding> E s [<discriminator>]
Mike Stump1eb44332009-09-09 15:08:12 +0000887 // <discriminator> := _ <non-negative number>
Fariborz Jahanian57058532010-03-03 19:41:08 +0000888 const DeclContext *DC = ND->getDeclContext();
Anders Carlsson1b42c792009-04-02 16:24:45 +0000889 Out << 'Z';
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000890
Charles Davis685b1d92010-05-26 18:25:27 +0000891 if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(DC)) {
892 mangleObjCMethodName(MD);
John McCall82b7d7b2010-10-18 21:28:44 +0000893 } else if (const CXXRecordDecl *RD = GetLocalClassDecl(ND)) {
894 mangleFunctionEncoding(cast<FunctionDecl>(RD->getDeclContext()));
Fariborz Jahanian57058532010-03-03 19:41:08 +0000895 Out << 'E';
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000896
John McCall82b7d7b2010-10-18 21:28:44 +0000897 // Mangle the name relative to the closest enclosing function.
898 if (ND == RD) // equality ok because RD derived from ND above
899 mangleUnqualifiedName(ND);
900 else
901 mangleNestedName(ND, DC, true /*NoFunction*/);
902
Fariborz Jahanian4819ac42010-03-04 01:02:03 +0000903 unsigned disc;
John McCall82b7d7b2010-10-18 21:28:44 +0000904 if (Context.getNextDiscriminator(RD, disc)) {
Fariborz Jahanian4819ac42010-03-04 01:02:03 +0000905 if (disc < 10)
906 Out << '_' << disc;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000907 else
Fariborz Jahanian4819ac42010-03-04 01:02:03 +0000908 Out << "__" << disc << '_';
909 }
Fariborz Jahanian57058532010-03-03 19:41:08 +0000910
911 return;
912 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000913 else
Fariborz Jahanian57058532010-03-03 19:41:08 +0000914 mangleFunctionEncoding(cast<FunctionDecl>(DC));
Anders Carlsson7b06f6c2009-12-10 03:14:39 +0000915
Anders Carlsson1b42c792009-04-02 16:24:45 +0000916 Out << 'E';
Eli Friedman6f9f25d2009-12-11 20:21:38 +0000917 mangleUnqualifiedName(ND);
Anders Carlsson1b42c792009-04-02 16:24:45 +0000918}
919
Fariborz Jahanian57058532010-03-03 19:41:08 +0000920void CXXNameMangler::manglePrefix(const DeclContext *DC, bool NoFunction) {
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000921 // <prefix> ::= <prefix> <unqualified-name>
922 // ::= <template-prefix> <template-args>
923 // ::= <template-param>
924 // ::= # empty
925 // ::= <substitution>
Anders Carlsson6862fc72009-09-17 04:16:28 +0000926
Anders Carlssonadd28822009-09-22 20:33:31 +0000927 while (isa<LinkageSpecDecl>(DC))
928 DC = DC->getParent();
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000929
Anders Carlsson9263e912009-09-18 18:39:58 +0000930 if (DC->isTranslationUnit())
931 return;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000932
Douglas Gregor35415f52010-05-25 17:04:15 +0000933 if (const BlockDecl *Block = dyn_cast<BlockDecl>(DC)) {
934 manglePrefix(DC->getParent(), NoFunction);
935 llvm::SmallString<64> Name;
Rafael Espindolac4850c22011-02-10 23:59:36 +0000936 llvm::raw_svector_ostream NameStream(Name);
937 Context.mangleBlock(Block, NameStream);
938 NameStream.flush();
Douglas Gregor35415f52010-05-25 17:04:15 +0000939 Out << Name.size() << Name;
940 return;
941 }
942
Anders Carlsson6862fc72009-09-17 04:16:28 +0000943 if (mangleSubstitution(cast<NamedDecl>(DC)))
944 return;
Anders Carlsson7482e242009-09-18 04:29:09 +0000945
Anders Carlsson2ee3fca2009-09-18 20:11:09 +0000946 // Check if we have a template.
947 const TemplateArgumentList *TemplateArgs = 0;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000948 if (const TemplateDecl *TD = isTemplate(cast<NamedDecl>(DC), TemplateArgs)) {
Anders Carlsson2ee3fca2009-09-18 20:11:09 +0000949 mangleTemplatePrefix(TD);
Rafael Espindolad9800722010-03-11 14:07:00 +0000950 TemplateParameterList *TemplateParameters = TD->getTemplateParameters();
951 mangleTemplateArgs(*TemplateParameters, *TemplateArgs);
Fariborz Jahanian57058532010-03-03 19:41:08 +0000952 }
Douglas Gregor35415f52010-05-25 17:04:15 +0000953 else if(NoFunction && (isa<FunctionDecl>(DC) || isa<ObjCMethodDecl>(DC)))
Fariborz Jahanian57058532010-03-03 19:41:08 +0000954 return;
Douglas Gregor35415f52010-05-25 17:04:15 +0000955 else if (const ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(DC))
956 mangleObjCMethodName(Method);
Fariborz Jahanian57058532010-03-03 19:41:08 +0000957 else {
958 manglePrefix(DC->getParent(), NoFunction);
Anders Carlsson2ee3fca2009-09-18 20:11:09 +0000959 mangleUnqualifiedName(cast<NamedDecl>(DC));
960 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000961
Anders Carlsson6862fc72009-09-17 04:16:28 +0000962 addSubstitution(cast<NamedDecl>(DC));
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000963}
964
Douglas Gregor20f0cc72010-04-23 03:10:43 +0000965void CXXNameMangler::mangleTemplatePrefix(TemplateName Template) {
966 // <template-prefix> ::= <prefix> <template unqualified-name>
967 // ::= <template-param>
968 // ::= <substitution>
969 if (TemplateDecl *TD = Template.getAsTemplateDecl())
970 return mangleTemplatePrefix(TD);
971
972 if (QualifiedTemplateName *Qualified = Template.getAsQualifiedTemplateName())
973 mangleUnresolvedScope(Qualified->getQualifier());
Sean Huntc3021132010-05-05 15:23:54 +0000974
Douglas Gregor20f0cc72010-04-23 03:10:43 +0000975 if (OverloadedTemplateStorage *Overloaded
976 = Template.getAsOverloadedTemplate()) {
Sean Huntc3021132010-05-05 15:23:54 +0000977 mangleUnqualifiedName(0, (*Overloaded->begin())->getDeclName(),
Douglas Gregor20f0cc72010-04-23 03:10:43 +0000978 UnknownArity);
979 return;
980 }
Sean Huntc3021132010-05-05 15:23:54 +0000981
Douglas Gregor20f0cc72010-04-23 03:10:43 +0000982 DependentTemplateName *Dependent = Template.getAsDependentTemplateName();
983 assert(Dependent && "Unknown template name kind?");
984 mangleUnresolvedScope(Dependent->getQualifier());
Douglas Gregor1e9268e2010-04-28 05:58:56 +0000985 mangleUnscopedTemplateName(Template);
Douglas Gregor20f0cc72010-04-23 03:10:43 +0000986}
987
Anders Carlsson0fa6df42009-09-26 19:45:45 +0000988void CXXNameMangler::mangleTemplatePrefix(const TemplateDecl *ND) {
Anders Carlsson7482e242009-09-18 04:29:09 +0000989 // <template-prefix> ::= <prefix> <template unqualified-name>
990 // ::= <template-param>
991 // ::= <substitution>
Douglas Gregor32fb4e12010-02-05 20:45:00 +0000992 // <template-template-param> ::= <template-param>
993 // <substitution>
Anders Carlsson7482e242009-09-18 04:29:09 +0000994
Anders Carlssonaeb85372009-09-26 22:18:22 +0000995 if (mangleSubstitution(ND))
996 return;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000997
Douglas Gregor32fb4e12010-02-05 20:45:00 +0000998 // <template-template-param> ::= <template-param>
999 if (const TemplateTemplateParmDecl *TTP
1000 = dyn_cast<TemplateTemplateParmDecl>(ND)) {
1001 mangleTemplateParameter(TTP->getIndex());
1002 return;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001003 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00001004
Anders Carlssonaa73ab12009-09-18 18:47:07 +00001005 manglePrefix(ND->getDeclContext());
Anders Carlsson1668f202009-09-26 20:13:56 +00001006 mangleUnqualifiedName(ND->getTemplatedDecl());
Anders Carlssonaeb85372009-09-26 22:18:22 +00001007 addSubstitution(ND);
Anders Carlsson7482e242009-09-18 04:29:09 +00001008}
1009
John McCallb6f532e2010-07-14 06:43:17 +00001010/// Mangles a template name under the production <type>. Required for
1011/// template template arguments.
1012/// <type> ::= <class-enum-type>
1013/// ::= <template-param>
1014/// ::= <substitution>
1015void CXXNameMangler::mangleType(TemplateName TN) {
1016 if (mangleSubstitution(TN))
1017 return;
1018
1019 TemplateDecl *TD = 0;
1020
1021 switch (TN.getKind()) {
1022 case TemplateName::QualifiedTemplate:
1023 TD = TN.getAsQualifiedTemplateName()->getTemplateDecl();
1024 goto HaveDecl;
1025
1026 case TemplateName::Template:
1027 TD = TN.getAsTemplateDecl();
1028 goto HaveDecl;
1029
1030 HaveDecl:
1031 if (isa<TemplateTemplateParmDecl>(TD))
1032 mangleTemplateParameter(cast<TemplateTemplateParmDecl>(TD)->getIndex());
1033 else
1034 mangleName(TD);
1035 break;
1036
1037 case TemplateName::OverloadedTemplate:
1038 llvm_unreachable("can't mangle an overloaded template name as a <type>");
1039 break;
1040
1041 case TemplateName::DependentTemplate: {
1042 const DependentTemplateName *Dependent = TN.getAsDependentTemplateName();
1043 assert(Dependent->isIdentifier());
1044
1045 // <class-enum-type> ::= <name>
1046 // <name> ::= <nested-name>
1047 mangleUnresolvedScope(Dependent->getQualifier());
1048 mangleSourceName(Dependent->getIdentifier());
1049 break;
1050 }
1051
Douglas Gregor1aee05d2011-01-15 06:45:20 +00001052 case TemplateName::SubstTemplateTemplateParmPack: {
1053 SubstTemplateTemplateParmPackStorage *SubstPack
1054 = TN.getAsSubstTemplateTemplateParmPack();
1055 mangleTemplateParameter(SubstPack->getParameterPack()->getIndex());
1056 break;
1057 }
John McCallb6f532e2010-07-14 06:43:17 +00001058 }
1059
1060 addSubstitution(TN);
1061}
1062
Mike Stump1eb44332009-09-09 15:08:12 +00001063void
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001064CXXNameMangler::mangleOperatorName(OverloadedOperatorKind OO, unsigned Arity) {
1065 switch (OO) {
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001066 // <operator-name> ::= nw # new
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001067 case OO_New: Out << "nw"; break;
1068 // ::= na # new[]
1069 case OO_Array_New: Out << "na"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001070 // ::= dl # delete
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001071 case OO_Delete: Out << "dl"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001072 // ::= da # delete[]
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001073 case OO_Array_Delete: Out << "da"; break;
1074 // ::= ps # + (unary)
John McCall5e1e89b2010-08-18 19:18:59 +00001075 // ::= pl # + (binary or unknown)
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001076 case OO_Plus:
Anders Carlsson8257d412009-12-22 06:36:32 +00001077 Out << (Arity == 1? "ps" : "pl"); break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001078 // ::= ng # - (unary)
John McCall5e1e89b2010-08-18 19:18:59 +00001079 // ::= mi # - (binary or unknown)
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001080 case OO_Minus:
Anders Carlsson8257d412009-12-22 06:36:32 +00001081 Out << (Arity == 1? "ng" : "mi"); break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001082 // ::= ad # & (unary)
John McCall5e1e89b2010-08-18 19:18:59 +00001083 // ::= an # & (binary or unknown)
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001084 case OO_Amp:
Anders Carlsson8257d412009-12-22 06:36:32 +00001085 Out << (Arity == 1? "ad" : "an"); break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001086 // ::= de # * (unary)
John McCall5e1e89b2010-08-18 19:18:59 +00001087 // ::= ml # * (binary or unknown)
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001088 case OO_Star:
John McCall5e1e89b2010-08-18 19:18:59 +00001089 // Use binary when unknown.
Anders Carlsson8257d412009-12-22 06:36:32 +00001090 Out << (Arity == 1? "de" : "ml"); break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001091 // ::= co # ~
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001092 case OO_Tilde: Out << "co"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001093 // ::= dv # /
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001094 case OO_Slash: Out << "dv"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001095 // ::= rm # %
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001096 case OO_Percent: Out << "rm"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001097 // ::= or # |
1098 case OO_Pipe: Out << "or"; break;
1099 // ::= eo # ^
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001100 case OO_Caret: Out << "eo"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001101 // ::= aS # =
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001102 case OO_Equal: Out << "aS"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001103 // ::= pL # +=
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001104 case OO_PlusEqual: Out << "pL"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001105 // ::= mI # -=
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001106 case OO_MinusEqual: Out << "mI"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001107 // ::= mL # *=
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001108 case OO_StarEqual: Out << "mL"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001109 // ::= dV # /=
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001110 case OO_SlashEqual: Out << "dV"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001111 // ::= rM # %=
1112 case OO_PercentEqual: Out << "rM"; break;
1113 // ::= aN # &=
1114 case OO_AmpEqual: Out << "aN"; break;
1115 // ::= oR # |=
1116 case OO_PipeEqual: Out << "oR"; break;
1117 // ::= eO # ^=
1118 case OO_CaretEqual: Out << "eO"; break;
1119 // ::= ls # <<
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001120 case OO_LessLess: Out << "ls"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001121 // ::= rs # >>
1122 case OO_GreaterGreater: Out << "rs"; break;
1123 // ::= lS # <<=
1124 case OO_LessLessEqual: Out << "lS"; break;
1125 // ::= rS # >>=
1126 case OO_GreaterGreaterEqual: Out << "rS"; break;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001127 // ::= eq # ==
1128 case OO_EqualEqual: Out << "eq"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001129 // ::= ne # !=
1130 case OO_ExclaimEqual: Out << "ne"; break;
1131 // ::= lt # <
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001132 case OO_Less: Out << "lt"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001133 // ::= gt # >
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001134 case OO_Greater: Out << "gt"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001135 // ::= le # <=
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001136 case OO_LessEqual: Out << "le"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001137 // ::= ge # >=
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001138 case OO_GreaterEqual: Out << "ge"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001139 // ::= nt # !
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001140 case OO_Exclaim: Out << "nt"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001141 // ::= aa # &&
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001142 case OO_AmpAmp: Out << "aa"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001143 // ::= oo # ||
1144 case OO_PipePipe: Out << "oo"; break;
1145 // ::= pp # ++
1146 case OO_PlusPlus: Out << "pp"; break;
1147 // ::= mm # --
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001148 case OO_MinusMinus: Out << "mm"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001149 // ::= cm # ,
1150 case OO_Comma: Out << "cm"; break;
1151 // ::= pm # ->*
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001152 case OO_ArrowStar: Out << "pm"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001153 // ::= pt # ->
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001154 case OO_Arrow: Out << "pt"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001155 // ::= cl # ()
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001156 case OO_Call: Out << "cl"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001157 // ::= ix # []
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001158 case OO_Subscript: Out << "ix"; break;
Anders Carlssone170ba72009-12-14 01:45:37 +00001159
1160 // ::= qu # ?
1161 // The conditional operator can't be overloaded, but we still handle it when
1162 // mangling expressions.
1163 case OO_Conditional: Out << "qu"; break;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001164
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001165 case OO_None:
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001166 case NUM_OVERLOADED_OPERATORS:
Mike Stump1eb44332009-09-09 15:08:12 +00001167 assert(false && "Not an overloaded operator");
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001168 break;
1169 }
1170}
1171
John McCall0953e762009-09-24 19:53:00 +00001172void CXXNameMangler::mangleQualifiers(Qualifiers Quals) {
Mike Stump1eb44332009-09-09 15:08:12 +00001173 // <CV-qualifiers> ::= [r] [V] [K] # restrict (C99), volatile, const
John McCall0953e762009-09-24 19:53:00 +00001174 if (Quals.hasRestrict())
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001175 Out << 'r';
John McCall0953e762009-09-24 19:53:00 +00001176 if (Quals.hasVolatile())
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001177 Out << 'V';
John McCall0953e762009-09-24 19:53:00 +00001178 if (Quals.hasConst())
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001179 Out << 'K';
John McCall0953e762009-09-24 19:53:00 +00001180
Douglas Gregor56079f72010-06-14 23:15:08 +00001181 if (Quals.hasAddressSpace()) {
1182 // Extension:
1183 //
1184 // <type> ::= U <address-space-number>
1185 //
1186 // where <address-space-number> is a source name consisting of 'AS'
1187 // followed by the address space <number>.
1188 llvm::SmallString<64> ASString;
1189 ASString = "AS" + llvm::utostr_32(Quals.getAddressSpace());
1190 Out << 'U' << ASString.size() << ASString;
1191 }
1192
John McCall0953e762009-09-24 19:53:00 +00001193 // FIXME: For now, just drop all extension qualifiers on the floor.
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001194}
1195
Douglas Gregor0a9a6d62011-01-26 17:36:28 +00001196void CXXNameMangler::mangleRefQualifier(RefQualifierKind RefQualifier) {
1197 // <ref-qualifier> ::= R # lvalue reference
1198 // ::= O # rvalue-reference
1199 // Proposal to Itanium C++ ABI list on 1/26/11
1200 switch (RefQualifier) {
1201 case RQ_None:
1202 break;
1203
1204 case RQ_LValue:
1205 Out << 'R';
1206 break;
1207
1208 case RQ_RValue:
1209 Out << 'O';
1210 break;
1211 }
1212}
1213
Anders Carlsson7b06f6c2009-12-10 03:14:39 +00001214void CXXNameMangler::mangleObjCMethodName(const ObjCMethodDecl *MD) {
Rafael Espindolaf0be9792011-02-11 02:52:17 +00001215 Context.mangleObjCMethodName(MD, Out);
Anders Carlsson7b06f6c2009-12-10 03:14:39 +00001216}
1217
John McCallb47f7482011-01-26 20:05:40 +00001218void CXXNameMangler::mangleType(QualType nonCanon) {
Anders Carlsson4843e582009-03-10 17:07:44 +00001219 // Only operate on the canonical type!
John McCallb47f7482011-01-26 20:05:40 +00001220 QualType canon = nonCanon.getCanonicalType();
Anders Carlsson4843e582009-03-10 17:07:44 +00001221
John McCallb47f7482011-01-26 20:05:40 +00001222 SplitQualType split = canon.split();
1223 Qualifiers quals = split.second;
1224 const Type *ty = split.first;
1225
1226 bool isSubstitutable = quals || !isa<BuiltinType>(ty);
1227 if (isSubstitutable && mangleSubstitution(canon))
Anders Carlsson76967372009-09-17 00:43:46 +00001228 return;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001229
John McCallb47f7482011-01-26 20:05:40 +00001230 // If we're mangling a qualified array type, push the qualifiers to
1231 // the element type.
1232 if (quals && isa<ArrayType>(ty)) {
1233 ty = Context.getASTContext().getAsArrayType(canon);
1234 quals = Qualifiers();
1235
1236 // Note that we don't update canon: we want to add the
1237 // substitution at the canonical type.
1238 }
1239
1240 if (quals) {
1241 mangleQualifiers(quals);
John McCall0953e762009-09-24 19:53:00 +00001242 // Recurse: even if the qualified type isn't yet substitutable,
1243 // the unqualified type might be.
John McCallb47f7482011-01-26 20:05:40 +00001244 mangleType(QualType(ty, 0));
Anders Carlsson76967372009-09-17 00:43:46 +00001245 } else {
John McCallb47f7482011-01-26 20:05:40 +00001246 switch (ty->getTypeClass()) {
John McCallefe6aee2009-09-05 07:56:18 +00001247#define ABSTRACT_TYPE(CLASS, PARENT)
1248#define NON_CANONICAL_TYPE(CLASS, PARENT) \
Anders Carlsson76967372009-09-17 00:43:46 +00001249 case Type::CLASS: \
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +00001250 llvm_unreachable("can't mangle non-canonical type " #CLASS "Type"); \
Anders Carlsson76967372009-09-17 00:43:46 +00001251 return;
John McCallefe6aee2009-09-05 07:56:18 +00001252#define TYPE(CLASS, PARENT) \
Anders Carlsson76967372009-09-17 00:43:46 +00001253 case Type::CLASS: \
John McCallb47f7482011-01-26 20:05:40 +00001254 mangleType(static_cast<const CLASS##Type*>(ty)); \
Anders Carlsson76967372009-09-17 00:43:46 +00001255 break;
John McCallefe6aee2009-09-05 07:56:18 +00001256#include "clang/AST/TypeNodes.def"
Anders Carlsson76967372009-09-17 00:43:46 +00001257 }
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001258 }
Anders Carlsson76967372009-09-17 00:43:46 +00001259
1260 // Add the substitution.
John McCallb47f7482011-01-26 20:05:40 +00001261 if (isSubstitutable)
1262 addSubstitution(canon);
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001263}
1264
Douglas Gregor1b12a3b2010-05-26 05:11:13 +00001265void CXXNameMangler::mangleNameOrStandardSubstitution(const NamedDecl *ND) {
1266 if (!mangleStandardSubstitution(ND))
1267 mangleName(ND);
1268}
1269
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001270void CXXNameMangler::mangleType(const BuiltinType *T) {
John McCallefe6aee2009-09-05 07:56:18 +00001271 // <type> ::= <builtin-type>
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001272 // <builtin-type> ::= v # void
1273 // ::= w # wchar_t
1274 // ::= b # bool
1275 // ::= c # char
1276 // ::= a # signed char
1277 // ::= h # unsigned char
1278 // ::= s # short
1279 // ::= t # unsigned short
1280 // ::= i # int
1281 // ::= j # unsigned int
1282 // ::= l # long
1283 // ::= m # unsigned long
1284 // ::= x # long long, __int64
1285 // ::= y # unsigned long long, __int64
1286 // ::= n # __int128
1287 // UNSUPPORTED: ::= o # unsigned __int128
1288 // ::= f # float
1289 // ::= d # double
1290 // ::= e # long double, __float80
1291 // UNSUPPORTED: ::= g # __float128
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001292 // UNSUPPORTED: ::= Dd # IEEE 754r decimal floating point (64 bits)
1293 // UNSUPPORTED: ::= De # IEEE 754r decimal floating point (128 bits)
1294 // UNSUPPORTED: ::= Df # IEEE 754r decimal floating point (32 bits)
1295 // UNSUPPORTED: ::= Dh # IEEE 754r half-precision floating point (16 bits)
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001296 // ::= Di # char32_t
1297 // ::= Ds # char16_t
Anders Carlssone2923682010-11-04 04:31:32 +00001298 // ::= Dn # std::nullptr_t (i.e., decltype(nullptr))
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001299 // ::= u <source-name> # vendor extended type
1300 switch (T->getKind()) {
1301 case BuiltinType::Void: Out << 'v'; break;
1302 case BuiltinType::Bool: Out << 'b'; break;
1303 case BuiltinType::Char_U: case BuiltinType::Char_S: Out << 'c'; break;
1304 case BuiltinType::UChar: Out << 'h'; break;
1305 case BuiltinType::UShort: Out << 't'; break;
1306 case BuiltinType::UInt: Out << 'j'; break;
1307 case BuiltinType::ULong: Out << 'm'; break;
1308 case BuiltinType::ULongLong: Out << 'y'; break;
Chris Lattner2df9ced2009-04-30 02:43:43 +00001309 case BuiltinType::UInt128: Out << 'o'; break;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001310 case BuiltinType::SChar: Out << 'a'; break;
Chris Lattner3f59c972010-12-25 23:25:43 +00001311 case BuiltinType::WChar_S:
1312 case BuiltinType::WChar_U: Out << 'w'; break;
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001313 case BuiltinType::Char16: Out << "Ds"; break;
1314 case BuiltinType::Char32: Out << "Di"; break;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001315 case BuiltinType::Short: Out << 's'; break;
1316 case BuiltinType::Int: Out << 'i'; break;
1317 case BuiltinType::Long: Out << 'l'; break;
1318 case BuiltinType::LongLong: Out << 'x'; break;
Chris Lattner2df9ced2009-04-30 02:43:43 +00001319 case BuiltinType::Int128: Out << 'n'; break;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001320 case BuiltinType::Float: Out << 'f'; break;
1321 case BuiltinType::Double: Out << 'd'; break;
1322 case BuiltinType::LongDouble: Out << 'e'; break;
Anders Carlssone2923682010-11-04 04:31:32 +00001323 case BuiltinType::NullPtr: Out << "Dn"; break;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001324
1325 case BuiltinType::Overload:
1326 case BuiltinType::Dependent:
Mike Stump1eb44332009-09-09 15:08:12 +00001327 assert(false &&
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001328 "Overloaded and dependent types shouldn't get to name mangling");
1329 break;
Steve Naroff9533a7f2009-07-22 17:14:51 +00001330 case BuiltinType::ObjCId: Out << "11objc_object"; break;
1331 case BuiltinType::ObjCClass: Out << "10objc_class"; break;
Fariborz Jahanian13dcd002009-11-21 19:53:08 +00001332 case BuiltinType::ObjCSel: Out << "13objc_selector"; break;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001333 }
1334}
1335
John McCallefe6aee2009-09-05 07:56:18 +00001336// <type> ::= <function-type>
1337// <function-type> ::= F [Y] <bare-function-type> E
1338void CXXNameMangler::mangleType(const FunctionProtoType *T) {
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001339 Out << 'F';
Mike Stumpf5408fe2009-05-16 07:57:57 +00001340 // FIXME: We don't have enough information in the AST to produce the 'Y'
1341 // encoding for extern "C" function types.
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001342 mangleBareFunctionType(T, /*MangleReturnType=*/true);
1343 Out << 'E';
1344}
John McCallefe6aee2009-09-05 07:56:18 +00001345void CXXNameMangler::mangleType(const FunctionNoProtoType *T) {
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +00001346 llvm_unreachable("Can't mangle K&R function prototypes");
John McCallefe6aee2009-09-05 07:56:18 +00001347}
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001348void CXXNameMangler::mangleBareFunctionType(const FunctionType *T,
1349 bool MangleReturnType) {
John McCallefe6aee2009-09-05 07:56:18 +00001350 // We should never be mangling something without a prototype.
1351 const FunctionProtoType *Proto = cast<FunctionProtoType>(T);
1352
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001353 // <bare-function-type> ::= <signature type>+
1354 if (MangleReturnType)
John McCallefe6aee2009-09-05 07:56:18 +00001355 mangleType(Proto->getResultType());
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001356
Anders Carlsson93296682010-06-02 04:40:13 +00001357 if (Proto->getNumArgs() == 0 && !Proto->isVariadic()) {
Eli Friedmana7e68452010-08-22 01:00:03 +00001358 // <builtin-type> ::= v # void
Anders Carlssonc6c91bc2009-04-01 00:15:23 +00001359 Out << 'v';
1360 return;
1361 }
Mike Stump1eb44332009-09-09 15:08:12 +00001362
Douglas Gregor72564e72009-02-26 23:50:07 +00001363 for (FunctionProtoType::arg_type_iterator Arg = Proto->arg_type_begin(),
Mike Stump1eb44332009-09-09 15:08:12 +00001364 ArgEnd = Proto->arg_type_end();
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001365 Arg != ArgEnd; ++Arg)
1366 mangleType(*Arg);
Douglas Gregor219cc612009-02-13 01:28:03 +00001367
1368 // <builtin-type> ::= z # ellipsis
1369 if (Proto->isVariadic())
1370 Out << 'z';
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001371}
1372
John McCallefe6aee2009-09-05 07:56:18 +00001373// <type> ::= <class-enum-type>
Mike Stump1eb44332009-09-09 15:08:12 +00001374// <class-enum-type> ::= <name>
John McCalled976492009-12-04 22:46:56 +00001375void CXXNameMangler::mangleType(const UnresolvedUsingType *T) {
1376 mangleName(T->getDecl());
1377}
1378
1379// <type> ::= <class-enum-type>
1380// <class-enum-type> ::= <name>
John McCallefe6aee2009-09-05 07:56:18 +00001381void CXXNameMangler::mangleType(const EnumType *T) {
1382 mangleType(static_cast<const TagType*>(T));
1383}
1384void CXXNameMangler::mangleType(const RecordType *T) {
1385 mangleType(static_cast<const TagType*>(T));
1386}
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001387void CXXNameMangler::mangleType(const TagType *T) {
Eli Friedmanecb7e932009-12-11 18:00:57 +00001388 mangleName(T->getDecl());
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001389}
1390
John McCallefe6aee2009-09-05 07:56:18 +00001391// <type> ::= <array-type>
1392// <array-type> ::= A <positive dimension number> _ <element type>
1393// ::= A [<dimension expression>] _ <element type>
1394void CXXNameMangler::mangleType(const ConstantArrayType *T) {
1395 Out << 'A' << T->getSize() << '_';
1396 mangleType(T->getElementType());
1397}
1398void CXXNameMangler::mangleType(const VariableArrayType *T) {
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001399 Out << 'A';
Fariborz Jahanian7281d1f2010-11-02 16:54:00 +00001400 // decayed vla types (size 0) will just be skipped.
1401 if (T->getSizeExpr())
1402 mangleExpression(T->getSizeExpr());
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001403 Out << '_';
1404 mangleType(T->getElementType());
1405}
John McCallefe6aee2009-09-05 07:56:18 +00001406void CXXNameMangler::mangleType(const DependentSizedArrayType *T) {
1407 Out << 'A';
1408 mangleExpression(T->getSizeExpr());
1409 Out << '_';
1410 mangleType(T->getElementType());
1411}
1412void CXXNameMangler::mangleType(const IncompleteArrayType *T) {
Nick Lewycky271b6652010-09-05 03:40:33 +00001413 Out << "A_";
John McCallefe6aee2009-09-05 07:56:18 +00001414 mangleType(T->getElementType());
1415}
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001416
John McCallefe6aee2009-09-05 07:56:18 +00001417// <type> ::= <pointer-to-member-type>
1418// <pointer-to-member-type> ::= M <class type> <member type>
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001419void CXXNameMangler::mangleType(const MemberPointerType *T) {
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001420 Out << 'M';
1421 mangleType(QualType(T->getClass(), 0));
Anders Carlsson0e650012009-05-17 17:41:20 +00001422 QualType PointeeType = T->getPointeeType();
1423 if (const FunctionProtoType *FPT = dyn_cast<FunctionProtoType>(PointeeType)) {
John McCall0953e762009-09-24 19:53:00 +00001424 mangleQualifiers(Qualifiers::fromCVRMask(FPT->getTypeQuals()));
Douglas Gregor0a9a6d62011-01-26 17:36:28 +00001425 mangleRefQualifier(FPT->getRefQualifier());
Anders Carlsson0e650012009-05-17 17:41:20 +00001426 mangleType(FPT);
Anders Carlsson9d85b722010-06-02 04:29:50 +00001427
1428 // Itanium C++ ABI 5.1.8:
1429 //
1430 // The type of a non-static member function is considered to be different,
1431 // for the purposes of substitution, from the type of a namespace-scope or
1432 // static member function whose type appears similar. The types of two
1433 // non-static member functions are considered to be different, for the
1434 // purposes of substitution, if the functions are members of different
1435 // classes. In other words, for the purposes of substitution, the class of
1436 // which the function is a member is considered part of the type of
1437 // function.
1438
1439 // We increment the SeqID here to emulate adding an entry to the
1440 // substitution table. We can't actually add it because we don't want this
1441 // particular function type to be substituted.
1442 ++SeqID;
Mike Stump1eb44332009-09-09 15:08:12 +00001443 } else
Anders Carlsson0e650012009-05-17 17:41:20 +00001444 mangleType(PointeeType);
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001445}
1446
John McCallefe6aee2009-09-05 07:56:18 +00001447// <type> ::= <template-param>
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001448void CXXNameMangler::mangleType(const TemplateTypeParmType *T) {
Anders Carlsson0ccdf8d2009-09-27 00:38:53 +00001449 mangleTemplateParameter(T->getIndex());
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001450}
1451
Douglas Gregorc3069d62011-01-14 02:55:32 +00001452// <type> ::= <template-param>
1453void CXXNameMangler::mangleType(const SubstTemplateTypeParmPackType *T) {
1454 mangleTemplateParameter(T->getReplacedParameter()->getIndex());
1455}
1456
John McCallefe6aee2009-09-05 07:56:18 +00001457// <type> ::= P <type> # pointer-to
1458void CXXNameMangler::mangleType(const PointerType *T) {
1459 Out << 'P';
1460 mangleType(T->getPointeeType());
1461}
1462void CXXNameMangler::mangleType(const ObjCObjectPointerType *T) {
1463 Out << 'P';
1464 mangleType(T->getPointeeType());
1465}
1466
1467// <type> ::= R <type> # reference-to
1468void CXXNameMangler::mangleType(const LValueReferenceType *T) {
1469 Out << 'R';
1470 mangleType(T->getPointeeType());
1471}
1472
1473// <type> ::= O <type> # rvalue reference-to (C++0x)
1474void CXXNameMangler::mangleType(const RValueReferenceType *T) {
1475 Out << 'O';
1476 mangleType(T->getPointeeType());
1477}
1478
1479// <type> ::= C <type> # complex pair (C 2000)
1480void CXXNameMangler::mangleType(const ComplexType *T) {
1481 Out << 'C';
1482 mangleType(T->getElementType());
1483}
1484
Bob Wilsonc7df92d2010-11-12 17:24:43 +00001485// ARM's ABI for Neon vector types specifies that they should be mangled as
Bob Wilson57147a82010-11-16 00:32:18 +00001486// if they are structs (to match ARM's initial implementation). The
1487// vector type must be one of the special types predefined by ARM.
1488void CXXNameMangler::mangleNeonVectorType(const VectorType *T) {
Bob Wilsonc7df92d2010-11-12 17:24:43 +00001489 QualType EltType = T->getElementType();
Bob Wilson57147a82010-11-16 00:32:18 +00001490 assert(EltType->isBuiltinType() && "Neon vector element not a BuiltinType");
Bob Wilsonc7df92d2010-11-12 17:24:43 +00001491 const char *EltName = 0;
Bob Wilson491328c2010-11-12 17:24:46 +00001492 if (T->getVectorKind() == VectorType::NeonPolyVector) {
1493 switch (cast<BuiltinType>(EltType)->getKind()) {
Bob Wilson4cfaa5d2010-11-12 17:24:49 +00001494 case BuiltinType::SChar: EltName = "poly8_t"; break;
1495 case BuiltinType::Short: EltName = "poly16_t"; break;
Bob Wilson57147a82010-11-16 00:32:18 +00001496 default: llvm_unreachable("unexpected Neon polynomial vector element type");
Bob Wilson491328c2010-11-12 17:24:46 +00001497 }
1498 } else {
1499 switch (cast<BuiltinType>(EltType)->getKind()) {
Bob Wilson4cfaa5d2010-11-12 17:24:49 +00001500 case BuiltinType::SChar: EltName = "int8_t"; break;
1501 case BuiltinType::UChar: EltName = "uint8_t"; break;
1502 case BuiltinType::Short: EltName = "int16_t"; break;
1503 case BuiltinType::UShort: EltName = "uint16_t"; break;
1504 case BuiltinType::Int: EltName = "int32_t"; break;
1505 case BuiltinType::UInt: EltName = "uint32_t"; break;
1506 case BuiltinType::LongLong: EltName = "int64_t"; break;
1507 case BuiltinType::ULongLong: EltName = "uint64_t"; break;
1508 case BuiltinType::Float: EltName = "float32_t"; break;
Bob Wilson57147a82010-11-16 00:32:18 +00001509 default: llvm_unreachable("unexpected Neon vector element type");
Bob Wilson491328c2010-11-12 17:24:46 +00001510 }
Bob Wilsonc7df92d2010-11-12 17:24:43 +00001511 }
1512 const char *BaseName = 0;
Bob Wilson4cfaa5d2010-11-12 17:24:49 +00001513 unsigned BitSize = (T->getNumElements() *
Bob Wilson3a723022010-11-16 00:32:12 +00001514 getASTContext().getTypeSize(EltType));
Bob Wilsonc7df92d2010-11-12 17:24:43 +00001515 if (BitSize == 64)
1516 BaseName = "__simd64_";
Bob Wilson57147a82010-11-16 00:32:18 +00001517 else {
1518 assert(BitSize == 128 && "Neon vector type not 64 or 128 bits");
Bob Wilsonc7df92d2010-11-12 17:24:43 +00001519 BaseName = "__simd128_";
Bob Wilson57147a82010-11-16 00:32:18 +00001520 }
Bob Wilsonc7df92d2010-11-12 17:24:43 +00001521 Out << strlen(BaseName) + strlen(EltName);
1522 Out << BaseName << EltName;
Bob Wilsonc7df92d2010-11-12 17:24:43 +00001523}
1524
John McCallefe6aee2009-09-05 07:56:18 +00001525// GNU extension: vector types
Chris Lattner788b0fd2010-06-23 06:00:24 +00001526// <type> ::= <vector-type>
1527// <vector-type> ::= Dv <positive dimension number> _
1528// <extended element type>
1529// ::= Dv [<dimension expression>] _ <element type>
1530// <extended element type> ::= <element type>
1531// ::= p # AltiVec vector pixel
John McCallefe6aee2009-09-05 07:56:18 +00001532void CXXNameMangler::mangleType(const VectorType *T) {
Bob Wilson491328c2010-11-12 17:24:46 +00001533 if ((T->getVectorKind() == VectorType::NeonVector ||
Bob Wilson57147a82010-11-16 00:32:18 +00001534 T->getVectorKind() == VectorType::NeonPolyVector)) {
1535 mangleNeonVectorType(T);
Bob Wilsonc7df92d2010-11-12 17:24:43 +00001536 return;
Bob Wilson57147a82010-11-16 00:32:18 +00001537 }
Nick Lewycky0e5f0672010-03-26 07:18:04 +00001538 Out << "Dv" << T->getNumElements() << '_';
Bob Wilsone86d78c2010-11-10 21:56:12 +00001539 if (T->getVectorKind() == VectorType::AltiVecPixel)
Chris Lattner788b0fd2010-06-23 06:00:24 +00001540 Out << 'p';
Bob Wilsone86d78c2010-11-10 21:56:12 +00001541 else if (T->getVectorKind() == VectorType::AltiVecBool)
Chris Lattner788b0fd2010-06-23 06:00:24 +00001542 Out << 'b';
1543 else
1544 mangleType(T->getElementType());
John McCallefe6aee2009-09-05 07:56:18 +00001545}
1546void CXXNameMangler::mangleType(const ExtVectorType *T) {
1547 mangleType(static_cast<const VectorType*>(T));
1548}
1549void CXXNameMangler::mangleType(const DependentSizedExtVectorType *T) {
Nick Lewycky0e5f0672010-03-26 07:18:04 +00001550 Out << "Dv";
1551 mangleExpression(T->getSizeExpr());
1552 Out << '_';
John McCallefe6aee2009-09-05 07:56:18 +00001553 mangleType(T->getElementType());
1554}
1555
Douglas Gregor7536dd52010-12-20 02:24:11 +00001556void CXXNameMangler::mangleType(const PackExpansionType *T) {
Douglas Gregor4fc48662011-01-13 16:39:34 +00001557 // <type> ::= Dp <type> # pack expansion (C++0x)
Douglas Gregor255c2692011-01-13 17:44:36 +00001558 Out << "Dp";
Douglas Gregor7536dd52010-12-20 02:24:11 +00001559 mangleType(T->getPattern());
1560}
1561
Anders Carlssona40c5e42009-03-07 22:03:21 +00001562void CXXNameMangler::mangleType(const ObjCInterfaceType *T) {
1563 mangleSourceName(T->getDecl()->getIdentifier());
1564}
1565
John McCallc12c5bb2010-05-15 11:32:37 +00001566void CXXNameMangler::mangleType(const ObjCObjectType *T) {
John McCallc00c1f62010-05-15 17:06:29 +00001567 // We don't allow overloading by different protocol qualification,
1568 // so mangling them isn't necessary.
John McCallc12c5bb2010-05-15 11:32:37 +00001569 mangleType(T->getBaseType());
1570}
1571
John McCallefe6aee2009-09-05 07:56:18 +00001572void CXXNameMangler::mangleType(const BlockPointerType *T) {
Anders Carlssonf28c6872009-12-23 22:31:44 +00001573 Out << "U13block_pointer";
1574 mangleType(T->getPointeeType());
John McCallefe6aee2009-09-05 07:56:18 +00001575}
1576
John McCall31f17ec2010-04-27 00:57:59 +00001577void CXXNameMangler::mangleType(const InjectedClassNameType *T) {
1578 // Mangle injected class name types as if the user had written the
1579 // specialization out fully. It may not actually be possible to see
1580 // this mangling, though.
1581 mangleType(T->getInjectedSpecializationType());
1582}
1583
John McCallefe6aee2009-09-05 07:56:18 +00001584void CXXNameMangler::mangleType(const TemplateSpecializationType *T) {
Douglas Gregor1e9268e2010-04-28 05:58:56 +00001585 if (TemplateDecl *TD = T->getTemplateName().getAsTemplateDecl()) {
1586 mangleName(TD, T->getArgs(), T->getNumArgs());
1587 } else {
1588 if (mangleSubstitution(QualType(T, 0)))
1589 return;
Sean Huntc3021132010-05-05 15:23:54 +00001590
Douglas Gregor1e9268e2010-04-28 05:58:56 +00001591 mangleTemplatePrefix(T->getTemplateName());
Sean Huntc3021132010-05-05 15:23:54 +00001592
Douglas Gregor1e9268e2010-04-28 05:58:56 +00001593 // FIXME: GCC does not appear to mangle the template arguments when
1594 // the template in question is a dependent template name. Should we
1595 // emulate that badness?
1596 mangleTemplateArgs(T->getTemplateName(), T->getArgs(), T->getNumArgs());
1597 addSubstitution(QualType(T, 0));
1598 }
John McCallefe6aee2009-09-05 07:56:18 +00001599}
1600
Douglas Gregor4714c122010-03-31 17:34:00 +00001601void CXXNameMangler::mangleType(const DependentNameType *T) {
Anders Carlssonae352482009-09-26 02:26:02 +00001602 // Typename types are always nested
1603 Out << 'N';
John McCall33500952010-06-11 00:33:02 +00001604 mangleUnresolvedScope(T->getQualifier());
1605 mangleSourceName(T->getIdentifier());
1606 Out << 'E';
1607}
John McCall6ab30e02010-06-09 07:26:17 +00001608
John McCall33500952010-06-11 00:33:02 +00001609void CXXNameMangler::mangleType(const DependentTemplateSpecializationType *T) {
Douglas Gregoraa2187d2011-02-28 00:04:36 +00001610 // Dependently-scoped template types are nested if they have a prefix.
John McCall33500952010-06-11 00:33:02 +00001611 Out << 'N';
1612
1613 // TODO: avoid making this TemplateName.
1614 TemplateName Prefix =
1615 getASTContext().getDependentTemplateName(T->getQualifier(),
1616 T->getIdentifier());
1617 mangleTemplatePrefix(Prefix);
1618
1619 // FIXME: GCC does not appear to mangle the template arguments when
1620 // the template in question is a dependent template name. Should we
1621 // emulate that badness?
1622 mangleTemplateArgs(Prefix, T->getArgs(), T->getNumArgs());
Anders Carlssonae352482009-09-26 02:26:02 +00001623 Out << 'E';
John McCallefe6aee2009-09-05 07:56:18 +00001624}
1625
John McCallad5e7382010-03-01 23:49:17 +00001626void CXXNameMangler::mangleType(const TypeOfType *T) {
1627 // FIXME: this is pretty unsatisfactory, but there isn't an obvious
1628 // "extension with parameters" mangling.
1629 Out << "u6typeof";
1630}
1631
1632void CXXNameMangler::mangleType(const TypeOfExprType *T) {
1633 // FIXME: this is pretty unsatisfactory, but there isn't an obvious
1634 // "extension with parameters" mangling.
1635 Out << "u6typeof";
1636}
1637
1638void CXXNameMangler::mangleType(const DecltypeType *T) {
1639 Expr *E = T->getUnderlyingExpr();
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001640
John McCallad5e7382010-03-01 23:49:17 +00001641 // type ::= Dt <expression> E # decltype of an id-expression
1642 // # or class member access
1643 // ::= DT <expression> E # decltype of an expression
1644
1645 // This purports to be an exhaustive list of id-expressions and
1646 // class member accesses. Note that we do not ignore parentheses;
1647 // parentheses change the semantics of decltype for these
1648 // expressions (and cause the mangler to use the other form).
1649 if (isa<DeclRefExpr>(E) ||
1650 isa<MemberExpr>(E) ||
1651 isa<UnresolvedLookupExpr>(E) ||
1652 isa<DependentScopeDeclRefExpr>(E) ||
1653 isa<CXXDependentScopeMemberExpr>(E) ||
1654 isa<UnresolvedMemberExpr>(E))
1655 Out << "Dt";
1656 else
1657 Out << "DT";
1658 mangleExpression(E);
1659 Out << 'E';
1660}
1661
Richard Smith34b41d92011-02-20 03:19:35 +00001662void CXXNameMangler::mangleType(const AutoType *T) {
1663 QualType D = T->getDeducedType();
Richard Smith967ecd32011-02-21 20:10:02 +00001664 // <builtin-type> ::= Da # dependent auto
1665 if (D.isNull())
1666 Out << "Da";
1667 else
1668 mangleType(D);
Richard Smith34b41d92011-02-20 03:19:35 +00001669}
1670
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001671void CXXNameMangler::mangleIntegerLiteral(QualType T,
Anders Carlssone170ba72009-12-14 01:45:37 +00001672 const llvm::APSInt &Value) {
1673 // <expr-primary> ::= L <type> <value number> E # integer literal
1674 Out << 'L';
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001675
Anders Carlssone170ba72009-12-14 01:45:37 +00001676 mangleType(T);
1677 if (T->isBooleanType()) {
1678 // Boolean values are encoded as 0/1.
1679 Out << (Value.getBoolValue() ? '1' : '0');
1680 } else {
John McCall0512e482010-07-14 04:20:34 +00001681 mangleNumber(Value);
Anders Carlssone170ba72009-12-14 01:45:37 +00001682 }
1683 Out << 'E';
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001684
Anders Carlssone170ba72009-12-14 01:45:37 +00001685}
1686
John McCall2f27bf82010-02-04 02:56:29 +00001687/// Mangles a member expression. Implicit accesses are not handled,
1688/// but that should be okay, because you shouldn't be able to
1689/// make an implicit access in a function template declaration.
John McCall2f27bf82010-02-04 02:56:29 +00001690void CXXNameMangler::mangleMemberExpr(const Expr *Base,
1691 bool IsArrow,
1692 NestedNameSpecifier *Qualifier,
1693 DeclarationName Member,
1694 unsigned Arity) {
John McCalle1e342f2010-03-01 19:12:25 +00001695 // gcc-4.4 uses 'dt' for dot expressions, which is reasonable.
1696 // OTOH, gcc also mangles the name as an expression.
1697 Out << (IsArrow ? "pt" : "dt");
John McCall2f27bf82010-02-04 02:56:29 +00001698 mangleExpression(Base);
1699 mangleUnresolvedName(Qualifier, Member, Arity);
1700}
1701
John McCall5e1e89b2010-08-18 19:18:59 +00001702void CXXNameMangler::mangleExpression(const Expr *E, unsigned Arity) {
Anders Carlssond553f8c2009-09-21 01:21:10 +00001703 // <expression> ::= <unary operator-name> <expression>
John McCall09cc1412010-02-03 00:55:45 +00001704 // ::= <binary operator-name> <expression> <expression>
1705 // ::= <trinary operator-name> <expression> <expression> <expression>
Eli Friedmana7e68452010-08-22 01:00:03 +00001706 // ::= cl <expression>* E # call
Anders Carlssond553f8c2009-09-21 01:21:10 +00001707 // ::= cv <type> expression # conversion with one argument
1708 // ::= cv <type> _ <expression>* E # conversion with a different number of arguments
Eli Friedmana7e68452010-08-22 01:00:03 +00001709 // ::= st <type> # sizeof (a type)
Anders Carlssond553f8c2009-09-21 01:21:10 +00001710 // ::= at <type> # alignof (a type)
1711 // ::= <template-param>
1712 // ::= <function-param>
1713 // ::= sr <type> <unqualified-name> # dependent name
1714 // ::= sr <type> <unqualified-name> <template-args> # dependent template-id
1715 // ::= sZ <template-param> # size of a parameter pack
Douglas Gregor4fc48662011-01-13 16:39:34 +00001716 // ::= sZ <function-param> # size of a function parameter pack
John McCall09cc1412010-02-03 00:55:45 +00001717 // ::= <expr-primary>
John McCall1dd73832010-02-04 01:42:13 +00001718 // <expr-primary> ::= L <type> <value number> E # integer literal
1719 // ::= L <type <value float> E # floating literal
1720 // ::= L <mangled-name> E # external name
Anders Carlssond553f8c2009-09-21 01:21:10 +00001721 switch (E->getStmtClass()) {
John McCall6ae1f352010-04-09 22:26:14 +00001722 case Expr::NoStmtClass:
John McCall63c00d72011-02-09 08:16:59 +00001723#define ABSTRACT_STMT(Type)
John McCall6ae1f352010-04-09 22:26:14 +00001724#define EXPR(Type, Base)
1725#define STMT(Type, Base) \
1726 case Expr::Type##Class:
Sean Hunt4bfe1962010-05-05 15:24:00 +00001727#include "clang/AST/StmtNodes.inc"
John McCall0512e482010-07-14 04:20:34 +00001728 // fallthrough
1729
1730 // These all can only appear in local or variable-initialization
1731 // contexts and so should never appear in a mangling.
1732 case Expr::AddrLabelExprClass:
1733 case Expr::BlockDeclRefExprClass:
1734 case Expr::CXXThisExprClass:
1735 case Expr::DesignatedInitExprClass:
1736 case Expr::ImplicitValueInitExprClass:
1737 case Expr::InitListExprClass:
1738 case Expr::ParenListExprClass:
1739 case Expr::CXXScalarValueInitExprClass:
John McCall09cc1412010-02-03 00:55:45 +00001740 llvm_unreachable("unexpected statement kind");
1741 break;
1742
John McCall0512e482010-07-14 04:20:34 +00001743 // FIXME: invent manglings for all these.
1744 case Expr::BlockExprClass:
1745 case Expr::CXXPseudoDestructorExprClass:
1746 case Expr::ChooseExprClass:
1747 case Expr::CompoundLiteralExprClass:
1748 case Expr::ExtVectorElementExprClass:
1749 case Expr::ObjCEncodeExprClass:
John McCall0512e482010-07-14 04:20:34 +00001750 case Expr::ObjCIsaExprClass:
1751 case Expr::ObjCIvarRefExprClass:
1752 case Expr::ObjCMessageExprClass:
1753 case Expr::ObjCPropertyRefExprClass:
1754 case Expr::ObjCProtocolExprClass:
1755 case Expr::ObjCSelectorExprClass:
1756 case Expr::ObjCStringLiteralClass:
John McCall0512e482010-07-14 04:20:34 +00001757 case Expr::OffsetOfExprClass:
1758 case Expr::PredefinedExprClass:
1759 case Expr::ShuffleVectorExprClass:
1760 case Expr::StmtExprClass:
John McCall0512e482010-07-14 04:20:34 +00001761 case Expr::UnaryTypeTraitExprClass:
Francois Pichet6ad6f282010-12-07 00:08:36 +00001762 case Expr::BinaryTypeTraitExprClass:
Francois Pichet9be88402010-09-08 23:47:05 +00001763 case Expr::VAArgExprClass:
Sebastian Redl2e156222010-09-10 20:55:43 +00001764 case Expr::CXXUuidofExprClass:
Peter Collingbournee08ce652011-02-09 21:07:24 +00001765 case Expr::CXXNoexceptExprClass:
1766 case Expr::CUDAKernelCallExprClass: {
John McCall6ae1f352010-04-09 22:26:14 +00001767 // As bad as this diagnostic is, it's better than crashing.
1768 Diagnostic &Diags = Context.getDiags();
1769 unsigned DiagID = Diags.getCustomDiagID(Diagnostic::Error,
1770 "cannot yet mangle expression type %0");
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +00001771 Diags.Report(E->getExprLoc(), DiagID)
John McCall739bf092010-04-10 09:39:25 +00001772 << E->getStmtClassName() << E->getSourceRange();
John McCall6ae1f352010-04-09 22:26:14 +00001773 break;
1774 }
1775
John McCall56ca35d2011-02-17 10:25:35 +00001776 // Even gcc-4.5 doesn't mangle this.
1777 case Expr::BinaryConditionalOperatorClass: {
1778 Diagnostic &Diags = Context.getDiags();
1779 unsigned DiagID =
1780 Diags.getCustomDiagID(Diagnostic::Error,
1781 "?: operator with omitted middle operand cannot be mangled");
1782 Diags.Report(E->getExprLoc(), DiagID)
1783 << E->getStmtClassName() << E->getSourceRange();
1784 break;
1785 }
1786
1787 // These are used for internal purposes and cannot be meaningfully mangled.
John McCall7cd7d1a2010-11-15 23:31:06 +00001788 case Expr::OpaqueValueExprClass:
1789 llvm_unreachable("cannot mangle opaque value; mangling wrong thing?");
1790
John McCall0512e482010-07-14 04:20:34 +00001791 case Expr::CXXDefaultArgExprClass:
John McCall5e1e89b2010-08-18 19:18:59 +00001792 mangleExpression(cast<CXXDefaultArgExpr>(E)->getExpr(), Arity);
John McCall0512e482010-07-14 04:20:34 +00001793 break;
1794
1795 case Expr::CXXMemberCallExprClass: // fallthrough
John McCall1dd73832010-02-04 01:42:13 +00001796 case Expr::CallExprClass: {
1797 const CallExpr *CE = cast<CallExpr>(E);
1798 Out << "cl";
John McCall5e1e89b2010-08-18 19:18:59 +00001799 mangleExpression(CE->getCallee(), CE->getNumArgs());
John McCall1dd73832010-02-04 01:42:13 +00001800 for (unsigned I = 0, N = CE->getNumArgs(); I != N; ++I)
1801 mangleExpression(CE->getArg(I));
Benjamin Kramer35f59b62010-04-10 16:03:31 +00001802 Out << 'E';
John McCall09cc1412010-02-03 00:55:45 +00001803 break;
John McCall1dd73832010-02-04 01:42:13 +00001804 }
John McCall09cc1412010-02-03 00:55:45 +00001805
John McCall0512e482010-07-14 04:20:34 +00001806 case Expr::CXXNewExprClass: {
1807 // Proposal from David Vandervoorde, 2010.06.30
1808 const CXXNewExpr *New = cast<CXXNewExpr>(E);
1809 if (New->isGlobalNew()) Out << "gs";
1810 Out << (New->isArray() ? "na" : "nw");
1811 for (CXXNewExpr::const_arg_iterator I = New->placement_arg_begin(),
1812 E = New->placement_arg_end(); I != E; ++I)
1813 mangleExpression(*I);
1814 Out << '_';
1815 mangleType(New->getAllocatedType());
1816 if (New->hasInitializer()) {
1817 Out << "pi";
1818 for (CXXNewExpr::const_arg_iterator I = New->constructor_arg_begin(),
1819 E = New->constructor_arg_end(); I != E; ++I)
1820 mangleExpression(*I);
1821 }
1822 Out << 'E';
1823 break;
1824 }
1825
John McCall2f27bf82010-02-04 02:56:29 +00001826 case Expr::MemberExprClass: {
1827 const MemberExpr *ME = cast<MemberExpr>(E);
1828 mangleMemberExpr(ME->getBase(), ME->isArrow(),
1829 ME->getQualifier(), ME->getMemberDecl()->getDeclName(),
John McCall5e1e89b2010-08-18 19:18:59 +00001830 Arity);
John McCall2f27bf82010-02-04 02:56:29 +00001831 break;
1832 }
1833
1834 case Expr::UnresolvedMemberExprClass: {
1835 const UnresolvedMemberExpr *ME = cast<UnresolvedMemberExpr>(E);
1836 mangleMemberExpr(ME->getBase(), ME->isArrow(),
1837 ME->getQualifier(), ME->getMemberName(),
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
1844 case Expr::CXXDependentScopeMemberExprClass: {
1845 const CXXDependentScopeMemberExpr *ME
1846 = cast<CXXDependentScopeMemberExpr>(E);
1847 mangleMemberExpr(ME->getBase(), ME->isArrow(),
1848 ME->getQualifier(), ME->getMember(),
John McCall5e1e89b2010-08-18 19:18:59 +00001849 Arity);
John McCall6dbce192010-08-20 00:17:19 +00001850 if (ME->hasExplicitTemplateArgs())
1851 mangleTemplateArgs(ME->getExplicitTemplateArgs());
John McCall2f27bf82010-02-04 02:56:29 +00001852 break;
1853 }
1854
John McCall1dd73832010-02-04 01:42:13 +00001855 case Expr::UnresolvedLookupExprClass: {
John McCalla3218e72010-02-04 01:48:38 +00001856 // The ABI doesn't cover how to mangle overload sets, so we mangle
1857 // using something as close as possible to the original lookup
1858 // expression.
John McCall1dd73832010-02-04 01:42:13 +00001859 const UnresolvedLookupExpr *ULE = cast<UnresolvedLookupExpr>(E);
John McCall5e1e89b2010-08-18 19:18:59 +00001860 mangleUnresolvedName(ULE->getQualifier(), ULE->getName(), Arity);
John McCall6dbce192010-08-20 00:17:19 +00001861 if (ULE->hasExplicitTemplateArgs())
1862 mangleTemplateArgs(ULE->getExplicitTemplateArgs());
John McCall1dd73832010-02-04 01:42:13 +00001863 break;
1864 }
1865
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001866 case Expr::CXXUnresolvedConstructExprClass: {
John McCall1dd73832010-02-04 01:42:13 +00001867 const CXXUnresolvedConstructExpr *CE = cast<CXXUnresolvedConstructExpr>(E);
1868 unsigned N = CE->arg_size();
1869
1870 Out << "cv";
1871 mangleType(CE->getType());
Benjamin Kramer35f59b62010-04-10 16:03:31 +00001872 if (N != 1) Out << '_';
John McCall1dd73832010-02-04 01:42:13 +00001873 for (unsigned I = 0; I != N; ++I) mangleExpression(CE->getArg(I));
Benjamin Kramer35f59b62010-04-10 16:03:31 +00001874 if (N != 1) Out << 'E';
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001875 break;
John McCall1dd73832010-02-04 01:42:13 +00001876 }
John McCall09cc1412010-02-03 00:55:45 +00001877
John McCall1dd73832010-02-04 01:42:13 +00001878 case Expr::CXXTemporaryObjectExprClass:
1879 case Expr::CXXConstructExprClass: {
1880 const CXXConstructExpr *CE = cast<CXXConstructExpr>(E);
1881 unsigned N = CE->getNumArgs();
1882
1883 Out << "cv";
1884 mangleType(CE->getType());
Benjamin Kramer35f59b62010-04-10 16:03:31 +00001885 if (N != 1) Out << '_';
John McCall1dd73832010-02-04 01:42:13 +00001886 for (unsigned I = 0; I != N; ++I) mangleExpression(CE->getArg(I));
Benjamin Kramer35f59b62010-04-10 16:03:31 +00001887 if (N != 1) Out << 'E';
John McCall09cc1412010-02-03 00:55:45 +00001888 break;
John McCall1dd73832010-02-04 01:42:13 +00001889 }
1890
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +00001891 case Expr::UnaryExprOrTypeTraitExprClass: {
1892 const UnaryExprOrTypeTraitExpr *SAE = cast<UnaryExprOrTypeTraitExpr>(E);
1893 switch(SAE->getKind()) {
1894 case UETT_SizeOf:
1895 Out << 's';
1896 break;
1897 case UETT_AlignOf:
1898 Out << 'a';
1899 break;
1900 case UETT_VecStep:
1901 Diagnostic &Diags = Context.getDiags();
1902 unsigned DiagID = Diags.getCustomDiagID(Diagnostic::Error,
1903 "cannot yet mangle vec_step expression");
1904 Diags.Report(DiagID);
1905 return;
1906 }
John McCall1dd73832010-02-04 01:42:13 +00001907 if (SAE->isArgumentType()) {
Benjamin Kramer35f59b62010-04-10 16:03:31 +00001908 Out << 't';
John McCall1dd73832010-02-04 01:42:13 +00001909 mangleType(SAE->getArgumentType());
1910 } else {
Benjamin Kramer35f59b62010-04-10 16:03:31 +00001911 Out << 'z';
John McCall1dd73832010-02-04 01:42:13 +00001912 mangleExpression(SAE->getArgumentExpr());
1913 }
1914 break;
1915 }
Anders Carlssona7694082009-11-06 02:50:19 +00001916
John McCall0512e482010-07-14 04:20:34 +00001917 case Expr::CXXThrowExprClass: {
1918 const CXXThrowExpr *TE = cast<CXXThrowExpr>(E);
1919
1920 // Proposal from David Vandervoorde, 2010.06.30
1921 if (TE->getSubExpr()) {
1922 Out << "tw";
1923 mangleExpression(TE->getSubExpr());
1924 } else {
1925 Out << "tr";
1926 }
1927 break;
1928 }
1929
1930 case Expr::CXXTypeidExprClass: {
1931 const CXXTypeidExpr *TIE = cast<CXXTypeidExpr>(E);
1932
1933 // Proposal from David Vandervoorde, 2010.06.30
1934 if (TIE->isTypeOperand()) {
1935 Out << "ti";
1936 mangleType(TIE->getTypeOperand());
1937 } else {
1938 Out << "te";
1939 mangleExpression(TIE->getExprOperand());
1940 }
1941 break;
1942 }
1943
1944 case Expr::CXXDeleteExprClass: {
1945 const CXXDeleteExpr *DE = cast<CXXDeleteExpr>(E);
1946
1947 // Proposal from David Vandervoorde, 2010.06.30
1948 if (DE->isGlobalDelete()) Out << "gs";
1949 Out << (DE->isArrayForm() ? "da" : "dl");
1950 mangleExpression(DE->getArgument());
1951 break;
1952 }
1953
Anders Carlssone170ba72009-12-14 01:45:37 +00001954 case Expr::UnaryOperatorClass: {
1955 const UnaryOperator *UO = cast<UnaryOperator>(E);
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001956 mangleOperatorName(UnaryOperator::getOverloadedOperator(UO->getOpcode()),
Anders Carlssone170ba72009-12-14 01:45:37 +00001957 /*Arity=*/1);
1958 mangleExpression(UO->getSubExpr());
1959 break;
1960 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001961
John McCall0512e482010-07-14 04:20:34 +00001962 case Expr::ArraySubscriptExprClass: {
1963 const ArraySubscriptExpr *AE = cast<ArraySubscriptExpr>(E);
1964
1965 // Array subscript is treated as a syntactically wierd form of
1966 // binary operator.
1967 Out << "ix";
1968 mangleExpression(AE->getLHS());
1969 mangleExpression(AE->getRHS());
1970 break;
1971 }
1972
1973 case Expr::CompoundAssignOperatorClass: // fallthrough
Anders Carlssone170ba72009-12-14 01:45:37 +00001974 case Expr::BinaryOperatorClass: {
1975 const BinaryOperator *BO = cast<BinaryOperator>(E);
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001976 mangleOperatorName(BinaryOperator::getOverloadedOperator(BO->getOpcode()),
Anders Carlssone170ba72009-12-14 01:45:37 +00001977 /*Arity=*/2);
1978 mangleExpression(BO->getLHS());
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001979 mangleExpression(BO->getRHS());
Anders Carlssone170ba72009-12-14 01:45:37 +00001980 break;
John McCall2f27bf82010-02-04 02:56:29 +00001981 }
Anders Carlssone170ba72009-12-14 01:45:37 +00001982
1983 case Expr::ConditionalOperatorClass: {
1984 const ConditionalOperator *CO = cast<ConditionalOperator>(E);
1985 mangleOperatorName(OO_Conditional, /*Arity=*/3);
1986 mangleExpression(CO->getCond());
John McCall5e1e89b2010-08-18 19:18:59 +00001987 mangleExpression(CO->getLHS(), Arity);
1988 mangleExpression(CO->getRHS(), Arity);
Anders Carlssone170ba72009-12-14 01:45:37 +00001989 break;
1990 }
1991
Douglas Gregor46287c72010-01-29 16:37:09 +00001992 case Expr::ImplicitCastExprClass: {
John McCall5e1e89b2010-08-18 19:18:59 +00001993 mangleExpression(cast<ImplicitCastExpr>(E)->getSubExpr(), Arity);
Douglas Gregor46287c72010-01-29 16:37:09 +00001994 break;
1995 }
1996
1997 case Expr::CStyleCastExprClass:
1998 case Expr::CXXStaticCastExprClass:
1999 case Expr::CXXDynamicCastExprClass:
2000 case Expr::CXXReinterpretCastExprClass:
2001 case Expr::CXXConstCastExprClass:
2002 case Expr::CXXFunctionalCastExprClass: {
2003 const ExplicitCastExpr *ECE = cast<ExplicitCastExpr>(E);
2004 Out << "cv";
2005 mangleType(ECE->getType());
2006 mangleExpression(ECE->getSubExpr());
2007 break;
2008 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002009
Anders Carlsson58040a52009-12-16 05:48:46 +00002010 case Expr::CXXOperatorCallExprClass: {
2011 const CXXOperatorCallExpr *CE = cast<CXXOperatorCallExpr>(E);
2012 unsigned NumArgs = CE->getNumArgs();
2013 mangleOperatorName(CE->getOperator(), /*Arity=*/NumArgs);
2014 // Mangle the arguments.
2015 for (unsigned i = 0; i != NumArgs; ++i)
2016 mangleExpression(CE->getArg(i));
2017 break;
2018 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002019
Anders Carlssona7694082009-11-06 02:50:19 +00002020 case Expr::ParenExprClass:
John McCall5e1e89b2010-08-18 19:18:59 +00002021 mangleExpression(cast<ParenExpr>(E)->getSubExpr(), Arity);
Anders Carlssona7694082009-11-06 02:50:19 +00002022 break;
2023
Anders Carlssond553f8c2009-09-21 01:21:10 +00002024 case Expr::DeclRefExprClass: {
Douglas Gregor5ed1bc32010-02-28 21:40:32 +00002025 const NamedDecl *D = cast<DeclRefExpr>(E)->getDecl();
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002026
Anders Carlssond553f8c2009-09-21 01:21:10 +00002027 switch (D->getKind()) {
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002028 default:
Douglas Gregor5ed1bc32010-02-28 21:40:32 +00002029 // <expr-primary> ::= L <mangled-name> E # external name
2030 Out << 'L';
2031 mangle(D, "_Z");
2032 Out << 'E';
2033 break;
2034
John McCall3dc7e7b2010-07-24 01:17:35 +00002035 case Decl::EnumConstant: {
2036 const EnumConstantDecl *ED = cast<EnumConstantDecl>(D);
2037 mangleIntegerLiteral(ED->getType(), ED->getInitVal());
2038 break;
2039 }
2040
Anders Carlssond553f8c2009-09-21 01:21:10 +00002041 case Decl::NonTypeTemplateParm: {
2042 const NonTypeTemplateParmDecl *PD = cast<NonTypeTemplateParmDecl>(D);
Anders Carlsson0ccdf8d2009-09-27 00:38:53 +00002043 mangleTemplateParameter(PD->getIndex());
Anders Carlssond553f8c2009-09-21 01:21:10 +00002044 break;
2045 }
2046
2047 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002048
Anders Carlsson50755b02009-09-27 20:11:34 +00002049 break;
Anders Carlssond553f8c2009-09-21 01:21:10 +00002050 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002051
Douglas Gregorc7793c72011-01-15 01:15:58 +00002052 case Expr::SubstNonTypeTemplateParmPackExprClass:
2053 mangleTemplateParameter(
2054 cast<SubstNonTypeTemplateParmPackExpr>(E)->getParameterPack()->getIndex());
2055 break;
2056
John McCall865d4472009-11-19 22:55:06 +00002057 case Expr::DependentScopeDeclRefExprClass: {
2058 const DependentScopeDeclRefExpr *DRE = cast<DependentScopeDeclRefExpr>(E);
Douglas Gregor4b2ccfc2010-02-28 22:05:49 +00002059 NestedNameSpecifier *NNS = DRE->getQualifier();
2060 const Type *QTy = NNS->getAsType();
2061
2062 // When we're dealing with a nested-name-specifier that has just a
2063 // dependent identifier in it, mangle that as a typename. FIXME:
2064 // It isn't clear that we ever actually want to have such a
2065 // nested-name-specifier; why not just represent it as a typename type?
2066 if (!QTy && NNS->getAsIdentifier() && NNS->getPrefix()) {
Douglas Gregor4a2023f2010-03-31 20:19:30 +00002067 QTy = getASTContext().getDependentNameType(ETK_Typename,
2068 NNS->getPrefix(),
2069 NNS->getAsIdentifier())
Douglas Gregor4b2ccfc2010-02-28 22:05:49 +00002070 .getTypePtr();
2071 }
Anders Carlsson50755b02009-09-27 20:11:34 +00002072 assert(QTy && "Qualifier was not type!");
2073
John McCall6dbce192010-08-20 00:17:19 +00002074 // ::= sr <type> <unqualified-name> # dependent name
2075 // ::= sr <type> <unqualified-name> <template-args> # dependent template-id
Anders Carlsson50755b02009-09-27 20:11:34 +00002076 Out << "sr";
2077 mangleType(QualType(QTy, 0));
John McCall5e1e89b2010-08-18 19:18:59 +00002078 mangleUnqualifiedName(0, DRE->getDeclName(), Arity);
John McCall6dbce192010-08-20 00:17:19 +00002079 if (DRE->hasExplicitTemplateArgs())
2080 mangleTemplateArgs(DRE->getExplicitTemplateArgs());
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002081
Anders Carlsson50755b02009-09-27 20:11:34 +00002082 break;
2083 }
2084
John McCalld9307602010-04-09 22:54:09 +00002085 case Expr::CXXBindTemporaryExprClass:
2086 mangleExpression(cast<CXXBindTemporaryExpr>(E)->getSubExpr());
2087 break;
2088
John McCall4765fa02010-12-06 08:20:24 +00002089 case Expr::ExprWithCleanupsClass:
2090 mangleExpression(cast<ExprWithCleanups>(E)->getSubExpr(), Arity);
John McCalld9307602010-04-09 22:54:09 +00002091 break;
2092
John McCall1dd73832010-02-04 01:42:13 +00002093 case Expr::FloatingLiteralClass: {
2094 const FloatingLiteral *FL = cast<FloatingLiteral>(E);
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002095 Out << 'L';
John McCall1dd73832010-02-04 01:42:13 +00002096 mangleType(FL->getType());
John McCall0512e482010-07-14 04:20:34 +00002097 mangleFloat(FL->getValue());
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002098 Out << 'E';
John McCall1dd73832010-02-04 01:42:13 +00002099 break;
2100 }
2101
John McCallde810632010-04-09 21:48:08 +00002102 case Expr::CharacterLiteralClass:
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002103 Out << 'L';
John McCallde810632010-04-09 21:48:08 +00002104 mangleType(E->getType());
2105 Out << cast<CharacterLiteral>(E)->getValue();
2106 Out << 'E';
2107 break;
2108
2109 case Expr::CXXBoolLiteralExprClass:
2110 Out << "Lb";
2111 Out << (cast<CXXBoolLiteralExpr>(E)->getValue() ? '1' : '0');
2112 Out << 'E';
2113 break;
2114
John McCall0512e482010-07-14 04:20:34 +00002115 case Expr::IntegerLiteralClass: {
2116 llvm::APSInt Value(cast<IntegerLiteral>(E)->getValue());
2117 if (E->getType()->isSignedIntegerType())
2118 Value.setIsSigned(true);
2119 mangleIntegerLiteral(E->getType(), Value);
Anders Carlssone170ba72009-12-14 01:45:37 +00002120 break;
John McCall0512e482010-07-14 04:20:34 +00002121 }
2122
2123 case Expr::ImaginaryLiteralClass: {
2124 const ImaginaryLiteral *IE = cast<ImaginaryLiteral>(E);
2125 // Mangle as if a complex literal.
Nick Lewycky271b6652010-09-05 03:40:33 +00002126 // Proposal from David Vandevoorde, 2010.06.30.
John McCall0512e482010-07-14 04:20:34 +00002127 Out << 'L';
2128 mangleType(E->getType());
2129 if (const FloatingLiteral *Imag =
2130 dyn_cast<FloatingLiteral>(IE->getSubExpr())) {
2131 // Mangle a floating-point zero of the appropriate type.
2132 mangleFloat(llvm::APFloat(Imag->getValue().getSemantics()));
2133 Out << '_';
2134 mangleFloat(Imag->getValue());
2135 } else {
Nick Lewycky271b6652010-09-05 03:40:33 +00002136 Out << "0_";
John McCall0512e482010-07-14 04:20:34 +00002137 llvm::APSInt Value(cast<IntegerLiteral>(IE->getSubExpr())->getValue());
2138 if (IE->getSubExpr()->getType()->isSignedIntegerType())
2139 Value.setIsSigned(true);
2140 mangleNumber(Value);
2141 }
2142 Out << 'E';
2143 break;
2144 }
2145
2146 case Expr::StringLiteralClass: {
John McCall1658c392010-07-15 21:53:03 +00002147 // Revised proposal from David Vandervoorde, 2010.07.15.
John McCall0512e482010-07-14 04:20:34 +00002148 Out << 'L';
John McCall1658c392010-07-15 21:53:03 +00002149 assert(isa<ConstantArrayType>(E->getType()));
2150 mangleType(E->getType());
John McCall0512e482010-07-14 04:20:34 +00002151 Out << 'E';
2152 break;
2153 }
2154
2155 case Expr::GNUNullExprClass:
2156 // FIXME: should this really be mangled the same as nullptr?
2157 // fallthrough
2158
2159 case Expr::CXXNullPtrLiteralExprClass: {
2160 // Proposal from David Vandervoorde, 2010.06.30, as
2161 // modified by ABI list discussion.
2162 Out << "LDnE";
2163 break;
2164 }
Douglas Gregorbe230c32011-01-03 17:17:50 +00002165
2166 case Expr::PackExpansionExprClass:
2167 Out << "sp";
2168 mangleExpression(cast<PackExpansionExpr>(E)->getPattern());
2169 break;
Douglas Gregor2e774c42011-01-04 18:56:13 +00002170
2171 case Expr::SizeOfPackExprClass: {
Douglas Gregor2e774c42011-01-04 18:56:13 +00002172 Out << "sZ";
2173 const NamedDecl *Pack = cast<SizeOfPackExpr>(E)->getPack();
2174 if (const TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(Pack))
2175 mangleTemplateParameter(TTP->getIndex());
2176 else if (const NonTypeTemplateParmDecl *NTTP
2177 = dyn_cast<NonTypeTemplateParmDecl>(Pack))
2178 mangleTemplateParameter(NTTP->getIndex());
2179 else if (const TemplateTemplateParmDecl *TempTP
2180 = dyn_cast<TemplateTemplateParmDecl>(Pack))
2181 mangleTemplateParameter(TempTP->getIndex());
2182 else {
Douglas Gregor4fc48662011-01-13 16:39:34 +00002183 // Note: proposed by Mike Herrick on 11/30/10
2184 // <expression> ::= sZ <function-param> # size of function parameter pack
Douglas Gregor2e774c42011-01-04 18:56:13 +00002185 Diagnostic &Diags = Context.getDiags();
2186 unsigned DiagID = Diags.getCustomDiagID(Diagnostic::Error,
2187 "cannot mangle sizeof...(function parameter pack)");
2188 Diags.Report(DiagID);
2189 return;
2190 }
Douglas Gregordfbbcf92011-03-03 02:20:19 +00002191 break;
Douglas Gregor2e774c42011-01-04 18:56:13 +00002192 }
Anders Carlssond553f8c2009-09-21 01:21:10 +00002193 }
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00002194}
2195
Anders Carlsson3ac86b52009-04-15 05:36:58 +00002196void CXXNameMangler::mangleCXXCtorType(CXXCtorType T) {
2197 // <ctor-dtor-name> ::= C1 # complete object constructor
2198 // ::= C2 # base object constructor
2199 // ::= C3 # complete object allocating constructor
2200 //
2201 switch (T) {
2202 case Ctor_Complete:
2203 Out << "C1";
2204 break;
2205 case Ctor_Base:
2206 Out << "C2";
2207 break;
2208 case Ctor_CompleteAllocating:
2209 Out << "C3";
2210 break;
2211 }
2212}
2213
Anders Carlsson27ae5362009-04-17 01:58:57 +00002214void CXXNameMangler::mangleCXXDtorType(CXXDtorType T) {
2215 // <ctor-dtor-name> ::= D0 # deleting destructor
2216 // ::= D1 # complete object destructor
2217 // ::= D2 # base object destructor
2218 //
2219 switch (T) {
2220 case Dtor_Deleting:
2221 Out << "D0";
2222 break;
2223 case Dtor_Complete:
2224 Out << "D1";
2225 break;
2226 case Dtor_Base:
2227 Out << "D2";
2228 break;
2229 }
2230}
2231
John McCall6dbce192010-08-20 00:17:19 +00002232void CXXNameMangler::mangleTemplateArgs(
2233 const ExplicitTemplateArgumentList &TemplateArgs) {
2234 // <template-args> ::= I <template-arg>+ E
2235 Out << 'I';
2236 for (unsigned I = 0, E = TemplateArgs.NumTemplateArgs; I != E; ++I)
2237 mangleTemplateArg(0, TemplateArgs.getTemplateArgs()[I].getArgument());
2238 Out << 'E';
2239}
2240
Douglas Gregor20f0cc72010-04-23 03:10:43 +00002241void CXXNameMangler::mangleTemplateArgs(TemplateName Template,
2242 const TemplateArgument *TemplateArgs,
2243 unsigned NumTemplateArgs) {
2244 if (TemplateDecl *TD = Template.getAsTemplateDecl())
2245 return mangleTemplateArgs(*TD->getTemplateParameters(), TemplateArgs,
2246 NumTemplateArgs);
Sean Huntc3021132010-05-05 15:23:54 +00002247
Douglas Gregor20f0cc72010-04-23 03:10:43 +00002248 // <template-args> ::= I <template-arg>+ E
2249 Out << 'I';
2250 for (unsigned i = 0; i != NumTemplateArgs; ++i)
2251 mangleTemplateArg(0, TemplateArgs[i]);
2252 Out << 'E';
2253}
2254
Rafael Espindolad9800722010-03-11 14:07:00 +00002255void CXXNameMangler::mangleTemplateArgs(const TemplateParameterList &PL,
2256 const TemplateArgumentList &AL) {
Anders Carlsson7a0ba872009-05-15 16:09:15 +00002257 // <template-args> ::= I <template-arg>+ E
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002258 Out << 'I';
Rafael Espindolad9800722010-03-11 14:07:00 +00002259 for (unsigned i = 0, e = AL.size(); i != e; ++i)
2260 mangleTemplateArg(PL.getParam(i), AL[i]);
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002261 Out << 'E';
Anders Carlsson7a0ba872009-05-15 16:09:15 +00002262}
2263
Rafael Espindolad9800722010-03-11 14:07:00 +00002264void CXXNameMangler::mangleTemplateArgs(const TemplateParameterList &PL,
2265 const TemplateArgument *TemplateArgs,
Anders Carlsson7624f212009-09-18 02:42:01 +00002266 unsigned NumTemplateArgs) {
2267 // <template-args> ::= I <template-arg>+ E
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002268 Out << 'I';
Daniel Dunbar7e0c1952009-11-21 09:17:15 +00002269 for (unsigned i = 0; i != NumTemplateArgs; ++i)
Rafael Espindolad9800722010-03-11 14:07:00 +00002270 mangleTemplateArg(PL.getParam(i), TemplateArgs[i]);
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002271 Out << 'E';
Anders Carlsson7624f212009-09-18 02:42:01 +00002272}
2273
Rafael Espindolad9800722010-03-11 14:07:00 +00002274void CXXNameMangler::mangleTemplateArg(const NamedDecl *P,
2275 const TemplateArgument &A) {
Mike Stump1eb44332009-09-09 15:08:12 +00002276 // <template-arg> ::= <type> # type or template
Anders Carlsson7a0ba872009-05-15 16:09:15 +00002277 // ::= X <expression> E # expression
2278 // ::= <expr-primary> # simple expressions
Douglas Gregor4fc48662011-01-13 16:39:34 +00002279 // ::= J <template-arg>* E # argument pack
Anders Carlsson7a0ba872009-05-15 16:09:15 +00002280 // ::= sp <expression> # pack expansion of (C++0x)
2281 switch (A.getKind()) {
Douglas Gregorf90b27a2011-01-03 22:36:02 +00002282 case TemplateArgument::Null:
2283 llvm_unreachable("Cannot mangle NULL template argument");
2284
Anders Carlsson7a0ba872009-05-15 16:09:15 +00002285 case TemplateArgument::Type:
2286 mangleType(A.getAsType());
2287 break;
Anders Carlsson9e85c742009-12-23 19:30:55 +00002288 case TemplateArgument::Template:
John McCallb6f532e2010-07-14 06:43:17 +00002289 // This is mangled as <type>.
2290 mangleType(A.getAsTemplate());
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002291 break;
Douglas Gregora7fc9012011-01-05 18:58:31 +00002292 case TemplateArgument::TemplateExpansion:
Douglas Gregor4fc48662011-01-13 16:39:34 +00002293 // <type> ::= Dp <type> # pack expansion (C++0x)
Douglas Gregora7fc9012011-01-05 18:58:31 +00002294 Out << "Dp";
2295 mangleType(A.getAsTemplateOrTemplatePattern());
2296 break;
Anders Carlssond553f8c2009-09-21 01:21:10 +00002297 case TemplateArgument::Expression:
2298 Out << 'X';
2299 mangleExpression(A.getAsExpr());
2300 Out << 'E';
2301 break;
Anders Carlssone170ba72009-12-14 01:45:37 +00002302 case TemplateArgument::Integral:
2303 mangleIntegerLiteral(A.getIntegralType(), *A.getAsIntegral());
Anders Carlsson7a0ba872009-05-15 16:09:15 +00002304 break;
Daniel Dunbar7e0c1952009-11-21 09:17:15 +00002305 case TemplateArgument::Declaration: {
Douglas Gregor20f0cc72010-04-23 03:10:43 +00002306 assert(P && "Missing template parameter for declaration argument");
Daniel Dunbar7e0c1952009-11-21 09:17:15 +00002307 // <expr-primary> ::= L <mangled-name> E # external name
2308
Rafael Espindolad9800722010-03-11 14:07:00 +00002309 // Clang produces AST's where pointer-to-member-function expressions
Daniel Dunbar7e0c1952009-11-21 09:17:15 +00002310 // and pointer-to-function expressions are represented as a declaration not
Rafael Espindolad9800722010-03-11 14:07:00 +00002311 // an expression. We compensate for it here to produce the correct mangling.
2312 NamedDecl *D = cast<NamedDecl>(A.getAsDecl());
2313 const NonTypeTemplateParmDecl *Parameter = cast<NonTypeTemplateParmDecl>(P);
2314 bool compensateMangling = D->isCXXClassMember() &&
2315 !Parameter->getType()->isReferenceType();
2316 if (compensateMangling) {
2317 Out << 'X';
2318 mangleOperatorName(OO_Amp, 1);
2319 }
2320
Daniel Dunbar7e0c1952009-11-21 09:17:15 +00002321 Out << 'L';
2322 // References to external entities use the mangled name; if the name would
2323 // not normally be manged then mangle it as unqualified.
2324 //
2325 // FIXME: The ABI specifies that external names here should have _Z, but
2326 // gcc leaves this off.
Rafael Espindolad9800722010-03-11 14:07:00 +00002327 if (compensateMangling)
2328 mangle(D, "_Z");
2329 else
2330 mangle(D, "Z");
Daniel Dunbar7e0c1952009-11-21 09:17:15 +00002331 Out << 'E';
Rafael Espindolad9800722010-03-11 14:07:00 +00002332
2333 if (compensateMangling)
2334 Out << 'E';
2335
Daniel Dunbar7e0c1952009-11-21 09:17:15 +00002336 break;
2337 }
Douglas Gregorf90b27a2011-01-03 22:36:02 +00002338
2339 case TemplateArgument::Pack: {
2340 // Note: proposal by Mike Herrick on 12/20/10
2341 Out << 'J';
2342 for (TemplateArgument::pack_iterator PA = A.pack_begin(),
2343 PAEnd = A.pack_end();
2344 PA != PAEnd; ++PA)
2345 mangleTemplateArg(P, *PA);
2346 Out << 'E';
2347 }
Daniel Dunbar7e0c1952009-11-21 09:17:15 +00002348 }
Anders Carlsson7a0ba872009-05-15 16:09:15 +00002349}
2350
Anders Carlsson0ccdf8d2009-09-27 00:38:53 +00002351void CXXNameMangler::mangleTemplateParameter(unsigned Index) {
2352 // <template-param> ::= T_ # first template parameter
2353 // ::= T <parameter-2 non-negative number> _
2354 if (Index == 0)
2355 Out << "T_";
2356 else
2357 Out << 'T' << (Index - 1) << '_';
2358}
2359
Anders Carlsson76967372009-09-17 00:43:46 +00002360// <substitution> ::= S <seq-id> _
2361// ::= S_
Anders Carlsson6862fc72009-09-17 04:16:28 +00002362bool CXXNameMangler::mangleSubstitution(const NamedDecl *ND) {
Anders Carlssone7c8cb62009-09-26 20:53:44 +00002363 // Try one of the standard substitutions first.
2364 if (mangleStandardSubstitution(ND))
2365 return true;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002366
Anders Carlsson433d1372009-11-07 04:26:04 +00002367 ND = cast<NamedDecl>(ND->getCanonicalDecl());
Anders Carlsson6862fc72009-09-17 04:16:28 +00002368 return mangleSubstitution(reinterpret_cast<uintptr_t>(ND));
2369}
2370
Anders Carlsson76967372009-09-17 00:43:46 +00002371bool CXXNameMangler::mangleSubstitution(QualType T) {
Anders Carlssond99edc42009-09-26 03:55:37 +00002372 if (!T.getCVRQualifiers()) {
2373 if (const RecordType *RT = T->getAs<RecordType>())
2374 return mangleSubstitution(RT->getDecl());
2375 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002376
Anders Carlsson76967372009-09-17 00:43:46 +00002377 uintptr_t TypePtr = reinterpret_cast<uintptr_t>(T.getAsOpaquePtr());
2378
Anders Carlssond3a932a2009-09-17 03:53:28 +00002379 return mangleSubstitution(TypePtr);
2380}
2381
Douglas Gregor1e9268e2010-04-28 05:58:56 +00002382bool CXXNameMangler::mangleSubstitution(TemplateName Template) {
2383 if (TemplateDecl *TD = Template.getAsTemplateDecl())
2384 return mangleSubstitution(TD);
Sean Huntc3021132010-05-05 15:23:54 +00002385
Douglas Gregor1e9268e2010-04-28 05:58:56 +00002386 Template = Context.getASTContext().getCanonicalTemplateName(Template);
2387 return mangleSubstitution(
2388 reinterpret_cast<uintptr_t>(Template.getAsVoidPointer()));
2389}
2390
Anders Carlssond3a932a2009-09-17 03:53:28 +00002391bool CXXNameMangler::mangleSubstitution(uintptr_t Ptr) {
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002392 llvm::DenseMap<uintptr_t, unsigned>::iterator I = Substitutions.find(Ptr);
Anders Carlsson76967372009-09-17 00:43:46 +00002393 if (I == Substitutions.end())
2394 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002395
Anders Carlsson76967372009-09-17 00:43:46 +00002396 unsigned SeqID = I->second;
2397 if (SeqID == 0)
2398 Out << "S_";
2399 else {
2400 SeqID--;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002401
Anders Carlsson76967372009-09-17 00:43:46 +00002402 // <seq-id> is encoded in base-36, using digits and upper case letters.
2403 char Buffer[10];
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002404 char *BufferPtr = llvm::array_endof(Buffer);
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002405
Anders Carlsson76967372009-09-17 00:43:46 +00002406 if (SeqID == 0) *--BufferPtr = '0';
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002407
Anders Carlsson76967372009-09-17 00:43:46 +00002408 while (SeqID) {
2409 assert(BufferPtr > Buffer && "Buffer overflow!");
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002410
John McCall6ab30e02010-06-09 07:26:17 +00002411 char c = static_cast<char>(SeqID % 36);
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002412
Anders Carlsson76967372009-09-17 00:43:46 +00002413 *--BufferPtr = (c < 10 ? '0' + c : 'A' + c - 10);
2414 SeqID /= 36;
2415 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002416
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002417 Out << 'S'
2418 << llvm::StringRef(BufferPtr, llvm::array_endof(Buffer)-BufferPtr)
2419 << '_';
Anders Carlsson76967372009-09-17 00:43:46 +00002420 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002421
Anders Carlsson76967372009-09-17 00:43:46 +00002422 return true;
2423}
2424
Anders Carlssonf514b542009-09-27 00:12:57 +00002425static bool isCharType(QualType T) {
2426 if (T.isNull())
2427 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002428
Anders Carlssonf514b542009-09-27 00:12:57 +00002429 return T->isSpecificBuiltinType(BuiltinType::Char_S) ||
2430 T->isSpecificBuiltinType(BuiltinType::Char_U);
2431}
2432
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002433/// isCharSpecialization - Returns whether a given type is a template
Anders Carlssonf514b542009-09-27 00:12:57 +00002434/// specialization of a given name with a single argument of type char.
2435static bool isCharSpecialization(QualType T, const char *Name) {
2436 if (T.isNull())
2437 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002438
Anders Carlssonf514b542009-09-27 00:12:57 +00002439 const RecordType *RT = T->getAs<RecordType>();
2440 if (!RT)
2441 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002442
2443 const ClassTemplateSpecializationDecl *SD =
Anders Carlssonf514b542009-09-27 00:12:57 +00002444 dyn_cast<ClassTemplateSpecializationDecl>(RT->getDecl());
2445 if (!SD)
2446 return false;
2447
2448 if (!isStdNamespace(SD->getDeclContext()))
2449 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002450
Anders Carlssonf514b542009-09-27 00:12:57 +00002451 const TemplateArgumentList &TemplateArgs = SD->getTemplateArgs();
2452 if (TemplateArgs.size() != 1)
2453 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002454
Anders Carlssonf514b542009-09-27 00:12:57 +00002455 if (!isCharType(TemplateArgs[0].getAsType()))
2456 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002457
Daniel Dunbar01eb9b92009-10-18 21:17:35 +00002458 return SD->getIdentifier()->getName() == Name;
Anders Carlssonf514b542009-09-27 00:12:57 +00002459}
2460
Anders Carlsson91f88602009-12-07 19:56:42 +00002461template <std::size_t StrLen>
Benjamin Kramer54353f42010-11-25 18:29:30 +00002462static bool isStreamCharSpecialization(const ClassTemplateSpecializationDecl*SD,
2463 const char (&Str)[StrLen]) {
Anders Carlsson91f88602009-12-07 19:56:42 +00002464 if (!SD->getIdentifier()->isStr(Str))
2465 return false;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002466
Anders Carlsson91f88602009-12-07 19:56:42 +00002467 const TemplateArgumentList &TemplateArgs = SD->getTemplateArgs();
2468 if (TemplateArgs.size() != 2)
2469 return false;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002470
Anders Carlsson91f88602009-12-07 19:56:42 +00002471 if (!isCharType(TemplateArgs[0].getAsType()))
2472 return false;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002473
Anders Carlsson91f88602009-12-07 19:56:42 +00002474 if (!isCharSpecialization(TemplateArgs[1].getAsType(), "char_traits"))
2475 return false;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002476
Anders Carlsson91f88602009-12-07 19:56:42 +00002477 return true;
2478}
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002479
Anders Carlssone7c8cb62009-09-26 20:53:44 +00002480bool CXXNameMangler::mangleStandardSubstitution(const NamedDecl *ND) {
2481 // <substitution> ::= St # ::std::
Anders Carlsson8c031552009-09-26 23:10:05 +00002482 if (const NamespaceDecl *NS = dyn_cast<NamespaceDecl>(ND)) {
Anders Carlsson47846d22009-12-04 06:23:23 +00002483 if (isStd(NS)) {
Anders Carlsson8c031552009-09-26 23:10:05 +00002484 Out << "St";
2485 return true;
2486 }
2487 }
2488
2489 if (const ClassTemplateDecl *TD = dyn_cast<ClassTemplateDecl>(ND)) {
2490 if (!isStdNamespace(TD->getDeclContext()))
2491 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002492
Anders Carlsson8c031552009-09-26 23:10:05 +00002493 // <substitution> ::= Sa # ::std::allocator
2494 if (TD->getIdentifier()->isStr("allocator")) {
2495 Out << "Sa";
2496 return true;
2497 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002498
Anders Carlsson189d59c2009-09-26 23:14:39 +00002499 // <<substitution> ::= Sb # ::std::basic_string
2500 if (TD->getIdentifier()->isStr("basic_string")) {
2501 Out << "Sb";
2502 return true;
2503 }
Anders Carlsson8c031552009-09-26 23:10:05 +00002504 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002505
2506 if (const ClassTemplateSpecializationDecl *SD =
Anders Carlssonf514b542009-09-27 00:12:57 +00002507 dyn_cast<ClassTemplateSpecializationDecl>(ND)) {
Eli Friedman5370ee22010-02-23 18:25:09 +00002508 if (!isStdNamespace(SD->getDeclContext()))
2509 return false;
2510
Anders Carlssonf514b542009-09-27 00:12:57 +00002511 // <substitution> ::= Ss # ::std::basic_string<char,
2512 // ::std::char_traits<char>,
2513 // ::std::allocator<char> >
2514 if (SD->getIdentifier()->isStr("basic_string")) {
2515 const TemplateArgumentList &TemplateArgs = SD->getTemplateArgs();
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002516
Anders Carlssonf514b542009-09-27 00:12:57 +00002517 if (TemplateArgs.size() != 3)
2518 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002519
Anders Carlssonf514b542009-09-27 00:12:57 +00002520 if (!isCharType(TemplateArgs[0].getAsType()))
2521 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002522
Anders Carlssonf514b542009-09-27 00:12:57 +00002523 if (!isCharSpecialization(TemplateArgs[1].getAsType(), "char_traits"))
2524 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002525
Anders Carlssonf514b542009-09-27 00:12:57 +00002526 if (!isCharSpecialization(TemplateArgs[2].getAsType(), "allocator"))
2527 return false;
2528
2529 Out << "Ss";
2530 return true;
2531 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002532
Anders Carlsson91f88602009-12-07 19:56:42 +00002533 // <substitution> ::= Si # ::std::basic_istream<char,
2534 // ::std::char_traits<char> >
2535 if (isStreamCharSpecialization(SD, "basic_istream")) {
2536 Out << "Si";
2537 return true;
2538 }
2539
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002540 // <substitution> ::= So # ::std::basic_ostream<char,
Anders Carlsson8f8fd8e2009-10-08 17:20:26 +00002541 // ::std::char_traits<char> >
Anders Carlsson91f88602009-12-07 19:56:42 +00002542 if (isStreamCharSpecialization(SD, "basic_ostream")) {
Anders Carlsson8f8fd8e2009-10-08 17:20:26 +00002543 Out << "So";
2544 return true;
2545 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002546
Anders Carlsson91f88602009-12-07 19:56:42 +00002547 // <substitution> ::= Sd # ::std::basic_iostream<char,
2548 // ::std::char_traits<char> >
2549 if (isStreamCharSpecialization(SD, "basic_iostream")) {
2550 Out << "Sd";
2551 return true;
2552 }
Anders Carlssonf514b542009-09-27 00:12:57 +00002553 }
Anders Carlsson8c031552009-09-26 23:10:05 +00002554 return false;
Anders Carlssone7c8cb62009-09-26 20:53:44 +00002555}
2556
Anders Carlsson76967372009-09-17 00:43:46 +00002557void CXXNameMangler::addSubstitution(QualType T) {
Anders Carlssond99edc42009-09-26 03:55:37 +00002558 if (!T.getCVRQualifiers()) {
2559 if (const RecordType *RT = T->getAs<RecordType>()) {
2560 addSubstitution(RT->getDecl());
2561 return;
2562 }
2563 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002564
Anders Carlsson76967372009-09-17 00:43:46 +00002565 uintptr_t TypePtr = reinterpret_cast<uintptr_t>(T.getAsOpaquePtr());
Anders Carlssond3a932a2009-09-17 03:53:28 +00002566 addSubstitution(TypePtr);
2567}
2568
Douglas Gregor1e9268e2010-04-28 05:58:56 +00002569void CXXNameMangler::addSubstitution(TemplateName Template) {
2570 if (TemplateDecl *TD = Template.getAsTemplateDecl())
2571 return addSubstitution(TD);
Sean Huntc3021132010-05-05 15:23:54 +00002572
Douglas Gregor1e9268e2010-04-28 05:58:56 +00002573 Template = Context.getASTContext().getCanonicalTemplateName(Template);
2574 addSubstitution(reinterpret_cast<uintptr_t>(Template.getAsVoidPointer()));
2575}
2576
Anders Carlssond3a932a2009-09-17 03:53:28 +00002577void CXXNameMangler::addSubstitution(uintptr_t Ptr) {
Anders Carlssond3a932a2009-09-17 03:53:28 +00002578 assert(!Substitutions.count(Ptr) && "Substitution already exists!");
Anders Carlsson9d85b722010-06-02 04:29:50 +00002579 Substitutions[Ptr] = SeqID++;
Anders Carlsson76967372009-09-17 00:43:46 +00002580}
2581
Daniel Dunbar1b077112009-11-21 09:06:10 +00002582//
Mike Stump1eb44332009-09-09 15:08:12 +00002583
Daniel Dunbar1b077112009-11-21 09:06:10 +00002584/// \brief Mangles the name of the declaration D and emits that name to the
2585/// given output stream.
2586///
2587/// If the declaration D requires a mangled name, this routine will emit that
2588/// mangled name to \p os and return true. Otherwise, \p os will be unchanged
2589/// and this routine will return false. In this case, the caller should just
2590/// emit the identifier of the declaration (\c D->getIdentifier()) as its
2591/// name.
Peter Collingbourne14110472011-01-13 18:57:25 +00002592void ItaniumMangleContext::mangleName(const NamedDecl *D,
Rafael Espindola0e376a02011-02-11 01:41:00 +00002593 llvm::raw_ostream &Out) {
Daniel Dunbarc02ab4c2009-11-21 09:14:44 +00002594 assert((isa<FunctionDecl>(D) || isa<VarDecl>(D)) &&
2595 "Invalid mangleName() call, argument is not a variable or function!");
2596 assert(!isa<CXXConstructorDecl>(D) && !isa<CXXDestructorDecl>(D) &&
2597 "Invalid mangleName() call on 'structor decl!");
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002598
Daniel Dunbar1b077112009-11-21 09:06:10 +00002599 PrettyStackTraceDecl CrashInfo(D, SourceLocation(),
2600 getASTContext().getSourceManager(),
2601 "Mangling declaration");
Mike Stump1eb44332009-09-09 15:08:12 +00002602
Rafael Espindolac4850c22011-02-10 23:59:36 +00002603 CXXNameMangler Mangler(*this, Out);
Daniel Dunbar94fd26d2009-11-21 09:06:22 +00002604 return Mangler.mangle(D);
Daniel Dunbar1b077112009-11-21 09:06:10 +00002605}
Mike Stump1eb44332009-09-09 15:08:12 +00002606
Peter Collingbourne14110472011-01-13 18:57:25 +00002607void ItaniumMangleContext::mangleCXXCtor(const CXXConstructorDecl *D,
2608 CXXCtorType Type,
Rafael Espindola0e376a02011-02-11 01:41:00 +00002609 llvm::raw_ostream &Out) {
Rafael Espindolac4850c22011-02-10 23:59:36 +00002610 CXXNameMangler Mangler(*this, Out, D, Type);
Daniel Dunbar77939c92009-11-21 09:06:31 +00002611 Mangler.mangle(D);
Daniel Dunbar1b077112009-11-21 09:06:10 +00002612}
Mike Stump1eb44332009-09-09 15:08:12 +00002613
Peter Collingbourne14110472011-01-13 18:57:25 +00002614void ItaniumMangleContext::mangleCXXDtor(const CXXDestructorDecl *D,
2615 CXXDtorType Type,
Rafael Espindola0e376a02011-02-11 01:41:00 +00002616 llvm::raw_ostream &Out) {
Rafael Espindolac4850c22011-02-10 23:59:36 +00002617 CXXNameMangler Mangler(*this, Out, D, Type);
Daniel Dunbar77939c92009-11-21 09:06:31 +00002618 Mangler.mangle(D);
Daniel Dunbar1b077112009-11-21 09:06:10 +00002619}
Mike Stumpf1216772009-07-31 18:25:34 +00002620
Peter Collingbourne14110472011-01-13 18:57:25 +00002621void ItaniumMangleContext::mangleThunk(const CXXMethodDecl *MD,
2622 const ThunkInfo &Thunk,
Rafael Espindolaf0be9792011-02-11 02:52:17 +00002623 llvm::raw_ostream &Out) {
Anders Carlsson19879c92010-03-23 17:17:29 +00002624 // <special-name> ::= T <call-offset> <base encoding>
2625 // # base is the nominal target function of thunk
2626 // <special-name> ::= Tc <call-offset> <call-offset> <base encoding>
2627 // # base is the nominal target function of thunk
2628 // # first call-offset is 'this' adjustment
2629 // # second call-offset is result adjustment
Sean Huntc3021132010-05-05 15:23:54 +00002630
Anders Carlsson19879c92010-03-23 17:17:29 +00002631 assert(!isa<CXXDestructorDecl>(MD) &&
2632 "Use mangleCXXDtor for destructor decls!");
Rafael Espindolac4850c22011-02-10 23:59:36 +00002633 CXXNameMangler Mangler(*this, Out);
Anders Carlsson19879c92010-03-23 17:17:29 +00002634 Mangler.getStream() << "_ZT";
2635 if (!Thunk.Return.isEmpty())
2636 Mangler.getStream() << 'c';
Sean Huntc3021132010-05-05 15:23:54 +00002637
Anders Carlsson19879c92010-03-23 17:17:29 +00002638 // Mangle the 'this' pointer adjustment.
2639 Mangler.mangleCallOffset(Thunk.This.NonVirtual, Thunk.This.VCallOffsetOffset);
Sean Huntc3021132010-05-05 15:23:54 +00002640
Anders Carlsson19879c92010-03-23 17:17:29 +00002641 // Mangle the return pointer adjustment if there is one.
2642 if (!Thunk.Return.isEmpty())
2643 Mangler.mangleCallOffset(Thunk.Return.NonVirtual,
2644 Thunk.Return.VBaseOffsetOffset);
Sean Huntc3021132010-05-05 15:23:54 +00002645
Anders Carlsson19879c92010-03-23 17:17:29 +00002646 Mangler.mangleFunctionEncoding(MD);
2647}
2648
Sean Huntc3021132010-05-05 15:23:54 +00002649void
Peter Collingbourne14110472011-01-13 18:57:25 +00002650ItaniumMangleContext::mangleCXXDtorThunk(const CXXDestructorDecl *DD,
2651 CXXDtorType Type,
2652 const ThisAdjustment &ThisAdjustment,
Rafael Espindolaf0be9792011-02-11 02:52:17 +00002653 llvm::raw_ostream &Out) {
Anders Carlsson19879c92010-03-23 17:17:29 +00002654 // <special-name> ::= T <call-offset> <base encoding>
2655 // # base is the nominal target function of thunk
Rafael Espindolac4850c22011-02-10 23:59:36 +00002656 CXXNameMangler Mangler(*this, Out, DD, Type);
Anders Carlsson19879c92010-03-23 17:17:29 +00002657 Mangler.getStream() << "_ZT";
2658
2659 // Mangle the 'this' pointer adjustment.
Sean Huntc3021132010-05-05 15:23:54 +00002660 Mangler.mangleCallOffset(ThisAdjustment.NonVirtual,
Anders Carlsson19879c92010-03-23 17:17:29 +00002661 ThisAdjustment.VCallOffsetOffset);
2662
2663 Mangler.mangleFunctionEncoding(DD);
2664}
2665
Daniel Dunbarc0747712009-11-21 09:12:13 +00002666/// mangleGuardVariable - Returns the mangled name for a guard variable
2667/// for the passed in VarDecl.
Peter Collingbourne14110472011-01-13 18:57:25 +00002668void ItaniumMangleContext::mangleItaniumGuardVariable(const VarDecl *D,
Rafael Espindolaf0be9792011-02-11 02:52:17 +00002669 llvm::raw_ostream &Out) {
Daniel Dunbarc0747712009-11-21 09:12:13 +00002670 // <special-name> ::= GV <object name> # Guard variable for one-time
2671 // # initialization
Rafael Espindolac4850c22011-02-10 23:59:36 +00002672 CXXNameMangler Mangler(*this, Out);
Daniel Dunbarc0747712009-11-21 09:12:13 +00002673 Mangler.getStream() << "_ZGV";
2674 Mangler.mangleName(D);
2675}
2676
Peter Collingbourne14110472011-01-13 18:57:25 +00002677void ItaniumMangleContext::mangleReferenceTemporary(const VarDecl *D,
Rafael Espindolaf0be9792011-02-11 02:52:17 +00002678 llvm::raw_ostream &Out) {
Anders Carlsson715edf22010-06-26 16:09:40 +00002679 // We match the GCC mangling here.
2680 // <special-name> ::= GR <object name>
Rafael Espindolac4850c22011-02-10 23:59:36 +00002681 CXXNameMangler Mangler(*this, Out);
Anders Carlsson715edf22010-06-26 16:09:40 +00002682 Mangler.getStream() << "_ZGR";
2683 Mangler.mangleName(D);
2684}
2685
Peter Collingbourne14110472011-01-13 18:57:25 +00002686void ItaniumMangleContext::mangleCXXVTable(const CXXRecordDecl *RD,
Rafael Espindolaf0be9792011-02-11 02:52:17 +00002687 llvm::raw_ostream &Out) {
Daniel Dunbarc0747712009-11-21 09:12:13 +00002688 // <special-name> ::= TV <type> # virtual table
Rafael Espindolac4850c22011-02-10 23:59:36 +00002689 CXXNameMangler Mangler(*this, Out);
Daniel Dunbarc0747712009-11-21 09:12:13 +00002690 Mangler.getStream() << "_ZTV";
Douglas Gregor1b12a3b2010-05-26 05:11:13 +00002691 Mangler.mangleNameOrStandardSubstitution(RD);
Daniel Dunbar1b077112009-11-21 09:06:10 +00002692}
Mike Stump82d75b02009-11-10 01:58:37 +00002693
Peter Collingbourne14110472011-01-13 18:57:25 +00002694void ItaniumMangleContext::mangleCXXVTT(const CXXRecordDecl *RD,
Rafael Espindolaf0be9792011-02-11 02:52:17 +00002695 llvm::raw_ostream &Out) {
Daniel Dunbarc0747712009-11-21 09:12:13 +00002696 // <special-name> ::= TT <type> # VTT structure
Rafael Espindolac4850c22011-02-10 23:59:36 +00002697 CXXNameMangler Mangler(*this, Out);
Daniel Dunbarc0747712009-11-21 09:12:13 +00002698 Mangler.getStream() << "_ZTT";
Douglas Gregor1b12a3b2010-05-26 05:11:13 +00002699 Mangler.mangleNameOrStandardSubstitution(RD);
Daniel Dunbar1b077112009-11-21 09:06:10 +00002700}
Mike Stumpab3f7e92009-11-10 01:41:59 +00002701
Peter Collingbourne14110472011-01-13 18:57:25 +00002702void ItaniumMangleContext::mangleCXXCtorVTable(const CXXRecordDecl *RD,
2703 int64_t Offset,
2704 const CXXRecordDecl *Type,
Rafael Espindolaf0be9792011-02-11 02:52:17 +00002705 llvm::raw_ostream &Out) {
Daniel Dunbarc0747712009-11-21 09:12:13 +00002706 // <special-name> ::= TC <type> <offset number> _ <base type>
Rafael Espindolac4850c22011-02-10 23:59:36 +00002707 CXXNameMangler Mangler(*this, Out);
Daniel Dunbarc0747712009-11-21 09:12:13 +00002708 Mangler.getStream() << "_ZTC";
Douglas Gregor1b12a3b2010-05-26 05:11:13 +00002709 Mangler.mangleNameOrStandardSubstitution(RD);
Daniel Dunbarc0747712009-11-21 09:12:13 +00002710 Mangler.getStream() << Offset;
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002711 Mangler.getStream() << '_';
Douglas Gregor1b12a3b2010-05-26 05:11:13 +00002712 Mangler.mangleNameOrStandardSubstitution(Type);
Daniel Dunbar1b077112009-11-21 09:06:10 +00002713}
Mike Stump738f8c22009-07-31 23:15:31 +00002714
Peter Collingbourne14110472011-01-13 18:57:25 +00002715void ItaniumMangleContext::mangleCXXRTTI(QualType Ty,
Rafael Espindolaf0be9792011-02-11 02:52:17 +00002716 llvm::raw_ostream &Out) {
Daniel Dunbarc0747712009-11-21 09:12:13 +00002717 // <special-name> ::= TI <type> # typeinfo structure
Douglas Gregor154fe982009-12-23 22:04:40 +00002718 assert(!Ty.hasQualifiers() && "RTTI info cannot have top-level qualifiers");
Rafael Espindolac4850c22011-02-10 23:59:36 +00002719 CXXNameMangler Mangler(*this, Out);
Daniel Dunbarc0747712009-11-21 09:12:13 +00002720 Mangler.getStream() << "_ZTI";
2721 Mangler.mangleType(Ty);
Daniel Dunbar1b077112009-11-21 09:06:10 +00002722}
Mike Stump67795982009-11-14 00:14:13 +00002723
Peter Collingbourne14110472011-01-13 18:57:25 +00002724void ItaniumMangleContext::mangleCXXRTTIName(QualType Ty,
Rafael Espindolaf0be9792011-02-11 02:52:17 +00002725 llvm::raw_ostream &Out) {
Daniel Dunbarc0747712009-11-21 09:12:13 +00002726 // <special-name> ::= TS <type> # typeinfo name (null terminated byte string)
Rafael Espindolac4850c22011-02-10 23:59:36 +00002727 CXXNameMangler Mangler(*this, Out);
Daniel Dunbarc0747712009-11-21 09:12:13 +00002728 Mangler.getStream() << "_ZTS";
2729 Mangler.mangleType(Ty);
Mike Stumpf1216772009-07-31 18:25:34 +00002730}
Peter Collingbourne14110472011-01-13 18:57:25 +00002731
2732MangleContext *clang::createItaniumMangleContext(ASTContext &Context,
2733 Diagnostic &Diags) {
2734 return new ItaniumMangleContext(Context, Diags);
2735}