blob: 93ebcc50cf2157dc20cee911c6132373b0103a25 [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
55 = dyn_cast_or_null<ParmVarDecl>(RD->getLambdaContextDecl()))
56 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
Anders Carlsson5cc58c62009-09-22 17:23:30 +0000556 while (isa<LinkageSpecDecl>(DC))
Douglas Gregorccc1b5e2012-02-21 00:37:24 +0000557 DC = getEffectiveParentContext(DC);
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000558
Anders Carlssond58d6f72009-09-17 16:12:20 +0000559 if (DC->isTranslationUnit() || isStdNamespace(DC)) {
Anders Carlsson2744a062009-09-18 19:00:18 +0000560 // Check if we have a template.
561 const TemplateArgumentList *TemplateArgs = 0;
Anders Carlsson0fa6df42009-09-26 19:45:45 +0000562 if (const TemplateDecl *TD = isTemplate(ND, TemplateArgs)) {
Anders Carlsson2744a062009-09-18 19:00:18 +0000563 mangleUnscopedTemplateName(TD);
Rafael Espindolad9800722010-03-11 14:07:00 +0000564 TemplateParameterList *TemplateParameters = TD->getTemplateParameters();
565 mangleTemplateArgs(*TemplateParameters, *TemplateArgs);
Anders Carlsson2744a062009-09-18 19:00:18 +0000566 return;
Anders Carlsson7482e242009-09-18 04:29:09 +0000567 }
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000568
Anders Carlsson7482e242009-09-18 04:29:09 +0000569 mangleUnscopedName(ND);
570 return;
571 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000572
Anders Carlsson7b06f6c2009-12-10 03:14:39 +0000573 if (isa<FunctionDecl>(DC) || isa<ObjCMethodDecl>(DC)) {
Anders Carlsson7482e242009-09-18 04:29:09 +0000574 mangleLocalName(ND);
575 return;
576 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000577
Eli Friedman7facf842009-12-02 20:32:49 +0000578 mangleNestedName(ND, DC);
Anders Carlsson7482e242009-09-18 04:29:09 +0000579}
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000580void CXXNameMangler::mangleName(const TemplateDecl *TD,
Anders Carlsson7624f212009-09-18 02:42:01 +0000581 const TemplateArgument *TemplateArgs,
582 unsigned NumTemplateArgs) {
Douglas Gregorccc1b5e2012-02-21 00:37:24 +0000583 const DeclContext *DC = IgnoreLinkageSpecDecls(getEffectiveDeclContext(TD));
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000584
Anders Carlsson7624f212009-09-18 02:42:01 +0000585 if (DC->isTranslationUnit() || isStdNamespace(DC)) {
Anders Carlsson0fa6df42009-09-26 19:45:45 +0000586 mangleUnscopedTemplateName(TD);
Rafael Espindolad9800722010-03-11 14:07:00 +0000587 TemplateParameterList *TemplateParameters = TD->getTemplateParameters();
588 mangleTemplateArgs(*TemplateParameters, TemplateArgs, NumTemplateArgs);
Anders Carlsson7624f212009-09-18 02:42:01 +0000589 } else {
590 mangleNestedName(TD, TemplateArgs, NumTemplateArgs);
591 }
592}
593
Anders Carlsson201ce742009-09-17 03:17:01 +0000594void CXXNameMangler::mangleUnscopedName(const NamedDecl *ND) {
595 // <unscoped-name> ::= <unqualified-name>
596 // ::= St <unqualified-name> # ::std::
Douglas Gregorccc1b5e2012-02-21 00:37:24 +0000597 if (isStdNamespace(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)) {
1117 if (Record->isLambda()) {
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) {
1316 // FIXME: Figure out if we're in a function body, default argument,
1317 // or initializer for a class member.
1318
1319 Out << "Ul";
1320 DeclarationName Name
1321 = getASTContext().DeclarationNames.getCXXOperatorName(OO_Call);
1322 const FunctionProtoType *Proto
1323 = cast<CXXMethodDecl>(*Lambda->lookup(Name).first)->getType()->
1324 getAs<FunctionProtoType>();
1325 mangleBareFunctionType(Proto, /*MangleReturnType=*/false);
1326 Out << "E";
1327
1328 // The number is omitted for the first closure type with a given
1329 // <lambda-sig> in a given context; it is n-2 for the nth closure type
1330 // (in lexical order) with that same <lambda-sig> and context.
1331 //
1332 // The AST keeps track of the number for us.
1333 if (unsigned Number = Lambda->getLambdaManglingNumber()) {
1334 if (Number > 1)
1335 mangleNumber(Number - 2);
1336 }
1337 Out << '_';
1338}
1339
John McCalla0ce15c2011-04-24 08:23:24 +00001340void CXXNameMangler::manglePrefix(NestedNameSpecifier *qualifier) {
1341 switch (qualifier->getKind()) {
1342 case NestedNameSpecifier::Global:
1343 // nothing
1344 return;
1345
1346 case NestedNameSpecifier::Namespace:
1347 mangleName(qualifier->getAsNamespace());
1348 return;
1349
1350 case NestedNameSpecifier::NamespaceAlias:
1351 mangleName(qualifier->getAsNamespaceAlias()->getNamespace());
1352 return;
1353
1354 case NestedNameSpecifier::TypeSpec:
1355 case NestedNameSpecifier::TypeSpecWithTemplate:
John McCall4f4e4132011-05-04 01:45:19 +00001356 manglePrefix(QualType(qualifier->getAsType(), 0));
John McCalla0ce15c2011-04-24 08:23:24 +00001357 return;
1358
1359 case NestedNameSpecifier::Identifier:
1360 // Member expressions can have these without prefixes, but that
1361 // should end up in mangleUnresolvedPrefix instead.
1362 assert(qualifier->getPrefix());
1363 manglePrefix(qualifier->getPrefix());
1364
1365 mangleSourceName(qualifier->getAsIdentifier());
1366 return;
1367 }
1368
1369 llvm_unreachable("unexpected nested name specifier");
1370}
1371
Fariborz Jahanian57058532010-03-03 19:41:08 +00001372void CXXNameMangler::manglePrefix(const DeclContext *DC, bool NoFunction) {
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001373 // <prefix> ::= <prefix> <unqualified-name>
1374 // ::= <template-prefix> <template-args>
1375 // ::= <template-param>
1376 // ::= # empty
1377 // ::= <substitution>
Anders Carlsson6862fc72009-09-17 04:16:28 +00001378
Anders Carlssonadd28822009-09-22 20:33:31 +00001379 while (isa<LinkageSpecDecl>(DC))
Douglas Gregorccc1b5e2012-02-21 00:37:24 +00001380 DC = getEffectiveParentContext(DC);
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00001381
Anders Carlsson9263e912009-09-18 18:39:58 +00001382 if (DC->isTranslationUnit())
1383 return;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00001384
Douglas Gregor35415f52010-05-25 17:04:15 +00001385 if (const BlockDecl *Block = dyn_cast<BlockDecl>(DC)) {
Douglas Gregorccc1b5e2012-02-21 00:37:24 +00001386 manglePrefix(getEffectiveParentContext(DC), NoFunction);
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00001387 SmallString<64> Name;
Rafael Espindolac4850c22011-02-10 23:59:36 +00001388 llvm::raw_svector_ostream NameStream(Name);
1389 Context.mangleBlock(Block, NameStream);
1390 NameStream.flush();
Douglas Gregor35415f52010-05-25 17:04:15 +00001391 Out << Name.size() << Name;
1392 return;
1393 }
1394
Anders Carlsson6862fc72009-09-17 04:16:28 +00001395 if (mangleSubstitution(cast<NamedDecl>(DC)))
1396 return;
Anders Carlsson7482e242009-09-18 04:29:09 +00001397
Anders Carlsson2ee3fca2009-09-18 20:11:09 +00001398 // Check if we have a template.
1399 const TemplateArgumentList *TemplateArgs = 0;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00001400 if (const TemplateDecl *TD = isTemplate(cast<NamedDecl>(DC), TemplateArgs)) {
Anders Carlsson2ee3fca2009-09-18 20:11:09 +00001401 mangleTemplatePrefix(TD);
Rafael Espindolad9800722010-03-11 14:07:00 +00001402 TemplateParameterList *TemplateParameters = TD->getTemplateParameters();
1403 mangleTemplateArgs(*TemplateParameters, *TemplateArgs);
Fariborz Jahanian57058532010-03-03 19:41:08 +00001404 }
Douglas Gregor35415f52010-05-25 17:04:15 +00001405 else if(NoFunction && (isa<FunctionDecl>(DC) || isa<ObjCMethodDecl>(DC)))
Fariborz Jahanian57058532010-03-03 19:41:08 +00001406 return;
Douglas Gregor35415f52010-05-25 17:04:15 +00001407 else if (const ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(DC))
1408 mangleObjCMethodName(Method);
Fariborz Jahanian57058532010-03-03 19:41:08 +00001409 else {
Douglas Gregorccc1b5e2012-02-21 00:37:24 +00001410 manglePrefix(getEffectiveParentContext(DC), NoFunction);
Anders Carlsson2ee3fca2009-09-18 20:11:09 +00001411 mangleUnqualifiedName(cast<NamedDecl>(DC));
1412 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00001413
Anders Carlsson6862fc72009-09-17 04:16:28 +00001414 addSubstitution(cast<NamedDecl>(DC));
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001415}
1416
Douglas Gregor20f0cc72010-04-23 03:10:43 +00001417void CXXNameMangler::mangleTemplatePrefix(TemplateName Template) {
1418 // <template-prefix> ::= <prefix> <template unqualified-name>
1419 // ::= <template-param>
1420 // ::= <substitution>
1421 if (TemplateDecl *TD = Template.getAsTemplateDecl())
1422 return mangleTemplatePrefix(TD);
1423
1424 if (QualifiedTemplateName *Qualified = Template.getAsQualifiedTemplateName())
John McCalla0ce15c2011-04-24 08:23:24 +00001425 manglePrefix(Qualified->getQualifier());
Sean Huntc3021132010-05-05 15:23:54 +00001426
Douglas Gregor20f0cc72010-04-23 03:10:43 +00001427 if (OverloadedTemplateStorage *Overloaded
1428 = Template.getAsOverloadedTemplate()) {
Sean Huntc3021132010-05-05 15:23:54 +00001429 mangleUnqualifiedName(0, (*Overloaded->begin())->getDeclName(),
Douglas Gregor20f0cc72010-04-23 03:10:43 +00001430 UnknownArity);
1431 return;
1432 }
Sean Huntc3021132010-05-05 15:23:54 +00001433
Douglas Gregor20f0cc72010-04-23 03:10:43 +00001434 DependentTemplateName *Dependent = Template.getAsDependentTemplateName();
1435 assert(Dependent && "Unknown template name kind?");
John McCalla0ce15c2011-04-24 08:23:24 +00001436 manglePrefix(Dependent->getQualifier());
Douglas Gregor1e9268e2010-04-28 05:58:56 +00001437 mangleUnscopedTemplateName(Template);
Douglas Gregor20f0cc72010-04-23 03:10:43 +00001438}
1439
Anders Carlsson0fa6df42009-09-26 19:45:45 +00001440void CXXNameMangler::mangleTemplatePrefix(const TemplateDecl *ND) {
Anders Carlsson7482e242009-09-18 04:29:09 +00001441 // <template-prefix> ::= <prefix> <template unqualified-name>
1442 // ::= <template-param>
1443 // ::= <substitution>
Douglas Gregor32fb4e12010-02-05 20:45:00 +00001444 // <template-template-param> ::= <template-param>
1445 // <substitution>
Anders Carlsson7482e242009-09-18 04:29:09 +00001446
Anders Carlssonaeb85372009-09-26 22:18:22 +00001447 if (mangleSubstitution(ND))
1448 return;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00001449
Douglas Gregor32fb4e12010-02-05 20:45:00 +00001450 // <template-template-param> ::= <template-param>
1451 if (const TemplateTemplateParmDecl *TTP
1452 = dyn_cast<TemplateTemplateParmDecl>(ND)) {
1453 mangleTemplateParameter(TTP->getIndex());
1454 return;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001455 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00001456
Douglas Gregorccc1b5e2012-02-21 00:37:24 +00001457 manglePrefix(getEffectiveDeclContext(ND));
Anders Carlsson1668f202009-09-26 20:13:56 +00001458 mangleUnqualifiedName(ND->getTemplatedDecl());
Anders Carlssonaeb85372009-09-26 22:18:22 +00001459 addSubstitution(ND);
Anders Carlsson7482e242009-09-18 04:29:09 +00001460}
1461
John McCallb6f532e2010-07-14 06:43:17 +00001462/// Mangles a template name under the production <type>. Required for
1463/// template template arguments.
1464/// <type> ::= <class-enum-type>
1465/// ::= <template-param>
1466/// ::= <substitution>
1467void CXXNameMangler::mangleType(TemplateName TN) {
1468 if (mangleSubstitution(TN))
1469 return;
1470
1471 TemplateDecl *TD = 0;
1472
1473 switch (TN.getKind()) {
1474 case TemplateName::QualifiedTemplate:
1475 TD = TN.getAsQualifiedTemplateName()->getTemplateDecl();
1476 goto HaveDecl;
1477
1478 case TemplateName::Template:
1479 TD = TN.getAsTemplateDecl();
1480 goto HaveDecl;
1481
1482 HaveDecl:
1483 if (isa<TemplateTemplateParmDecl>(TD))
1484 mangleTemplateParameter(cast<TemplateTemplateParmDecl>(TD)->getIndex());
1485 else
1486 mangleName(TD);
1487 break;
1488
1489 case TemplateName::OverloadedTemplate:
1490 llvm_unreachable("can't mangle an overloaded template name as a <type>");
John McCallb6f532e2010-07-14 06:43:17 +00001491
1492 case TemplateName::DependentTemplate: {
1493 const DependentTemplateName *Dependent = TN.getAsDependentTemplateName();
1494 assert(Dependent->isIdentifier());
1495
1496 // <class-enum-type> ::= <name>
1497 // <name> ::= <nested-name>
John McCalla0ce15c2011-04-24 08:23:24 +00001498 mangleUnresolvedPrefix(Dependent->getQualifier(), 0);
John McCallb6f532e2010-07-14 06:43:17 +00001499 mangleSourceName(Dependent->getIdentifier());
1500 break;
1501 }
1502
John McCallb44e0cf2011-06-30 21:59:02 +00001503 case TemplateName::SubstTemplateTemplateParm: {
1504 // Substituted template parameters are mangled as the substituted
1505 // template. This will check for the substitution twice, which is
1506 // fine, but we have to return early so that we don't try to *add*
1507 // the substitution twice.
1508 SubstTemplateTemplateParmStorage *subst
1509 = TN.getAsSubstTemplateTemplateParm();
1510 mangleType(subst->getReplacement());
1511 return;
1512 }
John McCall14606042011-06-30 08:33:18 +00001513
Douglas Gregor1aee05d2011-01-15 06:45:20 +00001514 case TemplateName::SubstTemplateTemplateParmPack: {
John McCall68a51a72011-07-01 00:04:39 +00001515 // FIXME: not clear how to mangle this!
1516 // template <template <class> class T...> class A {
1517 // template <template <class> class U...> void foo(B<T,U> x...);
1518 // };
1519 Out << "_SUBSTPACK_";
Douglas Gregor1aee05d2011-01-15 06:45:20 +00001520 break;
1521 }
John McCallb6f532e2010-07-14 06:43:17 +00001522 }
1523
1524 addSubstitution(TN);
1525}
1526
Mike Stump1eb44332009-09-09 15:08:12 +00001527void
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001528CXXNameMangler::mangleOperatorName(OverloadedOperatorKind OO, unsigned Arity) {
1529 switch (OO) {
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001530 // <operator-name> ::= nw # new
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001531 case OO_New: Out << "nw"; break;
1532 // ::= na # new[]
1533 case OO_Array_New: Out << "na"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001534 // ::= dl # delete
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001535 case OO_Delete: Out << "dl"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001536 // ::= da # delete[]
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001537 case OO_Array_Delete: Out << "da"; break;
1538 // ::= ps # + (unary)
John McCall5e1e89b2010-08-18 19:18:59 +00001539 // ::= pl # + (binary or unknown)
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001540 case OO_Plus:
Anders Carlsson8257d412009-12-22 06:36:32 +00001541 Out << (Arity == 1? "ps" : "pl"); break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001542 // ::= ng # - (unary)
John McCall5e1e89b2010-08-18 19:18:59 +00001543 // ::= mi # - (binary or unknown)
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001544 case OO_Minus:
Anders Carlsson8257d412009-12-22 06:36:32 +00001545 Out << (Arity == 1? "ng" : "mi"); break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001546 // ::= ad # & (unary)
John McCall5e1e89b2010-08-18 19:18:59 +00001547 // ::= an # & (binary or unknown)
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001548 case OO_Amp:
Anders Carlsson8257d412009-12-22 06:36:32 +00001549 Out << (Arity == 1? "ad" : "an"); break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001550 // ::= de # * (unary)
John McCall5e1e89b2010-08-18 19:18:59 +00001551 // ::= ml # * (binary or unknown)
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001552 case OO_Star:
John McCall5e1e89b2010-08-18 19:18:59 +00001553 // Use binary when unknown.
Anders Carlsson8257d412009-12-22 06:36:32 +00001554 Out << (Arity == 1? "de" : "ml"); break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001555 // ::= co # ~
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001556 case OO_Tilde: Out << "co"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001557 // ::= dv # /
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001558 case OO_Slash: Out << "dv"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001559 // ::= rm # %
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001560 case OO_Percent: Out << "rm"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001561 // ::= or # |
1562 case OO_Pipe: Out << "or"; break;
1563 // ::= eo # ^
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001564 case OO_Caret: Out << "eo"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001565 // ::= aS # =
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001566 case OO_Equal: Out << "aS"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001567 // ::= pL # +=
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001568 case OO_PlusEqual: Out << "pL"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001569 // ::= mI # -=
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001570 case OO_MinusEqual: Out << "mI"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001571 // ::= mL # *=
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001572 case OO_StarEqual: Out << "mL"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001573 // ::= dV # /=
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001574 case OO_SlashEqual: Out << "dV"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001575 // ::= rM # %=
1576 case OO_PercentEqual: Out << "rM"; break;
1577 // ::= aN # &=
1578 case OO_AmpEqual: Out << "aN"; break;
1579 // ::= oR # |=
1580 case OO_PipeEqual: Out << "oR"; break;
1581 // ::= eO # ^=
1582 case OO_CaretEqual: Out << "eO"; break;
1583 // ::= ls # <<
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001584 case OO_LessLess: Out << "ls"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001585 // ::= rs # >>
1586 case OO_GreaterGreater: Out << "rs"; break;
1587 // ::= lS # <<=
1588 case OO_LessLessEqual: Out << "lS"; break;
1589 // ::= rS # >>=
1590 case OO_GreaterGreaterEqual: Out << "rS"; break;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001591 // ::= eq # ==
1592 case OO_EqualEqual: Out << "eq"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001593 // ::= ne # !=
1594 case OO_ExclaimEqual: Out << "ne"; break;
1595 // ::= lt # <
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001596 case OO_Less: Out << "lt"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001597 // ::= gt # >
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001598 case OO_Greater: Out << "gt"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001599 // ::= le # <=
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001600 case OO_LessEqual: Out << "le"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001601 // ::= ge # >=
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001602 case OO_GreaterEqual: Out << "ge"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001603 // ::= nt # !
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001604 case OO_Exclaim: Out << "nt"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001605 // ::= aa # &&
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001606 case OO_AmpAmp: Out << "aa"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001607 // ::= oo # ||
1608 case OO_PipePipe: Out << "oo"; break;
1609 // ::= pp # ++
1610 case OO_PlusPlus: Out << "pp"; break;
1611 // ::= mm # --
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001612 case OO_MinusMinus: Out << "mm"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001613 // ::= cm # ,
1614 case OO_Comma: Out << "cm"; break;
1615 // ::= pm # ->*
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001616 case OO_ArrowStar: Out << "pm"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001617 // ::= pt # ->
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001618 case OO_Arrow: Out << "pt"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001619 // ::= cl # ()
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001620 case OO_Call: Out << "cl"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001621 // ::= ix # []
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001622 case OO_Subscript: Out << "ix"; break;
Anders Carlssone170ba72009-12-14 01:45:37 +00001623
1624 // ::= qu # ?
1625 // The conditional operator can't be overloaded, but we still handle it when
1626 // mangling expressions.
1627 case OO_Conditional: Out << "qu"; break;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001628
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001629 case OO_None:
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001630 case NUM_OVERLOADED_OPERATORS:
David Blaikieb219cfc2011-09-23 05:06:16 +00001631 llvm_unreachable("Not an overloaded operator");
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001632 }
1633}
1634
John McCall0953e762009-09-24 19:53:00 +00001635void CXXNameMangler::mangleQualifiers(Qualifiers Quals) {
Mike Stump1eb44332009-09-09 15:08:12 +00001636 // <CV-qualifiers> ::= [r] [V] [K] # restrict (C99), volatile, const
John McCall0953e762009-09-24 19:53:00 +00001637 if (Quals.hasRestrict())
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001638 Out << 'r';
John McCall0953e762009-09-24 19:53:00 +00001639 if (Quals.hasVolatile())
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001640 Out << 'V';
John McCall0953e762009-09-24 19:53:00 +00001641 if (Quals.hasConst())
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001642 Out << 'K';
John McCall0953e762009-09-24 19:53:00 +00001643
Douglas Gregor56079f72010-06-14 23:15:08 +00001644 if (Quals.hasAddressSpace()) {
1645 // Extension:
1646 //
1647 // <type> ::= U <address-space-number>
1648 //
1649 // where <address-space-number> is a source name consisting of 'AS'
1650 // followed by the address space <number>.
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00001651 SmallString<64> ASString;
Douglas Gregor56079f72010-06-14 23:15:08 +00001652 ASString = "AS" + llvm::utostr_32(Quals.getAddressSpace());
1653 Out << 'U' << ASString.size() << ASString;
1654 }
1655
Chris Lattner5f9e2722011-07-23 10:55:15 +00001656 StringRef LifetimeName;
John McCallf85e1932011-06-15 23:02:42 +00001657 switch (Quals.getObjCLifetime()) {
1658 // Objective-C ARC Extension:
1659 //
1660 // <type> ::= U "__strong"
1661 // <type> ::= U "__weak"
1662 // <type> ::= U "__autoreleasing"
John McCallf85e1932011-06-15 23:02:42 +00001663 case Qualifiers::OCL_None:
1664 break;
1665
1666 case Qualifiers::OCL_Weak:
1667 LifetimeName = "__weak";
1668 break;
1669
1670 case Qualifiers::OCL_Strong:
1671 LifetimeName = "__strong";
1672 break;
1673
1674 case Qualifiers::OCL_Autoreleasing:
1675 LifetimeName = "__autoreleasing";
1676 break;
1677
1678 case Qualifiers::OCL_ExplicitNone:
Douglas Gregorc22d6992011-06-17 22:26:49 +00001679 // The __unsafe_unretained qualifier is *not* mangled, so that
1680 // __unsafe_unretained types in ARC produce the same manglings as the
1681 // equivalent (but, naturally, unqualified) types in non-ARC, providing
1682 // better ABI compatibility.
1683 //
1684 // It's safe to do this because unqualified 'id' won't show up
1685 // in any type signatures that need to be mangled.
John McCallf85e1932011-06-15 23:02:42 +00001686 break;
1687 }
1688 if (!LifetimeName.empty())
1689 Out << 'U' << LifetimeName.size() << LifetimeName;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001690}
1691
Douglas Gregor0a9a6d62011-01-26 17:36:28 +00001692void CXXNameMangler::mangleRefQualifier(RefQualifierKind RefQualifier) {
1693 // <ref-qualifier> ::= R # lvalue reference
1694 // ::= O # rvalue-reference
1695 // Proposal to Itanium C++ ABI list on 1/26/11
1696 switch (RefQualifier) {
1697 case RQ_None:
1698 break;
1699
1700 case RQ_LValue:
1701 Out << 'R';
1702 break;
1703
1704 case RQ_RValue:
1705 Out << 'O';
1706 break;
1707 }
1708}
1709
Anders Carlsson7b06f6c2009-12-10 03:14:39 +00001710void CXXNameMangler::mangleObjCMethodName(const ObjCMethodDecl *MD) {
Rafael Espindolaf0be9792011-02-11 02:52:17 +00001711 Context.mangleObjCMethodName(MD, Out);
Anders Carlsson7b06f6c2009-12-10 03:14:39 +00001712}
1713
Douglas Gregorf1588662011-07-12 15:18:55 +00001714void CXXNameMangler::mangleType(QualType T) {
1715 // If our type is instantiation-dependent but not dependent, we mangle
1716 // it as it was written in the source, removing any top-level sugar.
1717 // Otherwise, use the canonical type.
1718 //
1719 // FIXME: This is an approximation of the instantiation-dependent name
1720 // mangling rules, since we should really be using the type as written and
1721 // augmented via semantic analysis (i.e., with implicit conversions and
1722 // default template arguments) for any instantiation-dependent type.
1723 // Unfortunately, that requires several changes to our AST:
1724 // - Instantiation-dependent TemplateSpecializationTypes will need to be
1725 // uniqued, so that we can handle substitutions properly
1726 // - Default template arguments will need to be represented in the
1727 // TemplateSpecializationType, since they need to be mangled even though
1728 // they aren't written.
1729 // - Conversions on non-type template arguments need to be expressed, since
1730 // they can affect the mangling of sizeof/alignof.
1731 if (!T->isInstantiationDependentType() || T->isDependentType())
1732 T = T.getCanonicalType();
1733 else {
1734 // Desugar any types that are purely sugar.
1735 do {
1736 // Don't desugar through template specialization types that aren't
1737 // type aliases. We need to mangle the template arguments as written.
1738 if (const TemplateSpecializationType *TST
1739 = dyn_cast<TemplateSpecializationType>(T))
1740 if (!TST->isTypeAlias())
1741 break;
Anders Carlsson4843e582009-03-10 17:07:44 +00001742
Douglas Gregorf1588662011-07-12 15:18:55 +00001743 QualType Desugared
1744 = T.getSingleStepDesugaredType(Context.getASTContext());
1745 if (Desugared == T)
1746 break;
1747
1748 T = Desugared;
1749 } while (true);
1750 }
1751 SplitQualType split = T.split();
John McCall200fa532012-02-08 00:46:36 +00001752 Qualifiers quals = split.Quals;
1753 const Type *ty = split.Ty;
John McCallb47f7482011-01-26 20:05:40 +00001754
Douglas Gregorf1588662011-07-12 15:18:55 +00001755 bool isSubstitutable = quals || !isa<BuiltinType>(T);
1756 if (isSubstitutable && mangleSubstitution(T))
Anders Carlsson76967372009-09-17 00:43:46 +00001757 return;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001758
John McCallb47f7482011-01-26 20:05:40 +00001759 // If we're mangling a qualified array type, push the qualifiers to
1760 // the element type.
Douglas Gregorf1588662011-07-12 15:18:55 +00001761 if (quals && isa<ArrayType>(T)) {
1762 ty = Context.getASTContext().getAsArrayType(T);
John McCallb47f7482011-01-26 20:05:40 +00001763 quals = Qualifiers();
1764
Douglas Gregorf1588662011-07-12 15:18:55 +00001765 // Note that we don't update T: we want to add the
1766 // substitution at the original type.
John McCallb47f7482011-01-26 20:05:40 +00001767 }
1768
1769 if (quals) {
1770 mangleQualifiers(quals);
John McCall0953e762009-09-24 19:53:00 +00001771 // Recurse: even if the qualified type isn't yet substitutable,
1772 // the unqualified type might be.
John McCallb47f7482011-01-26 20:05:40 +00001773 mangleType(QualType(ty, 0));
Anders Carlsson76967372009-09-17 00:43:46 +00001774 } else {
John McCallb47f7482011-01-26 20:05:40 +00001775 switch (ty->getTypeClass()) {
John McCallefe6aee2009-09-05 07:56:18 +00001776#define ABSTRACT_TYPE(CLASS, PARENT)
1777#define NON_CANONICAL_TYPE(CLASS, PARENT) \
Anders Carlsson76967372009-09-17 00:43:46 +00001778 case Type::CLASS: \
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +00001779 llvm_unreachable("can't mangle non-canonical type " #CLASS "Type"); \
Anders Carlsson76967372009-09-17 00:43:46 +00001780 return;
John McCallefe6aee2009-09-05 07:56:18 +00001781#define TYPE(CLASS, PARENT) \
Anders Carlsson76967372009-09-17 00:43:46 +00001782 case Type::CLASS: \
John McCallb47f7482011-01-26 20:05:40 +00001783 mangleType(static_cast<const CLASS##Type*>(ty)); \
Anders Carlsson76967372009-09-17 00:43:46 +00001784 break;
John McCallefe6aee2009-09-05 07:56:18 +00001785#include "clang/AST/TypeNodes.def"
Anders Carlsson76967372009-09-17 00:43:46 +00001786 }
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001787 }
Anders Carlsson76967372009-09-17 00:43:46 +00001788
1789 // Add the substitution.
John McCallb47f7482011-01-26 20:05:40 +00001790 if (isSubstitutable)
Douglas Gregorf1588662011-07-12 15:18:55 +00001791 addSubstitution(T);
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001792}
1793
Douglas Gregor1b12a3b2010-05-26 05:11:13 +00001794void CXXNameMangler::mangleNameOrStandardSubstitution(const NamedDecl *ND) {
1795 if (!mangleStandardSubstitution(ND))
1796 mangleName(ND);
1797}
1798
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001799void CXXNameMangler::mangleType(const BuiltinType *T) {
John McCallefe6aee2009-09-05 07:56:18 +00001800 // <type> ::= <builtin-type>
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001801 // <builtin-type> ::= v # void
1802 // ::= w # wchar_t
1803 // ::= b # bool
1804 // ::= c # char
1805 // ::= a # signed char
1806 // ::= h # unsigned char
1807 // ::= s # short
1808 // ::= t # unsigned short
1809 // ::= i # int
1810 // ::= j # unsigned int
1811 // ::= l # long
1812 // ::= m # unsigned long
1813 // ::= x # long long, __int64
1814 // ::= y # unsigned long long, __int64
1815 // ::= n # __int128
1816 // UNSUPPORTED: ::= o # unsigned __int128
1817 // ::= f # float
1818 // ::= d # double
1819 // ::= e # long double, __float80
1820 // UNSUPPORTED: ::= g # __float128
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001821 // UNSUPPORTED: ::= Dd # IEEE 754r decimal floating point (64 bits)
1822 // UNSUPPORTED: ::= De # IEEE 754r decimal floating point (128 bits)
1823 // UNSUPPORTED: ::= Df # IEEE 754r decimal floating point (32 bits)
Anton Korobeynikovaa4a99b2011-10-14 23:23:15 +00001824 // ::= Dh # IEEE 754r half-precision floating point (16 bits)
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001825 // ::= Di # char32_t
1826 // ::= Ds # char16_t
Anders Carlssone2923682010-11-04 04:31:32 +00001827 // ::= Dn # std::nullptr_t (i.e., decltype(nullptr))
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001828 // ::= u <source-name> # vendor extended type
1829 switch (T->getKind()) {
1830 case BuiltinType::Void: Out << 'v'; break;
1831 case BuiltinType::Bool: Out << 'b'; break;
1832 case BuiltinType::Char_U: case BuiltinType::Char_S: Out << 'c'; break;
1833 case BuiltinType::UChar: Out << 'h'; break;
1834 case BuiltinType::UShort: Out << 't'; break;
1835 case BuiltinType::UInt: Out << 'j'; break;
1836 case BuiltinType::ULong: Out << 'm'; break;
1837 case BuiltinType::ULongLong: Out << 'y'; break;
Chris Lattner2df9ced2009-04-30 02:43:43 +00001838 case BuiltinType::UInt128: Out << 'o'; break;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001839 case BuiltinType::SChar: Out << 'a'; break;
Chris Lattner3f59c972010-12-25 23:25:43 +00001840 case BuiltinType::WChar_S:
1841 case BuiltinType::WChar_U: Out << 'w'; break;
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001842 case BuiltinType::Char16: Out << "Ds"; break;
1843 case BuiltinType::Char32: Out << "Di"; break;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001844 case BuiltinType::Short: Out << 's'; break;
1845 case BuiltinType::Int: Out << 'i'; break;
1846 case BuiltinType::Long: Out << 'l'; break;
1847 case BuiltinType::LongLong: Out << 'x'; break;
Chris Lattner2df9ced2009-04-30 02:43:43 +00001848 case BuiltinType::Int128: Out << 'n'; break;
Anton Korobeynikovaa4a99b2011-10-14 23:23:15 +00001849 case BuiltinType::Half: Out << "Dh"; break;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001850 case BuiltinType::Float: Out << 'f'; break;
1851 case BuiltinType::Double: Out << 'd'; break;
1852 case BuiltinType::LongDouble: Out << 'e'; break;
Anders Carlssone2923682010-11-04 04:31:32 +00001853 case BuiltinType::NullPtr: Out << "Dn"; break;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001854
John McCalle0a22d02011-10-18 21:02:43 +00001855#define BUILTIN_TYPE(Id, SingletonId)
1856#define PLACEHOLDER_TYPE(Id, SingletonId) \
1857 case BuiltinType::Id:
1858#include "clang/AST/BuiltinTypes.def"
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001859 case BuiltinType::Dependent:
John McCallfb44de92011-05-01 22:35:37 +00001860 llvm_unreachable("mangling a placeholder type");
Steve Naroff9533a7f2009-07-22 17:14:51 +00001861 case BuiltinType::ObjCId: Out << "11objc_object"; break;
1862 case BuiltinType::ObjCClass: Out << "10objc_class"; break;
Fariborz Jahanian13dcd002009-11-21 19:53:08 +00001863 case BuiltinType::ObjCSel: Out << "13objc_selector"; break;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001864 }
1865}
1866
John McCallefe6aee2009-09-05 07:56:18 +00001867// <type> ::= <function-type>
1868// <function-type> ::= F [Y] <bare-function-type> E
1869void CXXNameMangler::mangleType(const FunctionProtoType *T) {
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001870 Out << 'F';
Mike Stumpf5408fe2009-05-16 07:57:57 +00001871 // FIXME: We don't have enough information in the AST to produce the 'Y'
1872 // encoding for extern "C" function types.
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001873 mangleBareFunctionType(T, /*MangleReturnType=*/true);
1874 Out << 'E';
1875}
John McCallefe6aee2009-09-05 07:56:18 +00001876void CXXNameMangler::mangleType(const FunctionNoProtoType *T) {
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +00001877 llvm_unreachable("Can't mangle K&R function prototypes");
John McCallefe6aee2009-09-05 07:56:18 +00001878}
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001879void CXXNameMangler::mangleBareFunctionType(const FunctionType *T,
1880 bool MangleReturnType) {
John McCallefe6aee2009-09-05 07:56:18 +00001881 // We should never be mangling something without a prototype.
1882 const FunctionProtoType *Proto = cast<FunctionProtoType>(T);
1883
John McCallfb44de92011-05-01 22:35:37 +00001884 // Record that we're in a function type. See mangleFunctionParam
1885 // for details on what we're trying to achieve here.
1886 FunctionTypeDepthState saved = FunctionTypeDepth.push();
1887
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001888 // <bare-function-type> ::= <signature type>+
John McCallfb44de92011-05-01 22:35:37 +00001889 if (MangleReturnType) {
1890 FunctionTypeDepth.enterResultType();
John McCallefe6aee2009-09-05 07:56:18 +00001891 mangleType(Proto->getResultType());
John McCallfb44de92011-05-01 22:35:37 +00001892 FunctionTypeDepth.leaveResultType();
1893 }
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001894
Anders Carlsson93296682010-06-02 04:40:13 +00001895 if (Proto->getNumArgs() == 0 && !Proto->isVariadic()) {
Eli Friedmana7e68452010-08-22 01:00:03 +00001896 // <builtin-type> ::= v # void
Anders Carlssonc6c91bc2009-04-01 00:15:23 +00001897 Out << 'v';
John McCallfb44de92011-05-01 22:35:37 +00001898
1899 FunctionTypeDepth.pop(saved);
Anders Carlssonc6c91bc2009-04-01 00:15:23 +00001900 return;
1901 }
Mike Stump1eb44332009-09-09 15:08:12 +00001902
Douglas Gregor72564e72009-02-26 23:50:07 +00001903 for (FunctionProtoType::arg_type_iterator Arg = Proto->arg_type_begin(),
Mike Stump1eb44332009-09-09 15:08:12 +00001904 ArgEnd = Proto->arg_type_end();
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001905 Arg != ArgEnd; ++Arg)
Douglas Gregor79e6bd32011-07-12 04:42:08 +00001906 mangleType(Context.getASTContext().getSignatureParameterType(*Arg));
Douglas Gregor219cc612009-02-13 01:28:03 +00001907
John McCallfb44de92011-05-01 22:35:37 +00001908 FunctionTypeDepth.pop(saved);
1909
Douglas Gregor219cc612009-02-13 01:28:03 +00001910 // <builtin-type> ::= z # ellipsis
1911 if (Proto->isVariadic())
1912 Out << 'z';
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001913}
1914
John McCallefe6aee2009-09-05 07:56:18 +00001915// <type> ::= <class-enum-type>
Mike Stump1eb44332009-09-09 15:08:12 +00001916// <class-enum-type> ::= <name>
John McCalled976492009-12-04 22:46:56 +00001917void CXXNameMangler::mangleType(const UnresolvedUsingType *T) {
1918 mangleName(T->getDecl());
1919}
1920
1921// <type> ::= <class-enum-type>
1922// <class-enum-type> ::= <name>
John McCallefe6aee2009-09-05 07:56:18 +00001923void CXXNameMangler::mangleType(const EnumType *T) {
1924 mangleType(static_cast<const TagType*>(T));
1925}
1926void CXXNameMangler::mangleType(const RecordType *T) {
1927 mangleType(static_cast<const TagType*>(T));
1928}
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001929void CXXNameMangler::mangleType(const TagType *T) {
Eli Friedmanecb7e932009-12-11 18:00:57 +00001930 mangleName(T->getDecl());
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001931}
1932
John McCallefe6aee2009-09-05 07:56:18 +00001933// <type> ::= <array-type>
1934// <array-type> ::= A <positive dimension number> _ <element type>
1935// ::= A [<dimension expression>] _ <element type>
1936void CXXNameMangler::mangleType(const ConstantArrayType *T) {
1937 Out << 'A' << T->getSize() << '_';
1938 mangleType(T->getElementType());
1939}
1940void CXXNameMangler::mangleType(const VariableArrayType *T) {
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001941 Out << 'A';
Fariborz Jahanian7281d1f2010-11-02 16:54:00 +00001942 // decayed vla types (size 0) will just be skipped.
1943 if (T->getSizeExpr())
1944 mangleExpression(T->getSizeExpr());
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001945 Out << '_';
1946 mangleType(T->getElementType());
1947}
John McCallefe6aee2009-09-05 07:56:18 +00001948void CXXNameMangler::mangleType(const DependentSizedArrayType *T) {
1949 Out << 'A';
1950 mangleExpression(T->getSizeExpr());
1951 Out << '_';
1952 mangleType(T->getElementType());
1953}
1954void CXXNameMangler::mangleType(const IncompleteArrayType *T) {
Nick Lewycky271b6652010-09-05 03:40:33 +00001955 Out << "A_";
John McCallefe6aee2009-09-05 07:56:18 +00001956 mangleType(T->getElementType());
1957}
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001958
John McCallefe6aee2009-09-05 07:56:18 +00001959// <type> ::= <pointer-to-member-type>
1960// <pointer-to-member-type> ::= M <class type> <member type>
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001961void CXXNameMangler::mangleType(const MemberPointerType *T) {
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001962 Out << 'M';
1963 mangleType(QualType(T->getClass(), 0));
Anders Carlsson0e650012009-05-17 17:41:20 +00001964 QualType PointeeType = T->getPointeeType();
1965 if (const FunctionProtoType *FPT = dyn_cast<FunctionProtoType>(PointeeType)) {
John McCall0953e762009-09-24 19:53:00 +00001966 mangleQualifiers(Qualifiers::fromCVRMask(FPT->getTypeQuals()));
Douglas Gregor0a9a6d62011-01-26 17:36:28 +00001967 mangleRefQualifier(FPT->getRefQualifier());
Anders Carlsson0e650012009-05-17 17:41:20 +00001968 mangleType(FPT);
Anders Carlsson9d85b722010-06-02 04:29:50 +00001969
1970 // Itanium C++ ABI 5.1.8:
1971 //
1972 // The type of a non-static member function is considered to be different,
1973 // for the purposes of substitution, from the type of a namespace-scope or
1974 // static member function whose type appears similar. The types of two
1975 // non-static member functions are considered to be different, for the
1976 // purposes of substitution, if the functions are members of different
1977 // classes. In other words, for the purposes of substitution, the class of
1978 // which the function is a member is considered part of the type of
1979 // function.
1980
1981 // We increment the SeqID here to emulate adding an entry to the
1982 // substitution table. We can't actually add it because we don't want this
1983 // particular function type to be substituted.
1984 ++SeqID;
Mike Stump1eb44332009-09-09 15:08:12 +00001985 } else
Anders Carlsson0e650012009-05-17 17:41:20 +00001986 mangleType(PointeeType);
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001987}
1988
John McCallefe6aee2009-09-05 07:56:18 +00001989// <type> ::= <template-param>
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001990void CXXNameMangler::mangleType(const TemplateTypeParmType *T) {
Anders Carlsson0ccdf8d2009-09-27 00:38:53 +00001991 mangleTemplateParameter(T->getIndex());
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001992}
1993
Douglas Gregorc3069d62011-01-14 02:55:32 +00001994// <type> ::= <template-param>
1995void CXXNameMangler::mangleType(const SubstTemplateTypeParmPackType *T) {
John McCall68a51a72011-07-01 00:04:39 +00001996 // FIXME: not clear how to mangle this!
1997 // template <class T...> class A {
1998 // template <class U...> void foo(T(*)(U) x...);
1999 // };
2000 Out << "_SUBSTPACK_";
Douglas Gregorc3069d62011-01-14 02:55:32 +00002001}
2002
John McCallefe6aee2009-09-05 07:56:18 +00002003// <type> ::= P <type> # pointer-to
2004void CXXNameMangler::mangleType(const PointerType *T) {
2005 Out << 'P';
2006 mangleType(T->getPointeeType());
2007}
2008void CXXNameMangler::mangleType(const ObjCObjectPointerType *T) {
2009 Out << 'P';
2010 mangleType(T->getPointeeType());
2011}
2012
2013// <type> ::= R <type> # reference-to
2014void CXXNameMangler::mangleType(const LValueReferenceType *T) {
2015 Out << 'R';
2016 mangleType(T->getPointeeType());
2017}
2018
2019// <type> ::= O <type> # rvalue reference-to (C++0x)
2020void CXXNameMangler::mangleType(const RValueReferenceType *T) {
2021 Out << 'O';
2022 mangleType(T->getPointeeType());
2023}
2024
2025// <type> ::= C <type> # complex pair (C 2000)
2026void CXXNameMangler::mangleType(const ComplexType *T) {
2027 Out << 'C';
2028 mangleType(T->getElementType());
2029}
2030
Bob Wilsonc7df92d2010-11-12 17:24:43 +00002031// ARM's ABI for Neon vector types specifies that they should be mangled as
Bob Wilson57147a82010-11-16 00:32:18 +00002032// if they are structs (to match ARM's initial implementation). The
2033// vector type must be one of the special types predefined by ARM.
2034void CXXNameMangler::mangleNeonVectorType(const VectorType *T) {
Bob Wilsonc7df92d2010-11-12 17:24:43 +00002035 QualType EltType = T->getElementType();
Bob Wilson57147a82010-11-16 00:32:18 +00002036 assert(EltType->isBuiltinType() && "Neon vector element not a BuiltinType");
Bob Wilsonc7df92d2010-11-12 17:24:43 +00002037 const char *EltName = 0;
Bob Wilson491328c2010-11-12 17:24:46 +00002038 if (T->getVectorKind() == VectorType::NeonPolyVector) {
2039 switch (cast<BuiltinType>(EltType)->getKind()) {
Bob Wilson4cfaa5d2010-11-12 17:24:49 +00002040 case BuiltinType::SChar: EltName = "poly8_t"; break;
2041 case BuiltinType::Short: EltName = "poly16_t"; break;
Bob Wilson57147a82010-11-16 00:32:18 +00002042 default: llvm_unreachable("unexpected Neon polynomial vector element type");
Bob Wilson491328c2010-11-12 17:24:46 +00002043 }
2044 } else {
2045 switch (cast<BuiltinType>(EltType)->getKind()) {
Bob Wilson4cfaa5d2010-11-12 17:24:49 +00002046 case BuiltinType::SChar: EltName = "int8_t"; break;
2047 case BuiltinType::UChar: EltName = "uint8_t"; break;
2048 case BuiltinType::Short: EltName = "int16_t"; break;
2049 case BuiltinType::UShort: EltName = "uint16_t"; break;
2050 case BuiltinType::Int: EltName = "int32_t"; break;
2051 case BuiltinType::UInt: EltName = "uint32_t"; break;
2052 case BuiltinType::LongLong: EltName = "int64_t"; break;
2053 case BuiltinType::ULongLong: EltName = "uint64_t"; break;
2054 case BuiltinType::Float: EltName = "float32_t"; break;
Bob Wilson57147a82010-11-16 00:32:18 +00002055 default: llvm_unreachable("unexpected Neon vector element type");
Bob Wilson491328c2010-11-12 17:24:46 +00002056 }
Bob Wilsonc7df92d2010-11-12 17:24:43 +00002057 }
2058 const char *BaseName = 0;
Bob Wilson4cfaa5d2010-11-12 17:24:49 +00002059 unsigned BitSize = (T->getNumElements() *
Bob Wilson3a723022010-11-16 00:32:12 +00002060 getASTContext().getTypeSize(EltType));
Bob Wilsonc7df92d2010-11-12 17:24:43 +00002061 if (BitSize == 64)
2062 BaseName = "__simd64_";
Bob Wilson57147a82010-11-16 00:32:18 +00002063 else {
2064 assert(BitSize == 128 && "Neon vector type not 64 or 128 bits");
Bob Wilsonc7df92d2010-11-12 17:24:43 +00002065 BaseName = "__simd128_";
Bob Wilson57147a82010-11-16 00:32:18 +00002066 }
Bob Wilsonc7df92d2010-11-12 17:24:43 +00002067 Out << strlen(BaseName) + strlen(EltName);
2068 Out << BaseName << EltName;
Bob Wilsonc7df92d2010-11-12 17:24:43 +00002069}
2070
John McCallefe6aee2009-09-05 07:56:18 +00002071// GNU extension: vector types
Chris Lattner788b0fd2010-06-23 06:00:24 +00002072// <type> ::= <vector-type>
2073// <vector-type> ::= Dv <positive dimension number> _
2074// <extended element type>
2075// ::= Dv [<dimension expression>] _ <element type>
2076// <extended element type> ::= <element type>
2077// ::= p # AltiVec vector pixel
John McCallefe6aee2009-09-05 07:56:18 +00002078void CXXNameMangler::mangleType(const VectorType *T) {
Bob Wilson491328c2010-11-12 17:24:46 +00002079 if ((T->getVectorKind() == VectorType::NeonVector ||
Bob Wilson57147a82010-11-16 00:32:18 +00002080 T->getVectorKind() == VectorType::NeonPolyVector)) {
2081 mangleNeonVectorType(T);
Bob Wilsonc7df92d2010-11-12 17:24:43 +00002082 return;
Bob Wilson57147a82010-11-16 00:32:18 +00002083 }
Nick Lewycky0e5f0672010-03-26 07:18:04 +00002084 Out << "Dv" << T->getNumElements() << '_';
Bob Wilsone86d78c2010-11-10 21:56:12 +00002085 if (T->getVectorKind() == VectorType::AltiVecPixel)
Chris Lattner788b0fd2010-06-23 06:00:24 +00002086 Out << 'p';
Bob Wilsone86d78c2010-11-10 21:56:12 +00002087 else if (T->getVectorKind() == VectorType::AltiVecBool)
Chris Lattner788b0fd2010-06-23 06:00:24 +00002088 Out << 'b';
2089 else
2090 mangleType(T->getElementType());
John McCallefe6aee2009-09-05 07:56:18 +00002091}
2092void CXXNameMangler::mangleType(const ExtVectorType *T) {
2093 mangleType(static_cast<const VectorType*>(T));
2094}
2095void CXXNameMangler::mangleType(const DependentSizedExtVectorType *T) {
Nick Lewycky0e5f0672010-03-26 07:18:04 +00002096 Out << "Dv";
2097 mangleExpression(T->getSizeExpr());
2098 Out << '_';
John McCallefe6aee2009-09-05 07:56:18 +00002099 mangleType(T->getElementType());
2100}
2101
Douglas Gregor7536dd52010-12-20 02:24:11 +00002102void CXXNameMangler::mangleType(const PackExpansionType *T) {
Douglas Gregor4fc48662011-01-13 16:39:34 +00002103 // <type> ::= Dp <type> # pack expansion (C++0x)
Douglas Gregor255c2692011-01-13 17:44:36 +00002104 Out << "Dp";
Douglas Gregor7536dd52010-12-20 02:24:11 +00002105 mangleType(T->getPattern());
2106}
2107
Anders Carlssona40c5e42009-03-07 22:03:21 +00002108void CXXNameMangler::mangleType(const ObjCInterfaceType *T) {
2109 mangleSourceName(T->getDecl()->getIdentifier());
2110}
2111
John McCallc12c5bb2010-05-15 11:32:37 +00002112void CXXNameMangler::mangleType(const ObjCObjectType *T) {
John McCallc00c1f62010-05-15 17:06:29 +00002113 // We don't allow overloading by different protocol qualification,
2114 // so mangling them isn't necessary.
John McCallc12c5bb2010-05-15 11:32:37 +00002115 mangleType(T->getBaseType());
2116}
2117
John McCallefe6aee2009-09-05 07:56:18 +00002118void CXXNameMangler::mangleType(const BlockPointerType *T) {
Anders Carlssonf28c6872009-12-23 22:31:44 +00002119 Out << "U13block_pointer";
2120 mangleType(T->getPointeeType());
John McCallefe6aee2009-09-05 07:56:18 +00002121}
2122
John McCall31f17ec2010-04-27 00:57:59 +00002123void CXXNameMangler::mangleType(const InjectedClassNameType *T) {
2124 // Mangle injected class name types as if the user had written the
2125 // specialization out fully. It may not actually be possible to see
2126 // this mangling, though.
2127 mangleType(T->getInjectedSpecializationType());
2128}
2129
John McCallefe6aee2009-09-05 07:56:18 +00002130void CXXNameMangler::mangleType(const TemplateSpecializationType *T) {
Douglas Gregor1e9268e2010-04-28 05:58:56 +00002131 if (TemplateDecl *TD = T->getTemplateName().getAsTemplateDecl()) {
2132 mangleName(TD, T->getArgs(), T->getNumArgs());
2133 } else {
2134 if (mangleSubstitution(QualType(T, 0)))
2135 return;
Sean Huntc3021132010-05-05 15:23:54 +00002136
Douglas Gregor1e9268e2010-04-28 05:58:56 +00002137 mangleTemplatePrefix(T->getTemplateName());
Sean Huntc3021132010-05-05 15:23:54 +00002138
Douglas Gregor1e9268e2010-04-28 05:58:56 +00002139 // FIXME: GCC does not appear to mangle the template arguments when
2140 // the template in question is a dependent template name. Should we
2141 // emulate that badness?
2142 mangleTemplateArgs(T->getTemplateName(), T->getArgs(), T->getNumArgs());
2143 addSubstitution(QualType(T, 0));
2144 }
John McCallefe6aee2009-09-05 07:56:18 +00002145}
2146
Douglas Gregor4714c122010-03-31 17:34:00 +00002147void CXXNameMangler::mangleType(const DependentNameType *T) {
Anders Carlssonae352482009-09-26 02:26:02 +00002148 // Typename types are always nested
2149 Out << 'N';
John McCalla0ce15c2011-04-24 08:23:24 +00002150 manglePrefix(T->getQualifier());
John McCall33500952010-06-11 00:33:02 +00002151 mangleSourceName(T->getIdentifier());
2152 Out << 'E';
2153}
John McCall6ab30e02010-06-09 07:26:17 +00002154
John McCall33500952010-06-11 00:33:02 +00002155void CXXNameMangler::mangleType(const DependentTemplateSpecializationType *T) {
Douglas Gregoraa2187d2011-02-28 00:04:36 +00002156 // Dependently-scoped template types are nested if they have a prefix.
John McCall33500952010-06-11 00:33:02 +00002157 Out << 'N';
2158
2159 // TODO: avoid making this TemplateName.
2160 TemplateName Prefix =
2161 getASTContext().getDependentTemplateName(T->getQualifier(),
2162 T->getIdentifier());
2163 mangleTemplatePrefix(Prefix);
2164
2165 // FIXME: GCC does not appear to mangle the template arguments when
2166 // the template in question is a dependent template name. Should we
2167 // emulate that badness?
2168 mangleTemplateArgs(Prefix, T->getArgs(), T->getNumArgs());
Anders Carlssonae352482009-09-26 02:26:02 +00002169 Out << 'E';
John McCallefe6aee2009-09-05 07:56:18 +00002170}
2171
John McCallad5e7382010-03-01 23:49:17 +00002172void CXXNameMangler::mangleType(const TypeOfType *T) {
2173 // FIXME: this is pretty unsatisfactory, but there isn't an obvious
2174 // "extension with parameters" mangling.
2175 Out << "u6typeof";
2176}
2177
2178void CXXNameMangler::mangleType(const TypeOfExprType *T) {
2179 // FIXME: this is pretty unsatisfactory, but there isn't an obvious
2180 // "extension with parameters" mangling.
2181 Out << "u6typeof";
2182}
2183
2184void CXXNameMangler::mangleType(const DecltypeType *T) {
2185 Expr *E = T->getUnderlyingExpr();
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002186
John McCallad5e7382010-03-01 23:49:17 +00002187 // type ::= Dt <expression> E # decltype of an id-expression
2188 // # or class member access
2189 // ::= DT <expression> E # decltype of an expression
2190
2191 // This purports to be an exhaustive list of id-expressions and
2192 // class member accesses. Note that we do not ignore parentheses;
2193 // parentheses change the semantics of decltype for these
2194 // expressions (and cause the mangler to use the other form).
2195 if (isa<DeclRefExpr>(E) ||
2196 isa<MemberExpr>(E) ||
2197 isa<UnresolvedLookupExpr>(E) ||
2198 isa<DependentScopeDeclRefExpr>(E) ||
2199 isa<CXXDependentScopeMemberExpr>(E) ||
2200 isa<UnresolvedMemberExpr>(E))
2201 Out << "Dt";
2202 else
2203 Out << "DT";
2204 mangleExpression(E);
2205 Out << 'E';
2206}
2207
Sean Huntca63c202011-05-24 22:41:36 +00002208void CXXNameMangler::mangleType(const UnaryTransformType *T) {
2209 // If this is dependent, we need to record that. If not, we simply
2210 // mangle it as the underlying type since they are equivalent.
2211 if (T->isDependentType()) {
2212 Out << 'U';
2213
2214 switch (T->getUTTKind()) {
2215 case UnaryTransformType::EnumUnderlyingType:
2216 Out << "3eut";
2217 break;
2218 }
2219 }
2220
2221 mangleType(T->getUnderlyingType());
2222}
2223
Richard Smith34b41d92011-02-20 03:19:35 +00002224void CXXNameMangler::mangleType(const AutoType *T) {
2225 QualType D = T->getDeducedType();
Richard Smith967ecd32011-02-21 20:10:02 +00002226 // <builtin-type> ::= Da # dependent auto
2227 if (D.isNull())
2228 Out << "Da";
2229 else
2230 mangleType(D);
Richard Smith34b41d92011-02-20 03:19:35 +00002231}
2232
Eli Friedmanb001de72011-10-06 23:00:33 +00002233void CXXNameMangler::mangleType(const AtomicType *T) {
2234 // <type> ::= U <source-name> <type> # vendor extended type qualifier
2235 // (Until there's a standardized mangling...)
2236 Out << "U7_Atomic";
2237 mangleType(T->getValueType());
2238}
2239
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002240void CXXNameMangler::mangleIntegerLiteral(QualType T,
Anders Carlssone170ba72009-12-14 01:45:37 +00002241 const llvm::APSInt &Value) {
2242 // <expr-primary> ::= L <type> <value number> E # integer literal
2243 Out << 'L';
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002244
Anders Carlssone170ba72009-12-14 01:45:37 +00002245 mangleType(T);
2246 if (T->isBooleanType()) {
2247 // Boolean values are encoded as 0/1.
2248 Out << (Value.getBoolValue() ? '1' : '0');
2249 } else {
John McCall0512e482010-07-14 04:20:34 +00002250 mangleNumber(Value);
Anders Carlssone170ba72009-12-14 01:45:37 +00002251 }
2252 Out << 'E';
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002253
Anders Carlssone170ba72009-12-14 01:45:37 +00002254}
2255
John McCall2f27bf82010-02-04 02:56:29 +00002256/// Mangles a member expression. Implicit accesses are not handled,
2257/// but that should be okay, because you shouldn't be able to
2258/// make an implicit access in a function template declaration.
John McCalla0ce15c2011-04-24 08:23:24 +00002259void CXXNameMangler::mangleMemberExpr(const Expr *base,
2260 bool isArrow,
2261 NestedNameSpecifier *qualifier,
2262 NamedDecl *firstQualifierLookup,
2263 DeclarationName member,
2264 unsigned arity) {
2265 // <expression> ::= dt <expression> <unresolved-name>
2266 // ::= pt <expression> <unresolved-name>
2267 Out << (isArrow ? "pt" : "dt");
2268 mangleExpression(base);
2269 mangleUnresolvedName(qualifier, firstQualifierLookup, member, arity);
John McCall2f27bf82010-02-04 02:56:29 +00002270}
2271
John McCall5a7e6f72011-04-28 02:52:03 +00002272/// Look at the callee of the given call expression and determine if
2273/// it's a parenthesized id-expression which would have triggered ADL
2274/// otherwise.
2275static bool isParenthesizedADLCallee(const CallExpr *call) {
2276 const Expr *callee = call->getCallee();
2277 const Expr *fn = callee->IgnoreParens();
2278
2279 // Must be parenthesized. IgnoreParens() skips __extension__ nodes,
2280 // too, but for those to appear in the callee, it would have to be
2281 // parenthesized.
2282 if (callee == fn) return false;
2283
2284 // Must be an unresolved lookup.
2285 const UnresolvedLookupExpr *lookup = dyn_cast<UnresolvedLookupExpr>(fn);
2286 if (!lookup) return false;
2287
2288 assert(!lookup->requiresADL());
2289
2290 // Must be an unqualified lookup.
2291 if (lookup->getQualifier()) return false;
2292
2293 // Must not have found a class member. Note that if one is a class
2294 // member, they're all class members.
2295 if (lookup->getNumDecls() > 0 &&
2296 (*lookup->decls_begin())->isCXXClassMember())
2297 return false;
2298
2299 // Otherwise, ADL would have been triggered.
2300 return true;
2301}
2302
John McCall5e1e89b2010-08-18 19:18:59 +00002303void CXXNameMangler::mangleExpression(const Expr *E, unsigned Arity) {
Anders Carlssond553f8c2009-09-21 01:21:10 +00002304 // <expression> ::= <unary operator-name> <expression>
John McCall09cc1412010-02-03 00:55:45 +00002305 // ::= <binary operator-name> <expression> <expression>
2306 // ::= <trinary operator-name> <expression> <expression> <expression>
Anders Carlssond553f8c2009-09-21 01:21:10 +00002307 // ::= cv <type> expression # conversion with one argument
2308 // ::= cv <type> _ <expression>* E # conversion with a different number of arguments
Eli Friedmana7e68452010-08-22 01:00:03 +00002309 // ::= st <type> # sizeof (a type)
Anders Carlssond553f8c2009-09-21 01:21:10 +00002310 // ::= at <type> # alignof (a type)
2311 // ::= <template-param>
2312 // ::= <function-param>
2313 // ::= sr <type> <unqualified-name> # dependent name
2314 // ::= sr <type> <unqualified-name> <template-args> # dependent template-id
Douglas Gregor63f62df2011-06-05 05:27:58 +00002315 // ::= ds <expression> <expression> # expr.*expr
Anders Carlssond553f8c2009-09-21 01:21:10 +00002316 // ::= sZ <template-param> # size of a parameter pack
Douglas Gregor4fc48662011-01-13 16:39:34 +00002317 // ::= sZ <function-param> # size of a function parameter pack
John McCall09cc1412010-02-03 00:55:45 +00002318 // ::= <expr-primary>
John McCall1dd73832010-02-04 01:42:13 +00002319 // <expr-primary> ::= L <type> <value number> E # integer literal
2320 // ::= L <type <value float> E # floating literal
2321 // ::= L <mangled-name> E # external name
Douglas Gregoredee94b2011-07-12 04:47:20 +00002322 QualType ImplicitlyConvertedToType;
2323
2324recurse:
Anders Carlssond553f8c2009-09-21 01:21:10 +00002325 switch (E->getStmtClass()) {
John McCall6ae1f352010-04-09 22:26:14 +00002326 case Expr::NoStmtClass:
John McCall63c00d72011-02-09 08:16:59 +00002327#define ABSTRACT_STMT(Type)
John McCall6ae1f352010-04-09 22:26:14 +00002328#define EXPR(Type, Base)
2329#define STMT(Type, Base) \
2330 case Expr::Type##Class:
Sean Hunt4bfe1962010-05-05 15:24:00 +00002331#include "clang/AST/StmtNodes.inc"
John McCall0512e482010-07-14 04:20:34 +00002332 // fallthrough
2333
2334 // These all can only appear in local or variable-initialization
2335 // contexts and so should never appear in a mangling.
2336 case Expr::AddrLabelExprClass:
2337 case Expr::BlockDeclRefExprClass:
2338 case Expr::CXXThisExprClass:
2339 case Expr::DesignatedInitExprClass:
2340 case Expr::ImplicitValueInitExprClass:
2341 case Expr::InitListExprClass:
2342 case Expr::ParenListExprClass:
Douglas Gregor01d08012012-02-07 10:09:13 +00002343 case Expr::LambdaExprClass:
John McCall09cc1412010-02-03 00:55:45 +00002344 llvm_unreachable("unexpected statement kind");
John McCall09cc1412010-02-03 00:55:45 +00002345
John McCall0512e482010-07-14 04:20:34 +00002346 // FIXME: invent manglings for all these.
2347 case Expr::BlockExprClass:
2348 case Expr::CXXPseudoDestructorExprClass:
2349 case Expr::ChooseExprClass:
2350 case Expr::CompoundLiteralExprClass:
2351 case Expr::ExtVectorElementExprClass:
Peter Collingbournef111d932011-04-15 00:35:48 +00002352 case Expr::GenericSelectionExprClass:
John McCall0512e482010-07-14 04:20:34 +00002353 case Expr::ObjCEncodeExprClass:
John McCall0512e482010-07-14 04:20:34 +00002354 case Expr::ObjCIsaExprClass:
2355 case Expr::ObjCIvarRefExprClass:
2356 case Expr::ObjCMessageExprClass:
2357 case Expr::ObjCPropertyRefExprClass:
2358 case Expr::ObjCProtocolExprClass:
2359 case Expr::ObjCSelectorExprClass:
2360 case Expr::ObjCStringLiteralClass:
John McCallf85e1932011-06-15 23:02:42 +00002361 case Expr::ObjCIndirectCopyRestoreExprClass:
John McCall0512e482010-07-14 04:20:34 +00002362 case Expr::OffsetOfExprClass:
2363 case Expr::PredefinedExprClass:
2364 case Expr::ShuffleVectorExprClass:
2365 case Expr::StmtExprClass:
John McCall0512e482010-07-14 04:20:34 +00002366 case Expr::UnaryTypeTraitExprClass:
Francois Pichet6ad6f282010-12-07 00:08:36 +00002367 case Expr::BinaryTypeTraitExprClass:
John Wiegley21ff2e52011-04-28 00:16:57 +00002368 case Expr::ArrayTypeTraitExprClass:
John Wiegley55262202011-04-25 06:54:41 +00002369 case Expr::ExpressionTraitExprClass:
Francois Pichet9be88402010-09-08 23:47:05 +00002370 case Expr::VAArgExprClass:
Sebastian Redl2e156222010-09-10 20:55:43 +00002371 case Expr::CXXUuidofExprClass:
Peter Collingbournee08ce652011-02-09 21:07:24 +00002372 case Expr::CXXNoexceptExprClass:
Tanya Lattner61eee0c2011-06-04 00:47:47 +00002373 case Expr::CUDAKernelCallExprClass:
2374 case Expr::AsTypeExprClass:
John McCall4b9c2d22011-11-06 09:01:30 +00002375 case Expr::PseudoObjectExprClass:
Eli Friedman276b0612011-10-11 02:20:01 +00002376 case Expr::AtomicExprClass:
Tanya Lattner61eee0c2011-06-04 00:47:47 +00002377 {
John McCall6ae1f352010-04-09 22:26:14 +00002378 // As bad as this diagnostic is, it's better than crashing.
David Blaikied6471f72011-09-25 23:23:43 +00002379 DiagnosticsEngine &Diags = Context.getDiags();
2380 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
John McCall6ae1f352010-04-09 22:26:14 +00002381 "cannot yet mangle expression type %0");
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +00002382 Diags.Report(E->getExprLoc(), DiagID)
John McCall739bf092010-04-10 09:39:25 +00002383 << E->getStmtClassName() << E->getSourceRange();
John McCall6ae1f352010-04-09 22:26:14 +00002384 break;
2385 }
2386
John McCall56ca35d2011-02-17 10:25:35 +00002387 // Even gcc-4.5 doesn't mangle this.
2388 case Expr::BinaryConditionalOperatorClass: {
David Blaikied6471f72011-09-25 23:23:43 +00002389 DiagnosticsEngine &Diags = Context.getDiags();
John McCall56ca35d2011-02-17 10:25:35 +00002390 unsigned DiagID =
David Blaikied6471f72011-09-25 23:23:43 +00002391 Diags.getCustomDiagID(DiagnosticsEngine::Error,
John McCall56ca35d2011-02-17 10:25:35 +00002392 "?: operator with omitted middle operand cannot be mangled");
2393 Diags.Report(E->getExprLoc(), DiagID)
2394 << E->getStmtClassName() << E->getSourceRange();
2395 break;
2396 }
2397
2398 // These are used for internal purposes and cannot be meaningfully mangled.
John McCall7cd7d1a2010-11-15 23:31:06 +00002399 case Expr::OpaqueValueExprClass:
2400 llvm_unreachable("cannot mangle opaque value; mangling wrong thing?");
2401
John McCall0512e482010-07-14 04:20:34 +00002402 case Expr::CXXDefaultArgExprClass:
John McCall5e1e89b2010-08-18 19:18:59 +00002403 mangleExpression(cast<CXXDefaultArgExpr>(E)->getExpr(), Arity);
John McCall0512e482010-07-14 04:20:34 +00002404 break;
2405
John McCall91a57552011-07-15 05:09:51 +00002406 case Expr::SubstNonTypeTemplateParmExprClass:
2407 mangleExpression(cast<SubstNonTypeTemplateParmExpr>(E)->getReplacement(),
2408 Arity);
2409 break;
2410
John McCall0512e482010-07-14 04:20:34 +00002411 case Expr::CXXMemberCallExprClass: // fallthrough
John McCall1dd73832010-02-04 01:42:13 +00002412 case Expr::CallExprClass: {
2413 const CallExpr *CE = cast<CallExpr>(E);
John McCall5a7e6f72011-04-28 02:52:03 +00002414
2415 // <expression> ::= cp <simple-id> <expression>* E
2416 // We use this mangling only when the call would use ADL except
2417 // for being parenthesized. Per discussion with David
2418 // Vandervoorde, 2011.04.25.
2419 if (isParenthesizedADLCallee(CE)) {
2420 Out << "cp";
2421 // The callee here is a parenthesized UnresolvedLookupExpr with
2422 // no qualifier and should always get mangled as a <simple-id>
2423 // anyway.
2424
2425 // <expression> ::= cl <expression>* E
2426 } else {
2427 Out << "cl";
2428 }
2429
John McCall5e1e89b2010-08-18 19:18:59 +00002430 mangleExpression(CE->getCallee(), CE->getNumArgs());
John McCall1dd73832010-02-04 01:42:13 +00002431 for (unsigned I = 0, N = CE->getNumArgs(); I != N; ++I)
2432 mangleExpression(CE->getArg(I));
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002433 Out << 'E';
John McCall09cc1412010-02-03 00:55:45 +00002434 break;
John McCall1dd73832010-02-04 01:42:13 +00002435 }
John McCall09cc1412010-02-03 00:55:45 +00002436
John McCall0512e482010-07-14 04:20:34 +00002437 case Expr::CXXNewExprClass: {
2438 // Proposal from David Vandervoorde, 2010.06.30
2439 const CXXNewExpr *New = cast<CXXNewExpr>(E);
2440 if (New->isGlobalNew()) Out << "gs";
2441 Out << (New->isArray() ? "na" : "nw");
2442 for (CXXNewExpr::const_arg_iterator I = New->placement_arg_begin(),
2443 E = New->placement_arg_end(); I != E; ++I)
2444 mangleExpression(*I);
2445 Out << '_';
2446 mangleType(New->getAllocatedType());
2447 if (New->hasInitializer()) {
Sebastian Redl2aed8b82012-02-16 12:22:20 +00002448 // FIXME: Does this mean "parenthesized initializer"?
John McCall0512e482010-07-14 04:20:34 +00002449 Out << "pi";
Sebastian Redl2aed8b82012-02-16 12:22:20 +00002450 const Expr *Init = New->getInitializer();
2451 if (const CXXConstructExpr *CCE = dyn_cast<CXXConstructExpr>(Init)) {
2452 // Directly inline the initializers.
2453 for (CXXConstructExpr::const_arg_iterator I = CCE->arg_begin(),
2454 E = CCE->arg_end();
2455 I != E; ++I)
2456 mangleExpression(*I);
2457 } else if (const ParenListExpr *PLE = dyn_cast<ParenListExpr>(Init)) {
2458 for (unsigned i = 0, e = PLE->getNumExprs(); i != e; ++i)
2459 mangleExpression(PLE->getExpr(i));
2460 } else
2461 mangleExpression(Init);
John McCall0512e482010-07-14 04:20:34 +00002462 }
2463 Out << 'E';
2464 break;
2465 }
2466
John McCall2f27bf82010-02-04 02:56:29 +00002467 case Expr::MemberExprClass: {
2468 const MemberExpr *ME = cast<MemberExpr>(E);
2469 mangleMemberExpr(ME->getBase(), ME->isArrow(),
John McCalla0ce15c2011-04-24 08:23:24 +00002470 ME->getQualifier(), 0, ME->getMemberDecl()->getDeclName(),
John McCall5e1e89b2010-08-18 19:18:59 +00002471 Arity);
John McCall2f27bf82010-02-04 02:56:29 +00002472 break;
2473 }
2474
2475 case Expr::UnresolvedMemberExprClass: {
2476 const UnresolvedMemberExpr *ME = cast<UnresolvedMemberExpr>(E);
2477 mangleMemberExpr(ME->getBase(), ME->isArrow(),
John McCalla0ce15c2011-04-24 08:23:24 +00002478 ME->getQualifier(), 0, ME->getMemberName(),
John McCall5e1e89b2010-08-18 19:18:59 +00002479 Arity);
John McCall6dbce192010-08-20 00:17:19 +00002480 if (ME->hasExplicitTemplateArgs())
2481 mangleTemplateArgs(ME->getExplicitTemplateArgs());
John McCall2f27bf82010-02-04 02:56:29 +00002482 break;
2483 }
2484
2485 case Expr::CXXDependentScopeMemberExprClass: {
2486 const CXXDependentScopeMemberExpr *ME
2487 = cast<CXXDependentScopeMemberExpr>(E);
2488 mangleMemberExpr(ME->getBase(), ME->isArrow(),
John McCalla0ce15c2011-04-24 08:23:24 +00002489 ME->getQualifier(), ME->getFirstQualifierFoundInScope(),
2490 ME->getMember(), Arity);
John McCall6dbce192010-08-20 00:17:19 +00002491 if (ME->hasExplicitTemplateArgs())
2492 mangleTemplateArgs(ME->getExplicitTemplateArgs());
John McCall2f27bf82010-02-04 02:56:29 +00002493 break;
2494 }
2495
John McCall1dd73832010-02-04 01:42:13 +00002496 case Expr::UnresolvedLookupExprClass: {
2497 const UnresolvedLookupExpr *ULE = cast<UnresolvedLookupExpr>(E);
John McCalla0ce15c2011-04-24 08:23:24 +00002498 mangleUnresolvedName(ULE->getQualifier(), 0, ULE->getName(), Arity);
John McCall26a6ec72011-06-21 22:12:46 +00002499
2500 // All the <unresolved-name> productions end in a
2501 // base-unresolved-name, where <template-args> are just tacked
2502 // onto the end.
John McCall6dbce192010-08-20 00:17:19 +00002503 if (ULE->hasExplicitTemplateArgs())
2504 mangleTemplateArgs(ULE->getExplicitTemplateArgs());
John McCall1dd73832010-02-04 01:42:13 +00002505 break;
2506 }
2507
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002508 case Expr::CXXUnresolvedConstructExprClass: {
John McCall1dd73832010-02-04 01:42:13 +00002509 const CXXUnresolvedConstructExpr *CE = cast<CXXUnresolvedConstructExpr>(E);
2510 unsigned N = CE->arg_size();
2511
2512 Out << "cv";
2513 mangleType(CE->getType());
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002514 if (N != 1) Out << '_';
John McCall1dd73832010-02-04 01:42:13 +00002515 for (unsigned I = 0; I != N; ++I) mangleExpression(CE->getArg(I));
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002516 if (N != 1) Out << 'E';
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002517 break;
John McCall1dd73832010-02-04 01:42:13 +00002518 }
John McCall09cc1412010-02-03 00:55:45 +00002519
John McCall1dd73832010-02-04 01:42:13 +00002520 case Expr::CXXTemporaryObjectExprClass:
2521 case Expr::CXXConstructExprClass: {
2522 const CXXConstructExpr *CE = cast<CXXConstructExpr>(E);
2523 unsigned N = CE->getNumArgs();
2524
2525 Out << "cv";
2526 mangleType(CE->getType());
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002527 if (N != 1) Out << '_';
John McCall1dd73832010-02-04 01:42:13 +00002528 for (unsigned I = 0; I != N; ++I) mangleExpression(CE->getArg(I));
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002529 if (N != 1) Out << 'E';
John McCall09cc1412010-02-03 00:55:45 +00002530 break;
John McCall1dd73832010-02-04 01:42:13 +00002531 }
2532
Richard Smith41576d42012-02-06 02:54:51 +00002533 case Expr::CXXScalarValueInitExprClass:
2534 Out <<"cv";
2535 mangleType(E->getType());
2536 Out <<"_E";
2537 break;
2538
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +00002539 case Expr::UnaryExprOrTypeTraitExprClass: {
2540 const UnaryExprOrTypeTraitExpr *SAE = cast<UnaryExprOrTypeTraitExpr>(E);
Douglas Gregoredee94b2011-07-12 04:47:20 +00002541
2542 if (!SAE->isInstantiationDependent()) {
2543 // Itanium C++ ABI:
2544 // If the operand of a sizeof or alignof operator is not
2545 // instantiation-dependent it is encoded as an integer literal
2546 // reflecting the result of the operator.
2547 //
2548 // If the result of the operator is implicitly converted to a known
2549 // integer type, that type is used for the literal; otherwise, the type
2550 // of std::size_t or std::ptrdiff_t is used.
2551 QualType T = (ImplicitlyConvertedToType.isNull() ||
2552 !ImplicitlyConvertedToType->isIntegerType())? SAE->getType()
2553 : ImplicitlyConvertedToType;
Richard Smitha6b8b2c2011-10-10 18:28:20 +00002554 llvm::APSInt V = SAE->EvaluateKnownConstInt(Context.getASTContext());
2555 mangleIntegerLiteral(T, V);
Douglas Gregoredee94b2011-07-12 04:47:20 +00002556 break;
2557 }
2558
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +00002559 switch(SAE->getKind()) {
2560 case UETT_SizeOf:
2561 Out << 's';
2562 break;
2563 case UETT_AlignOf:
2564 Out << 'a';
2565 break;
2566 case UETT_VecStep:
David Blaikied6471f72011-09-25 23:23:43 +00002567 DiagnosticsEngine &Diags = Context.getDiags();
2568 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +00002569 "cannot yet mangle vec_step expression");
2570 Diags.Report(DiagID);
2571 return;
2572 }
John McCall1dd73832010-02-04 01:42:13 +00002573 if (SAE->isArgumentType()) {
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002574 Out << 't';
John McCall1dd73832010-02-04 01:42:13 +00002575 mangleType(SAE->getArgumentType());
2576 } else {
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002577 Out << 'z';
John McCall1dd73832010-02-04 01:42:13 +00002578 mangleExpression(SAE->getArgumentExpr());
2579 }
2580 break;
2581 }
Anders Carlssona7694082009-11-06 02:50:19 +00002582
John McCall0512e482010-07-14 04:20:34 +00002583 case Expr::CXXThrowExprClass: {
2584 const CXXThrowExpr *TE = cast<CXXThrowExpr>(E);
2585
2586 // Proposal from David Vandervoorde, 2010.06.30
2587 if (TE->getSubExpr()) {
2588 Out << "tw";
2589 mangleExpression(TE->getSubExpr());
2590 } else {
2591 Out << "tr";
2592 }
2593 break;
2594 }
2595
2596 case Expr::CXXTypeidExprClass: {
2597 const CXXTypeidExpr *TIE = cast<CXXTypeidExpr>(E);
2598
2599 // Proposal from David Vandervoorde, 2010.06.30
2600 if (TIE->isTypeOperand()) {
2601 Out << "ti";
2602 mangleType(TIE->getTypeOperand());
2603 } else {
2604 Out << "te";
2605 mangleExpression(TIE->getExprOperand());
2606 }
2607 break;
2608 }
2609
2610 case Expr::CXXDeleteExprClass: {
2611 const CXXDeleteExpr *DE = cast<CXXDeleteExpr>(E);
2612
2613 // Proposal from David Vandervoorde, 2010.06.30
2614 if (DE->isGlobalDelete()) Out << "gs";
2615 Out << (DE->isArrayForm() ? "da" : "dl");
2616 mangleExpression(DE->getArgument());
2617 break;
2618 }
2619
Anders Carlssone170ba72009-12-14 01:45:37 +00002620 case Expr::UnaryOperatorClass: {
2621 const UnaryOperator *UO = cast<UnaryOperator>(E);
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002622 mangleOperatorName(UnaryOperator::getOverloadedOperator(UO->getOpcode()),
Anders Carlssone170ba72009-12-14 01:45:37 +00002623 /*Arity=*/1);
2624 mangleExpression(UO->getSubExpr());
2625 break;
2626 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002627
John McCall0512e482010-07-14 04:20:34 +00002628 case Expr::ArraySubscriptExprClass: {
2629 const ArraySubscriptExpr *AE = cast<ArraySubscriptExpr>(E);
2630
Chris Lattnerfc8f0e12011-04-15 05:22:18 +00002631 // Array subscript is treated as a syntactically weird form of
John McCall0512e482010-07-14 04:20:34 +00002632 // binary operator.
2633 Out << "ix";
2634 mangleExpression(AE->getLHS());
2635 mangleExpression(AE->getRHS());
2636 break;
2637 }
2638
2639 case Expr::CompoundAssignOperatorClass: // fallthrough
Anders Carlssone170ba72009-12-14 01:45:37 +00002640 case Expr::BinaryOperatorClass: {
2641 const BinaryOperator *BO = cast<BinaryOperator>(E);
Douglas Gregor63f62df2011-06-05 05:27:58 +00002642 if (BO->getOpcode() == BO_PtrMemD)
2643 Out << "ds";
2644 else
2645 mangleOperatorName(BinaryOperator::getOverloadedOperator(BO->getOpcode()),
2646 /*Arity=*/2);
Anders Carlssone170ba72009-12-14 01:45:37 +00002647 mangleExpression(BO->getLHS());
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002648 mangleExpression(BO->getRHS());
Anders Carlssone170ba72009-12-14 01:45:37 +00002649 break;
John McCall2f27bf82010-02-04 02:56:29 +00002650 }
Anders Carlssone170ba72009-12-14 01:45:37 +00002651
2652 case Expr::ConditionalOperatorClass: {
2653 const ConditionalOperator *CO = cast<ConditionalOperator>(E);
2654 mangleOperatorName(OO_Conditional, /*Arity=*/3);
2655 mangleExpression(CO->getCond());
John McCall5e1e89b2010-08-18 19:18:59 +00002656 mangleExpression(CO->getLHS(), Arity);
2657 mangleExpression(CO->getRHS(), Arity);
Anders Carlssone170ba72009-12-14 01:45:37 +00002658 break;
2659 }
2660
Douglas Gregor46287c72010-01-29 16:37:09 +00002661 case Expr::ImplicitCastExprClass: {
Douglas Gregoredee94b2011-07-12 04:47:20 +00002662 ImplicitlyConvertedToType = E->getType();
2663 E = cast<ImplicitCastExpr>(E)->getSubExpr();
2664 goto recurse;
Douglas Gregor46287c72010-01-29 16:37:09 +00002665 }
John McCallf85e1932011-06-15 23:02:42 +00002666
2667 case Expr::ObjCBridgedCastExprClass: {
2668 // Mangle ownership casts as a vendor extended operator __bridge,
2669 // __bridge_transfer, or __bridge_retain.
Chris Lattner5f9e2722011-07-23 10:55:15 +00002670 StringRef Kind = cast<ObjCBridgedCastExpr>(E)->getBridgeKindName();
John McCallf85e1932011-06-15 23:02:42 +00002671 Out << "v1U" << Kind.size() << Kind;
2672 }
2673 // Fall through to mangle the cast itself.
2674
Douglas Gregor46287c72010-01-29 16:37:09 +00002675 case Expr::CStyleCastExprClass:
2676 case Expr::CXXStaticCastExprClass:
2677 case Expr::CXXDynamicCastExprClass:
2678 case Expr::CXXReinterpretCastExprClass:
2679 case Expr::CXXConstCastExprClass:
2680 case Expr::CXXFunctionalCastExprClass: {
2681 const ExplicitCastExpr *ECE = cast<ExplicitCastExpr>(E);
2682 Out << "cv";
2683 mangleType(ECE->getType());
2684 mangleExpression(ECE->getSubExpr());
2685 break;
2686 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002687
Anders Carlsson58040a52009-12-16 05:48:46 +00002688 case Expr::CXXOperatorCallExprClass: {
2689 const CXXOperatorCallExpr *CE = cast<CXXOperatorCallExpr>(E);
2690 unsigned NumArgs = CE->getNumArgs();
2691 mangleOperatorName(CE->getOperator(), /*Arity=*/NumArgs);
2692 // Mangle the arguments.
2693 for (unsigned i = 0; i != NumArgs; ++i)
2694 mangleExpression(CE->getArg(i));
2695 break;
2696 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002697
Anders Carlssona7694082009-11-06 02:50:19 +00002698 case Expr::ParenExprClass:
John McCall5e1e89b2010-08-18 19:18:59 +00002699 mangleExpression(cast<ParenExpr>(E)->getSubExpr(), Arity);
Anders Carlssona7694082009-11-06 02:50:19 +00002700 break;
2701
Anders Carlssond553f8c2009-09-21 01:21:10 +00002702 case Expr::DeclRefExprClass: {
Douglas Gregor5ed1bc32010-02-28 21:40:32 +00002703 const NamedDecl *D = cast<DeclRefExpr>(E)->getDecl();
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002704
Anders Carlssond553f8c2009-09-21 01:21:10 +00002705 switch (D->getKind()) {
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002706 default:
Douglas Gregor5ed1bc32010-02-28 21:40:32 +00002707 // <expr-primary> ::= L <mangled-name> E # external name
2708 Out << 'L';
2709 mangle(D, "_Z");
2710 Out << 'E';
2711 break;
2712
John McCallfb44de92011-05-01 22:35:37 +00002713 case Decl::ParmVar:
2714 mangleFunctionParam(cast<ParmVarDecl>(D));
2715 break;
2716
John McCall3dc7e7b2010-07-24 01:17:35 +00002717 case Decl::EnumConstant: {
2718 const EnumConstantDecl *ED = cast<EnumConstantDecl>(D);
2719 mangleIntegerLiteral(ED->getType(), ED->getInitVal());
2720 break;
2721 }
2722
Anders Carlssond553f8c2009-09-21 01:21:10 +00002723 case Decl::NonTypeTemplateParm: {
2724 const NonTypeTemplateParmDecl *PD = cast<NonTypeTemplateParmDecl>(D);
Anders Carlsson0ccdf8d2009-09-27 00:38:53 +00002725 mangleTemplateParameter(PD->getIndex());
Anders Carlssond553f8c2009-09-21 01:21:10 +00002726 break;
2727 }
2728
2729 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002730
Anders Carlsson50755b02009-09-27 20:11:34 +00002731 break;
Anders Carlssond553f8c2009-09-21 01:21:10 +00002732 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002733
Douglas Gregorc7793c72011-01-15 01:15:58 +00002734 case Expr::SubstNonTypeTemplateParmPackExprClass:
John McCall68a51a72011-07-01 00:04:39 +00002735 // FIXME: not clear how to mangle this!
2736 // template <unsigned N...> class A {
2737 // template <class U...> void foo(U (&x)[N]...);
2738 // };
2739 Out << "_SUBSTPACK_";
Douglas Gregorc7793c72011-01-15 01:15:58 +00002740 break;
2741
John McCall865d4472009-11-19 22:55:06 +00002742 case Expr::DependentScopeDeclRefExprClass: {
2743 const DependentScopeDeclRefExpr *DRE = cast<DependentScopeDeclRefExpr>(E);
John McCall26a6ec72011-06-21 22:12:46 +00002744 mangleUnresolvedName(DRE->getQualifier(), 0, DRE->getDeclName(), Arity);
Douglas Gregor4b2ccfc2010-02-28 22:05:49 +00002745
John McCall26a6ec72011-06-21 22:12:46 +00002746 // All the <unresolved-name> productions end in a
2747 // base-unresolved-name, where <template-args> are just tacked
2748 // onto the end.
John McCall6dbce192010-08-20 00:17:19 +00002749 if (DRE->hasExplicitTemplateArgs())
2750 mangleTemplateArgs(DRE->getExplicitTemplateArgs());
Anders Carlsson50755b02009-09-27 20:11:34 +00002751 break;
2752 }
2753
John McCalld9307602010-04-09 22:54:09 +00002754 case Expr::CXXBindTemporaryExprClass:
2755 mangleExpression(cast<CXXBindTemporaryExpr>(E)->getSubExpr());
2756 break;
2757
John McCall4765fa02010-12-06 08:20:24 +00002758 case Expr::ExprWithCleanupsClass:
2759 mangleExpression(cast<ExprWithCleanups>(E)->getSubExpr(), Arity);
John McCalld9307602010-04-09 22:54:09 +00002760 break;
2761
John McCall1dd73832010-02-04 01:42:13 +00002762 case Expr::FloatingLiteralClass: {
2763 const FloatingLiteral *FL = cast<FloatingLiteral>(E);
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002764 Out << 'L';
John McCall1dd73832010-02-04 01:42:13 +00002765 mangleType(FL->getType());
John McCall0512e482010-07-14 04:20:34 +00002766 mangleFloat(FL->getValue());
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002767 Out << 'E';
John McCall1dd73832010-02-04 01:42:13 +00002768 break;
2769 }
2770
John McCallde810632010-04-09 21:48:08 +00002771 case Expr::CharacterLiteralClass:
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002772 Out << 'L';
John McCallde810632010-04-09 21:48:08 +00002773 mangleType(E->getType());
2774 Out << cast<CharacterLiteral>(E)->getValue();
2775 Out << 'E';
2776 break;
2777
2778 case Expr::CXXBoolLiteralExprClass:
2779 Out << "Lb";
2780 Out << (cast<CXXBoolLiteralExpr>(E)->getValue() ? '1' : '0');
2781 Out << 'E';
2782 break;
2783
John McCall0512e482010-07-14 04:20:34 +00002784 case Expr::IntegerLiteralClass: {
2785 llvm::APSInt Value(cast<IntegerLiteral>(E)->getValue());
2786 if (E->getType()->isSignedIntegerType())
2787 Value.setIsSigned(true);
2788 mangleIntegerLiteral(E->getType(), Value);
Anders Carlssone170ba72009-12-14 01:45:37 +00002789 break;
John McCall0512e482010-07-14 04:20:34 +00002790 }
2791
2792 case Expr::ImaginaryLiteralClass: {
2793 const ImaginaryLiteral *IE = cast<ImaginaryLiteral>(E);
2794 // Mangle as if a complex literal.
Nick Lewycky271b6652010-09-05 03:40:33 +00002795 // Proposal from David Vandevoorde, 2010.06.30.
John McCall0512e482010-07-14 04:20:34 +00002796 Out << 'L';
2797 mangleType(E->getType());
2798 if (const FloatingLiteral *Imag =
2799 dyn_cast<FloatingLiteral>(IE->getSubExpr())) {
2800 // Mangle a floating-point zero of the appropriate type.
2801 mangleFloat(llvm::APFloat(Imag->getValue().getSemantics()));
2802 Out << '_';
2803 mangleFloat(Imag->getValue());
2804 } else {
Nick Lewycky271b6652010-09-05 03:40:33 +00002805 Out << "0_";
John McCall0512e482010-07-14 04:20:34 +00002806 llvm::APSInt Value(cast<IntegerLiteral>(IE->getSubExpr())->getValue());
2807 if (IE->getSubExpr()->getType()->isSignedIntegerType())
2808 Value.setIsSigned(true);
2809 mangleNumber(Value);
2810 }
2811 Out << 'E';
2812 break;
2813 }
2814
2815 case Expr::StringLiteralClass: {
John McCall1658c392010-07-15 21:53:03 +00002816 // Revised proposal from David Vandervoorde, 2010.07.15.
John McCall0512e482010-07-14 04:20:34 +00002817 Out << 'L';
John McCall1658c392010-07-15 21:53:03 +00002818 assert(isa<ConstantArrayType>(E->getType()));
2819 mangleType(E->getType());
John McCall0512e482010-07-14 04:20:34 +00002820 Out << 'E';
2821 break;
2822 }
2823
2824 case Expr::GNUNullExprClass:
2825 // FIXME: should this really be mangled the same as nullptr?
2826 // fallthrough
2827
2828 case Expr::CXXNullPtrLiteralExprClass: {
2829 // Proposal from David Vandervoorde, 2010.06.30, as
2830 // modified by ABI list discussion.
2831 Out << "LDnE";
2832 break;
2833 }
Douglas Gregorbe230c32011-01-03 17:17:50 +00002834
2835 case Expr::PackExpansionExprClass:
2836 Out << "sp";
2837 mangleExpression(cast<PackExpansionExpr>(E)->getPattern());
2838 break;
Douglas Gregor2e774c42011-01-04 18:56:13 +00002839
2840 case Expr::SizeOfPackExprClass: {
Douglas Gregor2e774c42011-01-04 18:56:13 +00002841 Out << "sZ";
2842 const NamedDecl *Pack = cast<SizeOfPackExpr>(E)->getPack();
2843 if (const TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(Pack))
2844 mangleTemplateParameter(TTP->getIndex());
2845 else if (const NonTypeTemplateParmDecl *NTTP
2846 = dyn_cast<NonTypeTemplateParmDecl>(Pack))
2847 mangleTemplateParameter(NTTP->getIndex());
2848 else if (const TemplateTemplateParmDecl *TempTP
2849 = dyn_cast<TemplateTemplateParmDecl>(Pack))
2850 mangleTemplateParameter(TempTP->getIndex());
Douglas Gregor91832362011-07-12 07:03:48 +00002851 else
2852 mangleFunctionParam(cast<ParmVarDecl>(Pack));
Douglas Gregordfbbcf92011-03-03 02:20:19 +00002853 break;
Douglas Gregor2e774c42011-01-04 18:56:13 +00002854 }
Douglas Gregor03e80032011-06-21 17:03:29 +00002855
2856 case Expr::MaterializeTemporaryExprClass: {
2857 mangleExpression(cast<MaterializeTemporaryExpr>(E)->GetTemporaryExpr());
2858 break;
2859 }
Anders Carlssond553f8c2009-09-21 01:21:10 +00002860 }
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00002861}
2862
John McCallfb44de92011-05-01 22:35:37 +00002863/// Mangle an expression which refers to a parameter variable.
2864///
2865/// <expression> ::= <function-param>
2866/// <function-param> ::= fp <top-level CV-qualifiers> _ # L == 0, I == 0
2867/// <function-param> ::= fp <top-level CV-qualifiers>
2868/// <parameter-2 non-negative number> _ # L == 0, I > 0
2869/// <function-param> ::= fL <L-1 non-negative number>
2870/// p <top-level CV-qualifiers> _ # L > 0, I == 0
2871/// <function-param> ::= fL <L-1 non-negative number>
2872/// p <top-level CV-qualifiers>
2873/// <I-1 non-negative number> _ # L > 0, I > 0
2874///
2875/// L is the nesting depth of the parameter, defined as 1 if the
2876/// parameter comes from the innermost function prototype scope
2877/// enclosing the current context, 2 if from the next enclosing
2878/// function prototype scope, and so on, with one special case: if
2879/// we've processed the full parameter clause for the innermost
2880/// function type, then L is one less. This definition conveniently
2881/// makes it irrelevant whether a function's result type was written
2882/// trailing or leading, but is otherwise overly complicated; the
2883/// numbering was first designed without considering references to
2884/// parameter in locations other than return types, and then the
2885/// mangling had to be generalized without changing the existing
2886/// manglings.
2887///
2888/// I is the zero-based index of the parameter within its parameter
2889/// declaration clause. Note that the original ABI document describes
2890/// this using 1-based ordinals.
2891void CXXNameMangler::mangleFunctionParam(const ParmVarDecl *parm) {
2892 unsigned parmDepth = parm->getFunctionScopeDepth();
2893 unsigned parmIndex = parm->getFunctionScopeIndex();
2894
2895 // Compute 'L'.
2896 // parmDepth does not include the declaring function prototype.
2897 // FunctionTypeDepth does account for that.
2898 assert(parmDepth < FunctionTypeDepth.getDepth());
2899 unsigned nestingDepth = FunctionTypeDepth.getDepth() - parmDepth;
2900 if (FunctionTypeDepth.isInResultType())
2901 nestingDepth--;
2902
2903 if (nestingDepth == 0) {
2904 Out << "fp";
2905 } else {
2906 Out << "fL" << (nestingDepth - 1) << 'p';
2907 }
2908
2909 // Top-level qualifiers. We don't have to worry about arrays here,
2910 // because parameters declared as arrays should already have been
2911 // tranformed to have pointer type. FIXME: apparently these don't
2912 // get mangled if used as an rvalue of a known non-class type?
2913 assert(!parm->getType()->isArrayType()
2914 && "parameter's type is still an array type?");
2915 mangleQualifiers(parm->getType().getQualifiers());
2916
2917 // Parameter index.
2918 if (parmIndex != 0) {
2919 Out << (parmIndex - 1);
2920 }
2921 Out << '_';
2922}
2923
Anders Carlsson3ac86b52009-04-15 05:36:58 +00002924void CXXNameMangler::mangleCXXCtorType(CXXCtorType T) {
2925 // <ctor-dtor-name> ::= C1 # complete object constructor
2926 // ::= C2 # base object constructor
2927 // ::= C3 # complete object allocating constructor
2928 //
2929 switch (T) {
2930 case Ctor_Complete:
2931 Out << "C1";
2932 break;
2933 case Ctor_Base:
2934 Out << "C2";
2935 break;
2936 case Ctor_CompleteAllocating:
2937 Out << "C3";
2938 break;
2939 }
2940}
2941
Anders Carlsson27ae5362009-04-17 01:58:57 +00002942void CXXNameMangler::mangleCXXDtorType(CXXDtorType T) {
2943 // <ctor-dtor-name> ::= D0 # deleting destructor
2944 // ::= D1 # complete object destructor
2945 // ::= D2 # base object destructor
2946 //
2947 switch (T) {
2948 case Dtor_Deleting:
2949 Out << "D0";
2950 break;
2951 case Dtor_Complete:
2952 Out << "D1";
2953 break;
2954 case Dtor_Base:
2955 Out << "D2";
2956 break;
2957 }
2958}
2959
John McCall6dbce192010-08-20 00:17:19 +00002960void CXXNameMangler::mangleTemplateArgs(
Argyrios Kyrtzidisb0c3e092011-09-22 20:07:03 +00002961 const ASTTemplateArgumentListInfo &TemplateArgs) {
John McCall6dbce192010-08-20 00:17:19 +00002962 // <template-args> ::= I <template-arg>+ E
2963 Out << 'I';
John McCall4f4e4132011-05-04 01:45:19 +00002964 for (unsigned i = 0, e = TemplateArgs.NumTemplateArgs; i != e; ++i)
2965 mangleTemplateArg(0, TemplateArgs.getTemplateArgs()[i].getArgument());
John McCall6dbce192010-08-20 00:17:19 +00002966 Out << 'E';
2967}
2968
Douglas Gregor20f0cc72010-04-23 03:10:43 +00002969void CXXNameMangler::mangleTemplateArgs(TemplateName Template,
2970 const TemplateArgument *TemplateArgs,
2971 unsigned NumTemplateArgs) {
2972 if (TemplateDecl *TD = Template.getAsTemplateDecl())
2973 return mangleTemplateArgs(*TD->getTemplateParameters(), TemplateArgs,
2974 NumTemplateArgs);
Sean Huntc3021132010-05-05 15:23:54 +00002975
John McCall4f4e4132011-05-04 01:45:19 +00002976 mangleUnresolvedTemplateArgs(TemplateArgs, NumTemplateArgs);
2977}
2978
2979void CXXNameMangler::mangleUnresolvedTemplateArgs(const TemplateArgument *args,
2980 unsigned numArgs) {
Douglas Gregor20f0cc72010-04-23 03:10:43 +00002981 // <template-args> ::= I <template-arg>+ E
2982 Out << 'I';
John McCall4f4e4132011-05-04 01:45:19 +00002983 for (unsigned i = 0; i != numArgs; ++i)
2984 mangleTemplateArg(0, args[i]);
Douglas Gregor20f0cc72010-04-23 03:10:43 +00002985 Out << 'E';
2986}
2987
Rafael Espindolad9800722010-03-11 14:07:00 +00002988void CXXNameMangler::mangleTemplateArgs(const TemplateParameterList &PL,
2989 const TemplateArgumentList &AL) {
Anders Carlsson7a0ba872009-05-15 16:09:15 +00002990 // <template-args> ::= I <template-arg>+ E
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002991 Out << 'I';
Rafael Espindolad9800722010-03-11 14:07:00 +00002992 for (unsigned i = 0, e = AL.size(); i != e; ++i)
2993 mangleTemplateArg(PL.getParam(i), AL[i]);
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002994 Out << 'E';
Anders Carlsson7a0ba872009-05-15 16:09:15 +00002995}
2996
Rafael Espindolad9800722010-03-11 14:07:00 +00002997void CXXNameMangler::mangleTemplateArgs(const TemplateParameterList &PL,
2998 const TemplateArgument *TemplateArgs,
Anders Carlsson7624f212009-09-18 02:42:01 +00002999 unsigned NumTemplateArgs) {
3000 // <template-args> ::= I <template-arg>+ E
Benjamin Kramer35f59b62010-04-10 16:03:31 +00003001 Out << 'I';
Daniel Dunbar7e0c1952009-11-21 09:17:15 +00003002 for (unsigned i = 0; i != NumTemplateArgs; ++i)
Rafael Espindolad9800722010-03-11 14:07:00 +00003003 mangleTemplateArg(PL.getParam(i), TemplateArgs[i]);
Benjamin Kramer35f59b62010-04-10 16:03:31 +00003004 Out << 'E';
Anders Carlsson7624f212009-09-18 02:42:01 +00003005}
3006
Rafael Espindolad9800722010-03-11 14:07:00 +00003007void CXXNameMangler::mangleTemplateArg(const NamedDecl *P,
Douglas Gregorf1588662011-07-12 15:18:55 +00003008 TemplateArgument A) {
Mike Stump1eb44332009-09-09 15:08:12 +00003009 // <template-arg> ::= <type> # type or template
Anders Carlsson7a0ba872009-05-15 16:09:15 +00003010 // ::= X <expression> E # expression
3011 // ::= <expr-primary> # simple expressions
Douglas Gregor4fc48662011-01-13 16:39:34 +00003012 // ::= J <template-arg>* E # argument pack
Douglas Gregorf1588662011-07-12 15:18:55 +00003013 // ::= sp <expression> # pack expansion of (C++0x)
3014 if (!A.isInstantiationDependent() || A.isDependent())
3015 A = Context.getASTContext().getCanonicalTemplateArgument(A);
3016
Anders Carlsson7a0ba872009-05-15 16:09:15 +00003017 switch (A.getKind()) {
Douglas Gregorf90b27a2011-01-03 22:36:02 +00003018 case TemplateArgument::Null:
3019 llvm_unreachable("Cannot mangle NULL template argument");
3020
Anders Carlsson7a0ba872009-05-15 16:09:15 +00003021 case TemplateArgument::Type:
3022 mangleType(A.getAsType());
3023 break;
Anders Carlsson9e85c742009-12-23 19:30:55 +00003024 case TemplateArgument::Template:
John McCallb6f532e2010-07-14 06:43:17 +00003025 // This is mangled as <type>.
3026 mangleType(A.getAsTemplate());
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00003027 break;
Douglas Gregora7fc9012011-01-05 18:58:31 +00003028 case TemplateArgument::TemplateExpansion:
Douglas Gregor4fc48662011-01-13 16:39:34 +00003029 // <type> ::= Dp <type> # pack expansion (C++0x)
Douglas Gregora7fc9012011-01-05 18:58:31 +00003030 Out << "Dp";
3031 mangleType(A.getAsTemplateOrTemplatePattern());
3032 break;
John McCall092beef2012-01-06 05:06:35 +00003033 case TemplateArgument::Expression: {
3034 // It's possible to end up with a DeclRefExpr here in certain
3035 // dependent cases, in which case we should mangle as a
3036 // declaration.
3037 const Expr *E = A.getAsExpr()->IgnoreParens();
3038 if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E)) {
3039 const ValueDecl *D = DRE->getDecl();
3040 if (isa<VarDecl>(D) || isa<FunctionDecl>(D)) {
3041 Out << "L";
3042 mangle(D, "_Z");
3043 Out << 'E';
3044 break;
3045 }
3046 }
3047
Anders Carlssond553f8c2009-09-21 01:21:10 +00003048 Out << 'X';
John McCall092beef2012-01-06 05:06:35 +00003049 mangleExpression(E);
Anders Carlssond553f8c2009-09-21 01:21:10 +00003050 Out << 'E';
3051 break;
John McCall092beef2012-01-06 05:06:35 +00003052 }
Anders Carlssone170ba72009-12-14 01:45:37 +00003053 case TemplateArgument::Integral:
3054 mangleIntegerLiteral(A.getIntegralType(), *A.getAsIntegral());
Anders Carlsson7a0ba872009-05-15 16:09:15 +00003055 break;
Daniel Dunbar7e0c1952009-11-21 09:17:15 +00003056 case TemplateArgument::Declaration: {
Douglas Gregor20f0cc72010-04-23 03:10:43 +00003057 assert(P && "Missing template parameter for declaration argument");
Daniel Dunbar7e0c1952009-11-21 09:17:15 +00003058 // <expr-primary> ::= L <mangled-name> E # external name
3059
Rafael Espindolad9800722010-03-11 14:07:00 +00003060 // Clang produces AST's where pointer-to-member-function expressions
Daniel Dunbar7e0c1952009-11-21 09:17:15 +00003061 // and pointer-to-function expressions are represented as a declaration not
Rafael Espindolad9800722010-03-11 14:07:00 +00003062 // an expression. We compensate for it here to produce the correct mangling.
3063 NamedDecl *D = cast<NamedDecl>(A.getAsDecl());
3064 const NonTypeTemplateParmDecl *Parameter = cast<NonTypeTemplateParmDecl>(P);
John McCallc0a45592011-04-24 08:43:07 +00003065 bool compensateMangling = !Parameter->getType()->isReferenceType();
Rafael Espindolad9800722010-03-11 14:07:00 +00003066 if (compensateMangling) {
3067 Out << 'X';
3068 mangleOperatorName(OO_Amp, 1);
3069 }
3070
Daniel Dunbar7e0c1952009-11-21 09:17:15 +00003071 Out << 'L';
3072 // References to external entities use the mangled name; if the name would
3073 // not normally be manged then mangle it as unqualified.
3074 //
3075 // FIXME: The ABI specifies that external names here should have _Z, but
3076 // gcc leaves this off.
Rafael Espindolad9800722010-03-11 14:07:00 +00003077 if (compensateMangling)
3078 mangle(D, "_Z");
3079 else
3080 mangle(D, "Z");
Daniel Dunbar7e0c1952009-11-21 09:17:15 +00003081 Out << 'E';
Rafael Espindolad9800722010-03-11 14:07:00 +00003082
3083 if (compensateMangling)
3084 Out << 'E';
3085
Daniel Dunbar7e0c1952009-11-21 09:17:15 +00003086 break;
3087 }
Douglas Gregorf90b27a2011-01-03 22:36:02 +00003088
3089 case TemplateArgument::Pack: {
3090 // Note: proposal by Mike Herrick on 12/20/10
3091 Out << 'J';
3092 for (TemplateArgument::pack_iterator PA = A.pack_begin(),
3093 PAEnd = A.pack_end();
3094 PA != PAEnd; ++PA)
3095 mangleTemplateArg(P, *PA);
3096 Out << 'E';
3097 }
Daniel Dunbar7e0c1952009-11-21 09:17:15 +00003098 }
Anders Carlsson7a0ba872009-05-15 16:09:15 +00003099}
3100
Anders Carlsson0ccdf8d2009-09-27 00:38:53 +00003101void CXXNameMangler::mangleTemplateParameter(unsigned Index) {
3102 // <template-param> ::= T_ # first template parameter
3103 // ::= T <parameter-2 non-negative number> _
3104 if (Index == 0)
3105 Out << "T_";
3106 else
3107 Out << 'T' << (Index - 1) << '_';
3108}
3109
John McCall68a51a72011-07-01 00:04:39 +00003110void CXXNameMangler::mangleExistingSubstitution(QualType type) {
3111 bool result = mangleSubstitution(type);
3112 assert(result && "no existing substitution for type");
3113 (void) result;
3114}
3115
3116void CXXNameMangler::mangleExistingSubstitution(TemplateName tname) {
3117 bool result = mangleSubstitution(tname);
3118 assert(result && "no existing substitution for template name");
3119 (void) result;
3120}
3121
Anders Carlsson76967372009-09-17 00:43:46 +00003122// <substitution> ::= S <seq-id> _
3123// ::= S_
Anders Carlsson6862fc72009-09-17 04:16:28 +00003124bool CXXNameMangler::mangleSubstitution(const NamedDecl *ND) {
Anders Carlssone7c8cb62009-09-26 20:53:44 +00003125 // Try one of the standard substitutions first.
3126 if (mangleStandardSubstitution(ND))
3127 return true;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00003128
Anders Carlsson433d1372009-11-07 04:26:04 +00003129 ND = cast<NamedDecl>(ND->getCanonicalDecl());
Anders Carlsson6862fc72009-09-17 04:16:28 +00003130 return mangleSubstitution(reinterpret_cast<uintptr_t>(ND));
3131}
3132
Douglas Gregor14795c82011-12-03 18:24:43 +00003133/// \brief Determine whether the given type has any qualifiers that are
3134/// relevant for substitutions.
3135static bool hasMangledSubstitutionQualifiers(QualType T) {
3136 Qualifiers Qs = T.getQualifiers();
3137 return Qs.getCVRQualifiers() || Qs.hasAddressSpace();
3138}
3139
Anders Carlsson76967372009-09-17 00:43:46 +00003140bool CXXNameMangler::mangleSubstitution(QualType T) {
Douglas Gregor14795c82011-12-03 18:24:43 +00003141 if (!hasMangledSubstitutionQualifiers(T)) {
Anders Carlssond99edc42009-09-26 03:55:37 +00003142 if (const RecordType *RT = T->getAs<RecordType>())
3143 return mangleSubstitution(RT->getDecl());
3144 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00003145
Anders Carlsson76967372009-09-17 00:43:46 +00003146 uintptr_t TypePtr = reinterpret_cast<uintptr_t>(T.getAsOpaquePtr());
3147
Anders Carlssond3a932a2009-09-17 03:53:28 +00003148 return mangleSubstitution(TypePtr);
3149}
3150
Douglas Gregor1e9268e2010-04-28 05:58:56 +00003151bool CXXNameMangler::mangleSubstitution(TemplateName Template) {
3152 if (TemplateDecl *TD = Template.getAsTemplateDecl())
3153 return mangleSubstitution(TD);
Sean Huntc3021132010-05-05 15:23:54 +00003154
Douglas Gregor1e9268e2010-04-28 05:58:56 +00003155 Template = Context.getASTContext().getCanonicalTemplateName(Template);
3156 return mangleSubstitution(
3157 reinterpret_cast<uintptr_t>(Template.getAsVoidPointer()));
3158}
3159
Anders Carlssond3a932a2009-09-17 03:53:28 +00003160bool CXXNameMangler::mangleSubstitution(uintptr_t Ptr) {
Benjamin Kramer35f59b62010-04-10 16:03:31 +00003161 llvm::DenseMap<uintptr_t, unsigned>::iterator I = Substitutions.find(Ptr);
Anders Carlsson76967372009-09-17 00:43:46 +00003162 if (I == Substitutions.end())
3163 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00003164
Anders Carlsson76967372009-09-17 00:43:46 +00003165 unsigned SeqID = I->second;
3166 if (SeqID == 0)
3167 Out << "S_";
3168 else {
3169 SeqID--;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00003170
Anders Carlsson76967372009-09-17 00:43:46 +00003171 // <seq-id> is encoded in base-36, using digits and upper case letters.
3172 char Buffer[10];
Benjamin Kramer35f59b62010-04-10 16:03:31 +00003173 char *BufferPtr = llvm::array_endof(Buffer);
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00003174
Anders Carlsson76967372009-09-17 00:43:46 +00003175 if (SeqID == 0) *--BufferPtr = '0';
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00003176
Anders Carlsson76967372009-09-17 00:43:46 +00003177 while (SeqID) {
3178 assert(BufferPtr > Buffer && "Buffer overflow!");
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00003179
John McCall6ab30e02010-06-09 07:26:17 +00003180 char c = static_cast<char>(SeqID % 36);
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00003181
Anders Carlsson76967372009-09-17 00:43:46 +00003182 *--BufferPtr = (c < 10 ? '0' + c : 'A' + c - 10);
3183 SeqID /= 36;
3184 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00003185
Benjamin Kramer35f59b62010-04-10 16:03:31 +00003186 Out << 'S'
Chris Lattner5f9e2722011-07-23 10:55:15 +00003187 << StringRef(BufferPtr, llvm::array_endof(Buffer)-BufferPtr)
Benjamin Kramer35f59b62010-04-10 16:03:31 +00003188 << '_';
Anders Carlsson76967372009-09-17 00:43:46 +00003189 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00003190
Anders Carlsson76967372009-09-17 00:43:46 +00003191 return true;
3192}
3193
Anders Carlssonf514b542009-09-27 00:12:57 +00003194static bool isCharType(QualType T) {
3195 if (T.isNull())
3196 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00003197
Anders Carlssonf514b542009-09-27 00:12:57 +00003198 return T->isSpecificBuiltinType(BuiltinType::Char_S) ||
3199 T->isSpecificBuiltinType(BuiltinType::Char_U);
3200}
3201
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00003202/// isCharSpecialization - Returns whether a given type is a template
Anders Carlssonf514b542009-09-27 00:12:57 +00003203/// specialization of a given name with a single argument of type char.
3204static bool isCharSpecialization(QualType T, const char *Name) {
3205 if (T.isNull())
3206 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00003207
Anders Carlssonf514b542009-09-27 00:12:57 +00003208 const RecordType *RT = T->getAs<RecordType>();
3209 if (!RT)
3210 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00003211
3212 const ClassTemplateSpecializationDecl *SD =
Anders Carlssonf514b542009-09-27 00:12:57 +00003213 dyn_cast<ClassTemplateSpecializationDecl>(RT->getDecl());
3214 if (!SD)
3215 return false;
3216
Douglas Gregorccc1b5e2012-02-21 00:37:24 +00003217 if (!isStdNamespace(getEffectiveDeclContext(SD)))
Anders Carlssonf514b542009-09-27 00:12:57 +00003218 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00003219
Anders Carlssonf514b542009-09-27 00:12:57 +00003220 const TemplateArgumentList &TemplateArgs = SD->getTemplateArgs();
3221 if (TemplateArgs.size() != 1)
3222 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00003223
Anders Carlssonf514b542009-09-27 00:12:57 +00003224 if (!isCharType(TemplateArgs[0].getAsType()))
3225 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00003226
Daniel Dunbar01eb9b92009-10-18 21:17:35 +00003227 return SD->getIdentifier()->getName() == Name;
Anders Carlssonf514b542009-09-27 00:12:57 +00003228}
3229
Anders Carlsson91f88602009-12-07 19:56:42 +00003230template <std::size_t StrLen>
Benjamin Kramer54353f42010-11-25 18:29:30 +00003231static bool isStreamCharSpecialization(const ClassTemplateSpecializationDecl*SD,
3232 const char (&Str)[StrLen]) {
Anders Carlsson91f88602009-12-07 19:56:42 +00003233 if (!SD->getIdentifier()->isStr(Str))
3234 return false;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00003235
Anders Carlsson91f88602009-12-07 19:56:42 +00003236 const TemplateArgumentList &TemplateArgs = SD->getTemplateArgs();
3237 if (TemplateArgs.size() != 2)
3238 return false;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00003239
Anders Carlsson91f88602009-12-07 19:56:42 +00003240 if (!isCharType(TemplateArgs[0].getAsType()))
3241 return false;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00003242
Anders Carlsson91f88602009-12-07 19:56:42 +00003243 if (!isCharSpecialization(TemplateArgs[1].getAsType(), "char_traits"))
3244 return false;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00003245
Anders Carlsson91f88602009-12-07 19:56:42 +00003246 return true;
3247}
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00003248
Anders Carlssone7c8cb62009-09-26 20:53:44 +00003249bool CXXNameMangler::mangleStandardSubstitution(const NamedDecl *ND) {
3250 // <substitution> ::= St # ::std::
Anders Carlsson8c031552009-09-26 23:10:05 +00003251 if (const NamespaceDecl *NS = dyn_cast<NamespaceDecl>(ND)) {
Anders Carlsson47846d22009-12-04 06:23:23 +00003252 if (isStd(NS)) {
Anders Carlsson8c031552009-09-26 23:10:05 +00003253 Out << "St";
3254 return true;
3255 }
3256 }
3257
3258 if (const ClassTemplateDecl *TD = dyn_cast<ClassTemplateDecl>(ND)) {
Douglas Gregorccc1b5e2012-02-21 00:37:24 +00003259 if (!isStdNamespace(getEffectiveDeclContext(TD)))
Anders Carlsson8c031552009-09-26 23:10:05 +00003260 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00003261
Anders Carlsson8c031552009-09-26 23:10:05 +00003262 // <substitution> ::= Sa # ::std::allocator
3263 if (TD->getIdentifier()->isStr("allocator")) {
3264 Out << "Sa";
3265 return true;
3266 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00003267
Anders Carlsson189d59c2009-09-26 23:14:39 +00003268 // <<substitution> ::= Sb # ::std::basic_string
3269 if (TD->getIdentifier()->isStr("basic_string")) {
3270 Out << "Sb";
3271 return true;
3272 }
Anders Carlsson8c031552009-09-26 23:10:05 +00003273 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00003274
3275 if (const ClassTemplateSpecializationDecl *SD =
Anders Carlssonf514b542009-09-27 00:12:57 +00003276 dyn_cast<ClassTemplateSpecializationDecl>(ND)) {
Douglas Gregorccc1b5e2012-02-21 00:37:24 +00003277 if (!isStdNamespace(getEffectiveDeclContext(SD)))
Eli Friedman5370ee22010-02-23 18:25:09 +00003278 return false;
3279
Anders Carlssonf514b542009-09-27 00:12:57 +00003280 // <substitution> ::= Ss # ::std::basic_string<char,
3281 // ::std::char_traits<char>,
3282 // ::std::allocator<char> >
3283 if (SD->getIdentifier()->isStr("basic_string")) {
3284 const TemplateArgumentList &TemplateArgs = SD->getTemplateArgs();
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00003285
Anders Carlssonf514b542009-09-27 00:12:57 +00003286 if (TemplateArgs.size() != 3)
3287 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00003288
Anders Carlssonf514b542009-09-27 00:12:57 +00003289 if (!isCharType(TemplateArgs[0].getAsType()))
3290 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00003291
Anders Carlssonf514b542009-09-27 00:12:57 +00003292 if (!isCharSpecialization(TemplateArgs[1].getAsType(), "char_traits"))
3293 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00003294
Anders Carlssonf514b542009-09-27 00:12:57 +00003295 if (!isCharSpecialization(TemplateArgs[2].getAsType(), "allocator"))
3296 return false;
3297
3298 Out << "Ss";
3299 return true;
3300 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00003301
Anders Carlsson91f88602009-12-07 19:56:42 +00003302 // <substitution> ::= Si # ::std::basic_istream<char,
3303 // ::std::char_traits<char> >
3304 if (isStreamCharSpecialization(SD, "basic_istream")) {
3305 Out << "Si";
3306 return true;
3307 }
3308
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00003309 // <substitution> ::= So # ::std::basic_ostream<char,
Anders Carlsson8f8fd8e2009-10-08 17:20:26 +00003310 // ::std::char_traits<char> >
Anders Carlsson91f88602009-12-07 19:56:42 +00003311 if (isStreamCharSpecialization(SD, "basic_ostream")) {
Anders Carlsson8f8fd8e2009-10-08 17:20:26 +00003312 Out << "So";
3313 return true;
3314 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00003315
Anders Carlsson91f88602009-12-07 19:56:42 +00003316 // <substitution> ::= Sd # ::std::basic_iostream<char,
3317 // ::std::char_traits<char> >
3318 if (isStreamCharSpecialization(SD, "basic_iostream")) {
3319 Out << "Sd";
3320 return true;
3321 }
Anders Carlssonf514b542009-09-27 00:12:57 +00003322 }
Anders Carlsson8c031552009-09-26 23:10:05 +00003323 return false;
Anders Carlssone7c8cb62009-09-26 20:53:44 +00003324}
3325
Anders Carlsson76967372009-09-17 00:43:46 +00003326void CXXNameMangler::addSubstitution(QualType T) {
Douglas Gregor14795c82011-12-03 18:24:43 +00003327 if (!hasMangledSubstitutionQualifiers(T)) {
Anders Carlssond99edc42009-09-26 03:55:37 +00003328 if (const RecordType *RT = T->getAs<RecordType>()) {
3329 addSubstitution(RT->getDecl());
3330 return;
3331 }
3332 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00003333
Anders Carlsson76967372009-09-17 00:43:46 +00003334 uintptr_t TypePtr = reinterpret_cast<uintptr_t>(T.getAsOpaquePtr());
Anders Carlssond3a932a2009-09-17 03:53:28 +00003335 addSubstitution(TypePtr);
3336}
3337
Douglas Gregor1e9268e2010-04-28 05:58:56 +00003338void CXXNameMangler::addSubstitution(TemplateName Template) {
3339 if (TemplateDecl *TD = Template.getAsTemplateDecl())
3340 return addSubstitution(TD);
Sean Huntc3021132010-05-05 15:23:54 +00003341
Douglas Gregor1e9268e2010-04-28 05:58:56 +00003342 Template = Context.getASTContext().getCanonicalTemplateName(Template);
3343 addSubstitution(reinterpret_cast<uintptr_t>(Template.getAsVoidPointer()));
3344}
3345
Anders Carlssond3a932a2009-09-17 03:53:28 +00003346void CXXNameMangler::addSubstitution(uintptr_t Ptr) {
Anders Carlssond3a932a2009-09-17 03:53:28 +00003347 assert(!Substitutions.count(Ptr) && "Substitution already exists!");
Anders Carlsson9d85b722010-06-02 04:29:50 +00003348 Substitutions[Ptr] = SeqID++;
Anders Carlsson76967372009-09-17 00:43:46 +00003349}
3350
Daniel Dunbar1b077112009-11-21 09:06:10 +00003351//
Mike Stump1eb44332009-09-09 15:08:12 +00003352
Daniel Dunbar1b077112009-11-21 09:06:10 +00003353/// \brief Mangles the name of the declaration D and emits that name to the
3354/// given output stream.
3355///
3356/// If the declaration D requires a mangled name, this routine will emit that
3357/// mangled name to \p os and return true. Otherwise, \p os will be unchanged
3358/// and this routine will return false. In this case, the caller should just
3359/// emit the identifier of the declaration (\c D->getIdentifier()) as its
3360/// name.
Peter Collingbourne14110472011-01-13 18:57:25 +00003361void ItaniumMangleContext::mangleName(const NamedDecl *D,
Chris Lattner5f9e2722011-07-23 10:55:15 +00003362 raw_ostream &Out) {
Daniel Dunbarc02ab4c2009-11-21 09:14:44 +00003363 assert((isa<FunctionDecl>(D) || isa<VarDecl>(D)) &&
3364 "Invalid mangleName() call, argument is not a variable or function!");
3365 assert(!isa<CXXConstructorDecl>(D) && !isa<CXXDestructorDecl>(D) &&
3366 "Invalid mangleName() call on 'structor decl!");
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00003367
Daniel Dunbar1b077112009-11-21 09:06:10 +00003368 PrettyStackTraceDecl CrashInfo(D, SourceLocation(),
3369 getASTContext().getSourceManager(),
3370 "Mangling declaration");
Mike Stump1eb44332009-09-09 15:08:12 +00003371
John McCallfb44de92011-05-01 22:35:37 +00003372 CXXNameMangler Mangler(*this, Out, D);
Daniel Dunbar94fd26d2009-11-21 09:06:22 +00003373 return Mangler.mangle(D);
Daniel Dunbar1b077112009-11-21 09:06:10 +00003374}
Mike Stump1eb44332009-09-09 15:08:12 +00003375
Peter Collingbourne14110472011-01-13 18:57:25 +00003376void ItaniumMangleContext::mangleCXXCtor(const CXXConstructorDecl *D,
3377 CXXCtorType Type,
Chris Lattner5f9e2722011-07-23 10:55:15 +00003378 raw_ostream &Out) {
Rafael Espindolac4850c22011-02-10 23:59:36 +00003379 CXXNameMangler Mangler(*this, Out, D, Type);
Daniel Dunbar77939c92009-11-21 09:06:31 +00003380 Mangler.mangle(D);
Daniel Dunbar1b077112009-11-21 09:06:10 +00003381}
Mike Stump1eb44332009-09-09 15:08:12 +00003382
Peter Collingbourne14110472011-01-13 18:57:25 +00003383void ItaniumMangleContext::mangleCXXDtor(const CXXDestructorDecl *D,
3384 CXXDtorType Type,
Chris Lattner5f9e2722011-07-23 10:55:15 +00003385 raw_ostream &Out) {
Rafael Espindolac4850c22011-02-10 23:59:36 +00003386 CXXNameMangler Mangler(*this, Out, D, Type);
Daniel Dunbar77939c92009-11-21 09:06:31 +00003387 Mangler.mangle(D);
Daniel Dunbar1b077112009-11-21 09:06:10 +00003388}
Mike Stumpf1216772009-07-31 18:25:34 +00003389
Peter Collingbourne14110472011-01-13 18:57:25 +00003390void ItaniumMangleContext::mangleThunk(const CXXMethodDecl *MD,
3391 const ThunkInfo &Thunk,
Chris Lattner5f9e2722011-07-23 10:55:15 +00003392 raw_ostream &Out) {
Anders Carlsson19879c92010-03-23 17:17:29 +00003393 // <special-name> ::= T <call-offset> <base encoding>
3394 // # base is the nominal target function of thunk
3395 // <special-name> ::= Tc <call-offset> <call-offset> <base encoding>
3396 // # base is the nominal target function of thunk
3397 // # first call-offset is 'this' adjustment
3398 // # second call-offset is result adjustment
Sean Huntc3021132010-05-05 15:23:54 +00003399
Anders Carlsson19879c92010-03-23 17:17:29 +00003400 assert(!isa<CXXDestructorDecl>(MD) &&
3401 "Use mangleCXXDtor for destructor decls!");
Rafael Espindolac4850c22011-02-10 23:59:36 +00003402 CXXNameMangler Mangler(*this, Out);
Anders Carlsson19879c92010-03-23 17:17:29 +00003403 Mangler.getStream() << "_ZT";
3404 if (!Thunk.Return.isEmpty())
3405 Mangler.getStream() << 'c';
Sean Huntc3021132010-05-05 15:23:54 +00003406
Anders Carlsson19879c92010-03-23 17:17:29 +00003407 // Mangle the 'this' pointer adjustment.
3408 Mangler.mangleCallOffset(Thunk.This.NonVirtual, Thunk.This.VCallOffsetOffset);
Sean Huntc3021132010-05-05 15:23:54 +00003409
Anders Carlsson19879c92010-03-23 17:17:29 +00003410 // Mangle the return pointer adjustment if there is one.
3411 if (!Thunk.Return.isEmpty())
3412 Mangler.mangleCallOffset(Thunk.Return.NonVirtual,
3413 Thunk.Return.VBaseOffsetOffset);
Sean Huntc3021132010-05-05 15:23:54 +00003414
Anders Carlsson19879c92010-03-23 17:17:29 +00003415 Mangler.mangleFunctionEncoding(MD);
3416}
3417
Sean Huntc3021132010-05-05 15:23:54 +00003418void
Peter Collingbourne14110472011-01-13 18:57:25 +00003419ItaniumMangleContext::mangleCXXDtorThunk(const CXXDestructorDecl *DD,
3420 CXXDtorType Type,
3421 const ThisAdjustment &ThisAdjustment,
Chris Lattner5f9e2722011-07-23 10:55:15 +00003422 raw_ostream &Out) {
Anders Carlsson19879c92010-03-23 17:17:29 +00003423 // <special-name> ::= T <call-offset> <base encoding>
3424 // # base is the nominal target function of thunk
Rafael Espindolac4850c22011-02-10 23:59:36 +00003425 CXXNameMangler Mangler(*this, Out, DD, Type);
Anders Carlsson19879c92010-03-23 17:17:29 +00003426 Mangler.getStream() << "_ZT";
3427
3428 // Mangle the 'this' pointer adjustment.
Sean Huntc3021132010-05-05 15:23:54 +00003429 Mangler.mangleCallOffset(ThisAdjustment.NonVirtual,
Anders Carlsson19879c92010-03-23 17:17:29 +00003430 ThisAdjustment.VCallOffsetOffset);
3431
3432 Mangler.mangleFunctionEncoding(DD);
3433}
3434
Daniel Dunbarc0747712009-11-21 09:12:13 +00003435/// mangleGuardVariable - Returns the mangled name for a guard variable
3436/// for the passed in VarDecl.
Peter Collingbourne14110472011-01-13 18:57:25 +00003437void ItaniumMangleContext::mangleItaniumGuardVariable(const VarDecl *D,
Chris Lattner5f9e2722011-07-23 10:55:15 +00003438 raw_ostream &Out) {
Daniel Dunbarc0747712009-11-21 09:12:13 +00003439 // <special-name> ::= GV <object name> # Guard variable for one-time
3440 // # initialization
Rafael Espindolac4850c22011-02-10 23:59:36 +00003441 CXXNameMangler Mangler(*this, Out);
Daniel Dunbarc0747712009-11-21 09:12:13 +00003442 Mangler.getStream() << "_ZGV";
3443 Mangler.mangleName(D);
3444}
3445
Peter Collingbourne14110472011-01-13 18:57:25 +00003446void ItaniumMangleContext::mangleReferenceTemporary(const VarDecl *D,
Chris Lattner5f9e2722011-07-23 10:55:15 +00003447 raw_ostream &Out) {
Anders Carlsson715edf22010-06-26 16:09:40 +00003448 // We match the GCC mangling here.
3449 // <special-name> ::= GR <object name>
Rafael Espindolac4850c22011-02-10 23:59:36 +00003450 CXXNameMangler Mangler(*this, Out);
Anders Carlsson715edf22010-06-26 16:09:40 +00003451 Mangler.getStream() << "_ZGR";
3452 Mangler.mangleName(D);
3453}
3454
Peter Collingbourne14110472011-01-13 18:57:25 +00003455void ItaniumMangleContext::mangleCXXVTable(const CXXRecordDecl *RD,
Chris Lattner5f9e2722011-07-23 10:55:15 +00003456 raw_ostream &Out) {
Daniel Dunbarc0747712009-11-21 09:12:13 +00003457 // <special-name> ::= TV <type> # virtual table
Rafael Espindolac4850c22011-02-10 23:59:36 +00003458 CXXNameMangler Mangler(*this, Out);
Daniel Dunbarc0747712009-11-21 09:12:13 +00003459 Mangler.getStream() << "_ZTV";
Douglas Gregor1b12a3b2010-05-26 05:11:13 +00003460 Mangler.mangleNameOrStandardSubstitution(RD);
Daniel Dunbar1b077112009-11-21 09:06:10 +00003461}
Mike Stump82d75b02009-11-10 01:58:37 +00003462
Peter Collingbourne14110472011-01-13 18:57:25 +00003463void ItaniumMangleContext::mangleCXXVTT(const CXXRecordDecl *RD,
Chris Lattner5f9e2722011-07-23 10:55:15 +00003464 raw_ostream &Out) {
Daniel Dunbarc0747712009-11-21 09:12:13 +00003465 // <special-name> ::= TT <type> # VTT structure
Rafael Espindolac4850c22011-02-10 23:59:36 +00003466 CXXNameMangler Mangler(*this, Out);
Daniel Dunbarc0747712009-11-21 09:12:13 +00003467 Mangler.getStream() << "_ZTT";
Douglas Gregor1b12a3b2010-05-26 05:11:13 +00003468 Mangler.mangleNameOrStandardSubstitution(RD);
Daniel Dunbar1b077112009-11-21 09:06:10 +00003469}
Mike Stumpab3f7e92009-11-10 01:41:59 +00003470
Peter Collingbourne14110472011-01-13 18:57:25 +00003471void ItaniumMangleContext::mangleCXXCtorVTable(const CXXRecordDecl *RD,
3472 int64_t Offset,
3473 const CXXRecordDecl *Type,
Chris Lattner5f9e2722011-07-23 10:55:15 +00003474 raw_ostream &Out) {
Daniel Dunbarc0747712009-11-21 09:12:13 +00003475 // <special-name> ::= TC <type> <offset number> _ <base type>
Rafael Espindolac4850c22011-02-10 23:59:36 +00003476 CXXNameMangler Mangler(*this, Out);
Daniel Dunbarc0747712009-11-21 09:12:13 +00003477 Mangler.getStream() << "_ZTC";
Douglas Gregor1b12a3b2010-05-26 05:11:13 +00003478 Mangler.mangleNameOrStandardSubstitution(RD);
Daniel Dunbarc0747712009-11-21 09:12:13 +00003479 Mangler.getStream() << Offset;
Benjamin Kramer35f59b62010-04-10 16:03:31 +00003480 Mangler.getStream() << '_';
Douglas Gregor1b12a3b2010-05-26 05:11:13 +00003481 Mangler.mangleNameOrStandardSubstitution(Type);
Daniel Dunbar1b077112009-11-21 09:06:10 +00003482}
Mike Stump738f8c22009-07-31 23:15:31 +00003483
Peter Collingbourne14110472011-01-13 18:57:25 +00003484void ItaniumMangleContext::mangleCXXRTTI(QualType Ty,
Chris Lattner5f9e2722011-07-23 10:55:15 +00003485 raw_ostream &Out) {
Daniel Dunbarc0747712009-11-21 09:12:13 +00003486 // <special-name> ::= TI <type> # typeinfo structure
Douglas Gregor154fe982009-12-23 22:04:40 +00003487 assert(!Ty.hasQualifiers() && "RTTI info cannot have top-level qualifiers");
Rafael Espindolac4850c22011-02-10 23:59:36 +00003488 CXXNameMangler Mangler(*this, Out);
Daniel Dunbarc0747712009-11-21 09:12:13 +00003489 Mangler.getStream() << "_ZTI";
3490 Mangler.mangleType(Ty);
Daniel Dunbar1b077112009-11-21 09:06:10 +00003491}
Mike Stump67795982009-11-14 00:14:13 +00003492
Peter Collingbourne14110472011-01-13 18:57:25 +00003493void ItaniumMangleContext::mangleCXXRTTIName(QualType Ty,
Chris Lattner5f9e2722011-07-23 10:55:15 +00003494 raw_ostream &Out) {
Daniel Dunbarc0747712009-11-21 09:12:13 +00003495 // <special-name> ::= TS <type> # typeinfo name (null terminated byte string)
Rafael Espindolac4850c22011-02-10 23:59:36 +00003496 CXXNameMangler Mangler(*this, Out);
Daniel Dunbarc0747712009-11-21 09:12:13 +00003497 Mangler.getStream() << "_ZTS";
3498 Mangler.mangleType(Ty);
Mike Stumpf1216772009-07-31 18:25:34 +00003499}
Peter Collingbourne14110472011-01-13 18:57:25 +00003500
3501MangleContext *clang::createItaniumMangleContext(ASTContext &Context,
David Blaikied6471f72011-09-25 23:23:43 +00003502 DiagnosticsEngine &Diags) {
Peter Collingbourne14110472011-01-13 18:57:25 +00003503 return new ItaniumMangleContext(Context, Diags);
3504}