blob: 7f1ea3aaca462bcacb7b8ec06099859b8beca036 [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
John McCall3a4a4c52011-07-01 00:04:39 +0000240 void mangleExistingSubstitution(QualType type);
241 void mangleExistingSubstitution(TemplateName name);
242
Daniel Dunbaref5d75a2009-11-21 09:06:10 +0000243 bool mangleStandardSubstitution(const NamedDecl *ND);
Daniel Dunbard614e322009-11-21 09:05:47 +0000244
Daniel Dunbaref5d75a2009-11-21 09:06:10 +0000245 void addSubstitution(const NamedDecl *ND) {
246 ND = cast<NamedDecl>(ND->getCanonicalDecl());
Anders Carlssonce214312009-11-07 04:26:04 +0000247
Daniel Dunbaref5d75a2009-11-21 09:06:10 +0000248 addSubstitution(reinterpret_cast<uintptr_t>(ND));
249 }
250 void addSubstitution(QualType T);
Douglas Gregor7988def2010-04-28 05:58:56 +0000251 void addSubstitution(TemplateName Template);
Daniel Dunbaref5d75a2009-11-21 09:06:10 +0000252 void addSubstitution(uintptr_t Ptr);
Daniel Dunbard614e322009-11-21 09:05:47 +0000253
John McCall6dc0a2b2011-04-24 08:23:24 +0000254 void mangleUnresolvedPrefix(NestedNameSpecifier *qualifier,
255 NamedDecl *firstQualifierLookup,
256 bool recursive = false);
257 void mangleUnresolvedName(NestedNameSpecifier *qualifier,
258 NamedDecl *firstQualifierLookup,
259 DeclarationName name,
John McCall09de8ec2010-02-04 01:42:13 +0000260 unsigned KnownArity = UnknownArity);
261
Daniel Dunbaref5d75a2009-11-21 09:06:10 +0000262 void mangleName(const TemplateDecl *TD,
263 const TemplateArgument *TemplateArgs,
264 unsigned NumTemplateArgs);
John McCall09de8ec2010-02-04 01:42:13 +0000265 void mangleUnqualifiedName(const NamedDecl *ND) {
266 mangleUnqualifiedName(ND, ND->getDeclName(), UnknownArity);
267 }
268 void mangleUnqualifiedName(const NamedDecl *ND, DeclarationName Name,
269 unsigned KnownArity);
Daniel Dunbaref5d75a2009-11-21 09:06:10 +0000270 void mangleUnscopedName(const NamedDecl *ND);
271 void mangleUnscopedTemplateName(const TemplateDecl *ND);
Douglas Gregor7988def2010-04-28 05:58:56 +0000272 void mangleUnscopedTemplateName(TemplateName);
Daniel Dunbaref5d75a2009-11-21 09:06:10 +0000273 void mangleSourceName(const IdentifierInfo *II);
274 void mangleLocalName(const NamedDecl *ND);
Fariborz Jahaniana529c252010-03-03 19:41:08 +0000275 void mangleNestedName(const NamedDecl *ND, const DeclContext *DC,
276 bool NoFunction=false);
Daniel Dunbaref5d75a2009-11-21 09:06:10 +0000277 void mangleNestedName(const TemplateDecl *TD,
278 const TemplateArgument *TemplateArgs,
279 unsigned NumTemplateArgs);
John McCall6dc0a2b2011-04-24 08:23:24 +0000280 void manglePrefix(NestedNameSpecifier *qualifier);
Fariborz Jahaniana529c252010-03-03 19:41:08 +0000281 void manglePrefix(const DeclContext *DC, bool NoFunction=false);
John McCall3ab84762011-05-04 01:45:19 +0000282 void manglePrefix(QualType type);
Daniel Dunbaref5d75a2009-11-21 09:06:10 +0000283 void mangleTemplatePrefix(const TemplateDecl *ND);
Douglas Gregor17362712010-04-23 03:10:43 +0000284 void mangleTemplatePrefix(TemplateName Template);
Daniel Dunbaref5d75a2009-11-21 09:06:10 +0000285 void mangleOperatorName(OverloadedOperatorKind OO, unsigned Arity);
286 void mangleQualifiers(Qualifiers Quals);
Douglas Gregorf3ea1ed2011-01-26 17:36:28 +0000287 void mangleRefQualifier(RefQualifierKind RefQualifier);
John McCallcc5e23c2009-09-05 07:56:18 +0000288
Anders Carlssonbf5694602009-12-10 03:14:39 +0000289 void mangleObjCMethodName(const ObjCMethodDecl *MD);
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +0000290
Daniel Dunbaref5d75a2009-11-21 09:06:10 +0000291 // Declare manglers for every type class.
John McCallcc5e23c2009-09-05 07:56:18 +0000292#define ABSTRACT_TYPE(CLASS, PARENT)
293#define NON_CANONICAL_TYPE(CLASS, PARENT)
294#define TYPE(CLASS, PARENT) void mangleType(const CLASS##Type *T);
295#include "clang/AST/TypeNodes.def"
296
Daniel Dunbaref5d75a2009-11-21 09:06:10 +0000297 void mangleType(const TagType*);
John McCall4f3b5f32010-07-14 06:43:17 +0000298 void mangleType(TemplateName);
Daniel Dunbaref5d75a2009-11-21 09:06:10 +0000299 void mangleBareFunctionType(const FunctionType *T,
300 bool MangleReturnType);
Bob Wilsone4652b12010-11-16 00:32:18 +0000301 void mangleNeonVectorType(const VectorType *T);
Anders Carlssone66e2942009-12-14 01:45:37 +0000302
303 void mangleIntegerLiteral(QualType T, const llvm::APSInt &Value);
John McCall6dc0a2b2011-04-24 08:23:24 +0000304 void mangleMemberExpr(const Expr *base, bool isArrow,
305 NestedNameSpecifier *qualifier,
306 NamedDecl *firstQualifierLookup,
307 DeclarationName name,
308 unsigned knownArity);
John McCall78fbb612010-08-18 19:18:59 +0000309 void mangleExpression(const Expr *E, unsigned Arity = UnknownArity);
Daniel Dunbaref5d75a2009-11-21 09:06:10 +0000310 void mangleCXXCtorType(CXXCtorType T);
311 void mangleCXXDtorType(CXXDtorType T);
Mike Stump11289f42009-09-09 15:08:12 +0000312
John McCallf834bcd2010-08-20 00:17:19 +0000313 void mangleTemplateArgs(const ExplicitTemplateArgumentList &TemplateArgs);
Douglas Gregor17362712010-04-23 03:10:43 +0000314 void mangleTemplateArgs(TemplateName Template,
315 const TemplateArgument *TemplateArgs,
Alexis Hunta8136cc2010-05-05 15:23:54 +0000316 unsigned NumTemplateArgs);
Rafael Espindola4d5c3d92010-03-11 14:07:00 +0000317 void mangleTemplateArgs(const TemplateParameterList &PL,
318 const TemplateArgument *TemplateArgs,
Daniel Dunbaref5d75a2009-11-21 09:06:10 +0000319 unsigned NumTemplateArgs);
Rafael Espindola4d5c3d92010-03-11 14:07:00 +0000320 void mangleTemplateArgs(const TemplateParameterList &PL,
321 const TemplateArgumentList &AL);
322 void mangleTemplateArg(const NamedDecl *P, const TemplateArgument &A);
John McCall3ab84762011-05-04 01:45:19 +0000323 void mangleUnresolvedTemplateArgs(const TemplateArgument *args,
324 unsigned numArgs);
Daniel Dunbard614e322009-11-21 09:05:47 +0000325
Daniel Dunbaref5d75a2009-11-21 09:06:10 +0000326 void mangleTemplateParameter(unsigned Index);
John McCall8fb0d9d2011-05-01 22:35:37 +0000327
328 void mangleFunctionParam(const ParmVarDecl *parm);
Daniel Dunbaref5d75a2009-11-21 09:06:10 +0000329};
Peter Collingbourne0ff0b372011-01-13 18:57:25 +0000330
Douglas Gregor5fec5b02009-02-13 00:10:09 +0000331}
332
Anders Carlsson810679c2009-04-02 15:51:53 +0000333static bool isInCLinkageSpecification(const Decl *D) {
Douglas Gregor05925032009-10-28 16:31:34 +0000334 D = D->getCanonicalDecl();
Mike Stump11289f42009-09-09 15:08:12 +0000335 for (const DeclContext *DC = D->getDeclContext();
Anders Carlsson810679c2009-04-02 15:51:53 +0000336 !DC->isTranslationUnit(); DC = DC->getParent()) {
Mike Stump11289f42009-09-09 15:08:12 +0000337 if (const LinkageSpecDecl *Linkage = dyn_cast<LinkageSpecDecl>(DC))
Anders Carlsson810679c2009-04-02 15:51:53 +0000338 return Linkage->getLanguage() == LinkageSpecDecl::lang_c;
339 }
Mike Stump11289f42009-09-09 15:08:12 +0000340
Anders Carlsson810679c2009-04-02 15:51:53 +0000341 return false;
342}
343
Peter Collingbourne0ff0b372011-01-13 18:57:25 +0000344bool ItaniumMangleContext::shouldMangleDeclName(const NamedDecl *D) {
Daniel Dunbare949e6c2009-11-21 09:14:52 +0000345 // In C, functions with no attributes never need to be mangled. Fastpath them.
346 if (!getASTContext().getLangOptions().CPlusPlus && !D->hasAttrs())
347 return false;
348
349 // Any decl can be declared with __asm("foo") on it, and this takes precedence
350 // over all other naming in the .o file.
351 if (D->hasAttr<AsmLabelAttr>())
352 return true;
353
Mike Stump9cc7d302009-09-02 00:25:38 +0000354 // Clang's "overloadable" attribute extension to C/C++ implies name mangling
Anders Carlssonc0a35612009-11-07 07:15:03 +0000355 // (always) as does passing a C++ member function and a function
356 // whose name is not a simple identifier.
Daniel Dunbare949e6c2009-11-21 09:14:52 +0000357 const FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
358 if (FD && (FD->hasAttr<OverloadableAttr>() || isa<CXXMethodDecl>(FD) ||
359 !FD->getDeclName().isIdentifier()))
360 return true;
Mike Stump11289f42009-09-09 15:08:12 +0000361
Daniel Dunbare949e6c2009-11-21 09:14:52 +0000362 // Otherwise, no mangling is done outside C++ mode.
363 if (!getASTContext().getLangOptions().CPlusPlus)
364 return false;
Mike Stump11289f42009-09-09 15:08:12 +0000365
Alexis Huntaecc45c2010-01-24 03:04:27 +0000366 // Variables at global scope with non-internal linkage are not mangled
Eli Friedmand4df7752009-12-02 20:32:49 +0000367 if (!FD) {
368 const DeclContext *DC = D->getDeclContext();
369 // Check for extern variable declared locally.
Fariborz Jahanian02995322010-06-30 18:57:21 +0000370 if (DC->isFunctionOrMethod() && D->hasLinkage())
Eli Friedmand4df7752009-12-02 20:32:49 +0000371 while (!DC->isNamespace() && !DC->isTranslationUnit())
372 DC = DC->getParent();
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000373 if (DC->isTranslationUnit() && D->getLinkage() != InternalLinkage)
Eli Friedmand4df7752009-12-02 20:32:49 +0000374 return false;
375 }
376
Eli Friedmanf8340de2010-07-18 20:49:59 +0000377 // Class members are always mangled.
378 if (D->getDeclContext()->isRecord())
379 return true;
380
Eli Friedmand4df7752009-12-02 20:32:49 +0000381 // C functions and "main" are not mangled.
382 if ((FD && FD->isMain()) || isInCLinkageSpecification(D))
Daniel Dunbare949e6c2009-11-21 09:14:52 +0000383 return false;
384
Anders Carlsson810679c2009-04-02 15:51:53 +0000385 return true;
386}
Douglas Gregor5fec5b02009-02-13 00:10:09 +0000387
Daniel Dunbar88ad4c52009-11-21 09:17:15 +0000388void CXXNameMangler::mangle(const NamedDecl *D, llvm::StringRef Prefix) {
Mike Stump9cc7d302009-09-02 00:25:38 +0000389 // Any decl can be declared with __asm("foo") on it, and this takes precedence
390 // over all other naming in the .o file.
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000391 if (const AsmLabelAttr *ALA = D->getAttr<AsmLabelAttr>()) {
Chris Lattner65749062009-03-21 08:24:40 +0000392 // If we have an asm name, then we use it as the mangling.
Rafael Espindolad62acb52011-02-15 22:23:51 +0000393
394 // Adding the prefix can cause problems when one file has a "foo" and
395 // another has a "\01foo". That is known to happen on ELF with the
396 // tricks normally used for producing aliases (PR9177). Fortunately the
397 // llvm mangler on ELF is a nop, so we can just avoid adding the \01
Peter Collingbourneba3e6662011-04-06 12:29:09 +0000398 // marker. We also avoid adding the marker if this is an alias for an
399 // LLVM intrinsic.
Rafael Espindolad62acb52011-02-15 22:23:51 +0000400 llvm::StringRef UserLabelPrefix =
401 getASTContext().Target.getUserLabelPrefix();
Peter Collingbourneba3e6662011-04-06 12:29:09 +0000402 if (!UserLabelPrefix.empty() && !ALA->getLabel().startswith("llvm."))
Rafael Espindolad62acb52011-02-15 22:23:51 +0000403 Out << '\01'; // LLVM IR Marker for __asm("foo")
404
Chris Lattner65749062009-03-21 08:24:40 +0000405 Out << ALA->getLabel();
Daniel Dunbare949e6c2009-11-21 09:14:52 +0000406 return;
Chris Lattner65749062009-03-21 08:24:40 +0000407 }
Mike Stump11289f42009-09-09 15:08:12 +0000408
Alexis Huntaecc45c2010-01-24 03:04:27 +0000409 // <mangled-name> ::= _Z <encoding>
Douglas Gregor5fec5b02009-02-13 00:10:09 +0000410 // ::= <data name>
411 // ::= <special-name>
Daniel Dunbar88ad4c52009-11-21 09:17:15 +0000412 Out << Prefix;
413 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
Daniel Dunbare949e6c2009-11-21 09:14:52 +0000414 mangleFunctionEncoding(FD);
Rafael Espindola4d5c3d92010-03-11 14:07:00 +0000415 else if (const VarDecl *VD = dyn_cast<VarDecl>(D))
416 mangleName(VD);
Daniel Dunbar88ad4c52009-11-21 09:17:15 +0000417 else
Rafael Espindola4d5c3d92010-03-11 14:07:00 +0000418 mangleName(cast<FieldDecl>(D));
Douglas Gregor5fec5b02009-02-13 00:10:09 +0000419}
420
421void CXXNameMangler::mangleFunctionEncoding(const FunctionDecl *FD) {
422 // <encoding> ::= <function name> <bare-function-type>
423 mangleName(FD);
Mike Stump11289f42009-09-09 15:08:12 +0000424
Daniel Dunbar88ad4c52009-11-21 09:17:15 +0000425 // Don't mangle in the type if this isn't a decl we should typically mangle.
426 if (!Context.shouldMangleDeclName(FD))
427 return;
428
Mike Stump9cc7d302009-09-02 00:25:38 +0000429 // Whether the mangling of a function type includes the return type depends on
430 // the context and the nature of the function. The rules for deciding whether
431 // the return type is included are:
Mike Stump11289f42009-09-09 15:08:12 +0000432 //
Douglas Gregore8925db2009-06-29 22:39:32 +0000433 // 1. Template functions (names or types) have return types encoded, with
434 // the exceptions listed below.
Mike Stump11289f42009-09-09 15:08:12 +0000435 // 2. Function types not appearing as part of a function name mangling,
Douglas Gregore8925db2009-06-29 22:39:32 +0000436 // e.g. parameters, pointer types, etc., have return type encoded, with the
437 // exceptions listed below.
438 // 3. Non-template function names do not have return types encoded.
439 //
Mike Stump9cc7d302009-09-02 00:25:38 +0000440 // The exceptions mentioned in (1) and (2) above, for which the return type is
441 // never included, are
Douglas Gregore8925db2009-06-29 22:39:32 +0000442 // 1. Constructors.
443 // 2. Destructors.
444 // 3. Conversion operator functions, e.g. operator int.
445 bool MangleReturnType = false;
Anders Carlssondf644fb2009-09-17 03:46:43 +0000446 if (FunctionTemplateDecl *PrimaryTemplate = FD->getPrimaryTemplate()) {
447 if (!(isa<CXXConstructorDecl>(FD) || isa<CXXDestructorDecl>(FD) ||
448 isa<CXXConversionDecl>(FD)))
449 MangleReturnType = true;
Daniel Dunbard614e322009-11-21 09:05:47 +0000450
Anders Carlssondf644fb2009-09-17 03:46:43 +0000451 // Mangle the type of the primary template.
452 FD = PrimaryTemplate->getTemplatedDecl();
453 }
454
Douglas Gregor84280642011-07-12 04:42:08 +0000455 mangleBareFunctionType(FD->getType()->getAs<FunctionType>(),
456 MangleReturnType);
Douglas Gregor5fec5b02009-02-13 00:10:09 +0000457}
458
Anders Carlsson5c9e7b12009-12-04 06:23:23 +0000459static const DeclContext *IgnoreLinkageSpecDecls(const DeclContext *DC) {
460 while (isa<LinkageSpecDecl>(DC)) {
Anders Carlsson5c9e7b12009-12-04 06:23:23 +0000461 DC = DC->getParent();
462 }
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +0000463
Anders Carlsson5c9e7b12009-12-04 06:23:23 +0000464 return DC;
465}
466
Anders Carlssonb4d2cdb2010-06-02 15:58:27 +0000467/// isStd - Return whether a given namespace is the 'std' namespace.
468static bool isStd(const NamespaceDecl *NS) {
469 if (!IgnoreLinkageSpecDecls(NS->getParent())->isTranslationUnit())
470 return false;
471
472 const IdentifierInfo *II = NS->getOriginalNamespace()->getIdentifier();
473 return II && II->isStr("std");
474}
475
Anders Carlsson5c9e7b12009-12-04 06:23:23 +0000476// isStdNamespace - Return whether a given decl context is a toplevel 'std'
477// namespace.
Daniel Dunbarfdae6f72009-11-21 09:11:45 +0000478static bool isStdNamespace(const DeclContext *DC) {
Anders Carlsson5c9e7b12009-12-04 06:23:23 +0000479 if (!DC->isNamespace())
480 return false;
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +0000481
Anders Carlsson5c9e7b12009-12-04 06:23:23 +0000482 return isStd(cast<NamespaceDecl>(DC));
Daniel Dunbarfdae6f72009-11-21 09:11:45 +0000483}
484
Anders Carlsson9f8e3d12009-09-26 03:24:57 +0000485static const TemplateDecl *
486isTemplate(const NamedDecl *ND, const TemplateArgumentList *&TemplateArgs) {
Anders Carlsson559d9742009-09-18 19:00:18 +0000487 // Check if we have a function template.
488 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)){
Anders Carlsson9f8e3d12009-09-26 03:24:57 +0000489 if (const TemplateDecl *TD = FD->getPrimaryTemplate()) {
Anders Carlsson559d9742009-09-18 19:00:18 +0000490 TemplateArgs = FD->getTemplateSpecializationArgs();
Anders Carlsson9f8e3d12009-09-26 03:24:57 +0000491 return TD;
Anders Carlsson559d9742009-09-18 19:00:18 +0000492 }
493 }
494
Anders Carlssonc3773bd2009-09-18 19:44:50 +0000495 // Check if we have a class template.
496 if (const ClassTemplateSpecializationDecl *Spec =
497 dyn_cast<ClassTemplateSpecializationDecl>(ND)) {
498 TemplateArgs = &Spec->getTemplateArgs();
Anders Carlsson9f8e3d12009-09-26 03:24:57 +0000499 return Spec->getSpecializedTemplate();
Anders Carlssonc3773bd2009-09-18 19:44:50 +0000500 }
Daniel Dunbard614e322009-11-21 09:05:47 +0000501
Anders Carlsson559d9742009-09-18 19:00:18 +0000502 return 0;
503}
504
Douglas Gregor5fec5b02009-02-13 00:10:09 +0000505void CXXNameMangler::mangleName(const NamedDecl *ND) {
506 // <name> ::= <nested-name>
507 // ::= <unscoped-name>
508 // ::= <unscoped-template-name> <template-args>
Anders Carlsson98e00bb2009-09-17 03:17:01 +0000509 // ::= <local-name>
Douglas Gregor5fec5b02009-02-13 00:10:09 +0000510 //
Anders Carlssonca51ef12009-09-17 16:12:20 +0000511 const DeclContext *DC = ND->getDeclContext();
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +0000512
Eli Friedmand4df7752009-12-02 20:32:49 +0000513 // If this is an extern variable declared locally, the relevant DeclContext
514 // is that of the containing namespace, or the translation unit.
515 if (isa<FunctionDecl>(DC) && ND->hasLinkage())
516 while (!DC->isNamespace() && !DC->isTranslationUnit())
517 DC = DC->getParent();
John McCall18e4eda2010-10-18 21:28:44 +0000518 else if (GetLocalClassDecl(ND)) {
519 mangleLocalName(ND);
520 return;
521 }
Eli Friedmand4df7752009-12-02 20:32:49 +0000522
Anders Carlssonfc51cc92009-09-22 17:23:30 +0000523 while (isa<LinkageSpecDecl>(DC))
Anders Carlssonca51ef12009-09-17 16:12:20 +0000524 DC = DC->getParent();
Daniel Dunbard614e322009-11-21 09:05:47 +0000525
Anders Carlssonca51ef12009-09-17 16:12:20 +0000526 if (DC->isTranslationUnit() || isStdNamespace(DC)) {
Anders Carlsson559d9742009-09-18 19:00:18 +0000527 // Check if we have a template.
528 const TemplateArgumentList *TemplateArgs = 0;
Anders Carlsson26e67af2009-09-26 19:45:45 +0000529 if (const TemplateDecl *TD = isTemplate(ND, TemplateArgs)) {
Anders Carlsson559d9742009-09-18 19:00:18 +0000530 mangleUnscopedTemplateName(TD);
Rafael Espindola4d5c3d92010-03-11 14:07:00 +0000531 TemplateParameterList *TemplateParameters = TD->getTemplateParameters();
532 mangleTemplateArgs(*TemplateParameters, *TemplateArgs);
Anders Carlsson559d9742009-09-18 19:00:18 +0000533 return;
Anders Carlsson2b5e1dd2009-09-18 04:29:09 +0000534 }
Douglas Gregor5fec5b02009-02-13 00:10:09 +0000535
Anders Carlsson2b5e1dd2009-09-18 04:29:09 +0000536 mangleUnscopedName(ND);
537 return;
538 }
Daniel Dunbard614e322009-11-21 09:05:47 +0000539
Anders Carlssonbf5694602009-12-10 03:14:39 +0000540 if (isa<FunctionDecl>(DC) || isa<ObjCMethodDecl>(DC)) {
Anders Carlsson2b5e1dd2009-09-18 04:29:09 +0000541 mangleLocalName(ND);
542 return;
543 }
Daniel Dunbard614e322009-11-21 09:05:47 +0000544
Eli Friedmand4df7752009-12-02 20:32:49 +0000545 mangleNestedName(ND, DC);
Anders Carlsson2b5e1dd2009-09-18 04:29:09 +0000546}
Daniel Dunbard614e322009-11-21 09:05:47 +0000547void CXXNameMangler::mangleName(const TemplateDecl *TD,
Anders Carlsson7a8a74f2009-09-18 02:42:01 +0000548 const TemplateArgument *TemplateArgs,
549 unsigned NumTemplateArgs) {
Anders Carlsson5c9e7b12009-12-04 06:23:23 +0000550 const DeclContext *DC = IgnoreLinkageSpecDecls(TD->getDeclContext());
Daniel Dunbard614e322009-11-21 09:05:47 +0000551
Anders Carlsson7a8a74f2009-09-18 02:42:01 +0000552 if (DC->isTranslationUnit() || isStdNamespace(DC)) {
Anders Carlsson26e67af2009-09-26 19:45:45 +0000553 mangleUnscopedTemplateName(TD);
Rafael Espindola4d5c3d92010-03-11 14:07:00 +0000554 TemplateParameterList *TemplateParameters = TD->getTemplateParameters();
555 mangleTemplateArgs(*TemplateParameters, TemplateArgs, NumTemplateArgs);
Anders Carlsson7a8a74f2009-09-18 02:42:01 +0000556 } else {
557 mangleNestedName(TD, TemplateArgs, NumTemplateArgs);
558 }
559}
560
Anders Carlsson98e00bb2009-09-17 03:17:01 +0000561void CXXNameMangler::mangleUnscopedName(const NamedDecl *ND) {
562 // <unscoped-name> ::= <unqualified-name>
563 // ::= St <unqualified-name> # ::std::
564 if (isStdNamespace(ND->getDeclContext()))
565 Out << "St";
Daniel Dunbard614e322009-11-21 09:05:47 +0000566
Anders Carlsson98e00bb2009-09-17 03:17:01 +0000567 mangleUnqualifiedName(ND);
568}
569
Anders Carlsson26e67af2009-09-26 19:45:45 +0000570void CXXNameMangler::mangleUnscopedTemplateName(const TemplateDecl *ND) {
Anders Carlsson98e00bb2009-09-17 03:17:01 +0000571 // <unscoped-template-name> ::= <unscoped-name>
572 // ::= <substitution>
Anders Carlsson7a8a74f2009-09-18 02:42:01 +0000573 if (mangleSubstitution(ND))
Anders Carlssona2fb9bc2009-09-17 04:02:31 +0000574 return;
Daniel Dunbard614e322009-11-21 09:05:47 +0000575
Douglas Gregora16b0ca2010-02-05 20:45:00 +0000576 // <template-template-param> ::= <template-param>
577 if (const TemplateTemplateParmDecl *TTP
578 = dyn_cast<TemplateTemplateParmDecl>(ND)) {
579 mangleTemplateParameter(TTP->getIndex());
580 return;
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +0000581 }
Douglas Gregora16b0ca2010-02-05 20:45:00 +0000582
Anders Carlsson49232b92009-09-26 20:13:56 +0000583 mangleUnscopedName(ND->getTemplatedDecl());
Anders Carlsson7a8a74f2009-09-18 02:42:01 +0000584 addSubstitution(ND);
Anders Carlsson98e00bb2009-09-17 03:17:01 +0000585}
586
Douglas Gregor7988def2010-04-28 05:58:56 +0000587void CXXNameMangler::mangleUnscopedTemplateName(TemplateName Template) {
588 // <unscoped-template-name> ::= <unscoped-name>
589 // ::= <substitution>
590 if (TemplateDecl *TD = Template.getAsTemplateDecl())
591 return mangleUnscopedTemplateName(TD);
Alexis Hunta8136cc2010-05-05 15:23:54 +0000592
Douglas Gregor7988def2010-04-28 05:58:56 +0000593 if (mangleSubstitution(Template))
594 return;
595
596 // FIXME: How to cope with operators here?
597 DependentTemplateName *Dependent = Template.getAsDependentTemplateName();
598 assert(Dependent && "Not a dependent template name?");
599 if (!Dependent->isIdentifier()) {
600 // FIXME: We can't possibly know the arity of the operator here!
601 Diagnostic &Diags = Context.getDiags();
602 unsigned DiagID = Diags.getCustomDiagID(Diagnostic::Error,
603 "cannot mangle dependent operator name");
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000604 Diags.Report(DiagID);
Douglas Gregor7988def2010-04-28 05:58:56 +0000605 return;
606 }
Alexis Hunta8136cc2010-05-05 15:23:54 +0000607
Douglas Gregor7988def2010-04-28 05:58:56 +0000608 mangleSourceName(Dependent->getIdentifier());
609 addSubstitution(Template);
610}
611
John McCalld8d1e2a2011-04-24 03:07:16 +0000612void CXXNameMangler::mangleFloat(const llvm::APFloat &f) {
613 // ABI:
614 // Floating-point literals are encoded using a fixed-length
615 // lowercase hexadecimal string corresponding to the internal
616 // representation (IEEE on Itanium), high-order bytes first,
617 // without leading zeroes. For example: "Lf bf800000 E" is -1.0f
618 // on Itanium.
619 // APInt::toString uses uppercase hexadecimal, and it's not really
620 // worth embellishing that interface for this use case, so we just
621 // do a second pass to lowercase things.
622 typedef llvm::SmallString<20> buffer_t;
623 buffer_t buffer;
624 f.bitcastToAPInt().toString(buffer, 16, false);
625
626 for (buffer_t::iterator i = buffer.begin(), e = buffer.end(); i != e; ++i)
627 if (isupper(*i)) *i = tolower(*i);
628
629 Out.write(buffer.data(), buffer.size());
John McCall05ddbb82010-07-14 04:20:34 +0000630}
631
632void CXXNameMangler::mangleNumber(const llvm::APSInt &Value) {
633 if (Value.isSigned() && Value.isNegative()) {
634 Out << 'n';
635 Value.abs().print(Out, true);
636 } else
637 Value.print(Out, Value.isSigned());
638}
639
Anders Carlssonc7785402009-11-26 02:32:05 +0000640void CXXNameMangler::mangleNumber(int64_t Number) {
641 // <number> ::= [n] <non-negative decimal integer>
642 if (Number < 0) {
643 Out << 'n';
644 Number = -Number;
645 }
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +0000646
Anders Carlssonc7785402009-11-26 02:32:05 +0000647 Out << Number;
648}
649
Anders Carlssoncd836f02010-03-23 17:17:29 +0000650void CXXNameMangler::mangleCallOffset(int64_t NonVirtual, int64_t Virtual) {
Mike Stump9cc7d302009-09-02 00:25:38 +0000651 // <call-offset> ::= h <nv-offset> _
652 // ::= v <v-offset> _
653 // <nv-offset> ::= <offset number> # non-virtual base override
Anders Carlssonc7785402009-11-26 02:32:05 +0000654 // <v-offset> ::= <offset number> _ <virtual offset number>
Mike Stump9cc7d302009-09-02 00:25:38 +0000655 // # virtual base override, with vcall offset
Anders Carlssoncd836f02010-03-23 17:17:29 +0000656 if (!Virtual) {
Anders Carlssonc7785402009-11-26 02:32:05 +0000657 Out << 'h';
Anders Carlssoncd836f02010-03-23 17:17:29 +0000658 mangleNumber(NonVirtual);
Anders Carlssonc7785402009-11-26 02:32:05 +0000659 Out << '_';
660 return;
Mike Stump9cc7d302009-09-02 00:25:38 +0000661 }
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +0000662
Anders Carlssonc7785402009-11-26 02:32:05 +0000663 Out << 'v';
Anders Carlssoncd836f02010-03-23 17:17:29 +0000664 mangleNumber(NonVirtual);
Anders Carlssonc7785402009-11-26 02:32:05 +0000665 Out << '_';
Anders Carlssoncd836f02010-03-23 17:17:29 +0000666 mangleNumber(Virtual);
Anders Carlssonc7785402009-11-26 02:32:05 +0000667 Out << '_';
Mike Stump3f707e92009-09-02 00:56:18 +0000668}
669
John McCall3ab84762011-05-04 01:45:19 +0000670void CXXNameMangler::manglePrefix(QualType type) {
John McCall6dc0a2b2011-04-24 08:23:24 +0000671 if (const TemplateSpecializationType *TST =
672 type->getAs<TemplateSpecializationType>()) {
673 if (!mangleSubstitution(QualType(TST, 0))) {
674 mangleTemplatePrefix(TST->getTemplateName());
Alexis Hunta8136cc2010-05-05 15:23:54 +0000675
Douglas Gregor6e068012011-02-28 00:04:36 +0000676 // FIXME: GCC does not appear to mangle the template arguments when
677 // the template in question is a dependent template name. Should we
678 // emulate that badness?
John McCall6dc0a2b2011-04-24 08:23:24 +0000679 mangleTemplateArgs(TST->getTemplateName(), TST->getArgs(),
680 TST->getNumArgs());
681 addSubstitution(QualType(TST, 0));
Rafael Espindolacd7eef92010-03-17 04:28:11 +0000682 }
John McCall6dc0a2b2011-04-24 08:23:24 +0000683 } else if (const DependentTemplateSpecializationType *DTST
684 = type->getAs<DependentTemplateSpecializationType>()) {
685 TemplateName Template
686 = getASTContext().getDependentTemplateName(DTST->getQualifier(),
687 DTST->getIdentifier());
688 mangleTemplatePrefix(Template);
689
690 // FIXME: GCC does not appear to mangle the template arguments when
691 // the template in question is a dependent template name. Should we
692 // emulate that badness?
693 mangleTemplateArgs(Template, DTST->getArgs(), DTST->getNumArgs());
694 } else {
695 // We use the QualType mangle type variant here because it handles
696 // substitutions.
697 mangleType(type);
John McCall09de8ec2010-02-04 01:42:13 +0000698 }
699}
700
John McCall6dc0a2b2011-04-24 08:23:24 +0000701/// Mangle everything prior to the base-unresolved-name in an unresolved-name.
702///
703/// \param firstQualifierLookup - the entity found by unqualified lookup
704/// for the first name in the qualifier, if this is for a member expression
705/// \param recursive - true if this is being called recursively,
706/// i.e. if there is more prefix "to the right".
707void CXXNameMangler::mangleUnresolvedPrefix(NestedNameSpecifier *qualifier,
708 NamedDecl *firstQualifierLookup,
709 bool recursive) {
John McCall09de8ec2010-02-04 01:42:13 +0000710
John McCall6dc0a2b2011-04-24 08:23:24 +0000711 // x, ::x
712 // <unresolved-name> ::= [gs] <base-unresolved-name>
713
714 // T::x / decltype(p)::x
715 // <unresolved-name> ::= sr <unresolved-type> <base-unresolved-name>
716
717 // T::N::x /decltype(p)::N::x
718 // <unresolved-name> ::= srN <unresolved-type> <unresolved-qualifier-level>+ E
719 // <base-unresolved-name>
720
721 // A::x, N::y, A<T>::z; "gs" means leading "::"
722 // <unresolved-name> ::= [gs] sr <unresolved-qualifier-level>+ E
723 // <base-unresolved-name>
724
725 switch (qualifier->getKind()) {
726 case NestedNameSpecifier::Global:
727 Out << "gs";
728
729 // We want an 'sr' unless this is the entire NNS.
730 if (recursive)
731 Out << "sr";
732
733 // We never want an 'E' here.
734 return;
735
736 case NestedNameSpecifier::Namespace:
737 if (qualifier->getPrefix())
738 mangleUnresolvedPrefix(qualifier->getPrefix(), firstQualifierLookup,
739 /*recursive*/ true);
740 else
741 Out << "sr";
742 mangleSourceName(qualifier->getAsNamespace()->getIdentifier());
743 break;
744 case NestedNameSpecifier::NamespaceAlias:
745 if (qualifier->getPrefix())
746 mangleUnresolvedPrefix(qualifier->getPrefix(), firstQualifierLookup,
747 /*recursive*/ true);
748 else
749 Out << "sr";
750 mangleSourceName(qualifier->getAsNamespaceAlias()->getIdentifier());
751 break;
752
753 case NestedNameSpecifier::TypeSpec:
754 case NestedNameSpecifier::TypeSpecWithTemplate: {
John McCall3ab84762011-05-04 01:45:19 +0000755 const Type *type = qualifier->getAsType();
John McCall6dc0a2b2011-04-24 08:23:24 +0000756
John McCall3ab84762011-05-04 01:45:19 +0000757 // We only want to use an unresolved-type encoding if this is one of:
758 // - a decltype
759 // - a template type parameter
760 // - a template template parameter with arguments
761 // In all of these cases, we should have no prefix.
762 if (qualifier->getPrefix()) {
763 mangleUnresolvedPrefix(qualifier->getPrefix(), firstQualifierLookup,
764 /*recursive*/ true);
765 } else {
766 // Otherwise, all the cases want this.
767 Out << "sr";
John McCall3ab84762011-05-04 01:45:19 +0000768 }
769
John McCall3ab84762011-05-04 01:45:19 +0000770 // Only certain other types are valid as prefixes; enumerate them.
John McCall15547bb2011-06-28 16:49:23 +0000771 switch (type->getTypeClass()) {
772 case Type::Builtin:
773 case Type::Complex:
774 case Type::Pointer:
775 case Type::BlockPointer:
776 case Type::LValueReference:
777 case Type::RValueReference:
778 case Type::MemberPointer:
779 case Type::ConstantArray:
780 case Type::IncompleteArray:
781 case Type::VariableArray:
782 case Type::DependentSizedArray:
783 case Type::DependentSizedExtVector:
784 case Type::Vector:
785 case Type::ExtVector:
786 case Type::FunctionProto:
787 case Type::FunctionNoProto:
788 case Type::Enum:
789 case Type::Paren:
790 case Type::Elaborated:
791 case Type::Attributed:
792 case Type::Auto:
793 case Type::PackExpansion:
John McCall15547bb2011-06-28 16:49:23 +0000794 case Type::ObjCObject:
795 case Type::ObjCInterface:
796 case Type::ObjCObjectPointer:
797 llvm_unreachable("type is illegal as a nested name specifier");
798
John McCall3a4a4c52011-07-01 00:04:39 +0000799 case Type::SubstTemplateTypeParmPack:
800 // FIXME: not clear how to mangle this!
801 // template <class T...> class A {
802 // template <class U...> void foo(decltype(T::foo(U())) x...);
803 // };
804 Out << "_SUBSTPACK_";
805 break;
806
John McCall15547bb2011-06-28 16:49:23 +0000807 // <unresolved-type> ::= <template-param>
808 // ::= <decltype>
809 // ::= <template-template-param> <template-args>
810 // (this last is not official yet)
811 case Type::TypeOfExpr:
812 case Type::TypeOf:
813 case Type::Decltype:
814 case Type::TemplateTypeParm:
815 case Type::UnaryTransform:
John McCall2de1c332011-07-01 02:19:08 +0000816 case Type::SubstTemplateTypeParm:
John McCall15547bb2011-06-28 16:49:23 +0000817 unresolvedType:
818 assert(!qualifier->getPrefix());
819
820 // We only get here recursively if we're followed by identifiers.
821 if (recursive) Out << 'N';
822
John McCall2de1c332011-07-01 02:19:08 +0000823 // This seems to do everything we want. It's not really
824 // sanctioned for a substituted template parameter, though.
John McCall15547bb2011-06-28 16:49:23 +0000825 mangleType(QualType(type, 0));
826
827 // We never want to print 'E' directly after an unresolved-type,
828 // so we return directly.
829 return;
830
John McCall15547bb2011-06-28 16:49:23 +0000831 case Type::Typedef:
832 mangleSourceName(cast<TypedefType>(type)->getDecl()->getIdentifier());
833 break;
834
835 case Type::UnresolvedUsing:
836 mangleSourceName(cast<UnresolvedUsingType>(type)->getDecl()
837 ->getIdentifier());
838 break;
839
840 case Type::Record:
841 mangleSourceName(cast<RecordType>(type)->getDecl()->getIdentifier());
842 break;
843
844 case Type::TemplateSpecialization: {
845 const TemplateSpecializationType *tst
846 = cast<TemplateSpecializationType>(type);
John McCall3a4a4c52011-07-01 00:04:39 +0000847 TemplateName name = tst->getTemplateName();
848 switch (name.getKind()) {
849 case TemplateName::Template:
850 case TemplateName::QualifiedTemplate: {
851 TemplateDecl *temp = name.getAsTemplateDecl();
John McCall15547bb2011-06-28 16:49:23 +0000852
John McCall3a4a4c52011-07-01 00:04:39 +0000853 // If the base is a template template parameter, this is an
854 // unresolved type.
855 assert(temp && "no template for template specialization type");
856 if (isa<TemplateTemplateParmDecl>(temp)) goto unresolvedType;
John McCall15547bb2011-06-28 16:49:23 +0000857
John McCall3a4a4c52011-07-01 00:04:39 +0000858 mangleSourceName(temp->getIdentifier());
859 break;
860 }
861
862 case TemplateName::OverloadedTemplate:
863 case TemplateName::DependentTemplate:
864 llvm_unreachable("invalid base for a template specialization type");
865
866 case TemplateName::SubstTemplateTemplateParm: {
867 SubstTemplateTemplateParmStorage *subst
868 = name.getAsSubstTemplateTemplateParm();
869 mangleExistingSubstitution(subst->getReplacement());
870 break;
871 }
872
873 case TemplateName::SubstTemplateTemplateParmPack: {
874 // FIXME: not clear how to mangle this!
875 // template <template <class U> class T...> class A {
876 // template <class U...> void foo(decltype(T<U>::foo) x...);
877 // };
878 Out << "_SUBSTPACK_";
879 break;
880 }
881 }
882
John McCall3ab84762011-05-04 01:45:19 +0000883 mangleUnresolvedTemplateArgs(tst->getArgs(), tst->getNumArgs());
John McCall15547bb2011-06-28 16:49:23 +0000884 break;
885 }
886
887 case Type::InjectedClassName:
888 mangleSourceName(cast<InjectedClassNameType>(type)->getDecl()
889 ->getIdentifier());
890 break;
891
892 case Type::DependentName:
893 mangleSourceName(cast<DependentNameType>(type)->getIdentifier());
894 break;
895
896 case Type::DependentTemplateSpecialization: {
897 const DependentTemplateSpecializationType *tst
898 = cast<DependentTemplateSpecializationType>(type);
John McCall3ab84762011-05-04 01:45:19 +0000899 mangleSourceName(tst->getIdentifier());
900 mangleUnresolvedTemplateArgs(tst->getArgs(), tst->getNumArgs());
John McCall15547bb2011-06-28 16:49:23 +0000901 break;
902 }
John McCall3ab84762011-05-04 01:45:19 +0000903 }
904 break;
John McCall6dc0a2b2011-04-24 08:23:24 +0000905 }
906
907 case NestedNameSpecifier::Identifier:
908 // Member expressions can have these without prefixes.
909 if (qualifier->getPrefix()) {
910 mangleUnresolvedPrefix(qualifier->getPrefix(), firstQualifierLookup,
911 /*recursive*/ true);
912 } else if (firstQualifierLookup) {
913
914 // Try to make a proper qualifier out of the lookup result, and
915 // then just recurse on that.
916 NestedNameSpecifier *newQualifier;
917 if (TypeDecl *typeDecl = dyn_cast<TypeDecl>(firstQualifierLookup)) {
918 QualType type = getASTContext().getTypeDeclType(typeDecl);
919
920 // Pretend we had a different nested name specifier.
921 newQualifier = NestedNameSpecifier::Create(getASTContext(),
922 /*prefix*/ 0,
923 /*template*/ false,
924 type.getTypePtr());
925 } else if (NamespaceDecl *nspace =
926 dyn_cast<NamespaceDecl>(firstQualifierLookup)) {
927 newQualifier = NestedNameSpecifier::Create(getASTContext(),
928 /*prefix*/ 0,
929 nspace);
930 } else if (NamespaceAliasDecl *alias =
931 dyn_cast<NamespaceAliasDecl>(firstQualifierLookup)) {
932 newQualifier = NestedNameSpecifier::Create(getASTContext(),
933 /*prefix*/ 0,
934 alias);
935 } else {
936 // No sensible mangling to do here.
937 newQualifier = 0;
938 }
939
940 if (newQualifier)
941 return mangleUnresolvedPrefix(newQualifier, /*lookup*/ 0, recursive);
942
943 } else {
944 Out << "sr";
945 }
946
947 mangleSourceName(qualifier->getAsIdentifier());
948 break;
949 }
950
951 // If this was the innermost part of the NNS, and we fell out to
952 // here, append an 'E'.
953 if (!recursive)
954 Out << 'E';
955}
956
957/// Mangle an unresolved-name, which is generally used for names which
958/// weren't resolved to specific entities.
959void CXXNameMangler::mangleUnresolvedName(NestedNameSpecifier *qualifier,
960 NamedDecl *firstQualifierLookup,
961 DeclarationName name,
962 unsigned knownArity) {
963 if (qualifier) mangleUnresolvedPrefix(qualifier, firstQualifierLookup);
964 mangleUnqualifiedName(0, name, knownArity);
John McCall09de8ec2010-02-04 01:42:13 +0000965}
966
Anders Carlsson248704c2010-06-08 14:49:03 +0000967static const FieldDecl *FindFirstNamedDataMember(const RecordDecl *RD) {
968 assert(RD->isAnonymousStructOrUnion() &&
969 "Expected anonymous struct or union!");
970
971 for (RecordDecl::field_iterator I = RD->field_begin(), E = RD->field_end();
972 I != E; ++I) {
973 const FieldDecl *FD = *I;
974
975 if (FD->getIdentifier())
976 return FD;
977
978 if (const RecordType *RT = FD->getType()->getAs<RecordType>()) {
979 if (const FieldDecl *NamedDataMember =
980 FindFirstNamedDataMember(RT->getDecl()))
981 return NamedDataMember;
982 }
983 }
984
985 // We didn't find a named data member.
986 return 0;
987}
988
John McCall09de8ec2010-02-04 01:42:13 +0000989void CXXNameMangler::mangleUnqualifiedName(const NamedDecl *ND,
990 DeclarationName Name,
991 unsigned KnownArity) {
Douglas Gregor5fec5b02009-02-13 00:10:09 +0000992 // <unqualified-name> ::= <operator-name>
Mike Stump11289f42009-09-09 15:08:12 +0000993 // ::= <ctor-dtor-name>
994 // ::= <source-name>
Douglas Gregor5fec5b02009-02-13 00:10:09 +0000995 switch (Name.getNameKind()) {
Anders Carlsson1e39bd92009-10-07 01:45:02 +0000996 case DeclarationName::Identifier: {
Anders Carlsson1e39bd92009-10-07 01:45:02 +0000997 if (const IdentifierInfo *II = Name.getAsIdentifierInfo()) {
Alexis Huntaecc45c2010-01-24 03:04:27 +0000998 // We must avoid conflicts between internally- and externally-
John McCall290b32b2011-03-22 06:34:45 +0000999 // linked variable and function declaration names in the same TU:
1000 // void test() { extern void foo(); }
1001 // static void foo();
1002 // This naming convention is the same as that followed by GCC,
1003 // though it shouldn't actually matter.
1004 if (ND && ND->getLinkage() == InternalLinkage &&
Alexis Huntaecc45c2010-01-24 03:04:27 +00001005 ND->getDeclContext()->isFileContext())
1006 Out << 'L';
1007
Anders Carlsson1e39bd92009-10-07 01:45:02 +00001008 mangleSourceName(II);
1009 break;
1010 }
Daniel Dunbard614e322009-11-21 09:05:47 +00001011
John McCall09de8ec2010-02-04 01:42:13 +00001012 // Otherwise, an anonymous entity. We must have a declaration.
1013 assert(ND && "mangling empty name without declaration");
1014
1015 if (const NamespaceDecl *NS = dyn_cast<NamespaceDecl>(ND)) {
1016 if (NS->isAnonymousNamespace()) {
1017 // This is how gcc mangles these names.
1018 Out << "12_GLOBAL__N_1";
1019 break;
1020 }
1021 }
1022
Anders Carlsson248704c2010-06-08 14:49:03 +00001023 if (const VarDecl *VD = dyn_cast<VarDecl>(ND)) {
1024 // We must have an anonymous union or struct declaration.
1025 const RecordDecl *RD =
1026 cast<RecordDecl>(VD->getType()->getAs<RecordType>()->getDecl());
1027
1028 // Itanium C++ ABI 5.1.2:
1029 //
1030 // For the purposes of mangling, the name of an anonymous union is
1031 // considered to be the name of the first named data member found by a
1032 // pre-order, depth-first, declaration-order walk of the data members of
1033 // the anonymous union. If there is no such data member (i.e., if all of
1034 // the data members in the union are unnamed), then there is no way for
1035 // a program to refer to the anonymous union, and there is therefore no
1036 // need to mangle its name.
1037 const FieldDecl *FD = FindFirstNamedDataMember(RD);
John McCall49146242010-08-05 22:02:13 +00001038
1039 // It's actually possible for various reasons for us to get here
1040 // with an empty anonymous struct / union. Fortunately, it
1041 // doesn't really matter what name we generate.
1042 if (!FD) break;
Anders Carlsson248704c2010-06-08 14:49:03 +00001043 assert(FD->getIdentifier() && "Data member name isn't an identifier!");
1044
1045 mangleSourceName(FD->getIdentifier());
1046 break;
1047 }
1048
Anders Carlsson1e39bd92009-10-07 01:45:02 +00001049 // We must have an anonymous struct.
1050 const TagDecl *TD = cast<TagDecl>(ND);
Richard Smithdda56e42011-04-15 14:24:37 +00001051 if (const TypedefNameDecl *D = TD->getTypedefNameForAnonDecl()) {
Anders Carlsson1e39bd92009-10-07 01:45:02 +00001052 assert(TD->getDeclContext() == D->getDeclContext() &&
1053 "Typedef should not be in another decl context!");
1054 assert(D->getDeclName().getAsIdentifierInfo() &&
1055 "Typedef was not named!");
1056 mangleSourceName(D->getDeclName().getAsIdentifierInfo());
1057 break;
1058 }
Daniel Dunbard614e322009-11-21 09:05:47 +00001059
Anders Carlsson1e39bd92009-10-07 01:45:02 +00001060 // Get a unique id for the anonymous struct.
1061 uint64_t AnonStructId = Context.getAnonymousStructId(TD);
1062
1063 // Mangle it as a source name in the form
Daniel Dunbard614e322009-11-21 09:05:47 +00001064 // [n] $_<id>
Anders Carlsson1e39bd92009-10-07 01:45:02 +00001065 // where n is the length of the string.
1066 llvm::SmallString<8> Str;
1067 Str += "$_";
1068 Str += llvm::utostr(AnonStructId);
1069
1070 Out << Str.size();
1071 Out << Str.str();
Douglas Gregor5fec5b02009-02-13 00:10:09 +00001072 break;
Anders Carlsson1e39bd92009-10-07 01:45:02 +00001073 }
Douglas Gregor5fec5b02009-02-13 00:10:09 +00001074
1075 case DeclarationName::ObjCZeroArgSelector:
1076 case DeclarationName::ObjCOneArgSelector:
1077 case DeclarationName::ObjCMultiArgSelector:
1078 assert(false && "Can't mangle Objective-C selector names here!");
1079 break;
1080
1081 case DeclarationName::CXXConstructorName:
Anders Carlssoneaa28f72009-04-17 01:58:57 +00001082 if (ND == Structor)
Mike Stump9cc7d302009-09-02 00:25:38 +00001083 // If the named decl is the C++ constructor we're mangling, use the type
1084 // we were given.
Anders Carlssoneaa28f72009-04-17 01:58:57 +00001085 mangleCXXCtorType(static_cast<CXXCtorType>(StructorType));
Anders Carlssone4c40c82009-04-15 05:36:58 +00001086 else
1087 // Otherwise, use the complete constructor name. This is relevant if a
1088 // class with a constructor is declared within a constructor.
1089 mangleCXXCtorType(Ctor_Complete);
Douglas Gregor5fec5b02009-02-13 00:10:09 +00001090 break;
1091
1092 case DeclarationName::CXXDestructorName:
Anders Carlssoneaa28f72009-04-17 01:58:57 +00001093 if (ND == Structor)
Mike Stump9cc7d302009-09-02 00:25:38 +00001094 // If the named decl is the C++ destructor we're mangling, use the type we
1095 // were given.
Anders Carlssoneaa28f72009-04-17 01:58:57 +00001096 mangleCXXDtorType(static_cast<CXXDtorType>(StructorType));
1097 else
1098 // Otherwise, use the complete destructor name. This is relevant if a
1099 // class with a destructor is declared within a destructor.
1100 mangleCXXDtorType(Dtor_Complete);
Douglas Gregor5fec5b02009-02-13 00:10:09 +00001101 break;
1102
1103 case DeclarationName::CXXConversionFunctionName:
Mike Stump11289f42009-09-09 15:08:12 +00001104 // <operator-name> ::= cv <type> # (cast)
Douglas Gregoradb02012009-02-13 01:28:03 +00001105 Out << "cv";
Douglas Gregor84280642011-07-12 04:42:08 +00001106 mangleType(Name.getCXXNameType());
Douglas Gregor5fec5b02009-02-13 00:10:09 +00001107 break;
1108
Anders Carlsson8a35c792009-12-22 06:36:32 +00001109 case DeclarationName::CXXOperatorName: {
John McCall09de8ec2010-02-04 01:42:13 +00001110 unsigned Arity;
1111 if (ND) {
1112 Arity = cast<FunctionDecl>(ND)->getNumParams();
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00001113
John McCall09de8ec2010-02-04 01:42:13 +00001114 // If we have a C++ member function, we need to include the 'this' pointer.
1115 // FIXME: This does not make sense for operators that are static, but their
1116 // names stay the same regardless of the arity (operator new for instance).
1117 if (isa<CXXMethodDecl>(ND))
1118 Arity++;
1119 } else
1120 Arity = KnownArity;
1121
Anders Carlsson8a35c792009-12-22 06:36:32 +00001122 mangleOperatorName(Name.getCXXOverloadedOperator(), Arity);
Douglas Gregor5fec5b02009-02-13 00:10:09 +00001123 break;
Anders Carlsson8a35c792009-12-22 06:36:32 +00001124 }
Douglas Gregor5fec5b02009-02-13 00:10:09 +00001125
Alexis Hunt3d221f22009-11-29 07:34:05 +00001126 case DeclarationName::CXXLiteralOperatorName:
Alexis Hunt9b3a3952009-12-04 21:11:13 +00001127 // FIXME: This mangling is not yet official.
Alexis Huntbf2f0c22009-12-04 21:01:37 +00001128 Out << "li";
Alexis Hunt3d221f22009-11-29 07:34:05 +00001129 mangleSourceName(Name.getCXXLiteralIdentifier());
1130 break;
1131
Douglas Gregor5fec5b02009-02-13 00:10:09 +00001132 case DeclarationName::CXXUsingDirective:
1133 assert(false && "Can't mangle a using directive name!");
Douglas Gregoradb02012009-02-13 01:28:03 +00001134 break;
Douglas Gregor5fec5b02009-02-13 00:10:09 +00001135 }
1136}
1137
1138void CXXNameMangler::mangleSourceName(const IdentifierInfo *II) {
1139 // <source-name> ::= <positive length number> <identifier>
1140 // <number> ::= [n] <non-negative decimal integer>
1141 // <identifier> ::= <unqualified source code identifier>
1142 Out << II->getLength() << II->getName();
1143}
1144
Eli Friedmand4df7752009-12-02 20:32:49 +00001145void CXXNameMangler::mangleNestedName(const NamedDecl *ND,
Fariborz Jahaniana529c252010-03-03 19:41:08 +00001146 const DeclContext *DC,
1147 bool NoFunction) {
Douglas Gregorf3ea1ed2011-01-26 17:36:28 +00001148 // <nested-name>
1149 // ::= N [<CV-qualifiers>] [<ref-qualifier>] <prefix> <unqualified-name> E
1150 // ::= N [<CV-qualifiers>] [<ref-qualifier>] <template-prefix>
1151 // <template-args> E
Anders Carlsson296f8dc2009-09-26 03:55:37 +00001152
Douglas Gregor5fec5b02009-02-13 00:10:09 +00001153 Out << 'N';
Douglas Gregorf3ea1ed2011-01-26 17:36:28 +00001154 if (const CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(ND)) {
John McCall8ccfcb52009-09-24 19:53:00 +00001155 mangleQualifiers(Qualifiers::fromCVRMask(Method->getTypeQualifiers()));
Douglas Gregorf3ea1ed2011-01-26 17:36:28 +00001156 mangleRefQualifier(Method->getRefQualifier());
1157 }
1158
Anders Carlsson559d9742009-09-18 19:00:18 +00001159 // Check if we have a template.
1160 const TemplateArgumentList *TemplateArgs = 0;
Daniel Dunbard614e322009-11-21 09:05:47 +00001161 if (const TemplateDecl *TD = isTemplate(ND, TemplateArgs)) {
Anders Carlsson559d9742009-09-18 19:00:18 +00001162 mangleTemplatePrefix(TD);
Rafael Espindola4d5c3d92010-03-11 14:07:00 +00001163 TemplateParameterList *TemplateParameters = TD->getTemplateParameters();
1164 mangleTemplateArgs(*TemplateParameters, *TemplateArgs);
Fariborz Jahaniana529c252010-03-03 19:41:08 +00001165 }
1166 else {
1167 manglePrefix(DC, NoFunction);
Anders Carlsson2b5e1dd2009-09-18 04:29:09 +00001168 mangleUnqualifiedName(ND);
1169 }
Daniel Dunbard614e322009-11-21 09:05:47 +00001170
Douglas Gregor5fec5b02009-02-13 00:10:09 +00001171 Out << 'E';
1172}
Daniel Dunbard614e322009-11-21 09:05:47 +00001173void CXXNameMangler::mangleNestedName(const TemplateDecl *TD,
Anders Carlsson7a8a74f2009-09-18 02:42:01 +00001174 const TemplateArgument *TemplateArgs,
1175 unsigned NumTemplateArgs) {
Anders Carlssond122de52009-09-27 19:53:49 +00001176 // <nested-name> ::= N [<CV-qualifiers>] <template-prefix> <template-args> E
1177
Anders Carlsson7a8a74f2009-09-18 02:42:01 +00001178 Out << 'N';
Daniel Dunbard614e322009-11-21 09:05:47 +00001179
Anders Carlssond122de52009-09-27 19:53:49 +00001180 mangleTemplatePrefix(TD);
Rafael Espindola4d5c3d92010-03-11 14:07:00 +00001181 TemplateParameterList *TemplateParameters = TD->getTemplateParameters();
1182 mangleTemplateArgs(*TemplateParameters, TemplateArgs, NumTemplateArgs);
Daniel Dunbard614e322009-11-21 09:05:47 +00001183
Anders Carlsson7a8a74f2009-09-18 02:42:01 +00001184 Out << 'E';
1185}
1186
Anders Carlsson4eca1092009-04-02 16:24:45 +00001187void CXXNameMangler::mangleLocalName(const NamedDecl *ND) {
1188 // <local-name> := Z <function encoding> E <entity name> [<discriminator>]
1189 // := Z <function encoding> E s [<discriminator>]
Mike Stump11289f42009-09-09 15:08:12 +00001190 // <discriminator> := _ <non-negative number>
Fariborz Jahaniana529c252010-03-03 19:41:08 +00001191 const DeclContext *DC = ND->getDeclContext();
Fariborz Jahanian624b2992011-06-09 19:25:01 +00001192 if (isa<ObjCMethodDecl>(DC) && isa<FunctionDecl>(ND)) {
1193 // Don't add objc method name mangling to locally declared function
1194 mangleUnqualifiedName(ND);
1195 return;
1196 }
1197
Anders Carlsson4eca1092009-04-02 16:24:45 +00001198 Out << 'Z';
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00001199
Charles Davisc9e78142010-05-26 18:25:27 +00001200 if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(DC)) {
1201 mangleObjCMethodName(MD);
John McCall18e4eda2010-10-18 21:28:44 +00001202 } else if (const CXXRecordDecl *RD = GetLocalClassDecl(ND)) {
1203 mangleFunctionEncoding(cast<FunctionDecl>(RD->getDeclContext()));
Fariborz Jahaniana529c252010-03-03 19:41:08 +00001204 Out << 'E';
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00001205
John McCall18e4eda2010-10-18 21:28:44 +00001206 // Mangle the name relative to the closest enclosing function.
1207 if (ND == RD) // equality ok because RD derived from ND above
1208 mangleUnqualifiedName(ND);
1209 else
1210 mangleNestedName(ND, DC, true /*NoFunction*/);
1211
Fariborz Jahanian9eba9df2010-03-04 01:02:03 +00001212 unsigned disc;
John McCall18e4eda2010-10-18 21:28:44 +00001213 if (Context.getNextDiscriminator(RD, disc)) {
Fariborz Jahanian9eba9df2010-03-04 01:02:03 +00001214 if (disc < 10)
1215 Out << '_' << disc;
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00001216 else
Fariborz Jahanian9eba9df2010-03-04 01:02:03 +00001217 Out << "__" << disc << '_';
1218 }
Fariborz Jahaniana529c252010-03-03 19:41:08 +00001219
1220 return;
1221 }
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00001222 else
Fariborz Jahaniana529c252010-03-03 19:41:08 +00001223 mangleFunctionEncoding(cast<FunctionDecl>(DC));
Anders Carlssonbf5694602009-12-10 03:14:39 +00001224
Anders Carlsson4eca1092009-04-02 16:24:45 +00001225 Out << 'E';
Eli Friedman75c9b972009-12-11 20:21:38 +00001226 mangleUnqualifiedName(ND);
Anders Carlsson4eca1092009-04-02 16:24:45 +00001227}
1228
John McCall6dc0a2b2011-04-24 08:23:24 +00001229void CXXNameMangler::manglePrefix(NestedNameSpecifier *qualifier) {
1230 switch (qualifier->getKind()) {
1231 case NestedNameSpecifier::Global:
1232 // nothing
1233 return;
1234
1235 case NestedNameSpecifier::Namespace:
1236 mangleName(qualifier->getAsNamespace());
1237 return;
1238
1239 case NestedNameSpecifier::NamespaceAlias:
1240 mangleName(qualifier->getAsNamespaceAlias()->getNamespace());
1241 return;
1242
1243 case NestedNameSpecifier::TypeSpec:
1244 case NestedNameSpecifier::TypeSpecWithTemplate:
John McCall3ab84762011-05-04 01:45:19 +00001245 manglePrefix(QualType(qualifier->getAsType(), 0));
John McCall6dc0a2b2011-04-24 08:23:24 +00001246 return;
1247
1248 case NestedNameSpecifier::Identifier:
1249 // Member expressions can have these without prefixes, but that
1250 // should end up in mangleUnresolvedPrefix instead.
1251 assert(qualifier->getPrefix());
1252 manglePrefix(qualifier->getPrefix());
1253
1254 mangleSourceName(qualifier->getAsIdentifier());
1255 return;
1256 }
1257
1258 llvm_unreachable("unexpected nested name specifier");
1259}
1260
Fariborz Jahaniana529c252010-03-03 19:41:08 +00001261void CXXNameMangler::manglePrefix(const DeclContext *DC, bool NoFunction) {
Douglas Gregor5fec5b02009-02-13 00:10:09 +00001262 // <prefix> ::= <prefix> <unqualified-name>
1263 // ::= <template-prefix> <template-args>
1264 // ::= <template-param>
1265 // ::= # empty
1266 // ::= <substitution>
Anders Carlsson4245bf92009-09-17 04:16:28 +00001267
Anders Carlsson1d3b6f62009-09-22 20:33:31 +00001268 while (isa<LinkageSpecDecl>(DC))
1269 DC = DC->getParent();
Daniel Dunbard614e322009-11-21 09:05:47 +00001270
Anders Carlssonc1370cb2009-09-18 18:39:58 +00001271 if (DC->isTranslationUnit())
1272 return;
Daniel Dunbard614e322009-11-21 09:05:47 +00001273
Douglas Gregor7e10f362010-05-25 17:04:15 +00001274 if (const BlockDecl *Block = dyn_cast<BlockDecl>(DC)) {
1275 manglePrefix(DC->getParent(), NoFunction);
1276 llvm::SmallString<64> Name;
Rafael Espindolaac00f5d2011-02-10 23:59:36 +00001277 llvm::raw_svector_ostream NameStream(Name);
1278 Context.mangleBlock(Block, NameStream);
1279 NameStream.flush();
Douglas Gregor7e10f362010-05-25 17:04:15 +00001280 Out << Name.size() << Name;
1281 return;
1282 }
1283
Anders Carlsson4245bf92009-09-17 04:16:28 +00001284 if (mangleSubstitution(cast<NamedDecl>(DC)))
1285 return;
Anders Carlsson2b5e1dd2009-09-18 04:29:09 +00001286
Anders Carlsson82b688e2009-09-18 20:11:09 +00001287 // Check if we have a template.
1288 const TemplateArgumentList *TemplateArgs = 0;
Daniel Dunbard614e322009-11-21 09:05:47 +00001289 if (const TemplateDecl *TD = isTemplate(cast<NamedDecl>(DC), TemplateArgs)) {
Anders Carlsson82b688e2009-09-18 20:11:09 +00001290 mangleTemplatePrefix(TD);
Rafael Espindola4d5c3d92010-03-11 14:07:00 +00001291 TemplateParameterList *TemplateParameters = TD->getTemplateParameters();
1292 mangleTemplateArgs(*TemplateParameters, *TemplateArgs);
Fariborz Jahaniana529c252010-03-03 19:41:08 +00001293 }
Douglas Gregor7e10f362010-05-25 17:04:15 +00001294 else if(NoFunction && (isa<FunctionDecl>(DC) || isa<ObjCMethodDecl>(DC)))
Fariborz Jahaniana529c252010-03-03 19:41:08 +00001295 return;
Douglas Gregor7e10f362010-05-25 17:04:15 +00001296 else if (const ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(DC))
1297 mangleObjCMethodName(Method);
Fariborz Jahaniana529c252010-03-03 19:41:08 +00001298 else {
1299 manglePrefix(DC->getParent(), NoFunction);
Anders Carlsson82b688e2009-09-18 20:11:09 +00001300 mangleUnqualifiedName(cast<NamedDecl>(DC));
1301 }
Daniel Dunbard614e322009-11-21 09:05:47 +00001302
Anders Carlsson4245bf92009-09-17 04:16:28 +00001303 addSubstitution(cast<NamedDecl>(DC));
Douglas Gregor5fec5b02009-02-13 00:10:09 +00001304}
1305
Douglas Gregor17362712010-04-23 03:10:43 +00001306void CXXNameMangler::mangleTemplatePrefix(TemplateName Template) {
1307 // <template-prefix> ::= <prefix> <template unqualified-name>
1308 // ::= <template-param>
1309 // ::= <substitution>
1310 if (TemplateDecl *TD = Template.getAsTemplateDecl())
1311 return mangleTemplatePrefix(TD);
1312
1313 if (QualifiedTemplateName *Qualified = Template.getAsQualifiedTemplateName())
John McCall6dc0a2b2011-04-24 08:23:24 +00001314 manglePrefix(Qualified->getQualifier());
Alexis Hunta8136cc2010-05-05 15:23:54 +00001315
Douglas Gregor17362712010-04-23 03:10:43 +00001316 if (OverloadedTemplateStorage *Overloaded
1317 = Template.getAsOverloadedTemplate()) {
Alexis Hunta8136cc2010-05-05 15:23:54 +00001318 mangleUnqualifiedName(0, (*Overloaded->begin())->getDeclName(),
Douglas Gregor17362712010-04-23 03:10:43 +00001319 UnknownArity);
1320 return;
1321 }
Alexis Hunta8136cc2010-05-05 15:23:54 +00001322
Douglas Gregor17362712010-04-23 03:10:43 +00001323 DependentTemplateName *Dependent = Template.getAsDependentTemplateName();
1324 assert(Dependent && "Unknown template name kind?");
John McCall6dc0a2b2011-04-24 08:23:24 +00001325 manglePrefix(Dependent->getQualifier());
Douglas Gregor7988def2010-04-28 05:58:56 +00001326 mangleUnscopedTemplateName(Template);
Douglas Gregor17362712010-04-23 03:10:43 +00001327}
1328
Anders Carlsson26e67af2009-09-26 19:45:45 +00001329void CXXNameMangler::mangleTemplatePrefix(const TemplateDecl *ND) {
Anders Carlsson2b5e1dd2009-09-18 04:29:09 +00001330 // <template-prefix> ::= <prefix> <template unqualified-name>
1331 // ::= <template-param>
1332 // ::= <substitution>
Douglas Gregora16b0ca2010-02-05 20:45:00 +00001333 // <template-template-param> ::= <template-param>
1334 // <substitution>
Anders Carlsson2b5e1dd2009-09-18 04:29:09 +00001335
Anders Carlsson3e83c302009-09-26 22:18:22 +00001336 if (mangleSubstitution(ND))
1337 return;
Daniel Dunbard614e322009-11-21 09:05:47 +00001338
Douglas Gregora16b0ca2010-02-05 20:45:00 +00001339 // <template-template-param> ::= <template-param>
1340 if (const TemplateTemplateParmDecl *TTP
1341 = dyn_cast<TemplateTemplateParmDecl>(ND)) {
1342 mangleTemplateParameter(TTP->getIndex());
1343 return;
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00001344 }
Daniel Dunbard614e322009-11-21 09:05:47 +00001345
Anders Carlssoncaf4a642009-09-18 18:47:07 +00001346 manglePrefix(ND->getDeclContext());
Anders Carlsson49232b92009-09-26 20:13:56 +00001347 mangleUnqualifiedName(ND->getTemplatedDecl());
Anders Carlsson3e83c302009-09-26 22:18:22 +00001348 addSubstitution(ND);
Anders Carlsson2b5e1dd2009-09-18 04:29:09 +00001349}
1350
John McCall4f3b5f32010-07-14 06:43:17 +00001351/// Mangles a template name under the production <type>. Required for
1352/// template template arguments.
1353/// <type> ::= <class-enum-type>
1354/// ::= <template-param>
1355/// ::= <substitution>
1356void CXXNameMangler::mangleType(TemplateName TN) {
1357 if (mangleSubstitution(TN))
1358 return;
1359
1360 TemplateDecl *TD = 0;
1361
1362 switch (TN.getKind()) {
1363 case TemplateName::QualifiedTemplate:
1364 TD = TN.getAsQualifiedTemplateName()->getTemplateDecl();
1365 goto HaveDecl;
1366
1367 case TemplateName::Template:
1368 TD = TN.getAsTemplateDecl();
1369 goto HaveDecl;
1370
1371 HaveDecl:
1372 if (isa<TemplateTemplateParmDecl>(TD))
1373 mangleTemplateParameter(cast<TemplateTemplateParmDecl>(TD)->getIndex());
1374 else
1375 mangleName(TD);
1376 break;
1377
1378 case TemplateName::OverloadedTemplate:
1379 llvm_unreachable("can't mangle an overloaded template name as a <type>");
1380 break;
1381
1382 case TemplateName::DependentTemplate: {
1383 const DependentTemplateName *Dependent = TN.getAsDependentTemplateName();
1384 assert(Dependent->isIdentifier());
1385
1386 // <class-enum-type> ::= <name>
1387 // <name> ::= <nested-name>
John McCall6dc0a2b2011-04-24 08:23:24 +00001388 mangleUnresolvedPrefix(Dependent->getQualifier(), 0);
John McCall4f3b5f32010-07-14 06:43:17 +00001389 mangleSourceName(Dependent->getIdentifier());
1390 break;
1391 }
1392
John McCalldb499692011-06-30 21:59:02 +00001393 case TemplateName::SubstTemplateTemplateParm: {
1394 // Substituted template parameters are mangled as the substituted
1395 // template. This will check for the substitution twice, which is
1396 // fine, but we have to return early so that we don't try to *add*
1397 // the substitution twice.
1398 SubstTemplateTemplateParmStorage *subst
1399 = TN.getAsSubstTemplateTemplateParm();
1400 mangleType(subst->getReplacement());
1401 return;
1402 }
John McCalld9dfe3a2011-06-30 08:33:18 +00001403
Douglas Gregor5590be02011-01-15 06:45:20 +00001404 case TemplateName::SubstTemplateTemplateParmPack: {
John McCall3a4a4c52011-07-01 00:04:39 +00001405 // FIXME: not clear how to mangle this!
1406 // template <template <class> class T...> class A {
1407 // template <template <class> class U...> void foo(B<T,U> x...);
1408 // };
1409 Out << "_SUBSTPACK_";
Douglas Gregor5590be02011-01-15 06:45:20 +00001410 break;
1411 }
John McCall4f3b5f32010-07-14 06:43:17 +00001412 }
1413
1414 addSubstitution(TN);
1415}
1416
Mike Stump11289f42009-09-09 15:08:12 +00001417void
Douglas Gregor5fec5b02009-02-13 00:10:09 +00001418CXXNameMangler::mangleOperatorName(OverloadedOperatorKind OO, unsigned Arity) {
1419 switch (OO) {
Sebastian Redl1a99f442009-04-16 17:51:27 +00001420 // <operator-name> ::= nw # new
Douglas Gregor5fec5b02009-02-13 00:10:09 +00001421 case OO_New: Out << "nw"; break;
1422 // ::= na # new[]
1423 case OO_Array_New: Out << "na"; break;
Sebastian Redl1a99f442009-04-16 17:51:27 +00001424 // ::= dl # delete
Douglas Gregor5fec5b02009-02-13 00:10:09 +00001425 case OO_Delete: Out << "dl"; break;
Sebastian Redl1a99f442009-04-16 17:51:27 +00001426 // ::= da # delete[]
Douglas Gregor5fec5b02009-02-13 00:10:09 +00001427 case OO_Array_Delete: Out << "da"; break;
1428 // ::= ps # + (unary)
John McCall78fbb612010-08-18 19:18:59 +00001429 // ::= pl # + (binary or unknown)
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00001430 case OO_Plus:
Anders Carlsson8a35c792009-12-22 06:36:32 +00001431 Out << (Arity == 1? "ps" : "pl"); break;
Sebastian Redl1a99f442009-04-16 17:51:27 +00001432 // ::= ng # - (unary)
John McCall78fbb612010-08-18 19:18:59 +00001433 // ::= mi # - (binary or unknown)
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00001434 case OO_Minus:
Anders Carlsson8a35c792009-12-22 06:36:32 +00001435 Out << (Arity == 1? "ng" : "mi"); break;
Sebastian Redl1a99f442009-04-16 17:51:27 +00001436 // ::= ad # & (unary)
John McCall78fbb612010-08-18 19:18:59 +00001437 // ::= an # & (binary or unknown)
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00001438 case OO_Amp:
Anders Carlsson8a35c792009-12-22 06:36:32 +00001439 Out << (Arity == 1? "ad" : "an"); break;
Sebastian Redl1a99f442009-04-16 17:51:27 +00001440 // ::= de # * (unary)
John McCall78fbb612010-08-18 19:18:59 +00001441 // ::= ml # * (binary or unknown)
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00001442 case OO_Star:
John McCall78fbb612010-08-18 19:18:59 +00001443 // Use binary when unknown.
Anders Carlsson8a35c792009-12-22 06:36:32 +00001444 Out << (Arity == 1? "de" : "ml"); break;
Sebastian Redl1a99f442009-04-16 17:51:27 +00001445 // ::= co # ~
Douglas Gregor5fec5b02009-02-13 00:10:09 +00001446 case OO_Tilde: Out << "co"; break;
Sebastian Redl1a99f442009-04-16 17:51:27 +00001447 // ::= dv # /
Douglas Gregor5fec5b02009-02-13 00:10:09 +00001448 case OO_Slash: Out << "dv"; break;
Sebastian Redl1a99f442009-04-16 17:51:27 +00001449 // ::= rm # %
Douglas Gregor5fec5b02009-02-13 00:10:09 +00001450 case OO_Percent: Out << "rm"; break;
Sebastian Redl1a99f442009-04-16 17:51:27 +00001451 // ::= or # |
1452 case OO_Pipe: Out << "or"; break;
1453 // ::= eo # ^
Douglas Gregor5fec5b02009-02-13 00:10:09 +00001454 case OO_Caret: Out << "eo"; break;
Sebastian Redl1a99f442009-04-16 17:51:27 +00001455 // ::= aS # =
Douglas Gregor5fec5b02009-02-13 00:10:09 +00001456 case OO_Equal: Out << "aS"; break;
Sebastian Redl1a99f442009-04-16 17:51:27 +00001457 // ::= pL # +=
Douglas Gregor5fec5b02009-02-13 00:10:09 +00001458 case OO_PlusEqual: Out << "pL"; break;
Sebastian Redl1a99f442009-04-16 17:51:27 +00001459 // ::= mI # -=
Douglas Gregor5fec5b02009-02-13 00:10:09 +00001460 case OO_MinusEqual: Out << "mI"; break;
Sebastian Redl1a99f442009-04-16 17:51:27 +00001461 // ::= mL # *=
Douglas Gregor5fec5b02009-02-13 00:10:09 +00001462 case OO_StarEqual: Out << "mL"; break;
Sebastian Redl1a99f442009-04-16 17:51:27 +00001463 // ::= dV # /=
Douglas Gregor5fec5b02009-02-13 00:10:09 +00001464 case OO_SlashEqual: Out << "dV"; break;
Sebastian Redl1a99f442009-04-16 17:51:27 +00001465 // ::= rM # %=
1466 case OO_PercentEqual: Out << "rM"; break;
1467 // ::= aN # &=
1468 case OO_AmpEqual: Out << "aN"; break;
1469 // ::= oR # |=
1470 case OO_PipeEqual: Out << "oR"; break;
1471 // ::= eO # ^=
1472 case OO_CaretEqual: Out << "eO"; break;
1473 // ::= ls # <<
Douglas Gregor5fec5b02009-02-13 00:10:09 +00001474 case OO_LessLess: Out << "ls"; break;
Sebastian Redl1a99f442009-04-16 17:51:27 +00001475 // ::= rs # >>
1476 case OO_GreaterGreater: Out << "rs"; break;
1477 // ::= lS # <<=
1478 case OO_LessLessEqual: Out << "lS"; break;
1479 // ::= rS # >>=
1480 case OO_GreaterGreaterEqual: Out << "rS"; break;
Douglas Gregor5fec5b02009-02-13 00:10:09 +00001481 // ::= eq # ==
1482 case OO_EqualEqual: Out << "eq"; break;
Sebastian Redl1a99f442009-04-16 17:51:27 +00001483 // ::= ne # !=
1484 case OO_ExclaimEqual: Out << "ne"; break;
1485 // ::= lt # <
Douglas Gregor5fec5b02009-02-13 00:10:09 +00001486 case OO_Less: Out << "lt"; break;
Sebastian Redl1a99f442009-04-16 17:51:27 +00001487 // ::= gt # >
Douglas Gregor5fec5b02009-02-13 00:10:09 +00001488 case OO_Greater: Out << "gt"; break;
Sebastian Redl1a99f442009-04-16 17:51:27 +00001489 // ::= le # <=
Douglas Gregor5fec5b02009-02-13 00:10:09 +00001490 case OO_LessEqual: Out << "le"; break;
Sebastian Redl1a99f442009-04-16 17:51:27 +00001491 // ::= ge # >=
Douglas Gregor5fec5b02009-02-13 00:10:09 +00001492 case OO_GreaterEqual: Out << "ge"; break;
Sebastian Redl1a99f442009-04-16 17:51:27 +00001493 // ::= nt # !
Douglas Gregor5fec5b02009-02-13 00:10:09 +00001494 case OO_Exclaim: Out << "nt"; break;
Sebastian Redl1a99f442009-04-16 17:51:27 +00001495 // ::= aa # &&
Douglas Gregor5fec5b02009-02-13 00:10:09 +00001496 case OO_AmpAmp: Out << "aa"; break;
Sebastian Redl1a99f442009-04-16 17:51:27 +00001497 // ::= oo # ||
1498 case OO_PipePipe: Out << "oo"; break;
1499 // ::= pp # ++
1500 case OO_PlusPlus: Out << "pp"; break;
1501 // ::= mm # --
Douglas Gregor5fec5b02009-02-13 00:10:09 +00001502 case OO_MinusMinus: Out << "mm"; break;
Sebastian Redl1a99f442009-04-16 17:51:27 +00001503 // ::= cm # ,
1504 case OO_Comma: Out << "cm"; break;
1505 // ::= pm # ->*
Douglas Gregor5fec5b02009-02-13 00:10:09 +00001506 case OO_ArrowStar: Out << "pm"; break;
Sebastian Redl1a99f442009-04-16 17:51:27 +00001507 // ::= pt # ->
Douglas Gregor5fec5b02009-02-13 00:10:09 +00001508 case OO_Arrow: Out << "pt"; break;
Sebastian Redl1a99f442009-04-16 17:51:27 +00001509 // ::= cl # ()
Douglas Gregor5fec5b02009-02-13 00:10:09 +00001510 case OO_Call: Out << "cl"; break;
Sebastian Redl1a99f442009-04-16 17:51:27 +00001511 // ::= ix # []
Douglas Gregor5fec5b02009-02-13 00:10:09 +00001512 case OO_Subscript: Out << "ix"; break;
Anders Carlssone66e2942009-12-14 01:45:37 +00001513
1514 // ::= qu # ?
1515 // The conditional operator can't be overloaded, but we still handle it when
1516 // mangling expressions.
1517 case OO_Conditional: Out << "qu"; break;
Douglas Gregor5fec5b02009-02-13 00:10:09 +00001518
Sebastian Redl1a99f442009-04-16 17:51:27 +00001519 case OO_None:
Douglas Gregor5fec5b02009-02-13 00:10:09 +00001520 case NUM_OVERLOADED_OPERATORS:
Mike Stump11289f42009-09-09 15:08:12 +00001521 assert(false && "Not an overloaded operator");
Douglas Gregor5fec5b02009-02-13 00:10:09 +00001522 break;
1523 }
1524}
1525
John McCall8ccfcb52009-09-24 19:53:00 +00001526void CXXNameMangler::mangleQualifiers(Qualifiers Quals) {
Mike Stump11289f42009-09-09 15:08:12 +00001527 // <CV-qualifiers> ::= [r] [V] [K] # restrict (C99), volatile, const
John McCall8ccfcb52009-09-24 19:53:00 +00001528 if (Quals.hasRestrict())
Douglas Gregor5fec5b02009-02-13 00:10:09 +00001529 Out << 'r';
John McCall8ccfcb52009-09-24 19:53:00 +00001530 if (Quals.hasVolatile())
Douglas Gregor5fec5b02009-02-13 00:10:09 +00001531 Out << 'V';
John McCall8ccfcb52009-09-24 19:53:00 +00001532 if (Quals.hasConst())
Douglas Gregor5fec5b02009-02-13 00:10:09 +00001533 Out << 'K';
John McCall8ccfcb52009-09-24 19:53:00 +00001534
Douglas Gregor1726d152010-06-14 23:15:08 +00001535 if (Quals.hasAddressSpace()) {
1536 // Extension:
1537 //
1538 // <type> ::= U <address-space-number>
1539 //
1540 // where <address-space-number> is a source name consisting of 'AS'
1541 // followed by the address space <number>.
1542 llvm::SmallString<64> ASString;
1543 ASString = "AS" + llvm::utostr_32(Quals.getAddressSpace());
1544 Out << 'U' << ASString.size() << ASString;
1545 }
1546
John McCall31168b02011-06-15 23:02:42 +00001547 llvm::StringRef LifetimeName;
1548 switch (Quals.getObjCLifetime()) {
1549 // Objective-C ARC Extension:
1550 //
1551 // <type> ::= U "__strong"
1552 // <type> ::= U "__weak"
1553 // <type> ::= U "__autoreleasing"
John McCall31168b02011-06-15 23:02:42 +00001554 case Qualifiers::OCL_None:
1555 break;
1556
1557 case Qualifiers::OCL_Weak:
1558 LifetimeName = "__weak";
1559 break;
1560
1561 case Qualifiers::OCL_Strong:
1562 LifetimeName = "__strong";
1563 break;
1564
1565 case Qualifiers::OCL_Autoreleasing:
1566 LifetimeName = "__autoreleasing";
1567 break;
1568
1569 case Qualifiers::OCL_ExplicitNone:
Douglas Gregorb5176a52011-06-17 22:26:49 +00001570 // The __unsafe_unretained qualifier is *not* mangled, so that
1571 // __unsafe_unretained types in ARC produce the same manglings as the
1572 // equivalent (but, naturally, unqualified) types in non-ARC, providing
1573 // better ABI compatibility.
1574 //
1575 // It's safe to do this because unqualified 'id' won't show up
1576 // in any type signatures that need to be mangled.
John McCall31168b02011-06-15 23:02:42 +00001577 break;
1578 }
1579 if (!LifetimeName.empty())
1580 Out << 'U' << LifetimeName.size() << LifetimeName;
Douglas Gregor5fec5b02009-02-13 00:10:09 +00001581}
1582
Douglas Gregorf3ea1ed2011-01-26 17:36:28 +00001583void CXXNameMangler::mangleRefQualifier(RefQualifierKind RefQualifier) {
1584 // <ref-qualifier> ::= R # lvalue reference
1585 // ::= O # rvalue-reference
1586 // Proposal to Itanium C++ ABI list on 1/26/11
1587 switch (RefQualifier) {
1588 case RQ_None:
1589 break;
1590
1591 case RQ_LValue:
1592 Out << 'R';
1593 break;
1594
1595 case RQ_RValue:
1596 Out << 'O';
1597 break;
1598 }
1599}
1600
Anders Carlssonbf5694602009-12-10 03:14:39 +00001601void CXXNameMangler::mangleObjCMethodName(const ObjCMethodDecl *MD) {
Rafael Espindola3968cd02011-02-11 02:52:17 +00001602 Context.mangleObjCMethodName(MD, Out);
Anders Carlssonbf5694602009-12-10 03:14:39 +00001603}
1604
John McCall5143d642011-01-26 20:05:40 +00001605void CXXNameMangler::mangleType(QualType nonCanon) {
Anders Carlsson02751152009-03-10 17:07:44 +00001606 // Only operate on the canonical type!
John McCall5143d642011-01-26 20:05:40 +00001607 QualType canon = nonCanon.getCanonicalType();
Anders Carlsson02751152009-03-10 17:07:44 +00001608
John McCall5143d642011-01-26 20:05:40 +00001609 SplitQualType split = canon.split();
1610 Qualifiers quals = split.second;
1611 const Type *ty = split.first;
1612
1613 bool isSubstitutable = quals || !isa<BuiltinType>(ty);
1614 if (isSubstitutable && mangleSubstitution(canon))
Anders Carlssonfeb60502009-09-17 00:43:46 +00001615 return;
Douglas Gregor5fec5b02009-02-13 00:10:09 +00001616
John McCall5143d642011-01-26 20:05:40 +00001617 // If we're mangling a qualified array type, push the qualifiers to
1618 // the element type.
1619 if (quals && isa<ArrayType>(ty)) {
1620 ty = Context.getASTContext().getAsArrayType(canon);
1621 quals = Qualifiers();
1622
1623 // Note that we don't update canon: we want to add the
1624 // substitution at the canonical type.
1625 }
1626
1627 if (quals) {
1628 mangleQualifiers(quals);
John McCall8ccfcb52009-09-24 19:53:00 +00001629 // Recurse: even if the qualified type isn't yet substitutable,
1630 // the unqualified type might be.
John McCall5143d642011-01-26 20:05:40 +00001631 mangleType(QualType(ty, 0));
Anders Carlssonfeb60502009-09-17 00:43:46 +00001632 } else {
John McCall5143d642011-01-26 20:05:40 +00001633 switch (ty->getTypeClass()) {
John McCallcc5e23c2009-09-05 07:56:18 +00001634#define ABSTRACT_TYPE(CLASS, PARENT)
1635#define NON_CANONICAL_TYPE(CLASS, PARENT) \
Anders Carlssonfeb60502009-09-17 00:43:46 +00001636 case Type::CLASS: \
Jeffrey Yasskin1615d452009-12-12 05:05:38 +00001637 llvm_unreachable("can't mangle non-canonical type " #CLASS "Type"); \
Anders Carlssonfeb60502009-09-17 00:43:46 +00001638 return;
John McCallcc5e23c2009-09-05 07:56:18 +00001639#define TYPE(CLASS, PARENT) \
Anders Carlssonfeb60502009-09-17 00:43:46 +00001640 case Type::CLASS: \
John McCall5143d642011-01-26 20:05:40 +00001641 mangleType(static_cast<const CLASS##Type*>(ty)); \
Anders Carlssonfeb60502009-09-17 00:43:46 +00001642 break;
John McCallcc5e23c2009-09-05 07:56:18 +00001643#include "clang/AST/TypeNodes.def"
Anders Carlssonfeb60502009-09-17 00:43:46 +00001644 }
Douglas Gregor5fec5b02009-02-13 00:10:09 +00001645 }
Anders Carlssonfeb60502009-09-17 00:43:46 +00001646
1647 // Add the substitution.
John McCall5143d642011-01-26 20:05:40 +00001648 if (isSubstitutable)
1649 addSubstitution(canon);
Douglas Gregor5fec5b02009-02-13 00:10:09 +00001650}
1651
Douglas Gregor0094eb22010-05-26 05:11:13 +00001652void CXXNameMangler::mangleNameOrStandardSubstitution(const NamedDecl *ND) {
1653 if (!mangleStandardSubstitution(ND))
1654 mangleName(ND);
1655}
1656
Douglas Gregor5fec5b02009-02-13 00:10:09 +00001657void CXXNameMangler::mangleType(const BuiltinType *T) {
John McCallcc5e23c2009-09-05 07:56:18 +00001658 // <type> ::= <builtin-type>
Douglas Gregor5fec5b02009-02-13 00:10:09 +00001659 // <builtin-type> ::= v # void
1660 // ::= w # wchar_t
1661 // ::= b # bool
1662 // ::= c # char
1663 // ::= a # signed char
1664 // ::= h # unsigned char
1665 // ::= s # short
1666 // ::= t # unsigned short
1667 // ::= i # int
1668 // ::= j # unsigned int
1669 // ::= l # long
1670 // ::= m # unsigned long
1671 // ::= x # long long, __int64
1672 // ::= y # unsigned long long, __int64
1673 // ::= n # __int128
1674 // UNSUPPORTED: ::= o # unsigned __int128
1675 // ::= f # float
1676 // ::= d # double
1677 // ::= e # long double, __float80
1678 // UNSUPPORTED: ::= g # __float128
Douglas Gregor5fec5b02009-02-13 00:10:09 +00001679 // UNSUPPORTED: ::= Dd # IEEE 754r decimal floating point (64 bits)
1680 // UNSUPPORTED: ::= De # IEEE 754r decimal floating point (128 bits)
1681 // UNSUPPORTED: ::= Df # IEEE 754r decimal floating point (32 bits)
1682 // UNSUPPORTED: ::= Dh # IEEE 754r half-precision floating point (16 bits)
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00001683 // ::= Di # char32_t
1684 // ::= Ds # char16_t
Anders Carlsson2683fd62010-11-04 04:31:32 +00001685 // ::= Dn # std::nullptr_t (i.e., decltype(nullptr))
Douglas Gregor5fec5b02009-02-13 00:10:09 +00001686 // ::= u <source-name> # vendor extended type
1687 switch (T->getKind()) {
1688 case BuiltinType::Void: Out << 'v'; break;
1689 case BuiltinType::Bool: Out << 'b'; break;
1690 case BuiltinType::Char_U: case BuiltinType::Char_S: Out << 'c'; break;
1691 case BuiltinType::UChar: Out << 'h'; break;
1692 case BuiltinType::UShort: Out << 't'; break;
1693 case BuiltinType::UInt: Out << 'j'; break;
1694 case BuiltinType::ULong: Out << 'm'; break;
1695 case BuiltinType::ULongLong: Out << 'y'; break;
Chris Lattnerf122cef2009-04-30 02:43:43 +00001696 case BuiltinType::UInt128: Out << 'o'; break;
Douglas Gregor5fec5b02009-02-13 00:10:09 +00001697 case BuiltinType::SChar: Out << 'a'; break;
Chris Lattnerad3467e2010-12-25 23:25:43 +00001698 case BuiltinType::WChar_S:
1699 case BuiltinType::WChar_U: Out << 'w'; break;
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00001700 case BuiltinType::Char16: Out << "Ds"; break;
1701 case BuiltinType::Char32: Out << "Di"; break;
Douglas Gregor5fec5b02009-02-13 00:10:09 +00001702 case BuiltinType::Short: Out << 's'; break;
1703 case BuiltinType::Int: Out << 'i'; break;
1704 case BuiltinType::Long: Out << 'l'; break;
1705 case BuiltinType::LongLong: Out << 'x'; break;
Chris Lattnerf122cef2009-04-30 02:43:43 +00001706 case BuiltinType::Int128: Out << 'n'; break;
Douglas Gregor5fec5b02009-02-13 00:10:09 +00001707 case BuiltinType::Float: Out << 'f'; break;
1708 case BuiltinType::Double: Out << 'd'; break;
1709 case BuiltinType::LongDouble: Out << 'e'; break;
Anders Carlsson2683fd62010-11-04 04:31:32 +00001710 case BuiltinType::NullPtr: Out << "Dn"; break;
Douglas Gregor5fec5b02009-02-13 00:10:09 +00001711
1712 case BuiltinType::Overload:
1713 case BuiltinType::Dependent:
John McCall0009fcc2011-04-26 20:42:42 +00001714 case BuiltinType::BoundMember:
John McCall31996342011-04-07 08:22:57 +00001715 case BuiltinType::UnknownAny:
John McCall8fb0d9d2011-05-01 22:35:37 +00001716 llvm_unreachable("mangling a placeholder type");
Douglas Gregor5fec5b02009-02-13 00:10:09 +00001717 break;
Steve Naroff3de6b702009-07-22 17:14:51 +00001718 case BuiltinType::ObjCId: Out << "11objc_object"; break;
1719 case BuiltinType::ObjCClass: Out << "10objc_class"; break;
Fariborz Jahanian252ba5f2009-11-21 19:53:08 +00001720 case BuiltinType::ObjCSel: Out << "13objc_selector"; break;
Douglas Gregor5fec5b02009-02-13 00:10:09 +00001721 }
1722}
1723
John McCallcc5e23c2009-09-05 07:56:18 +00001724// <type> ::= <function-type>
1725// <function-type> ::= F [Y] <bare-function-type> E
1726void CXXNameMangler::mangleType(const FunctionProtoType *T) {
Douglas Gregor5fec5b02009-02-13 00:10:09 +00001727 Out << 'F';
Mike Stump18bb9282009-05-16 07:57:57 +00001728 // FIXME: We don't have enough information in the AST to produce the 'Y'
1729 // encoding for extern "C" function types.
Douglas Gregor5fec5b02009-02-13 00:10:09 +00001730 mangleBareFunctionType(T, /*MangleReturnType=*/true);
1731 Out << 'E';
1732}
John McCallcc5e23c2009-09-05 07:56:18 +00001733void CXXNameMangler::mangleType(const FunctionNoProtoType *T) {
Jeffrey Yasskin1615d452009-12-12 05:05:38 +00001734 llvm_unreachable("Can't mangle K&R function prototypes");
John McCallcc5e23c2009-09-05 07:56:18 +00001735}
Douglas Gregor5fec5b02009-02-13 00:10:09 +00001736void CXXNameMangler::mangleBareFunctionType(const FunctionType *T,
1737 bool MangleReturnType) {
John McCallcc5e23c2009-09-05 07:56:18 +00001738 // We should never be mangling something without a prototype.
1739 const FunctionProtoType *Proto = cast<FunctionProtoType>(T);
1740
John McCall8fb0d9d2011-05-01 22:35:37 +00001741 // Record that we're in a function type. See mangleFunctionParam
1742 // for details on what we're trying to achieve here.
1743 FunctionTypeDepthState saved = FunctionTypeDepth.push();
1744
Douglas Gregor5fec5b02009-02-13 00:10:09 +00001745 // <bare-function-type> ::= <signature type>+
John McCall8fb0d9d2011-05-01 22:35:37 +00001746 if (MangleReturnType) {
1747 FunctionTypeDepth.enterResultType();
John McCallcc5e23c2009-09-05 07:56:18 +00001748 mangleType(Proto->getResultType());
John McCall8fb0d9d2011-05-01 22:35:37 +00001749 FunctionTypeDepth.leaveResultType();
1750 }
Douglas Gregor5fec5b02009-02-13 00:10:09 +00001751
Anders Carlsson728fe442010-06-02 04:40:13 +00001752 if (Proto->getNumArgs() == 0 && !Proto->isVariadic()) {
Eli Friedman04831922010-08-22 01:00:03 +00001753 // <builtin-type> ::= v # void
Anders Carlsson7a6f8b92009-04-01 00:15:23 +00001754 Out << 'v';
John McCall8fb0d9d2011-05-01 22:35:37 +00001755
1756 FunctionTypeDepth.pop(saved);
Anders Carlsson7a6f8b92009-04-01 00:15:23 +00001757 return;
1758 }
Mike Stump11289f42009-09-09 15:08:12 +00001759
Douglas Gregordeaad8c2009-02-26 23:50:07 +00001760 for (FunctionProtoType::arg_type_iterator Arg = Proto->arg_type_begin(),
Mike Stump11289f42009-09-09 15:08:12 +00001761 ArgEnd = Proto->arg_type_end();
Douglas Gregor5fec5b02009-02-13 00:10:09 +00001762 Arg != ArgEnd; ++Arg)
Douglas Gregor84280642011-07-12 04:42:08 +00001763 mangleType(Context.getASTContext().getSignatureParameterType(*Arg));
Douglas Gregoradb02012009-02-13 01:28:03 +00001764
John McCall8fb0d9d2011-05-01 22:35:37 +00001765 FunctionTypeDepth.pop(saved);
1766
Douglas Gregoradb02012009-02-13 01:28:03 +00001767 // <builtin-type> ::= z # ellipsis
1768 if (Proto->isVariadic())
1769 Out << 'z';
Douglas Gregor5fec5b02009-02-13 00:10:09 +00001770}
1771
John McCallcc5e23c2009-09-05 07:56:18 +00001772// <type> ::= <class-enum-type>
Mike Stump11289f42009-09-09 15:08:12 +00001773// <class-enum-type> ::= <name>
John McCallb96ec562009-12-04 22:46:56 +00001774void CXXNameMangler::mangleType(const UnresolvedUsingType *T) {
1775 mangleName(T->getDecl());
1776}
1777
1778// <type> ::= <class-enum-type>
1779// <class-enum-type> ::= <name>
John McCallcc5e23c2009-09-05 07:56:18 +00001780void CXXNameMangler::mangleType(const EnumType *T) {
1781 mangleType(static_cast<const TagType*>(T));
1782}
1783void CXXNameMangler::mangleType(const RecordType *T) {
1784 mangleType(static_cast<const TagType*>(T));
1785}
Douglas Gregor5fec5b02009-02-13 00:10:09 +00001786void CXXNameMangler::mangleType(const TagType *T) {
Eli Friedman30e94d042009-12-11 18:00:57 +00001787 mangleName(T->getDecl());
Douglas Gregor5fec5b02009-02-13 00:10:09 +00001788}
1789
John McCallcc5e23c2009-09-05 07:56:18 +00001790// <type> ::= <array-type>
1791// <array-type> ::= A <positive dimension number> _ <element type>
1792// ::= A [<dimension expression>] _ <element type>
1793void CXXNameMangler::mangleType(const ConstantArrayType *T) {
1794 Out << 'A' << T->getSize() << '_';
1795 mangleType(T->getElementType());
1796}
1797void CXXNameMangler::mangleType(const VariableArrayType *T) {
Douglas Gregor5fec5b02009-02-13 00:10:09 +00001798 Out << 'A';
Fariborz Jahaniandc9bc5a2010-11-02 16:54:00 +00001799 // decayed vla types (size 0) will just be skipped.
1800 if (T->getSizeExpr())
1801 mangleExpression(T->getSizeExpr());
Douglas Gregor5fec5b02009-02-13 00:10:09 +00001802 Out << '_';
1803 mangleType(T->getElementType());
1804}
John McCallcc5e23c2009-09-05 07:56:18 +00001805void CXXNameMangler::mangleType(const DependentSizedArrayType *T) {
1806 Out << 'A';
1807 mangleExpression(T->getSizeExpr());
1808 Out << '_';
1809 mangleType(T->getElementType());
1810}
1811void CXXNameMangler::mangleType(const IncompleteArrayType *T) {
Nick Lewycky1b36b552010-09-05 03:40:33 +00001812 Out << "A_";
John McCallcc5e23c2009-09-05 07:56:18 +00001813 mangleType(T->getElementType());
1814}
Douglas Gregor5fec5b02009-02-13 00:10:09 +00001815
John McCallcc5e23c2009-09-05 07:56:18 +00001816// <type> ::= <pointer-to-member-type>
1817// <pointer-to-member-type> ::= M <class type> <member type>
Douglas Gregor5fec5b02009-02-13 00:10:09 +00001818void CXXNameMangler::mangleType(const MemberPointerType *T) {
Douglas Gregor5fec5b02009-02-13 00:10:09 +00001819 Out << 'M';
1820 mangleType(QualType(T->getClass(), 0));
Anders Carlsson23ca0b42009-05-17 17:41:20 +00001821 QualType PointeeType = T->getPointeeType();
1822 if (const FunctionProtoType *FPT = dyn_cast<FunctionProtoType>(PointeeType)) {
John McCall8ccfcb52009-09-24 19:53:00 +00001823 mangleQualifiers(Qualifiers::fromCVRMask(FPT->getTypeQuals()));
Douglas Gregorf3ea1ed2011-01-26 17:36:28 +00001824 mangleRefQualifier(FPT->getRefQualifier());
Anders Carlsson23ca0b42009-05-17 17:41:20 +00001825 mangleType(FPT);
Anders Carlssond5639232010-06-02 04:29:50 +00001826
1827 // Itanium C++ ABI 5.1.8:
1828 //
1829 // The type of a non-static member function is considered to be different,
1830 // for the purposes of substitution, from the type of a namespace-scope or
1831 // static member function whose type appears similar. The types of two
1832 // non-static member functions are considered to be different, for the
1833 // purposes of substitution, if the functions are members of different
1834 // classes. In other words, for the purposes of substitution, the class of
1835 // which the function is a member is considered part of the type of
1836 // function.
1837
1838 // We increment the SeqID here to emulate adding an entry to the
1839 // substitution table. We can't actually add it because we don't want this
1840 // particular function type to be substituted.
1841 ++SeqID;
Mike Stump11289f42009-09-09 15:08:12 +00001842 } else
Anders Carlsson23ca0b42009-05-17 17:41:20 +00001843 mangleType(PointeeType);
Douglas Gregor5fec5b02009-02-13 00:10:09 +00001844}
1845
John McCallcc5e23c2009-09-05 07:56:18 +00001846// <type> ::= <template-param>
Douglas Gregor5fec5b02009-02-13 00:10:09 +00001847void CXXNameMangler::mangleType(const TemplateTypeParmType *T) {
Anders Carlssone00745b2009-09-27 00:38:53 +00001848 mangleTemplateParameter(T->getIndex());
Douglas Gregor5fec5b02009-02-13 00:10:09 +00001849}
1850
Douglas Gregorada4b792011-01-14 02:55:32 +00001851// <type> ::= <template-param>
1852void CXXNameMangler::mangleType(const SubstTemplateTypeParmPackType *T) {
John McCall3a4a4c52011-07-01 00:04:39 +00001853 // FIXME: not clear how to mangle this!
1854 // template <class T...> class A {
1855 // template <class U...> void foo(T(*)(U) x...);
1856 // };
1857 Out << "_SUBSTPACK_";
Douglas Gregorada4b792011-01-14 02:55:32 +00001858}
1859
John McCallcc5e23c2009-09-05 07:56:18 +00001860// <type> ::= P <type> # pointer-to
1861void CXXNameMangler::mangleType(const PointerType *T) {
1862 Out << 'P';
1863 mangleType(T->getPointeeType());
1864}
1865void CXXNameMangler::mangleType(const ObjCObjectPointerType *T) {
1866 Out << 'P';
1867 mangleType(T->getPointeeType());
1868}
1869
1870// <type> ::= R <type> # reference-to
1871void CXXNameMangler::mangleType(const LValueReferenceType *T) {
1872 Out << 'R';
1873 mangleType(T->getPointeeType());
1874}
1875
1876// <type> ::= O <type> # rvalue reference-to (C++0x)
1877void CXXNameMangler::mangleType(const RValueReferenceType *T) {
1878 Out << 'O';
1879 mangleType(T->getPointeeType());
1880}
1881
1882// <type> ::= C <type> # complex pair (C 2000)
1883void CXXNameMangler::mangleType(const ComplexType *T) {
1884 Out << 'C';
1885 mangleType(T->getElementType());
1886}
1887
Bob Wilsonc1555212010-11-12 17:24:43 +00001888// ARM's ABI for Neon vector types specifies that they should be mangled as
Bob Wilsone4652b12010-11-16 00:32:18 +00001889// if they are structs (to match ARM's initial implementation). The
1890// vector type must be one of the special types predefined by ARM.
1891void CXXNameMangler::mangleNeonVectorType(const VectorType *T) {
Bob Wilsonc1555212010-11-12 17:24:43 +00001892 QualType EltType = T->getElementType();
Bob Wilsone4652b12010-11-16 00:32:18 +00001893 assert(EltType->isBuiltinType() && "Neon vector element not a BuiltinType");
Bob Wilsonc1555212010-11-12 17:24:43 +00001894 const char *EltName = 0;
Bob Wilson8470b332010-11-12 17:24:46 +00001895 if (T->getVectorKind() == VectorType::NeonPolyVector) {
1896 switch (cast<BuiltinType>(EltType)->getKind()) {
Bob Wilsonf58e8a42010-11-12 17:24:49 +00001897 case BuiltinType::SChar: EltName = "poly8_t"; break;
1898 case BuiltinType::Short: EltName = "poly16_t"; break;
Bob Wilsone4652b12010-11-16 00:32:18 +00001899 default: llvm_unreachable("unexpected Neon polynomial vector element type");
Bob Wilson8470b332010-11-12 17:24:46 +00001900 }
1901 } else {
1902 switch (cast<BuiltinType>(EltType)->getKind()) {
Bob Wilsonf58e8a42010-11-12 17:24:49 +00001903 case BuiltinType::SChar: EltName = "int8_t"; break;
1904 case BuiltinType::UChar: EltName = "uint8_t"; break;
1905 case BuiltinType::Short: EltName = "int16_t"; break;
1906 case BuiltinType::UShort: EltName = "uint16_t"; break;
1907 case BuiltinType::Int: EltName = "int32_t"; break;
1908 case BuiltinType::UInt: EltName = "uint32_t"; break;
1909 case BuiltinType::LongLong: EltName = "int64_t"; break;
1910 case BuiltinType::ULongLong: EltName = "uint64_t"; break;
1911 case BuiltinType::Float: EltName = "float32_t"; break;
Bob Wilsone4652b12010-11-16 00:32:18 +00001912 default: llvm_unreachable("unexpected Neon vector element type");
Bob Wilson8470b332010-11-12 17:24:46 +00001913 }
Bob Wilsonc1555212010-11-12 17:24:43 +00001914 }
1915 const char *BaseName = 0;
Bob Wilsonf58e8a42010-11-12 17:24:49 +00001916 unsigned BitSize = (T->getNumElements() *
Bob Wilson59f9dec2010-11-16 00:32:12 +00001917 getASTContext().getTypeSize(EltType));
Bob Wilsonc1555212010-11-12 17:24:43 +00001918 if (BitSize == 64)
1919 BaseName = "__simd64_";
Bob Wilsone4652b12010-11-16 00:32:18 +00001920 else {
1921 assert(BitSize == 128 && "Neon vector type not 64 or 128 bits");
Bob Wilsonc1555212010-11-12 17:24:43 +00001922 BaseName = "__simd128_";
Bob Wilsone4652b12010-11-16 00:32:18 +00001923 }
Bob Wilsonc1555212010-11-12 17:24:43 +00001924 Out << strlen(BaseName) + strlen(EltName);
1925 Out << BaseName << EltName;
Bob Wilsonc1555212010-11-12 17:24:43 +00001926}
1927
John McCallcc5e23c2009-09-05 07:56:18 +00001928// GNU extension: vector types
Chris Lattner37141f42010-06-23 06:00:24 +00001929// <type> ::= <vector-type>
1930// <vector-type> ::= Dv <positive dimension number> _
1931// <extended element type>
1932// ::= Dv [<dimension expression>] _ <element type>
1933// <extended element type> ::= <element type>
1934// ::= p # AltiVec vector pixel
John McCallcc5e23c2009-09-05 07:56:18 +00001935void CXXNameMangler::mangleType(const VectorType *T) {
Bob Wilson8470b332010-11-12 17:24:46 +00001936 if ((T->getVectorKind() == VectorType::NeonVector ||
Bob Wilsone4652b12010-11-16 00:32:18 +00001937 T->getVectorKind() == VectorType::NeonPolyVector)) {
1938 mangleNeonVectorType(T);
Bob Wilsonc1555212010-11-12 17:24:43 +00001939 return;
Bob Wilsone4652b12010-11-16 00:32:18 +00001940 }
Nick Lewycky3cfe6af2010-03-26 07:18:04 +00001941 Out << "Dv" << T->getNumElements() << '_';
Bob Wilsonaeb56442010-11-10 21:56:12 +00001942 if (T->getVectorKind() == VectorType::AltiVecPixel)
Chris Lattner37141f42010-06-23 06:00:24 +00001943 Out << 'p';
Bob Wilsonaeb56442010-11-10 21:56:12 +00001944 else if (T->getVectorKind() == VectorType::AltiVecBool)
Chris Lattner37141f42010-06-23 06:00:24 +00001945 Out << 'b';
1946 else
1947 mangleType(T->getElementType());
John McCallcc5e23c2009-09-05 07:56:18 +00001948}
1949void CXXNameMangler::mangleType(const ExtVectorType *T) {
1950 mangleType(static_cast<const VectorType*>(T));
1951}
1952void CXXNameMangler::mangleType(const DependentSizedExtVectorType *T) {
Nick Lewycky3cfe6af2010-03-26 07:18:04 +00001953 Out << "Dv";
1954 mangleExpression(T->getSizeExpr());
1955 Out << '_';
John McCallcc5e23c2009-09-05 07:56:18 +00001956 mangleType(T->getElementType());
1957}
1958
Douglas Gregord2fa7662010-12-20 02:24:11 +00001959void CXXNameMangler::mangleType(const PackExpansionType *T) {
Douglas Gregord81c7c12011-01-13 16:39:34 +00001960 // <type> ::= Dp <type> # pack expansion (C++0x)
Douglas Gregorb720ff22011-01-13 17:44:36 +00001961 Out << "Dp";
Douglas Gregord2fa7662010-12-20 02:24:11 +00001962 mangleType(T->getPattern());
1963}
1964
Anders Carlsson16d5d292009-03-07 22:03:21 +00001965void CXXNameMangler::mangleType(const ObjCInterfaceType *T) {
1966 mangleSourceName(T->getDecl()->getIdentifier());
1967}
1968
John McCall8b07ec22010-05-15 11:32:37 +00001969void CXXNameMangler::mangleType(const ObjCObjectType *T) {
John McCallb0efad12010-05-15 17:06:29 +00001970 // We don't allow overloading by different protocol qualification,
1971 // so mangling them isn't necessary.
John McCall8b07ec22010-05-15 11:32:37 +00001972 mangleType(T->getBaseType());
1973}
1974
John McCallcc5e23c2009-09-05 07:56:18 +00001975void CXXNameMangler::mangleType(const BlockPointerType *T) {
Anders Carlssona88d1972009-12-23 22:31:44 +00001976 Out << "U13block_pointer";
1977 mangleType(T->getPointeeType());
John McCallcc5e23c2009-09-05 07:56:18 +00001978}
1979
John McCall2408e322010-04-27 00:57:59 +00001980void CXXNameMangler::mangleType(const InjectedClassNameType *T) {
1981 // Mangle injected class name types as if the user had written the
1982 // specialization out fully. It may not actually be possible to see
1983 // this mangling, though.
1984 mangleType(T->getInjectedSpecializationType());
1985}
1986
John McCallcc5e23c2009-09-05 07:56:18 +00001987void CXXNameMangler::mangleType(const TemplateSpecializationType *T) {
Douglas Gregor7988def2010-04-28 05:58:56 +00001988 if (TemplateDecl *TD = T->getTemplateName().getAsTemplateDecl()) {
1989 mangleName(TD, T->getArgs(), T->getNumArgs());
1990 } else {
1991 if (mangleSubstitution(QualType(T, 0)))
1992 return;
Alexis Hunta8136cc2010-05-05 15:23:54 +00001993
Douglas Gregor7988def2010-04-28 05:58:56 +00001994 mangleTemplatePrefix(T->getTemplateName());
Alexis Hunta8136cc2010-05-05 15:23:54 +00001995
Douglas Gregor7988def2010-04-28 05:58:56 +00001996 // FIXME: GCC does not appear to mangle the template arguments when
1997 // the template in question is a dependent template name. Should we
1998 // emulate that badness?
1999 mangleTemplateArgs(T->getTemplateName(), T->getArgs(), T->getNumArgs());
2000 addSubstitution(QualType(T, 0));
2001 }
John McCallcc5e23c2009-09-05 07:56:18 +00002002}
2003
Douglas Gregorc1d2d8a2010-03-31 17:34:00 +00002004void CXXNameMangler::mangleType(const DependentNameType *T) {
Anders Carlssonbdaaab42009-09-26 02:26:02 +00002005 // Typename types are always nested
2006 Out << 'N';
John McCall6dc0a2b2011-04-24 08:23:24 +00002007 manglePrefix(T->getQualifier());
John McCallc392f372010-06-11 00:33:02 +00002008 mangleSourceName(T->getIdentifier());
2009 Out << 'E';
2010}
John McCalla5dd3262010-06-09 07:26:17 +00002011
John McCallc392f372010-06-11 00:33:02 +00002012void CXXNameMangler::mangleType(const DependentTemplateSpecializationType *T) {
Douglas Gregor6e068012011-02-28 00:04:36 +00002013 // Dependently-scoped template types are nested if they have a prefix.
John McCallc392f372010-06-11 00:33:02 +00002014 Out << 'N';
2015
2016 // TODO: avoid making this TemplateName.
2017 TemplateName Prefix =
2018 getASTContext().getDependentTemplateName(T->getQualifier(),
2019 T->getIdentifier());
2020 mangleTemplatePrefix(Prefix);
2021
2022 // FIXME: GCC does not appear to mangle the template arguments when
2023 // the template in question is a dependent template name. Should we
2024 // emulate that badness?
2025 mangleTemplateArgs(Prefix, T->getArgs(), T->getNumArgs());
Anders Carlssonbdaaab42009-09-26 02:26:02 +00002026 Out << 'E';
John McCallcc5e23c2009-09-05 07:56:18 +00002027}
2028
John McCallbd8d9bd2010-03-01 23:49:17 +00002029void CXXNameMangler::mangleType(const TypeOfType *T) {
2030 // FIXME: this is pretty unsatisfactory, but there isn't an obvious
2031 // "extension with parameters" mangling.
2032 Out << "u6typeof";
2033}
2034
2035void CXXNameMangler::mangleType(const TypeOfExprType *T) {
2036 // FIXME: this is pretty unsatisfactory, but there isn't an obvious
2037 // "extension with parameters" mangling.
2038 Out << "u6typeof";
2039}
2040
2041void CXXNameMangler::mangleType(const DecltypeType *T) {
2042 Expr *E = T->getUnderlyingExpr();
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00002043
John McCallbd8d9bd2010-03-01 23:49:17 +00002044 // type ::= Dt <expression> E # decltype of an id-expression
2045 // # or class member access
2046 // ::= DT <expression> E # decltype of an expression
2047
2048 // This purports to be an exhaustive list of id-expressions and
2049 // class member accesses. Note that we do not ignore parentheses;
2050 // parentheses change the semantics of decltype for these
2051 // expressions (and cause the mangler to use the other form).
2052 if (isa<DeclRefExpr>(E) ||
2053 isa<MemberExpr>(E) ||
2054 isa<UnresolvedLookupExpr>(E) ||
2055 isa<DependentScopeDeclRefExpr>(E) ||
2056 isa<CXXDependentScopeMemberExpr>(E) ||
2057 isa<UnresolvedMemberExpr>(E))
2058 Out << "Dt";
2059 else
2060 Out << "DT";
2061 mangleExpression(E);
2062 Out << 'E';
2063}
2064
Alexis Hunte852b102011-05-24 22:41:36 +00002065void CXXNameMangler::mangleType(const UnaryTransformType *T) {
2066 // If this is dependent, we need to record that. If not, we simply
2067 // mangle it as the underlying type since they are equivalent.
2068 if (T->isDependentType()) {
2069 Out << 'U';
2070
2071 switch (T->getUTTKind()) {
2072 case UnaryTransformType::EnumUnderlyingType:
2073 Out << "3eut";
2074 break;
2075 }
2076 }
2077
2078 mangleType(T->getUnderlyingType());
2079}
2080
Richard Smith30482bc2011-02-20 03:19:35 +00002081void CXXNameMangler::mangleType(const AutoType *T) {
2082 QualType D = T->getDeducedType();
Richard Smith55038052011-02-21 20:10:02 +00002083 // <builtin-type> ::= Da # dependent auto
2084 if (D.isNull())
2085 Out << "Da";
2086 else
2087 mangleType(D);
Richard Smith30482bc2011-02-20 03:19:35 +00002088}
2089
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00002090void CXXNameMangler::mangleIntegerLiteral(QualType T,
Anders Carlssone66e2942009-12-14 01:45:37 +00002091 const llvm::APSInt &Value) {
2092 // <expr-primary> ::= L <type> <value number> E # integer literal
2093 Out << 'L';
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00002094
Anders Carlssone66e2942009-12-14 01:45:37 +00002095 mangleType(T);
2096 if (T->isBooleanType()) {
2097 // Boolean values are encoded as 0/1.
2098 Out << (Value.getBoolValue() ? '1' : '0');
2099 } else {
John McCall05ddbb82010-07-14 04:20:34 +00002100 mangleNumber(Value);
Anders Carlssone66e2942009-12-14 01:45:37 +00002101 }
2102 Out << 'E';
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00002103
Anders Carlssone66e2942009-12-14 01:45:37 +00002104}
2105
John McCalld061b442010-02-04 02:56:29 +00002106/// Mangles a member expression. Implicit accesses are not handled,
2107/// but that should be okay, because you shouldn't be able to
2108/// make an implicit access in a function template declaration.
John McCall6dc0a2b2011-04-24 08:23:24 +00002109void CXXNameMangler::mangleMemberExpr(const Expr *base,
2110 bool isArrow,
2111 NestedNameSpecifier *qualifier,
2112 NamedDecl *firstQualifierLookup,
2113 DeclarationName member,
2114 unsigned arity) {
2115 // <expression> ::= dt <expression> <unresolved-name>
2116 // ::= pt <expression> <unresolved-name>
2117 Out << (isArrow ? "pt" : "dt");
2118 mangleExpression(base);
2119 mangleUnresolvedName(qualifier, firstQualifierLookup, member, arity);
John McCalld061b442010-02-04 02:56:29 +00002120}
2121
John McCall7f0a0d52011-04-28 02:52:03 +00002122/// Look at the callee of the given call expression and determine if
2123/// it's a parenthesized id-expression which would have triggered ADL
2124/// otherwise.
2125static bool isParenthesizedADLCallee(const CallExpr *call) {
2126 const Expr *callee = call->getCallee();
2127 const Expr *fn = callee->IgnoreParens();
2128
2129 // Must be parenthesized. IgnoreParens() skips __extension__ nodes,
2130 // too, but for those to appear in the callee, it would have to be
2131 // parenthesized.
2132 if (callee == fn) return false;
2133
2134 // Must be an unresolved lookup.
2135 const UnresolvedLookupExpr *lookup = dyn_cast<UnresolvedLookupExpr>(fn);
2136 if (!lookup) return false;
2137
2138 assert(!lookup->requiresADL());
2139
2140 // Must be an unqualified lookup.
2141 if (lookup->getQualifier()) return false;
2142
2143 // Must not have found a class member. Note that if one is a class
2144 // member, they're all class members.
2145 if (lookup->getNumDecls() > 0 &&
2146 (*lookup->decls_begin())->isCXXClassMember())
2147 return false;
2148
2149 // Otherwise, ADL would have been triggered.
2150 return true;
2151}
2152
John McCall78fbb612010-08-18 19:18:59 +00002153void CXXNameMangler::mangleExpression(const Expr *E, unsigned Arity) {
Anders Carlssona18322c2009-09-21 01:21:10 +00002154 // <expression> ::= <unary operator-name> <expression>
John McCall2adddca2010-02-03 00:55:45 +00002155 // ::= <binary operator-name> <expression> <expression>
2156 // ::= <trinary operator-name> <expression> <expression> <expression>
Anders Carlssona18322c2009-09-21 01:21:10 +00002157 // ::= cv <type> expression # conversion with one argument
2158 // ::= cv <type> _ <expression>* E # conversion with a different number of arguments
Eli Friedman04831922010-08-22 01:00:03 +00002159 // ::= st <type> # sizeof (a type)
Anders Carlssona18322c2009-09-21 01:21:10 +00002160 // ::= at <type> # alignof (a type)
2161 // ::= <template-param>
2162 // ::= <function-param>
2163 // ::= sr <type> <unqualified-name> # dependent name
2164 // ::= sr <type> <unqualified-name> <template-args> # dependent template-id
Douglas Gregorc507db42011-06-05 05:27:58 +00002165 // ::= ds <expression> <expression> # expr.*expr
Anders Carlssona18322c2009-09-21 01:21:10 +00002166 // ::= sZ <template-param> # size of a parameter pack
Douglas Gregord81c7c12011-01-13 16:39:34 +00002167 // ::= sZ <function-param> # size of a function parameter pack
John McCall2adddca2010-02-03 00:55:45 +00002168 // ::= <expr-primary>
John McCall09de8ec2010-02-04 01:42:13 +00002169 // <expr-primary> ::= L <type> <value number> E # integer literal
2170 // ::= L <type <value float> E # floating literal
2171 // ::= L <mangled-name> E # external name
Anders Carlssona18322c2009-09-21 01:21:10 +00002172 switch (E->getStmtClass()) {
John McCall6936c862010-04-09 22:26:14 +00002173 case Expr::NoStmtClass:
John McCallbd066782011-02-09 08:16:59 +00002174#define ABSTRACT_STMT(Type)
John McCall6936c862010-04-09 22:26:14 +00002175#define EXPR(Type, Base)
2176#define STMT(Type, Base) \
2177 case Expr::Type##Class:
Alexis Hunt656bb312010-05-05 15:24:00 +00002178#include "clang/AST/StmtNodes.inc"
John McCall05ddbb82010-07-14 04:20:34 +00002179 // fallthrough
2180
2181 // These all can only appear in local or variable-initialization
2182 // contexts and so should never appear in a mangling.
2183 case Expr::AddrLabelExprClass:
2184 case Expr::BlockDeclRefExprClass:
2185 case Expr::CXXThisExprClass:
2186 case Expr::DesignatedInitExprClass:
2187 case Expr::ImplicitValueInitExprClass:
2188 case Expr::InitListExprClass:
2189 case Expr::ParenListExprClass:
2190 case Expr::CXXScalarValueInitExprClass:
John McCall2adddca2010-02-03 00:55:45 +00002191 llvm_unreachable("unexpected statement kind");
2192 break;
2193
John McCall05ddbb82010-07-14 04:20:34 +00002194 // FIXME: invent manglings for all these.
2195 case Expr::BlockExprClass:
2196 case Expr::CXXPseudoDestructorExprClass:
2197 case Expr::ChooseExprClass:
2198 case Expr::CompoundLiteralExprClass:
2199 case Expr::ExtVectorElementExprClass:
Peter Collingbourne91147592011-04-15 00:35:48 +00002200 case Expr::GenericSelectionExprClass:
John McCall05ddbb82010-07-14 04:20:34 +00002201 case Expr::ObjCEncodeExprClass:
John McCall05ddbb82010-07-14 04:20:34 +00002202 case Expr::ObjCIsaExprClass:
2203 case Expr::ObjCIvarRefExprClass:
2204 case Expr::ObjCMessageExprClass:
2205 case Expr::ObjCPropertyRefExprClass:
2206 case Expr::ObjCProtocolExprClass:
2207 case Expr::ObjCSelectorExprClass:
2208 case Expr::ObjCStringLiteralClass:
John McCall31168b02011-06-15 23:02:42 +00002209 case Expr::ObjCIndirectCopyRestoreExprClass:
John McCall05ddbb82010-07-14 04:20:34 +00002210 case Expr::OffsetOfExprClass:
2211 case Expr::PredefinedExprClass:
2212 case Expr::ShuffleVectorExprClass:
2213 case Expr::StmtExprClass:
John McCall05ddbb82010-07-14 04:20:34 +00002214 case Expr::UnaryTypeTraitExprClass:
Francois Pichet9dfa3ce2010-12-07 00:08:36 +00002215 case Expr::BinaryTypeTraitExprClass:
John Wiegley6242b6a2011-04-28 00:16:57 +00002216 case Expr::ArrayTypeTraitExprClass:
John Wiegleyf9f65842011-04-25 06:54:41 +00002217 case Expr::ExpressionTraitExprClass:
Francois Pichet5cc0a672010-09-08 23:47:05 +00002218 case Expr::VAArgExprClass:
Sebastian Redl4202c0f2010-09-10 20:55:43 +00002219 case Expr::CXXUuidofExprClass:
Peter Collingbourne41f85462011-02-09 21:07:24 +00002220 case Expr::CXXNoexceptExprClass:
Tanya Lattner55808c12011-06-04 00:47:47 +00002221 case Expr::CUDAKernelCallExprClass:
2222 case Expr::AsTypeExprClass:
2223 {
John McCall6936c862010-04-09 22:26:14 +00002224 // As bad as this diagnostic is, it's better than crashing.
2225 Diagnostic &Diags = Context.getDiags();
2226 unsigned DiagID = Diags.getCustomDiagID(Diagnostic::Error,
2227 "cannot yet mangle expression type %0");
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +00002228 Diags.Report(E->getExprLoc(), DiagID)
John McCalle2f35242010-04-10 09:39:25 +00002229 << E->getStmtClassName() << E->getSourceRange();
John McCall6936c862010-04-09 22:26:14 +00002230 break;
2231 }
2232
John McCallc07a0c72011-02-17 10:25:35 +00002233 // Even gcc-4.5 doesn't mangle this.
2234 case Expr::BinaryConditionalOperatorClass: {
2235 Diagnostic &Diags = Context.getDiags();
2236 unsigned DiagID =
2237 Diags.getCustomDiagID(Diagnostic::Error,
2238 "?: operator with omitted middle operand cannot be mangled");
2239 Diags.Report(E->getExprLoc(), DiagID)
2240 << E->getStmtClassName() << E->getSourceRange();
2241 break;
2242 }
2243
2244 // These are used for internal purposes and cannot be meaningfully mangled.
John McCall8d69a212010-11-15 23:31:06 +00002245 case Expr::OpaqueValueExprClass:
2246 llvm_unreachable("cannot mangle opaque value; mangling wrong thing?");
2247
John McCall05ddbb82010-07-14 04:20:34 +00002248 case Expr::CXXDefaultArgExprClass:
John McCall78fbb612010-08-18 19:18:59 +00002249 mangleExpression(cast<CXXDefaultArgExpr>(E)->getExpr(), Arity);
John McCall05ddbb82010-07-14 04:20:34 +00002250 break;
2251
2252 case Expr::CXXMemberCallExprClass: // fallthrough
John McCall09de8ec2010-02-04 01:42:13 +00002253 case Expr::CallExprClass: {
2254 const CallExpr *CE = cast<CallExpr>(E);
John McCall7f0a0d52011-04-28 02:52:03 +00002255
2256 // <expression> ::= cp <simple-id> <expression>* E
2257 // We use this mangling only when the call would use ADL except
2258 // for being parenthesized. Per discussion with David
2259 // Vandervoorde, 2011.04.25.
2260 if (isParenthesizedADLCallee(CE)) {
2261 Out << "cp";
2262 // The callee here is a parenthesized UnresolvedLookupExpr with
2263 // no qualifier and should always get mangled as a <simple-id>
2264 // anyway.
2265
2266 // <expression> ::= cl <expression>* E
2267 } else {
2268 Out << "cl";
2269 }
2270
John McCall78fbb612010-08-18 19:18:59 +00002271 mangleExpression(CE->getCallee(), CE->getNumArgs());
John McCall09de8ec2010-02-04 01:42:13 +00002272 for (unsigned I = 0, N = CE->getNumArgs(); I != N; ++I)
2273 mangleExpression(CE->getArg(I));
Benjamin Kramer1554e4a2010-04-10 16:03:31 +00002274 Out << 'E';
John McCall2adddca2010-02-03 00:55:45 +00002275 break;
John McCall09de8ec2010-02-04 01:42:13 +00002276 }
John McCall2adddca2010-02-03 00:55:45 +00002277
John McCall05ddbb82010-07-14 04:20:34 +00002278 case Expr::CXXNewExprClass: {
2279 // Proposal from David Vandervoorde, 2010.06.30
2280 const CXXNewExpr *New = cast<CXXNewExpr>(E);
2281 if (New->isGlobalNew()) Out << "gs";
2282 Out << (New->isArray() ? "na" : "nw");
2283 for (CXXNewExpr::const_arg_iterator I = New->placement_arg_begin(),
2284 E = New->placement_arg_end(); I != E; ++I)
2285 mangleExpression(*I);
2286 Out << '_';
2287 mangleType(New->getAllocatedType());
2288 if (New->hasInitializer()) {
2289 Out << "pi";
2290 for (CXXNewExpr::const_arg_iterator I = New->constructor_arg_begin(),
2291 E = New->constructor_arg_end(); I != E; ++I)
2292 mangleExpression(*I);
2293 }
2294 Out << 'E';
2295 break;
2296 }
2297
John McCalld061b442010-02-04 02:56:29 +00002298 case Expr::MemberExprClass: {
2299 const MemberExpr *ME = cast<MemberExpr>(E);
2300 mangleMemberExpr(ME->getBase(), ME->isArrow(),
John McCall6dc0a2b2011-04-24 08:23:24 +00002301 ME->getQualifier(), 0, ME->getMemberDecl()->getDeclName(),
John McCall78fbb612010-08-18 19:18:59 +00002302 Arity);
John McCalld061b442010-02-04 02:56:29 +00002303 break;
2304 }
2305
2306 case Expr::UnresolvedMemberExprClass: {
2307 const UnresolvedMemberExpr *ME = cast<UnresolvedMemberExpr>(E);
2308 mangleMemberExpr(ME->getBase(), ME->isArrow(),
John McCall6dc0a2b2011-04-24 08:23:24 +00002309 ME->getQualifier(), 0, ME->getMemberName(),
John McCall78fbb612010-08-18 19:18:59 +00002310 Arity);
John McCallf834bcd2010-08-20 00:17:19 +00002311 if (ME->hasExplicitTemplateArgs())
2312 mangleTemplateArgs(ME->getExplicitTemplateArgs());
John McCalld061b442010-02-04 02:56:29 +00002313 break;
2314 }
2315
2316 case Expr::CXXDependentScopeMemberExprClass: {
2317 const CXXDependentScopeMemberExpr *ME
2318 = cast<CXXDependentScopeMemberExpr>(E);
2319 mangleMemberExpr(ME->getBase(), ME->isArrow(),
John McCall6dc0a2b2011-04-24 08:23:24 +00002320 ME->getQualifier(), ME->getFirstQualifierFoundInScope(),
2321 ME->getMember(), Arity);
John McCallf834bcd2010-08-20 00:17:19 +00002322 if (ME->hasExplicitTemplateArgs())
2323 mangleTemplateArgs(ME->getExplicitTemplateArgs());
John McCalld061b442010-02-04 02:56:29 +00002324 break;
2325 }
2326
John McCall09de8ec2010-02-04 01:42:13 +00002327 case Expr::UnresolvedLookupExprClass: {
2328 const UnresolvedLookupExpr *ULE = cast<UnresolvedLookupExpr>(E);
John McCall6dc0a2b2011-04-24 08:23:24 +00002329 mangleUnresolvedName(ULE->getQualifier(), 0, ULE->getName(), Arity);
John McCall575fda12011-06-21 22:12:46 +00002330
2331 // All the <unresolved-name> productions end in a
2332 // base-unresolved-name, where <template-args> are just tacked
2333 // onto the end.
John McCallf834bcd2010-08-20 00:17:19 +00002334 if (ULE->hasExplicitTemplateArgs())
2335 mangleTemplateArgs(ULE->getExplicitTemplateArgs());
John McCall09de8ec2010-02-04 01:42:13 +00002336 break;
2337 }
2338
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00002339 case Expr::CXXUnresolvedConstructExprClass: {
John McCall09de8ec2010-02-04 01:42:13 +00002340 const CXXUnresolvedConstructExpr *CE = cast<CXXUnresolvedConstructExpr>(E);
2341 unsigned N = CE->arg_size();
2342
2343 Out << "cv";
2344 mangleType(CE->getType());
Benjamin Kramer1554e4a2010-04-10 16:03:31 +00002345 if (N != 1) Out << '_';
John McCall09de8ec2010-02-04 01:42:13 +00002346 for (unsigned I = 0; I != N; ++I) mangleExpression(CE->getArg(I));
Benjamin Kramer1554e4a2010-04-10 16:03:31 +00002347 if (N != 1) Out << 'E';
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00002348 break;
John McCall09de8ec2010-02-04 01:42:13 +00002349 }
John McCall2adddca2010-02-03 00:55:45 +00002350
John McCall09de8ec2010-02-04 01:42:13 +00002351 case Expr::CXXTemporaryObjectExprClass:
2352 case Expr::CXXConstructExprClass: {
2353 const CXXConstructExpr *CE = cast<CXXConstructExpr>(E);
2354 unsigned N = CE->getNumArgs();
2355
2356 Out << "cv";
2357 mangleType(CE->getType());
Benjamin Kramer1554e4a2010-04-10 16:03:31 +00002358 if (N != 1) Out << '_';
John McCall09de8ec2010-02-04 01:42:13 +00002359 for (unsigned I = 0; I != N; ++I) mangleExpression(CE->getArg(I));
Benjamin Kramer1554e4a2010-04-10 16:03:31 +00002360 if (N != 1) Out << 'E';
John McCall2adddca2010-02-03 00:55:45 +00002361 break;
John McCall09de8ec2010-02-04 01:42:13 +00002362 }
2363
Peter Collingbournee190dee2011-03-11 19:24:49 +00002364 case Expr::UnaryExprOrTypeTraitExprClass: {
2365 const UnaryExprOrTypeTraitExpr *SAE = cast<UnaryExprOrTypeTraitExpr>(E);
2366 switch(SAE->getKind()) {
2367 case UETT_SizeOf:
2368 Out << 's';
2369 break;
2370 case UETT_AlignOf:
2371 Out << 'a';
2372 break;
2373 case UETT_VecStep:
2374 Diagnostic &Diags = Context.getDiags();
2375 unsigned DiagID = Diags.getCustomDiagID(Diagnostic::Error,
2376 "cannot yet mangle vec_step expression");
2377 Diags.Report(DiagID);
2378 return;
2379 }
John McCall09de8ec2010-02-04 01:42:13 +00002380 if (SAE->isArgumentType()) {
Benjamin Kramer1554e4a2010-04-10 16:03:31 +00002381 Out << 't';
John McCall09de8ec2010-02-04 01:42:13 +00002382 mangleType(SAE->getArgumentType());
2383 } else {
Benjamin Kramer1554e4a2010-04-10 16:03:31 +00002384 Out << 'z';
John McCall09de8ec2010-02-04 01:42:13 +00002385 mangleExpression(SAE->getArgumentExpr());
2386 }
2387 break;
2388 }
Anders Carlsson9e4e0232009-11-06 02:50:19 +00002389
John McCall05ddbb82010-07-14 04:20:34 +00002390 case Expr::CXXThrowExprClass: {
2391 const CXXThrowExpr *TE = cast<CXXThrowExpr>(E);
2392
2393 // Proposal from David Vandervoorde, 2010.06.30
2394 if (TE->getSubExpr()) {
2395 Out << "tw";
2396 mangleExpression(TE->getSubExpr());
2397 } else {
2398 Out << "tr";
2399 }
2400 break;
2401 }
2402
2403 case Expr::CXXTypeidExprClass: {
2404 const CXXTypeidExpr *TIE = cast<CXXTypeidExpr>(E);
2405
2406 // Proposal from David Vandervoorde, 2010.06.30
2407 if (TIE->isTypeOperand()) {
2408 Out << "ti";
2409 mangleType(TIE->getTypeOperand());
2410 } else {
2411 Out << "te";
2412 mangleExpression(TIE->getExprOperand());
2413 }
2414 break;
2415 }
2416
2417 case Expr::CXXDeleteExprClass: {
2418 const CXXDeleteExpr *DE = cast<CXXDeleteExpr>(E);
2419
2420 // Proposal from David Vandervoorde, 2010.06.30
2421 if (DE->isGlobalDelete()) Out << "gs";
2422 Out << (DE->isArrayForm() ? "da" : "dl");
2423 mangleExpression(DE->getArgument());
2424 break;
2425 }
2426
Anders Carlssone66e2942009-12-14 01:45:37 +00002427 case Expr::UnaryOperatorClass: {
2428 const UnaryOperator *UO = cast<UnaryOperator>(E);
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00002429 mangleOperatorName(UnaryOperator::getOverloadedOperator(UO->getOpcode()),
Anders Carlssone66e2942009-12-14 01:45:37 +00002430 /*Arity=*/1);
2431 mangleExpression(UO->getSubExpr());
2432 break;
2433 }
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00002434
John McCall05ddbb82010-07-14 04:20:34 +00002435 case Expr::ArraySubscriptExprClass: {
2436 const ArraySubscriptExpr *AE = cast<ArraySubscriptExpr>(E);
2437
Chris Lattner57540c52011-04-15 05:22:18 +00002438 // Array subscript is treated as a syntactically weird form of
John McCall05ddbb82010-07-14 04:20:34 +00002439 // binary operator.
2440 Out << "ix";
2441 mangleExpression(AE->getLHS());
2442 mangleExpression(AE->getRHS());
2443 break;
2444 }
2445
2446 case Expr::CompoundAssignOperatorClass: // fallthrough
Anders Carlssone66e2942009-12-14 01:45:37 +00002447 case Expr::BinaryOperatorClass: {
2448 const BinaryOperator *BO = cast<BinaryOperator>(E);
Douglas Gregorc507db42011-06-05 05:27:58 +00002449 if (BO->getOpcode() == BO_PtrMemD)
2450 Out << "ds";
2451 else
2452 mangleOperatorName(BinaryOperator::getOverloadedOperator(BO->getOpcode()),
2453 /*Arity=*/2);
Anders Carlssone66e2942009-12-14 01:45:37 +00002454 mangleExpression(BO->getLHS());
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00002455 mangleExpression(BO->getRHS());
Anders Carlssone66e2942009-12-14 01:45:37 +00002456 break;
John McCalld061b442010-02-04 02:56:29 +00002457 }
Anders Carlssone66e2942009-12-14 01:45:37 +00002458
2459 case Expr::ConditionalOperatorClass: {
2460 const ConditionalOperator *CO = cast<ConditionalOperator>(E);
2461 mangleOperatorName(OO_Conditional, /*Arity=*/3);
2462 mangleExpression(CO->getCond());
John McCall78fbb612010-08-18 19:18:59 +00002463 mangleExpression(CO->getLHS(), Arity);
2464 mangleExpression(CO->getRHS(), Arity);
Anders Carlssone66e2942009-12-14 01:45:37 +00002465 break;
2466 }
2467
Douglas Gregor16810ca2010-01-29 16:37:09 +00002468 case Expr::ImplicitCastExprClass: {
John McCall78fbb612010-08-18 19:18:59 +00002469 mangleExpression(cast<ImplicitCastExpr>(E)->getSubExpr(), Arity);
Douglas Gregor16810ca2010-01-29 16:37:09 +00002470 break;
2471 }
John McCall31168b02011-06-15 23:02:42 +00002472
2473 case Expr::ObjCBridgedCastExprClass: {
2474 // Mangle ownership casts as a vendor extended operator __bridge,
2475 // __bridge_transfer, or __bridge_retain.
2476 llvm::StringRef Kind = cast<ObjCBridgedCastExpr>(E)->getBridgeKindName();
2477 Out << "v1U" << Kind.size() << Kind;
2478 }
2479 // Fall through to mangle the cast itself.
2480
Douglas Gregor16810ca2010-01-29 16:37:09 +00002481 case Expr::CStyleCastExprClass:
2482 case Expr::CXXStaticCastExprClass:
2483 case Expr::CXXDynamicCastExprClass:
2484 case Expr::CXXReinterpretCastExprClass:
2485 case Expr::CXXConstCastExprClass:
2486 case Expr::CXXFunctionalCastExprClass: {
2487 const ExplicitCastExpr *ECE = cast<ExplicitCastExpr>(E);
2488 Out << "cv";
2489 mangleType(ECE->getType());
2490 mangleExpression(ECE->getSubExpr());
2491 break;
2492 }
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00002493
Anders Carlssonb7e93e22009-12-16 05:48:46 +00002494 case Expr::CXXOperatorCallExprClass: {
2495 const CXXOperatorCallExpr *CE = cast<CXXOperatorCallExpr>(E);
2496 unsigned NumArgs = CE->getNumArgs();
2497 mangleOperatorName(CE->getOperator(), /*Arity=*/NumArgs);
2498 // Mangle the arguments.
2499 for (unsigned i = 0; i != NumArgs; ++i)
2500 mangleExpression(CE->getArg(i));
2501 break;
2502 }
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00002503
Anders Carlsson9e4e0232009-11-06 02:50:19 +00002504 case Expr::ParenExprClass:
John McCall78fbb612010-08-18 19:18:59 +00002505 mangleExpression(cast<ParenExpr>(E)->getSubExpr(), Arity);
Anders Carlsson9e4e0232009-11-06 02:50:19 +00002506 break;
2507
Anders Carlssona18322c2009-09-21 01:21:10 +00002508 case Expr::DeclRefExprClass: {
Douglas Gregor5610db62010-02-28 21:40:32 +00002509 const NamedDecl *D = cast<DeclRefExpr>(E)->getDecl();
Daniel Dunbard614e322009-11-21 09:05:47 +00002510
Anders Carlssona18322c2009-09-21 01:21:10 +00002511 switch (D->getKind()) {
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00002512 default:
Douglas Gregor5610db62010-02-28 21:40:32 +00002513 // <expr-primary> ::= L <mangled-name> E # external name
2514 Out << 'L';
2515 mangle(D, "_Z");
2516 Out << 'E';
2517 break;
2518
John McCall8fb0d9d2011-05-01 22:35:37 +00002519 case Decl::ParmVar:
2520 mangleFunctionParam(cast<ParmVarDecl>(D));
2521 break;
2522
John McCall268b5762010-07-24 01:17:35 +00002523 case Decl::EnumConstant: {
2524 const EnumConstantDecl *ED = cast<EnumConstantDecl>(D);
2525 mangleIntegerLiteral(ED->getType(), ED->getInitVal());
2526 break;
2527 }
2528
Anders Carlssona18322c2009-09-21 01:21:10 +00002529 case Decl::NonTypeTemplateParm: {
2530 const NonTypeTemplateParmDecl *PD = cast<NonTypeTemplateParmDecl>(D);
Anders Carlssone00745b2009-09-27 00:38:53 +00002531 mangleTemplateParameter(PD->getIndex());
Anders Carlssona18322c2009-09-21 01:21:10 +00002532 break;
2533 }
2534
2535 }
Daniel Dunbard614e322009-11-21 09:05:47 +00002536
Anders Carlsson02bca732009-09-27 20:11:34 +00002537 break;
Anders Carlssona18322c2009-09-21 01:21:10 +00002538 }
Daniel Dunbard614e322009-11-21 09:05:47 +00002539
Douglas Gregorcdbc5392011-01-15 01:15:58 +00002540 case Expr::SubstNonTypeTemplateParmPackExprClass:
John McCall3a4a4c52011-07-01 00:04:39 +00002541 // FIXME: not clear how to mangle this!
2542 // template <unsigned N...> class A {
2543 // template <class U...> void foo(U (&x)[N]...);
2544 // };
2545 Out << "_SUBSTPACK_";
Douglas Gregorcdbc5392011-01-15 01:15:58 +00002546 break;
2547
John McCall8cd78132009-11-19 22:55:06 +00002548 case Expr::DependentScopeDeclRefExprClass: {
2549 const DependentScopeDeclRefExpr *DRE = cast<DependentScopeDeclRefExpr>(E);
John McCall575fda12011-06-21 22:12:46 +00002550 mangleUnresolvedName(DRE->getQualifier(), 0, DRE->getDeclName(), Arity);
Douglas Gregora51c6742010-02-28 22:05:49 +00002551
John McCall575fda12011-06-21 22:12:46 +00002552 // All the <unresolved-name> productions end in a
2553 // base-unresolved-name, where <template-args> are just tacked
2554 // onto the end.
John McCallf834bcd2010-08-20 00:17:19 +00002555 if (DRE->hasExplicitTemplateArgs())
2556 mangleTemplateArgs(DRE->getExplicitTemplateArgs());
Anders Carlsson02bca732009-09-27 20:11:34 +00002557 break;
2558 }
2559
John McCalle8917e02010-04-09 22:54:09 +00002560 case Expr::CXXBindTemporaryExprClass:
2561 mangleExpression(cast<CXXBindTemporaryExpr>(E)->getSubExpr());
2562 break;
2563
John McCall5d413782010-12-06 08:20:24 +00002564 case Expr::ExprWithCleanupsClass:
2565 mangleExpression(cast<ExprWithCleanups>(E)->getSubExpr(), Arity);
John McCalle8917e02010-04-09 22:54:09 +00002566 break;
2567
John McCall09de8ec2010-02-04 01:42:13 +00002568 case Expr::FloatingLiteralClass: {
2569 const FloatingLiteral *FL = cast<FloatingLiteral>(E);
Benjamin Kramer1554e4a2010-04-10 16:03:31 +00002570 Out << 'L';
John McCall09de8ec2010-02-04 01:42:13 +00002571 mangleType(FL->getType());
John McCall05ddbb82010-07-14 04:20:34 +00002572 mangleFloat(FL->getValue());
Benjamin Kramer1554e4a2010-04-10 16:03:31 +00002573 Out << 'E';
John McCall09de8ec2010-02-04 01:42:13 +00002574 break;
2575 }
2576
John McCall092775422010-04-09 21:48:08 +00002577 case Expr::CharacterLiteralClass:
Benjamin Kramer1554e4a2010-04-10 16:03:31 +00002578 Out << 'L';
John McCall092775422010-04-09 21:48:08 +00002579 mangleType(E->getType());
2580 Out << cast<CharacterLiteral>(E)->getValue();
2581 Out << 'E';
2582 break;
2583
2584 case Expr::CXXBoolLiteralExprClass:
2585 Out << "Lb";
2586 Out << (cast<CXXBoolLiteralExpr>(E)->getValue() ? '1' : '0');
2587 Out << 'E';
2588 break;
2589
John McCall05ddbb82010-07-14 04:20:34 +00002590 case Expr::IntegerLiteralClass: {
2591 llvm::APSInt Value(cast<IntegerLiteral>(E)->getValue());
2592 if (E->getType()->isSignedIntegerType())
2593 Value.setIsSigned(true);
2594 mangleIntegerLiteral(E->getType(), Value);
Anders Carlssone66e2942009-12-14 01:45:37 +00002595 break;
John McCall05ddbb82010-07-14 04:20:34 +00002596 }
2597
2598 case Expr::ImaginaryLiteralClass: {
2599 const ImaginaryLiteral *IE = cast<ImaginaryLiteral>(E);
2600 // Mangle as if a complex literal.
Nick Lewycky1b36b552010-09-05 03:40:33 +00002601 // Proposal from David Vandevoorde, 2010.06.30.
John McCall05ddbb82010-07-14 04:20:34 +00002602 Out << 'L';
2603 mangleType(E->getType());
2604 if (const FloatingLiteral *Imag =
2605 dyn_cast<FloatingLiteral>(IE->getSubExpr())) {
2606 // Mangle a floating-point zero of the appropriate type.
2607 mangleFloat(llvm::APFloat(Imag->getValue().getSemantics()));
2608 Out << '_';
2609 mangleFloat(Imag->getValue());
2610 } else {
Nick Lewycky1b36b552010-09-05 03:40:33 +00002611 Out << "0_";
John McCall05ddbb82010-07-14 04:20:34 +00002612 llvm::APSInt Value(cast<IntegerLiteral>(IE->getSubExpr())->getValue());
2613 if (IE->getSubExpr()->getType()->isSignedIntegerType())
2614 Value.setIsSigned(true);
2615 mangleNumber(Value);
2616 }
2617 Out << 'E';
2618 break;
2619 }
2620
2621 case Expr::StringLiteralClass: {
John McCallec624b22010-07-15 21:53:03 +00002622 // Revised proposal from David Vandervoorde, 2010.07.15.
John McCall05ddbb82010-07-14 04:20:34 +00002623 Out << 'L';
John McCallec624b22010-07-15 21:53:03 +00002624 assert(isa<ConstantArrayType>(E->getType()));
2625 mangleType(E->getType());
John McCall05ddbb82010-07-14 04:20:34 +00002626 Out << 'E';
2627 break;
2628 }
2629
2630 case Expr::GNUNullExprClass:
2631 // FIXME: should this really be mangled the same as nullptr?
2632 // fallthrough
2633
2634 case Expr::CXXNullPtrLiteralExprClass: {
2635 // Proposal from David Vandervoorde, 2010.06.30, as
2636 // modified by ABI list discussion.
2637 Out << "LDnE";
2638 break;
2639 }
Douglas Gregore8e9dd62011-01-03 17:17:50 +00002640
2641 case Expr::PackExpansionExprClass:
2642 Out << "sp";
2643 mangleExpression(cast<PackExpansionExpr>(E)->getPattern());
2644 break;
Douglas Gregorbe63bbf2011-01-04 18:56:13 +00002645
2646 case Expr::SizeOfPackExprClass: {
Douglas Gregorbe63bbf2011-01-04 18:56:13 +00002647 Out << "sZ";
2648 const NamedDecl *Pack = cast<SizeOfPackExpr>(E)->getPack();
2649 if (const TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(Pack))
2650 mangleTemplateParameter(TTP->getIndex());
2651 else if (const NonTypeTemplateParmDecl *NTTP
2652 = dyn_cast<NonTypeTemplateParmDecl>(Pack))
2653 mangleTemplateParameter(NTTP->getIndex());
2654 else if (const TemplateTemplateParmDecl *TempTP
2655 = dyn_cast<TemplateTemplateParmDecl>(Pack))
2656 mangleTemplateParameter(TempTP->getIndex());
2657 else {
Douglas Gregord81c7c12011-01-13 16:39:34 +00002658 // Note: proposed by Mike Herrick on 11/30/10
2659 // <expression> ::= sZ <function-param> # size of function parameter pack
Douglas Gregorbe63bbf2011-01-04 18:56:13 +00002660 Diagnostic &Diags = Context.getDiags();
2661 unsigned DiagID = Diags.getCustomDiagID(Diagnostic::Error,
2662 "cannot mangle sizeof...(function parameter pack)");
2663 Diags.Report(DiagID);
2664 return;
2665 }
Douglas Gregorce449ab2011-03-03 02:20:19 +00002666 break;
Douglas Gregorbe63bbf2011-01-04 18:56:13 +00002667 }
Douglas Gregorfe314812011-06-21 17:03:29 +00002668
2669 case Expr::MaterializeTemporaryExprClass: {
2670 mangleExpression(cast<MaterializeTemporaryExpr>(E)->GetTemporaryExpr());
2671 break;
2672 }
Anders Carlssona18322c2009-09-21 01:21:10 +00002673 }
Douglas Gregor5fec5b02009-02-13 00:10:09 +00002674}
2675
John McCall8fb0d9d2011-05-01 22:35:37 +00002676/// Mangle an expression which refers to a parameter variable.
2677///
2678/// <expression> ::= <function-param>
2679/// <function-param> ::= fp <top-level CV-qualifiers> _ # L == 0, I == 0
2680/// <function-param> ::= fp <top-level CV-qualifiers>
2681/// <parameter-2 non-negative number> _ # L == 0, I > 0
2682/// <function-param> ::= fL <L-1 non-negative number>
2683/// p <top-level CV-qualifiers> _ # L > 0, I == 0
2684/// <function-param> ::= fL <L-1 non-negative number>
2685/// p <top-level CV-qualifiers>
2686/// <I-1 non-negative number> _ # L > 0, I > 0
2687///
2688/// L is the nesting depth of the parameter, defined as 1 if the
2689/// parameter comes from the innermost function prototype scope
2690/// enclosing the current context, 2 if from the next enclosing
2691/// function prototype scope, and so on, with one special case: if
2692/// we've processed the full parameter clause for the innermost
2693/// function type, then L is one less. This definition conveniently
2694/// makes it irrelevant whether a function's result type was written
2695/// trailing or leading, but is otherwise overly complicated; the
2696/// numbering was first designed without considering references to
2697/// parameter in locations other than return types, and then the
2698/// mangling had to be generalized without changing the existing
2699/// manglings.
2700///
2701/// I is the zero-based index of the parameter within its parameter
2702/// declaration clause. Note that the original ABI document describes
2703/// this using 1-based ordinals.
2704void CXXNameMangler::mangleFunctionParam(const ParmVarDecl *parm) {
2705 unsigned parmDepth = parm->getFunctionScopeDepth();
2706 unsigned parmIndex = parm->getFunctionScopeIndex();
2707
2708 // Compute 'L'.
2709 // parmDepth does not include the declaring function prototype.
2710 // FunctionTypeDepth does account for that.
2711 assert(parmDepth < FunctionTypeDepth.getDepth());
2712 unsigned nestingDepth = FunctionTypeDepth.getDepth() - parmDepth;
2713 if (FunctionTypeDepth.isInResultType())
2714 nestingDepth--;
2715
2716 if (nestingDepth == 0) {
2717 Out << "fp";
2718 } else {
2719 Out << "fL" << (nestingDepth - 1) << 'p';
2720 }
2721
2722 // Top-level qualifiers. We don't have to worry about arrays here,
2723 // because parameters declared as arrays should already have been
2724 // tranformed to have pointer type. FIXME: apparently these don't
2725 // get mangled if used as an rvalue of a known non-class type?
2726 assert(!parm->getType()->isArrayType()
2727 && "parameter's type is still an array type?");
2728 mangleQualifiers(parm->getType().getQualifiers());
2729
2730 // Parameter index.
2731 if (parmIndex != 0) {
2732 Out << (parmIndex - 1);
2733 }
2734 Out << '_';
2735}
2736
Anders Carlssone4c40c82009-04-15 05:36:58 +00002737void CXXNameMangler::mangleCXXCtorType(CXXCtorType T) {
2738 // <ctor-dtor-name> ::= C1 # complete object constructor
2739 // ::= C2 # base object constructor
2740 // ::= C3 # complete object allocating constructor
2741 //
2742 switch (T) {
2743 case Ctor_Complete:
2744 Out << "C1";
2745 break;
2746 case Ctor_Base:
2747 Out << "C2";
2748 break;
2749 case Ctor_CompleteAllocating:
2750 Out << "C3";
2751 break;
2752 }
2753}
2754
Anders Carlssoneaa28f72009-04-17 01:58:57 +00002755void CXXNameMangler::mangleCXXDtorType(CXXDtorType T) {
2756 // <ctor-dtor-name> ::= D0 # deleting destructor
2757 // ::= D1 # complete object destructor
2758 // ::= D2 # base object destructor
2759 //
2760 switch (T) {
2761 case Dtor_Deleting:
2762 Out << "D0";
2763 break;
2764 case Dtor_Complete:
2765 Out << "D1";
2766 break;
2767 case Dtor_Base:
2768 Out << "D2";
2769 break;
2770 }
2771}
2772
John McCallf834bcd2010-08-20 00:17:19 +00002773void CXXNameMangler::mangleTemplateArgs(
2774 const ExplicitTemplateArgumentList &TemplateArgs) {
2775 // <template-args> ::= I <template-arg>+ E
2776 Out << 'I';
John McCall3ab84762011-05-04 01:45:19 +00002777 for (unsigned i = 0, e = TemplateArgs.NumTemplateArgs; i != e; ++i)
2778 mangleTemplateArg(0, TemplateArgs.getTemplateArgs()[i].getArgument());
John McCallf834bcd2010-08-20 00:17:19 +00002779 Out << 'E';
2780}
2781
Douglas Gregor17362712010-04-23 03:10:43 +00002782void CXXNameMangler::mangleTemplateArgs(TemplateName Template,
2783 const TemplateArgument *TemplateArgs,
2784 unsigned NumTemplateArgs) {
2785 if (TemplateDecl *TD = Template.getAsTemplateDecl())
2786 return mangleTemplateArgs(*TD->getTemplateParameters(), TemplateArgs,
2787 NumTemplateArgs);
Alexis Hunta8136cc2010-05-05 15:23:54 +00002788
John McCall3ab84762011-05-04 01:45:19 +00002789 mangleUnresolvedTemplateArgs(TemplateArgs, NumTemplateArgs);
2790}
2791
2792void CXXNameMangler::mangleUnresolvedTemplateArgs(const TemplateArgument *args,
2793 unsigned numArgs) {
Douglas Gregor17362712010-04-23 03:10:43 +00002794 // <template-args> ::= I <template-arg>+ E
2795 Out << 'I';
John McCall3ab84762011-05-04 01:45:19 +00002796 for (unsigned i = 0; i != numArgs; ++i)
2797 mangleTemplateArg(0, args[i]);
Douglas Gregor17362712010-04-23 03:10:43 +00002798 Out << 'E';
2799}
2800
Rafael Espindola4d5c3d92010-03-11 14:07:00 +00002801void CXXNameMangler::mangleTemplateArgs(const TemplateParameterList &PL,
2802 const TemplateArgumentList &AL) {
Anders Carlssonf6e9ece2009-05-15 16:09:15 +00002803 // <template-args> ::= I <template-arg>+ E
Benjamin Kramer1554e4a2010-04-10 16:03:31 +00002804 Out << 'I';
Rafael Espindola4d5c3d92010-03-11 14:07:00 +00002805 for (unsigned i = 0, e = AL.size(); i != e; ++i)
2806 mangleTemplateArg(PL.getParam(i), AL[i]);
Benjamin Kramer1554e4a2010-04-10 16:03:31 +00002807 Out << 'E';
Anders Carlssonf6e9ece2009-05-15 16:09:15 +00002808}
2809
Rafael Espindola4d5c3d92010-03-11 14:07:00 +00002810void CXXNameMangler::mangleTemplateArgs(const TemplateParameterList &PL,
2811 const TemplateArgument *TemplateArgs,
Anders Carlsson7a8a74f2009-09-18 02:42:01 +00002812 unsigned NumTemplateArgs) {
2813 // <template-args> ::= I <template-arg>+ E
Benjamin Kramer1554e4a2010-04-10 16:03:31 +00002814 Out << 'I';
Daniel Dunbar88ad4c52009-11-21 09:17:15 +00002815 for (unsigned i = 0; i != NumTemplateArgs; ++i)
Rafael Espindola4d5c3d92010-03-11 14:07:00 +00002816 mangleTemplateArg(PL.getParam(i), TemplateArgs[i]);
Benjamin Kramer1554e4a2010-04-10 16:03:31 +00002817 Out << 'E';
Anders Carlsson7a8a74f2009-09-18 02:42:01 +00002818}
2819
Rafael Espindola4d5c3d92010-03-11 14:07:00 +00002820void CXXNameMangler::mangleTemplateArg(const NamedDecl *P,
2821 const TemplateArgument &A) {
Mike Stump11289f42009-09-09 15:08:12 +00002822 // <template-arg> ::= <type> # type or template
Anders Carlssonf6e9ece2009-05-15 16:09:15 +00002823 // ::= X <expression> E # expression
2824 // ::= <expr-primary> # simple expressions
Douglas Gregord81c7c12011-01-13 16:39:34 +00002825 // ::= J <template-arg>* E # argument pack
Anders Carlssonf6e9ece2009-05-15 16:09:15 +00002826 // ::= sp <expression> # pack expansion of (C++0x)
2827 switch (A.getKind()) {
Douglas Gregor752a5952011-01-03 22:36:02 +00002828 case TemplateArgument::Null:
2829 llvm_unreachable("Cannot mangle NULL template argument");
2830
Anders Carlssonf6e9ece2009-05-15 16:09:15 +00002831 case TemplateArgument::Type:
2832 mangleType(A.getAsType());
2833 break;
Anders Carlsson910847c2009-12-23 19:30:55 +00002834 case TemplateArgument::Template:
John McCall4f3b5f32010-07-14 06:43:17 +00002835 // This is mangled as <type>.
2836 mangleType(A.getAsTemplate());
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00002837 break;
Douglas Gregore4ff4b52011-01-05 18:58:31 +00002838 case TemplateArgument::TemplateExpansion:
Douglas Gregord81c7c12011-01-13 16:39:34 +00002839 // <type> ::= Dp <type> # pack expansion (C++0x)
Douglas Gregore4ff4b52011-01-05 18:58:31 +00002840 Out << "Dp";
2841 mangleType(A.getAsTemplateOrTemplatePattern());
2842 break;
Anders Carlssona18322c2009-09-21 01:21:10 +00002843 case TemplateArgument::Expression:
2844 Out << 'X';
2845 mangleExpression(A.getAsExpr());
2846 Out << 'E';
2847 break;
Anders Carlssone66e2942009-12-14 01:45:37 +00002848 case TemplateArgument::Integral:
2849 mangleIntegerLiteral(A.getIntegralType(), *A.getAsIntegral());
Anders Carlssonf6e9ece2009-05-15 16:09:15 +00002850 break;
Daniel Dunbar88ad4c52009-11-21 09:17:15 +00002851 case TemplateArgument::Declaration: {
Douglas Gregor17362712010-04-23 03:10:43 +00002852 assert(P && "Missing template parameter for declaration argument");
Daniel Dunbar88ad4c52009-11-21 09:17:15 +00002853 // <expr-primary> ::= L <mangled-name> E # external name
2854
Rafael Espindola4d5c3d92010-03-11 14:07:00 +00002855 // Clang produces AST's where pointer-to-member-function expressions
Daniel Dunbar88ad4c52009-11-21 09:17:15 +00002856 // and pointer-to-function expressions are represented as a declaration not
Rafael Espindola4d5c3d92010-03-11 14:07:00 +00002857 // an expression. We compensate for it here to produce the correct mangling.
2858 NamedDecl *D = cast<NamedDecl>(A.getAsDecl());
2859 const NonTypeTemplateParmDecl *Parameter = cast<NonTypeTemplateParmDecl>(P);
John McCall617339e2011-04-24 08:43:07 +00002860 bool compensateMangling = !Parameter->getType()->isReferenceType();
Rafael Espindola4d5c3d92010-03-11 14:07:00 +00002861 if (compensateMangling) {
2862 Out << 'X';
2863 mangleOperatorName(OO_Amp, 1);
2864 }
2865
Daniel Dunbar88ad4c52009-11-21 09:17:15 +00002866 Out << 'L';
2867 // References to external entities use the mangled name; if the name would
2868 // not normally be manged then mangle it as unqualified.
2869 //
2870 // FIXME: The ABI specifies that external names here should have _Z, but
2871 // gcc leaves this off.
Rafael Espindola4d5c3d92010-03-11 14:07:00 +00002872 if (compensateMangling)
2873 mangle(D, "_Z");
2874 else
2875 mangle(D, "Z");
Daniel Dunbar88ad4c52009-11-21 09:17:15 +00002876 Out << 'E';
Rafael Espindola4d5c3d92010-03-11 14:07:00 +00002877
2878 if (compensateMangling)
2879 Out << 'E';
2880
Daniel Dunbar88ad4c52009-11-21 09:17:15 +00002881 break;
2882 }
Douglas Gregor752a5952011-01-03 22:36:02 +00002883
2884 case TemplateArgument::Pack: {
2885 // Note: proposal by Mike Herrick on 12/20/10
2886 Out << 'J';
2887 for (TemplateArgument::pack_iterator PA = A.pack_begin(),
2888 PAEnd = A.pack_end();
2889 PA != PAEnd; ++PA)
2890 mangleTemplateArg(P, *PA);
2891 Out << 'E';
2892 }
Daniel Dunbar88ad4c52009-11-21 09:17:15 +00002893 }
Anders Carlssonf6e9ece2009-05-15 16:09:15 +00002894}
2895
Anders Carlssone00745b2009-09-27 00:38:53 +00002896void CXXNameMangler::mangleTemplateParameter(unsigned Index) {
2897 // <template-param> ::= T_ # first template parameter
2898 // ::= T <parameter-2 non-negative number> _
2899 if (Index == 0)
2900 Out << "T_";
2901 else
2902 Out << 'T' << (Index - 1) << '_';
2903}
2904
John McCall3a4a4c52011-07-01 00:04:39 +00002905void CXXNameMangler::mangleExistingSubstitution(QualType type) {
2906 bool result = mangleSubstitution(type);
2907 assert(result && "no existing substitution for type");
2908 (void) result;
2909}
2910
2911void CXXNameMangler::mangleExistingSubstitution(TemplateName tname) {
2912 bool result = mangleSubstitution(tname);
2913 assert(result && "no existing substitution for template name");
2914 (void) result;
2915}
2916
Anders Carlssonfeb60502009-09-17 00:43:46 +00002917// <substitution> ::= S <seq-id> _
2918// ::= S_
Anders Carlsson4245bf92009-09-17 04:16:28 +00002919bool CXXNameMangler::mangleSubstitution(const NamedDecl *ND) {
Anders Carlssonaa9e3c82009-09-26 20:53:44 +00002920 // Try one of the standard substitutions first.
2921 if (mangleStandardSubstitution(ND))
2922 return true;
Daniel Dunbard614e322009-11-21 09:05:47 +00002923
Anders Carlssonce214312009-11-07 04:26:04 +00002924 ND = cast<NamedDecl>(ND->getCanonicalDecl());
Anders Carlsson4245bf92009-09-17 04:16:28 +00002925 return mangleSubstitution(reinterpret_cast<uintptr_t>(ND));
2926}
2927
Anders Carlssonfeb60502009-09-17 00:43:46 +00002928bool CXXNameMangler::mangleSubstitution(QualType T) {
Anders Carlsson296f8dc2009-09-26 03:55:37 +00002929 if (!T.getCVRQualifiers()) {
2930 if (const RecordType *RT = T->getAs<RecordType>())
2931 return mangleSubstitution(RT->getDecl());
2932 }
Daniel Dunbard614e322009-11-21 09:05:47 +00002933
Anders Carlssonfeb60502009-09-17 00:43:46 +00002934 uintptr_t TypePtr = reinterpret_cast<uintptr_t>(T.getAsOpaquePtr());
2935
Anders Carlssona96f56f2009-09-17 03:53:28 +00002936 return mangleSubstitution(TypePtr);
2937}
2938
Douglas Gregor7988def2010-04-28 05:58:56 +00002939bool CXXNameMangler::mangleSubstitution(TemplateName Template) {
2940 if (TemplateDecl *TD = Template.getAsTemplateDecl())
2941 return mangleSubstitution(TD);
Alexis Hunta8136cc2010-05-05 15:23:54 +00002942
Douglas Gregor7988def2010-04-28 05:58:56 +00002943 Template = Context.getASTContext().getCanonicalTemplateName(Template);
2944 return mangleSubstitution(
2945 reinterpret_cast<uintptr_t>(Template.getAsVoidPointer()));
2946}
2947
Anders Carlssona96f56f2009-09-17 03:53:28 +00002948bool CXXNameMangler::mangleSubstitution(uintptr_t Ptr) {
Benjamin Kramer1554e4a2010-04-10 16:03:31 +00002949 llvm::DenseMap<uintptr_t, unsigned>::iterator I = Substitutions.find(Ptr);
Anders Carlssonfeb60502009-09-17 00:43:46 +00002950 if (I == Substitutions.end())
2951 return false;
Daniel Dunbard614e322009-11-21 09:05:47 +00002952
Anders Carlssonfeb60502009-09-17 00:43:46 +00002953 unsigned SeqID = I->second;
2954 if (SeqID == 0)
2955 Out << "S_";
2956 else {
2957 SeqID--;
Daniel Dunbard614e322009-11-21 09:05:47 +00002958
Anders Carlssonfeb60502009-09-17 00:43:46 +00002959 // <seq-id> is encoded in base-36, using digits and upper case letters.
2960 char Buffer[10];
Benjamin Kramer1554e4a2010-04-10 16:03:31 +00002961 char *BufferPtr = llvm::array_endof(Buffer);
Daniel Dunbard614e322009-11-21 09:05:47 +00002962
Anders Carlssonfeb60502009-09-17 00:43:46 +00002963 if (SeqID == 0) *--BufferPtr = '0';
Daniel Dunbard614e322009-11-21 09:05:47 +00002964
Anders Carlssonfeb60502009-09-17 00:43:46 +00002965 while (SeqID) {
2966 assert(BufferPtr > Buffer && "Buffer overflow!");
Daniel Dunbard614e322009-11-21 09:05:47 +00002967
John McCalla5dd3262010-06-09 07:26:17 +00002968 char c = static_cast<char>(SeqID % 36);
Daniel Dunbard614e322009-11-21 09:05:47 +00002969
Anders Carlssonfeb60502009-09-17 00:43:46 +00002970 *--BufferPtr = (c < 10 ? '0' + c : 'A' + c - 10);
2971 SeqID /= 36;
2972 }
Daniel Dunbard614e322009-11-21 09:05:47 +00002973
Benjamin Kramer1554e4a2010-04-10 16:03:31 +00002974 Out << 'S'
2975 << llvm::StringRef(BufferPtr, llvm::array_endof(Buffer)-BufferPtr)
2976 << '_';
Anders Carlssonfeb60502009-09-17 00:43:46 +00002977 }
Daniel Dunbard614e322009-11-21 09:05:47 +00002978
Anders Carlssonfeb60502009-09-17 00:43:46 +00002979 return true;
2980}
2981
Anders Carlsson2e593522009-09-27 00:12:57 +00002982static bool isCharType(QualType T) {
2983 if (T.isNull())
2984 return false;
Daniel Dunbard614e322009-11-21 09:05:47 +00002985
Anders Carlsson2e593522009-09-27 00:12:57 +00002986 return T->isSpecificBuiltinType(BuiltinType::Char_S) ||
2987 T->isSpecificBuiltinType(BuiltinType::Char_U);
2988}
2989
Daniel Dunbard614e322009-11-21 09:05:47 +00002990/// isCharSpecialization - Returns whether a given type is a template
Anders Carlsson2e593522009-09-27 00:12:57 +00002991/// specialization of a given name with a single argument of type char.
2992static bool isCharSpecialization(QualType T, const char *Name) {
2993 if (T.isNull())
2994 return false;
Daniel Dunbard614e322009-11-21 09:05:47 +00002995
Anders Carlsson2e593522009-09-27 00:12:57 +00002996 const RecordType *RT = T->getAs<RecordType>();
2997 if (!RT)
2998 return false;
Daniel Dunbard614e322009-11-21 09:05:47 +00002999
3000 const ClassTemplateSpecializationDecl *SD =
Anders Carlsson2e593522009-09-27 00:12:57 +00003001 dyn_cast<ClassTemplateSpecializationDecl>(RT->getDecl());
3002 if (!SD)
3003 return false;
3004
3005 if (!isStdNamespace(SD->getDeclContext()))
3006 return false;
Daniel Dunbard614e322009-11-21 09:05:47 +00003007
Anders Carlsson2e593522009-09-27 00:12:57 +00003008 const TemplateArgumentList &TemplateArgs = SD->getTemplateArgs();
3009 if (TemplateArgs.size() != 1)
3010 return false;
Daniel Dunbard614e322009-11-21 09:05:47 +00003011
Anders Carlsson2e593522009-09-27 00:12:57 +00003012 if (!isCharType(TemplateArgs[0].getAsType()))
3013 return false;
Daniel Dunbard614e322009-11-21 09:05:47 +00003014
Daniel Dunbar07d07852009-10-18 21:17:35 +00003015 return SD->getIdentifier()->getName() == Name;
Anders Carlsson2e593522009-09-27 00:12:57 +00003016}
3017
Anders Carlsson1aaecfa2009-12-07 19:56:42 +00003018template <std::size_t StrLen>
Benjamin Kramer90b5b682010-11-25 18:29:30 +00003019static bool isStreamCharSpecialization(const ClassTemplateSpecializationDecl*SD,
3020 const char (&Str)[StrLen]) {
Anders Carlsson1aaecfa2009-12-07 19:56:42 +00003021 if (!SD->getIdentifier()->isStr(Str))
3022 return false;
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00003023
Anders Carlsson1aaecfa2009-12-07 19:56:42 +00003024 const TemplateArgumentList &TemplateArgs = SD->getTemplateArgs();
3025 if (TemplateArgs.size() != 2)
3026 return false;
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00003027
Anders Carlsson1aaecfa2009-12-07 19:56:42 +00003028 if (!isCharType(TemplateArgs[0].getAsType()))
3029 return false;
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00003030
Anders Carlsson1aaecfa2009-12-07 19:56:42 +00003031 if (!isCharSpecialization(TemplateArgs[1].getAsType(), "char_traits"))
3032 return false;
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00003033
Anders Carlsson1aaecfa2009-12-07 19:56:42 +00003034 return true;
3035}
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00003036
Anders Carlssonaa9e3c82009-09-26 20:53:44 +00003037bool CXXNameMangler::mangleStandardSubstitution(const NamedDecl *ND) {
3038 // <substitution> ::= St # ::std::
Anders Carlsson872ce0d2009-09-26 23:10:05 +00003039 if (const NamespaceDecl *NS = dyn_cast<NamespaceDecl>(ND)) {
Anders Carlsson5c9e7b12009-12-04 06:23:23 +00003040 if (isStd(NS)) {
Anders Carlsson872ce0d2009-09-26 23:10:05 +00003041 Out << "St";
3042 return true;
3043 }
3044 }
3045
3046 if (const ClassTemplateDecl *TD = dyn_cast<ClassTemplateDecl>(ND)) {
3047 if (!isStdNamespace(TD->getDeclContext()))
3048 return false;
Daniel Dunbard614e322009-11-21 09:05:47 +00003049
Anders Carlsson872ce0d2009-09-26 23:10:05 +00003050 // <substitution> ::= Sa # ::std::allocator
3051 if (TD->getIdentifier()->isStr("allocator")) {
3052 Out << "Sa";
3053 return true;
3054 }
Daniel Dunbard614e322009-11-21 09:05:47 +00003055
Anders Carlsson853bb502009-09-26 23:14:39 +00003056 // <<substitution> ::= Sb # ::std::basic_string
3057 if (TD->getIdentifier()->isStr("basic_string")) {
3058 Out << "Sb";
3059 return true;
3060 }
Anders Carlsson872ce0d2009-09-26 23:10:05 +00003061 }
Daniel Dunbard614e322009-11-21 09:05:47 +00003062
3063 if (const ClassTemplateSpecializationDecl *SD =
Anders Carlsson2e593522009-09-27 00:12:57 +00003064 dyn_cast<ClassTemplateSpecializationDecl>(ND)) {
Eli Friedman1e6bc132010-02-23 18:25:09 +00003065 if (!isStdNamespace(SD->getDeclContext()))
3066 return false;
3067
Anders Carlsson2e593522009-09-27 00:12:57 +00003068 // <substitution> ::= Ss # ::std::basic_string<char,
3069 // ::std::char_traits<char>,
3070 // ::std::allocator<char> >
3071 if (SD->getIdentifier()->isStr("basic_string")) {
3072 const TemplateArgumentList &TemplateArgs = SD->getTemplateArgs();
Daniel Dunbard614e322009-11-21 09:05:47 +00003073
Anders Carlsson2e593522009-09-27 00:12:57 +00003074 if (TemplateArgs.size() != 3)
3075 return false;
Daniel Dunbard614e322009-11-21 09:05:47 +00003076
Anders Carlsson2e593522009-09-27 00:12:57 +00003077 if (!isCharType(TemplateArgs[0].getAsType()))
3078 return false;
Daniel Dunbard614e322009-11-21 09:05:47 +00003079
Anders Carlsson2e593522009-09-27 00:12:57 +00003080 if (!isCharSpecialization(TemplateArgs[1].getAsType(), "char_traits"))
3081 return false;
Daniel Dunbard614e322009-11-21 09:05:47 +00003082
Anders Carlsson2e593522009-09-27 00:12:57 +00003083 if (!isCharSpecialization(TemplateArgs[2].getAsType(), "allocator"))
3084 return false;
3085
3086 Out << "Ss";
3087 return true;
3088 }
Daniel Dunbard614e322009-11-21 09:05:47 +00003089
Anders Carlsson1aaecfa2009-12-07 19:56:42 +00003090 // <substitution> ::= Si # ::std::basic_istream<char,
3091 // ::std::char_traits<char> >
3092 if (isStreamCharSpecialization(SD, "basic_istream")) {
3093 Out << "Si";
3094 return true;
3095 }
3096
Daniel Dunbard614e322009-11-21 09:05:47 +00003097 // <substitution> ::= So # ::std::basic_ostream<char,
Anders Carlsson3482b812009-10-08 17:20:26 +00003098 // ::std::char_traits<char> >
Anders Carlsson1aaecfa2009-12-07 19:56:42 +00003099 if (isStreamCharSpecialization(SD, "basic_ostream")) {
Anders Carlsson3482b812009-10-08 17:20:26 +00003100 Out << "So";
3101 return true;
3102 }
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00003103
Anders Carlsson1aaecfa2009-12-07 19:56:42 +00003104 // <substitution> ::= Sd # ::std::basic_iostream<char,
3105 // ::std::char_traits<char> >
3106 if (isStreamCharSpecialization(SD, "basic_iostream")) {
3107 Out << "Sd";
3108 return true;
3109 }
Anders Carlsson2e593522009-09-27 00:12:57 +00003110 }
Anders Carlsson872ce0d2009-09-26 23:10:05 +00003111 return false;
Anders Carlssonaa9e3c82009-09-26 20:53:44 +00003112}
3113
Anders Carlssonfeb60502009-09-17 00:43:46 +00003114void CXXNameMangler::addSubstitution(QualType T) {
Anders Carlsson296f8dc2009-09-26 03:55:37 +00003115 if (!T.getCVRQualifiers()) {
3116 if (const RecordType *RT = T->getAs<RecordType>()) {
3117 addSubstitution(RT->getDecl());
3118 return;
3119 }
3120 }
Daniel Dunbard614e322009-11-21 09:05:47 +00003121
Anders Carlssonfeb60502009-09-17 00:43:46 +00003122 uintptr_t TypePtr = reinterpret_cast<uintptr_t>(T.getAsOpaquePtr());
Anders Carlssona96f56f2009-09-17 03:53:28 +00003123 addSubstitution(TypePtr);
3124}
3125
Douglas Gregor7988def2010-04-28 05:58:56 +00003126void CXXNameMangler::addSubstitution(TemplateName Template) {
3127 if (TemplateDecl *TD = Template.getAsTemplateDecl())
3128 return addSubstitution(TD);
Alexis Hunta8136cc2010-05-05 15:23:54 +00003129
Douglas Gregor7988def2010-04-28 05:58:56 +00003130 Template = Context.getASTContext().getCanonicalTemplateName(Template);
3131 addSubstitution(reinterpret_cast<uintptr_t>(Template.getAsVoidPointer()));
3132}
3133
Anders Carlssona96f56f2009-09-17 03:53:28 +00003134void CXXNameMangler::addSubstitution(uintptr_t Ptr) {
Anders Carlssona96f56f2009-09-17 03:53:28 +00003135 assert(!Substitutions.count(Ptr) && "Substitution already exists!");
Anders Carlssond5639232010-06-02 04:29:50 +00003136 Substitutions[Ptr] = SeqID++;
Anders Carlssonfeb60502009-09-17 00:43:46 +00003137}
3138
Daniel Dunbaref5d75a2009-11-21 09:06:10 +00003139//
Mike Stump11289f42009-09-09 15:08:12 +00003140
Daniel Dunbaref5d75a2009-11-21 09:06:10 +00003141/// \brief Mangles the name of the declaration D and emits that name to the
3142/// given output stream.
3143///
3144/// If the declaration D requires a mangled name, this routine will emit that
3145/// mangled name to \p os and return true. Otherwise, \p os will be unchanged
3146/// and this routine will return false. In this case, the caller should just
3147/// emit the identifier of the declaration (\c D->getIdentifier()) as its
3148/// name.
Peter Collingbourne0ff0b372011-01-13 18:57:25 +00003149void ItaniumMangleContext::mangleName(const NamedDecl *D,
Rafael Espindolaa759d722011-02-11 01:41:00 +00003150 llvm::raw_ostream &Out) {
Daniel Dunbar161ade42009-11-21 09:14:44 +00003151 assert((isa<FunctionDecl>(D) || isa<VarDecl>(D)) &&
3152 "Invalid mangleName() call, argument is not a variable or function!");
3153 assert(!isa<CXXConstructorDecl>(D) && !isa<CXXDestructorDecl>(D) &&
3154 "Invalid mangleName() call on 'structor decl!");
Daniel Dunbard614e322009-11-21 09:05:47 +00003155
Daniel Dunbaref5d75a2009-11-21 09:06:10 +00003156 PrettyStackTraceDecl CrashInfo(D, SourceLocation(),
3157 getASTContext().getSourceManager(),
3158 "Mangling declaration");
Mike Stump11289f42009-09-09 15:08:12 +00003159
John McCall8fb0d9d2011-05-01 22:35:37 +00003160 CXXNameMangler Mangler(*this, Out, D);
Daniel Dunbare128dd12009-11-21 09:06:22 +00003161 return Mangler.mangle(D);
Daniel Dunbaref5d75a2009-11-21 09:06:10 +00003162}
Mike Stump11289f42009-09-09 15:08:12 +00003163
Peter Collingbourne0ff0b372011-01-13 18:57:25 +00003164void ItaniumMangleContext::mangleCXXCtor(const CXXConstructorDecl *D,
3165 CXXCtorType Type,
Rafael Espindolaa759d722011-02-11 01:41:00 +00003166 llvm::raw_ostream &Out) {
Rafael Espindolaac00f5d2011-02-10 23:59:36 +00003167 CXXNameMangler Mangler(*this, Out, D, Type);
Daniel Dunbar4118ec52009-11-21 09:06:31 +00003168 Mangler.mangle(D);
Daniel Dunbaref5d75a2009-11-21 09:06:10 +00003169}
Mike Stump11289f42009-09-09 15:08:12 +00003170
Peter Collingbourne0ff0b372011-01-13 18:57:25 +00003171void ItaniumMangleContext::mangleCXXDtor(const CXXDestructorDecl *D,
3172 CXXDtorType Type,
Rafael Espindolaa759d722011-02-11 01:41:00 +00003173 llvm::raw_ostream &Out) {
Rafael Espindolaac00f5d2011-02-10 23:59:36 +00003174 CXXNameMangler Mangler(*this, Out, D, Type);
Daniel Dunbar4118ec52009-11-21 09:06:31 +00003175 Mangler.mangle(D);
Daniel Dunbaref5d75a2009-11-21 09:06:10 +00003176}
Mike Stumpbc78a722009-07-31 18:25:34 +00003177
Peter Collingbourne0ff0b372011-01-13 18:57:25 +00003178void ItaniumMangleContext::mangleThunk(const CXXMethodDecl *MD,
3179 const ThunkInfo &Thunk,
Rafael Espindola3968cd02011-02-11 02:52:17 +00003180 llvm::raw_ostream &Out) {
Anders Carlssoncd836f02010-03-23 17:17:29 +00003181 // <special-name> ::= T <call-offset> <base encoding>
3182 // # base is the nominal target function of thunk
3183 // <special-name> ::= Tc <call-offset> <call-offset> <base encoding>
3184 // # base is the nominal target function of thunk
3185 // # first call-offset is 'this' adjustment
3186 // # second call-offset is result adjustment
Alexis Hunta8136cc2010-05-05 15:23:54 +00003187
Anders Carlssoncd836f02010-03-23 17:17:29 +00003188 assert(!isa<CXXDestructorDecl>(MD) &&
3189 "Use mangleCXXDtor for destructor decls!");
Rafael Espindolaac00f5d2011-02-10 23:59:36 +00003190 CXXNameMangler Mangler(*this, Out);
Anders Carlssoncd836f02010-03-23 17:17:29 +00003191 Mangler.getStream() << "_ZT";
3192 if (!Thunk.Return.isEmpty())
3193 Mangler.getStream() << 'c';
Alexis Hunta8136cc2010-05-05 15:23:54 +00003194
Anders Carlssoncd836f02010-03-23 17:17:29 +00003195 // Mangle the 'this' pointer adjustment.
3196 Mangler.mangleCallOffset(Thunk.This.NonVirtual, Thunk.This.VCallOffsetOffset);
Alexis Hunta8136cc2010-05-05 15:23:54 +00003197
Anders Carlssoncd836f02010-03-23 17:17:29 +00003198 // Mangle the return pointer adjustment if there is one.
3199 if (!Thunk.Return.isEmpty())
3200 Mangler.mangleCallOffset(Thunk.Return.NonVirtual,
3201 Thunk.Return.VBaseOffsetOffset);
Alexis Hunta8136cc2010-05-05 15:23:54 +00003202
Anders Carlssoncd836f02010-03-23 17:17:29 +00003203 Mangler.mangleFunctionEncoding(MD);
3204}
3205
Alexis Hunta8136cc2010-05-05 15:23:54 +00003206void
Peter Collingbourne0ff0b372011-01-13 18:57:25 +00003207ItaniumMangleContext::mangleCXXDtorThunk(const CXXDestructorDecl *DD,
3208 CXXDtorType Type,
3209 const ThisAdjustment &ThisAdjustment,
Rafael Espindola3968cd02011-02-11 02:52:17 +00003210 llvm::raw_ostream &Out) {
Anders Carlssoncd836f02010-03-23 17:17:29 +00003211 // <special-name> ::= T <call-offset> <base encoding>
3212 // # base is the nominal target function of thunk
Rafael Espindolaac00f5d2011-02-10 23:59:36 +00003213 CXXNameMangler Mangler(*this, Out, DD, Type);
Anders Carlssoncd836f02010-03-23 17:17:29 +00003214 Mangler.getStream() << "_ZT";
3215
3216 // Mangle the 'this' pointer adjustment.
Alexis Hunta8136cc2010-05-05 15:23:54 +00003217 Mangler.mangleCallOffset(ThisAdjustment.NonVirtual,
Anders Carlssoncd836f02010-03-23 17:17:29 +00003218 ThisAdjustment.VCallOffsetOffset);
3219
3220 Mangler.mangleFunctionEncoding(DD);
3221}
3222
Daniel Dunbar8483d212009-11-21 09:12:13 +00003223/// mangleGuardVariable - Returns the mangled name for a guard variable
3224/// for the passed in VarDecl.
Peter Collingbourne0ff0b372011-01-13 18:57:25 +00003225void ItaniumMangleContext::mangleItaniumGuardVariable(const VarDecl *D,
Rafael Espindola3968cd02011-02-11 02:52:17 +00003226 llvm::raw_ostream &Out) {
Daniel Dunbar8483d212009-11-21 09:12:13 +00003227 // <special-name> ::= GV <object name> # Guard variable for one-time
3228 // # initialization
Rafael Espindolaac00f5d2011-02-10 23:59:36 +00003229 CXXNameMangler Mangler(*this, Out);
Daniel Dunbar8483d212009-11-21 09:12:13 +00003230 Mangler.getStream() << "_ZGV";
3231 Mangler.mangleName(D);
3232}
3233
Peter Collingbourne0ff0b372011-01-13 18:57:25 +00003234void ItaniumMangleContext::mangleReferenceTemporary(const VarDecl *D,
Rafael Espindola3968cd02011-02-11 02:52:17 +00003235 llvm::raw_ostream &Out) {
Anders Carlsson709ef8e2010-06-26 16:09:40 +00003236 // We match the GCC mangling here.
3237 // <special-name> ::= GR <object name>
Rafael Espindolaac00f5d2011-02-10 23:59:36 +00003238 CXXNameMangler Mangler(*this, Out);
Anders Carlsson709ef8e2010-06-26 16:09:40 +00003239 Mangler.getStream() << "_ZGR";
3240 Mangler.mangleName(D);
3241}
3242
Peter Collingbourne0ff0b372011-01-13 18:57:25 +00003243void ItaniumMangleContext::mangleCXXVTable(const CXXRecordDecl *RD,
Rafael Espindola3968cd02011-02-11 02:52:17 +00003244 llvm::raw_ostream &Out) {
Daniel Dunbar8483d212009-11-21 09:12:13 +00003245 // <special-name> ::= TV <type> # virtual table
Rafael Espindolaac00f5d2011-02-10 23:59:36 +00003246 CXXNameMangler Mangler(*this, Out);
Daniel Dunbar8483d212009-11-21 09:12:13 +00003247 Mangler.getStream() << "_ZTV";
Douglas Gregor0094eb22010-05-26 05:11:13 +00003248 Mangler.mangleNameOrStandardSubstitution(RD);
Daniel Dunbaref5d75a2009-11-21 09:06:10 +00003249}
Mike Stump3b917692009-11-10 01:58:37 +00003250
Peter Collingbourne0ff0b372011-01-13 18:57:25 +00003251void ItaniumMangleContext::mangleCXXVTT(const CXXRecordDecl *RD,
Rafael Espindola3968cd02011-02-11 02:52:17 +00003252 llvm::raw_ostream &Out) {
Daniel Dunbar8483d212009-11-21 09:12:13 +00003253 // <special-name> ::= TT <type> # VTT structure
Rafael Espindolaac00f5d2011-02-10 23:59:36 +00003254 CXXNameMangler Mangler(*this, Out);
Daniel Dunbar8483d212009-11-21 09:12:13 +00003255 Mangler.getStream() << "_ZTT";
Douglas Gregor0094eb22010-05-26 05:11:13 +00003256 Mangler.mangleNameOrStandardSubstitution(RD);
Daniel Dunbaref5d75a2009-11-21 09:06:10 +00003257}
Mike Stumpef157442009-11-10 01:41:59 +00003258
Peter Collingbourne0ff0b372011-01-13 18:57:25 +00003259void ItaniumMangleContext::mangleCXXCtorVTable(const CXXRecordDecl *RD,
3260 int64_t Offset,
3261 const CXXRecordDecl *Type,
Rafael Espindola3968cd02011-02-11 02:52:17 +00003262 llvm::raw_ostream &Out) {
Daniel Dunbar8483d212009-11-21 09:12:13 +00003263 // <special-name> ::= TC <type> <offset number> _ <base type>
Rafael Espindolaac00f5d2011-02-10 23:59:36 +00003264 CXXNameMangler Mangler(*this, Out);
Daniel Dunbar8483d212009-11-21 09:12:13 +00003265 Mangler.getStream() << "_ZTC";
Douglas Gregor0094eb22010-05-26 05:11:13 +00003266 Mangler.mangleNameOrStandardSubstitution(RD);
Daniel Dunbar8483d212009-11-21 09:12:13 +00003267 Mangler.getStream() << Offset;
Benjamin Kramer1554e4a2010-04-10 16:03:31 +00003268 Mangler.getStream() << '_';
Douglas Gregor0094eb22010-05-26 05:11:13 +00003269 Mangler.mangleNameOrStandardSubstitution(Type);
Daniel Dunbaref5d75a2009-11-21 09:06:10 +00003270}
Mike Stump183c3d22009-07-31 23:15:31 +00003271
Peter Collingbourne0ff0b372011-01-13 18:57:25 +00003272void ItaniumMangleContext::mangleCXXRTTI(QualType Ty,
Rafael Espindola3968cd02011-02-11 02:52:17 +00003273 llvm::raw_ostream &Out) {
Daniel Dunbar8483d212009-11-21 09:12:13 +00003274 // <special-name> ::= TI <type> # typeinfo structure
Douglas Gregor247894b2009-12-23 22:04:40 +00003275 assert(!Ty.hasQualifiers() && "RTTI info cannot have top-level qualifiers");
Rafael Espindolaac00f5d2011-02-10 23:59:36 +00003276 CXXNameMangler Mangler(*this, Out);
Daniel Dunbar8483d212009-11-21 09:12:13 +00003277 Mangler.getStream() << "_ZTI";
3278 Mangler.mangleType(Ty);
Daniel Dunbaref5d75a2009-11-21 09:06:10 +00003279}
Mike Stump2ec5dd72009-11-14 00:14:13 +00003280
Peter Collingbourne0ff0b372011-01-13 18:57:25 +00003281void ItaniumMangleContext::mangleCXXRTTIName(QualType Ty,
Rafael Espindola3968cd02011-02-11 02:52:17 +00003282 llvm::raw_ostream &Out) {
Daniel Dunbar8483d212009-11-21 09:12:13 +00003283 // <special-name> ::= TS <type> # typeinfo name (null terminated byte string)
Rafael Espindolaac00f5d2011-02-10 23:59:36 +00003284 CXXNameMangler Mangler(*this, Out);
Daniel Dunbar8483d212009-11-21 09:12:13 +00003285 Mangler.getStream() << "_ZTS";
3286 Mangler.mangleType(Ty);
Mike Stumpbc78a722009-07-31 18:25:34 +00003287}
Peter Collingbourne0ff0b372011-01-13 18:57:25 +00003288
3289MangleContext *clang::createItaniumMangleContext(ASTContext &Context,
3290 Diagnostic &Diags) {
3291 return new ItaniumMangleContext(Context, Diags);
3292}