blob: be17ccf2d823384e6bc8d2b75d046da473bfc7ab [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
Peter Collingbourne69317432011-04-06 12:29:09 +0000337 // marker. We also avoid adding the marker if this is an alias for an
338 // LLVM intrinsic.
Rafael Espindola4e274e92011-02-15 22:23:51 +0000339 llvm::StringRef UserLabelPrefix =
340 getASTContext().Target.getUserLabelPrefix();
Peter Collingbourne69317432011-04-06 12:29:09 +0000341 if (!UserLabelPrefix.empty() && !ALA->getLabel().startswith("llvm."))
Rafael Espindola4e274e92011-02-15 22:23:51 +0000342 Out << '\01'; // LLVM IR Marker for __asm("foo")
343
Chris Lattnerca3f25c2009-03-21 08:24:40 +0000344 Out << ALA->getLabel();
Daniel Dunbarf981bf82009-11-21 09:14:52 +0000345 return;
Chris Lattnerca3f25c2009-03-21 08:24:40 +0000346 }
Mike Stump1eb44332009-09-09 15:08:12 +0000347
Sean Hunt31455252010-01-24 03:04:27 +0000348 // <mangled-name> ::= _Z <encoding>
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000349 // ::= <data name>
350 // ::= <special-name>
Daniel Dunbar7e0c1952009-11-21 09:17:15 +0000351 Out << Prefix;
352 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
Daniel Dunbarf981bf82009-11-21 09:14:52 +0000353 mangleFunctionEncoding(FD);
Rafael Espindolad9800722010-03-11 14:07:00 +0000354 else if (const VarDecl *VD = dyn_cast<VarDecl>(D))
355 mangleName(VD);
Daniel Dunbar7e0c1952009-11-21 09:17:15 +0000356 else
Rafael Espindolad9800722010-03-11 14:07:00 +0000357 mangleName(cast<FieldDecl>(D));
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000358}
359
360void CXXNameMangler::mangleFunctionEncoding(const FunctionDecl *FD) {
361 // <encoding> ::= <function name> <bare-function-type>
362 mangleName(FD);
Mike Stump1eb44332009-09-09 15:08:12 +0000363
Daniel Dunbar7e0c1952009-11-21 09:17:15 +0000364 // Don't mangle in the type if this isn't a decl we should typically mangle.
365 if (!Context.shouldMangleDeclName(FD))
366 return;
367
Mike Stump141c5af2009-09-02 00:25:38 +0000368 // Whether the mangling of a function type includes the return type depends on
369 // the context and the nature of the function. The rules for deciding whether
370 // the return type is included are:
Mike Stump1eb44332009-09-09 15:08:12 +0000371 //
Douglas Gregor1fd2dd12009-06-29 22:39:32 +0000372 // 1. Template functions (names or types) have return types encoded, with
373 // the exceptions listed below.
Mike Stump1eb44332009-09-09 15:08:12 +0000374 // 2. Function types not appearing as part of a function name mangling,
Douglas Gregor1fd2dd12009-06-29 22:39:32 +0000375 // e.g. parameters, pointer types, etc., have return type encoded, with the
376 // exceptions listed below.
377 // 3. Non-template function names do not have return types encoded.
378 //
Mike Stump141c5af2009-09-02 00:25:38 +0000379 // The exceptions mentioned in (1) and (2) above, for which the return type is
380 // never included, are
Douglas Gregor1fd2dd12009-06-29 22:39:32 +0000381 // 1. Constructors.
382 // 2. Destructors.
383 // 3. Conversion operator functions, e.g. operator int.
384 bool MangleReturnType = false;
Anders Carlsson9234b7f2009-09-17 03:46:43 +0000385 if (FunctionTemplateDecl *PrimaryTemplate = FD->getPrimaryTemplate()) {
386 if (!(isa<CXXConstructorDecl>(FD) || isa<CXXDestructorDecl>(FD) ||
387 isa<CXXConversionDecl>(FD)))
388 MangleReturnType = true;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000389
Anders Carlsson9234b7f2009-09-17 03:46:43 +0000390 // Mangle the type of the primary template.
391 FD = PrimaryTemplate->getTemplatedDecl();
392 }
393
John McCall54e14c42009-10-22 22:37:11 +0000394 // Do the canonicalization out here because parameter types can
395 // undergo additional canonicalization (e.g. array decay).
John McCallf4c73712011-01-19 06:33:43 +0000396 const FunctionType *FT
397 = cast<FunctionType>(Context.getASTContext()
John McCall54e14c42009-10-22 22:37:11 +0000398 .getCanonicalType(FD->getType()));
399
400 mangleBareFunctionType(FT, MangleReturnType);
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000401}
402
Anders Carlsson47846d22009-12-04 06:23:23 +0000403static const DeclContext *IgnoreLinkageSpecDecls(const DeclContext *DC) {
404 while (isa<LinkageSpecDecl>(DC)) {
Anders Carlsson47846d22009-12-04 06:23:23 +0000405 DC = DC->getParent();
406 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000407
Anders Carlsson47846d22009-12-04 06:23:23 +0000408 return DC;
409}
410
Anders Carlssonc820f902010-06-02 15:58:27 +0000411/// isStd - Return whether a given namespace is the 'std' namespace.
412static bool isStd(const NamespaceDecl *NS) {
413 if (!IgnoreLinkageSpecDecls(NS->getParent())->isTranslationUnit())
414 return false;
415
416 const IdentifierInfo *II = NS->getOriginalNamespace()->getIdentifier();
417 return II && II->isStr("std");
418}
419
Anders Carlsson47846d22009-12-04 06:23:23 +0000420// isStdNamespace - Return whether a given decl context is a toplevel 'std'
421// namespace.
Daniel Dunbar1308af92009-11-21 09:11:45 +0000422static bool isStdNamespace(const DeclContext *DC) {
Anders Carlsson47846d22009-12-04 06:23:23 +0000423 if (!DC->isNamespace())
424 return false;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000425
Anders Carlsson47846d22009-12-04 06:23:23 +0000426 return isStd(cast<NamespaceDecl>(DC));
Daniel Dunbar1308af92009-11-21 09:11:45 +0000427}
428
Anders Carlssonbb36ba42009-09-26 03:24:57 +0000429static const TemplateDecl *
430isTemplate(const NamedDecl *ND, const TemplateArgumentList *&TemplateArgs) {
Anders Carlsson2744a062009-09-18 19:00:18 +0000431 // Check if we have a function template.
432 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)){
Anders Carlssonbb36ba42009-09-26 03:24:57 +0000433 if (const TemplateDecl *TD = FD->getPrimaryTemplate()) {
Anders Carlsson2744a062009-09-18 19:00:18 +0000434 TemplateArgs = FD->getTemplateSpecializationArgs();
Anders Carlssonbb36ba42009-09-26 03:24:57 +0000435 return TD;
Anders Carlsson2744a062009-09-18 19:00:18 +0000436 }
437 }
438
Anders Carlssoneafc6dc2009-09-18 19:44:50 +0000439 // Check if we have a class template.
440 if (const ClassTemplateSpecializationDecl *Spec =
441 dyn_cast<ClassTemplateSpecializationDecl>(ND)) {
442 TemplateArgs = &Spec->getTemplateArgs();
Anders Carlssonbb36ba42009-09-26 03:24:57 +0000443 return Spec->getSpecializedTemplate();
Anders Carlssoneafc6dc2009-09-18 19:44:50 +0000444 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000445
Anders Carlsson2744a062009-09-18 19:00:18 +0000446 return 0;
447}
448
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000449void CXXNameMangler::mangleName(const NamedDecl *ND) {
450 // <name> ::= <nested-name>
451 // ::= <unscoped-name>
452 // ::= <unscoped-template-name> <template-args>
Anders Carlsson201ce742009-09-17 03:17:01 +0000453 // ::= <local-name>
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000454 //
Anders Carlssond58d6f72009-09-17 16:12:20 +0000455 const DeclContext *DC = ND->getDeclContext();
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000456
Eli Friedman7facf842009-12-02 20:32:49 +0000457 // If this is an extern variable declared locally, the relevant DeclContext
458 // is that of the containing namespace, or the translation unit.
459 if (isa<FunctionDecl>(DC) && ND->hasLinkage())
460 while (!DC->isNamespace() && !DC->isTranslationUnit())
461 DC = DC->getParent();
John McCall82b7d7b2010-10-18 21:28:44 +0000462 else if (GetLocalClassDecl(ND)) {
463 mangleLocalName(ND);
464 return;
465 }
Eli Friedman7facf842009-12-02 20:32:49 +0000466
Anders Carlsson5cc58c62009-09-22 17:23:30 +0000467 while (isa<LinkageSpecDecl>(DC))
Anders Carlssond58d6f72009-09-17 16:12:20 +0000468 DC = DC->getParent();
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000469
Anders Carlssond58d6f72009-09-17 16:12:20 +0000470 if (DC->isTranslationUnit() || isStdNamespace(DC)) {
Anders Carlsson2744a062009-09-18 19:00:18 +0000471 // Check if we have a template.
472 const TemplateArgumentList *TemplateArgs = 0;
Anders Carlsson0fa6df42009-09-26 19:45:45 +0000473 if (const TemplateDecl *TD = isTemplate(ND, TemplateArgs)) {
Anders Carlsson2744a062009-09-18 19:00:18 +0000474 mangleUnscopedTemplateName(TD);
Rafael Espindolad9800722010-03-11 14:07:00 +0000475 TemplateParameterList *TemplateParameters = TD->getTemplateParameters();
476 mangleTemplateArgs(*TemplateParameters, *TemplateArgs);
Anders Carlsson2744a062009-09-18 19:00:18 +0000477 return;
Anders Carlsson7482e242009-09-18 04:29:09 +0000478 }
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000479
Anders Carlsson7482e242009-09-18 04:29:09 +0000480 mangleUnscopedName(ND);
481 return;
482 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000483
Anders Carlsson7b06f6c2009-12-10 03:14:39 +0000484 if (isa<FunctionDecl>(DC) || isa<ObjCMethodDecl>(DC)) {
Anders Carlsson7482e242009-09-18 04:29:09 +0000485 mangleLocalName(ND);
486 return;
487 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000488
Eli Friedman7facf842009-12-02 20:32:49 +0000489 mangleNestedName(ND, DC);
Anders Carlsson7482e242009-09-18 04:29:09 +0000490}
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000491void CXXNameMangler::mangleName(const TemplateDecl *TD,
Anders Carlsson7624f212009-09-18 02:42:01 +0000492 const TemplateArgument *TemplateArgs,
493 unsigned NumTemplateArgs) {
Anders Carlsson47846d22009-12-04 06:23:23 +0000494 const DeclContext *DC = IgnoreLinkageSpecDecls(TD->getDeclContext());
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000495
Anders Carlsson7624f212009-09-18 02:42:01 +0000496 if (DC->isTranslationUnit() || isStdNamespace(DC)) {
Anders Carlsson0fa6df42009-09-26 19:45:45 +0000497 mangleUnscopedTemplateName(TD);
Rafael Espindolad9800722010-03-11 14:07:00 +0000498 TemplateParameterList *TemplateParameters = TD->getTemplateParameters();
499 mangleTemplateArgs(*TemplateParameters, TemplateArgs, NumTemplateArgs);
Anders Carlsson7624f212009-09-18 02:42:01 +0000500 } else {
501 mangleNestedName(TD, TemplateArgs, NumTemplateArgs);
502 }
503}
504
Anders Carlsson201ce742009-09-17 03:17:01 +0000505void CXXNameMangler::mangleUnscopedName(const NamedDecl *ND) {
506 // <unscoped-name> ::= <unqualified-name>
507 // ::= St <unqualified-name> # ::std::
508 if (isStdNamespace(ND->getDeclContext()))
509 Out << "St";
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000510
Anders Carlsson201ce742009-09-17 03:17:01 +0000511 mangleUnqualifiedName(ND);
512}
513
Anders Carlsson0fa6df42009-09-26 19:45:45 +0000514void CXXNameMangler::mangleUnscopedTemplateName(const TemplateDecl *ND) {
Anders Carlsson201ce742009-09-17 03:17:01 +0000515 // <unscoped-template-name> ::= <unscoped-name>
516 // ::= <substitution>
Anders Carlsson7624f212009-09-18 02:42:01 +0000517 if (mangleSubstitution(ND))
Anders Carlsson03c9d532009-09-17 04:02:31 +0000518 return;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000519
Douglas Gregor32fb4e12010-02-05 20:45:00 +0000520 // <template-template-param> ::= <template-param>
521 if (const TemplateTemplateParmDecl *TTP
522 = dyn_cast<TemplateTemplateParmDecl>(ND)) {
523 mangleTemplateParameter(TTP->getIndex());
524 return;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000525 }
Douglas Gregor32fb4e12010-02-05 20:45:00 +0000526
Anders Carlsson1668f202009-09-26 20:13:56 +0000527 mangleUnscopedName(ND->getTemplatedDecl());
Anders Carlsson7624f212009-09-18 02:42:01 +0000528 addSubstitution(ND);
Anders Carlsson201ce742009-09-17 03:17:01 +0000529}
530
Douglas Gregor1e9268e2010-04-28 05:58:56 +0000531void CXXNameMangler::mangleUnscopedTemplateName(TemplateName Template) {
532 // <unscoped-template-name> ::= <unscoped-name>
533 // ::= <substitution>
534 if (TemplateDecl *TD = Template.getAsTemplateDecl())
535 return mangleUnscopedTemplateName(TD);
Sean Huntc3021132010-05-05 15:23:54 +0000536
Douglas Gregor1e9268e2010-04-28 05:58:56 +0000537 if (mangleSubstitution(Template))
538 return;
539
540 // FIXME: How to cope with operators here?
541 DependentTemplateName *Dependent = Template.getAsDependentTemplateName();
542 assert(Dependent && "Not a dependent template name?");
543 if (!Dependent->isIdentifier()) {
544 // FIXME: We can't possibly know the arity of the operator here!
545 Diagnostic &Diags = Context.getDiags();
546 unsigned DiagID = Diags.getCustomDiagID(Diagnostic::Error,
547 "cannot mangle dependent operator name");
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000548 Diags.Report(DiagID);
Douglas Gregor1e9268e2010-04-28 05:58:56 +0000549 return;
550 }
Sean Huntc3021132010-05-05 15:23:54 +0000551
Douglas Gregor1e9268e2010-04-28 05:58:56 +0000552 mangleSourceName(Dependent->getIdentifier());
553 addSubstitution(Template);
554}
555
John McCall0512e482010-07-14 04:20:34 +0000556void CXXNameMangler::mangleFloat(const llvm::APFloat &F) {
557 // TODO: avoid this copy with careful stream management.
558 llvm::SmallString<20> Buffer;
559 F.bitcastToAPInt().toString(Buffer, 16, false);
560 Out.write(Buffer.data(), Buffer.size());
561}
562
563void CXXNameMangler::mangleNumber(const llvm::APSInt &Value) {
564 if (Value.isSigned() && Value.isNegative()) {
565 Out << 'n';
566 Value.abs().print(Out, true);
567 } else
568 Value.print(Out, Value.isSigned());
569}
570
Anders Carlssona94822e2009-11-26 02:32:05 +0000571void CXXNameMangler::mangleNumber(int64_t Number) {
572 // <number> ::= [n] <non-negative decimal integer>
573 if (Number < 0) {
574 Out << 'n';
575 Number = -Number;
576 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000577
Anders Carlssona94822e2009-11-26 02:32:05 +0000578 Out << Number;
579}
580
Anders Carlsson19879c92010-03-23 17:17:29 +0000581void CXXNameMangler::mangleCallOffset(int64_t NonVirtual, int64_t Virtual) {
Mike Stump141c5af2009-09-02 00:25:38 +0000582 // <call-offset> ::= h <nv-offset> _
583 // ::= v <v-offset> _
584 // <nv-offset> ::= <offset number> # non-virtual base override
Anders Carlssona94822e2009-11-26 02:32:05 +0000585 // <v-offset> ::= <offset number> _ <virtual offset number>
Mike Stump141c5af2009-09-02 00:25:38 +0000586 // # virtual base override, with vcall offset
Anders Carlsson19879c92010-03-23 17:17:29 +0000587 if (!Virtual) {
Anders Carlssona94822e2009-11-26 02:32:05 +0000588 Out << 'h';
Anders Carlsson19879c92010-03-23 17:17:29 +0000589 mangleNumber(NonVirtual);
Anders Carlssona94822e2009-11-26 02:32:05 +0000590 Out << '_';
591 return;
Mike Stump141c5af2009-09-02 00:25:38 +0000592 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000593
Anders Carlssona94822e2009-11-26 02:32:05 +0000594 Out << 'v';
Anders Carlsson19879c92010-03-23 17:17:29 +0000595 mangleNumber(NonVirtual);
Anders Carlssona94822e2009-11-26 02:32:05 +0000596 Out << '_';
Anders Carlsson19879c92010-03-23 17:17:29 +0000597 mangleNumber(Virtual);
Anders Carlssona94822e2009-11-26 02:32:05 +0000598 Out << '_';
Mike Stump9124bcc2009-09-02 00:56:18 +0000599}
600
John McCall1dd73832010-02-04 01:42:13 +0000601void CXXNameMangler::mangleUnresolvedScope(NestedNameSpecifier *Qualifier) {
602 Qualifier = getASTContext().getCanonicalNestedNameSpecifier(Qualifier);
603 switch (Qualifier->getKind()) {
604 case NestedNameSpecifier::Global:
605 // nothing
606 break;
607 case NestedNameSpecifier::Namespace:
608 mangleName(Qualifier->getAsNamespace());
609 break;
Douglas Gregor14aba762011-02-24 02:36:08 +0000610 case NestedNameSpecifier::NamespaceAlias:
611 mangleName(Qualifier->getAsNamespaceAlias()->getNamespace());
612 break;
John McCall1dd73832010-02-04 01:42:13 +0000613 case NestedNameSpecifier::TypeSpec:
Rafael Espindola9b35b252010-03-17 04:28:11 +0000614 case NestedNameSpecifier::TypeSpecWithTemplate: {
615 const Type *QTy = Qualifier->getAsType();
616
617 if (const TemplateSpecializationType *TST =
618 dyn_cast<TemplateSpecializationType>(QTy)) {
619 if (!mangleSubstitution(QualType(TST, 0))) {
Douglas Gregor20f0cc72010-04-23 03:10:43 +0000620 mangleTemplatePrefix(TST->getTemplateName());
Sean Huntc3021132010-05-05 15:23:54 +0000621
Douglas Gregor20f0cc72010-04-23 03:10:43 +0000622 // FIXME: GCC does not appear to mangle the template arguments when
623 // the template in question is a dependent template name. Should we
624 // emulate that badness?
625 mangleTemplateArgs(TST->getTemplateName(), TST->getArgs(),
Rafael Espindola9b35b252010-03-17 04:28:11 +0000626 TST->getNumArgs());
627 addSubstitution(QualType(TST, 0));
628 }
Douglas Gregoraa2187d2011-02-28 00:04:36 +0000629 } else if (const DependentTemplateSpecializationType *DTST
630 = dyn_cast<DependentTemplateSpecializationType>(QTy)) {
631 TemplateName Template
632 = getASTContext().getDependentTemplateName(DTST->getQualifier(),
633 DTST->getIdentifier());
634 mangleTemplatePrefix(Template);
635
636 // FIXME: GCC does not appear to mangle the template arguments when
637 // the template in question is a dependent template name. Should we
638 // emulate that badness?
639 mangleTemplateArgs(Template, DTST->getArgs(), DTST->getNumArgs());
Rafael Espindola9b35b252010-03-17 04:28:11 +0000640 } else {
641 // We use the QualType mangle type variant here because it handles
642 // substitutions.
643 mangleType(QualType(QTy, 0));
644 }
645 }
John McCall1dd73832010-02-04 01:42:13 +0000646 break;
647 case NestedNameSpecifier::Identifier:
John McCallad5e7382010-03-01 23:49:17 +0000648 // Member expressions can have these without prefixes.
649 if (Qualifier->getPrefix())
650 mangleUnresolvedScope(Qualifier->getPrefix());
John McCall1dd73832010-02-04 01:42:13 +0000651 mangleSourceName(Qualifier->getAsIdentifier());
652 break;
653 }
654}
655
656/// Mangles a name which was not resolved to a specific entity.
657void CXXNameMangler::mangleUnresolvedName(NestedNameSpecifier *Qualifier,
658 DeclarationName Name,
659 unsigned KnownArity) {
660 if (Qualifier)
661 mangleUnresolvedScope(Qualifier);
662 // FIXME: ambiguity of unqualified lookup with ::
663
664 mangleUnqualifiedName(0, Name, KnownArity);
665}
666
Anders Carlsson6f7e2f42010-06-08 14:49:03 +0000667static const FieldDecl *FindFirstNamedDataMember(const RecordDecl *RD) {
668 assert(RD->isAnonymousStructOrUnion() &&
669 "Expected anonymous struct or union!");
670
671 for (RecordDecl::field_iterator I = RD->field_begin(), E = RD->field_end();
672 I != E; ++I) {
673 const FieldDecl *FD = *I;
674
675 if (FD->getIdentifier())
676 return FD;
677
678 if (const RecordType *RT = FD->getType()->getAs<RecordType>()) {
679 if (const FieldDecl *NamedDataMember =
680 FindFirstNamedDataMember(RT->getDecl()))
681 return NamedDataMember;
682 }
683 }
684
685 // We didn't find a named data member.
686 return 0;
687}
688
John McCall1dd73832010-02-04 01:42:13 +0000689void CXXNameMangler::mangleUnqualifiedName(const NamedDecl *ND,
690 DeclarationName Name,
691 unsigned KnownArity) {
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000692 // <unqualified-name> ::= <operator-name>
Mike Stump1eb44332009-09-09 15:08:12 +0000693 // ::= <ctor-dtor-name>
694 // ::= <source-name>
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000695 switch (Name.getNameKind()) {
Anders Carlssonc4355b62009-10-07 01:45:02 +0000696 case DeclarationName::Identifier: {
Anders Carlssonc4355b62009-10-07 01:45:02 +0000697 if (const IdentifierInfo *II = Name.getAsIdentifierInfo()) {
Sean Hunt31455252010-01-24 03:04:27 +0000698 // We must avoid conflicts between internally- and externally-
John McCall74990f42011-03-22 06:34:45 +0000699 // linked variable and function declaration names in the same TU:
700 // void test() { extern void foo(); }
701 // static void foo();
702 // This naming convention is the same as that followed by GCC,
703 // though it shouldn't actually matter.
704 if (ND && ND->getLinkage() == InternalLinkage &&
Sean Hunt31455252010-01-24 03:04:27 +0000705 ND->getDeclContext()->isFileContext())
706 Out << 'L';
707
Anders Carlssonc4355b62009-10-07 01:45:02 +0000708 mangleSourceName(II);
709 break;
710 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000711
John McCall1dd73832010-02-04 01:42:13 +0000712 // Otherwise, an anonymous entity. We must have a declaration.
713 assert(ND && "mangling empty name without declaration");
714
715 if (const NamespaceDecl *NS = dyn_cast<NamespaceDecl>(ND)) {
716 if (NS->isAnonymousNamespace()) {
717 // This is how gcc mangles these names.
718 Out << "12_GLOBAL__N_1";
719 break;
720 }
721 }
722
Anders Carlsson6f7e2f42010-06-08 14:49:03 +0000723 if (const VarDecl *VD = dyn_cast<VarDecl>(ND)) {
724 // We must have an anonymous union or struct declaration.
725 const RecordDecl *RD =
726 cast<RecordDecl>(VD->getType()->getAs<RecordType>()->getDecl());
727
728 // Itanium C++ ABI 5.1.2:
729 //
730 // For the purposes of mangling, the name of an anonymous union is
731 // considered to be the name of the first named data member found by a
732 // pre-order, depth-first, declaration-order walk of the data members of
733 // the anonymous union. If there is no such data member (i.e., if all of
734 // the data members in the union are unnamed), then there is no way for
735 // a program to refer to the anonymous union, and there is therefore no
736 // need to mangle its name.
737 const FieldDecl *FD = FindFirstNamedDataMember(RD);
John McCall7121c8f2010-08-05 22:02:13 +0000738
739 // It's actually possible for various reasons for us to get here
740 // with an empty anonymous struct / union. Fortunately, it
741 // doesn't really matter what name we generate.
742 if (!FD) break;
Anders Carlsson6f7e2f42010-06-08 14:49:03 +0000743 assert(FD->getIdentifier() && "Data member name isn't an identifier!");
744
745 mangleSourceName(FD->getIdentifier());
746 break;
747 }
748
Anders Carlssonc4355b62009-10-07 01:45:02 +0000749 // We must have an anonymous struct.
750 const TagDecl *TD = cast<TagDecl>(ND);
751 if (const TypedefDecl *D = TD->getTypedefForAnonDecl()) {
752 assert(TD->getDeclContext() == D->getDeclContext() &&
753 "Typedef should not be in another decl context!");
754 assert(D->getDeclName().getAsIdentifierInfo() &&
755 "Typedef was not named!");
756 mangleSourceName(D->getDeclName().getAsIdentifierInfo());
757 break;
758 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000759
Anders Carlssonc4355b62009-10-07 01:45:02 +0000760 // Get a unique id for the anonymous struct.
761 uint64_t AnonStructId = Context.getAnonymousStructId(TD);
762
763 // Mangle it as a source name in the form
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000764 // [n] $_<id>
Anders Carlssonc4355b62009-10-07 01:45:02 +0000765 // where n is the length of the string.
766 llvm::SmallString<8> Str;
767 Str += "$_";
768 Str += llvm::utostr(AnonStructId);
769
770 Out << Str.size();
771 Out << Str.str();
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000772 break;
Anders Carlssonc4355b62009-10-07 01:45:02 +0000773 }
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000774
775 case DeclarationName::ObjCZeroArgSelector:
776 case DeclarationName::ObjCOneArgSelector:
777 case DeclarationName::ObjCMultiArgSelector:
778 assert(false && "Can't mangle Objective-C selector names here!");
779 break;
780
781 case DeclarationName::CXXConstructorName:
Anders Carlsson27ae5362009-04-17 01:58:57 +0000782 if (ND == Structor)
Mike Stump141c5af2009-09-02 00:25:38 +0000783 // If the named decl is the C++ constructor we're mangling, use the type
784 // we were given.
Anders Carlsson27ae5362009-04-17 01:58:57 +0000785 mangleCXXCtorType(static_cast<CXXCtorType>(StructorType));
Anders Carlsson3ac86b52009-04-15 05:36:58 +0000786 else
787 // Otherwise, use the complete constructor name. This is relevant if a
788 // class with a constructor is declared within a constructor.
789 mangleCXXCtorType(Ctor_Complete);
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000790 break;
791
792 case DeclarationName::CXXDestructorName:
Anders Carlsson27ae5362009-04-17 01:58:57 +0000793 if (ND == Structor)
Mike Stump141c5af2009-09-02 00:25:38 +0000794 // If the named decl is the C++ destructor we're mangling, use the type we
795 // were given.
Anders Carlsson27ae5362009-04-17 01:58:57 +0000796 mangleCXXDtorType(static_cast<CXXDtorType>(StructorType));
797 else
798 // Otherwise, use the complete destructor name. This is relevant if a
799 // class with a destructor is declared within a destructor.
800 mangleCXXDtorType(Dtor_Complete);
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000801 break;
802
803 case DeclarationName::CXXConversionFunctionName:
Mike Stump1eb44332009-09-09 15:08:12 +0000804 // <operator-name> ::= cv <type> # (cast)
Douglas Gregor219cc612009-02-13 01:28:03 +0000805 Out << "cv";
Anders Carlssonb5404912009-10-07 01:06:45 +0000806 mangleType(Context.getASTContext().getCanonicalType(Name.getCXXNameType()));
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000807 break;
808
Anders Carlsson8257d412009-12-22 06:36:32 +0000809 case DeclarationName::CXXOperatorName: {
John McCall1dd73832010-02-04 01:42:13 +0000810 unsigned Arity;
811 if (ND) {
812 Arity = cast<FunctionDecl>(ND)->getNumParams();
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000813
John McCall1dd73832010-02-04 01:42:13 +0000814 // If we have a C++ member function, we need to include the 'this' pointer.
815 // FIXME: This does not make sense for operators that are static, but their
816 // names stay the same regardless of the arity (operator new for instance).
817 if (isa<CXXMethodDecl>(ND))
818 Arity++;
819 } else
820 Arity = KnownArity;
821
Anders Carlsson8257d412009-12-22 06:36:32 +0000822 mangleOperatorName(Name.getCXXOverloadedOperator(), Arity);
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000823 break;
Anders Carlsson8257d412009-12-22 06:36:32 +0000824 }
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000825
Sean Hunt3e518bd2009-11-29 07:34:05 +0000826 case DeclarationName::CXXLiteralOperatorName:
Sean Hunt5dd6b392009-12-04 21:11:13 +0000827 // FIXME: This mangling is not yet official.
Sean Hunt2421f662009-12-04 21:01:37 +0000828 Out << "li";
Sean Hunt3e518bd2009-11-29 07:34:05 +0000829 mangleSourceName(Name.getCXXLiteralIdentifier());
830 break;
831
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000832 case DeclarationName::CXXUsingDirective:
833 assert(false && "Can't mangle a using directive name!");
Douglas Gregor219cc612009-02-13 01:28:03 +0000834 break;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000835 }
836}
837
838void CXXNameMangler::mangleSourceName(const IdentifierInfo *II) {
839 // <source-name> ::= <positive length number> <identifier>
840 // <number> ::= [n] <non-negative decimal integer>
841 // <identifier> ::= <unqualified source code identifier>
842 Out << II->getLength() << II->getName();
843}
844
Eli Friedman7facf842009-12-02 20:32:49 +0000845void CXXNameMangler::mangleNestedName(const NamedDecl *ND,
Fariborz Jahanian57058532010-03-03 19:41:08 +0000846 const DeclContext *DC,
847 bool NoFunction) {
Douglas Gregor0a9a6d62011-01-26 17:36:28 +0000848 // <nested-name>
849 // ::= N [<CV-qualifiers>] [<ref-qualifier>] <prefix> <unqualified-name> E
850 // ::= N [<CV-qualifiers>] [<ref-qualifier>] <template-prefix>
851 // <template-args> E
Anders Carlssond99edc42009-09-26 03:55:37 +0000852
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000853 Out << 'N';
Douglas Gregor0a9a6d62011-01-26 17:36:28 +0000854 if (const CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(ND)) {
John McCall0953e762009-09-24 19:53:00 +0000855 mangleQualifiers(Qualifiers::fromCVRMask(Method->getTypeQualifiers()));
Douglas Gregor0a9a6d62011-01-26 17:36:28 +0000856 mangleRefQualifier(Method->getRefQualifier());
857 }
858
Anders Carlsson2744a062009-09-18 19:00:18 +0000859 // Check if we have a template.
860 const TemplateArgumentList *TemplateArgs = 0;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000861 if (const TemplateDecl *TD = isTemplate(ND, TemplateArgs)) {
Anders Carlsson2744a062009-09-18 19:00:18 +0000862 mangleTemplatePrefix(TD);
Rafael Espindolad9800722010-03-11 14:07:00 +0000863 TemplateParameterList *TemplateParameters = TD->getTemplateParameters();
864 mangleTemplateArgs(*TemplateParameters, *TemplateArgs);
Fariborz Jahanian57058532010-03-03 19:41:08 +0000865 }
866 else {
867 manglePrefix(DC, NoFunction);
Anders Carlsson7482e242009-09-18 04:29:09 +0000868 mangleUnqualifiedName(ND);
869 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000870
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000871 Out << 'E';
872}
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000873void CXXNameMangler::mangleNestedName(const TemplateDecl *TD,
Anders Carlsson7624f212009-09-18 02:42:01 +0000874 const TemplateArgument *TemplateArgs,
875 unsigned NumTemplateArgs) {
Anders Carlssone45117b2009-09-27 19:53:49 +0000876 // <nested-name> ::= N [<CV-qualifiers>] <template-prefix> <template-args> E
877
Anders Carlsson7624f212009-09-18 02:42:01 +0000878 Out << 'N';
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000879
Anders Carlssone45117b2009-09-27 19:53:49 +0000880 mangleTemplatePrefix(TD);
Rafael Espindolad9800722010-03-11 14:07:00 +0000881 TemplateParameterList *TemplateParameters = TD->getTemplateParameters();
882 mangleTemplateArgs(*TemplateParameters, TemplateArgs, NumTemplateArgs);
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000883
Anders Carlsson7624f212009-09-18 02:42:01 +0000884 Out << 'E';
885}
886
Anders Carlsson1b42c792009-04-02 16:24:45 +0000887void CXXNameMangler::mangleLocalName(const NamedDecl *ND) {
888 // <local-name> := Z <function encoding> E <entity name> [<discriminator>]
889 // := Z <function encoding> E s [<discriminator>]
Mike Stump1eb44332009-09-09 15:08:12 +0000890 // <discriminator> := _ <non-negative number>
Fariborz Jahanian57058532010-03-03 19:41:08 +0000891 const DeclContext *DC = ND->getDeclContext();
Anders Carlsson1b42c792009-04-02 16:24:45 +0000892 Out << 'Z';
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000893
Charles Davis685b1d92010-05-26 18:25:27 +0000894 if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(DC)) {
895 mangleObjCMethodName(MD);
John McCall82b7d7b2010-10-18 21:28:44 +0000896 } else if (const CXXRecordDecl *RD = GetLocalClassDecl(ND)) {
897 mangleFunctionEncoding(cast<FunctionDecl>(RD->getDeclContext()));
Fariborz Jahanian57058532010-03-03 19:41:08 +0000898 Out << 'E';
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000899
John McCall82b7d7b2010-10-18 21:28:44 +0000900 // Mangle the name relative to the closest enclosing function.
901 if (ND == RD) // equality ok because RD derived from ND above
902 mangleUnqualifiedName(ND);
903 else
904 mangleNestedName(ND, DC, true /*NoFunction*/);
905
Fariborz Jahanian4819ac42010-03-04 01:02:03 +0000906 unsigned disc;
John McCall82b7d7b2010-10-18 21:28:44 +0000907 if (Context.getNextDiscriminator(RD, disc)) {
Fariborz Jahanian4819ac42010-03-04 01:02:03 +0000908 if (disc < 10)
909 Out << '_' << disc;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000910 else
Fariborz Jahanian4819ac42010-03-04 01:02:03 +0000911 Out << "__" << disc << '_';
912 }
Fariborz Jahanian57058532010-03-03 19:41:08 +0000913
914 return;
915 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000916 else
Fariborz Jahanian57058532010-03-03 19:41:08 +0000917 mangleFunctionEncoding(cast<FunctionDecl>(DC));
Anders Carlsson7b06f6c2009-12-10 03:14:39 +0000918
Anders Carlsson1b42c792009-04-02 16:24:45 +0000919 Out << 'E';
Eli Friedman6f9f25d2009-12-11 20:21:38 +0000920 mangleUnqualifiedName(ND);
Anders Carlsson1b42c792009-04-02 16:24:45 +0000921}
922
Fariborz Jahanian57058532010-03-03 19:41:08 +0000923void CXXNameMangler::manglePrefix(const DeclContext *DC, bool NoFunction) {
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000924 // <prefix> ::= <prefix> <unqualified-name>
925 // ::= <template-prefix> <template-args>
926 // ::= <template-param>
927 // ::= # empty
928 // ::= <substitution>
Anders Carlsson6862fc72009-09-17 04:16:28 +0000929
Anders Carlssonadd28822009-09-22 20:33:31 +0000930 while (isa<LinkageSpecDecl>(DC))
931 DC = DC->getParent();
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000932
Anders Carlsson9263e912009-09-18 18:39:58 +0000933 if (DC->isTranslationUnit())
934 return;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000935
Douglas Gregor35415f52010-05-25 17:04:15 +0000936 if (const BlockDecl *Block = dyn_cast<BlockDecl>(DC)) {
937 manglePrefix(DC->getParent(), NoFunction);
938 llvm::SmallString<64> Name;
Rafael Espindolac4850c22011-02-10 23:59:36 +0000939 llvm::raw_svector_ostream NameStream(Name);
940 Context.mangleBlock(Block, NameStream);
941 NameStream.flush();
Douglas Gregor35415f52010-05-25 17:04:15 +0000942 Out << Name.size() << Name;
943 return;
944 }
945
Anders Carlsson6862fc72009-09-17 04:16:28 +0000946 if (mangleSubstitution(cast<NamedDecl>(DC)))
947 return;
Anders Carlsson7482e242009-09-18 04:29:09 +0000948
Anders Carlsson2ee3fca2009-09-18 20:11:09 +0000949 // Check if we have a template.
950 const TemplateArgumentList *TemplateArgs = 0;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000951 if (const TemplateDecl *TD = isTemplate(cast<NamedDecl>(DC), TemplateArgs)) {
Anders Carlsson2ee3fca2009-09-18 20:11:09 +0000952 mangleTemplatePrefix(TD);
Rafael Espindolad9800722010-03-11 14:07:00 +0000953 TemplateParameterList *TemplateParameters = TD->getTemplateParameters();
954 mangleTemplateArgs(*TemplateParameters, *TemplateArgs);
Fariborz Jahanian57058532010-03-03 19:41:08 +0000955 }
Douglas Gregor35415f52010-05-25 17:04:15 +0000956 else if(NoFunction && (isa<FunctionDecl>(DC) || isa<ObjCMethodDecl>(DC)))
Fariborz Jahanian57058532010-03-03 19:41:08 +0000957 return;
Douglas Gregor35415f52010-05-25 17:04:15 +0000958 else if (const ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(DC))
959 mangleObjCMethodName(Method);
Fariborz Jahanian57058532010-03-03 19:41:08 +0000960 else {
961 manglePrefix(DC->getParent(), NoFunction);
Anders Carlsson2ee3fca2009-09-18 20:11:09 +0000962 mangleUnqualifiedName(cast<NamedDecl>(DC));
963 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000964
Anders Carlsson6862fc72009-09-17 04:16:28 +0000965 addSubstitution(cast<NamedDecl>(DC));
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000966}
967
Douglas Gregor20f0cc72010-04-23 03:10:43 +0000968void CXXNameMangler::mangleTemplatePrefix(TemplateName Template) {
969 // <template-prefix> ::= <prefix> <template unqualified-name>
970 // ::= <template-param>
971 // ::= <substitution>
972 if (TemplateDecl *TD = Template.getAsTemplateDecl())
973 return mangleTemplatePrefix(TD);
974
975 if (QualifiedTemplateName *Qualified = Template.getAsQualifiedTemplateName())
976 mangleUnresolvedScope(Qualified->getQualifier());
Sean Huntc3021132010-05-05 15:23:54 +0000977
Douglas Gregor20f0cc72010-04-23 03:10:43 +0000978 if (OverloadedTemplateStorage *Overloaded
979 = Template.getAsOverloadedTemplate()) {
Sean Huntc3021132010-05-05 15:23:54 +0000980 mangleUnqualifiedName(0, (*Overloaded->begin())->getDeclName(),
Douglas Gregor20f0cc72010-04-23 03:10:43 +0000981 UnknownArity);
982 return;
983 }
Sean Huntc3021132010-05-05 15:23:54 +0000984
Douglas Gregor20f0cc72010-04-23 03:10:43 +0000985 DependentTemplateName *Dependent = Template.getAsDependentTemplateName();
986 assert(Dependent && "Unknown template name kind?");
987 mangleUnresolvedScope(Dependent->getQualifier());
Douglas Gregor1e9268e2010-04-28 05:58:56 +0000988 mangleUnscopedTemplateName(Template);
Douglas Gregor20f0cc72010-04-23 03:10:43 +0000989}
990
Anders Carlsson0fa6df42009-09-26 19:45:45 +0000991void CXXNameMangler::mangleTemplatePrefix(const TemplateDecl *ND) {
Anders Carlsson7482e242009-09-18 04:29:09 +0000992 // <template-prefix> ::= <prefix> <template unqualified-name>
993 // ::= <template-param>
994 // ::= <substitution>
Douglas Gregor32fb4e12010-02-05 20:45:00 +0000995 // <template-template-param> ::= <template-param>
996 // <substitution>
Anders Carlsson7482e242009-09-18 04:29:09 +0000997
Anders Carlssonaeb85372009-09-26 22:18:22 +0000998 if (mangleSubstitution(ND))
999 return;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00001000
Douglas Gregor32fb4e12010-02-05 20:45:00 +00001001 // <template-template-param> ::= <template-param>
1002 if (const TemplateTemplateParmDecl *TTP
1003 = dyn_cast<TemplateTemplateParmDecl>(ND)) {
1004 mangleTemplateParameter(TTP->getIndex());
1005 return;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001006 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00001007
Anders Carlssonaa73ab12009-09-18 18:47:07 +00001008 manglePrefix(ND->getDeclContext());
Anders Carlsson1668f202009-09-26 20:13:56 +00001009 mangleUnqualifiedName(ND->getTemplatedDecl());
Anders Carlssonaeb85372009-09-26 22:18:22 +00001010 addSubstitution(ND);
Anders Carlsson7482e242009-09-18 04:29:09 +00001011}
1012
John McCallb6f532e2010-07-14 06:43:17 +00001013/// Mangles a template name under the production <type>. Required for
1014/// template template arguments.
1015/// <type> ::= <class-enum-type>
1016/// ::= <template-param>
1017/// ::= <substitution>
1018void CXXNameMangler::mangleType(TemplateName TN) {
1019 if (mangleSubstitution(TN))
1020 return;
1021
1022 TemplateDecl *TD = 0;
1023
1024 switch (TN.getKind()) {
1025 case TemplateName::QualifiedTemplate:
1026 TD = TN.getAsQualifiedTemplateName()->getTemplateDecl();
1027 goto HaveDecl;
1028
1029 case TemplateName::Template:
1030 TD = TN.getAsTemplateDecl();
1031 goto HaveDecl;
1032
1033 HaveDecl:
1034 if (isa<TemplateTemplateParmDecl>(TD))
1035 mangleTemplateParameter(cast<TemplateTemplateParmDecl>(TD)->getIndex());
1036 else
1037 mangleName(TD);
1038 break;
1039
1040 case TemplateName::OverloadedTemplate:
1041 llvm_unreachable("can't mangle an overloaded template name as a <type>");
1042 break;
1043
1044 case TemplateName::DependentTemplate: {
1045 const DependentTemplateName *Dependent = TN.getAsDependentTemplateName();
1046 assert(Dependent->isIdentifier());
1047
1048 // <class-enum-type> ::= <name>
1049 // <name> ::= <nested-name>
1050 mangleUnresolvedScope(Dependent->getQualifier());
1051 mangleSourceName(Dependent->getIdentifier());
1052 break;
1053 }
1054
Douglas Gregor1aee05d2011-01-15 06:45:20 +00001055 case TemplateName::SubstTemplateTemplateParmPack: {
1056 SubstTemplateTemplateParmPackStorage *SubstPack
1057 = TN.getAsSubstTemplateTemplateParmPack();
1058 mangleTemplateParameter(SubstPack->getParameterPack()->getIndex());
1059 break;
1060 }
John McCallb6f532e2010-07-14 06:43:17 +00001061 }
1062
1063 addSubstitution(TN);
1064}
1065
Mike Stump1eb44332009-09-09 15:08:12 +00001066void
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001067CXXNameMangler::mangleOperatorName(OverloadedOperatorKind OO, unsigned Arity) {
1068 switch (OO) {
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001069 // <operator-name> ::= nw # new
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001070 case OO_New: Out << "nw"; break;
1071 // ::= na # new[]
1072 case OO_Array_New: Out << "na"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001073 // ::= dl # delete
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001074 case OO_Delete: Out << "dl"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001075 // ::= da # delete[]
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001076 case OO_Array_Delete: Out << "da"; break;
1077 // ::= ps # + (unary)
John McCall5e1e89b2010-08-18 19:18:59 +00001078 // ::= pl # + (binary or unknown)
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001079 case OO_Plus:
Anders Carlsson8257d412009-12-22 06:36:32 +00001080 Out << (Arity == 1? "ps" : "pl"); break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001081 // ::= ng # - (unary)
John McCall5e1e89b2010-08-18 19:18:59 +00001082 // ::= mi # - (binary or unknown)
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001083 case OO_Minus:
Anders Carlsson8257d412009-12-22 06:36:32 +00001084 Out << (Arity == 1? "ng" : "mi"); break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001085 // ::= ad # & (unary)
John McCall5e1e89b2010-08-18 19:18:59 +00001086 // ::= an # & (binary or unknown)
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001087 case OO_Amp:
Anders Carlsson8257d412009-12-22 06:36:32 +00001088 Out << (Arity == 1? "ad" : "an"); break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001089 // ::= de # * (unary)
John McCall5e1e89b2010-08-18 19:18:59 +00001090 // ::= ml # * (binary or unknown)
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001091 case OO_Star:
John McCall5e1e89b2010-08-18 19:18:59 +00001092 // Use binary when unknown.
Anders Carlsson8257d412009-12-22 06:36:32 +00001093 Out << (Arity == 1? "de" : "ml"); break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001094 // ::= co # ~
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001095 case OO_Tilde: Out << "co"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001096 // ::= dv # /
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001097 case OO_Slash: Out << "dv"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001098 // ::= rm # %
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001099 case OO_Percent: Out << "rm"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001100 // ::= or # |
1101 case OO_Pipe: Out << "or"; break;
1102 // ::= eo # ^
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001103 case OO_Caret: Out << "eo"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001104 // ::= aS # =
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001105 case OO_Equal: Out << "aS"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001106 // ::= pL # +=
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001107 case OO_PlusEqual: Out << "pL"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001108 // ::= mI # -=
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001109 case OO_MinusEqual: Out << "mI"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001110 // ::= mL # *=
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001111 case OO_StarEqual: Out << "mL"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001112 // ::= dV # /=
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001113 case OO_SlashEqual: Out << "dV"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001114 // ::= rM # %=
1115 case OO_PercentEqual: Out << "rM"; break;
1116 // ::= aN # &=
1117 case OO_AmpEqual: Out << "aN"; break;
1118 // ::= oR # |=
1119 case OO_PipeEqual: Out << "oR"; break;
1120 // ::= eO # ^=
1121 case OO_CaretEqual: Out << "eO"; break;
1122 // ::= ls # <<
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001123 case OO_LessLess: Out << "ls"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001124 // ::= rs # >>
1125 case OO_GreaterGreater: Out << "rs"; break;
1126 // ::= lS # <<=
1127 case OO_LessLessEqual: Out << "lS"; break;
1128 // ::= rS # >>=
1129 case OO_GreaterGreaterEqual: Out << "rS"; break;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001130 // ::= eq # ==
1131 case OO_EqualEqual: Out << "eq"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001132 // ::= ne # !=
1133 case OO_ExclaimEqual: Out << "ne"; break;
1134 // ::= lt # <
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001135 case OO_Less: Out << "lt"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001136 // ::= gt # >
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001137 case OO_Greater: Out << "gt"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001138 // ::= le # <=
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001139 case OO_LessEqual: Out << "le"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001140 // ::= ge # >=
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001141 case OO_GreaterEqual: Out << "ge"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001142 // ::= nt # !
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001143 case OO_Exclaim: Out << "nt"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001144 // ::= aa # &&
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001145 case OO_AmpAmp: Out << "aa"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001146 // ::= oo # ||
1147 case OO_PipePipe: Out << "oo"; break;
1148 // ::= pp # ++
1149 case OO_PlusPlus: Out << "pp"; break;
1150 // ::= mm # --
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001151 case OO_MinusMinus: Out << "mm"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001152 // ::= cm # ,
1153 case OO_Comma: Out << "cm"; break;
1154 // ::= pm # ->*
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001155 case OO_ArrowStar: Out << "pm"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001156 // ::= pt # ->
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001157 case OO_Arrow: Out << "pt"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001158 // ::= cl # ()
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001159 case OO_Call: Out << "cl"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001160 // ::= ix # []
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001161 case OO_Subscript: Out << "ix"; break;
Anders Carlssone170ba72009-12-14 01:45:37 +00001162
1163 // ::= qu # ?
1164 // The conditional operator can't be overloaded, but we still handle it when
1165 // mangling expressions.
1166 case OO_Conditional: Out << "qu"; break;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001167
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001168 case OO_None:
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001169 case NUM_OVERLOADED_OPERATORS:
Mike Stump1eb44332009-09-09 15:08:12 +00001170 assert(false && "Not an overloaded operator");
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001171 break;
1172 }
1173}
1174
John McCall0953e762009-09-24 19:53:00 +00001175void CXXNameMangler::mangleQualifiers(Qualifiers Quals) {
Mike Stump1eb44332009-09-09 15:08:12 +00001176 // <CV-qualifiers> ::= [r] [V] [K] # restrict (C99), volatile, const
John McCall0953e762009-09-24 19:53:00 +00001177 if (Quals.hasRestrict())
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001178 Out << 'r';
John McCall0953e762009-09-24 19:53:00 +00001179 if (Quals.hasVolatile())
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001180 Out << 'V';
John McCall0953e762009-09-24 19:53:00 +00001181 if (Quals.hasConst())
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001182 Out << 'K';
John McCall0953e762009-09-24 19:53:00 +00001183
Douglas Gregor56079f72010-06-14 23:15:08 +00001184 if (Quals.hasAddressSpace()) {
1185 // Extension:
1186 //
1187 // <type> ::= U <address-space-number>
1188 //
1189 // where <address-space-number> is a source name consisting of 'AS'
1190 // followed by the address space <number>.
1191 llvm::SmallString<64> ASString;
1192 ASString = "AS" + llvm::utostr_32(Quals.getAddressSpace());
1193 Out << 'U' << ASString.size() << ASString;
1194 }
1195
John McCall0953e762009-09-24 19:53:00 +00001196 // FIXME: For now, just drop all extension qualifiers on the floor.
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001197}
1198
Douglas Gregor0a9a6d62011-01-26 17:36:28 +00001199void CXXNameMangler::mangleRefQualifier(RefQualifierKind RefQualifier) {
1200 // <ref-qualifier> ::= R # lvalue reference
1201 // ::= O # rvalue-reference
1202 // Proposal to Itanium C++ ABI list on 1/26/11
1203 switch (RefQualifier) {
1204 case RQ_None:
1205 break;
1206
1207 case RQ_LValue:
1208 Out << 'R';
1209 break;
1210
1211 case RQ_RValue:
1212 Out << 'O';
1213 break;
1214 }
1215}
1216
Anders Carlsson7b06f6c2009-12-10 03:14:39 +00001217void CXXNameMangler::mangleObjCMethodName(const ObjCMethodDecl *MD) {
Rafael Espindolaf0be9792011-02-11 02:52:17 +00001218 Context.mangleObjCMethodName(MD, Out);
Anders Carlsson7b06f6c2009-12-10 03:14:39 +00001219}
1220
John McCallb47f7482011-01-26 20:05:40 +00001221void CXXNameMangler::mangleType(QualType nonCanon) {
Anders Carlsson4843e582009-03-10 17:07:44 +00001222 // Only operate on the canonical type!
John McCallb47f7482011-01-26 20:05:40 +00001223 QualType canon = nonCanon.getCanonicalType();
Anders Carlsson4843e582009-03-10 17:07:44 +00001224
John McCallb47f7482011-01-26 20:05:40 +00001225 SplitQualType split = canon.split();
1226 Qualifiers quals = split.second;
1227 const Type *ty = split.first;
1228
1229 bool isSubstitutable = quals || !isa<BuiltinType>(ty);
1230 if (isSubstitutable && mangleSubstitution(canon))
Anders Carlsson76967372009-09-17 00:43:46 +00001231 return;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001232
John McCallb47f7482011-01-26 20:05:40 +00001233 // If we're mangling a qualified array type, push the qualifiers to
1234 // the element type.
1235 if (quals && isa<ArrayType>(ty)) {
1236 ty = Context.getASTContext().getAsArrayType(canon);
1237 quals = Qualifiers();
1238
1239 // Note that we don't update canon: we want to add the
1240 // substitution at the canonical type.
1241 }
1242
1243 if (quals) {
1244 mangleQualifiers(quals);
John McCall0953e762009-09-24 19:53:00 +00001245 // Recurse: even if the qualified type isn't yet substitutable,
1246 // the unqualified type might be.
John McCallb47f7482011-01-26 20:05:40 +00001247 mangleType(QualType(ty, 0));
Anders Carlsson76967372009-09-17 00:43:46 +00001248 } else {
John McCallb47f7482011-01-26 20:05:40 +00001249 switch (ty->getTypeClass()) {
John McCallefe6aee2009-09-05 07:56:18 +00001250#define ABSTRACT_TYPE(CLASS, PARENT)
1251#define NON_CANONICAL_TYPE(CLASS, PARENT) \
Anders Carlsson76967372009-09-17 00:43:46 +00001252 case Type::CLASS: \
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +00001253 llvm_unreachable("can't mangle non-canonical type " #CLASS "Type"); \
Anders Carlsson76967372009-09-17 00:43:46 +00001254 return;
John McCallefe6aee2009-09-05 07:56:18 +00001255#define TYPE(CLASS, PARENT) \
Anders Carlsson76967372009-09-17 00:43:46 +00001256 case Type::CLASS: \
John McCallb47f7482011-01-26 20:05:40 +00001257 mangleType(static_cast<const CLASS##Type*>(ty)); \
Anders Carlsson76967372009-09-17 00:43:46 +00001258 break;
John McCallefe6aee2009-09-05 07:56:18 +00001259#include "clang/AST/TypeNodes.def"
Anders Carlsson76967372009-09-17 00:43:46 +00001260 }
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001261 }
Anders Carlsson76967372009-09-17 00:43:46 +00001262
1263 // Add the substitution.
John McCallb47f7482011-01-26 20:05:40 +00001264 if (isSubstitutable)
1265 addSubstitution(canon);
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001266}
1267
Douglas Gregor1b12a3b2010-05-26 05:11:13 +00001268void CXXNameMangler::mangleNameOrStandardSubstitution(const NamedDecl *ND) {
1269 if (!mangleStandardSubstitution(ND))
1270 mangleName(ND);
1271}
1272
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001273void CXXNameMangler::mangleType(const BuiltinType *T) {
John McCallefe6aee2009-09-05 07:56:18 +00001274 // <type> ::= <builtin-type>
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001275 // <builtin-type> ::= v # void
1276 // ::= w # wchar_t
1277 // ::= b # bool
1278 // ::= c # char
1279 // ::= a # signed char
1280 // ::= h # unsigned char
1281 // ::= s # short
1282 // ::= t # unsigned short
1283 // ::= i # int
1284 // ::= j # unsigned int
1285 // ::= l # long
1286 // ::= m # unsigned long
1287 // ::= x # long long, __int64
1288 // ::= y # unsigned long long, __int64
1289 // ::= n # __int128
1290 // UNSUPPORTED: ::= o # unsigned __int128
1291 // ::= f # float
1292 // ::= d # double
1293 // ::= e # long double, __float80
1294 // UNSUPPORTED: ::= g # __float128
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001295 // UNSUPPORTED: ::= Dd # IEEE 754r decimal floating point (64 bits)
1296 // UNSUPPORTED: ::= De # IEEE 754r decimal floating point (128 bits)
1297 // UNSUPPORTED: ::= Df # IEEE 754r decimal floating point (32 bits)
1298 // UNSUPPORTED: ::= Dh # IEEE 754r half-precision floating point (16 bits)
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001299 // ::= Di # char32_t
1300 // ::= Ds # char16_t
Anders Carlssone2923682010-11-04 04:31:32 +00001301 // ::= Dn # std::nullptr_t (i.e., decltype(nullptr))
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001302 // ::= u <source-name> # vendor extended type
1303 switch (T->getKind()) {
1304 case BuiltinType::Void: Out << 'v'; break;
1305 case BuiltinType::Bool: Out << 'b'; break;
1306 case BuiltinType::Char_U: case BuiltinType::Char_S: Out << 'c'; break;
1307 case BuiltinType::UChar: Out << 'h'; break;
1308 case BuiltinType::UShort: Out << 't'; break;
1309 case BuiltinType::UInt: Out << 'j'; break;
1310 case BuiltinType::ULong: Out << 'm'; break;
1311 case BuiltinType::ULongLong: Out << 'y'; break;
Chris Lattner2df9ced2009-04-30 02:43:43 +00001312 case BuiltinType::UInt128: Out << 'o'; break;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001313 case BuiltinType::SChar: Out << 'a'; break;
Chris Lattner3f59c972010-12-25 23:25:43 +00001314 case BuiltinType::WChar_S:
1315 case BuiltinType::WChar_U: Out << 'w'; break;
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001316 case BuiltinType::Char16: Out << "Ds"; break;
1317 case BuiltinType::Char32: Out << "Di"; break;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001318 case BuiltinType::Short: Out << 's'; break;
1319 case BuiltinType::Int: Out << 'i'; break;
1320 case BuiltinType::Long: Out << 'l'; break;
1321 case BuiltinType::LongLong: Out << 'x'; break;
Chris Lattner2df9ced2009-04-30 02:43:43 +00001322 case BuiltinType::Int128: Out << 'n'; break;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001323 case BuiltinType::Float: Out << 'f'; break;
1324 case BuiltinType::Double: Out << 'd'; break;
1325 case BuiltinType::LongDouble: Out << 'e'; break;
Anders Carlssone2923682010-11-04 04:31:32 +00001326 case BuiltinType::NullPtr: Out << "Dn"; break;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001327
1328 case BuiltinType::Overload:
1329 case BuiltinType::Dependent:
John McCall1de4d4e2011-04-07 08:22:57 +00001330 case BuiltinType::UnknownAny:
Mike Stump1eb44332009-09-09 15:08:12 +00001331 assert(false &&
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001332 "Overloaded and dependent types shouldn't get to name mangling");
1333 break;
Steve Naroff9533a7f2009-07-22 17:14:51 +00001334 case BuiltinType::ObjCId: Out << "11objc_object"; break;
1335 case BuiltinType::ObjCClass: Out << "10objc_class"; break;
Fariborz Jahanian13dcd002009-11-21 19:53:08 +00001336 case BuiltinType::ObjCSel: Out << "13objc_selector"; break;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001337 }
1338}
1339
John McCallefe6aee2009-09-05 07:56:18 +00001340// <type> ::= <function-type>
1341// <function-type> ::= F [Y] <bare-function-type> E
1342void CXXNameMangler::mangleType(const FunctionProtoType *T) {
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001343 Out << 'F';
Mike Stumpf5408fe2009-05-16 07:57:57 +00001344 // FIXME: We don't have enough information in the AST to produce the 'Y'
1345 // encoding for extern "C" function types.
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001346 mangleBareFunctionType(T, /*MangleReturnType=*/true);
1347 Out << 'E';
1348}
John McCallefe6aee2009-09-05 07:56:18 +00001349void CXXNameMangler::mangleType(const FunctionNoProtoType *T) {
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +00001350 llvm_unreachable("Can't mangle K&R function prototypes");
John McCallefe6aee2009-09-05 07:56:18 +00001351}
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001352void CXXNameMangler::mangleBareFunctionType(const FunctionType *T,
1353 bool MangleReturnType) {
John McCallefe6aee2009-09-05 07:56:18 +00001354 // We should never be mangling something without a prototype.
1355 const FunctionProtoType *Proto = cast<FunctionProtoType>(T);
1356
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001357 // <bare-function-type> ::= <signature type>+
1358 if (MangleReturnType)
John McCallefe6aee2009-09-05 07:56:18 +00001359 mangleType(Proto->getResultType());
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001360
Anders Carlsson93296682010-06-02 04:40:13 +00001361 if (Proto->getNumArgs() == 0 && !Proto->isVariadic()) {
Eli Friedmana7e68452010-08-22 01:00:03 +00001362 // <builtin-type> ::= v # void
Anders Carlssonc6c91bc2009-04-01 00:15:23 +00001363 Out << 'v';
1364 return;
1365 }
Mike Stump1eb44332009-09-09 15:08:12 +00001366
Douglas Gregor72564e72009-02-26 23:50:07 +00001367 for (FunctionProtoType::arg_type_iterator Arg = Proto->arg_type_begin(),
Mike Stump1eb44332009-09-09 15:08:12 +00001368 ArgEnd = Proto->arg_type_end();
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001369 Arg != ArgEnd; ++Arg)
1370 mangleType(*Arg);
Douglas Gregor219cc612009-02-13 01:28:03 +00001371
1372 // <builtin-type> ::= z # ellipsis
1373 if (Proto->isVariadic())
1374 Out << 'z';
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001375}
1376
John McCallefe6aee2009-09-05 07:56:18 +00001377// <type> ::= <class-enum-type>
Mike Stump1eb44332009-09-09 15:08:12 +00001378// <class-enum-type> ::= <name>
John McCalled976492009-12-04 22:46:56 +00001379void CXXNameMangler::mangleType(const UnresolvedUsingType *T) {
1380 mangleName(T->getDecl());
1381}
1382
1383// <type> ::= <class-enum-type>
1384// <class-enum-type> ::= <name>
John McCallefe6aee2009-09-05 07:56:18 +00001385void CXXNameMangler::mangleType(const EnumType *T) {
1386 mangleType(static_cast<const TagType*>(T));
1387}
1388void CXXNameMangler::mangleType(const RecordType *T) {
1389 mangleType(static_cast<const TagType*>(T));
1390}
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001391void CXXNameMangler::mangleType(const TagType *T) {
Eli Friedmanecb7e932009-12-11 18:00:57 +00001392 mangleName(T->getDecl());
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001393}
1394
John McCallefe6aee2009-09-05 07:56:18 +00001395// <type> ::= <array-type>
1396// <array-type> ::= A <positive dimension number> _ <element type>
1397// ::= A [<dimension expression>] _ <element type>
1398void CXXNameMangler::mangleType(const ConstantArrayType *T) {
1399 Out << 'A' << T->getSize() << '_';
1400 mangleType(T->getElementType());
1401}
1402void CXXNameMangler::mangleType(const VariableArrayType *T) {
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001403 Out << 'A';
Fariborz Jahanian7281d1f2010-11-02 16:54:00 +00001404 // decayed vla types (size 0) will just be skipped.
1405 if (T->getSizeExpr())
1406 mangleExpression(T->getSizeExpr());
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001407 Out << '_';
1408 mangleType(T->getElementType());
1409}
John McCallefe6aee2009-09-05 07:56:18 +00001410void CXXNameMangler::mangleType(const DependentSizedArrayType *T) {
1411 Out << 'A';
1412 mangleExpression(T->getSizeExpr());
1413 Out << '_';
1414 mangleType(T->getElementType());
1415}
1416void CXXNameMangler::mangleType(const IncompleteArrayType *T) {
Nick Lewycky271b6652010-09-05 03:40:33 +00001417 Out << "A_";
John McCallefe6aee2009-09-05 07:56:18 +00001418 mangleType(T->getElementType());
1419}
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001420
John McCallefe6aee2009-09-05 07:56:18 +00001421// <type> ::= <pointer-to-member-type>
1422// <pointer-to-member-type> ::= M <class type> <member type>
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001423void CXXNameMangler::mangleType(const MemberPointerType *T) {
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001424 Out << 'M';
1425 mangleType(QualType(T->getClass(), 0));
Anders Carlsson0e650012009-05-17 17:41:20 +00001426 QualType PointeeType = T->getPointeeType();
1427 if (const FunctionProtoType *FPT = dyn_cast<FunctionProtoType>(PointeeType)) {
John McCall0953e762009-09-24 19:53:00 +00001428 mangleQualifiers(Qualifiers::fromCVRMask(FPT->getTypeQuals()));
Douglas Gregor0a9a6d62011-01-26 17:36:28 +00001429 mangleRefQualifier(FPT->getRefQualifier());
Anders Carlsson0e650012009-05-17 17:41:20 +00001430 mangleType(FPT);
Anders Carlsson9d85b722010-06-02 04:29:50 +00001431
1432 // Itanium C++ ABI 5.1.8:
1433 //
1434 // The type of a non-static member function is considered to be different,
1435 // for the purposes of substitution, from the type of a namespace-scope or
1436 // static member function whose type appears similar. The types of two
1437 // non-static member functions are considered to be different, for the
1438 // purposes of substitution, if the functions are members of different
1439 // classes. In other words, for the purposes of substitution, the class of
1440 // which the function is a member is considered part of the type of
1441 // function.
1442
1443 // We increment the SeqID here to emulate adding an entry to the
1444 // substitution table. We can't actually add it because we don't want this
1445 // particular function type to be substituted.
1446 ++SeqID;
Mike Stump1eb44332009-09-09 15:08:12 +00001447 } else
Anders Carlsson0e650012009-05-17 17:41:20 +00001448 mangleType(PointeeType);
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001449}
1450
John McCallefe6aee2009-09-05 07:56:18 +00001451// <type> ::= <template-param>
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001452void CXXNameMangler::mangleType(const TemplateTypeParmType *T) {
Anders Carlsson0ccdf8d2009-09-27 00:38:53 +00001453 mangleTemplateParameter(T->getIndex());
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001454}
1455
Douglas Gregorc3069d62011-01-14 02:55:32 +00001456// <type> ::= <template-param>
1457void CXXNameMangler::mangleType(const SubstTemplateTypeParmPackType *T) {
1458 mangleTemplateParameter(T->getReplacedParameter()->getIndex());
1459}
1460
John McCallefe6aee2009-09-05 07:56:18 +00001461// <type> ::= P <type> # pointer-to
1462void CXXNameMangler::mangleType(const PointerType *T) {
1463 Out << 'P';
1464 mangleType(T->getPointeeType());
1465}
1466void CXXNameMangler::mangleType(const ObjCObjectPointerType *T) {
1467 Out << 'P';
1468 mangleType(T->getPointeeType());
1469}
1470
1471// <type> ::= R <type> # reference-to
1472void CXXNameMangler::mangleType(const LValueReferenceType *T) {
1473 Out << 'R';
1474 mangleType(T->getPointeeType());
1475}
1476
1477// <type> ::= O <type> # rvalue reference-to (C++0x)
1478void CXXNameMangler::mangleType(const RValueReferenceType *T) {
1479 Out << 'O';
1480 mangleType(T->getPointeeType());
1481}
1482
1483// <type> ::= C <type> # complex pair (C 2000)
1484void CXXNameMangler::mangleType(const ComplexType *T) {
1485 Out << 'C';
1486 mangleType(T->getElementType());
1487}
1488
Bob Wilsonc7df92d2010-11-12 17:24:43 +00001489// ARM's ABI for Neon vector types specifies that they should be mangled as
Bob Wilson57147a82010-11-16 00:32:18 +00001490// if they are structs (to match ARM's initial implementation). The
1491// vector type must be one of the special types predefined by ARM.
1492void CXXNameMangler::mangleNeonVectorType(const VectorType *T) {
Bob Wilsonc7df92d2010-11-12 17:24:43 +00001493 QualType EltType = T->getElementType();
Bob Wilson57147a82010-11-16 00:32:18 +00001494 assert(EltType->isBuiltinType() && "Neon vector element not a BuiltinType");
Bob Wilsonc7df92d2010-11-12 17:24:43 +00001495 const char *EltName = 0;
Bob Wilson491328c2010-11-12 17:24:46 +00001496 if (T->getVectorKind() == VectorType::NeonPolyVector) {
1497 switch (cast<BuiltinType>(EltType)->getKind()) {
Bob Wilson4cfaa5d2010-11-12 17:24:49 +00001498 case BuiltinType::SChar: EltName = "poly8_t"; break;
1499 case BuiltinType::Short: EltName = "poly16_t"; break;
Bob Wilson57147a82010-11-16 00:32:18 +00001500 default: llvm_unreachable("unexpected Neon polynomial vector element type");
Bob Wilson491328c2010-11-12 17:24:46 +00001501 }
1502 } else {
1503 switch (cast<BuiltinType>(EltType)->getKind()) {
Bob Wilson4cfaa5d2010-11-12 17:24:49 +00001504 case BuiltinType::SChar: EltName = "int8_t"; break;
1505 case BuiltinType::UChar: EltName = "uint8_t"; break;
1506 case BuiltinType::Short: EltName = "int16_t"; break;
1507 case BuiltinType::UShort: EltName = "uint16_t"; break;
1508 case BuiltinType::Int: EltName = "int32_t"; break;
1509 case BuiltinType::UInt: EltName = "uint32_t"; break;
1510 case BuiltinType::LongLong: EltName = "int64_t"; break;
1511 case BuiltinType::ULongLong: EltName = "uint64_t"; break;
1512 case BuiltinType::Float: EltName = "float32_t"; break;
Bob Wilson57147a82010-11-16 00:32:18 +00001513 default: llvm_unreachable("unexpected Neon vector element type");
Bob Wilson491328c2010-11-12 17:24:46 +00001514 }
Bob Wilsonc7df92d2010-11-12 17:24:43 +00001515 }
1516 const char *BaseName = 0;
Bob Wilson4cfaa5d2010-11-12 17:24:49 +00001517 unsigned BitSize = (T->getNumElements() *
Bob Wilson3a723022010-11-16 00:32:12 +00001518 getASTContext().getTypeSize(EltType));
Bob Wilsonc7df92d2010-11-12 17:24:43 +00001519 if (BitSize == 64)
1520 BaseName = "__simd64_";
Bob Wilson57147a82010-11-16 00:32:18 +00001521 else {
1522 assert(BitSize == 128 && "Neon vector type not 64 or 128 bits");
Bob Wilsonc7df92d2010-11-12 17:24:43 +00001523 BaseName = "__simd128_";
Bob Wilson57147a82010-11-16 00:32:18 +00001524 }
Bob Wilsonc7df92d2010-11-12 17:24:43 +00001525 Out << strlen(BaseName) + strlen(EltName);
1526 Out << BaseName << EltName;
Bob Wilsonc7df92d2010-11-12 17:24:43 +00001527}
1528
John McCallefe6aee2009-09-05 07:56:18 +00001529// GNU extension: vector types
Chris Lattner788b0fd2010-06-23 06:00:24 +00001530// <type> ::= <vector-type>
1531// <vector-type> ::= Dv <positive dimension number> _
1532// <extended element type>
1533// ::= Dv [<dimension expression>] _ <element type>
1534// <extended element type> ::= <element type>
1535// ::= p # AltiVec vector pixel
John McCallefe6aee2009-09-05 07:56:18 +00001536void CXXNameMangler::mangleType(const VectorType *T) {
Bob Wilson491328c2010-11-12 17:24:46 +00001537 if ((T->getVectorKind() == VectorType::NeonVector ||
Bob Wilson57147a82010-11-16 00:32:18 +00001538 T->getVectorKind() == VectorType::NeonPolyVector)) {
1539 mangleNeonVectorType(T);
Bob Wilsonc7df92d2010-11-12 17:24:43 +00001540 return;
Bob Wilson57147a82010-11-16 00:32:18 +00001541 }
Nick Lewycky0e5f0672010-03-26 07:18:04 +00001542 Out << "Dv" << T->getNumElements() << '_';
Bob Wilsone86d78c2010-11-10 21:56:12 +00001543 if (T->getVectorKind() == VectorType::AltiVecPixel)
Chris Lattner788b0fd2010-06-23 06:00:24 +00001544 Out << 'p';
Bob Wilsone86d78c2010-11-10 21:56:12 +00001545 else if (T->getVectorKind() == VectorType::AltiVecBool)
Chris Lattner788b0fd2010-06-23 06:00:24 +00001546 Out << 'b';
1547 else
1548 mangleType(T->getElementType());
John McCallefe6aee2009-09-05 07:56:18 +00001549}
1550void CXXNameMangler::mangleType(const ExtVectorType *T) {
1551 mangleType(static_cast<const VectorType*>(T));
1552}
1553void CXXNameMangler::mangleType(const DependentSizedExtVectorType *T) {
Nick Lewycky0e5f0672010-03-26 07:18:04 +00001554 Out << "Dv";
1555 mangleExpression(T->getSizeExpr());
1556 Out << '_';
John McCallefe6aee2009-09-05 07:56:18 +00001557 mangleType(T->getElementType());
1558}
1559
Douglas Gregor7536dd52010-12-20 02:24:11 +00001560void CXXNameMangler::mangleType(const PackExpansionType *T) {
Douglas Gregor4fc48662011-01-13 16:39:34 +00001561 // <type> ::= Dp <type> # pack expansion (C++0x)
Douglas Gregor255c2692011-01-13 17:44:36 +00001562 Out << "Dp";
Douglas Gregor7536dd52010-12-20 02:24:11 +00001563 mangleType(T->getPattern());
1564}
1565
Anders Carlssona40c5e42009-03-07 22:03:21 +00001566void CXXNameMangler::mangleType(const ObjCInterfaceType *T) {
1567 mangleSourceName(T->getDecl()->getIdentifier());
1568}
1569
John McCallc12c5bb2010-05-15 11:32:37 +00001570void CXXNameMangler::mangleType(const ObjCObjectType *T) {
John McCallc00c1f62010-05-15 17:06:29 +00001571 // We don't allow overloading by different protocol qualification,
1572 // so mangling them isn't necessary.
John McCallc12c5bb2010-05-15 11:32:37 +00001573 mangleType(T->getBaseType());
1574}
1575
John McCallefe6aee2009-09-05 07:56:18 +00001576void CXXNameMangler::mangleType(const BlockPointerType *T) {
Anders Carlssonf28c6872009-12-23 22:31:44 +00001577 Out << "U13block_pointer";
1578 mangleType(T->getPointeeType());
John McCallefe6aee2009-09-05 07:56:18 +00001579}
1580
John McCall31f17ec2010-04-27 00:57:59 +00001581void CXXNameMangler::mangleType(const InjectedClassNameType *T) {
1582 // Mangle injected class name types as if the user had written the
1583 // specialization out fully. It may not actually be possible to see
1584 // this mangling, though.
1585 mangleType(T->getInjectedSpecializationType());
1586}
1587
John McCallefe6aee2009-09-05 07:56:18 +00001588void CXXNameMangler::mangleType(const TemplateSpecializationType *T) {
Douglas Gregor1e9268e2010-04-28 05:58:56 +00001589 if (TemplateDecl *TD = T->getTemplateName().getAsTemplateDecl()) {
1590 mangleName(TD, T->getArgs(), T->getNumArgs());
1591 } else {
1592 if (mangleSubstitution(QualType(T, 0)))
1593 return;
Sean Huntc3021132010-05-05 15:23:54 +00001594
Douglas Gregor1e9268e2010-04-28 05:58:56 +00001595 mangleTemplatePrefix(T->getTemplateName());
Sean Huntc3021132010-05-05 15:23:54 +00001596
Douglas Gregor1e9268e2010-04-28 05:58:56 +00001597 // FIXME: GCC does not appear to mangle the template arguments when
1598 // the template in question is a dependent template name. Should we
1599 // emulate that badness?
1600 mangleTemplateArgs(T->getTemplateName(), T->getArgs(), T->getNumArgs());
1601 addSubstitution(QualType(T, 0));
1602 }
John McCallefe6aee2009-09-05 07:56:18 +00001603}
1604
Douglas Gregor4714c122010-03-31 17:34:00 +00001605void CXXNameMangler::mangleType(const DependentNameType *T) {
Anders Carlssonae352482009-09-26 02:26:02 +00001606 // Typename types are always nested
1607 Out << 'N';
John McCall33500952010-06-11 00:33:02 +00001608 mangleUnresolvedScope(T->getQualifier());
1609 mangleSourceName(T->getIdentifier());
1610 Out << 'E';
1611}
John McCall6ab30e02010-06-09 07:26:17 +00001612
John McCall33500952010-06-11 00:33:02 +00001613void CXXNameMangler::mangleType(const DependentTemplateSpecializationType *T) {
Douglas Gregoraa2187d2011-02-28 00:04:36 +00001614 // Dependently-scoped template types are nested if they have a prefix.
John McCall33500952010-06-11 00:33:02 +00001615 Out << 'N';
1616
1617 // TODO: avoid making this TemplateName.
1618 TemplateName Prefix =
1619 getASTContext().getDependentTemplateName(T->getQualifier(),
1620 T->getIdentifier());
1621 mangleTemplatePrefix(Prefix);
1622
1623 // FIXME: GCC does not appear to mangle the template arguments when
1624 // the template in question is a dependent template name. Should we
1625 // emulate that badness?
1626 mangleTemplateArgs(Prefix, T->getArgs(), T->getNumArgs());
Anders Carlssonae352482009-09-26 02:26:02 +00001627 Out << 'E';
John McCallefe6aee2009-09-05 07:56:18 +00001628}
1629
John McCallad5e7382010-03-01 23:49:17 +00001630void CXXNameMangler::mangleType(const TypeOfType *T) {
1631 // FIXME: this is pretty unsatisfactory, but there isn't an obvious
1632 // "extension with parameters" mangling.
1633 Out << "u6typeof";
1634}
1635
1636void CXXNameMangler::mangleType(const TypeOfExprType *T) {
1637 // FIXME: this is pretty unsatisfactory, but there isn't an obvious
1638 // "extension with parameters" mangling.
1639 Out << "u6typeof";
1640}
1641
1642void CXXNameMangler::mangleType(const DecltypeType *T) {
1643 Expr *E = T->getUnderlyingExpr();
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001644
John McCallad5e7382010-03-01 23:49:17 +00001645 // type ::= Dt <expression> E # decltype of an id-expression
1646 // # or class member access
1647 // ::= DT <expression> E # decltype of an expression
1648
1649 // This purports to be an exhaustive list of id-expressions and
1650 // class member accesses. Note that we do not ignore parentheses;
1651 // parentheses change the semantics of decltype for these
1652 // expressions (and cause the mangler to use the other form).
1653 if (isa<DeclRefExpr>(E) ||
1654 isa<MemberExpr>(E) ||
1655 isa<UnresolvedLookupExpr>(E) ||
1656 isa<DependentScopeDeclRefExpr>(E) ||
1657 isa<CXXDependentScopeMemberExpr>(E) ||
1658 isa<UnresolvedMemberExpr>(E))
1659 Out << "Dt";
1660 else
1661 Out << "DT";
1662 mangleExpression(E);
1663 Out << 'E';
1664}
1665
Richard Smith34b41d92011-02-20 03:19:35 +00001666void CXXNameMangler::mangleType(const AutoType *T) {
1667 QualType D = T->getDeducedType();
Richard Smith967ecd32011-02-21 20:10:02 +00001668 // <builtin-type> ::= Da # dependent auto
1669 if (D.isNull())
1670 Out << "Da";
1671 else
1672 mangleType(D);
Richard Smith34b41d92011-02-20 03:19:35 +00001673}
1674
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001675void CXXNameMangler::mangleIntegerLiteral(QualType T,
Anders Carlssone170ba72009-12-14 01:45:37 +00001676 const llvm::APSInt &Value) {
1677 // <expr-primary> ::= L <type> <value number> E # integer literal
1678 Out << 'L';
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001679
Anders Carlssone170ba72009-12-14 01:45:37 +00001680 mangleType(T);
1681 if (T->isBooleanType()) {
1682 // Boolean values are encoded as 0/1.
1683 Out << (Value.getBoolValue() ? '1' : '0');
1684 } else {
John McCall0512e482010-07-14 04:20:34 +00001685 mangleNumber(Value);
Anders Carlssone170ba72009-12-14 01:45:37 +00001686 }
1687 Out << 'E';
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001688
Anders Carlssone170ba72009-12-14 01:45:37 +00001689}
1690
John McCall2f27bf82010-02-04 02:56:29 +00001691/// Mangles a member expression. Implicit accesses are not handled,
1692/// but that should be okay, because you shouldn't be able to
1693/// make an implicit access in a function template declaration.
John McCall2f27bf82010-02-04 02:56:29 +00001694void CXXNameMangler::mangleMemberExpr(const Expr *Base,
1695 bool IsArrow,
1696 NestedNameSpecifier *Qualifier,
1697 DeclarationName Member,
1698 unsigned Arity) {
John McCalle1e342f2010-03-01 19:12:25 +00001699 // gcc-4.4 uses 'dt' for dot expressions, which is reasonable.
1700 // OTOH, gcc also mangles the name as an expression.
1701 Out << (IsArrow ? "pt" : "dt");
John McCall2f27bf82010-02-04 02:56:29 +00001702 mangleExpression(Base);
1703 mangleUnresolvedName(Qualifier, Member, Arity);
1704}
1705
John McCall5e1e89b2010-08-18 19:18:59 +00001706void CXXNameMangler::mangleExpression(const Expr *E, unsigned Arity) {
Anders Carlssond553f8c2009-09-21 01:21:10 +00001707 // <expression> ::= <unary operator-name> <expression>
John McCall09cc1412010-02-03 00:55:45 +00001708 // ::= <binary operator-name> <expression> <expression>
1709 // ::= <trinary operator-name> <expression> <expression> <expression>
Eli Friedmana7e68452010-08-22 01:00:03 +00001710 // ::= cl <expression>* E # call
Anders Carlssond553f8c2009-09-21 01:21:10 +00001711 // ::= cv <type> expression # conversion with one argument
1712 // ::= cv <type> _ <expression>* E # conversion with a different number of arguments
Eli Friedmana7e68452010-08-22 01:00:03 +00001713 // ::= st <type> # sizeof (a type)
Anders Carlssond553f8c2009-09-21 01:21:10 +00001714 // ::= at <type> # alignof (a type)
1715 // ::= <template-param>
1716 // ::= <function-param>
1717 // ::= sr <type> <unqualified-name> # dependent name
1718 // ::= sr <type> <unqualified-name> <template-args> # dependent template-id
1719 // ::= sZ <template-param> # size of a parameter pack
Douglas Gregor4fc48662011-01-13 16:39:34 +00001720 // ::= sZ <function-param> # size of a function parameter pack
John McCall09cc1412010-02-03 00:55:45 +00001721 // ::= <expr-primary>
John McCall1dd73832010-02-04 01:42:13 +00001722 // <expr-primary> ::= L <type> <value number> E # integer literal
1723 // ::= L <type <value float> E # floating literal
1724 // ::= L <mangled-name> E # external name
Anders Carlssond553f8c2009-09-21 01:21:10 +00001725 switch (E->getStmtClass()) {
John McCall6ae1f352010-04-09 22:26:14 +00001726 case Expr::NoStmtClass:
John McCall63c00d72011-02-09 08:16:59 +00001727#define ABSTRACT_STMT(Type)
John McCall6ae1f352010-04-09 22:26:14 +00001728#define EXPR(Type, Base)
1729#define STMT(Type, Base) \
1730 case Expr::Type##Class:
Sean Hunt4bfe1962010-05-05 15:24:00 +00001731#include "clang/AST/StmtNodes.inc"
John McCall0512e482010-07-14 04:20:34 +00001732 // fallthrough
1733
1734 // These all can only appear in local or variable-initialization
1735 // contexts and so should never appear in a mangling.
1736 case Expr::AddrLabelExprClass:
1737 case Expr::BlockDeclRefExprClass:
1738 case Expr::CXXThisExprClass:
1739 case Expr::DesignatedInitExprClass:
1740 case Expr::ImplicitValueInitExprClass:
1741 case Expr::InitListExprClass:
1742 case Expr::ParenListExprClass:
1743 case Expr::CXXScalarValueInitExprClass:
John McCall09cc1412010-02-03 00:55:45 +00001744 llvm_unreachable("unexpected statement kind");
1745 break;
1746
John McCall0512e482010-07-14 04:20:34 +00001747 // FIXME: invent manglings for all these.
1748 case Expr::BlockExprClass:
1749 case Expr::CXXPseudoDestructorExprClass:
1750 case Expr::ChooseExprClass:
1751 case Expr::CompoundLiteralExprClass:
1752 case Expr::ExtVectorElementExprClass:
1753 case Expr::ObjCEncodeExprClass:
John McCall0512e482010-07-14 04:20:34 +00001754 case Expr::ObjCIsaExprClass:
1755 case Expr::ObjCIvarRefExprClass:
1756 case Expr::ObjCMessageExprClass:
1757 case Expr::ObjCPropertyRefExprClass:
1758 case Expr::ObjCProtocolExprClass:
1759 case Expr::ObjCSelectorExprClass:
1760 case Expr::ObjCStringLiteralClass:
John McCall0512e482010-07-14 04:20:34 +00001761 case Expr::OffsetOfExprClass:
1762 case Expr::PredefinedExprClass:
1763 case Expr::ShuffleVectorExprClass:
1764 case Expr::StmtExprClass:
John McCall0512e482010-07-14 04:20:34 +00001765 case Expr::UnaryTypeTraitExprClass:
Francois Pichet6ad6f282010-12-07 00:08:36 +00001766 case Expr::BinaryTypeTraitExprClass:
Francois Pichet9be88402010-09-08 23:47:05 +00001767 case Expr::VAArgExprClass:
Sebastian Redl2e156222010-09-10 20:55:43 +00001768 case Expr::CXXUuidofExprClass:
Peter Collingbournee08ce652011-02-09 21:07:24 +00001769 case Expr::CXXNoexceptExprClass:
1770 case Expr::CUDAKernelCallExprClass: {
John McCall6ae1f352010-04-09 22:26:14 +00001771 // As bad as this diagnostic is, it's better than crashing.
1772 Diagnostic &Diags = Context.getDiags();
1773 unsigned DiagID = Diags.getCustomDiagID(Diagnostic::Error,
1774 "cannot yet mangle expression type %0");
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +00001775 Diags.Report(E->getExprLoc(), DiagID)
John McCall739bf092010-04-10 09:39:25 +00001776 << E->getStmtClassName() << E->getSourceRange();
John McCall6ae1f352010-04-09 22:26:14 +00001777 break;
1778 }
1779
John McCall56ca35d2011-02-17 10:25:35 +00001780 // Even gcc-4.5 doesn't mangle this.
1781 case Expr::BinaryConditionalOperatorClass: {
1782 Diagnostic &Diags = Context.getDiags();
1783 unsigned DiagID =
1784 Diags.getCustomDiagID(Diagnostic::Error,
1785 "?: operator with omitted middle operand cannot be mangled");
1786 Diags.Report(E->getExprLoc(), DiagID)
1787 << E->getStmtClassName() << E->getSourceRange();
1788 break;
1789 }
1790
1791 // These are used for internal purposes and cannot be meaningfully mangled.
John McCall7cd7d1a2010-11-15 23:31:06 +00001792 case Expr::OpaqueValueExprClass:
1793 llvm_unreachable("cannot mangle opaque value; mangling wrong thing?");
1794
John McCall0512e482010-07-14 04:20:34 +00001795 case Expr::CXXDefaultArgExprClass:
John McCall5e1e89b2010-08-18 19:18:59 +00001796 mangleExpression(cast<CXXDefaultArgExpr>(E)->getExpr(), Arity);
John McCall0512e482010-07-14 04:20:34 +00001797 break;
1798
1799 case Expr::CXXMemberCallExprClass: // fallthrough
John McCall1dd73832010-02-04 01:42:13 +00001800 case Expr::CallExprClass: {
1801 const CallExpr *CE = cast<CallExpr>(E);
1802 Out << "cl";
John McCall5e1e89b2010-08-18 19:18:59 +00001803 mangleExpression(CE->getCallee(), CE->getNumArgs());
John McCall1dd73832010-02-04 01:42:13 +00001804 for (unsigned I = 0, N = CE->getNumArgs(); I != N; ++I)
1805 mangleExpression(CE->getArg(I));
Benjamin Kramer35f59b62010-04-10 16:03:31 +00001806 Out << 'E';
John McCall09cc1412010-02-03 00:55:45 +00001807 break;
John McCall1dd73832010-02-04 01:42:13 +00001808 }
John McCall09cc1412010-02-03 00:55:45 +00001809
John McCall0512e482010-07-14 04:20:34 +00001810 case Expr::CXXNewExprClass: {
1811 // Proposal from David Vandervoorde, 2010.06.30
1812 const CXXNewExpr *New = cast<CXXNewExpr>(E);
1813 if (New->isGlobalNew()) Out << "gs";
1814 Out << (New->isArray() ? "na" : "nw");
1815 for (CXXNewExpr::const_arg_iterator I = New->placement_arg_begin(),
1816 E = New->placement_arg_end(); I != E; ++I)
1817 mangleExpression(*I);
1818 Out << '_';
1819 mangleType(New->getAllocatedType());
1820 if (New->hasInitializer()) {
1821 Out << "pi";
1822 for (CXXNewExpr::const_arg_iterator I = New->constructor_arg_begin(),
1823 E = New->constructor_arg_end(); I != E; ++I)
1824 mangleExpression(*I);
1825 }
1826 Out << 'E';
1827 break;
1828 }
1829
John McCall2f27bf82010-02-04 02:56:29 +00001830 case Expr::MemberExprClass: {
1831 const MemberExpr *ME = cast<MemberExpr>(E);
1832 mangleMemberExpr(ME->getBase(), ME->isArrow(),
1833 ME->getQualifier(), ME->getMemberDecl()->getDeclName(),
John McCall5e1e89b2010-08-18 19:18:59 +00001834 Arity);
John McCall2f27bf82010-02-04 02:56:29 +00001835 break;
1836 }
1837
1838 case Expr::UnresolvedMemberExprClass: {
1839 const UnresolvedMemberExpr *ME = cast<UnresolvedMemberExpr>(E);
1840 mangleMemberExpr(ME->getBase(), ME->isArrow(),
1841 ME->getQualifier(), ME->getMemberName(),
John McCall5e1e89b2010-08-18 19:18:59 +00001842 Arity);
John McCall6dbce192010-08-20 00:17:19 +00001843 if (ME->hasExplicitTemplateArgs())
1844 mangleTemplateArgs(ME->getExplicitTemplateArgs());
John McCall2f27bf82010-02-04 02:56:29 +00001845 break;
1846 }
1847
1848 case Expr::CXXDependentScopeMemberExprClass: {
1849 const CXXDependentScopeMemberExpr *ME
1850 = cast<CXXDependentScopeMemberExpr>(E);
1851 mangleMemberExpr(ME->getBase(), ME->isArrow(),
1852 ME->getQualifier(), ME->getMember(),
John McCall5e1e89b2010-08-18 19:18:59 +00001853 Arity);
John McCall6dbce192010-08-20 00:17:19 +00001854 if (ME->hasExplicitTemplateArgs())
1855 mangleTemplateArgs(ME->getExplicitTemplateArgs());
John McCall2f27bf82010-02-04 02:56:29 +00001856 break;
1857 }
1858
John McCall1dd73832010-02-04 01:42:13 +00001859 case Expr::UnresolvedLookupExprClass: {
John McCalla3218e72010-02-04 01:48:38 +00001860 // The ABI doesn't cover how to mangle overload sets, so we mangle
1861 // using something as close as possible to the original lookup
1862 // expression.
John McCall1dd73832010-02-04 01:42:13 +00001863 const UnresolvedLookupExpr *ULE = cast<UnresolvedLookupExpr>(E);
John McCall5e1e89b2010-08-18 19:18:59 +00001864 mangleUnresolvedName(ULE->getQualifier(), ULE->getName(), Arity);
John McCall6dbce192010-08-20 00:17:19 +00001865 if (ULE->hasExplicitTemplateArgs())
1866 mangleTemplateArgs(ULE->getExplicitTemplateArgs());
John McCall1dd73832010-02-04 01:42:13 +00001867 break;
1868 }
1869
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001870 case Expr::CXXUnresolvedConstructExprClass: {
John McCall1dd73832010-02-04 01:42:13 +00001871 const CXXUnresolvedConstructExpr *CE = cast<CXXUnresolvedConstructExpr>(E);
1872 unsigned N = CE->arg_size();
1873
1874 Out << "cv";
1875 mangleType(CE->getType());
Benjamin Kramer35f59b62010-04-10 16:03:31 +00001876 if (N != 1) Out << '_';
John McCall1dd73832010-02-04 01:42:13 +00001877 for (unsigned I = 0; I != N; ++I) mangleExpression(CE->getArg(I));
Benjamin Kramer35f59b62010-04-10 16:03:31 +00001878 if (N != 1) Out << 'E';
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001879 break;
John McCall1dd73832010-02-04 01:42:13 +00001880 }
John McCall09cc1412010-02-03 00:55:45 +00001881
John McCall1dd73832010-02-04 01:42:13 +00001882 case Expr::CXXTemporaryObjectExprClass:
1883 case Expr::CXXConstructExprClass: {
1884 const CXXConstructExpr *CE = cast<CXXConstructExpr>(E);
1885 unsigned N = CE->getNumArgs();
1886
1887 Out << "cv";
1888 mangleType(CE->getType());
Benjamin Kramer35f59b62010-04-10 16:03:31 +00001889 if (N != 1) Out << '_';
John McCall1dd73832010-02-04 01:42:13 +00001890 for (unsigned I = 0; I != N; ++I) mangleExpression(CE->getArg(I));
Benjamin Kramer35f59b62010-04-10 16:03:31 +00001891 if (N != 1) Out << 'E';
John McCall09cc1412010-02-03 00:55:45 +00001892 break;
John McCall1dd73832010-02-04 01:42:13 +00001893 }
1894
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +00001895 case Expr::UnaryExprOrTypeTraitExprClass: {
1896 const UnaryExprOrTypeTraitExpr *SAE = cast<UnaryExprOrTypeTraitExpr>(E);
1897 switch(SAE->getKind()) {
1898 case UETT_SizeOf:
1899 Out << 's';
1900 break;
1901 case UETT_AlignOf:
1902 Out << 'a';
1903 break;
1904 case UETT_VecStep:
1905 Diagnostic &Diags = Context.getDiags();
1906 unsigned DiagID = Diags.getCustomDiagID(Diagnostic::Error,
1907 "cannot yet mangle vec_step expression");
1908 Diags.Report(DiagID);
1909 return;
1910 }
John McCall1dd73832010-02-04 01:42:13 +00001911 if (SAE->isArgumentType()) {
Benjamin Kramer35f59b62010-04-10 16:03:31 +00001912 Out << 't';
John McCall1dd73832010-02-04 01:42:13 +00001913 mangleType(SAE->getArgumentType());
1914 } else {
Benjamin Kramer35f59b62010-04-10 16:03:31 +00001915 Out << 'z';
John McCall1dd73832010-02-04 01:42:13 +00001916 mangleExpression(SAE->getArgumentExpr());
1917 }
1918 break;
1919 }
Anders Carlssona7694082009-11-06 02:50:19 +00001920
John McCall0512e482010-07-14 04:20:34 +00001921 case Expr::CXXThrowExprClass: {
1922 const CXXThrowExpr *TE = cast<CXXThrowExpr>(E);
1923
1924 // Proposal from David Vandervoorde, 2010.06.30
1925 if (TE->getSubExpr()) {
1926 Out << "tw";
1927 mangleExpression(TE->getSubExpr());
1928 } else {
1929 Out << "tr";
1930 }
1931 break;
1932 }
1933
1934 case Expr::CXXTypeidExprClass: {
1935 const CXXTypeidExpr *TIE = cast<CXXTypeidExpr>(E);
1936
1937 // Proposal from David Vandervoorde, 2010.06.30
1938 if (TIE->isTypeOperand()) {
1939 Out << "ti";
1940 mangleType(TIE->getTypeOperand());
1941 } else {
1942 Out << "te";
1943 mangleExpression(TIE->getExprOperand());
1944 }
1945 break;
1946 }
1947
1948 case Expr::CXXDeleteExprClass: {
1949 const CXXDeleteExpr *DE = cast<CXXDeleteExpr>(E);
1950
1951 // Proposal from David Vandervoorde, 2010.06.30
1952 if (DE->isGlobalDelete()) Out << "gs";
1953 Out << (DE->isArrayForm() ? "da" : "dl");
1954 mangleExpression(DE->getArgument());
1955 break;
1956 }
1957
Anders Carlssone170ba72009-12-14 01:45:37 +00001958 case Expr::UnaryOperatorClass: {
1959 const UnaryOperator *UO = cast<UnaryOperator>(E);
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001960 mangleOperatorName(UnaryOperator::getOverloadedOperator(UO->getOpcode()),
Anders Carlssone170ba72009-12-14 01:45:37 +00001961 /*Arity=*/1);
1962 mangleExpression(UO->getSubExpr());
1963 break;
1964 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001965
John McCall0512e482010-07-14 04:20:34 +00001966 case Expr::ArraySubscriptExprClass: {
1967 const ArraySubscriptExpr *AE = cast<ArraySubscriptExpr>(E);
1968
1969 // Array subscript is treated as a syntactically wierd form of
1970 // binary operator.
1971 Out << "ix";
1972 mangleExpression(AE->getLHS());
1973 mangleExpression(AE->getRHS());
1974 break;
1975 }
1976
1977 case Expr::CompoundAssignOperatorClass: // fallthrough
Anders Carlssone170ba72009-12-14 01:45:37 +00001978 case Expr::BinaryOperatorClass: {
1979 const BinaryOperator *BO = cast<BinaryOperator>(E);
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001980 mangleOperatorName(BinaryOperator::getOverloadedOperator(BO->getOpcode()),
Anders Carlssone170ba72009-12-14 01:45:37 +00001981 /*Arity=*/2);
1982 mangleExpression(BO->getLHS());
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001983 mangleExpression(BO->getRHS());
Anders Carlssone170ba72009-12-14 01:45:37 +00001984 break;
John McCall2f27bf82010-02-04 02:56:29 +00001985 }
Anders Carlssone170ba72009-12-14 01:45:37 +00001986
1987 case Expr::ConditionalOperatorClass: {
1988 const ConditionalOperator *CO = cast<ConditionalOperator>(E);
1989 mangleOperatorName(OO_Conditional, /*Arity=*/3);
1990 mangleExpression(CO->getCond());
John McCall5e1e89b2010-08-18 19:18:59 +00001991 mangleExpression(CO->getLHS(), Arity);
1992 mangleExpression(CO->getRHS(), Arity);
Anders Carlssone170ba72009-12-14 01:45:37 +00001993 break;
1994 }
1995
Douglas Gregor46287c72010-01-29 16:37:09 +00001996 case Expr::ImplicitCastExprClass: {
John McCall5e1e89b2010-08-18 19:18:59 +00001997 mangleExpression(cast<ImplicitCastExpr>(E)->getSubExpr(), Arity);
Douglas Gregor46287c72010-01-29 16:37:09 +00001998 break;
1999 }
2000
2001 case Expr::CStyleCastExprClass:
2002 case Expr::CXXStaticCastExprClass:
2003 case Expr::CXXDynamicCastExprClass:
2004 case Expr::CXXReinterpretCastExprClass:
2005 case Expr::CXXConstCastExprClass:
2006 case Expr::CXXFunctionalCastExprClass: {
2007 const ExplicitCastExpr *ECE = cast<ExplicitCastExpr>(E);
2008 Out << "cv";
2009 mangleType(ECE->getType());
2010 mangleExpression(ECE->getSubExpr());
2011 break;
2012 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002013
Anders Carlsson58040a52009-12-16 05:48:46 +00002014 case Expr::CXXOperatorCallExprClass: {
2015 const CXXOperatorCallExpr *CE = cast<CXXOperatorCallExpr>(E);
2016 unsigned NumArgs = CE->getNumArgs();
2017 mangleOperatorName(CE->getOperator(), /*Arity=*/NumArgs);
2018 // Mangle the arguments.
2019 for (unsigned i = 0; i != NumArgs; ++i)
2020 mangleExpression(CE->getArg(i));
2021 break;
2022 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002023
Anders Carlssona7694082009-11-06 02:50:19 +00002024 case Expr::ParenExprClass:
John McCall5e1e89b2010-08-18 19:18:59 +00002025 mangleExpression(cast<ParenExpr>(E)->getSubExpr(), Arity);
Anders Carlssona7694082009-11-06 02:50:19 +00002026 break;
2027
Anders Carlssond553f8c2009-09-21 01:21:10 +00002028 case Expr::DeclRefExprClass: {
Douglas Gregor5ed1bc32010-02-28 21:40:32 +00002029 const NamedDecl *D = cast<DeclRefExpr>(E)->getDecl();
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002030
Anders Carlssond553f8c2009-09-21 01:21:10 +00002031 switch (D->getKind()) {
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002032 default:
Douglas Gregor5ed1bc32010-02-28 21:40:32 +00002033 // <expr-primary> ::= L <mangled-name> E # external name
2034 Out << 'L';
2035 mangle(D, "_Z");
2036 Out << 'E';
2037 break;
2038
John McCall3dc7e7b2010-07-24 01:17:35 +00002039 case Decl::EnumConstant: {
2040 const EnumConstantDecl *ED = cast<EnumConstantDecl>(D);
2041 mangleIntegerLiteral(ED->getType(), ED->getInitVal());
2042 break;
2043 }
2044
Anders Carlssond553f8c2009-09-21 01:21:10 +00002045 case Decl::NonTypeTemplateParm: {
2046 const NonTypeTemplateParmDecl *PD = cast<NonTypeTemplateParmDecl>(D);
Anders Carlsson0ccdf8d2009-09-27 00:38:53 +00002047 mangleTemplateParameter(PD->getIndex());
Anders Carlssond553f8c2009-09-21 01:21:10 +00002048 break;
2049 }
2050
2051 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002052
Anders Carlsson50755b02009-09-27 20:11:34 +00002053 break;
Anders Carlssond553f8c2009-09-21 01:21:10 +00002054 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002055
Douglas Gregorc7793c72011-01-15 01:15:58 +00002056 case Expr::SubstNonTypeTemplateParmPackExprClass:
2057 mangleTemplateParameter(
2058 cast<SubstNonTypeTemplateParmPackExpr>(E)->getParameterPack()->getIndex());
2059 break;
2060
John McCall865d4472009-11-19 22:55:06 +00002061 case Expr::DependentScopeDeclRefExprClass: {
2062 const DependentScopeDeclRefExpr *DRE = cast<DependentScopeDeclRefExpr>(E);
Douglas Gregor4b2ccfc2010-02-28 22:05:49 +00002063 NestedNameSpecifier *NNS = DRE->getQualifier();
2064 const Type *QTy = NNS->getAsType();
2065
2066 // When we're dealing with a nested-name-specifier that has just a
2067 // dependent identifier in it, mangle that as a typename. FIXME:
2068 // It isn't clear that we ever actually want to have such a
2069 // nested-name-specifier; why not just represent it as a typename type?
2070 if (!QTy && NNS->getAsIdentifier() && NNS->getPrefix()) {
Douglas Gregor4a2023f2010-03-31 20:19:30 +00002071 QTy = getASTContext().getDependentNameType(ETK_Typename,
2072 NNS->getPrefix(),
2073 NNS->getAsIdentifier())
Douglas Gregor4b2ccfc2010-02-28 22:05:49 +00002074 .getTypePtr();
2075 }
Anders Carlsson50755b02009-09-27 20:11:34 +00002076 assert(QTy && "Qualifier was not type!");
2077
John McCall6dbce192010-08-20 00:17:19 +00002078 // ::= sr <type> <unqualified-name> # dependent name
2079 // ::= sr <type> <unqualified-name> <template-args> # dependent template-id
Anders Carlsson50755b02009-09-27 20:11:34 +00002080 Out << "sr";
2081 mangleType(QualType(QTy, 0));
John McCall5e1e89b2010-08-18 19:18:59 +00002082 mangleUnqualifiedName(0, DRE->getDeclName(), Arity);
John McCall6dbce192010-08-20 00:17:19 +00002083 if (DRE->hasExplicitTemplateArgs())
2084 mangleTemplateArgs(DRE->getExplicitTemplateArgs());
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002085
Anders Carlsson50755b02009-09-27 20:11:34 +00002086 break;
2087 }
2088
John McCalld9307602010-04-09 22:54:09 +00002089 case Expr::CXXBindTemporaryExprClass:
2090 mangleExpression(cast<CXXBindTemporaryExpr>(E)->getSubExpr());
2091 break;
2092
John McCall4765fa02010-12-06 08:20:24 +00002093 case Expr::ExprWithCleanupsClass:
2094 mangleExpression(cast<ExprWithCleanups>(E)->getSubExpr(), Arity);
John McCalld9307602010-04-09 22:54:09 +00002095 break;
2096
John McCall1dd73832010-02-04 01:42:13 +00002097 case Expr::FloatingLiteralClass: {
2098 const FloatingLiteral *FL = cast<FloatingLiteral>(E);
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002099 Out << 'L';
John McCall1dd73832010-02-04 01:42:13 +00002100 mangleType(FL->getType());
John McCall0512e482010-07-14 04:20:34 +00002101 mangleFloat(FL->getValue());
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002102 Out << 'E';
John McCall1dd73832010-02-04 01:42:13 +00002103 break;
2104 }
2105
John McCallde810632010-04-09 21:48:08 +00002106 case Expr::CharacterLiteralClass:
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002107 Out << 'L';
John McCallde810632010-04-09 21:48:08 +00002108 mangleType(E->getType());
2109 Out << cast<CharacterLiteral>(E)->getValue();
2110 Out << 'E';
2111 break;
2112
2113 case Expr::CXXBoolLiteralExprClass:
2114 Out << "Lb";
2115 Out << (cast<CXXBoolLiteralExpr>(E)->getValue() ? '1' : '0');
2116 Out << 'E';
2117 break;
2118
John McCall0512e482010-07-14 04:20:34 +00002119 case Expr::IntegerLiteralClass: {
2120 llvm::APSInt Value(cast<IntegerLiteral>(E)->getValue());
2121 if (E->getType()->isSignedIntegerType())
2122 Value.setIsSigned(true);
2123 mangleIntegerLiteral(E->getType(), Value);
Anders Carlssone170ba72009-12-14 01:45:37 +00002124 break;
John McCall0512e482010-07-14 04:20:34 +00002125 }
2126
2127 case Expr::ImaginaryLiteralClass: {
2128 const ImaginaryLiteral *IE = cast<ImaginaryLiteral>(E);
2129 // Mangle as if a complex literal.
Nick Lewycky271b6652010-09-05 03:40:33 +00002130 // Proposal from David Vandevoorde, 2010.06.30.
John McCall0512e482010-07-14 04:20:34 +00002131 Out << 'L';
2132 mangleType(E->getType());
2133 if (const FloatingLiteral *Imag =
2134 dyn_cast<FloatingLiteral>(IE->getSubExpr())) {
2135 // Mangle a floating-point zero of the appropriate type.
2136 mangleFloat(llvm::APFloat(Imag->getValue().getSemantics()));
2137 Out << '_';
2138 mangleFloat(Imag->getValue());
2139 } else {
Nick Lewycky271b6652010-09-05 03:40:33 +00002140 Out << "0_";
John McCall0512e482010-07-14 04:20:34 +00002141 llvm::APSInt Value(cast<IntegerLiteral>(IE->getSubExpr())->getValue());
2142 if (IE->getSubExpr()->getType()->isSignedIntegerType())
2143 Value.setIsSigned(true);
2144 mangleNumber(Value);
2145 }
2146 Out << 'E';
2147 break;
2148 }
2149
2150 case Expr::StringLiteralClass: {
John McCall1658c392010-07-15 21:53:03 +00002151 // Revised proposal from David Vandervoorde, 2010.07.15.
John McCall0512e482010-07-14 04:20:34 +00002152 Out << 'L';
John McCall1658c392010-07-15 21:53:03 +00002153 assert(isa<ConstantArrayType>(E->getType()));
2154 mangleType(E->getType());
John McCall0512e482010-07-14 04:20:34 +00002155 Out << 'E';
2156 break;
2157 }
2158
2159 case Expr::GNUNullExprClass:
2160 // FIXME: should this really be mangled the same as nullptr?
2161 // fallthrough
2162
2163 case Expr::CXXNullPtrLiteralExprClass: {
2164 // Proposal from David Vandervoorde, 2010.06.30, as
2165 // modified by ABI list discussion.
2166 Out << "LDnE";
2167 break;
2168 }
Douglas Gregorbe230c32011-01-03 17:17:50 +00002169
2170 case Expr::PackExpansionExprClass:
2171 Out << "sp";
2172 mangleExpression(cast<PackExpansionExpr>(E)->getPattern());
2173 break;
Douglas Gregor2e774c42011-01-04 18:56:13 +00002174
2175 case Expr::SizeOfPackExprClass: {
Douglas Gregor2e774c42011-01-04 18:56:13 +00002176 Out << "sZ";
2177 const NamedDecl *Pack = cast<SizeOfPackExpr>(E)->getPack();
2178 if (const TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(Pack))
2179 mangleTemplateParameter(TTP->getIndex());
2180 else if (const NonTypeTemplateParmDecl *NTTP
2181 = dyn_cast<NonTypeTemplateParmDecl>(Pack))
2182 mangleTemplateParameter(NTTP->getIndex());
2183 else if (const TemplateTemplateParmDecl *TempTP
2184 = dyn_cast<TemplateTemplateParmDecl>(Pack))
2185 mangleTemplateParameter(TempTP->getIndex());
2186 else {
Douglas Gregor4fc48662011-01-13 16:39:34 +00002187 // Note: proposed by Mike Herrick on 11/30/10
2188 // <expression> ::= sZ <function-param> # size of function parameter pack
Douglas Gregor2e774c42011-01-04 18:56:13 +00002189 Diagnostic &Diags = Context.getDiags();
2190 unsigned DiagID = Diags.getCustomDiagID(Diagnostic::Error,
2191 "cannot mangle sizeof...(function parameter pack)");
2192 Diags.Report(DiagID);
2193 return;
2194 }
Douglas Gregordfbbcf92011-03-03 02:20:19 +00002195 break;
Douglas Gregor2e774c42011-01-04 18:56:13 +00002196 }
Anders Carlssond553f8c2009-09-21 01:21:10 +00002197 }
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00002198}
2199
Anders Carlsson3ac86b52009-04-15 05:36:58 +00002200void CXXNameMangler::mangleCXXCtorType(CXXCtorType T) {
2201 // <ctor-dtor-name> ::= C1 # complete object constructor
2202 // ::= C2 # base object constructor
2203 // ::= C3 # complete object allocating constructor
2204 //
2205 switch (T) {
2206 case Ctor_Complete:
2207 Out << "C1";
2208 break;
2209 case Ctor_Base:
2210 Out << "C2";
2211 break;
2212 case Ctor_CompleteAllocating:
2213 Out << "C3";
2214 break;
2215 }
2216}
2217
Anders Carlsson27ae5362009-04-17 01:58:57 +00002218void CXXNameMangler::mangleCXXDtorType(CXXDtorType T) {
2219 // <ctor-dtor-name> ::= D0 # deleting destructor
2220 // ::= D1 # complete object destructor
2221 // ::= D2 # base object destructor
2222 //
2223 switch (T) {
2224 case Dtor_Deleting:
2225 Out << "D0";
2226 break;
2227 case Dtor_Complete:
2228 Out << "D1";
2229 break;
2230 case Dtor_Base:
2231 Out << "D2";
2232 break;
2233 }
2234}
2235
John McCall6dbce192010-08-20 00:17:19 +00002236void CXXNameMangler::mangleTemplateArgs(
2237 const ExplicitTemplateArgumentList &TemplateArgs) {
2238 // <template-args> ::= I <template-arg>+ E
2239 Out << 'I';
2240 for (unsigned I = 0, E = TemplateArgs.NumTemplateArgs; I != E; ++I)
2241 mangleTemplateArg(0, TemplateArgs.getTemplateArgs()[I].getArgument());
2242 Out << 'E';
2243}
2244
Douglas Gregor20f0cc72010-04-23 03:10:43 +00002245void CXXNameMangler::mangleTemplateArgs(TemplateName Template,
2246 const TemplateArgument *TemplateArgs,
2247 unsigned NumTemplateArgs) {
2248 if (TemplateDecl *TD = Template.getAsTemplateDecl())
2249 return mangleTemplateArgs(*TD->getTemplateParameters(), TemplateArgs,
2250 NumTemplateArgs);
Sean Huntc3021132010-05-05 15:23:54 +00002251
Douglas Gregor20f0cc72010-04-23 03:10:43 +00002252 // <template-args> ::= I <template-arg>+ E
2253 Out << 'I';
2254 for (unsigned i = 0; i != NumTemplateArgs; ++i)
2255 mangleTemplateArg(0, TemplateArgs[i]);
2256 Out << 'E';
2257}
2258
Rafael Espindolad9800722010-03-11 14:07:00 +00002259void CXXNameMangler::mangleTemplateArgs(const TemplateParameterList &PL,
2260 const TemplateArgumentList &AL) {
Anders Carlsson7a0ba872009-05-15 16:09:15 +00002261 // <template-args> ::= I <template-arg>+ E
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002262 Out << 'I';
Rafael Espindolad9800722010-03-11 14:07:00 +00002263 for (unsigned i = 0, e = AL.size(); i != e; ++i)
2264 mangleTemplateArg(PL.getParam(i), AL[i]);
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002265 Out << 'E';
Anders Carlsson7a0ba872009-05-15 16:09:15 +00002266}
2267
Rafael Espindolad9800722010-03-11 14:07:00 +00002268void CXXNameMangler::mangleTemplateArgs(const TemplateParameterList &PL,
2269 const TemplateArgument *TemplateArgs,
Anders Carlsson7624f212009-09-18 02:42:01 +00002270 unsigned NumTemplateArgs) {
2271 // <template-args> ::= I <template-arg>+ E
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002272 Out << 'I';
Daniel Dunbar7e0c1952009-11-21 09:17:15 +00002273 for (unsigned i = 0; i != NumTemplateArgs; ++i)
Rafael Espindolad9800722010-03-11 14:07:00 +00002274 mangleTemplateArg(PL.getParam(i), TemplateArgs[i]);
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002275 Out << 'E';
Anders Carlsson7624f212009-09-18 02:42:01 +00002276}
2277
Rafael Espindolad9800722010-03-11 14:07:00 +00002278void CXXNameMangler::mangleTemplateArg(const NamedDecl *P,
2279 const TemplateArgument &A) {
Mike Stump1eb44332009-09-09 15:08:12 +00002280 // <template-arg> ::= <type> # type or template
Anders Carlsson7a0ba872009-05-15 16:09:15 +00002281 // ::= X <expression> E # expression
2282 // ::= <expr-primary> # simple expressions
Douglas Gregor4fc48662011-01-13 16:39:34 +00002283 // ::= J <template-arg>* E # argument pack
Anders Carlsson7a0ba872009-05-15 16:09:15 +00002284 // ::= sp <expression> # pack expansion of (C++0x)
2285 switch (A.getKind()) {
Douglas Gregorf90b27a2011-01-03 22:36:02 +00002286 case TemplateArgument::Null:
2287 llvm_unreachable("Cannot mangle NULL template argument");
2288
Anders Carlsson7a0ba872009-05-15 16:09:15 +00002289 case TemplateArgument::Type:
2290 mangleType(A.getAsType());
2291 break;
Anders Carlsson9e85c742009-12-23 19:30:55 +00002292 case TemplateArgument::Template:
John McCallb6f532e2010-07-14 06:43:17 +00002293 // This is mangled as <type>.
2294 mangleType(A.getAsTemplate());
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002295 break;
Douglas Gregora7fc9012011-01-05 18:58:31 +00002296 case TemplateArgument::TemplateExpansion:
Douglas Gregor4fc48662011-01-13 16:39:34 +00002297 // <type> ::= Dp <type> # pack expansion (C++0x)
Douglas Gregora7fc9012011-01-05 18:58:31 +00002298 Out << "Dp";
2299 mangleType(A.getAsTemplateOrTemplatePattern());
2300 break;
Anders Carlssond553f8c2009-09-21 01:21:10 +00002301 case TemplateArgument::Expression:
2302 Out << 'X';
2303 mangleExpression(A.getAsExpr());
2304 Out << 'E';
2305 break;
Anders Carlssone170ba72009-12-14 01:45:37 +00002306 case TemplateArgument::Integral:
2307 mangleIntegerLiteral(A.getIntegralType(), *A.getAsIntegral());
Anders Carlsson7a0ba872009-05-15 16:09:15 +00002308 break;
Daniel Dunbar7e0c1952009-11-21 09:17:15 +00002309 case TemplateArgument::Declaration: {
Douglas Gregor20f0cc72010-04-23 03:10:43 +00002310 assert(P && "Missing template parameter for declaration argument");
Daniel Dunbar7e0c1952009-11-21 09:17:15 +00002311 // <expr-primary> ::= L <mangled-name> E # external name
2312
Rafael Espindolad9800722010-03-11 14:07:00 +00002313 // Clang produces AST's where pointer-to-member-function expressions
Daniel Dunbar7e0c1952009-11-21 09:17:15 +00002314 // and pointer-to-function expressions are represented as a declaration not
Rafael Espindolad9800722010-03-11 14:07:00 +00002315 // an expression. We compensate for it here to produce the correct mangling.
2316 NamedDecl *D = cast<NamedDecl>(A.getAsDecl());
2317 const NonTypeTemplateParmDecl *Parameter = cast<NonTypeTemplateParmDecl>(P);
2318 bool compensateMangling = D->isCXXClassMember() &&
2319 !Parameter->getType()->isReferenceType();
2320 if (compensateMangling) {
2321 Out << 'X';
2322 mangleOperatorName(OO_Amp, 1);
2323 }
2324
Daniel Dunbar7e0c1952009-11-21 09:17:15 +00002325 Out << 'L';
2326 // References to external entities use the mangled name; if the name would
2327 // not normally be manged then mangle it as unqualified.
2328 //
2329 // FIXME: The ABI specifies that external names here should have _Z, but
2330 // gcc leaves this off.
Rafael Espindolad9800722010-03-11 14:07:00 +00002331 if (compensateMangling)
2332 mangle(D, "_Z");
2333 else
2334 mangle(D, "Z");
Daniel Dunbar7e0c1952009-11-21 09:17:15 +00002335 Out << 'E';
Rafael Espindolad9800722010-03-11 14:07:00 +00002336
2337 if (compensateMangling)
2338 Out << 'E';
2339
Daniel Dunbar7e0c1952009-11-21 09:17:15 +00002340 break;
2341 }
Douglas Gregorf90b27a2011-01-03 22:36:02 +00002342
2343 case TemplateArgument::Pack: {
2344 // Note: proposal by Mike Herrick on 12/20/10
2345 Out << 'J';
2346 for (TemplateArgument::pack_iterator PA = A.pack_begin(),
2347 PAEnd = A.pack_end();
2348 PA != PAEnd; ++PA)
2349 mangleTemplateArg(P, *PA);
2350 Out << 'E';
2351 }
Daniel Dunbar7e0c1952009-11-21 09:17:15 +00002352 }
Anders Carlsson7a0ba872009-05-15 16:09:15 +00002353}
2354
Anders Carlsson0ccdf8d2009-09-27 00:38:53 +00002355void CXXNameMangler::mangleTemplateParameter(unsigned Index) {
2356 // <template-param> ::= T_ # first template parameter
2357 // ::= T <parameter-2 non-negative number> _
2358 if (Index == 0)
2359 Out << "T_";
2360 else
2361 Out << 'T' << (Index - 1) << '_';
2362}
2363
Anders Carlsson76967372009-09-17 00:43:46 +00002364// <substitution> ::= S <seq-id> _
2365// ::= S_
Anders Carlsson6862fc72009-09-17 04:16:28 +00002366bool CXXNameMangler::mangleSubstitution(const NamedDecl *ND) {
Anders Carlssone7c8cb62009-09-26 20:53:44 +00002367 // Try one of the standard substitutions first.
2368 if (mangleStandardSubstitution(ND))
2369 return true;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002370
Anders Carlsson433d1372009-11-07 04:26:04 +00002371 ND = cast<NamedDecl>(ND->getCanonicalDecl());
Anders Carlsson6862fc72009-09-17 04:16:28 +00002372 return mangleSubstitution(reinterpret_cast<uintptr_t>(ND));
2373}
2374
Anders Carlsson76967372009-09-17 00:43:46 +00002375bool CXXNameMangler::mangleSubstitution(QualType T) {
Anders Carlssond99edc42009-09-26 03:55:37 +00002376 if (!T.getCVRQualifiers()) {
2377 if (const RecordType *RT = T->getAs<RecordType>())
2378 return mangleSubstitution(RT->getDecl());
2379 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002380
Anders Carlsson76967372009-09-17 00:43:46 +00002381 uintptr_t TypePtr = reinterpret_cast<uintptr_t>(T.getAsOpaquePtr());
2382
Anders Carlssond3a932a2009-09-17 03:53:28 +00002383 return mangleSubstitution(TypePtr);
2384}
2385
Douglas Gregor1e9268e2010-04-28 05:58:56 +00002386bool CXXNameMangler::mangleSubstitution(TemplateName Template) {
2387 if (TemplateDecl *TD = Template.getAsTemplateDecl())
2388 return mangleSubstitution(TD);
Sean Huntc3021132010-05-05 15:23:54 +00002389
Douglas Gregor1e9268e2010-04-28 05:58:56 +00002390 Template = Context.getASTContext().getCanonicalTemplateName(Template);
2391 return mangleSubstitution(
2392 reinterpret_cast<uintptr_t>(Template.getAsVoidPointer()));
2393}
2394
Anders Carlssond3a932a2009-09-17 03:53:28 +00002395bool CXXNameMangler::mangleSubstitution(uintptr_t Ptr) {
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002396 llvm::DenseMap<uintptr_t, unsigned>::iterator I = Substitutions.find(Ptr);
Anders Carlsson76967372009-09-17 00:43:46 +00002397 if (I == Substitutions.end())
2398 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002399
Anders Carlsson76967372009-09-17 00:43:46 +00002400 unsigned SeqID = I->second;
2401 if (SeqID == 0)
2402 Out << "S_";
2403 else {
2404 SeqID--;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002405
Anders Carlsson76967372009-09-17 00:43:46 +00002406 // <seq-id> is encoded in base-36, using digits and upper case letters.
2407 char Buffer[10];
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002408 char *BufferPtr = llvm::array_endof(Buffer);
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002409
Anders Carlsson76967372009-09-17 00:43:46 +00002410 if (SeqID == 0) *--BufferPtr = '0';
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002411
Anders Carlsson76967372009-09-17 00:43:46 +00002412 while (SeqID) {
2413 assert(BufferPtr > Buffer && "Buffer overflow!");
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002414
John McCall6ab30e02010-06-09 07:26:17 +00002415 char c = static_cast<char>(SeqID % 36);
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002416
Anders Carlsson76967372009-09-17 00:43:46 +00002417 *--BufferPtr = (c < 10 ? '0' + c : 'A' + c - 10);
2418 SeqID /= 36;
2419 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002420
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002421 Out << 'S'
2422 << llvm::StringRef(BufferPtr, llvm::array_endof(Buffer)-BufferPtr)
2423 << '_';
Anders Carlsson76967372009-09-17 00:43:46 +00002424 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002425
Anders Carlsson76967372009-09-17 00:43:46 +00002426 return true;
2427}
2428
Anders Carlssonf514b542009-09-27 00:12:57 +00002429static bool isCharType(QualType T) {
2430 if (T.isNull())
2431 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002432
Anders Carlssonf514b542009-09-27 00:12:57 +00002433 return T->isSpecificBuiltinType(BuiltinType::Char_S) ||
2434 T->isSpecificBuiltinType(BuiltinType::Char_U);
2435}
2436
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002437/// isCharSpecialization - Returns whether a given type is a template
Anders Carlssonf514b542009-09-27 00:12:57 +00002438/// specialization of a given name with a single argument of type char.
2439static bool isCharSpecialization(QualType T, const char *Name) {
2440 if (T.isNull())
2441 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002442
Anders Carlssonf514b542009-09-27 00:12:57 +00002443 const RecordType *RT = T->getAs<RecordType>();
2444 if (!RT)
2445 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002446
2447 const ClassTemplateSpecializationDecl *SD =
Anders Carlssonf514b542009-09-27 00:12:57 +00002448 dyn_cast<ClassTemplateSpecializationDecl>(RT->getDecl());
2449 if (!SD)
2450 return false;
2451
2452 if (!isStdNamespace(SD->getDeclContext()))
2453 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002454
Anders Carlssonf514b542009-09-27 00:12:57 +00002455 const TemplateArgumentList &TemplateArgs = SD->getTemplateArgs();
2456 if (TemplateArgs.size() != 1)
2457 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002458
Anders Carlssonf514b542009-09-27 00:12:57 +00002459 if (!isCharType(TemplateArgs[0].getAsType()))
2460 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002461
Daniel Dunbar01eb9b92009-10-18 21:17:35 +00002462 return SD->getIdentifier()->getName() == Name;
Anders Carlssonf514b542009-09-27 00:12:57 +00002463}
2464
Anders Carlsson91f88602009-12-07 19:56:42 +00002465template <std::size_t StrLen>
Benjamin Kramer54353f42010-11-25 18:29:30 +00002466static bool isStreamCharSpecialization(const ClassTemplateSpecializationDecl*SD,
2467 const char (&Str)[StrLen]) {
Anders Carlsson91f88602009-12-07 19:56:42 +00002468 if (!SD->getIdentifier()->isStr(Str))
2469 return false;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002470
Anders Carlsson91f88602009-12-07 19:56:42 +00002471 const TemplateArgumentList &TemplateArgs = SD->getTemplateArgs();
2472 if (TemplateArgs.size() != 2)
2473 return false;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002474
Anders Carlsson91f88602009-12-07 19:56:42 +00002475 if (!isCharType(TemplateArgs[0].getAsType()))
2476 return false;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002477
Anders Carlsson91f88602009-12-07 19:56:42 +00002478 if (!isCharSpecialization(TemplateArgs[1].getAsType(), "char_traits"))
2479 return false;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002480
Anders Carlsson91f88602009-12-07 19:56:42 +00002481 return true;
2482}
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002483
Anders Carlssone7c8cb62009-09-26 20:53:44 +00002484bool CXXNameMangler::mangleStandardSubstitution(const NamedDecl *ND) {
2485 // <substitution> ::= St # ::std::
Anders Carlsson8c031552009-09-26 23:10:05 +00002486 if (const NamespaceDecl *NS = dyn_cast<NamespaceDecl>(ND)) {
Anders Carlsson47846d22009-12-04 06:23:23 +00002487 if (isStd(NS)) {
Anders Carlsson8c031552009-09-26 23:10:05 +00002488 Out << "St";
2489 return true;
2490 }
2491 }
2492
2493 if (const ClassTemplateDecl *TD = dyn_cast<ClassTemplateDecl>(ND)) {
2494 if (!isStdNamespace(TD->getDeclContext()))
2495 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002496
Anders Carlsson8c031552009-09-26 23:10:05 +00002497 // <substitution> ::= Sa # ::std::allocator
2498 if (TD->getIdentifier()->isStr("allocator")) {
2499 Out << "Sa";
2500 return true;
2501 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002502
Anders Carlsson189d59c2009-09-26 23:14:39 +00002503 // <<substitution> ::= Sb # ::std::basic_string
2504 if (TD->getIdentifier()->isStr("basic_string")) {
2505 Out << "Sb";
2506 return true;
2507 }
Anders Carlsson8c031552009-09-26 23:10:05 +00002508 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002509
2510 if (const ClassTemplateSpecializationDecl *SD =
Anders Carlssonf514b542009-09-27 00:12:57 +00002511 dyn_cast<ClassTemplateSpecializationDecl>(ND)) {
Eli Friedman5370ee22010-02-23 18:25:09 +00002512 if (!isStdNamespace(SD->getDeclContext()))
2513 return false;
2514
Anders Carlssonf514b542009-09-27 00:12:57 +00002515 // <substitution> ::= Ss # ::std::basic_string<char,
2516 // ::std::char_traits<char>,
2517 // ::std::allocator<char> >
2518 if (SD->getIdentifier()->isStr("basic_string")) {
2519 const TemplateArgumentList &TemplateArgs = SD->getTemplateArgs();
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002520
Anders Carlssonf514b542009-09-27 00:12:57 +00002521 if (TemplateArgs.size() != 3)
2522 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002523
Anders Carlssonf514b542009-09-27 00:12:57 +00002524 if (!isCharType(TemplateArgs[0].getAsType()))
2525 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002526
Anders Carlssonf514b542009-09-27 00:12:57 +00002527 if (!isCharSpecialization(TemplateArgs[1].getAsType(), "char_traits"))
2528 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002529
Anders Carlssonf514b542009-09-27 00:12:57 +00002530 if (!isCharSpecialization(TemplateArgs[2].getAsType(), "allocator"))
2531 return false;
2532
2533 Out << "Ss";
2534 return true;
2535 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002536
Anders Carlsson91f88602009-12-07 19:56:42 +00002537 // <substitution> ::= Si # ::std::basic_istream<char,
2538 // ::std::char_traits<char> >
2539 if (isStreamCharSpecialization(SD, "basic_istream")) {
2540 Out << "Si";
2541 return true;
2542 }
2543
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002544 // <substitution> ::= So # ::std::basic_ostream<char,
Anders Carlsson8f8fd8e2009-10-08 17:20:26 +00002545 // ::std::char_traits<char> >
Anders Carlsson91f88602009-12-07 19:56:42 +00002546 if (isStreamCharSpecialization(SD, "basic_ostream")) {
Anders Carlsson8f8fd8e2009-10-08 17:20:26 +00002547 Out << "So";
2548 return true;
2549 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002550
Anders Carlsson91f88602009-12-07 19:56:42 +00002551 // <substitution> ::= Sd # ::std::basic_iostream<char,
2552 // ::std::char_traits<char> >
2553 if (isStreamCharSpecialization(SD, "basic_iostream")) {
2554 Out << "Sd";
2555 return true;
2556 }
Anders Carlssonf514b542009-09-27 00:12:57 +00002557 }
Anders Carlsson8c031552009-09-26 23:10:05 +00002558 return false;
Anders Carlssone7c8cb62009-09-26 20:53:44 +00002559}
2560
Anders Carlsson76967372009-09-17 00:43:46 +00002561void CXXNameMangler::addSubstitution(QualType T) {
Anders Carlssond99edc42009-09-26 03:55:37 +00002562 if (!T.getCVRQualifiers()) {
2563 if (const RecordType *RT = T->getAs<RecordType>()) {
2564 addSubstitution(RT->getDecl());
2565 return;
2566 }
2567 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002568
Anders Carlsson76967372009-09-17 00:43:46 +00002569 uintptr_t TypePtr = reinterpret_cast<uintptr_t>(T.getAsOpaquePtr());
Anders Carlssond3a932a2009-09-17 03:53:28 +00002570 addSubstitution(TypePtr);
2571}
2572
Douglas Gregor1e9268e2010-04-28 05:58:56 +00002573void CXXNameMangler::addSubstitution(TemplateName Template) {
2574 if (TemplateDecl *TD = Template.getAsTemplateDecl())
2575 return addSubstitution(TD);
Sean Huntc3021132010-05-05 15:23:54 +00002576
Douglas Gregor1e9268e2010-04-28 05:58:56 +00002577 Template = Context.getASTContext().getCanonicalTemplateName(Template);
2578 addSubstitution(reinterpret_cast<uintptr_t>(Template.getAsVoidPointer()));
2579}
2580
Anders Carlssond3a932a2009-09-17 03:53:28 +00002581void CXXNameMangler::addSubstitution(uintptr_t Ptr) {
Anders Carlssond3a932a2009-09-17 03:53:28 +00002582 assert(!Substitutions.count(Ptr) && "Substitution already exists!");
Anders Carlsson9d85b722010-06-02 04:29:50 +00002583 Substitutions[Ptr] = SeqID++;
Anders Carlsson76967372009-09-17 00:43:46 +00002584}
2585
Daniel Dunbar1b077112009-11-21 09:06:10 +00002586//
Mike Stump1eb44332009-09-09 15:08:12 +00002587
Daniel Dunbar1b077112009-11-21 09:06:10 +00002588/// \brief Mangles the name of the declaration D and emits that name to the
2589/// given output stream.
2590///
2591/// If the declaration D requires a mangled name, this routine will emit that
2592/// mangled name to \p os and return true. Otherwise, \p os will be unchanged
2593/// and this routine will return false. In this case, the caller should just
2594/// emit the identifier of the declaration (\c D->getIdentifier()) as its
2595/// name.
Peter Collingbourne14110472011-01-13 18:57:25 +00002596void ItaniumMangleContext::mangleName(const NamedDecl *D,
Rafael Espindola0e376a02011-02-11 01:41:00 +00002597 llvm::raw_ostream &Out) {
Daniel Dunbarc02ab4c2009-11-21 09:14:44 +00002598 assert((isa<FunctionDecl>(D) || isa<VarDecl>(D)) &&
2599 "Invalid mangleName() call, argument is not a variable or function!");
2600 assert(!isa<CXXConstructorDecl>(D) && !isa<CXXDestructorDecl>(D) &&
2601 "Invalid mangleName() call on 'structor decl!");
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002602
Daniel Dunbar1b077112009-11-21 09:06:10 +00002603 PrettyStackTraceDecl CrashInfo(D, SourceLocation(),
2604 getASTContext().getSourceManager(),
2605 "Mangling declaration");
Mike Stump1eb44332009-09-09 15:08:12 +00002606
Rafael Espindolac4850c22011-02-10 23:59:36 +00002607 CXXNameMangler Mangler(*this, Out);
Daniel Dunbar94fd26d2009-11-21 09:06:22 +00002608 return Mangler.mangle(D);
Daniel Dunbar1b077112009-11-21 09:06:10 +00002609}
Mike Stump1eb44332009-09-09 15:08:12 +00002610
Peter Collingbourne14110472011-01-13 18:57:25 +00002611void ItaniumMangleContext::mangleCXXCtor(const CXXConstructorDecl *D,
2612 CXXCtorType Type,
Rafael Espindola0e376a02011-02-11 01:41:00 +00002613 llvm::raw_ostream &Out) {
Rafael Espindolac4850c22011-02-10 23:59:36 +00002614 CXXNameMangler Mangler(*this, Out, D, Type);
Daniel Dunbar77939c92009-11-21 09:06:31 +00002615 Mangler.mangle(D);
Daniel Dunbar1b077112009-11-21 09:06:10 +00002616}
Mike Stump1eb44332009-09-09 15:08:12 +00002617
Peter Collingbourne14110472011-01-13 18:57:25 +00002618void ItaniumMangleContext::mangleCXXDtor(const CXXDestructorDecl *D,
2619 CXXDtorType Type,
Rafael Espindola0e376a02011-02-11 01:41:00 +00002620 llvm::raw_ostream &Out) {
Rafael Espindolac4850c22011-02-10 23:59:36 +00002621 CXXNameMangler Mangler(*this, Out, D, Type);
Daniel Dunbar77939c92009-11-21 09:06:31 +00002622 Mangler.mangle(D);
Daniel Dunbar1b077112009-11-21 09:06:10 +00002623}
Mike Stumpf1216772009-07-31 18:25:34 +00002624
Peter Collingbourne14110472011-01-13 18:57:25 +00002625void ItaniumMangleContext::mangleThunk(const CXXMethodDecl *MD,
2626 const ThunkInfo &Thunk,
Rafael Espindolaf0be9792011-02-11 02:52:17 +00002627 llvm::raw_ostream &Out) {
Anders Carlsson19879c92010-03-23 17:17:29 +00002628 // <special-name> ::= T <call-offset> <base encoding>
2629 // # base is the nominal target function of thunk
2630 // <special-name> ::= Tc <call-offset> <call-offset> <base encoding>
2631 // # base is the nominal target function of thunk
2632 // # first call-offset is 'this' adjustment
2633 // # second call-offset is result adjustment
Sean Huntc3021132010-05-05 15:23:54 +00002634
Anders Carlsson19879c92010-03-23 17:17:29 +00002635 assert(!isa<CXXDestructorDecl>(MD) &&
2636 "Use mangleCXXDtor for destructor decls!");
Rafael Espindolac4850c22011-02-10 23:59:36 +00002637 CXXNameMangler Mangler(*this, Out);
Anders Carlsson19879c92010-03-23 17:17:29 +00002638 Mangler.getStream() << "_ZT";
2639 if (!Thunk.Return.isEmpty())
2640 Mangler.getStream() << 'c';
Sean Huntc3021132010-05-05 15:23:54 +00002641
Anders Carlsson19879c92010-03-23 17:17:29 +00002642 // Mangle the 'this' pointer adjustment.
2643 Mangler.mangleCallOffset(Thunk.This.NonVirtual, Thunk.This.VCallOffsetOffset);
Sean Huntc3021132010-05-05 15:23:54 +00002644
Anders Carlsson19879c92010-03-23 17:17:29 +00002645 // Mangle the return pointer adjustment if there is one.
2646 if (!Thunk.Return.isEmpty())
2647 Mangler.mangleCallOffset(Thunk.Return.NonVirtual,
2648 Thunk.Return.VBaseOffsetOffset);
Sean Huntc3021132010-05-05 15:23:54 +00002649
Anders Carlsson19879c92010-03-23 17:17:29 +00002650 Mangler.mangleFunctionEncoding(MD);
2651}
2652
Sean Huntc3021132010-05-05 15:23:54 +00002653void
Peter Collingbourne14110472011-01-13 18:57:25 +00002654ItaniumMangleContext::mangleCXXDtorThunk(const CXXDestructorDecl *DD,
2655 CXXDtorType Type,
2656 const ThisAdjustment &ThisAdjustment,
Rafael Espindolaf0be9792011-02-11 02:52:17 +00002657 llvm::raw_ostream &Out) {
Anders Carlsson19879c92010-03-23 17:17:29 +00002658 // <special-name> ::= T <call-offset> <base encoding>
2659 // # base is the nominal target function of thunk
Rafael Espindolac4850c22011-02-10 23:59:36 +00002660 CXXNameMangler Mangler(*this, Out, DD, Type);
Anders Carlsson19879c92010-03-23 17:17:29 +00002661 Mangler.getStream() << "_ZT";
2662
2663 // Mangle the 'this' pointer adjustment.
Sean Huntc3021132010-05-05 15:23:54 +00002664 Mangler.mangleCallOffset(ThisAdjustment.NonVirtual,
Anders Carlsson19879c92010-03-23 17:17:29 +00002665 ThisAdjustment.VCallOffsetOffset);
2666
2667 Mangler.mangleFunctionEncoding(DD);
2668}
2669
Daniel Dunbarc0747712009-11-21 09:12:13 +00002670/// mangleGuardVariable - Returns the mangled name for a guard variable
2671/// for the passed in VarDecl.
Peter Collingbourne14110472011-01-13 18:57:25 +00002672void ItaniumMangleContext::mangleItaniumGuardVariable(const VarDecl *D,
Rafael Espindolaf0be9792011-02-11 02:52:17 +00002673 llvm::raw_ostream &Out) {
Daniel Dunbarc0747712009-11-21 09:12:13 +00002674 // <special-name> ::= GV <object name> # Guard variable for one-time
2675 // # initialization
Rafael Espindolac4850c22011-02-10 23:59:36 +00002676 CXXNameMangler Mangler(*this, Out);
Daniel Dunbarc0747712009-11-21 09:12:13 +00002677 Mangler.getStream() << "_ZGV";
2678 Mangler.mangleName(D);
2679}
2680
Peter Collingbourne14110472011-01-13 18:57:25 +00002681void ItaniumMangleContext::mangleReferenceTemporary(const VarDecl *D,
Rafael Espindolaf0be9792011-02-11 02:52:17 +00002682 llvm::raw_ostream &Out) {
Anders Carlsson715edf22010-06-26 16:09:40 +00002683 // We match the GCC mangling here.
2684 // <special-name> ::= GR <object name>
Rafael Espindolac4850c22011-02-10 23:59:36 +00002685 CXXNameMangler Mangler(*this, Out);
Anders Carlsson715edf22010-06-26 16:09:40 +00002686 Mangler.getStream() << "_ZGR";
2687 Mangler.mangleName(D);
2688}
2689
Peter Collingbourne14110472011-01-13 18:57:25 +00002690void ItaniumMangleContext::mangleCXXVTable(const CXXRecordDecl *RD,
Rafael Espindolaf0be9792011-02-11 02:52:17 +00002691 llvm::raw_ostream &Out) {
Daniel Dunbarc0747712009-11-21 09:12:13 +00002692 // <special-name> ::= TV <type> # virtual table
Rafael Espindolac4850c22011-02-10 23:59:36 +00002693 CXXNameMangler Mangler(*this, Out);
Daniel Dunbarc0747712009-11-21 09:12:13 +00002694 Mangler.getStream() << "_ZTV";
Douglas Gregor1b12a3b2010-05-26 05:11:13 +00002695 Mangler.mangleNameOrStandardSubstitution(RD);
Daniel Dunbar1b077112009-11-21 09:06:10 +00002696}
Mike Stump82d75b02009-11-10 01:58:37 +00002697
Peter Collingbourne14110472011-01-13 18:57:25 +00002698void ItaniumMangleContext::mangleCXXVTT(const CXXRecordDecl *RD,
Rafael Espindolaf0be9792011-02-11 02:52:17 +00002699 llvm::raw_ostream &Out) {
Daniel Dunbarc0747712009-11-21 09:12:13 +00002700 // <special-name> ::= TT <type> # VTT structure
Rafael Espindolac4850c22011-02-10 23:59:36 +00002701 CXXNameMangler Mangler(*this, Out);
Daniel Dunbarc0747712009-11-21 09:12:13 +00002702 Mangler.getStream() << "_ZTT";
Douglas Gregor1b12a3b2010-05-26 05:11:13 +00002703 Mangler.mangleNameOrStandardSubstitution(RD);
Daniel Dunbar1b077112009-11-21 09:06:10 +00002704}
Mike Stumpab3f7e92009-11-10 01:41:59 +00002705
Peter Collingbourne14110472011-01-13 18:57:25 +00002706void ItaniumMangleContext::mangleCXXCtorVTable(const CXXRecordDecl *RD,
2707 int64_t Offset,
2708 const CXXRecordDecl *Type,
Rafael Espindolaf0be9792011-02-11 02:52:17 +00002709 llvm::raw_ostream &Out) {
Daniel Dunbarc0747712009-11-21 09:12:13 +00002710 // <special-name> ::= TC <type> <offset number> _ <base type>
Rafael Espindolac4850c22011-02-10 23:59:36 +00002711 CXXNameMangler Mangler(*this, Out);
Daniel Dunbarc0747712009-11-21 09:12:13 +00002712 Mangler.getStream() << "_ZTC";
Douglas Gregor1b12a3b2010-05-26 05:11:13 +00002713 Mangler.mangleNameOrStandardSubstitution(RD);
Daniel Dunbarc0747712009-11-21 09:12:13 +00002714 Mangler.getStream() << Offset;
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002715 Mangler.getStream() << '_';
Douglas Gregor1b12a3b2010-05-26 05:11:13 +00002716 Mangler.mangleNameOrStandardSubstitution(Type);
Daniel Dunbar1b077112009-11-21 09:06:10 +00002717}
Mike Stump738f8c22009-07-31 23:15:31 +00002718
Peter Collingbourne14110472011-01-13 18:57:25 +00002719void ItaniumMangleContext::mangleCXXRTTI(QualType Ty,
Rafael Espindolaf0be9792011-02-11 02:52:17 +00002720 llvm::raw_ostream &Out) {
Daniel Dunbarc0747712009-11-21 09:12:13 +00002721 // <special-name> ::= TI <type> # typeinfo structure
Douglas Gregor154fe982009-12-23 22:04:40 +00002722 assert(!Ty.hasQualifiers() && "RTTI info cannot have top-level qualifiers");
Rafael Espindolac4850c22011-02-10 23:59:36 +00002723 CXXNameMangler Mangler(*this, Out);
Daniel Dunbarc0747712009-11-21 09:12:13 +00002724 Mangler.getStream() << "_ZTI";
2725 Mangler.mangleType(Ty);
Daniel Dunbar1b077112009-11-21 09:06:10 +00002726}
Mike Stump67795982009-11-14 00:14:13 +00002727
Peter Collingbourne14110472011-01-13 18:57:25 +00002728void ItaniumMangleContext::mangleCXXRTTIName(QualType Ty,
Rafael Espindolaf0be9792011-02-11 02:52:17 +00002729 llvm::raw_ostream &Out) {
Daniel Dunbarc0747712009-11-21 09:12:13 +00002730 // <special-name> ::= TS <type> # typeinfo name (null terminated byte string)
Rafael Espindolac4850c22011-02-10 23:59:36 +00002731 CXXNameMangler Mangler(*this, Out);
Daniel Dunbarc0747712009-11-21 09:12:13 +00002732 Mangler.getStream() << "_ZTS";
2733 Mangler.mangleType(Ty);
Mike Stumpf1216772009-07-31 18:25:34 +00002734}
Peter Collingbourne14110472011-01-13 18:57:25 +00002735
2736MangleContext *clang::createItaniumMangleContext(ASTContext &Context,
2737 Diagnostic &Diags) {
2738 return new ItaniumMangleContext(Context, Diags);
2739}