blob: 04d705eb1ab8fb0a621fd6ca8346d311a334a919 [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 McCall5a7e6f72011-04-28 02:52:03 +00001852/// Look at the callee of the given call expression and determine if
1853/// it's a parenthesized id-expression which would have triggered ADL
1854/// otherwise.
1855static bool isParenthesizedADLCallee(const CallExpr *call) {
1856 const Expr *callee = call->getCallee();
1857 const Expr *fn = callee->IgnoreParens();
1858
1859 // Must be parenthesized. IgnoreParens() skips __extension__ nodes,
1860 // too, but for those to appear in the callee, it would have to be
1861 // parenthesized.
1862 if (callee == fn) return false;
1863
1864 // Must be an unresolved lookup.
1865 const UnresolvedLookupExpr *lookup = dyn_cast<UnresolvedLookupExpr>(fn);
1866 if (!lookup) return false;
1867
1868 assert(!lookup->requiresADL());
1869
1870 // Must be an unqualified lookup.
1871 if (lookup->getQualifier()) return false;
1872
1873 // Must not have found a class member. Note that if one is a class
1874 // member, they're all class members.
1875 if (lookup->getNumDecls() > 0 &&
1876 (*lookup->decls_begin())->isCXXClassMember())
1877 return false;
1878
1879 // Otherwise, ADL would have been triggered.
1880 return true;
1881}
1882
John McCall5e1e89b2010-08-18 19:18:59 +00001883void CXXNameMangler::mangleExpression(const Expr *E, unsigned Arity) {
Anders Carlssond553f8c2009-09-21 01:21:10 +00001884 // <expression> ::= <unary operator-name> <expression>
John McCall09cc1412010-02-03 00:55:45 +00001885 // ::= <binary operator-name> <expression> <expression>
1886 // ::= <trinary operator-name> <expression> <expression> <expression>
Anders Carlssond553f8c2009-09-21 01:21:10 +00001887 // ::= cv <type> expression # conversion with one argument
1888 // ::= cv <type> _ <expression>* E # conversion with a different number of arguments
Eli Friedmana7e68452010-08-22 01:00:03 +00001889 // ::= st <type> # sizeof (a type)
Anders Carlssond553f8c2009-09-21 01:21:10 +00001890 // ::= at <type> # alignof (a type)
1891 // ::= <template-param>
1892 // ::= <function-param>
1893 // ::= sr <type> <unqualified-name> # dependent name
1894 // ::= sr <type> <unqualified-name> <template-args> # dependent template-id
1895 // ::= sZ <template-param> # size of a parameter pack
Douglas Gregor4fc48662011-01-13 16:39:34 +00001896 // ::= sZ <function-param> # size of a function parameter pack
John McCall09cc1412010-02-03 00:55:45 +00001897 // ::= <expr-primary>
John McCall1dd73832010-02-04 01:42:13 +00001898 // <expr-primary> ::= L <type> <value number> E # integer literal
1899 // ::= L <type <value float> E # floating literal
1900 // ::= L <mangled-name> E # external name
Anders Carlssond553f8c2009-09-21 01:21:10 +00001901 switch (E->getStmtClass()) {
John McCall6ae1f352010-04-09 22:26:14 +00001902 case Expr::NoStmtClass:
John McCall63c00d72011-02-09 08:16:59 +00001903#define ABSTRACT_STMT(Type)
John McCall6ae1f352010-04-09 22:26:14 +00001904#define EXPR(Type, Base)
1905#define STMT(Type, Base) \
1906 case Expr::Type##Class:
Sean Hunt4bfe1962010-05-05 15:24:00 +00001907#include "clang/AST/StmtNodes.inc"
John McCall0512e482010-07-14 04:20:34 +00001908 // fallthrough
1909
1910 // These all can only appear in local or variable-initialization
1911 // contexts and so should never appear in a mangling.
1912 case Expr::AddrLabelExprClass:
1913 case Expr::BlockDeclRefExprClass:
1914 case Expr::CXXThisExprClass:
1915 case Expr::DesignatedInitExprClass:
1916 case Expr::ImplicitValueInitExprClass:
1917 case Expr::InitListExprClass:
1918 case Expr::ParenListExprClass:
1919 case Expr::CXXScalarValueInitExprClass:
John McCall09cc1412010-02-03 00:55:45 +00001920 llvm_unreachable("unexpected statement kind");
1921 break;
1922
John McCall0512e482010-07-14 04:20:34 +00001923 // FIXME: invent manglings for all these.
1924 case Expr::BlockExprClass:
1925 case Expr::CXXPseudoDestructorExprClass:
1926 case Expr::ChooseExprClass:
1927 case Expr::CompoundLiteralExprClass:
1928 case Expr::ExtVectorElementExprClass:
Peter Collingbournef111d932011-04-15 00:35:48 +00001929 case Expr::GenericSelectionExprClass:
John McCall0512e482010-07-14 04:20:34 +00001930 case Expr::ObjCEncodeExprClass:
John McCall0512e482010-07-14 04:20:34 +00001931 case Expr::ObjCIsaExprClass:
1932 case Expr::ObjCIvarRefExprClass:
1933 case Expr::ObjCMessageExprClass:
1934 case Expr::ObjCPropertyRefExprClass:
1935 case Expr::ObjCProtocolExprClass:
1936 case Expr::ObjCSelectorExprClass:
1937 case Expr::ObjCStringLiteralClass:
John McCall0512e482010-07-14 04:20:34 +00001938 case Expr::OffsetOfExprClass:
1939 case Expr::PredefinedExprClass:
1940 case Expr::ShuffleVectorExprClass:
1941 case Expr::StmtExprClass:
John McCall0512e482010-07-14 04:20:34 +00001942 case Expr::UnaryTypeTraitExprClass:
Francois Pichet6ad6f282010-12-07 00:08:36 +00001943 case Expr::BinaryTypeTraitExprClass:
John Wiegley21ff2e52011-04-28 00:16:57 +00001944 case Expr::ArrayTypeTraitExprClass:
John Wiegley55262202011-04-25 06:54:41 +00001945 case Expr::ExpressionTraitExprClass:
Francois Pichet9be88402010-09-08 23:47:05 +00001946 case Expr::VAArgExprClass:
Sebastian Redl2e156222010-09-10 20:55:43 +00001947 case Expr::CXXUuidofExprClass:
Peter Collingbournee08ce652011-02-09 21:07:24 +00001948 case Expr::CXXNoexceptExprClass:
1949 case Expr::CUDAKernelCallExprClass: {
John McCall6ae1f352010-04-09 22:26:14 +00001950 // As bad as this diagnostic is, it's better than crashing.
1951 Diagnostic &Diags = Context.getDiags();
1952 unsigned DiagID = Diags.getCustomDiagID(Diagnostic::Error,
1953 "cannot yet mangle expression type %0");
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +00001954 Diags.Report(E->getExprLoc(), DiagID)
John McCall739bf092010-04-10 09:39:25 +00001955 << E->getStmtClassName() << E->getSourceRange();
John McCall6ae1f352010-04-09 22:26:14 +00001956 break;
1957 }
1958
John McCall56ca35d2011-02-17 10:25:35 +00001959 // Even gcc-4.5 doesn't mangle this.
1960 case Expr::BinaryConditionalOperatorClass: {
1961 Diagnostic &Diags = Context.getDiags();
1962 unsigned DiagID =
1963 Diags.getCustomDiagID(Diagnostic::Error,
1964 "?: operator with omitted middle operand cannot be mangled");
1965 Diags.Report(E->getExprLoc(), DiagID)
1966 << E->getStmtClassName() << E->getSourceRange();
1967 break;
1968 }
1969
1970 // These are used for internal purposes and cannot be meaningfully mangled.
John McCall7cd7d1a2010-11-15 23:31:06 +00001971 case Expr::OpaqueValueExprClass:
1972 llvm_unreachable("cannot mangle opaque value; mangling wrong thing?");
1973
John McCall0512e482010-07-14 04:20:34 +00001974 case Expr::CXXDefaultArgExprClass:
John McCall5e1e89b2010-08-18 19:18:59 +00001975 mangleExpression(cast<CXXDefaultArgExpr>(E)->getExpr(), Arity);
John McCall0512e482010-07-14 04:20:34 +00001976 break;
1977
1978 case Expr::CXXMemberCallExprClass: // fallthrough
John McCall1dd73832010-02-04 01:42:13 +00001979 case Expr::CallExprClass: {
1980 const CallExpr *CE = cast<CallExpr>(E);
John McCall5a7e6f72011-04-28 02:52:03 +00001981
1982 // <expression> ::= cp <simple-id> <expression>* E
1983 // We use this mangling only when the call would use ADL except
1984 // for being parenthesized. Per discussion with David
1985 // Vandervoorde, 2011.04.25.
1986 if (isParenthesizedADLCallee(CE)) {
1987 Out << "cp";
1988 // The callee here is a parenthesized UnresolvedLookupExpr with
1989 // no qualifier and should always get mangled as a <simple-id>
1990 // anyway.
1991
1992 // <expression> ::= cl <expression>* E
1993 } else {
1994 Out << "cl";
1995 }
1996
John McCall5e1e89b2010-08-18 19:18:59 +00001997 mangleExpression(CE->getCallee(), CE->getNumArgs());
John McCall1dd73832010-02-04 01:42:13 +00001998 for (unsigned I = 0, N = CE->getNumArgs(); I != N; ++I)
1999 mangleExpression(CE->getArg(I));
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002000 Out << 'E';
John McCall09cc1412010-02-03 00:55:45 +00002001 break;
John McCall1dd73832010-02-04 01:42:13 +00002002 }
John McCall09cc1412010-02-03 00:55:45 +00002003
John McCall0512e482010-07-14 04:20:34 +00002004 case Expr::CXXNewExprClass: {
2005 // Proposal from David Vandervoorde, 2010.06.30
2006 const CXXNewExpr *New = cast<CXXNewExpr>(E);
2007 if (New->isGlobalNew()) Out << "gs";
2008 Out << (New->isArray() ? "na" : "nw");
2009 for (CXXNewExpr::const_arg_iterator I = New->placement_arg_begin(),
2010 E = New->placement_arg_end(); I != E; ++I)
2011 mangleExpression(*I);
2012 Out << '_';
2013 mangleType(New->getAllocatedType());
2014 if (New->hasInitializer()) {
2015 Out << "pi";
2016 for (CXXNewExpr::const_arg_iterator I = New->constructor_arg_begin(),
2017 E = New->constructor_arg_end(); I != E; ++I)
2018 mangleExpression(*I);
2019 }
2020 Out << 'E';
2021 break;
2022 }
2023
John McCall2f27bf82010-02-04 02:56:29 +00002024 case Expr::MemberExprClass: {
2025 const MemberExpr *ME = cast<MemberExpr>(E);
2026 mangleMemberExpr(ME->getBase(), ME->isArrow(),
John McCalla0ce15c2011-04-24 08:23:24 +00002027 ME->getQualifier(), 0, ME->getMemberDecl()->getDeclName(),
John McCall5e1e89b2010-08-18 19:18:59 +00002028 Arity);
John McCall2f27bf82010-02-04 02:56:29 +00002029 break;
2030 }
2031
2032 case Expr::UnresolvedMemberExprClass: {
2033 const UnresolvedMemberExpr *ME = cast<UnresolvedMemberExpr>(E);
2034 mangleMemberExpr(ME->getBase(), ME->isArrow(),
John McCalla0ce15c2011-04-24 08:23:24 +00002035 ME->getQualifier(), 0, ME->getMemberName(),
John McCall5e1e89b2010-08-18 19:18:59 +00002036 Arity);
John McCall6dbce192010-08-20 00:17:19 +00002037 if (ME->hasExplicitTemplateArgs())
2038 mangleTemplateArgs(ME->getExplicitTemplateArgs());
John McCall2f27bf82010-02-04 02:56:29 +00002039 break;
2040 }
2041
2042 case Expr::CXXDependentScopeMemberExprClass: {
2043 const CXXDependentScopeMemberExpr *ME
2044 = cast<CXXDependentScopeMemberExpr>(E);
2045 mangleMemberExpr(ME->getBase(), ME->isArrow(),
John McCalla0ce15c2011-04-24 08:23:24 +00002046 ME->getQualifier(), ME->getFirstQualifierFoundInScope(),
2047 ME->getMember(), Arity);
John McCall6dbce192010-08-20 00:17:19 +00002048 if (ME->hasExplicitTemplateArgs())
2049 mangleTemplateArgs(ME->getExplicitTemplateArgs());
John McCall2f27bf82010-02-04 02:56:29 +00002050 break;
2051 }
2052
John McCall1dd73832010-02-04 01:42:13 +00002053 case Expr::UnresolvedLookupExprClass: {
2054 const UnresolvedLookupExpr *ULE = cast<UnresolvedLookupExpr>(E);
John McCalla0ce15c2011-04-24 08:23:24 +00002055 mangleUnresolvedName(ULE->getQualifier(), 0, ULE->getName(), Arity);
John McCall6dbce192010-08-20 00:17:19 +00002056 if (ULE->hasExplicitTemplateArgs())
2057 mangleTemplateArgs(ULE->getExplicitTemplateArgs());
John McCall1dd73832010-02-04 01:42:13 +00002058 break;
2059 }
2060
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002061 case Expr::CXXUnresolvedConstructExprClass: {
John McCall1dd73832010-02-04 01:42:13 +00002062 const CXXUnresolvedConstructExpr *CE = cast<CXXUnresolvedConstructExpr>(E);
2063 unsigned N = CE->arg_size();
2064
2065 Out << "cv";
2066 mangleType(CE->getType());
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002067 if (N != 1) Out << '_';
John McCall1dd73832010-02-04 01:42:13 +00002068 for (unsigned I = 0; I != N; ++I) mangleExpression(CE->getArg(I));
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002069 if (N != 1) Out << 'E';
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002070 break;
John McCall1dd73832010-02-04 01:42:13 +00002071 }
John McCall09cc1412010-02-03 00:55:45 +00002072
John McCall1dd73832010-02-04 01:42:13 +00002073 case Expr::CXXTemporaryObjectExprClass:
2074 case Expr::CXXConstructExprClass: {
2075 const CXXConstructExpr *CE = cast<CXXConstructExpr>(E);
2076 unsigned N = CE->getNumArgs();
2077
2078 Out << "cv";
2079 mangleType(CE->getType());
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002080 if (N != 1) Out << '_';
John McCall1dd73832010-02-04 01:42:13 +00002081 for (unsigned I = 0; I != N; ++I) mangleExpression(CE->getArg(I));
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002082 if (N != 1) Out << 'E';
John McCall09cc1412010-02-03 00:55:45 +00002083 break;
John McCall1dd73832010-02-04 01:42:13 +00002084 }
2085
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +00002086 case Expr::UnaryExprOrTypeTraitExprClass: {
2087 const UnaryExprOrTypeTraitExpr *SAE = cast<UnaryExprOrTypeTraitExpr>(E);
2088 switch(SAE->getKind()) {
2089 case UETT_SizeOf:
2090 Out << 's';
2091 break;
2092 case UETT_AlignOf:
2093 Out << 'a';
2094 break;
2095 case UETT_VecStep:
2096 Diagnostic &Diags = Context.getDiags();
2097 unsigned DiagID = Diags.getCustomDiagID(Diagnostic::Error,
2098 "cannot yet mangle vec_step expression");
2099 Diags.Report(DiagID);
2100 return;
2101 }
John McCall1dd73832010-02-04 01:42:13 +00002102 if (SAE->isArgumentType()) {
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002103 Out << 't';
John McCall1dd73832010-02-04 01:42:13 +00002104 mangleType(SAE->getArgumentType());
2105 } else {
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002106 Out << 'z';
John McCall1dd73832010-02-04 01:42:13 +00002107 mangleExpression(SAE->getArgumentExpr());
2108 }
2109 break;
2110 }
Anders Carlssona7694082009-11-06 02:50:19 +00002111
John McCall0512e482010-07-14 04:20:34 +00002112 case Expr::CXXThrowExprClass: {
2113 const CXXThrowExpr *TE = cast<CXXThrowExpr>(E);
2114
2115 // Proposal from David Vandervoorde, 2010.06.30
2116 if (TE->getSubExpr()) {
2117 Out << "tw";
2118 mangleExpression(TE->getSubExpr());
2119 } else {
2120 Out << "tr";
2121 }
2122 break;
2123 }
2124
2125 case Expr::CXXTypeidExprClass: {
2126 const CXXTypeidExpr *TIE = cast<CXXTypeidExpr>(E);
2127
2128 // Proposal from David Vandervoorde, 2010.06.30
2129 if (TIE->isTypeOperand()) {
2130 Out << "ti";
2131 mangleType(TIE->getTypeOperand());
2132 } else {
2133 Out << "te";
2134 mangleExpression(TIE->getExprOperand());
2135 }
2136 break;
2137 }
2138
2139 case Expr::CXXDeleteExprClass: {
2140 const CXXDeleteExpr *DE = cast<CXXDeleteExpr>(E);
2141
2142 // Proposal from David Vandervoorde, 2010.06.30
2143 if (DE->isGlobalDelete()) Out << "gs";
2144 Out << (DE->isArrayForm() ? "da" : "dl");
2145 mangleExpression(DE->getArgument());
2146 break;
2147 }
2148
Anders Carlssone170ba72009-12-14 01:45:37 +00002149 case Expr::UnaryOperatorClass: {
2150 const UnaryOperator *UO = cast<UnaryOperator>(E);
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002151 mangleOperatorName(UnaryOperator::getOverloadedOperator(UO->getOpcode()),
Anders Carlssone170ba72009-12-14 01:45:37 +00002152 /*Arity=*/1);
2153 mangleExpression(UO->getSubExpr());
2154 break;
2155 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002156
John McCall0512e482010-07-14 04:20:34 +00002157 case Expr::ArraySubscriptExprClass: {
2158 const ArraySubscriptExpr *AE = cast<ArraySubscriptExpr>(E);
2159
Chris Lattnerfc8f0e12011-04-15 05:22:18 +00002160 // Array subscript is treated as a syntactically weird form of
John McCall0512e482010-07-14 04:20:34 +00002161 // binary operator.
2162 Out << "ix";
2163 mangleExpression(AE->getLHS());
2164 mangleExpression(AE->getRHS());
2165 break;
2166 }
2167
2168 case Expr::CompoundAssignOperatorClass: // fallthrough
Anders Carlssone170ba72009-12-14 01:45:37 +00002169 case Expr::BinaryOperatorClass: {
2170 const BinaryOperator *BO = cast<BinaryOperator>(E);
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002171 mangleOperatorName(BinaryOperator::getOverloadedOperator(BO->getOpcode()),
Anders Carlssone170ba72009-12-14 01:45:37 +00002172 /*Arity=*/2);
2173 mangleExpression(BO->getLHS());
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002174 mangleExpression(BO->getRHS());
Anders Carlssone170ba72009-12-14 01:45:37 +00002175 break;
John McCall2f27bf82010-02-04 02:56:29 +00002176 }
Anders Carlssone170ba72009-12-14 01:45:37 +00002177
2178 case Expr::ConditionalOperatorClass: {
2179 const ConditionalOperator *CO = cast<ConditionalOperator>(E);
2180 mangleOperatorName(OO_Conditional, /*Arity=*/3);
2181 mangleExpression(CO->getCond());
John McCall5e1e89b2010-08-18 19:18:59 +00002182 mangleExpression(CO->getLHS(), Arity);
2183 mangleExpression(CO->getRHS(), Arity);
Anders Carlssone170ba72009-12-14 01:45:37 +00002184 break;
2185 }
2186
Douglas Gregor46287c72010-01-29 16:37:09 +00002187 case Expr::ImplicitCastExprClass: {
John McCall5e1e89b2010-08-18 19:18:59 +00002188 mangleExpression(cast<ImplicitCastExpr>(E)->getSubExpr(), Arity);
Douglas Gregor46287c72010-01-29 16:37:09 +00002189 break;
2190 }
2191
2192 case Expr::CStyleCastExprClass:
2193 case Expr::CXXStaticCastExprClass:
2194 case Expr::CXXDynamicCastExprClass:
2195 case Expr::CXXReinterpretCastExprClass:
2196 case Expr::CXXConstCastExprClass:
2197 case Expr::CXXFunctionalCastExprClass: {
2198 const ExplicitCastExpr *ECE = cast<ExplicitCastExpr>(E);
2199 Out << "cv";
2200 mangleType(ECE->getType());
2201 mangleExpression(ECE->getSubExpr());
2202 break;
2203 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002204
Anders Carlsson58040a52009-12-16 05:48:46 +00002205 case Expr::CXXOperatorCallExprClass: {
2206 const CXXOperatorCallExpr *CE = cast<CXXOperatorCallExpr>(E);
2207 unsigned NumArgs = CE->getNumArgs();
2208 mangleOperatorName(CE->getOperator(), /*Arity=*/NumArgs);
2209 // Mangle the arguments.
2210 for (unsigned i = 0; i != NumArgs; ++i)
2211 mangleExpression(CE->getArg(i));
2212 break;
2213 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002214
Anders Carlssona7694082009-11-06 02:50:19 +00002215 case Expr::ParenExprClass:
John McCall5e1e89b2010-08-18 19:18:59 +00002216 mangleExpression(cast<ParenExpr>(E)->getSubExpr(), Arity);
Anders Carlssona7694082009-11-06 02:50:19 +00002217 break;
2218
Anders Carlssond553f8c2009-09-21 01:21:10 +00002219 case Expr::DeclRefExprClass: {
Douglas Gregor5ed1bc32010-02-28 21:40:32 +00002220 const NamedDecl *D = cast<DeclRefExpr>(E)->getDecl();
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002221
Anders Carlssond553f8c2009-09-21 01:21:10 +00002222 switch (D->getKind()) {
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002223 default:
Douglas Gregor5ed1bc32010-02-28 21:40:32 +00002224 // <expr-primary> ::= L <mangled-name> E # external name
2225 Out << 'L';
2226 mangle(D, "_Z");
2227 Out << 'E';
2228 break;
2229
John McCall3dc7e7b2010-07-24 01:17:35 +00002230 case Decl::EnumConstant: {
2231 const EnumConstantDecl *ED = cast<EnumConstantDecl>(D);
2232 mangleIntegerLiteral(ED->getType(), ED->getInitVal());
2233 break;
2234 }
2235
Anders Carlssond553f8c2009-09-21 01:21:10 +00002236 case Decl::NonTypeTemplateParm: {
2237 const NonTypeTemplateParmDecl *PD = cast<NonTypeTemplateParmDecl>(D);
Anders Carlsson0ccdf8d2009-09-27 00:38:53 +00002238 mangleTemplateParameter(PD->getIndex());
Anders Carlssond553f8c2009-09-21 01:21:10 +00002239 break;
2240 }
2241
2242 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002243
Anders Carlsson50755b02009-09-27 20:11:34 +00002244 break;
Anders Carlssond553f8c2009-09-21 01:21:10 +00002245 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002246
Douglas Gregorc7793c72011-01-15 01:15:58 +00002247 case Expr::SubstNonTypeTemplateParmPackExprClass:
2248 mangleTemplateParameter(
2249 cast<SubstNonTypeTemplateParmPackExpr>(E)->getParameterPack()->getIndex());
2250 break;
2251
John McCall865d4472009-11-19 22:55:06 +00002252 case Expr::DependentScopeDeclRefExprClass: {
2253 const DependentScopeDeclRefExpr *DRE = cast<DependentScopeDeclRefExpr>(E);
Douglas Gregor4b2ccfc2010-02-28 22:05:49 +00002254 NestedNameSpecifier *NNS = DRE->getQualifier();
2255 const Type *QTy = NNS->getAsType();
2256
2257 // When we're dealing with a nested-name-specifier that has just a
2258 // dependent identifier in it, mangle that as a typename. FIXME:
2259 // It isn't clear that we ever actually want to have such a
2260 // nested-name-specifier; why not just represent it as a typename type?
2261 if (!QTy && NNS->getAsIdentifier() && NNS->getPrefix()) {
Douglas Gregor4a2023f2010-03-31 20:19:30 +00002262 QTy = getASTContext().getDependentNameType(ETK_Typename,
2263 NNS->getPrefix(),
2264 NNS->getAsIdentifier())
Douglas Gregor4b2ccfc2010-02-28 22:05:49 +00002265 .getTypePtr();
2266 }
Anders Carlsson50755b02009-09-27 20:11:34 +00002267 assert(QTy && "Qualifier was not type!");
2268
John McCall6dbce192010-08-20 00:17:19 +00002269 // ::= sr <type> <unqualified-name> # dependent name
2270 // ::= sr <type> <unqualified-name> <template-args> # dependent template-id
Anders Carlsson50755b02009-09-27 20:11:34 +00002271 Out << "sr";
2272 mangleType(QualType(QTy, 0));
John McCall5e1e89b2010-08-18 19:18:59 +00002273 mangleUnqualifiedName(0, DRE->getDeclName(), Arity);
John McCall6dbce192010-08-20 00:17:19 +00002274 if (DRE->hasExplicitTemplateArgs())
2275 mangleTemplateArgs(DRE->getExplicitTemplateArgs());
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002276
Anders Carlsson50755b02009-09-27 20:11:34 +00002277 break;
2278 }
2279
John McCalld9307602010-04-09 22:54:09 +00002280 case Expr::CXXBindTemporaryExprClass:
2281 mangleExpression(cast<CXXBindTemporaryExpr>(E)->getSubExpr());
2282 break;
2283
John McCall4765fa02010-12-06 08:20:24 +00002284 case Expr::ExprWithCleanupsClass:
2285 mangleExpression(cast<ExprWithCleanups>(E)->getSubExpr(), Arity);
John McCalld9307602010-04-09 22:54:09 +00002286 break;
2287
John McCall1dd73832010-02-04 01:42:13 +00002288 case Expr::FloatingLiteralClass: {
2289 const FloatingLiteral *FL = cast<FloatingLiteral>(E);
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002290 Out << 'L';
John McCall1dd73832010-02-04 01:42:13 +00002291 mangleType(FL->getType());
John McCall0512e482010-07-14 04:20:34 +00002292 mangleFloat(FL->getValue());
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002293 Out << 'E';
John McCall1dd73832010-02-04 01:42:13 +00002294 break;
2295 }
2296
John McCallde810632010-04-09 21:48:08 +00002297 case Expr::CharacterLiteralClass:
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002298 Out << 'L';
John McCallde810632010-04-09 21:48:08 +00002299 mangleType(E->getType());
2300 Out << cast<CharacterLiteral>(E)->getValue();
2301 Out << 'E';
2302 break;
2303
2304 case Expr::CXXBoolLiteralExprClass:
2305 Out << "Lb";
2306 Out << (cast<CXXBoolLiteralExpr>(E)->getValue() ? '1' : '0');
2307 Out << 'E';
2308 break;
2309
John McCall0512e482010-07-14 04:20:34 +00002310 case Expr::IntegerLiteralClass: {
2311 llvm::APSInt Value(cast<IntegerLiteral>(E)->getValue());
2312 if (E->getType()->isSignedIntegerType())
2313 Value.setIsSigned(true);
2314 mangleIntegerLiteral(E->getType(), Value);
Anders Carlssone170ba72009-12-14 01:45:37 +00002315 break;
John McCall0512e482010-07-14 04:20:34 +00002316 }
2317
2318 case Expr::ImaginaryLiteralClass: {
2319 const ImaginaryLiteral *IE = cast<ImaginaryLiteral>(E);
2320 // Mangle as if a complex literal.
Nick Lewycky271b6652010-09-05 03:40:33 +00002321 // Proposal from David Vandevoorde, 2010.06.30.
John McCall0512e482010-07-14 04:20:34 +00002322 Out << 'L';
2323 mangleType(E->getType());
2324 if (const FloatingLiteral *Imag =
2325 dyn_cast<FloatingLiteral>(IE->getSubExpr())) {
2326 // Mangle a floating-point zero of the appropriate type.
2327 mangleFloat(llvm::APFloat(Imag->getValue().getSemantics()));
2328 Out << '_';
2329 mangleFloat(Imag->getValue());
2330 } else {
Nick Lewycky271b6652010-09-05 03:40:33 +00002331 Out << "0_";
John McCall0512e482010-07-14 04:20:34 +00002332 llvm::APSInt Value(cast<IntegerLiteral>(IE->getSubExpr())->getValue());
2333 if (IE->getSubExpr()->getType()->isSignedIntegerType())
2334 Value.setIsSigned(true);
2335 mangleNumber(Value);
2336 }
2337 Out << 'E';
2338 break;
2339 }
2340
2341 case Expr::StringLiteralClass: {
John McCall1658c392010-07-15 21:53:03 +00002342 // Revised proposal from David Vandervoorde, 2010.07.15.
John McCall0512e482010-07-14 04:20:34 +00002343 Out << 'L';
John McCall1658c392010-07-15 21:53:03 +00002344 assert(isa<ConstantArrayType>(E->getType()));
2345 mangleType(E->getType());
John McCall0512e482010-07-14 04:20:34 +00002346 Out << 'E';
2347 break;
2348 }
2349
2350 case Expr::GNUNullExprClass:
2351 // FIXME: should this really be mangled the same as nullptr?
2352 // fallthrough
2353
2354 case Expr::CXXNullPtrLiteralExprClass: {
2355 // Proposal from David Vandervoorde, 2010.06.30, as
2356 // modified by ABI list discussion.
2357 Out << "LDnE";
2358 break;
2359 }
Douglas Gregorbe230c32011-01-03 17:17:50 +00002360
2361 case Expr::PackExpansionExprClass:
2362 Out << "sp";
2363 mangleExpression(cast<PackExpansionExpr>(E)->getPattern());
2364 break;
Douglas Gregor2e774c42011-01-04 18:56:13 +00002365
2366 case Expr::SizeOfPackExprClass: {
Douglas Gregor2e774c42011-01-04 18:56:13 +00002367 Out << "sZ";
2368 const NamedDecl *Pack = cast<SizeOfPackExpr>(E)->getPack();
2369 if (const TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(Pack))
2370 mangleTemplateParameter(TTP->getIndex());
2371 else if (const NonTypeTemplateParmDecl *NTTP
2372 = dyn_cast<NonTypeTemplateParmDecl>(Pack))
2373 mangleTemplateParameter(NTTP->getIndex());
2374 else if (const TemplateTemplateParmDecl *TempTP
2375 = dyn_cast<TemplateTemplateParmDecl>(Pack))
2376 mangleTemplateParameter(TempTP->getIndex());
2377 else {
Douglas Gregor4fc48662011-01-13 16:39:34 +00002378 // Note: proposed by Mike Herrick on 11/30/10
2379 // <expression> ::= sZ <function-param> # size of function parameter pack
Douglas Gregor2e774c42011-01-04 18:56:13 +00002380 Diagnostic &Diags = Context.getDiags();
2381 unsigned DiagID = Diags.getCustomDiagID(Diagnostic::Error,
2382 "cannot mangle sizeof...(function parameter pack)");
2383 Diags.Report(DiagID);
2384 return;
2385 }
Douglas Gregordfbbcf92011-03-03 02:20:19 +00002386 break;
Douglas Gregor2e774c42011-01-04 18:56:13 +00002387 }
Anders Carlssond553f8c2009-09-21 01:21:10 +00002388 }
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00002389}
2390
Anders Carlsson3ac86b52009-04-15 05:36:58 +00002391void CXXNameMangler::mangleCXXCtorType(CXXCtorType T) {
2392 // <ctor-dtor-name> ::= C1 # complete object constructor
2393 // ::= C2 # base object constructor
2394 // ::= C3 # complete object allocating constructor
2395 //
2396 switch (T) {
2397 case Ctor_Complete:
2398 Out << "C1";
2399 break;
2400 case Ctor_Base:
2401 Out << "C2";
2402 break;
2403 case Ctor_CompleteAllocating:
2404 Out << "C3";
2405 break;
2406 }
2407}
2408
Anders Carlsson27ae5362009-04-17 01:58:57 +00002409void CXXNameMangler::mangleCXXDtorType(CXXDtorType T) {
2410 // <ctor-dtor-name> ::= D0 # deleting destructor
2411 // ::= D1 # complete object destructor
2412 // ::= D2 # base object destructor
2413 //
2414 switch (T) {
2415 case Dtor_Deleting:
2416 Out << "D0";
2417 break;
2418 case Dtor_Complete:
2419 Out << "D1";
2420 break;
2421 case Dtor_Base:
2422 Out << "D2";
2423 break;
2424 }
2425}
2426
John McCall6dbce192010-08-20 00:17:19 +00002427void CXXNameMangler::mangleTemplateArgs(
2428 const ExplicitTemplateArgumentList &TemplateArgs) {
2429 // <template-args> ::= I <template-arg>+ E
2430 Out << 'I';
2431 for (unsigned I = 0, E = TemplateArgs.NumTemplateArgs; I != E; ++I)
2432 mangleTemplateArg(0, TemplateArgs.getTemplateArgs()[I].getArgument());
2433 Out << 'E';
2434}
2435
Douglas Gregor20f0cc72010-04-23 03:10:43 +00002436void CXXNameMangler::mangleTemplateArgs(TemplateName Template,
2437 const TemplateArgument *TemplateArgs,
2438 unsigned NumTemplateArgs) {
2439 if (TemplateDecl *TD = Template.getAsTemplateDecl())
2440 return mangleTemplateArgs(*TD->getTemplateParameters(), TemplateArgs,
2441 NumTemplateArgs);
Sean Huntc3021132010-05-05 15:23:54 +00002442
Douglas Gregor20f0cc72010-04-23 03:10:43 +00002443 // <template-args> ::= I <template-arg>+ E
2444 Out << 'I';
2445 for (unsigned i = 0; i != NumTemplateArgs; ++i)
2446 mangleTemplateArg(0, TemplateArgs[i]);
2447 Out << 'E';
2448}
2449
Rafael Espindolad9800722010-03-11 14:07:00 +00002450void CXXNameMangler::mangleTemplateArgs(const TemplateParameterList &PL,
2451 const TemplateArgumentList &AL) {
Anders Carlsson7a0ba872009-05-15 16:09:15 +00002452 // <template-args> ::= I <template-arg>+ E
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002453 Out << 'I';
Rafael Espindolad9800722010-03-11 14:07:00 +00002454 for (unsigned i = 0, e = AL.size(); i != e; ++i)
2455 mangleTemplateArg(PL.getParam(i), AL[i]);
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002456 Out << 'E';
Anders Carlsson7a0ba872009-05-15 16:09:15 +00002457}
2458
Rafael Espindolad9800722010-03-11 14:07:00 +00002459void CXXNameMangler::mangleTemplateArgs(const TemplateParameterList &PL,
2460 const TemplateArgument *TemplateArgs,
Anders Carlsson7624f212009-09-18 02:42:01 +00002461 unsigned NumTemplateArgs) {
2462 // <template-args> ::= I <template-arg>+ E
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002463 Out << 'I';
Daniel Dunbar7e0c1952009-11-21 09:17:15 +00002464 for (unsigned i = 0; i != NumTemplateArgs; ++i)
Rafael Espindolad9800722010-03-11 14:07:00 +00002465 mangleTemplateArg(PL.getParam(i), TemplateArgs[i]);
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002466 Out << 'E';
Anders Carlsson7624f212009-09-18 02:42:01 +00002467}
2468
Rafael Espindolad9800722010-03-11 14:07:00 +00002469void CXXNameMangler::mangleTemplateArg(const NamedDecl *P,
2470 const TemplateArgument &A) {
Mike Stump1eb44332009-09-09 15:08:12 +00002471 // <template-arg> ::= <type> # type or template
Anders Carlsson7a0ba872009-05-15 16:09:15 +00002472 // ::= X <expression> E # expression
2473 // ::= <expr-primary> # simple expressions
Douglas Gregor4fc48662011-01-13 16:39:34 +00002474 // ::= J <template-arg>* E # argument pack
Anders Carlsson7a0ba872009-05-15 16:09:15 +00002475 // ::= sp <expression> # pack expansion of (C++0x)
2476 switch (A.getKind()) {
Douglas Gregorf90b27a2011-01-03 22:36:02 +00002477 case TemplateArgument::Null:
2478 llvm_unreachable("Cannot mangle NULL template argument");
2479
Anders Carlsson7a0ba872009-05-15 16:09:15 +00002480 case TemplateArgument::Type:
2481 mangleType(A.getAsType());
2482 break;
Anders Carlsson9e85c742009-12-23 19:30:55 +00002483 case TemplateArgument::Template:
John McCallb6f532e2010-07-14 06:43:17 +00002484 // This is mangled as <type>.
2485 mangleType(A.getAsTemplate());
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002486 break;
Douglas Gregora7fc9012011-01-05 18:58:31 +00002487 case TemplateArgument::TemplateExpansion:
Douglas Gregor4fc48662011-01-13 16:39:34 +00002488 // <type> ::= Dp <type> # pack expansion (C++0x)
Douglas Gregora7fc9012011-01-05 18:58:31 +00002489 Out << "Dp";
2490 mangleType(A.getAsTemplateOrTemplatePattern());
2491 break;
Anders Carlssond553f8c2009-09-21 01:21:10 +00002492 case TemplateArgument::Expression:
2493 Out << 'X';
2494 mangleExpression(A.getAsExpr());
2495 Out << 'E';
2496 break;
Anders Carlssone170ba72009-12-14 01:45:37 +00002497 case TemplateArgument::Integral:
2498 mangleIntegerLiteral(A.getIntegralType(), *A.getAsIntegral());
Anders Carlsson7a0ba872009-05-15 16:09:15 +00002499 break;
Daniel Dunbar7e0c1952009-11-21 09:17:15 +00002500 case TemplateArgument::Declaration: {
Douglas Gregor20f0cc72010-04-23 03:10:43 +00002501 assert(P && "Missing template parameter for declaration argument");
Daniel Dunbar7e0c1952009-11-21 09:17:15 +00002502 // <expr-primary> ::= L <mangled-name> E # external name
2503
Rafael Espindolad9800722010-03-11 14:07:00 +00002504 // Clang produces AST's where pointer-to-member-function expressions
Daniel Dunbar7e0c1952009-11-21 09:17:15 +00002505 // and pointer-to-function expressions are represented as a declaration not
Rafael Espindolad9800722010-03-11 14:07:00 +00002506 // an expression. We compensate for it here to produce the correct mangling.
2507 NamedDecl *D = cast<NamedDecl>(A.getAsDecl());
2508 const NonTypeTemplateParmDecl *Parameter = cast<NonTypeTemplateParmDecl>(P);
John McCallc0a45592011-04-24 08:43:07 +00002509 bool compensateMangling = !Parameter->getType()->isReferenceType();
Rafael Espindolad9800722010-03-11 14:07:00 +00002510 if (compensateMangling) {
2511 Out << 'X';
2512 mangleOperatorName(OO_Amp, 1);
2513 }
2514
Daniel Dunbar7e0c1952009-11-21 09:17:15 +00002515 Out << 'L';
2516 // References to external entities use the mangled name; if the name would
2517 // not normally be manged then mangle it as unqualified.
2518 //
2519 // FIXME: The ABI specifies that external names here should have _Z, but
2520 // gcc leaves this off.
Rafael Espindolad9800722010-03-11 14:07:00 +00002521 if (compensateMangling)
2522 mangle(D, "_Z");
2523 else
2524 mangle(D, "Z");
Daniel Dunbar7e0c1952009-11-21 09:17:15 +00002525 Out << 'E';
Rafael Espindolad9800722010-03-11 14:07:00 +00002526
2527 if (compensateMangling)
2528 Out << 'E';
2529
Daniel Dunbar7e0c1952009-11-21 09:17:15 +00002530 break;
2531 }
Douglas Gregorf90b27a2011-01-03 22:36:02 +00002532
2533 case TemplateArgument::Pack: {
2534 // Note: proposal by Mike Herrick on 12/20/10
2535 Out << 'J';
2536 for (TemplateArgument::pack_iterator PA = A.pack_begin(),
2537 PAEnd = A.pack_end();
2538 PA != PAEnd; ++PA)
2539 mangleTemplateArg(P, *PA);
2540 Out << 'E';
2541 }
Daniel Dunbar7e0c1952009-11-21 09:17:15 +00002542 }
Anders Carlsson7a0ba872009-05-15 16:09:15 +00002543}
2544
Anders Carlsson0ccdf8d2009-09-27 00:38:53 +00002545void CXXNameMangler::mangleTemplateParameter(unsigned Index) {
2546 // <template-param> ::= T_ # first template parameter
2547 // ::= T <parameter-2 non-negative number> _
2548 if (Index == 0)
2549 Out << "T_";
2550 else
2551 Out << 'T' << (Index - 1) << '_';
2552}
2553
Anders Carlsson76967372009-09-17 00:43:46 +00002554// <substitution> ::= S <seq-id> _
2555// ::= S_
Anders Carlsson6862fc72009-09-17 04:16:28 +00002556bool CXXNameMangler::mangleSubstitution(const NamedDecl *ND) {
Anders Carlssone7c8cb62009-09-26 20:53:44 +00002557 // Try one of the standard substitutions first.
2558 if (mangleStandardSubstitution(ND))
2559 return true;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002560
Anders Carlsson433d1372009-11-07 04:26:04 +00002561 ND = cast<NamedDecl>(ND->getCanonicalDecl());
Anders Carlsson6862fc72009-09-17 04:16:28 +00002562 return mangleSubstitution(reinterpret_cast<uintptr_t>(ND));
2563}
2564
Anders Carlsson76967372009-09-17 00:43:46 +00002565bool CXXNameMangler::mangleSubstitution(QualType T) {
Anders Carlssond99edc42009-09-26 03:55:37 +00002566 if (!T.getCVRQualifiers()) {
2567 if (const RecordType *RT = T->getAs<RecordType>())
2568 return mangleSubstitution(RT->getDecl());
2569 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002570
Anders Carlsson76967372009-09-17 00:43:46 +00002571 uintptr_t TypePtr = reinterpret_cast<uintptr_t>(T.getAsOpaquePtr());
2572
Anders Carlssond3a932a2009-09-17 03:53:28 +00002573 return mangleSubstitution(TypePtr);
2574}
2575
Douglas Gregor1e9268e2010-04-28 05:58:56 +00002576bool CXXNameMangler::mangleSubstitution(TemplateName Template) {
2577 if (TemplateDecl *TD = Template.getAsTemplateDecl())
2578 return mangleSubstitution(TD);
Sean Huntc3021132010-05-05 15:23:54 +00002579
Douglas Gregor1e9268e2010-04-28 05:58:56 +00002580 Template = Context.getASTContext().getCanonicalTemplateName(Template);
2581 return mangleSubstitution(
2582 reinterpret_cast<uintptr_t>(Template.getAsVoidPointer()));
2583}
2584
Anders Carlssond3a932a2009-09-17 03:53:28 +00002585bool CXXNameMangler::mangleSubstitution(uintptr_t Ptr) {
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002586 llvm::DenseMap<uintptr_t, unsigned>::iterator I = Substitutions.find(Ptr);
Anders Carlsson76967372009-09-17 00:43:46 +00002587 if (I == Substitutions.end())
2588 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002589
Anders Carlsson76967372009-09-17 00:43:46 +00002590 unsigned SeqID = I->second;
2591 if (SeqID == 0)
2592 Out << "S_";
2593 else {
2594 SeqID--;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002595
Anders Carlsson76967372009-09-17 00:43:46 +00002596 // <seq-id> is encoded in base-36, using digits and upper case letters.
2597 char Buffer[10];
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002598 char *BufferPtr = llvm::array_endof(Buffer);
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002599
Anders Carlsson76967372009-09-17 00:43:46 +00002600 if (SeqID == 0) *--BufferPtr = '0';
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002601
Anders Carlsson76967372009-09-17 00:43:46 +00002602 while (SeqID) {
2603 assert(BufferPtr > Buffer && "Buffer overflow!");
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002604
John McCall6ab30e02010-06-09 07:26:17 +00002605 char c = static_cast<char>(SeqID % 36);
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002606
Anders Carlsson76967372009-09-17 00:43:46 +00002607 *--BufferPtr = (c < 10 ? '0' + c : 'A' + c - 10);
2608 SeqID /= 36;
2609 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002610
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002611 Out << 'S'
2612 << llvm::StringRef(BufferPtr, llvm::array_endof(Buffer)-BufferPtr)
2613 << '_';
Anders Carlsson76967372009-09-17 00:43:46 +00002614 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002615
Anders Carlsson76967372009-09-17 00:43:46 +00002616 return true;
2617}
2618
Anders Carlssonf514b542009-09-27 00:12:57 +00002619static bool isCharType(QualType T) {
2620 if (T.isNull())
2621 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002622
Anders Carlssonf514b542009-09-27 00:12:57 +00002623 return T->isSpecificBuiltinType(BuiltinType::Char_S) ||
2624 T->isSpecificBuiltinType(BuiltinType::Char_U);
2625}
2626
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002627/// isCharSpecialization - Returns whether a given type is a template
Anders Carlssonf514b542009-09-27 00:12:57 +00002628/// specialization of a given name with a single argument of type char.
2629static bool isCharSpecialization(QualType T, const char *Name) {
2630 if (T.isNull())
2631 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002632
Anders Carlssonf514b542009-09-27 00:12:57 +00002633 const RecordType *RT = T->getAs<RecordType>();
2634 if (!RT)
2635 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002636
2637 const ClassTemplateSpecializationDecl *SD =
Anders Carlssonf514b542009-09-27 00:12:57 +00002638 dyn_cast<ClassTemplateSpecializationDecl>(RT->getDecl());
2639 if (!SD)
2640 return false;
2641
2642 if (!isStdNamespace(SD->getDeclContext()))
2643 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002644
Anders Carlssonf514b542009-09-27 00:12:57 +00002645 const TemplateArgumentList &TemplateArgs = SD->getTemplateArgs();
2646 if (TemplateArgs.size() != 1)
2647 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002648
Anders Carlssonf514b542009-09-27 00:12:57 +00002649 if (!isCharType(TemplateArgs[0].getAsType()))
2650 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002651
Daniel Dunbar01eb9b92009-10-18 21:17:35 +00002652 return SD->getIdentifier()->getName() == Name;
Anders Carlssonf514b542009-09-27 00:12:57 +00002653}
2654
Anders Carlsson91f88602009-12-07 19:56:42 +00002655template <std::size_t StrLen>
Benjamin Kramer54353f42010-11-25 18:29:30 +00002656static bool isStreamCharSpecialization(const ClassTemplateSpecializationDecl*SD,
2657 const char (&Str)[StrLen]) {
Anders Carlsson91f88602009-12-07 19:56:42 +00002658 if (!SD->getIdentifier()->isStr(Str))
2659 return false;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002660
Anders Carlsson91f88602009-12-07 19:56:42 +00002661 const TemplateArgumentList &TemplateArgs = SD->getTemplateArgs();
2662 if (TemplateArgs.size() != 2)
2663 return false;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002664
Anders Carlsson91f88602009-12-07 19:56:42 +00002665 if (!isCharType(TemplateArgs[0].getAsType()))
2666 return false;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002667
Anders Carlsson91f88602009-12-07 19:56:42 +00002668 if (!isCharSpecialization(TemplateArgs[1].getAsType(), "char_traits"))
2669 return false;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002670
Anders Carlsson91f88602009-12-07 19:56:42 +00002671 return true;
2672}
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002673
Anders Carlssone7c8cb62009-09-26 20:53:44 +00002674bool CXXNameMangler::mangleStandardSubstitution(const NamedDecl *ND) {
2675 // <substitution> ::= St # ::std::
Anders Carlsson8c031552009-09-26 23:10:05 +00002676 if (const NamespaceDecl *NS = dyn_cast<NamespaceDecl>(ND)) {
Anders Carlsson47846d22009-12-04 06:23:23 +00002677 if (isStd(NS)) {
Anders Carlsson8c031552009-09-26 23:10:05 +00002678 Out << "St";
2679 return true;
2680 }
2681 }
2682
2683 if (const ClassTemplateDecl *TD = dyn_cast<ClassTemplateDecl>(ND)) {
2684 if (!isStdNamespace(TD->getDeclContext()))
2685 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002686
Anders Carlsson8c031552009-09-26 23:10:05 +00002687 // <substitution> ::= Sa # ::std::allocator
2688 if (TD->getIdentifier()->isStr("allocator")) {
2689 Out << "Sa";
2690 return true;
2691 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002692
Anders Carlsson189d59c2009-09-26 23:14:39 +00002693 // <<substitution> ::= Sb # ::std::basic_string
2694 if (TD->getIdentifier()->isStr("basic_string")) {
2695 Out << "Sb";
2696 return true;
2697 }
Anders Carlsson8c031552009-09-26 23:10:05 +00002698 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002699
2700 if (const ClassTemplateSpecializationDecl *SD =
Anders Carlssonf514b542009-09-27 00:12:57 +00002701 dyn_cast<ClassTemplateSpecializationDecl>(ND)) {
Eli Friedman5370ee22010-02-23 18:25:09 +00002702 if (!isStdNamespace(SD->getDeclContext()))
2703 return false;
2704
Anders Carlssonf514b542009-09-27 00:12:57 +00002705 // <substitution> ::= Ss # ::std::basic_string<char,
2706 // ::std::char_traits<char>,
2707 // ::std::allocator<char> >
2708 if (SD->getIdentifier()->isStr("basic_string")) {
2709 const TemplateArgumentList &TemplateArgs = SD->getTemplateArgs();
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002710
Anders Carlssonf514b542009-09-27 00:12:57 +00002711 if (TemplateArgs.size() != 3)
2712 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002713
Anders Carlssonf514b542009-09-27 00:12:57 +00002714 if (!isCharType(TemplateArgs[0].getAsType()))
2715 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002716
Anders Carlssonf514b542009-09-27 00:12:57 +00002717 if (!isCharSpecialization(TemplateArgs[1].getAsType(), "char_traits"))
2718 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002719
Anders Carlssonf514b542009-09-27 00:12:57 +00002720 if (!isCharSpecialization(TemplateArgs[2].getAsType(), "allocator"))
2721 return false;
2722
2723 Out << "Ss";
2724 return true;
2725 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002726
Anders Carlsson91f88602009-12-07 19:56:42 +00002727 // <substitution> ::= Si # ::std::basic_istream<char,
2728 // ::std::char_traits<char> >
2729 if (isStreamCharSpecialization(SD, "basic_istream")) {
2730 Out << "Si";
2731 return true;
2732 }
2733
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002734 // <substitution> ::= So # ::std::basic_ostream<char,
Anders Carlsson8f8fd8e2009-10-08 17:20:26 +00002735 // ::std::char_traits<char> >
Anders Carlsson91f88602009-12-07 19:56:42 +00002736 if (isStreamCharSpecialization(SD, "basic_ostream")) {
Anders Carlsson8f8fd8e2009-10-08 17:20:26 +00002737 Out << "So";
2738 return true;
2739 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002740
Anders Carlsson91f88602009-12-07 19:56:42 +00002741 // <substitution> ::= Sd # ::std::basic_iostream<char,
2742 // ::std::char_traits<char> >
2743 if (isStreamCharSpecialization(SD, "basic_iostream")) {
2744 Out << "Sd";
2745 return true;
2746 }
Anders Carlssonf514b542009-09-27 00:12:57 +00002747 }
Anders Carlsson8c031552009-09-26 23:10:05 +00002748 return false;
Anders Carlssone7c8cb62009-09-26 20:53:44 +00002749}
2750
Anders Carlsson76967372009-09-17 00:43:46 +00002751void CXXNameMangler::addSubstitution(QualType T) {
Anders Carlssond99edc42009-09-26 03:55:37 +00002752 if (!T.getCVRQualifiers()) {
2753 if (const RecordType *RT = T->getAs<RecordType>()) {
2754 addSubstitution(RT->getDecl());
2755 return;
2756 }
2757 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002758
Anders Carlsson76967372009-09-17 00:43:46 +00002759 uintptr_t TypePtr = reinterpret_cast<uintptr_t>(T.getAsOpaquePtr());
Anders Carlssond3a932a2009-09-17 03:53:28 +00002760 addSubstitution(TypePtr);
2761}
2762
Douglas Gregor1e9268e2010-04-28 05:58:56 +00002763void CXXNameMangler::addSubstitution(TemplateName Template) {
2764 if (TemplateDecl *TD = Template.getAsTemplateDecl())
2765 return addSubstitution(TD);
Sean Huntc3021132010-05-05 15:23:54 +00002766
Douglas Gregor1e9268e2010-04-28 05:58:56 +00002767 Template = Context.getASTContext().getCanonicalTemplateName(Template);
2768 addSubstitution(reinterpret_cast<uintptr_t>(Template.getAsVoidPointer()));
2769}
2770
Anders Carlssond3a932a2009-09-17 03:53:28 +00002771void CXXNameMangler::addSubstitution(uintptr_t Ptr) {
Anders Carlssond3a932a2009-09-17 03:53:28 +00002772 assert(!Substitutions.count(Ptr) && "Substitution already exists!");
Anders Carlsson9d85b722010-06-02 04:29:50 +00002773 Substitutions[Ptr] = SeqID++;
Anders Carlsson76967372009-09-17 00:43:46 +00002774}
2775
Daniel Dunbar1b077112009-11-21 09:06:10 +00002776//
Mike Stump1eb44332009-09-09 15:08:12 +00002777
Daniel Dunbar1b077112009-11-21 09:06:10 +00002778/// \brief Mangles the name of the declaration D and emits that name to the
2779/// given output stream.
2780///
2781/// If the declaration D requires a mangled name, this routine will emit that
2782/// mangled name to \p os and return true. Otherwise, \p os will be unchanged
2783/// and this routine will return false. In this case, the caller should just
2784/// emit the identifier of the declaration (\c D->getIdentifier()) as its
2785/// name.
Peter Collingbourne14110472011-01-13 18:57:25 +00002786void ItaniumMangleContext::mangleName(const NamedDecl *D,
Rafael Espindola0e376a02011-02-11 01:41:00 +00002787 llvm::raw_ostream &Out) {
Daniel Dunbarc02ab4c2009-11-21 09:14:44 +00002788 assert((isa<FunctionDecl>(D) || isa<VarDecl>(D)) &&
2789 "Invalid mangleName() call, argument is not a variable or function!");
2790 assert(!isa<CXXConstructorDecl>(D) && !isa<CXXDestructorDecl>(D) &&
2791 "Invalid mangleName() call on 'structor decl!");
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002792
Daniel Dunbar1b077112009-11-21 09:06:10 +00002793 PrettyStackTraceDecl CrashInfo(D, SourceLocation(),
2794 getASTContext().getSourceManager(),
2795 "Mangling declaration");
Mike Stump1eb44332009-09-09 15:08:12 +00002796
Rafael Espindolac4850c22011-02-10 23:59:36 +00002797 CXXNameMangler Mangler(*this, Out);
Daniel Dunbar94fd26d2009-11-21 09:06:22 +00002798 return Mangler.mangle(D);
Daniel Dunbar1b077112009-11-21 09:06:10 +00002799}
Mike Stump1eb44332009-09-09 15:08:12 +00002800
Peter Collingbourne14110472011-01-13 18:57:25 +00002801void ItaniumMangleContext::mangleCXXCtor(const CXXConstructorDecl *D,
2802 CXXCtorType Type,
Rafael Espindola0e376a02011-02-11 01:41:00 +00002803 llvm::raw_ostream &Out) {
Rafael Espindolac4850c22011-02-10 23:59:36 +00002804 CXXNameMangler Mangler(*this, Out, D, Type);
Daniel Dunbar77939c92009-11-21 09:06:31 +00002805 Mangler.mangle(D);
Daniel Dunbar1b077112009-11-21 09:06:10 +00002806}
Mike Stump1eb44332009-09-09 15:08:12 +00002807
Peter Collingbourne14110472011-01-13 18:57:25 +00002808void ItaniumMangleContext::mangleCXXDtor(const CXXDestructorDecl *D,
2809 CXXDtorType Type,
Rafael Espindola0e376a02011-02-11 01:41:00 +00002810 llvm::raw_ostream &Out) {
Rafael Espindolac4850c22011-02-10 23:59:36 +00002811 CXXNameMangler Mangler(*this, Out, D, Type);
Daniel Dunbar77939c92009-11-21 09:06:31 +00002812 Mangler.mangle(D);
Daniel Dunbar1b077112009-11-21 09:06:10 +00002813}
Mike Stumpf1216772009-07-31 18:25:34 +00002814
Peter Collingbourne14110472011-01-13 18:57:25 +00002815void ItaniumMangleContext::mangleThunk(const CXXMethodDecl *MD,
2816 const ThunkInfo &Thunk,
Rafael Espindolaf0be9792011-02-11 02:52:17 +00002817 llvm::raw_ostream &Out) {
Anders Carlsson19879c92010-03-23 17:17:29 +00002818 // <special-name> ::= T <call-offset> <base encoding>
2819 // # base is the nominal target function of thunk
2820 // <special-name> ::= Tc <call-offset> <call-offset> <base encoding>
2821 // # base is the nominal target function of thunk
2822 // # first call-offset is 'this' adjustment
2823 // # second call-offset is result adjustment
Sean Huntc3021132010-05-05 15:23:54 +00002824
Anders Carlsson19879c92010-03-23 17:17:29 +00002825 assert(!isa<CXXDestructorDecl>(MD) &&
2826 "Use mangleCXXDtor for destructor decls!");
Rafael Espindolac4850c22011-02-10 23:59:36 +00002827 CXXNameMangler Mangler(*this, Out);
Anders Carlsson19879c92010-03-23 17:17:29 +00002828 Mangler.getStream() << "_ZT";
2829 if (!Thunk.Return.isEmpty())
2830 Mangler.getStream() << 'c';
Sean Huntc3021132010-05-05 15:23:54 +00002831
Anders Carlsson19879c92010-03-23 17:17:29 +00002832 // Mangle the 'this' pointer adjustment.
2833 Mangler.mangleCallOffset(Thunk.This.NonVirtual, Thunk.This.VCallOffsetOffset);
Sean Huntc3021132010-05-05 15:23:54 +00002834
Anders Carlsson19879c92010-03-23 17:17:29 +00002835 // Mangle the return pointer adjustment if there is one.
2836 if (!Thunk.Return.isEmpty())
2837 Mangler.mangleCallOffset(Thunk.Return.NonVirtual,
2838 Thunk.Return.VBaseOffsetOffset);
Sean Huntc3021132010-05-05 15:23:54 +00002839
Anders Carlsson19879c92010-03-23 17:17:29 +00002840 Mangler.mangleFunctionEncoding(MD);
2841}
2842
Sean Huntc3021132010-05-05 15:23:54 +00002843void
Peter Collingbourne14110472011-01-13 18:57:25 +00002844ItaniumMangleContext::mangleCXXDtorThunk(const CXXDestructorDecl *DD,
2845 CXXDtorType Type,
2846 const ThisAdjustment &ThisAdjustment,
Rafael Espindolaf0be9792011-02-11 02:52:17 +00002847 llvm::raw_ostream &Out) {
Anders Carlsson19879c92010-03-23 17:17:29 +00002848 // <special-name> ::= T <call-offset> <base encoding>
2849 // # base is the nominal target function of thunk
Rafael Espindolac4850c22011-02-10 23:59:36 +00002850 CXXNameMangler Mangler(*this, Out, DD, Type);
Anders Carlsson19879c92010-03-23 17:17:29 +00002851 Mangler.getStream() << "_ZT";
2852
2853 // Mangle the 'this' pointer adjustment.
Sean Huntc3021132010-05-05 15:23:54 +00002854 Mangler.mangleCallOffset(ThisAdjustment.NonVirtual,
Anders Carlsson19879c92010-03-23 17:17:29 +00002855 ThisAdjustment.VCallOffsetOffset);
2856
2857 Mangler.mangleFunctionEncoding(DD);
2858}
2859
Daniel Dunbarc0747712009-11-21 09:12:13 +00002860/// mangleGuardVariable - Returns the mangled name for a guard variable
2861/// for the passed in VarDecl.
Peter Collingbourne14110472011-01-13 18:57:25 +00002862void ItaniumMangleContext::mangleItaniumGuardVariable(const VarDecl *D,
Rafael Espindolaf0be9792011-02-11 02:52:17 +00002863 llvm::raw_ostream &Out) {
Daniel Dunbarc0747712009-11-21 09:12:13 +00002864 // <special-name> ::= GV <object name> # Guard variable for one-time
2865 // # initialization
Rafael Espindolac4850c22011-02-10 23:59:36 +00002866 CXXNameMangler Mangler(*this, Out);
Daniel Dunbarc0747712009-11-21 09:12:13 +00002867 Mangler.getStream() << "_ZGV";
2868 Mangler.mangleName(D);
2869}
2870
Peter Collingbourne14110472011-01-13 18:57:25 +00002871void ItaniumMangleContext::mangleReferenceTemporary(const VarDecl *D,
Rafael Espindolaf0be9792011-02-11 02:52:17 +00002872 llvm::raw_ostream &Out) {
Anders Carlsson715edf22010-06-26 16:09:40 +00002873 // We match the GCC mangling here.
2874 // <special-name> ::= GR <object name>
Rafael Espindolac4850c22011-02-10 23:59:36 +00002875 CXXNameMangler Mangler(*this, Out);
Anders Carlsson715edf22010-06-26 16:09:40 +00002876 Mangler.getStream() << "_ZGR";
2877 Mangler.mangleName(D);
2878}
2879
Peter Collingbourne14110472011-01-13 18:57:25 +00002880void ItaniumMangleContext::mangleCXXVTable(const CXXRecordDecl *RD,
Rafael Espindolaf0be9792011-02-11 02:52:17 +00002881 llvm::raw_ostream &Out) {
Daniel Dunbarc0747712009-11-21 09:12:13 +00002882 // <special-name> ::= TV <type> # virtual table
Rafael Espindolac4850c22011-02-10 23:59:36 +00002883 CXXNameMangler Mangler(*this, Out);
Daniel Dunbarc0747712009-11-21 09:12:13 +00002884 Mangler.getStream() << "_ZTV";
Douglas Gregor1b12a3b2010-05-26 05:11:13 +00002885 Mangler.mangleNameOrStandardSubstitution(RD);
Daniel Dunbar1b077112009-11-21 09:06:10 +00002886}
Mike Stump82d75b02009-11-10 01:58:37 +00002887
Peter Collingbourne14110472011-01-13 18:57:25 +00002888void ItaniumMangleContext::mangleCXXVTT(const CXXRecordDecl *RD,
Rafael Espindolaf0be9792011-02-11 02:52:17 +00002889 llvm::raw_ostream &Out) {
Daniel Dunbarc0747712009-11-21 09:12:13 +00002890 // <special-name> ::= TT <type> # VTT structure
Rafael Espindolac4850c22011-02-10 23:59:36 +00002891 CXXNameMangler Mangler(*this, Out);
Daniel Dunbarc0747712009-11-21 09:12:13 +00002892 Mangler.getStream() << "_ZTT";
Douglas Gregor1b12a3b2010-05-26 05:11:13 +00002893 Mangler.mangleNameOrStandardSubstitution(RD);
Daniel Dunbar1b077112009-11-21 09:06:10 +00002894}
Mike Stumpab3f7e92009-11-10 01:41:59 +00002895
Peter Collingbourne14110472011-01-13 18:57:25 +00002896void ItaniumMangleContext::mangleCXXCtorVTable(const CXXRecordDecl *RD,
2897 int64_t Offset,
2898 const CXXRecordDecl *Type,
Rafael Espindolaf0be9792011-02-11 02:52:17 +00002899 llvm::raw_ostream &Out) {
Daniel Dunbarc0747712009-11-21 09:12:13 +00002900 // <special-name> ::= TC <type> <offset number> _ <base type>
Rafael Espindolac4850c22011-02-10 23:59:36 +00002901 CXXNameMangler Mangler(*this, Out);
Daniel Dunbarc0747712009-11-21 09:12:13 +00002902 Mangler.getStream() << "_ZTC";
Douglas Gregor1b12a3b2010-05-26 05:11:13 +00002903 Mangler.mangleNameOrStandardSubstitution(RD);
Daniel Dunbarc0747712009-11-21 09:12:13 +00002904 Mangler.getStream() << Offset;
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002905 Mangler.getStream() << '_';
Douglas Gregor1b12a3b2010-05-26 05:11:13 +00002906 Mangler.mangleNameOrStandardSubstitution(Type);
Daniel Dunbar1b077112009-11-21 09:06:10 +00002907}
Mike Stump738f8c22009-07-31 23:15:31 +00002908
Peter Collingbourne14110472011-01-13 18:57:25 +00002909void ItaniumMangleContext::mangleCXXRTTI(QualType Ty,
Rafael Espindolaf0be9792011-02-11 02:52:17 +00002910 llvm::raw_ostream &Out) {
Daniel Dunbarc0747712009-11-21 09:12:13 +00002911 // <special-name> ::= TI <type> # typeinfo structure
Douglas Gregor154fe982009-12-23 22:04:40 +00002912 assert(!Ty.hasQualifiers() && "RTTI info cannot have top-level qualifiers");
Rafael Espindolac4850c22011-02-10 23:59:36 +00002913 CXXNameMangler Mangler(*this, Out);
Daniel Dunbarc0747712009-11-21 09:12:13 +00002914 Mangler.getStream() << "_ZTI";
2915 Mangler.mangleType(Ty);
Daniel Dunbar1b077112009-11-21 09:06:10 +00002916}
Mike Stump67795982009-11-14 00:14:13 +00002917
Peter Collingbourne14110472011-01-13 18:57:25 +00002918void ItaniumMangleContext::mangleCXXRTTIName(QualType Ty,
Rafael Espindolaf0be9792011-02-11 02:52:17 +00002919 llvm::raw_ostream &Out) {
Daniel Dunbarc0747712009-11-21 09:12:13 +00002920 // <special-name> ::= TS <type> # typeinfo name (null terminated byte string)
Rafael Espindolac4850c22011-02-10 23:59:36 +00002921 CXXNameMangler Mangler(*this, Out);
Daniel Dunbarc0747712009-11-21 09:12:13 +00002922 Mangler.getStream() << "_ZTS";
2923 Mangler.mangleType(Ty);
Mike Stumpf1216772009-07-31 18:25:34 +00002924}
Peter Collingbourne14110472011-01-13 18:57:25 +00002925
2926MangleContext *clang::createItaniumMangleContext(ASTContext &Context,
2927 Diagnostic &Diags) {
2928 return new ItaniumMangleContext(Context, Diags);
2929}