blob: e81ec7e54b623ab81cb78a673fc9d91c63af43b3 [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 McCallfb44de92011-05-01 22:35:37 +000024#include "clang/AST/TypeLoc.h"
Peter Collingbourne14110472011-01-13 18:57:25 +000025#include "clang/Basic/ABI.h"
Douglas Gregor6ec36682009-02-18 23:53:56 +000026#include "clang/Basic/SourceManager.h"
Rafael Espindola4e274e92011-02-15 22:23:51 +000027#include "clang/Basic/TargetInfo.h"
Anders Carlssonc4355b62009-10-07 01:45:02 +000028#include "llvm/ADT/StringExtras.h"
Douglas Gregor5f2bfd42009-02-13 00:10:09 +000029#include "llvm/Support/raw_ostream.h"
John McCallefe6aee2009-09-05 07:56:18 +000030#include "llvm/Support/ErrorHandling.h"
Anders Carlssonf98574b2010-02-05 07:31:37 +000031
32#define MANGLE_CHECKER 0
33
34#if MANGLE_CHECKER
35#include <cxxabi.h>
36#endif
37
Douglas Gregor5f2bfd42009-02-13 00:10:09 +000038using namespace clang;
Charles Davis685b1d92010-05-26 18:25:27 +000039
Douglas Gregor5f2bfd42009-02-13 00:10:09 +000040namespace {
Fariborz Jahanian57058532010-03-03 19:41:08 +000041
John McCall82b7d7b2010-10-18 21:28:44 +000042static const CXXRecordDecl *GetLocalClassDecl(const NamedDecl *ND) {
43 const DeclContext *DC = dyn_cast<DeclContext>(ND);
44 if (!DC)
45 DC = ND->getDeclContext();
46 while (!DC->isNamespace() && !DC->isTranslationUnit()) {
47 if (isa<FunctionDecl>(DC->getParent()))
48 return dyn_cast<CXXRecordDecl>(DC);
49 DC = DC->getParent();
Fariborz Jahanian57058532010-03-03 19:41:08 +000050 }
51 return 0;
52}
53
John McCallfb44de92011-05-01 22:35:37 +000054static const FunctionDecl *getStructor(const FunctionDecl *fn) {
55 if (const FunctionTemplateDecl *ftd = fn->getPrimaryTemplate())
56 return ftd->getTemplatedDecl();
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +000057
John McCallfb44de92011-05-01 22:35:37 +000058 return fn;
59}
Anders Carlsson7e120032009-11-24 05:36:32 +000060
John McCallfb44de92011-05-01 22:35:37 +000061static const NamedDecl *getStructor(const NamedDecl *decl) {
62 const FunctionDecl *fn = dyn_cast_or_null<FunctionDecl>(decl);
63 return (fn ? getStructor(fn) : decl);
Anders Carlsson7e120032009-11-24 05:36:32 +000064}
John McCall1dd73832010-02-04 01:42:13 +000065
66static const unsigned UnknownArity = ~0U;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +000067
Peter Collingbourne14110472011-01-13 18:57:25 +000068class ItaniumMangleContext : public MangleContext {
69 llvm::DenseMap<const TagDecl *, uint64_t> AnonStructIds;
70 unsigned Discriminator;
71 llvm::DenseMap<const NamedDecl*, unsigned> Uniquifier;
72
73public:
74 explicit ItaniumMangleContext(ASTContext &Context,
75 Diagnostic &Diags)
76 : MangleContext(Context, Diags) { }
77
78 uint64_t getAnonymousStructId(const TagDecl *TD) {
79 std::pair<llvm::DenseMap<const TagDecl *,
80 uint64_t>::iterator, bool> Result =
81 AnonStructIds.insert(std::make_pair(TD, AnonStructIds.size()));
82 return Result.first->second;
83 }
84
85 void startNewFunction() {
86 MangleContext::startNewFunction();
87 mangleInitDiscriminator();
88 }
89
90 /// @name Mangler Entry Points
91 /// @{
92
93 bool shouldMangleDeclName(const NamedDecl *D);
Rafael Espindola0e376a02011-02-11 01:41:00 +000094 void mangleName(const NamedDecl *D, llvm::raw_ostream &);
Peter Collingbourne14110472011-01-13 18:57:25 +000095 void mangleThunk(const CXXMethodDecl *MD,
96 const ThunkInfo &Thunk,
Rafael Espindolaf0be9792011-02-11 02:52:17 +000097 llvm::raw_ostream &);
Peter Collingbourne14110472011-01-13 18:57:25 +000098 void mangleCXXDtorThunk(const CXXDestructorDecl *DD, CXXDtorType Type,
99 const ThisAdjustment &ThisAdjustment,
Rafael Espindolaf0be9792011-02-11 02:52:17 +0000100 llvm::raw_ostream &);
Peter Collingbourne14110472011-01-13 18:57:25 +0000101 void mangleReferenceTemporary(const VarDecl *D,
Rafael Espindolaf0be9792011-02-11 02:52:17 +0000102 llvm::raw_ostream &);
Peter Collingbourne14110472011-01-13 18:57:25 +0000103 void mangleCXXVTable(const CXXRecordDecl *RD,
Rafael Espindolaf0be9792011-02-11 02:52:17 +0000104 llvm::raw_ostream &);
Peter Collingbourne14110472011-01-13 18:57:25 +0000105 void mangleCXXVTT(const CXXRecordDecl *RD,
Rafael Espindolaf0be9792011-02-11 02:52:17 +0000106 llvm::raw_ostream &);
Peter Collingbourne14110472011-01-13 18:57:25 +0000107 void mangleCXXCtorVTable(const CXXRecordDecl *RD, int64_t Offset,
108 const CXXRecordDecl *Type,
Rafael Espindolaf0be9792011-02-11 02:52:17 +0000109 llvm::raw_ostream &);
110 void mangleCXXRTTI(QualType T, llvm::raw_ostream &);
111 void mangleCXXRTTIName(QualType T, llvm::raw_ostream &);
Peter Collingbourne14110472011-01-13 18:57:25 +0000112 void mangleCXXCtor(const CXXConstructorDecl *D, CXXCtorType Type,
Rafael Espindola0e376a02011-02-11 01:41:00 +0000113 llvm::raw_ostream &);
Peter Collingbourne14110472011-01-13 18:57:25 +0000114 void mangleCXXDtor(const CXXDestructorDecl *D, CXXDtorType Type,
Rafael Espindola0e376a02011-02-11 01:41:00 +0000115 llvm::raw_ostream &);
Peter Collingbourne14110472011-01-13 18:57:25 +0000116
Rafael Espindolaf0be9792011-02-11 02:52:17 +0000117 void mangleItaniumGuardVariable(const VarDecl *D, llvm::raw_ostream &);
Peter Collingbourne14110472011-01-13 18:57:25 +0000118
119 void mangleInitDiscriminator() {
120 Discriminator = 0;
121 }
122
123 bool getNextDiscriminator(const NamedDecl *ND, unsigned &disc) {
124 unsigned &discriminator = Uniquifier[ND];
125 if (!discriminator)
126 discriminator = ++Discriminator;
127 if (discriminator == 1)
128 return false;
129 disc = discriminator-2;
130 return true;
131 }
132 /// @}
133};
134
Daniel Dunbar1b077112009-11-21 09:06:10 +0000135/// CXXNameMangler - Manage the mangling of a single name.
Daniel Dunbarc0747712009-11-21 09:12:13 +0000136class CXXNameMangler {
Peter Collingbourne14110472011-01-13 18:57:25 +0000137 ItaniumMangleContext &Context;
Rafael Espindola0e376a02011-02-11 01:41:00 +0000138 llvm::raw_ostream &Out;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000139
John McCallfb44de92011-05-01 22:35:37 +0000140 /// The "structor" is the top-level declaration being mangled, if
141 /// that's not a template specialization; otherwise it's the pattern
142 /// for that specialization.
143 const NamedDecl *Structor;
Daniel Dunbar1b077112009-11-21 09:06:10 +0000144 unsigned StructorType;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000145
Anders Carlsson9d85b722010-06-02 04:29:50 +0000146 /// SeqID - The next subsitution sequence number.
147 unsigned SeqID;
148
John McCallfb44de92011-05-01 22:35:37 +0000149 class FunctionTypeDepthState {
150 unsigned Bits;
151
152 enum { InResultTypeMask = 1 };
153
154 public:
155 FunctionTypeDepthState() : Bits(0) {}
156
157 /// The number of function types we're inside.
158 unsigned getDepth() const {
159 return Bits >> 1;
160 }
161
162 /// True if we're in the return type of the innermost function type.
163 bool isInResultType() const {
164 return Bits & InResultTypeMask;
165 }
166
167 FunctionTypeDepthState push() {
168 FunctionTypeDepthState tmp = *this;
169 Bits = (Bits & ~InResultTypeMask) + 2;
170 return tmp;
171 }
172
173 void enterResultType() {
174 Bits |= InResultTypeMask;
175 }
176
177 void leaveResultType() {
178 Bits &= ~InResultTypeMask;
179 }
180
181 void pop(FunctionTypeDepthState saved) {
182 assert(getDepth() == saved.getDepth() + 1);
183 Bits = saved.Bits;
184 }
185
186 } FunctionTypeDepth;
187
Daniel Dunbar1b077112009-11-21 09:06:10 +0000188 llvm::DenseMap<uintptr_t, unsigned> Substitutions;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000189
John McCall1dd73832010-02-04 01:42:13 +0000190 ASTContext &getASTContext() const { return Context.getASTContext(); }
191
Daniel Dunbarc0747712009-11-21 09:12:13 +0000192public:
John McCallfb44de92011-05-01 22:35:37 +0000193 CXXNameMangler(ItaniumMangleContext &C, llvm::raw_ostream &Out_,
194 const NamedDecl *D = 0)
195 : Context(C), Out(Out_), Structor(getStructor(D)), StructorType(0),
196 SeqID(0) {
197 // These can't be mangled without a ctor type or dtor type.
198 assert(!D || (!isa<CXXDestructorDecl>(D) &&
199 !isa<CXXConstructorDecl>(D)));
200 }
Rafael Espindola0e376a02011-02-11 01:41:00 +0000201 CXXNameMangler(ItaniumMangleContext &C, llvm::raw_ostream &Out_,
Daniel Dunbar77939c92009-11-21 09:06:31 +0000202 const CXXConstructorDecl *D, CXXCtorType Type)
Rafael Espindolac4850c22011-02-10 23:59:36 +0000203 : Context(C), Out(Out_), Structor(getStructor(D)), StructorType(Type),
John McCallfb44de92011-05-01 22:35:37 +0000204 SeqID(0) { }
Rafael Espindola0e376a02011-02-11 01:41:00 +0000205 CXXNameMangler(ItaniumMangleContext &C, llvm::raw_ostream &Out_,
Daniel Dunbar77939c92009-11-21 09:06:31 +0000206 const CXXDestructorDecl *D, CXXDtorType Type)
Rafael Espindolac4850c22011-02-10 23:59:36 +0000207 : Context(C), Out(Out_), Structor(getStructor(D)), StructorType(Type),
John McCallfb44de92011-05-01 22:35:37 +0000208 SeqID(0) { }
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000209
Anders Carlssonf98574b2010-02-05 07:31:37 +0000210#if MANGLE_CHECKER
211 ~CXXNameMangler() {
212 if (Out.str()[0] == '\01')
213 return;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000214
Anders Carlssonf98574b2010-02-05 07:31:37 +0000215 int status = 0;
216 char *result = abi::__cxa_demangle(Out.str().str().c_str(), 0, 0, &status);
217 assert(status == 0 && "Could not demangle mangled name!");
218 free(result);
219 }
220#endif
Rafael Espindola0e376a02011-02-11 01:41:00 +0000221 llvm::raw_ostream &getStream() { return Out; }
Daniel Dunbarc0747712009-11-21 09:12:13 +0000222
Daniel Dunbar7e0c1952009-11-21 09:17:15 +0000223 void mangle(const NamedDecl *D, llvm::StringRef Prefix = "_Z");
Anders Carlsson19879c92010-03-23 17:17:29 +0000224 void mangleCallOffset(int64_t NonVirtual, int64_t Virtual);
John McCall0512e482010-07-14 04:20:34 +0000225 void mangleNumber(const llvm::APSInt &I);
Anders Carlssona94822e2009-11-26 02:32:05 +0000226 void mangleNumber(int64_t Number);
John McCall0512e482010-07-14 04:20:34 +0000227 void mangleFloat(const llvm::APFloat &F);
Daniel Dunbarc0747712009-11-21 09:12:13 +0000228 void mangleFunctionEncoding(const FunctionDecl *FD);
229 void mangleName(const NamedDecl *ND);
230 void mangleType(QualType T);
Douglas Gregor1b12a3b2010-05-26 05:11:13 +0000231 void mangleNameOrStandardSubstitution(const NamedDecl *ND);
232
Daniel Dunbarc0747712009-11-21 09:12:13 +0000233private:
Daniel Dunbar1b077112009-11-21 09:06:10 +0000234 bool mangleSubstitution(const NamedDecl *ND);
235 bool mangleSubstitution(QualType T);
Douglas Gregor1e9268e2010-04-28 05:58:56 +0000236 bool mangleSubstitution(TemplateName Template);
Daniel Dunbar1b077112009-11-21 09:06:10 +0000237 bool mangleSubstitution(uintptr_t Ptr);
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000238
Daniel Dunbar1b077112009-11-21 09:06:10 +0000239 bool mangleStandardSubstitution(const NamedDecl *ND);
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000240
Daniel Dunbar1b077112009-11-21 09:06:10 +0000241 void addSubstitution(const NamedDecl *ND) {
242 ND = cast<NamedDecl>(ND->getCanonicalDecl());
Anders Carlsson433d1372009-11-07 04:26:04 +0000243
Daniel Dunbar1b077112009-11-21 09:06:10 +0000244 addSubstitution(reinterpret_cast<uintptr_t>(ND));
245 }
246 void addSubstitution(QualType T);
Douglas Gregor1e9268e2010-04-28 05:58:56 +0000247 void addSubstitution(TemplateName Template);
Daniel Dunbar1b077112009-11-21 09:06:10 +0000248 void addSubstitution(uintptr_t Ptr);
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000249
John McCalla0ce15c2011-04-24 08:23:24 +0000250 void mangleUnresolvedPrefix(NestedNameSpecifier *qualifier,
251 NamedDecl *firstQualifierLookup,
252 bool recursive = false);
253 void mangleUnresolvedName(NestedNameSpecifier *qualifier,
254 NamedDecl *firstQualifierLookup,
255 DeclarationName name,
John McCall1dd73832010-02-04 01:42:13 +0000256 unsigned KnownArity = UnknownArity);
257
John McCall4f4e4132011-05-04 01:45:19 +0000258 static bool isUnresolvedType(const Type *type);
259 void mangleUnresolvedType(const Type *type);
John McCalla0ce15c2011-04-24 08:23:24 +0000260
Daniel Dunbar1b077112009-11-21 09:06:10 +0000261 void mangleName(const TemplateDecl *TD,
262 const TemplateArgument *TemplateArgs,
263 unsigned NumTemplateArgs);
John McCall1dd73832010-02-04 01:42:13 +0000264 void mangleUnqualifiedName(const NamedDecl *ND) {
265 mangleUnqualifiedName(ND, ND->getDeclName(), UnknownArity);
266 }
267 void mangleUnqualifiedName(const NamedDecl *ND, DeclarationName Name,
268 unsigned KnownArity);
Daniel Dunbar1b077112009-11-21 09:06:10 +0000269 void mangleUnscopedName(const NamedDecl *ND);
270 void mangleUnscopedTemplateName(const TemplateDecl *ND);
Douglas Gregor1e9268e2010-04-28 05:58:56 +0000271 void mangleUnscopedTemplateName(TemplateName);
Daniel Dunbar1b077112009-11-21 09:06:10 +0000272 void mangleSourceName(const IdentifierInfo *II);
273 void mangleLocalName(const NamedDecl *ND);
Fariborz Jahanian57058532010-03-03 19:41:08 +0000274 void mangleNestedName(const NamedDecl *ND, const DeclContext *DC,
275 bool NoFunction=false);
Daniel Dunbar1b077112009-11-21 09:06:10 +0000276 void mangleNestedName(const TemplateDecl *TD,
277 const TemplateArgument *TemplateArgs,
278 unsigned NumTemplateArgs);
John McCalla0ce15c2011-04-24 08:23:24 +0000279 void manglePrefix(NestedNameSpecifier *qualifier);
Fariborz Jahanian57058532010-03-03 19:41:08 +0000280 void manglePrefix(const DeclContext *DC, bool NoFunction=false);
John McCall4f4e4132011-05-04 01:45:19 +0000281 void manglePrefix(QualType type);
Daniel Dunbar1b077112009-11-21 09:06:10 +0000282 void mangleTemplatePrefix(const TemplateDecl *ND);
Douglas Gregor20f0cc72010-04-23 03:10:43 +0000283 void mangleTemplatePrefix(TemplateName Template);
Daniel Dunbar1b077112009-11-21 09:06:10 +0000284 void mangleOperatorName(OverloadedOperatorKind OO, unsigned Arity);
285 void mangleQualifiers(Qualifiers Quals);
Douglas Gregor0a9a6d62011-01-26 17:36:28 +0000286 void mangleRefQualifier(RefQualifierKind RefQualifier);
John McCallefe6aee2009-09-05 07:56:18 +0000287
Anders Carlsson7b06f6c2009-12-10 03:14:39 +0000288 void mangleObjCMethodName(const ObjCMethodDecl *MD);
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000289
Daniel Dunbar1b077112009-11-21 09:06:10 +0000290 // Declare manglers for every type class.
John McCallefe6aee2009-09-05 07:56:18 +0000291#define ABSTRACT_TYPE(CLASS, PARENT)
292#define NON_CANONICAL_TYPE(CLASS, PARENT)
293#define TYPE(CLASS, PARENT) void mangleType(const CLASS##Type *T);
294#include "clang/AST/TypeNodes.def"
295
Daniel Dunbar1b077112009-11-21 09:06:10 +0000296 void mangleType(const TagType*);
John McCallb6f532e2010-07-14 06:43:17 +0000297 void mangleType(TemplateName);
Daniel Dunbar1b077112009-11-21 09:06:10 +0000298 void mangleBareFunctionType(const FunctionType *T,
299 bool MangleReturnType);
Bob Wilson57147a82010-11-16 00:32:18 +0000300 void mangleNeonVectorType(const VectorType *T);
Anders Carlssone170ba72009-12-14 01:45:37 +0000301
302 void mangleIntegerLiteral(QualType T, const llvm::APSInt &Value);
John McCalla0ce15c2011-04-24 08:23:24 +0000303 void mangleMemberExpr(const Expr *base, bool isArrow,
304 NestedNameSpecifier *qualifier,
305 NamedDecl *firstQualifierLookup,
306 DeclarationName name,
307 unsigned knownArity);
John McCall5e1e89b2010-08-18 19:18:59 +0000308 void mangleExpression(const Expr *E, unsigned Arity = UnknownArity);
Daniel Dunbar1b077112009-11-21 09:06:10 +0000309 void mangleCXXCtorType(CXXCtorType T);
310 void mangleCXXDtorType(CXXDtorType T);
Mike Stump1eb44332009-09-09 15:08:12 +0000311
John McCall6dbce192010-08-20 00:17:19 +0000312 void mangleTemplateArgs(const ExplicitTemplateArgumentList &TemplateArgs);
Douglas Gregor20f0cc72010-04-23 03:10:43 +0000313 void mangleTemplateArgs(TemplateName Template,
314 const TemplateArgument *TemplateArgs,
Sean Huntc3021132010-05-05 15:23:54 +0000315 unsigned NumTemplateArgs);
Rafael Espindolad9800722010-03-11 14:07:00 +0000316 void mangleTemplateArgs(const TemplateParameterList &PL,
317 const TemplateArgument *TemplateArgs,
Daniel Dunbar1b077112009-11-21 09:06:10 +0000318 unsigned NumTemplateArgs);
Rafael Espindolad9800722010-03-11 14:07:00 +0000319 void mangleTemplateArgs(const TemplateParameterList &PL,
320 const TemplateArgumentList &AL);
321 void mangleTemplateArg(const NamedDecl *P, const TemplateArgument &A);
John McCall4f4e4132011-05-04 01:45:19 +0000322 void mangleUnresolvedTemplateArgs(const TemplateArgument *args,
323 unsigned numArgs);
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000324
Daniel Dunbar1b077112009-11-21 09:06:10 +0000325 void mangleTemplateParameter(unsigned Index);
John McCallfb44de92011-05-01 22:35:37 +0000326
327 void mangleFunctionParam(const ParmVarDecl *parm);
Daniel Dunbar1b077112009-11-21 09:06:10 +0000328};
Peter Collingbourne14110472011-01-13 18:57:25 +0000329
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000330}
331
Anders Carlsson43f17402009-04-02 15:51:53 +0000332static bool isInCLinkageSpecification(const Decl *D) {
Douglas Gregor457e2812009-10-28 16:31:34 +0000333 D = D->getCanonicalDecl();
Mike Stump1eb44332009-09-09 15:08:12 +0000334 for (const DeclContext *DC = D->getDeclContext();
Anders Carlsson43f17402009-04-02 15:51:53 +0000335 !DC->isTranslationUnit(); DC = DC->getParent()) {
Mike Stump1eb44332009-09-09 15:08:12 +0000336 if (const LinkageSpecDecl *Linkage = dyn_cast<LinkageSpecDecl>(DC))
Anders Carlsson43f17402009-04-02 15:51:53 +0000337 return Linkage->getLanguage() == LinkageSpecDecl::lang_c;
338 }
Mike Stump1eb44332009-09-09 15:08:12 +0000339
Anders Carlsson43f17402009-04-02 15:51:53 +0000340 return false;
341}
342
Peter Collingbourne14110472011-01-13 18:57:25 +0000343bool ItaniumMangleContext::shouldMangleDeclName(const NamedDecl *D) {
Daniel Dunbarf981bf82009-11-21 09:14:52 +0000344 // In C, functions with no attributes never need to be mangled. Fastpath them.
345 if (!getASTContext().getLangOptions().CPlusPlus && !D->hasAttrs())
346 return false;
347
348 // Any decl can be declared with __asm("foo") on it, and this takes precedence
349 // over all other naming in the .o file.
350 if (D->hasAttr<AsmLabelAttr>())
351 return true;
352
Mike Stump141c5af2009-09-02 00:25:38 +0000353 // Clang's "overloadable" attribute extension to C/C++ implies name mangling
Anders Carlssona1e16222009-11-07 07:15:03 +0000354 // (always) as does passing a C++ member function and a function
355 // whose name is not a simple identifier.
Daniel Dunbarf981bf82009-11-21 09:14:52 +0000356 const FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
357 if (FD && (FD->hasAttr<OverloadableAttr>() || isa<CXXMethodDecl>(FD) ||
358 !FD->getDeclName().isIdentifier()))
359 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000360
Daniel Dunbarf981bf82009-11-21 09:14:52 +0000361 // Otherwise, no mangling is done outside C++ mode.
362 if (!getASTContext().getLangOptions().CPlusPlus)
363 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000364
Sean Hunt31455252010-01-24 03:04:27 +0000365 // Variables at global scope with non-internal linkage are not mangled
Eli Friedman7facf842009-12-02 20:32:49 +0000366 if (!FD) {
367 const DeclContext *DC = D->getDeclContext();
368 // Check for extern variable declared locally.
Fariborz Jahaniane81c5612010-06-30 18:57:21 +0000369 if (DC->isFunctionOrMethod() && D->hasLinkage())
Eli Friedman7facf842009-12-02 20:32:49 +0000370 while (!DC->isNamespace() && !DC->isTranslationUnit())
371 DC = DC->getParent();
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000372 if (DC->isTranslationUnit() && D->getLinkage() != InternalLinkage)
Eli Friedman7facf842009-12-02 20:32:49 +0000373 return false;
374 }
375
Eli Friedmanc00cb642010-07-18 20:49:59 +0000376 // Class members are always mangled.
377 if (D->getDeclContext()->isRecord())
378 return true;
379
Eli Friedman7facf842009-12-02 20:32:49 +0000380 // C functions and "main" are not mangled.
381 if ((FD && FD->isMain()) || isInCLinkageSpecification(D))
Daniel Dunbarf981bf82009-11-21 09:14:52 +0000382 return false;
383
Anders Carlsson43f17402009-04-02 15:51:53 +0000384 return true;
385}
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000386
Daniel Dunbar7e0c1952009-11-21 09:17:15 +0000387void CXXNameMangler::mangle(const NamedDecl *D, llvm::StringRef Prefix) {
Mike Stump141c5af2009-09-02 00:25:38 +0000388 // Any decl can be declared with __asm("foo") on it, and this takes precedence
389 // over all other naming in the .o file.
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000390 if (const AsmLabelAttr *ALA = D->getAttr<AsmLabelAttr>()) {
Chris Lattnerca3f25c2009-03-21 08:24:40 +0000391 // If we have an asm name, then we use it as the mangling.
Rafael Espindola4e274e92011-02-15 22:23:51 +0000392
393 // Adding the prefix can cause problems when one file has a "foo" and
394 // another has a "\01foo". That is known to happen on ELF with the
395 // tricks normally used for producing aliases (PR9177). Fortunately the
396 // llvm mangler on ELF is a nop, so we can just avoid adding the \01
Peter Collingbourne69317432011-04-06 12:29:09 +0000397 // marker. We also avoid adding the marker if this is an alias for an
398 // LLVM intrinsic.
Rafael Espindola4e274e92011-02-15 22:23:51 +0000399 llvm::StringRef UserLabelPrefix =
400 getASTContext().Target.getUserLabelPrefix();
Peter Collingbourne69317432011-04-06 12:29:09 +0000401 if (!UserLabelPrefix.empty() && !ALA->getLabel().startswith("llvm."))
Rafael Espindola4e274e92011-02-15 22:23:51 +0000402 Out << '\01'; // LLVM IR Marker for __asm("foo")
403
Chris Lattnerca3f25c2009-03-21 08:24:40 +0000404 Out << ALA->getLabel();
Daniel Dunbarf981bf82009-11-21 09:14:52 +0000405 return;
Chris Lattnerca3f25c2009-03-21 08:24:40 +0000406 }
Mike Stump1eb44332009-09-09 15:08:12 +0000407
Sean Hunt31455252010-01-24 03:04:27 +0000408 // <mangled-name> ::= _Z <encoding>
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000409 // ::= <data name>
410 // ::= <special-name>
Daniel Dunbar7e0c1952009-11-21 09:17:15 +0000411 Out << Prefix;
412 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
Daniel Dunbarf981bf82009-11-21 09:14:52 +0000413 mangleFunctionEncoding(FD);
Rafael Espindolad9800722010-03-11 14:07:00 +0000414 else if (const VarDecl *VD = dyn_cast<VarDecl>(D))
415 mangleName(VD);
Daniel Dunbar7e0c1952009-11-21 09:17:15 +0000416 else
Rafael Espindolad9800722010-03-11 14:07:00 +0000417 mangleName(cast<FieldDecl>(D));
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000418}
419
420void CXXNameMangler::mangleFunctionEncoding(const FunctionDecl *FD) {
421 // <encoding> ::= <function name> <bare-function-type>
422 mangleName(FD);
Mike Stump1eb44332009-09-09 15:08:12 +0000423
Daniel Dunbar7e0c1952009-11-21 09:17:15 +0000424 // Don't mangle in the type if this isn't a decl we should typically mangle.
425 if (!Context.shouldMangleDeclName(FD))
426 return;
427
Mike Stump141c5af2009-09-02 00:25:38 +0000428 // Whether the mangling of a function type includes the return type depends on
429 // the context and the nature of the function. The rules for deciding whether
430 // the return type is included are:
Mike Stump1eb44332009-09-09 15:08:12 +0000431 //
Douglas Gregor1fd2dd12009-06-29 22:39:32 +0000432 // 1. Template functions (names or types) have return types encoded, with
433 // the exceptions listed below.
Mike Stump1eb44332009-09-09 15:08:12 +0000434 // 2. Function types not appearing as part of a function name mangling,
Douglas Gregor1fd2dd12009-06-29 22:39:32 +0000435 // e.g. parameters, pointer types, etc., have return type encoded, with the
436 // exceptions listed below.
437 // 3. Non-template function names do not have return types encoded.
438 //
Mike Stump141c5af2009-09-02 00:25:38 +0000439 // The exceptions mentioned in (1) and (2) above, for which the return type is
440 // never included, are
Douglas Gregor1fd2dd12009-06-29 22:39:32 +0000441 // 1. Constructors.
442 // 2. Destructors.
443 // 3. Conversion operator functions, e.g. operator int.
444 bool MangleReturnType = false;
Anders Carlsson9234b7f2009-09-17 03:46:43 +0000445 if (FunctionTemplateDecl *PrimaryTemplate = FD->getPrimaryTemplate()) {
446 if (!(isa<CXXConstructorDecl>(FD) || isa<CXXDestructorDecl>(FD) ||
447 isa<CXXConversionDecl>(FD)))
448 MangleReturnType = true;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000449
Anders Carlsson9234b7f2009-09-17 03:46:43 +0000450 // Mangle the type of the primary template.
451 FD = PrimaryTemplate->getTemplatedDecl();
452 }
453
John McCall54e14c42009-10-22 22:37:11 +0000454 // Do the canonicalization out here because parameter types can
455 // undergo additional canonicalization (e.g. array decay).
John McCallf4c73712011-01-19 06:33:43 +0000456 const FunctionType *FT
457 = cast<FunctionType>(Context.getASTContext()
John McCall54e14c42009-10-22 22:37:11 +0000458 .getCanonicalType(FD->getType()));
459
460 mangleBareFunctionType(FT, MangleReturnType);
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000461}
462
Anders Carlsson47846d22009-12-04 06:23:23 +0000463static const DeclContext *IgnoreLinkageSpecDecls(const DeclContext *DC) {
464 while (isa<LinkageSpecDecl>(DC)) {
Anders Carlsson47846d22009-12-04 06:23:23 +0000465 DC = DC->getParent();
466 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000467
Anders Carlsson47846d22009-12-04 06:23:23 +0000468 return DC;
469}
470
Anders Carlssonc820f902010-06-02 15:58:27 +0000471/// isStd - Return whether a given namespace is the 'std' namespace.
472static bool isStd(const NamespaceDecl *NS) {
473 if (!IgnoreLinkageSpecDecls(NS->getParent())->isTranslationUnit())
474 return false;
475
476 const IdentifierInfo *II = NS->getOriginalNamespace()->getIdentifier();
477 return II && II->isStr("std");
478}
479
Anders Carlsson47846d22009-12-04 06:23:23 +0000480// isStdNamespace - Return whether a given decl context is a toplevel 'std'
481// namespace.
Daniel Dunbar1308af92009-11-21 09:11:45 +0000482static bool isStdNamespace(const DeclContext *DC) {
Anders Carlsson47846d22009-12-04 06:23:23 +0000483 if (!DC->isNamespace())
484 return false;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000485
Anders Carlsson47846d22009-12-04 06:23:23 +0000486 return isStd(cast<NamespaceDecl>(DC));
Daniel Dunbar1308af92009-11-21 09:11:45 +0000487}
488
Anders Carlssonbb36ba42009-09-26 03:24:57 +0000489static const TemplateDecl *
490isTemplate(const NamedDecl *ND, const TemplateArgumentList *&TemplateArgs) {
Anders Carlsson2744a062009-09-18 19:00:18 +0000491 // Check if we have a function template.
492 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)){
Anders Carlssonbb36ba42009-09-26 03:24:57 +0000493 if (const TemplateDecl *TD = FD->getPrimaryTemplate()) {
Anders Carlsson2744a062009-09-18 19:00:18 +0000494 TemplateArgs = FD->getTemplateSpecializationArgs();
Anders Carlssonbb36ba42009-09-26 03:24:57 +0000495 return TD;
Anders Carlsson2744a062009-09-18 19:00:18 +0000496 }
497 }
498
Anders Carlssoneafc6dc2009-09-18 19:44:50 +0000499 // Check if we have a class template.
500 if (const ClassTemplateSpecializationDecl *Spec =
501 dyn_cast<ClassTemplateSpecializationDecl>(ND)) {
502 TemplateArgs = &Spec->getTemplateArgs();
Anders Carlssonbb36ba42009-09-26 03:24:57 +0000503 return Spec->getSpecializedTemplate();
Anders Carlssoneafc6dc2009-09-18 19:44:50 +0000504 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000505
Anders Carlsson2744a062009-09-18 19:00:18 +0000506 return 0;
507}
508
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000509void CXXNameMangler::mangleName(const NamedDecl *ND) {
510 // <name> ::= <nested-name>
511 // ::= <unscoped-name>
512 // ::= <unscoped-template-name> <template-args>
Anders Carlsson201ce742009-09-17 03:17:01 +0000513 // ::= <local-name>
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000514 //
Anders Carlssond58d6f72009-09-17 16:12:20 +0000515 const DeclContext *DC = ND->getDeclContext();
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000516
Eli Friedman7facf842009-12-02 20:32:49 +0000517 // If this is an extern variable declared locally, the relevant DeclContext
518 // is that of the containing namespace, or the translation unit.
519 if (isa<FunctionDecl>(DC) && ND->hasLinkage())
520 while (!DC->isNamespace() && !DC->isTranslationUnit())
521 DC = DC->getParent();
John McCall82b7d7b2010-10-18 21:28:44 +0000522 else if (GetLocalClassDecl(ND)) {
523 mangleLocalName(ND);
524 return;
525 }
Eli Friedman7facf842009-12-02 20:32:49 +0000526
Anders Carlsson5cc58c62009-09-22 17:23:30 +0000527 while (isa<LinkageSpecDecl>(DC))
Anders Carlssond58d6f72009-09-17 16:12:20 +0000528 DC = DC->getParent();
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000529
Anders Carlssond58d6f72009-09-17 16:12:20 +0000530 if (DC->isTranslationUnit() || isStdNamespace(DC)) {
Anders Carlsson2744a062009-09-18 19:00:18 +0000531 // Check if we have a template.
532 const TemplateArgumentList *TemplateArgs = 0;
Anders Carlsson0fa6df42009-09-26 19:45:45 +0000533 if (const TemplateDecl *TD = isTemplate(ND, TemplateArgs)) {
Anders Carlsson2744a062009-09-18 19:00:18 +0000534 mangleUnscopedTemplateName(TD);
Rafael Espindolad9800722010-03-11 14:07:00 +0000535 TemplateParameterList *TemplateParameters = TD->getTemplateParameters();
536 mangleTemplateArgs(*TemplateParameters, *TemplateArgs);
Anders Carlsson2744a062009-09-18 19:00:18 +0000537 return;
Anders Carlsson7482e242009-09-18 04:29:09 +0000538 }
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000539
Anders Carlsson7482e242009-09-18 04:29:09 +0000540 mangleUnscopedName(ND);
541 return;
542 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000543
Anders Carlsson7b06f6c2009-12-10 03:14:39 +0000544 if (isa<FunctionDecl>(DC) || isa<ObjCMethodDecl>(DC)) {
Anders Carlsson7482e242009-09-18 04:29:09 +0000545 mangleLocalName(ND);
546 return;
547 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000548
Eli Friedman7facf842009-12-02 20:32:49 +0000549 mangleNestedName(ND, DC);
Anders Carlsson7482e242009-09-18 04:29:09 +0000550}
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000551void CXXNameMangler::mangleName(const TemplateDecl *TD,
Anders Carlsson7624f212009-09-18 02:42:01 +0000552 const TemplateArgument *TemplateArgs,
553 unsigned NumTemplateArgs) {
Anders Carlsson47846d22009-12-04 06:23:23 +0000554 const DeclContext *DC = IgnoreLinkageSpecDecls(TD->getDeclContext());
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000555
Anders Carlsson7624f212009-09-18 02:42:01 +0000556 if (DC->isTranslationUnit() || isStdNamespace(DC)) {
Anders Carlsson0fa6df42009-09-26 19:45:45 +0000557 mangleUnscopedTemplateName(TD);
Rafael Espindolad9800722010-03-11 14:07:00 +0000558 TemplateParameterList *TemplateParameters = TD->getTemplateParameters();
559 mangleTemplateArgs(*TemplateParameters, TemplateArgs, NumTemplateArgs);
Anders Carlsson7624f212009-09-18 02:42:01 +0000560 } else {
561 mangleNestedName(TD, TemplateArgs, NumTemplateArgs);
562 }
563}
564
Anders Carlsson201ce742009-09-17 03:17:01 +0000565void CXXNameMangler::mangleUnscopedName(const NamedDecl *ND) {
566 // <unscoped-name> ::= <unqualified-name>
567 // ::= St <unqualified-name> # ::std::
568 if (isStdNamespace(ND->getDeclContext()))
569 Out << "St";
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000570
Anders Carlsson201ce742009-09-17 03:17:01 +0000571 mangleUnqualifiedName(ND);
572}
573
Anders Carlsson0fa6df42009-09-26 19:45:45 +0000574void CXXNameMangler::mangleUnscopedTemplateName(const TemplateDecl *ND) {
Anders Carlsson201ce742009-09-17 03:17:01 +0000575 // <unscoped-template-name> ::= <unscoped-name>
576 // ::= <substitution>
Anders Carlsson7624f212009-09-18 02:42:01 +0000577 if (mangleSubstitution(ND))
Anders Carlsson03c9d532009-09-17 04:02:31 +0000578 return;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000579
Douglas Gregor32fb4e12010-02-05 20:45:00 +0000580 // <template-template-param> ::= <template-param>
581 if (const TemplateTemplateParmDecl *TTP
582 = dyn_cast<TemplateTemplateParmDecl>(ND)) {
583 mangleTemplateParameter(TTP->getIndex());
584 return;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000585 }
Douglas Gregor32fb4e12010-02-05 20:45:00 +0000586
Anders Carlsson1668f202009-09-26 20:13:56 +0000587 mangleUnscopedName(ND->getTemplatedDecl());
Anders Carlsson7624f212009-09-18 02:42:01 +0000588 addSubstitution(ND);
Anders Carlsson201ce742009-09-17 03:17:01 +0000589}
590
Douglas Gregor1e9268e2010-04-28 05:58:56 +0000591void CXXNameMangler::mangleUnscopedTemplateName(TemplateName Template) {
592 // <unscoped-template-name> ::= <unscoped-name>
593 // ::= <substitution>
594 if (TemplateDecl *TD = Template.getAsTemplateDecl())
595 return mangleUnscopedTemplateName(TD);
Sean Huntc3021132010-05-05 15:23:54 +0000596
Douglas Gregor1e9268e2010-04-28 05:58:56 +0000597 if (mangleSubstitution(Template))
598 return;
599
600 // FIXME: How to cope with operators here?
601 DependentTemplateName *Dependent = Template.getAsDependentTemplateName();
602 assert(Dependent && "Not a dependent template name?");
603 if (!Dependent->isIdentifier()) {
604 // FIXME: We can't possibly know the arity of the operator here!
605 Diagnostic &Diags = Context.getDiags();
606 unsigned DiagID = Diags.getCustomDiagID(Diagnostic::Error,
607 "cannot mangle dependent operator name");
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000608 Diags.Report(DiagID);
Douglas Gregor1e9268e2010-04-28 05:58:56 +0000609 return;
610 }
Sean Huntc3021132010-05-05 15:23:54 +0000611
Douglas Gregor1e9268e2010-04-28 05:58:56 +0000612 mangleSourceName(Dependent->getIdentifier());
613 addSubstitution(Template);
614}
615
John McCall1b600522011-04-24 03:07:16 +0000616void CXXNameMangler::mangleFloat(const llvm::APFloat &f) {
617 // ABI:
618 // Floating-point literals are encoded using a fixed-length
619 // lowercase hexadecimal string corresponding to the internal
620 // representation (IEEE on Itanium), high-order bytes first,
621 // without leading zeroes. For example: "Lf bf800000 E" is -1.0f
622 // on Itanium.
623 // APInt::toString uses uppercase hexadecimal, and it's not really
624 // worth embellishing that interface for this use case, so we just
625 // do a second pass to lowercase things.
626 typedef llvm::SmallString<20> buffer_t;
627 buffer_t buffer;
628 f.bitcastToAPInt().toString(buffer, 16, false);
629
630 for (buffer_t::iterator i = buffer.begin(), e = buffer.end(); i != e; ++i)
631 if (isupper(*i)) *i = tolower(*i);
632
633 Out.write(buffer.data(), buffer.size());
John McCall0512e482010-07-14 04:20:34 +0000634}
635
636void CXXNameMangler::mangleNumber(const llvm::APSInt &Value) {
637 if (Value.isSigned() && Value.isNegative()) {
638 Out << 'n';
639 Value.abs().print(Out, true);
640 } else
641 Value.print(Out, Value.isSigned());
642}
643
Anders Carlssona94822e2009-11-26 02:32:05 +0000644void CXXNameMangler::mangleNumber(int64_t Number) {
645 // <number> ::= [n] <non-negative decimal integer>
646 if (Number < 0) {
647 Out << 'n';
648 Number = -Number;
649 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000650
Anders Carlssona94822e2009-11-26 02:32:05 +0000651 Out << Number;
652}
653
Anders Carlsson19879c92010-03-23 17:17:29 +0000654void CXXNameMangler::mangleCallOffset(int64_t NonVirtual, int64_t Virtual) {
Mike Stump141c5af2009-09-02 00:25:38 +0000655 // <call-offset> ::= h <nv-offset> _
656 // ::= v <v-offset> _
657 // <nv-offset> ::= <offset number> # non-virtual base override
Anders Carlssona94822e2009-11-26 02:32:05 +0000658 // <v-offset> ::= <offset number> _ <virtual offset number>
Mike Stump141c5af2009-09-02 00:25:38 +0000659 // # virtual base override, with vcall offset
Anders Carlsson19879c92010-03-23 17:17:29 +0000660 if (!Virtual) {
Anders Carlssona94822e2009-11-26 02:32:05 +0000661 Out << 'h';
Anders Carlsson19879c92010-03-23 17:17:29 +0000662 mangleNumber(NonVirtual);
Anders Carlssona94822e2009-11-26 02:32:05 +0000663 Out << '_';
664 return;
Mike Stump141c5af2009-09-02 00:25:38 +0000665 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000666
Anders Carlssona94822e2009-11-26 02:32:05 +0000667 Out << 'v';
Anders Carlsson19879c92010-03-23 17:17:29 +0000668 mangleNumber(NonVirtual);
Anders Carlssona94822e2009-11-26 02:32:05 +0000669 Out << '_';
Anders Carlsson19879c92010-03-23 17:17:29 +0000670 mangleNumber(Virtual);
Anders Carlssona94822e2009-11-26 02:32:05 +0000671 Out << '_';
Mike Stump9124bcc2009-09-02 00:56:18 +0000672}
673
John McCall4f4e4132011-05-04 01:45:19 +0000674void CXXNameMangler::manglePrefix(QualType type) {
John McCalla0ce15c2011-04-24 08:23:24 +0000675 if (const TemplateSpecializationType *TST =
676 type->getAs<TemplateSpecializationType>()) {
677 if (!mangleSubstitution(QualType(TST, 0))) {
678 mangleTemplatePrefix(TST->getTemplateName());
Sean Huntc3021132010-05-05 15:23:54 +0000679
Douglas Gregoraa2187d2011-02-28 00:04:36 +0000680 // FIXME: GCC does not appear to mangle the template arguments when
681 // the template in question is a dependent template name. Should we
682 // emulate that badness?
John McCalla0ce15c2011-04-24 08:23:24 +0000683 mangleTemplateArgs(TST->getTemplateName(), TST->getArgs(),
684 TST->getNumArgs());
685 addSubstitution(QualType(TST, 0));
Rafael Espindola9b35b252010-03-17 04:28:11 +0000686 }
John McCalla0ce15c2011-04-24 08:23:24 +0000687 } else if (const DependentTemplateSpecializationType *DTST
688 = type->getAs<DependentTemplateSpecializationType>()) {
689 TemplateName Template
690 = getASTContext().getDependentTemplateName(DTST->getQualifier(),
691 DTST->getIdentifier());
692 mangleTemplatePrefix(Template);
693
694 // FIXME: GCC does not appear to mangle the template arguments when
695 // the template in question is a dependent template name. Should we
696 // emulate that badness?
697 mangleTemplateArgs(Template, DTST->getArgs(), DTST->getNumArgs());
698 } else {
699 // We use the QualType mangle type variant here because it handles
700 // substitutions.
701 mangleType(type);
John McCall1dd73832010-02-04 01:42:13 +0000702 }
703}
704
John McCall4f4e4132011-05-04 01:45:19 +0000705/// Returns true if the given type, appearing within an
706/// unresolved-name, should be mangled as an unresolved-type.
707bool CXXNameMangler::isUnresolvedType(const Type *type) {
708 // <unresolved-type> ::= <template-param>
709 // ::= <decltype>
710 // ::= <template-template-param> <template-args>
711 // (this last is not official yet)
712
713 if (isa<TemplateTypeParmType>(type)) return true;
714 if (isa<DecltypeType>(type)) return true;
715 // typeof?
716 if (const TemplateSpecializationType *tst =
717 dyn_cast<TemplateSpecializationType>(type)) {
718 TemplateDecl *temp = tst->getTemplateName().getAsTemplateDecl();
719 if (temp && isa<TemplateTemplateParmDecl>(temp))
720 return true;
721 }
722 return false;
723}
724
725void CXXNameMangler::mangleUnresolvedType(const Type *type) {
726 // This seems to be do everything we want.
727 mangleType(QualType(type, 0));
728}
729
John McCalla0ce15c2011-04-24 08:23:24 +0000730/// Mangle everything prior to the base-unresolved-name in an unresolved-name.
731///
732/// \param firstQualifierLookup - the entity found by unqualified lookup
733/// for the first name in the qualifier, if this is for a member expression
734/// \param recursive - true if this is being called recursively,
735/// i.e. if there is more prefix "to the right".
736void CXXNameMangler::mangleUnresolvedPrefix(NestedNameSpecifier *qualifier,
737 NamedDecl *firstQualifierLookup,
738 bool recursive) {
John McCall1dd73832010-02-04 01:42:13 +0000739
John McCalla0ce15c2011-04-24 08:23:24 +0000740 // x, ::x
741 // <unresolved-name> ::= [gs] <base-unresolved-name>
742
743 // T::x / decltype(p)::x
744 // <unresolved-name> ::= sr <unresolved-type> <base-unresolved-name>
745
746 // T::N::x /decltype(p)::N::x
747 // <unresolved-name> ::= srN <unresolved-type> <unresolved-qualifier-level>+ E
748 // <base-unresolved-name>
749
750 // A::x, N::y, A<T>::z; "gs" means leading "::"
751 // <unresolved-name> ::= [gs] sr <unresolved-qualifier-level>+ E
752 // <base-unresolved-name>
753
754 switch (qualifier->getKind()) {
755 case NestedNameSpecifier::Global:
756 Out << "gs";
757
758 // We want an 'sr' unless this is the entire NNS.
759 if (recursive)
760 Out << "sr";
761
762 // We never want an 'E' here.
763 return;
764
765 case NestedNameSpecifier::Namespace:
766 if (qualifier->getPrefix())
767 mangleUnresolvedPrefix(qualifier->getPrefix(), firstQualifierLookup,
768 /*recursive*/ true);
769 else
770 Out << "sr";
771 mangleSourceName(qualifier->getAsNamespace()->getIdentifier());
772 break;
773 case NestedNameSpecifier::NamespaceAlias:
774 if (qualifier->getPrefix())
775 mangleUnresolvedPrefix(qualifier->getPrefix(), firstQualifierLookup,
776 /*recursive*/ true);
777 else
778 Out << "sr";
779 mangleSourceName(qualifier->getAsNamespaceAlias()->getIdentifier());
780 break;
781
782 case NestedNameSpecifier::TypeSpec:
783 case NestedNameSpecifier::TypeSpecWithTemplate: {
John McCall4f4e4132011-05-04 01:45:19 +0000784 const Type *type = qualifier->getAsType();
John McCalla0ce15c2011-04-24 08:23:24 +0000785
John McCall4f4e4132011-05-04 01:45:19 +0000786 // We only want to use an unresolved-type encoding if this is one of:
787 // - a decltype
788 // - a template type parameter
789 // - a template template parameter with arguments
790 // In all of these cases, we should have no prefix.
791 if (qualifier->getPrefix()) {
792 mangleUnresolvedPrefix(qualifier->getPrefix(), firstQualifierLookup,
793 /*recursive*/ true);
794 } else {
795 // Otherwise, all the cases want this.
796 Out << "sr";
John McCalla0ce15c2011-04-24 08:23:24 +0000797
John McCall4f4e4132011-05-04 01:45:19 +0000798 if (isUnresolvedType(type)) {
799 // We only get here recursively if we're followed by identifiers.
800 if (recursive) Out << 'N';
801 mangleUnresolvedType(type);
John McCalla0ce15c2011-04-24 08:23:24 +0000802
John McCall4f4e4132011-05-04 01:45:19 +0000803 // We never want to print 'E' directly after an unresolved-type,
804 // so we return directly.
805 return;
806 }
807 }
808
809 assert(!isUnresolvedType(type));
810
811 // Only certain other types are valid as prefixes; enumerate them.
812 // FIXME: can we get ElaboratedTypes here?
813 // FIXME: SubstTemplateTypeParmType?
814 if (const TagType *t = dyn_cast<TagType>(type)) {
815 mangleSourceName(t->getDecl()->getIdentifier());
816 } else if (const TypedefType *t = dyn_cast<TypedefType>(type)) {
817 mangleSourceName(t->getDecl()->getIdentifier());
818 } else if (const UnresolvedUsingType *t
819 = dyn_cast<UnresolvedUsingType>(type)) {
820 mangleSourceName(t->getDecl()->getIdentifier());
821 } else if (const DependentNameType *t
822 = dyn_cast<DependentNameType>(type)) {
823 mangleSourceName(t->getIdentifier());
824 } else if (const TemplateSpecializationType *tst
825 = dyn_cast<TemplateSpecializationType>(type)) {
826 TemplateDecl *temp = tst->getTemplateName().getAsTemplateDecl();
827 assert(temp && "no template for template specialization type");
828 mangleSourceName(temp->getIdentifier());
829 mangleUnresolvedTemplateArgs(tst->getArgs(), tst->getNumArgs());
830 } else if (const DependentTemplateSpecializationType *tst
831 = dyn_cast<DependentTemplateSpecializationType>(type)) {
832 mangleSourceName(tst->getIdentifier());
833 mangleUnresolvedTemplateArgs(tst->getArgs(), tst->getNumArgs());
834 } else {
835 llvm_unreachable("unexpected type in nested name specifier!");
836 }
837 break;
John McCalla0ce15c2011-04-24 08:23:24 +0000838 }
839
840 case NestedNameSpecifier::Identifier:
841 // Member expressions can have these without prefixes.
842 if (qualifier->getPrefix()) {
843 mangleUnresolvedPrefix(qualifier->getPrefix(), firstQualifierLookup,
844 /*recursive*/ true);
845 } else if (firstQualifierLookup) {
846
847 // Try to make a proper qualifier out of the lookup result, and
848 // then just recurse on that.
849 NestedNameSpecifier *newQualifier;
850 if (TypeDecl *typeDecl = dyn_cast<TypeDecl>(firstQualifierLookup)) {
851 QualType type = getASTContext().getTypeDeclType(typeDecl);
852
853 // Pretend we had a different nested name specifier.
854 newQualifier = NestedNameSpecifier::Create(getASTContext(),
855 /*prefix*/ 0,
856 /*template*/ false,
857 type.getTypePtr());
858 } else if (NamespaceDecl *nspace =
859 dyn_cast<NamespaceDecl>(firstQualifierLookup)) {
860 newQualifier = NestedNameSpecifier::Create(getASTContext(),
861 /*prefix*/ 0,
862 nspace);
863 } else if (NamespaceAliasDecl *alias =
864 dyn_cast<NamespaceAliasDecl>(firstQualifierLookup)) {
865 newQualifier = NestedNameSpecifier::Create(getASTContext(),
866 /*prefix*/ 0,
867 alias);
868 } else {
869 // No sensible mangling to do here.
870 newQualifier = 0;
871 }
872
873 if (newQualifier)
874 return mangleUnresolvedPrefix(newQualifier, /*lookup*/ 0, recursive);
875
876 } else {
877 Out << "sr";
878 }
879
880 mangleSourceName(qualifier->getAsIdentifier());
881 break;
882 }
883
884 // If this was the innermost part of the NNS, and we fell out to
885 // here, append an 'E'.
886 if (!recursive)
887 Out << 'E';
888}
889
890/// Mangle an unresolved-name, which is generally used for names which
891/// weren't resolved to specific entities.
892void CXXNameMangler::mangleUnresolvedName(NestedNameSpecifier *qualifier,
893 NamedDecl *firstQualifierLookup,
894 DeclarationName name,
895 unsigned knownArity) {
896 if (qualifier) mangleUnresolvedPrefix(qualifier, firstQualifierLookup);
897 mangleUnqualifiedName(0, name, knownArity);
John McCall1dd73832010-02-04 01:42:13 +0000898}
899
Anders Carlsson6f7e2f42010-06-08 14:49:03 +0000900static const FieldDecl *FindFirstNamedDataMember(const RecordDecl *RD) {
901 assert(RD->isAnonymousStructOrUnion() &&
902 "Expected anonymous struct or union!");
903
904 for (RecordDecl::field_iterator I = RD->field_begin(), E = RD->field_end();
905 I != E; ++I) {
906 const FieldDecl *FD = *I;
907
908 if (FD->getIdentifier())
909 return FD;
910
911 if (const RecordType *RT = FD->getType()->getAs<RecordType>()) {
912 if (const FieldDecl *NamedDataMember =
913 FindFirstNamedDataMember(RT->getDecl()))
914 return NamedDataMember;
915 }
916 }
917
918 // We didn't find a named data member.
919 return 0;
920}
921
John McCall1dd73832010-02-04 01:42:13 +0000922void CXXNameMangler::mangleUnqualifiedName(const NamedDecl *ND,
923 DeclarationName Name,
924 unsigned KnownArity) {
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000925 // <unqualified-name> ::= <operator-name>
Mike Stump1eb44332009-09-09 15:08:12 +0000926 // ::= <ctor-dtor-name>
927 // ::= <source-name>
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000928 switch (Name.getNameKind()) {
Anders Carlssonc4355b62009-10-07 01:45:02 +0000929 case DeclarationName::Identifier: {
Anders Carlssonc4355b62009-10-07 01:45:02 +0000930 if (const IdentifierInfo *II = Name.getAsIdentifierInfo()) {
Sean Hunt31455252010-01-24 03:04:27 +0000931 // We must avoid conflicts between internally- and externally-
John McCall74990f42011-03-22 06:34:45 +0000932 // linked variable and function declaration names in the same TU:
933 // void test() { extern void foo(); }
934 // static void foo();
935 // This naming convention is the same as that followed by GCC,
936 // though it shouldn't actually matter.
937 if (ND && ND->getLinkage() == InternalLinkage &&
Sean Hunt31455252010-01-24 03:04:27 +0000938 ND->getDeclContext()->isFileContext())
939 Out << 'L';
940
Anders Carlssonc4355b62009-10-07 01:45:02 +0000941 mangleSourceName(II);
942 break;
943 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000944
John McCall1dd73832010-02-04 01:42:13 +0000945 // Otherwise, an anonymous entity. We must have a declaration.
946 assert(ND && "mangling empty name without declaration");
947
948 if (const NamespaceDecl *NS = dyn_cast<NamespaceDecl>(ND)) {
949 if (NS->isAnonymousNamespace()) {
950 // This is how gcc mangles these names.
951 Out << "12_GLOBAL__N_1";
952 break;
953 }
954 }
955
Anders Carlsson6f7e2f42010-06-08 14:49:03 +0000956 if (const VarDecl *VD = dyn_cast<VarDecl>(ND)) {
957 // We must have an anonymous union or struct declaration.
958 const RecordDecl *RD =
959 cast<RecordDecl>(VD->getType()->getAs<RecordType>()->getDecl());
960
961 // Itanium C++ ABI 5.1.2:
962 //
963 // For the purposes of mangling, the name of an anonymous union is
964 // considered to be the name of the first named data member found by a
965 // pre-order, depth-first, declaration-order walk of the data members of
966 // the anonymous union. If there is no such data member (i.e., if all of
967 // the data members in the union are unnamed), then there is no way for
968 // a program to refer to the anonymous union, and there is therefore no
969 // need to mangle its name.
970 const FieldDecl *FD = FindFirstNamedDataMember(RD);
John McCall7121c8f2010-08-05 22:02:13 +0000971
972 // It's actually possible for various reasons for us to get here
973 // with an empty anonymous struct / union. Fortunately, it
974 // doesn't really matter what name we generate.
975 if (!FD) break;
Anders Carlsson6f7e2f42010-06-08 14:49:03 +0000976 assert(FD->getIdentifier() && "Data member name isn't an identifier!");
977
978 mangleSourceName(FD->getIdentifier());
979 break;
980 }
981
Anders Carlssonc4355b62009-10-07 01:45:02 +0000982 // We must have an anonymous struct.
983 const TagDecl *TD = cast<TagDecl>(ND);
Richard Smith162e1c12011-04-15 14:24:37 +0000984 if (const TypedefNameDecl *D = TD->getTypedefNameForAnonDecl()) {
Anders Carlssonc4355b62009-10-07 01:45:02 +0000985 assert(TD->getDeclContext() == D->getDeclContext() &&
986 "Typedef should not be in another decl context!");
987 assert(D->getDeclName().getAsIdentifierInfo() &&
988 "Typedef was not named!");
989 mangleSourceName(D->getDeclName().getAsIdentifierInfo());
990 break;
991 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000992
Anders Carlssonc4355b62009-10-07 01:45:02 +0000993 // Get a unique id for the anonymous struct.
994 uint64_t AnonStructId = Context.getAnonymousStructId(TD);
995
996 // Mangle it as a source name in the form
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000997 // [n] $_<id>
Anders Carlssonc4355b62009-10-07 01:45:02 +0000998 // where n is the length of the string.
999 llvm::SmallString<8> Str;
1000 Str += "$_";
1001 Str += llvm::utostr(AnonStructId);
1002
1003 Out << Str.size();
1004 Out << Str.str();
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001005 break;
Anders Carlssonc4355b62009-10-07 01:45:02 +00001006 }
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001007
1008 case DeclarationName::ObjCZeroArgSelector:
1009 case DeclarationName::ObjCOneArgSelector:
1010 case DeclarationName::ObjCMultiArgSelector:
1011 assert(false && "Can't mangle Objective-C selector names here!");
1012 break;
1013
1014 case DeclarationName::CXXConstructorName:
Anders Carlsson27ae5362009-04-17 01:58:57 +00001015 if (ND == Structor)
Mike Stump141c5af2009-09-02 00:25:38 +00001016 // If the named decl is the C++ constructor we're mangling, use the type
1017 // we were given.
Anders Carlsson27ae5362009-04-17 01:58:57 +00001018 mangleCXXCtorType(static_cast<CXXCtorType>(StructorType));
Anders Carlsson3ac86b52009-04-15 05:36:58 +00001019 else
1020 // Otherwise, use the complete constructor name. This is relevant if a
1021 // class with a constructor is declared within a constructor.
1022 mangleCXXCtorType(Ctor_Complete);
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001023 break;
1024
1025 case DeclarationName::CXXDestructorName:
Anders Carlsson27ae5362009-04-17 01:58:57 +00001026 if (ND == Structor)
Mike Stump141c5af2009-09-02 00:25:38 +00001027 // If the named decl is the C++ destructor we're mangling, use the type we
1028 // were given.
Anders Carlsson27ae5362009-04-17 01:58:57 +00001029 mangleCXXDtorType(static_cast<CXXDtorType>(StructorType));
1030 else
1031 // Otherwise, use the complete destructor name. This is relevant if a
1032 // class with a destructor is declared within a destructor.
1033 mangleCXXDtorType(Dtor_Complete);
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001034 break;
1035
1036 case DeclarationName::CXXConversionFunctionName:
Mike Stump1eb44332009-09-09 15:08:12 +00001037 // <operator-name> ::= cv <type> # (cast)
Douglas Gregor219cc612009-02-13 01:28:03 +00001038 Out << "cv";
Anders Carlssonb5404912009-10-07 01:06:45 +00001039 mangleType(Context.getASTContext().getCanonicalType(Name.getCXXNameType()));
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001040 break;
1041
Anders Carlsson8257d412009-12-22 06:36:32 +00001042 case DeclarationName::CXXOperatorName: {
John McCall1dd73832010-02-04 01:42:13 +00001043 unsigned Arity;
1044 if (ND) {
1045 Arity = cast<FunctionDecl>(ND)->getNumParams();
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001046
John McCall1dd73832010-02-04 01:42:13 +00001047 // If we have a C++ member function, we need to include the 'this' pointer.
1048 // FIXME: This does not make sense for operators that are static, but their
1049 // names stay the same regardless of the arity (operator new for instance).
1050 if (isa<CXXMethodDecl>(ND))
1051 Arity++;
1052 } else
1053 Arity = KnownArity;
1054
Anders Carlsson8257d412009-12-22 06:36:32 +00001055 mangleOperatorName(Name.getCXXOverloadedOperator(), Arity);
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001056 break;
Anders Carlsson8257d412009-12-22 06:36:32 +00001057 }
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001058
Sean Hunt3e518bd2009-11-29 07:34:05 +00001059 case DeclarationName::CXXLiteralOperatorName:
Sean Hunt5dd6b392009-12-04 21:11:13 +00001060 // FIXME: This mangling is not yet official.
Sean Hunt2421f662009-12-04 21:01:37 +00001061 Out << "li";
Sean Hunt3e518bd2009-11-29 07:34:05 +00001062 mangleSourceName(Name.getCXXLiteralIdentifier());
1063 break;
1064
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001065 case DeclarationName::CXXUsingDirective:
1066 assert(false && "Can't mangle a using directive name!");
Douglas Gregor219cc612009-02-13 01:28:03 +00001067 break;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001068 }
1069}
1070
1071void CXXNameMangler::mangleSourceName(const IdentifierInfo *II) {
1072 // <source-name> ::= <positive length number> <identifier>
1073 // <number> ::= [n] <non-negative decimal integer>
1074 // <identifier> ::= <unqualified source code identifier>
1075 Out << II->getLength() << II->getName();
1076}
1077
Eli Friedman7facf842009-12-02 20:32:49 +00001078void CXXNameMangler::mangleNestedName(const NamedDecl *ND,
Fariborz Jahanian57058532010-03-03 19:41:08 +00001079 const DeclContext *DC,
1080 bool NoFunction) {
Douglas Gregor0a9a6d62011-01-26 17:36:28 +00001081 // <nested-name>
1082 // ::= N [<CV-qualifiers>] [<ref-qualifier>] <prefix> <unqualified-name> E
1083 // ::= N [<CV-qualifiers>] [<ref-qualifier>] <template-prefix>
1084 // <template-args> E
Anders Carlssond99edc42009-09-26 03:55:37 +00001085
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001086 Out << 'N';
Douglas Gregor0a9a6d62011-01-26 17:36:28 +00001087 if (const CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(ND)) {
John McCall0953e762009-09-24 19:53:00 +00001088 mangleQualifiers(Qualifiers::fromCVRMask(Method->getTypeQualifiers()));
Douglas Gregor0a9a6d62011-01-26 17:36:28 +00001089 mangleRefQualifier(Method->getRefQualifier());
1090 }
1091
Anders Carlsson2744a062009-09-18 19:00:18 +00001092 // Check if we have a template.
1093 const TemplateArgumentList *TemplateArgs = 0;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00001094 if (const TemplateDecl *TD = isTemplate(ND, TemplateArgs)) {
Anders Carlsson2744a062009-09-18 19:00:18 +00001095 mangleTemplatePrefix(TD);
Rafael Espindolad9800722010-03-11 14:07:00 +00001096 TemplateParameterList *TemplateParameters = TD->getTemplateParameters();
1097 mangleTemplateArgs(*TemplateParameters, *TemplateArgs);
Fariborz Jahanian57058532010-03-03 19:41:08 +00001098 }
1099 else {
1100 manglePrefix(DC, NoFunction);
Anders Carlsson7482e242009-09-18 04:29:09 +00001101 mangleUnqualifiedName(ND);
1102 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00001103
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001104 Out << 'E';
1105}
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00001106void CXXNameMangler::mangleNestedName(const TemplateDecl *TD,
Anders Carlsson7624f212009-09-18 02:42:01 +00001107 const TemplateArgument *TemplateArgs,
1108 unsigned NumTemplateArgs) {
Anders Carlssone45117b2009-09-27 19:53:49 +00001109 // <nested-name> ::= N [<CV-qualifiers>] <template-prefix> <template-args> E
1110
Anders Carlsson7624f212009-09-18 02:42:01 +00001111 Out << 'N';
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00001112
Anders Carlssone45117b2009-09-27 19:53:49 +00001113 mangleTemplatePrefix(TD);
Rafael Espindolad9800722010-03-11 14:07:00 +00001114 TemplateParameterList *TemplateParameters = TD->getTemplateParameters();
1115 mangleTemplateArgs(*TemplateParameters, TemplateArgs, NumTemplateArgs);
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00001116
Anders Carlsson7624f212009-09-18 02:42:01 +00001117 Out << 'E';
1118}
1119
Anders Carlsson1b42c792009-04-02 16:24:45 +00001120void CXXNameMangler::mangleLocalName(const NamedDecl *ND) {
1121 // <local-name> := Z <function encoding> E <entity name> [<discriminator>]
1122 // := Z <function encoding> E s [<discriminator>]
Mike Stump1eb44332009-09-09 15:08:12 +00001123 // <discriminator> := _ <non-negative number>
Fariborz Jahanian57058532010-03-03 19:41:08 +00001124 const DeclContext *DC = ND->getDeclContext();
Fariborz Jahanian8805fe82011-06-09 19:25:01 +00001125 if (isa<ObjCMethodDecl>(DC) && isa<FunctionDecl>(ND)) {
1126 // Don't add objc method name mangling to locally declared function
1127 mangleUnqualifiedName(ND);
1128 return;
1129 }
1130
Anders Carlsson1b42c792009-04-02 16:24:45 +00001131 Out << 'Z';
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001132
Charles Davis685b1d92010-05-26 18:25:27 +00001133 if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(DC)) {
1134 mangleObjCMethodName(MD);
John McCall82b7d7b2010-10-18 21:28:44 +00001135 } else if (const CXXRecordDecl *RD = GetLocalClassDecl(ND)) {
1136 mangleFunctionEncoding(cast<FunctionDecl>(RD->getDeclContext()));
Fariborz Jahanian57058532010-03-03 19:41:08 +00001137 Out << 'E';
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001138
John McCall82b7d7b2010-10-18 21:28:44 +00001139 // Mangle the name relative to the closest enclosing function.
1140 if (ND == RD) // equality ok because RD derived from ND above
1141 mangleUnqualifiedName(ND);
1142 else
1143 mangleNestedName(ND, DC, true /*NoFunction*/);
1144
Fariborz Jahanian4819ac42010-03-04 01:02:03 +00001145 unsigned disc;
John McCall82b7d7b2010-10-18 21:28:44 +00001146 if (Context.getNextDiscriminator(RD, disc)) {
Fariborz Jahanian4819ac42010-03-04 01:02:03 +00001147 if (disc < 10)
1148 Out << '_' << disc;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001149 else
Fariborz Jahanian4819ac42010-03-04 01:02:03 +00001150 Out << "__" << disc << '_';
1151 }
Fariborz Jahanian57058532010-03-03 19:41:08 +00001152
1153 return;
1154 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001155 else
Fariborz Jahanian57058532010-03-03 19:41:08 +00001156 mangleFunctionEncoding(cast<FunctionDecl>(DC));
Anders Carlsson7b06f6c2009-12-10 03:14:39 +00001157
Anders Carlsson1b42c792009-04-02 16:24:45 +00001158 Out << 'E';
Eli Friedman6f9f25d2009-12-11 20:21:38 +00001159 mangleUnqualifiedName(ND);
Anders Carlsson1b42c792009-04-02 16:24:45 +00001160}
1161
John McCalla0ce15c2011-04-24 08:23:24 +00001162void CXXNameMangler::manglePrefix(NestedNameSpecifier *qualifier) {
1163 switch (qualifier->getKind()) {
1164 case NestedNameSpecifier::Global:
1165 // nothing
1166 return;
1167
1168 case NestedNameSpecifier::Namespace:
1169 mangleName(qualifier->getAsNamespace());
1170 return;
1171
1172 case NestedNameSpecifier::NamespaceAlias:
1173 mangleName(qualifier->getAsNamespaceAlias()->getNamespace());
1174 return;
1175
1176 case NestedNameSpecifier::TypeSpec:
1177 case NestedNameSpecifier::TypeSpecWithTemplate:
John McCall4f4e4132011-05-04 01:45:19 +00001178 manglePrefix(QualType(qualifier->getAsType(), 0));
John McCalla0ce15c2011-04-24 08:23:24 +00001179 return;
1180
1181 case NestedNameSpecifier::Identifier:
1182 // Member expressions can have these without prefixes, but that
1183 // should end up in mangleUnresolvedPrefix instead.
1184 assert(qualifier->getPrefix());
1185 manglePrefix(qualifier->getPrefix());
1186
1187 mangleSourceName(qualifier->getAsIdentifier());
1188 return;
1189 }
1190
1191 llvm_unreachable("unexpected nested name specifier");
1192}
1193
Fariborz Jahanian57058532010-03-03 19:41:08 +00001194void CXXNameMangler::manglePrefix(const DeclContext *DC, bool NoFunction) {
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001195 // <prefix> ::= <prefix> <unqualified-name>
1196 // ::= <template-prefix> <template-args>
1197 // ::= <template-param>
1198 // ::= # empty
1199 // ::= <substitution>
Anders Carlsson6862fc72009-09-17 04:16:28 +00001200
Anders Carlssonadd28822009-09-22 20:33:31 +00001201 while (isa<LinkageSpecDecl>(DC))
1202 DC = DC->getParent();
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00001203
Anders Carlsson9263e912009-09-18 18:39:58 +00001204 if (DC->isTranslationUnit())
1205 return;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00001206
Douglas Gregor35415f52010-05-25 17:04:15 +00001207 if (const BlockDecl *Block = dyn_cast<BlockDecl>(DC)) {
1208 manglePrefix(DC->getParent(), NoFunction);
1209 llvm::SmallString<64> Name;
Rafael Espindolac4850c22011-02-10 23:59:36 +00001210 llvm::raw_svector_ostream NameStream(Name);
1211 Context.mangleBlock(Block, NameStream);
1212 NameStream.flush();
Douglas Gregor35415f52010-05-25 17:04:15 +00001213 Out << Name.size() << Name;
1214 return;
1215 }
1216
Anders Carlsson6862fc72009-09-17 04:16:28 +00001217 if (mangleSubstitution(cast<NamedDecl>(DC)))
1218 return;
Anders Carlsson7482e242009-09-18 04:29:09 +00001219
Anders Carlsson2ee3fca2009-09-18 20:11:09 +00001220 // Check if we have a template.
1221 const TemplateArgumentList *TemplateArgs = 0;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00001222 if (const TemplateDecl *TD = isTemplate(cast<NamedDecl>(DC), TemplateArgs)) {
Anders Carlsson2ee3fca2009-09-18 20:11:09 +00001223 mangleTemplatePrefix(TD);
Rafael Espindolad9800722010-03-11 14:07:00 +00001224 TemplateParameterList *TemplateParameters = TD->getTemplateParameters();
1225 mangleTemplateArgs(*TemplateParameters, *TemplateArgs);
Fariborz Jahanian57058532010-03-03 19:41:08 +00001226 }
Douglas Gregor35415f52010-05-25 17:04:15 +00001227 else if(NoFunction && (isa<FunctionDecl>(DC) || isa<ObjCMethodDecl>(DC)))
Fariborz Jahanian57058532010-03-03 19:41:08 +00001228 return;
Douglas Gregor35415f52010-05-25 17:04:15 +00001229 else if (const ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(DC))
1230 mangleObjCMethodName(Method);
Fariborz Jahanian57058532010-03-03 19:41:08 +00001231 else {
1232 manglePrefix(DC->getParent(), NoFunction);
Anders Carlsson2ee3fca2009-09-18 20:11:09 +00001233 mangleUnqualifiedName(cast<NamedDecl>(DC));
1234 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00001235
Anders Carlsson6862fc72009-09-17 04:16:28 +00001236 addSubstitution(cast<NamedDecl>(DC));
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001237}
1238
Douglas Gregor20f0cc72010-04-23 03:10:43 +00001239void CXXNameMangler::mangleTemplatePrefix(TemplateName Template) {
1240 // <template-prefix> ::= <prefix> <template unqualified-name>
1241 // ::= <template-param>
1242 // ::= <substitution>
1243 if (TemplateDecl *TD = Template.getAsTemplateDecl())
1244 return mangleTemplatePrefix(TD);
1245
1246 if (QualifiedTemplateName *Qualified = Template.getAsQualifiedTemplateName())
John McCalla0ce15c2011-04-24 08:23:24 +00001247 manglePrefix(Qualified->getQualifier());
Sean Huntc3021132010-05-05 15:23:54 +00001248
Douglas Gregor20f0cc72010-04-23 03:10:43 +00001249 if (OverloadedTemplateStorage *Overloaded
1250 = Template.getAsOverloadedTemplate()) {
Sean Huntc3021132010-05-05 15:23:54 +00001251 mangleUnqualifiedName(0, (*Overloaded->begin())->getDeclName(),
Douglas Gregor20f0cc72010-04-23 03:10:43 +00001252 UnknownArity);
1253 return;
1254 }
Sean Huntc3021132010-05-05 15:23:54 +00001255
Douglas Gregor20f0cc72010-04-23 03:10:43 +00001256 DependentTemplateName *Dependent = Template.getAsDependentTemplateName();
1257 assert(Dependent && "Unknown template name kind?");
John McCalla0ce15c2011-04-24 08:23:24 +00001258 manglePrefix(Dependent->getQualifier());
Douglas Gregor1e9268e2010-04-28 05:58:56 +00001259 mangleUnscopedTemplateName(Template);
Douglas Gregor20f0cc72010-04-23 03:10:43 +00001260}
1261
Anders Carlsson0fa6df42009-09-26 19:45:45 +00001262void CXXNameMangler::mangleTemplatePrefix(const TemplateDecl *ND) {
Anders Carlsson7482e242009-09-18 04:29:09 +00001263 // <template-prefix> ::= <prefix> <template unqualified-name>
1264 // ::= <template-param>
1265 // ::= <substitution>
Douglas Gregor32fb4e12010-02-05 20:45:00 +00001266 // <template-template-param> ::= <template-param>
1267 // <substitution>
Anders Carlsson7482e242009-09-18 04:29:09 +00001268
Anders Carlssonaeb85372009-09-26 22:18:22 +00001269 if (mangleSubstitution(ND))
1270 return;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00001271
Douglas Gregor32fb4e12010-02-05 20:45:00 +00001272 // <template-template-param> ::= <template-param>
1273 if (const TemplateTemplateParmDecl *TTP
1274 = dyn_cast<TemplateTemplateParmDecl>(ND)) {
1275 mangleTemplateParameter(TTP->getIndex());
1276 return;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001277 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00001278
Anders Carlssonaa73ab12009-09-18 18:47:07 +00001279 manglePrefix(ND->getDeclContext());
Anders Carlsson1668f202009-09-26 20:13:56 +00001280 mangleUnqualifiedName(ND->getTemplatedDecl());
Anders Carlssonaeb85372009-09-26 22:18:22 +00001281 addSubstitution(ND);
Anders Carlsson7482e242009-09-18 04:29:09 +00001282}
1283
John McCallb6f532e2010-07-14 06:43:17 +00001284/// Mangles a template name under the production <type>. Required for
1285/// template template arguments.
1286/// <type> ::= <class-enum-type>
1287/// ::= <template-param>
1288/// ::= <substitution>
1289void CXXNameMangler::mangleType(TemplateName TN) {
1290 if (mangleSubstitution(TN))
1291 return;
1292
1293 TemplateDecl *TD = 0;
1294
1295 switch (TN.getKind()) {
1296 case TemplateName::QualifiedTemplate:
1297 TD = TN.getAsQualifiedTemplateName()->getTemplateDecl();
1298 goto HaveDecl;
1299
1300 case TemplateName::Template:
1301 TD = TN.getAsTemplateDecl();
1302 goto HaveDecl;
1303
1304 HaveDecl:
1305 if (isa<TemplateTemplateParmDecl>(TD))
1306 mangleTemplateParameter(cast<TemplateTemplateParmDecl>(TD)->getIndex());
1307 else
1308 mangleName(TD);
1309 break;
1310
1311 case TemplateName::OverloadedTemplate:
1312 llvm_unreachable("can't mangle an overloaded template name as a <type>");
1313 break;
1314
1315 case TemplateName::DependentTemplate: {
1316 const DependentTemplateName *Dependent = TN.getAsDependentTemplateName();
1317 assert(Dependent->isIdentifier());
1318
1319 // <class-enum-type> ::= <name>
1320 // <name> ::= <nested-name>
John McCalla0ce15c2011-04-24 08:23:24 +00001321 mangleUnresolvedPrefix(Dependent->getQualifier(), 0);
John McCallb6f532e2010-07-14 06:43:17 +00001322 mangleSourceName(Dependent->getIdentifier());
1323 break;
1324 }
1325
Douglas Gregor1aee05d2011-01-15 06:45:20 +00001326 case TemplateName::SubstTemplateTemplateParmPack: {
1327 SubstTemplateTemplateParmPackStorage *SubstPack
1328 = TN.getAsSubstTemplateTemplateParmPack();
1329 mangleTemplateParameter(SubstPack->getParameterPack()->getIndex());
1330 break;
1331 }
John McCallb6f532e2010-07-14 06:43:17 +00001332 }
1333
1334 addSubstitution(TN);
1335}
1336
Mike Stump1eb44332009-09-09 15:08:12 +00001337void
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001338CXXNameMangler::mangleOperatorName(OverloadedOperatorKind OO, unsigned Arity) {
1339 switch (OO) {
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001340 // <operator-name> ::= nw # new
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001341 case OO_New: Out << "nw"; break;
1342 // ::= na # new[]
1343 case OO_Array_New: Out << "na"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001344 // ::= dl # delete
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001345 case OO_Delete: Out << "dl"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001346 // ::= da # delete[]
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001347 case OO_Array_Delete: Out << "da"; break;
1348 // ::= ps # + (unary)
John McCall5e1e89b2010-08-18 19:18:59 +00001349 // ::= pl # + (binary or unknown)
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001350 case OO_Plus:
Anders Carlsson8257d412009-12-22 06:36:32 +00001351 Out << (Arity == 1? "ps" : "pl"); break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001352 // ::= ng # - (unary)
John McCall5e1e89b2010-08-18 19:18:59 +00001353 // ::= mi # - (binary or unknown)
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001354 case OO_Minus:
Anders Carlsson8257d412009-12-22 06:36:32 +00001355 Out << (Arity == 1? "ng" : "mi"); break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001356 // ::= ad # & (unary)
John McCall5e1e89b2010-08-18 19:18:59 +00001357 // ::= an # & (binary or unknown)
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001358 case OO_Amp:
Anders Carlsson8257d412009-12-22 06:36:32 +00001359 Out << (Arity == 1? "ad" : "an"); break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001360 // ::= de # * (unary)
John McCall5e1e89b2010-08-18 19:18:59 +00001361 // ::= ml # * (binary or unknown)
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001362 case OO_Star:
John McCall5e1e89b2010-08-18 19:18:59 +00001363 // Use binary when unknown.
Anders Carlsson8257d412009-12-22 06:36:32 +00001364 Out << (Arity == 1? "de" : "ml"); break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001365 // ::= co # ~
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001366 case OO_Tilde: Out << "co"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001367 // ::= dv # /
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001368 case OO_Slash: Out << "dv"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001369 // ::= rm # %
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001370 case OO_Percent: Out << "rm"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001371 // ::= or # |
1372 case OO_Pipe: Out << "or"; break;
1373 // ::= eo # ^
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001374 case OO_Caret: Out << "eo"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001375 // ::= aS # =
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001376 case OO_Equal: Out << "aS"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001377 // ::= pL # +=
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001378 case OO_PlusEqual: Out << "pL"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001379 // ::= mI # -=
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001380 case OO_MinusEqual: Out << "mI"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001381 // ::= mL # *=
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001382 case OO_StarEqual: Out << "mL"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001383 // ::= dV # /=
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001384 case OO_SlashEqual: Out << "dV"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001385 // ::= rM # %=
1386 case OO_PercentEqual: Out << "rM"; break;
1387 // ::= aN # &=
1388 case OO_AmpEqual: Out << "aN"; break;
1389 // ::= oR # |=
1390 case OO_PipeEqual: Out << "oR"; break;
1391 // ::= eO # ^=
1392 case OO_CaretEqual: Out << "eO"; break;
1393 // ::= ls # <<
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001394 case OO_LessLess: Out << "ls"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001395 // ::= rs # >>
1396 case OO_GreaterGreater: Out << "rs"; break;
1397 // ::= lS # <<=
1398 case OO_LessLessEqual: Out << "lS"; break;
1399 // ::= rS # >>=
1400 case OO_GreaterGreaterEqual: Out << "rS"; break;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001401 // ::= eq # ==
1402 case OO_EqualEqual: Out << "eq"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001403 // ::= ne # !=
1404 case OO_ExclaimEqual: Out << "ne"; break;
1405 // ::= lt # <
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001406 case OO_Less: Out << "lt"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001407 // ::= gt # >
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001408 case OO_Greater: Out << "gt"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001409 // ::= le # <=
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001410 case OO_LessEqual: Out << "le"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001411 // ::= ge # >=
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001412 case OO_GreaterEqual: Out << "ge"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001413 // ::= nt # !
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001414 case OO_Exclaim: Out << "nt"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001415 // ::= aa # &&
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001416 case OO_AmpAmp: Out << "aa"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001417 // ::= oo # ||
1418 case OO_PipePipe: Out << "oo"; break;
1419 // ::= pp # ++
1420 case OO_PlusPlus: Out << "pp"; break;
1421 // ::= mm # --
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001422 case OO_MinusMinus: Out << "mm"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001423 // ::= cm # ,
1424 case OO_Comma: Out << "cm"; break;
1425 // ::= pm # ->*
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001426 case OO_ArrowStar: Out << "pm"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001427 // ::= pt # ->
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001428 case OO_Arrow: Out << "pt"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001429 // ::= cl # ()
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001430 case OO_Call: Out << "cl"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001431 // ::= ix # []
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001432 case OO_Subscript: Out << "ix"; break;
Anders Carlssone170ba72009-12-14 01:45:37 +00001433
1434 // ::= qu # ?
1435 // The conditional operator can't be overloaded, but we still handle it when
1436 // mangling expressions.
1437 case OO_Conditional: Out << "qu"; break;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001438
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001439 case OO_None:
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001440 case NUM_OVERLOADED_OPERATORS:
Mike Stump1eb44332009-09-09 15:08:12 +00001441 assert(false && "Not an overloaded operator");
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001442 break;
1443 }
1444}
1445
John McCall0953e762009-09-24 19:53:00 +00001446void CXXNameMangler::mangleQualifiers(Qualifiers Quals) {
Mike Stump1eb44332009-09-09 15:08:12 +00001447 // <CV-qualifiers> ::= [r] [V] [K] # restrict (C99), volatile, const
John McCall0953e762009-09-24 19:53:00 +00001448 if (Quals.hasRestrict())
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001449 Out << 'r';
John McCall0953e762009-09-24 19:53:00 +00001450 if (Quals.hasVolatile())
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001451 Out << 'V';
John McCall0953e762009-09-24 19:53:00 +00001452 if (Quals.hasConst())
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001453 Out << 'K';
John McCall0953e762009-09-24 19:53:00 +00001454
Douglas Gregor56079f72010-06-14 23:15:08 +00001455 if (Quals.hasAddressSpace()) {
1456 // Extension:
1457 //
1458 // <type> ::= U <address-space-number>
1459 //
1460 // where <address-space-number> is a source name consisting of 'AS'
1461 // followed by the address space <number>.
1462 llvm::SmallString<64> ASString;
1463 ASString = "AS" + llvm::utostr_32(Quals.getAddressSpace());
1464 Out << 'U' << ASString.size() << ASString;
1465 }
1466
John McCall0953e762009-09-24 19:53:00 +00001467 // FIXME: For now, just drop all extension qualifiers on the floor.
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001468}
1469
Douglas Gregor0a9a6d62011-01-26 17:36:28 +00001470void CXXNameMangler::mangleRefQualifier(RefQualifierKind RefQualifier) {
1471 // <ref-qualifier> ::= R # lvalue reference
1472 // ::= O # rvalue-reference
1473 // Proposal to Itanium C++ ABI list on 1/26/11
1474 switch (RefQualifier) {
1475 case RQ_None:
1476 break;
1477
1478 case RQ_LValue:
1479 Out << 'R';
1480 break;
1481
1482 case RQ_RValue:
1483 Out << 'O';
1484 break;
1485 }
1486}
1487
Anders Carlsson7b06f6c2009-12-10 03:14:39 +00001488void CXXNameMangler::mangleObjCMethodName(const ObjCMethodDecl *MD) {
Rafael Espindolaf0be9792011-02-11 02:52:17 +00001489 Context.mangleObjCMethodName(MD, Out);
Anders Carlsson7b06f6c2009-12-10 03:14:39 +00001490}
1491
John McCallb47f7482011-01-26 20:05:40 +00001492void CXXNameMangler::mangleType(QualType nonCanon) {
Anders Carlsson4843e582009-03-10 17:07:44 +00001493 // Only operate on the canonical type!
John McCallb47f7482011-01-26 20:05:40 +00001494 QualType canon = nonCanon.getCanonicalType();
Anders Carlsson4843e582009-03-10 17:07:44 +00001495
John McCallb47f7482011-01-26 20:05:40 +00001496 SplitQualType split = canon.split();
1497 Qualifiers quals = split.second;
1498 const Type *ty = split.first;
1499
1500 bool isSubstitutable = quals || !isa<BuiltinType>(ty);
1501 if (isSubstitutable && mangleSubstitution(canon))
Anders Carlsson76967372009-09-17 00:43:46 +00001502 return;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001503
John McCallb47f7482011-01-26 20:05:40 +00001504 // If we're mangling a qualified array type, push the qualifiers to
1505 // the element type.
1506 if (quals && isa<ArrayType>(ty)) {
1507 ty = Context.getASTContext().getAsArrayType(canon);
1508 quals = Qualifiers();
1509
1510 // Note that we don't update canon: we want to add the
1511 // substitution at the canonical type.
1512 }
1513
1514 if (quals) {
1515 mangleQualifiers(quals);
John McCall0953e762009-09-24 19:53:00 +00001516 // Recurse: even if the qualified type isn't yet substitutable,
1517 // the unqualified type might be.
John McCallb47f7482011-01-26 20:05:40 +00001518 mangleType(QualType(ty, 0));
Anders Carlsson76967372009-09-17 00:43:46 +00001519 } else {
John McCallb47f7482011-01-26 20:05:40 +00001520 switch (ty->getTypeClass()) {
John McCallefe6aee2009-09-05 07:56:18 +00001521#define ABSTRACT_TYPE(CLASS, PARENT)
1522#define NON_CANONICAL_TYPE(CLASS, PARENT) \
Anders Carlsson76967372009-09-17 00:43:46 +00001523 case Type::CLASS: \
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +00001524 llvm_unreachable("can't mangle non-canonical type " #CLASS "Type"); \
Anders Carlsson76967372009-09-17 00:43:46 +00001525 return;
John McCallefe6aee2009-09-05 07:56:18 +00001526#define TYPE(CLASS, PARENT) \
Anders Carlsson76967372009-09-17 00:43:46 +00001527 case Type::CLASS: \
John McCallb47f7482011-01-26 20:05:40 +00001528 mangleType(static_cast<const CLASS##Type*>(ty)); \
Anders Carlsson76967372009-09-17 00:43:46 +00001529 break;
John McCallefe6aee2009-09-05 07:56:18 +00001530#include "clang/AST/TypeNodes.def"
Anders Carlsson76967372009-09-17 00:43:46 +00001531 }
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001532 }
Anders Carlsson76967372009-09-17 00:43:46 +00001533
1534 // Add the substitution.
John McCallb47f7482011-01-26 20:05:40 +00001535 if (isSubstitutable)
1536 addSubstitution(canon);
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001537}
1538
Douglas Gregor1b12a3b2010-05-26 05:11:13 +00001539void CXXNameMangler::mangleNameOrStandardSubstitution(const NamedDecl *ND) {
1540 if (!mangleStandardSubstitution(ND))
1541 mangleName(ND);
1542}
1543
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001544void CXXNameMangler::mangleType(const BuiltinType *T) {
John McCallefe6aee2009-09-05 07:56:18 +00001545 // <type> ::= <builtin-type>
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001546 // <builtin-type> ::= v # void
1547 // ::= w # wchar_t
1548 // ::= b # bool
1549 // ::= c # char
1550 // ::= a # signed char
1551 // ::= h # unsigned char
1552 // ::= s # short
1553 // ::= t # unsigned short
1554 // ::= i # int
1555 // ::= j # unsigned int
1556 // ::= l # long
1557 // ::= m # unsigned long
1558 // ::= x # long long, __int64
1559 // ::= y # unsigned long long, __int64
1560 // ::= n # __int128
1561 // UNSUPPORTED: ::= o # unsigned __int128
1562 // ::= f # float
1563 // ::= d # double
1564 // ::= e # long double, __float80
1565 // UNSUPPORTED: ::= g # __float128
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001566 // UNSUPPORTED: ::= Dd # IEEE 754r decimal floating point (64 bits)
1567 // UNSUPPORTED: ::= De # IEEE 754r decimal floating point (128 bits)
1568 // UNSUPPORTED: ::= Df # IEEE 754r decimal floating point (32 bits)
1569 // UNSUPPORTED: ::= Dh # IEEE 754r half-precision floating point (16 bits)
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001570 // ::= Di # char32_t
1571 // ::= Ds # char16_t
Anders Carlssone2923682010-11-04 04:31:32 +00001572 // ::= Dn # std::nullptr_t (i.e., decltype(nullptr))
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001573 // ::= u <source-name> # vendor extended type
1574 switch (T->getKind()) {
1575 case BuiltinType::Void: Out << 'v'; break;
1576 case BuiltinType::Bool: Out << 'b'; break;
1577 case BuiltinType::Char_U: case BuiltinType::Char_S: Out << 'c'; break;
1578 case BuiltinType::UChar: Out << 'h'; break;
1579 case BuiltinType::UShort: Out << 't'; break;
1580 case BuiltinType::UInt: Out << 'j'; break;
1581 case BuiltinType::ULong: Out << 'm'; break;
1582 case BuiltinType::ULongLong: Out << 'y'; break;
Chris Lattner2df9ced2009-04-30 02:43:43 +00001583 case BuiltinType::UInt128: Out << 'o'; break;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001584 case BuiltinType::SChar: Out << 'a'; break;
Chris Lattner3f59c972010-12-25 23:25:43 +00001585 case BuiltinType::WChar_S:
1586 case BuiltinType::WChar_U: Out << 'w'; break;
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001587 case BuiltinType::Char16: Out << "Ds"; break;
1588 case BuiltinType::Char32: Out << "Di"; break;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001589 case BuiltinType::Short: Out << 's'; break;
1590 case BuiltinType::Int: Out << 'i'; break;
1591 case BuiltinType::Long: Out << 'l'; break;
1592 case BuiltinType::LongLong: Out << 'x'; break;
Chris Lattner2df9ced2009-04-30 02:43:43 +00001593 case BuiltinType::Int128: Out << 'n'; break;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001594 case BuiltinType::Float: Out << 'f'; break;
1595 case BuiltinType::Double: Out << 'd'; break;
1596 case BuiltinType::LongDouble: Out << 'e'; break;
Anders Carlssone2923682010-11-04 04:31:32 +00001597 case BuiltinType::NullPtr: Out << "Dn"; break;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001598
1599 case BuiltinType::Overload:
1600 case BuiltinType::Dependent:
John McCall864c0412011-04-26 20:42:42 +00001601 case BuiltinType::BoundMember:
John McCall1de4d4e2011-04-07 08:22:57 +00001602 case BuiltinType::UnknownAny:
John McCallfb44de92011-05-01 22:35:37 +00001603 llvm_unreachable("mangling a placeholder type");
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001604 break;
Steve Naroff9533a7f2009-07-22 17:14:51 +00001605 case BuiltinType::ObjCId: Out << "11objc_object"; break;
1606 case BuiltinType::ObjCClass: Out << "10objc_class"; break;
Fariborz Jahanian13dcd002009-11-21 19:53:08 +00001607 case BuiltinType::ObjCSel: Out << "13objc_selector"; break;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001608 }
1609}
1610
John McCallefe6aee2009-09-05 07:56:18 +00001611// <type> ::= <function-type>
1612// <function-type> ::= F [Y] <bare-function-type> E
1613void CXXNameMangler::mangleType(const FunctionProtoType *T) {
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001614 Out << 'F';
Mike Stumpf5408fe2009-05-16 07:57:57 +00001615 // FIXME: We don't have enough information in the AST to produce the 'Y'
1616 // encoding for extern "C" function types.
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001617 mangleBareFunctionType(T, /*MangleReturnType=*/true);
1618 Out << 'E';
1619}
John McCallefe6aee2009-09-05 07:56:18 +00001620void CXXNameMangler::mangleType(const FunctionNoProtoType *T) {
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +00001621 llvm_unreachable("Can't mangle K&R function prototypes");
John McCallefe6aee2009-09-05 07:56:18 +00001622}
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001623void CXXNameMangler::mangleBareFunctionType(const FunctionType *T,
1624 bool MangleReturnType) {
John McCallefe6aee2009-09-05 07:56:18 +00001625 // We should never be mangling something without a prototype.
1626 const FunctionProtoType *Proto = cast<FunctionProtoType>(T);
1627
John McCallfb44de92011-05-01 22:35:37 +00001628 // Record that we're in a function type. See mangleFunctionParam
1629 // for details on what we're trying to achieve here.
1630 FunctionTypeDepthState saved = FunctionTypeDepth.push();
1631
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001632 // <bare-function-type> ::= <signature type>+
John McCallfb44de92011-05-01 22:35:37 +00001633 if (MangleReturnType) {
1634 FunctionTypeDepth.enterResultType();
John McCallefe6aee2009-09-05 07:56:18 +00001635 mangleType(Proto->getResultType());
John McCallfb44de92011-05-01 22:35:37 +00001636 FunctionTypeDepth.leaveResultType();
1637 }
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001638
Anders Carlsson93296682010-06-02 04:40:13 +00001639 if (Proto->getNumArgs() == 0 && !Proto->isVariadic()) {
Eli Friedmana7e68452010-08-22 01:00:03 +00001640 // <builtin-type> ::= v # void
Anders Carlssonc6c91bc2009-04-01 00:15:23 +00001641 Out << 'v';
John McCallfb44de92011-05-01 22:35:37 +00001642
1643 FunctionTypeDepth.pop(saved);
Anders Carlssonc6c91bc2009-04-01 00:15:23 +00001644 return;
1645 }
Mike Stump1eb44332009-09-09 15:08:12 +00001646
Douglas Gregor72564e72009-02-26 23:50:07 +00001647 for (FunctionProtoType::arg_type_iterator Arg = Proto->arg_type_begin(),
Mike Stump1eb44332009-09-09 15:08:12 +00001648 ArgEnd = Proto->arg_type_end();
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001649 Arg != ArgEnd; ++Arg)
1650 mangleType(*Arg);
Douglas Gregor219cc612009-02-13 01:28:03 +00001651
John McCallfb44de92011-05-01 22:35:37 +00001652 FunctionTypeDepth.pop(saved);
1653
Douglas Gregor219cc612009-02-13 01:28:03 +00001654 // <builtin-type> ::= z # ellipsis
1655 if (Proto->isVariadic())
1656 Out << 'z';
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001657}
1658
John McCallefe6aee2009-09-05 07:56:18 +00001659// <type> ::= <class-enum-type>
Mike Stump1eb44332009-09-09 15:08:12 +00001660// <class-enum-type> ::= <name>
John McCalled976492009-12-04 22:46:56 +00001661void CXXNameMangler::mangleType(const UnresolvedUsingType *T) {
1662 mangleName(T->getDecl());
1663}
1664
1665// <type> ::= <class-enum-type>
1666// <class-enum-type> ::= <name>
John McCallefe6aee2009-09-05 07:56:18 +00001667void CXXNameMangler::mangleType(const EnumType *T) {
1668 mangleType(static_cast<const TagType*>(T));
1669}
1670void CXXNameMangler::mangleType(const RecordType *T) {
1671 mangleType(static_cast<const TagType*>(T));
1672}
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001673void CXXNameMangler::mangleType(const TagType *T) {
Eli Friedmanecb7e932009-12-11 18:00:57 +00001674 mangleName(T->getDecl());
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001675}
1676
John McCallefe6aee2009-09-05 07:56:18 +00001677// <type> ::= <array-type>
1678// <array-type> ::= A <positive dimension number> _ <element type>
1679// ::= A [<dimension expression>] _ <element type>
1680void CXXNameMangler::mangleType(const ConstantArrayType *T) {
1681 Out << 'A' << T->getSize() << '_';
1682 mangleType(T->getElementType());
1683}
1684void CXXNameMangler::mangleType(const VariableArrayType *T) {
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001685 Out << 'A';
Fariborz Jahanian7281d1f2010-11-02 16:54:00 +00001686 // decayed vla types (size 0) will just be skipped.
1687 if (T->getSizeExpr())
1688 mangleExpression(T->getSizeExpr());
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001689 Out << '_';
1690 mangleType(T->getElementType());
1691}
John McCallefe6aee2009-09-05 07:56:18 +00001692void CXXNameMangler::mangleType(const DependentSizedArrayType *T) {
1693 Out << 'A';
1694 mangleExpression(T->getSizeExpr());
1695 Out << '_';
1696 mangleType(T->getElementType());
1697}
1698void CXXNameMangler::mangleType(const IncompleteArrayType *T) {
Nick Lewycky271b6652010-09-05 03:40:33 +00001699 Out << "A_";
John McCallefe6aee2009-09-05 07:56:18 +00001700 mangleType(T->getElementType());
1701}
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001702
John McCallefe6aee2009-09-05 07:56:18 +00001703// <type> ::= <pointer-to-member-type>
1704// <pointer-to-member-type> ::= M <class type> <member type>
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001705void CXXNameMangler::mangleType(const MemberPointerType *T) {
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001706 Out << 'M';
1707 mangleType(QualType(T->getClass(), 0));
Anders Carlsson0e650012009-05-17 17:41:20 +00001708 QualType PointeeType = T->getPointeeType();
1709 if (const FunctionProtoType *FPT = dyn_cast<FunctionProtoType>(PointeeType)) {
John McCall0953e762009-09-24 19:53:00 +00001710 mangleQualifiers(Qualifiers::fromCVRMask(FPT->getTypeQuals()));
Douglas Gregor0a9a6d62011-01-26 17:36:28 +00001711 mangleRefQualifier(FPT->getRefQualifier());
Anders Carlsson0e650012009-05-17 17:41:20 +00001712 mangleType(FPT);
Anders Carlsson9d85b722010-06-02 04:29:50 +00001713
1714 // Itanium C++ ABI 5.1.8:
1715 //
1716 // The type of a non-static member function is considered to be different,
1717 // for the purposes of substitution, from the type of a namespace-scope or
1718 // static member function whose type appears similar. The types of two
1719 // non-static member functions are considered to be different, for the
1720 // purposes of substitution, if the functions are members of different
1721 // classes. In other words, for the purposes of substitution, the class of
1722 // which the function is a member is considered part of the type of
1723 // function.
1724
1725 // We increment the SeqID here to emulate adding an entry to the
1726 // substitution table. We can't actually add it because we don't want this
1727 // particular function type to be substituted.
1728 ++SeqID;
Mike Stump1eb44332009-09-09 15:08:12 +00001729 } else
Anders Carlsson0e650012009-05-17 17:41:20 +00001730 mangleType(PointeeType);
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001731}
1732
John McCallefe6aee2009-09-05 07:56:18 +00001733// <type> ::= <template-param>
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001734void CXXNameMangler::mangleType(const TemplateTypeParmType *T) {
Anders Carlsson0ccdf8d2009-09-27 00:38:53 +00001735 mangleTemplateParameter(T->getIndex());
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001736}
1737
Douglas Gregorc3069d62011-01-14 02:55:32 +00001738// <type> ::= <template-param>
1739void CXXNameMangler::mangleType(const SubstTemplateTypeParmPackType *T) {
1740 mangleTemplateParameter(T->getReplacedParameter()->getIndex());
1741}
1742
John McCallefe6aee2009-09-05 07:56:18 +00001743// <type> ::= P <type> # pointer-to
1744void CXXNameMangler::mangleType(const PointerType *T) {
1745 Out << 'P';
1746 mangleType(T->getPointeeType());
1747}
1748void CXXNameMangler::mangleType(const ObjCObjectPointerType *T) {
1749 Out << 'P';
1750 mangleType(T->getPointeeType());
1751}
1752
1753// <type> ::= R <type> # reference-to
1754void CXXNameMangler::mangleType(const LValueReferenceType *T) {
1755 Out << 'R';
1756 mangleType(T->getPointeeType());
1757}
1758
1759// <type> ::= O <type> # rvalue reference-to (C++0x)
1760void CXXNameMangler::mangleType(const RValueReferenceType *T) {
1761 Out << 'O';
1762 mangleType(T->getPointeeType());
1763}
1764
1765// <type> ::= C <type> # complex pair (C 2000)
1766void CXXNameMangler::mangleType(const ComplexType *T) {
1767 Out << 'C';
1768 mangleType(T->getElementType());
1769}
1770
Bob Wilsonc7df92d2010-11-12 17:24:43 +00001771// ARM's ABI for Neon vector types specifies that they should be mangled as
Bob Wilson57147a82010-11-16 00:32:18 +00001772// if they are structs (to match ARM's initial implementation). The
1773// vector type must be one of the special types predefined by ARM.
1774void CXXNameMangler::mangleNeonVectorType(const VectorType *T) {
Bob Wilsonc7df92d2010-11-12 17:24:43 +00001775 QualType EltType = T->getElementType();
Bob Wilson57147a82010-11-16 00:32:18 +00001776 assert(EltType->isBuiltinType() && "Neon vector element not a BuiltinType");
Bob Wilsonc7df92d2010-11-12 17:24:43 +00001777 const char *EltName = 0;
Bob Wilson491328c2010-11-12 17:24:46 +00001778 if (T->getVectorKind() == VectorType::NeonPolyVector) {
1779 switch (cast<BuiltinType>(EltType)->getKind()) {
Bob Wilson4cfaa5d2010-11-12 17:24:49 +00001780 case BuiltinType::SChar: EltName = "poly8_t"; break;
1781 case BuiltinType::Short: EltName = "poly16_t"; break;
Bob Wilson57147a82010-11-16 00:32:18 +00001782 default: llvm_unreachable("unexpected Neon polynomial vector element type");
Bob Wilson491328c2010-11-12 17:24:46 +00001783 }
1784 } else {
1785 switch (cast<BuiltinType>(EltType)->getKind()) {
Bob Wilson4cfaa5d2010-11-12 17:24:49 +00001786 case BuiltinType::SChar: EltName = "int8_t"; break;
1787 case BuiltinType::UChar: EltName = "uint8_t"; break;
1788 case BuiltinType::Short: EltName = "int16_t"; break;
1789 case BuiltinType::UShort: EltName = "uint16_t"; break;
1790 case BuiltinType::Int: EltName = "int32_t"; break;
1791 case BuiltinType::UInt: EltName = "uint32_t"; break;
1792 case BuiltinType::LongLong: EltName = "int64_t"; break;
1793 case BuiltinType::ULongLong: EltName = "uint64_t"; break;
1794 case BuiltinType::Float: EltName = "float32_t"; break;
Bob Wilson57147a82010-11-16 00:32:18 +00001795 default: llvm_unreachable("unexpected Neon vector element type");
Bob Wilson491328c2010-11-12 17:24:46 +00001796 }
Bob Wilsonc7df92d2010-11-12 17:24:43 +00001797 }
1798 const char *BaseName = 0;
Bob Wilson4cfaa5d2010-11-12 17:24:49 +00001799 unsigned BitSize = (T->getNumElements() *
Bob Wilson3a723022010-11-16 00:32:12 +00001800 getASTContext().getTypeSize(EltType));
Bob Wilsonc7df92d2010-11-12 17:24:43 +00001801 if (BitSize == 64)
1802 BaseName = "__simd64_";
Bob Wilson57147a82010-11-16 00:32:18 +00001803 else {
1804 assert(BitSize == 128 && "Neon vector type not 64 or 128 bits");
Bob Wilsonc7df92d2010-11-12 17:24:43 +00001805 BaseName = "__simd128_";
Bob Wilson57147a82010-11-16 00:32:18 +00001806 }
Bob Wilsonc7df92d2010-11-12 17:24:43 +00001807 Out << strlen(BaseName) + strlen(EltName);
1808 Out << BaseName << EltName;
Bob Wilsonc7df92d2010-11-12 17:24:43 +00001809}
1810
John McCallefe6aee2009-09-05 07:56:18 +00001811// GNU extension: vector types
Chris Lattner788b0fd2010-06-23 06:00:24 +00001812// <type> ::= <vector-type>
1813// <vector-type> ::= Dv <positive dimension number> _
1814// <extended element type>
1815// ::= Dv [<dimension expression>] _ <element type>
1816// <extended element type> ::= <element type>
1817// ::= p # AltiVec vector pixel
John McCallefe6aee2009-09-05 07:56:18 +00001818void CXXNameMangler::mangleType(const VectorType *T) {
Bob Wilson491328c2010-11-12 17:24:46 +00001819 if ((T->getVectorKind() == VectorType::NeonVector ||
Bob Wilson57147a82010-11-16 00:32:18 +00001820 T->getVectorKind() == VectorType::NeonPolyVector)) {
1821 mangleNeonVectorType(T);
Bob Wilsonc7df92d2010-11-12 17:24:43 +00001822 return;
Bob Wilson57147a82010-11-16 00:32:18 +00001823 }
Nick Lewycky0e5f0672010-03-26 07:18:04 +00001824 Out << "Dv" << T->getNumElements() << '_';
Bob Wilsone86d78c2010-11-10 21:56:12 +00001825 if (T->getVectorKind() == VectorType::AltiVecPixel)
Chris Lattner788b0fd2010-06-23 06:00:24 +00001826 Out << 'p';
Bob Wilsone86d78c2010-11-10 21:56:12 +00001827 else if (T->getVectorKind() == VectorType::AltiVecBool)
Chris Lattner788b0fd2010-06-23 06:00:24 +00001828 Out << 'b';
1829 else
1830 mangleType(T->getElementType());
John McCallefe6aee2009-09-05 07:56:18 +00001831}
1832void CXXNameMangler::mangleType(const ExtVectorType *T) {
1833 mangleType(static_cast<const VectorType*>(T));
1834}
1835void CXXNameMangler::mangleType(const DependentSizedExtVectorType *T) {
Nick Lewycky0e5f0672010-03-26 07:18:04 +00001836 Out << "Dv";
1837 mangleExpression(T->getSizeExpr());
1838 Out << '_';
John McCallefe6aee2009-09-05 07:56:18 +00001839 mangleType(T->getElementType());
1840}
1841
Douglas Gregor7536dd52010-12-20 02:24:11 +00001842void CXXNameMangler::mangleType(const PackExpansionType *T) {
Douglas Gregor4fc48662011-01-13 16:39:34 +00001843 // <type> ::= Dp <type> # pack expansion (C++0x)
Douglas Gregor255c2692011-01-13 17:44:36 +00001844 Out << "Dp";
Douglas Gregor7536dd52010-12-20 02:24:11 +00001845 mangleType(T->getPattern());
1846}
1847
Anders Carlssona40c5e42009-03-07 22:03:21 +00001848void CXXNameMangler::mangleType(const ObjCInterfaceType *T) {
1849 mangleSourceName(T->getDecl()->getIdentifier());
1850}
1851
John McCallc12c5bb2010-05-15 11:32:37 +00001852void CXXNameMangler::mangleType(const ObjCObjectType *T) {
John McCallc00c1f62010-05-15 17:06:29 +00001853 // We don't allow overloading by different protocol qualification,
1854 // so mangling them isn't necessary.
John McCallc12c5bb2010-05-15 11:32:37 +00001855 mangleType(T->getBaseType());
1856}
1857
John McCallefe6aee2009-09-05 07:56:18 +00001858void CXXNameMangler::mangleType(const BlockPointerType *T) {
Anders Carlssonf28c6872009-12-23 22:31:44 +00001859 Out << "U13block_pointer";
1860 mangleType(T->getPointeeType());
John McCallefe6aee2009-09-05 07:56:18 +00001861}
1862
John McCall31f17ec2010-04-27 00:57:59 +00001863void CXXNameMangler::mangleType(const InjectedClassNameType *T) {
1864 // Mangle injected class name types as if the user had written the
1865 // specialization out fully. It may not actually be possible to see
1866 // this mangling, though.
1867 mangleType(T->getInjectedSpecializationType());
1868}
1869
John McCallefe6aee2009-09-05 07:56:18 +00001870void CXXNameMangler::mangleType(const TemplateSpecializationType *T) {
Douglas Gregor1e9268e2010-04-28 05:58:56 +00001871 if (TemplateDecl *TD = T->getTemplateName().getAsTemplateDecl()) {
1872 mangleName(TD, T->getArgs(), T->getNumArgs());
1873 } else {
1874 if (mangleSubstitution(QualType(T, 0)))
1875 return;
Sean Huntc3021132010-05-05 15:23:54 +00001876
Douglas Gregor1e9268e2010-04-28 05:58:56 +00001877 mangleTemplatePrefix(T->getTemplateName());
Sean Huntc3021132010-05-05 15:23:54 +00001878
Douglas Gregor1e9268e2010-04-28 05:58:56 +00001879 // FIXME: GCC does not appear to mangle the template arguments when
1880 // the template in question is a dependent template name. Should we
1881 // emulate that badness?
1882 mangleTemplateArgs(T->getTemplateName(), T->getArgs(), T->getNumArgs());
1883 addSubstitution(QualType(T, 0));
1884 }
John McCallefe6aee2009-09-05 07:56:18 +00001885}
1886
Douglas Gregor4714c122010-03-31 17:34:00 +00001887void CXXNameMangler::mangleType(const DependentNameType *T) {
Anders Carlssonae352482009-09-26 02:26:02 +00001888 // Typename types are always nested
1889 Out << 'N';
John McCalla0ce15c2011-04-24 08:23:24 +00001890 manglePrefix(T->getQualifier());
John McCall33500952010-06-11 00:33:02 +00001891 mangleSourceName(T->getIdentifier());
1892 Out << 'E';
1893}
John McCall6ab30e02010-06-09 07:26:17 +00001894
John McCall33500952010-06-11 00:33:02 +00001895void CXXNameMangler::mangleType(const DependentTemplateSpecializationType *T) {
Douglas Gregoraa2187d2011-02-28 00:04:36 +00001896 // Dependently-scoped template types are nested if they have a prefix.
John McCall33500952010-06-11 00:33:02 +00001897 Out << 'N';
1898
1899 // TODO: avoid making this TemplateName.
1900 TemplateName Prefix =
1901 getASTContext().getDependentTemplateName(T->getQualifier(),
1902 T->getIdentifier());
1903 mangleTemplatePrefix(Prefix);
1904
1905 // FIXME: GCC does not appear to mangle the template arguments when
1906 // the template in question is a dependent template name. Should we
1907 // emulate that badness?
1908 mangleTemplateArgs(Prefix, T->getArgs(), T->getNumArgs());
Anders Carlssonae352482009-09-26 02:26:02 +00001909 Out << 'E';
John McCallefe6aee2009-09-05 07:56:18 +00001910}
1911
John McCallad5e7382010-03-01 23:49:17 +00001912void CXXNameMangler::mangleType(const TypeOfType *T) {
1913 // FIXME: this is pretty unsatisfactory, but there isn't an obvious
1914 // "extension with parameters" mangling.
1915 Out << "u6typeof";
1916}
1917
1918void CXXNameMangler::mangleType(const TypeOfExprType *T) {
1919 // FIXME: this is pretty unsatisfactory, but there isn't an obvious
1920 // "extension with parameters" mangling.
1921 Out << "u6typeof";
1922}
1923
1924void CXXNameMangler::mangleType(const DecltypeType *T) {
1925 Expr *E = T->getUnderlyingExpr();
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001926
John McCallad5e7382010-03-01 23:49:17 +00001927 // type ::= Dt <expression> E # decltype of an id-expression
1928 // # or class member access
1929 // ::= DT <expression> E # decltype of an expression
1930
1931 // This purports to be an exhaustive list of id-expressions and
1932 // class member accesses. Note that we do not ignore parentheses;
1933 // parentheses change the semantics of decltype for these
1934 // expressions (and cause the mangler to use the other form).
1935 if (isa<DeclRefExpr>(E) ||
1936 isa<MemberExpr>(E) ||
1937 isa<UnresolvedLookupExpr>(E) ||
1938 isa<DependentScopeDeclRefExpr>(E) ||
1939 isa<CXXDependentScopeMemberExpr>(E) ||
1940 isa<UnresolvedMemberExpr>(E))
1941 Out << "Dt";
1942 else
1943 Out << "DT";
1944 mangleExpression(E);
1945 Out << 'E';
1946}
1947
Sean Huntca63c202011-05-24 22:41:36 +00001948void CXXNameMangler::mangleType(const UnaryTransformType *T) {
1949 // If this is dependent, we need to record that. If not, we simply
1950 // mangle it as the underlying type since they are equivalent.
1951 if (T->isDependentType()) {
1952 Out << 'U';
1953
1954 switch (T->getUTTKind()) {
1955 case UnaryTransformType::EnumUnderlyingType:
1956 Out << "3eut";
1957 break;
1958 }
1959 }
1960
1961 mangleType(T->getUnderlyingType());
1962}
1963
Richard Smith34b41d92011-02-20 03:19:35 +00001964void CXXNameMangler::mangleType(const AutoType *T) {
1965 QualType D = T->getDeducedType();
Richard Smith967ecd32011-02-21 20:10:02 +00001966 // <builtin-type> ::= Da # dependent auto
1967 if (D.isNull())
1968 Out << "Da";
1969 else
1970 mangleType(D);
Richard Smith34b41d92011-02-20 03:19:35 +00001971}
1972
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001973void CXXNameMangler::mangleIntegerLiteral(QualType T,
Anders Carlssone170ba72009-12-14 01:45:37 +00001974 const llvm::APSInt &Value) {
1975 // <expr-primary> ::= L <type> <value number> E # integer literal
1976 Out << 'L';
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001977
Anders Carlssone170ba72009-12-14 01:45:37 +00001978 mangleType(T);
1979 if (T->isBooleanType()) {
1980 // Boolean values are encoded as 0/1.
1981 Out << (Value.getBoolValue() ? '1' : '0');
1982 } else {
John McCall0512e482010-07-14 04:20:34 +00001983 mangleNumber(Value);
Anders Carlssone170ba72009-12-14 01:45:37 +00001984 }
1985 Out << 'E';
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001986
Anders Carlssone170ba72009-12-14 01:45:37 +00001987}
1988
John McCall2f27bf82010-02-04 02:56:29 +00001989/// Mangles a member expression. Implicit accesses are not handled,
1990/// but that should be okay, because you shouldn't be able to
1991/// make an implicit access in a function template declaration.
John McCalla0ce15c2011-04-24 08:23:24 +00001992void CXXNameMangler::mangleMemberExpr(const Expr *base,
1993 bool isArrow,
1994 NestedNameSpecifier *qualifier,
1995 NamedDecl *firstQualifierLookup,
1996 DeclarationName member,
1997 unsigned arity) {
1998 // <expression> ::= dt <expression> <unresolved-name>
1999 // ::= pt <expression> <unresolved-name>
2000 Out << (isArrow ? "pt" : "dt");
2001 mangleExpression(base);
2002 mangleUnresolvedName(qualifier, firstQualifierLookup, member, arity);
John McCall2f27bf82010-02-04 02:56:29 +00002003}
2004
John McCall5a7e6f72011-04-28 02:52:03 +00002005/// Look at the callee of the given call expression and determine if
2006/// it's a parenthesized id-expression which would have triggered ADL
2007/// otherwise.
2008static bool isParenthesizedADLCallee(const CallExpr *call) {
2009 const Expr *callee = call->getCallee();
2010 const Expr *fn = callee->IgnoreParens();
2011
2012 // Must be parenthesized. IgnoreParens() skips __extension__ nodes,
2013 // too, but for those to appear in the callee, it would have to be
2014 // parenthesized.
2015 if (callee == fn) return false;
2016
2017 // Must be an unresolved lookup.
2018 const UnresolvedLookupExpr *lookup = dyn_cast<UnresolvedLookupExpr>(fn);
2019 if (!lookup) return false;
2020
2021 assert(!lookup->requiresADL());
2022
2023 // Must be an unqualified lookup.
2024 if (lookup->getQualifier()) return false;
2025
2026 // Must not have found a class member. Note that if one is a class
2027 // member, they're all class members.
2028 if (lookup->getNumDecls() > 0 &&
2029 (*lookup->decls_begin())->isCXXClassMember())
2030 return false;
2031
2032 // Otherwise, ADL would have been triggered.
2033 return true;
2034}
2035
John McCall5e1e89b2010-08-18 19:18:59 +00002036void CXXNameMangler::mangleExpression(const Expr *E, unsigned Arity) {
Anders Carlssond553f8c2009-09-21 01:21:10 +00002037 // <expression> ::= <unary operator-name> <expression>
John McCall09cc1412010-02-03 00:55:45 +00002038 // ::= <binary operator-name> <expression> <expression>
2039 // ::= <trinary operator-name> <expression> <expression> <expression>
Anders Carlssond553f8c2009-09-21 01:21:10 +00002040 // ::= cv <type> expression # conversion with one argument
2041 // ::= cv <type> _ <expression>* E # conversion with a different number of arguments
Eli Friedmana7e68452010-08-22 01:00:03 +00002042 // ::= st <type> # sizeof (a type)
Anders Carlssond553f8c2009-09-21 01:21:10 +00002043 // ::= at <type> # alignof (a type)
2044 // ::= <template-param>
2045 // ::= <function-param>
2046 // ::= sr <type> <unqualified-name> # dependent name
2047 // ::= sr <type> <unqualified-name> <template-args> # dependent template-id
Douglas Gregor63f62df2011-06-05 05:27:58 +00002048 // ::= ds <expression> <expression> # expr.*expr
Anders Carlssond553f8c2009-09-21 01:21:10 +00002049 // ::= sZ <template-param> # size of a parameter pack
Douglas Gregor4fc48662011-01-13 16:39:34 +00002050 // ::= sZ <function-param> # size of a function parameter pack
John McCall09cc1412010-02-03 00:55:45 +00002051 // ::= <expr-primary>
John McCall1dd73832010-02-04 01:42:13 +00002052 // <expr-primary> ::= L <type> <value number> E # integer literal
2053 // ::= L <type <value float> E # floating literal
2054 // ::= L <mangled-name> E # external name
Anders Carlssond553f8c2009-09-21 01:21:10 +00002055 switch (E->getStmtClass()) {
John McCall6ae1f352010-04-09 22:26:14 +00002056 case Expr::NoStmtClass:
John McCall63c00d72011-02-09 08:16:59 +00002057#define ABSTRACT_STMT(Type)
John McCall6ae1f352010-04-09 22:26:14 +00002058#define EXPR(Type, Base)
2059#define STMT(Type, Base) \
2060 case Expr::Type##Class:
Sean Hunt4bfe1962010-05-05 15:24:00 +00002061#include "clang/AST/StmtNodes.inc"
John McCall0512e482010-07-14 04:20:34 +00002062 // fallthrough
2063
2064 // These all can only appear in local or variable-initialization
2065 // contexts and so should never appear in a mangling.
2066 case Expr::AddrLabelExprClass:
2067 case Expr::BlockDeclRefExprClass:
2068 case Expr::CXXThisExprClass:
2069 case Expr::DesignatedInitExprClass:
2070 case Expr::ImplicitValueInitExprClass:
2071 case Expr::InitListExprClass:
2072 case Expr::ParenListExprClass:
2073 case Expr::CXXScalarValueInitExprClass:
John McCall09cc1412010-02-03 00:55:45 +00002074 llvm_unreachable("unexpected statement kind");
2075 break;
2076
John McCall0512e482010-07-14 04:20:34 +00002077 // FIXME: invent manglings for all these.
2078 case Expr::BlockExprClass:
2079 case Expr::CXXPseudoDestructorExprClass:
2080 case Expr::ChooseExprClass:
2081 case Expr::CompoundLiteralExprClass:
2082 case Expr::ExtVectorElementExprClass:
Peter Collingbournef111d932011-04-15 00:35:48 +00002083 case Expr::GenericSelectionExprClass:
John McCall0512e482010-07-14 04:20:34 +00002084 case Expr::ObjCEncodeExprClass:
John McCall0512e482010-07-14 04:20:34 +00002085 case Expr::ObjCIsaExprClass:
2086 case Expr::ObjCIvarRefExprClass:
2087 case Expr::ObjCMessageExprClass:
2088 case Expr::ObjCPropertyRefExprClass:
2089 case Expr::ObjCProtocolExprClass:
2090 case Expr::ObjCSelectorExprClass:
2091 case Expr::ObjCStringLiteralClass:
John McCall0512e482010-07-14 04:20:34 +00002092 case Expr::OffsetOfExprClass:
2093 case Expr::PredefinedExprClass:
2094 case Expr::ShuffleVectorExprClass:
2095 case Expr::StmtExprClass:
John McCall0512e482010-07-14 04:20:34 +00002096 case Expr::UnaryTypeTraitExprClass:
Francois Pichet6ad6f282010-12-07 00:08:36 +00002097 case Expr::BinaryTypeTraitExprClass:
John Wiegley21ff2e52011-04-28 00:16:57 +00002098 case Expr::ArrayTypeTraitExprClass:
John Wiegley55262202011-04-25 06:54:41 +00002099 case Expr::ExpressionTraitExprClass:
Francois Pichet9be88402010-09-08 23:47:05 +00002100 case Expr::VAArgExprClass:
Sebastian Redl2e156222010-09-10 20:55:43 +00002101 case Expr::CXXUuidofExprClass:
Peter Collingbournee08ce652011-02-09 21:07:24 +00002102 case Expr::CXXNoexceptExprClass:
Tanya Lattner61eee0c2011-06-04 00:47:47 +00002103 case Expr::CUDAKernelCallExprClass:
2104 case Expr::AsTypeExprClass:
2105 {
John McCall6ae1f352010-04-09 22:26:14 +00002106 // As bad as this diagnostic is, it's better than crashing.
2107 Diagnostic &Diags = Context.getDiags();
2108 unsigned DiagID = Diags.getCustomDiagID(Diagnostic::Error,
2109 "cannot yet mangle expression type %0");
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +00002110 Diags.Report(E->getExprLoc(), DiagID)
John McCall739bf092010-04-10 09:39:25 +00002111 << E->getStmtClassName() << E->getSourceRange();
John McCall6ae1f352010-04-09 22:26:14 +00002112 break;
2113 }
2114
John McCall56ca35d2011-02-17 10:25:35 +00002115 // Even gcc-4.5 doesn't mangle this.
2116 case Expr::BinaryConditionalOperatorClass: {
2117 Diagnostic &Diags = Context.getDiags();
2118 unsigned DiagID =
2119 Diags.getCustomDiagID(Diagnostic::Error,
2120 "?: operator with omitted middle operand cannot be mangled");
2121 Diags.Report(E->getExprLoc(), DiagID)
2122 << E->getStmtClassName() << E->getSourceRange();
2123 break;
2124 }
2125
2126 // These are used for internal purposes and cannot be meaningfully mangled.
John McCall7cd7d1a2010-11-15 23:31:06 +00002127 case Expr::OpaqueValueExprClass:
2128 llvm_unreachable("cannot mangle opaque value; mangling wrong thing?");
2129
John McCall0512e482010-07-14 04:20:34 +00002130 case Expr::CXXDefaultArgExprClass:
John McCall5e1e89b2010-08-18 19:18:59 +00002131 mangleExpression(cast<CXXDefaultArgExpr>(E)->getExpr(), Arity);
John McCall0512e482010-07-14 04:20:34 +00002132 break;
2133
2134 case Expr::CXXMemberCallExprClass: // fallthrough
John McCall1dd73832010-02-04 01:42:13 +00002135 case Expr::CallExprClass: {
2136 const CallExpr *CE = cast<CallExpr>(E);
John McCall5a7e6f72011-04-28 02:52:03 +00002137
2138 // <expression> ::= cp <simple-id> <expression>* E
2139 // We use this mangling only when the call would use ADL except
2140 // for being parenthesized. Per discussion with David
2141 // Vandervoorde, 2011.04.25.
2142 if (isParenthesizedADLCallee(CE)) {
2143 Out << "cp";
2144 // The callee here is a parenthesized UnresolvedLookupExpr with
2145 // no qualifier and should always get mangled as a <simple-id>
2146 // anyway.
2147
2148 // <expression> ::= cl <expression>* E
2149 } else {
2150 Out << "cl";
2151 }
2152
John McCall5e1e89b2010-08-18 19:18:59 +00002153 mangleExpression(CE->getCallee(), CE->getNumArgs());
John McCall1dd73832010-02-04 01:42:13 +00002154 for (unsigned I = 0, N = CE->getNumArgs(); I != N; ++I)
2155 mangleExpression(CE->getArg(I));
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002156 Out << 'E';
John McCall09cc1412010-02-03 00:55:45 +00002157 break;
John McCall1dd73832010-02-04 01:42:13 +00002158 }
John McCall09cc1412010-02-03 00:55:45 +00002159
John McCall0512e482010-07-14 04:20:34 +00002160 case Expr::CXXNewExprClass: {
2161 // Proposal from David Vandervoorde, 2010.06.30
2162 const CXXNewExpr *New = cast<CXXNewExpr>(E);
2163 if (New->isGlobalNew()) Out << "gs";
2164 Out << (New->isArray() ? "na" : "nw");
2165 for (CXXNewExpr::const_arg_iterator I = New->placement_arg_begin(),
2166 E = New->placement_arg_end(); I != E; ++I)
2167 mangleExpression(*I);
2168 Out << '_';
2169 mangleType(New->getAllocatedType());
2170 if (New->hasInitializer()) {
2171 Out << "pi";
2172 for (CXXNewExpr::const_arg_iterator I = New->constructor_arg_begin(),
2173 E = New->constructor_arg_end(); I != E; ++I)
2174 mangleExpression(*I);
2175 }
2176 Out << 'E';
2177 break;
2178 }
2179
John McCall2f27bf82010-02-04 02:56:29 +00002180 case Expr::MemberExprClass: {
2181 const MemberExpr *ME = cast<MemberExpr>(E);
2182 mangleMemberExpr(ME->getBase(), ME->isArrow(),
John McCalla0ce15c2011-04-24 08:23:24 +00002183 ME->getQualifier(), 0, ME->getMemberDecl()->getDeclName(),
John McCall5e1e89b2010-08-18 19:18:59 +00002184 Arity);
John McCall2f27bf82010-02-04 02:56:29 +00002185 break;
2186 }
2187
2188 case Expr::UnresolvedMemberExprClass: {
2189 const UnresolvedMemberExpr *ME = cast<UnresolvedMemberExpr>(E);
2190 mangleMemberExpr(ME->getBase(), ME->isArrow(),
John McCalla0ce15c2011-04-24 08:23:24 +00002191 ME->getQualifier(), 0, ME->getMemberName(),
John McCall5e1e89b2010-08-18 19:18:59 +00002192 Arity);
John McCall6dbce192010-08-20 00:17:19 +00002193 if (ME->hasExplicitTemplateArgs())
2194 mangleTemplateArgs(ME->getExplicitTemplateArgs());
John McCall2f27bf82010-02-04 02:56:29 +00002195 break;
2196 }
2197
2198 case Expr::CXXDependentScopeMemberExprClass: {
2199 const CXXDependentScopeMemberExpr *ME
2200 = cast<CXXDependentScopeMemberExpr>(E);
2201 mangleMemberExpr(ME->getBase(), ME->isArrow(),
John McCalla0ce15c2011-04-24 08:23:24 +00002202 ME->getQualifier(), ME->getFirstQualifierFoundInScope(),
2203 ME->getMember(), Arity);
John McCall6dbce192010-08-20 00:17:19 +00002204 if (ME->hasExplicitTemplateArgs())
2205 mangleTemplateArgs(ME->getExplicitTemplateArgs());
John McCall2f27bf82010-02-04 02:56:29 +00002206 break;
2207 }
2208
John McCall1dd73832010-02-04 01:42:13 +00002209 case Expr::UnresolvedLookupExprClass: {
2210 const UnresolvedLookupExpr *ULE = cast<UnresolvedLookupExpr>(E);
John McCalla0ce15c2011-04-24 08:23:24 +00002211 mangleUnresolvedName(ULE->getQualifier(), 0, ULE->getName(), Arity);
John McCall6dbce192010-08-20 00:17:19 +00002212 if (ULE->hasExplicitTemplateArgs())
2213 mangleTemplateArgs(ULE->getExplicitTemplateArgs());
John McCall1dd73832010-02-04 01:42:13 +00002214 break;
2215 }
2216
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002217 case Expr::CXXUnresolvedConstructExprClass: {
John McCall1dd73832010-02-04 01:42:13 +00002218 const CXXUnresolvedConstructExpr *CE = cast<CXXUnresolvedConstructExpr>(E);
2219 unsigned N = CE->arg_size();
2220
2221 Out << "cv";
2222 mangleType(CE->getType());
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002223 if (N != 1) Out << '_';
John McCall1dd73832010-02-04 01:42:13 +00002224 for (unsigned I = 0; I != N; ++I) mangleExpression(CE->getArg(I));
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002225 if (N != 1) Out << 'E';
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002226 break;
John McCall1dd73832010-02-04 01:42:13 +00002227 }
John McCall09cc1412010-02-03 00:55:45 +00002228
John McCall1dd73832010-02-04 01:42:13 +00002229 case Expr::CXXTemporaryObjectExprClass:
2230 case Expr::CXXConstructExprClass: {
2231 const CXXConstructExpr *CE = cast<CXXConstructExpr>(E);
2232 unsigned N = CE->getNumArgs();
2233
2234 Out << "cv";
2235 mangleType(CE->getType());
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002236 if (N != 1) Out << '_';
John McCall1dd73832010-02-04 01:42:13 +00002237 for (unsigned I = 0; I != N; ++I) mangleExpression(CE->getArg(I));
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002238 if (N != 1) Out << 'E';
John McCall09cc1412010-02-03 00:55:45 +00002239 break;
John McCall1dd73832010-02-04 01:42:13 +00002240 }
2241
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +00002242 case Expr::UnaryExprOrTypeTraitExprClass: {
2243 const UnaryExprOrTypeTraitExpr *SAE = cast<UnaryExprOrTypeTraitExpr>(E);
2244 switch(SAE->getKind()) {
2245 case UETT_SizeOf:
2246 Out << 's';
2247 break;
2248 case UETT_AlignOf:
2249 Out << 'a';
2250 break;
2251 case UETT_VecStep:
2252 Diagnostic &Diags = Context.getDiags();
2253 unsigned DiagID = Diags.getCustomDiagID(Diagnostic::Error,
2254 "cannot yet mangle vec_step expression");
2255 Diags.Report(DiagID);
2256 return;
2257 }
John McCall1dd73832010-02-04 01:42:13 +00002258 if (SAE->isArgumentType()) {
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002259 Out << 't';
John McCall1dd73832010-02-04 01:42:13 +00002260 mangleType(SAE->getArgumentType());
2261 } else {
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002262 Out << 'z';
John McCall1dd73832010-02-04 01:42:13 +00002263 mangleExpression(SAE->getArgumentExpr());
2264 }
2265 break;
2266 }
Anders Carlssona7694082009-11-06 02:50:19 +00002267
John McCall0512e482010-07-14 04:20:34 +00002268 case Expr::CXXThrowExprClass: {
2269 const CXXThrowExpr *TE = cast<CXXThrowExpr>(E);
2270
2271 // Proposal from David Vandervoorde, 2010.06.30
2272 if (TE->getSubExpr()) {
2273 Out << "tw";
2274 mangleExpression(TE->getSubExpr());
2275 } else {
2276 Out << "tr";
2277 }
2278 break;
2279 }
2280
2281 case Expr::CXXTypeidExprClass: {
2282 const CXXTypeidExpr *TIE = cast<CXXTypeidExpr>(E);
2283
2284 // Proposal from David Vandervoorde, 2010.06.30
2285 if (TIE->isTypeOperand()) {
2286 Out << "ti";
2287 mangleType(TIE->getTypeOperand());
2288 } else {
2289 Out << "te";
2290 mangleExpression(TIE->getExprOperand());
2291 }
2292 break;
2293 }
2294
2295 case Expr::CXXDeleteExprClass: {
2296 const CXXDeleteExpr *DE = cast<CXXDeleteExpr>(E);
2297
2298 // Proposal from David Vandervoorde, 2010.06.30
2299 if (DE->isGlobalDelete()) Out << "gs";
2300 Out << (DE->isArrayForm() ? "da" : "dl");
2301 mangleExpression(DE->getArgument());
2302 break;
2303 }
2304
Anders Carlssone170ba72009-12-14 01:45:37 +00002305 case Expr::UnaryOperatorClass: {
2306 const UnaryOperator *UO = cast<UnaryOperator>(E);
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002307 mangleOperatorName(UnaryOperator::getOverloadedOperator(UO->getOpcode()),
Anders Carlssone170ba72009-12-14 01:45:37 +00002308 /*Arity=*/1);
2309 mangleExpression(UO->getSubExpr());
2310 break;
2311 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002312
John McCall0512e482010-07-14 04:20:34 +00002313 case Expr::ArraySubscriptExprClass: {
2314 const ArraySubscriptExpr *AE = cast<ArraySubscriptExpr>(E);
2315
Chris Lattnerfc8f0e12011-04-15 05:22:18 +00002316 // Array subscript is treated as a syntactically weird form of
John McCall0512e482010-07-14 04:20:34 +00002317 // binary operator.
2318 Out << "ix";
2319 mangleExpression(AE->getLHS());
2320 mangleExpression(AE->getRHS());
2321 break;
2322 }
2323
2324 case Expr::CompoundAssignOperatorClass: // fallthrough
Anders Carlssone170ba72009-12-14 01:45:37 +00002325 case Expr::BinaryOperatorClass: {
2326 const BinaryOperator *BO = cast<BinaryOperator>(E);
Douglas Gregor63f62df2011-06-05 05:27:58 +00002327 if (BO->getOpcode() == BO_PtrMemD)
2328 Out << "ds";
2329 else
2330 mangleOperatorName(BinaryOperator::getOverloadedOperator(BO->getOpcode()),
2331 /*Arity=*/2);
Anders Carlssone170ba72009-12-14 01:45:37 +00002332 mangleExpression(BO->getLHS());
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002333 mangleExpression(BO->getRHS());
Anders Carlssone170ba72009-12-14 01:45:37 +00002334 break;
John McCall2f27bf82010-02-04 02:56:29 +00002335 }
Anders Carlssone170ba72009-12-14 01:45:37 +00002336
2337 case Expr::ConditionalOperatorClass: {
2338 const ConditionalOperator *CO = cast<ConditionalOperator>(E);
2339 mangleOperatorName(OO_Conditional, /*Arity=*/3);
2340 mangleExpression(CO->getCond());
John McCall5e1e89b2010-08-18 19:18:59 +00002341 mangleExpression(CO->getLHS(), Arity);
2342 mangleExpression(CO->getRHS(), Arity);
Anders Carlssone170ba72009-12-14 01:45:37 +00002343 break;
2344 }
2345
Douglas Gregor46287c72010-01-29 16:37:09 +00002346 case Expr::ImplicitCastExprClass: {
John McCall5e1e89b2010-08-18 19:18:59 +00002347 mangleExpression(cast<ImplicitCastExpr>(E)->getSubExpr(), Arity);
Douglas Gregor46287c72010-01-29 16:37:09 +00002348 break;
2349 }
2350
2351 case Expr::CStyleCastExprClass:
2352 case Expr::CXXStaticCastExprClass:
2353 case Expr::CXXDynamicCastExprClass:
2354 case Expr::CXXReinterpretCastExprClass:
2355 case Expr::CXXConstCastExprClass:
2356 case Expr::CXXFunctionalCastExprClass: {
2357 const ExplicitCastExpr *ECE = cast<ExplicitCastExpr>(E);
2358 Out << "cv";
2359 mangleType(ECE->getType());
2360 mangleExpression(ECE->getSubExpr());
2361 break;
2362 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002363
Anders Carlsson58040a52009-12-16 05:48:46 +00002364 case Expr::CXXOperatorCallExprClass: {
2365 const CXXOperatorCallExpr *CE = cast<CXXOperatorCallExpr>(E);
2366 unsigned NumArgs = CE->getNumArgs();
2367 mangleOperatorName(CE->getOperator(), /*Arity=*/NumArgs);
2368 // Mangle the arguments.
2369 for (unsigned i = 0; i != NumArgs; ++i)
2370 mangleExpression(CE->getArg(i));
2371 break;
2372 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002373
Anders Carlssona7694082009-11-06 02:50:19 +00002374 case Expr::ParenExprClass:
John McCall5e1e89b2010-08-18 19:18:59 +00002375 mangleExpression(cast<ParenExpr>(E)->getSubExpr(), Arity);
Anders Carlssona7694082009-11-06 02:50:19 +00002376 break;
2377
Anders Carlssond553f8c2009-09-21 01:21:10 +00002378 case Expr::DeclRefExprClass: {
Douglas Gregor5ed1bc32010-02-28 21:40:32 +00002379 const NamedDecl *D = cast<DeclRefExpr>(E)->getDecl();
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002380
Anders Carlssond553f8c2009-09-21 01:21:10 +00002381 switch (D->getKind()) {
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002382 default:
Douglas Gregor5ed1bc32010-02-28 21:40:32 +00002383 // <expr-primary> ::= L <mangled-name> E # external name
2384 Out << 'L';
2385 mangle(D, "_Z");
2386 Out << 'E';
2387 break;
2388
John McCallfb44de92011-05-01 22:35:37 +00002389 case Decl::ParmVar:
2390 mangleFunctionParam(cast<ParmVarDecl>(D));
2391 break;
2392
John McCall3dc7e7b2010-07-24 01:17:35 +00002393 case Decl::EnumConstant: {
2394 const EnumConstantDecl *ED = cast<EnumConstantDecl>(D);
2395 mangleIntegerLiteral(ED->getType(), ED->getInitVal());
2396 break;
2397 }
2398
Anders Carlssond553f8c2009-09-21 01:21:10 +00002399 case Decl::NonTypeTemplateParm: {
2400 const NonTypeTemplateParmDecl *PD = cast<NonTypeTemplateParmDecl>(D);
Anders Carlsson0ccdf8d2009-09-27 00:38:53 +00002401 mangleTemplateParameter(PD->getIndex());
Anders Carlssond553f8c2009-09-21 01:21:10 +00002402 break;
2403 }
2404
2405 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002406
Anders Carlsson50755b02009-09-27 20:11:34 +00002407 break;
Anders Carlssond553f8c2009-09-21 01:21:10 +00002408 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002409
Douglas Gregorc7793c72011-01-15 01:15:58 +00002410 case Expr::SubstNonTypeTemplateParmPackExprClass:
2411 mangleTemplateParameter(
2412 cast<SubstNonTypeTemplateParmPackExpr>(E)->getParameterPack()->getIndex());
2413 break;
2414
John McCall865d4472009-11-19 22:55:06 +00002415 case Expr::DependentScopeDeclRefExprClass: {
2416 const DependentScopeDeclRefExpr *DRE = cast<DependentScopeDeclRefExpr>(E);
Douglas Gregor4b2ccfc2010-02-28 22:05:49 +00002417 NestedNameSpecifier *NNS = DRE->getQualifier();
2418 const Type *QTy = NNS->getAsType();
2419
2420 // When we're dealing with a nested-name-specifier that has just a
2421 // dependent identifier in it, mangle that as a typename. FIXME:
2422 // It isn't clear that we ever actually want to have such a
2423 // nested-name-specifier; why not just represent it as a typename type?
2424 if (!QTy && NNS->getAsIdentifier() && NNS->getPrefix()) {
Douglas Gregor4a2023f2010-03-31 20:19:30 +00002425 QTy = getASTContext().getDependentNameType(ETK_Typename,
2426 NNS->getPrefix(),
2427 NNS->getAsIdentifier())
Douglas Gregor4b2ccfc2010-02-28 22:05:49 +00002428 .getTypePtr();
2429 }
Anders Carlsson50755b02009-09-27 20:11:34 +00002430 assert(QTy && "Qualifier was not type!");
2431
John McCall6dbce192010-08-20 00:17:19 +00002432 // ::= sr <type> <unqualified-name> # dependent name
2433 // ::= sr <type> <unqualified-name> <template-args> # dependent template-id
Anders Carlsson50755b02009-09-27 20:11:34 +00002434 Out << "sr";
2435 mangleType(QualType(QTy, 0));
John McCall5e1e89b2010-08-18 19:18:59 +00002436 mangleUnqualifiedName(0, DRE->getDeclName(), Arity);
John McCall6dbce192010-08-20 00:17:19 +00002437 if (DRE->hasExplicitTemplateArgs())
2438 mangleTemplateArgs(DRE->getExplicitTemplateArgs());
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002439
Anders Carlsson50755b02009-09-27 20:11:34 +00002440 break;
2441 }
2442
John McCalld9307602010-04-09 22:54:09 +00002443 case Expr::CXXBindTemporaryExprClass:
2444 mangleExpression(cast<CXXBindTemporaryExpr>(E)->getSubExpr());
2445 break;
2446
John McCall4765fa02010-12-06 08:20:24 +00002447 case Expr::ExprWithCleanupsClass:
2448 mangleExpression(cast<ExprWithCleanups>(E)->getSubExpr(), Arity);
John McCalld9307602010-04-09 22:54:09 +00002449 break;
2450
John McCall1dd73832010-02-04 01:42:13 +00002451 case Expr::FloatingLiteralClass: {
2452 const FloatingLiteral *FL = cast<FloatingLiteral>(E);
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002453 Out << 'L';
John McCall1dd73832010-02-04 01:42:13 +00002454 mangleType(FL->getType());
John McCall0512e482010-07-14 04:20:34 +00002455 mangleFloat(FL->getValue());
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002456 Out << 'E';
John McCall1dd73832010-02-04 01:42:13 +00002457 break;
2458 }
2459
John McCallde810632010-04-09 21:48:08 +00002460 case Expr::CharacterLiteralClass:
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002461 Out << 'L';
John McCallde810632010-04-09 21:48:08 +00002462 mangleType(E->getType());
2463 Out << cast<CharacterLiteral>(E)->getValue();
2464 Out << 'E';
2465 break;
2466
2467 case Expr::CXXBoolLiteralExprClass:
2468 Out << "Lb";
2469 Out << (cast<CXXBoolLiteralExpr>(E)->getValue() ? '1' : '0');
2470 Out << 'E';
2471 break;
2472
John McCall0512e482010-07-14 04:20:34 +00002473 case Expr::IntegerLiteralClass: {
2474 llvm::APSInt Value(cast<IntegerLiteral>(E)->getValue());
2475 if (E->getType()->isSignedIntegerType())
2476 Value.setIsSigned(true);
2477 mangleIntegerLiteral(E->getType(), Value);
Anders Carlssone170ba72009-12-14 01:45:37 +00002478 break;
John McCall0512e482010-07-14 04:20:34 +00002479 }
2480
2481 case Expr::ImaginaryLiteralClass: {
2482 const ImaginaryLiteral *IE = cast<ImaginaryLiteral>(E);
2483 // Mangle as if a complex literal.
Nick Lewycky271b6652010-09-05 03:40:33 +00002484 // Proposal from David Vandevoorde, 2010.06.30.
John McCall0512e482010-07-14 04:20:34 +00002485 Out << 'L';
2486 mangleType(E->getType());
2487 if (const FloatingLiteral *Imag =
2488 dyn_cast<FloatingLiteral>(IE->getSubExpr())) {
2489 // Mangle a floating-point zero of the appropriate type.
2490 mangleFloat(llvm::APFloat(Imag->getValue().getSemantics()));
2491 Out << '_';
2492 mangleFloat(Imag->getValue());
2493 } else {
Nick Lewycky271b6652010-09-05 03:40:33 +00002494 Out << "0_";
John McCall0512e482010-07-14 04:20:34 +00002495 llvm::APSInt Value(cast<IntegerLiteral>(IE->getSubExpr())->getValue());
2496 if (IE->getSubExpr()->getType()->isSignedIntegerType())
2497 Value.setIsSigned(true);
2498 mangleNumber(Value);
2499 }
2500 Out << 'E';
2501 break;
2502 }
2503
2504 case Expr::StringLiteralClass: {
John McCall1658c392010-07-15 21:53:03 +00002505 // Revised proposal from David Vandervoorde, 2010.07.15.
John McCall0512e482010-07-14 04:20:34 +00002506 Out << 'L';
John McCall1658c392010-07-15 21:53:03 +00002507 assert(isa<ConstantArrayType>(E->getType()));
2508 mangleType(E->getType());
John McCall0512e482010-07-14 04:20:34 +00002509 Out << 'E';
2510 break;
2511 }
2512
2513 case Expr::GNUNullExprClass:
2514 // FIXME: should this really be mangled the same as nullptr?
2515 // fallthrough
2516
2517 case Expr::CXXNullPtrLiteralExprClass: {
2518 // Proposal from David Vandervoorde, 2010.06.30, as
2519 // modified by ABI list discussion.
2520 Out << "LDnE";
2521 break;
2522 }
Douglas Gregorbe230c32011-01-03 17:17:50 +00002523
2524 case Expr::PackExpansionExprClass:
2525 Out << "sp";
2526 mangleExpression(cast<PackExpansionExpr>(E)->getPattern());
2527 break;
Douglas Gregor2e774c42011-01-04 18:56:13 +00002528
2529 case Expr::SizeOfPackExprClass: {
Douglas Gregor2e774c42011-01-04 18:56:13 +00002530 Out << "sZ";
2531 const NamedDecl *Pack = cast<SizeOfPackExpr>(E)->getPack();
2532 if (const TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(Pack))
2533 mangleTemplateParameter(TTP->getIndex());
2534 else if (const NonTypeTemplateParmDecl *NTTP
2535 = dyn_cast<NonTypeTemplateParmDecl>(Pack))
2536 mangleTemplateParameter(NTTP->getIndex());
2537 else if (const TemplateTemplateParmDecl *TempTP
2538 = dyn_cast<TemplateTemplateParmDecl>(Pack))
2539 mangleTemplateParameter(TempTP->getIndex());
2540 else {
Douglas Gregor4fc48662011-01-13 16:39:34 +00002541 // Note: proposed by Mike Herrick on 11/30/10
2542 // <expression> ::= sZ <function-param> # size of function parameter pack
Douglas Gregor2e774c42011-01-04 18:56:13 +00002543 Diagnostic &Diags = Context.getDiags();
2544 unsigned DiagID = Diags.getCustomDiagID(Diagnostic::Error,
2545 "cannot mangle sizeof...(function parameter pack)");
2546 Diags.Report(DiagID);
2547 return;
2548 }
Douglas Gregordfbbcf92011-03-03 02:20:19 +00002549 break;
Douglas Gregor2e774c42011-01-04 18:56:13 +00002550 }
Anders Carlssond553f8c2009-09-21 01:21:10 +00002551 }
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00002552}
2553
John McCallfb44de92011-05-01 22:35:37 +00002554/// Mangle an expression which refers to a parameter variable.
2555///
2556/// <expression> ::= <function-param>
2557/// <function-param> ::= fp <top-level CV-qualifiers> _ # L == 0, I == 0
2558/// <function-param> ::= fp <top-level CV-qualifiers>
2559/// <parameter-2 non-negative number> _ # L == 0, I > 0
2560/// <function-param> ::= fL <L-1 non-negative number>
2561/// p <top-level CV-qualifiers> _ # L > 0, I == 0
2562/// <function-param> ::= fL <L-1 non-negative number>
2563/// p <top-level CV-qualifiers>
2564/// <I-1 non-negative number> _ # L > 0, I > 0
2565///
2566/// L is the nesting depth of the parameter, defined as 1 if the
2567/// parameter comes from the innermost function prototype scope
2568/// enclosing the current context, 2 if from the next enclosing
2569/// function prototype scope, and so on, with one special case: if
2570/// we've processed the full parameter clause for the innermost
2571/// function type, then L is one less. This definition conveniently
2572/// makes it irrelevant whether a function's result type was written
2573/// trailing or leading, but is otherwise overly complicated; the
2574/// numbering was first designed without considering references to
2575/// parameter in locations other than return types, and then the
2576/// mangling had to be generalized without changing the existing
2577/// manglings.
2578///
2579/// I is the zero-based index of the parameter within its parameter
2580/// declaration clause. Note that the original ABI document describes
2581/// this using 1-based ordinals.
2582void CXXNameMangler::mangleFunctionParam(const ParmVarDecl *parm) {
2583 unsigned parmDepth = parm->getFunctionScopeDepth();
2584 unsigned parmIndex = parm->getFunctionScopeIndex();
2585
2586 // Compute 'L'.
2587 // parmDepth does not include the declaring function prototype.
2588 // FunctionTypeDepth does account for that.
2589 assert(parmDepth < FunctionTypeDepth.getDepth());
2590 unsigned nestingDepth = FunctionTypeDepth.getDepth() - parmDepth;
2591 if (FunctionTypeDepth.isInResultType())
2592 nestingDepth--;
2593
2594 if (nestingDepth == 0) {
2595 Out << "fp";
2596 } else {
2597 Out << "fL" << (nestingDepth - 1) << 'p';
2598 }
2599
2600 // Top-level qualifiers. We don't have to worry about arrays here,
2601 // because parameters declared as arrays should already have been
2602 // tranformed to have pointer type. FIXME: apparently these don't
2603 // get mangled if used as an rvalue of a known non-class type?
2604 assert(!parm->getType()->isArrayType()
2605 && "parameter's type is still an array type?");
2606 mangleQualifiers(parm->getType().getQualifiers());
2607
2608 // Parameter index.
2609 if (parmIndex != 0) {
2610 Out << (parmIndex - 1);
2611 }
2612 Out << '_';
2613}
2614
Anders Carlsson3ac86b52009-04-15 05:36:58 +00002615void CXXNameMangler::mangleCXXCtorType(CXXCtorType T) {
2616 // <ctor-dtor-name> ::= C1 # complete object constructor
2617 // ::= C2 # base object constructor
2618 // ::= C3 # complete object allocating constructor
2619 //
2620 switch (T) {
2621 case Ctor_Complete:
2622 Out << "C1";
2623 break;
2624 case Ctor_Base:
2625 Out << "C2";
2626 break;
2627 case Ctor_CompleteAllocating:
2628 Out << "C3";
2629 break;
2630 }
2631}
2632
Anders Carlsson27ae5362009-04-17 01:58:57 +00002633void CXXNameMangler::mangleCXXDtorType(CXXDtorType T) {
2634 // <ctor-dtor-name> ::= D0 # deleting destructor
2635 // ::= D1 # complete object destructor
2636 // ::= D2 # base object destructor
2637 //
2638 switch (T) {
2639 case Dtor_Deleting:
2640 Out << "D0";
2641 break;
2642 case Dtor_Complete:
2643 Out << "D1";
2644 break;
2645 case Dtor_Base:
2646 Out << "D2";
2647 break;
2648 }
2649}
2650
John McCall6dbce192010-08-20 00:17:19 +00002651void CXXNameMangler::mangleTemplateArgs(
2652 const ExplicitTemplateArgumentList &TemplateArgs) {
2653 // <template-args> ::= I <template-arg>+ E
2654 Out << 'I';
John McCall4f4e4132011-05-04 01:45:19 +00002655 for (unsigned i = 0, e = TemplateArgs.NumTemplateArgs; i != e; ++i)
2656 mangleTemplateArg(0, TemplateArgs.getTemplateArgs()[i].getArgument());
John McCall6dbce192010-08-20 00:17:19 +00002657 Out << 'E';
2658}
2659
Douglas Gregor20f0cc72010-04-23 03:10:43 +00002660void CXXNameMangler::mangleTemplateArgs(TemplateName Template,
2661 const TemplateArgument *TemplateArgs,
2662 unsigned NumTemplateArgs) {
2663 if (TemplateDecl *TD = Template.getAsTemplateDecl())
2664 return mangleTemplateArgs(*TD->getTemplateParameters(), TemplateArgs,
2665 NumTemplateArgs);
Sean Huntc3021132010-05-05 15:23:54 +00002666
John McCall4f4e4132011-05-04 01:45:19 +00002667 mangleUnresolvedTemplateArgs(TemplateArgs, NumTemplateArgs);
2668}
2669
2670void CXXNameMangler::mangleUnresolvedTemplateArgs(const TemplateArgument *args,
2671 unsigned numArgs) {
Douglas Gregor20f0cc72010-04-23 03:10:43 +00002672 // <template-args> ::= I <template-arg>+ E
2673 Out << 'I';
John McCall4f4e4132011-05-04 01:45:19 +00002674 for (unsigned i = 0; i != numArgs; ++i)
2675 mangleTemplateArg(0, args[i]);
Douglas Gregor20f0cc72010-04-23 03:10:43 +00002676 Out << 'E';
2677}
2678
Rafael Espindolad9800722010-03-11 14:07:00 +00002679void CXXNameMangler::mangleTemplateArgs(const TemplateParameterList &PL,
2680 const TemplateArgumentList &AL) {
Anders Carlsson7a0ba872009-05-15 16:09:15 +00002681 // <template-args> ::= I <template-arg>+ E
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002682 Out << 'I';
Rafael Espindolad9800722010-03-11 14:07:00 +00002683 for (unsigned i = 0, e = AL.size(); i != e; ++i)
2684 mangleTemplateArg(PL.getParam(i), AL[i]);
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002685 Out << 'E';
Anders Carlsson7a0ba872009-05-15 16:09:15 +00002686}
2687
Rafael Espindolad9800722010-03-11 14:07:00 +00002688void CXXNameMangler::mangleTemplateArgs(const TemplateParameterList &PL,
2689 const TemplateArgument *TemplateArgs,
Anders Carlsson7624f212009-09-18 02:42:01 +00002690 unsigned NumTemplateArgs) {
2691 // <template-args> ::= I <template-arg>+ E
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002692 Out << 'I';
Daniel Dunbar7e0c1952009-11-21 09:17:15 +00002693 for (unsigned i = 0; i != NumTemplateArgs; ++i)
Rafael Espindolad9800722010-03-11 14:07:00 +00002694 mangleTemplateArg(PL.getParam(i), TemplateArgs[i]);
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002695 Out << 'E';
Anders Carlsson7624f212009-09-18 02:42:01 +00002696}
2697
Rafael Espindolad9800722010-03-11 14:07:00 +00002698void CXXNameMangler::mangleTemplateArg(const NamedDecl *P,
2699 const TemplateArgument &A) {
Mike Stump1eb44332009-09-09 15:08:12 +00002700 // <template-arg> ::= <type> # type or template
Anders Carlsson7a0ba872009-05-15 16:09:15 +00002701 // ::= X <expression> E # expression
2702 // ::= <expr-primary> # simple expressions
Douglas Gregor4fc48662011-01-13 16:39:34 +00002703 // ::= J <template-arg>* E # argument pack
Anders Carlsson7a0ba872009-05-15 16:09:15 +00002704 // ::= sp <expression> # pack expansion of (C++0x)
2705 switch (A.getKind()) {
Douglas Gregorf90b27a2011-01-03 22:36:02 +00002706 case TemplateArgument::Null:
2707 llvm_unreachable("Cannot mangle NULL template argument");
2708
Anders Carlsson7a0ba872009-05-15 16:09:15 +00002709 case TemplateArgument::Type:
2710 mangleType(A.getAsType());
2711 break;
Anders Carlsson9e85c742009-12-23 19:30:55 +00002712 case TemplateArgument::Template:
John McCallb6f532e2010-07-14 06:43:17 +00002713 // This is mangled as <type>.
2714 mangleType(A.getAsTemplate());
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002715 break;
Douglas Gregora7fc9012011-01-05 18:58:31 +00002716 case TemplateArgument::TemplateExpansion:
Douglas Gregor4fc48662011-01-13 16:39:34 +00002717 // <type> ::= Dp <type> # pack expansion (C++0x)
Douglas Gregora7fc9012011-01-05 18:58:31 +00002718 Out << "Dp";
2719 mangleType(A.getAsTemplateOrTemplatePattern());
2720 break;
Anders Carlssond553f8c2009-09-21 01:21:10 +00002721 case TemplateArgument::Expression:
2722 Out << 'X';
2723 mangleExpression(A.getAsExpr());
2724 Out << 'E';
2725 break;
Anders Carlssone170ba72009-12-14 01:45:37 +00002726 case TemplateArgument::Integral:
2727 mangleIntegerLiteral(A.getIntegralType(), *A.getAsIntegral());
Anders Carlsson7a0ba872009-05-15 16:09:15 +00002728 break;
Daniel Dunbar7e0c1952009-11-21 09:17:15 +00002729 case TemplateArgument::Declaration: {
Douglas Gregor20f0cc72010-04-23 03:10:43 +00002730 assert(P && "Missing template parameter for declaration argument");
Daniel Dunbar7e0c1952009-11-21 09:17:15 +00002731 // <expr-primary> ::= L <mangled-name> E # external name
2732
Rafael Espindolad9800722010-03-11 14:07:00 +00002733 // Clang produces AST's where pointer-to-member-function expressions
Daniel Dunbar7e0c1952009-11-21 09:17:15 +00002734 // and pointer-to-function expressions are represented as a declaration not
Rafael Espindolad9800722010-03-11 14:07:00 +00002735 // an expression. We compensate for it here to produce the correct mangling.
2736 NamedDecl *D = cast<NamedDecl>(A.getAsDecl());
2737 const NonTypeTemplateParmDecl *Parameter = cast<NonTypeTemplateParmDecl>(P);
John McCallc0a45592011-04-24 08:43:07 +00002738 bool compensateMangling = !Parameter->getType()->isReferenceType();
Rafael Espindolad9800722010-03-11 14:07:00 +00002739 if (compensateMangling) {
2740 Out << 'X';
2741 mangleOperatorName(OO_Amp, 1);
2742 }
2743
Daniel Dunbar7e0c1952009-11-21 09:17:15 +00002744 Out << 'L';
2745 // References to external entities use the mangled name; if the name would
2746 // not normally be manged then mangle it as unqualified.
2747 //
2748 // FIXME: The ABI specifies that external names here should have _Z, but
2749 // gcc leaves this off.
Rafael Espindolad9800722010-03-11 14:07:00 +00002750 if (compensateMangling)
2751 mangle(D, "_Z");
2752 else
2753 mangle(D, "Z");
Daniel Dunbar7e0c1952009-11-21 09:17:15 +00002754 Out << 'E';
Rafael Espindolad9800722010-03-11 14:07:00 +00002755
2756 if (compensateMangling)
2757 Out << 'E';
2758
Daniel Dunbar7e0c1952009-11-21 09:17:15 +00002759 break;
2760 }
Douglas Gregorf90b27a2011-01-03 22:36:02 +00002761
2762 case TemplateArgument::Pack: {
2763 // Note: proposal by Mike Herrick on 12/20/10
2764 Out << 'J';
2765 for (TemplateArgument::pack_iterator PA = A.pack_begin(),
2766 PAEnd = A.pack_end();
2767 PA != PAEnd; ++PA)
2768 mangleTemplateArg(P, *PA);
2769 Out << 'E';
2770 }
Daniel Dunbar7e0c1952009-11-21 09:17:15 +00002771 }
Anders Carlsson7a0ba872009-05-15 16:09:15 +00002772}
2773
Anders Carlsson0ccdf8d2009-09-27 00:38:53 +00002774void CXXNameMangler::mangleTemplateParameter(unsigned Index) {
2775 // <template-param> ::= T_ # first template parameter
2776 // ::= T <parameter-2 non-negative number> _
2777 if (Index == 0)
2778 Out << "T_";
2779 else
2780 Out << 'T' << (Index - 1) << '_';
2781}
2782
Anders Carlsson76967372009-09-17 00:43:46 +00002783// <substitution> ::= S <seq-id> _
2784// ::= S_
Anders Carlsson6862fc72009-09-17 04:16:28 +00002785bool CXXNameMangler::mangleSubstitution(const NamedDecl *ND) {
Anders Carlssone7c8cb62009-09-26 20:53:44 +00002786 // Try one of the standard substitutions first.
2787 if (mangleStandardSubstitution(ND))
2788 return true;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002789
Anders Carlsson433d1372009-11-07 04:26:04 +00002790 ND = cast<NamedDecl>(ND->getCanonicalDecl());
Anders Carlsson6862fc72009-09-17 04:16:28 +00002791 return mangleSubstitution(reinterpret_cast<uintptr_t>(ND));
2792}
2793
Anders Carlsson76967372009-09-17 00:43:46 +00002794bool CXXNameMangler::mangleSubstitution(QualType T) {
Anders Carlssond99edc42009-09-26 03:55:37 +00002795 if (!T.getCVRQualifiers()) {
2796 if (const RecordType *RT = T->getAs<RecordType>())
2797 return mangleSubstitution(RT->getDecl());
2798 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002799
Anders Carlsson76967372009-09-17 00:43:46 +00002800 uintptr_t TypePtr = reinterpret_cast<uintptr_t>(T.getAsOpaquePtr());
2801
Anders Carlssond3a932a2009-09-17 03:53:28 +00002802 return mangleSubstitution(TypePtr);
2803}
2804
Douglas Gregor1e9268e2010-04-28 05:58:56 +00002805bool CXXNameMangler::mangleSubstitution(TemplateName Template) {
2806 if (TemplateDecl *TD = Template.getAsTemplateDecl())
2807 return mangleSubstitution(TD);
Sean Huntc3021132010-05-05 15:23:54 +00002808
Douglas Gregor1e9268e2010-04-28 05:58:56 +00002809 Template = Context.getASTContext().getCanonicalTemplateName(Template);
2810 return mangleSubstitution(
2811 reinterpret_cast<uintptr_t>(Template.getAsVoidPointer()));
2812}
2813
Anders Carlssond3a932a2009-09-17 03:53:28 +00002814bool CXXNameMangler::mangleSubstitution(uintptr_t Ptr) {
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002815 llvm::DenseMap<uintptr_t, unsigned>::iterator I = Substitutions.find(Ptr);
Anders Carlsson76967372009-09-17 00:43:46 +00002816 if (I == Substitutions.end())
2817 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002818
Anders Carlsson76967372009-09-17 00:43:46 +00002819 unsigned SeqID = I->second;
2820 if (SeqID == 0)
2821 Out << "S_";
2822 else {
2823 SeqID--;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002824
Anders Carlsson76967372009-09-17 00:43:46 +00002825 // <seq-id> is encoded in base-36, using digits and upper case letters.
2826 char Buffer[10];
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002827 char *BufferPtr = llvm::array_endof(Buffer);
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002828
Anders Carlsson76967372009-09-17 00:43:46 +00002829 if (SeqID == 0) *--BufferPtr = '0';
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002830
Anders Carlsson76967372009-09-17 00:43:46 +00002831 while (SeqID) {
2832 assert(BufferPtr > Buffer && "Buffer overflow!");
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002833
John McCall6ab30e02010-06-09 07:26:17 +00002834 char c = static_cast<char>(SeqID % 36);
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002835
Anders Carlsson76967372009-09-17 00:43:46 +00002836 *--BufferPtr = (c < 10 ? '0' + c : 'A' + c - 10);
2837 SeqID /= 36;
2838 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002839
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002840 Out << 'S'
2841 << llvm::StringRef(BufferPtr, llvm::array_endof(Buffer)-BufferPtr)
2842 << '_';
Anders Carlsson76967372009-09-17 00:43:46 +00002843 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002844
Anders Carlsson76967372009-09-17 00:43:46 +00002845 return true;
2846}
2847
Anders Carlssonf514b542009-09-27 00:12:57 +00002848static bool isCharType(QualType T) {
2849 if (T.isNull())
2850 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002851
Anders Carlssonf514b542009-09-27 00:12:57 +00002852 return T->isSpecificBuiltinType(BuiltinType::Char_S) ||
2853 T->isSpecificBuiltinType(BuiltinType::Char_U);
2854}
2855
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002856/// isCharSpecialization - Returns whether a given type is a template
Anders Carlssonf514b542009-09-27 00:12:57 +00002857/// specialization of a given name with a single argument of type char.
2858static bool isCharSpecialization(QualType T, const char *Name) {
2859 if (T.isNull())
2860 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002861
Anders Carlssonf514b542009-09-27 00:12:57 +00002862 const RecordType *RT = T->getAs<RecordType>();
2863 if (!RT)
2864 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002865
2866 const ClassTemplateSpecializationDecl *SD =
Anders Carlssonf514b542009-09-27 00:12:57 +00002867 dyn_cast<ClassTemplateSpecializationDecl>(RT->getDecl());
2868 if (!SD)
2869 return false;
2870
2871 if (!isStdNamespace(SD->getDeclContext()))
2872 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002873
Anders Carlssonf514b542009-09-27 00:12:57 +00002874 const TemplateArgumentList &TemplateArgs = SD->getTemplateArgs();
2875 if (TemplateArgs.size() != 1)
2876 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002877
Anders Carlssonf514b542009-09-27 00:12:57 +00002878 if (!isCharType(TemplateArgs[0].getAsType()))
2879 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002880
Daniel Dunbar01eb9b92009-10-18 21:17:35 +00002881 return SD->getIdentifier()->getName() == Name;
Anders Carlssonf514b542009-09-27 00:12:57 +00002882}
2883
Anders Carlsson91f88602009-12-07 19:56:42 +00002884template <std::size_t StrLen>
Benjamin Kramer54353f42010-11-25 18:29:30 +00002885static bool isStreamCharSpecialization(const ClassTemplateSpecializationDecl*SD,
2886 const char (&Str)[StrLen]) {
Anders Carlsson91f88602009-12-07 19:56:42 +00002887 if (!SD->getIdentifier()->isStr(Str))
2888 return false;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002889
Anders Carlsson91f88602009-12-07 19:56:42 +00002890 const TemplateArgumentList &TemplateArgs = SD->getTemplateArgs();
2891 if (TemplateArgs.size() != 2)
2892 return false;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002893
Anders Carlsson91f88602009-12-07 19:56:42 +00002894 if (!isCharType(TemplateArgs[0].getAsType()))
2895 return false;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002896
Anders Carlsson91f88602009-12-07 19:56:42 +00002897 if (!isCharSpecialization(TemplateArgs[1].getAsType(), "char_traits"))
2898 return false;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002899
Anders Carlsson91f88602009-12-07 19:56:42 +00002900 return true;
2901}
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002902
Anders Carlssone7c8cb62009-09-26 20:53:44 +00002903bool CXXNameMangler::mangleStandardSubstitution(const NamedDecl *ND) {
2904 // <substitution> ::= St # ::std::
Anders Carlsson8c031552009-09-26 23:10:05 +00002905 if (const NamespaceDecl *NS = dyn_cast<NamespaceDecl>(ND)) {
Anders Carlsson47846d22009-12-04 06:23:23 +00002906 if (isStd(NS)) {
Anders Carlsson8c031552009-09-26 23:10:05 +00002907 Out << "St";
2908 return true;
2909 }
2910 }
2911
2912 if (const ClassTemplateDecl *TD = dyn_cast<ClassTemplateDecl>(ND)) {
2913 if (!isStdNamespace(TD->getDeclContext()))
2914 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002915
Anders Carlsson8c031552009-09-26 23:10:05 +00002916 // <substitution> ::= Sa # ::std::allocator
2917 if (TD->getIdentifier()->isStr("allocator")) {
2918 Out << "Sa";
2919 return true;
2920 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002921
Anders Carlsson189d59c2009-09-26 23:14:39 +00002922 // <<substitution> ::= Sb # ::std::basic_string
2923 if (TD->getIdentifier()->isStr("basic_string")) {
2924 Out << "Sb";
2925 return true;
2926 }
Anders Carlsson8c031552009-09-26 23:10:05 +00002927 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002928
2929 if (const ClassTemplateSpecializationDecl *SD =
Anders Carlssonf514b542009-09-27 00:12:57 +00002930 dyn_cast<ClassTemplateSpecializationDecl>(ND)) {
Eli Friedman5370ee22010-02-23 18:25:09 +00002931 if (!isStdNamespace(SD->getDeclContext()))
2932 return false;
2933
Anders Carlssonf514b542009-09-27 00:12:57 +00002934 // <substitution> ::= Ss # ::std::basic_string<char,
2935 // ::std::char_traits<char>,
2936 // ::std::allocator<char> >
2937 if (SD->getIdentifier()->isStr("basic_string")) {
2938 const TemplateArgumentList &TemplateArgs = SD->getTemplateArgs();
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002939
Anders Carlssonf514b542009-09-27 00:12:57 +00002940 if (TemplateArgs.size() != 3)
2941 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002942
Anders Carlssonf514b542009-09-27 00:12:57 +00002943 if (!isCharType(TemplateArgs[0].getAsType()))
2944 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002945
Anders Carlssonf514b542009-09-27 00:12:57 +00002946 if (!isCharSpecialization(TemplateArgs[1].getAsType(), "char_traits"))
2947 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002948
Anders Carlssonf514b542009-09-27 00:12:57 +00002949 if (!isCharSpecialization(TemplateArgs[2].getAsType(), "allocator"))
2950 return false;
2951
2952 Out << "Ss";
2953 return true;
2954 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002955
Anders Carlsson91f88602009-12-07 19:56:42 +00002956 // <substitution> ::= Si # ::std::basic_istream<char,
2957 // ::std::char_traits<char> >
2958 if (isStreamCharSpecialization(SD, "basic_istream")) {
2959 Out << "Si";
2960 return true;
2961 }
2962
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002963 // <substitution> ::= So # ::std::basic_ostream<char,
Anders Carlsson8f8fd8e2009-10-08 17:20:26 +00002964 // ::std::char_traits<char> >
Anders Carlsson91f88602009-12-07 19:56:42 +00002965 if (isStreamCharSpecialization(SD, "basic_ostream")) {
Anders Carlsson8f8fd8e2009-10-08 17:20:26 +00002966 Out << "So";
2967 return true;
2968 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002969
Anders Carlsson91f88602009-12-07 19:56:42 +00002970 // <substitution> ::= Sd # ::std::basic_iostream<char,
2971 // ::std::char_traits<char> >
2972 if (isStreamCharSpecialization(SD, "basic_iostream")) {
2973 Out << "Sd";
2974 return true;
2975 }
Anders Carlssonf514b542009-09-27 00:12:57 +00002976 }
Anders Carlsson8c031552009-09-26 23:10:05 +00002977 return false;
Anders Carlssone7c8cb62009-09-26 20:53:44 +00002978}
2979
Anders Carlsson76967372009-09-17 00:43:46 +00002980void CXXNameMangler::addSubstitution(QualType T) {
Anders Carlssond99edc42009-09-26 03:55:37 +00002981 if (!T.getCVRQualifiers()) {
2982 if (const RecordType *RT = T->getAs<RecordType>()) {
2983 addSubstitution(RT->getDecl());
2984 return;
2985 }
2986 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002987
Anders Carlsson76967372009-09-17 00:43:46 +00002988 uintptr_t TypePtr = reinterpret_cast<uintptr_t>(T.getAsOpaquePtr());
Anders Carlssond3a932a2009-09-17 03:53:28 +00002989 addSubstitution(TypePtr);
2990}
2991
Douglas Gregor1e9268e2010-04-28 05:58:56 +00002992void CXXNameMangler::addSubstitution(TemplateName Template) {
2993 if (TemplateDecl *TD = Template.getAsTemplateDecl())
2994 return addSubstitution(TD);
Sean Huntc3021132010-05-05 15:23:54 +00002995
Douglas Gregor1e9268e2010-04-28 05:58:56 +00002996 Template = Context.getASTContext().getCanonicalTemplateName(Template);
2997 addSubstitution(reinterpret_cast<uintptr_t>(Template.getAsVoidPointer()));
2998}
2999
Anders Carlssond3a932a2009-09-17 03:53:28 +00003000void CXXNameMangler::addSubstitution(uintptr_t Ptr) {
Anders Carlssond3a932a2009-09-17 03:53:28 +00003001 assert(!Substitutions.count(Ptr) && "Substitution already exists!");
Anders Carlsson9d85b722010-06-02 04:29:50 +00003002 Substitutions[Ptr] = SeqID++;
Anders Carlsson76967372009-09-17 00:43:46 +00003003}
3004
Daniel Dunbar1b077112009-11-21 09:06:10 +00003005//
Mike Stump1eb44332009-09-09 15:08:12 +00003006
Daniel Dunbar1b077112009-11-21 09:06:10 +00003007/// \brief Mangles the name of the declaration D and emits that name to the
3008/// given output stream.
3009///
3010/// If the declaration D requires a mangled name, this routine will emit that
3011/// mangled name to \p os and return true. Otherwise, \p os will be unchanged
3012/// and this routine will return false. In this case, the caller should just
3013/// emit the identifier of the declaration (\c D->getIdentifier()) as its
3014/// name.
Peter Collingbourne14110472011-01-13 18:57:25 +00003015void ItaniumMangleContext::mangleName(const NamedDecl *D,
Rafael Espindola0e376a02011-02-11 01:41:00 +00003016 llvm::raw_ostream &Out) {
Daniel Dunbarc02ab4c2009-11-21 09:14:44 +00003017 assert((isa<FunctionDecl>(D) || isa<VarDecl>(D)) &&
3018 "Invalid mangleName() call, argument is not a variable or function!");
3019 assert(!isa<CXXConstructorDecl>(D) && !isa<CXXDestructorDecl>(D) &&
3020 "Invalid mangleName() call on 'structor decl!");
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00003021
Daniel Dunbar1b077112009-11-21 09:06:10 +00003022 PrettyStackTraceDecl CrashInfo(D, SourceLocation(),
3023 getASTContext().getSourceManager(),
3024 "Mangling declaration");
Mike Stump1eb44332009-09-09 15:08:12 +00003025
John McCallfb44de92011-05-01 22:35:37 +00003026 CXXNameMangler Mangler(*this, Out, D);
Daniel Dunbar94fd26d2009-11-21 09:06:22 +00003027 return Mangler.mangle(D);
Daniel Dunbar1b077112009-11-21 09:06:10 +00003028}
Mike Stump1eb44332009-09-09 15:08:12 +00003029
Peter Collingbourne14110472011-01-13 18:57:25 +00003030void ItaniumMangleContext::mangleCXXCtor(const CXXConstructorDecl *D,
3031 CXXCtorType Type,
Rafael Espindola0e376a02011-02-11 01:41:00 +00003032 llvm::raw_ostream &Out) {
Rafael Espindolac4850c22011-02-10 23:59:36 +00003033 CXXNameMangler Mangler(*this, Out, D, Type);
Daniel Dunbar77939c92009-11-21 09:06:31 +00003034 Mangler.mangle(D);
Daniel Dunbar1b077112009-11-21 09:06:10 +00003035}
Mike Stump1eb44332009-09-09 15:08:12 +00003036
Peter Collingbourne14110472011-01-13 18:57:25 +00003037void ItaniumMangleContext::mangleCXXDtor(const CXXDestructorDecl *D,
3038 CXXDtorType Type,
Rafael Espindola0e376a02011-02-11 01:41:00 +00003039 llvm::raw_ostream &Out) {
Rafael Espindolac4850c22011-02-10 23:59:36 +00003040 CXXNameMangler Mangler(*this, Out, D, Type);
Daniel Dunbar77939c92009-11-21 09:06:31 +00003041 Mangler.mangle(D);
Daniel Dunbar1b077112009-11-21 09:06:10 +00003042}
Mike Stumpf1216772009-07-31 18:25:34 +00003043
Peter Collingbourne14110472011-01-13 18:57:25 +00003044void ItaniumMangleContext::mangleThunk(const CXXMethodDecl *MD,
3045 const ThunkInfo &Thunk,
Rafael Espindolaf0be9792011-02-11 02:52:17 +00003046 llvm::raw_ostream &Out) {
Anders Carlsson19879c92010-03-23 17:17:29 +00003047 // <special-name> ::= T <call-offset> <base encoding>
3048 // # base is the nominal target function of thunk
3049 // <special-name> ::= Tc <call-offset> <call-offset> <base encoding>
3050 // # base is the nominal target function of thunk
3051 // # first call-offset is 'this' adjustment
3052 // # second call-offset is result adjustment
Sean Huntc3021132010-05-05 15:23:54 +00003053
Anders Carlsson19879c92010-03-23 17:17:29 +00003054 assert(!isa<CXXDestructorDecl>(MD) &&
3055 "Use mangleCXXDtor for destructor decls!");
Rafael Espindolac4850c22011-02-10 23:59:36 +00003056 CXXNameMangler Mangler(*this, Out);
Anders Carlsson19879c92010-03-23 17:17:29 +00003057 Mangler.getStream() << "_ZT";
3058 if (!Thunk.Return.isEmpty())
3059 Mangler.getStream() << 'c';
Sean Huntc3021132010-05-05 15:23:54 +00003060
Anders Carlsson19879c92010-03-23 17:17:29 +00003061 // Mangle the 'this' pointer adjustment.
3062 Mangler.mangleCallOffset(Thunk.This.NonVirtual, Thunk.This.VCallOffsetOffset);
Sean Huntc3021132010-05-05 15:23:54 +00003063
Anders Carlsson19879c92010-03-23 17:17:29 +00003064 // Mangle the return pointer adjustment if there is one.
3065 if (!Thunk.Return.isEmpty())
3066 Mangler.mangleCallOffset(Thunk.Return.NonVirtual,
3067 Thunk.Return.VBaseOffsetOffset);
Sean Huntc3021132010-05-05 15:23:54 +00003068
Anders Carlsson19879c92010-03-23 17:17:29 +00003069 Mangler.mangleFunctionEncoding(MD);
3070}
3071
Sean Huntc3021132010-05-05 15:23:54 +00003072void
Peter Collingbourne14110472011-01-13 18:57:25 +00003073ItaniumMangleContext::mangleCXXDtorThunk(const CXXDestructorDecl *DD,
3074 CXXDtorType Type,
3075 const ThisAdjustment &ThisAdjustment,
Rafael Espindolaf0be9792011-02-11 02:52:17 +00003076 llvm::raw_ostream &Out) {
Anders Carlsson19879c92010-03-23 17:17:29 +00003077 // <special-name> ::= T <call-offset> <base encoding>
3078 // # base is the nominal target function of thunk
Rafael Espindolac4850c22011-02-10 23:59:36 +00003079 CXXNameMangler Mangler(*this, Out, DD, Type);
Anders Carlsson19879c92010-03-23 17:17:29 +00003080 Mangler.getStream() << "_ZT";
3081
3082 // Mangle the 'this' pointer adjustment.
Sean Huntc3021132010-05-05 15:23:54 +00003083 Mangler.mangleCallOffset(ThisAdjustment.NonVirtual,
Anders Carlsson19879c92010-03-23 17:17:29 +00003084 ThisAdjustment.VCallOffsetOffset);
3085
3086 Mangler.mangleFunctionEncoding(DD);
3087}
3088
Daniel Dunbarc0747712009-11-21 09:12:13 +00003089/// mangleGuardVariable - Returns the mangled name for a guard variable
3090/// for the passed in VarDecl.
Peter Collingbourne14110472011-01-13 18:57:25 +00003091void ItaniumMangleContext::mangleItaniumGuardVariable(const VarDecl *D,
Rafael Espindolaf0be9792011-02-11 02:52:17 +00003092 llvm::raw_ostream &Out) {
Daniel Dunbarc0747712009-11-21 09:12:13 +00003093 // <special-name> ::= GV <object name> # Guard variable for one-time
3094 // # initialization
Rafael Espindolac4850c22011-02-10 23:59:36 +00003095 CXXNameMangler Mangler(*this, Out);
Daniel Dunbarc0747712009-11-21 09:12:13 +00003096 Mangler.getStream() << "_ZGV";
3097 Mangler.mangleName(D);
3098}
3099
Peter Collingbourne14110472011-01-13 18:57:25 +00003100void ItaniumMangleContext::mangleReferenceTemporary(const VarDecl *D,
Rafael Espindolaf0be9792011-02-11 02:52:17 +00003101 llvm::raw_ostream &Out) {
Anders Carlsson715edf22010-06-26 16:09:40 +00003102 // We match the GCC mangling here.
3103 // <special-name> ::= GR <object name>
Rafael Espindolac4850c22011-02-10 23:59:36 +00003104 CXXNameMangler Mangler(*this, Out);
Anders Carlsson715edf22010-06-26 16:09:40 +00003105 Mangler.getStream() << "_ZGR";
3106 Mangler.mangleName(D);
3107}
3108
Peter Collingbourne14110472011-01-13 18:57:25 +00003109void ItaniumMangleContext::mangleCXXVTable(const CXXRecordDecl *RD,
Rafael Espindolaf0be9792011-02-11 02:52:17 +00003110 llvm::raw_ostream &Out) {
Daniel Dunbarc0747712009-11-21 09:12:13 +00003111 // <special-name> ::= TV <type> # virtual table
Rafael Espindolac4850c22011-02-10 23:59:36 +00003112 CXXNameMangler Mangler(*this, Out);
Daniel Dunbarc0747712009-11-21 09:12:13 +00003113 Mangler.getStream() << "_ZTV";
Douglas Gregor1b12a3b2010-05-26 05:11:13 +00003114 Mangler.mangleNameOrStandardSubstitution(RD);
Daniel Dunbar1b077112009-11-21 09:06:10 +00003115}
Mike Stump82d75b02009-11-10 01:58:37 +00003116
Peter Collingbourne14110472011-01-13 18:57:25 +00003117void ItaniumMangleContext::mangleCXXVTT(const CXXRecordDecl *RD,
Rafael Espindolaf0be9792011-02-11 02:52:17 +00003118 llvm::raw_ostream &Out) {
Daniel Dunbarc0747712009-11-21 09:12:13 +00003119 // <special-name> ::= TT <type> # VTT structure
Rafael Espindolac4850c22011-02-10 23:59:36 +00003120 CXXNameMangler Mangler(*this, Out);
Daniel Dunbarc0747712009-11-21 09:12:13 +00003121 Mangler.getStream() << "_ZTT";
Douglas Gregor1b12a3b2010-05-26 05:11:13 +00003122 Mangler.mangleNameOrStandardSubstitution(RD);
Daniel Dunbar1b077112009-11-21 09:06:10 +00003123}
Mike Stumpab3f7e92009-11-10 01:41:59 +00003124
Peter Collingbourne14110472011-01-13 18:57:25 +00003125void ItaniumMangleContext::mangleCXXCtorVTable(const CXXRecordDecl *RD,
3126 int64_t Offset,
3127 const CXXRecordDecl *Type,
Rafael Espindolaf0be9792011-02-11 02:52:17 +00003128 llvm::raw_ostream &Out) {
Daniel Dunbarc0747712009-11-21 09:12:13 +00003129 // <special-name> ::= TC <type> <offset number> _ <base type>
Rafael Espindolac4850c22011-02-10 23:59:36 +00003130 CXXNameMangler Mangler(*this, Out);
Daniel Dunbarc0747712009-11-21 09:12:13 +00003131 Mangler.getStream() << "_ZTC";
Douglas Gregor1b12a3b2010-05-26 05:11:13 +00003132 Mangler.mangleNameOrStandardSubstitution(RD);
Daniel Dunbarc0747712009-11-21 09:12:13 +00003133 Mangler.getStream() << Offset;
Benjamin Kramer35f59b62010-04-10 16:03:31 +00003134 Mangler.getStream() << '_';
Douglas Gregor1b12a3b2010-05-26 05:11:13 +00003135 Mangler.mangleNameOrStandardSubstitution(Type);
Daniel Dunbar1b077112009-11-21 09:06:10 +00003136}
Mike Stump738f8c22009-07-31 23:15:31 +00003137
Peter Collingbourne14110472011-01-13 18:57:25 +00003138void ItaniumMangleContext::mangleCXXRTTI(QualType Ty,
Rafael Espindolaf0be9792011-02-11 02:52:17 +00003139 llvm::raw_ostream &Out) {
Daniel Dunbarc0747712009-11-21 09:12:13 +00003140 // <special-name> ::= TI <type> # typeinfo structure
Douglas Gregor154fe982009-12-23 22:04:40 +00003141 assert(!Ty.hasQualifiers() && "RTTI info cannot have top-level qualifiers");
Rafael Espindolac4850c22011-02-10 23:59:36 +00003142 CXXNameMangler Mangler(*this, Out);
Daniel Dunbarc0747712009-11-21 09:12:13 +00003143 Mangler.getStream() << "_ZTI";
3144 Mangler.mangleType(Ty);
Daniel Dunbar1b077112009-11-21 09:06:10 +00003145}
Mike Stump67795982009-11-14 00:14:13 +00003146
Peter Collingbourne14110472011-01-13 18:57:25 +00003147void ItaniumMangleContext::mangleCXXRTTIName(QualType Ty,
Rafael Espindolaf0be9792011-02-11 02:52:17 +00003148 llvm::raw_ostream &Out) {
Daniel Dunbarc0747712009-11-21 09:12:13 +00003149 // <special-name> ::= TS <type> # typeinfo name (null terminated byte string)
Rafael Espindolac4850c22011-02-10 23:59:36 +00003150 CXXNameMangler Mangler(*this, Out);
Daniel Dunbarc0747712009-11-21 09:12:13 +00003151 Mangler.getStream() << "_ZTS";
3152 Mangler.mangleType(Ty);
Mike Stumpf1216772009-07-31 18:25:34 +00003153}
Peter Collingbourne14110472011-01-13 18:57:25 +00003154
3155MangleContext *clang::createItaniumMangleContext(ASTContext &Context,
3156 Diagnostic &Diags) {
3157 return new ItaniumMangleContext(Context, Diags);
3158}