blob: c4eed7c34d625271cde42cc941dd5fec4de0fdc7 [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);
Douglas Gregor20f0cc72010-04-23 03:10:43 +0000346 void mangleTemplateArgs(TemplateName Template,
347 const TemplateArgument *TemplateArgs,
Sean Huntc3021132010-05-05 15:23:54 +0000348 unsigned NumTemplateArgs);
Rafael Espindolad9800722010-03-11 14:07:00 +0000349 void mangleTemplateArgs(const TemplateParameterList &PL,
350 const TemplateArgument *TemplateArgs,
Daniel Dunbar1b077112009-11-21 09:06:10 +0000351 unsigned NumTemplateArgs);
Rafael Espindolad9800722010-03-11 14:07:00 +0000352 void mangleTemplateArgs(const TemplateParameterList &PL,
353 const TemplateArgumentList &AL);
Douglas Gregorf1588662011-07-12 15:18:55 +0000354 void mangleTemplateArg(const NamedDecl *P, TemplateArgument A);
John McCall4f4e4132011-05-04 01:45:19 +0000355 void mangleUnresolvedTemplateArgs(const TemplateArgument *args,
356 unsigned numArgs);
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000357
Daniel Dunbar1b077112009-11-21 09:06:10 +0000358 void mangleTemplateParameter(unsigned Index);
John McCallfb44de92011-05-01 22:35:37 +0000359
360 void mangleFunctionParam(const ParmVarDecl *parm);
Daniel Dunbar1b077112009-11-21 09:06:10 +0000361};
Peter Collingbourne14110472011-01-13 18:57:25 +0000362
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000363}
364
Anders Carlsson43f17402009-04-02 15:51:53 +0000365static bool isInCLinkageSpecification(const Decl *D) {
Douglas Gregor457e2812009-10-28 16:31:34 +0000366 D = D->getCanonicalDecl();
Douglas Gregorccc1b5e2012-02-21 00:37:24 +0000367 for (const DeclContext *DC = getEffectiveDeclContext(D);
368 !DC->isTranslationUnit(); DC = getEffectiveParentContext(DC)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000369 if (const LinkageSpecDecl *Linkage = dyn_cast<LinkageSpecDecl>(DC))
Anders Carlsson43f17402009-04-02 15:51:53 +0000370 return Linkage->getLanguage() == LinkageSpecDecl::lang_c;
371 }
Mike Stump1eb44332009-09-09 15:08:12 +0000372
Anders Carlsson43f17402009-04-02 15:51:53 +0000373 return false;
374}
375
Peter Collingbourne14110472011-01-13 18:57:25 +0000376bool ItaniumMangleContext::shouldMangleDeclName(const NamedDecl *D) {
Daniel Dunbarf981bf82009-11-21 09:14:52 +0000377 // In C, functions with no attributes never need to be mangled. Fastpath them.
378 if (!getASTContext().getLangOptions().CPlusPlus && !D->hasAttrs())
379 return false;
380
381 // Any decl can be declared with __asm("foo") on it, and this takes precedence
382 // over all other naming in the .o file.
383 if (D->hasAttr<AsmLabelAttr>())
384 return true;
385
Mike Stump141c5af2009-09-02 00:25:38 +0000386 // Clang's "overloadable" attribute extension to C/C++ implies name mangling
Anders Carlssona1e16222009-11-07 07:15:03 +0000387 // (always) as does passing a C++ member function and a function
388 // whose name is not a simple identifier.
Daniel Dunbarf981bf82009-11-21 09:14:52 +0000389 const FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
390 if (FD && (FD->hasAttr<OverloadableAttr>() || isa<CXXMethodDecl>(FD) ||
391 !FD->getDeclName().isIdentifier()))
392 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000393
Daniel Dunbarf981bf82009-11-21 09:14:52 +0000394 // Otherwise, no mangling is done outside C++ mode.
395 if (!getASTContext().getLangOptions().CPlusPlus)
396 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000397
Sean Hunt31455252010-01-24 03:04:27 +0000398 // Variables at global scope with non-internal linkage are not mangled
Eli Friedman7facf842009-12-02 20:32:49 +0000399 if (!FD) {
Douglas Gregorccc1b5e2012-02-21 00:37:24 +0000400 const DeclContext *DC = getEffectiveDeclContext(D);
Eli Friedman7facf842009-12-02 20:32:49 +0000401 // Check for extern variable declared locally.
Fariborz Jahaniane81c5612010-06-30 18:57:21 +0000402 if (DC->isFunctionOrMethod() && D->hasLinkage())
Eli Friedman7facf842009-12-02 20:32:49 +0000403 while (!DC->isNamespace() && !DC->isTranslationUnit())
Douglas Gregorccc1b5e2012-02-21 00:37:24 +0000404 DC = getEffectiveParentContext(DC);
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000405 if (DC->isTranslationUnit() && D->getLinkage() != InternalLinkage)
Eli Friedman7facf842009-12-02 20:32:49 +0000406 return false;
407 }
408
Eli Friedmanc00cb642010-07-18 20:49:59 +0000409 // Class members are always mangled.
Douglas Gregorccc1b5e2012-02-21 00:37:24 +0000410 if (getEffectiveDeclContext(D)->isRecord())
Eli Friedmanc00cb642010-07-18 20:49:59 +0000411 return true;
412
Eli Friedman7facf842009-12-02 20:32:49 +0000413 // C functions and "main" are not mangled.
414 if ((FD && FD->isMain()) || isInCLinkageSpecification(D))
Daniel Dunbarf981bf82009-11-21 09:14:52 +0000415 return false;
416
Anders Carlsson43f17402009-04-02 15:51:53 +0000417 return true;
418}
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000419
Chris Lattner5f9e2722011-07-23 10:55:15 +0000420void CXXNameMangler::mangle(const NamedDecl *D, StringRef Prefix) {
Mike Stump141c5af2009-09-02 00:25:38 +0000421 // Any decl can be declared with __asm("foo") on it, and this takes precedence
422 // over all other naming in the .o file.
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000423 if (const AsmLabelAttr *ALA = D->getAttr<AsmLabelAttr>()) {
Chris Lattnerca3f25c2009-03-21 08:24:40 +0000424 // If we have an asm name, then we use it as the mangling.
Rafael Espindola4e274e92011-02-15 22:23:51 +0000425
426 // Adding the prefix can cause problems when one file has a "foo" and
427 // another has a "\01foo". That is known to happen on ELF with the
428 // tricks normally used for producing aliases (PR9177). Fortunately the
429 // llvm mangler on ELF is a nop, so we can just avoid adding the \01
Peter Collingbourne69317432011-04-06 12:29:09 +0000430 // marker. We also avoid adding the marker if this is an alias for an
431 // LLVM intrinsic.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000432 StringRef UserLabelPrefix =
Douglas Gregorbcfd1f52011-09-02 00:18:52 +0000433 getASTContext().getTargetInfo().getUserLabelPrefix();
Peter Collingbourne69317432011-04-06 12:29:09 +0000434 if (!UserLabelPrefix.empty() && !ALA->getLabel().startswith("llvm."))
Rafael Espindola4e274e92011-02-15 22:23:51 +0000435 Out << '\01'; // LLVM IR Marker for __asm("foo")
436
Chris Lattnerca3f25c2009-03-21 08:24:40 +0000437 Out << ALA->getLabel();
Daniel Dunbarf981bf82009-11-21 09:14:52 +0000438 return;
Chris Lattnerca3f25c2009-03-21 08:24:40 +0000439 }
Mike Stump1eb44332009-09-09 15:08:12 +0000440
Sean Hunt31455252010-01-24 03:04:27 +0000441 // <mangled-name> ::= _Z <encoding>
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000442 // ::= <data name>
443 // ::= <special-name>
Daniel Dunbar7e0c1952009-11-21 09:17:15 +0000444 Out << Prefix;
445 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
Daniel Dunbarf981bf82009-11-21 09:14:52 +0000446 mangleFunctionEncoding(FD);
Rafael Espindolad9800722010-03-11 14:07:00 +0000447 else if (const VarDecl *VD = dyn_cast<VarDecl>(D))
448 mangleName(VD);
Daniel Dunbar7e0c1952009-11-21 09:17:15 +0000449 else
Rafael Espindolad9800722010-03-11 14:07:00 +0000450 mangleName(cast<FieldDecl>(D));
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000451}
452
453void CXXNameMangler::mangleFunctionEncoding(const FunctionDecl *FD) {
454 // <encoding> ::= <function name> <bare-function-type>
455 mangleName(FD);
Mike Stump1eb44332009-09-09 15:08:12 +0000456
Daniel Dunbar7e0c1952009-11-21 09:17:15 +0000457 // Don't mangle in the type if this isn't a decl we should typically mangle.
458 if (!Context.shouldMangleDeclName(FD))
459 return;
460
Mike Stump141c5af2009-09-02 00:25:38 +0000461 // Whether the mangling of a function type includes the return type depends on
462 // the context and the nature of the function. The rules for deciding whether
463 // the return type is included are:
Mike Stump1eb44332009-09-09 15:08:12 +0000464 //
Douglas Gregor1fd2dd12009-06-29 22:39:32 +0000465 // 1. Template functions (names or types) have return types encoded, with
466 // the exceptions listed below.
Mike Stump1eb44332009-09-09 15:08:12 +0000467 // 2. Function types not appearing as part of a function name mangling,
Douglas Gregor1fd2dd12009-06-29 22:39:32 +0000468 // e.g. parameters, pointer types, etc., have return type encoded, with the
469 // exceptions listed below.
470 // 3. Non-template function names do not have return types encoded.
471 //
Mike Stump141c5af2009-09-02 00:25:38 +0000472 // The exceptions mentioned in (1) and (2) above, for which the return type is
473 // never included, are
Douglas Gregor1fd2dd12009-06-29 22:39:32 +0000474 // 1. Constructors.
475 // 2. Destructors.
476 // 3. Conversion operator functions, e.g. operator int.
477 bool MangleReturnType = false;
Anders Carlsson9234b7f2009-09-17 03:46:43 +0000478 if (FunctionTemplateDecl *PrimaryTemplate = FD->getPrimaryTemplate()) {
479 if (!(isa<CXXConstructorDecl>(FD) || isa<CXXDestructorDecl>(FD) ||
480 isa<CXXConversionDecl>(FD)))
481 MangleReturnType = true;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000482
Anders Carlsson9234b7f2009-09-17 03:46:43 +0000483 // Mangle the type of the primary template.
484 FD = PrimaryTemplate->getTemplatedDecl();
485 }
486
Douglas Gregor79e6bd32011-07-12 04:42:08 +0000487 mangleBareFunctionType(FD->getType()->getAs<FunctionType>(),
488 MangleReturnType);
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000489}
490
Anders Carlsson47846d22009-12-04 06:23:23 +0000491static const DeclContext *IgnoreLinkageSpecDecls(const DeclContext *DC) {
492 while (isa<LinkageSpecDecl>(DC)) {
Douglas Gregorccc1b5e2012-02-21 00:37:24 +0000493 DC = getEffectiveParentContext(DC);
Anders Carlsson47846d22009-12-04 06:23:23 +0000494 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000495
Anders Carlsson47846d22009-12-04 06:23:23 +0000496 return DC;
497}
498
Anders Carlssonc820f902010-06-02 15:58:27 +0000499/// isStd - Return whether a given namespace is the 'std' namespace.
500static bool isStd(const NamespaceDecl *NS) {
Douglas Gregorccc1b5e2012-02-21 00:37:24 +0000501 if (!IgnoreLinkageSpecDecls(getEffectiveParentContext(NS))
502 ->isTranslationUnit())
Anders Carlssonc820f902010-06-02 15:58:27 +0000503 return false;
504
505 const IdentifierInfo *II = NS->getOriginalNamespace()->getIdentifier();
506 return II && II->isStr("std");
507}
508
Anders Carlsson47846d22009-12-04 06:23:23 +0000509// isStdNamespace - Return whether a given decl context is a toplevel 'std'
510// namespace.
Daniel Dunbar1308af92009-11-21 09:11:45 +0000511static bool isStdNamespace(const DeclContext *DC) {
Anders Carlsson47846d22009-12-04 06:23:23 +0000512 if (!DC->isNamespace())
513 return false;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000514
Anders Carlsson47846d22009-12-04 06:23:23 +0000515 return isStd(cast<NamespaceDecl>(DC));
Daniel Dunbar1308af92009-11-21 09:11:45 +0000516}
517
Anders Carlssonbb36ba42009-09-26 03:24:57 +0000518static const TemplateDecl *
519isTemplate(const NamedDecl *ND, const TemplateArgumentList *&TemplateArgs) {
Anders Carlsson2744a062009-09-18 19:00:18 +0000520 // Check if we have a function template.
521 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)){
Anders Carlssonbb36ba42009-09-26 03:24:57 +0000522 if (const TemplateDecl *TD = FD->getPrimaryTemplate()) {
Anders Carlsson2744a062009-09-18 19:00:18 +0000523 TemplateArgs = FD->getTemplateSpecializationArgs();
Anders Carlssonbb36ba42009-09-26 03:24:57 +0000524 return TD;
Anders Carlsson2744a062009-09-18 19:00:18 +0000525 }
526 }
527
Anders Carlssoneafc6dc2009-09-18 19:44:50 +0000528 // Check if we have a class template.
529 if (const ClassTemplateSpecializationDecl *Spec =
530 dyn_cast<ClassTemplateSpecializationDecl>(ND)) {
531 TemplateArgs = &Spec->getTemplateArgs();
Anders Carlssonbb36ba42009-09-26 03:24:57 +0000532 return Spec->getSpecializedTemplate();
Anders Carlssoneafc6dc2009-09-18 19:44:50 +0000533 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000534
Anders Carlsson2744a062009-09-18 19:00:18 +0000535 return 0;
536}
537
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000538void CXXNameMangler::mangleName(const NamedDecl *ND) {
539 // <name> ::= <nested-name>
540 // ::= <unscoped-name>
541 // ::= <unscoped-template-name> <template-args>
Anders Carlsson201ce742009-09-17 03:17:01 +0000542 // ::= <local-name>
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000543 //
Douglas Gregorccc1b5e2012-02-21 00:37:24 +0000544 const DeclContext *DC = getEffectiveDeclContext(ND);
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000545
Eli Friedman7facf842009-12-02 20:32:49 +0000546 // If this is an extern variable declared locally, the relevant DeclContext
547 // is that of the containing namespace, or the translation unit.
548 if (isa<FunctionDecl>(DC) && ND->hasLinkage())
549 while (!DC->isNamespace() && !DC->isTranslationUnit())
Douglas Gregorccc1b5e2012-02-21 00:37:24 +0000550 DC = getEffectiveParentContext(DC);
John McCall82b7d7b2010-10-18 21:28:44 +0000551 else if (GetLocalClassDecl(ND)) {
552 mangleLocalName(ND);
553 return;
554 }
Eli Friedman7facf842009-12-02 20:32:49 +0000555
James Molloyb3c312c2012-03-05 09:59:43 +0000556 DC = IgnoreLinkageSpecDecls(DC);
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000557
Anders Carlssond58d6f72009-09-17 16:12:20 +0000558 if (DC->isTranslationUnit() || isStdNamespace(DC)) {
Anders Carlsson2744a062009-09-18 19:00:18 +0000559 // Check if we have a template.
560 const TemplateArgumentList *TemplateArgs = 0;
Anders Carlsson0fa6df42009-09-26 19:45:45 +0000561 if (const TemplateDecl *TD = isTemplate(ND, TemplateArgs)) {
Anders Carlsson2744a062009-09-18 19:00:18 +0000562 mangleUnscopedTemplateName(TD);
Rafael Espindolad9800722010-03-11 14:07:00 +0000563 TemplateParameterList *TemplateParameters = TD->getTemplateParameters();
564 mangleTemplateArgs(*TemplateParameters, *TemplateArgs);
Anders Carlsson2744a062009-09-18 19:00:18 +0000565 return;
Anders Carlsson7482e242009-09-18 04:29:09 +0000566 }
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000567
Anders Carlsson7482e242009-09-18 04:29:09 +0000568 mangleUnscopedName(ND);
569 return;
570 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000571
Anders Carlsson7b06f6c2009-12-10 03:14:39 +0000572 if (isa<FunctionDecl>(DC) || isa<ObjCMethodDecl>(DC)) {
Anders Carlsson7482e242009-09-18 04:29:09 +0000573 mangleLocalName(ND);
574 return;
575 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000576
Eli Friedman7facf842009-12-02 20:32:49 +0000577 mangleNestedName(ND, DC);
Anders Carlsson7482e242009-09-18 04:29:09 +0000578}
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000579void CXXNameMangler::mangleName(const TemplateDecl *TD,
Anders Carlsson7624f212009-09-18 02:42:01 +0000580 const TemplateArgument *TemplateArgs,
581 unsigned NumTemplateArgs) {
Douglas Gregorccc1b5e2012-02-21 00:37:24 +0000582 const DeclContext *DC = IgnoreLinkageSpecDecls(getEffectiveDeclContext(TD));
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000583
Anders Carlsson7624f212009-09-18 02:42:01 +0000584 if (DC->isTranslationUnit() || isStdNamespace(DC)) {
Anders Carlsson0fa6df42009-09-26 19:45:45 +0000585 mangleUnscopedTemplateName(TD);
Rafael Espindolad9800722010-03-11 14:07:00 +0000586 TemplateParameterList *TemplateParameters = TD->getTemplateParameters();
587 mangleTemplateArgs(*TemplateParameters, TemplateArgs, NumTemplateArgs);
Anders Carlsson7624f212009-09-18 02:42:01 +0000588 } else {
589 mangleNestedName(TD, TemplateArgs, NumTemplateArgs);
590 }
591}
592
Anders Carlsson201ce742009-09-17 03:17:01 +0000593void CXXNameMangler::mangleUnscopedName(const NamedDecl *ND) {
594 // <unscoped-name> ::= <unqualified-name>
595 // ::= St <unqualified-name> # ::std::
James Molloyb3c312c2012-03-05 09:59:43 +0000596
597 if (isStdNamespace(IgnoreLinkageSpecDecls(getEffectiveDeclContext(ND))))
Anders Carlsson201ce742009-09-17 03:17:01 +0000598 Out << "St";
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000599
Anders Carlsson201ce742009-09-17 03:17:01 +0000600 mangleUnqualifiedName(ND);
601}
602
Anders Carlsson0fa6df42009-09-26 19:45:45 +0000603void CXXNameMangler::mangleUnscopedTemplateName(const TemplateDecl *ND) {
Anders Carlsson201ce742009-09-17 03:17:01 +0000604 // <unscoped-template-name> ::= <unscoped-name>
605 // ::= <substitution>
Anders Carlsson7624f212009-09-18 02:42:01 +0000606 if (mangleSubstitution(ND))
Anders Carlsson03c9d532009-09-17 04:02:31 +0000607 return;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000608
Douglas Gregor32fb4e12010-02-05 20:45:00 +0000609 // <template-template-param> ::= <template-param>
610 if (const TemplateTemplateParmDecl *TTP
611 = dyn_cast<TemplateTemplateParmDecl>(ND)) {
612 mangleTemplateParameter(TTP->getIndex());
613 return;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000614 }
Douglas Gregor32fb4e12010-02-05 20:45:00 +0000615
Anders Carlsson1668f202009-09-26 20:13:56 +0000616 mangleUnscopedName(ND->getTemplatedDecl());
Anders Carlsson7624f212009-09-18 02:42:01 +0000617 addSubstitution(ND);
Anders Carlsson201ce742009-09-17 03:17:01 +0000618}
619
Douglas Gregor1e9268e2010-04-28 05:58:56 +0000620void CXXNameMangler::mangleUnscopedTemplateName(TemplateName Template) {
621 // <unscoped-template-name> ::= <unscoped-name>
622 // ::= <substitution>
623 if (TemplateDecl *TD = Template.getAsTemplateDecl())
624 return mangleUnscopedTemplateName(TD);
Sean Huntc3021132010-05-05 15:23:54 +0000625
Douglas Gregor1e9268e2010-04-28 05:58:56 +0000626 if (mangleSubstitution(Template))
627 return;
628
Douglas Gregor1e9268e2010-04-28 05:58:56 +0000629 DependentTemplateName *Dependent = Template.getAsDependentTemplateName();
630 assert(Dependent && "Not a dependent template name?");
Douglas Gregor19617912011-07-12 05:06:05 +0000631 if (const IdentifierInfo *Id = Dependent->getIdentifier())
632 mangleSourceName(Id);
633 else
634 mangleOperatorName(Dependent->getOperator(), UnknownArity);
Sean Huntc3021132010-05-05 15:23:54 +0000635
Douglas Gregor1e9268e2010-04-28 05:58:56 +0000636 addSubstitution(Template);
637}
638
John McCall1b600522011-04-24 03:07:16 +0000639void CXXNameMangler::mangleFloat(const llvm::APFloat &f) {
640 // ABI:
641 // Floating-point literals are encoded using a fixed-length
642 // lowercase hexadecimal string corresponding to the internal
643 // representation (IEEE on Itanium), high-order bytes first,
644 // without leading zeroes. For example: "Lf bf800000 E" is -1.0f
645 // on Itanium.
John McCall0c8731a2012-01-30 18:36:31 +0000646 // The 'without leading zeroes' thing seems to be an editorial
647 // mistake; see the discussion on cxx-abi-dev beginning on
648 // 2012-01-16.
John McCall1b600522011-04-24 03:07:16 +0000649
John McCall0c8731a2012-01-30 18:36:31 +0000650 // Our requirements here are just barely wierd enough to justify
651 // using a custom algorithm instead of post-processing APInt::toString().
John McCall1b600522011-04-24 03:07:16 +0000652
John McCall0c8731a2012-01-30 18:36:31 +0000653 llvm::APInt valueBits = f.bitcastToAPInt();
654 unsigned numCharacters = (valueBits.getBitWidth() + 3) / 4;
655 assert(numCharacters != 0);
656
657 // Allocate a buffer of the right number of characters.
658 llvm::SmallVector<char, 20> buffer;
659 buffer.set_size(numCharacters);
660
661 // Fill the buffer left-to-right.
662 for (unsigned stringIndex = 0; stringIndex != numCharacters; ++stringIndex) {
663 // The bit-index of the next hex digit.
664 unsigned digitBitIndex = 4 * (numCharacters - stringIndex - 1);
665
666 // Project out 4 bits starting at 'digitIndex'.
667 llvm::integerPart hexDigit
668 = valueBits.getRawData()[digitBitIndex / llvm::integerPartWidth];
669 hexDigit >>= (digitBitIndex % llvm::integerPartWidth);
670 hexDigit &= 0xF;
671
672 // Map that over to a lowercase hex digit.
673 static const char charForHex[16] = {
674 '0', '1', '2', '3', '4', '5', '6', '7',
675 '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'
676 };
677 buffer[stringIndex] = charForHex[hexDigit];
678 }
679
680 Out.write(buffer.data(), numCharacters);
John McCall0512e482010-07-14 04:20:34 +0000681}
682
683void CXXNameMangler::mangleNumber(const llvm::APSInt &Value) {
684 if (Value.isSigned() && Value.isNegative()) {
685 Out << 'n';
686 Value.abs().print(Out, true);
687 } else
688 Value.print(Out, Value.isSigned());
689}
690
Anders Carlssona94822e2009-11-26 02:32:05 +0000691void CXXNameMangler::mangleNumber(int64_t Number) {
692 // <number> ::= [n] <non-negative decimal integer>
693 if (Number < 0) {
694 Out << 'n';
695 Number = -Number;
696 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000697
Anders Carlssona94822e2009-11-26 02:32:05 +0000698 Out << Number;
699}
700
Anders Carlsson19879c92010-03-23 17:17:29 +0000701void CXXNameMangler::mangleCallOffset(int64_t NonVirtual, int64_t Virtual) {
Mike Stump141c5af2009-09-02 00:25:38 +0000702 // <call-offset> ::= h <nv-offset> _
703 // ::= v <v-offset> _
704 // <nv-offset> ::= <offset number> # non-virtual base override
Anders Carlssona94822e2009-11-26 02:32:05 +0000705 // <v-offset> ::= <offset number> _ <virtual offset number>
Mike Stump141c5af2009-09-02 00:25:38 +0000706 // # virtual base override, with vcall offset
Anders Carlsson19879c92010-03-23 17:17:29 +0000707 if (!Virtual) {
Anders Carlssona94822e2009-11-26 02:32:05 +0000708 Out << 'h';
Anders Carlsson19879c92010-03-23 17:17:29 +0000709 mangleNumber(NonVirtual);
Anders Carlssona94822e2009-11-26 02:32:05 +0000710 Out << '_';
711 return;
Mike Stump141c5af2009-09-02 00:25:38 +0000712 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000713
Anders Carlssona94822e2009-11-26 02:32:05 +0000714 Out << 'v';
Anders Carlsson19879c92010-03-23 17:17:29 +0000715 mangleNumber(NonVirtual);
Anders Carlssona94822e2009-11-26 02:32:05 +0000716 Out << '_';
Anders Carlsson19879c92010-03-23 17:17:29 +0000717 mangleNumber(Virtual);
Anders Carlssona94822e2009-11-26 02:32:05 +0000718 Out << '_';
Mike Stump9124bcc2009-09-02 00:56:18 +0000719}
720
John McCall4f4e4132011-05-04 01:45:19 +0000721void CXXNameMangler::manglePrefix(QualType type) {
John McCalla0ce15c2011-04-24 08:23:24 +0000722 if (const TemplateSpecializationType *TST =
723 type->getAs<TemplateSpecializationType>()) {
724 if (!mangleSubstitution(QualType(TST, 0))) {
725 mangleTemplatePrefix(TST->getTemplateName());
Sean Huntc3021132010-05-05 15:23:54 +0000726
Douglas Gregoraa2187d2011-02-28 00:04:36 +0000727 // FIXME: GCC does not appear to mangle the template arguments when
728 // the template in question is a dependent template name. Should we
729 // emulate that badness?
John McCalla0ce15c2011-04-24 08:23:24 +0000730 mangleTemplateArgs(TST->getTemplateName(), TST->getArgs(),
731 TST->getNumArgs());
732 addSubstitution(QualType(TST, 0));
Rafael Espindola9b35b252010-03-17 04:28:11 +0000733 }
John McCalla0ce15c2011-04-24 08:23:24 +0000734 } else if (const DependentTemplateSpecializationType *DTST
735 = type->getAs<DependentTemplateSpecializationType>()) {
736 TemplateName Template
737 = getASTContext().getDependentTemplateName(DTST->getQualifier(),
738 DTST->getIdentifier());
739 mangleTemplatePrefix(Template);
740
741 // FIXME: GCC does not appear to mangle the template arguments when
742 // the template in question is a dependent template name. Should we
743 // emulate that badness?
744 mangleTemplateArgs(Template, DTST->getArgs(), DTST->getNumArgs());
745 } else {
746 // We use the QualType mangle type variant here because it handles
747 // substitutions.
748 mangleType(type);
John McCall1dd73832010-02-04 01:42:13 +0000749 }
750}
751
John McCalla0ce15c2011-04-24 08:23:24 +0000752/// Mangle everything prior to the base-unresolved-name in an unresolved-name.
753///
754/// \param firstQualifierLookup - the entity found by unqualified lookup
755/// for the first name in the qualifier, if this is for a member expression
756/// \param recursive - true if this is being called recursively,
757/// i.e. if there is more prefix "to the right".
758void CXXNameMangler::mangleUnresolvedPrefix(NestedNameSpecifier *qualifier,
759 NamedDecl *firstQualifierLookup,
760 bool recursive) {
John McCall1dd73832010-02-04 01:42:13 +0000761
John McCalla0ce15c2011-04-24 08:23:24 +0000762 // x, ::x
763 // <unresolved-name> ::= [gs] <base-unresolved-name>
764
765 // T::x / decltype(p)::x
766 // <unresolved-name> ::= sr <unresolved-type> <base-unresolved-name>
767
768 // T::N::x /decltype(p)::N::x
769 // <unresolved-name> ::= srN <unresolved-type> <unresolved-qualifier-level>+ E
770 // <base-unresolved-name>
771
772 // A::x, N::y, A<T>::z; "gs" means leading "::"
773 // <unresolved-name> ::= [gs] sr <unresolved-qualifier-level>+ E
774 // <base-unresolved-name>
775
776 switch (qualifier->getKind()) {
777 case NestedNameSpecifier::Global:
778 Out << "gs";
779
780 // We want an 'sr' unless this is the entire NNS.
781 if (recursive)
782 Out << "sr";
783
784 // We never want an 'E' here.
785 return;
786
787 case NestedNameSpecifier::Namespace:
788 if (qualifier->getPrefix())
789 mangleUnresolvedPrefix(qualifier->getPrefix(), firstQualifierLookup,
790 /*recursive*/ true);
791 else
792 Out << "sr";
793 mangleSourceName(qualifier->getAsNamespace()->getIdentifier());
794 break;
795 case NestedNameSpecifier::NamespaceAlias:
796 if (qualifier->getPrefix())
797 mangleUnresolvedPrefix(qualifier->getPrefix(), firstQualifierLookup,
798 /*recursive*/ true);
799 else
800 Out << "sr";
801 mangleSourceName(qualifier->getAsNamespaceAlias()->getIdentifier());
802 break;
803
804 case NestedNameSpecifier::TypeSpec:
805 case NestedNameSpecifier::TypeSpecWithTemplate: {
John McCall4f4e4132011-05-04 01:45:19 +0000806 const Type *type = qualifier->getAsType();
John McCalla0ce15c2011-04-24 08:23:24 +0000807
John McCall4f4e4132011-05-04 01:45:19 +0000808 // We only want to use an unresolved-type encoding if this is one of:
809 // - a decltype
810 // - a template type parameter
811 // - a template template parameter with arguments
812 // In all of these cases, we should have no prefix.
813 if (qualifier->getPrefix()) {
814 mangleUnresolvedPrefix(qualifier->getPrefix(), firstQualifierLookup,
815 /*recursive*/ true);
816 } else {
817 // Otherwise, all the cases want this.
818 Out << "sr";
John McCall4f4e4132011-05-04 01:45:19 +0000819 }
820
John McCall4f4e4132011-05-04 01:45:19 +0000821 // Only certain other types are valid as prefixes; enumerate them.
John McCalld3d49bb2011-06-28 16:49:23 +0000822 switch (type->getTypeClass()) {
823 case Type::Builtin:
824 case Type::Complex:
825 case Type::Pointer:
826 case Type::BlockPointer:
827 case Type::LValueReference:
828 case Type::RValueReference:
829 case Type::MemberPointer:
830 case Type::ConstantArray:
831 case Type::IncompleteArray:
832 case Type::VariableArray:
833 case Type::DependentSizedArray:
834 case Type::DependentSizedExtVector:
835 case Type::Vector:
836 case Type::ExtVector:
837 case Type::FunctionProto:
838 case Type::FunctionNoProto:
839 case Type::Enum:
840 case Type::Paren:
841 case Type::Elaborated:
842 case Type::Attributed:
843 case Type::Auto:
844 case Type::PackExpansion:
John McCalld3d49bb2011-06-28 16:49:23 +0000845 case Type::ObjCObject:
846 case Type::ObjCInterface:
847 case Type::ObjCObjectPointer:
Eli Friedmanb001de72011-10-06 23:00:33 +0000848 case Type::Atomic:
John McCalld3d49bb2011-06-28 16:49:23 +0000849 llvm_unreachable("type is illegal as a nested name specifier");
850
John McCall68a51a72011-07-01 00:04:39 +0000851 case Type::SubstTemplateTypeParmPack:
852 // FIXME: not clear how to mangle this!
853 // template <class T...> class A {
854 // template <class U...> void foo(decltype(T::foo(U())) x...);
855 // };
856 Out << "_SUBSTPACK_";
857 break;
858
John McCalld3d49bb2011-06-28 16:49:23 +0000859 // <unresolved-type> ::= <template-param>
860 // ::= <decltype>
861 // ::= <template-template-param> <template-args>
862 // (this last is not official yet)
863 case Type::TypeOfExpr:
864 case Type::TypeOf:
865 case Type::Decltype:
866 case Type::TemplateTypeParm:
867 case Type::UnaryTransform:
John McCall35ee32e2011-07-01 02:19:08 +0000868 case Type::SubstTemplateTypeParm:
John McCalld3d49bb2011-06-28 16:49:23 +0000869 unresolvedType:
870 assert(!qualifier->getPrefix());
871
872 // We only get here recursively if we're followed by identifiers.
873 if (recursive) Out << 'N';
874
John McCall35ee32e2011-07-01 02:19:08 +0000875 // This seems to do everything we want. It's not really
876 // sanctioned for a substituted template parameter, though.
John McCalld3d49bb2011-06-28 16:49:23 +0000877 mangleType(QualType(type, 0));
878
879 // We never want to print 'E' directly after an unresolved-type,
880 // so we return directly.
881 return;
882
John McCalld3d49bb2011-06-28 16:49:23 +0000883 case Type::Typedef:
884 mangleSourceName(cast<TypedefType>(type)->getDecl()->getIdentifier());
885 break;
886
887 case Type::UnresolvedUsing:
888 mangleSourceName(cast<UnresolvedUsingType>(type)->getDecl()
889 ->getIdentifier());
890 break;
891
892 case Type::Record:
893 mangleSourceName(cast<RecordType>(type)->getDecl()->getIdentifier());
894 break;
895
896 case Type::TemplateSpecialization: {
897 const TemplateSpecializationType *tst
898 = cast<TemplateSpecializationType>(type);
John McCall68a51a72011-07-01 00:04:39 +0000899 TemplateName name = tst->getTemplateName();
900 switch (name.getKind()) {
901 case TemplateName::Template:
902 case TemplateName::QualifiedTemplate: {
903 TemplateDecl *temp = name.getAsTemplateDecl();
John McCalld3d49bb2011-06-28 16:49:23 +0000904
John McCall68a51a72011-07-01 00:04:39 +0000905 // If the base is a template template parameter, this is an
906 // unresolved type.
907 assert(temp && "no template for template specialization type");
908 if (isa<TemplateTemplateParmDecl>(temp)) goto unresolvedType;
John McCalld3d49bb2011-06-28 16:49:23 +0000909
John McCall68a51a72011-07-01 00:04:39 +0000910 mangleSourceName(temp->getIdentifier());
911 break;
912 }
913
914 case TemplateName::OverloadedTemplate:
915 case TemplateName::DependentTemplate:
916 llvm_unreachable("invalid base for a template specialization type");
917
918 case TemplateName::SubstTemplateTemplateParm: {
919 SubstTemplateTemplateParmStorage *subst
920 = name.getAsSubstTemplateTemplateParm();
921 mangleExistingSubstitution(subst->getReplacement());
922 break;
923 }
924
925 case TemplateName::SubstTemplateTemplateParmPack: {
926 // FIXME: not clear how to mangle this!
927 // template <template <class U> class T...> class A {
928 // template <class U...> void foo(decltype(T<U>::foo) x...);
929 // };
930 Out << "_SUBSTPACK_";
931 break;
932 }
933 }
934
John McCall4f4e4132011-05-04 01:45:19 +0000935 mangleUnresolvedTemplateArgs(tst->getArgs(), tst->getNumArgs());
John McCalld3d49bb2011-06-28 16:49:23 +0000936 break;
937 }
938
939 case Type::InjectedClassName:
940 mangleSourceName(cast<InjectedClassNameType>(type)->getDecl()
941 ->getIdentifier());
942 break;
943
944 case Type::DependentName:
945 mangleSourceName(cast<DependentNameType>(type)->getIdentifier());
946 break;
947
948 case Type::DependentTemplateSpecialization: {
949 const DependentTemplateSpecializationType *tst
950 = cast<DependentTemplateSpecializationType>(type);
John McCall4f4e4132011-05-04 01:45:19 +0000951 mangleSourceName(tst->getIdentifier());
952 mangleUnresolvedTemplateArgs(tst->getArgs(), tst->getNumArgs());
John McCalld3d49bb2011-06-28 16:49:23 +0000953 break;
954 }
John McCall4f4e4132011-05-04 01:45:19 +0000955 }
956 break;
John McCalla0ce15c2011-04-24 08:23:24 +0000957 }
958
959 case NestedNameSpecifier::Identifier:
960 // Member expressions can have these without prefixes.
961 if (qualifier->getPrefix()) {
962 mangleUnresolvedPrefix(qualifier->getPrefix(), firstQualifierLookup,
963 /*recursive*/ true);
964 } else if (firstQualifierLookup) {
965
966 // Try to make a proper qualifier out of the lookup result, and
967 // then just recurse on that.
968 NestedNameSpecifier *newQualifier;
969 if (TypeDecl *typeDecl = dyn_cast<TypeDecl>(firstQualifierLookup)) {
970 QualType type = getASTContext().getTypeDeclType(typeDecl);
971
972 // Pretend we had a different nested name specifier.
973 newQualifier = NestedNameSpecifier::Create(getASTContext(),
974 /*prefix*/ 0,
975 /*template*/ false,
976 type.getTypePtr());
977 } else if (NamespaceDecl *nspace =
978 dyn_cast<NamespaceDecl>(firstQualifierLookup)) {
979 newQualifier = NestedNameSpecifier::Create(getASTContext(),
980 /*prefix*/ 0,
981 nspace);
982 } else if (NamespaceAliasDecl *alias =
983 dyn_cast<NamespaceAliasDecl>(firstQualifierLookup)) {
984 newQualifier = NestedNameSpecifier::Create(getASTContext(),
985 /*prefix*/ 0,
986 alias);
987 } else {
988 // No sensible mangling to do here.
989 newQualifier = 0;
990 }
991
992 if (newQualifier)
993 return mangleUnresolvedPrefix(newQualifier, /*lookup*/ 0, recursive);
994
995 } else {
996 Out << "sr";
997 }
998
999 mangleSourceName(qualifier->getAsIdentifier());
1000 break;
1001 }
1002
1003 // If this was the innermost part of the NNS, and we fell out to
1004 // here, append an 'E'.
1005 if (!recursive)
1006 Out << 'E';
1007}
1008
1009/// Mangle an unresolved-name, which is generally used for names which
1010/// weren't resolved to specific entities.
1011void CXXNameMangler::mangleUnresolvedName(NestedNameSpecifier *qualifier,
1012 NamedDecl *firstQualifierLookup,
1013 DeclarationName name,
1014 unsigned knownArity) {
1015 if (qualifier) mangleUnresolvedPrefix(qualifier, firstQualifierLookup);
1016 mangleUnqualifiedName(0, name, knownArity);
John McCall1dd73832010-02-04 01:42:13 +00001017}
1018
Anders Carlsson6f7e2f42010-06-08 14:49:03 +00001019static const FieldDecl *FindFirstNamedDataMember(const RecordDecl *RD) {
1020 assert(RD->isAnonymousStructOrUnion() &&
1021 "Expected anonymous struct or union!");
1022
1023 for (RecordDecl::field_iterator I = RD->field_begin(), E = RD->field_end();
1024 I != E; ++I) {
1025 const FieldDecl *FD = *I;
1026
1027 if (FD->getIdentifier())
1028 return FD;
1029
1030 if (const RecordType *RT = FD->getType()->getAs<RecordType>()) {
1031 if (const FieldDecl *NamedDataMember =
1032 FindFirstNamedDataMember(RT->getDecl()))
1033 return NamedDataMember;
1034 }
1035 }
1036
1037 // We didn't find a named data member.
1038 return 0;
1039}
1040
John McCall1dd73832010-02-04 01:42:13 +00001041void CXXNameMangler::mangleUnqualifiedName(const NamedDecl *ND,
1042 DeclarationName Name,
1043 unsigned KnownArity) {
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001044 // <unqualified-name> ::= <operator-name>
Mike Stump1eb44332009-09-09 15:08:12 +00001045 // ::= <ctor-dtor-name>
1046 // ::= <source-name>
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001047 switch (Name.getNameKind()) {
Anders Carlssonc4355b62009-10-07 01:45:02 +00001048 case DeclarationName::Identifier: {
Anders Carlssonc4355b62009-10-07 01:45:02 +00001049 if (const IdentifierInfo *II = Name.getAsIdentifierInfo()) {
Sean Hunt31455252010-01-24 03:04:27 +00001050 // We must avoid conflicts between internally- and externally-
John McCall74990f42011-03-22 06:34:45 +00001051 // linked variable and function declaration names in the same TU:
1052 // void test() { extern void foo(); }
1053 // static void foo();
1054 // This naming convention is the same as that followed by GCC,
1055 // though it shouldn't actually matter.
1056 if (ND && ND->getLinkage() == InternalLinkage &&
Douglas Gregorccc1b5e2012-02-21 00:37:24 +00001057 getEffectiveDeclContext(ND)->isFileContext())
Sean Hunt31455252010-01-24 03:04:27 +00001058 Out << 'L';
1059
Anders Carlssonc4355b62009-10-07 01:45:02 +00001060 mangleSourceName(II);
1061 break;
1062 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00001063
John McCall1dd73832010-02-04 01:42:13 +00001064 // Otherwise, an anonymous entity. We must have a declaration.
1065 assert(ND && "mangling empty name without declaration");
1066
1067 if (const NamespaceDecl *NS = dyn_cast<NamespaceDecl>(ND)) {
1068 if (NS->isAnonymousNamespace()) {
1069 // This is how gcc mangles these names.
1070 Out << "12_GLOBAL__N_1";
1071 break;
1072 }
1073 }
1074
Anders Carlsson6f7e2f42010-06-08 14:49:03 +00001075 if (const VarDecl *VD = dyn_cast<VarDecl>(ND)) {
1076 // We must have an anonymous union or struct declaration.
1077 const RecordDecl *RD =
1078 cast<RecordDecl>(VD->getType()->getAs<RecordType>()->getDecl());
1079
1080 // Itanium C++ ABI 5.1.2:
1081 //
1082 // For the purposes of mangling, the name of an anonymous union is
1083 // considered to be the name of the first named data member found by a
1084 // pre-order, depth-first, declaration-order walk of the data members of
1085 // the anonymous union. If there is no such data member (i.e., if all of
1086 // the data members in the union are unnamed), then there is no way for
1087 // a program to refer to the anonymous union, and there is therefore no
1088 // need to mangle its name.
1089 const FieldDecl *FD = FindFirstNamedDataMember(RD);
John McCall7121c8f2010-08-05 22:02:13 +00001090
1091 // It's actually possible for various reasons for us to get here
1092 // with an empty anonymous struct / union. Fortunately, it
1093 // doesn't really matter what name we generate.
1094 if (!FD) break;
Anders Carlsson6f7e2f42010-06-08 14:49:03 +00001095 assert(FD->getIdentifier() && "Data member name isn't an identifier!");
1096
1097 mangleSourceName(FD->getIdentifier());
1098 break;
1099 }
1100
Anders Carlssonc4355b62009-10-07 01:45:02 +00001101 // We must have an anonymous struct.
1102 const TagDecl *TD = cast<TagDecl>(ND);
Richard Smith162e1c12011-04-15 14:24:37 +00001103 if (const TypedefNameDecl *D = TD->getTypedefNameForAnonDecl()) {
Anders Carlssonc4355b62009-10-07 01:45:02 +00001104 assert(TD->getDeclContext() == D->getDeclContext() &&
1105 "Typedef should not be in another decl context!");
1106 assert(D->getDeclName().getAsIdentifierInfo() &&
1107 "Typedef was not named!");
1108 mangleSourceName(D->getDeclName().getAsIdentifierInfo());
1109 break;
1110 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00001111
Douglas Gregor9e8c92a2012-02-20 19:44:39 +00001112 // <unnamed-type-name> ::= <closure-type-name>
1113 //
1114 // <closure-type-name> ::= Ul <lambda-sig> E [ <nonnegative number> ] _
1115 // <lambda-sig> ::= <parameter-type>+ # Parameter types or 'v' for 'void'.
1116 if (const CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(TD)) {
Douglas Gregor5878cbc2012-02-21 04:17:39 +00001117 if (Record->isLambda() && Record->getLambdaManglingNumber()) {
Douglas Gregorccc1b5e2012-02-21 00:37:24 +00001118 mangleLambda(Record);
Douglas Gregor9e8c92a2012-02-20 19:44:39 +00001119 break;
1120 }
1121 }
1122
Anders Carlssonc4355b62009-10-07 01:45:02 +00001123 // Get a unique id for the anonymous struct.
1124 uint64_t AnonStructId = Context.getAnonymousStructId(TD);
1125
1126 // Mangle it as a source name in the form
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00001127 // [n] $_<id>
Anders Carlssonc4355b62009-10-07 01:45:02 +00001128 // where n is the length of the string.
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00001129 SmallString<8> Str;
Anders Carlssonc4355b62009-10-07 01:45:02 +00001130 Str += "$_";
1131 Str += llvm::utostr(AnonStructId);
1132
1133 Out << Str.size();
1134 Out << Str.str();
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001135 break;
Anders Carlssonc4355b62009-10-07 01:45:02 +00001136 }
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001137
1138 case DeclarationName::ObjCZeroArgSelector:
1139 case DeclarationName::ObjCOneArgSelector:
1140 case DeclarationName::ObjCMultiArgSelector:
David Blaikieb219cfc2011-09-23 05:06:16 +00001141 llvm_unreachable("Can't mangle Objective-C selector names here!");
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001142
1143 case DeclarationName::CXXConstructorName:
Anders Carlsson27ae5362009-04-17 01:58:57 +00001144 if (ND == Structor)
Mike Stump141c5af2009-09-02 00:25:38 +00001145 // If the named decl is the C++ constructor we're mangling, use the type
1146 // we were given.
Anders Carlsson27ae5362009-04-17 01:58:57 +00001147 mangleCXXCtorType(static_cast<CXXCtorType>(StructorType));
Anders Carlsson3ac86b52009-04-15 05:36:58 +00001148 else
1149 // Otherwise, use the complete constructor name. This is relevant if a
1150 // class with a constructor is declared within a constructor.
1151 mangleCXXCtorType(Ctor_Complete);
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001152 break;
1153
1154 case DeclarationName::CXXDestructorName:
Anders Carlsson27ae5362009-04-17 01:58:57 +00001155 if (ND == Structor)
Mike Stump141c5af2009-09-02 00:25:38 +00001156 // If the named decl is the C++ destructor we're mangling, use the type we
1157 // were given.
Anders Carlsson27ae5362009-04-17 01:58:57 +00001158 mangleCXXDtorType(static_cast<CXXDtorType>(StructorType));
1159 else
1160 // Otherwise, use the complete destructor name. This is relevant if a
1161 // class with a destructor is declared within a destructor.
1162 mangleCXXDtorType(Dtor_Complete);
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001163 break;
1164
1165 case DeclarationName::CXXConversionFunctionName:
Mike Stump1eb44332009-09-09 15:08:12 +00001166 // <operator-name> ::= cv <type> # (cast)
Douglas Gregor219cc612009-02-13 01:28:03 +00001167 Out << "cv";
Douglas Gregor79e6bd32011-07-12 04:42:08 +00001168 mangleType(Name.getCXXNameType());
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001169 break;
1170
Anders Carlsson8257d412009-12-22 06:36:32 +00001171 case DeclarationName::CXXOperatorName: {
John McCall1dd73832010-02-04 01:42:13 +00001172 unsigned Arity;
1173 if (ND) {
1174 Arity = cast<FunctionDecl>(ND)->getNumParams();
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001175
John McCall1dd73832010-02-04 01:42:13 +00001176 // If we have a C++ member function, we need to include the 'this' pointer.
1177 // FIXME: This does not make sense for operators that are static, but their
1178 // names stay the same regardless of the arity (operator new for instance).
1179 if (isa<CXXMethodDecl>(ND))
1180 Arity++;
1181 } else
1182 Arity = KnownArity;
1183
Anders Carlsson8257d412009-12-22 06:36:32 +00001184 mangleOperatorName(Name.getCXXOverloadedOperator(), Arity);
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001185 break;
Anders Carlsson8257d412009-12-22 06:36:32 +00001186 }
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001187
Sean Hunt3e518bd2009-11-29 07:34:05 +00001188 case DeclarationName::CXXLiteralOperatorName:
Sean Hunt5dd6b392009-12-04 21:11:13 +00001189 // FIXME: This mangling is not yet official.
Sean Hunt2421f662009-12-04 21:01:37 +00001190 Out << "li";
Sean Hunt3e518bd2009-11-29 07:34:05 +00001191 mangleSourceName(Name.getCXXLiteralIdentifier());
1192 break;
1193
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001194 case DeclarationName::CXXUsingDirective:
David Blaikieb219cfc2011-09-23 05:06:16 +00001195 llvm_unreachable("Can't mangle a using directive name!");
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001196 }
1197}
1198
1199void CXXNameMangler::mangleSourceName(const IdentifierInfo *II) {
1200 // <source-name> ::= <positive length number> <identifier>
1201 // <number> ::= [n] <non-negative decimal integer>
1202 // <identifier> ::= <unqualified source code identifier>
1203 Out << II->getLength() << II->getName();
1204}
1205
Eli Friedman7facf842009-12-02 20:32:49 +00001206void CXXNameMangler::mangleNestedName(const NamedDecl *ND,
Fariborz Jahanian57058532010-03-03 19:41:08 +00001207 const DeclContext *DC,
1208 bool NoFunction) {
Douglas Gregor0a9a6d62011-01-26 17:36:28 +00001209 // <nested-name>
1210 // ::= N [<CV-qualifiers>] [<ref-qualifier>] <prefix> <unqualified-name> E
1211 // ::= N [<CV-qualifiers>] [<ref-qualifier>] <template-prefix>
1212 // <template-args> E
Anders Carlssond99edc42009-09-26 03:55:37 +00001213
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001214 Out << 'N';
Douglas Gregor0a9a6d62011-01-26 17:36:28 +00001215 if (const CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(ND)) {
John McCall0953e762009-09-24 19:53:00 +00001216 mangleQualifiers(Qualifiers::fromCVRMask(Method->getTypeQualifiers()));
Douglas Gregor0a9a6d62011-01-26 17:36:28 +00001217 mangleRefQualifier(Method->getRefQualifier());
1218 }
1219
Anders Carlsson2744a062009-09-18 19:00:18 +00001220 // Check if we have a template.
1221 const TemplateArgumentList *TemplateArgs = 0;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00001222 if (const TemplateDecl *TD = isTemplate(ND, TemplateArgs)) {
Anders Carlsson2744a062009-09-18 19:00:18 +00001223 mangleTemplatePrefix(TD);
Rafael Espindolad9800722010-03-11 14:07:00 +00001224 TemplateParameterList *TemplateParameters = TD->getTemplateParameters();
1225 mangleTemplateArgs(*TemplateParameters, *TemplateArgs);
Fariborz Jahanian57058532010-03-03 19:41:08 +00001226 }
1227 else {
1228 manglePrefix(DC, NoFunction);
Anders Carlsson7482e242009-09-18 04:29:09 +00001229 mangleUnqualifiedName(ND);
1230 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00001231
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001232 Out << 'E';
1233}
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00001234void CXXNameMangler::mangleNestedName(const TemplateDecl *TD,
Anders Carlsson7624f212009-09-18 02:42:01 +00001235 const TemplateArgument *TemplateArgs,
1236 unsigned NumTemplateArgs) {
Anders Carlssone45117b2009-09-27 19:53:49 +00001237 // <nested-name> ::= N [<CV-qualifiers>] <template-prefix> <template-args> E
1238
Anders Carlsson7624f212009-09-18 02:42:01 +00001239 Out << 'N';
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00001240
Anders Carlssone45117b2009-09-27 19:53:49 +00001241 mangleTemplatePrefix(TD);
Rafael Espindolad9800722010-03-11 14:07:00 +00001242 TemplateParameterList *TemplateParameters = TD->getTemplateParameters();
1243 mangleTemplateArgs(*TemplateParameters, TemplateArgs, NumTemplateArgs);
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00001244
Anders Carlsson7624f212009-09-18 02:42:01 +00001245 Out << 'E';
1246}
1247
Anders Carlsson1b42c792009-04-02 16:24:45 +00001248void CXXNameMangler::mangleLocalName(const NamedDecl *ND) {
1249 // <local-name> := Z <function encoding> E <entity name> [<discriminator>]
1250 // := Z <function encoding> E s [<discriminator>]
Douglas Gregorccc1b5e2012-02-21 00:37:24 +00001251 // <local-name> := Z <function encoding> E d [ <parameter number> ]
1252 // _ <entity name>
Mike Stump1eb44332009-09-09 15:08:12 +00001253 // <discriminator> := _ <non-negative number>
Douglas Gregorccc1b5e2012-02-21 00:37:24 +00001254 const DeclContext *DC = getEffectiveDeclContext(ND);
Fariborz Jahanian8805fe82011-06-09 19:25:01 +00001255 if (isa<ObjCMethodDecl>(DC) && isa<FunctionDecl>(ND)) {
1256 // Don't add objc method name mangling to locally declared function
1257 mangleUnqualifiedName(ND);
1258 return;
1259 }
1260
Anders Carlsson1b42c792009-04-02 16:24:45 +00001261 Out << 'Z';
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001262
Charles Davis685b1d92010-05-26 18:25:27 +00001263 if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(DC)) {
1264 mangleObjCMethodName(MD);
John McCall82b7d7b2010-10-18 21:28:44 +00001265 } else if (const CXXRecordDecl *RD = GetLocalClassDecl(ND)) {
Douglas Gregorccc1b5e2012-02-21 00:37:24 +00001266 mangleFunctionEncoding(cast<FunctionDecl>(getEffectiveDeclContext(RD)));
Fariborz Jahanian57058532010-03-03 19:41:08 +00001267 Out << 'E';
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001268
Douglas Gregorccc1b5e2012-02-21 00:37:24 +00001269 // The parameter number is omitted for the last parameter, 0 for the
1270 // second-to-last parameter, 1 for the third-to-last parameter, etc. The
1271 // <entity name> will of course contain a <closure-type-name>: Its
1272 // numbering will be local to the particular argument in which it appears
1273 // -- other default arguments do not affect its encoding.
1274 bool SkipDiscriminator = false;
1275 if (RD->isLambda()) {
1276 if (const ParmVarDecl *Parm
1277 = dyn_cast_or_null<ParmVarDecl>(RD->getLambdaContextDecl())) {
1278 if (const FunctionDecl *Func
1279 = dyn_cast<FunctionDecl>(Parm->getDeclContext())) {
1280 Out << 'd';
1281 unsigned Num = Func->getNumParams() - Parm->getFunctionScopeIndex();
1282 if (Num > 1)
1283 mangleNumber(Num - 2);
1284 Out << '_';
1285 SkipDiscriminator = true;
1286 }
1287 }
1288 }
1289
John McCall82b7d7b2010-10-18 21:28:44 +00001290 // Mangle the name relative to the closest enclosing function.
1291 if (ND == RD) // equality ok because RD derived from ND above
1292 mangleUnqualifiedName(ND);
1293 else
1294 mangleNestedName(ND, DC, true /*NoFunction*/);
1295
Douglas Gregorccc1b5e2012-02-21 00:37:24 +00001296 if (!SkipDiscriminator) {
1297 unsigned disc;
1298 if (Context.getNextDiscriminator(RD, disc)) {
1299 if (disc < 10)
1300 Out << '_' << disc;
1301 else
1302 Out << "__" << disc << '_';
1303 }
Fariborz Jahanian4819ac42010-03-04 01:02:03 +00001304 }
Douglas Gregorccc1b5e2012-02-21 00:37:24 +00001305
Fariborz Jahanian57058532010-03-03 19:41:08 +00001306 return;
1307 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001308 else
Fariborz Jahanian57058532010-03-03 19:41:08 +00001309 mangleFunctionEncoding(cast<FunctionDecl>(DC));
Anders Carlsson7b06f6c2009-12-10 03:14:39 +00001310
Anders Carlsson1b42c792009-04-02 16:24:45 +00001311 Out << 'E';
Eli Friedman6f9f25d2009-12-11 20:21:38 +00001312 mangleUnqualifiedName(ND);
Anders Carlsson1b42c792009-04-02 16:24:45 +00001313}
1314
Douglas Gregorccc1b5e2012-02-21 00:37:24 +00001315void CXXNameMangler::mangleLambda(const CXXRecordDecl *Lambda) {
Douglas Gregor552e2992012-02-21 02:22:07 +00001316 // If the context of a closure type is an initializer for a class member
1317 // (static or nonstatic), it is encoded in a qualified name with a final
1318 // <prefix> of the form:
1319 //
1320 // <data-member-prefix> := <member source-name> M
1321 //
1322 // Technically, the data-member-prefix is part of the <prefix>. However,
1323 // since a closure type will always be mangled with a prefix, it's easier
1324 // to emit that last part of the prefix here.
1325 if (Decl *Context = Lambda->getLambdaContextDecl()) {
1326 if ((isa<VarDecl>(Context) || isa<FieldDecl>(Context)) &&
1327 Context->getDeclContext()->isRecord()) {
1328 if (const IdentifierInfo *Name
1329 = cast<NamedDecl>(Context)->getIdentifier()) {
1330 mangleSourceName(Name);
1331 Out << 'M';
1332 }
1333 }
1334 }
1335
Douglas Gregorccc1b5e2012-02-21 00:37:24 +00001336 Out << "Ul";
1337 DeclarationName Name
1338 = getASTContext().DeclarationNames.getCXXOperatorName(OO_Call);
1339 const FunctionProtoType *Proto
1340 = cast<CXXMethodDecl>(*Lambda->lookup(Name).first)->getType()->
1341 getAs<FunctionProtoType>();
1342 mangleBareFunctionType(Proto, /*MangleReturnType=*/false);
1343 Out << "E";
1344
1345 // The number is omitted for the first closure type with a given
1346 // <lambda-sig> in a given context; it is n-2 for the nth closure type
1347 // (in lexical order) with that same <lambda-sig> and context.
1348 //
1349 // The AST keeps track of the number for us.
Douglas Gregor5878cbc2012-02-21 04:17:39 +00001350 unsigned Number = Lambda->getLambdaManglingNumber();
1351 assert(Number > 0 && "Lambda should be mangled as an unnamed class");
1352 if (Number > 1)
1353 mangleNumber(Number - 2);
Douglas Gregorccc1b5e2012-02-21 00:37:24 +00001354 Out << '_';
1355}
1356
John McCalla0ce15c2011-04-24 08:23:24 +00001357void CXXNameMangler::manglePrefix(NestedNameSpecifier *qualifier) {
1358 switch (qualifier->getKind()) {
1359 case NestedNameSpecifier::Global:
1360 // nothing
1361 return;
1362
1363 case NestedNameSpecifier::Namespace:
1364 mangleName(qualifier->getAsNamespace());
1365 return;
1366
1367 case NestedNameSpecifier::NamespaceAlias:
1368 mangleName(qualifier->getAsNamespaceAlias()->getNamespace());
1369 return;
1370
1371 case NestedNameSpecifier::TypeSpec:
1372 case NestedNameSpecifier::TypeSpecWithTemplate:
John McCall4f4e4132011-05-04 01:45:19 +00001373 manglePrefix(QualType(qualifier->getAsType(), 0));
John McCalla0ce15c2011-04-24 08:23:24 +00001374 return;
1375
1376 case NestedNameSpecifier::Identifier:
1377 // Member expressions can have these without prefixes, but that
1378 // should end up in mangleUnresolvedPrefix instead.
1379 assert(qualifier->getPrefix());
1380 manglePrefix(qualifier->getPrefix());
1381
1382 mangleSourceName(qualifier->getAsIdentifier());
1383 return;
1384 }
1385
1386 llvm_unreachable("unexpected nested name specifier");
1387}
1388
Fariborz Jahanian57058532010-03-03 19:41:08 +00001389void CXXNameMangler::manglePrefix(const DeclContext *DC, bool NoFunction) {
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001390 // <prefix> ::= <prefix> <unqualified-name>
1391 // ::= <template-prefix> <template-args>
1392 // ::= <template-param>
1393 // ::= # empty
1394 // ::= <substitution>
Anders Carlsson6862fc72009-09-17 04:16:28 +00001395
James Molloyb3c312c2012-03-05 09:59:43 +00001396 DC = IgnoreLinkageSpecDecls(DC);
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00001397
Anders Carlsson9263e912009-09-18 18:39:58 +00001398 if (DC->isTranslationUnit())
1399 return;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00001400
Douglas Gregor35415f52010-05-25 17:04:15 +00001401 if (const BlockDecl *Block = dyn_cast<BlockDecl>(DC)) {
Douglas Gregorccc1b5e2012-02-21 00:37:24 +00001402 manglePrefix(getEffectiveParentContext(DC), NoFunction);
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00001403 SmallString<64> Name;
Rafael Espindolac4850c22011-02-10 23:59:36 +00001404 llvm::raw_svector_ostream NameStream(Name);
1405 Context.mangleBlock(Block, NameStream);
1406 NameStream.flush();
Douglas Gregor35415f52010-05-25 17:04:15 +00001407 Out << Name.size() << Name;
1408 return;
1409 }
1410
Douglas Gregor552e2992012-02-21 02:22:07 +00001411 const NamedDecl *ND = cast<NamedDecl>(DC);
1412 if (mangleSubstitution(ND))
Anders Carlsson6862fc72009-09-17 04:16:28 +00001413 return;
Douglas Gregor552e2992012-02-21 02:22:07 +00001414
Anders Carlsson2ee3fca2009-09-18 20:11:09 +00001415 // Check if we have a template.
1416 const TemplateArgumentList *TemplateArgs = 0;
Douglas Gregor552e2992012-02-21 02:22:07 +00001417 if (const TemplateDecl *TD = isTemplate(ND, TemplateArgs)) {
Anders Carlsson2ee3fca2009-09-18 20:11:09 +00001418 mangleTemplatePrefix(TD);
Rafael Espindolad9800722010-03-11 14:07:00 +00001419 TemplateParameterList *TemplateParameters = TD->getTemplateParameters();
1420 mangleTemplateArgs(*TemplateParameters, *TemplateArgs);
Fariborz Jahanian57058532010-03-03 19:41:08 +00001421 }
Douglas Gregor552e2992012-02-21 02:22:07 +00001422 else if(NoFunction && (isa<FunctionDecl>(ND) || isa<ObjCMethodDecl>(ND)))
Fariborz Jahanian57058532010-03-03 19:41:08 +00001423 return;
Douglas Gregor552e2992012-02-21 02:22:07 +00001424 else if (const ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(ND))
Douglas Gregor35415f52010-05-25 17:04:15 +00001425 mangleObjCMethodName(Method);
Fariborz Jahanian57058532010-03-03 19:41:08 +00001426 else {
Douglas Gregor552e2992012-02-21 02:22:07 +00001427 manglePrefix(getEffectiveDeclContext(ND), NoFunction);
1428 mangleUnqualifiedName(ND);
Anders Carlsson2ee3fca2009-09-18 20:11:09 +00001429 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00001430
Douglas Gregor552e2992012-02-21 02:22:07 +00001431 addSubstitution(ND);
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001432}
1433
Douglas Gregor20f0cc72010-04-23 03:10:43 +00001434void CXXNameMangler::mangleTemplatePrefix(TemplateName Template) {
1435 // <template-prefix> ::= <prefix> <template unqualified-name>
1436 // ::= <template-param>
1437 // ::= <substitution>
1438 if (TemplateDecl *TD = Template.getAsTemplateDecl())
1439 return mangleTemplatePrefix(TD);
1440
1441 if (QualifiedTemplateName *Qualified = Template.getAsQualifiedTemplateName())
John McCalla0ce15c2011-04-24 08:23:24 +00001442 manglePrefix(Qualified->getQualifier());
Sean Huntc3021132010-05-05 15:23:54 +00001443
Douglas Gregor20f0cc72010-04-23 03:10:43 +00001444 if (OverloadedTemplateStorage *Overloaded
1445 = Template.getAsOverloadedTemplate()) {
Sean Huntc3021132010-05-05 15:23:54 +00001446 mangleUnqualifiedName(0, (*Overloaded->begin())->getDeclName(),
Douglas Gregor20f0cc72010-04-23 03:10:43 +00001447 UnknownArity);
1448 return;
1449 }
Sean Huntc3021132010-05-05 15:23:54 +00001450
Douglas Gregor20f0cc72010-04-23 03:10:43 +00001451 DependentTemplateName *Dependent = Template.getAsDependentTemplateName();
1452 assert(Dependent && "Unknown template name kind?");
John McCalla0ce15c2011-04-24 08:23:24 +00001453 manglePrefix(Dependent->getQualifier());
Douglas Gregor1e9268e2010-04-28 05:58:56 +00001454 mangleUnscopedTemplateName(Template);
Douglas Gregor20f0cc72010-04-23 03:10:43 +00001455}
1456
Anders Carlsson0fa6df42009-09-26 19:45:45 +00001457void CXXNameMangler::mangleTemplatePrefix(const TemplateDecl *ND) {
Anders Carlsson7482e242009-09-18 04:29:09 +00001458 // <template-prefix> ::= <prefix> <template unqualified-name>
1459 // ::= <template-param>
1460 // ::= <substitution>
Douglas Gregor32fb4e12010-02-05 20:45:00 +00001461 // <template-template-param> ::= <template-param>
1462 // <substitution>
Anders Carlsson7482e242009-09-18 04:29:09 +00001463
Anders Carlssonaeb85372009-09-26 22:18:22 +00001464 if (mangleSubstitution(ND))
1465 return;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00001466
Douglas Gregor32fb4e12010-02-05 20:45:00 +00001467 // <template-template-param> ::= <template-param>
1468 if (const TemplateTemplateParmDecl *TTP
1469 = dyn_cast<TemplateTemplateParmDecl>(ND)) {
1470 mangleTemplateParameter(TTP->getIndex());
1471 return;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001472 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00001473
Douglas Gregorccc1b5e2012-02-21 00:37:24 +00001474 manglePrefix(getEffectiveDeclContext(ND));
Anders Carlsson1668f202009-09-26 20:13:56 +00001475 mangleUnqualifiedName(ND->getTemplatedDecl());
Anders Carlssonaeb85372009-09-26 22:18:22 +00001476 addSubstitution(ND);
Anders Carlsson7482e242009-09-18 04:29:09 +00001477}
1478
John McCallb6f532e2010-07-14 06:43:17 +00001479/// Mangles a template name under the production <type>. Required for
1480/// template template arguments.
1481/// <type> ::= <class-enum-type>
1482/// ::= <template-param>
1483/// ::= <substitution>
1484void CXXNameMangler::mangleType(TemplateName TN) {
1485 if (mangleSubstitution(TN))
1486 return;
1487
1488 TemplateDecl *TD = 0;
1489
1490 switch (TN.getKind()) {
1491 case TemplateName::QualifiedTemplate:
1492 TD = TN.getAsQualifiedTemplateName()->getTemplateDecl();
1493 goto HaveDecl;
1494
1495 case TemplateName::Template:
1496 TD = TN.getAsTemplateDecl();
1497 goto HaveDecl;
1498
1499 HaveDecl:
1500 if (isa<TemplateTemplateParmDecl>(TD))
1501 mangleTemplateParameter(cast<TemplateTemplateParmDecl>(TD)->getIndex());
1502 else
1503 mangleName(TD);
1504 break;
1505
1506 case TemplateName::OverloadedTemplate:
1507 llvm_unreachable("can't mangle an overloaded template name as a <type>");
John McCallb6f532e2010-07-14 06:43:17 +00001508
1509 case TemplateName::DependentTemplate: {
1510 const DependentTemplateName *Dependent = TN.getAsDependentTemplateName();
1511 assert(Dependent->isIdentifier());
1512
1513 // <class-enum-type> ::= <name>
1514 // <name> ::= <nested-name>
John McCalla0ce15c2011-04-24 08:23:24 +00001515 mangleUnresolvedPrefix(Dependent->getQualifier(), 0);
John McCallb6f532e2010-07-14 06:43:17 +00001516 mangleSourceName(Dependent->getIdentifier());
1517 break;
1518 }
1519
John McCallb44e0cf2011-06-30 21:59:02 +00001520 case TemplateName::SubstTemplateTemplateParm: {
1521 // Substituted template parameters are mangled as the substituted
1522 // template. This will check for the substitution twice, which is
1523 // fine, but we have to return early so that we don't try to *add*
1524 // the substitution twice.
1525 SubstTemplateTemplateParmStorage *subst
1526 = TN.getAsSubstTemplateTemplateParm();
1527 mangleType(subst->getReplacement());
1528 return;
1529 }
John McCall14606042011-06-30 08:33:18 +00001530
Douglas Gregor1aee05d2011-01-15 06:45:20 +00001531 case TemplateName::SubstTemplateTemplateParmPack: {
John McCall68a51a72011-07-01 00:04:39 +00001532 // FIXME: not clear how to mangle this!
1533 // template <template <class> class T...> class A {
1534 // template <template <class> class U...> void foo(B<T,U> x...);
1535 // };
1536 Out << "_SUBSTPACK_";
Douglas Gregor1aee05d2011-01-15 06:45:20 +00001537 break;
1538 }
John McCallb6f532e2010-07-14 06:43:17 +00001539 }
1540
1541 addSubstitution(TN);
1542}
1543
Mike Stump1eb44332009-09-09 15:08:12 +00001544void
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001545CXXNameMangler::mangleOperatorName(OverloadedOperatorKind OO, unsigned Arity) {
1546 switch (OO) {
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001547 // <operator-name> ::= nw # new
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001548 case OO_New: Out << "nw"; break;
1549 // ::= na # new[]
1550 case OO_Array_New: Out << "na"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001551 // ::= dl # delete
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001552 case OO_Delete: Out << "dl"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001553 // ::= da # delete[]
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001554 case OO_Array_Delete: Out << "da"; break;
1555 // ::= ps # + (unary)
John McCall5e1e89b2010-08-18 19:18:59 +00001556 // ::= pl # + (binary or unknown)
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001557 case OO_Plus:
Anders Carlsson8257d412009-12-22 06:36:32 +00001558 Out << (Arity == 1? "ps" : "pl"); break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001559 // ::= ng # - (unary)
John McCall5e1e89b2010-08-18 19:18:59 +00001560 // ::= mi # - (binary or unknown)
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001561 case OO_Minus:
Anders Carlsson8257d412009-12-22 06:36:32 +00001562 Out << (Arity == 1? "ng" : "mi"); break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001563 // ::= ad # & (unary)
John McCall5e1e89b2010-08-18 19:18:59 +00001564 // ::= an # & (binary or unknown)
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001565 case OO_Amp:
Anders Carlsson8257d412009-12-22 06:36:32 +00001566 Out << (Arity == 1? "ad" : "an"); break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001567 // ::= de # * (unary)
John McCall5e1e89b2010-08-18 19:18:59 +00001568 // ::= ml # * (binary or unknown)
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001569 case OO_Star:
John McCall5e1e89b2010-08-18 19:18:59 +00001570 // Use binary when unknown.
Anders Carlsson8257d412009-12-22 06:36:32 +00001571 Out << (Arity == 1? "de" : "ml"); break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001572 // ::= co # ~
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001573 case OO_Tilde: Out << "co"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001574 // ::= dv # /
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001575 case OO_Slash: Out << "dv"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001576 // ::= rm # %
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001577 case OO_Percent: Out << "rm"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001578 // ::= or # |
1579 case OO_Pipe: Out << "or"; break;
1580 // ::= eo # ^
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001581 case OO_Caret: Out << "eo"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001582 // ::= aS # =
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001583 case OO_Equal: Out << "aS"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001584 // ::= pL # +=
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001585 case OO_PlusEqual: Out << "pL"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001586 // ::= mI # -=
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001587 case OO_MinusEqual: Out << "mI"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001588 // ::= mL # *=
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001589 case OO_StarEqual: Out << "mL"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001590 // ::= dV # /=
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001591 case OO_SlashEqual: Out << "dV"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001592 // ::= rM # %=
1593 case OO_PercentEqual: Out << "rM"; break;
1594 // ::= aN # &=
1595 case OO_AmpEqual: Out << "aN"; break;
1596 // ::= oR # |=
1597 case OO_PipeEqual: Out << "oR"; break;
1598 // ::= eO # ^=
1599 case OO_CaretEqual: Out << "eO"; break;
1600 // ::= ls # <<
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001601 case OO_LessLess: Out << "ls"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001602 // ::= rs # >>
1603 case OO_GreaterGreater: Out << "rs"; break;
1604 // ::= lS # <<=
1605 case OO_LessLessEqual: Out << "lS"; break;
1606 // ::= rS # >>=
1607 case OO_GreaterGreaterEqual: Out << "rS"; break;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001608 // ::= eq # ==
1609 case OO_EqualEqual: Out << "eq"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001610 // ::= ne # !=
1611 case OO_ExclaimEqual: Out << "ne"; break;
1612 // ::= lt # <
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001613 case OO_Less: Out << "lt"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001614 // ::= gt # >
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001615 case OO_Greater: Out << "gt"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001616 // ::= le # <=
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001617 case OO_LessEqual: Out << "le"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001618 // ::= ge # >=
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001619 case OO_GreaterEqual: Out << "ge"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001620 // ::= nt # !
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001621 case OO_Exclaim: Out << "nt"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001622 // ::= aa # &&
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001623 case OO_AmpAmp: Out << "aa"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001624 // ::= oo # ||
1625 case OO_PipePipe: Out << "oo"; break;
1626 // ::= pp # ++
1627 case OO_PlusPlus: Out << "pp"; break;
1628 // ::= mm # --
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001629 case OO_MinusMinus: Out << "mm"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001630 // ::= cm # ,
1631 case OO_Comma: Out << "cm"; break;
1632 // ::= pm # ->*
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001633 case OO_ArrowStar: Out << "pm"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001634 // ::= pt # ->
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001635 case OO_Arrow: Out << "pt"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001636 // ::= cl # ()
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001637 case OO_Call: Out << "cl"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001638 // ::= ix # []
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001639 case OO_Subscript: Out << "ix"; break;
Anders Carlssone170ba72009-12-14 01:45:37 +00001640
1641 // ::= qu # ?
1642 // The conditional operator can't be overloaded, but we still handle it when
1643 // mangling expressions.
1644 case OO_Conditional: Out << "qu"; break;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001645
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001646 case OO_None:
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001647 case NUM_OVERLOADED_OPERATORS:
David Blaikieb219cfc2011-09-23 05:06:16 +00001648 llvm_unreachable("Not an overloaded operator");
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001649 }
1650}
1651
John McCall0953e762009-09-24 19:53:00 +00001652void CXXNameMangler::mangleQualifiers(Qualifiers Quals) {
Mike Stump1eb44332009-09-09 15:08:12 +00001653 // <CV-qualifiers> ::= [r] [V] [K] # restrict (C99), volatile, const
John McCall0953e762009-09-24 19:53:00 +00001654 if (Quals.hasRestrict())
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001655 Out << 'r';
John McCall0953e762009-09-24 19:53:00 +00001656 if (Quals.hasVolatile())
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001657 Out << 'V';
John McCall0953e762009-09-24 19:53:00 +00001658 if (Quals.hasConst())
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001659 Out << 'K';
John McCall0953e762009-09-24 19:53:00 +00001660
Douglas Gregor56079f72010-06-14 23:15:08 +00001661 if (Quals.hasAddressSpace()) {
1662 // Extension:
1663 //
1664 // <type> ::= U <address-space-number>
1665 //
1666 // where <address-space-number> is a source name consisting of 'AS'
1667 // followed by the address space <number>.
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00001668 SmallString<64> ASString;
Douglas Gregor56079f72010-06-14 23:15:08 +00001669 ASString = "AS" + llvm::utostr_32(Quals.getAddressSpace());
1670 Out << 'U' << ASString.size() << ASString;
1671 }
1672
Chris Lattner5f9e2722011-07-23 10:55:15 +00001673 StringRef LifetimeName;
John McCallf85e1932011-06-15 23:02:42 +00001674 switch (Quals.getObjCLifetime()) {
1675 // Objective-C ARC Extension:
1676 //
1677 // <type> ::= U "__strong"
1678 // <type> ::= U "__weak"
1679 // <type> ::= U "__autoreleasing"
John McCallf85e1932011-06-15 23:02:42 +00001680 case Qualifiers::OCL_None:
1681 break;
1682
1683 case Qualifiers::OCL_Weak:
1684 LifetimeName = "__weak";
1685 break;
1686
1687 case Qualifiers::OCL_Strong:
1688 LifetimeName = "__strong";
1689 break;
1690
1691 case Qualifiers::OCL_Autoreleasing:
1692 LifetimeName = "__autoreleasing";
1693 break;
1694
1695 case Qualifiers::OCL_ExplicitNone:
Douglas Gregorc22d6992011-06-17 22:26:49 +00001696 // The __unsafe_unretained qualifier is *not* mangled, so that
1697 // __unsafe_unretained types in ARC produce the same manglings as the
1698 // equivalent (but, naturally, unqualified) types in non-ARC, providing
1699 // better ABI compatibility.
1700 //
1701 // It's safe to do this because unqualified 'id' won't show up
1702 // in any type signatures that need to be mangled.
John McCallf85e1932011-06-15 23:02:42 +00001703 break;
1704 }
1705 if (!LifetimeName.empty())
1706 Out << 'U' << LifetimeName.size() << LifetimeName;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001707}
1708
Douglas Gregor0a9a6d62011-01-26 17:36:28 +00001709void CXXNameMangler::mangleRefQualifier(RefQualifierKind RefQualifier) {
1710 // <ref-qualifier> ::= R # lvalue reference
1711 // ::= O # rvalue-reference
1712 // Proposal to Itanium C++ ABI list on 1/26/11
1713 switch (RefQualifier) {
1714 case RQ_None:
1715 break;
1716
1717 case RQ_LValue:
1718 Out << 'R';
1719 break;
1720
1721 case RQ_RValue:
1722 Out << 'O';
1723 break;
1724 }
1725}
1726
Anders Carlsson7b06f6c2009-12-10 03:14:39 +00001727void CXXNameMangler::mangleObjCMethodName(const ObjCMethodDecl *MD) {
Rafael Espindolaf0be9792011-02-11 02:52:17 +00001728 Context.mangleObjCMethodName(MD, Out);
Anders Carlsson7b06f6c2009-12-10 03:14:39 +00001729}
1730
Douglas Gregorf1588662011-07-12 15:18:55 +00001731void CXXNameMangler::mangleType(QualType T) {
1732 // If our type is instantiation-dependent but not dependent, we mangle
1733 // it as it was written in the source, removing any top-level sugar.
1734 // Otherwise, use the canonical type.
1735 //
1736 // FIXME: This is an approximation of the instantiation-dependent name
1737 // mangling rules, since we should really be using the type as written and
1738 // augmented via semantic analysis (i.e., with implicit conversions and
1739 // default template arguments) for any instantiation-dependent type.
1740 // Unfortunately, that requires several changes to our AST:
1741 // - Instantiation-dependent TemplateSpecializationTypes will need to be
1742 // uniqued, so that we can handle substitutions properly
1743 // - Default template arguments will need to be represented in the
1744 // TemplateSpecializationType, since they need to be mangled even though
1745 // they aren't written.
1746 // - Conversions on non-type template arguments need to be expressed, since
1747 // they can affect the mangling of sizeof/alignof.
1748 if (!T->isInstantiationDependentType() || T->isDependentType())
1749 T = T.getCanonicalType();
1750 else {
1751 // Desugar any types that are purely sugar.
1752 do {
1753 // Don't desugar through template specialization types that aren't
1754 // type aliases. We need to mangle the template arguments as written.
1755 if (const TemplateSpecializationType *TST
1756 = dyn_cast<TemplateSpecializationType>(T))
1757 if (!TST->isTypeAlias())
1758 break;
Anders Carlsson4843e582009-03-10 17:07:44 +00001759
Douglas Gregorf1588662011-07-12 15:18:55 +00001760 QualType Desugared
1761 = T.getSingleStepDesugaredType(Context.getASTContext());
1762 if (Desugared == T)
1763 break;
1764
1765 T = Desugared;
1766 } while (true);
1767 }
1768 SplitQualType split = T.split();
John McCall200fa532012-02-08 00:46:36 +00001769 Qualifiers quals = split.Quals;
1770 const Type *ty = split.Ty;
John McCallb47f7482011-01-26 20:05:40 +00001771
Douglas Gregorf1588662011-07-12 15:18:55 +00001772 bool isSubstitutable = quals || !isa<BuiltinType>(T);
1773 if (isSubstitutable && mangleSubstitution(T))
Anders Carlsson76967372009-09-17 00:43:46 +00001774 return;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001775
John McCallb47f7482011-01-26 20:05:40 +00001776 // If we're mangling a qualified array type, push the qualifiers to
1777 // the element type.
Douglas Gregorf1588662011-07-12 15:18:55 +00001778 if (quals && isa<ArrayType>(T)) {
1779 ty = Context.getASTContext().getAsArrayType(T);
John McCallb47f7482011-01-26 20:05:40 +00001780 quals = Qualifiers();
1781
Douglas Gregorf1588662011-07-12 15:18:55 +00001782 // Note that we don't update T: we want to add the
1783 // substitution at the original type.
John McCallb47f7482011-01-26 20:05:40 +00001784 }
1785
1786 if (quals) {
1787 mangleQualifiers(quals);
John McCall0953e762009-09-24 19:53:00 +00001788 // Recurse: even if the qualified type isn't yet substitutable,
1789 // the unqualified type might be.
John McCallb47f7482011-01-26 20:05:40 +00001790 mangleType(QualType(ty, 0));
Anders Carlsson76967372009-09-17 00:43:46 +00001791 } else {
John McCallb47f7482011-01-26 20:05:40 +00001792 switch (ty->getTypeClass()) {
John McCallefe6aee2009-09-05 07:56:18 +00001793#define ABSTRACT_TYPE(CLASS, PARENT)
1794#define NON_CANONICAL_TYPE(CLASS, PARENT) \
Anders Carlsson76967372009-09-17 00:43:46 +00001795 case Type::CLASS: \
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +00001796 llvm_unreachable("can't mangle non-canonical type " #CLASS "Type"); \
Anders Carlsson76967372009-09-17 00:43:46 +00001797 return;
John McCallefe6aee2009-09-05 07:56:18 +00001798#define TYPE(CLASS, PARENT) \
Anders Carlsson76967372009-09-17 00:43:46 +00001799 case Type::CLASS: \
John McCallb47f7482011-01-26 20:05:40 +00001800 mangleType(static_cast<const CLASS##Type*>(ty)); \
Anders Carlsson76967372009-09-17 00:43:46 +00001801 break;
John McCallefe6aee2009-09-05 07:56:18 +00001802#include "clang/AST/TypeNodes.def"
Anders Carlsson76967372009-09-17 00:43:46 +00001803 }
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001804 }
Anders Carlsson76967372009-09-17 00:43:46 +00001805
1806 // Add the substitution.
John McCallb47f7482011-01-26 20:05:40 +00001807 if (isSubstitutable)
Douglas Gregorf1588662011-07-12 15:18:55 +00001808 addSubstitution(T);
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001809}
1810
Douglas Gregor1b12a3b2010-05-26 05:11:13 +00001811void CXXNameMangler::mangleNameOrStandardSubstitution(const NamedDecl *ND) {
1812 if (!mangleStandardSubstitution(ND))
1813 mangleName(ND);
1814}
1815
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001816void CXXNameMangler::mangleType(const BuiltinType *T) {
John McCallefe6aee2009-09-05 07:56:18 +00001817 // <type> ::= <builtin-type>
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001818 // <builtin-type> ::= v # void
1819 // ::= w # wchar_t
1820 // ::= b # bool
1821 // ::= c # char
1822 // ::= a # signed char
1823 // ::= h # unsigned char
1824 // ::= s # short
1825 // ::= t # unsigned short
1826 // ::= i # int
1827 // ::= j # unsigned int
1828 // ::= l # long
1829 // ::= m # unsigned long
1830 // ::= x # long long, __int64
1831 // ::= y # unsigned long long, __int64
1832 // ::= n # __int128
1833 // UNSUPPORTED: ::= o # unsigned __int128
1834 // ::= f # float
1835 // ::= d # double
1836 // ::= e # long double, __float80
1837 // UNSUPPORTED: ::= g # __float128
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001838 // UNSUPPORTED: ::= Dd # IEEE 754r decimal floating point (64 bits)
1839 // UNSUPPORTED: ::= De # IEEE 754r decimal floating point (128 bits)
1840 // UNSUPPORTED: ::= Df # IEEE 754r decimal floating point (32 bits)
Anton Korobeynikovaa4a99b2011-10-14 23:23:15 +00001841 // ::= Dh # IEEE 754r half-precision floating point (16 bits)
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001842 // ::= Di # char32_t
1843 // ::= Ds # char16_t
Anders Carlssone2923682010-11-04 04:31:32 +00001844 // ::= Dn # std::nullptr_t (i.e., decltype(nullptr))
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001845 // ::= u <source-name> # vendor extended type
1846 switch (T->getKind()) {
1847 case BuiltinType::Void: Out << 'v'; break;
1848 case BuiltinType::Bool: Out << 'b'; break;
1849 case BuiltinType::Char_U: case BuiltinType::Char_S: Out << 'c'; break;
1850 case BuiltinType::UChar: Out << 'h'; break;
1851 case BuiltinType::UShort: Out << 't'; break;
1852 case BuiltinType::UInt: Out << 'j'; break;
1853 case BuiltinType::ULong: Out << 'm'; break;
1854 case BuiltinType::ULongLong: Out << 'y'; break;
Chris Lattner2df9ced2009-04-30 02:43:43 +00001855 case BuiltinType::UInt128: Out << 'o'; break;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001856 case BuiltinType::SChar: Out << 'a'; break;
Chris Lattner3f59c972010-12-25 23:25:43 +00001857 case BuiltinType::WChar_S:
1858 case BuiltinType::WChar_U: Out << 'w'; break;
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001859 case BuiltinType::Char16: Out << "Ds"; break;
1860 case BuiltinType::Char32: Out << "Di"; break;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001861 case BuiltinType::Short: Out << 's'; break;
1862 case BuiltinType::Int: Out << 'i'; break;
1863 case BuiltinType::Long: Out << 'l'; break;
1864 case BuiltinType::LongLong: Out << 'x'; break;
Chris Lattner2df9ced2009-04-30 02:43:43 +00001865 case BuiltinType::Int128: Out << 'n'; break;
Anton Korobeynikovaa4a99b2011-10-14 23:23:15 +00001866 case BuiltinType::Half: Out << "Dh"; break;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001867 case BuiltinType::Float: Out << 'f'; break;
1868 case BuiltinType::Double: Out << 'd'; break;
1869 case BuiltinType::LongDouble: Out << 'e'; break;
Anders Carlssone2923682010-11-04 04:31:32 +00001870 case BuiltinType::NullPtr: Out << "Dn"; break;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001871
John McCalle0a22d02011-10-18 21:02:43 +00001872#define BUILTIN_TYPE(Id, SingletonId)
1873#define PLACEHOLDER_TYPE(Id, SingletonId) \
1874 case BuiltinType::Id:
1875#include "clang/AST/BuiltinTypes.def"
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001876 case BuiltinType::Dependent:
John McCallfb44de92011-05-01 22:35:37 +00001877 llvm_unreachable("mangling a placeholder type");
Steve Naroff9533a7f2009-07-22 17:14:51 +00001878 case BuiltinType::ObjCId: Out << "11objc_object"; break;
1879 case BuiltinType::ObjCClass: Out << "10objc_class"; break;
Fariborz Jahanian13dcd002009-11-21 19:53:08 +00001880 case BuiltinType::ObjCSel: Out << "13objc_selector"; break;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001881 }
1882}
1883
John McCallefe6aee2009-09-05 07:56:18 +00001884// <type> ::= <function-type>
1885// <function-type> ::= F [Y] <bare-function-type> E
1886void CXXNameMangler::mangleType(const FunctionProtoType *T) {
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001887 Out << 'F';
Mike Stumpf5408fe2009-05-16 07:57:57 +00001888 // FIXME: We don't have enough information in the AST to produce the 'Y'
1889 // encoding for extern "C" function types.
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001890 mangleBareFunctionType(T, /*MangleReturnType=*/true);
1891 Out << 'E';
1892}
John McCallefe6aee2009-09-05 07:56:18 +00001893void CXXNameMangler::mangleType(const FunctionNoProtoType *T) {
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +00001894 llvm_unreachable("Can't mangle K&R function prototypes");
John McCallefe6aee2009-09-05 07:56:18 +00001895}
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001896void CXXNameMangler::mangleBareFunctionType(const FunctionType *T,
1897 bool MangleReturnType) {
John McCallefe6aee2009-09-05 07:56:18 +00001898 // We should never be mangling something without a prototype.
1899 const FunctionProtoType *Proto = cast<FunctionProtoType>(T);
1900
John McCallfb44de92011-05-01 22:35:37 +00001901 // Record that we're in a function type. See mangleFunctionParam
1902 // for details on what we're trying to achieve here.
1903 FunctionTypeDepthState saved = FunctionTypeDepth.push();
1904
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001905 // <bare-function-type> ::= <signature type>+
John McCallfb44de92011-05-01 22:35:37 +00001906 if (MangleReturnType) {
1907 FunctionTypeDepth.enterResultType();
John McCallefe6aee2009-09-05 07:56:18 +00001908 mangleType(Proto->getResultType());
John McCallfb44de92011-05-01 22:35:37 +00001909 FunctionTypeDepth.leaveResultType();
1910 }
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001911
Anders Carlsson93296682010-06-02 04:40:13 +00001912 if (Proto->getNumArgs() == 0 && !Proto->isVariadic()) {
Eli Friedmana7e68452010-08-22 01:00:03 +00001913 // <builtin-type> ::= v # void
Anders Carlssonc6c91bc2009-04-01 00:15:23 +00001914 Out << 'v';
John McCallfb44de92011-05-01 22:35:37 +00001915
1916 FunctionTypeDepth.pop(saved);
Anders Carlssonc6c91bc2009-04-01 00:15:23 +00001917 return;
1918 }
Mike Stump1eb44332009-09-09 15:08:12 +00001919
Douglas Gregor72564e72009-02-26 23:50:07 +00001920 for (FunctionProtoType::arg_type_iterator Arg = Proto->arg_type_begin(),
Mike Stump1eb44332009-09-09 15:08:12 +00001921 ArgEnd = Proto->arg_type_end();
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001922 Arg != ArgEnd; ++Arg)
Douglas Gregor79e6bd32011-07-12 04:42:08 +00001923 mangleType(Context.getASTContext().getSignatureParameterType(*Arg));
Douglas Gregor219cc612009-02-13 01:28:03 +00001924
John McCallfb44de92011-05-01 22:35:37 +00001925 FunctionTypeDepth.pop(saved);
1926
Douglas Gregor219cc612009-02-13 01:28:03 +00001927 // <builtin-type> ::= z # ellipsis
1928 if (Proto->isVariadic())
1929 Out << 'z';
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001930}
1931
John McCallefe6aee2009-09-05 07:56:18 +00001932// <type> ::= <class-enum-type>
Mike Stump1eb44332009-09-09 15:08:12 +00001933// <class-enum-type> ::= <name>
John McCalled976492009-12-04 22:46:56 +00001934void CXXNameMangler::mangleType(const UnresolvedUsingType *T) {
1935 mangleName(T->getDecl());
1936}
1937
1938// <type> ::= <class-enum-type>
1939// <class-enum-type> ::= <name>
John McCallefe6aee2009-09-05 07:56:18 +00001940void CXXNameMangler::mangleType(const EnumType *T) {
1941 mangleType(static_cast<const TagType*>(T));
1942}
1943void CXXNameMangler::mangleType(const RecordType *T) {
1944 mangleType(static_cast<const TagType*>(T));
1945}
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001946void CXXNameMangler::mangleType(const TagType *T) {
Eli Friedmanecb7e932009-12-11 18:00:57 +00001947 mangleName(T->getDecl());
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001948}
1949
John McCallefe6aee2009-09-05 07:56:18 +00001950// <type> ::= <array-type>
1951// <array-type> ::= A <positive dimension number> _ <element type>
1952// ::= A [<dimension expression>] _ <element type>
1953void CXXNameMangler::mangleType(const ConstantArrayType *T) {
1954 Out << 'A' << T->getSize() << '_';
1955 mangleType(T->getElementType());
1956}
1957void CXXNameMangler::mangleType(const VariableArrayType *T) {
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001958 Out << 'A';
Fariborz Jahanian7281d1f2010-11-02 16:54:00 +00001959 // decayed vla types (size 0) will just be skipped.
1960 if (T->getSizeExpr())
1961 mangleExpression(T->getSizeExpr());
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001962 Out << '_';
1963 mangleType(T->getElementType());
1964}
John McCallefe6aee2009-09-05 07:56:18 +00001965void CXXNameMangler::mangleType(const DependentSizedArrayType *T) {
1966 Out << 'A';
1967 mangleExpression(T->getSizeExpr());
1968 Out << '_';
1969 mangleType(T->getElementType());
1970}
1971void CXXNameMangler::mangleType(const IncompleteArrayType *T) {
Nick Lewycky271b6652010-09-05 03:40:33 +00001972 Out << "A_";
John McCallefe6aee2009-09-05 07:56:18 +00001973 mangleType(T->getElementType());
1974}
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001975
John McCallefe6aee2009-09-05 07:56:18 +00001976// <type> ::= <pointer-to-member-type>
1977// <pointer-to-member-type> ::= M <class type> <member type>
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001978void CXXNameMangler::mangleType(const MemberPointerType *T) {
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001979 Out << 'M';
1980 mangleType(QualType(T->getClass(), 0));
Anders Carlsson0e650012009-05-17 17:41:20 +00001981 QualType PointeeType = T->getPointeeType();
1982 if (const FunctionProtoType *FPT = dyn_cast<FunctionProtoType>(PointeeType)) {
John McCall0953e762009-09-24 19:53:00 +00001983 mangleQualifiers(Qualifiers::fromCVRMask(FPT->getTypeQuals()));
Douglas Gregor0a9a6d62011-01-26 17:36:28 +00001984 mangleRefQualifier(FPT->getRefQualifier());
Anders Carlsson0e650012009-05-17 17:41:20 +00001985 mangleType(FPT);
Anders Carlsson9d85b722010-06-02 04:29:50 +00001986
1987 // Itanium C++ ABI 5.1.8:
1988 //
1989 // The type of a non-static member function is considered to be different,
1990 // for the purposes of substitution, from the type of a namespace-scope or
1991 // static member function whose type appears similar. The types of two
1992 // non-static member functions are considered to be different, for the
1993 // purposes of substitution, if the functions are members of different
1994 // classes. In other words, for the purposes of substitution, the class of
1995 // which the function is a member is considered part of the type of
1996 // function.
1997
1998 // We increment the SeqID here to emulate adding an entry to the
1999 // substitution table. We can't actually add it because we don't want this
2000 // particular function type to be substituted.
2001 ++SeqID;
Mike Stump1eb44332009-09-09 15:08:12 +00002002 } else
Anders Carlsson0e650012009-05-17 17:41:20 +00002003 mangleType(PointeeType);
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00002004}
2005
John McCallefe6aee2009-09-05 07:56:18 +00002006// <type> ::= <template-param>
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00002007void CXXNameMangler::mangleType(const TemplateTypeParmType *T) {
Anders Carlsson0ccdf8d2009-09-27 00:38:53 +00002008 mangleTemplateParameter(T->getIndex());
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00002009}
2010
Douglas Gregorc3069d62011-01-14 02:55:32 +00002011// <type> ::= <template-param>
2012void CXXNameMangler::mangleType(const SubstTemplateTypeParmPackType *T) {
John McCall68a51a72011-07-01 00:04:39 +00002013 // FIXME: not clear how to mangle this!
2014 // template <class T...> class A {
2015 // template <class U...> void foo(T(*)(U) x...);
2016 // };
2017 Out << "_SUBSTPACK_";
Douglas Gregorc3069d62011-01-14 02:55:32 +00002018}
2019
John McCallefe6aee2009-09-05 07:56:18 +00002020// <type> ::= P <type> # pointer-to
2021void CXXNameMangler::mangleType(const PointerType *T) {
2022 Out << 'P';
2023 mangleType(T->getPointeeType());
2024}
2025void CXXNameMangler::mangleType(const ObjCObjectPointerType *T) {
2026 Out << 'P';
2027 mangleType(T->getPointeeType());
2028}
2029
2030// <type> ::= R <type> # reference-to
2031void CXXNameMangler::mangleType(const LValueReferenceType *T) {
2032 Out << 'R';
2033 mangleType(T->getPointeeType());
2034}
2035
2036// <type> ::= O <type> # rvalue reference-to (C++0x)
2037void CXXNameMangler::mangleType(const RValueReferenceType *T) {
2038 Out << 'O';
2039 mangleType(T->getPointeeType());
2040}
2041
2042// <type> ::= C <type> # complex pair (C 2000)
2043void CXXNameMangler::mangleType(const ComplexType *T) {
2044 Out << 'C';
2045 mangleType(T->getElementType());
2046}
2047
Bob Wilsonc7df92d2010-11-12 17:24:43 +00002048// ARM's ABI for Neon vector types specifies that they should be mangled as
Bob Wilson57147a82010-11-16 00:32:18 +00002049// if they are structs (to match ARM's initial implementation). The
2050// vector type must be one of the special types predefined by ARM.
2051void CXXNameMangler::mangleNeonVectorType(const VectorType *T) {
Bob Wilsonc7df92d2010-11-12 17:24:43 +00002052 QualType EltType = T->getElementType();
Bob Wilson57147a82010-11-16 00:32:18 +00002053 assert(EltType->isBuiltinType() && "Neon vector element not a BuiltinType");
Bob Wilsonc7df92d2010-11-12 17:24:43 +00002054 const char *EltName = 0;
Bob Wilson491328c2010-11-12 17:24:46 +00002055 if (T->getVectorKind() == VectorType::NeonPolyVector) {
2056 switch (cast<BuiltinType>(EltType)->getKind()) {
Bob Wilson4cfaa5d2010-11-12 17:24:49 +00002057 case BuiltinType::SChar: EltName = "poly8_t"; break;
2058 case BuiltinType::Short: EltName = "poly16_t"; break;
Bob Wilson57147a82010-11-16 00:32:18 +00002059 default: llvm_unreachable("unexpected Neon polynomial vector element type");
Bob Wilson491328c2010-11-12 17:24:46 +00002060 }
2061 } else {
2062 switch (cast<BuiltinType>(EltType)->getKind()) {
Bob Wilson4cfaa5d2010-11-12 17:24:49 +00002063 case BuiltinType::SChar: EltName = "int8_t"; break;
2064 case BuiltinType::UChar: EltName = "uint8_t"; break;
2065 case BuiltinType::Short: EltName = "int16_t"; break;
2066 case BuiltinType::UShort: EltName = "uint16_t"; break;
2067 case BuiltinType::Int: EltName = "int32_t"; break;
2068 case BuiltinType::UInt: EltName = "uint32_t"; break;
2069 case BuiltinType::LongLong: EltName = "int64_t"; break;
2070 case BuiltinType::ULongLong: EltName = "uint64_t"; break;
2071 case BuiltinType::Float: EltName = "float32_t"; break;
Bob Wilson57147a82010-11-16 00:32:18 +00002072 default: llvm_unreachable("unexpected Neon vector element type");
Bob Wilson491328c2010-11-12 17:24:46 +00002073 }
Bob Wilsonc7df92d2010-11-12 17:24:43 +00002074 }
2075 const char *BaseName = 0;
Bob Wilson4cfaa5d2010-11-12 17:24:49 +00002076 unsigned BitSize = (T->getNumElements() *
Bob Wilson3a723022010-11-16 00:32:12 +00002077 getASTContext().getTypeSize(EltType));
Bob Wilsonc7df92d2010-11-12 17:24:43 +00002078 if (BitSize == 64)
2079 BaseName = "__simd64_";
Bob Wilson57147a82010-11-16 00:32:18 +00002080 else {
2081 assert(BitSize == 128 && "Neon vector type not 64 or 128 bits");
Bob Wilsonc7df92d2010-11-12 17:24:43 +00002082 BaseName = "__simd128_";
Bob Wilson57147a82010-11-16 00:32:18 +00002083 }
Bob Wilsonc7df92d2010-11-12 17:24:43 +00002084 Out << strlen(BaseName) + strlen(EltName);
2085 Out << BaseName << EltName;
Bob Wilsonc7df92d2010-11-12 17:24:43 +00002086}
2087
John McCallefe6aee2009-09-05 07:56:18 +00002088// GNU extension: vector types
Chris Lattner788b0fd2010-06-23 06:00:24 +00002089// <type> ::= <vector-type>
2090// <vector-type> ::= Dv <positive dimension number> _
2091// <extended element type>
2092// ::= Dv [<dimension expression>] _ <element type>
2093// <extended element type> ::= <element type>
2094// ::= p # AltiVec vector pixel
John McCallefe6aee2009-09-05 07:56:18 +00002095void CXXNameMangler::mangleType(const VectorType *T) {
Bob Wilson491328c2010-11-12 17:24:46 +00002096 if ((T->getVectorKind() == VectorType::NeonVector ||
Bob Wilson57147a82010-11-16 00:32:18 +00002097 T->getVectorKind() == VectorType::NeonPolyVector)) {
2098 mangleNeonVectorType(T);
Bob Wilsonc7df92d2010-11-12 17:24:43 +00002099 return;
Bob Wilson57147a82010-11-16 00:32:18 +00002100 }
Nick Lewycky0e5f0672010-03-26 07:18:04 +00002101 Out << "Dv" << T->getNumElements() << '_';
Bob Wilsone86d78c2010-11-10 21:56:12 +00002102 if (T->getVectorKind() == VectorType::AltiVecPixel)
Chris Lattner788b0fd2010-06-23 06:00:24 +00002103 Out << 'p';
Bob Wilsone86d78c2010-11-10 21:56:12 +00002104 else if (T->getVectorKind() == VectorType::AltiVecBool)
Chris Lattner788b0fd2010-06-23 06:00:24 +00002105 Out << 'b';
2106 else
2107 mangleType(T->getElementType());
John McCallefe6aee2009-09-05 07:56:18 +00002108}
2109void CXXNameMangler::mangleType(const ExtVectorType *T) {
2110 mangleType(static_cast<const VectorType*>(T));
2111}
2112void CXXNameMangler::mangleType(const DependentSizedExtVectorType *T) {
Nick Lewycky0e5f0672010-03-26 07:18:04 +00002113 Out << "Dv";
2114 mangleExpression(T->getSizeExpr());
2115 Out << '_';
John McCallefe6aee2009-09-05 07:56:18 +00002116 mangleType(T->getElementType());
2117}
2118
Douglas Gregor7536dd52010-12-20 02:24:11 +00002119void CXXNameMangler::mangleType(const PackExpansionType *T) {
Douglas Gregor4fc48662011-01-13 16:39:34 +00002120 // <type> ::= Dp <type> # pack expansion (C++0x)
Douglas Gregor255c2692011-01-13 17:44:36 +00002121 Out << "Dp";
Douglas Gregor7536dd52010-12-20 02:24:11 +00002122 mangleType(T->getPattern());
2123}
2124
Anders Carlssona40c5e42009-03-07 22:03:21 +00002125void CXXNameMangler::mangleType(const ObjCInterfaceType *T) {
2126 mangleSourceName(T->getDecl()->getIdentifier());
2127}
2128
John McCallc12c5bb2010-05-15 11:32:37 +00002129void CXXNameMangler::mangleType(const ObjCObjectType *T) {
John McCallc00c1f62010-05-15 17:06:29 +00002130 // We don't allow overloading by different protocol qualification,
2131 // so mangling them isn't necessary.
John McCallc12c5bb2010-05-15 11:32:37 +00002132 mangleType(T->getBaseType());
2133}
2134
John McCallefe6aee2009-09-05 07:56:18 +00002135void CXXNameMangler::mangleType(const BlockPointerType *T) {
Anders Carlssonf28c6872009-12-23 22:31:44 +00002136 Out << "U13block_pointer";
2137 mangleType(T->getPointeeType());
John McCallefe6aee2009-09-05 07:56:18 +00002138}
2139
John McCall31f17ec2010-04-27 00:57:59 +00002140void CXXNameMangler::mangleType(const InjectedClassNameType *T) {
2141 // Mangle injected class name types as if the user had written the
2142 // specialization out fully. It may not actually be possible to see
2143 // this mangling, though.
2144 mangleType(T->getInjectedSpecializationType());
2145}
2146
John McCallefe6aee2009-09-05 07:56:18 +00002147void CXXNameMangler::mangleType(const TemplateSpecializationType *T) {
Douglas Gregor1e9268e2010-04-28 05:58:56 +00002148 if (TemplateDecl *TD = T->getTemplateName().getAsTemplateDecl()) {
2149 mangleName(TD, T->getArgs(), T->getNumArgs());
2150 } else {
2151 if (mangleSubstitution(QualType(T, 0)))
2152 return;
Sean Huntc3021132010-05-05 15:23:54 +00002153
Douglas Gregor1e9268e2010-04-28 05:58:56 +00002154 mangleTemplatePrefix(T->getTemplateName());
Sean Huntc3021132010-05-05 15:23:54 +00002155
Douglas Gregor1e9268e2010-04-28 05:58:56 +00002156 // FIXME: GCC does not appear to mangle the template arguments when
2157 // the template in question is a dependent template name. Should we
2158 // emulate that badness?
2159 mangleTemplateArgs(T->getTemplateName(), T->getArgs(), T->getNumArgs());
2160 addSubstitution(QualType(T, 0));
2161 }
John McCallefe6aee2009-09-05 07:56:18 +00002162}
2163
Douglas Gregor4714c122010-03-31 17:34:00 +00002164void CXXNameMangler::mangleType(const DependentNameType *T) {
Anders Carlssonae352482009-09-26 02:26:02 +00002165 // Typename types are always nested
2166 Out << 'N';
John McCalla0ce15c2011-04-24 08:23:24 +00002167 manglePrefix(T->getQualifier());
John McCall33500952010-06-11 00:33:02 +00002168 mangleSourceName(T->getIdentifier());
2169 Out << 'E';
2170}
John McCall6ab30e02010-06-09 07:26:17 +00002171
John McCall33500952010-06-11 00:33:02 +00002172void CXXNameMangler::mangleType(const DependentTemplateSpecializationType *T) {
Douglas Gregoraa2187d2011-02-28 00:04:36 +00002173 // Dependently-scoped template types are nested if they have a prefix.
John McCall33500952010-06-11 00:33:02 +00002174 Out << 'N';
2175
2176 // TODO: avoid making this TemplateName.
2177 TemplateName Prefix =
2178 getASTContext().getDependentTemplateName(T->getQualifier(),
2179 T->getIdentifier());
2180 mangleTemplatePrefix(Prefix);
2181
2182 // FIXME: GCC does not appear to mangle the template arguments when
2183 // the template in question is a dependent template name. Should we
2184 // emulate that badness?
2185 mangleTemplateArgs(Prefix, T->getArgs(), T->getNumArgs());
Anders Carlssonae352482009-09-26 02:26:02 +00002186 Out << 'E';
John McCallefe6aee2009-09-05 07:56:18 +00002187}
2188
John McCallad5e7382010-03-01 23:49:17 +00002189void CXXNameMangler::mangleType(const TypeOfType *T) {
2190 // FIXME: this is pretty unsatisfactory, but there isn't an obvious
2191 // "extension with parameters" mangling.
2192 Out << "u6typeof";
2193}
2194
2195void CXXNameMangler::mangleType(const TypeOfExprType *T) {
2196 // FIXME: this is pretty unsatisfactory, but there isn't an obvious
2197 // "extension with parameters" mangling.
2198 Out << "u6typeof";
2199}
2200
2201void CXXNameMangler::mangleType(const DecltypeType *T) {
2202 Expr *E = T->getUnderlyingExpr();
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002203
John McCallad5e7382010-03-01 23:49:17 +00002204 // type ::= Dt <expression> E # decltype of an id-expression
2205 // # or class member access
2206 // ::= DT <expression> E # decltype of an expression
2207
2208 // This purports to be an exhaustive list of id-expressions and
2209 // class member accesses. Note that we do not ignore parentheses;
2210 // parentheses change the semantics of decltype for these
2211 // expressions (and cause the mangler to use the other form).
2212 if (isa<DeclRefExpr>(E) ||
2213 isa<MemberExpr>(E) ||
2214 isa<UnresolvedLookupExpr>(E) ||
2215 isa<DependentScopeDeclRefExpr>(E) ||
2216 isa<CXXDependentScopeMemberExpr>(E) ||
2217 isa<UnresolvedMemberExpr>(E))
2218 Out << "Dt";
2219 else
2220 Out << "DT";
2221 mangleExpression(E);
2222 Out << 'E';
2223}
2224
Sean Huntca63c202011-05-24 22:41:36 +00002225void CXXNameMangler::mangleType(const UnaryTransformType *T) {
2226 // If this is dependent, we need to record that. If not, we simply
2227 // mangle it as the underlying type since they are equivalent.
2228 if (T->isDependentType()) {
2229 Out << 'U';
2230
2231 switch (T->getUTTKind()) {
2232 case UnaryTransformType::EnumUnderlyingType:
2233 Out << "3eut";
2234 break;
2235 }
2236 }
2237
2238 mangleType(T->getUnderlyingType());
2239}
2240
Richard Smith34b41d92011-02-20 03:19:35 +00002241void CXXNameMangler::mangleType(const AutoType *T) {
2242 QualType D = T->getDeducedType();
Richard Smith967ecd32011-02-21 20:10:02 +00002243 // <builtin-type> ::= Da # dependent auto
2244 if (D.isNull())
2245 Out << "Da";
2246 else
2247 mangleType(D);
Richard Smith34b41d92011-02-20 03:19:35 +00002248}
2249
Eli Friedmanb001de72011-10-06 23:00:33 +00002250void CXXNameMangler::mangleType(const AtomicType *T) {
2251 // <type> ::= U <source-name> <type> # vendor extended type qualifier
2252 // (Until there's a standardized mangling...)
2253 Out << "U7_Atomic";
2254 mangleType(T->getValueType());
2255}
2256
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002257void CXXNameMangler::mangleIntegerLiteral(QualType T,
Anders Carlssone170ba72009-12-14 01:45:37 +00002258 const llvm::APSInt &Value) {
2259 // <expr-primary> ::= L <type> <value number> E # integer literal
2260 Out << 'L';
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002261
Anders Carlssone170ba72009-12-14 01:45:37 +00002262 mangleType(T);
2263 if (T->isBooleanType()) {
2264 // Boolean values are encoded as 0/1.
2265 Out << (Value.getBoolValue() ? '1' : '0');
2266 } else {
John McCall0512e482010-07-14 04:20:34 +00002267 mangleNumber(Value);
Anders Carlssone170ba72009-12-14 01:45:37 +00002268 }
2269 Out << 'E';
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002270
Anders Carlssone170ba72009-12-14 01:45:37 +00002271}
2272
John McCall2f27bf82010-02-04 02:56:29 +00002273/// Mangles a member expression. Implicit accesses are not handled,
2274/// but that should be okay, because you shouldn't be able to
2275/// make an implicit access in a function template declaration.
John McCalla0ce15c2011-04-24 08:23:24 +00002276void CXXNameMangler::mangleMemberExpr(const Expr *base,
2277 bool isArrow,
2278 NestedNameSpecifier *qualifier,
2279 NamedDecl *firstQualifierLookup,
2280 DeclarationName member,
2281 unsigned arity) {
2282 // <expression> ::= dt <expression> <unresolved-name>
2283 // ::= pt <expression> <unresolved-name>
2284 Out << (isArrow ? "pt" : "dt");
2285 mangleExpression(base);
2286 mangleUnresolvedName(qualifier, firstQualifierLookup, member, arity);
John McCall2f27bf82010-02-04 02:56:29 +00002287}
2288
John McCall5a7e6f72011-04-28 02:52:03 +00002289/// Look at the callee of the given call expression and determine if
2290/// it's a parenthesized id-expression which would have triggered ADL
2291/// otherwise.
2292static bool isParenthesizedADLCallee(const CallExpr *call) {
2293 const Expr *callee = call->getCallee();
2294 const Expr *fn = callee->IgnoreParens();
2295
2296 // Must be parenthesized. IgnoreParens() skips __extension__ nodes,
2297 // too, but for those to appear in the callee, it would have to be
2298 // parenthesized.
2299 if (callee == fn) return false;
2300
2301 // Must be an unresolved lookup.
2302 const UnresolvedLookupExpr *lookup = dyn_cast<UnresolvedLookupExpr>(fn);
2303 if (!lookup) return false;
2304
2305 assert(!lookup->requiresADL());
2306
2307 // Must be an unqualified lookup.
2308 if (lookup->getQualifier()) return false;
2309
2310 // Must not have found a class member. Note that if one is a class
2311 // member, they're all class members.
2312 if (lookup->getNumDecls() > 0 &&
2313 (*lookup->decls_begin())->isCXXClassMember())
2314 return false;
2315
2316 // Otherwise, ADL would have been triggered.
2317 return true;
2318}
2319
John McCall5e1e89b2010-08-18 19:18:59 +00002320void CXXNameMangler::mangleExpression(const Expr *E, unsigned Arity) {
Anders Carlssond553f8c2009-09-21 01:21:10 +00002321 // <expression> ::= <unary operator-name> <expression>
John McCall09cc1412010-02-03 00:55:45 +00002322 // ::= <binary operator-name> <expression> <expression>
2323 // ::= <trinary operator-name> <expression> <expression> <expression>
Anders Carlssond553f8c2009-09-21 01:21:10 +00002324 // ::= cv <type> expression # conversion with one argument
2325 // ::= cv <type> _ <expression>* E # conversion with a different number of arguments
Eli Friedmana7e68452010-08-22 01:00:03 +00002326 // ::= st <type> # sizeof (a type)
Anders Carlssond553f8c2009-09-21 01:21:10 +00002327 // ::= at <type> # alignof (a type)
2328 // ::= <template-param>
2329 // ::= <function-param>
2330 // ::= sr <type> <unqualified-name> # dependent name
2331 // ::= sr <type> <unqualified-name> <template-args> # dependent template-id
Douglas Gregor63f62df2011-06-05 05:27:58 +00002332 // ::= ds <expression> <expression> # expr.*expr
Anders Carlssond553f8c2009-09-21 01:21:10 +00002333 // ::= sZ <template-param> # size of a parameter pack
Douglas Gregor4fc48662011-01-13 16:39:34 +00002334 // ::= sZ <function-param> # size of a function parameter pack
John McCall09cc1412010-02-03 00:55:45 +00002335 // ::= <expr-primary>
John McCall1dd73832010-02-04 01:42:13 +00002336 // <expr-primary> ::= L <type> <value number> E # integer literal
2337 // ::= L <type <value float> E # floating literal
2338 // ::= L <mangled-name> E # external name
Douglas Gregoredee94b2011-07-12 04:47:20 +00002339 QualType ImplicitlyConvertedToType;
2340
2341recurse:
Anders Carlssond553f8c2009-09-21 01:21:10 +00002342 switch (E->getStmtClass()) {
John McCall6ae1f352010-04-09 22:26:14 +00002343 case Expr::NoStmtClass:
John McCall63c00d72011-02-09 08:16:59 +00002344#define ABSTRACT_STMT(Type)
John McCall6ae1f352010-04-09 22:26:14 +00002345#define EXPR(Type, Base)
2346#define STMT(Type, Base) \
2347 case Expr::Type##Class:
Sean Hunt4bfe1962010-05-05 15:24:00 +00002348#include "clang/AST/StmtNodes.inc"
John McCall0512e482010-07-14 04:20:34 +00002349 // fallthrough
2350
2351 // These all can only appear in local or variable-initialization
2352 // contexts and so should never appear in a mangling.
2353 case Expr::AddrLabelExprClass:
2354 case Expr::BlockDeclRefExprClass:
2355 case Expr::CXXThisExprClass:
2356 case Expr::DesignatedInitExprClass:
2357 case Expr::ImplicitValueInitExprClass:
John McCall0512e482010-07-14 04:20:34 +00002358 case Expr::ParenListExprClass:
Douglas Gregor01d08012012-02-07 10:09:13 +00002359 case Expr::LambdaExprClass:
John McCall09cc1412010-02-03 00:55:45 +00002360 llvm_unreachable("unexpected statement kind");
John McCall09cc1412010-02-03 00:55:45 +00002361
John McCall0512e482010-07-14 04:20:34 +00002362 // FIXME: invent manglings for all these.
2363 case Expr::BlockExprClass:
2364 case Expr::CXXPseudoDestructorExprClass:
2365 case Expr::ChooseExprClass:
2366 case Expr::CompoundLiteralExprClass:
2367 case Expr::ExtVectorElementExprClass:
Peter Collingbournef111d932011-04-15 00:35:48 +00002368 case Expr::GenericSelectionExprClass:
John McCall0512e482010-07-14 04:20:34 +00002369 case Expr::ObjCEncodeExprClass:
John McCall0512e482010-07-14 04:20:34 +00002370 case Expr::ObjCIsaExprClass:
2371 case Expr::ObjCIvarRefExprClass:
2372 case Expr::ObjCMessageExprClass:
2373 case Expr::ObjCPropertyRefExprClass:
2374 case Expr::ObjCProtocolExprClass:
2375 case Expr::ObjCSelectorExprClass:
2376 case Expr::ObjCStringLiteralClass:
Ted Kremenekebcb57a2012-03-06 20:05:56 +00002377 case Expr::ObjCNumericLiteralClass:
2378 case Expr::ObjCArrayLiteralClass:
2379 case Expr::ObjCDictionaryLiteralClass:
2380 case Expr::ObjCSubscriptRefExprClass:
John McCallf85e1932011-06-15 23:02:42 +00002381 case Expr::ObjCIndirectCopyRestoreExprClass:
John McCall0512e482010-07-14 04:20:34 +00002382 case Expr::OffsetOfExprClass:
2383 case Expr::PredefinedExprClass:
2384 case Expr::ShuffleVectorExprClass:
2385 case Expr::StmtExprClass:
John McCall0512e482010-07-14 04:20:34 +00002386 case Expr::UnaryTypeTraitExprClass:
Francois Pichet6ad6f282010-12-07 00:08:36 +00002387 case Expr::BinaryTypeTraitExprClass:
Douglas Gregor4ca8ac22012-02-24 07:38:34 +00002388 case Expr::TypeTraitExprClass:
John Wiegley21ff2e52011-04-28 00:16:57 +00002389 case Expr::ArrayTypeTraitExprClass:
John Wiegley55262202011-04-25 06:54:41 +00002390 case Expr::ExpressionTraitExprClass:
Francois Pichet9be88402010-09-08 23:47:05 +00002391 case Expr::VAArgExprClass:
Sebastian Redl2e156222010-09-10 20:55:43 +00002392 case Expr::CXXUuidofExprClass:
Peter Collingbournee08ce652011-02-09 21:07:24 +00002393 case Expr::CXXNoexceptExprClass:
Tanya Lattner61eee0c2011-06-04 00:47:47 +00002394 case Expr::CUDAKernelCallExprClass:
2395 case Expr::AsTypeExprClass:
John McCall4b9c2d22011-11-06 09:01:30 +00002396 case Expr::PseudoObjectExprClass:
Eli Friedman276b0612011-10-11 02:20:01 +00002397 case Expr::AtomicExprClass:
Tanya Lattner61eee0c2011-06-04 00:47:47 +00002398 {
John McCall6ae1f352010-04-09 22:26:14 +00002399 // As bad as this diagnostic is, it's better than crashing.
David Blaikied6471f72011-09-25 23:23:43 +00002400 DiagnosticsEngine &Diags = Context.getDiags();
2401 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
John McCall6ae1f352010-04-09 22:26:14 +00002402 "cannot yet mangle expression type %0");
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +00002403 Diags.Report(E->getExprLoc(), DiagID)
John McCall739bf092010-04-10 09:39:25 +00002404 << E->getStmtClassName() << E->getSourceRange();
John McCall6ae1f352010-04-09 22:26:14 +00002405 break;
2406 }
2407
John McCall56ca35d2011-02-17 10:25:35 +00002408 // Even gcc-4.5 doesn't mangle this.
2409 case Expr::BinaryConditionalOperatorClass: {
David Blaikied6471f72011-09-25 23:23:43 +00002410 DiagnosticsEngine &Diags = Context.getDiags();
John McCall56ca35d2011-02-17 10:25:35 +00002411 unsigned DiagID =
David Blaikied6471f72011-09-25 23:23:43 +00002412 Diags.getCustomDiagID(DiagnosticsEngine::Error,
John McCall56ca35d2011-02-17 10:25:35 +00002413 "?: operator with omitted middle operand cannot be mangled");
2414 Diags.Report(E->getExprLoc(), DiagID)
2415 << E->getStmtClassName() << E->getSourceRange();
2416 break;
2417 }
2418
2419 // These are used for internal purposes and cannot be meaningfully mangled.
John McCall7cd7d1a2010-11-15 23:31:06 +00002420 case Expr::OpaqueValueExprClass:
2421 llvm_unreachable("cannot mangle opaque value; mangling wrong thing?");
2422
Sebastian Redlfaf4ef62012-02-25 22:59:28 +00002423 case Expr::InitListExprClass: {
2424 // Proposal by Jason Merrill, 2012-01-03
2425 Out << "il";
2426 const InitListExpr *InitList = cast<InitListExpr>(E);
2427 for (unsigned i = 0, e = InitList->getNumInits(); i != e; ++i)
2428 mangleExpression(InitList->getInit(i));
2429 Out << "E";
2430 break;
2431 }
2432
John McCall0512e482010-07-14 04:20:34 +00002433 case Expr::CXXDefaultArgExprClass:
John McCall5e1e89b2010-08-18 19:18:59 +00002434 mangleExpression(cast<CXXDefaultArgExpr>(E)->getExpr(), Arity);
John McCall0512e482010-07-14 04:20:34 +00002435 break;
2436
John McCall91a57552011-07-15 05:09:51 +00002437 case Expr::SubstNonTypeTemplateParmExprClass:
2438 mangleExpression(cast<SubstNonTypeTemplateParmExpr>(E)->getReplacement(),
2439 Arity);
2440 break;
2441
Richard Smith9fcce652012-03-07 08:35:16 +00002442 case Expr::UserDefinedLiteralClass:
2443 // We follow g++'s approach of mangling a UDL as a call to the literal
2444 // operator.
John McCall0512e482010-07-14 04:20:34 +00002445 case Expr::CXXMemberCallExprClass: // fallthrough
John McCall1dd73832010-02-04 01:42:13 +00002446 case Expr::CallExprClass: {
2447 const CallExpr *CE = cast<CallExpr>(E);
John McCall5a7e6f72011-04-28 02:52:03 +00002448
2449 // <expression> ::= cp <simple-id> <expression>* E
2450 // We use this mangling only when the call would use ADL except
2451 // for being parenthesized. Per discussion with David
2452 // Vandervoorde, 2011.04.25.
2453 if (isParenthesizedADLCallee(CE)) {
2454 Out << "cp";
2455 // The callee here is a parenthesized UnresolvedLookupExpr with
2456 // no qualifier and should always get mangled as a <simple-id>
2457 // anyway.
2458
2459 // <expression> ::= cl <expression>* E
2460 } else {
2461 Out << "cl";
2462 }
2463
John McCall5e1e89b2010-08-18 19:18:59 +00002464 mangleExpression(CE->getCallee(), CE->getNumArgs());
John McCall1dd73832010-02-04 01:42:13 +00002465 for (unsigned I = 0, N = CE->getNumArgs(); I != N; ++I)
2466 mangleExpression(CE->getArg(I));
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002467 Out << 'E';
John McCall09cc1412010-02-03 00:55:45 +00002468 break;
John McCall1dd73832010-02-04 01:42:13 +00002469 }
John McCall09cc1412010-02-03 00:55:45 +00002470
John McCall0512e482010-07-14 04:20:34 +00002471 case Expr::CXXNewExprClass: {
John McCall0512e482010-07-14 04:20:34 +00002472 const CXXNewExpr *New = cast<CXXNewExpr>(E);
2473 if (New->isGlobalNew()) Out << "gs";
2474 Out << (New->isArray() ? "na" : "nw");
2475 for (CXXNewExpr::const_arg_iterator I = New->placement_arg_begin(),
2476 E = New->placement_arg_end(); I != E; ++I)
2477 mangleExpression(*I);
2478 Out << '_';
2479 mangleType(New->getAllocatedType());
2480 if (New->hasInitializer()) {
Sebastian Redlfaf4ef62012-02-25 22:59:28 +00002481 // Proposal by Jason Merrill, 2012-01-03
Sebastian Redlb76ffc52012-02-25 20:51:07 +00002482 if (New->getInitializationStyle() == CXXNewExpr::ListInit)
Sebastian Redlfaf4ef62012-02-25 22:59:28 +00002483 Out << "il";
Sebastian Redlb76ffc52012-02-25 20:51:07 +00002484 else
2485 Out << "pi";
Sebastian Redl2aed8b82012-02-16 12:22:20 +00002486 const Expr *Init = New->getInitializer();
2487 if (const CXXConstructExpr *CCE = dyn_cast<CXXConstructExpr>(Init)) {
2488 // Directly inline the initializers.
2489 for (CXXConstructExpr::const_arg_iterator I = CCE->arg_begin(),
2490 E = CCE->arg_end();
2491 I != E; ++I)
2492 mangleExpression(*I);
2493 } else if (const ParenListExpr *PLE = dyn_cast<ParenListExpr>(Init)) {
2494 for (unsigned i = 0, e = PLE->getNumExprs(); i != e; ++i)
2495 mangleExpression(PLE->getExpr(i));
Sebastian Redlb76ffc52012-02-25 20:51:07 +00002496 } else if (New->getInitializationStyle() == CXXNewExpr::ListInit &&
2497 isa<InitListExpr>(Init)) {
Sebastian Redlfaf4ef62012-02-25 22:59:28 +00002498 // Only take InitListExprs apart for list-initialization.
Sebastian Redlb76ffc52012-02-25 20:51:07 +00002499 const InitListExpr *InitList = cast<InitListExpr>(Init);
2500 for (unsigned i = 0, e = InitList->getNumInits(); i != e; ++i)
2501 mangleExpression(InitList->getInit(i));
Sebastian Redl2aed8b82012-02-16 12:22:20 +00002502 } else
2503 mangleExpression(Init);
John McCall0512e482010-07-14 04:20:34 +00002504 }
2505 Out << 'E';
2506 break;
2507 }
2508
John McCall2f27bf82010-02-04 02:56:29 +00002509 case Expr::MemberExprClass: {
2510 const MemberExpr *ME = cast<MemberExpr>(E);
2511 mangleMemberExpr(ME->getBase(), ME->isArrow(),
John McCalla0ce15c2011-04-24 08:23:24 +00002512 ME->getQualifier(), 0, ME->getMemberDecl()->getDeclName(),
John McCall5e1e89b2010-08-18 19:18:59 +00002513 Arity);
John McCall2f27bf82010-02-04 02:56:29 +00002514 break;
2515 }
2516
2517 case Expr::UnresolvedMemberExprClass: {
2518 const UnresolvedMemberExpr *ME = cast<UnresolvedMemberExpr>(E);
2519 mangleMemberExpr(ME->getBase(), ME->isArrow(),
John McCalla0ce15c2011-04-24 08:23:24 +00002520 ME->getQualifier(), 0, ME->getMemberName(),
John McCall5e1e89b2010-08-18 19:18:59 +00002521 Arity);
John McCall6dbce192010-08-20 00:17:19 +00002522 if (ME->hasExplicitTemplateArgs())
2523 mangleTemplateArgs(ME->getExplicitTemplateArgs());
John McCall2f27bf82010-02-04 02:56:29 +00002524 break;
2525 }
2526
2527 case Expr::CXXDependentScopeMemberExprClass: {
2528 const CXXDependentScopeMemberExpr *ME
2529 = cast<CXXDependentScopeMemberExpr>(E);
2530 mangleMemberExpr(ME->getBase(), ME->isArrow(),
John McCalla0ce15c2011-04-24 08:23:24 +00002531 ME->getQualifier(), ME->getFirstQualifierFoundInScope(),
2532 ME->getMember(), Arity);
John McCall6dbce192010-08-20 00:17:19 +00002533 if (ME->hasExplicitTemplateArgs())
2534 mangleTemplateArgs(ME->getExplicitTemplateArgs());
John McCall2f27bf82010-02-04 02:56:29 +00002535 break;
2536 }
2537
John McCall1dd73832010-02-04 01:42:13 +00002538 case Expr::UnresolvedLookupExprClass: {
2539 const UnresolvedLookupExpr *ULE = cast<UnresolvedLookupExpr>(E);
John McCalla0ce15c2011-04-24 08:23:24 +00002540 mangleUnresolvedName(ULE->getQualifier(), 0, ULE->getName(), Arity);
John McCall26a6ec72011-06-21 22:12:46 +00002541
2542 // All the <unresolved-name> productions end in a
2543 // base-unresolved-name, where <template-args> are just tacked
2544 // onto the end.
John McCall6dbce192010-08-20 00:17:19 +00002545 if (ULE->hasExplicitTemplateArgs())
2546 mangleTemplateArgs(ULE->getExplicitTemplateArgs());
John McCall1dd73832010-02-04 01:42:13 +00002547 break;
2548 }
2549
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002550 case Expr::CXXUnresolvedConstructExprClass: {
John McCall1dd73832010-02-04 01:42:13 +00002551 const CXXUnresolvedConstructExpr *CE = cast<CXXUnresolvedConstructExpr>(E);
2552 unsigned N = CE->arg_size();
2553
2554 Out << "cv";
2555 mangleType(CE->getType());
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002556 if (N != 1) Out << '_';
John McCall1dd73832010-02-04 01:42:13 +00002557 for (unsigned I = 0; I != N; ++I) mangleExpression(CE->getArg(I));
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002558 if (N != 1) Out << 'E';
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002559 break;
John McCall1dd73832010-02-04 01:42:13 +00002560 }
John McCall09cc1412010-02-03 00:55:45 +00002561
John McCall1dd73832010-02-04 01:42:13 +00002562 case Expr::CXXTemporaryObjectExprClass:
2563 case Expr::CXXConstructExprClass: {
2564 const CXXConstructExpr *CE = cast<CXXConstructExpr>(E);
2565 unsigned N = CE->getNumArgs();
2566
Sebastian Redlfaf4ef62012-02-25 22:59:28 +00002567 // Proposal by Jason Merrill, 2012-01-03
2568 if (CE->isListInitialization())
2569 Out << "tl";
2570 else
2571 Out << "cv";
John McCall1dd73832010-02-04 01:42:13 +00002572 mangleType(CE->getType());
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002573 if (N != 1) Out << '_';
John McCall1dd73832010-02-04 01:42:13 +00002574 for (unsigned I = 0; I != N; ++I) mangleExpression(CE->getArg(I));
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002575 if (N != 1) Out << 'E';
John McCall09cc1412010-02-03 00:55:45 +00002576 break;
John McCall1dd73832010-02-04 01:42:13 +00002577 }
2578
Richard Smith41576d42012-02-06 02:54:51 +00002579 case Expr::CXXScalarValueInitExprClass:
2580 Out <<"cv";
2581 mangleType(E->getType());
2582 Out <<"_E";
2583 break;
2584
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +00002585 case Expr::UnaryExprOrTypeTraitExprClass: {
2586 const UnaryExprOrTypeTraitExpr *SAE = cast<UnaryExprOrTypeTraitExpr>(E);
Douglas Gregoredee94b2011-07-12 04:47:20 +00002587
2588 if (!SAE->isInstantiationDependent()) {
2589 // Itanium C++ ABI:
2590 // If the operand of a sizeof or alignof operator is not
2591 // instantiation-dependent it is encoded as an integer literal
2592 // reflecting the result of the operator.
2593 //
2594 // If the result of the operator is implicitly converted to a known
2595 // integer type, that type is used for the literal; otherwise, the type
2596 // of std::size_t or std::ptrdiff_t is used.
2597 QualType T = (ImplicitlyConvertedToType.isNull() ||
2598 !ImplicitlyConvertedToType->isIntegerType())? SAE->getType()
2599 : ImplicitlyConvertedToType;
Richard Smitha6b8b2c2011-10-10 18:28:20 +00002600 llvm::APSInt V = SAE->EvaluateKnownConstInt(Context.getASTContext());
2601 mangleIntegerLiteral(T, V);
Douglas Gregoredee94b2011-07-12 04:47:20 +00002602 break;
2603 }
2604
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +00002605 switch(SAE->getKind()) {
2606 case UETT_SizeOf:
2607 Out << 's';
2608 break;
2609 case UETT_AlignOf:
2610 Out << 'a';
2611 break;
2612 case UETT_VecStep:
David Blaikied6471f72011-09-25 23:23:43 +00002613 DiagnosticsEngine &Diags = Context.getDiags();
2614 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +00002615 "cannot yet mangle vec_step expression");
2616 Diags.Report(DiagID);
2617 return;
2618 }
John McCall1dd73832010-02-04 01:42:13 +00002619 if (SAE->isArgumentType()) {
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002620 Out << 't';
John McCall1dd73832010-02-04 01:42:13 +00002621 mangleType(SAE->getArgumentType());
2622 } else {
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002623 Out << 'z';
John McCall1dd73832010-02-04 01:42:13 +00002624 mangleExpression(SAE->getArgumentExpr());
2625 }
2626 break;
2627 }
Anders Carlssona7694082009-11-06 02:50:19 +00002628
John McCall0512e482010-07-14 04:20:34 +00002629 case Expr::CXXThrowExprClass: {
2630 const CXXThrowExpr *TE = cast<CXXThrowExpr>(E);
2631
2632 // Proposal from David Vandervoorde, 2010.06.30
2633 if (TE->getSubExpr()) {
2634 Out << "tw";
2635 mangleExpression(TE->getSubExpr());
2636 } else {
2637 Out << "tr";
2638 }
2639 break;
2640 }
2641
2642 case Expr::CXXTypeidExprClass: {
2643 const CXXTypeidExpr *TIE = cast<CXXTypeidExpr>(E);
2644
2645 // Proposal from David Vandervoorde, 2010.06.30
2646 if (TIE->isTypeOperand()) {
2647 Out << "ti";
2648 mangleType(TIE->getTypeOperand());
2649 } else {
2650 Out << "te";
2651 mangleExpression(TIE->getExprOperand());
2652 }
2653 break;
2654 }
2655
2656 case Expr::CXXDeleteExprClass: {
2657 const CXXDeleteExpr *DE = cast<CXXDeleteExpr>(E);
2658
2659 // Proposal from David Vandervoorde, 2010.06.30
2660 if (DE->isGlobalDelete()) Out << "gs";
2661 Out << (DE->isArrayForm() ? "da" : "dl");
2662 mangleExpression(DE->getArgument());
2663 break;
2664 }
2665
Anders Carlssone170ba72009-12-14 01:45:37 +00002666 case Expr::UnaryOperatorClass: {
2667 const UnaryOperator *UO = cast<UnaryOperator>(E);
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002668 mangleOperatorName(UnaryOperator::getOverloadedOperator(UO->getOpcode()),
Anders Carlssone170ba72009-12-14 01:45:37 +00002669 /*Arity=*/1);
2670 mangleExpression(UO->getSubExpr());
2671 break;
2672 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002673
John McCall0512e482010-07-14 04:20:34 +00002674 case Expr::ArraySubscriptExprClass: {
2675 const ArraySubscriptExpr *AE = cast<ArraySubscriptExpr>(E);
2676
Chris Lattnerfc8f0e12011-04-15 05:22:18 +00002677 // Array subscript is treated as a syntactically weird form of
John McCall0512e482010-07-14 04:20:34 +00002678 // binary operator.
2679 Out << "ix";
2680 mangleExpression(AE->getLHS());
2681 mangleExpression(AE->getRHS());
2682 break;
2683 }
2684
2685 case Expr::CompoundAssignOperatorClass: // fallthrough
Anders Carlssone170ba72009-12-14 01:45:37 +00002686 case Expr::BinaryOperatorClass: {
2687 const BinaryOperator *BO = cast<BinaryOperator>(E);
Douglas Gregor63f62df2011-06-05 05:27:58 +00002688 if (BO->getOpcode() == BO_PtrMemD)
2689 Out << "ds";
2690 else
2691 mangleOperatorName(BinaryOperator::getOverloadedOperator(BO->getOpcode()),
2692 /*Arity=*/2);
Anders Carlssone170ba72009-12-14 01:45:37 +00002693 mangleExpression(BO->getLHS());
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002694 mangleExpression(BO->getRHS());
Anders Carlssone170ba72009-12-14 01:45:37 +00002695 break;
John McCall2f27bf82010-02-04 02:56:29 +00002696 }
Anders Carlssone170ba72009-12-14 01:45:37 +00002697
2698 case Expr::ConditionalOperatorClass: {
2699 const ConditionalOperator *CO = cast<ConditionalOperator>(E);
2700 mangleOperatorName(OO_Conditional, /*Arity=*/3);
2701 mangleExpression(CO->getCond());
John McCall5e1e89b2010-08-18 19:18:59 +00002702 mangleExpression(CO->getLHS(), Arity);
2703 mangleExpression(CO->getRHS(), Arity);
Anders Carlssone170ba72009-12-14 01:45:37 +00002704 break;
2705 }
2706
Douglas Gregor46287c72010-01-29 16:37:09 +00002707 case Expr::ImplicitCastExprClass: {
Douglas Gregoredee94b2011-07-12 04:47:20 +00002708 ImplicitlyConvertedToType = E->getType();
2709 E = cast<ImplicitCastExpr>(E)->getSubExpr();
2710 goto recurse;
Douglas Gregor46287c72010-01-29 16:37:09 +00002711 }
John McCallf85e1932011-06-15 23:02:42 +00002712
2713 case Expr::ObjCBridgedCastExprClass: {
2714 // Mangle ownership casts as a vendor extended operator __bridge,
2715 // __bridge_transfer, or __bridge_retain.
Chris Lattner5f9e2722011-07-23 10:55:15 +00002716 StringRef Kind = cast<ObjCBridgedCastExpr>(E)->getBridgeKindName();
John McCallf85e1932011-06-15 23:02:42 +00002717 Out << "v1U" << Kind.size() << Kind;
2718 }
2719 // Fall through to mangle the cast itself.
2720
Douglas Gregor46287c72010-01-29 16:37:09 +00002721 case Expr::CStyleCastExprClass:
2722 case Expr::CXXStaticCastExprClass:
2723 case Expr::CXXDynamicCastExprClass:
2724 case Expr::CXXReinterpretCastExprClass:
2725 case Expr::CXXConstCastExprClass:
2726 case Expr::CXXFunctionalCastExprClass: {
2727 const ExplicitCastExpr *ECE = cast<ExplicitCastExpr>(E);
2728 Out << "cv";
2729 mangleType(ECE->getType());
2730 mangleExpression(ECE->getSubExpr());
2731 break;
2732 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002733
Anders Carlsson58040a52009-12-16 05:48:46 +00002734 case Expr::CXXOperatorCallExprClass: {
2735 const CXXOperatorCallExpr *CE = cast<CXXOperatorCallExpr>(E);
2736 unsigned NumArgs = CE->getNumArgs();
2737 mangleOperatorName(CE->getOperator(), /*Arity=*/NumArgs);
2738 // Mangle the arguments.
2739 for (unsigned i = 0; i != NumArgs; ++i)
2740 mangleExpression(CE->getArg(i));
2741 break;
2742 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002743
Anders Carlssona7694082009-11-06 02:50:19 +00002744 case Expr::ParenExprClass:
John McCall5e1e89b2010-08-18 19:18:59 +00002745 mangleExpression(cast<ParenExpr>(E)->getSubExpr(), Arity);
Anders Carlssona7694082009-11-06 02:50:19 +00002746 break;
2747
Anders Carlssond553f8c2009-09-21 01:21:10 +00002748 case Expr::DeclRefExprClass: {
Douglas Gregor5ed1bc32010-02-28 21:40:32 +00002749 const NamedDecl *D = cast<DeclRefExpr>(E)->getDecl();
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002750
Anders Carlssond553f8c2009-09-21 01:21:10 +00002751 switch (D->getKind()) {
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002752 default:
Douglas Gregor5ed1bc32010-02-28 21:40:32 +00002753 // <expr-primary> ::= L <mangled-name> E # external name
2754 Out << 'L';
2755 mangle(D, "_Z");
2756 Out << 'E';
2757 break;
2758
John McCallfb44de92011-05-01 22:35:37 +00002759 case Decl::ParmVar:
2760 mangleFunctionParam(cast<ParmVarDecl>(D));
2761 break;
2762
John McCall3dc7e7b2010-07-24 01:17:35 +00002763 case Decl::EnumConstant: {
2764 const EnumConstantDecl *ED = cast<EnumConstantDecl>(D);
2765 mangleIntegerLiteral(ED->getType(), ED->getInitVal());
2766 break;
2767 }
2768
Anders Carlssond553f8c2009-09-21 01:21:10 +00002769 case Decl::NonTypeTemplateParm: {
2770 const NonTypeTemplateParmDecl *PD = cast<NonTypeTemplateParmDecl>(D);
Anders Carlsson0ccdf8d2009-09-27 00:38:53 +00002771 mangleTemplateParameter(PD->getIndex());
Anders Carlssond553f8c2009-09-21 01:21:10 +00002772 break;
2773 }
2774
2775 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002776
Anders Carlsson50755b02009-09-27 20:11:34 +00002777 break;
Anders Carlssond553f8c2009-09-21 01:21:10 +00002778 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002779
Douglas Gregorc7793c72011-01-15 01:15:58 +00002780 case Expr::SubstNonTypeTemplateParmPackExprClass:
John McCall68a51a72011-07-01 00:04:39 +00002781 // FIXME: not clear how to mangle this!
2782 // template <unsigned N...> class A {
2783 // template <class U...> void foo(U (&x)[N]...);
2784 // };
2785 Out << "_SUBSTPACK_";
Douglas Gregorc7793c72011-01-15 01:15:58 +00002786 break;
2787
John McCall865d4472009-11-19 22:55:06 +00002788 case Expr::DependentScopeDeclRefExprClass: {
2789 const DependentScopeDeclRefExpr *DRE = cast<DependentScopeDeclRefExpr>(E);
John McCall26a6ec72011-06-21 22:12:46 +00002790 mangleUnresolvedName(DRE->getQualifier(), 0, DRE->getDeclName(), Arity);
Douglas Gregor4b2ccfc2010-02-28 22:05:49 +00002791
John McCall26a6ec72011-06-21 22:12:46 +00002792 // All the <unresolved-name> productions end in a
2793 // base-unresolved-name, where <template-args> are just tacked
2794 // onto the end.
John McCall6dbce192010-08-20 00:17:19 +00002795 if (DRE->hasExplicitTemplateArgs())
2796 mangleTemplateArgs(DRE->getExplicitTemplateArgs());
Anders Carlsson50755b02009-09-27 20:11:34 +00002797 break;
2798 }
2799
John McCalld9307602010-04-09 22:54:09 +00002800 case Expr::CXXBindTemporaryExprClass:
2801 mangleExpression(cast<CXXBindTemporaryExpr>(E)->getSubExpr());
2802 break;
2803
John McCall4765fa02010-12-06 08:20:24 +00002804 case Expr::ExprWithCleanupsClass:
2805 mangleExpression(cast<ExprWithCleanups>(E)->getSubExpr(), Arity);
John McCalld9307602010-04-09 22:54:09 +00002806 break;
2807
John McCall1dd73832010-02-04 01:42:13 +00002808 case Expr::FloatingLiteralClass: {
2809 const FloatingLiteral *FL = cast<FloatingLiteral>(E);
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002810 Out << 'L';
John McCall1dd73832010-02-04 01:42:13 +00002811 mangleType(FL->getType());
John McCall0512e482010-07-14 04:20:34 +00002812 mangleFloat(FL->getValue());
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002813 Out << 'E';
John McCall1dd73832010-02-04 01:42:13 +00002814 break;
2815 }
2816
John McCallde810632010-04-09 21:48:08 +00002817 case Expr::CharacterLiteralClass:
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002818 Out << 'L';
John McCallde810632010-04-09 21:48:08 +00002819 mangleType(E->getType());
2820 Out << cast<CharacterLiteral>(E)->getValue();
2821 Out << 'E';
2822 break;
2823
Ted Kremenekebcb57a2012-03-06 20:05:56 +00002824 // FIXME. __objc_yes/__objc_no are mangled same as true/false
2825 case Expr::ObjCBoolLiteralExprClass:
2826 Out << "Lb";
2827 Out << (cast<ObjCBoolLiteralExpr>(E)->getValue() ? '1' : '0');
2828 Out << 'E';
2829 break;
2830
John McCallde810632010-04-09 21:48:08 +00002831 case Expr::CXXBoolLiteralExprClass:
2832 Out << "Lb";
2833 Out << (cast<CXXBoolLiteralExpr>(E)->getValue() ? '1' : '0');
2834 Out << 'E';
2835 break;
2836
John McCall0512e482010-07-14 04:20:34 +00002837 case Expr::IntegerLiteralClass: {
2838 llvm::APSInt Value(cast<IntegerLiteral>(E)->getValue());
2839 if (E->getType()->isSignedIntegerType())
2840 Value.setIsSigned(true);
2841 mangleIntegerLiteral(E->getType(), Value);
Anders Carlssone170ba72009-12-14 01:45:37 +00002842 break;
John McCall0512e482010-07-14 04:20:34 +00002843 }
2844
2845 case Expr::ImaginaryLiteralClass: {
2846 const ImaginaryLiteral *IE = cast<ImaginaryLiteral>(E);
2847 // Mangle as if a complex literal.
Nick Lewycky271b6652010-09-05 03:40:33 +00002848 // Proposal from David Vandevoorde, 2010.06.30.
John McCall0512e482010-07-14 04:20:34 +00002849 Out << 'L';
2850 mangleType(E->getType());
2851 if (const FloatingLiteral *Imag =
2852 dyn_cast<FloatingLiteral>(IE->getSubExpr())) {
2853 // Mangle a floating-point zero of the appropriate type.
2854 mangleFloat(llvm::APFloat(Imag->getValue().getSemantics()));
2855 Out << '_';
2856 mangleFloat(Imag->getValue());
2857 } else {
Nick Lewycky271b6652010-09-05 03:40:33 +00002858 Out << "0_";
John McCall0512e482010-07-14 04:20:34 +00002859 llvm::APSInt Value(cast<IntegerLiteral>(IE->getSubExpr())->getValue());
2860 if (IE->getSubExpr()->getType()->isSignedIntegerType())
2861 Value.setIsSigned(true);
2862 mangleNumber(Value);
2863 }
2864 Out << 'E';
2865 break;
2866 }
2867
2868 case Expr::StringLiteralClass: {
John McCall1658c392010-07-15 21:53:03 +00002869 // Revised proposal from David Vandervoorde, 2010.07.15.
John McCall0512e482010-07-14 04:20:34 +00002870 Out << 'L';
John McCall1658c392010-07-15 21:53:03 +00002871 assert(isa<ConstantArrayType>(E->getType()));
2872 mangleType(E->getType());
John McCall0512e482010-07-14 04:20:34 +00002873 Out << 'E';
2874 break;
2875 }
2876
2877 case Expr::GNUNullExprClass:
2878 // FIXME: should this really be mangled the same as nullptr?
2879 // fallthrough
2880
2881 case Expr::CXXNullPtrLiteralExprClass: {
2882 // Proposal from David Vandervoorde, 2010.06.30, as
2883 // modified by ABI list discussion.
2884 Out << "LDnE";
2885 break;
2886 }
Douglas Gregorbe230c32011-01-03 17:17:50 +00002887
2888 case Expr::PackExpansionExprClass:
2889 Out << "sp";
2890 mangleExpression(cast<PackExpansionExpr>(E)->getPattern());
2891 break;
Douglas Gregor2e774c42011-01-04 18:56:13 +00002892
2893 case Expr::SizeOfPackExprClass: {
Douglas Gregor2e774c42011-01-04 18:56:13 +00002894 Out << "sZ";
2895 const NamedDecl *Pack = cast<SizeOfPackExpr>(E)->getPack();
2896 if (const TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(Pack))
2897 mangleTemplateParameter(TTP->getIndex());
2898 else if (const NonTypeTemplateParmDecl *NTTP
2899 = dyn_cast<NonTypeTemplateParmDecl>(Pack))
2900 mangleTemplateParameter(NTTP->getIndex());
2901 else if (const TemplateTemplateParmDecl *TempTP
2902 = dyn_cast<TemplateTemplateParmDecl>(Pack))
2903 mangleTemplateParameter(TempTP->getIndex());
Douglas Gregor91832362011-07-12 07:03:48 +00002904 else
2905 mangleFunctionParam(cast<ParmVarDecl>(Pack));
Douglas Gregordfbbcf92011-03-03 02:20:19 +00002906 break;
Douglas Gregor2e774c42011-01-04 18:56:13 +00002907 }
Douglas Gregor03e80032011-06-21 17:03:29 +00002908
2909 case Expr::MaterializeTemporaryExprClass: {
2910 mangleExpression(cast<MaterializeTemporaryExpr>(E)->GetTemporaryExpr());
2911 break;
2912 }
Anders Carlssond553f8c2009-09-21 01:21:10 +00002913 }
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00002914}
2915
John McCallfb44de92011-05-01 22:35:37 +00002916/// Mangle an expression which refers to a parameter variable.
2917///
2918/// <expression> ::= <function-param>
2919/// <function-param> ::= fp <top-level CV-qualifiers> _ # L == 0, I == 0
2920/// <function-param> ::= fp <top-level CV-qualifiers>
2921/// <parameter-2 non-negative number> _ # L == 0, I > 0
2922/// <function-param> ::= fL <L-1 non-negative number>
2923/// p <top-level CV-qualifiers> _ # L > 0, I == 0
2924/// <function-param> ::= fL <L-1 non-negative number>
2925/// p <top-level CV-qualifiers>
2926/// <I-1 non-negative number> _ # L > 0, I > 0
2927///
2928/// L is the nesting depth of the parameter, defined as 1 if the
2929/// parameter comes from the innermost function prototype scope
2930/// enclosing the current context, 2 if from the next enclosing
2931/// function prototype scope, and so on, with one special case: if
2932/// we've processed the full parameter clause for the innermost
2933/// function type, then L is one less. This definition conveniently
2934/// makes it irrelevant whether a function's result type was written
2935/// trailing or leading, but is otherwise overly complicated; the
2936/// numbering was first designed without considering references to
2937/// parameter in locations other than return types, and then the
2938/// mangling had to be generalized without changing the existing
2939/// manglings.
2940///
2941/// I is the zero-based index of the parameter within its parameter
2942/// declaration clause. Note that the original ABI document describes
2943/// this using 1-based ordinals.
2944void CXXNameMangler::mangleFunctionParam(const ParmVarDecl *parm) {
2945 unsigned parmDepth = parm->getFunctionScopeDepth();
2946 unsigned parmIndex = parm->getFunctionScopeIndex();
2947
2948 // Compute 'L'.
2949 // parmDepth does not include the declaring function prototype.
2950 // FunctionTypeDepth does account for that.
2951 assert(parmDepth < FunctionTypeDepth.getDepth());
2952 unsigned nestingDepth = FunctionTypeDepth.getDepth() - parmDepth;
2953 if (FunctionTypeDepth.isInResultType())
2954 nestingDepth--;
2955
2956 if (nestingDepth == 0) {
2957 Out << "fp";
2958 } else {
2959 Out << "fL" << (nestingDepth - 1) << 'p';
2960 }
2961
2962 // Top-level qualifiers. We don't have to worry about arrays here,
2963 // because parameters declared as arrays should already have been
2964 // tranformed to have pointer type. FIXME: apparently these don't
2965 // get mangled if used as an rvalue of a known non-class type?
2966 assert(!parm->getType()->isArrayType()
2967 && "parameter's type is still an array type?");
2968 mangleQualifiers(parm->getType().getQualifiers());
2969
2970 // Parameter index.
2971 if (parmIndex != 0) {
2972 Out << (parmIndex - 1);
2973 }
2974 Out << '_';
2975}
2976
Anders Carlsson3ac86b52009-04-15 05:36:58 +00002977void CXXNameMangler::mangleCXXCtorType(CXXCtorType T) {
2978 // <ctor-dtor-name> ::= C1 # complete object constructor
2979 // ::= C2 # base object constructor
2980 // ::= C3 # complete object allocating constructor
2981 //
2982 switch (T) {
2983 case Ctor_Complete:
2984 Out << "C1";
2985 break;
2986 case Ctor_Base:
2987 Out << "C2";
2988 break;
2989 case Ctor_CompleteAllocating:
2990 Out << "C3";
2991 break;
2992 }
2993}
2994
Anders Carlsson27ae5362009-04-17 01:58:57 +00002995void CXXNameMangler::mangleCXXDtorType(CXXDtorType T) {
2996 // <ctor-dtor-name> ::= D0 # deleting destructor
2997 // ::= D1 # complete object destructor
2998 // ::= D2 # base object destructor
2999 //
3000 switch (T) {
3001 case Dtor_Deleting:
3002 Out << "D0";
3003 break;
3004 case Dtor_Complete:
3005 Out << "D1";
3006 break;
3007 case Dtor_Base:
3008 Out << "D2";
3009 break;
3010 }
3011}
3012
John McCall6dbce192010-08-20 00:17:19 +00003013void CXXNameMangler::mangleTemplateArgs(
Argyrios Kyrtzidisb0c3e092011-09-22 20:07:03 +00003014 const ASTTemplateArgumentListInfo &TemplateArgs) {
John McCall6dbce192010-08-20 00:17:19 +00003015 // <template-args> ::= I <template-arg>+ E
3016 Out << 'I';
John McCall4f4e4132011-05-04 01:45:19 +00003017 for (unsigned i = 0, e = TemplateArgs.NumTemplateArgs; i != e; ++i)
3018 mangleTemplateArg(0, TemplateArgs.getTemplateArgs()[i].getArgument());
John McCall6dbce192010-08-20 00:17:19 +00003019 Out << 'E';
3020}
3021
Douglas Gregor20f0cc72010-04-23 03:10:43 +00003022void CXXNameMangler::mangleTemplateArgs(TemplateName Template,
3023 const TemplateArgument *TemplateArgs,
3024 unsigned NumTemplateArgs) {
3025 if (TemplateDecl *TD = Template.getAsTemplateDecl())
3026 return mangleTemplateArgs(*TD->getTemplateParameters(), TemplateArgs,
3027 NumTemplateArgs);
Sean Huntc3021132010-05-05 15:23:54 +00003028
John McCall4f4e4132011-05-04 01:45:19 +00003029 mangleUnresolvedTemplateArgs(TemplateArgs, NumTemplateArgs);
3030}
3031
3032void CXXNameMangler::mangleUnresolvedTemplateArgs(const TemplateArgument *args,
3033 unsigned numArgs) {
Douglas Gregor20f0cc72010-04-23 03:10:43 +00003034 // <template-args> ::= I <template-arg>+ E
3035 Out << 'I';
John McCall4f4e4132011-05-04 01:45:19 +00003036 for (unsigned i = 0; i != numArgs; ++i)
3037 mangleTemplateArg(0, args[i]);
Douglas Gregor20f0cc72010-04-23 03:10:43 +00003038 Out << 'E';
3039}
3040
Rafael Espindolad9800722010-03-11 14:07:00 +00003041void CXXNameMangler::mangleTemplateArgs(const TemplateParameterList &PL,
3042 const TemplateArgumentList &AL) {
Anders Carlsson7a0ba872009-05-15 16:09:15 +00003043 // <template-args> ::= I <template-arg>+ E
Benjamin Kramer35f59b62010-04-10 16:03:31 +00003044 Out << 'I';
Rafael Espindolad9800722010-03-11 14:07:00 +00003045 for (unsigned i = 0, e = AL.size(); i != e; ++i)
3046 mangleTemplateArg(PL.getParam(i), AL[i]);
Benjamin Kramer35f59b62010-04-10 16:03:31 +00003047 Out << 'E';
Anders Carlsson7a0ba872009-05-15 16:09:15 +00003048}
3049
Rafael Espindolad9800722010-03-11 14:07:00 +00003050void CXXNameMangler::mangleTemplateArgs(const TemplateParameterList &PL,
3051 const TemplateArgument *TemplateArgs,
Anders Carlsson7624f212009-09-18 02:42:01 +00003052 unsigned NumTemplateArgs) {
3053 // <template-args> ::= I <template-arg>+ E
Benjamin Kramer35f59b62010-04-10 16:03:31 +00003054 Out << 'I';
Daniel Dunbar7e0c1952009-11-21 09:17:15 +00003055 for (unsigned i = 0; i != NumTemplateArgs; ++i)
Rafael Espindolad9800722010-03-11 14:07:00 +00003056 mangleTemplateArg(PL.getParam(i), TemplateArgs[i]);
Benjamin Kramer35f59b62010-04-10 16:03:31 +00003057 Out << 'E';
Anders Carlsson7624f212009-09-18 02:42:01 +00003058}
3059
Rafael Espindolad9800722010-03-11 14:07:00 +00003060void CXXNameMangler::mangleTemplateArg(const NamedDecl *P,
Douglas Gregorf1588662011-07-12 15:18:55 +00003061 TemplateArgument A) {
Mike Stump1eb44332009-09-09 15:08:12 +00003062 // <template-arg> ::= <type> # type or template
Anders Carlsson7a0ba872009-05-15 16:09:15 +00003063 // ::= X <expression> E # expression
3064 // ::= <expr-primary> # simple expressions
Douglas Gregor4fc48662011-01-13 16:39:34 +00003065 // ::= J <template-arg>* E # argument pack
Douglas Gregorf1588662011-07-12 15:18:55 +00003066 // ::= sp <expression> # pack expansion of (C++0x)
3067 if (!A.isInstantiationDependent() || A.isDependent())
3068 A = Context.getASTContext().getCanonicalTemplateArgument(A);
3069
Anders Carlsson7a0ba872009-05-15 16:09:15 +00003070 switch (A.getKind()) {
Douglas Gregorf90b27a2011-01-03 22:36:02 +00003071 case TemplateArgument::Null:
3072 llvm_unreachable("Cannot mangle NULL template argument");
3073
Anders Carlsson7a0ba872009-05-15 16:09:15 +00003074 case TemplateArgument::Type:
3075 mangleType(A.getAsType());
3076 break;
Anders Carlsson9e85c742009-12-23 19:30:55 +00003077 case TemplateArgument::Template:
John McCallb6f532e2010-07-14 06:43:17 +00003078 // This is mangled as <type>.
3079 mangleType(A.getAsTemplate());
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00003080 break;
Douglas Gregora7fc9012011-01-05 18:58:31 +00003081 case TemplateArgument::TemplateExpansion:
Douglas Gregor4fc48662011-01-13 16:39:34 +00003082 // <type> ::= Dp <type> # pack expansion (C++0x)
Douglas Gregora7fc9012011-01-05 18:58:31 +00003083 Out << "Dp";
3084 mangleType(A.getAsTemplateOrTemplatePattern());
3085 break;
John McCall092beef2012-01-06 05:06:35 +00003086 case TemplateArgument::Expression: {
3087 // It's possible to end up with a DeclRefExpr here in certain
3088 // dependent cases, in which case we should mangle as a
3089 // declaration.
3090 const Expr *E = A.getAsExpr()->IgnoreParens();
3091 if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E)) {
3092 const ValueDecl *D = DRE->getDecl();
3093 if (isa<VarDecl>(D) || isa<FunctionDecl>(D)) {
3094 Out << "L";
3095 mangle(D, "_Z");
3096 Out << 'E';
3097 break;
3098 }
3099 }
3100
Anders Carlssond553f8c2009-09-21 01:21:10 +00003101 Out << 'X';
John McCall092beef2012-01-06 05:06:35 +00003102 mangleExpression(E);
Anders Carlssond553f8c2009-09-21 01:21:10 +00003103 Out << 'E';
3104 break;
John McCall092beef2012-01-06 05:06:35 +00003105 }
Anders Carlssone170ba72009-12-14 01:45:37 +00003106 case TemplateArgument::Integral:
3107 mangleIntegerLiteral(A.getIntegralType(), *A.getAsIntegral());
Anders Carlsson7a0ba872009-05-15 16:09:15 +00003108 break;
Daniel Dunbar7e0c1952009-11-21 09:17:15 +00003109 case TemplateArgument::Declaration: {
Douglas Gregor20f0cc72010-04-23 03:10:43 +00003110 assert(P && "Missing template parameter for declaration argument");
Daniel Dunbar7e0c1952009-11-21 09:17:15 +00003111 // <expr-primary> ::= L <mangled-name> E # external name
3112
Rafael Espindolad9800722010-03-11 14:07:00 +00003113 // Clang produces AST's where pointer-to-member-function expressions
Daniel Dunbar7e0c1952009-11-21 09:17:15 +00003114 // and pointer-to-function expressions are represented as a declaration not
Rafael Espindolad9800722010-03-11 14:07:00 +00003115 // an expression. We compensate for it here to produce the correct mangling.
3116 NamedDecl *D = cast<NamedDecl>(A.getAsDecl());
3117 const NonTypeTemplateParmDecl *Parameter = cast<NonTypeTemplateParmDecl>(P);
John McCallc0a45592011-04-24 08:43:07 +00003118 bool compensateMangling = !Parameter->getType()->isReferenceType();
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 }
Douglas Gregorf90b27a2011-01-03 22:36:02 +00003141
3142 case TemplateArgument::Pack: {
3143 // Note: proposal by Mike Herrick on 12/20/10
3144 Out << 'J';
3145 for (TemplateArgument::pack_iterator PA = A.pack_begin(),
3146 PAEnd = A.pack_end();
3147 PA != PAEnd; ++PA)
3148 mangleTemplateArg(P, *PA);
3149 Out << 'E';
3150 }
Daniel Dunbar7e0c1952009-11-21 09:17:15 +00003151 }
Anders Carlsson7a0ba872009-05-15 16:09:15 +00003152}
3153
Anders Carlsson0ccdf8d2009-09-27 00:38:53 +00003154void CXXNameMangler::mangleTemplateParameter(unsigned Index) {
3155 // <template-param> ::= T_ # first template parameter
3156 // ::= T <parameter-2 non-negative number> _
3157 if (Index == 0)
3158 Out << "T_";
3159 else
3160 Out << 'T' << (Index - 1) << '_';
3161}
3162
John McCall68a51a72011-07-01 00:04:39 +00003163void CXXNameMangler::mangleExistingSubstitution(QualType type) {
3164 bool result = mangleSubstitution(type);
3165 assert(result && "no existing substitution for type");
3166 (void) result;
3167}
3168
3169void CXXNameMangler::mangleExistingSubstitution(TemplateName tname) {
3170 bool result = mangleSubstitution(tname);
3171 assert(result && "no existing substitution for template name");
3172 (void) result;
3173}
3174
Anders Carlsson76967372009-09-17 00:43:46 +00003175// <substitution> ::= S <seq-id> _
3176// ::= S_
Anders Carlsson6862fc72009-09-17 04:16:28 +00003177bool CXXNameMangler::mangleSubstitution(const NamedDecl *ND) {
Anders Carlssone7c8cb62009-09-26 20:53:44 +00003178 // Try one of the standard substitutions first.
3179 if (mangleStandardSubstitution(ND))
3180 return true;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00003181
Anders Carlsson433d1372009-11-07 04:26:04 +00003182 ND = cast<NamedDecl>(ND->getCanonicalDecl());
Anders Carlsson6862fc72009-09-17 04:16:28 +00003183 return mangleSubstitution(reinterpret_cast<uintptr_t>(ND));
3184}
3185
Douglas Gregor14795c82011-12-03 18:24:43 +00003186/// \brief Determine whether the given type has any qualifiers that are
3187/// relevant for substitutions.
3188static bool hasMangledSubstitutionQualifiers(QualType T) {
3189 Qualifiers Qs = T.getQualifiers();
3190 return Qs.getCVRQualifiers() || Qs.hasAddressSpace();
3191}
3192
Anders Carlsson76967372009-09-17 00:43:46 +00003193bool CXXNameMangler::mangleSubstitution(QualType T) {
Douglas Gregor14795c82011-12-03 18:24:43 +00003194 if (!hasMangledSubstitutionQualifiers(T)) {
Anders Carlssond99edc42009-09-26 03:55:37 +00003195 if (const RecordType *RT = T->getAs<RecordType>())
3196 return mangleSubstitution(RT->getDecl());
3197 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00003198
Anders Carlsson76967372009-09-17 00:43:46 +00003199 uintptr_t TypePtr = reinterpret_cast<uintptr_t>(T.getAsOpaquePtr());
3200
Anders Carlssond3a932a2009-09-17 03:53:28 +00003201 return mangleSubstitution(TypePtr);
3202}
3203
Douglas Gregor1e9268e2010-04-28 05:58:56 +00003204bool CXXNameMangler::mangleSubstitution(TemplateName Template) {
3205 if (TemplateDecl *TD = Template.getAsTemplateDecl())
3206 return mangleSubstitution(TD);
Sean Huntc3021132010-05-05 15:23:54 +00003207
Douglas Gregor1e9268e2010-04-28 05:58:56 +00003208 Template = Context.getASTContext().getCanonicalTemplateName(Template);
3209 return mangleSubstitution(
3210 reinterpret_cast<uintptr_t>(Template.getAsVoidPointer()));
3211}
3212
Anders Carlssond3a932a2009-09-17 03:53:28 +00003213bool CXXNameMangler::mangleSubstitution(uintptr_t Ptr) {
Benjamin Kramer35f59b62010-04-10 16:03:31 +00003214 llvm::DenseMap<uintptr_t, unsigned>::iterator I = Substitutions.find(Ptr);
Anders Carlsson76967372009-09-17 00:43:46 +00003215 if (I == Substitutions.end())
3216 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00003217
Anders Carlsson76967372009-09-17 00:43:46 +00003218 unsigned SeqID = I->second;
3219 if (SeqID == 0)
3220 Out << "S_";
3221 else {
3222 SeqID--;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00003223
Anders Carlsson76967372009-09-17 00:43:46 +00003224 // <seq-id> is encoded in base-36, using digits and upper case letters.
3225 char Buffer[10];
Benjamin Kramer35f59b62010-04-10 16:03:31 +00003226 char *BufferPtr = llvm::array_endof(Buffer);
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00003227
Anders Carlsson76967372009-09-17 00:43:46 +00003228 if (SeqID == 0) *--BufferPtr = '0';
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00003229
Anders Carlsson76967372009-09-17 00:43:46 +00003230 while (SeqID) {
3231 assert(BufferPtr > Buffer && "Buffer overflow!");
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00003232
John McCall6ab30e02010-06-09 07:26:17 +00003233 char c = static_cast<char>(SeqID % 36);
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00003234
Anders Carlsson76967372009-09-17 00:43:46 +00003235 *--BufferPtr = (c < 10 ? '0' + c : 'A' + c - 10);
3236 SeqID /= 36;
3237 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00003238
Benjamin Kramer35f59b62010-04-10 16:03:31 +00003239 Out << 'S'
Chris Lattner5f9e2722011-07-23 10:55:15 +00003240 << StringRef(BufferPtr, llvm::array_endof(Buffer)-BufferPtr)
Benjamin Kramer35f59b62010-04-10 16:03:31 +00003241 << '_';
Anders Carlsson76967372009-09-17 00:43:46 +00003242 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00003243
Anders Carlsson76967372009-09-17 00:43:46 +00003244 return true;
3245}
3246
Anders Carlssonf514b542009-09-27 00:12:57 +00003247static bool isCharType(QualType T) {
3248 if (T.isNull())
3249 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00003250
Anders Carlssonf514b542009-09-27 00:12:57 +00003251 return T->isSpecificBuiltinType(BuiltinType::Char_S) ||
3252 T->isSpecificBuiltinType(BuiltinType::Char_U);
3253}
3254
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00003255/// isCharSpecialization - Returns whether a given type is a template
Anders Carlssonf514b542009-09-27 00:12:57 +00003256/// specialization of a given name with a single argument of type char.
3257static bool isCharSpecialization(QualType T, const char *Name) {
3258 if (T.isNull())
3259 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00003260
Anders Carlssonf514b542009-09-27 00:12:57 +00003261 const RecordType *RT = T->getAs<RecordType>();
3262 if (!RT)
3263 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00003264
3265 const ClassTemplateSpecializationDecl *SD =
Anders Carlssonf514b542009-09-27 00:12:57 +00003266 dyn_cast<ClassTemplateSpecializationDecl>(RT->getDecl());
3267 if (!SD)
3268 return false;
3269
Douglas Gregorccc1b5e2012-02-21 00:37:24 +00003270 if (!isStdNamespace(getEffectiveDeclContext(SD)))
Anders Carlssonf514b542009-09-27 00:12:57 +00003271 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00003272
Anders Carlssonf514b542009-09-27 00:12:57 +00003273 const TemplateArgumentList &TemplateArgs = SD->getTemplateArgs();
3274 if (TemplateArgs.size() != 1)
3275 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00003276
Anders Carlssonf514b542009-09-27 00:12:57 +00003277 if (!isCharType(TemplateArgs[0].getAsType()))
3278 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00003279
Daniel Dunbar01eb9b92009-10-18 21:17:35 +00003280 return SD->getIdentifier()->getName() == Name;
Anders Carlssonf514b542009-09-27 00:12:57 +00003281}
3282
Anders Carlsson91f88602009-12-07 19:56:42 +00003283template <std::size_t StrLen>
Benjamin Kramer54353f42010-11-25 18:29:30 +00003284static bool isStreamCharSpecialization(const ClassTemplateSpecializationDecl*SD,
3285 const char (&Str)[StrLen]) {
Anders Carlsson91f88602009-12-07 19:56:42 +00003286 if (!SD->getIdentifier()->isStr(Str))
3287 return false;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00003288
Anders Carlsson91f88602009-12-07 19:56:42 +00003289 const TemplateArgumentList &TemplateArgs = SD->getTemplateArgs();
3290 if (TemplateArgs.size() != 2)
3291 return false;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00003292
Anders Carlsson91f88602009-12-07 19:56:42 +00003293 if (!isCharType(TemplateArgs[0].getAsType()))
3294 return false;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00003295
Anders Carlsson91f88602009-12-07 19:56:42 +00003296 if (!isCharSpecialization(TemplateArgs[1].getAsType(), "char_traits"))
3297 return false;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00003298
Anders Carlsson91f88602009-12-07 19:56:42 +00003299 return true;
3300}
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00003301
Anders Carlssone7c8cb62009-09-26 20:53:44 +00003302bool CXXNameMangler::mangleStandardSubstitution(const NamedDecl *ND) {
3303 // <substitution> ::= St # ::std::
Anders Carlsson8c031552009-09-26 23:10:05 +00003304 if (const NamespaceDecl *NS = dyn_cast<NamespaceDecl>(ND)) {
Anders Carlsson47846d22009-12-04 06:23:23 +00003305 if (isStd(NS)) {
Anders Carlsson8c031552009-09-26 23:10:05 +00003306 Out << "St";
3307 return true;
3308 }
3309 }
3310
3311 if (const ClassTemplateDecl *TD = dyn_cast<ClassTemplateDecl>(ND)) {
Douglas Gregorccc1b5e2012-02-21 00:37:24 +00003312 if (!isStdNamespace(getEffectiveDeclContext(TD)))
Anders Carlsson8c031552009-09-26 23:10:05 +00003313 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00003314
Anders Carlsson8c031552009-09-26 23:10:05 +00003315 // <substitution> ::= Sa # ::std::allocator
3316 if (TD->getIdentifier()->isStr("allocator")) {
3317 Out << "Sa";
3318 return true;
3319 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00003320
Anders Carlsson189d59c2009-09-26 23:14:39 +00003321 // <<substitution> ::= Sb # ::std::basic_string
3322 if (TD->getIdentifier()->isStr("basic_string")) {
3323 Out << "Sb";
3324 return true;
3325 }
Anders Carlsson8c031552009-09-26 23:10:05 +00003326 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00003327
3328 if (const ClassTemplateSpecializationDecl *SD =
Anders Carlssonf514b542009-09-27 00:12:57 +00003329 dyn_cast<ClassTemplateSpecializationDecl>(ND)) {
Douglas Gregorccc1b5e2012-02-21 00:37:24 +00003330 if (!isStdNamespace(getEffectiveDeclContext(SD)))
Eli Friedman5370ee22010-02-23 18:25:09 +00003331 return false;
3332
Anders Carlssonf514b542009-09-27 00:12:57 +00003333 // <substitution> ::= Ss # ::std::basic_string<char,
3334 // ::std::char_traits<char>,
3335 // ::std::allocator<char> >
3336 if (SD->getIdentifier()->isStr("basic_string")) {
3337 const TemplateArgumentList &TemplateArgs = SD->getTemplateArgs();
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00003338
Anders Carlssonf514b542009-09-27 00:12:57 +00003339 if (TemplateArgs.size() != 3)
3340 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00003341
Anders Carlssonf514b542009-09-27 00:12:57 +00003342 if (!isCharType(TemplateArgs[0].getAsType()))
3343 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00003344
Anders Carlssonf514b542009-09-27 00:12:57 +00003345 if (!isCharSpecialization(TemplateArgs[1].getAsType(), "char_traits"))
3346 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00003347
Anders Carlssonf514b542009-09-27 00:12:57 +00003348 if (!isCharSpecialization(TemplateArgs[2].getAsType(), "allocator"))
3349 return false;
3350
3351 Out << "Ss";
3352 return true;
3353 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00003354
Anders Carlsson91f88602009-12-07 19:56:42 +00003355 // <substitution> ::= Si # ::std::basic_istream<char,
3356 // ::std::char_traits<char> >
3357 if (isStreamCharSpecialization(SD, "basic_istream")) {
3358 Out << "Si";
3359 return true;
3360 }
3361
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00003362 // <substitution> ::= So # ::std::basic_ostream<char,
Anders Carlsson8f8fd8e2009-10-08 17:20:26 +00003363 // ::std::char_traits<char> >
Anders Carlsson91f88602009-12-07 19:56:42 +00003364 if (isStreamCharSpecialization(SD, "basic_ostream")) {
Anders Carlsson8f8fd8e2009-10-08 17:20:26 +00003365 Out << "So";
3366 return true;
3367 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00003368
Anders Carlsson91f88602009-12-07 19:56:42 +00003369 // <substitution> ::= Sd # ::std::basic_iostream<char,
3370 // ::std::char_traits<char> >
3371 if (isStreamCharSpecialization(SD, "basic_iostream")) {
3372 Out << "Sd";
3373 return true;
3374 }
Anders Carlssonf514b542009-09-27 00:12:57 +00003375 }
Anders Carlsson8c031552009-09-26 23:10:05 +00003376 return false;
Anders Carlssone7c8cb62009-09-26 20:53:44 +00003377}
3378
Anders Carlsson76967372009-09-17 00:43:46 +00003379void CXXNameMangler::addSubstitution(QualType T) {
Douglas Gregor14795c82011-12-03 18:24:43 +00003380 if (!hasMangledSubstitutionQualifiers(T)) {
Anders Carlssond99edc42009-09-26 03:55:37 +00003381 if (const RecordType *RT = T->getAs<RecordType>()) {
3382 addSubstitution(RT->getDecl());
3383 return;
3384 }
3385 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00003386
Anders Carlsson76967372009-09-17 00:43:46 +00003387 uintptr_t TypePtr = reinterpret_cast<uintptr_t>(T.getAsOpaquePtr());
Anders Carlssond3a932a2009-09-17 03:53:28 +00003388 addSubstitution(TypePtr);
3389}
3390
Douglas Gregor1e9268e2010-04-28 05:58:56 +00003391void CXXNameMangler::addSubstitution(TemplateName Template) {
3392 if (TemplateDecl *TD = Template.getAsTemplateDecl())
3393 return addSubstitution(TD);
Sean Huntc3021132010-05-05 15:23:54 +00003394
Douglas Gregor1e9268e2010-04-28 05:58:56 +00003395 Template = Context.getASTContext().getCanonicalTemplateName(Template);
3396 addSubstitution(reinterpret_cast<uintptr_t>(Template.getAsVoidPointer()));
3397}
3398
Anders Carlssond3a932a2009-09-17 03:53:28 +00003399void CXXNameMangler::addSubstitution(uintptr_t Ptr) {
Anders Carlssond3a932a2009-09-17 03:53:28 +00003400 assert(!Substitutions.count(Ptr) && "Substitution already exists!");
Anders Carlsson9d85b722010-06-02 04:29:50 +00003401 Substitutions[Ptr] = SeqID++;
Anders Carlsson76967372009-09-17 00:43:46 +00003402}
3403
Daniel Dunbar1b077112009-11-21 09:06:10 +00003404//
Mike Stump1eb44332009-09-09 15:08:12 +00003405
Daniel Dunbar1b077112009-11-21 09:06:10 +00003406/// \brief Mangles the name of the declaration D and emits that name to the
3407/// given output stream.
3408///
3409/// If the declaration D requires a mangled name, this routine will emit that
3410/// mangled name to \p os and return true. Otherwise, \p os will be unchanged
3411/// and this routine will return false. In this case, the caller should just
3412/// emit the identifier of the declaration (\c D->getIdentifier()) as its
3413/// name.
Peter Collingbourne14110472011-01-13 18:57:25 +00003414void ItaniumMangleContext::mangleName(const NamedDecl *D,
Chris Lattner5f9e2722011-07-23 10:55:15 +00003415 raw_ostream &Out) {
Daniel Dunbarc02ab4c2009-11-21 09:14:44 +00003416 assert((isa<FunctionDecl>(D) || isa<VarDecl>(D)) &&
3417 "Invalid mangleName() call, argument is not a variable or function!");
3418 assert(!isa<CXXConstructorDecl>(D) && !isa<CXXDestructorDecl>(D) &&
3419 "Invalid mangleName() call on 'structor decl!");
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00003420
Daniel Dunbar1b077112009-11-21 09:06:10 +00003421 PrettyStackTraceDecl CrashInfo(D, SourceLocation(),
3422 getASTContext().getSourceManager(),
3423 "Mangling declaration");
Mike Stump1eb44332009-09-09 15:08:12 +00003424
John McCallfb44de92011-05-01 22:35:37 +00003425 CXXNameMangler Mangler(*this, Out, D);
Daniel Dunbar94fd26d2009-11-21 09:06:22 +00003426 return Mangler.mangle(D);
Daniel Dunbar1b077112009-11-21 09:06:10 +00003427}
Mike Stump1eb44332009-09-09 15:08:12 +00003428
Peter Collingbourne14110472011-01-13 18:57:25 +00003429void ItaniumMangleContext::mangleCXXCtor(const CXXConstructorDecl *D,
3430 CXXCtorType Type,
Chris Lattner5f9e2722011-07-23 10:55:15 +00003431 raw_ostream &Out) {
Rafael Espindolac4850c22011-02-10 23:59:36 +00003432 CXXNameMangler Mangler(*this, Out, D, Type);
Daniel Dunbar77939c92009-11-21 09:06:31 +00003433 Mangler.mangle(D);
Daniel Dunbar1b077112009-11-21 09:06:10 +00003434}
Mike Stump1eb44332009-09-09 15:08:12 +00003435
Peter Collingbourne14110472011-01-13 18:57:25 +00003436void ItaniumMangleContext::mangleCXXDtor(const CXXDestructorDecl *D,
3437 CXXDtorType Type,
Chris Lattner5f9e2722011-07-23 10:55:15 +00003438 raw_ostream &Out) {
Rafael Espindolac4850c22011-02-10 23:59:36 +00003439 CXXNameMangler Mangler(*this, Out, D, Type);
Daniel Dunbar77939c92009-11-21 09:06:31 +00003440 Mangler.mangle(D);
Daniel Dunbar1b077112009-11-21 09:06:10 +00003441}
Mike Stumpf1216772009-07-31 18:25:34 +00003442
Peter Collingbourne14110472011-01-13 18:57:25 +00003443void ItaniumMangleContext::mangleThunk(const CXXMethodDecl *MD,
3444 const ThunkInfo &Thunk,
Chris Lattner5f9e2722011-07-23 10:55:15 +00003445 raw_ostream &Out) {
Anders Carlsson19879c92010-03-23 17:17:29 +00003446 // <special-name> ::= T <call-offset> <base encoding>
3447 // # base is the nominal target function of thunk
3448 // <special-name> ::= Tc <call-offset> <call-offset> <base encoding>
3449 // # base is the nominal target function of thunk
3450 // # first call-offset is 'this' adjustment
3451 // # second call-offset is result adjustment
Sean Huntc3021132010-05-05 15:23:54 +00003452
Anders Carlsson19879c92010-03-23 17:17:29 +00003453 assert(!isa<CXXDestructorDecl>(MD) &&
3454 "Use mangleCXXDtor for destructor decls!");
Rafael Espindolac4850c22011-02-10 23:59:36 +00003455 CXXNameMangler Mangler(*this, Out);
Anders Carlsson19879c92010-03-23 17:17:29 +00003456 Mangler.getStream() << "_ZT";
3457 if (!Thunk.Return.isEmpty())
3458 Mangler.getStream() << 'c';
Sean Huntc3021132010-05-05 15:23:54 +00003459
Anders Carlsson19879c92010-03-23 17:17:29 +00003460 // Mangle the 'this' pointer adjustment.
3461 Mangler.mangleCallOffset(Thunk.This.NonVirtual, Thunk.This.VCallOffsetOffset);
Sean Huntc3021132010-05-05 15:23:54 +00003462
Anders Carlsson19879c92010-03-23 17:17:29 +00003463 // Mangle the return pointer adjustment if there is one.
3464 if (!Thunk.Return.isEmpty())
3465 Mangler.mangleCallOffset(Thunk.Return.NonVirtual,
3466 Thunk.Return.VBaseOffsetOffset);
Sean Huntc3021132010-05-05 15:23:54 +00003467
Anders Carlsson19879c92010-03-23 17:17:29 +00003468 Mangler.mangleFunctionEncoding(MD);
3469}
3470
Sean Huntc3021132010-05-05 15:23:54 +00003471void
Peter Collingbourne14110472011-01-13 18:57:25 +00003472ItaniumMangleContext::mangleCXXDtorThunk(const CXXDestructorDecl *DD,
3473 CXXDtorType Type,
3474 const ThisAdjustment &ThisAdjustment,
Chris Lattner5f9e2722011-07-23 10:55:15 +00003475 raw_ostream &Out) {
Anders Carlsson19879c92010-03-23 17:17:29 +00003476 // <special-name> ::= T <call-offset> <base encoding>
3477 // # base is the nominal target function of thunk
Rafael Espindolac4850c22011-02-10 23:59:36 +00003478 CXXNameMangler Mangler(*this, Out, DD, Type);
Anders Carlsson19879c92010-03-23 17:17:29 +00003479 Mangler.getStream() << "_ZT";
3480
3481 // Mangle the 'this' pointer adjustment.
Sean Huntc3021132010-05-05 15:23:54 +00003482 Mangler.mangleCallOffset(ThisAdjustment.NonVirtual,
Anders Carlsson19879c92010-03-23 17:17:29 +00003483 ThisAdjustment.VCallOffsetOffset);
3484
3485 Mangler.mangleFunctionEncoding(DD);
3486}
3487
Daniel Dunbarc0747712009-11-21 09:12:13 +00003488/// mangleGuardVariable - Returns the mangled name for a guard variable
3489/// for the passed in VarDecl.
Peter Collingbourne14110472011-01-13 18:57:25 +00003490void ItaniumMangleContext::mangleItaniumGuardVariable(const VarDecl *D,
Chris Lattner5f9e2722011-07-23 10:55:15 +00003491 raw_ostream &Out) {
Daniel Dunbarc0747712009-11-21 09:12:13 +00003492 // <special-name> ::= GV <object name> # Guard variable for one-time
3493 // # initialization
Rafael Espindolac4850c22011-02-10 23:59:36 +00003494 CXXNameMangler Mangler(*this, Out);
Daniel Dunbarc0747712009-11-21 09:12:13 +00003495 Mangler.getStream() << "_ZGV";
3496 Mangler.mangleName(D);
3497}
3498
Peter Collingbourne14110472011-01-13 18:57:25 +00003499void ItaniumMangleContext::mangleReferenceTemporary(const VarDecl *D,
Chris Lattner5f9e2722011-07-23 10:55:15 +00003500 raw_ostream &Out) {
Anders Carlsson715edf22010-06-26 16:09:40 +00003501 // We match the GCC mangling here.
3502 // <special-name> ::= GR <object name>
Rafael Espindolac4850c22011-02-10 23:59:36 +00003503 CXXNameMangler Mangler(*this, Out);
Anders Carlsson715edf22010-06-26 16:09:40 +00003504 Mangler.getStream() << "_ZGR";
3505 Mangler.mangleName(D);
3506}
3507
Peter Collingbourne14110472011-01-13 18:57:25 +00003508void ItaniumMangleContext::mangleCXXVTable(const CXXRecordDecl *RD,
Chris Lattner5f9e2722011-07-23 10:55:15 +00003509 raw_ostream &Out) {
Daniel Dunbarc0747712009-11-21 09:12:13 +00003510 // <special-name> ::= TV <type> # virtual table
Rafael Espindolac4850c22011-02-10 23:59:36 +00003511 CXXNameMangler Mangler(*this, Out);
Daniel Dunbarc0747712009-11-21 09:12:13 +00003512 Mangler.getStream() << "_ZTV";
Douglas Gregor1b12a3b2010-05-26 05:11:13 +00003513 Mangler.mangleNameOrStandardSubstitution(RD);
Daniel Dunbar1b077112009-11-21 09:06:10 +00003514}
Mike Stump82d75b02009-11-10 01:58:37 +00003515
Peter Collingbourne14110472011-01-13 18:57:25 +00003516void ItaniumMangleContext::mangleCXXVTT(const CXXRecordDecl *RD,
Chris Lattner5f9e2722011-07-23 10:55:15 +00003517 raw_ostream &Out) {
Daniel Dunbarc0747712009-11-21 09:12:13 +00003518 // <special-name> ::= TT <type> # VTT structure
Rafael Espindolac4850c22011-02-10 23:59:36 +00003519 CXXNameMangler Mangler(*this, Out);
Daniel Dunbarc0747712009-11-21 09:12:13 +00003520 Mangler.getStream() << "_ZTT";
Douglas Gregor1b12a3b2010-05-26 05:11:13 +00003521 Mangler.mangleNameOrStandardSubstitution(RD);
Daniel Dunbar1b077112009-11-21 09:06:10 +00003522}
Mike Stumpab3f7e92009-11-10 01:41:59 +00003523
Peter Collingbourne14110472011-01-13 18:57:25 +00003524void ItaniumMangleContext::mangleCXXCtorVTable(const CXXRecordDecl *RD,
3525 int64_t Offset,
3526 const CXXRecordDecl *Type,
Chris Lattner5f9e2722011-07-23 10:55:15 +00003527 raw_ostream &Out) {
Daniel Dunbarc0747712009-11-21 09:12:13 +00003528 // <special-name> ::= TC <type> <offset number> _ <base type>
Rafael Espindolac4850c22011-02-10 23:59:36 +00003529 CXXNameMangler Mangler(*this, Out);
Daniel Dunbarc0747712009-11-21 09:12:13 +00003530 Mangler.getStream() << "_ZTC";
Douglas Gregor1b12a3b2010-05-26 05:11:13 +00003531 Mangler.mangleNameOrStandardSubstitution(RD);
Daniel Dunbarc0747712009-11-21 09:12:13 +00003532 Mangler.getStream() << Offset;
Benjamin Kramer35f59b62010-04-10 16:03:31 +00003533 Mangler.getStream() << '_';
Douglas Gregor1b12a3b2010-05-26 05:11:13 +00003534 Mangler.mangleNameOrStandardSubstitution(Type);
Daniel Dunbar1b077112009-11-21 09:06:10 +00003535}
Mike Stump738f8c22009-07-31 23:15:31 +00003536
Peter Collingbourne14110472011-01-13 18:57:25 +00003537void ItaniumMangleContext::mangleCXXRTTI(QualType Ty,
Chris Lattner5f9e2722011-07-23 10:55:15 +00003538 raw_ostream &Out) {
Daniel Dunbarc0747712009-11-21 09:12:13 +00003539 // <special-name> ::= TI <type> # typeinfo structure
Douglas Gregor154fe982009-12-23 22:04:40 +00003540 assert(!Ty.hasQualifiers() && "RTTI info cannot have top-level qualifiers");
Rafael Espindolac4850c22011-02-10 23:59:36 +00003541 CXXNameMangler Mangler(*this, Out);
Daniel Dunbarc0747712009-11-21 09:12:13 +00003542 Mangler.getStream() << "_ZTI";
3543 Mangler.mangleType(Ty);
Daniel Dunbar1b077112009-11-21 09:06:10 +00003544}
Mike Stump67795982009-11-14 00:14:13 +00003545
Peter Collingbourne14110472011-01-13 18:57:25 +00003546void ItaniumMangleContext::mangleCXXRTTIName(QualType Ty,
Chris Lattner5f9e2722011-07-23 10:55:15 +00003547 raw_ostream &Out) {
Daniel Dunbarc0747712009-11-21 09:12:13 +00003548 // <special-name> ::= TS <type> # typeinfo name (null terminated byte string)
Rafael Espindolac4850c22011-02-10 23:59:36 +00003549 CXXNameMangler Mangler(*this, Out);
Daniel Dunbarc0747712009-11-21 09:12:13 +00003550 Mangler.getStream() << "_ZTS";
3551 Mangler.mangleType(Ty);
Mike Stumpf1216772009-07-31 18:25:34 +00003552}
Peter Collingbourne14110472011-01-13 18:57:25 +00003553
3554MangleContext *clang::createItaniumMangleContext(ASTContext &Context,
David Blaikied6471f72011-09-25 23:23:43 +00003555 DiagnosticsEngine &Diags) {
Peter Collingbourne14110472011-01-13 18:57:25 +00003556 return new ItaniumMangleContext(Context, Diags);
3557}