blob: 4843716909fb237eb57be57a744432c87c941b1e [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,
David Blaikied6471f72011-09-25 23:23:43 +000076 DiagnosticsEngine &Diags)
Peter Collingbourne14110472011-01-13 18:57:25 +000077 : 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.
John McCall0c8731a2012-01-30 18:36:31 +0000613 // The 'without leading zeroes' thing seems to be an editorial
614 // mistake; see the discussion on cxx-abi-dev beginning on
615 // 2012-01-16.
John McCall1b600522011-04-24 03:07:16 +0000616
John McCall0c8731a2012-01-30 18:36:31 +0000617 // Our requirements here are just barely wierd enough to justify
618 // using a custom algorithm instead of post-processing APInt::toString().
John McCall1b600522011-04-24 03:07:16 +0000619
John McCall0c8731a2012-01-30 18:36:31 +0000620 llvm::APInt valueBits = f.bitcastToAPInt();
621 unsigned numCharacters = (valueBits.getBitWidth() + 3) / 4;
622 assert(numCharacters != 0);
623
624 // Allocate a buffer of the right number of characters.
625 llvm::SmallVector<char, 20> buffer;
626 buffer.set_size(numCharacters);
627
628 // Fill the buffer left-to-right.
629 for (unsigned stringIndex = 0; stringIndex != numCharacters; ++stringIndex) {
630 // The bit-index of the next hex digit.
631 unsigned digitBitIndex = 4 * (numCharacters - stringIndex - 1);
632
633 // Project out 4 bits starting at 'digitIndex'.
634 llvm::integerPart hexDigit
635 = valueBits.getRawData()[digitBitIndex / llvm::integerPartWidth];
636 hexDigit >>= (digitBitIndex % llvm::integerPartWidth);
637 hexDigit &= 0xF;
638
639 // Map that over to a lowercase hex digit.
640 static const char charForHex[16] = {
641 '0', '1', '2', '3', '4', '5', '6', '7',
642 '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'
643 };
644 buffer[stringIndex] = charForHex[hexDigit];
645 }
646
647 Out.write(buffer.data(), numCharacters);
John McCall0512e482010-07-14 04:20:34 +0000648}
649
650void CXXNameMangler::mangleNumber(const llvm::APSInt &Value) {
651 if (Value.isSigned() && Value.isNegative()) {
652 Out << 'n';
653 Value.abs().print(Out, true);
654 } else
655 Value.print(Out, Value.isSigned());
656}
657
Anders Carlssona94822e2009-11-26 02:32:05 +0000658void CXXNameMangler::mangleNumber(int64_t Number) {
659 // <number> ::= [n] <non-negative decimal integer>
660 if (Number < 0) {
661 Out << 'n';
662 Number = -Number;
663 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000664
Anders Carlssona94822e2009-11-26 02:32:05 +0000665 Out << Number;
666}
667
Anders Carlsson19879c92010-03-23 17:17:29 +0000668void CXXNameMangler::mangleCallOffset(int64_t NonVirtual, int64_t Virtual) {
Mike Stump141c5af2009-09-02 00:25:38 +0000669 // <call-offset> ::= h <nv-offset> _
670 // ::= v <v-offset> _
671 // <nv-offset> ::= <offset number> # non-virtual base override
Anders Carlssona94822e2009-11-26 02:32:05 +0000672 // <v-offset> ::= <offset number> _ <virtual offset number>
Mike Stump141c5af2009-09-02 00:25:38 +0000673 // # virtual base override, with vcall offset
Anders Carlsson19879c92010-03-23 17:17:29 +0000674 if (!Virtual) {
Anders Carlssona94822e2009-11-26 02:32:05 +0000675 Out << 'h';
Anders Carlsson19879c92010-03-23 17:17:29 +0000676 mangleNumber(NonVirtual);
Anders Carlssona94822e2009-11-26 02:32:05 +0000677 Out << '_';
678 return;
Mike Stump141c5af2009-09-02 00:25:38 +0000679 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000680
Anders Carlssona94822e2009-11-26 02:32:05 +0000681 Out << 'v';
Anders Carlsson19879c92010-03-23 17:17:29 +0000682 mangleNumber(NonVirtual);
Anders Carlssona94822e2009-11-26 02:32:05 +0000683 Out << '_';
Anders Carlsson19879c92010-03-23 17:17:29 +0000684 mangleNumber(Virtual);
Anders Carlssona94822e2009-11-26 02:32:05 +0000685 Out << '_';
Mike Stump9124bcc2009-09-02 00:56:18 +0000686}
687
John McCall4f4e4132011-05-04 01:45:19 +0000688void CXXNameMangler::manglePrefix(QualType type) {
John McCalla0ce15c2011-04-24 08:23:24 +0000689 if (const TemplateSpecializationType *TST =
690 type->getAs<TemplateSpecializationType>()) {
691 if (!mangleSubstitution(QualType(TST, 0))) {
692 mangleTemplatePrefix(TST->getTemplateName());
Sean Huntc3021132010-05-05 15:23:54 +0000693
Douglas Gregoraa2187d2011-02-28 00:04:36 +0000694 // FIXME: GCC does not appear to mangle the template arguments when
695 // the template in question is a dependent template name. Should we
696 // emulate that badness?
John McCalla0ce15c2011-04-24 08:23:24 +0000697 mangleTemplateArgs(TST->getTemplateName(), TST->getArgs(),
698 TST->getNumArgs());
699 addSubstitution(QualType(TST, 0));
Rafael Espindola9b35b252010-03-17 04:28:11 +0000700 }
John McCalla0ce15c2011-04-24 08:23:24 +0000701 } else if (const DependentTemplateSpecializationType *DTST
702 = type->getAs<DependentTemplateSpecializationType>()) {
703 TemplateName Template
704 = getASTContext().getDependentTemplateName(DTST->getQualifier(),
705 DTST->getIdentifier());
706 mangleTemplatePrefix(Template);
707
708 // FIXME: GCC does not appear to mangle the template arguments when
709 // the template in question is a dependent template name. Should we
710 // emulate that badness?
711 mangleTemplateArgs(Template, DTST->getArgs(), DTST->getNumArgs());
712 } else {
713 // We use the QualType mangle type variant here because it handles
714 // substitutions.
715 mangleType(type);
John McCall1dd73832010-02-04 01:42:13 +0000716 }
717}
718
John McCalla0ce15c2011-04-24 08:23:24 +0000719/// Mangle everything prior to the base-unresolved-name in an unresolved-name.
720///
721/// \param firstQualifierLookup - the entity found by unqualified lookup
722/// for the first name in the qualifier, if this is for a member expression
723/// \param recursive - true if this is being called recursively,
724/// i.e. if there is more prefix "to the right".
725void CXXNameMangler::mangleUnresolvedPrefix(NestedNameSpecifier *qualifier,
726 NamedDecl *firstQualifierLookup,
727 bool recursive) {
John McCall1dd73832010-02-04 01:42:13 +0000728
John McCalla0ce15c2011-04-24 08:23:24 +0000729 // x, ::x
730 // <unresolved-name> ::= [gs] <base-unresolved-name>
731
732 // T::x / decltype(p)::x
733 // <unresolved-name> ::= sr <unresolved-type> <base-unresolved-name>
734
735 // T::N::x /decltype(p)::N::x
736 // <unresolved-name> ::= srN <unresolved-type> <unresolved-qualifier-level>+ E
737 // <base-unresolved-name>
738
739 // A::x, N::y, A<T>::z; "gs" means leading "::"
740 // <unresolved-name> ::= [gs] sr <unresolved-qualifier-level>+ E
741 // <base-unresolved-name>
742
743 switch (qualifier->getKind()) {
744 case NestedNameSpecifier::Global:
745 Out << "gs";
746
747 // We want an 'sr' unless this is the entire NNS.
748 if (recursive)
749 Out << "sr";
750
751 // We never want an 'E' here.
752 return;
753
754 case NestedNameSpecifier::Namespace:
755 if (qualifier->getPrefix())
756 mangleUnresolvedPrefix(qualifier->getPrefix(), firstQualifierLookup,
757 /*recursive*/ true);
758 else
759 Out << "sr";
760 mangleSourceName(qualifier->getAsNamespace()->getIdentifier());
761 break;
762 case NestedNameSpecifier::NamespaceAlias:
763 if (qualifier->getPrefix())
764 mangleUnresolvedPrefix(qualifier->getPrefix(), firstQualifierLookup,
765 /*recursive*/ true);
766 else
767 Out << "sr";
768 mangleSourceName(qualifier->getAsNamespaceAlias()->getIdentifier());
769 break;
770
771 case NestedNameSpecifier::TypeSpec:
772 case NestedNameSpecifier::TypeSpecWithTemplate: {
John McCall4f4e4132011-05-04 01:45:19 +0000773 const Type *type = qualifier->getAsType();
John McCalla0ce15c2011-04-24 08:23:24 +0000774
John McCall4f4e4132011-05-04 01:45:19 +0000775 // We only want to use an unresolved-type encoding if this is one of:
776 // - a decltype
777 // - a template type parameter
778 // - a template template parameter with arguments
779 // In all of these cases, we should have no prefix.
780 if (qualifier->getPrefix()) {
781 mangleUnresolvedPrefix(qualifier->getPrefix(), firstQualifierLookup,
782 /*recursive*/ true);
783 } else {
784 // Otherwise, all the cases want this.
785 Out << "sr";
John McCall4f4e4132011-05-04 01:45:19 +0000786 }
787
John McCall4f4e4132011-05-04 01:45:19 +0000788 // Only certain other types are valid as prefixes; enumerate them.
John McCalld3d49bb2011-06-28 16:49:23 +0000789 switch (type->getTypeClass()) {
790 case Type::Builtin:
791 case Type::Complex:
792 case Type::Pointer:
793 case Type::BlockPointer:
794 case Type::LValueReference:
795 case Type::RValueReference:
796 case Type::MemberPointer:
797 case Type::ConstantArray:
798 case Type::IncompleteArray:
799 case Type::VariableArray:
800 case Type::DependentSizedArray:
801 case Type::DependentSizedExtVector:
802 case Type::Vector:
803 case Type::ExtVector:
804 case Type::FunctionProto:
805 case Type::FunctionNoProto:
806 case Type::Enum:
807 case Type::Paren:
808 case Type::Elaborated:
809 case Type::Attributed:
810 case Type::Auto:
811 case Type::PackExpansion:
John McCalld3d49bb2011-06-28 16:49:23 +0000812 case Type::ObjCObject:
813 case Type::ObjCInterface:
814 case Type::ObjCObjectPointer:
Eli Friedmanb001de72011-10-06 23:00:33 +0000815 case Type::Atomic:
John McCalld3d49bb2011-06-28 16:49:23 +0000816 llvm_unreachable("type is illegal as a nested name specifier");
817
John McCall68a51a72011-07-01 00:04:39 +0000818 case Type::SubstTemplateTypeParmPack:
819 // FIXME: not clear how to mangle this!
820 // template <class T...> class A {
821 // template <class U...> void foo(decltype(T::foo(U())) x...);
822 // };
823 Out << "_SUBSTPACK_";
824 break;
825
John McCalld3d49bb2011-06-28 16:49:23 +0000826 // <unresolved-type> ::= <template-param>
827 // ::= <decltype>
828 // ::= <template-template-param> <template-args>
829 // (this last is not official yet)
830 case Type::TypeOfExpr:
831 case Type::TypeOf:
832 case Type::Decltype:
833 case Type::TemplateTypeParm:
834 case Type::UnaryTransform:
John McCall35ee32e2011-07-01 02:19:08 +0000835 case Type::SubstTemplateTypeParm:
John McCalld3d49bb2011-06-28 16:49:23 +0000836 unresolvedType:
837 assert(!qualifier->getPrefix());
838
839 // We only get here recursively if we're followed by identifiers.
840 if (recursive) Out << 'N';
841
John McCall35ee32e2011-07-01 02:19:08 +0000842 // This seems to do everything we want. It's not really
843 // sanctioned for a substituted template parameter, though.
John McCalld3d49bb2011-06-28 16:49:23 +0000844 mangleType(QualType(type, 0));
845
846 // We never want to print 'E' directly after an unresolved-type,
847 // so we return directly.
848 return;
849
John McCalld3d49bb2011-06-28 16:49:23 +0000850 case Type::Typedef:
851 mangleSourceName(cast<TypedefType>(type)->getDecl()->getIdentifier());
852 break;
853
854 case Type::UnresolvedUsing:
855 mangleSourceName(cast<UnresolvedUsingType>(type)->getDecl()
856 ->getIdentifier());
857 break;
858
859 case Type::Record:
860 mangleSourceName(cast<RecordType>(type)->getDecl()->getIdentifier());
861 break;
862
863 case Type::TemplateSpecialization: {
864 const TemplateSpecializationType *tst
865 = cast<TemplateSpecializationType>(type);
John McCall68a51a72011-07-01 00:04:39 +0000866 TemplateName name = tst->getTemplateName();
867 switch (name.getKind()) {
868 case TemplateName::Template:
869 case TemplateName::QualifiedTemplate: {
870 TemplateDecl *temp = name.getAsTemplateDecl();
John McCalld3d49bb2011-06-28 16:49:23 +0000871
John McCall68a51a72011-07-01 00:04:39 +0000872 // If the base is a template template parameter, this is an
873 // unresolved type.
874 assert(temp && "no template for template specialization type");
875 if (isa<TemplateTemplateParmDecl>(temp)) goto unresolvedType;
John McCalld3d49bb2011-06-28 16:49:23 +0000876
John McCall68a51a72011-07-01 00:04:39 +0000877 mangleSourceName(temp->getIdentifier());
878 break;
879 }
880
881 case TemplateName::OverloadedTemplate:
882 case TemplateName::DependentTemplate:
883 llvm_unreachable("invalid base for a template specialization type");
884
885 case TemplateName::SubstTemplateTemplateParm: {
886 SubstTemplateTemplateParmStorage *subst
887 = name.getAsSubstTemplateTemplateParm();
888 mangleExistingSubstitution(subst->getReplacement());
889 break;
890 }
891
892 case TemplateName::SubstTemplateTemplateParmPack: {
893 // FIXME: not clear how to mangle this!
894 // template <template <class U> class T...> class A {
895 // template <class U...> void foo(decltype(T<U>::foo) x...);
896 // };
897 Out << "_SUBSTPACK_";
898 break;
899 }
900 }
901
John McCall4f4e4132011-05-04 01:45:19 +0000902 mangleUnresolvedTemplateArgs(tst->getArgs(), tst->getNumArgs());
John McCalld3d49bb2011-06-28 16:49:23 +0000903 break;
904 }
905
906 case Type::InjectedClassName:
907 mangleSourceName(cast<InjectedClassNameType>(type)->getDecl()
908 ->getIdentifier());
909 break;
910
911 case Type::DependentName:
912 mangleSourceName(cast<DependentNameType>(type)->getIdentifier());
913 break;
914
915 case Type::DependentTemplateSpecialization: {
916 const DependentTemplateSpecializationType *tst
917 = cast<DependentTemplateSpecializationType>(type);
John McCall4f4e4132011-05-04 01:45:19 +0000918 mangleSourceName(tst->getIdentifier());
919 mangleUnresolvedTemplateArgs(tst->getArgs(), tst->getNumArgs());
John McCalld3d49bb2011-06-28 16:49:23 +0000920 break;
921 }
John McCall4f4e4132011-05-04 01:45:19 +0000922 }
923 break;
John McCalla0ce15c2011-04-24 08:23:24 +0000924 }
925
926 case NestedNameSpecifier::Identifier:
927 // Member expressions can have these without prefixes.
928 if (qualifier->getPrefix()) {
929 mangleUnresolvedPrefix(qualifier->getPrefix(), firstQualifierLookup,
930 /*recursive*/ true);
931 } else if (firstQualifierLookup) {
932
933 // Try to make a proper qualifier out of the lookup result, and
934 // then just recurse on that.
935 NestedNameSpecifier *newQualifier;
936 if (TypeDecl *typeDecl = dyn_cast<TypeDecl>(firstQualifierLookup)) {
937 QualType type = getASTContext().getTypeDeclType(typeDecl);
938
939 // Pretend we had a different nested name specifier.
940 newQualifier = NestedNameSpecifier::Create(getASTContext(),
941 /*prefix*/ 0,
942 /*template*/ false,
943 type.getTypePtr());
944 } else if (NamespaceDecl *nspace =
945 dyn_cast<NamespaceDecl>(firstQualifierLookup)) {
946 newQualifier = NestedNameSpecifier::Create(getASTContext(),
947 /*prefix*/ 0,
948 nspace);
949 } else if (NamespaceAliasDecl *alias =
950 dyn_cast<NamespaceAliasDecl>(firstQualifierLookup)) {
951 newQualifier = NestedNameSpecifier::Create(getASTContext(),
952 /*prefix*/ 0,
953 alias);
954 } else {
955 // No sensible mangling to do here.
956 newQualifier = 0;
957 }
958
959 if (newQualifier)
960 return mangleUnresolvedPrefix(newQualifier, /*lookup*/ 0, recursive);
961
962 } else {
963 Out << "sr";
964 }
965
966 mangleSourceName(qualifier->getAsIdentifier());
967 break;
968 }
969
970 // If this was the innermost part of the NNS, and we fell out to
971 // here, append an 'E'.
972 if (!recursive)
973 Out << 'E';
974}
975
976/// Mangle an unresolved-name, which is generally used for names which
977/// weren't resolved to specific entities.
978void CXXNameMangler::mangleUnresolvedName(NestedNameSpecifier *qualifier,
979 NamedDecl *firstQualifierLookup,
980 DeclarationName name,
981 unsigned knownArity) {
982 if (qualifier) mangleUnresolvedPrefix(qualifier, firstQualifierLookup);
983 mangleUnqualifiedName(0, name, knownArity);
John McCall1dd73832010-02-04 01:42:13 +0000984}
985
Anders Carlsson6f7e2f42010-06-08 14:49:03 +0000986static const FieldDecl *FindFirstNamedDataMember(const RecordDecl *RD) {
987 assert(RD->isAnonymousStructOrUnion() &&
988 "Expected anonymous struct or union!");
989
990 for (RecordDecl::field_iterator I = RD->field_begin(), E = RD->field_end();
991 I != E; ++I) {
992 const FieldDecl *FD = *I;
993
994 if (FD->getIdentifier())
995 return FD;
996
997 if (const RecordType *RT = FD->getType()->getAs<RecordType>()) {
998 if (const FieldDecl *NamedDataMember =
999 FindFirstNamedDataMember(RT->getDecl()))
1000 return NamedDataMember;
1001 }
1002 }
1003
1004 // We didn't find a named data member.
1005 return 0;
1006}
1007
John McCall1dd73832010-02-04 01:42:13 +00001008void CXXNameMangler::mangleUnqualifiedName(const NamedDecl *ND,
1009 DeclarationName Name,
1010 unsigned KnownArity) {
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001011 // <unqualified-name> ::= <operator-name>
Mike Stump1eb44332009-09-09 15:08:12 +00001012 // ::= <ctor-dtor-name>
1013 // ::= <source-name>
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001014 switch (Name.getNameKind()) {
Anders Carlssonc4355b62009-10-07 01:45:02 +00001015 case DeclarationName::Identifier: {
Anders Carlssonc4355b62009-10-07 01:45:02 +00001016 if (const IdentifierInfo *II = Name.getAsIdentifierInfo()) {
Sean Hunt31455252010-01-24 03:04:27 +00001017 // We must avoid conflicts between internally- and externally-
John McCall74990f42011-03-22 06:34:45 +00001018 // linked variable and function declaration names in the same TU:
1019 // void test() { extern void foo(); }
1020 // static void foo();
1021 // This naming convention is the same as that followed by GCC,
1022 // though it shouldn't actually matter.
1023 if (ND && ND->getLinkage() == InternalLinkage &&
Sean Hunt31455252010-01-24 03:04:27 +00001024 ND->getDeclContext()->isFileContext())
1025 Out << 'L';
1026
Anders Carlssonc4355b62009-10-07 01:45:02 +00001027 mangleSourceName(II);
1028 break;
1029 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00001030
John McCall1dd73832010-02-04 01:42:13 +00001031 // Otherwise, an anonymous entity. We must have a declaration.
1032 assert(ND && "mangling empty name without declaration");
1033
1034 if (const NamespaceDecl *NS = dyn_cast<NamespaceDecl>(ND)) {
1035 if (NS->isAnonymousNamespace()) {
1036 // This is how gcc mangles these names.
1037 Out << "12_GLOBAL__N_1";
1038 break;
1039 }
1040 }
1041
Anders Carlsson6f7e2f42010-06-08 14:49:03 +00001042 if (const VarDecl *VD = dyn_cast<VarDecl>(ND)) {
1043 // We must have an anonymous union or struct declaration.
1044 const RecordDecl *RD =
1045 cast<RecordDecl>(VD->getType()->getAs<RecordType>()->getDecl());
1046
1047 // Itanium C++ ABI 5.1.2:
1048 //
1049 // For the purposes of mangling, the name of an anonymous union is
1050 // considered to be the name of the first named data member found by a
1051 // pre-order, depth-first, declaration-order walk of the data members of
1052 // the anonymous union. If there is no such data member (i.e., if all of
1053 // the data members in the union are unnamed), then there is no way for
1054 // a program to refer to the anonymous union, and there is therefore no
1055 // need to mangle its name.
1056 const FieldDecl *FD = FindFirstNamedDataMember(RD);
John McCall7121c8f2010-08-05 22:02:13 +00001057
1058 // It's actually possible for various reasons for us to get here
1059 // with an empty anonymous struct / union. Fortunately, it
1060 // doesn't really matter what name we generate.
1061 if (!FD) break;
Anders Carlsson6f7e2f42010-06-08 14:49:03 +00001062 assert(FD->getIdentifier() && "Data member name isn't an identifier!");
1063
1064 mangleSourceName(FD->getIdentifier());
1065 break;
1066 }
1067
Anders Carlssonc4355b62009-10-07 01:45:02 +00001068 // We must have an anonymous struct.
1069 const TagDecl *TD = cast<TagDecl>(ND);
Richard Smith162e1c12011-04-15 14:24:37 +00001070 if (const TypedefNameDecl *D = TD->getTypedefNameForAnonDecl()) {
Anders Carlssonc4355b62009-10-07 01:45:02 +00001071 assert(TD->getDeclContext() == D->getDeclContext() &&
1072 "Typedef should not be in another decl context!");
1073 assert(D->getDeclName().getAsIdentifierInfo() &&
1074 "Typedef was not named!");
1075 mangleSourceName(D->getDeclName().getAsIdentifierInfo());
1076 break;
1077 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00001078
Anders Carlssonc4355b62009-10-07 01:45:02 +00001079 // Get a unique id for the anonymous struct.
1080 uint64_t AnonStructId = Context.getAnonymousStructId(TD);
1081
1082 // Mangle it as a source name in the form
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00001083 // [n] $_<id>
Anders Carlssonc4355b62009-10-07 01:45:02 +00001084 // where n is the length of the string.
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00001085 SmallString<8> Str;
Anders Carlssonc4355b62009-10-07 01:45:02 +00001086 Str += "$_";
1087 Str += llvm::utostr(AnonStructId);
1088
1089 Out << Str.size();
1090 Out << Str.str();
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001091 break;
Anders Carlssonc4355b62009-10-07 01:45:02 +00001092 }
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001093
1094 case DeclarationName::ObjCZeroArgSelector:
1095 case DeclarationName::ObjCOneArgSelector:
1096 case DeclarationName::ObjCMultiArgSelector:
David Blaikieb219cfc2011-09-23 05:06:16 +00001097 llvm_unreachable("Can't mangle Objective-C selector names here!");
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001098
1099 case DeclarationName::CXXConstructorName:
Anders Carlsson27ae5362009-04-17 01:58:57 +00001100 if (ND == Structor)
Mike Stump141c5af2009-09-02 00:25:38 +00001101 // If the named decl is the C++ constructor we're mangling, use the type
1102 // we were given.
Anders Carlsson27ae5362009-04-17 01:58:57 +00001103 mangleCXXCtorType(static_cast<CXXCtorType>(StructorType));
Anders Carlsson3ac86b52009-04-15 05:36:58 +00001104 else
1105 // Otherwise, use the complete constructor name. This is relevant if a
1106 // class with a constructor is declared within a constructor.
1107 mangleCXXCtorType(Ctor_Complete);
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001108 break;
1109
1110 case DeclarationName::CXXDestructorName:
Anders Carlsson27ae5362009-04-17 01:58:57 +00001111 if (ND == Structor)
Mike Stump141c5af2009-09-02 00:25:38 +00001112 // If the named decl is the C++ destructor we're mangling, use the type we
1113 // were given.
Anders Carlsson27ae5362009-04-17 01:58:57 +00001114 mangleCXXDtorType(static_cast<CXXDtorType>(StructorType));
1115 else
1116 // Otherwise, use the complete destructor name. This is relevant if a
1117 // class with a destructor is declared within a destructor.
1118 mangleCXXDtorType(Dtor_Complete);
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001119 break;
1120
1121 case DeclarationName::CXXConversionFunctionName:
Mike Stump1eb44332009-09-09 15:08:12 +00001122 // <operator-name> ::= cv <type> # (cast)
Douglas Gregor219cc612009-02-13 01:28:03 +00001123 Out << "cv";
Douglas Gregor79e6bd32011-07-12 04:42:08 +00001124 mangleType(Name.getCXXNameType());
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001125 break;
1126
Anders Carlsson8257d412009-12-22 06:36:32 +00001127 case DeclarationName::CXXOperatorName: {
John McCall1dd73832010-02-04 01:42:13 +00001128 unsigned Arity;
1129 if (ND) {
1130 Arity = cast<FunctionDecl>(ND)->getNumParams();
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001131
John McCall1dd73832010-02-04 01:42:13 +00001132 // If we have a C++ member function, we need to include the 'this' pointer.
1133 // FIXME: This does not make sense for operators that are static, but their
1134 // names stay the same regardless of the arity (operator new for instance).
1135 if (isa<CXXMethodDecl>(ND))
1136 Arity++;
1137 } else
1138 Arity = KnownArity;
1139
Anders Carlsson8257d412009-12-22 06:36:32 +00001140 mangleOperatorName(Name.getCXXOverloadedOperator(), Arity);
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001141 break;
Anders Carlsson8257d412009-12-22 06:36:32 +00001142 }
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001143
Sean Hunt3e518bd2009-11-29 07:34:05 +00001144 case DeclarationName::CXXLiteralOperatorName:
Sean Hunt5dd6b392009-12-04 21:11:13 +00001145 // FIXME: This mangling is not yet official.
Sean Hunt2421f662009-12-04 21:01:37 +00001146 Out << "li";
Sean Hunt3e518bd2009-11-29 07:34:05 +00001147 mangleSourceName(Name.getCXXLiteralIdentifier());
1148 break;
1149
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001150 case DeclarationName::CXXUsingDirective:
David Blaikieb219cfc2011-09-23 05:06:16 +00001151 llvm_unreachable("Can't mangle a using directive name!");
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001152 }
1153}
1154
1155void CXXNameMangler::mangleSourceName(const IdentifierInfo *II) {
1156 // <source-name> ::= <positive length number> <identifier>
1157 // <number> ::= [n] <non-negative decimal integer>
1158 // <identifier> ::= <unqualified source code identifier>
1159 Out << II->getLength() << II->getName();
1160}
1161
Eli Friedman7facf842009-12-02 20:32:49 +00001162void CXXNameMangler::mangleNestedName(const NamedDecl *ND,
Fariborz Jahanian57058532010-03-03 19:41:08 +00001163 const DeclContext *DC,
1164 bool NoFunction) {
Douglas Gregor0a9a6d62011-01-26 17:36:28 +00001165 // <nested-name>
1166 // ::= N [<CV-qualifiers>] [<ref-qualifier>] <prefix> <unqualified-name> E
1167 // ::= N [<CV-qualifiers>] [<ref-qualifier>] <template-prefix>
1168 // <template-args> E
Anders Carlssond99edc42009-09-26 03:55:37 +00001169
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001170 Out << 'N';
Douglas Gregor0a9a6d62011-01-26 17:36:28 +00001171 if (const CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(ND)) {
John McCall0953e762009-09-24 19:53:00 +00001172 mangleQualifiers(Qualifiers::fromCVRMask(Method->getTypeQualifiers()));
Douglas Gregor0a9a6d62011-01-26 17:36:28 +00001173 mangleRefQualifier(Method->getRefQualifier());
1174 }
1175
Anders Carlsson2744a062009-09-18 19:00:18 +00001176 // Check if we have a template.
1177 const TemplateArgumentList *TemplateArgs = 0;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00001178 if (const TemplateDecl *TD = isTemplate(ND, TemplateArgs)) {
Anders Carlsson2744a062009-09-18 19:00:18 +00001179 mangleTemplatePrefix(TD);
Rafael Espindolad9800722010-03-11 14:07:00 +00001180 TemplateParameterList *TemplateParameters = TD->getTemplateParameters();
1181 mangleTemplateArgs(*TemplateParameters, *TemplateArgs);
Fariborz Jahanian57058532010-03-03 19:41:08 +00001182 }
1183 else {
1184 manglePrefix(DC, NoFunction);
Anders Carlsson7482e242009-09-18 04:29:09 +00001185 mangleUnqualifiedName(ND);
1186 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00001187
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001188 Out << 'E';
1189}
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00001190void CXXNameMangler::mangleNestedName(const TemplateDecl *TD,
Anders Carlsson7624f212009-09-18 02:42:01 +00001191 const TemplateArgument *TemplateArgs,
1192 unsigned NumTemplateArgs) {
Anders Carlssone45117b2009-09-27 19:53:49 +00001193 // <nested-name> ::= N [<CV-qualifiers>] <template-prefix> <template-args> E
1194
Anders Carlsson7624f212009-09-18 02:42:01 +00001195 Out << 'N';
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00001196
Anders Carlssone45117b2009-09-27 19:53:49 +00001197 mangleTemplatePrefix(TD);
Rafael Espindolad9800722010-03-11 14:07:00 +00001198 TemplateParameterList *TemplateParameters = TD->getTemplateParameters();
1199 mangleTemplateArgs(*TemplateParameters, TemplateArgs, NumTemplateArgs);
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00001200
Anders Carlsson7624f212009-09-18 02:42:01 +00001201 Out << 'E';
1202}
1203
Anders Carlsson1b42c792009-04-02 16:24:45 +00001204void CXXNameMangler::mangleLocalName(const NamedDecl *ND) {
1205 // <local-name> := Z <function encoding> E <entity name> [<discriminator>]
1206 // := Z <function encoding> E s [<discriminator>]
Mike Stump1eb44332009-09-09 15:08:12 +00001207 // <discriminator> := _ <non-negative number>
Fariborz Jahanian57058532010-03-03 19:41:08 +00001208 const DeclContext *DC = ND->getDeclContext();
Fariborz Jahanian8805fe82011-06-09 19:25:01 +00001209 if (isa<ObjCMethodDecl>(DC) && isa<FunctionDecl>(ND)) {
1210 // Don't add objc method name mangling to locally declared function
1211 mangleUnqualifiedName(ND);
1212 return;
1213 }
1214
Anders Carlsson1b42c792009-04-02 16:24:45 +00001215 Out << 'Z';
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001216
Charles Davis685b1d92010-05-26 18:25:27 +00001217 if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(DC)) {
1218 mangleObjCMethodName(MD);
John McCall82b7d7b2010-10-18 21:28:44 +00001219 } else if (const CXXRecordDecl *RD = GetLocalClassDecl(ND)) {
1220 mangleFunctionEncoding(cast<FunctionDecl>(RD->getDeclContext()));
Fariborz Jahanian57058532010-03-03 19:41:08 +00001221 Out << 'E';
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001222
John McCall82b7d7b2010-10-18 21:28:44 +00001223 // Mangle the name relative to the closest enclosing function.
1224 if (ND == RD) // equality ok because RD derived from ND above
1225 mangleUnqualifiedName(ND);
1226 else
1227 mangleNestedName(ND, DC, true /*NoFunction*/);
1228
Fariborz Jahanian4819ac42010-03-04 01:02:03 +00001229 unsigned disc;
John McCall82b7d7b2010-10-18 21:28:44 +00001230 if (Context.getNextDiscriminator(RD, disc)) {
Fariborz Jahanian4819ac42010-03-04 01:02:03 +00001231 if (disc < 10)
1232 Out << '_' << disc;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001233 else
Fariborz Jahanian4819ac42010-03-04 01:02:03 +00001234 Out << "__" << disc << '_';
1235 }
Fariborz Jahanian57058532010-03-03 19:41:08 +00001236
1237 return;
1238 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001239 else
Fariborz Jahanian57058532010-03-03 19:41:08 +00001240 mangleFunctionEncoding(cast<FunctionDecl>(DC));
Anders Carlsson7b06f6c2009-12-10 03:14:39 +00001241
Anders Carlsson1b42c792009-04-02 16:24:45 +00001242 Out << 'E';
Eli Friedman6f9f25d2009-12-11 20:21:38 +00001243 mangleUnqualifiedName(ND);
Anders Carlsson1b42c792009-04-02 16:24:45 +00001244}
1245
John McCalla0ce15c2011-04-24 08:23:24 +00001246void CXXNameMangler::manglePrefix(NestedNameSpecifier *qualifier) {
1247 switch (qualifier->getKind()) {
1248 case NestedNameSpecifier::Global:
1249 // nothing
1250 return;
1251
1252 case NestedNameSpecifier::Namespace:
1253 mangleName(qualifier->getAsNamespace());
1254 return;
1255
1256 case NestedNameSpecifier::NamespaceAlias:
1257 mangleName(qualifier->getAsNamespaceAlias()->getNamespace());
1258 return;
1259
1260 case NestedNameSpecifier::TypeSpec:
1261 case NestedNameSpecifier::TypeSpecWithTemplate:
John McCall4f4e4132011-05-04 01:45:19 +00001262 manglePrefix(QualType(qualifier->getAsType(), 0));
John McCalla0ce15c2011-04-24 08:23:24 +00001263 return;
1264
1265 case NestedNameSpecifier::Identifier:
1266 // Member expressions can have these without prefixes, but that
1267 // should end up in mangleUnresolvedPrefix instead.
1268 assert(qualifier->getPrefix());
1269 manglePrefix(qualifier->getPrefix());
1270
1271 mangleSourceName(qualifier->getAsIdentifier());
1272 return;
1273 }
1274
1275 llvm_unreachable("unexpected nested name specifier");
1276}
1277
Fariborz Jahanian57058532010-03-03 19:41:08 +00001278void CXXNameMangler::manglePrefix(const DeclContext *DC, bool NoFunction) {
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001279 // <prefix> ::= <prefix> <unqualified-name>
1280 // ::= <template-prefix> <template-args>
1281 // ::= <template-param>
1282 // ::= # empty
1283 // ::= <substitution>
Anders Carlsson6862fc72009-09-17 04:16:28 +00001284
Anders Carlssonadd28822009-09-22 20:33:31 +00001285 while (isa<LinkageSpecDecl>(DC))
1286 DC = DC->getParent();
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00001287
Anders Carlsson9263e912009-09-18 18:39:58 +00001288 if (DC->isTranslationUnit())
1289 return;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00001290
Douglas Gregor35415f52010-05-25 17:04:15 +00001291 if (const BlockDecl *Block = dyn_cast<BlockDecl>(DC)) {
1292 manglePrefix(DC->getParent(), NoFunction);
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00001293 SmallString<64> Name;
Rafael Espindolac4850c22011-02-10 23:59:36 +00001294 llvm::raw_svector_ostream NameStream(Name);
1295 Context.mangleBlock(Block, NameStream);
1296 NameStream.flush();
Douglas Gregor35415f52010-05-25 17:04:15 +00001297 Out << Name.size() << Name;
1298 return;
1299 }
1300
Anders Carlsson6862fc72009-09-17 04:16:28 +00001301 if (mangleSubstitution(cast<NamedDecl>(DC)))
1302 return;
Anders Carlsson7482e242009-09-18 04:29:09 +00001303
Anders Carlsson2ee3fca2009-09-18 20:11:09 +00001304 // Check if we have a template.
1305 const TemplateArgumentList *TemplateArgs = 0;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00001306 if (const TemplateDecl *TD = isTemplate(cast<NamedDecl>(DC), TemplateArgs)) {
Anders Carlsson2ee3fca2009-09-18 20:11:09 +00001307 mangleTemplatePrefix(TD);
Rafael Espindolad9800722010-03-11 14:07:00 +00001308 TemplateParameterList *TemplateParameters = TD->getTemplateParameters();
1309 mangleTemplateArgs(*TemplateParameters, *TemplateArgs);
Fariborz Jahanian57058532010-03-03 19:41:08 +00001310 }
Douglas Gregor35415f52010-05-25 17:04:15 +00001311 else if(NoFunction && (isa<FunctionDecl>(DC) || isa<ObjCMethodDecl>(DC)))
Fariborz Jahanian57058532010-03-03 19:41:08 +00001312 return;
Douglas Gregor35415f52010-05-25 17:04:15 +00001313 else if (const ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(DC))
1314 mangleObjCMethodName(Method);
Fariborz Jahanian57058532010-03-03 19:41:08 +00001315 else {
1316 manglePrefix(DC->getParent(), NoFunction);
Anders Carlsson2ee3fca2009-09-18 20:11:09 +00001317 mangleUnqualifiedName(cast<NamedDecl>(DC));
1318 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00001319
Anders Carlsson6862fc72009-09-17 04:16:28 +00001320 addSubstitution(cast<NamedDecl>(DC));
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001321}
1322
Douglas Gregor20f0cc72010-04-23 03:10:43 +00001323void CXXNameMangler::mangleTemplatePrefix(TemplateName Template) {
1324 // <template-prefix> ::= <prefix> <template unqualified-name>
1325 // ::= <template-param>
1326 // ::= <substitution>
1327 if (TemplateDecl *TD = Template.getAsTemplateDecl())
1328 return mangleTemplatePrefix(TD);
1329
1330 if (QualifiedTemplateName *Qualified = Template.getAsQualifiedTemplateName())
John McCalla0ce15c2011-04-24 08:23:24 +00001331 manglePrefix(Qualified->getQualifier());
Sean Huntc3021132010-05-05 15:23:54 +00001332
Douglas Gregor20f0cc72010-04-23 03:10:43 +00001333 if (OverloadedTemplateStorage *Overloaded
1334 = Template.getAsOverloadedTemplate()) {
Sean Huntc3021132010-05-05 15:23:54 +00001335 mangleUnqualifiedName(0, (*Overloaded->begin())->getDeclName(),
Douglas Gregor20f0cc72010-04-23 03:10:43 +00001336 UnknownArity);
1337 return;
1338 }
Sean Huntc3021132010-05-05 15:23:54 +00001339
Douglas Gregor20f0cc72010-04-23 03:10:43 +00001340 DependentTemplateName *Dependent = Template.getAsDependentTemplateName();
1341 assert(Dependent && "Unknown template name kind?");
John McCalla0ce15c2011-04-24 08:23:24 +00001342 manglePrefix(Dependent->getQualifier());
Douglas Gregor1e9268e2010-04-28 05:58:56 +00001343 mangleUnscopedTemplateName(Template);
Douglas Gregor20f0cc72010-04-23 03:10:43 +00001344}
1345
Anders Carlsson0fa6df42009-09-26 19:45:45 +00001346void CXXNameMangler::mangleTemplatePrefix(const TemplateDecl *ND) {
Anders Carlsson7482e242009-09-18 04:29:09 +00001347 // <template-prefix> ::= <prefix> <template unqualified-name>
1348 // ::= <template-param>
1349 // ::= <substitution>
Douglas Gregor32fb4e12010-02-05 20:45:00 +00001350 // <template-template-param> ::= <template-param>
1351 // <substitution>
Anders Carlsson7482e242009-09-18 04:29:09 +00001352
Anders Carlssonaeb85372009-09-26 22:18:22 +00001353 if (mangleSubstitution(ND))
1354 return;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00001355
Douglas Gregor32fb4e12010-02-05 20:45:00 +00001356 // <template-template-param> ::= <template-param>
1357 if (const TemplateTemplateParmDecl *TTP
1358 = dyn_cast<TemplateTemplateParmDecl>(ND)) {
1359 mangleTemplateParameter(TTP->getIndex());
1360 return;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001361 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00001362
Anders Carlssonaa73ab12009-09-18 18:47:07 +00001363 manglePrefix(ND->getDeclContext());
Anders Carlsson1668f202009-09-26 20:13:56 +00001364 mangleUnqualifiedName(ND->getTemplatedDecl());
Anders Carlssonaeb85372009-09-26 22:18:22 +00001365 addSubstitution(ND);
Anders Carlsson7482e242009-09-18 04:29:09 +00001366}
1367
John McCallb6f532e2010-07-14 06:43:17 +00001368/// Mangles a template name under the production <type>. Required for
1369/// template template arguments.
1370/// <type> ::= <class-enum-type>
1371/// ::= <template-param>
1372/// ::= <substitution>
1373void CXXNameMangler::mangleType(TemplateName TN) {
1374 if (mangleSubstitution(TN))
1375 return;
1376
1377 TemplateDecl *TD = 0;
1378
1379 switch (TN.getKind()) {
1380 case TemplateName::QualifiedTemplate:
1381 TD = TN.getAsQualifiedTemplateName()->getTemplateDecl();
1382 goto HaveDecl;
1383
1384 case TemplateName::Template:
1385 TD = TN.getAsTemplateDecl();
1386 goto HaveDecl;
1387
1388 HaveDecl:
1389 if (isa<TemplateTemplateParmDecl>(TD))
1390 mangleTemplateParameter(cast<TemplateTemplateParmDecl>(TD)->getIndex());
1391 else
1392 mangleName(TD);
1393 break;
1394
1395 case TemplateName::OverloadedTemplate:
1396 llvm_unreachable("can't mangle an overloaded template name as a <type>");
John McCallb6f532e2010-07-14 06:43:17 +00001397
1398 case TemplateName::DependentTemplate: {
1399 const DependentTemplateName *Dependent = TN.getAsDependentTemplateName();
1400 assert(Dependent->isIdentifier());
1401
1402 // <class-enum-type> ::= <name>
1403 // <name> ::= <nested-name>
John McCalla0ce15c2011-04-24 08:23:24 +00001404 mangleUnresolvedPrefix(Dependent->getQualifier(), 0);
John McCallb6f532e2010-07-14 06:43:17 +00001405 mangleSourceName(Dependent->getIdentifier());
1406 break;
1407 }
1408
John McCallb44e0cf2011-06-30 21:59:02 +00001409 case TemplateName::SubstTemplateTemplateParm: {
1410 // Substituted template parameters are mangled as the substituted
1411 // template. This will check for the substitution twice, which is
1412 // fine, but we have to return early so that we don't try to *add*
1413 // the substitution twice.
1414 SubstTemplateTemplateParmStorage *subst
1415 = TN.getAsSubstTemplateTemplateParm();
1416 mangleType(subst->getReplacement());
1417 return;
1418 }
John McCall14606042011-06-30 08:33:18 +00001419
Douglas Gregor1aee05d2011-01-15 06:45:20 +00001420 case TemplateName::SubstTemplateTemplateParmPack: {
John McCall68a51a72011-07-01 00:04:39 +00001421 // FIXME: not clear how to mangle this!
1422 // template <template <class> class T...> class A {
1423 // template <template <class> class U...> void foo(B<T,U> x...);
1424 // };
1425 Out << "_SUBSTPACK_";
Douglas Gregor1aee05d2011-01-15 06:45:20 +00001426 break;
1427 }
John McCallb6f532e2010-07-14 06:43:17 +00001428 }
1429
1430 addSubstitution(TN);
1431}
1432
Mike Stump1eb44332009-09-09 15:08:12 +00001433void
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001434CXXNameMangler::mangleOperatorName(OverloadedOperatorKind OO, unsigned Arity) {
1435 switch (OO) {
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001436 // <operator-name> ::= nw # new
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001437 case OO_New: Out << "nw"; break;
1438 // ::= na # new[]
1439 case OO_Array_New: Out << "na"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001440 // ::= dl # delete
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001441 case OO_Delete: Out << "dl"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001442 // ::= da # delete[]
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001443 case OO_Array_Delete: Out << "da"; break;
1444 // ::= ps # + (unary)
John McCall5e1e89b2010-08-18 19:18:59 +00001445 // ::= pl # + (binary or unknown)
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001446 case OO_Plus:
Anders Carlsson8257d412009-12-22 06:36:32 +00001447 Out << (Arity == 1? "ps" : "pl"); break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001448 // ::= ng # - (unary)
John McCall5e1e89b2010-08-18 19:18:59 +00001449 // ::= mi # - (binary or unknown)
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001450 case OO_Minus:
Anders Carlsson8257d412009-12-22 06:36:32 +00001451 Out << (Arity == 1? "ng" : "mi"); break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001452 // ::= ad # & (unary)
John McCall5e1e89b2010-08-18 19:18:59 +00001453 // ::= an # & (binary or unknown)
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001454 case OO_Amp:
Anders Carlsson8257d412009-12-22 06:36:32 +00001455 Out << (Arity == 1? "ad" : "an"); break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001456 // ::= de # * (unary)
John McCall5e1e89b2010-08-18 19:18:59 +00001457 // ::= ml # * (binary or unknown)
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001458 case OO_Star:
John McCall5e1e89b2010-08-18 19:18:59 +00001459 // Use binary when unknown.
Anders Carlsson8257d412009-12-22 06:36:32 +00001460 Out << (Arity == 1? "de" : "ml"); break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001461 // ::= co # ~
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001462 case OO_Tilde: Out << "co"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001463 // ::= dv # /
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001464 case OO_Slash: Out << "dv"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001465 // ::= rm # %
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001466 case OO_Percent: Out << "rm"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001467 // ::= or # |
1468 case OO_Pipe: Out << "or"; break;
1469 // ::= eo # ^
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001470 case OO_Caret: Out << "eo"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001471 // ::= aS # =
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001472 case OO_Equal: Out << "aS"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001473 // ::= pL # +=
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001474 case OO_PlusEqual: Out << "pL"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001475 // ::= mI # -=
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001476 case OO_MinusEqual: Out << "mI"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001477 // ::= mL # *=
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001478 case OO_StarEqual: Out << "mL"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001479 // ::= dV # /=
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001480 case OO_SlashEqual: Out << "dV"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001481 // ::= rM # %=
1482 case OO_PercentEqual: Out << "rM"; break;
1483 // ::= aN # &=
1484 case OO_AmpEqual: Out << "aN"; break;
1485 // ::= oR # |=
1486 case OO_PipeEqual: Out << "oR"; break;
1487 // ::= eO # ^=
1488 case OO_CaretEqual: Out << "eO"; break;
1489 // ::= ls # <<
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001490 case OO_LessLess: Out << "ls"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001491 // ::= rs # >>
1492 case OO_GreaterGreater: Out << "rs"; break;
1493 // ::= lS # <<=
1494 case OO_LessLessEqual: Out << "lS"; break;
1495 // ::= rS # >>=
1496 case OO_GreaterGreaterEqual: Out << "rS"; break;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001497 // ::= eq # ==
1498 case OO_EqualEqual: Out << "eq"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001499 // ::= ne # !=
1500 case OO_ExclaimEqual: Out << "ne"; break;
1501 // ::= lt # <
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001502 case OO_Less: Out << "lt"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001503 // ::= gt # >
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001504 case OO_Greater: Out << "gt"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001505 // ::= le # <=
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001506 case OO_LessEqual: Out << "le"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001507 // ::= ge # >=
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001508 case OO_GreaterEqual: Out << "ge"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001509 // ::= nt # !
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001510 case OO_Exclaim: Out << "nt"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001511 // ::= aa # &&
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001512 case OO_AmpAmp: Out << "aa"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001513 // ::= oo # ||
1514 case OO_PipePipe: Out << "oo"; break;
1515 // ::= pp # ++
1516 case OO_PlusPlus: Out << "pp"; break;
1517 // ::= mm # --
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001518 case OO_MinusMinus: Out << "mm"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001519 // ::= cm # ,
1520 case OO_Comma: Out << "cm"; break;
1521 // ::= pm # ->*
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001522 case OO_ArrowStar: Out << "pm"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001523 // ::= pt # ->
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001524 case OO_Arrow: Out << "pt"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001525 // ::= cl # ()
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001526 case OO_Call: Out << "cl"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001527 // ::= ix # []
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001528 case OO_Subscript: Out << "ix"; break;
Anders Carlssone170ba72009-12-14 01:45:37 +00001529
1530 // ::= qu # ?
1531 // The conditional operator can't be overloaded, but we still handle it when
1532 // mangling expressions.
1533 case OO_Conditional: Out << "qu"; break;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001534
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001535 case OO_None:
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001536 case NUM_OVERLOADED_OPERATORS:
David Blaikieb219cfc2011-09-23 05:06:16 +00001537 llvm_unreachable("Not an overloaded operator");
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001538 }
1539}
1540
John McCall0953e762009-09-24 19:53:00 +00001541void CXXNameMangler::mangleQualifiers(Qualifiers Quals) {
Mike Stump1eb44332009-09-09 15:08:12 +00001542 // <CV-qualifiers> ::= [r] [V] [K] # restrict (C99), volatile, const
John McCall0953e762009-09-24 19:53:00 +00001543 if (Quals.hasRestrict())
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001544 Out << 'r';
John McCall0953e762009-09-24 19:53:00 +00001545 if (Quals.hasVolatile())
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001546 Out << 'V';
John McCall0953e762009-09-24 19:53:00 +00001547 if (Quals.hasConst())
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001548 Out << 'K';
John McCall0953e762009-09-24 19:53:00 +00001549
Douglas Gregor56079f72010-06-14 23:15:08 +00001550 if (Quals.hasAddressSpace()) {
1551 // Extension:
1552 //
1553 // <type> ::= U <address-space-number>
1554 //
1555 // where <address-space-number> is a source name consisting of 'AS'
1556 // followed by the address space <number>.
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00001557 SmallString<64> ASString;
Douglas Gregor56079f72010-06-14 23:15:08 +00001558 ASString = "AS" + llvm::utostr_32(Quals.getAddressSpace());
1559 Out << 'U' << ASString.size() << ASString;
1560 }
1561
Chris Lattner5f9e2722011-07-23 10:55:15 +00001562 StringRef LifetimeName;
John McCallf85e1932011-06-15 23:02:42 +00001563 switch (Quals.getObjCLifetime()) {
1564 // Objective-C ARC Extension:
1565 //
1566 // <type> ::= U "__strong"
1567 // <type> ::= U "__weak"
1568 // <type> ::= U "__autoreleasing"
John McCallf85e1932011-06-15 23:02:42 +00001569 case Qualifiers::OCL_None:
1570 break;
1571
1572 case Qualifiers::OCL_Weak:
1573 LifetimeName = "__weak";
1574 break;
1575
1576 case Qualifiers::OCL_Strong:
1577 LifetimeName = "__strong";
1578 break;
1579
1580 case Qualifiers::OCL_Autoreleasing:
1581 LifetimeName = "__autoreleasing";
1582 break;
1583
1584 case Qualifiers::OCL_ExplicitNone:
Douglas Gregorc22d6992011-06-17 22:26:49 +00001585 // The __unsafe_unretained qualifier is *not* mangled, so that
1586 // __unsafe_unretained types in ARC produce the same manglings as the
1587 // equivalent (but, naturally, unqualified) types in non-ARC, providing
1588 // better ABI compatibility.
1589 //
1590 // It's safe to do this because unqualified 'id' won't show up
1591 // in any type signatures that need to be mangled.
John McCallf85e1932011-06-15 23:02:42 +00001592 break;
1593 }
1594 if (!LifetimeName.empty())
1595 Out << 'U' << LifetimeName.size() << LifetimeName;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001596}
1597
Douglas Gregor0a9a6d62011-01-26 17:36:28 +00001598void CXXNameMangler::mangleRefQualifier(RefQualifierKind RefQualifier) {
1599 // <ref-qualifier> ::= R # lvalue reference
1600 // ::= O # rvalue-reference
1601 // Proposal to Itanium C++ ABI list on 1/26/11
1602 switch (RefQualifier) {
1603 case RQ_None:
1604 break;
1605
1606 case RQ_LValue:
1607 Out << 'R';
1608 break;
1609
1610 case RQ_RValue:
1611 Out << 'O';
1612 break;
1613 }
1614}
1615
Anders Carlsson7b06f6c2009-12-10 03:14:39 +00001616void CXXNameMangler::mangleObjCMethodName(const ObjCMethodDecl *MD) {
Rafael Espindolaf0be9792011-02-11 02:52:17 +00001617 Context.mangleObjCMethodName(MD, Out);
Anders Carlsson7b06f6c2009-12-10 03:14:39 +00001618}
1619
Douglas Gregorf1588662011-07-12 15:18:55 +00001620void CXXNameMangler::mangleType(QualType T) {
1621 // If our type is instantiation-dependent but not dependent, we mangle
1622 // it as it was written in the source, removing any top-level sugar.
1623 // Otherwise, use the canonical type.
1624 //
1625 // FIXME: This is an approximation of the instantiation-dependent name
1626 // mangling rules, since we should really be using the type as written and
1627 // augmented via semantic analysis (i.e., with implicit conversions and
1628 // default template arguments) for any instantiation-dependent type.
1629 // Unfortunately, that requires several changes to our AST:
1630 // - Instantiation-dependent TemplateSpecializationTypes will need to be
1631 // uniqued, so that we can handle substitutions properly
1632 // - Default template arguments will need to be represented in the
1633 // TemplateSpecializationType, since they need to be mangled even though
1634 // they aren't written.
1635 // - Conversions on non-type template arguments need to be expressed, since
1636 // they can affect the mangling of sizeof/alignof.
1637 if (!T->isInstantiationDependentType() || T->isDependentType())
1638 T = T.getCanonicalType();
1639 else {
1640 // Desugar any types that are purely sugar.
1641 do {
1642 // Don't desugar through template specialization types that aren't
1643 // type aliases. We need to mangle the template arguments as written.
1644 if (const TemplateSpecializationType *TST
1645 = dyn_cast<TemplateSpecializationType>(T))
1646 if (!TST->isTypeAlias())
1647 break;
Anders Carlsson4843e582009-03-10 17:07:44 +00001648
Douglas Gregorf1588662011-07-12 15:18:55 +00001649 QualType Desugared
1650 = T.getSingleStepDesugaredType(Context.getASTContext());
1651 if (Desugared == T)
1652 break;
1653
1654 T = Desugared;
1655 } while (true);
1656 }
1657 SplitQualType split = T.split();
John McCall200fa532012-02-08 00:46:36 +00001658 Qualifiers quals = split.Quals;
1659 const Type *ty = split.Ty;
John McCallb47f7482011-01-26 20:05:40 +00001660
Douglas Gregorf1588662011-07-12 15:18:55 +00001661 bool isSubstitutable = quals || !isa<BuiltinType>(T);
1662 if (isSubstitutable && mangleSubstitution(T))
Anders Carlsson76967372009-09-17 00:43:46 +00001663 return;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001664
John McCallb47f7482011-01-26 20:05:40 +00001665 // If we're mangling a qualified array type, push the qualifiers to
1666 // the element type.
Douglas Gregorf1588662011-07-12 15:18:55 +00001667 if (quals && isa<ArrayType>(T)) {
1668 ty = Context.getASTContext().getAsArrayType(T);
John McCallb47f7482011-01-26 20:05:40 +00001669 quals = Qualifiers();
1670
Douglas Gregorf1588662011-07-12 15:18:55 +00001671 // Note that we don't update T: we want to add the
1672 // substitution at the original type.
John McCallb47f7482011-01-26 20:05:40 +00001673 }
1674
1675 if (quals) {
1676 mangleQualifiers(quals);
John McCall0953e762009-09-24 19:53:00 +00001677 // Recurse: even if the qualified type isn't yet substitutable,
1678 // the unqualified type might be.
John McCallb47f7482011-01-26 20:05:40 +00001679 mangleType(QualType(ty, 0));
Anders Carlsson76967372009-09-17 00:43:46 +00001680 } else {
John McCallb47f7482011-01-26 20:05:40 +00001681 switch (ty->getTypeClass()) {
John McCallefe6aee2009-09-05 07:56:18 +00001682#define ABSTRACT_TYPE(CLASS, PARENT)
1683#define NON_CANONICAL_TYPE(CLASS, PARENT) \
Anders Carlsson76967372009-09-17 00:43:46 +00001684 case Type::CLASS: \
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +00001685 llvm_unreachable("can't mangle non-canonical type " #CLASS "Type"); \
Anders Carlsson76967372009-09-17 00:43:46 +00001686 return;
John McCallefe6aee2009-09-05 07:56:18 +00001687#define TYPE(CLASS, PARENT) \
Anders Carlsson76967372009-09-17 00:43:46 +00001688 case Type::CLASS: \
John McCallb47f7482011-01-26 20:05:40 +00001689 mangleType(static_cast<const CLASS##Type*>(ty)); \
Anders Carlsson76967372009-09-17 00:43:46 +00001690 break;
John McCallefe6aee2009-09-05 07:56:18 +00001691#include "clang/AST/TypeNodes.def"
Anders Carlsson76967372009-09-17 00:43:46 +00001692 }
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001693 }
Anders Carlsson76967372009-09-17 00:43:46 +00001694
1695 // Add the substitution.
John McCallb47f7482011-01-26 20:05:40 +00001696 if (isSubstitutable)
Douglas Gregorf1588662011-07-12 15:18:55 +00001697 addSubstitution(T);
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001698}
1699
Douglas Gregor1b12a3b2010-05-26 05:11:13 +00001700void CXXNameMangler::mangleNameOrStandardSubstitution(const NamedDecl *ND) {
1701 if (!mangleStandardSubstitution(ND))
1702 mangleName(ND);
1703}
1704
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001705void CXXNameMangler::mangleType(const BuiltinType *T) {
John McCallefe6aee2009-09-05 07:56:18 +00001706 // <type> ::= <builtin-type>
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001707 // <builtin-type> ::= v # void
1708 // ::= w # wchar_t
1709 // ::= b # bool
1710 // ::= c # char
1711 // ::= a # signed char
1712 // ::= h # unsigned char
1713 // ::= s # short
1714 // ::= t # unsigned short
1715 // ::= i # int
1716 // ::= j # unsigned int
1717 // ::= l # long
1718 // ::= m # unsigned long
1719 // ::= x # long long, __int64
1720 // ::= y # unsigned long long, __int64
1721 // ::= n # __int128
1722 // UNSUPPORTED: ::= o # unsigned __int128
1723 // ::= f # float
1724 // ::= d # double
1725 // ::= e # long double, __float80
1726 // UNSUPPORTED: ::= g # __float128
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001727 // UNSUPPORTED: ::= Dd # IEEE 754r decimal floating point (64 bits)
1728 // UNSUPPORTED: ::= De # IEEE 754r decimal floating point (128 bits)
1729 // UNSUPPORTED: ::= Df # IEEE 754r decimal floating point (32 bits)
Anton Korobeynikovaa4a99b2011-10-14 23:23:15 +00001730 // ::= Dh # IEEE 754r half-precision floating point (16 bits)
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001731 // ::= Di # char32_t
1732 // ::= Ds # char16_t
Anders Carlssone2923682010-11-04 04:31:32 +00001733 // ::= Dn # std::nullptr_t (i.e., decltype(nullptr))
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001734 // ::= u <source-name> # vendor extended type
1735 switch (T->getKind()) {
1736 case BuiltinType::Void: Out << 'v'; break;
1737 case BuiltinType::Bool: Out << 'b'; break;
1738 case BuiltinType::Char_U: case BuiltinType::Char_S: Out << 'c'; break;
1739 case BuiltinType::UChar: Out << 'h'; break;
1740 case BuiltinType::UShort: Out << 't'; break;
1741 case BuiltinType::UInt: Out << 'j'; break;
1742 case BuiltinType::ULong: Out << 'm'; break;
1743 case BuiltinType::ULongLong: Out << 'y'; break;
Chris Lattner2df9ced2009-04-30 02:43:43 +00001744 case BuiltinType::UInt128: Out << 'o'; break;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001745 case BuiltinType::SChar: Out << 'a'; break;
Chris Lattner3f59c972010-12-25 23:25:43 +00001746 case BuiltinType::WChar_S:
1747 case BuiltinType::WChar_U: Out << 'w'; break;
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001748 case BuiltinType::Char16: Out << "Ds"; break;
1749 case BuiltinType::Char32: Out << "Di"; break;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001750 case BuiltinType::Short: Out << 's'; break;
1751 case BuiltinType::Int: Out << 'i'; break;
1752 case BuiltinType::Long: Out << 'l'; break;
1753 case BuiltinType::LongLong: Out << 'x'; break;
Chris Lattner2df9ced2009-04-30 02:43:43 +00001754 case BuiltinType::Int128: Out << 'n'; break;
Anton Korobeynikovaa4a99b2011-10-14 23:23:15 +00001755 case BuiltinType::Half: Out << "Dh"; break;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001756 case BuiltinType::Float: Out << 'f'; break;
1757 case BuiltinType::Double: Out << 'd'; break;
1758 case BuiltinType::LongDouble: Out << 'e'; break;
Anders Carlssone2923682010-11-04 04:31:32 +00001759 case BuiltinType::NullPtr: Out << "Dn"; break;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001760
John McCalle0a22d02011-10-18 21:02:43 +00001761#define BUILTIN_TYPE(Id, SingletonId)
1762#define PLACEHOLDER_TYPE(Id, SingletonId) \
1763 case BuiltinType::Id:
1764#include "clang/AST/BuiltinTypes.def"
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001765 case BuiltinType::Dependent:
John McCallfb44de92011-05-01 22:35:37 +00001766 llvm_unreachable("mangling a placeholder type");
Steve Naroff9533a7f2009-07-22 17:14:51 +00001767 case BuiltinType::ObjCId: Out << "11objc_object"; break;
1768 case BuiltinType::ObjCClass: Out << "10objc_class"; break;
Fariborz Jahanian13dcd002009-11-21 19:53:08 +00001769 case BuiltinType::ObjCSel: Out << "13objc_selector"; break;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001770 }
1771}
1772
John McCallefe6aee2009-09-05 07:56:18 +00001773// <type> ::= <function-type>
1774// <function-type> ::= F [Y] <bare-function-type> E
1775void CXXNameMangler::mangleType(const FunctionProtoType *T) {
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001776 Out << 'F';
Mike Stumpf5408fe2009-05-16 07:57:57 +00001777 // FIXME: We don't have enough information in the AST to produce the 'Y'
1778 // encoding for extern "C" function types.
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001779 mangleBareFunctionType(T, /*MangleReturnType=*/true);
1780 Out << 'E';
1781}
John McCallefe6aee2009-09-05 07:56:18 +00001782void CXXNameMangler::mangleType(const FunctionNoProtoType *T) {
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +00001783 llvm_unreachable("Can't mangle K&R function prototypes");
John McCallefe6aee2009-09-05 07:56:18 +00001784}
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001785void CXXNameMangler::mangleBareFunctionType(const FunctionType *T,
1786 bool MangleReturnType) {
John McCallefe6aee2009-09-05 07:56:18 +00001787 // We should never be mangling something without a prototype.
1788 const FunctionProtoType *Proto = cast<FunctionProtoType>(T);
1789
John McCallfb44de92011-05-01 22:35:37 +00001790 // Record that we're in a function type. See mangleFunctionParam
1791 // for details on what we're trying to achieve here.
1792 FunctionTypeDepthState saved = FunctionTypeDepth.push();
1793
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001794 // <bare-function-type> ::= <signature type>+
John McCallfb44de92011-05-01 22:35:37 +00001795 if (MangleReturnType) {
1796 FunctionTypeDepth.enterResultType();
John McCallefe6aee2009-09-05 07:56:18 +00001797 mangleType(Proto->getResultType());
John McCallfb44de92011-05-01 22:35:37 +00001798 FunctionTypeDepth.leaveResultType();
1799 }
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001800
Anders Carlsson93296682010-06-02 04:40:13 +00001801 if (Proto->getNumArgs() == 0 && !Proto->isVariadic()) {
Eli Friedmana7e68452010-08-22 01:00:03 +00001802 // <builtin-type> ::= v # void
Anders Carlssonc6c91bc2009-04-01 00:15:23 +00001803 Out << 'v';
John McCallfb44de92011-05-01 22:35:37 +00001804
1805 FunctionTypeDepth.pop(saved);
Anders Carlssonc6c91bc2009-04-01 00:15:23 +00001806 return;
1807 }
Mike Stump1eb44332009-09-09 15:08:12 +00001808
Douglas Gregor72564e72009-02-26 23:50:07 +00001809 for (FunctionProtoType::arg_type_iterator Arg = Proto->arg_type_begin(),
Mike Stump1eb44332009-09-09 15:08:12 +00001810 ArgEnd = Proto->arg_type_end();
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001811 Arg != ArgEnd; ++Arg)
Douglas Gregor79e6bd32011-07-12 04:42:08 +00001812 mangleType(Context.getASTContext().getSignatureParameterType(*Arg));
Douglas Gregor219cc612009-02-13 01:28:03 +00001813
John McCallfb44de92011-05-01 22:35:37 +00001814 FunctionTypeDepth.pop(saved);
1815
Douglas Gregor219cc612009-02-13 01:28:03 +00001816 // <builtin-type> ::= z # ellipsis
1817 if (Proto->isVariadic())
1818 Out << 'z';
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001819}
1820
John McCallefe6aee2009-09-05 07:56:18 +00001821// <type> ::= <class-enum-type>
Mike Stump1eb44332009-09-09 15:08:12 +00001822// <class-enum-type> ::= <name>
John McCalled976492009-12-04 22:46:56 +00001823void CXXNameMangler::mangleType(const UnresolvedUsingType *T) {
1824 mangleName(T->getDecl());
1825}
1826
1827// <type> ::= <class-enum-type>
1828// <class-enum-type> ::= <name>
John McCallefe6aee2009-09-05 07:56:18 +00001829void CXXNameMangler::mangleType(const EnumType *T) {
1830 mangleType(static_cast<const TagType*>(T));
1831}
1832void CXXNameMangler::mangleType(const RecordType *T) {
1833 mangleType(static_cast<const TagType*>(T));
1834}
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001835void CXXNameMangler::mangleType(const TagType *T) {
Eli Friedmanecb7e932009-12-11 18:00:57 +00001836 mangleName(T->getDecl());
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001837}
1838
John McCallefe6aee2009-09-05 07:56:18 +00001839// <type> ::= <array-type>
1840// <array-type> ::= A <positive dimension number> _ <element type>
1841// ::= A [<dimension expression>] _ <element type>
1842void CXXNameMangler::mangleType(const ConstantArrayType *T) {
1843 Out << 'A' << T->getSize() << '_';
1844 mangleType(T->getElementType());
1845}
1846void CXXNameMangler::mangleType(const VariableArrayType *T) {
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001847 Out << 'A';
Fariborz Jahanian7281d1f2010-11-02 16:54:00 +00001848 // decayed vla types (size 0) will just be skipped.
1849 if (T->getSizeExpr())
1850 mangleExpression(T->getSizeExpr());
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001851 Out << '_';
1852 mangleType(T->getElementType());
1853}
John McCallefe6aee2009-09-05 07:56:18 +00001854void CXXNameMangler::mangleType(const DependentSizedArrayType *T) {
1855 Out << 'A';
1856 mangleExpression(T->getSizeExpr());
1857 Out << '_';
1858 mangleType(T->getElementType());
1859}
1860void CXXNameMangler::mangleType(const IncompleteArrayType *T) {
Nick Lewycky271b6652010-09-05 03:40:33 +00001861 Out << "A_";
John McCallefe6aee2009-09-05 07:56:18 +00001862 mangleType(T->getElementType());
1863}
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001864
John McCallefe6aee2009-09-05 07:56:18 +00001865// <type> ::= <pointer-to-member-type>
1866// <pointer-to-member-type> ::= M <class type> <member type>
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001867void CXXNameMangler::mangleType(const MemberPointerType *T) {
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001868 Out << 'M';
1869 mangleType(QualType(T->getClass(), 0));
Anders Carlsson0e650012009-05-17 17:41:20 +00001870 QualType PointeeType = T->getPointeeType();
1871 if (const FunctionProtoType *FPT = dyn_cast<FunctionProtoType>(PointeeType)) {
John McCall0953e762009-09-24 19:53:00 +00001872 mangleQualifiers(Qualifiers::fromCVRMask(FPT->getTypeQuals()));
Douglas Gregor0a9a6d62011-01-26 17:36:28 +00001873 mangleRefQualifier(FPT->getRefQualifier());
Anders Carlsson0e650012009-05-17 17:41:20 +00001874 mangleType(FPT);
Anders Carlsson9d85b722010-06-02 04:29:50 +00001875
1876 // Itanium C++ ABI 5.1.8:
1877 //
1878 // The type of a non-static member function is considered to be different,
1879 // for the purposes of substitution, from the type of a namespace-scope or
1880 // static member function whose type appears similar. The types of two
1881 // non-static member functions are considered to be different, for the
1882 // purposes of substitution, if the functions are members of different
1883 // classes. In other words, for the purposes of substitution, the class of
1884 // which the function is a member is considered part of the type of
1885 // function.
1886
1887 // We increment the SeqID here to emulate adding an entry to the
1888 // substitution table. We can't actually add it because we don't want this
1889 // particular function type to be substituted.
1890 ++SeqID;
Mike Stump1eb44332009-09-09 15:08:12 +00001891 } else
Anders Carlsson0e650012009-05-17 17:41:20 +00001892 mangleType(PointeeType);
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001893}
1894
John McCallefe6aee2009-09-05 07:56:18 +00001895// <type> ::= <template-param>
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001896void CXXNameMangler::mangleType(const TemplateTypeParmType *T) {
Anders Carlsson0ccdf8d2009-09-27 00:38:53 +00001897 mangleTemplateParameter(T->getIndex());
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001898}
1899
Douglas Gregorc3069d62011-01-14 02:55:32 +00001900// <type> ::= <template-param>
1901void CXXNameMangler::mangleType(const SubstTemplateTypeParmPackType *T) {
John McCall68a51a72011-07-01 00:04:39 +00001902 // FIXME: not clear how to mangle this!
1903 // template <class T...> class A {
1904 // template <class U...> void foo(T(*)(U) x...);
1905 // };
1906 Out << "_SUBSTPACK_";
Douglas Gregorc3069d62011-01-14 02:55:32 +00001907}
1908
John McCallefe6aee2009-09-05 07:56:18 +00001909// <type> ::= P <type> # pointer-to
1910void CXXNameMangler::mangleType(const PointerType *T) {
1911 Out << 'P';
1912 mangleType(T->getPointeeType());
1913}
1914void CXXNameMangler::mangleType(const ObjCObjectPointerType *T) {
1915 Out << 'P';
1916 mangleType(T->getPointeeType());
1917}
1918
1919// <type> ::= R <type> # reference-to
1920void CXXNameMangler::mangleType(const LValueReferenceType *T) {
1921 Out << 'R';
1922 mangleType(T->getPointeeType());
1923}
1924
1925// <type> ::= O <type> # rvalue reference-to (C++0x)
1926void CXXNameMangler::mangleType(const RValueReferenceType *T) {
1927 Out << 'O';
1928 mangleType(T->getPointeeType());
1929}
1930
1931// <type> ::= C <type> # complex pair (C 2000)
1932void CXXNameMangler::mangleType(const ComplexType *T) {
1933 Out << 'C';
1934 mangleType(T->getElementType());
1935}
1936
Bob Wilsonc7df92d2010-11-12 17:24:43 +00001937// ARM's ABI for Neon vector types specifies that they should be mangled as
Bob Wilson57147a82010-11-16 00:32:18 +00001938// if they are structs (to match ARM's initial implementation). The
1939// vector type must be one of the special types predefined by ARM.
1940void CXXNameMangler::mangleNeonVectorType(const VectorType *T) {
Bob Wilsonc7df92d2010-11-12 17:24:43 +00001941 QualType EltType = T->getElementType();
Bob Wilson57147a82010-11-16 00:32:18 +00001942 assert(EltType->isBuiltinType() && "Neon vector element not a BuiltinType");
Bob Wilsonc7df92d2010-11-12 17:24:43 +00001943 const char *EltName = 0;
Bob Wilson491328c2010-11-12 17:24:46 +00001944 if (T->getVectorKind() == VectorType::NeonPolyVector) {
1945 switch (cast<BuiltinType>(EltType)->getKind()) {
Bob Wilson4cfaa5d2010-11-12 17:24:49 +00001946 case BuiltinType::SChar: EltName = "poly8_t"; break;
1947 case BuiltinType::Short: EltName = "poly16_t"; break;
Bob Wilson57147a82010-11-16 00:32:18 +00001948 default: llvm_unreachable("unexpected Neon polynomial vector element type");
Bob Wilson491328c2010-11-12 17:24:46 +00001949 }
1950 } else {
1951 switch (cast<BuiltinType>(EltType)->getKind()) {
Bob Wilson4cfaa5d2010-11-12 17:24:49 +00001952 case BuiltinType::SChar: EltName = "int8_t"; break;
1953 case BuiltinType::UChar: EltName = "uint8_t"; break;
1954 case BuiltinType::Short: EltName = "int16_t"; break;
1955 case BuiltinType::UShort: EltName = "uint16_t"; break;
1956 case BuiltinType::Int: EltName = "int32_t"; break;
1957 case BuiltinType::UInt: EltName = "uint32_t"; break;
1958 case BuiltinType::LongLong: EltName = "int64_t"; break;
1959 case BuiltinType::ULongLong: EltName = "uint64_t"; break;
1960 case BuiltinType::Float: EltName = "float32_t"; break;
Bob Wilson57147a82010-11-16 00:32:18 +00001961 default: llvm_unreachable("unexpected Neon vector element type");
Bob Wilson491328c2010-11-12 17:24:46 +00001962 }
Bob Wilsonc7df92d2010-11-12 17:24:43 +00001963 }
1964 const char *BaseName = 0;
Bob Wilson4cfaa5d2010-11-12 17:24:49 +00001965 unsigned BitSize = (T->getNumElements() *
Bob Wilson3a723022010-11-16 00:32:12 +00001966 getASTContext().getTypeSize(EltType));
Bob Wilsonc7df92d2010-11-12 17:24:43 +00001967 if (BitSize == 64)
1968 BaseName = "__simd64_";
Bob Wilson57147a82010-11-16 00:32:18 +00001969 else {
1970 assert(BitSize == 128 && "Neon vector type not 64 or 128 bits");
Bob Wilsonc7df92d2010-11-12 17:24:43 +00001971 BaseName = "__simd128_";
Bob Wilson57147a82010-11-16 00:32:18 +00001972 }
Bob Wilsonc7df92d2010-11-12 17:24:43 +00001973 Out << strlen(BaseName) + strlen(EltName);
1974 Out << BaseName << EltName;
Bob Wilsonc7df92d2010-11-12 17:24:43 +00001975}
1976
John McCallefe6aee2009-09-05 07:56:18 +00001977// GNU extension: vector types
Chris Lattner788b0fd2010-06-23 06:00:24 +00001978// <type> ::= <vector-type>
1979// <vector-type> ::= Dv <positive dimension number> _
1980// <extended element type>
1981// ::= Dv [<dimension expression>] _ <element type>
1982// <extended element type> ::= <element type>
1983// ::= p # AltiVec vector pixel
John McCallefe6aee2009-09-05 07:56:18 +00001984void CXXNameMangler::mangleType(const VectorType *T) {
Bob Wilson491328c2010-11-12 17:24:46 +00001985 if ((T->getVectorKind() == VectorType::NeonVector ||
Bob Wilson57147a82010-11-16 00:32:18 +00001986 T->getVectorKind() == VectorType::NeonPolyVector)) {
1987 mangleNeonVectorType(T);
Bob Wilsonc7df92d2010-11-12 17:24:43 +00001988 return;
Bob Wilson57147a82010-11-16 00:32:18 +00001989 }
Nick Lewycky0e5f0672010-03-26 07:18:04 +00001990 Out << "Dv" << T->getNumElements() << '_';
Bob Wilsone86d78c2010-11-10 21:56:12 +00001991 if (T->getVectorKind() == VectorType::AltiVecPixel)
Chris Lattner788b0fd2010-06-23 06:00:24 +00001992 Out << 'p';
Bob Wilsone86d78c2010-11-10 21:56:12 +00001993 else if (T->getVectorKind() == VectorType::AltiVecBool)
Chris Lattner788b0fd2010-06-23 06:00:24 +00001994 Out << 'b';
1995 else
1996 mangleType(T->getElementType());
John McCallefe6aee2009-09-05 07:56:18 +00001997}
1998void CXXNameMangler::mangleType(const ExtVectorType *T) {
1999 mangleType(static_cast<const VectorType*>(T));
2000}
2001void CXXNameMangler::mangleType(const DependentSizedExtVectorType *T) {
Nick Lewycky0e5f0672010-03-26 07:18:04 +00002002 Out << "Dv";
2003 mangleExpression(T->getSizeExpr());
2004 Out << '_';
John McCallefe6aee2009-09-05 07:56:18 +00002005 mangleType(T->getElementType());
2006}
2007
Douglas Gregor7536dd52010-12-20 02:24:11 +00002008void CXXNameMangler::mangleType(const PackExpansionType *T) {
Douglas Gregor4fc48662011-01-13 16:39:34 +00002009 // <type> ::= Dp <type> # pack expansion (C++0x)
Douglas Gregor255c2692011-01-13 17:44:36 +00002010 Out << "Dp";
Douglas Gregor7536dd52010-12-20 02:24:11 +00002011 mangleType(T->getPattern());
2012}
2013
Anders Carlssona40c5e42009-03-07 22:03:21 +00002014void CXXNameMangler::mangleType(const ObjCInterfaceType *T) {
2015 mangleSourceName(T->getDecl()->getIdentifier());
2016}
2017
John McCallc12c5bb2010-05-15 11:32:37 +00002018void CXXNameMangler::mangleType(const ObjCObjectType *T) {
John McCallc00c1f62010-05-15 17:06:29 +00002019 // We don't allow overloading by different protocol qualification,
2020 // so mangling them isn't necessary.
John McCallc12c5bb2010-05-15 11:32:37 +00002021 mangleType(T->getBaseType());
2022}
2023
John McCallefe6aee2009-09-05 07:56:18 +00002024void CXXNameMangler::mangleType(const BlockPointerType *T) {
Anders Carlssonf28c6872009-12-23 22:31:44 +00002025 Out << "U13block_pointer";
2026 mangleType(T->getPointeeType());
John McCallefe6aee2009-09-05 07:56:18 +00002027}
2028
John McCall31f17ec2010-04-27 00:57:59 +00002029void CXXNameMangler::mangleType(const InjectedClassNameType *T) {
2030 // Mangle injected class name types as if the user had written the
2031 // specialization out fully. It may not actually be possible to see
2032 // this mangling, though.
2033 mangleType(T->getInjectedSpecializationType());
2034}
2035
John McCallefe6aee2009-09-05 07:56:18 +00002036void CXXNameMangler::mangleType(const TemplateSpecializationType *T) {
Douglas Gregor1e9268e2010-04-28 05:58:56 +00002037 if (TemplateDecl *TD = T->getTemplateName().getAsTemplateDecl()) {
2038 mangleName(TD, T->getArgs(), T->getNumArgs());
2039 } else {
2040 if (mangleSubstitution(QualType(T, 0)))
2041 return;
Sean Huntc3021132010-05-05 15:23:54 +00002042
Douglas Gregor1e9268e2010-04-28 05:58:56 +00002043 mangleTemplatePrefix(T->getTemplateName());
Sean Huntc3021132010-05-05 15:23:54 +00002044
Douglas Gregor1e9268e2010-04-28 05:58:56 +00002045 // FIXME: GCC does not appear to mangle the template arguments when
2046 // the template in question is a dependent template name. Should we
2047 // emulate that badness?
2048 mangleTemplateArgs(T->getTemplateName(), T->getArgs(), T->getNumArgs());
2049 addSubstitution(QualType(T, 0));
2050 }
John McCallefe6aee2009-09-05 07:56:18 +00002051}
2052
Douglas Gregor4714c122010-03-31 17:34:00 +00002053void CXXNameMangler::mangleType(const DependentNameType *T) {
Anders Carlssonae352482009-09-26 02:26:02 +00002054 // Typename types are always nested
2055 Out << 'N';
John McCalla0ce15c2011-04-24 08:23:24 +00002056 manglePrefix(T->getQualifier());
John McCall33500952010-06-11 00:33:02 +00002057 mangleSourceName(T->getIdentifier());
2058 Out << 'E';
2059}
John McCall6ab30e02010-06-09 07:26:17 +00002060
John McCall33500952010-06-11 00:33:02 +00002061void CXXNameMangler::mangleType(const DependentTemplateSpecializationType *T) {
Douglas Gregoraa2187d2011-02-28 00:04:36 +00002062 // Dependently-scoped template types are nested if they have a prefix.
John McCall33500952010-06-11 00:33:02 +00002063 Out << 'N';
2064
2065 // TODO: avoid making this TemplateName.
2066 TemplateName Prefix =
2067 getASTContext().getDependentTemplateName(T->getQualifier(),
2068 T->getIdentifier());
2069 mangleTemplatePrefix(Prefix);
2070
2071 // FIXME: GCC does not appear to mangle the template arguments when
2072 // the template in question is a dependent template name. Should we
2073 // emulate that badness?
2074 mangleTemplateArgs(Prefix, T->getArgs(), T->getNumArgs());
Anders Carlssonae352482009-09-26 02:26:02 +00002075 Out << 'E';
John McCallefe6aee2009-09-05 07:56:18 +00002076}
2077
John McCallad5e7382010-03-01 23:49:17 +00002078void CXXNameMangler::mangleType(const TypeOfType *T) {
2079 // FIXME: this is pretty unsatisfactory, but there isn't an obvious
2080 // "extension with parameters" mangling.
2081 Out << "u6typeof";
2082}
2083
2084void CXXNameMangler::mangleType(const TypeOfExprType *T) {
2085 // FIXME: this is pretty unsatisfactory, but there isn't an obvious
2086 // "extension with parameters" mangling.
2087 Out << "u6typeof";
2088}
2089
2090void CXXNameMangler::mangleType(const DecltypeType *T) {
2091 Expr *E = T->getUnderlyingExpr();
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002092
John McCallad5e7382010-03-01 23:49:17 +00002093 // type ::= Dt <expression> E # decltype of an id-expression
2094 // # or class member access
2095 // ::= DT <expression> E # decltype of an expression
2096
2097 // This purports to be an exhaustive list of id-expressions and
2098 // class member accesses. Note that we do not ignore parentheses;
2099 // parentheses change the semantics of decltype for these
2100 // expressions (and cause the mangler to use the other form).
2101 if (isa<DeclRefExpr>(E) ||
2102 isa<MemberExpr>(E) ||
2103 isa<UnresolvedLookupExpr>(E) ||
2104 isa<DependentScopeDeclRefExpr>(E) ||
2105 isa<CXXDependentScopeMemberExpr>(E) ||
2106 isa<UnresolvedMemberExpr>(E))
2107 Out << "Dt";
2108 else
2109 Out << "DT";
2110 mangleExpression(E);
2111 Out << 'E';
2112}
2113
Sean Huntca63c202011-05-24 22:41:36 +00002114void CXXNameMangler::mangleType(const UnaryTransformType *T) {
2115 // If this is dependent, we need to record that. If not, we simply
2116 // mangle it as the underlying type since they are equivalent.
2117 if (T->isDependentType()) {
2118 Out << 'U';
2119
2120 switch (T->getUTTKind()) {
2121 case UnaryTransformType::EnumUnderlyingType:
2122 Out << "3eut";
2123 break;
2124 }
2125 }
2126
2127 mangleType(T->getUnderlyingType());
2128}
2129
Richard Smith34b41d92011-02-20 03:19:35 +00002130void CXXNameMangler::mangleType(const AutoType *T) {
2131 QualType D = T->getDeducedType();
Richard Smith967ecd32011-02-21 20:10:02 +00002132 // <builtin-type> ::= Da # dependent auto
2133 if (D.isNull())
2134 Out << "Da";
2135 else
2136 mangleType(D);
Richard Smith34b41d92011-02-20 03:19:35 +00002137}
2138
Eli Friedmanb001de72011-10-06 23:00:33 +00002139void CXXNameMangler::mangleType(const AtomicType *T) {
2140 // <type> ::= U <source-name> <type> # vendor extended type qualifier
2141 // (Until there's a standardized mangling...)
2142 Out << "U7_Atomic";
2143 mangleType(T->getValueType());
2144}
2145
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002146void CXXNameMangler::mangleIntegerLiteral(QualType T,
Anders Carlssone170ba72009-12-14 01:45:37 +00002147 const llvm::APSInt &Value) {
2148 // <expr-primary> ::= L <type> <value number> E # integer literal
2149 Out << 'L';
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002150
Anders Carlssone170ba72009-12-14 01:45:37 +00002151 mangleType(T);
2152 if (T->isBooleanType()) {
2153 // Boolean values are encoded as 0/1.
2154 Out << (Value.getBoolValue() ? '1' : '0');
2155 } else {
John McCall0512e482010-07-14 04:20:34 +00002156 mangleNumber(Value);
Anders Carlssone170ba72009-12-14 01:45:37 +00002157 }
2158 Out << 'E';
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002159
Anders Carlssone170ba72009-12-14 01:45:37 +00002160}
2161
John McCall2f27bf82010-02-04 02:56:29 +00002162/// Mangles a member expression. Implicit accesses are not handled,
2163/// but that should be okay, because you shouldn't be able to
2164/// make an implicit access in a function template declaration.
John McCalla0ce15c2011-04-24 08:23:24 +00002165void CXXNameMangler::mangleMemberExpr(const Expr *base,
2166 bool isArrow,
2167 NestedNameSpecifier *qualifier,
2168 NamedDecl *firstQualifierLookup,
2169 DeclarationName member,
2170 unsigned arity) {
2171 // <expression> ::= dt <expression> <unresolved-name>
2172 // ::= pt <expression> <unresolved-name>
2173 Out << (isArrow ? "pt" : "dt");
2174 mangleExpression(base);
2175 mangleUnresolvedName(qualifier, firstQualifierLookup, member, arity);
John McCall2f27bf82010-02-04 02:56:29 +00002176}
2177
John McCall5a7e6f72011-04-28 02:52:03 +00002178/// Look at the callee of the given call expression and determine if
2179/// it's a parenthesized id-expression which would have triggered ADL
2180/// otherwise.
2181static bool isParenthesizedADLCallee(const CallExpr *call) {
2182 const Expr *callee = call->getCallee();
2183 const Expr *fn = callee->IgnoreParens();
2184
2185 // Must be parenthesized. IgnoreParens() skips __extension__ nodes,
2186 // too, but for those to appear in the callee, it would have to be
2187 // parenthesized.
2188 if (callee == fn) return false;
2189
2190 // Must be an unresolved lookup.
2191 const UnresolvedLookupExpr *lookup = dyn_cast<UnresolvedLookupExpr>(fn);
2192 if (!lookup) return false;
2193
2194 assert(!lookup->requiresADL());
2195
2196 // Must be an unqualified lookup.
2197 if (lookup->getQualifier()) return false;
2198
2199 // Must not have found a class member. Note that if one is a class
2200 // member, they're all class members.
2201 if (lookup->getNumDecls() > 0 &&
2202 (*lookup->decls_begin())->isCXXClassMember())
2203 return false;
2204
2205 // Otherwise, ADL would have been triggered.
2206 return true;
2207}
2208
John McCall5e1e89b2010-08-18 19:18:59 +00002209void CXXNameMangler::mangleExpression(const Expr *E, unsigned Arity) {
Anders Carlssond553f8c2009-09-21 01:21:10 +00002210 // <expression> ::= <unary operator-name> <expression>
John McCall09cc1412010-02-03 00:55:45 +00002211 // ::= <binary operator-name> <expression> <expression>
2212 // ::= <trinary operator-name> <expression> <expression> <expression>
Anders Carlssond553f8c2009-09-21 01:21:10 +00002213 // ::= cv <type> expression # conversion with one argument
2214 // ::= cv <type> _ <expression>* E # conversion with a different number of arguments
Eli Friedmana7e68452010-08-22 01:00:03 +00002215 // ::= st <type> # sizeof (a type)
Anders Carlssond553f8c2009-09-21 01:21:10 +00002216 // ::= at <type> # alignof (a type)
2217 // ::= <template-param>
2218 // ::= <function-param>
2219 // ::= sr <type> <unqualified-name> # dependent name
2220 // ::= sr <type> <unqualified-name> <template-args> # dependent template-id
Douglas Gregor63f62df2011-06-05 05:27:58 +00002221 // ::= ds <expression> <expression> # expr.*expr
Anders Carlssond553f8c2009-09-21 01:21:10 +00002222 // ::= sZ <template-param> # size of a parameter pack
Douglas Gregor4fc48662011-01-13 16:39:34 +00002223 // ::= sZ <function-param> # size of a function parameter pack
John McCall09cc1412010-02-03 00:55:45 +00002224 // ::= <expr-primary>
John McCall1dd73832010-02-04 01:42:13 +00002225 // <expr-primary> ::= L <type> <value number> E # integer literal
2226 // ::= L <type <value float> E # floating literal
2227 // ::= L <mangled-name> E # external name
Douglas Gregoredee94b2011-07-12 04:47:20 +00002228 QualType ImplicitlyConvertedToType;
2229
2230recurse:
Anders Carlssond553f8c2009-09-21 01:21:10 +00002231 switch (E->getStmtClass()) {
John McCall6ae1f352010-04-09 22:26:14 +00002232 case Expr::NoStmtClass:
John McCall63c00d72011-02-09 08:16:59 +00002233#define ABSTRACT_STMT(Type)
John McCall6ae1f352010-04-09 22:26:14 +00002234#define EXPR(Type, Base)
2235#define STMT(Type, Base) \
2236 case Expr::Type##Class:
Sean Hunt4bfe1962010-05-05 15:24:00 +00002237#include "clang/AST/StmtNodes.inc"
John McCall0512e482010-07-14 04:20:34 +00002238 // fallthrough
2239
2240 // These all can only appear in local or variable-initialization
2241 // contexts and so should never appear in a mangling.
2242 case Expr::AddrLabelExprClass:
2243 case Expr::BlockDeclRefExprClass:
2244 case Expr::CXXThisExprClass:
2245 case Expr::DesignatedInitExprClass:
2246 case Expr::ImplicitValueInitExprClass:
2247 case Expr::InitListExprClass:
2248 case Expr::ParenListExprClass:
Douglas Gregor01d08012012-02-07 10:09:13 +00002249 case Expr::LambdaExprClass:
John McCall09cc1412010-02-03 00:55:45 +00002250 llvm_unreachable("unexpected statement kind");
John McCall09cc1412010-02-03 00:55:45 +00002251
John McCall0512e482010-07-14 04:20:34 +00002252 // FIXME: invent manglings for all these.
2253 case Expr::BlockExprClass:
2254 case Expr::CXXPseudoDestructorExprClass:
2255 case Expr::ChooseExprClass:
2256 case Expr::CompoundLiteralExprClass:
2257 case Expr::ExtVectorElementExprClass:
Peter Collingbournef111d932011-04-15 00:35:48 +00002258 case Expr::GenericSelectionExprClass:
John McCall0512e482010-07-14 04:20:34 +00002259 case Expr::ObjCEncodeExprClass:
John McCall0512e482010-07-14 04:20:34 +00002260 case Expr::ObjCIsaExprClass:
2261 case Expr::ObjCIvarRefExprClass:
2262 case Expr::ObjCMessageExprClass:
2263 case Expr::ObjCPropertyRefExprClass:
2264 case Expr::ObjCProtocolExprClass:
2265 case Expr::ObjCSelectorExprClass:
2266 case Expr::ObjCStringLiteralClass:
John McCallf85e1932011-06-15 23:02:42 +00002267 case Expr::ObjCIndirectCopyRestoreExprClass:
John McCall0512e482010-07-14 04:20:34 +00002268 case Expr::OffsetOfExprClass:
2269 case Expr::PredefinedExprClass:
2270 case Expr::ShuffleVectorExprClass:
2271 case Expr::StmtExprClass:
John McCall0512e482010-07-14 04:20:34 +00002272 case Expr::UnaryTypeTraitExprClass:
Francois Pichet6ad6f282010-12-07 00:08:36 +00002273 case Expr::BinaryTypeTraitExprClass:
John Wiegley21ff2e52011-04-28 00:16:57 +00002274 case Expr::ArrayTypeTraitExprClass:
John Wiegley55262202011-04-25 06:54:41 +00002275 case Expr::ExpressionTraitExprClass:
Francois Pichet9be88402010-09-08 23:47:05 +00002276 case Expr::VAArgExprClass:
Sebastian Redl2e156222010-09-10 20:55:43 +00002277 case Expr::CXXUuidofExprClass:
Peter Collingbournee08ce652011-02-09 21:07:24 +00002278 case Expr::CXXNoexceptExprClass:
Tanya Lattner61eee0c2011-06-04 00:47:47 +00002279 case Expr::CUDAKernelCallExprClass:
2280 case Expr::AsTypeExprClass:
John McCall4b9c2d22011-11-06 09:01:30 +00002281 case Expr::PseudoObjectExprClass:
Eli Friedman276b0612011-10-11 02:20:01 +00002282 case Expr::AtomicExprClass:
Tanya Lattner61eee0c2011-06-04 00:47:47 +00002283 {
John McCall6ae1f352010-04-09 22:26:14 +00002284 // As bad as this diagnostic is, it's better than crashing.
David Blaikied6471f72011-09-25 23:23:43 +00002285 DiagnosticsEngine &Diags = Context.getDiags();
2286 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
John McCall6ae1f352010-04-09 22:26:14 +00002287 "cannot yet mangle expression type %0");
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +00002288 Diags.Report(E->getExprLoc(), DiagID)
John McCall739bf092010-04-10 09:39:25 +00002289 << E->getStmtClassName() << E->getSourceRange();
John McCall6ae1f352010-04-09 22:26:14 +00002290 break;
2291 }
2292
John McCall56ca35d2011-02-17 10:25:35 +00002293 // Even gcc-4.5 doesn't mangle this.
2294 case Expr::BinaryConditionalOperatorClass: {
David Blaikied6471f72011-09-25 23:23:43 +00002295 DiagnosticsEngine &Diags = Context.getDiags();
John McCall56ca35d2011-02-17 10:25:35 +00002296 unsigned DiagID =
David Blaikied6471f72011-09-25 23:23:43 +00002297 Diags.getCustomDiagID(DiagnosticsEngine::Error,
John McCall56ca35d2011-02-17 10:25:35 +00002298 "?: operator with omitted middle operand cannot be mangled");
2299 Diags.Report(E->getExprLoc(), DiagID)
2300 << E->getStmtClassName() << E->getSourceRange();
2301 break;
2302 }
2303
2304 // These are used for internal purposes and cannot be meaningfully mangled.
John McCall7cd7d1a2010-11-15 23:31:06 +00002305 case Expr::OpaqueValueExprClass:
2306 llvm_unreachable("cannot mangle opaque value; mangling wrong thing?");
2307
John McCall0512e482010-07-14 04:20:34 +00002308 case Expr::CXXDefaultArgExprClass:
John McCall5e1e89b2010-08-18 19:18:59 +00002309 mangleExpression(cast<CXXDefaultArgExpr>(E)->getExpr(), Arity);
John McCall0512e482010-07-14 04:20:34 +00002310 break;
2311
John McCall91a57552011-07-15 05:09:51 +00002312 case Expr::SubstNonTypeTemplateParmExprClass:
2313 mangleExpression(cast<SubstNonTypeTemplateParmExpr>(E)->getReplacement(),
2314 Arity);
2315 break;
2316
John McCall0512e482010-07-14 04:20:34 +00002317 case Expr::CXXMemberCallExprClass: // fallthrough
John McCall1dd73832010-02-04 01:42:13 +00002318 case Expr::CallExprClass: {
2319 const CallExpr *CE = cast<CallExpr>(E);
John McCall5a7e6f72011-04-28 02:52:03 +00002320
2321 // <expression> ::= cp <simple-id> <expression>* E
2322 // We use this mangling only when the call would use ADL except
2323 // for being parenthesized. Per discussion with David
2324 // Vandervoorde, 2011.04.25.
2325 if (isParenthesizedADLCallee(CE)) {
2326 Out << "cp";
2327 // The callee here is a parenthesized UnresolvedLookupExpr with
2328 // no qualifier and should always get mangled as a <simple-id>
2329 // anyway.
2330
2331 // <expression> ::= cl <expression>* E
2332 } else {
2333 Out << "cl";
2334 }
2335
John McCall5e1e89b2010-08-18 19:18:59 +00002336 mangleExpression(CE->getCallee(), CE->getNumArgs());
John McCall1dd73832010-02-04 01:42:13 +00002337 for (unsigned I = 0, N = CE->getNumArgs(); I != N; ++I)
2338 mangleExpression(CE->getArg(I));
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002339 Out << 'E';
John McCall09cc1412010-02-03 00:55:45 +00002340 break;
John McCall1dd73832010-02-04 01:42:13 +00002341 }
John McCall09cc1412010-02-03 00:55:45 +00002342
John McCall0512e482010-07-14 04:20:34 +00002343 case Expr::CXXNewExprClass: {
2344 // Proposal from David Vandervoorde, 2010.06.30
2345 const CXXNewExpr *New = cast<CXXNewExpr>(E);
2346 if (New->isGlobalNew()) Out << "gs";
2347 Out << (New->isArray() ? "na" : "nw");
2348 for (CXXNewExpr::const_arg_iterator I = New->placement_arg_begin(),
2349 E = New->placement_arg_end(); I != E; ++I)
2350 mangleExpression(*I);
2351 Out << '_';
2352 mangleType(New->getAllocatedType());
2353 if (New->hasInitializer()) {
Sebastian Redl2aed8b82012-02-16 12:22:20 +00002354 // FIXME: Does this mean "parenthesized initializer"?
John McCall0512e482010-07-14 04:20:34 +00002355 Out << "pi";
Sebastian Redl2aed8b82012-02-16 12:22:20 +00002356 const Expr *Init = New->getInitializer();
2357 if (const CXXConstructExpr *CCE = dyn_cast<CXXConstructExpr>(Init)) {
2358 // Directly inline the initializers.
2359 for (CXXConstructExpr::const_arg_iterator I = CCE->arg_begin(),
2360 E = CCE->arg_end();
2361 I != E; ++I)
2362 mangleExpression(*I);
2363 } else if (const ParenListExpr *PLE = dyn_cast<ParenListExpr>(Init)) {
2364 for (unsigned i = 0, e = PLE->getNumExprs(); i != e; ++i)
2365 mangleExpression(PLE->getExpr(i));
2366 } else
2367 mangleExpression(Init);
John McCall0512e482010-07-14 04:20:34 +00002368 }
2369 Out << 'E';
2370 break;
2371 }
2372
John McCall2f27bf82010-02-04 02:56:29 +00002373 case Expr::MemberExprClass: {
2374 const MemberExpr *ME = cast<MemberExpr>(E);
2375 mangleMemberExpr(ME->getBase(), ME->isArrow(),
John McCalla0ce15c2011-04-24 08:23:24 +00002376 ME->getQualifier(), 0, ME->getMemberDecl()->getDeclName(),
John McCall5e1e89b2010-08-18 19:18:59 +00002377 Arity);
John McCall2f27bf82010-02-04 02:56:29 +00002378 break;
2379 }
2380
2381 case Expr::UnresolvedMemberExprClass: {
2382 const UnresolvedMemberExpr *ME = cast<UnresolvedMemberExpr>(E);
2383 mangleMemberExpr(ME->getBase(), ME->isArrow(),
John McCalla0ce15c2011-04-24 08:23:24 +00002384 ME->getQualifier(), 0, ME->getMemberName(),
John McCall5e1e89b2010-08-18 19:18:59 +00002385 Arity);
John McCall6dbce192010-08-20 00:17:19 +00002386 if (ME->hasExplicitTemplateArgs())
2387 mangleTemplateArgs(ME->getExplicitTemplateArgs());
John McCall2f27bf82010-02-04 02:56:29 +00002388 break;
2389 }
2390
2391 case Expr::CXXDependentScopeMemberExprClass: {
2392 const CXXDependentScopeMemberExpr *ME
2393 = cast<CXXDependentScopeMemberExpr>(E);
2394 mangleMemberExpr(ME->getBase(), ME->isArrow(),
John McCalla0ce15c2011-04-24 08:23:24 +00002395 ME->getQualifier(), ME->getFirstQualifierFoundInScope(),
2396 ME->getMember(), Arity);
John McCall6dbce192010-08-20 00:17:19 +00002397 if (ME->hasExplicitTemplateArgs())
2398 mangleTemplateArgs(ME->getExplicitTemplateArgs());
John McCall2f27bf82010-02-04 02:56:29 +00002399 break;
2400 }
2401
John McCall1dd73832010-02-04 01:42:13 +00002402 case Expr::UnresolvedLookupExprClass: {
2403 const UnresolvedLookupExpr *ULE = cast<UnresolvedLookupExpr>(E);
John McCalla0ce15c2011-04-24 08:23:24 +00002404 mangleUnresolvedName(ULE->getQualifier(), 0, ULE->getName(), Arity);
John McCall26a6ec72011-06-21 22:12:46 +00002405
2406 // All the <unresolved-name> productions end in a
2407 // base-unresolved-name, where <template-args> are just tacked
2408 // onto the end.
John McCall6dbce192010-08-20 00:17:19 +00002409 if (ULE->hasExplicitTemplateArgs())
2410 mangleTemplateArgs(ULE->getExplicitTemplateArgs());
John McCall1dd73832010-02-04 01:42:13 +00002411 break;
2412 }
2413
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002414 case Expr::CXXUnresolvedConstructExprClass: {
John McCall1dd73832010-02-04 01:42:13 +00002415 const CXXUnresolvedConstructExpr *CE = cast<CXXUnresolvedConstructExpr>(E);
2416 unsigned N = CE->arg_size();
2417
2418 Out << "cv";
2419 mangleType(CE->getType());
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002420 if (N != 1) Out << '_';
John McCall1dd73832010-02-04 01:42:13 +00002421 for (unsigned I = 0; I != N; ++I) mangleExpression(CE->getArg(I));
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002422 if (N != 1) Out << 'E';
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002423 break;
John McCall1dd73832010-02-04 01:42:13 +00002424 }
John McCall09cc1412010-02-03 00:55:45 +00002425
John McCall1dd73832010-02-04 01:42:13 +00002426 case Expr::CXXTemporaryObjectExprClass:
2427 case Expr::CXXConstructExprClass: {
2428 const CXXConstructExpr *CE = cast<CXXConstructExpr>(E);
2429 unsigned N = CE->getNumArgs();
2430
2431 Out << "cv";
2432 mangleType(CE->getType());
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002433 if (N != 1) Out << '_';
John McCall1dd73832010-02-04 01:42:13 +00002434 for (unsigned I = 0; I != N; ++I) mangleExpression(CE->getArg(I));
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002435 if (N != 1) Out << 'E';
John McCall09cc1412010-02-03 00:55:45 +00002436 break;
John McCall1dd73832010-02-04 01:42:13 +00002437 }
2438
Richard Smith41576d42012-02-06 02:54:51 +00002439 case Expr::CXXScalarValueInitExprClass:
2440 Out <<"cv";
2441 mangleType(E->getType());
2442 Out <<"_E";
2443 break;
2444
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +00002445 case Expr::UnaryExprOrTypeTraitExprClass: {
2446 const UnaryExprOrTypeTraitExpr *SAE = cast<UnaryExprOrTypeTraitExpr>(E);
Douglas Gregoredee94b2011-07-12 04:47:20 +00002447
2448 if (!SAE->isInstantiationDependent()) {
2449 // Itanium C++ ABI:
2450 // If the operand of a sizeof or alignof operator is not
2451 // instantiation-dependent it is encoded as an integer literal
2452 // reflecting the result of the operator.
2453 //
2454 // If the result of the operator is implicitly converted to a known
2455 // integer type, that type is used for the literal; otherwise, the type
2456 // of std::size_t or std::ptrdiff_t is used.
2457 QualType T = (ImplicitlyConvertedToType.isNull() ||
2458 !ImplicitlyConvertedToType->isIntegerType())? SAE->getType()
2459 : ImplicitlyConvertedToType;
Richard Smitha6b8b2c2011-10-10 18:28:20 +00002460 llvm::APSInt V = SAE->EvaluateKnownConstInt(Context.getASTContext());
2461 mangleIntegerLiteral(T, V);
Douglas Gregoredee94b2011-07-12 04:47:20 +00002462 break;
2463 }
2464
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +00002465 switch(SAE->getKind()) {
2466 case UETT_SizeOf:
2467 Out << 's';
2468 break;
2469 case UETT_AlignOf:
2470 Out << 'a';
2471 break;
2472 case UETT_VecStep:
David Blaikied6471f72011-09-25 23:23:43 +00002473 DiagnosticsEngine &Diags = Context.getDiags();
2474 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +00002475 "cannot yet mangle vec_step expression");
2476 Diags.Report(DiagID);
2477 return;
2478 }
John McCall1dd73832010-02-04 01:42:13 +00002479 if (SAE->isArgumentType()) {
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002480 Out << 't';
John McCall1dd73832010-02-04 01:42:13 +00002481 mangleType(SAE->getArgumentType());
2482 } else {
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002483 Out << 'z';
John McCall1dd73832010-02-04 01:42:13 +00002484 mangleExpression(SAE->getArgumentExpr());
2485 }
2486 break;
2487 }
Anders Carlssona7694082009-11-06 02:50:19 +00002488
John McCall0512e482010-07-14 04:20:34 +00002489 case Expr::CXXThrowExprClass: {
2490 const CXXThrowExpr *TE = cast<CXXThrowExpr>(E);
2491
2492 // Proposal from David Vandervoorde, 2010.06.30
2493 if (TE->getSubExpr()) {
2494 Out << "tw";
2495 mangleExpression(TE->getSubExpr());
2496 } else {
2497 Out << "tr";
2498 }
2499 break;
2500 }
2501
2502 case Expr::CXXTypeidExprClass: {
2503 const CXXTypeidExpr *TIE = cast<CXXTypeidExpr>(E);
2504
2505 // Proposal from David Vandervoorde, 2010.06.30
2506 if (TIE->isTypeOperand()) {
2507 Out << "ti";
2508 mangleType(TIE->getTypeOperand());
2509 } else {
2510 Out << "te";
2511 mangleExpression(TIE->getExprOperand());
2512 }
2513 break;
2514 }
2515
2516 case Expr::CXXDeleteExprClass: {
2517 const CXXDeleteExpr *DE = cast<CXXDeleteExpr>(E);
2518
2519 // Proposal from David Vandervoorde, 2010.06.30
2520 if (DE->isGlobalDelete()) Out << "gs";
2521 Out << (DE->isArrayForm() ? "da" : "dl");
2522 mangleExpression(DE->getArgument());
2523 break;
2524 }
2525
Anders Carlssone170ba72009-12-14 01:45:37 +00002526 case Expr::UnaryOperatorClass: {
2527 const UnaryOperator *UO = cast<UnaryOperator>(E);
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002528 mangleOperatorName(UnaryOperator::getOverloadedOperator(UO->getOpcode()),
Anders Carlssone170ba72009-12-14 01:45:37 +00002529 /*Arity=*/1);
2530 mangleExpression(UO->getSubExpr());
2531 break;
2532 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002533
John McCall0512e482010-07-14 04:20:34 +00002534 case Expr::ArraySubscriptExprClass: {
2535 const ArraySubscriptExpr *AE = cast<ArraySubscriptExpr>(E);
2536
Chris Lattnerfc8f0e12011-04-15 05:22:18 +00002537 // Array subscript is treated as a syntactically weird form of
John McCall0512e482010-07-14 04:20:34 +00002538 // binary operator.
2539 Out << "ix";
2540 mangleExpression(AE->getLHS());
2541 mangleExpression(AE->getRHS());
2542 break;
2543 }
2544
2545 case Expr::CompoundAssignOperatorClass: // fallthrough
Anders Carlssone170ba72009-12-14 01:45:37 +00002546 case Expr::BinaryOperatorClass: {
2547 const BinaryOperator *BO = cast<BinaryOperator>(E);
Douglas Gregor63f62df2011-06-05 05:27:58 +00002548 if (BO->getOpcode() == BO_PtrMemD)
2549 Out << "ds";
2550 else
2551 mangleOperatorName(BinaryOperator::getOverloadedOperator(BO->getOpcode()),
2552 /*Arity=*/2);
Anders Carlssone170ba72009-12-14 01:45:37 +00002553 mangleExpression(BO->getLHS());
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002554 mangleExpression(BO->getRHS());
Anders Carlssone170ba72009-12-14 01:45:37 +00002555 break;
John McCall2f27bf82010-02-04 02:56:29 +00002556 }
Anders Carlssone170ba72009-12-14 01:45:37 +00002557
2558 case Expr::ConditionalOperatorClass: {
2559 const ConditionalOperator *CO = cast<ConditionalOperator>(E);
2560 mangleOperatorName(OO_Conditional, /*Arity=*/3);
2561 mangleExpression(CO->getCond());
John McCall5e1e89b2010-08-18 19:18:59 +00002562 mangleExpression(CO->getLHS(), Arity);
2563 mangleExpression(CO->getRHS(), Arity);
Anders Carlssone170ba72009-12-14 01:45:37 +00002564 break;
2565 }
2566
Douglas Gregor46287c72010-01-29 16:37:09 +00002567 case Expr::ImplicitCastExprClass: {
Douglas Gregoredee94b2011-07-12 04:47:20 +00002568 ImplicitlyConvertedToType = E->getType();
2569 E = cast<ImplicitCastExpr>(E)->getSubExpr();
2570 goto recurse;
Douglas Gregor46287c72010-01-29 16:37:09 +00002571 }
John McCallf85e1932011-06-15 23:02:42 +00002572
2573 case Expr::ObjCBridgedCastExprClass: {
2574 // Mangle ownership casts as a vendor extended operator __bridge,
2575 // __bridge_transfer, or __bridge_retain.
Chris Lattner5f9e2722011-07-23 10:55:15 +00002576 StringRef Kind = cast<ObjCBridgedCastExpr>(E)->getBridgeKindName();
John McCallf85e1932011-06-15 23:02:42 +00002577 Out << "v1U" << Kind.size() << Kind;
2578 }
2579 // Fall through to mangle the cast itself.
2580
Douglas Gregor46287c72010-01-29 16:37:09 +00002581 case Expr::CStyleCastExprClass:
2582 case Expr::CXXStaticCastExprClass:
2583 case Expr::CXXDynamicCastExprClass:
2584 case Expr::CXXReinterpretCastExprClass:
2585 case Expr::CXXConstCastExprClass:
2586 case Expr::CXXFunctionalCastExprClass: {
2587 const ExplicitCastExpr *ECE = cast<ExplicitCastExpr>(E);
2588 Out << "cv";
2589 mangleType(ECE->getType());
2590 mangleExpression(ECE->getSubExpr());
2591 break;
2592 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002593
Anders Carlsson58040a52009-12-16 05:48:46 +00002594 case Expr::CXXOperatorCallExprClass: {
2595 const CXXOperatorCallExpr *CE = cast<CXXOperatorCallExpr>(E);
2596 unsigned NumArgs = CE->getNumArgs();
2597 mangleOperatorName(CE->getOperator(), /*Arity=*/NumArgs);
2598 // Mangle the arguments.
2599 for (unsigned i = 0; i != NumArgs; ++i)
2600 mangleExpression(CE->getArg(i));
2601 break;
2602 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002603
Anders Carlssona7694082009-11-06 02:50:19 +00002604 case Expr::ParenExprClass:
John McCall5e1e89b2010-08-18 19:18:59 +00002605 mangleExpression(cast<ParenExpr>(E)->getSubExpr(), Arity);
Anders Carlssona7694082009-11-06 02:50:19 +00002606 break;
2607
Anders Carlssond553f8c2009-09-21 01:21:10 +00002608 case Expr::DeclRefExprClass: {
Douglas Gregor5ed1bc32010-02-28 21:40:32 +00002609 const NamedDecl *D = cast<DeclRefExpr>(E)->getDecl();
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002610
Anders Carlssond553f8c2009-09-21 01:21:10 +00002611 switch (D->getKind()) {
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002612 default:
Douglas Gregor5ed1bc32010-02-28 21:40:32 +00002613 // <expr-primary> ::= L <mangled-name> E # external name
2614 Out << 'L';
2615 mangle(D, "_Z");
2616 Out << 'E';
2617 break;
2618
John McCallfb44de92011-05-01 22:35:37 +00002619 case Decl::ParmVar:
2620 mangleFunctionParam(cast<ParmVarDecl>(D));
2621 break;
2622
John McCall3dc7e7b2010-07-24 01:17:35 +00002623 case Decl::EnumConstant: {
2624 const EnumConstantDecl *ED = cast<EnumConstantDecl>(D);
2625 mangleIntegerLiteral(ED->getType(), ED->getInitVal());
2626 break;
2627 }
2628
Anders Carlssond553f8c2009-09-21 01:21:10 +00002629 case Decl::NonTypeTemplateParm: {
2630 const NonTypeTemplateParmDecl *PD = cast<NonTypeTemplateParmDecl>(D);
Anders Carlsson0ccdf8d2009-09-27 00:38:53 +00002631 mangleTemplateParameter(PD->getIndex());
Anders Carlssond553f8c2009-09-21 01:21:10 +00002632 break;
2633 }
2634
2635 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002636
Anders Carlsson50755b02009-09-27 20:11:34 +00002637 break;
Anders Carlssond553f8c2009-09-21 01:21:10 +00002638 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002639
Douglas Gregorc7793c72011-01-15 01:15:58 +00002640 case Expr::SubstNonTypeTemplateParmPackExprClass:
John McCall68a51a72011-07-01 00:04:39 +00002641 // FIXME: not clear how to mangle this!
2642 // template <unsigned N...> class A {
2643 // template <class U...> void foo(U (&x)[N]...);
2644 // };
2645 Out << "_SUBSTPACK_";
Douglas Gregorc7793c72011-01-15 01:15:58 +00002646 break;
2647
John McCall865d4472009-11-19 22:55:06 +00002648 case Expr::DependentScopeDeclRefExprClass: {
2649 const DependentScopeDeclRefExpr *DRE = cast<DependentScopeDeclRefExpr>(E);
John McCall26a6ec72011-06-21 22:12:46 +00002650 mangleUnresolvedName(DRE->getQualifier(), 0, DRE->getDeclName(), Arity);
Douglas Gregor4b2ccfc2010-02-28 22:05:49 +00002651
John McCall26a6ec72011-06-21 22:12:46 +00002652 // All the <unresolved-name> productions end in a
2653 // base-unresolved-name, where <template-args> are just tacked
2654 // onto the end.
John McCall6dbce192010-08-20 00:17:19 +00002655 if (DRE->hasExplicitTemplateArgs())
2656 mangleTemplateArgs(DRE->getExplicitTemplateArgs());
Anders Carlsson50755b02009-09-27 20:11:34 +00002657 break;
2658 }
2659
John McCalld9307602010-04-09 22:54:09 +00002660 case Expr::CXXBindTemporaryExprClass:
2661 mangleExpression(cast<CXXBindTemporaryExpr>(E)->getSubExpr());
2662 break;
2663
John McCall4765fa02010-12-06 08:20:24 +00002664 case Expr::ExprWithCleanupsClass:
2665 mangleExpression(cast<ExprWithCleanups>(E)->getSubExpr(), Arity);
John McCalld9307602010-04-09 22:54:09 +00002666 break;
2667
John McCall1dd73832010-02-04 01:42:13 +00002668 case Expr::FloatingLiteralClass: {
2669 const FloatingLiteral *FL = cast<FloatingLiteral>(E);
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002670 Out << 'L';
John McCall1dd73832010-02-04 01:42:13 +00002671 mangleType(FL->getType());
John McCall0512e482010-07-14 04:20:34 +00002672 mangleFloat(FL->getValue());
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002673 Out << 'E';
John McCall1dd73832010-02-04 01:42:13 +00002674 break;
2675 }
2676
John McCallde810632010-04-09 21:48:08 +00002677 case Expr::CharacterLiteralClass:
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002678 Out << 'L';
John McCallde810632010-04-09 21:48:08 +00002679 mangleType(E->getType());
2680 Out << cast<CharacterLiteral>(E)->getValue();
2681 Out << 'E';
2682 break;
2683
2684 case Expr::CXXBoolLiteralExprClass:
2685 Out << "Lb";
2686 Out << (cast<CXXBoolLiteralExpr>(E)->getValue() ? '1' : '0');
2687 Out << 'E';
2688 break;
2689
John McCall0512e482010-07-14 04:20:34 +00002690 case Expr::IntegerLiteralClass: {
2691 llvm::APSInt Value(cast<IntegerLiteral>(E)->getValue());
2692 if (E->getType()->isSignedIntegerType())
2693 Value.setIsSigned(true);
2694 mangleIntegerLiteral(E->getType(), Value);
Anders Carlssone170ba72009-12-14 01:45:37 +00002695 break;
John McCall0512e482010-07-14 04:20:34 +00002696 }
2697
2698 case Expr::ImaginaryLiteralClass: {
2699 const ImaginaryLiteral *IE = cast<ImaginaryLiteral>(E);
2700 // Mangle as if a complex literal.
Nick Lewycky271b6652010-09-05 03:40:33 +00002701 // Proposal from David Vandevoorde, 2010.06.30.
John McCall0512e482010-07-14 04:20:34 +00002702 Out << 'L';
2703 mangleType(E->getType());
2704 if (const FloatingLiteral *Imag =
2705 dyn_cast<FloatingLiteral>(IE->getSubExpr())) {
2706 // Mangle a floating-point zero of the appropriate type.
2707 mangleFloat(llvm::APFloat(Imag->getValue().getSemantics()));
2708 Out << '_';
2709 mangleFloat(Imag->getValue());
2710 } else {
Nick Lewycky271b6652010-09-05 03:40:33 +00002711 Out << "0_";
John McCall0512e482010-07-14 04:20:34 +00002712 llvm::APSInt Value(cast<IntegerLiteral>(IE->getSubExpr())->getValue());
2713 if (IE->getSubExpr()->getType()->isSignedIntegerType())
2714 Value.setIsSigned(true);
2715 mangleNumber(Value);
2716 }
2717 Out << 'E';
2718 break;
2719 }
2720
2721 case Expr::StringLiteralClass: {
John McCall1658c392010-07-15 21:53:03 +00002722 // Revised proposal from David Vandervoorde, 2010.07.15.
John McCall0512e482010-07-14 04:20:34 +00002723 Out << 'L';
John McCall1658c392010-07-15 21:53:03 +00002724 assert(isa<ConstantArrayType>(E->getType()));
2725 mangleType(E->getType());
John McCall0512e482010-07-14 04:20:34 +00002726 Out << 'E';
2727 break;
2728 }
2729
2730 case Expr::GNUNullExprClass:
2731 // FIXME: should this really be mangled the same as nullptr?
2732 // fallthrough
2733
2734 case Expr::CXXNullPtrLiteralExprClass: {
2735 // Proposal from David Vandervoorde, 2010.06.30, as
2736 // modified by ABI list discussion.
2737 Out << "LDnE";
2738 break;
2739 }
Douglas Gregorbe230c32011-01-03 17:17:50 +00002740
2741 case Expr::PackExpansionExprClass:
2742 Out << "sp";
2743 mangleExpression(cast<PackExpansionExpr>(E)->getPattern());
2744 break;
Douglas Gregor2e774c42011-01-04 18:56:13 +00002745
2746 case Expr::SizeOfPackExprClass: {
Douglas Gregor2e774c42011-01-04 18:56:13 +00002747 Out << "sZ";
2748 const NamedDecl *Pack = cast<SizeOfPackExpr>(E)->getPack();
2749 if (const TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(Pack))
2750 mangleTemplateParameter(TTP->getIndex());
2751 else if (const NonTypeTemplateParmDecl *NTTP
2752 = dyn_cast<NonTypeTemplateParmDecl>(Pack))
2753 mangleTemplateParameter(NTTP->getIndex());
2754 else if (const TemplateTemplateParmDecl *TempTP
2755 = dyn_cast<TemplateTemplateParmDecl>(Pack))
2756 mangleTemplateParameter(TempTP->getIndex());
Douglas Gregor91832362011-07-12 07:03:48 +00002757 else
2758 mangleFunctionParam(cast<ParmVarDecl>(Pack));
Douglas Gregordfbbcf92011-03-03 02:20:19 +00002759 break;
Douglas Gregor2e774c42011-01-04 18:56:13 +00002760 }
Douglas Gregor03e80032011-06-21 17:03:29 +00002761
2762 case Expr::MaterializeTemporaryExprClass: {
2763 mangleExpression(cast<MaterializeTemporaryExpr>(E)->GetTemporaryExpr());
2764 break;
2765 }
Anders Carlssond553f8c2009-09-21 01:21:10 +00002766 }
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00002767}
2768
John McCallfb44de92011-05-01 22:35:37 +00002769/// Mangle an expression which refers to a parameter variable.
2770///
2771/// <expression> ::= <function-param>
2772/// <function-param> ::= fp <top-level CV-qualifiers> _ # L == 0, I == 0
2773/// <function-param> ::= fp <top-level CV-qualifiers>
2774/// <parameter-2 non-negative number> _ # L == 0, I > 0
2775/// <function-param> ::= fL <L-1 non-negative number>
2776/// p <top-level CV-qualifiers> _ # L > 0, I == 0
2777/// <function-param> ::= fL <L-1 non-negative number>
2778/// p <top-level CV-qualifiers>
2779/// <I-1 non-negative number> _ # L > 0, I > 0
2780///
2781/// L is the nesting depth of the parameter, defined as 1 if the
2782/// parameter comes from the innermost function prototype scope
2783/// enclosing the current context, 2 if from the next enclosing
2784/// function prototype scope, and so on, with one special case: if
2785/// we've processed the full parameter clause for the innermost
2786/// function type, then L is one less. This definition conveniently
2787/// makes it irrelevant whether a function's result type was written
2788/// trailing or leading, but is otherwise overly complicated; the
2789/// numbering was first designed without considering references to
2790/// parameter in locations other than return types, and then the
2791/// mangling had to be generalized without changing the existing
2792/// manglings.
2793///
2794/// I is the zero-based index of the parameter within its parameter
2795/// declaration clause. Note that the original ABI document describes
2796/// this using 1-based ordinals.
2797void CXXNameMangler::mangleFunctionParam(const ParmVarDecl *parm) {
2798 unsigned parmDepth = parm->getFunctionScopeDepth();
2799 unsigned parmIndex = parm->getFunctionScopeIndex();
2800
2801 // Compute 'L'.
2802 // parmDepth does not include the declaring function prototype.
2803 // FunctionTypeDepth does account for that.
2804 assert(parmDepth < FunctionTypeDepth.getDepth());
2805 unsigned nestingDepth = FunctionTypeDepth.getDepth() - parmDepth;
2806 if (FunctionTypeDepth.isInResultType())
2807 nestingDepth--;
2808
2809 if (nestingDepth == 0) {
2810 Out << "fp";
2811 } else {
2812 Out << "fL" << (nestingDepth - 1) << 'p';
2813 }
2814
2815 // Top-level qualifiers. We don't have to worry about arrays here,
2816 // because parameters declared as arrays should already have been
2817 // tranformed to have pointer type. FIXME: apparently these don't
2818 // get mangled if used as an rvalue of a known non-class type?
2819 assert(!parm->getType()->isArrayType()
2820 && "parameter's type is still an array type?");
2821 mangleQualifiers(parm->getType().getQualifiers());
2822
2823 // Parameter index.
2824 if (parmIndex != 0) {
2825 Out << (parmIndex - 1);
2826 }
2827 Out << '_';
2828}
2829
Anders Carlsson3ac86b52009-04-15 05:36:58 +00002830void CXXNameMangler::mangleCXXCtorType(CXXCtorType T) {
2831 // <ctor-dtor-name> ::= C1 # complete object constructor
2832 // ::= C2 # base object constructor
2833 // ::= C3 # complete object allocating constructor
2834 //
2835 switch (T) {
2836 case Ctor_Complete:
2837 Out << "C1";
2838 break;
2839 case Ctor_Base:
2840 Out << "C2";
2841 break;
2842 case Ctor_CompleteAllocating:
2843 Out << "C3";
2844 break;
2845 }
2846}
2847
Anders Carlsson27ae5362009-04-17 01:58:57 +00002848void CXXNameMangler::mangleCXXDtorType(CXXDtorType T) {
2849 // <ctor-dtor-name> ::= D0 # deleting destructor
2850 // ::= D1 # complete object destructor
2851 // ::= D2 # base object destructor
2852 //
2853 switch (T) {
2854 case Dtor_Deleting:
2855 Out << "D0";
2856 break;
2857 case Dtor_Complete:
2858 Out << "D1";
2859 break;
2860 case Dtor_Base:
2861 Out << "D2";
2862 break;
2863 }
2864}
2865
John McCall6dbce192010-08-20 00:17:19 +00002866void CXXNameMangler::mangleTemplateArgs(
Argyrios Kyrtzidisb0c3e092011-09-22 20:07:03 +00002867 const ASTTemplateArgumentListInfo &TemplateArgs) {
John McCall6dbce192010-08-20 00:17:19 +00002868 // <template-args> ::= I <template-arg>+ E
2869 Out << 'I';
John McCall4f4e4132011-05-04 01:45:19 +00002870 for (unsigned i = 0, e = TemplateArgs.NumTemplateArgs; i != e; ++i)
2871 mangleTemplateArg(0, TemplateArgs.getTemplateArgs()[i].getArgument());
John McCall6dbce192010-08-20 00:17:19 +00002872 Out << 'E';
2873}
2874
Douglas Gregor20f0cc72010-04-23 03:10:43 +00002875void CXXNameMangler::mangleTemplateArgs(TemplateName Template,
2876 const TemplateArgument *TemplateArgs,
2877 unsigned NumTemplateArgs) {
2878 if (TemplateDecl *TD = Template.getAsTemplateDecl())
2879 return mangleTemplateArgs(*TD->getTemplateParameters(), TemplateArgs,
2880 NumTemplateArgs);
Sean Huntc3021132010-05-05 15:23:54 +00002881
John McCall4f4e4132011-05-04 01:45:19 +00002882 mangleUnresolvedTemplateArgs(TemplateArgs, NumTemplateArgs);
2883}
2884
2885void CXXNameMangler::mangleUnresolvedTemplateArgs(const TemplateArgument *args,
2886 unsigned numArgs) {
Douglas Gregor20f0cc72010-04-23 03:10:43 +00002887 // <template-args> ::= I <template-arg>+ E
2888 Out << 'I';
John McCall4f4e4132011-05-04 01:45:19 +00002889 for (unsigned i = 0; i != numArgs; ++i)
2890 mangleTemplateArg(0, args[i]);
Douglas Gregor20f0cc72010-04-23 03:10:43 +00002891 Out << 'E';
2892}
2893
Rafael Espindolad9800722010-03-11 14:07:00 +00002894void CXXNameMangler::mangleTemplateArgs(const TemplateParameterList &PL,
2895 const TemplateArgumentList &AL) {
Anders Carlsson7a0ba872009-05-15 16:09:15 +00002896 // <template-args> ::= I <template-arg>+ E
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002897 Out << 'I';
Rafael Espindolad9800722010-03-11 14:07:00 +00002898 for (unsigned i = 0, e = AL.size(); i != e; ++i)
2899 mangleTemplateArg(PL.getParam(i), AL[i]);
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002900 Out << 'E';
Anders Carlsson7a0ba872009-05-15 16:09:15 +00002901}
2902
Rafael Espindolad9800722010-03-11 14:07:00 +00002903void CXXNameMangler::mangleTemplateArgs(const TemplateParameterList &PL,
2904 const TemplateArgument *TemplateArgs,
Anders Carlsson7624f212009-09-18 02:42:01 +00002905 unsigned NumTemplateArgs) {
2906 // <template-args> ::= I <template-arg>+ E
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002907 Out << 'I';
Daniel Dunbar7e0c1952009-11-21 09:17:15 +00002908 for (unsigned i = 0; i != NumTemplateArgs; ++i)
Rafael Espindolad9800722010-03-11 14:07:00 +00002909 mangleTemplateArg(PL.getParam(i), TemplateArgs[i]);
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002910 Out << 'E';
Anders Carlsson7624f212009-09-18 02:42:01 +00002911}
2912
Rafael Espindolad9800722010-03-11 14:07:00 +00002913void CXXNameMangler::mangleTemplateArg(const NamedDecl *P,
Douglas Gregorf1588662011-07-12 15:18:55 +00002914 TemplateArgument A) {
Mike Stump1eb44332009-09-09 15:08:12 +00002915 // <template-arg> ::= <type> # type or template
Anders Carlsson7a0ba872009-05-15 16:09:15 +00002916 // ::= X <expression> E # expression
2917 // ::= <expr-primary> # simple expressions
Douglas Gregor4fc48662011-01-13 16:39:34 +00002918 // ::= J <template-arg>* E # argument pack
Douglas Gregorf1588662011-07-12 15:18:55 +00002919 // ::= sp <expression> # pack expansion of (C++0x)
2920 if (!A.isInstantiationDependent() || A.isDependent())
2921 A = Context.getASTContext().getCanonicalTemplateArgument(A);
2922
Anders Carlsson7a0ba872009-05-15 16:09:15 +00002923 switch (A.getKind()) {
Douglas Gregorf90b27a2011-01-03 22:36:02 +00002924 case TemplateArgument::Null:
2925 llvm_unreachable("Cannot mangle NULL template argument");
2926
Anders Carlsson7a0ba872009-05-15 16:09:15 +00002927 case TemplateArgument::Type:
2928 mangleType(A.getAsType());
2929 break;
Anders Carlsson9e85c742009-12-23 19:30:55 +00002930 case TemplateArgument::Template:
John McCallb6f532e2010-07-14 06:43:17 +00002931 // This is mangled as <type>.
2932 mangleType(A.getAsTemplate());
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002933 break;
Douglas Gregora7fc9012011-01-05 18:58:31 +00002934 case TemplateArgument::TemplateExpansion:
Douglas Gregor4fc48662011-01-13 16:39:34 +00002935 // <type> ::= Dp <type> # pack expansion (C++0x)
Douglas Gregora7fc9012011-01-05 18:58:31 +00002936 Out << "Dp";
2937 mangleType(A.getAsTemplateOrTemplatePattern());
2938 break;
John McCall092beef2012-01-06 05:06:35 +00002939 case TemplateArgument::Expression: {
2940 // It's possible to end up with a DeclRefExpr here in certain
2941 // dependent cases, in which case we should mangle as a
2942 // declaration.
2943 const Expr *E = A.getAsExpr()->IgnoreParens();
2944 if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E)) {
2945 const ValueDecl *D = DRE->getDecl();
2946 if (isa<VarDecl>(D) || isa<FunctionDecl>(D)) {
2947 Out << "L";
2948 mangle(D, "_Z");
2949 Out << 'E';
2950 break;
2951 }
2952 }
2953
Anders Carlssond553f8c2009-09-21 01:21:10 +00002954 Out << 'X';
John McCall092beef2012-01-06 05:06:35 +00002955 mangleExpression(E);
Anders Carlssond553f8c2009-09-21 01:21:10 +00002956 Out << 'E';
2957 break;
John McCall092beef2012-01-06 05:06:35 +00002958 }
Anders Carlssone170ba72009-12-14 01:45:37 +00002959 case TemplateArgument::Integral:
2960 mangleIntegerLiteral(A.getIntegralType(), *A.getAsIntegral());
Anders Carlsson7a0ba872009-05-15 16:09:15 +00002961 break;
Daniel Dunbar7e0c1952009-11-21 09:17:15 +00002962 case TemplateArgument::Declaration: {
Douglas Gregor20f0cc72010-04-23 03:10:43 +00002963 assert(P && "Missing template parameter for declaration argument");
Daniel Dunbar7e0c1952009-11-21 09:17:15 +00002964 // <expr-primary> ::= L <mangled-name> E # external name
2965
Rafael Espindolad9800722010-03-11 14:07:00 +00002966 // Clang produces AST's where pointer-to-member-function expressions
Daniel Dunbar7e0c1952009-11-21 09:17:15 +00002967 // and pointer-to-function expressions are represented as a declaration not
Rafael Espindolad9800722010-03-11 14:07:00 +00002968 // an expression. We compensate for it here to produce the correct mangling.
2969 NamedDecl *D = cast<NamedDecl>(A.getAsDecl());
2970 const NonTypeTemplateParmDecl *Parameter = cast<NonTypeTemplateParmDecl>(P);
John McCallc0a45592011-04-24 08:43:07 +00002971 bool compensateMangling = !Parameter->getType()->isReferenceType();
Rafael Espindolad9800722010-03-11 14:07:00 +00002972 if (compensateMangling) {
2973 Out << 'X';
2974 mangleOperatorName(OO_Amp, 1);
2975 }
2976
Daniel Dunbar7e0c1952009-11-21 09:17:15 +00002977 Out << 'L';
2978 // References to external entities use the mangled name; if the name would
2979 // not normally be manged then mangle it as unqualified.
2980 //
2981 // FIXME: The ABI specifies that external names here should have _Z, but
2982 // gcc leaves this off.
Rafael Espindolad9800722010-03-11 14:07:00 +00002983 if (compensateMangling)
2984 mangle(D, "_Z");
2985 else
2986 mangle(D, "Z");
Daniel Dunbar7e0c1952009-11-21 09:17:15 +00002987 Out << 'E';
Rafael Espindolad9800722010-03-11 14:07:00 +00002988
2989 if (compensateMangling)
2990 Out << 'E';
2991
Daniel Dunbar7e0c1952009-11-21 09:17:15 +00002992 break;
2993 }
Douglas Gregorf90b27a2011-01-03 22:36:02 +00002994
2995 case TemplateArgument::Pack: {
2996 // Note: proposal by Mike Herrick on 12/20/10
2997 Out << 'J';
2998 for (TemplateArgument::pack_iterator PA = A.pack_begin(),
2999 PAEnd = A.pack_end();
3000 PA != PAEnd; ++PA)
3001 mangleTemplateArg(P, *PA);
3002 Out << 'E';
3003 }
Daniel Dunbar7e0c1952009-11-21 09:17:15 +00003004 }
Anders Carlsson7a0ba872009-05-15 16:09:15 +00003005}
3006
Anders Carlsson0ccdf8d2009-09-27 00:38:53 +00003007void CXXNameMangler::mangleTemplateParameter(unsigned Index) {
3008 // <template-param> ::= T_ # first template parameter
3009 // ::= T <parameter-2 non-negative number> _
3010 if (Index == 0)
3011 Out << "T_";
3012 else
3013 Out << 'T' << (Index - 1) << '_';
3014}
3015
John McCall68a51a72011-07-01 00:04:39 +00003016void CXXNameMangler::mangleExistingSubstitution(QualType type) {
3017 bool result = mangleSubstitution(type);
3018 assert(result && "no existing substitution for type");
3019 (void) result;
3020}
3021
3022void CXXNameMangler::mangleExistingSubstitution(TemplateName tname) {
3023 bool result = mangleSubstitution(tname);
3024 assert(result && "no existing substitution for template name");
3025 (void) result;
3026}
3027
Anders Carlsson76967372009-09-17 00:43:46 +00003028// <substitution> ::= S <seq-id> _
3029// ::= S_
Anders Carlsson6862fc72009-09-17 04:16:28 +00003030bool CXXNameMangler::mangleSubstitution(const NamedDecl *ND) {
Anders Carlssone7c8cb62009-09-26 20:53:44 +00003031 // Try one of the standard substitutions first.
3032 if (mangleStandardSubstitution(ND))
3033 return true;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00003034
Anders Carlsson433d1372009-11-07 04:26:04 +00003035 ND = cast<NamedDecl>(ND->getCanonicalDecl());
Anders Carlsson6862fc72009-09-17 04:16:28 +00003036 return mangleSubstitution(reinterpret_cast<uintptr_t>(ND));
3037}
3038
Douglas Gregor14795c82011-12-03 18:24:43 +00003039/// \brief Determine whether the given type has any qualifiers that are
3040/// relevant for substitutions.
3041static bool hasMangledSubstitutionQualifiers(QualType T) {
3042 Qualifiers Qs = T.getQualifiers();
3043 return Qs.getCVRQualifiers() || Qs.hasAddressSpace();
3044}
3045
Anders Carlsson76967372009-09-17 00:43:46 +00003046bool CXXNameMangler::mangleSubstitution(QualType T) {
Douglas Gregor14795c82011-12-03 18:24:43 +00003047 if (!hasMangledSubstitutionQualifiers(T)) {
Anders Carlssond99edc42009-09-26 03:55:37 +00003048 if (const RecordType *RT = T->getAs<RecordType>())
3049 return mangleSubstitution(RT->getDecl());
3050 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00003051
Anders Carlsson76967372009-09-17 00:43:46 +00003052 uintptr_t TypePtr = reinterpret_cast<uintptr_t>(T.getAsOpaquePtr());
3053
Anders Carlssond3a932a2009-09-17 03:53:28 +00003054 return mangleSubstitution(TypePtr);
3055}
3056
Douglas Gregor1e9268e2010-04-28 05:58:56 +00003057bool CXXNameMangler::mangleSubstitution(TemplateName Template) {
3058 if (TemplateDecl *TD = Template.getAsTemplateDecl())
3059 return mangleSubstitution(TD);
Sean Huntc3021132010-05-05 15:23:54 +00003060
Douglas Gregor1e9268e2010-04-28 05:58:56 +00003061 Template = Context.getASTContext().getCanonicalTemplateName(Template);
3062 return mangleSubstitution(
3063 reinterpret_cast<uintptr_t>(Template.getAsVoidPointer()));
3064}
3065
Anders Carlssond3a932a2009-09-17 03:53:28 +00003066bool CXXNameMangler::mangleSubstitution(uintptr_t Ptr) {
Benjamin Kramer35f59b62010-04-10 16:03:31 +00003067 llvm::DenseMap<uintptr_t, unsigned>::iterator I = Substitutions.find(Ptr);
Anders Carlsson76967372009-09-17 00:43:46 +00003068 if (I == Substitutions.end())
3069 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00003070
Anders Carlsson76967372009-09-17 00:43:46 +00003071 unsigned SeqID = I->second;
3072 if (SeqID == 0)
3073 Out << "S_";
3074 else {
3075 SeqID--;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00003076
Anders Carlsson76967372009-09-17 00:43:46 +00003077 // <seq-id> is encoded in base-36, using digits and upper case letters.
3078 char Buffer[10];
Benjamin Kramer35f59b62010-04-10 16:03:31 +00003079 char *BufferPtr = llvm::array_endof(Buffer);
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00003080
Anders Carlsson76967372009-09-17 00:43:46 +00003081 if (SeqID == 0) *--BufferPtr = '0';
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00003082
Anders Carlsson76967372009-09-17 00:43:46 +00003083 while (SeqID) {
3084 assert(BufferPtr > Buffer && "Buffer overflow!");
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00003085
John McCall6ab30e02010-06-09 07:26:17 +00003086 char c = static_cast<char>(SeqID % 36);
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00003087
Anders Carlsson76967372009-09-17 00:43:46 +00003088 *--BufferPtr = (c < 10 ? '0' + c : 'A' + c - 10);
3089 SeqID /= 36;
3090 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00003091
Benjamin Kramer35f59b62010-04-10 16:03:31 +00003092 Out << 'S'
Chris Lattner5f9e2722011-07-23 10:55:15 +00003093 << StringRef(BufferPtr, llvm::array_endof(Buffer)-BufferPtr)
Benjamin Kramer35f59b62010-04-10 16:03:31 +00003094 << '_';
Anders Carlsson76967372009-09-17 00:43:46 +00003095 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00003096
Anders Carlsson76967372009-09-17 00:43:46 +00003097 return true;
3098}
3099
Anders Carlssonf514b542009-09-27 00:12:57 +00003100static bool isCharType(QualType T) {
3101 if (T.isNull())
3102 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00003103
Anders Carlssonf514b542009-09-27 00:12:57 +00003104 return T->isSpecificBuiltinType(BuiltinType::Char_S) ||
3105 T->isSpecificBuiltinType(BuiltinType::Char_U);
3106}
3107
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00003108/// isCharSpecialization - Returns whether a given type is a template
Anders Carlssonf514b542009-09-27 00:12:57 +00003109/// specialization of a given name with a single argument of type char.
3110static bool isCharSpecialization(QualType T, const char *Name) {
3111 if (T.isNull())
3112 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00003113
Anders Carlssonf514b542009-09-27 00:12:57 +00003114 const RecordType *RT = T->getAs<RecordType>();
3115 if (!RT)
3116 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00003117
3118 const ClassTemplateSpecializationDecl *SD =
Anders Carlssonf514b542009-09-27 00:12:57 +00003119 dyn_cast<ClassTemplateSpecializationDecl>(RT->getDecl());
3120 if (!SD)
3121 return false;
3122
3123 if (!isStdNamespace(SD->getDeclContext()))
3124 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00003125
Anders Carlssonf514b542009-09-27 00:12:57 +00003126 const TemplateArgumentList &TemplateArgs = SD->getTemplateArgs();
3127 if (TemplateArgs.size() != 1)
3128 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00003129
Anders Carlssonf514b542009-09-27 00:12:57 +00003130 if (!isCharType(TemplateArgs[0].getAsType()))
3131 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00003132
Daniel Dunbar01eb9b92009-10-18 21:17:35 +00003133 return SD->getIdentifier()->getName() == Name;
Anders Carlssonf514b542009-09-27 00:12:57 +00003134}
3135
Anders Carlsson91f88602009-12-07 19:56:42 +00003136template <std::size_t StrLen>
Benjamin Kramer54353f42010-11-25 18:29:30 +00003137static bool isStreamCharSpecialization(const ClassTemplateSpecializationDecl*SD,
3138 const char (&Str)[StrLen]) {
Anders Carlsson91f88602009-12-07 19:56:42 +00003139 if (!SD->getIdentifier()->isStr(Str))
3140 return false;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00003141
Anders Carlsson91f88602009-12-07 19:56:42 +00003142 const TemplateArgumentList &TemplateArgs = SD->getTemplateArgs();
3143 if (TemplateArgs.size() != 2)
3144 return false;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00003145
Anders Carlsson91f88602009-12-07 19:56:42 +00003146 if (!isCharType(TemplateArgs[0].getAsType()))
3147 return false;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00003148
Anders Carlsson91f88602009-12-07 19:56:42 +00003149 if (!isCharSpecialization(TemplateArgs[1].getAsType(), "char_traits"))
3150 return false;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00003151
Anders Carlsson91f88602009-12-07 19:56:42 +00003152 return true;
3153}
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00003154
Anders Carlssone7c8cb62009-09-26 20:53:44 +00003155bool CXXNameMangler::mangleStandardSubstitution(const NamedDecl *ND) {
3156 // <substitution> ::= St # ::std::
Anders Carlsson8c031552009-09-26 23:10:05 +00003157 if (const NamespaceDecl *NS = dyn_cast<NamespaceDecl>(ND)) {
Anders Carlsson47846d22009-12-04 06:23:23 +00003158 if (isStd(NS)) {
Anders Carlsson8c031552009-09-26 23:10:05 +00003159 Out << "St";
3160 return true;
3161 }
3162 }
3163
3164 if (const ClassTemplateDecl *TD = dyn_cast<ClassTemplateDecl>(ND)) {
3165 if (!isStdNamespace(TD->getDeclContext()))
3166 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00003167
Anders Carlsson8c031552009-09-26 23:10:05 +00003168 // <substitution> ::= Sa # ::std::allocator
3169 if (TD->getIdentifier()->isStr("allocator")) {
3170 Out << "Sa";
3171 return true;
3172 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00003173
Anders Carlsson189d59c2009-09-26 23:14:39 +00003174 // <<substitution> ::= Sb # ::std::basic_string
3175 if (TD->getIdentifier()->isStr("basic_string")) {
3176 Out << "Sb";
3177 return true;
3178 }
Anders Carlsson8c031552009-09-26 23:10:05 +00003179 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00003180
3181 if (const ClassTemplateSpecializationDecl *SD =
Anders Carlssonf514b542009-09-27 00:12:57 +00003182 dyn_cast<ClassTemplateSpecializationDecl>(ND)) {
Eli Friedman5370ee22010-02-23 18:25:09 +00003183 if (!isStdNamespace(SD->getDeclContext()))
3184 return false;
3185
Anders Carlssonf514b542009-09-27 00:12:57 +00003186 // <substitution> ::= Ss # ::std::basic_string<char,
3187 // ::std::char_traits<char>,
3188 // ::std::allocator<char> >
3189 if (SD->getIdentifier()->isStr("basic_string")) {
3190 const TemplateArgumentList &TemplateArgs = SD->getTemplateArgs();
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00003191
Anders Carlssonf514b542009-09-27 00:12:57 +00003192 if (TemplateArgs.size() != 3)
3193 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00003194
Anders Carlssonf514b542009-09-27 00:12:57 +00003195 if (!isCharType(TemplateArgs[0].getAsType()))
3196 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00003197
Anders Carlssonf514b542009-09-27 00:12:57 +00003198 if (!isCharSpecialization(TemplateArgs[1].getAsType(), "char_traits"))
3199 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00003200
Anders Carlssonf514b542009-09-27 00:12:57 +00003201 if (!isCharSpecialization(TemplateArgs[2].getAsType(), "allocator"))
3202 return false;
3203
3204 Out << "Ss";
3205 return true;
3206 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00003207
Anders Carlsson91f88602009-12-07 19:56:42 +00003208 // <substitution> ::= Si # ::std::basic_istream<char,
3209 // ::std::char_traits<char> >
3210 if (isStreamCharSpecialization(SD, "basic_istream")) {
3211 Out << "Si";
3212 return true;
3213 }
3214
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00003215 // <substitution> ::= So # ::std::basic_ostream<char,
Anders Carlsson8f8fd8e2009-10-08 17:20:26 +00003216 // ::std::char_traits<char> >
Anders Carlsson91f88602009-12-07 19:56:42 +00003217 if (isStreamCharSpecialization(SD, "basic_ostream")) {
Anders Carlsson8f8fd8e2009-10-08 17:20:26 +00003218 Out << "So";
3219 return true;
3220 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00003221
Anders Carlsson91f88602009-12-07 19:56:42 +00003222 // <substitution> ::= Sd # ::std::basic_iostream<char,
3223 // ::std::char_traits<char> >
3224 if (isStreamCharSpecialization(SD, "basic_iostream")) {
3225 Out << "Sd";
3226 return true;
3227 }
Anders Carlssonf514b542009-09-27 00:12:57 +00003228 }
Anders Carlsson8c031552009-09-26 23:10:05 +00003229 return false;
Anders Carlssone7c8cb62009-09-26 20:53:44 +00003230}
3231
Anders Carlsson76967372009-09-17 00:43:46 +00003232void CXXNameMangler::addSubstitution(QualType T) {
Douglas Gregor14795c82011-12-03 18:24:43 +00003233 if (!hasMangledSubstitutionQualifiers(T)) {
Anders Carlssond99edc42009-09-26 03:55:37 +00003234 if (const RecordType *RT = T->getAs<RecordType>()) {
3235 addSubstitution(RT->getDecl());
3236 return;
3237 }
3238 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00003239
Anders Carlsson76967372009-09-17 00:43:46 +00003240 uintptr_t TypePtr = reinterpret_cast<uintptr_t>(T.getAsOpaquePtr());
Anders Carlssond3a932a2009-09-17 03:53:28 +00003241 addSubstitution(TypePtr);
3242}
3243
Douglas Gregor1e9268e2010-04-28 05:58:56 +00003244void CXXNameMangler::addSubstitution(TemplateName Template) {
3245 if (TemplateDecl *TD = Template.getAsTemplateDecl())
3246 return addSubstitution(TD);
Sean Huntc3021132010-05-05 15:23:54 +00003247
Douglas Gregor1e9268e2010-04-28 05:58:56 +00003248 Template = Context.getASTContext().getCanonicalTemplateName(Template);
3249 addSubstitution(reinterpret_cast<uintptr_t>(Template.getAsVoidPointer()));
3250}
3251
Anders Carlssond3a932a2009-09-17 03:53:28 +00003252void CXXNameMangler::addSubstitution(uintptr_t Ptr) {
Anders Carlssond3a932a2009-09-17 03:53:28 +00003253 assert(!Substitutions.count(Ptr) && "Substitution already exists!");
Anders Carlsson9d85b722010-06-02 04:29:50 +00003254 Substitutions[Ptr] = SeqID++;
Anders Carlsson76967372009-09-17 00:43:46 +00003255}
3256
Daniel Dunbar1b077112009-11-21 09:06:10 +00003257//
Mike Stump1eb44332009-09-09 15:08:12 +00003258
Daniel Dunbar1b077112009-11-21 09:06:10 +00003259/// \brief Mangles the name of the declaration D and emits that name to the
3260/// given output stream.
3261///
3262/// If the declaration D requires a mangled name, this routine will emit that
3263/// mangled name to \p os and return true. Otherwise, \p os will be unchanged
3264/// and this routine will return false. In this case, the caller should just
3265/// emit the identifier of the declaration (\c D->getIdentifier()) as its
3266/// name.
Peter Collingbourne14110472011-01-13 18:57:25 +00003267void ItaniumMangleContext::mangleName(const NamedDecl *D,
Chris Lattner5f9e2722011-07-23 10:55:15 +00003268 raw_ostream &Out) {
Daniel Dunbarc02ab4c2009-11-21 09:14:44 +00003269 assert((isa<FunctionDecl>(D) || isa<VarDecl>(D)) &&
3270 "Invalid mangleName() call, argument is not a variable or function!");
3271 assert(!isa<CXXConstructorDecl>(D) && !isa<CXXDestructorDecl>(D) &&
3272 "Invalid mangleName() call on 'structor decl!");
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00003273
Daniel Dunbar1b077112009-11-21 09:06:10 +00003274 PrettyStackTraceDecl CrashInfo(D, SourceLocation(),
3275 getASTContext().getSourceManager(),
3276 "Mangling declaration");
Mike Stump1eb44332009-09-09 15:08:12 +00003277
John McCallfb44de92011-05-01 22:35:37 +00003278 CXXNameMangler Mangler(*this, Out, D);
Daniel Dunbar94fd26d2009-11-21 09:06:22 +00003279 return Mangler.mangle(D);
Daniel Dunbar1b077112009-11-21 09:06:10 +00003280}
Mike Stump1eb44332009-09-09 15:08:12 +00003281
Peter Collingbourne14110472011-01-13 18:57:25 +00003282void ItaniumMangleContext::mangleCXXCtor(const CXXConstructorDecl *D,
3283 CXXCtorType Type,
Chris Lattner5f9e2722011-07-23 10:55:15 +00003284 raw_ostream &Out) {
Rafael Espindolac4850c22011-02-10 23:59:36 +00003285 CXXNameMangler Mangler(*this, Out, D, Type);
Daniel Dunbar77939c92009-11-21 09:06:31 +00003286 Mangler.mangle(D);
Daniel Dunbar1b077112009-11-21 09:06:10 +00003287}
Mike Stump1eb44332009-09-09 15:08:12 +00003288
Peter Collingbourne14110472011-01-13 18:57:25 +00003289void ItaniumMangleContext::mangleCXXDtor(const CXXDestructorDecl *D,
3290 CXXDtorType Type,
Chris Lattner5f9e2722011-07-23 10:55:15 +00003291 raw_ostream &Out) {
Rafael Espindolac4850c22011-02-10 23:59:36 +00003292 CXXNameMangler Mangler(*this, Out, D, Type);
Daniel Dunbar77939c92009-11-21 09:06:31 +00003293 Mangler.mangle(D);
Daniel Dunbar1b077112009-11-21 09:06:10 +00003294}
Mike Stumpf1216772009-07-31 18:25:34 +00003295
Peter Collingbourne14110472011-01-13 18:57:25 +00003296void ItaniumMangleContext::mangleThunk(const CXXMethodDecl *MD,
3297 const ThunkInfo &Thunk,
Chris Lattner5f9e2722011-07-23 10:55:15 +00003298 raw_ostream &Out) {
Anders Carlsson19879c92010-03-23 17:17:29 +00003299 // <special-name> ::= T <call-offset> <base encoding>
3300 // # base is the nominal target function of thunk
3301 // <special-name> ::= Tc <call-offset> <call-offset> <base encoding>
3302 // # base is the nominal target function of thunk
3303 // # first call-offset is 'this' adjustment
3304 // # second call-offset is result adjustment
Sean Huntc3021132010-05-05 15:23:54 +00003305
Anders Carlsson19879c92010-03-23 17:17:29 +00003306 assert(!isa<CXXDestructorDecl>(MD) &&
3307 "Use mangleCXXDtor for destructor decls!");
Rafael Espindolac4850c22011-02-10 23:59:36 +00003308 CXXNameMangler Mangler(*this, Out);
Anders Carlsson19879c92010-03-23 17:17:29 +00003309 Mangler.getStream() << "_ZT";
3310 if (!Thunk.Return.isEmpty())
3311 Mangler.getStream() << 'c';
Sean Huntc3021132010-05-05 15:23:54 +00003312
Anders Carlsson19879c92010-03-23 17:17:29 +00003313 // Mangle the 'this' pointer adjustment.
3314 Mangler.mangleCallOffset(Thunk.This.NonVirtual, Thunk.This.VCallOffsetOffset);
Sean Huntc3021132010-05-05 15:23:54 +00003315
Anders Carlsson19879c92010-03-23 17:17:29 +00003316 // Mangle the return pointer adjustment if there is one.
3317 if (!Thunk.Return.isEmpty())
3318 Mangler.mangleCallOffset(Thunk.Return.NonVirtual,
3319 Thunk.Return.VBaseOffsetOffset);
Sean Huntc3021132010-05-05 15:23:54 +00003320
Anders Carlsson19879c92010-03-23 17:17:29 +00003321 Mangler.mangleFunctionEncoding(MD);
3322}
3323
Sean Huntc3021132010-05-05 15:23:54 +00003324void
Peter Collingbourne14110472011-01-13 18:57:25 +00003325ItaniumMangleContext::mangleCXXDtorThunk(const CXXDestructorDecl *DD,
3326 CXXDtorType Type,
3327 const ThisAdjustment &ThisAdjustment,
Chris Lattner5f9e2722011-07-23 10:55:15 +00003328 raw_ostream &Out) {
Anders Carlsson19879c92010-03-23 17:17:29 +00003329 // <special-name> ::= T <call-offset> <base encoding>
3330 // # base is the nominal target function of thunk
Rafael Espindolac4850c22011-02-10 23:59:36 +00003331 CXXNameMangler Mangler(*this, Out, DD, Type);
Anders Carlsson19879c92010-03-23 17:17:29 +00003332 Mangler.getStream() << "_ZT";
3333
3334 // Mangle the 'this' pointer adjustment.
Sean Huntc3021132010-05-05 15:23:54 +00003335 Mangler.mangleCallOffset(ThisAdjustment.NonVirtual,
Anders Carlsson19879c92010-03-23 17:17:29 +00003336 ThisAdjustment.VCallOffsetOffset);
3337
3338 Mangler.mangleFunctionEncoding(DD);
3339}
3340
Daniel Dunbarc0747712009-11-21 09:12:13 +00003341/// mangleGuardVariable - Returns the mangled name for a guard variable
3342/// for the passed in VarDecl.
Peter Collingbourne14110472011-01-13 18:57:25 +00003343void ItaniumMangleContext::mangleItaniumGuardVariable(const VarDecl *D,
Chris Lattner5f9e2722011-07-23 10:55:15 +00003344 raw_ostream &Out) {
Daniel Dunbarc0747712009-11-21 09:12:13 +00003345 // <special-name> ::= GV <object name> # Guard variable for one-time
3346 // # initialization
Rafael Espindolac4850c22011-02-10 23:59:36 +00003347 CXXNameMangler Mangler(*this, Out);
Daniel Dunbarc0747712009-11-21 09:12:13 +00003348 Mangler.getStream() << "_ZGV";
3349 Mangler.mangleName(D);
3350}
3351
Peter Collingbourne14110472011-01-13 18:57:25 +00003352void ItaniumMangleContext::mangleReferenceTemporary(const VarDecl *D,
Chris Lattner5f9e2722011-07-23 10:55:15 +00003353 raw_ostream &Out) {
Anders Carlsson715edf22010-06-26 16:09:40 +00003354 // We match the GCC mangling here.
3355 // <special-name> ::= GR <object name>
Rafael Espindolac4850c22011-02-10 23:59:36 +00003356 CXXNameMangler Mangler(*this, Out);
Anders Carlsson715edf22010-06-26 16:09:40 +00003357 Mangler.getStream() << "_ZGR";
3358 Mangler.mangleName(D);
3359}
3360
Peter Collingbourne14110472011-01-13 18:57:25 +00003361void ItaniumMangleContext::mangleCXXVTable(const CXXRecordDecl *RD,
Chris Lattner5f9e2722011-07-23 10:55:15 +00003362 raw_ostream &Out) {
Daniel Dunbarc0747712009-11-21 09:12:13 +00003363 // <special-name> ::= TV <type> # virtual table
Rafael Espindolac4850c22011-02-10 23:59:36 +00003364 CXXNameMangler Mangler(*this, Out);
Daniel Dunbarc0747712009-11-21 09:12:13 +00003365 Mangler.getStream() << "_ZTV";
Douglas Gregor1b12a3b2010-05-26 05:11:13 +00003366 Mangler.mangleNameOrStandardSubstitution(RD);
Daniel Dunbar1b077112009-11-21 09:06:10 +00003367}
Mike Stump82d75b02009-11-10 01:58:37 +00003368
Peter Collingbourne14110472011-01-13 18:57:25 +00003369void ItaniumMangleContext::mangleCXXVTT(const CXXRecordDecl *RD,
Chris Lattner5f9e2722011-07-23 10:55:15 +00003370 raw_ostream &Out) {
Daniel Dunbarc0747712009-11-21 09:12:13 +00003371 // <special-name> ::= TT <type> # VTT structure
Rafael Espindolac4850c22011-02-10 23:59:36 +00003372 CXXNameMangler Mangler(*this, Out);
Daniel Dunbarc0747712009-11-21 09:12:13 +00003373 Mangler.getStream() << "_ZTT";
Douglas Gregor1b12a3b2010-05-26 05:11:13 +00003374 Mangler.mangleNameOrStandardSubstitution(RD);
Daniel Dunbar1b077112009-11-21 09:06:10 +00003375}
Mike Stumpab3f7e92009-11-10 01:41:59 +00003376
Peter Collingbourne14110472011-01-13 18:57:25 +00003377void ItaniumMangleContext::mangleCXXCtorVTable(const CXXRecordDecl *RD,
3378 int64_t Offset,
3379 const CXXRecordDecl *Type,
Chris Lattner5f9e2722011-07-23 10:55:15 +00003380 raw_ostream &Out) {
Daniel Dunbarc0747712009-11-21 09:12:13 +00003381 // <special-name> ::= TC <type> <offset number> _ <base type>
Rafael Espindolac4850c22011-02-10 23:59:36 +00003382 CXXNameMangler Mangler(*this, Out);
Daniel Dunbarc0747712009-11-21 09:12:13 +00003383 Mangler.getStream() << "_ZTC";
Douglas Gregor1b12a3b2010-05-26 05:11:13 +00003384 Mangler.mangleNameOrStandardSubstitution(RD);
Daniel Dunbarc0747712009-11-21 09:12:13 +00003385 Mangler.getStream() << Offset;
Benjamin Kramer35f59b62010-04-10 16:03:31 +00003386 Mangler.getStream() << '_';
Douglas Gregor1b12a3b2010-05-26 05:11:13 +00003387 Mangler.mangleNameOrStandardSubstitution(Type);
Daniel Dunbar1b077112009-11-21 09:06:10 +00003388}
Mike Stump738f8c22009-07-31 23:15:31 +00003389
Peter Collingbourne14110472011-01-13 18:57:25 +00003390void ItaniumMangleContext::mangleCXXRTTI(QualType Ty,
Chris Lattner5f9e2722011-07-23 10:55:15 +00003391 raw_ostream &Out) {
Daniel Dunbarc0747712009-11-21 09:12:13 +00003392 // <special-name> ::= TI <type> # typeinfo structure
Douglas Gregor154fe982009-12-23 22:04:40 +00003393 assert(!Ty.hasQualifiers() && "RTTI info cannot have top-level qualifiers");
Rafael Espindolac4850c22011-02-10 23:59:36 +00003394 CXXNameMangler Mangler(*this, Out);
Daniel Dunbarc0747712009-11-21 09:12:13 +00003395 Mangler.getStream() << "_ZTI";
3396 Mangler.mangleType(Ty);
Daniel Dunbar1b077112009-11-21 09:06:10 +00003397}
Mike Stump67795982009-11-14 00:14:13 +00003398
Peter Collingbourne14110472011-01-13 18:57:25 +00003399void ItaniumMangleContext::mangleCXXRTTIName(QualType Ty,
Chris Lattner5f9e2722011-07-23 10:55:15 +00003400 raw_ostream &Out) {
Daniel Dunbarc0747712009-11-21 09:12:13 +00003401 // <special-name> ::= TS <type> # typeinfo name (null terminated byte string)
Rafael Espindolac4850c22011-02-10 23:59:36 +00003402 CXXNameMangler Mangler(*this, Out);
Daniel Dunbarc0747712009-11-21 09:12:13 +00003403 Mangler.getStream() << "_ZTS";
3404 Mangler.mangleType(Ty);
Mike Stumpf1216772009-07-31 18:25:34 +00003405}
Peter Collingbourne14110472011-01-13 18:57:25 +00003406
3407MangleContext *clang::createItaniumMangleContext(ASTContext &Context,
David Blaikied6471f72011-09-25 23:23:43 +00003408 DiagnosticsEngine &Diags) {
Peter Collingbourne14110472011-01-13 18:57:25 +00003409 return new ItaniumMangleContext(Context, Diags);
3410}