blob: 78f149c2df73a5b7509c0126715c84235c5aa378 [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"
John McCallf85e1932011-06-15 23:02:42 +000024#include "clang/AST/ExprObjC.h"
John McCallfb44de92011-05-01 22:35:37 +000025#include "clang/AST/TypeLoc.h"
Peter Collingbourne14110472011-01-13 18:57:25 +000026#include "clang/Basic/ABI.h"
Douglas Gregor6ec36682009-02-18 23:53:56 +000027#include "clang/Basic/SourceManager.h"
Rafael Espindola4e274e92011-02-15 22:23:51 +000028#include "clang/Basic/TargetInfo.h"
Anders Carlssonc4355b62009-10-07 01:45:02 +000029#include "llvm/ADT/StringExtras.h"
Douglas Gregor5f2bfd42009-02-13 00:10:09 +000030#include "llvm/Support/raw_ostream.h"
John McCallefe6aee2009-09-05 07:56:18 +000031#include "llvm/Support/ErrorHandling.h"
Anders Carlssonf98574b2010-02-05 07:31:37 +000032
33#define MANGLE_CHECKER 0
34
35#if MANGLE_CHECKER
36#include <cxxabi.h>
37#endif
38
Douglas Gregor5f2bfd42009-02-13 00:10:09 +000039using namespace clang;
Charles Davis685b1d92010-05-26 18:25:27 +000040
Douglas Gregor5f2bfd42009-02-13 00:10:09 +000041namespace {
Fariborz Jahanian57058532010-03-03 19:41:08 +000042
John McCall82b7d7b2010-10-18 21:28:44 +000043static const CXXRecordDecl *GetLocalClassDecl(const NamedDecl *ND) {
44 const DeclContext *DC = dyn_cast<DeclContext>(ND);
45 if (!DC)
46 DC = ND->getDeclContext();
47 while (!DC->isNamespace() && !DC->isTranslationUnit()) {
48 if (isa<FunctionDecl>(DC->getParent()))
49 return dyn_cast<CXXRecordDecl>(DC);
50 DC = DC->getParent();
Fariborz Jahanian57058532010-03-03 19:41:08 +000051 }
52 return 0;
53}
54
John McCallfb44de92011-05-01 22:35:37 +000055static const FunctionDecl *getStructor(const FunctionDecl *fn) {
56 if (const FunctionTemplateDecl *ftd = fn->getPrimaryTemplate())
57 return ftd->getTemplatedDecl();
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +000058
John McCallfb44de92011-05-01 22:35:37 +000059 return fn;
60}
Anders Carlsson7e120032009-11-24 05:36:32 +000061
John McCallfb44de92011-05-01 22:35:37 +000062static const NamedDecl *getStructor(const NamedDecl *decl) {
63 const FunctionDecl *fn = dyn_cast_or_null<FunctionDecl>(decl);
64 return (fn ? getStructor(fn) : decl);
Anders Carlsson7e120032009-11-24 05:36:32 +000065}
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);
Chris Lattner5f9e2722011-07-23 10:55:15 +000095 void mangleName(const NamedDecl *D, raw_ostream &);
Peter Collingbourne14110472011-01-13 18:57:25 +000096 void mangleThunk(const CXXMethodDecl *MD,
97 const ThunkInfo &Thunk,
Chris Lattner5f9e2722011-07-23 10:55:15 +000098 raw_ostream &);
Peter Collingbourne14110472011-01-13 18:57:25 +000099 void mangleCXXDtorThunk(const CXXDestructorDecl *DD, CXXDtorType Type,
100 const ThisAdjustment &ThisAdjustment,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000101 raw_ostream &);
Peter Collingbourne14110472011-01-13 18:57:25 +0000102 void mangleReferenceTemporary(const VarDecl *D,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000103 raw_ostream &);
Peter Collingbourne14110472011-01-13 18:57:25 +0000104 void mangleCXXVTable(const CXXRecordDecl *RD,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000105 raw_ostream &);
Peter Collingbourne14110472011-01-13 18:57:25 +0000106 void mangleCXXVTT(const CXXRecordDecl *RD,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000107 raw_ostream &);
Peter Collingbourne14110472011-01-13 18:57:25 +0000108 void mangleCXXCtorVTable(const CXXRecordDecl *RD, int64_t Offset,
109 const CXXRecordDecl *Type,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000110 raw_ostream &);
111 void mangleCXXRTTI(QualType T, raw_ostream &);
112 void mangleCXXRTTIName(QualType T, raw_ostream &);
Peter Collingbourne14110472011-01-13 18:57:25 +0000113 void mangleCXXCtor(const CXXConstructorDecl *D, CXXCtorType Type,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000114 raw_ostream &);
Peter Collingbourne14110472011-01-13 18:57:25 +0000115 void mangleCXXDtor(const CXXDestructorDecl *D, CXXDtorType Type,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000116 raw_ostream &);
Peter Collingbourne14110472011-01-13 18:57:25 +0000117
Chris Lattner5f9e2722011-07-23 10:55:15 +0000118 void mangleItaniumGuardVariable(const VarDecl *D, 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;
Chris Lattner5f9e2722011-07-23 10:55:15 +0000139 raw_ostream &Out;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000140
John McCallfb44de92011-05-01 22:35:37 +0000141 /// The "structor" is the top-level declaration being mangled, if
142 /// that's not a template specialization; otherwise it's the pattern
143 /// for that specialization.
144 const NamedDecl *Structor;
Daniel Dunbar1b077112009-11-21 09:06:10 +0000145 unsigned StructorType;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000146
Anders Carlsson9d85b722010-06-02 04:29:50 +0000147 /// SeqID - The next subsitution sequence number.
148 unsigned SeqID;
149
John McCallfb44de92011-05-01 22:35:37 +0000150 class FunctionTypeDepthState {
151 unsigned Bits;
152
153 enum { InResultTypeMask = 1 };
154
155 public:
156 FunctionTypeDepthState() : Bits(0) {}
157
158 /// The number of function types we're inside.
159 unsigned getDepth() const {
160 return Bits >> 1;
161 }
162
163 /// True if we're in the return type of the innermost function type.
164 bool isInResultType() const {
165 return Bits & InResultTypeMask;
166 }
167
168 FunctionTypeDepthState push() {
169 FunctionTypeDepthState tmp = *this;
170 Bits = (Bits & ~InResultTypeMask) + 2;
171 return tmp;
172 }
173
174 void enterResultType() {
175 Bits |= InResultTypeMask;
176 }
177
178 void leaveResultType() {
179 Bits &= ~InResultTypeMask;
180 }
181
182 void pop(FunctionTypeDepthState saved) {
183 assert(getDepth() == saved.getDepth() + 1);
184 Bits = saved.Bits;
185 }
186
187 } FunctionTypeDepth;
188
Daniel Dunbar1b077112009-11-21 09:06:10 +0000189 llvm::DenseMap<uintptr_t, unsigned> Substitutions;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000190
John McCall1dd73832010-02-04 01:42:13 +0000191 ASTContext &getASTContext() const { return Context.getASTContext(); }
192
Daniel Dunbarc0747712009-11-21 09:12:13 +0000193public:
Chris Lattner5f9e2722011-07-23 10:55:15 +0000194 CXXNameMangler(ItaniumMangleContext &C, raw_ostream &Out_,
John McCallfb44de92011-05-01 22:35:37 +0000195 const NamedDecl *D = 0)
196 : Context(C), Out(Out_), Structor(getStructor(D)), StructorType(0),
197 SeqID(0) {
198 // These can't be mangled without a ctor type or dtor type.
199 assert(!D || (!isa<CXXDestructorDecl>(D) &&
200 !isa<CXXConstructorDecl>(D)));
201 }
Chris Lattner5f9e2722011-07-23 10:55:15 +0000202 CXXNameMangler(ItaniumMangleContext &C, raw_ostream &Out_,
Daniel Dunbar77939c92009-11-21 09:06:31 +0000203 const CXXConstructorDecl *D, CXXCtorType Type)
Rafael Espindolac4850c22011-02-10 23:59:36 +0000204 : Context(C), Out(Out_), Structor(getStructor(D)), StructorType(Type),
John McCallfb44de92011-05-01 22:35:37 +0000205 SeqID(0) { }
Chris Lattner5f9e2722011-07-23 10:55:15 +0000206 CXXNameMangler(ItaniumMangleContext &C, raw_ostream &Out_,
Daniel Dunbar77939c92009-11-21 09:06:31 +0000207 const CXXDestructorDecl *D, CXXDtorType Type)
Rafael Espindolac4850c22011-02-10 23:59:36 +0000208 : Context(C), Out(Out_), Structor(getStructor(D)), StructorType(Type),
John McCallfb44de92011-05-01 22:35:37 +0000209 SeqID(0) { }
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000210
Anders Carlssonf98574b2010-02-05 07:31:37 +0000211#if MANGLE_CHECKER
212 ~CXXNameMangler() {
213 if (Out.str()[0] == '\01')
214 return;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000215
Anders Carlssonf98574b2010-02-05 07:31:37 +0000216 int status = 0;
217 char *result = abi::__cxa_demangle(Out.str().str().c_str(), 0, 0, &status);
218 assert(status == 0 && "Could not demangle mangled name!");
219 free(result);
220 }
221#endif
Chris Lattner5f9e2722011-07-23 10:55:15 +0000222 raw_ostream &getStream() { return Out; }
Daniel Dunbarc0747712009-11-21 09:12:13 +0000223
Chris Lattner5f9e2722011-07-23 10:55:15 +0000224 void mangle(const NamedDecl *D, StringRef Prefix = "_Z");
Anders Carlsson19879c92010-03-23 17:17:29 +0000225 void mangleCallOffset(int64_t NonVirtual, int64_t Virtual);
John McCall0512e482010-07-14 04:20:34 +0000226 void mangleNumber(const llvm::APSInt &I);
Anders Carlssona94822e2009-11-26 02:32:05 +0000227 void mangleNumber(int64_t Number);
John McCall0512e482010-07-14 04:20:34 +0000228 void mangleFloat(const llvm::APFloat &F);
Daniel Dunbarc0747712009-11-21 09:12:13 +0000229 void mangleFunctionEncoding(const FunctionDecl *FD);
230 void mangleName(const NamedDecl *ND);
231 void mangleType(QualType T);
Douglas Gregor1b12a3b2010-05-26 05:11:13 +0000232 void mangleNameOrStandardSubstitution(const NamedDecl *ND);
233
Daniel Dunbarc0747712009-11-21 09:12:13 +0000234private:
Daniel Dunbar1b077112009-11-21 09:06:10 +0000235 bool mangleSubstitution(const NamedDecl *ND);
236 bool mangleSubstitution(QualType T);
Douglas Gregor1e9268e2010-04-28 05:58:56 +0000237 bool mangleSubstitution(TemplateName Template);
Daniel Dunbar1b077112009-11-21 09:06:10 +0000238 bool mangleSubstitution(uintptr_t Ptr);
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000239
John McCall68a51a72011-07-01 00:04:39 +0000240 void mangleExistingSubstitution(QualType type);
241 void mangleExistingSubstitution(TemplateName name);
242
Daniel Dunbar1b077112009-11-21 09:06:10 +0000243 bool mangleStandardSubstitution(const NamedDecl *ND);
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000244
Daniel Dunbar1b077112009-11-21 09:06:10 +0000245 void addSubstitution(const NamedDecl *ND) {
246 ND = cast<NamedDecl>(ND->getCanonicalDecl());
Anders Carlsson433d1372009-11-07 04:26:04 +0000247
Daniel Dunbar1b077112009-11-21 09:06:10 +0000248 addSubstitution(reinterpret_cast<uintptr_t>(ND));
249 }
250 void addSubstitution(QualType T);
Douglas Gregor1e9268e2010-04-28 05:58:56 +0000251 void addSubstitution(TemplateName Template);
Daniel Dunbar1b077112009-11-21 09:06:10 +0000252 void addSubstitution(uintptr_t Ptr);
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000253
John McCalla0ce15c2011-04-24 08:23:24 +0000254 void mangleUnresolvedPrefix(NestedNameSpecifier *qualifier,
255 NamedDecl *firstQualifierLookup,
256 bool recursive = false);
257 void mangleUnresolvedName(NestedNameSpecifier *qualifier,
258 NamedDecl *firstQualifierLookup,
259 DeclarationName name,
John McCall1dd73832010-02-04 01:42:13 +0000260 unsigned KnownArity = UnknownArity);
261
Daniel Dunbar1b077112009-11-21 09:06:10 +0000262 void mangleName(const TemplateDecl *TD,
263 const TemplateArgument *TemplateArgs,
264 unsigned NumTemplateArgs);
John McCall1dd73832010-02-04 01:42:13 +0000265 void mangleUnqualifiedName(const NamedDecl *ND) {
266 mangleUnqualifiedName(ND, ND->getDeclName(), UnknownArity);
267 }
268 void mangleUnqualifiedName(const NamedDecl *ND, DeclarationName Name,
269 unsigned KnownArity);
Daniel Dunbar1b077112009-11-21 09:06:10 +0000270 void mangleUnscopedName(const NamedDecl *ND);
271 void mangleUnscopedTemplateName(const TemplateDecl *ND);
Douglas Gregor1e9268e2010-04-28 05:58:56 +0000272 void mangleUnscopedTemplateName(TemplateName);
Daniel Dunbar1b077112009-11-21 09:06:10 +0000273 void mangleSourceName(const IdentifierInfo *II);
274 void mangleLocalName(const NamedDecl *ND);
Fariborz Jahanian57058532010-03-03 19:41:08 +0000275 void mangleNestedName(const NamedDecl *ND, const DeclContext *DC,
276 bool NoFunction=false);
Daniel Dunbar1b077112009-11-21 09:06:10 +0000277 void mangleNestedName(const TemplateDecl *TD,
278 const TemplateArgument *TemplateArgs,
279 unsigned NumTemplateArgs);
John McCalla0ce15c2011-04-24 08:23:24 +0000280 void manglePrefix(NestedNameSpecifier *qualifier);
Fariborz Jahanian57058532010-03-03 19:41:08 +0000281 void manglePrefix(const DeclContext *DC, bool NoFunction=false);
John McCall4f4e4132011-05-04 01:45:19 +0000282 void manglePrefix(QualType type);
Daniel Dunbar1b077112009-11-21 09:06:10 +0000283 void mangleTemplatePrefix(const TemplateDecl *ND);
Douglas Gregor20f0cc72010-04-23 03:10:43 +0000284 void mangleTemplatePrefix(TemplateName Template);
Daniel Dunbar1b077112009-11-21 09:06:10 +0000285 void mangleOperatorName(OverloadedOperatorKind OO, unsigned Arity);
286 void mangleQualifiers(Qualifiers Quals);
Douglas Gregor0a9a6d62011-01-26 17:36:28 +0000287 void mangleRefQualifier(RefQualifierKind RefQualifier);
John McCallefe6aee2009-09-05 07:56:18 +0000288
Anders Carlsson7b06f6c2009-12-10 03:14:39 +0000289 void mangleObjCMethodName(const ObjCMethodDecl *MD);
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000290
Daniel Dunbar1b077112009-11-21 09:06:10 +0000291 // Declare manglers for every type class.
John McCallefe6aee2009-09-05 07:56:18 +0000292#define ABSTRACT_TYPE(CLASS, PARENT)
293#define NON_CANONICAL_TYPE(CLASS, PARENT)
294#define TYPE(CLASS, PARENT) void mangleType(const CLASS##Type *T);
295#include "clang/AST/TypeNodes.def"
296
Daniel Dunbar1b077112009-11-21 09:06:10 +0000297 void mangleType(const TagType*);
John McCallb6f532e2010-07-14 06:43:17 +0000298 void mangleType(TemplateName);
Daniel Dunbar1b077112009-11-21 09:06:10 +0000299 void mangleBareFunctionType(const FunctionType *T,
300 bool MangleReturnType);
Bob Wilson57147a82010-11-16 00:32:18 +0000301 void mangleNeonVectorType(const VectorType *T);
Anders Carlssone170ba72009-12-14 01:45:37 +0000302
303 void mangleIntegerLiteral(QualType T, const llvm::APSInt &Value);
John McCalla0ce15c2011-04-24 08:23:24 +0000304 void mangleMemberExpr(const Expr *base, bool isArrow,
305 NestedNameSpecifier *qualifier,
306 NamedDecl *firstQualifierLookup,
307 DeclarationName name,
308 unsigned knownArity);
John McCall5e1e89b2010-08-18 19:18:59 +0000309 void mangleExpression(const Expr *E, unsigned Arity = UnknownArity);
Daniel Dunbar1b077112009-11-21 09:06:10 +0000310 void mangleCXXCtorType(CXXCtorType T);
311 void mangleCXXDtorType(CXXDtorType T);
Mike Stump1eb44332009-09-09 15:08:12 +0000312
Argyrios Kyrtzidisb0c3e092011-09-22 20:07:03 +0000313 void mangleTemplateArgs(const ASTTemplateArgumentListInfo &TemplateArgs);
Douglas Gregor20f0cc72010-04-23 03:10:43 +0000314 void mangleTemplateArgs(TemplateName Template,
315 const TemplateArgument *TemplateArgs,
Sean Huntc3021132010-05-05 15:23:54 +0000316 unsigned NumTemplateArgs);
Rafael Espindolad9800722010-03-11 14:07:00 +0000317 void mangleTemplateArgs(const TemplateParameterList &PL,
318 const TemplateArgument *TemplateArgs,
Daniel Dunbar1b077112009-11-21 09:06:10 +0000319 unsigned NumTemplateArgs);
Rafael Espindolad9800722010-03-11 14:07:00 +0000320 void mangleTemplateArgs(const TemplateParameterList &PL,
321 const TemplateArgumentList &AL);
Douglas Gregorf1588662011-07-12 15:18:55 +0000322 void mangleTemplateArg(const NamedDecl *P, TemplateArgument A);
John McCall4f4e4132011-05-04 01:45:19 +0000323 void mangleUnresolvedTemplateArgs(const TemplateArgument *args,
324 unsigned numArgs);
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000325
Daniel Dunbar1b077112009-11-21 09:06:10 +0000326 void mangleTemplateParameter(unsigned Index);
John McCallfb44de92011-05-01 22:35:37 +0000327
328 void mangleFunctionParam(const ParmVarDecl *parm);
Daniel Dunbar1b077112009-11-21 09:06:10 +0000329};
Peter Collingbourne14110472011-01-13 18:57:25 +0000330
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000331}
332
Anders Carlsson43f17402009-04-02 15:51:53 +0000333static bool isInCLinkageSpecification(const Decl *D) {
Douglas Gregor457e2812009-10-28 16:31:34 +0000334 D = D->getCanonicalDecl();
Mike Stump1eb44332009-09-09 15:08:12 +0000335 for (const DeclContext *DC = D->getDeclContext();
Anders Carlsson43f17402009-04-02 15:51:53 +0000336 !DC->isTranslationUnit(); DC = DC->getParent()) {
Mike Stump1eb44332009-09-09 15:08:12 +0000337 if (const LinkageSpecDecl *Linkage = dyn_cast<LinkageSpecDecl>(DC))
Anders Carlsson43f17402009-04-02 15:51:53 +0000338 return Linkage->getLanguage() == LinkageSpecDecl::lang_c;
339 }
Mike Stump1eb44332009-09-09 15:08:12 +0000340
Anders Carlsson43f17402009-04-02 15:51:53 +0000341 return false;
342}
343
Peter Collingbourne14110472011-01-13 18:57:25 +0000344bool ItaniumMangleContext::shouldMangleDeclName(const NamedDecl *D) {
Daniel Dunbarf981bf82009-11-21 09:14:52 +0000345 // In C, functions with no attributes never need to be mangled. Fastpath them.
346 if (!getASTContext().getLangOptions().CPlusPlus && !D->hasAttrs())
347 return false;
348
349 // Any decl can be declared with __asm("foo") on it, and this takes precedence
350 // over all other naming in the .o file.
351 if (D->hasAttr<AsmLabelAttr>())
352 return true;
353
Mike Stump141c5af2009-09-02 00:25:38 +0000354 // Clang's "overloadable" attribute extension to C/C++ implies name mangling
Anders Carlssona1e16222009-11-07 07:15:03 +0000355 // (always) as does passing a C++ member function and a function
356 // whose name is not a simple identifier.
Daniel Dunbarf981bf82009-11-21 09:14:52 +0000357 const FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
358 if (FD && (FD->hasAttr<OverloadableAttr>() || isa<CXXMethodDecl>(FD) ||
359 !FD->getDeclName().isIdentifier()))
360 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000361
Daniel Dunbarf981bf82009-11-21 09:14:52 +0000362 // Otherwise, no mangling is done outside C++ mode.
363 if (!getASTContext().getLangOptions().CPlusPlus)
364 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000365
Sean Hunt31455252010-01-24 03:04:27 +0000366 // Variables at global scope with non-internal linkage are not mangled
Eli Friedman7facf842009-12-02 20:32:49 +0000367 if (!FD) {
368 const DeclContext *DC = D->getDeclContext();
369 // Check for extern variable declared locally.
Fariborz Jahaniane81c5612010-06-30 18:57:21 +0000370 if (DC->isFunctionOrMethod() && D->hasLinkage())
Eli Friedman7facf842009-12-02 20:32:49 +0000371 while (!DC->isNamespace() && !DC->isTranslationUnit())
372 DC = DC->getParent();
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000373 if (DC->isTranslationUnit() && D->getLinkage() != InternalLinkage)
Eli Friedman7facf842009-12-02 20:32:49 +0000374 return false;
375 }
376
Eli Friedmanc00cb642010-07-18 20:49:59 +0000377 // Class members are always mangled.
378 if (D->getDeclContext()->isRecord())
379 return true;
380
Eli Friedman7facf842009-12-02 20:32:49 +0000381 // C functions and "main" are not mangled.
382 if ((FD && FD->isMain()) || isInCLinkageSpecification(D))
Daniel Dunbarf981bf82009-11-21 09:14:52 +0000383 return false;
384
Anders Carlsson43f17402009-04-02 15:51:53 +0000385 return true;
386}
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000387
Chris Lattner5f9e2722011-07-23 10:55:15 +0000388void CXXNameMangler::mangle(const NamedDecl *D, StringRef Prefix) {
Mike Stump141c5af2009-09-02 00:25:38 +0000389 // Any decl can be declared with __asm("foo") on it, and this takes precedence
390 // over all other naming in the .o file.
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000391 if (const AsmLabelAttr *ALA = D->getAttr<AsmLabelAttr>()) {
Chris Lattnerca3f25c2009-03-21 08:24:40 +0000392 // If we have an asm name, then we use it as the mangling.
Rafael Espindola4e274e92011-02-15 22:23:51 +0000393
394 // Adding the prefix can cause problems when one file has a "foo" and
395 // another has a "\01foo". That is known to happen on ELF with the
396 // tricks normally used for producing aliases (PR9177). Fortunately the
397 // llvm mangler on ELF is a nop, so we can just avoid adding the \01
Peter Collingbourne69317432011-04-06 12:29:09 +0000398 // marker. We also avoid adding the marker if this is an alias for an
399 // LLVM intrinsic.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000400 StringRef UserLabelPrefix =
Douglas Gregorbcfd1f52011-09-02 00:18:52 +0000401 getASTContext().getTargetInfo().getUserLabelPrefix();
Peter Collingbourne69317432011-04-06 12:29:09 +0000402 if (!UserLabelPrefix.empty() && !ALA->getLabel().startswith("llvm."))
Rafael Espindola4e274e92011-02-15 22:23:51 +0000403 Out << '\01'; // LLVM IR Marker for __asm("foo")
404
Chris Lattnerca3f25c2009-03-21 08:24:40 +0000405 Out << ALA->getLabel();
Daniel Dunbarf981bf82009-11-21 09:14:52 +0000406 return;
Chris Lattnerca3f25c2009-03-21 08:24:40 +0000407 }
Mike Stump1eb44332009-09-09 15:08:12 +0000408
Sean Hunt31455252010-01-24 03:04:27 +0000409 // <mangled-name> ::= _Z <encoding>
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000410 // ::= <data name>
411 // ::= <special-name>
Daniel Dunbar7e0c1952009-11-21 09:17:15 +0000412 Out << Prefix;
413 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
Daniel Dunbarf981bf82009-11-21 09:14:52 +0000414 mangleFunctionEncoding(FD);
Rafael Espindolad9800722010-03-11 14:07:00 +0000415 else if (const VarDecl *VD = dyn_cast<VarDecl>(D))
416 mangleName(VD);
Daniel Dunbar7e0c1952009-11-21 09:17:15 +0000417 else
Rafael Espindolad9800722010-03-11 14:07:00 +0000418 mangleName(cast<FieldDecl>(D));
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000419}
420
421void CXXNameMangler::mangleFunctionEncoding(const FunctionDecl *FD) {
422 // <encoding> ::= <function name> <bare-function-type>
423 mangleName(FD);
Mike Stump1eb44332009-09-09 15:08:12 +0000424
Daniel Dunbar7e0c1952009-11-21 09:17:15 +0000425 // Don't mangle in the type if this isn't a decl we should typically mangle.
426 if (!Context.shouldMangleDeclName(FD))
427 return;
428
Mike Stump141c5af2009-09-02 00:25:38 +0000429 // Whether the mangling of a function type includes the return type depends on
430 // the context and the nature of the function. The rules for deciding whether
431 // the return type is included are:
Mike Stump1eb44332009-09-09 15:08:12 +0000432 //
Douglas Gregor1fd2dd12009-06-29 22:39:32 +0000433 // 1. Template functions (names or types) have return types encoded, with
434 // the exceptions listed below.
Mike Stump1eb44332009-09-09 15:08:12 +0000435 // 2. Function types not appearing as part of a function name mangling,
Douglas Gregor1fd2dd12009-06-29 22:39:32 +0000436 // e.g. parameters, pointer types, etc., have return type encoded, with the
437 // exceptions listed below.
438 // 3. Non-template function names do not have return types encoded.
439 //
Mike Stump141c5af2009-09-02 00:25:38 +0000440 // The exceptions mentioned in (1) and (2) above, for which the return type is
441 // never included, are
Douglas Gregor1fd2dd12009-06-29 22:39:32 +0000442 // 1. Constructors.
443 // 2. Destructors.
444 // 3. Conversion operator functions, e.g. operator int.
445 bool MangleReturnType = false;
Anders Carlsson9234b7f2009-09-17 03:46:43 +0000446 if (FunctionTemplateDecl *PrimaryTemplate = FD->getPrimaryTemplate()) {
447 if (!(isa<CXXConstructorDecl>(FD) || isa<CXXDestructorDecl>(FD) ||
448 isa<CXXConversionDecl>(FD)))
449 MangleReturnType = true;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000450
Anders Carlsson9234b7f2009-09-17 03:46:43 +0000451 // Mangle the type of the primary template.
452 FD = PrimaryTemplate->getTemplatedDecl();
453 }
454
Douglas Gregor79e6bd32011-07-12 04:42:08 +0000455 mangleBareFunctionType(FD->getType()->getAs<FunctionType>(),
456 MangleReturnType);
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000457}
458
Anders Carlsson47846d22009-12-04 06:23:23 +0000459static const DeclContext *IgnoreLinkageSpecDecls(const DeclContext *DC) {
460 while (isa<LinkageSpecDecl>(DC)) {
Anders Carlsson47846d22009-12-04 06:23:23 +0000461 DC = DC->getParent();
462 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000463
Anders Carlsson47846d22009-12-04 06:23:23 +0000464 return DC;
465}
466
Anders Carlssonc820f902010-06-02 15:58:27 +0000467/// isStd - Return whether a given namespace is the 'std' namespace.
468static bool isStd(const NamespaceDecl *NS) {
469 if (!IgnoreLinkageSpecDecls(NS->getParent())->isTranslationUnit())
470 return false;
471
472 const IdentifierInfo *II = NS->getOriginalNamespace()->getIdentifier();
473 return II && II->isStr("std");
474}
475
Anders Carlsson47846d22009-12-04 06:23:23 +0000476// isStdNamespace - Return whether a given decl context is a toplevel 'std'
477// namespace.
Daniel Dunbar1308af92009-11-21 09:11:45 +0000478static bool isStdNamespace(const DeclContext *DC) {
Anders Carlsson47846d22009-12-04 06:23:23 +0000479 if (!DC->isNamespace())
480 return false;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000481
Anders Carlsson47846d22009-12-04 06:23:23 +0000482 return isStd(cast<NamespaceDecl>(DC));
Daniel Dunbar1308af92009-11-21 09:11:45 +0000483}
484
Anders Carlssonbb36ba42009-09-26 03:24:57 +0000485static const TemplateDecl *
486isTemplate(const NamedDecl *ND, const TemplateArgumentList *&TemplateArgs) {
Anders Carlsson2744a062009-09-18 19:00:18 +0000487 // Check if we have a function template.
488 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)){
Anders Carlssonbb36ba42009-09-26 03:24:57 +0000489 if (const TemplateDecl *TD = FD->getPrimaryTemplate()) {
Anders Carlsson2744a062009-09-18 19:00:18 +0000490 TemplateArgs = FD->getTemplateSpecializationArgs();
Anders Carlssonbb36ba42009-09-26 03:24:57 +0000491 return TD;
Anders Carlsson2744a062009-09-18 19:00:18 +0000492 }
493 }
494
Anders Carlssoneafc6dc2009-09-18 19:44:50 +0000495 // Check if we have a class template.
496 if (const ClassTemplateSpecializationDecl *Spec =
497 dyn_cast<ClassTemplateSpecializationDecl>(ND)) {
498 TemplateArgs = &Spec->getTemplateArgs();
Anders Carlssonbb36ba42009-09-26 03:24:57 +0000499 return Spec->getSpecializedTemplate();
Anders Carlssoneafc6dc2009-09-18 19:44:50 +0000500 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000501
Anders Carlsson2744a062009-09-18 19:00:18 +0000502 return 0;
503}
504
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000505void CXXNameMangler::mangleName(const NamedDecl *ND) {
506 // <name> ::= <nested-name>
507 // ::= <unscoped-name>
508 // ::= <unscoped-template-name> <template-args>
Anders Carlsson201ce742009-09-17 03:17:01 +0000509 // ::= <local-name>
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000510 //
Anders Carlssond58d6f72009-09-17 16:12:20 +0000511 const DeclContext *DC = ND->getDeclContext();
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000512
Eli Friedman7facf842009-12-02 20:32:49 +0000513 // If this is an extern variable declared locally, the relevant DeclContext
514 // is that of the containing namespace, or the translation unit.
515 if (isa<FunctionDecl>(DC) && ND->hasLinkage())
516 while (!DC->isNamespace() && !DC->isTranslationUnit())
517 DC = DC->getParent();
John McCall82b7d7b2010-10-18 21:28:44 +0000518 else if (GetLocalClassDecl(ND)) {
519 mangleLocalName(ND);
520 return;
521 }
Eli Friedman7facf842009-12-02 20:32:49 +0000522
Anders Carlsson5cc58c62009-09-22 17:23:30 +0000523 while (isa<LinkageSpecDecl>(DC))
Anders Carlssond58d6f72009-09-17 16:12:20 +0000524 DC = DC->getParent();
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000525
Anders Carlssond58d6f72009-09-17 16:12:20 +0000526 if (DC->isTranslationUnit() || isStdNamespace(DC)) {
Anders Carlsson2744a062009-09-18 19:00:18 +0000527 // Check if we have a template.
528 const TemplateArgumentList *TemplateArgs = 0;
Anders Carlsson0fa6df42009-09-26 19:45:45 +0000529 if (const TemplateDecl *TD = isTemplate(ND, TemplateArgs)) {
Anders Carlsson2744a062009-09-18 19:00:18 +0000530 mangleUnscopedTemplateName(TD);
Rafael Espindolad9800722010-03-11 14:07:00 +0000531 TemplateParameterList *TemplateParameters = TD->getTemplateParameters();
532 mangleTemplateArgs(*TemplateParameters, *TemplateArgs);
Anders Carlsson2744a062009-09-18 19:00:18 +0000533 return;
Anders Carlsson7482e242009-09-18 04:29:09 +0000534 }
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000535
Anders Carlsson7482e242009-09-18 04:29:09 +0000536 mangleUnscopedName(ND);
537 return;
538 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000539
Anders Carlsson7b06f6c2009-12-10 03:14:39 +0000540 if (isa<FunctionDecl>(DC) || isa<ObjCMethodDecl>(DC)) {
Anders Carlsson7482e242009-09-18 04:29:09 +0000541 mangleLocalName(ND);
542 return;
543 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000544
Eli Friedman7facf842009-12-02 20:32:49 +0000545 mangleNestedName(ND, DC);
Anders Carlsson7482e242009-09-18 04:29:09 +0000546}
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000547void CXXNameMangler::mangleName(const TemplateDecl *TD,
Anders Carlsson7624f212009-09-18 02:42:01 +0000548 const TemplateArgument *TemplateArgs,
549 unsigned NumTemplateArgs) {
Anders Carlsson47846d22009-12-04 06:23:23 +0000550 const DeclContext *DC = IgnoreLinkageSpecDecls(TD->getDeclContext());
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000551
Anders Carlsson7624f212009-09-18 02:42:01 +0000552 if (DC->isTranslationUnit() || isStdNamespace(DC)) {
Anders Carlsson0fa6df42009-09-26 19:45:45 +0000553 mangleUnscopedTemplateName(TD);
Rafael Espindolad9800722010-03-11 14:07:00 +0000554 TemplateParameterList *TemplateParameters = TD->getTemplateParameters();
555 mangleTemplateArgs(*TemplateParameters, TemplateArgs, NumTemplateArgs);
Anders Carlsson7624f212009-09-18 02:42:01 +0000556 } else {
557 mangleNestedName(TD, TemplateArgs, NumTemplateArgs);
558 }
559}
560
Anders Carlsson201ce742009-09-17 03:17:01 +0000561void CXXNameMangler::mangleUnscopedName(const NamedDecl *ND) {
562 // <unscoped-name> ::= <unqualified-name>
563 // ::= St <unqualified-name> # ::std::
564 if (isStdNamespace(ND->getDeclContext()))
565 Out << "St";
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000566
Anders Carlsson201ce742009-09-17 03:17:01 +0000567 mangleUnqualifiedName(ND);
568}
569
Anders Carlsson0fa6df42009-09-26 19:45:45 +0000570void CXXNameMangler::mangleUnscopedTemplateName(const TemplateDecl *ND) {
Anders Carlsson201ce742009-09-17 03:17:01 +0000571 // <unscoped-template-name> ::= <unscoped-name>
572 // ::= <substitution>
Anders Carlsson7624f212009-09-18 02:42:01 +0000573 if (mangleSubstitution(ND))
Anders Carlsson03c9d532009-09-17 04:02:31 +0000574 return;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000575
Douglas Gregor32fb4e12010-02-05 20:45:00 +0000576 // <template-template-param> ::= <template-param>
577 if (const TemplateTemplateParmDecl *TTP
578 = dyn_cast<TemplateTemplateParmDecl>(ND)) {
579 mangleTemplateParameter(TTP->getIndex());
580 return;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000581 }
Douglas Gregor32fb4e12010-02-05 20:45:00 +0000582
Anders Carlsson1668f202009-09-26 20:13:56 +0000583 mangleUnscopedName(ND->getTemplatedDecl());
Anders Carlsson7624f212009-09-18 02:42:01 +0000584 addSubstitution(ND);
Anders Carlsson201ce742009-09-17 03:17:01 +0000585}
586
Douglas Gregor1e9268e2010-04-28 05:58:56 +0000587void CXXNameMangler::mangleUnscopedTemplateName(TemplateName Template) {
588 // <unscoped-template-name> ::= <unscoped-name>
589 // ::= <substitution>
590 if (TemplateDecl *TD = Template.getAsTemplateDecl())
591 return mangleUnscopedTemplateName(TD);
Sean Huntc3021132010-05-05 15:23:54 +0000592
Douglas Gregor1e9268e2010-04-28 05:58:56 +0000593 if (mangleSubstitution(Template))
594 return;
595
Douglas Gregor1e9268e2010-04-28 05:58:56 +0000596 DependentTemplateName *Dependent = Template.getAsDependentTemplateName();
597 assert(Dependent && "Not a dependent template name?");
Douglas Gregor19617912011-07-12 05:06:05 +0000598 if (const IdentifierInfo *Id = Dependent->getIdentifier())
599 mangleSourceName(Id);
600 else
601 mangleOperatorName(Dependent->getOperator(), UnknownArity);
Sean Huntc3021132010-05-05 15:23:54 +0000602
Douglas Gregor1e9268e2010-04-28 05:58:56 +0000603 addSubstitution(Template);
604}
605
John McCall1b600522011-04-24 03:07:16 +0000606void CXXNameMangler::mangleFloat(const llvm::APFloat &f) {
607 // ABI:
608 // Floating-point literals are encoded using a fixed-length
609 // lowercase hexadecimal string corresponding to the internal
610 // representation (IEEE on Itanium), high-order bytes first,
611 // without leading zeroes. For example: "Lf bf800000 E" is -1.0f
612 // on Itanium.
613 // APInt::toString uses uppercase hexadecimal, and it's not really
614 // worth embellishing that interface for this use case, so we just
615 // do a second pass to lowercase things.
616 typedef llvm::SmallString<20> buffer_t;
617 buffer_t buffer;
618 f.bitcastToAPInt().toString(buffer, 16, false);
619
620 for (buffer_t::iterator i = buffer.begin(), e = buffer.end(); i != e; ++i)
621 if (isupper(*i)) *i = tolower(*i);
622
623 Out.write(buffer.data(), buffer.size());
John McCall0512e482010-07-14 04:20:34 +0000624}
625
626void CXXNameMangler::mangleNumber(const llvm::APSInt &Value) {
627 if (Value.isSigned() && Value.isNegative()) {
628 Out << 'n';
629 Value.abs().print(Out, true);
630 } else
631 Value.print(Out, Value.isSigned());
632}
633
Anders Carlssona94822e2009-11-26 02:32:05 +0000634void CXXNameMangler::mangleNumber(int64_t Number) {
635 // <number> ::= [n] <non-negative decimal integer>
636 if (Number < 0) {
637 Out << 'n';
638 Number = -Number;
639 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000640
Anders Carlssona94822e2009-11-26 02:32:05 +0000641 Out << Number;
642}
643
Anders Carlsson19879c92010-03-23 17:17:29 +0000644void CXXNameMangler::mangleCallOffset(int64_t NonVirtual, int64_t Virtual) {
Mike Stump141c5af2009-09-02 00:25:38 +0000645 // <call-offset> ::= h <nv-offset> _
646 // ::= v <v-offset> _
647 // <nv-offset> ::= <offset number> # non-virtual base override
Anders Carlssona94822e2009-11-26 02:32:05 +0000648 // <v-offset> ::= <offset number> _ <virtual offset number>
Mike Stump141c5af2009-09-02 00:25:38 +0000649 // # virtual base override, with vcall offset
Anders Carlsson19879c92010-03-23 17:17:29 +0000650 if (!Virtual) {
Anders Carlssona94822e2009-11-26 02:32:05 +0000651 Out << 'h';
Anders Carlsson19879c92010-03-23 17:17:29 +0000652 mangleNumber(NonVirtual);
Anders Carlssona94822e2009-11-26 02:32:05 +0000653 Out << '_';
654 return;
Mike Stump141c5af2009-09-02 00:25:38 +0000655 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000656
Anders Carlssona94822e2009-11-26 02:32:05 +0000657 Out << 'v';
Anders Carlsson19879c92010-03-23 17:17:29 +0000658 mangleNumber(NonVirtual);
Anders Carlssona94822e2009-11-26 02:32:05 +0000659 Out << '_';
Anders Carlsson19879c92010-03-23 17:17:29 +0000660 mangleNumber(Virtual);
Anders Carlssona94822e2009-11-26 02:32:05 +0000661 Out << '_';
Mike Stump9124bcc2009-09-02 00:56:18 +0000662}
663
John McCall4f4e4132011-05-04 01:45:19 +0000664void CXXNameMangler::manglePrefix(QualType type) {
John McCalla0ce15c2011-04-24 08:23:24 +0000665 if (const TemplateSpecializationType *TST =
666 type->getAs<TemplateSpecializationType>()) {
667 if (!mangleSubstitution(QualType(TST, 0))) {
668 mangleTemplatePrefix(TST->getTemplateName());
Sean Huntc3021132010-05-05 15:23:54 +0000669
Douglas Gregoraa2187d2011-02-28 00:04:36 +0000670 // FIXME: GCC does not appear to mangle the template arguments when
671 // the template in question is a dependent template name. Should we
672 // emulate that badness?
John McCalla0ce15c2011-04-24 08:23:24 +0000673 mangleTemplateArgs(TST->getTemplateName(), TST->getArgs(),
674 TST->getNumArgs());
675 addSubstitution(QualType(TST, 0));
Rafael Espindola9b35b252010-03-17 04:28:11 +0000676 }
John McCalla0ce15c2011-04-24 08:23:24 +0000677 } else if (const DependentTemplateSpecializationType *DTST
678 = type->getAs<DependentTemplateSpecializationType>()) {
679 TemplateName Template
680 = getASTContext().getDependentTemplateName(DTST->getQualifier(),
681 DTST->getIdentifier());
682 mangleTemplatePrefix(Template);
683
684 // FIXME: GCC does not appear to mangle the template arguments when
685 // the template in question is a dependent template name. Should we
686 // emulate that badness?
687 mangleTemplateArgs(Template, DTST->getArgs(), DTST->getNumArgs());
688 } else {
689 // We use the QualType mangle type variant here because it handles
690 // substitutions.
691 mangleType(type);
John McCall1dd73832010-02-04 01:42:13 +0000692 }
693}
694
John McCalla0ce15c2011-04-24 08:23:24 +0000695/// Mangle everything prior to the base-unresolved-name in an unresolved-name.
696///
697/// \param firstQualifierLookup - the entity found by unqualified lookup
698/// for the first name in the qualifier, if this is for a member expression
699/// \param recursive - true if this is being called recursively,
700/// i.e. if there is more prefix "to the right".
701void CXXNameMangler::mangleUnresolvedPrefix(NestedNameSpecifier *qualifier,
702 NamedDecl *firstQualifierLookup,
703 bool recursive) {
John McCall1dd73832010-02-04 01:42:13 +0000704
John McCalla0ce15c2011-04-24 08:23:24 +0000705 // x, ::x
706 // <unresolved-name> ::= [gs] <base-unresolved-name>
707
708 // T::x / decltype(p)::x
709 // <unresolved-name> ::= sr <unresolved-type> <base-unresolved-name>
710
711 // T::N::x /decltype(p)::N::x
712 // <unresolved-name> ::= srN <unresolved-type> <unresolved-qualifier-level>+ E
713 // <base-unresolved-name>
714
715 // A::x, N::y, A<T>::z; "gs" means leading "::"
716 // <unresolved-name> ::= [gs] sr <unresolved-qualifier-level>+ E
717 // <base-unresolved-name>
718
719 switch (qualifier->getKind()) {
720 case NestedNameSpecifier::Global:
721 Out << "gs";
722
723 // We want an 'sr' unless this is the entire NNS.
724 if (recursive)
725 Out << "sr";
726
727 // We never want an 'E' here.
728 return;
729
730 case NestedNameSpecifier::Namespace:
731 if (qualifier->getPrefix())
732 mangleUnresolvedPrefix(qualifier->getPrefix(), firstQualifierLookup,
733 /*recursive*/ true);
734 else
735 Out << "sr";
736 mangleSourceName(qualifier->getAsNamespace()->getIdentifier());
737 break;
738 case NestedNameSpecifier::NamespaceAlias:
739 if (qualifier->getPrefix())
740 mangleUnresolvedPrefix(qualifier->getPrefix(), firstQualifierLookup,
741 /*recursive*/ true);
742 else
743 Out << "sr";
744 mangleSourceName(qualifier->getAsNamespaceAlias()->getIdentifier());
745 break;
746
747 case NestedNameSpecifier::TypeSpec:
748 case NestedNameSpecifier::TypeSpecWithTemplate: {
John McCall4f4e4132011-05-04 01:45:19 +0000749 const Type *type = qualifier->getAsType();
John McCalla0ce15c2011-04-24 08:23:24 +0000750
John McCall4f4e4132011-05-04 01:45:19 +0000751 // We only want to use an unresolved-type encoding if this is one of:
752 // - a decltype
753 // - a template type parameter
754 // - a template template parameter with arguments
755 // In all of these cases, we should have no prefix.
756 if (qualifier->getPrefix()) {
757 mangleUnresolvedPrefix(qualifier->getPrefix(), firstQualifierLookup,
758 /*recursive*/ true);
759 } else {
760 // Otherwise, all the cases want this.
761 Out << "sr";
John McCall4f4e4132011-05-04 01:45:19 +0000762 }
763
John McCall4f4e4132011-05-04 01:45:19 +0000764 // Only certain other types are valid as prefixes; enumerate them.
John McCalld3d49bb2011-06-28 16:49:23 +0000765 switch (type->getTypeClass()) {
766 case Type::Builtin:
767 case Type::Complex:
768 case Type::Pointer:
769 case Type::BlockPointer:
770 case Type::LValueReference:
771 case Type::RValueReference:
772 case Type::MemberPointer:
773 case Type::ConstantArray:
774 case Type::IncompleteArray:
775 case Type::VariableArray:
776 case Type::DependentSizedArray:
777 case Type::DependentSizedExtVector:
778 case Type::Vector:
779 case Type::ExtVector:
780 case Type::FunctionProto:
781 case Type::FunctionNoProto:
782 case Type::Enum:
783 case Type::Paren:
784 case Type::Elaborated:
785 case Type::Attributed:
786 case Type::Auto:
787 case Type::PackExpansion:
John McCalld3d49bb2011-06-28 16:49:23 +0000788 case Type::ObjCObject:
789 case Type::ObjCInterface:
790 case Type::ObjCObjectPointer:
791 llvm_unreachable("type is illegal as a nested name specifier");
792
John McCall68a51a72011-07-01 00:04:39 +0000793 case Type::SubstTemplateTypeParmPack:
794 // FIXME: not clear how to mangle this!
795 // template <class T...> class A {
796 // template <class U...> void foo(decltype(T::foo(U())) x...);
797 // };
798 Out << "_SUBSTPACK_";
799 break;
800
John McCalld3d49bb2011-06-28 16:49:23 +0000801 // <unresolved-type> ::= <template-param>
802 // ::= <decltype>
803 // ::= <template-template-param> <template-args>
804 // (this last is not official yet)
805 case Type::TypeOfExpr:
806 case Type::TypeOf:
807 case Type::Decltype:
808 case Type::TemplateTypeParm:
809 case Type::UnaryTransform:
John McCall35ee32e2011-07-01 02:19:08 +0000810 case Type::SubstTemplateTypeParm:
John McCalld3d49bb2011-06-28 16:49:23 +0000811 unresolvedType:
812 assert(!qualifier->getPrefix());
813
814 // We only get here recursively if we're followed by identifiers.
815 if (recursive) Out << 'N';
816
John McCall35ee32e2011-07-01 02:19:08 +0000817 // This seems to do everything we want. It's not really
818 // sanctioned for a substituted template parameter, though.
John McCalld3d49bb2011-06-28 16:49:23 +0000819 mangleType(QualType(type, 0));
820
821 // We never want to print 'E' directly after an unresolved-type,
822 // so we return directly.
823 return;
824
John McCalld3d49bb2011-06-28 16:49:23 +0000825 case Type::Typedef:
826 mangleSourceName(cast<TypedefType>(type)->getDecl()->getIdentifier());
827 break;
828
829 case Type::UnresolvedUsing:
830 mangleSourceName(cast<UnresolvedUsingType>(type)->getDecl()
831 ->getIdentifier());
832 break;
833
834 case Type::Record:
835 mangleSourceName(cast<RecordType>(type)->getDecl()->getIdentifier());
836 break;
837
838 case Type::TemplateSpecialization: {
839 const TemplateSpecializationType *tst
840 = cast<TemplateSpecializationType>(type);
John McCall68a51a72011-07-01 00:04:39 +0000841 TemplateName name = tst->getTemplateName();
842 switch (name.getKind()) {
843 case TemplateName::Template:
844 case TemplateName::QualifiedTemplate: {
845 TemplateDecl *temp = name.getAsTemplateDecl();
John McCalld3d49bb2011-06-28 16:49:23 +0000846
John McCall68a51a72011-07-01 00:04:39 +0000847 // If the base is a template template parameter, this is an
848 // unresolved type.
849 assert(temp && "no template for template specialization type");
850 if (isa<TemplateTemplateParmDecl>(temp)) goto unresolvedType;
John McCalld3d49bb2011-06-28 16:49:23 +0000851
John McCall68a51a72011-07-01 00:04:39 +0000852 mangleSourceName(temp->getIdentifier());
853 break;
854 }
855
856 case TemplateName::OverloadedTemplate:
857 case TemplateName::DependentTemplate:
858 llvm_unreachable("invalid base for a template specialization type");
859
860 case TemplateName::SubstTemplateTemplateParm: {
861 SubstTemplateTemplateParmStorage *subst
862 = name.getAsSubstTemplateTemplateParm();
863 mangleExistingSubstitution(subst->getReplacement());
864 break;
865 }
866
867 case TemplateName::SubstTemplateTemplateParmPack: {
868 // FIXME: not clear how to mangle this!
869 // template <template <class U> class T...> class A {
870 // template <class U...> void foo(decltype(T<U>::foo) x...);
871 // };
872 Out << "_SUBSTPACK_";
873 break;
874 }
875 }
876
John McCall4f4e4132011-05-04 01:45:19 +0000877 mangleUnresolvedTemplateArgs(tst->getArgs(), tst->getNumArgs());
John McCalld3d49bb2011-06-28 16:49:23 +0000878 break;
879 }
880
881 case Type::InjectedClassName:
882 mangleSourceName(cast<InjectedClassNameType>(type)->getDecl()
883 ->getIdentifier());
884 break;
885
886 case Type::DependentName:
887 mangleSourceName(cast<DependentNameType>(type)->getIdentifier());
888 break;
889
890 case Type::DependentTemplateSpecialization: {
891 const DependentTemplateSpecializationType *tst
892 = cast<DependentTemplateSpecializationType>(type);
John McCall4f4e4132011-05-04 01:45:19 +0000893 mangleSourceName(tst->getIdentifier());
894 mangleUnresolvedTemplateArgs(tst->getArgs(), tst->getNumArgs());
John McCalld3d49bb2011-06-28 16:49:23 +0000895 break;
896 }
John McCall4f4e4132011-05-04 01:45:19 +0000897 }
898 break;
John McCalla0ce15c2011-04-24 08:23:24 +0000899 }
900
901 case NestedNameSpecifier::Identifier:
902 // Member expressions can have these without prefixes.
903 if (qualifier->getPrefix()) {
904 mangleUnresolvedPrefix(qualifier->getPrefix(), firstQualifierLookup,
905 /*recursive*/ true);
906 } else if (firstQualifierLookup) {
907
908 // Try to make a proper qualifier out of the lookup result, and
909 // then just recurse on that.
910 NestedNameSpecifier *newQualifier;
911 if (TypeDecl *typeDecl = dyn_cast<TypeDecl>(firstQualifierLookup)) {
912 QualType type = getASTContext().getTypeDeclType(typeDecl);
913
914 // Pretend we had a different nested name specifier.
915 newQualifier = NestedNameSpecifier::Create(getASTContext(),
916 /*prefix*/ 0,
917 /*template*/ false,
918 type.getTypePtr());
919 } else if (NamespaceDecl *nspace =
920 dyn_cast<NamespaceDecl>(firstQualifierLookup)) {
921 newQualifier = NestedNameSpecifier::Create(getASTContext(),
922 /*prefix*/ 0,
923 nspace);
924 } else if (NamespaceAliasDecl *alias =
925 dyn_cast<NamespaceAliasDecl>(firstQualifierLookup)) {
926 newQualifier = NestedNameSpecifier::Create(getASTContext(),
927 /*prefix*/ 0,
928 alias);
929 } else {
930 // No sensible mangling to do here.
931 newQualifier = 0;
932 }
933
934 if (newQualifier)
935 return mangleUnresolvedPrefix(newQualifier, /*lookup*/ 0, recursive);
936
937 } else {
938 Out << "sr";
939 }
940
941 mangleSourceName(qualifier->getAsIdentifier());
942 break;
943 }
944
945 // If this was the innermost part of the NNS, and we fell out to
946 // here, append an 'E'.
947 if (!recursive)
948 Out << 'E';
949}
950
951/// Mangle an unresolved-name, which is generally used for names which
952/// weren't resolved to specific entities.
953void CXXNameMangler::mangleUnresolvedName(NestedNameSpecifier *qualifier,
954 NamedDecl *firstQualifierLookup,
955 DeclarationName name,
956 unsigned knownArity) {
957 if (qualifier) mangleUnresolvedPrefix(qualifier, firstQualifierLookup);
958 mangleUnqualifiedName(0, name, knownArity);
John McCall1dd73832010-02-04 01:42:13 +0000959}
960
Anders Carlsson6f7e2f42010-06-08 14:49:03 +0000961static const FieldDecl *FindFirstNamedDataMember(const RecordDecl *RD) {
962 assert(RD->isAnonymousStructOrUnion() &&
963 "Expected anonymous struct or union!");
964
965 for (RecordDecl::field_iterator I = RD->field_begin(), E = RD->field_end();
966 I != E; ++I) {
967 const FieldDecl *FD = *I;
968
969 if (FD->getIdentifier())
970 return FD;
971
972 if (const RecordType *RT = FD->getType()->getAs<RecordType>()) {
973 if (const FieldDecl *NamedDataMember =
974 FindFirstNamedDataMember(RT->getDecl()))
975 return NamedDataMember;
976 }
977 }
978
979 // We didn't find a named data member.
980 return 0;
981}
982
John McCall1dd73832010-02-04 01:42:13 +0000983void CXXNameMangler::mangleUnqualifiedName(const NamedDecl *ND,
984 DeclarationName Name,
985 unsigned KnownArity) {
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000986 // <unqualified-name> ::= <operator-name>
Mike Stump1eb44332009-09-09 15:08:12 +0000987 // ::= <ctor-dtor-name>
988 // ::= <source-name>
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000989 switch (Name.getNameKind()) {
Anders Carlssonc4355b62009-10-07 01:45:02 +0000990 case DeclarationName::Identifier: {
Anders Carlssonc4355b62009-10-07 01:45:02 +0000991 if (const IdentifierInfo *II = Name.getAsIdentifierInfo()) {
Sean Hunt31455252010-01-24 03:04:27 +0000992 // We must avoid conflicts between internally- and externally-
John McCall74990f42011-03-22 06:34:45 +0000993 // linked variable and function declaration names in the same TU:
994 // void test() { extern void foo(); }
995 // static void foo();
996 // This naming convention is the same as that followed by GCC,
997 // though it shouldn't actually matter.
998 if (ND && ND->getLinkage() == InternalLinkage &&
Sean Hunt31455252010-01-24 03:04:27 +0000999 ND->getDeclContext()->isFileContext())
1000 Out << 'L';
1001
Anders Carlssonc4355b62009-10-07 01:45:02 +00001002 mangleSourceName(II);
1003 break;
1004 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00001005
John McCall1dd73832010-02-04 01:42:13 +00001006 // Otherwise, an anonymous entity. We must have a declaration.
1007 assert(ND && "mangling empty name without declaration");
1008
1009 if (const NamespaceDecl *NS = dyn_cast<NamespaceDecl>(ND)) {
1010 if (NS->isAnonymousNamespace()) {
1011 // This is how gcc mangles these names.
1012 Out << "12_GLOBAL__N_1";
1013 break;
1014 }
1015 }
1016
Anders Carlsson6f7e2f42010-06-08 14:49:03 +00001017 if (const VarDecl *VD = dyn_cast<VarDecl>(ND)) {
1018 // We must have an anonymous union or struct declaration.
1019 const RecordDecl *RD =
1020 cast<RecordDecl>(VD->getType()->getAs<RecordType>()->getDecl());
1021
1022 // Itanium C++ ABI 5.1.2:
1023 //
1024 // For the purposes of mangling, the name of an anonymous union is
1025 // considered to be the name of the first named data member found by a
1026 // pre-order, depth-first, declaration-order walk of the data members of
1027 // the anonymous union. If there is no such data member (i.e., if all of
1028 // the data members in the union are unnamed), then there is no way for
1029 // a program to refer to the anonymous union, and there is therefore no
1030 // need to mangle its name.
1031 const FieldDecl *FD = FindFirstNamedDataMember(RD);
John McCall7121c8f2010-08-05 22:02:13 +00001032
1033 // It's actually possible for various reasons for us to get here
1034 // with an empty anonymous struct / union. Fortunately, it
1035 // doesn't really matter what name we generate.
1036 if (!FD) break;
Anders Carlsson6f7e2f42010-06-08 14:49:03 +00001037 assert(FD->getIdentifier() && "Data member name isn't an identifier!");
1038
1039 mangleSourceName(FD->getIdentifier());
1040 break;
1041 }
1042
Anders Carlssonc4355b62009-10-07 01:45:02 +00001043 // We must have an anonymous struct.
1044 const TagDecl *TD = cast<TagDecl>(ND);
Richard Smith162e1c12011-04-15 14:24:37 +00001045 if (const TypedefNameDecl *D = TD->getTypedefNameForAnonDecl()) {
Anders Carlssonc4355b62009-10-07 01:45:02 +00001046 assert(TD->getDeclContext() == D->getDeclContext() &&
1047 "Typedef should not be in another decl context!");
1048 assert(D->getDeclName().getAsIdentifierInfo() &&
1049 "Typedef was not named!");
1050 mangleSourceName(D->getDeclName().getAsIdentifierInfo());
1051 break;
1052 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00001053
Anders Carlssonc4355b62009-10-07 01:45:02 +00001054 // Get a unique id for the anonymous struct.
1055 uint64_t AnonStructId = Context.getAnonymousStructId(TD);
1056
1057 // Mangle it as a source name in the form
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00001058 // [n] $_<id>
Anders Carlssonc4355b62009-10-07 01:45:02 +00001059 // where n is the length of the string.
1060 llvm::SmallString<8> Str;
1061 Str += "$_";
1062 Str += llvm::utostr(AnonStructId);
1063
1064 Out << Str.size();
1065 Out << Str.str();
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001066 break;
Anders Carlssonc4355b62009-10-07 01:45:02 +00001067 }
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001068
1069 case DeclarationName::ObjCZeroArgSelector:
1070 case DeclarationName::ObjCOneArgSelector:
1071 case DeclarationName::ObjCMultiArgSelector:
David Blaikieb219cfc2011-09-23 05:06:16 +00001072 llvm_unreachable("Can't mangle Objective-C selector names here!");
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001073
1074 case DeclarationName::CXXConstructorName:
Anders Carlsson27ae5362009-04-17 01:58:57 +00001075 if (ND == Structor)
Mike Stump141c5af2009-09-02 00:25:38 +00001076 // If the named decl is the C++ constructor we're mangling, use the type
1077 // we were given.
Anders Carlsson27ae5362009-04-17 01:58:57 +00001078 mangleCXXCtorType(static_cast<CXXCtorType>(StructorType));
Anders Carlsson3ac86b52009-04-15 05:36:58 +00001079 else
1080 // Otherwise, use the complete constructor name. This is relevant if a
1081 // class with a constructor is declared within a constructor.
1082 mangleCXXCtorType(Ctor_Complete);
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001083 break;
1084
1085 case DeclarationName::CXXDestructorName:
Anders Carlsson27ae5362009-04-17 01:58:57 +00001086 if (ND == Structor)
Mike Stump141c5af2009-09-02 00:25:38 +00001087 // If the named decl is the C++ destructor we're mangling, use the type we
1088 // were given.
Anders Carlsson27ae5362009-04-17 01:58:57 +00001089 mangleCXXDtorType(static_cast<CXXDtorType>(StructorType));
1090 else
1091 // Otherwise, use the complete destructor name. This is relevant if a
1092 // class with a destructor is declared within a destructor.
1093 mangleCXXDtorType(Dtor_Complete);
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001094 break;
1095
1096 case DeclarationName::CXXConversionFunctionName:
Mike Stump1eb44332009-09-09 15:08:12 +00001097 // <operator-name> ::= cv <type> # (cast)
Douglas Gregor219cc612009-02-13 01:28:03 +00001098 Out << "cv";
Douglas Gregor79e6bd32011-07-12 04:42:08 +00001099 mangleType(Name.getCXXNameType());
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001100 break;
1101
Anders Carlsson8257d412009-12-22 06:36:32 +00001102 case DeclarationName::CXXOperatorName: {
John McCall1dd73832010-02-04 01:42:13 +00001103 unsigned Arity;
1104 if (ND) {
1105 Arity = cast<FunctionDecl>(ND)->getNumParams();
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001106
John McCall1dd73832010-02-04 01:42:13 +00001107 // If we have a C++ member function, we need to include the 'this' pointer.
1108 // FIXME: This does not make sense for operators that are static, but their
1109 // names stay the same regardless of the arity (operator new for instance).
1110 if (isa<CXXMethodDecl>(ND))
1111 Arity++;
1112 } else
1113 Arity = KnownArity;
1114
Anders Carlsson8257d412009-12-22 06:36:32 +00001115 mangleOperatorName(Name.getCXXOverloadedOperator(), Arity);
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001116 break;
Anders Carlsson8257d412009-12-22 06:36:32 +00001117 }
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001118
Sean Hunt3e518bd2009-11-29 07:34:05 +00001119 case DeclarationName::CXXLiteralOperatorName:
Sean Hunt5dd6b392009-12-04 21:11:13 +00001120 // FIXME: This mangling is not yet official.
Sean Hunt2421f662009-12-04 21:01:37 +00001121 Out << "li";
Sean Hunt3e518bd2009-11-29 07:34:05 +00001122 mangleSourceName(Name.getCXXLiteralIdentifier());
1123 break;
1124
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001125 case DeclarationName::CXXUsingDirective:
David Blaikieb219cfc2011-09-23 05:06:16 +00001126 llvm_unreachable("Can't mangle a using directive name!");
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001127 }
1128}
1129
1130void CXXNameMangler::mangleSourceName(const IdentifierInfo *II) {
1131 // <source-name> ::= <positive length number> <identifier>
1132 // <number> ::= [n] <non-negative decimal integer>
1133 // <identifier> ::= <unqualified source code identifier>
1134 Out << II->getLength() << II->getName();
1135}
1136
Eli Friedman7facf842009-12-02 20:32:49 +00001137void CXXNameMangler::mangleNestedName(const NamedDecl *ND,
Fariborz Jahanian57058532010-03-03 19:41:08 +00001138 const DeclContext *DC,
1139 bool NoFunction) {
Douglas Gregor0a9a6d62011-01-26 17:36:28 +00001140 // <nested-name>
1141 // ::= N [<CV-qualifiers>] [<ref-qualifier>] <prefix> <unqualified-name> E
1142 // ::= N [<CV-qualifiers>] [<ref-qualifier>] <template-prefix>
1143 // <template-args> E
Anders Carlssond99edc42009-09-26 03:55:37 +00001144
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001145 Out << 'N';
Douglas Gregor0a9a6d62011-01-26 17:36:28 +00001146 if (const CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(ND)) {
John McCall0953e762009-09-24 19:53:00 +00001147 mangleQualifiers(Qualifiers::fromCVRMask(Method->getTypeQualifiers()));
Douglas Gregor0a9a6d62011-01-26 17:36:28 +00001148 mangleRefQualifier(Method->getRefQualifier());
1149 }
1150
Anders Carlsson2744a062009-09-18 19:00:18 +00001151 // Check if we have a template.
1152 const TemplateArgumentList *TemplateArgs = 0;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00001153 if (const TemplateDecl *TD = isTemplate(ND, TemplateArgs)) {
Anders Carlsson2744a062009-09-18 19:00:18 +00001154 mangleTemplatePrefix(TD);
Rafael Espindolad9800722010-03-11 14:07:00 +00001155 TemplateParameterList *TemplateParameters = TD->getTemplateParameters();
1156 mangleTemplateArgs(*TemplateParameters, *TemplateArgs);
Fariborz Jahanian57058532010-03-03 19:41:08 +00001157 }
1158 else {
1159 manglePrefix(DC, NoFunction);
Anders Carlsson7482e242009-09-18 04:29:09 +00001160 mangleUnqualifiedName(ND);
1161 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00001162
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001163 Out << 'E';
1164}
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00001165void CXXNameMangler::mangleNestedName(const TemplateDecl *TD,
Anders Carlsson7624f212009-09-18 02:42:01 +00001166 const TemplateArgument *TemplateArgs,
1167 unsigned NumTemplateArgs) {
Anders Carlssone45117b2009-09-27 19:53:49 +00001168 // <nested-name> ::= N [<CV-qualifiers>] <template-prefix> <template-args> E
1169
Anders Carlsson7624f212009-09-18 02:42:01 +00001170 Out << 'N';
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00001171
Anders Carlssone45117b2009-09-27 19:53:49 +00001172 mangleTemplatePrefix(TD);
Rafael Espindolad9800722010-03-11 14:07:00 +00001173 TemplateParameterList *TemplateParameters = TD->getTemplateParameters();
1174 mangleTemplateArgs(*TemplateParameters, TemplateArgs, NumTemplateArgs);
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00001175
Anders Carlsson7624f212009-09-18 02:42:01 +00001176 Out << 'E';
1177}
1178
Anders Carlsson1b42c792009-04-02 16:24:45 +00001179void CXXNameMangler::mangleLocalName(const NamedDecl *ND) {
1180 // <local-name> := Z <function encoding> E <entity name> [<discriminator>]
1181 // := Z <function encoding> E s [<discriminator>]
Mike Stump1eb44332009-09-09 15:08:12 +00001182 // <discriminator> := _ <non-negative number>
Fariborz Jahanian57058532010-03-03 19:41:08 +00001183 const DeclContext *DC = ND->getDeclContext();
Fariborz Jahanian8805fe82011-06-09 19:25:01 +00001184 if (isa<ObjCMethodDecl>(DC) && isa<FunctionDecl>(ND)) {
1185 // Don't add objc method name mangling to locally declared function
1186 mangleUnqualifiedName(ND);
1187 return;
1188 }
1189
Anders Carlsson1b42c792009-04-02 16:24:45 +00001190 Out << 'Z';
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001191
Charles Davis685b1d92010-05-26 18:25:27 +00001192 if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(DC)) {
1193 mangleObjCMethodName(MD);
John McCall82b7d7b2010-10-18 21:28:44 +00001194 } else if (const CXXRecordDecl *RD = GetLocalClassDecl(ND)) {
1195 mangleFunctionEncoding(cast<FunctionDecl>(RD->getDeclContext()));
Fariborz Jahanian57058532010-03-03 19:41:08 +00001196 Out << 'E';
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001197
John McCall82b7d7b2010-10-18 21:28:44 +00001198 // Mangle the name relative to the closest enclosing function.
1199 if (ND == RD) // equality ok because RD derived from ND above
1200 mangleUnqualifiedName(ND);
1201 else
1202 mangleNestedName(ND, DC, true /*NoFunction*/);
1203
Fariborz Jahanian4819ac42010-03-04 01:02:03 +00001204 unsigned disc;
John McCall82b7d7b2010-10-18 21:28:44 +00001205 if (Context.getNextDiscriminator(RD, disc)) {
Fariborz Jahanian4819ac42010-03-04 01:02:03 +00001206 if (disc < 10)
1207 Out << '_' << disc;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001208 else
Fariborz Jahanian4819ac42010-03-04 01:02:03 +00001209 Out << "__" << disc << '_';
1210 }
Fariborz Jahanian57058532010-03-03 19:41:08 +00001211
1212 return;
1213 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001214 else
Fariborz Jahanian57058532010-03-03 19:41:08 +00001215 mangleFunctionEncoding(cast<FunctionDecl>(DC));
Anders Carlsson7b06f6c2009-12-10 03:14:39 +00001216
Anders Carlsson1b42c792009-04-02 16:24:45 +00001217 Out << 'E';
Eli Friedman6f9f25d2009-12-11 20:21:38 +00001218 mangleUnqualifiedName(ND);
Anders Carlsson1b42c792009-04-02 16:24:45 +00001219}
1220
John McCalla0ce15c2011-04-24 08:23:24 +00001221void CXXNameMangler::manglePrefix(NestedNameSpecifier *qualifier) {
1222 switch (qualifier->getKind()) {
1223 case NestedNameSpecifier::Global:
1224 // nothing
1225 return;
1226
1227 case NestedNameSpecifier::Namespace:
1228 mangleName(qualifier->getAsNamespace());
1229 return;
1230
1231 case NestedNameSpecifier::NamespaceAlias:
1232 mangleName(qualifier->getAsNamespaceAlias()->getNamespace());
1233 return;
1234
1235 case NestedNameSpecifier::TypeSpec:
1236 case NestedNameSpecifier::TypeSpecWithTemplate:
John McCall4f4e4132011-05-04 01:45:19 +00001237 manglePrefix(QualType(qualifier->getAsType(), 0));
John McCalla0ce15c2011-04-24 08:23:24 +00001238 return;
1239
1240 case NestedNameSpecifier::Identifier:
1241 // Member expressions can have these without prefixes, but that
1242 // should end up in mangleUnresolvedPrefix instead.
1243 assert(qualifier->getPrefix());
1244 manglePrefix(qualifier->getPrefix());
1245
1246 mangleSourceName(qualifier->getAsIdentifier());
1247 return;
1248 }
1249
1250 llvm_unreachable("unexpected nested name specifier");
1251}
1252
Fariborz Jahanian57058532010-03-03 19:41:08 +00001253void CXXNameMangler::manglePrefix(const DeclContext *DC, bool NoFunction) {
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001254 // <prefix> ::= <prefix> <unqualified-name>
1255 // ::= <template-prefix> <template-args>
1256 // ::= <template-param>
1257 // ::= # empty
1258 // ::= <substitution>
Anders Carlsson6862fc72009-09-17 04:16:28 +00001259
Anders Carlssonadd28822009-09-22 20:33:31 +00001260 while (isa<LinkageSpecDecl>(DC))
1261 DC = DC->getParent();
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00001262
Anders Carlsson9263e912009-09-18 18:39:58 +00001263 if (DC->isTranslationUnit())
1264 return;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00001265
Douglas Gregor35415f52010-05-25 17:04:15 +00001266 if (const BlockDecl *Block = dyn_cast<BlockDecl>(DC)) {
1267 manglePrefix(DC->getParent(), NoFunction);
1268 llvm::SmallString<64> Name;
Rafael Espindolac4850c22011-02-10 23:59:36 +00001269 llvm::raw_svector_ostream NameStream(Name);
1270 Context.mangleBlock(Block, NameStream);
1271 NameStream.flush();
Douglas Gregor35415f52010-05-25 17:04:15 +00001272 Out << Name.size() << Name;
1273 return;
1274 }
1275
Anders Carlsson6862fc72009-09-17 04:16:28 +00001276 if (mangleSubstitution(cast<NamedDecl>(DC)))
1277 return;
Anders Carlsson7482e242009-09-18 04:29:09 +00001278
Anders Carlsson2ee3fca2009-09-18 20:11:09 +00001279 // Check if we have a template.
1280 const TemplateArgumentList *TemplateArgs = 0;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00001281 if (const TemplateDecl *TD = isTemplate(cast<NamedDecl>(DC), TemplateArgs)) {
Anders Carlsson2ee3fca2009-09-18 20:11:09 +00001282 mangleTemplatePrefix(TD);
Rafael Espindolad9800722010-03-11 14:07:00 +00001283 TemplateParameterList *TemplateParameters = TD->getTemplateParameters();
1284 mangleTemplateArgs(*TemplateParameters, *TemplateArgs);
Fariborz Jahanian57058532010-03-03 19:41:08 +00001285 }
Douglas Gregor35415f52010-05-25 17:04:15 +00001286 else if(NoFunction && (isa<FunctionDecl>(DC) || isa<ObjCMethodDecl>(DC)))
Fariborz Jahanian57058532010-03-03 19:41:08 +00001287 return;
Douglas Gregor35415f52010-05-25 17:04:15 +00001288 else if (const ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(DC))
1289 mangleObjCMethodName(Method);
Fariborz Jahanian57058532010-03-03 19:41:08 +00001290 else {
1291 manglePrefix(DC->getParent(), NoFunction);
Anders Carlsson2ee3fca2009-09-18 20:11:09 +00001292 mangleUnqualifiedName(cast<NamedDecl>(DC));
1293 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00001294
Anders Carlsson6862fc72009-09-17 04:16:28 +00001295 addSubstitution(cast<NamedDecl>(DC));
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001296}
1297
Douglas Gregor20f0cc72010-04-23 03:10:43 +00001298void CXXNameMangler::mangleTemplatePrefix(TemplateName Template) {
1299 // <template-prefix> ::= <prefix> <template unqualified-name>
1300 // ::= <template-param>
1301 // ::= <substitution>
1302 if (TemplateDecl *TD = Template.getAsTemplateDecl())
1303 return mangleTemplatePrefix(TD);
1304
1305 if (QualifiedTemplateName *Qualified = Template.getAsQualifiedTemplateName())
John McCalla0ce15c2011-04-24 08:23:24 +00001306 manglePrefix(Qualified->getQualifier());
Sean Huntc3021132010-05-05 15:23:54 +00001307
Douglas Gregor20f0cc72010-04-23 03:10:43 +00001308 if (OverloadedTemplateStorage *Overloaded
1309 = Template.getAsOverloadedTemplate()) {
Sean Huntc3021132010-05-05 15:23:54 +00001310 mangleUnqualifiedName(0, (*Overloaded->begin())->getDeclName(),
Douglas Gregor20f0cc72010-04-23 03:10:43 +00001311 UnknownArity);
1312 return;
1313 }
Sean Huntc3021132010-05-05 15:23:54 +00001314
Douglas Gregor20f0cc72010-04-23 03:10:43 +00001315 DependentTemplateName *Dependent = Template.getAsDependentTemplateName();
1316 assert(Dependent && "Unknown template name kind?");
John McCalla0ce15c2011-04-24 08:23:24 +00001317 manglePrefix(Dependent->getQualifier());
Douglas Gregor1e9268e2010-04-28 05:58:56 +00001318 mangleUnscopedTemplateName(Template);
Douglas Gregor20f0cc72010-04-23 03:10:43 +00001319}
1320
Anders Carlsson0fa6df42009-09-26 19:45:45 +00001321void CXXNameMangler::mangleTemplatePrefix(const TemplateDecl *ND) {
Anders Carlsson7482e242009-09-18 04:29:09 +00001322 // <template-prefix> ::= <prefix> <template unqualified-name>
1323 // ::= <template-param>
1324 // ::= <substitution>
Douglas Gregor32fb4e12010-02-05 20:45:00 +00001325 // <template-template-param> ::= <template-param>
1326 // <substitution>
Anders Carlsson7482e242009-09-18 04:29:09 +00001327
Anders Carlssonaeb85372009-09-26 22:18:22 +00001328 if (mangleSubstitution(ND))
1329 return;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00001330
Douglas Gregor32fb4e12010-02-05 20:45:00 +00001331 // <template-template-param> ::= <template-param>
1332 if (const TemplateTemplateParmDecl *TTP
1333 = dyn_cast<TemplateTemplateParmDecl>(ND)) {
1334 mangleTemplateParameter(TTP->getIndex());
1335 return;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001336 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00001337
Anders Carlssonaa73ab12009-09-18 18:47:07 +00001338 manglePrefix(ND->getDeclContext());
Anders Carlsson1668f202009-09-26 20:13:56 +00001339 mangleUnqualifiedName(ND->getTemplatedDecl());
Anders Carlssonaeb85372009-09-26 22:18:22 +00001340 addSubstitution(ND);
Anders Carlsson7482e242009-09-18 04:29:09 +00001341}
1342
John McCallb6f532e2010-07-14 06:43:17 +00001343/// Mangles a template name under the production <type>. Required for
1344/// template template arguments.
1345/// <type> ::= <class-enum-type>
1346/// ::= <template-param>
1347/// ::= <substitution>
1348void CXXNameMangler::mangleType(TemplateName TN) {
1349 if (mangleSubstitution(TN))
1350 return;
1351
1352 TemplateDecl *TD = 0;
1353
1354 switch (TN.getKind()) {
1355 case TemplateName::QualifiedTemplate:
1356 TD = TN.getAsQualifiedTemplateName()->getTemplateDecl();
1357 goto HaveDecl;
1358
1359 case TemplateName::Template:
1360 TD = TN.getAsTemplateDecl();
1361 goto HaveDecl;
1362
1363 HaveDecl:
1364 if (isa<TemplateTemplateParmDecl>(TD))
1365 mangleTemplateParameter(cast<TemplateTemplateParmDecl>(TD)->getIndex());
1366 else
1367 mangleName(TD);
1368 break;
1369
1370 case TemplateName::OverloadedTemplate:
1371 llvm_unreachable("can't mangle an overloaded template name as a <type>");
1372 break;
1373
1374 case TemplateName::DependentTemplate: {
1375 const DependentTemplateName *Dependent = TN.getAsDependentTemplateName();
1376 assert(Dependent->isIdentifier());
1377
1378 // <class-enum-type> ::= <name>
1379 // <name> ::= <nested-name>
John McCalla0ce15c2011-04-24 08:23:24 +00001380 mangleUnresolvedPrefix(Dependent->getQualifier(), 0);
John McCallb6f532e2010-07-14 06:43:17 +00001381 mangleSourceName(Dependent->getIdentifier());
1382 break;
1383 }
1384
John McCallb44e0cf2011-06-30 21:59:02 +00001385 case TemplateName::SubstTemplateTemplateParm: {
1386 // Substituted template parameters are mangled as the substituted
1387 // template. This will check for the substitution twice, which is
1388 // fine, but we have to return early so that we don't try to *add*
1389 // the substitution twice.
1390 SubstTemplateTemplateParmStorage *subst
1391 = TN.getAsSubstTemplateTemplateParm();
1392 mangleType(subst->getReplacement());
1393 return;
1394 }
John McCall14606042011-06-30 08:33:18 +00001395
Douglas Gregor1aee05d2011-01-15 06:45:20 +00001396 case TemplateName::SubstTemplateTemplateParmPack: {
John McCall68a51a72011-07-01 00:04:39 +00001397 // FIXME: not clear how to mangle this!
1398 // template <template <class> class T...> class A {
1399 // template <template <class> class U...> void foo(B<T,U> x...);
1400 // };
1401 Out << "_SUBSTPACK_";
Douglas Gregor1aee05d2011-01-15 06:45:20 +00001402 break;
1403 }
John McCallb6f532e2010-07-14 06:43:17 +00001404 }
1405
1406 addSubstitution(TN);
1407}
1408
Mike Stump1eb44332009-09-09 15:08:12 +00001409void
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001410CXXNameMangler::mangleOperatorName(OverloadedOperatorKind OO, unsigned Arity) {
1411 switch (OO) {
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001412 // <operator-name> ::= nw # new
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001413 case OO_New: Out << "nw"; break;
1414 // ::= na # new[]
1415 case OO_Array_New: Out << "na"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001416 // ::= dl # delete
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001417 case OO_Delete: Out << "dl"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001418 // ::= da # delete[]
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001419 case OO_Array_Delete: Out << "da"; break;
1420 // ::= ps # + (unary)
John McCall5e1e89b2010-08-18 19:18:59 +00001421 // ::= pl # + (binary or unknown)
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001422 case OO_Plus:
Anders Carlsson8257d412009-12-22 06:36:32 +00001423 Out << (Arity == 1? "ps" : "pl"); break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001424 // ::= ng # - (unary)
John McCall5e1e89b2010-08-18 19:18:59 +00001425 // ::= mi # - (binary or unknown)
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001426 case OO_Minus:
Anders Carlsson8257d412009-12-22 06:36:32 +00001427 Out << (Arity == 1? "ng" : "mi"); break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001428 // ::= ad # & (unary)
John McCall5e1e89b2010-08-18 19:18:59 +00001429 // ::= an # & (binary or unknown)
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001430 case OO_Amp:
Anders Carlsson8257d412009-12-22 06:36:32 +00001431 Out << (Arity == 1? "ad" : "an"); break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001432 // ::= de # * (unary)
John McCall5e1e89b2010-08-18 19:18:59 +00001433 // ::= ml # * (binary or unknown)
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001434 case OO_Star:
John McCall5e1e89b2010-08-18 19:18:59 +00001435 // Use binary when unknown.
Anders Carlsson8257d412009-12-22 06:36:32 +00001436 Out << (Arity == 1? "de" : "ml"); break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001437 // ::= co # ~
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001438 case OO_Tilde: Out << "co"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001439 // ::= dv # /
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001440 case OO_Slash: Out << "dv"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001441 // ::= rm # %
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001442 case OO_Percent: Out << "rm"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001443 // ::= or # |
1444 case OO_Pipe: Out << "or"; break;
1445 // ::= eo # ^
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001446 case OO_Caret: Out << "eo"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001447 // ::= aS # =
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001448 case OO_Equal: Out << "aS"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001449 // ::= pL # +=
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001450 case OO_PlusEqual: Out << "pL"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001451 // ::= mI # -=
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001452 case OO_MinusEqual: Out << "mI"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001453 // ::= mL # *=
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001454 case OO_StarEqual: Out << "mL"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001455 // ::= dV # /=
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001456 case OO_SlashEqual: Out << "dV"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001457 // ::= rM # %=
1458 case OO_PercentEqual: Out << "rM"; break;
1459 // ::= aN # &=
1460 case OO_AmpEqual: Out << "aN"; break;
1461 // ::= oR # |=
1462 case OO_PipeEqual: Out << "oR"; break;
1463 // ::= eO # ^=
1464 case OO_CaretEqual: Out << "eO"; break;
1465 // ::= ls # <<
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001466 case OO_LessLess: Out << "ls"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001467 // ::= rs # >>
1468 case OO_GreaterGreater: Out << "rs"; break;
1469 // ::= lS # <<=
1470 case OO_LessLessEqual: Out << "lS"; break;
1471 // ::= rS # >>=
1472 case OO_GreaterGreaterEqual: Out << "rS"; break;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001473 // ::= eq # ==
1474 case OO_EqualEqual: Out << "eq"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001475 // ::= ne # !=
1476 case OO_ExclaimEqual: Out << "ne"; break;
1477 // ::= lt # <
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001478 case OO_Less: Out << "lt"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001479 // ::= gt # >
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001480 case OO_Greater: Out << "gt"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001481 // ::= le # <=
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001482 case OO_LessEqual: Out << "le"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001483 // ::= ge # >=
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001484 case OO_GreaterEqual: Out << "ge"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001485 // ::= nt # !
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001486 case OO_Exclaim: Out << "nt"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001487 // ::= aa # &&
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001488 case OO_AmpAmp: Out << "aa"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001489 // ::= oo # ||
1490 case OO_PipePipe: Out << "oo"; break;
1491 // ::= pp # ++
1492 case OO_PlusPlus: Out << "pp"; break;
1493 // ::= mm # --
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001494 case OO_MinusMinus: Out << "mm"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001495 // ::= cm # ,
1496 case OO_Comma: Out << "cm"; break;
1497 // ::= pm # ->*
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001498 case OO_ArrowStar: Out << "pm"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001499 // ::= pt # ->
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001500 case OO_Arrow: Out << "pt"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001501 // ::= cl # ()
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001502 case OO_Call: Out << "cl"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001503 // ::= ix # []
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001504 case OO_Subscript: Out << "ix"; break;
Anders Carlssone170ba72009-12-14 01:45:37 +00001505
1506 // ::= qu # ?
1507 // The conditional operator can't be overloaded, but we still handle it when
1508 // mangling expressions.
1509 case OO_Conditional: Out << "qu"; break;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001510
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001511 case OO_None:
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001512 case NUM_OVERLOADED_OPERATORS:
David Blaikieb219cfc2011-09-23 05:06:16 +00001513 llvm_unreachable("Not an overloaded operator");
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001514 }
1515}
1516
John McCall0953e762009-09-24 19:53:00 +00001517void CXXNameMangler::mangleQualifiers(Qualifiers Quals) {
Mike Stump1eb44332009-09-09 15:08:12 +00001518 // <CV-qualifiers> ::= [r] [V] [K] # restrict (C99), volatile, const
John McCall0953e762009-09-24 19:53:00 +00001519 if (Quals.hasRestrict())
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001520 Out << 'r';
John McCall0953e762009-09-24 19:53:00 +00001521 if (Quals.hasVolatile())
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001522 Out << 'V';
John McCall0953e762009-09-24 19:53:00 +00001523 if (Quals.hasConst())
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001524 Out << 'K';
John McCall0953e762009-09-24 19:53:00 +00001525
Douglas Gregor56079f72010-06-14 23:15:08 +00001526 if (Quals.hasAddressSpace()) {
1527 // Extension:
1528 //
1529 // <type> ::= U <address-space-number>
1530 //
1531 // where <address-space-number> is a source name consisting of 'AS'
1532 // followed by the address space <number>.
1533 llvm::SmallString<64> ASString;
1534 ASString = "AS" + llvm::utostr_32(Quals.getAddressSpace());
1535 Out << 'U' << ASString.size() << ASString;
1536 }
1537
Chris Lattner5f9e2722011-07-23 10:55:15 +00001538 StringRef LifetimeName;
John McCallf85e1932011-06-15 23:02:42 +00001539 switch (Quals.getObjCLifetime()) {
1540 // Objective-C ARC Extension:
1541 //
1542 // <type> ::= U "__strong"
1543 // <type> ::= U "__weak"
1544 // <type> ::= U "__autoreleasing"
John McCallf85e1932011-06-15 23:02:42 +00001545 case Qualifiers::OCL_None:
1546 break;
1547
1548 case Qualifiers::OCL_Weak:
1549 LifetimeName = "__weak";
1550 break;
1551
1552 case Qualifiers::OCL_Strong:
1553 LifetimeName = "__strong";
1554 break;
1555
1556 case Qualifiers::OCL_Autoreleasing:
1557 LifetimeName = "__autoreleasing";
1558 break;
1559
1560 case Qualifiers::OCL_ExplicitNone:
Douglas Gregorc22d6992011-06-17 22:26:49 +00001561 // The __unsafe_unretained qualifier is *not* mangled, so that
1562 // __unsafe_unretained types in ARC produce the same manglings as the
1563 // equivalent (but, naturally, unqualified) types in non-ARC, providing
1564 // better ABI compatibility.
1565 //
1566 // It's safe to do this because unqualified 'id' won't show up
1567 // in any type signatures that need to be mangled.
John McCallf85e1932011-06-15 23:02:42 +00001568 break;
1569 }
1570 if (!LifetimeName.empty())
1571 Out << 'U' << LifetimeName.size() << LifetimeName;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001572}
1573
Douglas Gregor0a9a6d62011-01-26 17:36:28 +00001574void CXXNameMangler::mangleRefQualifier(RefQualifierKind RefQualifier) {
1575 // <ref-qualifier> ::= R # lvalue reference
1576 // ::= O # rvalue-reference
1577 // Proposal to Itanium C++ ABI list on 1/26/11
1578 switch (RefQualifier) {
1579 case RQ_None:
1580 break;
1581
1582 case RQ_LValue:
1583 Out << 'R';
1584 break;
1585
1586 case RQ_RValue:
1587 Out << 'O';
1588 break;
1589 }
1590}
1591
Anders Carlsson7b06f6c2009-12-10 03:14:39 +00001592void CXXNameMangler::mangleObjCMethodName(const ObjCMethodDecl *MD) {
Rafael Espindolaf0be9792011-02-11 02:52:17 +00001593 Context.mangleObjCMethodName(MD, Out);
Anders Carlsson7b06f6c2009-12-10 03:14:39 +00001594}
1595
Douglas Gregorf1588662011-07-12 15:18:55 +00001596void CXXNameMangler::mangleType(QualType T) {
1597 // If our type is instantiation-dependent but not dependent, we mangle
1598 // it as it was written in the source, removing any top-level sugar.
1599 // Otherwise, use the canonical type.
1600 //
1601 // FIXME: This is an approximation of the instantiation-dependent name
1602 // mangling rules, since we should really be using the type as written and
1603 // augmented via semantic analysis (i.e., with implicit conversions and
1604 // default template arguments) for any instantiation-dependent type.
1605 // Unfortunately, that requires several changes to our AST:
1606 // - Instantiation-dependent TemplateSpecializationTypes will need to be
1607 // uniqued, so that we can handle substitutions properly
1608 // - Default template arguments will need to be represented in the
1609 // TemplateSpecializationType, since they need to be mangled even though
1610 // they aren't written.
1611 // - Conversions on non-type template arguments need to be expressed, since
1612 // they can affect the mangling of sizeof/alignof.
1613 if (!T->isInstantiationDependentType() || T->isDependentType())
1614 T = T.getCanonicalType();
1615 else {
1616 // Desugar any types that are purely sugar.
1617 do {
1618 // Don't desugar through template specialization types that aren't
1619 // type aliases. We need to mangle the template arguments as written.
1620 if (const TemplateSpecializationType *TST
1621 = dyn_cast<TemplateSpecializationType>(T))
1622 if (!TST->isTypeAlias())
1623 break;
Anders Carlsson4843e582009-03-10 17:07:44 +00001624
Douglas Gregorf1588662011-07-12 15:18:55 +00001625 QualType Desugared
1626 = T.getSingleStepDesugaredType(Context.getASTContext());
1627 if (Desugared == T)
1628 break;
1629
1630 T = Desugared;
1631 } while (true);
1632 }
1633 SplitQualType split = T.split();
John McCallb47f7482011-01-26 20:05:40 +00001634 Qualifiers quals = split.second;
1635 const Type *ty = split.first;
1636
Douglas Gregorf1588662011-07-12 15:18:55 +00001637 bool isSubstitutable = quals || !isa<BuiltinType>(T);
1638 if (isSubstitutable && mangleSubstitution(T))
Anders Carlsson76967372009-09-17 00:43:46 +00001639 return;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001640
John McCallb47f7482011-01-26 20:05:40 +00001641 // If we're mangling a qualified array type, push the qualifiers to
1642 // the element type.
Douglas Gregorf1588662011-07-12 15:18:55 +00001643 if (quals && isa<ArrayType>(T)) {
1644 ty = Context.getASTContext().getAsArrayType(T);
John McCallb47f7482011-01-26 20:05:40 +00001645 quals = Qualifiers();
1646
Douglas Gregorf1588662011-07-12 15:18:55 +00001647 // Note that we don't update T: we want to add the
1648 // substitution at the original type.
John McCallb47f7482011-01-26 20:05:40 +00001649 }
1650
1651 if (quals) {
1652 mangleQualifiers(quals);
John McCall0953e762009-09-24 19:53:00 +00001653 // Recurse: even if the qualified type isn't yet substitutable,
1654 // the unqualified type might be.
John McCallb47f7482011-01-26 20:05:40 +00001655 mangleType(QualType(ty, 0));
Anders Carlsson76967372009-09-17 00:43:46 +00001656 } else {
John McCallb47f7482011-01-26 20:05:40 +00001657 switch (ty->getTypeClass()) {
John McCallefe6aee2009-09-05 07:56:18 +00001658#define ABSTRACT_TYPE(CLASS, PARENT)
1659#define NON_CANONICAL_TYPE(CLASS, PARENT) \
Anders Carlsson76967372009-09-17 00:43:46 +00001660 case Type::CLASS: \
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +00001661 llvm_unreachable("can't mangle non-canonical type " #CLASS "Type"); \
Anders Carlsson76967372009-09-17 00:43:46 +00001662 return;
John McCallefe6aee2009-09-05 07:56:18 +00001663#define TYPE(CLASS, PARENT) \
Anders Carlsson76967372009-09-17 00:43:46 +00001664 case Type::CLASS: \
John McCallb47f7482011-01-26 20:05:40 +00001665 mangleType(static_cast<const CLASS##Type*>(ty)); \
Anders Carlsson76967372009-09-17 00:43:46 +00001666 break;
John McCallefe6aee2009-09-05 07:56:18 +00001667#include "clang/AST/TypeNodes.def"
Anders Carlsson76967372009-09-17 00:43:46 +00001668 }
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001669 }
Anders Carlsson76967372009-09-17 00:43:46 +00001670
1671 // Add the substitution.
John McCallb47f7482011-01-26 20:05:40 +00001672 if (isSubstitutable)
Douglas Gregorf1588662011-07-12 15:18:55 +00001673 addSubstitution(T);
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001674}
1675
Douglas Gregor1b12a3b2010-05-26 05:11:13 +00001676void CXXNameMangler::mangleNameOrStandardSubstitution(const NamedDecl *ND) {
1677 if (!mangleStandardSubstitution(ND))
1678 mangleName(ND);
1679}
1680
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001681void CXXNameMangler::mangleType(const BuiltinType *T) {
John McCallefe6aee2009-09-05 07:56:18 +00001682 // <type> ::= <builtin-type>
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001683 // <builtin-type> ::= v # void
1684 // ::= w # wchar_t
1685 // ::= b # bool
1686 // ::= c # char
1687 // ::= a # signed char
1688 // ::= h # unsigned char
1689 // ::= s # short
1690 // ::= t # unsigned short
1691 // ::= i # int
1692 // ::= j # unsigned int
1693 // ::= l # long
1694 // ::= m # unsigned long
1695 // ::= x # long long, __int64
1696 // ::= y # unsigned long long, __int64
1697 // ::= n # __int128
1698 // UNSUPPORTED: ::= o # unsigned __int128
1699 // ::= f # float
1700 // ::= d # double
1701 // ::= e # long double, __float80
1702 // UNSUPPORTED: ::= g # __float128
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001703 // UNSUPPORTED: ::= Dd # IEEE 754r decimal floating point (64 bits)
1704 // UNSUPPORTED: ::= De # IEEE 754r decimal floating point (128 bits)
1705 // UNSUPPORTED: ::= Df # IEEE 754r decimal floating point (32 bits)
1706 // UNSUPPORTED: ::= Dh # IEEE 754r half-precision floating point (16 bits)
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001707 // ::= Di # char32_t
1708 // ::= Ds # char16_t
Anders Carlssone2923682010-11-04 04:31:32 +00001709 // ::= Dn # std::nullptr_t (i.e., decltype(nullptr))
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001710 // ::= u <source-name> # vendor extended type
1711 switch (T->getKind()) {
1712 case BuiltinType::Void: Out << 'v'; break;
1713 case BuiltinType::Bool: Out << 'b'; break;
1714 case BuiltinType::Char_U: case BuiltinType::Char_S: Out << 'c'; break;
1715 case BuiltinType::UChar: Out << 'h'; break;
1716 case BuiltinType::UShort: Out << 't'; break;
1717 case BuiltinType::UInt: Out << 'j'; break;
1718 case BuiltinType::ULong: Out << 'm'; break;
1719 case BuiltinType::ULongLong: Out << 'y'; break;
Chris Lattner2df9ced2009-04-30 02:43:43 +00001720 case BuiltinType::UInt128: Out << 'o'; break;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001721 case BuiltinType::SChar: Out << 'a'; break;
Chris Lattner3f59c972010-12-25 23:25:43 +00001722 case BuiltinType::WChar_S:
1723 case BuiltinType::WChar_U: Out << 'w'; break;
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001724 case BuiltinType::Char16: Out << "Ds"; break;
1725 case BuiltinType::Char32: Out << "Di"; break;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001726 case BuiltinType::Short: Out << 's'; break;
1727 case BuiltinType::Int: Out << 'i'; break;
1728 case BuiltinType::Long: Out << 'l'; break;
1729 case BuiltinType::LongLong: Out << 'x'; break;
Chris Lattner2df9ced2009-04-30 02:43:43 +00001730 case BuiltinType::Int128: Out << 'n'; break;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001731 case BuiltinType::Float: Out << 'f'; break;
1732 case BuiltinType::Double: Out << 'd'; break;
1733 case BuiltinType::LongDouble: Out << 'e'; break;
Anders Carlssone2923682010-11-04 04:31:32 +00001734 case BuiltinType::NullPtr: Out << "Dn"; break;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001735
1736 case BuiltinType::Overload:
1737 case BuiltinType::Dependent:
John McCall864c0412011-04-26 20:42:42 +00001738 case BuiltinType::BoundMember:
John McCall1de4d4e2011-04-07 08:22:57 +00001739 case BuiltinType::UnknownAny:
John McCallfb44de92011-05-01 22:35:37 +00001740 llvm_unreachable("mangling a placeholder type");
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001741 break;
Steve Naroff9533a7f2009-07-22 17:14:51 +00001742 case BuiltinType::ObjCId: Out << "11objc_object"; break;
1743 case BuiltinType::ObjCClass: Out << "10objc_class"; break;
Fariborz Jahanian13dcd002009-11-21 19:53:08 +00001744 case BuiltinType::ObjCSel: Out << "13objc_selector"; break;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001745 }
1746}
1747
John McCallefe6aee2009-09-05 07:56:18 +00001748// <type> ::= <function-type>
1749// <function-type> ::= F [Y] <bare-function-type> E
1750void CXXNameMangler::mangleType(const FunctionProtoType *T) {
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001751 Out << 'F';
Mike Stumpf5408fe2009-05-16 07:57:57 +00001752 // FIXME: We don't have enough information in the AST to produce the 'Y'
1753 // encoding for extern "C" function types.
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001754 mangleBareFunctionType(T, /*MangleReturnType=*/true);
1755 Out << 'E';
1756}
John McCallefe6aee2009-09-05 07:56:18 +00001757void CXXNameMangler::mangleType(const FunctionNoProtoType *T) {
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +00001758 llvm_unreachable("Can't mangle K&R function prototypes");
John McCallefe6aee2009-09-05 07:56:18 +00001759}
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001760void CXXNameMangler::mangleBareFunctionType(const FunctionType *T,
1761 bool MangleReturnType) {
John McCallefe6aee2009-09-05 07:56:18 +00001762 // We should never be mangling something without a prototype.
1763 const FunctionProtoType *Proto = cast<FunctionProtoType>(T);
1764
John McCallfb44de92011-05-01 22:35:37 +00001765 // Record that we're in a function type. See mangleFunctionParam
1766 // for details on what we're trying to achieve here.
1767 FunctionTypeDepthState saved = FunctionTypeDepth.push();
1768
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001769 // <bare-function-type> ::= <signature type>+
John McCallfb44de92011-05-01 22:35:37 +00001770 if (MangleReturnType) {
1771 FunctionTypeDepth.enterResultType();
John McCallefe6aee2009-09-05 07:56:18 +00001772 mangleType(Proto->getResultType());
John McCallfb44de92011-05-01 22:35:37 +00001773 FunctionTypeDepth.leaveResultType();
1774 }
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001775
Anders Carlsson93296682010-06-02 04:40:13 +00001776 if (Proto->getNumArgs() == 0 && !Proto->isVariadic()) {
Eli Friedmana7e68452010-08-22 01:00:03 +00001777 // <builtin-type> ::= v # void
Anders Carlssonc6c91bc2009-04-01 00:15:23 +00001778 Out << 'v';
John McCallfb44de92011-05-01 22:35:37 +00001779
1780 FunctionTypeDepth.pop(saved);
Anders Carlssonc6c91bc2009-04-01 00:15:23 +00001781 return;
1782 }
Mike Stump1eb44332009-09-09 15:08:12 +00001783
Douglas Gregor72564e72009-02-26 23:50:07 +00001784 for (FunctionProtoType::arg_type_iterator Arg = Proto->arg_type_begin(),
Mike Stump1eb44332009-09-09 15:08:12 +00001785 ArgEnd = Proto->arg_type_end();
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001786 Arg != ArgEnd; ++Arg)
Douglas Gregor79e6bd32011-07-12 04:42:08 +00001787 mangleType(Context.getASTContext().getSignatureParameterType(*Arg));
Douglas Gregor219cc612009-02-13 01:28:03 +00001788
John McCallfb44de92011-05-01 22:35:37 +00001789 FunctionTypeDepth.pop(saved);
1790
Douglas Gregor219cc612009-02-13 01:28:03 +00001791 // <builtin-type> ::= z # ellipsis
1792 if (Proto->isVariadic())
1793 Out << 'z';
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001794}
1795
John McCallefe6aee2009-09-05 07:56:18 +00001796// <type> ::= <class-enum-type>
Mike Stump1eb44332009-09-09 15:08:12 +00001797// <class-enum-type> ::= <name>
John McCalled976492009-12-04 22:46:56 +00001798void CXXNameMangler::mangleType(const UnresolvedUsingType *T) {
1799 mangleName(T->getDecl());
1800}
1801
1802// <type> ::= <class-enum-type>
1803// <class-enum-type> ::= <name>
John McCallefe6aee2009-09-05 07:56:18 +00001804void CXXNameMangler::mangleType(const EnumType *T) {
1805 mangleType(static_cast<const TagType*>(T));
1806}
1807void CXXNameMangler::mangleType(const RecordType *T) {
1808 mangleType(static_cast<const TagType*>(T));
1809}
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001810void CXXNameMangler::mangleType(const TagType *T) {
Eli Friedmanecb7e932009-12-11 18:00:57 +00001811 mangleName(T->getDecl());
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001812}
1813
John McCallefe6aee2009-09-05 07:56:18 +00001814// <type> ::= <array-type>
1815// <array-type> ::= A <positive dimension number> _ <element type>
1816// ::= A [<dimension expression>] _ <element type>
1817void CXXNameMangler::mangleType(const ConstantArrayType *T) {
1818 Out << 'A' << T->getSize() << '_';
1819 mangleType(T->getElementType());
1820}
1821void CXXNameMangler::mangleType(const VariableArrayType *T) {
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001822 Out << 'A';
Fariborz Jahanian7281d1f2010-11-02 16:54:00 +00001823 // decayed vla types (size 0) will just be skipped.
1824 if (T->getSizeExpr())
1825 mangleExpression(T->getSizeExpr());
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001826 Out << '_';
1827 mangleType(T->getElementType());
1828}
John McCallefe6aee2009-09-05 07:56:18 +00001829void CXXNameMangler::mangleType(const DependentSizedArrayType *T) {
1830 Out << 'A';
1831 mangleExpression(T->getSizeExpr());
1832 Out << '_';
1833 mangleType(T->getElementType());
1834}
1835void CXXNameMangler::mangleType(const IncompleteArrayType *T) {
Nick Lewycky271b6652010-09-05 03:40:33 +00001836 Out << "A_";
John McCallefe6aee2009-09-05 07:56:18 +00001837 mangleType(T->getElementType());
1838}
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001839
John McCallefe6aee2009-09-05 07:56:18 +00001840// <type> ::= <pointer-to-member-type>
1841// <pointer-to-member-type> ::= M <class type> <member type>
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001842void CXXNameMangler::mangleType(const MemberPointerType *T) {
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001843 Out << 'M';
1844 mangleType(QualType(T->getClass(), 0));
Anders Carlsson0e650012009-05-17 17:41:20 +00001845 QualType PointeeType = T->getPointeeType();
1846 if (const FunctionProtoType *FPT = dyn_cast<FunctionProtoType>(PointeeType)) {
John McCall0953e762009-09-24 19:53:00 +00001847 mangleQualifiers(Qualifiers::fromCVRMask(FPT->getTypeQuals()));
Douglas Gregor0a9a6d62011-01-26 17:36:28 +00001848 mangleRefQualifier(FPT->getRefQualifier());
Anders Carlsson0e650012009-05-17 17:41:20 +00001849 mangleType(FPT);
Anders Carlsson9d85b722010-06-02 04:29:50 +00001850
1851 // Itanium C++ ABI 5.1.8:
1852 //
1853 // The type of a non-static member function is considered to be different,
1854 // for the purposes of substitution, from the type of a namespace-scope or
1855 // static member function whose type appears similar. The types of two
1856 // non-static member functions are considered to be different, for the
1857 // purposes of substitution, if the functions are members of different
1858 // classes. In other words, for the purposes of substitution, the class of
1859 // which the function is a member is considered part of the type of
1860 // function.
1861
1862 // We increment the SeqID here to emulate adding an entry to the
1863 // substitution table. We can't actually add it because we don't want this
1864 // particular function type to be substituted.
1865 ++SeqID;
Mike Stump1eb44332009-09-09 15:08:12 +00001866 } else
Anders Carlsson0e650012009-05-17 17:41:20 +00001867 mangleType(PointeeType);
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001868}
1869
John McCallefe6aee2009-09-05 07:56:18 +00001870// <type> ::= <template-param>
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001871void CXXNameMangler::mangleType(const TemplateTypeParmType *T) {
Anders Carlsson0ccdf8d2009-09-27 00:38:53 +00001872 mangleTemplateParameter(T->getIndex());
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001873}
1874
Douglas Gregorc3069d62011-01-14 02:55:32 +00001875// <type> ::= <template-param>
1876void CXXNameMangler::mangleType(const SubstTemplateTypeParmPackType *T) {
John McCall68a51a72011-07-01 00:04:39 +00001877 // FIXME: not clear how to mangle this!
1878 // template <class T...> class A {
1879 // template <class U...> void foo(T(*)(U) x...);
1880 // };
1881 Out << "_SUBSTPACK_";
Douglas Gregorc3069d62011-01-14 02:55:32 +00001882}
1883
John McCallefe6aee2009-09-05 07:56:18 +00001884// <type> ::= P <type> # pointer-to
1885void CXXNameMangler::mangleType(const PointerType *T) {
1886 Out << 'P';
1887 mangleType(T->getPointeeType());
1888}
1889void CXXNameMangler::mangleType(const ObjCObjectPointerType *T) {
1890 Out << 'P';
1891 mangleType(T->getPointeeType());
1892}
1893
1894// <type> ::= R <type> # reference-to
1895void CXXNameMangler::mangleType(const LValueReferenceType *T) {
1896 Out << 'R';
1897 mangleType(T->getPointeeType());
1898}
1899
1900// <type> ::= O <type> # rvalue reference-to (C++0x)
1901void CXXNameMangler::mangleType(const RValueReferenceType *T) {
1902 Out << 'O';
1903 mangleType(T->getPointeeType());
1904}
1905
1906// <type> ::= C <type> # complex pair (C 2000)
1907void CXXNameMangler::mangleType(const ComplexType *T) {
1908 Out << 'C';
1909 mangleType(T->getElementType());
1910}
1911
Bob Wilsonc7df92d2010-11-12 17:24:43 +00001912// ARM's ABI for Neon vector types specifies that they should be mangled as
Bob Wilson57147a82010-11-16 00:32:18 +00001913// if they are structs (to match ARM's initial implementation). The
1914// vector type must be one of the special types predefined by ARM.
1915void CXXNameMangler::mangleNeonVectorType(const VectorType *T) {
Bob Wilsonc7df92d2010-11-12 17:24:43 +00001916 QualType EltType = T->getElementType();
Bob Wilson57147a82010-11-16 00:32:18 +00001917 assert(EltType->isBuiltinType() && "Neon vector element not a BuiltinType");
Bob Wilsonc7df92d2010-11-12 17:24:43 +00001918 const char *EltName = 0;
Bob Wilson491328c2010-11-12 17:24:46 +00001919 if (T->getVectorKind() == VectorType::NeonPolyVector) {
1920 switch (cast<BuiltinType>(EltType)->getKind()) {
Bob Wilson4cfaa5d2010-11-12 17:24:49 +00001921 case BuiltinType::SChar: EltName = "poly8_t"; break;
1922 case BuiltinType::Short: EltName = "poly16_t"; break;
Bob Wilson57147a82010-11-16 00:32:18 +00001923 default: llvm_unreachable("unexpected Neon polynomial vector element type");
Bob Wilson491328c2010-11-12 17:24:46 +00001924 }
1925 } else {
1926 switch (cast<BuiltinType>(EltType)->getKind()) {
Bob Wilson4cfaa5d2010-11-12 17:24:49 +00001927 case BuiltinType::SChar: EltName = "int8_t"; break;
1928 case BuiltinType::UChar: EltName = "uint8_t"; break;
1929 case BuiltinType::Short: EltName = "int16_t"; break;
1930 case BuiltinType::UShort: EltName = "uint16_t"; break;
1931 case BuiltinType::Int: EltName = "int32_t"; break;
1932 case BuiltinType::UInt: EltName = "uint32_t"; break;
1933 case BuiltinType::LongLong: EltName = "int64_t"; break;
1934 case BuiltinType::ULongLong: EltName = "uint64_t"; break;
1935 case BuiltinType::Float: EltName = "float32_t"; break;
Bob Wilson57147a82010-11-16 00:32:18 +00001936 default: llvm_unreachable("unexpected Neon vector element type");
Bob Wilson491328c2010-11-12 17:24:46 +00001937 }
Bob Wilsonc7df92d2010-11-12 17:24:43 +00001938 }
1939 const char *BaseName = 0;
Bob Wilson4cfaa5d2010-11-12 17:24:49 +00001940 unsigned BitSize = (T->getNumElements() *
Bob Wilson3a723022010-11-16 00:32:12 +00001941 getASTContext().getTypeSize(EltType));
Bob Wilsonc7df92d2010-11-12 17:24:43 +00001942 if (BitSize == 64)
1943 BaseName = "__simd64_";
Bob Wilson57147a82010-11-16 00:32:18 +00001944 else {
1945 assert(BitSize == 128 && "Neon vector type not 64 or 128 bits");
Bob Wilsonc7df92d2010-11-12 17:24:43 +00001946 BaseName = "__simd128_";
Bob Wilson57147a82010-11-16 00:32:18 +00001947 }
Bob Wilsonc7df92d2010-11-12 17:24:43 +00001948 Out << strlen(BaseName) + strlen(EltName);
1949 Out << BaseName << EltName;
Bob Wilsonc7df92d2010-11-12 17:24:43 +00001950}
1951
John McCallefe6aee2009-09-05 07:56:18 +00001952// GNU extension: vector types
Chris Lattner788b0fd2010-06-23 06:00:24 +00001953// <type> ::= <vector-type>
1954// <vector-type> ::= Dv <positive dimension number> _
1955// <extended element type>
1956// ::= Dv [<dimension expression>] _ <element type>
1957// <extended element type> ::= <element type>
1958// ::= p # AltiVec vector pixel
John McCallefe6aee2009-09-05 07:56:18 +00001959void CXXNameMangler::mangleType(const VectorType *T) {
Bob Wilson491328c2010-11-12 17:24:46 +00001960 if ((T->getVectorKind() == VectorType::NeonVector ||
Bob Wilson57147a82010-11-16 00:32:18 +00001961 T->getVectorKind() == VectorType::NeonPolyVector)) {
1962 mangleNeonVectorType(T);
Bob Wilsonc7df92d2010-11-12 17:24:43 +00001963 return;
Bob Wilson57147a82010-11-16 00:32:18 +00001964 }
Nick Lewycky0e5f0672010-03-26 07:18:04 +00001965 Out << "Dv" << T->getNumElements() << '_';
Bob Wilsone86d78c2010-11-10 21:56:12 +00001966 if (T->getVectorKind() == VectorType::AltiVecPixel)
Chris Lattner788b0fd2010-06-23 06:00:24 +00001967 Out << 'p';
Bob Wilsone86d78c2010-11-10 21:56:12 +00001968 else if (T->getVectorKind() == VectorType::AltiVecBool)
Chris Lattner788b0fd2010-06-23 06:00:24 +00001969 Out << 'b';
1970 else
1971 mangleType(T->getElementType());
John McCallefe6aee2009-09-05 07:56:18 +00001972}
1973void CXXNameMangler::mangleType(const ExtVectorType *T) {
1974 mangleType(static_cast<const VectorType*>(T));
1975}
1976void CXXNameMangler::mangleType(const DependentSizedExtVectorType *T) {
Nick Lewycky0e5f0672010-03-26 07:18:04 +00001977 Out << "Dv";
1978 mangleExpression(T->getSizeExpr());
1979 Out << '_';
John McCallefe6aee2009-09-05 07:56:18 +00001980 mangleType(T->getElementType());
1981}
1982
Douglas Gregor7536dd52010-12-20 02:24:11 +00001983void CXXNameMangler::mangleType(const PackExpansionType *T) {
Douglas Gregor4fc48662011-01-13 16:39:34 +00001984 // <type> ::= Dp <type> # pack expansion (C++0x)
Douglas Gregor255c2692011-01-13 17:44:36 +00001985 Out << "Dp";
Douglas Gregor7536dd52010-12-20 02:24:11 +00001986 mangleType(T->getPattern());
1987}
1988
Anders Carlssona40c5e42009-03-07 22:03:21 +00001989void CXXNameMangler::mangleType(const ObjCInterfaceType *T) {
1990 mangleSourceName(T->getDecl()->getIdentifier());
1991}
1992
John McCallc12c5bb2010-05-15 11:32:37 +00001993void CXXNameMangler::mangleType(const ObjCObjectType *T) {
John McCallc00c1f62010-05-15 17:06:29 +00001994 // We don't allow overloading by different protocol qualification,
1995 // so mangling them isn't necessary.
John McCallc12c5bb2010-05-15 11:32:37 +00001996 mangleType(T->getBaseType());
1997}
1998
John McCallefe6aee2009-09-05 07:56:18 +00001999void CXXNameMangler::mangleType(const BlockPointerType *T) {
Anders Carlssonf28c6872009-12-23 22:31:44 +00002000 Out << "U13block_pointer";
2001 mangleType(T->getPointeeType());
John McCallefe6aee2009-09-05 07:56:18 +00002002}
2003
John McCall31f17ec2010-04-27 00:57:59 +00002004void CXXNameMangler::mangleType(const InjectedClassNameType *T) {
2005 // Mangle injected class name types as if the user had written the
2006 // specialization out fully. It may not actually be possible to see
2007 // this mangling, though.
2008 mangleType(T->getInjectedSpecializationType());
2009}
2010
John McCallefe6aee2009-09-05 07:56:18 +00002011void CXXNameMangler::mangleType(const TemplateSpecializationType *T) {
Douglas Gregor1e9268e2010-04-28 05:58:56 +00002012 if (TemplateDecl *TD = T->getTemplateName().getAsTemplateDecl()) {
2013 mangleName(TD, T->getArgs(), T->getNumArgs());
2014 } else {
2015 if (mangleSubstitution(QualType(T, 0)))
2016 return;
Sean Huntc3021132010-05-05 15:23:54 +00002017
Douglas Gregor1e9268e2010-04-28 05:58:56 +00002018 mangleTemplatePrefix(T->getTemplateName());
Sean Huntc3021132010-05-05 15:23:54 +00002019
Douglas Gregor1e9268e2010-04-28 05:58:56 +00002020 // FIXME: GCC does not appear to mangle the template arguments when
2021 // the template in question is a dependent template name. Should we
2022 // emulate that badness?
2023 mangleTemplateArgs(T->getTemplateName(), T->getArgs(), T->getNumArgs());
2024 addSubstitution(QualType(T, 0));
2025 }
John McCallefe6aee2009-09-05 07:56:18 +00002026}
2027
Douglas Gregor4714c122010-03-31 17:34:00 +00002028void CXXNameMangler::mangleType(const DependentNameType *T) {
Anders Carlssonae352482009-09-26 02:26:02 +00002029 // Typename types are always nested
2030 Out << 'N';
John McCalla0ce15c2011-04-24 08:23:24 +00002031 manglePrefix(T->getQualifier());
John McCall33500952010-06-11 00:33:02 +00002032 mangleSourceName(T->getIdentifier());
2033 Out << 'E';
2034}
John McCall6ab30e02010-06-09 07:26:17 +00002035
John McCall33500952010-06-11 00:33:02 +00002036void CXXNameMangler::mangleType(const DependentTemplateSpecializationType *T) {
Douglas Gregoraa2187d2011-02-28 00:04:36 +00002037 // Dependently-scoped template types are nested if they have a prefix.
John McCall33500952010-06-11 00:33:02 +00002038 Out << 'N';
2039
2040 // TODO: avoid making this TemplateName.
2041 TemplateName Prefix =
2042 getASTContext().getDependentTemplateName(T->getQualifier(),
2043 T->getIdentifier());
2044 mangleTemplatePrefix(Prefix);
2045
2046 // FIXME: GCC does not appear to mangle the template arguments when
2047 // the template in question is a dependent template name. Should we
2048 // emulate that badness?
2049 mangleTemplateArgs(Prefix, T->getArgs(), T->getNumArgs());
Anders Carlssonae352482009-09-26 02:26:02 +00002050 Out << 'E';
John McCallefe6aee2009-09-05 07:56:18 +00002051}
2052
John McCallad5e7382010-03-01 23:49:17 +00002053void CXXNameMangler::mangleType(const TypeOfType *T) {
2054 // FIXME: this is pretty unsatisfactory, but there isn't an obvious
2055 // "extension with parameters" mangling.
2056 Out << "u6typeof";
2057}
2058
2059void CXXNameMangler::mangleType(const TypeOfExprType *T) {
2060 // FIXME: this is pretty unsatisfactory, but there isn't an obvious
2061 // "extension with parameters" mangling.
2062 Out << "u6typeof";
2063}
2064
2065void CXXNameMangler::mangleType(const DecltypeType *T) {
2066 Expr *E = T->getUnderlyingExpr();
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002067
John McCallad5e7382010-03-01 23:49:17 +00002068 // type ::= Dt <expression> E # decltype of an id-expression
2069 // # or class member access
2070 // ::= DT <expression> E # decltype of an expression
2071
2072 // This purports to be an exhaustive list of id-expressions and
2073 // class member accesses. Note that we do not ignore parentheses;
2074 // parentheses change the semantics of decltype for these
2075 // expressions (and cause the mangler to use the other form).
2076 if (isa<DeclRefExpr>(E) ||
2077 isa<MemberExpr>(E) ||
2078 isa<UnresolvedLookupExpr>(E) ||
2079 isa<DependentScopeDeclRefExpr>(E) ||
2080 isa<CXXDependentScopeMemberExpr>(E) ||
2081 isa<UnresolvedMemberExpr>(E))
2082 Out << "Dt";
2083 else
2084 Out << "DT";
2085 mangleExpression(E);
2086 Out << 'E';
2087}
2088
Sean Huntca63c202011-05-24 22:41:36 +00002089void CXXNameMangler::mangleType(const UnaryTransformType *T) {
2090 // If this is dependent, we need to record that. If not, we simply
2091 // mangle it as the underlying type since they are equivalent.
2092 if (T->isDependentType()) {
2093 Out << 'U';
2094
2095 switch (T->getUTTKind()) {
2096 case UnaryTransformType::EnumUnderlyingType:
2097 Out << "3eut";
2098 break;
2099 }
2100 }
2101
2102 mangleType(T->getUnderlyingType());
2103}
2104
Richard Smith34b41d92011-02-20 03:19:35 +00002105void CXXNameMangler::mangleType(const AutoType *T) {
2106 QualType D = T->getDeducedType();
Richard Smith967ecd32011-02-21 20:10:02 +00002107 // <builtin-type> ::= Da # dependent auto
2108 if (D.isNull())
2109 Out << "Da";
2110 else
2111 mangleType(D);
Richard Smith34b41d92011-02-20 03:19:35 +00002112}
2113
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002114void CXXNameMangler::mangleIntegerLiteral(QualType T,
Anders Carlssone170ba72009-12-14 01:45:37 +00002115 const llvm::APSInt &Value) {
2116 // <expr-primary> ::= L <type> <value number> E # integer literal
2117 Out << 'L';
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002118
Anders Carlssone170ba72009-12-14 01:45:37 +00002119 mangleType(T);
2120 if (T->isBooleanType()) {
2121 // Boolean values are encoded as 0/1.
2122 Out << (Value.getBoolValue() ? '1' : '0');
2123 } else {
John McCall0512e482010-07-14 04:20:34 +00002124 mangleNumber(Value);
Anders Carlssone170ba72009-12-14 01:45:37 +00002125 }
2126 Out << 'E';
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002127
Anders Carlssone170ba72009-12-14 01:45:37 +00002128}
2129
John McCall2f27bf82010-02-04 02:56:29 +00002130/// Mangles a member expression. Implicit accesses are not handled,
2131/// but that should be okay, because you shouldn't be able to
2132/// make an implicit access in a function template declaration.
John McCalla0ce15c2011-04-24 08:23:24 +00002133void CXXNameMangler::mangleMemberExpr(const Expr *base,
2134 bool isArrow,
2135 NestedNameSpecifier *qualifier,
2136 NamedDecl *firstQualifierLookup,
2137 DeclarationName member,
2138 unsigned arity) {
2139 // <expression> ::= dt <expression> <unresolved-name>
2140 // ::= pt <expression> <unresolved-name>
2141 Out << (isArrow ? "pt" : "dt");
2142 mangleExpression(base);
2143 mangleUnresolvedName(qualifier, firstQualifierLookup, member, arity);
John McCall2f27bf82010-02-04 02:56:29 +00002144}
2145
John McCall5a7e6f72011-04-28 02:52:03 +00002146/// Look at the callee of the given call expression and determine if
2147/// it's a parenthesized id-expression which would have triggered ADL
2148/// otherwise.
2149static bool isParenthesizedADLCallee(const CallExpr *call) {
2150 const Expr *callee = call->getCallee();
2151 const Expr *fn = callee->IgnoreParens();
2152
2153 // Must be parenthesized. IgnoreParens() skips __extension__ nodes,
2154 // too, but for those to appear in the callee, it would have to be
2155 // parenthesized.
2156 if (callee == fn) return false;
2157
2158 // Must be an unresolved lookup.
2159 const UnresolvedLookupExpr *lookup = dyn_cast<UnresolvedLookupExpr>(fn);
2160 if (!lookup) return false;
2161
2162 assert(!lookup->requiresADL());
2163
2164 // Must be an unqualified lookup.
2165 if (lookup->getQualifier()) return false;
2166
2167 // Must not have found a class member. Note that if one is a class
2168 // member, they're all class members.
2169 if (lookup->getNumDecls() > 0 &&
2170 (*lookup->decls_begin())->isCXXClassMember())
2171 return false;
2172
2173 // Otherwise, ADL would have been triggered.
2174 return true;
2175}
2176
John McCall5e1e89b2010-08-18 19:18:59 +00002177void CXXNameMangler::mangleExpression(const Expr *E, unsigned Arity) {
Anders Carlssond553f8c2009-09-21 01:21:10 +00002178 // <expression> ::= <unary operator-name> <expression>
John McCall09cc1412010-02-03 00:55:45 +00002179 // ::= <binary operator-name> <expression> <expression>
2180 // ::= <trinary operator-name> <expression> <expression> <expression>
Anders Carlssond553f8c2009-09-21 01:21:10 +00002181 // ::= cv <type> expression # conversion with one argument
2182 // ::= cv <type> _ <expression>* E # conversion with a different number of arguments
Eli Friedmana7e68452010-08-22 01:00:03 +00002183 // ::= st <type> # sizeof (a type)
Anders Carlssond553f8c2009-09-21 01:21:10 +00002184 // ::= at <type> # alignof (a type)
2185 // ::= <template-param>
2186 // ::= <function-param>
2187 // ::= sr <type> <unqualified-name> # dependent name
2188 // ::= sr <type> <unqualified-name> <template-args> # dependent template-id
Douglas Gregor63f62df2011-06-05 05:27:58 +00002189 // ::= ds <expression> <expression> # expr.*expr
Anders Carlssond553f8c2009-09-21 01:21:10 +00002190 // ::= sZ <template-param> # size of a parameter pack
Douglas Gregor4fc48662011-01-13 16:39:34 +00002191 // ::= sZ <function-param> # size of a function parameter pack
John McCall09cc1412010-02-03 00:55:45 +00002192 // ::= <expr-primary>
John McCall1dd73832010-02-04 01:42:13 +00002193 // <expr-primary> ::= L <type> <value number> E # integer literal
2194 // ::= L <type <value float> E # floating literal
2195 // ::= L <mangled-name> E # external name
Douglas Gregoredee94b2011-07-12 04:47:20 +00002196 QualType ImplicitlyConvertedToType;
2197
2198recurse:
Anders Carlssond553f8c2009-09-21 01:21:10 +00002199 switch (E->getStmtClass()) {
John McCall6ae1f352010-04-09 22:26:14 +00002200 case Expr::NoStmtClass:
John McCall63c00d72011-02-09 08:16:59 +00002201#define ABSTRACT_STMT(Type)
John McCall6ae1f352010-04-09 22:26:14 +00002202#define EXPR(Type, Base)
2203#define STMT(Type, Base) \
2204 case Expr::Type##Class:
Sean Hunt4bfe1962010-05-05 15:24:00 +00002205#include "clang/AST/StmtNodes.inc"
John McCall0512e482010-07-14 04:20:34 +00002206 // fallthrough
2207
2208 // These all can only appear in local or variable-initialization
2209 // contexts and so should never appear in a mangling.
2210 case Expr::AddrLabelExprClass:
2211 case Expr::BlockDeclRefExprClass:
2212 case Expr::CXXThisExprClass:
2213 case Expr::DesignatedInitExprClass:
2214 case Expr::ImplicitValueInitExprClass:
2215 case Expr::InitListExprClass:
2216 case Expr::ParenListExprClass:
2217 case Expr::CXXScalarValueInitExprClass:
John McCall09cc1412010-02-03 00:55:45 +00002218 llvm_unreachable("unexpected statement kind");
2219 break;
2220
John McCall0512e482010-07-14 04:20:34 +00002221 // FIXME: invent manglings for all these.
2222 case Expr::BlockExprClass:
2223 case Expr::CXXPseudoDestructorExprClass:
2224 case Expr::ChooseExprClass:
2225 case Expr::CompoundLiteralExprClass:
2226 case Expr::ExtVectorElementExprClass:
Peter Collingbournef111d932011-04-15 00:35:48 +00002227 case Expr::GenericSelectionExprClass:
John McCall0512e482010-07-14 04:20:34 +00002228 case Expr::ObjCEncodeExprClass:
John McCall0512e482010-07-14 04:20:34 +00002229 case Expr::ObjCIsaExprClass:
2230 case Expr::ObjCIvarRefExprClass:
2231 case Expr::ObjCMessageExprClass:
2232 case Expr::ObjCPropertyRefExprClass:
2233 case Expr::ObjCProtocolExprClass:
2234 case Expr::ObjCSelectorExprClass:
2235 case Expr::ObjCStringLiteralClass:
John McCallf85e1932011-06-15 23:02:42 +00002236 case Expr::ObjCIndirectCopyRestoreExprClass:
John McCall0512e482010-07-14 04:20:34 +00002237 case Expr::OffsetOfExprClass:
2238 case Expr::PredefinedExprClass:
2239 case Expr::ShuffleVectorExprClass:
2240 case Expr::StmtExprClass:
John McCall0512e482010-07-14 04:20:34 +00002241 case Expr::UnaryTypeTraitExprClass:
Francois Pichet6ad6f282010-12-07 00:08:36 +00002242 case Expr::BinaryTypeTraitExprClass:
John Wiegley21ff2e52011-04-28 00:16:57 +00002243 case Expr::ArrayTypeTraitExprClass:
John Wiegley55262202011-04-25 06:54:41 +00002244 case Expr::ExpressionTraitExprClass:
Francois Pichet9be88402010-09-08 23:47:05 +00002245 case Expr::VAArgExprClass:
Sebastian Redl2e156222010-09-10 20:55:43 +00002246 case Expr::CXXUuidofExprClass:
Peter Collingbournee08ce652011-02-09 21:07:24 +00002247 case Expr::CXXNoexceptExprClass:
Tanya Lattner61eee0c2011-06-04 00:47:47 +00002248 case Expr::CUDAKernelCallExprClass:
2249 case Expr::AsTypeExprClass:
2250 {
John McCall6ae1f352010-04-09 22:26:14 +00002251 // As bad as this diagnostic is, it's better than crashing.
2252 Diagnostic &Diags = Context.getDiags();
2253 unsigned DiagID = Diags.getCustomDiagID(Diagnostic::Error,
2254 "cannot yet mangle expression type %0");
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +00002255 Diags.Report(E->getExprLoc(), DiagID)
John McCall739bf092010-04-10 09:39:25 +00002256 << E->getStmtClassName() << E->getSourceRange();
John McCall6ae1f352010-04-09 22:26:14 +00002257 break;
2258 }
2259
John McCall56ca35d2011-02-17 10:25:35 +00002260 // Even gcc-4.5 doesn't mangle this.
2261 case Expr::BinaryConditionalOperatorClass: {
2262 Diagnostic &Diags = Context.getDiags();
2263 unsigned DiagID =
2264 Diags.getCustomDiagID(Diagnostic::Error,
2265 "?: operator with omitted middle operand cannot be mangled");
2266 Diags.Report(E->getExprLoc(), DiagID)
2267 << E->getStmtClassName() << E->getSourceRange();
2268 break;
2269 }
2270
2271 // These are used for internal purposes and cannot be meaningfully mangled.
John McCall7cd7d1a2010-11-15 23:31:06 +00002272 case Expr::OpaqueValueExprClass:
2273 llvm_unreachable("cannot mangle opaque value; mangling wrong thing?");
2274
John McCall0512e482010-07-14 04:20:34 +00002275 case Expr::CXXDefaultArgExprClass:
John McCall5e1e89b2010-08-18 19:18:59 +00002276 mangleExpression(cast<CXXDefaultArgExpr>(E)->getExpr(), Arity);
John McCall0512e482010-07-14 04:20:34 +00002277 break;
2278
John McCall91a57552011-07-15 05:09:51 +00002279 case Expr::SubstNonTypeTemplateParmExprClass:
2280 mangleExpression(cast<SubstNonTypeTemplateParmExpr>(E)->getReplacement(),
2281 Arity);
2282 break;
2283
John McCall0512e482010-07-14 04:20:34 +00002284 case Expr::CXXMemberCallExprClass: // fallthrough
John McCall1dd73832010-02-04 01:42:13 +00002285 case Expr::CallExprClass: {
2286 const CallExpr *CE = cast<CallExpr>(E);
John McCall5a7e6f72011-04-28 02:52:03 +00002287
2288 // <expression> ::= cp <simple-id> <expression>* E
2289 // We use this mangling only when the call would use ADL except
2290 // for being parenthesized. Per discussion with David
2291 // Vandervoorde, 2011.04.25.
2292 if (isParenthesizedADLCallee(CE)) {
2293 Out << "cp";
2294 // The callee here is a parenthesized UnresolvedLookupExpr with
2295 // no qualifier and should always get mangled as a <simple-id>
2296 // anyway.
2297
2298 // <expression> ::= cl <expression>* E
2299 } else {
2300 Out << "cl";
2301 }
2302
John McCall5e1e89b2010-08-18 19:18:59 +00002303 mangleExpression(CE->getCallee(), CE->getNumArgs());
John McCall1dd73832010-02-04 01:42:13 +00002304 for (unsigned I = 0, N = CE->getNumArgs(); I != N; ++I)
2305 mangleExpression(CE->getArg(I));
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002306 Out << 'E';
John McCall09cc1412010-02-03 00:55:45 +00002307 break;
John McCall1dd73832010-02-04 01:42:13 +00002308 }
John McCall09cc1412010-02-03 00:55:45 +00002309
John McCall0512e482010-07-14 04:20:34 +00002310 case Expr::CXXNewExprClass: {
2311 // Proposal from David Vandervoorde, 2010.06.30
2312 const CXXNewExpr *New = cast<CXXNewExpr>(E);
2313 if (New->isGlobalNew()) Out << "gs";
2314 Out << (New->isArray() ? "na" : "nw");
2315 for (CXXNewExpr::const_arg_iterator I = New->placement_arg_begin(),
2316 E = New->placement_arg_end(); I != E; ++I)
2317 mangleExpression(*I);
2318 Out << '_';
2319 mangleType(New->getAllocatedType());
2320 if (New->hasInitializer()) {
2321 Out << "pi";
2322 for (CXXNewExpr::const_arg_iterator I = New->constructor_arg_begin(),
2323 E = New->constructor_arg_end(); I != E; ++I)
2324 mangleExpression(*I);
2325 }
2326 Out << 'E';
2327 break;
2328 }
2329
John McCall2f27bf82010-02-04 02:56:29 +00002330 case Expr::MemberExprClass: {
2331 const MemberExpr *ME = cast<MemberExpr>(E);
2332 mangleMemberExpr(ME->getBase(), ME->isArrow(),
John McCalla0ce15c2011-04-24 08:23:24 +00002333 ME->getQualifier(), 0, ME->getMemberDecl()->getDeclName(),
John McCall5e1e89b2010-08-18 19:18:59 +00002334 Arity);
John McCall2f27bf82010-02-04 02:56:29 +00002335 break;
2336 }
2337
2338 case Expr::UnresolvedMemberExprClass: {
2339 const UnresolvedMemberExpr *ME = cast<UnresolvedMemberExpr>(E);
2340 mangleMemberExpr(ME->getBase(), ME->isArrow(),
John McCalla0ce15c2011-04-24 08:23:24 +00002341 ME->getQualifier(), 0, ME->getMemberName(),
John McCall5e1e89b2010-08-18 19:18:59 +00002342 Arity);
John McCall6dbce192010-08-20 00:17:19 +00002343 if (ME->hasExplicitTemplateArgs())
2344 mangleTemplateArgs(ME->getExplicitTemplateArgs());
John McCall2f27bf82010-02-04 02:56:29 +00002345 break;
2346 }
2347
2348 case Expr::CXXDependentScopeMemberExprClass: {
2349 const CXXDependentScopeMemberExpr *ME
2350 = cast<CXXDependentScopeMemberExpr>(E);
2351 mangleMemberExpr(ME->getBase(), ME->isArrow(),
John McCalla0ce15c2011-04-24 08:23:24 +00002352 ME->getQualifier(), ME->getFirstQualifierFoundInScope(),
2353 ME->getMember(), Arity);
John McCall6dbce192010-08-20 00:17:19 +00002354 if (ME->hasExplicitTemplateArgs())
2355 mangleTemplateArgs(ME->getExplicitTemplateArgs());
John McCall2f27bf82010-02-04 02:56:29 +00002356 break;
2357 }
2358
John McCall1dd73832010-02-04 01:42:13 +00002359 case Expr::UnresolvedLookupExprClass: {
2360 const UnresolvedLookupExpr *ULE = cast<UnresolvedLookupExpr>(E);
John McCalla0ce15c2011-04-24 08:23:24 +00002361 mangleUnresolvedName(ULE->getQualifier(), 0, ULE->getName(), Arity);
John McCall26a6ec72011-06-21 22:12:46 +00002362
2363 // All the <unresolved-name> productions end in a
2364 // base-unresolved-name, where <template-args> are just tacked
2365 // onto the end.
John McCall6dbce192010-08-20 00:17:19 +00002366 if (ULE->hasExplicitTemplateArgs())
2367 mangleTemplateArgs(ULE->getExplicitTemplateArgs());
John McCall1dd73832010-02-04 01:42:13 +00002368 break;
2369 }
2370
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002371 case Expr::CXXUnresolvedConstructExprClass: {
John McCall1dd73832010-02-04 01:42:13 +00002372 const CXXUnresolvedConstructExpr *CE = cast<CXXUnresolvedConstructExpr>(E);
2373 unsigned N = CE->arg_size();
2374
2375 Out << "cv";
2376 mangleType(CE->getType());
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002377 if (N != 1) Out << '_';
John McCall1dd73832010-02-04 01:42:13 +00002378 for (unsigned I = 0; I != N; ++I) mangleExpression(CE->getArg(I));
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002379 if (N != 1) Out << 'E';
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002380 break;
John McCall1dd73832010-02-04 01:42:13 +00002381 }
John McCall09cc1412010-02-03 00:55:45 +00002382
John McCall1dd73832010-02-04 01:42:13 +00002383 case Expr::CXXTemporaryObjectExprClass:
2384 case Expr::CXXConstructExprClass: {
2385 const CXXConstructExpr *CE = cast<CXXConstructExpr>(E);
2386 unsigned N = CE->getNumArgs();
2387
2388 Out << "cv";
2389 mangleType(CE->getType());
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002390 if (N != 1) Out << '_';
John McCall1dd73832010-02-04 01:42:13 +00002391 for (unsigned I = 0; I != N; ++I) mangleExpression(CE->getArg(I));
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002392 if (N != 1) Out << 'E';
John McCall09cc1412010-02-03 00:55:45 +00002393 break;
John McCall1dd73832010-02-04 01:42:13 +00002394 }
2395
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +00002396 case Expr::UnaryExprOrTypeTraitExprClass: {
2397 const UnaryExprOrTypeTraitExpr *SAE = cast<UnaryExprOrTypeTraitExpr>(E);
Douglas Gregoredee94b2011-07-12 04:47:20 +00002398
2399 if (!SAE->isInstantiationDependent()) {
2400 // Itanium C++ ABI:
2401 // If the operand of a sizeof or alignof operator is not
2402 // instantiation-dependent it is encoded as an integer literal
2403 // reflecting the result of the operator.
2404 //
2405 // If the result of the operator is implicitly converted to a known
2406 // integer type, that type is used for the literal; otherwise, the type
2407 // of std::size_t or std::ptrdiff_t is used.
2408 QualType T = (ImplicitlyConvertedToType.isNull() ||
2409 !ImplicitlyConvertedToType->isIntegerType())? SAE->getType()
2410 : ImplicitlyConvertedToType;
2411 mangleIntegerLiteral(T, SAE->EvaluateAsInt(Context.getASTContext()));
2412 break;
2413 }
2414
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +00002415 switch(SAE->getKind()) {
2416 case UETT_SizeOf:
2417 Out << 's';
2418 break;
2419 case UETT_AlignOf:
2420 Out << 'a';
2421 break;
2422 case UETT_VecStep:
2423 Diagnostic &Diags = Context.getDiags();
2424 unsigned DiagID = Diags.getCustomDiagID(Diagnostic::Error,
2425 "cannot yet mangle vec_step expression");
2426 Diags.Report(DiagID);
2427 return;
2428 }
John McCall1dd73832010-02-04 01:42:13 +00002429 if (SAE->isArgumentType()) {
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002430 Out << 't';
John McCall1dd73832010-02-04 01:42:13 +00002431 mangleType(SAE->getArgumentType());
2432 } else {
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002433 Out << 'z';
John McCall1dd73832010-02-04 01:42:13 +00002434 mangleExpression(SAE->getArgumentExpr());
2435 }
2436 break;
2437 }
Anders Carlssona7694082009-11-06 02:50:19 +00002438
John McCall0512e482010-07-14 04:20:34 +00002439 case Expr::CXXThrowExprClass: {
2440 const CXXThrowExpr *TE = cast<CXXThrowExpr>(E);
2441
2442 // Proposal from David Vandervoorde, 2010.06.30
2443 if (TE->getSubExpr()) {
2444 Out << "tw";
2445 mangleExpression(TE->getSubExpr());
2446 } else {
2447 Out << "tr";
2448 }
2449 break;
2450 }
2451
2452 case Expr::CXXTypeidExprClass: {
2453 const CXXTypeidExpr *TIE = cast<CXXTypeidExpr>(E);
2454
2455 // Proposal from David Vandervoorde, 2010.06.30
2456 if (TIE->isTypeOperand()) {
2457 Out << "ti";
2458 mangleType(TIE->getTypeOperand());
2459 } else {
2460 Out << "te";
2461 mangleExpression(TIE->getExprOperand());
2462 }
2463 break;
2464 }
2465
2466 case Expr::CXXDeleteExprClass: {
2467 const CXXDeleteExpr *DE = cast<CXXDeleteExpr>(E);
2468
2469 // Proposal from David Vandervoorde, 2010.06.30
2470 if (DE->isGlobalDelete()) Out << "gs";
2471 Out << (DE->isArrayForm() ? "da" : "dl");
2472 mangleExpression(DE->getArgument());
2473 break;
2474 }
2475
Anders Carlssone170ba72009-12-14 01:45:37 +00002476 case Expr::UnaryOperatorClass: {
2477 const UnaryOperator *UO = cast<UnaryOperator>(E);
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002478 mangleOperatorName(UnaryOperator::getOverloadedOperator(UO->getOpcode()),
Anders Carlssone170ba72009-12-14 01:45:37 +00002479 /*Arity=*/1);
2480 mangleExpression(UO->getSubExpr());
2481 break;
2482 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002483
John McCall0512e482010-07-14 04:20:34 +00002484 case Expr::ArraySubscriptExprClass: {
2485 const ArraySubscriptExpr *AE = cast<ArraySubscriptExpr>(E);
2486
Chris Lattnerfc8f0e12011-04-15 05:22:18 +00002487 // Array subscript is treated as a syntactically weird form of
John McCall0512e482010-07-14 04:20:34 +00002488 // binary operator.
2489 Out << "ix";
2490 mangleExpression(AE->getLHS());
2491 mangleExpression(AE->getRHS());
2492 break;
2493 }
2494
2495 case Expr::CompoundAssignOperatorClass: // fallthrough
Anders Carlssone170ba72009-12-14 01:45:37 +00002496 case Expr::BinaryOperatorClass: {
2497 const BinaryOperator *BO = cast<BinaryOperator>(E);
Douglas Gregor63f62df2011-06-05 05:27:58 +00002498 if (BO->getOpcode() == BO_PtrMemD)
2499 Out << "ds";
2500 else
2501 mangleOperatorName(BinaryOperator::getOverloadedOperator(BO->getOpcode()),
2502 /*Arity=*/2);
Anders Carlssone170ba72009-12-14 01:45:37 +00002503 mangleExpression(BO->getLHS());
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002504 mangleExpression(BO->getRHS());
Anders Carlssone170ba72009-12-14 01:45:37 +00002505 break;
John McCall2f27bf82010-02-04 02:56:29 +00002506 }
Anders Carlssone170ba72009-12-14 01:45:37 +00002507
2508 case Expr::ConditionalOperatorClass: {
2509 const ConditionalOperator *CO = cast<ConditionalOperator>(E);
2510 mangleOperatorName(OO_Conditional, /*Arity=*/3);
2511 mangleExpression(CO->getCond());
John McCall5e1e89b2010-08-18 19:18:59 +00002512 mangleExpression(CO->getLHS(), Arity);
2513 mangleExpression(CO->getRHS(), Arity);
Anders Carlssone170ba72009-12-14 01:45:37 +00002514 break;
2515 }
2516
Douglas Gregor46287c72010-01-29 16:37:09 +00002517 case Expr::ImplicitCastExprClass: {
Douglas Gregoredee94b2011-07-12 04:47:20 +00002518 ImplicitlyConvertedToType = E->getType();
2519 E = cast<ImplicitCastExpr>(E)->getSubExpr();
2520 goto recurse;
Douglas Gregor46287c72010-01-29 16:37:09 +00002521 }
John McCallf85e1932011-06-15 23:02:42 +00002522
2523 case Expr::ObjCBridgedCastExprClass: {
2524 // Mangle ownership casts as a vendor extended operator __bridge,
2525 // __bridge_transfer, or __bridge_retain.
Chris Lattner5f9e2722011-07-23 10:55:15 +00002526 StringRef Kind = cast<ObjCBridgedCastExpr>(E)->getBridgeKindName();
John McCallf85e1932011-06-15 23:02:42 +00002527 Out << "v1U" << Kind.size() << Kind;
2528 }
2529 // Fall through to mangle the cast itself.
2530
Douglas Gregor46287c72010-01-29 16:37:09 +00002531 case Expr::CStyleCastExprClass:
2532 case Expr::CXXStaticCastExprClass:
2533 case Expr::CXXDynamicCastExprClass:
2534 case Expr::CXXReinterpretCastExprClass:
2535 case Expr::CXXConstCastExprClass:
2536 case Expr::CXXFunctionalCastExprClass: {
2537 const ExplicitCastExpr *ECE = cast<ExplicitCastExpr>(E);
2538 Out << "cv";
2539 mangleType(ECE->getType());
2540 mangleExpression(ECE->getSubExpr());
2541 break;
2542 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002543
Anders Carlsson58040a52009-12-16 05:48:46 +00002544 case Expr::CXXOperatorCallExprClass: {
2545 const CXXOperatorCallExpr *CE = cast<CXXOperatorCallExpr>(E);
2546 unsigned NumArgs = CE->getNumArgs();
2547 mangleOperatorName(CE->getOperator(), /*Arity=*/NumArgs);
2548 // Mangle the arguments.
2549 for (unsigned i = 0; i != NumArgs; ++i)
2550 mangleExpression(CE->getArg(i));
2551 break;
2552 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002553
Anders Carlssona7694082009-11-06 02:50:19 +00002554 case Expr::ParenExprClass:
John McCall5e1e89b2010-08-18 19:18:59 +00002555 mangleExpression(cast<ParenExpr>(E)->getSubExpr(), Arity);
Anders Carlssona7694082009-11-06 02:50:19 +00002556 break;
2557
Anders Carlssond553f8c2009-09-21 01:21:10 +00002558 case Expr::DeclRefExprClass: {
Douglas Gregor5ed1bc32010-02-28 21:40:32 +00002559 const NamedDecl *D = cast<DeclRefExpr>(E)->getDecl();
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002560
Anders Carlssond553f8c2009-09-21 01:21:10 +00002561 switch (D->getKind()) {
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002562 default:
Douglas Gregor5ed1bc32010-02-28 21:40:32 +00002563 // <expr-primary> ::= L <mangled-name> E # external name
2564 Out << 'L';
2565 mangle(D, "_Z");
2566 Out << 'E';
2567 break;
2568
John McCallfb44de92011-05-01 22:35:37 +00002569 case Decl::ParmVar:
2570 mangleFunctionParam(cast<ParmVarDecl>(D));
2571 break;
2572
John McCall3dc7e7b2010-07-24 01:17:35 +00002573 case Decl::EnumConstant: {
2574 const EnumConstantDecl *ED = cast<EnumConstantDecl>(D);
2575 mangleIntegerLiteral(ED->getType(), ED->getInitVal());
2576 break;
2577 }
2578
Anders Carlssond553f8c2009-09-21 01:21:10 +00002579 case Decl::NonTypeTemplateParm: {
2580 const NonTypeTemplateParmDecl *PD = cast<NonTypeTemplateParmDecl>(D);
Anders Carlsson0ccdf8d2009-09-27 00:38:53 +00002581 mangleTemplateParameter(PD->getIndex());
Anders Carlssond553f8c2009-09-21 01:21:10 +00002582 break;
2583 }
2584
2585 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002586
Anders Carlsson50755b02009-09-27 20:11:34 +00002587 break;
Anders Carlssond553f8c2009-09-21 01:21:10 +00002588 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002589
Douglas Gregorc7793c72011-01-15 01:15:58 +00002590 case Expr::SubstNonTypeTemplateParmPackExprClass:
John McCall68a51a72011-07-01 00:04:39 +00002591 // FIXME: not clear how to mangle this!
2592 // template <unsigned N...> class A {
2593 // template <class U...> void foo(U (&x)[N]...);
2594 // };
2595 Out << "_SUBSTPACK_";
Douglas Gregorc7793c72011-01-15 01:15:58 +00002596 break;
2597
John McCall865d4472009-11-19 22:55:06 +00002598 case Expr::DependentScopeDeclRefExprClass: {
2599 const DependentScopeDeclRefExpr *DRE = cast<DependentScopeDeclRefExpr>(E);
John McCall26a6ec72011-06-21 22:12:46 +00002600 mangleUnresolvedName(DRE->getQualifier(), 0, DRE->getDeclName(), Arity);
Douglas Gregor4b2ccfc2010-02-28 22:05:49 +00002601
John McCall26a6ec72011-06-21 22:12:46 +00002602 // All the <unresolved-name> productions end in a
2603 // base-unresolved-name, where <template-args> are just tacked
2604 // onto the end.
John McCall6dbce192010-08-20 00:17:19 +00002605 if (DRE->hasExplicitTemplateArgs())
2606 mangleTemplateArgs(DRE->getExplicitTemplateArgs());
Anders Carlsson50755b02009-09-27 20:11:34 +00002607 break;
2608 }
2609
John McCalld9307602010-04-09 22:54:09 +00002610 case Expr::CXXBindTemporaryExprClass:
2611 mangleExpression(cast<CXXBindTemporaryExpr>(E)->getSubExpr());
2612 break;
2613
John McCall4765fa02010-12-06 08:20:24 +00002614 case Expr::ExprWithCleanupsClass:
2615 mangleExpression(cast<ExprWithCleanups>(E)->getSubExpr(), Arity);
John McCalld9307602010-04-09 22:54:09 +00002616 break;
2617
John McCall1dd73832010-02-04 01:42:13 +00002618 case Expr::FloatingLiteralClass: {
2619 const FloatingLiteral *FL = cast<FloatingLiteral>(E);
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002620 Out << 'L';
John McCall1dd73832010-02-04 01:42:13 +00002621 mangleType(FL->getType());
John McCall0512e482010-07-14 04:20:34 +00002622 mangleFloat(FL->getValue());
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002623 Out << 'E';
John McCall1dd73832010-02-04 01:42:13 +00002624 break;
2625 }
2626
John McCallde810632010-04-09 21:48:08 +00002627 case Expr::CharacterLiteralClass:
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002628 Out << 'L';
John McCallde810632010-04-09 21:48:08 +00002629 mangleType(E->getType());
2630 Out << cast<CharacterLiteral>(E)->getValue();
2631 Out << 'E';
2632 break;
2633
2634 case Expr::CXXBoolLiteralExprClass:
2635 Out << "Lb";
2636 Out << (cast<CXXBoolLiteralExpr>(E)->getValue() ? '1' : '0');
2637 Out << 'E';
2638 break;
2639
John McCall0512e482010-07-14 04:20:34 +00002640 case Expr::IntegerLiteralClass: {
2641 llvm::APSInt Value(cast<IntegerLiteral>(E)->getValue());
2642 if (E->getType()->isSignedIntegerType())
2643 Value.setIsSigned(true);
2644 mangleIntegerLiteral(E->getType(), Value);
Anders Carlssone170ba72009-12-14 01:45:37 +00002645 break;
John McCall0512e482010-07-14 04:20:34 +00002646 }
2647
2648 case Expr::ImaginaryLiteralClass: {
2649 const ImaginaryLiteral *IE = cast<ImaginaryLiteral>(E);
2650 // Mangle as if a complex literal.
Nick Lewycky271b6652010-09-05 03:40:33 +00002651 // Proposal from David Vandevoorde, 2010.06.30.
John McCall0512e482010-07-14 04:20:34 +00002652 Out << 'L';
2653 mangleType(E->getType());
2654 if (const FloatingLiteral *Imag =
2655 dyn_cast<FloatingLiteral>(IE->getSubExpr())) {
2656 // Mangle a floating-point zero of the appropriate type.
2657 mangleFloat(llvm::APFloat(Imag->getValue().getSemantics()));
2658 Out << '_';
2659 mangleFloat(Imag->getValue());
2660 } else {
Nick Lewycky271b6652010-09-05 03:40:33 +00002661 Out << "0_";
John McCall0512e482010-07-14 04:20:34 +00002662 llvm::APSInt Value(cast<IntegerLiteral>(IE->getSubExpr())->getValue());
2663 if (IE->getSubExpr()->getType()->isSignedIntegerType())
2664 Value.setIsSigned(true);
2665 mangleNumber(Value);
2666 }
2667 Out << 'E';
2668 break;
2669 }
2670
2671 case Expr::StringLiteralClass: {
John McCall1658c392010-07-15 21:53:03 +00002672 // Revised proposal from David Vandervoorde, 2010.07.15.
John McCall0512e482010-07-14 04:20:34 +00002673 Out << 'L';
John McCall1658c392010-07-15 21:53:03 +00002674 assert(isa<ConstantArrayType>(E->getType()));
2675 mangleType(E->getType());
John McCall0512e482010-07-14 04:20:34 +00002676 Out << 'E';
2677 break;
2678 }
2679
2680 case Expr::GNUNullExprClass:
2681 // FIXME: should this really be mangled the same as nullptr?
2682 // fallthrough
2683
2684 case Expr::CXXNullPtrLiteralExprClass: {
2685 // Proposal from David Vandervoorde, 2010.06.30, as
2686 // modified by ABI list discussion.
2687 Out << "LDnE";
2688 break;
2689 }
Douglas Gregorbe230c32011-01-03 17:17:50 +00002690
2691 case Expr::PackExpansionExprClass:
2692 Out << "sp";
2693 mangleExpression(cast<PackExpansionExpr>(E)->getPattern());
2694 break;
Douglas Gregor2e774c42011-01-04 18:56:13 +00002695
2696 case Expr::SizeOfPackExprClass: {
Douglas Gregor2e774c42011-01-04 18:56:13 +00002697 Out << "sZ";
2698 const NamedDecl *Pack = cast<SizeOfPackExpr>(E)->getPack();
2699 if (const TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(Pack))
2700 mangleTemplateParameter(TTP->getIndex());
2701 else if (const NonTypeTemplateParmDecl *NTTP
2702 = dyn_cast<NonTypeTemplateParmDecl>(Pack))
2703 mangleTemplateParameter(NTTP->getIndex());
2704 else if (const TemplateTemplateParmDecl *TempTP
2705 = dyn_cast<TemplateTemplateParmDecl>(Pack))
2706 mangleTemplateParameter(TempTP->getIndex());
Douglas Gregor91832362011-07-12 07:03:48 +00002707 else
2708 mangleFunctionParam(cast<ParmVarDecl>(Pack));
Douglas Gregordfbbcf92011-03-03 02:20:19 +00002709 break;
Douglas Gregor2e774c42011-01-04 18:56:13 +00002710 }
Douglas Gregor03e80032011-06-21 17:03:29 +00002711
2712 case Expr::MaterializeTemporaryExprClass: {
2713 mangleExpression(cast<MaterializeTemporaryExpr>(E)->GetTemporaryExpr());
2714 break;
2715 }
Anders Carlssond553f8c2009-09-21 01:21:10 +00002716 }
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00002717}
2718
John McCallfb44de92011-05-01 22:35:37 +00002719/// Mangle an expression which refers to a parameter variable.
2720///
2721/// <expression> ::= <function-param>
2722/// <function-param> ::= fp <top-level CV-qualifiers> _ # L == 0, I == 0
2723/// <function-param> ::= fp <top-level CV-qualifiers>
2724/// <parameter-2 non-negative number> _ # L == 0, I > 0
2725/// <function-param> ::= fL <L-1 non-negative number>
2726/// p <top-level CV-qualifiers> _ # L > 0, I == 0
2727/// <function-param> ::= fL <L-1 non-negative number>
2728/// p <top-level CV-qualifiers>
2729/// <I-1 non-negative number> _ # L > 0, I > 0
2730///
2731/// L is the nesting depth of the parameter, defined as 1 if the
2732/// parameter comes from the innermost function prototype scope
2733/// enclosing the current context, 2 if from the next enclosing
2734/// function prototype scope, and so on, with one special case: if
2735/// we've processed the full parameter clause for the innermost
2736/// function type, then L is one less. This definition conveniently
2737/// makes it irrelevant whether a function's result type was written
2738/// trailing or leading, but is otherwise overly complicated; the
2739/// numbering was first designed without considering references to
2740/// parameter in locations other than return types, and then the
2741/// mangling had to be generalized without changing the existing
2742/// manglings.
2743///
2744/// I is the zero-based index of the parameter within its parameter
2745/// declaration clause. Note that the original ABI document describes
2746/// this using 1-based ordinals.
2747void CXXNameMangler::mangleFunctionParam(const ParmVarDecl *parm) {
2748 unsigned parmDepth = parm->getFunctionScopeDepth();
2749 unsigned parmIndex = parm->getFunctionScopeIndex();
2750
2751 // Compute 'L'.
2752 // parmDepth does not include the declaring function prototype.
2753 // FunctionTypeDepth does account for that.
2754 assert(parmDepth < FunctionTypeDepth.getDepth());
2755 unsigned nestingDepth = FunctionTypeDepth.getDepth() - parmDepth;
2756 if (FunctionTypeDepth.isInResultType())
2757 nestingDepth--;
2758
2759 if (nestingDepth == 0) {
2760 Out << "fp";
2761 } else {
2762 Out << "fL" << (nestingDepth - 1) << 'p';
2763 }
2764
2765 // Top-level qualifiers. We don't have to worry about arrays here,
2766 // because parameters declared as arrays should already have been
2767 // tranformed to have pointer type. FIXME: apparently these don't
2768 // get mangled if used as an rvalue of a known non-class type?
2769 assert(!parm->getType()->isArrayType()
2770 && "parameter's type is still an array type?");
2771 mangleQualifiers(parm->getType().getQualifiers());
2772
2773 // Parameter index.
2774 if (parmIndex != 0) {
2775 Out << (parmIndex - 1);
2776 }
2777 Out << '_';
2778}
2779
Anders Carlsson3ac86b52009-04-15 05:36:58 +00002780void CXXNameMangler::mangleCXXCtorType(CXXCtorType T) {
2781 // <ctor-dtor-name> ::= C1 # complete object constructor
2782 // ::= C2 # base object constructor
2783 // ::= C3 # complete object allocating constructor
2784 //
2785 switch (T) {
2786 case Ctor_Complete:
2787 Out << "C1";
2788 break;
2789 case Ctor_Base:
2790 Out << "C2";
2791 break;
2792 case Ctor_CompleteAllocating:
2793 Out << "C3";
2794 break;
2795 }
2796}
2797
Anders Carlsson27ae5362009-04-17 01:58:57 +00002798void CXXNameMangler::mangleCXXDtorType(CXXDtorType T) {
2799 // <ctor-dtor-name> ::= D0 # deleting destructor
2800 // ::= D1 # complete object destructor
2801 // ::= D2 # base object destructor
2802 //
2803 switch (T) {
2804 case Dtor_Deleting:
2805 Out << "D0";
2806 break;
2807 case Dtor_Complete:
2808 Out << "D1";
2809 break;
2810 case Dtor_Base:
2811 Out << "D2";
2812 break;
2813 }
2814}
2815
John McCall6dbce192010-08-20 00:17:19 +00002816void CXXNameMangler::mangleTemplateArgs(
Argyrios Kyrtzidisb0c3e092011-09-22 20:07:03 +00002817 const ASTTemplateArgumentListInfo &TemplateArgs) {
John McCall6dbce192010-08-20 00:17:19 +00002818 // <template-args> ::= I <template-arg>+ E
2819 Out << 'I';
John McCall4f4e4132011-05-04 01:45:19 +00002820 for (unsigned i = 0, e = TemplateArgs.NumTemplateArgs; i != e; ++i)
2821 mangleTemplateArg(0, TemplateArgs.getTemplateArgs()[i].getArgument());
John McCall6dbce192010-08-20 00:17:19 +00002822 Out << 'E';
2823}
2824
Douglas Gregor20f0cc72010-04-23 03:10:43 +00002825void CXXNameMangler::mangleTemplateArgs(TemplateName Template,
2826 const TemplateArgument *TemplateArgs,
2827 unsigned NumTemplateArgs) {
2828 if (TemplateDecl *TD = Template.getAsTemplateDecl())
2829 return mangleTemplateArgs(*TD->getTemplateParameters(), TemplateArgs,
2830 NumTemplateArgs);
Sean Huntc3021132010-05-05 15:23:54 +00002831
John McCall4f4e4132011-05-04 01:45:19 +00002832 mangleUnresolvedTemplateArgs(TemplateArgs, NumTemplateArgs);
2833}
2834
2835void CXXNameMangler::mangleUnresolvedTemplateArgs(const TemplateArgument *args,
2836 unsigned numArgs) {
Douglas Gregor20f0cc72010-04-23 03:10:43 +00002837 // <template-args> ::= I <template-arg>+ E
2838 Out << 'I';
John McCall4f4e4132011-05-04 01:45:19 +00002839 for (unsigned i = 0; i != numArgs; ++i)
2840 mangleTemplateArg(0, args[i]);
Douglas Gregor20f0cc72010-04-23 03:10:43 +00002841 Out << 'E';
2842}
2843
Rafael Espindolad9800722010-03-11 14:07:00 +00002844void CXXNameMangler::mangleTemplateArgs(const TemplateParameterList &PL,
2845 const TemplateArgumentList &AL) {
Anders Carlsson7a0ba872009-05-15 16:09:15 +00002846 // <template-args> ::= I <template-arg>+ E
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002847 Out << 'I';
Rafael Espindolad9800722010-03-11 14:07:00 +00002848 for (unsigned i = 0, e = AL.size(); i != e; ++i)
2849 mangleTemplateArg(PL.getParam(i), AL[i]);
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002850 Out << 'E';
Anders Carlsson7a0ba872009-05-15 16:09:15 +00002851}
2852
Rafael Espindolad9800722010-03-11 14:07:00 +00002853void CXXNameMangler::mangleTemplateArgs(const TemplateParameterList &PL,
2854 const TemplateArgument *TemplateArgs,
Anders Carlsson7624f212009-09-18 02:42:01 +00002855 unsigned NumTemplateArgs) {
2856 // <template-args> ::= I <template-arg>+ E
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002857 Out << 'I';
Daniel Dunbar7e0c1952009-11-21 09:17:15 +00002858 for (unsigned i = 0; i != NumTemplateArgs; ++i)
Rafael Espindolad9800722010-03-11 14:07:00 +00002859 mangleTemplateArg(PL.getParam(i), TemplateArgs[i]);
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002860 Out << 'E';
Anders Carlsson7624f212009-09-18 02:42:01 +00002861}
2862
Rafael Espindolad9800722010-03-11 14:07:00 +00002863void CXXNameMangler::mangleTemplateArg(const NamedDecl *P,
Douglas Gregorf1588662011-07-12 15:18:55 +00002864 TemplateArgument A) {
Mike Stump1eb44332009-09-09 15:08:12 +00002865 // <template-arg> ::= <type> # type or template
Anders Carlsson7a0ba872009-05-15 16:09:15 +00002866 // ::= X <expression> E # expression
2867 // ::= <expr-primary> # simple expressions
Douglas Gregor4fc48662011-01-13 16:39:34 +00002868 // ::= J <template-arg>* E # argument pack
Douglas Gregorf1588662011-07-12 15:18:55 +00002869 // ::= sp <expression> # pack expansion of (C++0x)
2870 if (!A.isInstantiationDependent() || A.isDependent())
2871 A = Context.getASTContext().getCanonicalTemplateArgument(A);
2872
Anders Carlsson7a0ba872009-05-15 16:09:15 +00002873 switch (A.getKind()) {
Douglas Gregorf90b27a2011-01-03 22:36:02 +00002874 case TemplateArgument::Null:
2875 llvm_unreachable("Cannot mangle NULL template argument");
2876
Anders Carlsson7a0ba872009-05-15 16:09:15 +00002877 case TemplateArgument::Type:
2878 mangleType(A.getAsType());
2879 break;
Anders Carlsson9e85c742009-12-23 19:30:55 +00002880 case TemplateArgument::Template:
John McCallb6f532e2010-07-14 06:43:17 +00002881 // This is mangled as <type>.
2882 mangleType(A.getAsTemplate());
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002883 break;
Douglas Gregora7fc9012011-01-05 18:58:31 +00002884 case TemplateArgument::TemplateExpansion:
Douglas Gregor4fc48662011-01-13 16:39:34 +00002885 // <type> ::= Dp <type> # pack expansion (C++0x)
Douglas Gregora7fc9012011-01-05 18:58:31 +00002886 Out << "Dp";
2887 mangleType(A.getAsTemplateOrTemplatePattern());
2888 break;
Anders Carlssond553f8c2009-09-21 01:21:10 +00002889 case TemplateArgument::Expression:
2890 Out << 'X';
2891 mangleExpression(A.getAsExpr());
2892 Out << 'E';
2893 break;
Anders Carlssone170ba72009-12-14 01:45:37 +00002894 case TemplateArgument::Integral:
2895 mangleIntegerLiteral(A.getIntegralType(), *A.getAsIntegral());
Anders Carlsson7a0ba872009-05-15 16:09:15 +00002896 break;
Daniel Dunbar7e0c1952009-11-21 09:17:15 +00002897 case TemplateArgument::Declaration: {
Douglas Gregor20f0cc72010-04-23 03:10:43 +00002898 assert(P && "Missing template parameter for declaration argument");
Daniel Dunbar7e0c1952009-11-21 09:17:15 +00002899 // <expr-primary> ::= L <mangled-name> E # external name
2900
Rafael Espindolad9800722010-03-11 14:07:00 +00002901 // Clang produces AST's where pointer-to-member-function expressions
Daniel Dunbar7e0c1952009-11-21 09:17:15 +00002902 // and pointer-to-function expressions are represented as a declaration not
Rafael Espindolad9800722010-03-11 14:07:00 +00002903 // an expression. We compensate for it here to produce the correct mangling.
2904 NamedDecl *D = cast<NamedDecl>(A.getAsDecl());
2905 const NonTypeTemplateParmDecl *Parameter = cast<NonTypeTemplateParmDecl>(P);
John McCallc0a45592011-04-24 08:43:07 +00002906 bool compensateMangling = !Parameter->getType()->isReferenceType();
Rafael Espindolad9800722010-03-11 14:07:00 +00002907 if (compensateMangling) {
2908 Out << 'X';
2909 mangleOperatorName(OO_Amp, 1);
2910 }
2911
Daniel Dunbar7e0c1952009-11-21 09:17:15 +00002912 Out << 'L';
2913 // References to external entities use the mangled name; if the name would
2914 // not normally be manged then mangle it as unqualified.
2915 //
2916 // FIXME: The ABI specifies that external names here should have _Z, but
2917 // gcc leaves this off.
Rafael Espindolad9800722010-03-11 14:07:00 +00002918 if (compensateMangling)
2919 mangle(D, "_Z");
2920 else
2921 mangle(D, "Z");
Daniel Dunbar7e0c1952009-11-21 09:17:15 +00002922 Out << 'E';
Rafael Espindolad9800722010-03-11 14:07:00 +00002923
2924 if (compensateMangling)
2925 Out << 'E';
2926
Daniel Dunbar7e0c1952009-11-21 09:17:15 +00002927 break;
2928 }
Douglas Gregorf90b27a2011-01-03 22:36:02 +00002929
2930 case TemplateArgument::Pack: {
2931 // Note: proposal by Mike Herrick on 12/20/10
2932 Out << 'J';
2933 for (TemplateArgument::pack_iterator PA = A.pack_begin(),
2934 PAEnd = A.pack_end();
2935 PA != PAEnd; ++PA)
2936 mangleTemplateArg(P, *PA);
2937 Out << 'E';
2938 }
Daniel Dunbar7e0c1952009-11-21 09:17:15 +00002939 }
Anders Carlsson7a0ba872009-05-15 16:09:15 +00002940}
2941
Anders Carlsson0ccdf8d2009-09-27 00:38:53 +00002942void CXXNameMangler::mangleTemplateParameter(unsigned Index) {
2943 // <template-param> ::= T_ # first template parameter
2944 // ::= T <parameter-2 non-negative number> _
2945 if (Index == 0)
2946 Out << "T_";
2947 else
2948 Out << 'T' << (Index - 1) << '_';
2949}
2950
John McCall68a51a72011-07-01 00:04:39 +00002951void CXXNameMangler::mangleExistingSubstitution(QualType type) {
2952 bool result = mangleSubstitution(type);
2953 assert(result && "no existing substitution for type");
2954 (void) result;
2955}
2956
2957void CXXNameMangler::mangleExistingSubstitution(TemplateName tname) {
2958 bool result = mangleSubstitution(tname);
2959 assert(result && "no existing substitution for template name");
2960 (void) result;
2961}
2962
Anders Carlsson76967372009-09-17 00:43:46 +00002963// <substitution> ::= S <seq-id> _
2964// ::= S_
Anders Carlsson6862fc72009-09-17 04:16:28 +00002965bool CXXNameMangler::mangleSubstitution(const NamedDecl *ND) {
Anders Carlssone7c8cb62009-09-26 20:53:44 +00002966 // Try one of the standard substitutions first.
2967 if (mangleStandardSubstitution(ND))
2968 return true;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002969
Anders Carlsson433d1372009-11-07 04:26:04 +00002970 ND = cast<NamedDecl>(ND->getCanonicalDecl());
Anders Carlsson6862fc72009-09-17 04:16:28 +00002971 return mangleSubstitution(reinterpret_cast<uintptr_t>(ND));
2972}
2973
Anders Carlsson76967372009-09-17 00:43:46 +00002974bool CXXNameMangler::mangleSubstitution(QualType T) {
Anders Carlssond99edc42009-09-26 03:55:37 +00002975 if (!T.getCVRQualifiers()) {
2976 if (const RecordType *RT = T->getAs<RecordType>())
2977 return mangleSubstitution(RT->getDecl());
2978 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002979
Anders Carlsson76967372009-09-17 00:43:46 +00002980 uintptr_t TypePtr = reinterpret_cast<uintptr_t>(T.getAsOpaquePtr());
2981
Anders Carlssond3a932a2009-09-17 03:53:28 +00002982 return mangleSubstitution(TypePtr);
2983}
2984
Douglas Gregor1e9268e2010-04-28 05:58:56 +00002985bool CXXNameMangler::mangleSubstitution(TemplateName Template) {
2986 if (TemplateDecl *TD = Template.getAsTemplateDecl())
2987 return mangleSubstitution(TD);
Sean Huntc3021132010-05-05 15:23:54 +00002988
Douglas Gregor1e9268e2010-04-28 05:58:56 +00002989 Template = Context.getASTContext().getCanonicalTemplateName(Template);
2990 return mangleSubstitution(
2991 reinterpret_cast<uintptr_t>(Template.getAsVoidPointer()));
2992}
2993
Anders Carlssond3a932a2009-09-17 03:53:28 +00002994bool CXXNameMangler::mangleSubstitution(uintptr_t Ptr) {
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002995 llvm::DenseMap<uintptr_t, unsigned>::iterator I = Substitutions.find(Ptr);
Anders Carlsson76967372009-09-17 00:43:46 +00002996 if (I == Substitutions.end())
2997 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002998
Anders Carlsson76967372009-09-17 00:43:46 +00002999 unsigned SeqID = I->second;
3000 if (SeqID == 0)
3001 Out << "S_";
3002 else {
3003 SeqID--;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00003004
Anders Carlsson76967372009-09-17 00:43:46 +00003005 // <seq-id> is encoded in base-36, using digits and upper case letters.
3006 char Buffer[10];
Benjamin Kramer35f59b62010-04-10 16:03:31 +00003007 char *BufferPtr = llvm::array_endof(Buffer);
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00003008
Anders Carlsson76967372009-09-17 00:43:46 +00003009 if (SeqID == 0) *--BufferPtr = '0';
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00003010
Anders Carlsson76967372009-09-17 00:43:46 +00003011 while (SeqID) {
3012 assert(BufferPtr > Buffer && "Buffer overflow!");
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00003013
John McCall6ab30e02010-06-09 07:26:17 +00003014 char c = static_cast<char>(SeqID % 36);
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00003015
Anders Carlsson76967372009-09-17 00:43:46 +00003016 *--BufferPtr = (c < 10 ? '0' + c : 'A' + c - 10);
3017 SeqID /= 36;
3018 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00003019
Benjamin Kramer35f59b62010-04-10 16:03:31 +00003020 Out << 'S'
Chris Lattner5f9e2722011-07-23 10:55:15 +00003021 << StringRef(BufferPtr, llvm::array_endof(Buffer)-BufferPtr)
Benjamin Kramer35f59b62010-04-10 16:03:31 +00003022 << '_';
Anders Carlsson76967372009-09-17 00:43:46 +00003023 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00003024
Anders Carlsson76967372009-09-17 00:43:46 +00003025 return true;
3026}
3027
Anders Carlssonf514b542009-09-27 00:12:57 +00003028static bool isCharType(QualType T) {
3029 if (T.isNull())
3030 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00003031
Anders Carlssonf514b542009-09-27 00:12:57 +00003032 return T->isSpecificBuiltinType(BuiltinType::Char_S) ||
3033 T->isSpecificBuiltinType(BuiltinType::Char_U);
3034}
3035
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00003036/// isCharSpecialization - Returns whether a given type is a template
Anders Carlssonf514b542009-09-27 00:12:57 +00003037/// specialization of a given name with a single argument of type char.
3038static bool isCharSpecialization(QualType T, const char *Name) {
3039 if (T.isNull())
3040 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00003041
Anders Carlssonf514b542009-09-27 00:12:57 +00003042 const RecordType *RT = T->getAs<RecordType>();
3043 if (!RT)
3044 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00003045
3046 const ClassTemplateSpecializationDecl *SD =
Anders Carlssonf514b542009-09-27 00:12:57 +00003047 dyn_cast<ClassTemplateSpecializationDecl>(RT->getDecl());
3048 if (!SD)
3049 return false;
3050
3051 if (!isStdNamespace(SD->getDeclContext()))
3052 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00003053
Anders Carlssonf514b542009-09-27 00:12:57 +00003054 const TemplateArgumentList &TemplateArgs = SD->getTemplateArgs();
3055 if (TemplateArgs.size() != 1)
3056 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00003057
Anders Carlssonf514b542009-09-27 00:12:57 +00003058 if (!isCharType(TemplateArgs[0].getAsType()))
3059 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00003060
Daniel Dunbar01eb9b92009-10-18 21:17:35 +00003061 return SD->getIdentifier()->getName() == Name;
Anders Carlssonf514b542009-09-27 00:12:57 +00003062}
3063
Anders Carlsson91f88602009-12-07 19:56:42 +00003064template <std::size_t StrLen>
Benjamin Kramer54353f42010-11-25 18:29:30 +00003065static bool isStreamCharSpecialization(const ClassTemplateSpecializationDecl*SD,
3066 const char (&Str)[StrLen]) {
Anders Carlsson91f88602009-12-07 19:56:42 +00003067 if (!SD->getIdentifier()->isStr(Str))
3068 return false;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00003069
Anders Carlsson91f88602009-12-07 19:56:42 +00003070 const TemplateArgumentList &TemplateArgs = SD->getTemplateArgs();
3071 if (TemplateArgs.size() != 2)
3072 return false;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00003073
Anders Carlsson91f88602009-12-07 19:56:42 +00003074 if (!isCharType(TemplateArgs[0].getAsType()))
3075 return false;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00003076
Anders Carlsson91f88602009-12-07 19:56:42 +00003077 if (!isCharSpecialization(TemplateArgs[1].getAsType(), "char_traits"))
3078 return false;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00003079
Anders Carlsson91f88602009-12-07 19:56:42 +00003080 return true;
3081}
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00003082
Anders Carlssone7c8cb62009-09-26 20:53:44 +00003083bool CXXNameMangler::mangleStandardSubstitution(const NamedDecl *ND) {
3084 // <substitution> ::= St # ::std::
Anders Carlsson8c031552009-09-26 23:10:05 +00003085 if (const NamespaceDecl *NS = dyn_cast<NamespaceDecl>(ND)) {
Anders Carlsson47846d22009-12-04 06:23:23 +00003086 if (isStd(NS)) {
Anders Carlsson8c031552009-09-26 23:10:05 +00003087 Out << "St";
3088 return true;
3089 }
3090 }
3091
3092 if (const ClassTemplateDecl *TD = dyn_cast<ClassTemplateDecl>(ND)) {
3093 if (!isStdNamespace(TD->getDeclContext()))
3094 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00003095
Anders Carlsson8c031552009-09-26 23:10:05 +00003096 // <substitution> ::= Sa # ::std::allocator
3097 if (TD->getIdentifier()->isStr("allocator")) {
3098 Out << "Sa";
3099 return true;
3100 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00003101
Anders Carlsson189d59c2009-09-26 23:14:39 +00003102 // <<substitution> ::= Sb # ::std::basic_string
3103 if (TD->getIdentifier()->isStr("basic_string")) {
3104 Out << "Sb";
3105 return true;
3106 }
Anders Carlsson8c031552009-09-26 23:10:05 +00003107 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00003108
3109 if (const ClassTemplateSpecializationDecl *SD =
Anders Carlssonf514b542009-09-27 00:12:57 +00003110 dyn_cast<ClassTemplateSpecializationDecl>(ND)) {
Eli Friedman5370ee22010-02-23 18:25:09 +00003111 if (!isStdNamespace(SD->getDeclContext()))
3112 return false;
3113
Anders Carlssonf514b542009-09-27 00:12:57 +00003114 // <substitution> ::= Ss # ::std::basic_string<char,
3115 // ::std::char_traits<char>,
3116 // ::std::allocator<char> >
3117 if (SD->getIdentifier()->isStr("basic_string")) {
3118 const TemplateArgumentList &TemplateArgs = SD->getTemplateArgs();
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00003119
Anders Carlssonf514b542009-09-27 00:12:57 +00003120 if (TemplateArgs.size() != 3)
3121 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00003122
Anders Carlssonf514b542009-09-27 00:12:57 +00003123 if (!isCharType(TemplateArgs[0].getAsType()))
3124 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00003125
Anders Carlssonf514b542009-09-27 00:12:57 +00003126 if (!isCharSpecialization(TemplateArgs[1].getAsType(), "char_traits"))
3127 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00003128
Anders Carlssonf514b542009-09-27 00:12:57 +00003129 if (!isCharSpecialization(TemplateArgs[2].getAsType(), "allocator"))
3130 return false;
3131
3132 Out << "Ss";
3133 return true;
3134 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00003135
Anders Carlsson91f88602009-12-07 19:56:42 +00003136 // <substitution> ::= Si # ::std::basic_istream<char,
3137 // ::std::char_traits<char> >
3138 if (isStreamCharSpecialization(SD, "basic_istream")) {
3139 Out << "Si";
3140 return true;
3141 }
3142
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00003143 // <substitution> ::= So # ::std::basic_ostream<char,
Anders Carlsson8f8fd8e2009-10-08 17:20:26 +00003144 // ::std::char_traits<char> >
Anders Carlsson91f88602009-12-07 19:56:42 +00003145 if (isStreamCharSpecialization(SD, "basic_ostream")) {
Anders Carlsson8f8fd8e2009-10-08 17:20:26 +00003146 Out << "So";
3147 return true;
3148 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00003149
Anders Carlsson91f88602009-12-07 19:56:42 +00003150 // <substitution> ::= Sd # ::std::basic_iostream<char,
3151 // ::std::char_traits<char> >
3152 if (isStreamCharSpecialization(SD, "basic_iostream")) {
3153 Out << "Sd";
3154 return true;
3155 }
Anders Carlssonf514b542009-09-27 00:12:57 +00003156 }
Anders Carlsson8c031552009-09-26 23:10:05 +00003157 return false;
Anders Carlssone7c8cb62009-09-26 20:53:44 +00003158}
3159
Anders Carlsson76967372009-09-17 00:43:46 +00003160void CXXNameMangler::addSubstitution(QualType T) {
Anders Carlssond99edc42009-09-26 03:55:37 +00003161 if (!T.getCVRQualifiers()) {
3162 if (const RecordType *RT = T->getAs<RecordType>()) {
3163 addSubstitution(RT->getDecl());
3164 return;
3165 }
3166 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00003167
Anders Carlsson76967372009-09-17 00:43:46 +00003168 uintptr_t TypePtr = reinterpret_cast<uintptr_t>(T.getAsOpaquePtr());
Anders Carlssond3a932a2009-09-17 03:53:28 +00003169 addSubstitution(TypePtr);
3170}
3171
Douglas Gregor1e9268e2010-04-28 05:58:56 +00003172void CXXNameMangler::addSubstitution(TemplateName Template) {
3173 if (TemplateDecl *TD = Template.getAsTemplateDecl())
3174 return addSubstitution(TD);
Sean Huntc3021132010-05-05 15:23:54 +00003175
Douglas Gregor1e9268e2010-04-28 05:58:56 +00003176 Template = Context.getASTContext().getCanonicalTemplateName(Template);
3177 addSubstitution(reinterpret_cast<uintptr_t>(Template.getAsVoidPointer()));
3178}
3179
Anders Carlssond3a932a2009-09-17 03:53:28 +00003180void CXXNameMangler::addSubstitution(uintptr_t Ptr) {
Anders Carlssond3a932a2009-09-17 03:53:28 +00003181 assert(!Substitutions.count(Ptr) && "Substitution already exists!");
Anders Carlsson9d85b722010-06-02 04:29:50 +00003182 Substitutions[Ptr] = SeqID++;
Anders Carlsson76967372009-09-17 00:43:46 +00003183}
3184
Daniel Dunbar1b077112009-11-21 09:06:10 +00003185//
Mike Stump1eb44332009-09-09 15:08:12 +00003186
Daniel Dunbar1b077112009-11-21 09:06:10 +00003187/// \brief Mangles the name of the declaration D and emits that name to the
3188/// given output stream.
3189///
3190/// If the declaration D requires a mangled name, this routine will emit that
3191/// mangled name to \p os and return true. Otherwise, \p os will be unchanged
3192/// and this routine will return false. In this case, the caller should just
3193/// emit the identifier of the declaration (\c D->getIdentifier()) as its
3194/// name.
Peter Collingbourne14110472011-01-13 18:57:25 +00003195void ItaniumMangleContext::mangleName(const NamedDecl *D,
Chris Lattner5f9e2722011-07-23 10:55:15 +00003196 raw_ostream &Out) {
Daniel Dunbarc02ab4c2009-11-21 09:14:44 +00003197 assert((isa<FunctionDecl>(D) || isa<VarDecl>(D)) &&
3198 "Invalid mangleName() call, argument is not a variable or function!");
3199 assert(!isa<CXXConstructorDecl>(D) && !isa<CXXDestructorDecl>(D) &&
3200 "Invalid mangleName() call on 'structor decl!");
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00003201
Daniel Dunbar1b077112009-11-21 09:06:10 +00003202 PrettyStackTraceDecl CrashInfo(D, SourceLocation(),
3203 getASTContext().getSourceManager(),
3204 "Mangling declaration");
Mike Stump1eb44332009-09-09 15:08:12 +00003205
John McCallfb44de92011-05-01 22:35:37 +00003206 CXXNameMangler Mangler(*this, Out, D);
Daniel Dunbar94fd26d2009-11-21 09:06:22 +00003207 return Mangler.mangle(D);
Daniel Dunbar1b077112009-11-21 09:06:10 +00003208}
Mike Stump1eb44332009-09-09 15:08:12 +00003209
Peter Collingbourne14110472011-01-13 18:57:25 +00003210void ItaniumMangleContext::mangleCXXCtor(const CXXConstructorDecl *D,
3211 CXXCtorType Type,
Chris Lattner5f9e2722011-07-23 10:55:15 +00003212 raw_ostream &Out) {
Rafael Espindolac4850c22011-02-10 23:59:36 +00003213 CXXNameMangler Mangler(*this, Out, D, Type);
Daniel Dunbar77939c92009-11-21 09:06:31 +00003214 Mangler.mangle(D);
Daniel Dunbar1b077112009-11-21 09:06:10 +00003215}
Mike Stump1eb44332009-09-09 15:08:12 +00003216
Peter Collingbourne14110472011-01-13 18:57:25 +00003217void ItaniumMangleContext::mangleCXXDtor(const CXXDestructorDecl *D,
3218 CXXDtorType Type,
Chris Lattner5f9e2722011-07-23 10:55:15 +00003219 raw_ostream &Out) {
Rafael Espindolac4850c22011-02-10 23:59:36 +00003220 CXXNameMangler Mangler(*this, Out, D, Type);
Daniel Dunbar77939c92009-11-21 09:06:31 +00003221 Mangler.mangle(D);
Daniel Dunbar1b077112009-11-21 09:06:10 +00003222}
Mike Stumpf1216772009-07-31 18:25:34 +00003223
Peter Collingbourne14110472011-01-13 18:57:25 +00003224void ItaniumMangleContext::mangleThunk(const CXXMethodDecl *MD,
3225 const ThunkInfo &Thunk,
Chris Lattner5f9e2722011-07-23 10:55:15 +00003226 raw_ostream &Out) {
Anders Carlsson19879c92010-03-23 17:17:29 +00003227 // <special-name> ::= T <call-offset> <base encoding>
3228 // # base is the nominal target function of thunk
3229 // <special-name> ::= Tc <call-offset> <call-offset> <base encoding>
3230 // # base is the nominal target function of thunk
3231 // # first call-offset is 'this' adjustment
3232 // # second call-offset is result adjustment
Sean Huntc3021132010-05-05 15:23:54 +00003233
Anders Carlsson19879c92010-03-23 17:17:29 +00003234 assert(!isa<CXXDestructorDecl>(MD) &&
3235 "Use mangleCXXDtor for destructor decls!");
Rafael Espindolac4850c22011-02-10 23:59:36 +00003236 CXXNameMangler Mangler(*this, Out);
Anders Carlsson19879c92010-03-23 17:17:29 +00003237 Mangler.getStream() << "_ZT";
3238 if (!Thunk.Return.isEmpty())
3239 Mangler.getStream() << 'c';
Sean Huntc3021132010-05-05 15:23:54 +00003240
Anders Carlsson19879c92010-03-23 17:17:29 +00003241 // Mangle the 'this' pointer adjustment.
3242 Mangler.mangleCallOffset(Thunk.This.NonVirtual, Thunk.This.VCallOffsetOffset);
Sean Huntc3021132010-05-05 15:23:54 +00003243
Anders Carlsson19879c92010-03-23 17:17:29 +00003244 // Mangle the return pointer adjustment if there is one.
3245 if (!Thunk.Return.isEmpty())
3246 Mangler.mangleCallOffset(Thunk.Return.NonVirtual,
3247 Thunk.Return.VBaseOffsetOffset);
Sean Huntc3021132010-05-05 15:23:54 +00003248
Anders Carlsson19879c92010-03-23 17:17:29 +00003249 Mangler.mangleFunctionEncoding(MD);
3250}
3251
Sean Huntc3021132010-05-05 15:23:54 +00003252void
Peter Collingbourne14110472011-01-13 18:57:25 +00003253ItaniumMangleContext::mangleCXXDtorThunk(const CXXDestructorDecl *DD,
3254 CXXDtorType Type,
3255 const ThisAdjustment &ThisAdjustment,
Chris Lattner5f9e2722011-07-23 10:55:15 +00003256 raw_ostream &Out) {
Anders Carlsson19879c92010-03-23 17:17:29 +00003257 // <special-name> ::= T <call-offset> <base encoding>
3258 // # base is the nominal target function of thunk
Rafael Espindolac4850c22011-02-10 23:59:36 +00003259 CXXNameMangler Mangler(*this, Out, DD, Type);
Anders Carlsson19879c92010-03-23 17:17:29 +00003260 Mangler.getStream() << "_ZT";
3261
3262 // Mangle the 'this' pointer adjustment.
Sean Huntc3021132010-05-05 15:23:54 +00003263 Mangler.mangleCallOffset(ThisAdjustment.NonVirtual,
Anders Carlsson19879c92010-03-23 17:17:29 +00003264 ThisAdjustment.VCallOffsetOffset);
3265
3266 Mangler.mangleFunctionEncoding(DD);
3267}
3268
Daniel Dunbarc0747712009-11-21 09:12:13 +00003269/// mangleGuardVariable - Returns the mangled name for a guard variable
3270/// for the passed in VarDecl.
Peter Collingbourne14110472011-01-13 18:57:25 +00003271void ItaniumMangleContext::mangleItaniumGuardVariable(const VarDecl *D,
Chris Lattner5f9e2722011-07-23 10:55:15 +00003272 raw_ostream &Out) {
Daniel Dunbarc0747712009-11-21 09:12:13 +00003273 // <special-name> ::= GV <object name> # Guard variable for one-time
3274 // # initialization
Rafael Espindolac4850c22011-02-10 23:59:36 +00003275 CXXNameMangler Mangler(*this, Out);
Daniel Dunbarc0747712009-11-21 09:12:13 +00003276 Mangler.getStream() << "_ZGV";
3277 Mangler.mangleName(D);
3278}
3279
Peter Collingbourne14110472011-01-13 18:57:25 +00003280void ItaniumMangleContext::mangleReferenceTemporary(const VarDecl *D,
Chris Lattner5f9e2722011-07-23 10:55:15 +00003281 raw_ostream &Out) {
Anders Carlsson715edf22010-06-26 16:09:40 +00003282 // We match the GCC mangling here.
3283 // <special-name> ::= GR <object name>
Rafael Espindolac4850c22011-02-10 23:59:36 +00003284 CXXNameMangler Mangler(*this, Out);
Anders Carlsson715edf22010-06-26 16:09:40 +00003285 Mangler.getStream() << "_ZGR";
3286 Mangler.mangleName(D);
3287}
3288
Peter Collingbourne14110472011-01-13 18:57:25 +00003289void ItaniumMangleContext::mangleCXXVTable(const CXXRecordDecl *RD,
Chris Lattner5f9e2722011-07-23 10:55:15 +00003290 raw_ostream &Out) {
Daniel Dunbarc0747712009-11-21 09:12:13 +00003291 // <special-name> ::= TV <type> # virtual table
Rafael Espindolac4850c22011-02-10 23:59:36 +00003292 CXXNameMangler Mangler(*this, Out);
Daniel Dunbarc0747712009-11-21 09:12:13 +00003293 Mangler.getStream() << "_ZTV";
Douglas Gregor1b12a3b2010-05-26 05:11:13 +00003294 Mangler.mangleNameOrStandardSubstitution(RD);
Daniel Dunbar1b077112009-11-21 09:06:10 +00003295}
Mike Stump82d75b02009-11-10 01:58:37 +00003296
Peter Collingbourne14110472011-01-13 18:57:25 +00003297void ItaniumMangleContext::mangleCXXVTT(const CXXRecordDecl *RD,
Chris Lattner5f9e2722011-07-23 10:55:15 +00003298 raw_ostream &Out) {
Daniel Dunbarc0747712009-11-21 09:12:13 +00003299 // <special-name> ::= TT <type> # VTT structure
Rafael Espindolac4850c22011-02-10 23:59:36 +00003300 CXXNameMangler Mangler(*this, Out);
Daniel Dunbarc0747712009-11-21 09:12:13 +00003301 Mangler.getStream() << "_ZTT";
Douglas Gregor1b12a3b2010-05-26 05:11:13 +00003302 Mangler.mangleNameOrStandardSubstitution(RD);
Daniel Dunbar1b077112009-11-21 09:06:10 +00003303}
Mike Stumpab3f7e92009-11-10 01:41:59 +00003304
Peter Collingbourne14110472011-01-13 18:57:25 +00003305void ItaniumMangleContext::mangleCXXCtorVTable(const CXXRecordDecl *RD,
3306 int64_t Offset,
3307 const CXXRecordDecl *Type,
Chris Lattner5f9e2722011-07-23 10:55:15 +00003308 raw_ostream &Out) {
Daniel Dunbarc0747712009-11-21 09:12:13 +00003309 // <special-name> ::= TC <type> <offset number> _ <base type>
Rafael Espindolac4850c22011-02-10 23:59:36 +00003310 CXXNameMangler Mangler(*this, Out);
Daniel Dunbarc0747712009-11-21 09:12:13 +00003311 Mangler.getStream() << "_ZTC";
Douglas Gregor1b12a3b2010-05-26 05:11:13 +00003312 Mangler.mangleNameOrStandardSubstitution(RD);
Daniel Dunbarc0747712009-11-21 09:12:13 +00003313 Mangler.getStream() << Offset;
Benjamin Kramer35f59b62010-04-10 16:03:31 +00003314 Mangler.getStream() << '_';
Douglas Gregor1b12a3b2010-05-26 05:11:13 +00003315 Mangler.mangleNameOrStandardSubstitution(Type);
Daniel Dunbar1b077112009-11-21 09:06:10 +00003316}
Mike Stump738f8c22009-07-31 23:15:31 +00003317
Peter Collingbourne14110472011-01-13 18:57:25 +00003318void ItaniumMangleContext::mangleCXXRTTI(QualType Ty,
Chris Lattner5f9e2722011-07-23 10:55:15 +00003319 raw_ostream &Out) {
Daniel Dunbarc0747712009-11-21 09:12:13 +00003320 // <special-name> ::= TI <type> # typeinfo structure
Douglas Gregor154fe982009-12-23 22:04:40 +00003321 assert(!Ty.hasQualifiers() && "RTTI info cannot have top-level qualifiers");
Rafael Espindolac4850c22011-02-10 23:59:36 +00003322 CXXNameMangler Mangler(*this, Out);
Daniel Dunbarc0747712009-11-21 09:12:13 +00003323 Mangler.getStream() << "_ZTI";
3324 Mangler.mangleType(Ty);
Daniel Dunbar1b077112009-11-21 09:06:10 +00003325}
Mike Stump67795982009-11-14 00:14:13 +00003326
Peter Collingbourne14110472011-01-13 18:57:25 +00003327void ItaniumMangleContext::mangleCXXRTTIName(QualType Ty,
Chris Lattner5f9e2722011-07-23 10:55:15 +00003328 raw_ostream &Out) {
Daniel Dunbarc0747712009-11-21 09:12:13 +00003329 // <special-name> ::= TS <type> # typeinfo name (null terminated byte string)
Rafael Espindolac4850c22011-02-10 23:59:36 +00003330 CXXNameMangler Mangler(*this, Out);
Daniel Dunbarc0747712009-11-21 09:12:13 +00003331 Mangler.getStream() << "_ZTS";
3332 Mangler.mangleType(Ty);
Mike Stumpf1216772009-07-31 18:25:34 +00003333}
Peter Collingbourne14110472011-01-13 18:57:25 +00003334
3335MangleContext *clang::createItaniumMangleContext(ASTContext &Context,
3336 Diagnostic &Diags) {
3337 return new ItaniumMangleContext(Context, Diags);
3338}