blob: 14ee74be124b93a3ba01c7c7d0caac4088e70cd9 [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 McCall864c0412011-04-26 20:42:42 +00001474 case BuiltinType::BoundMember:
John McCall1de4d4e2011-04-07 08:22:57 +00001475 case BuiltinType::UnknownAny:
Mike Stump1eb44332009-09-09 15:08:12 +00001476 assert(false &&
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001477 "Overloaded and dependent types shouldn't get to name mangling");
1478 break;
Steve Naroff9533a7f2009-07-22 17:14:51 +00001479 case BuiltinType::ObjCId: Out << "11objc_object"; break;
1480 case BuiltinType::ObjCClass: Out << "10objc_class"; break;
Fariborz Jahanian13dcd002009-11-21 19:53:08 +00001481 case BuiltinType::ObjCSel: Out << "13objc_selector"; break;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001482 }
1483}
1484
John McCallefe6aee2009-09-05 07:56:18 +00001485// <type> ::= <function-type>
1486// <function-type> ::= F [Y] <bare-function-type> E
1487void CXXNameMangler::mangleType(const FunctionProtoType *T) {
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001488 Out << 'F';
Mike Stumpf5408fe2009-05-16 07:57:57 +00001489 // FIXME: We don't have enough information in the AST to produce the 'Y'
1490 // encoding for extern "C" function types.
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001491 mangleBareFunctionType(T, /*MangleReturnType=*/true);
1492 Out << 'E';
1493}
John McCallefe6aee2009-09-05 07:56:18 +00001494void CXXNameMangler::mangleType(const FunctionNoProtoType *T) {
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +00001495 llvm_unreachable("Can't mangle K&R function prototypes");
John McCallefe6aee2009-09-05 07:56:18 +00001496}
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001497void CXXNameMangler::mangleBareFunctionType(const FunctionType *T,
1498 bool MangleReturnType) {
John McCallefe6aee2009-09-05 07:56:18 +00001499 // We should never be mangling something without a prototype.
1500 const FunctionProtoType *Proto = cast<FunctionProtoType>(T);
1501
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001502 // <bare-function-type> ::= <signature type>+
1503 if (MangleReturnType)
John McCallefe6aee2009-09-05 07:56:18 +00001504 mangleType(Proto->getResultType());
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001505
Anders Carlsson93296682010-06-02 04:40:13 +00001506 if (Proto->getNumArgs() == 0 && !Proto->isVariadic()) {
Eli Friedmana7e68452010-08-22 01:00:03 +00001507 // <builtin-type> ::= v # void
Anders Carlssonc6c91bc2009-04-01 00:15:23 +00001508 Out << 'v';
1509 return;
1510 }
Mike Stump1eb44332009-09-09 15:08:12 +00001511
Douglas Gregor72564e72009-02-26 23:50:07 +00001512 for (FunctionProtoType::arg_type_iterator Arg = Proto->arg_type_begin(),
Mike Stump1eb44332009-09-09 15:08:12 +00001513 ArgEnd = Proto->arg_type_end();
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001514 Arg != ArgEnd; ++Arg)
1515 mangleType(*Arg);
Douglas Gregor219cc612009-02-13 01:28:03 +00001516
1517 // <builtin-type> ::= z # ellipsis
1518 if (Proto->isVariadic())
1519 Out << 'z';
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001520}
1521
John McCallefe6aee2009-09-05 07:56:18 +00001522// <type> ::= <class-enum-type>
Mike Stump1eb44332009-09-09 15:08:12 +00001523// <class-enum-type> ::= <name>
John McCalled976492009-12-04 22:46:56 +00001524void CXXNameMangler::mangleType(const UnresolvedUsingType *T) {
1525 mangleName(T->getDecl());
1526}
1527
1528// <type> ::= <class-enum-type>
1529// <class-enum-type> ::= <name>
John McCallefe6aee2009-09-05 07:56:18 +00001530void CXXNameMangler::mangleType(const EnumType *T) {
1531 mangleType(static_cast<const TagType*>(T));
1532}
1533void CXXNameMangler::mangleType(const RecordType *T) {
1534 mangleType(static_cast<const TagType*>(T));
1535}
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001536void CXXNameMangler::mangleType(const TagType *T) {
Eli Friedmanecb7e932009-12-11 18:00:57 +00001537 mangleName(T->getDecl());
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001538}
1539
John McCallefe6aee2009-09-05 07:56:18 +00001540// <type> ::= <array-type>
1541// <array-type> ::= A <positive dimension number> _ <element type>
1542// ::= A [<dimension expression>] _ <element type>
1543void CXXNameMangler::mangleType(const ConstantArrayType *T) {
1544 Out << 'A' << T->getSize() << '_';
1545 mangleType(T->getElementType());
1546}
1547void CXXNameMangler::mangleType(const VariableArrayType *T) {
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001548 Out << 'A';
Fariborz Jahanian7281d1f2010-11-02 16:54:00 +00001549 // decayed vla types (size 0) will just be skipped.
1550 if (T->getSizeExpr())
1551 mangleExpression(T->getSizeExpr());
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001552 Out << '_';
1553 mangleType(T->getElementType());
1554}
John McCallefe6aee2009-09-05 07:56:18 +00001555void CXXNameMangler::mangleType(const DependentSizedArrayType *T) {
1556 Out << 'A';
1557 mangleExpression(T->getSizeExpr());
1558 Out << '_';
1559 mangleType(T->getElementType());
1560}
1561void CXXNameMangler::mangleType(const IncompleteArrayType *T) {
Nick Lewycky271b6652010-09-05 03:40:33 +00001562 Out << "A_";
John McCallefe6aee2009-09-05 07:56:18 +00001563 mangleType(T->getElementType());
1564}
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001565
John McCallefe6aee2009-09-05 07:56:18 +00001566// <type> ::= <pointer-to-member-type>
1567// <pointer-to-member-type> ::= M <class type> <member type>
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001568void CXXNameMangler::mangleType(const MemberPointerType *T) {
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001569 Out << 'M';
1570 mangleType(QualType(T->getClass(), 0));
Anders Carlsson0e650012009-05-17 17:41:20 +00001571 QualType PointeeType = T->getPointeeType();
1572 if (const FunctionProtoType *FPT = dyn_cast<FunctionProtoType>(PointeeType)) {
John McCall0953e762009-09-24 19:53:00 +00001573 mangleQualifiers(Qualifiers::fromCVRMask(FPT->getTypeQuals()));
Douglas Gregor0a9a6d62011-01-26 17:36:28 +00001574 mangleRefQualifier(FPT->getRefQualifier());
Anders Carlsson0e650012009-05-17 17:41:20 +00001575 mangleType(FPT);
Anders Carlsson9d85b722010-06-02 04:29:50 +00001576
1577 // Itanium C++ ABI 5.1.8:
1578 //
1579 // The type of a non-static member function is considered to be different,
1580 // for the purposes of substitution, from the type of a namespace-scope or
1581 // static member function whose type appears similar. The types of two
1582 // non-static member functions are considered to be different, for the
1583 // purposes of substitution, if the functions are members of different
1584 // classes. In other words, for the purposes of substitution, the class of
1585 // which the function is a member is considered part of the type of
1586 // function.
1587
1588 // We increment the SeqID here to emulate adding an entry to the
1589 // substitution table. We can't actually add it because we don't want this
1590 // particular function type to be substituted.
1591 ++SeqID;
Mike Stump1eb44332009-09-09 15:08:12 +00001592 } else
Anders Carlsson0e650012009-05-17 17:41:20 +00001593 mangleType(PointeeType);
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001594}
1595
John McCallefe6aee2009-09-05 07:56:18 +00001596// <type> ::= <template-param>
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001597void CXXNameMangler::mangleType(const TemplateTypeParmType *T) {
Anders Carlsson0ccdf8d2009-09-27 00:38:53 +00001598 mangleTemplateParameter(T->getIndex());
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001599}
1600
Douglas Gregorc3069d62011-01-14 02:55:32 +00001601// <type> ::= <template-param>
1602void CXXNameMangler::mangleType(const SubstTemplateTypeParmPackType *T) {
1603 mangleTemplateParameter(T->getReplacedParameter()->getIndex());
1604}
1605
John McCallefe6aee2009-09-05 07:56:18 +00001606// <type> ::= P <type> # pointer-to
1607void CXXNameMangler::mangleType(const PointerType *T) {
1608 Out << 'P';
1609 mangleType(T->getPointeeType());
1610}
1611void CXXNameMangler::mangleType(const ObjCObjectPointerType *T) {
1612 Out << 'P';
1613 mangleType(T->getPointeeType());
1614}
1615
1616// <type> ::= R <type> # reference-to
1617void CXXNameMangler::mangleType(const LValueReferenceType *T) {
1618 Out << 'R';
1619 mangleType(T->getPointeeType());
1620}
1621
1622// <type> ::= O <type> # rvalue reference-to (C++0x)
1623void CXXNameMangler::mangleType(const RValueReferenceType *T) {
1624 Out << 'O';
1625 mangleType(T->getPointeeType());
1626}
1627
1628// <type> ::= C <type> # complex pair (C 2000)
1629void CXXNameMangler::mangleType(const ComplexType *T) {
1630 Out << 'C';
1631 mangleType(T->getElementType());
1632}
1633
Bob Wilsonc7df92d2010-11-12 17:24:43 +00001634// ARM's ABI for Neon vector types specifies that they should be mangled as
Bob Wilson57147a82010-11-16 00:32:18 +00001635// if they are structs (to match ARM's initial implementation). The
1636// vector type must be one of the special types predefined by ARM.
1637void CXXNameMangler::mangleNeonVectorType(const VectorType *T) {
Bob Wilsonc7df92d2010-11-12 17:24:43 +00001638 QualType EltType = T->getElementType();
Bob Wilson57147a82010-11-16 00:32:18 +00001639 assert(EltType->isBuiltinType() && "Neon vector element not a BuiltinType");
Bob Wilsonc7df92d2010-11-12 17:24:43 +00001640 const char *EltName = 0;
Bob Wilson491328c2010-11-12 17:24:46 +00001641 if (T->getVectorKind() == VectorType::NeonPolyVector) {
1642 switch (cast<BuiltinType>(EltType)->getKind()) {
Bob Wilson4cfaa5d2010-11-12 17:24:49 +00001643 case BuiltinType::SChar: EltName = "poly8_t"; break;
1644 case BuiltinType::Short: EltName = "poly16_t"; break;
Bob Wilson57147a82010-11-16 00:32:18 +00001645 default: llvm_unreachable("unexpected Neon polynomial vector element type");
Bob Wilson491328c2010-11-12 17:24:46 +00001646 }
1647 } else {
1648 switch (cast<BuiltinType>(EltType)->getKind()) {
Bob Wilson4cfaa5d2010-11-12 17:24:49 +00001649 case BuiltinType::SChar: EltName = "int8_t"; break;
1650 case BuiltinType::UChar: EltName = "uint8_t"; break;
1651 case BuiltinType::Short: EltName = "int16_t"; break;
1652 case BuiltinType::UShort: EltName = "uint16_t"; break;
1653 case BuiltinType::Int: EltName = "int32_t"; break;
1654 case BuiltinType::UInt: EltName = "uint32_t"; break;
1655 case BuiltinType::LongLong: EltName = "int64_t"; break;
1656 case BuiltinType::ULongLong: EltName = "uint64_t"; break;
1657 case BuiltinType::Float: EltName = "float32_t"; break;
Bob Wilson57147a82010-11-16 00:32:18 +00001658 default: llvm_unreachable("unexpected Neon vector element type");
Bob Wilson491328c2010-11-12 17:24:46 +00001659 }
Bob Wilsonc7df92d2010-11-12 17:24:43 +00001660 }
1661 const char *BaseName = 0;
Bob Wilson4cfaa5d2010-11-12 17:24:49 +00001662 unsigned BitSize = (T->getNumElements() *
Bob Wilson3a723022010-11-16 00:32:12 +00001663 getASTContext().getTypeSize(EltType));
Bob Wilsonc7df92d2010-11-12 17:24:43 +00001664 if (BitSize == 64)
1665 BaseName = "__simd64_";
Bob Wilson57147a82010-11-16 00:32:18 +00001666 else {
1667 assert(BitSize == 128 && "Neon vector type not 64 or 128 bits");
Bob Wilsonc7df92d2010-11-12 17:24:43 +00001668 BaseName = "__simd128_";
Bob Wilson57147a82010-11-16 00:32:18 +00001669 }
Bob Wilsonc7df92d2010-11-12 17:24:43 +00001670 Out << strlen(BaseName) + strlen(EltName);
1671 Out << BaseName << EltName;
Bob Wilsonc7df92d2010-11-12 17:24:43 +00001672}
1673
John McCallefe6aee2009-09-05 07:56:18 +00001674// GNU extension: vector types
Chris Lattner788b0fd2010-06-23 06:00:24 +00001675// <type> ::= <vector-type>
1676// <vector-type> ::= Dv <positive dimension number> _
1677// <extended element type>
1678// ::= Dv [<dimension expression>] _ <element type>
1679// <extended element type> ::= <element type>
1680// ::= p # AltiVec vector pixel
John McCallefe6aee2009-09-05 07:56:18 +00001681void CXXNameMangler::mangleType(const VectorType *T) {
Bob Wilson491328c2010-11-12 17:24:46 +00001682 if ((T->getVectorKind() == VectorType::NeonVector ||
Bob Wilson57147a82010-11-16 00:32:18 +00001683 T->getVectorKind() == VectorType::NeonPolyVector)) {
1684 mangleNeonVectorType(T);
Bob Wilsonc7df92d2010-11-12 17:24:43 +00001685 return;
Bob Wilson57147a82010-11-16 00:32:18 +00001686 }
Nick Lewycky0e5f0672010-03-26 07:18:04 +00001687 Out << "Dv" << T->getNumElements() << '_';
Bob Wilsone86d78c2010-11-10 21:56:12 +00001688 if (T->getVectorKind() == VectorType::AltiVecPixel)
Chris Lattner788b0fd2010-06-23 06:00:24 +00001689 Out << 'p';
Bob Wilsone86d78c2010-11-10 21:56:12 +00001690 else if (T->getVectorKind() == VectorType::AltiVecBool)
Chris Lattner788b0fd2010-06-23 06:00:24 +00001691 Out << 'b';
1692 else
1693 mangleType(T->getElementType());
John McCallefe6aee2009-09-05 07:56:18 +00001694}
1695void CXXNameMangler::mangleType(const ExtVectorType *T) {
1696 mangleType(static_cast<const VectorType*>(T));
1697}
1698void CXXNameMangler::mangleType(const DependentSizedExtVectorType *T) {
Nick Lewycky0e5f0672010-03-26 07:18:04 +00001699 Out << "Dv";
1700 mangleExpression(T->getSizeExpr());
1701 Out << '_';
John McCallefe6aee2009-09-05 07:56:18 +00001702 mangleType(T->getElementType());
1703}
1704
Douglas Gregor7536dd52010-12-20 02:24:11 +00001705void CXXNameMangler::mangleType(const PackExpansionType *T) {
Douglas Gregor4fc48662011-01-13 16:39:34 +00001706 // <type> ::= Dp <type> # pack expansion (C++0x)
Douglas Gregor255c2692011-01-13 17:44:36 +00001707 Out << "Dp";
Douglas Gregor7536dd52010-12-20 02:24:11 +00001708 mangleType(T->getPattern());
1709}
1710
Anders Carlssona40c5e42009-03-07 22:03:21 +00001711void CXXNameMangler::mangleType(const ObjCInterfaceType *T) {
1712 mangleSourceName(T->getDecl()->getIdentifier());
1713}
1714
John McCallc12c5bb2010-05-15 11:32:37 +00001715void CXXNameMangler::mangleType(const ObjCObjectType *T) {
John McCallc00c1f62010-05-15 17:06:29 +00001716 // We don't allow overloading by different protocol qualification,
1717 // so mangling them isn't necessary.
John McCallc12c5bb2010-05-15 11:32:37 +00001718 mangleType(T->getBaseType());
1719}
1720
John McCallefe6aee2009-09-05 07:56:18 +00001721void CXXNameMangler::mangleType(const BlockPointerType *T) {
Anders Carlssonf28c6872009-12-23 22:31:44 +00001722 Out << "U13block_pointer";
1723 mangleType(T->getPointeeType());
John McCallefe6aee2009-09-05 07:56:18 +00001724}
1725
John McCall31f17ec2010-04-27 00:57:59 +00001726void CXXNameMangler::mangleType(const InjectedClassNameType *T) {
1727 // Mangle injected class name types as if the user had written the
1728 // specialization out fully. It may not actually be possible to see
1729 // this mangling, though.
1730 mangleType(T->getInjectedSpecializationType());
1731}
1732
John McCallefe6aee2009-09-05 07:56:18 +00001733void CXXNameMangler::mangleType(const TemplateSpecializationType *T) {
Douglas Gregor1e9268e2010-04-28 05:58:56 +00001734 if (TemplateDecl *TD = T->getTemplateName().getAsTemplateDecl()) {
1735 mangleName(TD, T->getArgs(), T->getNumArgs());
1736 } else {
1737 if (mangleSubstitution(QualType(T, 0)))
1738 return;
Sean Huntc3021132010-05-05 15:23:54 +00001739
Douglas Gregor1e9268e2010-04-28 05:58:56 +00001740 mangleTemplatePrefix(T->getTemplateName());
Sean Huntc3021132010-05-05 15:23:54 +00001741
Douglas Gregor1e9268e2010-04-28 05:58:56 +00001742 // FIXME: GCC does not appear to mangle the template arguments when
1743 // the template in question is a dependent template name. Should we
1744 // emulate that badness?
1745 mangleTemplateArgs(T->getTemplateName(), T->getArgs(), T->getNumArgs());
1746 addSubstitution(QualType(T, 0));
1747 }
John McCallefe6aee2009-09-05 07:56:18 +00001748}
1749
Douglas Gregor4714c122010-03-31 17:34:00 +00001750void CXXNameMangler::mangleType(const DependentNameType *T) {
Anders Carlssonae352482009-09-26 02:26:02 +00001751 // Typename types are always nested
1752 Out << 'N';
John McCalla0ce15c2011-04-24 08:23:24 +00001753 manglePrefix(T->getQualifier());
John McCall33500952010-06-11 00:33:02 +00001754 mangleSourceName(T->getIdentifier());
1755 Out << 'E';
1756}
John McCall6ab30e02010-06-09 07:26:17 +00001757
John McCall33500952010-06-11 00:33:02 +00001758void CXXNameMangler::mangleType(const DependentTemplateSpecializationType *T) {
Douglas Gregoraa2187d2011-02-28 00:04:36 +00001759 // Dependently-scoped template types are nested if they have a prefix.
John McCall33500952010-06-11 00:33:02 +00001760 Out << 'N';
1761
1762 // TODO: avoid making this TemplateName.
1763 TemplateName Prefix =
1764 getASTContext().getDependentTemplateName(T->getQualifier(),
1765 T->getIdentifier());
1766 mangleTemplatePrefix(Prefix);
1767
1768 // FIXME: GCC does not appear to mangle the template arguments when
1769 // the template in question is a dependent template name. Should we
1770 // emulate that badness?
1771 mangleTemplateArgs(Prefix, T->getArgs(), T->getNumArgs());
Anders Carlssonae352482009-09-26 02:26:02 +00001772 Out << 'E';
John McCallefe6aee2009-09-05 07:56:18 +00001773}
1774
John McCallad5e7382010-03-01 23:49:17 +00001775void CXXNameMangler::mangleType(const TypeOfType *T) {
1776 // FIXME: this is pretty unsatisfactory, but there isn't an obvious
1777 // "extension with parameters" mangling.
1778 Out << "u6typeof";
1779}
1780
1781void CXXNameMangler::mangleType(const TypeOfExprType *T) {
1782 // FIXME: this is pretty unsatisfactory, but there isn't an obvious
1783 // "extension with parameters" mangling.
1784 Out << "u6typeof";
1785}
1786
1787void CXXNameMangler::mangleType(const DecltypeType *T) {
1788 Expr *E = T->getUnderlyingExpr();
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001789
John McCallad5e7382010-03-01 23:49:17 +00001790 // type ::= Dt <expression> E # decltype of an id-expression
1791 // # or class member access
1792 // ::= DT <expression> E # decltype of an expression
1793
1794 // This purports to be an exhaustive list of id-expressions and
1795 // class member accesses. Note that we do not ignore parentheses;
1796 // parentheses change the semantics of decltype for these
1797 // expressions (and cause the mangler to use the other form).
1798 if (isa<DeclRefExpr>(E) ||
1799 isa<MemberExpr>(E) ||
1800 isa<UnresolvedLookupExpr>(E) ||
1801 isa<DependentScopeDeclRefExpr>(E) ||
1802 isa<CXXDependentScopeMemberExpr>(E) ||
1803 isa<UnresolvedMemberExpr>(E))
1804 Out << "Dt";
1805 else
1806 Out << "DT";
1807 mangleExpression(E);
1808 Out << 'E';
1809}
1810
Richard Smith34b41d92011-02-20 03:19:35 +00001811void CXXNameMangler::mangleType(const AutoType *T) {
1812 QualType D = T->getDeducedType();
Richard Smith967ecd32011-02-21 20:10:02 +00001813 // <builtin-type> ::= Da # dependent auto
1814 if (D.isNull())
1815 Out << "Da";
1816 else
1817 mangleType(D);
Richard Smith34b41d92011-02-20 03:19:35 +00001818}
1819
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001820void CXXNameMangler::mangleIntegerLiteral(QualType T,
Anders Carlssone170ba72009-12-14 01:45:37 +00001821 const llvm::APSInt &Value) {
1822 // <expr-primary> ::= L <type> <value number> E # integer literal
1823 Out << 'L';
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001824
Anders Carlssone170ba72009-12-14 01:45:37 +00001825 mangleType(T);
1826 if (T->isBooleanType()) {
1827 // Boolean values are encoded as 0/1.
1828 Out << (Value.getBoolValue() ? '1' : '0');
1829 } else {
John McCall0512e482010-07-14 04:20:34 +00001830 mangleNumber(Value);
Anders Carlssone170ba72009-12-14 01:45:37 +00001831 }
1832 Out << 'E';
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001833
Anders Carlssone170ba72009-12-14 01:45:37 +00001834}
1835
John McCall2f27bf82010-02-04 02:56:29 +00001836/// Mangles a member expression. Implicit accesses are not handled,
1837/// but that should be okay, because you shouldn't be able to
1838/// make an implicit access in a function template declaration.
John McCalla0ce15c2011-04-24 08:23:24 +00001839void CXXNameMangler::mangleMemberExpr(const Expr *base,
1840 bool isArrow,
1841 NestedNameSpecifier *qualifier,
1842 NamedDecl *firstQualifierLookup,
1843 DeclarationName member,
1844 unsigned arity) {
1845 // <expression> ::= dt <expression> <unresolved-name>
1846 // ::= pt <expression> <unresolved-name>
1847 Out << (isArrow ? "pt" : "dt");
1848 mangleExpression(base);
1849 mangleUnresolvedName(qualifier, firstQualifierLookup, member, arity);
John McCall2f27bf82010-02-04 02:56:29 +00001850}
1851
John McCall5e1e89b2010-08-18 19:18:59 +00001852void CXXNameMangler::mangleExpression(const Expr *E, unsigned Arity) {
Anders Carlssond553f8c2009-09-21 01:21:10 +00001853 // <expression> ::= <unary operator-name> <expression>
John McCall09cc1412010-02-03 00:55:45 +00001854 // ::= <binary operator-name> <expression> <expression>
1855 // ::= <trinary operator-name> <expression> <expression> <expression>
Eli Friedmana7e68452010-08-22 01:00:03 +00001856 // ::= cl <expression>* E # call
Anders Carlssond553f8c2009-09-21 01:21:10 +00001857 // ::= cv <type> expression # conversion with one argument
1858 // ::= cv <type> _ <expression>* E # conversion with a different number of arguments
Eli Friedmana7e68452010-08-22 01:00:03 +00001859 // ::= st <type> # sizeof (a type)
Anders Carlssond553f8c2009-09-21 01:21:10 +00001860 // ::= at <type> # alignof (a type)
1861 // ::= <template-param>
1862 // ::= <function-param>
1863 // ::= sr <type> <unqualified-name> # dependent name
1864 // ::= sr <type> <unqualified-name> <template-args> # dependent template-id
1865 // ::= sZ <template-param> # size of a parameter pack
Douglas Gregor4fc48662011-01-13 16:39:34 +00001866 // ::= sZ <function-param> # size of a function parameter pack
John McCall09cc1412010-02-03 00:55:45 +00001867 // ::= <expr-primary>
John McCall1dd73832010-02-04 01:42:13 +00001868 // <expr-primary> ::= L <type> <value number> E # integer literal
1869 // ::= L <type <value float> E # floating literal
1870 // ::= L <mangled-name> E # external name
Anders Carlssond553f8c2009-09-21 01:21:10 +00001871 switch (E->getStmtClass()) {
John McCall6ae1f352010-04-09 22:26:14 +00001872 case Expr::NoStmtClass:
John McCall63c00d72011-02-09 08:16:59 +00001873#define ABSTRACT_STMT(Type)
John McCall6ae1f352010-04-09 22:26:14 +00001874#define EXPR(Type, Base)
1875#define STMT(Type, Base) \
1876 case Expr::Type##Class:
Sean Hunt4bfe1962010-05-05 15:24:00 +00001877#include "clang/AST/StmtNodes.inc"
John McCall0512e482010-07-14 04:20:34 +00001878 // fallthrough
1879
1880 // These all can only appear in local or variable-initialization
1881 // contexts and so should never appear in a mangling.
1882 case Expr::AddrLabelExprClass:
1883 case Expr::BlockDeclRefExprClass:
1884 case Expr::CXXThisExprClass:
1885 case Expr::DesignatedInitExprClass:
1886 case Expr::ImplicitValueInitExprClass:
1887 case Expr::InitListExprClass:
1888 case Expr::ParenListExprClass:
1889 case Expr::CXXScalarValueInitExprClass:
John McCall09cc1412010-02-03 00:55:45 +00001890 llvm_unreachable("unexpected statement kind");
1891 break;
1892
John McCall0512e482010-07-14 04:20:34 +00001893 // FIXME: invent manglings for all these.
1894 case Expr::BlockExprClass:
1895 case Expr::CXXPseudoDestructorExprClass:
1896 case Expr::ChooseExprClass:
1897 case Expr::CompoundLiteralExprClass:
1898 case Expr::ExtVectorElementExprClass:
Peter Collingbournef111d932011-04-15 00:35:48 +00001899 case Expr::GenericSelectionExprClass:
John McCall0512e482010-07-14 04:20:34 +00001900 case Expr::ObjCEncodeExprClass:
John McCall0512e482010-07-14 04:20:34 +00001901 case Expr::ObjCIsaExprClass:
1902 case Expr::ObjCIvarRefExprClass:
1903 case Expr::ObjCMessageExprClass:
1904 case Expr::ObjCPropertyRefExprClass:
1905 case Expr::ObjCProtocolExprClass:
1906 case Expr::ObjCSelectorExprClass:
1907 case Expr::ObjCStringLiteralClass:
John McCall0512e482010-07-14 04:20:34 +00001908 case Expr::OffsetOfExprClass:
1909 case Expr::PredefinedExprClass:
1910 case Expr::ShuffleVectorExprClass:
1911 case Expr::StmtExprClass:
John McCall0512e482010-07-14 04:20:34 +00001912 case Expr::UnaryTypeTraitExprClass:
Francois Pichet6ad6f282010-12-07 00:08:36 +00001913 case Expr::BinaryTypeTraitExprClass:
John Wiegley55262202011-04-25 06:54:41 +00001914 case Expr::ExpressionTraitExprClass:
Francois Pichet9be88402010-09-08 23:47:05 +00001915 case Expr::VAArgExprClass:
Sebastian Redl2e156222010-09-10 20:55:43 +00001916 case Expr::CXXUuidofExprClass:
Peter Collingbournee08ce652011-02-09 21:07:24 +00001917 case Expr::CXXNoexceptExprClass:
1918 case Expr::CUDAKernelCallExprClass: {
John McCall6ae1f352010-04-09 22:26:14 +00001919 // As bad as this diagnostic is, it's better than crashing.
1920 Diagnostic &Diags = Context.getDiags();
1921 unsigned DiagID = Diags.getCustomDiagID(Diagnostic::Error,
1922 "cannot yet mangle expression type %0");
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +00001923 Diags.Report(E->getExprLoc(), DiagID)
John McCall739bf092010-04-10 09:39:25 +00001924 << E->getStmtClassName() << E->getSourceRange();
John McCall6ae1f352010-04-09 22:26:14 +00001925 break;
1926 }
1927
John McCall56ca35d2011-02-17 10:25:35 +00001928 // Even gcc-4.5 doesn't mangle this.
1929 case Expr::BinaryConditionalOperatorClass: {
1930 Diagnostic &Diags = Context.getDiags();
1931 unsigned DiagID =
1932 Diags.getCustomDiagID(Diagnostic::Error,
1933 "?: operator with omitted middle operand cannot be mangled");
1934 Diags.Report(E->getExprLoc(), DiagID)
1935 << E->getStmtClassName() << E->getSourceRange();
1936 break;
1937 }
1938
1939 // These are used for internal purposes and cannot be meaningfully mangled.
John McCall7cd7d1a2010-11-15 23:31:06 +00001940 case Expr::OpaqueValueExprClass:
1941 llvm_unreachable("cannot mangle opaque value; mangling wrong thing?");
1942
John McCall0512e482010-07-14 04:20:34 +00001943 case Expr::CXXDefaultArgExprClass:
John McCall5e1e89b2010-08-18 19:18:59 +00001944 mangleExpression(cast<CXXDefaultArgExpr>(E)->getExpr(), Arity);
John McCall0512e482010-07-14 04:20:34 +00001945 break;
1946
1947 case Expr::CXXMemberCallExprClass: // fallthrough
John McCall1dd73832010-02-04 01:42:13 +00001948 case Expr::CallExprClass: {
1949 const CallExpr *CE = cast<CallExpr>(E);
1950 Out << "cl";
John McCall5e1e89b2010-08-18 19:18:59 +00001951 mangleExpression(CE->getCallee(), CE->getNumArgs());
John McCall1dd73832010-02-04 01:42:13 +00001952 for (unsigned I = 0, N = CE->getNumArgs(); I != N; ++I)
1953 mangleExpression(CE->getArg(I));
Benjamin Kramer35f59b62010-04-10 16:03:31 +00001954 Out << 'E';
John McCall09cc1412010-02-03 00:55:45 +00001955 break;
John McCall1dd73832010-02-04 01:42:13 +00001956 }
John McCall09cc1412010-02-03 00:55:45 +00001957
John McCall0512e482010-07-14 04:20:34 +00001958 case Expr::CXXNewExprClass: {
1959 // Proposal from David Vandervoorde, 2010.06.30
1960 const CXXNewExpr *New = cast<CXXNewExpr>(E);
1961 if (New->isGlobalNew()) Out << "gs";
1962 Out << (New->isArray() ? "na" : "nw");
1963 for (CXXNewExpr::const_arg_iterator I = New->placement_arg_begin(),
1964 E = New->placement_arg_end(); I != E; ++I)
1965 mangleExpression(*I);
1966 Out << '_';
1967 mangleType(New->getAllocatedType());
1968 if (New->hasInitializer()) {
1969 Out << "pi";
1970 for (CXXNewExpr::const_arg_iterator I = New->constructor_arg_begin(),
1971 E = New->constructor_arg_end(); I != E; ++I)
1972 mangleExpression(*I);
1973 }
1974 Out << 'E';
1975 break;
1976 }
1977
John McCall2f27bf82010-02-04 02:56:29 +00001978 case Expr::MemberExprClass: {
1979 const MemberExpr *ME = cast<MemberExpr>(E);
1980 mangleMemberExpr(ME->getBase(), ME->isArrow(),
John McCalla0ce15c2011-04-24 08:23:24 +00001981 ME->getQualifier(), 0, ME->getMemberDecl()->getDeclName(),
John McCall5e1e89b2010-08-18 19:18:59 +00001982 Arity);
John McCall2f27bf82010-02-04 02:56:29 +00001983 break;
1984 }
1985
1986 case Expr::UnresolvedMemberExprClass: {
1987 const UnresolvedMemberExpr *ME = cast<UnresolvedMemberExpr>(E);
1988 mangleMemberExpr(ME->getBase(), ME->isArrow(),
John McCalla0ce15c2011-04-24 08:23:24 +00001989 ME->getQualifier(), 0, ME->getMemberName(),
John McCall5e1e89b2010-08-18 19:18:59 +00001990 Arity);
John McCall6dbce192010-08-20 00:17:19 +00001991 if (ME->hasExplicitTemplateArgs())
1992 mangleTemplateArgs(ME->getExplicitTemplateArgs());
John McCall2f27bf82010-02-04 02:56:29 +00001993 break;
1994 }
1995
1996 case Expr::CXXDependentScopeMemberExprClass: {
1997 const CXXDependentScopeMemberExpr *ME
1998 = cast<CXXDependentScopeMemberExpr>(E);
1999 mangleMemberExpr(ME->getBase(), ME->isArrow(),
John McCalla0ce15c2011-04-24 08:23:24 +00002000 ME->getQualifier(), ME->getFirstQualifierFoundInScope(),
2001 ME->getMember(), Arity);
John McCall6dbce192010-08-20 00:17:19 +00002002 if (ME->hasExplicitTemplateArgs())
2003 mangleTemplateArgs(ME->getExplicitTemplateArgs());
John McCall2f27bf82010-02-04 02:56:29 +00002004 break;
2005 }
2006
John McCall1dd73832010-02-04 01:42:13 +00002007 case Expr::UnresolvedLookupExprClass: {
2008 const UnresolvedLookupExpr *ULE = cast<UnresolvedLookupExpr>(E);
John McCalla0ce15c2011-04-24 08:23:24 +00002009 mangleUnresolvedName(ULE->getQualifier(), 0, ULE->getName(), Arity);
John McCall6dbce192010-08-20 00:17:19 +00002010 if (ULE->hasExplicitTemplateArgs())
2011 mangleTemplateArgs(ULE->getExplicitTemplateArgs());
John McCall1dd73832010-02-04 01:42:13 +00002012 break;
2013 }
2014
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002015 case Expr::CXXUnresolvedConstructExprClass: {
John McCall1dd73832010-02-04 01:42:13 +00002016 const CXXUnresolvedConstructExpr *CE = cast<CXXUnresolvedConstructExpr>(E);
2017 unsigned N = CE->arg_size();
2018
2019 Out << "cv";
2020 mangleType(CE->getType());
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002021 if (N != 1) Out << '_';
John McCall1dd73832010-02-04 01:42:13 +00002022 for (unsigned I = 0; I != N; ++I) mangleExpression(CE->getArg(I));
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002023 if (N != 1) Out << 'E';
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002024 break;
John McCall1dd73832010-02-04 01:42:13 +00002025 }
John McCall09cc1412010-02-03 00:55:45 +00002026
John McCall1dd73832010-02-04 01:42:13 +00002027 case Expr::CXXTemporaryObjectExprClass:
2028 case Expr::CXXConstructExprClass: {
2029 const CXXConstructExpr *CE = cast<CXXConstructExpr>(E);
2030 unsigned N = CE->getNumArgs();
2031
2032 Out << "cv";
2033 mangleType(CE->getType());
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002034 if (N != 1) Out << '_';
John McCall1dd73832010-02-04 01:42:13 +00002035 for (unsigned I = 0; I != N; ++I) mangleExpression(CE->getArg(I));
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002036 if (N != 1) Out << 'E';
John McCall09cc1412010-02-03 00:55:45 +00002037 break;
John McCall1dd73832010-02-04 01:42:13 +00002038 }
2039
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +00002040 case Expr::UnaryExprOrTypeTraitExprClass: {
2041 const UnaryExprOrTypeTraitExpr *SAE = cast<UnaryExprOrTypeTraitExpr>(E);
2042 switch(SAE->getKind()) {
2043 case UETT_SizeOf:
2044 Out << 's';
2045 break;
2046 case UETT_AlignOf:
2047 Out << 'a';
2048 break;
2049 case UETT_VecStep:
2050 Diagnostic &Diags = Context.getDiags();
2051 unsigned DiagID = Diags.getCustomDiagID(Diagnostic::Error,
2052 "cannot yet mangle vec_step expression");
2053 Diags.Report(DiagID);
2054 return;
2055 }
John McCall1dd73832010-02-04 01:42:13 +00002056 if (SAE->isArgumentType()) {
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002057 Out << 't';
John McCall1dd73832010-02-04 01:42:13 +00002058 mangleType(SAE->getArgumentType());
2059 } else {
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002060 Out << 'z';
John McCall1dd73832010-02-04 01:42:13 +00002061 mangleExpression(SAE->getArgumentExpr());
2062 }
2063 break;
2064 }
Anders Carlssona7694082009-11-06 02:50:19 +00002065
John McCall0512e482010-07-14 04:20:34 +00002066 case Expr::CXXThrowExprClass: {
2067 const CXXThrowExpr *TE = cast<CXXThrowExpr>(E);
2068
2069 // Proposal from David Vandervoorde, 2010.06.30
2070 if (TE->getSubExpr()) {
2071 Out << "tw";
2072 mangleExpression(TE->getSubExpr());
2073 } else {
2074 Out << "tr";
2075 }
2076 break;
2077 }
2078
2079 case Expr::CXXTypeidExprClass: {
2080 const CXXTypeidExpr *TIE = cast<CXXTypeidExpr>(E);
2081
2082 // Proposal from David Vandervoorde, 2010.06.30
2083 if (TIE->isTypeOperand()) {
2084 Out << "ti";
2085 mangleType(TIE->getTypeOperand());
2086 } else {
2087 Out << "te";
2088 mangleExpression(TIE->getExprOperand());
2089 }
2090 break;
2091 }
2092
2093 case Expr::CXXDeleteExprClass: {
2094 const CXXDeleteExpr *DE = cast<CXXDeleteExpr>(E);
2095
2096 // Proposal from David Vandervoorde, 2010.06.30
2097 if (DE->isGlobalDelete()) Out << "gs";
2098 Out << (DE->isArrayForm() ? "da" : "dl");
2099 mangleExpression(DE->getArgument());
2100 break;
2101 }
2102
Anders Carlssone170ba72009-12-14 01:45:37 +00002103 case Expr::UnaryOperatorClass: {
2104 const UnaryOperator *UO = cast<UnaryOperator>(E);
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002105 mangleOperatorName(UnaryOperator::getOverloadedOperator(UO->getOpcode()),
Anders Carlssone170ba72009-12-14 01:45:37 +00002106 /*Arity=*/1);
2107 mangleExpression(UO->getSubExpr());
2108 break;
2109 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002110
John McCall0512e482010-07-14 04:20:34 +00002111 case Expr::ArraySubscriptExprClass: {
2112 const ArraySubscriptExpr *AE = cast<ArraySubscriptExpr>(E);
2113
Chris Lattnerfc8f0e12011-04-15 05:22:18 +00002114 // Array subscript is treated as a syntactically weird form of
John McCall0512e482010-07-14 04:20:34 +00002115 // binary operator.
2116 Out << "ix";
2117 mangleExpression(AE->getLHS());
2118 mangleExpression(AE->getRHS());
2119 break;
2120 }
2121
2122 case Expr::CompoundAssignOperatorClass: // fallthrough
Anders Carlssone170ba72009-12-14 01:45:37 +00002123 case Expr::BinaryOperatorClass: {
2124 const BinaryOperator *BO = cast<BinaryOperator>(E);
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002125 mangleOperatorName(BinaryOperator::getOverloadedOperator(BO->getOpcode()),
Anders Carlssone170ba72009-12-14 01:45:37 +00002126 /*Arity=*/2);
2127 mangleExpression(BO->getLHS());
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002128 mangleExpression(BO->getRHS());
Anders Carlssone170ba72009-12-14 01:45:37 +00002129 break;
John McCall2f27bf82010-02-04 02:56:29 +00002130 }
Anders Carlssone170ba72009-12-14 01:45:37 +00002131
2132 case Expr::ConditionalOperatorClass: {
2133 const ConditionalOperator *CO = cast<ConditionalOperator>(E);
2134 mangleOperatorName(OO_Conditional, /*Arity=*/3);
2135 mangleExpression(CO->getCond());
John McCall5e1e89b2010-08-18 19:18:59 +00002136 mangleExpression(CO->getLHS(), Arity);
2137 mangleExpression(CO->getRHS(), Arity);
Anders Carlssone170ba72009-12-14 01:45:37 +00002138 break;
2139 }
2140
Douglas Gregor46287c72010-01-29 16:37:09 +00002141 case Expr::ImplicitCastExprClass: {
John McCall5e1e89b2010-08-18 19:18:59 +00002142 mangleExpression(cast<ImplicitCastExpr>(E)->getSubExpr(), Arity);
Douglas Gregor46287c72010-01-29 16:37:09 +00002143 break;
2144 }
2145
2146 case Expr::CStyleCastExprClass:
2147 case Expr::CXXStaticCastExprClass:
2148 case Expr::CXXDynamicCastExprClass:
2149 case Expr::CXXReinterpretCastExprClass:
2150 case Expr::CXXConstCastExprClass:
2151 case Expr::CXXFunctionalCastExprClass: {
2152 const ExplicitCastExpr *ECE = cast<ExplicitCastExpr>(E);
2153 Out << "cv";
2154 mangleType(ECE->getType());
2155 mangleExpression(ECE->getSubExpr());
2156 break;
2157 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002158
Anders Carlsson58040a52009-12-16 05:48:46 +00002159 case Expr::CXXOperatorCallExprClass: {
2160 const CXXOperatorCallExpr *CE = cast<CXXOperatorCallExpr>(E);
2161 unsigned NumArgs = CE->getNumArgs();
2162 mangleOperatorName(CE->getOperator(), /*Arity=*/NumArgs);
2163 // Mangle the arguments.
2164 for (unsigned i = 0; i != NumArgs; ++i)
2165 mangleExpression(CE->getArg(i));
2166 break;
2167 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002168
Anders Carlssona7694082009-11-06 02:50:19 +00002169 case Expr::ParenExprClass:
John McCall5e1e89b2010-08-18 19:18:59 +00002170 mangleExpression(cast<ParenExpr>(E)->getSubExpr(), Arity);
Anders Carlssona7694082009-11-06 02:50:19 +00002171 break;
2172
Anders Carlssond553f8c2009-09-21 01:21:10 +00002173 case Expr::DeclRefExprClass: {
Douglas Gregor5ed1bc32010-02-28 21:40:32 +00002174 const NamedDecl *D = cast<DeclRefExpr>(E)->getDecl();
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002175
Anders Carlssond553f8c2009-09-21 01:21:10 +00002176 switch (D->getKind()) {
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002177 default:
Douglas Gregor5ed1bc32010-02-28 21:40:32 +00002178 // <expr-primary> ::= L <mangled-name> E # external name
2179 Out << 'L';
2180 mangle(D, "_Z");
2181 Out << 'E';
2182 break;
2183
John McCall3dc7e7b2010-07-24 01:17:35 +00002184 case Decl::EnumConstant: {
2185 const EnumConstantDecl *ED = cast<EnumConstantDecl>(D);
2186 mangleIntegerLiteral(ED->getType(), ED->getInitVal());
2187 break;
2188 }
2189
Anders Carlssond553f8c2009-09-21 01:21:10 +00002190 case Decl::NonTypeTemplateParm: {
2191 const NonTypeTemplateParmDecl *PD = cast<NonTypeTemplateParmDecl>(D);
Anders Carlsson0ccdf8d2009-09-27 00:38:53 +00002192 mangleTemplateParameter(PD->getIndex());
Anders Carlssond553f8c2009-09-21 01:21:10 +00002193 break;
2194 }
2195
2196 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002197
Anders Carlsson50755b02009-09-27 20:11:34 +00002198 break;
Anders Carlssond553f8c2009-09-21 01:21:10 +00002199 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002200
Douglas Gregorc7793c72011-01-15 01:15:58 +00002201 case Expr::SubstNonTypeTemplateParmPackExprClass:
2202 mangleTemplateParameter(
2203 cast<SubstNonTypeTemplateParmPackExpr>(E)->getParameterPack()->getIndex());
2204 break;
2205
John McCall865d4472009-11-19 22:55:06 +00002206 case Expr::DependentScopeDeclRefExprClass: {
2207 const DependentScopeDeclRefExpr *DRE = cast<DependentScopeDeclRefExpr>(E);
Douglas Gregor4b2ccfc2010-02-28 22:05:49 +00002208 NestedNameSpecifier *NNS = DRE->getQualifier();
2209 const Type *QTy = NNS->getAsType();
2210
2211 // When we're dealing with a nested-name-specifier that has just a
2212 // dependent identifier in it, mangle that as a typename. FIXME:
2213 // It isn't clear that we ever actually want to have such a
2214 // nested-name-specifier; why not just represent it as a typename type?
2215 if (!QTy && NNS->getAsIdentifier() && NNS->getPrefix()) {
Douglas Gregor4a2023f2010-03-31 20:19:30 +00002216 QTy = getASTContext().getDependentNameType(ETK_Typename,
2217 NNS->getPrefix(),
2218 NNS->getAsIdentifier())
Douglas Gregor4b2ccfc2010-02-28 22:05:49 +00002219 .getTypePtr();
2220 }
Anders Carlsson50755b02009-09-27 20:11:34 +00002221 assert(QTy && "Qualifier was not type!");
2222
John McCall6dbce192010-08-20 00:17:19 +00002223 // ::= sr <type> <unqualified-name> # dependent name
2224 // ::= sr <type> <unqualified-name> <template-args> # dependent template-id
Anders Carlsson50755b02009-09-27 20:11:34 +00002225 Out << "sr";
2226 mangleType(QualType(QTy, 0));
John McCall5e1e89b2010-08-18 19:18:59 +00002227 mangleUnqualifiedName(0, DRE->getDeclName(), Arity);
John McCall6dbce192010-08-20 00:17:19 +00002228 if (DRE->hasExplicitTemplateArgs())
2229 mangleTemplateArgs(DRE->getExplicitTemplateArgs());
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002230
Anders Carlsson50755b02009-09-27 20:11:34 +00002231 break;
2232 }
2233
John McCalld9307602010-04-09 22:54:09 +00002234 case Expr::CXXBindTemporaryExprClass:
2235 mangleExpression(cast<CXXBindTemporaryExpr>(E)->getSubExpr());
2236 break;
2237
John McCall4765fa02010-12-06 08:20:24 +00002238 case Expr::ExprWithCleanupsClass:
2239 mangleExpression(cast<ExprWithCleanups>(E)->getSubExpr(), Arity);
John McCalld9307602010-04-09 22:54:09 +00002240 break;
2241
John McCall1dd73832010-02-04 01:42:13 +00002242 case Expr::FloatingLiteralClass: {
2243 const FloatingLiteral *FL = cast<FloatingLiteral>(E);
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002244 Out << 'L';
John McCall1dd73832010-02-04 01:42:13 +00002245 mangleType(FL->getType());
John McCall0512e482010-07-14 04:20:34 +00002246 mangleFloat(FL->getValue());
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002247 Out << 'E';
John McCall1dd73832010-02-04 01:42:13 +00002248 break;
2249 }
2250
John McCallde810632010-04-09 21:48:08 +00002251 case Expr::CharacterLiteralClass:
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002252 Out << 'L';
John McCallde810632010-04-09 21:48:08 +00002253 mangleType(E->getType());
2254 Out << cast<CharacterLiteral>(E)->getValue();
2255 Out << 'E';
2256 break;
2257
2258 case Expr::CXXBoolLiteralExprClass:
2259 Out << "Lb";
2260 Out << (cast<CXXBoolLiteralExpr>(E)->getValue() ? '1' : '0');
2261 Out << 'E';
2262 break;
2263
John McCall0512e482010-07-14 04:20:34 +00002264 case Expr::IntegerLiteralClass: {
2265 llvm::APSInt Value(cast<IntegerLiteral>(E)->getValue());
2266 if (E->getType()->isSignedIntegerType())
2267 Value.setIsSigned(true);
2268 mangleIntegerLiteral(E->getType(), Value);
Anders Carlssone170ba72009-12-14 01:45:37 +00002269 break;
John McCall0512e482010-07-14 04:20:34 +00002270 }
2271
2272 case Expr::ImaginaryLiteralClass: {
2273 const ImaginaryLiteral *IE = cast<ImaginaryLiteral>(E);
2274 // Mangle as if a complex literal.
Nick Lewycky271b6652010-09-05 03:40:33 +00002275 // Proposal from David Vandevoorde, 2010.06.30.
John McCall0512e482010-07-14 04:20:34 +00002276 Out << 'L';
2277 mangleType(E->getType());
2278 if (const FloatingLiteral *Imag =
2279 dyn_cast<FloatingLiteral>(IE->getSubExpr())) {
2280 // Mangle a floating-point zero of the appropriate type.
2281 mangleFloat(llvm::APFloat(Imag->getValue().getSemantics()));
2282 Out << '_';
2283 mangleFloat(Imag->getValue());
2284 } else {
Nick Lewycky271b6652010-09-05 03:40:33 +00002285 Out << "0_";
John McCall0512e482010-07-14 04:20:34 +00002286 llvm::APSInt Value(cast<IntegerLiteral>(IE->getSubExpr())->getValue());
2287 if (IE->getSubExpr()->getType()->isSignedIntegerType())
2288 Value.setIsSigned(true);
2289 mangleNumber(Value);
2290 }
2291 Out << 'E';
2292 break;
2293 }
2294
2295 case Expr::StringLiteralClass: {
John McCall1658c392010-07-15 21:53:03 +00002296 // Revised proposal from David Vandervoorde, 2010.07.15.
John McCall0512e482010-07-14 04:20:34 +00002297 Out << 'L';
John McCall1658c392010-07-15 21:53:03 +00002298 assert(isa<ConstantArrayType>(E->getType()));
2299 mangleType(E->getType());
John McCall0512e482010-07-14 04:20:34 +00002300 Out << 'E';
2301 break;
2302 }
2303
2304 case Expr::GNUNullExprClass:
2305 // FIXME: should this really be mangled the same as nullptr?
2306 // fallthrough
2307
2308 case Expr::CXXNullPtrLiteralExprClass: {
2309 // Proposal from David Vandervoorde, 2010.06.30, as
2310 // modified by ABI list discussion.
2311 Out << "LDnE";
2312 break;
2313 }
Douglas Gregorbe230c32011-01-03 17:17:50 +00002314
2315 case Expr::PackExpansionExprClass:
2316 Out << "sp";
2317 mangleExpression(cast<PackExpansionExpr>(E)->getPattern());
2318 break;
Douglas Gregor2e774c42011-01-04 18:56:13 +00002319
2320 case Expr::SizeOfPackExprClass: {
Douglas Gregor2e774c42011-01-04 18:56:13 +00002321 Out << "sZ";
2322 const NamedDecl *Pack = cast<SizeOfPackExpr>(E)->getPack();
2323 if (const TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(Pack))
2324 mangleTemplateParameter(TTP->getIndex());
2325 else if (const NonTypeTemplateParmDecl *NTTP
2326 = dyn_cast<NonTypeTemplateParmDecl>(Pack))
2327 mangleTemplateParameter(NTTP->getIndex());
2328 else if (const TemplateTemplateParmDecl *TempTP
2329 = dyn_cast<TemplateTemplateParmDecl>(Pack))
2330 mangleTemplateParameter(TempTP->getIndex());
2331 else {
Douglas Gregor4fc48662011-01-13 16:39:34 +00002332 // Note: proposed by Mike Herrick on 11/30/10
2333 // <expression> ::= sZ <function-param> # size of function parameter pack
Douglas Gregor2e774c42011-01-04 18:56:13 +00002334 Diagnostic &Diags = Context.getDiags();
2335 unsigned DiagID = Diags.getCustomDiagID(Diagnostic::Error,
2336 "cannot mangle sizeof...(function parameter pack)");
2337 Diags.Report(DiagID);
2338 return;
2339 }
Douglas Gregordfbbcf92011-03-03 02:20:19 +00002340 break;
Douglas Gregor2e774c42011-01-04 18:56:13 +00002341 }
Anders Carlssond553f8c2009-09-21 01:21:10 +00002342 }
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00002343}
2344
Anders Carlsson3ac86b52009-04-15 05:36:58 +00002345void CXXNameMangler::mangleCXXCtorType(CXXCtorType T) {
2346 // <ctor-dtor-name> ::= C1 # complete object constructor
2347 // ::= C2 # base object constructor
2348 // ::= C3 # complete object allocating constructor
2349 //
2350 switch (T) {
2351 case Ctor_Complete:
2352 Out << "C1";
2353 break;
2354 case Ctor_Base:
2355 Out << "C2";
2356 break;
2357 case Ctor_CompleteAllocating:
2358 Out << "C3";
2359 break;
2360 }
2361}
2362
Anders Carlsson27ae5362009-04-17 01:58:57 +00002363void CXXNameMangler::mangleCXXDtorType(CXXDtorType T) {
2364 // <ctor-dtor-name> ::= D0 # deleting destructor
2365 // ::= D1 # complete object destructor
2366 // ::= D2 # base object destructor
2367 //
2368 switch (T) {
2369 case Dtor_Deleting:
2370 Out << "D0";
2371 break;
2372 case Dtor_Complete:
2373 Out << "D1";
2374 break;
2375 case Dtor_Base:
2376 Out << "D2";
2377 break;
2378 }
2379}
2380
John McCall6dbce192010-08-20 00:17:19 +00002381void CXXNameMangler::mangleTemplateArgs(
2382 const ExplicitTemplateArgumentList &TemplateArgs) {
2383 // <template-args> ::= I <template-arg>+ E
2384 Out << 'I';
2385 for (unsigned I = 0, E = TemplateArgs.NumTemplateArgs; I != E; ++I)
2386 mangleTemplateArg(0, TemplateArgs.getTemplateArgs()[I].getArgument());
2387 Out << 'E';
2388}
2389
Douglas Gregor20f0cc72010-04-23 03:10:43 +00002390void CXXNameMangler::mangleTemplateArgs(TemplateName Template,
2391 const TemplateArgument *TemplateArgs,
2392 unsigned NumTemplateArgs) {
2393 if (TemplateDecl *TD = Template.getAsTemplateDecl())
2394 return mangleTemplateArgs(*TD->getTemplateParameters(), TemplateArgs,
2395 NumTemplateArgs);
Sean Huntc3021132010-05-05 15:23:54 +00002396
Douglas Gregor20f0cc72010-04-23 03:10:43 +00002397 // <template-args> ::= I <template-arg>+ E
2398 Out << 'I';
2399 for (unsigned i = 0; i != NumTemplateArgs; ++i)
2400 mangleTemplateArg(0, TemplateArgs[i]);
2401 Out << 'E';
2402}
2403
Rafael Espindolad9800722010-03-11 14:07:00 +00002404void CXXNameMangler::mangleTemplateArgs(const TemplateParameterList &PL,
2405 const TemplateArgumentList &AL) {
Anders Carlsson7a0ba872009-05-15 16:09:15 +00002406 // <template-args> ::= I <template-arg>+ E
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002407 Out << 'I';
Rafael Espindolad9800722010-03-11 14:07:00 +00002408 for (unsigned i = 0, e = AL.size(); i != e; ++i)
2409 mangleTemplateArg(PL.getParam(i), AL[i]);
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002410 Out << 'E';
Anders Carlsson7a0ba872009-05-15 16:09:15 +00002411}
2412
Rafael Espindolad9800722010-03-11 14:07:00 +00002413void CXXNameMangler::mangleTemplateArgs(const TemplateParameterList &PL,
2414 const TemplateArgument *TemplateArgs,
Anders Carlsson7624f212009-09-18 02:42:01 +00002415 unsigned NumTemplateArgs) {
2416 // <template-args> ::= I <template-arg>+ E
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002417 Out << 'I';
Daniel Dunbar7e0c1952009-11-21 09:17:15 +00002418 for (unsigned i = 0; i != NumTemplateArgs; ++i)
Rafael Espindolad9800722010-03-11 14:07:00 +00002419 mangleTemplateArg(PL.getParam(i), TemplateArgs[i]);
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002420 Out << 'E';
Anders Carlsson7624f212009-09-18 02:42:01 +00002421}
2422
Rafael Espindolad9800722010-03-11 14:07:00 +00002423void CXXNameMangler::mangleTemplateArg(const NamedDecl *P,
2424 const TemplateArgument &A) {
Mike Stump1eb44332009-09-09 15:08:12 +00002425 // <template-arg> ::= <type> # type or template
Anders Carlsson7a0ba872009-05-15 16:09:15 +00002426 // ::= X <expression> E # expression
2427 // ::= <expr-primary> # simple expressions
Douglas Gregor4fc48662011-01-13 16:39:34 +00002428 // ::= J <template-arg>* E # argument pack
Anders Carlsson7a0ba872009-05-15 16:09:15 +00002429 // ::= sp <expression> # pack expansion of (C++0x)
2430 switch (A.getKind()) {
Douglas Gregorf90b27a2011-01-03 22:36:02 +00002431 case TemplateArgument::Null:
2432 llvm_unreachable("Cannot mangle NULL template argument");
2433
Anders Carlsson7a0ba872009-05-15 16:09:15 +00002434 case TemplateArgument::Type:
2435 mangleType(A.getAsType());
2436 break;
Anders Carlsson9e85c742009-12-23 19:30:55 +00002437 case TemplateArgument::Template:
John McCallb6f532e2010-07-14 06:43:17 +00002438 // This is mangled as <type>.
2439 mangleType(A.getAsTemplate());
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002440 break;
Douglas Gregora7fc9012011-01-05 18:58:31 +00002441 case TemplateArgument::TemplateExpansion:
Douglas Gregor4fc48662011-01-13 16:39:34 +00002442 // <type> ::= Dp <type> # pack expansion (C++0x)
Douglas Gregora7fc9012011-01-05 18:58:31 +00002443 Out << "Dp";
2444 mangleType(A.getAsTemplateOrTemplatePattern());
2445 break;
Anders Carlssond553f8c2009-09-21 01:21:10 +00002446 case TemplateArgument::Expression:
2447 Out << 'X';
2448 mangleExpression(A.getAsExpr());
2449 Out << 'E';
2450 break;
Anders Carlssone170ba72009-12-14 01:45:37 +00002451 case TemplateArgument::Integral:
2452 mangleIntegerLiteral(A.getIntegralType(), *A.getAsIntegral());
Anders Carlsson7a0ba872009-05-15 16:09:15 +00002453 break;
Daniel Dunbar7e0c1952009-11-21 09:17:15 +00002454 case TemplateArgument::Declaration: {
Douglas Gregor20f0cc72010-04-23 03:10:43 +00002455 assert(P && "Missing template parameter for declaration argument");
Daniel Dunbar7e0c1952009-11-21 09:17:15 +00002456 // <expr-primary> ::= L <mangled-name> E # external name
2457
Rafael Espindolad9800722010-03-11 14:07:00 +00002458 // Clang produces AST's where pointer-to-member-function expressions
Daniel Dunbar7e0c1952009-11-21 09:17:15 +00002459 // and pointer-to-function expressions are represented as a declaration not
Rafael Espindolad9800722010-03-11 14:07:00 +00002460 // an expression. We compensate for it here to produce the correct mangling.
2461 NamedDecl *D = cast<NamedDecl>(A.getAsDecl());
2462 const NonTypeTemplateParmDecl *Parameter = cast<NonTypeTemplateParmDecl>(P);
John McCallc0a45592011-04-24 08:43:07 +00002463 bool compensateMangling = !Parameter->getType()->isReferenceType();
Rafael Espindolad9800722010-03-11 14:07:00 +00002464 if (compensateMangling) {
2465 Out << 'X';
2466 mangleOperatorName(OO_Amp, 1);
2467 }
2468
Daniel Dunbar7e0c1952009-11-21 09:17:15 +00002469 Out << 'L';
2470 // References to external entities use the mangled name; if the name would
2471 // not normally be manged then mangle it as unqualified.
2472 //
2473 // FIXME: The ABI specifies that external names here should have _Z, but
2474 // gcc leaves this off.
Rafael Espindolad9800722010-03-11 14:07:00 +00002475 if (compensateMangling)
2476 mangle(D, "_Z");
2477 else
2478 mangle(D, "Z");
Daniel Dunbar7e0c1952009-11-21 09:17:15 +00002479 Out << 'E';
Rafael Espindolad9800722010-03-11 14:07:00 +00002480
2481 if (compensateMangling)
2482 Out << 'E';
2483
Daniel Dunbar7e0c1952009-11-21 09:17:15 +00002484 break;
2485 }
Douglas Gregorf90b27a2011-01-03 22:36:02 +00002486
2487 case TemplateArgument::Pack: {
2488 // Note: proposal by Mike Herrick on 12/20/10
2489 Out << 'J';
2490 for (TemplateArgument::pack_iterator PA = A.pack_begin(),
2491 PAEnd = A.pack_end();
2492 PA != PAEnd; ++PA)
2493 mangleTemplateArg(P, *PA);
2494 Out << 'E';
2495 }
Daniel Dunbar7e0c1952009-11-21 09:17:15 +00002496 }
Anders Carlsson7a0ba872009-05-15 16:09:15 +00002497}
2498
Anders Carlsson0ccdf8d2009-09-27 00:38:53 +00002499void CXXNameMangler::mangleTemplateParameter(unsigned Index) {
2500 // <template-param> ::= T_ # first template parameter
2501 // ::= T <parameter-2 non-negative number> _
2502 if (Index == 0)
2503 Out << "T_";
2504 else
2505 Out << 'T' << (Index - 1) << '_';
2506}
2507
Anders Carlsson76967372009-09-17 00:43:46 +00002508// <substitution> ::= S <seq-id> _
2509// ::= S_
Anders Carlsson6862fc72009-09-17 04:16:28 +00002510bool CXXNameMangler::mangleSubstitution(const NamedDecl *ND) {
Anders Carlssone7c8cb62009-09-26 20:53:44 +00002511 // Try one of the standard substitutions first.
2512 if (mangleStandardSubstitution(ND))
2513 return true;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002514
Anders Carlsson433d1372009-11-07 04:26:04 +00002515 ND = cast<NamedDecl>(ND->getCanonicalDecl());
Anders Carlsson6862fc72009-09-17 04:16:28 +00002516 return mangleSubstitution(reinterpret_cast<uintptr_t>(ND));
2517}
2518
Anders Carlsson76967372009-09-17 00:43:46 +00002519bool CXXNameMangler::mangleSubstitution(QualType T) {
Anders Carlssond99edc42009-09-26 03:55:37 +00002520 if (!T.getCVRQualifiers()) {
2521 if (const RecordType *RT = T->getAs<RecordType>())
2522 return mangleSubstitution(RT->getDecl());
2523 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002524
Anders Carlsson76967372009-09-17 00:43:46 +00002525 uintptr_t TypePtr = reinterpret_cast<uintptr_t>(T.getAsOpaquePtr());
2526
Anders Carlssond3a932a2009-09-17 03:53:28 +00002527 return mangleSubstitution(TypePtr);
2528}
2529
Douglas Gregor1e9268e2010-04-28 05:58:56 +00002530bool CXXNameMangler::mangleSubstitution(TemplateName Template) {
2531 if (TemplateDecl *TD = Template.getAsTemplateDecl())
2532 return mangleSubstitution(TD);
Sean Huntc3021132010-05-05 15:23:54 +00002533
Douglas Gregor1e9268e2010-04-28 05:58:56 +00002534 Template = Context.getASTContext().getCanonicalTemplateName(Template);
2535 return mangleSubstitution(
2536 reinterpret_cast<uintptr_t>(Template.getAsVoidPointer()));
2537}
2538
Anders Carlssond3a932a2009-09-17 03:53:28 +00002539bool CXXNameMangler::mangleSubstitution(uintptr_t Ptr) {
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002540 llvm::DenseMap<uintptr_t, unsigned>::iterator I = Substitutions.find(Ptr);
Anders Carlsson76967372009-09-17 00:43:46 +00002541 if (I == Substitutions.end())
2542 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002543
Anders Carlsson76967372009-09-17 00:43:46 +00002544 unsigned SeqID = I->second;
2545 if (SeqID == 0)
2546 Out << "S_";
2547 else {
2548 SeqID--;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002549
Anders Carlsson76967372009-09-17 00:43:46 +00002550 // <seq-id> is encoded in base-36, using digits and upper case letters.
2551 char Buffer[10];
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002552 char *BufferPtr = llvm::array_endof(Buffer);
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002553
Anders Carlsson76967372009-09-17 00:43:46 +00002554 if (SeqID == 0) *--BufferPtr = '0';
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002555
Anders Carlsson76967372009-09-17 00:43:46 +00002556 while (SeqID) {
2557 assert(BufferPtr > Buffer && "Buffer overflow!");
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002558
John McCall6ab30e02010-06-09 07:26:17 +00002559 char c = static_cast<char>(SeqID % 36);
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002560
Anders Carlsson76967372009-09-17 00:43:46 +00002561 *--BufferPtr = (c < 10 ? '0' + c : 'A' + c - 10);
2562 SeqID /= 36;
2563 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002564
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002565 Out << 'S'
2566 << llvm::StringRef(BufferPtr, llvm::array_endof(Buffer)-BufferPtr)
2567 << '_';
Anders Carlsson76967372009-09-17 00:43:46 +00002568 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002569
Anders Carlsson76967372009-09-17 00:43:46 +00002570 return true;
2571}
2572
Anders Carlssonf514b542009-09-27 00:12:57 +00002573static bool isCharType(QualType T) {
2574 if (T.isNull())
2575 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002576
Anders Carlssonf514b542009-09-27 00:12:57 +00002577 return T->isSpecificBuiltinType(BuiltinType::Char_S) ||
2578 T->isSpecificBuiltinType(BuiltinType::Char_U);
2579}
2580
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002581/// isCharSpecialization - Returns whether a given type is a template
Anders Carlssonf514b542009-09-27 00:12:57 +00002582/// specialization of a given name with a single argument of type char.
2583static bool isCharSpecialization(QualType T, const char *Name) {
2584 if (T.isNull())
2585 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002586
Anders Carlssonf514b542009-09-27 00:12:57 +00002587 const RecordType *RT = T->getAs<RecordType>();
2588 if (!RT)
2589 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002590
2591 const ClassTemplateSpecializationDecl *SD =
Anders Carlssonf514b542009-09-27 00:12:57 +00002592 dyn_cast<ClassTemplateSpecializationDecl>(RT->getDecl());
2593 if (!SD)
2594 return false;
2595
2596 if (!isStdNamespace(SD->getDeclContext()))
2597 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002598
Anders Carlssonf514b542009-09-27 00:12:57 +00002599 const TemplateArgumentList &TemplateArgs = SD->getTemplateArgs();
2600 if (TemplateArgs.size() != 1)
2601 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002602
Anders Carlssonf514b542009-09-27 00:12:57 +00002603 if (!isCharType(TemplateArgs[0].getAsType()))
2604 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002605
Daniel Dunbar01eb9b92009-10-18 21:17:35 +00002606 return SD->getIdentifier()->getName() == Name;
Anders Carlssonf514b542009-09-27 00:12:57 +00002607}
2608
Anders Carlsson91f88602009-12-07 19:56:42 +00002609template <std::size_t StrLen>
Benjamin Kramer54353f42010-11-25 18:29:30 +00002610static bool isStreamCharSpecialization(const ClassTemplateSpecializationDecl*SD,
2611 const char (&Str)[StrLen]) {
Anders Carlsson91f88602009-12-07 19:56:42 +00002612 if (!SD->getIdentifier()->isStr(Str))
2613 return false;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002614
Anders Carlsson91f88602009-12-07 19:56:42 +00002615 const TemplateArgumentList &TemplateArgs = SD->getTemplateArgs();
2616 if (TemplateArgs.size() != 2)
2617 return false;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002618
Anders Carlsson91f88602009-12-07 19:56:42 +00002619 if (!isCharType(TemplateArgs[0].getAsType()))
2620 return false;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002621
Anders Carlsson91f88602009-12-07 19:56:42 +00002622 if (!isCharSpecialization(TemplateArgs[1].getAsType(), "char_traits"))
2623 return false;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002624
Anders Carlsson91f88602009-12-07 19:56:42 +00002625 return true;
2626}
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002627
Anders Carlssone7c8cb62009-09-26 20:53:44 +00002628bool CXXNameMangler::mangleStandardSubstitution(const NamedDecl *ND) {
2629 // <substitution> ::= St # ::std::
Anders Carlsson8c031552009-09-26 23:10:05 +00002630 if (const NamespaceDecl *NS = dyn_cast<NamespaceDecl>(ND)) {
Anders Carlsson47846d22009-12-04 06:23:23 +00002631 if (isStd(NS)) {
Anders Carlsson8c031552009-09-26 23:10:05 +00002632 Out << "St";
2633 return true;
2634 }
2635 }
2636
2637 if (const ClassTemplateDecl *TD = dyn_cast<ClassTemplateDecl>(ND)) {
2638 if (!isStdNamespace(TD->getDeclContext()))
2639 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002640
Anders Carlsson8c031552009-09-26 23:10:05 +00002641 // <substitution> ::= Sa # ::std::allocator
2642 if (TD->getIdentifier()->isStr("allocator")) {
2643 Out << "Sa";
2644 return true;
2645 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002646
Anders Carlsson189d59c2009-09-26 23:14:39 +00002647 // <<substitution> ::= Sb # ::std::basic_string
2648 if (TD->getIdentifier()->isStr("basic_string")) {
2649 Out << "Sb";
2650 return true;
2651 }
Anders Carlsson8c031552009-09-26 23:10:05 +00002652 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002653
2654 if (const ClassTemplateSpecializationDecl *SD =
Anders Carlssonf514b542009-09-27 00:12:57 +00002655 dyn_cast<ClassTemplateSpecializationDecl>(ND)) {
Eli Friedman5370ee22010-02-23 18:25:09 +00002656 if (!isStdNamespace(SD->getDeclContext()))
2657 return false;
2658
Anders Carlssonf514b542009-09-27 00:12:57 +00002659 // <substitution> ::= Ss # ::std::basic_string<char,
2660 // ::std::char_traits<char>,
2661 // ::std::allocator<char> >
2662 if (SD->getIdentifier()->isStr("basic_string")) {
2663 const TemplateArgumentList &TemplateArgs = SD->getTemplateArgs();
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002664
Anders Carlssonf514b542009-09-27 00:12:57 +00002665 if (TemplateArgs.size() != 3)
2666 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002667
Anders Carlssonf514b542009-09-27 00:12:57 +00002668 if (!isCharType(TemplateArgs[0].getAsType()))
2669 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002670
Anders Carlssonf514b542009-09-27 00:12:57 +00002671 if (!isCharSpecialization(TemplateArgs[1].getAsType(), "char_traits"))
2672 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002673
Anders Carlssonf514b542009-09-27 00:12:57 +00002674 if (!isCharSpecialization(TemplateArgs[2].getAsType(), "allocator"))
2675 return false;
2676
2677 Out << "Ss";
2678 return true;
2679 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002680
Anders Carlsson91f88602009-12-07 19:56:42 +00002681 // <substitution> ::= Si # ::std::basic_istream<char,
2682 // ::std::char_traits<char> >
2683 if (isStreamCharSpecialization(SD, "basic_istream")) {
2684 Out << "Si";
2685 return true;
2686 }
2687
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002688 // <substitution> ::= So # ::std::basic_ostream<char,
Anders Carlsson8f8fd8e2009-10-08 17:20:26 +00002689 // ::std::char_traits<char> >
Anders Carlsson91f88602009-12-07 19:56:42 +00002690 if (isStreamCharSpecialization(SD, "basic_ostream")) {
Anders Carlsson8f8fd8e2009-10-08 17:20:26 +00002691 Out << "So";
2692 return true;
2693 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002694
Anders Carlsson91f88602009-12-07 19:56:42 +00002695 // <substitution> ::= Sd # ::std::basic_iostream<char,
2696 // ::std::char_traits<char> >
2697 if (isStreamCharSpecialization(SD, "basic_iostream")) {
2698 Out << "Sd";
2699 return true;
2700 }
Anders Carlssonf514b542009-09-27 00:12:57 +00002701 }
Anders Carlsson8c031552009-09-26 23:10:05 +00002702 return false;
Anders Carlssone7c8cb62009-09-26 20:53:44 +00002703}
2704
Anders Carlsson76967372009-09-17 00:43:46 +00002705void CXXNameMangler::addSubstitution(QualType T) {
Anders Carlssond99edc42009-09-26 03:55:37 +00002706 if (!T.getCVRQualifiers()) {
2707 if (const RecordType *RT = T->getAs<RecordType>()) {
2708 addSubstitution(RT->getDecl());
2709 return;
2710 }
2711 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002712
Anders Carlsson76967372009-09-17 00:43:46 +00002713 uintptr_t TypePtr = reinterpret_cast<uintptr_t>(T.getAsOpaquePtr());
Anders Carlssond3a932a2009-09-17 03:53:28 +00002714 addSubstitution(TypePtr);
2715}
2716
Douglas Gregor1e9268e2010-04-28 05:58:56 +00002717void CXXNameMangler::addSubstitution(TemplateName Template) {
2718 if (TemplateDecl *TD = Template.getAsTemplateDecl())
2719 return addSubstitution(TD);
Sean Huntc3021132010-05-05 15:23:54 +00002720
Douglas Gregor1e9268e2010-04-28 05:58:56 +00002721 Template = Context.getASTContext().getCanonicalTemplateName(Template);
2722 addSubstitution(reinterpret_cast<uintptr_t>(Template.getAsVoidPointer()));
2723}
2724
Anders Carlssond3a932a2009-09-17 03:53:28 +00002725void CXXNameMangler::addSubstitution(uintptr_t Ptr) {
Anders Carlssond3a932a2009-09-17 03:53:28 +00002726 assert(!Substitutions.count(Ptr) && "Substitution already exists!");
Anders Carlsson9d85b722010-06-02 04:29:50 +00002727 Substitutions[Ptr] = SeqID++;
Anders Carlsson76967372009-09-17 00:43:46 +00002728}
2729
Daniel Dunbar1b077112009-11-21 09:06:10 +00002730//
Mike Stump1eb44332009-09-09 15:08:12 +00002731
Daniel Dunbar1b077112009-11-21 09:06:10 +00002732/// \brief Mangles the name of the declaration D and emits that name to the
2733/// given output stream.
2734///
2735/// If the declaration D requires a mangled name, this routine will emit that
2736/// mangled name to \p os and return true. Otherwise, \p os will be unchanged
2737/// and this routine will return false. In this case, the caller should just
2738/// emit the identifier of the declaration (\c D->getIdentifier()) as its
2739/// name.
Peter Collingbourne14110472011-01-13 18:57:25 +00002740void ItaniumMangleContext::mangleName(const NamedDecl *D,
Rafael Espindola0e376a02011-02-11 01:41:00 +00002741 llvm::raw_ostream &Out) {
Daniel Dunbarc02ab4c2009-11-21 09:14:44 +00002742 assert((isa<FunctionDecl>(D) || isa<VarDecl>(D)) &&
2743 "Invalid mangleName() call, argument is not a variable or function!");
2744 assert(!isa<CXXConstructorDecl>(D) && !isa<CXXDestructorDecl>(D) &&
2745 "Invalid mangleName() call on 'structor decl!");
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002746
Daniel Dunbar1b077112009-11-21 09:06:10 +00002747 PrettyStackTraceDecl CrashInfo(D, SourceLocation(),
2748 getASTContext().getSourceManager(),
2749 "Mangling declaration");
Mike Stump1eb44332009-09-09 15:08:12 +00002750
Rafael Espindolac4850c22011-02-10 23:59:36 +00002751 CXXNameMangler Mangler(*this, Out);
Daniel Dunbar94fd26d2009-11-21 09:06:22 +00002752 return Mangler.mangle(D);
Daniel Dunbar1b077112009-11-21 09:06:10 +00002753}
Mike Stump1eb44332009-09-09 15:08:12 +00002754
Peter Collingbourne14110472011-01-13 18:57:25 +00002755void ItaniumMangleContext::mangleCXXCtor(const CXXConstructorDecl *D,
2756 CXXCtorType Type,
Rafael Espindola0e376a02011-02-11 01:41:00 +00002757 llvm::raw_ostream &Out) {
Rafael Espindolac4850c22011-02-10 23:59:36 +00002758 CXXNameMangler Mangler(*this, Out, D, Type);
Daniel Dunbar77939c92009-11-21 09:06:31 +00002759 Mangler.mangle(D);
Daniel Dunbar1b077112009-11-21 09:06:10 +00002760}
Mike Stump1eb44332009-09-09 15:08:12 +00002761
Peter Collingbourne14110472011-01-13 18:57:25 +00002762void ItaniumMangleContext::mangleCXXDtor(const CXXDestructorDecl *D,
2763 CXXDtorType Type,
Rafael Espindola0e376a02011-02-11 01:41:00 +00002764 llvm::raw_ostream &Out) {
Rafael Espindolac4850c22011-02-10 23:59:36 +00002765 CXXNameMangler Mangler(*this, Out, D, Type);
Daniel Dunbar77939c92009-11-21 09:06:31 +00002766 Mangler.mangle(D);
Daniel Dunbar1b077112009-11-21 09:06:10 +00002767}
Mike Stumpf1216772009-07-31 18:25:34 +00002768
Peter Collingbourne14110472011-01-13 18:57:25 +00002769void ItaniumMangleContext::mangleThunk(const CXXMethodDecl *MD,
2770 const ThunkInfo &Thunk,
Rafael Espindolaf0be9792011-02-11 02:52:17 +00002771 llvm::raw_ostream &Out) {
Anders Carlsson19879c92010-03-23 17:17:29 +00002772 // <special-name> ::= T <call-offset> <base encoding>
2773 // # base is the nominal target function of thunk
2774 // <special-name> ::= Tc <call-offset> <call-offset> <base encoding>
2775 // # base is the nominal target function of thunk
2776 // # first call-offset is 'this' adjustment
2777 // # second call-offset is result adjustment
Sean Huntc3021132010-05-05 15:23:54 +00002778
Anders Carlsson19879c92010-03-23 17:17:29 +00002779 assert(!isa<CXXDestructorDecl>(MD) &&
2780 "Use mangleCXXDtor for destructor decls!");
Rafael Espindolac4850c22011-02-10 23:59:36 +00002781 CXXNameMangler Mangler(*this, Out);
Anders Carlsson19879c92010-03-23 17:17:29 +00002782 Mangler.getStream() << "_ZT";
2783 if (!Thunk.Return.isEmpty())
2784 Mangler.getStream() << 'c';
Sean Huntc3021132010-05-05 15:23:54 +00002785
Anders Carlsson19879c92010-03-23 17:17:29 +00002786 // Mangle the 'this' pointer adjustment.
2787 Mangler.mangleCallOffset(Thunk.This.NonVirtual, Thunk.This.VCallOffsetOffset);
Sean Huntc3021132010-05-05 15:23:54 +00002788
Anders Carlsson19879c92010-03-23 17:17:29 +00002789 // Mangle the return pointer adjustment if there is one.
2790 if (!Thunk.Return.isEmpty())
2791 Mangler.mangleCallOffset(Thunk.Return.NonVirtual,
2792 Thunk.Return.VBaseOffsetOffset);
Sean Huntc3021132010-05-05 15:23:54 +00002793
Anders Carlsson19879c92010-03-23 17:17:29 +00002794 Mangler.mangleFunctionEncoding(MD);
2795}
2796
Sean Huntc3021132010-05-05 15:23:54 +00002797void
Peter Collingbourne14110472011-01-13 18:57:25 +00002798ItaniumMangleContext::mangleCXXDtorThunk(const CXXDestructorDecl *DD,
2799 CXXDtorType Type,
2800 const ThisAdjustment &ThisAdjustment,
Rafael Espindolaf0be9792011-02-11 02:52:17 +00002801 llvm::raw_ostream &Out) {
Anders Carlsson19879c92010-03-23 17:17:29 +00002802 // <special-name> ::= T <call-offset> <base encoding>
2803 // # base is the nominal target function of thunk
Rafael Espindolac4850c22011-02-10 23:59:36 +00002804 CXXNameMangler Mangler(*this, Out, DD, Type);
Anders Carlsson19879c92010-03-23 17:17:29 +00002805 Mangler.getStream() << "_ZT";
2806
2807 // Mangle the 'this' pointer adjustment.
Sean Huntc3021132010-05-05 15:23:54 +00002808 Mangler.mangleCallOffset(ThisAdjustment.NonVirtual,
Anders Carlsson19879c92010-03-23 17:17:29 +00002809 ThisAdjustment.VCallOffsetOffset);
2810
2811 Mangler.mangleFunctionEncoding(DD);
2812}
2813
Daniel Dunbarc0747712009-11-21 09:12:13 +00002814/// mangleGuardVariable - Returns the mangled name for a guard variable
2815/// for the passed in VarDecl.
Peter Collingbourne14110472011-01-13 18:57:25 +00002816void ItaniumMangleContext::mangleItaniumGuardVariable(const VarDecl *D,
Rafael Espindolaf0be9792011-02-11 02:52:17 +00002817 llvm::raw_ostream &Out) {
Daniel Dunbarc0747712009-11-21 09:12:13 +00002818 // <special-name> ::= GV <object name> # Guard variable for one-time
2819 // # initialization
Rafael Espindolac4850c22011-02-10 23:59:36 +00002820 CXXNameMangler Mangler(*this, Out);
Daniel Dunbarc0747712009-11-21 09:12:13 +00002821 Mangler.getStream() << "_ZGV";
2822 Mangler.mangleName(D);
2823}
2824
Peter Collingbourne14110472011-01-13 18:57:25 +00002825void ItaniumMangleContext::mangleReferenceTemporary(const VarDecl *D,
Rafael Espindolaf0be9792011-02-11 02:52:17 +00002826 llvm::raw_ostream &Out) {
Anders Carlsson715edf22010-06-26 16:09:40 +00002827 // We match the GCC mangling here.
2828 // <special-name> ::= GR <object name>
Rafael Espindolac4850c22011-02-10 23:59:36 +00002829 CXXNameMangler Mangler(*this, Out);
Anders Carlsson715edf22010-06-26 16:09:40 +00002830 Mangler.getStream() << "_ZGR";
2831 Mangler.mangleName(D);
2832}
2833
Peter Collingbourne14110472011-01-13 18:57:25 +00002834void ItaniumMangleContext::mangleCXXVTable(const CXXRecordDecl *RD,
Rafael Espindolaf0be9792011-02-11 02:52:17 +00002835 llvm::raw_ostream &Out) {
Daniel Dunbarc0747712009-11-21 09:12:13 +00002836 // <special-name> ::= TV <type> # virtual table
Rafael Espindolac4850c22011-02-10 23:59:36 +00002837 CXXNameMangler Mangler(*this, Out);
Daniel Dunbarc0747712009-11-21 09:12:13 +00002838 Mangler.getStream() << "_ZTV";
Douglas Gregor1b12a3b2010-05-26 05:11:13 +00002839 Mangler.mangleNameOrStandardSubstitution(RD);
Daniel Dunbar1b077112009-11-21 09:06:10 +00002840}
Mike Stump82d75b02009-11-10 01:58:37 +00002841
Peter Collingbourne14110472011-01-13 18:57:25 +00002842void ItaniumMangleContext::mangleCXXVTT(const CXXRecordDecl *RD,
Rafael Espindolaf0be9792011-02-11 02:52:17 +00002843 llvm::raw_ostream &Out) {
Daniel Dunbarc0747712009-11-21 09:12:13 +00002844 // <special-name> ::= TT <type> # VTT structure
Rafael Espindolac4850c22011-02-10 23:59:36 +00002845 CXXNameMangler Mangler(*this, Out);
Daniel Dunbarc0747712009-11-21 09:12:13 +00002846 Mangler.getStream() << "_ZTT";
Douglas Gregor1b12a3b2010-05-26 05:11:13 +00002847 Mangler.mangleNameOrStandardSubstitution(RD);
Daniel Dunbar1b077112009-11-21 09:06:10 +00002848}
Mike Stumpab3f7e92009-11-10 01:41:59 +00002849
Peter Collingbourne14110472011-01-13 18:57:25 +00002850void ItaniumMangleContext::mangleCXXCtorVTable(const CXXRecordDecl *RD,
2851 int64_t Offset,
2852 const CXXRecordDecl *Type,
Rafael Espindolaf0be9792011-02-11 02:52:17 +00002853 llvm::raw_ostream &Out) {
Daniel Dunbarc0747712009-11-21 09:12:13 +00002854 // <special-name> ::= TC <type> <offset number> _ <base type>
Rafael Espindolac4850c22011-02-10 23:59:36 +00002855 CXXNameMangler Mangler(*this, Out);
Daniel Dunbarc0747712009-11-21 09:12:13 +00002856 Mangler.getStream() << "_ZTC";
Douglas Gregor1b12a3b2010-05-26 05:11:13 +00002857 Mangler.mangleNameOrStandardSubstitution(RD);
Daniel Dunbarc0747712009-11-21 09:12:13 +00002858 Mangler.getStream() << Offset;
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002859 Mangler.getStream() << '_';
Douglas Gregor1b12a3b2010-05-26 05:11:13 +00002860 Mangler.mangleNameOrStandardSubstitution(Type);
Daniel Dunbar1b077112009-11-21 09:06:10 +00002861}
Mike Stump738f8c22009-07-31 23:15:31 +00002862
Peter Collingbourne14110472011-01-13 18:57:25 +00002863void ItaniumMangleContext::mangleCXXRTTI(QualType Ty,
Rafael Espindolaf0be9792011-02-11 02:52:17 +00002864 llvm::raw_ostream &Out) {
Daniel Dunbarc0747712009-11-21 09:12:13 +00002865 // <special-name> ::= TI <type> # typeinfo structure
Douglas Gregor154fe982009-12-23 22:04:40 +00002866 assert(!Ty.hasQualifiers() && "RTTI info cannot have top-level qualifiers");
Rafael Espindolac4850c22011-02-10 23:59:36 +00002867 CXXNameMangler Mangler(*this, Out);
Daniel Dunbarc0747712009-11-21 09:12:13 +00002868 Mangler.getStream() << "_ZTI";
2869 Mangler.mangleType(Ty);
Daniel Dunbar1b077112009-11-21 09:06:10 +00002870}
Mike Stump67795982009-11-14 00:14:13 +00002871
Peter Collingbourne14110472011-01-13 18:57:25 +00002872void ItaniumMangleContext::mangleCXXRTTIName(QualType Ty,
Rafael Espindolaf0be9792011-02-11 02:52:17 +00002873 llvm::raw_ostream &Out) {
Daniel Dunbarc0747712009-11-21 09:12:13 +00002874 // <special-name> ::= TS <type> # typeinfo name (null terminated byte string)
Rafael Espindolac4850c22011-02-10 23:59:36 +00002875 CXXNameMangler Mangler(*this, Out);
Daniel Dunbarc0747712009-11-21 09:12:13 +00002876 Mangler.getStream() << "_ZTS";
2877 Mangler.mangleType(Ty);
Mike Stumpf1216772009-07-31 18:25:34 +00002878}
Peter Collingbourne14110472011-01-13 18:57:25 +00002879
2880MangleContext *clang::createItaniumMangleContext(ASTContext &Context,
2881 Diagnostic &Diags) {
2882 return new ItaniumMangleContext(Context, Diags);
2883}