blob: aad6e9c48dea5e3151c7158e211bc2ca1174b4a8 [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();
Anders Carlsson1b42c792009-04-02 16:24:45 +00001125 Out << 'Z';
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001126
Charles Davis685b1d92010-05-26 18:25:27 +00001127 if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(DC)) {
1128 mangleObjCMethodName(MD);
John McCall82b7d7b2010-10-18 21:28:44 +00001129 } else if (const CXXRecordDecl *RD = GetLocalClassDecl(ND)) {
1130 mangleFunctionEncoding(cast<FunctionDecl>(RD->getDeclContext()));
Fariborz Jahanian57058532010-03-03 19:41:08 +00001131 Out << 'E';
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001132
John McCall82b7d7b2010-10-18 21:28:44 +00001133 // Mangle the name relative to the closest enclosing function.
1134 if (ND == RD) // equality ok because RD derived from ND above
1135 mangleUnqualifiedName(ND);
1136 else
1137 mangleNestedName(ND, DC, true /*NoFunction*/);
1138
Fariborz Jahanian4819ac42010-03-04 01:02:03 +00001139 unsigned disc;
John McCall82b7d7b2010-10-18 21:28:44 +00001140 if (Context.getNextDiscriminator(RD, disc)) {
Fariborz Jahanian4819ac42010-03-04 01:02:03 +00001141 if (disc < 10)
1142 Out << '_' << disc;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001143 else
Fariborz Jahanian4819ac42010-03-04 01:02:03 +00001144 Out << "__" << disc << '_';
1145 }
Fariborz Jahanian57058532010-03-03 19:41:08 +00001146
1147 return;
1148 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001149 else
Fariborz Jahanian57058532010-03-03 19:41:08 +00001150 mangleFunctionEncoding(cast<FunctionDecl>(DC));
Anders Carlsson7b06f6c2009-12-10 03:14:39 +00001151
Anders Carlsson1b42c792009-04-02 16:24:45 +00001152 Out << 'E';
Eli Friedman6f9f25d2009-12-11 20:21:38 +00001153 mangleUnqualifiedName(ND);
Anders Carlsson1b42c792009-04-02 16:24:45 +00001154}
1155
John McCalla0ce15c2011-04-24 08:23:24 +00001156void CXXNameMangler::manglePrefix(NestedNameSpecifier *qualifier) {
1157 switch (qualifier->getKind()) {
1158 case NestedNameSpecifier::Global:
1159 // nothing
1160 return;
1161
1162 case NestedNameSpecifier::Namespace:
1163 mangleName(qualifier->getAsNamespace());
1164 return;
1165
1166 case NestedNameSpecifier::NamespaceAlias:
1167 mangleName(qualifier->getAsNamespaceAlias()->getNamespace());
1168 return;
1169
1170 case NestedNameSpecifier::TypeSpec:
1171 case NestedNameSpecifier::TypeSpecWithTemplate:
John McCall4f4e4132011-05-04 01:45:19 +00001172 manglePrefix(QualType(qualifier->getAsType(), 0));
John McCalla0ce15c2011-04-24 08:23:24 +00001173 return;
1174
1175 case NestedNameSpecifier::Identifier:
1176 // Member expressions can have these without prefixes, but that
1177 // should end up in mangleUnresolvedPrefix instead.
1178 assert(qualifier->getPrefix());
1179 manglePrefix(qualifier->getPrefix());
1180
1181 mangleSourceName(qualifier->getAsIdentifier());
1182 return;
1183 }
1184
1185 llvm_unreachable("unexpected nested name specifier");
1186}
1187
Fariborz Jahanian57058532010-03-03 19:41:08 +00001188void CXXNameMangler::manglePrefix(const DeclContext *DC, bool NoFunction) {
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001189 // <prefix> ::= <prefix> <unqualified-name>
1190 // ::= <template-prefix> <template-args>
1191 // ::= <template-param>
1192 // ::= # empty
1193 // ::= <substitution>
Anders Carlsson6862fc72009-09-17 04:16:28 +00001194
Anders Carlssonadd28822009-09-22 20:33:31 +00001195 while (isa<LinkageSpecDecl>(DC))
1196 DC = DC->getParent();
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00001197
Anders Carlsson9263e912009-09-18 18:39:58 +00001198 if (DC->isTranslationUnit())
1199 return;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00001200
Douglas Gregor35415f52010-05-25 17:04:15 +00001201 if (const BlockDecl *Block = dyn_cast<BlockDecl>(DC)) {
1202 manglePrefix(DC->getParent(), NoFunction);
1203 llvm::SmallString<64> Name;
Rafael Espindolac4850c22011-02-10 23:59:36 +00001204 llvm::raw_svector_ostream NameStream(Name);
1205 Context.mangleBlock(Block, NameStream);
1206 NameStream.flush();
Douglas Gregor35415f52010-05-25 17:04:15 +00001207 Out << Name.size() << Name;
1208 return;
1209 }
1210
Anders Carlsson6862fc72009-09-17 04:16:28 +00001211 if (mangleSubstitution(cast<NamedDecl>(DC)))
1212 return;
Anders Carlsson7482e242009-09-18 04:29:09 +00001213
Anders Carlsson2ee3fca2009-09-18 20:11:09 +00001214 // Check if we have a template.
1215 const TemplateArgumentList *TemplateArgs = 0;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00001216 if (const TemplateDecl *TD = isTemplate(cast<NamedDecl>(DC), TemplateArgs)) {
Anders Carlsson2ee3fca2009-09-18 20:11:09 +00001217 mangleTemplatePrefix(TD);
Rafael Espindolad9800722010-03-11 14:07:00 +00001218 TemplateParameterList *TemplateParameters = TD->getTemplateParameters();
1219 mangleTemplateArgs(*TemplateParameters, *TemplateArgs);
Fariborz Jahanian57058532010-03-03 19:41:08 +00001220 }
Douglas Gregor35415f52010-05-25 17:04:15 +00001221 else if(NoFunction && (isa<FunctionDecl>(DC) || isa<ObjCMethodDecl>(DC)))
Fariborz Jahanian57058532010-03-03 19:41:08 +00001222 return;
Douglas Gregor35415f52010-05-25 17:04:15 +00001223 else if (const ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(DC))
1224 mangleObjCMethodName(Method);
Fariborz Jahanian57058532010-03-03 19:41:08 +00001225 else {
1226 manglePrefix(DC->getParent(), NoFunction);
Anders Carlsson2ee3fca2009-09-18 20:11:09 +00001227 mangleUnqualifiedName(cast<NamedDecl>(DC));
1228 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00001229
Anders Carlsson6862fc72009-09-17 04:16:28 +00001230 addSubstitution(cast<NamedDecl>(DC));
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001231}
1232
Douglas Gregor20f0cc72010-04-23 03:10:43 +00001233void CXXNameMangler::mangleTemplatePrefix(TemplateName Template) {
1234 // <template-prefix> ::= <prefix> <template unqualified-name>
1235 // ::= <template-param>
1236 // ::= <substitution>
1237 if (TemplateDecl *TD = Template.getAsTemplateDecl())
1238 return mangleTemplatePrefix(TD);
1239
1240 if (QualifiedTemplateName *Qualified = Template.getAsQualifiedTemplateName())
John McCalla0ce15c2011-04-24 08:23:24 +00001241 manglePrefix(Qualified->getQualifier());
Sean Huntc3021132010-05-05 15:23:54 +00001242
Douglas Gregor20f0cc72010-04-23 03:10:43 +00001243 if (OverloadedTemplateStorage *Overloaded
1244 = Template.getAsOverloadedTemplate()) {
Sean Huntc3021132010-05-05 15:23:54 +00001245 mangleUnqualifiedName(0, (*Overloaded->begin())->getDeclName(),
Douglas Gregor20f0cc72010-04-23 03:10:43 +00001246 UnknownArity);
1247 return;
1248 }
Sean Huntc3021132010-05-05 15:23:54 +00001249
Douglas Gregor20f0cc72010-04-23 03:10:43 +00001250 DependentTemplateName *Dependent = Template.getAsDependentTemplateName();
1251 assert(Dependent && "Unknown template name kind?");
John McCalla0ce15c2011-04-24 08:23:24 +00001252 manglePrefix(Dependent->getQualifier());
Douglas Gregor1e9268e2010-04-28 05:58:56 +00001253 mangleUnscopedTemplateName(Template);
Douglas Gregor20f0cc72010-04-23 03:10:43 +00001254}
1255
Anders Carlsson0fa6df42009-09-26 19:45:45 +00001256void CXXNameMangler::mangleTemplatePrefix(const TemplateDecl *ND) {
Anders Carlsson7482e242009-09-18 04:29:09 +00001257 // <template-prefix> ::= <prefix> <template unqualified-name>
1258 // ::= <template-param>
1259 // ::= <substitution>
Douglas Gregor32fb4e12010-02-05 20:45:00 +00001260 // <template-template-param> ::= <template-param>
1261 // <substitution>
Anders Carlsson7482e242009-09-18 04:29:09 +00001262
Anders Carlssonaeb85372009-09-26 22:18:22 +00001263 if (mangleSubstitution(ND))
1264 return;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00001265
Douglas Gregor32fb4e12010-02-05 20:45:00 +00001266 // <template-template-param> ::= <template-param>
1267 if (const TemplateTemplateParmDecl *TTP
1268 = dyn_cast<TemplateTemplateParmDecl>(ND)) {
1269 mangleTemplateParameter(TTP->getIndex());
1270 return;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001271 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00001272
Anders Carlssonaa73ab12009-09-18 18:47:07 +00001273 manglePrefix(ND->getDeclContext());
Anders Carlsson1668f202009-09-26 20:13:56 +00001274 mangleUnqualifiedName(ND->getTemplatedDecl());
Anders Carlssonaeb85372009-09-26 22:18:22 +00001275 addSubstitution(ND);
Anders Carlsson7482e242009-09-18 04:29:09 +00001276}
1277
John McCallb6f532e2010-07-14 06:43:17 +00001278/// Mangles a template name under the production <type>. Required for
1279/// template template arguments.
1280/// <type> ::= <class-enum-type>
1281/// ::= <template-param>
1282/// ::= <substitution>
1283void CXXNameMangler::mangleType(TemplateName TN) {
1284 if (mangleSubstitution(TN))
1285 return;
1286
1287 TemplateDecl *TD = 0;
1288
1289 switch (TN.getKind()) {
1290 case TemplateName::QualifiedTemplate:
1291 TD = TN.getAsQualifiedTemplateName()->getTemplateDecl();
1292 goto HaveDecl;
1293
1294 case TemplateName::Template:
1295 TD = TN.getAsTemplateDecl();
1296 goto HaveDecl;
1297
1298 HaveDecl:
1299 if (isa<TemplateTemplateParmDecl>(TD))
1300 mangleTemplateParameter(cast<TemplateTemplateParmDecl>(TD)->getIndex());
1301 else
1302 mangleName(TD);
1303 break;
1304
1305 case TemplateName::OverloadedTemplate:
1306 llvm_unreachable("can't mangle an overloaded template name as a <type>");
1307 break;
1308
1309 case TemplateName::DependentTemplate: {
1310 const DependentTemplateName *Dependent = TN.getAsDependentTemplateName();
1311 assert(Dependent->isIdentifier());
1312
1313 // <class-enum-type> ::= <name>
1314 // <name> ::= <nested-name>
John McCalla0ce15c2011-04-24 08:23:24 +00001315 mangleUnresolvedPrefix(Dependent->getQualifier(), 0);
John McCallb6f532e2010-07-14 06:43:17 +00001316 mangleSourceName(Dependent->getIdentifier());
1317 break;
1318 }
1319
Douglas Gregor1aee05d2011-01-15 06:45:20 +00001320 case TemplateName::SubstTemplateTemplateParmPack: {
1321 SubstTemplateTemplateParmPackStorage *SubstPack
1322 = TN.getAsSubstTemplateTemplateParmPack();
1323 mangleTemplateParameter(SubstPack->getParameterPack()->getIndex());
1324 break;
1325 }
John McCallb6f532e2010-07-14 06:43:17 +00001326 }
1327
1328 addSubstitution(TN);
1329}
1330
Mike Stump1eb44332009-09-09 15:08:12 +00001331void
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001332CXXNameMangler::mangleOperatorName(OverloadedOperatorKind OO, unsigned Arity) {
1333 switch (OO) {
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001334 // <operator-name> ::= nw # new
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001335 case OO_New: Out << "nw"; break;
1336 // ::= na # new[]
1337 case OO_Array_New: Out << "na"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001338 // ::= dl # delete
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001339 case OO_Delete: Out << "dl"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001340 // ::= da # delete[]
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001341 case OO_Array_Delete: Out << "da"; break;
1342 // ::= ps # + (unary)
John McCall5e1e89b2010-08-18 19:18:59 +00001343 // ::= pl # + (binary or unknown)
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001344 case OO_Plus:
Anders Carlsson8257d412009-12-22 06:36:32 +00001345 Out << (Arity == 1? "ps" : "pl"); break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001346 // ::= ng # - (unary)
John McCall5e1e89b2010-08-18 19:18:59 +00001347 // ::= mi # - (binary or unknown)
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001348 case OO_Minus:
Anders Carlsson8257d412009-12-22 06:36:32 +00001349 Out << (Arity == 1? "ng" : "mi"); break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001350 // ::= ad # & (unary)
John McCall5e1e89b2010-08-18 19:18:59 +00001351 // ::= an # & (binary or unknown)
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001352 case OO_Amp:
Anders Carlsson8257d412009-12-22 06:36:32 +00001353 Out << (Arity == 1? "ad" : "an"); break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001354 // ::= de # * (unary)
John McCall5e1e89b2010-08-18 19:18:59 +00001355 // ::= ml # * (binary or unknown)
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001356 case OO_Star:
John McCall5e1e89b2010-08-18 19:18:59 +00001357 // Use binary when unknown.
Anders Carlsson8257d412009-12-22 06:36:32 +00001358 Out << (Arity == 1? "de" : "ml"); break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001359 // ::= co # ~
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001360 case OO_Tilde: Out << "co"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001361 // ::= dv # /
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001362 case OO_Slash: Out << "dv"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001363 // ::= rm # %
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001364 case OO_Percent: Out << "rm"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001365 // ::= or # |
1366 case OO_Pipe: Out << "or"; break;
1367 // ::= eo # ^
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001368 case OO_Caret: Out << "eo"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001369 // ::= aS # =
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001370 case OO_Equal: Out << "aS"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001371 // ::= pL # +=
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001372 case OO_PlusEqual: Out << "pL"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001373 // ::= mI # -=
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001374 case OO_MinusEqual: Out << "mI"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001375 // ::= mL # *=
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001376 case OO_StarEqual: Out << "mL"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001377 // ::= dV # /=
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001378 case OO_SlashEqual: Out << "dV"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001379 // ::= rM # %=
1380 case OO_PercentEqual: Out << "rM"; break;
1381 // ::= aN # &=
1382 case OO_AmpEqual: Out << "aN"; break;
1383 // ::= oR # |=
1384 case OO_PipeEqual: Out << "oR"; break;
1385 // ::= eO # ^=
1386 case OO_CaretEqual: Out << "eO"; break;
1387 // ::= ls # <<
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001388 case OO_LessLess: Out << "ls"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001389 // ::= rs # >>
1390 case OO_GreaterGreater: Out << "rs"; break;
1391 // ::= lS # <<=
1392 case OO_LessLessEqual: Out << "lS"; break;
1393 // ::= rS # >>=
1394 case OO_GreaterGreaterEqual: Out << "rS"; break;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001395 // ::= eq # ==
1396 case OO_EqualEqual: Out << "eq"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001397 // ::= ne # !=
1398 case OO_ExclaimEqual: Out << "ne"; break;
1399 // ::= lt # <
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001400 case OO_Less: Out << "lt"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001401 // ::= gt # >
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001402 case OO_Greater: Out << "gt"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001403 // ::= le # <=
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001404 case OO_LessEqual: Out << "le"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001405 // ::= ge # >=
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001406 case OO_GreaterEqual: Out << "ge"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001407 // ::= nt # !
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001408 case OO_Exclaim: Out << "nt"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001409 // ::= aa # &&
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001410 case OO_AmpAmp: Out << "aa"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001411 // ::= oo # ||
1412 case OO_PipePipe: Out << "oo"; break;
1413 // ::= pp # ++
1414 case OO_PlusPlus: Out << "pp"; break;
1415 // ::= mm # --
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001416 case OO_MinusMinus: Out << "mm"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001417 // ::= cm # ,
1418 case OO_Comma: Out << "cm"; break;
1419 // ::= pm # ->*
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001420 case OO_ArrowStar: Out << "pm"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001421 // ::= pt # ->
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001422 case OO_Arrow: Out << "pt"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001423 // ::= cl # ()
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001424 case OO_Call: Out << "cl"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001425 // ::= ix # []
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001426 case OO_Subscript: Out << "ix"; break;
Anders Carlssone170ba72009-12-14 01:45:37 +00001427
1428 // ::= qu # ?
1429 // The conditional operator can't be overloaded, but we still handle it when
1430 // mangling expressions.
1431 case OO_Conditional: Out << "qu"; break;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001432
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001433 case OO_None:
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001434 case NUM_OVERLOADED_OPERATORS:
Mike Stump1eb44332009-09-09 15:08:12 +00001435 assert(false && "Not an overloaded operator");
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001436 break;
1437 }
1438}
1439
John McCall0953e762009-09-24 19:53:00 +00001440void CXXNameMangler::mangleQualifiers(Qualifiers Quals) {
Mike Stump1eb44332009-09-09 15:08:12 +00001441 // <CV-qualifiers> ::= [r] [V] [K] # restrict (C99), volatile, const
John McCall0953e762009-09-24 19:53:00 +00001442 if (Quals.hasRestrict())
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001443 Out << 'r';
John McCall0953e762009-09-24 19:53:00 +00001444 if (Quals.hasVolatile())
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001445 Out << 'V';
John McCall0953e762009-09-24 19:53:00 +00001446 if (Quals.hasConst())
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001447 Out << 'K';
John McCall0953e762009-09-24 19:53:00 +00001448
Douglas Gregor56079f72010-06-14 23:15:08 +00001449 if (Quals.hasAddressSpace()) {
1450 // Extension:
1451 //
1452 // <type> ::= U <address-space-number>
1453 //
1454 // where <address-space-number> is a source name consisting of 'AS'
1455 // followed by the address space <number>.
1456 llvm::SmallString<64> ASString;
1457 ASString = "AS" + llvm::utostr_32(Quals.getAddressSpace());
1458 Out << 'U' << ASString.size() << ASString;
1459 }
1460
John McCall0953e762009-09-24 19:53:00 +00001461 // FIXME: For now, just drop all extension qualifiers on the floor.
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001462}
1463
Douglas Gregor0a9a6d62011-01-26 17:36:28 +00001464void CXXNameMangler::mangleRefQualifier(RefQualifierKind RefQualifier) {
1465 // <ref-qualifier> ::= R # lvalue reference
1466 // ::= O # rvalue-reference
1467 // Proposal to Itanium C++ ABI list on 1/26/11
1468 switch (RefQualifier) {
1469 case RQ_None:
1470 break;
1471
1472 case RQ_LValue:
1473 Out << 'R';
1474 break;
1475
1476 case RQ_RValue:
1477 Out << 'O';
1478 break;
1479 }
1480}
1481
Anders Carlsson7b06f6c2009-12-10 03:14:39 +00001482void CXXNameMangler::mangleObjCMethodName(const ObjCMethodDecl *MD) {
Rafael Espindolaf0be9792011-02-11 02:52:17 +00001483 Context.mangleObjCMethodName(MD, Out);
Anders Carlsson7b06f6c2009-12-10 03:14:39 +00001484}
1485
John McCallb47f7482011-01-26 20:05:40 +00001486void CXXNameMangler::mangleType(QualType nonCanon) {
Anders Carlsson4843e582009-03-10 17:07:44 +00001487 // Only operate on the canonical type!
John McCallb47f7482011-01-26 20:05:40 +00001488 QualType canon = nonCanon.getCanonicalType();
Anders Carlsson4843e582009-03-10 17:07:44 +00001489
John McCallb47f7482011-01-26 20:05:40 +00001490 SplitQualType split = canon.split();
1491 Qualifiers quals = split.second;
1492 const Type *ty = split.first;
1493
1494 bool isSubstitutable = quals || !isa<BuiltinType>(ty);
1495 if (isSubstitutable && mangleSubstitution(canon))
Anders Carlsson76967372009-09-17 00:43:46 +00001496 return;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001497
John McCallb47f7482011-01-26 20:05:40 +00001498 // If we're mangling a qualified array type, push the qualifiers to
1499 // the element type.
1500 if (quals && isa<ArrayType>(ty)) {
1501 ty = Context.getASTContext().getAsArrayType(canon);
1502 quals = Qualifiers();
1503
1504 // Note that we don't update canon: we want to add the
1505 // substitution at the canonical type.
1506 }
1507
1508 if (quals) {
1509 mangleQualifiers(quals);
John McCall0953e762009-09-24 19:53:00 +00001510 // Recurse: even if the qualified type isn't yet substitutable,
1511 // the unqualified type might be.
John McCallb47f7482011-01-26 20:05:40 +00001512 mangleType(QualType(ty, 0));
Anders Carlsson76967372009-09-17 00:43:46 +00001513 } else {
John McCallb47f7482011-01-26 20:05:40 +00001514 switch (ty->getTypeClass()) {
John McCallefe6aee2009-09-05 07:56:18 +00001515#define ABSTRACT_TYPE(CLASS, PARENT)
1516#define NON_CANONICAL_TYPE(CLASS, PARENT) \
Anders Carlsson76967372009-09-17 00:43:46 +00001517 case Type::CLASS: \
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +00001518 llvm_unreachable("can't mangle non-canonical type " #CLASS "Type"); \
Anders Carlsson76967372009-09-17 00:43:46 +00001519 return;
John McCallefe6aee2009-09-05 07:56:18 +00001520#define TYPE(CLASS, PARENT) \
Anders Carlsson76967372009-09-17 00:43:46 +00001521 case Type::CLASS: \
John McCallb47f7482011-01-26 20:05:40 +00001522 mangleType(static_cast<const CLASS##Type*>(ty)); \
Anders Carlsson76967372009-09-17 00:43:46 +00001523 break;
John McCallefe6aee2009-09-05 07:56:18 +00001524#include "clang/AST/TypeNodes.def"
Anders Carlsson76967372009-09-17 00:43:46 +00001525 }
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001526 }
Anders Carlsson76967372009-09-17 00:43:46 +00001527
1528 // Add the substitution.
John McCallb47f7482011-01-26 20:05:40 +00001529 if (isSubstitutable)
1530 addSubstitution(canon);
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001531}
1532
Douglas Gregor1b12a3b2010-05-26 05:11:13 +00001533void CXXNameMangler::mangleNameOrStandardSubstitution(const NamedDecl *ND) {
1534 if (!mangleStandardSubstitution(ND))
1535 mangleName(ND);
1536}
1537
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001538void CXXNameMangler::mangleType(const BuiltinType *T) {
John McCallefe6aee2009-09-05 07:56:18 +00001539 // <type> ::= <builtin-type>
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001540 // <builtin-type> ::= v # void
1541 // ::= w # wchar_t
1542 // ::= b # bool
1543 // ::= c # char
1544 // ::= a # signed char
1545 // ::= h # unsigned char
1546 // ::= s # short
1547 // ::= t # unsigned short
1548 // ::= i # int
1549 // ::= j # unsigned int
1550 // ::= l # long
1551 // ::= m # unsigned long
1552 // ::= x # long long, __int64
1553 // ::= y # unsigned long long, __int64
1554 // ::= n # __int128
1555 // UNSUPPORTED: ::= o # unsigned __int128
1556 // ::= f # float
1557 // ::= d # double
1558 // ::= e # long double, __float80
1559 // UNSUPPORTED: ::= g # __float128
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001560 // UNSUPPORTED: ::= Dd # IEEE 754r decimal floating point (64 bits)
1561 // UNSUPPORTED: ::= De # IEEE 754r decimal floating point (128 bits)
1562 // UNSUPPORTED: ::= Df # IEEE 754r decimal floating point (32 bits)
1563 // UNSUPPORTED: ::= Dh # IEEE 754r half-precision floating point (16 bits)
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001564 // ::= Di # char32_t
1565 // ::= Ds # char16_t
Anders Carlssone2923682010-11-04 04:31:32 +00001566 // ::= Dn # std::nullptr_t (i.e., decltype(nullptr))
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001567 // ::= u <source-name> # vendor extended type
1568 switch (T->getKind()) {
1569 case BuiltinType::Void: Out << 'v'; break;
1570 case BuiltinType::Bool: Out << 'b'; break;
1571 case BuiltinType::Char_U: case BuiltinType::Char_S: Out << 'c'; break;
1572 case BuiltinType::UChar: Out << 'h'; break;
1573 case BuiltinType::UShort: Out << 't'; break;
1574 case BuiltinType::UInt: Out << 'j'; break;
1575 case BuiltinType::ULong: Out << 'm'; break;
1576 case BuiltinType::ULongLong: Out << 'y'; break;
Chris Lattner2df9ced2009-04-30 02:43:43 +00001577 case BuiltinType::UInt128: Out << 'o'; break;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001578 case BuiltinType::SChar: Out << 'a'; break;
Chris Lattner3f59c972010-12-25 23:25:43 +00001579 case BuiltinType::WChar_S:
1580 case BuiltinType::WChar_U: Out << 'w'; break;
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001581 case BuiltinType::Char16: Out << "Ds"; break;
1582 case BuiltinType::Char32: Out << "Di"; break;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001583 case BuiltinType::Short: Out << 's'; break;
1584 case BuiltinType::Int: Out << 'i'; break;
1585 case BuiltinType::Long: Out << 'l'; break;
1586 case BuiltinType::LongLong: Out << 'x'; break;
Chris Lattner2df9ced2009-04-30 02:43:43 +00001587 case BuiltinType::Int128: Out << 'n'; break;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001588 case BuiltinType::Float: Out << 'f'; break;
1589 case BuiltinType::Double: Out << 'd'; break;
1590 case BuiltinType::LongDouble: Out << 'e'; break;
Anders Carlssone2923682010-11-04 04:31:32 +00001591 case BuiltinType::NullPtr: Out << "Dn"; break;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001592
1593 case BuiltinType::Overload:
1594 case BuiltinType::Dependent:
John McCall864c0412011-04-26 20:42:42 +00001595 case BuiltinType::BoundMember:
John McCall1de4d4e2011-04-07 08:22:57 +00001596 case BuiltinType::UnknownAny:
John McCallfb44de92011-05-01 22:35:37 +00001597 llvm_unreachable("mangling a placeholder type");
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001598 break;
Steve Naroff9533a7f2009-07-22 17:14:51 +00001599 case BuiltinType::ObjCId: Out << "11objc_object"; break;
1600 case BuiltinType::ObjCClass: Out << "10objc_class"; break;
Fariborz Jahanian13dcd002009-11-21 19:53:08 +00001601 case BuiltinType::ObjCSel: Out << "13objc_selector"; break;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001602 }
1603}
1604
John McCallefe6aee2009-09-05 07:56:18 +00001605// <type> ::= <function-type>
1606// <function-type> ::= F [Y] <bare-function-type> E
1607void CXXNameMangler::mangleType(const FunctionProtoType *T) {
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001608 Out << 'F';
Mike Stumpf5408fe2009-05-16 07:57:57 +00001609 // FIXME: We don't have enough information in the AST to produce the 'Y'
1610 // encoding for extern "C" function types.
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001611 mangleBareFunctionType(T, /*MangleReturnType=*/true);
1612 Out << 'E';
1613}
John McCallefe6aee2009-09-05 07:56:18 +00001614void CXXNameMangler::mangleType(const FunctionNoProtoType *T) {
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +00001615 llvm_unreachable("Can't mangle K&R function prototypes");
John McCallefe6aee2009-09-05 07:56:18 +00001616}
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001617void CXXNameMangler::mangleBareFunctionType(const FunctionType *T,
1618 bool MangleReturnType) {
John McCallefe6aee2009-09-05 07:56:18 +00001619 // We should never be mangling something without a prototype.
1620 const FunctionProtoType *Proto = cast<FunctionProtoType>(T);
1621
John McCallfb44de92011-05-01 22:35:37 +00001622 // Record that we're in a function type. See mangleFunctionParam
1623 // for details on what we're trying to achieve here.
1624 FunctionTypeDepthState saved = FunctionTypeDepth.push();
1625
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001626 // <bare-function-type> ::= <signature type>+
John McCallfb44de92011-05-01 22:35:37 +00001627 if (MangleReturnType) {
1628 FunctionTypeDepth.enterResultType();
John McCallefe6aee2009-09-05 07:56:18 +00001629 mangleType(Proto->getResultType());
John McCallfb44de92011-05-01 22:35:37 +00001630 FunctionTypeDepth.leaveResultType();
1631 }
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001632
Anders Carlsson93296682010-06-02 04:40:13 +00001633 if (Proto->getNumArgs() == 0 && !Proto->isVariadic()) {
Eli Friedmana7e68452010-08-22 01:00:03 +00001634 // <builtin-type> ::= v # void
Anders Carlssonc6c91bc2009-04-01 00:15:23 +00001635 Out << 'v';
John McCallfb44de92011-05-01 22:35:37 +00001636
1637 FunctionTypeDepth.pop(saved);
Anders Carlssonc6c91bc2009-04-01 00:15:23 +00001638 return;
1639 }
Mike Stump1eb44332009-09-09 15:08:12 +00001640
Douglas Gregor72564e72009-02-26 23:50:07 +00001641 for (FunctionProtoType::arg_type_iterator Arg = Proto->arg_type_begin(),
Mike Stump1eb44332009-09-09 15:08:12 +00001642 ArgEnd = Proto->arg_type_end();
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001643 Arg != ArgEnd; ++Arg)
1644 mangleType(*Arg);
Douglas Gregor219cc612009-02-13 01:28:03 +00001645
John McCallfb44de92011-05-01 22:35:37 +00001646 FunctionTypeDepth.pop(saved);
1647
Douglas Gregor219cc612009-02-13 01:28:03 +00001648 // <builtin-type> ::= z # ellipsis
1649 if (Proto->isVariadic())
1650 Out << 'z';
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001651}
1652
John McCallefe6aee2009-09-05 07:56:18 +00001653// <type> ::= <class-enum-type>
Mike Stump1eb44332009-09-09 15:08:12 +00001654// <class-enum-type> ::= <name>
John McCalled976492009-12-04 22:46:56 +00001655void CXXNameMangler::mangleType(const UnresolvedUsingType *T) {
1656 mangleName(T->getDecl());
1657}
1658
1659// <type> ::= <class-enum-type>
1660// <class-enum-type> ::= <name>
John McCallefe6aee2009-09-05 07:56:18 +00001661void CXXNameMangler::mangleType(const EnumType *T) {
1662 mangleType(static_cast<const TagType*>(T));
1663}
1664void CXXNameMangler::mangleType(const RecordType *T) {
1665 mangleType(static_cast<const TagType*>(T));
1666}
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001667void CXXNameMangler::mangleType(const TagType *T) {
Eli Friedmanecb7e932009-12-11 18:00:57 +00001668 mangleName(T->getDecl());
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001669}
1670
John McCallefe6aee2009-09-05 07:56:18 +00001671// <type> ::= <array-type>
1672// <array-type> ::= A <positive dimension number> _ <element type>
1673// ::= A [<dimension expression>] _ <element type>
1674void CXXNameMangler::mangleType(const ConstantArrayType *T) {
1675 Out << 'A' << T->getSize() << '_';
1676 mangleType(T->getElementType());
1677}
1678void CXXNameMangler::mangleType(const VariableArrayType *T) {
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001679 Out << 'A';
Fariborz Jahanian7281d1f2010-11-02 16:54:00 +00001680 // decayed vla types (size 0) will just be skipped.
1681 if (T->getSizeExpr())
1682 mangleExpression(T->getSizeExpr());
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001683 Out << '_';
1684 mangleType(T->getElementType());
1685}
John McCallefe6aee2009-09-05 07:56:18 +00001686void CXXNameMangler::mangleType(const DependentSizedArrayType *T) {
1687 Out << 'A';
1688 mangleExpression(T->getSizeExpr());
1689 Out << '_';
1690 mangleType(T->getElementType());
1691}
1692void CXXNameMangler::mangleType(const IncompleteArrayType *T) {
Nick Lewycky271b6652010-09-05 03:40:33 +00001693 Out << "A_";
John McCallefe6aee2009-09-05 07:56:18 +00001694 mangleType(T->getElementType());
1695}
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001696
John McCallefe6aee2009-09-05 07:56:18 +00001697// <type> ::= <pointer-to-member-type>
1698// <pointer-to-member-type> ::= M <class type> <member type>
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001699void CXXNameMangler::mangleType(const MemberPointerType *T) {
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001700 Out << 'M';
1701 mangleType(QualType(T->getClass(), 0));
Anders Carlsson0e650012009-05-17 17:41:20 +00001702 QualType PointeeType = T->getPointeeType();
1703 if (const FunctionProtoType *FPT = dyn_cast<FunctionProtoType>(PointeeType)) {
John McCall0953e762009-09-24 19:53:00 +00001704 mangleQualifiers(Qualifiers::fromCVRMask(FPT->getTypeQuals()));
Douglas Gregor0a9a6d62011-01-26 17:36:28 +00001705 mangleRefQualifier(FPT->getRefQualifier());
Anders Carlsson0e650012009-05-17 17:41:20 +00001706 mangleType(FPT);
Anders Carlsson9d85b722010-06-02 04:29:50 +00001707
1708 // Itanium C++ ABI 5.1.8:
1709 //
1710 // The type of a non-static member function is considered to be different,
1711 // for the purposes of substitution, from the type of a namespace-scope or
1712 // static member function whose type appears similar. The types of two
1713 // non-static member functions are considered to be different, for the
1714 // purposes of substitution, if the functions are members of different
1715 // classes. In other words, for the purposes of substitution, the class of
1716 // which the function is a member is considered part of the type of
1717 // function.
1718
1719 // We increment the SeqID here to emulate adding an entry to the
1720 // substitution table. We can't actually add it because we don't want this
1721 // particular function type to be substituted.
1722 ++SeqID;
Mike Stump1eb44332009-09-09 15:08:12 +00001723 } else
Anders Carlsson0e650012009-05-17 17:41:20 +00001724 mangleType(PointeeType);
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001725}
1726
John McCallefe6aee2009-09-05 07:56:18 +00001727// <type> ::= <template-param>
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001728void CXXNameMangler::mangleType(const TemplateTypeParmType *T) {
Anders Carlsson0ccdf8d2009-09-27 00:38:53 +00001729 mangleTemplateParameter(T->getIndex());
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001730}
1731
Douglas Gregorc3069d62011-01-14 02:55:32 +00001732// <type> ::= <template-param>
1733void CXXNameMangler::mangleType(const SubstTemplateTypeParmPackType *T) {
1734 mangleTemplateParameter(T->getReplacedParameter()->getIndex());
1735}
1736
John McCallefe6aee2009-09-05 07:56:18 +00001737// <type> ::= P <type> # pointer-to
1738void CXXNameMangler::mangleType(const PointerType *T) {
1739 Out << 'P';
1740 mangleType(T->getPointeeType());
1741}
1742void CXXNameMangler::mangleType(const ObjCObjectPointerType *T) {
1743 Out << 'P';
1744 mangleType(T->getPointeeType());
1745}
1746
1747// <type> ::= R <type> # reference-to
1748void CXXNameMangler::mangleType(const LValueReferenceType *T) {
1749 Out << 'R';
1750 mangleType(T->getPointeeType());
1751}
1752
1753// <type> ::= O <type> # rvalue reference-to (C++0x)
1754void CXXNameMangler::mangleType(const RValueReferenceType *T) {
1755 Out << 'O';
1756 mangleType(T->getPointeeType());
1757}
1758
1759// <type> ::= C <type> # complex pair (C 2000)
1760void CXXNameMangler::mangleType(const ComplexType *T) {
1761 Out << 'C';
1762 mangleType(T->getElementType());
1763}
1764
Bob Wilsonc7df92d2010-11-12 17:24:43 +00001765// ARM's ABI for Neon vector types specifies that they should be mangled as
Bob Wilson57147a82010-11-16 00:32:18 +00001766// if they are structs (to match ARM's initial implementation). The
1767// vector type must be one of the special types predefined by ARM.
1768void CXXNameMangler::mangleNeonVectorType(const VectorType *T) {
Bob Wilsonc7df92d2010-11-12 17:24:43 +00001769 QualType EltType = T->getElementType();
Bob Wilson57147a82010-11-16 00:32:18 +00001770 assert(EltType->isBuiltinType() && "Neon vector element not a BuiltinType");
Bob Wilsonc7df92d2010-11-12 17:24:43 +00001771 const char *EltName = 0;
Bob Wilson491328c2010-11-12 17:24:46 +00001772 if (T->getVectorKind() == VectorType::NeonPolyVector) {
1773 switch (cast<BuiltinType>(EltType)->getKind()) {
Bob Wilson4cfaa5d2010-11-12 17:24:49 +00001774 case BuiltinType::SChar: EltName = "poly8_t"; break;
1775 case BuiltinType::Short: EltName = "poly16_t"; break;
Bob Wilson57147a82010-11-16 00:32:18 +00001776 default: llvm_unreachable("unexpected Neon polynomial vector element type");
Bob Wilson491328c2010-11-12 17:24:46 +00001777 }
1778 } else {
1779 switch (cast<BuiltinType>(EltType)->getKind()) {
Bob Wilson4cfaa5d2010-11-12 17:24:49 +00001780 case BuiltinType::SChar: EltName = "int8_t"; break;
1781 case BuiltinType::UChar: EltName = "uint8_t"; break;
1782 case BuiltinType::Short: EltName = "int16_t"; break;
1783 case BuiltinType::UShort: EltName = "uint16_t"; break;
1784 case BuiltinType::Int: EltName = "int32_t"; break;
1785 case BuiltinType::UInt: EltName = "uint32_t"; break;
1786 case BuiltinType::LongLong: EltName = "int64_t"; break;
1787 case BuiltinType::ULongLong: EltName = "uint64_t"; break;
1788 case BuiltinType::Float: EltName = "float32_t"; break;
Bob Wilson57147a82010-11-16 00:32:18 +00001789 default: llvm_unreachable("unexpected Neon vector element type");
Bob Wilson491328c2010-11-12 17:24:46 +00001790 }
Bob Wilsonc7df92d2010-11-12 17:24:43 +00001791 }
1792 const char *BaseName = 0;
Bob Wilson4cfaa5d2010-11-12 17:24:49 +00001793 unsigned BitSize = (T->getNumElements() *
Bob Wilson3a723022010-11-16 00:32:12 +00001794 getASTContext().getTypeSize(EltType));
Bob Wilsonc7df92d2010-11-12 17:24:43 +00001795 if (BitSize == 64)
1796 BaseName = "__simd64_";
Bob Wilson57147a82010-11-16 00:32:18 +00001797 else {
1798 assert(BitSize == 128 && "Neon vector type not 64 or 128 bits");
Bob Wilsonc7df92d2010-11-12 17:24:43 +00001799 BaseName = "__simd128_";
Bob Wilson57147a82010-11-16 00:32:18 +00001800 }
Bob Wilsonc7df92d2010-11-12 17:24:43 +00001801 Out << strlen(BaseName) + strlen(EltName);
1802 Out << BaseName << EltName;
Bob Wilsonc7df92d2010-11-12 17:24:43 +00001803}
1804
John McCallefe6aee2009-09-05 07:56:18 +00001805// GNU extension: vector types
Chris Lattner788b0fd2010-06-23 06:00:24 +00001806// <type> ::= <vector-type>
1807// <vector-type> ::= Dv <positive dimension number> _
1808// <extended element type>
1809// ::= Dv [<dimension expression>] _ <element type>
1810// <extended element type> ::= <element type>
1811// ::= p # AltiVec vector pixel
John McCallefe6aee2009-09-05 07:56:18 +00001812void CXXNameMangler::mangleType(const VectorType *T) {
Bob Wilson491328c2010-11-12 17:24:46 +00001813 if ((T->getVectorKind() == VectorType::NeonVector ||
Bob Wilson57147a82010-11-16 00:32:18 +00001814 T->getVectorKind() == VectorType::NeonPolyVector)) {
1815 mangleNeonVectorType(T);
Bob Wilsonc7df92d2010-11-12 17:24:43 +00001816 return;
Bob Wilson57147a82010-11-16 00:32:18 +00001817 }
Nick Lewycky0e5f0672010-03-26 07:18:04 +00001818 Out << "Dv" << T->getNumElements() << '_';
Bob Wilsone86d78c2010-11-10 21:56:12 +00001819 if (T->getVectorKind() == VectorType::AltiVecPixel)
Chris Lattner788b0fd2010-06-23 06:00:24 +00001820 Out << 'p';
Bob Wilsone86d78c2010-11-10 21:56:12 +00001821 else if (T->getVectorKind() == VectorType::AltiVecBool)
Chris Lattner788b0fd2010-06-23 06:00:24 +00001822 Out << 'b';
1823 else
1824 mangleType(T->getElementType());
John McCallefe6aee2009-09-05 07:56:18 +00001825}
1826void CXXNameMangler::mangleType(const ExtVectorType *T) {
1827 mangleType(static_cast<const VectorType*>(T));
1828}
1829void CXXNameMangler::mangleType(const DependentSizedExtVectorType *T) {
Nick Lewycky0e5f0672010-03-26 07:18:04 +00001830 Out << "Dv";
1831 mangleExpression(T->getSizeExpr());
1832 Out << '_';
John McCallefe6aee2009-09-05 07:56:18 +00001833 mangleType(T->getElementType());
1834}
1835
Douglas Gregor7536dd52010-12-20 02:24:11 +00001836void CXXNameMangler::mangleType(const PackExpansionType *T) {
Douglas Gregor4fc48662011-01-13 16:39:34 +00001837 // <type> ::= Dp <type> # pack expansion (C++0x)
Douglas Gregor255c2692011-01-13 17:44:36 +00001838 Out << "Dp";
Douglas Gregor7536dd52010-12-20 02:24:11 +00001839 mangleType(T->getPattern());
1840}
1841
Anders Carlssona40c5e42009-03-07 22:03:21 +00001842void CXXNameMangler::mangleType(const ObjCInterfaceType *T) {
1843 mangleSourceName(T->getDecl()->getIdentifier());
1844}
1845
John McCallc12c5bb2010-05-15 11:32:37 +00001846void CXXNameMangler::mangleType(const ObjCObjectType *T) {
John McCallc00c1f62010-05-15 17:06:29 +00001847 // We don't allow overloading by different protocol qualification,
1848 // so mangling them isn't necessary.
John McCallc12c5bb2010-05-15 11:32:37 +00001849 mangleType(T->getBaseType());
1850}
1851
John McCallefe6aee2009-09-05 07:56:18 +00001852void CXXNameMangler::mangleType(const BlockPointerType *T) {
Anders Carlssonf28c6872009-12-23 22:31:44 +00001853 Out << "U13block_pointer";
1854 mangleType(T->getPointeeType());
John McCallefe6aee2009-09-05 07:56:18 +00001855}
1856
John McCall31f17ec2010-04-27 00:57:59 +00001857void CXXNameMangler::mangleType(const InjectedClassNameType *T) {
1858 // Mangle injected class name types as if the user had written the
1859 // specialization out fully. It may not actually be possible to see
1860 // this mangling, though.
1861 mangleType(T->getInjectedSpecializationType());
1862}
1863
John McCallefe6aee2009-09-05 07:56:18 +00001864void CXXNameMangler::mangleType(const TemplateSpecializationType *T) {
Douglas Gregor1e9268e2010-04-28 05:58:56 +00001865 if (TemplateDecl *TD = T->getTemplateName().getAsTemplateDecl()) {
1866 mangleName(TD, T->getArgs(), T->getNumArgs());
1867 } else {
1868 if (mangleSubstitution(QualType(T, 0)))
1869 return;
Sean Huntc3021132010-05-05 15:23:54 +00001870
Douglas Gregor1e9268e2010-04-28 05:58:56 +00001871 mangleTemplatePrefix(T->getTemplateName());
Sean Huntc3021132010-05-05 15:23:54 +00001872
Douglas Gregor1e9268e2010-04-28 05:58:56 +00001873 // FIXME: GCC does not appear to mangle the template arguments when
1874 // the template in question is a dependent template name. Should we
1875 // emulate that badness?
1876 mangleTemplateArgs(T->getTemplateName(), T->getArgs(), T->getNumArgs());
1877 addSubstitution(QualType(T, 0));
1878 }
John McCallefe6aee2009-09-05 07:56:18 +00001879}
1880
Douglas Gregor4714c122010-03-31 17:34:00 +00001881void CXXNameMangler::mangleType(const DependentNameType *T) {
Anders Carlssonae352482009-09-26 02:26:02 +00001882 // Typename types are always nested
1883 Out << 'N';
John McCalla0ce15c2011-04-24 08:23:24 +00001884 manglePrefix(T->getQualifier());
John McCall33500952010-06-11 00:33:02 +00001885 mangleSourceName(T->getIdentifier());
1886 Out << 'E';
1887}
John McCall6ab30e02010-06-09 07:26:17 +00001888
John McCall33500952010-06-11 00:33:02 +00001889void CXXNameMangler::mangleType(const DependentTemplateSpecializationType *T) {
Douglas Gregoraa2187d2011-02-28 00:04:36 +00001890 // Dependently-scoped template types are nested if they have a prefix.
John McCall33500952010-06-11 00:33:02 +00001891 Out << 'N';
1892
1893 // TODO: avoid making this TemplateName.
1894 TemplateName Prefix =
1895 getASTContext().getDependentTemplateName(T->getQualifier(),
1896 T->getIdentifier());
1897 mangleTemplatePrefix(Prefix);
1898
1899 // FIXME: GCC does not appear to mangle the template arguments when
1900 // the template in question is a dependent template name. Should we
1901 // emulate that badness?
1902 mangleTemplateArgs(Prefix, T->getArgs(), T->getNumArgs());
Anders Carlssonae352482009-09-26 02:26:02 +00001903 Out << 'E';
John McCallefe6aee2009-09-05 07:56:18 +00001904}
1905
John McCallad5e7382010-03-01 23:49:17 +00001906void CXXNameMangler::mangleType(const TypeOfType *T) {
1907 // FIXME: this is pretty unsatisfactory, but there isn't an obvious
1908 // "extension with parameters" mangling.
1909 Out << "u6typeof";
1910}
1911
1912void CXXNameMangler::mangleType(const TypeOfExprType *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 DecltypeType *T) {
1919 Expr *E = T->getUnderlyingExpr();
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001920
John McCallad5e7382010-03-01 23:49:17 +00001921 // type ::= Dt <expression> E # decltype of an id-expression
1922 // # or class member access
1923 // ::= DT <expression> E # decltype of an expression
1924
1925 // This purports to be an exhaustive list of id-expressions and
1926 // class member accesses. Note that we do not ignore parentheses;
1927 // parentheses change the semantics of decltype for these
1928 // expressions (and cause the mangler to use the other form).
1929 if (isa<DeclRefExpr>(E) ||
1930 isa<MemberExpr>(E) ||
1931 isa<UnresolvedLookupExpr>(E) ||
1932 isa<DependentScopeDeclRefExpr>(E) ||
1933 isa<CXXDependentScopeMemberExpr>(E) ||
1934 isa<UnresolvedMemberExpr>(E))
1935 Out << "Dt";
1936 else
1937 Out << "DT";
1938 mangleExpression(E);
1939 Out << 'E';
1940}
1941
Sean Huntca63c202011-05-24 22:41:36 +00001942void CXXNameMangler::mangleType(const UnaryTransformType *T) {
1943 // If this is dependent, we need to record that. If not, we simply
1944 // mangle it as the underlying type since they are equivalent.
1945 if (T->isDependentType()) {
1946 Out << 'U';
1947
1948 switch (T->getUTTKind()) {
1949 case UnaryTransformType::EnumUnderlyingType:
1950 Out << "3eut";
1951 break;
1952 }
1953 }
1954
1955 mangleType(T->getUnderlyingType());
1956}
1957
Richard Smith34b41d92011-02-20 03:19:35 +00001958void CXXNameMangler::mangleType(const AutoType *T) {
1959 QualType D = T->getDeducedType();
Richard Smith967ecd32011-02-21 20:10:02 +00001960 // <builtin-type> ::= Da # dependent auto
1961 if (D.isNull())
1962 Out << "Da";
1963 else
1964 mangleType(D);
Richard Smith34b41d92011-02-20 03:19:35 +00001965}
1966
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001967void CXXNameMangler::mangleIntegerLiteral(QualType T,
Anders Carlssone170ba72009-12-14 01:45:37 +00001968 const llvm::APSInt &Value) {
1969 // <expr-primary> ::= L <type> <value number> E # integer literal
1970 Out << 'L';
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001971
Anders Carlssone170ba72009-12-14 01:45:37 +00001972 mangleType(T);
1973 if (T->isBooleanType()) {
1974 // Boolean values are encoded as 0/1.
1975 Out << (Value.getBoolValue() ? '1' : '0');
1976 } else {
John McCall0512e482010-07-14 04:20:34 +00001977 mangleNumber(Value);
Anders Carlssone170ba72009-12-14 01:45:37 +00001978 }
1979 Out << 'E';
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001980
Anders Carlssone170ba72009-12-14 01:45:37 +00001981}
1982
John McCall2f27bf82010-02-04 02:56:29 +00001983/// Mangles a member expression. Implicit accesses are not handled,
1984/// but that should be okay, because you shouldn't be able to
1985/// make an implicit access in a function template declaration.
John McCalla0ce15c2011-04-24 08:23:24 +00001986void CXXNameMangler::mangleMemberExpr(const Expr *base,
1987 bool isArrow,
1988 NestedNameSpecifier *qualifier,
1989 NamedDecl *firstQualifierLookup,
1990 DeclarationName member,
1991 unsigned arity) {
1992 // <expression> ::= dt <expression> <unresolved-name>
1993 // ::= pt <expression> <unresolved-name>
1994 Out << (isArrow ? "pt" : "dt");
1995 mangleExpression(base);
1996 mangleUnresolvedName(qualifier, firstQualifierLookup, member, arity);
John McCall2f27bf82010-02-04 02:56:29 +00001997}
1998
John McCall5a7e6f72011-04-28 02:52:03 +00001999/// Look at the callee of the given call expression and determine if
2000/// it's a parenthesized id-expression which would have triggered ADL
2001/// otherwise.
2002static bool isParenthesizedADLCallee(const CallExpr *call) {
2003 const Expr *callee = call->getCallee();
2004 const Expr *fn = callee->IgnoreParens();
2005
2006 // Must be parenthesized. IgnoreParens() skips __extension__ nodes,
2007 // too, but for those to appear in the callee, it would have to be
2008 // parenthesized.
2009 if (callee == fn) return false;
2010
2011 // Must be an unresolved lookup.
2012 const UnresolvedLookupExpr *lookup = dyn_cast<UnresolvedLookupExpr>(fn);
2013 if (!lookup) return false;
2014
2015 assert(!lookup->requiresADL());
2016
2017 // Must be an unqualified lookup.
2018 if (lookup->getQualifier()) return false;
2019
2020 // Must not have found a class member. Note that if one is a class
2021 // member, they're all class members.
2022 if (lookup->getNumDecls() > 0 &&
2023 (*lookup->decls_begin())->isCXXClassMember())
2024 return false;
2025
2026 // Otherwise, ADL would have been triggered.
2027 return true;
2028}
2029
John McCall5e1e89b2010-08-18 19:18:59 +00002030void CXXNameMangler::mangleExpression(const Expr *E, unsigned Arity) {
Anders Carlssond553f8c2009-09-21 01:21:10 +00002031 // <expression> ::= <unary operator-name> <expression>
John McCall09cc1412010-02-03 00:55:45 +00002032 // ::= <binary operator-name> <expression> <expression>
2033 // ::= <trinary operator-name> <expression> <expression> <expression>
Anders Carlssond553f8c2009-09-21 01:21:10 +00002034 // ::= cv <type> expression # conversion with one argument
2035 // ::= cv <type> _ <expression>* E # conversion with a different number of arguments
Eli Friedmana7e68452010-08-22 01:00:03 +00002036 // ::= st <type> # sizeof (a type)
Anders Carlssond553f8c2009-09-21 01:21:10 +00002037 // ::= at <type> # alignof (a type)
2038 // ::= <template-param>
2039 // ::= <function-param>
2040 // ::= sr <type> <unqualified-name> # dependent name
2041 // ::= sr <type> <unqualified-name> <template-args> # dependent template-id
2042 // ::= sZ <template-param> # size of a parameter pack
Douglas Gregor4fc48662011-01-13 16:39:34 +00002043 // ::= sZ <function-param> # size of a function parameter pack
John McCall09cc1412010-02-03 00:55:45 +00002044 // ::= <expr-primary>
John McCall1dd73832010-02-04 01:42:13 +00002045 // <expr-primary> ::= L <type> <value number> E # integer literal
2046 // ::= L <type <value float> E # floating literal
2047 // ::= L <mangled-name> E # external name
Anders Carlssond553f8c2009-09-21 01:21:10 +00002048 switch (E->getStmtClass()) {
John McCall6ae1f352010-04-09 22:26:14 +00002049 case Expr::NoStmtClass:
John McCall63c00d72011-02-09 08:16:59 +00002050#define ABSTRACT_STMT(Type)
John McCall6ae1f352010-04-09 22:26:14 +00002051#define EXPR(Type, Base)
2052#define STMT(Type, Base) \
2053 case Expr::Type##Class:
Sean Hunt4bfe1962010-05-05 15:24:00 +00002054#include "clang/AST/StmtNodes.inc"
John McCall0512e482010-07-14 04:20:34 +00002055 // fallthrough
2056
2057 // These all can only appear in local or variable-initialization
2058 // contexts and so should never appear in a mangling.
2059 case Expr::AddrLabelExprClass:
2060 case Expr::BlockDeclRefExprClass:
2061 case Expr::CXXThisExprClass:
2062 case Expr::DesignatedInitExprClass:
2063 case Expr::ImplicitValueInitExprClass:
2064 case Expr::InitListExprClass:
2065 case Expr::ParenListExprClass:
2066 case Expr::CXXScalarValueInitExprClass:
John McCall09cc1412010-02-03 00:55:45 +00002067 llvm_unreachable("unexpected statement kind");
2068 break;
2069
John McCall0512e482010-07-14 04:20:34 +00002070 // FIXME: invent manglings for all these.
2071 case Expr::BlockExprClass:
2072 case Expr::CXXPseudoDestructorExprClass:
2073 case Expr::ChooseExprClass:
2074 case Expr::CompoundLiteralExprClass:
2075 case Expr::ExtVectorElementExprClass:
Peter Collingbournef111d932011-04-15 00:35:48 +00002076 case Expr::GenericSelectionExprClass:
John McCall0512e482010-07-14 04:20:34 +00002077 case Expr::ObjCEncodeExprClass:
John McCall0512e482010-07-14 04:20:34 +00002078 case Expr::ObjCIsaExprClass:
2079 case Expr::ObjCIvarRefExprClass:
2080 case Expr::ObjCMessageExprClass:
2081 case Expr::ObjCPropertyRefExprClass:
2082 case Expr::ObjCProtocolExprClass:
2083 case Expr::ObjCSelectorExprClass:
2084 case Expr::ObjCStringLiteralClass:
John McCall0512e482010-07-14 04:20:34 +00002085 case Expr::OffsetOfExprClass:
2086 case Expr::PredefinedExprClass:
2087 case Expr::ShuffleVectorExprClass:
2088 case Expr::StmtExprClass:
John McCall0512e482010-07-14 04:20:34 +00002089 case Expr::UnaryTypeTraitExprClass:
Francois Pichet6ad6f282010-12-07 00:08:36 +00002090 case Expr::BinaryTypeTraitExprClass:
John Wiegley21ff2e52011-04-28 00:16:57 +00002091 case Expr::ArrayTypeTraitExprClass:
John Wiegley55262202011-04-25 06:54:41 +00002092 case Expr::ExpressionTraitExprClass:
Francois Pichet9be88402010-09-08 23:47:05 +00002093 case Expr::VAArgExprClass:
Sebastian Redl2e156222010-09-10 20:55:43 +00002094 case Expr::CXXUuidofExprClass:
Peter Collingbournee08ce652011-02-09 21:07:24 +00002095 case Expr::CXXNoexceptExprClass:
2096 case Expr::CUDAKernelCallExprClass: {
John McCall6ae1f352010-04-09 22:26:14 +00002097 // As bad as this diagnostic is, it's better than crashing.
2098 Diagnostic &Diags = Context.getDiags();
2099 unsigned DiagID = Diags.getCustomDiagID(Diagnostic::Error,
2100 "cannot yet mangle expression type %0");
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +00002101 Diags.Report(E->getExprLoc(), DiagID)
John McCall739bf092010-04-10 09:39:25 +00002102 << E->getStmtClassName() << E->getSourceRange();
John McCall6ae1f352010-04-09 22:26:14 +00002103 break;
2104 }
2105
John McCall56ca35d2011-02-17 10:25:35 +00002106 // Even gcc-4.5 doesn't mangle this.
2107 case Expr::BinaryConditionalOperatorClass: {
2108 Diagnostic &Diags = Context.getDiags();
2109 unsigned DiagID =
2110 Diags.getCustomDiagID(Diagnostic::Error,
2111 "?: operator with omitted middle operand cannot be mangled");
2112 Diags.Report(E->getExprLoc(), DiagID)
2113 << E->getStmtClassName() << E->getSourceRange();
2114 break;
2115 }
2116
2117 // These are used for internal purposes and cannot be meaningfully mangled.
John McCall7cd7d1a2010-11-15 23:31:06 +00002118 case Expr::OpaqueValueExprClass:
2119 llvm_unreachable("cannot mangle opaque value; mangling wrong thing?");
2120
John McCall0512e482010-07-14 04:20:34 +00002121 case Expr::CXXDefaultArgExprClass:
John McCall5e1e89b2010-08-18 19:18:59 +00002122 mangleExpression(cast<CXXDefaultArgExpr>(E)->getExpr(), Arity);
John McCall0512e482010-07-14 04:20:34 +00002123 break;
2124
2125 case Expr::CXXMemberCallExprClass: // fallthrough
John McCall1dd73832010-02-04 01:42:13 +00002126 case Expr::CallExprClass: {
2127 const CallExpr *CE = cast<CallExpr>(E);
John McCall5a7e6f72011-04-28 02:52:03 +00002128
2129 // <expression> ::= cp <simple-id> <expression>* E
2130 // We use this mangling only when the call would use ADL except
2131 // for being parenthesized. Per discussion with David
2132 // Vandervoorde, 2011.04.25.
2133 if (isParenthesizedADLCallee(CE)) {
2134 Out << "cp";
2135 // The callee here is a parenthesized UnresolvedLookupExpr with
2136 // no qualifier and should always get mangled as a <simple-id>
2137 // anyway.
2138
2139 // <expression> ::= cl <expression>* E
2140 } else {
2141 Out << "cl";
2142 }
2143
John McCall5e1e89b2010-08-18 19:18:59 +00002144 mangleExpression(CE->getCallee(), CE->getNumArgs());
John McCall1dd73832010-02-04 01:42:13 +00002145 for (unsigned I = 0, N = CE->getNumArgs(); I != N; ++I)
2146 mangleExpression(CE->getArg(I));
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002147 Out << 'E';
John McCall09cc1412010-02-03 00:55:45 +00002148 break;
John McCall1dd73832010-02-04 01:42:13 +00002149 }
John McCall09cc1412010-02-03 00:55:45 +00002150
John McCall0512e482010-07-14 04:20:34 +00002151 case Expr::CXXNewExprClass: {
2152 // Proposal from David Vandervoorde, 2010.06.30
2153 const CXXNewExpr *New = cast<CXXNewExpr>(E);
2154 if (New->isGlobalNew()) Out << "gs";
2155 Out << (New->isArray() ? "na" : "nw");
2156 for (CXXNewExpr::const_arg_iterator I = New->placement_arg_begin(),
2157 E = New->placement_arg_end(); I != E; ++I)
2158 mangleExpression(*I);
2159 Out << '_';
2160 mangleType(New->getAllocatedType());
2161 if (New->hasInitializer()) {
2162 Out << "pi";
2163 for (CXXNewExpr::const_arg_iterator I = New->constructor_arg_begin(),
2164 E = New->constructor_arg_end(); I != E; ++I)
2165 mangleExpression(*I);
2166 }
2167 Out << 'E';
2168 break;
2169 }
2170
John McCall2f27bf82010-02-04 02:56:29 +00002171 case Expr::MemberExprClass: {
2172 const MemberExpr *ME = cast<MemberExpr>(E);
2173 mangleMemberExpr(ME->getBase(), ME->isArrow(),
John McCalla0ce15c2011-04-24 08:23:24 +00002174 ME->getQualifier(), 0, ME->getMemberDecl()->getDeclName(),
John McCall5e1e89b2010-08-18 19:18:59 +00002175 Arity);
John McCall2f27bf82010-02-04 02:56:29 +00002176 break;
2177 }
2178
2179 case Expr::UnresolvedMemberExprClass: {
2180 const UnresolvedMemberExpr *ME = cast<UnresolvedMemberExpr>(E);
2181 mangleMemberExpr(ME->getBase(), ME->isArrow(),
John McCalla0ce15c2011-04-24 08:23:24 +00002182 ME->getQualifier(), 0, ME->getMemberName(),
John McCall5e1e89b2010-08-18 19:18:59 +00002183 Arity);
John McCall6dbce192010-08-20 00:17:19 +00002184 if (ME->hasExplicitTemplateArgs())
2185 mangleTemplateArgs(ME->getExplicitTemplateArgs());
John McCall2f27bf82010-02-04 02:56:29 +00002186 break;
2187 }
2188
2189 case Expr::CXXDependentScopeMemberExprClass: {
2190 const CXXDependentScopeMemberExpr *ME
2191 = cast<CXXDependentScopeMemberExpr>(E);
2192 mangleMemberExpr(ME->getBase(), ME->isArrow(),
John McCalla0ce15c2011-04-24 08:23:24 +00002193 ME->getQualifier(), ME->getFirstQualifierFoundInScope(),
2194 ME->getMember(), Arity);
John McCall6dbce192010-08-20 00:17:19 +00002195 if (ME->hasExplicitTemplateArgs())
2196 mangleTemplateArgs(ME->getExplicitTemplateArgs());
John McCall2f27bf82010-02-04 02:56:29 +00002197 break;
2198 }
2199
John McCall1dd73832010-02-04 01:42:13 +00002200 case Expr::UnresolvedLookupExprClass: {
2201 const UnresolvedLookupExpr *ULE = cast<UnresolvedLookupExpr>(E);
John McCalla0ce15c2011-04-24 08:23:24 +00002202 mangleUnresolvedName(ULE->getQualifier(), 0, ULE->getName(), Arity);
John McCall6dbce192010-08-20 00:17:19 +00002203 if (ULE->hasExplicitTemplateArgs())
2204 mangleTemplateArgs(ULE->getExplicitTemplateArgs());
John McCall1dd73832010-02-04 01:42:13 +00002205 break;
2206 }
2207
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002208 case Expr::CXXUnresolvedConstructExprClass: {
John McCall1dd73832010-02-04 01:42:13 +00002209 const CXXUnresolvedConstructExpr *CE = cast<CXXUnresolvedConstructExpr>(E);
2210 unsigned N = CE->arg_size();
2211
2212 Out << "cv";
2213 mangleType(CE->getType());
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002214 if (N != 1) Out << '_';
John McCall1dd73832010-02-04 01:42:13 +00002215 for (unsigned I = 0; I != N; ++I) mangleExpression(CE->getArg(I));
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002216 if (N != 1) Out << 'E';
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002217 break;
John McCall1dd73832010-02-04 01:42:13 +00002218 }
John McCall09cc1412010-02-03 00:55:45 +00002219
John McCall1dd73832010-02-04 01:42:13 +00002220 case Expr::CXXTemporaryObjectExprClass:
2221 case Expr::CXXConstructExprClass: {
2222 const CXXConstructExpr *CE = cast<CXXConstructExpr>(E);
2223 unsigned N = CE->getNumArgs();
2224
2225 Out << "cv";
2226 mangleType(CE->getType());
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002227 if (N != 1) Out << '_';
John McCall1dd73832010-02-04 01:42:13 +00002228 for (unsigned I = 0; I != N; ++I) mangleExpression(CE->getArg(I));
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002229 if (N != 1) Out << 'E';
John McCall09cc1412010-02-03 00:55:45 +00002230 break;
John McCall1dd73832010-02-04 01:42:13 +00002231 }
2232
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +00002233 case Expr::UnaryExprOrTypeTraitExprClass: {
2234 const UnaryExprOrTypeTraitExpr *SAE = cast<UnaryExprOrTypeTraitExpr>(E);
2235 switch(SAE->getKind()) {
2236 case UETT_SizeOf:
2237 Out << 's';
2238 break;
2239 case UETT_AlignOf:
2240 Out << 'a';
2241 break;
2242 case UETT_VecStep:
2243 Diagnostic &Diags = Context.getDiags();
2244 unsigned DiagID = Diags.getCustomDiagID(Diagnostic::Error,
2245 "cannot yet mangle vec_step expression");
2246 Diags.Report(DiagID);
2247 return;
2248 }
John McCall1dd73832010-02-04 01:42:13 +00002249 if (SAE->isArgumentType()) {
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002250 Out << 't';
John McCall1dd73832010-02-04 01:42:13 +00002251 mangleType(SAE->getArgumentType());
2252 } else {
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002253 Out << 'z';
John McCall1dd73832010-02-04 01:42:13 +00002254 mangleExpression(SAE->getArgumentExpr());
2255 }
2256 break;
2257 }
Anders Carlssona7694082009-11-06 02:50:19 +00002258
John McCall0512e482010-07-14 04:20:34 +00002259 case Expr::CXXThrowExprClass: {
2260 const CXXThrowExpr *TE = cast<CXXThrowExpr>(E);
2261
2262 // Proposal from David Vandervoorde, 2010.06.30
2263 if (TE->getSubExpr()) {
2264 Out << "tw";
2265 mangleExpression(TE->getSubExpr());
2266 } else {
2267 Out << "tr";
2268 }
2269 break;
2270 }
2271
2272 case Expr::CXXTypeidExprClass: {
2273 const CXXTypeidExpr *TIE = cast<CXXTypeidExpr>(E);
2274
2275 // Proposal from David Vandervoorde, 2010.06.30
2276 if (TIE->isTypeOperand()) {
2277 Out << "ti";
2278 mangleType(TIE->getTypeOperand());
2279 } else {
2280 Out << "te";
2281 mangleExpression(TIE->getExprOperand());
2282 }
2283 break;
2284 }
2285
2286 case Expr::CXXDeleteExprClass: {
2287 const CXXDeleteExpr *DE = cast<CXXDeleteExpr>(E);
2288
2289 // Proposal from David Vandervoorde, 2010.06.30
2290 if (DE->isGlobalDelete()) Out << "gs";
2291 Out << (DE->isArrayForm() ? "da" : "dl");
2292 mangleExpression(DE->getArgument());
2293 break;
2294 }
2295
Anders Carlssone170ba72009-12-14 01:45:37 +00002296 case Expr::UnaryOperatorClass: {
2297 const UnaryOperator *UO = cast<UnaryOperator>(E);
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002298 mangleOperatorName(UnaryOperator::getOverloadedOperator(UO->getOpcode()),
Anders Carlssone170ba72009-12-14 01:45:37 +00002299 /*Arity=*/1);
2300 mangleExpression(UO->getSubExpr());
2301 break;
2302 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002303
John McCall0512e482010-07-14 04:20:34 +00002304 case Expr::ArraySubscriptExprClass: {
2305 const ArraySubscriptExpr *AE = cast<ArraySubscriptExpr>(E);
2306
Chris Lattnerfc8f0e12011-04-15 05:22:18 +00002307 // Array subscript is treated as a syntactically weird form of
John McCall0512e482010-07-14 04:20:34 +00002308 // binary operator.
2309 Out << "ix";
2310 mangleExpression(AE->getLHS());
2311 mangleExpression(AE->getRHS());
2312 break;
2313 }
2314
2315 case Expr::CompoundAssignOperatorClass: // fallthrough
Anders Carlssone170ba72009-12-14 01:45:37 +00002316 case Expr::BinaryOperatorClass: {
2317 const BinaryOperator *BO = cast<BinaryOperator>(E);
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002318 mangleOperatorName(BinaryOperator::getOverloadedOperator(BO->getOpcode()),
Anders Carlssone170ba72009-12-14 01:45:37 +00002319 /*Arity=*/2);
2320 mangleExpression(BO->getLHS());
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002321 mangleExpression(BO->getRHS());
Anders Carlssone170ba72009-12-14 01:45:37 +00002322 break;
John McCall2f27bf82010-02-04 02:56:29 +00002323 }
Anders Carlssone170ba72009-12-14 01:45:37 +00002324
2325 case Expr::ConditionalOperatorClass: {
2326 const ConditionalOperator *CO = cast<ConditionalOperator>(E);
2327 mangleOperatorName(OO_Conditional, /*Arity=*/3);
2328 mangleExpression(CO->getCond());
John McCall5e1e89b2010-08-18 19:18:59 +00002329 mangleExpression(CO->getLHS(), Arity);
2330 mangleExpression(CO->getRHS(), Arity);
Anders Carlssone170ba72009-12-14 01:45:37 +00002331 break;
2332 }
2333
Douglas Gregor46287c72010-01-29 16:37:09 +00002334 case Expr::ImplicitCastExprClass: {
John McCall5e1e89b2010-08-18 19:18:59 +00002335 mangleExpression(cast<ImplicitCastExpr>(E)->getSubExpr(), Arity);
Douglas Gregor46287c72010-01-29 16:37:09 +00002336 break;
2337 }
2338
2339 case Expr::CStyleCastExprClass:
2340 case Expr::CXXStaticCastExprClass:
2341 case Expr::CXXDynamicCastExprClass:
2342 case Expr::CXXReinterpretCastExprClass:
2343 case Expr::CXXConstCastExprClass:
2344 case Expr::CXXFunctionalCastExprClass: {
2345 const ExplicitCastExpr *ECE = cast<ExplicitCastExpr>(E);
2346 Out << "cv";
2347 mangleType(ECE->getType());
2348 mangleExpression(ECE->getSubExpr());
2349 break;
2350 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002351
Anders Carlsson58040a52009-12-16 05:48:46 +00002352 case Expr::CXXOperatorCallExprClass: {
2353 const CXXOperatorCallExpr *CE = cast<CXXOperatorCallExpr>(E);
2354 unsigned NumArgs = CE->getNumArgs();
2355 mangleOperatorName(CE->getOperator(), /*Arity=*/NumArgs);
2356 // Mangle the arguments.
2357 for (unsigned i = 0; i != NumArgs; ++i)
2358 mangleExpression(CE->getArg(i));
2359 break;
2360 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002361
Anders Carlssona7694082009-11-06 02:50:19 +00002362 case Expr::ParenExprClass:
John McCall5e1e89b2010-08-18 19:18:59 +00002363 mangleExpression(cast<ParenExpr>(E)->getSubExpr(), Arity);
Anders Carlssona7694082009-11-06 02:50:19 +00002364 break;
2365
Anders Carlssond553f8c2009-09-21 01:21:10 +00002366 case Expr::DeclRefExprClass: {
Douglas Gregor5ed1bc32010-02-28 21:40:32 +00002367 const NamedDecl *D = cast<DeclRefExpr>(E)->getDecl();
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002368
Anders Carlssond553f8c2009-09-21 01:21:10 +00002369 switch (D->getKind()) {
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002370 default:
Douglas Gregor5ed1bc32010-02-28 21:40:32 +00002371 // <expr-primary> ::= L <mangled-name> E # external name
2372 Out << 'L';
2373 mangle(D, "_Z");
2374 Out << 'E';
2375 break;
2376
John McCallfb44de92011-05-01 22:35:37 +00002377 case Decl::ParmVar:
2378 mangleFunctionParam(cast<ParmVarDecl>(D));
2379 break;
2380
John McCall3dc7e7b2010-07-24 01:17:35 +00002381 case Decl::EnumConstant: {
2382 const EnumConstantDecl *ED = cast<EnumConstantDecl>(D);
2383 mangleIntegerLiteral(ED->getType(), ED->getInitVal());
2384 break;
2385 }
2386
Anders Carlssond553f8c2009-09-21 01:21:10 +00002387 case Decl::NonTypeTemplateParm: {
2388 const NonTypeTemplateParmDecl *PD = cast<NonTypeTemplateParmDecl>(D);
Anders Carlsson0ccdf8d2009-09-27 00:38:53 +00002389 mangleTemplateParameter(PD->getIndex());
Anders Carlssond553f8c2009-09-21 01:21:10 +00002390 break;
2391 }
2392
2393 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002394
Anders Carlsson50755b02009-09-27 20:11:34 +00002395 break;
Anders Carlssond553f8c2009-09-21 01:21:10 +00002396 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002397
Douglas Gregorc7793c72011-01-15 01:15:58 +00002398 case Expr::SubstNonTypeTemplateParmPackExprClass:
2399 mangleTemplateParameter(
2400 cast<SubstNonTypeTemplateParmPackExpr>(E)->getParameterPack()->getIndex());
2401 break;
2402
John McCall865d4472009-11-19 22:55:06 +00002403 case Expr::DependentScopeDeclRefExprClass: {
2404 const DependentScopeDeclRefExpr *DRE = cast<DependentScopeDeclRefExpr>(E);
Douglas Gregor4b2ccfc2010-02-28 22:05:49 +00002405 NestedNameSpecifier *NNS = DRE->getQualifier();
2406 const Type *QTy = NNS->getAsType();
2407
2408 // When we're dealing with a nested-name-specifier that has just a
2409 // dependent identifier in it, mangle that as a typename. FIXME:
2410 // It isn't clear that we ever actually want to have such a
2411 // nested-name-specifier; why not just represent it as a typename type?
2412 if (!QTy && NNS->getAsIdentifier() && NNS->getPrefix()) {
Douglas Gregor4a2023f2010-03-31 20:19:30 +00002413 QTy = getASTContext().getDependentNameType(ETK_Typename,
2414 NNS->getPrefix(),
2415 NNS->getAsIdentifier())
Douglas Gregor4b2ccfc2010-02-28 22:05:49 +00002416 .getTypePtr();
2417 }
Anders Carlsson50755b02009-09-27 20:11:34 +00002418 assert(QTy && "Qualifier was not type!");
2419
John McCall6dbce192010-08-20 00:17:19 +00002420 // ::= sr <type> <unqualified-name> # dependent name
2421 // ::= sr <type> <unqualified-name> <template-args> # dependent template-id
Anders Carlsson50755b02009-09-27 20:11:34 +00002422 Out << "sr";
2423 mangleType(QualType(QTy, 0));
John McCall5e1e89b2010-08-18 19:18:59 +00002424 mangleUnqualifiedName(0, DRE->getDeclName(), Arity);
John McCall6dbce192010-08-20 00:17:19 +00002425 if (DRE->hasExplicitTemplateArgs())
2426 mangleTemplateArgs(DRE->getExplicitTemplateArgs());
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002427
Anders Carlsson50755b02009-09-27 20:11:34 +00002428 break;
2429 }
2430
John McCalld9307602010-04-09 22:54:09 +00002431 case Expr::CXXBindTemporaryExprClass:
2432 mangleExpression(cast<CXXBindTemporaryExpr>(E)->getSubExpr());
2433 break;
2434
John McCall4765fa02010-12-06 08:20:24 +00002435 case Expr::ExprWithCleanupsClass:
2436 mangleExpression(cast<ExprWithCleanups>(E)->getSubExpr(), Arity);
John McCalld9307602010-04-09 22:54:09 +00002437 break;
2438
John McCall1dd73832010-02-04 01:42:13 +00002439 case Expr::FloatingLiteralClass: {
2440 const FloatingLiteral *FL = cast<FloatingLiteral>(E);
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002441 Out << 'L';
John McCall1dd73832010-02-04 01:42:13 +00002442 mangleType(FL->getType());
John McCall0512e482010-07-14 04:20:34 +00002443 mangleFloat(FL->getValue());
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002444 Out << 'E';
John McCall1dd73832010-02-04 01:42:13 +00002445 break;
2446 }
2447
John McCallde810632010-04-09 21:48:08 +00002448 case Expr::CharacterLiteralClass:
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002449 Out << 'L';
John McCallde810632010-04-09 21:48:08 +00002450 mangleType(E->getType());
2451 Out << cast<CharacterLiteral>(E)->getValue();
2452 Out << 'E';
2453 break;
2454
2455 case Expr::CXXBoolLiteralExprClass:
2456 Out << "Lb";
2457 Out << (cast<CXXBoolLiteralExpr>(E)->getValue() ? '1' : '0');
2458 Out << 'E';
2459 break;
2460
John McCall0512e482010-07-14 04:20:34 +00002461 case Expr::IntegerLiteralClass: {
2462 llvm::APSInt Value(cast<IntegerLiteral>(E)->getValue());
2463 if (E->getType()->isSignedIntegerType())
2464 Value.setIsSigned(true);
2465 mangleIntegerLiteral(E->getType(), Value);
Anders Carlssone170ba72009-12-14 01:45:37 +00002466 break;
John McCall0512e482010-07-14 04:20:34 +00002467 }
2468
2469 case Expr::ImaginaryLiteralClass: {
2470 const ImaginaryLiteral *IE = cast<ImaginaryLiteral>(E);
2471 // Mangle as if a complex literal.
Nick Lewycky271b6652010-09-05 03:40:33 +00002472 // Proposal from David Vandevoorde, 2010.06.30.
John McCall0512e482010-07-14 04:20:34 +00002473 Out << 'L';
2474 mangleType(E->getType());
2475 if (const FloatingLiteral *Imag =
2476 dyn_cast<FloatingLiteral>(IE->getSubExpr())) {
2477 // Mangle a floating-point zero of the appropriate type.
2478 mangleFloat(llvm::APFloat(Imag->getValue().getSemantics()));
2479 Out << '_';
2480 mangleFloat(Imag->getValue());
2481 } else {
Nick Lewycky271b6652010-09-05 03:40:33 +00002482 Out << "0_";
John McCall0512e482010-07-14 04:20:34 +00002483 llvm::APSInt Value(cast<IntegerLiteral>(IE->getSubExpr())->getValue());
2484 if (IE->getSubExpr()->getType()->isSignedIntegerType())
2485 Value.setIsSigned(true);
2486 mangleNumber(Value);
2487 }
2488 Out << 'E';
2489 break;
2490 }
2491
2492 case Expr::StringLiteralClass: {
John McCall1658c392010-07-15 21:53:03 +00002493 // Revised proposal from David Vandervoorde, 2010.07.15.
John McCall0512e482010-07-14 04:20:34 +00002494 Out << 'L';
John McCall1658c392010-07-15 21:53:03 +00002495 assert(isa<ConstantArrayType>(E->getType()));
2496 mangleType(E->getType());
John McCall0512e482010-07-14 04:20:34 +00002497 Out << 'E';
2498 break;
2499 }
2500
2501 case Expr::GNUNullExprClass:
2502 // FIXME: should this really be mangled the same as nullptr?
2503 // fallthrough
2504
2505 case Expr::CXXNullPtrLiteralExprClass: {
2506 // Proposal from David Vandervoorde, 2010.06.30, as
2507 // modified by ABI list discussion.
2508 Out << "LDnE";
2509 break;
2510 }
Douglas Gregorbe230c32011-01-03 17:17:50 +00002511
2512 case Expr::PackExpansionExprClass:
2513 Out << "sp";
2514 mangleExpression(cast<PackExpansionExpr>(E)->getPattern());
2515 break;
Douglas Gregor2e774c42011-01-04 18:56:13 +00002516
2517 case Expr::SizeOfPackExprClass: {
Douglas Gregor2e774c42011-01-04 18:56:13 +00002518 Out << "sZ";
2519 const NamedDecl *Pack = cast<SizeOfPackExpr>(E)->getPack();
2520 if (const TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(Pack))
2521 mangleTemplateParameter(TTP->getIndex());
2522 else if (const NonTypeTemplateParmDecl *NTTP
2523 = dyn_cast<NonTypeTemplateParmDecl>(Pack))
2524 mangleTemplateParameter(NTTP->getIndex());
2525 else if (const TemplateTemplateParmDecl *TempTP
2526 = dyn_cast<TemplateTemplateParmDecl>(Pack))
2527 mangleTemplateParameter(TempTP->getIndex());
2528 else {
Douglas Gregor4fc48662011-01-13 16:39:34 +00002529 // Note: proposed by Mike Herrick on 11/30/10
2530 // <expression> ::= sZ <function-param> # size of function parameter pack
Douglas Gregor2e774c42011-01-04 18:56:13 +00002531 Diagnostic &Diags = Context.getDiags();
2532 unsigned DiagID = Diags.getCustomDiagID(Diagnostic::Error,
2533 "cannot mangle sizeof...(function parameter pack)");
2534 Diags.Report(DiagID);
2535 return;
2536 }
Douglas Gregordfbbcf92011-03-03 02:20:19 +00002537 break;
Douglas Gregor2e774c42011-01-04 18:56:13 +00002538 }
Anders Carlssond553f8c2009-09-21 01:21:10 +00002539 }
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00002540}
2541
John McCallfb44de92011-05-01 22:35:37 +00002542/// Mangle an expression which refers to a parameter variable.
2543///
2544/// <expression> ::= <function-param>
2545/// <function-param> ::= fp <top-level CV-qualifiers> _ # L == 0, I == 0
2546/// <function-param> ::= fp <top-level CV-qualifiers>
2547/// <parameter-2 non-negative number> _ # L == 0, I > 0
2548/// <function-param> ::= fL <L-1 non-negative number>
2549/// p <top-level CV-qualifiers> _ # L > 0, I == 0
2550/// <function-param> ::= fL <L-1 non-negative number>
2551/// p <top-level CV-qualifiers>
2552/// <I-1 non-negative number> _ # L > 0, I > 0
2553///
2554/// L is the nesting depth of the parameter, defined as 1 if the
2555/// parameter comes from the innermost function prototype scope
2556/// enclosing the current context, 2 if from the next enclosing
2557/// function prototype scope, and so on, with one special case: if
2558/// we've processed the full parameter clause for the innermost
2559/// function type, then L is one less. This definition conveniently
2560/// makes it irrelevant whether a function's result type was written
2561/// trailing or leading, but is otherwise overly complicated; the
2562/// numbering was first designed without considering references to
2563/// parameter in locations other than return types, and then the
2564/// mangling had to be generalized without changing the existing
2565/// manglings.
2566///
2567/// I is the zero-based index of the parameter within its parameter
2568/// declaration clause. Note that the original ABI document describes
2569/// this using 1-based ordinals.
2570void CXXNameMangler::mangleFunctionParam(const ParmVarDecl *parm) {
2571 unsigned parmDepth = parm->getFunctionScopeDepth();
2572 unsigned parmIndex = parm->getFunctionScopeIndex();
2573
2574 // Compute 'L'.
2575 // parmDepth does not include the declaring function prototype.
2576 // FunctionTypeDepth does account for that.
2577 assert(parmDepth < FunctionTypeDepth.getDepth());
2578 unsigned nestingDepth = FunctionTypeDepth.getDepth() - parmDepth;
2579 if (FunctionTypeDepth.isInResultType())
2580 nestingDepth--;
2581
2582 if (nestingDepth == 0) {
2583 Out << "fp";
2584 } else {
2585 Out << "fL" << (nestingDepth - 1) << 'p';
2586 }
2587
2588 // Top-level qualifiers. We don't have to worry about arrays here,
2589 // because parameters declared as arrays should already have been
2590 // tranformed to have pointer type. FIXME: apparently these don't
2591 // get mangled if used as an rvalue of a known non-class type?
2592 assert(!parm->getType()->isArrayType()
2593 && "parameter's type is still an array type?");
2594 mangleQualifiers(parm->getType().getQualifiers());
2595
2596 // Parameter index.
2597 if (parmIndex != 0) {
2598 Out << (parmIndex - 1);
2599 }
2600 Out << '_';
2601}
2602
Anders Carlsson3ac86b52009-04-15 05:36:58 +00002603void CXXNameMangler::mangleCXXCtorType(CXXCtorType T) {
2604 // <ctor-dtor-name> ::= C1 # complete object constructor
2605 // ::= C2 # base object constructor
2606 // ::= C3 # complete object allocating constructor
2607 //
2608 switch (T) {
2609 case Ctor_Complete:
2610 Out << "C1";
2611 break;
2612 case Ctor_Base:
2613 Out << "C2";
2614 break;
2615 case Ctor_CompleteAllocating:
2616 Out << "C3";
2617 break;
2618 }
2619}
2620
Anders Carlsson27ae5362009-04-17 01:58:57 +00002621void CXXNameMangler::mangleCXXDtorType(CXXDtorType T) {
2622 // <ctor-dtor-name> ::= D0 # deleting destructor
2623 // ::= D1 # complete object destructor
2624 // ::= D2 # base object destructor
2625 //
2626 switch (T) {
2627 case Dtor_Deleting:
2628 Out << "D0";
2629 break;
2630 case Dtor_Complete:
2631 Out << "D1";
2632 break;
2633 case Dtor_Base:
2634 Out << "D2";
2635 break;
2636 }
2637}
2638
John McCall6dbce192010-08-20 00:17:19 +00002639void CXXNameMangler::mangleTemplateArgs(
2640 const ExplicitTemplateArgumentList &TemplateArgs) {
2641 // <template-args> ::= I <template-arg>+ E
2642 Out << 'I';
John McCall4f4e4132011-05-04 01:45:19 +00002643 for (unsigned i = 0, e = TemplateArgs.NumTemplateArgs; i != e; ++i)
2644 mangleTemplateArg(0, TemplateArgs.getTemplateArgs()[i].getArgument());
John McCall6dbce192010-08-20 00:17:19 +00002645 Out << 'E';
2646}
2647
Douglas Gregor20f0cc72010-04-23 03:10:43 +00002648void CXXNameMangler::mangleTemplateArgs(TemplateName Template,
2649 const TemplateArgument *TemplateArgs,
2650 unsigned NumTemplateArgs) {
2651 if (TemplateDecl *TD = Template.getAsTemplateDecl())
2652 return mangleTemplateArgs(*TD->getTemplateParameters(), TemplateArgs,
2653 NumTemplateArgs);
Sean Huntc3021132010-05-05 15:23:54 +00002654
John McCall4f4e4132011-05-04 01:45:19 +00002655 mangleUnresolvedTemplateArgs(TemplateArgs, NumTemplateArgs);
2656}
2657
2658void CXXNameMangler::mangleUnresolvedTemplateArgs(const TemplateArgument *args,
2659 unsigned numArgs) {
Douglas Gregor20f0cc72010-04-23 03:10:43 +00002660 // <template-args> ::= I <template-arg>+ E
2661 Out << 'I';
John McCall4f4e4132011-05-04 01:45:19 +00002662 for (unsigned i = 0; i != numArgs; ++i)
2663 mangleTemplateArg(0, args[i]);
Douglas Gregor20f0cc72010-04-23 03:10:43 +00002664 Out << 'E';
2665}
2666
Rafael Espindolad9800722010-03-11 14:07:00 +00002667void CXXNameMangler::mangleTemplateArgs(const TemplateParameterList &PL,
2668 const TemplateArgumentList &AL) {
Anders Carlsson7a0ba872009-05-15 16:09:15 +00002669 // <template-args> ::= I <template-arg>+ E
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002670 Out << 'I';
Rafael Espindolad9800722010-03-11 14:07:00 +00002671 for (unsigned i = 0, e = AL.size(); i != e; ++i)
2672 mangleTemplateArg(PL.getParam(i), AL[i]);
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002673 Out << 'E';
Anders Carlsson7a0ba872009-05-15 16:09:15 +00002674}
2675
Rafael Espindolad9800722010-03-11 14:07:00 +00002676void CXXNameMangler::mangleTemplateArgs(const TemplateParameterList &PL,
2677 const TemplateArgument *TemplateArgs,
Anders Carlsson7624f212009-09-18 02:42:01 +00002678 unsigned NumTemplateArgs) {
2679 // <template-args> ::= I <template-arg>+ E
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002680 Out << 'I';
Daniel Dunbar7e0c1952009-11-21 09:17:15 +00002681 for (unsigned i = 0; i != NumTemplateArgs; ++i)
Rafael Espindolad9800722010-03-11 14:07:00 +00002682 mangleTemplateArg(PL.getParam(i), TemplateArgs[i]);
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002683 Out << 'E';
Anders Carlsson7624f212009-09-18 02:42:01 +00002684}
2685
Rafael Espindolad9800722010-03-11 14:07:00 +00002686void CXXNameMangler::mangleTemplateArg(const NamedDecl *P,
2687 const TemplateArgument &A) {
Mike Stump1eb44332009-09-09 15:08:12 +00002688 // <template-arg> ::= <type> # type or template
Anders Carlsson7a0ba872009-05-15 16:09:15 +00002689 // ::= X <expression> E # expression
2690 // ::= <expr-primary> # simple expressions
Douglas Gregor4fc48662011-01-13 16:39:34 +00002691 // ::= J <template-arg>* E # argument pack
Anders Carlsson7a0ba872009-05-15 16:09:15 +00002692 // ::= sp <expression> # pack expansion of (C++0x)
2693 switch (A.getKind()) {
Douglas Gregorf90b27a2011-01-03 22:36:02 +00002694 case TemplateArgument::Null:
2695 llvm_unreachable("Cannot mangle NULL template argument");
2696
Anders Carlsson7a0ba872009-05-15 16:09:15 +00002697 case TemplateArgument::Type:
2698 mangleType(A.getAsType());
2699 break;
Anders Carlsson9e85c742009-12-23 19:30:55 +00002700 case TemplateArgument::Template:
John McCallb6f532e2010-07-14 06:43:17 +00002701 // This is mangled as <type>.
2702 mangleType(A.getAsTemplate());
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002703 break;
Douglas Gregora7fc9012011-01-05 18:58:31 +00002704 case TemplateArgument::TemplateExpansion:
Douglas Gregor4fc48662011-01-13 16:39:34 +00002705 // <type> ::= Dp <type> # pack expansion (C++0x)
Douglas Gregora7fc9012011-01-05 18:58:31 +00002706 Out << "Dp";
2707 mangleType(A.getAsTemplateOrTemplatePattern());
2708 break;
Anders Carlssond553f8c2009-09-21 01:21:10 +00002709 case TemplateArgument::Expression:
2710 Out << 'X';
2711 mangleExpression(A.getAsExpr());
2712 Out << 'E';
2713 break;
Anders Carlssone170ba72009-12-14 01:45:37 +00002714 case TemplateArgument::Integral:
2715 mangleIntegerLiteral(A.getIntegralType(), *A.getAsIntegral());
Anders Carlsson7a0ba872009-05-15 16:09:15 +00002716 break;
Daniel Dunbar7e0c1952009-11-21 09:17:15 +00002717 case TemplateArgument::Declaration: {
Douglas Gregor20f0cc72010-04-23 03:10:43 +00002718 assert(P && "Missing template parameter for declaration argument");
Daniel Dunbar7e0c1952009-11-21 09:17:15 +00002719 // <expr-primary> ::= L <mangled-name> E # external name
2720
Rafael Espindolad9800722010-03-11 14:07:00 +00002721 // Clang produces AST's where pointer-to-member-function expressions
Daniel Dunbar7e0c1952009-11-21 09:17:15 +00002722 // and pointer-to-function expressions are represented as a declaration not
Rafael Espindolad9800722010-03-11 14:07:00 +00002723 // an expression. We compensate for it here to produce the correct mangling.
2724 NamedDecl *D = cast<NamedDecl>(A.getAsDecl());
2725 const NonTypeTemplateParmDecl *Parameter = cast<NonTypeTemplateParmDecl>(P);
John McCallc0a45592011-04-24 08:43:07 +00002726 bool compensateMangling = !Parameter->getType()->isReferenceType();
Rafael Espindolad9800722010-03-11 14:07:00 +00002727 if (compensateMangling) {
2728 Out << 'X';
2729 mangleOperatorName(OO_Amp, 1);
2730 }
2731
Daniel Dunbar7e0c1952009-11-21 09:17:15 +00002732 Out << 'L';
2733 // References to external entities use the mangled name; if the name would
2734 // not normally be manged then mangle it as unqualified.
2735 //
2736 // FIXME: The ABI specifies that external names here should have _Z, but
2737 // gcc leaves this off.
Rafael Espindolad9800722010-03-11 14:07:00 +00002738 if (compensateMangling)
2739 mangle(D, "_Z");
2740 else
2741 mangle(D, "Z");
Daniel Dunbar7e0c1952009-11-21 09:17:15 +00002742 Out << 'E';
Rafael Espindolad9800722010-03-11 14:07:00 +00002743
2744 if (compensateMangling)
2745 Out << 'E';
2746
Daniel Dunbar7e0c1952009-11-21 09:17:15 +00002747 break;
2748 }
Douglas Gregorf90b27a2011-01-03 22:36:02 +00002749
2750 case TemplateArgument::Pack: {
2751 // Note: proposal by Mike Herrick on 12/20/10
2752 Out << 'J';
2753 for (TemplateArgument::pack_iterator PA = A.pack_begin(),
2754 PAEnd = A.pack_end();
2755 PA != PAEnd; ++PA)
2756 mangleTemplateArg(P, *PA);
2757 Out << 'E';
2758 }
Daniel Dunbar7e0c1952009-11-21 09:17:15 +00002759 }
Anders Carlsson7a0ba872009-05-15 16:09:15 +00002760}
2761
Anders Carlsson0ccdf8d2009-09-27 00:38:53 +00002762void CXXNameMangler::mangleTemplateParameter(unsigned Index) {
2763 // <template-param> ::= T_ # first template parameter
2764 // ::= T <parameter-2 non-negative number> _
2765 if (Index == 0)
2766 Out << "T_";
2767 else
2768 Out << 'T' << (Index - 1) << '_';
2769}
2770
Anders Carlsson76967372009-09-17 00:43:46 +00002771// <substitution> ::= S <seq-id> _
2772// ::= S_
Anders Carlsson6862fc72009-09-17 04:16:28 +00002773bool CXXNameMangler::mangleSubstitution(const NamedDecl *ND) {
Anders Carlssone7c8cb62009-09-26 20:53:44 +00002774 // Try one of the standard substitutions first.
2775 if (mangleStandardSubstitution(ND))
2776 return true;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002777
Anders Carlsson433d1372009-11-07 04:26:04 +00002778 ND = cast<NamedDecl>(ND->getCanonicalDecl());
Anders Carlsson6862fc72009-09-17 04:16:28 +00002779 return mangleSubstitution(reinterpret_cast<uintptr_t>(ND));
2780}
2781
Anders Carlsson76967372009-09-17 00:43:46 +00002782bool CXXNameMangler::mangleSubstitution(QualType T) {
Anders Carlssond99edc42009-09-26 03:55:37 +00002783 if (!T.getCVRQualifiers()) {
2784 if (const RecordType *RT = T->getAs<RecordType>())
2785 return mangleSubstitution(RT->getDecl());
2786 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002787
Anders Carlsson76967372009-09-17 00:43:46 +00002788 uintptr_t TypePtr = reinterpret_cast<uintptr_t>(T.getAsOpaquePtr());
2789
Anders Carlssond3a932a2009-09-17 03:53:28 +00002790 return mangleSubstitution(TypePtr);
2791}
2792
Douglas Gregor1e9268e2010-04-28 05:58:56 +00002793bool CXXNameMangler::mangleSubstitution(TemplateName Template) {
2794 if (TemplateDecl *TD = Template.getAsTemplateDecl())
2795 return mangleSubstitution(TD);
Sean Huntc3021132010-05-05 15:23:54 +00002796
Douglas Gregor1e9268e2010-04-28 05:58:56 +00002797 Template = Context.getASTContext().getCanonicalTemplateName(Template);
2798 return mangleSubstitution(
2799 reinterpret_cast<uintptr_t>(Template.getAsVoidPointer()));
2800}
2801
Anders Carlssond3a932a2009-09-17 03:53:28 +00002802bool CXXNameMangler::mangleSubstitution(uintptr_t Ptr) {
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002803 llvm::DenseMap<uintptr_t, unsigned>::iterator I = Substitutions.find(Ptr);
Anders Carlsson76967372009-09-17 00:43:46 +00002804 if (I == Substitutions.end())
2805 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002806
Anders Carlsson76967372009-09-17 00:43:46 +00002807 unsigned SeqID = I->second;
2808 if (SeqID == 0)
2809 Out << "S_";
2810 else {
2811 SeqID--;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002812
Anders Carlsson76967372009-09-17 00:43:46 +00002813 // <seq-id> is encoded in base-36, using digits and upper case letters.
2814 char Buffer[10];
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002815 char *BufferPtr = llvm::array_endof(Buffer);
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002816
Anders Carlsson76967372009-09-17 00:43:46 +00002817 if (SeqID == 0) *--BufferPtr = '0';
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002818
Anders Carlsson76967372009-09-17 00:43:46 +00002819 while (SeqID) {
2820 assert(BufferPtr > Buffer && "Buffer overflow!");
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002821
John McCall6ab30e02010-06-09 07:26:17 +00002822 char c = static_cast<char>(SeqID % 36);
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002823
Anders Carlsson76967372009-09-17 00:43:46 +00002824 *--BufferPtr = (c < 10 ? '0' + c : 'A' + c - 10);
2825 SeqID /= 36;
2826 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002827
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002828 Out << 'S'
2829 << llvm::StringRef(BufferPtr, llvm::array_endof(Buffer)-BufferPtr)
2830 << '_';
Anders Carlsson76967372009-09-17 00:43:46 +00002831 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002832
Anders Carlsson76967372009-09-17 00:43:46 +00002833 return true;
2834}
2835
Anders Carlssonf514b542009-09-27 00:12:57 +00002836static bool isCharType(QualType T) {
2837 if (T.isNull())
2838 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002839
Anders Carlssonf514b542009-09-27 00:12:57 +00002840 return T->isSpecificBuiltinType(BuiltinType::Char_S) ||
2841 T->isSpecificBuiltinType(BuiltinType::Char_U);
2842}
2843
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002844/// isCharSpecialization - Returns whether a given type is a template
Anders Carlssonf514b542009-09-27 00:12:57 +00002845/// specialization of a given name with a single argument of type char.
2846static bool isCharSpecialization(QualType T, const char *Name) {
2847 if (T.isNull())
2848 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002849
Anders Carlssonf514b542009-09-27 00:12:57 +00002850 const RecordType *RT = T->getAs<RecordType>();
2851 if (!RT)
2852 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002853
2854 const ClassTemplateSpecializationDecl *SD =
Anders Carlssonf514b542009-09-27 00:12:57 +00002855 dyn_cast<ClassTemplateSpecializationDecl>(RT->getDecl());
2856 if (!SD)
2857 return false;
2858
2859 if (!isStdNamespace(SD->getDeclContext()))
2860 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002861
Anders Carlssonf514b542009-09-27 00:12:57 +00002862 const TemplateArgumentList &TemplateArgs = SD->getTemplateArgs();
2863 if (TemplateArgs.size() != 1)
2864 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002865
Anders Carlssonf514b542009-09-27 00:12:57 +00002866 if (!isCharType(TemplateArgs[0].getAsType()))
2867 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002868
Daniel Dunbar01eb9b92009-10-18 21:17:35 +00002869 return SD->getIdentifier()->getName() == Name;
Anders Carlssonf514b542009-09-27 00:12:57 +00002870}
2871
Anders Carlsson91f88602009-12-07 19:56:42 +00002872template <std::size_t StrLen>
Benjamin Kramer54353f42010-11-25 18:29:30 +00002873static bool isStreamCharSpecialization(const ClassTemplateSpecializationDecl*SD,
2874 const char (&Str)[StrLen]) {
Anders Carlsson91f88602009-12-07 19:56:42 +00002875 if (!SD->getIdentifier()->isStr(Str))
2876 return false;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002877
Anders Carlsson91f88602009-12-07 19:56:42 +00002878 const TemplateArgumentList &TemplateArgs = SD->getTemplateArgs();
2879 if (TemplateArgs.size() != 2)
2880 return false;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002881
Anders Carlsson91f88602009-12-07 19:56:42 +00002882 if (!isCharType(TemplateArgs[0].getAsType()))
2883 return false;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002884
Anders Carlsson91f88602009-12-07 19:56:42 +00002885 if (!isCharSpecialization(TemplateArgs[1].getAsType(), "char_traits"))
2886 return false;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002887
Anders Carlsson91f88602009-12-07 19:56:42 +00002888 return true;
2889}
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002890
Anders Carlssone7c8cb62009-09-26 20:53:44 +00002891bool CXXNameMangler::mangleStandardSubstitution(const NamedDecl *ND) {
2892 // <substitution> ::= St # ::std::
Anders Carlsson8c031552009-09-26 23:10:05 +00002893 if (const NamespaceDecl *NS = dyn_cast<NamespaceDecl>(ND)) {
Anders Carlsson47846d22009-12-04 06:23:23 +00002894 if (isStd(NS)) {
Anders Carlsson8c031552009-09-26 23:10:05 +00002895 Out << "St";
2896 return true;
2897 }
2898 }
2899
2900 if (const ClassTemplateDecl *TD = dyn_cast<ClassTemplateDecl>(ND)) {
2901 if (!isStdNamespace(TD->getDeclContext()))
2902 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002903
Anders Carlsson8c031552009-09-26 23:10:05 +00002904 // <substitution> ::= Sa # ::std::allocator
2905 if (TD->getIdentifier()->isStr("allocator")) {
2906 Out << "Sa";
2907 return true;
2908 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002909
Anders Carlsson189d59c2009-09-26 23:14:39 +00002910 // <<substitution> ::= Sb # ::std::basic_string
2911 if (TD->getIdentifier()->isStr("basic_string")) {
2912 Out << "Sb";
2913 return true;
2914 }
Anders Carlsson8c031552009-09-26 23:10:05 +00002915 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002916
2917 if (const ClassTemplateSpecializationDecl *SD =
Anders Carlssonf514b542009-09-27 00:12:57 +00002918 dyn_cast<ClassTemplateSpecializationDecl>(ND)) {
Eli Friedman5370ee22010-02-23 18:25:09 +00002919 if (!isStdNamespace(SD->getDeclContext()))
2920 return false;
2921
Anders Carlssonf514b542009-09-27 00:12:57 +00002922 // <substitution> ::= Ss # ::std::basic_string<char,
2923 // ::std::char_traits<char>,
2924 // ::std::allocator<char> >
2925 if (SD->getIdentifier()->isStr("basic_string")) {
2926 const TemplateArgumentList &TemplateArgs = SD->getTemplateArgs();
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002927
Anders Carlssonf514b542009-09-27 00:12:57 +00002928 if (TemplateArgs.size() != 3)
2929 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002930
Anders Carlssonf514b542009-09-27 00:12:57 +00002931 if (!isCharType(TemplateArgs[0].getAsType()))
2932 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002933
Anders Carlssonf514b542009-09-27 00:12:57 +00002934 if (!isCharSpecialization(TemplateArgs[1].getAsType(), "char_traits"))
2935 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002936
Anders Carlssonf514b542009-09-27 00:12:57 +00002937 if (!isCharSpecialization(TemplateArgs[2].getAsType(), "allocator"))
2938 return false;
2939
2940 Out << "Ss";
2941 return true;
2942 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002943
Anders Carlsson91f88602009-12-07 19:56:42 +00002944 // <substitution> ::= Si # ::std::basic_istream<char,
2945 // ::std::char_traits<char> >
2946 if (isStreamCharSpecialization(SD, "basic_istream")) {
2947 Out << "Si";
2948 return true;
2949 }
2950
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002951 // <substitution> ::= So # ::std::basic_ostream<char,
Anders Carlsson8f8fd8e2009-10-08 17:20:26 +00002952 // ::std::char_traits<char> >
Anders Carlsson91f88602009-12-07 19:56:42 +00002953 if (isStreamCharSpecialization(SD, "basic_ostream")) {
Anders Carlsson8f8fd8e2009-10-08 17:20:26 +00002954 Out << "So";
2955 return true;
2956 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002957
Anders Carlsson91f88602009-12-07 19:56:42 +00002958 // <substitution> ::= Sd # ::std::basic_iostream<char,
2959 // ::std::char_traits<char> >
2960 if (isStreamCharSpecialization(SD, "basic_iostream")) {
2961 Out << "Sd";
2962 return true;
2963 }
Anders Carlssonf514b542009-09-27 00:12:57 +00002964 }
Anders Carlsson8c031552009-09-26 23:10:05 +00002965 return false;
Anders Carlssone7c8cb62009-09-26 20:53:44 +00002966}
2967
Anders Carlsson76967372009-09-17 00:43:46 +00002968void CXXNameMangler::addSubstitution(QualType T) {
Anders Carlssond99edc42009-09-26 03:55:37 +00002969 if (!T.getCVRQualifiers()) {
2970 if (const RecordType *RT = T->getAs<RecordType>()) {
2971 addSubstitution(RT->getDecl());
2972 return;
2973 }
2974 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002975
Anders Carlsson76967372009-09-17 00:43:46 +00002976 uintptr_t TypePtr = reinterpret_cast<uintptr_t>(T.getAsOpaquePtr());
Anders Carlssond3a932a2009-09-17 03:53:28 +00002977 addSubstitution(TypePtr);
2978}
2979
Douglas Gregor1e9268e2010-04-28 05:58:56 +00002980void CXXNameMangler::addSubstitution(TemplateName Template) {
2981 if (TemplateDecl *TD = Template.getAsTemplateDecl())
2982 return addSubstitution(TD);
Sean Huntc3021132010-05-05 15:23:54 +00002983
Douglas Gregor1e9268e2010-04-28 05:58:56 +00002984 Template = Context.getASTContext().getCanonicalTemplateName(Template);
2985 addSubstitution(reinterpret_cast<uintptr_t>(Template.getAsVoidPointer()));
2986}
2987
Anders Carlssond3a932a2009-09-17 03:53:28 +00002988void CXXNameMangler::addSubstitution(uintptr_t Ptr) {
Anders Carlssond3a932a2009-09-17 03:53:28 +00002989 assert(!Substitutions.count(Ptr) && "Substitution already exists!");
Anders Carlsson9d85b722010-06-02 04:29:50 +00002990 Substitutions[Ptr] = SeqID++;
Anders Carlsson76967372009-09-17 00:43:46 +00002991}
2992
Daniel Dunbar1b077112009-11-21 09:06:10 +00002993//
Mike Stump1eb44332009-09-09 15:08:12 +00002994
Daniel Dunbar1b077112009-11-21 09:06:10 +00002995/// \brief Mangles the name of the declaration D and emits that name to the
2996/// given output stream.
2997///
2998/// If the declaration D requires a mangled name, this routine will emit that
2999/// mangled name to \p os and return true. Otherwise, \p os will be unchanged
3000/// and this routine will return false. In this case, the caller should just
3001/// emit the identifier of the declaration (\c D->getIdentifier()) as its
3002/// name.
Peter Collingbourne14110472011-01-13 18:57:25 +00003003void ItaniumMangleContext::mangleName(const NamedDecl *D,
Rafael Espindola0e376a02011-02-11 01:41:00 +00003004 llvm::raw_ostream &Out) {
Daniel Dunbarc02ab4c2009-11-21 09:14:44 +00003005 assert((isa<FunctionDecl>(D) || isa<VarDecl>(D)) &&
3006 "Invalid mangleName() call, argument is not a variable or function!");
3007 assert(!isa<CXXConstructorDecl>(D) && !isa<CXXDestructorDecl>(D) &&
3008 "Invalid mangleName() call on 'structor decl!");
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00003009
Daniel Dunbar1b077112009-11-21 09:06:10 +00003010 PrettyStackTraceDecl CrashInfo(D, SourceLocation(),
3011 getASTContext().getSourceManager(),
3012 "Mangling declaration");
Mike Stump1eb44332009-09-09 15:08:12 +00003013
John McCallfb44de92011-05-01 22:35:37 +00003014 CXXNameMangler Mangler(*this, Out, D);
Daniel Dunbar94fd26d2009-11-21 09:06:22 +00003015 return Mangler.mangle(D);
Daniel Dunbar1b077112009-11-21 09:06:10 +00003016}
Mike Stump1eb44332009-09-09 15:08:12 +00003017
Peter Collingbourne14110472011-01-13 18:57:25 +00003018void ItaniumMangleContext::mangleCXXCtor(const CXXConstructorDecl *D,
3019 CXXCtorType Type,
Rafael Espindola0e376a02011-02-11 01:41:00 +00003020 llvm::raw_ostream &Out) {
Rafael Espindolac4850c22011-02-10 23:59:36 +00003021 CXXNameMangler Mangler(*this, Out, D, Type);
Daniel Dunbar77939c92009-11-21 09:06:31 +00003022 Mangler.mangle(D);
Daniel Dunbar1b077112009-11-21 09:06:10 +00003023}
Mike Stump1eb44332009-09-09 15:08:12 +00003024
Peter Collingbourne14110472011-01-13 18:57:25 +00003025void ItaniumMangleContext::mangleCXXDtor(const CXXDestructorDecl *D,
3026 CXXDtorType Type,
Rafael Espindola0e376a02011-02-11 01:41:00 +00003027 llvm::raw_ostream &Out) {
Rafael Espindolac4850c22011-02-10 23:59:36 +00003028 CXXNameMangler Mangler(*this, Out, D, Type);
Daniel Dunbar77939c92009-11-21 09:06:31 +00003029 Mangler.mangle(D);
Daniel Dunbar1b077112009-11-21 09:06:10 +00003030}
Mike Stumpf1216772009-07-31 18:25:34 +00003031
Peter Collingbourne14110472011-01-13 18:57:25 +00003032void ItaniumMangleContext::mangleThunk(const CXXMethodDecl *MD,
3033 const ThunkInfo &Thunk,
Rafael Espindolaf0be9792011-02-11 02:52:17 +00003034 llvm::raw_ostream &Out) {
Anders Carlsson19879c92010-03-23 17:17:29 +00003035 // <special-name> ::= T <call-offset> <base encoding>
3036 // # base is the nominal target function of thunk
3037 // <special-name> ::= Tc <call-offset> <call-offset> <base encoding>
3038 // # base is the nominal target function of thunk
3039 // # first call-offset is 'this' adjustment
3040 // # second call-offset is result adjustment
Sean Huntc3021132010-05-05 15:23:54 +00003041
Anders Carlsson19879c92010-03-23 17:17:29 +00003042 assert(!isa<CXXDestructorDecl>(MD) &&
3043 "Use mangleCXXDtor for destructor decls!");
Rafael Espindolac4850c22011-02-10 23:59:36 +00003044 CXXNameMangler Mangler(*this, Out);
Anders Carlsson19879c92010-03-23 17:17:29 +00003045 Mangler.getStream() << "_ZT";
3046 if (!Thunk.Return.isEmpty())
3047 Mangler.getStream() << 'c';
Sean Huntc3021132010-05-05 15:23:54 +00003048
Anders Carlsson19879c92010-03-23 17:17:29 +00003049 // Mangle the 'this' pointer adjustment.
3050 Mangler.mangleCallOffset(Thunk.This.NonVirtual, Thunk.This.VCallOffsetOffset);
Sean Huntc3021132010-05-05 15:23:54 +00003051
Anders Carlsson19879c92010-03-23 17:17:29 +00003052 // Mangle the return pointer adjustment if there is one.
3053 if (!Thunk.Return.isEmpty())
3054 Mangler.mangleCallOffset(Thunk.Return.NonVirtual,
3055 Thunk.Return.VBaseOffsetOffset);
Sean Huntc3021132010-05-05 15:23:54 +00003056
Anders Carlsson19879c92010-03-23 17:17:29 +00003057 Mangler.mangleFunctionEncoding(MD);
3058}
3059
Sean Huntc3021132010-05-05 15:23:54 +00003060void
Peter Collingbourne14110472011-01-13 18:57:25 +00003061ItaniumMangleContext::mangleCXXDtorThunk(const CXXDestructorDecl *DD,
3062 CXXDtorType Type,
3063 const ThisAdjustment &ThisAdjustment,
Rafael Espindolaf0be9792011-02-11 02:52:17 +00003064 llvm::raw_ostream &Out) {
Anders Carlsson19879c92010-03-23 17:17:29 +00003065 // <special-name> ::= T <call-offset> <base encoding>
3066 // # base is the nominal target function of thunk
Rafael Espindolac4850c22011-02-10 23:59:36 +00003067 CXXNameMangler Mangler(*this, Out, DD, Type);
Anders Carlsson19879c92010-03-23 17:17:29 +00003068 Mangler.getStream() << "_ZT";
3069
3070 // Mangle the 'this' pointer adjustment.
Sean Huntc3021132010-05-05 15:23:54 +00003071 Mangler.mangleCallOffset(ThisAdjustment.NonVirtual,
Anders Carlsson19879c92010-03-23 17:17:29 +00003072 ThisAdjustment.VCallOffsetOffset);
3073
3074 Mangler.mangleFunctionEncoding(DD);
3075}
3076
Daniel Dunbarc0747712009-11-21 09:12:13 +00003077/// mangleGuardVariable - Returns the mangled name for a guard variable
3078/// for the passed in VarDecl.
Peter Collingbourne14110472011-01-13 18:57:25 +00003079void ItaniumMangleContext::mangleItaniumGuardVariable(const VarDecl *D,
Rafael Espindolaf0be9792011-02-11 02:52:17 +00003080 llvm::raw_ostream &Out) {
Daniel Dunbarc0747712009-11-21 09:12:13 +00003081 // <special-name> ::= GV <object name> # Guard variable for one-time
3082 // # initialization
Rafael Espindolac4850c22011-02-10 23:59:36 +00003083 CXXNameMangler Mangler(*this, Out);
Daniel Dunbarc0747712009-11-21 09:12:13 +00003084 Mangler.getStream() << "_ZGV";
3085 Mangler.mangleName(D);
3086}
3087
Peter Collingbourne14110472011-01-13 18:57:25 +00003088void ItaniumMangleContext::mangleReferenceTemporary(const VarDecl *D,
Rafael Espindolaf0be9792011-02-11 02:52:17 +00003089 llvm::raw_ostream &Out) {
Anders Carlsson715edf22010-06-26 16:09:40 +00003090 // We match the GCC mangling here.
3091 // <special-name> ::= GR <object name>
Rafael Espindolac4850c22011-02-10 23:59:36 +00003092 CXXNameMangler Mangler(*this, Out);
Anders Carlsson715edf22010-06-26 16:09:40 +00003093 Mangler.getStream() << "_ZGR";
3094 Mangler.mangleName(D);
3095}
3096
Peter Collingbourne14110472011-01-13 18:57:25 +00003097void ItaniumMangleContext::mangleCXXVTable(const CXXRecordDecl *RD,
Rafael Espindolaf0be9792011-02-11 02:52:17 +00003098 llvm::raw_ostream &Out) {
Daniel Dunbarc0747712009-11-21 09:12:13 +00003099 // <special-name> ::= TV <type> # virtual table
Rafael Espindolac4850c22011-02-10 23:59:36 +00003100 CXXNameMangler Mangler(*this, Out);
Daniel Dunbarc0747712009-11-21 09:12:13 +00003101 Mangler.getStream() << "_ZTV";
Douglas Gregor1b12a3b2010-05-26 05:11:13 +00003102 Mangler.mangleNameOrStandardSubstitution(RD);
Daniel Dunbar1b077112009-11-21 09:06:10 +00003103}
Mike Stump82d75b02009-11-10 01:58:37 +00003104
Peter Collingbourne14110472011-01-13 18:57:25 +00003105void ItaniumMangleContext::mangleCXXVTT(const CXXRecordDecl *RD,
Rafael Espindolaf0be9792011-02-11 02:52:17 +00003106 llvm::raw_ostream &Out) {
Daniel Dunbarc0747712009-11-21 09:12:13 +00003107 // <special-name> ::= TT <type> # VTT structure
Rafael Espindolac4850c22011-02-10 23:59:36 +00003108 CXXNameMangler Mangler(*this, Out);
Daniel Dunbarc0747712009-11-21 09:12:13 +00003109 Mangler.getStream() << "_ZTT";
Douglas Gregor1b12a3b2010-05-26 05:11:13 +00003110 Mangler.mangleNameOrStandardSubstitution(RD);
Daniel Dunbar1b077112009-11-21 09:06:10 +00003111}
Mike Stumpab3f7e92009-11-10 01:41:59 +00003112
Peter Collingbourne14110472011-01-13 18:57:25 +00003113void ItaniumMangleContext::mangleCXXCtorVTable(const CXXRecordDecl *RD,
3114 int64_t Offset,
3115 const CXXRecordDecl *Type,
Rafael Espindolaf0be9792011-02-11 02:52:17 +00003116 llvm::raw_ostream &Out) {
Daniel Dunbarc0747712009-11-21 09:12:13 +00003117 // <special-name> ::= TC <type> <offset number> _ <base type>
Rafael Espindolac4850c22011-02-10 23:59:36 +00003118 CXXNameMangler Mangler(*this, Out);
Daniel Dunbarc0747712009-11-21 09:12:13 +00003119 Mangler.getStream() << "_ZTC";
Douglas Gregor1b12a3b2010-05-26 05:11:13 +00003120 Mangler.mangleNameOrStandardSubstitution(RD);
Daniel Dunbarc0747712009-11-21 09:12:13 +00003121 Mangler.getStream() << Offset;
Benjamin Kramer35f59b62010-04-10 16:03:31 +00003122 Mangler.getStream() << '_';
Douglas Gregor1b12a3b2010-05-26 05:11:13 +00003123 Mangler.mangleNameOrStandardSubstitution(Type);
Daniel Dunbar1b077112009-11-21 09:06:10 +00003124}
Mike Stump738f8c22009-07-31 23:15:31 +00003125
Peter Collingbourne14110472011-01-13 18:57:25 +00003126void ItaniumMangleContext::mangleCXXRTTI(QualType Ty,
Rafael Espindolaf0be9792011-02-11 02:52:17 +00003127 llvm::raw_ostream &Out) {
Daniel Dunbarc0747712009-11-21 09:12:13 +00003128 // <special-name> ::= TI <type> # typeinfo structure
Douglas Gregor154fe982009-12-23 22:04:40 +00003129 assert(!Ty.hasQualifiers() && "RTTI info cannot have top-level qualifiers");
Rafael Espindolac4850c22011-02-10 23:59:36 +00003130 CXXNameMangler Mangler(*this, Out);
Daniel Dunbarc0747712009-11-21 09:12:13 +00003131 Mangler.getStream() << "_ZTI";
3132 Mangler.mangleType(Ty);
Daniel Dunbar1b077112009-11-21 09:06:10 +00003133}
Mike Stump67795982009-11-14 00:14:13 +00003134
Peter Collingbourne14110472011-01-13 18:57:25 +00003135void ItaniumMangleContext::mangleCXXRTTIName(QualType Ty,
Rafael Espindolaf0be9792011-02-11 02:52:17 +00003136 llvm::raw_ostream &Out) {
Daniel Dunbarc0747712009-11-21 09:12:13 +00003137 // <special-name> ::= TS <type> # typeinfo name (null terminated byte string)
Rafael Espindolac4850c22011-02-10 23:59:36 +00003138 CXXNameMangler Mangler(*this, Out);
Daniel Dunbarc0747712009-11-21 09:12:13 +00003139 Mangler.getStream() << "_ZTS";
3140 Mangler.mangleType(Ty);
Mike Stumpf1216772009-07-31 18:25:34 +00003141}
Peter Collingbourne14110472011-01-13 18:57:25 +00003142
3143MangleContext *clang::createItaniumMangleContext(ASTContext &Context,
3144 Diagnostic &Diags) {
3145 return new ItaniumMangleContext(Context, Diags);
3146}