blob: d8c6594d02bbf891eb37222207a0e41893282405 [file] [log] [blame]
Peter Collingbourne14110472011-01-13 18:57:25 +00001//===--- ItaniumMangle.cpp - Itanium C++ Name Mangling ----------*- C++ -*-===//
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// Implements C++ name mangling according to the Itanium C++ ABI,
11// which is used in GCC 3.2 and newer (and many compilers that are
12// ABI-compatible with GCC):
13//
14// http://www.codesourcery.com/public/cxx-abi/abi.html
15//
16//===----------------------------------------------------------------------===//
Peter Collingbourne14110472011-01-13 18:57:25 +000017#include "clang/AST/Mangle.h"
Douglas Gregor5f2bfd42009-02-13 00:10:09 +000018#include "clang/AST/ASTContext.h"
19#include "clang/AST/Decl.h"
20#include "clang/AST/DeclCXX.h"
Anders Carlssona40c5e42009-03-07 22:03:21 +000021#include "clang/AST/DeclObjC.h"
Anders Carlsson7a0ba872009-05-15 16:09:15 +000022#include "clang/AST/DeclTemplate.h"
Anders Carlsson50755b02009-09-27 20:11:34 +000023#include "clang/AST/ExprCXX.h"
John McCallf85e1932011-06-15 23:02:42 +000024#include "clang/AST/ExprObjC.h"
John McCallfb44de92011-05-01 22:35:37 +000025#include "clang/AST/TypeLoc.h"
Peter Collingbourne14110472011-01-13 18:57:25 +000026#include "clang/Basic/ABI.h"
Douglas Gregor6ec36682009-02-18 23:53:56 +000027#include "clang/Basic/SourceManager.h"
Rafael Espindola4e274e92011-02-15 22:23:51 +000028#include "clang/Basic/TargetInfo.h"
Anders Carlssonc4355b62009-10-07 01:45:02 +000029#include "llvm/ADT/StringExtras.h"
Douglas Gregor5f2bfd42009-02-13 00:10:09 +000030#include "llvm/Support/raw_ostream.h"
John McCallefe6aee2009-09-05 07:56:18 +000031#include "llvm/Support/ErrorHandling.h"
Anders Carlssonf98574b2010-02-05 07:31:37 +000032
33#define MANGLE_CHECKER 0
34
35#if MANGLE_CHECKER
36#include <cxxabi.h>
37#endif
38
Douglas Gregor5f2bfd42009-02-13 00:10:09 +000039using namespace clang;
Charles Davis685b1d92010-05-26 18:25:27 +000040
Douglas Gregor5f2bfd42009-02-13 00:10:09 +000041namespace {
Fariborz Jahanian57058532010-03-03 19:41:08 +000042
Douglas Gregorccc1b5e2012-02-21 00:37:24 +000043/// \brief Retrieve the declaration context that should be used when mangling
44/// the given declaration.
45static const DeclContext *getEffectiveDeclContext(const Decl *D) {
46 // The ABI assumes that lambda closure types that occur within
47 // default arguments live in the context of the function. However, due to
48 // the way in which Clang parses and creates function declarations, this is
49 // not the case: the lambda closure type ends up living in the context
50 // where the function itself resides, because the function declaration itself
51 // had not yet been created. Fix the context here.
52 if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D)) {
53 if (RD->isLambda())
54 if (ParmVarDecl *ContextParam
Douglas Gregor5878cbc2012-02-21 04:17:39 +000055 = dyn_cast_or_null<ParmVarDecl>(RD->getLambdaContextDecl()))
Douglas Gregorccc1b5e2012-02-21 00:37:24 +000056 return ContextParam->getDeclContext();
57 }
58
59 return D->getDeclContext();
60}
61
62static const DeclContext *getEffectiveParentContext(const DeclContext *DC) {
63 return getEffectiveDeclContext(cast<Decl>(DC));
64}
65
John McCall82b7d7b2010-10-18 21:28:44 +000066static const CXXRecordDecl *GetLocalClassDecl(const NamedDecl *ND) {
67 const DeclContext *DC = dyn_cast<DeclContext>(ND);
68 if (!DC)
Douglas Gregorccc1b5e2012-02-21 00:37:24 +000069 DC = getEffectiveDeclContext(ND);
John McCall82b7d7b2010-10-18 21:28:44 +000070 while (!DC->isNamespace() && !DC->isTranslationUnit()) {
Douglas Gregorccc1b5e2012-02-21 00:37:24 +000071 const DeclContext *Parent = getEffectiveDeclContext(cast<Decl>(DC));
72 if (isa<FunctionDecl>(Parent))
John McCall82b7d7b2010-10-18 21:28:44 +000073 return dyn_cast<CXXRecordDecl>(DC);
Douglas Gregorccc1b5e2012-02-21 00:37:24 +000074 DC = Parent;
Fariborz Jahanian57058532010-03-03 19:41:08 +000075 }
76 return 0;
77}
78
John McCallfb44de92011-05-01 22:35:37 +000079static const FunctionDecl *getStructor(const FunctionDecl *fn) {
80 if (const FunctionTemplateDecl *ftd = fn->getPrimaryTemplate())
81 return ftd->getTemplatedDecl();
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +000082
John McCallfb44de92011-05-01 22:35:37 +000083 return fn;
84}
Anders Carlsson7e120032009-11-24 05:36:32 +000085
John McCallfb44de92011-05-01 22:35:37 +000086static const NamedDecl *getStructor(const NamedDecl *decl) {
87 const FunctionDecl *fn = dyn_cast_or_null<FunctionDecl>(decl);
88 return (fn ? getStructor(fn) : decl);
Anders Carlsson7e120032009-11-24 05:36:32 +000089}
Douglas Gregorccc1b5e2012-02-21 00:37:24 +000090
John McCall1dd73832010-02-04 01:42:13 +000091static const unsigned UnknownArity = ~0U;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +000092
Peter Collingbourne14110472011-01-13 18:57:25 +000093class ItaniumMangleContext : public MangleContext {
94 llvm::DenseMap<const TagDecl *, uint64_t> AnonStructIds;
95 unsigned Discriminator;
96 llvm::DenseMap<const NamedDecl*, unsigned> Uniquifier;
97
98public:
99 explicit ItaniumMangleContext(ASTContext &Context,
David Blaikied6471f72011-09-25 23:23:43 +0000100 DiagnosticsEngine &Diags)
Peter Collingbourne14110472011-01-13 18:57:25 +0000101 : MangleContext(Context, Diags) { }
102
103 uint64_t getAnonymousStructId(const TagDecl *TD) {
104 std::pair<llvm::DenseMap<const TagDecl *,
105 uint64_t>::iterator, bool> Result =
106 AnonStructIds.insert(std::make_pair(TD, AnonStructIds.size()));
107 return Result.first->second;
108 }
109
110 void startNewFunction() {
111 MangleContext::startNewFunction();
112 mangleInitDiscriminator();
113 }
114
115 /// @name Mangler Entry Points
116 /// @{
117
118 bool shouldMangleDeclName(const NamedDecl *D);
Chris Lattner5f9e2722011-07-23 10:55:15 +0000119 void mangleName(const NamedDecl *D, raw_ostream &);
Peter Collingbourne14110472011-01-13 18:57:25 +0000120 void mangleThunk(const CXXMethodDecl *MD,
121 const ThunkInfo &Thunk,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000122 raw_ostream &);
Peter Collingbourne14110472011-01-13 18:57:25 +0000123 void mangleCXXDtorThunk(const CXXDestructorDecl *DD, CXXDtorType Type,
124 const ThisAdjustment &ThisAdjustment,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000125 raw_ostream &);
Peter Collingbourne14110472011-01-13 18:57:25 +0000126 void mangleReferenceTemporary(const VarDecl *D,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000127 raw_ostream &);
Peter Collingbourne14110472011-01-13 18:57:25 +0000128 void mangleCXXVTable(const CXXRecordDecl *RD,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000129 raw_ostream &);
Peter Collingbourne14110472011-01-13 18:57:25 +0000130 void mangleCXXVTT(const CXXRecordDecl *RD,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000131 raw_ostream &);
Peter Collingbourne14110472011-01-13 18:57:25 +0000132 void mangleCXXCtorVTable(const CXXRecordDecl *RD, int64_t Offset,
133 const CXXRecordDecl *Type,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000134 raw_ostream &);
135 void mangleCXXRTTI(QualType T, raw_ostream &);
136 void mangleCXXRTTIName(QualType T, raw_ostream &);
Peter Collingbourne14110472011-01-13 18:57:25 +0000137 void mangleCXXCtor(const CXXConstructorDecl *D, CXXCtorType Type,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000138 raw_ostream &);
Peter Collingbourne14110472011-01-13 18:57:25 +0000139 void mangleCXXDtor(const CXXDestructorDecl *D, CXXDtorType Type,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000140 raw_ostream &);
Peter Collingbourne14110472011-01-13 18:57:25 +0000141
Chris Lattner5f9e2722011-07-23 10:55:15 +0000142 void mangleItaniumGuardVariable(const VarDecl *D, raw_ostream &);
Peter Collingbourne14110472011-01-13 18:57:25 +0000143
144 void mangleInitDiscriminator() {
145 Discriminator = 0;
146 }
147
148 bool getNextDiscriminator(const NamedDecl *ND, unsigned &disc) {
Douglas Gregor9e8c92a2012-02-20 19:44:39 +0000149 // Lambda closure types with external linkage (indicated by a
150 // non-zero lambda mangling number) have their own numbering scheme, so
151 // they do not need a discriminator.
152 if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(ND))
153 if (RD->isLambda() && RD->getLambdaManglingNumber() > 0)
154 return false;
155
Peter Collingbourne14110472011-01-13 18:57:25 +0000156 unsigned &discriminator = Uniquifier[ND];
157 if (!discriminator)
158 discriminator = ++Discriminator;
159 if (discriminator == 1)
160 return false;
161 disc = discriminator-2;
162 return true;
163 }
164 /// @}
165};
166
Daniel Dunbar1b077112009-11-21 09:06:10 +0000167/// CXXNameMangler - Manage the mangling of a single name.
Daniel Dunbarc0747712009-11-21 09:12:13 +0000168class CXXNameMangler {
Peter Collingbourne14110472011-01-13 18:57:25 +0000169 ItaniumMangleContext &Context;
Chris Lattner5f9e2722011-07-23 10:55:15 +0000170 raw_ostream &Out;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000171
John McCallfb44de92011-05-01 22:35:37 +0000172 /// The "structor" is the top-level declaration being mangled, if
173 /// that's not a template specialization; otherwise it's the pattern
174 /// for that specialization.
175 const NamedDecl *Structor;
Daniel Dunbar1b077112009-11-21 09:06:10 +0000176 unsigned StructorType;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000177
Anders Carlsson9d85b722010-06-02 04:29:50 +0000178 /// SeqID - The next subsitution sequence number.
179 unsigned SeqID;
180
John McCallfb44de92011-05-01 22:35:37 +0000181 class FunctionTypeDepthState {
182 unsigned Bits;
183
184 enum { InResultTypeMask = 1 };
185
186 public:
187 FunctionTypeDepthState() : Bits(0) {}
188
189 /// The number of function types we're inside.
190 unsigned getDepth() const {
191 return Bits >> 1;
192 }
193
194 /// True if we're in the return type of the innermost function type.
195 bool isInResultType() const {
196 return Bits & InResultTypeMask;
197 }
198
199 FunctionTypeDepthState push() {
200 FunctionTypeDepthState tmp = *this;
201 Bits = (Bits & ~InResultTypeMask) + 2;
202 return tmp;
203 }
204
205 void enterResultType() {
206 Bits |= InResultTypeMask;
207 }
208
209 void leaveResultType() {
210 Bits &= ~InResultTypeMask;
211 }
212
213 void pop(FunctionTypeDepthState saved) {
214 assert(getDepth() == saved.getDepth() + 1);
215 Bits = saved.Bits;
216 }
217
218 } FunctionTypeDepth;
219
Daniel Dunbar1b077112009-11-21 09:06:10 +0000220 llvm::DenseMap<uintptr_t, unsigned> Substitutions;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000221
John McCall1dd73832010-02-04 01:42:13 +0000222 ASTContext &getASTContext() const { return Context.getASTContext(); }
223
Daniel Dunbarc0747712009-11-21 09:12:13 +0000224public:
Chris Lattner5f9e2722011-07-23 10:55:15 +0000225 CXXNameMangler(ItaniumMangleContext &C, raw_ostream &Out_,
John McCallfb44de92011-05-01 22:35:37 +0000226 const NamedDecl *D = 0)
227 : Context(C), Out(Out_), Structor(getStructor(D)), StructorType(0),
228 SeqID(0) {
229 // These can't be mangled without a ctor type or dtor type.
230 assert(!D || (!isa<CXXDestructorDecl>(D) &&
231 !isa<CXXConstructorDecl>(D)));
232 }
Chris Lattner5f9e2722011-07-23 10:55:15 +0000233 CXXNameMangler(ItaniumMangleContext &C, raw_ostream &Out_,
Daniel Dunbar77939c92009-11-21 09:06:31 +0000234 const CXXConstructorDecl *D, CXXCtorType Type)
Rafael Espindolac4850c22011-02-10 23:59:36 +0000235 : Context(C), Out(Out_), Structor(getStructor(D)), StructorType(Type),
John McCallfb44de92011-05-01 22:35:37 +0000236 SeqID(0) { }
Chris Lattner5f9e2722011-07-23 10:55:15 +0000237 CXXNameMangler(ItaniumMangleContext &C, raw_ostream &Out_,
Daniel Dunbar77939c92009-11-21 09:06:31 +0000238 const CXXDestructorDecl *D, CXXDtorType Type)
Rafael Espindolac4850c22011-02-10 23:59:36 +0000239 : Context(C), Out(Out_), Structor(getStructor(D)), StructorType(Type),
John McCallfb44de92011-05-01 22:35:37 +0000240 SeqID(0) { }
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000241
Anders Carlssonf98574b2010-02-05 07:31:37 +0000242#if MANGLE_CHECKER
243 ~CXXNameMangler() {
244 if (Out.str()[0] == '\01')
245 return;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000246
Anders Carlssonf98574b2010-02-05 07:31:37 +0000247 int status = 0;
248 char *result = abi::__cxa_demangle(Out.str().str().c_str(), 0, 0, &status);
249 assert(status == 0 && "Could not demangle mangled name!");
250 free(result);
251 }
252#endif
Chris Lattner5f9e2722011-07-23 10:55:15 +0000253 raw_ostream &getStream() { return Out; }
Daniel Dunbarc0747712009-11-21 09:12:13 +0000254
Chris Lattner5f9e2722011-07-23 10:55:15 +0000255 void mangle(const NamedDecl *D, StringRef Prefix = "_Z");
Anders Carlsson19879c92010-03-23 17:17:29 +0000256 void mangleCallOffset(int64_t NonVirtual, int64_t Virtual);
John McCall0512e482010-07-14 04:20:34 +0000257 void mangleNumber(const llvm::APSInt &I);
Anders Carlssona94822e2009-11-26 02:32:05 +0000258 void mangleNumber(int64_t Number);
John McCall0512e482010-07-14 04:20:34 +0000259 void mangleFloat(const llvm::APFloat &F);
Daniel Dunbarc0747712009-11-21 09:12:13 +0000260 void mangleFunctionEncoding(const FunctionDecl *FD);
261 void mangleName(const NamedDecl *ND);
262 void mangleType(QualType T);
Douglas Gregor1b12a3b2010-05-26 05:11:13 +0000263 void mangleNameOrStandardSubstitution(const NamedDecl *ND);
264
Daniel Dunbarc0747712009-11-21 09:12:13 +0000265private:
Daniel Dunbar1b077112009-11-21 09:06:10 +0000266 bool mangleSubstitution(const NamedDecl *ND);
267 bool mangleSubstitution(QualType T);
Douglas Gregor1e9268e2010-04-28 05:58:56 +0000268 bool mangleSubstitution(TemplateName Template);
Daniel Dunbar1b077112009-11-21 09:06:10 +0000269 bool mangleSubstitution(uintptr_t Ptr);
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000270
John McCall68a51a72011-07-01 00:04:39 +0000271 void mangleExistingSubstitution(QualType type);
272 void mangleExistingSubstitution(TemplateName name);
273
Daniel Dunbar1b077112009-11-21 09:06:10 +0000274 bool mangleStandardSubstitution(const NamedDecl *ND);
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000275
Daniel Dunbar1b077112009-11-21 09:06:10 +0000276 void addSubstitution(const NamedDecl *ND) {
277 ND = cast<NamedDecl>(ND->getCanonicalDecl());
Anders Carlsson433d1372009-11-07 04:26:04 +0000278
Daniel Dunbar1b077112009-11-21 09:06:10 +0000279 addSubstitution(reinterpret_cast<uintptr_t>(ND));
280 }
281 void addSubstitution(QualType T);
Douglas Gregor1e9268e2010-04-28 05:58:56 +0000282 void addSubstitution(TemplateName Template);
Daniel Dunbar1b077112009-11-21 09:06:10 +0000283 void addSubstitution(uintptr_t Ptr);
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000284
John McCalla0ce15c2011-04-24 08:23:24 +0000285 void mangleUnresolvedPrefix(NestedNameSpecifier *qualifier,
286 NamedDecl *firstQualifierLookup,
287 bool recursive = false);
288 void mangleUnresolvedName(NestedNameSpecifier *qualifier,
289 NamedDecl *firstQualifierLookup,
290 DeclarationName name,
John McCall1dd73832010-02-04 01:42:13 +0000291 unsigned KnownArity = UnknownArity);
292
Daniel Dunbar1b077112009-11-21 09:06:10 +0000293 void mangleName(const TemplateDecl *TD,
294 const TemplateArgument *TemplateArgs,
295 unsigned NumTemplateArgs);
John McCall1dd73832010-02-04 01:42:13 +0000296 void mangleUnqualifiedName(const NamedDecl *ND) {
297 mangleUnqualifiedName(ND, ND->getDeclName(), UnknownArity);
298 }
299 void mangleUnqualifiedName(const NamedDecl *ND, DeclarationName Name,
300 unsigned KnownArity);
Daniel Dunbar1b077112009-11-21 09:06:10 +0000301 void mangleUnscopedName(const NamedDecl *ND);
302 void mangleUnscopedTemplateName(const TemplateDecl *ND);
Douglas Gregor1e9268e2010-04-28 05:58:56 +0000303 void mangleUnscopedTemplateName(TemplateName);
Daniel Dunbar1b077112009-11-21 09:06:10 +0000304 void mangleSourceName(const IdentifierInfo *II);
305 void mangleLocalName(const NamedDecl *ND);
Douglas Gregorccc1b5e2012-02-21 00:37:24 +0000306 void mangleLambda(const CXXRecordDecl *Lambda);
Fariborz Jahanian57058532010-03-03 19:41:08 +0000307 void mangleNestedName(const NamedDecl *ND, const DeclContext *DC,
308 bool NoFunction=false);
Daniel Dunbar1b077112009-11-21 09:06:10 +0000309 void mangleNestedName(const TemplateDecl *TD,
310 const TemplateArgument *TemplateArgs,
311 unsigned NumTemplateArgs);
John McCalla0ce15c2011-04-24 08:23:24 +0000312 void manglePrefix(NestedNameSpecifier *qualifier);
Fariborz Jahanian57058532010-03-03 19:41:08 +0000313 void manglePrefix(const DeclContext *DC, bool NoFunction=false);
John McCall4f4e4132011-05-04 01:45:19 +0000314 void manglePrefix(QualType type);
Daniel Dunbar1b077112009-11-21 09:06:10 +0000315 void mangleTemplatePrefix(const TemplateDecl *ND);
Douglas Gregor20f0cc72010-04-23 03:10:43 +0000316 void mangleTemplatePrefix(TemplateName Template);
Daniel Dunbar1b077112009-11-21 09:06:10 +0000317 void mangleOperatorName(OverloadedOperatorKind OO, unsigned Arity);
318 void mangleQualifiers(Qualifiers Quals);
Douglas Gregor0a9a6d62011-01-26 17:36:28 +0000319 void mangleRefQualifier(RefQualifierKind RefQualifier);
John McCallefe6aee2009-09-05 07:56:18 +0000320
Anders Carlsson7b06f6c2009-12-10 03:14:39 +0000321 void mangleObjCMethodName(const ObjCMethodDecl *MD);
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000322
Daniel Dunbar1b077112009-11-21 09:06:10 +0000323 // Declare manglers for every type class.
John McCallefe6aee2009-09-05 07:56:18 +0000324#define ABSTRACT_TYPE(CLASS, PARENT)
325#define NON_CANONICAL_TYPE(CLASS, PARENT)
326#define TYPE(CLASS, PARENT) void mangleType(const CLASS##Type *T);
327#include "clang/AST/TypeNodes.def"
328
Daniel Dunbar1b077112009-11-21 09:06:10 +0000329 void mangleType(const TagType*);
John McCallb6f532e2010-07-14 06:43:17 +0000330 void mangleType(TemplateName);
Daniel Dunbar1b077112009-11-21 09:06:10 +0000331 void mangleBareFunctionType(const FunctionType *T,
332 bool MangleReturnType);
Bob Wilson57147a82010-11-16 00:32:18 +0000333 void mangleNeonVectorType(const VectorType *T);
Anders Carlssone170ba72009-12-14 01:45:37 +0000334
335 void mangleIntegerLiteral(QualType T, const llvm::APSInt &Value);
John McCalla0ce15c2011-04-24 08:23:24 +0000336 void mangleMemberExpr(const Expr *base, bool isArrow,
337 NestedNameSpecifier *qualifier,
338 NamedDecl *firstQualifierLookup,
339 DeclarationName name,
340 unsigned knownArity);
John McCall5e1e89b2010-08-18 19:18:59 +0000341 void mangleExpression(const Expr *E, unsigned Arity = UnknownArity);
Daniel Dunbar1b077112009-11-21 09:06:10 +0000342 void mangleCXXCtorType(CXXCtorType T);
343 void mangleCXXDtorType(CXXDtorType T);
Mike Stump1eb44332009-09-09 15:08:12 +0000344
Argyrios Kyrtzidisb0c3e092011-09-22 20:07:03 +0000345 void mangleTemplateArgs(const ASTTemplateArgumentListInfo &TemplateArgs);
Eli Friedmand7a6b162012-09-26 02:36:12 +0000346 void mangleTemplateArgs(const TemplateArgument *TemplateArgs,
Daniel Dunbar1b077112009-11-21 09:06:10 +0000347 unsigned NumTemplateArgs);
Eli Friedmand7a6b162012-09-26 02:36:12 +0000348 void mangleTemplateArgs(const TemplateArgumentList &AL);
349 void mangleTemplateArg(TemplateArgument A);
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000350
Daniel Dunbar1b077112009-11-21 09:06:10 +0000351 void mangleTemplateParameter(unsigned Index);
John McCallfb44de92011-05-01 22:35:37 +0000352
353 void mangleFunctionParam(const ParmVarDecl *parm);
Daniel Dunbar1b077112009-11-21 09:06:10 +0000354};
Peter Collingbourne14110472011-01-13 18:57:25 +0000355
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000356}
357
Anders Carlsson43f17402009-04-02 15:51:53 +0000358static bool isInCLinkageSpecification(const Decl *D) {
Douglas Gregor457e2812009-10-28 16:31:34 +0000359 D = D->getCanonicalDecl();
Douglas Gregorccc1b5e2012-02-21 00:37:24 +0000360 for (const DeclContext *DC = getEffectiveDeclContext(D);
361 !DC->isTranslationUnit(); DC = getEffectiveParentContext(DC)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000362 if (const LinkageSpecDecl *Linkage = dyn_cast<LinkageSpecDecl>(DC))
Anders Carlsson43f17402009-04-02 15:51:53 +0000363 return Linkage->getLanguage() == LinkageSpecDecl::lang_c;
364 }
Mike Stump1eb44332009-09-09 15:08:12 +0000365
Anders Carlsson43f17402009-04-02 15:51:53 +0000366 return false;
367}
368
Peter Collingbourne14110472011-01-13 18:57:25 +0000369bool ItaniumMangleContext::shouldMangleDeclName(const NamedDecl *D) {
Daniel Dunbarf981bf82009-11-21 09:14:52 +0000370 // In C, functions with no attributes never need to be mangled. Fastpath them.
David Blaikie4e4d0842012-03-11 07:00:24 +0000371 if (!getASTContext().getLangOpts().CPlusPlus && !D->hasAttrs())
Daniel Dunbarf981bf82009-11-21 09:14:52 +0000372 return false;
373
374 // Any decl can be declared with __asm("foo") on it, and this takes precedence
375 // over all other naming in the .o file.
376 if (D->hasAttr<AsmLabelAttr>())
377 return true;
378
Mike Stump141c5af2009-09-02 00:25:38 +0000379 // Clang's "overloadable" attribute extension to C/C++ implies name mangling
Anders Carlssona1e16222009-11-07 07:15:03 +0000380 // (always) as does passing a C++ member function and a function
381 // whose name is not a simple identifier.
Daniel Dunbarf981bf82009-11-21 09:14:52 +0000382 const FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
383 if (FD && (FD->hasAttr<OverloadableAttr>() || isa<CXXMethodDecl>(FD) ||
384 !FD->getDeclName().isIdentifier()))
385 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000386
Daniel Dunbarf981bf82009-11-21 09:14:52 +0000387 // Otherwise, no mangling is done outside C++ mode.
David Blaikie4e4d0842012-03-11 07:00:24 +0000388 if (!getASTContext().getLangOpts().CPlusPlus)
Daniel Dunbarf981bf82009-11-21 09:14:52 +0000389 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000390
Sean Hunt31455252010-01-24 03:04:27 +0000391 // Variables at global scope with non-internal linkage are not mangled
Eli Friedman7facf842009-12-02 20:32:49 +0000392 if (!FD) {
Douglas Gregorccc1b5e2012-02-21 00:37:24 +0000393 const DeclContext *DC = getEffectiveDeclContext(D);
Eli Friedman7facf842009-12-02 20:32:49 +0000394 // Check for extern variable declared locally.
Fariborz Jahaniane81c5612010-06-30 18:57:21 +0000395 if (DC->isFunctionOrMethod() && D->hasLinkage())
Eli Friedman7facf842009-12-02 20:32:49 +0000396 while (!DC->isNamespace() && !DC->isTranslationUnit())
Douglas Gregorccc1b5e2012-02-21 00:37:24 +0000397 DC = getEffectiveParentContext(DC);
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000398 if (DC->isTranslationUnit() && D->getLinkage() != InternalLinkage)
Eli Friedman7facf842009-12-02 20:32:49 +0000399 return false;
400 }
401
Eli Friedmanc00cb642010-07-18 20:49:59 +0000402 // Class members are always mangled.
Douglas Gregorccc1b5e2012-02-21 00:37:24 +0000403 if (getEffectiveDeclContext(D)->isRecord())
Eli Friedmanc00cb642010-07-18 20:49:59 +0000404 return true;
405
Eli Friedman7facf842009-12-02 20:32:49 +0000406 // C functions and "main" are not mangled.
407 if ((FD && FD->isMain()) || isInCLinkageSpecification(D))
Daniel Dunbarf981bf82009-11-21 09:14:52 +0000408 return false;
409
Anders Carlsson43f17402009-04-02 15:51:53 +0000410 return true;
411}
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000412
Chris Lattner5f9e2722011-07-23 10:55:15 +0000413void CXXNameMangler::mangle(const NamedDecl *D, StringRef Prefix) {
Mike Stump141c5af2009-09-02 00:25:38 +0000414 // Any decl can be declared with __asm("foo") on it, and this takes precedence
415 // over all other naming in the .o file.
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000416 if (const AsmLabelAttr *ALA = D->getAttr<AsmLabelAttr>()) {
Chris Lattnerca3f25c2009-03-21 08:24:40 +0000417 // If we have an asm name, then we use it as the mangling.
Rafael Espindola4e274e92011-02-15 22:23:51 +0000418
419 // Adding the prefix can cause problems when one file has a "foo" and
420 // another has a "\01foo". That is known to happen on ELF with the
421 // tricks normally used for producing aliases (PR9177). Fortunately the
422 // llvm mangler on ELF is a nop, so we can just avoid adding the \01
Peter Collingbourne69317432011-04-06 12:29:09 +0000423 // marker. We also avoid adding the marker if this is an alias for an
424 // LLVM intrinsic.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000425 StringRef UserLabelPrefix =
Douglas Gregorbcfd1f52011-09-02 00:18:52 +0000426 getASTContext().getTargetInfo().getUserLabelPrefix();
Peter Collingbourne69317432011-04-06 12:29:09 +0000427 if (!UserLabelPrefix.empty() && !ALA->getLabel().startswith("llvm."))
Rafael Espindola4e274e92011-02-15 22:23:51 +0000428 Out << '\01'; // LLVM IR Marker for __asm("foo")
429
Chris Lattnerca3f25c2009-03-21 08:24:40 +0000430 Out << ALA->getLabel();
Daniel Dunbarf981bf82009-11-21 09:14:52 +0000431 return;
Chris Lattnerca3f25c2009-03-21 08:24:40 +0000432 }
Mike Stump1eb44332009-09-09 15:08:12 +0000433
Sean Hunt31455252010-01-24 03:04:27 +0000434 // <mangled-name> ::= _Z <encoding>
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000435 // ::= <data name>
436 // ::= <special-name>
Daniel Dunbar7e0c1952009-11-21 09:17:15 +0000437 Out << Prefix;
438 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
Daniel Dunbarf981bf82009-11-21 09:14:52 +0000439 mangleFunctionEncoding(FD);
Rafael Espindolad9800722010-03-11 14:07:00 +0000440 else if (const VarDecl *VD = dyn_cast<VarDecl>(D))
441 mangleName(VD);
Daniel Dunbar7e0c1952009-11-21 09:17:15 +0000442 else
Rafael Espindolad9800722010-03-11 14:07:00 +0000443 mangleName(cast<FieldDecl>(D));
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000444}
445
446void CXXNameMangler::mangleFunctionEncoding(const FunctionDecl *FD) {
447 // <encoding> ::= <function name> <bare-function-type>
448 mangleName(FD);
Mike Stump1eb44332009-09-09 15:08:12 +0000449
Daniel Dunbar7e0c1952009-11-21 09:17:15 +0000450 // Don't mangle in the type if this isn't a decl we should typically mangle.
451 if (!Context.shouldMangleDeclName(FD))
452 return;
453
Mike Stump141c5af2009-09-02 00:25:38 +0000454 // Whether the mangling of a function type includes the return type depends on
455 // the context and the nature of the function. The rules for deciding whether
456 // the return type is included are:
Mike Stump1eb44332009-09-09 15:08:12 +0000457 //
Douglas Gregor1fd2dd12009-06-29 22:39:32 +0000458 // 1. Template functions (names or types) have return types encoded, with
459 // the exceptions listed below.
Mike Stump1eb44332009-09-09 15:08:12 +0000460 // 2. Function types not appearing as part of a function name mangling,
Douglas Gregor1fd2dd12009-06-29 22:39:32 +0000461 // e.g. parameters, pointer types, etc., have return type encoded, with the
462 // exceptions listed below.
463 // 3. Non-template function names do not have return types encoded.
464 //
Mike Stump141c5af2009-09-02 00:25:38 +0000465 // The exceptions mentioned in (1) and (2) above, for which the return type is
466 // never included, are
Douglas Gregor1fd2dd12009-06-29 22:39:32 +0000467 // 1. Constructors.
468 // 2. Destructors.
469 // 3. Conversion operator functions, e.g. operator int.
470 bool MangleReturnType = false;
Anders Carlsson9234b7f2009-09-17 03:46:43 +0000471 if (FunctionTemplateDecl *PrimaryTemplate = FD->getPrimaryTemplate()) {
472 if (!(isa<CXXConstructorDecl>(FD) || isa<CXXDestructorDecl>(FD) ||
473 isa<CXXConversionDecl>(FD)))
474 MangleReturnType = true;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000475
Anders Carlsson9234b7f2009-09-17 03:46:43 +0000476 // Mangle the type of the primary template.
477 FD = PrimaryTemplate->getTemplatedDecl();
478 }
479
Douglas Gregor79e6bd32011-07-12 04:42:08 +0000480 mangleBareFunctionType(FD->getType()->getAs<FunctionType>(),
481 MangleReturnType);
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000482}
483
Anders Carlsson47846d22009-12-04 06:23:23 +0000484static const DeclContext *IgnoreLinkageSpecDecls(const DeclContext *DC) {
485 while (isa<LinkageSpecDecl>(DC)) {
Douglas Gregorccc1b5e2012-02-21 00:37:24 +0000486 DC = getEffectiveParentContext(DC);
Anders Carlsson47846d22009-12-04 06:23:23 +0000487 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000488
Anders Carlsson47846d22009-12-04 06:23:23 +0000489 return DC;
490}
491
Anders Carlssonc820f902010-06-02 15:58:27 +0000492/// isStd - Return whether a given namespace is the 'std' namespace.
493static bool isStd(const NamespaceDecl *NS) {
Douglas Gregorccc1b5e2012-02-21 00:37:24 +0000494 if (!IgnoreLinkageSpecDecls(getEffectiveParentContext(NS))
495 ->isTranslationUnit())
Anders Carlssonc820f902010-06-02 15:58:27 +0000496 return false;
497
498 const IdentifierInfo *II = NS->getOriginalNamespace()->getIdentifier();
499 return II && II->isStr("std");
500}
501
Anders Carlsson47846d22009-12-04 06:23:23 +0000502// isStdNamespace - Return whether a given decl context is a toplevel 'std'
503// namespace.
Daniel Dunbar1308af92009-11-21 09:11:45 +0000504static bool isStdNamespace(const DeclContext *DC) {
Anders Carlsson47846d22009-12-04 06:23:23 +0000505 if (!DC->isNamespace())
506 return false;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000507
Anders Carlsson47846d22009-12-04 06:23:23 +0000508 return isStd(cast<NamespaceDecl>(DC));
Daniel Dunbar1308af92009-11-21 09:11:45 +0000509}
510
Anders Carlssonbb36ba42009-09-26 03:24:57 +0000511static const TemplateDecl *
512isTemplate(const NamedDecl *ND, const TemplateArgumentList *&TemplateArgs) {
Anders Carlsson2744a062009-09-18 19:00:18 +0000513 // Check if we have a function template.
514 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)){
Anders Carlssonbb36ba42009-09-26 03:24:57 +0000515 if (const TemplateDecl *TD = FD->getPrimaryTemplate()) {
Anders Carlsson2744a062009-09-18 19:00:18 +0000516 TemplateArgs = FD->getTemplateSpecializationArgs();
Anders Carlssonbb36ba42009-09-26 03:24:57 +0000517 return TD;
Anders Carlsson2744a062009-09-18 19:00:18 +0000518 }
519 }
520
Anders Carlssoneafc6dc2009-09-18 19:44:50 +0000521 // Check if we have a class template.
522 if (const ClassTemplateSpecializationDecl *Spec =
523 dyn_cast<ClassTemplateSpecializationDecl>(ND)) {
524 TemplateArgs = &Spec->getTemplateArgs();
Anders Carlssonbb36ba42009-09-26 03:24:57 +0000525 return Spec->getSpecializedTemplate();
Anders Carlssoneafc6dc2009-09-18 19:44:50 +0000526 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000527
Anders Carlsson2744a062009-09-18 19:00:18 +0000528 return 0;
529}
530
Douglas Gregorf54486a2012-04-04 17:40:10 +0000531static bool isLambda(const NamedDecl *ND) {
532 const CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(ND);
533 if (!Record)
534 return false;
535
536 return Record->isLambda();
537}
538
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000539void CXXNameMangler::mangleName(const NamedDecl *ND) {
540 // <name> ::= <nested-name>
541 // ::= <unscoped-name>
542 // ::= <unscoped-template-name> <template-args>
Anders Carlsson201ce742009-09-17 03:17:01 +0000543 // ::= <local-name>
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000544 //
Douglas Gregorccc1b5e2012-02-21 00:37:24 +0000545 const DeclContext *DC = getEffectiveDeclContext(ND);
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000546
Eli Friedman7facf842009-12-02 20:32:49 +0000547 // If this is an extern variable declared locally, the relevant DeclContext
548 // is that of the containing namespace, or the translation unit.
Douglas Gregorf54486a2012-04-04 17:40:10 +0000549 // FIXME: This is a hack; extern variables declared locally should have
550 // a proper semantic declaration context!
551 if (isa<FunctionDecl>(DC) && ND->hasLinkage() && !isLambda(ND))
Eli Friedman7facf842009-12-02 20:32:49 +0000552 while (!DC->isNamespace() && !DC->isTranslationUnit())
Douglas Gregorccc1b5e2012-02-21 00:37:24 +0000553 DC = getEffectiveParentContext(DC);
John McCall82b7d7b2010-10-18 21:28:44 +0000554 else if (GetLocalClassDecl(ND)) {
555 mangleLocalName(ND);
556 return;
557 }
Eli Friedman7facf842009-12-02 20:32:49 +0000558
James Molloyb3c312c2012-03-05 09:59:43 +0000559 DC = IgnoreLinkageSpecDecls(DC);
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000560
Anders Carlssond58d6f72009-09-17 16:12:20 +0000561 if (DC->isTranslationUnit() || isStdNamespace(DC)) {
Anders Carlsson2744a062009-09-18 19:00:18 +0000562 // Check if we have a template.
563 const TemplateArgumentList *TemplateArgs = 0;
Anders Carlsson0fa6df42009-09-26 19:45:45 +0000564 if (const TemplateDecl *TD = isTemplate(ND, TemplateArgs)) {
Anders Carlsson2744a062009-09-18 19:00:18 +0000565 mangleUnscopedTemplateName(TD);
Eli Friedmand7a6b162012-09-26 02:36:12 +0000566 mangleTemplateArgs(*TemplateArgs);
Anders Carlsson2744a062009-09-18 19:00:18 +0000567 return;
Anders Carlsson7482e242009-09-18 04:29:09 +0000568 }
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000569
Anders Carlsson7482e242009-09-18 04:29:09 +0000570 mangleUnscopedName(ND);
571 return;
572 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000573
Anders Carlsson7b06f6c2009-12-10 03:14:39 +0000574 if (isa<FunctionDecl>(DC) || isa<ObjCMethodDecl>(DC)) {
Anders Carlsson7482e242009-09-18 04:29:09 +0000575 mangleLocalName(ND);
576 return;
577 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000578
Eli Friedman7facf842009-12-02 20:32:49 +0000579 mangleNestedName(ND, DC);
Anders Carlsson7482e242009-09-18 04:29:09 +0000580}
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000581void CXXNameMangler::mangleName(const TemplateDecl *TD,
Anders Carlsson7624f212009-09-18 02:42:01 +0000582 const TemplateArgument *TemplateArgs,
583 unsigned NumTemplateArgs) {
Douglas Gregorccc1b5e2012-02-21 00:37:24 +0000584 const DeclContext *DC = IgnoreLinkageSpecDecls(getEffectiveDeclContext(TD));
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000585
Anders Carlsson7624f212009-09-18 02:42:01 +0000586 if (DC->isTranslationUnit() || isStdNamespace(DC)) {
Anders Carlsson0fa6df42009-09-26 19:45:45 +0000587 mangleUnscopedTemplateName(TD);
Eli Friedmand7a6b162012-09-26 02:36:12 +0000588 mangleTemplateArgs(TemplateArgs, NumTemplateArgs);
Anders Carlsson7624f212009-09-18 02:42:01 +0000589 } else {
590 mangleNestedName(TD, TemplateArgs, NumTemplateArgs);
591 }
592}
593
Anders Carlsson201ce742009-09-17 03:17:01 +0000594void CXXNameMangler::mangleUnscopedName(const NamedDecl *ND) {
595 // <unscoped-name> ::= <unqualified-name>
596 // ::= St <unqualified-name> # ::std::
James Molloyb3c312c2012-03-05 09:59:43 +0000597
598 if (isStdNamespace(IgnoreLinkageSpecDecls(getEffectiveDeclContext(ND))))
Anders Carlsson201ce742009-09-17 03:17:01 +0000599 Out << "St";
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000600
Anders Carlsson201ce742009-09-17 03:17:01 +0000601 mangleUnqualifiedName(ND);
602}
603
Anders Carlsson0fa6df42009-09-26 19:45:45 +0000604void CXXNameMangler::mangleUnscopedTemplateName(const TemplateDecl *ND) {
Anders Carlsson201ce742009-09-17 03:17:01 +0000605 // <unscoped-template-name> ::= <unscoped-name>
606 // ::= <substitution>
Anders Carlsson7624f212009-09-18 02:42:01 +0000607 if (mangleSubstitution(ND))
Anders Carlsson03c9d532009-09-17 04:02:31 +0000608 return;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000609
Douglas Gregor32fb4e12010-02-05 20:45:00 +0000610 // <template-template-param> ::= <template-param>
611 if (const TemplateTemplateParmDecl *TTP
612 = dyn_cast<TemplateTemplateParmDecl>(ND)) {
613 mangleTemplateParameter(TTP->getIndex());
614 return;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000615 }
Douglas Gregor32fb4e12010-02-05 20:45:00 +0000616
Anders Carlsson1668f202009-09-26 20:13:56 +0000617 mangleUnscopedName(ND->getTemplatedDecl());
Anders Carlsson7624f212009-09-18 02:42:01 +0000618 addSubstitution(ND);
Anders Carlsson201ce742009-09-17 03:17:01 +0000619}
620
Douglas Gregor1e9268e2010-04-28 05:58:56 +0000621void CXXNameMangler::mangleUnscopedTemplateName(TemplateName Template) {
622 // <unscoped-template-name> ::= <unscoped-name>
623 // ::= <substitution>
624 if (TemplateDecl *TD = Template.getAsTemplateDecl())
625 return mangleUnscopedTemplateName(TD);
Sean Huntc3021132010-05-05 15:23:54 +0000626
Douglas Gregor1e9268e2010-04-28 05:58:56 +0000627 if (mangleSubstitution(Template))
628 return;
629
Douglas Gregor1e9268e2010-04-28 05:58:56 +0000630 DependentTemplateName *Dependent = Template.getAsDependentTemplateName();
631 assert(Dependent && "Not a dependent template name?");
Douglas Gregor19617912011-07-12 05:06:05 +0000632 if (const IdentifierInfo *Id = Dependent->getIdentifier())
633 mangleSourceName(Id);
634 else
635 mangleOperatorName(Dependent->getOperator(), UnknownArity);
Sean Huntc3021132010-05-05 15:23:54 +0000636
Douglas Gregor1e9268e2010-04-28 05:58:56 +0000637 addSubstitution(Template);
638}
639
John McCall1b600522011-04-24 03:07:16 +0000640void CXXNameMangler::mangleFloat(const llvm::APFloat &f) {
641 // ABI:
642 // Floating-point literals are encoded using a fixed-length
643 // lowercase hexadecimal string corresponding to the internal
644 // representation (IEEE on Itanium), high-order bytes first,
645 // without leading zeroes. For example: "Lf bf800000 E" is -1.0f
646 // on Itanium.
John McCall0c8731a2012-01-30 18:36:31 +0000647 // The 'without leading zeroes' thing seems to be an editorial
648 // mistake; see the discussion on cxx-abi-dev beginning on
649 // 2012-01-16.
John McCall1b600522011-04-24 03:07:16 +0000650
Benjamin Kramer48d798c2012-06-02 10:20:41 +0000651 // Our requirements here are just barely weird enough to justify
John McCall0c8731a2012-01-30 18:36:31 +0000652 // using a custom algorithm instead of post-processing APInt::toString().
John McCall1b600522011-04-24 03:07:16 +0000653
John McCall0c8731a2012-01-30 18:36:31 +0000654 llvm::APInt valueBits = f.bitcastToAPInt();
655 unsigned numCharacters = (valueBits.getBitWidth() + 3) / 4;
656 assert(numCharacters != 0);
657
658 // Allocate a buffer of the right number of characters.
659 llvm::SmallVector<char, 20> buffer;
660 buffer.set_size(numCharacters);
661
662 // Fill the buffer left-to-right.
663 for (unsigned stringIndex = 0; stringIndex != numCharacters; ++stringIndex) {
664 // The bit-index of the next hex digit.
665 unsigned digitBitIndex = 4 * (numCharacters - stringIndex - 1);
666
667 // Project out 4 bits starting at 'digitIndex'.
668 llvm::integerPart hexDigit
669 = valueBits.getRawData()[digitBitIndex / llvm::integerPartWidth];
670 hexDigit >>= (digitBitIndex % llvm::integerPartWidth);
671 hexDigit &= 0xF;
672
673 // Map that over to a lowercase hex digit.
674 static const char charForHex[16] = {
675 '0', '1', '2', '3', '4', '5', '6', '7',
676 '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'
677 };
678 buffer[stringIndex] = charForHex[hexDigit];
679 }
680
681 Out.write(buffer.data(), numCharacters);
John McCall0512e482010-07-14 04:20:34 +0000682}
683
684void CXXNameMangler::mangleNumber(const llvm::APSInt &Value) {
685 if (Value.isSigned() && Value.isNegative()) {
686 Out << 'n';
John McCall54c86f72012-08-18 04:51:52 +0000687 Value.abs().print(Out, /*signed*/ false);
688 } else {
689 Value.print(Out, /*signed*/ false);
690 }
John McCall0512e482010-07-14 04:20:34 +0000691}
692
Anders Carlssona94822e2009-11-26 02:32:05 +0000693void CXXNameMangler::mangleNumber(int64_t Number) {
694 // <number> ::= [n] <non-negative decimal integer>
695 if (Number < 0) {
696 Out << 'n';
697 Number = -Number;
698 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000699
Anders Carlssona94822e2009-11-26 02:32:05 +0000700 Out << Number;
701}
702
Anders Carlsson19879c92010-03-23 17:17:29 +0000703void CXXNameMangler::mangleCallOffset(int64_t NonVirtual, int64_t Virtual) {
Mike Stump141c5af2009-09-02 00:25:38 +0000704 // <call-offset> ::= h <nv-offset> _
705 // ::= v <v-offset> _
706 // <nv-offset> ::= <offset number> # non-virtual base override
Anders Carlssona94822e2009-11-26 02:32:05 +0000707 // <v-offset> ::= <offset number> _ <virtual offset number>
Mike Stump141c5af2009-09-02 00:25:38 +0000708 // # virtual base override, with vcall offset
Anders Carlsson19879c92010-03-23 17:17:29 +0000709 if (!Virtual) {
Anders Carlssona94822e2009-11-26 02:32:05 +0000710 Out << 'h';
Anders Carlsson19879c92010-03-23 17:17:29 +0000711 mangleNumber(NonVirtual);
Anders Carlssona94822e2009-11-26 02:32:05 +0000712 Out << '_';
713 return;
Mike Stump141c5af2009-09-02 00:25:38 +0000714 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000715
Anders Carlssona94822e2009-11-26 02:32:05 +0000716 Out << 'v';
Anders Carlsson19879c92010-03-23 17:17:29 +0000717 mangleNumber(NonVirtual);
Anders Carlssona94822e2009-11-26 02:32:05 +0000718 Out << '_';
Anders Carlsson19879c92010-03-23 17:17:29 +0000719 mangleNumber(Virtual);
Anders Carlssona94822e2009-11-26 02:32:05 +0000720 Out << '_';
Mike Stump9124bcc2009-09-02 00:56:18 +0000721}
722
John McCall4f4e4132011-05-04 01:45:19 +0000723void CXXNameMangler::manglePrefix(QualType type) {
John McCalla0ce15c2011-04-24 08:23:24 +0000724 if (const TemplateSpecializationType *TST =
725 type->getAs<TemplateSpecializationType>()) {
726 if (!mangleSubstitution(QualType(TST, 0))) {
727 mangleTemplatePrefix(TST->getTemplateName());
Sean Huntc3021132010-05-05 15:23:54 +0000728
Douglas Gregoraa2187d2011-02-28 00:04:36 +0000729 // FIXME: GCC does not appear to mangle the template arguments when
730 // the template in question is a dependent template name. Should we
731 // emulate that badness?
Eli Friedmand7a6b162012-09-26 02:36:12 +0000732 mangleTemplateArgs(TST->getArgs(), TST->getNumArgs());
John McCalla0ce15c2011-04-24 08:23:24 +0000733 addSubstitution(QualType(TST, 0));
Rafael Espindola9b35b252010-03-17 04:28:11 +0000734 }
John McCalla0ce15c2011-04-24 08:23:24 +0000735 } else if (const DependentTemplateSpecializationType *DTST
736 = type->getAs<DependentTemplateSpecializationType>()) {
737 TemplateName Template
738 = getASTContext().getDependentTemplateName(DTST->getQualifier(),
739 DTST->getIdentifier());
740 mangleTemplatePrefix(Template);
741
742 // FIXME: GCC does not appear to mangle the template arguments when
743 // the template in question is a dependent template name. Should we
744 // emulate that badness?
Eli Friedmand7a6b162012-09-26 02:36:12 +0000745 mangleTemplateArgs(DTST->getArgs(), DTST->getNumArgs());
John McCalla0ce15c2011-04-24 08:23:24 +0000746 } else {
747 // We use the QualType mangle type variant here because it handles
748 // substitutions.
749 mangleType(type);
John McCall1dd73832010-02-04 01:42:13 +0000750 }
751}
752
John McCalla0ce15c2011-04-24 08:23:24 +0000753/// Mangle everything prior to the base-unresolved-name in an unresolved-name.
754///
755/// \param firstQualifierLookup - the entity found by unqualified lookup
756/// for the first name in the qualifier, if this is for a member expression
757/// \param recursive - true if this is being called recursively,
758/// i.e. if there is more prefix "to the right".
759void CXXNameMangler::mangleUnresolvedPrefix(NestedNameSpecifier *qualifier,
760 NamedDecl *firstQualifierLookup,
761 bool recursive) {
John McCall1dd73832010-02-04 01:42:13 +0000762
John McCalla0ce15c2011-04-24 08:23:24 +0000763 // x, ::x
764 // <unresolved-name> ::= [gs] <base-unresolved-name>
765
766 // T::x / decltype(p)::x
767 // <unresolved-name> ::= sr <unresolved-type> <base-unresolved-name>
768
769 // T::N::x /decltype(p)::N::x
770 // <unresolved-name> ::= srN <unresolved-type> <unresolved-qualifier-level>+ E
771 // <base-unresolved-name>
772
773 // A::x, N::y, A<T>::z; "gs" means leading "::"
774 // <unresolved-name> ::= [gs] sr <unresolved-qualifier-level>+ E
775 // <base-unresolved-name>
776
777 switch (qualifier->getKind()) {
778 case NestedNameSpecifier::Global:
779 Out << "gs";
780
781 // We want an 'sr' unless this is the entire NNS.
782 if (recursive)
783 Out << "sr";
784
785 // We never want an 'E' here.
786 return;
787
788 case NestedNameSpecifier::Namespace:
789 if (qualifier->getPrefix())
790 mangleUnresolvedPrefix(qualifier->getPrefix(), firstQualifierLookup,
791 /*recursive*/ true);
792 else
793 Out << "sr";
794 mangleSourceName(qualifier->getAsNamespace()->getIdentifier());
795 break;
796 case NestedNameSpecifier::NamespaceAlias:
797 if (qualifier->getPrefix())
798 mangleUnresolvedPrefix(qualifier->getPrefix(), firstQualifierLookup,
799 /*recursive*/ true);
800 else
801 Out << "sr";
802 mangleSourceName(qualifier->getAsNamespaceAlias()->getIdentifier());
803 break;
804
805 case NestedNameSpecifier::TypeSpec:
806 case NestedNameSpecifier::TypeSpecWithTemplate: {
John McCall4f4e4132011-05-04 01:45:19 +0000807 const Type *type = qualifier->getAsType();
John McCalla0ce15c2011-04-24 08:23:24 +0000808
John McCall4f4e4132011-05-04 01:45:19 +0000809 // We only want to use an unresolved-type encoding if this is one of:
810 // - a decltype
811 // - a template type parameter
812 // - a template template parameter with arguments
813 // In all of these cases, we should have no prefix.
814 if (qualifier->getPrefix()) {
815 mangleUnresolvedPrefix(qualifier->getPrefix(), firstQualifierLookup,
816 /*recursive*/ true);
817 } else {
818 // Otherwise, all the cases want this.
819 Out << "sr";
John McCall4f4e4132011-05-04 01:45:19 +0000820 }
821
John McCall4f4e4132011-05-04 01:45:19 +0000822 // Only certain other types are valid as prefixes; enumerate them.
John McCalld3d49bb2011-06-28 16:49:23 +0000823 switch (type->getTypeClass()) {
824 case Type::Builtin:
825 case Type::Complex:
826 case Type::Pointer:
827 case Type::BlockPointer:
828 case Type::LValueReference:
829 case Type::RValueReference:
830 case Type::MemberPointer:
831 case Type::ConstantArray:
832 case Type::IncompleteArray:
833 case Type::VariableArray:
834 case Type::DependentSizedArray:
835 case Type::DependentSizedExtVector:
836 case Type::Vector:
837 case Type::ExtVector:
838 case Type::FunctionProto:
839 case Type::FunctionNoProto:
840 case Type::Enum:
841 case Type::Paren:
842 case Type::Elaborated:
843 case Type::Attributed:
844 case Type::Auto:
845 case Type::PackExpansion:
John McCalld3d49bb2011-06-28 16:49:23 +0000846 case Type::ObjCObject:
847 case Type::ObjCInterface:
848 case Type::ObjCObjectPointer:
Eli Friedmanb001de72011-10-06 23:00:33 +0000849 case Type::Atomic:
John McCalld3d49bb2011-06-28 16:49:23 +0000850 llvm_unreachable("type is illegal as a nested name specifier");
851
John McCall68a51a72011-07-01 00:04:39 +0000852 case Type::SubstTemplateTypeParmPack:
853 // FIXME: not clear how to mangle this!
854 // template <class T...> class A {
855 // template <class U...> void foo(decltype(T::foo(U())) x...);
856 // };
857 Out << "_SUBSTPACK_";
858 break;
859
John McCalld3d49bb2011-06-28 16:49:23 +0000860 // <unresolved-type> ::= <template-param>
861 // ::= <decltype>
862 // ::= <template-template-param> <template-args>
863 // (this last is not official yet)
864 case Type::TypeOfExpr:
865 case Type::TypeOf:
866 case Type::Decltype:
867 case Type::TemplateTypeParm:
868 case Type::UnaryTransform:
John McCall35ee32e2011-07-01 02:19:08 +0000869 case Type::SubstTemplateTypeParm:
John McCalld3d49bb2011-06-28 16:49:23 +0000870 unresolvedType:
871 assert(!qualifier->getPrefix());
872
873 // We only get here recursively if we're followed by identifiers.
874 if (recursive) Out << 'N';
875
John McCall35ee32e2011-07-01 02:19:08 +0000876 // This seems to do everything we want. It's not really
877 // sanctioned for a substituted template parameter, though.
John McCalld3d49bb2011-06-28 16:49:23 +0000878 mangleType(QualType(type, 0));
879
880 // We never want to print 'E' directly after an unresolved-type,
881 // so we return directly.
882 return;
883
John McCalld3d49bb2011-06-28 16:49:23 +0000884 case Type::Typedef:
885 mangleSourceName(cast<TypedefType>(type)->getDecl()->getIdentifier());
886 break;
887
888 case Type::UnresolvedUsing:
889 mangleSourceName(cast<UnresolvedUsingType>(type)->getDecl()
890 ->getIdentifier());
891 break;
892
893 case Type::Record:
894 mangleSourceName(cast<RecordType>(type)->getDecl()->getIdentifier());
895 break;
896
897 case Type::TemplateSpecialization: {
898 const TemplateSpecializationType *tst
899 = cast<TemplateSpecializationType>(type);
John McCall68a51a72011-07-01 00:04:39 +0000900 TemplateName name = tst->getTemplateName();
901 switch (name.getKind()) {
902 case TemplateName::Template:
903 case TemplateName::QualifiedTemplate: {
904 TemplateDecl *temp = name.getAsTemplateDecl();
John McCalld3d49bb2011-06-28 16:49:23 +0000905
John McCall68a51a72011-07-01 00:04:39 +0000906 // If the base is a template template parameter, this is an
907 // unresolved type.
908 assert(temp && "no template for template specialization type");
909 if (isa<TemplateTemplateParmDecl>(temp)) goto unresolvedType;
John McCalld3d49bb2011-06-28 16:49:23 +0000910
John McCall68a51a72011-07-01 00:04:39 +0000911 mangleSourceName(temp->getIdentifier());
912 break;
913 }
914
915 case TemplateName::OverloadedTemplate:
916 case TemplateName::DependentTemplate:
917 llvm_unreachable("invalid base for a template specialization type");
918
919 case TemplateName::SubstTemplateTemplateParm: {
920 SubstTemplateTemplateParmStorage *subst
921 = name.getAsSubstTemplateTemplateParm();
922 mangleExistingSubstitution(subst->getReplacement());
923 break;
924 }
925
926 case TemplateName::SubstTemplateTemplateParmPack: {
927 // FIXME: not clear how to mangle this!
928 // template <template <class U> class T...> class A {
929 // template <class U...> void foo(decltype(T<U>::foo) x...);
930 // };
931 Out << "_SUBSTPACK_";
932 break;
933 }
934 }
935
Eli Friedmand7a6b162012-09-26 02:36:12 +0000936 mangleTemplateArgs(tst->getArgs(), tst->getNumArgs());
John McCalld3d49bb2011-06-28 16:49:23 +0000937 break;
938 }
939
940 case Type::InjectedClassName:
941 mangleSourceName(cast<InjectedClassNameType>(type)->getDecl()
942 ->getIdentifier());
943 break;
944
945 case Type::DependentName:
946 mangleSourceName(cast<DependentNameType>(type)->getIdentifier());
947 break;
948
949 case Type::DependentTemplateSpecialization: {
950 const DependentTemplateSpecializationType *tst
951 = cast<DependentTemplateSpecializationType>(type);
John McCall4f4e4132011-05-04 01:45:19 +0000952 mangleSourceName(tst->getIdentifier());
Eli Friedmand7a6b162012-09-26 02:36:12 +0000953 mangleTemplateArgs(tst->getArgs(), tst->getNumArgs());
John McCalld3d49bb2011-06-28 16:49:23 +0000954 break;
955 }
John McCall4f4e4132011-05-04 01:45:19 +0000956 }
957 break;
John McCalla0ce15c2011-04-24 08:23:24 +0000958 }
959
960 case NestedNameSpecifier::Identifier:
961 // Member expressions can have these without prefixes.
962 if (qualifier->getPrefix()) {
963 mangleUnresolvedPrefix(qualifier->getPrefix(), firstQualifierLookup,
964 /*recursive*/ true);
965 } else if (firstQualifierLookup) {
966
967 // Try to make a proper qualifier out of the lookup result, and
968 // then just recurse on that.
969 NestedNameSpecifier *newQualifier;
970 if (TypeDecl *typeDecl = dyn_cast<TypeDecl>(firstQualifierLookup)) {
971 QualType type = getASTContext().getTypeDeclType(typeDecl);
972
973 // Pretend we had a different nested name specifier.
974 newQualifier = NestedNameSpecifier::Create(getASTContext(),
975 /*prefix*/ 0,
976 /*template*/ false,
977 type.getTypePtr());
978 } else if (NamespaceDecl *nspace =
979 dyn_cast<NamespaceDecl>(firstQualifierLookup)) {
980 newQualifier = NestedNameSpecifier::Create(getASTContext(),
981 /*prefix*/ 0,
982 nspace);
983 } else if (NamespaceAliasDecl *alias =
984 dyn_cast<NamespaceAliasDecl>(firstQualifierLookup)) {
985 newQualifier = NestedNameSpecifier::Create(getASTContext(),
986 /*prefix*/ 0,
987 alias);
988 } else {
989 // No sensible mangling to do here.
990 newQualifier = 0;
991 }
992
993 if (newQualifier)
994 return mangleUnresolvedPrefix(newQualifier, /*lookup*/ 0, recursive);
995
996 } else {
997 Out << "sr";
998 }
999
1000 mangleSourceName(qualifier->getAsIdentifier());
1001 break;
1002 }
1003
1004 // If this was the innermost part of the NNS, and we fell out to
1005 // here, append an 'E'.
1006 if (!recursive)
1007 Out << 'E';
1008}
1009
1010/// Mangle an unresolved-name, which is generally used for names which
1011/// weren't resolved to specific entities.
1012void CXXNameMangler::mangleUnresolvedName(NestedNameSpecifier *qualifier,
1013 NamedDecl *firstQualifierLookup,
1014 DeclarationName name,
1015 unsigned knownArity) {
1016 if (qualifier) mangleUnresolvedPrefix(qualifier, firstQualifierLookup);
1017 mangleUnqualifiedName(0, name, knownArity);
John McCall1dd73832010-02-04 01:42:13 +00001018}
1019
Anders Carlsson6f7e2f42010-06-08 14:49:03 +00001020static const FieldDecl *FindFirstNamedDataMember(const RecordDecl *RD) {
1021 assert(RD->isAnonymousStructOrUnion() &&
1022 "Expected anonymous struct or union!");
1023
1024 for (RecordDecl::field_iterator I = RD->field_begin(), E = RD->field_end();
1025 I != E; ++I) {
David Blaikie581deb32012-06-06 20:45:41 +00001026 if (I->getIdentifier())
1027 return *I;
Anders Carlsson6f7e2f42010-06-08 14:49:03 +00001028
David Blaikie581deb32012-06-06 20:45:41 +00001029 if (const RecordType *RT = I->getType()->getAs<RecordType>())
Anders Carlsson6f7e2f42010-06-08 14:49:03 +00001030 if (const FieldDecl *NamedDataMember =
1031 FindFirstNamedDataMember(RT->getDecl()))
1032 return NamedDataMember;
1033 }
Anders Carlsson6f7e2f42010-06-08 14:49:03 +00001034
1035 // We didn't find a named data member.
1036 return 0;
1037}
1038
John McCall1dd73832010-02-04 01:42:13 +00001039void CXXNameMangler::mangleUnqualifiedName(const NamedDecl *ND,
1040 DeclarationName Name,
1041 unsigned KnownArity) {
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001042 // <unqualified-name> ::= <operator-name>
Mike Stump1eb44332009-09-09 15:08:12 +00001043 // ::= <ctor-dtor-name>
1044 // ::= <source-name>
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001045 switch (Name.getNameKind()) {
Anders Carlssonc4355b62009-10-07 01:45:02 +00001046 case DeclarationName::Identifier: {
Anders Carlssonc4355b62009-10-07 01:45:02 +00001047 if (const IdentifierInfo *II = Name.getAsIdentifierInfo()) {
Sean Hunt31455252010-01-24 03:04:27 +00001048 // We must avoid conflicts between internally- and externally-
John McCall74990f42011-03-22 06:34:45 +00001049 // linked variable and function declaration names in the same TU:
1050 // void test() { extern void foo(); }
1051 // static void foo();
1052 // This naming convention is the same as that followed by GCC,
1053 // though it shouldn't actually matter.
1054 if (ND && ND->getLinkage() == InternalLinkage &&
Douglas Gregorccc1b5e2012-02-21 00:37:24 +00001055 getEffectiveDeclContext(ND)->isFileContext())
Sean Hunt31455252010-01-24 03:04:27 +00001056 Out << 'L';
1057
Anders Carlssonc4355b62009-10-07 01:45:02 +00001058 mangleSourceName(II);
1059 break;
1060 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00001061
John McCall1dd73832010-02-04 01:42:13 +00001062 // Otherwise, an anonymous entity. We must have a declaration.
1063 assert(ND && "mangling empty name without declaration");
1064
1065 if (const NamespaceDecl *NS = dyn_cast<NamespaceDecl>(ND)) {
1066 if (NS->isAnonymousNamespace()) {
1067 // This is how gcc mangles these names.
1068 Out << "12_GLOBAL__N_1";
1069 break;
1070 }
1071 }
1072
Anders Carlsson6f7e2f42010-06-08 14:49:03 +00001073 if (const VarDecl *VD = dyn_cast<VarDecl>(ND)) {
1074 // We must have an anonymous union or struct declaration.
1075 const RecordDecl *RD =
1076 cast<RecordDecl>(VD->getType()->getAs<RecordType>()->getDecl());
1077
1078 // Itanium C++ ABI 5.1.2:
1079 //
1080 // For the purposes of mangling, the name of an anonymous union is
1081 // considered to be the name of the first named data member found by a
1082 // pre-order, depth-first, declaration-order walk of the data members of
1083 // the anonymous union. If there is no such data member (i.e., if all of
1084 // the data members in the union are unnamed), then there is no way for
1085 // a program to refer to the anonymous union, and there is therefore no
1086 // need to mangle its name.
1087 const FieldDecl *FD = FindFirstNamedDataMember(RD);
John McCall7121c8f2010-08-05 22:02:13 +00001088
1089 // It's actually possible for various reasons for us to get here
1090 // with an empty anonymous struct / union. Fortunately, it
1091 // doesn't really matter what name we generate.
1092 if (!FD) break;
Anders Carlsson6f7e2f42010-06-08 14:49:03 +00001093 assert(FD->getIdentifier() && "Data member name isn't an identifier!");
1094
1095 mangleSourceName(FD->getIdentifier());
1096 break;
1097 }
1098
Anders Carlssonc4355b62009-10-07 01:45:02 +00001099 // We must have an anonymous struct.
1100 const TagDecl *TD = cast<TagDecl>(ND);
Richard Smith162e1c12011-04-15 14:24:37 +00001101 if (const TypedefNameDecl *D = TD->getTypedefNameForAnonDecl()) {
Anders Carlssonc4355b62009-10-07 01:45:02 +00001102 assert(TD->getDeclContext() == D->getDeclContext() &&
1103 "Typedef should not be in another decl context!");
1104 assert(D->getDeclName().getAsIdentifierInfo() &&
1105 "Typedef was not named!");
1106 mangleSourceName(D->getDeclName().getAsIdentifierInfo());
1107 break;
1108 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00001109
Douglas Gregor9e8c92a2012-02-20 19:44:39 +00001110 // <unnamed-type-name> ::= <closure-type-name>
1111 //
1112 // <closure-type-name> ::= Ul <lambda-sig> E [ <nonnegative number> ] _
1113 // <lambda-sig> ::= <parameter-type>+ # Parameter types or 'v' for 'void'.
1114 if (const CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(TD)) {
Douglas Gregor5878cbc2012-02-21 04:17:39 +00001115 if (Record->isLambda() && Record->getLambdaManglingNumber()) {
Douglas Gregorccc1b5e2012-02-21 00:37:24 +00001116 mangleLambda(Record);
Douglas Gregor9e8c92a2012-02-20 19:44:39 +00001117 break;
1118 }
1119 }
1120
Anders Carlssonc4355b62009-10-07 01:45:02 +00001121 // Get a unique id for the anonymous struct.
1122 uint64_t AnonStructId = Context.getAnonymousStructId(TD);
1123
1124 // Mangle it as a source name in the form
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00001125 // [n] $_<id>
Anders Carlssonc4355b62009-10-07 01:45:02 +00001126 // where n is the length of the string.
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00001127 SmallString<8> Str;
Anders Carlssonc4355b62009-10-07 01:45:02 +00001128 Str += "$_";
1129 Str += llvm::utostr(AnonStructId);
1130
1131 Out << Str.size();
1132 Out << Str.str();
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001133 break;
Anders Carlssonc4355b62009-10-07 01:45:02 +00001134 }
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001135
1136 case DeclarationName::ObjCZeroArgSelector:
1137 case DeclarationName::ObjCOneArgSelector:
1138 case DeclarationName::ObjCMultiArgSelector:
David Blaikieb219cfc2011-09-23 05:06:16 +00001139 llvm_unreachable("Can't mangle Objective-C selector names here!");
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001140
1141 case DeclarationName::CXXConstructorName:
Anders Carlsson27ae5362009-04-17 01:58:57 +00001142 if (ND == Structor)
Mike Stump141c5af2009-09-02 00:25:38 +00001143 // If the named decl is the C++ constructor we're mangling, use the type
1144 // we were given.
Anders Carlsson27ae5362009-04-17 01:58:57 +00001145 mangleCXXCtorType(static_cast<CXXCtorType>(StructorType));
Anders Carlsson3ac86b52009-04-15 05:36:58 +00001146 else
1147 // Otherwise, use the complete constructor name. This is relevant if a
1148 // class with a constructor is declared within a constructor.
1149 mangleCXXCtorType(Ctor_Complete);
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001150 break;
1151
1152 case DeclarationName::CXXDestructorName:
Anders Carlsson27ae5362009-04-17 01:58:57 +00001153 if (ND == Structor)
Mike Stump141c5af2009-09-02 00:25:38 +00001154 // If the named decl is the C++ destructor we're mangling, use the type we
1155 // were given.
Anders Carlsson27ae5362009-04-17 01:58:57 +00001156 mangleCXXDtorType(static_cast<CXXDtorType>(StructorType));
1157 else
1158 // Otherwise, use the complete destructor name. This is relevant if a
1159 // class with a destructor is declared within a destructor.
1160 mangleCXXDtorType(Dtor_Complete);
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001161 break;
1162
1163 case DeclarationName::CXXConversionFunctionName:
Mike Stump1eb44332009-09-09 15:08:12 +00001164 // <operator-name> ::= cv <type> # (cast)
Douglas Gregor219cc612009-02-13 01:28:03 +00001165 Out << "cv";
Douglas Gregor79e6bd32011-07-12 04:42:08 +00001166 mangleType(Name.getCXXNameType());
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001167 break;
1168
Anders Carlsson8257d412009-12-22 06:36:32 +00001169 case DeclarationName::CXXOperatorName: {
John McCall1dd73832010-02-04 01:42:13 +00001170 unsigned Arity;
1171 if (ND) {
1172 Arity = cast<FunctionDecl>(ND)->getNumParams();
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001173
John McCall1dd73832010-02-04 01:42:13 +00001174 // If we have a C++ member function, we need to include the 'this' pointer.
1175 // FIXME: This does not make sense for operators that are static, but their
1176 // names stay the same regardless of the arity (operator new for instance).
1177 if (isa<CXXMethodDecl>(ND))
1178 Arity++;
1179 } else
1180 Arity = KnownArity;
1181
Anders Carlsson8257d412009-12-22 06:36:32 +00001182 mangleOperatorName(Name.getCXXOverloadedOperator(), Arity);
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001183 break;
Anders Carlsson8257d412009-12-22 06:36:32 +00001184 }
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001185
Sean Hunt3e518bd2009-11-29 07:34:05 +00001186 case DeclarationName::CXXLiteralOperatorName:
Sean Hunt5dd6b392009-12-04 21:11:13 +00001187 // FIXME: This mangling is not yet official.
Sean Hunt2421f662009-12-04 21:01:37 +00001188 Out << "li";
Sean Hunt3e518bd2009-11-29 07:34:05 +00001189 mangleSourceName(Name.getCXXLiteralIdentifier());
1190 break;
1191
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001192 case DeclarationName::CXXUsingDirective:
David Blaikieb219cfc2011-09-23 05:06:16 +00001193 llvm_unreachable("Can't mangle a using directive name!");
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001194 }
1195}
1196
1197void CXXNameMangler::mangleSourceName(const IdentifierInfo *II) {
1198 // <source-name> ::= <positive length number> <identifier>
1199 // <number> ::= [n] <non-negative decimal integer>
1200 // <identifier> ::= <unqualified source code identifier>
1201 Out << II->getLength() << II->getName();
1202}
1203
Eli Friedman7facf842009-12-02 20:32:49 +00001204void CXXNameMangler::mangleNestedName(const NamedDecl *ND,
Fariborz Jahanian57058532010-03-03 19:41:08 +00001205 const DeclContext *DC,
1206 bool NoFunction) {
Douglas Gregor0a9a6d62011-01-26 17:36:28 +00001207 // <nested-name>
1208 // ::= N [<CV-qualifiers>] [<ref-qualifier>] <prefix> <unqualified-name> E
1209 // ::= N [<CV-qualifiers>] [<ref-qualifier>] <template-prefix>
1210 // <template-args> E
Anders Carlssond99edc42009-09-26 03:55:37 +00001211
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001212 Out << 'N';
Douglas Gregor0a9a6d62011-01-26 17:36:28 +00001213 if (const CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(ND)) {
John McCall0953e762009-09-24 19:53:00 +00001214 mangleQualifiers(Qualifiers::fromCVRMask(Method->getTypeQualifiers()));
Douglas Gregor0a9a6d62011-01-26 17:36:28 +00001215 mangleRefQualifier(Method->getRefQualifier());
1216 }
1217
Anders Carlsson2744a062009-09-18 19:00:18 +00001218 // Check if we have a template.
1219 const TemplateArgumentList *TemplateArgs = 0;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00001220 if (const TemplateDecl *TD = isTemplate(ND, TemplateArgs)) {
Anders Carlsson2744a062009-09-18 19:00:18 +00001221 mangleTemplatePrefix(TD);
Eli Friedmand7a6b162012-09-26 02:36:12 +00001222 mangleTemplateArgs(*TemplateArgs);
Fariborz Jahanian57058532010-03-03 19:41:08 +00001223 }
1224 else {
1225 manglePrefix(DC, NoFunction);
Anders Carlsson7482e242009-09-18 04:29:09 +00001226 mangleUnqualifiedName(ND);
1227 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00001228
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001229 Out << 'E';
1230}
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00001231void CXXNameMangler::mangleNestedName(const TemplateDecl *TD,
Anders Carlsson7624f212009-09-18 02:42:01 +00001232 const TemplateArgument *TemplateArgs,
1233 unsigned NumTemplateArgs) {
Anders Carlssone45117b2009-09-27 19:53:49 +00001234 // <nested-name> ::= N [<CV-qualifiers>] <template-prefix> <template-args> E
1235
Anders Carlsson7624f212009-09-18 02:42:01 +00001236 Out << 'N';
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00001237
Anders Carlssone45117b2009-09-27 19:53:49 +00001238 mangleTemplatePrefix(TD);
Eli Friedmand7a6b162012-09-26 02:36:12 +00001239 mangleTemplateArgs(TemplateArgs, NumTemplateArgs);
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00001240
Anders Carlsson7624f212009-09-18 02:42:01 +00001241 Out << 'E';
1242}
1243
Anders Carlsson1b42c792009-04-02 16:24:45 +00001244void CXXNameMangler::mangleLocalName(const NamedDecl *ND) {
1245 // <local-name> := Z <function encoding> E <entity name> [<discriminator>]
1246 // := Z <function encoding> E s [<discriminator>]
Douglas Gregorccc1b5e2012-02-21 00:37:24 +00001247 // <local-name> := Z <function encoding> E d [ <parameter number> ]
1248 // _ <entity name>
Mike Stump1eb44332009-09-09 15:08:12 +00001249 // <discriminator> := _ <non-negative number>
Douglas Gregorccc1b5e2012-02-21 00:37:24 +00001250 const DeclContext *DC = getEffectiveDeclContext(ND);
Fariborz Jahanian8805fe82011-06-09 19:25:01 +00001251 if (isa<ObjCMethodDecl>(DC) && isa<FunctionDecl>(ND)) {
1252 // Don't add objc method name mangling to locally declared function
1253 mangleUnqualifiedName(ND);
1254 return;
1255 }
1256
Anders Carlsson1b42c792009-04-02 16:24:45 +00001257 Out << 'Z';
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001258
Charles Davis685b1d92010-05-26 18:25:27 +00001259 if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(DC)) {
1260 mangleObjCMethodName(MD);
John McCall82b7d7b2010-10-18 21:28:44 +00001261 } else if (const CXXRecordDecl *RD = GetLocalClassDecl(ND)) {
Douglas Gregorccc1b5e2012-02-21 00:37:24 +00001262 mangleFunctionEncoding(cast<FunctionDecl>(getEffectiveDeclContext(RD)));
Fariborz Jahanian57058532010-03-03 19:41:08 +00001263 Out << 'E';
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001264
Douglas Gregorccc1b5e2012-02-21 00:37:24 +00001265 // The parameter number is omitted for the last parameter, 0 for the
1266 // second-to-last parameter, 1 for the third-to-last parameter, etc. The
1267 // <entity name> will of course contain a <closure-type-name>: Its
1268 // numbering will be local to the particular argument in which it appears
1269 // -- other default arguments do not affect its encoding.
1270 bool SkipDiscriminator = false;
1271 if (RD->isLambda()) {
1272 if (const ParmVarDecl *Parm
1273 = dyn_cast_or_null<ParmVarDecl>(RD->getLambdaContextDecl())) {
1274 if (const FunctionDecl *Func
1275 = dyn_cast<FunctionDecl>(Parm->getDeclContext())) {
1276 Out << 'd';
1277 unsigned Num = Func->getNumParams() - Parm->getFunctionScopeIndex();
1278 if (Num > 1)
1279 mangleNumber(Num - 2);
1280 Out << '_';
1281 SkipDiscriminator = true;
1282 }
1283 }
1284 }
1285
John McCall82b7d7b2010-10-18 21:28:44 +00001286 // Mangle the name relative to the closest enclosing function.
1287 if (ND == RD) // equality ok because RD derived from ND above
1288 mangleUnqualifiedName(ND);
1289 else
1290 mangleNestedName(ND, DC, true /*NoFunction*/);
1291
Douglas Gregorccc1b5e2012-02-21 00:37:24 +00001292 if (!SkipDiscriminator) {
1293 unsigned disc;
1294 if (Context.getNextDiscriminator(RD, disc)) {
1295 if (disc < 10)
1296 Out << '_' << disc;
1297 else
1298 Out << "__" << disc << '_';
1299 }
Fariborz Jahanian4819ac42010-03-04 01:02:03 +00001300 }
Douglas Gregorccc1b5e2012-02-21 00:37:24 +00001301
Fariborz Jahanian57058532010-03-03 19:41:08 +00001302 return;
1303 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001304 else
Fariborz Jahanian57058532010-03-03 19:41:08 +00001305 mangleFunctionEncoding(cast<FunctionDecl>(DC));
Anders Carlsson7b06f6c2009-12-10 03:14:39 +00001306
Anders Carlsson1b42c792009-04-02 16:24:45 +00001307 Out << 'E';
Eli Friedman6f9f25d2009-12-11 20:21:38 +00001308 mangleUnqualifiedName(ND);
Anders Carlsson1b42c792009-04-02 16:24:45 +00001309}
1310
Douglas Gregorccc1b5e2012-02-21 00:37:24 +00001311void CXXNameMangler::mangleLambda(const CXXRecordDecl *Lambda) {
Douglas Gregor552e2992012-02-21 02:22:07 +00001312 // If the context of a closure type is an initializer for a class member
1313 // (static or nonstatic), it is encoded in a qualified name with a final
1314 // <prefix> of the form:
1315 //
1316 // <data-member-prefix> := <member source-name> M
1317 //
1318 // Technically, the data-member-prefix is part of the <prefix>. However,
1319 // since a closure type will always be mangled with a prefix, it's easier
1320 // to emit that last part of the prefix here.
1321 if (Decl *Context = Lambda->getLambdaContextDecl()) {
1322 if ((isa<VarDecl>(Context) || isa<FieldDecl>(Context)) &&
1323 Context->getDeclContext()->isRecord()) {
1324 if (const IdentifierInfo *Name
1325 = cast<NamedDecl>(Context)->getIdentifier()) {
1326 mangleSourceName(Name);
1327 Out << 'M';
1328 }
1329 }
1330 }
1331
Douglas Gregorccc1b5e2012-02-21 00:37:24 +00001332 Out << "Ul";
Eli Friedman8da8a662012-09-19 01:18:11 +00001333 const FunctionProtoType *Proto = Lambda->getLambdaTypeInfo()->getType()->
1334 getAs<FunctionProtoType>();
Douglas Gregorccc1b5e2012-02-21 00:37:24 +00001335 mangleBareFunctionType(Proto, /*MangleReturnType=*/false);
1336 Out << "E";
1337
1338 // The number is omitted for the first closure type with a given
1339 // <lambda-sig> in a given context; it is n-2 for the nth closure type
1340 // (in lexical order) with that same <lambda-sig> and context.
1341 //
1342 // The AST keeps track of the number for us.
Douglas Gregor5878cbc2012-02-21 04:17:39 +00001343 unsigned Number = Lambda->getLambdaManglingNumber();
1344 assert(Number > 0 && "Lambda should be mangled as an unnamed class");
1345 if (Number > 1)
1346 mangleNumber(Number - 2);
Douglas Gregorccc1b5e2012-02-21 00:37:24 +00001347 Out << '_';
1348}
1349
John McCalla0ce15c2011-04-24 08:23:24 +00001350void CXXNameMangler::manglePrefix(NestedNameSpecifier *qualifier) {
1351 switch (qualifier->getKind()) {
1352 case NestedNameSpecifier::Global:
1353 // nothing
1354 return;
1355
1356 case NestedNameSpecifier::Namespace:
1357 mangleName(qualifier->getAsNamespace());
1358 return;
1359
1360 case NestedNameSpecifier::NamespaceAlias:
1361 mangleName(qualifier->getAsNamespaceAlias()->getNamespace());
1362 return;
1363
1364 case NestedNameSpecifier::TypeSpec:
1365 case NestedNameSpecifier::TypeSpecWithTemplate:
John McCall4f4e4132011-05-04 01:45:19 +00001366 manglePrefix(QualType(qualifier->getAsType(), 0));
John McCalla0ce15c2011-04-24 08:23:24 +00001367 return;
1368
1369 case NestedNameSpecifier::Identifier:
1370 // Member expressions can have these without prefixes, but that
1371 // should end up in mangleUnresolvedPrefix instead.
1372 assert(qualifier->getPrefix());
1373 manglePrefix(qualifier->getPrefix());
1374
1375 mangleSourceName(qualifier->getAsIdentifier());
1376 return;
1377 }
1378
1379 llvm_unreachable("unexpected nested name specifier");
1380}
1381
Fariborz Jahanian57058532010-03-03 19:41:08 +00001382void CXXNameMangler::manglePrefix(const DeclContext *DC, bool NoFunction) {
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001383 // <prefix> ::= <prefix> <unqualified-name>
1384 // ::= <template-prefix> <template-args>
1385 // ::= <template-param>
1386 // ::= # empty
1387 // ::= <substitution>
Anders Carlsson6862fc72009-09-17 04:16:28 +00001388
James Molloyb3c312c2012-03-05 09:59:43 +00001389 DC = IgnoreLinkageSpecDecls(DC);
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00001390
Anders Carlsson9263e912009-09-18 18:39:58 +00001391 if (DC->isTranslationUnit())
1392 return;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00001393
Douglas Gregor35415f52010-05-25 17:04:15 +00001394 if (const BlockDecl *Block = dyn_cast<BlockDecl>(DC)) {
Douglas Gregorccc1b5e2012-02-21 00:37:24 +00001395 manglePrefix(getEffectiveParentContext(DC), NoFunction);
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00001396 SmallString<64> Name;
Rafael Espindolac4850c22011-02-10 23:59:36 +00001397 llvm::raw_svector_ostream NameStream(Name);
1398 Context.mangleBlock(Block, NameStream);
1399 NameStream.flush();
Douglas Gregor35415f52010-05-25 17:04:15 +00001400 Out << Name.size() << Name;
1401 return;
1402 }
1403
Douglas Gregor552e2992012-02-21 02:22:07 +00001404 const NamedDecl *ND = cast<NamedDecl>(DC);
1405 if (mangleSubstitution(ND))
Anders Carlsson6862fc72009-09-17 04:16:28 +00001406 return;
Douglas Gregor552e2992012-02-21 02:22:07 +00001407
Anders Carlsson2ee3fca2009-09-18 20:11:09 +00001408 // Check if we have a template.
1409 const TemplateArgumentList *TemplateArgs = 0;
Douglas Gregor552e2992012-02-21 02:22:07 +00001410 if (const TemplateDecl *TD = isTemplate(ND, TemplateArgs)) {
Anders Carlsson2ee3fca2009-09-18 20:11:09 +00001411 mangleTemplatePrefix(TD);
Eli Friedmand7a6b162012-09-26 02:36:12 +00001412 mangleTemplateArgs(*TemplateArgs);
Fariborz Jahanian57058532010-03-03 19:41:08 +00001413 }
Douglas Gregor552e2992012-02-21 02:22:07 +00001414 else if(NoFunction && (isa<FunctionDecl>(ND) || isa<ObjCMethodDecl>(ND)))
Fariborz Jahanian57058532010-03-03 19:41:08 +00001415 return;
Douglas Gregor552e2992012-02-21 02:22:07 +00001416 else if (const ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(ND))
Douglas Gregor35415f52010-05-25 17:04:15 +00001417 mangleObjCMethodName(Method);
Fariborz Jahanian57058532010-03-03 19:41:08 +00001418 else {
Douglas Gregor552e2992012-02-21 02:22:07 +00001419 manglePrefix(getEffectiveDeclContext(ND), NoFunction);
1420 mangleUnqualifiedName(ND);
Anders Carlsson2ee3fca2009-09-18 20:11:09 +00001421 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00001422
Douglas Gregor552e2992012-02-21 02:22:07 +00001423 addSubstitution(ND);
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001424}
1425
Douglas Gregor20f0cc72010-04-23 03:10:43 +00001426void CXXNameMangler::mangleTemplatePrefix(TemplateName Template) {
1427 // <template-prefix> ::= <prefix> <template unqualified-name>
1428 // ::= <template-param>
1429 // ::= <substitution>
1430 if (TemplateDecl *TD = Template.getAsTemplateDecl())
1431 return mangleTemplatePrefix(TD);
1432
1433 if (QualifiedTemplateName *Qualified = Template.getAsQualifiedTemplateName())
John McCalla0ce15c2011-04-24 08:23:24 +00001434 manglePrefix(Qualified->getQualifier());
Sean Huntc3021132010-05-05 15:23:54 +00001435
Douglas Gregor20f0cc72010-04-23 03:10:43 +00001436 if (OverloadedTemplateStorage *Overloaded
1437 = Template.getAsOverloadedTemplate()) {
Sean Huntc3021132010-05-05 15:23:54 +00001438 mangleUnqualifiedName(0, (*Overloaded->begin())->getDeclName(),
Douglas Gregor20f0cc72010-04-23 03:10:43 +00001439 UnknownArity);
1440 return;
1441 }
Sean Huntc3021132010-05-05 15:23:54 +00001442
Douglas Gregor20f0cc72010-04-23 03:10:43 +00001443 DependentTemplateName *Dependent = Template.getAsDependentTemplateName();
1444 assert(Dependent && "Unknown template name kind?");
John McCalla0ce15c2011-04-24 08:23:24 +00001445 manglePrefix(Dependent->getQualifier());
Douglas Gregor1e9268e2010-04-28 05:58:56 +00001446 mangleUnscopedTemplateName(Template);
Douglas Gregor20f0cc72010-04-23 03:10:43 +00001447}
1448
Anders Carlsson0fa6df42009-09-26 19:45:45 +00001449void CXXNameMangler::mangleTemplatePrefix(const TemplateDecl *ND) {
Anders Carlsson7482e242009-09-18 04:29:09 +00001450 // <template-prefix> ::= <prefix> <template unqualified-name>
1451 // ::= <template-param>
1452 // ::= <substitution>
Douglas Gregor32fb4e12010-02-05 20:45:00 +00001453 // <template-template-param> ::= <template-param>
1454 // <substitution>
Anders Carlsson7482e242009-09-18 04:29:09 +00001455
Anders Carlssonaeb85372009-09-26 22:18:22 +00001456 if (mangleSubstitution(ND))
1457 return;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00001458
Douglas Gregor32fb4e12010-02-05 20:45:00 +00001459 // <template-template-param> ::= <template-param>
1460 if (const TemplateTemplateParmDecl *TTP
1461 = dyn_cast<TemplateTemplateParmDecl>(ND)) {
1462 mangleTemplateParameter(TTP->getIndex());
1463 return;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001464 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00001465
Douglas Gregorccc1b5e2012-02-21 00:37:24 +00001466 manglePrefix(getEffectiveDeclContext(ND));
Anders Carlsson1668f202009-09-26 20:13:56 +00001467 mangleUnqualifiedName(ND->getTemplatedDecl());
Anders Carlssonaeb85372009-09-26 22:18:22 +00001468 addSubstitution(ND);
Anders Carlsson7482e242009-09-18 04:29:09 +00001469}
1470
John McCallb6f532e2010-07-14 06:43:17 +00001471/// Mangles a template name under the production <type>. Required for
1472/// template template arguments.
1473/// <type> ::= <class-enum-type>
1474/// ::= <template-param>
1475/// ::= <substitution>
1476void CXXNameMangler::mangleType(TemplateName TN) {
1477 if (mangleSubstitution(TN))
1478 return;
1479
1480 TemplateDecl *TD = 0;
1481
1482 switch (TN.getKind()) {
1483 case TemplateName::QualifiedTemplate:
1484 TD = TN.getAsQualifiedTemplateName()->getTemplateDecl();
1485 goto HaveDecl;
1486
1487 case TemplateName::Template:
1488 TD = TN.getAsTemplateDecl();
1489 goto HaveDecl;
1490
1491 HaveDecl:
1492 if (isa<TemplateTemplateParmDecl>(TD))
1493 mangleTemplateParameter(cast<TemplateTemplateParmDecl>(TD)->getIndex());
1494 else
1495 mangleName(TD);
1496 break;
1497
1498 case TemplateName::OverloadedTemplate:
1499 llvm_unreachable("can't mangle an overloaded template name as a <type>");
John McCallb6f532e2010-07-14 06:43:17 +00001500
1501 case TemplateName::DependentTemplate: {
1502 const DependentTemplateName *Dependent = TN.getAsDependentTemplateName();
1503 assert(Dependent->isIdentifier());
1504
1505 // <class-enum-type> ::= <name>
1506 // <name> ::= <nested-name>
John McCalla0ce15c2011-04-24 08:23:24 +00001507 mangleUnresolvedPrefix(Dependent->getQualifier(), 0);
John McCallb6f532e2010-07-14 06:43:17 +00001508 mangleSourceName(Dependent->getIdentifier());
1509 break;
1510 }
1511
John McCallb44e0cf2011-06-30 21:59:02 +00001512 case TemplateName::SubstTemplateTemplateParm: {
1513 // Substituted template parameters are mangled as the substituted
1514 // template. This will check for the substitution twice, which is
1515 // fine, but we have to return early so that we don't try to *add*
1516 // the substitution twice.
1517 SubstTemplateTemplateParmStorage *subst
1518 = TN.getAsSubstTemplateTemplateParm();
1519 mangleType(subst->getReplacement());
1520 return;
1521 }
John McCall14606042011-06-30 08:33:18 +00001522
Douglas Gregor1aee05d2011-01-15 06:45:20 +00001523 case TemplateName::SubstTemplateTemplateParmPack: {
John McCall68a51a72011-07-01 00:04:39 +00001524 // FIXME: not clear how to mangle this!
1525 // template <template <class> class T...> class A {
1526 // template <template <class> class U...> void foo(B<T,U> x...);
1527 // };
1528 Out << "_SUBSTPACK_";
Douglas Gregor1aee05d2011-01-15 06:45:20 +00001529 break;
1530 }
John McCallb6f532e2010-07-14 06:43:17 +00001531 }
1532
1533 addSubstitution(TN);
1534}
1535
Mike Stump1eb44332009-09-09 15:08:12 +00001536void
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001537CXXNameMangler::mangleOperatorName(OverloadedOperatorKind OO, unsigned Arity) {
1538 switch (OO) {
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001539 // <operator-name> ::= nw # new
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001540 case OO_New: Out << "nw"; break;
1541 // ::= na # new[]
1542 case OO_Array_New: Out << "na"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001543 // ::= dl # delete
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001544 case OO_Delete: Out << "dl"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001545 // ::= da # delete[]
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001546 case OO_Array_Delete: Out << "da"; break;
1547 // ::= ps # + (unary)
John McCall5e1e89b2010-08-18 19:18:59 +00001548 // ::= pl # + (binary or unknown)
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001549 case OO_Plus:
Anders Carlsson8257d412009-12-22 06:36:32 +00001550 Out << (Arity == 1? "ps" : "pl"); break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001551 // ::= ng # - (unary)
John McCall5e1e89b2010-08-18 19:18:59 +00001552 // ::= mi # - (binary or unknown)
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001553 case OO_Minus:
Anders Carlsson8257d412009-12-22 06:36:32 +00001554 Out << (Arity == 1? "ng" : "mi"); break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001555 // ::= ad # & (unary)
John McCall5e1e89b2010-08-18 19:18:59 +00001556 // ::= an # & (binary or unknown)
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001557 case OO_Amp:
Anders Carlsson8257d412009-12-22 06:36:32 +00001558 Out << (Arity == 1? "ad" : "an"); break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001559 // ::= de # * (unary)
John McCall5e1e89b2010-08-18 19:18:59 +00001560 // ::= ml # * (binary or unknown)
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001561 case OO_Star:
John McCall5e1e89b2010-08-18 19:18:59 +00001562 // Use binary when unknown.
Anders Carlsson8257d412009-12-22 06:36:32 +00001563 Out << (Arity == 1? "de" : "ml"); break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001564 // ::= co # ~
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001565 case OO_Tilde: Out << "co"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001566 // ::= dv # /
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001567 case OO_Slash: Out << "dv"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001568 // ::= rm # %
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001569 case OO_Percent: Out << "rm"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001570 // ::= or # |
1571 case OO_Pipe: Out << "or"; break;
1572 // ::= eo # ^
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001573 case OO_Caret: Out << "eo"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001574 // ::= aS # =
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001575 case OO_Equal: Out << "aS"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001576 // ::= pL # +=
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001577 case OO_PlusEqual: Out << "pL"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001578 // ::= mI # -=
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001579 case OO_MinusEqual: Out << "mI"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001580 // ::= mL # *=
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001581 case OO_StarEqual: Out << "mL"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001582 // ::= dV # /=
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001583 case OO_SlashEqual: Out << "dV"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001584 // ::= rM # %=
1585 case OO_PercentEqual: Out << "rM"; break;
1586 // ::= aN # &=
1587 case OO_AmpEqual: Out << "aN"; break;
1588 // ::= oR # |=
1589 case OO_PipeEqual: Out << "oR"; break;
1590 // ::= eO # ^=
1591 case OO_CaretEqual: Out << "eO"; break;
1592 // ::= ls # <<
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001593 case OO_LessLess: Out << "ls"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001594 // ::= rs # >>
1595 case OO_GreaterGreater: Out << "rs"; break;
1596 // ::= lS # <<=
1597 case OO_LessLessEqual: Out << "lS"; break;
1598 // ::= rS # >>=
1599 case OO_GreaterGreaterEqual: Out << "rS"; break;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001600 // ::= eq # ==
1601 case OO_EqualEqual: Out << "eq"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001602 // ::= ne # !=
1603 case OO_ExclaimEqual: Out << "ne"; break;
1604 // ::= lt # <
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001605 case OO_Less: Out << "lt"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001606 // ::= gt # >
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001607 case OO_Greater: Out << "gt"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001608 // ::= le # <=
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001609 case OO_LessEqual: Out << "le"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001610 // ::= ge # >=
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001611 case OO_GreaterEqual: Out << "ge"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001612 // ::= nt # !
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001613 case OO_Exclaim: Out << "nt"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001614 // ::= aa # &&
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001615 case OO_AmpAmp: Out << "aa"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001616 // ::= oo # ||
1617 case OO_PipePipe: Out << "oo"; break;
1618 // ::= pp # ++
1619 case OO_PlusPlus: Out << "pp"; break;
1620 // ::= mm # --
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001621 case OO_MinusMinus: Out << "mm"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001622 // ::= cm # ,
1623 case OO_Comma: Out << "cm"; break;
1624 // ::= pm # ->*
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001625 case OO_ArrowStar: Out << "pm"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001626 // ::= pt # ->
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001627 case OO_Arrow: Out << "pt"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001628 // ::= cl # ()
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001629 case OO_Call: Out << "cl"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001630 // ::= ix # []
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001631 case OO_Subscript: Out << "ix"; break;
Anders Carlssone170ba72009-12-14 01:45:37 +00001632
1633 // ::= qu # ?
1634 // The conditional operator can't be overloaded, but we still handle it when
1635 // mangling expressions.
1636 case OO_Conditional: Out << "qu"; break;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001637
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001638 case OO_None:
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001639 case NUM_OVERLOADED_OPERATORS:
David Blaikieb219cfc2011-09-23 05:06:16 +00001640 llvm_unreachable("Not an overloaded operator");
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001641 }
1642}
1643
John McCall0953e762009-09-24 19:53:00 +00001644void CXXNameMangler::mangleQualifiers(Qualifiers Quals) {
Mike Stump1eb44332009-09-09 15:08:12 +00001645 // <CV-qualifiers> ::= [r] [V] [K] # restrict (C99), volatile, const
John McCall0953e762009-09-24 19:53:00 +00001646 if (Quals.hasRestrict())
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001647 Out << 'r';
John McCall0953e762009-09-24 19:53:00 +00001648 if (Quals.hasVolatile())
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001649 Out << 'V';
John McCall0953e762009-09-24 19:53:00 +00001650 if (Quals.hasConst())
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001651 Out << 'K';
John McCall0953e762009-09-24 19:53:00 +00001652
Douglas Gregor56079f72010-06-14 23:15:08 +00001653 if (Quals.hasAddressSpace()) {
1654 // Extension:
1655 //
1656 // <type> ::= U <address-space-number>
1657 //
1658 // where <address-space-number> is a source name consisting of 'AS'
1659 // followed by the address space <number>.
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00001660 SmallString<64> ASString;
Douglas Gregor56079f72010-06-14 23:15:08 +00001661 ASString = "AS" + llvm::utostr_32(Quals.getAddressSpace());
1662 Out << 'U' << ASString.size() << ASString;
1663 }
1664
Chris Lattner5f9e2722011-07-23 10:55:15 +00001665 StringRef LifetimeName;
John McCallf85e1932011-06-15 23:02:42 +00001666 switch (Quals.getObjCLifetime()) {
1667 // Objective-C ARC Extension:
1668 //
1669 // <type> ::= U "__strong"
1670 // <type> ::= U "__weak"
1671 // <type> ::= U "__autoreleasing"
John McCallf85e1932011-06-15 23:02:42 +00001672 case Qualifiers::OCL_None:
1673 break;
1674
1675 case Qualifiers::OCL_Weak:
1676 LifetimeName = "__weak";
1677 break;
1678
1679 case Qualifiers::OCL_Strong:
1680 LifetimeName = "__strong";
1681 break;
1682
1683 case Qualifiers::OCL_Autoreleasing:
1684 LifetimeName = "__autoreleasing";
1685 break;
1686
1687 case Qualifiers::OCL_ExplicitNone:
Douglas Gregorc22d6992011-06-17 22:26:49 +00001688 // The __unsafe_unretained qualifier is *not* mangled, so that
1689 // __unsafe_unretained types in ARC produce the same manglings as the
1690 // equivalent (but, naturally, unqualified) types in non-ARC, providing
1691 // better ABI compatibility.
1692 //
1693 // It's safe to do this because unqualified 'id' won't show up
1694 // in any type signatures that need to be mangled.
John McCallf85e1932011-06-15 23:02:42 +00001695 break;
1696 }
1697 if (!LifetimeName.empty())
1698 Out << 'U' << LifetimeName.size() << LifetimeName;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001699}
1700
Douglas Gregor0a9a6d62011-01-26 17:36:28 +00001701void CXXNameMangler::mangleRefQualifier(RefQualifierKind RefQualifier) {
1702 // <ref-qualifier> ::= R # lvalue reference
1703 // ::= O # rvalue-reference
1704 // Proposal to Itanium C++ ABI list on 1/26/11
1705 switch (RefQualifier) {
1706 case RQ_None:
1707 break;
1708
1709 case RQ_LValue:
1710 Out << 'R';
1711 break;
1712
1713 case RQ_RValue:
1714 Out << 'O';
1715 break;
1716 }
1717}
1718
Anders Carlsson7b06f6c2009-12-10 03:14:39 +00001719void CXXNameMangler::mangleObjCMethodName(const ObjCMethodDecl *MD) {
Rafael Espindolaf0be9792011-02-11 02:52:17 +00001720 Context.mangleObjCMethodName(MD, Out);
Anders Carlsson7b06f6c2009-12-10 03:14:39 +00001721}
1722
Douglas Gregorf1588662011-07-12 15:18:55 +00001723void CXXNameMangler::mangleType(QualType T) {
1724 // If our type is instantiation-dependent but not dependent, we mangle
1725 // it as it was written in the source, removing any top-level sugar.
1726 // Otherwise, use the canonical type.
1727 //
1728 // FIXME: This is an approximation of the instantiation-dependent name
1729 // mangling rules, since we should really be using the type as written and
1730 // augmented via semantic analysis (i.e., with implicit conversions and
1731 // default template arguments) for any instantiation-dependent type.
1732 // Unfortunately, that requires several changes to our AST:
1733 // - Instantiation-dependent TemplateSpecializationTypes will need to be
1734 // uniqued, so that we can handle substitutions properly
1735 // - Default template arguments will need to be represented in the
1736 // TemplateSpecializationType, since they need to be mangled even though
1737 // they aren't written.
1738 // - Conversions on non-type template arguments need to be expressed, since
1739 // they can affect the mangling of sizeof/alignof.
1740 if (!T->isInstantiationDependentType() || T->isDependentType())
1741 T = T.getCanonicalType();
1742 else {
1743 // Desugar any types that are purely sugar.
1744 do {
1745 // Don't desugar through template specialization types that aren't
1746 // type aliases. We need to mangle the template arguments as written.
1747 if (const TemplateSpecializationType *TST
1748 = dyn_cast<TemplateSpecializationType>(T))
1749 if (!TST->isTypeAlias())
1750 break;
Anders Carlsson4843e582009-03-10 17:07:44 +00001751
Douglas Gregorf1588662011-07-12 15:18:55 +00001752 QualType Desugared
1753 = T.getSingleStepDesugaredType(Context.getASTContext());
1754 if (Desugared == T)
1755 break;
1756
1757 T = Desugared;
1758 } while (true);
1759 }
1760 SplitQualType split = T.split();
John McCall200fa532012-02-08 00:46:36 +00001761 Qualifiers quals = split.Quals;
1762 const Type *ty = split.Ty;
John McCallb47f7482011-01-26 20:05:40 +00001763
Douglas Gregorf1588662011-07-12 15:18:55 +00001764 bool isSubstitutable = quals || !isa<BuiltinType>(T);
1765 if (isSubstitutable && mangleSubstitution(T))
Anders Carlsson76967372009-09-17 00:43:46 +00001766 return;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001767
John McCallb47f7482011-01-26 20:05:40 +00001768 // If we're mangling a qualified array type, push the qualifiers to
1769 // the element type.
Douglas Gregorf1588662011-07-12 15:18:55 +00001770 if (quals && isa<ArrayType>(T)) {
1771 ty = Context.getASTContext().getAsArrayType(T);
John McCallb47f7482011-01-26 20:05:40 +00001772 quals = Qualifiers();
1773
Douglas Gregorf1588662011-07-12 15:18:55 +00001774 // Note that we don't update T: we want to add the
1775 // substitution at the original type.
John McCallb47f7482011-01-26 20:05:40 +00001776 }
1777
1778 if (quals) {
1779 mangleQualifiers(quals);
John McCall0953e762009-09-24 19:53:00 +00001780 // Recurse: even if the qualified type isn't yet substitutable,
1781 // the unqualified type might be.
John McCallb47f7482011-01-26 20:05:40 +00001782 mangleType(QualType(ty, 0));
Anders Carlsson76967372009-09-17 00:43:46 +00001783 } else {
John McCallb47f7482011-01-26 20:05:40 +00001784 switch (ty->getTypeClass()) {
John McCallefe6aee2009-09-05 07:56:18 +00001785#define ABSTRACT_TYPE(CLASS, PARENT)
1786#define NON_CANONICAL_TYPE(CLASS, PARENT) \
Anders Carlsson76967372009-09-17 00:43:46 +00001787 case Type::CLASS: \
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +00001788 llvm_unreachable("can't mangle non-canonical type " #CLASS "Type"); \
Anders Carlsson76967372009-09-17 00:43:46 +00001789 return;
John McCallefe6aee2009-09-05 07:56:18 +00001790#define TYPE(CLASS, PARENT) \
Anders Carlsson76967372009-09-17 00:43:46 +00001791 case Type::CLASS: \
John McCallb47f7482011-01-26 20:05:40 +00001792 mangleType(static_cast<const CLASS##Type*>(ty)); \
Anders Carlsson76967372009-09-17 00:43:46 +00001793 break;
John McCallefe6aee2009-09-05 07:56:18 +00001794#include "clang/AST/TypeNodes.def"
Anders Carlsson76967372009-09-17 00:43:46 +00001795 }
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001796 }
Anders Carlsson76967372009-09-17 00:43:46 +00001797
1798 // Add the substitution.
John McCallb47f7482011-01-26 20:05:40 +00001799 if (isSubstitutable)
Douglas Gregorf1588662011-07-12 15:18:55 +00001800 addSubstitution(T);
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001801}
1802
Douglas Gregor1b12a3b2010-05-26 05:11:13 +00001803void CXXNameMangler::mangleNameOrStandardSubstitution(const NamedDecl *ND) {
1804 if (!mangleStandardSubstitution(ND))
1805 mangleName(ND);
1806}
1807
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001808void CXXNameMangler::mangleType(const BuiltinType *T) {
John McCallefe6aee2009-09-05 07:56:18 +00001809 // <type> ::= <builtin-type>
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001810 // <builtin-type> ::= v # void
1811 // ::= w # wchar_t
1812 // ::= b # bool
1813 // ::= c # char
1814 // ::= a # signed char
1815 // ::= h # unsigned char
1816 // ::= s # short
1817 // ::= t # unsigned short
1818 // ::= i # int
1819 // ::= j # unsigned int
1820 // ::= l # long
1821 // ::= m # unsigned long
1822 // ::= x # long long, __int64
1823 // ::= y # unsigned long long, __int64
1824 // ::= n # __int128
1825 // UNSUPPORTED: ::= o # unsigned __int128
1826 // ::= f # float
1827 // ::= d # double
1828 // ::= e # long double, __float80
1829 // UNSUPPORTED: ::= g # __float128
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001830 // UNSUPPORTED: ::= Dd # IEEE 754r decimal floating point (64 bits)
1831 // UNSUPPORTED: ::= De # IEEE 754r decimal floating point (128 bits)
1832 // UNSUPPORTED: ::= Df # IEEE 754r decimal floating point (32 bits)
Anton Korobeynikovaa4a99b2011-10-14 23:23:15 +00001833 // ::= Dh # IEEE 754r half-precision floating point (16 bits)
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001834 // ::= Di # char32_t
1835 // ::= Ds # char16_t
Anders Carlssone2923682010-11-04 04:31:32 +00001836 // ::= Dn # std::nullptr_t (i.e., decltype(nullptr))
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001837 // ::= u <source-name> # vendor extended type
1838 switch (T->getKind()) {
1839 case BuiltinType::Void: Out << 'v'; break;
1840 case BuiltinType::Bool: Out << 'b'; break;
1841 case BuiltinType::Char_U: case BuiltinType::Char_S: Out << 'c'; break;
1842 case BuiltinType::UChar: Out << 'h'; break;
1843 case BuiltinType::UShort: Out << 't'; break;
1844 case BuiltinType::UInt: Out << 'j'; break;
1845 case BuiltinType::ULong: Out << 'm'; break;
1846 case BuiltinType::ULongLong: Out << 'y'; break;
Chris Lattner2df9ced2009-04-30 02:43:43 +00001847 case BuiltinType::UInt128: Out << 'o'; break;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001848 case BuiltinType::SChar: Out << 'a'; break;
Chris Lattner3f59c972010-12-25 23:25:43 +00001849 case BuiltinType::WChar_S:
1850 case BuiltinType::WChar_U: Out << 'w'; break;
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001851 case BuiltinType::Char16: Out << "Ds"; break;
1852 case BuiltinType::Char32: Out << "Di"; break;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001853 case BuiltinType::Short: Out << 's'; break;
1854 case BuiltinType::Int: Out << 'i'; break;
1855 case BuiltinType::Long: Out << 'l'; break;
1856 case BuiltinType::LongLong: Out << 'x'; break;
Chris Lattner2df9ced2009-04-30 02:43:43 +00001857 case BuiltinType::Int128: Out << 'n'; break;
Anton Korobeynikovaa4a99b2011-10-14 23:23:15 +00001858 case BuiltinType::Half: Out << "Dh"; break;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001859 case BuiltinType::Float: Out << 'f'; break;
1860 case BuiltinType::Double: Out << 'd'; break;
1861 case BuiltinType::LongDouble: Out << 'e'; break;
Anders Carlssone2923682010-11-04 04:31:32 +00001862 case BuiltinType::NullPtr: Out << "Dn"; break;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001863
John McCalle0a22d02011-10-18 21:02:43 +00001864#define BUILTIN_TYPE(Id, SingletonId)
1865#define PLACEHOLDER_TYPE(Id, SingletonId) \
1866 case BuiltinType::Id:
1867#include "clang/AST/BuiltinTypes.def"
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001868 case BuiltinType::Dependent:
John McCallfb44de92011-05-01 22:35:37 +00001869 llvm_unreachable("mangling a placeholder type");
Steve Naroff9533a7f2009-07-22 17:14:51 +00001870 case BuiltinType::ObjCId: Out << "11objc_object"; break;
1871 case BuiltinType::ObjCClass: Out << "10objc_class"; break;
Fariborz Jahanian13dcd002009-11-21 19:53:08 +00001872 case BuiltinType::ObjCSel: Out << "13objc_selector"; break;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001873 }
1874}
1875
John McCallefe6aee2009-09-05 07:56:18 +00001876// <type> ::= <function-type>
John McCall4b502632012-05-15 02:01:59 +00001877// <function-type> ::= [<CV-qualifiers>] F [Y]
1878// <bare-function-type> [<ref-qualifier>] E
1879// (Proposal to cxx-abi-dev, 2012-05-11)
John McCallefe6aee2009-09-05 07:56:18 +00001880void CXXNameMangler::mangleType(const FunctionProtoType *T) {
John McCall4b502632012-05-15 02:01:59 +00001881 // Mangle CV-qualifiers, if present. These are 'this' qualifiers,
1882 // e.g. "const" in "int (A::*)() const".
1883 mangleQualifiers(Qualifiers::fromCVRMask(T->getTypeQuals()));
1884
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001885 Out << 'F';
John McCall4b502632012-05-15 02:01:59 +00001886
Mike Stumpf5408fe2009-05-16 07:57:57 +00001887 // FIXME: We don't have enough information in the AST to produce the 'Y'
1888 // encoding for extern "C" function types.
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001889 mangleBareFunctionType(T, /*MangleReturnType=*/true);
John McCall4b502632012-05-15 02:01:59 +00001890
1891 // Mangle the ref-qualifier, if present.
1892 mangleRefQualifier(T->getRefQualifier());
1893
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001894 Out << 'E';
1895}
John McCallefe6aee2009-09-05 07:56:18 +00001896void CXXNameMangler::mangleType(const FunctionNoProtoType *T) {
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +00001897 llvm_unreachable("Can't mangle K&R function prototypes");
John McCallefe6aee2009-09-05 07:56:18 +00001898}
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001899void CXXNameMangler::mangleBareFunctionType(const FunctionType *T,
1900 bool MangleReturnType) {
John McCallefe6aee2009-09-05 07:56:18 +00001901 // We should never be mangling something without a prototype.
1902 const FunctionProtoType *Proto = cast<FunctionProtoType>(T);
1903
John McCallfb44de92011-05-01 22:35:37 +00001904 // Record that we're in a function type. See mangleFunctionParam
1905 // for details on what we're trying to achieve here.
1906 FunctionTypeDepthState saved = FunctionTypeDepth.push();
1907
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001908 // <bare-function-type> ::= <signature type>+
John McCallfb44de92011-05-01 22:35:37 +00001909 if (MangleReturnType) {
1910 FunctionTypeDepth.enterResultType();
John McCallefe6aee2009-09-05 07:56:18 +00001911 mangleType(Proto->getResultType());
John McCallfb44de92011-05-01 22:35:37 +00001912 FunctionTypeDepth.leaveResultType();
1913 }
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001914
Anders Carlsson93296682010-06-02 04:40:13 +00001915 if (Proto->getNumArgs() == 0 && !Proto->isVariadic()) {
Eli Friedmana7e68452010-08-22 01:00:03 +00001916 // <builtin-type> ::= v # void
Anders Carlssonc6c91bc2009-04-01 00:15:23 +00001917 Out << 'v';
John McCallfb44de92011-05-01 22:35:37 +00001918
1919 FunctionTypeDepth.pop(saved);
Anders Carlssonc6c91bc2009-04-01 00:15:23 +00001920 return;
1921 }
Mike Stump1eb44332009-09-09 15:08:12 +00001922
Douglas Gregor72564e72009-02-26 23:50:07 +00001923 for (FunctionProtoType::arg_type_iterator Arg = Proto->arg_type_begin(),
Mike Stump1eb44332009-09-09 15:08:12 +00001924 ArgEnd = Proto->arg_type_end();
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001925 Arg != ArgEnd; ++Arg)
Douglas Gregor79e6bd32011-07-12 04:42:08 +00001926 mangleType(Context.getASTContext().getSignatureParameterType(*Arg));
Douglas Gregor219cc612009-02-13 01:28:03 +00001927
John McCallfb44de92011-05-01 22:35:37 +00001928 FunctionTypeDepth.pop(saved);
1929
Douglas Gregor219cc612009-02-13 01:28:03 +00001930 // <builtin-type> ::= z # ellipsis
1931 if (Proto->isVariadic())
1932 Out << 'z';
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001933}
1934
John McCallefe6aee2009-09-05 07:56:18 +00001935// <type> ::= <class-enum-type>
Mike Stump1eb44332009-09-09 15:08:12 +00001936// <class-enum-type> ::= <name>
John McCalled976492009-12-04 22:46:56 +00001937void CXXNameMangler::mangleType(const UnresolvedUsingType *T) {
1938 mangleName(T->getDecl());
1939}
1940
1941// <type> ::= <class-enum-type>
1942// <class-enum-type> ::= <name>
John McCallefe6aee2009-09-05 07:56:18 +00001943void CXXNameMangler::mangleType(const EnumType *T) {
1944 mangleType(static_cast<const TagType*>(T));
1945}
1946void CXXNameMangler::mangleType(const RecordType *T) {
1947 mangleType(static_cast<const TagType*>(T));
1948}
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001949void CXXNameMangler::mangleType(const TagType *T) {
Eli Friedmanecb7e932009-12-11 18:00:57 +00001950 mangleName(T->getDecl());
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001951}
1952
John McCallefe6aee2009-09-05 07:56:18 +00001953// <type> ::= <array-type>
1954// <array-type> ::= A <positive dimension number> _ <element type>
1955// ::= A [<dimension expression>] _ <element type>
1956void CXXNameMangler::mangleType(const ConstantArrayType *T) {
1957 Out << 'A' << T->getSize() << '_';
1958 mangleType(T->getElementType());
1959}
1960void CXXNameMangler::mangleType(const VariableArrayType *T) {
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001961 Out << 'A';
Fariborz Jahanian7281d1f2010-11-02 16:54:00 +00001962 // decayed vla types (size 0) will just be skipped.
1963 if (T->getSizeExpr())
1964 mangleExpression(T->getSizeExpr());
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001965 Out << '_';
1966 mangleType(T->getElementType());
1967}
John McCallefe6aee2009-09-05 07:56:18 +00001968void CXXNameMangler::mangleType(const DependentSizedArrayType *T) {
1969 Out << 'A';
1970 mangleExpression(T->getSizeExpr());
1971 Out << '_';
1972 mangleType(T->getElementType());
1973}
1974void CXXNameMangler::mangleType(const IncompleteArrayType *T) {
Nick Lewycky271b6652010-09-05 03:40:33 +00001975 Out << "A_";
John McCallefe6aee2009-09-05 07:56:18 +00001976 mangleType(T->getElementType());
1977}
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001978
John McCallefe6aee2009-09-05 07:56:18 +00001979// <type> ::= <pointer-to-member-type>
1980// <pointer-to-member-type> ::= M <class type> <member type>
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001981void CXXNameMangler::mangleType(const MemberPointerType *T) {
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001982 Out << 'M';
1983 mangleType(QualType(T->getClass(), 0));
Anders Carlsson0e650012009-05-17 17:41:20 +00001984 QualType PointeeType = T->getPointeeType();
1985 if (const FunctionProtoType *FPT = dyn_cast<FunctionProtoType>(PointeeType)) {
Anders Carlsson0e650012009-05-17 17:41:20 +00001986 mangleType(FPT);
Anders Carlsson9d85b722010-06-02 04:29:50 +00001987
1988 // Itanium C++ ABI 5.1.8:
1989 //
1990 // The type of a non-static member function is considered to be different,
1991 // for the purposes of substitution, from the type of a namespace-scope or
1992 // static member function whose type appears similar. The types of two
1993 // non-static member functions are considered to be different, for the
1994 // purposes of substitution, if the functions are members of different
1995 // classes. In other words, for the purposes of substitution, the class of
1996 // which the function is a member is considered part of the type of
1997 // function.
1998
John McCall4b502632012-05-15 02:01:59 +00001999 // Given that we already substitute member function pointers as a
2000 // whole, the net effect of this rule is just to unconditionally
2001 // suppress substitution on the function type in a member pointer.
Anders Carlsson9d85b722010-06-02 04:29:50 +00002002 // We increment the SeqID here to emulate adding an entry to the
John McCall4b502632012-05-15 02:01:59 +00002003 // substitution table.
Anders Carlsson9d85b722010-06-02 04:29:50 +00002004 ++SeqID;
Mike Stump1eb44332009-09-09 15:08:12 +00002005 } else
Anders Carlsson0e650012009-05-17 17:41:20 +00002006 mangleType(PointeeType);
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00002007}
2008
John McCallefe6aee2009-09-05 07:56:18 +00002009// <type> ::= <template-param>
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00002010void CXXNameMangler::mangleType(const TemplateTypeParmType *T) {
Anders Carlsson0ccdf8d2009-09-27 00:38:53 +00002011 mangleTemplateParameter(T->getIndex());
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00002012}
2013
Douglas Gregorc3069d62011-01-14 02:55:32 +00002014// <type> ::= <template-param>
2015void CXXNameMangler::mangleType(const SubstTemplateTypeParmPackType *T) {
John McCall68a51a72011-07-01 00:04:39 +00002016 // FIXME: not clear how to mangle this!
2017 // template <class T...> class A {
2018 // template <class U...> void foo(T(*)(U) x...);
2019 // };
2020 Out << "_SUBSTPACK_";
Douglas Gregorc3069d62011-01-14 02:55:32 +00002021}
2022
John McCallefe6aee2009-09-05 07:56:18 +00002023// <type> ::= P <type> # pointer-to
2024void CXXNameMangler::mangleType(const PointerType *T) {
2025 Out << 'P';
2026 mangleType(T->getPointeeType());
2027}
2028void CXXNameMangler::mangleType(const ObjCObjectPointerType *T) {
2029 Out << 'P';
2030 mangleType(T->getPointeeType());
2031}
2032
2033// <type> ::= R <type> # reference-to
2034void CXXNameMangler::mangleType(const LValueReferenceType *T) {
2035 Out << 'R';
2036 mangleType(T->getPointeeType());
2037}
2038
2039// <type> ::= O <type> # rvalue reference-to (C++0x)
2040void CXXNameMangler::mangleType(const RValueReferenceType *T) {
2041 Out << 'O';
2042 mangleType(T->getPointeeType());
2043}
2044
2045// <type> ::= C <type> # complex pair (C 2000)
2046void CXXNameMangler::mangleType(const ComplexType *T) {
2047 Out << 'C';
2048 mangleType(T->getElementType());
2049}
2050
Bob Wilsonc7df92d2010-11-12 17:24:43 +00002051// ARM's ABI for Neon vector types specifies that they should be mangled as
Bob Wilson57147a82010-11-16 00:32:18 +00002052// if they are structs (to match ARM's initial implementation). The
2053// vector type must be one of the special types predefined by ARM.
2054void CXXNameMangler::mangleNeonVectorType(const VectorType *T) {
Bob Wilsonc7df92d2010-11-12 17:24:43 +00002055 QualType EltType = T->getElementType();
Bob Wilson57147a82010-11-16 00:32:18 +00002056 assert(EltType->isBuiltinType() && "Neon vector element not a BuiltinType");
Bob Wilsonc7df92d2010-11-12 17:24:43 +00002057 const char *EltName = 0;
Bob Wilson491328c2010-11-12 17:24:46 +00002058 if (T->getVectorKind() == VectorType::NeonPolyVector) {
2059 switch (cast<BuiltinType>(EltType)->getKind()) {
Bob Wilson4cfaa5d2010-11-12 17:24:49 +00002060 case BuiltinType::SChar: EltName = "poly8_t"; break;
2061 case BuiltinType::Short: EltName = "poly16_t"; break;
Bob Wilson57147a82010-11-16 00:32:18 +00002062 default: llvm_unreachable("unexpected Neon polynomial vector element type");
Bob Wilson491328c2010-11-12 17:24:46 +00002063 }
2064 } else {
2065 switch (cast<BuiltinType>(EltType)->getKind()) {
Bob Wilson4cfaa5d2010-11-12 17:24:49 +00002066 case BuiltinType::SChar: EltName = "int8_t"; break;
2067 case BuiltinType::UChar: EltName = "uint8_t"; break;
2068 case BuiltinType::Short: EltName = "int16_t"; break;
2069 case BuiltinType::UShort: EltName = "uint16_t"; break;
2070 case BuiltinType::Int: EltName = "int32_t"; break;
2071 case BuiltinType::UInt: EltName = "uint32_t"; break;
2072 case BuiltinType::LongLong: EltName = "int64_t"; break;
2073 case BuiltinType::ULongLong: EltName = "uint64_t"; break;
2074 case BuiltinType::Float: EltName = "float32_t"; break;
Bob Wilson57147a82010-11-16 00:32:18 +00002075 default: llvm_unreachable("unexpected Neon vector element type");
Bob Wilson491328c2010-11-12 17:24:46 +00002076 }
Bob Wilsonc7df92d2010-11-12 17:24:43 +00002077 }
2078 const char *BaseName = 0;
Bob Wilson4cfaa5d2010-11-12 17:24:49 +00002079 unsigned BitSize = (T->getNumElements() *
Bob Wilson3a723022010-11-16 00:32:12 +00002080 getASTContext().getTypeSize(EltType));
Bob Wilsonc7df92d2010-11-12 17:24:43 +00002081 if (BitSize == 64)
2082 BaseName = "__simd64_";
Bob Wilson57147a82010-11-16 00:32:18 +00002083 else {
2084 assert(BitSize == 128 && "Neon vector type not 64 or 128 bits");
Bob Wilsonc7df92d2010-11-12 17:24:43 +00002085 BaseName = "__simd128_";
Bob Wilson57147a82010-11-16 00:32:18 +00002086 }
Bob Wilsonc7df92d2010-11-12 17:24:43 +00002087 Out << strlen(BaseName) + strlen(EltName);
2088 Out << BaseName << EltName;
Bob Wilsonc7df92d2010-11-12 17:24:43 +00002089}
2090
John McCallefe6aee2009-09-05 07:56:18 +00002091// GNU extension: vector types
Chris Lattner788b0fd2010-06-23 06:00:24 +00002092// <type> ::= <vector-type>
2093// <vector-type> ::= Dv <positive dimension number> _
2094// <extended element type>
2095// ::= Dv [<dimension expression>] _ <element type>
2096// <extended element type> ::= <element type>
2097// ::= p # AltiVec vector pixel
John McCallefe6aee2009-09-05 07:56:18 +00002098void CXXNameMangler::mangleType(const VectorType *T) {
Bob Wilson491328c2010-11-12 17:24:46 +00002099 if ((T->getVectorKind() == VectorType::NeonVector ||
Bob Wilson57147a82010-11-16 00:32:18 +00002100 T->getVectorKind() == VectorType::NeonPolyVector)) {
2101 mangleNeonVectorType(T);
Bob Wilsonc7df92d2010-11-12 17:24:43 +00002102 return;
Bob Wilson57147a82010-11-16 00:32:18 +00002103 }
Nick Lewycky0e5f0672010-03-26 07:18:04 +00002104 Out << "Dv" << T->getNumElements() << '_';
Bob Wilsone86d78c2010-11-10 21:56:12 +00002105 if (T->getVectorKind() == VectorType::AltiVecPixel)
Chris Lattner788b0fd2010-06-23 06:00:24 +00002106 Out << 'p';
Bob Wilsone86d78c2010-11-10 21:56:12 +00002107 else if (T->getVectorKind() == VectorType::AltiVecBool)
Chris Lattner788b0fd2010-06-23 06:00:24 +00002108 Out << 'b';
2109 else
2110 mangleType(T->getElementType());
John McCallefe6aee2009-09-05 07:56:18 +00002111}
2112void CXXNameMangler::mangleType(const ExtVectorType *T) {
2113 mangleType(static_cast<const VectorType*>(T));
2114}
2115void CXXNameMangler::mangleType(const DependentSizedExtVectorType *T) {
Nick Lewycky0e5f0672010-03-26 07:18:04 +00002116 Out << "Dv";
2117 mangleExpression(T->getSizeExpr());
2118 Out << '_';
John McCallefe6aee2009-09-05 07:56:18 +00002119 mangleType(T->getElementType());
2120}
2121
Douglas Gregor7536dd52010-12-20 02:24:11 +00002122void CXXNameMangler::mangleType(const PackExpansionType *T) {
Douglas Gregor4fc48662011-01-13 16:39:34 +00002123 // <type> ::= Dp <type> # pack expansion (C++0x)
Douglas Gregor255c2692011-01-13 17:44:36 +00002124 Out << "Dp";
Douglas Gregor7536dd52010-12-20 02:24:11 +00002125 mangleType(T->getPattern());
2126}
2127
Anders Carlssona40c5e42009-03-07 22:03:21 +00002128void CXXNameMangler::mangleType(const ObjCInterfaceType *T) {
2129 mangleSourceName(T->getDecl()->getIdentifier());
2130}
2131
John McCallc12c5bb2010-05-15 11:32:37 +00002132void CXXNameMangler::mangleType(const ObjCObjectType *T) {
John McCallc00c1f62010-05-15 17:06:29 +00002133 // We don't allow overloading by different protocol qualification,
2134 // so mangling them isn't necessary.
John McCallc12c5bb2010-05-15 11:32:37 +00002135 mangleType(T->getBaseType());
2136}
2137
John McCallefe6aee2009-09-05 07:56:18 +00002138void CXXNameMangler::mangleType(const BlockPointerType *T) {
Anders Carlssonf28c6872009-12-23 22:31:44 +00002139 Out << "U13block_pointer";
2140 mangleType(T->getPointeeType());
John McCallefe6aee2009-09-05 07:56:18 +00002141}
2142
John McCall31f17ec2010-04-27 00:57:59 +00002143void CXXNameMangler::mangleType(const InjectedClassNameType *T) {
2144 // Mangle injected class name types as if the user had written the
2145 // specialization out fully. It may not actually be possible to see
2146 // this mangling, though.
2147 mangleType(T->getInjectedSpecializationType());
2148}
2149
John McCallefe6aee2009-09-05 07:56:18 +00002150void CXXNameMangler::mangleType(const TemplateSpecializationType *T) {
Douglas Gregor1e9268e2010-04-28 05:58:56 +00002151 if (TemplateDecl *TD = T->getTemplateName().getAsTemplateDecl()) {
2152 mangleName(TD, T->getArgs(), T->getNumArgs());
2153 } else {
2154 if (mangleSubstitution(QualType(T, 0)))
2155 return;
Sean Huntc3021132010-05-05 15:23:54 +00002156
Douglas Gregor1e9268e2010-04-28 05:58:56 +00002157 mangleTemplatePrefix(T->getTemplateName());
Sean Huntc3021132010-05-05 15:23:54 +00002158
Douglas Gregor1e9268e2010-04-28 05:58:56 +00002159 // FIXME: GCC does not appear to mangle the template arguments when
2160 // the template in question is a dependent template name. Should we
2161 // emulate that badness?
Eli Friedmand7a6b162012-09-26 02:36:12 +00002162 mangleTemplateArgs(T->getArgs(), T->getNumArgs());
Douglas Gregor1e9268e2010-04-28 05:58:56 +00002163 addSubstitution(QualType(T, 0));
2164 }
John McCallefe6aee2009-09-05 07:56:18 +00002165}
2166
Douglas Gregor4714c122010-03-31 17:34:00 +00002167void CXXNameMangler::mangleType(const DependentNameType *T) {
Anders Carlssonae352482009-09-26 02:26:02 +00002168 // Typename types are always nested
2169 Out << 'N';
John McCalla0ce15c2011-04-24 08:23:24 +00002170 manglePrefix(T->getQualifier());
John McCall33500952010-06-11 00:33:02 +00002171 mangleSourceName(T->getIdentifier());
2172 Out << 'E';
2173}
John McCall6ab30e02010-06-09 07:26:17 +00002174
John McCall33500952010-06-11 00:33:02 +00002175void CXXNameMangler::mangleType(const DependentTemplateSpecializationType *T) {
Douglas Gregoraa2187d2011-02-28 00:04:36 +00002176 // Dependently-scoped template types are nested if they have a prefix.
John McCall33500952010-06-11 00:33:02 +00002177 Out << 'N';
2178
2179 // TODO: avoid making this TemplateName.
2180 TemplateName Prefix =
2181 getASTContext().getDependentTemplateName(T->getQualifier(),
2182 T->getIdentifier());
2183 mangleTemplatePrefix(Prefix);
2184
2185 // FIXME: GCC does not appear to mangle the template arguments when
2186 // the template in question is a dependent template name. Should we
2187 // emulate that badness?
Eli Friedmand7a6b162012-09-26 02:36:12 +00002188 mangleTemplateArgs(T->getArgs(), T->getNumArgs());
Anders Carlssonae352482009-09-26 02:26:02 +00002189 Out << 'E';
John McCallefe6aee2009-09-05 07:56:18 +00002190}
2191
John McCallad5e7382010-03-01 23:49:17 +00002192void CXXNameMangler::mangleType(const TypeOfType *T) {
2193 // FIXME: this is pretty unsatisfactory, but there isn't an obvious
2194 // "extension with parameters" mangling.
2195 Out << "u6typeof";
2196}
2197
2198void CXXNameMangler::mangleType(const TypeOfExprType *T) {
2199 // FIXME: this is pretty unsatisfactory, but there isn't an obvious
2200 // "extension with parameters" mangling.
2201 Out << "u6typeof";
2202}
2203
2204void CXXNameMangler::mangleType(const DecltypeType *T) {
2205 Expr *E = T->getUnderlyingExpr();
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002206
John McCallad5e7382010-03-01 23:49:17 +00002207 // type ::= Dt <expression> E # decltype of an id-expression
2208 // # or class member access
2209 // ::= DT <expression> E # decltype of an expression
2210
2211 // This purports to be an exhaustive list of id-expressions and
2212 // class member accesses. Note that we do not ignore parentheses;
2213 // parentheses change the semantics of decltype for these
2214 // expressions (and cause the mangler to use the other form).
2215 if (isa<DeclRefExpr>(E) ||
2216 isa<MemberExpr>(E) ||
2217 isa<UnresolvedLookupExpr>(E) ||
2218 isa<DependentScopeDeclRefExpr>(E) ||
2219 isa<CXXDependentScopeMemberExpr>(E) ||
2220 isa<UnresolvedMemberExpr>(E))
2221 Out << "Dt";
2222 else
2223 Out << "DT";
2224 mangleExpression(E);
2225 Out << 'E';
2226}
2227
Sean Huntca63c202011-05-24 22:41:36 +00002228void CXXNameMangler::mangleType(const UnaryTransformType *T) {
2229 // If this is dependent, we need to record that. If not, we simply
2230 // mangle it as the underlying type since they are equivalent.
2231 if (T->isDependentType()) {
2232 Out << 'U';
2233
2234 switch (T->getUTTKind()) {
2235 case UnaryTransformType::EnumUnderlyingType:
2236 Out << "3eut";
2237 break;
2238 }
2239 }
2240
2241 mangleType(T->getUnderlyingType());
2242}
2243
Richard Smith34b41d92011-02-20 03:19:35 +00002244void CXXNameMangler::mangleType(const AutoType *T) {
2245 QualType D = T->getDeducedType();
Richard Smith967ecd32011-02-21 20:10:02 +00002246 // <builtin-type> ::= Da # dependent auto
2247 if (D.isNull())
2248 Out << "Da";
2249 else
2250 mangleType(D);
Richard Smith34b41d92011-02-20 03:19:35 +00002251}
2252
Eli Friedmanb001de72011-10-06 23:00:33 +00002253void CXXNameMangler::mangleType(const AtomicType *T) {
2254 // <type> ::= U <source-name> <type> # vendor extended type qualifier
2255 // (Until there's a standardized mangling...)
2256 Out << "U7_Atomic";
2257 mangleType(T->getValueType());
2258}
2259
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002260void CXXNameMangler::mangleIntegerLiteral(QualType T,
Anders Carlssone170ba72009-12-14 01:45:37 +00002261 const llvm::APSInt &Value) {
2262 // <expr-primary> ::= L <type> <value number> E # integer literal
2263 Out << 'L';
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002264
Anders Carlssone170ba72009-12-14 01:45:37 +00002265 mangleType(T);
2266 if (T->isBooleanType()) {
2267 // Boolean values are encoded as 0/1.
2268 Out << (Value.getBoolValue() ? '1' : '0');
2269 } else {
John McCall0512e482010-07-14 04:20:34 +00002270 mangleNumber(Value);
Anders Carlssone170ba72009-12-14 01:45:37 +00002271 }
2272 Out << 'E';
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002273
Anders Carlssone170ba72009-12-14 01:45:37 +00002274}
2275
Douglas Gregorcefc3af2012-04-16 07:05:22 +00002276/// Mangles a member expression.
John McCalla0ce15c2011-04-24 08:23:24 +00002277void CXXNameMangler::mangleMemberExpr(const Expr *base,
2278 bool isArrow,
2279 NestedNameSpecifier *qualifier,
2280 NamedDecl *firstQualifierLookup,
2281 DeclarationName member,
2282 unsigned arity) {
2283 // <expression> ::= dt <expression> <unresolved-name>
2284 // ::= pt <expression> <unresolved-name>
Douglas Gregorcefc3af2012-04-16 07:05:22 +00002285 if (base) {
2286 if (base->isImplicitCXXThis()) {
2287 // Note: GCC mangles member expressions to the implicit 'this' as
2288 // *this., whereas we represent them as this->. The Itanium C++ ABI
2289 // does not specify anything here, so we follow GCC.
2290 Out << "dtdefpT";
2291 } else {
2292 Out << (isArrow ? "pt" : "dt");
2293 mangleExpression(base);
2294 }
2295 }
John McCalla0ce15c2011-04-24 08:23:24 +00002296 mangleUnresolvedName(qualifier, firstQualifierLookup, member, arity);
John McCall2f27bf82010-02-04 02:56:29 +00002297}
2298
John McCall5a7e6f72011-04-28 02:52:03 +00002299/// Look at the callee of the given call expression and determine if
2300/// it's a parenthesized id-expression which would have triggered ADL
2301/// otherwise.
2302static bool isParenthesizedADLCallee(const CallExpr *call) {
2303 const Expr *callee = call->getCallee();
2304 const Expr *fn = callee->IgnoreParens();
2305
2306 // Must be parenthesized. IgnoreParens() skips __extension__ nodes,
2307 // too, but for those to appear in the callee, it would have to be
2308 // parenthesized.
2309 if (callee == fn) return false;
2310
2311 // Must be an unresolved lookup.
2312 const UnresolvedLookupExpr *lookup = dyn_cast<UnresolvedLookupExpr>(fn);
2313 if (!lookup) return false;
2314
2315 assert(!lookup->requiresADL());
2316
2317 // Must be an unqualified lookup.
2318 if (lookup->getQualifier()) return false;
2319
2320 // Must not have found a class member. Note that if one is a class
2321 // member, they're all class members.
2322 if (lookup->getNumDecls() > 0 &&
2323 (*lookup->decls_begin())->isCXXClassMember())
2324 return false;
2325
2326 // Otherwise, ADL would have been triggered.
2327 return true;
2328}
2329
John McCall5e1e89b2010-08-18 19:18:59 +00002330void CXXNameMangler::mangleExpression(const Expr *E, unsigned Arity) {
Anders Carlssond553f8c2009-09-21 01:21:10 +00002331 // <expression> ::= <unary operator-name> <expression>
John McCall09cc1412010-02-03 00:55:45 +00002332 // ::= <binary operator-name> <expression> <expression>
2333 // ::= <trinary operator-name> <expression> <expression> <expression>
Anders Carlssond553f8c2009-09-21 01:21:10 +00002334 // ::= cv <type> expression # conversion with one argument
2335 // ::= cv <type> _ <expression>* E # conversion with a different number of arguments
Eli Friedmana7e68452010-08-22 01:00:03 +00002336 // ::= st <type> # sizeof (a type)
Anders Carlssond553f8c2009-09-21 01:21:10 +00002337 // ::= at <type> # alignof (a type)
2338 // ::= <template-param>
2339 // ::= <function-param>
2340 // ::= sr <type> <unqualified-name> # dependent name
2341 // ::= sr <type> <unqualified-name> <template-args> # dependent template-id
Douglas Gregor63f62df2011-06-05 05:27:58 +00002342 // ::= ds <expression> <expression> # expr.*expr
Anders Carlssond553f8c2009-09-21 01:21:10 +00002343 // ::= sZ <template-param> # size of a parameter pack
Douglas Gregor4fc48662011-01-13 16:39:34 +00002344 // ::= sZ <function-param> # size of a function parameter pack
John McCall09cc1412010-02-03 00:55:45 +00002345 // ::= <expr-primary>
John McCall1dd73832010-02-04 01:42:13 +00002346 // <expr-primary> ::= L <type> <value number> E # integer literal
2347 // ::= L <type <value float> E # floating literal
2348 // ::= L <mangled-name> E # external name
Douglas Gregorcefc3af2012-04-16 07:05:22 +00002349 // ::= fpT # 'this' expression
Douglas Gregoredee94b2011-07-12 04:47:20 +00002350 QualType ImplicitlyConvertedToType;
2351
2352recurse:
Anders Carlssond553f8c2009-09-21 01:21:10 +00002353 switch (E->getStmtClass()) {
John McCall6ae1f352010-04-09 22:26:14 +00002354 case Expr::NoStmtClass:
John McCall63c00d72011-02-09 08:16:59 +00002355#define ABSTRACT_STMT(Type)
John McCall6ae1f352010-04-09 22:26:14 +00002356#define EXPR(Type, Base)
2357#define STMT(Type, Base) \
2358 case Expr::Type##Class:
Sean Hunt4bfe1962010-05-05 15:24:00 +00002359#include "clang/AST/StmtNodes.inc"
John McCall0512e482010-07-14 04:20:34 +00002360 // fallthrough
2361
2362 // These all can only appear in local or variable-initialization
2363 // contexts and so should never appear in a mangling.
2364 case Expr::AddrLabelExprClass:
John McCall0512e482010-07-14 04:20:34 +00002365 case Expr::DesignatedInitExprClass:
2366 case Expr::ImplicitValueInitExprClass:
John McCall0512e482010-07-14 04:20:34 +00002367 case Expr::ParenListExprClass:
Douglas Gregor01d08012012-02-07 10:09:13 +00002368 case Expr::LambdaExprClass:
John McCall09cc1412010-02-03 00:55:45 +00002369 llvm_unreachable("unexpected statement kind");
John McCall09cc1412010-02-03 00:55:45 +00002370
John McCall0512e482010-07-14 04:20:34 +00002371 // FIXME: invent manglings for all these.
2372 case Expr::BlockExprClass:
2373 case Expr::CXXPseudoDestructorExprClass:
2374 case Expr::ChooseExprClass:
2375 case Expr::CompoundLiteralExprClass:
2376 case Expr::ExtVectorElementExprClass:
Peter Collingbournef111d932011-04-15 00:35:48 +00002377 case Expr::GenericSelectionExprClass:
John McCall0512e482010-07-14 04:20:34 +00002378 case Expr::ObjCEncodeExprClass:
John McCall0512e482010-07-14 04:20:34 +00002379 case Expr::ObjCIsaExprClass:
2380 case Expr::ObjCIvarRefExprClass:
2381 case Expr::ObjCMessageExprClass:
2382 case Expr::ObjCPropertyRefExprClass:
2383 case Expr::ObjCProtocolExprClass:
2384 case Expr::ObjCSelectorExprClass:
2385 case Expr::ObjCStringLiteralClass:
Patrick Beardeb382ec2012-04-19 00:25:12 +00002386 case Expr::ObjCBoxedExprClass:
Ted Kremenekebcb57a2012-03-06 20:05:56 +00002387 case Expr::ObjCArrayLiteralClass:
2388 case Expr::ObjCDictionaryLiteralClass:
2389 case Expr::ObjCSubscriptRefExprClass:
John McCallf85e1932011-06-15 23:02:42 +00002390 case Expr::ObjCIndirectCopyRestoreExprClass:
John McCall0512e482010-07-14 04:20:34 +00002391 case Expr::OffsetOfExprClass:
2392 case Expr::PredefinedExprClass:
2393 case Expr::ShuffleVectorExprClass:
2394 case Expr::StmtExprClass:
John McCall0512e482010-07-14 04:20:34 +00002395 case Expr::UnaryTypeTraitExprClass:
Francois Pichet6ad6f282010-12-07 00:08:36 +00002396 case Expr::BinaryTypeTraitExprClass:
Douglas Gregor4ca8ac22012-02-24 07:38:34 +00002397 case Expr::TypeTraitExprClass:
John Wiegley21ff2e52011-04-28 00:16:57 +00002398 case Expr::ArrayTypeTraitExprClass:
John Wiegley55262202011-04-25 06:54:41 +00002399 case Expr::ExpressionTraitExprClass:
Francois Pichet9be88402010-09-08 23:47:05 +00002400 case Expr::VAArgExprClass:
Sebastian Redl2e156222010-09-10 20:55:43 +00002401 case Expr::CXXUuidofExprClass:
Tanya Lattner61eee0c2011-06-04 00:47:47 +00002402 case Expr::CUDAKernelCallExprClass:
2403 case Expr::AsTypeExprClass:
John McCall4b9c2d22011-11-06 09:01:30 +00002404 case Expr::PseudoObjectExprClass:
Eli Friedman276b0612011-10-11 02:20:01 +00002405 case Expr::AtomicExprClass:
Tanya Lattner61eee0c2011-06-04 00:47:47 +00002406 {
John McCall6ae1f352010-04-09 22:26:14 +00002407 // As bad as this diagnostic is, it's better than crashing.
David Blaikied6471f72011-09-25 23:23:43 +00002408 DiagnosticsEngine &Diags = Context.getDiags();
2409 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
John McCall6ae1f352010-04-09 22:26:14 +00002410 "cannot yet mangle expression type %0");
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +00002411 Diags.Report(E->getExprLoc(), DiagID)
John McCall739bf092010-04-10 09:39:25 +00002412 << E->getStmtClassName() << E->getSourceRange();
John McCall6ae1f352010-04-09 22:26:14 +00002413 break;
2414 }
2415
John McCall56ca35d2011-02-17 10:25:35 +00002416 // Even gcc-4.5 doesn't mangle this.
2417 case Expr::BinaryConditionalOperatorClass: {
David Blaikied6471f72011-09-25 23:23:43 +00002418 DiagnosticsEngine &Diags = Context.getDiags();
John McCall56ca35d2011-02-17 10:25:35 +00002419 unsigned DiagID =
David Blaikied6471f72011-09-25 23:23:43 +00002420 Diags.getCustomDiagID(DiagnosticsEngine::Error,
John McCall56ca35d2011-02-17 10:25:35 +00002421 "?: operator with omitted middle operand cannot be mangled");
2422 Diags.Report(E->getExprLoc(), DiagID)
2423 << E->getStmtClassName() << E->getSourceRange();
2424 break;
2425 }
2426
2427 // These are used for internal purposes and cannot be meaningfully mangled.
John McCall7cd7d1a2010-11-15 23:31:06 +00002428 case Expr::OpaqueValueExprClass:
2429 llvm_unreachable("cannot mangle opaque value; mangling wrong thing?");
2430
Sebastian Redlfaf4ef62012-02-25 22:59:28 +00002431 case Expr::InitListExprClass: {
2432 // Proposal by Jason Merrill, 2012-01-03
2433 Out << "il";
2434 const InitListExpr *InitList = cast<InitListExpr>(E);
2435 for (unsigned i = 0, e = InitList->getNumInits(); i != e; ++i)
2436 mangleExpression(InitList->getInit(i));
2437 Out << "E";
2438 break;
2439 }
2440
John McCall0512e482010-07-14 04:20:34 +00002441 case Expr::CXXDefaultArgExprClass:
John McCall5e1e89b2010-08-18 19:18:59 +00002442 mangleExpression(cast<CXXDefaultArgExpr>(E)->getExpr(), Arity);
John McCall0512e482010-07-14 04:20:34 +00002443 break;
2444
John McCall91a57552011-07-15 05:09:51 +00002445 case Expr::SubstNonTypeTemplateParmExprClass:
2446 mangleExpression(cast<SubstNonTypeTemplateParmExpr>(E)->getReplacement(),
2447 Arity);
2448 break;
2449
Richard Smith9fcce652012-03-07 08:35:16 +00002450 case Expr::UserDefinedLiteralClass:
2451 // We follow g++'s approach of mangling a UDL as a call to the literal
2452 // operator.
John McCall0512e482010-07-14 04:20:34 +00002453 case Expr::CXXMemberCallExprClass: // fallthrough
John McCall1dd73832010-02-04 01:42:13 +00002454 case Expr::CallExprClass: {
2455 const CallExpr *CE = cast<CallExpr>(E);
John McCall5a7e6f72011-04-28 02:52:03 +00002456
2457 // <expression> ::= cp <simple-id> <expression>* E
2458 // We use this mangling only when the call would use ADL except
2459 // for being parenthesized. Per discussion with David
2460 // Vandervoorde, 2011.04.25.
2461 if (isParenthesizedADLCallee(CE)) {
2462 Out << "cp";
2463 // The callee here is a parenthesized UnresolvedLookupExpr with
2464 // no qualifier and should always get mangled as a <simple-id>
2465 // anyway.
2466
2467 // <expression> ::= cl <expression>* E
2468 } else {
2469 Out << "cl";
2470 }
2471
John McCall5e1e89b2010-08-18 19:18:59 +00002472 mangleExpression(CE->getCallee(), CE->getNumArgs());
John McCall1dd73832010-02-04 01:42:13 +00002473 for (unsigned I = 0, N = CE->getNumArgs(); I != N; ++I)
2474 mangleExpression(CE->getArg(I));
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002475 Out << 'E';
John McCall09cc1412010-02-03 00:55:45 +00002476 break;
John McCall1dd73832010-02-04 01:42:13 +00002477 }
John McCall09cc1412010-02-03 00:55:45 +00002478
John McCall0512e482010-07-14 04:20:34 +00002479 case Expr::CXXNewExprClass: {
John McCall0512e482010-07-14 04:20:34 +00002480 const CXXNewExpr *New = cast<CXXNewExpr>(E);
2481 if (New->isGlobalNew()) Out << "gs";
2482 Out << (New->isArray() ? "na" : "nw");
2483 for (CXXNewExpr::const_arg_iterator I = New->placement_arg_begin(),
2484 E = New->placement_arg_end(); I != E; ++I)
2485 mangleExpression(*I);
2486 Out << '_';
2487 mangleType(New->getAllocatedType());
2488 if (New->hasInitializer()) {
Sebastian Redlfaf4ef62012-02-25 22:59:28 +00002489 // Proposal by Jason Merrill, 2012-01-03
Sebastian Redlb76ffc52012-02-25 20:51:07 +00002490 if (New->getInitializationStyle() == CXXNewExpr::ListInit)
Sebastian Redlfaf4ef62012-02-25 22:59:28 +00002491 Out << "il";
Sebastian Redlb76ffc52012-02-25 20:51:07 +00002492 else
2493 Out << "pi";
Sebastian Redl2aed8b82012-02-16 12:22:20 +00002494 const Expr *Init = New->getInitializer();
2495 if (const CXXConstructExpr *CCE = dyn_cast<CXXConstructExpr>(Init)) {
2496 // Directly inline the initializers.
2497 for (CXXConstructExpr::const_arg_iterator I = CCE->arg_begin(),
2498 E = CCE->arg_end();
2499 I != E; ++I)
2500 mangleExpression(*I);
2501 } else if (const ParenListExpr *PLE = dyn_cast<ParenListExpr>(Init)) {
2502 for (unsigned i = 0, e = PLE->getNumExprs(); i != e; ++i)
2503 mangleExpression(PLE->getExpr(i));
Sebastian Redlb76ffc52012-02-25 20:51:07 +00002504 } else if (New->getInitializationStyle() == CXXNewExpr::ListInit &&
2505 isa<InitListExpr>(Init)) {
Sebastian Redlfaf4ef62012-02-25 22:59:28 +00002506 // Only take InitListExprs apart for list-initialization.
Sebastian Redlb76ffc52012-02-25 20:51:07 +00002507 const InitListExpr *InitList = cast<InitListExpr>(Init);
2508 for (unsigned i = 0, e = InitList->getNumInits(); i != e; ++i)
2509 mangleExpression(InitList->getInit(i));
Sebastian Redl2aed8b82012-02-16 12:22:20 +00002510 } else
2511 mangleExpression(Init);
John McCall0512e482010-07-14 04:20:34 +00002512 }
2513 Out << 'E';
2514 break;
2515 }
2516
John McCall2f27bf82010-02-04 02:56:29 +00002517 case Expr::MemberExprClass: {
2518 const MemberExpr *ME = cast<MemberExpr>(E);
2519 mangleMemberExpr(ME->getBase(), ME->isArrow(),
John McCalla0ce15c2011-04-24 08:23:24 +00002520 ME->getQualifier(), 0, ME->getMemberDecl()->getDeclName(),
John McCall5e1e89b2010-08-18 19:18:59 +00002521 Arity);
John McCall2f27bf82010-02-04 02:56:29 +00002522 break;
2523 }
2524
2525 case Expr::UnresolvedMemberExprClass: {
2526 const UnresolvedMemberExpr *ME = cast<UnresolvedMemberExpr>(E);
2527 mangleMemberExpr(ME->getBase(), ME->isArrow(),
John McCalla0ce15c2011-04-24 08:23:24 +00002528 ME->getQualifier(), 0, ME->getMemberName(),
John McCall5e1e89b2010-08-18 19:18:59 +00002529 Arity);
John McCall6dbce192010-08-20 00:17:19 +00002530 if (ME->hasExplicitTemplateArgs())
2531 mangleTemplateArgs(ME->getExplicitTemplateArgs());
John McCall2f27bf82010-02-04 02:56:29 +00002532 break;
2533 }
2534
2535 case Expr::CXXDependentScopeMemberExprClass: {
2536 const CXXDependentScopeMemberExpr *ME
2537 = cast<CXXDependentScopeMemberExpr>(E);
2538 mangleMemberExpr(ME->getBase(), ME->isArrow(),
John McCalla0ce15c2011-04-24 08:23:24 +00002539 ME->getQualifier(), ME->getFirstQualifierFoundInScope(),
2540 ME->getMember(), 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
John McCall1dd73832010-02-04 01:42:13 +00002546 case Expr::UnresolvedLookupExprClass: {
2547 const UnresolvedLookupExpr *ULE = cast<UnresolvedLookupExpr>(E);
John McCalla0ce15c2011-04-24 08:23:24 +00002548 mangleUnresolvedName(ULE->getQualifier(), 0, ULE->getName(), Arity);
John McCall26a6ec72011-06-21 22:12:46 +00002549
2550 // All the <unresolved-name> productions end in a
2551 // base-unresolved-name, where <template-args> are just tacked
2552 // onto the end.
John McCall6dbce192010-08-20 00:17:19 +00002553 if (ULE->hasExplicitTemplateArgs())
2554 mangleTemplateArgs(ULE->getExplicitTemplateArgs());
John McCall1dd73832010-02-04 01:42:13 +00002555 break;
2556 }
2557
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002558 case Expr::CXXUnresolvedConstructExprClass: {
John McCall1dd73832010-02-04 01:42:13 +00002559 const CXXUnresolvedConstructExpr *CE = cast<CXXUnresolvedConstructExpr>(E);
2560 unsigned N = CE->arg_size();
2561
2562 Out << "cv";
2563 mangleType(CE->getType());
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002564 if (N != 1) Out << '_';
John McCall1dd73832010-02-04 01:42:13 +00002565 for (unsigned I = 0; I != N; ++I) mangleExpression(CE->getArg(I));
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002566 if (N != 1) Out << 'E';
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002567 break;
John McCall1dd73832010-02-04 01:42:13 +00002568 }
John McCall09cc1412010-02-03 00:55:45 +00002569
John McCall1dd73832010-02-04 01:42:13 +00002570 case Expr::CXXTemporaryObjectExprClass:
2571 case Expr::CXXConstructExprClass: {
2572 const CXXConstructExpr *CE = cast<CXXConstructExpr>(E);
2573 unsigned N = CE->getNumArgs();
2574
Sebastian Redlfaf4ef62012-02-25 22:59:28 +00002575 // Proposal by Jason Merrill, 2012-01-03
2576 if (CE->isListInitialization())
2577 Out << "tl";
2578 else
2579 Out << "cv";
John McCall1dd73832010-02-04 01:42:13 +00002580 mangleType(CE->getType());
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002581 if (N != 1) Out << '_';
John McCall1dd73832010-02-04 01:42:13 +00002582 for (unsigned I = 0; I != N; ++I) mangleExpression(CE->getArg(I));
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002583 if (N != 1) Out << 'E';
John McCall09cc1412010-02-03 00:55:45 +00002584 break;
John McCall1dd73832010-02-04 01:42:13 +00002585 }
2586
Richard Smith41576d42012-02-06 02:54:51 +00002587 case Expr::CXXScalarValueInitExprClass:
2588 Out <<"cv";
2589 mangleType(E->getType());
2590 Out <<"_E";
2591 break;
2592
John McCall9653ab52012-09-25 09:10:17 +00002593 case Expr::CXXNoexceptExprClass:
2594 Out << "nx";
2595 mangleExpression(cast<CXXNoexceptExpr>(E)->getOperand());
2596 break;
2597
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +00002598 case Expr::UnaryExprOrTypeTraitExprClass: {
2599 const UnaryExprOrTypeTraitExpr *SAE = cast<UnaryExprOrTypeTraitExpr>(E);
Douglas Gregoredee94b2011-07-12 04:47:20 +00002600
2601 if (!SAE->isInstantiationDependent()) {
2602 // Itanium C++ ABI:
2603 // If the operand of a sizeof or alignof operator is not
2604 // instantiation-dependent it is encoded as an integer literal
2605 // reflecting the result of the operator.
2606 //
2607 // If the result of the operator is implicitly converted to a known
2608 // integer type, that type is used for the literal; otherwise, the type
2609 // of std::size_t or std::ptrdiff_t is used.
2610 QualType T = (ImplicitlyConvertedToType.isNull() ||
2611 !ImplicitlyConvertedToType->isIntegerType())? SAE->getType()
2612 : ImplicitlyConvertedToType;
Richard Smitha6b8b2c2011-10-10 18:28:20 +00002613 llvm::APSInt V = SAE->EvaluateKnownConstInt(Context.getASTContext());
2614 mangleIntegerLiteral(T, V);
Douglas Gregoredee94b2011-07-12 04:47:20 +00002615 break;
2616 }
2617
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +00002618 switch(SAE->getKind()) {
2619 case UETT_SizeOf:
2620 Out << 's';
2621 break;
2622 case UETT_AlignOf:
2623 Out << 'a';
2624 break;
2625 case UETT_VecStep:
David Blaikied6471f72011-09-25 23:23:43 +00002626 DiagnosticsEngine &Diags = Context.getDiags();
2627 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +00002628 "cannot yet mangle vec_step expression");
2629 Diags.Report(DiagID);
2630 return;
2631 }
John McCall1dd73832010-02-04 01:42:13 +00002632 if (SAE->isArgumentType()) {
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002633 Out << 't';
John McCall1dd73832010-02-04 01:42:13 +00002634 mangleType(SAE->getArgumentType());
2635 } else {
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002636 Out << 'z';
John McCall1dd73832010-02-04 01:42:13 +00002637 mangleExpression(SAE->getArgumentExpr());
2638 }
2639 break;
2640 }
Anders Carlssona7694082009-11-06 02:50:19 +00002641
John McCall0512e482010-07-14 04:20:34 +00002642 case Expr::CXXThrowExprClass: {
2643 const CXXThrowExpr *TE = cast<CXXThrowExpr>(E);
2644
2645 // Proposal from David Vandervoorde, 2010.06.30
2646 if (TE->getSubExpr()) {
2647 Out << "tw";
2648 mangleExpression(TE->getSubExpr());
2649 } else {
2650 Out << "tr";
2651 }
2652 break;
2653 }
2654
2655 case Expr::CXXTypeidExprClass: {
2656 const CXXTypeidExpr *TIE = cast<CXXTypeidExpr>(E);
2657
2658 // Proposal from David Vandervoorde, 2010.06.30
2659 if (TIE->isTypeOperand()) {
2660 Out << "ti";
2661 mangleType(TIE->getTypeOperand());
2662 } else {
2663 Out << "te";
2664 mangleExpression(TIE->getExprOperand());
2665 }
2666 break;
2667 }
2668
2669 case Expr::CXXDeleteExprClass: {
2670 const CXXDeleteExpr *DE = cast<CXXDeleteExpr>(E);
2671
2672 // Proposal from David Vandervoorde, 2010.06.30
2673 if (DE->isGlobalDelete()) Out << "gs";
2674 Out << (DE->isArrayForm() ? "da" : "dl");
2675 mangleExpression(DE->getArgument());
2676 break;
2677 }
2678
Anders Carlssone170ba72009-12-14 01:45:37 +00002679 case Expr::UnaryOperatorClass: {
2680 const UnaryOperator *UO = cast<UnaryOperator>(E);
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002681 mangleOperatorName(UnaryOperator::getOverloadedOperator(UO->getOpcode()),
Anders Carlssone170ba72009-12-14 01:45:37 +00002682 /*Arity=*/1);
2683 mangleExpression(UO->getSubExpr());
2684 break;
2685 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002686
John McCall0512e482010-07-14 04:20:34 +00002687 case Expr::ArraySubscriptExprClass: {
2688 const ArraySubscriptExpr *AE = cast<ArraySubscriptExpr>(E);
2689
Chris Lattnerfc8f0e12011-04-15 05:22:18 +00002690 // Array subscript is treated as a syntactically weird form of
John McCall0512e482010-07-14 04:20:34 +00002691 // binary operator.
2692 Out << "ix";
2693 mangleExpression(AE->getLHS());
2694 mangleExpression(AE->getRHS());
2695 break;
2696 }
2697
2698 case Expr::CompoundAssignOperatorClass: // fallthrough
Anders Carlssone170ba72009-12-14 01:45:37 +00002699 case Expr::BinaryOperatorClass: {
2700 const BinaryOperator *BO = cast<BinaryOperator>(E);
Douglas Gregor63f62df2011-06-05 05:27:58 +00002701 if (BO->getOpcode() == BO_PtrMemD)
2702 Out << "ds";
2703 else
2704 mangleOperatorName(BinaryOperator::getOverloadedOperator(BO->getOpcode()),
2705 /*Arity=*/2);
Anders Carlssone170ba72009-12-14 01:45:37 +00002706 mangleExpression(BO->getLHS());
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002707 mangleExpression(BO->getRHS());
Anders Carlssone170ba72009-12-14 01:45:37 +00002708 break;
John McCall2f27bf82010-02-04 02:56:29 +00002709 }
Anders Carlssone170ba72009-12-14 01:45:37 +00002710
2711 case Expr::ConditionalOperatorClass: {
2712 const ConditionalOperator *CO = cast<ConditionalOperator>(E);
2713 mangleOperatorName(OO_Conditional, /*Arity=*/3);
2714 mangleExpression(CO->getCond());
John McCall5e1e89b2010-08-18 19:18:59 +00002715 mangleExpression(CO->getLHS(), Arity);
2716 mangleExpression(CO->getRHS(), Arity);
Anders Carlssone170ba72009-12-14 01:45:37 +00002717 break;
2718 }
2719
Douglas Gregor46287c72010-01-29 16:37:09 +00002720 case Expr::ImplicitCastExprClass: {
Douglas Gregoredee94b2011-07-12 04:47:20 +00002721 ImplicitlyConvertedToType = E->getType();
2722 E = cast<ImplicitCastExpr>(E)->getSubExpr();
2723 goto recurse;
Douglas Gregor46287c72010-01-29 16:37:09 +00002724 }
John McCallf85e1932011-06-15 23:02:42 +00002725
2726 case Expr::ObjCBridgedCastExprClass: {
2727 // Mangle ownership casts as a vendor extended operator __bridge,
2728 // __bridge_transfer, or __bridge_retain.
Chris Lattner5f9e2722011-07-23 10:55:15 +00002729 StringRef Kind = cast<ObjCBridgedCastExpr>(E)->getBridgeKindName();
John McCallf85e1932011-06-15 23:02:42 +00002730 Out << "v1U" << Kind.size() << Kind;
2731 }
2732 // Fall through to mangle the cast itself.
2733
Douglas Gregor46287c72010-01-29 16:37:09 +00002734 case Expr::CStyleCastExprClass:
2735 case Expr::CXXStaticCastExprClass:
2736 case Expr::CXXDynamicCastExprClass:
2737 case Expr::CXXReinterpretCastExprClass:
2738 case Expr::CXXConstCastExprClass:
2739 case Expr::CXXFunctionalCastExprClass: {
2740 const ExplicitCastExpr *ECE = cast<ExplicitCastExpr>(E);
2741 Out << "cv";
2742 mangleType(ECE->getType());
2743 mangleExpression(ECE->getSubExpr());
2744 break;
2745 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002746
Anders Carlsson58040a52009-12-16 05:48:46 +00002747 case Expr::CXXOperatorCallExprClass: {
2748 const CXXOperatorCallExpr *CE = cast<CXXOperatorCallExpr>(E);
2749 unsigned NumArgs = CE->getNumArgs();
2750 mangleOperatorName(CE->getOperator(), /*Arity=*/NumArgs);
2751 // Mangle the arguments.
2752 for (unsigned i = 0; i != NumArgs; ++i)
2753 mangleExpression(CE->getArg(i));
2754 break;
2755 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002756
Anders Carlssona7694082009-11-06 02:50:19 +00002757 case Expr::ParenExprClass:
John McCall5e1e89b2010-08-18 19:18:59 +00002758 mangleExpression(cast<ParenExpr>(E)->getSubExpr(), Arity);
Anders Carlssona7694082009-11-06 02:50:19 +00002759 break;
2760
Anders Carlssond553f8c2009-09-21 01:21:10 +00002761 case Expr::DeclRefExprClass: {
Douglas Gregor5ed1bc32010-02-28 21:40:32 +00002762 const NamedDecl *D = cast<DeclRefExpr>(E)->getDecl();
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002763
Anders Carlssond553f8c2009-09-21 01:21:10 +00002764 switch (D->getKind()) {
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002765 default:
Douglas Gregor5ed1bc32010-02-28 21:40:32 +00002766 // <expr-primary> ::= L <mangled-name> E # external name
2767 Out << 'L';
2768 mangle(D, "_Z");
2769 Out << 'E';
2770 break;
2771
John McCallfb44de92011-05-01 22:35:37 +00002772 case Decl::ParmVar:
2773 mangleFunctionParam(cast<ParmVarDecl>(D));
2774 break;
2775
John McCall3dc7e7b2010-07-24 01:17:35 +00002776 case Decl::EnumConstant: {
2777 const EnumConstantDecl *ED = cast<EnumConstantDecl>(D);
2778 mangleIntegerLiteral(ED->getType(), ED->getInitVal());
2779 break;
2780 }
2781
Anders Carlssond553f8c2009-09-21 01:21:10 +00002782 case Decl::NonTypeTemplateParm: {
2783 const NonTypeTemplateParmDecl *PD = cast<NonTypeTemplateParmDecl>(D);
Anders Carlsson0ccdf8d2009-09-27 00:38:53 +00002784 mangleTemplateParameter(PD->getIndex());
Anders Carlssond553f8c2009-09-21 01:21:10 +00002785 break;
2786 }
2787
2788 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002789
Anders Carlsson50755b02009-09-27 20:11:34 +00002790 break;
Anders Carlssond553f8c2009-09-21 01:21:10 +00002791 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002792
Douglas Gregorc7793c72011-01-15 01:15:58 +00002793 case Expr::SubstNonTypeTemplateParmPackExprClass:
John McCall68a51a72011-07-01 00:04:39 +00002794 // FIXME: not clear how to mangle this!
2795 // template <unsigned N...> class A {
2796 // template <class U...> void foo(U (&x)[N]...);
2797 // };
2798 Out << "_SUBSTPACK_";
Douglas Gregorc7793c72011-01-15 01:15:58 +00002799 break;
Richard Smith9a4db032012-09-12 00:56:43 +00002800
2801 case Expr::FunctionParmPackExprClass: {
2802 // FIXME: not clear how to mangle this!
2803 const FunctionParmPackExpr *FPPE = cast<FunctionParmPackExpr>(E);
2804 Out << "v110_SUBSTPACK";
2805 mangleFunctionParam(FPPE->getParameterPack());
2806 break;
2807 }
2808
John McCall865d4472009-11-19 22:55:06 +00002809 case Expr::DependentScopeDeclRefExprClass: {
2810 const DependentScopeDeclRefExpr *DRE = cast<DependentScopeDeclRefExpr>(E);
John McCall26a6ec72011-06-21 22:12:46 +00002811 mangleUnresolvedName(DRE->getQualifier(), 0, DRE->getDeclName(), Arity);
Douglas Gregor4b2ccfc2010-02-28 22:05:49 +00002812
John McCall26a6ec72011-06-21 22:12:46 +00002813 // All the <unresolved-name> productions end in a
2814 // base-unresolved-name, where <template-args> are just tacked
2815 // onto the end.
John McCall6dbce192010-08-20 00:17:19 +00002816 if (DRE->hasExplicitTemplateArgs())
2817 mangleTemplateArgs(DRE->getExplicitTemplateArgs());
Anders Carlsson50755b02009-09-27 20:11:34 +00002818 break;
2819 }
2820
John McCalld9307602010-04-09 22:54:09 +00002821 case Expr::CXXBindTemporaryExprClass:
2822 mangleExpression(cast<CXXBindTemporaryExpr>(E)->getSubExpr());
2823 break;
2824
John McCall4765fa02010-12-06 08:20:24 +00002825 case Expr::ExprWithCleanupsClass:
2826 mangleExpression(cast<ExprWithCleanups>(E)->getSubExpr(), Arity);
John McCalld9307602010-04-09 22:54:09 +00002827 break;
2828
John McCall1dd73832010-02-04 01:42:13 +00002829 case Expr::FloatingLiteralClass: {
2830 const FloatingLiteral *FL = cast<FloatingLiteral>(E);
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002831 Out << 'L';
John McCall1dd73832010-02-04 01:42:13 +00002832 mangleType(FL->getType());
John McCall0512e482010-07-14 04:20:34 +00002833 mangleFloat(FL->getValue());
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002834 Out << 'E';
John McCall1dd73832010-02-04 01:42:13 +00002835 break;
2836 }
2837
John McCallde810632010-04-09 21:48:08 +00002838 case Expr::CharacterLiteralClass:
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002839 Out << 'L';
John McCallde810632010-04-09 21:48:08 +00002840 mangleType(E->getType());
2841 Out << cast<CharacterLiteral>(E)->getValue();
2842 Out << 'E';
2843 break;
2844
Ted Kremenekebcb57a2012-03-06 20:05:56 +00002845 // FIXME. __objc_yes/__objc_no are mangled same as true/false
2846 case Expr::ObjCBoolLiteralExprClass:
2847 Out << "Lb";
2848 Out << (cast<ObjCBoolLiteralExpr>(E)->getValue() ? '1' : '0');
2849 Out << 'E';
2850 break;
2851
John McCallde810632010-04-09 21:48:08 +00002852 case Expr::CXXBoolLiteralExprClass:
2853 Out << "Lb";
2854 Out << (cast<CXXBoolLiteralExpr>(E)->getValue() ? '1' : '0');
2855 Out << 'E';
2856 break;
2857
John McCall0512e482010-07-14 04:20:34 +00002858 case Expr::IntegerLiteralClass: {
2859 llvm::APSInt Value(cast<IntegerLiteral>(E)->getValue());
2860 if (E->getType()->isSignedIntegerType())
2861 Value.setIsSigned(true);
2862 mangleIntegerLiteral(E->getType(), Value);
Anders Carlssone170ba72009-12-14 01:45:37 +00002863 break;
John McCall0512e482010-07-14 04:20:34 +00002864 }
2865
2866 case Expr::ImaginaryLiteralClass: {
2867 const ImaginaryLiteral *IE = cast<ImaginaryLiteral>(E);
2868 // Mangle as if a complex literal.
Nick Lewycky271b6652010-09-05 03:40:33 +00002869 // Proposal from David Vandevoorde, 2010.06.30.
John McCall0512e482010-07-14 04:20:34 +00002870 Out << 'L';
2871 mangleType(E->getType());
2872 if (const FloatingLiteral *Imag =
2873 dyn_cast<FloatingLiteral>(IE->getSubExpr())) {
2874 // Mangle a floating-point zero of the appropriate type.
2875 mangleFloat(llvm::APFloat(Imag->getValue().getSemantics()));
2876 Out << '_';
2877 mangleFloat(Imag->getValue());
2878 } else {
Nick Lewycky271b6652010-09-05 03:40:33 +00002879 Out << "0_";
John McCall0512e482010-07-14 04:20:34 +00002880 llvm::APSInt Value(cast<IntegerLiteral>(IE->getSubExpr())->getValue());
2881 if (IE->getSubExpr()->getType()->isSignedIntegerType())
2882 Value.setIsSigned(true);
2883 mangleNumber(Value);
2884 }
2885 Out << 'E';
2886 break;
2887 }
2888
2889 case Expr::StringLiteralClass: {
John McCall1658c392010-07-15 21:53:03 +00002890 // Revised proposal from David Vandervoorde, 2010.07.15.
John McCall0512e482010-07-14 04:20:34 +00002891 Out << 'L';
John McCall1658c392010-07-15 21:53:03 +00002892 assert(isa<ConstantArrayType>(E->getType()));
2893 mangleType(E->getType());
John McCall0512e482010-07-14 04:20:34 +00002894 Out << 'E';
2895 break;
2896 }
2897
2898 case Expr::GNUNullExprClass:
2899 // FIXME: should this really be mangled the same as nullptr?
2900 // fallthrough
2901
2902 case Expr::CXXNullPtrLiteralExprClass: {
2903 // Proposal from David Vandervoorde, 2010.06.30, as
2904 // modified by ABI list discussion.
2905 Out << "LDnE";
2906 break;
2907 }
Douglas Gregorbe230c32011-01-03 17:17:50 +00002908
2909 case Expr::PackExpansionExprClass:
2910 Out << "sp";
2911 mangleExpression(cast<PackExpansionExpr>(E)->getPattern());
2912 break;
Douglas Gregor2e774c42011-01-04 18:56:13 +00002913
2914 case Expr::SizeOfPackExprClass: {
Douglas Gregor2e774c42011-01-04 18:56:13 +00002915 Out << "sZ";
2916 const NamedDecl *Pack = cast<SizeOfPackExpr>(E)->getPack();
2917 if (const TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(Pack))
2918 mangleTemplateParameter(TTP->getIndex());
2919 else if (const NonTypeTemplateParmDecl *NTTP
2920 = dyn_cast<NonTypeTemplateParmDecl>(Pack))
2921 mangleTemplateParameter(NTTP->getIndex());
2922 else if (const TemplateTemplateParmDecl *TempTP
2923 = dyn_cast<TemplateTemplateParmDecl>(Pack))
2924 mangleTemplateParameter(TempTP->getIndex());
Douglas Gregor91832362011-07-12 07:03:48 +00002925 else
2926 mangleFunctionParam(cast<ParmVarDecl>(Pack));
Douglas Gregordfbbcf92011-03-03 02:20:19 +00002927 break;
Douglas Gregor2e774c42011-01-04 18:56:13 +00002928 }
Douglas Gregor03e80032011-06-21 17:03:29 +00002929
2930 case Expr::MaterializeTemporaryExprClass: {
2931 mangleExpression(cast<MaterializeTemporaryExpr>(E)->GetTemporaryExpr());
2932 break;
2933 }
Douglas Gregorcefc3af2012-04-16 07:05:22 +00002934
2935 case Expr::CXXThisExprClass:
2936 Out << "fpT";
2937 break;
Anders Carlssond553f8c2009-09-21 01:21:10 +00002938 }
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00002939}
2940
John McCallfb44de92011-05-01 22:35:37 +00002941/// Mangle an expression which refers to a parameter variable.
2942///
2943/// <expression> ::= <function-param>
2944/// <function-param> ::= fp <top-level CV-qualifiers> _ # L == 0, I == 0
2945/// <function-param> ::= fp <top-level CV-qualifiers>
2946/// <parameter-2 non-negative number> _ # L == 0, I > 0
2947/// <function-param> ::= fL <L-1 non-negative number>
2948/// p <top-level CV-qualifiers> _ # L > 0, I == 0
2949/// <function-param> ::= fL <L-1 non-negative number>
2950/// p <top-level CV-qualifiers>
2951/// <I-1 non-negative number> _ # L > 0, I > 0
2952///
2953/// L is the nesting depth of the parameter, defined as 1 if the
2954/// parameter comes from the innermost function prototype scope
2955/// enclosing the current context, 2 if from the next enclosing
2956/// function prototype scope, and so on, with one special case: if
2957/// we've processed the full parameter clause for the innermost
2958/// function type, then L is one less. This definition conveniently
2959/// makes it irrelevant whether a function's result type was written
2960/// trailing or leading, but is otherwise overly complicated; the
2961/// numbering was first designed without considering references to
2962/// parameter in locations other than return types, and then the
2963/// mangling had to be generalized without changing the existing
2964/// manglings.
2965///
2966/// I is the zero-based index of the parameter within its parameter
2967/// declaration clause. Note that the original ABI document describes
2968/// this using 1-based ordinals.
2969void CXXNameMangler::mangleFunctionParam(const ParmVarDecl *parm) {
2970 unsigned parmDepth = parm->getFunctionScopeDepth();
2971 unsigned parmIndex = parm->getFunctionScopeIndex();
2972
2973 // Compute 'L'.
2974 // parmDepth does not include the declaring function prototype.
2975 // FunctionTypeDepth does account for that.
2976 assert(parmDepth < FunctionTypeDepth.getDepth());
2977 unsigned nestingDepth = FunctionTypeDepth.getDepth() - parmDepth;
2978 if (FunctionTypeDepth.isInResultType())
2979 nestingDepth--;
2980
2981 if (nestingDepth == 0) {
2982 Out << "fp";
2983 } else {
2984 Out << "fL" << (nestingDepth - 1) << 'p';
2985 }
2986
2987 // Top-level qualifiers. We don't have to worry about arrays here,
2988 // because parameters declared as arrays should already have been
Benjamin Kramer48d798c2012-06-02 10:20:41 +00002989 // transformed to have pointer type. FIXME: apparently these don't
John McCallfb44de92011-05-01 22:35:37 +00002990 // get mangled if used as an rvalue of a known non-class type?
2991 assert(!parm->getType()->isArrayType()
2992 && "parameter's type is still an array type?");
2993 mangleQualifiers(parm->getType().getQualifiers());
2994
2995 // Parameter index.
2996 if (parmIndex != 0) {
2997 Out << (parmIndex - 1);
2998 }
2999 Out << '_';
3000}
3001
Anders Carlsson3ac86b52009-04-15 05:36:58 +00003002void CXXNameMangler::mangleCXXCtorType(CXXCtorType T) {
3003 // <ctor-dtor-name> ::= C1 # complete object constructor
3004 // ::= C2 # base object constructor
3005 // ::= C3 # complete object allocating constructor
3006 //
3007 switch (T) {
3008 case Ctor_Complete:
3009 Out << "C1";
3010 break;
3011 case Ctor_Base:
3012 Out << "C2";
3013 break;
3014 case Ctor_CompleteAllocating:
3015 Out << "C3";
3016 break;
3017 }
3018}
3019
Anders Carlsson27ae5362009-04-17 01:58:57 +00003020void CXXNameMangler::mangleCXXDtorType(CXXDtorType T) {
3021 // <ctor-dtor-name> ::= D0 # deleting destructor
3022 // ::= D1 # complete object destructor
3023 // ::= D2 # base object destructor
3024 //
3025 switch (T) {
3026 case Dtor_Deleting:
3027 Out << "D0";
3028 break;
3029 case Dtor_Complete:
3030 Out << "D1";
3031 break;
3032 case Dtor_Base:
3033 Out << "D2";
3034 break;
3035 }
3036}
3037
John McCall6dbce192010-08-20 00:17:19 +00003038void CXXNameMangler::mangleTemplateArgs(
Argyrios Kyrtzidisb0c3e092011-09-22 20:07:03 +00003039 const ASTTemplateArgumentListInfo &TemplateArgs) {
John McCall6dbce192010-08-20 00:17:19 +00003040 // <template-args> ::= I <template-arg>+ E
3041 Out << 'I';
John McCall4f4e4132011-05-04 01:45:19 +00003042 for (unsigned i = 0, e = TemplateArgs.NumTemplateArgs; i != e; ++i)
Eli Friedmand7a6b162012-09-26 02:36:12 +00003043 mangleTemplateArg(TemplateArgs.getTemplateArgs()[i].getArgument());
John McCall6dbce192010-08-20 00:17:19 +00003044 Out << 'E';
3045}
3046
Eli Friedmand7a6b162012-09-26 02:36:12 +00003047void CXXNameMangler::mangleTemplateArgs(const TemplateArgumentList &AL) {
Anders Carlsson7a0ba872009-05-15 16:09:15 +00003048 // <template-args> ::= I <template-arg>+ E
Benjamin Kramer35f59b62010-04-10 16:03:31 +00003049 Out << 'I';
Rafael Espindolad9800722010-03-11 14:07:00 +00003050 for (unsigned i = 0, e = AL.size(); i != e; ++i)
Eli Friedmand7a6b162012-09-26 02:36:12 +00003051 mangleTemplateArg(AL[i]);
Benjamin Kramer35f59b62010-04-10 16:03:31 +00003052 Out << 'E';
Anders Carlsson7a0ba872009-05-15 16:09:15 +00003053}
3054
Eli Friedmand7a6b162012-09-26 02:36:12 +00003055void CXXNameMangler::mangleTemplateArgs(const TemplateArgument *TemplateArgs,
Anders Carlsson7624f212009-09-18 02:42:01 +00003056 unsigned NumTemplateArgs) {
3057 // <template-args> ::= I <template-arg>+ E
Benjamin Kramer35f59b62010-04-10 16:03:31 +00003058 Out << 'I';
Daniel Dunbar7e0c1952009-11-21 09:17:15 +00003059 for (unsigned i = 0; i != NumTemplateArgs; ++i)
Eli Friedmand7a6b162012-09-26 02:36:12 +00003060 mangleTemplateArg(TemplateArgs[i]);
Benjamin Kramer35f59b62010-04-10 16:03:31 +00003061 Out << 'E';
Anders Carlsson7624f212009-09-18 02:42:01 +00003062}
3063
Eli Friedmand7a6b162012-09-26 02:36:12 +00003064void CXXNameMangler::mangleTemplateArg(TemplateArgument A) {
Mike Stump1eb44332009-09-09 15:08:12 +00003065 // <template-arg> ::= <type> # type or template
Anders Carlsson7a0ba872009-05-15 16:09:15 +00003066 // ::= X <expression> E # expression
3067 // ::= <expr-primary> # simple expressions
Douglas Gregor4fc48662011-01-13 16:39:34 +00003068 // ::= J <template-arg>* E # argument pack
Douglas Gregorf1588662011-07-12 15:18:55 +00003069 // ::= sp <expression> # pack expansion of (C++0x)
3070 if (!A.isInstantiationDependent() || A.isDependent())
3071 A = Context.getASTContext().getCanonicalTemplateArgument(A);
3072
Anders Carlsson7a0ba872009-05-15 16:09:15 +00003073 switch (A.getKind()) {
Douglas Gregorf90b27a2011-01-03 22:36:02 +00003074 case TemplateArgument::Null:
3075 llvm_unreachable("Cannot mangle NULL template argument");
3076
Anders Carlsson7a0ba872009-05-15 16:09:15 +00003077 case TemplateArgument::Type:
3078 mangleType(A.getAsType());
3079 break;
Anders Carlsson9e85c742009-12-23 19:30:55 +00003080 case TemplateArgument::Template:
John McCallb6f532e2010-07-14 06:43:17 +00003081 // This is mangled as <type>.
3082 mangleType(A.getAsTemplate());
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00003083 break;
Douglas Gregora7fc9012011-01-05 18:58:31 +00003084 case TemplateArgument::TemplateExpansion:
Douglas Gregor4fc48662011-01-13 16:39:34 +00003085 // <type> ::= Dp <type> # pack expansion (C++0x)
Douglas Gregora7fc9012011-01-05 18:58:31 +00003086 Out << "Dp";
3087 mangleType(A.getAsTemplateOrTemplatePattern());
3088 break;
John McCall092beef2012-01-06 05:06:35 +00003089 case TemplateArgument::Expression: {
3090 // It's possible to end up with a DeclRefExpr here in certain
3091 // dependent cases, in which case we should mangle as a
3092 // declaration.
3093 const Expr *E = A.getAsExpr()->IgnoreParens();
3094 if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E)) {
3095 const ValueDecl *D = DRE->getDecl();
3096 if (isa<VarDecl>(D) || isa<FunctionDecl>(D)) {
3097 Out << "L";
3098 mangle(D, "_Z");
3099 Out << 'E';
3100 break;
3101 }
3102 }
3103
Anders Carlssond553f8c2009-09-21 01:21:10 +00003104 Out << 'X';
John McCall092beef2012-01-06 05:06:35 +00003105 mangleExpression(E);
Anders Carlssond553f8c2009-09-21 01:21:10 +00003106 Out << 'E';
3107 break;
John McCall092beef2012-01-06 05:06:35 +00003108 }
Anders Carlssone170ba72009-12-14 01:45:37 +00003109 case TemplateArgument::Integral:
Benjamin Kramer85524372012-06-07 15:09:51 +00003110 mangleIntegerLiteral(A.getIntegralType(), A.getAsIntegral());
Anders Carlsson7a0ba872009-05-15 16:09:15 +00003111 break;
Daniel Dunbar7e0c1952009-11-21 09:17:15 +00003112 case TemplateArgument::Declaration: {
3113 // <expr-primary> ::= L <mangled-name> E # external name
Rafael Espindolad9800722010-03-11 14:07:00 +00003114 // Clang produces AST's where pointer-to-member-function expressions
Daniel Dunbar7e0c1952009-11-21 09:17:15 +00003115 // and pointer-to-function expressions are represented as a declaration not
Rafael Espindolad9800722010-03-11 14:07:00 +00003116 // an expression. We compensate for it here to produce the correct mangling.
Eli Friedmand7a6b162012-09-26 02:36:12 +00003117 ValueDecl *D = A.getAsDecl();
3118 bool compensateMangling = !A.isDeclForReferenceParam();
Rafael Espindolad9800722010-03-11 14:07:00 +00003119 if (compensateMangling) {
3120 Out << 'X';
3121 mangleOperatorName(OO_Amp, 1);
3122 }
3123
Daniel Dunbar7e0c1952009-11-21 09:17:15 +00003124 Out << 'L';
3125 // References to external entities use the mangled name; if the name would
3126 // not normally be manged then mangle it as unqualified.
3127 //
3128 // FIXME: The ABI specifies that external names here should have _Z, but
3129 // gcc leaves this off.
Rafael Espindolad9800722010-03-11 14:07:00 +00003130 if (compensateMangling)
3131 mangle(D, "_Z");
3132 else
3133 mangle(D, "Z");
Daniel Dunbar7e0c1952009-11-21 09:17:15 +00003134 Out << 'E';
Rafael Espindolad9800722010-03-11 14:07:00 +00003135
3136 if (compensateMangling)
3137 Out << 'E';
3138
Daniel Dunbar7e0c1952009-11-21 09:17:15 +00003139 break;
3140 }
Eli Friedmand7a6b162012-09-26 02:36:12 +00003141 case TemplateArgument::NullPtr: {
3142 // <expr-primary> ::= L <type> 0 E
3143 Out << 'L';
3144 mangleType(A.getNullPtrType());
3145 Out << "0E";
3146 break;
3147 }
Douglas Gregorf90b27a2011-01-03 22:36:02 +00003148 case TemplateArgument::Pack: {
3149 // Note: proposal by Mike Herrick on 12/20/10
3150 Out << 'J';
3151 for (TemplateArgument::pack_iterator PA = A.pack_begin(),
3152 PAEnd = A.pack_end();
3153 PA != PAEnd; ++PA)
Eli Friedmand7a6b162012-09-26 02:36:12 +00003154 mangleTemplateArg(*PA);
Douglas Gregorf90b27a2011-01-03 22:36:02 +00003155 Out << 'E';
3156 }
Daniel Dunbar7e0c1952009-11-21 09:17:15 +00003157 }
Anders Carlsson7a0ba872009-05-15 16:09:15 +00003158}
3159
Anders Carlsson0ccdf8d2009-09-27 00:38:53 +00003160void CXXNameMangler::mangleTemplateParameter(unsigned Index) {
3161 // <template-param> ::= T_ # first template parameter
3162 // ::= T <parameter-2 non-negative number> _
3163 if (Index == 0)
3164 Out << "T_";
3165 else
3166 Out << 'T' << (Index - 1) << '_';
3167}
3168
John McCall68a51a72011-07-01 00:04:39 +00003169void CXXNameMangler::mangleExistingSubstitution(QualType type) {
3170 bool result = mangleSubstitution(type);
3171 assert(result && "no existing substitution for type");
3172 (void) result;
3173}
3174
3175void CXXNameMangler::mangleExistingSubstitution(TemplateName tname) {
3176 bool result = mangleSubstitution(tname);
3177 assert(result && "no existing substitution for template name");
3178 (void) result;
3179}
3180
Anders Carlsson76967372009-09-17 00:43:46 +00003181// <substitution> ::= S <seq-id> _
3182// ::= S_
Anders Carlsson6862fc72009-09-17 04:16:28 +00003183bool CXXNameMangler::mangleSubstitution(const NamedDecl *ND) {
Anders Carlssone7c8cb62009-09-26 20:53:44 +00003184 // Try one of the standard substitutions first.
3185 if (mangleStandardSubstitution(ND))
3186 return true;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00003187
Anders Carlsson433d1372009-11-07 04:26:04 +00003188 ND = cast<NamedDecl>(ND->getCanonicalDecl());
Anders Carlsson6862fc72009-09-17 04:16:28 +00003189 return mangleSubstitution(reinterpret_cast<uintptr_t>(ND));
3190}
3191
Douglas Gregor14795c82011-12-03 18:24:43 +00003192/// \brief Determine whether the given type has any qualifiers that are
3193/// relevant for substitutions.
3194static bool hasMangledSubstitutionQualifiers(QualType T) {
3195 Qualifiers Qs = T.getQualifiers();
3196 return Qs.getCVRQualifiers() || Qs.hasAddressSpace();
3197}
3198
Anders Carlsson76967372009-09-17 00:43:46 +00003199bool CXXNameMangler::mangleSubstitution(QualType T) {
Douglas Gregor14795c82011-12-03 18:24:43 +00003200 if (!hasMangledSubstitutionQualifiers(T)) {
Anders Carlssond99edc42009-09-26 03:55:37 +00003201 if (const RecordType *RT = T->getAs<RecordType>())
3202 return mangleSubstitution(RT->getDecl());
3203 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00003204
Anders Carlsson76967372009-09-17 00:43:46 +00003205 uintptr_t TypePtr = reinterpret_cast<uintptr_t>(T.getAsOpaquePtr());
3206
Anders Carlssond3a932a2009-09-17 03:53:28 +00003207 return mangleSubstitution(TypePtr);
3208}
3209
Douglas Gregor1e9268e2010-04-28 05:58:56 +00003210bool CXXNameMangler::mangleSubstitution(TemplateName Template) {
3211 if (TemplateDecl *TD = Template.getAsTemplateDecl())
3212 return mangleSubstitution(TD);
Sean Huntc3021132010-05-05 15:23:54 +00003213
Douglas Gregor1e9268e2010-04-28 05:58:56 +00003214 Template = Context.getASTContext().getCanonicalTemplateName(Template);
3215 return mangleSubstitution(
3216 reinterpret_cast<uintptr_t>(Template.getAsVoidPointer()));
3217}
3218
Anders Carlssond3a932a2009-09-17 03:53:28 +00003219bool CXXNameMangler::mangleSubstitution(uintptr_t Ptr) {
Benjamin Kramer35f59b62010-04-10 16:03:31 +00003220 llvm::DenseMap<uintptr_t, unsigned>::iterator I = Substitutions.find(Ptr);
Anders Carlsson76967372009-09-17 00:43:46 +00003221 if (I == Substitutions.end())
3222 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00003223
Anders Carlsson76967372009-09-17 00:43:46 +00003224 unsigned SeqID = I->second;
3225 if (SeqID == 0)
3226 Out << "S_";
3227 else {
3228 SeqID--;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00003229
Anders Carlsson76967372009-09-17 00:43:46 +00003230 // <seq-id> is encoded in base-36, using digits and upper case letters.
3231 char Buffer[10];
Benjamin Kramer35f59b62010-04-10 16:03:31 +00003232 char *BufferPtr = llvm::array_endof(Buffer);
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00003233
Anders Carlsson76967372009-09-17 00:43:46 +00003234 if (SeqID == 0) *--BufferPtr = '0';
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00003235
Anders Carlsson76967372009-09-17 00:43:46 +00003236 while (SeqID) {
3237 assert(BufferPtr > Buffer && "Buffer overflow!");
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00003238
John McCall6ab30e02010-06-09 07:26:17 +00003239 char c = static_cast<char>(SeqID % 36);
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00003240
Anders Carlsson76967372009-09-17 00:43:46 +00003241 *--BufferPtr = (c < 10 ? '0' + c : 'A' + c - 10);
3242 SeqID /= 36;
3243 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00003244
Benjamin Kramer35f59b62010-04-10 16:03:31 +00003245 Out << 'S'
Chris Lattner5f9e2722011-07-23 10:55:15 +00003246 << StringRef(BufferPtr, llvm::array_endof(Buffer)-BufferPtr)
Benjamin Kramer35f59b62010-04-10 16:03:31 +00003247 << '_';
Anders Carlsson76967372009-09-17 00:43:46 +00003248 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00003249
Anders Carlsson76967372009-09-17 00:43:46 +00003250 return true;
3251}
3252
Anders Carlssonf514b542009-09-27 00:12:57 +00003253static bool isCharType(QualType T) {
3254 if (T.isNull())
3255 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00003256
Anders Carlssonf514b542009-09-27 00:12:57 +00003257 return T->isSpecificBuiltinType(BuiltinType::Char_S) ||
3258 T->isSpecificBuiltinType(BuiltinType::Char_U);
3259}
3260
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00003261/// isCharSpecialization - Returns whether a given type is a template
Anders Carlssonf514b542009-09-27 00:12:57 +00003262/// specialization of a given name with a single argument of type char.
3263static bool isCharSpecialization(QualType T, const char *Name) {
3264 if (T.isNull())
3265 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00003266
Anders Carlssonf514b542009-09-27 00:12:57 +00003267 const RecordType *RT = T->getAs<RecordType>();
3268 if (!RT)
3269 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00003270
3271 const ClassTemplateSpecializationDecl *SD =
Anders Carlssonf514b542009-09-27 00:12:57 +00003272 dyn_cast<ClassTemplateSpecializationDecl>(RT->getDecl());
3273 if (!SD)
3274 return false;
3275
Douglas Gregorccc1b5e2012-02-21 00:37:24 +00003276 if (!isStdNamespace(getEffectiveDeclContext(SD)))
Anders Carlssonf514b542009-09-27 00:12:57 +00003277 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00003278
Anders Carlssonf514b542009-09-27 00:12:57 +00003279 const TemplateArgumentList &TemplateArgs = SD->getTemplateArgs();
3280 if (TemplateArgs.size() != 1)
3281 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00003282
Anders Carlssonf514b542009-09-27 00:12:57 +00003283 if (!isCharType(TemplateArgs[0].getAsType()))
3284 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00003285
Daniel Dunbar01eb9b92009-10-18 21:17:35 +00003286 return SD->getIdentifier()->getName() == Name;
Anders Carlssonf514b542009-09-27 00:12:57 +00003287}
3288
Anders Carlsson91f88602009-12-07 19:56:42 +00003289template <std::size_t StrLen>
Benjamin Kramer54353f42010-11-25 18:29:30 +00003290static bool isStreamCharSpecialization(const ClassTemplateSpecializationDecl*SD,
3291 const char (&Str)[StrLen]) {
Anders Carlsson91f88602009-12-07 19:56:42 +00003292 if (!SD->getIdentifier()->isStr(Str))
3293 return false;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00003294
Anders Carlsson91f88602009-12-07 19:56:42 +00003295 const TemplateArgumentList &TemplateArgs = SD->getTemplateArgs();
3296 if (TemplateArgs.size() != 2)
3297 return false;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00003298
Anders Carlsson91f88602009-12-07 19:56:42 +00003299 if (!isCharType(TemplateArgs[0].getAsType()))
3300 return false;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00003301
Anders Carlsson91f88602009-12-07 19:56:42 +00003302 if (!isCharSpecialization(TemplateArgs[1].getAsType(), "char_traits"))
3303 return false;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00003304
Anders Carlsson91f88602009-12-07 19:56:42 +00003305 return true;
3306}
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00003307
Anders Carlssone7c8cb62009-09-26 20:53:44 +00003308bool CXXNameMangler::mangleStandardSubstitution(const NamedDecl *ND) {
3309 // <substitution> ::= St # ::std::
Anders Carlsson8c031552009-09-26 23:10:05 +00003310 if (const NamespaceDecl *NS = dyn_cast<NamespaceDecl>(ND)) {
Anders Carlsson47846d22009-12-04 06:23:23 +00003311 if (isStd(NS)) {
Anders Carlsson8c031552009-09-26 23:10:05 +00003312 Out << "St";
3313 return true;
3314 }
3315 }
3316
3317 if (const ClassTemplateDecl *TD = dyn_cast<ClassTemplateDecl>(ND)) {
Douglas Gregorccc1b5e2012-02-21 00:37:24 +00003318 if (!isStdNamespace(getEffectiveDeclContext(TD)))
Anders Carlsson8c031552009-09-26 23:10:05 +00003319 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00003320
Anders Carlsson8c031552009-09-26 23:10:05 +00003321 // <substitution> ::= Sa # ::std::allocator
3322 if (TD->getIdentifier()->isStr("allocator")) {
3323 Out << "Sa";
3324 return true;
3325 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00003326
Anders Carlsson189d59c2009-09-26 23:14:39 +00003327 // <<substitution> ::= Sb # ::std::basic_string
3328 if (TD->getIdentifier()->isStr("basic_string")) {
3329 Out << "Sb";
3330 return true;
3331 }
Anders Carlsson8c031552009-09-26 23:10:05 +00003332 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00003333
3334 if (const ClassTemplateSpecializationDecl *SD =
Anders Carlssonf514b542009-09-27 00:12:57 +00003335 dyn_cast<ClassTemplateSpecializationDecl>(ND)) {
Douglas Gregorccc1b5e2012-02-21 00:37:24 +00003336 if (!isStdNamespace(getEffectiveDeclContext(SD)))
Eli Friedman5370ee22010-02-23 18:25:09 +00003337 return false;
3338
Anders Carlssonf514b542009-09-27 00:12:57 +00003339 // <substitution> ::= Ss # ::std::basic_string<char,
3340 // ::std::char_traits<char>,
3341 // ::std::allocator<char> >
3342 if (SD->getIdentifier()->isStr("basic_string")) {
3343 const TemplateArgumentList &TemplateArgs = SD->getTemplateArgs();
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00003344
Anders Carlssonf514b542009-09-27 00:12:57 +00003345 if (TemplateArgs.size() != 3)
3346 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00003347
Anders Carlssonf514b542009-09-27 00:12:57 +00003348 if (!isCharType(TemplateArgs[0].getAsType()))
3349 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00003350
Anders Carlssonf514b542009-09-27 00:12:57 +00003351 if (!isCharSpecialization(TemplateArgs[1].getAsType(), "char_traits"))
3352 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00003353
Anders Carlssonf514b542009-09-27 00:12:57 +00003354 if (!isCharSpecialization(TemplateArgs[2].getAsType(), "allocator"))
3355 return false;
3356
3357 Out << "Ss";
3358 return true;
3359 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00003360
Anders Carlsson91f88602009-12-07 19:56:42 +00003361 // <substitution> ::= Si # ::std::basic_istream<char,
3362 // ::std::char_traits<char> >
3363 if (isStreamCharSpecialization(SD, "basic_istream")) {
3364 Out << "Si";
3365 return true;
3366 }
3367
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00003368 // <substitution> ::= So # ::std::basic_ostream<char,
Anders Carlsson8f8fd8e2009-10-08 17:20:26 +00003369 // ::std::char_traits<char> >
Anders Carlsson91f88602009-12-07 19:56:42 +00003370 if (isStreamCharSpecialization(SD, "basic_ostream")) {
Anders Carlsson8f8fd8e2009-10-08 17:20:26 +00003371 Out << "So";
3372 return true;
3373 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00003374
Anders Carlsson91f88602009-12-07 19:56:42 +00003375 // <substitution> ::= Sd # ::std::basic_iostream<char,
3376 // ::std::char_traits<char> >
3377 if (isStreamCharSpecialization(SD, "basic_iostream")) {
3378 Out << "Sd";
3379 return true;
3380 }
Anders Carlssonf514b542009-09-27 00:12:57 +00003381 }
Anders Carlsson8c031552009-09-26 23:10:05 +00003382 return false;
Anders Carlssone7c8cb62009-09-26 20:53:44 +00003383}
3384
Anders Carlsson76967372009-09-17 00:43:46 +00003385void CXXNameMangler::addSubstitution(QualType T) {
Douglas Gregor14795c82011-12-03 18:24:43 +00003386 if (!hasMangledSubstitutionQualifiers(T)) {
Anders Carlssond99edc42009-09-26 03:55:37 +00003387 if (const RecordType *RT = T->getAs<RecordType>()) {
3388 addSubstitution(RT->getDecl());
3389 return;
3390 }
3391 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00003392
Anders Carlsson76967372009-09-17 00:43:46 +00003393 uintptr_t TypePtr = reinterpret_cast<uintptr_t>(T.getAsOpaquePtr());
Anders Carlssond3a932a2009-09-17 03:53:28 +00003394 addSubstitution(TypePtr);
3395}
3396
Douglas Gregor1e9268e2010-04-28 05:58:56 +00003397void CXXNameMangler::addSubstitution(TemplateName Template) {
3398 if (TemplateDecl *TD = Template.getAsTemplateDecl())
3399 return addSubstitution(TD);
Sean Huntc3021132010-05-05 15:23:54 +00003400
Douglas Gregor1e9268e2010-04-28 05:58:56 +00003401 Template = Context.getASTContext().getCanonicalTemplateName(Template);
3402 addSubstitution(reinterpret_cast<uintptr_t>(Template.getAsVoidPointer()));
3403}
3404
Anders Carlssond3a932a2009-09-17 03:53:28 +00003405void CXXNameMangler::addSubstitution(uintptr_t Ptr) {
Anders Carlssond3a932a2009-09-17 03:53:28 +00003406 assert(!Substitutions.count(Ptr) && "Substitution already exists!");
Anders Carlsson9d85b722010-06-02 04:29:50 +00003407 Substitutions[Ptr] = SeqID++;
Anders Carlsson76967372009-09-17 00:43:46 +00003408}
3409
Daniel Dunbar1b077112009-11-21 09:06:10 +00003410//
Mike Stump1eb44332009-09-09 15:08:12 +00003411
Daniel Dunbar1b077112009-11-21 09:06:10 +00003412/// \brief Mangles the name of the declaration D and emits that name to the
3413/// given output stream.
3414///
3415/// If the declaration D requires a mangled name, this routine will emit that
3416/// mangled name to \p os and return true. Otherwise, \p os will be unchanged
3417/// and this routine will return false. In this case, the caller should just
3418/// emit the identifier of the declaration (\c D->getIdentifier()) as its
3419/// name.
Peter Collingbourne14110472011-01-13 18:57:25 +00003420void ItaniumMangleContext::mangleName(const NamedDecl *D,
Chris Lattner5f9e2722011-07-23 10:55:15 +00003421 raw_ostream &Out) {
Daniel Dunbarc02ab4c2009-11-21 09:14:44 +00003422 assert((isa<FunctionDecl>(D) || isa<VarDecl>(D)) &&
3423 "Invalid mangleName() call, argument is not a variable or function!");
3424 assert(!isa<CXXConstructorDecl>(D) && !isa<CXXDestructorDecl>(D) &&
3425 "Invalid mangleName() call on 'structor decl!");
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00003426
Daniel Dunbar1b077112009-11-21 09:06:10 +00003427 PrettyStackTraceDecl CrashInfo(D, SourceLocation(),
3428 getASTContext().getSourceManager(),
3429 "Mangling declaration");
Mike Stump1eb44332009-09-09 15:08:12 +00003430
John McCallfb44de92011-05-01 22:35:37 +00003431 CXXNameMangler Mangler(*this, Out, D);
Daniel Dunbar94fd26d2009-11-21 09:06:22 +00003432 return Mangler.mangle(D);
Daniel Dunbar1b077112009-11-21 09:06:10 +00003433}
Mike Stump1eb44332009-09-09 15:08:12 +00003434
Peter Collingbourne14110472011-01-13 18:57:25 +00003435void ItaniumMangleContext::mangleCXXCtor(const CXXConstructorDecl *D,
3436 CXXCtorType Type,
Chris Lattner5f9e2722011-07-23 10:55:15 +00003437 raw_ostream &Out) {
Rafael Espindolac4850c22011-02-10 23:59:36 +00003438 CXXNameMangler Mangler(*this, Out, D, Type);
Daniel Dunbar77939c92009-11-21 09:06:31 +00003439 Mangler.mangle(D);
Daniel Dunbar1b077112009-11-21 09:06:10 +00003440}
Mike Stump1eb44332009-09-09 15:08:12 +00003441
Peter Collingbourne14110472011-01-13 18:57:25 +00003442void ItaniumMangleContext::mangleCXXDtor(const CXXDestructorDecl *D,
3443 CXXDtorType Type,
Chris Lattner5f9e2722011-07-23 10:55:15 +00003444 raw_ostream &Out) {
Rafael Espindolac4850c22011-02-10 23:59:36 +00003445 CXXNameMangler Mangler(*this, Out, D, Type);
Daniel Dunbar77939c92009-11-21 09:06:31 +00003446 Mangler.mangle(D);
Daniel Dunbar1b077112009-11-21 09:06:10 +00003447}
Mike Stumpf1216772009-07-31 18:25:34 +00003448
Peter Collingbourne14110472011-01-13 18:57:25 +00003449void ItaniumMangleContext::mangleThunk(const CXXMethodDecl *MD,
3450 const ThunkInfo &Thunk,
Chris Lattner5f9e2722011-07-23 10:55:15 +00003451 raw_ostream &Out) {
Anders Carlsson19879c92010-03-23 17:17:29 +00003452 // <special-name> ::= T <call-offset> <base encoding>
3453 // # base is the nominal target function of thunk
3454 // <special-name> ::= Tc <call-offset> <call-offset> <base encoding>
3455 // # base is the nominal target function of thunk
3456 // # first call-offset is 'this' adjustment
3457 // # second call-offset is result adjustment
Sean Huntc3021132010-05-05 15:23:54 +00003458
Anders Carlsson19879c92010-03-23 17:17:29 +00003459 assert(!isa<CXXDestructorDecl>(MD) &&
3460 "Use mangleCXXDtor for destructor decls!");
Rafael Espindolac4850c22011-02-10 23:59:36 +00003461 CXXNameMangler Mangler(*this, Out);
Anders Carlsson19879c92010-03-23 17:17:29 +00003462 Mangler.getStream() << "_ZT";
3463 if (!Thunk.Return.isEmpty())
3464 Mangler.getStream() << 'c';
Sean Huntc3021132010-05-05 15:23:54 +00003465
Anders Carlsson19879c92010-03-23 17:17:29 +00003466 // Mangle the 'this' pointer adjustment.
3467 Mangler.mangleCallOffset(Thunk.This.NonVirtual, Thunk.This.VCallOffsetOffset);
Sean Huntc3021132010-05-05 15:23:54 +00003468
Anders Carlsson19879c92010-03-23 17:17:29 +00003469 // Mangle the return pointer adjustment if there is one.
3470 if (!Thunk.Return.isEmpty())
3471 Mangler.mangleCallOffset(Thunk.Return.NonVirtual,
3472 Thunk.Return.VBaseOffsetOffset);
Sean Huntc3021132010-05-05 15:23:54 +00003473
Anders Carlsson19879c92010-03-23 17:17:29 +00003474 Mangler.mangleFunctionEncoding(MD);
3475}
3476
Sean Huntc3021132010-05-05 15:23:54 +00003477void
Peter Collingbourne14110472011-01-13 18:57:25 +00003478ItaniumMangleContext::mangleCXXDtorThunk(const CXXDestructorDecl *DD,
3479 CXXDtorType Type,
3480 const ThisAdjustment &ThisAdjustment,
Chris Lattner5f9e2722011-07-23 10:55:15 +00003481 raw_ostream &Out) {
Anders Carlsson19879c92010-03-23 17:17:29 +00003482 // <special-name> ::= T <call-offset> <base encoding>
3483 // # base is the nominal target function of thunk
Rafael Espindolac4850c22011-02-10 23:59:36 +00003484 CXXNameMangler Mangler(*this, Out, DD, Type);
Anders Carlsson19879c92010-03-23 17:17:29 +00003485 Mangler.getStream() << "_ZT";
3486
3487 // Mangle the 'this' pointer adjustment.
Sean Huntc3021132010-05-05 15:23:54 +00003488 Mangler.mangleCallOffset(ThisAdjustment.NonVirtual,
Anders Carlsson19879c92010-03-23 17:17:29 +00003489 ThisAdjustment.VCallOffsetOffset);
3490
3491 Mangler.mangleFunctionEncoding(DD);
3492}
3493
Daniel Dunbarc0747712009-11-21 09:12:13 +00003494/// mangleGuardVariable - Returns the mangled name for a guard variable
3495/// for the passed in VarDecl.
Peter Collingbourne14110472011-01-13 18:57:25 +00003496void ItaniumMangleContext::mangleItaniumGuardVariable(const VarDecl *D,
Chris Lattner5f9e2722011-07-23 10:55:15 +00003497 raw_ostream &Out) {
Daniel Dunbarc0747712009-11-21 09:12:13 +00003498 // <special-name> ::= GV <object name> # Guard variable for one-time
3499 // # initialization
Rafael Espindolac4850c22011-02-10 23:59:36 +00003500 CXXNameMangler Mangler(*this, Out);
Daniel Dunbarc0747712009-11-21 09:12:13 +00003501 Mangler.getStream() << "_ZGV";
3502 Mangler.mangleName(D);
3503}
3504
Peter Collingbourne14110472011-01-13 18:57:25 +00003505void ItaniumMangleContext::mangleReferenceTemporary(const VarDecl *D,
Chris Lattner5f9e2722011-07-23 10:55:15 +00003506 raw_ostream &Out) {
Anders Carlsson715edf22010-06-26 16:09:40 +00003507 // We match the GCC mangling here.
3508 // <special-name> ::= GR <object name>
Rafael Espindolac4850c22011-02-10 23:59:36 +00003509 CXXNameMangler Mangler(*this, Out);
Anders Carlsson715edf22010-06-26 16:09:40 +00003510 Mangler.getStream() << "_ZGR";
3511 Mangler.mangleName(D);
3512}
3513
Peter Collingbourne14110472011-01-13 18:57:25 +00003514void ItaniumMangleContext::mangleCXXVTable(const CXXRecordDecl *RD,
Chris Lattner5f9e2722011-07-23 10:55:15 +00003515 raw_ostream &Out) {
Daniel Dunbarc0747712009-11-21 09:12:13 +00003516 // <special-name> ::= TV <type> # virtual table
Rafael Espindolac4850c22011-02-10 23:59:36 +00003517 CXXNameMangler Mangler(*this, Out);
Daniel Dunbarc0747712009-11-21 09:12:13 +00003518 Mangler.getStream() << "_ZTV";
Douglas Gregor1b12a3b2010-05-26 05:11:13 +00003519 Mangler.mangleNameOrStandardSubstitution(RD);
Daniel Dunbar1b077112009-11-21 09:06:10 +00003520}
Mike Stump82d75b02009-11-10 01:58:37 +00003521
Peter Collingbourne14110472011-01-13 18:57:25 +00003522void ItaniumMangleContext::mangleCXXVTT(const CXXRecordDecl *RD,
Chris Lattner5f9e2722011-07-23 10:55:15 +00003523 raw_ostream &Out) {
Daniel Dunbarc0747712009-11-21 09:12:13 +00003524 // <special-name> ::= TT <type> # VTT structure
Rafael Espindolac4850c22011-02-10 23:59:36 +00003525 CXXNameMangler Mangler(*this, Out);
Daniel Dunbarc0747712009-11-21 09:12:13 +00003526 Mangler.getStream() << "_ZTT";
Douglas Gregor1b12a3b2010-05-26 05:11:13 +00003527 Mangler.mangleNameOrStandardSubstitution(RD);
Daniel Dunbar1b077112009-11-21 09:06:10 +00003528}
Mike Stumpab3f7e92009-11-10 01:41:59 +00003529
Peter Collingbourne14110472011-01-13 18:57:25 +00003530void ItaniumMangleContext::mangleCXXCtorVTable(const CXXRecordDecl *RD,
3531 int64_t Offset,
3532 const CXXRecordDecl *Type,
Chris Lattner5f9e2722011-07-23 10:55:15 +00003533 raw_ostream &Out) {
Daniel Dunbarc0747712009-11-21 09:12:13 +00003534 // <special-name> ::= TC <type> <offset number> _ <base type>
Rafael Espindolac4850c22011-02-10 23:59:36 +00003535 CXXNameMangler Mangler(*this, Out);
Daniel Dunbarc0747712009-11-21 09:12:13 +00003536 Mangler.getStream() << "_ZTC";
Douglas Gregor1b12a3b2010-05-26 05:11:13 +00003537 Mangler.mangleNameOrStandardSubstitution(RD);
Daniel Dunbarc0747712009-11-21 09:12:13 +00003538 Mangler.getStream() << Offset;
Benjamin Kramer35f59b62010-04-10 16:03:31 +00003539 Mangler.getStream() << '_';
Douglas Gregor1b12a3b2010-05-26 05:11:13 +00003540 Mangler.mangleNameOrStandardSubstitution(Type);
Daniel Dunbar1b077112009-11-21 09:06:10 +00003541}
Mike Stump738f8c22009-07-31 23:15:31 +00003542
Peter Collingbourne14110472011-01-13 18:57:25 +00003543void ItaniumMangleContext::mangleCXXRTTI(QualType Ty,
Chris Lattner5f9e2722011-07-23 10:55:15 +00003544 raw_ostream &Out) {
Daniel Dunbarc0747712009-11-21 09:12:13 +00003545 // <special-name> ::= TI <type> # typeinfo structure
Douglas Gregor154fe982009-12-23 22:04:40 +00003546 assert(!Ty.hasQualifiers() && "RTTI info cannot have top-level qualifiers");
Rafael Espindolac4850c22011-02-10 23:59:36 +00003547 CXXNameMangler Mangler(*this, Out);
Daniel Dunbarc0747712009-11-21 09:12:13 +00003548 Mangler.getStream() << "_ZTI";
3549 Mangler.mangleType(Ty);
Daniel Dunbar1b077112009-11-21 09:06:10 +00003550}
Mike Stump67795982009-11-14 00:14:13 +00003551
Peter Collingbourne14110472011-01-13 18:57:25 +00003552void ItaniumMangleContext::mangleCXXRTTIName(QualType Ty,
Chris Lattner5f9e2722011-07-23 10:55:15 +00003553 raw_ostream &Out) {
Daniel Dunbarc0747712009-11-21 09:12:13 +00003554 // <special-name> ::= TS <type> # typeinfo name (null terminated byte string)
Rafael Espindolac4850c22011-02-10 23:59:36 +00003555 CXXNameMangler Mangler(*this, Out);
Daniel Dunbarc0747712009-11-21 09:12:13 +00003556 Mangler.getStream() << "_ZTS";
3557 Mangler.mangleType(Ty);
Mike Stumpf1216772009-07-31 18:25:34 +00003558}
Peter Collingbourne14110472011-01-13 18:57:25 +00003559
3560MangleContext *clang::createItaniumMangleContext(ASTContext &Context,
David Blaikied6471f72011-09-25 23:23:43 +00003561 DiagnosticsEngine &Diags) {
Peter Collingbourne14110472011-01-13 18:57:25 +00003562 return new ItaniumMangleContext(Context, Diags);
3563}