blob: a77fe5f48ab1cfef3e72c78c8af60aa62d2a6495 [file] [log] [blame]
Peter Collingbourne14110472011-01-13 18:57:25 +00001//===--- ItaniumMangle.cpp - Itanium C++ Name Mangling ----------*- C++ -*-===//
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// Implements C++ name mangling according to the Itanium C++ ABI,
11// which is used in GCC 3.2 and newer (and many compilers that are
12// ABI-compatible with GCC):
13//
14// http://www.codesourcery.com/public/cxx-abi/abi.html
15//
16//===----------------------------------------------------------------------===//
Peter Collingbourne14110472011-01-13 18:57:25 +000017#include "clang/AST/Mangle.h"
Douglas Gregor5f2bfd42009-02-13 00:10:09 +000018#include "clang/AST/ASTContext.h"
19#include "clang/AST/Decl.h"
20#include "clang/AST/DeclCXX.h"
Anders Carlssona40c5e42009-03-07 22:03:21 +000021#include "clang/AST/DeclObjC.h"
Anders Carlsson7a0ba872009-05-15 16:09:15 +000022#include "clang/AST/DeclTemplate.h"
Anders Carlsson50755b02009-09-27 20:11:34 +000023#include "clang/AST/ExprCXX.h"
John McCallf85e1932011-06-15 23:02:42 +000024#include "clang/AST/ExprObjC.h"
John McCallfb44de92011-05-01 22:35:37 +000025#include "clang/AST/TypeLoc.h"
Peter Collingbourne14110472011-01-13 18:57:25 +000026#include "clang/Basic/ABI.h"
Douglas Gregor6ec36682009-02-18 23:53:56 +000027#include "clang/Basic/SourceManager.h"
Rafael Espindola4e274e92011-02-15 22:23:51 +000028#include "clang/Basic/TargetInfo.h"
Anders Carlssonc4355b62009-10-07 01:45:02 +000029#include "llvm/ADT/StringExtras.h"
Douglas Gregor5f2bfd42009-02-13 00:10:09 +000030#include "llvm/Support/raw_ostream.h"
John McCallefe6aee2009-09-05 07:56:18 +000031#include "llvm/Support/ErrorHandling.h"
Anders Carlssonf98574b2010-02-05 07:31:37 +000032
33#define MANGLE_CHECKER 0
34
35#if MANGLE_CHECKER
36#include <cxxabi.h>
37#endif
38
Douglas Gregor5f2bfd42009-02-13 00:10:09 +000039using namespace clang;
Charles Davis685b1d92010-05-26 18:25:27 +000040
Douglas Gregor5f2bfd42009-02-13 00:10:09 +000041namespace {
Fariborz Jahanian57058532010-03-03 19:41:08 +000042
John McCall82b7d7b2010-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 Jahanian57058532010-03-03 19:41:08 +000051 }
52 return 0;
53}
54
John McCallfb44de92011-05-01 22:35:37 +000055static const FunctionDecl *getStructor(const FunctionDecl *fn) {
56 if (const FunctionTemplateDecl *ftd = fn->getPrimaryTemplate())
57 return ftd->getTemplatedDecl();
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +000058
John McCallfb44de92011-05-01 22:35:37 +000059 return fn;
60}
Anders Carlsson7e120032009-11-24 05:36:32 +000061
John McCallfb44de92011-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 Carlsson7e120032009-11-24 05:36:32 +000065}
John McCall1dd73832010-02-04 01:42:13 +000066
67static const unsigned UnknownArity = ~0U;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +000068
Peter Collingbourne14110472011-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 Espindola0e376a02011-02-11 01:41:00 +000095 void mangleName(const NamedDecl *D, llvm::raw_ostream &);
Peter Collingbourne14110472011-01-13 18:57:25 +000096 void mangleThunk(const CXXMethodDecl *MD,
97 const ThunkInfo &Thunk,
Rafael Espindolaf0be9792011-02-11 02:52:17 +000098 llvm::raw_ostream &);
Peter Collingbourne14110472011-01-13 18:57:25 +000099 void mangleCXXDtorThunk(const CXXDestructorDecl *DD, CXXDtorType Type,
100 const ThisAdjustment &ThisAdjustment,
Rafael Espindolaf0be9792011-02-11 02:52:17 +0000101 llvm::raw_ostream &);
Peter Collingbourne14110472011-01-13 18:57:25 +0000102 void mangleReferenceTemporary(const VarDecl *D,
Rafael Espindolaf0be9792011-02-11 02:52:17 +0000103 llvm::raw_ostream &);
Peter Collingbourne14110472011-01-13 18:57:25 +0000104 void mangleCXXVTable(const CXXRecordDecl *RD,
Rafael Espindolaf0be9792011-02-11 02:52:17 +0000105 llvm::raw_ostream &);
Peter Collingbourne14110472011-01-13 18:57:25 +0000106 void mangleCXXVTT(const CXXRecordDecl *RD,
Rafael Espindolaf0be9792011-02-11 02:52:17 +0000107 llvm::raw_ostream &);
Peter Collingbourne14110472011-01-13 18:57:25 +0000108 void mangleCXXCtorVTable(const CXXRecordDecl *RD, int64_t Offset,
109 const CXXRecordDecl *Type,
Rafael Espindolaf0be9792011-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 Collingbourne14110472011-01-13 18:57:25 +0000113 void mangleCXXCtor(const CXXConstructorDecl *D, CXXCtorType Type,
Rafael Espindola0e376a02011-02-11 01:41:00 +0000114 llvm::raw_ostream &);
Peter Collingbourne14110472011-01-13 18:57:25 +0000115 void mangleCXXDtor(const CXXDestructorDecl *D, CXXDtorType Type,
Rafael Espindola0e376a02011-02-11 01:41:00 +0000116 llvm::raw_ostream &);
Peter Collingbourne14110472011-01-13 18:57:25 +0000117
Rafael Espindolaf0be9792011-02-11 02:52:17 +0000118 void mangleItaniumGuardVariable(const VarDecl *D, llvm::raw_ostream &);
Peter Collingbourne14110472011-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 Dunbar1b077112009-11-21 09:06:10 +0000136/// CXXNameMangler - Manage the mangling of a single name.
Daniel Dunbarc0747712009-11-21 09:12:13 +0000137class CXXNameMangler {
Peter Collingbourne14110472011-01-13 18:57:25 +0000138 ItaniumMangleContext &Context;
Rafael Espindola0e376a02011-02-11 01:41:00 +0000139 llvm::raw_ostream &Out;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000140
John McCallfb44de92011-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 Dunbar1b077112009-11-21 09:06:10 +0000145 unsigned StructorType;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000146
Anders Carlsson9d85b722010-06-02 04:29:50 +0000147 /// SeqID - The next subsitution sequence number.
148 unsigned SeqID;
149
John McCallfb44de92011-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 Dunbar1b077112009-11-21 09:06:10 +0000189 llvm::DenseMap<uintptr_t, unsigned> Substitutions;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000190
John McCall1dd73832010-02-04 01:42:13 +0000191 ASTContext &getASTContext() const { return Context.getASTContext(); }
192
Daniel Dunbarc0747712009-11-21 09:12:13 +0000193public:
John McCallfb44de92011-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 Espindola0e376a02011-02-11 01:41:00 +0000202 CXXNameMangler(ItaniumMangleContext &C, llvm::raw_ostream &Out_,
Daniel Dunbar77939c92009-11-21 09:06:31 +0000203 const CXXConstructorDecl *D, CXXCtorType Type)
Rafael Espindolac4850c22011-02-10 23:59:36 +0000204 : Context(C), Out(Out_), Structor(getStructor(D)), StructorType(Type),
John McCallfb44de92011-05-01 22:35:37 +0000205 SeqID(0) { }
Rafael Espindola0e376a02011-02-11 01:41:00 +0000206 CXXNameMangler(ItaniumMangleContext &C, llvm::raw_ostream &Out_,
Daniel Dunbar77939c92009-11-21 09:06:31 +0000207 const CXXDestructorDecl *D, CXXDtorType Type)
Rafael Espindolac4850c22011-02-10 23:59:36 +0000208 : Context(C), Out(Out_), Structor(getStructor(D)), StructorType(Type),
John McCallfb44de92011-05-01 22:35:37 +0000209 SeqID(0) { }
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000210
Anders Carlssonf98574b2010-02-05 07:31:37 +0000211#if MANGLE_CHECKER
212 ~CXXNameMangler() {
213 if (Out.str()[0] == '\01')
214 return;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000215
Anders Carlssonf98574b2010-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 Espindola0e376a02011-02-11 01:41:00 +0000222 llvm::raw_ostream &getStream() { return Out; }
Daniel Dunbarc0747712009-11-21 09:12:13 +0000223
Daniel Dunbar7e0c1952009-11-21 09:17:15 +0000224 void mangle(const NamedDecl *D, llvm::StringRef Prefix = "_Z");
Anders Carlsson19879c92010-03-23 17:17:29 +0000225 void mangleCallOffset(int64_t NonVirtual, int64_t Virtual);
John McCall0512e482010-07-14 04:20:34 +0000226 void mangleNumber(const llvm::APSInt &I);
Anders Carlssona94822e2009-11-26 02:32:05 +0000227 void mangleNumber(int64_t Number);
John McCall0512e482010-07-14 04:20:34 +0000228 void mangleFloat(const llvm::APFloat &F);
Daniel Dunbarc0747712009-11-21 09:12:13 +0000229 void mangleFunctionEncoding(const FunctionDecl *FD);
230 void mangleName(const NamedDecl *ND);
231 void mangleType(QualType T);
Douglas Gregor1b12a3b2010-05-26 05:11:13 +0000232 void mangleNameOrStandardSubstitution(const NamedDecl *ND);
233
Daniel Dunbarc0747712009-11-21 09:12:13 +0000234private:
Daniel Dunbar1b077112009-11-21 09:06:10 +0000235 bool mangleSubstitution(const NamedDecl *ND);
236 bool mangleSubstitution(QualType T);
Douglas Gregor1e9268e2010-04-28 05:58:56 +0000237 bool mangleSubstitution(TemplateName Template);
Daniel Dunbar1b077112009-11-21 09:06:10 +0000238 bool mangleSubstitution(uintptr_t Ptr);
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000239
Daniel Dunbar1b077112009-11-21 09:06:10 +0000240 bool mangleStandardSubstitution(const NamedDecl *ND);
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000241
Daniel Dunbar1b077112009-11-21 09:06:10 +0000242 void addSubstitution(const NamedDecl *ND) {
243 ND = cast<NamedDecl>(ND->getCanonicalDecl());
Anders Carlsson433d1372009-11-07 04:26:04 +0000244
Daniel Dunbar1b077112009-11-21 09:06:10 +0000245 addSubstitution(reinterpret_cast<uintptr_t>(ND));
246 }
247 void addSubstitution(QualType T);
Douglas Gregor1e9268e2010-04-28 05:58:56 +0000248 void addSubstitution(TemplateName Template);
Daniel Dunbar1b077112009-11-21 09:06:10 +0000249 void addSubstitution(uintptr_t Ptr);
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000250
John McCalla0ce15c2011-04-24 08:23:24 +0000251 void mangleUnresolvedPrefix(NestedNameSpecifier *qualifier,
252 NamedDecl *firstQualifierLookup,
253 bool recursive = false);
254 void mangleUnresolvedName(NestedNameSpecifier *qualifier,
255 NamedDecl *firstQualifierLookup,
256 DeclarationName name,
John McCall1dd73832010-02-04 01:42:13 +0000257 unsigned KnownArity = UnknownArity);
258
John McCall4f4e4132011-05-04 01:45:19 +0000259 static bool isUnresolvedType(const Type *type);
260 void mangleUnresolvedType(const Type *type);
John McCalla0ce15c2011-04-24 08:23:24 +0000261
Daniel Dunbar1b077112009-11-21 09:06:10 +0000262 void mangleName(const TemplateDecl *TD,
263 const TemplateArgument *TemplateArgs,
264 unsigned NumTemplateArgs);
John McCall1dd73832010-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 Dunbar1b077112009-11-21 09:06:10 +0000270 void mangleUnscopedName(const NamedDecl *ND);
271 void mangleUnscopedTemplateName(const TemplateDecl *ND);
Douglas Gregor1e9268e2010-04-28 05:58:56 +0000272 void mangleUnscopedTemplateName(TemplateName);
Daniel Dunbar1b077112009-11-21 09:06:10 +0000273 void mangleSourceName(const IdentifierInfo *II);
274 void mangleLocalName(const NamedDecl *ND);
Fariborz Jahanian57058532010-03-03 19:41:08 +0000275 void mangleNestedName(const NamedDecl *ND, const DeclContext *DC,
276 bool NoFunction=false);
Daniel Dunbar1b077112009-11-21 09:06:10 +0000277 void mangleNestedName(const TemplateDecl *TD,
278 const TemplateArgument *TemplateArgs,
279 unsigned NumTemplateArgs);
John McCalla0ce15c2011-04-24 08:23:24 +0000280 void manglePrefix(NestedNameSpecifier *qualifier);
Fariborz Jahanian57058532010-03-03 19:41:08 +0000281 void manglePrefix(const DeclContext *DC, bool NoFunction=false);
John McCall4f4e4132011-05-04 01:45:19 +0000282 void manglePrefix(QualType type);
Daniel Dunbar1b077112009-11-21 09:06:10 +0000283 void mangleTemplatePrefix(const TemplateDecl *ND);
Douglas Gregor20f0cc72010-04-23 03:10:43 +0000284 void mangleTemplatePrefix(TemplateName Template);
Daniel Dunbar1b077112009-11-21 09:06:10 +0000285 void mangleOperatorName(OverloadedOperatorKind OO, unsigned Arity);
286 void mangleQualifiers(Qualifiers Quals);
Douglas Gregor0a9a6d62011-01-26 17:36:28 +0000287 void mangleRefQualifier(RefQualifierKind RefQualifier);
John McCallefe6aee2009-09-05 07:56:18 +0000288
Anders Carlsson7b06f6c2009-12-10 03:14:39 +0000289 void mangleObjCMethodName(const ObjCMethodDecl *MD);
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000290
Daniel Dunbar1b077112009-11-21 09:06:10 +0000291 // Declare manglers for every type class.
John McCallefe6aee2009-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 Dunbar1b077112009-11-21 09:06:10 +0000297 void mangleType(const TagType*);
John McCallb6f532e2010-07-14 06:43:17 +0000298 void mangleType(TemplateName);
Daniel Dunbar1b077112009-11-21 09:06:10 +0000299 void mangleBareFunctionType(const FunctionType *T,
300 bool MangleReturnType);
Bob Wilson57147a82010-11-16 00:32:18 +0000301 void mangleNeonVectorType(const VectorType *T);
Anders Carlssone170ba72009-12-14 01:45:37 +0000302
303 void mangleIntegerLiteral(QualType T, const llvm::APSInt &Value);
John McCalla0ce15c2011-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 McCall5e1e89b2010-08-18 19:18:59 +0000309 void mangleExpression(const Expr *E, unsigned Arity = UnknownArity);
Daniel Dunbar1b077112009-11-21 09:06:10 +0000310 void mangleCXXCtorType(CXXCtorType T);
311 void mangleCXXDtorType(CXXDtorType T);
Mike Stump1eb44332009-09-09 15:08:12 +0000312
John McCall6dbce192010-08-20 00:17:19 +0000313 void mangleTemplateArgs(const ExplicitTemplateArgumentList &TemplateArgs);
Douglas Gregor20f0cc72010-04-23 03:10:43 +0000314 void mangleTemplateArgs(TemplateName Template,
315 const TemplateArgument *TemplateArgs,
Sean Huntc3021132010-05-05 15:23:54 +0000316 unsigned NumTemplateArgs);
Rafael Espindolad9800722010-03-11 14:07:00 +0000317 void mangleTemplateArgs(const TemplateParameterList &PL,
318 const TemplateArgument *TemplateArgs,
Daniel Dunbar1b077112009-11-21 09:06:10 +0000319 unsigned NumTemplateArgs);
Rafael Espindolad9800722010-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 McCall4f4e4132011-05-04 01:45:19 +0000323 void mangleUnresolvedTemplateArgs(const TemplateArgument *args,
324 unsigned numArgs);
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000325
Daniel Dunbar1b077112009-11-21 09:06:10 +0000326 void mangleTemplateParameter(unsigned Index);
John McCallfb44de92011-05-01 22:35:37 +0000327
328 void mangleFunctionParam(const ParmVarDecl *parm);
Daniel Dunbar1b077112009-11-21 09:06:10 +0000329};
Peter Collingbourne14110472011-01-13 18:57:25 +0000330
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000331}
332
Anders Carlsson43f17402009-04-02 15:51:53 +0000333static bool isInCLinkageSpecification(const Decl *D) {
Douglas Gregor457e2812009-10-28 16:31:34 +0000334 D = D->getCanonicalDecl();
Mike Stump1eb44332009-09-09 15:08:12 +0000335 for (const DeclContext *DC = D->getDeclContext();
Anders Carlsson43f17402009-04-02 15:51:53 +0000336 !DC->isTranslationUnit(); DC = DC->getParent()) {
Mike Stump1eb44332009-09-09 15:08:12 +0000337 if (const LinkageSpecDecl *Linkage = dyn_cast<LinkageSpecDecl>(DC))
Anders Carlsson43f17402009-04-02 15:51:53 +0000338 return Linkage->getLanguage() == LinkageSpecDecl::lang_c;
339 }
Mike Stump1eb44332009-09-09 15:08:12 +0000340
Anders Carlsson43f17402009-04-02 15:51:53 +0000341 return false;
342}
343
Peter Collingbourne14110472011-01-13 18:57:25 +0000344bool ItaniumMangleContext::shouldMangleDeclName(const NamedDecl *D) {
Daniel Dunbarf981bf82009-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 Stump141c5af2009-09-02 00:25:38 +0000354 // Clang's "overloadable" attribute extension to C/C++ implies name mangling
Anders Carlssona1e16222009-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 Dunbarf981bf82009-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 Stump1eb44332009-09-09 15:08:12 +0000361
Daniel Dunbarf981bf82009-11-21 09:14:52 +0000362 // Otherwise, no mangling is done outside C++ mode.
363 if (!getASTContext().getLangOptions().CPlusPlus)
364 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000365
Sean Hunt31455252010-01-24 03:04:27 +0000366 // Variables at global scope with non-internal linkage are not mangled
Eli Friedman7facf842009-12-02 20:32:49 +0000367 if (!FD) {
368 const DeclContext *DC = D->getDeclContext();
369 // Check for extern variable declared locally.
Fariborz Jahaniane81c5612010-06-30 18:57:21 +0000370 if (DC->isFunctionOrMethod() && D->hasLinkage())
Eli Friedman7facf842009-12-02 20:32:49 +0000371 while (!DC->isNamespace() && !DC->isTranslationUnit())
372 DC = DC->getParent();
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000373 if (DC->isTranslationUnit() && D->getLinkage() != InternalLinkage)
Eli Friedman7facf842009-12-02 20:32:49 +0000374 return false;
375 }
376
Eli Friedmanc00cb642010-07-18 20:49:59 +0000377 // Class members are always mangled.
378 if (D->getDeclContext()->isRecord())
379 return true;
380
Eli Friedman7facf842009-12-02 20:32:49 +0000381 // C functions and "main" are not mangled.
382 if ((FD && FD->isMain()) || isInCLinkageSpecification(D))
Daniel Dunbarf981bf82009-11-21 09:14:52 +0000383 return false;
384
Anders Carlsson43f17402009-04-02 15:51:53 +0000385 return true;
386}
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000387
Daniel Dunbar7e0c1952009-11-21 09:17:15 +0000388void CXXNameMangler::mangle(const NamedDecl *D, llvm::StringRef Prefix) {
Mike Stump141c5af2009-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 Kyrtzidis40b598e2009-06-30 02:34:44 +0000391 if (const AsmLabelAttr *ALA = D->getAttr<AsmLabelAttr>()) {
Chris Lattnerca3f25c2009-03-21 08:24:40 +0000392 // If we have an asm name, then we use it as the mangling.
Rafael Espindola4e274e92011-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 Collingbourne69317432011-04-06 12:29:09 +0000398 // marker. We also avoid adding the marker if this is an alias for an
399 // LLVM intrinsic.
Rafael Espindola4e274e92011-02-15 22:23:51 +0000400 llvm::StringRef UserLabelPrefix =
401 getASTContext().Target.getUserLabelPrefix();
Peter Collingbourne69317432011-04-06 12:29:09 +0000402 if (!UserLabelPrefix.empty() && !ALA->getLabel().startswith("llvm."))
Rafael Espindola4e274e92011-02-15 22:23:51 +0000403 Out << '\01'; // LLVM IR Marker for __asm("foo")
404
Chris Lattnerca3f25c2009-03-21 08:24:40 +0000405 Out << ALA->getLabel();
Daniel Dunbarf981bf82009-11-21 09:14:52 +0000406 return;
Chris Lattnerca3f25c2009-03-21 08:24:40 +0000407 }
Mike Stump1eb44332009-09-09 15:08:12 +0000408
Sean Hunt31455252010-01-24 03:04:27 +0000409 // <mangled-name> ::= _Z <encoding>
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000410 // ::= <data name>
411 // ::= <special-name>
Daniel Dunbar7e0c1952009-11-21 09:17:15 +0000412 Out << Prefix;
413 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
Daniel Dunbarf981bf82009-11-21 09:14:52 +0000414 mangleFunctionEncoding(FD);
Rafael Espindolad9800722010-03-11 14:07:00 +0000415 else if (const VarDecl *VD = dyn_cast<VarDecl>(D))
416 mangleName(VD);
Daniel Dunbar7e0c1952009-11-21 09:17:15 +0000417 else
Rafael Espindolad9800722010-03-11 14:07:00 +0000418 mangleName(cast<FieldDecl>(D));
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000419}
420
421void CXXNameMangler::mangleFunctionEncoding(const FunctionDecl *FD) {
422 // <encoding> ::= <function name> <bare-function-type>
423 mangleName(FD);
Mike Stump1eb44332009-09-09 15:08:12 +0000424
Daniel Dunbar7e0c1952009-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 Stump141c5af2009-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 Stump1eb44332009-09-09 15:08:12 +0000432 //
Douglas Gregor1fd2dd12009-06-29 22:39:32 +0000433 // 1. Template functions (names or types) have return types encoded, with
434 // the exceptions listed below.
Mike Stump1eb44332009-09-09 15:08:12 +0000435 // 2. Function types not appearing as part of a function name mangling,
Douglas Gregor1fd2dd12009-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 Stump141c5af2009-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 Gregor1fd2dd12009-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 Carlsson9234b7f2009-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 Dunbar3c9e4632009-11-21 09:05:47 +0000450
Anders Carlsson9234b7f2009-09-17 03:46:43 +0000451 // Mangle the type of the primary template.
452 FD = PrimaryTemplate->getTemplatedDecl();
453 }
454
John McCall54e14c42009-10-22 22:37:11 +0000455 // Do the canonicalization out here because parameter types can
456 // undergo additional canonicalization (e.g. array decay).
John McCallf4c73712011-01-19 06:33:43 +0000457 const FunctionType *FT
458 = cast<FunctionType>(Context.getASTContext()
John McCall54e14c42009-10-22 22:37:11 +0000459 .getCanonicalType(FD->getType()));
460
461 mangleBareFunctionType(FT, MangleReturnType);
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000462}
463
Anders Carlsson47846d22009-12-04 06:23:23 +0000464static const DeclContext *IgnoreLinkageSpecDecls(const DeclContext *DC) {
465 while (isa<LinkageSpecDecl>(DC)) {
Anders Carlsson47846d22009-12-04 06:23:23 +0000466 DC = DC->getParent();
467 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000468
Anders Carlsson47846d22009-12-04 06:23:23 +0000469 return DC;
470}
471
Anders Carlssonc820f902010-06-02 15:58:27 +0000472/// isStd - Return whether a given namespace is the 'std' namespace.
473static bool isStd(const NamespaceDecl *NS) {
474 if (!IgnoreLinkageSpecDecls(NS->getParent())->isTranslationUnit())
475 return false;
476
477 const IdentifierInfo *II = NS->getOriginalNamespace()->getIdentifier();
478 return II && II->isStr("std");
479}
480
Anders Carlsson47846d22009-12-04 06:23:23 +0000481// isStdNamespace - Return whether a given decl context is a toplevel 'std'
482// namespace.
Daniel Dunbar1308af92009-11-21 09:11:45 +0000483static bool isStdNamespace(const DeclContext *DC) {
Anders Carlsson47846d22009-12-04 06:23:23 +0000484 if (!DC->isNamespace())
485 return false;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000486
Anders Carlsson47846d22009-12-04 06:23:23 +0000487 return isStd(cast<NamespaceDecl>(DC));
Daniel Dunbar1308af92009-11-21 09:11:45 +0000488}
489
Anders Carlssonbb36ba42009-09-26 03:24:57 +0000490static const TemplateDecl *
491isTemplate(const NamedDecl *ND, const TemplateArgumentList *&TemplateArgs) {
Anders Carlsson2744a062009-09-18 19:00:18 +0000492 // Check if we have a function template.
493 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)){
Anders Carlssonbb36ba42009-09-26 03:24:57 +0000494 if (const TemplateDecl *TD = FD->getPrimaryTemplate()) {
Anders Carlsson2744a062009-09-18 19:00:18 +0000495 TemplateArgs = FD->getTemplateSpecializationArgs();
Anders Carlssonbb36ba42009-09-26 03:24:57 +0000496 return TD;
Anders Carlsson2744a062009-09-18 19:00:18 +0000497 }
498 }
499
Anders Carlssoneafc6dc2009-09-18 19:44:50 +0000500 // Check if we have a class template.
501 if (const ClassTemplateSpecializationDecl *Spec =
502 dyn_cast<ClassTemplateSpecializationDecl>(ND)) {
503 TemplateArgs = &Spec->getTemplateArgs();
Anders Carlssonbb36ba42009-09-26 03:24:57 +0000504 return Spec->getSpecializedTemplate();
Anders Carlssoneafc6dc2009-09-18 19:44:50 +0000505 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000506
Anders Carlsson2744a062009-09-18 19:00:18 +0000507 return 0;
508}
509
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000510void CXXNameMangler::mangleName(const NamedDecl *ND) {
511 // <name> ::= <nested-name>
512 // ::= <unscoped-name>
513 // ::= <unscoped-template-name> <template-args>
Anders Carlsson201ce742009-09-17 03:17:01 +0000514 // ::= <local-name>
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000515 //
Anders Carlssond58d6f72009-09-17 16:12:20 +0000516 const DeclContext *DC = ND->getDeclContext();
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000517
Eli Friedman7facf842009-12-02 20:32:49 +0000518 // If this is an extern variable declared locally, the relevant DeclContext
519 // is that of the containing namespace, or the translation unit.
520 if (isa<FunctionDecl>(DC) && ND->hasLinkage())
521 while (!DC->isNamespace() && !DC->isTranslationUnit())
522 DC = DC->getParent();
John McCall82b7d7b2010-10-18 21:28:44 +0000523 else if (GetLocalClassDecl(ND)) {
524 mangleLocalName(ND);
525 return;
526 }
Eli Friedman7facf842009-12-02 20:32:49 +0000527
Anders Carlsson5cc58c62009-09-22 17:23:30 +0000528 while (isa<LinkageSpecDecl>(DC))
Anders Carlssond58d6f72009-09-17 16:12:20 +0000529 DC = DC->getParent();
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000530
Anders Carlssond58d6f72009-09-17 16:12:20 +0000531 if (DC->isTranslationUnit() || isStdNamespace(DC)) {
Anders Carlsson2744a062009-09-18 19:00:18 +0000532 // Check if we have a template.
533 const TemplateArgumentList *TemplateArgs = 0;
Anders Carlsson0fa6df42009-09-26 19:45:45 +0000534 if (const TemplateDecl *TD = isTemplate(ND, TemplateArgs)) {
Anders Carlsson2744a062009-09-18 19:00:18 +0000535 mangleUnscopedTemplateName(TD);
Rafael Espindolad9800722010-03-11 14:07:00 +0000536 TemplateParameterList *TemplateParameters = TD->getTemplateParameters();
537 mangleTemplateArgs(*TemplateParameters, *TemplateArgs);
Anders Carlsson2744a062009-09-18 19:00:18 +0000538 return;
Anders Carlsson7482e242009-09-18 04:29:09 +0000539 }
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000540
Anders Carlsson7482e242009-09-18 04:29:09 +0000541 mangleUnscopedName(ND);
542 return;
543 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000544
Anders Carlsson7b06f6c2009-12-10 03:14:39 +0000545 if (isa<FunctionDecl>(DC) || isa<ObjCMethodDecl>(DC)) {
Anders Carlsson7482e242009-09-18 04:29:09 +0000546 mangleLocalName(ND);
547 return;
548 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000549
Eli Friedman7facf842009-12-02 20:32:49 +0000550 mangleNestedName(ND, DC);
Anders Carlsson7482e242009-09-18 04:29:09 +0000551}
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000552void CXXNameMangler::mangleName(const TemplateDecl *TD,
Anders Carlsson7624f212009-09-18 02:42:01 +0000553 const TemplateArgument *TemplateArgs,
554 unsigned NumTemplateArgs) {
Anders Carlsson47846d22009-12-04 06:23:23 +0000555 const DeclContext *DC = IgnoreLinkageSpecDecls(TD->getDeclContext());
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000556
Anders Carlsson7624f212009-09-18 02:42:01 +0000557 if (DC->isTranslationUnit() || isStdNamespace(DC)) {
Anders Carlsson0fa6df42009-09-26 19:45:45 +0000558 mangleUnscopedTemplateName(TD);
Rafael Espindolad9800722010-03-11 14:07:00 +0000559 TemplateParameterList *TemplateParameters = TD->getTemplateParameters();
560 mangleTemplateArgs(*TemplateParameters, TemplateArgs, NumTemplateArgs);
Anders Carlsson7624f212009-09-18 02:42:01 +0000561 } else {
562 mangleNestedName(TD, TemplateArgs, NumTemplateArgs);
563 }
564}
565
Anders Carlsson201ce742009-09-17 03:17:01 +0000566void CXXNameMangler::mangleUnscopedName(const NamedDecl *ND) {
567 // <unscoped-name> ::= <unqualified-name>
568 // ::= St <unqualified-name> # ::std::
569 if (isStdNamespace(ND->getDeclContext()))
570 Out << "St";
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000571
Anders Carlsson201ce742009-09-17 03:17:01 +0000572 mangleUnqualifiedName(ND);
573}
574
Anders Carlsson0fa6df42009-09-26 19:45:45 +0000575void CXXNameMangler::mangleUnscopedTemplateName(const TemplateDecl *ND) {
Anders Carlsson201ce742009-09-17 03:17:01 +0000576 // <unscoped-template-name> ::= <unscoped-name>
577 // ::= <substitution>
Anders Carlsson7624f212009-09-18 02:42:01 +0000578 if (mangleSubstitution(ND))
Anders Carlsson03c9d532009-09-17 04:02:31 +0000579 return;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000580
Douglas Gregor32fb4e12010-02-05 20:45:00 +0000581 // <template-template-param> ::= <template-param>
582 if (const TemplateTemplateParmDecl *TTP
583 = dyn_cast<TemplateTemplateParmDecl>(ND)) {
584 mangleTemplateParameter(TTP->getIndex());
585 return;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000586 }
Douglas Gregor32fb4e12010-02-05 20:45:00 +0000587
Anders Carlsson1668f202009-09-26 20:13:56 +0000588 mangleUnscopedName(ND->getTemplatedDecl());
Anders Carlsson7624f212009-09-18 02:42:01 +0000589 addSubstitution(ND);
Anders Carlsson201ce742009-09-17 03:17:01 +0000590}
591
Douglas Gregor1e9268e2010-04-28 05:58:56 +0000592void CXXNameMangler::mangleUnscopedTemplateName(TemplateName Template) {
593 // <unscoped-template-name> ::= <unscoped-name>
594 // ::= <substitution>
595 if (TemplateDecl *TD = Template.getAsTemplateDecl())
596 return mangleUnscopedTemplateName(TD);
Sean Huntc3021132010-05-05 15:23:54 +0000597
Douglas Gregor1e9268e2010-04-28 05:58:56 +0000598 if (mangleSubstitution(Template))
599 return;
600
601 // FIXME: How to cope with operators here?
602 DependentTemplateName *Dependent = Template.getAsDependentTemplateName();
603 assert(Dependent && "Not a dependent template name?");
604 if (!Dependent->isIdentifier()) {
605 // FIXME: We can't possibly know the arity of the operator here!
606 Diagnostic &Diags = Context.getDiags();
607 unsigned DiagID = Diags.getCustomDiagID(Diagnostic::Error,
608 "cannot mangle dependent operator name");
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000609 Diags.Report(DiagID);
Douglas Gregor1e9268e2010-04-28 05:58:56 +0000610 return;
611 }
Sean Huntc3021132010-05-05 15:23:54 +0000612
Douglas Gregor1e9268e2010-04-28 05:58:56 +0000613 mangleSourceName(Dependent->getIdentifier());
614 addSubstitution(Template);
615}
616
John McCall1b600522011-04-24 03:07:16 +0000617void CXXNameMangler::mangleFloat(const llvm::APFloat &f) {
618 // ABI:
619 // Floating-point literals are encoded using a fixed-length
620 // lowercase hexadecimal string corresponding to the internal
621 // representation (IEEE on Itanium), high-order bytes first,
622 // without leading zeroes. For example: "Lf bf800000 E" is -1.0f
623 // on Itanium.
624 // APInt::toString uses uppercase hexadecimal, and it's not really
625 // worth embellishing that interface for this use case, so we just
626 // do a second pass to lowercase things.
627 typedef llvm::SmallString<20> buffer_t;
628 buffer_t buffer;
629 f.bitcastToAPInt().toString(buffer, 16, false);
630
631 for (buffer_t::iterator i = buffer.begin(), e = buffer.end(); i != e; ++i)
632 if (isupper(*i)) *i = tolower(*i);
633
634 Out.write(buffer.data(), buffer.size());
John McCall0512e482010-07-14 04:20:34 +0000635}
636
637void CXXNameMangler::mangleNumber(const llvm::APSInt &Value) {
638 if (Value.isSigned() && Value.isNegative()) {
639 Out << 'n';
640 Value.abs().print(Out, true);
641 } else
642 Value.print(Out, Value.isSigned());
643}
644
Anders Carlssona94822e2009-11-26 02:32:05 +0000645void CXXNameMangler::mangleNumber(int64_t Number) {
646 // <number> ::= [n] <non-negative decimal integer>
647 if (Number < 0) {
648 Out << 'n';
649 Number = -Number;
650 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000651
Anders Carlssona94822e2009-11-26 02:32:05 +0000652 Out << Number;
653}
654
Anders Carlsson19879c92010-03-23 17:17:29 +0000655void CXXNameMangler::mangleCallOffset(int64_t NonVirtual, int64_t Virtual) {
Mike Stump141c5af2009-09-02 00:25:38 +0000656 // <call-offset> ::= h <nv-offset> _
657 // ::= v <v-offset> _
658 // <nv-offset> ::= <offset number> # non-virtual base override
Anders Carlssona94822e2009-11-26 02:32:05 +0000659 // <v-offset> ::= <offset number> _ <virtual offset number>
Mike Stump141c5af2009-09-02 00:25:38 +0000660 // # virtual base override, with vcall offset
Anders Carlsson19879c92010-03-23 17:17:29 +0000661 if (!Virtual) {
Anders Carlssona94822e2009-11-26 02:32:05 +0000662 Out << 'h';
Anders Carlsson19879c92010-03-23 17:17:29 +0000663 mangleNumber(NonVirtual);
Anders Carlssona94822e2009-11-26 02:32:05 +0000664 Out << '_';
665 return;
Mike Stump141c5af2009-09-02 00:25:38 +0000666 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000667
Anders Carlssona94822e2009-11-26 02:32:05 +0000668 Out << 'v';
Anders Carlsson19879c92010-03-23 17:17:29 +0000669 mangleNumber(NonVirtual);
Anders Carlssona94822e2009-11-26 02:32:05 +0000670 Out << '_';
Anders Carlsson19879c92010-03-23 17:17:29 +0000671 mangleNumber(Virtual);
Anders Carlssona94822e2009-11-26 02:32:05 +0000672 Out << '_';
Mike Stump9124bcc2009-09-02 00:56:18 +0000673}
674
John McCall4f4e4132011-05-04 01:45:19 +0000675void CXXNameMangler::manglePrefix(QualType type) {
John McCalla0ce15c2011-04-24 08:23:24 +0000676 if (const TemplateSpecializationType *TST =
677 type->getAs<TemplateSpecializationType>()) {
678 if (!mangleSubstitution(QualType(TST, 0))) {
679 mangleTemplatePrefix(TST->getTemplateName());
Sean Huntc3021132010-05-05 15:23:54 +0000680
Douglas Gregoraa2187d2011-02-28 00:04:36 +0000681 // FIXME: GCC does not appear to mangle the template arguments when
682 // the template in question is a dependent template name. Should we
683 // emulate that badness?
John McCalla0ce15c2011-04-24 08:23:24 +0000684 mangleTemplateArgs(TST->getTemplateName(), TST->getArgs(),
685 TST->getNumArgs());
686 addSubstitution(QualType(TST, 0));
Rafael Espindola9b35b252010-03-17 04:28:11 +0000687 }
John McCalla0ce15c2011-04-24 08:23:24 +0000688 } else if (const DependentTemplateSpecializationType *DTST
689 = type->getAs<DependentTemplateSpecializationType>()) {
690 TemplateName Template
691 = getASTContext().getDependentTemplateName(DTST->getQualifier(),
692 DTST->getIdentifier());
693 mangleTemplatePrefix(Template);
694
695 // FIXME: GCC does not appear to mangle the template arguments when
696 // the template in question is a dependent template name. Should we
697 // emulate that badness?
698 mangleTemplateArgs(Template, DTST->getArgs(), DTST->getNumArgs());
699 } else {
700 // We use the QualType mangle type variant here because it handles
701 // substitutions.
702 mangleType(type);
John McCall1dd73832010-02-04 01:42:13 +0000703 }
704}
705
John McCall4f4e4132011-05-04 01:45:19 +0000706/// Returns true if the given type, appearing within an
707/// unresolved-name, should be mangled as an unresolved-type.
708bool CXXNameMangler::isUnresolvedType(const Type *type) {
709 // <unresolved-type> ::= <template-param>
710 // ::= <decltype>
711 // ::= <template-template-param> <template-args>
712 // (this last is not official yet)
713
714 if (isa<TemplateTypeParmType>(type)) return true;
715 if (isa<DecltypeType>(type)) return true;
716 // typeof?
717 if (const TemplateSpecializationType *tst =
718 dyn_cast<TemplateSpecializationType>(type)) {
719 TemplateDecl *temp = tst->getTemplateName().getAsTemplateDecl();
720 if (temp && isa<TemplateTemplateParmDecl>(temp))
721 return true;
722 }
723 return false;
724}
725
726void CXXNameMangler::mangleUnresolvedType(const Type *type) {
727 // This seems to be do everything we want.
728 mangleType(QualType(type, 0));
729}
730
John McCalla0ce15c2011-04-24 08:23:24 +0000731/// Mangle everything prior to the base-unresolved-name in an unresolved-name.
732///
733/// \param firstQualifierLookup - the entity found by unqualified lookup
734/// for the first name in the qualifier, if this is for a member expression
735/// \param recursive - true if this is being called recursively,
736/// i.e. if there is more prefix "to the right".
737void CXXNameMangler::mangleUnresolvedPrefix(NestedNameSpecifier *qualifier,
738 NamedDecl *firstQualifierLookup,
739 bool recursive) {
John McCall1dd73832010-02-04 01:42:13 +0000740
John McCalla0ce15c2011-04-24 08:23:24 +0000741 // x, ::x
742 // <unresolved-name> ::= [gs] <base-unresolved-name>
743
744 // T::x / decltype(p)::x
745 // <unresolved-name> ::= sr <unresolved-type> <base-unresolved-name>
746
747 // T::N::x /decltype(p)::N::x
748 // <unresolved-name> ::= srN <unresolved-type> <unresolved-qualifier-level>+ E
749 // <base-unresolved-name>
750
751 // A::x, N::y, A<T>::z; "gs" means leading "::"
752 // <unresolved-name> ::= [gs] sr <unresolved-qualifier-level>+ E
753 // <base-unresolved-name>
754
755 switch (qualifier->getKind()) {
756 case NestedNameSpecifier::Global:
757 Out << "gs";
758
759 // We want an 'sr' unless this is the entire NNS.
760 if (recursive)
761 Out << "sr";
762
763 // We never want an 'E' here.
764 return;
765
766 case NestedNameSpecifier::Namespace:
767 if (qualifier->getPrefix())
768 mangleUnresolvedPrefix(qualifier->getPrefix(), firstQualifierLookup,
769 /*recursive*/ true);
770 else
771 Out << "sr";
772 mangleSourceName(qualifier->getAsNamespace()->getIdentifier());
773 break;
774 case NestedNameSpecifier::NamespaceAlias:
775 if (qualifier->getPrefix())
776 mangleUnresolvedPrefix(qualifier->getPrefix(), firstQualifierLookup,
777 /*recursive*/ true);
778 else
779 Out << "sr";
780 mangleSourceName(qualifier->getAsNamespaceAlias()->getIdentifier());
781 break;
782
783 case NestedNameSpecifier::TypeSpec:
784 case NestedNameSpecifier::TypeSpecWithTemplate: {
John McCall4f4e4132011-05-04 01:45:19 +0000785 const Type *type = qualifier->getAsType();
John McCalla0ce15c2011-04-24 08:23:24 +0000786
John McCall4f4e4132011-05-04 01:45:19 +0000787 // We only want to use an unresolved-type encoding if this is one of:
788 // - a decltype
789 // - a template type parameter
790 // - a template template parameter with arguments
791 // In all of these cases, we should have no prefix.
792 if (qualifier->getPrefix()) {
793 mangleUnresolvedPrefix(qualifier->getPrefix(), firstQualifierLookup,
794 /*recursive*/ true);
795 } else {
796 // Otherwise, all the cases want this.
797 Out << "sr";
John McCalla0ce15c2011-04-24 08:23:24 +0000798
John McCall4f4e4132011-05-04 01:45:19 +0000799 if (isUnresolvedType(type)) {
800 // We only get here recursively if we're followed by identifiers.
801 if (recursive) Out << 'N';
802 mangleUnresolvedType(type);
John McCalla0ce15c2011-04-24 08:23:24 +0000803
John McCall4f4e4132011-05-04 01:45:19 +0000804 // We never want to print 'E' directly after an unresolved-type,
805 // so we return directly.
806 return;
807 }
808 }
809
810 assert(!isUnresolvedType(type));
811
812 // Only certain other types are valid as prefixes; enumerate them.
813 // FIXME: can we get ElaboratedTypes here?
814 // FIXME: SubstTemplateTypeParmType?
815 if (const TagType *t = dyn_cast<TagType>(type)) {
816 mangleSourceName(t->getDecl()->getIdentifier());
817 } else if (const TypedefType *t = dyn_cast<TypedefType>(type)) {
818 mangleSourceName(t->getDecl()->getIdentifier());
819 } else if (const UnresolvedUsingType *t
820 = dyn_cast<UnresolvedUsingType>(type)) {
821 mangleSourceName(t->getDecl()->getIdentifier());
822 } else if (const DependentNameType *t
823 = dyn_cast<DependentNameType>(type)) {
824 mangleSourceName(t->getIdentifier());
825 } else if (const TemplateSpecializationType *tst
826 = dyn_cast<TemplateSpecializationType>(type)) {
827 TemplateDecl *temp = tst->getTemplateName().getAsTemplateDecl();
828 assert(temp && "no template for template specialization type");
829 mangleSourceName(temp->getIdentifier());
830 mangleUnresolvedTemplateArgs(tst->getArgs(), tst->getNumArgs());
831 } else if (const DependentTemplateSpecializationType *tst
832 = dyn_cast<DependentTemplateSpecializationType>(type)) {
833 mangleSourceName(tst->getIdentifier());
834 mangleUnresolvedTemplateArgs(tst->getArgs(), tst->getNumArgs());
835 } else {
836 llvm_unreachable("unexpected type in nested name specifier!");
837 }
838 break;
John McCalla0ce15c2011-04-24 08:23:24 +0000839 }
840
841 case NestedNameSpecifier::Identifier:
842 // Member expressions can have these without prefixes.
843 if (qualifier->getPrefix()) {
844 mangleUnresolvedPrefix(qualifier->getPrefix(), firstQualifierLookup,
845 /*recursive*/ true);
846 } else if (firstQualifierLookup) {
847
848 // Try to make a proper qualifier out of the lookup result, and
849 // then just recurse on that.
850 NestedNameSpecifier *newQualifier;
851 if (TypeDecl *typeDecl = dyn_cast<TypeDecl>(firstQualifierLookup)) {
852 QualType type = getASTContext().getTypeDeclType(typeDecl);
853
854 // Pretend we had a different nested name specifier.
855 newQualifier = NestedNameSpecifier::Create(getASTContext(),
856 /*prefix*/ 0,
857 /*template*/ false,
858 type.getTypePtr());
859 } else if (NamespaceDecl *nspace =
860 dyn_cast<NamespaceDecl>(firstQualifierLookup)) {
861 newQualifier = NestedNameSpecifier::Create(getASTContext(),
862 /*prefix*/ 0,
863 nspace);
864 } else if (NamespaceAliasDecl *alias =
865 dyn_cast<NamespaceAliasDecl>(firstQualifierLookup)) {
866 newQualifier = NestedNameSpecifier::Create(getASTContext(),
867 /*prefix*/ 0,
868 alias);
869 } else {
870 // No sensible mangling to do here.
871 newQualifier = 0;
872 }
873
874 if (newQualifier)
875 return mangleUnresolvedPrefix(newQualifier, /*lookup*/ 0, recursive);
876
877 } else {
878 Out << "sr";
879 }
880
881 mangleSourceName(qualifier->getAsIdentifier());
882 break;
883 }
884
885 // If this was the innermost part of the NNS, and we fell out to
886 // here, append an 'E'.
887 if (!recursive)
888 Out << 'E';
889}
890
891/// Mangle an unresolved-name, which is generally used for names which
892/// weren't resolved to specific entities.
893void CXXNameMangler::mangleUnresolvedName(NestedNameSpecifier *qualifier,
894 NamedDecl *firstQualifierLookup,
895 DeclarationName name,
896 unsigned knownArity) {
897 if (qualifier) mangleUnresolvedPrefix(qualifier, firstQualifierLookup);
898 mangleUnqualifiedName(0, name, knownArity);
John McCall1dd73832010-02-04 01:42:13 +0000899}
900
Anders Carlsson6f7e2f42010-06-08 14:49:03 +0000901static const FieldDecl *FindFirstNamedDataMember(const RecordDecl *RD) {
902 assert(RD->isAnonymousStructOrUnion() &&
903 "Expected anonymous struct or union!");
904
905 for (RecordDecl::field_iterator I = RD->field_begin(), E = RD->field_end();
906 I != E; ++I) {
907 const FieldDecl *FD = *I;
908
909 if (FD->getIdentifier())
910 return FD;
911
912 if (const RecordType *RT = FD->getType()->getAs<RecordType>()) {
913 if (const FieldDecl *NamedDataMember =
914 FindFirstNamedDataMember(RT->getDecl()))
915 return NamedDataMember;
916 }
917 }
918
919 // We didn't find a named data member.
920 return 0;
921}
922
John McCall1dd73832010-02-04 01:42:13 +0000923void CXXNameMangler::mangleUnqualifiedName(const NamedDecl *ND,
924 DeclarationName Name,
925 unsigned KnownArity) {
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000926 // <unqualified-name> ::= <operator-name>
Mike Stump1eb44332009-09-09 15:08:12 +0000927 // ::= <ctor-dtor-name>
928 // ::= <source-name>
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000929 switch (Name.getNameKind()) {
Anders Carlssonc4355b62009-10-07 01:45:02 +0000930 case DeclarationName::Identifier: {
Anders Carlssonc4355b62009-10-07 01:45:02 +0000931 if (const IdentifierInfo *II = Name.getAsIdentifierInfo()) {
Sean Hunt31455252010-01-24 03:04:27 +0000932 // We must avoid conflicts between internally- and externally-
John McCall74990f42011-03-22 06:34:45 +0000933 // linked variable and function declaration names in the same TU:
934 // void test() { extern void foo(); }
935 // static void foo();
936 // This naming convention is the same as that followed by GCC,
937 // though it shouldn't actually matter.
938 if (ND && ND->getLinkage() == InternalLinkage &&
Sean Hunt31455252010-01-24 03:04:27 +0000939 ND->getDeclContext()->isFileContext())
940 Out << 'L';
941
Anders Carlssonc4355b62009-10-07 01:45:02 +0000942 mangleSourceName(II);
943 break;
944 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000945
John McCall1dd73832010-02-04 01:42:13 +0000946 // Otherwise, an anonymous entity. We must have a declaration.
947 assert(ND && "mangling empty name without declaration");
948
949 if (const NamespaceDecl *NS = dyn_cast<NamespaceDecl>(ND)) {
950 if (NS->isAnonymousNamespace()) {
951 // This is how gcc mangles these names.
952 Out << "12_GLOBAL__N_1";
953 break;
954 }
955 }
956
Anders Carlsson6f7e2f42010-06-08 14:49:03 +0000957 if (const VarDecl *VD = dyn_cast<VarDecl>(ND)) {
958 // We must have an anonymous union or struct declaration.
959 const RecordDecl *RD =
960 cast<RecordDecl>(VD->getType()->getAs<RecordType>()->getDecl());
961
962 // Itanium C++ ABI 5.1.2:
963 //
964 // For the purposes of mangling, the name of an anonymous union is
965 // considered to be the name of the first named data member found by a
966 // pre-order, depth-first, declaration-order walk of the data members of
967 // the anonymous union. If there is no such data member (i.e., if all of
968 // the data members in the union are unnamed), then there is no way for
969 // a program to refer to the anonymous union, and there is therefore no
970 // need to mangle its name.
971 const FieldDecl *FD = FindFirstNamedDataMember(RD);
John McCall7121c8f2010-08-05 22:02:13 +0000972
973 // It's actually possible for various reasons for us to get here
974 // with an empty anonymous struct / union. Fortunately, it
975 // doesn't really matter what name we generate.
976 if (!FD) break;
Anders Carlsson6f7e2f42010-06-08 14:49:03 +0000977 assert(FD->getIdentifier() && "Data member name isn't an identifier!");
978
979 mangleSourceName(FD->getIdentifier());
980 break;
981 }
982
Anders Carlssonc4355b62009-10-07 01:45:02 +0000983 // We must have an anonymous struct.
984 const TagDecl *TD = cast<TagDecl>(ND);
Richard Smith162e1c12011-04-15 14:24:37 +0000985 if (const TypedefNameDecl *D = TD->getTypedefNameForAnonDecl()) {
Anders Carlssonc4355b62009-10-07 01:45:02 +0000986 assert(TD->getDeclContext() == D->getDeclContext() &&
987 "Typedef should not be in another decl context!");
988 assert(D->getDeclName().getAsIdentifierInfo() &&
989 "Typedef was not named!");
990 mangleSourceName(D->getDeclName().getAsIdentifierInfo());
991 break;
992 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000993
Anders Carlssonc4355b62009-10-07 01:45:02 +0000994 // Get a unique id for the anonymous struct.
995 uint64_t AnonStructId = Context.getAnonymousStructId(TD);
996
997 // Mangle it as a source name in the form
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000998 // [n] $_<id>
Anders Carlssonc4355b62009-10-07 01:45:02 +0000999 // where n is the length of the string.
1000 llvm::SmallString<8> Str;
1001 Str += "$_";
1002 Str += llvm::utostr(AnonStructId);
1003
1004 Out << Str.size();
1005 Out << Str.str();
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001006 break;
Anders Carlssonc4355b62009-10-07 01:45:02 +00001007 }
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001008
1009 case DeclarationName::ObjCZeroArgSelector:
1010 case DeclarationName::ObjCOneArgSelector:
1011 case DeclarationName::ObjCMultiArgSelector:
1012 assert(false && "Can't mangle Objective-C selector names here!");
1013 break;
1014
1015 case DeclarationName::CXXConstructorName:
Anders Carlsson27ae5362009-04-17 01:58:57 +00001016 if (ND == Structor)
Mike Stump141c5af2009-09-02 00:25:38 +00001017 // If the named decl is the C++ constructor we're mangling, use the type
1018 // we were given.
Anders Carlsson27ae5362009-04-17 01:58:57 +00001019 mangleCXXCtorType(static_cast<CXXCtorType>(StructorType));
Anders Carlsson3ac86b52009-04-15 05:36:58 +00001020 else
1021 // Otherwise, use the complete constructor name. This is relevant if a
1022 // class with a constructor is declared within a constructor.
1023 mangleCXXCtorType(Ctor_Complete);
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001024 break;
1025
1026 case DeclarationName::CXXDestructorName:
Anders Carlsson27ae5362009-04-17 01:58:57 +00001027 if (ND == Structor)
Mike Stump141c5af2009-09-02 00:25:38 +00001028 // If the named decl is the C++ destructor we're mangling, use the type we
1029 // were given.
Anders Carlsson27ae5362009-04-17 01:58:57 +00001030 mangleCXXDtorType(static_cast<CXXDtorType>(StructorType));
1031 else
1032 // Otherwise, use the complete destructor name. This is relevant if a
1033 // class with a destructor is declared within a destructor.
1034 mangleCXXDtorType(Dtor_Complete);
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001035 break;
1036
1037 case DeclarationName::CXXConversionFunctionName:
Mike Stump1eb44332009-09-09 15:08:12 +00001038 // <operator-name> ::= cv <type> # (cast)
Douglas Gregor219cc612009-02-13 01:28:03 +00001039 Out << "cv";
Anders Carlssonb5404912009-10-07 01:06:45 +00001040 mangleType(Context.getASTContext().getCanonicalType(Name.getCXXNameType()));
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001041 break;
1042
Anders Carlsson8257d412009-12-22 06:36:32 +00001043 case DeclarationName::CXXOperatorName: {
John McCall1dd73832010-02-04 01:42:13 +00001044 unsigned Arity;
1045 if (ND) {
1046 Arity = cast<FunctionDecl>(ND)->getNumParams();
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001047
John McCall1dd73832010-02-04 01:42:13 +00001048 // If we have a C++ member function, we need to include the 'this' pointer.
1049 // FIXME: This does not make sense for operators that are static, but their
1050 // names stay the same regardless of the arity (operator new for instance).
1051 if (isa<CXXMethodDecl>(ND))
1052 Arity++;
1053 } else
1054 Arity = KnownArity;
1055
Anders Carlsson8257d412009-12-22 06:36:32 +00001056 mangleOperatorName(Name.getCXXOverloadedOperator(), Arity);
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001057 break;
Anders Carlsson8257d412009-12-22 06:36:32 +00001058 }
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001059
Sean Hunt3e518bd2009-11-29 07:34:05 +00001060 case DeclarationName::CXXLiteralOperatorName:
Sean Hunt5dd6b392009-12-04 21:11:13 +00001061 // FIXME: This mangling is not yet official.
Sean Hunt2421f662009-12-04 21:01:37 +00001062 Out << "li";
Sean Hunt3e518bd2009-11-29 07:34:05 +00001063 mangleSourceName(Name.getCXXLiteralIdentifier());
1064 break;
1065
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001066 case DeclarationName::CXXUsingDirective:
1067 assert(false && "Can't mangle a using directive name!");
Douglas Gregor219cc612009-02-13 01:28:03 +00001068 break;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001069 }
1070}
1071
1072void CXXNameMangler::mangleSourceName(const IdentifierInfo *II) {
1073 // <source-name> ::= <positive length number> <identifier>
1074 // <number> ::= [n] <non-negative decimal integer>
1075 // <identifier> ::= <unqualified source code identifier>
1076 Out << II->getLength() << II->getName();
1077}
1078
Eli Friedman7facf842009-12-02 20:32:49 +00001079void CXXNameMangler::mangleNestedName(const NamedDecl *ND,
Fariborz Jahanian57058532010-03-03 19:41:08 +00001080 const DeclContext *DC,
1081 bool NoFunction) {
Douglas Gregor0a9a6d62011-01-26 17:36:28 +00001082 // <nested-name>
1083 // ::= N [<CV-qualifiers>] [<ref-qualifier>] <prefix> <unqualified-name> E
1084 // ::= N [<CV-qualifiers>] [<ref-qualifier>] <template-prefix>
1085 // <template-args> E
Anders Carlssond99edc42009-09-26 03:55:37 +00001086
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001087 Out << 'N';
Douglas Gregor0a9a6d62011-01-26 17:36:28 +00001088 if (const CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(ND)) {
John McCall0953e762009-09-24 19:53:00 +00001089 mangleQualifiers(Qualifiers::fromCVRMask(Method->getTypeQualifiers()));
Douglas Gregor0a9a6d62011-01-26 17:36:28 +00001090 mangleRefQualifier(Method->getRefQualifier());
1091 }
1092
Anders Carlsson2744a062009-09-18 19:00:18 +00001093 // Check if we have a template.
1094 const TemplateArgumentList *TemplateArgs = 0;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00001095 if (const TemplateDecl *TD = isTemplate(ND, TemplateArgs)) {
Anders Carlsson2744a062009-09-18 19:00:18 +00001096 mangleTemplatePrefix(TD);
Rafael Espindolad9800722010-03-11 14:07:00 +00001097 TemplateParameterList *TemplateParameters = TD->getTemplateParameters();
1098 mangleTemplateArgs(*TemplateParameters, *TemplateArgs);
Fariborz Jahanian57058532010-03-03 19:41:08 +00001099 }
1100 else {
1101 manglePrefix(DC, NoFunction);
Anders Carlsson7482e242009-09-18 04:29:09 +00001102 mangleUnqualifiedName(ND);
1103 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00001104
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001105 Out << 'E';
1106}
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00001107void CXXNameMangler::mangleNestedName(const TemplateDecl *TD,
Anders Carlsson7624f212009-09-18 02:42:01 +00001108 const TemplateArgument *TemplateArgs,
1109 unsigned NumTemplateArgs) {
Anders Carlssone45117b2009-09-27 19:53:49 +00001110 // <nested-name> ::= N [<CV-qualifiers>] <template-prefix> <template-args> E
1111
Anders Carlsson7624f212009-09-18 02:42:01 +00001112 Out << 'N';
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00001113
Anders Carlssone45117b2009-09-27 19:53:49 +00001114 mangleTemplatePrefix(TD);
Rafael Espindolad9800722010-03-11 14:07:00 +00001115 TemplateParameterList *TemplateParameters = TD->getTemplateParameters();
1116 mangleTemplateArgs(*TemplateParameters, TemplateArgs, NumTemplateArgs);
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00001117
Anders Carlsson7624f212009-09-18 02:42:01 +00001118 Out << 'E';
1119}
1120
Anders Carlsson1b42c792009-04-02 16:24:45 +00001121void CXXNameMangler::mangleLocalName(const NamedDecl *ND) {
1122 // <local-name> := Z <function encoding> E <entity name> [<discriminator>]
1123 // := Z <function encoding> E s [<discriminator>]
Mike Stump1eb44332009-09-09 15:08:12 +00001124 // <discriminator> := _ <non-negative number>
Fariborz Jahanian57058532010-03-03 19:41:08 +00001125 const DeclContext *DC = ND->getDeclContext();
Fariborz Jahanian8805fe82011-06-09 19:25:01 +00001126 if (isa<ObjCMethodDecl>(DC) && isa<FunctionDecl>(ND)) {
1127 // Don't add objc method name mangling to locally declared function
1128 mangleUnqualifiedName(ND);
1129 return;
1130 }
1131
Anders Carlsson1b42c792009-04-02 16:24:45 +00001132 Out << 'Z';
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001133
Charles Davis685b1d92010-05-26 18:25:27 +00001134 if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(DC)) {
1135 mangleObjCMethodName(MD);
John McCall82b7d7b2010-10-18 21:28:44 +00001136 } else if (const CXXRecordDecl *RD = GetLocalClassDecl(ND)) {
1137 mangleFunctionEncoding(cast<FunctionDecl>(RD->getDeclContext()));
Fariborz Jahanian57058532010-03-03 19:41:08 +00001138 Out << 'E';
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001139
John McCall82b7d7b2010-10-18 21:28:44 +00001140 // Mangle the name relative to the closest enclosing function.
1141 if (ND == RD) // equality ok because RD derived from ND above
1142 mangleUnqualifiedName(ND);
1143 else
1144 mangleNestedName(ND, DC, true /*NoFunction*/);
1145
Fariborz Jahanian4819ac42010-03-04 01:02:03 +00001146 unsigned disc;
John McCall82b7d7b2010-10-18 21:28:44 +00001147 if (Context.getNextDiscriminator(RD, disc)) {
Fariborz Jahanian4819ac42010-03-04 01:02:03 +00001148 if (disc < 10)
1149 Out << '_' << disc;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001150 else
Fariborz Jahanian4819ac42010-03-04 01:02:03 +00001151 Out << "__" << disc << '_';
1152 }
Fariborz Jahanian57058532010-03-03 19:41:08 +00001153
1154 return;
1155 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001156 else
Fariborz Jahanian57058532010-03-03 19:41:08 +00001157 mangleFunctionEncoding(cast<FunctionDecl>(DC));
Anders Carlsson7b06f6c2009-12-10 03:14:39 +00001158
Anders Carlsson1b42c792009-04-02 16:24:45 +00001159 Out << 'E';
Eli Friedman6f9f25d2009-12-11 20:21:38 +00001160 mangleUnqualifiedName(ND);
Anders Carlsson1b42c792009-04-02 16:24:45 +00001161}
1162
John McCalla0ce15c2011-04-24 08:23:24 +00001163void CXXNameMangler::manglePrefix(NestedNameSpecifier *qualifier) {
1164 switch (qualifier->getKind()) {
1165 case NestedNameSpecifier::Global:
1166 // nothing
1167 return;
1168
1169 case NestedNameSpecifier::Namespace:
1170 mangleName(qualifier->getAsNamespace());
1171 return;
1172
1173 case NestedNameSpecifier::NamespaceAlias:
1174 mangleName(qualifier->getAsNamespaceAlias()->getNamespace());
1175 return;
1176
1177 case NestedNameSpecifier::TypeSpec:
1178 case NestedNameSpecifier::TypeSpecWithTemplate:
John McCall4f4e4132011-05-04 01:45:19 +00001179 manglePrefix(QualType(qualifier->getAsType(), 0));
John McCalla0ce15c2011-04-24 08:23:24 +00001180 return;
1181
1182 case NestedNameSpecifier::Identifier:
1183 // Member expressions can have these without prefixes, but that
1184 // should end up in mangleUnresolvedPrefix instead.
1185 assert(qualifier->getPrefix());
1186 manglePrefix(qualifier->getPrefix());
1187
1188 mangleSourceName(qualifier->getAsIdentifier());
1189 return;
1190 }
1191
1192 llvm_unreachable("unexpected nested name specifier");
1193}
1194
Fariborz Jahanian57058532010-03-03 19:41:08 +00001195void CXXNameMangler::manglePrefix(const DeclContext *DC, bool NoFunction) {
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001196 // <prefix> ::= <prefix> <unqualified-name>
1197 // ::= <template-prefix> <template-args>
1198 // ::= <template-param>
1199 // ::= # empty
1200 // ::= <substitution>
Anders Carlsson6862fc72009-09-17 04:16:28 +00001201
Anders Carlssonadd28822009-09-22 20:33:31 +00001202 while (isa<LinkageSpecDecl>(DC))
1203 DC = DC->getParent();
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00001204
Anders Carlsson9263e912009-09-18 18:39:58 +00001205 if (DC->isTranslationUnit())
1206 return;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00001207
Douglas Gregor35415f52010-05-25 17:04:15 +00001208 if (const BlockDecl *Block = dyn_cast<BlockDecl>(DC)) {
1209 manglePrefix(DC->getParent(), NoFunction);
1210 llvm::SmallString<64> Name;
Rafael Espindolac4850c22011-02-10 23:59:36 +00001211 llvm::raw_svector_ostream NameStream(Name);
1212 Context.mangleBlock(Block, NameStream);
1213 NameStream.flush();
Douglas Gregor35415f52010-05-25 17:04:15 +00001214 Out << Name.size() << Name;
1215 return;
1216 }
1217
Anders Carlsson6862fc72009-09-17 04:16:28 +00001218 if (mangleSubstitution(cast<NamedDecl>(DC)))
1219 return;
Anders Carlsson7482e242009-09-18 04:29:09 +00001220
Anders Carlsson2ee3fca2009-09-18 20:11:09 +00001221 // Check if we have a template.
1222 const TemplateArgumentList *TemplateArgs = 0;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00001223 if (const TemplateDecl *TD = isTemplate(cast<NamedDecl>(DC), TemplateArgs)) {
Anders Carlsson2ee3fca2009-09-18 20:11:09 +00001224 mangleTemplatePrefix(TD);
Rafael Espindolad9800722010-03-11 14:07:00 +00001225 TemplateParameterList *TemplateParameters = TD->getTemplateParameters();
1226 mangleTemplateArgs(*TemplateParameters, *TemplateArgs);
Fariborz Jahanian57058532010-03-03 19:41:08 +00001227 }
Douglas Gregor35415f52010-05-25 17:04:15 +00001228 else if(NoFunction && (isa<FunctionDecl>(DC) || isa<ObjCMethodDecl>(DC)))
Fariborz Jahanian57058532010-03-03 19:41:08 +00001229 return;
Douglas Gregor35415f52010-05-25 17:04:15 +00001230 else if (const ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(DC))
1231 mangleObjCMethodName(Method);
Fariborz Jahanian57058532010-03-03 19:41:08 +00001232 else {
1233 manglePrefix(DC->getParent(), NoFunction);
Anders Carlsson2ee3fca2009-09-18 20:11:09 +00001234 mangleUnqualifiedName(cast<NamedDecl>(DC));
1235 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00001236
Anders Carlsson6862fc72009-09-17 04:16:28 +00001237 addSubstitution(cast<NamedDecl>(DC));
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001238}
1239
Douglas Gregor20f0cc72010-04-23 03:10:43 +00001240void CXXNameMangler::mangleTemplatePrefix(TemplateName Template) {
1241 // <template-prefix> ::= <prefix> <template unqualified-name>
1242 // ::= <template-param>
1243 // ::= <substitution>
1244 if (TemplateDecl *TD = Template.getAsTemplateDecl())
1245 return mangleTemplatePrefix(TD);
1246
1247 if (QualifiedTemplateName *Qualified = Template.getAsQualifiedTemplateName())
John McCalla0ce15c2011-04-24 08:23:24 +00001248 manglePrefix(Qualified->getQualifier());
Sean Huntc3021132010-05-05 15:23:54 +00001249
Douglas Gregor20f0cc72010-04-23 03:10:43 +00001250 if (OverloadedTemplateStorage *Overloaded
1251 = Template.getAsOverloadedTemplate()) {
Sean Huntc3021132010-05-05 15:23:54 +00001252 mangleUnqualifiedName(0, (*Overloaded->begin())->getDeclName(),
Douglas Gregor20f0cc72010-04-23 03:10:43 +00001253 UnknownArity);
1254 return;
1255 }
Sean Huntc3021132010-05-05 15:23:54 +00001256
Douglas Gregor20f0cc72010-04-23 03:10:43 +00001257 DependentTemplateName *Dependent = Template.getAsDependentTemplateName();
1258 assert(Dependent && "Unknown template name kind?");
John McCalla0ce15c2011-04-24 08:23:24 +00001259 manglePrefix(Dependent->getQualifier());
Douglas Gregor1e9268e2010-04-28 05:58:56 +00001260 mangleUnscopedTemplateName(Template);
Douglas Gregor20f0cc72010-04-23 03:10:43 +00001261}
1262
Anders Carlsson0fa6df42009-09-26 19:45:45 +00001263void CXXNameMangler::mangleTemplatePrefix(const TemplateDecl *ND) {
Anders Carlsson7482e242009-09-18 04:29:09 +00001264 // <template-prefix> ::= <prefix> <template unqualified-name>
1265 // ::= <template-param>
1266 // ::= <substitution>
Douglas Gregor32fb4e12010-02-05 20:45:00 +00001267 // <template-template-param> ::= <template-param>
1268 // <substitution>
Anders Carlsson7482e242009-09-18 04:29:09 +00001269
Anders Carlssonaeb85372009-09-26 22:18:22 +00001270 if (mangleSubstitution(ND))
1271 return;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00001272
Douglas Gregor32fb4e12010-02-05 20:45:00 +00001273 // <template-template-param> ::= <template-param>
1274 if (const TemplateTemplateParmDecl *TTP
1275 = dyn_cast<TemplateTemplateParmDecl>(ND)) {
1276 mangleTemplateParameter(TTP->getIndex());
1277 return;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001278 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00001279
Anders Carlssonaa73ab12009-09-18 18:47:07 +00001280 manglePrefix(ND->getDeclContext());
Anders Carlsson1668f202009-09-26 20:13:56 +00001281 mangleUnqualifiedName(ND->getTemplatedDecl());
Anders Carlssonaeb85372009-09-26 22:18:22 +00001282 addSubstitution(ND);
Anders Carlsson7482e242009-09-18 04:29:09 +00001283}
1284
John McCallb6f532e2010-07-14 06:43:17 +00001285/// Mangles a template name under the production <type>. Required for
1286/// template template arguments.
1287/// <type> ::= <class-enum-type>
1288/// ::= <template-param>
1289/// ::= <substitution>
1290void CXXNameMangler::mangleType(TemplateName TN) {
1291 if (mangleSubstitution(TN))
1292 return;
1293
1294 TemplateDecl *TD = 0;
1295
1296 switch (TN.getKind()) {
1297 case TemplateName::QualifiedTemplate:
1298 TD = TN.getAsQualifiedTemplateName()->getTemplateDecl();
1299 goto HaveDecl;
1300
1301 case TemplateName::Template:
1302 TD = TN.getAsTemplateDecl();
1303 goto HaveDecl;
1304
1305 HaveDecl:
1306 if (isa<TemplateTemplateParmDecl>(TD))
1307 mangleTemplateParameter(cast<TemplateTemplateParmDecl>(TD)->getIndex());
1308 else
1309 mangleName(TD);
1310 break;
1311
1312 case TemplateName::OverloadedTemplate:
1313 llvm_unreachable("can't mangle an overloaded template name as a <type>");
1314 break;
1315
1316 case TemplateName::DependentTemplate: {
1317 const DependentTemplateName *Dependent = TN.getAsDependentTemplateName();
1318 assert(Dependent->isIdentifier());
1319
1320 // <class-enum-type> ::= <name>
1321 // <name> ::= <nested-name>
John McCalla0ce15c2011-04-24 08:23:24 +00001322 mangleUnresolvedPrefix(Dependent->getQualifier(), 0);
John McCallb6f532e2010-07-14 06:43:17 +00001323 mangleSourceName(Dependent->getIdentifier());
1324 break;
1325 }
1326
Douglas Gregor1aee05d2011-01-15 06:45:20 +00001327 case TemplateName::SubstTemplateTemplateParmPack: {
1328 SubstTemplateTemplateParmPackStorage *SubstPack
1329 = TN.getAsSubstTemplateTemplateParmPack();
1330 mangleTemplateParameter(SubstPack->getParameterPack()->getIndex());
1331 break;
1332 }
John McCallb6f532e2010-07-14 06:43:17 +00001333 }
1334
1335 addSubstitution(TN);
1336}
1337
Mike Stump1eb44332009-09-09 15:08:12 +00001338void
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001339CXXNameMangler::mangleOperatorName(OverloadedOperatorKind OO, unsigned Arity) {
1340 switch (OO) {
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001341 // <operator-name> ::= nw # new
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001342 case OO_New: Out << "nw"; break;
1343 // ::= na # new[]
1344 case OO_Array_New: Out << "na"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001345 // ::= dl # delete
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001346 case OO_Delete: Out << "dl"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001347 // ::= da # delete[]
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001348 case OO_Array_Delete: Out << "da"; break;
1349 // ::= ps # + (unary)
John McCall5e1e89b2010-08-18 19:18:59 +00001350 // ::= pl # + (binary or unknown)
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001351 case OO_Plus:
Anders Carlsson8257d412009-12-22 06:36:32 +00001352 Out << (Arity == 1? "ps" : "pl"); break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001353 // ::= ng # - (unary)
John McCall5e1e89b2010-08-18 19:18:59 +00001354 // ::= mi # - (binary or unknown)
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001355 case OO_Minus:
Anders Carlsson8257d412009-12-22 06:36:32 +00001356 Out << (Arity == 1? "ng" : "mi"); break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001357 // ::= ad # & (unary)
John McCall5e1e89b2010-08-18 19:18:59 +00001358 // ::= an # & (binary or unknown)
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001359 case OO_Amp:
Anders Carlsson8257d412009-12-22 06:36:32 +00001360 Out << (Arity == 1? "ad" : "an"); break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001361 // ::= de # * (unary)
John McCall5e1e89b2010-08-18 19:18:59 +00001362 // ::= ml # * (binary or unknown)
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001363 case OO_Star:
John McCall5e1e89b2010-08-18 19:18:59 +00001364 // Use binary when unknown.
Anders Carlsson8257d412009-12-22 06:36:32 +00001365 Out << (Arity == 1? "de" : "ml"); break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001366 // ::= co # ~
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001367 case OO_Tilde: Out << "co"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001368 // ::= dv # /
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001369 case OO_Slash: Out << "dv"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001370 // ::= rm # %
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001371 case OO_Percent: Out << "rm"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001372 // ::= or # |
1373 case OO_Pipe: Out << "or"; break;
1374 // ::= eo # ^
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001375 case OO_Caret: Out << "eo"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001376 // ::= aS # =
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001377 case OO_Equal: Out << "aS"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001378 // ::= pL # +=
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001379 case OO_PlusEqual: Out << "pL"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001380 // ::= mI # -=
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001381 case OO_MinusEqual: Out << "mI"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001382 // ::= mL # *=
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001383 case OO_StarEqual: Out << "mL"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001384 // ::= dV # /=
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001385 case OO_SlashEqual: Out << "dV"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001386 // ::= rM # %=
1387 case OO_PercentEqual: Out << "rM"; break;
1388 // ::= aN # &=
1389 case OO_AmpEqual: Out << "aN"; break;
1390 // ::= oR # |=
1391 case OO_PipeEqual: Out << "oR"; break;
1392 // ::= eO # ^=
1393 case OO_CaretEqual: Out << "eO"; break;
1394 // ::= ls # <<
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001395 case OO_LessLess: Out << "ls"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001396 // ::= rs # >>
1397 case OO_GreaterGreater: Out << "rs"; break;
1398 // ::= lS # <<=
1399 case OO_LessLessEqual: Out << "lS"; break;
1400 // ::= rS # >>=
1401 case OO_GreaterGreaterEqual: Out << "rS"; break;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001402 // ::= eq # ==
1403 case OO_EqualEqual: Out << "eq"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001404 // ::= ne # !=
1405 case OO_ExclaimEqual: Out << "ne"; break;
1406 // ::= lt # <
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001407 case OO_Less: Out << "lt"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001408 // ::= gt # >
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001409 case OO_Greater: Out << "gt"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001410 // ::= le # <=
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001411 case OO_LessEqual: Out << "le"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001412 // ::= ge # >=
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001413 case OO_GreaterEqual: Out << "ge"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001414 // ::= nt # !
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001415 case OO_Exclaim: Out << "nt"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001416 // ::= aa # &&
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001417 case OO_AmpAmp: Out << "aa"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001418 // ::= oo # ||
1419 case OO_PipePipe: Out << "oo"; break;
1420 // ::= pp # ++
1421 case OO_PlusPlus: Out << "pp"; break;
1422 // ::= mm # --
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001423 case OO_MinusMinus: Out << "mm"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001424 // ::= cm # ,
1425 case OO_Comma: Out << "cm"; break;
1426 // ::= pm # ->*
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001427 case OO_ArrowStar: Out << "pm"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001428 // ::= pt # ->
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001429 case OO_Arrow: Out << "pt"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001430 // ::= cl # ()
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001431 case OO_Call: Out << "cl"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001432 // ::= ix # []
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001433 case OO_Subscript: Out << "ix"; break;
Anders Carlssone170ba72009-12-14 01:45:37 +00001434
1435 // ::= qu # ?
1436 // The conditional operator can't be overloaded, but we still handle it when
1437 // mangling expressions.
1438 case OO_Conditional: Out << "qu"; break;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001439
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001440 case OO_None:
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001441 case NUM_OVERLOADED_OPERATORS:
Mike Stump1eb44332009-09-09 15:08:12 +00001442 assert(false && "Not an overloaded operator");
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001443 break;
1444 }
1445}
1446
John McCall0953e762009-09-24 19:53:00 +00001447void CXXNameMangler::mangleQualifiers(Qualifiers Quals) {
Mike Stump1eb44332009-09-09 15:08:12 +00001448 // <CV-qualifiers> ::= [r] [V] [K] # restrict (C99), volatile, const
John McCall0953e762009-09-24 19:53:00 +00001449 if (Quals.hasRestrict())
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001450 Out << 'r';
John McCall0953e762009-09-24 19:53:00 +00001451 if (Quals.hasVolatile())
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001452 Out << 'V';
John McCall0953e762009-09-24 19:53:00 +00001453 if (Quals.hasConst())
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001454 Out << 'K';
John McCall0953e762009-09-24 19:53:00 +00001455
Douglas Gregor56079f72010-06-14 23:15:08 +00001456 if (Quals.hasAddressSpace()) {
1457 // Extension:
1458 //
1459 // <type> ::= U <address-space-number>
1460 //
1461 // where <address-space-number> is a source name consisting of 'AS'
1462 // followed by the address space <number>.
1463 llvm::SmallString<64> ASString;
1464 ASString = "AS" + llvm::utostr_32(Quals.getAddressSpace());
1465 Out << 'U' << ASString.size() << ASString;
1466 }
1467
John McCallf85e1932011-06-15 23:02:42 +00001468 llvm::StringRef LifetimeName;
1469 switch (Quals.getObjCLifetime()) {
1470 // Objective-C ARC Extension:
1471 //
1472 // <type> ::= U "__strong"
1473 // <type> ::= U "__weak"
1474 // <type> ::= U "__autoreleasing"
1475 // <type> ::= U "__unsafe_unretained"
1476 case Qualifiers::OCL_None:
1477 break;
1478
1479 case Qualifiers::OCL_Weak:
1480 LifetimeName = "__weak";
1481 break;
1482
1483 case Qualifiers::OCL_Strong:
1484 LifetimeName = "__strong";
1485 break;
1486
1487 case Qualifiers::OCL_Autoreleasing:
1488 LifetimeName = "__autoreleasing";
1489 break;
1490
1491 case Qualifiers::OCL_ExplicitNone:
1492 LifetimeName = "__unsafe_unretained";
1493 break;
1494 }
1495 if (!LifetimeName.empty())
1496 Out << 'U' << LifetimeName.size() << LifetimeName;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001497}
1498
Douglas Gregor0a9a6d62011-01-26 17:36:28 +00001499void CXXNameMangler::mangleRefQualifier(RefQualifierKind RefQualifier) {
1500 // <ref-qualifier> ::= R # lvalue reference
1501 // ::= O # rvalue-reference
1502 // Proposal to Itanium C++ ABI list on 1/26/11
1503 switch (RefQualifier) {
1504 case RQ_None:
1505 break;
1506
1507 case RQ_LValue:
1508 Out << 'R';
1509 break;
1510
1511 case RQ_RValue:
1512 Out << 'O';
1513 break;
1514 }
1515}
1516
Anders Carlsson7b06f6c2009-12-10 03:14:39 +00001517void CXXNameMangler::mangleObjCMethodName(const ObjCMethodDecl *MD) {
Rafael Espindolaf0be9792011-02-11 02:52:17 +00001518 Context.mangleObjCMethodName(MD, Out);
Anders Carlsson7b06f6c2009-12-10 03:14:39 +00001519}
1520
John McCallb47f7482011-01-26 20:05:40 +00001521void CXXNameMangler::mangleType(QualType nonCanon) {
Anders Carlsson4843e582009-03-10 17:07:44 +00001522 // Only operate on the canonical type!
John McCallb47f7482011-01-26 20:05:40 +00001523 QualType canon = nonCanon.getCanonicalType();
Anders Carlsson4843e582009-03-10 17:07:44 +00001524
John McCallb47f7482011-01-26 20:05:40 +00001525 SplitQualType split = canon.split();
1526 Qualifiers quals = split.second;
1527 const Type *ty = split.first;
1528
1529 bool isSubstitutable = quals || !isa<BuiltinType>(ty);
1530 if (isSubstitutable && mangleSubstitution(canon))
Anders Carlsson76967372009-09-17 00:43:46 +00001531 return;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001532
John McCallb47f7482011-01-26 20:05:40 +00001533 // If we're mangling a qualified array type, push the qualifiers to
1534 // the element type.
1535 if (quals && isa<ArrayType>(ty)) {
1536 ty = Context.getASTContext().getAsArrayType(canon);
1537 quals = Qualifiers();
1538
1539 // Note that we don't update canon: we want to add the
1540 // substitution at the canonical type.
1541 }
1542
1543 if (quals) {
1544 mangleQualifiers(quals);
John McCall0953e762009-09-24 19:53:00 +00001545 // Recurse: even if the qualified type isn't yet substitutable,
1546 // the unqualified type might be.
John McCallb47f7482011-01-26 20:05:40 +00001547 mangleType(QualType(ty, 0));
Anders Carlsson76967372009-09-17 00:43:46 +00001548 } else {
John McCallb47f7482011-01-26 20:05:40 +00001549 switch (ty->getTypeClass()) {
John McCallefe6aee2009-09-05 07:56:18 +00001550#define ABSTRACT_TYPE(CLASS, PARENT)
1551#define NON_CANONICAL_TYPE(CLASS, PARENT) \
Anders Carlsson76967372009-09-17 00:43:46 +00001552 case Type::CLASS: \
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +00001553 llvm_unreachable("can't mangle non-canonical type " #CLASS "Type"); \
Anders Carlsson76967372009-09-17 00:43:46 +00001554 return;
John McCallefe6aee2009-09-05 07:56:18 +00001555#define TYPE(CLASS, PARENT) \
Anders Carlsson76967372009-09-17 00:43:46 +00001556 case Type::CLASS: \
John McCallb47f7482011-01-26 20:05:40 +00001557 mangleType(static_cast<const CLASS##Type*>(ty)); \
Anders Carlsson76967372009-09-17 00:43:46 +00001558 break;
John McCallefe6aee2009-09-05 07:56:18 +00001559#include "clang/AST/TypeNodes.def"
Anders Carlsson76967372009-09-17 00:43:46 +00001560 }
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001561 }
Anders Carlsson76967372009-09-17 00:43:46 +00001562
1563 // Add the substitution.
John McCallb47f7482011-01-26 20:05:40 +00001564 if (isSubstitutable)
1565 addSubstitution(canon);
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001566}
1567
Douglas Gregor1b12a3b2010-05-26 05:11:13 +00001568void CXXNameMangler::mangleNameOrStandardSubstitution(const NamedDecl *ND) {
1569 if (!mangleStandardSubstitution(ND))
1570 mangleName(ND);
1571}
1572
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001573void CXXNameMangler::mangleType(const BuiltinType *T) {
John McCallefe6aee2009-09-05 07:56:18 +00001574 // <type> ::= <builtin-type>
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001575 // <builtin-type> ::= v # void
1576 // ::= w # wchar_t
1577 // ::= b # bool
1578 // ::= c # char
1579 // ::= a # signed char
1580 // ::= h # unsigned char
1581 // ::= s # short
1582 // ::= t # unsigned short
1583 // ::= i # int
1584 // ::= j # unsigned int
1585 // ::= l # long
1586 // ::= m # unsigned long
1587 // ::= x # long long, __int64
1588 // ::= y # unsigned long long, __int64
1589 // ::= n # __int128
1590 // UNSUPPORTED: ::= o # unsigned __int128
1591 // ::= f # float
1592 // ::= d # double
1593 // ::= e # long double, __float80
1594 // UNSUPPORTED: ::= g # __float128
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001595 // UNSUPPORTED: ::= Dd # IEEE 754r decimal floating point (64 bits)
1596 // UNSUPPORTED: ::= De # IEEE 754r decimal floating point (128 bits)
1597 // UNSUPPORTED: ::= Df # IEEE 754r decimal floating point (32 bits)
1598 // UNSUPPORTED: ::= Dh # IEEE 754r half-precision floating point (16 bits)
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001599 // ::= Di # char32_t
1600 // ::= Ds # char16_t
Anders Carlssone2923682010-11-04 04:31:32 +00001601 // ::= Dn # std::nullptr_t (i.e., decltype(nullptr))
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001602 // ::= u <source-name> # vendor extended type
1603 switch (T->getKind()) {
1604 case BuiltinType::Void: Out << 'v'; break;
1605 case BuiltinType::Bool: Out << 'b'; break;
1606 case BuiltinType::Char_U: case BuiltinType::Char_S: Out << 'c'; break;
1607 case BuiltinType::UChar: Out << 'h'; break;
1608 case BuiltinType::UShort: Out << 't'; break;
1609 case BuiltinType::UInt: Out << 'j'; break;
1610 case BuiltinType::ULong: Out << 'm'; break;
1611 case BuiltinType::ULongLong: Out << 'y'; break;
Chris Lattner2df9ced2009-04-30 02:43:43 +00001612 case BuiltinType::UInt128: Out << 'o'; break;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001613 case BuiltinType::SChar: Out << 'a'; break;
Chris Lattner3f59c972010-12-25 23:25:43 +00001614 case BuiltinType::WChar_S:
1615 case BuiltinType::WChar_U: Out << 'w'; break;
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001616 case BuiltinType::Char16: Out << "Ds"; break;
1617 case BuiltinType::Char32: Out << "Di"; break;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001618 case BuiltinType::Short: Out << 's'; break;
1619 case BuiltinType::Int: Out << 'i'; break;
1620 case BuiltinType::Long: Out << 'l'; break;
1621 case BuiltinType::LongLong: Out << 'x'; break;
Chris Lattner2df9ced2009-04-30 02:43:43 +00001622 case BuiltinType::Int128: Out << 'n'; break;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001623 case BuiltinType::Float: Out << 'f'; break;
1624 case BuiltinType::Double: Out << 'd'; break;
1625 case BuiltinType::LongDouble: Out << 'e'; break;
Anders Carlssone2923682010-11-04 04:31:32 +00001626 case BuiltinType::NullPtr: Out << "Dn"; break;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001627
1628 case BuiltinType::Overload:
1629 case BuiltinType::Dependent:
John McCall864c0412011-04-26 20:42:42 +00001630 case BuiltinType::BoundMember:
John McCall1de4d4e2011-04-07 08:22:57 +00001631 case BuiltinType::UnknownAny:
John McCallfb44de92011-05-01 22:35:37 +00001632 llvm_unreachable("mangling a placeholder type");
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001633 break;
Steve Naroff9533a7f2009-07-22 17:14:51 +00001634 case BuiltinType::ObjCId: Out << "11objc_object"; break;
1635 case BuiltinType::ObjCClass: Out << "10objc_class"; break;
Fariborz Jahanian13dcd002009-11-21 19:53:08 +00001636 case BuiltinType::ObjCSel: Out << "13objc_selector"; break;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001637 }
1638}
1639
John McCallefe6aee2009-09-05 07:56:18 +00001640// <type> ::= <function-type>
1641// <function-type> ::= F [Y] <bare-function-type> E
1642void CXXNameMangler::mangleType(const FunctionProtoType *T) {
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001643 Out << 'F';
Mike Stumpf5408fe2009-05-16 07:57:57 +00001644 // FIXME: We don't have enough information in the AST to produce the 'Y'
1645 // encoding for extern "C" function types.
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001646 mangleBareFunctionType(T, /*MangleReturnType=*/true);
1647 Out << 'E';
1648}
John McCallefe6aee2009-09-05 07:56:18 +00001649void CXXNameMangler::mangleType(const FunctionNoProtoType *T) {
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +00001650 llvm_unreachable("Can't mangle K&R function prototypes");
John McCallefe6aee2009-09-05 07:56:18 +00001651}
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001652void CXXNameMangler::mangleBareFunctionType(const FunctionType *T,
1653 bool MangleReturnType) {
John McCallefe6aee2009-09-05 07:56:18 +00001654 // We should never be mangling something without a prototype.
1655 const FunctionProtoType *Proto = cast<FunctionProtoType>(T);
1656
John McCallfb44de92011-05-01 22:35:37 +00001657 // Record that we're in a function type. See mangleFunctionParam
1658 // for details on what we're trying to achieve here.
1659 FunctionTypeDepthState saved = FunctionTypeDepth.push();
1660
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001661 // <bare-function-type> ::= <signature type>+
John McCallfb44de92011-05-01 22:35:37 +00001662 if (MangleReturnType) {
1663 FunctionTypeDepth.enterResultType();
John McCallefe6aee2009-09-05 07:56:18 +00001664 mangleType(Proto->getResultType());
John McCallfb44de92011-05-01 22:35:37 +00001665 FunctionTypeDepth.leaveResultType();
1666 }
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001667
Anders Carlsson93296682010-06-02 04:40:13 +00001668 if (Proto->getNumArgs() == 0 && !Proto->isVariadic()) {
Eli Friedmana7e68452010-08-22 01:00:03 +00001669 // <builtin-type> ::= v # void
Anders Carlssonc6c91bc2009-04-01 00:15:23 +00001670 Out << 'v';
John McCallfb44de92011-05-01 22:35:37 +00001671
1672 FunctionTypeDepth.pop(saved);
Anders Carlssonc6c91bc2009-04-01 00:15:23 +00001673 return;
1674 }
Mike Stump1eb44332009-09-09 15:08:12 +00001675
Douglas Gregor72564e72009-02-26 23:50:07 +00001676 for (FunctionProtoType::arg_type_iterator Arg = Proto->arg_type_begin(),
Mike Stump1eb44332009-09-09 15:08:12 +00001677 ArgEnd = Proto->arg_type_end();
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001678 Arg != ArgEnd; ++Arg)
1679 mangleType(*Arg);
Douglas Gregor219cc612009-02-13 01:28:03 +00001680
John McCallfb44de92011-05-01 22:35:37 +00001681 FunctionTypeDepth.pop(saved);
1682
Douglas Gregor219cc612009-02-13 01:28:03 +00001683 // <builtin-type> ::= z # ellipsis
1684 if (Proto->isVariadic())
1685 Out << 'z';
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001686}
1687
John McCallefe6aee2009-09-05 07:56:18 +00001688// <type> ::= <class-enum-type>
Mike Stump1eb44332009-09-09 15:08:12 +00001689// <class-enum-type> ::= <name>
John McCalled976492009-12-04 22:46:56 +00001690void CXXNameMangler::mangleType(const UnresolvedUsingType *T) {
1691 mangleName(T->getDecl());
1692}
1693
1694// <type> ::= <class-enum-type>
1695// <class-enum-type> ::= <name>
John McCallefe6aee2009-09-05 07:56:18 +00001696void CXXNameMangler::mangleType(const EnumType *T) {
1697 mangleType(static_cast<const TagType*>(T));
1698}
1699void CXXNameMangler::mangleType(const RecordType *T) {
1700 mangleType(static_cast<const TagType*>(T));
1701}
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001702void CXXNameMangler::mangleType(const TagType *T) {
Eli Friedmanecb7e932009-12-11 18:00:57 +00001703 mangleName(T->getDecl());
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001704}
1705
John McCallefe6aee2009-09-05 07:56:18 +00001706// <type> ::= <array-type>
1707// <array-type> ::= A <positive dimension number> _ <element type>
1708// ::= A [<dimension expression>] _ <element type>
1709void CXXNameMangler::mangleType(const ConstantArrayType *T) {
1710 Out << 'A' << T->getSize() << '_';
1711 mangleType(T->getElementType());
1712}
1713void CXXNameMangler::mangleType(const VariableArrayType *T) {
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001714 Out << 'A';
Fariborz Jahanian7281d1f2010-11-02 16:54:00 +00001715 // decayed vla types (size 0) will just be skipped.
1716 if (T->getSizeExpr())
1717 mangleExpression(T->getSizeExpr());
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001718 Out << '_';
1719 mangleType(T->getElementType());
1720}
John McCallefe6aee2009-09-05 07:56:18 +00001721void CXXNameMangler::mangleType(const DependentSizedArrayType *T) {
1722 Out << 'A';
1723 mangleExpression(T->getSizeExpr());
1724 Out << '_';
1725 mangleType(T->getElementType());
1726}
1727void CXXNameMangler::mangleType(const IncompleteArrayType *T) {
Nick Lewycky271b6652010-09-05 03:40:33 +00001728 Out << "A_";
John McCallefe6aee2009-09-05 07:56:18 +00001729 mangleType(T->getElementType());
1730}
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001731
John McCallefe6aee2009-09-05 07:56:18 +00001732// <type> ::= <pointer-to-member-type>
1733// <pointer-to-member-type> ::= M <class type> <member type>
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001734void CXXNameMangler::mangleType(const MemberPointerType *T) {
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001735 Out << 'M';
1736 mangleType(QualType(T->getClass(), 0));
Anders Carlsson0e650012009-05-17 17:41:20 +00001737 QualType PointeeType = T->getPointeeType();
1738 if (const FunctionProtoType *FPT = dyn_cast<FunctionProtoType>(PointeeType)) {
John McCall0953e762009-09-24 19:53:00 +00001739 mangleQualifiers(Qualifiers::fromCVRMask(FPT->getTypeQuals()));
Douglas Gregor0a9a6d62011-01-26 17:36:28 +00001740 mangleRefQualifier(FPT->getRefQualifier());
Anders Carlsson0e650012009-05-17 17:41:20 +00001741 mangleType(FPT);
Anders Carlsson9d85b722010-06-02 04:29:50 +00001742
1743 // Itanium C++ ABI 5.1.8:
1744 //
1745 // The type of a non-static member function is considered to be different,
1746 // for the purposes of substitution, from the type of a namespace-scope or
1747 // static member function whose type appears similar. The types of two
1748 // non-static member functions are considered to be different, for the
1749 // purposes of substitution, if the functions are members of different
1750 // classes. In other words, for the purposes of substitution, the class of
1751 // which the function is a member is considered part of the type of
1752 // function.
1753
1754 // We increment the SeqID here to emulate adding an entry to the
1755 // substitution table. We can't actually add it because we don't want this
1756 // particular function type to be substituted.
1757 ++SeqID;
Mike Stump1eb44332009-09-09 15:08:12 +00001758 } else
Anders Carlsson0e650012009-05-17 17:41:20 +00001759 mangleType(PointeeType);
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001760}
1761
John McCallefe6aee2009-09-05 07:56:18 +00001762// <type> ::= <template-param>
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001763void CXXNameMangler::mangleType(const TemplateTypeParmType *T) {
Anders Carlsson0ccdf8d2009-09-27 00:38:53 +00001764 mangleTemplateParameter(T->getIndex());
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001765}
1766
Douglas Gregorc3069d62011-01-14 02:55:32 +00001767// <type> ::= <template-param>
1768void CXXNameMangler::mangleType(const SubstTemplateTypeParmPackType *T) {
1769 mangleTemplateParameter(T->getReplacedParameter()->getIndex());
1770}
1771
John McCallefe6aee2009-09-05 07:56:18 +00001772// <type> ::= P <type> # pointer-to
1773void CXXNameMangler::mangleType(const PointerType *T) {
1774 Out << 'P';
1775 mangleType(T->getPointeeType());
1776}
1777void CXXNameMangler::mangleType(const ObjCObjectPointerType *T) {
1778 Out << 'P';
1779 mangleType(T->getPointeeType());
1780}
1781
1782// <type> ::= R <type> # reference-to
1783void CXXNameMangler::mangleType(const LValueReferenceType *T) {
1784 Out << 'R';
1785 mangleType(T->getPointeeType());
1786}
1787
1788// <type> ::= O <type> # rvalue reference-to (C++0x)
1789void CXXNameMangler::mangleType(const RValueReferenceType *T) {
1790 Out << 'O';
1791 mangleType(T->getPointeeType());
1792}
1793
1794// <type> ::= C <type> # complex pair (C 2000)
1795void CXXNameMangler::mangleType(const ComplexType *T) {
1796 Out << 'C';
1797 mangleType(T->getElementType());
1798}
1799
Bob Wilsonc7df92d2010-11-12 17:24:43 +00001800// ARM's ABI for Neon vector types specifies that they should be mangled as
Bob Wilson57147a82010-11-16 00:32:18 +00001801// if they are structs (to match ARM's initial implementation). The
1802// vector type must be one of the special types predefined by ARM.
1803void CXXNameMangler::mangleNeonVectorType(const VectorType *T) {
Bob Wilsonc7df92d2010-11-12 17:24:43 +00001804 QualType EltType = T->getElementType();
Bob Wilson57147a82010-11-16 00:32:18 +00001805 assert(EltType->isBuiltinType() && "Neon vector element not a BuiltinType");
Bob Wilsonc7df92d2010-11-12 17:24:43 +00001806 const char *EltName = 0;
Bob Wilson491328c2010-11-12 17:24:46 +00001807 if (T->getVectorKind() == VectorType::NeonPolyVector) {
1808 switch (cast<BuiltinType>(EltType)->getKind()) {
Bob Wilson4cfaa5d2010-11-12 17:24:49 +00001809 case BuiltinType::SChar: EltName = "poly8_t"; break;
1810 case BuiltinType::Short: EltName = "poly16_t"; break;
Bob Wilson57147a82010-11-16 00:32:18 +00001811 default: llvm_unreachable("unexpected Neon polynomial vector element type");
Bob Wilson491328c2010-11-12 17:24:46 +00001812 }
1813 } else {
1814 switch (cast<BuiltinType>(EltType)->getKind()) {
Bob Wilson4cfaa5d2010-11-12 17:24:49 +00001815 case BuiltinType::SChar: EltName = "int8_t"; break;
1816 case BuiltinType::UChar: EltName = "uint8_t"; break;
1817 case BuiltinType::Short: EltName = "int16_t"; break;
1818 case BuiltinType::UShort: EltName = "uint16_t"; break;
1819 case BuiltinType::Int: EltName = "int32_t"; break;
1820 case BuiltinType::UInt: EltName = "uint32_t"; break;
1821 case BuiltinType::LongLong: EltName = "int64_t"; break;
1822 case BuiltinType::ULongLong: EltName = "uint64_t"; break;
1823 case BuiltinType::Float: EltName = "float32_t"; break;
Bob Wilson57147a82010-11-16 00:32:18 +00001824 default: llvm_unreachable("unexpected Neon vector element type");
Bob Wilson491328c2010-11-12 17:24:46 +00001825 }
Bob Wilsonc7df92d2010-11-12 17:24:43 +00001826 }
1827 const char *BaseName = 0;
Bob Wilson4cfaa5d2010-11-12 17:24:49 +00001828 unsigned BitSize = (T->getNumElements() *
Bob Wilson3a723022010-11-16 00:32:12 +00001829 getASTContext().getTypeSize(EltType));
Bob Wilsonc7df92d2010-11-12 17:24:43 +00001830 if (BitSize == 64)
1831 BaseName = "__simd64_";
Bob Wilson57147a82010-11-16 00:32:18 +00001832 else {
1833 assert(BitSize == 128 && "Neon vector type not 64 or 128 bits");
Bob Wilsonc7df92d2010-11-12 17:24:43 +00001834 BaseName = "__simd128_";
Bob Wilson57147a82010-11-16 00:32:18 +00001835 }
Bob Wilsonc7df92d2010-11-12 17:24:43 +00001836 Out << strlen(BaseName) + strlen(EltName);
1837 Out << BaseName << EltName;
Bob Wilsonc7df92d2010-11-12 17:24:43 +00001838}
1839
John McCallefe6aee2009-09-05 07:56:18 +00001840// GNU extension: vector types
Chris Lattner788b0fd2010-06-23 06:00:24 +00001841// <type> ::= <vector-type>
1842// <vector-type> ::= Dv <positive dimension number> _
1843// <extended element type>
1844// ::= Dv [<dimension expression>] _ <element type>
1845// <extended element type> ::= <element type>
1846// ::= p # AltiVec vector pixel
John McCallefe6aee2009-09-05 07:56:18 +00001847void CXXNameMangler::mangleType(const VectorType *T) {
Bob Wilson491328c2010-11-12 17:24:46 +00001848 if ((T->getVectorKind() == VectorType::NeonVector ||
Bob Wilson57147a82010-11-16 00:32:18 +00001849 T->getVectorKind() == VectorType::NeonPolyVector)) {
1850 mangleNeonVectorType(T);
Bob Wilsonc7df92d2010-11-12 17:24:43 +00001851 return;
Bob Wilson57147a82010-11-16 00:32:18 +00001852 }
Nick Lewycky0e5f0672010-03-26 07:18:04 +00001853 Out << "Dv" << T->getNumElements() << '_';
Bob Wilsone86d78c2010-11-10 21:56:12 +00001854 if (T->getVectorKind() == VectorType::AltiVecPixel)
Chris Lattner788b0fd2010-06-23 06:00:24 +00001855 Out << 'p';
Bob Wilsone86d78c2010-11-10 21:56:12 +00001856 else if (T->getVectorKind() == VectorType::AltiVecBool)
Chris Lattner788b0fd2010-06-23 06:00:24 +00001857 Out << 'b';
1858 else
1859 mangleType(T->getElementType());
John McCallefe6aee2009-09-05 07:56:18 +00001860}
1861void CXXNameMangler::mangleType(const ExtVectorType *T) {
1862 mangleType(static_cast<const VectorType*>(T));
1863}
1864void CXXNameMangler::mangleType(const DependentSizedExtVectorType *T) {
Nick Lewycky0e5f0672010-03-26 07:18:04 +00001865 Out << "Dv";
1866 mangleExpression(T->getSizeExpr());
1867 Out << '_';
John McCallefe6aee2009-09-05 07:56:18 +00001868 mangleType(T->getElementType());
1869}
1870
Douglas Gregor7536dd52010-12-20 02:24:11 +00001871void CXXNameMangler::mangleType(const PackExpansionType *T) {
Douglas Gregor4fc48662011-01-13 16:39:34 +00001872 // <type> ::= Dp <type> # pack expansion (C++0x)
Douglas Gregor255c2692011-01-13 17:44:36 +00001873 Out << "Dp";
Douglas Gregor7536dd52010-12-20 02:24:11 +00001874 mangleType(T->getPattern());
1875}
1876
Anders Carlssona40c5e42009-03-07 22:03:21 +00001877void CXXNameMangler::mangleType(const ObjCInterfaceType *T) {
1878 mangleSourceName(T->getDecl()->getIdentifier());
1879}
1880
John McCallc12c5bb2010-05-15 11:32:37 +00001881void CXXNameMangler::mangleType(const ObjCObjectType *T) {
John McCallc00c1f62010-05-15 17:06:29 +00001882 // We don't allow overloading by different protocol qualification,
1883 // so mangling them isn't necessary.
John McCallc12c5bb2010-05-15 11:32:37 +00001884 mangleType(T->getBaseType());
1885}
1886
John McCallefe6aee2009-09-05 07:56:18 +00001887void CXXNameMangler::mangleType(const BlockPointerType *T) {
Anders Carlssonf28c6872009-12-23 22:31:44 +00001888 Out << "U13block_pointer";
1889 mangleType(T->getPointeeType());
John McCallefe6aee2009-09-05 07:56:18 +00001890}
1891
John McCall31f17ec2010-04-27 00:57:59 +00001892void CXXNameMangler::mangleType(const InjectedClassNameType *T) {
1893 // Mangle injected class name types as if the user had written the
1894 // specialization out fully. It may not actually be possible to see
1895 // this mangling, though.
1896 mangleType(T->getInjectedSpecializationType());
1897}
1898
John McCallefe6aee2009-09-05 07:56:18 +00001899void CXXNameMangler::mangleType(const TemplateSpecializationType *T) {
Douglas Gregor1e9268e2010-04-28 05:58:56 +00001900 if (TemplateDecl *TD = T->getTemplateName().getAsTemplateDecl()) {
1901 mangleName(TD, T->getArgs(), T->getNumArgs());
1902 } else {
1903 if (mangleSubstitution(QualType(T, 0)))
1904 return;
Sean Huntc3021132010-05-05 15:23:54 +00001905
Douglas Gregor1e9268e2010-04-28 05:58:56 +00001906 mangleTemplatePrefix(T->getTemplateName());
Sean Huntc3021132010-05-05 15:23:54 +00001907
Douglas Gregor1e9268e2010-04-28 05:58:56 +00001908 // FIXME: GCC does not appear to mangle the template arguments when
1909 // the template in question is a dependent template name. Should we
1910 // emulate that badness?
1911 mangleTemplateArgs(T->getTemplateName(), T->getArgs(), T->getNumArgs());
1912 addSubstitution(QualType(T, 0));
1913 }
John McCallefe6aee2009-09-05 07:56:18 +00001914}
1915
Douglas Gregor4714c122010-03-31 17:34:00 +00001916void CXXNameMangler::mangleType(const DependentNameType *T) {
Anders Carlssonae352482009-09-26 02:26:02 +00001917 // Typename types are always nested
1918 Out << 'N';
John McCalla0ce15c2011-04-24 08:23:24 +00001919 manglePrefix(T->getQualifier());
John McCall33500952010-06-11 00:33:02 +00001920 mangleSourceName(T->getIdentifier());
1921 Out << 'E';
1922}
John McCall6ab30e02010-06-09 07:26:17 +00001923
John McCall33500952010-06-11 00:33:02 +00001924void CXXNameMangler::mangleType(const DependentTemplateSpecializationType *T) {
Douglas Gregoraa2187d2011-02-28 00:04:36 +00001925 // Dependently-scoped template types are nested if they have a prefix.
John McCall33500952010-06-11 00:33:02 +00001926 Out << 'N';
1927
1928 // TODO: avoid making this TemplateName.
1929 TemplateName Prefix =
1930 getASTContext().getDependentTemplateName(T->getQualifier(),
1931 T->getIdentifier());
1932 mangleTemplatePrefix(Prefix);
1933
1934 // FIXME: GCC does not appear to mangle the template arguments when
1935 // the template in question is a dependent template name. Should we
1936 // emulate that badness?
1937 mangleTemplateArgs(Prefix, T->getArgs(), T->getNumArgs());
Anders Carlssonae352482009-09-26 02:26:02 +00001938 Out << 'E';
John McCallefe6aee2009-09-05 07:56:18 +00001939}
1940
John McCallad5e7382010-03-01 23:49:17 +00001941void CXXNameMangler::mangleType(const TypeOfType *T) {
1942 // FIXME: this is pretty unsatisfactory, but there isn't an obvious
1943 // "extension with parameters" mangling.
1944 Out << "u6typeof";
1945}
1946
1947void CXXNameMangler::mangleType(const TypeOfExprType *T) {
1948 // FIXME: this is pretty unsatisfactory, but there isn't an obvious
1949 // "extension with parameters" mangling.
1950 Out << "u6typeof";
1951}
1952
1953void CXXNameMangler::mangleType(const DecltypeType *T) {
1954 Expr *E = T->getUnderlyingExpr();
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001955
John McCallad5e7382010-03-01 23:49:17 +00001956 // type ::= Dt <expression> E # decltype of an id-expression
1957 // # or class member access
1958 // ::= DT <expression> E # decltype of an expression
1959
1960 // This purports to be an exhaustive list of id-expressions and
1961 // class member accesses. Note that we do not ignore parentheses;
1962 // parentheses change the semantics of decltype for these
1963 // expressions (and cause the mangler to use the other form).
1964 if (isa<DeclRefExpr>(E) ||
1965 isa<MemberExpr>(E) ||
1966 isa<UnresolvedLookupExpr>(E) ||
1967 isa<DependentScopeDeclRefExpr>(E) ||
1968 isa<CXXDependentScopeMemberExpr>(E) ||
1969 isa<UnresolvedMemberExpr>(E))
1970 Out << "Dt";
1971 else
1972 Out << "DT";
1973 mangleExpression(E);
1974 Out << 'E';
1975}
1976
Sean Huntca63c202011-05-24 22:41:36 +00001977void CXXNameMangler::mangleType(const UnaryTransformType *T) {
1978 // If this is dependent, we need to record that. If not, we simply
1979 // mangle it as the underlying type since they are equivalent.
1980 if (T->isDependentType()) {
1981 Out << 'U';
1982
1983 switch (T->getUTTKind()) {
1984 case UnaryTransformType::EnumUnderlyingType:
1985 Out << "3eut";
1986 break;
1987 }
1988 }
1989
1990 mangleType(T->getUnderlyingType());
1991}
1992
Richard Smith34b41d92011-02-20 03:19:35 +00001993void CXXNameMangler::mangleType(const AutoType *T) {
1994 QualType D = T->getDeducedType();
Richard Smith967ecd32011-02-21 20:10:02 +00001995 // <builtin-type> ::= Da # dependent auto
1996 if (D.isNull())
1997 Out << "Da";
1998 else
1999 mangleType(D);
Richard Smith34b41d92011-02-20 03:19:35 +00002000}
2001
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002002void CXXNameMangler::mangleIntegerLiteral(QualType T,
Anders Carlssone170ba72009-12-14 01:45:37 +00002003 const llvm::APSInt &Value) {
2004 // <expr-primary> ::= L <type> <value number> E # integer literal
2005 Out << 'L';
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002006
Anders Carlssone170ba72009-12-14 01:45:37 +00002007 mangleType(T);
2008 if (T->isBooleanType()) {
2009 // Boolean values are encoded as 0/1.
2010 Out << (Value.getBoolValue() ? '1' : '0');
2011 } else {
John McCall0512e482010-07-14 04:20:34 +00002012 mangleNumber(Value);
Anders Carlssone170ba72009-12-14 01:45:37 +00002013 }
2014 Out << 'E';
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002015
Anders Carlssone170ba72009-12-14 01:45:37 +00002016}
2017
John McCall2f27bf82010-02-04 02:56:29 +00002018/// Mangles a member expression. Implicit accesses are not handled,
2019/// but that should be okay, because you shouldn't be able to
2020/// make an implicit access in a function template declaration.
John McCalla0ce15c2011-04-24 08:23:24 +00002021void CXXNameMangler::mangleMemberExpr(const Expr *base,
2022 bool isArrow,
2023 NestedNameSpecifier *qualifier,
2024 NamedDecl *firstQualifierLookup,
2025 DeclarationName member,
2026 unsigned arity) {
2027 // <expression> ::= dt <expression> <unresolved-name>
2028 // ::= pt <expression> <unresolved-name>
2029 Out << (isArrow ? "pt" : "dt");
2030 mangleExpression(base);
2031 mangleUnresolvedName(qualifier, firstQualifierLookup, member, arity);
John McCall2f27bf82010-02-04 02:56:29 +00002032}
2033
John McCall5a7e6f72011-04-28 02:52:03 +00002034/// Look at the callee of the given call expression and determine if
2035/// it's a parenthesized id-expression which would have triggered ADL
2036/// otherwise.
2037static bool isParenthesizedADLCallee(const CallExpr *call) {
2038 const Expr *callee = call->getCallee();
2039 const Expr *fn = callee->IgnoreParens();
2040
2041 // Must be parenthesized. IgnoreParens() skips __extension__ nodes,
2042 // too, but for those to appear in the callee, it would have to be
2043 // parenthesized.
2044 if (callee == fn) return false;
2045
2046 // Must be an unresolved lookup.
2047 const UnresolvedLookupExpr *lookup = dyn_cast<UnresolvedLookupExpr>(fn);
2048 if (!lookup) return false;
2049
2050 assert(!lookup->requiresADL());
2051
2052 // Must be an unqualified lookup.
2053 if (lookup->getQualifier()) return false;
2054
2055 // Must not have found a class member. Note that if one is a class
2056 // member, they're all class members.
2057 if (lookup->getNumDecls() > 0 &&
2058 (*lookup->decls_begin())->isCXXClassMember())
2059 return false;
2060
2061 // Otherwise, ADL would have been triggered.
2062 return true;
2063}
2064
John McCall5e1e89b2010-08-18 19:18:59 +00002065void CXXNameMangler::mangleExpression(const Expr *E, unsigned Arity) {
Anders Carlssond553f8c2009-09-21 01:21:10 +00002066 // <expression> ::= <unary operator-name> <expression>
John McCall09cc1412010-02-03 00:55:45 +00002067 // ::= <binary operator-name> <expression> <expression>
2068 // ::= <trinary operator-name> <expression> <expression> <expression>
Anders Carlssond553f8c2009-09-21 01:21:10 +00002069 // ::= cv <type> expression # conversion with one argument
2070 // ::= cv <type> _ <expression>* E # conversion with a different number of arguments
Eli Friedmana7e68452010-08-22 01:00:03 +00002071 // ::= st <type> # sizeof (a type)
Anders Carlssond553f8c2009-09-21 01:21:10 +00002072 // ::= at <type> # alignof (a type)
2073 // ::= <template-param>
2074 // ::= <function-param>
2075 // ::= sr <type> <unqualified-name> # dependent name
2076 // ::= sr <type> <unqualified-name> <template-args> # dependent template-id
Douglas Gregor63f62df2011-06-05 05:27:58 +00002077 // ::= ds <expression> <expression> # expr.*expr
Anders Carlssond553f8c2009-09-21 01:21:10 +00002078 // ::= sZ <template-param> # size of a parameter pack
Douglas Gregor4fc48662011-01-13 16:39:34 +00002079 // ::= sZ <function-param> # size of a function parameter pack
John McCall09cc1412010-02-03 00:55:45 +00002080 // ::= <expr-primary>
John McCall1dd73832010-02-04 01:42:13 +00002081 // <expr-primary> ::= L <type> <value number> E # integer literal
2082 // ::= L <type <value float> E # floating literal
2083 // ::= L <mangled-name> E # external name
Anders Carlssond553f8c2009-09-21 01:21:10 +00002084 switch (E->getStmtClass()) {
John McCall6ae1f352010-04-09 22:26:14 +00002085 case Expr::NoStmtClass:
John McCall63c00d72011-02-09 08:16:59 +00002086#define ABSTRACT_STMT(Type)
John McCall6ae1f352010-04-09 22:26:14 +00002087#define EXPR(Type, Base)
2088#define STMT(Type, Base) \
2089 case Expr::Type##Class:
Sean Hunt4bfe1962010-05-05 15:24:00 +00002090#include "clang/AST/StmtNodes.inc"
John McCall0512e482010-07-14 04:20:34 +00002091 // fallthrough
2092
2093 // These all can only appear in local or variable-initialization
2094 // contexts and so should never appear in a mangling.
2095 case Expr::AddrLabelExprClass:
2096 case Expr::BlockDeclRefExprClass:
2097 case Expr::CXXThisExprClass:
2098 case Expr::DesignatedInitExprClass:
2099 case Expr::ImplicitValueInitExprClass:
2100 case Expr::InitListExprClass:
2101 case Expr::ParenListExprClass:
2102 case Expr::CXXScalarValueInitExprClass:
John McCall09cc1412010-02-03 00:55:45 +00002103 llvm_unreachable("unexpected statement kind");
2104 break;
2105
John McCall0512e482010-07-14 04:20:34 +00002106 // FIXME: invent manglings for all these.
2107 case Expr::BlockExprClass:
2108 case Expr::CXXPseudoDestructorExprClass:
2109 case Expr::ChooseExprClass:
2110 case Expr::CompoundLiteralExprClass:
2111 case Expr::ExtVectorElementExprClass:
Peter Collingbournef111d932011-04-15 00:35:48 +00002112 case Expr::GenericSelectionExprClass:
John McCall0512e482010-07-14 04:20:34 +00002113 case Expr::ObjCEncodeExprClass:
John McCall0512e482010-07-14 04:20:34 +00002114 case Expr::ObjCIsaExprClass:
2115 case Expr::ObjCIvarRefExprClass:
2116 case Expr::ObjCMessageExprClass:
2117 case Expr::ObjCPropertyRefExprClass:
2118 case Expr::ObjCProtocolExprClass:
2119 case Expr::ObjCSelectorExprClass:
2120 case Expr::ObjCStringLiteralClass:
John McCallf85e1932011-06-15 23:02:42 +00002121 case Expr::ObjCIndirectCopyRestoreExprClass:
John McCall0512e482010-07-14 04:20:34 +00002122 case Expr::OffsetOfExprClass:
2123 case Expr::PredefinedExprClass:
2124 case Expr::ShuffleVectorExprClass:
2125 case Expr::StmtExprClass:
John McCall0512e482010-07-14 04:20:34 +00002126 case Expr::UnaryTypeTraitExprClass:
Francois Pichet6ad6f282010-12-07 00:08:36 +00002127 case Expr::BinaryTypeTraitExprClass:
John Wiegley21ff2e52011-04-28 00:16:57 +00002128 case Expr::ArrayTypeTraitExprClass:
John Wiegley55262202011-04-25 06:54:41 +00002129 case Expr::ExpressionTraitExprClass:
Francois Pichet9be88402010-09-08 23:47:05 +00002130 case Expr::VAArgExprClass:
Sebastian Redl2e156222010-09-10 20:55:43 +00002131 case Expr::CXXUuidofExprClass:
Peter Collingbournee08ce652011-02-09 21:07:24 +00002132 case Expr::CXXNoexceptExprClass:
Tanya Lattner61eee0c2011-06-04 00:47:47 +00002133 case Expr::CUDAKernelCallExprClass:
2134 case Expr::AsTypeExprClass:
2135 {
John McCall6ae1f352010-04-09 22:26:14 +00002136 // As bad as this diagnostic is, it's better than crashing.
2137 Diagnostic &Diags = Context.getDiags();
2138 unsigned DiagID = Diags.getCustomDiagID(Diagnostic::Error,
2139 "cannot yet mangle expression type %0");
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +00002140 Diags.Report(E->getExprLoc(), DiagID)
John McCall739bf092010-04-10 09:39:25 +00002141 << E->getStmtClassName() << E->getSourceRange();
John McCall6ae1f352010-04-09 22:26:14 +00002142 break;
2143 }
2144
John McCall56ca35d2011-02-17 10:25:35 +00002145 // Even gcc-4.5 doesn't mangle this.
2146 case Expr::BinaryConditionalOperatorClass: {
2147 Diagnostic &Diags = Context.getDiags();
2148 unsigned DiagID =
2149 Diags.getCustomDiagID(Diagnostic::Error,
2150 "?: operator with omitted middle operand cannot be mangled");
2151 Diags.Report(E->getExprLoc(), DiagID)
2152 << E->getStmtClassName() << E->getSourceRange();
2153 break;
2154 }
2155
2156 // These are used for internal purposes and cannot be meaningfully mangled.
John McCall7cd7d1a2010-11-15 23:31:06 +00002157 case Expr::OpaqueValueExprClass:
2158 llvm_unreachable("cannot mangle opaque value; mangling wrong thing?");
2159
John McCall0512e482010-07-14 04:20:34 +00002160 case Expr::CXXDefaultArgExprClass:
John McCall5e1e89b2010-08-18 19:18:59 +00002161 mangleExpression(cast<CXXDefaultArgExpr>(E)->getExpr(), Arity);
John McCall0512e482010-07-14 04:20:34 +00002162 break;
2163
2164 case Expr::CXXMemberCallExprClass: // fallthrough
John McCall1dd73832010-02-04 01:42:13 +00002165 case Expr::CallExprClass: {
2166 const CallExpr *CE = cast<CallExpr>(E);
John McCall5a7e6f72011-04-28 02:52:03 +00002167
2168 // <expression> ::= cp <simple-id> <expression>* E
2169 // We use this mangling only when the call would use ADL except
2170 // for being parenthesized. Per discussion with David
2171 // Vandervoorde, 2011.04.25.
2172 if (isParenthesizedADLCallee(CE)) {
2173 Out << "cp";
2174 // The callee here is a parenthesized UnresolvedLookupExpr with
2175 // no qualifier and should always get mangled as a <simple-id>
2176 // anyway.
2177
2178 // <expression> ::= cl <expression>* E
2179 } else {
2180 Out << "cl";
2181 }
2182
John McCall5e1e89b2010-08-18 19:18:59 +00002183 mangleExpression(CE->getCallee(), CE->getNumArgs());
John McCall1dd73832010-02-04 01:42:13 +00002184 for (unsigned I = 0, N = CE->getNumArgs(); I != N; ++I)
2185 mangleExpression(CE->getArg(I));
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002186 Out << 'E';
John McCall09cc1412010-02-03 00:55:45 +00002187 break;
John McCall1dd73832010-02-04 01:42:13 +00002188 }
John McCall09cc1412010-02-03 00:55:45 +00002189
John McCall0512e482010-07-14 04:20:34 +00002190 case Expr::CXXNewExprClass: {
2191 // Proposal from David Vandervoorde, 2010.06.30
2192 const CXXNewExpr *New = cast<CXXNewExpr>(E);
2193 if (New->isGlobalNew()) Out << "gs";
2194 Out << (New->isArray() ? "na" : "nw");
2195 for (CXXNewExpr::const_arg_iterator I = New->placement_arg_begin(),
2196 E = New->placement_arg_end(); I != E; ++I)
2197 mangleExpression(*I);
2198 Out << '_';
2199 mangleType(New->getAllocatedType());
2200 if (New->hasInitializer()) {
2201 Out << "pi";
2202 for (CXXNewExpr::const_arg_iterator I = New->constructor_arg_begin(),
2203 E = New->constructor_arg_end(); I != E; ++I)
2204 mangleExpression(*I);
2205 }
2206 Out << 'E';
2207 break;
2208 }
2209
John McCall2f27bf82010-02-04 02:56:29 +00002210 case Expr::MemberExprClass: {
2211 const MemberExpr *ME = cast<MemberExpr>(E);
2212 mangleMemberExpr(ME->getBase(), ME->isArrow(),
John McCalla0ce15c2011-04-24 08:23:24 +00002213 ME->getQualifier(), 0, ME->getMemberDecl()->getDeclName(),
John McCall5e1e89b2010-08-18 19:18:59 +00002214 Arity);
John McCall2f27bf82010-02-04 02:56:29 +00002215 break;
2216 }
2217
2218 case Expr::UnresolvedMemberExprClass: {
2219 const UnresolvedMemberExpr *ME = cast<UnresolvedMemberExpr>(E);
2220 mangleMemberExpr(ME->getBase(), ME->isArrow(),
John McCalla0ce15c2011-04-24 08:23:24 +00002221 ME->getQualifier(), 0, ME->getMemberName(),
John McCall5e1e89b2010-08-18 19:18:59 +00002222 Arity);
John McCall6dbce192010-08-20 00:17:19 +00002223 if (ME->hasExplicitTemplateArgs())
2224 mangleTemplateArgs(ME->getExplicitTemplateArgs());
John McCall2f27bf82010-02-04 02:56:29 +00002225 break;
2226 }
2227
2228 case Expr::CXXDependentScopeMemberExprClass: {
2229 const CXXDependentScopeMemberExpr *ME
2230 = cast<CXXDependentScopeMemberExpr>(E);
2231 mangleMemberExpr(ME->getBase(), ME->isArrow(),
John McCalla0ce15c2011-04-24 08:23:24 +00002232 ME->getQualifier(), ME->getFirstQualifierFoundInScope(),
2233 ME->getMember(), Arity);
John McCall6dbce192010-08-20 00:17:19 +00002234 if (ME->hasExplicitTemplateArgs())
2235 mangleTemplateArgs(ME->getExplicitTemplateArgs());
John McCall2f27bf82010-02-04 02:56:29 +00002236 break;
2237 }
2238
John McCall1dd73832010-02-04 01:42:13 +00002239 case Expr::UnresolvedLookupExprClass: {
2240 const UnresolvedLookupExpr *ULE = cast<UnresolvedLookupExpr>(E);
John McCalla0ce15c2011-04-24 08:23:24 +00002241 mangleUnresolvedName(ULE->getQualifier(), 0, ULE->getName(), Arity);
John McCall6dbce192010-08-20 00:17:19 +00002242 if (ULE->hasExplicitTemplateArgs())
2243 mangleTemplateArgs(ULE->getExplicitTemplateArgs());
John McCall1dd73832010-02-04 01:42:13 +00002244 break;
2245 }
2246
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002247 case Expr::CXXUnresolvedConstructExprClass: {
John McCall1dd73832010-02-04 01:42:13 +00002248 const CXXUnresolvedConstructExpr *CE = cast<CXXUnresolvedConstructExpr>(E);
2249 unsigned N = CE->arg_size();
2250
2251 Out << "cv";
2252 mangleType(CE->getType());
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002253 if (N != 1) Out << '_';
John McCall1dd73832010-02-04 01:42:13 +00002254 for (unsigned I = 0; I != N; ++I) mangleExpression(CE->getArg(I));
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002255 if (N != 1) Out << 'E';
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002256 break;
John McCall1dd73832010-02-04 01:42:13 +00002257 }
John McCall09cc1412010-02-03 00:55:45 +00002258
John McCall1dd73832010-02-04 01:42:13 +00002259 case Expr::CXXTemporaryObjectExprClass:
2260 case Expr::CXXConstructExprClass: {
2261 const CXXConstructExpr *CE = cast<CXXConstructExpr>(E);
2262 unsigned N = CE->getNumArgs();
2263
2264 Out << "cv";
2265 mangleType(CE->getType());
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002266 if (N != 1) Out << '_';
John McCall1dd73832010-02-04 01:42:13 +00002267 for (unsigned I = 0; I != N; ++I) mangleExpression(CE->getArg(I));
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002268 if (N != 1) Out << 'E';
John McCall09cc1412010-02-03 00:55:45 +00002269 break;
John McCall1dd73832010-02-04 01:42:13 +00002270 }
2271
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +00002272 case Expr::UnaryExprOrTypeTraitExprClass: {
2273 const UnaryExprOrTypeTraitExpr *SAE = cast<UnaryExprOrTypeTraitExpr>(E);
2274 switch(SAE->getKind()) {
2275 case UETT_SizeOf:
2276 Out << 's';
2277 break;
2278 case UETT_AlignOf:
2279 Out << 'a';
2280 break;
2281 case UETT_VecStep:
2282 Diagnostic &Diags = Context.getDiags();
2283 unsigned DiagID = Diags.getCustomDiagID(Diagnostic::Error,
2284 "cannot yet mangle vec_step expression");
2285 Diags.Report(DiagID);
2286 return;
2287 }
John McCall1dd73832010-02-04 01:42:13 +00002288 if (SAE->isArgumentType()) {
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002289 Out << 't';
John McCall1dd73832010-02-04 01:42:13 +00002290 mangleType(SAE->getArgumentType());
2291 } else {
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002292 Out << 'z';
John McCall1dd73832010-02-04 01:42:13 +00002293 mangleExpression(SAE->getArgumentExpr());
2294 }
2295 break;
2296 }
Anders Carlssona7694082009-11-06 02:50:19 +00002297
John McCall0512e482010-07-14 04:20:34 +00002298 case Expr::CXXThrowExprClass: {
2299 const CXXThrowExpr *TE = cast<CXXThrowExpr>(E);
2300
2301 // Proposal from David Vandervoorde, 2010.06.30
2302 if (TE->getSubExpr()) {
2303 Out << "tw";
2304 mangleExpression(TE->getSubExpr());
2305 } else {
2306 Out << "tr";
2307 }
2308 break;
2309 }
2310
2311 case Expr::CXXTypeidExprClass: {
2312 const CXXTypeidExpr *TIE = cast<CXXTypeidExpr>(E);
2313
2314 // Proposal from David Vandervoorde, 2010.06.30
2315 if (TIE->isTypeOperand()) {
2316 Out << "ti";
2317 mangleType(TIE->getTypeOperand());
2318 } else {
2319 Out << "te";
2320 mangleExpression(TIE->getExprOperand());
2321 }
2322 break;
2323 }
2324
2325 case Expr::CXXDeleteExprClass: {
2326 const CXXDeleteExpr *DE = cast<CXXDeleteExpr>(E);
2327
2328 // Proposal from David Vandervoorde, 2010.06.30
2329 if (DE->isGlobalDelete()) Out << "gs";
2330 Out << (DE->isArrayForm() ? "da" : "dl");
2331 mangleExpression(DE->getArgument());
2332 break;
2333 }
2334
Anders Carlssone170ba72009-12-14 01:45:37 +00002335 case Expr::UnaryOperatorClass: {
2336 const UnaryOperator *UO = cast<UnaryOperator>(E);
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002337 mangleOperatorName(UnaryOperator::getOverloadedOperator(UO->getOpcode()),
Anders Carlssone170ba72009-12-14 01:45:37 +00002338 /*Arity=*/1);
2339 mangleExpression(UO->getSubExpr());
2340 break;
2341 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002342
John McCall0512e482010-07-14 04:20:34 +00002343 case Expr::ArraySubscriptExprClass: {
2344 const ArraySubscriptExpr *AE = cast<ArraySubscriptExpr>(E);
2345
Chris Lattnerfc8f0e12011-04-15 05:22:18 +00002346 // Array subscript is treated as a syntactically weird form of
John McCall0512e482010-07-14 04:20:34 +00002347 // binary operator.
2348 Out << "ix";
2349 mangleExpression(AE->getLHS());
2350 mangleExpression(AE->getRHS());
2351 break;
2352 }
2353
2354 case Expr::CompoundAssignOperatorClass: // fallthrough
Anders Carlssone170ba72009-12-14 01:45:37 +00002355 case Expr::BinaryOperatorClass: {
2356 const BinaryOperator *BO = cast<BinaryOperator>(E);
Douglas Gregor63f62df2011-06-05 05:27:58 +00002357 if (BO->getOpcode() == BO_PtrMemD)
2358 Out << "ds";
2359 else
2360 mangleOperatorName(BinaryOperator::getOverloadedOperator(BO->getOpcode()),
2361 /*Arity=*/2);
Anders Carlssone170ba72009-12-14 01:45:37 +00002362 mangleExpression(BO->getLHS());
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002363 mangleExpression(BO->getRHS());
Anders Carlssone170ba72009-12-14 01:45:37 +00002364 break;
John McCall2f27bf82010-02-04 02:56:29 +00002365 }
Anders Carlssone170ba72009-12-14 01:45:37 +00002366
2367 case Expr::ConditionalOperatorClass: {
2368 const ConditionalOperator *CO = cast<ConditionalOperator>(E);
2369 mangleOperatorName(OO_Conditional, /*Arity=*/3);
2370 mangleExpression(CO->getCond());
John McCall5e1e89b2010-08-18 19:18:59 +00002371 mangleExpression(CO->getLHS(), Arity);
2372 mangleExpression(CO->getRHS(), Arity);
Anders Carlssone170ba72009-12-14 01:45:37 +00002373 break;
2374 }
2375
Douglas Gregor46287c72010-01-29 16:37:09 +00002376 case Expr::ImplicitCastExprClass: {
John McCall5e1e89b2010-08-18 19:18:59 +00002377 mangleExpression(cast<ImplicitCastExpr>(E)->getSubExpr(), Arity);
Douglas Gregor46287c72010-01-29 16:37:09 +00002378 break;
2379 }
John McCallf85e1932011-06-15 23:02:42 +00002380
2381 case Expr::ObjCBridgedCastExprClass: {
2382 // Mangle ownership casts as a vendor extended operator __bridge,
2383 // __bridge_transfer, or __bridge_retain.
2384 llvm::StringRef Kind = cast<ObjCBridgedCastExpr>(E)->getBridgeKindName();
2385 Out << "v1U" << Kind.size() << Kind;
2386 }
2387 // Fall through to mangle the cast itself.
2388
Douglas Gregor46287c72010-01-29 16:37:09 +00002389 case Expr::CStyleCastExprClass:
2390 case Expr::CXXStaticCastExprClass:
2391 case Expr::CXXDynamicCastExprClass:
2392 case Expr::CXXReinterpretCastExprClass:
2393 case Expr::CXXConstCastExprClass:
2394 case Expr::CXXFunctionalCastExprClass: {
2395 const ExplicitCastExpr *ECE = cast<ExplicitCastExpr>(E);
2396 Out << "cv";
2397 mangleType(ECE->getType());
2398 mangleExpression(ECE->getSubExpr());
2399 break;
2400 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002401
Anders Carlsson58040a52009-12-16 05:48:46 +00002402 case Expr::CXXOperatorCallExprClass: {
2403 const CXXOperatorCallExpr *CE = cast<CXXOperatorCallExpr>(E);
2404 unsigned NumArgs = CE->getNumArgs();
2405 mangleOperatorName(CE->getOperator(), /*Arity=*/NumArgs);
2406 // Mangle the arguments.
2407 for (unsigned i = 0; i != NumArgs; ++i)
2408 mangleExpression(CE->getArg(i));
2409 break;
2410 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002411
Anders Carlssona7694082009-11-06 02:50:19 +00002412 case Expr::ParenExprClass:
John McCall5e1e89b2010-08-18 19:18:59 +00002413 mangleExpression(cast<ParenExpr>(E)->getSubExpr(), Arity);
Anders Carlssona7694082009-11-06 02:50:19 +00002414 break;
2415
Anders Carlssond553f8c2009-09-21 01:21:10 +00002416 case Expr::DeclRefExprClass: {
Douglas Gregor5ed1bc32010-02-28 21:40:32 +00002417 const NamedDecl *D = cast<DeclRefExpr>(E)->getDecl();
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002418
Anders Carlssond553f8c2009-09-21 01:21:10 +00002419 switch (D->getKind()) {
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002420 default:
Douglas Gregor5ed1bc32010-02-28 21:40:32 +00002421 // <expr-primary> ::= L <mangled-name> E # external name
2422 Out << 'L';
2423 mangle(D, "_Z");
2424 Out << 'E';
2425 break;
2426
John McCallfb44de92011-05-01 22:35:37 +00002427 case Decl::ParmVar:
2428 mangleFunctionParam(cast<ParmVarDecl>(D));
2429 break;
2430
John McCall3dc7e7b2010-07-24 01:17:35 +00002431 case Decl::EnumConstant: {
2432 const EnumConstantDecl *ED = cast<EnumConstantDecl>(D);
2433 mangleIntegerLiteral(ED->getType(), ED->getInitVal());
2434 break;
2435 }
2436
Anders Carlssond553f8c2009-09-21 01:21:10 +00002437 case Decl::NonTypeTemplateParm: {
2438 const NonTypeTemplateParmDecl *PD = cast<NonTypeTemplateParmDecl>(D);
Anders Carlsson0ccdf8d2009-09-27 00:38:53 +00002439 mangleTemplateParameter(PD->getIndex());
Anders Carlssond553f8c2009-09-21 01:21:10 +00002440 break;
2441 }
2442
2443 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002444
Anders Carlsson50755b02009-09-27 20:11:34 +00002445 break;
Anders Carlssond553f8c2009-09-21 01:21:10 +00002446 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002447
Douglas Gregorc7793c72011-01-15 01:15:58 +00002448 case Expr::SubstNonTypeTemplateParmPackExprClass:
2449 mangleTemplateParameter(
2450 cast<SubstNonTypeTemplateParmPackExpr>(E)->getParameterPack()->getIndex());
2451 break;
2452
John McCall865d4472009-11-19 22:55:06 +00002453 case Expr::DependentScopeDeclRefExprClass: {
2454 const DependentScopeDeclRefExpr *DRE = cast<DependentScopeDeclRefExpr>(E);
Douglas Gregor4b2ccfc2010-02-28 22:05:49 +00002455 NestedNameSpecifier *NNS = DRE->getQualifier();
2456 const Type *QTy = NNS->getAsType();
2457
2458 // When we're dealing with a nested-name-specifier that has just a
2459 // dependent identifier in it, mangle that as a typename. FIXME:
2460 // It isn't clear that we ever actually want to have such a
2461 // nested-name-specifier; why not just represent it as a typename type?
2462 if (!QTy && NNS->getAsIdentifier() && NNS->getPrefix()) {
Douglas Gregor4a2023f2010-03-31 20:19:30 +00002463 QTy = getASTContext().getDependentNameType(ETK_Typename,
2464 NNS->getPrefix(),
2465 NNS->getAsIdentifier())
Douglas Gregor4b2ccfc2010-02-28 22:05:49 +00002466 .getTypePtr();
2467 }
Anders Carlsson50755b02009-09-27 20:11:34 +00002468 assert(QTy && "Qualifier was not type!");
2469
John McCall6dbce192010-08-20 00:17:19 +00002470 // ::= sr <type> <unqualified-name> # dependent name
2471 // ::= sr <type> <unqualified-name> <template-args> # dependent template-id
Anders Carlsson50755b02009-09-27 20:11:34 +00002472 Out << "sr";
2473 mangleType(QualType(QTy, 0));
John McCall5e1e89b2010-08-18 19:18:59 +00002474 mangleUnqualifiedName(0, DRE->getDeclName(), Arity);
John McCall6dbce192010-08-20 00:17:19 +00002475 if (DRE->hasExplicitTemplateArgs())
2476 mangleTemplateArgs(DRE->getExplicitTemplateArgs());
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002477
Anders Carlsson50755b02009-09-27 20:11:34 +00002478 break;
2479 }
2480
John McCalld9307602010-04-09 22:54:09 +00002481 case Expr::CXXBindTemporaryExprClass:
2482 mangleExpression(cast<CXXBindTemporaryExpr>(E)->getSubExpr());
2483 break;
2484
John McCall4765fa02010-12-06 08:20:24 +00002485 case Expr::ExprWithCleanupsClass:
2486 mangleExpression(cast<ExprWithCleanups>(E)->getSubExpr(), Arity);
John McCalld9307602010-04-09 22:54:09 +00002487 break;
2488
John McCall1dd73832010-02-04 01:42:13 +00002489 case Expr::FloatingLiteralClass: {
2490 const FloatingLiteral *FL = cast<FloatingLiteral>(E);
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002491 Out << 'L';
John McCall1dd73832010-02-04 01:42:13 +00002492 mangleType(FL->getType());
John McCall0512e482010-07-14 04:20:34 +00002493 mangleFloat(FL->getValue());
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002494 Out << 'E';
John McCall1dd73832010-02-04 01:42:13 +00002495 break;
2496 }
2497
John McCallde810632010-04-09 21:48:08 +00002498 case Expr::CharacterLiteralClass:
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002499 Out << 'L';
John McCallde810632010-04-09 21:48:08 +00002500 mangleType(E->getType());
2501 Out << cast<CharacterLiteral>(E)->getValue();
2502 Out << 'E';
2503 break;
2504
2505 case Expr::CXXBoolLiteralExprClass:
2506 Out << "Lb";
2507 Out << (cast<CXXBoolLiteralExpr>(E)->getValue() ? '1' : '0');
2508 Out << 'E';
2509 break;
2510
John McCall0512e482010-07-14 04:20:34 +00002511 case Expr::IntegerLiteralClass: {
2512 llvm::APSInt Value(cast<IntegerLiteral>(E)->getValue());
2513 if (E->getType()->isSignedIntegerType())
2514 Value.setIsSigned(true);
2515 mangleIntegerLiteral(E->getType(), Value);
Anders Carlssone170ba72009-12-14 01:45:37 +00002516 break;
John McCall0512e482010-07-14 04:20:34 +00002517 }
2518
2519 case Expr::ImaginaryLiteralClass: {
2520 const ImaginaryLiteral *IE = cast<ImaginaryLiteral>(E);
2521 // Mangle as if a complex literal.
Nick Lewycky271b6652010-09-05 03:40:33 +00002522 // Proposal from David Vandevoorde, 2010.06.30.
John McCall0512e482010-07-14 04:20:34 +00002523 Out << 'L';
2524 mangleType(E->getType());
2525 if (const FloatingLiteral *Imag =
2526 dyn_cast<FloatingLiteral>(IE->getSubExpr())) {
2527 // Mangle a floating-point zero of the appropriate type.
2528 mangleFloat(llvm::APFloat(Imag->getValue().getSemantics()));
2529 Out << '_';
2530 mangleFloat(Imag->getValue());
2531 } else {
Nick Lewycky271b6652010-09-05 03:40:33 +00002532 Out << "0_";
John McCall0512e482010-07-14 04:20:34 +00002533 llvm::APSInt Value(cast<IntegerLiteral>(IE->getSubExpr())->getValue());
2534 if (IE->getSubExpr()->getType()->isSignedIntegerType())
2535 Value.setIsSigned(true);
2536 mangleNumber(Value);
2537 }
2538 Out << 'E';
2539 break;
2540 }
2541
2542 case Expr::StringLiteralClass: {
John McCall1658c392010-07-15 21:53:03 +00002543 // Revised proposal from David Vandervoorde, 2010.07.15.
John McCall0512e482010-07-14 04:20:34 +00002544 Out << 'L';
John McCall1658c392010-07-15 21:53:03 +00002545 assert(isa<ConstantArrayType>(E->getType()));
2546 mangleType(E->getType());
John McCall0512e482010-07-14 04:20:34 +00002547 Out << 'E';
2548 break;
2549 }
2550
2551 case Expr::GNUNullExprClass:
2552 // FIXME: should this really be mangled the same as nullptr?
2553 // fallthrough
2554
2555 case Expr::CXXNullPtrLiteralExprClass: {
2556 // Proposal from David Vandervoorde, 2010.06.30, as
2557 // modified by ABI list discussion.
2558 Out << "LDnE";
2559 break;
2560 }
Douglas Gregorbe230c32011-01-03 17:17:50 +00002561
2562 case Expr::PackExpansionExprClass:
2563 Out << "sp";
2564 mangleExpression(cast<PackExpansionExpr>(E)->getPattern());
2565 break;
Douglas Gregor2e774c42011-01-04 18:56:13 +00002566
2567 case Expr::SizeOfPackExprClass: {
Douglas Gregor2e774c42011-01-04 18:56:13 +00002568 Out << "sZ";
2569 const NamedDecl *Pack = cast<SizeOfPackExpr>(E)->getPack();
2570 if (const TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(Pack))
2571 mangleTemplateParameter(TTP->getIndex());
2572 else if (const NonTypeTemplateParmDecl *NTTP
2573 = dyn_cast<NonTypeTemplateParmDecl>(Pack))
2574 mangleTemplateParameter(NTTP->getIndex());
2575 else if (const TemplateTemplateParmDecl *TempTP
2576 = dyn_cast<TemplateTemplateParmDecl>(Pack))
2577 mangleTemplateParameter(TempTP->getIndex());
2578 else {
Douglas Gregor4fc48662011-01-13 16:39:34 +00002579 // Note: proposed by Mike Herrick on 11/30/10
2580 // <expression> ::= sZ <function-param> # size of function parameter pack
Douglas Gregor2e774c42011-01-04 18:56:13 +00002581 Diagnostic &Diags = Context.getDiags();
2582 unsigned DiagID = Diags.getCustomDiagID(Diagnostic::Error,
2583 "cannot mangle sizeof...(function parameter pack)");
2584 Diags.Report(DiagID);
2585 return;
2586 }
Douglas Gregordfbbcf92011-03-03 02:20:19 +00002587 break;
Douglas Gregor2e774c42011-01-04 18:56:13 +00002588 }
Anders Carlssond553f8c2009-09-21 01:21:10 +00002589 }
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00002590}
2591
John McCallfb44de92011-05-01 22:35:37 +00002592/// Mangle an expression which refers to a parameter variable.
2593///
2594/// <expression> ::= <function-param>
2595/// <function-param> ::= fp <top-level CV-qualifiers> _ # L == 0, I == 0
2596/// <function-param> ::= fp <top-level CV-qualifiers>
2597/// <parameter-2 non-negative number> _ # L == 0, I > 0
2598/// <function-param> ::= fL <L-1 non-negative number>
2599/// p <top-level CV-qualifiers> _ # L > 0, I == 0
2600/// <function-param> ::= fL <L-1 non-negative number>
2601/// p <top-level CV-qualifiers>
2602/// <I-1 non-negative number> _ # L > 0, I > 0
2603///
2604/// L is the nesting depth of the parameter, defined as 1 if the
2605/// parameter comes from the innermost function prototype scope
2606/// enclosing the current context, 2 if from the next enclosing
2607/// function prototype scope, and so on, with one special case: if
2608/// we've processed the full parameter clause for the innermost
2609/// function type, then L is one less. This definition conveniently
2610/// makes it irrelevant whether a function's result type was written
2611/// trailing or leading, but is otherwise overly complicated; the
2612/// numbering was first designed without considering references to
2613/// parameter in locations other than return types, and then the
2614/// mangling had to be generalized without changing the existing
2615/// manglings.
2616///
2617/// I is the zero-based index of the parameter within its parameter
2618/// declaration clause. Note that the original ABI document describes
2619/// this using 1-based ordinals.
2620void CXXNameMangler::mangleFunctionParam(const ParmVarDecl *parm) {
2621 unsigned parmDepth = parm->getFunctionScopeDepth();
2622 unsigned parmIndex = parm->getFunctionScopeIndex();
2623
2624 // Compute 'L'.
2625 // parmDepth does not include the declaring function prototype.
2626 // FunctionTypeDepth does account for that.
2627 assert(parmDepth < FunctionTypeDepth.getDepth());
2628 unsigned nestingDepth = FunctionTypeDepth.getDepth() - parmDepth;
2629 if (FunctionTypeDepth.isInResultType())
2630 nestingDepth--;
2631
2632 if (nestingDepth == 0) {
2633 Out << "fp";
2634 } else {
2635 Out << "fL" << (nestingDepth - 1) << 'p';
2636 }
2637
2638 // Top-level qualifiers. We don't have to worry about arrays here,
2639 // because parameters declared as arrays should already have been
2640 // tranformed to have pointer type. FIXME: apparently these don't
2641 // get mangled if used as an rvalue of a known non-class type?
2642 assert(!parm->getType()->isArrayType()
2643 && "parameter's type is still an array type?");
2644 mangleQualifiers(parm->getType().getQualifiers());
2645
2646 // Parameter index.
2647 if (parmIndex != 0) {
2648 Out << (parmIndex - 1);
2649 }
2650 Out << '_';
2651}
2652
Anders Carlsson3ac86b52009-04-15 05:36:58 +00002653void CXXNameMangler::mangleCXXCtorType(CXXCtorType T) {
2654 // <ctor-dtor-name> ::= C1 # complete object constructor
2655 // ::= C2 # base object constructor
2656 // ::= C3 # complete object allocating constructor
2657 //
2658 switch (T) {
2659 case Ctor_Complete:
2660 Out << "C1";
2661 break;
2662 case Ctor_Base:
2663 Out << "C2";
2664 break;
2665 case Ctor_CompleteAllocating:
2666 Out << "C3";
2667 break;
2668 }
2669}
2670
Anders Carlsson27ae5362009-04-17 01:58:57 +00002671void CXXNameMangler::mangleCXXDtorType(CXXDtorType T) {
2672 // <ctor-dtor-name> ::= D0 # deleting destructor
2673 // ::= D1 # complete object destructor
2674 // ::= D2 # base object destructor
2675 //
2676 switch (T) {
2677 case Dtor_Deleting:
2678 Out << "D0";
2679 break;
2680 case Dtor_Complete:
2681 Out << "D1";
2682 break;
2683 case Dtor_Base:
2684 Out << "D2";
2685 break;
2686 }
2687}
2688
John McCall6dbce192010-08-20 00:17:19 +00002689void CXXNameMangler::mangleTemplateArgs(
2690 const ExplicitTemplateArgumentList &TemplateArgs) {
2691 // <template-args> ::= I <template-arg>+ E
2692 Out << 'I';
John McCall4f4e4132011-05-04 01:45:19 +00002693 for (unsigned i = 0, e = TemplateArgs.NumTemplateArgs; i != e; ++i)
2694 mangleTemplateArg(0, TemplateArgs.getTemplateArgs()[i].getArgument());
John McCall6dbce192010-08-20 00:17:19 +00002695 Out << 'E';
2696}
2697
Douglas Gregor20f0cc72010-04-23 03:10:43 +00002698void CXXNameMangler::mangleTemplateArgs(TemplateName Template,
2699 const TemplateArgument *TemplateArgs,
2700 unsigned NumTemplateArgs) {
2701 if (TemplateDecl *TD = Template.getAsTemplateDecl())
2702 return mangleTemplateArgs(*TD->getTemplateParameters(), TemplateArgs,
2703 NumTemplateArgs);
Sean Huntc3021132010-05-05 15:23:54 +00002704
John McCall4f4e4132011-05-04 01:45:19 +00002705 mangleUnresolvedTemplateArgs(TemplateArgs, NumTemplateArgs);
2706}
2707
2708void CXXNameMangler::mangleUnresolvedTemplateArgs(const TemplateArgument *args,
2709 unsigned numArgs) {
Douglas Gregor20f0cc72010-04-23 03:10:43 +00002710 // <template-args> ::= I <template-arg>+ E
2711 Out << 'I';
John McCall4f4e4132011-05-04 01:45:19 +00002712 for (unsigned i = 0; i != numArgs; ++i)
2713 mangleTemplateArg(0, args[i]);
Douglas Gregor20f0cc72010-04-23 03:10:43 +00002714 Out << 'E';
2715}
2716
Rafael Espindolad9800722010-03-11 14:07:00 +00002717void CXXNameMangler::mangleTemplateArgs(const TemplateParameterList &PL,
2718 const TemplateArgumentList &AL) {
Anders Carlsson7a0ba872009-05-15 16:09:15 +00002719 // <template-args> ::= I <template-arg>+ E
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002720 Out << 'I';
Rafael Espindolad9800722010-03-11 14:07:00 +00002721 for (unsigned i = 0, e = AL.size(); i != e; ++i)
2722 mangleTemplateArg(PL.getParam(i), AL[i]);
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002723 Out << 'E';
Anders Carlsson7a0ba872009-05-15 16:09:15 +00002724}
2725
Rafael Espindolad9800722010-03-11 14:07:00 +00002726void CXXNameMangler::mangleTemplateArgs(const TemplateParameterList &PL,
2727 const TemplateArgument *TemplateArgs,
Anders Carlsson7624f212009-09-18 02:42:01 +00002728 unsigned NumTemplateArgs) {
2729 // <template-args> ::= I <template-arg>+ E
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002730 Out << 'I';
Daniel Dunbar7e0c1952009-11-21 09:17:15 +00002731 for (unsigned i = 0; i != NumTemplateArgs; ++i)
Rafael Espindolad9800722010-03-11 14:07:00 +00002732 mangleTemplateArg(PL.getParam(i), TemplateArgs[i]);
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002733 Out << 'E';
Anders Carlsson7624f212009-09-18 02:42:01 +00002734}
2735
Rafael Espindolad9800722010-03-11 14:07:00 +00002736void CXXNameMangler::mangleTemplateArg(const NamedDecl *P,
2737 const TemplateArgument &A) {
Mike Stump1eb44332009-09-09 15:08:12 +00002738 // <template-arg> ::= <type> # type or template
Anders Carlsson7a0ba872009-05-15 16:09:15 +00002739 // ::= X <expression> E # expression
2740 // ::= <expr-primary> # simple expressions
Douglas Gregor4fc48662011-01-13 16:39:34 +00002741 // ::= J <template-arg>* E # argument pack
Anders Carlsson7a0ba872009-05-15 16:09:15 +00002742 // ::= sp <expression> # pack expansion of (C++0x)
2743 switch (A.getKind()) {
Douglas Gregorf90b27a2011-01-03 22:36:02 +00002744 case TemplateArgument::Null:
2745 llvm_unreachable("Cannot mangle NULL template argument");
2746
Anders Carlsson7a0ba872009-05-15 16:09:15 +00002747 case TemplateArgument::Type:
2748 mangleType(A.getAsType());
2749 break;
Anders Carlsson9e85c742009-12-23 19:30:55 +00002750 case TemplateArgument::Template:
John McCallb6f532e2010-07-14 06:43:17 +00002751 // This is mangled as <type>.
2752 mangleType(A.getAsTemplate());
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002753 break;
Douglas Gregora7fc9012011-01-05 18:58:31 +00002754 case TemplateArgument::TemplateExpansion:
Douglas Gregor4fc48662011-01-13 16:39:34 +00002755 // <type> ::= Dp <type> # pack expansion (C++0x)
Douglas Gregora7fc9012011-01-05 18:58:31 +00002756 Out << "Dp";
2757 mangleType(A.getAsTemplateOrTemplatePattern());
2758 break;
Anders Carlssond553f8c2009-09-21 01:21:10 +00002759 case TemplateArgument::Expression:
2760 Out << 'X';
2761 mangleExpression(A.getAsExpr());
2762 Out << 'E';
2763 break;
Anders Carlssone170ba72009-12-14 01:45:37 +00002764 case TemplateArgument::Integral:
2765 mangleIntegerLiteral(A.getIntegralType(), *A.getAsIntegral());
Anders Carlsson7a0ba872009-05-15 16:09:15 +00002766 break;
Daniel Dunbar7e0c1952009-11-21 09:17:15 +00002767 case TemplateArgument::Declaration: {
Douglas Gregor20f0cc72010-04-23 03:10:43 +00002768 assert(P && "Missing template parameter for declaration argument");
Daniel Dunbar7e0c1952009-11-21 09:17:15 +00002769 // <expr-primary> ::= L <mangled-name> E # external name
2770
Rafael Espindolad9800722010-03-11 14:07:00 +00002771 // Clang produces AST's where pointer-to-member-function expressions
Daniel Dunbar7e0c1952009-11-21 09:17:15 +00002772 // and pointer-to-function expressions are represented as a declaration not
Rafael Espindolad9800722010-03-11 14:07:00 +00002773 // an expression. We compensate for it here to produce the correct mangling.
2774 NamedDecl *D = cast<NamedDecl>(A.getAsDecl());
2775 const NonTypeTemplateParmDecl *Parameter = cast<NonTypeTemplateParmDecl>(P);
John McCallc0a45592011-04-24 08:43:07 +00002776 bool compensateMangling = !Parameter->getType()->isReferenceType();
Rafael Espindolad9800722010-03-11 14:07:00 +00002777 if (compensateMangling) {
2778 Out << 'X';
2779 mangleOperatorName(OO_Amp, 1);
2780 }
2781
Daniel Dunbar7e0c1952009-11-21 09:17:15 +00002782 Out << 'L';
2783 // References to external entities use the mangled name; if the name would
2784 // not normally be manged then mangle it as unqualified.
2785 //
2786 // FIXME: The ABI specifies that external names here should have _Z, but
2787 // gcc leaves this off.
Rafael Espindolad9800722010-03-11 14:07:00 +00002788 if (compensateMangling)
2789 mangle(D, "_Z");
2790 else
2791 mangle(D, "Z");
Daniel Dunbar7e0c1952009-11-21 09:17:15 +00002792 Out << 'E';
Rafael Espindolad9800722010-03-11 14:07:00 +00002793
2794 if (compensateMangling)
2795 Out << 'E';
2796
Daniel Dunbar7e0c1952009-11-21 09:17:15 +00002797 break;
2798 }
Douglas Gregorf90b27a2011-01-03 22:36:02 +00002799
2800 case TemplateArgument::Pack: {
2801 // Note: proposal by Mike Herrick on 12/20/10
2802 Out << 'J';
2803 for (TemplateArgument::pack_iterator PA = A.pack_begin(),
2804 PAEnd = A.pack_end();
2805 PA != PAEnd; ++PA)
2806 mangleTemplateArg(P, *PA);
2807 Out << 'E';
2808 }
Daniel Dunbar7e0c1952009-11-21 09:17:15 +00002809 }
Anders Carlsson7a0ba872009-05-15 16:09:15 +00002810}
2811
Anders Carlsson0ccdf8d2009-09-27 00:38:53 +00002812void CXXNameMangler::mangleTemplateParameter(unsigned Index) {
2813 // <template-param> ::= T_ # first template parameter
2814 // ::= T <parameter-2 non-negative number> _
2815 if (Index == 0)
2816 Out << "T_";
2817 else
2818 Out << 'T' << (Index - 1) << '_';
2819}
2820
Anders Carlsson76967372009-09-17 00:43:46 +00002821// <substitution> ::= S <seq-id> _
2822// ::= S_
Anders Carlsson6862fc72009-09-17 04:16:28 +00002823bool CXXNameMangler::mangleSubstitution(const NamedDecl *ND) {
Anders Carlssone7c8cb62009-09-26 20:53:44 +00002824 // Try one of the standard substitutions first.
2825 if (mangleStandardSubstitution(ND))
2826 return true;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002827
Anders Carlsson433d1372009-11-07 04:26:04 +00002828 ND = cast<NamedDecl>(ND->getCanonicalDecl());
Anders Carlsson6862fc72009-09-17 04:16:28 +00002829 return mangleSubstitution(reinterpret_cast<uintptr_t>(ND));
2830}
2831
Anders Carlsson76967372009-09-17 00:43:46 +00002832bool CXXNameMangler::mangleSubstitution(QualType T) {
Anders Carlssond99edc42009-09-26 03:55:37 +00002833 if (!T.getCVRQualifiers()) {
2834 if (const RecordType *RT = T->getAs<RecordType>())
2835 return mangleSubstitution(RT->getDecl());
2836 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002837
Anders Carlsson76967372009-09-17 00:43:46 +00002838 uintptr_t TypePtr = reinterpret_cast<uintptr_t>(T.getAsOpaquePtr());
2839
Anders Carlssond3a932a2009-09-17 03:53:28 +00002840 return mangleSubstitution(TypePtr);
2841}
2842
Douglas Gregor1e9268e2010-04-28 05:58:56 +00002843bool CXXNameMangler::mangleSubstitution(TemplateName Template) {
2844 if (TemplateDecl *TD = Template.getAsTemplateDecl())
2845 return mangleSubstitution(TD);
Sean Huntc3021132010-05-05 15:23:54 +00002846
Douglas Gregor1e9268e2010-04-28 05:58:56 +00002847 Template = Context.getASTContext().getCanonicalTemplateName(Template);
2848 return mangleSubstitution(
2849 reinterpret_cast<uintptr_t>(Template.getAsVoidPointer()));
2850}
2851
Anders Carlssond3a932a2009-09-17 03:53:28 +00002852bool CXXNameMangler::mangleSubstitution(uintptr_t Ptr) {
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002853 llvm::DenseMap<uintptr_t, unsigned>::iterator I = Substitutions.find(Ptr);
Anders Carlsson76967372009-09-17 00:43:46 +00002854 if (I == Substitutions.end())
2855 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002856
Anders Carlsson76967372009-09-17 00:43:46 +00002857 unsigned SeqID = I->second;
2858 if (SeqID == 0)
2859 Out << "S_";
2860 else {
2861 SeqID--;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002862
Anders Carlsson76967372009-09-17 00:43:46 +00002863 // <seq-id> is encoded in base-36, using digits and upper case letters.
2864 char Buffer[10];
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002865 char *BufferPtr = llvm::array_endof(Buffer);
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002866
Anders Carlsson76967372009-09-17 00:43:46 +00002867 if (SeqID == 0) *--BufferPtr = '0';
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002868
Anders Carlsson76967372009-09-17 00:43:46 +00002869 while (SeqID) {
2870 assert(BufferPtr > Buffer && "Buffer overflow!");
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002871
John McCall6ab30e02010-06-09 07:26:17 +00002872 char c = static_cast<char>(SeqID % 36);
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002873
Anders Carlsson76967372009-09-17 00:43:46 +00002874 *--BufferPtr = (c < 10 ? '0' + c : 'A' + c - 10);
2875 SeqID /= 36;
2876 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002877
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002878 Out << 'S'
2879 << llvm::StringRef(BufferPtr, llvm::array_endof(Buffer)-BufferPtr)
2880 << '_';
Anders Carlsson76967372009-09-17 00:43:46 +00002881 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002882
Anders Carlsson76967372009-09-17 00:43:46 +00002883 return true;
2884}
2885
Anders Carlssonf514b542009-09-27 00:12:57 +00002886static bool isCharType(QualType T) {
2887 if (T.isNull())
2888 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002889
Anders Carlssonf514b542009-09-27 00:12:57 +00002890 return T->isSpecificBuiltinType(BuiltinType::Char_S) ||
2891 T->isSpecificBuiltinType(BuiltinType::Char_U);
2892}
2893
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002894/// isCharSpecialization - Returns whether a given type is a template
Anders Carlssonf514b542009-09-27 00:12:57 +00002895/// specialization of a given name with a single argument of type char.
2896static bool isCharSpecialization(QualType T, const char *Name) {
2897 if (T.isNull())
2898 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002899
Anders Carlssonf514b542009-09-27 00:12:57 +00002900 const RecordType *RT = T->getAs<RecordType>();
2901 if (!RT)
2902 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002903
2904 const ClassTemplateSpecializationDecl *SD =
Anders Carlssonf514b542009-09-27 00:12:57 +00002905 dyn_cast<ClassTemplateSpecializationDecl>(RT->getDecl());
2906 if (!SD)
2907 return false;
2908
2909 if (!isStdNamespace(SD->getDeclContext()))
2910 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002911
Anders Carlssonf514b542009-09-27 00:12:57 +00002912 const TemplateArgumentList &TemplateArgs = SD->getTemplateArgs();
2913 if (TemplateArgs.size() != 1)
2914 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002915
Anders Carlssonf514b542009-09-27 00:12:57 +00002916 if (!isCharType(TemplateArgs[0].getAsType()))
2917 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002918
Daniel Dunbar01eb9b92009-10-18 21:17:35 +00002919 return SD->getIdentifier()->getName() == Name;
Anders Carlssonf514b542009-09-27 00:12:57 +00002920}
2921
Anders Carlsson91f88602009-12-07 19:56:42 +00002922template <std::size_t StrLen>
Benjamin Kramer54353f42010-11-25 18:29:30 +00002923static bool isStreamCharSpecialization(const ClassTemplateSpecializationDecl*SD,
2924 const char (&Str)[StrLen]) {
Anders Carlsson91f88602009-12-07 19:56:42 +00002925 if (!SD->getIdentifier()->isStr(Str))
2926 return false;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002927
Anders Carlsson91f88602009-12-07 19:56:42 +00002928 const TemplateArgumentList &TemplateArgs = SD->getTemplateArgs();
2929 if (TemplateArgs.size() != 2)
2930 return false;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002931
Anders Carlsson91f88602009-12-07 19:56:42 +00002932 if (!isCharType(TemplateArgs[0].getAsType()))
2933 return false;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002934
Anders Carlsson91f88602009-12-07 19:56:42 +00002935 if (!isCharSpecialization(TemplateArgs[1].getAsType(), "char_traits"))
2936 return false;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002937
Anders Carlsson91f88602009-12-07 19:56:42 +00002938 return true;
2939}
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002940
Anders Carlssone7c8cb62009-09-26 20:53:44 +00002941bool CXXNameMangler::mangleStandardSubstitution(const NamedDecl *ND) {
2942 // <substitution> ::= St # ::std::
Anders Carlsson8c031552009-09-26 23:10:05 +00002943 if (const NamespaceDecl *NS = dyn_cast<NamespaceDecl>(ND)) {
Anders Carlsson47846d22009-12-04 06:23:23 +00002944 if (isStd(NS)) {
Anders Carlsson8c031552009-09-26 23:10:05 +00002945 Out << "St";
2946 return true;
2947 }
2948 }
2949
2950 if (const ClassTemplateDecl *TD = dyn_cast<ClassTemplateDecl>(ND)) {
2951 if (!isStdNamespace(TD->getDeclContext()))
2952 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002953
Anders Carlsson8c031552009-09-26 23:10:05 +00002954 // <substitution> ::= Sa # ::std::allocator
2955 if (TD->getIdentifier()->isStr("allocator")) {
2956 Out << "Sa";
2957 return true;
2958 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002959
Anders Carlsson189d59c2009-09-26 23:14:39 +00002960 // <<substitution> ::= Sb # ::std::basic_string
2961 if (TD->getIdentifier()->isStr("basic_string")) {
2962 Out << "Sb";
2963 return true;
2964 }
Anders Carlsson8c031552009-09-26 23:10:05 +00002965 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002966
2967 if (const ClassTemplateSpecializationDecl *SD =
Anders Carlssonf514b542009-09-27 00:12:57 +00002968 dyn_cast<ClassTemplateSpecializationDecl>(ND)) {
Eli Friedman5370ee22010-02-23 18:25:09 +00002969 if (!isStdNamespace(SD->getDeclContext()))
2970 return false;
2971
Anders Carlssonf514b542009-09-27 00:12:57 +00002972 // <substitution> ::= Ss # ::std::basic_string<char,
2973 // ::std::char_traits<char>,
2974 // ::std::allocator<char> >
2975 if (SD->getIdentifier()->isStr("basic_string")) {
2976 const TemplateArgumentList &TemplateArgs = SD->getTemplateArgs();
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002977
Anders Carlssonf514b542009-09-27 00:12:57 +00002978 if (TemplateArgs.size() != 3)
2979 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002980
Anders Carlssonf514b542009-09-27 00:12:57 +00002981 if (!isCharType(TemplateArgs[0].getAsType()))
2982 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002983
Anders Carlssonf514b542009-09-27 00:12:57 +00002984 if (!isCharSpecialization(TemplateArgs[1].getAsType(), "char_traits"))
2985 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002986
Anders Carlssonf514b542009-09-27 00:12:57 +00002987 if (!isCharSpecialization(TemplateArgs[2].getAsType(), "allocator"))
2988 return false;
2989
2990 Out << "Ss";
2991 return true;
2992 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002993
Anders Carlsson91f88602009-12-07 19:56:42 +00002994 // <substitution> ::= Si # ::std::basic_istream<char,
2995 // ::std::char_traits<char> >
2996 if (isStreamCharSpecialization(SD, "basic_istream")) {
2997 Out << "Si";
2998 return true;
2999 }
3000
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00003001 // <substitution> ::= So # ::std::basic_ostream<char,
Anders Carlsson8f8fd8e2009-10-08 17:20:26 +00003002 // ::std::char_traits<char> >
Anders Carlsson91f88602009-12-07 19:56:42 +00003003 if (isStreamCharSpecialization(SD, "basic_ostream")) {
Anders Carlsson8f8fd8e2009-10-08 17:20:26 +00003004 Out << "So";
3005 return true;
3006 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00003007
Anders Carlsson91f88602009-12-07 19:56:42 +00003008 // <substitution> ::= Sd # ::std::basic_iostream<char,
3009 // ::std::char_traits<char> >
3010 if (isStreamCharSpecialization(SD, "basic_iostream")) {
3011 Out << "Sd";
3012 return true;
3013 }
Anders Carlssonf514b542009-09-27 00:12:57 +00003014 }
Anders Carlsson8c031552009-09-26 23:10:05 +00003015 return false;
Anders Carlssone7c8cb62009-09-26 20:53:44 +00003016}
3017
Anders Carlsson76967372009-09-17 00:43:46 +00003018void CXXNameMangler::addSubstitution(QualType T) {
Anders Carlssond99edc42009-09-26 03:55:37 +00003019 if (!T.getCVRQualifiers()) {
3020 if (const RecordType *RT = T->getAs<RecordType>()) {
3021 addSubstitution(RT->getDecl());
3022 return;
3023 }
3024 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00003025
Anders Carlsson76967372009-09-17 00:43:46 +00003026 uintptr_t TypePtr = reinterpret_cast<uintptr_t>(T.getAsOpaquePtr());
Anders Carlssond3a932a2009-09-17 03:53:28 +00003027 addSubstitution(TypePtr);
3028}
3029
Douglas Gregor1e9268e2010-04-28 05:58:56 +00003030void CXXNameMangler::addSubstitution(TemplateName Template) {
3031 if (TemplateDecl *TD = Template.getAsTemplateDecl())
3032 return addSubstitution(TD);
Sean Huntc3021132010-05-05 15:23:54 +00003033
Douglas Gregor1e9268e2010-04-28 05:58:56 +00003034 Template = Context.getASTContext().getCanonicalTemplateName(Template);
3035 addSubstitution(reinterpret_cast<uintptr_t>(Template.getAsVoidPointer()));
3036}
3037
Anders Carlssond3a932a2009-09-17 03:53:28 +00003038void CXXNameMangler::addSubstitution(uintptr_t Ptr) {
Anders Carlssond3a932a2009-09-17 03:53:28 +00003039 assert(!Substitutions.count(Ptr) && "Substitution already exists!");
Anders Carlsson9d85b722010-06-02 04:29:50 +00003040 Substitutions[Ptr] = SeqID++;
Anders Carlsson76967372009-09-17 00:43:46 +00003041}
3042
Daniel Dunbar1b077112009-11-21 09:06:10 +00003043//
Mike Stump1eb44332009-09-09 15:08:12 +00003044
Daniel Dunbar1b077112009-11-21 09:06:10 +00003045/// \brief Mangles the name of the declaration D and emits that name to the
3046/// given output stream.
3047///
3048/// If the declaration D requires a mangled name, this routine will emit that
3049/// mangled name to \p os and return true. Otherwise, \p os will be unchanged
3050/// and this routine will return false. In this case, the caller should just
3051/// emit the identifier of the declaration (\c D->getIdentifier()) as its
3052/// name.
Peter Collingbourne14110472011-01-13 18:57:25 +00003053void ItaniumMangleContext::mangleName(const NamedDecl *D,
Rafael Espindola0e376a02011-02-11 01:41:00 +00003054 llvm::raw_ostream &Out) {
Daniel Dunbarc02ab4c2009-11-21 09:14:44 +00003055 assert((isa<FunctionDecl>(D) || isa<VarDecl>(D)) &&
3056 "Invalid mangleName() call, argument is not a variable or function!");
3057 assert(!isa<CXXConstructorDecl>(D) && !isa<CXXDestructorDecl>(D) &&
3058 "Invalid mangleName() call on 'structor decl!");
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00003059
Daniel Dunbar1b077112009-11-21 09:06:10 +00003060 PrettyStackTraceDecl CrashInfo(D, SourceLocation(),
3061 getASTContext().getSourceManager(),
3062 "Mangling declaration");
Mike Stump1eb44332009-09-09 15:08:12 +00003063
John McCallfb44de92011-05-01 22:35:37 +00003064 CXXNameMangler Mangler(*this, Out, D);
Daniel Dunbar94fd26d2009-11-21 09:06:22 +00003065 return Mangler.mangle(D);
Daniel Dunbar1b077112009-11-21 09:06:10 +00003066}
Mike Stump1eb44332009-09-09 15:08:12 +00003067
Peter Collingbourne14110472011-01-13 18:57:25 +00003068void ItaniumMangleContext::mangleCXXCtor(const CXXConstructorDecl *D,
3069 CXXCtorType Type,
Rafael Espindola0e376a02011-02-11 01:41:00 +00003070 llvm::raw_ostream &Out) {
Rafael Espindolac4850c22011-02-10 23:59:36 +00003071 CXXNameMangler Mangler(*this, Out, D, Type);
Daniel Dunbar77939c92009-11-21 09:06:31 +00003072 Mangler.mangle(D);
Daniel Dunbar1b077112009-11-21 09:06:10 +00003073}
Mike Stump1eb44332009-09-09 15:08:12 +00003074
Peter Collingbourne14110472011-01-13 18:57:25 +00003075void ItaniumMangleContext::mangleCXXDtor(const CXXDestructorDecl *D,
3076 CXXDtorType Type,
Rafael Espindola0e376a02011-02-11 01:41:00 +00003077 llvm::raw_ostream &Out) {
Rafael Espindolac4850c22011-02-10 23:59:36 +00003078 CXXNameMangler Mangler(*this, Out, D, Type);
Daniel Dunbar77939c92009-11-21 09:06:31 +00003079 Mangler.mangle(D);
Daniel Dunbar1b077112009-11-21 09:06:10 +00003080}
Mike Stumpf1216772009-07-31 18:25:34 +00003081
Peter Collingbourne14110472011-01-13 18:57:25 +00003082void ItaniumMangleContext::mangleThunk(const CXXMethodDecl *MD,
3083 const ThunkInfo &Thunk,
Rafael Espindolaf0be9792011-02-11 02:52:17 +00003084 llvm::raw_ostream &Out) {
Anders Carlsson19879c92010-03-23 17:17:29 +00003085 // <special-name> ::= T <call-offset> <base encoding>
3086 // # base is the nominal target function of thunk
3087 // <special-name> ::= Tc <call-offset> <call-offset> <base encoding>
3088 // # base is the nominal target function of thunk
3089 // # first call-offset is 'this' adjustment
3090 // # second call-offset is result adjustment
Sean Huntc3021132010-05-05 15:23:54 +00003091
Anders Carlsson19879c92010-03-23 17:17:29 +00003092 assert(!isa<CXXDestructorDecl>(MD) &&
3093 "Use mangleCXXDtor for destructor decls!");
Rafael Espindolac4850c22011-02-10 23:59:36 +00003094 CXXNameMangler Mangler(*this, Out);
Anders Carlsson19879c92010-03-23 17:17:29 +00003095 Mangler.getStream() << "_ZT";
3096 if (!Thunk.Return.isEmpty())
3097 Mangler.getStream() << 'c';
Sean Huntc3021132010-05-05 15:23:54 +00003098
Anders Carlsson19879c92010-03-23 17:17:29 +00003099 // Mangle the 'this' pointer adjustment.
3100 Mangler.mangleCallOffset(Thunk.This.NonVirtual, Thunk.This.VCallOffsetOffset);
Sean Huntc3021132010-05-05 15:23:54 +00003101
Anders Carlsson19879c92010-03-23 17:17:29 +00003102 // Mangle the return pointer adjustment if there is one.
3103 if (!Thunk.Return.isEmpty())
3104 Mangler.mangleCallOffset(Thunk.Return.NonVirtual,
3105 Thunk.Return.VBaseOffsetOffset);
Sean Huntc3021132010-05-05 15:23:54 +00003106
Anders Carlsson19879c92010-03-23 17:17:29 +00003107 Mangler.mangleFunctionEncoding(MD);
3108}
3109
Sean Huntc3021132010-05-05 15:23:54 +00003110void
Peter Collingbourne14110472011-01-13 18:57:25 +00003111ItaniumMangleContext::mangleCXXDtorThunk(const CXXDestructorDecl *DD,
3112 CXXDtorType Type,
3113 const ThisAdjustment &ThisAdjustment,
Rafael Espindolaf0be9792011-02-11 02:52:17 +00003114 llvm::raw_ostream &Out) {
Anders Carlsson19879c92010-03-23 17:17:29 +00003115 // <special-name> ::= T <call-offset> <base encoding>
3116 // # base is the nominal target function of thunk
Rafael Espindolac4850c22011-02-10 23:59:36 +00003117 CXXNameMangler Mangler(*this, Out, DD, Type);
Anders Carlsson19879c92010-03-23 17:17:29 +00003118 Mangler.getStream() << "_ZT";
3119
3120 // Mangle the 'this' pointer adjustment.
Sean Huntc3021132010-05-05 15:23:54 +00003121 Mangler.mangleCallOffset(ThisAdjustment.NonVirtual,
Anders Carlsson19879c92010-03-23 17:17:29 +00003122 ThisAdjustment.VCallOffsetOffset);
3123
3124 Mangler.mangleFunctionEncoding(DD);
3125}
3126
Daniel Dunbarc0747712009-11-21 09:12:13 +00003127/// mangleGuardVariable - Returns the mangled name for a guard variable
3128/// for the passed in VarDecl.
Peter Collingbourne14110472011-01-13 18:57:25 +00003129void ItaniumMangleContext::mangleItaniumGuardVariable(const VarDecl *D,
Rafael Espindolaf0be9792011-02-11 02:52:17 +00003130 llvm::raw_ostream &Out) {
Daniel Dunbarc0747712009-11-21 09:12:13 +00003131 // <special-name> ::= GV <object name> # Guard variable for one-time
3132 // # initialization
Rafael Espindolac4850c22011-02-10 23:59:36 +00003133 CXXNameMangler Mangler(*this, Out);
Daniel Dunbarc0747712009-11-21 09:12:13 +00003134 Mangler.getStream() << "_ZGV";
3135 Mangler.mangleName(D);
3136}
3137
Peter Collingbourne14110472011-01-13 18:57:25 +00003138void ItaniumMangleContext::mangleReferenceTemporary(const VarDecl *D,
Rafael Espindolaf0be9792011-02-11 02:52:17 +00003139 llvm::raw_ostream &Out) {
Anders Carlsson715edf22010-06-26 16:09:40 +00003140 // We match the GCC mangling here.
3141 // <special-name> ::= GR <object name>
Rafael Espindolac4850c22011-02-10 23:59:36 +00003142 CXXNameMangler Mangler(*this, Out);
Anders Carlsson715edf22010-06-26 16:09:40 +00003143 Mangler.getStream() << "_ZGR";
3144 Mangler.mangleName(D);
3145}
3146
Peter Collingbourne14110472011-01-13 18:57:25 +00003147void ItaniumMangleContext::mangleCXXVTable(const CXXRecordDecl *RD,
Rafael Espindolaf0be9792011-02-11 02:52:17 +00003148 llvm::raw_ostream &Out) {
Daniel Dunbarc0747712009-11-21 09:12:13 +00003149 // <special-name> ::= TV <type> # virtual table
Rafael Espindolac4850c22011-02-10 23:59:36 +00003150 CXXNameMangler Mangler(*this, Out);
Daniel Dunbarc0747712009-11-21 09:12:13 +00003151 Mangler.getStream() << "_ZTV";
Douglas Gregor1b12a3b2010-05-26 05:11:13 +00003152 Mangler.mangleNameOrStandardSubstitution(RD);
Daniel Dunbar1b077112009-11-21 09:06:10 +00003153}
Mike Stump82d75b02009-11-10 01:58:37 +00003154
Peter Collingbourne14110472011-01-13 18:57:25 +00003155void ItaniumMangleContext::mangleCXXVTT(const CXXRecordDecl *RD,
Rafael Espindolaf0be9792011-02-11 02:52:17 +00003156 llvm::raw_ostream &Out) {
Daniel Dunbarc0747712009-11-21 09:12:13 +00003157 // <special-name> ::= TT <type> # VTT structure
Rafael Espindolac4850c22011-02-10 23:59:36 +00003158 CXXNameMangler Mangler(*this, Out);
Daniel Dunbarc0747712009-11-21 09:12:13 +00003159 Mangler.getStream() << "_ZTT";
Douglas Gregor1b12a3b2010-05-26 05:11:13 +00003160 Mangler.mangleNameOrStandardSubstitution(RD);
Daniel Dunbar1b077112009-11-21 09:06:10 +00003161}
Mike Stumpab3f7e92009-11-10 01:41:59 +00003162
Peter Collingbourne14110472011-01-13 18:57:25 +00003163void ItaniumMangleContext::mangleCXXCtorVTable(const CXXRecordDecl *RD,
3164 int64_t Offset,
3165 const CXXRecordDecl *Type,
Rafael Espindolaf0be9792011-02-11 02:52:17 +00003166 llvm::raw_ostream &Out) {
Daniel Dunbarc0747712009-11-21 09:12:13 +00003167 // <special-name> ::= TC <type> <offset number> _ <base type>
Rafael Espindolac4850c22011-02-10 23:59:36 +00003168 CXXNameMangler Mangler(*this, Out);
Daniel Dunbarc0747712009-11-21 09:12:13 +00003169 Mangler.getStream() << "_ZTC";
Douglas Gregor1b12a3b2010-05-26 05:11:13 +00003170 Mangler.mangleNameOrStandardSubstitution(RD);
Daniel Dunbarc0747712009-11-21 09:12:13 +00003171 Mangler.getStream() << Offset;
Benjamin Kramer35f59b62010-04-10 16:03:31 +00003172 Mangler.getStream() << '_';
Douglas Gregor1b12a3b2010-05-26 05:11:13 +00003173 Mangler.mangleNameOrStandardSubstitution(Type);
Daniel Dunbar1b077112009-11-21 09:06:10 +00003174}
Mike Stump738f8c22009-07-31 23:15:31 +00003175
Peter Collingbourne14110472011-01-13 18:57:25 +00003176void ItaniumMangleContext::mangleCXXRTTI(QualType Ty,
Rafael Espindolaf0be9792011-02-11 02:52:17 +00003177 llvm::raw_ostream &Out) {
Daniel Dunbarc0747712009-11-21 09:12:13 +00003178 // <special-name> ::= TI <type> # typeinfo structure
Douglas Gregor154fe982009-12-23 22:04:40 +00003179 assert(!Ty.hasQualifiers() && "RTTI info cannot have top-level qualifiers");
Rafael Espindolac4850c22011-02-10 23:59:36 +00003180 CXXNameMangler Mangler(*this, Out);
Daniel Dunbarc0747712009-11-21 09:12:13 +00003181 Mangler.getStream() << "_ZTI";
3182 Mangler.mangleType(Ty);
Daniel Dunbar1b077112009-11-21 09:06:10 +00003183}
Mike Stump67795982009-11-14 00:14:13 +00003184
Peter Collingbourne14110472011-01-13 18:57:25 +00003185void ItaniumMangleContext::mangleCXXRTTIName(QualType Ty,
Rafael Espindolaf0be9792011-02-11 02:52:17 +00003186 llvm::raw_ostream &Out) {
Daniel Dunbarc0747712009-11-21 09:12:13 +00003187 // <special-name> ::= TS <type> # typeinfo name (null terminated byte string)
Rafael Espindolac4850c22011-02-10 23:59:36 +00003188 CXXNameMangler Mangler(*this, Out);
Daniel Dunbarc0747712009-11-21 09:12:13 +00003189 Mangler.getStream() << "_ZTS";
3190 Mangler.mangleType(Ty);
Mike Stumpf1216772009-07-31 18:25:34 +00003191}
Peter Collingbourne14110472011-01-13 18:57:25 +00003192
3193MangleContext *clang::createItaniumMangleContext(ASTContext &Context,
3194 Diagnostic &Diags) {
3195 return new ItaniumMangleContext(Context, Diags);
3196}