blob: 1a663a055da715c7ac012450c57d5f056b5d8cb7 [file] [log] [blame]
Peter Collingbourne14110472011-01-13 18:57:25 +00001//===--- ItaniumMangle.cpp - Itanium C++ Name Mangling ----------*- C++ -*-===//
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// Implements C++ name mangling according to the Itanium C++ ABI,
11// which is used in GCC 3.2 and newer (and many compilers that are
12// ABI-compatible with GCC):
13//
14// http://www.codesourcery.com/public/cxx-abi/abi.html
15//
16//===----------------------------------------------------------------------===//
Peter Collingbourne14110472011-01-13 18:57:25 +000017#include "clang/AST/Mangle.h"
Douglas Gregor5f2bfd42009-02-13 00:10:09 +000018#include "clang/AST/ASTContext.h"
19#include "clang/AST/Decl.h"
20#include "clang/AST/DeclCXX.h"
Anders Carlssona40c5e42009-03-07 22:03:21 +000021#include "clang/AST/DeclObjC.h"
Anders Carlsson7a0ba872009-05-15 16:09:15 +000022#include "clang/AST/DeclTemplate.h"
Anders Carlsson50755b02009-09-27 20:11:34 +000023#include "clang/AST/ExprCXX.h"
John McCallf85e1932011-06-15 23:02:42 +000024#include "clang/AST/ExprObjC.h"
John McCallfb44de92011-05-01 22:35:37 +000025#include "clang/AST/TypeLoc.h"
Peter Collingbourne14110472011-01-13 18:57:25 +000026#include "clang/Basic/ABI.h"
Douglas Gregor6ec36682009-02-18 23:53:56 +000027#include "clang/Basic/SourceManager.h"
Rafael Espindola4e274e92011-02-15 22:23:51 +000028#include "clang/Basic/TargetInfo.h"
Anders Carlssonc4355b62009-10-07 01:45:02 +000029#include "llvm/ADT/StringExtras.h"
Douglas Gregor5f2bfd42009-02-13 00:10:09 +000030#include "llvm/Support/raw_ostream.h"
John McCallefe6aee2009-09-05 07:56:18 +000031#include "llvm/Support/ErrorHandling.h"
Anders Carlssonf98574b2010-02-05 07:31:37 +000032
33#define MANGLE_CHECKER 0
34
35#if MANGLE_CHECKER
36#include <cxxabi.h>
37#endif
38
Douglas Gregor5f2bfd42009-02-13 00:10:09 +000039using namespace clang;
Charles Davis685b1d92010-05-26 18:25:27 +000040
Douglas Gregor5f2bfd42009-02-13 00:10:09 +000041namespace {
Fariborz Jahanian57058532010-03-03 19:41:08 +000042
Douglas Gregorccc1b5e2012-02-21 00:37:24 +000043/// \brief Retrieve the declaration context that should be used when mangling
44/// the given declaration.
45static const DeclContext *getEffectiveDeclContext(const Decl *D) {
46 // The ABI assumes that lambda closure types that occur within
47 // default arguments live in the context of the function. However, due to
48 // the way in which Clang parses and creates function declarations, this is
49 // not the case: the lambda closure type ends up living in the context
50 // where the function itself resides, because the function declaration itself
51 // had not yet been created. Fix the context here.
52 if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D)) {
53 if (RD->isLambda())
54 if (ParmVarDecl *ContextParam
Douglas Gregor5878cbc2012-02-21 04:17:39 +000055 = dyn_cast_or_null<ParmVarDecl>(RD->getLambdaContextDecl()))
Douglas Gregorccc1b5e2012-02-21 00:37:24 +000056 return ContextParam->getDeclContext();
57 }
58
59 return D->getDeclContext();
60}
61
62static const DeclContext *getEffectiveParentContext(const DeclContext *DC) {
63 return getEffectiveDeclContext(cast<Decl>(DC));
64}
65
John McCall82b7d7b2010-10-18 21:28:44 +000066static const CXXRecordDecl *GetLocalClassDecl(const NamedDecl *ND) {
67 const DeclContext *DC = dyn_cast<DeclContext>(ND);
68 if (!DC)
Douglas Gregorccc1b5e2012-02-21 00:37:24 +000069 DC = getEffectiveDeclContext(ND);
John McCall82b7d7b2010-10-18 21:28:44 +000070 while (!DC->isNamespace() && !DC->isTranslationUnit()) {
Douglas Gregorccc1b5e2012-02-21 00:37:24 +000071 const DeclContext *Parent = getEffectiveDeclContext(cast<Decl>(DC));
72 if (isa<FunctionDecl>(Parent))
John McCall82b7d7b2010-10-18 21:28:44 +000073 return dyn_cast<CXXRecordDecl>(DC);
Douglas Gregorccc1b5e2012-02-21 00:37:24 +000074 DC = Parent;
Fariborz Jahanian57058532010-03-03 19:41:08 +000075 }
76 return 0;
77}
78
John McCallfb44de92011-05-01 22:35:37 +000079static const FunctionDecl *getStructor(const FunctionDecl *fn) {
80 if (const FunctionTemplateDecl *ftd = fn->getPrimaryTemplate())
81 return ftd->getTemplatedDecl();
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +000082
John McCallfb44de92011-05-01 22:35:37 +000083 return fn;
84}
Anders Carlsson7e120032009-11-24 05:36:32 +000085
John McCallfb44de92011-05-01 22:35:37 +000086static const NamedDecl *getStructor(const NamedDecl *decl) {
87 const FunctionDecl *fn = dyn_cast_or_null<FunctionDecl>(decl);
88 return (fn ? getStructor(fn) : decl);
Anders Carlsson7e120032009-11-24 05:36:32 +000089}
Douglas Gregorccc1b5e2012-02-21 00:37:24 +000090
John McCall1dd73832010-02-04 01:42:13 +000091static const unsigned UnknownArity = ~0U;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +000092
Peter Collingbourne14110472011-01-13 18:57:25 +000093class ItaniumMangleContext : public MangleContext {
94 llvm::DenseMap<const TagDecl *, uint64_t> AnonStructIds;
95 unsigned Discriminator;
96 llvm::DenseMap<const NamedDecl*, unsigned> Uniquifier;
97
98public:
99 explicit ItaniumMangleContext(ASTContext &Context,
David Blaikied6471f72011-09-25 23:23:43 +0000100 DiagnosticsEngine &Diags)
Peter Collingbourne14110472011-01-13 18:57:25 +0000101 : MangleContext(Context, Diags) { }
102
103 uint64_t getAnonymousStructId(const TagDecl *TD) {
104 std::pair<llvm::DenseMap<const TagDecl *,
105 uint64_t>::iterator, bool> Result =
106 AnonStructIds.insert(std::make_pair(TD, AnonStructIds.size()));
107 return Result.first->second;
108 }
109
110 void startNewFunction() {
111 MangleContext::startNewFunction();
112 mangleInitDiscriminator();
113 }
114
115 /// @name Mangler Entry Points
116 /// @{
117
118 bool shouldMangleDeclName(const NamedDecl *D);
Chris Lattner5f9e2722011-07-23 10:55:15 +0000119 void mangleName(const NamedDecl *D, raw_ostream &);
Peter Collingbourne14110472011-01-13 18:57:25 +0000120 void mangleThunk(const CXXMethodDecl *MD,
121 const ThunkInfo &Thunk,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000122 raw_ostream &);
Peter Collingbourne14110472011-01-13 18:57:25 +0000123 void mangleCXXDtorThunk(const CXXDestructorDecl *DD, CXXDtorType Type,
124 const ThisAdjustment &ThisAdjustment,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000125 raw_ostream &);
Peter Collingbourne14110472011-01-13 18:57:25 +0000126 void mangleReferenceTemporary(const VarDecl *D,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000127 raw_ostream &);
Peter Collingbourne14110472011-01-13 18:57:25 +0000128 void mangleCXXVTable(const CXXRecordDecl *RD,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000129 raw_ostream &);
Peter Collingbourne14110472011-01-13 18:57:25 +0000130 void mangleCXXVTT(const CXXRecordDecl *RD,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000131 raw_ostream &);
Peter Collingbourne14110472011-01-13 18:57:25 +0000132 void mangleCXXCtorVTable(const CXXRecordDecl *RD, int64_t Offset,
133 const CXXRecordDecl *Type,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000134 raw_ostream &);
135 void mangleCXXRTTI(QualType T, raw_ostream &);
136 void mangleCXXRTTIName(QualType T, raw_ostream &);
Peter Collingbourne14110472011-01-13 18:57:25 +0000137 void mangleCXXCtor(const CXXConstructorDecl *D, CXXCtorType Type,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000138 raw_ostream &);
Peter Collingbourne14110472011-01-13 18:57:25 +0000139 void mangleCXXDtor(const CXXDestructorDecl *D, CXXDtorType Type,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000140 raw_ostream &);
Peter Collingbourne14110472011-01-13 18:57:25 +0000141
Chris Lattner5f9e2722011-07-23 10:55:15 +0000142 void mangleItaniumGuardVariable(const VarDecl *D, raw_ostream &);
Peter Collingbourne14110472011-01-13 18:57:25 +0000143
144 void mangleInitDiscriminator() {
145 Discriminator = 0;
146 }
147
148 bool getNextDiscriminator(const NamedDecl *ND, unsigned &disc) {
Douglas Gregor9e8c92a2012-02-20 19:44:39 +0000149 // Lambda closure types with external linkage (indicated by a
150 // non-zero lambda mangling number) have their own numbering scheme, so
151 // they do not need a discriminator.
152 if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(ND))
153 if (RD->isLambda() && RD->getLambdaManglingNumber() > 0)
154 return false;
155
Peter Collingbourne14110472011-01-13 18:57:25 +0000156 unsigned &discriminator = Uniquifier[ND];
157 if (!discriminator)
158 discriminator = ++Discriminator;
159 if (discriminator == 1)
160 return false;
161 disc = discriminator-2;
162 return true;
163 }
164 /// @}
165};
166
Daniel Dunbar1b077112009-11-21 09:06:10 +0000167/// CXXNameMangler - Manage the mangling of a single name.
Daniel Dunbarc0747712009-11-21 09:12:13 +0000168class CXXNameMangler {
Peter Collingbourne14110472011-01-13 18:57:25 +0000169 ItaniumMangleContext &Context;
Chris Lattner5f9e2722011-07-23 10:55:15 +0000170 raw_ostream &Out;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000171
John McCallfb44de92011-05-01 22:35:37 +0000172 /// The "structor" is the top-level declaration being mangled, if
173 /// that's not a template specialization; otherwise it's the pattern
174 /// for that specialization.
175 const NamedDecl *Structor;
Daniel Dunbar1b077112009-11-21 09:06:10 +0000176 unsigned StructorType;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000177
Anders Carlsson9d85b722010-06-02 04:29:50 +0000178 /// SeqID - The next subsitution sequence number.
179 unsigned SeqID;
180
John McCallfb44de92011-05-01 22:35:37 +0000181 class FunctionTypeDepthState {
182 unsigned Bits;
183
184 enum { InResultTypeMask = 1 };
185
186 public:
187 FunctionTypeDepthState() : Bits(0) {}
188
189 /// The number of function types we're inside.
190 unsigned getDepth() const {
191 return Bits >> 1;
192 }
193
194 /// True if we're in the return type of the innermost function type.
195 bool isInResultType() const {
196 return Bits & InResultTypeMask;
197 }
198
199 FunctionTypeDepthState push() {
200 FunctionTypeDepthState tmp = *this;
201 Bits = (Bits & ~InResultTypeMask) + 2;
202 return tmp;
203 }
204
205 void enterResultType() {
206 Bits |= InResultTypeMask;
207 }
208
209 void leaveResultType() {
210 Bits &= ~InResultTypeMask;
211 }
212
213 void pop(FunctionTypeDepthState saved) {
214 assert(getDepth() == saved.getDepth() + 1);
215 Bits = saved.Bits;
216 }
217
218 } FunctionTypeDepth;
219
Daniel Dunbar1b077112009-11-21 09:06:10 +0000220 llvm::DenseMap<uintptr_t, unsigned> Substitutions;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000221
John McCall1dd73832010-02-04 01:42:13 +0000222 ASTContext &getASTContext() const { return Context.getASTContext(); }
223
Daniel Dunbarc0747712009-11-21 09:12:13 +0000224public:
Chris Lattner5f9e2722011-07-23 10:55:15 +0000225 CXXNameMangler(ItaniumMangleContext &C, raw_ostream &Out_,
John McCallfb44de92011-05-01 22:35:37 +0000226 const NamedDecl *D = 0)
227 : Context(C), Out(Out_), Structor(getStructor(D)), StructorType(0),
228 SeqID(0) {
229 // These can't be mangled without a ctor type or dtor type.
230 assert(!D || (!isa<CXXDestructorDecl>(D) &&
231 !isa<CXXConstructorDecl>(D)));
232 }
Chris Lattner5f9e2722011-07-23 10:55:15 +0000233 CXXNameMangler(ItaniumMangleContext &C, raw_ostream &Out_,
Daniel Dunbar77939c92009-11-21 09:06:31 +0000234 const CXXConstructorDecl *D, CXXCtorType Type)
Rafael Espindolac4850c22011-02-10 23:59:36 +0000235 : Context(C), Out(Out_), Structor(getStructor(D)), StructorType(Type),
John McCallfb44de92011-05-01 22:35:37 +0000236 SeqID(0) { }
Chris Lattner5f9e2722011-07-23 10:55:15 +0000237 CXXNameMangler(ItaniumMangleContext &C, raw_ostream &Out_,
Daniel Dunbar77939c92009-11-21 09:06:31 +0000238 const CXXDestructorDecl *D, CXXDtorType Type)
Rafael Espindolac4850c22011-02-10 23:59:36 +0000239 : Context(C), Out(Out_), Structor(getStructor(D)), StructorType(Type),
John McCallfb44de92011-05-01 22:35:37 +0000240 SeqID(0) { }
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000241
Anders Carlssonf98574b2010-02-05 07:31:37 +0000242#if MANGLE_CHECKER
243 ~CXXNameMangler() {
244 if (Out.str()[0] == '\01')
245 return;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000246
Anders Carlssonf98574b2010-02-05 07:31:37 +0000247 int status = 0;
248 char *result = abi::__cxa_demangle(Out.str().str().c_str(), 0, 0, &status);
249 assert(status == 0 && "Could not demangle mangled name!");
250 free(result);
251 }
252#endif
Chris Lattner5f9e2722011-07-23 10:55:15 +0000253 raw_ostream &getStream() { return Out; }
Daniel Dunbarc0747712009-11-21 09:12:13 +0000254
Chris Lattner5f9e2722011-07-23 10:55:15 +0000255 void mangle(const NamedDecl *D, StringRef Prefix = "_Z");
Anders Carlsson19879c92010-03-23 17:17:29 +0000256 void mangleCallOffset(int64_t NonVirtual, int64_t Virtual);
John McCall0512e482010-07-14 04:20:34 +0000257 void mangleNumber(const llvm::APSInt &I);
Anders Carlssona94822e2009-11-26 02:32:05 +0000258 void mangleNumber(int64_t Number);
John McCall0512e482010-07-14 04:20:34 +0000259 void mangleFloat(const llvm::APFloat &F);
Daniel Dunbarc0747712009-11-21 09:12:13 +0000260 void mangleFunctionEncoding(const FunctionDecl *FD);
261 void mangleName(const NamedDecl *ND);
262 void mangleType(QualType T);
Douglas Gregor1b12a3b2010-05-26 05:11:13 +0000263 void mangleNameOrStandardSubstitution(const NamedDecl *ND);
264
Daniel Dunbarc0747712009-11-21 09:12:13 +0000265private:
Daniel Dunbar1b077112009-11-21 09:06:10 +0000266 bool mangleSubstitution(const NamedDecl *ND);
267 bool mangleSubstitution(QualType T);
Douglas Gregor1e9268e2010-04-28 05:58:56 +0000268 bool mangleSubstitution(TemplateName Template);
Daniel Dunbar1b077112009-11-21 09:06:10 +0000269 bool mangleSubstitution(uintptr_t Ptr);
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000270
John McCall68a51a72011-07-01 00:04:39 +0000271 void mangleExistingSubstitution(QualType type);
272 void mangleExistingSubstitution(TemplateName name);
273
Daniel Dunbar1b077112009-11-21 09:06:10 +0000274 bool mangleStandardSubstitution(const NamedDecl *ND);
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000275
Daniel Dunbar1b077112009-11-21 09:06:10 +0000276 void addSubstitution(const NamedDecl *ND) {
277 ND = cast<NamedDecl>(ND->getCanonicalDecl());
Anders Carlsson433d1372009-11-07 04:26:04 +0000278
Daniel Dunbar1b077112009-11-21 09:06:10 +0000279 addSubstitution(reinterpret_cast<uintptr_t>(ND));
280 }
281 void addSubstitution(QualType T);
Douglas Gregor1e9268e2010-04-28 05:58:56 +0000282 void addSubstitution(TemplateName Template);
Daniel Dunbar1b077112009-11-21 09:06:10 +0000283 void addSubstitution(uintptr_t Ptr);
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000284
John McCalla0ce15c2011-04-24 08:23:24 +0000285 void mangleUnresolvedPrefix(NestedNameSpecifier *qualifier,
286 NamedDecl *firstQualifierLookup,
287 bool recursive = false);
288 void mangleUnresolvedName(NestedNameSpecifier *qualifier,
289 NamedDecl *firstQualifierLookup,
290 DeclarationName name,
John McCall1dd73832010-02-04 01:42:13 +0000291 unsigned KnownArity = UnknownArity);
292
Daniel Dunbar1b077112009-11-21 09:06:10 +0000293 void mangleName(const TemplateDecl *TD,
294 const TemplateArgument *TemplateArgs,
295 unsigned NumTemplateArgs);
John McCall1dd73832010-02-04 01:42:13 +0000296 void mangleUnqualifiedName(const NamedDecl *ND) {
297 mangleUnqualifiedName(ND, ND->getDeclName(), UnknownArity);
298 }
299 void mangleUnqualifiedName(const NamedDecl *ND, DeclarationName Name,
300 unsigned KnownArity);
Daniel Dunbar1b077112009-11-21 09:06:10 +0000301 void mangleUnscopedName(const NamedDecl *ND);
302 void mangleUnscopedTemplateName(const TemplateDecl *ND);
Douglas Gregor1e9268e2010-04-28 05:58:56 +0000303 void mangleUnscopedTemplateName(TemplateName);
Daniel Dunbar1b077112009-11-21 09:06:10 +0000304 void mangleSourceName(const IdentifierInfo *II);
305 void mangleLocalName(const NamedDecl *ND);
Douglas Gregorccc1b5e2012-02-21 00:37:24 +0000306 void mangleLambda(const CXXRecordDecl *Lambda);
Fariborz Jahanian57058532010-03-03 19:41:08 +0000307 void mangleNestedName(const NamedDecl *ND, const DeclContext *DC,
308 bool NoFunction=false);
Daniel Dunbar1b077112009-11-21 09:06:10 +0000309 void mangleNestedName(const TemplateDecl *TD,
310 const TemplateArgument *TemplateArgs,
311 unsigned NumTemplateArgs);
John McCalla0ce15c2011-04-24 08:23:24 +0000312 void manglePrefix(NestedNameSpecifier *qualifier);
Fariborz Jahanian57058532010-03-03 19:41:08 +0000313 void manglePrefix(const DeclContext *DC, bool NoFunction=false);
John McCall4f4e4132011-05-04 01:45:19 +0000314 void manglePrefix(QualType type);
Daniel Dunbar1b077112009-11-21 09:06:10 +0000315 void mangleTemplatePrefix(const TemplateDecl *ND);
Douglas Gregor20f0cc72010-04-23 03:10:43 +0000316 void mangleTemplatePrefix(TemplateName Template);
Daniel Dunbar1b077112009-11-21 09:06:10 +0000317 void mangleOperatorName(OverloadedOperatorKind OO, unsigned Arity);
318 void mangleQualifiers(Qualifiers Quals);
Douglas Gregor0a9a6d62011-01-26 17:36:28 +0000319 void mangleRefQualifier(RefQualifierKind RefQualifier);
John McCallefe6aee2009-09-05 07:56:18 +0000320
Anders Carlsson7b06f6c2009-12-10 03:14:39 +0000321 void mangleObjCMethodName(const ObjCMethodDecl *MD);
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000322
Daniel Dunbar1b077112009-11-21 09:06:10 +0000323 // Declare manglers for every type class.
John McCallefe6aee2009-09-05 07:56:18 +0000324#define ABSTRACT_TYPE(CLASS, PARENT)
325#define NON_CANONICAL_TYPE(CLASS, PARENT)
326#define TYPE(CLASS, PARENT) void mangleType(const CLASS##Type *T);
327#include "clang/AST/TypeNodes.def"
328
Daniel Dunbar1b077112009-11-21 09:06:10 +0000329 void mangleType(const TagType*);
John McCallb6f532e2010-07-14 06:43:17 +0000330 void mangleType(TemplateName);
Daniel Dunbar1b077112009-11-21 09:06:10 +0000331 void mangleBareFunctionType(const FunctionType *T,
332 bool MangleReturnType);
Bob Wilson57147a82010-11-16 00:32:18 +0000333 void mangleNeonVectorType(const VectorType *T);
Anders Carlssone170ba72009-12-14 01:45:37 +0000334
335 void mangleIntegerLiteral(QualType T, const llvm::APSInt &Value);
John McCalla0ce15c2011-04-24 08:23:24 +0000336 void mangleMemberExpr(const Expr *base, bool isArrow,
337 NestedNameSpecifier *qualifier,
338 NamedDecl *firstQualifierLookup,
339 DeclarationName name,
340 unsigned knownArity);
John McCall5e1e89b2010-08-18 19:18:59 +0000341 void mangleExpression(const Expr *E, unsigned Arity = UnknownArity);
Daniel Dunbar1b077112009-11-21 09:06:10 +0000342 void mangleCXXCtorType(CXXCtorType T);
343 void mangleCXXDtorType(CXXDtorType T);
Mike Stump1eb44332009-09-09 15:08:12 +0000344
Argyrios Kyrtzidisb0c3e092011-09-22 20:07:03 +0000345 void mangleTemplateArgs(const ASTTemplateArgumentListInfo &TemplateArgs);
Douglas Gregor20f0cc72010-04-23 03:10:43 +0000346 void mangleTemplateArgs(TemplateName Template,
347 const TemplateArgument *TemplateArgs,
Sean Huntc3021132010-05-05 15:23:54 +0000348 unsigned NumTemplateArgs);
Rafael Espindolad9800722010-03-11 14:07:00 +0000349 void mangleTemplateArgs(const TemplateParameterList &PL,
350 const TemplateArgument *TemplateArgs,
Daniel Dunbar1b077112009-11-21 09:06:10 +0000351 unsigned NumTemplateArgs);
Rafael Espindolad9800722010-03-11 14:07:00 +0000352 void mangleTemplateArgs(const TemplateParameterList &PL,
353 const TemplateArgumentList &AL);
Douglas Gregorf1588662011-07-12 15:18:55 +0000354 void mangleTemplateArg(const NamedDecl *P, TemplateArgument A);
John McCall4f4e4132011-05-04 01:45:19 +0000355 void mangleUnresolvedTemplateArgs(const TemplateArgument *args,
356 unsigned numArgs);
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000357
Daniel Dunbar1b077112009-11-21 09:06:10 +0000358 void mangleTemplateParameter(unsigned Index);
John McCallfb44de92011-05-01 22:35:37 +0000359
360 void mangleFunctionParam(const ParmVarDecl *parm);
Daniel Dunbar1b077112009-11-21 09:06:10 +0000361};
Peter Collingbourne14110472011-01-13 18:57:25 +0000362
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000363}
364
Anders Carlsson43f17402009-04-02 15:51:53 +0000365static bool isInCLinkageSpecification(const Decl *D) {
Douglas Gregor457e2812009-10-28 16:31:34 +0000366 D = D->getCanonicalDecl();
Douglas Gregorccc1b5e2012-02-21 00:37:24 +0000367 for (const DeclContext *DC = getEffectiveDeclContext(D);
368 !DC->isTranslationUnit(); DC = getEffectiveParentContext(DC)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000369 if (const LinkageSpecDecl *Linkage = dyn_cast<LinkageSpecDecl>(DC))
Anders Carlsson43f17402009-04-02 15:51:53 +0000370 return Linkage->getLanguage() == LinkageSpecDecl::lang_c;
371 }
Mike Stump1eb44332009-09-09 15:08:12 +0000372
Anders Carlsson43f17402009-04-02 15:51:53 +0000373 return false;
374}
375
Peter Collingbourne14110472011-01-13 18:57:25 +0000376bool ItaniumMangleContext::shouldMangleDeclName(const NamedDecl *D) {
Daniel Dunbarf981bf82009-11-21 09:14:52 +0000377 // In C, functions with no attributes never need to be mangled. Fastpath them.
David Blaikie4e4d0842012-03-11 07:00:24 +0000378 if (!getASTContext().getLangOpts().CPlusPlus && !D->hasAttrs())
Daniel Dunbarf981bf82009-11-21 09:14:52 +0000379 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.
David Blaikie4e4d0842012-03-11 07:00:24 +0000395 if (!getASTContext().getLangOpts().CPlusPlus)
Daniel Dunbarf981bf82009-11-21 09:14:52 +0000396 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 Gregorf54486a2012-04-04 17:40:10 +0000538static bool isLambda(const NamedDecl *ND) {
539 const CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(ND);
540 if (!Record)
541 return false;
542
543 return Record->isLambda();
544}
545
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000546void CXXNameMangler::mangleName(const NamedDecl *ND) {
547 // <name> ::= <nested-name>
548 // ::= <unscoped-name>
549 // ::= <unscoped-template-name> <template-args>
Anders Carlsson201ce742009-09-17 03:17:01 +0000550 // ::= <local-name>
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000551 //
Douglas Gregorccc1b5e2012-02-21 00:37:24 +0000552 const DeclContext *DC = getEffectiveDeclContext(ND);
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000553
Eli Friedman7facf842009-12-02 20:32:49 +0000554 // If this is an extern variable declared locally, the relevant DeclContext
555 // is that of the containing namespace, or the translation unit.
Douglas Gregorf54486a2012-04-04 17:40:10 +0000556 // FIXME: This is a hack; extern variables declared locally should have
557 // a proper semantic declaration context!
558 if (isa<FunctionDecl>(DC) && ND->hasLinkage() && !isLambda(ND))
Eli Friedman7facf842009-12-02 20:32:49 +0000559 while (!DC->isNamespace() && !DC->isTranslationUnit())
Douglas Gregorccc1b5e2012-02-21 00:37:24 +0000560 DC = getEffectiveParentContext(DC);
John McCall82b7d7b2010-10-18 21:28:44 +0000561 else if (GetLocalClassDecl(ND)) {
562 mangleLocalName(ND);
563 return;
564 }
Eli Friedman7facf842009-12-02 20:32:49 +0000565
James Molloyb3c312c2012-03-05 09:59:43 +0000566 DC = IgnoreLinkageSpecDecls(DC);
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000567
Anders Carlssond58d6f72009-09-17 16:12:20 +0000568 if (DC->isTranslationUnit() || isStdNamespace(DC)) {
Anders Carlsson2744a062009-09-18 19:00:18 +0000569 // Check if we have a template.
570 const TemplateArgumentList *TemplateArgs = 0;
Anders Carlsson0fa6df42009-09-26 19:45:45 +0000571 if (const TemplateDecl *TD = isTemplate(ND, TemplateArgs)) {
Anders Carlsson2744a062009-09-18 19:00:18 +0000572 mangleUnscopedTemplateName(TD);
Rafael Espindolad9800722010-03-11 14:07:00 +0000573 TemplateParameterList *TemplateParameters = TD->getTemplateParameters();
574 mangleTemplateArgs(*TemplateParameters, *TemplateArgs);
Anders Carlsson2744a062009-09-18 19:00:18 +0000575 return;
Anders Carlsson7482e242009-09-18 04:29:09 +0000576 }
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000577
Anders Carlsson7482e242009-09-18 04:29:09 +0000578 mangleUnscopedName(ND);
579 return;
580 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000581
Anders Carlsson7b06f6c2009-12-10 03:14:39 +0000582 if (isa<FunctionDecl>(DC) || isa<ObjCMethodDecl>(DC)) {
Anders Carlsson7482e242009-09-18 04:29:09 +0000583 mangleLocalName(ND);
584 return;
585 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000586
Eli Friedman7facf842009-12-02 20:32:49 +0000587 mangleNestedName(ND, DC);
Anders Carlsson7482e242009-09-18 04:29:09 +0000588}
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000589void CXXNameMangler::mangleName(const TemplateDecl *TD,
Anders Carlsson7624f212009-09-18 02:42:01 +0000590 const TemplateArgument *TemplateArgs,
591 unsigned NumTemplateArgs) {
Douglas Gregorccc1b5e2012-02-21 00:37:24 +0000592 const DeclContext *DC = IgnoreLinkageSpecDecls(getEffectiveDeclContext(TD));
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000593
Anders Carlsson7624f212009-09-18 02:42:01 +0000594 if (DC->isTranslationUnit() || isStdNamespace(DC)) {
Anders Carlsson0fa6df42009-09-26 19:45:45 +0000595 mangleUnscopedTemplateName(TD);
Rafael Espindolad9800722010-03-11 14:07:00 +0000596 TemplateParameterList *TemplateParameters = TD->getTemplateParameters();
597 mangleTemplateArgs(*TemplateParameters, TemplateArgs, NumTemplateArgs);
Anders Carlsson7624f212009-09-18 02:42:01 +0000598 } else {
599 mangleNestedName(TD, TemplateArgs, NumTemplateArgs);
600 }
601}
602
Anders Carlsson201ce742009-09-17 03:17:01 +0000603void CXXNameMangler::mangleUnscopedName(const NamedDecl *ND) {
604 // <unscoped-name> ::= <unqualified-name>
605 // ::= St <unqualified-name> # ::std::
James Molloyb3c312c2012-03-05 09:59:43 +0000606
607 if (isStdNamespace(IgnoreLinkageSpecDecls(getEffectiveDeclContext(ND))))
Anders Carlsson201ce742009-09-17 03:17:01 +0000608 Out << "St";
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000609
Anders Carlsson201ce742009-09-17 03:17:01 +0000610 mangleUnqualifiedName(ND);
611}
612
Anders Carlsson0fa6df42009-09-26 19:45:45 +0000613void CXXNameMangler::mangleUnscopedTemplateName(const TemplateDecl *ND) {
Anders Carlsson201ce742009-09-17 03:17:01 +0000614 // <unscoped-template-name> ::= <unscoped-name>
615 // ::= <substitution>
Anders Carlsson7624f212009-09-18 02:42:01 +0000616 if (mangleSubstitution(ND))
Anders Carlsson03c9d532009-09-17 04:02:31 +0000617 return;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000618
Douglas Gregor32fb4e12010-02-05 20:45:00 +0000619 // <template-template-param> ::= <template-param>
620 if (const TemplateTemplateParmDecl *TTP
621 = dyn_cast<TemplateTemplateParmDecl>(ND)) {
622 mangleTemplateParameter(TTP->getIndex());
623 return;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000624 }
Douglas Gregor32fb4e12010-02-05 20:45:00 +0000625
Anders Carlsson1668f202009-09-26 20:13:56 +0000626 mangleUnscopedName(ND->getTemplatedDecl());
Anders Carlsson7624f212009-09-18 02:42:01 +0000627 addSubstitution(ND);
Anders Carlsson201ce742009-09-17 03:17:01 +0000628}
629
Douglas Gregor1e9268e2010-04-28 05:58:56 +0000630void CXXNameMangler::mangleUnscopedTemplateName(TemplateName Template) {
631 // <unscoped-template-name> ::= <unscoped-name>
632 // ::= <substitution>
633 if (TemplateDecl *TD = Template.getAsTemplateDecl())
634 return mangleUnscopedTemplateName(TD);
Sean Huntc3021132010-05-05 15:23:54 +0000635
Douglas Gregor1e9268e2010-04-28 05:58:56 +0000636 if (mangleSubstitution(Template))
637 return;
638
Douglas Gregor1e9268e2010-04-28 05:58:56 +0000639 DependentTemplateName *Dependent = Template.getAsDependentTemplateName();
640 assert(Dependent && "Not a dependent template name?");
Douglas Gregor19617912011-07-12 05:06:05 +0000641 if (const IdentifierInfo *Id = Dependent->getIdentifier())
642 mangleSourceName(Id);
643 else
644 mangleOperatorName(Dependent->getOperator(), UnknownArity);
Sean Huntc3021132010-05-05 15:23:54 +0000645
Douglas Gregor1e9268e2010-04-28 05:58:56 +0000646 addSubstitution(Template);
647}
648
John McCall1b600522011-04-24 03:07:16 +0000649void CXXNameMangler::mangleFloat(const llvm::APFloat &f) {
650 // ABI:
651 // Floating-point literals are encoded using a fixed-length
652 // lowercase hexadecimal string corresponding to the internal
653 // representation (IEEE on Itanium), high-order bytes first,
654 // without leading zeroes. For example: "Lf bf800000 E" is -1.0f
655 // on Itanium.
John McCall0c8731a2012-01-30 18:36:31 +0000656 // The 'without leading zeroes' thing seems to be an editorial
657 // mistake; see the discussion on cxx-abi-dev beginning on
658 // 2012-01-16.
John McCall1b600522011-04-24 03:07:16 +0000659
Benjamin Kramer48d798c2012-06-02 10:20:41 +0000660 // Our requirements here are just barely weird enough to justify
John McCall0c8731a2012-01-30 18:36:31 +0000661 // using a custom algorithm instead of post-processing APInt::toString().
John McCall1b600522011-04-24 03:07:16 +0000662
John McCall0c8731a2012-01-30 18:36:31 +0000663 llvm::APInt valueBits = f.bitcastToAPInt();
664 unsigned numCharacters = (valueBits.getBitWidth() + 3) / 4;
665 assert(numCharacters != 0);
666
667 // Allocate a buffer of the right number of characters.
668 llvm::SmallVector<char, 20> buffer;
669 buffer.set_size(numCharacters);
670
671 // Fill the buffer left-to-right.
672 for (unsigned stringIndex = 0; stringIndex != numCharacters; ++stringIndex) {
673 // The bit-index of the next hex digit.
674 unsigned digitBitIndex = 4 * (numCharacters - stringIndex - 1);
675
676 // Project out 4 bits starting at 'digitIndex'.
677 llvm::integerPart hexDigit
678 = valueBits.getRawData()[digitBitIndex / llvm::integerPartWidth];
679 hexDigit >>= (digitBitIndex % llvm::integerPartWidth);
680 hexDigit &= 0xF;
681
682 // Map that over to a lowercase hex digit.
683 static const char charForHex[16] = {
684 '0', '1', '2', '3', '4', '5', '6', '7',
685 '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'
686 };
687 buffer[stringIndex] = charForHex[hexDigit];
688 }
689
690 Out.write(buffer.data(), numCharacters);
John McCall0512e482010-07-14 04:20:34 +0000691}
692
693void CXXNameMangler::mangleNumber(const llvm::APSInt &Value) {
694 if (Value.isSigned() && Value.isNegative()) {
695 Out << 'n';
John McCall54c86f72012-08-18 04:51:52 +0000696 Value.abs().print(Out, /*signed*/ false);
697 } else {
698 Value.print(Out, /*signed*/ false);
699 }
John McCall0512e482010-07-14 04:20:34 +0000700}
701
Anders Carlssona94822e2009-11-26 02:32:05 +0000702void CXXNameMangler::mangleNumber(int64_t Number) {
703 // <number> ::= [n] <non-negative decimal integer>
704 if (Number < 0) {
705 Out << 'n';
706 Number = -Number;
707 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000708
Anders Carlssona94822e2009-11-26 02:32:05 +0000709 Out << Number;
710}
711
Anders Carlsson19879c92010-03-23 17:17:29 +0000712void CXXNameMangler::mangleCallOffset(int64_t NonVirtual, int64_t Virtual) {
Mike Stump141c5af2009-09-02 00:25:38 +0000713 // <call-offset> ::= h <nv-offset> _
714 // ::= v <v-offset> _
715 // <nv-offset> ::= <offset number> # non-virtual base override
Anders Carlssona94822e2009-11-26 02:32:05 +0000716 // <v-offset> ::= <offset number> _ <virtual offset number>
Mike Stump141c5af2009-09-02 00:25:38 +0000717 // # virtual base override, with vcall offset
Anders Carlsson19879c92010-03-23 17:17:29 +0000718 if (!Virtual) {
Anders Carlssona94822e2009-11-26 02:32:05 +0000719 Out << 'h';
Anders Carlsson19879c92010-03-23 17:17:29 +0000720 mangleNumber(NonVirtual);
Anders Carlssona94822e2009-11-26 02:32:05 +0000721 Out << '_';
722 return;
Mike Stump141c5af2009-09-02 00:25:38 +0000723 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000724
Anders Carlssona94822e2009-11-26 02:32:05 +0000725 Out << 'v';
Anders Carlsson19879c92010-03-23 17:17:29 +0000726 mangleNumber(NonVirtual);
Anders Carlssona94822e2009-11-26 02:32:05 +0000727 Out << '_';
Anders Carlsson19879c92010-03-23 17:17:29 +0000728 mangleNumber(Virtual);
Anders Carlssona94822e2009-11-26 02:32:05 +0000729 Out << '_';
Mike Stump9124bcc2009-09-02 00:56:18 +0000730}
731
John McCall4f4e4132011-05-04 01:45:19 +0000732void CXXNameMangler::manglePrefix(QualType type) {
John McCalla0ce15c2011-04-24 08:23:24 +0000733 if (const TemplateSpecializationType *TST =
734 type->getAs<TemplateSpecializationType>()) {
735 if (!mangleSubstitution(QualType(TST, 0))) {
736 mangleTemplatePrefix(TST->getTemplateName());
Sean Huntc3021132010-05-05 15:23:54 +0000737
Douglas Gregoraa2187d2011-02-28 00:04:36 +0000738 // FIXME: GCC does not appear to mangle the template arguments when
739 // the template in question is a dependent template name. Should we
740 // emulate that badness?
John McCalla0ce15c2011-04-24 08:23:24 +0000741 mangleTemplateArgs(TST->getTemplateName(), TST->getArgs(),
742 TST->getNumArgs());
743 addSubstitution(QualType(TST, 0));
Rafael Espindola9b35b252010-03-17 04:28:11 +0000744 }
John McCalla0ce15c2011-04-24 08:23:24 +0000745 } else if (const DependentTemplateSpecializationType *DTST
746 = type->getAs<DependentTemplateSpecializationType>()) {
747 TemplateName Template
748 = getASTContext().getDependentTemplateName(DTST->getQualifier(),
749 DTST->getIdentifier());
750 mangleTemplatePrefix(Template);
751
752 // FIXME: GCC does not appear to mangle the template arguments when
753 // the template in question is a dependent template name. Should we
754 // emulate that badness?
755 mangleTemplateArgs(Template, DTST->getArgs(), DTST->getNumArgs());
756 } else {
757 // We use the QualType mangle type variant here because it handles
758 // substitutions.
759 mangleType(type);
John McCall1dd73832010-02-04 01:42:13 +0000760 }
761}
762
John McCalla0ce15c2011-04-24 08:23:24 +0000763/// Mangle everything prior to the base-unresolved-name in an unresolved-name.
764///
765/// \param firstQualifierLookup - the entity found by unqualified lookup
766/// for the first name in the qualifier, if this is for a member expression
767/// \param recursive - true if this is being called recursively,
768/// i.e. if there is more prefix "to the right".
769void CXXNameMangler::mangleUnresolvedPrefix(NestedNameSpecifier *qualifier,
770 NamedDecl *firstQualifierLookup,
771 bool recursive) {
John McCall1dd73832010-02-04 01:42:13 +0000772
John McCalla0ce15c2011-04-24 08:23:24 +0000773 // x, ::x
774 // <unresolved-name> ::= [gs] <base-unresolved-name>
775
776 // T::x / decltype(p)::x
777 // <unresolved-name> ::= sr <unresolved-type> <base-unresolved-name>
778
779 // T::N::x /decltype(p)::N::x
780 // <unresolved-name> ::= srN <unresolved-type> <unresolved-qualifier-level>+ E
781 // <base-unresolved-name>
782
783 // A::x, N::y, A<T>::z; "gs" means leading "::"
784 // <unresolved-name> ::= [gs] sr <unresolved-qualifier-level>+ E
785 // <base-unresolved-name>
786
787 switch (qualifier->getKind()) {
788 case NestedNameSpecifier::Global:
789 Out << "gs";
790
791 // We want an 'sr' unless this is the entire NNS.
792 if (recursive)
793 Out << "sr";
794
795 // We never want an 'E' here.
796 return;
797
798 case NestedNameSpecifier::Namespace:
799 if (qualifier->getPrefix())
800 mangleUnresolvedPrefix(qualifier->getPrefix(), firstQualifierLookup,
801 /*recursive*/ true);
802 else
803 Out << "sr";
804 mangleSourceName(qualifier->getAsNamespace()->getIdentifier());
805 break;
806 case NestedNameSpecifier::NamespaceAlias:
807 if (qualifier->getPrefix())
808 mangleUnresolvedPrefix(qualifier->getPrefix(), firstQualifierLookup,
809 /*recursive*/ true);
810 else
811 Out << "sr";
812 mangleSourceName(qualifier->getAsNamespaceAlias()->getIdentifier());
813 break;
814
815 case NestedNameSpecifier::TypeSpec:
816 case NestedNameSpecifier::TypeSpecWithTemplate: {
John McCall4f4e4132011-05-04 01:45:19 +0000817 const Type *type = qualifier->getAsType();
John McCalla0ce15c2011-04-24 08:23:24 +0000818
John McCall4f4e4132011-05-04 01:45:19 +0000819 // We only want to use an unresolved-type encoding if this is one of:
820 // - a decltype
821 // - a template type parameter
822 // - a template template parameter with arguments
823 // In all of these cases, we should have no prefix.
824 if (qualifier->getPrefix()) {
825 mangleUnresolvedPrefix(qualifier->getPrefix(), firstQualifierLookup,
826 /*recursive*/ true);
827 } else {
828 // Otherwise, all the cases want this.
829 Out << "sr";
John McCall4f4e4132011-05-04 01:45:19 +0000830 }
831
John McCall4f4e4132011-05-04 01:45:19 +0000832 // Only certain other types are valid as prefixes; enumerate them.
John McCalld3d49bb2011-06-28 16:49:23 +0000833 switch (type->getTypeClass()) {
834 case Type::Builtin:
835 case Type::Complex:
836 case Type::Pointer:
837 case Type::BlockPointer:
838 case Type::LValueReference:
839 case Type::RValueReference:
840 case Type::MemberPointer:
841 case Type::ConstantArray:
842 case Type::IncompleteArray:
843 case Type::VariableArray:
844 case Type::DependentSizedArray:
845 case Type::DependentSizedExtVector:
846 case Type::Vector:
847 case Type::ExtVector:
848 case Type::FunctionProto:
849 case Type::FunctionNoProto:
850 case Type::Enum:
851 case Type::Paren:
852 case Type::Elaborated:
853 case Type::Attributed:
854 case Type::Auto:
855 case Type::PackExpansion:
John McCalld3d49bb2011-06-28 16:49:23 +0000856 case Type::ObjCObject:
857 case Type::ObjCInterface:
858 case Type::ObjCObjectPointer:
Eli Friedmanb001de72011-10-06 23:00:33 +0000859 case Type::Atomic:
John McCalld3d49bb2011-06-28 16:49:23 +0000860 llvm_unreachable("type is illegal as a nested name specifier");
861
John McCall68a51a72011-07-01 00:04:39 +0000862 case Type::SubstTemplateTypeParmPack:
863 // FIXME: not clear how to mangle this!
864 // template <class T...> class A {
865 // template <class U...> void foo(decltype(T::foo(U())) x...);
866 // };
867 Out << "_SUBSTPACK_";
868 break;
869
John McCalld3d49bb2011-06-28 16:49:23 +0000870 // <unresolved-type> ::= <template-param>
871 // ::= <decltype>
872 // ::= <template-template-param> <template-args>
873 // (this last is not official yet)
874 case Type::TypeOfExpr:
875 case Type::TypeOf:
876 case Type::Decltype:
877 case Type::TemplateTypeParm:
878 case Type::UnaryTransform:
John McCall35ee32e2011-07-01 02:19:08 +0000879 case Type::SubstTemplateTypeParm:
John McCalld3d49bb2011-06-28 16:49:23 +0000880 unresolvedType:
881 assert(!qualifier->getPrefix());
882
883 // We only get here recursively if we're followed by identifiers.
884 if (recursive) Out << 'N';
885
John McCall35ee32e2011-07-01 02:19:08 +0000886 // This seems to do everything we want. It's not really
887 // sanctioned for a substituted template parameter, though.
John McCalld3d49bb2011-06-28 16:49:23 +0000888 mangleType(QualType(type, 0));
889
890 // We never want to print 'E' directly after an unresolved-type,
891 // so we return directly.
892 return;
893
John McCalld3d49bb2011-06-28 16:49:23 +0000894 case Type::Typedef:
895 mangleSourceName(cast<TypedefType>(type)->getDecl()->getIdentifier());
896 break;
897
898 case Type::UnresolvedUsing:
899 mangleSourceName(cast<UnresolvedUsingType>(type)->getDecl()
900 ->getIdentifier());
901 break;
902
903 case Type::Record:
904 mangleSourceName(cast<RecordType>(type)->getDecl()->getIdentifier());
905 break;
906
907 case Type::TemplateSpecialization: {
908 const TemplateSpecializationType *tst
909 = cast<TemplateSpecializationType>(type);
John McCall68a51a72011-07-01 00:04:39 +0000910 TemplateName name = tst->getTemplateName();
911 switch (name.getKind()) {
912 case TemplateName::Template:
913 case TemplateName::QualifiedTemplate: {
914 TemplateDecl *temp = name.getAsTemplateDecl();
John McCalld3d49bb2011-06-28 16:49:23 +0000915
John McCall68a51a72011-07-01 00:04:39 +0000916 // If the base is a template template parameter, this is an
917 // unresolved type.
918 assert(temp && "no template for template specialization type");
919 if (isa<TemplateTemplateParmDecl>(temp)) goto unresolvedType;
John McCalld3d49bb2011-06-28 16:49:23 +0000920
John McCall68a51a72011-07-01 00:04:39 +0000921 mangleSourceName(temp->getIdentifier());
922 break;
923 }
924
925 case TemplateName::OverloadedTemplate:
926 case TemplateName::DependentTemplate:
927 llvm_unreachable("invalid base for a template specialization type");
928
929 case TemplateName::SubstTemplateTemplateParm: {
930 SubstTemplateTemplateParmStorage *subst
931 = name.getAsSubstTemplateTemplateParm();
932 mangleExistingSubstitution(subst->getReplacement());
933 break;
934 }
935
936 case TemplateName::SubstTemplateTemplateParmPack: {
937 // FIXME: not clear how to mangle this!
938 // template <template <class U> class T...> class A {
939 // template <class U...> void foo(decltype(T<U>::foo) x...);
940 // };
941 Out << "_SUBSTPACK_";
942 break;
943 }
944 }
945
John McCall4f4e4132011-05-04 01:45:19 +0000946 mangleUnresolvedTemplateArgs(tst->getArgs(), tst->getNumArgs());
John McCalld3d49bb2011-06-28 16:49:23 +0000947 break;
948 }
949
950 case Type::InjectedClassName:
951 mangleSourceName(cast<InjectedClassNameType>(type)->getDecl()
952 ->getIdentifier());
953 break;
954
955 case Type::DependentName:
956 mangleSourceName(cast<DependentNameType>(type)->getIdentifier());
957 break;
958
959 case Type::DependentTemplateSpecialization: {
960 const DependentTemplateSpecializationType *tst
961 = cast<DependentTemplateSpecializationType>(type);
John McCall4f4e4132011-05-04 01:45:19 +0000962 mangleSourceName(tst->getIdentifier());
963 mangleUnresolvedTemplateArgs(tst->getArgs(), tst->getNumArgs());
John McCalld3d49bb2011-06-28 16:49:23 +0000964 break;
965 }
John McCall4f4e4132011-05-04 01:45:19 +0000966 }
967 break;
John McCalla0ce15c2011-04-24 08:23:24 +0000968 }
969
970 case NestedNameSpecifier::Identifier:
971 // Member expressions can have these without prefixes.
972 if (qualifier->getPrefix()) {
973 mangleUnresolvedPrefix(qualifier->getPrefix(), firstQualifierLookup,
974 /*recursive*/ true);
975 } else if (firstQualifierLookup) {
976
977 // Try to make a proper qualifier out of the lookup result, and
978 // then just recurse on that.
979 NestedNameSpecifier *newQualifier;
980 if (TypeDecl *typeDecl = dyn_cast<TypeDecl>(firstQualifierLookup)) {
981 QualType type = getASTContext().getTypeDeclType(typeDecl);
982
983 // Pretend we had a different nested name specifier.
984 newQualifier = NestedNameSpecifier::Create(getASTContext(),
985 /*prefix*/ 0,
986 /*template*/ false,
987 type.getTypePtr());
988 } else if (NamespaceDecl *nspace =
989 dyn_cast<NamespaceDecl>(firstQualifierLookup)) {
990 newQualifier = NestedNameSpecifier::Create(getASTContext(),
991 /*prefix*/ 0,
992 nspace);
993 } else if (NamespaceAliasDecl *alias =
994 dyn_cast<NamespaceAliasDecl>(firstQualifierLookup)) {
995 newQualifier = NestedNameSpecifier::Create(getASTContext(),
996 /*prefix*/ 0,
997 alias);
998 } else {
999 // No sensible mangling to do here.
1000 newQualifier = 0;
1001 }
1002
1003 if (newQualifier)
1004 return mangleUnresolvedPrefix(newQualifier, /*lookup*/ 0, recursive);
1005
1006 } else {
1007 Out << "sr";
1008 }
1009
1010 mangleSourceName(qualifier->getAsIdentifier());
1011 break;
1012 }
1013
1014 // If this was the innermost part of the NNS, and we fell out to
1015 // here, append an 'E'.
1016 if (!recursive)
1017 Out << 'E';
1018}
1019
1020/// Mangle an unresolved-name, which is generally used for names which
1021/// weren't resolved to specific entities.
1022void CXXNameMangler::mangleUnresolvedName(NestedNameSpecifier *qualifier,
1023 NamedDecl *firstQualifierLookup,
1024 DeclarationName name,
1025 unsigned knownArity) {
1026 if (qualifier) mangleUnresolvedPrefix(qualifier, firstQualifierLookup);
1027 mangleUnqualifiedName(0, name, knownArity);
John McCall1dd73832010-02-04 01:42:13 +00001028}
1029
Anders Carlsson6f7e2f42010-06-08 14:49:03 +00001030static const FieldDecl *FindFirstNamedDataMember(const RecordDecl *RD) {
1031 assert(RD->isAnonymousStructOrUnion() &&
1032 "Expected anonymous struct or union!");
1033
1034 for (RecordDecl::field_iterator I = RD->field_begin(), E = RD->field_end();
1035 I != E; ++I) {
David Blaikie581deb32012-06-06 20:45:41 +00001036 if (I->getIdentifier())
1037 return *I;
Anders Carlsson6f7e2f42010-06-08 14:49:03 +00001038
David Blaikie581deb32012-06-06 20:45:41 +00001039 if (const RecordType *RT = I->getType()->getAs<RecordType>())
Anders Carlsson6f7e2f42010-06-08 14:49:03 +00001040 if (const FieldDecl *NamedDataMember =
1041 FindFirstNamedDataMember(RT->getDecl()))
1042 return NamedDataMember;
1043 }
Anders Carlsson6f7e2f42010-06-08 14:49:03 +00001044
1045 // We didn't find a named data member.
1046 return 0;
1047}
1048
John McCall1dd73832010-02-04 01:42:13 +00001049void CXXNameMangler::mangleUnqualifiedName(const NamedDecl *ND,
1050 DeclarationName Name,
1051 unsigned KnownArity) {
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001052 // <unqualified-name> ::= <operator-name>
Mike Stump1eb44332009-09-09 15:08:12 +00001053 // ::= <ctor-dtor-name>
1054 // ::= <source-name>
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001055 switch (Name.getNameKind()) {
Anders Carlssonc4355b62009-10-07 01:45:02 +00001056 case DeclarationName::Identifier: {
Anders Carlssonc4355b62009-10-07 01:45:02 +00001057 if (const IdentifierInfo *II = Name.getAsIdentifierInfo()) {
Sean Hunt31455252010-01-24 03:04:27 +00001058 // We must avoid conflicts between internally- and externally-
John McCall74990f42011-03-22 06:34:45 +00001059 // linked variable and function declaration names in the same TU:
1060 // void test() { extern void foo(); }
1061 // static void foo();
1062 // This naming convention is the same as that followed by GCC,
1063 // though it shouldn't actually matter.
1064 if (ND && ND->getLinkage() == InternalLinkage &&
Douglas Gregorccc1b5e2012-02-21 00:37:24 +00001065 getEffectiveDeclContext(ND)->isFileContext())
Sean Hunt31455252010-01-24 03:04:27 +00001066 Out << 'L';
1067
Anders Carlssonc4355b62009-10-07 01:45:02 +00001068 mangleSourceName(II);
1069 break;
1070 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00001071
John McCall1dd73832010-02-04 01:42:13 +00001072 // Otherwise, an anonymous entity. We must have a declaration.
1073 assert(ND && "mangling empty name without declaration");
1074
1075 if (const NamespaceDecl *NS = dyn_cast<NamespaceDecl>(ND)) {
1076 if (NS->isAnonymousNamespace()) {
1077 // This is how gcc mangles these names.
1078 Out << "12_GLOBAL__N_1";
1079 break;
1080 }
1081 }
1082
Anders Carlsson6f7e2f42010-06-08 14:49:03 +00001083 if (const VarDecl *VD = dyn_cast<VarDecl>(ND)) {
1084 // We must have an anonymous union or struct declaration.
1085 const RecordDecl *RD =
1086 cast<RecordDecl>(VD->getType()->getAs<RecordType>()->getDecl());
1087
1088 // Itanium C++ ABI 5.1.2:
1089 //
1090 // For the purposes of mangling, the name of an anonymous union is
1091 // considered to be the name of the first named data member found by a
1092 // pre-order, depth-first, declaration-order walk of the data members of
1093 // the anonymous union. If there is no such data member (i.e., if all of
1094 // the data members in the union are unnamed), then there is no way for
1095 // a program to refer to the anonymous union, and there is therefore no
1096 // need to mangle its name.
1097 const FieldDecl *FD = FindFirstNamedDataMember(RD);
John McCall7121c8f2010-08-05 22:02:13 +00001098
1099 // It's actually possible for various reasons for us to get here
1100 // with an empty anonymous struct / union. Fortunately, it
1101 // doesn't really matter what name we generate.
1102 if (!FD) break;
Anders Carlsson6f7e2f42010-06-08 14:49:03 +00001103 assert(FD->getIdentifier() && "Data member name isn't an identifier!");
1104
1105 mangleSourceName(FD->getIdentifier());
1106 break;
1107 }
1108
Anders Carlssonc4355b62009-10-07 01:45:02 +00001109 // We must have an anonymous struct.
1110 const TagDecl *TD = cast<TagDecl>(ND);
Richard Smith162e1c12011-04-15 14:24:37 +00001111 if (const TypedefNameDecl *D = TD->getTypedefNameForAnonDecl()) {
Anders Carlssonc4355b62009-10-07 01:45:02 +00001112 assert(TD->getDeclContext() == D->getDeclContext() &&
1113 "Typedef should not be in another decl context!");
1114 assert(D->getDeclName().getAsIdentifierInfo() &&
1115 "Typedef was not named!");
1116 mangleSourceName(D->getDeclName().getAsIdentifierInfo());
1117 break;
1118 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00001119
Douglas Gregor9e8c92a2012-02-20 19:44:39 +00001120 // <unnamed-type-name> ::= <closure-type-name>
1121 //
1122 // <closure-type-name> ::= Ul <lambda-sig> E [ <nonnegative number> ] _
1123 // <lambda-sig> ::= <parameter-type>+ # Parameter types or 'v' for 'void'.
1124 if (const CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(TD)) {
Douglas Gregor5878cbc2012-02-21 04:17:39 +00001125 if (Record->isLambda() && Record->getLambdaManglingNumber()) {
Douglas Gregorccc1b5e2012-02-21 00:37:24 +00001126 mangleLambda(Record);
Douglas Gregor9e8c92a2012-02-20 19:44:39 +00001127 break;
1128 }
1129 }
1130
Anders Carlssonc4355b62009-10-07 01:45:02 +00001131 // Get a unique id for the anonymous struct.
1132 uint64_t AnonStructId = Context.getAnonymousStructId(TD);
1133
1134 // Mangle it as a source name in the form
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00001135 // [n] $_<id>
Anders Carlssonc4355b62009-10-07 01:45:02 +00001136 // where n is the length of the string.
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00001137 SmallString<8> Str;
Anders Carlssonc4355b62009-10-07 01:45:02 +00001138 Str += "$_";
1139 Str += llvm::utostr(AnonStructId);
1140
1141 Out << Str.size();
1142 Out << Str.str();
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001143 break;
Anders Carlssonc4355b62009-10-07 01:45:02 +00001144 }
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001145
1146 case DeclarationName::ObjCZeroArgSelector:
1147 case DeclarationName::ObjCOneArgSelector:
1148 case DeclarationName::ObjCMultiArgSelector:
David Blaikieb219cfc2011-09-23 05:06:16 +00001149 llvm_unreachable("Can't mangle Objective-C selector names here!");
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001150
1151 case DeclarationName::CXXConstructorName:
Anders Carlsson27ae5362009-04-17 01:58:57 +00001152 if (ND == Structor)
Mike Stump141c5af2009-09-02 00:25:38 +00001153 // If the named decl is the C++ constructor we're mangling, use the type
1154 // we were given.
Anders Carlsson27ae5362009-04-17 01:58:57 +00001155 mangleCXXCtorType(static_cast<CXXCtorType>(StructorType));
Anders Carlsson3ac86b52009-04-15 05:36:58 +00001156 else
1157 // Otherwise, use the complete constructor name. This is relevant if a
1158 // class with a constructor is declared within a constructor.
1159 mangleCXXCtorType(Ctor_Complete);
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001160 break;
1161
1162 case DeclarationName::CXXDestructorName:
Anders Carlsson27ae5362009-04-17 01:58:57 +00001163 if (ND == Structor)
Mike Stump141c5af2009-09-02 00:25:38 +00001164 // If the named decl is the C++ destructor we're mangling, use the type we
1165 // were given.
Anders Carlsson27ae5362009-04-17 01:58:57 +00001166 mangleCXXDtorType(static_cast<CXXDtorType>(StructorType));
1167 else
1168 // Otherwise, use the complete destructor name. This is relevant if a
1169 // class with a destructor is declared within a destructor.
1170 mangleCXXDtorType(Dtor_Complete);
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001171 break;
1172
1173 case DeclarationName::CXXConversionFunctionName:
Mike Stump1eb44332009-09-09 15:08:12 +00001174 // <operator-name> ::= cv <type> # (cast)
Douglas Gregor219cc612009-02-13 01:28:03 +00001175 Out << "cv";
Douglas Gregor79e6bd32011-07-12 04:42:08 +00001176 mangleType(Name.getCXXNameType());
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001177 break;
1178
Anders Carlsson8257d412009-12-22 06:36:32 +00001179 case DeclarationName::CXXOperatorName: {
John McCall1dd73832010-02-04 01:42:13 +00001180 unsigned Arity;
1181 if (ND) {
1182 Arity = cast<FunctionDecl>(ND)->getNumParams();
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001183
John McCall1dd73832010-02-04 01:42:13 +00001184 // If we have a C++ member function, we need to include the 'this' pointer.
1185 // FIXME: This does not make sense for operators that are static, but their
1186 // names stay the same regardless of the arity (operator new for instance).
1187 if (isa<CXXMethodDecl>(ND))
1188 Arity++;
1189 } else
1190 Arity = KnownArity;
1191
Anders Carlsson8257d412009-12-22 06:36:32 +00001192 mangleOperatorName(Name.getCXXOverloadedOperator(), Arity);
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001193 break;
Anders Carlsson8257d412009-12-22 06:36:32 +00001194 }
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001195
Sean Hunt3e518bd2009-11-29 07:34:05 +00001196 case DeclarationName::CXXLiteralOperatorName:
Sean Hunt5dd6b392009-12-04 21:11:13 +00001197 // FIXME: This mangling is not yet official.
Sean Hunt2421f662009-12-04 21:01:37 +00001198 Out << "li";
Sean Hunt3e518bd2009-11-29 07:34:05 +00001199 mangleSourceName(Name.getCXXLiteralIdentifier());
1200 break;
1201
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001202 case DeclarationName::CXXUsingDirective:
David Blaikieb219cfc2011-09-23 05:06:16 +00001203 llvm_unreachable("Can't mangle a using directive name!");
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001204 }
1205}
1206
1207void CXXNameMangler::mangleSourceName(const IdentifierInfo *II) {
1208 // <source-name> ::= <positive length number> <identifier>
1209 // <number> ::= [n] <non-negative decimal integer>
1210 // <identifier> ::= <unqualified source code identifier>
1211 Out << II->getLength() << II->getName();
1212}
1213
Eli Friedman7facf842009-12-02 20:32:49 +00001214void CXXNameMangler::mangleNestedName(const NamedDecl *ND,
Fariborz Jahanian57058532010-03-03 19:41:08 +00001215 const DeclContext *DC,
1216 bool NoFunction) {
Douglas Gregor0a9a6d62011-01-26 17:36:28 +00001217 // <nested-name>
1218 // ::= N [<CV-qualifiers>] [<ref-qualifier>] <prefix> <unqualified-name> E
1219 // ::= N [<CV-qualifiers>] [<ref-qualifier>] <template-prefix>
1220 // <template-args> E
Anders Carlssond99edc42009-09-26 03:55:37 +00001221
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001222 Out << 'N';
Douglas Gregor0a9a6d62011-01-26 17:36:28 +00001223 if (const CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(ND)) {
John McCall0953e762009-09-24 19:53:00 +00001224 mangleQualifiers(Qualifiers::fromCVRMask(Method->getTypeQualifiers()));
Douglas Gregor0a9a6d62011-01-26 17:36:28 +00001225 mangleRefQualifier(Method->getRefQualifier());
1226 }
1227
Anders Carlsson2744a062009-09-18 19:00:18 +00001228 // Check if we have a template.
1229 const TemplateArgumentList *TemplateArgs = 0;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00001230 if (const TemplateDecl *TD = isTemplate(ND, TemplateArgs)) {
Anders Carlsson2744a062009-09-18 19:00:18 +00001231 mangleTemplatePrefix(TD);
Rafael Espindolad9800722010-03-11 14:07:00 +00001232 TemplateParameterList *TemplateParameters = TD->getTemplateParameters();
1233 mangleTemplateArgs(*TemplateParameters, *TemplateArgs);
Fariborz Jahanian57058532010-03-03 19:41:08 +00001234 }
1235 else {
1236 manglePrefix(DC, NoFunction);
Anders Carlsson7482e242009-09-18 04:29:09 +00001237 mangleUnqualifiedName(ND);
1238 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00001239
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001240 Out << 'E';
1241}
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00001242void CXXNameMangler::mangleNestedName(const TemplateDecl *TD,
Anders Carlsson7624f212009-09-18 02:42:01 +00001243 const TemplateArgument *TemplateArgs,
1244 unsigned NumTemplateArgs) {
Anders Carlssone45117b2009-09-27 19:53:49 +00001245 // <nested-name> ::= N [<CV-qualifiers>] <template-prefix> <template-args> E
1246
Anders Carlsson7624f212009-09-18 02:42:01 +00001247 Out << 'N';
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00001248
Anders Carlssone45117b2009-09-27 19:53:49 +00001249 mangleTemplatePrefix(TD);
Rafael Espindolad9800722010-03-11 14:07:00 +00001250 TemplateParameterList *TemplateParameters = TD->getTemplateParameters();
1251 mangleTemplateArgs(*TemplateParameters, TemplateArgs, NumTemplateArgs);
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00001252
Anders Carlsson7624f212009-09-18 02:42:01 +00001253 Out << 'E';
1254}
1255
Anders Carlsson1b42c792009-04-02 16:24:45 +00001256void CXXNameMangler::mangleLocalName(const NamedDecl *ND) {
1257 // <local-name> := Z <function encoding> E <entity name> [<discriminator>]
1258 // := Z <function encoding> E s [<discriminator>]
Douglas Gregorccc1b5e2012-02-21 00:37:24 +00001259 // <local-name> := Z <function encoding> E d [ <parameter number> ]
1260 // _ <entity name>
Mike Stump1eb44332009-09-09 15:08:12 +00001261 // <discriminator> := _ <non-negative number>
Douglas Gregorccc1b5e2012-02-21 00:37:24 +00001262 const DeclContext *DC = getEffectiveDeclContext(ND);
Fariborz Jahanian8805fe82011-06-09 19:25:01 +00001263 if (isa<ObjCMethodDecl>(DC) && isa<FunctionDecl>(ND)) {
1264 // Don't add objc method name mangling to locally declared function
1265 mangleUnqualifiedName(ND);
1266 return;
1267 }
1268
Anders Carlsson1b42c792009-04-02 16:24:45 +00001269 Out << 'Z';
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001270
Charles Davis685b1d92010-05-26 18:25:27 +00001271 if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(DC)) {
1272 mangleObjCMethodName(MD);
John McCall82b7d7b2010-10-18 21:28:44 +00001273 } else if (const CXXRecordDecl *RD = GetLocalClassDecl(ND)) {
Douglas Gregorccc1b5e2012-02-21 00:37:24 +00001274 mangleFunctionEncoding(cast<FunctionDecl>(getEffectiveDeclContext(RD)));
Fariborz Jahanian57058532010-03-03 19:41:08 +00001275 Out << 'E';
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001276
Douglas Gregorccc1b5e2012-02-21 00:37:24 +00001277 // The parameter number is omitted for the last parameter, 0 for the
1278 // second-to-last parameter, 1 for the third-to-last parameter, etc. The
1279 // <entity name> will of course contain a <closure-type-name>: Its
1280 // numbering will be local to the particular argument in which it appears
1281 // -- other default arguments do not affect its encoding.
1282 bool SkipDiscriminator = false;
1283 if (RD->isLambda()) {
1284 if (const ParmVarDecl *Parm
1285 = dyn_cast_or_null<ParmVarDecl>(RD->getLambdaContextDecl())) {
1286 if (const FunctionDecl *Func
1287 = dyn_cast<FunctionDecl>(Parm->getDeclContext())) {
1288 Out << 'd';
1289 unsigned Num = Func->getNumParams() - Parm->getFunctionScopeIndex();
1290 if (Num > 1)
1291 mangleNumber(Num - 2);
1292 Out << '_';
1293 SkipDiscriminator = true;
1294 }
1295 }
1296 }
1297
John McCall82b7d7b2010-10-18 21:28:44 +00001298 // Mangle the name relative to the closest enclosing function.
1299 if (ND == RD) // equality ok because RD derived from ND above
1300 mangleUnqualifiedName(ND);
1301 else
1302 mangleNestedName(ND, DC, true /*NoFunction*/);
1303
Douglas Gregorccc1b5e2012-02-21 00:37:24 +00001304 if (!SkipDiscriminator) {
1305 unsigned disc;
1306 if (Context.getNextDiscriminator(RD, disc)) {
1307 if (disc < 10)
1308 Out << '_' << disc;
1309 else
1310 Out << "__" << disc << '_';
1311 }
Fariborz Jahanian4819ac42010-03-04 01:02:03 +00001312 }
Douglas Gregorccc1b5e2012-02-21 00:37:24 +00001313
Fariborz Jahanian57058532010-03-03 19:41:08 +00001314 return;
1315 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001316 else
Fariborz Jahanian57058532010-03-03 19:41:08 +00001317 mangleFunctionEncoding(cast<FunctionDecl>(DC));
Anders Carlsson7b06f6c2009-12-10 03:14:39 +00001318
Anders Carlsson1b42c792009-04-02 16:24:45 +00001319 Out << 'E';
Eli Friedman6f9f25d2009-12-11 20:21:38 +00001320 mangleUnqualifiedName(ND);
Anders Carlsson1b42c792009-04-02 16:24:45 +00001321}
1322
Douglas Gregorccc1b5e2012-02-21 00:37:24 +00001323void CXXNameMangler::mangleLambda(const CXXRecordDecl *Lambda) {
Douglas Gregor552e2992012-02-21 02:22:07 +00001324 // If the context of a closure type is an initializer for a class member
1325 // (static or nonstatic), it is encoded in a qualified name with a final
1326 // <prefix> of the form:
1327 //
1328 // <data-member-prefix> := <member source-name> M
1329 //
1330 // Technically, the data-member-prefix is part of the <prefix>. However,
1331 // since a closure type will always be mangled with a prefix, it's easier
1332 // to emit that last part of the prefix here.
1333 if (Decl *Context = Lambda->getLambdaContextDecl()) {
1334 if ((isa<VarDecl>(Context) || isa<FieldDecl>(Context)) &&
1335 Context->getDeclContext()->isRecord()) {
1336 if (const IdentifierInfo *Name
1337 = cast<NamedDecl>(Context)->getIdentifier()) {
1338 mangleSourceName(Name);
1339 Out << 'M';
1340 }
1341 }
1342 }
1343
Douglas Gregorccc1b5e2012-02-21 00:37:24 +00001344 Out << "Ul";
1345 DeclarationName Name
1346 = getASTContext().DeclarationNames.getCXXOperatorName(OO_Call);
1347 const FunctionProtoType *Proto
1348 = cast<CXXMethodDecl>(*Lambda->lookup(Name).first)->getType()->
1349 getAs<FunctionProtoType>();
1350 mangleBareFunctionType(Proto, /*MangleReturnType=*/false);
1351 Out << "E";
1352
1353 // The number is omitted for the first closure type with a given
1354 // <lambda-sig> in a given context; it is n-2 for the nth closure type
1355 // (in lexical order) with that same <lambda-sig> and context.
1356 //
1357 // The AST keeps track of the number for us.
Douglas Gregor5878cbc2012-02-21 04:17:39 +00001358 unsigned Number = Lambda->getLambdaManglingNumber();
1359 assert(Number > 0 && "Lambda should be mangled as an unnamed class");
1360 if (Number > 1)
1361 mangleNumber(Number - 2);
Douglas Gregorccc1b5e2012-02-21 00:37:24 +00001362 Out << '_';
1363}
1364
John McCalla0ce15c2011-04-24 08:23:24 +00001365void CXXNameMangler::manglePrefix(NestedNameSpecifier *qualifier) {
1366 switch (qualifier->getKind()) {
1367 case NestedNameSpecifier::Global:
1368 // nothing
1369 return;
1370
1371 case NestedNameSpecifier::Namespace:
1372 mangleName(qualifier->getAsNamespace());
1373 return;
1374
1375 case NestedNameSpecifier::NamespaceAlias:
1376 mangleName(qualifier->getAsNamespaceAlias()->getNamespace());
1377 return;
1378
1379 case NestedNameSpecifier::TypeSpec:
1380 case NestedNameSpecifier::TypeSpecWithTemplate:
John McCall4f4e4132011-05-04 01:45:19 +00001381 manglePrefix(QualType(qualifier->getAsType(), 0));
John McCalla0ce15c2011-04-24 08:23:24 +00001382 return;
1383
1384 case NestedNameSpecifier::Identifier:
1385 // Member expressions can have these without prefixes, but that
1386 // should end up in mangleUnresolvedPrefix instead.
1387 assert(qualifier->getPrefix());
1388 manglePrefix(qualifier->getPrefix());
1389
1390 mangleSourceName(qualifier->getAsIdentifier());
1391 return;
1392 }
1393
1394 llvm_unreachable("unexpected nested name specifier");
1395}
1396
Fariborz Jahanian57058532010-03-03 19:41:08 +00001397void CXXNameMangler::manglePrefix(const DeclContext *DC, bool NoFunction) {
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001398 // <prefix> ::= <prefix> <unqualified-name>
1399 // ::= <template-prefix> <template-args>
1400 // ::= <template-param>
1401 // ::= # empty
1402 // ::= <substitution>
Anders Carlsson6862fc72009-09-17 04:16:28 +00001403
James Molloyb3c312c2012-03-05 09:59:43 +00001404 DC = IgnoreLinkageSpecDecls(DC);
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00001405
Anders Carlsson9263e912009-09-18 18:39:58 +00001406 if (DC->isTranslationUnit())
1407 return;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00001408
Douglas Gregor35415f52010-05-25 17:04:15 +00001409 if (const BlockDecl *Block = dyn_cast<BlockDecl>(DC)) {
Douglas Gregorccc1b5e2012-02-21 00:37:24 +00001410 manglePrefix(getEffectiveParentContext(DC), NoFunction);
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00001411 SmallString<64> Name;
Rafael Espindolac4850c22011-02-10 23:59:36 +00001412 llvm::raw_svector_ostream NameStream(Name);
1413 Context.mangleBlock(Block, NameStream);
1414 NameStream.flush();
Douglas Gregor35415f52010-05-25 17:04:15 +00001415 Out << Name.size() << Name;
1416 return;
1417 }
1418
Douglas Gregor552e2992012-02-21 02:22:07 +00001419 const NamedDecl *ND = cast<NamedDecl>(DC);
1420 if (mangleSubstitution(ND))
Anders Carlsson6862fc72009-09-17 04:16:28 +00001421 return;
Douglas Gregor552e2992012-02-21 02:22:07 +00001422
Anders Carlsson2ee3fca2009-09-18 20:11:09 +00001423 // Check if we have a template.
1424 const TemplateArgumentList *TemplateArgs = 0;
Douglas Gregor552e2992012-02-21 02:22:07 +00001425 if (const TemplateDecl *TD = isTemplate(ND, TemplateArgs)) {
Anders Carlsson2ee3fca2009-09-18 20:11:09 +00001426 mangleTemplatePrefix(TD);
Rafael Espindolad9800722010-03-11 14:07:00 +00001427 TemplateParameterList *TemplateParameters = TD->getTemplateParameters();
1428 mangleTemplateArgs(*TemplateParameters, *TemplateArgs);
Fariborz Jahanian57058532010-03-03 19:41:08 +00001429 }
Douglas Gregor552e2992012-02-21 02:22:07 +00001430 else if(NoFunction && (isa<FunctionDecl>(ND) || isa<ObjCMethodDecl>(ND)))
Fariborz Jahanian57058532010-03-03 19:41:08 +00001431 return;
Douglas Gregor552e2992012-02-21 02:22:07 +00001432 else if (const ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(ND))
Douglas Gregor35415f52010-05-25 17:04:15 +00001433 mangleObjCMethodName(Method);
Fariborz Jahanian57058532010-03-03 19:41:08 +00001434 else {
Douglas Gregor552e2992012-02-21 02:22:07 +00001435 manglePrefix(getEffectiveDeclContext(ND), NoFunction);
1436 mangleUnqualifiedName(ND);
Anders Carlsson2ee3fca2009-09-18 20:11:09 +00001437 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00001438
Douglas Gregor552e2992012-02-21 02:22:07 +00001439 addSubstitution(ND);
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001440}
1441
Douglas Gregor20f0cc72010-04-23 03:10:43 +00001442void CXXNameMangler::mangleTemplatePrefix(TemplateName Template) {
1443 // <template-prefix> ::= <prefix> <template unqualified-name>
1444 // ::= <template-param>
1445 // ::= <substitution>
1446 if (TemplateDecl *TD = Template.getAsTemplateDecl())
1447 return mangleTemplatePrefix(TD);
1448
1449 if (QualifiedTemplateName *Qualified = Template.getAsQualifiedTemplateName())
John McCalla0ce15c2011-04-24 08:23:24 +00001450 manglePrefix(Qualified->getQualifier());
Sean Huntc3021132010-05-05 15:23:54 +00001451
Douglas Gregor20f0cc72010-04-23 03:10:43 +00001452 if (OverloadedTemplateStorage *Overloaded
1453 = Template.getAsOverloadedTemplate()) {
Sean Huntc3021132010-05-05 15:23:54 +00001454 mangleUnqualifiedName(0, (*Overloaded->begin())->getDeclName(),
Douglas Gregor20f0cc72010-04-23 03:10:43 +00001455 UnknownArity);
1456 return;
1457 }
Sean Huntc3021132010-05-05 15:23:54 +00001458
Douglas Gregor20f0cc72010-04-23 03:10:43 +00001459 DependentTemplateName *Dependent = Template.getAsDependentTemplateName();
1460 assert(Dependent && "Unknown template name kind?");
John McCalla0ce15c2011-04-24 08:23:24 +00001461 manglePrefix(Dependent->getQualifier());
Douglas Gregor1e9268e2010-04-28 05:58:56 +00001462 mangleUnscopedTemplateName(Template);
Douglas Gregor20f0cc72010-04-23 03:10:43 +00001463}
1464
Anders Carlsson0fa6df42009-09-26 19:45:45 +00001465void CXXNameMangler::mangleTemplatePrefix(const TemplateDecl *ND) {
Anders Carlsson7482e242009-09-18 04:29:09 +00001466 // <template-prefix> ::= <prefix> <template unqualified-name>
1467 // ::= <template-param>
1468 // ::= <substitution>
Douglas Gregor32fb4e12010-02-05 20:45:00 +00001469 // <template-template-param> ::= <template-param>
1470 // <substitution>
Anders Carlsson7482e242009-09-18 04:29:09 +00001471
Anders Carlssonaeb85372009-09-26 22:18:22 +00001472 if (mangleSubstitution(ND))
1473 return;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00001474
Douglas Gregor32fb4e12010-02-05 20:45:00 +00001475 // <template-template-param> ::= <template-param>
1476 if (const TemplateTemplateParmDecl *TTP
1477 = dyn_cast<TemplateTemplateParmDecl>(ND)) {
1478 mangleTemplateParameter(TTP->getIndex());
1479 return;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001480 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00001481
Douglas Gregorccc1b5e2012-02-21 00:37:24 +00001482 manglePrefix(getEffectiveDeclContext(ND));
Anders Carlsson1668f202009-09-26 20:13:56 +00001483 mangleUnqualifiedName(ND->getTemplatedDecl());
Anders Carlssonaeb85372009-09-26 22:18:22 +00001484 addSubstitution(ND);
Anders Carlsson7482e242009-09-18 04:29:09 +00001485}
1486
John McCallb6f532e2010-07-14 06:43:17 +00001487/// Mangles a template name under the production <type>. Required for
1488/// template template arguments.
1489/// <type> ::= <class-enum-type>
1490/// ::= <template-param>
1491/// ::= <substitution>
1492void CXXNameMangler::mangleType(TemplateName TN) {
1493 if (mangleSubstitution(TN))
1494 return;
1495
1496 TemplateDecl *TD = 0;
1497
1498 switch (TN.getKind()) {
1499 case TemplateName::QualifiedTemplate:
1500 TD = TN.getAsQualifiedTemplateName()->getTemplateDecl();
1501 goto HaveDecl;
1502
1503 case TemplateName::Template:
1504 TD = TN.getAsTemplateDecl();
1505 goto HaveDecl;
1506
1507 HaveDecl:
1508 if (isa<TemplateTemplateParmDecl>(TD))
1509 mangleTemplateParameter(cast<TemplateTemplateParmDecl>(TD)->getIndex());
1510 else
1511 mangleName(TD);
1512 break;
1513
1514 case TemplateName::OverloadedTemplate:
1515 llvm_unreachable("can't mangle an overloaded template name as a <type>");
John McCallb6f532e2010-07-14 06:43:17 +00001516
1517 case TemplateName::DependentTemplate: {
1518 const DependentTemplateName *Dependent = TN.getAsDependentTemplateName();
1519 assert(Dependent->isIdentifier());
1520
1521 // <class-enum-type> ::= <name>
1522 // <name> ::= <nested-name>
John McCalla0ce15c2011-04-24 08:23:24 +00001523 mangleUnresolvedPrefix(Dependent->getQualifier(), 0);
John McCallb6f532e2010-07-14 06:43:17 +00001524 mangleSourceName(Dependent->getIdentifier());
1525 break;
1526 }
1527
John McCallb44e0cf2011-06-30 21:59:02 +00001528 case TemplateName::SubstTemplateTemplateParm: {
1529 // Substituted template parameters are mangled as the substituted
1530 // template. This will check for the substitution twice, which is
1531 // fine, but we have to return early so that we don't try to *add*
1532 // the substitution twice.
1533 SubstTemplateTemplateParmStorage *subst
1534 = TN.getAsSubstTemplateTemplateParm();
1535 mangleType(subst->getReplacement());
1536 return;
1537 }
John McCall14606042011-06-30 08:33:18 +00001538
Douglas Gregor1aee05d2011-01-15 06:45:20 +00001539 case TemplateName::SubstTemplateTemplateParmPack: {
John McCall68a51a72011-07-01 00:04:39 +00001540 // FIXME: not clear how to mangle this!
1541 // template <template <class> class T...> class A {
1542 // template <template <class> class U...> void foo(B<T,U> x...);
1543 // };
1544 Out << "_SUBSTPACK_";
Douglas Gregor1aee05d2011-01-15 06:45:20 +00001545 break;
1546 }
John McCallb6f532e2010-07-14 06:43:17 +00001547 }
1548
1549 addSubstitution(TN);
1550}
1551
Mike Stump1eb44332009-09-09 15:08:12 +00001552void
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001553CXXNameMangler::mangleOperatorName(OverloadedOperatorKind OO, unsigned Arity) {
1554 switch (OO) {
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001555 // <operator-name> ::= nw # new
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001556 case OO_New: Out << "nw"; break;
1557 // ::= na # new[]
1558 case OO_Array_New: Out << "na"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001559 // ::= dl # delete
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001560 case OO_Delete: Out << "dl"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001561 // ::= da # delete[]
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001562 case OO_Array_Delete: Out << "da"; break;
1563 // ::= ps # + (unary)
John McCall5e1e89b2010-08-18 19:18:59 +00001564 // ::= pl # + (binary or unknown)
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001565 case OO_Plus:
Anders Carlsson8257d412009-12-22 06:36:32 +00001566 Out << (Arity == 1? "ps" : "pl"); break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001567 // ::= ng # - (unary)
John McCall5e1e89b2010-08-18 19:18:59 +00001568 // ::= mi # - (binary or unknown)
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001569 case OO_Minus:
Anders Carlsson8257d412009-12-22 06:36:32 +00001570 Out << (Arity == 1? "ng" : "mi"); break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001571 // ::= ad # & (unary)
John McCall5e1e89b2010-08-18 19:18:59 +00001572 // ::= an # & (binary or unknown)
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001573 case OO_Amp:
Anders Carlsson8257d412009-12-22 06:36:32 +00001574 Out << (Arity == 1? "ad" : "an"); break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001575 // ::= de # * (unary)
John McCall5e1e89b2010-08-18 19:18:59 +00001576 // ::= ml # * (binary or unknown)
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001577 case OO_Star:
John McCall5e1e89b2010-08-18 19:18:59 +00001578 // Use binary when unknown.
Anders Carlsson8257d412009-12-22 06:36:32 +00001579 Out << (Arity == 1? "de" : "ml"); break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001580 // ::= co # ~
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001581 case OO_Tilde: Out << "co"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001582 // ::= dv # /
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001583 case OO_Slash: Out << "dv"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001584 // ::= rm # %
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001585 case OO_Percent: Out << "rm"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001586 // ::= or # |
1587 case OO_Pipe: Out << "or"; break;
1588 // ::= eo # ^
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001589 case OO_Caret: Out << "eo"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001590 // ::= aS # =
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001591 case OO_Equal: Out << "aS"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001592 // ::= pL # +=
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001593 case OO_PlusEqual: Out << "pL"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001594 // ::= mI # -=
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001595 case OO_MinusEqual: Out << "mI"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001596 // ::= mL # *=
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001597 case OO_StarEqual: Out << "mL"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001598 // ::= dV # /=
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001599 case OO_SlashEqual: Out << "dV"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001600 // ::= rM # %=
1601 case OO_PercentEqual: Out << "rM"; break;
1602 // ::= aN # &=
1603 case OO_AmpEqual: Out << "aN"; break;
1604 // ::= oR # |=
1605 case OO_PipeEqual: Out << "oR"; break;
1606 // ::= eO # ^=
1607 case OO_CaretEqual: Out << "eO"; break;
1608 // ::= ls # <<
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001609 case OO_LessLess: Out << "ls"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001610 // ::= rs # >>
1611 case OO_GreaterGreater: Out << "rs"; break;
1612 // ::= lS # <<=
1613 case OO_LessLessEqual: Out << "lS"; break;
1614 // ::= rS # >>=
1615 case OO_GreaterGreaterEqual: Out << "rS"; break;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001616 // ::= eq # ==
1617 case OO_EqualEqual: Out << "eq"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001618 // ::= ne # !=
1619 case OO_ExclaimEqual: Out << "ne"; break;
1620 // ::= lt # <
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001621 case OO_Less: Out << "lt"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001622 // ::= gt # >
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001623 case OO_Greater: Out << "gt"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001624 // ::= le # <=
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001625 case OO_LessEqual: Out << "le"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001626 // ::= ge # >=
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001627 case OO_GreaterEqual: Out << "ge"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001628 // ::= nt # !
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001629 case OO_Exclaim: Out << "nt"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001630 // ::= aa # &&
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001631 case OO_AmpAmp: Out << "aa"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001632 // ::= oo # ||
1633 case OO_PipePipe: Out << "oo"; break;
1634 // ::= pp # ++
1635 case OO_PlusPlus: Out << "pp"; break;
1636 // ::= mm # --
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001637 case OO_MinusMinus: Out << "mm"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001638 // ::= cm # ,
1639 case OO_Comma: Out << "cm"; break;
1640 // ::= pm # ->*
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001641 case OO_ArrowStar: Out << "pm"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001642 // ::= pt # ->
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001643 case OO_Arrow: Out << "pt"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001644 // ::= cl # ()
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001645 case OO_Call: Out << "cl"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001646 // ::= ix # []
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001647 case OO_Subscript: Out << "ix"; break;
Anders Carlssone170ba72009-12-14 01:45:37 +00001648
1649 // ::= qu # ?
1650 // The conditional operator can't be overloaded, but we still handle it when
1651 // mangling expressions.
1652 case OO_Conditional: Out << "qu"; break;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001653
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001654 case OO_None:
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001655 case NUM_OVERLOADED_OPERATORS:
David Blaikieb219cfc2011-09-23 05:06:16 +00001656 llvm_unreachable("Not an overloaded operator");
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001657 }
1658}
1659
John McCall0953e762009-09-24 19:53:00 +00001660void CXXNameMangler::mangleQualifiers(Qualifiers Quals) {
Mike Stump1eb44332009-09-09 15:08:12 +00001661 // <CV-qualifiers> ::= [r] [V] [K] # restrict (C99), volatile, const
John McCall0953e762009-09-24 19:53:00 +00001662 if (Quals.hasRestrict())
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001663 Out << 'r';
John McCall0953e762009-09-24 19:53:00 +00001664 if (Quals.hasVolatile())
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001665 Out << 'V';
John McCall0953e762009-09-24 19:53:00 +00001666 if (Quals.hasConst())
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001667 Out << 'K';
John McCall0953e762009-09-24 19:53:00 +00001668
Douglas Gregor56079f72010-06-14 23:15:08 +00001669 if (Quals.hasAddressSpace()) {
1670 // Extension:
1671 //
1672 // <type> ::= U <address-space-number>
1673 //
1674 // where <address-space-number> is a source name consisting of 'AS'
1675 // followed by the address space <number>.
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00001676 SmallString<64> ASString;
Douglas Gregor56079f72010-06-14 23:15:08 +00001677 ASString = "AS" + llvm::utostr_32(Quals.getAddressSpace());
1678 Out << 'U' << ASString.size() << ASString;
1679 }
1680
Chris Lattner5f9e2722011-07-23 10:55:15 +00001681 StringRef LifetimeName;
John McCallf85e1932011-06-15 23:02:42 +00001682 switch (Quals.getObjCLifetime()) {
1683 // Objective-C ARC Extension:
1684 //
1685 // <type> ::= U "__strong"
1686 // <type> ::= U "__weak"
1687 // <type> ::= U "__autoreleasing"
John McCallf85e1932011-06-15 23:02:42 +00001688 case Qualifiers::OCL_None:
1689 break;
1690
1691 case Qualifiers::OCL_Weak:
1692 LifetimeName = "__weak";
1693 break;
1694
1695 case Qualifiers::OCL_Strong:
1696 LifetimeName = "__strong";
1697 break;
1698
1699 case Qualifiers::OCL_Autoreleasing:
1700 LifetimeName = "__autoreleasing";
1701 break;
1702
1703 case Qualifiers::OCL_ExplicitNone:
Douglas Gregorc22d6992011-06-17 22:26:49 +00001704 // The __unsafe_unretained qualifier is *not* mangled, so that
1705 // __unsafe_unretained types in ARC produce the same manglings as the
1706 // equivalent (but, naturally, unqualified) types in non-ARC, providing
1707 // better ABI compatibility.
1708 //
1709 // It's safe to do this because unqualified 'id' won't show up
1710 // in any type signatures that need to be mangled.
John McCallf85e1932011-06-15 23:02:42 +00001711 break;
1712 }
1713 if (!LifetimeName.empty())
1714 Out << 'U' << LifetimeName.size() << LifetimeName;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001715}
1716
Douglas Gregor0a9a6d62011-01-26 17:36:28 +00001717void CXXNameMangler::mangleRefQualifier(RefQualifierKind RefQualifier) {
1718 // <ref-qualifier> ::= R # lvalue reference
1719 // ::= O # rvalue-reference
1720 // Proposal to Itanium C++ ABI list on 1/26/11
1721 switch (RefQualifier) {
1722 case RQ_None:
1723 break;
1724
1725 case RQ_LValue:
1726 Out << 'R';
1727 break;
1728
1729 case RQ_RValue:
1730 Out << 'O';
1731 break;
1732 }
1733}
1734
Anders Carlsson7b06f6c2009-12-10 03:14:39 +00001735void CXXNameMangler::mangleObjCMethodName(const ObjCMethodDecl *MD) {
Rafael Espindolaf0be9792011-02-11 02:52:17 +00001736 Context.mangleObjCMethodName(MD, Out);
Anders Carlsson7b06f6c2009-12-10 03:14:39 +00001737}
1738
Douglas Gregorf1588662011-07-12 15:18:55 +00001739void CXXNameMangler::mangleType(QualType T) {
1740 // If our type is instantiation-dependent but not dependent, we mangle
1741 // it as it was written in the source, removing any top-level sugar.
1742 // Otherwise, use the canonical type.
1743 //
1744 // FIXME: This is an approximation of the instantiation-dependent name
1745 // mangling rules, since we should really be using the type as written and
1746 // augmented via semantic analysis (i.e., with implicit conversions and
1747 // default template arguments) for any instantiation-dependent type.
1748 // Unfortunately, that requires several changes to our AST:
1749 // - Instantiation-dependent TemplateSpecializationTypes will need to be
1750 // uniqued, so that we can handle substitutions properly
1751 // - Default template arguments will need to be represented in the
1752 // TemplateSpecializationType, since they need to be mangled even though
1753 // they aren't written.
1754 // - Conversions on non-type template arguments need to be expressed, since
1755 // they can affect the mangling of sizeof/alignof.
1756 if (!T->isInstantiationDependentType() || T->isDependentType())
1757 T = T.getCanonicalType();
1758 else {
1759 // Desugar any types that are purely sugar.
1760 do {
1761 // Don't desugar through template specialization types that aren't
1762 // type aliases. We need to mangle the template arguments as written.
1763 if (const TemplateSpecializationType *TST
1764 = dyn_cast<TemplateSpecializationType>(T))
1765 if (!TST->isTypeAlias())
1766 break;
Anders Carlsson4843e582009-03-10 17:07:44 +00001767
Douglas Gregorf1588662011-07-12 15:18:55 +00001768 QualType Desugared
1769 = T.getSingleStepDesugaredType(Context.getASTContext());
1770 if (Desugared == T)
1771 break;
1772
1773 T = Desugared;
1774 } while (true);
1775 }
1776 SplitQualType split = T.split();
John McCall200fa532012-02-08 00:46:36 +00001777 Qualifiers quals = split.Quals;
1778 const Type *ty = split.Ty;
John McCallb47f7482011-01-26 20:05:40 +00001779
Douglas Gregorf1588662011-07-12 15:18:55 +00001780 bool isSubstitutable = quals || !isa<BuiltinType>(T);
1781 if (isSubstitutable && mangleSubstitution(T))
Anders Carlsson76967372009-09-17 00:43:46 +00001782 return;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001783
John McCallb47f7482011-01-26 20:05:40 +00001784 // If we're mangling a qualified array type, push the qualifiers to
1785 // the element type.
Douglas Gregorf1588662011-07-12 15:18:55 +00001786 if (quals && isa<ArrayType>(T)) {
1787 ty = Context.getASTContext().getAsArrayType(T);
John McCallb47f7482011-01-26 20:05:40 +00001788 quals = Qualifiers();
1789
Douglas Gregorf1588662011-07-12 15:18:55 +00001790 // Note that we don't update T: we want to add the
1791 // substitution at the original type.
John McCallb47f7482011-01-26 20:05:40 +00001792 }
1793
1794 if (quals) {
1795 mangleQualifiers(quals);
John McCall0953e762009-09-24 19:53:00 +00001796 // Recurse: even if the qualified type isn't yet substitutable,
1797 // the unqualified type might be.
John McCallb47f7482011-01-26 20:05:40 +00001798 mangleType(QualType(ty, 0));
Anders Carlsson76967372009-09-17 00:43:46 +00001799 } else {
John McCallb47f7482011-01-26 20:05:40 +00001800 switch (ty->getTypeClass()) {
John McCallefe6aee2009-09-05 07:56:18 +00001801#define ABSTRACT_TYPE(CLASS, PARENT)
1802#define NON_CANONICAL_TYPE(CLASS, PARENT) \
Anders Carlsson76967372009-09-17 00:43:46 +00001803 case Type::CLASS: \
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +00001804 llvm_unreachable("can't mangle non-canonical type " #CLASS "Type"); \
Anders Carlsson76967372009-09-17 00:43:46 +00001805 return;
John McCallefe6aee2009-09-05 07:56:18 +00001806#define TYPE(CLASS, PARENT) \
Anders Carlsson76967372009-09-17 00:43:46 +00001807 case Type::CLASS: \
John McCallb47f7482011-01-26 20:05:40 +00001808 mangleType(static_cast<const CLASS##Type*>(ty)); \
Anders Carlsson76967372009-09-17 00:43:46 +00001809 break;
John McCallefe6aee2009-09-05 07:56:18 +00001810#include "clang/AST/TypeNodes.def"
Anders Carlsson76967372009-09-17 00:43:46 +00001811 }
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001812 }
Anders Carlsson76967372009-09-17 00:43:46 +00001813
1814 // Add the substitution.
John McCallb47f7482011-01-26 20:05:40 +00001815 if (isSubstitutable)
Douglas Gregorf1588662011-07-12 15:18:55 +00001816 addSubstitution(T);
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001817}
1818
Douglas Gregor1b12a3b2010-05-26 05:11:13 +00001819void CXXNameMangler::mangleNameOrStandardSubstitution(const NamedDecl *ND) {
1820 if (!mangleStandardSubstitution(ND))
1821 mangleName(ND);
1822}
1823
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001824void CXXNameMangler::mangleType(const BuiltinType *T) {
John McCallefe6aee2009-09-05 07:56:18 +00001825 // <type> ::= <builtin-type>
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001826 // <builtin-type> ::= v # void
1827 // ::= w # wchar_t
1828 // ::= b # bool
1829 // ::= c # char
1830 // ::= a # signed char
1831 // ::= h # unsigned char
1832 // ::= s # short
1833 // ::= t # unsigned short
1834 // ::= i # int
1835 // ::= j # unsigned int
1836 // ::= l # long
1837 // ::= m # unsigned long
1838 // ::= x # long long, __int64
1839 // ::= y # unsigned long long, __int64
1840 // ::= n # __int128
1841 // UNSUPPORTED: ::= o # unsigned __int128
1842 // ::= f # float
1843 // ::= d # double
1844 // ::= e # long double, __float80
1845 // UNSUPPORTED: ::= g # __float128
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001846 // UNSUPPORTED: ::= Dd # IEEE 754r decimal floating point (64 bits)
1847 // UNSUPPORTED: ::= De # IEEE 754r decimal floating point (128 bits)
1848 // UNSUPPORTED: ::= Df # IEEE 754r decimal floating point (32 bits)
Anton Korobeynikovaa4a99b2011-10-14 23:23:15 +00001849 // ::= Dh # IEEE 754r half-precision floating point (16 bits)
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001850 // ::= Di # char32_t
1851 // ::= Ds # char16_t
Anders Carlssone2923682010-11-04 04:31:32 +00001852 // ::= Dn # std::nullptr_t (i.e., decltype(nullptr))
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001853 // ::= u <source-name> # vendor extended type
1854 switch (T->getKind()) {
1855 case BuiltinType::Void: Out << 'v'; break;
1856 case BuiltinType::Bool: Out << 'b'; break;
1857 case BuiltinType::Char_U: case BuiltinType::Char_S: Out << 'c'; break;
1858 case BuiltinType::UChar: Out << 'h'; break;
1859 case BuiltinType::UShort: Out << 't'; break;
1860 case BuiltinType::UInt: Out << 'j'; break;
1861 case BuiltinType::ULong: Out << 'm'; break;
1862 case BuiltinType::ULongLong: Out << 'y'; break;
Chris Lattner2df9ced2009-04-30 02:43:43 +00001863 case BuiltinType::UInt128: Out << 'o'; break;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001864 case BuiltinType::SChar: Out << 'a'; break;
Chris Lattner3f59c972010-12-25 23:25:43 +00001865 case BuiltinType::WChar_S:
1866 case BuiltinType::WChar_U: Out << 'w'; break;
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001867 case BuiltinType::Char16: Out << "Ds"; break;
1868 case BuiltinType::Char32: Out << "Di"; break;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001869 case BuiltinType::Short: Out << 's'; break;
1870 case BuiltinType::Int: Out << 'i'; break;
1871 case BuiltinType::Long: Out << 'l'; break;
1872 case BuiltinType::LongLong: Out << 'x'; break;
Chris Lattner2df9ced2009-04-30 02:43:43 +00001873 case BuiltinType::Int128: Out << 'n'; break;
Anton Korobeynikovaa4a99b2011-10-14 23:23:15 +00001874 case BuiltinType::Half: Out << "Dh"; break;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001875 case BuiltinType::Float: Out << 'f'; break;
1876 case BuiltinType::Double: Out << 'd'; break;
1877 case BuiltinType::LongDouble: Out << 'e'; break;
Anders Carlssone2923682010-11-04 04:31:32 +00001878 case BuiltinType::NullPtr: Out << "Dn"; break;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001879
John McCalle0a22d02011-10-18 21:02:43 +00001880#define BUILTIN_TYPE(Id, SingletonId)
1881#define PLACEHOLDER_TYPE(Id, SingletonId) \
1882 case BuiltinType::Id:
1883#include "clang/AST/BuiltinTypes.def"
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001884 case BuiltinType::Dependent:
John McCallfb44de92011-05-01 22:35:37 +00001885 llvm_unreachable("mangling a placeholder type");
Steve Naroff9533a7f2009-07-22 17:14:51 +00001886 case BuiltinType::ObjCId: Out << "11objc_object"; break;
1887 case BuiltinType::ObjCClass: Out << "10objc_class"; break;
Fariborz Jahanian13dcd002009-11-21 19:53:08 +00001888 case BuiltinType::ObjCSel: Out << "13objc_selector"; break;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001889 }
1890}
1891
John McCallefe6aee2009-09-05 07:56:18 +00001892// <type> ::= <function-type>
John McCall4b502632012-05-15 02:01:59 +00001893// <function-type> ::= [<CV-qualifiers>] F [Y]
1894// <bare-function-type> [<ref-qualifier>] E
1895// (Proposal to cxx-abi-dev, 2012-05-11)
John McCallefe6aee2009-09-05 07:56:18 +00001896void CXXNameMangler::mangleType(const FunctionProtoType *T) {
John McCall4b502632012-05-15 02:01:59 +00001897 // Mangle CV-qualifiers, if present. These are 'this' qualifiers,
1898 // e.g. "const" in "int (A::*)() const".
1899 mangleQualifiers(Qualifiers::fromCVRMask(T->getTypeQuals()));
1900
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001901 Out << 'F';
John McCall4b502632012-05-15 02:01:59 +00001902
Mike Stumpf5408fe2009-05-16 07:57:57 +00001903 // FIXME: We don't have enough information in the AST to produce the 'Y'
1904 // encoding for extern "C" function types.
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001905 mangleBareFunctionType(T, /*MangleReturnType=*/true);
John McCall4b502632012-05-15 02:01:59 +00001906
1907 // Mangle the ref-qualifier, if present.
1908 mangleRefQualifier(T->getRefQualifier());
1909
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001910 Out << 'E';
1911}
John McCallefe6aee2009-09-05 07:56:18 +00001912void CXXNameMangler::mangleType(const FunctionNoProtoType *T) {
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +00001913 llvm_unreachable("Can't mangle K&R function prototypes");
John McCallefe6aee2009-09-05 07:56:18 +00001914}
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001915void CXXNameMangler::mangleBareFunctionType(const FunctionType *T,
1916 bool MangleReturnType) {
John McCallefe6aee2009-09-05 07:56:18 +00001917 // We should never be mangling something without a prototype.
1918 const FunctionProtoType *Proto = cast<FunctionProtoType>(T);
1919
John McCallfb44de92011-05-01 22:35:37 +00001920 // Record that we're in a function type. See mangleFunctionParam
1921 // for details on what we're trying to achieve here.
1922 FunctionTypeDepthState saved = FunctionTypeDepth.push();
1923
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001924 // <bare-function-type> ::= <signature type>+
John McCallfb44de92011-05-01 22:35:37 +00001925 if (MangleReturnType) {
1926 FunctionTypeDepth.enterResultType();
John McCallefe6aee2009-09-05 07:56:18 +00001927 mangleType(Proto->getResultType());
John McCallfb44de92011-05-01 22:35:37 +00001928 FunctionTypeDepth.leaveResultType();
1929 }
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001930
Anders Carlsson93296682010-06-02 04:40:13 +00001931 if (Proto->getNumArgs() == 0 && !Proto->isVariadic()) {
Eli Friedmana7e68452010-08-22 01:00:03 +00001932 // <builtin-type> ::= v # void
Anders Carlssonc6c91bc2009-04-01 00:15:23 +00001933 Out << 'v';
John McCallfb44de92011-05-01 22:35:37 +00001934
1935 FunctionTypeDepth.pop(saved);
Anders Carlssonc6c91bc2009-04-01 00:15:23 +00001936 return;
1937 }
Mike Stump1eb44332009-09-09 15:08:12 +00001938
Douglas Gregor72564e72009-02-26 23:50:07 +00001939 for (FunctionProtoType::arg_type_iterator Arg = Proto->arg_type_begin(),
Mike Stump1eb44332009-09-09 15:08:12 +00001940 ArgEnd = Proto->arg_type_end();
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001941 Arg != ArgEnd; ++Arg)
Douglas Gregor79e6bd32011-07-12 04:42:08 +00001942 mangleType(Context.getASTContext().getSignatureParameterType(*Arg));
Douglas Gregor219cc612009-02-13 01:28:03 +00001943
John McCallfb44de92011-05-01 22:35:37 +00001944 FunctionTypeDepth.pop(saved);
1945
Douglas Gregor219cc612009-02-13 01:28:03 +00001946 // <builtin-type> ::= z # ellipsis
1947 if (Proto->isVariadic())
1948 Out << 'z';
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001949}
1950
John McCallefe6aee2009-09-05 07:56:18 +00001951// <type> ::= <class-enum-type>
Mike Stump1eb44332009-09-09 15:08:12 +00001952// <class-enum-type> ::= <name>
John McCalled976492009-12-04 22:46:56 +00001953void CXXNameMangler::mangleType(const UnresolvedUsingType *T) {
1954 mangleName(T->getDecl());
1955}
1956
1957// <type> ::= <class-enum-type>
1958// <class-enum-type> ::= <name>
John McCallefe6aee2009-09-05 07:56:18 +00001959void CXXNameMangler::mangleType(const EnumType *T) {
1960 mangleType(static_cast<const TagType*>(T));
1961}
1962void CXXNameMangler::mangleType(const RecordType *T) {
1963 mangleType(static_cast<const TagType*>(T));
1964}
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001965void CXXNameMangler::mangleType(const TagType *T) {
Eli Friedmanecb7e932009-12-11 18:00:57 +00001966 mangleName(T->getDecl());
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001967}
1968
John McCallefe6aee2009-09-05 07:56:18 +00001969// <type> ::= <array-type>
1970// <array-type> ::= A <positive dimension number> _ <element type>
1971// ::= A [<dimension expression>] _ <element type>
1972void CXXNameMangler::mangleType(const ConstantArrayType *T) {
1973 Out << 'A' << T->getSize() << '_';
1974 mangleType(T->getElementType());
1975}
1976void CXXNameMangler::mangleType(const VariableArrayType *T) {
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001977 Out << 'A';
Fariborz Jahanian7281d1f2010-11-02 16:54:00 +00001978 // decayed vla types (size 0) will just be skipped.
1979 if (T->getSizeExpr())
1980 mangleExpression(T->getSizeExpr());
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001981 Out << '_';
1982 mangleType(T->getElementType());
1983}
John McCallefe6aee2009-09-05 07:56:18 +00001984void CXXNameMangler::mangleType(const DependentSizedArrayType *T) {
1985 Out << 'A';
1986 mangleExpression(T->getSizeExpr());
1987 Out << '_';
1988 mangleType(T->getElementType());
1989}
1990void CXXNameMangler::mangleType(const IncompleteArrayType *T) {
Nick Lewycky271b6652010-09-05 03:40:33 +00001991 Out << "A_";
John McCallefe6aee2009-09-05 07:56:18 +00001992 mangleType(T->getElementType());
1993}
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001994
John McCallefe6aee2009-09-05 07:56:18 +00001995// <type> ::= <pointer-to-member-type>
1996// <pointer-to-member-type> ::= M <class type> <member type>
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001997void CXXNameMangler::mangleType(const MemberPointerType *T) {
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001998 Out << 'M';
1999 mangleType(QualType(T->getClass(), 0));
Anders Carlsson0e650012009-05-17 17:41:20 +00002000 QualType PointeeType = T->getPointeeType();
2001 if (const FunctionProtoType *FPT = dyn_cast<FunctionProtoType>(PointeeType)) {
Anders Carlsson0e650012009-05-17 17:41:20 +00002002 mangleType(FPT);
Anders Carlsson9d85b722010-06-02 04:29:50 +00002003
2004 // Itanium C++ ABI 5.1.8:
2005 //
2006 // The type of a non-static member function is considered to be different,
2007 // for the purposes of substitution, from the type of a namespace-scope or
2008 // static member function whose type appears similar. The types of two
2009 // non-static member functions are considered to be different, for the
2010 // purposes of substitution, if the functions are members of different
2011 // classes. In other words, for the purposes of substitution, the class of
2012 // which the function is a member is considered part of the type of
2013 // function.
2014
John McCall4b502632012-05-15 02:01:59 +00002015 // Given that we already substitute member function pointers as a
2016 // whole, the net effect of this rule is just to unconditionally
2017 // suppress substitution on the function type in a member pointer.
Anders Carlsson9d85b722010-06-02 04:29:50 +00002018 // We increment the SeqID here to emulate adding an entry to the
John McCall4b502632012-05-15 02:01:59 +00002019 // substitution table.
Anders Carlsson9d85b722010-06-02 04:29:50 +00002020 ++SeqID;
Mike Stump1eb44332009-09-09 15:08:12 +00002021 } else
Anders Carlsson0e650012009-05-17 17:41:20 +00002022 mangleType(PointeeType);
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00002023}
2024
John McCallefe6aee2009-09-05 07:56:18 +00002025// <type> ::= <template-param>
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00002026void CXXNameMangler::mangleType(const TemplateTypeParmType *T) {
Anders Carlsson0ccdf8d2009-09-27 00:38:53 +00002027 mangleTemplateParameter(T->getIndex());
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00002028}
2029
Douglas Gregorc3069d62011-01-14 02:55:32 +00002030// <type> ::= <template-param>
2031void CXXNameMangler::mangleType(const SubstTemplateTypeParmPackType *T) {
John McCall68a51a72011-07-01 00:04:39 +00002032 // FIXME: not clear how to mangle this!
2033 // template <class T...> class A {
2034 // template <class U...> void foo(T(*)(U) x...);
2035 // };
2036 Out << "_SUBSTPACK_";
Douglas Gregorc3069d62011-01-14 02:55:32 +00002037}
2038
John McCallefe6aee2009-09-05 07:56:18 +00002039// <type> ::= P <type> # pointer-to
2040void CXXNameMangler::mangleType(const PointerType *T) {
2041 Out << 'P';
2042 mangleType(T->getPointeeType());
2043}
2044void CXXNameMangler::mangleType(const ObjCObjectPointerType *T) {
2045 Out << 'P';
2046 mangleType(T->getPointeeType());
2047}
2048
2049// <type> ::= R <type> # reference-to
2050void CXXNameMangler::mangleType(const LValueReferenceType *T) {
2051 Out << 'R';
2052 mangleType(T->getPointeeType());
2053}
2054
2055// <type> ::= O <type> # rvalue reference-to (C++0x)
2056void CXXNameMangler::mangleType(const RValueReferenceType *T) {
2057 Out << 'O';
2058 mangleType(T->getPointeeType());
2059}
2060
2061// <type> ::= C <type> # complex pair (C 2000)
2062void CXXNameMangler::mangleType(const ComplexType *T) {
2063 Out << 'C';
2064 mangleType(T->getElementType());
2065}
2066
Bob Wilsonc7df92d2010-11-12 17:24:43 +00002067// ARM's ABI for Neon vector types specifies that they should be mangled as
Bob Wilson57147a82010-11-16 00:32:18 +00002068// if they are structs (to match ARM's initial implementation). The
2069// vector type must be one of the special types predefined by ARM.
2070void CXXNameMangler::mangleNeonVectorType(const VectorType *T) {
Bob Wilsonc7df92d2010-11-12 17:24:43 +00002071 QualType EltType = T->getElementType();
Bob Wilson57147a82010-11-16 00:32:18 +00002072 assert(EltType->isBuiltinType() && "Neon vector element not a BuiltinType");
Bob Wilsonc7df92d2010-11-12 17:24:43 +00002073 const char *EltName = 0;
Bob Wilson491328c2010-11-12 17:24:46 +00002074 if (T->getVectorKind() == VectorType::NeonPolyVector) {
2075 switch (cast<BuiltinType>(EltType)->getKind()) {
Bob Wilson4cfaa5d2010-11-12 17:24:49 +00002076 case BuiltinType::SChar: EltName = "poly8_t"; break;
2077 case BuiltinType::Short: EltName = "poly16_t"; break;
Bob Wilson57147a82010-11-16 00:32:18 +00002078 default: llvm_unreachable("unexpected Neon polynomial vector element type");
Bob Wilson491328c2010-11-12 17:24:46 +00002079 }
2080 } else {
2081 switch (cast<BuiltinType>(EltType)->getKind()) {
Bob Wilson4cfaa5d2010-11-12 17:24:49 +00002082 case BuiltinType::SChar: EltName = "int8_t"; break;
2083 case BuiltinType::UChar: EltName = "uint8_t"; break;
2084 case BuiltinType::Short: EltName = "int16_t"; break;
2085 case BuiltinType::UShort: EltName = "uint16_t"; break;
2086 case BuiltinType::Int: EltName = "int32_t"; break;
2087 case BuiltinType::UInt: EltName = "uint32_t"; break;
2088 case BuiltinType::LongLong: EltName = "int64_t"; break;
2089 case BuiltinType::ULongLong: EltName = "uint64_t"; break;
2090 case BuiltinType::Float: EltName = "float32_t"; break;
Bob Wilson57147a82010-11-16 00:32:18 +00002091 default: llvm_unreachable("unexpected Neon vector element type");
Bob Wilson491328c2010-11-12 17:24:46 +00002092 }
Bob Wilsonc7df92d2010-11-12 17:24:43 +00002093 }
2094 const char *BaseName = 0;
Bob Wilson4cfaa5d2010-11-12 17:24:49 +00002095 unsigned BitSize = (T->getNumElements() *
Bob Wilson3a723022010-11-16 00:32:12 +00002096 getASTContext().getTypeSize(EltType));
Bob Wilsonc7df92d2010-11-12 17:24:43 +00002097 if (BitSize == 64)
2098 BaseName = "__simd64_";
Bob Wilson57147a82010-11-16 00:32:18 +00002099 else {
2100 assert(BitSize == 128 && "Neon vector type not 64 or 128 bits");
Bob Wilsonc7df92d2010-11-12 17:24:43 +00002101 BaseName = "__simd128_";
Bob Wilson57147a82010-11-16 00:32:18 +00002102 }
Bob Wilsonc7df92d2010-11-12 17:24:43 +00002103 Out << strlen(BaseName) + strlen(EltName);
2104 Out << BaseName << EltName;
Bob Wilsonc7df92d2010-11-12 17:24:43 +00002105}
2106
John McCallefe6aee2009-09-05 07:56:18 +00002107// GNU extension: vector types
Chris Lattner788b0fd2010-06-23 06:00:24 +00002108// <type> ::= <vector-type>
2109// <vector-type> ::= Dv <positive dimension number> _
2110// <extended element type>
2111// ::= Dv [<dimension expression>] _ <element type>
2112// <extended element type> ::= <element type>
2113// ::= p # AltiVec vector pixel
John McCallefe6aee2009-09-05 07:56:18 +00002114void CXXNameMangler::mangleType(const VectorType *T) {
Bob Wilson491328c2010-11-12 17:24:46 +00002115 if ((T->getVectorKind() == VectorType::NeonVector ||
Bob Wilson57147a82010-11-16 00:32:18 +00002116 T->getVectorKind() == VectorType::NeonPolyVector)) {
2117 mangleNeonVectorType(T);
Bob Wilsonc7df92d2010-11-12 17:24:43 +00002118 return;
Bob Wilson57147a82010-11-16 00:32:18 +00002119 }
Nick Lewycky0e5f0672010-03-26 07:18:04 +00002120 Out << "Dv" << T->getNumElements() << '_';
Bob Wilsone86d78c2010-11-10 21:56:12 +00002121 if (T->getVectorKind() == VectorType::AltiVecPixel)
Chris Lattner788b0fd2010-06-23 06:00:24 +00002122 Out << 'p';
Bob Wilsone86d78c2010-11-10 21:56:12 +00002123 else if (T->getVectorKind() == VectorType::AltiVecBool)
Chris Lattner788b0fd2010-06-23 06:00:24 +00002124 Out << 'b';
2125 else
2126 mangleType(T->getElementType());
John McCallefe6aee2009-09-05 07:56:18 +00002127}
2128void CXXNameMangler::mangleType(const ExtVectorType *T) {
2129 mangleType(static_cast<const VectorType*>(T));
2130}
2131void CXXNameMangler::mangleType(const DependentSizedExtVectorType *T) {
Nick Lewycky0e5f0672010-03-26 07:18:04 +00002132 Out << "Dv";
2133 mangleExpression(T->getSizeExpr());
2134 Out << '_';
John McCallefe6aee2009-09-05 07:56:18 +00002135 mangleType(T->getElementType());
2136}
2137
Douglas Gregor7536dd52010-12-20 02:24:11 +00002138void CXXNameMangler::mangleType(const PackExpansionType *T) {
Douglas Gregor4fc48662011-01-13 16:39:34 +00002139 // <type> ::= Dp <type> # pack expansion (C++0x)
Douglas Gregor255c2692011-01-13 17:44:36 +00002140 Out << "Dp";
Douglas Gregor7536dd52010-12-20 02:24:11 +00002141 mangleType(T->getPattern());
2142}
2143
Anders Carlssona40c5e42009-03-07 22:03:21 +00002144void CXXNameMangler::mangleType(const ObjCInterfaceType *T) {
2145 mangleSourceName(T->getDecl()->getIdentifier());
2146}
2147
John McCallc12c5bb2010-05-15 11:32:37 +00002148void CXXNameMangler::mangleType(const ObjCObjectType *T) {
John McCallc00c1f62010-05-15 17:06:29 +00002149 // We don't allow overloading by different protocol qualification,
2150 // so mangling them isn't necessary.
John McCallc12c5bb2010-05-15 11:32:37 +00002151 mangleType(T->getBaseType());
2152}
2153
John McCallefe6aee2009-09-05 07:56:18 +00002154void CXXNameMangler::mangleType(const BlockPointerType *T) {
Anders Carlssonf28c6872009-12-23 22:31:44 +00002155 Out << "U13block_pointer";
2156 mangleType(T->getPointeeType());
John McCallefe6aee2009-09-05 07:56:18 +00002157}
2158
John McCall31f17ec2010-04-27 00:57:59 +00002159void CXXNameMangler::mangleType(const InjectedClassNameType *T) {
2160 // Mangle injected class name types as if the user had written the
2161 // specialization out fully. It may not actually be possible to see
2162 // this mangling, though.
2163 mangleType(T->getInjectedSpecializationType());
2164}
2165
John McCallefe6aee2009-09-05 07:56:18 +00002166void CXXNameMangler::mangleType(const TemplateSpecializationType *T) {
Douglas Gregor1e9268e2010-04-28 05:58:56 +00002167 if (TemplateDecl *TD = T->getTemplateName().getAsTemplateDecl()) {
2168 mangleName(TD, T->getArgs(), T->getNumArgs());
2169 } else {
2170 if (mangleSubstitution(QualType(T, 0)))
2171 return;
Sean Huntc3021132010-05-05 15:23:54 +00002172
Douglas Gregor1e9268e2010-04-28 05:58:56 +00002173 mangleTemplatePrefix(T->getTemplateName());
Sean Huntc3021132010-05-05 15:23:54 +00002174
Douglas Gregor1e9268e2010-04-28 05:58:56 +00002175 // FIXME: GCC does not appear to mangle the template arguments when
2176 // the template in question is a dependent template name. Should we
2177 // emulate that badness?
2178 mangleTemplateArgs(T->getTemplateName(), T->getArgs(), T->getNumArgs());
2179 addSubstitution(QualType(T, 0));
2180 }
John McCallefe6aee2009-09-05 07:56:18 +00002181}
2182
Douglas Gregor4714c122010-03-31 17:34:00 +00002183void CXXNameMangler::mangleType(const DependentNameType *T) {
Anders Carlssonae352482009-09-26 02:26:02 +00002184 // Typename types are always nested
2185 Out << 'N';
John McCalla0ce15c2011-04-24 08:23:24 +00002186 manglePrefix(T->getQualifier());
John McCall33500952010-06-11 00:33:02 +00002187 mangleSourceName(T->getIdentifier());
2188 Out << 'E';
2189}
John McCall6ab30e02010-06-09 07:26:17 +00002190
John McCall33500952010-06-11 00:33:02 +00002191void CXXNameMangler::mangleType(const DependentTemplateSpecializationType *T) {
Douglas Gregoraa2187d2011-02-28 00:04:36 +00002192 // Dependently-scoped template types are nested if they have a prefix.
John McCall33500952010-06-11 00:33:02 +00002193 Out << 'N';
2194
2195 // TODO: avoid making this TemplateName.
2196 TemplateName Prefix =
2197 getASTContext().getDependentTemplateName(T->getQualifier(),
2198 T->getIdentifier());
2199 mangleTemplatePrefix(Prefix);
2200
2201 // FIXME: GCC does not appear to mangle the template arguments when
2202 // the template in question is a dependent template name. Should we
2203 // emulate that badness?
2204 mangleTemplateArgs(Prefix, T->getArgs(), T->getNumArgs());
Anders Carlssonae352482009-09-26 02:26:02 +00002205 Out << 'E';
John McCallefe6aee2009-09-05 07:56:18 +00002206}
2207
John McCallad5e7382010-03-01 23:49:17 +00002208void CXXNameMangler::mangleType(const TypeOfType *T) {
2209 // FIXME: this is pretty unsatisfactory, but there isn't an obvious
2210 // "extension with parameters" mangling.
2211 Out << "u6typeof";
2212}
2213
2214void CXXNameMangler::mangleType(const TypeOfExprType *T) {
2215 // FIXME: this is pretty unsatisfactory, but there isn't an obvious
2216 // "extension with parameters" mangling.
2217 Out << "u6typeof";
2218}
2219
2220void CXXNameMangler::mangleType(const DecltypeType *T) {
2221 Expr *E = T->getUnderlyingExpr();
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002222
John McCallad5e7382010-03-01 23:49:17 +00002223 // type ::= Dt <expression> E # decltype of an id-expression
2224 // # or class member access
2225 // ::= DT <expression> E # decltype of an expression
2226
2227 // This purports to be an exhaustive list of id-expressions and
2228 // class member accesses. Note that we do not ignore parentheses;
2229 // parentheses change the semantics of decltype for these
2230 // expressions (and cause the mangler to use the other form).
2231 if (isa<DeclRefExpr>(E) ||
2232 isa<MemberExpr>(E) ||
2233 isa<UnresolvedLookupExpr>(E) ||
2234 isa<DependentScopeDeclRefExpr>(E) ||
2235 isa<CXXDependentScopeMemberExpr>(E) ||
2236 isa<UnresolvedMemberExpr>(E))
2237 Out << "Dt";
2238 else
2239 Out << "DT";
2240 mangleExpression(E);
2241 Out << 'E';
2242}
2243
Sean Huntca63c202011-05-24 22:41:36 +00002244void CXXNameMangler::mangleType(const UnaryTransformType *T) {
2245 // If this is dependent, we need to record that. If not, we simply
2246 // mangle it as the underlying type since they are equivalent.
2247 if (T->isDependentType()) {
2248 Out << 'U';
2249
2250 switch (T->getUTTKind()) {
2251 case UnaryTransformType::EnumUnderlyingType:
2252 Out << "3eut";
2253 break;
2254 }
2255 }
2256
2257 mangleType(T->getUnderlyingType());
2258}
2259
Richard Smith34b41d92011-02-20 03:19:35 +00002260void CXXNameMangler::mangleType(const AutoType *T) {
2261 QualType D = T->getDeducedType();
Richard Smith967ecd32011-02-21 20:10:02 +00002262 // <builtin-type> ::= Da # dependent auto
2263 if (D.isNull())
2264 Out << "Da";
2265 else
2266 mangleType(D);
Richard Smith34b41d92011-02-20 03:19:35 +00002267}
2268
Eli Friedmanb001de72011-10-06 23:00:33 +00002269void CXXNameMangler::mangleType(const AtomicType *T) {
2270 // <type> ::= U <source-name> <type> # vendor extended type qualifier
2271 // (Until there's a standardized mangling...)
2272 Out << "U7_Atomic";
2273 mangleType(T->getValueType());
2274}
2275
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002276void CXXNameMangler::mangleIntegerLiteral(QualType T,
Anders Carlssone170ba72009-12-14 01:45:37 +00002277 const llvm::APSInt &Value) {
2278 // <expr-primary> ::= L <type> <value number> E # integer literal
2279 Out << 'L';
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002280
Anders Carlssone170ba72009-12-14 01:45:37 +00002281 mangleType(T);
2282 if (T->isBooleanType()) {
2283 // Boolean values are encoded as 0/1.
2284 Out << (Value.getBoolValue() ? '1' : '0');
2285 } else {
John McCall0512e482010-07-14 04:20:34 +00002286 mangleNumber(Value);
Anders Carlssone170ba72009-12-14 01:45:37 +00002287 }
2288 Out << 'E';
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002289
Anders Carlssone170ba72009-12-14 01:45:37 +00002290}
2291
Douglas Gregorcefc3af2012-04-16 07:05:22 +00002292/// Mangles a member expression.
John McCalla0ce15c2011-04-24 08:23:24 +00002293void CXXNameMangler::mangleMemberExpr(const Expr *base,
2294 bool isArrow,
2295 NestedNameSpecifier *qualifier,
2296 NamedDecl *firstQualifierLookup,
2297 DeclarationName member,
2298 unsigned arity) {
2299 // <expression> ::= dt <expression> <unresolved-name>
2300 // ::= pt <expression> <unresolved-name>
Douglas Gregorcefc3af2012-04-16 07:05:22 +00002301 if (base) {
2302 if (base->isImplicitCXXThis()) {
2303 // Note: GCC mangles member expressions to the implicit 'this' as
2304 // *this., whereas we represent them as this->. The Itanium C++ ABI
2305 // does not specify anything here, so we follow GCC.
2306 Out << "dtdefpT";
2307 } else {
2308 Out << (isArrow ? "pt" : "dt");
2309 mangleExpression(base);
2310 }
2311 }
John McCalla0ce15c2011-04-24 08:23:24 +00002312 mangleUnresolvedName(qualifier, firstQualifierLookup, member, arity);
John McCall2f27bf82010-02-04 02:56:29 +00002313}
2314
John McCall5a7e6f72011-04-28 02:52:03 +00002315/// Look at the callee of the given call expression and determine if
2316/// it's a parenthesized id-expression which would have triggered ADL
2317/// otherwise.
2318static bool isParenthesizedADLCallee(const CallExpr *call) {
2319 const Expr *callee = call->getCallee();
2320 const Expr *fn = callee->IgnoreParens();
2321
2322 // Must be parenthesized. IgnoreParens() skips __extension__ nodes,
2323 // too, but for those to appear in the callee, it would have to be
2324 // parenthesized.
2325 if (callee == fn) return false;
2326
2327 // Must be an unresolved lookup.
2328 const UnresolvedLookupExpr *lookup = dyn_cast<UnresolvedLookupExpr>(fn);
2329 if (!lookup) return false;
2330
2331 assert(!lookup->requiresADL());
2332
2333 // Must be an unqualified lookup.
2334 if (lookup->getQualifier()) return false;
2335
2336 // Must not have found a class member. Note that if one is a class
2337 // member, they're all class members.
2338 if (lookup->getNumDecls() > 0 &&
2339 (*lookup->decls_begin())->isCXXClassMember())
2340 return false;
2341
2342 // Otherwise, ADL would have been triggered.
2343 return true;
2344}
2345
John McCall5e1e89b2010-08-18 19:18:59 +00002346void CXXNameMangler::mangleExpression(const Expr *E, unsigned Arity) {
Anders Carlssond553f8c2009-09-21 01:21:10 +00002347 // <expression> ::= <unary operator-name> <expression>
John McCall09cc1412010-02-03 00:55:45 +00002348 // ::= <binary operator-name> <expression> <expression>
2349 // ::= <trinary operator-name> <expression> <expression> <expression>
Anders Carlssond553f8c2009-09-21 01:21:10 +00002350 // ::= cv <type> expression # conversion with one argument
2351 // ::= cv <type> _ <expression>* E # conversion with a different number of arguments
Eli Friedmana7e68452010-08-22 01:00:03 +00002352 // ::= st <type> # sizeof (a type)
Anders Carlssond553f8c2009-09-21 01:21:10 +00002353 // ::= at <type> # alignof (a type)
2354 // ::= <template-param>
2355 // ::= <function-param>
2356 // ::= sr <type> <unqualified-name> # dependent name
2357 // ::= sr <type> <unqualified-name> <template-args> # dependent template-id
Douglas Gregor63f62df2011-06-05 05:27:58 +00002358 // ::= ds <expression> <expression> # expr.*expr
Anders Carlssond553f8c2009-09-21 01:21:10 +00002359 // ::= sZ <template-param> # size of a parameter pack
Douglas Gregor4fc48662011-01-13 16:39:34 +00002360 // ::= sZ <function-param> # size of a function parameter pack
John McCall09cc1412010-02-03 00:55:45 +00002361 // ::= <expr-primary>
John McCall1dd73832010-02-04 01:42:13 +00002362 // <expr-primary> ::= L <type> <value number> E # integer literal
2363 // ::= L <type <value float> E # floating literal
2364 // ::= L <mangled-name> E # external name
Douglas Gregorcefc3af2012-04-16 07:05:22 +00002365 // ::= fpT # 'this' expression
Douglas Gregoredee94b2011-07-12 04:47:20 +00002366 QualType ImplicitlyConvertedToType;
2367
2368recurse:
Anders Carlssond553f8c2009-09-21 01:21:10 +00002369 switch (E->getStmtClass()) {
John McCall6ae1f352010-04-09 22:26:14 +00002370 case Expr::NoStmtClass:
John McCall63c00d72011-02-09 08:16:59 +00002371#define ABSTRACT_STMT(Type)
John McCall6ae1f352010-04-09 22:26:14 +00002372#define EXPR(Type, Base)
2373#define STMT(Type, Base) \
2374 case Expr::Type##Class:
Sean Hunt4bfe1962010-05-05 15:24:00 +00002375#include "clang/AST/StmtNodes.inc"
John McCall0512e482010-07-14 04:20:34 +00002376 // fallthrough
2377
2378 // These all can only appear in local or variable-initialization
2379 // contexts and so should never appear in a mangling.
2380 case Expr::AddrLabelExprClass:
John McCall0512e482010-07-14 04:20:34 +00002381 case Expr::DesignatedInitExprClass:
2382 case Expr::ImplicitValueInitExprClass:
John McCall0512e482010-07-14 04:20:34 +00002383 case Expr::ParenListExprClass:
Douglas Gregor01d08012012-02-07 10:09:13 +00002384 case Expr::LambdaExprClass:
John McCall09cc1412010-02-03 00:55:45 +00002385 llvm_unreachable("unexpected statement kind");
John McCall09cc1412010-02-03 00:55:45 +00002386
John McCall0512e482010-07-14 04:20:34 +00002387 // FIXME: invent manglings for all these.
2388 case Expr::BlockExprClass:
2389 case Expr::CXXPseudoDestructorExprClass:
2390 case Expr::ChooseExprClass:
2391 case Expr::CompoundLiteralExprClass:
2392 case Expr::ExtVectorElementExprClass:
Peter Collingbournef111d932011-04-15 00:35:48 +00002393 case Expr::GenericSelectionExprClass:
John McCall0512e482010-07-14 04:20:34 +00002394 case Expr::ObjCEncodeExprClass:
John McCall0512e482010-07-14 04:20:34 +00002395 case Expr::ObjCIsaExprClass:
2396 case Expr::ObjCIvarRefExprClass:
2397 case Expr::ObjCMessageExprClass:
2398 case Expr::ObjCPropertyRefExprClass:
2399 case Expr::ObjCProtocolExprClass:
2400 case Expr::ObjCSelectorExprClass:
2401 case Expr::ObjCStringLiteralClass:
Patrick Beardeb382ec2012-04-19 00:25:12 +00002402 case Expr::ObjCBoxedExprClass:
Ted Kremenekebcb57a2012-03-06 20:05:56 +00002403 case Expr::ObjCArrayLiteralClass:
2404 case Expr::ObjCDictionaryLiteralClass:
2405 case Expr::ObjCSubscriptRefExprClass:
John McCallf85e1932011-06-15 23:02:42 +00002406 case Expr::ObjCIndirectCopyRestoreExprClass:
John McCall0512e482010-07-14 04:20:34 +00002407 case Expr::OffsetOfExprClass:
2408 case Expr::PredefinedExprClass:
2409 case Expr::ShuffleVectorExprClass:
2410 case Expr::StmtExprClass:
John McCall0512e482010-07-14 04:20:34 +00002411 case Expr::UnaryTypeTraitExprClass:
Francois Pichet6ad6f282010-12-07 00:08:36 +00002412 case Expr::BinaryTypeTraitExprClass:
Douglas Gregor4ca8ac22012-02-24 07:38:34 +00002413 case Expr::TypeTraitExprClass:
John Wiegley21ff2e52011-04-28 00:16:57 +00002414 case Expr::ArrayTypeTraitExprClass:
John Wiegley55262202011-04-25 06:54:41 +00002415 case Expr::ExpressionTraitExprClass:
Francois Pichet9be88402010-09-08 23:47:05 +00002416 case Expr::VAArgExprClass:
Sebastian Redl2e156222010-09-10 20:55:43 +00002417 case Expr::CXXUuidofExprClass:
Peter Collingbournee08ce652011-02-09 21:07:24 +00002418 case Expr::CXXNoexceptExprClass:
Tanya Lattner61eee0c2011-06-04 00:47:47 +00002419 case Expr::CUDAKernelCallExprClass:
2420 case Expr::AsTypeExprClass:
John McCall4b9c2d22011-11-06 09:01:30 +00002421 case Expr::PseudoObjectExprClass:
Eli Friedman276b0612011-10-11 02:20:01 +00002422 case Expr::AtomicExprClass:
Tanya Lattner61eee0c2011-06-04 00:47:47 +00002423 {
John McCall6ae1f352010-04-09 22:26:14 +00002424 // As bad as this diagnostic is, it's better than crashing.
David Blaikied6471f72011-09-25 23:23:43 +00002425 DiagnosticsEngine &Diags = Context.getDiags();
2426 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
John McCall6ae1f352010-04-09 22:26:14 +00002427 "cannot yet mangle expression type %0");
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +00002428 Diags.Report(E->getExprLoc(), DiagID)
John McCall739bf092010-04-10 09:39:25 +00002429 << E->getStmtClassName() << E->getSourceRange();
John McCall6ae1f352010-04-09 22:26:14 +00002430 break;
2431 }
2432
John McCall56ca35d2011-02-17 10:25:35 +00002433 // Even gcc-4.5 doesn't mangle this.
2434 case Expr::BinaryConditionalOperatorClass: {
David Blaikied6471f72011-09-25 23:23:43 +00002435 DiagnosticsEngine &Diags = Context.getDiags();
John McCall56ca35d2011-02-17 10:25:35 +00002436 unsigned DiagID =
David Blaikied6471f72011-09-25 23:23:43 +00002437 Diags.getCustomDiagID(DiagnosticsEngine::Error,
John McCall56ca35d2011-02-17 10:25:35 +00002438 "?: operator with omitted middle operand cannot be mangled");
2439 Diags.Report(E->getExprLoc(), DiagID)
2440 << E->getStmtClassName() << E->getSourceRange();
2441 break;
2442 }
2443
2444 // These are used for internal purposes and cannot be meaningfully mangled.
John McCall7cd7d1a2010-11-15 23:31:06 +00002445 case Expr::OpaqueValueExprClass:
2446 llvm_unreachable("cannot mangle opaque value; mangling wrong thing?");
2447
Sebastian Redlfaf4ef62012-02-25 22:59:28 +00002448 case Expr::InitListExprClass: {
2449 // Proposal by Jason Merrill, 2012-01-03
2450 Out << "il";
2451 const InitListExpr *InitList = cast<InitListExpr>(E);
2452 for (unsigned i = 0, e = InitList->getNumInits(); i != e; ++i)
2453 mangleExpression(InitList->getInit(i));
2454 Out << "E";
2455 break;
2456 }
2457
John McCall0512e482010-07-14 04:20:34 +00002458 case Expr::CXXDefaultArgExprClass:
John McCall5e1e89b2010-08-18 19:18:59 +00002459 mangleExpression(cast<CXXDefaultArgExpr>(E)->getExpr(), Arity);
John McCall0512e482010-07-14 04:20:34 +00002460 break;
2461
John McCall91a57552011-07-15 05:09:51 +00002462 case Expr::SubstNonTypeTemplateParmExprClass:
2463 mangleExpression(cast<SubstNonTypeTemplateParmExpr>(E)->getReplacement(),
2464 Arity);
2465 break;
2466
Richard Smith9fcce652012-03-07 08:35:16 +00002467 case Expr::UserDefinedLiteralClass:
2468 // We follow g++'s approach of mangling a UDL as a call to the literal
2469 // operator.
John McCall0512e482010-07-14 04:20:34 +00002470 case Expr::CXXMemberCallExprClass: // fallthrough
John McCall1dd73832010-02-04 01:42:13 +00002471 case Expr::CallExprClass: {
2472 const CallExpr *CE = cast<CallExpr>(E);
John McCall5a7e6f72011-04-28 02:52:03 +00002473
2474 // <expression> ::= cp <simple-id> <expression>* E
2475 // We use this mangling only when the call would use ADL except
2476 // for being parenthesized. Per discussion with David
2477 // Vandervoorde, 2011.04.25.
2478 if (isParenthesizedADLCallee(CE)) {
2479 Out << "cp";
2480 // The callee here is a parenthesized UnresolvedLookupExpr with
2481 // no qualifier and should always get mangled as a <simple-id>
2482 // anyway.
2483
2484 // <expression> ::= cl <expression>* E
2485 } else {
2486 Out << "cl";
2487 }
2488
John McCall5e1e89b2010-08-18 19:18:59 +00002489 mangleExpression(CE->getCallee(), CE->getNumArgs());
John McCall1dd73832010-02-04 01:42:13 +00002490 for (unsigned I = 0, N = CE->getNumArgs(); I != N; ++I)
2491 mangleExpression(CE->getArg(I));
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002492 Out << 'E';
John McCall09cc1412010-02-03 00:55:45 +00002493 break;
John McCall1dd73832010-02-04 01:42:13 +00002494 }
John McCall09cc1412010-02-03 00:55:45 +00002495
John McCall0512e482010-07-14 04:20:34 +00002496 case Expr::CXXNewExprClass: {
John McCall0512e482010-07-14 04:20:34 +00002497 const CXXNewExpr *New = cast<CXXNewExpr>(E);
2498 if (New->isGlobalNew()) Out << "gs";
2499 Out << (New->isArray() ? "na" : "nw");
2500 for (CXXNewExpr::const_arg_iterator I = New->placement_arg_begin(),
2501 E = New->placement_arg_end(); I != E; ++I)
2502 mangleExpression(*I);
2503 Out << '_';
2504 mangleType(New->getAllocatedType());
2505 if (New->hasInitializer()) {
Sebastian Redlfaf4ef62012-02-25 22:59:28 +00002506 // Proposal by Jason Merrill, 2012-01-03
Sebastian Redlb76ffc52012-02-25 20:51:07 +00002507 if (New->getInitializationStyle() == CXXNewExpr::ListInit)
Sebastian Redlfaf4ef62012-02-25 22:59:28 +00002508 Out << "il";
Sebastian Redlb76ffc52012-02-25 20:51:07 +00002509 else
2510 Out << "pi";
Sebastian Redl2aed8b82012-02-16 12:22:20 +00002511 const Expr *Init = New->getInitializer();
2512 if (const CXXConstructExpr *CCE = dyn_cast<CXXConstructExpr>(Init)) {
2513 // Directly inline the initializers.
2514 for (CXXConstructExpr::const_arg_iterator I = CCE->arg_begin(),
2515 E = CCE->arg_end();
2516 I != E; ++I)
2517 mangleExpression(*I);
2518 } else if (const ParenListExpr *PLE = dyn_cast<ParenListExpr>(Init)) {
2519 for (unsigned i = 0, e = PLE->getNumExprs(); i != e; ++i)
2520 mangleExpression(PLE->getExpr(i));
Sebastian Redlb76ffc52012-02-25 20:51:07 +00002521 } else if (New->getInitializationStyle() == CXXNewExpr::ListInit &&
2522 isa<InitListExpr>(Init)) {
Sebastian Redlfaf4ef62012-02-25 22:59:28 +00002523 // Only take InitListExprs apart for list-initialization.
Sebastian Redlb76ffc52012-02-25 20:51:07 +00002524 const InitListExpr *InitList = cast<InitListExpr>(Init);
2525 for (unsigned i = 0, e = InitList->getNumInits(); i != e; ++i)
2526 mangleExpression(InitList->getInit(i));
Sebastian Redl2aed8b82012-02-16 12:22:20 +00002527 } else
2528 mangleExpression(Init);
John McCall0512e482010-07-14 04:20:34 +00002529 }
2530 Out << 'E';
2531 break;
2532 }
2533
John McCall2f27bf82010-02-04 02:56:29 +00002534 case Expr::MemberExprClass: {
2535 const MemberExpr *ME = cast<MemberExpr>(E);
2536 mangleMemberExpr(ME->getBase(), ME->isArrow(),
John McCalla0ce15c2011-04-24 08:23:24 +00002537 ME->getQualifier(), 0, ME->getMemberDecl()->getDeclName(),
John McCall5e1e89b2010-08-18 19:18:59 +00002538 Arity);
John McCall2f27bf82010-02-04 02:56:29 +00002539 break;
2540 }
2541
2542 case Expr::UnresolvedMemberExprClass: {
2543 const UnresolvedMemberExpr *ME = cast<UnresolvedMemberExpr>(E);
2544 mangleMemberExpr(ME->getBase(), ME->isArrow(),
John McCalla0ce15c2011-04-24 08:23:24 +00002545 ME->getQualifier(), 0, ME->getMemberName(),
John McCall5e1e89b2010-08-18 19:18:59 +00002546 Arity);
John McCall6dbce192010-08-20 00:17:19 +00002547 if (ME->hasExplicitTemplateArgs())
2548 mangleTemplateArgs(ME->getExplicitTemplateArgs());
John McCall2f27bf82010-02-04 02:56:29 +00002549 break;
2550 }
2551
2552 case Expr::CXXDependentScopeMemberExprClass: {
2553 const CXXDependentScopeMemberExpr *ME
2554 = cast<CXXDependentScopeMemberExpr>(E);
2555 mangleMemberExpr(ME->getBase(), ME->isArrow(),
John McCalla0ce15c2011-04-24 08:23:24 +00002556 ME->getQualifier(), ME->getFirstQualifierFoundInScope(),
2557 ME->getMember(), Arity);
John McCall6dbce192010-08-20 00:17:19 +00002558 if (ME->hasExplicitTemplateArgs())
2559 mangleTemplateArgs(ME->getExplicitTemplateArgs());
John McCall2f27bf82010-02-04 02:56:29 +00002560 break;
2561 }
2562
John McCall1dd73832010-02-04 01:42:13 +00002563 case Expr::UnresolvedLookupExprClass: {
2564 const UnresolvedLookupExpr *ULE = cast<UnresolvedLookupExpr>(E);
John McCalla0ce15c2011-04-24 08:23:24 +00002565 mangleUnresolvedName(ULE->getQualifier(), 0, ULE->getName(), Arity);
John McCall26a6ec72011-06-21 22:12:46 +00002566
2567 // All the <unresolved-name> productions end in a
2568 // base-unresolved-name, where <template-args> are just tacked
2569 // onto the end.
John McCall6dbce192010-08-20 00:17:19 +00002570 if (ULE->hasExplicitTemplateArgs())
2571 mangleTemplateArgs(ULE->getExplicitTemplateArgs());
John McCall1dd73832010-02-04 01:42:13 +00002572 break;
2573 }
2574
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002575 case Expr::CXXUnresolvedConstructExprClass: {
John McCall1dd73832010-02-04 01:42:13 +00002576 const CXXUnresolvedConstructExpr *CE = cast<CXXUnresolvedConstructExpr>(E);
2577 unsigned N = CE->arg_size();
2578
2579 Out << "cv";
2580 mangleType(CE->getType());
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002581 if (N != 1) Out << '_';
John McCall1dd73832010-02-04 01:42:13 +00002582 for (unsigned I = 0; I != N; ++I) mangleExpression(CE->getArg(I));
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002583 if (N != 1) Out << 'E';
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002584 break;
John McCall1dd73832010-02-04 01:42:13 +00002585 }
John McCall09cc1412010-02-03 00:55:45 +00002586
John McCall1dd73832010-02-04 01:42:13 +00002587 case Expr::CXXTemporaryObjectExprClass:
2588 case Expr::CXXConstructExprClass: {
2589 const CXXConstructExpr *CE = cast<CXXConstructExpr>(E);
2590 unsigned N = CE->getNumArgs();
2591
Sebastian Redlfaf4ef62012-02-25 22:59:28 +00002592 // Proposal by Jason Merrill, 2012-01-03
2593 if (CE->isListInitialization())
2594 Out << "tl";
2595 else
2596 Out << "cv";
John McCall1dd73832010-02-04 01:42:13 +00002597 mangleType(CE->getType());
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002598 if (N != 1) Out << '_';
John McCall1dd73832010-02-04 01:42:13 +00002599 for (unsigned I = 0; I != N; ++I) mangleExpression(CE->getArg(I));
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002600 if (N != 1) Out << 'E';
John McCall09cc1412010-02-03 00:55:45 +00002601 break;
John McCall1dd73832010-02-04 01:42:13 +00002602 }
2603
Richard Smith41576d42012-02-06 02:54:51 +00002604 case Expr::CXXScalarValueInitExprClass:
2605 Out <<"cv";
2606 mangleType(E->getType());
2607 Out <<"_E";
2608 break;
2609
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +00002610 case Expr::UnaryExprOrTypeTraitExprClass: {
2611 const UnaryExprOrTypeTraitExpr *SAE = cast<UnaryExprOrTypeTraitExpr>(E);
Douglas Gregoredee94b2011-07-12 04:47:20 +00002612
2613 if (!SAE->isInstantiationDependent()) {
2614 // Itanium C++ ABI:
2615 // If the operand of a sizeof or alignof operator is not
2616 // instantiation-dependent it is encoded as an integer literal
2617 // reflecting the result of the operator.
2618 //
2619 // If the result of the operator is implicitly converted to a known
2620 // integer type, that type is used for the literal; otherwise, the type
2621 // of std::size_t or std::ptrdiff_t is used.
2622 QualType T = (ImplicitlyConvertedToType.isNull() ||
2623 !ImplicitlyConvertedToType->isIntegerType())? SAE->getType()
2624 : ImplicitlyConvertedToType;
Richard Smitha6b8b2c2011-10-10 18:28:20 +00002625 llvm::APSInt V = SAE->EvaluateKnownConstInt(Context.getASTContext());
2626 mangleIntegerLiteral(T, V);
Douglas Gregoredee94b2011-07-12 04:47:20 +00002627 break;
2628 }
2629
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +00002630 switch(SAE->getKind()) {
2631 case UETT_SizeOf:
2632 Out << 's';
2633 break;
2634 case UETT_AlignOf:
2635 Out << 'a';
2636 break;
2637 case UETT_VecStep:
David Blaikied6471f72011-09-25 23:23:43 +00002638 DiagnosticsEngine &Diags = Context.getDiags();
2639 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +00002640 "cannot yet mangle vec_step expression");
2641 Diags.Report(DiagID);
2642 return;
2643 }
John McCall1dd73832010-02-04 01:42:13 +00002644 if (SAE->isArgumentType()) {
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002645 Out << 't';
John McCall1dd73832010-02-04 01:42:13 +00002646 mangleType(SAE->getArgumentType());
2647 } else {
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002648 Out << 'z';
John McCall1dd73832010-02-04 01:42:13 +00002649 mangleExpression(SAE->getArgumentExpr());
2650 }
2651 break;
2652 }
Anders Carlssona7694082009-11-06 02:50:19 +00002653
John McCall0512e482010-07-14 04:20:34 +00002654 case Expr::CXXThrowExprClass: {
2655 const CXXThrowExpr *TE = cast<CXXThrowExpr>(E);
2656
2657 // Proposal from David Vandervoorde, 2010.06.30
2658 if (TE->getSubExpr()) {
2659 Out << "tw";
2660 mangleExpression(TE->getSubExpr());
2661 } else {
2662 Out << "tr";
2663 }
2664 break;
2665 }
2666
2667 case Expr::CXXTypeidExprClass: {
2668 const CXXTypeidExpr *TIE = cast<CXXTypeidExpr>(E);
2669
2670 // Proposal from David Vandervoorde, 2010.06.30
2671 if (TIE->isTypeOperand()) {
2672 Out << "ti";
2673 mangleType(TIE->getTypeOperand());
2674 } else {
2675 Out << "te";
2676 mangleExpression(TIE->getExprOperand());
2677 }
2678 break;
2679 }
2680
2681 case Expr::CXXDeleteExprClass: {
2682 const CXXDeleteExpr *DE = cast<CXXDeleteExpr>(E);
2683
2684 // Proposal from David Vandervoorde, 2010.06.30
2685 if (DE->isGlobalDelete()) Out << "gs";
2686 Out << (DE->isArrayForm() ? "da" : "dl");
2687 mangleExpression(DE->getArgument());
2688 break;
2689 }
2690
Anders Carlssone170ba72009-12-14 01:45:37 +00002691 case Expr::UnaryOperatorClass: {
2692 const UnaryOperator *UO = cast<UnaryOperator>(E);
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002693 mangleOperatorName(UnaryOperator::getOverloadedOperator(UO->getOpcode()),
Anders Carlssone170ba72009-12-14 01:45:37 +00002694 /*Arity=*/1);
2695 mangleExpression(UO->getSubExpr());
2696 break;
2697 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002698
John McCall0512e482010-07-14 04:20:34 +00002699 case Expr::ArraySubscriptExprClass: {
2700 const ArraySubscriptExpr *AE = cast<ArraySubscriptExpr>(E);
2701
Chris Lattnerfc8f0e12011-04-15 05:22:18 +00002702 // Array subscript is treated as a syntactically weird form of
John McCall0512e482010-07-14 04:20:34 +00002703 // binary operator.
2704 Out << "ix";
2705 mangleExpression(AE->getLHS());
2706 mangleExpression(AE->getRHS());
2707 break;
2708 }
2709
2710 case Expr::CompoundAssignOperatorClass: // fallthrough
Anders Carlssone170ba72009-12-14 01:45:37 +00002711 case Expr::BinaryOperatorClass: {
2712 const BinaryOperator *BO = cast<BinaryOperator>(E);
Douglas Gregor63f62df2011-06-05 05:27:58 +00002713 if (BO->getOpcode() == BO_PtrMemD)
2714 Out << "ds";
2715 else
2716 mangleOperatorName(BinaryOperator::getOverloadedOperator(BO->getOpcode()),
2717 /*Arity=*/2);
Anders Carlssone170ba72009-12-14 01:45:37 +00002718 mangleExpression(BO->getLHS());
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002719 mangleExpression(BO->getRHS());
Anders Carlssone170ba72009-12-14 01:45:37 +00002720 break;
John McCall2f27bf82010-02-04 02:56:29 +00002721 }
Anders Carlssone170ba72009-12-14 01:45:37 +00002722
2723 case Expr::ConditionalOperatorClass: {
2724 const ConditionalOperator *CO = cast<ConditionalOperator>(E);
2725 mangleOperatorName(OO_Conditional, /*Arity=*/3);
2726 mangleExpression(CO->getCond());
John McCall5e1e89b2010-08-18 19:18:59 +00002727 mangleExpression(CO->getLHS(), Arity);
2728 mangleExpression(CO->getRHS(), Arity);
Anders Carlssone170ba72009-12-14 01:45:37 +00002729 break;
2730 }
2731
Douglas Gregor46287c72010-01-29 16:37:09 +00002732 case Expr::ImplicitCastExprClass: {
Douglas Gregoredee94b2011-07-12 04:47:20 +00002733 ImplicitlyConvertedToType = E->getType();
2734 E = cast<ImplicitCastExpr>(E)->getSubExpr();
2735 goto recurse;
Douglas Gregor46287c72010-01-29 16:37:09 +00002736 }
John McCallf85e1932011-06-15 23:02:42 +00002737
2738 case Expr::ObjCBridgedCastExprClass: {
2739 // Mangle ownership casts as a vendor extended operator __bridge,
2740 // __bridge_transfer, or __bridge_retain.
Chris Lattner5f9e2722011-07-23 10:55:15 +00002741 StringRef Kind = cast<ObjCBridgedCastExpr>(E)->getBridgeKindName();
John McCallf85e1932011-06-15 23:02:42 +00002742 Out << "v1U" << Kind.size() << Kind;
2743 }
2744 // Fall through to mangle the cast itself.
2745
Douglas Gregor46287c72010-01-29 16:37:09 +00002746 case Expr::CStyleCastExprClass:
2747 case Expr::CXXStaticCastExprClass:
2748 case Expr::CXXDynamicCastExprClass:
2749 case Expr::CXXReinterpretCastExprClass:
2750 case Expr::CXXConstCastExprClass:
2751 case Expr::CXXFunctionalCastExprClass: {
2752 const ExplicitCastExpr *ECE = cast<ExplicitCastExpr>(E);
2753 Out << "cv";
2754 mangleType(ECE->getType());
2755 mangleExpression(ECE->getSubExpr());
2756 break;
2757 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002758
Anders Carlsson58040a52009-12-16 05:48:46 +00002759 case Expr::CXXOperatorCallExprClass: {
2760 const CXXOperatorCallExpr *CE = cast<CXXOperatorCallExpr>(E);
2761 unsigned NumArgs = CE->getNumArgs();
2762 mangleOperatorName(CE->getOperator(), /*Arity=*/NumArgs);
2763 // Mangle the arguments.
2764 for (unsigned i = 0; i != NumArgs; ++i)
2765 mangleExpression(CE->getArg(i));
2766 break;
2767 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002768
Anders Carlssona7694082009-11-06 02:50:19 +00002769 case Expr::ParenExprClass:
John McCall5e1e89b2010-08-18 19:18:59 +00002770 mangleExpression(cast<ParenExpr>(E)->getSubExpr(), Arity);
Anders Carlssona7694082009-11-06 02:50:19 +00002771 break;
2772
Anders Carlssond553f8c2009-09-21 01:21:10 +00002773 case Expr::DeclRefExprClass: {
Douglas Gregor5ed1bc32010-02-28 21:40:32 +00002774 const NamedDecl *D = cast<DeclRefExpr>(E)->getDecl();
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002775
Anders Carlssond553f8c2009-09-21 01:21:10 +00002776 switch (D->getKind()) {
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002777 default:
Douglas Gregor5ed1bc32010-02-28 21:40:32 +00002778 // <expr-primary> ::= L <mangled-name> E # external name
2779 Out << 'L';
2780 mangle(D, "_Z");
2781 Out << 'E';
2782 break;
2783
John McCallfb44de92011-05-01 22:35:37 +00002784 case Decl::ParmVar:
2785 mangleFunctionParam(cast<ParmVarDecl>(D));
2786 break;
2787
John McCall3dc7e7b2010-07-24 01:17:35 +00002788 case Decl::EnumConstant: {
2789 const EnumConstantDecl *ED = cast<EnumConstantDecl>(D);
2790 mangleIntegerLiteral(ED->getType(), ED->getInitVal());
2791 break;
2792 }
2793
Anders Carlssond553f8c2009-09-21 01:21:10 +00002794 case Decl::NonTypeTemplateParm: {
2795 const NonTypeTemplateParmDecl *PD = cast<NonTypeTemplateParmDecl>(D);
Anders Carlsson0ccdf8d2009-09-27 00:38:53 +00002796 mangleTemplateParameter(PD->getIndex());
Anders Carlssond553f8c2009-09-21 01:21:10 +00002797 break;
2798 }
2799
2800 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002801
Anders Carlsson50755b02009-09-27 20:11:34 +00002802 break;
Anders Carlssond553f8c2009-09-21 01:21:10 +00002803 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002804
Douglas Gregorc7793c72011-01-15 01:15:58 +00002805 case Expr::SubstNonTypeTemplateParmPackExprClass:
John McCall68a51a72011-07-01 00:04:39 +00002806 // FIXME: not clear how to mangle this!
2807 // template <unsigned N...> class A {
2808 // template <class U...> void foo(U (&x)[N]...);
2809 // };
2810 Out << "_SUBSTPACK_";
Douglas Gregorc7793c72011-01-15 01:15:58 +00002811 break;
Richard Smith9a4db032012-09-12 00:56:43 +00002812
2813 case Expr::FunctionParmPackExprClass: {
2814 // FIXME: not clear how to mangle this!
2815 const FunctionParmPackExpr *FPPE = cast<FunctionParmPackExpr>(E);
2816 Out << "v110_SUBSTPACK";
2817 mangleFunctionParam(FPPE->getParameterPack());
2818 break;
2819 }
2820
John McCall865d4472009-11-19 22:55:06 +00002821 case Expr::DependentScopeDeclRefExprClass: {
2822 const DependentScopeDeclRefExpr *DRE = cast<DependentScopeDeclRefExpr>(E);
John McCall26a6ec72011-06-21 22:12:46 +00002823 mangleUnresolvedName(DRE->getQualifier(), 0, DRE->getDeclName(), Arity);
Douglas Gregor4b2ccfc2010-02-28 22:05:49 +00002824
John McCall26a6ec72011-06-21 22:12:46 +00002825 // All the <unresolved-name> productions end in a
2826 // base-unresolved-name, where <template-args> are just tacked
2827 // onto the end.
John McCall6dbce192010-08-20 00:17:19 +00002828 if (DRE->hasExplicitTemplateArgs())
2829 mangleTemplateArgs(DRE->getExplicitTemplateArgs());
Anders Carlsson50755b02009-09-27 20:11:34 +00002830 break;
2831 }
2832
John McCalld9307602010-04-09 22:54:09 +00002833 case Expr::CXXBindTemporaryExprClass:
2834 mangleExpression(cast<CXXBindTemporaryExpr>(E)->getSubExpr());
2835 break;
2836
John McCall4765fa02010-12-06 08:20:24 +00002837 case Expr::ExprWithCleanupsClass:
2838 mangleExpression(cast<ExprWithCleanups>(E)->getSubExpr(), Arity);
John McCalld9307602010-04-09 22:54:09 +00002839 break;
2840
John McCall1dd73832010-02-04 01:42:13 +00002841 case Expr::FloatingLiteralClass: {
2842 const FloatingLiteral *FL = cast<FloatingLiteral>(E);
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002843 Out << 'L';
John McCall1dd73832010-02-04 01:42:13 +00002844 mangleType(FL->getType());
John McCall0512e482010-07-14 04:20:34 +00002845 mangleFloat(FL->getValue());
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002846 Out << 'E';
John McCall1dd73832010-02-04 01:42:13 +00002847 break;
2848 }
2849
John McCallde810632010-04-09 21:48:08 +00002850 case Expr::CharacterLiteralClass:
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002851 Out << 'L';
John McCallde810632010-04-09 21:48:08 +00002852 mangleType(E->getType());
2853 Out << cast<CharacterLiteral>(E)->getValue();
2854 Out << 'E';
2855 break;
2856
Ted Kremenekebcb57a2012-03-06 20:05:56 +00002857 // FIXME. __objc_yes/__objc_no are mangled same as true/false
2858 case Expr::ObjCBoolLiteralExprClass:
2859 Out << "Lb";
2860 Out << (cast<ObjCBoolLiteralExpr>(E)->getValue() ? '1' : '0');
2861 Out << 'E';
2862 break;
2863
John McCallde810632010-04-09 21:48:08 +00002864 case Expr::CXXBoolLiteralExprClass:
2865 Out << "Lb";
2866 Out << (cast<CXXBoolLiteralExpr>(E)->getValue() ? '1' : '0');
2867 Out << 'E';
2868 break;
2869
John McCall0512e482010-07-14 04:20:34 +00002870 case Expr::IntegerLiteralClass: {
2871 llvm::APSInt Value(cast<IntegerLiteral>(E)->getValue());
2872 if (E->getType()->isSignedIntegerType())
2873 Value.setIsSigned(true);
2874 mangleIntegerLiteral(E->getType(), Value);
Anders Carlssone170ba72009-12-14 01:45:37 +00002875 break;
John McCall0512e482010-07-14 04:20:34 +00002876 }
2877
2878 case Expr::ImaginaryLiteralClass: {
2879 const ImaginaryLiteral *IE = cast<ImaginaryLiteral>(E);
2880 // Mangle as if a complex literal.
Nick Lewycky271b6652010-09-05 03:40:33 +00002881 // Proposal from David Vandevoorde, 2010.06.30.
John McCall0512e482010-07-14 04:20:34 +00002882 Out << 'L';
2883 mangleType(E->getType());
2884 if (const FloatingLiteral *Imag =
2885 dyn_cast<FloatingLiteral>(IE->getSubExpr())) {
2886 // Mangle a floating-point zero of the appropriate type.
2887 mangleFloat(llvm::APFloat(Imag->getValue().getSemantics()));
2888 Out << '_';
2889 mangleFloat(Imag->getValue());
2890 } else {
Nick Lewycky271b6652010-09-05 03:40:33 +00002891 Out << "0_";
John McCall0512e482010-07-14 04:20:34 +00002892 llvm::APSInt Value(cast<IntegerLiteral>(IE->getSubExpr())->getValue());
2893 if (IE->getSubExpr()->getType()->isSignedIntegerType())
2894 Value.setIsSigned(true);
2895 mangleNumber(Value);
2896 }
2897 Out << 'E';
2898 break;
2899 }
2900
2901 case Expr::StringLiteralClass: {
John McCall1658c392010-07-15 21:53:03 +00002902 // Revised proposal from David Vandervoorde, 2010.07.15.
John McCall0512e482010-07-14 04:20:34 +00002903 Out << 'L';
John McCall1658c392010-07-15 21:53:03 +00002904 assert(isa<ConstantArrayType>(E->getType()));
2905 mangleType(E->getType());
John McCall0512e482010-07-14 04:20:34 +00002906 Out << 'E';
2907 break;
2908 }
2909
2910 case Expr::GNUNullExprClass:
2911 // FIXME: should this really be mangled the same as nullptr?
2912 // fallthrough
2913
2914 case Expr::CXXNullPtrLiteralExprClass: {
2915 // Proposal from David Vandervoorde, 2010.06.30, as
2916 // modified by ABI list discussion.
2917 Out << "LDnE";
2918 break;
2919 }
Douglas Gregorbe230c32011-01-03 17:17:50 +00002920
2921 case Expr::PackExpansionExprClass:
2922 Out << "sp";
2923 mangleExpression(cast<PackExpansionExpr>(E)->getPattern());
2924 break;
Douglas Gregor2e774c42011-01-04 18:56:13 +00002925
2926 case Expr::SizeOfPackExprClass: {
Douglas Gregor2e774c42011-01-04 18:56:13 +00002927 Out << "sZ";
2928 const NamedDecl *Pack = cast<SizeOfPackExpr>(E)->getPack();
2929 if (const TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(Pack))
2930 mangleTemplateParameter(TTP->getIndex());
2931 else if (const NonTypeTemplateParmDecl *NTTP
2932 = dyn_cast<NonTypeTemplateParmDecl>(Pack))
2933 mangleTemplateParameter(NTTP->getIndex());
2934 else if (const TemplateTemplateParmDecl *TempTP
2935 = dyn_cast<TemplateTemplateParmDecl>(Pack))
2936 mangleTemplateParameter(TempTP->getIndex());
Douglas Gregor91832362011-07-12 07:03:48 +00002937 else
2938 mangleFunctionParam(cast<ParmVarDecl>(Pack));
Douglas Gregordfbbcf92011-03-03 02:20:19 +00002939 break;
Douglas Gregor2e774c42011-01-04 18:56:13 +00002940 }
Douglas Gregor03e80032011-06-21 17:03:29 +00002941
2942 case Expr::MaterializeTemporaryExprClass: {
2943 mangleExpression(cast<MaterializeTemporaryExpr>(E)->GetTemporaryExpr());
2944 break;
2945 }
Douglas Gregorcefc3af2012-04-16 07:05:22 +00002946
2947 case Expr::CXXThisExprClass:
2948 Out << "fpT";
2949 break;
Anders Carlssond553f8c2009-09-21 01:21:10 +00002950 }
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00002951}
2952
John McCallfb44de92011-05-01 22:35:37 +00002953/// Mangle an expression which refers to a parameter variable.
2954///
2955/// <expression> ::= <function-param>
2956/// <function-param> ::= fp <top-level CV-qualifiers> _ # L == 0, I == 0
2957/// <function-param> ::= fp <top-level CV-qualifiers>
2958/// <parameter-2 non-negative number> _ # L == 0, I > 0
2959/// <function-param> ::= fL <L-1 non-negative number>
2960/// p <top-level CV-qualifiers> _ # L > 0, I == 0
2961/// <function-param> ::= fL <L-1 non-negative number>
2962/// p <top-level CV-qualifiers>
2963/// <I-1 non-negative number> _ # L > 0, I > 0
2964///
2965/// L is the nesting depth of the parameter, defined as 1 if the
2966/// parameter comes from the innermost function prototype scope
2967/// enclosing the current context, 2 if from the next enclosing
2968/// function prototype scope, and so on, with one special case: if
2969/// we've processed the full parameter clause for the innermost
2970/// function type, then L is one less. This definition conveniently
2971/// makes it irrelevant whether a function's result type was written
2972/// trailing or leading, but is otherwise overly complicated; the
2973/// numbering was first designed without considering references to
2974/// parameter in locations other than return types, and then the
2975/// mangling had to be generalized without changing the existing
2976/// manglings.
2977///
2978/// I is the zero-based index of the parameter within its parameter
2979/// declaration clause. Note that the original ABI document describes
2980/// this using 1-based ordinals.
2981void CXXNameMangler::mangleFunctionParam(const ParmVarDecl *parm) {
2982 unsigned parmDepth = parm->getFunctionScopeDepth();
2983 unsigned parmIndex = parm->getFunctionScopeIndex();
2984
2985 // Compute 'L'.
2986 // parmDepth does not include the declaring function prototype.
2987 // FunctionTypeDepth does account for that.
2988 assert(parmDepth < FunctionTypeDepth.getDepth());
2989 unsigned nestingDepth = FunctionTypeDepth.getDepth() - parmDepth;
2990 if (FunctionTypeDepth.isInResultType())
2991 nestingDepth--;
2992
2993 if (nestingDepth == 0) {
2994 Out << "fp";
2995 } else {
2996 Out << "fL" << (nestingDepth - 1) << 'p';
2997 }
2998
2999 // Top-level qualifiers. We don't have to worry about arrays here,
3000 // because parameters declared as arrays should already have been
Benjamin Kramer48d798c2012-06-02 10:20:41 +00003001 // transformed to have pointer type. FIXME: apparently these don't
John McCallfb44de92011-05-01 22:35:37 +00003002 // get mangled if used as an rvalue of a known non-class type?
3003 assert(!parm->getType()->isArrayType()
3004 && "parameter's type is still an array type?");
3005 mangleQualifiers(parm->getType().getQualifiers());
3006
3007 // Parameter index.
3008 if (parmIndex != 0) {
3009 Out << (parmIndex - 1);
3010 }
3011 Out << '_';
3012}
3013
Anders Carlsson3ac86b52009-04-15 05:36:58 +00003014void CXXNameMangler::mangleCXXCtorType(CXXCtorType T) {
3015 // <ctor-dtor-name> ::= C1 # complete object constructor
3016 // ::= C2 # base object constructor
3017 // ::= C3 # complete object allocating constructor
3018 //
3019 switch (T) {
3020 case Ctor_Complete:
3021 Out << "C1";
3022 break;
3023 case Ctor_Base:
3024 Out << "C2";
3025 break;
3026 case Ctor_CompleteAllocating:
3027 Out << "C3";
3028 break;
3029 }
3030}
3031
Anders Carlsson27ae5362009-04-17 01:58:57 +00003032void CXXNameMangler::mangleCXXDtorType(CXXDtorType T) {
3033 // <ctor-dtor-name> ::= D0 # deleting destructor
3034 // ::= D1 # complete object destructor
3035 // ::= D2 # base object destructor
3036 //
3037 switch (T) {
3038 case Dtor_Deleting:
3039 Out << "D0";
3040 break;
3041 case Dtor_Complete:
3042 Out << "D1";
3043 break;
3044 case Dtor_Base:
3045 Out << "D2";
3046 break;
3047 }
3048}
3049
John McCall6dbce192010-08-20 00:17:19 +00003050void CXXNameMangler::mangleTemplateArgs(
Argyrios Kyrtzidisb0c3e092011-09-22 20:07:03 +00003051 const ASTTemplateArgumentListInfo &TemplateArgs) {
John McCall6dbce192010-08-20 00:17:19 +00003052 // <template-args> ::= I <template-arg>+ E
3053 Out << 'I';
John McCall4f4e4132011-05-04 01:45:19 +00003054 for (unsigned i = 0, e = TemplateArgs.NumTemplateArgs; i != e; ++i)
3055 mangleTemplateArg(0, TemplateArgs.getTemplateArgs()[i].getArgument());
John McCall6dbce192010-08-20 00:17:19 +00003056 Out << 'E';
3057}
3058
Douglas Gregor20f0cc72010-04-23 03:10:43 +00003059void CXXNameMangler::mangleTemplateArgs(TemplateName Template,
3060 const TemplateArgument *TemplateArgs,
3061 unsigned NumTemplateArgs) {
3062 if (TemplateDecl *TD = Template.getAsTemplateDecl())
3063 return mangleTemplateArgs(*TD->getTemplateParameters(), TemplateArgs,
3064 NumTemplateArgs);
Sean Huntc3021132010-05-05 15:23:54 +00003065
John McCall4f4e4132011-05-04 01:45:19 +00003066 mangleUnresolvedTemplateArgs(TemplateArgs, NumTemplateArgs);
3067}
3068
3069void CXXNameMangler::mangleUnresolvedTemplateArgs(const TemplateArgument *args,
3070 unsigned numArgs) {
Douglas Gregor20f0cc72010-04-23 03:10:43 +00003071 // <template-args> ::= I <template-arg>+ E
3072 Out << 'I';
John McCall4f4e4132011-05-04 01:45:19 +00003073 for (unsigned i = 0; i != numArgs; ++i)
3074 mangleTemplateArg(0, args[i]);
Douglas Gregor20f0cc72010-04-23 03:10:43 +00003075 Out << 'E';
3076}
3077
Rafael Espindolad9800722010-03-11 14:07:00 +00003078void CXXNameMangler::mangleTemplateArgs(const TemplateParameterList &PL,
3079 const TemplateArgumentList &AL) {
Anders Carlsson7a0ba872009-05-15 16:09:15 +00003080 // <template-args> ::= I <template-arg>+ E
Benjamin Kramer35f59b62010-04-10 16:03:31 +00003081 Out << 'I';
Rafael Espindolad9800722010-03-11 14:07:00 +00003082 for (unsigned i = 0, e = AL.size(); i != e; ++i)
3083 mangleTemplateArg(PL.getParam(i), AL[i]);
Benjamin Kramer35f59b62010-04-10 16:03:31 +00003084 Out << 'E';
Anders Carlsson7a0ba872009-05-15 16:09:15 +00003085}
3086
Rafael Espindolad9800722010-03-11 14:07:00 +00003087void CXXNameMangler::mangleTemplateArgs(const TemplateParameterList &PL,
3088 const TemplateArgument *TemplateArgs,
Anders Carlsson7624f212009-09-18 02:42:01 +00003089 unsigned NumTemplateArgs) {
3090 // <template-args> ::= I <template-arg>+ E
Benjamin Kramer35f59b62010-04-10 16:03:31 +00003091 Out << 'I';
Daniel Dunbar7e0c1952009-11-21 09:17:15 +00003092 for (unsigned i = 0; i != NumTemplateArgs; ++i)
Rafael Espindolad9800722010-03-11 14:07:00 +00003093 mangleTemplateArg(PL.getParam(i), TemplateArgs[i]);
Benjamin Kramer35f59b62010-04-10 16:03:31 +00003094 Out << 'E';
Anders Carlsson7624f212009-09-18 02:42:01 +00003095}
3096
Rafael Espindolad9800722010-03-11 14:07:00 +00003097void CXXNameMangler::mangleTemplateArg(const NamedDecl *P,
Douglas Gregorf1588662011-07-12 15:18:55 +00003098 TemplateArgument A) {
Mike Stump1eb44332009-09-09 15:08:12 +00003099 // <template-arg> ::= <type> # type or template
Anders Carlsson7a0ba872009-05-15 16:09:15 +00003100 // ::= X <expression> E # expression
3101 // ::= <expr-primary> # simple expressions
Douglas Gregor4fc48662011-01-13 16:39:34 +00003102 // ::= J <template-arg>* E # argument pack
Douglas Gregorf1588662011-07-12 15:18:55 +00003103 // ::= sp <expression> # pack expansion of (C++0x)
3104 if (!A.isInstantiationDependent() || A.isDependent())
3105 A = Context.getASTContext().getCanonicalTemplateArgument(A);
3106
Anders Carlsson7a0ba872009-05-15 16:09:15 +00003107 switch (A.getKind()) {
Douglas Gregorf90b27a2011-01-03 22:36:02 +00003108 case TemplateArgument::Null:
3109 llvm_unreachable("Cannot mangle NULL template argument");
3110
Anders Carlsson7a0ba872009-05-15 16:09:15 +00003111 case TemplateArgument::Type:
3112 mangleType(A.getAsType());
3113 break;
Anders Carlsson9e85c742009-12-23 19:30:55 +00003114 case TemplateArgument::Template:
John McCallb6f532e2010-07-14 06:43:17 +00003115 // This is mangled as <type>.
3116 mangleType(A.getAsTemplate());
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00003117 break;
Douglas Gregora7fc9012011-01-05 18:58:31 +00003118 case TemplateArgument::TemplateExpansion:
Douglas Gregor4fc48662011-01-13 16:39:34 +00003119 // <type> ::= Dp <type> # pack expansion (C++0x)
Douglas Gregora7fc9012011-01-05 18:58:31 +00003120 Out << "Dp";
3121 mangleType(A.getAsTemplateOrTemplatePattern());
3122 break;
John McCall092beef2012-01-06 05:06:35 +00003123 case TemplateArgument::Expression: {
3124 // It's possible to end up with a DeclRefExpr here in certain
3125 // dependent cases, in which case we should mangle as a
3126 // declaration.
3127 const Expr *E = A.getAsExpr()->IgnoreParens();
3128 if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E)) {
3129 const ValueDecl *D = DRE->getDecl();
3130 if (isa<VarDecl>(D) || isa<FunctionDecl>(D)) {
3131 Out << "L";
3132 mangle(D, "_Z");
3133 Out << 'E';
3134 break;
3135 }
3136 }
3137
Anders Carlssond553f8c2009-09-21 01:21:10 +00003138 Out << 'X';
John McCall092beef2012-01-06 05:06:35 +00003139 mangleExpression(E);
Anders Carlssond553f8c2009-09-21 01:21:10 +00003140 Out << 'E';
3141 break;
John McCall092beef2012-01-06 05:06:35 +00003142 }
Anders Carlssone170ba72009-12-14 01:45:37 +00003143 case TemplateArgument::Integral:
Benjamin Kramer85524372012-06-07 15:09:51 +00003144 mangleIntegerLiteral(A.getIntegralType(), A.getAsIntegral());
Anders Carlsson7a0ba872009-05-15 16:09:15 +00003145 break;
Daniel Dunbar7e0c1952009-11-21 09:17:15 +00003146 case TemplateArgument::Declaration: {
Douglas Gregor20f0cc72010-04-23 03:10:43 +00003147 assert(P && "Missing template parameter for declaration argument");
Daniel Dunbar7e0c1952009-11-21 09:17:15 +00003148 // <expr-primary> ::= L <mangled-name> E # external name
Douglas Gregord2008e22012-04-06 22:40:38 +00003149 // <expr-primary> ::= L <type> 0 E
Rafael Espindolad9800722010-03-11 14:07:00 +00003150 // Clang produces AST's where pointer-to-member-function expressions
Daniel Dunbar7e0c1952009-11-21 09:17:15 +00003151 // and pointer-to-function expressions are represented as a declaration not
Rafael Espindolad9800722010-03-11 14:07:00 +00003152 // an expression. We compensate for it here to produce the correct mangling.
Rafael Espindolad9800722010-03-11 14:07:00 +00003153 const NonTypeTemplateParmDecl *Parameter = cast<NonTypeTemplateParmDecl>(P);
Douglas Gregord2008e22012-04-06 22:40:38 +00003154
3155 // Handle NULL pointer arguments.
3156 if (!A.getAsDecl()) {
3157 Out << "L";
3158 mangleType(Parameter->getType());
3159 Out << "0E";
3160 break;
3161 }
3162
3163
3164 NamedDecl *D = cast<NamedDecl>(A.getAsDecl());
John McCallc0a45592011-04-24 08:43:07 +00003165 bool compensateMangling = !Parameter->getType()->isReferenceType();
Rafael Espindolad9800722010-03-11 14:07:00 +00003166 if (compensateMangling) {
3167 Out << 'X';
3168 mangleOperatorName(OO_Amp, 1);
3169 }
3170
Daniel Dunbar7e0c1952009-11-21 09:17:15 +00003171 Out << 'L';
3172 // References to external entities use the mangled name; if the name would
3173 // not normally be manged then mangle it as unqualified.
3174 //
3175 // FIXME: The ABI specifies that external names here should have _Z, but
3176 // gcc leaves this off.
Rafael Espindolad9800722010-03-11 14:07:00 +00003177 if (compensateMangling)
3178 mangle(D, "_Z");
3179 else
3180 mangle(D, "Z");
Daniel Dunbar7e0c1952009-11-21 09:17:15 +00003181 Out << 'E';
Rafael Espindolad9800722010-03-11 14:07:00 +00003182
3183 if (compensateMangling)
3184 Out << 'E';
3185
Daniel Dunbar7e0c1952009-11-21 09:17:15 +00003186 break;
3187 }
Douglas Gregorf90b27a2011-01-03 22:36:02 +00003188
3189 case TemplateArgument::Pack: {
3190 // Note: proposal by Mike Herrick on 12/20/10
3191 Out << 'J';
3192 for (TemplateArgument::pack_iterator PA = A.pack_begin(),
3193 PAEnd = A.pack_end();
3194 PA != PAEnd; ++PA)
3195 mangleTemplateArg(P, *PA);
3196 Out << 'E';
3197 }
Daniel Dunbar7e0c1952009-11-21 09:17:15 +00003198 }
Anders Carlsson7a0ba872009-05-15 16:09:15 +00003199}
3200
Anders Carlsson0ccdf8d2009-09-27 00:38:53 +00003201void CXXNameMangler::mangleTemplateParameter(unsigned Index) {
3202 // <template-param> ::= T_ # first template parameter
3203 // ::= T <parameter-2 non-negative number> _
3204 if (Index == 0)
3205 Out << "T_";
3206 else
3207 Out << 'T' << (Index - 1) << '_';
3208}
3209
John McCall68a51a72011-07-01 00:04:39 +00003210void CXXNameMangler::mangleExistingSubstitution(QualType type) {
3211 bool result = mangleSubstitution(type);
3212 assert(result && "no existing substitution for type");
3213 (void) result;
3214}
3215
3216void CXXNameMangler::mangleExistingSubstitution(TemplateName tname) {
3217 bool result = mangleSubstitution(tname);
3218 assert(result && "no existing substitution for template name");
3219 (void) result;
3220}
3221
Anders Carlsson76967372009-09-17 00:43:46 +00003222// <substitution> ::= S <seq-id> _
3223// ::= S_
Anders Carlsson6862fc72009-09-17 04:16:28 +00003224bool CXXNameMangler::mangleSubstitution(const NamedDecl *ND) {
Anders Carlssone7c8cb62009-09-26 20:53:44 +00003225 // Try one of the standard substitutions first.
3226 if (mangleStandardSubstitution(ND))
3227 return true;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00003228
Anders Carlsson433d1372009-11-07 04:26:04 +00003229 ND = cast<NamedDecl>(ND->getCanonicalDecl());
Anders Carlsson6862fc72009-09-17 04:16:28 +00003230 return mangleSubstitution(reinterpret_cast<uintptr_t>(ND));
3231}
3232
Douglas Gregor14795c82011-12-03 18:24:43 +00003233/// \brief Determine whether the given type has any qualifiers that are
3234/// relevant for substitutions.
3235static bool hasMangledSubstitutionQualifiers(QualType T) {
3236 Qualifiers Qs = T.getQualifiers();
3237 return Qs.getCVRQualifiers() || Qs.hasAddressSpace();
3238}
3239
Anders Carlsson76967372009-09-17 00:43:46 +00003240bool CXXNameMangler::mangleSubstitution(QualType T) {
Douglas Gregor14795c82011-12-03 18:24:43 +00003241 if (!hasMangledSubstitutionQualifiers(T)) {
Anders Carlssond99edc42009-09-26 03:55:37 +00003242 if (const RecordType *RT = T->getAs<RecordType>())
3243 return mangleSubstitution(RT->getDecl());
3244 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00003245
Anders Carlsson76967372009-09-17 00:43:46 +00003246 uintptr_t TypePtr = reinterpret_cast<uintptr_t>(T.getAsOpaquePtr());
3247
Anders Carlssond3a932a2009-09-17 03:53:28 +00003248 return mangleSubstitution(TypePtr);
3249}
3250
Douglas Gregor1e9268e2010-04-28 05:58:56 +00003251bool CXXNameMangler::mangleSubstitution(TemplateName Template) {
3252 if (TemplateDecl *TD = Template.getAsTemplateDecl())
3253 return mangleSubstitution(TD);
Sean Huntc3021132010-05-05 15:23:54 +00003254
Douglas Gregor1e9268e2010-04-28 05:58:56 +00003255 Template = Context.getASTContext().getCanonicalTemplateName(Template);
3256 return mangleSubstitution(
3257 reinterpret_cast<uintptr_t>(Template.getAsVoidPointer()));
3258}
3259
Anders Carlssond3a932a2009-09-17 03:53:28 +00003260bool CXXNameMangler::mangleSubstitution(uintptr_t Ptr) {
Benjamin Kramer35f59b62010-04-10 16:03:31 +00003261 llvm::DenseMap<uintptr_t, unsigned>::iterator I = Substitutions.find(Ptr);
Anders Carlsson76967372009-09-17 00:43:46 +00003262 if (I == Substitutions.end())
3263 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00003264
Anders Carlsson76967372009-09-17 00:43:46 +00003265 unsigned SeqID = I->second;
3266 if (SeqID == 0)
3267 Out << "S_";
3268 else {
3269 SeqID--;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00003270
Anders Carlsson76967372009-09-17 00:43:46 +00003271 // <seq-id> is encoded in base-36, using digits and upper case letters.
3272 char Buffer[10];
Benjamin Kramer35f59b62010-04-10 16:03:31 +00003273 char *BufferPtr = llvm::array_endof(Buffer);
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00003274
Anders Carlsson76967372009-09-17 00:43:46 +00003275 if (SeqID == 0) *--BufferPtr = '0';
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00003276
Anders Carlsson76967372009-09-17 00:43:46 +00003277 while (SeqID) {
3278 assert(BufferPtr > Buffer && "Buffer overflow!");
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00003279
John McCall6ab30e02010-06-09 07:26:17 +00003280 char c = static_cast<char>(SeqID % 36);
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00003281
Anders Carlsson76967372009-09-17 00:43:46 +00003282 *--BufferPtr = (c < 10 ? '0' + c : 'A' + c - 10);
3283 SeqID /= 36;
3284 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00003285
Benjamin Kramer35f59b62010-04-10 16:03:31 +00003286 Out << 'S'
Chris Lattner5f9e2722011-07-23 10:55:15 +00003287 << StringRef(BufferPtr, llvm::array_endof(Buffer)-BufferPtr)
Benjamin Kramer35f59b62010-04-10 16:03:31 +00003288 << '_';
Anders Carlsson76967372009-09-17 00:43:46 +00003289 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00003290
Anders Carlsson76967372009-09-17 00:43:46 +00003291 return true;
3292}
3293
Anders Carlssonf514b542009-09-27 00:12:57 +00003294static bool isCharType(QualType T) {
3295 if (T.isNull())
3296 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00003297
Anders Carlssonf514b542009-09-27 00:12:57 +00003298 return T->isSpecificBuiltinType(BuiltinType::Char_S) ||
3299 T->isSpecificBuiltinType(BuiltinType::Char_U);
3300}
3301
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00003302/// isCharSpecialization - Returns whether a given type is a template
Anders Carlssonf514b542009-09-27 00:12:57 +00003303/// specialization of a given name with a single argument of type char.
3304static bool isCharSpecialization(QualType T, const char *Name) {
3305 if (T.isNull())
3306 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00003307
Anders Carlssonf514b542009-09-27 00:12:57 +00003308 const RecordType *RT = T->getAs<RecordType>();
3309 if (!RT)
3310 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00003311
3312 const ClassTemplateSpecializationDecl *SD =
Anders Carlssonf514b542009-09-27 00:12:57 +00003313 dyn_cast<ClassTemplateSpecializationDecl>(RT->getDecl());
3314 if (!SD)
3315 return false;
3316
Douglas Gregorccc1b5e2012-02-21 00:37:24 +00003317 if (!isStdNamespace(getEffectiveDeclContext(SD)))
Anders Carlssonf514b542009-09-27 00:12:57 +00003318 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00003319
Anders Carlssonf514b542009-09-27 00:12:57 +00003320 const TemplateArgumentList &TemplateArgs = SD->getTemplateArgs();
3321 if (TemplateArgs.size() != 1)
3322 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00003323
Anders Carlssonf514b542009-09-27 00:12:57 +00003324 if (!isCharType(TemplateArgs[0].getAsType()))
3325 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00003326
Daniel Dunbar01eb9b92009-10-18 21:17:35 +00003327 return SD->getIdentifier()->getName() == Name;
Anders Carlssonf514b542009-09-27 00:12:57 +00003328}
3329
Anders Carlsson91f88602009-12-07 19:56:42 +00003330template <std::size_t StrLen>
Benjamin Kramer54353f42010-11-25 18:29:30 +00003331static bool isStreamCharSpecialization(const ClassTemplateSpecializationDecl*SD,
3332 const char (&Str)[StrLen]) {
Anders Carlsson91f88602009-12-07 19:56:42 +00003333 if (!SD->getIdentifier()->isStr(Str))
3334 return false;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00003335
Anders Carlsson91f88602009-12-07 19:56:42 +00003336 const TemplateArgumentList &TemplateArgs = SD->getTemplateArgs();
3337 if (TemplateArgs.size() != 2)
3338 return false;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00003339
Anders Carlsson91f88602009-12-07 19:56:42 +00003340 if (!isCharType(TemplateArgs[0].getAsType()))
3341 return false;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00003342
Anders Carlsson91f88602009-12-07 19:56:42 +00003343 if (!isCharSpecialization(TemplateArgs[1].getAsType(), "char_traits"))
3344 return false;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00003345
Anders Carlsson91f88602009-12-07 19:56:42 +00003346 return true;
3347}
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00003348
Anders Carlssone7c8cb62009-09-26 20:53:44 +00003349bool CXXNameMangler::mangleStandardSubstitution(const NamedDecl *ND) {
3350 // <substitution> ::= St # ::std::
Anders Carlsson8c031552009-09-26 23:10:05 +00003351 if (const NamespaceDecl *NS = dyn_cast<NamespaceDecl>(ND)) {
Anders Carlsson47846d22009-12-04 06:23:23 +00003352 if (isStd(NS)) {
Anders Carlsson8c031552009-09-26 23:10:05 +00003353 Out << "St";
3354 return true;
3355 }
3356 }
3357
3358 if (const ClassTemplateDecl *TD = dyn_cast<ClassTemplateDecl>(ND)) {
Douglas Gregorccc1b5e2012-02-21 00:37:24 +00003359 if (!isStdNamespace(getEffectiveDeclContext(TD)))
Anders Carlsson8c031552009-09-26 23:10:05 +00003360 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00003361
Anders Carlsson8c031552009-09-26 23:10:05 +00003362 // <substitution> ::= Sa # ::std::allocator
3363 if (TD->getIdentifier()->isStr("allocator")) {
3364 Out << "Sa";
3365 return true;
3366 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00003367
Anders Carlsson189d59c2009-09-26 23:14:39 +00003368 // <<substitution> ::= Sb # ::std::basic_string
3369 if (TD->getIdentifier()->isStr("basic_string")) {
3370 Out << "Sb";
3371 return true;
3372 }
Anders Carlsson8c031552009-09-26 23:10:05 +00003373 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00003374
3375 if (const ClassTemplateSpecializationDecl *SD =
Anders Carlssonf514b542009-09-27 00:12:57 +00003376 dyn_cast<ClassTemplateSpecializationDecl>(ND)) {
Douglas Gregorccc1b5e2012-02-21 00:37:24 +00003377 if (!isStdNamespace(getEffectiveDeclContext(SD)))
Eli Friedman5370ee22010-02-23 18:25:09 +00003378 return false;
3379
Anders Carlssonf514b542009-09-27 00:12:57 +00003380 // <substitution> ::= Ss # ::std::basic_string<char,
3381 // ::std::char_traits<char>,
3382 // ::std::allocator<char> >
3383 if (SD->getIdentifier()->isStr("basic_string")) {
3384 const TemplateArgumentList &TemplateArgs = SD->getTemplateArgs();
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00003385
Anders Carlssonf514b542009-09-27 00:12:57 +00003386 if (TemplateArgs.size() != 3)
3387 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00003388
Anders Carlssonf514b542009-09-27 00:12:57 +00003389 if (!isCharType(TemplateArgs[0].getAsType()))
3390 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00003391
Anders Carlssonf514b542009-09-27 00:12:57 +00003392 if (!isCharSpecialization(TemplateArgs[1].getAsType(), "char_traits"))
3393 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00003394
Anders Carlssonf514b542009-09-27 00:12:57 +00003395 if (!isCharSpecialization(TemplateArgs[2].getAsType(), "allocator"))
3396 return false;
3397
3398 Out << "Ss";
3399 return true;
3400 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00003401
Anders Carlsson91f88602009-12-07 19:56:42 +00003402 // <substitution> ::= Si # ::std::basic_istream<char,
3403 // ::std::char_traits<char> >
3404 if (isStreamCharSpecialization(SD, "basic_istream")) {
3405 Out << "Si";
3406 return true;
3407 }
3408
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00003409 // <substitution> ::= So # ::std::basic_ostream<char,
Anders Carlsson8f8fd8e2009-10-08 17:20:26 +00003410 // ::std::char_traits<char> >
Anders Carlsson91f88602009-12-07 19:56:42 +00003411 if (isStreamCharSpecialization(SD, "basic_ostream")) {
Anders Carlsson8f8fd8e2009-10-08 17:20:26 +00003412 Out << "So";
3413 return true;
3414 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00003415
Anders Carlsson91f88602009-12-07 19:56:42 +00003416 // <substitution> ::= Sd # ::std::basic_iostream<char,
3417 // ::std::char_traits<char> >
3418 if (isStreamCharSpecialization(SD, "basic_iostream")) {
3419 Out << "Sd";
3420 return true;
3421 }
Anders Carlssonf514b542009-09-27 00:12:57 +00003422 }
Anders Carlsson8c031552009-09-26 23:10:05 +00003423 return false;
Anders Carlssone7c8cb62009-09-26 20:53:44 +00003424}
3425
Anders Carlsson76967372009-09-17 00:43:46 +00003426void CXXNameMangler::addSubstitution(QualType T) {
Douglas Gregor14795c82011-12-03 18:24:43 +00003427 if (!hasMangledSubstitutionQualifiers(T)) {
Anders Carlssond99edc42009-09-26 03:55:37 +00003428 if (const RecordType *RT = T->getAs<RecordType>()) {
3429 addSubstitution(RT->getDecl());
3430 return;
3431 }
3432 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00003433
Anders Carlsson76967372009-09-17 00:43:46 +00003434 uintptr_t TypePtr = reinterpret_cast<uintptr_t>(T.getAsOpaquePtr());
Anders Carlssond3a932a2009-09-17 03:53:28 +00003435 addSubstitution(TypePtr);
3436}
3437
Douglas Gregor1e9268e2010-04-28 05:58:56 +00003438void CXXNameMangler::addSubstitution(TemplateName Template) {
3439 if (TemplateDecl *TD = Template.getAsTemplateDecl())
3440 return addSubstitution(TD);
Sean Huntc3021132010-05-05 15:23:54 +00003441
Douglas Gregor1e9268e2010-04-28 05:58:56 +00003442 Template = Context.getASTContext().getCanonicalTemplateName(Template);
3443 addSubstitution(reinterpret_cast<uintptr_t>(Template.getAsVoidPointer()));
3444}
3445
Anders Carlssond3a932a2009-09-17 03:53:28 +00003446void CXXNameMangler::addSubstitution(uintptr_t Ptr) {
Anders Carlssond3a932a2009-09-17 03:53:28 +00003447 assert(!Substitutions.count(Ptr) && "Substitution already exists!");
Anders Carlsson9d85b722010-06-02 04:29:50 +00003448 Substitutions[Ptr] = SeqID++;
Anders Carlsson76967372009-09-17 00:43:46 +00003449}
3450
Daniel Dunbar1b077112009-11-21 09:06:10 +00003451//
Mike Stump1eb44332009-09-09 15:08:12 +00003452
Daniel Dunbar1b077112009-11-21 09:06:10 +00003453/// \brief Mangles the name of the declaration D and emits that name to the
3454/// given output stream.
3455///
3456/// If the declaration D requires a mangled name, this routine will emit that
3457/// mangled name to \p os and return true. Otherwise, \p os will be unchanged
3458/// and this routine will return false. In this case, the caller should just
3459/// emit the identifier of the declaration (\c D->getIdentifier()) as its
3460/// name.
Peter Collingbourne14110472011-01-13 18:57:25 +00003461void ItaniumMangleContext::mangleName(const NamedDecl *D,
Chris Lattner5f9e2722011-07-23 10:55:15 +00003462 raw_ostream &Out) {
Daniel Dunbarc02ab4c2009-11-21 09:14:44 +00003463 assert((isa<FunctionDecl>(D) || isa<VarDecl>(D)) &&
3464 "Invalid mangleName() call, argument is not a variable or function!");
3465 assert(!isa<CXXConstructorDecl>(D) && !isa<CXXDestructorDecl>(D) &&
3466 "Invalid mangleName() call on 'structor decl!");
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00003467
Daniel Dunbar1b077112009-11-21 09:06:10 +00003468 PrettyStackTraceDecl CrashInfo(D, SourceLocation(),
3469 getASTContext().getSourceManager(),
3470 "Mangling declaration");
Mike Stump1eb44332009-09-09 15:08:12 +00003471
John McCallfb44de92011-05-01 22:35:37 +00003472 CXXNameMangler Mangler(*this, Out, D);
Daniel Dunbar94fd26d2009-11-21 09:06:22 +00003473 return Mangler.mangle(D);
Daniel Dunbar1b077112009-11-21 09:06:10 +00003474}
Mike Stump1eb44332009-09-09 15:08:12 +00003475
Peter Collingbourne14110472011-01-13 18:57:25 +00003476void ItaniumMangleContext::mangleCXXCtor(const CXXConstructorDecl *D,
3477 CXXCtorType Type,
Chris Lattner5f9e2722011-07-23 10:55:15 +00003478 raw_ostream &Out) {
Rafael Espindolac4850c22011-02-10 23:59:36 +00003479 CXXNameMangler Mangler(*this, Out, D, Type);
Daniel Dunbar77939c92009-11-21 09:06:31 +00003480 Mangler.mangle(D);
Daniel Dunbar1b077112009-11-21 09:06:10 +00003481}
Mike Stump1eb44332009-09-09 15:08:12 +00003482
Peter Collingbourne14110472011-01-13 18:57:25 +00003483void ItaniumMangleContext::mangleCXXDtor(const CXXDestructorDecl *D,
3484 CXXDtorType Type,
Chris Lattner5f9e2722011-07-23 10:55:15 +00003485 raw_ostream &Out) {
Rafael Espindolac4850c22011-02-10 23:59:36 +00003486 CXXNameMangler Mangler(*this, Out, D, Type);
Daniel Dunbar77939c92009-11-21 09:06:31 +00003487 Mangler.mangle(D);
Daniel Dunbar1b077112009-11-21 09:06:10 +00003488}
Mike Stumpf1216772009-07-31 18:25:34 +00003489
Peter Collingbourne14110472011-01-13 18:57:25 +00003490void ItaniumMangleContext::mangleThunk(const CXXMethodDecl *MD,
3491 const ThunkInfo &Thunk,
Chris Lattner5f9e2722011-07-23 10:55:15 +00003492 raw_ostream &Out) {
Anders Carlsson19879c92010-03-23 17:17:29 +00003493 // <special-name> ::= T <call-offset> <base encoding>
3494 // # base is the nominal target function of thunk
3495 // <special-name> ::= Tc <call-offset> <call-offset> <base encoding>
3496 // # base is the nominal target function of thunk
3497 // # first call-offset is 'this' adjustment
3498 // # second call-offset is result adjustment
Sean Huntc3021132010-05-05 15:23:54 +00003499
Anders Carlsson19879c92010-03-23 17:17:29 +00003500 assert(!isa<CXXDestructorDecl>(MD) &&
3501 "Use mangleCXXDtor for destructor decls!");
Rafael Espindolac4850c22011-02-10 23:59:36 +00003502 CXXNameMangler Mangler(*this, Out);
Anders Carlsson19879c92010-03-23 17:17:29 +00003503 Mangler.getStream() << "_ZT";
3504 if (!Thunk.Return.isEmpty())
3505 Mangler.getStream() << 'c';
Sean Huntc3021132010-05-05 15:23:54 +00003506
Anders Carlsson19879c92010-03-23 17:17:29 +00003507 // Mangle the 'this' pointer adjustment.
3508 Mangler.mangleCallOffset(Thunk.This.NonVirtual, Thunk.This.VCallOffsetOffset);
Sean Huntc3021132010-05-05 15:23:54 +00003509
Anders Carlsson19879c92010-03-23 17:17:29 +00003510 // Mangle the return pointer adjustment if there is one.
3511 if (!Thunk.Return.isEmpty())
3512 Mangler.mangleCallOffset(Thunk.Return.NonVirtual,
3513 Thunk.Return.VBaseOffsetOffset);
Sean Huntc3021132010-05-05 15:23:54 +00003514
Anders Carlsson19879c92010-03-23 17:17:29 +00003515 Mangler.mangleFunctionEncoding(MD);
3516}
3517
Sean Huntc3021132010-05-05 15:23:54 +00003518void
Peter Collingbourne14110472011-01-13 18:57:25 +00003519ItaniumMangleContext::mangleCXXDtorThunk(const CXXDestructorDecl *DD,
3520 CXXDtorType Type,
3521 const ThisAdjustment &ThisAdjustment,
Chris Lattner5f9e2722011-07-23 10:55:15 +00003522 raw_ostream &Out) {
Anders Carlsson19879c92010-03-23 17:17:29 +00003523 // <special-name> ::= T <call-offset> <base encoding>
3524 // # base is the nominal target function of thunk
Rafael Espindolac4850c22011-02-10 23:59:36 +00003525 CXXNameMangler Mangler(*this, Out, DD, Type);
Anders Carlsson19879c92010-03-23 17:17:29 +00003526 Mangler.getStream() << "_ZT";
3527
3528 // Mangle the 'this' pointer adjustment.
Sean Huntc3021132010-05-05 15:23:54 +00003529 Mangler.mangleCallOffset(ThisAdjustment.NonVirtual,
Anders Carlsson19879c92010-03-23 17:17:29 +00003530 ThisAdjustment.VCallOffsetOffset);
3531
3532 Mangler.mangleFunctionEncoding(DD);
3533}
3534
Daniel Dunbarc0747712009-11-21 09:12:13 +00003535/// mangleGuardVariable - Returns the mangled name for a guard variable
3536/// for the passed in VarDecl.
Peter Collingbourne14110472011-01-13 18:57:25 +00003537void ItaniumMangleContext::mangleItaniumGuardVariable(const VarDecl *D,
Chris Lattner5f9e2722011-07-23 10:55:15 +00003538 raw_ostream &Out) {
Daniel Dunbarc0747712009-11-21 09:12:13 +00003539 // <special-name> ::= GV <object name> # Guard variable for one-time
3540 // # initialization
Rafael Espindolac4850c22011-02-10 23:59:36 +00003541 CXXNameMangler Mangler(*this, Out);
Daniel Dunbarc0747712009-11-21 09:12:13 +00003542 Mangler.getStream() << "_ZGV";
3543 Mangler.mangleName(D);
3544}
3545
Peter Collingbourne14110472011-01-13 18:57:25 +00003546void ItaniumMangleContext::mangleReferenceTemporary(const VarDecl *D,
Chris Lattner5f9e2722011-07-23 10:55:15 +00003547 raw_ostream &Out) {
Anders Carlsson715edf22010-06-26 16:09:40 +00003548 // We match the GCC mangling here.
3549 // <special-name> ::= GR <object name>
Rafael Espindolac4850c22011-02-10 23:59:36 +00003550 CXXNameMangler Mangler(*this, Out);
Anders Carlsson715edf22010-06-26 16:09:40 +00003551 Mangler.getStream() << "_ZGR";
3552 Mangler.mangleName(D);
3553}
3554
Peter Collingbourne14110472011-01-13 18:57:25 +00003555void ItaniumMangleContext::mangleCXXVTable(const CXXRecordDecl *RD,
Chris Lattner5f9e2722011-07-23 10:55:15 +00003556 raw_ostream &Out) {
Daniel Dunbarc0747712009-11-21 09:12:13 +00003557 // <special-name> ::= TV <type> # virtual table
Rafael Espindolac4850c22011-02-10 23:59:36 +00003558 CXXNameMangler Mangler(*this, Out);
Daniel Dunbarc0747712009-11-21 09:12:13 +00003559 Mangler.getStream() << "_ZTV";
Douglas Gregor1b12a3b2010-05-26 05:11:13 +00003560 Mangler.mangleNameOrStandardSubstitution(RD);
Daniel Dunbar1b077112009-11-21 09:06:10 +00003561}
Mike Stump82d75b02009-11-10 01:58:37 +00003562
Peter Collingbourne14110472011-01-13 18:57:25 +00003563void ItaniumMangleContext::mangleCXXVTT(const CXXRecordDecl *RD,
Chris Lattner5f9e2722011-07-23 10:55:15 +00003564 raw_ostream &Out) {
Daniel Dunbarc0747712009-11-21 09:12:13 +00003565 // <special-name> ::= TT <type> # VTT structure
Rafael Espindolac4850c22011-02-10 23:59:36 +00003566 CXXNameMangler Mangler(*this, Out);
Daniel Dunbarc0747712009-11-21 09:12:13 +00003567 Mangler.getStream() << "_ZTT";
Douglas Gregor1b12a3b2010-05-26 05:11:13 +00003568 Mangler.mangleNameOrStandardSubstitution(RD);
Daniel Dunbar1b077112009-11-21 09:06:10 +00003569}
Mike Stumpab3f7e92009-11-10 01:41:59 +00003570
Peter Collingbourne14110472011-01-13 18:57:25 +00003571void ItaniumMangleContext::mangleCXXCtorVTable(const CXXRecordDecl *RD,
3572 int64_t Offset,
3573 const CXXRecordDecl *Type,
Chris Lattner5f9e2722011-07-23 10:55:15 +00003574 raw_ostream &Out) {
Daniel Dunbarc0747712009-11-21 09:12:13 +00003575 // <special-name> ::= TC <type> <offset number> _ <base type>
Rafael Espindolac4850c22011-02-10 23:59:36 +00003576 CXXNameMangler Mangler(*this, Out);
Daniel Dunbarc0747712009-11-21 09:12:13 +00003577 Mangler.getStream() << "_ZTC";
Douglas Gregor1b12a3b2010-05-26 05:11:13 +00003578 Mangler.mangleNameOrStandardSubstitution(RD);
Daniel Dunbarc0747712009-11-21 09:12:13 +00003579 Mangler.getStream() << Offset;
Benjamin Kramer35f59b62010-04-10 16:03:31 +00003580 Mangler.getStream() << '_';
Douglas Gregor1b12a3b2010-05-26 05:11:13 +00003581 Mangler.mangleNameOrStandardSubstitution(Type);
Daniel Dunbar1b077112009-11-21 09:06:10 +00003582}
Mike Stump738f8c22009-07-31 23:15:31 +00003583
Peter Collingbourne14110472011-01-13 18:57:25 +00003584void ItaniumMangleContext::mangleCXXRTTI(QualType Ty,
Chris Lattner5f9e2722011-07-23 10:55:15 +00003585 raw_ostream &Out) {
Daniel Dunbarc0747712009-11-21 09:12:13 +00003586 // <special-name> ::= TI <type> # typeinfo structure
Douglas Gregor154fe982009-12-23 22:04:40 +00003587 assert(!Ty.hasQualifiers() && "RTTI info cannot have top-level qualifiers");
Rafael Espindolac4850c22011-02-10 23:59:36 +00003588 CXXNameMangler Mangler(*this, Out);
Daniel Dunbarc0747712009-11-21 09:12:13 +00003589 Mangler.getStream() << "_ZTI";
3590 Mangler.mangleType(Ty);
Daniel Dunbar1b077112009-11-21 09:06:10 +00003591}
Mike Stump67795982009-11-14 00:14:13 +00003592
Peter Collingbourne14110472011-01-13 18:57:25 +00003593void ItaniumMangleContext::mangleCXXRTTIName(QualType Ty,
Chris Lattner5f9e2722011-07-23 10:55:15 +00003594 raw_ostream &Out) {
Daniel Dunbarc0747712009-11-21 09:12:13 +00003595 // <special-name> ::= TS <type> # typeinfo name (null terminated byte string)
Rafael Espindolac4850c22011-02-10 23:59:36 +00003596 CXXNameMangler Mangler(*this, Out);
Daniel Dunbarc0747712009-11-21 09:12:13 +00003597 Mangler.getStream() << "_ZTS";
3598 Mangler.mangleType(Ty);
Mike Stumpf1216772009-07-31 18:25:34 +00003599}
Peter Collingbourne14110472011-01-13 18:57:25 +00003600
3601MangleContext *clang::createItaniumMangleContext(ASTContext &Context,
David Blaikied6471f72011-09-25 23:23:43 +00003602 DiagnosticsEngine &Diags) {
Peter Collingbourne14110472011-01-13 18:57:25 +00003603 return new ItaniumMangleContext(Context, Diags);
3604}