blob: fc61d88bd9fca73a60bc76fcc342c045831646cb [file] [log] [blame]
Peter Collingbourne14110472011-01-13 18:57:25 +00001//===--- ItaniumMangle.cpp - Itanium C++ Name Mangling ----------*- C++ -*-===//
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// Implements C++ name mangling according to the Itanium C++ ABI,
11// which is used in GCC 3.2 and newer (and many compilers that are
12// ABI-compatible with GCC):
13//
14// http://www.codesourcery.com/public/cxx-abi/abi.html
15//
16//===----------------------------------------------------------------------===//
Peter Collingbourne14110472011-01-13 18:57:25 +000017#include "clang/AST/Mangle.h"
Douglas Gregor5f2bfd42009-02-13 00:10:09 +000018#include "clang/AST/ASTContext.h"
19#include "clang/AST/Decl.h"
20#include "clang/AST/DeclCXX.h"
Anders Carlssona40c5e42009-03-07 22:03:21 +000021#include "clang/AST/DeclObjC.h"
Anders Carlsson7a0ba872009-05-15 16:09:15 +000022#include "clang/AST/DeclTemplate.h"
Anders Carlsson50755b02009-09-27 20:11:34 +000023#include "clang/AST/ExprCXX.h"
John McCallf85e1932011-06-15 23:02:42 +000024#include "clang/AST/ExprObjC.h"
John McCallfb44de92011-05-01 22:35:37 +000025#include "clang/AST/TypeLoc.h"
Peter Collingbourne14110472011-01-13 18:57:25 +000026#include "clang/Basic/ABI.h"
Douglas Gregor6ec36682009-02-18 23:53:56 +000027#include "clang/Basic/SourceManager.h"
Rafael Espindola4e274e92011-02-15 22:23:51 +000028#include "clang/Basic/TargetInfo.h"
Anders Carlssonc4355b62009-10-07 01:45:02 +000029#include "llvm/ADT/StringExtras.h"
Douglas Gregor5f2bfd42009-02-13 00:10:09 +000030#include "llvm/Support/raw_ostream.h"
John McCallefe6aee2009-09-05 07:56:18 +000031#include "llvm/Support/ErrorHandling.h"
Anders Carlssonf98574b2010-02-05 07:31:37 +000032
33#define MANGLE_CHECKER 0
34
35#if MANGLE_CHECKER
36#include <cxxabi.h>
37#endif
38
Douglas Gregor5f2bfd42009-02-13 00:10:09 +000039using namespace clang;
Charles Davis685b1d92010-05-26 18:25:27 +000040
Douglas Gregor5f2bfd42009-02-13 00:10:09 +000041namespace {
Fariborz Jahanian57058532010-03-03 19:41:08 +000042
Douglas Gregorccc1b5e2012-02-21 00:37:24 +000043/// \brief Retrieve the declaration context that should be used when mangling
44/// the given declaration.
45static const DeclContext *getEffectiveDeclContext(const Decl *D) {
46 // The ABI assumes that lambda closure types that occur within
47 // default arguments live in the context of the function. However, due to
48 // the way in which Clang parses and creates function declarations, this is
49 // not the case: the lambda closure type ends up living in the context
50 // where the function itself resides, because the function declaration itself
51 // had not yet been created. Fix the context here.
52 if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D)) {
53 if (RD->isLambda())
54 if (ParmVarDecl *ContextParam
Douglas Gregor5878cbc2012-02-21 04:17:39 +000055 = dyn_cast_or_null<ParmVarDecl>(RD->getLambdaContextDecl()))
Douglas Gregorccc1b5e2012-02-21 00:37:24 +000056 return ContextParam->getDeclContext();
57 }
58
59 return D->getDeclContext();
60}
61
62static const DeclContext *getEffectiveParentContext(const DeclContext *DC) {
63 return getEffectiveDeclContext(cast<Decl>(DC));
64}
65
John McCall82b7d7b2010-10-18 21:28:44 +000066static const CXXRecordDecl *GetLocalClassDecl(const NamedDecl *ND) {
67 const DeclContext *DC = dyn_cast<DeclContext>(ND);
68 if (!DC)
Douglas Gregorccc1b5e2012-02-21 00:37:24 +000069 DC = getEffectiveDeclContext(ND);
John McCall82b7d7b2010-10-18 21:28:44 +000070 while (!DC->isNamespace() && !DC->isTranslationUnit()) {
Douglas Gregorccc1b5e2012-02-21 00:37:24 +000071 const DeclContext *Parent = getEffectiveDeclContext(cast<Decl>(DC));
72 if (isa<FunctionDecl>(Parent))
John McCall82b7d7b2010-10-18 21:28:44 +000073 return dyn_cast<CXXRecordDecl>(DC);
Douglas Gregorccc1b5e2012-02-21 00:37:24 +000074 DC = Parent;
Fariborz Jahanian57058532010-03-03 19:41:08 +000075 }
76 return 0;
77}
78
John McCallfb44de92011-05-01 22:35:37 +000079static const FunctionDecl *getStructor(const FunctionDecl *fn) {
80 if (const FunctionTemplateDecl *ftd = fn->getPrimaryTemplate())
81 return ftd->getTemplatedDecl();
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +000082
John McCallfb44de92011-05-01 22:35:37 +000083 return fn;
84}
Anders Carlsson7e120032009-11-24 05:36:32 +000085
John McCallfb44de92011-05-01 22:35:37 +000086static const NamedDecl *getStructor(const NamedDecl *decl) {
87 const FunctionDecl *fn = dyn_cast_or_null<FunctionDecl>(decl);
88 return (fn ? getStructor(fn) : decl);
Anders Carlsson7e120032009-11-24 05:36:32 +000089}
Douglas Gregorccc1b5e2012-02-21 00:37:24 +000090
John McCall1dd73832010-02-04 01:42:13 +000091static const unsigned UnknownArity = ~0U;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +000092
Peter Collingbourne14110472011-01-13 18:57:25 +000093class ItaniumMangleContext : public MangleContext {
94 llvm::DenseMap<const TagDecl *, uint64_t> AnonStructIds;
95 unsigned Discriminator;
96 llvm::DenseMap<const NamedDecl*, unsigned> Uniquifier;
97
98public:
99 explicit ItaniumMangleContext(ASTContext &Context,
David Blaikied6471f72011-09-25 23:23:43 +0000100 DiagnosticsEngine &Diags)
Peter Collingbourne14110472011-01-13 18:57:25 +0000101 : MangleContext(Context, Diags) { }
102
103 uint64_t getAnonymousStructId(const TagDecl *TD) {
104 std::pair<llvm::DenseMap<const TagDecl *,
105 uint64_t>::iterator, bool> Result =
106 AnonStructIds.insert(std::make_pair(TD, AnonStructIds.size()));
107 return Result.first->second;
108 }
109
110 void startNewFunction() {
111 MangleContext::startNewFunction();
112 mangleInitDiscriminator();
113 }
114
115 /// @name Mangler Entry Points
116 /// @{
117
118 bool shouldMangleDeclName(const NamedDecl *D);
Chris Lattner5f9e2722011-07-23 10:55:15 +0000119 void mangleName(const NamedDecl *D, raw_ostream &);
Peter Collingbourne14110472011-01-13 18:57:25 +0000120 void mangleThunk(const CXXMethodDecl *MD,
121 const ThunkInfo &Thunk,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000122 raw_ostream &);
Peter Collingbourne14110472011-01-13 18:57:25 +0000123 void mangleCXXDtorThunk(const CXXDestructorDecl *DD, CXXDtorType Type,
124 const ThisAdjustment &ThisAdjustment,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000125 raw_ostream &);
Peter Collingbourne14110472011-01-13 18:57:25 +0000126 void mangleReferenceTemporary(const VarDecl *D,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000127 raw_ostream &);
Peter Collingbourne14110472011-01-13 18:57:25 +0000128 void mangleCXXVTable(const CXXRecordDecl *RD,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000129 raw_ostream &);
Peter Collingbourne14110472011-01-13 18:57:25 +0000130 void mangleCXXVTT(const CXXRecordDecl *RD,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000131 raw_ostream &);
Peter Collingbourne14110472011-01-13 18:57:25 +0000132 void mangleCXXCtorVTable(const CXXRecordDecl *RD, int64_t Offset,
133 const CXXRecordDecl *Type,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000134 raw_ostream &);
135 void mangleCXXRTTI(QualType T, raw_ostream &);
136 void mangleCXXRTTIName(QualType T, raw_ostream &);
Peter Collingbourne14110472011-01-13 18:57:25 +0000137 void mangleCXXCtor(const CXXConstructorDecl *D, CXXCtorType Type,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000138 raw_ostream &);
Peter Collingbourne14110472011-01-13 18:57:25 +0000139 void mangleCXXDtor(const CXXDestructorDecl *D, CXXDtorType Type,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000140 raw_ostream &);
Peter Collingbourne14110472011-01-13 18:57:25 +0000141
Chris Lattner5f9e2722011-07-23 10:55:15 +0000142 void mangleItaniumGuardVariable(const VarDecl *D, raw_ostream &);
Peter Collingbourne14110472011-01-13 18:57:25 +0000143
144 void mangleInitDiscriminator() {
145 Discriminator = 0;
146 }
147
148 bool getNextDiscriminator(const NamedDecl *ND, unsigned &disc) {
Douglas Gregor9e8c92a2012-02-20 19:44:39 +0000149 // Lambda closure types with external linkage (indicated by a
150 // non-zero lambda mangling number) have their own numbering scheme, so
151 // they do not need a discriminator.
152 if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(ND))
153 if (RD->isLambda() && RD->getLambdaManglingNumber() > 0)
154 return false;
155
Peter Collingbourne14110472011-01-13 18:57:25 +0000156 unsigned &discriminator = Uniquifier[ND];
157 if (!discriminator)
158 discriminator = ++Discriminator;
159 if (discriminator == 1)
160 return false;
161 disc = discriminator-2;
162 return true;
163 }
164 /// @}
165};
166
Daniel Dunbar1b077112009-11-21 09:06:10 +0000167/// CXXNameMangler - Manage the mangling of a single name.
Daniel Dunbarc0747712009-11-21 09:12:13 +0000168class CXXNameMangler {
Peter Collingbourne14110472011-01-13 18:57:25 +0000169 ItaniumMangleContext &Context;
Chris Lattner5f9e2722011-07-23 10:55:15 +0000170 raw_ostream &Out;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000171
John McCallfb44de92011-05-01 22:35:37 +0000172 /// The "structor" is the top-level declaration being mangled, if
173 /// that's not a template specialization; otherwise it's the pattern
174 /// for that specialization.
175 const NamedDecl *Structor;
Daniel Dunbar1b077112009-11-21 09:06:10 +0000176 unsigned StructorType;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000177
Anders Carlsson9d85b722010-06-02 04:29:50 +0000178 /// SeqID - The next subsitution sequence number.
179 unsigned SeqID;
180
John McCallfb44de92011-05-01 22:35:37 +0000181 class FunctionTypeDepthState {
182 unsigned Bits;
183
184 enum { InResultTypeMask = 1 };
185
186 public:
187 FunctionTypeDepthState() : Bits(0) {}
188
189 /// The number of function types we're inside.
190 unsigned getDepth() const {
191 return Bits >> 1;
192 }
193
194 /// True if we're in the return type of the innermost function type.
195 bool isInResultType() const {
196 return Bits & InResultTypeMask;
197 }
198
199 FunctionTypeDepthState push() {
200 FunctionTypeDepthState tmp = *this;
201 Bits = (Bits & ~InResultTypeMask) + 2;
202 return tmp;
203 }
204
205 void enterResultType() {
206 Bits |= InResultTypeMask;
207 }
208
209 void leaveResultType() {
210 Bits &= ~InResultTypeMask;
211 }
212
213 void pop(FunctionTypeDepthState saved) {
214 assert(getDepth() == saved.getDepth() + 1);
215 Bits = saved.Bits;
216 }
217
218 } FunctionTypeDepth;
219
Daniel Dunbar1b077112009-11-21 09:06:10 +0000220 llvm::DenseMap<uintptr_t, unsigned> Substitutions;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000221
John McCall1dd73832010-02-04 01:42:13 +0000222 ASTContext &getASTContext() const { return Context.getASTContext(); }
223
Daniel Dunbarc0747712009-11-21 09:12:13 +0000224public:
Chris Lattner5f9e2722011-07-23 10:55:15 +0000225 CXXNameMangler(ItaniumMangleContext &C, raw_ostream &Out_,
John McCallfb44de92011-05-01 22:35:37 +0000226 const NamedDecl *D = 0)
227 : Context(C), Out(Out_), Structor(getStructor(D)), StructorType(0),
228 SeqID(0) {
229 // These can't be mangled without a ctor type or dtor type.
230 assert(!D || (!isa<CXXDestructorDecl>(D) &&
231 !isa<CXXConstructorDecl>(D)));
232 }
Chris Lattner5f9e2722011-07-23 10:55:15 +0000233 CXXNameMangler(ItaniumMangleContext &C, raw_ostream &Out_,
Daniel Dunbar77939c92009-11-21 09:06:31 +0000234 const CXXConstructorDecl *D, CXXCtorType Type)
Rafael Espindolac4850c22011-02-10 23:59:36 +0000235 : Context(C), Out(Out_), Structor(getStructor(D)), StructorType(Type),
John McCallfb44de92011-05-01 22:35:37 +0000236 SeqID(0) { }
Chris Lattner5f9e2722011-07-23 10:55:15 +0000237 CXXNameMangler(ItaniumMangleContext &C, raw_ostream &Out_,
Daniel Dunbar77939c92009-11-21 09:06:31 +0000238 const CXXDestructorDecl *D, CXXDtorType Type)
Rafael Espindolac4850c22011-02-10 23:59:36 +0000239 : Context(C), Out(Out_), Structor(getStructor(D)), StructorType(Type),
John McCallfb44de92011-05-01 22:35:37 +0000240 SeqID(0) { }
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000241
Anders Carlssonf98574b2010-02-05 07:31:37 +0000242#if MANGLE_CHECKER
243 ~CXXNameMangler() {
244 if (Out.str()[0] == '\01')
245 return;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000246
Anders Carlssonf98574b2010-02-05 07:31:37 +0000247 int status = 0;
248 char *result = abi::__cxa_demangle(Out.str().str().c_str(), 0, 0, &status);
249 assert(status == 0 && "Could not demangle mangled name!");
250 free(result);
251 }
252#endif
Chris Lattner5f9e2722011-07-23 10:55:15 +0000253 raw_ostream &getStream() { return Out; }
Daniel Dunbarc0747712009-11-21 09:12:13 +0000254
Chris Lattner5f9e2722011-07-23 10:55:15 +0000255 void mangle(const NamedDecl *D, StringRef Prefix = "_Z");
Anders Carlsson19879c92010-03-23 17:17:29 +0000256 void mangleCallOffset(int64_t NonVirtual, int64_t Virtual);
John McCall0512e482010-07-14 04:20:34 +0000257 void mangleNumber(const llvm::APSInt &I);
Anders Carlssona94822e2009-11-26 02:32:05 +0000258 void mangleNumber(int64_t Number);
John McCall0512e482010-07-14 04:20:34 +0000259 void mangleFloat(const llvm::APFloat &F);
Daniel Dunbarc0747712009-11-21 09:12:13 +0000260 void mangleFunctionEncoding(const FunctionDecl *FD);
261 void mangleName(const NamedDecl *ND);
262 void mangleType(QualType T);
Douglas Gregor1b12a3b2010-05-26 05:11:13 +0000263 void mangleNameOrStandardSubstitution(const NamedDecl *ND);
264
Daniel Dunbarc0747712009-11-21 09:12:13 +0000265private:
Daniel Dunbar1b077112009-11-21 09:06:10 +0000266 bool mangleSubstitution(const NamedDecl *ND);
267 bool mangleSubstitution(QualType T);
Douglas Gregor1e9268e2010-04-28 05:58:56 +0000268 bool mangleSubstitution(TemplateName Template);
Daniel Dunbar1b077112009-11-21 09:06:10 +0000269 bool mangleSubstitution(uintptr_t Ptr);
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000270
John McCall68a51a72011-07-01 00:04:39 +0000271 void mangleExistingSubstitution(QualType type);
272 void mangleExistingSubstitution(TemplateName name);
273
Daniel Dunbar1b077112009-11-21 09:06:10 +0000274 bool mangleStandardSubstitution(const NamedDecl *ND);
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000275
Daniel Dunbar1b077112009-11-21 09:06:10 +0000276 void addSubstitution(const NamedDecl *ND) {
277 ND = cast<NamedDecl>(ND->getCanonicalDecl());
Anders Carlsson433d1372009-11-07 04:26:04 +0000278
Daniel Dunbar1b077112009-11-21 09:06:10 +0000279 addSubstitution(reinterpret_cast<uintptr_t>(ND));
280 }
281 void addSubstitution(QualType T);
Douglas Gregor1e9268e2010-04-28 05:58:56 +0000282 void addSubstitution(TemplateName Template);
Daniel Dunbar1b077112009-11-21 09:06:10 +0000283 void addSubstitution(uintptr_t Ptr);
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000284
John McCalla0ce15c2011-04-24 08:23:24 +0000285 void mangleUnresolvedPrefix(NestedNameSpecifier *qualifier,
286 NamedDecl *firstQualifierLookup,
287 bool recursive = false);
288 void mangleUnresolvedName(NestedNameSpecifier *qualifier,
289 NamedDecl *firstQualifierLookup,
290 DeclarationName name,
John McCall1dd73832010-02-04 01:42:13 +0000291 unsigned KnownArity = UnknownArity);
292
Daniel Dunbar1b077112009-11-21 09:06:10 +0000293 void mangleName(const TemplateDecl *TD,
294 const TemplateArgument *TemplateArgs,
295 unsigned NumTemplateArgs);
John McCall1dd73832010-02-04 01:42:13 +0000296 void mangleUnqualifiedName(const NamedDecl *ND) {
297 mangleUnqualifiedName(ND, ND->getDeclName(), UnknownArity);
298 }
299 void mangleUnqualifiedName(const NamedDecl *ND, DeclarationName Name,
300 unsigned KnownArity);
Daniel Dunbar1b077112009-11-21 09:06:10 +0000301 void mangleUnscopedName(const NamedDecl *ND);
302 void mangleUnscopedTemplateName(const TemplateDecl *ND);
Douglas Gregor1e9268e2010-04-28 05:58:56 +0000303 void mangleUnscopedTemplateName(TemplateName);
Daniel Dunbar1b077112009-11-21 09:06:10 +0000304 void mangleSourceName(const IdentifierInfo *II);
305 void mangleLocalName(const NamedDecl *ND);
Douglas Gregorccc1b5e2012-02-21 00:37:24 +0000306 void mangleLambda(const CXXRecordDecl *Lambda);
Fariborz Jahanian57058532010-03-03 19:41:08 +0000307 void mangleNestedName(const NamedDecl *ND, const DeclContext *DC,
308 bool NoFunction=false);
Daniel Dunbar1b077112009-11-21 09:06:10 +0000309 void mangleNestedName(const TemplateDecl *TD,
310 const TemplateArgument *TemplateArgs,
311 unsigned NumTemplateArgs);
John McCalla0ce15c2011-04-24 08:23:24 +0000312 void manglePrefix(NestedNameSpecifier *qualifier);
Fariborz Jahanian57058532010-03-03 19:41:08 +0000313 void manglePrefix(const DeclContext *DC, bool NoFunction=false);
John McCall4f4e4132011-05-04 01:45:19 +0000314 void manglePrefix(QualType type);
Daniel Dunbar1b077112009-11-21 09:06:10 +0000315 void mangleTemplatePrefix(const TemplateDecl *ND);
Douglas Gregor20f0cc72010-04-23 03:10:43 +0000316 void mangleTemplatePrefix(TemplateName Template);
Daniel Dunbar1b077112009-11-21 09:06:10 +0000317 void mangleOperatorName(OverloadedOperatorKind OO, unsigned Arity);
318 void mangleQualifiers(Qualifiers Quals);
Douglas Gregor0a9a6d62011-01-26 17:36:28 +0000319 void mangleRefQualifier(RefQualifierKind RefQualifier);
John McCallefe6aee2009-09-05 07:56:18 +0000320
Anders Carlsson7b06f6c2009-12-10 03:14:39 +0000321 void mangleObjCMethodName(const ObjCMethodDecl *MD);
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000322
Daniel Dunbar1b077112009-11-21 09:06:10 +0000323 // Declare manglers for every type class.
John McCallefe6aee2009-09-05 07:56:18 +0000324#define ABSTRACT_TYPE(CLASS, PARENT)
325#define NON_CANONICAL_TYPE(CLASS, PARENT)
326#define TYPE(CLASS, PARENT) void mangleType(const CLASS##Type *T);
327#include "clang/AST/TypeNodes.def"
328
Daniel Dunbar1b077112009-11-21 09:06:10 +0000329 void mangleType(const TagType*);
John McCallb6f532e2010-07-14 06:43:17 +0000330 void mangleType(TemplateName);
Daniel Dunbar1b077112009-11-21 09:06:10 +0000331 void mangleBareFunctionType(const FunctionType *T,
332 bool MangleReturnType);
Bob Wilson57147a82010-11-16 00:32:18 +0000333 void mangleNeonVectorType(const VectorType *T);
Anders Carlssone170ba72009-12-14 01:45:37 +0000334
335 void mangleIntegerLiteral(QualType T, const llvm::APSInt &Value);
John McCalla0ce15c2011-04-24 08:23:24 +0000336 void mangleMemberExpr(const Expr *base, bool isArrow,
337 NestedNameSpecifier *qualifier,
338 NamedDecl *firstQualifierLookup,
339 DeclarationName name,
340 unsigned knownArity);
John McCall5e1e89b2010-08-18 19:18:59 +0000341 void mangleExpression(const Expr *E, unsigned Arity = UnknownArity);
Daniel Dunbar1b077112009-11-21 09:06:10 +0000342 void mangleCXXCtorType(CXXCtorType T);
343 void mangleCXXDtorType(CXXDtorType T);
Mike Stump1eb44332009-09-09 15:08:12 +0000344
Argyrios Kyrtzidisb0c3e092011-09-22 20:07:03 +0000345 void mangleTemplateArgs(const ASTTemplateArgumentListInfo &TemplateArgs);
Eli Friedmand7a6b162012-09-26 02:36:12 +0000346 void mangleTemplateArgs(const TemplateArgument *TemplateArgs,
Daniel Dunbar1b077112009-11-21 09:06:10 +0000347 unsigned NumTemplateArgs);
Eli Friedmand7a6b162012-09-26 02:36:12 +0000348 void mangleTemplateArgs(const TemplateArgumentList &AL);
349 void mangleTemplateArg(TemplateArgument A);
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000350
Daniel Dunbar1b077112009-11-21 09:06:10 +0000351 void mangleTemplateParameter(unsigned Index);
John McCallfb44de92011-05-01 22:35:37 +0000352
353 void mangleFunctionParam(const ParmVarDecl *parm);
Daniel Dunbar1b077112009-11-21 09:06:10 +0000354};
Peter Collingbourne14110472011-01-13 18:57:25 +0000355
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000356}
357
Anders Carlsson43f17402009-04-02 15:51:53 +0000358static bool isInCLinkageSpecification(const Decl *D) {
Douglas Gregor457e2812009-10-28 16:31:34 +0000359 D = D->getCanonicalDecl();
Douglas Gregorccc1b5e2012-02-21 00:37:24 +0000360 for (const DeclContext *DC = getEffectiveDeclContext(D);
361 !DC->isTranslationUnit(); DC = getEffectiveParentContext(DC)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000362 if (const LinkageSpecDecl *Linkage = dyn_cast<LinkageSpecDecl>(DC))
Anders Carlsson43f17402009-04-02 15:51:53 +0000363 return Linkage->getLanguage() == LinkageSpecDecl::lang_c;
364 }
Mike Stump1eb44332009-09-09 15:08:12 +0000365
Anders Carlsson43f17402009-04-02 15:51:53 +0000366 return false;
367}
368
Peter Collingbourne14110472011-01-13 18:57:25 +0000369bool ItaniumMangleContext::shouldMangleDeclName(const NamedDecl *D) {
Daniel Dunbarf981bf82009-11-21 09:14:52 +0000370 // In C, functions with no attributes never need to be mangled. Fastpath them.
David Blaikie4e4d0842012-03-11 07:00:24 +0000371 if (!getASTContext().getLangOpts().CPlusPlus && !D->hasAttrs())
Daniel Dunbarf981bf82009-11-21 09:14:52 +0000372 return false;
373
374 // Any decl can be declared with __asm("foo") on it, and this takes precedence
375 // over all other naming in the .o file.
376 if (D->hasAttr<AsmLabelAttr>())
377 return true;
378
Mike Stump141c5af2009-09-02 00:25:38 +0000379 // Clang's "overloadable" attribute extension to C/C++ implies name mangling
Anders Carlssona1e16222009-11-07 07:15:03 +0000380 // (always) as does passing a C++ member function and a function
381 // whose name is not a simple identifier.
Daniel Dunbarf981bf82009-11-21 09:14:52 +0000382 const FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
383 if (FD && (FD->hasAttr<OverloadableAttr>() || isa<CXXMethodDecl>(FD) ||
384 !FD->getDeclName().isIdentifier()))
385 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000386
Daniel Dunbarf981bf82009-11-21 09:14:52 +0000387 // Otherwise, no mangling is done outside C++ mode.
David Blaikie4e4d0842012-03-11 07:00:24 +0000388 if (!getASTContext().getLangOpts().CPlusPlus)
Daniel Dunbarf981bf82009-11-21 09:14:52 +0000389 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000390
Sean Hunt31455252010-01-24 03:04:27 +0000391 // Variables at global scope with non-internal linkage are not mangled
Eli Friedman7facf842009-12-02 20:32:49 +0000392 if (!FD) {
Douglas Gregorccc1b5e2012-02-21 00:37:24 +0000393 const DeclContext *DC = getEffectiveDeclContext(D);
Eli Friedman7facf842009-12-02 20:32:49 +0000394 // Check for extern variable declared locally.
Fariborz Jahaniane81c5612010-06-30 18:57:21 +0000395 if (DC->isFunctionOrMethod() && D->hasLinkage())
Eli Friedman7facf842009-12-02 20:32:49 +0000396 while (!DC->isNamespace() && !DC->isTranslationUnit())
Douglas Gregorccc1b5e2012-02-21 00:37:24 +0000397 DC = getEffectiveParentContext(DC);
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000398 if (DC->isTranslationUnit() && D->getLinkage() != InternalLinkage)
Eli Friedman7facf842009-12-02 20:32:49 +0000399 return false;
400 }
401
Eli Friedmanc00cb642010-07-18 20:49:59 +0000402 // Class members are always mangled.
Douglas Gregorccc1b5e2012-02-21 00:37:24 +0000403 if (getEffectiveDeclContext(D)->isRecord())
Eli Friedmanc00cb642010-07-18 20:49:59 +0000404 return true;
405
Eli Friedman7facf842009-12-02 20:32:49 +0000406 // C functions and "main" are not mangled.
407 if ((FD && FD->isMain()) || isInCLinkageSpecification(D))
Daniel Dunbarf981bf82009-11-21 09:14:52 +0000408 return false;
409
Anders Carlsson43f17402009-04-02 15:51:53 +0000410 return true;
411}
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000412
Chris Lattner5f9e2722011-07-23 10:55:15 +0000413void CXXNameMangler::mangle(const NamedDecl *D, StringRef Prefix) {
Mike Stump141c5af2009-09-02 00:25:38 +0000414 // Any decl can be declared with __asm("foo") on it, and this takes precedence
415 // over all other naming in the .o file.
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000416 if (const AsmLabelAttr *ALA = D->getAttr<AsmLabelAttr>()) {
Chris Lattnerca3f25c2009-03-21 08:24:40 +0000417 // If we have an asm name, then we use it as the mangling.
Rafael Espindola4e274e92011-02-15 22:23:51 +0000418
419 // Adding the prefix can cause problems when one file has a "foo" and
420 // another has a "\01foo". That is known to happen on ELF with the
421 // tricks normally used for producing aliases (PR9177). Fortunately the
422 // llvm mangler on ELF is a nop, so we can just avoid adding the \01
Peter Collingbourne69317432011-04-06 12:29:09 +0000423 // marker. We also avoid adding the marker if this is an alias for an
424 // LLVM intrinsic.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000425 StringRef UserLabelPrefix =
Douglas Gregorbcfd1f52011-09-02 00:18:52 +0000426 getASTContext().getTargetInfo().getUserLabelPrefix();
Peter Collingbourne69317432011-04-06 12:29:09 +0000427 if (!UserLabelPrefix.empty() && !ALA->getLabel().startswith("llvm."))
Rafael Espindola4e274e92011-02-15 22:23:51 +0000428 Out << '\01'; // LLVM IR Marker for __asm("foo")
429
Chris Lattnerca3f25c2009-03-21 08:24:40 +0000430 Out << ALA->getLabel();
Daniel Dunbarf981bf82009-11-21 09:14:52 +0000431 return;
Chris Lattnerca3f25c2009-03-21 08:24:40 +0000432 }
Mike Stump1eb44332009-09-09 15:08:12 +0000433
Sean Hunt31455252010-01-24 03:04:27 +0000434 // <mangled-name> ::= _Z <encoding>
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000435 // ::= <data name>
436 // ::= <special-name>
Daniel Dunbar7e0c1952009-11-21 09:17:15 +0000437 Out << Prefix;
438 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
Daniel Dunbarf981bf82009-11-21 09:14:52 +0000439 mangleFunctionEncoding(FD);
Rafael Espindolad9800722010-03-11 14:07:00 +0000440 else if (const VarDecl *VD = dyn_cast<VarDecl>(D))
441 mangleName(VD);
Daniel Dunbar7e0c1952009-11-21 09:17:15 +0000442 else
Rafael Espindolad9800722010-03-11 14:07:00 +0000443 mangleName(cast<FieldDecl>(D));
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000444}
445
446void CXXNameMangler::mangleFunctionEncoding(const FunctionDecl *FD) {
447 // <encoding> ::= <function name> <bare-function-type>
448 mangleName(FD);
Mike Stump1eb44332009-09-09 15:08:12 +0000449
Daniel Dunbar7e0c1952009-11-21 09:17:15 +0000450 // Don't mangle in the type if this isn't a decl we should typically mangle.
451 if (!Context.shouldMangleDeclName(FD))
452 return;
453
Mike Stump141c5af2009-09-02 00:25:38 +0000454 // Whether the mangling of a function type includes the return type depends on
455 // the context and the nature of the function. The rules for deciding whether
456 // the return type is included are:
Mike Stump1eb44332009-09-09 15:08:12 +0000457 //
Douglas Gregor1fd2dd12009-06-29 22:39:32 +0000458 // 1. Template functions (names or types) have return types encoded, with
459 // the exceptions listed below.
Mike Stump1eb44332009-09-09 15:08:12 +0000460 // 2. Function types not appearing as part of a function name mangling,
Douglas Gregor1fd2dd12009-06-29 22:39:32 +0000461 // e.g. parameters, pointer types, etc., have return type encoded, with the
462 // exceptions listed below.
463 // 3. Non-template function names do not have return types encoded.
464 //
Mike Stump141c5af2009-09-02 00:25:38 +0000465 // The exceptions mentioned in (1) and (2) above, for which the return type is
466 // never included, are
Douglas Gregor1fd2dd12009-06-29 22:39:32 +0000467 // 1. Constructors.
468 // 2. Destructors.
469 // 3. Conversion operator functions, e.g. operator int.
470 bool MangleReturnType = false;
Anders Carlsson9234b7f2009-09-17 03:46:43 +0000471 if (FunctionTemplateDecl *PrimaryTemplate = FD->getPrimaryTemplate()) {
472 if (!(isa<CXXConstructorDecl>(FD) || isa<CXXDestructorDecl>(FD) ||
473 isa<CXXConversionDecl>(FD)))
474 MangleReturnType = true;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000475
Anders Carlsson9234b7f2009-09-17 03:46:43 +0000476 // Mangle the type of the primary template.
477 FD = PrimaryTemplate->getTemplatedDecl();
478 }
479
Douglas Gregor79e6bd32011-07-12 04:42:08 +0000480 mangleBareFunctionType(FD->getType()->getAs<FunctionType>(),
481 MangleReturnType);
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000482}
483
Anders Carlsson47846d22009-12-04 06:23:23 +0000484static const DeclContext *IgnoreLinkageSpecDecls(const DeclContext *DC) {
485 while (isa<LinkageSpecDecl>(DC)) {
Douglas Gregorccc1b5e2012-02-21 00:37:24 +0000486 DC = getEffectiveParentContext(DC);
Anders Carlsson47846d22009-12-04 06:23:23 +0000487 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000488
Anders Carlsson47846d22009-12-04 06:23:23 +0000489 return DC;
490}
491
Anders Carlssonc820f902010-06-02 15:58:27 +0000492/// isStd - Return whether a given namespace is the 'std' namespace.
493static bool isStd(const NamespaceDecl *NS) {
Douglas Gregorccc1b5e2012-02-21 00:37:24 +0000494 if (!IgnoreLinkageSpecDecls(getEffectiveParentContext(NS))
495 ->isTranslationUnit())
Anders Carlssonc820f902010-06-02 15:58:27 +0000496 return false;
497
498 const IdentifierInfo *II = NS->getOriginalNamespace()->getIdentifier();
499 return II && II->isStr("std");
500}
501
Anders Carlsson47846d22009-12-04 06:23:23 +0000502// isStdNamespace - Return whether a given decl context is a toplevel 'std'
503// namespace.
Daniel Dunbar1308af92009-11-21 09:11:45 +0000504static bool isStdNamespace(const DeclContext *DC) {
Anders Carlsson47846d22009-12-04 06:23:23 +0000505 if (!DC->isNamespace())
506 return false;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000507
Anders Carlsson47846d22009-12-04 06:23:23 +0000508 return isStd(cast<NamespaceDecl>(DC));
Daniel Dunbar1308af92009-11-21 09:11:45 +0000509}
510
Anders Carlssonbb36ba42009-09-26 03:24:57 +0000511static const TemplateDecl *
512isTemplate(const NamedDecl *ND, const TemplateArgumentList *&TemplateArgs) {
Anders Carlsson2744a062009-09-18 19:00:18 +0000513 // Check if we have a function template.
514 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)){
Anders Carlssonbb36ba42009-09-26 03:24:57 +0000515 if (const TemplateDecl *TD = FD->getPrimaryTemplate()) {
Anders Carlsson2744a062009-09-18 19:00:18 +0000516 TemplateArgs = FD->getTemplateSpecializationArgs();
Anders Carlssonbb36ba42009-09-26 03:24:57 +0000517 return TD;
Anders Carlsson2744a062009-09-18 19:00:18 +0000518 }
519 }
520
Anders Carlssoneafc6dc2009-09-18 19:44:50 +0000521 // Check if we have a class template.
522 if (const ClassTemplateSpecializationDecl *Spec =
523 dyn_cast<ClassTemplateSpecializationDecl>(ND)) {
524 TemplateArgs = &Spec->getTemplateArgs();
Anders Carlssonbb36ba42009-09-26 03:24:57 +0000525 return Spec->getSpecializedTemplate();
Anders Carlssoneafc6dc2009-09-18 19:44:50 +0000526 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000527
Anders Carlsson2744a062009-09-18 19:00:18 +0000528 return 0;
529}
530
Douglas Gregorf54486a2012-04-04 17:40:10 +0000531static bool isLambda(const NamedDecl *ND) {
532 const CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(ND);
533 if (!Record)
534 return false;
535
536 return Record->isLambda();
537}
538
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000539void CXXNameMangler::mangleName(const NamedDecl *ND) {
540 // <name> ::= <nested-name>
541 // ::= <unscoped-name>
542 // ::= <unscoped-template-name> <template-args>
Anders Carlsson201ce742009-09-17 03:17:01 +0000543 // ::= <local-name>
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000544 //
Douglas Gregorccc1b5e2012-02-21 00:37:24 +0000545 const DeclContext *DC = getEffectiveDeclContext(ND);
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000546
Eli Friedman7facf842009-12-02 20:32:49 +0000547 // If this is an extern variable declared locally, the relevant DeclContext
548 // is that of the containing namespace, or the translation unit.
Douglas Gregorf54486a2012-04-04 17:40:10 +0000549 // FIXME: This is a hack; extern variables declared locally should have
550 // a proper semantic declaration context!
551 if (isa<FunctionDecl>(DC) && ND->hasLinkage() && !isLambda(ND))
Eli Friedman7facf842009-12-02 20:32:49 +0000552 while (!DC->isNamespace() && !DC->isTranslationUnit())
Douglas Gregorccc1b5e2012-02-21 00:37:24 +0000553 DC = getEffectiveParentContext(DC);
John McCall82b7d7b2010-10-18 21:28:44 +0000554 else if (GetLocalClassDecl(ND)) {
555 mangleLocalName(ND);
556 return;
557 }
Eli Friedman7facf842009-12-02 20:32:49 +0000558
James Molloyb3c312c2012-03-05 09:59:43 +0000559 DC = IgnoreLinkageSpecDecls(DC);
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000560
Anders Carlssond58d6f72009-09-17 16:12:20 +0000561 if (DC->isTranslationUnit() || isStdNamespace(DC)) {
Anders Carlsson2744a062009-09-18 19:00:18 +0000562 // Check if we have a template.
563 const TemplateArgumentList *TemplateArgs = 0;
Anders Carlsson0fa6df42009-09-26 19:45:45 +0000564 if (const TemplateDecl *TD = isTemplate(ND, TemplateArgs)) {
Anders Carlsson2744a062009-09-18 19:00:18 +0000565 mangleUnscopedTemplateName(TD);
Eli Friedmand7a6b162012-09-26 02:36:12 +0000566 mangleTemplateArgs(*TemplateArgs);
Anders Carlsson2744a062009-09-18 19:00:18 +0000567 return;
Anders Carlsson7482e242009-09-18 04:29:09 +0000568 }
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000569
Anders Carlsson7482e242009-09-18 04:29:09 +0000570 mangleUnscopedName(ND);
571 return;
572 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000573
Anders Carlsson7b06f6c2009-12-10 03:14:39 +0000574 if (isa<FunctionDecl>(DC) || isa<ObjCMethodDecl>(DC)) {
Anders Carlsson7482e242009-09-18 04:29:09 +0000575 mangleLocalName(ND);
576 return;
577 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000578
Eli Friedman7facf842009-12-02 20:32:49 +0000579 mangleNestedName(ND, DC);
Anders Carlsson7482e242009-09-18 04:29:09 +0000580}
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000581void CXXNameMangler::mangleName(const TemplateDecl *TD,
Anders Carlsson7624f212009-09-18 02:42:01 +0000582 const TemplateArgument *TemplateArgs,
583 unsigned NumTemplateArgs) {
Douglas Gregorccc1b5e2012-02-21 00:37:24 +0000584 const DeclContext *DC = IgnoreLinkageSpecDecls(getEffectiveDeclContext(TD));
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000585
Anders Carlsson7624f212009-09-18 02:42:01 +0000586 if (DC->isTranslationUnit() || isStdNamespace(DC)) {
Anders Carlsson0fa6df42009-09-26 19:45:45 +0000587 mangleUnscopedTemplateName(TD);
Eli Friedmand7a6b162012-09-26 02:36:12 +0000588 mangleTemplateArgs(TemplateArgs, NumTemplateArgs);
Anders Carlsson7624f212009-09-18 02:42:01 +0000589 } else {
590 mangleNestedName(TD, TemplateArgs, NumTemplateArgs);
591 }
592}
593
Anders Carlsson201ce742009-09-17 03:17:01 +0000594void CXXNameMangler::mangleUnscopedName(const NamedDecl *ND) {
595 // <unscoped-name> ::= <unqualified-name>
596 // ::= St <unqualified-name> # ::std::
James Molloyb3c312c2012-03-05 09:59:43 +0000597
598 if (isStdNamespace(IgnoreLinkageSpecDecls(getEffectiveDeclContext(ND))))
Anders Carlsson201ce742009-09-17 03:17:01 +0000599 Out << "St";
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000600
Anders Carlsson201ce742009-09-17 03:17:01 +0000601 mangleUnqualifiedName(ND);
602}
603
Anders Carlsson0fa6df42009-09-26 19:45:45 +0000604void CXXNameMangler::mangleUnscopedTemplateName(const TemplateDecl *ND) {
Anders Carlsson201ce742009-09-17 03:17:01 +0000605 // <unscoped-template-name> ::= <unscoped-name>
606 // ::= <substitution>
Anders Carlsson7624f212009-09-18 02:42:01 +0000607 if (mangleSubstitution(ND))
Anders Carlsson03c9d532009-09-17 04:02:31 +0000608 return;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000609
Douglas Gregor32fb4e12010-02-05 20:45:00 +0000610 // <template-template-param> ::= <template-param>
611 if (const TemplateTemplateParmDecl *TTP
612 = dyn_cast<TemplateTemplateParmDecl>(ND)) {
613 mangleTemplateParameter(TTP->getIndex());
614 return;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000615 }
Douglas Gregor32fb4e12010-02-05 20:45:00 +0000616
Anders Carlsson1668f202009-09-26 20:13:56 +0000617 mangleUnscopedName(ND->getTemplatedDecl());
Anders Carlsson7624f212009-09-18 02:42:01 +0000618 addSubstitution(ND);
Anders Carlsson201ce742009-09-17 03:17:01 +0000619}
620
Douglas Gregor1e9268e2010-04-28 05:58:56 +0000621void CXXNameMangler::mangleUnscopedTemplateName(TemplateName Template) {
622 // <unscoped-template-name> ::= <unscoped-name>
623 // ::= <substitution>
624 if (TemplateDecl *TD = Template.getAsTemplateDecl())
625 return mangleUnscopedTemplateName(TD);
Sean Huntc3021132010-05-05 15:23:54 +0000626
Douglas Gregor1e9268e2010-04-28 05:58:56 +0000627 if (mangleSubstitution(Template))
628 return;
629
Douglas Gregor1e9268e2010-04-28 05:58:56 +0000630 DependentTemplateName *Dependent = Template.getAsDependentTemplateName();
631 assert(Dependent && "Not a dependent template name?");
Douglas Gregor19617912011-07-12 05:06:05 +0000632 if (const IdentifierInfo *Id = Dependent->getIdentifier())
633 mangleSourceName(Id);
634 else
635 mangleOperatorName(Dependent->getOperator(), UnknownArity);
Sean Huntc3021132010-05-05 15:23:54 +0000636
Douglas Gregor1e9268e2010-04-28 05:58:56 +0000637 addSubstitution(Template);
638}
639
John McCall1b600522011-04-24 03:07:16 +0000640void CXXNameMangler::mangleFloat(const llvm::APFloat &f) {
641 // ABI:
642 // Floating-point literals are encoded using a fixed-length
643 // lowercase hexadecimal string corresponding to the internal
644 // representation (IEEE on Itanium), high-order bytes first,
645 // without leading zeroes. For example: "Lf bf800000 E" is -1.0f
646 // on Itanium.
John McCall0c8731a2012-01-30 18:36:31 +0000647 // The 'without leading zeroes' thing seems to be an editorial
648 // mistake; see the discussion on cxx-abi-dev beginning on
649 // 2012-01-16.
John McCall1b600522011-04-24 03:07:16 +0000650
Benjamin Kramer48d798c2012-06-02 10:20:41 +0000651 // Our requirements here are just barely weird enough to justify
John McCall0c8731a2012-01-30 18:36:31 +0000652 // using a custom algorithm instead of post-processing APInt::toString().
John McCall1b600522011-04-24 03:07:16 +0000653
John McCall0c8731a2012-01-30 18:36:31 +0000654 llvm::APInt valueBits = f.bitcastToAPInt();
655 unsigned numCharacters = (valueBits.getBitWidth() + 3) / 4;
656 assert(numCharacters != 0);
657
658 // Allocate a buffer of the right number of characters.
659 llvm::SmallVector<char, 20> buffer;
660 buffer.set_size(numCharacters);
661
662 // Fill the buffer left-to-right.
663 for (unsigned stringIndex = 0; stringIndex != numCharacters; ++stringIndex) {
664 // The bit-index of the next hex digit.
665 unsigned digitBitIndex = 4 * (numCharacters - stringIndex - 1);
666
667 // Project out 4 bits starting at 'digitIndex'.
668 llvm::integerPart hexDigit
669 = valueBits.getRawData()[digitBitIndex / llvm::integerPartWidth];
670 hexDigit >>= (digitBitIndex % llvm::integerPartWidth);
671 hexDigit &= 0xF;
672
673 // Map that over to a lowercase hex digit.
674 static const char charForHex[16] = {
675 '0', '1', '2', '3', '4', '5', '6', '7',
676 '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'
677 };
678 buffer[stringIndex] = charForHex[hexDigit];
679 }
680
681 Out.write(buffer.data(), numCharacters);
John McCall0512e482010-07-14 04:20:34 +0000682}
683
684void CXXNameMangler::mangleNumber(const llvm::APSInt &Value) {
685 if (Value.isSigned() && Value.isNegative()) {
686 Out << 'n';
John McCall54c86f72012-08-18 04:51:52 +0000687 Value.abs().print(Out, /*signed*/ false);
688 } else {
689 Value.print(Out, /*signed*/ false);
690 }
John McCall0512e482010-07-14 04:20:34 +0000691}
692
Anders Carlssona94822e2009-11-26 02:32:05 +0000693void CXXNameMangler::mangleNumber(int64_t Number) {
694 // <number> ::= [n] <non-negative decimal integer>
695 if (Number < 0) {
696 Out << 'n';
697 Number = -Number;
698 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000699
Anders Carlssona94822e2009-11-26 02:32:05 +0000700 Out << Number;
701}
702
Anders Carlsson19879c92010-03-23 17:17:29 +0000703void CXXNameMangler::mangleCallOffset(int64_t NonVirtual, int64_t Virtual) {
Mike Stump141c5af2009-09-02 00:25:38 +0000704 // <call-offset> ::= h <nv-offset> _
705 // ::= v <v-offset> _
706 // <nv-offset> ::= <offset number> # non-virtual base override
Anders Carlssona94822e2009-11-26 02:32:05 +0000707 // <v-offset> ::= <offset number> _ <virtual offset number>
Mike Stump141c5af2009-09-02 00:25:38 +0000708 // # virtual base override, with vcall offset
Anders Carlsson19879c92010-03-23 17:17:29 +0000709 if (!Virtual) {
Anders Carlssona94822e2009-11-26 02:32:05 +0000710 Out << 'h';
Anders Carlsson19879c92010-03-23 17:17:29 +0000711 mangleNumber(NonVirtual);
Anders Carlssona94822e2009-11-26 02:32:05 +0000712 Out << '_';
713 return;
Mike Stump141c5af2009-09-02 00:25:38 +0000714 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000715
Anders Carlssona94822e2009-11-26 02:32:05 +0000716 Out << 'v';
Anders Carlsson19879c92010-03-23 17:17:29 +0000717 mangleNumber(NonVirtual);
Anders Carlssona94822e2009-11-26 02:32:05 +0000718 Out << '_';
Anders Carlsson19879c92010-03-23 17:17:29 +0000719 mangleNumber(Virtual);
Anders Carlssona94822e2009-11-26 02:32:05 +0000720 Out << '_';
Mike Stump9124bcc2009-09-02 00:56:18 +0000721}
722
John McCall4f4e4132011-05-04 01:45:19 +0000723void CXXNameMangler::manglePrefix(QualType type) {
John McCalla0ce15c2011-04-24 08:23:24 +0000724 if (const TemplateSpecializationType *TST =
725 type->getAs<TemplateSpecializationType>()) {
726 if (!mangleSubstitution(QualType(TST, 0))) {
727 mangleTemplatePrefix(TST->getTemplateName());
Sean Huntc3021132010-05-05 15:23:54 +0000728
Douglas Gregoraa2187d2011-02-28 00:04:36 +0000729 // FIXME: GCC does not appear to mangle the template arguments when
730 // the template in question is a dependent template name. Should we
731 // emulate that badness?
Eli Friedmand7a6b162012-09-26 02:36:12 +0000732 mangleTemplateArgs(TST->getArgs(), TST->getNumArgs());
John McCalla0ce15c2011-04-24 08:23:24 +0000733 addSubstitution(QualType(TST, 0));
Rafael Espindola9b35b252010-03-17 04:28:11 +0000734 }
John McCalla0ce15c2011-04-24 08:23:24 +0000735 } else if (const DependentTemplateSpecializationType *DTST
736 = type->getAs<DependentTemplateSpecializationType>()) {
737 TemplateName Template
738 = getASTContext().getDependentTemplateName(DTST->getQualifier(),
739 DTST->getIdentifier());
740 mangleTemplatePrefix(Template);
741
742 // FIXME: GCC does not appear to mangle the template arguments when
743 // the template in question is a dependent template name. Should we
744 // emulate that badness?
Eli Friedmand7a6b162012-09-26 02:36:12 +0000745 mangleTemplateArgs(DTST->getArgs(), DTST->getNumArgs());
John McCalla0ce15c2011-04-24 08:23:24 +0000746 } else {
747 // We use the QualType mangle type variant here because it handles
748 // substitutions.
749 mangleType(type);
John McCall1dd73832010-02-04 01:42:13 +0000750 }
751}
752
John McCalla0ce15c2011-04-24 08:23:24 +0000753/// Mangle everything prior to the base-unresolved-name in an unresolved-name.
754///
755/// \param firstQualifierLookup - the entity found by unqualified lookup
756/// for the first name in the qualifier, if this is for a member expression
757/// \param recursive - true if this is being called recursively,
758/// i.e. if there is more prefix "to the right".
759void CXXNameMangler::mangleUnresolvedPrefix(NestedNameSpecifier *qualifier,
760 NamedDecl *firstQualifierLookup,
761 bool recursive) {
John McCall1dd73832010-02-04 01:42:13 +0000762
John McCalla0ce15c2011-04-24 08:23:24 +0000763 // x, ::x
764 // <unresolved-name> ::= [gs] <base-unresolved-name>
765
766 // T::x / decltype(p)::x
767 // <unresolved-name> ::= sr <unresolved-type> <base-unresolved-name>
768
769 // T::N::x /decltype(p)::N::x
770 // <unresolved-name> ::= srN <unresolved-type> <unresolved-qualifier-level>+ E
771 // <base-unresolved-name>
772
773 // A::x, N::y, A<T>::z; "gs" means leading "::"
774 // <unresolved-name> ::= [gs] sr <unresolved-qualifier-level>+ E
775 // <base-unresolved-name>
776
777 switch (qualifier->getKind()) {
778 case NestedNameSpecifier::Global:
779 Out << "gs";
780
781 // We want an 'sr' unless this is the entire NNS.
782 if (recursive)
783 Out << "sr";
784
785 // We never want an 'E' here.
786 return;
787
788 case NestedNameSpecifier::Namespace:
789 if (qualifier->getPrefix())
790 mangleUnresolvedPrefix(qualifier->getPrefix(), firstQualifierLookup,
791 /*recursive*/ true);
792 else
793 Out << "sr";
794 mangleSourceName(qualifier->getAsNamespace()->getIdentifier());
795 break;
796 case NestedNameSpecifier::NamespaceAlias:
797 if (qualifier->getPrefix())
798 mangleUnresolvedPrefix(qualifier->getPrefix(), firstQualifierLookup,
799 /*recursive*/ true);
800 else
801 Out << "sr";
802 mangleSourceName(qualifier->getAsNamespaceAlias()->getIdentifier());
803 break;
804
805 case NestedNameSpecifier::TypeSpec:
806 case NestedNameSpecifier::TypeSpecWithTemplate: {
John McCall4f4e4132011-05-04 01:45:19 +0000807 const Type *type = qualifier->getAsType();
John McCalla0ce15c2011-04-24 08:23:24 +0000808
John McCall4f4e4132011-05-04 01:45:19 +0000809 // We only want to use an unresolved-type encoding if this is one of:
810 // - a decltype
811 // - a template type parameter
812 // - a template template parameter with arguments
813 // In all of these cases, we should have no prefix.
814 if (qualifier->getPrefix()) {
815 mangleUnresolvedPrefix(qualifier->getPrefix(), firstQualifierLookup,
816 /*recursive*/ true);
817 } else {
818 // Otherwise, all the cases want this.
819 Out << "sr";
John McCall4f4e4132011-05-04 01:45:19 +0000820 }
821
John McCall4f4e4132011-05-04 01:45:19 +0000822 // Only certain other types are valid as prefixes; enumerate them.
John McCalld3d49bb2011-06-28 16:49:23 +0000823 switch (type->getTypeClass()) {
824 case Type::Builtin:
825 case Type::Complex:
826 case Type::Pointer:
827 case Type::BlockPointer:
828 case Type::LValueReference:
829 case Type::RValueReference:
830 case Type::MemberPointer:
831 case Type::ConstantArray:
832 case Type::IncompleteArray:
833 case Type::VariableArray:
834 case Type::DependentSizedArray:
835 case Type::DependentSizedExtVector:
836 case Type::Vector:
837 case Type::ExtVector:
838 case Type::FunctionProto:
839 case Type::FunctionNoProto:
840 case Type::Enum:
841 case Type::Paren:
842 case Type::Elaborated:
843 case Type::Attributed:
844 case Type::Auto:
845 case Type::PackExpansion:
John McCalld3d49bb2011-06-28 16:49:23 +0000846 case Type::ObjCObject:
847 case Type::ObjCInterface:
848 case Type::ObjCObjectPointer:
Eli Friedmanb001de72011-10-06 23:00:33 +0000849 case Type::Atomic:
John McCalld3d49bb2011-06-28 16:49:23 +0000850 llvm_unreachable("type is illegal as a nested name specifier");
851
John McCall68a51a72011-07-01 00:04:39 +0000852 case Type::SubstTemplateTypeParmPack:
853 // FIXME: not clear how to mangle this!
854 // template <class T...> class A {
855 // template <class U...> void foo(decltype(T::foo(U())) x...);
856 // };
857 Out << "_SUBSTPACK_";
858 break;
859
John McCalld3d49bb2011-06-28 16:49:23 +0000860 // <unresolved-type> ::= <template-param>
861 // ::= <decltype>
862 // ::= <template-template-param> <template-args>
863 // (this last is not official yet)
864 case Type::TypeOfExpr:
865 case Type::TypeOf:
866 case Type::Decltype:
867 case Type::TemplateTypeParm:
868 case Type::UnaryTransform:
John McCall35ee32e2011-07-01 02:19:08 +0000869 case Type::SubstTemplateTypeParm:
John McCalld3d49bb2011-06-28 16:49:23 +0000870 unresolvedType:
871 assert(!qualifier->getPrefix());
872
873 // We only get here recursively if we're followed by identifiers.
874 if (recursive) Out << 'N';
875
John McCall35ee32e2011-07-01 02:19:08 +0000876 // This seems to do everything we want. It's not really
877 // sanctioned for a substituted template parameter, though.
John McCalld3d49bb2011-06-28 16:49:23 +0000878 mangleType(QualType(type, 0));
879
880 // We never want to print 'E' directly after an unresolved-type,
881 // so we return directly.
882 return;
883
John McCalld3d49bb2011-06-28 16:49:23 +0000884 case Type::Typedef:
885 mangleSourceName(cast<TypedefType>(type)->getDecl()->getIdentifier());
886 break;
887
888 case Type::UnresolvedUsing:
889 mangleSourceName(cast<UnresolvedUsingType>(type)->getDecl()
890 ->getIdentifier());
891 break;
892
893 case Type::Record:
894 mangleSourceName(cast<RecordType>(type)->getDecl()->getIdentifier());
895 break;
896
897 case Type::TemplateSpecialization: {
898 const TemplateSpecializationType *tst
899 = cast<TemplateSpecializationType>(type);
John McCall68a51a72011-07-01 00:04:39 +0000900 TemplateName name = tst->getTemplateName();
901 switch (name.getKind()) {
902 case TemplateName::Template:
903 case TemplateName::QualifiedTemplate: {
904 TemplateDecl *temp = name.getAsTemplateDecl();
John McCalld3d49bb2011-06-28 16:49:23 +0000905
John McCall68a51a72011-07-01 00:04:39 +0000906 // If the base is a template template parameter, this is an
907 // unresolved type.
908 assert(temp && "no template for template specialization type");
909 if (isa<TemplateTemplateParmDecl>(temp)) goto unresolvedType;
John McCalld3d49bb2011-06-28 16:49:23 +0000910
John McCall68a51a72011-07-01 00:04:39 +0000911 mangleSourceName(temp->getIdentifier());
912 break;
913 }
914
915 case TemplateName::OverloadedTemplate:
916 case TemplateName::DependentTemplate:
917 llvm_unreachable("invalid base for a template specialization type");
918
919 case TemplateName::SubstTemplateTemplateParm: {
920 SubstTemplateTemplateParmStorage *subst
921 = name.getAsSubstTemplateTemplateParm();
922 mangleExistingSubstitution(subst->getReplacement());
923 break;
924 }
925
926 case TemplateName::SubstTemplateTemplateParmPack: {
927 // FIXME: not clear how to mangle this!
928 // template <template <class U> class T...> class A {
929 // template <class U...> void foo(decltype(T<U>::foo) x...);
930 // };
931 Out << "_SUBSTPACK_";
932 break;
933 }
934 }
935
Eli Friedmand7a6b162012-09-26 02:36:12 +0000936 mangleTemplateArgs(tst->getArgs(), tst->getNumArgs());
John McCalld3d49bb2011-06-28 16:49:23 +0000937 break;
938 }
939
940 case Type::InjectedClassName:
941 mangleSourceName(cast<InjectedClassNameType>(type)->getDecl()
942 ->getIdentifier());
943 break;
944
945 case Type::DependentName:
946 mangleSourceName(cast<DependentNameType>(type)->getIdentifier());
947 break;
948
949 case Type::DependentTemplateSpecialization: {
950 const DependentTemplateSpecializationType *tst
951 = cast<DependentTemplateSpecializationType>(type);
John McCall4f4e4132011-05-04 01:45:19 +0000952 mangleSourceName(tst->getIdentifier());
Eli Friedmand7a6b162012-09-26 02:36:12 +0000953 mangleTemplateArgs(tst->getArgs(), tst->getNumArgs());
John McCalld3d49bb2011-06-28 16:49:23 +0000954 break;
955 }
John McCall4f4e4132011-05-04 01:45:19 +0000956 }
957 break;
John McCalla0ce15c2011-04-24 08:23:24 +0000958 }
959
960 case NestedNameSpecifier::Identifier:
961 // Member expressions can have these without prefixes.
962 if (qualifier->getPrefix()) {
963 mangleUnresolvedPrefix(qualifier->getPrefix(), firstQualifierLookup,
964 /*recursive*/ true);
965 } else if (firstQualifierLookup) {
966
967 // Try to make a proper qualifier out of the lookup result, and
968 // then just recurse on that.
969 NestedNameSpecifier *newQualifier;
970 if (TypeDecl *typeDecl = dyn_cast<TypeDecl>(firstQualifierLookup)) {
971 QualType type = getASTContext().getTypeDeclType(typeDecl);
972
973 // Pretend we had a different nested name specifier.
974 newQualifier = NestedNameSpecifier::Create(getASTContext(),
975 /*prefix*/ 0,
976 /*template*/ false,
977 type.getTypePtr());
978 } else if (NamespaceDecl *nspace =
979 dyn_cast<NamespaceDecl>(firstQualifierLookup)) {
980 newQualifier = NestedNameSpecifier::Create(getASTContext(),
981 /*prefix*/ 0,
982 nspace);
983 } else if (NamespaceAliasDecl *alias =
984 dyn_cast<NamespaceAliasDecl>(firstQualifierLookup)) {
985 newQualifier = NestedNameSpecifier::Create(getASTContext(),
986 /*prefix*/ 0,
987 alias);
988 } else {
989 // No sensible mangling to do here.
990 newQualifier = 0;
991 }
992
993 if (newQualifier)
994 return mangleUnresolvedPrefix(newQualifier, /*lookup*/ 0, recursive);
995
996 } else {
997 Out << "sr";
998 }
999
1000 mangleSourceName(qualifier->getAsIdentifier());
1001 break;
1002 }
1003
1004 // If this was the innermost part of the NNS, and we fell out to
1005 // here, append an 'E'.
1006 if (!recursive)
1007 Out << 'E';
1008}
1009
1010/// Mangle an unresolved-name, which is generally used for names which
1011/// weren't resolved to specific entities.
1012void CXXNameMangler::mangleUnresolvedName(NestedNameSpecifier *qualifier,
1013 NamedDecl *firstQualifierLookup,
1014 DeclarationName name,
1015 unsigned knownArity) {
1016 if (qualifier) mangleUnresolvedPrefix(qualifier, firstQualifierLookup);
1017 mangleUnqualifiedName(0, name, knownArity);
John McCall1dd73832010-02-04 01:42:13 +00001018}
1019
Anders Carlsson6f7e2f42010-06-08 14:49:03 +00001020static const FieldDecl *FindFirstNamedDataMember(const RecordDecl *RD) {
1021 assert(RD->isAnonymousStructOrUnion() &&
1022 "Expected anonymous struct or union!");
1023
1024 for (RecordDecl::field_iterator I = RD->field_begin(), E = RD->field_end();
1025 I != E; ++I) {
David Blaikie581deb32012-06-06 20:45:41 +00001026 if (I->getIdentifier())
1027 return *I;
Anders Carlsson6f7e2f42010-06-08 14:49:03 +00001028
David Blaikie581deb32012-06-06 20:45:41 +00001029 if (const RecordType *RT = I->getType()->getAs<RecordType>())
Anders Carlsson6f7e2f42010-06-08 14:49:03 +00001030 if (const FieldDecl *NamedDataMember =
1031 FindFirstNamedDataMember(RT->getDecl()))
1032 return NamedDataMember;
1033 }
Anders Carlsson6f7e2f42010-06-08 14:49:03 +00001034
1035 // We didn't find a named data member.
1036 return 0;
1037}
1038
John McCall1dd73832010-02-04 01:42:13 +00001039void CXXNameMangler::mangleUnqualifiedName(const NamedDecl *ND,
1040 DeclarationName Name,
1041 unsigned KnownArity) {
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001042 // <unqualified-name> ::= <operator-name>
Mike Stump1eb44332009-09-09 15:08:12 +00001043 // ::= <ctor-dtor-name>
1044 // ::= <source-name>
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001045 switch (Name.getNameKind()) {
Anders Carlssonc4355b62009-10-07 01:45:02 +00001046 case DeclarationName::Identifier: {
Anders Carlssonc4355b62009-10-07 01:45:02 +00001047 if (const IdentifierInfo *II = Name.getAsIdentifierInfo()) {
Sean Hunt31455252010-01-24 03:04:27 +00001048 // We must avoid conflicts between internally- and externally-
John McCall74990f42011-03-22 06:34:45 +00001049 // linked variable and function declaration names in the same TU:
1050 // void test() { extern void foo(); }
1051 // static void foo();
1052 // This naming convention is the same as that followed by GCC,
1053 // though it shouldn't actually matter.
1054 if (ND && ND->getLinkage() == InternalLinkage &&
Douglas Gregorccc1b5e2012-02-21 00:37:24 +00001055 getEffectiveDeclContext(ND)->isFileContext())
Sean Hunt31455252010-01-24 03:04:27 +00001056 Out << 'L';
1057
Anders Carlssonc4355b62009-10-07 01:45:02 +00001058 mangleSourceName(II);
1059 break;
1060 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00001061
John McCall1dd73832010-02-04 01:42:13 +00001062 // Otherwise, an anonymous entity. We must have a declaration.
1063 assert(ND && "mangling empty name without declaration");
1064
1065 if (const NamespaceDecl *NS = dyn_cast<NamespaceDecl>(ND)) {
1066 if (NS->isAnonymousNamespace()) {
1067 // This is how gcc mangles these names.
1068 Out << "12_GLOBAL__N_1";
1069 break;
1070 }
1071 }
1072
Anders Carlsson6f7e2f42010-06-08 14:49:03 +00001073 if (const VarDecl *VD = dyn_cast<VarDecl>(ND)) {
1074 // We must have an anonymous union or struct declaration.
1075 const RecordDecl *RD =
1076 cast<RecordDecl>(VD->getType()->getAs<RecordType>()->getDecl());
1077
1078 // Itanium C++ ABI 5.1.2:
1079 //
1080 // For the purposes of mangling, the name of an anonymous union is
1081 // considered to be the name of the first named data member found by a
1082 // pre-order, depth-first, declaration-order walk of the data members of
1083 // the anonymous union. If there is no such data member (i.e., if all of
1084 // the data members in the union are unnamed), then there is no way for
1085 // a program to refer to the anonymous union, and there is therefore no
1086 // need to mangle its name.
1087 const FieldDecl *FD = FindFirstNamedDataMember(RD);
John McCall7121c8f2010-08-05 22:02:13 +00001088
1089 // It's actually possible for various reasons for us to get here
1090 // with an empty anonymous struct / union. Fortunately, it
1091 // doesn't really matter what name we generate.
1092 if (!FD) break;
Anders Carlsson6f7e2f42010-06-08 14:49:03 +00001093 assert(FD->getIdentifier() && "Data member name isn't an identifier!");
1094
1095 mangleSourceName(FD->getIdentifier());
1096 break;
1097 }
1098
Anders Carlssonc4355b62009-10-07 01:45:02 +00001099 // We must have an anonymous struct.
1100 const TagDecl *TD = cast<TagDecl>(ND);
Richard Smith162e1c12011-04-15 14:24:37 +00001101 if (const TypedefNameDecl *D = TD->getTypedefNameForAnonDecl()) {
Anders Carlssonc4355b62009-10-07 01:45:02 +00001102 assert(TD->getDeclContext() == D->getDeclContext() &&
1103 "Typedef should not be in another decl context!");
1104 assert(D->getDeclName().getAsIdentifierInfo() &&
1105 "Typedef was not named!");
1106 mangleSourceName(D->getDeclName().getAsIdentifierInfo());
1107 break;
1108 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00001109
Douglas Gregor9e8c92a2012-02-20 19:44:39 +00001110 // <unnamed-type-name> ::= <closure-type-name>
1111 //
1112 // <closure-type-name> ::= Ul <lambda-sig> E [ <nonnegative number> ] _
1113 // <lambda-sig> ::= <parameter-type>+ # Parameter types or 'v' for 'void'.
1114 if (const CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(TD)) {
Douglas Gregor5878cbc2012-02-21 04:17:39 +00001115 if (Record->isLambda() && Record->getLambdaManglingNumber()) {
Douglas Gregorccc1b5e2012-02-21 00:37:24 +00001116 mangleLambda(Record);
Douglas Gregor9e8c92a2012-02-20 19:44:39 +00001117 break;
1118 }
1119 }
David Blaikie66cff722012-11-14 01:52:05 +00001120
1121 int UnnamedMangle = Context.getASTContext().getUnnamedTagManglingNumber(TD);
1122 if (UnnamedMangle != -1) {
1123 Out << "Ut";
1124 if (UnnamedMangle != 0)
1125 Out << llvm::utostr(UnnamedMangle - 1);
1126 Out << '_';
1127 break;
1128 }
1129
1130 //assert(cast<RecordDecl>(RD)->isAnonymousStructOrUnion() && "Don't mangle unnamed things as "
1131 // "anonymous things");
Douglas Gregor9e8c92a2012-02-20 19:44:39 +00001132
Anders Carlssonc4355b62009-10-07 01:45:02 +00001133 // Get a unique id for the anonymous struct.
1134 uint64_t AnonStructId = Context.getAnonymousStructId(TD);
1135
1136 // Mangle it as a source name in the form
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00001137 // [n] $_<id>
Anders Carlssonc4355b62009-10-07 01:45:02 +00001138 // where n is the length of the string.
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00001139 SmallString<8> Str;
Anders Carlssonc4355b62009-10-07 01:45:02 +00001140 Str += "$_";
1141 Str += llvm::utostr(AnonStructId);
1142
1143 Out << Str.size();
1144 Out << Str.str();
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001145 break;
Anders Carlssonc4355b62009-10-07 01:45:02 +00001146 }
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001147
1148 case DeclarationName::ObjCZeroArgSelector:
1149 case DeclarationName::ObjCOneArgSelector:
1150 case DeclarationName::ObjCMultiArgSelector:
David Blaikieb219cfc2011-09-23 05:06:16 +00001151 llvm_unreachable("Can't mangle Objective-C selector names here!");
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001152
1153 case DeclarationName::CXXConstructorName:
Anders Carlsson27ae5362009-04-17 01:58:57 +00001154 if (ND == Structor)
Mike Stump141c5af2009-09-02 00:25:38 +00001155 // If the named decl is the C++ constructor we're mangling, use the type
1156 // we were given.
Anders Carlsson27ae5362009-04-17 01:58:57 +00001157 mangleCXXCtorType(static_cast<CXXCtorType>(StructorType));
Anders Carlsson3ac86b52009-04-15 05:36:58 +00001158 else
1159 // Otherwise, use the complete constructor name. This is relevant if a
1160 // class with a constructor is declared within a constructor.
1161 mangleCXXCtorType(Ctor_Complete);
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001162 break;
1163
1164 case DeclarationName::CXXDestructorName:
Anders Carlsson27ae5362009-04-17 01:58:57 +00001165 if (ND == Structor)
Mike Stump141c5af2009-09-02 00:25:38 +00001166 // If the named decl is the C++ destructor we're mangling, use the type we
1167 // were given.
Anders Carlsson27ae5362009-04-17 01:58:57 +00001168 mangleCXXDtorType(static_cast<CXXDtorType>(StructorType));
1169 else
1170 // Otherwise, use the complete destructor name. This is relevant if a
1171 // class with a destructor is declared within a destructor.
1172 mangleCXXDtorType(Dtor_Complete);
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001173 break;
1174
1175 case DeclarationName::CXXConversionFunctionName:
Mike Stump1eb44332009-09-09 15:08:12 +00001176 // <operator-name> ::= cv <type> # (cast)
Douglas Gregor219cc612009-02-13 01:28:03 +00001177 Out << "cv";
Douglas Gregor79e6bd32011-07-12 04:42:08 +00001178 mangleType(Name.getCXXNameType());
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001179 break;
1180
Anders Carlsson8257d412009-12-22 06:36:32 +00001181 case DeclarationName::CXXOperatorName: {
John McCall1dd73832010-02-04 01:42:13 +00001182 unsigned Arity;
1183 if (ND) {
1184 Arity = cast<FunctionDecl>(ND)->getNumParams();
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001185
John McCall1dd73832010-02-04 01:42:13 +00001186 // If we have a C++ member function, we need to include the 'this' pointer.
1187 // FIXME: This does not make sense for operators that are static, but their
1188 // names stay the same regardless of the arity (operator new for instance).
1189 if (isa<CXXMethodDecl>(ND))
1190 Arity++;
1191 } else
1192 Arity = KnownArity;
1193
Anders Carlsson8257d412009-12-22 06:36:32 +00001194 mangleOperatorName(Name.getCXXOverloadedOperator(), Arity);
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001195 break;
Anders Carlsson8257d412009-12-22 06:36:32 +00001196 }
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001197
Sean Hunt3e518bd2009-11-29 07:34:05 +00001198 case DeclarationName::CXXLiteralOperatorName:
Sean Hunt5dd6b392009-12-04 21:11:13 +00001199 // FIXME: This mangling is not yet official.
Sean Hunt2421f662009-12-04 21:01:37 +00001200 Out << "li";
Sean Hunt3e518bd2009-11-29 07:34:05 +00001201 mangleSourceName(Name.getCXXLiteralIdentifier());
1202 break;
1203
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001204 case DeclarationName::CXXUsingDirective:
David Blaikieb219cfc2011-09-23 05:06:16 +00001205 llvm_unreachable("Can't mangle a using directive name!");
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001206 }
1207}
1208
1209void CXXNameMangler::mangleSourceName(const IdentifierInfo *II) {
1210 // <source-name> ::= <positive length number> <identifier>
1211 // <number> ::= [n] <non-negative decimal integer>
1212 // <identifier> ::= <unqualified source code identifier>
1213 Out << II->getLength() << II->getName();
1214}
1215
Eli Friedman7facf842009-12-02 20:32:49 +00001216void CXXNameMangler::mangleNestedName(const NamedDecl *ND,
Fariborz Jahanian57058532010-03-03 19:41:08 +00001217 const DeclContext *DC,
1218 bool NoFunction) {
Douglas Gregor0a9a6d62011-01-26 17:36:28 +00001219 // <nested-name>
1220 // ::= N [<CV-qualifiers>] [<ref-qualifier>] <prefix> <unqualified-name> E
1221 // ::= N [<CV-qualifiers>] [<ref-qualifier>] <template-prefix>
1222 // <template-args> E
Anders Carlssond99edc42009-09-26 03:55:37 +00001223
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001224 Out << 'N';
Douglas Gregor0a9a6d62011-01-26 17:36:28 +00001225 if (const CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(ND)) {
John McCall0953e762009-09-24 19:53:00 +00001226 mangleQualifiers(Qualifiers::fromCVRMask(Method->getTypeQualifiers()));
Douglas Gregor0a9a6d62011-01-26 17:36:28 +00001227 mangleRefQualifier(Method->getRefQualifier());
1228 }
1229
Anders Carlsson2744a062009-09-18 19:00:18 +00001230 // Check if we have a template.
1231 const TemplateArgumentList *TemplateArgs = 0;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00001232 if (const TemplateDecl *TD = isTemplate(ND, TemplateArgs)) {
Anders Carlsson2744a062009-09-18 19:00:18 +00001233 mangleTemplatePrefix(TD);
Eli Friedmand7a6b162012-09-26 02:36:12 +00001234 mangleTemplateArgs(*TemplateArgs);
Fariborz Jahanian57058532010-03-03 19:41:08 +00001235 }
1236 else {
1237 manglePrefix(DC, NoFunction);
Anders Carlsson7482e242009-09-18 04:29:09 +00001238 mangleUnqualifiedName(ND);
1239 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00001240
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001241 Out << 'E';
1242}
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00001243void CXXNameMangler::mangleNestedName(const TemplateDecl *TD,
Anders Carlsson7624f212009-09-18 02:42:01 +00001244 const TemplateArgument *TemplateArgs,
1245 unsigned NumTemplateArgs) {
Anders Carlssone45117b2009-09-27 19:53:49 +00001246 // <nested-name> ::= N [<CV-qualifiers>] <template-prefix> <template-args> E
1247
Anders Carlsson7624f212009-09-18 02:42:01 +00001248 Out << 'N';
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00001249
Anders Carlssone45117b2009-09-27 19:53:49 +00001250 mangleTemplatePrefix(TD);
Eli Friedmand7a6b162012-09-26 02:36:12 +00001251 mangleTemplateArgs(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";
Eli Friedman8da8a662012-09-19 01:18:11 +00001345 const FunctionProtoType *Proto = Lambda->getLambdaTypeInfo()->getType()->
1346 getAs<FunctionProtoType>();
Douglas Gregorccc1b5e2012-02-21 00:37:24 +00001347 mangleBareFunctionType(Proto, /*MangleReturnType=*/false);
1348 Out << "E";
1349
1350 // The number is omitted for the first closure type with a given
1351 // <lambda-sig> in a given context; it is n-2 for the nth closure type
1352 // (in lexical order) with that same <lambda-sig> and context.
1353 //
1354 // The AST keeps track of the number for us.
Douglas Gregor5878cbc2012-02-21 04:17:39 +00001355 unsigned Number = Lambda->getLambdaManglingNumber();
1356 assert(Number > 0 && "Lambda should be mangled as an unnamed class");
1357 if (Number > 1)
1358 mangleNumber(Number - 2);
Douglas Gregorccc1b5e2012-02-21 00:37:24 +00001359 Out << '_';
1360}
1361
John McCalla0ce15c2011-04-24 08:23:24 +00001362void CXXNameMangler::manglePrefix(NestedNameSpecifier *qualifier) {
1363 switch (qualifier->getKind()) {
1364 case NestedNameSpecifier::Global:
1365 // nothing
1366 return;
1367
1368 case NestedNameSpecifier::Namespace:
1369 mangleName(qualifier->getAsNamespace());
1370 return;
1371
1372 case NestedNameSpecifier::NamespaceAlias:
1373 mangleName(qualifier->getAsNamespaceAlias()->getNamespace());
1374 return;
1375
1376 case NestedNameSpecifier::TypeSpec:
1377 case NestedNameSpecifier::TypeSpecWithTemplate:
John McCall4f4e4132011-05-04 01:45:19 +00001378 manglePrefix(QualType(qualifier->getAsType(), 0));
John McCalla0ce15c2011-04-24 08:23:24 +00001379 return;
1380
1381 case NestedNameSpecifier::Identifier:
1382 // Member expressions can have these without prefixes, but that
1383 // should end up in mangleUnresolvedPrefix instead.
1384 assert(qualifier->getPrefix());
1385 manglePrefix(qualifier->getPrefix());
1386
1387 mangleSourceName(qualifier->getAsIdentifier());
1388 return;
1389 }
1390
1391 llvm_unreachable("unexpected nested name specifier");
1392}
1393
Fariborz Jahanian57058532010-03-03 19:41:08 +00001394void CXXNameMangler::manglePrefix(const DeclContext *DC, bool NoFunction) {
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001395 // <prefix> ::= <prefix> <unqualified-name>
1396 // ::= <template-prefix> <template-args>
1397 // ::= <template-param>
1398 // ::= # empty
1399 // ::= <substitution>
Anders Carlsson6862fc72009-09-17 04:16:28 +00001400
James Molloyb3c312c2012-03-05 09:59:43 +00001401 DC = IgnoreLinkageSpecDecls(DC);
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00001402
Anders Carlsson9263e912009-09-18 18:39:58 +00001403 if (DC->isTranslationUnit())
1404 return;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00001405
Douglas Gregor35415f52010-05-25 17:04:15 +00001406 if (const BlockDecl *Block = dyn_cast<BlockDecl>(DC)) {
Douglas Gregorccc1b5e2012-02-21 00:37:24 +00001407 manglePrefix(getEffectiveParentContext(DC), NoFunction);
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00001408 SmallString<64> Name;
Rafael Espindolac4850c22011-02-10 23:59:36 +00001409 llvm::raw_svector_ostream NameStream(Name);
1410 Context.mangleBlock(Block, NameStream);
1411 NameStream.flush();
Douglas Gregor35415f52010-05-25 17:04:15 +00001412 Out << Name.size() << Name;
1413 return;
1414 }
1415
Douglas Gregor552e2992012-02-21 02:22:07 +00001416 const NamedDecl *ND = cast<NamedDecl>(DC);
1417 if (mangleSubstitution(ND))
Anders Carlsson6862fc72009-09-17 04:16:28 +00001418 return;
Douglas Gregor552e2992012-02-21 02:22:07 +00001419
Anders Carlsson2ee3fca2009-09-18 20:11:09 +00001420 // Check if we have a template.
1421 const TemplateArgumentList *TemplateArgs = 0;
Douglas Gregor552e2992012-02-21 02:22:07 +00001422 if (const TemplateDecl *TD = isTemplate(ND, TemplateArgs)) {
Anders Carlsson2ee3fca2009-09-18 20:11:09 +00001423 mangleTemplatePrefix(TD);
Eli Friedmand7a6b162012-09-26 02:36:12 +00001424 mangleTemplateArgs(*TemplateArgs);
Fariborz Jahanian57058532010-03-03 19:41:08 +00001425 }
Douglas Gregor552e2992012-02-21 02:22:07 +00001426 else if(NoFunction && (isa<FunctionDecl>(ND) || isa<ObjCMethodDecl>(ND)))
Fariborz Jahanian57058532010-03-03 19:41:08 +00001427 return;
Douglas Gregor552e2992012-02-21 02:22:07 +00001428 else if (const ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(ND))
Douglas Gregor35415f52010-05-25 17:04:15 +00001429 mangleObjCMethodName(Method);
Fariborz Jahanian57058532010-03-03 19:41:08 +00001430 else {
Douglas Gregor552e2992012-02-21 02:22:07 +00001431 manglePrefix(getEffectiveDeclContext(ND), NoFunction);
1432 mangleUnqualifiedName(ND);
Anders Carlsson2ee3fca2009-09-18 20:11:09 +00001433 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00001434
Douglas Gregor552e2992012-02-21 02:22:07 +00001435 addSubstitution(ND);
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001436}
1437
Douglas Gregor20f0cc72010-04-23 03:10:43 +00001438void CXXNameMangler::mangleTemplatePrefix(TemplateName Template) {
1439 // <template-prefix> ::= <prefix> <template unqualified-name>
1440 // ::= <template-param>
1441 // ::= <substitution>
1442 if (TemplateDecl *TD = Template.getAsTemplateDecl())
1443 return mangleTemplatePrefix(TD);
1444
1445 if (QualifiedTemplateName *Qualified = Template.getAsQualifiedTemplateName())
John McCalla0ce15c2011-04-24 08:23:24 +00001446 manglePrefix(Qualified->getQualifier());
Sean Huntc3021132010-05-05 15:23:54 +00001447
Douglas Gregor20f0cc72010-04-23 03:10:43 +00001448 if (OverloadedTemplateStorage *Overloaded
1449 = Template.getAsOverloadedTemplate()) {
Sean Huntc3021132010-05-05 15:23:54 +00001450 mangleUnqualifiedName(0, (*Overloaded->begin())->getDeclName(),
Douglas Gregor20f0cc72010-04-23 03:10:43 +00001451 UnknownArity);
1452 return;
1453 }
Sean Huntc3021132010-05-05 15:23:54 +00001454
Douglas Gregor20f0cc72010-04-23 03:10:43 +00001455 DependentTemplateName *Dependent = Template.getAsDependentTemplateName();
1456 assert(Dependent && "Unknown template name kind?");
John McCalla0ce15c2011-04-24 08:23:24 +00001457 manglePrefix(Dependent->getQualifier());
Douglas Gregor1e9268e2010-04-28 05:58:56 +00001458 mangleUnscopedTemplateName(Template);
Douglas Gregor20f0cc72010-04-23 03:10:43 +00001459}
1460
Anders Carlsson0fa6df42009-09-26 19:45:45 +00001461void CXXNameMangler::mangleTemplatePrefix(const TemplateDecl *ND) {
Anders Carlsson7482e242009-09-18 04:29:09 +00001462 // <template-prefix> ::= <prefix> <template unqualified-name>
1463 // ::= <template-param>
1464 // ::= <substitution>
Douglas Gregor32fb4e12010-02-05 20:45:00 +00001465 // <template-template-param> ::= <template-param>
1466 // <substitution>
Anders Carlsson7482e242009-09-18 04:29:09 +00001467
Anders Carlssonaeb85372009-09-26 22:18:22 +00001468 if (mangleSubstitution(ND))
1469 return;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00001470
Douglas Gregor32fb4e12010-02-05 20:45:00 +00001471 // <template-template-param> ::= <template-param>
1472 if (const TemplateTemplateParmDecl *TTP
1473 = dyn_cast<TemplateTemplateParmDecl>(ND)) {
1474 mangleTemplateParameter(TTP->getIndex());
1475 return;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001476 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00001477
Douglas Gregorccc1b5e2012-02-21 00:37:24 +00001478 manglePrefix(getEffectiveDeclContext(ND));
Anders Carlsson1668f202009-09-26 20:13:56 +00001479 mangleUnqualifiedName(ND->getTemplatedDecl());
Anders Carlssonaeb85372009-09-26 22:18:22 +00001480 addSubstitution(ND);
Anders Carlsson7482e242009-09-18 04:29:09 +00001481}
1482
John McCallb6f532e2010-07-14 06:43:17 +00001483/// Mangles a template name under the production <type>. Required for
1484/// template template arguments.
1485/// <type> ::= <class-enum-type>
1486/// ::= <template-param>
1487/// ::= <substitution>
1488void CXXNameMangler::mangleType(TemplateName TN) {
1489 if (mangleSubstitution(TN))
1490 return;
1491
1492 TemplateDecl *TD = 0;
1493
1494 switch (TN.getKind()) {
1495 case TemplateName::QualifiedTemplate:
1496 TD = TN.getAsQualifiedTemplateName()->getTemplateDecl();
1497 goto HaveDecl;
1498
1499 case TemplateName::Template:
1500 TD = TN.getAsTemplateDecl();
1501 goto HaveDecl;
1502
1503 HaveDecl:
1504 if (isa<TemplateTemplateParmDecl>(TD))
1505 mangleTemplateParameter(cast<TemplateTemplateParmDecl>(TD)->getIndex());
1506 else
1507 mangleName(TD);
1508 break;
1509
1510 case TemplateName::OverloadedTemplate:
1511 llvm_unreachable("can't mangle an overloaded template name as a <type>");
John McCallb6f532e2010-07-14 06:43:17 +00001512
1513 case TemplateName::DependentTemplate: {
1514 const DependentTemplateName *Dependent = TN.getAsDependentTemplateName();
1515 assert(Dependent->isIdentifier());
1516
1517 // <class-enum-type> ::= <name>
1518 // <name> ::= <nested-name>
John McCalla0ce15c2011-04-24 08:23:24 +00001519 mangleUnresolvedPrefix(Dependent->getQualifier(), 0);
John McCallb6f532e2010-07-14 06:43:17 +00001520 mangleSourceName(Dependent->getIdentifier());
1521 break;
1522 }
1523
John McCallb44e0cf2011-06-30 21:59:02 +00001524 case TemplateName::SubstTemplateTemplateParm: {
1525 // Substituted template parameters are mangled as the substituted
1526 // template. This will check for the substitution twice, which is
1527 // fine, but we have to return early so that we don't try to *add*
1528 // the substitution twice.
1529 SubstTemplateTemplateParmStorage *subst
1530 = TN.getAsSubstTemplateTemplateParm();
1531 mangleType(subst->getReplacement());
1532 return;
1533 }
John McCall14606042011-06-30 08:33:18 +00001534
Douglas Gregor1aee05d2011-01-15 06:45:20 +00001535 case TemplateName::SubstTemplateTemplateParmPack: {
John McCall68a51a72011-07-01 00:04:39 +00001536 // FIXME: not clear how to mangle this!
1537 // template <template <class> class T...> class A {
1538 // template <template <class> class U...> void foo(B<T,U> x...);
1539 // };
1540 Out << "_SUBSTPACK_";
Douglas Gregor1aee05d2011-01-15 06:45:20 +00001541 break;
1542 }
John McCallb6f532e2010-07-14 06:43:17 +00001543 }
1544
1545 addSubstitution(TN);
1546}
1547
Mike Stump1eb44332009-09-09 15:08:12 +00001548void
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001549CXXNameMangler::mangleOperatorName(OverloadedOperatorKind OO, unsigned Arity) {
1550 switch (OO) {
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001551 // <operator-name> ::= nw # new
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001552 case OO_New: Out << "nw"; break;
1553 // ::= na # new[]
1554 case OO_Array_New: Out << "na"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001555 // ::= dl # delete
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001556 case OO_Delete: Out << "dl"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001557 // ::= da # delete[]
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001558 case OO_Array_Delete: Out << "da"; break;
1559 // ::= ps # + (unary)
John McCall5e1e89b2010-08-18 19:18:59 +00001560 // ::= pl # + (binary or unknown)
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001561 case OO_Plus:
Anders Carlsson8257d412009-12-22 06:36:32 +00001562 Out << (Arity == 1? "ps" : "pl"); break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001563 // ::= ng # - (unary)
John McCall5e1e89b2010-08-18 19:18:59 +00001564 // ::= mi # - (binary or unknown)
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001565 case OO_Minus:
Anders Carlsson8257d412009-12-22 06:36:32 +00001566 Out << (Arity == 1? "ng" : "mi"); break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001567 // ::= ad # & (unary)
John McCall5e1e89b2010-08-18 19:18:59 +00001568 // ::= an # & (binary or unknown)
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001569 case OO_Amp:
Anders Carlsson8257d412009-12-22 06:36:32 +00001570 Out << (Arity == 1? "ad" : "an"); break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001571 // ::= de # * (unary)
John McCall5e1e89b2010-08-18 19:18:59 +00001572 // ::= ml # * (binary or unknown)
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001573 case OO_Star:
John McCall5e1e89b2010-08-18 19:18:59 +00001574 // Use binary when unknown.
Anders Carlsson8257d412009-12-22 06:36:32 +00001575 Out << (Arity == 1? "de" : "ml"); break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001576 // ::= co # ~
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001577 case OO_Tilde: Out << "co"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001578 // ::= dv # /
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001579 case OO_Slash: Out << "dv"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001580 // ::= rm # %
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001581 case OO_Percent: Out << "rm"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001582 // ::= or # |
1583 case OO_Pipe: Out << "or"; break;
1584 // ::= eo # ^
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001585 case OO_Caret: Out << "eo"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001586 // ::= aS # =
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001587 case OO_Equal: Out << "aS"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001588 // ::= pL # +=
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001589 case OO_PlusEqual: Out << "pL"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001590 // ::= mI # -=
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001591 case OO_MinusEqual: Out << "mI"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001592 // ::= mL # *=
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001593 case OO_StarEqual: Out << "mL"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001594 // ::= dV # /=
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001595 case OO_SlashEqual: Out << "dV"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001596 // ::= rM # %=
1597 case OO_PercentEqual: Out << "rM"; break;
1598 // ::= aN # &=
1599 case OO_AmpEqual: Out << "aN"; break;
1600 // ::= oR # |=
1601 case OO_PipeEqual: Out << "oR"; break;
1602 // ::= eO # ^=
1603 case OO_CaretEqual: Out << "eO"; break;
1604 // ::= ls # <<
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001605 case OO_LessLess: Out << "ls"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001606 // ::= rs # >>
1607 case OO_GreaterGreater: Out << "rs"; break;
1608 // ::= lS # <<=
1609 case OO_LessLessEqual: Out << "lS"; break;
1610 // ::= rS # >>=
1611 case OO_GreaterGreaterEqual: Out << "rS"; break;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001612 // ::= eq # ==
1613 case OO_EqualEqual: Out << "eq"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001614 // ::= ne # !=
1615 case OO_ExclaimEqual: Out << "ne"; break;
1616 // ::= lt # <
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001617 case OO_Less: Out << "lt"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001618 // ::= gt # >
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001619 case OO_Greater: Out << "gt"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001620 // ::= le # <=
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001621 case OO_LessEqual: Out << "le"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001622 // ::= ge # >=
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001623 case OO_GreaterEqual: Out << "ge"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001624 // ::= nt # !
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001625 case OO_Exclaim: Out << "nt"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001626 // ::= aa # &&
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001627 case OO_AmpAmp: Out << "aa"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001628 // ::= oo # ||
1629 case OO_PipePipe: Out << "oo"; break;
1630 // ::= pp # ++
1631 case OO_PlusPlus: Out << "pp"; break;
1632 // ::= mm # --
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001633 case OO_MinusMinus: Out << "mm"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001634 // ::= cm # ,
1635 case OO_Comma: Out << "cm"; break;
1636 // ::= pm # ->*
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001637 case OO_ArrowStar: Out << "pm"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001638 // ::= pt # ->
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001639 case OO_Arrow: Out << "pt"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001640 // ::= cl # ()
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001641 case OO_Call: Out << "cl"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001642 // ::= ix # []
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001643 case OO_Subscript: Out << "ix"; break;
Anders Carlssone170ba72009-12-14 01:45:37 +00001644
1645 // ::= qu # ?
1646 // The conditional operator can't be overloaded, but we still handle it when
1647 // mangling expressions.
1648 case OO_Conditional: Out << "qu"; break;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001649
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001650 case OO_None:
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001651 case NUM_OVERLOADED_OPERATORS:
David Blaikieb219cfc2011-09-23 05:06:16 +00001652 llvm_unreachable("Not an overloaded operator");
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001653 }
1654}
1655
John McCall0953e762009-09-24 19:53:00 +00001656void CXXNameMangler::mangleQualifiers(Qualifiers Quals) {
Mike Stump1eb44332009-09-09 15:08:12 +00001657 // <CV-qualifiers> ::= [r] [V] [K] # restrict (C99), volatile, const
John McCall0953e762009-09-24 19:53:00 +00001658 if (Quals.hasRestrict())
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001659 Out << 'r';
John McCall0953e762009-09-24 19:53:00 +00001660 if (Quals.hasVolatile())
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001661 Out << 'V';
John McCall0953e762009-09-24 19:53:00 +00001662 if (Quals.hasConst())
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001663 Out << 'K';
John McCall0953e762009-09-24 19:53:00 +00001664
Douglas Gregor56079f72010-06-14 23:15:08 +00001665 if (Quals.hasAddressSpace()) {
1666 // Extension:
1667 //
1668 // <type> ::= U <address-space-number>
1669 //
1670 // where <address-space-number> is a source name consisting of 'AS'
1671 // followed by the address space <number>.
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00001672 SmallString<64> ASString;
Douglas Gregor56079f72010-06-14 23:15:08 +00001673 ASString = "AS" + llvm::utostr_32(Quals.getAddressSpace());
1674 Out << 'U' << ASString.size() << ASString;
1675 }
1676
Chris Lattner5f9e2722011-07-23 10:55:15 +00001677 StringRef LifetimeName;
John McCallf85e1932011-06-15 23:02:42 +00001678 switch (Quals.getObjCLifetime()) {
1679 // Objective-C ARC Extension:
1680 //
1681 // <type> ::= U "__strong"
1682 // <type> ::= U "__weak"
1683 // <type> ::= U "__autoreleasing"
John McCallf85e1932011-06-15 23:02:42 +00001684 case Qualifiers::OCL_None:
1685 break;
1686
1687 case Qualifiers::OCL_Weak:
1688 LifetimeName = "__weak";
1689 break;
1690
1691 case Qualifiers::OCL_Strong:
1692 LifetimeName = "__strong";
1693 break;
1694
1695 case Qualifiers::OCL_Autoreleasing:
1696 LifetimeName = "__autoreleasing";
1697 break;
1698
1699 case Qualifiers::OCL_ExplicitNone:
Douglas Gregorc22d6992011-06-17 22:26:49 +00001700 // The __unsafe_unretained qualifier is *not* mangled, so that
1701 // __unsafe_unretained types in ARC produce the same manglings as the
1702 // equivalent (but, naturally, unqualified) types in non-ARC, providing
1703 // better ABI compatibility.
1704 //
1705 // It's safe to do this because unqualified 'id' won't show up
1706 // in any type signatures that need to be mangled.
John McCallf85e1932011-06-15 23:02:42 +00001707 break;
1708 }
1709 if (!LifetimeName.empty())
1710 Out << 'U' << LifetimeName.size() << LifetimeName;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001711}
1712
Douglas Gregor0a9a6d62011-01-26 17:36:28 +00001713void CXXNameMangler::mangleRefQualifier(RefQualifierKind RefQualifier) {
1714 // <ref-qualifier> ::= R # lvalue reference
1715 // ::= O # rvalue-reference
1716 // Proposal to Itanium C++ ABI list on 1/26/11
1717 switch (RefQualifier) {
1718 case RQ_None:
1719 break;
1720
1721 case RQ_LValue:
1722 Out << 'R';
1723 break;
1724
1725 case RQ_RValue:
1726 Out << 'O';
1727 break;
1728 }
1729}
1730
Anders Carlsson7b06f6c2009-12-10 03:14:39 +00001731void CXXNameMangler::mangleObjCMethodName(const ObjCMethodDecl *MD) {
Rafael Espindolaf0be9792011-02-11 02:52:17 +00001732 Context.mangleObjCMethodName(MD, Out);
Anders Carlsson7b06f6c2009-12-10 03:14:39 +00001733}
1734
Douglas Gregorf1588662011-07-12 15:18:55 +00001735void CXXNameMangler::mangleType(QualType T) {
1736 // If our type is instantiation-dependent but not dependent, we mangle
1737 // it as it was written in the source, removing any top-level sugar.
1738 // Otherwise, use the canonical type.
1739 //
1740 // FIXME: This is an approximation of the instantiation-dependent name
1741 // mangling rules, since we should really be using the type as written and
1742 // augmented via semantic analysis (i.e., with implicit conversions and
1743 // default template arguments) for any instantiation-dependent type.
1744 // Unfortunately, that requires several changes to our AST:
1745 // - Instantiation-dependent TemplateSpecializationTypes will need to be
1746 // uniqued, so that we can handle substitutions properly
1747 // - Default template arguments will need to be represented in the
1748 // TemplateSpecializationType, since they need to be mangled even though
1749 // they aren't written.
1750 // - Conversions on non-type template arguments need to be expressed, since
1751 // they can affect the mangling of sizeof/alignof.
1752 if (!T->isInstantiationDependentType() || T->isDependentType())
1753 T = T.getCanonicalType();
1754 else {
1755 // Desugar any types that are purely sugar.
1756 do {
1757 // Don't desugar through template specialization types that aren't
1758 // type aliases. We need to mangle the template arguments as written.
1759 if (const TemplateSpecializationType *TST
1760 = dyn_cast<TemplateSpecializationType>(T))
1761 if (!TST->isTypeAlias())
1762 break;
Anders Carlsson4843e582009-03-10 17:07:44 +00001763
Douglas Gregorf1588662011-07-12 15:18:55 +00001764 QualType Desugared
1765 = T.getSingleStepDesugaredType(Context.getASTContext());
1766 if (Desugared == T)
1767 break;
1768
1769 T = Desugared;
1770 } while (true);
1771 }
1772 SplitQualType split = T.split();
John McCall200fa532012-02-08 00:46:36 +00001773 Qualifiers quals = split.Quals;
1774 const Type *ty = split.Ty;
John McCallb47f7482011-01-26 20:05:40 +00001775
Douglas Gregorf1588662011-07-12 15:18:55 +00001776 bool isSubstitutable = quals || !isa<BuiltinType>(T);
1777 if (isSubstitutable && mangleSubstitution(T))
Anders Carlsson76967372009-09-17 00:43:46 +00001778 return;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001779
John McCallb47f7482011-01-26 20:05:40 +00001780 // If we're mangling a qualified array type, push the qualifiers to
1781 // the element type.
Douglas Gregorf1588662011-07-12 15:18:55 +00001782 if (quals && isa<ArrayType>(T)) {
1783 ty = Context.getASTContext().getAsArrayType(T);
John McCallb47f7482011-01-26 20:05:40 +00001784 quals = Qualifiers();
1785
Douglas Gregorf1588662011-07-12 15:18:55 +00001786 // Note that we don't update T: we want to add the
1787 // substitution at the original type.
John McCallb47f7482011-01-26 20:05:40 +00001788 }
1789
1790 if (quals) {
1791 mangleQualifiers(quals);
John McCall0953e762009-09-24 19:53:00 +00001792 // Recurse: even if the qualified type isn't yet substitutable,
1793 // the unqualified type might be.
John McCallb47f7482011-01-26 20:05:40 +00001794 mangleType(QualType(ty, 0));
Anders Carlsson76967372009-09-17 00:43:46 +00001795 } else {
John McCallb47f7482011-01-26 20:05:40 +00001796 switch (ty->getTypeClass()) {
John McCallefe6aee2009-09-05 07:56:18 +00001797#define ABSTRACT_TYPE(CLASS, PARENT)
1798#define NON_CANONICAL_TYPE(CLASS, PARENT) \
Anders Carlsson76967372009-09-17 00:43:46 +00001799 case Type::CLASS: \
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +00001800 llvm_unreachable("can't mangle non-canonical type " #CLASS "Type"); \
Anders Carlsson76967372009-09-17 00:43:46 +00001801 return;
John McCallefe6aee2009-09-05 07:56:18 +00001802#define TYPE(CLASS, PARENT) \
Anders Carlsson76967372009-09-17 00:43:46 +00001803 case Type::CLASS: \
John McCallb47f7482011-01-26 20:05:40 +00001804 mangleType(static_cast<const CLASS##Type*>(ty)); \
Anders Carlsson76967372009-09-17 00:43:46 +00001805 break;
John McCallefe6aee2009-09-05 07:56:18 +00001806#include "clang/AST/TypeNodes.def"
Anders Carlsson76967372009-09-17 00:43:46 +00001807 }
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001808 }
Anders Carlsson76967372009-09-17 00:43:46 +00001809
1810 // Add the substitution.
John McCallb47f7482011-01-26 20:05:40 +00001811 if (isSubstitutable)
Douglas Gregorf1588662011-07-12 15:18:55 +00001812 addSubstitution(T);
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001813}
1814
Douglas Gregor1b12a3b2010-05-26 05:11:13 +00001815void CXXNameMangler::mangleNameOrStandardSubstitution(const NamedDecl *ND) {
1816 if (!mangleStandardSubstitution(ND))
1817 mangleName(ND);
1818}
1819
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001820void CXXNameMangler::mangleType(const BuiltinType *T) {
John McCallefe6aee2009-09-05 07:56:18 +00001821 // <type> ::= <builtin-type>
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001822 // <builtin-type> ::= v # void
1823 // ::= w # wchar_t
1824 // ::= b # bool
1825 // ::= c # char
1826 // ::= a # signed char
1827 // ::= h # unsigned char
1828 // ::= s # short
1829 // ::= t # unsigned short
1830 // ::= i # int
1831 // ::= j # unsigned int
1832 // ::= l # long
1833 // ::= m # unsigned long
1834 // ::= x # long long, __int64
1835 // ::= y # unsigned long long, __int64
1836 // ::= n # __int128
1837 // UNSUPPORTED: ::= o # unsigned __int128
1838 // ::= f # float
1839 // ::= d # double
1840 // ::= e # long double, __float80
1841 // UNSUPPORTED: ::= g # __float128
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001842 // UNSUPPORTED: ::= Dd # IEEE 754r decimal floating point (64 bits)
1843 // UNSUPPORTED: ::= De # IEEE 754r decimal floating point (128 bits)
1844 // UNSUPPORTED: ::= Df # IEEE 754r decimal floating point (32 bits)
Anton Korobeynikovaa4a99b2011-10-14 23:23:15 +00001845 // ::= Dh # IEEE 754r half-precision floating point (16 bits)
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001846 // ::= Di # char32_t
1847 // ::= Ds # char16_t
Anders Carlssone2923682010-11-04 04:31:32 +00001848 // ::= Dn # std::nullptr_t (i.e., decltype(nullptr))
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001849 // ::= u <source-name> # vendor extended type
1850 switch (T->getKind()) {
1851 case BuiltinType::Void: Out << 'v'; break;
1852 case BuiltinType::Bool: Out << 'b'; break;
1853 case BuiltinType::Char_U: case BuiltinType::Char_S: Out << 'c'; break;
1854 case BuiltinType::UChar: Out << 'h'; break;
1855 case BuiltinType::UShort: Out << 't'; break;
1856 case BuiltinType::UInt: Out << 'j'; break;
1857 case BuiltinType::ULong: Out << 'm'; break;
1858 case BuiltinType::ULongLong: Out << 'y'; break;
Chris Lattner2df9ced2009-04-30 02:43:43 +00001859 case BuiltinType::UInt128: Out << 'o'; break;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001860 case BuiltinType::SChar: Out << 'a'; break;
Chris Lattner3f59c972010-12-25 23:25:43 +00001861 case BuiltinType::WChar_S:
1862 case BuiltinType::WChar_U: Out << 'w'; break;
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001863 case BuiltinType::Char16: Out << "Ds"; break;
1864 case BuiltinType::Char32: Out << "Di"; break;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001865 case BuiltinType::Short: Out << 's'; break;
1866 case BuiltinType::Int: Out << 'i'; break;
1867 case BuiltinType::Long: Out << 'l'; break;
1868 case BuiltinType::LongLong: Out << 'x'; break;
Chris Lattner2df9ced2009-04-30 02:43:43 +00001869 case BuiltinType::Int128: Out << 'n'; break;
Anton Korobeynikovaa4a99b2011-10-14 23:23:15 +00001870 case BuiltinType::Half: Out << "Dh"; break;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001871 case BuiltinType::Float: Out << 'f'; break;
1872 case BuiltinType::Double: Out << 'd'; break;
1873 case BuiltinType::LongDouble: Out << 'e'; break;
Anders Carlssone2923682010-11-04 04:31:32 +00001874 case BuiltinType::NullPtr: Out << "Dn"; break;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001875
John McCalle0a22d02011-10-18 21:02:43 +00001876#define BUILTIN_TYPE(Id, SingletonId)
1877#define PLACEHOLDER_TYPE(Id, SingletonId) \
1878 case BuiltinType::Id:
1879#include "clang/AST/BuiltinTypes.def"
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001880 case BuiltinType::Dependent:
John McCallfb44de92011-05-01 22:35:37 +00001881 llvm_unreachable("mangling a placeholder type");
Steve Naroff9533a7f2009-07-22 17:14:51 +00001882 case BuiltinType::ObjCId: Out << "11objc_object"; break;
1883 case BuiltinType::ObjCClass: Out << "10objc_class"; break;
Fariborz Jahanian13dcd002009-11-21 19:53:08 +00001884 case BuiltinType::ObjCSel: Out << "13objc_selector"; break;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001885 }
1886}
1887
John McCallefe6aee2009-09-05 07:56:18 +00001888// <type> ::= <function-type>
John McCall4b502632012-05-15 02:01:59 +00001889// <function-type> ::= [<CV-qualifiers>] F [Y]
1890// <bare-function-type> [<ref-qualifier>] E
1891// (Proposal to cxx-abi-dev, 2012-05-11)
John McCallefe6aee2009-09-05 07:56:18 +00001892void CXXNameMangler::mangleType(const FunctionProtoType *T) {
John McCall4b502632012-05-15 02:01:59 +00001893 // Mangle CV-qualifiers, if present. These are 'this' qualifiers,
1894 // e.g. "const" in "int (A::*)() const".
1895 mangleQualifiers(Qualifiers::fromCVRMask(T->getTypeQuals()));
1896
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001897 Out << 'F';
John McCall4b502632012-05-15 02:01:59 +00001898
Mike Stumpf5408fe2009-05-16 07:57:57 +00001899 // FIXME: We don't have enough information in the AST to produce the 'Y'
1900 // encoding for extern "C" function types.
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001901 mangleBareFunctionType(T, /*MangleReturnType=*/true);
John McCall4b502632012-05-15 02:01:59 +00001902
1903 // Mangle the ref-qualifier, if present.
1904 mangleRefQualifier(T->getRefQualifier());
1905
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001906 Out << 'E';
1907}
John McCallefe6aee2009-09-05 07:56:18 +00001908void CXXNameMangler::mangleType(const FunctionNoProtoType *T) {
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +00001909 llvm_unreachable("Can't mangle K&R function prototypes");
John McCallefe6aee2009-09-05 07:56:18 +00001910}
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001911void CXXNameMangler::mangleBareFunctionType(const FunctionType *T,
1912 bool MangleReturnType) {
John McCallefe6aee2009-09-05 07:56:18 +00001913 // We should never be mangling something without a prototype.
1914 const FunctionProtoType *Proto = cast<FunctionProtoType>(T);
1915
John McCallfb44de92011-05-01 22:35:37 +00001916 // Record that we're in a function type. See mangleFunctionParam
1917 // for details on what we're trying to achieve here.
1918 FunctionTypeDepthState saved = FunctionTypeDepth.push();
1919
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001920 // <bare-function-type> ::= <signature type>+
John McCallfb44de92011-05-01 22:35:37 +00001921 if (MangleReturnType) {
1922 FunctionTypeDepth.enterResultType();
John McCallefe6aee2009-09-05 07:56:18 +00001923 mangleType(Proto->getResultType());
John McCallfb44de92011-05-01 22:35:37 +00001924 FunctionTypeDepth.leaveResultType();
1925 }
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001926
Anders Carlsson93296682010-06-02 04:40:13 +00001927 if (Proto->getNumArgs() == 0 && !Proto->isVariadic()) {
Eli Friedmana7e68452010-08-22 01:00:03 +00001928 // <builtin-type> ::= v # void
Anders Carlssonc6c91bc2009-04-01 00:15:23 +00001929 Out << 'v';
John McCallfb44de92011-05-01 22:35:37 +00001930
1931 FunctionTypeDepth.pop(saved);
Anders Carlssonc6c91bc2009-04-01 00:15:23 +00001932 return;
1933 }
Mike Stump1eb44332009-09-09 15:08:12 +00001934
Douglas Gregor72564e72009-02-26 23:50:07 +00001935 for (FunctionProtoType::arg_type_iterator Arg = Proto->arg_type_begin(),
Mike Stump1eb44332009-09-09 15:08:12 +00001936 ArgEnd = Proto->arg_type_end();
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001937 Arg != ArgEnd; ++Arg)
Douglas Gregor79e6bd32011-07-12 04:42:08 +00001938 mangleType(Context.getASTContext().getSignatureParameterType(*Arg));
Douglas Gregor219cc612009-02-13 01:28:03 +00001939
John McCallfb44de92011-05-01 22:35:37 +00001940 FunctionTypeDepth.pop(saved);
1941
Douglas Gregor219cc612009-02-13 01:28:03 +00001942 // <builtin-type> ::= z # ellipsis
1943 if (Proto->isVariadic())
1944 Out << 'z';
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001945}
1946
John McCallefe6aee2009-09-05 07:56:18 +00001947// <type> ::= <class-enum-type>
Mike Stump1eb44332009-09-09 15:08:12 +00001948// <class-enum-type> ::= <name>
John McCalled976492009-12-04 22:46:56 +00001949void CXXNameMangler::mangleType(const UnresolvedUsingType *T) {
1950 mangleName(T->getDecl());
1951}
1952
1953// <type> ::= <class-enum-type>
1954// <class-enum-type> ::= <name>
John McCallefe6aee2009-09-05 07:56:18 +00001955void CXXNameMangler::mangleType(const EnumType *T) {
1956 mangleType(static_cast<const TagType*>(T));
1957}
1958void CXXNameMangler::mangleType(const RecordType *T) {
1959 mangleType(static_cast<const TagType*>(T));
1960}
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001961void CXXNameMangler::mangleType(const TagType *T) {
Eli Friedmanecb7e932009-12-11 18:00:57 +00001962 mangleName(T->getDecl());
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001963}
1964
John McCallefe6aee2009-09-05 07:56:18 +00001965// <type> ::= <array-type>
1966// <array-type> ::= A <positive dimension number> _ <element type>
1967// ::= A [<dimension expression>] _ <element type>
1968void CXXNameMangler::mangleType(const ConstantArrayType *T) {
1969 Out << 'A' << T->getSize() << '_';
1970 mangleType(T->getElementType());
1971}
1972void CXXNameMangler::mangleType(const VariableArrayType *T) {
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001973 Out << 'A';
Fariborz Jahanian7281d1f2010-11-02 16:54:00 +00001974 // decayed vla types (size 0) will just be skipped.
1975 if (T->getSizeExpr())
1976 mangleExpression(T->getSizeExpr());
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001977 Out << '_';
1978 mangleType(T->getElementType());
1979}
John McCallefe6aee2009-09-05 07:56:18 +00001980void CXXNameMangler::mangleType(const DependentSizedArrayType *T) {
1981 Out << 'A';
1982 mangleExpression(T->getSizeExpr());
1983 Out << '_';
1984 mangleType(T->getElementType());
1985}
1986void CXXNameMangler::mangleType(const IncompleteArrayType *T) {
Nick Lewycky271b6652010-09-05 03:40:33 +00001987 Out << "A_";
John McCallefe6aee2009-09-05 07:56:18 +00001988 mangleType(T->getElementType());
1989}
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001990
John McCallefe6aee2009-09-05 07:56:18 +00001991// <type> ::= <pointer-to-member-type>
1992// <pointer-to-member-type> ::= M <class type> <member type>
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001993void CXXNameMangler::mangleType(const MemberPointerType *T) {
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001994 Out << 'M';
1995 mangleType(QualType(T->getClass(), 0));
Anders Carlsson0e650012009-05-17 17:41:20 +00001996 QualType PointeeType = T->getPointeeType();
1997 if (const FunctionProtoType *FPT = dyn_cast<FunctionProtoType>(PointeeType)) {
Anders Carlsson0e650012009-05-17 17:41:20 +00001998 mangleType(FPT);
Anders Carlsson9d85b722010-06-02 04:29:50 +00001999
2000 // Itanium C++ ABI 5.1.8:
2001 //
2002 // The type of a non-static member function is considered to be different,
2003 // for the purposes of substitution, from the type of a namespace-scope or
2004 // static member function whose type appears similar. The types of two
2005 // non-static member functions are considered to be different, for the
2006 // purposes of substitution, if the functions are members of different
2007 // classes. In other words, for the purposes of substitution, the class of
2008 // which the function is a member is considered part of the type of
2009 // function.
2010
John McCall4b502632012-05-15 02:01:59 +00002011 // Given that we already substitute member function pointers as a
2012 // whole, the net effect of this rule is just to unconditionally
2013 // suppress substitution on the function type in a member pointer.
Anders Carlsson9d85b722010-06-02 04:29:50 +00002014 // We increment the SeqID here to emulate adding an entry to the
John McCall4b502632012-05-15 02:01:59 +00002015 // substitution table.
Anders Carlsson9d85b722010-06-02 04:29:50 +00002016 ++SeqID;
Mike Stump1eb44332009-09-09 15:08:12 +00002017 } else
Anders Carlsson0e650012009-05-17 17:41:20 +00002018 mangleType(PointeeType);
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00002019}
2020
John McCallefe6aee2009-09-05 07:56:18 +00002021// <type> ::= <template-param>
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00002022void CXXNameMangler::mangleType(const TemplateTypeParmType *T) {
Anders Carlsson0ccdf8d2009-09-27 00:38:53 +00002023 mangleTemplateParameter(T->getIndex());
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00002024}
2025
Douglas Gregorc3069d62011-01-14 02:55:32 +00002026// <type> ::= <template-param>
2027void CXXNameMangler::mangleType(const SubstTemplateTypeParmPackType *T) {
John McCall68a51a72011-07-01 00:04:39 +00002028 // FIXME: not clear how to mangle this!
2029 // template <class T...> class A {
2030 // template <class U...> void foo(T(*)(U) x...);
2031 // };
2032 Out << "_SUBSTPACK_";
Douglas Gregorc3069d62011-01-14 02:55:32 +00002033}
2034
John McCallefe6aee2009-09-05 07:56:18 +00002035// <type> ::= P <type> # pointer-to
2036void CXXNameMangler::mangleType(const PointerType *T) {
2037 Out << 'P';
2038 mangleType(T->getPointeeType());
2039}
2040void CXXNameMangler::mangleType(const ObjCObjectPointerType *T) {
2041 Out << 'P';
2042 mangleType(T->getPointeeType());
2043}
2044
2045// <type> ::= R <type> # reference-to
2046void CXXNameMangler::mangleType(const LValueReferenceType *T) {
2047 Out << 'R';
2048 mangleType(T->getPointeeType());
2049}
2050
2051// <type> ::= O <type> # rvalue reference-to (C++0x)
2052void CXXNameMangler::mangleType(const RValueReferenceType *T) {
2053 Out << 'O';
2054 mangleType(T->getPointeeType());
2055}
2056
2057// <type> ::= C <type> # complex pair (C 2000)
2058void CXXNameMangler::mangleType(const ComplexType *T) {
2059 Out << 'C';
2060 mangleType(T->getElementType());
2061}
2062
Bob Wilsonc7df92d2010-11-12 17:24:43 +00002063// ARM's ABI for Neon vector types specifies that they should be mangled as
Bob Wilson57147a82010-11-16 00:32:18 +00002064// if they are structs (to match ARM's initial implementation). The
2065// vector type must be one of the special types predefined by ARM.
2066void CXXNameMangler::mangleNeonVectorType(const VectorType *T) {
Bob Wilsonc7df92d2010-11-12 17:24:43 +00002067 QualType EltType = T->getElementType();
Bob Wilson57147a82010-11-16 00:32:18 +00002068 assert(EltType->isBuiltinType() && "Neon vector element not a BuiltinType");
Bob Wilsonc7df92d2010-11-12 17:24:43 +00002069 const char *EltName = 0;
Bob Wilson491328c2010-11-12 17:24:46 +00002070 if (T->getVectorKind() == VectorType::NeonPolyVector) {
2071 switch (cast<BuiltinType>(EltType)->getKind()) {
Bob Wilson4cfaa5d2010-11-12 17:24:49 +00002072 case BuiltinType::SChar: EltName = "poly8_t"; break;
2073 case BuiltinType::Short: EltName = "poly16_t"; break;
Bob Wilson57147a82010-11-16 00:32:18 +00002074 default: llvm_unreachable("unexpected Neon polynomial vector element type");
Bob Wilson491328c2010-11-12 17:24:46 +00002075 }
2076 } else {
2077 switch (cast<BuiltinType>(EltType)->getKind()) {
Bob Wilson4cfaa5d2010-11-12 17:24:49 +00002078 case BuiltinType::SChar: EltName = "int8_t"; break;
2079 case BuiltinType::UChar: EltName = "uint8_t"; break;
2080 case BuiltinType::Short: EltName = "int16_t"; break;
2081 case BuiltinType::UShort: EltName = "uint16_t"; break;
2082 case BuiltinType::Int: EltName = "int32_t"; break;
2083 case BuiltinType::UInt: EltName = "uint32_t"; break;
2084 case BuiltinType::LongLong: EltName = "int64_t"; break;
2085 case BuiltinType::ULongLong: EltName = "uint64_t"; break;
2086 case BuiltinType::Float: EltName = "float32_t"; break;
Bob Wilson57147a82010-11-16 00:32:18 +00002087 default: llvm_unreachable("unexpected Neon vector element type");
Bob Wilson491328c2010-11-12 17:24:46 +00002088 }
Bob Wilsonc7df92d2010-11-12 17:24:43 +00002089 }
2090 const char *BaseName = 0;
Bob Wilson4cfaa5d2010-11-12 17:24:49 +00002091 unsigned BitSize = (T->getNumElements() *
Bob Wilson3a723022010-11-16 00:32:12 +00002092 getASTContext().getTypeSize(EltType));
Bob Wilsonc7df92d2010-11-12 17:24:43 +00002093 if (BitSize == 64)
2094 BaseName = "__simd64_";
Bob Wilson57147a82010-11-16 00:32:18 +00002095 else {
2096 assert(BitSize == 128 && "Neon vector type not 64 or 128 bits");
Bob Wilsonc7df92d2010-11-12 17:24:43 +00002097 BaseName = "__simd128_";
Bob Wilson57147a82010-11-16 00:32:18 +00002098 }
Bob Wilsonc7df92d2010-11-12 17:24:43 +00002099 Out << strlen(BaseName) + strlen(EltName);
2100 Out << BaseName << EltName;
Bob Wilsonc7df92d2010-11-12 17:24:43 +00002101}
2102
John McCallefe6aee2009-09-05 07:56:18 +00002103// GNU extension: vector types
Chris Lattner788b0fd2010-06-23 06:00:24 +00002104// <type> ::= <vector-type>
2105// <vector-type> ::= Dv <positive dimension number> _
2106// <extended element type>
2107// ::= Dv [<dimension expression>] _ <element type>
2108// <extended element type> ::= <element type>
2109// ::= p # AltiVec vector pixel
Nick Lewyckyacf0bb42012-10-04 04:58:17 +00002110// ::= b # Altivec vector bool
John McCallefe6aee2009-09-05 07:56:18 +00002111void CXXNameMangler::mangleType(const VectorType *T) {
Bob Wilson491328c2010-11-12 17:24:46 +00002112 if ((T->getVectorKind() == VectorType::NeonVector ||
Bob Wilson57147a82010-11-16 00:32:18 +00002113 T->getVectorKind() == VectorType::NeonPolyVector)) {
2114 mangleNeonVectorType(T);
Bob Wilsonc7df92d2010-11-12 17:24:43 +00002115 return;
Bob Wilson57147a82010-11-16 00:32:18 +00002116 }
Nick Lewycky0e5f0672010-03-26 07:18:04 +00002117 Out << "Dv" << T->getNumElements() << '_';
Bob Wilsone86d78c2010-11-10 21:56:12 +00002118 if (T->getVectorKind() == VectorType::AltiVecPixel)
Chris Lattner788b0fd2010-06-23 06:00:24 +00002119 Out << 'p';
Bob Wilsone86d78c2010-11-10 21:56:12 +00002120 else if (T->getVectorKind() == VectorType::AltiVecBool)
Chris Lattner788b0fd2010-06-23 06:00:24 +00002121 Out << 'b';
2122 else
2123 mangleType(T->getElementType());
John McCallefe6aee2009-09-05 07:56:18 +00002124}
2125void CXXNameMangler::mangleType(const ExtVectorType *T) {
2126 mangleType(static_cast<const VectorType*>(T));
2127}
2128void CXXNameMangler::mangleType(const DependentSizedExtVectorType *T) {
Nick Lewycky0e5f0672010-03-26 07:18:04 +00002129 Out << "Dv";
2130 mangleExpression(T->getSizeExpr());
2131 Out << '_';
John McCallefe6aee2009-09-05 07:56:18 +00002132 mangleType(T->getElementType());
2133}
2134
Douglas Gregor7536dd52010-12-20 02:24:11 +00002135void CXXNameMangler::mangleType(const PackExpansionType *T) {
Douglas Gregor4fc48662011-01-13 16:39:34 +00002136 // <type> ::= Dp <type> # pack expansion (C++0x)
Douglas Gregor255c2692011-01-13 17:44:36 +00002137 Out << "Dp";
Douglas Gregor7536dd52010-12-20 02:24:11 +00002138 mangleType(T->getPattern());
2139}
2140
Anders Carlssona40c5e42009-03-07 22:03:21 +00002141void CXXNameMangler::mangleType(const ObjCInterfaceType *T) {
2142 mangleSourceName(T->getDecl()->getIdentifier());
2143}
2144
John McCallc12c5bb2010-05-15 11:32:37 +00002145void CXXNameMangler::mangleType(const ObjCObjectType *T) {
John McCallc00c1f62010-05-15 17:06:29 +00002146 // We don't allow overloading by different protocol qualification,
2147 // so mangling them isn't necessary.
John McCallc12c5bb2010-05-15 11:32:37 +00002148 mangleType(T->getBaseType());
2149}
2150
John McCallefe6aee2009-09-05 07:56:18 +00002151void CXXNameMangler::mangleType(const BlockPointerType *T) {
Anders Carlssonf28c6872009-12-23 22:31:44 +00002152 Out << "U13block_pointer";
2153 mangleType(T->getPointeeType());
John McCallefe6aee2009-09-05 07:56:18 +00002154}
2155
John McCall31f17ec2010-04-27 00:57:59 +00002156void CXXNameMangler::mangleType(const InjectedClassNameType *T) {
2157 // Mangle injected class name types as if the user had written the
2158 // specialization out fully. It may not actually be possible to see
2159 // this mangling, though.
2160 mangleType(T->getInjectedSpecializationType());
2161}
2162
John McCallefe6aee2009-09-05 07:56:18 +00002163void CXXNameMangler::mangleType(const TemplateSpecializationType *T) {
Douglas Gregor1e9268e2010-04-28 05:58:56 +00002164 if (TemplateDecl *TD = T->getTemplateName().getAsTemplateDecl()) {
2165 mangleName(TD, T->getArgs(), T->getNumArgs());
2166 } else {
2167 if (mangleSubstitution(QualType(T, 0)))
2168 return;
Sean Huntc3021132010-05-05 15:23:54 +00002169
Douglas Gregor1e9268e2010-04-28 05:58:56 +00002170 mangleTemplatePrefix(T->getTemplateName());
Sean Huntc3021132010-05-05 15:23:54 +00002171
Douglas Gregor1e9268e2010-04-28 05:58:56 +00002172 // FIXME: GCC does not appear to mangle the template arguments when
2173 // the template in question is a dependent template name. Should we
2174 // emulate that badness?
Eli Friedmand7a6b162012-09-26 02:36:12 +00002175 mangleTemplateArgs(T->getArgs(), T->getNumArgs());
Douglas Gregor1e9268e2010-04-28 05:58:56 +00002176 addSubstitution(QualType(T, 0));
2177 }
John McCallefe6aee2009-09-05 07:56:18 +00002178}
2179
Douglas Gregor4714c122010-03-31 17:34:00 +00002180void CXXNameMangler::mangleType(const DependentNameType *T) {
Anders Carlssonae352482009-09-26 02:26:02 +00002181 // Typename types are always nested
2182 Out << 'N';
John McCalla0ce15c2011-04-24 08:23:24 +00002183 manglePrefix(T->getQualifier());
John McCall33500952010-06-11 00:33:02 +00002184 mangleSourceName(T->getIdentifier());
2185 Out << 'E';
2186}
John McCall6ab30e02010-06-09 07:26:17 +00002187
John McCall33500952010-06-11 00:33:02 +00002188void CXXNameMangler::mangleType(const DependentTemplateSpecializationType *T) {
Douglas Gregoraa2187d2011-02-28 00:04:36 +00002189 // Dependently-scoped template types are nested if they have a prefix.
John McCall33500952010-06-11 00:33:02 +00002190 Out << 'N';
2191
2192 // TODO: avoid making this TemplateName.
2193 TemplateName Prefix =
2194 getASTContext().getDependentTemplateName(T->getQualifier(),
2195 T->getIdentifier());
2196 mangleTemplatePrefix(Prefix);
2197
2198 // FIXME: GCC does not appear to mangle the template arguments when
2199 // the template in question is a dependent template name. Should we
2200 // emulate that badness?
Eli Friedmand7a6b162012-09-26 02:36:12 +00002201 mangleTemplateArgs(T->getArgs(), T->getNumArgs());
Anders Carlssonae352482009-09-26 02:26:02 +00002202 Out << 'E';
John McCallefe6aee2009-09-05 07:56:18 +00002203}
2204
John McCallad5e7382010-03-01 23:49:17 +00002205void CXXNameMangler::mangleType(const TypeOfType *T) {
2206 // FIXME: this is pretty unsatisfactory, but there isn't an obvious
2207 // "extension with parameters" mangling.
2208 Out << "u6typeof";
2209}
2210
2211void CXXNameMangler::mangleType(const TypeOfExprType *T) {
2212 // FIXME: this is pretty unsatisfactory, but there isn't an obvious
2213 // "extension with parameters" mangling.
2214 Out << "u6typeof";
2215}
2216
2217void CXXNameMangler::mangleType(const DecltypeType *T) {
2218 Expr *E = T->getUnderlyingExpr();
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002219
John McCallad5e7382010-03-01 23:49:17 +00002220 // type ::= Dt <expression> E # decltype of an id-expression
2221 // # or class member access
2222 // ::= DT <expression> E # decltype of an expression
2223
2224 // This purports to be an exhaustive list of id-expressions and
2225 // class member accesses. Note that we do not ignore parentheses;
2226 // parentheses change the semantics of decltype for these
2227 // expressions (and cause the mangler to use the other form).
2228 if (isa<DeclRefExpr>(E) ||
2229 isa<MemberExpr>(E) ||
2230 isa<UnresolvedLookupExpr>(E) ||
2231 isa<DependentScopeDeclRefExpr>(E) ||
2232 isa<CXXDependentScopeMemberExpr>(E) ||
2233 isa<UnresolvedMemberExpr>(E))
2234 Out << "Dt";
2235 else
2236 Out << "DT";
2237 mangleExpression(E);
2238 Out << 'E';
2239}
2240
Sean Huntca63c202011-05-24 22:41:36 +00002241void CXXNameMangler::mangleType(const UnaryTransformType *T) {
2242 // If this is dependent, we need to record that. If not, we simply
2243 // mangle it as the underlying type since they are equivalent.
2244 if (T->isDependentType()) {
2245 Out << 'U';
2246
2247 switch (T->getUTTKind()) {
2248 case UnaryTransformType::EnumUnderlyingType:
2249 Out << "3eut";
2250 break;
2251 }
2252 }
2253
2254 mangleType(T->getUnderlyingType());
2255}
2256
Richard Smith34b41d92011-02-20 03:19:35 +00002257void CXXNameMangler::mangleType(const AutoType *T) {
2258 QualType D = T->getDeducedType();
Richard Smith967ecd32011-02-21 20:10:02 +00002259 // <builtin-type> ::= Da # dependent auto
2260 if (D.isNull())
2261 Out << "Da";
2262 else
2263 mangleType(D);
Richard Smith34b41d92011-02-20 03:19:35 +00002264}
2265
Eli Friedmanb001de72011-10-06 23:00:33 +00002266void CXXNameMangler::mangleType(const AtomicType *T) {
2267 // <type> ::= U <source-name> <type> # vendor extended type qualifier
2268 // (Until there's a standardized mangling...)
2269 Out << "U7_Atomic";
2270 mangleType(T->getValueType());
2271}
2272
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002273void CXXNameMangler::mangleIntegerLiteral(QualType T,
Anders Carlssone170ba72009-12-14 01:45:37 +00002274 const llvm::APSInt &Value) {
2275 // <expr-primary> ::= L <type> <value number> E # integer literal
2276 Out << 'L';
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002277
Anders Carlssone170ba72009-12-14 01:45:37 +00002278 mangleType(T);
2279 if (T->isBooleanType()) {
2280 // Boolean values are encoded as 0/1.
2281 Out << (Value.getBoolValue() ? '1' : '0');
2282 } else {
John McCall0512e482010-07-14 04:20:34 +00002283 mangleNumber(Value);
Anders Carlssone170ba72009-12-14 01:45:37 +00002284 }
2285 Out << 'E';
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002286
Anders Carlssone170ba72009-12-14 01:45:37 +00002287}
2288
Douglas Gregorcefc3af2012-04-16 07:05:22 +00002289/// Mangles a member expression.
John McCalla0ce15c2011-04-24 08:23:24 +00002290void CXXNameMangler::mangleMemberExpr(const Expr *base,
2291 bool isArrow,
2292 NestedNameSpecifier *qualifier,
2293 NamedDecl *firstQualifierLookup,
2294 DeclarationName member,
2295 unsigned arity) {
2296 // <expression> ::= dt <expression> <unresolved-name>
2297 // ::= pt <expression> <unresolved-name>
Douglas Gregorcefc3af2012-04-16 07:05:22 +00002298 if (base) {
2299 if (base->isImplicitCXXThis()) {
2300 // Note: GCC mangles member expressions to the implicit 'this' as
2301 // *this., whereas we represent them as this->. The Itanium C++ ABI
2302 // does not specify anything here, so we follow GCC.
2303 Out << "dtdefpT";
2304 } else {
2305 Out << (isArrow ? "pt" : "dt");
2306 mangleExpression(base);
2307 }
2308 }
John McCalla0ce15c2011-04-24 08:23:24 +00002309 mangleUnresolvedName(qualifier, firstQualifierLookup, member, arity);
John McCall2f27bf82010-02-04 02:56:29 +00002310}
2311
John McCall5a7e6f72011-04-28 02:52:03 +00002312/// Look at the callee of the given call expression and determine if
2313/// it's a parenthesized id-expression which would have triggered ADL
2314/// otherwise.
2315static bool isParenthesizedADLCallee(const CallExpr *call) {
2316 const Expr *callee = call->getCallee();
2317 const Expr *fn = callee->IgnoreParens();
2318
2319 // Must be parenthesized. IgnoreParens() skips __extension__ nodes,
2320 // too, but for those to appear in the callee, it would have to be
2321 // parenthesized.
2322 if (callee == fn) return false;
2323
2324 // Must be an unresolved lookup.
2325 const UnresolvedLookupExpr *lookup = dyn_cast<UnresolvedLookupExpr>(fn);
2326 if (!lookup) return false;
2327
2328 assert(!lookup->requiresADL());
2329
2330 // Must be an unqualified lookup.
2331 if (lookup->getQualifier()) return false;
2332
2333 // Must not have found a class member. Note that if one is a class
2334 // member, they're all class members.
2335 if (lookup->getNumDecls() > 0 &&
2336 (*lookup->decls_begin())->isCXXClassMember())
2337 return false;
2338
2339 // Otherwise, ADL would have been triggered.
2340 return true;
2341}
2342
John McCall5e1e89b2010-08-18 19:18:59 +00002343void CXXNameMangler::mangleExpression(const Expr *E, unsigned Arity) {
Anders Carlssond553f8c2009-09-21 01:21:10 +00002344 // <expression> ::= <unary operator-name> <expression>
John McCall09cc1412010-02-03 00:55:45 +00002345 // ::= <binary operator-name> <expression> <expression>
2346 // ::= <trinary operator-name> <expression> <expression> <expression>
Anders Carlssond553f8c2009-09-21 01:21:10 +00002347 // ::= cv <type> expression # conversion with one argument
2348 // ::= cv <type> _ <expression>* E # conversion with a different number of arguments
Eli Friedmana7e68452010-08-22 01:00:03 +00002349 // ::= st <type> # sizeof (a type)
Anders Carlssond553f8c2009-09-21 01:21:10 +00002350 // ::= at <type> # alignof (a type)
2351 // ::= <template-param>
2352 // ::= <function-param>
2353 // ::= sr <type> <unqualified-name> # dependent name
2354 // ::= sr <type> <unqualified-name> <template-args> # dependent template-id
Douglas Gregor63f62df2011-06-05 05:27:58 +00002355 // ::= ds <expression> <expression> # expr.*expr
Anders Carlssond553f8c2009-09-21 01:21:10 +00002356 // ::= sZ <template-param> # size of a parameter pack
Douglas Gregor4fc48662011-01-13 16:39:34 +00002357 // ::= sZ <function-param> # size of a function parameter pack
John McCall09cc1412010-02-03 00:55:45 +00002358 // ::= <expr-primary>
John McCall1dd73832010-02-04 01:42:13 +00002359 // <expr-primary> ::= L <type> <value number> E # integer literal
2360 // ::= L <type <value float> E # floating literal
2361 // ::= L <mangled-name> E # external name
Douglas Gregorcefc3af2012-04-16 07:05:22 +00002362 // ::= fpT # 'this' expression
Douglas Gregoredee94b2011-07-12 04:47:20 +00002363 QualType ImplicitlyConvertedToType;
2364
2365recurse:
Anders Carlssond553f8c2009-09-21 01:21:10 +00002366 switch (E->getStmtClass()) {
John McCall6ae1f352010-04-09 22:26:14 +00002367 case Expr::NoStmtClass:
John McCall63c00d72011-02-09 08:16:59 +00002368#define ABSTRACT_STMT(Type)
John McCall6ae1f352010-04-09 22:26:14 +00002369#define EXPR(Type, Base)
2370#define STMT(Type, Base) \
2371 case Expr::Type##Class:
Sean Hunt4bfe1962010-05-05 15:24:00 +00002372#include "clang/AST/StmtNodes.inc"
John McCall0512e482010-07-14 04:20:34 +00002373 // fallthrough
2374
2375 // These all can only appear in local or variable-initialization
2376 // contexts and so should never appear in a mangling.
2377 case Expr::AddrLabelExprClass:
John McCall0512e482010-07-14 04:20:34 +00002378 case Expr::DesignatedInitExprClass:
2379 case Expr::ImplicitValueInitExprClass:
John McCall0512e482010-07-14 04:20:34 +00002380 case Expr::ParenListExprClass:
Douglas Gregor01d08012012-02-07 10:09:13 +00002381 case Expr::LambdaExprClass:
John McCall09cc1412010-02-03 00:55:45 +00002382 llvm_unreachable("unexpected statement kind");
John McCall09cc1412010-02-03 00:55:45 +00002383
John McCall0512e482010-07-14 04:20:34 +00002384 // FIXME: invent manglings for all these.
2385 case Expr::BlockExprClass:
2386 case Expr::CXXPseudoDestructorExprClass:
2387 case Expr::ChooseExprClass:
2388 case Expr::CompoundLiteralExprClass:
2389 case Expr::ExtVectorElementExprClass:
Peter Collingbournef111d932011-04-15 00:35:48 +00002390 case Expr::GenericSelectionExprClass:
John McCall0512e482010-07-14 04:20:34 +00002391 case Expr::ObjCEncodeExprClass:
John McCall0512e482010-07-14 04:20:34 +00002392 case Expr::ObjCIsaExprClass:
2393 case Expr::ObjCIvarRefExprClass:
2394 case Expr::ObjCMessageExprClass:
2395 case Expr::ObjCPropertyRefExprClass:
2396 case Expr::ObjCProtocolExprClass:
2397 case Expr::ObjCSelectorExprClass:
2398 case Expr::ObjCStringLiteralClass:
Patrick Beardeb382ec2012-04-19 00:25:12 +00002399 case Expr::ObjCBoxedExprClass:
Ted Kremenekebcb57a2012-03-06 20:05:56 +00002400 case Expr::ObjCArrayLiteralClass:
2401 case Expr::ObjCDictionaryLiteralClass:
2402 case Expr::ObjCSubscriptRefExprClass:
John McCallf85e1932011-06-15 23:02:42 +00002403 case Expr::ObjCIndirectCopyRestoreExprClass:
John McCall0512e482010-07-14 04:20:34 +00002404 case Expr::OffsetOfExprClass:
2405 case Expr::PredefinedExprClass:
2406 case Expr::ShuffleVectorExprClass:
2407 case Expr::StmtExprClass:
John McCall0512e482010-07-14 04:20:34 +00002408 case Expr::UnaryTypeTraitExprClass:
Francois Pichet6ad6f282010-12-07 00:08:36 +00002409 case Expr::BinaryTypeTraitExprClass:
Douglas Gregor4ca8ac22012-02-24 07:38:34 +00002410 case Expr::TypeTraitExprClass:
John Wiegley21ff2e52011-04-28 00:16:57 +00002411 case Expr::ArrayTypeTraitExprClass:
John Wiegley55262202011-04-25 06:54:41 +00002412 case Expr::ExpressionTraitExprClass:
Francois Pichet9be88402010-09-08 23:47:05 +00002413 case Expr::VAArgExprClass:
Sebastian Redl2e156222010-09-10 20:55:43 +00002414 case Expr::CXXUuidofExprClass:
Tanya Lattner61eee0c2011-06-04 00:47:47 +00002415 case Expr::CUDAKernelCallExprClass:
2416 case Expr::AsTypeExprClass:
John McCall4b9c2d22011-11-06 09:01:30 +00002417 case Expr::PseudoObjectExprClass:
Eli Friedman276b0612011-10-11 02:20:01 +00002418 case Expr::AtomicExprClass:
Tanya Lattner61eee0c2011-06-04 00:47:47 +00002419 {
John McCall6ae1f352010-04-09 22:26:14 +00002420 // As bad as this diagnostic is, it's better than crashing.
David Blaikied6471f72011-09-25 23:23:43 +00002421 DiagnosticsEngine &Diags = Context.getDiags();
2422 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
John McCall6ae1f352010-04-09 22:26:14 +00002423 "cannot yet mangle expression type %0");
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +00002424 Diags.Report(E->getExprLoc(), DiagID)
John McCall739bf092010-04-10 09:39:25 +00002425 << E->getStmtClassName() << E->getSourceRange();
John McCall6ae1f352010-04-09 22:26:14 +00002426 break;
2427 }
2428
John McCall56ca35d2011-02-17 10:25:35 +00002429 // Even gcc-4.5 doesn't mangle this.
2430 case Expr::BinaryConditionalOperatorClass: {
David Blaikied6471f72011-09-25 23:23:43 +00002431 DiagnosticsEngine &Diags = Context.getDiags();
John McCall56ca35d2011-02-17 10:25:35 +00002432 unsigned DiagID =
David Blaikied6471f72011-09-25 23:23:43 +00002433 Diags.getCustomDiagID(DiagnosticsEngine::Error,
John McCall56ca35d2011-02-17 10:25:35 +00002434 "?: operator with omitted middle operand cannot be mangled");
2435 Diags.Report(E->getExprLoc(), DiagID)
2436 << E->getStmtClassName() << E->getSourceRange();
2437 break;
2438 }
2439
2440 // These are used for internal purposes and cannot be meaningfully mangled.
John McCall7cd7d1a2010-11-15 23:31:06 +00002441 case Expr::OpaqueValueExprClass:
2442 llvm_unreachable("cannot mangle opaque value; mangling wrong thing?");
2443
Sebastian Redlfaf4ef62012-02-25 22:59:28 +00002444 case Expr::InitListExprClass: {
2445 // Proposal by Jason Merrill, 2012-01-03
2446 Out << "il";
2447 const InitListExpr *InitList = cast<InitListExpr>(E);
2448 for (unsigned i = 0, e = InitList->getNumInits(); i != e; ++i)
2449 mangleExpression(InitList->getInit(i));
2450 Out << "E";
2451 break;
2452 }
2453
John McCall0512e482010-07-14 04:20:34 +00002454 case Expr::CXXDefaultArgExprClass:
John McCall5e1e89b2010-08-18 19:18:59 +00002455 mangleExpression(cast<CXXDefaultArgExpr>(E)->getExpr(), Arity);
John McCall0512e482010-07-14 04:20:34 +00002456 break;
2457
John McCall91a57552011-07-15 05:09:51 +00002458 case Expr::SubstNonTypeTemplateParmExprClass:
2459 mangleExpression(cast<SubstNonTypeTemplateParmExpr>(E)->getReplacement(),
2460 Arity);
2461 break;
2462
Richard Smith9fcce652012-03-07 08:35:16 +00002463 case Expr::UserDefinedLiteralClass:
2464 // We follow g++'s approach of mangling a UDL as a call to the literal
2465 // operator.
John McCall0512e482010-07-14 04:20:34 +00002466 case Expr::CXXMemberCallExprClass: // fallthrough
John McCall1dd73832010-02-04 01:42:13 +00002467 case Expr::CallExprClass: {
2468 const CallExpr *CE = cast<CallExpr>(E);
John McCall5a7e6f72011-04-28 02:52:03 +00002469
2470 // <expression> ::= cp <simple-id> <expression>* E
2471 // We use this mangling only when the call would use ADL except
2472 // for being parenthesized. Per discussion with David
2473 // Vandervoorde, 2011.04.25.
2474 if (isParenthesizedADLCallee(CE)) {
2475 Out << "cp";
2476 // The callee here is a parenthesized UnresolvedLookupExpr with
2477 // no qualifier and should always get mangled as a <simple-id>
2478 // anyway.
2479
2480 // <expression> ::= cl <expression>* E
2481 } else {
2482 Out << "cl";
2483 }
2484
John McCall5e1e89b2010-08-18 19:18:59 +00002485 mangleExpression(CE->getCallee(), CE->getNumArgs());
John McCall1dd73832010-02-04 01:42:13 +00002486 for (unsigned I = 0, N = CE->getNumArgs(); I != N; ++I)
2487 mangleExpression(CE->getArg(I));
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002488 Out << 'E';
John McCall09cc1412010-02-03 00:55:45 +00002489 break;
John McCall1dd73832010-02-04 01:42:13 +00002490 }
John McCall09cc1412010-02-03 00:55:45 +00002491
John McCall0512e482010-07-14 04:20:34 +00002492 case Expr::CXXNewExprClass: {
John McCall0512e482010-07-14 04:20:34 +00002493 const CXXNewExpr *New = cast<CXXNewExpr>(E);
2494 if (New->isGlobalNew()) Out << "gs";
2495 Out << (New->isArray() ? "na" : "nw");
2496 for (CXXNewExpr::const_arg_iterator I = New->placement_arg_begin(),
2497 E = New->placement_arg_end(); I != E; ++I)
2498 mangleExpression(*I);
2499 Out << '_';
2500 mangleType(New->getAllocatedType());
2501 if (New->hasInitializer()) {
Sebastian Redlfaf4ef62012-02-25 22:59:28 +00002502 // Proposal by Jason Merrill, 2012-01-03
Sebastian Redlb76ffc52012-02-25 20:51:07 +00002503 if (New->getInitializationStyle() == CXXNewExpr::ListInit)
Sebastian Redlfaf4ef62012-02-25 22:59:28 +00002504 Out << "il";
Sebastian Redlb76ffc52012-02-25 20:51:07 +00002505 else
2506 Out << "pi";
Sebastian Redl2aed8b82012-02-16 12:22:20 +00002507 const Expr *Init = New->getInitializer();
2508 if (const CXXConstructExpr *CCE = dyn_cast<CXXConstructExpr>(Init)) {
2509 // Directly inline the initializers.
2510 for (CXXConstructExpr::const_arg_iterator I = CCE->arg_begin(),
2511 E = CCE->arg_end();
2512 I != E; ++I)
2513 mangleExpression(*I);
2514 } else if (const ParenListExpr *PLE = dyn_cast<ParenListExpr>(Init)) {
2515 for (unsigned i = 0, e = PLE->getNumExprs(); i != e; ++i)
2516 mangleExpression(PLE->getExpr(i));
Sebastian Redlb76ffc52012-02-25 20:51:07 +00002517 } else if (New->getInitializationStyle() == CXXNewExpr::ListInit &&
2518 isa<InitListExpr>(Init)) {
Sebastian Redlfaf4ef62012-02-25 22:59:28 +00002519 // Only take InitListExprs apart for list-initialization.
Sebastian Redlb76ffc52012-02-25 20:51:07 +00002520 const InitListExpr *InitList = cast<InitListExpr>(Init);
2521 for (unsigned i = 0, e = InitList->getNumInits(); i != e; ++i)
2522 mangleExpression(InitList->getInit(i));
Sebastian Redl2aed8b82012-02-16 12:22:20 +00002523 } else
2524 mangleExpression(Init);
John McCall0512e482010-07-14 04:20:34 +00002525 }
2526 Out << 'E';
2527 break;
2528 }
2529
John McCall2f27bf82010-02-04 02:56:29 +00002530 case Expr::MemberExprClass: {
2531 const MemberExpr *ME = cast<MemberExpr>(E);
2532 mangleMemberExpr(ME->getBase(), ME->isArrow(),
John McCalla0ce15c2011-04-24 08:23:24 +00002533 ME->getQualifier(), 0, ME->getMemberDecl()->getDeclName(),
John McCall5e1e89b2010-08-18 19:18:59 +00002534 Arity);
John McCall2f27bf82010-02-04 02:56:29 +00002535 break;
2536 }
2537
2538 case Expr::UnresolvedMemberExprClass: {
2539 const UnresolvedMemberExpr *ME = cast<UnresolvedMemberExpr>(E);
2540 mangleMemberExpr(ME->getBase(), ME->isArrow(),
John McCalla0ce15c2011-04-24 08:23:24 +00002541 ME->getQualifier(), 0, ME->getMemberName(),
John McCall5e1e89b2010-08-18 19:18:59 +00002542 Arity);
John McCall6dbce192010-08-20 00:17:19 +00002543 if (ME->hasExplicitTemplateArgs())
2544 mangleTemplateArgs(ME->getExplicitTemplateArgs());
John McCall2f27bf82010-02-04 02:56:29 +00002545 break;
2546 }
2547
2548 case Expr::CXXDependentScopeMemberExprClass: {
2549 const CXXDependentScopeMemberExpr *ME
2550 = cast<CXXDependentScopeMemberExpr>(E);
2551 mangleMemberExpr(ME->getBase(), ME->isArrow(),
John McCalla0ce15c2011-04-24 08:23:24 +00002552 ME->getQualifier(), ME->getFirstQualifierFoundInScope(),
2553 ME->getMember(), Arity);
John McCall6dbce192010-08-20 00:17:19 +00002554 if (ME->hasExplicitTemplateArgs())
2555 mangleTemplateArgs(ME->getExplicitTemplateArgs());
John McCall2f27bf82010-02-04 02:56:29 +00002556 break;
2557 }
2558
John McCall1dd73832010-02-04 01:42:13 +00002559 case Expr::UnresolvedLookupExprClass: {
2560 const UnresolvedLookupExpr *ULE = cast<UnresolvedLookupExpr>(E);
John McCalla0ce15c2011-04-24 08:23:24 +00002561 mangleUnresolvedName(ULE->getQualifier(), 0, ULE->getName(), Arity);
John McCall26a6ec72011-06-21 22:12:46 +00002562
2563 // All the <unresolved-name> productions end in a
2564 // base-unresolved-name, where <template-args> are just tacked
2565 // onto the end.
John McCall6dbce192010-08-20 00:17:19 +00002566 if (ULE->hasExplicitTemplateArgs())
2567 mangleTemplateArgs(ULE->getExplicitTemplateArgs());
John McCall1dd73832010-02-04 01:42:13 +00002568 break;
2569 }
2570
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002571 case Expr::CXXUnresolvedConstructExprClass: {
John McCall1dd73832010-02-04 01:42:13 +00002572 const CXXUnresolvedConstructExpr *CE = cast<CXXUnresolvedConstructExpr>(E);
2573 unsigned N = CE->arg_size();
2574
2575 Out << "cv";
2576 mangleType(CE->getType());
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002577 if (N != 1) Out << '_';
John McCall1dd73832010-02-04 01:42:13 +00002578 for (unsigned I = 0; I != N; ++I) mangleExpression(CE->getArg(I));
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002579 if (N != 1) Out << 'E';
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002580 break;
John McCall1dd73832010-02-04 01:42:13 +00002581 }
John McCall09cc1412010-02-03 00:55:45 +00002582
John McCall1dd73832010-02-04 01:42:13 +00002583 case Expr::CXXTemporaryObjectExprClass:
2584 case Expr::CXXConstructExprClass: {
2585 const CXXConstructExpr *CE = cast<CXXConstructExpr>(E);
2586 unsigned N = CE->getNumArgs();
2587
Sebastian Redlfaf4ef62012-02-25 22:59:28 +00002588 // Proposal by Jason Merrill, 2012-01-03
2589 if (CE->isListInitialization())
2590 Out << "tl";
2591 else
2592 Out << "cv";
John McCall1dd73832010-02-04 01:42:13 +00002593 mangleType(CE->getType());
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002594 if (N != 1) Out << '_';
John McCall1dd73832010-02-04 01:42:13 +00002595 for (unsigned I = 0; I != N; ++I) mangleExpression(CE->getArg(I));
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002596 if (N != 1) Out << 'E';
John McCall09cc1412010-02-03 00:55:45 +00002597 break;
John McCall1dd73832010-02-04 01:42:13 +00002598 }
2599
Richard Smith41576d42012-02-06 02:54:51 +00002600 case Expr::CXXScalarValueInitExprClass:
2601 Out <<"cv";
2602 mangleType(E->getType());
2603 Out <<"_E";
2604 break;
2605
John McCall9653ab52012-09-25 09:10:17 +00002606 case Expr::CXXNoexceptExprClass:
2607 Out << "nx";
2608 mangleExpression(cast<CXXNoexceptExpr>(E)->getOperand());
2609 break;
2610
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +00002611 case Expr::UnaryExprOrTypeTraitExprClass: {
2612 const UnaryExprOrTypeTraitExpr *SAE = cast<UnaryExprOrTypeTraitExpr>(E);
Douglas Gregoredee94b2011-07-12 04:47:20 +00002613
2614 if (!SAE->isInstantiationDependent()) {
2615 // Itanium C++ ABI:
2616 // If the operand of a sizeof or alignof operator is not
2617 // instantiation-dependent it is encoded as an integer literal
2618 // reflecting the result of the operator.
2619 //
2620 // If the result of the operator is implicitly converted to a known
2621 // integer type, that type is used for the literal; otherwise, the type
2622 // of std::size_t or std::ptrdiff_t is used.
2623 QualType T = (ImplicitlyConvertedToType.isNull() ||
2624 !ImplicitlyConvertedToType->isIntegerType())? SAE->getType()
2625 : ImplicitlyConvertedToType;
Richard Smitha6b8b2c2011-10-10 18:28:20 +00002626 llvm::APSInt V = SAE->EvaluateKnownConstInt(Context.getASTContext());
2627 mangleIntegerLiteral(T, V);
Douglas Gregoredee94b2011-07-12 04:47:20 +00002628 break;
2629 }
2630
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +00002631 switch(SAE->getKind()) {
2632 case UETT_SizeOf:
2633 Out << 's';
2634 break;
2635 case UETT_AlignOf:
2636 Out << 'a';
2637 break;
2638 case UETT_VecStep:
David Blaikied6471f72011-09-25 23:23:43 +00002639 DiagnosticsEngine &Diags = Context.getDiags();
2640 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +00002641 "cannot yet mangle vec_step expression");
2642 Diags.Report(DiagID);
2643 return;
2644 }
John McCall1dd73832010-02-04 01:42:13 +00002645 if (SAE->isArgumentType()) {
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002646 Out << 't';
John McCall1dd73832010-02-04 01:42:13 +00002647 mangleType(SAE->getArgumentType());
2648 } else {
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002649 Out << 'z';
John McCall1dd73832010-02-04 01:42:13 +00002650 mangleExpression(SAE->getArgumentExpr());
2651 }
2652 break;
2653 }
Anders Carlssona7694082009-11-06 02:50:19 +00002654
John McCall0512e482010-07-14 04:20:34 +00002655 case Expr::CXXThrowExprClass: {
2656 const CXXThrowExpr *TE = cast<CXXThrowExpr>(E);
2657
2658 // Proposal from David Vandervoorde, 2010.06.30
2659 if (TE->getSubExpr()) {
2660 Out << "tw";
2661 mangleExpression(TE->getSubExpr());
2662 } else {
2663 Out << "tr";
2664 }
2665 break;
2666 }
2667
2668 case Expr::CXXTypeidExprClass: {
2669 const CXXTypeidExpr *TIE = cast<CXXTypeidExpr>(E);
2670
2671 // Proposal from David Vandervoorde, 2010.06.30
2672 if (TIE->isTypeOperand()) {
2673 Out << "ti";
2674 mangleType(TIE->getTypeOperand());
2675 } else {
2676 Out << "te";
2677 mangleExpression(TIE->getExprOperand());
2678 }
2679 break;
2680 }
2681
2682 case Expr::CXXDeleteExprClass: {
2683 const CXXDeleteExpr *DE = cast<CXXDeleteExpr>(E);
2684
2685 // Proposal from David Vandervoorde, 2010.06.30
2686 if (DE->isGlobalDelete()) Out << "gs";
2687 Out << (DE->isArrayForm() ? "da" : "dl");
2688 mangleExpression(DE->getArgument());
2689 break;
2690 }
2691
Anders Carlssone170ba72009-12-14 01:45:37 +00002692 case Expr::UnaryOperatorClass: {
2693 const UnaryOperator *UO = cast<UnaryOperator>(E);
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002694 mangleOperatorName(UnaryOperator::getOverloadedOperator(UO->getOpcode()),
Anders Carlssone170ba72009-12-14 01:45:37 +00002695 /*Arity=*/1);
2696 mangleExpression(UO->getSubExpr());
2697 break;
2698 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002699
John McCall0512e482010-07-14 04:20:34 +00002700 case Expr::ArraySubscriptExprClass: {
2701 const ArraySubscriptExpr *AE = cast<ArraySubscriptExpr>(E);
2702
Chris Lattnerfc8f0e12011-04-15 05:22:18 +00002703 // Array subscript is treated as a syntactically weird form of
John McCall0512e482010-07-14 04:20:34 +00002704 // binary operator.
2705 Out << "ix";
2706 mangleExpression(AE->getLHS());
2707 mangleExpression(AE->getRHS());
2708 break;
2709 }
2710
2711 case Expr::CompoundAssignOperatorClass: // fallthrough
Anders Carlssone170ba72009-12-14 01:45:37 +00002712 case Expr::BinaryOperatorClass: {
2713 const BinaryOperator *BO = cast<BinaryOperator>(E);
Douglas Gregor63f62df2011-06-05 05:27:58 +00002714 if (BO->getOpcode() == BO_PtrMemD)
2715 Out << "ds";
2716 else
2717 mangleOperatorName(BinaryOperator::getOverloadedOperator(BO->getOpcode()),
2718 /*Arity=*/2);
Anders Carlssone170ba72009-12-14 01:45:37 +00002719 mangleExpression(BO->getLHS());
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002720 mangleExpression(BO->getRHS());
Anders Carlssone170ba72009-12-14 01:45:37 +00002721 break;
John McCall2f27bf82010-02-04 02:56:29 +00002722 }
Anders Carlssone170ba72009-12-14 01:45:37 +00002723
2724 case Expr::ConditionalOperatorClass: {
2725 const ConditionalOperator *CO = cast<ConditionalOperator>(E);
2726 mangleOperatorName(OO_Conditional, /*Arity=*/3);
2727 mangleExpression(CO->getCond());
John McCall5e1e89b2010-08-18 19:18:59 +00002728 mangleExpression(CO->getLHS(), Arity);
2729 mangleExpression(CO->getRHS(), Arity);
Anders Carlssone170ba72009-12-14 01:45:37 +00002730 break;
2731 }
2732
Douglas Gregor46287c72010-01-29 16:37:09 +00002733 case Expr::ImplicitCastExprClass: {
Douglas Gregoredee94b2011-07-12 04:47:20 +00002734 ImplicitlyConvertedToType = E->getType();
2735 E = cast<ImplicitCastExpr>(E)->getSubExpr();
2736 goto recurse;
Douglas Gregor46287c72010-01-29 16:37:09 +00002737 }
John McCallf85e1932011-06-15 23:02:42 +00002738
2739 case Expr::ObjCBridgedCastExprClass: {
2740 // Mangle ownership casts as a vendor extended operator __bridge,
2741 // __bridge_transfer, or __bridge_retain.
Chris Lattner5f9e2722011-07-23 10:55:15 +00002742 StringRef Kind = cast<ObjCBridgedCastExpr>(E)->getBridgeKindName();
John McCallf85e1932011-06-15 23:02:42 +00002743 Out << "v1U" << Kind.size() << Kind;
2744 }
2745 // Fall through to mangle the cast itself.
2746
Douglas Gregor46287c72010-01-29 16:37:09 +00002747 case Expr::CStyleCastExprClass:
2748 case Expr::CXXStaticCastExprClass:
2749 case Expr::CXXDynamicCastExprClass:
2750 case Expr::CXXReinterpretCastExprClass:
2751 case Expr::CXXConstCastExprClass:
2752 case Expr::CXXFunctionalCastExprClass: {
2753 const ExplicitCastExpr *ECE = cast<ExplicitCastExpr>(E);
2754 Out << "cv";
2755 mangleType(ECE->getType());
2756 mangleExpression(ECE->getSubExpr());
2757 break;
2758 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002759
Anders Carlsson58040a52009-12-16 05:48:46 +00002760 case Expr::CXXOperatorCallExprClass: {
2761 const CXXOperatorCallExpr *CE = cast<CXXOperatorCallExpr>(E);
2762 unsigned NumArgs = CE->getNumArgs();
2763 mangleOperatorName(CE->getOperator(), /*Arity=*/NumArgs);
2764 // Mangle the arguments.
2765 for (unsigned i = 0; i != NumArgs; ++i)
2766 mangleExpression(CE->getArg(i));
2767 break;
2768 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002769
Anders Carlssona7694082009-11-06 02:50:19 +00002770 case Expr::ParenExprClass:
John McCall5e1e89b2010-08-18 19:18:59 +00002771 mangleExpression(cast<ParenExpr>(E)->getSubExpr(), Arity);
Anders Carlssona7694082009-11-06 02:50:19 +00002772 break;
2773
Anders Carlssond553f8c2009-09-21 01:21:10 +00002774 case Expr::DeclRefExprClass: {
Douglas Gregor5ed1bc32010-02-28 21:40:32 +00002775 const NamedDecl *D = cast<DeclRefExpr>(E)->getDecl();
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002776
Anders Carlssond553f8c2009-09-21 01:21:10 +00002777 switch (D->getKind()) {
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002778 default:
Douglas Gregor5ed1bc32010-02-28 21:40:32 +00002779 // <expr-primary> ::= L <mangled-name> E # external name
2780 Out << 'L';
2781 mangle(D, "_Z");
2782 Out << 'E';
2783 break;
2784
John McCallfb44de92011-05-01 22:35:37 +00002785 case Decl::ParmVar:
2786 mangleFunctionParam(cast<ParmVarDecl>(D));
2787 break;
2788
John McCall3dc7e7b2010-07-24 01:17:35 +00002789 case Decl::EnumConstant: {
2790 const EnumConstantDecl *ED = cast<EnumConstantDecl>(D);
2791 mangleIntegerLiteral(ED->getType(), ED->getInitVal());
2792 break;
2793 }
2794
Anders Carlssond553f8c2009-09-21 01:21:10 +00002795 case Decl::NonTypeTemplateParm: {
2796 const NonTypeTemplateParmDecl *PD = cast<NonTypeTemplateParmDecl>(D);
Anders Carlsson0ccdf8d2009-09-27 00:38:53 +00002797 mangleTemplateParameter(PD->getIndex());
Anders Carlssond553f8c2009-09-21 01:21:10 +00002798 break;
2799 }
2800
2801 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002802
Anders Carlsson50755b02009-09-27 20:11:34 +00002803 break;
Anders Carlssond553f8c2009-09-21 01:21:10 +00002804 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002805
Douglas Gregorc7793c72011-01-15 01:15:58 +00002806 case Expr::SubstNonTypeTemplateParmPackExprClass:
John McCall68a51a72011-07-01 00:04:39 +00002807 // FIXME: not clear how to mangle this!
2808 // template <unsigned N...> class A {
2809 // template <class U...> void foo(U (&x)[N]...);
2810 // };
2811 Out << "_SUBSTPACK_";
Douglas Gregorc7793c72011-01-15 01:15:58 +00002812 break;
Richard Smith9a4db032012-09-12 00:56:43 +00002813
2814 case Expr::FunctionParmPackExprClass: {
2815 // FIXME: not clear how to mangle this!
2816 const FunctionParmPackExpr *FPPE = cast<FunctionParmPackExpr>(E);
2817 Out << "v110_SUBSTPACK";
2818 mangleFunctionParam(FPPE->getParameterPack());
2819 break;
2820 }
2821
John McCall865d4472009-11-19 22:55:06 +00002822 case Expr::DependentScopeDeclRefExprClass: {
2823 const DependentScopeDeclRefExpr *DRE = cast<DependentScopeDeclRefExpr>(E);
John McCall26a6ec72011-06-21 22:12:46 +00002824 mangleUnresolvedName(DRE->getQualifier(), 0, DRE->getDeclName(), Arity);
Douglas Gregor4b2ccfc2010-02-28 22:05:49 +00002825
John McCall26a6ec72011-06-21 22:12:46 +00002826 // All the <unresolved-name> productions end in a
2827 // base-unresolved-name, where <template-args> are just tacked
2828 // onto the end.
John McCall6dbce192010-08-20 00:17:19 +00002829 if (DRE->hasExplicitTemplateArgs())
2830 mangleTemplateArgs(DRE->getExplicitTemplateArgs());
Anders Carlsson50755b02009-09-27 20:11:34 +00002831 break;
2832 }
2833
John McCalld9307602010-04-09 22:54:09 +00002834 case Expr::CXXBindTemporaryExprClass:
2835 mangleExpression(cast<CXXBindTemporaryExpr>(E)->getSubExpr());
2836 break;
2837
John McCall4765fa02010-12-06 08:20:24 +00002838 case Expr::ExprWithCleanupsClass:
2839 mangleExpression(cast<ExprWithCleanups>(E)->getSubExpr(), Arity);
John McCalld9307602010-04-09 22:54:09 +00002840 break;
2841
John McCall1dd73832010-02-04 01:42:13 +00002842 case Expr::FloatingLiteralClass: {
2843 const FloatingLiteral *FL = cast<FloatingLiteral>(E);
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002844 Out << 'L';
John McCall1dd73832010-02-04 01:42:13 +00002845 mangleType(FL->getType());
John McCall0512e482010-07-14 04:20:34 +00002846 mangleFloat(FL->getValue());
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002847 Out << 'E';
John McCall1dd73832010-02-04 01:42:13 +00002848 break;
2849 }
2850
John McCallde810632010-04-09 21:48:08 +00002851 case Expr::CharacterLiteralClass:
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002852 Out << 'L';
John McCallde810632010-04-09 21:48:08 +00002853 mangleType(E->getType());
2854 Out << cast<CharacterLiteral>(E)->getValue();
2855 Out << 'E';
2856 break;
2857
Ted Kremenekebcb57a2012-03-06 20:05:56 +00002858 // FIXME. __objc_yes/__objc_no are mangled same as true/false
2859 case Expr::ObjCBoolLiteralExprClass:
2860 Out << "Lb";
2861 Out << (cast<ObjCBoolLiteralExpr>(E)->getValue() ? '1' : '0');
2862 Out << 'E';
2863 break;
2864
John McCallde810632010-04-09 21:48:08 +00002865 case Expr::CXXBoolLiteralExprClass:
2866 Out << "Lb";
2867 Out << (cast<CXXBoolLiteralExpr>(E)->getValue() ? '1' : '0');
2868 Out << 'E';
2869 break;
2870
John McCall0512e482010-07-14 04:20:34 +00002871 case Expr::IntegerLiteralClass: {
2872 llvm::APSInt Value(cast<IntegerLiteral>(E)->getValue());
2873 if (E->getType()->isSignedIntegerType())
2874 Value.setIsSigned(true);
2875 mangleIntegerLiteral(E->getType(), Value);
Anders Carlssone170ba72009-12-14 01:45:37 +00002876 break;
John McCall0512e482010-07-14 04:20:34 +00002877 }
2878
2879 case Expr::ImaginaryLiteralClass: {
2880 const ImaginaryLiteral *IE = cast<ImaginaryLiteral>(E);
2881 // Mangle as if a complex literal.
Nick Lewycky271b6652010-09-05 03:40:33 +00002882 // Proposal from David Vandevoorde, 2010.06.30.
John McCall0512e482010-07-14 04:20:34 +00002883 Out << 'L';
2884 mangleType(E->getType());
2885 if (const FloatingLiteral *Imag =
2886 dyn_cast<FloatingLiteral>(IE->getSubExpr())) {
2887 // Mangle a floating-point zero of the appropriate type.
2888 mangleFloat(llvm::APFloat(Imag->getValue().getSemantics()));
2889 Out << '_';
2890 mangleFloat(Imag->getValue());
2891 } else {
Nick Lewycky271b6652010-09-05 03:40:33 +00002892 Out << "0_";
John McCall0512e482010-07-14 04:20:34 +00002893 llvm::APSInt Value(cast<IntegerLiteral>(IE->getSubExpr())->getValue());
2894 if (IE->getSubExpr()->getType()->isSignedIntegerType())
2895 Value.setIsSigned(true);
2896 mangleNumber(Value);
2897 }
2898 Out << 'E';
2899 break;
2900 }
2901
2902 case Expr::StringLiteralClass: {
John McCall1658c392010-07-15 21:53:03 +00002903 // Revised proposal from David Vandervoorde, 2010.07.15.
John McCall0512e482010-07-14 04:20:34 +00002904 Out << 'L';
John McCall1658c392010-07-15 21:53:03 +00002905 assert(isa<ConstantArrayType>(E->getType()));
2906 mangleType(E->getType());
John McCall0512e482010-07-14 04:20:34 +00002907 Out << 'E';
2908 break;
2909 }
2910
2911 case Expr::GNUNullExprClass:
2912 // FIXME: should this really be mangled the same as nullptr?
2913 // fallthrough
2914
2915 case Expr::CXXNullPtrLiteralExprClass: {
2916 // Proposal from David Vandervoorde, 2010.06.30, as
2917 // modified by ABI list discussion.
2918 Out << "LDnE";
2919 break;
2920 }
Douglas Gregorbe230c32011-01-03 17:17:50 +00002921
2922 case Expr::PackExpansionExprClass:
2923 Out << "sp";
2924 mangleExpression(cast<PackExpansionExpr>(E)->getPattern());
2925 break;
Douglas Gregor2e774c42011-01-04 18:56:13 +00002926
2927 case Expr::SizeOfPackExprClass: {
Douglas Gregor2e774c42011-01-04 18:56:13 +00002928 Out << "sZ";
2929 const NamedDecl *Pack = cast<SizeOfPackExpr>(E)->getPack();
2930 if (const TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(Pack))
2931 mangleTemplateParameter(TTP->getIndex());
2932 else if (const NonTypeTemplateParmDecl *NTTP
2933 = dyn_cast<NonTypeTemplateParmDecl>(Pack))
2934 mangleTemplateParameter(NTTP->getIndex());
2935 else if (const TemplateTemplateParmDecl *TempTP
2936 = dyn_cast<TemplateTemplateParmDecl>(Pack))
2937 mangleTemplateParameter(TempTP->getIndex());
Douglas Gregor91832362011-07-12 07:03:48 +00002938 else
2939 mangleFunctionParam(cast<ParmVarDecl>(Pack));
Douglas Gregordfbbcf92011-03-03 02:20:19 +00002940 break;
Douglas Gregor2e774c42011-01-04 18:56:13 +00002941 }
Douglas Gregor03e80032011-06-21 17:03:29 +00002942
2943 case Expr::MaterializeTemporaryExprClass: {
2944 mangleExpression(cast<MaterializeTemporaryExpr>(E)->GetTemporaryExpr());
2945 break;
2946 }
Douglas Gregorcefc3af2012-04-16 07:05:22 +00002947
2948 case Expr::CXXThisExprClass:
2949 Out << "fpT";
2950 break;
Anders Carlssond553f8c2009-09-21 01:21:10 +00002951 }
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00002952}
2953
John McCallfb44de92011-05-01 22:35:37 +00002954/// Mangle an expression which refers to a parameter variable.
2955///
2956/// <expression> ::= <function-param>
2957/// <function-param> ::= fp <top-level CV-qualifiers> _ # L == 0, I == 0
2958/// <function-param> ::= fp <top-level CV-qualifiers>
2959/// <parameter-2 non-negative number> _ # L == 0, I > 0
2960/// <function-param> ::= fL <L-1 non-negative number>
2961/// p <top-level CV-qualifiers> _ # L > 0, I == 0
2962/// <function-param> ::= fL <L-1 non-negative number>
2963/// p <top-level CV-qualifiers>
2964/// <I-1 non-negative number> _ # L > 0, I > 0
2965///
2966/// L is the nesting depth of the parameter, defined as 1 if the
2967/// parameter comes from the innermost function prototype scope
2968/// enclosing the current context, 2 if from the next enclosing
2969/// function prototype scope, and so on, with one special case: if
2970/// we've processed the full parameter clause for the innermost
2971/// function type, then L is one less. This definition conveniently
2972/// makes it irrelevant whether a function's result type was written
2973/// trailing or leading, but is otherwise overly complicated; the
2974/// numbering was first designed without considering references to
2975/// parameter in locations other than return types, and then the
2976/// mangling had to be generalized without changing the existing
2977/// manglings.
2978///
2979/// I is the zero-based index of the parameter within its parameter
2980/// declaration clause. Note that the original ABI document describes
2981/// this using 1-based ordinals.
2982void CXXNameMangler::mangleFunctionParam(const ParmVarDecl *parm) {
2983 unsigned parmDepth = parm->getFunctionScopeDepth();
2984 unsigned parmIndex = parm->getFunctionScopeIndex();
2985
2986 // Compute 'L'.
2987 // parmDepth does not include the declaring function prototype.
2988 // FunctionTypeDepth does account for that.
2989 assert(parmDepth < FunctionTypeDepth.getDepth());
2990 unsigned nestingDepth = FunctionTypeDepth.getDepth() - parmDepth;
2991 if (FunctionTypeDepth.isInResultType())
2992 nestingDepth--;
2993
2994 if (nestingDepth == 0) {
2995 Out << "fp";
2996 } else {
2997 Out << "fL" << (nestingDepth - 1) << 'p';
2998 }
2999
3000 // Top-level qualifiers. We don't have to worry about arrays here,
3001 // because parameters declared as arrays should already have been
Benjamin Kramer48d798c2012-06-02 10:20:41 +00003002 // transformed to have pointer type. FIXME: apparently these don't
John McCallfb44de92011-05-01 22:35:37 +00003003 // get mangled if used as an rvalue of a known non-class type?
3004 assert(!parm->getType()->isArrayType()
3005 && "parameter's type is still an array type?");
3006 mangleQualifiers(parm->getType().getQualifiers());
3007
3008 // Parameter index.
3009 if (parmIndex != 0) {
3010 Out << (parmIndex - 1);
3011 }
3012 Out << '_';
3013}
3014
Anders Carlsson3ac86b52009-04-15 05:36:58 +00003015void CXXNameMangler::mangleCXXCtorType(CXXCtorType T) {
3016 // <ctor-dtor-name> ::= C1 # complete object constructor
3017 // ::= C2 # base object constructor
3018 // ::= C3 # complete object allocating constructor
3019 //
3020 switch (T) {
3021 case Ctor_Complete:
3022 Out << "C1";
3023 break;
3024 case Ctor_Base:
3025 Out << "C2";
3026 break;
3027 case Ctor_CompleteAllocating:
3028 Out << "C3";
3029 break;
3030 }
3031}
3032
Anders Carlsson27ae5362009-04-17 01:58:57 +00003033void CXXNameMangler::mangleCXXDtorType(CXXDtorType T) {
3034 // <ctor-dtor-name> ::= D0 # deleting destructor
3035 // ::= D1 # complete object destructor
3036 // ::= D2 # base object destructor
3037 //
3038 switch (T) {
3039 case Dtor_Deleting:
3040 Out << "D0";
3041 break;
3042 case Dtor_Complete:
3043 Out << "D1";
3044 break;
3045 case Dtor_Base:
3046 Out << "D2";
3047 break;
3048 }
3049}
3050
John McCall6dbce192010-08-20 00:17:19 +00003051void CXXNameMangler::mangleTemplateArgs(
Argyrios Kyrtzidisb0c3e092011-09-22 20:07:03 +00003052 const ASTTemplateArgumentListInfo &TemplateArgs) {
John McCall6dbce192010-08-20 00:17:19 +00003053 // <template-args> ::= I <template-arg>+ E
3054 Out << 'I';
John McCall4f4e4132011-05-04 01:45:19 +00003055 for (unsigned i = 0, e = TemplateArgs.NumTemplateArgs; i != e; ++i)
Eli Friedmand7a6b162012-09-26 02:36:12 +00003056 mangleTemplateArg(TemplateArgs.getTemplateArgs()[i].getArgument());
John McCall6dbce192010-08-20 00:17:19 +00003057 Out << 'E';
3058}
3059
Eli Friedmand7a6b162012-09-26 02:36:12 +00003060void CXXNameMangler::mangleTemplateArgs(const TemplateArgumentList &AL) {
Anders Carlsson7a0ba872009-05-15 16:09:15 +00003061 // <template-args> ::= I <template-arg>+ E
Benjamin Kramer35f59b62010-04-10 16:03:31 +00003062 Out << 'I';
Rafael Espindolad9800722010-03-11 14:07:00 +00003063 for (unsigned i = 0, e = AL.size(); i != e; ++i)
Eli Friedmand7a6b162012-09-26 02:36:12 +00003064 mangleTemplateArg(AL[i]);
Benjamin Kramer35f59b62010-04-10 16:03:31 +00003065 Out << 'E';
Anders Carlsson7a0ba872009-05-15 16:09:15 +00003066}
3067
Eli Friedmand7a6b162012-09-26 02:36:12 +00003068void CXXNameMangler::mangleTemplateArgs(const TemplateArgument *TemplateArgs,
Anders Carlsson7624f212009-09-18 02:42:01 +00003069 unsigned NumTemplateArgs) {
3070 // <template-args> ::= I <template-arg>+ E
Benjamin Kramer35f59b62010-04-10 16:03:31 +00003071 Out << 'I';
Daniel Dunbar7e0c1952009-11-21 09:17:15 +00003072 for (unsigned i = 0; i != NumTemplateArgs; ++i)
Eli Friedmand7a6b162012-09-26 02:36:12 +00003073 mangleTemplateArg(TemplateArgs[i]);
Benjamin Kramer35f59b62010-04-10 16:03:31 +00003074 Out << 'E';
Anders Carlsson7624f212009-09-18 02:42:01 +00003075}
3076
Eli Friedmand7a6b162012-09-26 02:36:12 +00003077void CXXNameMangler::mangleTemplateArg(TemplateArgument A) {
Mike Stump1eb44332009-09-09 15:08:12 +00003078 // <template-arg> ::= <type> # type or template
Anders Carlsson7a0ba872009-05-15 16:09:15 +00003079 // ::= X <expression> E # expression
3080 // ::= <expr-primary> # simple expressions
Douglas Gregor4fc48662011-01-13 16:39:34 +00003081 // ::= J <template-arg>* E # argument pack
Douglas Gregorf1588662011-07-12 15:18:55 +00003082 // ::= sp <expression> # pack expansion of (C++0x)
3083 if (!A.isInstantiationDependent() || A.isDependent())
3084 A = Context.getASTContext().getCanonicalTemplateArgument(A);
3085
Anders Carlsson7a0ba872009-05-15 16:09:15 +00003086 switch (A.getKind()) {
Douglas Gregorf90b27a2011-01-03 22:36:02 +00003087 case TemplateArgument::Null:
3088 llvm_unreachable("Cannot mangle NULL template argument");
3089
Anders Carlsson7a0ba872009-05-15 16:09:15 +00003090 case TemplateArgument::Type:
3091 mangleType(A.getAsType());
3092 break;
Anders Carlsson9e85c742009-12-23 19:30:55 +00003093 case TemplateArgument::Template:
John McCallb6f532e2010-07-14 06:43:17 +00003094 // This is mangled as <type>.
3095 mangleType(A.getAsTemplate());
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00003096 break;
Douglas Gregora7fc9012011-01-05 18:58:31 +00003097 case TemplateArgument::TemplateExpansion:
Douglas Gregor4fc48662011-01-13 16:39:34 +00003098 // <type> ::= Dp <type> # pack expansion (C++0x)
Douglas Gregora7fc9012011-01-05 18:58:31 +00003099 Out << "Dp";
3100 mangleType(A.getAsTemplateOrTemplatePattern());
3101 break;
John McCall092beef2012-01-06 05:06:35 +00003102 case TemplateArgument::Expression: {
3103 // It's possible to end up with a DeclRefExpr here in certain
3104 // dependent cases, in which case we should mangle as a
3105 // declaration.
3106 const Expr *E = A.getAsExpr()->IgnoreParens();
3107 if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E)) {
3108 const ValueDecl *D = DRE->getDecl();
3109 if (isa<VarDecl>(D) || isa<FunctionDecl>(D)) {
3110 Out << "L";
3111 mangle(D, "_Z");
3112 Out << 'E';
3113 break;
3114 }
3115 }
3116
Anders Carlssond553f8c2009-09-21 01:21:10 +00003117 Out << 'X';
John McCall092beef2012-01-06 05:06:35 +00003118 mangleExpression(E);
Anders Carlssond553f8c2009-09-21 01:21:10 +00003119 Out << 'E';
3120 break;
John McCall092beef2012-01-06 05:06:35 +00003121 }
Anders Carlssone170ba72009-12-14 01:45:37 +00003122 case TemplateArgument::Integral:
Benjamin Kramer85524372012-06-07 15:09:51 +00003123 mangleIntegerLiteral(A.getIntegralType(), A.getAsIntegral());
Anders Carlsson7a0ba872009-05-15 16:09:15 +00003124 break;
Daniel Dunbar7e0c1952009-11-21 09:17:15 +00003125 case TemplateArgument::Declaration: {
3126 // <expr-primary> ::= L <mangled-name> E # external name
Rafael Espindolad9800722010-03-11 14:07:00 +00003127 // Clang produces AST's where pointer-to-member-function expressions
Daniel Dunbar7e0c1952009-11-21 09:17:15 +00003128 // and pointer-to-function expressions are represented as a declaration not
Rafael Espindolad9800722010-03-11 14:07:00 +00003129 // an expression. We compensate for it here to produce the correct mangling.
Eli Friedmand7a6b162012-09-26 02:36:12 +00003130 ValueDecl *D = A.getAsDecl();
3131 bool compensateMangling = !A.isDeclForReferenceParam();
Rafael Espindolad9800722010-03-11 14:07:00 +00003132 if (compensateMangling) {
3133 Out << 'X';
3134 mangleOperatorName(OO_Amp, 1);
3135 }
3136
Daniel Dunbar7e0c1952009-11-21 09:17:15 +00003137 Out << 'L';
3138 // References to external entities use the mangled name; if the name would
3139 // not normally be manged then mangle it as unqualified.
3140 //
3141 // FIXME: The ABI specifies that external names here should have _Z, but
3142 // gcc leaves this off.
Rafael Espindolad9800722010-03-11 14:07:00 +00003143 if (compensateMangling)
3144 mangle(D, "_Z");
3145 else
3146 mangle(D, "Z");
Daniel Dunbar7e0c1952009-11-21 09:17:15 +00003147 Out << 'E';
Rafael Espindolad9800722010-03-11 14:07:00 +00003148
3149 if (compensateMangling)
3150 Out << 'E';
3151
Daniel Dunbar7e0c1952009-11-21 09:17:15 +00003152 break;
3153 }
Eli Friedmand7a6b162012-09-26 02:36:12 +00003154 case TemplateArgument::NullPtr: {
3155 // <expr-primary> ::= L <type> 0 E
3156 Out << 'L';
3157 mangleType(A.getNullPtrType());
3158 Out << "0E";
3159 break;
3160 }
Douglas Gregorf90b27a2011-01-03 22:36:02 +00003161 case TemplateArgument::Pack: {
3162 // Note: proposal by Mike Herrick on 12/20/10
3163 Out << 'J';
3164 for (TemplateArgument::pack_iterator PA = A.pack_begin(),
3165 PAEnd = A.pack_end();
3166 PA != PAEnd; ++PA)
Eli Friedmand7a6b162012-09-26 02:36:12 +00003167 mangleTemplateArg(*PA);
Douglas Gregorf90b27a2011-01-03 22:36:02 +00003168 Out << 'E';
3169 }
Daniel Dunbar7e0c1952009-11-21 09:17:15 +00003170 }
Anders Carlsson7a0ba872009-05-15 16:09:15 +00003171}
3172
Anders Carlsson0ccdf8d2009-09-27 00:38:53 +00003173void CXXNameMangler::mangleTemplateParameter(unsigned Index) {
3174 // <template-param> ::= T_ # first template parameter
3175 // ::= T <parameter-2 non-negative number> _
3176 if (Index == 0)
3177 Out << "T_";
3178 else
3179 Out << 'T' << (Index - 1) << '_';
3180}
3181
John McCall68a51a72011-07-01 00:04:39 +00003182void CXXNameMangler::mangleExistingSubstitution(QualType type) {
3183 bool result = mangleSubstitution(type);
3184 assert(result && "no existing substitution for type");
3185 (void) result;
3186}
3187
3188void CXXNameMangler::mangleExistingSubstitution(TemplateName tname) {
3189 bool result = mangleSubstitution(tname);
3190 assert(result && "no existing substitution for template name");
3191 (void) result;
3192}
3193
Anders Carlsson76967372009-09-17 00:43:46 +00003194// <substitution> ::= S <seq-id> _
3195// ::= S_
Anders Carlsson6862fc72009-09-17 04:16:28 +00003196bool CXXNameMangler::mangleSubstitution(const NamedDecl *ND) {
Anders Carlssone7c8cb62009-09-26 20:53:44 +00003197 // Try one of the standard substitutions first.
3198 if (mangleStandardSubstitution(ND))
3199 return true;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00003200
Anders Carlsson433d1372009-11-07 04:26:04 +00003201 ND = cast<NamedDecl>(ND->getCanonicalDecl());
Anders Carlsson6862fc72009-09-17 04:16:28 +00003202 return mangleSubstitution(reinterpret_cast<uintptr_t>(ND));
3203}
3204
Douglas Gregor14795c82011-12-03 18:24:43 +00003205/// \brief Determine whether the given type has any qualifiers that are
3206/// relevant for substitutions.
3207static bool hasMangledSubstitutionQualifiers(QualType T) {
3208 Qualifiers Qs = T.getQualifiers();
3209 return Qs.getCVRQualifiers() || Qs.hasAddressSpace();
3210}
3211
Anders Carlsson76967372009-09-17 00:43:46 +00003212bool CXXNameMangler::mangleSubstitution(QualType T) {
Douglas Gregor14795c82011-12-03 18:24:43 +00003213 if (!hasMangledSubstitutionQualifiers(T)) {
Anders Carlssond99edc42009-09-26 03:55:37 +00003214 if (const RecordType *RT = T->getAs<RecordType>())
3215 return mangleSubstitution(RT->getDecl());
3216 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00003217
Anders Carlsson76967372009-09-17 00:43:46 +00003218 uintptr_t TypePtr = reinterpret_cast<uintptr_t>(T.getAsOpaquePtr());
3219
Anders Carlssond3a932a2009-09-17 03:53:28 +00003220 return mangleSubstitution(TypePtr);
3221}
3222
Douglas Gregor1e9268e2010-04-28 05:58:56 +00003223bool CXXNameMangler::mangleSubstitution(TemplateName Template) {
3224 if (TemplateDecl *TD = Template.getAsTemplateDecl())
3225 return mangleSubstitution(TD);
Sean Huntc3021132010-05-05 15:23:54 +00003226
Douglas Gregor1e9268e2010-04-28 05:58:56 +00003227 Template = Context.getASTContext().getCanonicalTemplateName(Template);
3228 return mangleSubstitution(
3229 reinterpret_cast<uintptr_t>(Template.getAsVoidPointer()));
3230}
3231
Anders Carlssond3a932a2009-09-17 03:53:28 +00003232bool CXXNameMangler::mangleSubstitution(uintptr_t Ptr) {
Benjamin Kramer35f59b62010-04-10 16:03:31 +00003233 llvm::DenseMap<uintptr_t, unsigned>::iterator I = Substitutions.find(Ptr);
Anders Carlsson76967372009-09-17 00:43:46 +00003234 if (I == Substitutions.end())
3235 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00003236
Anders Carlsson76967372009-09-17 00:43:46 +00003237 unsigned SeqID = I->second;
3238 if (SeqID == 0)
3239 Out << "S_";
3240 else {
3241 SeqID--;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00003242
Anders Carlsson76967372009-09-17 00:43:46 +00003243 // <seq-id> is encoded in base-36, using digits and upper case letters.
3244 char Buffer[10];
Benjamin Kramer35f59b62010-04-10 16:03:31 +00003245 char *BufferPtr = llvm::array_endof(Buffer);
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00003246
Anders Carlsson76967372009-09-17 00:43:46 +00003247 if (SeqID == 0) *--BufferPtr = '0';
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00003248
Anders Carlsson76967372009-09-17 00:43:46 +00003249 while (SeqID) {
3250 assert(BufferPtr > Buffer && "Buffer overflow!");
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00003251
John McCall6ab30e02010-06-09 07:26:17 +00003252 char c = static_cast<char>(SeqID % 36);
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00003253
Anders Carlsson76967372009-09-17 00:43:46 +00003254 *--BufferPtr = (c < 10 ? '0' + c : 'A' + c - 10);
3255 SeqID /= 36;
3256 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00003257
Benjamin Kramer35f59b62010-04-10 16:03:31 +00003258 Out << 'S'
Chris Lattner5f9e2722011-07-23 10:55:15 +00003259 << StringRef(BufferPtr, llvm::array_endof(Buffer)-BufferPtr)
Benjamin Kramer35f59b62010-04-10 16:03:31 +00003260 << '_';
Anders Carlsson76967372009-09-17 00:43:46 +00003261 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00003262
Anders Carlsson76967372009-09-17 00:43:46 +00003263 return true;
3264}
3265
Anders Carlssonf514b542009-09-27 00:12:57 +00003266static bool isCharType(QualType T) {
3267 if (T.isNull())
3268 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00003269
Anders Carlssonf514b542009-09-27 00:12:57 +00003270 return T->isSpecificBuiltinType(BuiltinType::Char_S) ||
3271 T->isSpecificBuiltinType(BuiltinType::Char_U);
3272}
3273
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00003274/// isCharSpecialization - Returns whether a given type is a template
Anders Carlssonf514b542009-09-27 00:12:57 +00003275/// specialization of a given name with a single argument of type char.
3276static bool isCharSpecialization(QualType T, const char *Name) {
3277 if (T.isNull())
3278 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00003279
Anders Carlssonf514b542009-09-27 00:12:57 +00003280 const RecordType *RT = T->getAs<RecordType>();
3281 if (!RT)
3282 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00003283
3284 const ClassTemplateSpecializationDecl *SD =
Anders Carlssonf514b542009-09-27 00:12:57 +00003285 dyn_cast<ClassTemplateSpecializationDecl>(RT->getDecl());
3286 if (!SD)
3287 return false;
3288
Douglas Gregorccc1b5e2012-02-21 00:37:24 +00003289 if (!isStdNamespace(getEffectiveDeclContext(SD)))
Anders Carlssonf514b542009-09-27 00:12:57 +00003290 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00003291
Anders Carlssonf514b542009-09-27 00:12:57 +00003292 const TemplateArgumentList &TemplateArgs = SD->getTemplateArgs();
3293 if (TemplateArgs.size() != 1)
3294 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00003295
Anders Carlssonf514b542009-09-27 00:12:57 +00003296 if (!isCharType(TemplateArgs[0].getAsType()))
3297 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00003298
Daniel Dunbar01eb9b92009-10-18 21:17:35 +00003299 return SD->getIdentifier()->getName() == Name;
Anders Carlssonf514b542009-09-27 00:12:57 +00003300}
3301
Anders Carlsson91f88602009-12-07 19:56:42 +00003302template <std::size_t StrLen>
Benjamin Kramer54353f42010-11-25 18:29:30 +00003303static bool isStreamCharSpecialization(const ClassTemplateSpecializationDecl*SD,
3304 const char (&Str)[StrLen]) {
Anders Carlsson91f88602009-12-07 19:56:42 +00003305 if (!SD->getIdentifier()->isStr(Str))
3306 return false;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00003307
Anders Carlsson91f88602009-12-07 19:56:42 +00003308 const TemplateArgumentList &TemplateArgs = SD->getTemplateArgs();
3309 if (TemplateArgs.size() != 2)
3310 return false;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00003311
Anders Carlsson91f88602009-12-07 19:56:42 +00003312 if (!isCharType(TemplateArgs[0].getAsType()))
3313 return false;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00003314
Anders Carlsson91f88602009-12-07 19:56:42 +00003315 if (!isCharSpecialization(TemplateArgs[1].getAsType(), "char_traits"))
3316 return false;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00003317
Anders Carlsson91f88602009-12-07 19:56:42 +00003318 return true;
3319}
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00003320
Anders Carlssone7c8cb62009-09-26 20:53:44 +00003321bool CXXNameMangler::mangleStandardSubstitution(const NamedDecl *ND) {
3322 // <substitution> ::= St # ::std::
Anders Carlsson8c031552009-09-26 23:10:05 +00003323 if (const NamespaceDecl *NS = dyn_cast<NamespaceDecl>(ND)) {
Anders Carlsson47846d22009-12-04 06:23:23 +00003324 if (isStd(NS)) {
Anders Carlsson8c031552009-09-26 23:10:05 +00003325 Out << "St";
3326 return true;
3327 }
3328 }
3329
3330 if (const ClassTemplateDecl *TD = dyn_cast<ClassTemplateDecl>(ND)) {
Douglas Gregorccc1b5e2012-02-21 00:37:24 +00003331 if (!isStdNamespace(getEffectiveDeclContext(TD)))
Anders Carlsson8c031552009-09-26 23:10:05 +00003332 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00003333
Anders Carlsson8c031552009-09-26 23:10:05 +00003334 // <substitution> ::= Sa # ::std::allocator
3335 if (TD->getIdentifier()->isStr("allocator")) {
3336 Out << "Sa";
3337 return true;
3338 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00003339
Anders Carlsson189d59c2009-09-26 23:14:39 +00003340 // <<substitution> ::= Sb # ::std::basic_string
3341 if (TD->getIdentifier()->isStr("basic_string")) {
3342 Out << "Sb";
3343 return true;
3344 }
Anders Carlsson8c031552009-09-26 23:10:05 +00003345 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00003346
3347 if (const ClassTemplateSpecializationDecl *SD =
Anders Carlssonf514b542009-09-27 00:12:57 +00003348 dyn_cast<ClassTemplateSpecializationDecl>(ND)) {
Douglas Gregorccc1b5e2012-02-21 00:37:24 +00003349 if (!isStdNamespace(getEffectiveDeclContext(SD)))
Eli Friedman5370ee22010-02-23 18:25:09 +00003350 return false;
3351
Anders Carlssonf514b542009-09-27 00:12:57 +00003352 // <substitution> ::= Ss # ::std::basic_string<char,
3353 // ::std::char_traits<char>,
3354 // ::std::allocator<char> >
3355 if (SD->getIdentifier()->isStr("basic_string")) {
3356 const TemplateArgumentList &TemplateArgs = SD->getTemplateArgs();
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00003357
Anders Carlssonf514b542009-09-27 00:12:57 +00003358 if (TemplateArgs.size() != 3)
3359 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00003360
Anders Carlssonf514b542009-09-27 00:12:57 +00003361 if (!isCharType(TemplateArgs[0].getAsType()))
3362 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00003363
Anders Carlssonf514b542009-09-27 00:12:57 +00003364 if (!isCharSpecialization(TemplateArgs[1].getAsType(), "char_traits"))
3365 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00003366
Anders Carlssonf514b542009-09-27 00:12:57 +00003367 if (!isCharSpecialization(TemplateArgs[2].getAsType(), "allocator"))
3368 return false;
3369
3370 Out << "Ss";
3371 return true;
3372 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00003373
Anders Carlsson91f88602009-12-07 19:56:42 +00003374 // <substitution> ::= Si # ::std::basic_istream<char,
3375 // ::std::char_traits<char> >
3376 if (isStreamCharSpecialization(SD, "basic_istream")) {
3377 Out << "Si";
3378 return true;
3379 }
3380
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00003381 // <substitution> ::= So # ::std::basic_ostream<char,
Anders Carlsson8f8fd8e2009-10-08 17:20:26 +00003382 // ::std::char_traits<char> >
Anders Carlsson91f88602009-12-07 19:56:42 +00003383 if (isStreamCharSpecialization(SD, "basic_ostream")) {
Anders Carlsson8f8fd8e2009-10-08 17:20:26 +00003384 Out << "So";
3385 return true;
3386 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00003387
Anders Carlsson91f88602009-12-07 19:56:42 +00003388 // <substitution> ::= Sd # ::std::basic_iostream<char,
3389 // ::std::char_traits<char> >
3390 if (isStreamCharSpecialization(SD, "basic_iostream")) {
3391 Out << "Sd";
3392 return true;
3393 }
Anders Carlssonf514b542009-09-27 00:12:57 +00003394 }
Anders Carlsson8c031552009-09-26 23:10:05 +00003395 return false;
Anders Carlssone7c8cb62009-09-26 20:53:44 +00003396}
3397
Anders Carlsson76967372009-09-17 00:43:46 +00003398void CXXNameMangler::addSubstitution(QualType T) {
Douglas Gregor14795c82011-12-03 18:24:43 +00003399 if (!hasMangledSubstitutionQualifiers(T)) {
Anders Carlssond99edc42009-09-26 03:55:37 +00003400 if (const RecordType *RT = T->getAs<RecordType>()) {
3401 addSubstitution(RT->getDecl());
3402 return;
3403 }
3404 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00003405
Anders Carlsson76967372009-09-17 00:43:46 +00003406 uintptr_t TypePtr = reinterpret_cast<uintptr_t>(T.getAsOpaquePtr());
Anders Carlssond3a932a2009-09-17 03:53:28 +00003407 addSubstitution(TypePtr);
3408}
3409
Douglas Gregor1e9268e2010-04-28 05:58:56 +00003410void CXXNameMangler::addSubstitution(TemplateName Template) {
3411 if (TemplateDecl *TD = Template.getAsTemplateDecl())
3412 return addSubstitution(TD);
Sean Huntc3021132010-05-05 15:23:54 +00003413
Douglas Gregor1e9268e2010-04-28 05:58:56 +00003414 Template = Context.getASTContext().getCanonicalTemplateName(Template);
3415 addSubstitution(reinterpret_cast<uintptr_t>(Template.getAsVoidPointer()));
3416}
3417
Anders Carlssond3a932a2009-09-17 03:53:28 +00003418void CXXNameMangler::addSubstitution(uintptr_t Ptr) {
Anders Carlssond3a932a2009-09-17 03:53:28 +00003419 assert(!Substitutions.count(Ptr) && "Substitution already exists!");
Anders Carlsson9d85b722010-06-02 04:29:50 +00003420 Substitutions[Ptr] = SeqID++;
Anders Carlsson76967372009-09-17 00:43:46 +00003421}
3422
Daniel Dunbar1b077112009-11-21 09:06:10 +00003423//
Mike Stump1eb44332009-09-09 15:08:12 +00003424
Daniel Dunbar1b077112009-11-21 09:06:10 +00003425/// \brief Mangles the name of the declaration D and emits that name to the
3426/// given output stream.
3427///
3428/// If the declaration D requires a mangled name, this routine will emit that
3429/// mangled name to \p os and return true. Otherwise, \p os will be unchanged
3430/// and this routine will return false. In this case, the caller should just
3431/// emit the identifier of the declaration (\c D->getIdentifier()) as its
3432/// name.
Peter Collingbourne14110472011-01-13 18:57:25 +00003433void ItaniumMangleContext::mangleName(const NamedDecl *D,
Chris Lattner5f9e2722011-07-23 10:55:15 +00003434 raw_ostream &Out) {
Daniel Dunbarc02ab4c2009-11-21 09:14:44 +00003435 assert((isa<FunctionDecl>(D) || isa<VarDecl>(D)) &&
3436 "Invalid mangleName() call, argument is not a variable or function!");
3437 assert(!isa<CXXConstructorDecl>(D) && !isa<CXXDestructorDecl>(D) &&
3438 "Invalid mangleName() call on 'structor decl!");
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00003439
Daniel Dunbar1b077112009-11-21 09:06:10 +00003440 PrettyStackTraceDecl CrashInfo(D, SourceLocation(),
3441 getASTContext().getSourceManager(),
3442 "Mangling declaration");
Mike Stump1eb44332009-09-09 15:08:12 +00003443
John McCallfb44de92011-05-01 22:35:37 +00003444 CXXNameMangler Mangler(*this, Out, D);
Daniel Dunbar94fd26d2009-11-21 09:06:22 +00003445 return Mangler.mangle(D);
Daniel Dunbar1b077112009-11-21 09:06:10 +00003446}
Mike Stump1eb44332009-09-09 15:08:12 +00003447
Peter Collingbourne14110472011-01-13 18:57:25 +00003448void ItaniumMangleContext::mangleCXXCtor(const CXXConstructorDecl *D,
3449 CXXCtorType Type,
Chris Lattner5f9e2722011-07-23 10:55:15 +00003450 raw_ostream &Out) {
Rafael Espindolac4850c22011-02-10 23:59:36 +00003451 CXXNameMangler Mangler(*this, Out, D, Type);
Daniel Dunbar77939c92009-11-21 09:06:31 +00003452 Mangler.mangle(D);
Daniel Dunbar1b077112009-11-21 09:06:10 +00003453}
Mike Stump1eb44332009-09-09 15:08:12 +00003454
Peter Collingbourne14110472011-01-13 18:57:25 +00003455void ItaniumMangleContext::mangleCXXDtor(const CXXDestructorDecl *D,
3456 CXXDtorType Type,
Chris Lattner5f9e2722011-07-23 10:55:15 +00003457 raw_ostream &Out) {
Rafael Espindolac4850c22011-02-10 23:59:36 +00003458 CXXNameMangler Mangler(*this, Out, D, Type);
Daniel Dunbar77939c92009-11-21 09:06:31 +00003459 Mangler.mangle(D);
Daniel Dunbar1b077112009-11-21 09:06:10 +00003460}
Mike Stumpf1216772009-07-31 18:25:34 +00003461
Peter Collingbourne14110472011-01-13 18:57:25 +00003462void ItaniumMangleContext::mangleThunk(const CXXMethodDecl *MD,
3463 const ThunkInfo &Thunk,
Chris Lattner5f9e2722011-07-23 10:55:15 +00003464 raw_ostream &Out) {
Anders Carlsson19879c92010-03-23 17:17:29 +00003465 // <special-name> ::= T <call-offset> <base encoding>
3466 // # base is the nominal target function of thunk
3467 // <special-name> ::= Tc <call-offset> <call-offset> <base encoding>
3468 // # base is the nominal target function of thunk
3469 // # first call-offset is 'this' adjustment
3470 // # second call-offset is result adjustment
Sean Huntc3021132010-05-05 15:23:54 +00003471
Anders Carlsson19879c92010-03-23 17:17:29 +00003472 assert(!isa<CXXDestructorDecl>(MD) &&
3473 "Use mangleCXXDtor for destructor decls!");
Rafael Espindolac4850c22011-02-10 23:59:36 +00003474 CXXNameMangler Mangler(*this, Out);
Anders Carlsson19879c92010-03-23 17:17:29 +00003475 Mangler.getStream() << "_ZT";
3476 if (!Thunk.Return.isEmpty())
3477 Mangler.getStream() << 'c';
Sean Huntc3021132010-05-05 15:23:54 +00003478
Anders Carlsson19879c92010-03-23 17:17:29 +00003479 // Mangle the 'this' pointer adjustment.
3480 Mangler.mangleCallOffset(Thunk.This.NonVirtual, Thunk.This.VCallOffsetOffset);
Sean Huntc3021132010-05-05 15:23:54 +00003481
Anders Carlsson19879c92010-03-23 17:17:29 +00003482 // Mangle the return pointer adjustment if there is one.
3483 if (!Thunk.Return.isEmpty())
3484 Mangler.mangleCallOffset(Thunk.Return.NonVirtual,
3485 Thunk.Return.VBaseOffsetOffset);
Sean Huntc3021132010-05-05 15:23:54 +00003486
Anders Carlsson19879c92010-03-23 17:17:29 +00003487 Mangler.mangleFunctionEncoding(MD);
3488}
3489
Sean Huntc3021132010-05-05 15:23:54 +00003490void
Peter Collingbourne14110472011-01-13 18:57:25 +00003491ItaniumMangleContext::mangleCXXDtorThunk(const CXXDestructorDecl *DD,
3492 CXXDtorType Type,
3493 const ThisAdjustment &ThisAdjustment,
Chris Lattner5f9e2722011-07-23 10:55:15 +00003494 raw_ostream &Out) {
Anders Carlsson19879c92010-03-23 17:17:29 +00003495 // <special-name> ::= T <call-offset> <base encoding>
3496 // # base is the nominal target function of thunk
Rafael Espindolac4850c22011-02-10 23:59:36 +00003497 CXXNameMangler Mangler(*this, Out, DD, Type);
Anders Carlsson19879c92010-03-23 17:17:29 +00003498 Mangler.getStream() << "_ZT";
3499
3500 // Mangle the 'this' pointer adjustment.
Sean Huntc3021132010-05-05 15:23:54 +00003501 Mangler.mangleCallOffset(ThisAdjustment.NonVirtual,
Anders Carlsson19879c92010-03-23 17:17:29 +00003502 ThisAdjustment.VCallOffsetOffset);
3503
3504 Mangler.mangleFunctionEncoding(DD);
3505}
3506
Daniel Dunbarc0747712009-11-21 09:12:13 +00003507/// mangleGuardVariable - Returns the mangled name for a guard variable
3508/// for the passed in VarDecl.
Peter Collingbourne14110472011-01-13 18:57:25 +00003509void ItaniumMangleContext::mangleItaniumGuardVariable(const VarDecl *D,
Chris Lattner5f9e2722011-07-23 10:55:15 +00003510 raw_ostream &Out) {
Daniel Dunbarc0747712009-11-21 09:12:13 +00003511 // <special-name> ::= GV <object name> # Guard variable for one-time
3512 // # initialization
Rafael Espindolac4850c22011-02-10 23:59:36 +00003513 CXXNameMangler Mangler(*this, Out);
Daniel Dunbarc0747712009-11-21 09:12:13 +00003514 Mangler.getStream() << "_ZGV";
3515 Mangler.mangleName(D);
3516}
3517
Peter Collingbourne14110472011-01-13 18:57:25 +00003518void ItaniumMangleContext::mangleReferenceTemporary(const VarDecl *D,
Chris Lattner5f9e2722011-07-23 10:55:15 +00003519 raw_ostream &Out) {
Anders Carlsson715edf22010-06-26 16:09:40 +00003520 // We match the GCC mangling here.
3521 // <special-name> ::= GR <object name>
Rafael Espindolac4850c22011-02-10 23:59:36 +00003522 CXXNameMangler Mangler(*this, Out);
Anders Carlsson715edf22010-06-26 16:09:40 +00003523 Mangler.getStream() << "_ZGR";
3524 Mangler.mangleName(D);
3525}
3526
Peter Collingbourne14110472011-01-13 18:57:25 +00003527void ItaniumMangleContext::mangleCXXVTable(const CXXRecordDecl *RD,
Chris Lattner5f9e2722011-07-23 10:55:15 +00003528 raw_ostream &Out) {
Daniel Dunbarc0747712009-11-21 09:12:13 +00003529 // <special-name> ::= TV <type> # virtual table
Rafael Espindolac4850c22011-02-10 23:59:36 +00003530 CXXNameMangler Mangler(*this, Out);
Daniel Dunbarc0747712009-11-21 09:12:13 +00003531 Mangler.getStream() << "_ZTV";
Douglas Gregor1b12a3b2010-05-26 05:11:13 +00003532 Mangler.mangleNameOrStandardSubstitution(RD);
Daniel Dunbar1b077112009-11-21 09:06:10 +00003533}
Mike Stump82d75b02009-11-10 01:58:37 +00003534
Peter Collingbourne14110472011-01-13 18:57:25 +00003535void ItaniumMangleContext::mangleCXXVTT(const CXXRecordDecl *RD,
Chris Lattner5f9e2722011-07-23 10:55:15 +00003536 raw_ostream &Out) {
Daniel Dunbarc0747712009-11-21 09:12:13 +00003537 // <special-name> ::= TT <type> # VTT structure
Rafael Espindolac4850c22011-02-10 23:59:36 +00003538 CXXNameMangler Mangler(*this, Out);
Daniel Dunbarc0747712009-11-21 09:12:13 +00003539 Mangler.getStream() << "_ZTT";
Douglas Gregor1b12a3b2010-05-26 05:11:13 +00003540 Mangler.mangleNameOrStandardSubstitution(RD);
Daniel Dunbar1b077112009-11-21 09:06:10 +00003541}
Mike Stumpab3f7e92009-11-10 01:41:59 +00003542
Peter Collingbourne14110472011-01-13 18:57:25 +00003543void ItaniumMangleContext::mangleCXXCtorVTable(const CXXRecordDecl *RD,
3544 int64_t Offset,
3545 const CXXRecordDecl *Type,
Chris Lattner5f9e2722011-07-23 10:55:15 +00003546 raw_ostream &Out) {
Daniel Dunbarc0747712009-11-21 09:12:13 +00003547 // <special-name> ::= TC <type> <offset number> _ <base type>
Rafael Espindolac4850c22011-02-10 23:59:36 +00003548 CXXNameMangler Mangler(*this, Out);
Daniel Dunbarc0747712009-11-21 09:12:13 +00003549 Mangler.getStream() << "_ZTC";
Douglas Gregor1b12a3b2010-05-26 05:11:13 +00003550 Mangler.mangleNameOrStandardSubstitution(RD);
Daniel Dunbarc0747712009-11-21 09:12:13 +00003551 Mangler.getStream() << Offset;
Benjamin Kramer35f59b62010-04-10 16:03:31 +00003552 Mangler.getStream() << '_';
Douglas Gregor1b12a3b2010-05-26 05:11:13 +00003553 Mangler.mangleNameOrStandardSubstitution(Type);
Daniel Dunbar1b077112009-11-21 09:06:10 +00003554}
Mike Stump738f8c22009-07-31 23:15:31 +00003555
Peter Collingbourne14110472011-01-13 18:57:25 +00003556void ItaniumMangleContext::mangleCXXRTTI(QualType Ty,
Chris Lattner5f9e2722011-07-23 10:55:15 +00003557 raw_ostream &Out) {
Daniel Dunbarc0747712009-11-21 09:12:13 +00003558 // <special-name> ::= TI <type> # typeinfo structure
Douglas Gregor154fe982009-12-23 22:04:40 +00003559 assert(!Ty.hasQualifiers() && "RTTI info cannot have top-level qualifiers");
Rafael Espindolac4850c22011-02-10 23:59:36 +00003560 CXXNameMangler Mangler(*this, Out);
Daniel Dunbarc0747712009-11-21 09:12:13 +00003561 Mangler.getStream() << "_ZTI";
3562 Mangler.mangleType(Ty);
Daniel Dunbar1b077112009-11-21 09:06:10 +00003563}
Mike Stump67795982009-11-14 00:14:13 +00003564
Peter Collingbourne14110472011-01-13 18:57:25 +00003565void ItaniumMangleContext::mangleCXXRTTIName(QualType Ty,
Chris Lattner5f9e2722011-07-23 10:55:15 +00003566 raw_ostream &Out) {
Daniel Dunbarc0747712009-11-21 09:12:13 +00003567 // <special-name> ::= TS <type> # typeinfo name (null terminated byte string)
Rafael Espindolac4850c22011-02-10 23:59:36 +00003568 CXXNameMangler Mangler(*this, Out);
Daniel Dunbarc0747712009-11-21 09:12:13 +00003569 Mangler.getStream() << "_ZTS";
3570 Mangler.mangleType(Ty);
Mike Stumpf1216772009-07-31 18:25:34 +00003571}
Peter Collingbourne14110472011-01-13 18:57:25 +00003572
3573MangleContext *clang::createItaniumMangleContext(ASTContext &Context,
David Blaikied6471f72011-09-25 23:23:43 +00003574 DiagnosticsEngine &Diags) {
Peter Collingbourne14110472011-01-13 18:57:25 +00003575 return new ItaniumMangleContext(Context, Diags);
3576}