blob: 9f30f6e63a7d30170f4cf6de71f1fbefaf8edcab [file] [log] [blame]
Peter Collingbourne0ff0b372011-01-13 18:57:25 +00001//===--- ItaniumMangle.cpp - Itanium C++ Name Mangling ----------*- C++ -*-===//
Douglas Gregor5fec5b02009-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 Collingbourne0ff0b372011-01-13 18:57:25 +000017#include "clang/AST/Mangle.h"
Douglas Gregor5fec5b02009-02-13 00:10:09 +000018#include "clang/AST/ASTContext.h"
19#include "clang/AST/Decl.h"
20#include "clang/AST/DeclCXX.h"
Anders Carlsson16d5d292009-03-07 22:03:21 +000021#include "clang/AST/DeclObjC.h"
Anders Carlssonf6e9ece2009-05-15 16:09:15 +000022#include "clang/AST/DeclTemplate.h"
Anders Carlsson02bca732009-09-27 20:11:34 +000023#include "clang/AST/ExprCXX.h"
John McCall31168b02011-06-15 23:02:42 +000024#include "clang/AST/ExprObjC.h"
John McCall8fb0d9d2011-05-01 22:35:37 +000025#include "clang/AST/TypeLoc.h"
Peter Collingbourne0ff0b372011-01-13 18:57:25 +000026#include "clang/Basic/ABI.h"
Douglas Gregor5f361c92009-02-18 23:53:56 +000027#include "clang/Basic/SourceManager.h"
Rafael Espindolad62acb52011-02-15 22:23:51 +000028#include "clang/Basic/TargetInfo.h"
Anders Carlsson1e39bd92009-10-07 01:45:02 +000029#include "llvm/ADT/StringExtras.h"
Douglas Gregor5fec5b02009-02-13 00:10:09 +000030#include "llvm/Support/raw_ostream.h"
John McCallcc5e23c2009-09-05 07:56:18 +000031#include "llvm/Support/ErrorHandling.h"
Anders Carlssonc6eec402010-02-05 07:31:37 +000032
33#define MANGLE_CHECKER 0
34
35#if MANGLE_CHECKER
36#include <cxxabi.h>
37#endif
38
Douglas Gregor5fec5b02009-02-13 00:10:09 +000039using namespace clang;
Charles Davisc9e78142010-05-26 18:25:27 +000040
Douglas Gregor5fec5b02009-02-13 00:10:09 +000041namespace {
Fariborz Jahaniana529c252010-03-03 19:41:08 +000042
John McCall18e4eda2010-10-18 21:28:44 +000043static const CXXRecordDecl *GetLocalClassDecl(const NamedDecl *ND) {
44 const DeclContext *DC = dyn_cast<DeclContext>(ND);
45 if (!DC)
46 DC = ND->getDeclContext();
47 while (!DC->isNamespace() && !DC->isTranslationUnit()) {
48 if (isa<FunctionDecl>(DC->getParent()))
49 return dyn_cast<CXXRecordDecl>(DC);
50 DC = DC->getParent();
Fariborz Jahaniana529c252010-03-03 19:41:08 +000051 }
52 return 0;
53}
54
John McCall8fb0d9d2011-05-01 22:35:37 +000055static const FunctionDecl *getStructor(const FunctionDecl *fn) {
56 if (const FunctionTemplateDecl *ftd = fn->getPrimaryTemplate())
57 return ftd->getTemplatedDecl();
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +000058
John McCall8fb0d9d2011-05-01 22:35:37 +000059 return fn;
60}
Anders Carlssonbd975482009-11-24 05:36:32 +000061
John McCall8fb0d9d2011-05-01 22:35:37 +000062static const NamedDecl *getStructor(const NamedDecl *decl) {
63 const FunctionDecl *fn = dyn_cast_or_null<FunctionDecl>(decl);
64 return (fn ? getStructor(fn) : decl);
Anders Carlssonbd975482009-11-24 05:36:32 +000065}
John McCall09de8ec2010-02-04 01:42:13 +000066
67static const unsigned UnknownArity = ~0U;
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +000068
Peter Collingbourne0ff0b372011-01-13 18:57:25 +000069class ItaniumMangleContext : public MangleContext {
70 llvm::DenseMap<const TagDecl *, uint64_t> AnonStructIds;
71 unsigned Discriminator;
72 llvm::DenseMap<const NamedDecl*, unsigned> Uniquifier;
73
74public:
75 explicit ItaniumMangleContext(ASTContext &Context,
76 Diagnostic &Diags)
77 : MangleContext(Context, Diags) { }
78
79 uint64_t getAnonymousStructId(const TagDecl *TD) {
80 std::pair<llvm::DenseMap<const TagDecl *,
81 uint64_t>::iterator, bool> Result =
82 AnonStructIds.insert(std::make_pair(TD, AnonStructIds.size()));
83 return Result.first->second;
84 }
85
86 void startNewFunction() {
87 MangleContext::startNewFunction();
88 mangleInitDiscriminator();
89 }
90
91 /// @name Mangler Entry Points
92 /// @{
93
94 bool shouldMangleDeclName(const NamedDecl *D);
Rafael Espindolaa759d722011-02-11 01:41:00 +000095 void mangleName(const NamedDecl *D, llvm::raw_ostream &);
Peter Collingbourne0ff0b372011-01-13 18:57:25 +000096 void mangleThunk(const CXXMethodDecl *MD,
97 const ThunkInfo &Thunk,
Rafael Espindola3968cd02011-02-11 02:52:17 +000098 llvm::raw_ostream &);
Peter Collingbourne0ff0b372011-01-13 18:57:25 +000099 void mangleCXXDtorThunk(const CXXDestructorDecl *DD, CXXDtorType Type,
100 const ThisAdjustment &ThisAdjustment,
Rafael Espindola3968cd02011-02-11 02:52:17 +0000101 llvm::raw_ostream &);
Peter Collingbourne0ff0b372011-01-13 18:57:25 +0000102 void mangleReferenceTemporary(const VarDecl *D,
Rafael Espindola3968cd02011-02-11 02:52:17 +0000103 llvm::raw_ostream &);
Peter Collingbourne0ff0b372011-01-13 18:57:25 +0000104 void mangleCXXVTable(const CXXRecordDecl *RD,
Rafael Espindola3968cd02011-02-11 02:52:17 +0000105 llvm::raw_ostream &);
Peter Collingbourne0ff0b372011-01-13 18:57:25 +0000106 void mangleCXXVTT(const CXXRecordDecl *RD,
Rafael Espindola3968cd02011-02-11 02:52:17 +0000107 llvm::raw_ostream &);
Peter Collingbourne0ff0b372011-01-13 18:57:25 +0000108 void mangleCXXCtorVTable(const CXXRecordDecl *RD, int64_t Offset,
109 const CXXRecordDecl *Type,
Rafael Espindola3968cd02011-02-11 02:52:17 +0000110 llvm::raw_ostream &);
111 void mangleCXXRTTI(QualType T, llvm::raw_ostream &);
112 void mangleCXXRTTIName(QualType T, llvm::raw_ostream &);
Peter Collingbourne0ff0b372011-01-13 18:57:25 +0000113 void mangleCXXCtor(const CXXConstructorDecl *D, CXXCtorType Type,
Rafael Espindolaa759d722011-02-11 01:41:00 +0000114 llvm::raw_ostream &);
Peter Collingbourne0ff0b372011-01-13 18:57:25 +0000115 void mangleCXXDtor(const CXXDestructorDecl *D, CXXDtorType Type,
Rafael Espindolaa759d722011-02-11 01:41:00 +0000116 llvm::raw_ostream &);
Peter Collingbourne0ff0b372011-01-13 18:57:25 +0000117
Rafael Espindola3968cd02011-02-11 02:52:17 +0000118 void mangleItaniumGuardVariable(const VarDecl *D, llvm::raw_ostream &);
Peter Collingbourne0ff0b372011-01-13 18:57:25 +0000119
120 void mangleInitDiscriminator() {
121 Discriminator = 0;
122 }
123
124 bool getNextDiscriminator(const NamedDecl *ND, unsigned &disc) {
125 unsigned &discriminator = Uniquifier[ND];
126 if (!discriminator)
127 discriminator = ++Discriminator;
128 if (discriminator == 1)
129 return false;
130 disc = discriminator-2;
131 return true;
132 }
133 /// @}
134};
135
Daniel Dunbaref5d75a2009-11-21 09:06:10 +0000136/// CXXNameMangler - Manage the mangling of a single name.
Daniel Dunbar8483d212009-11-21 09:12:13 +0000137class CXXNameMangler {
Peter Collingbourne0ff0b372011-01-13 18:57:25 +0000138 ItaniumMangleContext &Context;
Rafael Espindolaa759d722011-02-11 01:41:00 +0000139 llvm::raw_ostream &Out;
Douglas Gregor5fec5b02009-02-13 00:10:09 +0000140
John McCall8fb0d9d2011-05-01 22:35:37 +0000141 /// The "structor" is the top-level declaration being mangled, if
142 /// that's not a template specialization; otherwise it's the pattern
143 /// for that specialization.
144 const NamedDecl *Structor;
Daniel Dunbaref5d75a2009-11-21 09:06:10 +0000145 unsigned StructorType;
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +0000146
Anders Carlssond5639232010-06-02 04:29:50 +0000147 /// SeqID - The next subsitution sequence number.
148 unsigned SeqID;
149
John McCall8fb0d9d2011-05-01 22:35:37 +0000150 class FunctionTypeDepthState {
151 unsigned Bits;
152
153 enum { InResultTypeMask = 1 };
154
155 public:
156 FunctionTypeDepthState() : Bits(0) {}
157
158 /// The number of function types we're inside.
159 unsigned getDepth() const {
160 return Bits >> 1;
161 }
162
163 /// True if we're in the return type of the innermost function type.
164 bool isInResultType() const {
165 return Bits & InResultTypeMask;
166 }
167
168 FunctionTypeDepthState push() {
169 FunctionTypeDepthState tmp = *this;
170 Bits = (Bits & ~InResultTypeMask) + 2;
171 return tmp;
172 }
173
174 void enterResultType() {
175 Bits |= InResultTypeMask;
176 }
177
178 void leaveResultType() {
179 Bits &= ~InResultTypeMask;
180 }
181
182 void pop(FunctionTypeDepthState saved) {
183 assert(getDepth() == saved.getDepth() + 1);
184 Bits = saved.Bits;
185 }
186
187 } FunctionTypeDepth;
188
Daniel Dunbaref5d75a2009-11-21 09:06:10 +0000189 llvm::DenseMap<uintptr_t, unsigned> Substitutions;
Daniel Dunbard614e322009-11-21 09:05:47 +0000190
John McCall09de8ec2010-02-04 01:42:13 +0000191 ASTContext &getASTContext() const { return Context.getASTContext(); }
192
Daniel Dunbar8483d212009-11-21 09:12:13 +0000193public:
John McCall8fb0d9d2011-05-01 22:35:37 +0000194 CXXNameMangler(ItaniumMangleContext &C, llvm::raw_ostream &Out_,
195 const NamedDecl *D = 0)
196 : Context(C), Out(Out_), Structor(getStructor(D)), StructorType(0),
197 SeqID(0) {
198 // These can't be mangled without a ctor type or dtor type.
199 assert(!D || (!isa<CXXDestructorDecl>(D) &&
200 !isa<CXXConstructorDecl>(D)));
201 }
Rafael Espindolaa759d722011-02-11 01:41:00 +0000202 CXXNameMangler(ItaniumMangleContext &C, llvm::raw_ostream &Out_,
Daniel Dunbar4118ec52009-11-21 09:06:31 +0000203 const CXXConstructorDecl *D, CXXCtorType Type)
Rafael Espindolaac00f5d2011-02-10 23:59:36 +0000204 : Context(C), Out(Out_), Structor(getStructor(D)), StructorType(Type),
John McCall8fb0d9d2011-05-01 22:35:37 +0000205 SeqID(0) { }
Rafael Espindolaa759d722011-02-11 01:41:00 +0000206 CXXNameMangler(ItaniumMangleContext &C, llvm::raw_ostream &Out_,
Daniel Dunbar4118ec52009-11-21 09:06:31 +0000207 const CXXDestructorDecl *D, CXXDtorType Type)
Rafael Espindolaac00f5d2011-02-10 23:59:36 +0000208 : Context(C), Out(Out_), Structor(getStructor(D)), StructorType(Type),
John McCall8fb0d9d2011-05-01 22:35:37 +0000209 SeqID(0) { }
Douglas Gregor5fec5b02009-02-13 00:10:09 +0000210
Anders Carlssonc6eec402010-02-05 07:31:37 +0000211#if MANGLE_CHECKER
212 ~CXXNameMangler() {
213 if (Out.str()[0] == '\01')
214 return;
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +0000215
Anders Carlssonc6eec402010-02-05 07:31:37 +0000216 int status = 0;
217 char *result = abi::__cxa_demangle(Out.str().str().c_str(), 0, 0, &status);
218 assert(status == 0 && "Could not demangle mangled name!");
219 free(result);
220 }
221#endif
Rafael Espindolaa759d722011-02-11 01:41:00 +0000222 llvm::raw_ostream &getStream() { return Out; }
Daniel Dunbar8483d212009-11-21 09:12:13 +0000223
Daniel Dunbar88ad4c52009-11-21 09:17:15 +0000224 void mangle(const NamedDecl *D, llvm::StringRef Prefix = "_Z");
Anders Carlssoncd836f02010-03-23 17:17:29 +0000225 void mangleCallOffset(int64_t NonVirtual, int64_t Virtual);
John McCall05ddbb82010-07-14 04:20:34 +0000226 void mangleNumber(const llvm::APSInt &I);
Anders Carlssonc7785402009-11-26 02:32:05 +0000227 void mangleNumber(int64_t Number);
John McCall05ddbb82010-07-14 04:20:34 +0000228 void mangleFloat(const llvm::APFloat &F);
Daniel Dunbar8483d212009-11-21 09:12:13 +0000229 void mangleFunctionEncoding(const FunctionDecl *FD);
230 void mangleName(const NamedDecl *ND);
231 void mangleType(QualType T);
Douglas Gregor0094eb22010-05-26 05:11:13 +0000232 void mangleNameOrStandardSubstitution(const NamedDecl *ND);
233
Daniel Dunbar8483d212009-11-21 09:12:13 +0000234private:
Daniel Dunbaref5d75a2009-11-21 09:06:10 +0000235 bool mangleSubstitution(const NamedDecl *ND);
236 bool mangleSubstitution(QualType T);
Douglas Gregor7988def2010-04-28 05:58:56 +0000237 bool mangleSubstitution(TemplateName Template);
Daniel Dunbaref5d75a2009-11-21 09:06:10 +0000238 bool mangleSubstitution(uintptr_t Ptr);
Daniel Dunbard614e322009-11-21 09:05:47 +0000239
Daniel Dunbaref5d75a2009-11-21 09:06:10 +0000240 bool mangleStandardSubstitution(const NamedDecl *ND);
Daniel Dunbard614e322009-11-21 09:05:47 +0000241
Daniel Dunbaref5d75a2009-11-21 09:06:10 +0000242 void addSubstitution(const NamedDecl *ND) {
243 ND = cast<NamedDecl>(ND->getCanonicalDecl());
Anders Carlssonce214312009-11-07 04:26:04 +0000244
Daniel Dunbaref5d75a2009-11-21 09:06:10 +0000245 addSubstitution(reinterpret_cast<uintptr_t>(ND));
246 }
247 void addSubstitution(QualType T);
Douglas Gregor7988def2010-04-28 05:58:56 +0000248 void addSubstitution(TemplateName Template);
Daniel Dunbaref5d75a2009-11-21 09:06:10 +0000249 void addSubstitution(uintptr_t Ptr);
Daniel Dunbard614e322009-11-21 09:05:47 +0000250
John McCall6dc0a2b2011-04-24 08:23:24 +0000251 void mangleUnresolvedPrefix(NestedNameSpecifier *qualifier,
252 NamedDecl *firstQualifierLookup,
253 bool recursive = false);
254 void mangleUnresolvedName(NestedNameSpecifier *qualifier,
255 NamedDecl *firstQualifierLookup,
256 DeclarationName name,
John McCall09de8ec2010-02-04 01:42:13 +0000257 unsigned KnownArity = UnknownArity);
258
Daniel Dunbaref5d75a2009-11-21 09:06:10 +0000259 void mangleName(const TemplateDecl *TD,
260 const TemplateArgument *TemplateArgs,
261 unsigned NumTemplateArgs);
John McCall09de8ec2010-02-04 01:42:13 +0000262 void mangleUnqualifiedName(const NamedDecl *ND) {
263 mangleUnqualifiedName(ND, ND->getDeclName(), UnknownArity);
264 }
265 void mangleUnqualifiedName(const NamedDecl *ND, DeclarationName Name,
266 unsigned KnownArity);
Daniel Dunbaref5d75a2009-11-21 09:06:10 +0000267 void mangleUnscopedName(const NamedDecl *ND);
268 void mangleUnscopedTemplateName(const TemplateDecl *ND);
Douglas Gregor7988def2010-04-28 05:58:56 +0000269 void mangleUnscopedTemplateName(TemplateName);
Daniel Dunbaref5d75a2009-11-21 09:06:10 +0000270 void mangleSourceName(const IdentifierInfo *II);
271 void mangleLocalName(const NamedDecl *ND);
Fariborz Jahaniana529c252010-03-03 19:41:08 +0000272 void mangleNestedName(const NamedDecl *ND, const DeclContext *DC,
273 bool NoFunction=false);
Daniel Dunbaref5d75a2009-11-21 09:06:10 +0000274 void mangleNestedName(const TemplateDecl *TD,
275 const TemplateArgument *TemplateArgs,
276 unsigned NumTemplateArgs);
John McCall6dc0a2b2011-04-24 08:23:24 +0000277 void manglePrefix(NestedNameSpecifier *qualifier);
Fariborz Jahaniana529c252010-03-03 19:41:08 +0000278 void manglePrefix(const DeclContext *DC, bool NoFunction=false);
John McCall3ab84762011-05-04 01:45:19 +0000279 void manglePrefix(QualType type);
Daniel Dunbaref5d75a2009-11-21 09:06:10 +0000280 void mangleTemplatePrefix(const TemplateDecl *ND);
Douglas Gregor17362712010-04-23 03:10:43 +0000281 void mangleTemplatePrefix(TemplateName Template);
Daniel Dunbaref5d75a2009-11-21 09:06:10 +0000282 void mangleOperatorName(OverloadedOperatorKind OO, unsigned Arity);
283 void mangleQualifiers(Qualifiers Quals);
Douglas Gregorf3ea1ed2011-01-26 17:36:28 +0000284 void mangleRefQualifier(RefQualifierKind RefQualifier);
John McCallcc5e23c2009-09-05 07:56:18 +0000285
Anders Carlssonbf5694602009-12-10 03:14:39 +0000286 void mangleObjCMethodName(const ObjCMethodDecl *MD);
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +0000287
Daniel Dunbaref5d75a2009-11-21 09:06:10 +0000288 // Declare manglers for every type class.
John McCallcc5e23c2009-09-05 07:56:18 +0000289#define ABSTRACT_TYPE(CLASS, PARENT)
290#define NON_CANONICAL_TYPE(CLASS, PARENT)
291#define TYPE(CLASS, PARENT) void mangleType(const CLASS##Type *T);
292#include "clang/AST/TypeNodes.def"
293
Daniel Dunbaref5d75a2009-11-21 09:06:10 +0000294 void mangleType(const TagType*);
John McCall4f3b5f32010-07-14 06:43:17 +0000295 void mangleType(TemplateName);
Daniel Dunbaref5d75a2009-11-21 09:06:10 +0000296 void mangleBareFunctionType(const FunctionType *T,
297 bool MangleReturnType);
Bob Wilsone4652b12010-11-16 00:32:18 +0000298 void mangleNeonVectorType(const VectorType *T);
Anders Carlssone66e2942009-12-14 01:45:37 +0000299
300 void mangleIntegerLiteral(QualType T, const llvm::APSInt &Value);
John McCall6dc0a2b2011-04-24 08:23:24 +0000301 void mangleMemberExpr(const Expr *base, bool isArrow,
302 NestedNameSpecifier *qualifier,
303 NamedDecl *firstQualifierLookup,
304 DeclarationName name,
305 unsigned knownArity);
John McCall78fbb612010-08-18 19:18:59 +0000306 void mangleExpression(const Expr *E, unsigned Arity = UnknownArity);
Daniel Dunbaref5d75a2009-11-21 09:06:10 +0000307 void mangleCXXCtorType(CXXCtorType T);
308 void mangleCXXDtorType(CXXDtorType T);
Mike Stump11289f42009-09-09 15:08:12 +0000309
John McCallf834bcd2010-08-20 00:17:19 +0000310 void mangleTemplateArgs(const ExplicitTemplateArgumentList &TemplateArgs);
Douglas Gregor17362712010-04-23 03:10:43 +0000311 void mangleTemplateArgs(TemplateName Template,
312 const TemplateArgument *TemplateArgs,
Alexis Hunta8136cc2010-05-05 15:23:54 +0000313 unsigned NumTemplateArgs);
Rafael Espindola4d5c3d92010-03-11 14:07:00 +0000314 void mangleTemplateArgs(const TemplateParameterList &PL,
315 const TemplateArgument *TemplateArgs,
Daniel Dunbaref5d75a2009-11-21 09:06:10 +0000316 unsigned NumTemplateArgs);
Rafael Espindola4d5c3d92010-03-11 14:07:00 +0000317 void mangleTemplateArgs(const TemplateParameterList &PL,
318 const TemplateArgumentList &AL);
319 void mangleTemplateArg(const NamedDecl *P, const TemplateArgument &A);
John McCall3ab84762011-05-04 01:45:19 +0000320 void mangleUnresolvedTemplateArgs(const TemplateArgument *args,
321 unsigned numArgs);
Daniel Dunbard614e322009-11-21 09:05:47 +0000322
Daniel Dunbaref5d75a2009-11-21 09:06:10 +0000323 void mangleTemplateParameter(unsigned Index);
John McCall8fb0d9d2011-05-01 22:35:37 +0000324
325 void mangleFunctionParam(const ParmVarDecl *parm);
Daniel Dunbaref5d75a2009-11-21 09:06:10 +0000326};
Peter Collingbourne0ff0b372011-01-13 18:57:25 +0000327
Douglas Gregor5fec5b02009-02-13 00:10:09 +0000328}
329
Anders Carlsson810679c2009-04-02 15:51:53 +0000330static bool isInCLinkageSpecification(const Decl *D) {
Douglas Gregor05925032009-10-28 16:31:34 +0000331 D = D->getCanonicalDecl();
Mike Stump11289f42009-09-09 15:08:12 +0000332 for (const DeclContext *DC = D->getDeclContext();
Anders Carlsson810679c2009-04-02 15:51:53 +0000333 !DC->isTranslationUnit(); DC = DC->getParent()) {
Mike Stump11289f42009-09-09 15:08:12 +0000334 if (const LinkageSpecDecl *Linkage = dyn_cast<LinkageSpecDecl>(DC))
Anders Carlsson810679c2009-04-02 15:51:53 +0000335 return Linkage->getLanguage() == LinkageSpecDecl::lang_c;
336 }
Mike Stump11289f42009-09-09 15:08:12 +0000337
Anders Carlsson810679c2009-04-02 15:51:53 +0000338 return false;
339}
340
Peter Collingbourne0ff0b372011-01-13 18:57:25 +0000341bool ItaniumMangleContext::shouldMangleDeclName(const NamedDecl *D) {
Daniel Dunbare949e6c2009-11-21 09:14:52 +0000342 // In C, functions with no attributes never need to be mangled. Fastpath them.
343 if (!getASTContext().getLangOptions().CPlusPlus && !D->hasAttrs())
344 return false;
345
346 // Any decl can be declared with __asm("foo") on it, and this takes precedence
347 // over all other naming in the .o file.
348 if (D->hasAttr<AsmLabelAttr>())
349 return true;
350
Mike Stump9cc7d302009-09-02 00:25:38 +0000351 // Clang's "overloadable" attribute extension to C/C++ implies name mangling
Anders Carlssonc0a35612009-11-07 07:15:03 +0000352 // (always) as does passing a C++ member function and a function
353 // whose name is not a simple identifier.
Daniel Dunbare949e6c2009-11-21 09:14:52 +0000354 const FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
355 if (FD && (FD->hasAttr<OverloadableAttr>() || isa<CXXMethodDecl>(FD) ||
356 !FD->getDeclName().isIdentifier()))
357 return true;
Mike Stump11289f42009-09-09 15:08:12 +0000358
Daniel Dunbare949e6c2009-11-21 09:14:52 +0000359 // Otherwise, no mangling is done outside C++ mode.
360 if (!getASTContext().getLangOptions().CPlusPlus)
361 return false;
Mike Stump11289f42009-09-09 15:08:12 +0000362
Alexis Huntaecc45c2010-01-24 03:04:27 +0000363 // Variables at global scope with non-internal linkage are not mangled
Eli Friedmand4df7752009-12-02 20:32:49 +0000364 if (!FD) {
365 const DeclContext *DC = D->getDeclContext();
366 // Check for extern variable declared locally.
Fariborz Jahanian02995322010-06-30 18:57:21 +0000367 if (DC->isFunctionOrMethod() && D->hasLinkage())
Eli Friedmand4df7752009-12-02 20:32:49 +0000368 while (!DC->isNamespace() && !DC->isTranslationUnit())
369 DC = DC->getParent();
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000370 if (DC->isTranslationUnit() && D->getLinkage() != InternalLinkage)
Eli Friedmand4df7752009-12-02 20:32:49 +0000371 return false;
372 }
373
Eli Friedmanf8340de2010-07-18 20:49:59 +0000374 // Class members are always mangled.
375 if (D->getDeclContext()->isRecord())
376 return true;
377
Eli Friedmand4df7752009-12-02 20:32:49 +0000378 // C functions and "main" are not mangled.
379 if ((FD && FD->isMain()) || isInCLinkageSpecification(D))
Daniel Dunbare949e6c2009-11-21 09:14:52 +0000380 return false;
381
Anders Carlsson810679c2009-04-02 15:51:53 +0000382 return true;
383}
Douglas Gregor5fec5b02009-02-13 00:10:09 +0000384
Daniel Dunbar88ad4c52009-11-21 09:17:15 +0000385void CXXNameMangler::mangle(const NamedDecl *D, llvm::StringRef Prefix) {
Mike Stump9cc7d302009-09-02 00:25:38 +0000386 // Any decl can be declared with __asm("foo") on it, and this takes precedence
387 // over all other naming in the .o file.
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000388 if (const AsmLabelAttr *ALA = D->getAttr<AsmLabelAttr>()) {
Chris Lattner65749062009-03-21 08:24:40 +0000389 // If we have an asm name, then we use it as the mangling.
Rafael Espindolad62acb52011-02-15 22:23:51 +0000390
391 // Adding the prefix can cause problems when one file has a "foo" and
392 // another has a "\01foo". That is known to happen on ELF with the
393 // tricks normally used for producing aliases (PR9177). Fortunately the
394 // llvm mangler on ELF is a nop, so we can just avoid adding the \01
Peter Collingbourneba3e6662011-04-06 12:29:09 +0000395 // marker. We also avoid adding the marker if this is an alias for an
396 // LLVM intrinsic.
Rafael Espindolad62acb52011-02-15 22:23:51 +0000397 llvm::StringRef UserLabelPrefix =
398 getASTContext().Target.getUserLabelPrefix();
Peter Collingbourneba3e6662011-04-06 12:29:09 +0000399 if (!UserLabelPrefix.empty() && !ALA->getLabel().startswith("llvm."))
Rafael Espindolad62acb52011-02-15 22:23:51 +0000400 Out << '\01'; // LLVM IR Marker for __asm("foo")
401
Chris Lattner65749062009-03-21 08:24:40 +0000402 Out << ALA->getLabel();
Daniel Dunbare949e6c2009-11-21 09:14:52 +0000403 return;
Chris Lattner65749062009-03-21 08:24:40 +0000404 }
Mike Stump11289f42009-09-09 15:08:12 +0000405
Alexis Huntaecc45c2010-01-24 03:04:27 +0000406 // <mangled-name> ::= _Z <encoding>
Douglas Gregor5fec5b02009-02-13 00:10:09 +0000407 // ::= <data name>
408 // ::= <special-name>
Daniel Dunbar88ad4c52009-11-21 09:17:15 +0000409 Out << Prefix;
410 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
Daniel Dunbare949e6c2009-11-21 09:14:52 +0000411 mangleFunctionEncoding(FD);
Rafael Espindola4d5c3d92010-03-11 14:07:00 +0000412 else if (const VarDecl *VD = dyn_cast<VarDecl>(D))
413 mangleName(VD);
Daniel Dunbar88ad4c52009-11-21 09:17:15 +0000414 else
Rafael Espindola4d5c3d92010-03-11 14:07:00 +0000415 mangleName(cast<FieldDecl>(D));
Douglas Gregor5fec5b02009-02-13 00:10:09 +0000416}
417
418void CXXNameMangler::mangleFunctionEncoding(const FunctionDecl *FD) {
419 // <encoding> ::= <function name> <bare-function-type>
420 mangleName(FD);
Mike Stump11289f42009-09-09 15:08:12 +0000421
Daniel Dunbar88ad4c52009-11-21 09:17:15 +0000422 // Don't mangle in the type if this isn't a decl we should typically mangle.
423 if (!Context.shouldMangleDeclName(FD))
424 return;
425
Mike Stump9cc7d302009-09-02 00:25:38 +0000426 // Whether the mangling of a function type includes the return type depends on
427 // the context and the nature of the function. The rules for deciding whether
428 // the return type is included are:
Mike Stump11289f42009-09-09 15:08:12 +0000429 //
Douglas Gregore8925db2009-06-29 22:39:32 +0000430 // 1. Template functions (names or types) have return types encoded, with
431 // the exceptions listed below.
Mike Stump11289f42009-09-09 15:08:12 +0000432 // 2. Function types not appearing as part of a function name mangling,
Douglas Gregore8925db2009-06-29 22:39:32 +0000433 // e.g. parameters, pointer types, etc., have return type encoded, with the
434 // exceptions listed below.
435 // 3. Non-template function names do not have return types encoded.
436 //
Mike Stump9cc7d302009-09-02 00:25:38 +0000437 // The exceptions mentioned in (1) and (2) above, for which the return type is
438 // never included, are
Douglas Gregore8925db2009-06-29 22:39:32 +0000439 // 1. Constructors.
440 // 2. Destructors.
441 // 3. Conversion operator functions, e.g. operator int.
442 bool MangleReturnType = false;
Anders Carlssondf644fb2009-09-17 03:46:43 +0000443 if (FunctionTemplateDecl *PrimaryTemplate = FD->getPrimaryTemplate()) {
444 if (!(isa<CXXConstructorDecl>(FD) || isa<CXXDestructorDecl>(FD) ||
445 isa<CXXConversionDecl>(FD)))
446 MangleReturnType = true;
Daniel Dunbard614e322009-11-21 09:05:47 +0000447
Anders Carlssondf644fb2009-09-17 03:46:43 +0000448 // Mangle the type of the primary template.
449 FD = PrimaryTemplate->getTemplatedDecl();
450 }
451
John McCallfc93cf92009-10-22 22:37:11 +0000452 // Do the canonicalization out here because parameter types can
453 // undergo additional canonicalization (e.g. array decay).
John McCall424cec92011-01-19 06:33:43 +0000454 const FunctionType *FT
455 = cast<FunctionType>(Context.getASTContext()
John McCallfc93cf92009-10-22 22:37:11 +0000456 .getCanonicalType(FD->getType()));
457
458 mangleBareFunctionType(FT, MangleReturnType);
Douglas Gregor5fec5b02009-02-13 00:10:09 +0000459}
460
Anders Carlsson5c9e7b12009-12-04 06:23:23 +0000461static const DeclContext *IgnoreLinkageSpecDecls(const DeclContext *DC) {
462 while (isa<LinkageSpecDecl>(DC)) {
Anders Carlsson5c9e7b12009-12-04 06:23:23 +0000463 DC = DC->getParent();
464 }
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +0000465
Anders Carlsson5c9e7b12009-12-04 06:23:23 +0000466 return DC;
467}
468
Anders Carlssonb4d2cdb2010-06-02 15:58:27 +0000469/// isStd - Return whether a given namespace is the 'std' namespace.
470static bool isStd(const NamespaceDecl *NS) {
471 if (!IgnoreLinkageSpecDecls(NS->getParent())->isTranslationUnit())
472 return false;
473
474 const IdentifierInfo *II = NS->getOriginalNamespace()->getIdentifier();
475 return II && II->isStr("std");
476}
477
Anders Carlsson5c9e7b12009-12-04 06:23:23 +0000478// isStdNamespace - Return whether a given decl context is a toplevel 'std'
479// namespace.
Daniel Dunbarfdae6f72009-11-21 09:11:45 +0000480static bool isStdNamespace(const DeclContext *DC) {
Anders Carlsson5c9e7b12009-12-04 06:23:23 +0000481 if (!DC->isNamespace())
482 return false;
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +0000483
Anders Carlsson5c9e7b12009-12-04 06:23:23 +0000484 return isStd(cast<NamespaceDecl>(DC));
Daniel Dunbarfdae6f72009-11-21 09:11:45 +0000485}
486
Anders Carlsson9f8e3d12009-09-26 03:24:57 +0000487static const TemplateDecl *
488isTemplate(const NamedDecl *ND, const TemplateArgumentList *&TemplateArgs) {
Anders Carlsson559d9742009-09-18 19:00:18 +0000489 // Check if we have a function template.
490 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)){
Anders Carlsson9f8e3d12009-09-26 03:24:57 +0000491 if (const TemplateDecl *TD = FD->getPrimaryTemplate()) {
Anders Carlsson559d9742009-09-18 19:00:18 +0000492 TemplateArgs = FD->getTemplateSpecializationArgs();
Anders Carlsson9f8e3d12009-09-26 03:24:57 +0000493 return TD;
Anders Carlsson559d9742009-09-18 19:00:18 +0000494 }
495 }
496
Anders Carlssonc3773bd2009-09-18 19:44:50 +0000497 // Check if we have a class template.
498 if (const ClassTemplateSpecializationDecl *Spec =
499 dyn_cast<ClassTemplateSpecializationDecl>(ND)) {
500 TemplateArgs = &Spec->getTemplateArgs();
Anders Carlsson9f8e3d12009-09-26 03:24:57 +0000501 return Spec->getSpecializedTemplate();
Anders Carlssonc3773bd2009-09-18 19:44:50 +0000502 }
Daniel Dunbard614e322009-11-21 09:05:47 +0000503
Anders Carlsson559d9742009-09-18 19:00:18 +0000504 return 0;
505}
506
Douglas Gregor5fec5b02009-02-13 00:10:09 +0000507void CXXNameMangler::mangleName(const NamedDecl *ND) {
508 // <name> ::= <nested-name>
509 // ::= <unscoped-name>
510 // ::= <unscoped-template-name> <template-args>
Anders Carlsson98e00bb2009-09-17 03:17:01 +0000511 // ::= <local-name>
Douglas Gregor5fec5b02009-02-13 00:10:09 +0000512 //
Anders Carlssonca51ef12009-09-17 16:12:20 +0000513 const DeclContext *DC = ND->getDeclContext();
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +0000514
Eli Friedmand4df7752009-12-02 20:32:49 +0000515 // If this is an extern variable declared locally, the relevant DeclContext
516 // is that of the containing namespace, or the translation unit.
517 if (isa<FunctionDecl>(DC) && ND->hasLinkage())
518 while (!DC->isNamespace() && !DC->isTranslationUnit())
519 DC = DC->getParent();
John McCall18e4eda2010-10-18 21:28:44 +0000520 else if (GetLocalClassDecl(ND)) {
521 mangleLocalName(ND);
522 return;
523 }
Eli Friedmand4df7752009-12-02 20:32:49 +0000524
Anders Carlssonfc51cc92009-09-22 17:23:30 +0000525 while (isa<LinkageSpecDecl>(DC))
Anders Carlssonca51ef12009-09-17 16:12:20 +0000526 DC = DC->getParent();
Daniel Dunbard614e322009-11-21 09:05:47 +0000527
Anders Carlssonca51ef12009-09-17 16:12:20 +0000528 if (DC->isTranslationUnit() || isStdNamespace(DC)) {
Anders Carlsson559d9742009-09-18 19:00:18 +0000529 // Check if we have a template.
530 const TemplateArgumentList *TemplateArgs = 0;
Anders Carlsson26e67af2009-09-26 19:45:45 +0000531 if (const TemplateDecl *TD = isTemplate(ND, TemplateArgs)) {
Anders Carlsson559d9742009-09-18 19:00:18 +0000532 mangleUnscopedTemplateName(TD);
Rafael Espindola4d5c3d92010-03-11 14:07:00 +0000533 TemplateParameterList *TemplateParameters = TD->getTemplateParameters();
534 mangleTemplateArgs(*TemplateParameters, *TemplateArgs);
Anders Carlsson559d9742009-09-18 19:00:18 +0000535 return;
Anders Carlsson2b5e1dd2009-09-18 04:29:09 +0000536 }
Douglas Gregor5fec5b02009-02-13 00:10:09 +0000537
Anders Carlsson2b5e1dd2009-09-18 04:29:09 +0000538 mangleUnscopedName(ND);
539 return;
540 }
Daniel Dunbard614e322009-11-21 09:05:47 +0000541
Anders Carlssonbf5694602009-12-10 03:14:39 +0000542 if (isa<FunctionDecl>(DC) || isa<ObjCMethodDecl>(DC)) {
Anders Carlsson2b5e1dd2009-09-18 04:29:09 +0000543 mangleLocalName(ND);
544 return;
545 }
Daniel Dunbard614e322009-11-21 09:05:47 +0000546
Eli Friedmand4df7752009-12-02 20:32:49 +0000547 mangleNestedName(ND, DC);
Anders Carlsson2b5e1dd2009-09-18 04:29:09 +0000548}
Daniel Dunbard614e322009-11-21 09:05:47 +0000549void CXXNameMangler::mangleName(const TemplateDecl *TD,
Anders Carlsson7a8a74f2009-09-18 02:42:01 +0000550 const TemplateArgument *TemplateArgs,
551 unsigned NumTemplateArgs) {
Anders Carlsson5c9e7b12009-12-04 06:23:23 +0000552 const DeclContext *DC = IgnoreLinkageSpecDecls(TD->getDeclContext());
Daniel Dunbard614e322009-11-21 09:05:47 +0000553
Anders Carlsson7a8a74f2009-09-18 02:42:01 +0000554 if (DC->isTranslationUnit() || isStdNamespace(DC)) {
Anders Carlsson26e67af2009-09-26 19:45:45 +0000555 mangleUnscopedTemplateName(TD);
Rafael Espindola4d5c3d92010-03-11 14:07:00 +0000556 TemplateParameterList *TemplateParameters = TD->getTemplateParameters();
557 mangleTemplateArgs(*TemplateParameters, TemplateArgs, NumTemplateArgs);
Anders Carlsson7a8a74f2009-09-18 02:42:01 +0000558 } else {
559 mangleNestedName(TD, TemplateArgs, NumTemplateArgs);
560 }
561}
562
Anders Carlsson98e00bb2009-09-17 03:17:01 +0000563void CXXNameMangler::mangleUnscopedName(const NamedDecl *ND) {
564 // <unscoped-name> ::= <unqualified-name>
565 // ::= St <unqualified-name> # ::std::
566 if (isStdNamespace(ND->getDeclContext()))
567 Out << "St";
Daniel Dunbard614e322009-11-21 09:05:47 +0000568
Anders Carlsson98e00bb2009-09-17 03:17:01 +0000569 mangleUnqualifiedName(ND);
570}
571
Anders Carlsson26e67af2009-09-26 19:45:45 +0000572void CXXNameMangler::mangleUnscopedTemplateName(const TemplateDecl *ND) {
Anders Carlsson98e00bb2009-09-17 03:17:01 +0000573 // <unscoped-template-name> ::= <unscoped-name>
574 // ::= <substitution>
Anders Carlsson7a8a74f2009-09-18 02:42:01 +0000575 if (mangleSubstitution(ND))
Anders Carlssona2fb9bc2009-09-17 04:02:31 +0000576 return;
Daniel Dunbard614e322009-11-21 09:05:47 +0000577
Douglas Gregora16b0ca2010-02-05 20:45:00 +0000578 // <template-template-param> ::= <template-param>
579 if (const TemplateTemplateParmDecl *TTP
580 = dyn_cast<TemplateTemplateParmDecl>(ND)) {
581 mangleTemplateParameter(TTP->getIndex());
582 return;
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +0000583 }
Douglas Gregora16b0ca2010-02-05 20:45:00 +0000584
Anders Carlsson49232b92009-09-26 20:13:56 +0000585 mangleUnscopedName(ND->getTemplatedDecl());
Anders Carlsson7a8a74f2009-09-18 02:42:01 +0000586 addSubstitution(ND);
Anders Carlsson98e00bb2009-09-17 03:17:01 +0000587}
588
Douglas Gregor7988def2010-04-28 05:58:56 +0000589void CXXNameMangler::mangleUnscopedTemplateName(TemplateName Template) {
590 // <unscoped-template-name> ::= <unscoped-name>
591 // ::= <substitution>
592 if (TemplateDecl *TD = Template.getAsTemplateDecl())
593 return mangleUnscopedTemplateName(TD);
Alexis Hunta8136cc2010-05-05 15:23:54 +0000594
Douglas Gregor7988def2010-04-28 05:58:56 +0000595 if (mangleSubstitution(Template))
596 return;
597
598 // FIXME: How to cope with operators here?
599 DependentTemplateName *Dependent = Template.getAsDependentTemplateName();
600 assert(Dependent && "Not a dependent template name?");
601 if (!Dependent->isIdentifier()) {
602 // FIXME: We can't possibly know the arity of the operator here!
603 Diagnostic &Diags = Context.getDiags();
604 unsigned DiagID = Diags.getCustomDiagID(Diagnostic::Error,
605 "cannot mangle dependent operator name");
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000606 Diags.Report(DiagID);
Douglas Gregor7988def2010-04-28 05:58:56 +0000607 return;
608 }
Alexis Hunta8136cc2010-05-05 15:23:54 +0000609
Douglas Gregor7988def2010-04-28 05:58:56 +0000610 mangleSourceName(Dependent->getIdentifier());
611 addSubstitution(Template);
612}
613
John McCalld8d1e2a2011-04-24 03:07:16 +0000614void CXXNameMangler::mangleFloat(const llvm::APFloat &f) {
615 // ABI:
616 // Floating-point literals are encoded using a fixed-length
617 // lowercase hexadecimal string corresponding to the internal
618 // representation (IEEE on Itanium), high-order bytes first,
619 // without leading zeroes. For example: "Lf bf800000 E" is -1.0f
620 // on Itanium.
621 // APInt::toString uses uppercase hexadecimal, and it's not really
622 // worth embellishing that interface for this use case, so we just
623 // do a second pass to lowercase things.
624 typedef llvm::SmallString<20> buffer_t;
625 buffer_t buffer;
626 f.bitcastToAPInt().toString(buffer, 16, false);
627
628 for (buffer_t::iterator i = buffer.begin(), e = buffer.end(); i != e; ++i)
629 if (isupper(*i)) *i = tolower(*i);
630
631 Out.write(buffer.data(), buffer.size());
John McCall05ddbb82010-07-14 04:20:34 +0000632}
633
634void CXXNameMangler::mangleNumber(const llvm::APSInt &Value) {
635 if (Value.isSigned() && Value.isNegative()) {
636 Out << 'n';
637 Value.abs().print(Out, true);
638 } else
639 Value.print(Out, Value.isSigned());
640}
641
Anders Carlssonc7785402009-11-26 02:32:05 +0000642void CXXNameMangler::mangleNumber(int64_t Number) {
643 // <number> ::= [n] <non-negative decimal integer>
644 if (Number < 0) {
645 Out << 'n';
646 Number = -Number;
647 }
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +0000648
Anders Carlssonc7785402009-11-26 02:32:05 +0000649 Out << Number;
650}
651
Anders Carlssoncd836f02010-03-23 17:17:29 +0000652void CXXNameMangler::mangleCallOffset(int64_t NonVirtual, int64_t Virtual) {
Mike Stump9cc7d302009-09-02 00:25:38 +0000653 // <call-offset> ::= h <nv-offset> _
654 // ::= v <v-offset> _
655 // <nv-offset> ::= <offset number> # non-virtual base override
Anders Carlssonc7785402009-11-26 02:32:05 +0000656 // <v-offset> ::= <offset number> _ <virtual offset number>
Mike Stump9cc7d302009-09-02 00:25:38 +0000657 // # virtual base override, with vcall offset
Anders Carlssoncd836f02010-03-23 17:17:29 +0000658 if (!Virtual) {
Anders Carlssonc7785402009-11-26 02:32:05 +0000659 Out << 'h';
Anders Carlssoncd836f02010-03-23 17:17:29 +0000660 mangleNumber(NonVirtual);
Anders Carlssonc7785402009-11-26 02:32:05 +0000661 Out << '_';
662 return;
Mike Stump9cc7d302009-09-02 00:25:38 +0000663 }
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +0000664
Anders Carlssonc7785402009-11-26 02:32:05 +0000665 Out << 'v';
Anders Carlssoncd836f02010-03-23 17:17:29 +0000666 mangleNumber(NonVirtual);
Anders Carlssonc7785402009-11-26 02:32:05 +0000667 Out << '_';
Anders Carlssoncd836f02010-03-23 17:17:29 +0000668 mangleNumber(Virtual);
Anders Carlssonc7785402009-11-26 02:32:05 +0000669 Out << '_';
Mike Stump3f707e92009-09-02 00:56:18 +0000670}
671
John McCall3ab84762011-05-04 01:45:19 +0000672void CXXNameMangler::manglePrefix(QualType type) {
John McCall6dc0a2b2011-04-24 08:23:24 +0000673 if (const TemplateSpecializationType *TST =
674 type->getAs<TemplateSpecializationType>()) {
675 if (!mangleSubstitution(QualType(TST, 0))) {
676 mangleTemplatePrefix(TST->getTemplateName());
Alexis Hunta8136cc2010-05-05 15:23:54 +0000677
Douglas Gregor6e068012011-02-28 00:04:36 +0000678 // FIXME: GCC does not appear to mangle the template arguments when
679 // the template in question is a dependent template name. Should we
680 // emulate that badness?
John McCall6dc0a2b2011-04-24 08:23:24 +0000681 mangleTemplateArgs(TST->getTemplateName(), TST->getArgs(),
682 TST->getNumArgs());
683 addSubstitution(QualType(TST, 0));
Rafael Espindolacd7eef92010-03-17 04:28:11 +0000684 }
John McCall6dc0a2b2011-04-24 08:23:24 +0000685 } else if (const DependentTemplateSpecializationType *DTST
686 = type->getAs<DependentTemplateSpecializationType>()) {
687 TemplateName Template
688 = getASTContext().getDependentTemplateName(DTST->getQualifier(),
689 DTST->getIdentifier());
690 mangleTemplatePrefix(Template);
691
692 // FIXME: GCC does not appear to mangle the template arguments when
693 // the template in question is a dependent template name. Should we
694 // emulate that badness?
695 mangleTemplateArgs(Template, DTST->getArgs(), DTST->getNumArgs());
696 } else {
697 // We use the QualType mangle type variant here because it handles
698 // substitutions.
699 mangleType(type);
John McCall09de8ec2010-02-04 01:42:13 +0000700 }
701}
702
John McCall6dc0a2b2011-04-24 08:23:24 +0000703/// Mangle everything prior to the base-unresolved-name in an unresolved-name.
704///
705/// \param firstQualifierLookup - the entity found by unqualified lookup
706/// for the first name in the qualifier, if this is for a member expression
707/// \param recursive - true if this is being called recursively,
708/// i.e. if there is more prefix "to the right".
709void CXXNameMangler::mangleUnresolvedPrefix(NestedNameSpecifier *qualifier,
710 NamedDecl *firstQualifierLookup,
711 bool recursive) {
John McCall09de8ec2010-02-04 01:42:13 +0000712
John McCall6dc0a2b2011-04-24 08:23:24 +0000713 // x, ::x
714 // <unresolved-name> ::= [gs] <base-unresolved-name>
715
716 // T::x / decltype(p)::x
717 // <unresolved-name> ::= sr <unresolved-type> <base-unresolved-name>
718
719 // T::N::x /decltype(p)::N::x
720 // <unresolved-name> ::= srN <unresolved-type> <unresolved-qualifier-level>+ E
721 // <base-unresolved-name>
722
723 // A::x, N::y, A<T>::z; "gs" means leading "::"
724 // <unresolved-name> ::= [gs] sr <unresolved-qualifier-level>+ E
725 // <base-unresolved-name>
726
727 switch (qualifier->getKind()) {
728 case NestedNameSpecifier::Global:
729 Out << "gs";
730
731 // We want an 'sr' unless this is the entire NNS.
732 if (recursive)
733 Out << "sr";
734
735 // We never want an 'E' here.
736 return;
737
738 case NestedNameSpecifier::Namespace:
739 if (qualifier->getPrefix())
740 mangleUnresolvedPrefix(qualifier->getPrefix(), firstQualifierLookup,
741 /*recursive*/ true);
742 else
743 Out << "sr";
744 mangleSourceName(qualifier->getAsNamespace()->getIdentifier());
745 break;
746 case NestedNameSpecifier::NamespaceAlias:
747 if (qualifier->getPrefix())
748 mangleUnresolvedPrefix(qualifier->getPrefix(), firstQualifierLookup,
749 /*recursive*/ true);
750 else
751 Out << "sr";
752 mangleSourceName(qualifier->getAsNamespaceAlias()->getIdentifier());
753 break;
754
755 case NestedNameSpecifier::TypeSpec:
756 case NestedNameSpecifier::TypeSpecWithTemplate: {
John McCall3ab84762011-05-04 01:45:19 +0000757 const Type *type = qualifier->getAsType();
John McCall6dc0a2b2011-04-24 08:23:24 +0000758
John McCall3ab84762011-05-04 01:45:19 +0000759 // We only want to use an unresolved-type encoding if this is one of:
760 // - a decltype
761 // - a template type parameter
762 // - a template template parameter with arguments
763 // In all of these cases, we should have no prefix.
764 if (qualifier->getPrefix()) {
765 mangleUnresolvedPrefix(qualifier->getPrefix(), firstQualifierLookup,
766 /*recursive*/ true);
767 } else {
768 // Otherwise, all the cases want this.
769 Out << "sr";
John McCall3ab84762011-05-04 01:45:19 +0000770 }
771
John McCall3ab84762011-05-04 01:45:19 +0000772 // Only certain other types are valid as prefixes; enumerate them.
773 // FIXME: can we get ElaboratedTypes here?
774 // FIXME: SubstTemplateTypeParmType?
John McCall15547bb2011-06-28 16:49:23 +0000775 switch (type->getTypeClass()) {
776 case Type::Builtin:
777 case Type::Complex:
778 case Type::Pointer:
779 case Type::BlockPointer:
780 case Type::LValueReference:
781 case Type::RValueReference:
782 case Type::MemberPointer:
783 case Type::ConstantArray:
784 case Type::IncompleteArray:
785 case Type::VariableArray:
786 case Type::DependentSizedArray:
787 case Type::DependentSizedExtVector:
788 case Type::Vector:
789 case Type::ExtVector:
790 case Type::FunctionProto:
791 case Type::FunctionNoProto:
792 case Type::Enum:
793 case Type::Paren:
794 case Type::Elaborated:
795 case Type::Attributed:
796 case Type::Auto:
797 case Type::PackExpansion:
798 case Type::SubstTemplateTypeParmPack:
799 case Type::ObjCObject:
800 case Type::ObjCInterface:
801 case Type::ObjCObjectPointer:
802 llvm_unreachable("type is illegal as a nested name specifier");
803
804 // <unresolved-type> ::= <template-param>
805 // ::= <decltype>
806 // ::= <template-template-param> <template-args>
807 // (this last is not official yet)
808 case Type::TypeOfExpr:
809 case Type::TypeOf:
810 case Type::Decltype:
811 case Type::TemplateTypeParm:
812 case Type::UnaryTransform:
813 unresolvedType:
814 assert(!qualifier->getPrefix());
815
816 // We only get here recursively if we're followed by identifiers.
817 if (recursive) Out << 'N';
818
819 // This seems to do everything we want.
820 mangleType(QualType(type, 0));
821
822 // We never want to print 'E' directly after an unresolved-type,
823 // so we return directly.
824 return;
825
826 // Substituted template type parameters should only come up with
827 // enclosing templates.
828 // <unresolved-type> ::= <existing-substitution> [ <template-args> ]
829 case Type::SubstTemplateTypeParm: {
830 if (recursive) Out << 'N';
831
832 bool wasSubstituted = mangleSubstitution(QualType(type, 0));
833 assert(wasSubstituted && "no substitution for outer template argument?");
834 (void) wasSubstituted;
835 return;
836 }
837
838 case Type::Typedef:
839 mangleSourceName(cast<TypedefType>(type)->getDecl()->getIdentifier());
840 break;
841
842 case Type::UnresolvedUsing:
843 mangleSourceName(cast<UnresolvedUsingType>(type)->getDecl()
844 ->getIdentifier());
845 break;
846
847 case Type::Record:
848 mangleSourceName(cast<RecordType>(type)->getDecl()->getIdentifier());
849 break;
850
851 case Type::TemplateSpecialization: {
852 const TemplateSpecializationType *tst
853 = cast<TemplateSpecializationType>(type);
John McCall3ab84762011-05-04 01:45:19 +0000854 TemplateDecl *temp = tst->getTemplateName().getAsTemplateDecl();
John McCall15547bb2011-06-28 16:49:23 +0000855
856 // If the base is a template template parameter, this is an
857 // unresolved type.
John McCall3ab84762011-05-04 01:45:19 +0000858 assert(temp && "no template for template specialization type");
John McCall15547bb2011-06-28 16:49:23 +0000859 if (isa<TemplateTemplateParmDecl>(temp)) goto unresolvedType;
860
John McCall3ab84762011-05-04 01:45:19 +0000861 mangleSourceName(temp->getIdentifier());
862 mangleUnresolvedTemplateArgs(tst->getArgs(), tst->getNumArgs());
John McCall15547bb2011-06-28 16:49:23 +0000863 break;
864 }
865
866 case Type::InjectedClassName:
867 mangleSourceName(cast<InjectedClassNameType>(type)->getDecl()
868 ->getIdentifier());
869 break;
870
871 case Type::DependentName:
872 mangleSourceName(cast<DependentNameType>(type)->getIdentifier());
873 break;
874
875 case Type::DependentTemplateSpecialization: {
876 const DependentTemplateSpecializationType *tst
877 = cast<DependentTemplateSpecializationType>(type);
John McCall3ab84762011-05-04 01:45:19 +0000878 mangleSourceName(tst->getIdentifier());
879 mangleUnresolvedTemplateArgs(tst->getArgs(), tst->getNumArgs());
John McCall15547bb2011-06-28 16:49:23 +0000880 break;
881 }
John McCall3ab84762011-05-04 01:45:19 +0000882 }
883 break;
John McCall6dc0a2b2011-04-24 08:23:24 +0000884 }
885
886 case NestedNameSpecifier::Identifier:
887 // Member expressions can have these without prefixes.
888 if (qualifier->getPrefix()) {
889 mangleUnresolvedPrefix(qualifier->getPrefix(), firstQualifierLookup,
890 /*recursive*/ true);
891 } else if (firstQualifierLookup) {
892
893 // Try to make a proper qualifier out of the lookup result, and
894 // then just recurse on that.
895 NestedNameSpecifier *newQualifier;
896 if (TypeDecl *typeDecl = dyn_cast<TypeDecl>(firstQualifierLookup)) {
897 QualType type = getASTContext().getTypeDeclType(typeDecl);
898
899 // Pretend we had a different nested name specifier.
900 newQualifier = NestedNameSpecifier::Create(getASTContext(),
901 /*prefix*/ 0,
902 /*template*/ false,
903 type.getTypePtr());
904 } else if (NamespaceDecl *nspace =
905 dyn_cast<NamespaceDecl>(firstQualifierLookup)) {
906 newQualifier = NestedNameSpecifier::Create(getASTContext(),
907 /*prefix*/ 0,
908 nspace);
909 } else if (NamespaceAliasDecl *alias =
910 dyn_cast<NamespaceAliasDecl>(firstQualifierLookup)) {
911 newQualifier = NestedNameSpecifier::Create(getASTContext(),
912 /*prefix*/ 0,
913 alias);
914 } else {
915 // No sensible mangling to do here.
916 newQualifier = 0;
917 }
918
919 if (newQualifier)
920 return mangleUnresolvedPrefix(newQualifier, /*lookup*/ 0, recursive);
921
922 } else {
923 Out << "sr";
924 }
925
926 mangleSourceName(qualifier->getAsIdentifier());
927 break;
928 }
929
930 // If this was the innermost part of the NNS, and we fell out to
931 // here, append an 'E'.
932 if (!recursive)
933 Out << 'E';
934}
935
936/// Mangle an unresolved-name, which is generally used for names which
937/// weren't resolved to specific entities.
938void CXXNameMangler::mangleUnresolvedName(NestedNameSpecifier *qualifier,
939 NamedDecl *firstQualifierLookup,
940 DeclarationName name,
941 unsigned knownArity) {
942 if (qualifier) mangleUnresolvedPrefix(qualifier, firstQualifierLookup);
943 mangleUnqualifiedName(0, name, knownArity);
John McCall09de8ec2010-02-04 01:42:13 +0000944}
945
Anders Carlsson248704c2010-06-08 14:49:03 +0000946static const FieldDecl *FindFirstNamedDataMember(const RecordDecl *RD) {
947 assert(RD->isAnonymousStructOrUnion() &&
948 "Expected anonymous struct or union!");
949
950 for (RecordDecl::field_iterator I = RD->field_begin(), E = RD->field_end();
951 I != E; ++I) {
952 const FieldDecl *FD = *I;
953
954 if (FD->getIdentifier())
955 return FD;
956
957 if (const RecordType *RT = FD->getType()->getAs<RecordType>()) {
958 if (const FieldDecl *NamedDataMember =
959 FindFirstNamedDataMember(RT->getDecl()))
960 return NamedDataMember;
961 }
962 }
963
964 // We didn't find a named data member.
965 return 0;
966}
967
John McCall09de8ec2010-02-04 01:42:13 +0000968void CXXNameMangler::mangleUnqualifiedName(const NamedDecl *ND,
969 DeclarationName Name,
970 unsigned KnownArity) {
Douglas Gregor5fec5b02009-02-13 00:10:09 +0000971 // <unqualified-name> ::= <operator-name>
Mike Stump11289f42009-09-09 15:08:12 +0000972 // ::= <ctor-dtor-name>
973 // ::= <source-name>
Douglas Gregor5fec5b02009-02-13 00:10:09 +0000974 switch (Name.getNameKind()) {
Anders Carlsson1e39bd92009-10-07 01:45:02 +0000975 case DeclarationName::Identifier: {
Anders Carlsson1e39bd92009-10-07 01:45:02 +0000976 if (const IdentifierInfo *II = Name.getAsIdentifierInfo()) {
Alexis Huntaecc45c2010-01-24 03:04:27 +0000977 // We must avoid conflicts between internally- and externally-
John McCall290b32b2011-03-22 06:34:45 +0000978 // linked variable and function declaration names in the same TU:
979 // void test() { extern void foo(); }
980 // static void foo();
981 // This naming convention is the same as that followed by GCC,
982 // though it shouldn't actually matter.
983 if (ND && ND->getLinkage() == InternalLinkage &&
Alexis Huntaecc45c2010-01-24 03:04:27 +0000984 ND->getDeclContext()->isFileContext())
985 Out << 'L';
986
Anders Carlsson1e39bd92009-10-07 01:45:02 +0000987 mangleSourceName(II);
988 break;
989 }
Daniel Dunbard614e322009-11-21 09:05:47 +0000990
John McCall09de8ec2010-02-04 01:42:13 +0000991 // Otherwise, an anonymous entity. We must have a declaration.
992 assert(ND && "mangling empty name without declaration");
993
994 if (const NamespaceDecl *NS = dyn_cast<NamespaceDecl>(ND)) {
995 if (NS->isAnonymousNamespace()) {
996 // This is how gcc mangles these names.
997 Out << "12_GLOBAL__N_1";
998 break;
999 }
1000 }
1001
Anders Carlsson248704c2010-06-08 14:49:03 +00001002 if (const VarDecl *VD = dyn_cast<VarDecl>(ND)) {
1003 // We must have an anonymous union or struct declaration.
1004 const RecordDecl *RD =
1005 cast<RecordDecl>(VD->getType()->getAs<RecordType>()->getDecl());
1006
1007 // Itanium C++ ABI 5.1.2:
1008 //
1009 // For the purposes of mangling, the name of an anonymous union is
1010 // considered to be the name of the first named data member found by a
1011 // pre-order, depth-first, declaration-order walk of the data members of
1012 // the anonymous union. If there is no such data member (i.e., if all of
1013 // the data members in the union are unnamed), then there is no way for
1014 // a program to refer to the anonymous union, and there is therefore no
1015 // need to mangle its name.
1016 const FieldDecl *FD = FindFirstNamedDataMember(RD);
John McCall49146242010-08-05 22:02:13 +00001017
1018 // It's actually possible for various reasons for us to get here
1019 // with an empty anonymous struct / union. Fortunately, it
1020 // doesn't really matter what name we generate.
1021 if (!FD) break;
Anders Carlsson248704c2010-06-08 14:49:03 +00001022 assert(FD->getIdentifier() && "Data member name isn't an identifier!");
1023
1024 mangleSourceName(FD->getIdentifier());
1025 break;
1026 }
1027
Anders Carlsson1e39bd92009-10-07 01:45:02 +00001028 // We must have an anonymous struct.
1029 const TagDecl *TD = cast<TagDecl>(ND);
Richard Smithdda56e42011-04-15 14:24:37 +00001030 if (const TypedefNameDecl *D = TD->getTypedefNameForAnonDecl()) {
Anders Carlsson1e39bd92009-10-07 01:45:02 +00001031 assert(TD->getDeclContext() == D->getDeclContext() &&
1032 "Typedef should not be in another decl context!");
1033 assert(D->getDeclName().getAsIdentifierInfo() &&
1034 "Typedef was not named!");
1035 mangleSourceName(D->getDeclName().getAsIdentifierInfo());
1036 break;
1037 }
Daniel Dunbard614e322009-11-21 09:05:47 +00001038
Anders Carlsson1e39bd92009-10-07 01:45:02 +00001039 // Get a unique id for the anonymous struct.
1040 uint64_t AnonStructId = Context.getAnonymousStructId(TD);
1041
1042 // Mangle it as a source name in the form
Daniel Dunbard614e322009-11-21 09:05:47 +00001043 // [n] $_<id>
Anders Carlsson1e39bd92009-10-07 01:45:02 +00001044 // where n is the length of the string.
1045 llvm::SmallString<8> Str;
1046 Str += "$_";
1047 Str += llvm::utostr(AnonStructId);
1048
1049 Out << Str.size();
1050 Out << Str.str();
Douglas Gregor5fec5b02009-02-13 00:10:09 +00001051 break;
Anders Carlsson1e39bd92009-10-07 01:45:02 +00001052 }
Douglas Gregor5fec5b02009-02-13 00:10:09 +00001053
1054 case DeclarationName::ObjCZeroArgSelector:
1055 case DeclarationName::ObjCOneArgSelector:
1056 case DeclarationName::ObjCMultiArgSelector:
1057 assert(false && "Can't mangle Objective-C selector names here!");
1058 break;
1059
1060 case DeclarationName::CXXConstructorName:
Anders Carlssoneaa28f72009-04-17 01:58:57 +00001061 if (ND == Structor)
Mike Stump9cc7d302009-09-02 00:25:38 +00001062 // If the named decl is the C++ constructor we're mangling, use the type
1063 // we were given.
Anders Carlssoneaa28f72009-04-17 01:58:57 +00001064 mangleCXXCtorType(static_cast<CXXCtorType>(StructorType));
Anders Carlssone4c40c82009-04-15 05:36:58 +00001065 else
1066 // Otherwise, use the complete constructor name. This is relevant if a
1067 // class with a constructor is declared within a constructor.
1068 mangleCXXCtorType(Ctor_Complete);
Douglas Gregor5fec5b02009-02-13 00:10:09 +00001069 break;
1070
1071 case DeclarationName::CXXDestructorName:
Anders Carlssoneaa28f72009-04-17 01:58:57 +00001072 if (ND == Structor)
Mike Stump9cc7d302009-09-02 00:25:38 +00001073 // If the named decl is the C++ destructor we're mangling, use the type we
1074 // were given.
Anders Carlssoneaa28f72009-04-17 01:58:57 +00001075 mangleCXXDtorType(static_cast<CXXDtorType>(StructorType));
1076 else
1077 // Otherwise, use the complete destructor name. This is relevant if a
1078 // class with a destructor is declared within a destructor.
1079 mangleCXXDtorType(Dtor_Complete);
Douglas Gregor5fec5b02009-02-13 00:10:09 +00001080 break;
1081
1082 case DeclarationName::CXXConversionFunctionName:
Mike Stump11289f42009-09-09 15:08:12 +00001083 // <operator-name> ::= cv <type> # (cast)
Douglas Gregoradb02012009-02-13 01:28:03 +00001084 Out << "cv";
Anders Carlssonff971e82009-10-07 01:06:45 +00001085 mangleType(Context.getASTContext().getCanonicalType(Name.getCXXNameType()));
Douglas Gregor5fec5b02009-02-13 00:10:09 +00001086 break;
1087
Anders Carlsson8a35c792009-12-22 06:36:32 +00001088 case DeclarationName::CXXOperatorName: {
John McCall09de8ec2010-02-04 01:42:13 +00001089 unsigned Arity;
1090 if (ND) {
1091 Arity = cast<FunctionDecl>(ND)->getNumParams();
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00001092
John McCall09de8ec2010-02-04 01:42:13 +00001093 // If we have a C++ member function, we need to include the 'this' pointer.
1094 // FIXME: This does not make sense for operators that are static, but their
1095 // names stay the same regardless of the arity (operator new for instance).
1096 if (isa<CXXMethodDecl>(ND))
1097 Arity++;
1098 } else
1099 Arity = KnownArity;
1100
Anders Carlsson8a35c792009-12-22 06:36:32 +00001101 mangleOperatorName(Name.getCXXOverloadedOperator(), Arity);
Douglas Gregor5fec5b02009-02-13 00:10:09 +00001102 break;
Anders Carlsson8a35c792009-12-22 06:36:32 +00001103 }
Douglas Gregor5fec5b02009-02-13 00:10:09 +00001104
Alexis Hunt3d221f22009-11-29 07:34:05 +00001105 case DeclarationName::CXXLiteralOperatorName:
Alexis Hunt9b3a3952009-12-04 21:11:13 +00001106 // FIXME: This mangling is not yet official.
Alexis Huntbf2f0c22009-12-04 21:01:37 +00001107 Out << "li";
Alexis Hunt3d221f22009-11-29 07:34:05 +00001108 mangleSourceName(Name.getCXXLiteralIdentifier());
1109 break;
1110
Douglas Gregor5fec5b02009-02-13 00:10:09 +00001111 case DeclarationName::CXXUsingDirective:
1112 assert(false && "Can't mangle a using directive name!");
Douglas Gregoradb02012009-02-13 01:28:03 +00001113 break;
Douglas Gregor5fec5b02009-02-13 00:10:09 +00001114 }
1115}
1116
1117void CXXNameMangler::mangleSourceName(const IdentifierInfo *II) {
1118 // <source-name> ::= <positive length number> <identifier>
1119 // <number> ::= [n] <non-negative decimal integer>
1120 // <identifier> ::= <unqualified source code identifier>
1121 Out << II->getLength() << II->getName();
1122}
1123
Eli Friedmand4df7752009-12-02 20:32:49 +00001124void CXXNameMangler::mangleNestedName(const NamedDecl *ND,
Fariborz Jahaniana529c252010-03-03 19:41:08 +00001125 const DeclContext *DC,
1126 bool NoFunction) {
Douglas Gregorf3ea1ed2011-01-26 17:36:28 +00001127 // <nested-name>
1128 // ::= N [<CV-qualifiers>] [<ref-qualifier>] <prefix> <unqualified-name> E
1129 // ::= N [<CV-qualifiers>] [<ref-qualifier>] <template-prefix>
1130 // <template-args> E
Anders Carlsson296f8dc2009-09-26 03:55:37 +00001131
Douglas Gregor5fec5b02009-02-13 00:10:09 +00001132 Out << 'N';
Douglas Gregorf3ea1ed2011-01-26 17:36:28 +00001133 if (const CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(ND)) {
John McCall8ccfcb52009-09-24 19:53:00 +00001134 mangleQualifiers(Qualifiers::fromCVRMask(Method->getTypeQualifiers()));
Douglas Gregorf3ea1ed2011-01-26 17:36:28 +00001135 mangleRefQualifier(Method->getRefQualifier());
1136 }
1137
Anders Carlsson559d9742009-09-18 19:00:18 +00001138 // Check if we have a template.
1139 const TemplateArgumentList *TemplateArgs = 0;
Daniel Dunbard614e322009-11-21 09:05:47 +00001140 if (const TemplateDecl *TD = isTemplate(ND, TemplateArgs)) {
Anders Carlsson559d9742009-09-18 19:00:18 +00001141 mangleTemplatePrefix(TD);
Rafael Espindola4d5c3d92010-03-11 14:07:00 +00001142 TemplateParameterList *TemplateParameters = TD->getTemplateParameters();
1143 mangleTemplateArgs(*TemplateParameters, *TemplateArgs);
Fariborz Jahaniana529c252010-03-03 19:41:08 +00001144 }
1145 else {
1146 manglePrefix(DC, NoFunction);
Anders Carlsson2b5e1dd2009-09-18 04:29:09 +00001147 mangleUnqualifiedName(ND);
1148 }
Daniel Dunbard614e322009-11-21 09:05:47 +00001149
Douglas Gregor5fec5b02009-02-13 00:10:09 +00001150 Out << 'E';
1151}
Daniel Dunbard614e322009-11-21 09:05:47 +00001152void CXXNameMangler::mangleNestedName(const TemplateDecl *TD,
Anders Carlsson7a8a74f2009-09-18 02:42:01 +00001153 const TemplateArgument *TemplateArgs,
1154 unsigned NumTemplateArgs) {
Anders Carlssond122de52009-09-27 19:53:49 +00001155 // <nested-name> ::= N [<CV-qualifiers>] <template-prefix> <template-args> E
1156
Anders Carlsson7a8a74f2009-09-18 02:42:01 +00001157 Out << 'N';
Daniel Dunbard614e322009-11-21 09:05:47 +00001158
Anders Carlssond122de52009-09-27 19:53:49 +00001159 mangleTemplatePrefix(TD);
Rafael Espindola4d5c3d92010-03-11 14:07:00 +00001160 TemplateParameterList *TemplateParameters = TD->getTemplateParameters();
1161 mangleTemplateArgs(*TemplateParameters, TemplateArgs, NumTemplateArgs);
Daniel Dunbard614e322009-11-21 09:05:47 +00001162
Anders Carlsson7a8a74f2009-09-18 02:42:01 +00001163 Out << 'E';
1164}
1165
Anders Carlsson4eca1092009-04-02 16:24:45 +00001166void CXXNameMangler::mangleLocalName(const NamedDecl *ND) {
1167 // <local-name> := Z <function encoding> E <entity name> [<discriminator>]
1168 // := Z <function encoding> E s [<discriminator>]
Mike Stump11289f42009-09-09 15:08:12 +00001169 // <discriminator> := _ <non-negative number>
Fariborz Jahaniana529c252010-03-03 19:41:08 +00001170 const DeclContext *DC = ND->getDeclContext();
Fariborz Jahanian624b2992011-06-09 19:25:01 +00001171 if (isa<ObjCMethodDecl>(DC) && isa<FunctionDecl>(ND)) {
1172 // Don't add objc method name mangling to locally declared function
1173 mangleUnqualifiedName(ND);
1174 return;
1175 }
1176
Anders Carlsson4eca1092009-04-02 16:24:45 +00001177 Out << 'Z';
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00001178
Charles Davisc9e78142010-05-26 18:25:27 +00001179 if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(DC)) {
1180 mangleObjCMethodName(MD);
John McCall18e4eda2010-10-18 21:28:44 +00001181 } else if (const CXXRecordDecl *RD = GetLocalClassDecl(ND)) {
1182 mangleFunctionEncoding(cast<FunctionDecl>(RD->getDeclContext()));
Fariborz Jahaniana529c252010-03-03 19:41:08 +00001183 Out << 'E';
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00001184
John McCall18e4eda2010-10-18 21:28:44 +00001185 // Mangle the name relative to the closest enclosing function.
1186 if (ND == RD) // equality ok because RD derived from ND above
1187 mangleUnqualifiedName(ND);
1188 else
1189 mangleNestedName(ND, DC, true /*NoFunction*/);
1190
Fariborz Jahanian9eba9df2010-03-04 01:02:03 +00001191 unsigned disc;
John McCall18e4eda2010-10-18 21:28:44 +00001192 if (Context.getNextDiscriminator(RD, disc)) {
Fariborz Jahanian9eba9df2010-03-04 01:02:03 +00001193 if (disc < 10)
1194 Out << '_' << disc;
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00001195 else
Fariborz Jahanian9eba9df2010-03-04 01:02:03 +00001196 Out << "__" << disc << '_';
1197 }
Fariborz Jahaniana529c252010-03-03 19:41:08 +00001198
1199 return;
1200 }
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00001201 else
Fariborz Jahaniana529c252010-03-03 19:41:08 +00001202 mangleFunctionEncoding(cast<FunctionDecl>(DC));
Anders Carlssonbf5694602009-12-10 03:14:39 +00001203
Anders Carlsson4eca1092009-04-02 16:24:45 +00001204 Out << 'E';
Eli Friedman75c9b972009-12-11 20:21:38 +00001205 mangleUnqualifiedName(ND);
Anders Carlsson4eca1092009-04-02 16:24:45 +00001206}
1207
John McCall6dc0a2b2011-04-24 08:23:24 +00001208void CXXNameMangler::manglePrefix(NestedNameSpecifier *qualifier) {
1209 switch (qualifier->getKind()) {
1210 case NestedNameSpecifier::Global:
1211 // nothing
1212 return;
1213
1214 case NestedNameSpecifier::Namespace:
1215 mangleName(qualifier->getAsNamespace());
1216 return;
1217
1218 case NestedNameSpecifier::NamespaceAlias:
1219 mangleName(qualifier->getAsNamespaceAlias()->getNamespace());
1220 return;
1221
1222 case NestedNameSpecifier::TypeSpec:
1223 case NestedNameSpecifier::TypeSpecWithTemplate:
John McCall3ab84762011-05-04 01:45:19 +00001224 manglePrefix(QualType(qualifier->getAsType(), 0));
John McCall6dc0a2b2011-04-24 08:23:24 +00001225 return;
1226
1227 case NestedNameSpecifier::Identifier:
1228 // Member expressions can have these without prefixes, but that
1229 // should end up in mangleUnresolvedPrefix instead.
1230 assert(qualifier->getPrefix());
1231 manglePrefix(qualifier->getPrefix());
1232
1233 mangleSourceName(qualifier->getAsIdentifier());
1234 return;
1235 }
1236
1237 llvm_unreachable("unexpected nested name specifier");
1238}
1239
Fariborz Jahaniana529c252010-03-03 19:41:08 +00001240void CXXNameMangler::manglePrefix(const DeclContext *DC, bool NoFunction) {
Douglas Gregor5fec5b02009-02-13 00:10:09 +00001241 // <prefix> ::= <prefix> <unqualified-name>
1242 // ::= <template-prefix> <template-args>
1243 // ::= <template-param>
1244 // ::= # empty
1245 // ::= <substitution>
Anders Carlsson4245bf92009-09-17 04:16:28 +00001246
Anders Carlsson1d3b6f62009-09-22 20:33:31 +00001247 while (isa<LinkageSpecDecl>(DC))
1248 DC = DC->getParent();
Daniel Dunbard614e322009-11-21 09:05:47 +00001249
Anders Carlssonc1370cb2009-09-18 18:39:58 +00001250 if (DC->isTranslationUnit())
1251 return;
Daniel Dunbard614e322009-11-21 09:05:47 +00001252
Douglas Gregor7e10f362010-05-25 17:04:15 +00001253 if (const BlockDecl *Block = dyn_cast<BlockDecl>(DC)) {
1254 manglePrefix(DC->getParent(), NoFunction);
1255 llvm::SmallString<64> Name;
Rafael Espindolaac00f5d2011-02-10 23:59:36 +00001256 llvm::raw_svector_ostream NameStream(Name);
1257 Context.mangleBlock(Block, NameStream);
1258 NameStream.flush();
Douglas Gregor7e10f362010-05-25 17:04:15 +00001259 Out << Name.size() << Name;
1260 return;
1261 }
1262
Anders Carlsson4245bf92009-09-17 04:16:28 +00001263 if (mangleSubstitution(cast<NamedDecl>(DC)))
1264 return;
Anders Carlsson2b5e1dd2009-09-18 04:29:09 +00001265
Anders Carlsson82b688e2009-09-18 20:11:09 +00001266 // Check if we have a template.
1267 const TemplateArgumentList *TemplateArgs = 0;
Daniel Dunbard614e322009-11-21 09:05:47 +00001268 if (const TemplateDecl *TD = isTemplate(cast<NamedDecl>(DC), TemplateArgs)) {
Anders Carlsson82b688e2009-09-18 20:11:09 +00001269 mangleTemplatePrefix(TD);
Rafael Espindola4d5c3d92010-03-11 14:07:00 +00001270 TemplateParameterList *TemplateParameters = TD->getTemplateParameters();
1271 mangleTemplateArgs(*TemplateParameters, *TemplateArgs);
Fariborz Jahaniana529c252010-03-03 19:41:08 +00001272 }
Douglas Gregor7e10f362010-05-25 17:04:15 +00001273 else if(NoFunction && (isa<FunctionDecl>(DC) || isa<ObjCMethodDecl>(DC)))
Fariborz Jahaniana529c252010-03-03 19:41:08 +00001274 return;
Douglas Gregor7e10f362010-05-25 17:04:15 +00001275 else if (const ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(DC))
1276 mangleObjCMethodName(Method);
Fariborz Jahaniana529c252010-03-03 19:41:08 +00001277 else {
1278 manglePrefix(DC->getParent(), NoFunction);
Anders Carlsson82b688e2009-09-18 20:11:09 +00001279 mangleUnqualifiedName(cast<NamedDecl>(DC));
1280 }
Daniel Dunbard614e322009-11-21 09:05:47 +00001281
Anders Carlsson4245bf92009-09-17 04:16:28 +00001282 addSubstitution(cast<NamedDecl>(DC));
Douglas Gregor5fec5b02009-02-13 00:10:09 +00001283}
1284
Douglas Gregor17362712010-04-23 03:10:43 +00001285void CXXNameMangler::mangleTemplatePrefix(TemplateName Template) {
1286 // <template-prefix> ::= <prefix> <template unqualified-name>
1287 // ::= <template-param>
1288 // ::= <substitution>
1289 if (TemplateDecl *TD = Template.getAsTemplateDecl())
1290 return mangleTemplatePrefix(TD);
1291
1292 if (QualifiedTemplateName *Qualified = Template.getAsQualifiedTemplateName())
John McCall6dc0a2b2011-04-24 08:23:24 +00001293 manglePrefix(Qualified->getQualifier());
Alexis Hunta8136cc2010-05-05 15:23:54 +00001294
Douglas Gregor17362712010-04-23 03:10:43 +00001295 if (OverloadedTemplateStorage *Overloaded
1296 = Template.getAsOverloadedTemplate()) {
Alexis Hunta8136cc2010-05-05 15:23:54 +00001297 mangleUnqualifiedName(0, (*Overloaded->begin())->getDeclName(),
Douglas Gregor17362712010-04-23 03:10:43 +00001298 UnknownArity);
1299 return;
1300 }
Alexis Hunta8136cc2010-05-05 15:23:54 +00001301
Douglas Gregor17362712010-04-23 03:10:43 +00001302 DependentTemplateName *Dependent = Template.getAsDependentTemplateName();
1303 assert(Dependent && "Unknown template name kind?");
John McCall6dc0a2b2011-04-24 08:23:24 +00001304 manglePrefix(Dependent->getQualifier());
Douglas Gregor7988def2010-04-28 05:58:56 +00001305 mangleUnscopedTemplateName(Template);
Douglas Gregor17362712010-04-23 03:10:43 +00001306}
1307
Anders Carlsson26e67af2009-09-26 19:45:45 +00001308void CXXNameMangler::mangleTemplatePrefix(const TemplateDecl *ND) {
Anders Carlsson2b5e1dd2009-09-18 04:29:09 +00001309 // <template-prefix> ::= <prefix> <template unqualified-name>
1310 // ::= <template-param>
1311 // ::= <substitution>
Douglas Gregora16b0ca2010-02-05 20:45:00 +00001312 // <template-template-param> ::= <template-param>
1313 // <substitution>
Anders Carlsson2b5e1dd2009-09-18 04:29:09 +00001314
Anders Carlsson3e83c302009-09-26 22:18:22 +00001315 if (mangleSubstitution(ND))
1316 return;
Daniel Dunbard614e322009-11-21 09:05:47 +00001317
Douglas Gregora16b0ca2010-02-05 20:45:00 +00001318 // <template-template-param> ::= <template-param>
1319 if (const TemplateTemplateParmDecl *TTP
1320 = dyn_cast<TemplateTemplateParmDecl>(ND)) {
1321 mangleTemplateParameter(TTP->getIndex());
1322 return;
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00001323 }
Daniel Dunbard614e322009-11-21 09:05:47 +00001324
Anders Carlssoncaf4a642009-09-18 18:47:07 +00001325 manglePrefix(ND->getDeclContext());
Anders Carlsson49232b92009-09-26 20:13:56 +00001326 mangleUnqualifiedName(ND->getTemplatedDecl());
Anders Carlsson3e83c302009-09-26 22:18:22 +00001327 addSubstitution(ND);
Anders Carlsson2b5e1dd2009-09-18 04:29:09 +00001328}
1329
John McCall4f3b5f32010-07-14 06:43:17 +00001330/// Mangles a template name under the production <type>. Required for
1331/// template template arguments.
1332/// <type> ::= <class-enum-type>
1333/// ::= <template-param>
1334/// ::= <substitution>
1335void CXXNameMangler::mangleType(TemplateName TN) {
1336 if (mangleSubstitution(TN))
1337 return;
1338
1339 TemplateDecl *TD = 0;
1340
1341 switch (TN.getKind()) {
1342 case TemplateName::QualifiedTemplate:
1343 TD = TN.getAsQualifiedTemplateName()->getTemplateDecl();
1344 goto HaveDecl;
1345
1346 case TemplateName::Template:
1347 TD = TN.getAsTemplateDecl();
1348 goto HaveDecl;
1349
1350 HaveDecl:
1351 if (isa<TemplateTemplateParmDecl>(TD))
1352 mangleTemplateParameter(cast<TemplateTemplateParmDecl>(TD)->getIndex());
1353 else
1354 mangleName(TD);
1355 break;
1356
1357 case TemplateName::OverloadedTemplate:
1358 llvm_unreachable("can't mangle an overloaded template name as a <type>");
1359 break;
1360
1361 case TemplateName::DependentTemplate: {
1362 const DependentTemplateName *Dependent = TN.getAsDependentTemplateName();
1363 assert(Dependent->isIdentifier());
1364
1365 // <class-enum-type> ::= <name>
1366 // <name> ::= <nested-name>
John McCall6dc0a2b2011-04-24 08:23:24 +00001367 mangleUnresolvedPrefix(Dependent->getQualifier(), 0);
John McCall4f3b5f32010-07-14 06:43:17 +00001368 mangleSourceName(Dependent->getIdentifier());
1369 break;
1370 }
1371
Douglas Gregor5590be02011-01-15 06:45:20 +00001372 case TemplateName::SubstTemplateTemplateParmPack: {
1373 SubstTemplateTemplateParmPackStorage *SubstPack
1374 = TN.getAsSubstTemplateTemplateParmPack();
1375 mangleTemplateParameter(SubstPack->getParameterPack()->getIndex());
1376 break;
1377 }
John McCall4f3b5f32010-07-14 06:43:17 +00001378 }
1379
1380 addSubstitution(TN);
1381}
1382
Mike Stump11289f42009-09-09 15:08:12 +00001383void
Douglas Gregor5fec5b02009-02-13 00:10:09 +00001384CXXNameMangler::mangleOperatorName(OverloadedOperatorKind OO, unsigned Arity) {
1385 switch (OO) {
Sebastian Redl1a99f442009-04-16 17:51:27 +00001386 // <operator-name> ::= nw # new
Douglas Gregor5fec5b02009-02-13 00:10:09 +00001387 case OO_New: Out << "nw"; break;
1388 // ::= na # new[]
1389 case OO_Array_New: Out << "na"; break;
Sebastian Redl1a99f442009-04-16 17:51:27 +00001390 // ::= dl # delete
Douglas Gregor5fec5b02009-02-13 00:10:09 +00001391 case OO_Delete: Out << "dl"; break;
Sebastian Redl1a99f442009-04-16 17:51:27 +00001392 // ::= da # delete[]
Douglas Gregor5fec5b02009-02-13 00:10:09 +00001393 case OO_Array_Delete: Out << "da"; break;
1394 // ::= ps # + (unary)
John McCall78fbb612010-08-18 19:18:59 +00001395 // ::= pl # + (binary or unknown)
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00001396 case OO_Plus:
Anders Carlsson8a35c792009-12-22 06:36:32 +00001397 Out << (Arity == 1? "ps" : "pl"); break;
Sebastian Redl1a99f442009-04-16 17:51:27 +00001398 // ::= ng # - (unary)
John McCall78fbb612010-08-18 19:18:59 +00001399 // ::= mi # - (binary or unknown)
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00001400 case OO_Minus:
Anders Carlsson8a35c792009-12-22 06:36:32 +00001401 Out << (Arity == 1? "ng" : "mi"); break;
Sebastian Redl1a99f442009-04-16 17:51:27 +00001402 // ::= ad # & (unary)
John McCall78fbb612010-08-18 19:18:59 +00001403 // ::= an # & (binary or unknown)
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00001404 case OO_Amp:
Anders Carlsson8a35c792009-12-22 06:36:32 +00001405 Out << (Arity == 1? "ad" : "an"); break;
Sebastian Redl1a99f442009-04-16 17:51:27 +00001406 // ::= de # * (unary)
John McCall78fbb612010-08-18 19:18:59 +00001407 // ::= ml # * (binary or unknown)
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00001408 case OO_Star:
John McCall78fbb612010-08-18 19:18:59 +00001409 // Use binary when unknown.
Anders Carlsson8a35c792009-12-22 06:36:32 +00001410 Out << (Arity == 1? "de" : "ml"); break;
Sebastian Redl1a99f442009-04-16 17:51:27 +00001411 // ::= co # ~
Douglas Gregor5fec5b02009-02-13 00:10:09 +00001412 case OO_Tilde: Out << "co"; break;
Sebastian Redl1a99f442009-04-16 17:51:27 +00001413 // ::= dv # /
Douglas Gregor5fec5b02009-02-13 00:10:09 +00001414 case OO_Slash: Out << "dv"; break;
Sebastian Redl1a99f442009-04-16 17:51:27 +00001415 // ::= rm # %
Douglas Gregor5fec5b02009-02-13 00:10:09 +00001416 case OO_Percent: Out << "rm"; break;
Sebastian Redl1a99f442009-04-16 17:51:27 +00001417 // ::= or # |
1418 case OO_Pipe: Out << "or"; break;
1419 // ::= eo # ^
Douglas Gregor5fec5b02009-02-13 00:10:09 +00001420 case OO_Caret: Out << "eo"; break;
Sebastian Redl1a99f442009-04-16 17:51:27 +00001421 // ::= aS # =
Douglas Gregor5fec5b02009-02-13 00:10:09 +00001422 case OO_Equal: Out << "aS"; break;
Sebastian Redl1a99f442009-04-16 17:51:27 +00001423 // ::= pL # +=
Douglas Gregor5fec5b02009-02-13 00:10:09 +00001424 case OO_PlusEqual: Out << "pL"; break;
Sebastian Redl1a99f442009-04-16 17:51:27 +00001425 // ::= mI # -=
Douglas Gregor5fec5b02009-02-13 00:10:09 +00001426 case OO_MinusEqual: Out << "mI"; break;
Sebastian Redl1a99f442009-04-16 17:51:27 +00001427 // ::= mL # *=
Douglas Gregor5fec5b02009-02-13 00:10:09 +00001428 case OO_StarEqual: Out << "mL"; break;
Sebastian Redl1a99f442009-04-16 17:51:27 +00001429 // ::= dV # /=
Douglas Gregor5fec5b02009-02-13 00:10:09 +00001430 case OO_SlashEqual: Out << "dV"; break;
Sebastian Redl1a99f442009-04-16 17:51:27 +00001431 // ::= rM # %=
1432 case OO_PercentEqual: Out << "rM"; break;
1433 // ::= aN # &=
1434 case OO_AmpEqual: Out << "aN"; break;
1435 // ::= oR # |=
1436 case OO_PipeEqual: Out << "oR"; break;
1437 // ::= eO # ^=
1438 case OO_CaretEqual: Out << "eO"; break;
1439 // ::= ls # <<
Douglas Gregor5fec5b02009-02-13 00:10:09 +00001440 case OO_LessLess: Out << "ls"; break;
Sebastian Redl1a99f442009-04-16 17:51:27 +00001441 // ::= rs # >>
1442 case OO_GreaterGreater: Out << "rs"; break;
1443 // ::= lS # <<=
1444 case OO_LessLessEqual: Out << "lS"; break;
1445 // ::= rS # >>=
1446 case OO_GreaterGreaterEqual: Out << "rS"; break;
Douglas Gregor5fec5b02009-02-13 00:10:09 +00001447 // ::= eq # ==
1448 case OO_EqualEqual: Out << "eq"; break;
Sebastian Redl1a99f442009-04-16 17:51:27 +00001449 // ::= ne # !=
1450 case OO_ExclaimEqual: Out << "ne"; break;
1451 // ::= lt # <
Douglas Gregor5fec5b02009-02-13 00:10:09 +00001452 case OO_Less: Out << "lt"; break;
Sebastian Redl1a99f442009-04-16 17:51:27 +00001453 // ::= gt # >
Douglas Gregor5fec5b02009-02-13 00:10:09 +00001454 case OO_Greater: Out << "gt"; break;
Sebastian Redl1a99f442009-04-16 17:51:27 +00001455 // ::= le # <=
Douglas Gregor5fec5b02009-02-13 00:10:09 +00001456 case OO_LessEqual: Out << "le"; break;
Sebastian Redl1a99f442009-04-16 17:51:27 +00001457 // ::= ge # >=
Douglas Gregor5fec5b02009-02-13 00:10:09 +00001458 case OO_GreaterEqual: Out << "ge"; break;
Sebastian Redl1a99f442009-04-16 17:51:27 +00001459 // ::= nt # !
Douglas Gregor5fec5b02009-02-13 00:10:09 +00001460 case OO_Exclaim: Out << "nt"; break;
Sebastian Redl1a99f442009-04-16 17:51:27 +00001461 // ::= aa # &&
Douglas Gregor5fec5b02009-02-13 00:10:09 +00001462 case OO_AmpAmp: Out << "aa"; break;
Sebastian Redl1a99f442009-04-16 17:51:27 +00001463 // ::= oo # ||
1464 case OO_PipePipe: Out << "oo"; break;
1465 // ::= pp # ++
1466 case OO_PlusPlus: Out << "pp"; break;
1467 // ::= mm # --
Douglas Gregor5fec5b02009-02-13 00:10:09 +00001468 case OO_MinusMinus: Out << "mm"; break;
Sebastian Redl1a99f442009-04-16 17:51:27 +00001469 // ::= cm # ,
1470 case OO_Comma: Out << "cm"; break;
1471 // ::= pm # ->*
Douglas Gregor5fec5b02009-02-13 00:10:09 +00001472 case OO_ArrowStar: Out << "pm"; break;
Sebastian Redl1a99f442009-04-16 17:51:27 +00001473 // ::= pt # ->
Douglas Gregor5fec5b02009-02-13 00:10:09 +00001474 case OO_Arrow: Out << "pt"; break;
Sebastian Redl1a99f442009-04-16 17:51:27 +00001475 // ::= cl # ()
Douglas Gregor5fec5b02009-02-13 00:10:09 +00001476 case OO_Call: Out << "cl"; break;
Sebastian Redl1a99f442009-04-16 17:51:27 +00001477 // ::= ix # []
Douglas Gregor5fec5b02009-02-13 00:10:09 +00001478 case OO_Subscript: Out << "ix"; break;
Anders Carlssone66e2942009-12-14 01:45:37 +00001479
1480 // ::= qu # ?
1481 // The conditional operator can't be overloaded, but we still handle it when
1482 // mangling expressions.
1483 case OO_Conditional: Out << "qu"; break;
Douglas Gregor5fec5b02009-02-13 00:10:09 +00001484
Sebastian Redl1a99f442009-04-16 17:51:27 +00001485 case OO_None:
Douglas Gregor5fec5b02009-02-13 00:10:09 +00001486 case NUM_OVERLOADED_OPERATORS:
Mike Stump11289f42009-09-09 15:08:12 +00001487 assert(false && "Not an overloaded operator");
Douglas Gregor5fec5b02009-02-13 00:10:09 +00001488 break;
1489 }
1490}
1491
John McCall8ccfcb52009-09-24 19:53:00 +00001492void CXXNameMangler::mangleQualifiers(Qualifiers Quals) {
Mike Stump11289f42009-09-09 15:08:12 +00001493 // <CV-qualifiers> ::= [r] [V] [K] # restrict (C99), volatile, const
John McCall8ccfcb52009-09-24 19:53:00 +00001494 if (Quals.hasRestrict())
Douglas Gregor5fec5b02009-02-13 00:10:09 +00001495 Out << 'r';
John McCall8ccfcb52009-09-24 19:53:00 +00001496 if (Quals.hasVolatile())
Douglas Gregor5fec5b02009-02-13 00:10:09 +00001497 Out << 'V';
John McCall8ccfcb52009-09-24 19:53:00 +00001498 if (Quals.hasConst())
Douglas Gregor5fec5b02009-02-13 00:10:09 +00001499 Out << 'K';
John McCall8ccfcb52009-09-24 19:53:00 +00001500
Douglas Gregor1726d152010-06-14 23:15:08 +00001501 if (Quals.hasAddressSpace()) {
1502 // Extension:
1503 //
1504 // <type> ::= U <address-space-number>
1505 //
1506 // where <address-space-number> is a source name consisting of 'AS'
1507 // followed by the address space <number>.
1508 llvm::SmallString<64> ASString;
1509 ASString = "AS" + llvm::utostr_32(Quals.getAddressSpace());
1510 Out << 'U' << ASString.size() << ASString;
1511 }
1512
John McCall31168b02011-06-15 23:02:42 +00001513 llvm::StringRef LifetimeName;
1514 switch (Quals.getObjCLifetime()) {
1515 // Objective-C ARC Extension:
1516 //
1517 // <type> ::= U "__strong"
1518 // <type> ::= U "__weak"
1519 // <type> ::= U "__autoreleasing"
John McCall31168b02011-06-15 23:02:42 +00001520 case Qualifiers::OCL_None:
1521 break;
1522
1523 case Qualifiers::OCL_Weak:
1524 LifetimeName = "__weak";
1525 break;
1526
1527 case Qualifiers::OCL_Strong:
1528 LifetimeName = "__strong";
1529 break;
1530
1531 case Qualifiers::OCL_Autoreleasing:
1532 LifetimeName = "__autoreleasing";
1533 break;
1534
1535 case Qualifiers::OCL_ExplicitNone:
Douglas Gregorb5176a52011-06-17 22:26:49 +00001536 // The __unsafe_unretained qualifier is *not* mangled, so that
1537 // __unsafe_unretained types in ARC produce the same manglings as the
1538 // equivalent (but, naturally, unqualified) types in non-ARC, providing
1539 // better ABI compatibility.
1540 //
1541 // It's safe to do this because unqualified 'id' won't show up
1542 // in any type signatures that need to be mangled.
John McCall31168b02011-06-15 23:02:42 +00001543 break;
1544 }
1545 if (!LifetimeName.empty())
1546 Out << 'U' << LifetimeName.size() << LifetimeName;
Douglas Gregor5fec5b02009-02-13 00:10:09 +00001547}
1548
Douglas Gregorf3ea1ed2011-01-26 17:36:28 +00001549void CXXNameMangler::mangleRefQualifier(RefQualifierKind RefQualifier) {
1550 // <ref-qualifier> ::= R # lvalue reference
1551 // ::= O # rvalue-reference
1552 // Proposal to Itanium C++ ABI list on 1/26/11
1553 switch (RefQualifier) {
1554 case RQ_None:
1555 break;
1556
1557 case RQ_LValue:
1558 Out << 'R';
1559 break;
1560
1561 case RQ_RValue:
1562 Out << 'O';
1563 break;
1564 }
1565}
1566
Anders Carlssonbf5694602009-12-10 03:14:39 +00001567void CXXNameMangler::mangleObjCMethodName(const ObjCMethodDecl *MD) {
Rafael Espindola3968cd02011-02-11 02:52:17 +00001568 Context.mangleObjCMethodName(MD, Out);
Anders Carlssonbf5694602009-12-10 03:14:39 +00001569}
1570
John McCall5143d642011-01-26 20:05:40 +00001571void CXXNameMangler::mangleType(QualType nonCanon) {
Anders Carlsson02751152009-03-10 17:07:44 +00001572 // Only operate on the canonical type!
John McCall5143d642011-01-26 20:05:40 +00001573 QualType canon = nonCanon.getCanonicalType();
Anders Carlsson02751152009-03-10 17:07:44 +00001574
John McCall5143d642011-01-26 20:05:40 +00001575 SplitQualType split = canon.split();
1576 Qualifiers quals = split.second;
1577 const Type *ty = split.first;
1578
1579 bool isSubstitutable = quals || !isa<BuiltinType>(ty);
1580 if (isSubstitutable && mangleSubstitution(canon))
Anders Carlssonfeb60502009-09-17 00:43:46 +00001581 return;
Douglas Gregor5fec5b02009-02-13 00:10:09 +00001582
John McCall5143d642011-01-26 20:05:40 +00001583 // If we're mangling a qualified array type, push the qualifiers to
1584 // the element type.
1585 if (quals && isa<ArrayType>(ty)) {
1586 ty = Context.getASTContext().getAsArrayType(canon);
1587 quals = Qualifiers();
1588
1589 // Note that we don't update canon: we want to add the
1590 // substitution at the canonical type.
1591 }
1592
1593 if (quals) {
1594 mangleQualifiers(quals);
John McCall8ccfcb52009-09-24 19:53:00 +00001595 // Recurse: even if the qualified type isn't yet substitutable,
1596 // the unqualified type might be.
John McCall5143d642011-01-26 20:05:40 +00001597 mangleType(QualType(ty, 0));
Anders Carlssonfeb60502009-09-17 00:43:46 +00001598 } else {
John McCall5143d642011-01-26 20:05:40 +00001599 switch (ty->getTypeClass()) {
John McCallcc5e23c2009-09-05 07:56:18 +00001600#define ABSTRACT_TYPE(CLASS, PARENT)
1601#define NON_CANONICAL_TYPE(CLASS, PARENT) \
Anders Carlssonfeb60502009-09-17 00:43:46 +00001602 case Type::CLASS: \
Jeffrey Yasskin1615d452009-12-12 05:05:38 +00001603 llvm_unreachable("can't mangle non-canonical type " #CLASS "Type"); \
Anders Carlssonfeb60502009-09-17 00:43:46 +00001604 return;
John McCallcc5e23c2009-09-05 07:56:18 +00001605#define TYPE(CLASS, PARENT) \
Anders Carlssonfeb60502009-09-17 00:43:46 +00001606 case Type::CLASS: \
John McCall5143d642011-01-26 20:05:40 +00001607 mangleType(static_cast<const CLASS##Type*>(ty)); \
Anders Carlssonfeb60502009-09-17 00:43:46 +00001608 break;
John McCallcc5e23c2009-09-05 07:56:18 +00001609#include "clang/AST/TypeNodes.def"
Anders Carlssonfeb60502009-09-17 00:43:46 +00001610 }
Douglas Gregor5fec5b02009-02-13 00:10:09 +00001611 }
Anders Carlssonfeb60502009-09-17 00:43:46 +00001612
1613 // Add the substitution.
John McCall5143d642011-01-26 20:05:40 +00001614 if (isSubstitutable)
1615 addSubstitution(canon);
Douglas Gregor5fec5b02009-02-13 00:10:09 +00001616}
1617
Douglas Gregor0094eb22010-05-26 05:11:13 +00001618void CXXNameMangler::mangleNameOrStandardSubstitution(const NamedDecl *ND) {
1619 if (!mangleStandardSubstitution(ND))
1620 mangleName(ND);
1621}
1622
Douglas Gregor5fec5b02009-02-13 00:10:09 +00001623void CXXNameMangler::mangleType(const BuiltinType *T) {
John McCallcc5e23c2009-09-05 07:56:18 +00001624 // <type> ::= <builtin-type>
Douglas Gregor5fec5b02009-02-13 00:10:09 +00001625 // <builtin-type> ::= v # void
1626 // ::= w # wchar_t
1627 // ::= b # bool
1628 // ::= c # char
1629 // ::= a # signed char
1630 // ::= h # unsigned char
1631 // ::= s # short
1632 // ::= t # unsigned short
1633 // ::= i # int
1634 // ::= j # unsigned int
1635 // ::= l # long
1636 // ::= m # unsigned long
1637 // ::= x # long long, __int64
1638 // ::= y # unsigned long long, __int64
1639 // ::= n # __int128
1640 // UNSUPPORTED: ::= o # unsigned __int128
1641 // ::= f # float
1642 // ::= d # double
1643 // ::= e # long double, __float80
1644 // UNSUPPORTED: ::= g # __float128
Douglas Gregor5fec5b02009-02-13 00:10:09 +00001645 // UNSUPPORTED: ::= Dd # IEEE 754r decimal floating point (64 bits)
1646 // UNSUPPORTED: ::= De # IEEE 754r decimal floating point (128 bits)
1647 // UNSUPPORTED: ::= Df # IEEE 754r decimal floating point (32 bits)
1648 // UNSUPPORTED: ::= Dh # IEEE 754r half-precision floating point (16 bits)
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00001649 // ::= Di # char32_t
1650 // ::= Ds # char16_t
Anders Carlsson2683fd62010-11-04 04:31:32 +00001651 // ::= Dn # std::nullptr_t (i.e., decltype(nullptr))
Douglas Gregor5fec5b02009-02-13 00:10:09 +00001652 // ::= u <source-name> # vendor extended type
1653 switch (T->getKind()) {
1654 case BuiltinType::Void: Out << 'v'; break;
1655 case BuiltinType::Bool: Out << 'b'; break;
1656 case BuiltinType::Char_U: case BuiltinType::Char_S: Out << 'c'; break;
1657 case BuiltinType::UChar: Out << 'h'; break;
1658 case BuiltinType::UShort: Out << 't'; break;
1659 case BuiltinType::UInt: Out << 'j'; break;
1660 case BuiltinType::ULong: Out << 'm'; break;
1661 case BuiltinType::ULongLong: Out << 'y'; break;
Chris Lattnerf122cef2009-04-30 02:43:43 +00001662 case BuiltinType::UInt128: Out << 'o'; break;
Douglas Gregor5fec5b02009-02-13 00:10:09 +00001663 case BuiltinType::SChar: Out << 'a'; break;
Chris Lattnerad3467e2010-12-25 23:25:43 +00001664 case BuiltinType::WChar_S:
1665 case BuiltinType::WChar_U: Out << 'w'; break;
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00001666 case BuiltinType::Char16: Out << "Ds"; break;
1667 case BuiltinType::Char32: Out << "Di"; break;
Douglas Gregor5fec5b02009-02-13 00:10:09 +00001668 case BuiltinType::Short: Out << 's'; break;
1669 case BuiltinType::Int: Out << 'i'; break;
1670 case BuiltinType::Long: Out << 'l'; break;
1671 case BuiltinType::LongLong: Out << 'x'; break;
Chris Lattnerf122cef2009-04-30 02:43:43 +00001672 case BuiltinType::Int128: Out << 'n'; break;
Douglas Gregor5fec5b02009-02-13 00:10:09 +00001673 case BuiltinType::Float: Out << 'f'; break;
1674 case BuiltinType::Double: Out << 'd'; break;
1675 case BuiltinType::LongDouble: Out << 'e'; break;
Anders Carlsson2683fd62010-11-04 04:31:32 +00001676 case BuiltinType::NullPtr: Out << "Dn"; break;
Douglas Gregor5fec5b02009-02-13 00:10:09 +00001677
1678 case BuiltinType::Overload:
1679 case BuiltinType::Dependent:
John McCall0009fcc2011-04-26 20:42:42 +00001680 case BuiltinType::BoundMember:
John McCall31996342011-04-07 08:22:57 +00001681 case BuiltinType::UnknownAny:
John McCall8fb0d9d2011-05-01 22:35:37 +00001682 llvm_unreachable("mangling a placeholder type");
Douglas Gregor5fec5b02009-02-13 00:10:09 +00001683 break;
Steve Naroff3de6b702009-07-22 17:14:51 +00001684 case BuiltinType::ObjCId: Out << "11objc_object"; break;
1685 case BuiltinType::ObjCClass: Out << "10objc_class"; break;
Fariborz Jahanian252ba5f2009-11-21 19:53:08 +00001686 case BuiltinType::ObjCSel: Out << "13objc_selector"; break;
Douglas Gregor5fec5b02009-02-13 00:10:09 +00001687 }
1688}
1689
John McCallcc5e23c2009-09-05 07:56:18 +00001690// <type> ::= <function-type>
1691// <function-type> ::= F [Y] <bare-function-type> E
1692void CXXNameMangler::mangleType(const FunctionProtoType *T) {
Douglas Gregor5fec5b02009-02-13 00:10:09 +00001693 Out << 'F';
Mike Stump18bb9282009-05-16 07:57:57 +00001694 // FIXME: We don't have enough information in the AST to produce the 'Y'
1695 // encoding for extern "C" function types.
Douglas Gregor5fec5b02009-02-13 00:10:09 +00001696 mangleBareFunctionType(T, /*MangleReturnType=*/true);
1697 Out << 'E';
1698}
John McCallcc5e23c2009-09-05 07:56:18 +00001699void CXXNameMangler::mangleType(const FunctionNoProtoType *T) {
Jeffrey Yasskin1615d452009-12-12 05:05:38 +00001700 llvm_unreachable("Can't mangle K&R function prototypes");
John McCallcc5e23c2009-09-05 07:56:18 +00001701}
Douglas Gregor5fec5b02009-02-13 00:10:09 +00001702void CXXNameMangler::mangleBareFunctionType(const FunctionType *T,
1703 bool MangleReturnType) {
John McCallcc5e23c2009-09-05 07:56:18 +00001704 // We should never be mangling something without a prototype.
1705 const FunctionProtoType *Proto = cast<FunctionProtoType>(T);
1706
John McCall8fb0d9d2011-05-01 22:35:37 +00001707 // Record that we're in a function type. See mangleFunctionParam
1708 // for details on what we're trying to achieve here.
1709 FunctionTypeDepthState saved = FunctionTypeDepth.push();
1710
Douglas Gregor5fec5b02009-02-13 00:10:09 +00001711 // <bare-function-type> ::= <signature type>+
John McCall8fb0d9d2011-05-01 22:35:37 +00001712 if (MangleReturnType) {
1713 FunctionTypeDepth.enterResultType();
John McCallcc5e23c2009-09-05 07:56:18 +00001714 mangleType(Proto->getResultType());
John McCall8fb0d9d2011-05-01 22:35:37 +00001715 FunctionTypeDepth.leaveResultType();
1716 }
Douglas Gregor5fec5b02009-02-13 00:10:09 +00001717
Anders Carlsson728fe442010-06-02 04:40:13 +00001718 if (Proto->getNumArgs() == 0 && !Proto->isVariadic()) {
Eli Friedman04831922010-08-22 01:00:03 +00001719 // <builtin-type> ::= v # void
Anders Carlsson7a6f8b92009-04-01 00:15:23 +00001720 Out << 'v';
John McCall8fb0d9d2011-05-01 22:35:37 +00001721
1722 FunctionTypeDepth.pop(saved);
Anders Carlsson7a6f8b92009-04-01 00:15:23 +00001723 return;
1724 }
Mike Stump11289f42009-09-09 15:08:12 +00001725
Douglas Gregordeaad8c2009-02-26 23:50:07 +00001726 for (FunctionProtoType::arg_type_iterator Arg = Proto->arg_type_begin(),
Mike Stump11289f42009-09-09 15:08:12 +00001727 ArgEnd = Proto->arg_type_end();
Douglas Gregor5fec5b02009-02-13 00:10:09 +00001728 Arg != ArgEnd; ++Arg)
1729 mangleType(*Arg);
Douglas Gregoradb02012009-02-13 01:28:03 +00001730
John McCall8fb0d9d2011-05-01 22:35:37 +00001731 FunctionTypeDepth.pop(saved);
1732
Douglas Gregoradb02012009-02-13 01:28:03 +00001733 // <builtin-type> ::= z # ellipsis
1734 if (Proto->isVariadic())
1735 Out << 'z';
Douglas Gregor5fec5b02009-02-13 00:10:09 +00001736}
1737
John McCallcc5e23c2009-09-05 07:56:18 +00001738// <type> ::= <class-enum-type>
Mike Stump11289f42009-09-09 15:08:12 +00001739// <class-enum-type> ::= <name>
John McCallb96ec562009-12-04 22:46:56 +00001740void CXXNameMangler::mangleType(const UnresolvedUsingType *T) {
1741 mangleName(T->getDecl());
1742}
1743
1744// <type> ::= <class-enum-type>
1745// <class-enum-type> ::= <name>
John McCallcc5e23c2009-09-05 07:56:18 +00001746void CXXNameMangler::mangleType(const EnumType *T) {
1747 mangleType(static_cast<const TagType*>(T));
1748}
1749void CXXNameMangler::mangleType(const RecordType *T) {
1750 mangleType(static_cast<const TagType*>(T));
1751}
Douglas Gregor5fec5b02009-02-13 00:10:09 +00001752void CXXNameMangler::mangleType(const TagType *T) {
Eli Friedman30e94d042009-12-11 18:00:57 +00001753 mangleName(T->getDecl());
Douglas Gregor5fec5b02009-02-13 00:10:09 +00001754}
1755
John McCallcc5e23c2009-09-05 07:56:18 +00001756// <type> ::= <array-type>
1757// <array-type> ::= A <positive dimension number> _ <element type>
1758// ::= A [<dimension expression>] _ <element type>
1759void CXXNameMangler::mangleType(const ConstantArrayType *T) {
1760 Out << 'A' << T->getSize() << '_';
1761 mangleType(T->getElementType());
1762}
1763void CXXNameMangler::mangleType(const VariableArrayType *T) {
Douglas Gregor5fec5b02009-02-13 00:10:09 +00001764 Out << 'A';
Fariborz Jahaniandc9bc5a2010-11-02 16:54:00 +00001765 // decayed vla types (size 0) will just be skipped.
1766 if (T->getSizeExpr())
1767 mangleExpression(T->getSizeExpr());
Douglas Gregor5fec5b02009-02-13 00:10:09 +00001768 Out << '_';
1769 mangleType(T->getElementType());
1770}
John McCallcc5e23c2009-09-05 07:56:18 +00001771void CXXNameMangler::mangleType(const DependentSizedArrayType *T) {
1772 Out << 'A';
1773 mangleExpression(T->getSizeExpr());
1774 Out << '_';
1775 mangleType(T->getElementType());
1776}
1777void CXXNameMangler::mangleType(const IncompleteArrayType *T) {
Nick Lewycky1b36b552010-09-05 03:40:33 +00001778 Out << "A_";
John McCallcc5e23c2009-09-05 07:56:18 +00001779 mangleType(T->getElementType());
1780}
Douglas Gregor5fec5b02009-02-13 00:10:09 +00001781
John McCallcc5e23c2009-09-05 07:56:18 +00001782// <type> ::= <pointer-to-member-type>
1783// <pointer-to-member-type> ::= M <class type> <member type>
Douglas Gregor5fec5b02009-02-13 00:10:09 +00001784void CXXNameMangler::mangleType(const MemberPointerType *T) {
Douglas Gregor5fec5b02009-02-13 00:10:09 +00001785 Out << 'M';
1786 mangleType(QualType(T->getClass(), 0));
Anders Carlsson23ca0b42009-05-17 17:41:20 +00001787 QualType PointeeType = T->getPointeeType();
1788 if (const FunctionProtoType *FPT = dyn_cast<FunctionProtoType>(PointeeType)) {
John McCall8ccfcb52009-09-24 19:53:00 +00001789 mangleQualifiers(Qualifiers::fromCVRMask(FPT->getTypeQuals()));
Douglas Gregorf3ea1ed2011-01-26 17:36:28 +00001790 mangleRefQualifier(FPT->getRefQualifier());
Anders Carlsson23ca0b42009-05-17 17:41:20 +00001791 mangleType(FPT);
Anders Carlssond5639232010-06-02 04:29:50 +00001792
1793 // Itanium C++ ABI 5.1.8:
1794 //
1795 // The type of a non-static member function is considered to be different,
1796 // for the purposes of substitution, from the type of a namespace-scope or
1797 // static member function whose type appears similar. The types of two
1798 // non-static member functions are considered to be different, for the
1799 // purposes of substitution, if the functions are members of different
1800 // classes. In other words, for the purposes of substitution, the class of
1801 // which the function is a member is considered part of the type of
1802 // function.
1803
1804 // We increment the SeqID here to emulate adding an entry to the
1805 // substitution table. We can't actually add it because we don't want this
1806 // particular function type to be substituted.
1807 ++SeqID;
Mike Stump11289f42009-09-09 15:08:12 +00001808 } else
Anders Carlsson23ca0b42009-05-17 17:41:20 +00001809 mangleType(PointeeType);
Douglas Gregor5fec5b02009-02-13 00:10:09 +00001810}
1811
John McCallcc5e23c2009-09-05 07:56:18 +00001812// <type> ::= <template-param>
Douglas Gregor5fec5b02009-02-13 00:10:09 +00001813void CXXNameMangler::mangleType(const TemplateTypeParmType *T) {
Anders Carlssone00745b2009-09-27 00:38:53 +00001814 mangleTemplateParameter(T->getIndex());
Douglas Gregor5fec5b02009-02-13 00:10:09 +00001815}
1816
Douglas Gregorada4b792011-01-14 02:55:32 +00001817// <type> ::= <template-param>
1818void CXXNameMangler::mangleType(const SubstTemplateTypeParmPackType *T) {
1819 mangleTemplateParameter(T->getReplacedParameter()->getIndex());
1820}
1821
John McCallcc5e23c2009-09-05 07:56:18 +00001822// <type> ::= P <type> # pointer-to
1823void CXXNameMangler::mangleType(const PointerType *T) {
1824 Out << 'P';
1825 mangleType(T->getPointeeType());
1826}
1827void CXXNameMangler::mangleType(const ObjCObjectPointerType *T) {
1828 Out << 'P';
1829 mangleType(T->getPointeeType());
1830}
1831
1832// <type> ::= R <type> # reference-to
1833void CXXNameMangler::mangleType(const LValueReferenceType *T) {
1834 Out << 'R';
1835 mangleType(T->getPointeeType());
1836}
1837
1838// <type> ::= O <type> # rvalue reference-to (C++0x)
1839void CXXNameMangler::mangleType(const RValueReferenceType *T) {
1840 Out << 'O';
1841 mangleType(T->getPointeeType());
1842}
1843
1844// <type> ::= C <type> # complex pair (C 2000)
1845void CXXNameMangler::mangleType(const ComplexType *T) {
1846 Out << 'C';
1847 mangleType(T->getElementType());
1848}
1849
Bob Wilsonc1555212010-11-12 17:24:43 +00001850// ARM's ABI for Neon vector types specifies that they should be mangled as
Bob Wilsone4652b12010-11-16 00:32:18 +00001851// if they are structs (to match ARM's initial implementation). The
1852// vector type must be one of the special types predefined by ARM.
1853void CXXNameMangler::mangleNeonVectorType(const VectorType *T) {
Bob Wilsonc1555212010-11-12 17:24:43 +00001854 QualType EltType = T->getElementType();
Bob Wilsone4652b12010-11-16 00:32:18 +00001855 assert(EltType->isBuiltinType() && "Neon vector element not a BuiltinType");
Bob Wilsonc1555212010-11-12 17:24:43 +00001856 const char *EltName = 0;
Bob Wilson8470b332010-11-12 17:24:46 +00001857 if (T->getVectorKind() == VectorType::NeonPolyVector) {
1858 switch (cast<BuiltinType>(EltType)->getKind()) {
Bob Wilsonf58e8a42010-11-12 17:24:49 +00001859 case BuiltinType::SChar: EltName = "poly8_t"; break;
1860 case BuiltinType::Short: EltName = "poly16_t"; break;
Bob Wilsone4652b12010-11-16 00:32:18 +00001861 default: llvm_unreachable("unexpected Neon polynomial vector element type");
Bob Wilson8470b332010-11-12 17:24:46 +00001862 }
1863 } else {
1864 switch (cast<BuiltinType>(EltType)->getKind()) {
Bob Wilsonf58e8a42010-11-12 17:24:49 +00001865 case BuiltinType::SChar: EltName = "int8_t"; break;
1866 case BuiltinType::UChar: EltName = "uint8_t"; break;
1867 case BuiltinType::Short: EltName = "int16_t"; break;
1868 case BuiltinType::UShort: EltName = "uint16_t"; break;
1869 case BuiltinType::Int: EltName = "int32_t"; break;
1870 case BuiltinType::UInt: EltName = "uint32_t"; break;
1871 case BuiltinType::LongLong: EltName = "int64_t"; break;
1872 case BuiltinType::ULongLong: EltName = "uint64_t"; break;
1873 case BuiltinType::Float: EltName = "float32_t"; break;
Bob Wilsone4652b12010-11-16 00:32:18 +00001874 default: llvm_unreachable("unexpected Neon vector element type");
Bob Wilson8470b332010-11-12 17:24:46 +00001875 }
Bob Wilsonc1555212010-11-12 17:24:43 +00001876 }
1877 const char *BaseName = 0;
Bob Wilsonf58e8a42010-11-12 17:24:49 +00001878 unsigned BitSize = (T->getNumElements() *
Bob Wilson59f9dec2010-11-16 00:32:12 +00001879 getASTContext().getTypeSize(EltType));
Bob Wilsonc1555212010-11-12 17:24:43 +00001880 if (BitSize == 64)
1881 BaseName = "__simd64_";
Bob Wilsone4652b12010-11-16 00:32:18 +00001882 else {
1883 assert(BitSize == 128 && "Neon vector type not 64 or 128 bits");
Bob Wilsonc1555212010-11-12 17:24:43 +00001884 BaseName = "__simd128_";
Bob Wilsone4652b12010-11-16 00:32:18 +00001885 }
Bob Wilsonc1555212010-11-12 17:24:43 +00001886 Out << strlen(BaseName) + strlen(EltName);
1887 Out << BaseName << EltName;
Bob Wilsonc1555212010-11-12 17:24:43 +00001888}
1889
John McCallcc5e23c2009-09-05 07:56:18 +00001890// GNU extension: vector types
Chris Lattner37141f42010-06-23 06:00:24 +00001891// <type> ::= <vector-type>
1892// <vector-type> ::= Dv <positive dimension number> _
1893// <extended element type>
1894// ::= Dv [<dimension expression>] _ <element type>
1895// <extended element type> ::= <element type>
1896// ::= p # AltiVec vector pixel
John McCallcc5e23c2009-09-05 07:56:18 +00001897void CXXNameMangler::mangleType(const VectorType *T) {
Bob Wilson8470b332010-11-12 17:24:46 +00001898 if ((T->getVectorKind() == VectorType::NeonVector ||
Bob Wilsone4652b12010-11-16 00:32:18 +00001899 T->getVectorKind() == VectorType::NeonPolyVector)) {
1900 mangleNeonVectorType(T);
Bob Wilsonc1555212010-11-12 17:24:43 +00001901 return;
Bob Wilsone4652b12010-11-16 00:32:18 +00001902 }
Nick Lewycky3cfe6af2010-03-26 07:18:04 +00001903 Out << "Dv" << T->getNumElements() << '_';
Bob Wilsonaeb56442010-11-10 21:56:12 +00001904 if (T->getVectorKind() == VectorType::AltiVecPixel)
Chris Lattner37141f42010-06-23 06:00:24 +00001905 Out << 'p';
Bob Wilsonaeb56442010-11-10 21:56:12 +00001906 else if (T->getVectorKind() == VectorType::AltiVecBool)
Chris Lattner37141f42010-06-23 06:00:24 +00001907 Out << 'b';
1908 else
1909 mangleType(T->getElementType());
John McCallcc5e23c2009-09-05 07:56:18 +00001910}
1911void CXXNameMangler::mangleType(const ExtVectorType *T) {
1912 mangleType(static_cast<const VectorType*>(T));
1913}
1914void CXXNameMangler::mangleType(const DependentSizedExtVectorType *T) {
Nick Lewycky3cfe6af2010-03-26 07:18:04 +00001915 Out << "Dv";
1916 mangleExpression(T->getSizeExpr());
1917 Out << '_';
John McCallcc5e23c2009-09-05 07:56:18 +00001918 mangleType(T->getElementType());
1919}
1920
Douglas Gregord2fa7662010-12-20 02:24:11 +00001921void CXXNameMangler::mangleType(const PackExpansionType *T) {
Douglas Gregord81c7c12011-01-13 16:39:34 +00001922 // <type> ::= Dp <type> # pack expansion (C++0x)
Douglas Gregorb720ff22011-01-13 17:44:36 +00001923 Out << "Dp";
Douglas Gregord2fa7662010-12-20 02:24:11 +00001924 mangleType(T->getPattern());
1925}
1926
Anders Carlsson16d5d292009-03-07 22:03:21 +00001927void CXXNameMangler::mangleType(const ObjCInterfaceType *T) {
1928 mangleSourceName(T->getDecl()->getIdentifier());
1929}
1930
John McCall8b07ec22010-05-15 11:32:37 +00001931void CXXNameMangler::mangleType(const ObjCObjectType *T) {
John McCallb0efad12010-05-15 17:06:29 +00001932 // We don't allow overloading by different protocol qualification,
1933 // so mangling them isn't necessary.
John McCall8b07ec22010-05-15 11:32:37 +00001934 mangleType(T->getBaseType());
1935}
1936
John McCallcc5e23c2009-09-05 07:56:18 +00001937void CXXNameMangler::mangleType(const BlockPointerType *T) {
Anders Carlssona88d1972009-12-23 22:31:44 +00001938 Out << "U13block_pointer";
1939 mangleType(T->getPointeeType());
John McCallcc5e23c2009-09-05 07:56:18 +00001940}
1941
John McCall2408e322010-04-27 00:57:59 +00001942void CXXNameMangler::mangleType(const InjectedClassNameType *T) {
1943 // Mangle injected class name types as if the user had written the
1944 // specialization out fully. It may not actually be possible to see
1945 // this mangling, though.
1946 mangleType(T->getInjectedSpecializationType());
1947}
1948
John McCallcc5e23c2009-09-05 07:56:18 +00001949void CXXNameMangler::mangleType(const TemplateSpecializationType *T) {
Douglas Gregor7988def2010-04-28 05:58:56 +00001950 if (TemplateDecl *TD = T->getTemplateName().getAsTemplateDecl()) {
1951 mangleName(TD, T->getArgs(), T->getNumArgs());
1952 } else {
1953 if (mangleSubstitution(QualType(T, 0)))
1954 return;
Alexis Hunta8136cc2010-05-05 15:23:54 +00001955
Douglas Gregor7988def2010-04-28 05:58:56 +00001956 mangleTemplatePrefix(T->getTemplateName());
Alexis Hunta8136cc2010-05-05 15:23:54 +00001957
Douglas Gregor7988def2010-04-28 05:58:56 +00001958 // FIXME: GCC does not appear to mangle the template arguments when
1959 // the template in question is a dependent template name. Should we
1960 // emulate that badness?
1961 mangleTemplateArgs(T->getTemplateName(), T->getArgs(), T->getNumArgs());
1962 addSubstitution(QualType(T, 0));
1963 }
John McCallcc5e23c2009-09-05 07:56:18 +00001964}
1965
Douglas Gregorc1d2d8a2010-03-31 17:34:00 +00001966void CXXNameMangler::mangleType(const DependentNameType *T) {
Anders Carlssonbdaaab42009-09-26 02:26:02 +00001967 // Typename types are always nested
1968 Out << 'N';
John McCall6dc0a2b2011-04-24 08:23:24 +00001969 manglePrefix(T->getQualifier());
John McCallc392f372010-06-11 00:33:02 +00001970 mangleSourceName(T->getIdentifier());
1971 Out << 'E';
1972}
John McCalla5dd3262010-06-09 07:26:17 +00001973
John McCallc392f372010-06-11 00:33:02 +00001974void CXXNameMangler::mangleType(const DependentTemplateSpecializationType *T) {
Douglas Gregor6e068012011-02-28 00:04:36 +00001975 // Dependently-scoped template types are nested if they have a prefix.
John McCallc392f372010-06-11 00:33:02 +00001976 Out << 'N';
1977
1978 // TODO: avoid making this TemplateName.
1979 TemplateName Prefix =
1980 getASTContext().getDependentTemplateName(T->getQualifier(),
1981 T->getIdentifier());
1982 mangleTemplatePrefix(Prefix);
1983
1984 // FIXME: GCC does not appear to mangle the template arguments when
1985 // the template in question is a dependent template name. Should we
1986 // emulate that badness?
1987 mangleTemplateArgs(Prefix, T->getArgs(), T->getNumArgs());
Anders Carlssonbdaaab42009-09-26 02:26:02 +00001988 Out << 'E';
John McCallcc5e23c2009-09-05 07:56:18 +00001989}
1990
John McCallbd8d9bd2010-03-01 23:49:17 +00001991void CXXNameMangler::mangleType(const TypeOfType *T) {
1992 // FIXME: this is pretty unsatisfactory, but there isn't an obvious
1993 // "extension with parameters" mangling.
1994 Out << "u6typeof";
1995}
1996
1997void CXXNameMangler::mangleType(const TypeOfExprType *T) {
1998 // FIXME: this is pretty unsatisfactory, but there isn't an obvious
1999 // "extension with parameters" mangling.
2000 Out << "u6typeof";
2001}
2002
2003void CXXNameMangler::mangleType(const DecltypeType *T) {
2004 Expr *E = T->getUnderlyingExpr();
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00002005
John McCallbd8d9bd2010-03-01 23:49:17 +00002006 // type ::= Dt <expression> E # decltype of an id-expression
2007 // # or class member access
2008 // ::= DT <expression> E # decltype of an expression
2009
2010 // This purports to be an exhaustive list of id-expressions and
2011 // class member accesses. Note that we do not ignore parentheses;
2012 // parentheses change the semantics of decltype for these
2013 // expressions (and cause the mangler to use the other form).
2014 if (isa<DeclRefExpr>(E) ||
2015 isa<MemberExpr>(E) ||
2016 isa<UnresolvedLookupExpr>(E) ||
2017 isa<DependentScopeDeclRefExpr>(E) ||
2018 isa<CXXDependentScopeMemberExpr>(E) ||
2019 isa<UnresolvedMemberExpr>(E))
2020 Out << "Dt";
2021 else
2022 Out << "DT";
2023 mangleExpression(E);
2024 Out << 'E';
2025}
2026
Alexis Hunte852b102011-05-24 22:41:36 +00002027void CXXNameMangler::mangleType(const UnaryTransformType *T) {
2028 // If this is dependent, we need to record that. If not, we simply
2029 // mangle it as the underlying type since they are equivalent.
2030 if (T->isDependentType()) {
2031 Out << 'U';
2032
2033 switch (T->getUTTKind()) {
2034 case UnaryTransformType::EnumUnderlyingType:
2035 Out << "3eut";
2036 break;
2037 }
2038 }
2039
2040 mangleType(T->getUnderlyingType());
2041}
2042
Richard Smith30482bc2011-02-20 03:19:35 +00002043void CXXNameMangler::mangleType(const AutoType *T) {
2044 QualType D = T->getDeducedType();
Richard Smith55038052011-02-21 20:10:02 +00002045 // <builtin-type> ::= Da # dependent auto
2046 if (D.isNull())
2047 Out << "Da";
2048 else
2049 mangleType(D);
Richard Smith30482bc2011-02-20 03:19:35 +00002050}
2051
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00002052void CXXNameMangler::mangleIntegerLiteral(QualType T,
Anders Carlssone66e2942009-12-14 01:45:37 +00002053 const llvm::APSInt &Value) {
2054 // <expr-primary> ::= L <type> <value number> E # integer literal
2055 Out << 'L';
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00002056
Anders Carlssone66e2942009-12-14 01:45:37 +00002057 mangleType(T);
2058 if (T->isBooleanType()) {
2059 // Boolean values are encoded as 0/1.
2060 Out << (Value.getBoolValue() ? '1' : '0');
2061 } else {
John McCall05ddbb82010-07-14 04:20:34 +00002062 mangleNumber(Value);
Anders Carlssone66e2942009-12-14 01:45:37 +00002063 }
2064 Out << 'E';
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00002065
Anders Carlssone66e2942009-12-14 01:45:37 +00002066}
2067
John McCalld061b442010-02-04 02:56:29 +00002068/// Mangles a member expression. Implicit accesses are not handled,
2069/// but that should be okay, because you shouldn't be able to
2070/// make an implicit access in a function template declaration.
John McCall6dc0a2b2011-04-24 08:23:24 +00002071void CXXNameMangler::mangleMemberExpr(const Expr *base,
2072 bool isArrow,
2073 NestedNameSpecifier *qualifier,
2074 NamedDecl *firstQualifierLookup,
2075 DeclarationName member,
2076 unsigned arity) {
2077 // <expression> ::= dt <expression> <unresolved-name>
2078 // ::= pt <expression> <unresolved-name>
2079 Out << (isArrow ? "pt" : "dt");
2080 mangleExpression(base);
2081 mangleUnresolvedName(qualifier, firstQualifierLookup, member, arity);
John McCalld061b442010-02-04 02:56:29 +00002082}
2083
John McCall7f0a0d52011-04-28 02:52:03 +00002084/// Look at the callee of the given call expression and determine if
2085/// it's a parenthesized id-expression which would have triggered ADL
2086/// otherwise.
2087static bool isParenthesizedADLCallee(const CallExpr *call) {
2088 const Expr *callee = call->getCallee();
2089 const Expr *fn = callee->IgnoreParens();
2090
2091 // Must be parenthesized. IgnoreParens() skips __extension__ nodes,
2092 // too, but for those to appear in the callee, it would have to be
2093 // parenthesized.
2094 if (callee == fn) return false;
2095
2096 // Must be an unresolved lookup.
2097 const UnresolvedLookupExpr *lookup = dyn_cast<UnresolvedLookupExpr>(fn);
2098 if (!lookup) return false;
2099
2100 assert(!lookup->requiresADL());
2101
2102 // Must be an unqualified lookup.
2103 if (lookup->getQualifier()) return false;
2104
2105 // Must not have found a class member. Note that if one is a class
2106 // member, they're all class members.
2107 if (lookup->getNumDecls() > 0 &&
2108 (*lookup->decls_begin())->isCXXClassMember())
2109 return false;
2110
2111 // Otherwise, ADL would have been triggered.
2112 return true;
2113}
2114
John McCall78fbb612010-08-18 19:18:59 +00002115void CXXNameMangler::mangleExpression(const Expr *E, unsigned Arity) {
Anders Carlssona18322c2009-09-21 01:21:10 +00002116 // <expression> ::= <unary operator-name> <expression>
John McCall2adddca2010-02-03 00:55:45 +00002117 // ::= <binary operator-name> <expression> <expression>
2118 // ::= <trinary operator-name> <expression> <expression> <expression>
Anders Carlssona18322c2009-09-21 01:21:10 +00002119 // ::= cv <type> expression # conversion with one argument
2120 // ::= cv <type> _ <expression>* E # conversion with a different number of arguments
Eli Friedman04831922010-08-22 01:00:03 +00002121 // ::= st <type> # sizeof (a type)
Anders Carlssona18322c2009-09-21 01:21:10 +00002122 // ::= at <type> # alignof (a type)
2123 // ::= <template-param>
2124 // ::= <function-param>
2125 // ::= sr <type> <unqualified-name> # dependent name
2126 // ::= sr <type> <unqualified-name> <template-args> # dependent template-id
Douglas Gregorc507db42011-06-05 05:27:58 +00002127 // ::= ds <expression> <expression> # expr.*expr
Anders Carlssona18322c2009-09-21 01:21:10 +00002128 // ::= sZ <template-param> # size of a parameter pack
Douglas Gregord81c7c12011-01-13 16:39:34 +00002129 // ::= sZ <function-param> # size of a function parameter pack
John McCall2adddca2010-02-03 00:55:45 +00002130 // ::= <expr-primary>
John McCall09de8ec2010-02-04 01:42:13 +00002131 // <expr-primary> ::= L <type> <value number> E # integer literal
2132 // ::= L <type <value float> E # floating literal
2133 // ::= L <mangled-name> E # external name
Anders Carlssona18322c2009-09-21 01:21:10 +00002134 switch (E->getStmtClass()) {
John McCall6936c862010-04-09 22:26:14 +00002135 case Expr::NoStmtClass:
John McCallbd066782011-02-09 08:16:59 +00002136#define ABSTRACT_STMT(Type)
John McCall6936c862010-04-09 22:26:14 +00002137#define EXPR(Type, Base)
2138#define STMT(Type, Base) \
2139 case Expr::Type##Class:
Alexis Hunt656bb312010-05-05 15:24:00 +00002140#include "clang/AST/StmtNodes.inc"
John McCall05ddbb82010-07-14 04:20:34 +00002141 // fallthrough
2142
2143 // These all can only appear in local or variable-initialization
2144 // contexts and so should never appear in a mangling.
2145 case Expr::AddrLabelExprClass:
2146 case Expr::BlockDeclRefExprClass:
2147 case Expr::CXXThisExprClass:
2148 case Expr::DesignatedInitExprClass:
2149 case Expr::ImplicitValueInitExprClass:
2150 case Expr::InitListExprClass:
2151 case Expr::ParenListExprClass:
2152 case Expr::CXXScalarValueInitExprClass:
John McCall2adddca2010-02-03 00:55:45 +00002153 llvm_unreachable("unexpected statement kind");
2154 break;
2155
John McCall05ddbb82010-07-14 04:20:34 +00002156 // FIXME: invent manglings for all these.
2157 case Expr::BlockExprClass:
2158 case Expr::CXXPseudoDestructorExprClass:
2159 case Expr::ChooseExprClass:
2160 case Expr::CompoundLiteralExprClass:
2161 case Expr::ExtVectorElementExprClass:
Peter Collingbourne91147592011-04-15 00:35:48 +00002162 case Expr::GenericSelectionExprClass:
John McCall05ddbb82010-07-14 04:20:34 +00002163 case Expr::ObjCEncodeExprClass:
John McCall05ddbb82010-07-14 04:20:34 +00002164 case Expr::ObjCIsaExprClass:
2165 case Expr::ObjCIvarRefExprClass:
2166 case Expr::ObjCMessageExprClass:
2167 case Expr::ObjCPropertyRefExprClass:
2168 case Expr::ObjCProtocolExprClass:
2169 case Expr::ObjCSelectorExprClass:
2170 case Expr::ObjCStringLiteralClass:
John McCall31168b02011-06-15 23:02:42 +00002171 case Expr::ObjCIndirectCopyRestoreExprClass:
John McCall05ddbb82010-07-14 04:20:34 +00002172 case Expr::OffsetOfExprClass:
2173 case Expr::PredefinedExprClass:
2174 case Expr::ShuffleVectorExprClass:
2175 case Expr::StmtExprClass:
John McCall05ddbb82010-07-14 04:20:34 +00002176 case Expr::UnaryTypeTraitExprClass:
Francois Pichet9dfa3ce2010-12-07 00:08:36 +00002177 case Expr::BinaryTypeTraitExprClass:
John Wiegley6242b6a2011-04-28 00:16:57 +00002178 case Expr::ArrayTypeTraitExprClass:
John Wiegleyf9f65842011-04-25 06:54:41 +00002179 case Expr::ExpressionTraitExprClass:
Francois Pichet5cc0a672010-09-08 23:47:05 +00002180 case Expr::VAArgExprClass:
Sebastian Redl4202c0f2010-09-10 20:55:43 +00002181 case Expr::CXXUuidofExprClass:
Peter Collingbourne41f85462011-02-09 21:07:24 +00002182 case Expr::CXXNoexceptExprClass:
Tanya Lattner55808c12011-06-04 00:47:47 +00002183 case Expr::CUDAKernelCallExprClass:
2184 case Expr::AsTypeExprClass:
2185 {
John McCall6936c862010-04-09 22:26:14 +00002186 // As bad as this diagnostic is, it's better than crashing.
2187 Diagnostic &Diags = Context.getDiags();
2188 unsigned DiagID = Diags.getCustomDiagID(Diagnostic::Error,
2189 "cannot yet mangle expression type %0");
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +00002190 Diags.Report(E->getExprLoc(), DiagID)
John McCalle2f35242010-04-10 09:39:25 +00002191 << E->getStmtClassName() << E->getSourceRange();
John McCall6936c862010-04-09 22:26:14 +00002192 break;
2193 }
2194
John McCallc07a0c72011-02-17 10:25:35 +00002195 // Even gcc-4.5 doesn't mangle this.
2196 case Expr::BinaryConditionalOperatorClass: {
2197 Diagnostic &Diags = Context.getDiags();
2198 unsigned DiagID =
2199 Diags.getCustomDiagID(Diagnostic::Error,
2200 "?: operator with omitted middle operand cannot be mangled");
2201 Diags.Report(E->getExprLoc(), DiagID)
2202 << E->getStmtClassName() << E->getSourceRange();
2203 break;
2204 }
2205
2206 // These are used for internal purposes and cannot be meaningfully mangled.
John McCall8d69a212010-11-15 23:31:06 +00002207 case Expr::OpaqueValueExprClass:
2208 llvm_unreachable("cannot mangle opaque value; mangling wrong thing?");
2209
John McCall05ddbb82010-07-14 04:20:34 +00002210 case Expr::CXXDefaultArgExprClass:
John McCall78fbb612010-08-18 19:18:59 +00002211 mangleExpression(cast<CXXDefaultArgExpr>(E)->getExpr(), Arity);
John McCall05ddbb82010-07-14 04:20:34 +00002212 break;
2213
2214 case Expr::CXXMemberCallExprClass: // fallthrough
John McCall09de8ec2010-02-04 01:42:13 +00002215 case Expr::CallExprClass: {
2216 const CallExpr *CE = cast<CallExpr>(E);
John McCall7f0a0d52011-04-28 02:52:03 +00002217
2218 // <expression> ::= cp <simple-id> <expression>* E
2219 // We use this mangling only when the call would use ADL except
2220 // for being parenthesized. Per discussion with David
2221 // Vandervoorde, 2011.04.25.
2222 if (isParenthesizedADLCallee(CE)) {
2223 Out << "cp";
2224 // The callee here is a parenthesized UnresolvedLookupExpr with
2225 // no qualifier and should always get mangled as a <simple-id>
2226 // anyway.
2227
2228 // <expression> ::= cl <expression>* E
2229 } else {
2230 Out << "cl";
2231 }
2232
John McCall78fbb612010-08-18 19:18:59 +00002233 mangleExpression(CE->getCallee(), CE->getNumArgs());
John McCall09de8ec2010-02-04 01:42:13 +00002234 for (unsigned I = 0, N = CE->getNumArgs(); I != N; ++I)
2235 mangleExpression(CE->getArg(I));
Benjamin Kramer1554e4a2010-04-10 16:03:31 +00002236 Out << 'E';
John McCall2adddca2010-02-03 00:55:45 +00002237 break;
John McCall09de8ec2010-02-04 01:42:13 +00002238 }
John McCall2adddca2010-02-03 00:55:45 +00002239
John McCall05ddbb82010-07-14 04:20:34 +00002240 case Expr::CXXNewExprClass: {
2241 // Proposal from David Vandervoorde, 2010.06.30
2242 const CXXNewExpr *New = cast<CXXNewExpr>(E);
2243 if (New->isGlobalNew()) Out << "gs";
2244 Out << (New->isArray() ? "na" : "nw");
2245 for (CXXNewExpr::const_arg_iterator I = New->placement_arg_begin(),
2246 E = New->placement_arg_end(); I != E; ++I)
2247 mangleExpression(*I);
2248 Out << '_';
2249 mangleType(New->getAllocatedType());
2250 if (New->hasInitializer()) {
2251 Out << "pi";
2252 for (CXXNewExpr::const_arg_iterator I = New->constructor_arg_begin(),
2253 E = New->constructor_arg_end(); I != E; ++I)
2254 mangleExpression(*I);
2255 }
2256 Out << 'E';
2257 break;
2258 }
2259
John McCalld061b442010-02-04 02:56:29 +00002260 case Expr::MemberExprClass: {
2261 const MemberExpr *ME = cast<MemberExpr>(E);
2262 mangleMemberExpr(ME->getBase(), ME->isArrow(),
John McCall6dc0a2b2011-04-24 08:23:24 +00002263 ME->getQualifier(), 0, ME->getMemberDecl()->getDeclName(),
John McCall78fbb612010-08-18 19:18:59 +00002264 Arity);
John McCalld061b442010-02-04 02:56:29 +00002265 break;
2266 }
2267
2268 case Expr::UnresolvedMemberExprClass: {
2269 const UnresolvedMemberExpr *ME = cast<UnresolvedMemberExpr>(E);
2270 mangleMemberExpr(ME->getBase(), ME->isArrow(),
John McCall6dc0a2b2011-04-24 08:23:24 +00002271 ME->getQualifier(), 0, ME->getMemberName(),
John McCall78fbb612010-08-18 19:18:59 +00002272 Arity);
John McCallf834bcd2010-08-20 00:17:19 +00002273 if (ME->hasExplicitTemplateArgs())
2274 mangleTemplateArgs(ME->getExplicitTemplateArgs());
John McCalld061b442010-02-04 02:56:29 +00002275 break;
2276 }
2277
2278 case Expr::CXXDependentScopeMemberExprClass: {
2279 const CXXDependentScopeMemberExpr *ME
2280 = cast<CXXDependentScopeMemberExpr>(E);
2281 mangleMemberExpr(ME->getBase(), ME->isArrow(),
John McCall6dc0a2b2011-04-24 08:23:24 +00002282 ME->getQualifier(), ME->getFirstQualifierFoundInScope(),
2283 ME->getMember(), Arity);
John McCallf834bcd2010-08-20 00:17:19 +00002284 if (ME->hasExplicitTemplateArgs())
2285 mangleTemplateArgs(ME->getExplicitTemplateArgs());
John McCalld061b442010-02-04 02:56:29 +00002286 break;
2287 }
2288
John McCall09de8ec2010-02-04 01:42:13 +00002289 case Expr::UnresolvedLookupExprClass: {
2290 const UnresolvedLookupExpr *ULE = cast<UnresolvedLookupExpr>(E);
John McCall6dc0a2b2011-04-24 08:23:24 +00002291 mangleUnresolvedName(ULE->getQualifier(), 0, ULE->getName(), Arity);
John McCall575fda12011-06-21 22:12:46 +00002292
2293 // All the <unresolved-name> productions end in a
2294 // base-unresolved-name, where <template-args> are just tacked
2295 // onto the end.
John McCallf834bcd2010-08-20 00:17:19 +00002296 if (ULE->hasExplicitTemplateArgs())
2297 mangleTemplateArgs(ULE->getExplicitTemplateArgs());
John McCall09de8ec2010-02-04 01:42:13 +00002298 break;
2299 }
2300
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00002301 case Expr::CXXUnresolvedConstructExprClass: {
John McCall09de8ec2010-02-04 01:42:13 +00002302 const CXXUnresolvedConstructExpr *CE = cast<CXXUnresolvedConstructExpr>(E);
2303 unsigned N = CE->arg_size();
2304
2305 Out << "cv";
2306 mangleType(CE->getType());
Benjamin Kramer1554e4a2010-04-10 16:03:31 +00002307 if (N != 1) Out << '_';
John McCall09de8ec2010-02-04 01:42:13 +00002308 for (unsigned I = 0; I != N; ++I) mangleExpression(CE->getArg(I));
Benjamin Kramer1554e4a2010-04-10 16:03:31 +00002309 if (N != 1) Out << 'E';
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00002310 break;
John McCall09de8ec2010-02-04 01:42:13 +00002311 }
John McCall2adddca2010-02-03 00:55:45 +00002312
John McCall09de8ec2010-02-04 01:42:13 +00002313 case Expr::CXXTemporaryObjectExprClass:
2314 case Expr::CXXConstructExprClass: {
2315 const CXXConstructExpr *CE = cast<CXXConstructExpr>(E);
2316 unsigned N = CE->getNumArgs();
2317
2318 Out << "cv";
2319 mangleType(CE->getType());
Benjamin Kramer1554e4a2010-04-10 16:03:31 +00002320 if (N != 1) Out << '_';
John McCall09de8ec2010-02-04 01:42:13 +00002321 for (unsigned I = 0; I != N; ++I) mangleExpression(CE->getArg(I));
Benjamin Kramer1554e4a2010-04-10 16:03:31 +00002322 if (N != 1) Out << 'E';
John McCall2adddca2010-02-03 00:55:45 +00002323 break;
John McCall09de8ec2010-02-04 01:42:13 +00002324 }
2325
Peter Collingbournee190dee2011-03-11 19:24:49 +00002326 case Expr::UnaryExprOrTypeTraitExprClass: {
2327 const UnaryExprOrTypeTraitExpr *SAE = cast<UnaryExprOrTypeTraitExpr>(E);
2328 switch(SAE->getKind()) {
2329 case UETT_SizeOf:
2330 Out << 's';
2331 break;
2332 case UETT_AlignOf:
2333 Out << 'a';
2334 break;
2335 case UETT_VecStep:
2336 Diagnostic &Diags = Context.getDiags();
2337 unsigned DiagID = Diags.getCustomDiagID(Diagnostic::Error,
2338 "cannot yet mangle vec_step expression");
2339 Diags.Report(DiagID);
2340 return;
2341 }
John McCall09de8ec2010-02-04 01:42:13 +00002342 if (SAE->isArgumentType()) {
Benjamin Kramer1554e4a2010-04-10 16:03:31 +00002343 Out << 't';
John McCall09de8ec2010-02-04 01:42:13 +00002344 mangleType(SAE->getArgumentType());
2345 } else {
Benjamin Kramer1554e4a2010-04-10 16:03:31 +00002346 Out << 'z';
John McCall09de8ec2010-02-04 01:42:13 +00002347 mangleExpression(SAE->getArgumentExpr());
2348 }
2349 break;
2350 }
Anders Carlsson9e4e0232009-11-06 02:50:19 +00002351
John McCall05ddbb82010-07-14 04:20:34 +00002352 case Expr::CXXThrowExprClass: {
2353 const CXXThrowExpr *TE = cast<CXXThrowExpr>(E);
2354
2355 // Proposal from David Vandervoorde, 2010.06.30
2356 if (TE->getSubExpr()) {
2357 Out << "tw";
2358 mangleExpression(TE->getSubExpr());
2359 } else {
2360 Out << "tr";
2361 }
2362 break;
2363 }
2364
2365 case Expr::CXXTypeidExprClass: {
2366 const CXXTypeidExpr *TIE = cast<CXXTypeidExpr>(E);
2367
2368 // Proposal from David Vandervoorde, 2010.06.30
2369 if (TIE->isTypeOperand()) {
2370 Out << "ti";
2371 mangleType(TIE->getTypeOperand());
2372 } else {
2373 Out << "te";
2374 mangleExpression(TIE->getExprOperand());
2375 }
2376 break;
2377 }
2378
2379 case Expr::CXXDeleteExprClass: {
2380 const CXXDeleteExpr *DE = cast<CXXDeleteExpr>(E);
2381
2382 // Proposal from David Vandervoorde, 2010.06.30
2383 if (DE->isGlobalDelete()) Out << "gs";
2384 Out << (DE->isArrayForm() ? "da" : "dl");
2385 mangleExpression(DE->getArgument());
2386 break;
2387 }
2388
Anders Carlssone66e2942009-12-14 01:45:37 +00002389 case Expr::UnaryOperatorClass: {
2390 const UnaryOperator *UO = cast<UnaryOperator>(E);
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00002391 mangleOperatorName(UnaryOperator::getOverloadedOperator(UO->getOpcode()),
Anders Carlssone66e2942009-12-14 01:45:37 +00002392 /*Arity=*/1);
2393 mangleExpression(UO->getSubExpr());
2394 break;
2395 }
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00002396
John McCall05ddbb82010-07-14 04:20:34 +00002397 case Expr::ArraySubscriptExprClass: {
2398 const ArraySubscriptExpr *AE = cast<ArraySubscriptExpr>(E);
2399
Chris Lattner57540c52011-04-15 05:22:18 +00002400 // Array subscript is treated as a syntactically weird form of
John McCall05ddbb82010-07-14 04:20:34 +00002401 // binary operator.
2402 Out << "ix";
2403 mangleExpression(AE->getLHS());
2404 mangleExpression(AE->getRHS());
2405 break;
2406 }
2407
2408 case Expr::CompoundAssignOperatorClass: // fallthrough
Anders Carlssone66e2942009-12-14 01:45:37 +00002409 case Expr::BinaryOperatorClass: {
2410 const BinaryOperator *BO = cast<BinaryOperator>(E);
Douglas Gregorc507db42011-06-05 05:27:58 +00002411 if (BO->getOpcode() == BO_PtrMemD)
2412 Out << "ds";
2413 else
2414 mangleOperatorName(BinaryOperator::getOverloadedOperator(BO->getOpcode()),
2415 /*Arity=*/2);
Anders Carlssone66e2942009-12-14 01:45:37 +00002416 mangleExpression(BO->getLHS());
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00002417 mangleExpression(BO->getRHS());
Anders Carlssone66e2942009-12-14 01:45:37 +00002418 break;
John McCalld061b442010-02-04 02:56:29 +00002419 }
Anders Carlssone66e2942009-12-14 01:45:37 +00002420
2421 case Expr::ConditionalOperatorClass: {
2422 const ConditionalOperator *CO = cast<ConditionalOperator>(E);
2423 mangleOperatorName(OO_Conditional, /*Arity=*/3);
2424 mangleExpression(CO->getCond());
John McCall78fbb612010-08-18 19:18:59 +00002425 mangleExpression(CO->getLHS(), Arity);
2426 mangleExpression(CO->getRHS(), Arity);
Anders Carlssone66e2942009-12-14 01:45:37 +00002427 break;
2428 }
2429
Douglas Gregor16810ca2010-01-29 16:37:09 +00002430 case Expr::ImplicitCastExprClass: {
John McCall78fbb612010-08-18 19:18:59 +00002431 mangleExpression(cast<ImplicitCastExpr>(E)->getSubExpr(), Arity);
Douglas Gregor16810ca2010-01-29 16:37:09 +00002432 break;
2433 }
John McCall31168b02011-06-15 23:02:42 +00002434
2435 case Expr::ObjCBridgedCastExprClass: {
2436 // Mangle ownership casts as a vendor extended operator __bridge,
2437 // __bridge_transfer, or __bridge_retain.
2438 llvm::StringRef Kind = cast<ObjCBridgedCastExpr>(E)->getBridgeKindName();
2439 Out << "v1U" << Kind.size() << Kind;
2440 }
2441 // Fall through to mangle the cast itself.
2442
Douglas Gregor16810ca2010-01-29 16:37:09 +00002443 case Expr::CStyleCastExprClass:
2444 case Expr::CXXStaticCastExprClass:
2445 case Expr::CXXDynamicCastExprClass:
2446 case Expr::CXXReinterpretCastExprClass:
2447 case Expr::CXXConstCastExprClass:
2448 case Expr::CXXFunctionalCastExprClass: {
2449 const ExplicitCastExpr *ECE = cast<ExplicitCastExpr>(E);
2450 Out << "cv";
2451 mangleType(ECE->getType());
2452 mangleExpression(ECE->getSubExpr());
2453 break;
2454 }
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00002455
Anders Carlssonb7e93e22009-12-16 05:48:46 +00002456 case Expr::CXXOperatorCallExprClass: {
2457 const CXXOperatorCallExpr *CE = cast<CXXOperatorCallExpr>(E);
2458 unsigned NumArgs = CE->getNumArgs();
2459 mangleOperatorName(CE->getOperator(), /*Arity=*/NumArgs);
2460 // Mangle the arguments.
2461 for (unsigned i = 0; i != NumArgs; ++i)
2462 mangleExpression(CE->getArg(i));
2463 break;
2464 }
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00002465
Anders Carlsson9e4e0232009-11-06 02:50:19 +00002466 case Expr::ParenExprClass:
John McCall78fbb612010-08-18 19:18:59 +00002467 mangleExpression(cast<ParenExpr>(E)->getSubExpr(), Arity);
Anders Carlsson9e4e0232009-11-06 02:50:19 +00002468 break;
2469
Anders Carlssona18322c2009-09-21 01:21:10 +00002470 case Expr::DeclRefExprClass: {
Douglas Gregor5610db62010-02-28 21:40:32 +00002471 const NamedDecl *D = cast<DeclRefExpr>(E)->getDecl();
Daniel Dunbard614e322009-11-21 09:05:47 +00002472
Anders Carlssona18322c2009-09-21 01:21:10 +00002473 switch (D->getKind()) {
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00002474 default:
Douglas Gregor5610db62010-02-28 21:40:32 +00002475 // <expr-primary> ::= L <mangled-name> E # external name
2476 Out << 'L';
2477 mangle(D, "_Z");
2478 Out << 'E';
2479 break;
2480
John McCall8fb0d9d2011-05-01 22:35:37 +00002481 case Decl::ParmVar:
2482 mangleFunctionParam(cast<ParmVarDecl>(D));
2483 break;
2484
John McCall268b5762010-07-24 01:17:35 +00002485 case Decl::EnumConstant: {
2486 const EnumConstantDecl *ED = cast<EnumConstantDecl>(D);
2487 mangleIntegerLiteral(ED->getType(), ED->getInitVal());
2488 break;
2489 }
2490
Anders Carlssona18322c2009-09-21 01:21:10 +00002491 case Decl::NonTypeTemplateParm: {
2492 const NonTypeTemplateParmDecl *PD = cast<NonTypeTemplateParmDecl>(D);
Anders Carlssone00745b2009-09-27 00:38:53 +00002493 mangleTemplateParameter(PD->getIndex());
Anders Carlssona18322c2009-09-21 01:21:10 +00002494 break;
2495 }
2496
2497 }
Daniel Dunbard614e322009-11-21 09:05:47 +00002498
Anders Carlsson02bca732009-09-27 20:11:34 +00002499 break;
Anders Carlssona18322c2009-09-21 01:21:10 +00002500 }
Daniel Dunbard614e322009-11-21 09:05:47 +00002501
Douglas Gregorcdbc5392011-01-15 01:15:58 +00002502 case Expr::SubstNonTypeTemplateParmPackExprClass:
2503 mangleTemplateParameter(
2504 cast<SubstNonTypeTemplateParmPackExpr>(E)->getParameterPack()->getIndex());
2505 break;
2506
John McCall8cd78132009-11-19 22:55:06 +00002507 case Expr::DependentScopeDeclRefExprClass: {
2508 const DependentScopeDeclRefExpr *DRE = cast<DependentScopeDeclRefExpr>(E);
John McCall575fda12011-06-21 22:12:46 +00002509 mangleUnresolvedName(DRE->getQualifier(), 0, DRE->getDeclName(), Arity);
Douglas Gregora51c6742010-02-28 22:05:49 +00002510
John McCall575fda12011-06-21 22:12:46 +00002511 // All the <unresolved-name> productions end in a
2512 // base-unresolved-name, where <template-args> are just tacked
2513 // onto the end.
John McCallf834bcd2010-08-20 00:17:19 +00002514 if (DRE->hasExplicitTemplateArgs())
2515 mangleTemplateArgs(DRE->getExplicitTemplateArgs());
Anders Carlsson02bca732009-09-27 20:11:34 +00002516 break;
2517 }
2518
John McCalle8917e02010-04-09 22:54:09 +00002519 case Expr::CXXBindTemporaryExprClass:
2520 mangleExpression(cast<CXXBindTemporaryExpr>(E)->getSubExpr());
2521 break;
2522
John McCall5d413782010-12-06 08:20:24 +00002523 case Expr::ExprWithCleanupsClass:
2524 mangleExpression(cast<ExprWithCleanups>(E)->getSubExpr(), Arity);
John McCalle8917e02010-04-09 22:54:09 +00002525 break;
2526
John McCall09de8ec2010-02-04 01:42:13 +00002527 case Expr::FloatingLiteralClass: {
2528 const FloatingLiteral *FL = cast<FloatingLiteral>(E);
Benjamin Kramer1554e4a2010-04-10 16:03:31 +00002529 Out << 'L';
John McCall09de8ec2010-02-04 01:42:13 +00002530 mangleType(FL->getType());
John McCall05ddbb82010-07-14 04:20:34 +00002531 mangleFloat(FL->getValue());
Benjamin Kramer1554e4a2010-04-10 16:03:31 +00002532 Out << 'E';
John McCall09de8ec2010-02-04 01:42:13 +00002533 break;
2534 }
2535
John McCall092775422010-04-09 21:48:08 +00002536 case Expr::CharacterLiteralClass:
Benjamin Kramer1554e4a2010-04-10 16:03:31 +00002537 Out << 'L';
John McCall092775422010-04-09 21:48:08 +00002538 mangleType(E->getType());
2539 Out << cast<CharacterLiteral>(E)->getValue();
2540 Out << 'E';
2541 break;
2542
2543 case Expr::CXXBoolLiteralExprClass:
2544 Out << "Lb";
2545 Out << (cast<CXXBoolLiteralExpr>(E)->getValue() ? '1' : '0');
2546 Out << 'E';
2547 break;
2548
John McCall05ddbb82010-07-14 04:20:34 +00002549 case Expr::IntegerLiteralClass: {
2550 llvm::APSInt Value(cast<IntegerLiteral>(E)->getValue());
2551 if (E->getType()->isSignedIntegerType())
2552 Value.setIsSigned(true);
2553 mangleIntegerLiteral(E->getType(), Value);
Anders Carlssone66e2942009-12-14 01:45:37 +00002554 break;
John McCall05ddbb82010-07-14 04:20:34 +00002555 }
2556
2557 case Expr::ImaginaryLiteralClass: {
2558 const ImaginaryLiteral *IE = cast<ImaginaryLiteral>(E);
2559 // Mangle as if a complex literal.
Nick Lewycky1b36b552010-09-05 03:40:33 +00002560 // Proposal from David Vandevoorde, 2010.06.30.
John McCall05ddbb82010-07-14 04:20:34 +00002561 Out << 'L';
2562 mangleType(E->getType());
2563 if (const FloatingLiteral *Imag =
2564 dyn_cast<FloatingLiteral>(IE->getSubExpr())) {
2565 // Mangle a floating-point zero of the appropriate type.
2566 mangleFloat(llvm::APFloat(Imag->getValue().getSemantics()));
2567 Out << '_';
2568 mangleFloat(Imag->getValue());
2569 } else {
Nick Lewycky1b36b552010-09-05 03:40:33 +00002570 Out << "0_";
John McCall05ddbb82010-07-14 04:20:34 +00002571 llvm::APSInt Value(cast<IntegerLiteral>(IE->getSubExpr())->getValue());
2572 if (IE->getSubExpr()->getType()->isSignedIntegerType())
2573 Value.setIsSigned(true);
2574 mangleNumber(Value);
2575 }
2576 Out << 'E';
2577 break;
2578 }
2579
2580 case Expr::StringLiteralClass: {
John McCallec624b22010-07-15 21:53:03 +00002581 // Revised proposal from David Vandervoorde, 2010.07.15.
John McCall05ddbb82010-07-14 04:20:34 +00002582 Out << 'L';
John McCallec624b22010-07-15 21:53:03 +00002583 assert(isa<ConstantArrayType>(E->getType()));
2584 mangleType(E->getType());
John McCall05ddbb82010-07-14 04:20:34 +00002585 Out << 'E';
2586 break;
2587 }
2588
2589 case Expr::GNUNullExprClass:
2590 // FIXME: should this really be mangled the same as nullptr?
2591 // fallthrough
2592
2593 case Expr::CXXNullPtrLiteralExprClass: {
2594 // Proposal from David Vandervoorde, 2010.06.30, as
2595 // modified by ABI list discussion.
2596 Out << "LDnE";
2597 break;
2598 }
Douglas Gregore8e9dd62011-01-03 17:17:50 +00002599
2600 case Expr::PackExpansionExprClass:
2601 Out << "sp";
2602 mangleExpression(cast<PackExpansionExpr>(E)->getPattern());
2603 break;
Douglas Gregorbe63bbf2011-01-04 18:56:13 +00002604
2605 case Expr::SizeOfPackExprClass: {
Douglas Gregorbe63bbf2011-01-04 18:56:13 +00002606 Out << "sZ";
2607 const NamedDecl *Pack = cast<SizeOfPackExpr>(E)->getPack();
2608 if (const TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(Pack))
2609 mangleTemplateParameter(TTP->getIndex());
2610 else if (const NonTypeTemplateParmDecl *NTTP
2611 = dyn_cast<NonTypeTemplateParmDecl>(Pack))
2612 mangleTemplateParameter(NTTP->getIndex());
2613 else if (const TemplateTemplateParmDecl *TempTP
2614 = dyn_cast<TemplateTemplateParmDecl>(Pack))
2615 mangleTemplateParameter(TempTP->getIndex());
2616 else {
Douglas Gregord81c7c12011-01-13 16:39:34 +00002617 // Note: proposed by Mike Herrick on 11/30/10
2618 // <expression> ::= sZ <function-param> # size of function parameter pack
Douglas Gregorbe63bbf2011-01-04 18:56:13 +00002619 Diagnostic &Diags = Context.getDiags();
2620 unsigned DiagID = Diags.getCustomDiagID(Diagnostic::Error,
2621 "cannot mangle sizeof...(function parameter pack)");
2622 Diags.Report(DiagID);
2623 return;
2624 }
Douglas Gregorce449ab2011-03-03 02:20:19 +00002625 break;
Douglas Gregorbe63bbf2011-01-04 18:56:13 +00002626 }
Douglas Gregorfe314812011-06-21 17:03:29 +00002627
2628 case Expr::MaterializeTemporaryExprClass: {
2629 mangleExpression(cast<MaterializeTemporaryExpr>(E)->GetTemporaryExpr());
2630 break;
2631 }
Anders Carlssona18322c2009-09-21 01:21:10 +00002632 }
Douglas Gregor5fec5b02009-02-13 00:10:09 +00002633}
2634
John McCall8fb0d9d2011-05-01 22:35:37 +00002635/// Mangle an expression which refers to a parameter variable.
2636///
2637/// <expression> ::= <function-param>
2638/// <function-param> ::= fp <top-level CV-qualifiers> _ # L == 0, I == 0
2639/// <function-param> ::= fp <top-level CV-qualifiers>
2640/// <parameter-2 non-negative number> _ # L == 0, I > 0
2641/// <function-param> ::= fL <L-1 non-negative number>
2642/// p <top-level CV-qualifiers> _ # L > 0, I == 0
2643/// <function-param> ::= fL <L-1 non-negative number>
2644/// p <top-level CV-qualifiers>
2645/// <I-1 non-negative number> _ # L > 0, I > 0
2646///
2647/// L is the nesting depth of the parameter, defined as 1 if the
2648/// parameter comes from the innermost function prototype scope
2649/// enclosing the current context, 2 if from the next enclosing
2650/// function prototype scope, and so on, with one special case: if
2651/// we've processed the full parameter clause for the innermost
2652/// function type, then L is one less. This definition conveniently
2653/// makes it irrelevant whether a function's result type was written
2654/// trailing or leading, but is otherwise overly complicated; the
2655/// numbering was first designed without considering references to
2656/// parameter in locations other than return types, and then the
2657/// mangling had to be generalized without changing the existing
2658/// manglings.
2659///
2660/// I is the zero-based index of the parameter within its parameter
2661/// declaration clause. Note that the original ABI document describes
2662/// this using 1-based ordinals.
2663void CXXNameMangler::mangleFunctionParam(const ParmVarDecl *parm) {
2664 unsigned parmDepth = parm->getFunctionScopeDepth();
2665 unsigned parmIndex = parm->getFunctionScopeIndex();
2666
2667 // Compute 'L'.
2668 // parmDepth does not include the declaring function prototype.
2669 // FunctionTypeDepth does account for that.
2670 assert(parmDepth < FunctionTypeDepth.getDepth());
2671 unsigned nestingDepth = FunctionTypeDepth.getDepth() - parmDepth;
2672 if (FunctionTypeDepth.isInResultType())
2673 nestingDepth--;
2674
2675 if (nestingDepth == 0) {
2676 Out << "fp";
2677 } else {
2678 Out << "fL" << (nestingDepth - 1) << 'p';
2679 }
2680
2681 // Top-level qualifiers. We don't have to worry about arrays here,
2682 // because parameters declared as arrays should already have been
2683 // tranformed to have pointer type. FIXME: apparently these don't
2684 // get mangled if used as an rvalue of a known non-class type?
2685 assert(!parm->getType()->isArrayType()
2686 && "parameter's type is still an array type?");
2687 mangleQualifiers(parm->getType().getQualifiers());
2688
2689 // Parameter index.
2690 if (parmIndex != 0) {
2691 Out << (parmIndex - 1);
2692 }
2693 Out << '_';
2694}
2695
Anders Carlssone4c40c82009-04-15 05:36:58 +00002696void CXXNameMangler::mangleCXXCtorType(CXXCtorType T) {
2697 // <ctor-dtor-name> ::= C1 # complete object constructor
2698 // ::= C2 # base object constructor
2699 // ::= C3 # complete object allocating constructor
2700 //
2701 switch (T) {
2702 case Ctor_Complete:
2703 Out << "C1";
2704 break;
2705 case Ctor_Base:
2706 Out << "C2";
2707 break;
2708 case Ctor_CompleteAllocating:
2709 Out << "C3";
2710 break;
2711 }
2712}
2713
Anders Carlssoneaa28f72009-04-17 01:58:57 +00002714void CXXNameMangler::mangleCXXDtorType(CXXDtorType T) {
2715 // <ctor-dtor-name> ::= D0 # deleting destructor
2716 // ::= D1 # complete object destructor
2717 // ::= D2 # base object destructor
2718 //
2719 switch (T) {
2720 case Dtor_Deleting:
2721 Out << "D0";
2722 break;
2723 case Dtor_Complete:
2724 Out << "D1";
2725 break;
2726 case Dtor_Base:
2727 Out << "D2";
2728 break;
2729 }
2730}
2731
John McCallf834bcd2010-08-20 00:17:19 +00002732void CXXNameMangler::mangleTemplateArgs(
2733 const ExplicitTemplateArgumentList &TemplateArgs) {
2734 // <template-args> ::= I <template-arg>+ E
2735 Out << 'I';
John McCall3ab84762011-05-04 01:45:19 +00002736 for (unsigned i = 0, e = TemplateArgs.NumTemplateArgs; i != e; ++i)
2737 mangleTemplateArg(0, TemplateArgs.getTemplateArgs()[i].getArgument());
John McCallf834bcd2010-08-20 00:17:19 +00002738 Out << 'E';
2739}
2740
Douglas Gregor17362712010-04-23 03:10:43 +00002741void CXXNameMangler::mangleTemplateArgs(TemplateName Template,
2742 const TemplateArgument *TemplateArgs,
2743 unsigned NumTemplateArgs) {
2744 if (TemplateDecl *TD = Template.getAsTemplateDecl())
2745 return mangleTemplateArgs(*TD->getTemplateParameters(), TemplateArgs,
2746 NumTemplateArgs);
Alexis Hunta8136cc2010-05-05 15:23:54 +00002747
John McCall3ab84762011-05-04 01:45:19 +00002748 mangleUnresolvedTemplateArgs(TemplateArgs, NumTemplateArgs);
2749}
2750
2751void CXXNameMangler::mangleUnresolvedTemplateArgs(const TemplateArgument *args,
2752 unsigned numArgs) {
Douglas Gregor17362712010-04-23 03:10:43 +00002753 // <template-args> ::= I <template-arg>+ E
2754 Out << 'I';
John McCall3ab84762011-05-04 01:45:19 +00002755 for (unsigned i = 0; i != numArgs; ++i)
2756 mangleTemplateArg(0, args[i]);
Douglas Gregor17362712010-04-23 03:10:43 +00002757 Out << 'E';
2758}
2759
Rafael Espindola4d5c3d92010-03-11 14:07:00 +00002760void CXXNameMangler::mangleTemplateArgs(const TemplateParameterList &PL,
2761 const TemplateArgumentList &AL) {
Anders Carlssonf6e9ece2009-05-15 16:09:15 +00002762 // <template-args> ::= I <template-arg>+ E
Benjamin Kramer1554e4a2010-04-10 16:03:31 +00002763 Out << 'I';
Rafael Espindola4d5c3d92010-03-11 14:07:00 +00002764 for (unsigned i = 0, e = AL.size(); i != e; ++i)
2765 mangleTemplateArg(PL.getParam(i), AL[i]);
Benjamin Kramer1554e4a2010-04-10 16:03:31 +00002766 Out << 'E';
Anders Carlssonf6e9ece2009-05-15 16:09:15 +00002767}
2768
Rafael Espindola4d5c3d92010-03-11 14:07:00 +00002769void CXXNameMangler::mangleTemplateArgs(const TemplateParameterList &PL,
2770 const TemplateArgument *TemplateArgs,
Anders Carlsson7a8a74f2009-09-18 02:42:01 +00002771 unsigned NumTemplateArgs) {
2772 // <template-args> ::= I <template-arg>+ E
Benjamin Kramer1554e4a2010-04-10 16:03:31 +00002773 Out << 'I';
Daniel Dunbar88ad4c52009-11-21 09:17:15 +00002774 for (unsigned i = 0; i != NumTemplateArgs; ++i)
Rafael Espindola4d5c3d92010-03-11 14:07:00 +00002775 mangleTemplateArg(PL.getParam(i), TemplateArgs[i]);
Benjamin Kramer1554e4a2010-04-10 16:03:31 +00002776 Out << 'E';
Anders Carlsson7a8a74f2009-09-18 02:42:01 +00002777}
2778
Rafael Espindola4d5c3d92010-03-11 14:07:00 +00002779void CXXNameMangler::mangleTemplateArg(const NamedDecl *P,
2780 const TemplateArgument &A) {
Mike Stump11289f42009-09-09 15:08:12 +00002781 // <template-arg> ::= <type> # type or template
Anders Carlssonf6e9ece2009-05-15 16:09:15 +00002782 // ::= X <expression> E # expression
2783 // ::= <expr-primary> # simple expressions
Douglas Gregord81c7c12011-01-13 16:39:34 +00002784 // ::= J <template-arg>* E # argument pack
Anders Carlssonf6e9ece2009-05-15 16:09:15 +00002785 // ::= sp <expression> # pack expansion of (C++0x)
2786 switch (A.getKind()) {
Douglas Gregor752a5952011-01-03 22:36:02 +00002787 case TemplateArgument::Null:
2788 llvm_unreachable("Cannot mangle NULL template argument");
2789
Anders Carlssonf6e9ece2009-05-15 16:09:15 +00002790 case TemplateArgument::Type:
2791 mangleType(A.getAsType());
2792 break;
Anders Carlsson910847c2009-12-23 19:30:55 +00002793 case TemplateArgument::Template:
John McCall4f3b5f32010-07-14 06:43:17 +00002794 // This is mangled as <type>.
2795 mangleType(A.getAsTemplate());
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00002796 break;
Douglas Gregore4ff4b52011-01-05 18:58:31 +00002797 case TemplateArgument::TemplateExpansion:
Douglas Gregord81c7c12011-01-13 16:39:34 +00002798 // <type> ::= Dp <type> # pack expansion (C++0x)
Douglas Gregore4ff4b52011-01-05 18:58:31 +00002799 Out << "Dp";
2800 mangleType(A.getAsTemplateOrTemplatePattern());
2801 break;
Anders Carlssona18322c2009-09-21 01:21:10 +00002802 case TemplateArgument::Expression:
2803 Out << 'X';
2804 mangleExpression(A.getAsExpr());
2805 Out << 'E';
2806 break;
Anders Carlssone66e2942009-12-14 01:45:37 +00002807 case TemplateArgument::Integral:
2808 mangleIntegerLiteral(A.getIntegralType(), *A.getAsIntegral());
Anders Carlssonf6e9ece2009-05-15 16:09:15 +00002809 break;
Daniel Dunbar88ad4c52009-11-21 09:17:15 +00002810 case TemplateArgument::Declaration: {
Douglas Gregor17362712010-04-23 03:10:43 +00002811 assert(P && "Missing template parameter for declaration argument");
Daniel Dunbar88ad4c52009-11-21 09:17:15 +00002812 // <expr-primary> ::= L <mangled-name> E # external name
2813
Rafael Espindola4d5c3d92010-03-11 14:07:00 +00002814 // Clang produces AST's where pointer-to-member-function expressions
Daniel Dunbar88ad4c52009-11-21 09:17:15 +00002815 // and pointer-to-function expressions are represented as a declaration not
Rafael Espindola4d5c3d92010-03-11 14:07:00 +00002816 // an expression. We compensate for it here to produce the correct mangling.
2817 NamedDecl *D = cast<NamedDecl>(A.getAsDecl());
2818 const NonTypeTemplateParmDecl *Parameter = cast<NonTypeTemplateParmDecl>(P);
John McCall617339e2011-04-24 08:43:07 +00002819 bool compensateMangling = !Parameter->getType()->isReferenceType();
Rafael Espindola4d5c3d92010-03-11 14:07:00 +00002820 if (compensateMangling) {
2821 Out << 'X';
2822 mangleOperatorName(OO_Amp, 1);
2823 }
2824
Daniel Dunbar88ad4c52009-11-21 09:17:15 +00002825 Out << 'L';
2826 // References to external entities use the mangled name; if the name would
2827 // not normally be manged then mangle it as unqualified.
2828 //
2829 // FIXME: The ABI specifies that external names here should have _Z, but
2830 // gcc leaves this off.
Rafael Espindola4d5c3d92010-03-11 14:07:00 +00002831 if (compensateMangling)
2832 mangle(D, "_Z");
2833 else
2834 mangle(D, "Z");
Daniel Dunbar88ad4c52009-11-21 09:17:15 +00002835 Out << 'E';
Rafael Espindola4d5c3d92010-03-11 14:07:00 +00002836
2837 if (compensateMangling)
2838 Out << 'E';
2839
Daniel Dunbar88ad4c52009-11-21 09:17:15 +00002840 break;
2841 }
Douglas Gregor752a5952011-01-03 22:36:02 +00002842
2843 case TemplateArgument::Pack: {
2844 // Note: proposal by Mike Herrick on 12/20/10
2845 Out << 'J';
2846 for (TemplateArgument::pack_iterator PA = A.pack_begin(),
2847 PAEnd = A.pack_end();
2848 PA != PAEnd; ++PA)
2849 mangleTemplateArg(P, *PA);
2850 Out << 'E';
2851 }
Daniel Dunbar88ad4c52009-11-21 09:17:15 +00002852 }
Anders Carlssonf6e9ece2009-05-15 16:09:15 +00002853}
2854
Anders Carlssone00745b2009-09-27 00:38:53 +00002855void CXXNameMangler::mangleTemplateParameter(unsigned Index) {
2856 // <template-param> ::= T_ # first template parameter
2857 // ::= T <parameter-2 non-negative number> _
2858 if (Index == 0)
2859 Out << "T_";
2860 else
2861 Out << 'T' << (Index - 1) << '_';
2862}
2863
Anders Carlssonfeb60502009-09-17 00:43:46 +00002864// <substitution> ::= S <seq-id> _
2865// ::= S_
Anders Carlsson4245bf92009-09-17 04:16:28 +00002866bool CXXNameMangler::mangleSubstitution(const NamedDecl *ND) {
Anders Carlssonaa9e3c82009-09-26 20:53:44 +00002867 // Try one of the standard substitutions first.
2868 if (mangleStandardSubstitution(ND))
2869 return true;
Daniel Dunbard614e322009-11-21 09:05:47 +00002870
Anders Carlssonce214312009-11-07 04:26:04 +00002871 ND = cast<NamedDecl>(ND->getCanonicalDecl());
Anders Carlsson4245bf92009-09-17 04:16:28 +00002872 return mangleSubstitution(reinterpret_cast<uintptr_t>(ND));
2873}
2874
Anders Carlssonfeb60502009-09-17 00:43:46 +00002875bool CXXNameMangler::mangleSubstitution(QualType T) {
Anders Carlsson296f8dc2009-09-26 03:55:37 +00002876 if (!T.getCVRQualifiers()) {
2877 if (const RecordType *RT = T->getAs<RecordType>())
2878 return mangleSubstitution(RT->getDecl());
2879 }
Daniel Dunbard614e322009-11-21 09:05:47 +00002880
Anders Carlssonfeb60502009-09-17 00:43:46 +00002881 uintptr_t TypePtr = reinterpret_cast<uintptr_t>(T.getAsOpaquePtr());
2882
Anders Carlssona96f56f2009-09-17 03:53:28 +00002883 return mangleSubstitution(TypePtr);
2884}
2885
Douglas Gregor7988def2010-04-28 05:58:56 +00002886bool CXXNameMangler::mangleSubstitution(TemplateName Template) {
2887 if (TemplateDecl *TD = Template.getAsTemplateDecl())
2888 return mangleSubstitution(TD);
Alexis Hunta8136cc2010-05-05 15:23:54 +00002889
Douglas Gregor7988def2010-04-28 05:58:56 +00002890 Template = Context.getASTContext().getCanonicalTemplateName(Template);
2891 return mangleSubstitution(
2892 reinterpret_cast<uintptr_t>(Template.getAsVoidPointer()));
2893}
2894
Anders Carlssona96f56f2009-09-17 03:53:28 +00002895bool CXXNameMangler::mangleSubstitution(uintptr_t Ptr) {
Benjamin Kramer1554e4a2010-04-10 16:03:31 +00002896 llvm::DenseMap<uintptr_t, unsigned>::iterator I = Substitutions.find(Ptr);
Anders Carlssonfeb60502009-09-17 00:43:46 +00002897 if (I == Substitutions.end())
2898 return false;
Daniel Dunbard614e322009-11-21 09:05:47 +00002899
Anders Carlssonfeb60502009-09-17 00:43:46 +00002900 unsigned SeqID = I->second;
2901 if (SeqID == 0)
2902 Out << "S_";
2903 else {
2904 SeqID--;
Daniel Dunbard614e322009-11-21 09:05:47 +00002905
Anders Carlssonfeb60502009-09-17 00:43:46 +00002906 // <seq-id> is encoded in base-36, using digits and upper case letters.
2907 char Buffer[10];
Benjamin Kramer1554e4a2010-04-10 16:03:31 +00002908 char *BufferPtr = llvm::array_endof(Buffer);
Daniel Dunbard614e322009-11-21 09:05:47 +00002909
Anders Carlssonfeb60502009-09-17 00:43:46 +00002910 if (SeqID == 0) *--BufferPtr = '0';
Daniel Dunbard614e322009-11-21 09:05:47 +00002911
Anders Carlssonfeb60502009-09-17 00:43:46 +00002912 while (SeqID) {
2913 assert(BufferPtr > Buffer && "Buffer overflow!");
Daniel Dunbard614e322009-11-21 09:05:47 +00002914
John McCalla5dd3262010-06-09 07:26:17 +00002915 char c = static_cast<char>(SeqID % 36);
Daniel Dunbard614e322009-11-21 09:05:47 +00002916
Anders Carlssonfeb60502009-09-17 00:43:46 +00002917 *--BufferPtr = (c < 10 ? '0' + c : 'A' + c - 10);
2918 SeqID /= 36;
2919 }
Daniel Dunbard614e322009-11-21 09:05:47 +00002920
Benjamin Kramer1554e4a2010-04-10 16:03:31 +00002921 Out << 'S'
2922 << llvm::StringRef(BufferPtr, llvm::array_endof(Buffer)-BufferPtr)
2923 << '_';
Anders Carlssonfeb60502009-09-17 00:43:46 +00002924 }
Daniel Dunbard614e322009-11-21 09:05:47 +00002925
Anders Carlssonfeb60502009-09-17 00:43:46 +00002926 return true;
2927}
2928
Anders Carlsson2e593522009-09-27 00:12:57 +00002929static bool isCharType(QualType T) {
2930 if (T.isNull())
2931 return false;
Daniel Dunbard614e322009-11-21 09:05:47 +00002932
Anders Carlsson2e593522009-09-27 00:12:57 +00002933 return T->isSpecificBuiltinType(BuiltinType::Char_S) ||
2934 T->isSpecificBuiltinType(BuiltinType::Char_U);
2935}
2936
Daniel Dunbard614e322009-11-21 09:05:47 +00002937/// isCharSpecialization - Returns whether a given type is a template
Anders Carlsson2e593522009-09-27 00:12:57 +00002938/// specialization of a given name with a single argument of type char.
2939static bool isCharSpecialization(QualType T, const char *Name) {
2940 if (T.isNull())
2941 return false;
Daniel Dunbard614e322009-11-21 09:05:47 +00002942
Anders Carlsson2e593522009-09-27 00:12:57 +00002943 const RecordType *RT = T->getAs<RecordType>();
2944 if (!RT)
2945 return false;
Daniel Dunbard614e322009-11-21 09:05:47 +00002946
2947 const ClassTemplateSpecializationDecl *SD =
Anders Carlsson2e593522009-09-27 00:12:57 +00002948 dyn_cast<ClassTemplateSpecializationDecl>(RT->getDecl());
2949 if (!SD)
2950 return false;
2951
2952 if (!isStdNamespace(SD->getDeclContext()))
2953 return false;
Daniel Dunbard614e322009-11-21 09:05:47 +00002954
Anders Carlsson2e593522009-09-27 00:12:57 +00002955 const TemplateArgumentList &TemplateArgs = SD->getTemplateArgs();
2956 if (TemplateArgs.size() != 1)
2957 return false;
Daniel Dunbard614e322009-11-21 09:05:47 +00002958
Anders Carlsson2e593522009-09-27 00:12:57 +00002959 if (!isCharType(TemplateArgs[0].getAsType()))
2960 return false;
Daniel Dunbard614e322009-11-21 09:05:47 +00002961
Daniel Dunbar07d07852009-10-18 21:17:35 +00002962 return SD->getIdentifier()->getName() == Name;
Anders Carlsson2e593522009-09-27 00:12:57 +00002963}
2964
Anders Carlsson1aaecfa2009-12-07 19:56:42 +00002965template <std::size_t StrLen>
Benjamin Kramer90b5b682010-11-25 18:29:30 +00002966static bool isStreamCharSpecialization(const ClassTemplateSpecializationDecl*SD,
2967 const char (&Str)[StrLen]) {
Anders Carlsson1aaecfa2009-12-07 19:56:42 +00002968 if (!SD->getIdentifier()->isStr(Str))
2969 return false;
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00002970
Anders Carlsson1aaecfa2009-12-07 19:56:42 +00002971 const TemplateArgumentList &TemplateArgs = SD->getTemplateArgs();
2972 if (TemplateArgs.size() != 2)
2973 return false;
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00002974
Anders Carlsson1aaecfa2009-12-07 19:56:42 +00002975 if (!isCharType(TemplateArgs[0].getAsType()))
2976 return false;
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00002977
Anders Carlsson1aaecfa2009-12-07 19:56:42 +00002978 if (!isCharSpecialization(TemplateArgs[1].getAsType(), "char_traits"))
2979 return false;
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00002980
Anders Carlsson1aaecfa2009-12-07 19:56:42 +00002981 return true;
2982}
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00002983
Anders Carlssonaa9e3c82009-09-26 20:53:44 +00002984bool CXXNameMangler::mangleStandardSubstitution(const NamedDecl *ND) {
2985 // <substitution> ::= St # ::std::
Anders Carlsson872ce0d2009-09-26 23:10:05 +00002986 if (const NamespaceDecl *NS = dyn_cast<NamespaceDecl>(ND)) {
Anders Carlsson5c9e7b12009-12-04 06:23:23 +00002987 if (isStd(NS)) {
Anders Carlsson872ce0d2009-09-26 23:10:05 +00002988 Out << "St";
2989 return true;
2990 }
2991 }
2992
2993 if (const ClassTemplateDecl *TD = dyn_cast<ClassTemplateDecl>(ND)) {
2994 if (!isStdNamespace(TD->getDeclContext()))
2995 return false;
Daniel Dunbard614e322009-11-21 09:05:47 +00002996
Anders Carlsson872ce0d2009-09-26 23:10:05 +00002997 // <substitution> ::= Sa # ::std::allocator
2998 if (TD->getIdentifier()->isStr("allocator")) {
2999 Out << "Sa";
3000 return true;
3001 }
Daniel Dunbard614e322009-11-21 09:05:47 +00003002
Anders Carlsson853bb502009-09-26 23:14:39 +00003003 // <<substitution> ::= Sb # ::std::basic_string
3004 if (TD->getIdentifier()->isStr("basic_string")) {
3005 Out << "Sb";
3006 return true;
3007 }
Anders Carlsson872ce0d2009-09-26 23:10:05 +00003008 }
Daniel Dunbard614e322009-11-21 09:05:47 +00003009
3010 if (const ClassTemplateSpecializationDecl *SD =
Anders Carlsson2e593522009-09-27 00:12:57 +00003011 dyn_cast<ClassTemplateSpecializationDecl>(ND)) {
Eli Friedman1e6bc132010-02-23 18:25:09 +00003012 if (!isStdNamespace(SD->getDeclContext()))
3013 return false;
3014
Anders Carlsson2e593522009-09-27 00:12:57 +00003015 // <substitution> ::= Ss # ::std::basic_string<char,
3016 // ::std::char_traits<char>,
3017 // ::std::allocator<char> >
3018 if (SD->getIdentifier()->isStr("basic_string")) {
3019 const TemplateArgumentList &TemplateArgs = SD->getTemplateArgs();
Daniel Dunbard614e322009-11-21 09:05:47 +00003020
Anders Carlsson2e593522009-09-27 00:12:57 +00003021 if (TemplateArgs.size() != 3)
3022 return false;
Daniel Dunbard614e322009-11-21 09:05:47 +00003023
Anders Carlsson2e593522009-09-27 00:12:57 +00003024 if (!isCharType(TemplateArgs[0].getAsType()))
3025 return false;
Daniel Dunbard614e322009-11-21 09:05:47 +00003026
Anders Carlsson2e593522009-09-27 00:12:57 +00003027 if (!isCharSpecialization(TemplateArgs[1].getAsType(), "char_traits"))
3028 return false;
Daniel Dunbard614e322009-11-21 09:05:47 +00003029
Anders Carlsson2e593522009-09-27 00:12:57 +00003030 if (!isCharSpecialization(TemplateArgs[2].getAsType(), "allocator"))
3031 return false;
3032
3033 Out << "Ss";
3034 return true;
3035 }
Daniel Dunbard614e322009-11-21 09:05:47 +00003036
Anders Carlsson1aaecfa2009-12-07 19:56:42 +00003037 // <substitution> ::= Si # ::std::basic_istream<char,
3038 // ::std::char_traits<char> >
3039 if (isStreamCharSpecialization(SD, "basic_istream")) {
3040 Out << "Si";
3041 return true;
3042 }
3043
Daniel Dunbard614e322009-11-21 09:05:47 +00003044 // <substitution> ::= So # ::std::basic_ostream<char,
Anders Carlsson3482b812009-10-08 17:20:26 +00003045 // ::std::char_traits<char> >
Anders Carlsson1aaecfa2009-12-07 19:56:42 +00003046 if (isStreamCharSpecialization(SD, "basic_ostream")) {
Anders Carlsson3482b812009-10-08 17:20:26 +00003047 Out << "So";
3048 return true;
3049 }
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00003050
Anders Carlsson1aaecfa2009-12-07 19:56:42 +00003051 // <substitution> ::= Sd # ::std::basic_iostream<char,
3052 // ::std::char_traits<char> >
3053 if (isStreamCharSpecialization(SD, "basic_iostream")) {
3054 Out << "Sd";
3055 return true;
3056 }
Anders Carlsson2e593522009-09-27 00:12:57 +00003057 }
Anders Carlsson872ce0d2009-09-26 23:10:05 +00003058 return false;
Anders Carlssonaa9e3c82009-09-26 20:53:44 +00003059}
3060
Anders Carlssonfeb60502009-09-17 00:43:46 +00003061void CXXNameMangler::addSubstitution(QualType T) {
Anders Carlsson296f8dc2009-09-26 03:55:37 +00003062 if (!T.getCVRQualifiers()) {
3063 if (const RecordType *RT = T->getAs<RecordType>()) {
3064 addSubstitution(RT->getDecl());
3065 return;
3066 }
3067 }
Daniel Dunbard614e322009-11-21 09:05:47 +00003068
Anders Carlssonfeb60502009-09-17 00:43:46 +00003069 uintptr_t TypePtr = reinterpret_cast<uintptr_t>(T.getAsOpaquePtr());
Anders Carlssona96f56f2009-09-17 03:53:28 +00003070 addSubstitution(TypePtr);
3071}
3072
Douglas Gregor7988def2010-04-28 05:58:56 +00003073void CXXNameMangler::addSubstitution(TemplateName Template) {
3074 if (TemplateDecl *TD = Template.getAsTemplateDecl())
3075 return addSubstitution(TD);
Alexis Hunta8136cc2010-05-05 15:23:54 +00003076
Douglas Gregor7988def2010-04-28 05:58:56 +00003077 Template = Context.getASTContext().getCanonicalTemplateName(Template);
3078 addSubstitution(reinterpret_cast<uintptr_t>(Template.getAsVoidPointer()));
3079}
3080
Anders Carlssona96f56f2009-09-17 03:53:28 +00003081void CXXNameMangler::addSubstitution(uintptr_t Ptr) {
Anders Carlssona96f56f2009-09-17 03:53:28 +00003082 assert(!Substitutions.count(Ptr) && "Substitution already exists!");
Anders Carlssond5639232010-06-02 04:29:50 +00003083 Substitutions[Ptr] = SeqID++;
Anders Carlssonfeb60502009-09-17 00:43:46 +00003084}
3085
Daniel Dunbaref5d75a2009-11-21 09:06:10 +00003086//
Mike Stump11289f42009-09-09 15:08:12 +00003087
Daniel Dunbaref5d75a2009-11-21 09:06:10 +00003088/// \brief Mangles the name of the declaration D and emits that name to the
3089/// given output stream.
3090///
3091/// If the declaration D requires a mangled name, this routine will emit that
3092/// mangled name to \p os and return true. Otherwise, \p os will be unchanged
3093/// and this routine will return false. In this case, the caller should just
3094/// emit the identifier of the declaration (\c D->getIdentifier()) as its
3095/// name.
Peter Collingbourne0ff0b372011-01-13 18:57:25 +00003096void ItaniumMangleContext::mangleName(const NamedDecl *D,
Rafael Espindolaa759d722011-02-11 01:41:00 +00003097 llvm::raw_ostream &Out) {
Daniel Dunbar161ade42009-11-21 09:14:44 +00003098 assert((isa<FunctionDecl>(D) || isa<VarDecl>(D)) &&
3099 "Invalid mangleName() call, argument is not a variable or function!");
3100 assert(!isa<CXXConstructorDecl>(D) && !isa<CXXDestructorDecl>(D) &&
3101 "Invalid mangleName() call on 'structor decl!");
Daniel Dunbard614e322009-11-21 09:05:47 +00003102
Daniel Dunbaref5d75a2009-11-21 09:06:10 +00003103 PrettyStackTraceDecl CrashInfo(D, SourceLocation(),
3104 getASTContext().getSourceManager(),
3105 "Mangling declaration");
Mike Stump11289f42009-09-09 15:08:12 +00003106
John McCall8fb0d9d2011-05-01 22:35:37 +00003107 CXXNameMangler Mangler(*this, Out, D);
Daniel Dunbare128dd12009-11-21 09:06:22 +00003108 return Mangler.mangle(D);
Daniel Dunbaref5d75a2009-11-21 09:06:10 +00003109}
Mike Stump11289f42009-09-09 15:08:12 +00003110
Peter Collingbourne0ff0b372011-01-13 18:57:25 +00003111void ItaniumMangleContext::mangleCXXCtor(const CXXConstructorDecl *D,
3112 CXXCtorType Type,
Rafael Espindolaa759d722011-02-11 01:41:00 +00003113 llvm::raw_ostream &Out) {
Rafael Espindolaac00f5d2011-02-10 23:59:36 +00003114 CXXNameMangler Mangler(*this, Out, D, Type);
Daniel Dunbar4118ec52009-11-21 09:06:31 +00003115 Mangler.mangle(D);
Daniel Dunbaref5d75a2009-11-21 09:06:10 +00003116}
Mike Stump11289f42009-09-09 15:08:12 +00003117
Peter Collingbourne0ff0b372011-01-13 18:57:25 +00003118void ItaniumMangleContext::mangleCXXDtor(const CXXDestructorDecl *D,
3119 CXXDtorType Type,
Rafael Espindolaa759d722011-02-11 01:41:00 +00003120 llvm::raw_ostream &Out) {
Rafael Espindolaac00f5d2011-02-10 23:59:36 +00003121 CXXNameMangler Mangler(*this, Out, D, Type);
Daniel Dunbar4118ec52009-11-21 09:06:31 +00003122 Mangler.mangle(D);
Daniel Dunbaref5d75a2009-11-21 09:06:10 +00003123}
Mike Stumpbc78a722009-07-31 18:25:34 +00003124
Peter Collingbourne0ff0b372011-01-13 18:57:25 +00003125void ItaniumMangleContext::mangleThunk(const CXXMethodDecl *MD,
3126 const ThunkInfo &Thunk,
Rafael Espindola3968cd02011-02-11 02:52:17 +00003127 llvm::raw_ostream &Out) {
Anders Carlssoncd836f02010-03-23 17:17:29 +00003128 // <special-name> ::= T <call-offset> <base encoding>
3129 // # base is the nominal target function of thunk
3130 // <special-name> ::= Tc <call-offset> <call-offset> <base encoding>
3131 // # base is the nominal target function of thunk
3132 // # first call-offset is 'this' adjustment
3133 // # second call-offset is result adjustment
Alexis Hunta8136cc2010-05-05 15:23:54 +00003134
Anders Carlssoncd836f02010-03-23 17:17:29 +00003135 assert(!isa<CXXDestructorDecl>(MD) &&
3136 "Use mangleCXXDtor for destructor decls!");
Rafael Espindolaac00f5d2011-02-10 23:59:36 +00003137 CXXNameMangler Mangler(*this, Out);
Anders Carlssoncd836f02010-03-23 17:17:29 +00003138 Mangler.getStream() << "_ZT";
3139 if (!Thunk.Return.isEmpty())
3140 Mangler.getStream() << 'c';
Alexis Hunta8136cc2010-05-05 15:23:54 +00003141
Anders Carlssoncd836f02010-03-23 17:17:29 +00003142 // Mangle the 'this' pointer adjustment.
3143 Mangler.mangleCallOffset(Thunk.This.NonVirtual, Thunk.This.VCallOffsetOffset);
Alexis Hunta8136cc2010-05-05 15:23:54 +00003144
Anders Carlssoncd836f02010-03-23 17:17:29 +00003145 // Mangle the return pointer adjustment if there is one.
3146 if (!Thunk.Return.isEmpty())
3147 Mangler.mangleCallOffset(Thunk.Return.NonVirtual,
3148 Thunk.Return.VBaseOffsetOffset);
Alexis Hunta8136cc2010-05-05 15:23:54 +00003149
Anders Carlssoncd836f02010-03-23 17:17:29 +00003150 Mangler.mangleFunctionEncoding(MD);
3151}
3152
Alexis Hunta8136cc2010-05-05 15:23:54 +00003153void
Peter Collingbourne0ff0b372011-01-13 18:57:25 +00003154ItaniumMangleContext::mangleCXXDtorThunk(const CXXDestructorDecl *DD,
3155 CXXDtorType Type,
3156 const ThisAdjustment &ThisAdjustment,
Rafael Espindola3968cd02011-02-11 02:52:17 +00003157 llvm::raw_ostream &Out) {
Anders Carlssoncd836f02010-03-23 17:17:29 +00003158 // <special-name> ::= T <call-offset> <base encoding>
3159 // # base is the nominal target function of thunk
Rafael Espindolaac00f5d2011-02-10 23:59:36 +00003160 CXXNameMangler Mangler(*this, Out, DD, Type);
Anders Carlssoncd836f02010-03-23 17:17:29 +00003161 Mangler.getStream() << "_ZT";
3162
3163 // Mangle the 'this' pointer adjustment.
Alexis Hunta8136cc2010-05-05 15:23:54 +00003164 Mangler.mangleCallOffset(ThisAdjustment.NonVirtual,
Anders Carlssoncd836f02010-03-23 17:17:29 +00003165 ThisAdjustment.VCallOffsetOffset);
3166
3167 Mangler.mangleFunctionEncoding(DD);
3168}
3169
Daniel Dunbar8483d212009-11-21 09:12:13 +00003170/// mangleGuardVariable - Returns the mangled name for a guard variable
3171/// for the passed in VarDecl.
Peter Collingbourne0ff0b372011-01-13 18:57:25 +00003172void ItaniumMangleContext::mangleItaniumGuardVariable(const VarDecl *D,
Rafael Espindola3968cd02011-02-11 02:52:17 +00003173 llvm::raw_ostream &Out) {
Daniel Dunbar8483d212009-11-21 09:12:13 +00003174 // <special-name> ::= GV <object name> # Guard variable for one-time
3175 // # initialization
Rafael Espindolaac00f5d2011-02-10 23:59:36 +00003176 CXXNameMangler Mangler(*this, Out);
Daniel Dunbar8483d212009-11-21 09:12:13 +00003177 Mangler.getStream() << "_ZGV";
3178 Mangler.mangleName(D);
3179}
3180
Peter Collingbourne0ff0b372011-01-13 18:57:25 +00003181void ItaniumMangleContext::mangleReferenceTemporary(const VarDecl *D,
Rafael Espindola3968cd02011-02-11 02:52:17 +00003182 llvm::raw_ostream &Out) {
Anders Carlsson709ef8e2010-06-26 16:09:40 +00003183 // We match the GCC mangling here.
3184 // <special-name> ::= GR <object name>
Rafael Espindolaac00f5d2011-02-10 23:59:36 +00003185 CXXNameMangler Mangler(*this, Out);
Anders Carlsson709ef8e2010-06-26 16:09:40 +00003186 Mangler.getStream() << "_ZGR";
3187 Mangler.mangleName(D);
3188}
3189
Peter Collingbourne0ff0b372011-01-13 18:57:25 +00003190void ItaniumMangleContext::mangleCXXVTable(const CXXRecordDecl *RD,
Rafael Espindola3968cd02011-02-11 02:52:17 +00003191 llvm::raw_ostream &Out) {
Daniel Dunbar8483d212009-11-21 09:12:13 +00003192 // <special-name> ::= TV <type> # virtual table
Rafael Espindolaac00f5d2011-02-10 23:59:36 +00003193 CXXNameMangler Mangler(*this, Out);
Daniel Dunbar8483d212009-11-21 09:12:13 +00003194 Mangler.getStream() << "_ZTV";
Douglas Gregor0094eb22010-05-26 05:11:13 +00003195 Mangler.mangleNameOrStandardSubstitution(RD);
Daniel Dunbaref5d75a2009-11-21 09:06:10 +00003196}
Mike Stump3b917692009-11-10 01:58:37 +00003197
Peter Collingbourne0ff0b372011-01-13 18:57:25 +00003198void ItaniumMangleContext::mangleCXXVTT(const CXXRecordDecl *RD,
Rafael Espindola3968cd02011-02-11 02:52:17 +00003199 llvm::raw_ostream &Out) {
Daniel Dunbar8483d212009-11-21 09:12:13 +00003200 // <special-name> ::= TT <type> # VTT structure
Rafael Espindolaac00f5d2011-02-10 23:59:36 +00003201 CXXNameMangler Mangler(*this, Out);
Daniel Dunbar8483d212009-11-21 09:12:13 +00003202 Mangler.getStream() << "_ZTT";
Douglas Gregor0094eb22010-05-26 05:11:13 +00003203 Mangler.mangleNameOrStandardSubstitution(RD);
Daniel Dunbaref5d75a2009-11-21 09:06:10 +00003204}
Mike Stumpef157442009-11-10 01:41:59 +00003205
Peter Collingbourne0ff0b372011-01-13 18:57:25 +00003206void ItaniumMangleContext::mangleCXXCtorVTable(const CXXRecordDecl *RD,
3207 int64_t Offset,
3208 const CXXRecordDecl *Type,
Rafael Espindola3968cd02011-02-11 02:52:17 +00003209 llvm::raw_ostream &Out) {
Daniel Dunbar8483d212009-11-21 09:12:13 +00003210 // <special-name> ::= TC <type> <offset number> _ <base type>
Rafael Espindolaac00f5d2011-02-10 23:59:36 +00003211 CXXNameMangler Mangler(*this, Out);
Daniel Dunbar8483d212009-11-21 09:12:13 +00003212 Mangler.getStream() << "_ZTC";
Douglas Gregor0094eb22010-05-26 05:11:13 +00003213 Mangler.mangleNameOrStandardSubstitution(RD);
Daniel Dunbar8483d212009-11-21 09:12:13 +00003214 Mangler.getStream() << Offset;
Benjamin Kramer1554e4a2010-04-10 16:03:31 +00003215 Mangler.getStream() << '_';
Douglas Gregor0094eb22010-05-26 05:11:13 +00003216 Mangler.mangleNameOrStandardSubstitution(Type);
Daniel Dunbaref5d75a2009-11-21 09:06:10 +00003217}
Mike Stump183c3d22009-07-31 23:15:31 +00003218
Peter Collingbourne0ff0b372011-01-13 18:57:25 +00003219void ItaniumMangleContext::mangleCXXRTTI(QualType Ty,
Rafael Espindola3968cd02011-02-11 02:52:17 +00003220 llvm::raw_ostream &Out) {
Daniel Dunbar8483d212009-11-21 09:12:13 +00003221 // <special-name> ::= TI <type> # typeinfo structure
Douglas Gregor247894b2009-12-23 22:04:40 +00003222 assert(!Ty.hasQualifiers() && "RTTI info cannot have top-level qualifiers");
Rafael Espindolaac00f5d2011-02-10 23:59:36 +00003223 CXXNameMangler Mangler(*this, Out);
Daniel Dunbar8483d212009-11-21 09:12:13 +00003224 Mangler.getStream() << "_ZTI";
3225 Mangler.mangleType(Ty);
Daniel Dunbaref5d75a2009-11-21 09:06:10 +00003226}
Mike Stump2ec5dd72009-11-14 00:14:13 +00003227
Peter Collingbourne0ff0b372011-01-13 18:57:25 +00003228void ItaniumMangleContext::mangleCXXRTTIName(QualType Ty,
Rafael Espindola3968cd02011-02-11 02:52:17 +00003229 llvm::raw_ostream &Out) {
Daniel Dunbar8483d212009-11-21 09:12:13 +00003230 // <special-name> ::= TS <type> # typeinfo name (null terminated byte string)
Rafael Espindolaac00f5d2011-02-10 23:59:36 +00003231 CXXNameMangler Mangler(*this, Out);
Daniel Dunbar8483d212009-11-21 09:12:13 +00003232 Mangler.getStream() << "_ZTS";
3233 Mangler.mangleType(Ty);
Mike Stumpbc78a722009-07-31 18:25:34 +00003234}
Peter Collingbourne0ff0b372011-01-13 18:57:25 +00003235
3236MangleContext *clang::createItaniumMangleContext(ASTContext &Context,
3237 Diagnostic &Diags) {
3238 return new ItaniumMangleContext(Context, Diags);
3239}