blob: 566a3894cb97a44a2350dd45b4f4955c9e5ea707 [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"
Benjamin Kramer2fa67ef2012-12-01 15:09:41 +000019#include "clang/AST/Attr.h"
Douglas Gregor5f2bfd42009-02-13 00:10:09 +000020#include "clang/AST/Decl.h"
21#include "clang/AST/DeclCXX.h"
Anders Carlssona40c5e42009-03-07 22:03:21 +000022#include "clang/AST/DeclObjC.h"
Anders Carlsson7a0ba872009-05-15 16:09:15 +000023#include "clang/AST/DeclTemplate.h"
Anders Carlsson50755b02009-09-27 20:11:34 +000024#include "clang/AST/ExprCXX.h"
John McCallf85e1932011-06-15 23:02:42 +000025#include "clang/AST/ExprObjC.h"
John McCallfb44de92011-05-01 22:35:37 +000026#include "clang/AST/TypeLoc.h"
Peter Collingbourne14110472011-01-13 18:57:25 +000027#include "clang/Basic/ABI.h"
Douglas Gregor6ec36682009-02-18 23:53:56 +000028#include "clang/Basic/SourceManager.h"
Rafael Espindola4e274e92011-02-15 22:23:51 +000029#include "clang/Basic/TargetInfo.h"
Anders Carlssonc4355b62009-10-07 01:45:02 +000030#include "llvm/ADT/StringExtras.h"
John McCallefe6aee2009-09-05 07:56:18 +000031#include "llvm/Support/ErrorHandling.h"
Benjamin Kramer2fa67ef2012-12-01 15:09:41 +000032#include "llvm/Support/raw_ostream.h"
Anders Carlssonf98574b2010-02-05 07:31:37 +000033
34#define MANGLE_CHECKER 0
35
36#if MANGLE_CHECKER
37#include <cxxabi.h>
38#endif
39
Douglas Gregor5f2bfd42009-02-13 00:10:09 +000040using namespace clang;
Charles Davis685b1d92010-05-26 18:25:27 +000041
Douglas Gregor5f2bfd42009-02-13 00:10:09 +000042namespace {
Fariborz Jahanian57058532010-03-03 19:41:08 +000043
Douglas Gregorccc1b5e2012-02-21 00:37:24 +000044/// \brief Retrieve the declaration context that should be used when mangling
45/// the given declaration.
46static const DeclContext *getEffectiveDeclContext(const Decl *D) {
47 // The ABI assumes that lambda closure types that occur within
48 // default arguments live in the context of the function. However, due to
49 // the way in which Clang parses and creates function declarations, this is
50 // not the case: the lambda closure type ends up living in the context
51 // where the function itself resides, because the function declaration itself
52 // had not yet been created. Fix the context here.
53 if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D)) {
54 if (RD->isLambda())
55 if (ParmVarDecl *ContextParam
Douglas Gregor5878cbc2012-02-21 04:17:39 +000056 = dyn_cast_or_null<ParmVarDecl>(RD->getLambdaContextDecl()))
Douglas Gregorccc1b5e2012-02-21 00:37:24 +000057 return ContextParam->getDeclContext();
58 }
59
60 return D->getDeclContext();
61}
62
63static const DeclContext *getEffectiveParentContext(const DeclContext *DC) {
64 return getEffectiveDeclContext(cast<Decl>(DC));
65}
66
John McCall82b7d7b2010-10-18 21:28:44 +000067static const CXXRecordDecl *GetLocalClassDecl(const NamedDecl *ND) {
68 const DeclContext *DC = dyn_cast<DeclContext>(ND);
69 if (!DC)
Douglas Gregorccc1b5e2012-02-21 00:37:24 +000070 DC = getEffectiveDeclContext(ND);
John McCall82b7d7b2010-10-18 21:28:44 +000071 while (!DC->isNamespace() && !DC->isTranslationUnit()) {
Douglas Gregorccc1b5e2012-02-21 00:37:24 +000072 const DeclContext *Parent = getEffectiveDeclContext(cast<Decl>(DC));
73 if (isa<FunctionDecl>(Parent))
John McCall82b7d7b2010-10-18 21:28:44 +000074 return dyn_cast<CXXRecordDecl>(DC);
Douglas Gregorccc1b5e2012-02-21 00:37:24 +000075 DC = Parent;
Fariborz Jahanian57058532010-03-03 19:41:08 +000076 }
77 return 0;
78}
79
John McCallfb44de92011-05-01 22:35:37 +000080static const FunctionDecl *getStructor(const FunctionDecl *fn) {
81 if (const FunctionTemplateDecl *ftd = fn->getPrimaryTemplate())
82 return ftd->getTemplatedDecl();
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +000083
John McCallfb44de92011-05-01 22:35:37 +000084 return fn;
85}
Anders Carlsson7e120032009-11-24 05:36:32 +000086
John McCallfb44de92011-05-01 22:35:37 +000087static const NamedDecl *getStructor(const NamedDecl *decl) {
88 const FunctionDecl *fn = dyn_cast_or_null<FunctionDecl>(decl);
89 return (fn ? getStructor(fn) : decl);
Anders Carlsson7e120032009-11-24 05:36:32 +000090}
Douglas Gregorccc1b5e2012-02-21 00:37:24 +000091
John McCall1dd73832010-02-04 01:42:13 +000092static const unsigned UnknownArity = ~0U;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +000093
Peter Collingbourne14110472011-01-13 18:57:25 +000094class ItaniumMangleContext : public MangleContext {
95 llvm::DenseMap<const TagDecl *, uint64_t> AnonStructIds;
96 unsigned Discriminator;
97 llvm::DenseMap<const NamedDecl*, unsigned> Uniquifier;
98
99public:
100 explicit ItaniumMangleContext(ASTContext &Context,
David Blaikied6471f72011-09-25 23:23:43 +0000101 DiagnosticsEngine &Diags)
Peter Collingbourne14110472011-01-13 18:57:25 +0000102 : MangleContext(Context, Diags) { }
103
104 uint64_t getAnonymousStructId(const TagDecl *TD) {
105 std::pair<llvm::DenseMap<const TagDecl *,
106 uint64_t>::iterator, bool> Result =
107 AnonStructIds.insert(std::make_pair(TD, AnonStructIds.size()));
108 return Result.first->second;
109 }
110
111 void startNewFunction() {
112 MangleContext::startNewFunction();
113 mangleInitDiscriminator();
114 }
115
116 /// @name Mangler Entry Points
117 /// @{
118
119 bool shouldMangleDeclName(const NamedDecl *D);
Chris Lattner5f9e2722011-07-23 10:55:15 +0000120 void mangleName(const NamedDecl *D, raw_ostream &);
Peter Collingbourne14110472011-01-13 18:57:25 +0000121 void mangleThunk(const CXXMethodDecl *MD,
122 const ThunkInfo &Thunk,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000123 raw_ostream &);
Peter Collingbourne14110472011-01-13 18:57:25 +0000124 void mangleCXXDtorThunk(const CXXDestructorDecl *DD, CXXDtorType Type,
125 const ThisAdjustment &ThisAdjustment,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000126 raw_ostream &);
Peter Collingbourne14110472011-01-13 18:57:25 +0000127 void mangleReferenceTemporary(const VarDecl *D,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000128 raw_ostream &);
Peter Collingbourne14110472011-01-13 18:57:25 +0000129 void mangleCXXVTable(const CXXRecordDecl *RD,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000130 raw_ostream &);
Peter Collingbourne14110472011-01-13 18:57:25 +0000131 void mangleCXXVTT(const CXXRecordDecl *RD,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000132 raw_ostream &);
Peter Collingbourne14110472011-01-13 18:57:25 +0000133 void mangleCXXCtorVTable(const CXXRecordDecl *RD, int64_t Offset,
134 const CXXRecordDecl *Type,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000135 raw_ostream &);
136 void mangleCXXRTTI(QualType T, raw_ostream &);
137 void mangleCXXRTTIName(QualType T, raw_ostream &);
Peter Collingbourne14110472011-01-13 18:57:25 +0000138 void mangleCXXCtor(const CXXConstructorDecl *D, CXXCtorType Type,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000139 raw_ostream &);
Peter Collingbourne14110472011-01-13 18:57:25 +0000140 void mangleCXXDtor(const CXXDestructorDecl *D, CXXDtorType Type,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000141 raw_ostream &);
Peter Collingbourne14110472011-01-13 18:57:25 +0000142
Chris Lattner5f9e2722011-07-23 10:55:15 +0000143 void mangleItaniumGuardVariable(const VarDecl *D, raw_ostream &);
Peter Collingbourne14110472011-01-13 18:57:25 +0000144
145 void mangleInitDiscriminator() {
146 Discriminator = 0;
147 }
148
149 bool getNextDiscriminator(const NamedDecl *ND, unsigned &disc) {
Douglas Gregor9e8c92a2012-02-20 19:44:39 +0000150 // Lambda closure types with external linkage (indicated by a
151 // non-zero lambda mangling number) have their own numbering scheme, so
152 // they do not need a discriminator.
153 if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(ND))
154 if (RD->isLambda() && RD->getLambdaManglingNumber() > 0)
155 return false;
156
Peter Collingbourne14110472011-01-13 18:57:25 +0000157 unsigned &discriminator = Uniquifier[ND];
158 if (!discriminator)
159 discriminator = ++Discriminator;
160 if (discriminator == 1)
161 return false;
162 disc = discriminator-2;
163 return true;
164 }
165 /// @}
166};
167
Daniel Dunbar1b077112009-11-21 09:06:10 +0000168/// CXXNameMangler - Manage the mangling of a single name.
Daniel Dunbarc0747712009-11-21 09:12:13 +0000169class CXXNameMangler {
Peter Collingbourne14110472011-01-13 18:57:25 +0000170 ItaniumMangleContext &Context;
Chris Lattner5f9e2722011-07-23 10:55:15 +0000171 raw_ostream &Out;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000172
John McCallfb44de92011-05-01 22:35:37 +0000173 /// The "structor" is the top-level declaration being mangled, if
174 /// that's not a template specialization; otherwise it's the pattern
175 /// for that specialization.
176 const NamedDecl *Structor;
Daniel Dunbar1b077112009-11-21 09:06:10 +0000177 unsigned StructorType;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000178
Anders Carlsson9d85b722010-06-02 04:29:50 +0000179 /// SeqID - The next subsitution sequence number.
180 unsigned SeqID;
181
John McCallfb44de92011-05-01 22:35:37 +0000182 class FunctionTypeDepthState {
183 unsigned Bits;
184
185 enum { InResultTypeMask = 1 };
186
187 public:
188 FunctionTypeDepthState() : Bits(0) {}
189
190 /// The number of function types we're inside.
191 unsigned getDepth() const {
192 return Bits >> 1;
193 }
194
195 /// True if we're in the return type of the innermost function type.
196 bool isInResultType() const {
197 return Bits & InResultTypeMask;
198 }
199
200 FunctionTypeDepthState push() {
201 FunctionTypeDepthState tmp = *this;
202 Bits = (Bits & ~InResultTypeMask) + 2;
203 return tmp;
204 }
205
206 void enterResultType() {
207 Bits |= InResultTypeMask;
208 }
209
210 void leaveResultType() {
211 Bits &= ~InResultTypeMask;
212 }
213
214 void pop(FunctionTypeDepthState saved) {
215 assert(getDepth() == saved.getDepth() + 1);
216 Bits = saved.Bits;
217 }
218
219 } FunctionTypeDepth;
220
Daniel Dunbar1b077112009-11-21 09:06:10 +0000221 llvm::DenseMap<uintptr_t, unsigned> Substitutions;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000222
John McCall1dd73832010-02-04 01:42:13 +0000223 ASTContext &getASTContext() const { return Context.getASTContext(); }
224
Daniel Dunbarc0747712009-11-21 09:12:13 +0000225public:
Chris Lattner5f9e2722011-07-23 10:55:15 +0000226 CXXNameMangler(ItaniumMangleContext &C, raw_ostream &Out_,
John McCallfb44de92011-05-01 22:35:37 +0000227 const NamedDecl *D = 0)
228 : Context(C), Out(Out_), Structor(getStructor(D)), StructorType(0),
229 SeqID(0) {
230 // These can't be mangled without a ctor type or dtor type.
231 assert(!D || (!isa<CXXDestructorDecl>(D) &&
232 !isa<CXXConstructorDecl>(D)));
233 }
Chris Lattner5f9e2722011-07-23 10:55:15 +0000234 CXXNameMangler(ItaniumMangleContext &C, raw_ostream &Out_,
Daniel Dunbar77939c92009-11-21 09:06:31 +0000235 const CXXConstructorDecl *D, CXXCtorType Type)
Rafael Espindolac4850c22011-02-10 23:59:36 +0000236 : Context(C), Out(Out_), Structor(getStructor(D)), StructorType(Type),
John McCallfb44de92011-05-01 22:35:37 +0000237 SeqID(0) { }
Chris Lattner5f9e2722011-07-23 10:55:15 +0000238 CXXNameMangler(ItaniumMangleContext &C, raw_ostream &Out_,
Daniel Dunbar77939c92009-11-21 09:06:31 +0000239 const CXXDestructorDecl *D, CXXDtorType Type)
Rafael Espindolac4850c22011-02-10 23:59:36 +0000240 : Context(C), Out(Out_), Structor(getStructor(D)), StructorType(Type),
John McCallfb44de92011-05-01 22:35:37 +0000241 SeqID(0) { }
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000242
Anders Carlssonf98574b2010-02-05 07:31:37 +0000243#if MANGLE_CHECKER
244 ~CXXNameMangler() {
245 if (Out.str()[0] == '\01')
246 return;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000247
Anders Carlssonf98574b2010-02-05 07:31:37 +0000248 int status = 0;
249 char *result = abi::__cxa_demangle(Out.str().str().c_str(), 0, 0, &status);
250 assert(status == 0 && "Could not demangle mangled name!");
251 free(result);
252 }
253#endif
Chris Lattner5f9e2722011-07-23 10:55:15 +0000254 raw_ostream &getStream() { return Out; }
Daniel Dunbarc0747712009-11-21 09:12:13 +0000255
Chris Lattner5f9e2722011-07-23 10:55:15 +0000256 void mangle(const NamedDecl *D, StringRef Prefix = "_Z");
Anders Carlsson19879c92010-03-23 17:17:29 +0000257 void mangleCallOffset(int64_t NonVirtual, int64_t Virtual);
John McCall0512e482010-07-14 04:20:34 +0000258 void mangleNumber(const llvm::APSInt &I);
Anders Carlssona94822e2009-11-26 02:32:05 +0000259 void mangleNumber(int64_t Number);
John McCall0512e482010-07-14 04:20:34 +0000260 void mangleFloat(const llvm::APFloat &F);
Daniel Dunbarc0747712009-11-21 09:12:13 +0000261 void mangleFunctionEncoding(const FunctionDecl *FD);
262 void mangleName(const NamedDecl *ND);
263 void mangleType(QualType T);
Douglas Gregor1b12a3b2010-05-26 05:11:13 +0000264 void mangleNameOrStandardSubstitution(const NamedDecl *ND);
265
Daniel Dunbarc0747712009-11-21 09:12:13 +0000266private:
Daniel Dunbar1b077112009-11-21 09:06:10 +0000267 bool mangleSubstitution(const NamedDecl *ND);
268 bool mangleSubstitution(QualType T);
Douglas Gregor1e9268e2010-04-28 05:58:56 +0000269 bool mangleSubstitution(TemplateName Template);
Daniel Dunbar1b077112009-11-21 09:06:10 +0000270 bool mangleSubstitution(uintptr_t Ptr);
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000271
John McCall68a51a72011-07-01 00:04:39 +0000272 void mangleExistingSubstitution(QualType type);
273 void mangleExistingSubstitution(TemplateName name);
274
Daniel Dunbar1b077112009-11-21 09:06:10 +0000275 bool mangleStandardSubstitution(const NamedDecl *ND);
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000276
Daniel Dunbar1b077112009-11-21 09:06:10 +0000277 void addSubstitution(const NamedDecl *ND) {
278 ND = cast<NamedDecl>(ND->getCanonicalDecl());
Anders Carlsson433d1372009-11-07 04:26:04 +0000279
Daniel Dunbar1b077112009-11-21 09:06:10 +0000280 addSubstitution(reinterpret_cast<uintptr_t>(ND));
281 }
282 void addSubstitution(QualType T);
Douglas Gregor1e9268e2010-04-28 05:58:56 +0000283 void addSubstitution(TemplateName Template);
Daniel Dunbar1b077112009-11-21 09:06:10 +0000284 void addSubstitution(uintptr_t Ptr);
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000285
John McCalla0ce15c2011-04-24 08:23:24 +0000286 void mangleUnresolvedPrefix(NestedNameSpecifier *qualifier,
287 NamedDecl *firstQualifierLookup,
288 bool recursive = false);
289 void mangleUnresolvedName(NestedNameSpecifier *qualifier,
290 NamedDecl *firstQualifierLookup,
291 DeclarationName name,
John McCall1dd73832010-02-04 01:42:13 +0000292 unsigned KnownArity = UnknownArity);
293
Daniel Dunbar1b077112009-11-21 09:06:10 +0000294 void mangleName(const TemplateDecl *TD,
295 const TemplateArgument *TemplateArgs,
296 unsigned NumTemplateArgs);
John McCall1dd73832010-02-04 01:42:13 +0000297 void mangleUnqualifiedName(const NamedDecl *ND) {
298 mangleUnqualifiedName(ND, ND->getDeclName(), UnknownArity);
299 }
300 void mangleUnqualifiedName(const NamedDecl *ND, DeclarationName Name,
301 unsigned KnownArity);
Daniel Dunbar1b077112009-11-21 09:06:10 +0000302 void mangleUnscopedName(const NamedDecl *ND);
303 void mangleUnscopedTemplateName(const TemplateDecl *ND);
Douglas Gregor1e9268e2010-04-28 05:58:56 +0000304 void mangleUnscopedTemplateName(TemplateName);
Daniel Dunbar1b077112009-11-21 09:06:10 +0000305 void mangleSourceName(const IdentifierInfo *II);
306 void mangleLocalName(const NamedDecl *ND);
Douglas Gregorccc1b5e2012-02-21 00:37:24 +0000307 void mangleLambda(const CXXRecordDecl *Lambda);
Fariborz Jahanian57058532010-03-03 19:41:08 +0000308 void mangleNestedName(const NamedDecl *ND, const DeclContext *DC,
309 bool NoFunction=false);
Daniel Dunbar1b077112009-11-21 09:06:10 +0000310 void mangleNestedName(const TemplateDecl *TD,
311 const TemplateArgument *TemplateArgs,
312 unsigned NumTemplateArgs);
John McCalla0ce15c2011-04-24 08:23:24 +0000313 void manglePrefix(NestedNameSpecifier *qualifier);
Fariborz Jahanian57058532010-03-03 19:41:08 +0000314 void manglePrefix(const DeclContext *DC, bool NoFunction=false);
John McCall4f4e4132011-05-04 01:45:19 +0000315 void manglePrefix(QualType type);
Daniel Dunbar1b077112009-11-21 09:06:10 +0000316 void mangleTemplatePrefix(const TemplateDecl *ND);
Douglas Gregor20f0cc72010-04-23 03:10:43 +0000317 void mangleTemplatePrefix(TemplateName Template);
Daniel Dunbar1b077112009-11-21 09:06:10 +0000318 void mangleOperatorName(OverloadedOperatorKind OO, unsigned Arity);
319 void mangleQualifiers(Qualifiers Quals);
Douglas Gregor0a9a6d62011-01-26 17:36:28 +0000320 void mangleRefQualifier(RefQualifierKind RefQualifier);
John McCallefe6aee2009-09-05 07:56:18 +0000321
Anders Carlsson7b06f6c2009-12-10 03:14:39 +0000322 void mangleObjCMethodName(const ObjCMethodDecl *MD);
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000323
Daniel Dunbar1b077112009-11-21 09:06:10 +0000324 // Declare manglers for every type class.
John McCallefe6aee2009-09-05 07:56:18 +0000325#define ABSTRACT_TYPE(CLASS, PARENT)
326#define NON_CANONICAL_TYPE(CLASS, PARENT)
327#define TYPE(CLASS, PARENT) void mangleType(const CLASS##Type *T);
328#include "clang/AST/TypeNodes.def"
329
Daniel Dunbar1b077112009-11-21 09:06:10 +0000330 void mangleType(const TagType*);
John McCallb6f532e2010-07-14 06:43:17 +0000331 void mangleType(TemplateName);
Daniel Dunbar1b077112009-11-21 09:06:10 +0000332 void mangleBareFunctionType(const FunctionType *T,
333 bool MangleReturnType);
Bob Wilson57147a82010-11-16 00:32:18 +0000334 void mangleNeonVectorType(const VectorType *T);
Anders Carlssone170ba72009-12-14 01:45:37 +0000335
336 void mangleIntegerLiteral(QualType T, const llvm::APSInt &Value);
John McCalla0ce15c2011-04-24 08:23:24 +0000337 void mangleMemberExpr(const Expr *base, bool isArrow,
338 NestedNameSpecifier *qualifier,
339 NamedDecl *firstQualifierLookup,
340 DeclarationName name,
341 unsigned knownArity);
John McCall5e1e89b2010-08-18 19:18:59 +0000342 void mangleExpression(const Expr *E, unsigned Arity = UnknownArity);
Daniel Dunbar1b077112009-11-21 09:06:10 +0000343 void mangleCXXCtorType(CXXCtorType T);
344 void mangleCXXDtorType(CXXDtorType T);
Mike Stump1eb44332009-09-09 15:08:12 +0000345
Argyrios Kyrtzidisb0c3e092011-09-22 20:07:03 +0000346 void mangleTemplateArgs(const ASTTemplateArgumentListInfo &TemplateArgs);
Eli Friedmand7a6b162012-09-26 02:36:12 +0000347 void mangleTemplateArgs(const TemplateArgument *TemplateArgs,
Daniel Dunbar1b077112009-11-21 09:06:10 +0000348 unsigned NumTemplateArgs);
Eli Friedmand7a6b162012-09-26 02:36:12 +0000349 void mangleTemplateArgs(const TemplateArgumentList &AL);
350 void mangleTemplateArg(TemplateArgument A);
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000351
Daniel Dunbar1b077112009-11-21 09:06:10 +0000352 void mangleTemplateParameter(unsigned Index);
John McCallfb44de92011-05-01 22:35:37 +0000353
354 void mangleFunctionParam(const ParmVarDecl *parm);
Daniel Dunbar1b077112009-11-21 09:06:10 +0000355};
Peter Collingbourne14110472011-01-13 18:57:25 +0000356
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000357}
358
Anders Carlsson43f17402009-04-02 15:51:53 +0000359static bool isInCLinkageSpecification(const Decl *D) {
Douglas Gregor457e2812009-10-28 16:31:34 +0000360 D = D->getCanonicalDecl();
Douglas Gregorccc1b5e2012-02-21 00:37:24 +0000361 for (const DeclContext *DC = getEffectiveDeclContext(D);
362 !DC->isTranslationUnit(); DC = getEffectiveParentContext(DC)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000363 if (const LinkageSpecDecl *Linkage = dyn_cast<LinkageSpecDecl>(DC))
Anders Carlsson43f17402009-04-02 15:51:53 +0000364 return Linkage->getLanguage() == LinkageSpecDecl::lang_c;
365 }
Mike Stump1eb44332009-09-09 15:08:12 +0000366
Anders Carlsson43f17402009-04-02 15:51:53 +0000367 return false;
368}
369
Peter Collingbourne14110472011-01-13 18:57:25 +0000370bool ItaniumMangleContext::shouldMangleDeclName(const NamedDecl *D) {
Daniel Dunbarf981bf82009-11-21 09:14:52 +0000371 // In C, functions with no attributes never need to be mangled. Fastpath them.
David Blaikie4e4d0842012-03-11 07:00:24 +0000372 if (!getASTContext().getLangOpts().CPlusPlus && !D->hasAttrs())
Daniel Dunbarf981bf82009-11-21 09:14:52 +0000373 return false;
374
375 // Any decl can be declared with __asm("foo") on it, and this takes precedence
376 // over all other naming in the .o file.
377 if (D->hasAttr<AsmLabelAttr>())
378 return true;
379
Mike Stump141c5af2009-09-02 00:25:38 +0000380 // Clang's "overloadable" attribute extension to C/C++ implies name mangling
Anders Carlssona1e16222009-11-07 07:15:03 +0000381 // (always) as does passing a C++ member function and a function
382 // whose name is not a simple identifier.
Daniel Dunbarf981bf82009-11-21 09:14:52 +0000383 const FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
384 if (FD && (FD->hasAttr<OverloadableAttr>() || isa<CXXMethodDecl>(FD) ||
385 !FD->getDeclName().isIdentifier()))
386 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000387
Daniel Dunbarf981bf82009-11-21 09:14:52 +0000388 // Otherwise, no mangling is done outside C++ mode.
David Blaikie4e4d0842012-03-11 07:00:24 +0000389 if (!getASTContext().getLangOpts().CPlusPlus)
Daniel Dunbarf981bf82009-11-21 09:14:52 +0000390 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000391
Sean Hunt31455252010-01-24 03:04:27 +0000392 // Variables at global scope with non-internal linkage are not mangled
Eli Friedman7facf842009-12-02 20:32:49 +0000393 if (!FD) {
Douglas Gregorccc1b5e2012-02-21 00:37:24 +0000394 const DeclContext *DC = getEffectiveDeclContext(D);
Eli Friedman7facf842009-12-02 20:32:49 +0000395 // Check for extern variable declared locally.
Fariborz Jahaniane81c5612010-06-30 18:57:21 +0000396 if (DC->isFunctionOrMethod() && D->hasLinkage())
Eli Friedman7facf842009-12-02 20:32:49 +0000397 while (!DC->isNamespace() && !DC->isTranslationUnit())
Douglas Gregorccc1b5e2012-02-21 00:37:24 +0000398 DC = getEffectiveParentContext(DC);
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000399 if (DC->isTranslationUnit() && D->getLinkage() != InternalLinkage)
Eli Friedman7facf842009-12-02 20:32:49 +0000400 return false;
401 }
402
Eli Friedmanc00cb642010-07-18 20:49:59 +0000403 // Class members are always mangled.
Douglas Gregorccc1b5e2012-02-21 00:37:24 +0000404 if (getEffectiveDeclContext(D)->isRecord())
Eli Friedmanc00cb642010-07-18 20:49:59 +0000405 return true;
406
Eli Friedman7facf842009-12-02 20:32:49 +0000407 // C functions and "main" are not mangled.
408 if ((FD && FD->isMain()) || isInCLinkageSpecification(D))
Daniel Dunbarf981bf82009-11-21 09:14:52 +0000409 return false;
410
Anders Carlsson43f17402009-04-02 15:51:53 +0000411 return true;
412}
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000413
Chris Lattner5f9e2722011-07-23 10:55:15 +0000414void CXXNameMangler::mangle(const NamedDecl *D, StringRef Prefix) {
Mike Stump141c5af2009-09-02 00:25:38 +0000415 // Any decl can be declared with __asm("foo") on it, and this takes precedence
416 // over all other naming in the .o file.
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000417 if (const AsmLabelAttr *ALA = D->getAttr<AsmLabelAttr>()) {
Chris Lattnerca3f25c2009-03-21 08:24:40 +0000418 // If we have an asm name, then we use it as the mangling.
Rafael Espindola4e274e92011-02-15 22:23:51 +0000419
420 // Adding the prefix can cause problems when one file has a "foo" and
421 // another has a "\01foo". That is known to happen on ELF with the
422 // tricks normally used for producing aliases (PR9177). Fortunately the
423 // llvm mangler on ELF is a nop, so we can just avoid adding the \01
Peter Collingbourne69317432011-04-06 12:29:09 +0000424 // marker. We also avoid adding the marker if this is an alias for an
425 // LLVM intrinsic.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000426 StringRef UserLabelPrefix =
Douglas Gregorbcfd1f52011-09-02 00:18:52 +0000427 getASTContext().getTargetInfo().getUserLabelPrefix();
Peter Collingbourne69317432011-04-06 12:29:09 +0000428 if (!UserLabelPrefix.empty() && !ALA->getLabel().startswith("llvm."))
Rafael Espindola4e274e92011-02-15 22:23:51 +0000429 Out << '\01'; // LLVM IR Marker for __asm("foo")
430
Chris Lattnerca3f25c2009-03-21 08:24:40 +0000431 Out << ALA->getLabel();
Daniel Dunbarf981bf82009-11-21 09:14:52 +0000432 return;
Chris Lattnerca3f25c2009-03-21 08:24:40 +0000433 }
Mike Stump1eb44332009-09-09 15:08:12 +0000434
Sean Hunt31455252010-01-24 03:04:27 +0000435 // <mangled-name> ::= _Z <encoding>
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000436 // ::= <data name>
437 // ::= <special-name>
Daniel Dunbar7e0c1952009-11-21 09:17:15 +0000438 Out << Prefix;
439 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
Daniel Dunbarf981bf82009-11-21 09:14:52 +0000440 mangleFunctionEncoding(FD);
Rafael Espindolad9800722010-03-11 14:07:00 +0000441 else if (const VarDecl *VD = dyn_cast<VarDecl>(D))
442 mangleName(VD);
Daniel Dunbar7e0c1952009-11-21 09:17:15 +0000443 else
Rafael Espindolad9800722010-03-11 14:07:00 +0000444 mangleName(cast<FieldDecl>(D));
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000445}
446
447void CXXNameMangler::mangleFunctionEncoding(const FunctionDecl *FD) {
448 // <encoding> ::= <function name> <bare-function-type>
449 mangleName(FD);
Mike Stump1eb44332009-09-09 15:08:12 +0000450
Daniel Dunbar7e0c1952009-11-21 09:17:15 +0000451 // Don't mangle in the type if this isn't a decl we should typically mangle.
452 if (!Context.shouldMangleDeclName(FD))
453 return;
454
Mike Stump141c5af2009-09-02 00:25:38 +0000455 // Whether the mangling of a function type includes the return type depends on
456 // the context and the nature of the function. The rules for deciding whether
457 // the return type is included are:
Mike Stump1eb44332009-09-09 15:08:12 +0000458 //
Douglas Gregor1fd2dd12009-06-29 22:39:32 +0000459 // 1. Template functions (names or types) have return types encoded, with
460 // the exceptions listed below.
Mike Stump1eb44332009-09-09 15:08:12 +0000461 // 2. Function types not appearing as part of a function name mangling,
Douglas Gregor1fd2dd12009-06-29 22:39:32 +0000462 // e.g. parameters, pointer types, etc., have return type encoded, with the
463 // exceptions listed below.
464 // 3. Non-template function names do not have return types encoded.
465 //
Mike Stump141c5af2009-09-02 00:25:38 +0000466 // The exceptions mentioned in (1) and (2) above, for which the return type is
467 // never included, are
Douglas Gregor1fd2dd12009-06-29 22:39:32 +0000468 // 1. Constructors.
469 // 2. Destructors.
470 // 3. Conversion operator functions, e.g. operator int.
471 bool MangleReturnType = false;
Anders Carlsson9234b7f2009-09-17 03:46:43 +0000472 if (FunctionTemplateDecl *PrimaryTemplate = FD->getPrimaryTemplate()) {
473 if (!(isa<CXXConstructorDecl>(FD) || isa<CXXDestructorDecl>(FD) ||
474 isa<CXXConversionDecl>(FD)))
475 MangleReturnType = true;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000476
Anders Carlsson9234b7f2009-09-17 03:46:43 +0000477 // Mangle the type of the primary template.
478 FD = PrimaryTemplate->getTemplatedDecl();
479 }
480
Douglas Gregor79e6bd32011-07-12 04:42:08 +0000481 mangleBareFunctionType(FD->getType()->getAs<FunctionType>(),
482 MangleReturnType);
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000483}
484
Anders Carlsson47846d22009-12-04 06:23:23 +0000485static const DeclContext *IgnoreLinkageSpecDecls(const DeclContext *DC) {
486 while (isa<LinkageSpecDecl>(DC)) {
Douglas Gregorccc1b5e2012-02-21 00:37:24 +0000487 DC = getEffectiveParentContext(DC);
Anders Carlsson47846d22009-12-04 06:23:23 +0000488 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000489
Anders Carlsson47846d22009-12-04 06:23:23 +0000490 return DC;
491}
492
Anders Carlssonc820f902010-06-02 15:58:27 +0000493/// isStd - Return whether a given namespace is the 'std' namespace.
494static bool isStd(const NamespaceDecl *NS) {
Douglas Gregorccc1b5e2012-02-21 00:37:24 +0000495 if (!IgnoreLinkageSpecDecls(getEffectiveParentContext(NS))
496 ->isTranslationUnit())
Anders Carlssonc820f902010-06-02 15:58:27 +0000497 return false;
498
499 const IdentifierInfo *II = NS->getOriginalNamespace()->getIdentifier();
500 return II && II->isStr("std");
501}
502
Anders Carlsson47846d22009-12-04 06:23:23 +0000503// isStdNamespace - Return whether a given decl context is a toplevel 'std'
504// namespace.
Daniel Dunbar1308af92009-11-21 09:11:45 +0000505static bool isStdNamespace(const DeclContext *DC) {
Anders Carlsson47846d22009-12-04 06:23:23 +0000506 if (!DC->isNamespace())
507 return false;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000508
Anders Carlsson47846d22009-12-04 06:23:23 +0000509 return isStd(cast<NamespaceDecl>(DC));
Daniel Dunbar1308af92009-11-21 09:11:45 +0000510}
511
Anders Carlssonbb36ba42009-09-26 03:24:57 +0000512static const TemplateDecl *
513isTemplate(const NamedDecl *ND, const TemplateArgumentList *&TemplateArgs) {
Anders Carlsson2744a062009-09-18 19:00:18 +0000514 // Check if we have a function template.
515 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)){
Anders Carlssonbb36ba42009-09-26 03:24:57 +0000516 if (const TemplateDecl *TD = FD->getPrimaryTemplate()) {
Anders Carlsson2744a062009-09-18 19:00:18 +0000517 TemplateArgs = FD->getTemplateSpecializationArgs();
Anders Carlssonbb36ba42009-09-26 03:24:57 +0000518 return TD;
Anders Carlsson2744a062009-09-18 19:00:18 +0000519 }
520 }
521
Anders Carlssoneafc6dc2009-09-18 19:44:50 +0000522 // Check if we have a class template.
523 if (const ClassTemplateSpecializationDecl *Spec =
524 dyn_cast<ClassTemplateSpecializationDecl>(ND)) {
525 TemplateArgs = &Spec->getTemplateArgs();
Anders Carlssonbb36ba42009-09-26 03:24:57 +0000526 return Spec->getSpecializedTemplate();
Anders Carlssoneafc6dc2009-09-18 19:44:50 +0000527 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000528
Anders Carlsson2744a062009-09-18 19:00:18 +0000529 return 0;
530}
531
Douglas Gregorf54486a2012-04-04 17:40:10 +0000532static bool isLambda(const NamedDecl *ND) {
533 const CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(ND);
534 if (!Record)
535 return false;
536
537 return Record->isLambda();
538}
539
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000540void CXXNameMangler::mangleName(const NamedDecl *ND) {
541 // <name> ::= <nested-name>
542 // ::= <unscoped-name>
543 // ::= <unscoped-template-name> <template-args>
Anders Carlsson201ce742009-09-17 03:17:01 +0000544 // ::= <local-name>
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000545 //
Douglas Gregorccc1b5e2012-02-21 00:37:24 +0000546 const DeclContext *DC = getEffectiveDeclContext(ND);
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000547
Eli Friedman7facf842009-12-02 20:32:49 +0000548 // If this is an extern variable declared locally, the relevant DeclContext
549 // is that of the containing namespace, or the translation unit.
Douglas Gregorf54486a2012-04-04 17:40:10 +0000550 // FIXME: This is a hack; extern variables declared locally should have
551 // a proper semantic declaration context!
552 if (isa<FunctionDecl>(DC) && ND->hasLinkage() && !isLambda(ND))
Eli Friedman7facf842009-12-02 20:32:49 +0000553 while (!DC->isNamespace() && !DC->isTranslationUnit())
Douglas Gregorccc1b5e2012-02-21 00:37:24 +0000554 DC = getEffectiveParentContext(DC);
John McCall82b7d7b2010-10-18 21:28:44 +0000555 else if (GetLocalClassDecl(ND)) {
556 mangleLocalName(ND);
557 return;
558 }
Eli Friedman7facf842009-12-02 20:32:49 +0000559
James Molloyb3c312c2012-03-05 09:59:43 +0000560 DC = IgnoreLinkageSpecDecls(DC);
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000561
Anders Carlssond58d6f72009-09-17 16:12:20 +0000562 if (DC->isTranslationUnit() || isStdNamespace(DC)) {
Anders Carlsson2744a062009-09-18 19:00:18 +0000563 // Check if we have a template.
564 const TemplateArgumentList *TemplateArgs = 0;
Anders Carlsson0fa6df42009-09-26 19:45:45 +0000565 if (const TemplateDecl *TD = isTemplate(ND, TemplateArgs)) {
Anders Carlsson2744a062009-09-18 19:00:18 +0000566 mangleUnscopedTemplateName(TD);
Eli Friedmand7a6b162012-09-26 02:36:12 +0000567 mangleTemplateArgs(*TemplateArgs);
Anders Carlsson2744a062009-09-18 19:00:18 +0000568 return;
Anders Carlsson7482e242009-09-18 04:29:09 +0000569 }
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000570
Anders Carlsson7482e242009-09-18 04:29:09 +0000571 mangleUnscopedName(ND);
572 return;
573 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000574
Anders Carlsson7b06f6c2009-12-10 03:14:39 +0000575 if (isa<FunctionDecl>(DC) || isa<ObjCMethodDecl>(DC)) {
Anders Carlsson7482e242009-09-18 04:29:09 +0000576 mangleLocalName(ND);
577 return;
578 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000579
Eli Friedman7facf842009-12-02 20:32:49 +0000580 mangleNestedName(ND, DC);
Anders Carlsson7482e242009-09-18 04:29:09 +0000581}
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000582void CXXNameMangler::mangleName(const TemplateDecl *TD,
Anders Carlsson7624f212009-09-18 02:42:01 +0000583 const TemplateArgument *TemplateArgs,
584 unsigned NumTemplateArgs) {
Douglas Gregorccc1b5e2012-02-21 00:37:24 +0000585 const DeclContext *DC = IgnoreLinkageSpecDecls(getEffectiveDeclContext(TD));
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000586
Anders Carlsson7624f212009-09-18 02:42:01 +0000587 if (DC->isTranslationUnit() || isStdNamespace(DC)) {
Anders Carlsson0fa6df42009-09-26 19:45:45 +0000588 mangleUnscopedTemplateName(TD);
Eli Friedmand7a6b162012-09-26 02:36:12 +0000589 mangleTemplateArgs(TemplateArgs, NumTemplateArgs);
Anders Carlsson7624f212009-09-18 02:42:01 +0000590 } else {
591 mangleNestedName(TD, TemplateArgs, NumTemplateArgs);
592 }
593}
594
Anders Carlsson201ce742009-09-17 03:17:01 +0000595void CXXNameMangler::mangleUnscopedName(const NamedDecl *ND) {
596 // <unscoped-name> ::= <unqualified-name>
597 // ::= St <unqualified-name> # ::std::
James Molloyb3c312c2012-03-05 09:59:43 +0000598
599 if (isStdNamespace(IgnoreLinkageSpecDecls(getEffectiveDeclContext(ND))))
Anders Carlsson201ce742009-09-17 03:17:01 +0000600 Out << "St";
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000601
Anders Carlsson201ce742009-09-17 03:17:01 +0000602 mangleUnqualifiedName(ND);
603}
604
Anders Carlsson0fa6df42009-09-26 19:45:45 +0000605void CXXNameMangler::mangleUnscopedTemplateName(const TemplateDecl *ND) {
Anders Carlsson201ce742009-09-17 03:17:01 +0000606 // <unscoped-template-name> ::= <unscoped-name>
607 // ::= <substitution>
Anders Carlsson7624f212009-09-18 02:42:01 +0000608 if (mangleSubstitution(ND))
Anders Carlsson03c9d532009-09-17 04:02:31 +0000609 return;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000610
Douglas Gregor32fb4e12010-02-05 20:45:00 +0000611 // <template-template-param> ::= <template-param>
612 if (const TemplateTemplateParmDecl *TTP
613 = dyn_cast<TemplateTemplateParmDecl>(ND)) {
614 mangleTemplateParameter(TTP->getIndex());
615 return;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000616 }
Douglas Gregor32fb4e12010-02-05 20:45:00 +0000617
Anders Carlsson1668f202009-09-26 20:13:56 +0000618 mangleUnscopedName(ND->getTemplatedDecl());
Anders Carlsson7624f212009-09-18 02:42:01 +0000619 addSubstitution(ND);
Anders Carlsson201ce742009-09-17 03:17:01 +0000620}
621
Douglas Gregor1e9268e2010-04-28 05:58:56 +0000622void CXXNameMangler::mangleUnscopedTemplateName(TemplateName Template) {
623 // <unscoped-template-name> ::= <unscoped-name>
624 // ::= <substitution>
625 if (TemplateDecl *TD = Template.getAsTemplateDecl())
626 return mangleUnscopedTemplateName(TD);
Sean Huntc3021132010-05-05 15:23:54 +0000627
Douglas Gregor1e9268e2010-04-28 05:58:56 +0000628 if (mangleSubstitution(Template))
629 return;
630
Douglas Gregor1e9268e2010-04-28 05:58:56 +0000631 DependentTemplateName *Dependent = Template.getAsDependentTemplateName();
632 assert(Dependent && "Not a dependent template name?");
Douglas Gregor19617912011-07-12 05:06:05 +0000633 if (const IdentifierInfo *Id = Dependent->getIdentifier())
634 mangleSourceName(Id);
635 else
636 mangleOperatorName(Dependent->getOperator(), UnknownArity);
Sean Huntc3021132010-05-05 15:23:54 +0000637
Douglas Gregor1e9268e2010-04-28 05:58:56 +0000638 addSubstitution(Template);
639}
640
John McCall1b600522011-04-24 03:07:16 +0000641void CXXNameMangler::mangleFloat(const llvm::APFloat &f) {
642 // ABI:
643 // Floating-point literals are encoded using a fixed-length
644 // lowercase hexadecimal string corresponding to the internal
645 // representation (IEEE on Itanium), high-order bytes first,
646 // without leading zeroes. For example: "Lf bf800000 E" is -1.0f
647 // on Itanium.
John McCall0c8731a2012-01-30 18:36:31 +0000648 // The 'without leading zeroes' thing seems to be an editorial
649 // mistake; see the discussion on cxx-abi-dev beginning on
650 // 2012-01-16.
John McCall1b600522011-04-24 03:07:16 +0000651
Benjamin Kramer48d798c2012-06-02 10:20:41 +0000652 // Our requirements here are just barely weird enough to justify
John McCall0c8731a2012-01-30 18:36:31 +0000653 // using a custom algorithm instead of post-processing APInt::toString().
John McCall1b600522011-04-24 03:07:16 +0000654
John McCall0c8731a2012-01-30 18:36:31 +0000655 llvm::APInt valueBits = f.bitcastToAPInt();
656 unsigned numCharacters = (valueBits.getBitWidth() + 3) / 4;
657 assert(numCharacters != 0);
658
659 // Allocate a buffer of the right number of characters.
660 llvm::SmallVector<char, 20> buffer;
661 buffer.set_size(numCharacters);
662
663 // Fill the buffer left-to-right.
664 for (unsigned stringIndex = 0; stringIndex != numCharacters; ++stringIndex) {
665 // The bit-index of the next hex digit.
666 unsigned digitBitIndex = 4 * (numCharacters - stringIndex - 1);
667
668 // Project out 4 bits starting at 'digitIndex'.
669 llvm::integerPart hexDigit
670 = valueBits.getRawData()[digitBitIndex / llvm::integerPartWidth];
671 hexDigit >>= (digitBitIndex % llvm::integerPartWidth);
672 hexDigit &= 0xF;
673
674 // Map that over to a lowercase hex digit.
675 static const char charForHex[16] = {
676 '0', '1', '2', '3', '4', '5', '6', '7',
677 '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'
678 };
679 buffer[stringIndex] = charForHex[hexDigit];
680 }
681
682 Out.write(buffer.data(), numCharacters);
John McCall0512e482010-07-14 04:20:34 +0000683}
684
685void CXXNameMangler::mangleNumber(const llvm::APSInt &Value) {
686 if (Value.isSigned() && Value.isNegative()) {
687 Out << 'n';
John McCall54c86f72012-08-18 04:51:52 +0000688 Value.abs().print(Out, /*signed*/ false);
689 } else {
690 Value.print(Out, /*signed*/ false);
691 }
John McCall0512e482010-07-14 04:20:34 +0000692}
693
Anders Carlssona94822e2009-11-26 02:32:05 +0000694void CXXNameMangler::mangleNumber(int64_t Number) {
695 // <number> ::= [n] <non-negative decimal integer>
696 if (Number < 0) {
697 Out << 'n';
698 Number = -Number;
699 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000700
Anders Carlssona94822e2009-11-26 02:32:05 +0000701 Out << Number;
702}
703
Anders Carlsson19879c92010-03-23 17:17:29 +0000704void CXXNameMangler::mangleCallOffset(int64_t NonVirtual, int64_t Virtual) {
Mike Stump141c5af2009-09-02 00:25:38 +0000705 // <call-offset> ::= h <nv-offset> _
706 // ::= v <v-offset> _
707 // <nv-offset> ::= <offset number> # non-virtual base override
Anders Carlssona94822e2009-11-26 02:32:05 +0000708 // <v-offset> ::= <offset number> _ <virtual offset number>
Mike Stump141c5af2009-09-02 00:25:38 +0000709 // # virtual base override, with vcall offset
Anders Carlsson19879c92010-03-23 17:17:29 +0000710 if (!Virtual) {
Anders Carlssona94822e2009-11-26 02:32:05 +0000711 Out << 'h';
Anders Carlsson19879c92010-03-23 17:17:29 +0000712 mangleNumber(NonVirtual);
Anders Carlssona94822e2009-11-26 02:32:05 +0000713 Out << '_';
714 return;
Mike Stump141c5af2009-09-02 00:25:38 +0000715 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000716
Anders Carlssona94822e2009-11-26 02:32:05 +0000717 Out << 'v';
Anders Carlsson19879c92010-03-23 17:17:29 +0000718 mangleNumber(NonVirtual);
Anders Carlssona94822e2009-11-26 02:32:05 +0000719 Out << '_';
Anders Carlsson19879c92010-03-23 17:17:29 +0000720 mangleNumber(Virtual);
Anders Carlssona94822e2009-11-26 02:32:05 +0000721 Out << '_';
Mike Stump9124bcc2009-09-02 00:56:18 +0000722}
723
John McCall4f4e4132011-05-04 01:45:19 +0000724void CXXNameMangler::manglePrefix(QualType type) {
John McCalla0ce15c2011-04-24 08:23:24 +0000725 if (const TemplateSpecializationType *TST =
726 type->getAs<TemplateSpecializationType>()) {
727 if (!mangleSubstitution(QualType(TST, 0))) {
728 mangleTemplatePrefix(TST->getTemplateName());
Sean Huntc3021132010-05-05 15:23:54 +0000729
Douglas Gregoraa2187d2011-02-28 00:04:36 +0000730 // FIXME: GCC does not appear to mangle the template arguments when
731 // the template in question is a dependent template name. Should we
732 // emulate that badness?
Eli Friedmand7a6b162012-09-26 02:36:12 +0000733 mangleTemplateArgs(TST->getArgs(), TST->getNumArgs());
John McCalla0ce15c2011-04-24 08:23:24 +0000734 addSubstitution(QualType(TST, 0));
Rafael Espindola9b35b252010-03-17 04:28:11 +0000735 }
John McCalla0ce15c2011-04-24 08:23:24 +0000736 } else if (const DependentTemplateSpecializationType *DTST
737 = type->getAs<DependentTemplateSpecializationType>()) {
738 TemplateName Template
739 = getASTContext().getDependentTemplateName(DTST->getQualifier(),
740 DTST->getIdentifier());
741 mangleTemplatePrefix(Template);
742
743 // FIXME: GCC does not appear to mangle the template arguments when
744 // the template in question is a dependent template name. Should we
745 // emulate that badness?
Eli Friedmand7a6b162012-09-26 02:36:12 +0000746 mangleTemplateArgs(DTST->getArgs(), DTST->getNumArgs());
John McCalla0ce15c2011-04-24 08:23:24 +0000747 } else {
748 // We use the QualType mangle type variant here because it handles
749 // substitutions.
750 mangleType(type);
John McCall1dd73832010-02-04 01:42:13 +0000751 }
752}
753
John McCalla0ce15c2011-04-24 08:23:24 +0000754/// Mangle everything prior to the base-unresolved-name in an unresolved-name.
755///
756/// \param firstQualifierLookup - the entity found by unqualified lookup
757/// for the first name in the qualifier, if this is for a member expression
758/// \param recursive - true if this is being called recursively,
759/// i.e. if there is more prefix "to the right".
760void CXXNameMangler::mangleUnresolvedPrefix(NestedNameSpecifier *qualifier,
761 NamedDecl *firstQualifierLookup,
762 bool recursive) {
John McCall1dd73832010-02-04 01:42:13 +0000763
John McCalla0ce15c2011-04-24 08:23:24 +0000764 // x, ::x
765 // <unresolved-name> ::= [gs] <base-unresolved-name>
766
767 // T::x / decltype(p)::x
768 // <unresolved-name> ::= sr <unresolved-type> <base-unresolved-name>
769
770 // T::N::x /decltype(p)::N::x
771 // <unresolved-name> ::= srN <unresolved-type> <unresolved-qualifier-level>+ E
772 // <base-unresolved-name>
773
774 // A::x, N::y, A<T>::z; "gs" means leading "::"
775 // <unresolved-name> ::= [gs] sr <unresolved-qualifier-level>+ E
776 // <base-unresolved-name>
777
778 switch (qualifier->getKind()) {
779 case NestedNameSpecifier::Global:
780 Out << "gs";
781
782 // We want an 'sr' unless this is the entire NNS.
783 if (recursive)
784 Out << "sr";
785
786 // We never want an 'E' here.
787 return;
788
789 case NestedNameSpecifier::Namespace:
790 if (qualifier->getPrefix())
791 mangleUnresolvedPrefix(qualifier->getPrefix(), firstQualifierLookup,
792 /*recursive*/ true);
793 else
794 Out << "sr";
795 mangleSourceName(qualifier->getAsNamespace()->getIdentifier());
796 break;
797 case NestedNameSpecifier::NamespaceAlias:
798 if (qualifier->getPrefix())
799 mangleUnresolvedPrefix(qualifier->getPrefix(), firstQualifierLookup,
800 /*recursive*/ true);
801 else
802 Out << "sr";
803 mangleSourceName(qualifier->getAsNamespaceAlias()->getIdentifier());
804 break;
805
806 case NestedNameSpecifier::TypeSpec:
807 case NestedNameSpecifier::TypeSpecWithTemplate: {
John McCall4f4e4132011-05-04 01:45:19 +0000808 const Type *type = qualifier->getAsType();
John McCalla0ce15c2011-04-24 08:23:24 +0000809
John McCall4f4e4132011-05-04 01:45:19 +0000810 // We only want to use an unresolved-type encoding if this is one of:
811 // - a decltype
812 // - a template type parameter
813 // - a template template parameter with arguments
814 // In all of these cases, we should have no prefix.
815 if (qualifier->getPrefix()) {
816 mangleUnresolvedPrefix(qualifier->getPrefix(), firstQualifierLookup,
817 /*recursive*/ true);
818 } else {
819 // Otherwise, all the cases want this.
820 Out << "sr";
John McCall4f4e4132011-05-04 01:45:19 +0000821 }
822
John McCall4f4e4132011-05-04 01:45:19 +0000823 // Only certain other types are valid as prefixes; enumerate them.
John McCalld3d49bb2011-06-28 16:49:23 +0000824 switch (type->getTypeClass()) {
825 case Type::Builtin:
826 case Type::Complex:
827 case Type::Pointer:
828 case Type::BlockPointer:
829 case Type::LValueReference:
830 case Type::RValueReference:
831 case Type::MemberPointer:
832 case Type::ConstantArray:
833 case Type::IncompleteArray:
834 case Type::VariableArray:
835 case Type::DependentSizedArray:
836 case Type::DependentSizedExtVector:
837 case Type::Vector:
838 case Type::ExtVector:
839 case Type::FunctionProto:
840 case Type::FunctionNoProto:
841 case Type::Enum:
842 case Type::Paren:
843 case Type::Elaborated:
844 case Type::Attributed:
845 case Type::Auto:
846 case Type::PackExpansion:
John McCalld3d49bb2011-06-28 16:49:23 +0000847 case Type::ObjCObject:
848 case Type::ObjCInterface:
849 case Type::ObjCObjectPointer:
Eli Friedmanb001de72011-10-06 23:00:33 +0000850 case Type::Atomic:
John McCalld3d49bb2011-06-28 16:49:23 +0000851 llvm_unreachable("type is illegal as a nested name specifier");
852
John McCall68a51a72011-07-01 00:04:39 +0000853 case Type::SubstTemplateTypeParmPack:
854 // FIXME: not clear how to mangle this!
855 // template <class T...> class A {
856 // template <class U...> void foo(decltype(T::foo(U())) x...);
857 // };
858 Out << "_SUBSTPACK_";
859 break;
860
John McCalld3d49bb2011-06-28 16:49:23 +0000861 // <unresolved-type> ::= <template-param>
862 // ::= <decltype>
863 // ::= <template-template-param> <template-args>
864 // (this last is not official yet)
865 case Type::TypeOfExpr:
866 case Type::TypeOf:
867 case Type::Decltype:
868 case Type::TemplateTypeParm:
869 case Type::UnaryTransform:
John McCall35ee32e2011-07-01 02:19:08 +0000870 case Type::SubstTemplateTypeParm:
John McCalld3d49bb2011-06-28 16:49:23 +0000871 unresolvedType:
872 assert(!qualifier->getPrefix());
873
874 // We only get here recursively if we're followed by identifiers.
875 if (recursive) Out << 'N';
876
John McCall35ee32e2011-07-01 02:19:08 +0000877 // This seems to do everything we want. It's not really
878 // sanctioned for a substituted template parameter, though.
John McCalld3d49bb2011-06-28 16:49:23 +0000879 mangleType(QualType(type, 0));
880
881 // We never want to print 'E' directly after an unresolved-type,
882 // so we return directly.
883 return;
884
John McCalld3d49bb2011-06-28 16:49:23 +0000885 case Type::Typedef:
886 mangleSourceName(cast<TypedefType>(type)->getDecl()->getIdentifier());
887 break;
888
889 case Type::UnresolvedUsing:
890 mangleSourceName(cast<UnresolvedUsingType>(type)->getDecl()
891 ->getIdentifier());
892 break;
893
894 case Type::Record:
895 mangleSourceName(cast<RecordType>(type)->getDecl()->getIdentifier());
896 break;
897
898 case Type::TemplateSpecialization: {
899 const TemplateSpecializationType *tst
900 = cast<TemplateSpecializationType>(type);
John McCall68a51a72011-07-01 00:04:39 +0000901 TemplateName name = tst->getTemplateName();
902 switch (name.getKind()) {
903 case TemplateName::Template:
904 case TemplateName::QualifiedTemplate: {
905 TemplateDecl *temp = name.getAsTemplateDecl();
John McCalld3d49bb2011-06-28 16:49:23 +0000906
John McCall68a51a72011-07-01 00:04:39 +0000907 // If the base is a template template parameter, this is an
908 // unresolved type.
909 assert(temp && "no template for template specialization type");
910 if (isa<TemplateTemplateParmDecl>(temp)) goto unresolvedType;
John McCalld3d49bb2011-06-28 16:49:23 +0000911
John McCall68a51a72011-07-01 00:04:39 +0000912 mangleSourceName(temp->getIdentifier());
913 break;
914 }
915
916 case TemplateName::OverloadedTemplate:
917 case TemplateName::DependentTemplate:
918 llvm_unreachable("invalid base for a template specialization type");
919
920 case TemplateName::SubstTemplateTemplateParm: {
921 SubstTemplateTemplateParmStorage *subst
922 = name.getAsSubstTemplateTemplateParm();
923 mangleExistingSubstitution(subst->getReplacement());
924 break;
925 }
926
927 case TemplateName::SubstTemplateTemplateParmPack: {
928 // FIXME: not clear how to mangle this!
929 // template <template <class U> class T...> class A {
930 // template <class U...> void foo(decltype(T<U>::foo) x...);
931 // };
932 Out << "_SUBSTPACK_";
933 break;
934 }
935 }
936
Eli Friedmand7a6b162012-09-26 02:36:12 +0000937 mangleTemplateArgs(tst->getArgs(), tst->getNumArgs());
John McCalld3d49bb2011-06-28 16:49:23 +0000938 break;
939 }
940
941 case Type::InjectedClassName:
942 mangleSourceName(cast<InjectedClassNameType>(type)->getDecl()
943 ->getIdentifier());
944 break;
945
946 case Type::DependentName:
947 mangleSourceName(cast<DependentNameType>(type)->getIdentifier());
948 break;
949
950 case Type::DependentTemplateSpecialization: {
951 const DependentTemplateSpecializationType *tst
952 = cast<DependentTemplateSpecializationType>(type);
John McCall4f4e4132011-05-04 01:45:19 +0000953 mangleSourceName(tst->getIdentifier());
Eli Friedmand7a6b162012-09-26 02:36:12 +0000954 mangleTemplateArgs(tst->getArgs(), tst->getNumArgs());
John McCalld3d49bb2011-06-28 16:49:23 +0000955 break;
956 }
John McCall4f4e4132011-05-04 01:45:19 +0000957 }
958 break;
John McCalla0ce15c2011-04-24 08:23:24 +0000959 }
960
961 case NestedNameSpecifier::Identifier:
962 // Member expressions can have these without prefixes.
963 if (qualifier->getPrefix()) {
964 mangleUnresolvedPrefix(qualifier->getPrefix(), firstQualifierLookup,
965 /*recursive*/ true);
966 } else if (firstQualifierLookup) {
967
968 // Try to make a proper qualifier out of the lookup result, and
969 // then just recurse on that.
970 NestedNameSpecifier *newQualifier;
971 if (TypeDecl *typeDecl = dyn_cast<TypeDecl>(firstQualifierLookup)) {
972 QualType type = getASTContext().getTypeDeclType(typeDecl);
973
974 // Pretend we had a different nested name specifier.
975 newQualifier = NestedNameSpecifier::Create(getASTContext(),
976 /*prefix*/ 0,
977 /*template*/ false,
978 type.getTypePtr());
979 } else if (NamespaceDecl *nspace =
980 dyn_cast<NamespaceDecl>(firstQualifierLookup)) {
981 newQualifier = NestedNameSpecifier::Create(getASTContext(),
982 /*prefix*/ 0,
983 nspace);
984 } else if (NamespaceAliasDecl *alias =
985 dyn_cast<NamespaceAliasDecl>(firstQualifierLookup)) {
986 newQualifier = NestedNameSpecifier::Create(getASTContext(),
987 /*prefix*/ 0,
988 alias);
989 } else {
990 // No sensible mangling to do here.
991 newQualifier = 0;
992 }
993
994 if (newQualifier)
995 return mangleUnresolvedPrefix(newQualifier, /*lookup*/ 0, recursive);
996
997 } else {
998 Out << "sr";
999 }
1000
1001 mangleSourceName(qualifier->getAsIdentifier());
1002 break;
1003 }
1004
1005 // If this was the innermost part of the NNS, and we fell out to
1006 // here, append an 'E'.
1007 if (!recursive)
1008 Out << 'E';
1009}
1010
1011/// Mangle an unresolved-name, which is generally used for names which
1012/// weren't resolved to specific entities.
1013void CXXNameMangler::mangleUnresolvedName(NestedNameSpecifier *qualifier,
1014 NamedDecl *firstQualifierLookup,
1015 DeclarationName name,
1016 unsigned knownArity) {
1017 if (qualifier) mangleUnresolvedPrefix(qualifier, firstQualifierLookup);
1018 mangleUnqualifiedName(0, name, knownArity);
John McCall1dd73832010-02-04 01:42:13 +00001019}
1020
Anders Carlsson6f7e2f42010-06-08 14:49:03 +00001021static const FieldDecl *FindFirstNamedDataMember(const RecordDecl *RD) {
1022 assert(RD->isAnonymousStructOrUnion() &&
1023 "Expected anonymous struct or union!");
1024
1025 for (RecordDecl::field_iterator I = RD->field_begin(), E = RD->field_end();
1026 I != E; ++I) {
David Blaikie581deb32012-06-06 20:45:41 +00001027 if (I->getIdentifier())
1028 return *I;
Anders Carlsson6f7e2f42010-06-08 14:49:03 +00001029
David Blaikie581deb32012-06-06 20:45:41 +00001030 if (const RecordType *RT = I->getType()->getAs<RecordType>())
Anders Carlsson6f7e2f42010-06-08 14:49:03 +00001031 if (const FieldDecl *NamedDataMember =
1032 FindFirstNamedDataMember(RT->getDecl()))
1033 return NamedDataMember;
1034 }
Anders Carlsson6f7e2f42010-06-08 14:49:03 +00001035
1036 // We didn't find a named data member.
1037 return 0;
1038}
1039
John McCall1dd73832010-02-04 01:42:13 +00001040void CXXNameMangler::mangleUnqualifiedName(const NamedDecl *ND,
1041 DeclarationName Name,
1042 unsigned KnownArity) {
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001043 // <unqualified-name> ::= <operator-name>
Mike Stump1eb44332009-09-09 15:08:12 +00001044 // ::= <ctor-dtor-name>
1045 // ::= <source-name>
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001046 switch (Name.getNameKind()) {
Anders Carlssonc4355b62009-10-07 01:45:02 +00001047 case DeclarationName::Identifier: {
Anders Carlssonc4355b62009-10-07 01:45:02 +00001048 if (const IdentifierInfo *II = Name.getAsIdentifierInfo()) {
Sean Hunt31455252010-01-24 03:04:27 +00001049 // We must avoid conflicts between internally- and externally-
John McCall74990f42011-03-22 06:34:45 +00001050 // linked variable and function declaration names in the same TU:
1051 // void test() { extern void foo(); }
1052 // static void foo();
1053 // This naming convention is the same as that followed by GCC,
1054 // though it shouldn't actually matter.
1055 if (ND && ND->getLinkage() == InternalLinkage &&
Douglas Gregorccc1b5e2012-02-21 00:37:24 +00001056 getEffectiveDeclContext(ND)->isFileContext())
Sean Hunt31455252010-01-24 03:04:27 +00001057 Out << 'L';
1058
Anders Carlssonc4355b62009-10-07 01:45:02 +00001059 mangleSourceName(II);
1060 break;
1061 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00001062
John McCall1dd73832010-02-04 01:42:13 +00001063 // Otherwise, an anonymous entity. We must have a declaration.
1064 assert(ND && "mangling empty name without declaration");
1065
1066 if (const NamespaceDecl *NS = dyn_cast<NamespaceDecl>(ND)) {
1067 if (NS->isAnonymousNamespace()) {
1068 // This is how gcc mangles these names.
1069 Out << "12_GLOBAL__N_1";
1070 break;
1071 }
1072 }
1073
Anders Carlsson6f7e2f42010-06-08 14:49:03 +00001074 if (const VarDecl *VD = dyn_cast<VarDecl>(ND)) {
1075 // We must have an anonymous union or struct declaration.
1076 const RecordDecl *RD =
1077 cast<RecordDecl>(VD->getType()->getAs<RecordType>()->getDecl());
1078
1079 // Itanium C++ ABI 5.1.2:
1080 //
1081 // For the purposes of mangling, the name of an anonymous union is
1082 // considered to be the name of the first named data member found by a
1083 // pre-order, depth-first, declaration-order walk of the data members of
1084 // the anonymous union. If there is no such data member (i.e., if all of
1085 // the data members in the union are unnamed), then there is no way for
1086 // a program to refer to the anonymous union, and there is therefore no
1087 // need to mangle its name.
1088 const FieldDecl *FD = FindFirstNamedDataMember(RD);
John McCall7121c8f2010-08-05 22:02:13 +00001089
1090 // It's actually possible for various reasons for us to get here
1091 // with an empty anonymous struct / union. Fortunately, it
1092 // doesn't really matter what name we generate.
1093 if (!FD) break;
Anders Carlsson6f7e2f42010-06-08 14:49:03 +00001094 assert(FD->getIdentifier() && "Data member name isn't an identifier!");
1095
1096 mangleSourceName(FD->getIdentifier());
1097 break;
1098 }
1099
Anders Carlssonc4355b62009-10-07 01:45:02 +00001100 // We must have an anonymous struct.
1101 const TagDecl *TD = cast<TagDecl>(ND);
Richard Smith162e1c12011-04-15 14:24:37 +00001102 if (const TypedefNameDecl *D = TD->getTypedefNameForAnonDecl()) {
Anders Carlssonc4355b62009-10-07 01:45:02 +00001103 assert(TD->getDeclContext() == D->getDeclContext() &&
1104 "Typedef should not be in another decl context!");
1105 assert(D->getDeclName().getAsIdentifierInfo() &&
1106 "Typedef was not named!");
1107 mangleSourceName(D->getDeclName().getAsIdentifierInfo());
1108 break;
1109 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00001110
Douglas Gregor9e8c92a2012-02-20 19:44:39 +00001111 // <unnamed-type-name> ::= <closure-type-name>
1112 //
1113 // <closure-type-name> ::= Ul <lambda-sig> E [ <nonnegative number> ] _
1114 // <lambda-sig> ::= <parameter-type>+ # Parameter types or 'v' for 'void'.
1115 if (const CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(TD)) {
Douglas Gregor5878cbc2012-02-21 04:17:39 +00001116 if (Record->isLambda() && Record->getLambdaManglingNumber()) {
Douglas Gregorccc1b5e2012-02-21 00:37:24 +00001117 mangleLambda(Record);
Douglas Gregor9e8c92a2012-02-20 19:44:39 +00001118 break;
1119 }
1120 }
David Blaikie66cff722012-11-14 01:52:05 +00001121
1122 int UnnamedMangle = Context.getASTContext().getUnnamedTagManglingNumber(TD);
1123 if (UnnamedMangle != -1) {
1124 Out << "Ut";
1125 if (UnnamedMangle != 0)
1126 Out << llvm::utostr(UnnamedMangle - 1);
1127 Out << '_';
1128 break;
1129 }
1130
Anders Carlssonc4355b62009-10-07 01:45:02 +00001131 // Get a unique id for the anonymous struct.
1132 uint64_t AnonStructId = Context.getAnonymousStructId(TD);
1133
1134 // Mangle it as a source name in the form
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00001135 // [n] $_<id>
Anders Carlssonc4355b62009-10-07 01:45:02 +00001136 // where n is the length of the string.
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00001137 SmallString<8> Str;
Anders Carlssonc4355b62009-10-07 01:45:02 +00001138 Str += "$_";
1139 Str += llvm::utostr(AnonStructId);
1140
1141 Out << Str.size();
1142 Out << Str.str();
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001143 break;
Anders Carlssonc4355b62009-10-07 01:45:02 +00001144 }
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001145
1146 case DeclarationName::ObjCZeroArgSelector:
1147 case DeclarationName::ObjCOneArgSelector:
1148 case DeclarationName::ObjCMultiArgSelector:
David Blaikieb219cfc2011-09-23 05:06:16 +00001149 llvm_unreachable("Can't mangle Objective-C selector names here!");
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001150
1151 case DeclarationName::CXXConstructorName:
Anders Carlsson27ae5362009-04-17 01:58:57 +00001152 if (ND == Structor)
Mike Stump141c5af2009-09-02 00:25:38 +00001153 // If the named decl is the C++ constructor we're mangling, use the type
1154 // we were given.
Anders Carlsson27ae5362009-04-17 01:58:57 +00001155 mangleCXXCtorType(static_cast<CXXCtorType>(StructorType));
Anders Carlsson3ac86b52009-04-15 05:36:58 +00001156 else
1157 // Otherwise, use the complete constructor name. This is relevant if a
1158 // class with a constructor is declared within a constructor.
1159 mangleCXXCtorType(Ctor_Complete);
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001160 break;
1161
1162 case DeclarationName::CXXDestructorName:
Anders Carlsson27ae5362009-04-17 01:58:57 +00001163 if (ND == Structor)
Mike Stump141c5af2009-09-02 00:25:38 +00001164 // If the named decl is the C++ destructor we're mangling, use the type we
1165 // were given.
Anders Carlsson27ae5362009-04-17 01:58:57 +00001166 mangleCXXDtorType(static_cast<CXXDtorType>(StructorType));
1167 else
1168 // Otherwise, use the complete destructor name. This is relevant if a
1169 // class with a destructor is declared within a destructor.
1170 mangleCXXDtorType(Dtor_Complete);
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001171 break;
1172
1173 case DeclarationName::CXXConversionFunctionName:
Mike Stump1eb44332009-09-09 15:08:12 +00001174 // <operator-name> ::= cv <type> # (cast)
Douglas Gregor219cc612009-02-13 01:28:03 +00001175 Out << "cv";
Douglas Gregor79e6bd32011-07-12 04:42:08 +00001176 mangleType(Name.getCXXNameType());
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001177 break;
1178
Anders Carlsson8257d412009-12-22 06:36:32 +00001179 case DeclarationName::CXXOperatorName: {
John McCall1dd73832010-02-04 01:42:13 +00001180 unsigned Arity;
1181 if (ND) {
1182 Arity = cast<FunctionDecl>(ND)->getNumParams();
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001183
John McCall1dd73832010-02-04 01:42:13 +00001184 // If we have a C++ member function, we need to include the 'this' pointer.
1185 // FIXME: This does not make sense for operators that are static, but their
1186 // names stay the same regardless of the arity (operator new for instance).
1187 if (isa<CXXMethodDecl>(ND))
1188 Arity++;
1189 } else
1190 Arity = KnownArity;
1191
Anders Carlsson8257d412009-12-22 06:36:32 +00001192 mangleOperatorName(Name.getCXXOverloadedOperator(), Arity);
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001193 break;
Anders Carlsson8257d412009-12-22 06:36:32 +00001194 }
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001195
Sean Hunt3e518bd2009-11-29 07:34:05 +00001196 case DeclarationName::CXXLiteralOperatorName:
Sean Hunt5dd6b392009-12-04 21:11:13 +00001197 // FIXME: This mangling is not yet official.
Sean Hunt2421f662009-12-04 21:01:37 +00001198 Out << "li";
Sean Hunt3e518bd2009-11-29 07:34:05 +00001199 mangleSourceName(Name.getCXXLiteralIdentifier());
1200 break;
1201
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001202 case DeclarationName::CXXUsingDirective:
David Blaikieb219cfc2011-09-23 05:06:16 +00001203 llvm_unreachable("Can't mangle a using directive name!");
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001204 }
1205}
1206
1207void CXXNameMangler::mangleSourceName(const IdentifierInfo *II) {
1208 // <source-name> ::= <positive length number> <identifier>
1209 // <number> ::= [n] <non-negative decimal integer>
1210 // <identifier> ::= <unqualified source code identifier>
1211 Out << II->getLength() << II->getName();
1212}
1213
Eli Friedman7facf842009-12-02 20:32:49 +00001214void CXXNameMangler::mangleNestedName(const NamedDecl *ND,
Fariborz Jahanian57058532010-03-03 19:41:08 +00001215 const DeclContext *DC,
1216 bool NoFunction) {
Douglas Gregor0a9a6d62011-01-26 17:36:28 +00001217 // <nested-name>
1218 // ::= N [<CV-qualifiers>] [<ref-qualifier>] <prefix> <unqualified-name> E
1219 // ::= N [<CV-qualifiers>] [<ref-qualifier>] <template-prefix>
1220 // <template-args> E
Anders Carlssond99edc42009-09-26 03:55:37 +00001221
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001222 Out << 'N';
Douglas Gregor0a9a6d62011-01-26 17:36:28 +00001223 if (const CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(ND)) {
John McCall0953e762009-09-24 19:53:00 +00001224 mangleQualifiers(Qualifiers::fromCVRMask(Method->getTypeQualifiers()));
Douglas Gregor0a9a6d62011-01-26 17:36:28 +00001225 mangleRefQualifier(Method->getRefQualifier());
1226 }
1227
Anders Carlsson2744a062009-09-18 19:00:18 +00001228 // Check if we have a template.
1229 const TemplateArgumentList *TemplateArgs = 0;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00001230 if (const TemplateDecl *TD = isTemplate(ND, TemplateArgs)) {
Anders Carlsson2744a062009-09-18 19:00:18 +00001231 mangleTemplatePrefix(TD);
Eli Friedmand7a6b162012-09-26 02:36:12 +00001232 mangleTemplateArgs(*TemplateArgs);
Fariborz Jahanian57058532010-03-03 19:41:08 +00001233 }
1234 else {
1235 manglePrefix(DC, NoFunction);
Anders Carlsson7482e242009-09-18 04:29:09 +00001236 mangleUnqualifiedName(ND);
1237 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00001238
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001239 Out << 'E';
1240}
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00001241void CXXNameMangler::mangleNestedName(const TemplateDecl *TD,
Anders Carlsson7624f212009-09-18 02:42:01 +00001242 const TemplateArgument *TemplateArgs,
1243 unsigned NumTemplateArgs) {
Anders Carlssone45117b2009-09-27 19:53:49 +00001244 // <nested-name> ::= N [<CV-qualifiers>] <template-prefix> <template-args> E
1245
Anders Carlsson7624f212009-09-18 02:42:01 +00001246 Out << 'N';
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00001247
Anders Carlssone45117b2009-09-27 19:53:49 +00001248 mangleTemplatePrefix(TD);
Eli Friedmand7a6b162012-09-26 02:36:12 +00001249 mangleTemplateArgs(TemplateArgs, NumTemplateArgs);
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00001250
Anders Carlsson7624f212009-09-18 02:42:01 +00001251 Out << 'E';
1252}
1253
Anders Carlsson1b42c792009-04-02 16:24:45 +00001254void CXXNameMangler::mangleLocalName(const NamedDecl *ND) {
1255 // <local-name> := Z <function encoding> E <entity name> [<discriminator>]
1256 // := Z <function encoding> E s [<discriminator>]
Douglas Gregorccc1b5e2012-02-21 00:37:24 +00001257 // <local-name> := Z <function encoding> E d [ <parameter number> ]
1258 // _ <entity name>
Mike Stump1eb44332009-09-09 15:08:12 +00001259 // <discriminator> := _ <non-negative number>
Douglas Gregorccc1b5e2012-02-21 00:37:24 +00001260 const DeclContext *DC = getEffectiveDeclContext(ND);
Fariborz Jahanian8805fe82011-06-09 19:25:01 +00001261 if (isa<ObjCMethodDecl>(DC) && isa<FunctionDecl>(ND)) {
1262 // Don't add objc method name mangling to locally declared function
1263 mangleUnqualifiedName(ND);
1264 return;
1265 }
1266
Anders Carlsson1b42c792009-04-02 16:24:45 +00001267 Out << 'Z';
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001268
Charles Davis685b1d92010-05-26 18:25:27 +00001269 if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(DC)) {
1270 mangleObjCMethodName(MD);
John McCall82b7d7b2010-10-18 21:28:44 +00001271 } else if (const CXXRecordDecl *RD = GetLocalClassDecl(ND)) {
Douglas Gregorccc1b5e2012-02-21 00:37:24 +00001272 mangleFunctionEncoding(cast<FunctionDecl>(getEffectiveDeclContext(RD)));
Fariborz Jahanian57058532010-03-03 19:41:08 +00001273 Out << 'E';
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001274
Douglas Gregorccc1b5e2012-02-21 00:37:24 +00001275 // The parameter number is omitted for the last parameter, 0 for the
1276 // second-to-last parameter, 1 for the third-to-last parameter, etc. The
1277 // <entity name> will of course contain a <closure-type-name>: Its
1278 // numbering will be local to the particular argument in which it appears
1279 // -- other default arguments do not affect its encoding.
1280 bool SkipDiscriminator = false;
1281 if (RD->isLambda()) {
1282 if (const ParmVarDecl *Parm
1283 = dyn_cast_or_null<ParmVarDecl>(RD->getLambdaContextDecl())) {
1284 if (const FunctionDecl *Func
1285 = dyn_cast<FunctionDecl>(Parm->getDeclContext())) {
1286 Out << 'd';
1287 unsigned Num = Func->getNumParams() - Parm->getFunctionScopeIndex();
1288 if (Num > 1)
1289 mangleNumber(Num - 2);
1290 Out << '_';
1291 SkipDiscriminator = true;
1292 }
1293 }
1294 }
1295
John McCall82b7d7b2010-10-18 21:28:44 +00001296 // Mangle the name relative to the closest enclosing function.
1297 if (ND == RD) // equality ok because RD derived from ND above
1298 mangleUnqualifiedName(ND);
1299 else
1300 mangleNestedName(ND, DC, true /*NoFunction*/);
1301
Douglas Gregorccc1b5e2012-02-21 00:37:24 +00001302 if (!SkipDiscriminator) {
1303 unsigned disc;
1304 if (Context.getNextDiscriminator(RD, disc)) {
1305 if (disc < 10)
1306 Out << '_' << disc;
1307 else
1308 Out << "__" << disc << '_';
1309 }
Fariborz Jahanian4819ac42010-03-04 01:02:03 +00001310 }
Douglas Gregorccc1b5e2012-02-21 00:37:24 +00001311
Fariborz Jahanian57058532010-03-03 19:41:08 +00001312 return;
1313 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001314 else
Fariborz Jahanian57058532010-03-03 19:41:08 +00001315 mangleFunctionEncoding(cast<FunctionDecl>(DC));
Anders Carlsson7b06f6c2009-12-10 03:14:39 +00001316
Anders Carlsson1b42c792009-04-02 16:24:45 +00001317 Out << 'E';
Eli Friedman6f9f25d2009-12-11 20:21:38 +00001318 mangleUnqualifiedName(ND);
Anders Carlsson1b42c792009-04-02 16:24:45 +00001319}
1320
Douglas Gregorccc1b5e2012-02-21 00:37:24 +00001321void CXXNameMangler::mangleLambda(const CXXRecordDecl *Lambda) {
Douglas Gregor552e2992012-02-21 02:22:07 +00001322 // If the context of a closure type is an initializer for a class member
1323 // (static or nonstatic), it is encoded in a qualified name with a final
1324 // <prefix> of the form:
1325 //
1326 // <data-member-prefix> := <member source-name> M
1327 //
1328 // Technically, the data-member-prefix is part of the <prefix>. However,
1329 // since a closure type will always be mangled with a prefix, it's easier
1330 // to emit that last part of the prefix here.
1331 if (Decl *Context = Lambda->getLambdaContextDecl()) {
1332 if ((isa<VarDecl>(Context) || isa<FieldDecl>(Context)) &&
1333 Context->getDeclContext()->isRecord()) {
1334 if (const IdentifierInfo *Name
1335 = cast<NamedDecl>(Context)->getIdentifier()) {
1336 mangleSourceName(Name);
1337 Out << 'M';
1338 }
1339 }
1340 }
1341
Douglas Gregorccc1b5e2012-02-21 00:37:24 +00001342 Out << "Ul";
Eli Friedman8da8a662012-09-19 01:18:11 +00001343 const FunctionProtoType *Proto = Lambda->getLambdaTypeInfo()->getType()->
1344 getAs<FunctionProtoType>();
Douglas Gregorccc1b5e2012-02-21 00:37:24 +00001345 mangleBareFunctionType(Proto, /*MangleReturnType=*/false);
1346 Out << "E";
1347
1348 // The number is omitted for the first closure type with a given
1349 // <lambda-sig> in a given context; it is n-2 for the nth closure type
1350 // (in lexical order) with that same <lambda-sig> and context.
1351 //
1352 // The AST keeps track of the number for us.
Douglas Gregor5878cbc2012-02-21 04:17:39 +00001353 unsigned Number = Lambda->getLambdaManglingNumber();
1354 assert(Number > 0 && "Lambda should be mangled as an unnamed class");
1355 if (Number > 1)
1356 mangleNumber(Number - 2);
Douglas Gregorccc1b5e2012-02-21 00:37:24 +00001357 Out << '_';
1358}
1359
John McCalla0ce15c2011-04-24 08:23:24 +00001360void CXXNameMangler::manglePrefix(NestedNameSpecifier *qualifier) {
1361 switch (qualifier->getKind()) {
1362 case NestedNameSpecifier::Global:
1363 // nothing
1364 return;
1365
1366 case NestedNameSpecifier::Namespace:
1367 mangleName(qualifier->getAsNamespace());
1368 return;
1369
1370 case NestedNameSpecifier::NamespaceAlias:
1371 mangleName(qualifier->getAsNamespaceAlias()->getNamespace());
1372 return;
1373
1374 case NestedNameSpecifier::TypeSpec:
1375 case NestedNameSpecifier::TypeSpecWithTemplate:
John McCall4f4e4132011-05-04 01:45:19 +00001376 manglePrefix(QualType(qualifier->getAsType(), 0));
John McCalla0ce15c2011-04-24 08:23:24 +00001377 return;
1378
1379 case NestedNameSpecifier::Identifier:
1380 // Member expressions can have these without prefixes, but that
1381 // should end up in mangleUnresolvedPrefix instead.
1382 assert(qualifier->getPrefix());
1383 manglePrefix(qualifier->getPrefix());
1384
1385 mangleSourceName(qualifier->getAsIdentifier());
1386 return;
1387 }
1388
1389 llvm_unreachable("unexpected nested name specifier");
1390}
1391
Fariborz Jahanian57058532010-03-03 19:41:08 +00001392void CXXNameMangler::manglePrefix(const DeclContext *DC, bool NoFunction) {
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001393 // <prefix> ::= <prefix> <unqualified-name>
1394 // ::= <template-prefix> <template-args>
1395 // ::= <template-param>
1396 // ::= # empty
1397 // ::= <substitution>
Anders Carlsson6862fc72009-09-17 04:16:28 +00001398
James Molloyb3c312c2012-03-05 09:59:43 +00001399 DC = IgnoreLinkageSpecDecls(DC);
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00001400
Anders Carlsson9263e912009-09-18 18:39:58 +00001401 if (DC->isTranslationUnit())
1402 return;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00001403
Douglas Gregor35415f52010-05-25 17:04:15 +00001404 if (const BlockDecl *Block = dyn_cast<BlockDecl>(DC)) {
Douglas Gregorccc1b5e2012-02-21 00:37:24 +00001405 manglePrefix(getEffectiveParentContext(DC), NoFunction);
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00001406 SmallString<64> Name;
Rafael Espindolac4850c22011-02-10 23:59:36 +00001407 llvm::raw_svector_ostream NameStream(Name);
1408 Context.mangleBlock(Block, NameStream);
1409 NameStream.flush();
Douglas Gregor35415f52010-05-25 17:04:15 +00001410 Out << Name.size() << Name;
1411 return;
1412 }
1413
Douglas Gregor552e2992012-02-21 02:22:07 +00001414 const NamedDecl *ND = cast<NamedDecl>(DC);
1415 if (mangleSubstitution(ND))
Anders Carlsson6862fc72009-09-17 04:16:28 +00001416 return;
Douglas Gregor552e2992012-02-21 02:22:07 +00001417
Anders Carlsson2ee3fca2009-09-18 20:11:09 +00001418 // Check if we have a template.
1419 const TemplateArgumentList *TemplateArgs = 0;
Douglas Gregor552e2992012-02-21 02:22:07 +00001420 if (const TemplateDecl *TD = isTemplate(ND, TemplateArgs)) {
Anders Carlsson2ee3fca2009-09-18 20:11:09 +00001421 mangleTemplatePrefix(TD);
Eli Friedmand7a6b162012-09-26 02:36:12 +00001422 mangleTemplateArgs(*TemplateArgs);
Fariborz Jahanian57058532010-03-03 19:41:08 +00001423 }
Douglas Gregor552e2992012-02-21 02:22:07 +00001424 else if(NoFunction && (isa<FunctionDecl>(ND) || isa<ObjCMethodDecl>(ND)))
Fariborz Jahanian57058532010-03-03 19:41:08 +00001425 return;
Douglas Gregor552e2992012-02-21 02:22:07 +00001426 else if (const ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(ND))
Douglas Gregor35415f52010-05-25 17:04:15 +00001427 mangleObjCMethodName(Method);
Fariborz Jahanian57058532010-03-03 19:41:08 +00001428 else {
Douglas Gregor552e2992012-02-21 02:22:07 +00001429 manglePrefix(getEffectiveDeclContext(ND), NoFunction);
1430 mangleUnqualifiedName(ND);
Anders Carlsson2ee3fca2009-09-18 20:11:09 +00001431 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00001432
Douglas Gregor552e2992012-02-21 02:22:07 +00001433 addSubstitution(ND);
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001434}
1435
Douglas Gregor20f0cc72010-04-23 03:10:43 +00001436void CXXNameMangler::mangleTemplatePrefix(TemplateName Template) {
1437 // <template-prefix> ::= <prefix> <template unqualified-name>
1438 // ::= <template-param>
1439 // ::= <substitution>
1440 if (TemplateDecl *TD = Template.getAsTemplateDecl())
1441 return mangleTemplatePrefix(TD);
1442
1443 if (QualifiedTemplateName *Qualified = Template.getAsQualifiedTemplateName())
John McCalla0ce15c2011-04-24 08:23:24 +00001444 manglePrefix(Qualified->getQualifier());
Sean Huntc3021132010-05-05 15:23:54 +00001445
Douglas Gregor20f0cc72010-04-23 03:10:43 +00001446 if (OverloadedTemplateStorage *Overloaded
1447 = Template.getAsOverloadedTemplate()) {
Sean Huntc3021132010-05-05 15:23:54 +00001448 mangleUnqualifiedName(0, (*Overloaded->begin())->getDeclName(),
Douglas Gregor20f0cc72010-04-23 03:10:43 +00001449 UnknownArity);
1450 return;
1451 }
Sean Huntc3021132010-05-05 15:23:54 +00001452
Douglas Gregor20f0cc72010-04-23 03:10:43 +00001453 DependentTemplateName *Dependent = Template.getAsDependentTemplateName();
1454 assert(Dependent && "Unknown template name kind?");
John McCalla0ce15c2011-04-24 08:23:24 +00001455 manglePrefix(Dependent->getQualifier());
Douglas Gregor1e9268e2010-04-28 05:58:56 +00001456 mangleUnscopedTemplateName(Template);
Douglas Gregor20f0cc72010-04-23 03:10:43 +00001457}
1458
Anders Carlsson0fa6df42009-09-26 19:45:45 +00001459void CXXNameMangler::mangleTemplatePrefix(const TemplateDecl *ND) {
Anders Carlsson7482e242009-09-18 04:29:09 +00001460 // <template-prefix> ::= <prefix> <template unqualified-name>
1461 // ::= <template-param>
1462 // ::= <substitution>
Douglas Gregor32fb4e12010-02-05 20:45:00 +00001463 // <template-template-param> ::= <template-param>
1464 // <substitution>
Anders Carlsson7482e242009-09-18 04:29:09 +00001465
Anders Carlssonaeb85372009-09-26 22:18:22 +00001466 if (mangleSubstitution(ND))
1467 return;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00001468
Douglas Gregor32fb4e12010-02-05 20:45:00 +00001469 // <template-template-param> ::= <template-param>
1470 if (const TemplateTemplateParmDecl *TTP
1471 = dyn_cast<TemplateTemplateParmDecl>(ND)) {
1472 mangleTemplateParameter(TTP->getIndex());
1473 return;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001474 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00001475
Douglas Gregorccc1b5e2012-02-21 00:37:24 +00001476 manglePrefix(getEffectiveDeclContext(ND));
Anders Carlsson1668f202009-09-26 20:13:56 +00001477 mangleUnqualifiedName(ND->getTemplatedDecl());
Anders Carlssonaeb85372009-09-26 22:18:22 +00001478 addSubstitution(ND);
Anders Carlsson7482e242009-09-18 04:29:09 +00001479}
1480
John McCallb6f532e2010-07-14 06:43:17 +00001481/// Mangles a template name under the production <type>. Required for
1482/// template template arguments.
1483/// <type> ::= <class-enum-type>
1484/// ::= <template-param>
1485/// ::= <substitution>
1486void CXXNameMangler::mangleType(TemplateName TN) {
1487 if (mangleSubstitution(TN))
1488 return;
1489
1490 TemplateDecl *TD = 0;
1491
1492 switch (TN.getKind()) {
1493 case TemplateName::QualifiedTemplate:
1494 TD = TN.getAsQualifiedTemplateName()->getTemplateDecl();
1495 goto HaveDecl;
1496
1497 case TemplateName::Template:
1498 TD = TN.getAsTemplateDecl();
1499 goto HaveDecl;
1500
1501 HaveDecl:
1502 if (isa<TemplateTemplateParmDecl>(TD))
1503 mangleTemplateParameter(cast<TemplateTemplateParmDecl>(TD)->getIndex());
1504 else
1505 mangleName(TD);
1506 break;
1507
1508 case TemplateName::OverloadedTemplate:
1509 llvm_unreachable("can't mangle an overloaded template name as a <type>");
John McCallb6f532e2010-07-14 06:43:17 +00001510
1511 case TemplateName::DependentTemplate: {
1512 const DependentTemplateName *Dependent = TN.getAsDependentTemplateName();
1513 assert(Dependent->isIdentifier());
1514
1515 // <class-enum-type> ::= <name>
1516 // <name> ::= <nested-name>
John McCalla0ce15c2011-04-24 08:23:24 +00001517 mangleUnresolvedPrefix(Dependent->getQualifier(), 0);
John McCallb6f532e2010-07-14 06:43:17 +00001518 mangleSourceName(Dependent->getIdentifier());
1519 break;
1520 }
1521
John McCallb44e0cf2011-06-30 21:59:02 +00001522 case TemplateName::SubstTemplateTemplateParm: {
1523 // Substituted template parameters are mangled as the substituted
1524 // template. This will check for the substitution twice, which is
1525 // fine, but we have to return early so that we don't try to *add*
1526 // the substitution twice.
1527 SubstTemplateTemplateParmStorage *subst
1528 = TN.getAsSubstTemplateTemplateParm();
1529 mangleType(subst->getReplacement());
1530 return;
1531 }
John McCall14606042011-06-30 08:33:18 +00001532
Douglas Gregor1aee05d2011-01-15 06:45:20 +00001533 case TemplateName::SubstTemplateTemplateParmPack: {
John McCall68a51a72011-07-01 00:04:39 +00001534 // FIXME: not clear how to mangle this!
1535 // template <template <class> class T...> class A {
1536 // template <template <class> class U...> void foo(B<T,U> x...);
1537 // };
1538 Out << "_SUBSTPACK_";
Douglas Gregor1aee05d2011-01-15 06:45:20 +00001539 break;
1540 }
John McCallb6f532e2010-07-14 06:43:17 +00001541 }
1542
1543 addSubstitution(TN);
1544}
1545
Mike Stump1eb44332009-09-09 15:08:12 +00001546void
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001547CXXNameMangler::mangleOperatorName(OverloadedOperatorKind OO, unsigned Arity) {
1548 switch (OO) {
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001549 // <operator-name> ::= nw # new
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001550 case OO_New: Out << "nw"; break;
1551 // ::= na # new[]
1552 case OO_Array_New: Out << "na"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001553 // ::= dl # delete
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001554 case OO_Delete: Out << "dl"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001555 // ::= da # delete[]
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001556 case OO_Array_Delete: Out << "da"; break;
1557 // ::= ps # + (unary)
John McCall5e1e89b2010-08-18 19:18:59 +00001558 // ::= pl # + (binary or unknown)
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001559 case OO_Plus:
Anders Carlsson8257d412009-12-22 06:36:32 +00001560 Out << (Arity == 1? "ps" : "pl"); break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001561 // ::= ng # - (unary)
John McCall5e1e89b2010-08-18 19:18:59 +00001562 // ::= mi # - (binary or unknown)
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001563 case OO_Minus:
Anders Carlsson8257d412009-12-22 06:36:32 +00001564 Out << (Arity == 1? "ng" : "mi"); break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001565 // ::= ad # & (unary)
John McCall5e1e89b2010-08-18 19:18:59 +00001566 // ::= an # & (binary or unknown)
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001567 case OO_Amp:
Anders Carlsson8257d412009-12-22 06:36:32 +00001568 Out << (Arity == 1? "ad" : "an"); break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001569 // ::= de # * (unary)
John McCall5e1e89b2010-08-18 19:18:59 +00001570 // ::= ml # * (binary or unknown)
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001571 case OO_Star:
John McCall5e1e89b2010-08-18 19:18:59 +00001572 // Use binary when unknown.
Anders Carlsson8257d412009-12-22 06:36:32 +00001573 Out << (Arity == 1? "de" : "ml"); break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001574 // ::= co # ~
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001575 case OO_Tilde: Out << "co"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001576 // ::= dv # /
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001577 case OO_Slash: Out << "dv"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001578 // ::= rm # %
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001579 case OO_Percent: Out << "rm"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001580 // ::= or # |
1581 case OO_Pipe: Out << "or"; break;
1582 // ::= eo # ^
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001583 case OO_Caret: Out << "eo"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001584 // ::= aS # =
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001585 case OO_Equal: Out << "aS"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001586 // ::= pL # +=
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001587 case OO_PlusEqual: Out << "pL"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001588 // ::= mI # -=
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001589 case OO_MinusEqual: Out << "mI"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001590 // ::= mL # *=
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001591 case OO_StarEqual: Out << "mL"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001592 // ::= dV # /=
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001593 case OO_SlashEqual: Out << "dV"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001594 // ::= rM # %=
1595 case OO_PercentEqual: Out << "rM"; break;
1596 // ::= aN # &=
1597 case OO_AmpEqual: Out << "aN"; break;
1598 // ::= oR # |=
1599 case OO_PipeEqual: Out << "oR"; break;
1600 // ::= eO # ^=
1601 case OO_CaretEqual: Out << "eO"; break;
1602 // ::= ls # <<
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001603 case OO_LessLess: Out << "ls"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001604 // ::= rs # >>
1605 case OO_GreaterGreater: Out << "rs"; break;
1606 // ::= lS # <<=
1607 case OO_LessLessEqual: Out << "lS"; break;
1608 // ::= rS # >>=
1609 case OO_GreaterGreaterEqual: Out << "rS"; break;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001610 // ::= eq # ==
1611 case OO_EqualEqual: Out << "eq"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001612 // ::= ne # !=
1613 case OO_ExclaimEqual: Out << "ne"; break;
1614 // ::= lt # <
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001615 case OO_Less: Out << "lt"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001616 // ::= gt # >
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001617 case OO_Greater: Out << "gt"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001618 // ::= le # <=
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001619 case OO_LessEqual: Out << "le"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001620 // ::= ge # >=
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001621 case OO_GreaterEqual: Out << "ge"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001622 // ::= nt # !
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001623 case OO_Exclaim: Out << "nt"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001624 // ::= aa # &&
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001625 case OO_AmpAmp: Out << "aa"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001626 // ::= oo # ||
1627 case OO_PipePipe: Out << "oo"; break;
1628 // ::= pp # ++
1629 case OO_PlusPlus: Out << "pp"; break;
1630 // ::= mm # --
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001631 case OO_MinusMinus: Out << "mm"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001632 // ::= cm # ,
1633 case OO_Comma: Out << "cm"; break;
1634 // ::= pm # ->*
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001635 case OO_ArrowStar: Out << "pm"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001636 // ::= pt # ->
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001637 case OO_Arrow: Out << "pt"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001638 // ::= cl # ()
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001639 case OO_Call: Out << "cl"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001640 // ::= ix # []
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001641 case OO_Subscript: Out << "ix"; break;
Anders Carlssone170ba72009-12-14 01:45:37 +00001642
1643 // ::= qu # ?
1644 // The conditional operator can't be overloaded, but we still handle it when
1645 // mangling expressions.
1646 case OO_Conditional: Out << "qu"; break;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001647
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001648 case OO_None:
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001649 case NUM_OVERLOADED_OPERATORS:
David Blaikieb219cfc2011-09-23 05:06:16 +00001650 llvm_unreachable("Not an overloaded operator");
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001651 }
1652}
1653
John McCall0953e762009-09-24 19:53:00 +00001654void CXXNameMangler::mangleQualifiers(Qualifiers Quals) {
Mike Stump1eb44332009-09-09 15:08:12 +00001655 // <CV-qualifiers> ::= [r] [V] [K] # restrict (C99), volatile, const
John McCall0953e762009-09-24 19:53:00 +00001656 if (Quals.hasRestrict())
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001657 Out << 'r';
John McCall0953e762009-09-24 19:53:00 +00001658 if (Quals.hasVolatile())
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001659 Out << 'V';
John McCall0953e762009-09-24 19:53:00 +00001660 if (Quals.hasConst())
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001661 Out << 'K';
John McCall0953e762009-09-24 19:53:00 +00001662
Douglas Gregor56079f72010-06-14 23:15:08 +00001663 if (Quals.hasAddressSpace()) {
1664 // Extension:
1665 //
1666 // <type> ::= U <address-space-number>
1667 //
1668 // where <address-space-number> is a source name consisting of 'AS'
1669 // followed by the address space <number>.
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00001670 SmallString<64> ASString;
Douglas Gregor56079f72010-06-14 23:15:08 +00001671 ASString = "AS" + llvm::utostr_32(Quals.getAddressSpace());
1672 Out << 'U' << ASString.size() << ASString;
1673 }
1674
Chris Lattner5f9e2722011-07-23 10:55:15 +00001675 StringRef LifetimeName;
John McCallf85e1932011-06-15 23:02:42 +00001676 switch (Quals.getObjCLifetime()) {
1677 // Objective-C ARC Extension:
1678 //
1679 // <type> ::= U "__strong"
1680 // <type> ::= U "__weak"
1681 // <type> ::= U "__autoreleasing"
John McCallf85e1932011-06-15 23:02:42 +00001682 case Qualifiers::OCL_None:
1683 break;
1684
1685 case Qualifiers::OCL_Weak:
1686 LifetimeName = "__weak";
1687 break;
1688
1689 case Qualifiers::OCL_Strong:
1690 LifetimeName = "__strong";
1691 break;
1692
1693 case Qualifiers::OCL_Autoreleasing:
1694 LifetimeName = "__autoreleasing";
1695 break;
1696
1697 case Qualifiers::OCL_ExplicitNone:
Douglas Gregorc22d6992011-06-17 22:26:49 +00001698 // The __unsafe_unretained qualifier is *not* mangled, so that
1699 // __unsafe_unretained types in ARC produce the same manglings as the
1700 // equivalent (but, naturally, unqualified) types in non-ARC, providing
1701 // better ABI compatibility.
1702 //
1703 // It's safe to do this because unqualified 'id' won't show up
1704 // in any type signatures that need to be mangled.
John McCallf85e1932011-06-15 23:02:42 +00001705 break;
1706 }
1707 if (!LifetimeName.empty())
1708 Out << 'U' << LifetimeName.size() << LifetimeName;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001709}
1710
Douglas Gregor0a9a6d62011-01-26 17:36:28 +00001711void CXXNameMangler::mangleRefQualifier(RefQualifierKind RefQualifier) {
1712 // <ref-qualifier> ::= R # lvalue reference
1713 // ::= O # rvalue-reference
1714 // Proposal to Itanium C++ ABI list on 1/26/11
1715 switch (RefQualifier) {
1716 case RQ_None:
1717 break;
1718
1719 case RQ_LValue:
1720 Out << 'R';
1721 break;
1722
1723 case RQ_RValue:
1724 Out << 'O';
1725 break;
1726 }
1727}
1728
Anders Carlsson7b06f6c2009-12-10 03:14:39 +00001729void CXXNameMangler::mangleObjCMethodName(const ObjCMethodDecl *MD) {
Rafael Espindolaf0be9792011-02-11 02:52:17 +00001730 Context.mangleObjCMethodName(MD, Out);
Anders Carlsson7b06f6c2009-12-10 03:14:39 +00001731}
1732
Douglas Gregorf1588662011-07-12 15:18:55 +00001733void CXXNameMangler::mangleType(QualType T) {
1734 // If our type is instantiation-dependent but not dependent, we mangle
1735 // it as it was written in the source, removing any top-level sugar.
1736 // Otherwise, use the canonical type.
1737 //
1738 // FIXME: This is an approximation of the instantiation-dependent name
1739 // mangling rules, since we should really be using the type as written and
1740 // augmented via semantic analysis (i.e., with implicit conversions and
1741 // default template arguments) for any instantiation-dependent type.
1742 // Unfortunately, that requires several changes to our AST:
1743 // - Instantiation-dependent TemplateSpecializationTypes will need to be
1744 // uniqued, so that we can handle substitutions properly
1745 // - Default template arguments will need to be represented in the
1746 // TemplateSpecializationType, since they need to be mangled even though
1747 // they aren't written.
1748 // - Conversions on non-type template arguments need to be expressed, since
1749 // they can affect the mangling of sizeof/alignof.
1750 if (!T->isInstantiationDependentType() || T->isDependentType())
1751 T = T.getCanonicalType();
1752 else {
1753 // Desugar any types that are purely sugar.
1754 do {
1755 // Don't desugar through template specialization types that aren't
1756 // type aliases. We need to mangle the template arguments as written.
1757 if (const TemplateSpecializationType *TST
1758 = dyn_cast<TemplateSpecializationType>(T))
1759 if (!TST->isTypeAlias())
1760 break;
Anders Carlsson4843e582009-03-10 17:07:44 +00001761
Douglas Gregorf1588662011-07-12 15:18:55 +00001762 QualType Desugared
1763 = T.getSingleStepDesugaredType(Context.getASTContext());
1764 if (Desugared == T)
1765 break;
1766
1767 T = Desugared;
1768 } while (true);
1769 }
1770 SplitQualType split = T.split();
John McCall200fa532012-02-08 00:46:36 +00001771 Qualifiers quals = split.Quals;
1772 const Type *ty = split.Ty;
John McCallb47f7482011-01-26 20:05:40 +00001773
Douglas Gregorf1588662011-07-12 15:18:55 +00001774 bool isSubstitutable = quals || !isa<BuiltinType>(T);
1775 if (isSubstitutable && mangleSubstitution(T))
Anders Carlsson76967372009-09-17 00:43:46 +00001776 return;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001777
John McCallb47f7482011-01-26 20:05:40 +00001778 // If we're mangling a qualified array type, push the qualifiers to
1779 // the element type.
Douglas Gregorf1588662011-07-12 15:18:55 +00001780 if (quals && isa<ArrayType>(T)) {
1781 ty = Context.getASTContext().getAsArrayType(T);
John McCallb47f7482011-01-26 20:05:40 +00001782 quals = Qualifiers();
1783
Douglas Gregorf1588662011-07-12 15:18:55 +00001784 // Note that we don't update T: we want to add the
1785 // substitution at the original type.
John McCallb47f7482011-01-26 20:05:40 +00001786 }
1787
1788 if (quals) {
1789 mangleQualifiers(quals);
John McCall0953e762009-09-24 19:53:00 +00001790 // Recurse: even if the qualified type isn't yet substitutable,
1791 // the unqualified type might be.
John McCallb47f7482011-01-26 20:05:40 +00001792 mangleType(QualType(ty, 0));
Anders Carlsson76967372009-09-17 00:43:46 +00001793 } else {
John McCallb47f7482011-01-26 20:05:40 +00001794 switch (ty->getTypeClass()) {
John McCallefe6aee2009-09-05 07:56:18 +00001795#define ABSTRACT_TYPE(CLASS, PARENT)
1796#define NON_CANONICAL_TYPE(CLASS, PARENT) \
Anders Carlsson76967372009-09-17 00:43:46 +00001797 case Type::CLASS: \
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +00001798 llvm_unreachable("can't mangle non-canonical type " #CLASS "Type"); \
Anders Carlsson76967372009-09-17 00:43:46 +00001799 return;
John McCallefe6aee2009-09-05 07:56:18 +00001800#define TYPE(CLASS, PARENT) \
Anders Carlsson76967372009-09-17 00:43:46 +00001801 case Type::CLASS: \
John McCallb47f7482011-01-26 20:05:40 +00001802 mangleType(static_cast<const CLASS##Type*>(ty)); \
Anders Carlsson76967372009-09-17 00:43:46 +00001803 break;
John McCallefe6aee2009-09-05 07:56:18 +00001804#include "clang/AST/TypeNodes.def"
Anders Carlsson76967372009-09-17 00:43:46 +00001805 }
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001806 }
Anders Carlsson76967372009-09-17 00:43:46 +00001807
1808 // Add the substitution.
John McCallb47f7482011-01-26 20:05:40 +00001809 if (isSubstitutable)
Douglas Gregorf1588662011-07-12 15:18:55 +00001810 addSubstitution(T);
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001811}
1812
Douglas Gregor1b12a3b2010-05-26 05:11:13 +00001813void CXXNameMangler::mangleNameOrStandardSubstitution(const NamedDecl *ND) {
1814 if (!mangleStandardSubstitution(ND))
1815 mangleName(ND);
1816}
1817
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001818void CXXNameMangler::mangleType(const BuiltinType *T) {
John McCallefe6aee2009-09-05 07:56:18 +00001819 // <type> ::= <builtin-type>
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001820 // <builtin-type> ::= v # void
1821 // ::= w # wchar_t
1822 // ::= b # bool
1823 // ::= c # char
1824 // ::= a # signed char
1825 // ::= h # unsigned char
1826 // ::= s # short
1827 // ::= t # unsigned short
1828 // ::= i # int
1829 // ::= j # unsigned int
1830 // ::= l # long
1831 // ::= m # unsigned long
1832 // ::= x # long long, __int64
1833 // ::= y # unsigned long long, __int64
1834 // ::= n # __int128
1835 // UNSUPPORTED: ::= o # unsigned __int128
1836 // ::= f # float
1837 // ::= d # double
1838 // ::= e # long double, __float80
1839 // UNSUPPORTED: ::= g # __float128
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001840 // UNSUPPORTED: ::= Dd # IEEE 754r decimal floating point (64 bits)
1841 // UNSUPPORTED: ::= De # IEEE 754r decimal floating point (128 bits)
1842 // UNSUPPORTED: ::= Df # IEEE 754r decimal floating point (32 bits)
Anton Korobeynikovaa4a99b2011-10-14 23:23:15 +00001843 // ::= Dh # IEEE 754r half-precision floating point (16 bits)
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001844 // ::= Di # char32_t
1845 // ::= Ds # char16_t
Anders Carlssone2923682010-11-04 04:31:32 +00001846 // ::= Dn # std::nullptr_t (i.e., decltype(nullptr))
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001847 // ::= u <source-name> # vendor extended type
1848 switch (T->getKind()) {
1849 case BuiltinType::Void: Out << 'v'; break;
1850 case BuiltinType::Bool: Out << 'b'; break;
1851 case BuiltinType::Char_U: case BuiltinType::Char_S: Out << 'c'; break;
1852 case BuiltinType::UChar: Out << 'h'; break;
1853 case BuiltinType::UShort: Out << 't'; break;
1854 case BuiltinType::UInt: Out << 'j'; break;
1855 case BuiltinType::ULong: Out << 'm'; break;
1856 case BuiltinType::ULongLong: Out << 'y'; break;
Chris Lattner2df9ced2009-04-30 02:43:43 +00001857 case BuiltinType::UInt128: Out << 'o'; break;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001858 case BuiltinType::SChar: Out << 'a'; break;
Chris Lattner3f59c972010-12-25 23:25:43 +00001859 case BuiltinType::WChar_S:
1860 case BuiltinType::WChar_U: Out << 'w'; break;
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001861 case BuiltinType::Char16: Out << "Ds"; break;
1862 case BuiltinType::Char32: Out << "Di"; break;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001863 case BuiltinType::Short: Out << 's'; break;
1864 case BuiltinType::Int: Out << 'i'; break;
1865 case BuiltinType::Long: Out << 'l'; break;
1866 case BuiltinType::LongLong: Out << 'x'; break;
Chris Lattner2df9ced2009-04-30 02:43:43 +00001867 case BuiltinType::Int128: Out << 'n'; break;
Anton Korobeynikovaa4a99b2011-10-14 23:23:15 +00001868 case BuiltinType::Half: Out << "Dh"; break;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001869 case BuiltinType::Float: Out << 'f'; break;
1870 case BuiltinType::Double: Out << 'd'; break;
1871 case BuiltinType::LongDouble: Out << 'e'; break;
Anders Carlssone2923682010-11-04 04:31:32 +00001872 case BuiltinType::NullPtr: Out << "Dn"; break;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001873
John McCalle0a22d02011-10-18 21:02:43 +00001874#define BUILTIN_TYPE(Id, SingletonId)
1875#define PLACEHOLDER_TYPE(Id, SingletonId) \
1876 case BuiltinType::Id:
1877#include "clang/AST/BuiltinTypes.def"
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001878 case BuiltinType::Dependent:
John McCallfb44de92011-05-01 22:35:37 +00001879 llvm_unreachable("mangling a placeholder type");
Steve Naroff9533a7f2009-07-22 17:14:51 +00001880 case BuiltinType::ObjCId: Out << "11objc_object"; break;
1881 case BuiltinType::ObjCClass: Out << "10objc_class"; break;
Fariborz Jahanian13dcd002009-11-21 19:53:08 +00001882 case BuiltinType::ObjCSel: Out << "13objc_selector"; break;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001883 }
1884}
1885
John McCallefe6aee2009-09-05 07:56:18 +00001886// <type> ::= <function-type>
John McCall4b502632012-05-15 02:01:59 +00001887// <function-type> ::= [<CV-qualifiers>] F [Y]
1888// <bare-function-type> [<ref-qualifier>] E
1889// (Proposal to cxx-abi-dev, 2012-05-11)
John McCallefe6aee2009-09-05 07:56:18 +00001890void CXXNameMangler::mangleType(const FunctionProtoType *T) {
John McCall4b502632012-05-15 02:01:59 +00001891 // Mangle CV-qualifiers, if present. These are 'this' qualifiers,
1892 // e.g. "const" in "int (A::*)() const".
1893 mangleQualifiers(Qualifiers::fromCVRMask(T->getTypeQuals()));
1894
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001895 Out << 'F';
John McCall4b502632012-05-15 02:01:59 +00001896
Mike Stumpf5408fe2009-05-16 07:57:57 +00001897 // FIXME: We don't have enough information in the AST to produce the 'Y'
1898 // encoding for extern "C" function types.
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001899 mangleBareFunctionType(T, /*MangleReturnType=*/true);
John McCall4b502632012-05-15 02:01:59 +00001900
1901 // Mangle the ref-qualifier, if present.
1902 mangleRefQualifier(T->getRefQualifier());
1903
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001904 Out << 'E';
1905}
John McCallefe6aee2009-09-05 07:56:18 +00001906void CXXNameMangler::mangleType(const FunctionNoProtoType *T) {
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +00001907 llvm_unreachable("Can't mangle K&R function prototypes");
John McCallefe6aee2009-09-05 07:56:18 +00001908}
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001909void CXXNameMangler::mangleBareFunctionType(const FunctionType *T,
1910 bool MangleReturnType) {
John McCallefe6aee2009-09-05 07:56:18 +00001911 // We should never be mangling something without a prototype.
1912 const FunctionProtoType *Proto = cast<FunctionProtoType>(T);
1913
John McCallfb44de92011-05-01 22:35:37 +00001914 // Record that we're in a function type. See mangleFunctionParam
1915 // for details on what we're trying to achieve here.
1916 FunctionTypeDepthState saved = FunctionTypeDepth.push();
1917
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001918 // <bare-function-type> ::= <signature type>+
John McCallfb44de92011-05-01 22:35:37 +00001919 if (MangleReturnType) {
1920 FunctionTypeDepth.enterResultType();
John McCallefe6aee2009-09-05 07:56:18 +00001921 mangleType(Proto->getResultType());
John McCallfb44de92011-05-01 22:35:37 +00001922 FunctionTypeDepth.leaveResultType();
1923 }
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001924
Anders Carlsson93296682010-06-02 04:40:13 +00001925 if (Proto->getNumArgs() == 0 && !Proto->isVariadic()) {
Eli Friedmana7e68452010-08-22 01:00:03 +00001926 // <builtin-type> ::= v # void
Anders Carlssonc6c91bc2009-04-01 00:15:23 +00001927 Out << 'v';
John McCallfb44de92011-05-01 22:35:37 +00001928
1929 FunctionTypeDepth.pop(saved);
Anders Carlssonc6c91bc2009-04-01 00:15:23 +00001930 return;
1931 }
Mike Stump1eb44332009-09-09 15:08:12 +00001932
Douglas Gregor72564e72009-02-26 23:50:07 +00001933 for (FunctionProtoType::arg_type_iterator Arg = Proto->arg_type_begin(),
Mike Stump1eb44332009-09-09 15:08:12 +00001934 ArgEnd = Proto->arg_type_end();
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001935 Arg != ArgEnd; ++Arg)
Douglas Gregor79e6bd32011-07-12 04:42:08 +00001936 mangleType(Context.getASTContext().getSignatureParameterType(*Arg));
Douglas Gregor219cc612009-02-13 01:28:03 +00001937
John McCallfb44de92011-05-01 22:35:37 +00001938 FunctionTypeDepth.pop(saved);
1939
Douglas Gregor219cc612009-02-13 01:28:03 +00001940 // <builtin-type> ::= z # ellipsis
1941 if (Proto->isVariadic())
1942 Out << 'z';
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001943}
1944
John McCallefe6aee2009-09-05 07:56:18 +00001945// <type> ::= <class-enum-type>
Mike Stump1eb44332009-09-09 15:08:12 +00001946// <class-enum-type> ::= <name>
John McCalled976492009-12-04 22:46:56 +00001947void CXXNameMangler::mangleType(const UnresolvedUsingType *T) {
1948 mangleName(T->getDecl());
1949}
1950
1951// <type> ::= <class-enum-type>
1952// <class-enum-type> ::= <name>
John McCallefe6aee2009-09-05 07:56:18 +00001953void CXXNameMangler::mangleType(const EnumType *T) {
1954 mangleType(static_cast<const TagType*>(T));
1955}
1956void CXXNameMangler::mangleType(const RecordType *T) {
1957 mangleType(static_cast<const TagType*>(T));
1958}
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001959void CXXNameMangler::mangleType(const TagType *T) {
Eli Friedmanecb7e932009-12-11 18:00:57 +00001960 mangleName(T->getDecl());
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001961}
1962
John McCallefe6aee2009-09-05 07:56:18 +00001963// <type> ::= <array-type>
1964// <array-type> ::= A <positive dimension number> _ <element type>
1965// ::= A [<dimension expression>] _ <element type>
1966void CXXNameMangler::mangleType(const ConstantArrayType *T) {
1967 Out << 'A' << T->getSize() << '_';
1968 mangleType(T->getElementType());
1969}
1970void CXXNameMangler::mangleType(const VariableArrayType *T) {
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001971 Out << 'A';
Fariborz Jahanian7281d1f2010-11-02 16:54:00 +00001972 // decayed vla types (size 0) will just be skipped.
1973 if (T->getSizeExpr())
1974 mangleExpression(T->getSizeExpr());
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001975 Out << '_';
1976 mangleType(T->getElementType());
1977}
John McCallefe6aee2009-09-05 07:56:18 +00001978void CXXNameMangler::mangleType(const DependentSizedArrayType *T) {
1979 Out << 'A';
1980 mangleExpression(T->getSizeExpr());
1981 Out << '_';
1982 mangleType(T->getElementType());
1983}
1984void CXXNameMangler::mangleType(const IncompleteArrayType *T) {
Nick Lewycky271b6652010-09-05 03:40:33 +00001985 Out << "A_";
John McCallefe6aee2009-09-05 07:56:18 +00001986 mangleType(T->getElementType());
1987}
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001988
John McCallefe6aee2009-09-05 07:56:18 +00001989// <type> ::= <pointer-to-member-type>
1990// <pointer-to-member-type> ::= M <class type> <member type>
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001991void CXXNameMangler::mangleType(const MemberPointerType *T) {
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001992 Out << 'M';
1993 mangleType(QualType(T->getClass(), 0));
Anders Carlsson0e650012009-05-17 17:41:20 +00001994 QualType PointeeType = T->getPointeeType();
1995 if (const FunctionProtoType *FPT = dyn_cast<FunctionProtoType>(PointeeType)) {
Anders Carlsson0e650012009-05-17 17:41:20 +00001996 mangleType(FPT);
Anders Carlsson9d85b722010-06-02 04:29:50 +00001997
1998 // Itanium C++ ABI 5.1.8:
1999 //
2000 // The type of a non-static member function is considered to be different,
2001 // for the purposes of substitution, from the type of a namespace-scope or
2002 // static member function whose type appears similar. The types of two
2003 // non-static member functions are considered to be different, for the
2004 // purposes of substitution, if the functions are members of different
2005 // classes. In other words, for the purposes of substitution, the class of
2006 // which the function is a member is considered part of the type of
2007 // function.
2008
John McCall4b502632012-05-15 02:01:59 +00002009 // Given that we already substitute member function pointers as a
2010 // whole, the net effect of this rule is just to unconditionally
2011 // suppress substitution on the function type in a member pointer.
Anders Carlsson9d85b722010-06-02 04:29:50 +00002012 // We increment the SeqID here to emulate adding an entry to the
John McCall4b502632012-05-15 02:01:59 +00002013 // substitution table.
Anders Carlsson9d85b722010-06-02 04:29:50 +00002014 ++SeqID;
Mike Stump1eb44332009-09-09 15:08:12 +00002015 } else
Anders Carlsson0e650012009-05-17 17:41:20 +00002016 mangleType(PointeeType);
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00002017}
2018
John McCallefe6aee2009-09-05 07:56:18 +00002019// <type> ::= <template-param>
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00002020void CXXNameMangler::mangleType(const TemplateTypeParmType *T) {
Anders Carlsson0ccdf8d2009-09-27 00:38:53 +00002021 mangleTemplateParameter(T->getIndex());
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00002022}
2023
Douglas Gregorc3069d62011-01-14 02:55:32 +00002024// <type> ::= <template-param>
2025void CXXNameMangler::mangleType(const SubstTemplateTypeParmPackType *T) {
John McCall68a51a72011-07-01 00:04:39 +00002026 // FIXME: not clear how to mangle this!
2027 // template <class T...> class A {
2028 // template <class U...> void foo(T(*)(U) x...);
2029 // };
2030 Out << "_SUBSTPACK_";
Douglas Gregorc3069d62011-01-14 02:55:32 +00002031}
2032
John McCallefe6aee2009-09-05 07:56:18 +00002033// <type> ::= P <type> # pointer-to
2034void CXXNameMangler::mangleType(const PointerType *T) {
2035 Out << 'P';
2036 mangleType(T->getPointeeType());
2037}
2038void CXXNameMangler::mangleType(const ObjCObjectPointerType *T) {
2039 Out << 'P';
2040 mangleType(T->getPointeeType());
2041}
2042
2043// <type> ::= R <type> # reference-to
2044void CXXNameMangler::mangleType(const LValueReferenceType *T) {
2045 Out << 'R';
2046 mangleType(T->getPointeeType());
2047}
2048
2049// <type> ::= O <type> # rvalue reference-to (C++0x)
2050void CXXNameMangler::mangleType(const RValueReferenceType *T) {
2051 Out << 'O';
2052 mangleType(T->getPointeeType());
2053}
2054
2055// <type> ::= C <type> # complex pair (C 2000)
2056void CXXNameMangler::mangleType(const ComplexType *T) {
2057 Out << 'C';
2058 mangleType(T->getElementType());
2059}
2060
Bob Wilsonc7df92d2010-11-12 17:24:43 +00002061// ARM's ABI for Neon vector types specifies that they should be mangled as
Bob Wilson57147a82010-11-16 00:32:18 +00002062// if they are structs (to match ARM's initial implementation). The
2063// vector type must be one of the special types predefined by ARM.
2064void CXXNameMangler::mangleNeonVectorType(const VectorType *T) {
Bob Wilsonc7df92d2010-11-12 17:24:43 +00002065 QualType EltType = T->getElementType();
Bob Wilson57147a82010-11-16 00:32:18 +00002066 assert(EltType->isBuiltinType() && "Neon vector element not a BuiltinType");
Bob Wilsonc7df92d2010-11-12 17:24:43 +00002067 const char *EltName = 0;
Bob Wilson491328c2010-11-12 17:24:46 +00002068 if (T->getVectorKind() == VectorType::NeonPolyVector) {
2069 switch (cast<BuiltinType>(EltType)->getKind()) {
Bob Wilson4cfaa5d2010-11-12 17:24:49 +00002070 case BuiltinType::SChar: EltName = "poly8_t"; break;
2071 case BuiltinType::Short: EltName = "poly16_t"; break;
Bob Wilson57147a82010-11-16 00:32:18 +00002072 default: llvm_unreachable("unexpected Neon polynomial vector element type");
Bob Wilson491328c2010-11-12 17:24:46 +00002073 }
2074 } else {
2075 switch (cast<BuiltinType>(EltType)->getKind()) {
Bob Wilson4cfaa5d2010-11-12 17:24:49 +00002076 case BuiltinType::SChar: EltName = "int8_t"; break;
2077 case BuiltinType::UChar: EltName = "uint8_t"; break;
2078 case BuiltinType::Short: EltName = "int16_t"; break;
2079 case BuiltinType::UShort: EltName = "uint16_t"; break;
2080 case BuiltinType::Int: EltName = "int32_t"; break;
2081 case BuiltinType::UInt: EltName = "uint32_t"; break;
2082 case BuiltinType::LongLong: EltName = "int64_t"; break;
2083 case BuiltinType::ULongLong: EltName = "uint64_t"; break;
2084 case BuiltinType::Float: EltName = "float32_t"; break;
Bob Wilson57147a82010-11-16 00:32:18 +00002085 default: llvm_unreachable("unexpected Neon vector element type");
Bob Wilson491328c2010-11-12 17:24:46 +00002086 }
Bob Wilsonc7df92d2010-11-12 17:24:43 +00002087 }
2088 const char *BaseName = 0;
Bob Wilson4cfaa5d2010-11-12 17:24:49 +00002089 unsigned BitSize = (T->getNumElements() *
Bob Wilson3a723022010-11-16 00:32:12 +00002090 getASTContext().getTypeSize(EltType));
Bob Wilsonc7df92d2010-11-12 17:24:43 +00002091 if (BitSize == 64)
2092 BaseName = "__simd64_";
Bob Wilson57147a82010-11-16 00:32:18 +00002093 else {
2094 assert(BitSize == 128 && "Neon vector type not 64 or 128 bits");
Bob Wilsonc7df92d2010-11-12 17:24:43 +00002095 BaseName = "__simd128_";
Bob Wilson57147a82010-11-16 00:32:18 +00002096 }
Bob Wilsonc7df92d2010-11-12 17:24:43 +00002097 Out << strlen(BaseName) + strlen(EltName);
2098 Out << BaseName << EltName;
Bob Wilsonc7df92d2010-11-12 17:24:43 +00002099}
2100
John McCallefe6aee2009-09-05 07:56:18 +00002101// GNU extension: vector types
Chris Lattner788b0fd2010-06-23 06:00:24 +00002102// <type> ::= <vector-type>
2103// <vector-type> ::= Dv <positive dimension number> _
2104// <extended element type>
2105// ::= Dv [<dimension expression>] _ <element type>
2106// <extended element type> ::= <element type>
2107// ::= p # AltiVec vector pixel
Nick Lewyckyacf0bb42012-10-04 04:58:17 +00002108// ::= b # Altivec vector bool
John McCallefe6aee2009-09-05 07:56:18 +00002109void CXXNameMangler::mangleType(const VectorType *T) {
Bob Wilson491328c2010-11-12 17:24:46 +00002110 if ((T->getVectorKind() == VectorType::NeonVector ||
Bob Wilson57147a82010-11-16 00:32:18 +00002111 T->getVectorKind() == VectorType::NeonPolyVector)) {
2112 mangleNeonVectorType(T);
Bob Wilsonc7df92d2010-11-12 17:24:43 +00002113 return;
Bob Wilson57147a82010-11-16 00:32:18 +00002114 }
Nick Lewycky0e5f0672010-03-26 07:18:04 +00002115 Out << "Dv" << T->getNumElements() << '_';
Bob Wilsone86d78c2010-11-10 21:56:12 +00002116 if (T->getVectorKind() == VectorType::AltiVecPixel)
Chris Lattner788b0fd2010-06-23 06:00:24 +00002117 Out << 'p';
Bob Wilsone86d78c2010-11-10 21:56:12 +00002118 else if (T->getVectorKind() == VectorType::AltiVecBool)
Chris Lattner788b0fd2010-06-23 06:00:24 +00002119 Out << 'b';
2120 else
2121 mangleType(T->getElementType());
John McCallefe6aee2009-09-05 07:56:18 +00002122}
2123void CXXNameMangler::mangleType(const ExtVectorType *T) {
2124 mangleType(static_cast<const VectorType*>(T));
2125}
2126void CXXNameMangler::mangleType(const DependentSizedExtVectorType *T) {
Nick Lewycky0e5f0672010-03-26 07:18:04 +00002127 Out << "Dv";
2128 mangleExpression(T->getSizeExpr());
2129 Out << '_';
John McCallefe6aee2009-09-05 07:56:18 +00002130 mangleType(T->getElementType());
2131}
2132
Douglas Gregor7536dd52010-12-20 02:24:11 +00002133void CXXNameMangler::mangleType(const PackExpansionType *T) {
Douglas Gregor4fc48662011-01-13 16:39:34 +00002134 // <type> ::= Dp <type> # pack expansion (C++0x)
Douglas Gregor255c2692011-01-13 17:44:36 +00002135 Out << "Dp";
Douglas Gregor7536dd52010-12-20 02:24:11 +00002136 mangleType(T->getPattern());
2137}
2138
Anders Carlssona40c5e42009-03-07 22:03:21 +00002139void CXXNameMangler::mangleType(const ObjCInterfaceType *T) {
2140 mangleSourceName(T->getDecl()->getIdentifier());
2141}
2142
John McCallc12c5bb2010-05-15 11:32:37 +00002143void CXXNameMangler::mangleType(const ObjCObjectType *T) {
John McCallc00c1f62010-05-15 17:06:29 +00002144 // We don't allow overloading by different protocol qualification,
2145 // so mangling them isn't necessary.
John McCallc12c5bb2010-05-15 11:32:37 +00002146 mangleType(T->getBaseType());
2147}
2148
John McCallefe6aee2009-09-05 07:56:18 +00002149void CXXNameMangler::mangleType(const BlockPointerType *T) {
Anders Carlssonf28c6872009-12-23 22:31:44 +00002150 Out << "U13block_pointer";
2151 mangleType(T->getPointeeType());
John McCallefe6aee2009-09-05 07:56:18 +00002152}
2153
John McCall31f17ec2010-04-27 00:57:59 +00002154void CXXNameMangler::mangleType(const InjectedClassNameType *T) {
2155 // Mangle injected class name types as if the user had written the
2156 // specialization out fully. It may not actually be possible to see
2157 // this mangling, though.
2158 mangleType(T->getInjectedSpecializationType());
2159}
2160
John McCallefe6aee2009-09-05 07:56:18 +00002161void CXXNameMangler::mangleType(const TemplateSpecializationType *T) {
Douglas Gregor1e9268e2010-04-28 05:58:56 +00002162 if (TemplateDecl *TD = T->getTemplateName().getAsTemplateDecl()) {
2163 mangleName(TD, T->getArgs(), T->getNumArgs());
2164 } else {
2165 if (mangleSubstitution(QualType(T, 0)))
2166 return;
Sean Huntc3021132010-05-05 15:23:54 +00002167
Douglas Gregor1e9268e2010-04-28 05:58:56 +00002168 mangleTemplatePrefix(T->getTemplateName());
Sean Huntc3021132010-05-05 15:23:54 +00002169
Douglas Gregor1e9268e2010-04-28 05:58:56 +00002170 // FIXME: GCC does not appear to mangle the template arguments when
2171 // the template in question is a dependent template name. Should we
2172 // emulate that badness?
Eli Friedmand7a6b162012-09-26 02:36:12 +00002173 mangleTemplateArgs(T->getArgs(), T->getNumArgs());
Douglas Gregor1e9268e2010-04-28 05:58:56 +00002174 addSubstitution(QualType(T, 0));
2175 }
John McCallefe6aee2009-09-05 07:56:18 +00002176}
2177
Douglas Gregor4714c122010-03-31 17:34:00 +00002178void CXXNameMangler::mangleType(const DependentNameType *T) {
Anders Carlssonae352482009-09-26 02:26:02 +00002179 // Typename types are always nested
2180 Out << 'N';
John McCalla0ce15c2011-04-24 08:23:24 +00002181 manglePrefix(T->getQualifier());
John McCall33500952010-06-11 00:33:02 +00002182 mangleSourceName(T->getIdentifier());
2183 Out << 'E';
2184}
John McCall6ab30e02010-06-09 07:26:17 +00002185
John McCall33500952010-06-11 00:33:02 +00002186void CXXNameMangler::mangleType(const DependentTemplateSpecializationType *T) {
Douglas Gregoraa2187d2011-02-28 00:04:36 +00002187 // Dependently-scoped template types are nested if they have a prefix.
John McCall33500952010-06-11 00:33:02 +00002188 Out << 'N';
2189
2190 // TODO: avoid making this TemplateName.
2191 TemplateName Prefix =
2192 getASTContext().getDependentTemplateName(T->getQualifier(),
2193 T->getIdentifier());
2194 mangleTemplatePrefix(Prefix);
2195
2196 // FIXME: GCC does not appear to mangle the template arguments when
2197 // the template in question is a dependent template name. Should we
2198 // emulate that badness?
Eli Friedmand7a6b162012-09-26 02:36:12 +00002199 mangleTemplateArgs(T->getArgs(), T->getNumArgs());
Anders Carlssonae352482009-09-26 02:26:02 +00002200 Out << 'E';
John McCallefe6aee2009-09-05 07:56:18 +00002201}
2202
John McCallad5e7382010-03-01 23:49:17 +00002203void CXXNameMangler::mangleType(const TypeOfType *T) {
2204 // FIXME: this is pretty unsatisfactory, but there isn't an obvious
2205 // "extension with parameters" mangling.
2206 Out << "u6typeof";
2207}
2208
2209void CXXNameMangler::mangleType(const TypeOfExprType *T) {
2210 // FIXME: this is pretty unsatisfactory, but there isn't an obvious
2211 // "extension with parameters" mangling.
2212 Out << "u6typeof";
2213}
2214
2215void CXXNameMangler::mangleType(const DecltypeType *T) {
2216 Expr *E = T->getUnderlyingExpr();
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002217
John McCallad5e7382010-03-01 23:49:17 +00002218 // type ::= Dt <expression> E # decltype of an id-expression
2219 // # or class member access
2220 // ::= DT <expression> E # decltype of an expression
2221
2222 // This purports to be an exhaustive list of id-expressions and
2223 // class member accesses. Note that we do not ignore parentheses;
2224 // parentheses change the semantics of decltype for these
2225 // expressions (and cause the mangler to use the other form).
2226 if (isa<DeclRefExpr>(E) ||
2227 isa<MemberExpr>(E) ||
2228 isa<UnresolvedLookupExpr>(E) ||
2229 isa<DependentScopeDeclRefExpr>(E) ||
2230 isa<CXXDependentScopeMemberExpr>(E) ||
2231 isa<UnresolvedMemberExpr>(E))
2232 Out << "Dt";
2233 else
2234 Out << "DT";
2235 mangleExpression(E);
2236 Out << 'E';
2237}
2238
Sean Huntca63c202011-05-24 22:41:36 +00002239void CXXNameMangler::mangleType(const UnaryTransformType *T) {
2240 // If this is dependent, we need to record that. If not, we simply
2241 // mangle it as the underlying type since they are equivalent.
2242 if (T->isDependentType()) {
2243 Out << 'U';
2244
2245 switch (T->getUTTKind()) {
2246 case UnaryTransformType::EnumUnderlyingType:
2247 Out << "3eut";
2248 break;
2249 }
2250 }
2251
2252 mangleType(T->getUnderlyingType());
2253}
2254
Richard Smith34b41d92011-02-20 03:19:35 +00002255void CXXNameMangler::mangleType(const AutoType *T) {
2256 QualType D = T->getDeducedType();
Richard Smith967ecd32011-02-21 20:10:02 +00002257 // <builtin-type> ::= Da # dependent auto
2258 if (D.isNull())
2259 Out << "Da";
2260 else
2261 mangleType(D);
Richard Smith34b41d92011-02-20 03:19:35 +00002262}
2263
Eli Friedmanb001de72011-10-06 23:00:33 +00002264void CXXNameMangler::mangleType(const AtomicType *T) {
2265 // <type> ::= U <source-name> <type> # vendor extended type qualifier
2266 // (Until there's a standardized mangling...)
2267 Out << "U7_Atomic";
2268 mangleType(T->getValueType());
2269}
2270
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002271void CXXNameMangler::mangleIntegerLiteral(QualType T,
Anders Carlssone170ba72009-12-14 01:45:37 +00002272 const llvm::APSInt &Value) {
2273 // <expr-primary> ::= L <type> <value number> E # integer literal
2274 Out << 'L';
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002275
Anders Carlssone170ba72009-12-14 01:45:37 +00002276 mangleType(T);
2277 if (T->isBooleanType()) {
2278 // Boolean values are encoded as 0/1.
2279 Out << (Value.getBoolValue() ? '1' : '0');
2280 } else {
John McCall0512e482010-07-14 04:20:34 +00002281 mangleNumber(Value);
Anders Carlssone170ba72009-12-14 01:45:37 +00002282 }
2283 Out << 'E';
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002284
Anders Carlssone170ba72009-12-14 01:45:37 +00002285}
2286
Douglas Gregorcefc3af2012-04-16 07:05:22 +00002287/// Mangles a member expression.
John McCalla0ce15c2011-04-24 08:23:24 +00002288void CXXNameMangler::mangleMemberExpr(const Expr *base,
2289 bool isArrow,
2290 NestedNameSpecifier *qualifier,
2291 NamedDecl *firstQualifierLookup,
2292 DeclarationName member,
2293 unsigned arity) {
2294 // <expression> ::= dt <expression> <unresolved-name>
2295 // ::= pt <expression> <unresolved-name>
Douglas Gregorcefc3af2012-04-16 07:05:22 +00002296 if (base) {
2297 if (base->isImplicitCXXThis()) {
2298 // Note: GCC mangles member expressions to the implicit 'this' as
2299 // *this., whereas we represent them as this->. The Itanium C++ ABI
2300 // does not specify anything here, so we follow GCC.
2301 Out << "dtdefpT";
2302 } else {
2303 Out << (isArrow ? "pt" : "dt");
2304 mangleExpression(base);
2305 }
2306 }
John McCalla0ce15c2011-04-24 08:23:24 +00002307 mangleUnresolvedName(qualifier, firstQualifierLookup, member, arity);
John McCall2f27bf82010-02-04 02:56:29 +00002308}
2309
John McCall5a7e6f72011-04-28 02:52:03 +00002310/// Look at the callee of the given call expression and determine if
2311/// it's a parenthesized id-expression which would have triggered ADL
2312/// otherwise.
2313static bool isParenthesizedADLCallee(const CallExpr *call) {
2314 const Expr *callee = call->getCallee();
2315 const Expr *fn = callee->IgnoreParens();
2316
2317 // Must be parenthesized. IgnoreParens() skips __extension__ nodes,
2318 // too, but for those to appear in the callee, it would have to be
2319 // parenthesized.
2320 if (callee == fn) return false;
2321
2322 // Must be an unresolved lookup.
2323 const UnresolvedLookupExpr *lookup = dyn_cast<UnresolvedLookupExpr>(fn);
2324 if (!lookup) return false;
2325
2326 assert(!lookup->requiresADL());
2327
2328 // Must be an unqualified lookup.
2329 if (lookup->getQualifier()) return false;
2330
2331 // Must not have found a class member. Note that if one is a class
2332 // member, they're all class members.
2333 if (lookup->getNumDecls() > 0 &&
2334 (*lookup->decls_begin())->isCXXClassMember())
2335 return false;
2336
2337 // Otherwise, ADL would have been triggered.
2338 return true;
2339}
2340
John McCall5e1e89b2010-08-18 19:18:59 +00002341void CXXNameMangler::mangleExpression(const Expr *E, unsigned Arity) {
Anders Carlssond553f8c2009-09-21 01:21:10 +00002342 // <expression> ::= <unary operator-name> <expression>
John McCall09cc1412010-02-03 00:55:45 +00002343 // ::= <binary operator-name> <expression> <expression>
2344 // ::= <trinary operator-name> <expression> <expression> <expression>
Anders Carlssond553f8c2009-09-21 01:21:10 +00002345 // ::= cv <type> expression # conversion with one argument
2346 // ::= cv <type> _ <expression>* E # conversion with a different number of arguments
Eli Friedmana7e68452010-08-22 01:00:03 +00002347 // ::= st <type> # sizeof (a type)
Anders Carlssond553f8c2009-09-21 01:21:10 +00002348 // ::= at <type> # alignof (a type)
2349 // ::= <template-param>
2350 // ::= <function-param>
2351 // ::= sr <type> <unqualified-name> # dependent name
2352 // ::= sr <type> <unqualified-name> <template-args> # dependent template-id
Douglas Gregor63f62df2011-06-05 05:27:58 +00002353 // ::= ds <expression> <expression> # expr.*expr
Anders Carlssond553f8c2009-09-21 01:21:10 +00002354 // ::= sZ <template-param> # size of a parameter pack
Douglas Gregor4fc48662011-01-13 16:39:34 +00002355 // ::= sZ <function-param> # size of a function parameter pack
John McCall09cc1412010-02-03 00:55:45 +00002356 // ::= <expr-primary>
John McCall1dd73832010-02-04 01:42:13 +00002357 // <expr-primary> ::= L <type> <value number> E # integer literal
2358 // ::= L <type <value float> E # floating literal
2359 // ::= L <mangled-name> E # external name
Douglas Gregorcefc3af2012-04-16 07:05:22 +00002360 // ::= fpT # 'this' expression
Douglas Gregoredee94b2011-07-12 04:47:20 +00002361 QualType ImplicitlyConvertedToType;
2362
2363recurse:
Anders Carlssond553f8c2009-09-21 01:21:10 +00002364 switch (E->getStmtClass()) {
John McCall6ae1f352010-04-09 22:26:14 +00002365 case Expr::NoStmtClass:
John McCall63c00d72011-02-09 08:16:59 +00002366#define ABSTRACT_STMT(Type)
John McCall6ae1f352010-04-09 22:26:14 +00002367#define EXPR(Type, Base)
2368#define STMT(Type, Base) \
2369 case Expr::Type##Class:
Sean Hunt4bfe1962010-05-05 15:24:00 +00002370#include "clang/AST/StmtNodes.inc"
John McCall0512e482010-07-14 04:20:34 +00002371 // fallthrough
2372
2373 // These all can only appear in local or variable-initialization
2374 // contexts and so should never appear in a mangling.
2375 case Expr::AddrLabelExprClass:
John McCall0512e482010-07-14 04:20:34 +00002376 case Expr::DesignatedInitExprClass:
2377 case Expr::ImplicitValueInitExprClass:
John McCall0512e482010-07-14 04:20:34 +00002378 case Expr::ParenListExprClass:
Douglas Gregor01d08012012-02-07 10:09:13 +00002379 case Expr::LambdaExprClass:
John McCall09cc1412010-02-03 00:55:45 +00002380 llvm_unreachable("unexpected statement kind");
John McCall09cc1412010-02-03 00:55:45 +00002381
John McCall0512e482010-07-14 04:20:34 +00002382 // FIXME: invent manglings for all these.
2383 case Expr::BlockExprClass:
2384 case Expr::CXXPseudoDestructorExprClass:
2385 case Expr::ChooseExprClass:
2386 case Expr::CompoundLiteralExprClass:
2387 case Expr::ExtVectorElementExprClass:
Peter Collingbournef111d932011-04-15 00:35:48 +00002388 case Expr::GenericSelectionExprClass:
John McCall0512e482010-07-14 04:20:34 +00002389 case Expr::ObjCEncodeExprClass:
John McCall0512e482010-07-14 04:20:34 +00002390 case Expr::ObjCIsaExprClass:
2391 case Expr::ObjCIvarRefExprClass:
2392 case Expr::ObjCMessageExprClass:
2393 case Expr::ObjCPropertyRefExprClass:
2394 case Expr::ObjCProtocolExprClass:
2395 case Expr::ObjCSelectorExprClass:
2396 case Expr::ObjCStringLiteralClass:
Patrick Beardeb382ec2012-04-19 00:25:12 +00002397 case Expr::ObjCBoxedExprClass:
Ted Kremenekebcb57a2012-03-06 20:05:56 +00002398 case Expr::ObjCArrayLiteralClass:
2399 case Expr::ObjCDictionaryLiteralClass:
2400 case Expr::ObjCSubscriptRefExprClass:
John McCallf85e1932011-06-15 23:02:42 +00002401 case Expr::ObjCIndirectCopyRestoreExprClass:
John McCall0512e482010-07-14 04:20:34 +00002402 case Expr::OffsetOfExprClass:
2403 case Expr::PredefinedExprClass:
2404 case Expr::ShuffleVectorExprClass:
2405 case Expr::StmtExprClass:
John McCall0512e482010-07-14 04:20:34 +00002406 case Expr::UnaryTypeTraitExprClass:
Francois Pichet6ad6f282010-12-07 00:08:36 +00002407 case Expr::BinaryTypeTraitExprClass:
Douglas Gregor4ca8ac22012-02-24 07:38:34 +00002408 case Expr::TypeTraitExprClass:
John Wiegley21ff2e52011-04-28 00:16:57 +00002409 case Expr::ArrayTypeTraitExprClass:
John Wiegley55262202011-04-25 06:54:41 +00002410 case Expr::ExpressionTraitExprClass:
Francois Pichet9be88402010-09-08 23:47:05 +00002411 case Expr::VAArgExprClass:
Sebastian Redl2e156222010-09-10 20:55:43 +00002412 case Expr::CXXUuidofExprClass:
Tanya Lattner61eee0c2011-06-04 00:47:47 +00002413 case Expr::CUDAKernelCallExprClass:
2414 case Expr::AsTypeExprClass:
John McCall4b9c2d22011-11-06 09:01:30 +00002415 case Expr::PseudoObjectExprClass:
Eli Friedman276b0612011-10-11 02:20:01 +00002416 case Expr::AtomicExprClass:
Tanya Lattner61eee0c2011-06-04 00:47:47 +00002417 {
John McCall6ae1f352010-04-09 22:26:14 +00002418 // As bad as this diagnostic is, it's better than crashing.
David Blaikied6471f72011-09-25 23:23:43 +00002419 DiagnosticsEngine &Diags = Context.getDiags();
2420 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
John McCall6ae1f352010-04-09 22:26:14 +00002421 "cannot yet mangle expression type %0");
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +00002422 Diags.Report(E->getExprLoc(), DiagID)
John McCall739bf092010-04-10 09:39:25 +00002423 << E->getStmtClassName() << E->getSourceRange();
John McCall6ae1f352010-04-09 22:26:14 +00002424 break;
2425 }
2426
John McCall56ca35d2011-02-17 10:25:35 +00002427 // Even gcc-4.5 doesn't mangle this.
2428 case Expr::BinaryConditionalOperatorClass: {
David Blaikied6471f72011-09-25 23:23:43 +00002429 DiagnosticsEngine &Diags = Context.getDiags();
John McCall56ca35d2011-02-17 10:25:35 +00002430 unsigned DiagID =
David Blaikied6471f72011-09-25 23:23:43 +00002431 Diags.getCustomDiagID(DiagnosticsEngine::Error,
John McCall56ca35d2011-02-17 10:25:35 +00002432 "?: operator with omitted middle operand cannot be mangled");
2433 Diags.Report(E->getExprLoc(), DiagID)
2434 << E->getStmtClassName() << E->getSourceRange();
2435 break;
2436 }
2437
2438 // These are used for internal purposes and cannot be meaningfully mangled.
John McCall7cd7d1a2010-11-15 23:31:06 +00002439 case Expr::OpaqueValueExprClass:
2440 llvm_unreachable("cannot mangle opaque value; mangling wrong thing?");
2441
Sebastian Redlfaf4ef62012-02-25 22:59:28 +00002442 case Expr::InitListExprClass: {
2443 // Proposal by Jason Merrill, 2012-01-03
2444 Out << "il";
2445 const InitListExpr *InitList = cast<InitListExpr>(E);
2446 for (unsigned i = 0, e = InitList->getNumInits(); i != e; ++i)
2447 mangleExpression(InitList->getInit(i));
2448 Out << "E";
2449 break;
2450 }
2451
John McCall0512e482010-07-14 04:20:34 +00002452 case Expr::CXXDefaultArgExprClass:
John McCall5e1e89b2010-08-18 19:18:59 +00002453 mangleExpression(cast<CXXDefaultArgExpr>(E)->getExpr(), Arity);
John McCall0512e482010-07-14 04:20:34 +00002454 break;
2455
John McCall91a57552011-07-15 05:09:51 +00002456 case Expr::SubstNonTypeTemplateParmExprClass:
2457 mangleExpression(cast<SubstNonTypeTemplateParmExpr>(E)->getReplacement(),
2458 Arity);
2459 break;
2460
Richard Smith9fcce652012-03-07 08:35:16 +00002461 case Expr::UserDefinedLiteralClass:
2462 // We follow g++'s approach of mangling a UDL as a call to the literal
2463 // operator.
John McCall0512e482010-07-14 04:20:34 +00002464 case Expr::CXXMemberCallExprClass: // fallthrough
John McCall1dd73832010-02-04 01:42:13 +00002465 case Expr::CallExprClass: {
2466 const CallExpr *CE = cast<CallExpr>(E);
John McCall5a7e6f72011-04-28 02:52:03 +00002467
2468 // <expression> ::= cp <simple-id> <expression>* E
2469 // We use this mangling only when the call would use ADL except
2470 // for being parenthesized. Per discussion with David
2471 // Vandervoorde, 2011.04.25.
2472 if (isParenthesizedADLCallee(CE)) {
2473 Out << "cp";
2474 // The callee here is a parenthesized UnresolvedLookupExpr with
2475 // no qualifier and should always get mangled as a <simple-id>
2476 // anyway.
2477
2478 // <expression> ::= cl <expression>* E
2479 } else {
2480 Out << "cl";
2481 }
2482
John McCall5e1e89b2010-08-18 19:18:59 +00002483 mangleExpression(CE->getCallee(), CE->getNumArgs());
John McCall1dd73832010-02-04 01:42:13 +00002484 for (unsigned I = 0, N = CE->getNumArgs(); I != N; ++I)
2485 mangleExpression(CE->getArg(I));
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002486 Out << 'E';
John McCall09cc1412010-02-03 00:55:45 +00002487 break;
John McCall1dd73832010-02-04 01:42:13 +00002488 }
John McCall09cc1412010-02-03 00:55:45 +00002489
John McCall0512e482010-07-14 04:20:34 +00002490 case Expr::CXXNewExprClass: {
John McCall0512e482010-07-14 04:20:34 +00002491 const CXXNewExpr *New = cast<CXXNewExpr>(E);
2492 if (New->isGlobalNew()) Out << "gs";
2493 Out << (New->isArray() ? "na" : "nw");
2494 for (CXXNewExpr::const_arg_iterator I = New->placement_arg_begin(),
2495 E = New->placement_arg_end(); I != E; ++I)
2496 mangleExpression(*I);
2497 Out << '_';
2498 mangleType(New->getAllocatedType());
2499 if (New->hasInitializer()) {
Sebastian Redlfaf4ef62012-02-25 22:59:28 +00002500 // Proposal by Jason Merrill, 2012-01-03
Sebastian Redlb76ffc52012-02-25 20:51:07 +00002501 if (New->getInitializationStyle() == CXXNewExpr::ListInit)
Sebastian Redlfaf4ef62012-02-25 22:59:28 +00002502 Out << "il";
Sebastian Redlb76ffc52012-02-25 20:51:07 +00002503 else
2504 Out << "pi";
Sebastian Redl2aed8b82012-02-16 12:22:20 +00002505 const Expr *Init = New->getInitializer();
2506 if (const CXXConstructExpr *CCE = dyn_cast<CXXConstructExpr>(Init)) {
2507 // Directly inline the initializers.
2508 for (CXXConstructExpr::const_arg_iterator I = CCE->arg_begin(),
2509 E = CCE->arg_end();
2510 I != E; ++I)
2511 mangleExpression(*I);
2512 } else if (const ParenListExpr *PLE = dyn_cast<ParenListExpr>(Init)) {
2513 for (unsigned i = 0, e = PLE->getNumExprs(); i != e; ++i)
2514 mangleExpression(PLE->getExpr(i));
Sebastian Redlb76ffc52012-02-25 20:51:07 +00002515 } else if (New->getInitializationStyle() == CXXNewExpr::ListInit &&
2516 isa<InitListExpr>(Init)) {
Sebastian Redlfaf4ef62012-02-25 22:59:28 +00002517 // Only take InitListExprs apart for list-initialization.
Sebastian Redlb76ffc52012-02-25 20:51:07 +00002518 const InitListExpr *InitList = cast<InitListExpr>(Init);
2519 for (unsigned i = 0, e = InitList->getNumInits(); i != e; ++i)
2520 mangleExpression(InitList->getInit(i));
Sebastian Redl2aed8b82012-02-16 12:22:20 +00002521 } else
2522 mangleExpression(Init);
John McCall0512e482010-07-14 04:20:34 +00002523 }
2524 Out << 'E';
2525 break;
2526 }
2527
John McCall2f27bf82010-02-04 02:56:29 +00002528 case Expr::MemberExprClass: {
2529 const MemberExpr *ME = cast<MemberExpr>(E);
2530 mangleMemberExpr(ME->getBase(), ME->isArrow(),
John McCalla0ce15c2011-04-24 08:23:24 +00002531 ME->getQualifier(), 0, ME->getMemberDecl()->getDeclName(),
John McCall5e1e89b2010-08-18 19:18:59 +00002532 Arity);
John McCall2f27bf82010-02-04 02:56:29 +00002533 break;
2534 }
2535
2536 case Expr::UnresolvedMemberExprClass: {
2537 const UnresolvedMemberExpr *ME = cast<UnresolvedMemberExpr>(E);
2538 mangleMemberExpr(ME->getBase(), ME->isArrow(),
John McCalla0ce15c2011-04-24 08:23:24 +00002539 ME->getQualifier(), 0, ME->getMemberName(),
John McCall5e1e89b2010-08-18 19:18:59 +00002540 Arity);
John McCall6dbce192010-08-20 00:17:19 +00002541 if (ME->hasExplicitTemplateArgs())
2542 mangleTemplateArgs(ME->getExplicitTemplateArgs());
John McCall2f27bf82010-02-04 02:56:29 +00002543 break;
2544 }
2545
2546 case Expr::CXXDependentScopeMemberExprClass: {
2547 const CXXDependentScopeMemberExpr *ME
2548 = cast<CXXDependentScopeMemberExpr>(E);
2549 mangleMemberExpr(ME->getBase(), ME->isArrow(),
John McCalla0ce15c2011-04-24 08:23:24 +00002550 ME->getQualifier(), ME->getFirstQualifierFoundInScope(),
2551 ME->getMember(), Arity);
John McCall6dbce192010-08-20 00:17:19 +00002552 if (ME->hasExplicitTemplateArgs())
2553 mangleTemplateArgs(ME->getExplicitTemplateArgs());
John McCall2f27bf82010-02-04 02:56:29 +00002554 break;
2555 }
2556
John McCall1dd73832010-02-04 01:42:13 +00002557 case Expr::UnresolvedLookupExprClass: {
2558 const UnresolvedLookupExpr *ULE = cast<UnresolvedLookupExpr>(E);
John McCalla0ce15c2011-04-24 08:23:24 +00002559 mangleUnresolvedName(ULE->getQualifier(), 0, ULE->getName(), Arity);
John McCall26a6ec72011-06-21 22:12:46 +00002560
2561 // All the <unresolved-name> productions end in a
2562 // base-unresolved-name, where <template-args> are just tacked
2563 // onto the end.
John McCall6dbce192010-08-20 00:17:19 +00002564 if (ULE->hasExplicitTemplateArgs())
2565 mangleTemplateArgs(ULE->getExplicitTemplateArgs());
John McCall1dd73832010-02-04 01:42:13 +00002566 break;
2567 }
2568
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002569 case Expr::CXXUnresolvedConstructExprClass: {
John McCall1dd73832010-02-04 01:42:13 +00002570 const CXXUnresolvedConstructExpr *CE = cast<CXXUnresolvedConstructExpr>(E);
2571 unsigned N = CE->arg_size();
2572
2573 Out << "cv";
2574 mangleType(CE->getType());
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002575 if (N != 1) Out << '_';
John McCall1dd73832010-02-04 01:42:13 +00002576 for (unsigned I = 0; I != N; ++I) mangleExpression(CE->getArg(I));
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002577 if (N != 1) Out << 'E';
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002578 break;
John McCall1dd73832010-02-04 01:42:13 +00002579 }
John McCall09cc1412010-02-03 00:55:45 +00002580
John McCall1dd73832010-02-04 01:42:13 +00002581 case Expr::CXXTemporaryObjectExprClass:
2582 case Expr::CXXConstructExprClass: {
2583 const CXXConstructExpr *CE = cast<CXXConstructExpr>(E);
2584 unsigned N = CE->getNumArgs();
2585
Sebastian Redlfaf4ef62012-02-25 22:59:28 +00002586 // Proposal by Jason Merrill, 2012-01-03
2587 if (CE->isListInitialization())
2588 Out << "tl";
2589 else
2590 Out << "cv";
John McCall1dd73832010-02-04 01:42:13 +00002591 mangleType(CE->getType());
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002592 if (N != 1) Out << '_';
John McCall1dd73832010-02-04 01:42:13 +00002593 for (unsigned I = 0; I != N; ++I) mangleExpression(CE->getArg(I));
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002594 if (N != 1) Out << 'E';
John McCall09cc1412010-02-03 00:55:45 +00002595 break;
John McCall1dd73832010-02-04 01:42:13 +00002596 }
2597
Richard Smith41576d42012-02-06 02:54:51 +00002598 case Expr::CXXScalarValueInitExprClass:
2599 Out <<"cv";
2600 mangleType(E->getType());
2601 Out <<"_E";
2602 break;
2603
John McCall9653ab52012-09-25 09:10:17 +00002604 case Expr::CXXNoexceptExprClass:
2605 Out << "nx";
2606 mangleExpression(cast<CXXNoexceptExpr>(E)->getOperand());
2607 break;
2608
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +00002609 case Expr::UnaryExprOrTypeTraitExprClass: {
2610 const UnaryExprOrTypeTraitExpr *SAE = cast<UnaryExprOrTypeTraitExpr>(E);
Douglas Gregoredee94b2011-07-12 04:47:20 +00002611
2612 if (!SAE->isInstantiationDependent()) {
2613 // Itanium C++ ABI:
2614 // If the operand of a sizeof or alignof operator is not
2615 // instantiation-dependent it is encoded as an integer literal
2616 // reflecting the result of the operator.
2617 //
2618 // If the result of the operator is implicitly converted to a known
2619 // integer type, that type is used for the literal; otherwise, the type
2620 // of std::size_t or std::ptrdiff_t is used.
2621 QualType T = (ImplicitlyConvertedToType.isNull() ||
2622 !ImplicitlyConvertedToType->isIntegerType())? SAE->getType()
2623 : ImplicitlyConvertedToType;
Richard Smitha6b8b2c2011-10-10 18:28:20 +00002624 llvm::APSInt V = SAE->EvaluateKnownConstInt(Context.getASTContext());
2625 mangleIntegerLiteral(T, V);
Douglas Gregoredee94b2011-07-12 04:47:20 +00002626 break;
2627 }
2628
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +00002629 switch(SAE->getKind()) {
2630 case UETT_SizeOf:
2631 Out << 's';
2632 break;
2633 case UETT_AlignOf:
2634 Out << 'a';
2635 break;
2636 case UETT_VecStep:
David Blaikied6471f72011-09-25 23:23:43 +00002637 DiagnosticsEngine &Diags = Context.getDiags();
2638 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +00002639 "cannot yet mangle vec_step expression");
2640 Diags.Report(DiagID);
2641 return;
2642 }
John McCall1dd73832010-02-04 01:42:13 +00002643 if (SAE->isArgumentType()) {
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002644 Out << 't';
John McCall1dd73832010-02-04 01:42:13 +00002645 mangleType(SAE->getArgumentType());
2646 } else {
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002647 Out << 'z';
John McCall1dd73832010-02-04 01:42:13 +00002648 mangleExpression(SAE->getArgumentExpr());
2649 }
2650 break;
2651 }
Anders Carlssona7694082009-11-06 02:50:19 +00002652
John McCall0512e482010-07-14 04:20:34 +00002653 case Expr::CXXThrowExprClass: {
2654 const CXXThrowExpr *TE = cast<CXXThrowExpr>(E);
2655
2656 // Proposal from David Vandervoorde, 2010.06.30
2657 if (TE->getSubExpr()) {
2658 Out << "tw";
2659 mangleExpression(TE->getSubExpr());
2660 } else {
2661 Out << "tr";
2662 }
2663 break;
2664 }
2665
2666 case Expr::CXXTypeidExprClass: {
2667 const CXXTypeidExpr *TIE = cast<CXXTypeidExpr>(E);
2668
2669 // Proposal from David Vandervoorde, 2010.06.30
2670 if (TIE->isTypeOperand()) {
2671 Out << "ti";
2672 mangleType(TIE->getTypeOperand());
2673 } else {
2674 Out << "te";
2675 mangleExpression(TIE->getExprOperand());
2676 }
2677 break;
2678 }
2679
2680 case Expr::CXXDeleteExprClass: {
2681 const CXXDeleteExpr *DE = cast<CXXDeleteExpr>(E);
2682
2683 // Proposal from David Vandervoorde, 2010.06.30
2684 if (DE->isGlobalDelete()) Out << "gs";
2685 Out << (DE->isArrayForm() ? "da" : "dl");
2686 mangleExpression(DE->getArgument());
2687 break;
2688 }
2689
Anders Carlssone170ba72009-12-14 01:45:37 +00002690 case Expr::UnaryOperatorClass: {
2691 const UnaryOperator *UO = cast<UnaryOperator>(E);
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002692 mangleOperatorName(UnaryOperator::getOverloadedOperator(UO->getOpcode()),
Anders Carlssone170ba72009-12-14 01:45:37 +00002693 /*Arity=*/1);
2694 mangleExpression(UO->getSubExpr());
2695 break;
2696 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002697
John McCall0512e482010-07-14 04:20:34 +00002698 case Expr::ArraySubscriptExprClass: {
2699 const ArraySubscriptExpr *AE = cast<ArraySubscriptExpr>(E);
2700
Chris Lattnerfc8f0e12011-04-15 05:22:18 +00002701 // Array subscript is treated as a syntactically weird form of
John McCall0512e482010-07-14 04:20:34 +00002702 // binary operator.
2703 Out << "ix";
2704 mangleExpression(AE->getLHS());
2705 mangleExpression(AE->getRHS());
2706 break;
2707 }
2708
2709 case Expr::CompoundAssignOperatorClass: // fallthrough
Anders Carlssone170ba72009-12-14 01:45:37 +00002710 case Expr::BinaryOperatorClass: {
2711 const BinaryOperator *BO = cast<BinaryOperator>(E);
Douglas Gregor63f62df2011-06-05 05:27:58 +00002712 if (BO->getOpcode() == BO_PtrMemD)
2713 Out << "ds";
2714 else
2715 mangleOperatorName(BinaryOperator::getOverloadedOperator(BO->getOpcode()),
2716 /*Arity=*/2);
Anders Carlssone170ba72009-12-14 01:45:37 +00002717 mangleExpression(BO->getLHS());
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002718 mangleExpression(BO->getRHS());
Anders Carlssone170ba72009-12-14 01:45:37 +00002719 break;
John McCall2f27bf82010-02-04 02:56:29 +00002720 }
Anders Carlssone170ba72009-12-14 01:45:37 +00002721
2722 case Expr::ConditionalOperatorClass: {
2723 const ConditionalOperator *CO = cast<ConditionalOperator>(E);
2724 mangleOperatorName(OO_Conditional, /*Arity=*/3);
2725 mangleExpression(CO->getCond());
John McCall5e1e89b2010-08-18 19:18:59 +00002726 mangleExpression(CO->getLHS(), Arity);
2727 mangleExpression(CO->getRHS(), Arity);
Anders Carlssone170ba72009-12-14 01:45:37 +00002728 break;
2729 }
2730
Douglas Gregor46287c72010-01-29 16:37:09 +00002731 case Expr::ImplicitCastExprClass: {
Douglas Gregoredee94b2011-07-12 04:47:20 +00002732 ImplicitlyConvertedToType = E->getType();
2733 E = cast<ImplicitCastExpr>(E)->getSubExpr();
2734 goto recurse;
Douglas Gregor46287c72010-01-29 16:37:09 +00002735 }
John McCallf85e1932011-06-15 23:02:42 +00002736
2737 case Expr::ObjCBridgedCastExprClass: {
2738 // Mangle ownership casts as a vendor extended operator __bridge,
2739 // __bridge_transfer, or __bridge_retain.
Chris Lattner5f9e2722011-07-23 10:55:15 +00002740 StringRef Kind = cast<ObjCBridgedCastExpr>(E)->getBridgeKindName();
John McCallf85e1932011-06-15 23:02:42 +00002741 Out << "v1U" << Kind.size() << Kind;
2742 }
2743 // Fall through to mangle the cast itself.
2744
Douglas Gregor46287c72010-01-29 16:37:09 +00002745 case Expr::CStyleCastExprClass:
2746 case Expr::CXXStaticCastExprClass:
2747 case Expr::CXXDynamicCastExprClass:
2748 case Expr::CXXReinterpretCastExprClass:
2749 case Expr::CXXConstCastExprClass:
2750 case Expr::CXXFunctionalCastExprClass: {
2751 const ExplicitCastExpr *ECE = cast<ExplicitCastExpr>(E);
2752 Out << "cv";
2753 mangleType(ECE->getType());
2754 mangleExpression(ECE->getSubExpr());
2755 break;
2756 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002757
Anders Carlsson58040a52009-12-16 05:48:46 +00002758 case Expr::CXXOperatorCallExprClass: {
2759 const CXXOperatorCallExpr *CE = cast<CXXOperatorCallExpr>(E);
2760 unsigned NumArgs = CE->getNumArgs();
2761 mangleOperatorName(CE->getOperator(), /*Arity=*/NumArgs);
2762 // Mangle the arguments.
2763 for (unsigned i = 0; i != NumArgs; ++i)
2764 mangleExpression(CE->getArg(i));
2765 break;
2766 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002767
Anders Carlssona7694082009-11-06 02:50:19 +00002768 case Expr::ParenExprClass:
John McCall5e1e89b2010-08-18 19:18:59 +00002769 mangleExpression(cast<ParenExpr>(E)->getSubExpr(), Arity);
Anders Carlssona7694082009-11-06 02:50:19 +00002770 break;
2771
Anders Carlssond553f8c2009-09-21 01:21:10 +00002772 case Expr::DeclRefExprClass: {
Douglas Gregor5ed1bc32010-02-28 21:40:32 +00002773 const NamedDecl *D = cast<DeclRefExpr>(E)->getDecl();
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002774
Anders Carlssond553f8c2009-09-21 01:21:10 +00002775 switch (D->getKind()) {
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002776 default:
Douglas Gregor5ed1bc32010-02-28 21:40:32 +00002777 // <expr-primary> ::= L <mangled-name> E # external name
2778 Out << 'L';
2779 mangle(D, "_Z");
2780 Out << 'E';
2781 break;
2782
John McCallfb44de92011-05-01 22:35:37 +00002783 case Decl::ParmVar:
2784 mangleFunctionParam(cast<ParmVarDecl>(D));
2785 break;
2786
John McCall3dc7e7b2010-07-24 01:17:35 +00002787 case Decl::EnumConstant: {
2788 const EnumConstantDecl *ED = cast<EnumConstantDecl>(D);
2789 mangleIntegerLiteral(ED->getType(), ED->getInitVal());
2790 break;
2791 }
2792
Anders Carlssond553f8c2009-09-21 01:21:10 +00002793 case Decl::NonTypeTemplateParm: {
2794 const NonTypeTemplateParmDecl *PD = cast<NonTypeTemplateParmDecl>(D);
Anders Carlsson0ccdf8d2009-09-27 00:38:53 +00002795 mangleTemplateParameter(PD->getIndex());
Anders Carlssond553f8c2009-09-21 01:21:10 +00002796 break;
2797 }
2798
2799 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002800
Anders Carlsson50755b02009-09-27 20:11:34 +00002801 break;
Anders Carlssond553f8c2009-09-21 01:21:10 +00002802 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002803
Douglas Gregorc7793c72011-01-15 01:15:58 +00002804 case Expr::SubstNonTypeTemplateParmPackExprClass:
John McCall68a51a72011-07-01 00:04:39 +00002805 // FIXME: not clear how to mangle this!
2806 // template <unsigned N...> class A {
2807 // template <class U...> void foo(U (&x)[N]...);
2808 // };
2809 Out << "_SUBSTPACK_";
Douglas Gregorc7793c72011-01-15 01:15:58 +00002810 break;
Richard Smith9a4db032012-09-12 00:56:43 +00002811
2812 case Expr::FunctionParmPackExprClass: {
2813 // FIXME: not clear how to mangle this!
2814 const FunctionParmPackExpr *FPPE = cast<FunctionParmPackExpr>(E);
2815 Out << "v110_SUBSTPACK";
2816 mangleFunctionParam(FPPE->getParameterPack());
2817 break;
2818 }
2819
John McCall865d4472009-11-19 22:55:06 +00002820 case Expr::DependentScopeDeclRefExprClass: {
2821 const DependentScopeDeclRefExpr *DRE = cast<DependentScopeDeclRefExpr>(E);
John McCall26a6ec72011-06-21 22:12:46 +00002822 mangleUnresolvedName(DRE->getQualifier(), 0, DRE->getDeclName(), Arity);
Douglas Gregor4b2ccfc2010-02-28 22:05:49 +00002823
John McCall26a6ec72011-06-21 22:12:46 +00002824 // All the <unresolved-name> productions end in a
2825 // base-unresolved-name, where <template-args> are just tacked
2826 // onto the end.
John McCall6dbce192010-08-20 00:17:19 +00002827 if (DRE->hasExplicitTemplateArgs())
2828 mangleTemplateArgs(DRE->getExplicitTemplateArgs());
Anders Carlsson50755b02009-09-27 20:11:34 +00002829 break;
2830 }
2831
John McCalld9307602010-04-09 22:54:09 +00002832 case Expr::CXXBindTemporaryExprClass:
2833 mangleExpression(cast<CXXBindTemporaryExpr>(E)->getSubExpr());
2834 break;
2835
John McCall4765fa02010-12-06 08:20:24 +00002836 case Expr::ExprWithCleanupsClass:
2837 mangleExpression(cast<ExprWithCleanups>(E)->getSubExpr(), Arity);
John McCalld9307602010-04-09 22:54:09 +00002838 break;
2839
John McCall1dd73832010-02-04 01:42:13 +00002840 case Expr::FloatingLiteralClass: {
2841 const FloatingLiteral *FL = cast<FloatingLiteral>(E);
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002842 Out << 'L';
John McCall1dd73832010-02-04 01:42:13 +00002843 mangleType(FL->getType());
John McCall0512e482010-07-14 04:20:34 +00002844 mangleFloat(FL->getValue());
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002845 Out << 'E';
John McCall1dd73832010-02-04 01:42:13 +00002846 break;
2847 }
2848
John McCallde810632010-04-09 21:48:08 +00002849 case Expr::CharacterLiteralClass:
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002850 Out << 'L';
John McCallde810632010-04-09 21:48:08 +00002851 mangleType(E->getType());
2852 Out << cast<CharacterLiteral>(E)->getValue();
2853 Out << 'E';
2854 break;
2855
Ted Kremenekebcb57a2012-03-06 20:05:56 +00002856 // FIXME. __objc_yes/__objc_no are mangled same as true/false
2857 case Expr::ObjCBoolLiteralExprClass:
2858 Out << "Lb";
2859 Out << (cast<ObjCBoolLiteralExpr>(E)->getValue() ? '1' : '0');
2860 Out << 'E';
2861 break;
2862
John McCallde810632010-04-09 21:48:08 +00002863 case Expr::CXXBoolLiteralExprClass:
2864 Out << "Lb";
2865 Out << (cast<CXXBoolLiteralExpr>(E)->getValue() ? '1' : '0');
2866 Out << 'E';
2867 break;
2868
John McCall0512e482010-07-14 04:20:34 +00002869 case Expr::IntegerLiteralClass: {
2870 llvm::APSInt Value(cast<IntegerLiteral>(E)->getValue());
2871 if (E->getType()->isSignedIntegerType())
2872 Value.setIsSigned(true);
2873 mangleIntegerLiteral(E->getType(), Value);
Anders Carlssone170ba72009-12-14 01:45:37 +00002874 break;
John McCall0512e482010-07-14 04:20:34 +00002875 }
2876
2877 case Expr::ImaginaryLiteralClass: {
2878 const ImaginaryLiteral *IE = cast<ImaginaryLiteral>(E);
2879 // Mangle as if a complex literal.
Nick Lewycky271b6652010-09-05 03:40:33 +00002880 // Proposal from David Vandevoorde, 2010.06.30.
John McCall0512e482010-07-14 04:20:34 +00002881 Out << 'L';
2882 mangleType(E->getType());
2883 if (const FloatingLiteral *Imag =
2884 dyn_cast<FloatingLiteral>(IE->getSubExpr())) {
2885 // Mangle a floating-point zero of the appropriate type.
2886 mangleFloat(llvm::APFloat(Imag->getValue().getSemantics()));
2887 Out << '_';
2888 mangleFloat(Imag->getValue());
2889 } else {
Nick Lewycky271b6652010-09-05 03:40:33 +00002890 Out << "0_";
John McCall0512e482010-07-14 04:20:34 +00002891 llvm::APSInt Value(cast<IntegerLiteral>(IE->getSubExpr())->getValue());
2892 if (IE->getSubExpr()->getType()->isSignedIntegerType())
2893 Value.setIsSigned(true);
2894 mangleNumber(Value);
2895 }
2896 Out << 'E';
2897 break;
2898 }
2899
2900 case Expr::StringLiteralClass: {
John McCall1658c392010-07-15 21:53:03 +00002901 // Revised proposal from David Vandervoorde, 2010.07.15.
John McCall0512e482010-07-14 04:20:34 +00002902 Out << 'L';
John McCall1658c392010-07-15 21:53:03 +00002903 assert(isa<ConstantArrayType>(E->getType()));
2904 mangleType(E->getType());
John McCall0512e482010-07-14 04:20:34 +00002905 Out << 'E';
2906 break;
2907 }
2908
2909 case Expr::GNUNullExprClass:
2910 // FIXME: should this really be mangled the same as nullptr?
2911 // fallthrough
2912
2913 case Expr::CXXNullPtrLiteralExprClass: {
2914 // Proposal from David Vandervoorde, 2010.06.30, as
2915 // modified by ABI list discussion.
2916 Out << "LDnE";
2917 break;
2918 }
Douglas Gregorbe230c32011-01-03 17:17:50 +00002919
2920 case Expr::PackExpansionExprClass:
2921 Out << "sp";
2922 mangleExpression(cast<PackExpansionExpr>(E)->getPattern());
2923 break;
Douglas Gregor2e774c42011-01-04 18:56:13 +00002924
2925 case Expr::SizeOfPackExprClass: {
Douglas Gregor2e774c42011-01-04 18:56:13 +00002926 Out << "sZ";
2927 const NamedDecl *Pack = cast<SizeOfPackExpr>(E)->getPack();
2928 if (const TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(Pack))
2929 mangleTemplateParameter(TTP->getIndex());
2930 else if (const NonTypeTemplateParmDecl *NTTP
2931 = dyn_cast<NonTypeTemplateParmDecl>(Pack))
2932 mangleTemplateParameter(NTTP->getIndex());
2933 else if (const TemplateTemplateParmDecl *TempTP
2934 = dyn_cast<TemplateTemplateParmDecl>(Pack))
2935 mangleTemplateParameter(TempTP->getIndex());
Douglas Gregor91832362011-07-12 07:03:48 +00002936 else
2937 mangleFunctionParam(cast<ParmVarDecl>(Pack));
Douglas Gregordfbbcf92011-03-03 02:20:19 +00002938 break;
Douglas Gregor2e774c42011-01-04 18:56:13 +00002939 }
Douglas Gregor03e80032011-06-21 17:03:29 +00002940
2941 case Expr::MaterializeTemporaryExprClass: {
2942 mangleExpression(cast<MaterializeTemporaryExpr>(E)->GetTemporaryExpr());
2943 break;
2944 }
Douglas Gregorcefc3af2012-04-16 07:05:22 +00002945
2946 case Expr::CXXThisExprClass:
2947 Out << "fpT";
2948 break;
Anders Carlssond553f8c2009-09-21 01:21:10 +00002949 }
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00002950}
2951
John McCallfb44de92011-05-01 22:35:37 +00002952/// Mangle an expression which refers to a parameter variable.
2953///
2954/// <expression> ::= <function-param>
2955/// <function-param> ::= fp <top-level CV-qualifiers> _ # L == 0, I == 0
2956/// <function-param> ::= fp <top-level CV-qualifiers>
2957/// <parameter-2 non-negative number> _ # L == 0, I > 0
2958/// <function-param> ::= fL <L-1 non-negative number>
2959/// p <top-level CV-qualifiers> _ # L > 0, I == 0
2960/// <function-param> ::= fL <L-1 non-negative number>
2961/// p <top-level CV-qualifiers>
2962/// <I-1 non-negative number> _ # L > 0, I > 0
2963///
2964/// L is the nesting depth of the parameter, defined as 1 if the
2965/// parameter comes from the innermost function prototype scope
2966/// enclosing the current context, 2 if from the next enclosing
2967/// function prototype scope, and so on, with one special case: if
2968/// we've processed the full parameter clause for the innermost
2969/// function type, then L is one less. This definition conveniently
2970/// makes it irrelevant whether a function's result type was written
2971/// trailing or leading, but is otherwise overly complicated; the
2972/// numbering was first designed without considering references to
2973/// parameter in locations other than return types, and then the
2974/// mangling had to be generalized without changing the existing
2975/// manglings.
2976///
2977/// I is the zero-based index of the parameter within its parameter
2978/// declaration clause. Note that the original ABI document describes
2979/// this using 1-based ordinals.
2980void CXXNameMangler::mangleFunctionParam(const ParmVarDecl *parm) {
2981 unsigned parmDepth = parm->getFunctionScopeDepth();
2982 unsigned parmIndex = parm->getFunctionScopeIndex();
2983
2984 // Compute 'L'.
2985 // parmDepth does not include the declaring function prototype.
2986 // FunctionTypeDepth does account for that.
2987 assert(parmDepth < FunctionTypeDepth.getDepth());
2988 unsigned nestingDepth = FunctionTypeDepth.getDepth() - parmDepth;
2989 if (FunctionTypeDepth.isInResultType())
2990 nestingDepth--;
2991
2992 if (nestingDepth == 0) {
2993 Out << "fp";
2994 } else {
2995 Out << "fL" << (nestingDepth - 1) << 'p';
2996 }
2997
2998 // Top-level qualifiers. We don't have to worry about arrays here,
2999 // because parameters declared as arrays should already have been
Benjamin Kramer48d798c2012-06-02 10:20:41 +00003000 // transformed to have pointer type. FIXME: apparently these don't
John McCallfb44de92011-05-01 22:35:37 +00003001 // get mangled if used as an rvalue of a known non-class type?
3002 assert(!parm->getType()->isArrayType()
3003 && "parameter's type is still an array type?");
3004 mangleQualifiers(parm->getType().getQualifiers());
3005
3006 // Parameter index.
3007 if (parmIndex != 0) {
3008 Out << (parmIndex - 1);
3009 }
3010 Out << '_';
3011}
3012
Anders Carlsson3ac86b52009-04-15 05:36:58 +00003013void CXXNameMangler::mangleCXXCtorType(CXXCtorType T) {
3014 // <ctor-dtor-name> ::= C1 # complete object constructor
3015 // ::= C2 # base object constructor
3016 // ::= C3 # complete object allocating constructor
3017 //
3018 switch (T) {
3019 case Ctor_Complete:
3020 Out << "C1";
3021 break;
3022 case Ctor_Base:
3023 Out << "C2";
3024 break;
3025 case Ctor_CompleteAllocating:
3026 Out << "C3";
3027 break;
3028 }
3029}
3030
Anders Carlsson27ae5362009-04-17 01:58:57 +00003031void CXXNameMangler::mangleCXXDtorType(CXXDtorType T) {
3032 // <ctor-dtor-name> ::= D0 # deleting destructor
3033 // ::= D1 # complete object destructor
3034 // ::= D2 # base object destructor
3035 //
3036 switch (T) {
3037 case Dtor_Deleting:
3038 Out << "D0";
3039 break;
3040 case Dtor_Complete:
3041 Out << "D1";
3042 break;
3043 case Dtor_Base:
3044 Out << "D2";
3045 break;
3046 }
3047}
3048
John McCall6dbce192010-08-20 00:17:19 +00003049void CXXNameMangler::mangleTemplateArgs(
Argyrios Kyrtzidisb0c3e092011-09-22 20:07:03 +00003050 const ASTTemplateArgumentListInfo &TemplateArgs) {
John McCall6dbce192010-08-20 00:17:19 +00003051 // <template-args> ::= I <template-arg>+ E
3052 Out << 'I';
John McCall4f4e4132011-05-04 01:45:19 +00003053 for (unsigned i = 0, e = TemplateArgs.NumTemplateArgs; i != e; ++i)
Eli Friedmand7a6b162012-09-26 02:36:12 +00003054 mangleTemplateArg(TemplateArgs.getTemplateArgs()[i].getArgument());
John McCall6dbce192010-08-20 00:17:19 +00003055 Out << 'E';
3056}
3057
Eli Friedmand7a6b162012-09-26 02:36:12 +00003058void CXXNameMangler::mangleTemplateArgs(const TemplateArgumentList &AL) {
Anders Carlsson7a0ba872009-05-15 16:09:15 +00003059 // <template-args> ::= I <template-arg>+ E
Benjamin Kramer35f59b62010-04-10 16:03:31 +00003060 Out << 'I';
Rafael Espindolad9800722010-03-11 14:07:00 +00003061 for (unsigned i = 0, e = AL.size(); i != e; ++i)
Eli Friedmand7a6b162012-09-26 02:36:12 +00003062 mangleTemplateArg(AL[i]);
Benjamin Kramer35f59b62010-04-10 16:03:31 +00003063 Out << 'E';
Anders Carlsson7a0ba872009-05-15 16:09:15 +00003064}
3065
Eli Friedmand7a6b162012-09-26 02:36:12 +00003066void CXXNameMangler::mangleTemplateArgs(const TemplateArgument *TemplateArgs,
Anders Carlsson7624f212009-09-18 02:42:01 +00003067 unsigned NumTemplateArgs) {
3068 // <template-args> ::= I <template-arg>+ E
Benjamin Kramer35f59b62010-04-10 16:03:31 +00003069 Out << 'I';
Daniel Dunbar7e0c1952009-11-21 09:17:15 +00003070 for (unsigned i = 0; i != NumTemplateArgs; ++i)
Eli Friedmand7a6b162012-09-26 02:36:12 +00003071 mangleTemplateArg(TemplateArgs[i]);
Benjamin Kramer35f59b62010-04-10 16:03:31 +00003072 Out << 'E';
Anders Carlsson7624f212009-09-18 02:42:01 +00003073}
3074
Eli Friedmand7a6b162012-09-26 02:36:12 +00003075void CXXNameMangler::mangleTemplateArg(TemplateArgument A) {
Mike Stump1eb44332009-09-09 15:08:12 +00003076 // <template-arg> ::= <type> # type or template
Anders Carlsson7a0ba872009-05-15 16:09:15 +00003077 // ::= X <expression> E # expression
3078 // ::= <expr-primary> # simple expressions
Douglas Gregor4fc48662011-01-13 16:39:34 +00003079 // ::= J <template-arg>* E # argument pack
Douglas Gregorf1588662011-07-12 15:18:55 +00003080 // ::= sp <expression> # pack expansion of (C++0x)
3081 if (!A.isInstantiationDependent() || A.isDependent())
3082 A = Context.getASTContext().getCanonicalTemplateArgument(A);
3083
Anders Carlsson7a0ba872009-05-15 16:09:15 +00003084 switch (A.getKind()) {
Douglas Gregorf90b27a2011-01-03 22:36:02 +00003085 case TemplateArgument::Null:
3086 llvm_unreachable("Cannot mangle NULL template argument");
3087
Anders Carlsson7a0ba872009-05-15 16:09:15 +00003088 case TemplateArgument::Type:
3089 mangleType(A.getAsType());
3090 break;
Anders Carlsson9e85c742009-12-23 19:30:55 +00003091 case TemplateArgument::Template:
John McCallb6f532e2010-07-14 06:43:17 +00003092 // This is mangled as <type>.
3093 mangleType(A.getAsTemplate());
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00003094 break;
Douglas Gregora7fc9012011-01-05 18:58:31 +00003095 case TemplateArgument::TemplateExpansion:
Douglas Gregor4fc48662011-01-13 16:39:34 +00003096 // <type> ::= Dp <type> # pack expansion (C++0x)
Douglas Gregora7fc9012011-01-05 18:58:31 +00003097 Out << "Dp";
3098 mangleType(A.getAsTemplateOrTemplatePattern());
3099 break;
John McCall092beef2012-01-06 05:06:35 +00003100 case TemplateArgument::Expression: {
3101 // It's possible to end up with a DeclRefExpr here in certain
3102 // dependent cases, in which case we should mangle as a
3103 // declaration.
3104 const Expr *E = A.getAsExpr()->IgnoreParens();
3105 if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E)) {
3106 const ValueDecl *D = DRE->getDecl();
3107 if (isa<VarDecl>(D) || isa<FunctionDecl>(D)) {
3108 Out << "L";
3109 mangle(D, "_Z");
3110 Out << 'E';
3111 break;
3112 }
3113 }
3114
Anders Carlssond553f8c2009-09-21 01:21:10 +00003115 Out << 'X';
John McCall092beef2012-01-06 05:06:35 +00003116 mangleExpression(E);
Anders Carlssond553f8c2009-09-21 01:21:10 +00003117 Out << 'E';
3118 break;
John McCall092beef2012-01-06 05:06:35 +00003119 }
Anders Carlssone170ba72009-12-14 01:45:37 +00003120 case TemplateArgument::Integral:
Benjamin Kramer85524372012-06-07 15:09:51 +00003121 mangleIntegerLiteral(A.getIntegralType(), A.getAsIntegral());
Anders Carlsson7a0ba872009-05-15 16:09:15 +00003122 break;
Daniel Dunbar7e0c1952009-11-21 09:17:15 +00003123 case TemplateArgument::Declaration: {
3124 // <expr-primary> ::= L <mangled-name> E # external name
Rafael Espindolad9800722010-03-11 14:07:00 +00003125 // Clang produces AST's where pointer-to-member-function expressions
Daniel Dunbar7e0c1952009-11-21 09:17:15 +00003126 // and pointer-to-function expressions are represented as a declaration not
Rafael Espindolad9800722010-03-11 14:07:00 +00003127 // an expression. We compensate for it here to produce the correct mangling.
Eli Friedmand7a6b162012-09-26 02:36:12 +00003128 ValueDecl *D = A.getAsDecl();
3129 bool compensateMangling = !A.isDeclForReferenceParam();
Rafael Espindolad9800722010-03-11 14:07:00 +00003130 if (compensateMangling) {
3131 Out << 'X';
3132 mangleOperatorName(OO_Amp, 1);
3133 }
3134
Daniel Dunbar7e0c1952009-11-21 09:17:15 +00003135 Out << 'L';
3136 // References to external entities use the mangled name; if the name would
3137 // not normally be manged then mangle it as unqualified.
3138 //
3139 // FIXME: The ABI specifies that external names here should have _Z, but
3140 // gcc leaves this off.
Rafael Espindolad9800722010-03-11 14:07:00 +00003141 if (compensateMangling)
3142 mangle(D, "_Z");
3143 else
3144 mangle(D, "Z");
Daniel Dunbar7e0c1952009-11-21 09:17:15 +00003145 Out << 'E';
Rafael Espindolad9800722010-03-11 14:07:00 +00003146
3147 if (compensateMangling)
3148 Out << 'E';
3149
Daniel Dunbar7e0c1952009-11-21 09:17:15 +00003150 break;
3151 }
Eli Friedmand7a6b162012-09-26 02:36:12 +00003152 case TemplateArgument::NullPtr: {
3153 // <expr-primary> ::= L <type> 0 E
3154 Out << 'L';
3155 mangleType(A.getNullPtrType());
3156 Out << "0E";
3157 break;
3158 }
Douglas Gregorf90b27a2011-01-03 22:36:02 +00003159 case TemplateArgument::Pack: {
3160 // Note: proposal by Mike Herrick on 12/20/10
3161 Out << 'J';
3162 for (TemplateArgument::pack_iterator PA = A.pack_begin(),
3163 PAEnd = A.pack_end();
3164 PA != PAEnd; ++PA)
Eli Friedmand7a6b162012-09-26 02:36:12 +00003165 mangleTemplateArg(*PA);
Douglas Gregorf90b27a2011-01-03 22:36:02 +00003166 Out << 'E';
3167 }
Daniel Dunbar7e0c1952009-11-21 09:17:15 +00003168 }
Anders Carlsson7a0ba872009-05-15 16:09:15 +00003169}
3170
Anders Carlsson0ccdf8d2009-09-27 00:38:53 +00003171void CXXNameMangler::mangleTemplateParameter(unsigned Index) {
3172 // <template-param> ::= T_ # first template parameter
3173 // ::= T <parameter-2 non-negative number> _
3174 if (Index == 0)
3175 Out << "T_";
3176 else
3177 Out << 'T' << (Index - 1) << '_';
3178}
3179
John McCall68a51a72011-07-01 00:04:39 +00003180void CXXNameMangler::mangleExistingSubstitution(QualType type) {
3181 bool result = mangleSubstitution(type);
3182 assert(result && "no existing substitution for type");
3183 (void) result;
3184}
3185
3186void CXXNameMangler::mangleExistingSubstitution(TemplateName tname) {
3187 bool result = mangleSubstitution(tname);
3188 assert(result && "no existing substitution for template name");
3189 (void) result;
3190}
3191
Anders Carlsson76967372009-09-17 00:43:46 +00003192// <substitution> ::= S <seq-id> _
3193// ::= S_
Anders Carlsson6862fc72009-09-17 04:16:28 +00003194bool CXXNameMangler::mangleSubstitution(const NamedDecl *ND) {
Anders Carlssone7c8cb62009-09-26 20:53:44 +00003195 // Try one of the standard substitutions first.
3196 if (mangleStandardSubstitution(ND))
3197 return true;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00003198
Anders Carlsson433d1372009-11-07 04:26:04 +00003199 ND = cast<NamedDecl>(ND->getCanonicalDecl());
Anders Carlsson6862fc72009-09-17 04:16:28 +00003200 return mangleSubstitution(reinterpret_cast<uintptr_t>(ND));
3201}
3202
Douglas Gregor14795c82011-12-03 18:24:43 +00003203/// \brief Determine whether the given type has any qualifiers that are
3204/// relevant for substitutions.
3205static bool hasMangledSubstitutionQualifiers(QualType T) {
3206 Qualifiers Qs = T.getQualifiers();
3207 return Qs.getCVRQualifiers() || Qs.hasAddressSpace();
3208}
3209
Anders Carlsson76967372009-09-17 00:43:46 +00003210bool CXXNameMangler::mangleSubstitution(QualType T) {
Douglas Gregor14795c82011-12-03 18:24:43 +00003211 if (!hasMangledSubstitutionQualifiers(T)) {
Anders Carlssond99edc42009-09-26 03:55:37 +00003212 if (const RecordType *RT = T->getAs<RecordType>())
3213 return mangleSubstitution(RT->getDecl());
3214 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00003215
Anders Carlsson76967372009-09-17 00:43:46 +00003216 uintptr_t TypePtr = reinterpret_cast<uintptr_t>(T.getAsOpaquePtr());
3217
Anders Carlssond3a932a2009-09-17 03:53:28 +00003218 return mangleSubstitution(TypePtr);
3219}
3220
Douglas Gregor1e9268e2010-04-28 05:58:56 +00003221bool CXXNameMangler::mangleSubstitution(TemplateName Template) {
3222 if (TemplateDecl *TD = Template.getAsTemplateDecl())
3223 return mangleSubstitution(TD);
Sean Huntc3021132010-05-05 15:23:54 +00003224
Douglas Gregor1e9268e2010-04-28 05:58:56 +00003225 Template = Context.getASTContext().getCanonicalTemplateName(Template);
3226 return mangleSubstitution(
3227 reinterpret_cast<uintptr_t>(Template.getAsVoidPointer()));
3228}
3229
Anders Carlssond3a932a2009-09-17 03:53:28 +00003230bool CXXNameMangler::mangleSubstitution(uintptr_t Ptr) {
Benjamin Kramer35f59b62010-04-10 16:03:31 +00003231 llvm::DenseMap<uintptr_t, unsigned>::iterator I = Substitutions.find(Ptr);
Anders Carlsson76967372009-09-17 00:43:46 +00003232 if (I == Substitutions.end())
3233 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00003234
Anders Carlsson76967372009-09-17 00:43:46 +00003235 unsigned SeqID = I->second;
3236 if (SeqID == 0)
3237 Out << "S_";
3238 else {
3239 SeqID--;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00003240
Anders Carlsson76967372009-09-17 00:43:46 +00003241 // <seq-id> is encoded in base-36, using digits and upper case letters.
3242 char Buffer[10];
Benjamin Kramer35f59b62010-04-10 16:03:31 +00003243 char *BufferPtr = llvm::array_endof(Buffer);
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00003244
Anders Carlsson76967372009-09-17 00:43:46 +00003245 if (SeqID == 0) *--BufferPtr = '0';
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00003246
Anders Carlsson76967372009-09-17 00:43:46 +00003247 while (SeqID) {
3248 assert(BufferPtr > Buffer && "Buffer overflow!");
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00003249
John McCall6ab30e02010-06-09 07:26:17 +00003250 char c = static_cast<char>(SeqID % 36);
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00003251
Anders Carlsson76967372009-09-17 00:43:46 +00003252 *--BufferPtr = (c < 10 ? '0' + c : 'A' + c - 10);
3253 SeqID /= 36;
3254 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00003255
Benjamin Kramer35f59b62010-04-10 16:03:31 +00003256 Out << 'S'
Chris Lattner5f9e2722011-07-23 10:55:15 +00003257 << StringRef(BufferPtr, llvm::array_endof(Buffer)-BufferPtr)
Benjamin Kramer35f59b62010-04-10 16:03:31 +00003258 << '_';
Anders Carlsson76967372009-09-17 00:43:46 +00003259 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00003260
Anders Carlsson76967372009-09-17 00:43:46 +00003261 return true;
3262}
3263
Anders Carlssonf514b542009-09-27 00:12:57 +00003264static bool isCharType(QualType T) {
3265 if (T.isNull())
3266 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00003267
Anders Carlssonf514b542009-09-27 00:12:57 +00003268 return T->isSpecificBuiltinType(BuiltinType::Char_S) ||
3269 T->isSpecificBuiltinType(BuiltinType::Char_U);
3270}
3271
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00003272/// isCharSpecialization - Returns whether a given type is a template
Anders Carlssonf514b542009-09-27 00:12:57 +00003273/// specialization of a given name with a single argument of type char.
3274static bool isCharSpecialization(QualType T, const char *Name) {
3275 if (T.isNull())
3276 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00003277
Anders Carlssonf514b542009-09-27 00:12:57 +00003278 const RecordType *RT = T->getAs<RecordType>();
3279 if (!RT)
3280 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00003281
3282 const ClassTemplateSpecializationDecl *SD =
Anders Carlssonf514b542009-09-27 00:12:57 +00003283 dyn_cast<ClassTemplateSpecializationDecl>(RT->getDecl());
3284 if (!SD)
3285 return false;
3286
Douglas Gregorccc1b5e2012-02-21 00:37:24 +00003287 if (!isStdNamespace(getEffectiveDeclContext(SD)))
Anders Carlssonf514b542009-09-27 00:12:57 +00003288 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00003289
Anders Carlssonf514b542009-09-27 00:12:57 +00003290 const TemplateArgumentList &TemplateArgs = SD->getTemplateArgs();
3291 if (TemplateArgs.size() != 1)
3292 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00003293
Anders Carlssonf514b542009-09-27 00:12:57 +00003294 if (!isCharType(TemplateArgs[0].getAsType()))
3295 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00003296
Daniel Dunbar01eb9b92009-10-18 21:17:35 +00003297 return SD->getIdentifier()->getName() == Name;
Anders Carlssonf514b542009-09-27 00:12:57 +00003298}
3299
Anders Carlsson91f88602009-12-07 19:56:42 +00003300template <std::size_t StrLen>
Benjamin Kramer54353f42010-11-25 18:29:30 +00003301static bool isStreamCharSpecialization(const ClassTemplateSpecializationDecl*SD,
3302 const char (&Str)[StrLen]) {
Anders Carlsson91f88602009-12-07 19:56:42 +00003303 if (!SD->getIdentifier()->isStr(Str))
3304 return false;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00003305
Anders Carlsson91f88602009-12-07 19:56:42 +00003306 const TemplateArgumentList &TemplateArgs = SD->getTemplateArgs();
3307 if (TemplateArgs.size() != 2)
3308 return false;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00003309
Anders Carlsson91f88602009-12-07 19:56:42 +00003310 if (!isCharType(TemplateArgs[0].getAsType()))
3311 return false;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00003312
Anders Carlsson91f88602009-12-07 19:56:42 +00003313 if (!isCharSpecialization(TemplateArgs[1].getAsType(), "char_traits"))
3314 return false;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00003315
Anders Carlsson91f88602009-12-07 19:56:42 +00003316 return true;
3317}
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00003318
Anders Carlssone7c8cb62009-09-26 20:53:44 +00003319bool CXXNameMangler::mangleStandardSubstitution(const NamedDecl *ND) {
3320 // <substitution> ::= St # ::std::
Anders Carlsson8c031552009-09-26 23:10:05 +00003321 if (const NamespaceDecl *NS = dyn_cast<NamespaceDecl>(ND)) {
Anders Carlsson47846d22009-12-04 06:23:23 +00003322 if (isStd(NS)) {
Anders Carlsson8c031552009-09-26 23:10:05 +00003323 Out << "St";
3324 return true;
3325 }
3326 }
3327
3328 if (const ClassTemplateDecl *TD = dyn_cast<ClassTemplateDecl>(ND)) {
Douglas Gregorccc1b5e2012-02-21 00:37:24 +00003329 if (!isStdNamespace(getEffectiveDeclContext(TD)))
Anders Carlsson8c031552009-09-26 23:10:05 +00003330 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00003331
Anders Carlsson8c031552009-09-26 23:10:05 +00003332 // <substitution> ::= Sa # ::std::allocator
3333 if (TD->getIdentifier()->isStr("allocator")) {
3334 Out << "Sa";
3335 return true;
3336 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00003337
Anders Carlsson189d59c2009-09-26 23:14:39 +00003338 // <<substitution> ::= Sb # ::std::basic_string
3339 if (TD->getIdentifier()->isStr("basic_string")) {
3340 Out << "Sb";
3341 return true;
3342 }
Anders Carlsson8c031552009-09-26 23:10:05 +00003343 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00003344
3345 if (const ClassTemplateSpecializationDecl *SD =
Anders Carlssonf514b542009-09-27 00:12:57 +00003346 dyn_cast<ClassTemplateSpecializationDecl>(ND)) {
Douglas Gregorccc1b5e2012-02-21 00:37:24 +00003347 if (!isStdNamespace(getEffectiveDeclContext(SD)))
Eli Friedman5370ee22010-02-23 18:25:09 +00003348 return false;
3349
Anders Carlssonf514b542009-09-27 00:12:57 +00003350 // <substitution> ::= Ss # ::std::basic_string<char,
3351 // ::std::char_traits<char>,
3352 // ::std::allocator<char> >
3353 if (SD->getIdentifier()->isStr("basic_string")) {
3354 const TemplateArgumentList &TemplateArgs = SD->getTemplateArgs();
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00003355
Anders Carlssonf514b542009-09-27 00:12:57 +00003356 if (TemplateArgs.size() != 3)
3357 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00003358
Anders Carlssonf514b542009-09-27 00:12:57 +00003359 if (!isCharType(TemplateArgs[0].getAsType()))
3360 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00003361
Anders Carlssonf514b542009-09-27 00:12:57 +00003362 if (!isCharSpecialization(TemplateArgs[1].getAsType(), "char_traits"))
3363 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00003364
Anders Carlssonf514b542009-09-27 00:12:57 +00003365 if (!isCharSpecialization(TemplateArgs[2].getAsType(), "allocator"))
3366 return false;
3367
3368 Out << "Ss";
3369 return true;
3370 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00003371
Anders Carlsson91f88602009-12-07 19:56:42 +00003372 // <substitution> ::= Si # ::std::basic_istream<char,
3373 // ::std::char_traits<char> >
3374 if (isStreamCharSpecialization(SD, "basic_istream")) {
3375 Out << "Si";
3376 return true;
3377 }
3378
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00003379 // <substitution> ::= So # ::std::basic_ostream<char,
Anders Carlsson8f8fd8e2009-10-08 17:20:26 +00003380 // ::std::char_traits<char> >
Anders Carlsson91f88602009-12-07 19:56:42 +00003381 if (isStreamCharSpecialization(SD, "basic_ostream")) {
Anders Carlsson8f8fd8e2009-10-08 17:20:26 +00003382 Out << "So";
3383 return true;
3384 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00003385
Anders Carlsson91f88602009-12-07 19:56:42 +00003386 // <substitution> ::= Sd # ::std::basic_iostream<char,
3387 // ::std::char_traits<char> >
3388 if (isStreamCharSpecialization(SD, "basic_iostream")) {
3389 Out << "Sd";
3390 return true;
3391 }
Anders Carlssonf514b542009-09-27 00:12:57 +00003392 }
Anders Carlsson8c031552009-09-26 23:10:05 +00003393 return false;
Anders Carlssone7c8cb62009-09-26 20:53:44 +00003394}
3395
Anders Carlsson76967372009-09-17 00:43:46 +00003396void CXXNameMangler::addSubstitution(QualType T) {
Douglas Gregor14795c82011-12-03 18:24:43 +00003397 if (!hasMangledSubstitutionQualifiers(T)) {
Anders Carlssond99edc42009-09-26 03:55:37 +00003398 if (const RecordType *RT = T->getAs<RecordType>()) {
3399 addSubstitution(RT->getDecl());
3400 return;
3401 }
3402 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00003403
Anders Carlsson76967372009-09-17 00:43:46 +00003404 uintptr_t TypePtr = reinterpret_cast<uintptr_t>(T.getAsOpaquePtr());
Anders Carlssond3a932a2009-09-17 03:53:28 +00003405 addSubstitution(TypePtr);
3406}
3407
Douglas Gregor1e9268e2010-04-28 05:58:56 +00003408void CXXNameMangler::addSubstitution(TemplateName Template) {
3409 if (TemplateDecl *TD = Template.getAsTemplateDecl())
3410 return addSubstitution(TD);
Sean Huntc3021132010-05-05 15:23:54 +00003411
Douglas Gregor1e9268e2010-04-28 05:58:56 +00003412 Template = Context.getASTContext().getCanonicalTemplateName(Template);
3413 addSubstitution(reinterpret_cast<uintptr_t>(Template.getAsVoidPointer()));
3414}
3415
Anders Carlssond3a932a2009-09-17 03:53:28 +00003416void CXXNameMangler::addSubstitution(uintptr_t Ptr) {
Anders Carlssond3a932a2009-09-17 03:53:28 +00003417 assert(!Substitutions.count(Ptr) && "Substitution already exists!");
Anders Carlsson9d85b722010-06-02 04:29:50 +00003418 Substitutions[Ptr] = SeqID++;
Anders Carlsson76967372009-09-17 00:43:46 +00003419}
3420
Daniel Dunbar1b077112009-11-21 09:06:10 +00003421//
Mike Stump1eb44332009-09-09 15:08:12 +00003422
Daniel Dunbar1b077112009-11-21 09:06:10 +00003423/// \brief Mangles the name of the declaration D and emits that name to the
3424/// given output stream.
3425///
3426/// If the declaration D requires a mangled name, this routine will emit that
3427/// mangled name to \p os and return true. Otherwise, \p os will be unchanged
3428/// and this routine will return false. In this case, the caller should just
3429/// emit the identifier of the declaration (\c D->getIdentifier()) as its
3430/// name.
Peter Collingbourne14110472011-01-13 18:57:25 +00003431void ItaniumMangleContext::mangleName(const NamedDecl *D,
Chris Lattner5f9e2722011-07-23 10:55:15 +00003432 raw_ostream &Out) {
Daniel Dunbarc02ab4c2009-11-21 09:14:44 +00003433 assert((isa<FunctionDecl>(D) || isa<VarDecl>(D)) &&
3434 "Invalid mangleName() call, argument is not a variable or function!");
3435 assert(!isa<CXXConstructorDecl>(D) && !isa<CXXDestructorDecl>(D) &&
3436 "Invalid mangleName() call on 'structor decl!");
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00003437
Daniel Dunbar1b077112009-11-21 09:06:10 +00003438 PrettyStackTraceDecl CrashInfo(D, SourceLocation(),
3439 getASTContext().getSourceManager(),
3440 "Mangling declaration");
Mike Stump1eb44332009-09-09 15:08:12 +00003441
John McCallfb44de92011-05-01 22:35:37 +00003442 CXXNameMangler Mangler(*this, Out, D);
Daniel Dunbar94fd26d2009-11-21 09:06:22 +00003443 return Mangler.mangle(D);
Daniel Dunbar1b077112009-11-21 09:06:10 +00003444}
Mike Stump1eb44332009-09-09 15:08:12 +00003445
Peter Collingbourne14110472011-01-13 18:57:25 +00003446void ItaniumMangleContext::mangleCXXCtor(const CXXConstructorDecl *D,
3447 CXXCtorType Type,
Chris Lattner5f9e2722011-07-23 10:55:15 +00003448 raw_ostream &Out) {
Rafael Espindolac4850c22011-02-10 23:59:36 +00003449 CXXNameMangler Mangler(*this, Out, D, Type);
Daniel Dunbar77939c92009-11-21 09:06:31 +00003450 Mangler.mangle(D);
Daniel Dunbar1b077112009-11-21 09:06:10 +00003451}
Mike Stump1eb44332009-09-09 15:08:12 +00003452
Peter Collingbourne14110472011-01-13 18:57:25 +00003453void ItaniumMangleContext::mangleCXXDtor(const CXXDestructorDecl *D,
3454 CXXDtorType Type,
Chris Lattner5f9e2722011-07-23 10:55:15 +00003455 raw_ostream &Out) {
Rafael Espindolac4850c22011-02-10 23:59:36 +00003456 CXXNameMangler Mangler(*this, Out, D, Type);
Daniel Dunbar77939c92009-11-21 09:06:31 +00003457 Mangler.mangle(D);
Daniel Dunbar1b077112009-11-21 09:06:10 +00003458}
Mike Stumpf1216772009-07-31 18:25:34 +00003459
Peter Collingbourne14110472011-01-13 18:57:25 +00003460void ItaniumMangleContext::mangleThunk(const CXXMethodDecl *MD,
3461 const ThunkInfo &Thunk,
Chris Lattner5f9e2722011-07-23 10:55:15 +00003462 raw_ostream &Out) {
Anders Carlsson19879c92010-03-23 17:17:29 +00003463 // <special-name> ::= T <call-offset> <base encoding>
3464 // # base is the nominal target function of thunk
3465 // <special-name> ::= Tc <call-offset> <call-offset> <base encoding>
3466 // # base is the nominal target function of thunk
3467 // # first call-offset is 'this' adjustment
3468 // # second call-offset is result adjustment
Sean Huntc3021132010-05-05 15:23:54 +00003469
Anders Carlsson19879c92010-03-23 17:17:29 +00003470 assert(!isa<CXXDestructorDecl>(MD) &&
3471 "Use mangleCXXDtor for destructor decls!");
Rafael Espindolac4850c22011-02-10 23:59:36 +00003472 CXXNameMangler Mangler(*this, Out);
Anders Carlsson19879c92010-03-23 17:17:29 +00003473 Mangler.getStream() << "_ZT";
3474 if (!Thunk.Return.isEmpty())
3475 Mangler.getStream() << 'c';
Sean Huntc3021132010-05-05 15:23:54 +00003476
Anders Carlsson19879c92010-03-23 17:17:29 +00003477 // Mangle the 'this' pointer adjustment.
3478 Mangler.mangleCallOffset(Thunk.This.NonVirtual, Thunk.This.VCallOffsetOffset);
Sean Huntc3021132010-05-05 15:23:54 +00003479
Anders Carlsson19879c92010-03-23 17:17:29 +00003480 // Mangle the return pointer adjustment if there is one.
3481 if (!Thunk.Return.isEmpty())
3482 Mangler.mangleCallOffset(Thunk.Return.NonVirtual,
3483 Thunk.Return.VBaseOffsetOffset);
Sean Huntc3021132010-05-05 15:23:54 +00003484
Anders Carlsson19879c92010-03-23 17:17:29 +00003485 Mangler.mangleFunctionEncoding(MD);
3486}
3487
Sean Huntc3021132010-05-05 15:23:54 +00003488void
Peter Collingbourne14110472011-01-13 18:57:25 +00003489ItaniumMangleContext::mangleCXXDtorThunk(const CXXDestructorDecl *DD,
3490 CXXDtorType Type,
3491 const ThisAdjustment &ThisAdjustment,
Chris Lattner5f9e2722011-07-23 10:55:15 +00003492 raw_ostream &Out) {
Anders Carlsson19879c92010-03-23 17:17:29 +00003493 // <special-name> ::= T <call-offset> <base encoding>
3494 // # base is the nominal target function of thunk
Rafael Espindolac4850c22011-02-10 23:59:36 +00003495 CXXNameMangler Mangler(*this, Out, DD, Type);
Anders Carlsson19879c92010-03-23 17:17:29 +00003496 Mangler.getStream() << "_ZT";
3497
3498 // Mangle the 'this' pointer adjustment.
Sean Huntc3021132010-05-05 15:23:54 +00003499 Mangler.mangleCallOffset(ThisAdjustment.NonVirtual,
Anders Carlsson19879c92010-03-23 17:17:29 +00003500 ThisAdjustment.VCallOffsetOffset);
3501
3502 Mangler.mangleFunctionEncoding(DD);
3503}
3504
Daniel Dunbarc0747712009-11-21 09:12:13 +00003505/// mangleGuardVariable - Returns the mangled name for a guard variable
3506/// for the passed in VarDecl.
Peter Collingbourne14110472011-01-13 18:57:25 +00003507void ItaniumMangleContext::mangleItaniumGuardVariable(const VarDecl *D,
Chris Lattner5f9e2722011-07-23 10:55:15 +00003508 raw_ostream &Out) {
Daniel Dunbarc0747712009-11-21 09:12:13 +00003509 // <special-name> ::= GV <object name> # Guard variable for one-time
3510 // # initialization
Rafael Espindolac4850c22011-02-10 23:59:36 +00003511 CXXNameMangler Mangler(*this, Out);
Daniel Dunbarc0747712009-11-21 09:12:13 +00003512 Mangler.getStream() << "_ZGV";
3513 Mangler.mangleName(D);
3514}
3515
Peter Collingbourne14110472011-01-13 18:57:25 +00003516void ItaniumMangleContext::mangleReferenceTemporary(const VarDecl *D,
Chris Lattner5f9e2722011-07-23 10:55:15 +00003517 raw_ostream &Out) {
Anders Carlsson715edf22010-06-26 16:09:40 +00003518 // We match the GCC mangling here.
3519 // <special-name> ::= GR <object name>
Rafael Espindolac4850c22011-02-10 23:59:36 +00003520 CXXNameMangler Mangler(*this, Out);
Anders Carlsson715edf22010-06-26 16:09:40 +00003521 Mangler.getStream() << "_ZGR";
3522 Mangler.mangleName(D);
3523}
3524
Peter Collingbourne14110472011-01-13 18:57:25 +00003525void ItaniumMangleContext::mangleCXXVTable(const CXXRecordDecl *RD,
Chris Lattner5f9e2722011-07-23 10:55:15 +00003526 raw_ostream &Out) {
Daniel Dunbarc0747712009-11-21 09:12:13 +00003527 // <special-name> ::= TV <type> # virtual table
Rafael Espindolac4850c22011-02-10 23:59:36 +00003528 CXXNameMangler Mangler(*this, Out);
Daniel Dunbarc0747712009-11-21 09:12:13 +00003529 Mangler.getStream() << "_ZTV";
Douglas Gregor1b12a3b2010-05-26 05:11:13 +00003530 Mangler.mangleNameOrStandardSubstitution(RD);
Daniel Dunbar1b077112009-11-21 09:06:10 +00003531}
Mike Stump82d75b02009-11-10 01:58:37 +00003532
Peter Collingbourne14110472011-01-13 18:57:25 +00003533void ItaniumMangleContext::mangleCXXVTT(const CXXRecordDecl *RD,
Chris Lattner5f9e2722011-07-23 10:55:15 +00003534 raw_ostream &Out) {
Daniel Dunbarc0747712009-11-21 09:12:13 +00003535 // <special-name> ::= TT <type> # VTT structure
Rafael Espindolac4850c22011-02-10 23:59:36 +00003536 CXXNameMangler Mangler(*this, Out);
Daniel Dunbarc0747712009-11-21 09:12:13 +00003537 Mangler.getStream() << "_ZTT";
Douglas Gregor1b12a3b2010-05-26 05:11:13 +00003538 Mangler.mangleNameOrStandardSubstitution(RD);
Daniel Dunbar1b077112009-11-21 09:06:10 +00003539}
Mike Stumpab3f7e92009-11-10 01:41:59 +00003540
Peter Collingbourne14110472011-01-13 18:57:25 +00003541void ItaniumMangleContext::mangleCXXCtorVTable(const CXXRecordDecl *RD,
3542 int64_t Offset,
3543 const CXXRecordDecl *Type,
Chris Lattner5f9e2722011-07-23 10:55:15 +00003544 raw_ostream &Out) {
Daniel Dunbarc0747712009-11-21 09:12:13 +00003545 // <special-name> ::= TC <type> <offset number> _ <base type>
Rafael Espindolac4850c22011-02-10 23:59:36 +00003546 CXXNameMangler Mangler(*this, Out);
Daniel Dunbarc0747712009-11-21 09:12:13 +00003547 Mangler.getStream() << "_ZTC";
Douglas Gregor1b12a3b2010-05-26 05:11:13 +00003548 Mangler.mangleNameOrStandardSubstitution(RD);
Daniel Dunbarc0747712009-11-21 09:12:13 +00003549 Mangler.getStream() << Offset;
Benjamin Kramer35f59b62010-04-10 16:03:31 +00003550 Mangler.getStream() << '_';
Douglas Gregor1b12a3b2010-05-26 05:11:13 +00003551 Mangler.mangleNameOrStandardSubstitution(Type);
Daniel Dunbar1b077112009-11-21 09:06:10 +00003552}
Mike Stump738f8c22009-07-31 23:15:31 +00003553
Peter Collingbourne14110472011-01-13 18:57:25 +00003554void ItaniumMangleContext::mangleCXXRTTI(QualType Ty,
Chris Lattner5f9e2722011-07-23 10:55:15 +00003555 raw_ostream &Out) {
Daniel Dunbarc0747712009-11-21 09:12:13 +00003556 // <special-name> ::= TI <type> # typeinfo structure
Douglas Gregor154fe982009-12-23 22:04:40 +00003557 assert(!Ty.hasQualifiers() && "RTTI info cannot have top-level qualifiers");
Rafael Espindolac4850c22011-02-10 23:59:36 +00003558 CXXNameMangler Mangler(*this, Out);
Daniel Dunbarc0747712009-11-21 09:12:13 +00003559 Mangler.getStream() << "_ZTI";
3560 Mangler.mangleType(Ty);
Daniel Dunbar1b077112009-11-21 09:06:10 +00003561}
Mike Stump67795982009-11-14 00:14:13 +00003562
Peter Collingbourne14110472011-01-13 18:57:25 +00003563void ItaniumMangleContext::mangleCXXRTTIName(QualType Ty,
Chris Lattner5f9e2722011-07-23 10:55:15 +00003564 raw_ostream &Out) {
Daniel Dunbarc0747712009-11-21 09:12:13 +00003565 // <special-name> ::= TS <type> # typeinfo name (null terminated byte string)
Rafael Espindolac4850c22011-02-10 23:59:36 +00003566 CXXNameMangler Mangler(*this, Out);
Daniel Dunbarc0747712009-11-21 09:12:13 +00003567 Mangler.getStream() << "_ZTS";
3568 Mangler.mangleType(Ty);
Mike Stumpf1216772009-07-31 18:25:34 +00003569}
Peter Collingbourne14110472011-01-13 18:57:25 +00003570
3571MangleContext *clang::createItaniumMangleContext(ASTContext &Context,
David Blaikied6471f72011-09-25 23:23:43 +00003572 DiagnosticsEngine &Diags) {
Peter Collingbourne14110472011-01-13 18:57:25 +00003573 return new ItaniumMangleContext(Context, Diags);
3574}