blob: a32b0ef7512eed2f719c46dc5f3e7a6144c2b045 [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 McCalla0ce15c2011-04-24 08:23:24 +0000203 void mangleUnresolvedPrefix(NestedNameSpecifier *qualifier,
204 NamedDecl *firstQualifierLookup,
205 bool recursive = false);
206 void mangleUnresolvedName(NestedNameSpecifier *qualifier,
207 NamedDecl *firstQualifierLookup,
208 DeclarationName name,
John McCall1dd73832010-02-04 01:42:13 +0000209 unsigned KnownArity = UnknownArity);
210
John McCalla0ce15c2011-04-24 08:23:24 +0000211 void mangleUnresolvedType(QualType type);
212
Daniel Dunbar1b077112009-11-21 09:06:10 +0000213 void mangleName(const TemplateDecl *TD,
214 const TemplateArgument *TemplateArgs,
215 unsigned NumTemplateArgs);
John McCall1dd73832010-02-04 01:42:13 +0000216 void mangleUnqualifiedName(const NamedDecl *ND) {
217 mangleUnqualifiedName(ND, ND->getDeclName(), UnknownArity);
218 }
219 void mangleUnqualifiedName(const NamedDecl *ND, DeclarationName Name,
220 unsigned KnownArity);
Daniel Dunbar1b077112009-11-21 09:06:10 +0000221 void mangleUnscopedName(const NamedDecl *ND);
222 void mangleUnscopedTemplateName(const TemplateDecl *ND);
Douglas Gregor1e9268e2010-04-28 05:58:56 +0000223 void mangleUnscopedTemplateName(TemplateName);
Daniel Dunbar1b077112009-11-21 09:06:10 +0000224 void mangleSourceName(const IdentifierInfo *II);
225 void mangleLocalName(const NamedDecl *ND);
Fariborz Jahanian57058532010-03-03 19:41:08 +0000226 void mangleNestedName(const NamedDecl *ND, const DeclContext *DC,
227 bool NoFunction=false);
Daniel Dunbar1b077112009-11-21 09:06:10 +0000228 void mangleNestedName(const TemplateDecl *TD,
229 const TemplateArgument *TemplateArgs,
230 unsigned NumTemplateArgs);
John McCalla0ce15c2011-04-24 08:23:24 +0000231 void manglePrefix(NestedNameSpecifier *qualifier);
Fariborz Jahanian57058532010-03-03 19:41:08 +0000232 void manglePrefix(const DeclContext *DC, bool NoFunction=false);
Daniel Dunbar1b077112009-11-21 09:06:10 +0000233 void mangleTemplatePrefix(const TemplateDecl *ND);
Douglas Gregor20f0cc72010-04-23 03:10:43 +0000234 void mangleTemplatePrefix(TemplateName Template);
Daniel Dunbar1b077112009-11-21 09:06:10 +0000235 void mangleOperatorName(OverloadedOperatorKind OO, unsigned Arity);
236 void mangleQualifiers(Qualifiers Quals);
Douglas Gregor0a9a6d62011-01-26 17:36:28 +0000237 void mangleRefQualifier(RefQualifierKind RefQualifier);
John McCallefe6aee2009-09-05 07:56:18 +0000238
Anders Carlsson7b06f6c2009-12-10 03:14:39 +0000239 void mangleObjCMethodName(const ObjCMethodDecl *MD);
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000240
Daniel Dunbar1b077112009-11-21 09:06:10 +0000241 // Declare manglers for every type class.
John McCallefe6aee2009-09-05 07:56:18 +0000242#define ABSTRACT_TYPE(CLASS, PARENT)
243#define NON_CANONICAL_TYPE(CLASS, PARENT)
244#define TYPE(CLASS, PARENT) void mangleType(const CLASS##Type *T);
245#include "clang/AST/TypeNodes.def"
246
Daniel Dunbar1b077112009-11-21 09:06:10 +0000247 void mangleType(const TagType*);
John McCallb6f532e2010-07-14 06:43:17 +0000248 void mangleType(TemplateName);
Daniel Dunbar1b077112009-11-21 09:06:10 +0000249 void mangleBareFunctionType(const FunctionType *T,
250 bool MangleReturnType);
Bob Wilson57147a82010-11-16 00:32:18 +0000251 void mangleNeonVectorType(const VectorType *T);
Anders Carlssone170ba72009-12-14 01:45:37 +0000252
253 void mangleIntegerLiteral(QualType T, const llvm::APSInt &Value);
John McCalla0ce15c2011-04-24 08:23:24 +0000254 void mangleMemberExpr(const Expr *base, bool isArrow,
255 NestedNameSpecifier *qualifier,
256 NamedDecl *firstQualifierLookup,
257 DeclarationName name,
258 unsigned knownArity);
John McCall5e1e89b2010-08-18 19:18:59 +0000259 void mangleExpression(const Expr *E, unsigned Arity = UnknownArity);
Daniel Dunbar1b077112009-11-21 09:06:10 +0000260 void mangleCXXCtorType(CXXCtorType T);
261 void mangleCXXDtorType(CXXDtorType T);
Mike Stump1eb44332009-09-09 15:08:12 +0000262
John McCall6dbce192010-08-20 00:17:19 +0000263 void mangleTemplateArgs(const ExplicitTemplateArgumentList &TemplateArgs);
Douglas Gregor20f0cc72010-04-23 03:10:43 +0000264 void mangleTemplateArgs(TemplateName Template,
265 const TemplateArgument *TemplateArgs,
Sean Huntc3021132010-05-05 15:23:54 +0000266 unsigned NumTemplateArgs);
Rafael Espindolad9800722010-03-11 14:07:00 +0000267 void mangleTemplateArgs(const TemplateParameterList &PL,
268 const TemplateArgument *TemplateArgs,
Daniel Dunbar1b077112009-11-21 09:06:10 +0000269 unsigned NumTemplateArgs);
Rafael Espindolad9800722010-03-11 14:07:00 +0000270 void mangleTemplateArgs(const TemplateParameterList &PL,
271 const TemplateArgumentList &AL);
272 void mangleTemplateArg(const NamedDecl *P, const TemplateArgument &A);
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000273
Daniel Dunbar1b077112009-11-21 09:06:10 +0000274 void mangleTemplateParameter(unsigned Index);
275};
Peter Collingbourne14110472011-01-13 18:57:25 +0000276
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000277}
278
Anders Carlsson43f17402009-04-02 15:51:53 +0000279static bool isInCLinkageSpecification(const Decl *D) {
Douglas Gregor457e2812009-10-28 16:31:34 +0000280 D = D->getCanonicalDecl();
Mike Stump1eb44332009-09-09 15:08:12 +0000281 for (const DeclContext *DC = D->getDeclContext();
Anders Carlsson43f17402009-04-02 15:51:53 +0000282 !DC->isTranslationUnit(); DC = DC->getParent()) {
Mike Stump1eb44332009-09-09 15:08:12 +0000283 if (const LinkageSpecDecl *Linkage = dyn_cast<LinkageSpecDecl>(DC))
Anders Carlsson43f17402009-04-02 15:51:53 +0000284 return Linkage->getLanguage() == LinkageSpecDecl::lang_c;
285 }
Mike Stump1eb44332009-09-09 15:08:12 +0000286
Anders Carlsson43f17402009-04-02 15:51:53 +0000287 return false;
288}
289
Peter Collingbourne14110472011-01-13 18:57:25 +0000290bool ItaniumMangleContext::shouldMangleDeclName(const NamedDecl *D) {
Daniel Dunbarf981bf82009-11-21 09:14:52 +0000291 // In C, functions with no attributes never need to be mangled. Fastpath them.
292 if (!getASTContext().getLangOptions().CPlusPlus && !D->hasAttrs())
293 return false;
294
295 // Any decl can be declared with __asm("foo") on it, and this takes precedence
296 // over all other naming in the .o file.
297 if (D->hasAttr<AsmLabelAttr>())
298 return true;
299
Mike Stump141c5af2009-09-02 00:25:38 +0000300 // Clang's "overloadable" attribute extension to C/C++ implies name mangling
Anders Carlssona1e16222009-11-07 07:15:03 +0000301 // (always) as does passing a C++ member function and a function
302 // whose name is not a simple identifier.
Daniel Dunbarf981bf82009-11-21 09:14:52 +0000303 const FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
304 if (FD && (FD->hasAttr<OverloadableAttr>() || isa<CXXMethodDecl>(FD) ||
305 !FD->getDeclName().isIdentifier()))
306 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000307
Daniel Dunbarf981bf82009-11-21 09:14:52 +0000308 // Otherwise, no mangling is done outside C++ mode.
309 if (!getASTContext().getLangOptions().CPlusPlus)
310 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000311
Sean Hunt31455252010-01-24 03:04:27 +0000312 // Variables at global scope with non-internal linkage are not mangled
Eli Friedman7facf842009-12-02 20:32:49 +0000313 if (!FD) {
314 const DeclContext *DC = D->getDeclContext();
315 // Check for extern variable declared locally.
Fariborz Jahaniane81c5612010-06-30 18:57:21 +0000316 if (DC->isFunctionOrMethod() && D->hasLinkage())
Eli Friedman7facf842009-12-02 20:32:49 +0000317 while (!DC->isNamespace() && !DC->isTranslationUnit())
318 DC = DC->getParent();
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000319 if (DC->isTranslationUnit() && D->getLinkage() != InternalLinkage)
Eli Friedman7facf842009-12-02 20:32:49 +0000320 return false;
321 }
322
Eli Friedmanc00cb642010-07-18 20:49:59 +0000323 // Class members are always mangled.
324 if (D->getDeclContext()->isRecord())
325 return true;
326
Eli Friedman7facf842009-12-02 20:32:49 +0000327 // C functions and "main" are not mangled.
328 if ((FD && FD->isMain()) || isInCLinkageSpecification(D))
Daniel Dunbarf981bf82009-11-21 09:14:52 +0000329 return false;
330
Anders Carlsson43f17402009-04-02 15:51:53 +0000331 return true;
332}
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000333
Daniel Dunbar7e0c1952009-11-21 09:17:15 +0000334void CXXNameMangler::mangle(const NamedDecl *D, llvm::StringRef Prefix) {
Mike Stump141c5af2009-09-02 00:25:38 +0000335 // Any decl can be declared with __asm("foo") on it, and this takes precedence
336 // over all other naming in the .o file.
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000337 if (const AsmLabelAttr *ALA = D->getAttr<AsmLabelAttr>()) {
Chris Lattnerca3f25c2009-03-21 08:24:40 +0000338 // If we have an asm name, then we use it as the mangling.
Rafael Espindola4e274e92011-02-15 22:23:51 +0000339
340 // Adding the prefix can cause problems when one file has a "foo" and
341 // another has a "\01foo". That is known to happen on ELF with the
342 // tricks normally used for producing aliases (PR9177). Fortunately the
343 // llvm mangler on ELF is a nop, so we can just avoid adding the \01
Peter Collingbourne69317432011-04-06 12:29:09 +0000344 // marker. We also avoid adding the marker if this is an alias for an
345 // LLVM intrinsic.
Rafael Espindola4e274e92011-02-15 22:23:51 +0000346 llvm::StringRef UserLabelPrefix =
347 getASTContext().Target.getUserLabelPrefix();
Peter Collingbourne69317432011-04-06 12:29:09 +0000348 if (!UserLabelPrefix.empty() && !ALA->getLabel().startswith("llvm."))
Rafael Espindola4e274e92011-02-15 22:23:51 +0000349 Out << '\01'; // LLVM IR Marker for __asm("foo")
350
Chris Lattnerca3f25c2009-03-21 08:24:40 +0000351 Out << ALA->getLabel();
Daniel Dunbarf981bf82009-11-21 09:14:52 +0000352 return;
Chris Lattnerca3f25c2009-03-21 08:24:40 +0000353 }
Mike Stump1eb44332009-09-09 15:08:12 +0000354
Sean Hunt31455252010-01-24 03:04:27 +0000355 // <mangled-name> ::= _Z <encoding>
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000356 // ::= <data name>
357 // ::= <special-name>
Daniel Dunbar7e0c1952009-11-21 09:17:15 +0000358 Out << Prefix;
359 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
Daniel Dunbarf981bf82009-11-21 09:14:52 +0000360 mangleFunctionEncoding(FD);
Rafael Espindolad9800722010-03-11 14:07:00 +0000361 else if (const VarDecl *VD = dyn_cast<VarDecl>(D))
362 mangleName(VD);
Daniel Dunbar7e0c1952009-11-21 09:17:15 +0000363 else
Rafael Espindolad9800722010-03-11 14:07:00 +0000364 mangleName(cast<FieldDecl>(D));
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000365}
366
367void CXXNameMangler::mangleFunctionEncoding(const FunctionDecl *FD) {
368 // <encoding> ::= <function name> <bare-function-type>
369 mangleName(FD);
Mike Stump1eb44332009-09-09 15:08:12 +0000370
Daniel Dunbar7e0c1952009-11-21 09:17:15 +0000371 // Don't mangle in the type if this isn't a decl we should typically mangle.
372 if (!Context.shouldMangleDeclName(FD))
373 return;
374
Mike Stump141c5af2009-09-02 00:25:38 +0000375 // Whether the mangling of a function type includes the return type depends on
376 // the context and the nature of the function. The rules for deciding whether
377 // the return type is included are:
Mike Stump1eb44332009-09-09 15:08:12 +0000378 //
Douglas Gregor1fd2dd12009-06-29 22:39:32 +0000379 // 1. Template functions (names or types) have return types encoded, with
380 // the exceptions listed below.
Mike Stump1eb44332009-09-09 15:08:12 +0000381 // 2. Function types not appearing as part of a function name mangling,
Douglas Gregor1fd2dd12009-06-29 22:39:32 +0000382 // e.g. parameters, pointer types, etc., have return type encoded, with the
383 // exceptions listed below.
384 // 3. Non-template function names do not have return types encoded.
385 //
Mike Stump141c5af2009-09-02 00:25:38 +0000386 // The exceptions mentioned in (1) and (2) above, for which the return type is
387 // never included, are
Douglas Gregor1fd2dd12009-06-29 22:39:32 +0000388 // 1. Constructors.
389 // 2. Destructors.
390 // 3. Conversion operator functions, e.g. operator int.
391 bool MangleReturnType = false;
Anders Carlsson9234b7f2009-09-17 03:46:43 +0000392 if (FunctionTemplateDecl *PrimaryTemplate = FD->getPrimaryTemplate()) {
393 if (!(isa<CXXConstructorDecl>(FD) || isa<CXXDestructorDecl>(FD) ||
394 isa<CXXConversionDecl>(FD)))
395 MangleReturnType = true;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000396
Anders Carlsson9234b7f2009-09-17 03:46:43 +0000397 // Mangle the type of the primary template.
398 FD = PrimaryTemplate->getTemplatedDecl();
399 }
400
John McCall54e14c42009-10-22 22:37:11 +0000401 // Do the canonicalization out here because parameter types can
402 // undergo additional canonicalization (e.g. array decay).
John McCallf4c73712011-01-19 06:33:43 +0000403 const FunctionType *FT
404 = cast<FunctionType>(Context.getASTContext()
John McCall54e14c42009-10-22 22:37:11 +0000405 .getCanonicalType(FD->getType()));
406
407 mangleBareFunctionType(FT, MangleReturnType);
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000408}
409
Anders Carlsson47846d22009-12-04 06:23:23 +0000410static const DeclContext *IgnoreLinkageSpecDecls(const DeclContext *DC) {
411 while (isa<LinkageSpecDecl>(DC)) {
Anders Carlsson47846d22009-12-04 06:23:23 +0000412 DC = DC->getParent();
413 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000414
Anders Carlsson47846d22009-12-04 06:23:23 +0000415 return DC;
416}
417
Anders Carlssonc820f902010-06-02 15:58:27 +0000418/// isStd - Return whether a given namespace is the 'std' namespace.
419static bool isStd(const NamespaceDecl *NS) {
420 if (!IgnoreLinkageSpecDecls(NS->getParent())->isTranslationUnit())
421 return false;
422
423 const IdentifierInfo *II = NS->getOriginalNamespace()->getIdentifier();
424 return II && II->isStr("std");
425}
426
Anders Carlsson47846d22009-12-04 06:23:23 +0000427// isStdNamespace - Return whether a given decl context is a toplevel 'std'
428// namespace.
Daniel Dunbar1308af92009-11-21 09:11:45 +0000429static bool isStdNamespace(const DeclContext *DC) {
Anders Carlsson47846d22009-12-04 06:23:23 +0000430 if (!DC->isNamespace())
431 return false;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000432
Anders Carlsson47846d22009-12-04 06:23:23 +0000433 return isStd(cast<NamespaceDecl>(DC));
Daniel Dunbar1308af92009-11-21 09:11:45 +0000434}
435
Anders Carlssonbb36ba42009-09-26 03:24:57 +0000436static const TemplateDecl *
437isTemplate(const NamedDecl *ND, const TemplateArgumentList *&TemplateArgs) {
Anders Carlsson2744a062009-09-18 19:00:18 +0000438 // Check if we have a function template.
439 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)){
Anders Carlssonbb36ba42009-09-26 03:24:57 +0000440 if (const TemplateDecl *TD = FD->getPrimaryTemplate()) {
Anders Carlsson2744a062009-09-18 19:00:18 +0000441 TemplateArgs = FD->getTemplateSpecializationArgs();
Anders Carlssonbb36ba42009-09-26 03:24:57 +0000442 return TD;
Anders Carlsson2744a062009-09-18 19:00:18 +0000443 }
444 }
445
Anders Carlssoneafc6dc2009-09-18 19:44:50 +0000446 // Check if we have a class template.
447 if (const ClassTemplateSpecializationDecl *Spec =
448 dyn_cast<ClassTemplateSpecializationDecl>(ND)) {
449 TemplateArgs = &Spec->getTemplateArgs();
Anders Carlssonbb36ba42009-09-26 03:24:57 +0000450 return Spec->getSpecializedTemplate();
Anders Carlssoneafc6dc2009-09-18 19:44:50 +0000451 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000452
Anders Carlsson2744a062009-09-18 19:00:18 +0000453 return 0;
454}
455
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000456void CXXNameMangler::mangleName(const NamedDecl *ND) {
457 // <name> ::= <nested-name>
458 // ::= <unscoped-name>
459 // ::= <unscoped-template-name> <template-args>
Anders Carlsson201ce742009-09-17 03:17:01 +0000460 // ::= <local-name>
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000461 //
Anders Carlssond58d6f72009-09-17 16:12:20 +0000462 const DeclContext *DC = ND->getDeclContext();
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000463
Eli Friedman7facf842009-12-02 20:32:49 +0000464 // If this is an extern variable declared locally, the relevant DeclContext
465 // is that of the containing namespace, or the translation unit.
466 if (isa<FunctionDecl>(DC) && ND->hasLinkage())
467 while (!DC->isNamespace() && !DC->isTranslationUnit())
468 DC = DC->getParent();
John McCall82b7d7b2010-10-18 21:28:44 +0000469 else if (GetLocalClassDecl(ND)) {
470 mangleLocalName(ND);
471 return;
472 }
Eli Friedman7facf842009-12-02 20:32:49 +0000473
Anders Carlsson5cc58c62009-09-22 17:23:30 +0000474 while (isa<LinkageSpecDecl>(DC))
Anders Carlssond58d6f72009-09-17 16:12:20 +0000475 DC = DC->getParent();
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000476
Anders Carlssond58d6f72009-09-17 16:12:20 +0000477 if (DC->isTranslationUnit() || isStdNamespace(DC)) {
Anders Carlsson2744a062009-09-18 19:00:18 +0000478 // Check if we have a template.
479 const TemplateArgumentList *TemplateArgs = 0;
Anders Carlsson0fa6df42009-09-26 19:45:45 +0000480 if (const TemplateDecl *TD = isTemplate(ND, TemplateArgs)) {
Anders Carlsson2744a062009-09-18 19:00:18 +0000481 mangleUnscopedTemplateName(TD);
Rafael Espindolad9800722010-03-11 14:07:00 +0000482 TemplateParameterList *TemplateParameters = TD->getTemplateParameters();
483 mangleTemplateArgs(*TemplateParameters, *TemplateArgs);
Anders Carlsson2744a062009-09-18 19:00:18 +0000484 return;
Anders Carlsson7482e242009-09-18 04:29:09 +0000485 }
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000486
Anders Carlsson7482e242009-09-18 04:29:09 +0000487 mangleUnscopedName(ND);
488 return;
489 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000490
Anders Carlsson7b06f6c2009-12-10 03:14:39 +0000491 if (isa<FunctionDecl>(DC) || isa<ObjCMethodDecl>(DC)) {
Anders Carlsson7482e242009-09-18 04:29:09 +0000492 mangleLocalName(ND);
493 return;
494 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000495
Eli Friedman7facf842009-12-02 20:32:49 +0000496 mangleNestedName(ND, DC);
Anders Carlsson7482e242009-09-18 04:29:09 +0000497}
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000498void CXXNameMangler::mangleName(const TemplateDecl *TD,
Anders Carlsson7624f212009-09-18 02:42:01 +0000499 const TemplateArgument *TemplateArgs,
500 unsigned NumTemplateArgs) {
Anders Carlsson47846d22009-12-04 06:23:23 +0000501 const DeclContext *DC = IgnoreLinkageSpecDecls(TD->getDeclContext());
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000502
Anders Carlsson7624f212009-09-18 02:42:01 +0000503 if (DC->isTranslationUnit() || isStdNamespace(DC)) {
Anders Carlsson0fa6df42009-09-26 19:45:45 +0000504 mangleUnscopedTemplateName(TD);
Rafael Espindolad9800722010-03-11 14:07:00 +0000505 TemplateParameterList *TemplateParameters = TD->getTemplateParameters();
506 mangleTemplateArgs(*TemplateParameters, TemplateArgs, NumTemplateArgs);
Anders Carlsson7624f212009-09-18 02:42:01 +0000507 } else {
508 mangleNestedName(TD, TemplateArgs, NumTemplateArgs);
509 }
510}
511
Anders Carlsson201ce742009-09-17 03:17:01 +0000512void CXXNameMangler::mangleUnscopedName(const NamedDecl *ND) {
513 // <unscoped-name> ::= <unqualified-name>
514 // ::= St <unqualified-name> # ::std::
515 if (isStdNamespace(ND->getDeclContext()))
516 Out << "St";
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000517
Anders Carlsson201ce742009-09-17 03:17:01 +0000518 mangleUnqualifiedName(ND);
519}
520
Anders Carlsson0fa6df42009-09-26 19:45:45 +0000521void CXXNameMangler::mangleUnscopedTemplateName(const TemplateDecl *ND) {
Anders Carlsson201ce742009-09-17 03:17:01 +0000522 // <unscoped-template-name> ::= <unscoped-name>
523 // ::= <substitution>
Anders Carlsson7624f212009-09-18 02:42:01 +0000524 if (mangleSubstitution(ND))
Anders Carlsson03c9d532009-09-17 04:02:31 +0000525 return;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000526
Douglas Gregor32fb4e12010-02-05 20:45:00 +0000527 // <template-template-param> ::= <template-param>
528 if (const TemplateTemplateParmDecl *TTP
529 = dyn_cast<TemplateTemplateParmDecl>(ND)) {
530 mangleTemplateParameter(TTP->getIndex());
531 return;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000532 }
Douglas Gregor32fb4e12010-02-05 20:45:00 +0000533
Anders Carlsson1668f202009-09-26 20:13:56 +0000534 mangleUnscopedName(ND->getTemplatedDecl());
Anders Carlsson7624f212009-09-18 02:42:01 +0000535 addSubstitution(ND);
Anders Carlsson201ce742009-09-17 03:17:01 +0000536}
537
Douglas Gregor1e9268e2010-04-28 05:58:56 +0000538void CXXNameMangler::mangleUnscopedTemplateName(TemplateName Template) {
539 // <unscoped-template-name> ::= <unscoped-name>
540 // ::= <substitution>
541 if (TemplateDecl *TD = Template.getAsTemplateDecl())
542 return mangleUnscopedTemplateName(TD);
Sean Huntc3021132010-05-05 15:23:54 +0000543
Douglas Gregor1e9268e2010-04-28 05:58:56 +0000544 if (mangleSubstitution(Template))
545 return;
546
547 // FIXME: How to cope with operators here?
548 DependentTemplateName *Dependent = Template.getAsDependentTemplateName();
549 assert(Dependent && "Not a dependent template name?");
550 if (!Dependent->isIdentifier()) {
551 // FIXME: We can't possibly know the arity of the operator here!
552 Diagnostic &Diags = Context.getDiags();
553 unsigned DiagID = Diags.getCustomDiagID(Diagnostic::Error,
554 "cannot mangle dependent operator name");
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000555 Diags.Report(DiagID);
Douglas Gregor1e9268e2010-04-28 05:58:56 +0000556 return;
557 }
Sean Huntc3021132010-05-05 15:23:54 +0000558
Douglas Gregor1e9268e2010-04-28 05:58:56 +0000559 mangleSourceName(Dependent->getIdentifier());
560 addSubstitution(Template);
561}
562
John McCall1b600522011-04-24 03:07:16 +0000563void CXXNameMangler::mangleFloat(const llvm::APFloat &f) {
564 // ABI:
565 // Floating-point literals are encoded using a fixed-length
566 // lowercase hexadecimal string corresponding to the internal
567 // representation (IEEE on Itanium), high-order bytes first,
568 // without leading zeroes. For example: "Lf bf800000 E" is -1.0f
569 // on Itanium.
570 // APInt::toString uses uppercase hexadecimal, and it's not really
571 // worth embellishing that interface for this use case, so we just
572 // do a second pass to lowercase things.
573 typedef llvm::SmallString<20> buffer_t;
574 buffer_t buffer;
575 f.bitcastToAPInt().toString(buffer, 16, false);
576
577 for (buffer_t::iterator i = buffer.begin(), e = buffer.end(); i != e; ++i)
578 if (isupper(*i)) *i = tolower(*i);
579
580 Out.write(buffer.data(), buffer.size());
John McCall0512e482010-07-14 04:20:34 +0000581}
582
583void CXXNameMangler::mangleNumber(const llvm::APSInt &Value) {
584 if (Value.isSigned() && Value.isNegative()) {
585 Out << 'n';
586 Value.abs().print(Out, true);
587 } else
588 Value.print(Out, Value.isSigned());
589}
590
Anders Carlssona94822e2009-11-26 02:32:05 +0000591void CXXNameMangler::mangleNumber(int64_t Number) {
592 // <number> ::= [n] <non-negative decimal integer>
593 if (Number < 0) {
594 Out << 'n';
595 Number = -Number;
596 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000597
Anders Carlssona94822e2009-11-26 02:32:05 +0000598 Out << Number;
599}
600
Anders Carlsson19879c92010-03-23 17:17:29 +0000601void CXXNameMangler::mangleCallOffset(int64_t NonVirtual, int64_t Virtual) {
Mike Stump141c5af2009-09-02 00:25:38 +0000602 // <call-offset> ::= h <nv-offset> _
603 // ::= v <v-offset> _
604 // <nv-offset> ::= <offset number> # non-virtual base override
Anders Carlssona94822e2009-11-26 02:32:05 +0000605 // <v-offset> ::= <offset number> _ <virtual offset number>
Mike Stump141c5af2009-09-02 00:25:38 +0000606 // # virtual base override, with vcall offset
Anders Carlsson19879c92010-03-23 17:17:29 +0000607 if (!Virtual) {
Anders Carlssona94822e2009-11-26 02:32:05 +0000608 Out << 'h';
Anders Carlsson19879c92010-03-23 17:17:29 +0000609 mangleNumber(NonVirtual);
Anders Carlssona94822e2009-11-26 02:32:05 +0000610 Out << '_';
611 return;
Mike Stump141c5af2009-09-02 00:25:38 +0000612 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000613
Anders Carlssona94822e2009-11-26 02:32:05 +0000614 Out << 'v';
Anders Carlsson19879c92010-03-23 17:17:29 +0000615 mangleNumber(NonVirtual);
Anders Carlssona94822e2009-11-26 02:32:05 +0000616 Out << '_';
Anders Carlsson19879c92010-03-23 17:17:29 +0000617 mangleNumber(Virtual);
Anders Carlssona94822e2009-11-26 02:32:05 +0000618 Out << '_';
Mike Stump9124bcc2009-09-02 00:56:18 +0000619}
620
John McCalla0ce15c2011-04-24 08:23:24 +0000621void CXXNameMangler::mangleUnresolvedType(QualType type) {
622 if (const TemplateSpecializationType *TST =
623 type->getAs<TemplateSpecializationType>()) {
624 if (!mangleSubstitution(QualType(TST, 0))) {
625 mangleTemplatePrefix(TST->getTemplateName());
Sean Huntc3021132010-05-05 15:23:54 +0000626
Douglas Gregoraa2187d2011-02-28 00:04:36 +0000627 // FIXME: GCC does not appear to mangle the template arguments when
628 // the template in question is a dependent template name. Should we
629 // emulate that badness?
John McCalla0ce15c2011-04-24 08:23:24 +0000630 mangleTemplateArgs(TST->getTemplateName(), TST->getArgs(),
631 TST->getNumArgs());
632 addSubstitution(QualType(TST, 0));
Rafael Espindola9b35b252010-03-17 04:28:11 +0000633 }
John McCalla0ce15c2011-04-24 08:23:24 +0000634 } else if (const DependentTemplateSpecializationType *DTST
635 = type->getAs<DependentTemplateSpecializationType>()) {
636 TemplateName Template
637 = getASTContext().getDependentTemplateName(DTST->getQualifier(),
638 DTST->getIdentifier());
639 mangleTemplatePrefix(Template);
640
641 // FIXME: GCC does not appear to mangle the template arguments when
642 // the template in question is a dependent template name. Should we
643 // emulate that badness?
644 mangleTemplateArgs(Template, DTST->getArgs(), DTST->getNumArgs());
645 } else {
646 // We use the QualType mangle type variant here because it handles
647 // substitutions.
648 mangleType(type);
John McCall1dd73832010-02-04 01:42:13 +0000649 }
650}
651
John McCalla0ce15c2011-04-24 08:23:24 +0000652/// Mangle everything prior to the base-unresolved-name in an unresolved-name.
653///
654/// \param firstQualifierLookup - the entity found by unqualified lookup
655/// for the first name in the qualifier, if this is for a member expression
656/// \param recursive - true if this is being called recursively,
657/// i.e. if there is more prefix "to the right".
658void CXXNameMangler::mangleUnresolvedPrefix(NestedNameSpecifier *qualifier,
659 NamedDecl *firstQualifierLookup,
660 bool recursive) {
John McCall1dd73832010-02-04 01:42:13 +0000661
John McCalla0ce15c2011-04-24 08:23:24 +0000662 // x, ::x
663 // <unresolved-name> ::= [gs] <base-unresolved-name>
664
665 // T::x / decltype(p)::x
666 // <unresolved-name> ::= sr <unresolved-type> <base-unresolved-name>
667
668 // T::N::x /decltype(p)::N::x
669 // <unresolved-name> ::= srN <unresolved-type> <unresolved-qualifier-level>+ E
670 // <base-unresolved-name>
671
672 // A::x, N::y, A<T>::z; "gs" means leading "::"
673 // <unresolved-name> ::= [gs] sr <unresolved-qualifier-level>+ E
674 // <base-unresolved-name>
675
676 switch (qualifier->getKind()) {
677 case NestedNameSpecifier::Global:
678 Out << "gs";
679
680 // We want an 'sr' unless this is the entire NNS.
681 if (recursive)
682 Out << "sr";
683
684 // We never want an 'E' here.
685 return;
686
687 case NestedNameSpecifier::Namespace:
688 if (qualifier->getPrefix())
689 mangleUnresolvedPrefix(qualifier->getPrefix(), firstQualifierLookup,
690 /*recursive*/ true);
691 else
692 Out << "sr";
693 mangleSourceName(qualifier->getAsNamespace()->getIdentifier());
694 break;
695 case NestedNameSpecifier::NamespaceAlias:
696 if (qualifier->getPrefix())
697 mangleUnresolvedPrefix(qualifier->getPrefix(), firstQualifierLookup,
698 /*recursive*/ true);
699 else
700 Out << "sr";
701 mangleSourceName(qualifier->getAsNamespaceAlias()->getIdentifier());
702 break;
703
704 case NestedNameSpecifier::TypeSpec:
705 case NestedNameSpecifier::TypeSpecWithTemplate: {
706 // Both cases want this.
707 Out << "sr";
708
709 // We only get here recursively if we're followed by identifiers.
710 if (recursive) Out << 'N';
711
712 mangleUnresolvedType(QualType(qualifier->getAsType(), 0));
713
714 // We never want to print 'E' directly after an unresolved-type,
715 // so we return directly.
716 return;
717 }
718
719 case NestedNameSpecifier::Identifier:
720 // Member expressions can have these without prefixes.
721 if (qualifier->getPrefix()) {
722 mangleUnresolvedPrefix(qualifier->getPrefix(), firstQualifierLookup,
723 /*recursive*/ true);
724 } else if (firstQualifierLookup) {
725
726 // Try to make a proper qualifier out of the lookup result, and
727 // then just recurse on that.
728 NestedNameSpecifier *newQualifier;
729 if (TypeDecl *typeDecl = dyn_cast<TypeDecl>(firstQualifierLookup)) {
730 QualType type = getASTContext().getTypeDeclType(typeDecl);
731
732 // Pretend we had a different nested name specifier.
733 newQualifier = NestedNameSpecifier::Create(getASTContext(),
734 /*prefix*/ 0,
735 /*template*/ false,
736 type.getTypePtr());
737 } else if (NamespaceDecl *nspace =
738 dyn_cast<NamespaceDecl>(firstQualifierLookup)) {
739 newQualifier = NestedNameSpecifier::Create(getASTContext(),
740 /*prefix*/ 0,
741 nspace);
742 } else if (NamespaceAliasDecl *alias =
743 dyn_cast<NamespaceAliasDecl>(firstQualifierLookup)) {
744 newQualifier = NestedNameSpecifier::Create(getASTContext(),
745 /*prefix*/ 0,
746 alias);
747 } else {
748 // No sensible mangling to do here.
749 newQualifier = 0;
750 }
751
752 if (newQualifier)
753 return mangleUnresolvedPrefix(newQualifier, /*lookup*/ 0, recursive);
754
755 } else {
756 Out << "sr";
757 }
758
759 mangleSourceName(qualifier->getAsIdentifier());
760 break;
761 }
762
763 // If this was the innermost part of the NNS, and we fell out to
764 // here, append an 'E'.
765 if (!recursive)
766 Out << 'E';
767}
768
769/// Mangle an unresolved-name, which is generally used for names which
770/// weren't resolved to specific entities.
771void CXXNameMangler::mangleUnresolvedName(NestedNameSpecifier *qualifier,
772 NamedDecl *firstQualifierLookup,
773 DeclarationName name,
774 unsigned knownArity) {
775 if (qualifier) mangleUnresolvedPrefix(qualifier, firstQualifierLookup);
776 mangleUnqualifiedName(0, name, knownArity);
John McCall1dd73832010-02-04 01:42:13 +0000777}
778
Anders Carlsson6f7e2f42010-06-08 14:49:03 +0000779static const FieldDecl *FindFirstNamedDataMember(const RecordDecl *RD) {
780 assert(RD->isAnonymousStructOrUnion() &&
781 "Expected anonymous struct or union!");
782
783 for (RecordDecl::field_iterator I = RD->field_begin(), E = RD->field_end();
784 I != E; ++I) {
785 const FieldDecl *FD = *I;
786
787 if (FD->getIdentifier())
788 return FD;
789
790 if (const RecordType *RT = FD->getType()->getAs<RecordType>()) {
791 if (const FieldDecl *NamedDataMember =
792 FindFirstNamedDataMember(RT->getDecl()))
793 return NamedDataMember;
794 }
795 }
796
797 // We didn't find a named data member.
798 return 0;
799}
800
John McCall1dd73832010-02-04 01:42:13 +0000801void CXXNameMangler::mangleUnqualifiedName(const NamedDecl *ND,
802 DeclarationName Name,
803 unsigned KnownArity) {
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000804 // <unqualified-name> ::= <operator-name>
Mike Stump1eb44332009-09-09 15:08:12 +0000805 // ::= <ctor-dtor-name>
806 // ::= <source-name>
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000807 switch (Name.getNameKind()) {
Anders Carlssonc4355b62009-10-07 01:45:02 +0000808 case DeclarationName::Identifier: {
Anders Carlssonc4355b62009-10-07 01:45:02 +0000809 if (const IdentifierInfo *II = Name.getAsIdentifierInfo()) {
Sean Hunt31455252010-01-24 03:04:27 +0000810 // We must avoid conflicts between internally- and externally-
John McCall74990f42011-03-22 06:34:45 +0000811 // linked variable and function declaration names in the same TU:
812 // void test() { extern void foo(); }
813 // static void foo();
814 // This naming convention is the same as that followed by GCC,
815 // though it shouldn't actually matter.
816 if (ND && ND->getLinkage() == InternalLinkage &&
Sean Hunt31455252010-01-24 03:04:27 +0000817 ND->getDeclContext()->isFileContext())
818 Out << 'L';
819
Anders Carlssonc4355b62009-10-07 01:45:02 +0000820 mangleSourceName(II);
821 break;
822 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000823
John McCall1dd73832010-02-04 01:42:13 +0000824 // Otherwise, an anonymous entity. We must have a declaration.
825 assert(ND && "mangling empty name without declaration");
826
827 if (const NamespaceDecl *NS = dyn_cast<NamespaceDecl>(ND)) {
828 if (NS->isAnonymousNamespace()) {
829 // This is how gcc mangles these names.
830 Out << "12_GLOBAL__N_1";
831 break;
832 }
833 }
834
Anders Carlsson6f7e2f42010-06-08 14:49:03 +0000835 if (const VarDecl *VD = dyn_cast<VarDecl>(ND)) {
836 // We must have an anonymous union or struct declaration.
837 const RecordDecl *RD =
838 cast<RecordDecl>(VD->getType()->getAs<RecordType>()->getDecl());
839
840 // Itanium C++ ABI 5.1.2:
841 //
842 // For the purposes of mangling, the name of an anonymous union is
843 // considered to be the name of the first named data member found by a
844 // pre-order, depth-first, declaration-order walk of the data members of
845 // the anonymous union. If there is no such data member (i.e., if all of
846 // the data members in the union are unnamed), then there is no way for
847 // a program to refer to the anonymous union, and there is therefore no
848 // need to mangle its name.
849 const FieldDecl *FD = FindFirstNamedDataMember(RD);
John McCall7121c8f2010-08-05 22:02:13 +0000850
851 // It's actually possible for various reasons for us to get here
852 // with an empty anonymous struct / union. Fortunately, it
853 // doesn't really matter what name we generate.
854 if (!FD) break;
Anders Carlsson6f7e2f42010-06-08 14:49:03 +0000855 assert(FD->getIdentifier() && "Data member name isn't an identifier!");
856
857 mangleSourceName(FD->getIdentifier());
858 break;
859 }
860
Anders Carlssonc4355b62009-10-07 01:45:02 +0000861 // We must have an anonymous struct.
862 const TagDecl *TD = cast<TagDecl>(ND);
Richard Smith162e1c12011-04-15 14:24:37 +0000863 if (const TypedefNameDecl *D = TD->getTypedefNameForAnonDecl()) {
Anders Carlssonc4355b62009-10-07 01:45:02 +0000864 assert(TD->getDeclContext() == D->getDeclContext() &&
865 "Typedef should not be in another decl context!");
866 assert(D->getDeclName().getAsIdentifierInfo() &&
867 "Typedef was not named!");
868 mangleSourceName(D->getDeclName().getAsIdentifierInfo());
869 break;
870 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000871
Anders Carlssonc4355b62009-10-07 01:45:02 +0000872 // Get a unique id for the anonymous struct.
873 uint64_t AnonStructId = Context.getAnonymousStructId(TD);
874
875 // Mangle it as a source name in the form
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000876 // [n] $_<id>
Anders Carlssonc4355b62009-10-07 01:45:02 +0000877 // where n is the length of the string.
878 llvm::SmallString<8> Str;
879 Str += "$_";
880 Str += llvm::utostr(AnonStructId);
881
882 Out << Str.size();
883 Out << Str.str();
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000884 break;
Anders Carlssonc4355b62009-10-07 01:45:02 +0000885 }
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000886
887 case DeclarationName::ObjCZeroArgSelector:
888 case DeclarationName::ObjCOneArgSelector:
889 case DeclarationName::ObjCMultiArgSelector:
890 assert(false && "Can't mangle Objective-C selector names here!");
891 break;
892
893 case DeclarationName::CXXConstructorName:
Anders Carlsson27ae5362009-04-17 01:58:57 +0000894 if (ND == Structor)
Mike Stump141c5af2009-09-02 00:25:38 +0000895 // If the named decl is the C++ constructor we're mangling, use the type
896 // we were given.
Anders Carlsson27ae5362009-04-17 01:58:57 +0000897 mangleCXXCtorType(static_cast<CXXCtorType>(StructorType));
Anders Carlsson3ac86b52009-04-15 05:36:58 +0000898 else
899 // Otherwise, use the complete constructor name. This is relevant if a
900 // class with a constructor is declared within a constructor.
901 mangleCXXCtorType(Ctor_Complete);
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000902 break;
903
904 case DeclarationName::CXXDestructorName:
Anders Carlsson27ae5362009-04-17 01:58:57 +0000905 if (ND == Structor)
Mike Stump141c5af2009-09-02 00:25:38 +0000906 // If the named decl is the C++ destructor we're mangling, use the type we
907 // were given.
Anders Carlsson27ae5362009-04-17 01:58:57 +0000908 mangleCXXDtorType(static_cast<CXXDtorType>(StructorType));
909 else
910 // Otherwise, use the complete destructor name. This is relevant if a
911 // class with a destructor is declared within a destructor.
912 mangleCXXDtorType(Dtor_Complete);
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000913 break;
914
915 case DeclarationName::CXXConversionFunctionName:
Mike Stump1eb44332009-09-09 15:08:12 +0000916 // <operator-name> ::= cv <type> # (cast)
Douglas Gregor219cc612009-02-13 01:28:03 +0000917 Out << "cv";
Anders Carlssonb5404912009-10-07 01:06:45 +0000918 mangleType(Context.getASTContext().getCanonicalType(Name.getCXXNameType()));
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000919 break;
920
Anders Carlsson8257d412009-12-22 06:36:32 +0000921 case DeclarationName::CXXOperatorName: {
John McCall1dd73832010-02-04 01:42:13 +0000922 unsigned Arity;
923 if (ND) {
924 Arity = cast<FunctionDecl>(ND)->getNumParams();
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000925
John McCall1dd73832010-02-04 01:42:13 +0000926 // If we have a C++ member function, we need to include the 'this' pointer.
927 // FIXME: This does not make sense for operators that are static, but their
928 // names stay the same regardless of the arity (operator new for instance).
929 if (isa<CXXMethodDecl>(ND))
930 Arity++;
931 } else
932 Arity = KnownArity;
933
Anders Carlsson8257d412009-12-22 06:36:32 +0000934 mangleOperatorName(Name.getCXXOverloadedOperator(), Arity);
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000935 break;
Anders Carlsson8257d412009-12-22 06:36:32 +0000936 }
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000937
Sean Hunt3e518bd2009-11-29 07:34:05 +0000938 case DeclarationName::CXXLiteralOperatorName:
Sean Hunt5dd6b392009-12-04 21:11:13 +0000939 // FIXME: This mangling is not yet official.
Sean Hunt2421f662009-12-04 21:01:37 +0000940 Out << "li";
Sean Hunt3e518bd2009-11-29 07:34:05 +0000941 mangleSourceName(Name.getCXXLiteralIdentifier());
942 break;
943
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000944 case DeclarationName::CXXUsingDirective:
945 assert(false && "Can't mangle a using directive name!");
Douglas Gregor219cc612009-02-13 01:28:03 +0000946 break;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000947 }
948}
949
950void CXXNameMangler::mangleSourceName(const IdentifierInfo *II) {
951 // <source-name> ::= <positive length number> <identifier>
952 // <number> ::= [n] <non-negative decimal integer>
953 // <identifier> ::= <unqualified source code identifier>
954 Out << II->getLength() << II->getName();
955}
956
Eli Friedman7facf842009-12-02 20:32:49 +0000957void CXXNameMangler::mangleNestedName(const NamedDecl *ND,
Fariborz Jahanian57058532010-03-03 19:41:08 +0000958 const DeclContext *DC,
959 bool NoFunction) {
Douglas Gregor0a9a6d62011-01-26 17:36:28 +0000960 // <nested-name>
961 // ::= N [<CV-qualifiers>] [<ref-qualifier>] <prefix> <unqualified-name> E
962 // ::= N [<CV-qualifiers>] [<ref-qualifier>] <template-prefix>
963 // <template-args> E
Anders Carlssond99edc42009-09-26 03:55:37 +0000964
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000965 Out << 'N';
Douglas Gregor0a9a6d62011-01-26 17:36:28 +0000966 if (const CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(ND)) {
John McCall0953e762009-09-24 19:53:00 +0000967 mangleQualifiers(Qualifiers::fromCVRMask(Method->getTypeQualifiers()));
Douglas Gregor0a9a6d62011-01-26 17:36:28 +0000968 mangleRefQualifier(Method->getRefQualifier());
969 }
970
Anders Carlsson2744a062009-09-18 19:00:18 +0000971 // Check if we have a template.
972 const TemplateArgumentList *TemplateArgs = 0;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000973 if (const TemplateDecl *TD = isTemplate(ND, TemplateArgs)) {
Anders Carlsson2744a062009-09-18 19:00:18 +0000974 mangleTemplatePrefix(TD);
Rafael Espindolad9800722010-03-11 14:07:00 +0000975 TemplateParameterList *TemplateParameters = TD->getTemplateParameters();
976 mangleTemplateArgs(*TemplateParameters, *TemplateArgs);
Fariborz Jahanian57058532010-03-03 19:41:08 +0000977 }
978 else {
979 manglePrefix(DC, NoFunction);
Anders Carlsson7482e242009-09-18 04:29:09 +0000980 mangleUnqualifiedName(ND);
981 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000982
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000983 Out << 'E';
984}
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000985void CXXNameMangler::mangleNestedName(const TemplateDecl *TD,
Anders Carlsson7624f212009-09-18 02:42:01 +0000986 const TemplateArgument *TemplateArgs,
987 unsigned NumTemplateArgs) {
Anders Carlssone45117b2009-09-27 19:53:49 +0000988 // <nested-name> ::= N [<CV-qualifiers>] <template-prefix> <template-args> E
989
Anders Carlsson7624f212009-09-18 02:42:01 +0000990 Out << 'N';
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000991
Anders Carlssone45117b2009-09-27 19:53:49 +0000992 mangleTemplatePrefix(TD);
Rafael Espindolad9800722010-03-11 14:07:00 +0000993 TemplateParameterList *TemplateParameters = TD->getTemplateParameters();
994 mangleTemplateArgs(*TemplateParameters, TemplateArgs, NumTemplateArgs);
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000995
Anders Carlsson7624f212009-09-18 02:42:01 +0000996 Out << 'E';
997}
998
Anders Carlsson1b42c792009-04-02 16:24:45 +0000999void CXXNameMangler::mangleLocalName(const NamedDecl *ND) {
1000 // <local-name> := Z <function encoding> E <entity name> [<discriminator>]
1001 // := Z <function encoding> E s [<discriminator>]
Mike Stump1eb44332009-09-09 15:08:12 +00001002 // <discriminator> := _ <non-negative number>
Fariborz Jahanian57058532010-03-03 19:41:08 +00001003 const DeclContext *DC = ND->getDeclContext();
Anders Carlsson1b42c792009-04-02 16:24:45 +00001004 Out << 'Z';
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001005
Charles Davis685b1d92010-05-26 18:25:27 +00001006 if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(DC)) {
1007 mangleObjCMethodName(MD);
John McCall82b7d7b2010-10-18 21:28:44 +00001008 } else if (const CXXRecordDecl *RD = GetLocalClassDecl(ND)) {
1009 mangleFunctionEncoding(cast<FunctionDecl>(RD->getDeclContext()));
Fariborz Jahanian57058532010-03-03 19:41:08 +00001010 Out << 'E';
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001011
John McCall82b7d7b2010-10-18 21:28:44 +00001012 // Mangle the name relative to the closest enclosing function.
1013 if (ND == RD) // equality ok because RD derived from ND above
1014 mangleUnqualifiedName(ND);
1015 else
1016 mangleNestedName(ND, DC, true /*NoFunction*/);
1017
Fariborz Jahanian4819ac42010-03-04 01:02:03 +00001018 unsigned disc;
John McCall82b7d7b2010-10-18 21:28:44 +00001019 if (Context.getNextDiscriminator(RD, disc)) {
Fariborz Jahanian4819ac42010-03-04 01:02:03 +00001020 if (disc < 10)
1021 Out << '_' << disc;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001022 else
Fariborz Jahanian4819ac42010-03-04 01:02:03 +00001023 Out << "__" << disc << '_';
1024 }
Fariborz Jahanian57058532010-03-03 19:41:08 +00001025
1026 return;
1027 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001028 else
Fariborz Jahanian57058532010-03-03 19:41:08 +00001029 mangleFunctionEncoding(cast<FunctionDecl>(DC));
Anders Carlsson7b06f6c2009-12-10 03:14:39 +00001030
Anders Carlsson1b42c792009-04-02 16:24:45 +00001031 Out << 'E';
Eli Friedman6f9f25d2009-12-11 20:21:38 +00001032 mangleUnqualifiedName(ND);
Anders Carlsson1b42c792009-04-02 16:24:45 +00001033}
1034
John McCalla0ce15c2011-04-24 08:23:24 +00001035void CXXNameMangler::manglePrefix(NestedNameSpecifier *qualifier) {
1036 switch (qualifier->getKind()) {
1037 case NestedNameSpecifier::Global:
1038 // nothing
1039 return;
1040
1041 case NestedNameSpecifier::Namespace:
1042 mangleName(qualifier->getAsNamespace());
1043 return;
1044
1045 case NestedNameSpecifier::NamespaceAlias:
1046 mangleName(qualifier->getAsNamespaceAlias()->getNamespace());
1047 return;
1048
1049 case NestedNameSpecifier::TypeSpec:
1050 case NestedNameSpecifier::TypeSpecWithTemplate:
1051 mangleUnresolvedType(QualType(qualifier->getAsType(), 0));
1052 return;
1053
1054 case NestedNameSpecifier::Identifier:
1055 // Member expressions can have these without prefixes, but that
1056 // should end up in mangleUnresolvedPrefix instead.
1057 assert(qualifier->getPrefix());
1058 manglePrefix(qualifier->getPrefix());
1059
1060 mangleSourceName(qualifier->getAsIdentifier());
1061 return;
1062 }
1063
1064 llvm_unreachable("unexpected nested name specifier");
1065}
1066
Fariborz Jahanian57058532010-03-03 19:41:08 +00001067void CXXNameMangler::manglePrefix(const DeclContext *DC, bool NoFunction) {
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001068 // <prefix> ::= <prefix> <unqualified-name>
1069 // ::= <template-prefix> <template-args>
1070 // ::= <template-param>
1071 // ::= # empty
1072 // ::= <substitution>
Anders Carlsson6862fc72009-09-17 04:16:28 +00001073
Anders Carlssonadd28822009-09-22 20:33:31 +00001074 while (isa<LinkageSpecDecl>(DC))
1075 DC = DC->getParent();
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00001076
Anders Carlsson9263e912009-09-18 18:39:58 +00001077 if (DC->isTranslationUnit())
1078 return;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00001079
Douglas Gregor35415f52010-05-25 17:04:15 +00001080 if (const BlockDecl *Block = dyn_cast<BlockDecl>(DC)) {
1081 manglePrefix(DC->getParent(), NoFunction);
1082 llvm::SmallString<64> Name;
Rafael Espindolac4850c22011-02-10 23:59:36 +00001083 llvm::raw_svector_ostream NameStream(Name);
1084 Context.mangleBlock(Block, NameStream);
1085 NameStream.flush();
Douglas Gregor35415f52010-05-25 17:04:15 +00001086 Out << Name.size() << Name;
1087 return;
1088 }
1089
Anders Carlsson6862fc72009-09-17 04:16:28 +00001090 if (mangleSubstitution(cast<NamedDecl>(DC)))
1091 return;
Anders Carlsson7482e242009-09-18 04:29:09 +00001092
Anders Carlsson2ee3fca2009-09-18 20:11:09 +00001093 // Check if we have a template.
1094 const TemplateArgumentList *TemplateArgs = 0;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00001095 if (const TemplateDecl *TD = isTemplate(cast<NamedDecl>(DC), TemplateArgs)) {
Anders Carlsson2ee3fca2009-09-18 20:11:09 +00001096 mangleTemplatePrefix(TD);
Rafael Espindolad9800722010-03-11 14:07:00 +00001097 TemplateParameterList *TemplateParameters = TD->getTemplateParameters();
1098 mangleTemplateArgs(*TemplateParameters, *TemplateArgs);
Fariborz Jahanian57058532010-03-03 19:41:08 +00001099 }
Douglas Gregor35415f52010-05-25 17:04:15 +00001100 else if(NoFunction && (isa<FunctionDecl>(DC) || isa<ObjCMethodDecl>(DC)))
Fariborz Jahanian57058532010-03-03 19:41:08 +00001101 return;
Douglas Gregor35415f52010-05-25 17:04:15 +00001102 else if (const ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(DC))
1103 mangleObjCMethodName(Method);
Fariborz Jahanian57058532010-03-03 19:41:08 +00001104 else {
1105 manglePrefix(DC->getParent(), NoFunction);
Anders Carlsson2ee3fca2009-09-18 20:11:09 +00001106 mangleUnqualifiedName(cast<NamedDecl>(DC));
1107 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00001108
Anders Carlsson6862fc72009-09-17 04:16:28 +00001109 addSubstitution(cast<NamedDecl>(DC));
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001110}
1111
Douglas Gregor20f0cc72010-04-23 03:10:43 +00001112void CXXNameMangler::mangleTemplatePrefix(TemplateName Template) {
1113 // <template-prefix> ::= <prefix> <template unqualified-name>
1114 // ::= <template-param>
1115 // ::= <substitution>
1116 if (TemplateDecl *TD = Template.getAsTemplateDecl())
1117 return mangleTemplatePrefix(TD);
1118
1119 if (QualifiedTemplateName *Qualified = Template.getAsQualifiedTemplateName())
John McCalla0ce15c2011-04-24 08:23:24 +00001120 manglePrefix(Qualified->getQualifier());
Sean Huntc3021132010-05-05 15:23:54 +00001121
Douglas Gregor20f0cc72010-04-23 03:10:43 +00001122 if (OverloadedTemplateStorage *Overloaded
1123 = Template.getAsOverloadedTemplate()) {
Sean Huntc3021132010-05-05 15:23:54 +00001124 mangleUnqualifiedName(0, (*Overloaded->begin())->getDeclName(),
Douglas Gregor20f0cc72010-04-23 03:10:43 +00001125 UnknownArity);
1126 return;
1127 }
Sean Huntc3021132010-05-05 15:23:54 +00001128
Douglas Gregor20f0cc72010-04-23 03:10:43 +00001129 DependentTemplateName *Dependent = Template.getAsDependentTemplateName();
1130 assert(Dependent && "Unknown template name kind?");
John McCalla0ce15c2011-04-24 08:23:24 +00001131 manglePrefix(Dependent->getQualifier());
Douglas Gregor1e9268e2010-04-28 05:58:56 +00001132 mangleUnscopedTemplateName(Template);
Douglas Gregor20f0cc72010-04-23 03:10:43 +00001133}
1134
Anders Carlsson0fa6df42009-09-26 19:45:45 +00001135void CXXNameMangler::mangleTemplatePrefix(const TemplateDecl *ND) {
Anders Carlsson7482e242009-09-18 04:29:09 +00001136 // <template-prefix> ::= <prefix> <template unqualified-name>
1137 // ::= <template-param>
1138 // ::= <substitution>
Douglas Gregor32fb4e12010-02-05 20:45:00 +00001139 // <template-template-param> ::= <template-param>
1140 // <substitution>
Anders Carlsson7482e242009-09-18 04:29:09 +00001141
Anders Carlssonaeb85372009-09-26 22:18:22 +00001142 if (mangleSubstitution(ND))
1143 return;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00001144
Douglas Gregor32fb4e12010-02-05 20:45:00 +00001145 // <template-template-param> ::= <template-param>
1146 if (const TemplateTemplateParmDecl *TTP
1147 = dyn_cast<TemplateTemplateParmDecl>(ND)) {
1148 mangleTemplateParameter(TTP->getIndex());
1149 return;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001150 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00001151
Anders Carlssonaa73ab12009-09-18 18:47:07 +00001152 manglePrefix(ND->getDeclContext());
Anders Carlsson1668f202009-09-26 20:13:56 +00001153 mangleUnqualifiedName(ND->getTemplatedDecl());
Anders Carlssonaeb85372009-09-26 22:18:22 +00001154 addSubstitution(ND);
Anders Carlsson7482e242009-09-18 04:29:09 +00001155}
1156
John McCallb6f532e2010-07-14 06:43:17 +00001157/// Mangles a template name under the production <type>. Required for
1158/// template template arguments.
1159/// <type> ::= <class-enum-type>
1160/// ::= <template-param>
1161/// ::= <substitution>
1162void CXXNameMangler::mangleType(TemplateName TN) {
1163 if (mangleSubstitution(TN))
1164 return;
1165
1166 TemplateDecl *TD = 0;
1167
1168 switch (TN.getKind()) {
1169 case TemplateName::QualifiedTemplate:
1170 TD = TN.getAsQualifiedTemplateName()->getTemplateDecl();
1171 goto HaveDecl;
1172
1173 case TemplateName::Template:
1174 TD = TN.getAsTemplateDecl();
1175 goto HaveDecl;
1176
1177 HaveDecl:
1178 if (isa<TemplateTemplateParmDecl>(TD))
1179 mangleTemplateParameter(cast<TemplateTemplateParmDecl>(TD)->getIndex());
1180 else
1181 mangleName(TD);
1182 break;
1183
1184 case TemplateName::OverloadedTemplate:
1185 llvm_unreachable("can't mangle an overloaded template name as a <type>");
1186 break;
1187
1188 case TemplateName::DependentTemplate: {
1189 const DependentTemplateName *Dependent = TN.getAsDependentTemplateName();
1190 assert(Dependent->isIdentifier());
1191
1192 // <class-enum-type> ::= <name>
1193 // <name> ::= <nested-name>
John McCalla0ce15c2011-04-24 08:23:24 +00001194 mangleUnresolvedPrefix(Dependent->getQualifier(), 0);
John McCallb6f532e2010-07-14 06:43:17 +00001195 mangleSourceName(Dependent->getIdentifier());
1196 break;
1197 }
1198
Douglas Gregor1aee05d2011-01-15 06:45:20 +00001199 case TemplateName::SubstTemplateTemplateParmPack: {
1200 SubstTemplateTemplateParmPackStorage *SubstPack
1201 = TN.getAsSubstTemplateTemplateParmPack();
1202 mangleTemplateParameter(SubstPack->getParameterPack()->getIndex());
1203 break;
1204 }
John McCallb6f532e2010-07-14 06:43:17 +00001205 }
1206
1207 addSubstitution(TN);
1208}
1209
Mike Stump1eb44332009-09-09 15:08:12 +00001210void
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001211CXXNameMangler::mangleOperatorName(OverloadedOperatorKind OO, unsigned Arity) {
1212 switch (OO) {
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001213 // <operator-name> ::= nw # new
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001214 case OO_New: Out << "nw"; break;
1215 // ::= na # new[]
1216 case OO_Array_New: Out << "na"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001217 // ::= dl # delete
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001218 case OO_Delete: Out << "dl"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001219 // ::= da # delete[]
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001220 case OO_Array_Delete: Out << "da"; break;
1221 // ::= ps # + (unary)
John McCall5e1e89b2010-08-18 19:18:59 +00001222 // ::= pl # + (binary or unknown)
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001223 case OO_Plus:
Anders Carlsson8257d412009-12-22 06:36:32 +00001224 Out << (Arity == 1? "ps" : "pl"); break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001225 // ::= ng # - (unary)
John McCall5e1e89b2010-08-18 19:18:59 +00001226 // ::= mi # - (binary or unknown)
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001227 case OO_Minus:
Anders Carlsson8257d412009-12-22 06:36:32 +00001228 Out << (Arity == 1? "ng" : "mi"); break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001229 // ::= ad # & (unary)
John McCall5e1e89b2010-08-18 19:18:59 +00001230 // ::= an # & (binary or unknown)
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001231 case OO_Amp:
Anders Carlsson8257d412009-12-22 06:36:32 +00001232 Out << (Arity == 1? "ad" : "an"); break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001233 // ::= de # * (unary)
John McCall5e1e89b2010-08-18 19:18:59 +00001234 // ::= ml # * (binary or unknown)
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001235 case OO_Star:
John McCall5e1e89b2010-08-18 19:18:59 +00001236 // Use binary when unknown.
Anders Carlsson8257d412009-12-22 06:36:32 +00001237 Out << (Arity == 1? "de" : "ml"); break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001238 // ::= co # ~
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001239 case OO_Tilde: Out << "co"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001240 // ::= dv # /
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001241 case OO_Slash: Out << "dv"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001242 // ::= rm # %
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001243 case OO_Percent: Out << "rm"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001244 // ::= or # |
1245 case OO_Pipe: Out << "or"; break;
1246 // ::= eo # ^
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001247 case OO_Caret: Out << "eo"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001248 // ::= aS # =
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001249 case OO_Equal: Out << "aS"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001250 // ::= pL # +=
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001251 case OO_PlusEqual: Out << "pL"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001252 // ::= mI # -=
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001253 case OO_MinusEqual: Out << "mI"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001254 // ::= mL # *=
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001255 case OO_StarEqual: Out << "mL"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001256 // ::= dV # /=
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001257 case OO_SlashEqual: Out << "dV"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001258 // ::= rM # %=
1259 case OO_PercentEqual: Out << "rM"; break;
1260 // ::= aN # &=
1261 case OO_AmpEqual: Out << "aN"; break;
1262 // ::= oR # |=
1263 case OO_PipeEqual: Out << "oR"; break;
1264 // ::= eO # ^=
1265 case OO_CaretEqual: Out << "eO"; break;
1266 // ::= ls # <<
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001267 case OO_LessLess: Out << "ls"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001268 // ::= rs # >>
1269 case OO_GreaterGreater: Out << "rs"; break;
1270 // ::= lS # <<=
1271 case OO_LessLessEqual: Out << "lS"; break;
1272 // ::= rS # >>=
1273 case OO_GreaterGreaterEqual: Out << "rS"; break;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001274 // ::= eq # ==
1275 case OO_EqualEqual: Out << "eq"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001276 // ::= ne # !=
1277 case OO_ExclaimEqual: Out << "ne"; break;
1278 // ::= lt # <
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001279 case OO_Less: Out << "lt"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001280 // ::= gt # >
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001281 case OO_Greater: Out << "gt"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001282 // ::= le # <=
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001283 case OO_LessEqual: Out << "le"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001284 // ::= ge # >=
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001285 case OO_GreaterEqual: Out << "ge"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001286 // ::= nt # !
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001287 case OO_Exclaim: Out << "nt"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001288 // ::= aa # &&
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001289 case OO_AmpAmp: Out << "aa"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001290 // ::= oo # ||
1291 case OO_PipePipe: Out << "oo"; break;
1292 // ::= pp # ++
1293 case OO_PlusPlus: Out << "pp"; break;
1294 // ::= mm # --
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001295 case OO_MinusMinus: Out << "mm"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001296 // ::= cm # ,
1297 case OO_Comma: Out << "cm"; break;
1298 // ::= pm # ->*
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001299 case OO_ArrowStar: Out << "pm"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001300 // ::= pt # ->
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001301 case OO_Arrow: Out << "pt"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001302 // ::= cl # ()
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001303 case OO_Call: Out << "cl"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001304 // ::= ix # []
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001305 case OO_Subscript: Out << "ix"; break;
Anders Carlssone170ba72009-12-14 01:45:37 +00001306
1307 // ::= qu # ?
1308 // The conditional operator can't be overloaded, but we still handle it when
1309 // mangling expressions.
1310 case OO_Conditional: Out << "qu"; break;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001311
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001312 case OO_None:
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001313 case NUM_OVERLOADED_OPERATORS:
Mike Stump1eb44332009-09-09 15:08:12 +00001314 assert(false && "Not an overloaded operator");
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001315 break;
1316 }
1317}
1318
John McCall0953e762009-09-24 19:53:00 +00001319void CXXNameMangler::mangleQualifiers(Qualifiers Quals) {
Mike Stump1eb44332009-09-09 15:08:12 +00001320 // <CV-qualifiers> ::= [r] [V] [K] # restrict (C99), volatile, const
John McCall0953e762009-09-24 19:53:00 +00001321 if (Quals.hasRestrict())
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001322 Out << 'r';
John McCall0953e762009-09-24 19:53:00 +00001323 if (Quals.hasVolatile())
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001324 Out << 'V';
John McCall0953e762009-09-24 19:53:00 +00001325 if (Quals.hasConst())
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001326 Out << 'K';
John McCall0953e762009-09-24 19:53:00 +00001327
Douglas Gregor56079f72010-06-14 23:15:08 +00001328 if (Quals.hasAddressSpace()) {
1329 // Extension:
1330 //
1331 // <type> ::= U <address-space-number>
1332 //
1333 // where <address-space-number> is a source name consisting of 'AS'
1334 // followed by the address space <number>.
1335 llvm::SmallString<64> ASString;
1336 ASString = "AS" + llvm::utostr_32(Quals.getAddressSpace());
1337 Out << 'U' << ASString.size() << ASString;
1338 }
1339
John McCall0953e762009-09-24 19:53:00 +00001340 // FIXME: For now, just drop all extension qualifiers on the floor.
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001341}
1342
Douglas Gregor0a9a6d62011-01-26 17:36:28 +00001343void CXXNameMangler::mangleRefQualifier(RefQualifierKind RefQualifier) {
1344 // <ref-qualifier> ::= R # lvalue reference
1345 // ::= O # rvalue-reference
1346 // Proposal to Itanium C++ ABI list on 1/26/11
1347 switch (RefQualifier) {
1348 case RQ_None:
1349 break;
1350
1351 case RQ_LValue:
1352 Out << 'R';
1353 break;
1354
1355 case RQ_RValue:
1356 Out << 'O';
1357 break;
1358 }
1359}
1360
Anders Carlsson7b06f6c2009-12-10 03:14:39 +00001361void CXXNameMangler::mangleObjCMethodName(const ObjCMethodDecl *MD) {
Rafael Espindolaf0be9792011-02-11 02:52:17 +00001362 Context.mangleObjCMethodName(MD, Out);
Anders Carlsson7b06f6c2009-12-10 03:14:39 +00001363}
1364
John McCallb47f7482011-01-26 20:05:40 +00001365void CXXNameMangler::mangleType(QualType nonCanon) {
Anders Carlsson4843e582009-03-10 17:07:44 +00001366 // Only operate on the canonical type!
John McCallb47f7482011-01-26 20:05:40 +00001367 QualType canon = nonCanon.getCanonicalType();
Anders Carlsson4843e582009-03-10 17:07:44 +00001368
John McCallb47f7482011-01-26 20:05:40 +00001369 SplitQualType split = canon.split();
1370 Qualifiers quals = split.second;
1371 const Type *ty = split.first;
1372
1373 bool isSubstitutable = quals || !isa<BuiltinType>(ty);
1374 if (isSubstitutable && mangleSubstitution(canon))
Anders Carlsson76967372009-09-17 00:43:46 +00001375 return;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001376
John McCallb47f7482011-01-26 20:05:40 +00001377 // If we're mangling a qualified array type, push the qualifiers to
1378 // the element type.
1379 if (quals && isa<ArrayType>(ty)) {
1380 ty = Context.getASTContext().getAsArrayType(canon);
1381 quals = Qualifiers();
1382
1383 // Note that we don't update canon: we want to add the
1384 // substitution at the canonical type.
1385 }
1386
1387 if (quals) {
1388 mangleQualifiers(quals);
John McCall0953e762009-09-24 19:53:00 +00001389 // Recurse: even if the qualified type isn't yet substitutable,
1390 // the unqualified type might be.
John McCallb47f7482011-01-26 20:05:40 +00001391 mangleType(QualType(ty, 0));
Anders Carlsson76967372009-09-17 00:43:46 +00001392 } else {
John McCallb47f7482011-01-26 20:05:40 +00001393 switch (ty->getTypeClass()) {
John McCallefe6aee2009-09-05 07:56:18 +00001394#define ABSTRACT_TYPE(CLASS, PARENT)
1395#define NON_CANONICAL_TYPE(CLASS, PARENT) \
Anders Carlsson76967372009-09-17 00:43:46 +00001396 case Type::CLASS: \
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +00001397 llvm_unreachable("can't mangle non-canonical type " #CLASS "Type"); \
Anders Carlsson76967372009-09-17 00:43:46 +00001398 return;
John McCallefe6aee2009-09-05 07:56:18 +00001399#define TYPE(CLASS, PARENT) \
Anders Carlsson76967372009-09-17 00:43:46 +00001400 case Type::CLASS: \
John McCallb47f7482011-01-26 20:05:40 +00001401 mangleType(static_cast<const CLASS##Type*>(ty)); \
Anders Carlsson76967372009-09-17 00:43:46 +00001402 break;
John McCallefe6aee2009-09-05 07:56:18 +00001403#include "clang/AST/TypeNodes.def"
Anders Carlsson76967372009-09-17 00:43:46 +00001404 }
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001405 }
Anders Carlsson76967372009-09-17 00:43:46 +00001406
1407 // Add the substitution.
John McCallb47f7482011-01-26 20:05:40 +00001408 if (isSubstitutable)
1409 addSubstitution(canon);
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001410}
1411
Douglas Gregor1b12a3b2010-05-26 05:11:13 +00001412void CXXNameMangler::mangleNameOrStandardSubstitution(const NamedDecl *ND) {
1413 if (!mangleStandardSubstitution(ND))
1414 mangleName(ND);
1415}
1416
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001417void CXXNameMangler::mangleType(const BuiltinType *T) {
John McCallefe6aee2009-09-05 07:56:18 +00001418 // <type> ::= <builtin-type>
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001419 // <builtin-type> ::= v # void
1420 // ::= w # wchar_t
1421 // ::= b # bool
1422 // ::= c # char
1423 // ::= a # signed char
1424 // ::= h # unsigned char
1425 // ::= s # short
1426 // ::= t # unsigned short
1427 // ::= i # int
1428 // ::= j # unsigned int
1429 // ::= l # long
1430 // ::= m # unsigned long
1431 // ::= x # long long, __int64
1432 // ::= y # unsigned long long, __int64
1433 // ::= n # __int128
1434 // UNSUPPORTED: ::= o # unsigned __int128
1435 // ::= f # float
1436 // ::= d # double
1437 // ::= e # long double, __float80
1438 // UNSUPPORTED: ::= g # __float128
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001439 // UNSUPPORTED: ::= Dd # IEEE 754r decimal floating point (64 bits)
1440 // UNSUPPORTED: ::= De # IEEE 754r decimal floating point (128 bits)
1441 // UNSUPPORTED: ::= Df # IEEE 754r decimal floating point (32 bits)
1442 // UNSUPPORTED: ::= Dh # IEEE 754r half-precision floating point (16 bits)
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001443 // ::= Di # char32_t
1444 // ::= Ds # char16_t
Anders Carlssone2923682010-11-04 04:31:32 +00001445 // ::= Dn # std::nullptr_t (i.e., decltype(nullptr))
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001446 // ::= u <source-name> # vendor extended type
1447 switch (T->getKind()) {
1448 case BuiltinType::Void: Out << 'v'; break;
1449 case BuiltinType::Bool: Out << 'b'; break;
1450 case BuiltinType::Char_U: case BuiltinType::Char_S: Out << 'c'; break;
1451 case BuiltinType::UChar: Out << 'h'; break;
1452 case BuiltinType::UShort: Out << 't'; break;
1453 case BuiltinType::UInt: Out << 'j'; break;
1454 case BuiltinType::ULong: Out << 'm'; break;
1455 case BuiltinType::ULongLong: Out << 'y'; break;
Chris Lattner2df9ced2009-04-30 02:43:43 +00001456 case BuiltinType::UInt128: Out << 'o'; break;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001457 case BuiltinType::SChar: Out << 'a'; break;
Chris Lattner3f59c972010-12-25 23:25:43 +00001458 case BuiltinType::WChar_S:
1459 case BuiltinType::WChar_U: Out << 'w'; break;
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001460 case BuiltinType::Char16: Out << "Ds"; break;
1461 case BuiltinType::Char32: Out << "Di"; break;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001462 case BuiltinType::Short: Out << 's'; break;
1463 case BuiltinType::Int: Out << 'i'; break;
1464 case BuiltinType::Long: Out << 'l'; break;
1465 case BuiltinType::LongLong: Out << 'x'; break;
Chris Lattner2df9ced2009-04-30 02:43:43 +00001466 case BuiltinType::Int128: Out << 'n'; break;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001467 case BuiltinType::Float: Out << 'f'; break;
1468 case BuiltinType::Double: Out << 'd'; break;
1469 case BuiltinType::LongDouble: Out << 'e'; break;
Anders Carlssone2923682010-11-04 04:31:32 +00001470 case BuiltinType::NullPtr: Out << "Dn"; break;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001471
1472 case BuiltinType::Overload:
1473 case BuiltinType::Dependent:
John McCall1de4d4e2011-04-07 08:22:57 +00001474 case BuiltinType::UnknownAny:
Mike Stump1eb44332009-09-09 15:08:12 +00001475 assert(false &&
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001476 "Overloaded and dependent types shouldn't get to name mangling");
1477 break;
Steve Naroff9533a7f2009-07-22 17:14:51 +00001478 case BuiltinType::ObjCId: Out << "11objc_object"; break;
1479 case BuiltinType::ObjCClass: Out << "10objc_class"; break;
Fariborz Jahanian13dcd002009-11-21 19:53:08 +00001480 case BuiltinType::ObjCSel: Out << "13objc_selector"; break;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001481 }
1482}
1483
John McCallefe6aee2009-09-05 07:56:18 +00001484// <type> ::= <function-type>
1485// <function-type> ::= F [Y] <bare-function-type> E
1486void CXXNameMangler::mangleType(const FunctionProtoType *T) {
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001487 Out << 'F';
Mike Stumpf5408fe2009-05-16 07:57:57 +00001488 // FIXME: We don't have enough information in the AST to produce the 'Y'
1489 // encoding for extern "C" function types.
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001490 mangleBareFunctionType(T, /*MangleReturnType=*/true);
1491 Out << 'E';
1492}
John McCallefe6aee2009-09-05 07:56:18 +00001493void CXXNameMangler::mangleType(const FunctionNoProtoType *T) {
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +00001494 llvm_unreachable("Can't mangle K&R function prototypes");
John McCallefe6aee2009-09-05 07:56:18 +00001495}
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001496void CXXNameMangler::mangleBareFunctionType(const FunctionType *T,
1497 bool MangleReturnType) {
John McCallefe6aee2009-09-05 07:56:18 +00001498 // We should never be mangling something without a prototype.
1499 const FunctionProtoType *Proto = cast<FunctionProtoType>(T);
1500
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001501 // <bare-function-type> ::= <signature type>+
1502 if (MangleReturnType)
John McCallefe6aee2009-09-05 07:56:18 +00001503 mangleType(Proto->getResultType());
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001504
Anders Carlsson93296682010-06-02 04:40:13 +00001505 if (Proto->getNumArgs() == 0 && !Proto->isVariadic()) {
Eli Friedmana7e68452010-08-22 01:00:03 +00001506 // <builtin-type> ::= v # void
Anders Carlssonc6c91bc2009-04-01 00:15:23 +00001507 Out << 'v';
1508 return;
1509 }
Mike Stump1eb44332009-09-09 15:08:12 +00001510
Douglas Gregor72564e72009-02-26 23:50:07 +00001511 for (FunctionProtoType::arg_type_iterator Arg = Proto->arg_type_begin(),
Mike Stump1eb44332009-09-09 15:08:12 +00001512 ArgEnd = Proto->arg_type_end();
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001513 Arg != ArgEnd; ++Arg)
1514 mangleType(*Arg);
Douglas Gregor219cc612009-02-13 01:28:03 +00001515
1516 // <builtin-type> ::= z # ellipsis
1517 if (Proto->isVariadic())
1518 Out << 'z';
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001519}
1520
John McCallefe6aee2009-09-05 07:56:18 +00001521// <type> ::= <class-enum-type>
Mike Stump1eb44332009-09-09 15:08:12 +00001522// <class-enum-type> ::= <name>
John McCalled976492009-12-04 22:46:56 +00001523void CXXNameMangler::mangleType(const UnresolvedUsingType *T) {
1524 mangleName(T->getDecl());
1525}
1526
1527// <type> ::= <class-enum-type>
1528// <class-enum-type> ::= <name>
John McCallefe6aee2009-09-05 07:56:18 +00001529void CXXNameMangler::mangleType(const EnumType *T) {
1530 mangleType(static_cast<const TagType*>(T));
1531}
1532void CXXNameMangler::mangleType(const RecordType *T) {
1533 mangleType(static_cast<const TagType*>(T));
1534}
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001535void CXXNameMangler::mangleType(const TagType *T) {
Eli Friedmanecb7e932009-12-11 18:00:57 +00001536 mangleName(T->getDecl());
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001537}
1538
John McCallefe6aee2009-09-05 07:56:18 +00001539// <type> ::= <array-type>
1540// <array-type> ::= A <positive dimension number> _ <element type>
1541// ::= A [<dimension expression>] _ <element type>
1542void CXXNameMangler::mangleType(const ConstantArrayType *T) {
1543 Out << 'A' << T->getSize() << '_';
1544 mangleType(T->getElementType());
1545}
1546void CXXNameMangler::mangleType(const VariableArrayType *T) {
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001547 Out << 'A';
Fariborz Jahanian7281d1f2010-11-02 16:54:00 +00001548 // decayed vla types (size 0) will just be skipped.
1549 if (T->getSizeExpr())
1550 mangleExpression(T->getSizeExpr());
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001551 Out << '_';
1552 mangleType(T->getElementType());
1553}
John McCallefe6aee2009-09-05 07:56:18 +00001554void CXXNameMangler::mangleType(const DependentSizedArrayType *T) {
1555 Out << 'A';
1556 mangleExpression(T->getSizeExpr());
1557 Out << '_';
1558 mangleType(T->getElementType());
1559}
1560void CXXNameMangler::mangleType(const IncompleteArrayType *T) {
Nick Lewycky271b6652010-09-05 03:40:33 +00001561 Out << "A_";
John McCallefe6aee2009-09-05 07:56:18 +00001562 mangleType(T->getElementType());
1563}
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001564
John McCallefe6aee2009-09-05 07:56:18 +00001565// <type> ::= <pointer-to-member-type>
1566// <pointer-to-member-type> ::= M <class type> <member type>
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001567void CXXNameMangler::mangleType(const MemberPointerType *T) {
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001568 Out << 'M';
1569 mangleType(QualType(T->getClass(), 0));
Anders Carlsson0e650012009-05-17 17:41:20 +00001570 QualType PointeeType = T->getPointeeType();
1571 if (const FunctionProtoType *FPT = dyn_cast<FunctionProtoType>(PointeeType)) {
John McCall0953e762009-09-24 19:53:00 +00001572 mangleQualifiers(Qualifiers::fromCVRMask(FPT->getTypeQuals()));
Douglas Gregor0a9a6d62011-01-26 17:36:28 +00001573 mangleRefQualifier(FPT->getRefQualifier());
Anders Carlsson0e650012009-05-17 17:41:20 +00001574 mangleType(FPT);
Anders Carlsson9d85b722010-06-02 04:29:50 +00001575
1576 // Itanium C++ ABI 5.1.8:
1577 //
1578 // The type of a non-static member function is considered to be different,
1579 // for the purposes of substitution, from the type of a namespace-scope or
1580 // static member function whose type appears similar. The types of two
1581 // non-static member functions are considered to be different, for the
1582 // purposes of substitution, if the functions are members of different
1583 // classes. In other words, for the purposes of substitution, the class of
1584 // which the function is a member is considered part of the type of
1585 // function.
1586
1587 // We increment the SeqID here to emulate adding an entry to the
1588 // substitution table. We can't actually add it because we don't want this
1589 // particular function type to be substituted.
1590 ++SeqID;
Mike Stump1eb44332009-09-09 15:08:12 +00001591 } else
Anders Carlsson0e650012009-05-17 17:41:20 +00001592 mangleType(PointeeType);
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001593}
1594
John McCallefe6aee2009-09-05 07:56:18 +00001595// <type> ::= <template-param>
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001596void CXXNameMangler::mangleType(const TemplateTypeParmType *T) {
Anders Carlsson0ccdf8d2009-09-27 00:38:53 +00001597 mangleTemplateParameter(T->getIndex());
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001598}
1599
Douglas Gregorc3069d62011-01-14 02:55:32 +00001600// <type> ::= <template-param>
1601void CXXNameMangler::mangleType(const SubstTemplateTypeParmPackType *T) {
1602 mangleTemplateParameter(T->getReplacedParameter()->getIndex());
1603}
1604
John McCallefe6aee2009-09-05 07:56:18 +00001605// <type> ::= P <type> # pointer-to
1606void CXXNameMangler::mangleType(const PointerType *T) {
1607 Out << 'P';
1608 mangleType(T->getPointeeType());
1609}
1610void CXXNameMangler::mangleType(const ObjCObjectPointerType *T) {
1611 Out << 'P';
1612 mangleType(T->getPointeeType());
1613}
1614
1615// <type> ::= R <type> # reference-to
1616void CXXNameMangler::mangleType(const LValueReferenceType *T) {
1617 Out << 'R';
1618 mangleType(T->getPointeeType());
1619}
1620
1621// <type> ::= O <type> # rvalue reference-to (C++0x)
1622void CXXNameMangler::mangleType(const RValueReferenceType *T) {
1623 Out << 'O';
1624 mangleType(T->getPointeeType());
1625}
1626
1627// <type> ::= C <type> # complex pair (C 2000)
1628void CXXNameMangler::mangleType(const ComplexType *T) {
1629 Out << 'C';
1630 mangleType(T->getElementType());
1631}
1632
Bob Wilsonc7df92d2010-11-12 17:24:43 +00001633// ARM's ABI for Neon vector types specifies that they should be mangled as
Bob Wilson57147a82010-11-16 00:32:18 +00001634// if they are structs (to match ARM's initial implementation). The
1635// vector type must be one of the special types predefined by ARM.
1636void CXXNameMangler::mangleNeonVectorType(const VectorType *T) {
Bob Wilsonc7df92d2010-11-12 17:24:43 +00001637 QualType EltType = T->getElementType();
Bob Wilson57147a82010-11-16 00:32:18 +00001638 assert(EltType->isBuiltinType() && "Neon vector element not a BuiltinType");
Bob Wilsonc7df92d2010-11-12 17:24:43 +00001639 const char *EltName = 0;
Bob Wilson491328c2010-11-12 17:24:46 +00001640 if (T->getVectorKind() == VectorType::NeonPolyVector) {
1641 switch (cast<BuiltinType>(EltType)->getKind()) {
Bob Wilson4cfaa5d2010-11-12 17:24:49 +00001642 case BuiltinType::SChar: EltName = "poly8_t"; break;
1643 case BuiltinType::Short: EltName = "poly16_t"; break;
Bob Wilson57147a82010-11-16 00:32:18 +00001644 default: llvm_unreachable("unexpected Neon polynomial vector element type");
Bob Wilson491328c2010-11-12 17:24:46 +00001645 }
1646 } else {
1647 switch (cast<BuiltinType>(EltType)->getKind()) {
Bob Wilson4cfaa5d2010-11-12 17:24:49 +00001648 case BuiltinType::SChar: EltName = "int8_t"; break;
1649 case BuiltinType::UChar: EltName = "uint8_t"; break;
1650 case BuiltinType::Short: EltName = "int16_t"; break;
1651 case BuiltinType::UShort: EltName = "uint16_t"; break;
1652 case BuiltinType::Int: EltName = "int32_t"; break;
1653 case BuiltinType::UInt: EltName = "uint32_t"; break;
1654 case BuiltinType::LongLong: EltName = "int64_t"; break;
1655 case BuiltinType::ULongLong: EltName = "uint64_t"; break;
1656 case BuiltinType::Float: EltName = "float32_t"; break;
Bob Wilson57147a82010-11-16 00:32:18 +00001657 default: llvm_unreachable("unexpected Neon vector element type");
Bob Wilson491328c2010-11-12 17:24:46 +00001658 }
Bob Wilsonc7df92d2010-11-12 17:24:43 +00001659 }
1660 const char *BaseName = 0;
Bob Wilson4cfaa5d2010-11-12 17:24:49 +00001661 unsigned BitSize = (T->getNumElements() *
Bob Wilson3a723022010-11-16 00:32:12 +00001662 getASTContext().getTypeSize(EltType));
Bob Wilsonc7df92d2010-11-12 17:24:43 +00001663 if (BitSize == 64)
1664 BaseName = "__simd64_";
Bob Wilson57147a82010-11-16 00:32:18 +00001665 else {
1666 assert(BitSize == 128 && "Neon vector type not 64 or 128 bits");
Bob Wilsonc7df92d2010-11-12 17:24:43 +00001667 BaseName = "__simd128_";
Bob Wilson57147a82010-11-16 00:32:18 +00001668 }
Bob Wilsonc7df92d2010-11-12 17:24:43 +00001669 Out << strlen(BaseName) + strlen(EltName);
1670 Out << BaseName << EltName;
Bob Wilsonc7df92d2010-11-12 17:24:43 +00001671}
1672
John McCallefe6aee2009-09-05 07:56:18 +00001673// GNU extension: vector types
Chris Lattner788b0fd2010-06-23 06:00:24 +00001674// <type> ::= <vector-type>
1675// <vector-type> ::= Dv <positive dimension number> _
1676// <extended element type>
1677// ::= Dv [<dimension expression>] _ <element type>
1678// <extended element type> ::= <element type>
1679// ::= p # AltiVec vector pixel
John McCallefe6aee2009-09-05 07:56:18 +00001680void CXXNameMangler::mangleType(const VectorType *T) {
Bob Wilson491328c2010-11-12 17:24:46 +00001681 if ((T->getVectorKind() == VectorType::NeonVector ||
Bob Wilson57147a82010-11-16 00:32:18 +00001682 T->getVectorKind() == VectorType::NeonPolyVector)) {
1683 mangleNeonVectorType(T);
Bob Wilsonc7df92d2010-11-12 17:24:43 +00001684 return;
Bob Wilson57147a82010-11-16 00:32:18 +00001685 }
Nick Lewycky0e5f0672010-03-26 07:18:04 +00001686 Out << "Dv" << T->getNumElements() << '_';
Bob Wilsone86d78c2010-11-10 21:56:12 +00001687 if (T->getVectorKind() == VectorType::AltiVecPixel)
Chris Lattner788b0fd2010-06-23 06:00:24 +00001688 Out << 'p';
Bob Wilsone86d78c2010-11-10 21:56:12 +00001689 else if (T->getVectorKind() == VectorType::AltiVecBool)
Chris Lattner788b0fd2010-06-23 06:00:24 +00001690 Out << 'b';
1691 else
1692 mangleType(T->getElementType());
John McCallefe6aee2009-09-05 07:56:18 +00001693}
1694void CXXNameMangler::mangleType(const ExtVectorType *T) {
1695 mangleType(static_cast<const VectorType*>(T));
1696}
1697void CXXNameMangler::mangleType(const DependentSizedExtVectorType *T) {
Nick Lewycky0e5f0672010-03-26 07:18:04 +00001698 Out << "Dv";
1699 mangleExpression(T->getSizeExpr());
1700 Out << '_';
John McCallefe6aee2009-09-05 07:56:18 +00001701 mangleType(T->getElementType());
1702}
1703
Douglas Gregor7536dd52010-12-20 02:24:11 +00001704void CXXNameMangler::mangleType(const PackExpansionType *T) {
Douglas Gregor4fc48662011-01-13 16:39:34 +00001705 // <type> ::= Dp <type> # pack expansion (C++0x)
Douglas Gregor255c2692011-01-13 17:44:36 +00001706 Out << "Dp";
Douglas Gregor7536dd52010-12-20 02:24:11 +00001707 mangleType(T->getPattern());
1708}
1709
Anders Carlssona40c5e42009-03-07 22:03:21 +00001710void CXXNameMangler::mangleType(const ObjCInterfaceType *T) {
1711 mangleSourceName(T->getDecl()->getIdentifier());
1712}
1713
John McCallc12c5bb2010-05-15 11:32:37 +00001714void CXXNameMangler::mangleType(const ObjCObjectType *T) {
John McCallc00c1f62010-05-15 17:06:29 +00001715 // We don't allow overloading by different protocol qualification,
1716 // so mangling them isn't necessary.
John McCallc12c5bb2010-05-15 11:32:37 +00001717 mangleType(T->getBaseType());
1718}
1719
John McCallefe6aee2009-09-05 07:56:18 +00001720void CXXNameMangler::mangleType(const BlockPointerType *T) {
Anders Carlssonf28c6872009-12-23 22:31:44 +00001721 Out << "U13block_pointer";
1722 mangleType(T->getPointeeType());
John McCallefe6aee2009-09-05 07:56:18 +00001723}
1724
John McCall31f17ec2010-04-27 00:57:59 +00001725void CXXNameMangler::mangleType(const InjectedClassNameType *T) {
1726 // Mangle injected class name types as if the user had written the
1727 // specialization out fully. It may not actually be possible to see
1728 // this mangling, though.
1729 mangleType(T->getInjectedSpecializationType());
1730}
1731
John McCallefe6aee2009-09-05 07:56:18 +00001732void CXXNameMangler::mangleType(const TemplateSpecializationType *T) {
Douglas Gregor1e9268e2010-04-28 05:58:56 +00001733 if (TemplateDecl *TD = T->getTemplateName().getAsTemplateDecl()) {
1734 mangleName(TD, T->getArgs(), T->getNumArgs());
1735 } else {
1736 if (mangleSubstitution(QualType(T, 0)))
1737 return;
Sean Huntc3021132010-05-05 15:23:54 +00001738
Douglas Gregor1e9268e2010-04-28 05:58:56 +00001739 mangleTemplatePrefix(T->getTemplateName());
Sean Huntc3021132010-05-05 15:23:54 +00001740
Douglas Gregor1e9268e2010-04-28 05:58:56 +00001741 // FIXME: GCC does not appear to mangle the template arguments when
1742 // the template in question is a dependent template name. Should we
1743 // emulate that badness?
1744 mangleTemplateArgs(T->getTemplateName(), T->getArgs(), T->getNumArgs());
1745 addSubstitution(QualType(T, 0));
1746 }
John McCallefe6aee2009-09-05 07:56:18 +00001747}
1748
Douglas Gregor4714c122010-03-31 17:34:00 +00001749void CXXNameMangler::mangleType(const DependentNameType *T) {
Anders Carlssonae352482009-09-26 02:26:02 +00001750 // Typename types are always nested
1751 Out << 'N';
John McCalla0ce15c2011-04-24 08:23:24 +00001752 manglePrefix(T->getQualifier());
John McCall33500952010-06-11 00:33:02 +00001753 mangleSourceName(T->getIdentifier());
1754 Out << 'E';
1755}
John McCall6ab30e02010-06-09 07:26:17 +00001756
John McCall33500952010-06-11 00:33:02 +00001757void CXXNameMangler::mangleType(const DependentTemplateSpecializationType *T) {
Douglas Gregoraa2187d2011-02-28 00:04:36 +00001758 // Dependently-scoped template types are nested if they have a prefix.
John McCall33500952010-06-11 00:33:02 +00001759 Out << 'N';
1760
1761 // TODO: avoid making this TemplateName.
1762 TemplateName Prefix =
1763 getASTContext().getDependentTemplateName(T->getQualifier(),
1764 T->getIdentifier());
1765 mangleTemplatePrefix(Prefix);
1766
1767 // FIXME: GCC does not appear to mangle the template arguments when
1768 // the template in question is a dependent template name. Should we
1769 // emulate that badness?
1770 mangleTemplateArgs(Prefix, T->getArgs(), T->getNumArgs());
Anders Carlssonae352482009-09-26 02:26:02 +00001771 Out << 'E';
John McCallefe6aee2009-09-05 07:56:18 +00001772}
1773
John McCallad5e7382010-03-01 23:49:17 +00001774void CXXNameMangler::mangleType(const TypeOfType *T) {
1775 // FIXME: this is pretty unsatisfactory, but there isn't an obvious
1776 // "extension with parameters" mangling.
1777 Out << "u6typeof";
1778}
1779
1780void CXXNameMangler::mangleType(const TypeOfExprType *T) {
1781 // FIXME: this is pretty unsatisfactory, but there isn't an obvious
1782 // "extension with parameters" mangling.
1783 Out << "u6typeof";
1784}
1785
1786void CXXNameMangler::mangleType(const DecltypeType *T) {
1787 Expr *E = T->getUnderlyingExpr();
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001788
John McCallad5e7382010-03-01 23:49:17 +00001789 // type ::= Dt <expression> E # decltype of an id-expression
1790 // # or class member access
1791 // ::= DT <expression> E # decltype of an expression
1792
1793 // This purports to be an exhaustive list of id-expressions and
1794 // class member accesses. Note that we do not ignore parentheses;
1795 // parentheses change the semantics of decltype for these
1796 // expressions (and cause the mangler to use the other form).
1797 if (isa<DeclRefExpr>(E) ||
1798 isa<MemberExpr>(E) ||
1799 isa<UnresolvedLookupExpr>(E) ||
1800 isa<DependentScopeDeclRefExpr>(E) ||
1801 isa<CXXDependentScopeMemberExpr>(E) ||
1802 isa<UnresolvedMemberExpr>(E))
1803 Out << "Dt";
1804 else
1805 Out << "DT";
1806 mangleExpression(E);
1807 Out << 'E';
1808}
1809
Richard Smith34b41d92011-02-20 03:19:35 +00001810void CXXNameMangler::mangleType(const AutoType *T) {
1811 QualType D = T->getDeducedType();
Richard Smith967ecd32011-02-21 20:10:02 +00001812 // <builtin-type> ::= Da # dependent auto
1813 if (D.isNull())
1814 Out << "Da";
1815 else
1816 mangleType(D);
Richard Smith34b41d92011-02-20 03:19:35 +00001817}
1818
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001819void CXXNameMangler::mangleIntegerLiteral(QualType T,
Anders Carlssone170ba72009-12-14 01:45:37 +00001820 const llvm::APSInt &Value) {
1821 // <expr-primary> ::= L <type> <value number> E # integer literal
1822 Out << 'L';
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001823
Anders Carlssone170ba72009-12-14 01:45:37 +00001824 mangleType(T);
1825 if (T->isBooleanType()) {
1826 // Boolean values are encoded as 0/1.
1827 Out << (Value.getBoolValue() ? '1' : '0');
1828 } else {
John McCall0512e482010-07-14 04:20:34 +00001829 mangleNumber(Value);
Anders Carlssone170ba72009-12-14 01:45:37 +00001830 }
1831 Out << 'E';
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001832
Anders Carlssone170ba72009-12-14 01:45:37 +00001833}
1834
John McCall2f27bf82010-02-04 02:56:29 +00001835/// Mangles a member expression. Implicit accesses are not handled,
1836/// but that should be okay, because you shouldn't be able to
1837/// make an implicit access in a function template declaration.
John McCalla0ce15c2011-04-24 08:23:24 +00001838void CXXNameMangler::mangleMemberExpr(const Expr *base,
1839 bool isArrow,
1840 NestedNameSpecifier *qualifier,
1841 NamedDecl *firstQualifierLookup,
1842 DeclarationName member,
1843 unsigned arity) {
1844 // <expression> ::= dt <expression> <unresolved-name>
1845 // ::= pt <expression> <unresolved-name>
1846 Out << (isArrow ? "pt" : "dt");
1847 mangleExpression(base);
1848 mangleUnresolvedName(qualifier, firstQualifierLookup, member, arity);
John McCall2f27bf82010-02-04 02:56:29 +00001849}
1850
John McCall5e1e89b2010-08-18 19:18:59 +00001851void CXXNameMangler::mangleExpression(const Expr *E, unsigned Arity) {
Anders Carlssond553f8c2009-09-21 01:21:10 +00001852 // <expression> ::= <unary operator-name> <expression>
John McCall09cc1412010-02-03 00:55:45 +00001853 // ::= <binary operator-name> <expression> <expression>
1854 // ::= <trinary operator-name> <expression> <expression> <expression>
Eli Friedmana7e68452010-08-22 01:00:03 +00001855 // ::= cl <expression>* E # call
Anders Carlssond553f8c2009-09-21 01:21:10 +00001856 // ::= cv <type> expression # conversion with one argument
1857 // ::= cv <type> _ <expression>* E # conversion with a different number of arguments
Eli Friedmana7e68452010-08-22 01:00:03 +00001858 // ::= st <type> # sizeof (a type)
Anders Carlssond553f8c2009-09-21 01:21:10 +00001859 // ::= at <type> # alignof (a type)
1860 // ::= <template-param>
1861 // ::= <function-param>
1862 // ::= sr <type> <unqualified-name> # dependent name
1863 // ::= sr <type> <unqualified-name> <template-args> # dependent template-id
1864 // ::= sZ <template-param> # size of a parameter pack
Douglas Gregor4fc48662011-01-13 16:39:34 +00001865 // ::= sZ <function-param> # size of a function parameter pack
John McCall09cc1412010-02-03 00:55:45 +00001866 // ::= <expr-primary>
John McCall1dd73832010-02-04 01:42:13 +00001867 // <expr-primary> ::= L <type> <value number> E # integer literal
1868 // ::= L <type <value float> E # floating literal
1869 // ::= L <mangled-name> E # external name
Anders Carlssond553f8c2009-09-21 01:21:10 +00001870 switch (E->getStmtClass()) {
John McCall6ae1f352010-04-09 22:26:14 +00001871 case Expr::NoStmtClass:
John McCall63c00d72011-02-09 08:16:59 +00001872#define ABSTRACT_STMT(Type)
John McCall6ae1f352010-04-09 22:26:14 +00001873#define EXPR(Type, Base)
1874#define STMT(Type, Base) \
1875 case Expr::Type##Class:
Sean Hunt4bfe1962010-05-05 15:24:00 +00001876#include "clang/AST/StmtNodes.inc"
John McCall0512e482010-07-14 04:20:34 +00001877 // fallthrough
1878
1879 // These all can only appear in local or variable-initialization
1880 // contexts and so should never appear in a mangling.
1881 case Expr::AddrLabelExprClass:
1882 case Expr::BlockDeclRefExprClass:
1883 case Expr::CXXThisExprClass:
1884 case Expr::DesignatedInitExprClass:
1885 case Expr::ImplicitValueInitExprClass:
1886 case Expr::InitListExprClass:
1887 case Expr::ParenListExprClass:
1888 case Expr::CXXScalarValueInitExprClass:
John McCall09cc1412010-02-03 00:55:45 +00001889 llvm_unreachable("unexpected statement kind");
1890 break;
1891
John McCall0512e482010-07-14 04:20:34 +00001892 // FIXME: invent manglings for all these.
1893 case Expr::BlockExprClass:
1894 case Expr::CXXPseudoDestructorExprClass:
1895 case Expr::ChooseExprClass:
1896 case Expr::CompoundLiteralExprClass:
1897 case Expr::ExtVectorElementExprClass:
Peter Collingbournef111d932011-04-15 00:35:48 +00001898 case Expr::GenericSelectionExprClass:
John McCall0512e482010-07-14 04:20:34 +00001899 case Expr::ObjCEncodeExprClass:
John McCall0512e482010-07-14 04:20:34 +00001900 case Expr::ObjCIsaExprClass:
1901 case Expr::ObjCIvarRefExprClass:
1902 case Expr::ObjCMessageExprClass:
1903 case Expr::ObjCPropertyRefExprClass:
1904 case Expr::ObjCProtocolExprClass:
1905 case Expr::ObjCSelectorExprClass:
1906 case Expr::ObjCStringLiteralClass:
John McCall0512e482010-07-14 04:20:34 +00001907 case Expr::OffsetOfExprClass:
1908 case Expr::PredefinedExprClass:
1909 case Expr::ShuffleVectorExprClass:
1910 case Expr::StmtExprClass:
John McCall0512e482010-07-14 04:20:34 +00001911 case Expr::UnaryTypeTraitExprClass:
Francois Pichet6ad6f282010-12-07 00:08:36 +00001912 case Expr::BinaryTypeTraitExprClass:
Francois Pichet9be88402010-09-08 23:47:05 +00001913 case Expr::VAArgExprClass:
Sebastian Redl2e156222010-09-10 20:55:43 +00001914 case Expr::CXXUuidofExprClass:
Peter Collingbournee08ce652011-02-09 21:07:24 +00001915 case Expr::CXXNoexceptExprClass:
1916 case Expr::CUDAKernelCallExprClass: {
John McCall6ae1f352010-04-09 22:26:14 +00001917 // As bad as this diagnostic is, it's better than crashing.
1918 Diagnostic &Diags = Context.getDiags();
1919 unsigned DiagID = Diags.getCustomDiagID(Diagnostic::Error,
1920 "cannot yet mangle expression type %0");
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +00001921 Diags.Report(E->getExprLoc(), DiagID)
John McCall739bf092010-04-10 09:39:25 +00001922 << E->getStmtClassName() << E->getSourceRange();
John McCall6ae1f352010-04-09 22:26:14 +00001923 break;
1924 }
1925
John McCall56ca35d2011-02-17 10:25:35 +00001926 // Even gcc-4.5 doesn't mangle this.
1927 case Expr::BinaryConditionalOperatorClass: {
1928 Diagnostic &Diags = Context.getDiags();
1929 unsigned DiagID =
1930 Diags.getCustomDiagID(Diagnostic::Error,
1931 "?: operator with omitted middle operand cannot be mangled");
1932 Diags.Report(E->getExprLoc(), DiagID)
1933 << E->getStmtClassName() << E->getSourceRange();
1934 break;
1935 }
1936
1937 // These are used for internal purposes and cannot be meaningfully mangled.
John McCall7cd7d1a2010-11-15 23:31:06 +00001938 case Expr::OpaqueValueExprClass:
1939 llvm_unreachable("cannot mangle opaque value; mangling wrong thing?");
1940
John McCall0512e482010-07-14 04:20:34 +00001941 case Expr::CXXDefaultArgExprClass:
John McCall5e1e89b2010-08-18 19:18:59 +00001942 mangleExpression(cast<CXXDefaultArgExpr>(E)->getExpr(), Arity);
John McCall0512e482010-07-14 04:20:34 +00001943 break;
1944
1945 case Expr::CXXMemberCallExprClass: // fallthrough
John McCall1dd73832010-02-04 01:42:13 +00001946 case Expr::CallExprClass: {
1947 const CallExpr *CE = cast<CallExpr>(E);
1948 Out << "cl";
John McCall5e1e89b2010-08-18 19:18:59 +00001949 mangleExpression(CE->getCallee(), CE->getNumArgs());
John McCall1dd73832010-02-04 01:42:13 +00001950 for (unsigned I = 0, N = CE->getNumArgs(); I != N; ++I)
1951 mangleExpression(CE->getArg(I));
Benjamin Kramer35f59b62010-04-10 16:03:31 +00001952 Out << 'E';
John McCall09cc1412010-02-03 00:55:45 +00001953 break;
John McCall1dd73832010-02-04 01:42:13 +00001954 }
John McCall09cc1412010-02-03 00:55:45 +00001955
John McCall0512e482010-07-14 04:20:34 +00001956 case Expr::CXXNewExprClass: {
1957 // Proposal from David Vandervoorde, 2010.06.30
1958 const CXXNewExpr *New = cast<CXXNewExpr>(E);
1959 if (New->isGlobalNew()) Out << "gs";
1960 Out << (New->isArray() ? "na" : "nw");
1961 for (CXXNewExpr::const_arg_iterator I = New->placement_arg_begin(),
1962 E = New->placement_arg_end(); I != E; ++I)
1963 mangleExpression(*I);
1964 Out << '_';
1965 mangleType(New->getAllocatedType());
1966 if (New->hasInitializer()) {
1967 Out << "pi";
1968 for (CXXNewExpr::const_arg_iterator I = New->constructor_arg_begin(),
1969 E = New->constructor_arg_end(); I != E; ++I)
1970 mangleExpression(*I);
1971 }
1972 Out << 'E';
1973 break;
1974 }
1975
John McCall2f27bf82010-02-04 02:56:29 +00001976 case Expr::MemberExprClass: {
1977 const MemberExpr *ME = cast<MemberExpr>(E);
1978 mangleMemberExpr(ME->getBase(), ME->isArrow(),
John McCalla0ce15c2011-04-24 08:23:24 +00001979 ME->getQualifier(), 0, ME->getMemberDecl()->getDeclName(),
John McCall5e1e89b2010-08-18 19:18:59 +00001980 Arity);
John McCall2f27bf82010-02-04 02:56:29 +00001981 break;
1982 }
1983
1984 case Expr::UnresolvedMemberExprClass: {
1985 const UnresolvedMemberExpr *ME = cast<UnresolvedMemberExpr>(E);
1986 mangleMemberExpr(ME->getBase(), ME->isArrow(),
John McCalla0ce15c2011-04-24 08:23:24 +00001987 ME->getQualifier(), 0, ME->getMemberName(),
John McCall5e1e89b2010-08-18 19:18:59 +00001988 Arity);
John McCall6dbce192010-08-20 00:17:19 +00001989 if (ME->hasExplicitTemplateArgs())
1990 mangleTemplateArgs(ME->getExplicitTemplateArgs());
John McCall2f27bf82010-02-04 02:56:29 +00001991 break;
1992 }
1993
1994 case Expr::CXXDependentScopeMemberExprClass: {
1995 const CXXDependentScopeMemberExpr *ME
1996 = cast<CXXDependentScopeMemberExpr>(E);
1997 mangleMemberExpr(ME->getBase(), ME->isArrow(),
John McCalla0ce15c2011-04-24 08:23:24 +00001998 ME->getQualifier(), ME->getFirstQualifierFoundInScope(),
1999 ME->getMember(), Arity);
John McCall6dbce192010-08-20 00:17:19 +00002000 if (ME->hasExplicitTemplateArgs())
2001 mangleTemplateArgs(ME->getExplicitTemplateArgs());
John McCall2f27bf82010-02-04 02:56:29 +00002002 break;
2003 }
2004
John McCall1dd73832010-02-04 01:42:13 +00002005 case Expr::UnresolvedLookupExprClass: {
2006 const UnresolvedLookupExpr *ULE = cast<UnresolvedLookupExpr>(E);
John McCalla0ce15c2011-04-24 08:23:24 +00002007 mangleUnresolvedName(ULE->getQualifier(), 0, ULE->getName(), Arity);
John McCall6dbce192010-08-20 00:17:19 +00002008 if (ULE->hasExplicitTemplateArgs())
2009 mangleTemplateArgs(ULE->getExplicitTemplateArgs());
John McCall1dd73832010-02-04 01:42:13 +00002010 break;
2011 }
2012
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002013 case Expr::CXXUnresolvedConstructExprClass: {
John McCall1dd73832010-02-04 01:42:13 +00002014 const CXXUnresolvedConstructExpr *CE = cast<CXXUnresolvedConstructExpr>(E);
2015 unsigned N = CE->arg_size();
2016
2017 Out << "cv";
2018 mangleType(CE->getType());
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002019 if (N != 1) Out << '_';
John McCall1dd73832010-02-04 01:42:13 +00002020 for (unsigned I = 0; I != N; ++I) mangleExpression(CE->getArg(I));
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002021 if (N != 1) Out << 'E';
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002022 break;
John McCall1dd73832010-02-04 01:42:13 +00002023 }
John McCall09cc1412010-02-03 00:55:45 +00002024
John McCall1dd73832010-02-04 01:42:13 +00002025 case Expr::CXXTemporaryObjectExprClass:
2026 case Expr::CXXConstructExprClass: {
2027 const CXXConstructExpr *CE = cast<CXXConstructExpr>(E);
2028 unsigned N = CE->getNumArgs();
2029
2030 Out << "cv";
2031 mangleType(CE->getType());
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002032 if (N != 1) Out << '_';
John McCall1dd73832010-02-04 01:42:13 +00002033 for (unsigned I = 0; I != N; ++I) mangleExpression(CE->getArg(I));
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002034 if (N != 1) Out << 'E';
John McCall09cc1412010-02-03 00:55:45 +00002035 break;
John McCall1dd73832010-02-04 01:42:13 +00002036 }
2037
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +00002038 case Expr::UnaryExprOrTypeTraitExprClass: {
2039 const UnaryExprOrTypeTraitExpr *SAE = cast<UnaryExprOrTypeTraitExpr>(E);
2040 switch(SAE->getKind()) {
2041 case UETT_SizeOf:
2042 Out << 's';
2043 break;
2044 case UETT_AlignOf:
2045 Out << 'a';
2046 break;
2047 case UETT_VecStep:
2048 Diagnostic &Diags = Context.getDiags();
2049 unsigned DiagID = Diags.getCustomDiagID(Diagnostic::Error,
2050 "cannot yet mangle vec_step expression");
2051 Diags.Report(DiagID);
2052 return;
2053 }
John McCall1dd73832010-02-04 01:42:13 +00002054 if (SAE->isArgumentType()) {
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002055 Out << 't';
John McCall1dd73832010-02-04 01:42:13 +00002056 mangleType(SAE->getArgumentType());
2057 } else {
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002058 Out << 'z';
John McCall1dd73832010-02-04 01:42:13 +00002059 mangleExpression(SAE->getArgumentExpr());
2060 }
2061 break;
2062 }
Anders Carlssona7694082009-11-06 02:50:19 +00002063
John McCall0512e482010-07-14 04:20:34 +00002064 case Expr::CXXThrowExprClass: {
2065 const CXXThrowExpr *TE = cast<CXXThrowExpr>(E);
2066
2067 // Proposal from David Vandervoorde, 2010.06.30
2068 if (TE->getSubExpr()) {
2069 Out << "tw";
2070 mangleExpression(TE->getSubExpr());
2071 } else {
2072 Out << "tr";
2073 }
2074 break;
2075 }
2076
2077 case Expr::CXXTypeidExprClass: {
2078 const CXXTypeidExpr *TIE = cast<CXXTypeidExpr>(E);
2079
2080 // Proposal from David Vandervoorde, 2010.06.30
2081 if (TIE->isTypeOperand()) {
2082 Out << "ti";
2083 mangleType(TIE->getTypeOperand());
2084 } else {
2085 Out << "te";
2086 mangleExpression(TIE->getExprOperand());
2087 }
2088 break;
2089 }
2090
2091 case Expr::CXXDeleteExprClass: {
2092 const CXXDeleteExpr *DE = cast<CXXDeleteExpr>(E);
2093
2094 // Proposal from David Vandervoorde, 2010.06.30
2095 if (DE->isGlobalDelete()) Out << "gs";
2096 Out << (DE->isArrayForm() ? "da" : "dl");
2097 mangleExpression(DE->getArgument());
2098 break;
2099 }
2100
Anders Carlssone170ba72009-12-14 01:45:37 +00002101 case Expr::UnaryOperatorClass: {
2102 const UnaryOperator *UO = cast<UnaryOperator>(E);
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002103 mangleOperatorName(UnaryOperator::getOverloadedOperator(UO->getOpcode()),
Anders Carlssone170ba72009-12-14 01:45:37 +00002104 /*Arity=*/1);
2105 mangleExpression(UO->getSubExpr());
2106 break;
2107 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002108
John McCall0512e482010-07-14 04:20:34 +00002109 case Expr::ArraySubscriptExprClass: {
2110 const ArraySubscriptExpr *AE = cast<ArraySubscriptExpr>(E);
2111
Chris Lattnerfc8f0e12011-04-15 05:22:18 +00002112 // Array subscript is treated as a syntactically weird form of
John McCall0512e482010-07-14 04:20:34 +00002113 // binary operator.
2114 Out << "ix";
2115 mangleExpression(AE->getLHS());
2116 mangleExpression(AE->getRHS());
2117 break;
2118 }
2119
2120 case Expr::CompoundAssignOperatorClass: // fallthrough
Anders Carlssone170ba72009-12-14 01:45:37 +00002121 case Expr::BinaryOperatorClass: {
2122 const BinaryOperator *BO = cast<BinaryOperator>(E);
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002123 mangleOperatorName(BinaryOperator::getOverloadedOperator(BO->getOpcode()),
Anders Carlssone170ba72009-12-14 01:45:37 +00002124 /*Arity=*/2);
2125 mangleExpression(BO->getLHS());
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002126 mangleExpression(BO->getRHS());
Anders Carlssone170ba72009-12-14 01:45:37 +00002127 break;
John McCall2f27bf82010-02-04 02:56:29 +00002128 }
Anders Carlssone170ba72009-12-14 01:45:37 +00002129
2130 case Expr::ConditionalOperatorClass: {
2131 const ConditionalOperator *CO = cast<ConditionalOperator>(E);
2132 mangleOperatorName(OO_Conditional, /*Arity=*/3);
2133 mangleExpression(CO->getCond());
John McCall5e1e89b2010-08-18 19:18:59 +00002134 mangleExpression(CO->getLHS(), Arity);
2135 mangleExpression(CO->getRHS(), Arity);
Anders Carlssone170ba72009-12-14 01:45:37 +00002136 break;
2137 }
2138
Douglas Gregor46287c72010-01-29 16:37:09 +00002139 case Expr::ImplicitCastExprClass: {
John McCall5e1e89b2010-08-18 19:18:59 +00002140 mangleExpression(cast<ImplicitCastExpr>(E)->getSubExpr(), Arity);
Douglas Gregor46287c72010-01-29 16:37:09 +00002141 break;
2142 }
2143
2144 case Expr::CStyleCastExprClass:
2145 case Expr::CXXStaticCastExprClass:
2146 case Expr::CXXDynamicCastExprClass:
2147 case Expr::CXXReinterpretCastExprClass:
2148 case Expr::CXXConstCastExprClass:
2149 case Expr::CXXFunctionalCastExprClass: {
2150 const ExplicitCastExpr *ECE = cast<ExplicitCastExpr>(E);
2151 Out << "cv";
2152 mangleType(ECE->getType());
2153 mangleExpression(ECE->getSubExpr());
2154 break;
2155 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002156
Anders Carlsson58040a52009-12-16 05:48:46 +00002157 case Expr::CXXOperatorCallExprClass: {
2158 const CXXOperatorCallExpr *CE = cast<CXXOperatorCallExpr>(E);
2159 unsigned NumArgs = CE->getNumArgs();
2160 mangleOperatorName(CE->getOperator(), /*Arity=*/NumArgs);
2161 // Mangle the arguments.
2162 for (unsigned i = 0; i != NumArgs; ++i)
2163 mangleExpression(CE->getArg(i));
2164 break;
2165 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002166
Anders Carlssona7694082009-11-06 02:50:19 +00002167 case Expr::ParenExprClass:
John McCall5e1e89b2010-08-18 19:18:59 +00002168 mangleExpression(cast<ParenExpr>(E)->getSubExpr(), Arity);
Anders Carlssona7694082009-11-06 02:50:19 +00002169 break;
2170
Anders Carlssond553f8c2009-09-21 01:21:10 +00002171 case Expr::DeclRefExprClass: {
Douglas Gregor5ed1bc32010-02-28 21:40:32 +00002172 const NamedDecl *D = cast<DeclRefExpr>(E)->getDecl();
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002173
Anders Carlssond553f8c2009-09-21 01:21:10 +00002174 switch (D->getKind()) {
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002175 default:
Douglas Gregor5ed1bc32010-02-28 21:40:32 +00002176 // <expr-primary> ::= L <mangled-name> E # external name
2177 Out << 'L';
2178 mangle(D, "_Z");
2179 Out << 'E';
2180 break;
2181
John McCall3dc7e7b2010-07-24 01:17:35 +00002182 case Decl::EnumConstant: {
2183 const EnumConstantDecl *ED = cast<EnumConstantDecl>(D);
2184 mangleIntegerLiteral(ED->getType(), ED->getInitVal());
2185 break;
2186 }
2187
Anders Carlssond553f8c2009-09-21 01:21:10 +00002188 case Decl::NonTypeTemplateParm: {
2189 const NonTypeTemplateParmDecl *PD = cast<NonTypeTemplateParmDecl>(D);
Anders Carlsson0ccdf8d2009-09-27 00:38:53 +00002190 mangleTemplateParameter(PD->getIndex());
Anders Carlssond553f8c2009-09-21 01:21:10 +00002191 break;
2192 }
2193
2194 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002195
Anders Carlsson50755b02009-09-27 20:11:34 +00002196 break;
Anders Carlssond553f8c2009-09-21 01:21:10 +00002197 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002198
Douglas Gregorc7793c72011-01-15 01:15:58 +00002199 case Expr::SubstNonTypeTemplateParmPackExprClass:
2200 mangleTemplateParameter(
2201 cast<SubstNonTypeTemplateParmPackExpr>(E)->getParameterPack()->getIndex());
2202 break;
2203
John McCall865d4472009-11-19 22:55:06 +00002204 case Expr::DependentScopeDeclRefExprClass: {
2205 const DependentScopeDeclRefExpr *DRE = cast<DependentScopeDeclRefExpr>(E);
Douglas Gregor4b2ccfc2010-02-28 22:05:49 +00002206 NestedNameSpecifier *NNS = DRE->getQualifier();
2207 const Type *QTy = NNS->getAsType();
2208
2209 // When we're dealing with a nested-name-specifier that has just a
2210 // dependent identifier in it, mangle that as a typename. FIXME:
2211 // It isn't clear that we ever actually want to have such a
2212 // nested-name-specifier; why not just represent it as a typename type?
2213 if (!QTy && NNS->getAsIdentifier() && NNS->getPrefix()) {
Douglas Gregor4a2023f2010-03-31 20:19:30 +00002214 QTy = getASTContext().getDependentNameType(ETK_Typename,
2215 NNS->getPrefix(),
2216 NNS->getAsIdentifier())
Douglas Gregor4b2ccfc2010-02-28 22:05:49 +00002217 .getTypePtr();
2218 }
Anders Carlsson50755b02009-09-27 20:11:34 +00002219 assert(QTy && "Qualifier was not type!");
2220
John McCall6dbce192010-08-20 00:17:19 +00002221 // ::= sr <type> <unqualified-name> # dependent name
2222 // ::= sr <type> <unqualified-name> <template-args> # dependent template-id
Anders Carlsson50755b02009-09-27 20:11:34 +00002223 Out << "sr";
2224 mangleType(QualType(QTy, 0));
John McCall5e1e89b2010-08-18 19:18:59 +00002225 mangleUnqualifiedName(0, DRE->getDeclName(), Arity);
John McCall6dbce192010-08-20 00:17:19 +00002226 if (DRE->hasExplicitTemplateArgs())
2227 mangleTemplateArgs(DRE->getExplicitTemplateArgs());
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002228
Anders Carlsson50755b02009-09-27 20:11:34 +00002229 break;
2230 }
2231
John McCalld9307602010-04-09 22:54:09 +00002232 case Expr::CXXBindTemporaryExprClass:
2233 mangleExpression(cast<CXXBindTemporaryExpr>(E)->getSubExpr());
2234 break;
2235
John McCall4765fa02010-12-06 08:20:24 +00002236 case Expr::ExprWithCleanupsClass:
2237 mangleExpression(cast<ExprWithCleanups>(E)->getSubExpr(), Arity);
John McCalld9307602010-04-09 22:54:09 +00002238 break;
2239
John McCall1dd73832010-02-04 01:42:13 +00002240 case Expr::FloatingLiteralClass: {
2241 const FloatingLiteral *FL = cast<FloatingLiteral>(E);
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002242 Out << 'L';
John McCall1dd73832010-02-04 01:42:13 +00002243 mangleType(FL->getType());
John McCall0512e482010-07-14 04:20:34 +00002244 mangleFloat(FL->getValue());
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002245 Out << 'E';
John McCall1dd73832010-02-04 01:42:13 +00002246 break;
2247 }
2248
John McCallde810632010-04-09 21:48:08 +00002249 case Expr::CharacterLiteralClass:
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002250 Out << 'L';
John McCallde810632010-04-09 21:48:08 +00002251 mangleType(E->getType());
2252 Out << cast<CharacterLiteral>(E)->getValue();
2253 Out << 'E';
2254 break;
2255
2256 case Expr::CXXBoolLiteralExprClass:
2257 Out << "Lb";
2258 Out << (cast<CXXBoolLiteralExpr>(E)->getValue() ? '1' : '0');
2259 Out << 'E';
2260 break;
2261
John McCall0512e482010-07-14 04:20:34 +00002262 case Expr::IntegerLiteralClass: {
2263 llvm::APSInt Value(cast<IntegerLiteral>(E)->getValue());
2264 if (E->getType()->isSignedIntegerType())
2265 Value.setIsSigned(true);
2266 mangleIntegerLiteral(E->getType(), Value);
Anders Carlssone170ba72009-12-14 01:45:37 +00002267 break;
John McCall0512e482010-07-14 04:20:34 +00002268 }
2269
2270 case Expr::ImaginaryLiteralClass: {
2271 const ImaginaryLiteral *IE = cast<ImaginaryLiteral>(E);
2272 // Mangle as if a complex literal.
Nick Lewycky271b6652010-09-05 03:40:33 +00002273 // Proposal from David Vandevoorde, 2010.06.30.
John McCall0512e482010-07-14 04:20:34 +00002274 Out << 'L';
2275 mangleType(E->getType());
2276 if (const FloatingLiteral *Imag =
2277 dyn_cast<FloatingLiteral>(IE->getSubExpr())) {
2278 // Mangle a floating-point zero of the appropriate type.
2279 mangleFloat(llvm::APFloat(Imag->getValue().getSemantics()));
2280 Out << '_';
2281 mangleFloat(Imag->getValue());
2282 } else {
Nick Lewycky271b6652010-09-05 03:40:33 +00002283 Out << "0_";
John McCall0512e482010-07-14 04:20:34 +00002284 llvm::APSInt Value(cast<IntegerLiteral>(IE->getSubExpr())->getValue());
2285 if (IE->getSubExpr()->getType()->isSignedIntegerType())
2286 Value.setIsSigned(true);
2287 mangleNumber(Value);
2288 }
2289 Out << 'E';
2290 break;
2291 }
2292
2293 case Expr::StringLiteralClass: {
John McCall1658c392010-07-15 21:53:03 +00002294 // Revised proposal from David Vandervoorde, 2010.07.15.
John McCall0512e482010-07-14 04:20:34 +00002295 Out << 'L';
John McCall1658c392010-07-15 21:53:03 +00002296 assert(isa<ConstantArrayType>(E->getType()));
2297 mangleType(E->getType());
John McCall0512e482010-07-14 04:20:34 +00002298 Out << 'E';
2299 break;
2300 }
2301
2302 case Expr::GNUNullExprClass:
2303 // FIXME: should this really be mangled the same as nullptr?
2304 // fallthrough
2305
2306 case Expr::CXXNullPtrLiteralExprClass: {
2307 // Proposal from David Vandervoorde, 2010.06.30, as
2308 // modified by ABI list discussion.
2309 Out << "LDnE";
2310 break;
2311 }
Douglas Gregorbe230c32011-01-03 17:17:50 +00002312
2313 case Expr::PackExpansionExprClass:
2314 Out << "sp";
2315 mangleExpression(cast<PackExpansionExpr>(E)->getPattern());
2316 break;
Douglas Gregor2e774c42011-01-04 18:56:13 +00002317
2318 case Expr::SizeOfPackExprClass: {
Douglas Gregor2e774c42011-01-04 18:56:13 +00002319 Out << "sZ";
2320 const NamedDecl *Pack = cast<SizeOfPackExpr>(E)->getPack();
2321 if (const TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(Pack))
2322 mangleTemplateParameter(TTP->getIndex());
2323 else if (const NonTypeTemplateParmDecl *NTTP
2324 = dyn_cast<NonTypeTemplateParmDecl>(Pack))
2325 mangleTemplateParameter(NTTP->getIndex());
2326 else if (const TemplateTemplateParmDecl *TempTP
2327 = dyn_cast<TemplateTemplateParmDecl>(Pack))
2328 mangleTemplateParameter(TempTP->getIndex());
2329 else {
Douglas Gregor4fc48662011-01-13 16:39:34 +00002330 // Note: proposed by Mike Herrick on 11/30/10
2331 // <expression> ::= sZ <function-param> # size of function parameter pack
Douglas Gregor2e774c42011-01-04 18:56:13 +00002332 Diagnostic &Diags = Context.getDiags();
2333 unsigned DiagID = Diags.getCustomDiagID(Diagnostic::Error,
2334 "cannot mangle sizeof...(function parameter pack)");
2335 Diags.Report(DiagID);
2336 return;
2337 }
Douglas Gregordfbbcf92011-03-03 02:20:19 +00002338 break;
Douglas Gregor2e774c42011-01-04 18:56:13 +00002339 }
Anders Carlssond553f8c2009-09-21 01:21:10 +00002340 }
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00002341}
2342
Anders Carlsson3ac86b52009-04-15 05:36:58 +00002343void CXXNameMangler::mangleCXXCtorType(CXXCtorType T) {
2344 // <ctor-dtor-name> ::= C1 # complete object constructor
2345 // ::= C2 # base object constructor
2346 // ::= C3 # complete object allocating constructor
2347 //
2348 switch (T) {
2349 case Ctor_Complete:
2350 Out << "C1";
2351 break;
2352 case Ctor_Base:
2353 Out << "C2";
2354 break;
2355 case Ctor_CompleteAllocating:
2356 Out << "C3";
2357 break;
2358 }
2359}
2360
Anders Carlsson27ae5362009-04-17 01:58:57 +00002361void CXXNameMangler::mangleCXXDtorType(CXXDtorType T) {
2362 // <ctor-dtor-name> ::= D0 # deleting destructor
2363 // ::= D1 # complete object destructor
2364 // ::= D2 # base object destructor
2365 //
2366 switch (T) {
2367 case Dtor_Deleting:
2368 Out << "D0";
2369 break;
2370 case Dtor_Complete:
2371 Out << "D1";
2372 break;
2373 case Dtor_Base:
2374 Out << "D2";
2375 break;
2376 }
2377}
2378
John McCall6dbce192010-08-20 00:17:19 +00002379void CXXNameMangler::mangleTemplateArgs(
2380 const ExplicitTemplateArgumentList &TemplateArgs) {
2381 // <template-args> ::= I <template-arg>+ E
2382 Out << 'I';
2383 for (unsigned I = 0, E = TemplateArgs.NumTemplateArgs; I != E; ++I)
2384 mangleTemplateArg(0, TemplateArgs.getTemplateArgs()[I].getArgument());
2385 Out << 'E';
2386}
2387
Douglas Gregor20f0cc72010-04-23 03:10:43 +00002388void CXXNameMangler::mangleTemplateArgs(TemplateName Template,
2389 const TemplateArgument *TemplateArgs,
2390 unsigned NumTemplateArgs) {
2391 if (TemplateDecl *TD = Template.getAsTemplateDecl())
2392 return mangleTemplateArgs(*TD->getTemplateParameters(), TemplateArgs,
2393 NumTemplateArgs);
Sean Huntc3021132010-05-05 15:23:54 +00002394
Douglas Gregor20f0cc72010-04-23 03:10:43 +00002395 // <template-args> ::= I <template-arg>+ E
2396 Out << 'I';
2397 for (unsigned i = 0; i != NumTemplateArgs; ++i)
2398 mangleTemplateArg(0, TemplateArgs[i]);
2399 Out << 'E';
2400}
2401
Rafael Espindolad9800722010-03-11 14:07:00 +00002402void CXXNameMangler::mangleTemplateArgs(const TemplateParameterList &PL,
2403 const TemplateArgumentList &AL) {
Anders Carlsson7a0ba872009-05-15 16:09:15 +00002404 // <template-args> ::= I <template-arg>+ E
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002405 Out << 'I';
Rafael Espindolad9800722010-03-11 14:07:00 +00002406 for (unsigned i = 0, e = AL.size(); i != e; ++i)
2407 mangleTemplateArg(PL.getParam(i), AL[i]);
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002408 Out << 'E';
Anders Carlsson7a0ba872009-05-15 16:09:15 +00002409}
2410
Rafael Espindolad9800722010-03-11 14:07:00 +00002411void CXXNameMangler::mangleTemplateArgs(const TemplateParameterList &PL,
2412 const TemplateArgument *TemplateArgs,
Anders Carlsson7624f212009-09-18 02:42:01 +00002413 unsigned NumTemplateArgs) {
2414 // <template-args> ::= I <template-arg>+ E
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002415 Out << 'I';
Daniel Dunbar7e0c1952009-11-21 09:17:15 +00002416 for (unsigned i = 0; i != NumTemplateArgs; ++i)
Rafael Espindolad9800722010-03-11 14:07:00 +00002417 mangleTemplateArg(PL.getParam(i), TemplateArgs[i]);
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002418 Out << 'E';
Anders Carlsson7624f212009-09-18 02:42:01 +00002419}
2420
Rafael Espindolad9800722010-03-11 14:07:00 +00002421void CXXNameMangler::mangleTemplateArg(const NamedDecl *P,
2422 const TemplateArgument &A) {
Mike Stump1eb44332009-09-09 15:08:12 +00002423 // <template-arg> ::= <type> # type or template
Anders Carlsson7a0ba872009-05-15 16:09:15 +00002424 // ::= X <expression> E # expression
2425 // ::= <expr-primary> # simple expressions
Douglas Gregor4fc48662011-01-13 16:39:34 +00002426 // ::= J <template-arg>* E # argument pack
Anders Carlsson7a0ba872009-05-15 16:09:15 +00002427 // ::= sp <expression> # pack expansion of (C++0x)
2428 switch (A.getKind()) {
Douglas Gregorf90b27a2011-01-03 22:36:02 +00002429 case TemplateArgument::Null:
2430 llvm_unreachable("Cannot mangle NULL template argument");
2431
Anders Carlsson7a0ba872009-05-15 16:09:15 +00002432 case TemplateArgument::Type:
2433 mangleType(A.getAsType());
2434 break;
Anders Carlsson9e85c742009-12-23 19:30:55 +00002435 case TemplateArgument::Template:
John McCallb6f532e2010-07-14 06:43:17 +00002436 // This is mangled as <type>.
2437 mangleType(A.getAsTemplate());
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002438 break;
Douglas Gregora7fc9012011-01-05 18:58:31 +00002439 case TemplateArgument::TemplateExpansion:
Douglas Gregor4fc48662011-01-13 16:39:34 +00002440 // <type> ::= Dp <type> # pack expansion (C++0x)
Douglas Gregora7fc9012011-01-05 18:58:31 +00002441 Out << "Dp";
2442 mangleType(A.getAsTemplateOrTemplatePattern());
2443 break;
Anders Carlssond553f8c2009-09-21 01:21:10 +00002444 case TemplateArgument::Expression:
2445 Out << 'X';
2446 mangleExpression(A.getAsExpr());
2447 Out << 'E';
2448 break;
Anders Carlssone170ba72009-12-14 01:45:37 +00002449 case TemplateArgument::Integral:
2450 mangleIntegerLiteral(A.getIntegralType(), *A.getAsIntegral());
Anders Carlsson7a0ba872009-05-15 16:09:15 +00002451 break;
Daniel Dunbar7e0c1952009-11-21 09:17:15 +00002452 case TemplateArgument::Declaration: {
Douglas Gregor20f0cc72010-04-23 03:10:43 +00002453 assert(P && "Missing template parameter for declaration argument");
Daniel Dunbar7e0c1952009-11-21 09:17:15 +00002454 // <expr-primary> ::= L <mangled-name> E # external name
2455
Rafael Espindolad9800722010-03-11 14:07:00 +00002456 // Clang produces AST's where pointer-to-member-function expressions
Daniel Dunbar7e0c1952009-11-21 09:17:15 +00002457 // and pointer-to-function expressions are represented as a declaration not
Rafael Espindolad9800722010-03-11 14:07:00 +00002458 // an expression. We compensate for it here to produce the correct mangling.
2459 NamedDecl *D = cast<NamedDecl>(A.getAsDecl());
2460 const NonTypeTemplateParmDecl *Parameter = cast<NonTypeTemplateParmDecl>(P);
John McCallc0a45592011-04-24 08:43:07 +00002461 bool compensateMangling = !Parameter->getType()->isReferenceType();
Rafael Espindolad9800722010-03-11 14:07:00 +00002462 if (compensateMangling) {
2463 Out << 'X';
2464 mangleOperatorName(OO_Amp, 1);
2465 }
2466
Daniel Dunbar7e0c1952009-11-21 09:17:15 +00002467 Out << 'L';
2468 // References to external entities use the mangled name; if the name would
2469 // not normally be manged then mangle it as unqualified.
2470 //
2471 // FIXME: The ABI specifies that external names here should have _Z, but
2472 // gcc leaves this off.
Rafael Espindolad9800722010-03-11 14:07:00 +00002473 if (compensateMangling)
2474 mangle(D, "_Z");
2475 else
2476 mangle(D, "Z");
Daniel Dunbar7e0c1952009-11-21 09:17:15 +00002477 Out << 'E';
Rafael Espindolad9800722010-03-11 14:07:00 +00002478
2479 if (compensateMangling)
2480 Out << 'E';
2481
Daniel Dunbar7e0c1952009-11-21 09:17:15 +00002482 break;
2483 }
Douglas Gregorf90b27a2011-01-03 22:36:02 +00002484
2485 case TemplateArgument::Pack: {
2486 // Note: proposal by Mike Herrick on 12/20/10
2487 Out << 'J';
2488 for (TemplateArgument::pack_iterator PA = A.pack_begin(),
2489 PAEnd = A.pack_end();
2490 PA != PAEnd; ++PA)
2491 mangleTemplateArg(P, *PA);
2492 Out << 'E';
2493 }
Daniel Dunbar7e0c1952009-11-21 09:17:15 +00002494 }
Anders Carlsson7a0ba872009-05-15 16:09:15 +00002495}
2496
Anders Carlsson0ccdf8d2009-09-27 00:38:53 +00002497void CXXNameMangler::mangleTemplateParameter(unsigned Index) {
2498 // <template-param> ::= T_ # first template parameter
2499 // ::= T <parameter-2 non-negative number> _
2500 if (Index == 0)
2501 Out << "T_";
2502 else
2503 Out << 'T' << (Index - 1) << '_';
2504}
2505
Anders Carlsson76967372009-09-17 00:43:46 +00002506// <substitution> ::= S <seq-id> _
2507// ::= S_
Anders Carlsson6862fc72009-09-17 04:16:28 +00002508bool CXXNameMangler::mangleSubstitution(const NamedDecl *ND) {
Anders Carlssone7c8cb62009-09-26 20:53:44 +00002509 // Try one of the standard substitutions first.
2510 if (mangleStandardSubstitution(ND))
2511 return true;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002512
Anders Carlsson433d1372009-11-07 04:26:04 +00002513 ND = cast<NamedDecl>(ND->getCanonicalDecl());
Anders Carlsson6862fc72009-09-17 04:16:28 +00002514 return mangleSubstitution(reinterpret_cast<uintptr_t>(ND));
2515}
2516
Anders Carlsson76967372009-09-17 00:43:46 +00002517bool CXXNameMangler::mangleSubstitution(QualType T) {
Anders Carlssond99edc42009-09-26 03:55:37 +00002518 if (!T.getCVRQualifiers()) {
2519 if (const RecordType *RT = T->getAs<RecordType>())
2520 return mangleSubstitution(RT->getDecl());
2521 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002522
Anders Carlsson76967372009-09-17 00:43:46 +00002523 uintptr_t TypePtr = reinterpret_cast<uintptr_t>(T.getAsOpaquePtr());
2524
Anders Carlssond3a932a2009-09-17 03:53:28 +00002525 return mangleSubstitution(TypePtr);
2526}
2527
Douglas Gregor1e9268e2010-04-28 05:58:56 +00002528bool CXXNameMangler::mangleSubstitution(TemplateName Template) {
2529 if (TemplateDecl *TD = Template.getAsTemplateDecl())
2530 return mangleSubstitution(TD);
Sean Huntc3021132010-05-05 15:23:54 +00002531
Douglas Gregor1e9268e2010-04-28 05:58:56 +00002532 Template = Context.getASTContext().getCanonicalTemplateName(Template);
2533 return mangleSubstitution(
2534 reinterpret_cast<uintptr_t>(Template.getAsVoidPointer()));
2535}
2536
Anders Carlssond3a932a2009-09-17 03:53:28 +00002537bool CXXNameMangler::mangleSubstitution(uintptr_t Ptr) {
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002538 llvm::DenseMap<uintptr_t, unsigned>::iterator I = Substitutions.find(Ptr);
Anders Carlsson76967372009-09-17 00:43:46 +00002539 if (I == Substitutions.end())
2540 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002541
Anders Carlsson76967372009-09-17 00:43:46 +00002542 unsigned SeqID = I->second;
2543 if (SeqID == 0)
2544 Out << "S_";
2545 else {
2546 SeqID--;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002547
Anders Carlsson76967372009-09-17 00:43:46 +00002548 // <seq-id> is encoded in base-36, using digits and upper case letters.
2549 char Buffer[10];
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002550 char *BufferPtr = llvm::array_endof(Buffer);
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002551
Anders Carlsson76967372009-09-17 00:43:46 +00002552 if (SeqID == 0) *--BufferPtr = '0';
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002553
Anders Carlsson76967372009-09-17 00:43:46 +00002554 while (SeqID) {
2555 assert(BufferPtr > Buffer && "Buffer overflow!");
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002556
John McCall6ab30e02010-06-09 07:26:17 +00002557 char c = static_cast<char>(SeqID % 36);
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002558
Anders Carlsson76967372009-09-17 00:43:46 +00002559 *--BufferPtr = (c < 10 ? '0' + c : 'A' + c - 10);
2560 SeqID /= 36;
2561 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002562
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002563 Out << 'S'
2564 << llvm::StringRef(BufferPtr, llvm::array_endof(Buffer)-BufferPtr)
2565 << '_';
Anders Carlsson76967372009-09-17 00:43:46 +00002566 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002567
Anders Carlsson76967372009-09-17 00:43:46 +00002568 return true;
2569}
2570
Anders Carlssonf514b542009-09-27 00:12:57 +00002571static bool isCharType(QualType T) {
2572 if (T.isNull())
2573 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002574
Anders Carlssonf514b542009-09-27 00:12:57 +00002575 return T->isSpecificBuiltinType(BuiltinType::Char_S) ||
2576 T->isSpecificBuiltinType(BuiltinType::Char_U);
2577}
2578
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002579/// isCharSpecialization - Returns whether a given type is a template
Anders Carlssonf514b542009-09-27 00:12:57 +00002580/// specialization of a given name with a single argument of type char.
2581static bool isCharSpecialization(QualType T, const char *Name) {
2582 if (T.isNull())
2583 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002584
Anders Carlssonf514b542009-09-27 00:12:57 +00002585 const RecordType *RT = T->getAs<RecordType>();
2586 if (!RT)
2587 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002588
2589 const ClassTemplateSpecializationDecl *SD =
Anders Carlssonf514b542009-09-27 00:12:57 +00002590 dyn_cast<ClassTemplateSpecializationDecl>(RT->getDecl());
2591 if (!SD)
2592 return false;
2593
2594 if (!isStdNamespace(SD->getDeclContext()))
2595 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002596
Anders Carlssonf514b542009-09-27 00:12:57 +00002597 const TemplateArgumentList &TemplateArgs = SD->getTemplateArgs();
2598 if (TemplateArgs.size() != 1)
2599 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002600
Anders Carlssonf514b542009-09-27 00:12:57 +00002601 if (!isCharType(TemplateArgs[0].getAsType()))
2602 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002603
Daniel Dunbar01eb9b92009-10-18 21:17:35 +00002604 return SD->getIdentifier()->getName() == Name;
Anders Carlssonf514b542009-09-27 00:12:57 +00002605}
2606
Anders Carlsson91f88602009-12-07 19:56:42 +00002607template <std::size_t StrLen>
Benjamin Kramer54353f42010-11-25 18:29:30 +00002608static bool isStreamCharSpecialization(const ClassTemplateSpecializationDecl*SD,
2609 const char (&Str)[StrLen]) {
Anders Carlsson91f88602009-12-07 19:56:42 +00002610 if (!SD->getIdentifier()->isStr(Str))
2611 return false;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002612
Anders Carlsson91f88602009-12-07 19:56:42 +00002613 const TemplateArgumentList &TemplateArgs = SD->getTemplateArgs();
2614 if (TemplateArgs.size() != 2)
2615 return false;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002616
Anders Carlsson91f88602009-12-07 19:56:42 +00002617 if (!isCharType(TemplateArgs[0].getAsType()))
2618 return false;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002619
Anders Carlsson91f88602009-12-07 19:56:42 +00002620 if (!isCharSpecialization(TemplateArgs[1].getAsType(), "char_traits"))
2621 return false;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002622
Anders Carlsson91f88602009-12-07 19:56:42 +00002623 return true;
2624}
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002625
Anders Carlssone7c8cb62009-09-26 20:53:44 +00002626bool CXXNameMangler::mangleStandardSubstitution(const NamedDecl *ND) {
2627 // <substitution> ::= St # ::std::
Anders Carlsson8c031552009-09-26 23:10:05 +00002628 if (const NamespaceDecl *NS = dyn_cast<NamespaceDecl>(ND)) {
Anders Carlsson47846d22009-12-04 06:23:23 +00002629 if (isStd(NS)) {
Anders Carlsson8c031552009-09-26 23:10:05 +00002630 Out << "St";
2631 return true;
2632 }
2633 }
2634
2635 if (const ClassTemplateDecl *TD = dyn_cast<ClassTemplateDecl>(ND)) {
2636 if (!isStdNamespace(TD->getDeclContext()))
2637 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002638
Anders Carlsson8c031552009-09-26 23:10:05 +00002639 // <substitution> ::= Sa # ::std::allocator
2640 if (TD->getIdentifier()->isStr("allocator")) {
2641 Out << "Sa";
2642 return true;
2643 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002644
Anders Carlsson189d59c2009-09-26 23:14:39 +00002645 // <<substitution> ::= Sb # ::std::basic_string
2646 if (TD->getIdentifier()->isStr("basic_string")) {
2647 Out << "Sb";
2648 return true;
2649 }
Anders Carlsson8c031552009-09-26 23:10:05 +00002650 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002651
2652 if (const ClassTemplateSpecializationDecl *SD =
Anders Carlssonf514b542009-09-27 00:12:57 +00002653 dyn_cast<ClassTemplateSpecializationDecl>(ND)) {
Eli Friedman5370ee22010-02-23 18:25:09 +00002654 if (!isStdNamespace(SD->getDeclContext()))
2655 return false;
2656
Anders Carlssonf514b542009-09-27 00:12:57 +00002657 // <substitution> ::= Ss # ::std::basic_string<char,
2658 // ::std::char_traits<char>,
2659 // ::std::allocator<char> >
2660 if (SD->getIdentifier()->isStr("basic_string")) {
2661 const TemplateArgumentList &TemplateArgs = SD->getTemplateArgs();
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002662
Anders Carlssonf514b542009-09-27 00:12:57 +00002663 if (TemplateArgs.size() != 3)
2664 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002665
Anders Carlssonf514b542009-09-27 00:12:57 +00002666 if (!isCharType(TemplateArgs[0].getAsType()))
2667 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002668
Anders Carlssonf514b542009-09-27 00:12:57 +00002669 if (!isCharSpecialization(TemplateArgs[1].getAsType(), "char_traits"))
2670 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002671
Anders Carlssonf514b542009-09-27 00:12:57 +00002672 if (!isCharSpecialization(TemplateArgs[2].getAsType(), "allocator"))
2673 return false;
2674
2675 Out << "Ss";
2676 return true;
2677 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002678
Anders Carlsson91f88602009-12-07 19:56:42 +00002679 // <substitution> ::= Si # ::std::basic_istream<char,
2680 // ::std::char_traits<char> >
2681 if (isStreamCharSpecialization(SD, "basic_istream")) {
2682 Out << "Si";
2683 return true;
2684 }
2685
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002686 // <substitution> ::= So # ::std::basic_ostream<char,
Anders Carlsson8f8fd8e2009-10-08 17:20:26 +00002687 // ::std::char_traits<char> >
Anders Carlsson91f88602009-12-07 19:56:42 +00002688 if (isStreamCharSpecialization(SD, "basic_ostream")) {
Anders Carlsson8f8fd8e2009-10-08 17:20:26 +00002689 Out << "So";
2690 return true;
2691 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002692
Anders Carlsson91f88602009-12-07 19:56:42 +00002693 // <substitution> ::= Sd # ::std::basic_iostream<char,
2694 // ::std::char_traits<char> >
2695 if (isStreamCharSpecialization(SD, "basic_iostream")) {
2696 Out << "Sd";
2697 return true;
2698 }
Anders Carlssonf514b542009-09-27 00:12:57 +00002699 }
Anders Carlsson8c031552009-09-26 23:10:05 +00002700 return false;
Anders Carlssone7c8cb62009-09-26 20:53:44 +00002701}
2702
Anders Carlsson76967372009-09-17 00:43:46 +00002703void CXXNameMangler::addSubstitution(QualType T) {
Anders Carlssond99edc42009-09-26 03:55:37 +00002704 if (!T.getCVRQualifiers()) {
2705 if (const RecordType *RT = T->getAs<RecordType>()) {
2706 addSubstitution(RT->getDecl());
2707 return;
2708 }
2709 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002710
Anders Carlsson76967372009-09-17 00:43:46 +00002711 uintptr_t TypePtr = reinterpret_cast<uintptr_t>(T.getAsOpaquePtr());
Anders Carlssond3a932a2009-09-17 03:53:28 +00002712 addSubstitution(TypePtr);
2713}
2714
Douglas Gregor1e9268e2010-04-28 05:58:56 +00002715void CXXNameMangler::addSubstitution(TemplateName Template) {
2716 if (TemplateDecl *TD = Template.getAsTemplateDecl())
2717 return addSubstitution(TD);
Sean Huntc3021132010-05-05 15:23:54 +00002718
Douglas Gregor1e9268e2010-04-28 05:58:56 +00002719 Template = Context.getASTContext().getCanonicalTemplateName(Template);
2720 addSubstitution(reinterpret_cast<uintptr_t>(Template.getAsVoidPointer()));
2721}
2722
Anders Carlssond3a932a2009-09-17 03:53:28 +00002723void CXXNameMangler::addSubstitution(uintptr_t Ptr) {
Anders Carlssond3a932a2009-09-17 03:53:28 +00002724 assert(!Substitutions.count(Ptr) && "Substitution already exists!");
Anders Carlsson9d85b722010-06-02 04:29:50 +00002725 Substitutions[Ptr] = SeqID++;
Anders Carlsson76967372009-09-17 00:43:46 +00002726}
2727
Daniel Dunbar1b077112009-11-21 09:06:10 +00002728//
Mike Stump1eb44332009-09-09 15:08:12 +00002729
Daniel Dunbar1b077112009-11-21 09:06:10 +00002730/// \brief Mangles the name of the declaration D and emits that name to the
2731/// given output stream.
2732///
2733/// If the declaration D requires a mangled name, this routine will emit that
2734/// mangled name to \p os and return true. Otherwise, \p os will be unchanged
2735/// and this routine will return false. In this case, the caller should just
2736/// emit the identifier of the declaration (\c D->getIdentifier()) as its
2737/// name.
Peter Collingbourne14110472011-01-13 18:57:25 +00002738void ItaniumMangleContext::mangleName(const NamedDecl *D,
Rafael Espindola0e376a02011-02-11 01:41:00 +00002739 llvm::raw_ostream &Out) {
Daniel Dunbarc02ab4c2009-11-21 09:14:44 +00002740 assert((isa<FunctionDecl>(D) || isa<VarDecl>(D)) &&
2741 "Invalid mangleName() call, argument is not a variable or function!");
2742 assert(!isa<CXXConstructorDecl>(D) && !isa<CXXDestructorDecl>(D) &&
2743 "Invalid mangleName() call on 'structor decl!");
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002744
Daniel Dunbar1b077112009-11-21 09:06:10 +00002745 PrettyStackTraceDecl CrashInfo(D, SourceLocation(),
2746 getASTContext().getSourceManager(),
2747 "Mangling declaration");
Mike Stump1eb44332009-09-09 15:08:12 +00002748
Rafael Espindolac4850c22011-02-10 23:59:36 +00002749 CXXNameMangler Mangler(*this, Out);
Daniel Dunbar94fd26d2009-11-21 09:06:22 +00002750 return Mangler.mangle(D);
Daniel Dunbar1b077112009-11-21 09:06:10 +00002751}
Mike Stump1eb44332009-09-09 15:08:12 +00002752
Peter Collingbourne14110472011-01-13 18:57:25 +00002753void ItaniumMangleContext::mangleCXXCtor(const CXXConstructorDecl *D,
2754 CXXCtorType Type,
Rafael Espindola0e376a02011-02-11 01:41:00 +00002755 llvm::raw_ostream &Out) {
Rafael Espindolac4850c22011-02-10 23:59:36 +00002756 CXXNameMangler Mangler(*this, Out, D, Type);
Daniel Dunbar77939c92009-11-21 09:06:31 +00002757 Mangler.mangle(D);
Daniel Dunbar1b077112009-11-21 09:06:10 +00002758}
Mike Stump1eb44332009-09-09 15:08:12 +00002759
Peter Collingbourne14110472011-01-13 18:57:25 +00002760void ItaniumMangleContext::mangleCXXDtor(const CXXDestructorDecl *D,
2761 CXXDtorType Type,
Rafael Espindola0e376a02011-02-11 01:41:00 +00002762 llvm::raw_ostream &Out) {
Rafael Espindolac4850c22011-02-10 23:59:36 +00002763 CXXNameMangler Mangler(*this, Out, D, Type);
Daniel Dunbar77939c92009-11-21 09:06:31 +00002764 Mangler.mangle(D);
Daniel Dunbar1b077112009-11-21 09:06:10 +00002765}
Mike Stumpf1216772009-07-31 18:25:34 +00002766
Peter Collingbourne14110472011-01-13 18:57:25 +00002767void ItaniumMangleContext::mangleThunk(const CXXMethodDecl *MD,
2768 const ThunkInfo &Thunk,
Rafael Espindolaf0be9792011-02-11 02:52:17 +00002769 llvm::raw_ostream &Out) {
Anders Carlsson19879c92010-03-23 17:17:29 +00002770 // <special-name> ::= T <call-offset> <base encoding>
2771 // # base is the nominal target function of thunk
2772 // <special-name> ::= Tc <call-offset> <call-offset> <base encoding>
2773 // # base is the nominal target function of thunk
2774 // # first call-offset is 'this' adjustment
2775 // # second call-offset is result adjustment
Sean Huntc3021132010-05-05 15:23:54 +00002776
Anders Carlsson19879c92010-03-23 17:17:29 +00002777 assert(!isa<CXXDestructorDecl>(MD) &&
2778 "Use mangleCXXDtor for destructor decls!");
Rafael Espindolac4850c22011-02-10 23:59:36 +00002779 CXXNameMangler Mangler(*this, Out);
Anders Carlsson19879c92010-03-23 17:17:29 +00002780 Mangler.getStream() << "_ZT";
2781 if (!Thunk.Return.isEmpty())
2782 Mangler.getStream() << 'c';
Sean Huntc3021132010-05-05 15:23:54 +00002783
Anders Carlsson19879c92010-03-23 17:17:29 +00002784 // Mangle the 'this' pointer adjustment.
2785 Mangler.mangleCallOffset(Thunk.This.NonVirtual, Thunk.This.VCallOffsetOffset);
Sean Huntc3021132010-05-05 15:23:54 +00002786
Anders Carlsson19879c92010-03-23 17:17:29 +00002787 // Mangle the return pointer adjustment if there is one.
2788 if (!Thunk.Return.isEmpty())
2789 Mangler.mangleCallOffset(Thunk.Return.NonVirtual,
2790 Thunk.Return.VBaseOffsetOffset);
Sean Huntc3021132010-05-05 15:23:54 +00002791
Anders Carlsson19879c92010-03-23 17:17:29 +00002792 Mangler.mangleFunctionEncoding(MD);
2793}
2794
Sean Huntc3021132010-05-05 15:23:54 +00002795void
Peter Collingbourne14110472011-01-13 18:57:25 +00002796ItaniumMangleContext::mangleCXXDtorThunk(const CXXDestructorDecl *DD,
2797 CXXDtorType Type,
2798 const ThisAdjustment &ThisAdjustment,
Rafael Espindolaf0be9792011-02-11 02:52:17 +00002799 llvm::raw_ostream &Out) {
Anders Carlsson19879c92010-03-23 17:17:29 +00002800 // <special-name> ::= T <call-offset> <base encoding>
2801 // # base is the nominal target function of thunk
Rafael Espindolac4850c22011-02-10 23:59:36 +00002802 CXXNameMangler Mangler(*this, Out, DD, Type);
Anders Carlsson19879c92010-03-23 17:17:29 +00002803 Mangler.getStream() << "_ZT";
2804
2805 // Mangle the 'this' pointer adjustment.
Sean Huntc3021132010-05-05 15:23:54 +00002806 Mangler.mangleCallOffset(ThisAdjustment.NonVirtual,
Anders Carlsson19879c92010-03-23 17:17:29 +00002807 ThisAdjustment.VCallOffsetOffset);
2808
2809 Mangler.mangleFunctionEncoding(DD);
2810}
2811
Daniel Dunbarc0747712009-11-21 09:12:13 +00002812/// mangleGuardVariable - Returns the mangled name for a guard variable
2813/// for the passed in VarDecl.
Peter Collingbourne14110472011-01-13 18:57:25 +00002814void ItaniumMangleContext::mangleItaniumGuardVariable(const VarDecl *D,
Rafael Espindolaf0be9792011-02-11 02:52:17 +00002815 llvm::raw_ostream &Out) {
Daniel Dunbarc0747712009-11-21 09:12:13 +00002816 // <special-name> ::= GV <object name> # Guard variable for one-time
2817 // # initialization
Rafael Espindolac4850c22011-02-10 23:59:36 +00002818 CXXNameMangler Mangler(*this, Out);
Daniel Dunbarc0747712009-11-21 09:12:13 +00002819 Mangler.getStream() << "_ZGV";
2820 Mangler.mangleName(D);
2821}
2822
Peter Collingbourne14110472011-01-13 18:57:25 +00002823void ItaniumMangleContext::mangleReferenceTemporary(const VarDecl *D,
Rafael Espindolaf0be9792011-02-11 02:52:17 +00002824 llvm::raw_ostream &Out) {
Anders Carlsson715edf22010-06-26 16:09:40 +00002825 // We match the GCC mangling here.
2826 // <special-name> ::= GR <object name>
Rafael Espindolac4850c22011-02-10 23:59:36 +00002827 CXXNameMangler Mangler(*this, Out);
Anders Carlsson715edf22010-06-26 16:09:40 +00002828 Mangler.getStream() << "_ZGR";
2829 Mangler.mangleName(D);
2830}
2831
Peter Collingbourne14110472011-01-13 18:57:25 +00002832void ItaniumMangleContext::mangleCXXVTable(const CXXRecordDecl *RD,
Rafael Espindolaf0be9792011-02-11 02:52:17 +00002833 llvm::raw_ostream &Out) {
Daniel Dunbarc0747712009-11-21 09:12:13 +00002834 // <special-name> ::= TV <type> # virtual table
Rafael Espindolac4850c22011-02-10 23:59:36 +00002835 CXXNameMangler Mangler(*this, Out);
Daniel Dunbarc0747712009-11-21 09:12:13 +00002836 Mangler.getStream() << "_ZTV";
Douglas Gregor1b12a3b2010-05-26 05:11:13 +00002837 Mangler.mangleNameOrStandardSubstitution(RD);
Daniel Dunbar1b077112009-11-21 09:06:10 +00002838}
Mike Stump82d75b02009-11-10 01:58:37 +00002839
Peter Collingbourne14110472011-01-13 18:57:25 +00002840void ItaniumMangleContext::mangleCXXVTT(const CXXRecordDecl *RD,
Rafael Espindolaf0be9792011-02-11 02:52:17 +00002841 llvm::raw_ostream &Out) {
Daniel Dunbarc0747712009-11-21 09:12:13 +00002842 // <special-name> ::= TT <type> # VTT structure
Rafael Espindolac4850c22011-02-10 23:59:36 +00002843 CXXNameMangler Mangler(*this, Out);
Daniel Dunbarc0747712009-11-21 09:12:13 +00002844 Mangler.getStream() << "_ZTT";
Douglas Gregor1b12a3b2010-05-26 05:11:13 +00002845 Mangler.mangleNameOrStandardSubstitution(RD);
Daniel Dunbar1b077112009-11-21 09:06:10 +00002846}
Mike Stumpab3f7e92009-11-10 01:41:59 +00002847
Peter Collingbourne14110472011-01-13 18:57:25 +00002848void ItaniumMangleContext::mangleCXXCtorVTable(const CXXRecordDecl *RD,
2849 int64_t Offset,
2850 const CXXRecordDecl *Type,
Rafael Espindolaf0be9792011-02-11 02:52:17 +00002851 llvm::raw_ostream &Out) {
Daniel Dunbarc0747712009-11-21 09:12:13 +00002852 // <special-name> ::= TC <type> <offset number> _ <base type>
Rafael Espindolac4850c22011-02-10 23:59:36 +00002853 CXXNameMangler Mangler(*this, Out);
Daniel Dunbarc0747712009-11-21 09:12:13 +00002854 Mangler.getStream() << "_ZTC";
Douglas Gregor1b12a3b2010-05-26 05:11:13 +00002855 Mangler.mangleNameOrStandardSubstitution(RD);
Daniel Dunbarc0747712009-11-21 09:12:13 +00002856 Mangler.getStream() << Offset;
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002857 Mangler.getStream() << '_';
Douglas Gregor1b12a3b2010-05-26 05:11:13 +00002858 Mangler.mangleNameOrStandardSubstitution(Type);
Daniel Dunbar1b077112009-11-21 09:06:10 +00002859}
Mike Stump738f8c22009-07-31 23:15:31 +00002860
Peter Collingbourne14110472011-01-13 18:57:25 +00002861void ItaniumMangleContext::mangleCXXRTTI(QualType Ty,
Rafael Espindolaf0be9792011-02-11 02:52:17 +00002862 llvm::raw_ostream &Out) {
Daniel Dunbarc0747712009-11-21 09:12:13 +00002863 // <special-name> ::= TI <type> # typeinfo structure
Douglas Gregor154fe982009-12-23 22:04:40 +00002864 assert(!Ty.hasQualifiers() && "RTTI info cannot have top-level qualifiers");
Rafael Espindolac4850c22011-02-10 23:59:36 +00002865 CXXNameMangler Mangler(*this, Out);
Daniel Dunbarc0747712009-11-21 09:12:13 +00002866 Mangler.getStream() << "_ZTI";
2867 Mangler.mangleType(Ty);
Daniel Dunbar1b077112009-11-21 09:06:10 +00002868}
Mike Stump67795982009-11-14 00:14:13 +00002869
Peter Collingbourne14110472011-01-13 18:57:25 +00002870void ItaniumMangleContext::mangleCXXRTTIName(QualType Ty,
Rafael Espindolaf0be9792011-02-11 02:52:17 +00002871 llvm::raw_ostream &Out) {
Daniel Dunbarc0747712009-11-21 09:12:13 +00002872 // <special-name> ::= TS <type> # typeinfo name (null terminated byte string)
Rafael Espindolac4850c22011-02-10 23:59:36 +00002873 CXXNameMangler Mangler(*this, Out);
Daniel Dunbarc0747712009-11-21 09:12:13 +00002874 Mangler.getStream() << "_ZTS";
2875 Mangler.mangleType(Ty);
Mike Stumpf1216772009-07-31 18:25:34 +00002876}
Peter Collingbourne14110472011-01-13 18:57:25 +00002877
2878MangleContext *clang::createItaniumMangleContext(ASTContext &Context,
2879 Diagnostic &Diags) {
2880 return new ItaniumMangleContext(Context, Diags);
2881}