blob: 37ae62668365dd85d36c270e097d91892d449b66 [file] [log] [blame]
Peter Collingbourne14110472011-01-13 18:57:25 +00001//===--- ItaniumMangle.cpp - Itanium C++ Name Mangling ----------*- C++ -*-===//
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// Implements C++ name mangling according to the Itanium C++ ABI,
11// which is used in GCC 3.2 and newer (and many compilers that are
12// ABI-compatible with GCC):
13//
14// http://www.codesourcery.com/public/cxx-abi/abi.html
15//
16//===----------------------------------------------------------------------===//
Peter Collingbourne14110472011-01-13 18:57:25 +000017#include "clang/AST/Mangle.h"
Douglas Gregor5f2bfd42009-02-13 00:10:09 +000018#include "clang/AST/ASTContext.h"
19#include "clang/AST/Decl.h"
20#include "clang/AST/DeclCXX.h"
Anders Carlssona40c5e42009-03-07 22:03:21 +000021#include "clang/AST/DeclObjC.h"
Anders Carlsson7a0ba872009-05-15 16:09:15 +000022#include "clang/AST/DeclTemplate.h"
Anders Carlsson50755b02009-09-27 20:11:34 +000023#include "clang/AST/ExprCXX.h"
John McCallf85e1932011-06-15 23:02:42 +000024#include "clang/AST/ExprObjC.h"
John McCallfb44de92011-05-01 22:35:37 +000025#include "clang/AST/TypeLoc.h"
Peter Collingbourne14110472011-01-13 18:57:25 +000026#include "clang/Basic/ABI.h"
Benjamin Kramer00bd44d2012-02-04 12:31:12 +000027#include "clang/Basic/Diagnostic.h"
Douglas Gregor6ec36682009-02-18 23:53:56 +000028#include "clang/Basic/SourceManager.h"
Rafael Espindola4e274e92011-02-15 22:23:51 +000029#include "clang/Basic/TargetInfo.h"
Anders Carlssonc4355b62009-10-07 01:45:02 +000030#include "llvm/ADT/StringExtras.h"
Douglas Gregor5f2bfd42009-02-13 00:10:09 +000031#include "llvm/Support/raw_ostream.h"
John McCallefe6aee2009-09-05 07:56:18 +000032#include "llvm/Support/ErrorHandling.h"
Anders Carlssonf98574b2010-02-05 07:31:37 +000033
34#define MANGLE_CHECKER 0
35
36#if MANGLE_CHECKER
37#include <cxxabi.h>
38#endif
39
Douglas Gregor5f2bfd42009-02-13 00:10:09 +000040using namespace clang;
Charles Davis685b1d92010-05-26 18:25:27 +000041
Douglas Gregor5f2bfd42009-02-13 00:10:09 +000042namespace {
Fariborz Jahanian57058532010-03-03 19:41:08 +000043
John McCall82b7d7b2010-10-18 21:28:44 +000044static const CXXRecordDecl *GetLocalClassDecl(const NamedDecl *ND) {
45 const DeclContext *DC = dyn_cast<DeclContext>(ND);
46 if (!DC)
47 DC = ND->getDeclContext();
48 while (!DC->isNamespace() && !DC->isTranslationUnit()) {
49 if (isa<FunctionDecl>(DC->getParent()))
50 return dyn_cast<CXXRecordDecl>(DC);
51 DC = DC->getParent();
Fariborz Jahanian57058532010-03-03 19:41:08 +000052 }
53 return 0;
54}
55
John McCallfb44de92011-05-01 22:35:37 +000056static const FunctionDecl *getStructor(const FunctionDecl *fn) {
57 if (const FunctionTemplateDecl *ftd = fn->getPrimaryTemplate())
58 return ftd->getTemplatedDecl();
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +000059
John McCallfb44de92011-05-01 22:35:37 +000060 return fn;
61}
Anders Carlsson7e120032009-11-24 05:36:32 +000062
John McCallfb44de92011-05-01 22:35:37 +000063static const NamedDecl *getStructor(const NamedDecl *decl) {
64 const FunctionDecl *fn = dyn_cast_or_null<FunctionDecl>(decl);
65 return (fn ? getStructor(fn) : decl);
Anders Carlsson7e120032009-11-24 05:36:32 +000066}
John McCall1dd73832010-02-04 01:42:13 +000067
68static const unsigned UnknownArity = ~0U;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +000069
Peter Collingbourne14110472011-01-13 18:57:25 +000070class ItaniumMangleContext : public MangleContext {
71 llvm::DenseMap<const TagDecl *, uint64_t> AnonStructIds;
72 unsigned Discriminator;
73 llvm::DenseMap<const NamedDecl*, unsigned> Uniquifier;
74
75public:
76 explicit ItaniumMangleContext(ASTContext &Context,
David Blaikied6471f72011-09-25 23:23:43 +000077 DiagnosticsEngine &Diags)
Peter Collingbourne14110472011-01-13 18:57:25 +000078 : MangleContext(Context, Diags) { }
79
80 uint64_t getAnonymousStructId(const TagDecl *TD) {
81 std::pair<llvm::DenseMap<const TagDecl *,
82 uint64_t>::iterator, bool> Result =
83 AnonStructIds.insert(std::make_pair(TD, AnonStructIds.size()));
84 return Result.first->second;
85 }
86
87 void startNewFunction() {
88 MangleContext::startNewFunction();
89 mangleInitDiscriminator();
90 }
91
92 /// @name Mangler Entry Points
93 /// @{
94
95 bool shouldMangleDeclName(const NamedDecl *D);
Chris Lattner5f9e2722011-07-23 10:55:15 +000096 void mangleName(const NamedDecl *D, raw_ostream &);
Peter Collingbourne14110472011-01-13 18:57:25 +000097 void mangleThunk(const CXXMethodDecl *MD,
98 const ThunkInfo &Thunk,
Chris Lattner5f9e2722011-07-23 10:55:15 +000099 raw_ostream &);
Peter Collingbourne14110472011-01-13 18:57:25 +0000100 void mangleCXXDtorThunk(const CXXDestructorDecl *DD, CXXDtorType Type,
101 const ThisAdjustment &ThisAdjustment,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000102 raw_ostream &);
Peter Collingbourne14110472011-01-13 18:57:25 +0000103 void mangleReferenceTemporary(const VarDecl *D,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000104 raw_ostream &);
Peter Collingbourne14110472011-01-13 18:57:25 +0000105 void mangleCXXVTable(const CXXRecordDecl *RD,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000106 raw_ostream &);
Peter Collingbourne14110472011-01-13 18:57:25 +0000107 void mangleCXXVTT(const CXXRecordDecl *RD,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000108 raw_ostream &);
Peter Collingbourne14110472011-01-13 18:57:25 +0000109 void mangleCXXCtorVTable(const CXXRecordDecl *RD, int64_t Offset,
110 const CXXRecordDecl *Type,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000111 raw_ostream &);
112 void mangleCXXRTTI(QualType T, raw_ostream &);
113 void mangleCXXRTTIName(QualType T, raw_ostream &);
Peter Collingbourne14110472011-01-13 18:57:25 +0000114 void mangleCXXCtor(const CXXConstructorDecl *D, CXXCtorType Type,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000115 raw_ostream &);
Peter Collingbourne14110472011-01-13 18:57:25 +0000116 void mangleCXXDtor(const CXXDestructorDecl *D, CXXDtorType Type,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000117 raw_ostream &);
Peter Collingbourne14110472011-01-13 18:57:25 +0000118
Chris Lattner5f9e2722011-07-23 10:55:15 +0000119 void mangleItaniumGuardVariable(const VarDecl *D, raw_ostream &);
Peter Collingbourne14110472011-01-13 18:57:25 +0000120
121 void mangleInitDiscriminator() {
122 Discriminator = 0;
123 }
124
125 bool getNextDiscriminator(const NamedDecl *ND, unsigned &disc) {
126 unsigned &discriminator = Uniquifier[ND];
127 if (!discriminator)
128 discriminator = ++Discriminator;
129 if (discriminator == 1)
130 return false;
131 disc = discriminator-2;
132 return true;
133 }
134 /// @}
135};
136
Daniel Dunbar1b077112009-11-21 09:06:10 +0000137/// CXXNameMangler - Manage the mangling of a single name.
Daniel Dunbarc0747712009-11-21 09:12:13 +0000138class CXXNameMangler {
Peter Collingbourne14110472011-01-13 18:57:25 +0000139 ItaniumMangleContext &Context;
Chris Lattner5f9e2722011-07-23 10:55:15 +0000140 raw_ostream &Out;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000141
John McCallfb44de92011-05-01 22:35:37 +0000142 /// The "structor" is the top-level declaration being mangled, if
143 /// that's not a template specialization; otherwise it's the pattern
144 /// for that specialization.
145 const NamedDecl *Structor;
Daniel Dunbar1b077112009-11-21 09:06:10 +0000146 unsigned StructorType;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000147
Anders Carlsson9d85b722010-06-02 04:29:50 +0000148 /// SeqID - The next subsitution sequence number.
149 unsigned SeqID;
150
John McCallfb44de92011-05-01 22:35:37 +0000151 class FunctionTypeDepthState {
152 unsigned Bits;
153
154 enum { InResultTypeMask = 1 };
155
156 public:
157 FunctionTypeDepthState() : Bits(0) {}
158
159 /// The number of function types we're inside.
160 unsigned getDepth() const {
161 return Bits >> 1;
162 }
163
164 /// True if we're in the return type of the innermost function type.
165 bool isInResultType() const {
166 return Bits & InResultTypeMask;
167 }
168
169 FunctionTypeDepthState push() {
170 FunctionTypeDepthState tmp = *this;
171 Bits = (Bits & ~InResultTypeMask) + 2;
172 return tmp;
173 }
174
175 void enterResultType() {
176 Bits |= InResultTypeMask;
177 }
178
179 void leaveResultType() {
180 Bits &= ~InResultTypeMask;
181 }
182
183 void pop(FunctionTypeDepthState saved) {
184 assert(getDepth() == saved.getDepth() + 1);
185 Bits = saved.Bits;
186 }
187
188 } FunctionTypeDepth;
189
Daniel Dunbar1b077112009-11-21 09:06:10 +0000190 llvm::DenseMap<uintptr_t, unsigned> Substitutions;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000191
John McCall1dd73832010-02-04 01:42:13 +0000192 ASTContext &getASTContext() const { return Context.getASTContext(); }
193
Daniel Dunbarc0747712009-11-21 09:12:13 +0000194public:
Chris Lattner5f9e2722011-07-23 10:55:15 +0000195 CXXNameMangler(ItaniumMangleContext &C, raw_ostream &Out_,
John McCallfb44de92011-05-01 22:35:37 +0000196 const NamedDecl *D = 0)
197 : Context(C), Out(Out_), Structor(getStructor(D)), StructorType(0),
198 SeqID(0) {
199 // These can't be mangled without a ctor type or dtor type.
200 assert(!D || (!isa<CXXDestructorDecl>(D) &&
201 !isa<CXXConstructorDecl>(D)));
202 }
Chris Lattner5f9e2722011-07-23 10:55:15 +0000203 CXXNameMangler(ItaniumMangleContext &C, raw_ostream &Out_,
Daniel Dunbar77939c92009-11-21 09:06:31 +0000204 const CXXConstructorDecl *D, CXXCtorType Type)
Rafael Espindolac4850c22011-02-10 23:59:36 +0000205 : Context(C), Out(Out_), Structor(getStructor(D)), StructorType(Type),
John McCallfb44de92011-05-01 22:35:37 +0000206 SeqID(0) { }
Chris Lattner5f9e2722011-07-23 10:55:15 +0000207 CXXNameMangler(ItaniumMangleContext &C, raw_ostream &Out_,
Daniel Dunbar77939c92009-11-21 09:06:31 +0000208 const CXXDestructorDecl *D, CXXDtorType Type)
Rafael Espindolac4850c22011-02-10 23:59:36 +0000209 : Context(C), Out(Out_), Structor(getStructor(D)), StructorType(Type),
John McCallfb44de92011-05-01 22:35:37 +0000210 SeqID(0) { }
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000211
Anders Carlssonf98574b2010-02-05 07:31:37 +0000212#if MANGLE_CHECKER
213 ~CXXNameMangler() {
214 if (Out.str()[0] == '\01')
215 return;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000216
Anders Carlssonf98574b2010-02-05 07:31:37 +0000217 int status = 0;
218 char *result = abi::__cxa_demangle(Out.str().str().c_str(), 0, 0, &status);
219 assert(status == 0 && "Could not demangle mangled name!");
220 free(result);
221 }
222#endif
Chris Lattner5f9e2722011-07-23 10:55:15 +0000223 raw_ostream &getStream() { return Out; }
Daniel Dunbarc0747712009-11-21 09:12:13 +0000224
Chris Lattner5f9e2722011-07-23 10:55:15 +0000225 void mangle(const NamedDecl *D, StringRef Prefix = "_Z");
Anders Carlsson19879c92010-03-23 17:17:29 +0000226 void mangleCallOffset(int64_t NonVirtual, int64_t Virtual);
John McCall0512e482010-07-14 04:20:34 +0000227 void mangleNumber(const llvm::APSInt &I);
Anders Carlssona94822e2009-11-26 02:32:05 +0000228 void mangleNumber(int64_t Number);
John McCall0512e482010-07-14 04:20:34 +0000229 void mangleFloat(const llvm::APFloat &F);
Daniel Dunbarc0747712009-11-21 09:12:13 +0000230 void mangleFunctionEncoding(const FunctionDecl *FD);
231 void mangleName(const NamedDecl *ND);
232 void mangleType(QualType T);
Douglas Gregor1b12a3b2010-05-26 05:11:13 +0000233 void mangleNameOrStandardSubstitution(const NamedDecl *ND);
234
Daniel Dunbarc0747712009-11-21 09:12:13 +0000235private:
Daniel Dunbar1b077112009-11-21 09:06:10 +0000236 bool mangleSubstitution(const NamedDecl *ND);
237 bool mangleSubstitution(QualType T);
Douglas Gregor1e9268e2010-04-28 05:58:56 +0000238 bool mangleSubstitution(TemplateName Template);
Daniel Dunbar1b077112009-11-21 09:06:10 +0000239 bool mangleSubstitution(uintptr_t Ptr);
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000240
John McCall68a51a72011-07-01 00:04:39 +0000241 void mangleExistingSubstitution(QualType type);
242 void mangleExistingSubstitution(TemplateName name);
243
Daniel Dunbar1b077112009-11-21 09:06:10 +0000244 bool mangleStandardSubstitution(const NamedDecl *ND);
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000245
Daniel Dunbar1b077112009-11-21 09:06:10 +0000246 void addSubstitution(const NamedDecl *ND) {
247 ND = cast<NamedDecl>(ND->getCanonicalDecl());
Anders Carlsson433d1372009-11-07 04:26:04 +0000248
Daniel Dunbar1b077112009-11-21 09:06:10 +0000249 addSubstitution(reinterpret_cast<uintptr_t>(ND));
250 }
251 void addSubstitution(QualType T);
Douglas Gregor1e9268e2010-04-28 05:58:56 +0000252 void addSubstitution(TemplateName Template);
Daniel Dunbar1b077112009-11-21 09:06:10 +0000253 void addSubstitution(uintptr_t Ptr);
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000254
John McCalla0ce15c2011-04-24 08:23:24 +0000255 void mangleUnresolvedPrefix(NestedNameSpecifier *qualifier,
256 NamedDecl *firstQualifierLookup,
257 bool recursive = false);
258 void mangleUnresolvedName(NestedNameSpecifier *qualifier,
259 NamedDecl *firstQualifierLookup,
260 DeclarationName name,
John McCall1dd73832010-02-04 01:42:13 +0000261 unsigned KnownArity = UnknownArity);
262
Daniel Dunbar1b077112009-11-21 09:06:10 +0000263 void mangleName(const TemplateDecl *TD,
264 const TemplateArgument *TemplateArgs,
265 unsigned NumTemplateArgs);
John McCall1dd73832010-02-04 01:42:13 +0000266 void mangleUnqualifiedName(const NamedDecl *ND) {
267 mangleUnqualifiedName(ND, ND->getDeclName(), UnknownArity);
268 }
269 void mangleUnqualifiedName(const NamedDecl *ND, DeclarationName Name,
270 unsigned KnownArity);
Daniel Dunbar1b077112009-11-21 09:06:10 +0000271 void mangleUnscopedName(const NamedDecl *ND);
272 void mangleUnscopedTemplateName(const TemplateDecl *ND);
Douglas Gregor1e9268e2010-04-28 05:58:56 +0000273 void mangleUnscopedTemplateName(TemplateName);
Daniel Dunbar1b077112009-11-21 09:06:10 +0000274 void mangleSourceName(const IdentifierInfo *II);
275 void mangleLocalName(const NamedDecl *ND);
Fariborz Jahanian57058532010-03-03 19:41:08 +0000276 void mangleNestedName(const NamedDecl *ND, const DeclContext *DC,
277 bool NoFunction=false);
Daniel Dunbar1b077112009-11-21 09:06:10 +0000278 void mangleNestedName(const TemplateDecl *TD,
279 const TemplateArgument *TemplateArgs,
280 unsigned NumTemplateArgs);
John McCalla0ce15c2011-04-24 08:23:24 +0000281 void manglePrefix(NestedNameSpecifier *qualifier);
Fariborz Jahanian57058532010-03-03 19:41:08 +0000282 void manglePrefix(const DeclContext *DC, bool NoFunction=false);
John McCall4f4e4132011-05-04 01:45:19 +0000283 void manglePrefix(QualType type);
Daniel Dunbar1b077112009-11-21 09:06:10 +0000284 void mangleTemplatePrefix(const TemplateDecl *ND);
Douglas Gregor20f0cc72010-04-23 03:10:43 +0000285 void mangleTemplatePrefix(TemplateName Template);
Daniel Dunbar1b077112009-11-21 09:06:10 +0000286 void mangleOperatorName(OverloadedOperatorKind OO, unsigned Arity);
287 void mangleQualifiers(Qualifiers Quals);
Douglas Gregor0a9a6d62011-01-26 17:36:28 +0000288 void mangleRefQualifier(RefQualifierKind RefQualifier);
John McCallefe6aee2009-09-05 07:56:18 +0000289
Anders Carlsson7b06f6c2009-12-10 03:14:39 +0000290 void mangleObjCMethodName(const ObjCMethodDecl *MD);
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000291
Daniel Dunbar1b077112009-11-21 09:06:10 +0000292 // Declare manglers for every type class.
John McCallefe6aee2009-09-05 07:56:18 +0000293#define ABSTRACT_TYPE(CLASS, PARENT)
294#define NON_CANONICAL_TYPE(CLASS, PARENT)
295#define TYPE(CLASS, PARENT) void mangleType(const CLASS##Type *T);
296#include "clang/AST/TypeNodes.def"
297
Daniel Dunbar1b077112009-11-21 09:06:10 +0000298 void mangleType(const TagType*);
John McCallb6f532e2010-07-14 06:43:17 +0000299 void mangleType(TemplateName);
Daniel Dunbar1b077112009-11-21 09:06:10 +0000300 void mangleBareFunctionType(const FunctionType *T,
301 bool MangleReturnType);
Bob Wilson57147a82010-11-16 00:32:18 +0000302 void mangleNeonVectorType(const VectorType *T);
Anders Carlssone170ba72009-12-14 01:45:37 +0000303
304 void mangleIntegerLiteral(QualType T, const llvm::APSInt &Value);
John McCalla0ce15c2011-04-24 08:23:24 +0000305 void mangleMemberExpr(const Expr *base, bool isArrow,
306 NestedNameSpecifier *qualifier,
307 NamedDecl *firstQualifierLookup,
308 DeclarationName name,
309 unsigned knownArity);
John McCall5e1e89b2010-08-18 19:18:59 +0000310 void mangleExpression(const Expr *E, unsigned Arity = UnknownArity);
Daniel Dunbar1b077112009-11-21 09:06:10 +0000311 void mangleCXXCtorType(CXXCtorType T);
312 void mangleCXXDtorType(CXXDtorType T);
Mike Stump1eb44332009-09-09 15:08:12 +0000313
Argyrios Kyrtzidisb0c3e092011-09-22 20:07:03 +0000314 void mangleTemplateArgs(const ASTTemplateArgumentListInfo &TemplateArgs);
Douglas Gregor20f0cc72010-04-23 03:10:43 +0000315 void mangleTemplateArgs(TemplateName Template,
316 const TemplateArgument *TemplateArgs,
Sean Huntc3021132010-05-05 15:23:54 +0000317 unsigned NumTemplateArgs);
Rafael Espindolad9800722010-03-11 14:07:00 +0000318 void mangleTemplateArgs(const TemplateParameterList &PL,
319 const TemplateArgument *TemplateArgs,
Daniel Dunbar1b077112009-11-21 09:06:10 +0000320 unsigned NumTemplateArgs);
Rafael Espindolad9800722010-03-11 14:07:00 +0000321 void mangleTemplateArgs(const TemplateParameterList &PL,
322 const TemplateArgumentList &AL);
Douglas Gregorf1588662011-07-12 15:18:55 +0000323 void mangleTemplateArg(const NamedDecl *P, TemplateArgument A);
John McCall4f4e4132011-05-04 01:45:19 +0000324 void mangleUnresolvedTemplateArgs(const TemplateArgument *args,
325 unsigned numArgs);
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000326
Daniel Dunbar1b077112009-11-21 09:06:10 +0000327 void mangleTemplateParameter(unsigned Index);
John McCallfb44de92011-05-01 22:35:37 +0000328
329 void mangleFunctionParam(const ParmVarDecl *parm);
Daniel Dunbar1b077112009-11-21 09:06:10 +0000330};
Peter Collingbourne14110472011-01-13 18:57:25 +0000331
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000332}
333
Anders Carlsson43f17402009-04-02 15:51:53 +0000334static bool isInCLinkageSpecification(const Decl *D) {
Douglas Gregor457e2812009-10-28 16:31:34 +0000335 D = D->getCanonicalDecl();
Mike Stump1eb44332009-09-09 15:08:12 +0000336 for (const DeclContext *DC = D->getDeclContext();
Anders Carlsson43f17402009-04-02 15:51:53 +0000337 !DC->isTranslationUnit(); DC = DC->getParent()) {
Mike Stump1eb44332009-09-09 15:08:12 +0000338 if (const LinkageSpecDecl *Linkage = dyn_cast<LinkageSpecDecl>(DC))
Anders Carlsson43f17402009-04-02 15:51:53 +0000339 return Linkage->getLanguage() == LinkageSpecDecl::lang_c;
340 }
Mike Stump1eb44332009-09-09 15:08:12 +0000341
Anders Carlsson43f17402009-04-02 15:51:53 +0000342 return false;
343}
344
Peter Collingbourne14110472011-01-13 18:57:25 +0000345bool ItaniumMangleContext::shouldMangleDeclName(const NamedDecl *D) {
Daniel Dunbarf981bf82009-11-21 09:14:52 +0000346 // In C, functions with no attributes never need to be mangled. Fastpath them.
347 if (!getASTContext().getLangOptions().CPlusPlus && !D->hasAttrs())
348 return false;
349
350 // Any decl can be declared with __asm("foo") on it, and this takes precedence
351 // over all other naming in the .o file.
352 if (D->hasAttr<AsmLabelAttr>())
353 return true;
354
Mike Stump141c5af2009-09-02 00:25:38 +0000355 // Clang's "overloadable" attribute extension to C/C++ implies name mangling
Anders Carlssona1e16222009-11-07 07:15:03 +0000356 // (always) as does passing a C++ member function and a function
357 // whose name is not a simple identifier.
Daniel Dunbarf981bf82009-11-21 09:14:52 +0000358 const FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
359 if (FD && (FD->hasAttr<OverloadableAttr>() || isa<CXXMethodDecl>(FD) ||
360 !FD->getDeclName().isIdentifier()))
361 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000362
Daniel Dunbarf981bf82009-11-21 09:14:52 +0000363 // Otherwise, no mangling is done outside C++ mode.
364 if (!getASTContext().getLangOptions().CPlusPlus)
365 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000366
Sean Hunt31455252010-01-24 03:04:27 +0000367 // Variables at global scope with non-internal linkage are not mangled
Eli Friedman7facf842009-12-02 20:32:49 +0000368 if (!FD) {
369 const DeclContext *DC = D->getDeclContext();
370 // Check for extern variable declared locally.
Fariborz Jahaniane81c5612010-06-30 18:57:21 +0000371 if (DC->isFunctionOrMethod() && D->hasLinkage())
Eli Friedman7facf842009-12-02 20:32:49 +0000372 while (!DC->isNamespace() && !DC->isTranslationUnit())
373 DC = DC->getParent();
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000374 if (DC->isTranslationUnit() && D->getLinkage() != InternalLinkage)
Eli Friedman7facf842009-12-02 20:32:49 +0000375 return false;
376 }
377
Eli Friedmanc00cb642010-07-18 20:49:59 +0000378 // Class members are always mangled.
379 if (D->getDeclContext()->isRecord())
380 return true;
381
Eli Friedman7facf842009-12-02 20:32:49 +0000382 // C functions and "main" are not mangled.
383 if ((FD && FD->isMain()) || isInCLinkageSpecification(D))
Daniel Dunbarf981bf82009-11-21 09:14:52 +0000384 return false;
385
Anders Carlsson43f17402009-04-02 15:51:53 +0000386 return true;
387}
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000388
Chris Lattner5f9e2722011-07-23 10:55:15 +0000389void CXXNameMangler::mangle(const NamedDecl *D, StringRef Prefix) {
Mike Stump141c5af2009-09-02 00:25:38 +0000390 // Any decl can be declared with __asm("foo") on it, and this takes precedence
391 // over all other naming in the .o file.
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000392 if (const AsmLabelAttr *ALA = D->getAttr<AsmLabelAttr>()) {
Chris Lattnerca3f25c2009-03-21 08:24:40 +0000393 // If we have an asm name, then we use it as the mangling.
Rafael Espindola4e274e92011-02-15 22:23:51 +0000394
395 // Adding the prefix can cause problems when one file has a "foo" and
396 // another has a "\01foo". That is known to happen on ELF with the
397 // tricks normally used for producing aliases (PR9177). Fortunately the
398 // llvm mangler on ELF is a nop, so we can just avoid adding the \01
Peter Collingbourne69317432011-04-06 12:29:09 +0000399 // marker. We also avoid adding the marker if this is an alias for an
400 // LLVM intrinsic.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000401 StringRef UserLabelPrefix =
Douglas Gregorbcfd1f52011-09-02 00:18:52 +0000402 getASTContext().getTargetInfo().getUserLabelPrefix();
Peter Collingbourne69317432011-04-06 12:29:09 +0000403 if (!UserLabelPrefix.empty() && !ALA->getLabel().startswith("llvm."))
Rafael Espindola4e274e92011-02-15 22:23:51 +0000404 Out << '\01'; // LLVM IR Marker for __asm("foo")
405
Chris Lattnerca3f25c2009-03-21 08:24:40 +0000406 Out << ALA->getLabel();
Daniel Dunbarf981bf82009-11-21 09:14:52 +0000407 return;
Chris Lattnerca3f25c2009-03-21 08:24:40 +0000408 }
Mike Stump1eb44332009-09-09 15:08:12 +0000409
Sean Hunt31455252010-01-24 03:04:27 +0000410 // <mangled-name> ::= _Z <encoding>
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000411 // ::= <data name>
412 // ::= <special-name>
Daniel Dunbar7e0c1952009-11-21 09:17:15 +0000413 Out << Prefix;
414 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
Daniel Dunbarf981bf82009-11-21 09:14:52 +0000415 mangleFunctionEncoding(FD);
Rafael Espindolad9800722010-03-11 14:07:00 +0000416 else if (const VarDecl *VD = dyn_cast<VarDecl>(D))
417 mangleName(VD);
Daniel Dunbar7e0c1952009-11-21 09:17:15 +0000418 else
Rafael Espindolad9800722010-03-11 14:07:00 +0000419 mangleName(cast<FieldDecl>(D));
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000420}
421
422void CXXNameMangler::mangleFunctionEncoding(const FunctionDecl *FD) {
423 // <encoding> ::= <function name> <bare-function-type>
424 mangleName(FD);
Mike Stump1eb44332009-09-09 15:08:12 +0000425
Daniel Dunbar7e0c1952009-11-21 09:17:15 +0000426 // Don't mangle in the type if this isn't a decl we should typically mangle.
427 if (!Context.shouldMangleDeclName(FD))
428 return;
429
Mike Stump141c5af2009-09-02 00:25:38 +0000430 // Whether the mangling of a function type includes the return type depends on
431 // the context and the nature of the function. The rules for deciding whether
432 // the return type is included are:
Mike Stump1eb44332009-09-09 15:08:12 +0000433 //
Douglas Gregor1fd2dd12009-06-29 22:39:32 +0000434 // 1. Template functions (names or types) have return types encoded, with
435 // the exceptions listed below.
Mike Stump1eb44332009-09-09 15:08:12 +0000436 // 2. Function types not appearing as part of a function name mangling,
Douglas Gregor1fd2dd12009-06-29 22:39:32 +0000437 // e.g. parameters, pointer types, etc., have return type encoded, with the
438 // exceptions listed below.
439 // 3. Non-template function names do not have return types encoded.
440 //
Mike Stump141c5af2009-09-02 00:25:38 +0000441 // The exceptions mentioned in (1) and (2) above, for which the return type is
442 // never included, are
Douglas Gregor1fd2dd12009-06-29 22:39:32 +0000443 // 1. Constructors.
444 // 2. Destructors.
445 // 3. Conversion operator functions, e.g. operator int.
446 bool MangleReturnType = false;
Anders Carlsson9234b7f2009-09-17 03:46:43 +0000447 if (FunctionTemplateDecl *PrimaryTemplate = FD->getPrimaryTemplate()) {
448 if (!(isa<CXXConstructorDecl>(FD) || isa<CXXDestructorDecl>(FD) ||
449 isa<CXXConversionDecl>(FD)))
450 MangleReturnType = true;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000451
Anders Carlsson9234b7f2009-09-17 03:46:43 +0000452 // Mangle the type of the primary template.
453 FD = PrimaryTemplate->getTemplatedDecl();
454 }
455
Douglas Gregor79e6bd32011-07-12 04:42:08 +0000456 mangleBareFunctionType(FD->getType()->getAs<FunctionType>(),
457 MangleReturnType);
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000458}
459
Anders Carlsson47846d22009-12-04 06:23:23 +0000460static const DeclContext *IgnoreLinkageSpecDecls(const DeclContext *DC) {
461 while (isa<LinkageSpecDecl>(DC)) {
Anders Carlsson47846d22009-12-04 06:23:23 +0000462 DC = DC->getParent();
463 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000464
Anders Carlsson47846d22009-12-04 06:23:23 +0000465 return DC;
466}
467
Anders Carlssonc820f902010-06-02 15:58:27 +0000468/// isStd - Return whether a given namespace is the 'std' namespace.
469static bool isStd(const NamespaceDecl *NS) {
470 if (!IgnoreLinkageSpecDecls(NS->getParent())->isTranslationUnit())
471 return false;
472
473 const IdentifierInfo *II = NS->getOriginalNamespace()->getIdentifier();
474 return II && II->isStr("std");
475}
476
Anders Carlsson47846d22009-12-04 06:23:23 +0000477// isStdNamespace - Return whether a given decl context is a toplevel 'std'
478// namespace.
Daniel Dunbar1308af92009-11-21 09:11:45 +0000479static bool isStdNamespace(const DeclContext *DC) {
Anders Carlsson47846d22009-12-04 06:23:23 +0000480 if (!DC->isNamespace())
481 return false;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000482
Anders Carlsson47846d22009-12-04 06:23:23 +0000483 return isStd(cast<NamespaceDecl>(DC));
Daniel Dunbar1308af92009-11-21 09:11:45 +0000484}
485
Anders Carlssonbb36ba42009-09-26 03:24:57 +0000486static const TemplateDecl *
487isTemplate(const NamedDecl *ND, const TemplateArgumentList *&TemplateArgs) {
Anders Carlsson2744a062009-09-18 19:00:18 +0000488 // Check if we have a function template.
489 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)){
Anders Carlssonbb36ba42009-09-26 03:24:57 +0000490 if (const TemplateDecl *TD = FD->getPrimaryTemplate()) {
Anders Carlsson2744a062009-09-18 19:00:18 +0000491 TemplateArgs = FD->getTemplateSpecializationArgs();
Anders Carlssonbb36ba42009-09-26 03:24:57 +0000492 return TD;
Anders Carlsson2744a062009-09-18 19:00:18 +0000493 }
494 }
495
Anders Carlssoneafc6dc2009-09-18 19:44:50 +0000496 // Check if we have a class template.
497 if (const ClassTemplateSpecializationDecl *Spec =
498 dyn_cast<ClassTemplateSpecializationDecl>(ND)) {
499 TemplateArgs = &Spec->getTemplateArgs();
Anders Carlssonbb36ba42009-09-26 03:24:57 +0000500 return Spec->getSpecializedTemplate();
Anders Carlssoneafc6dc2009-09-18 19:44:50 +0000501 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000502
Anders Carlsson2744a062009-09-18 19:00:18 +0000503 return 0;
504}
505
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000506void CXXNameMangler::mangleName(const NamedDecl *ND) {
507 // <name> ::= <nested-name>
508 // ::= <unscoped-name>
509 // ::= <unscoped-template-name> <template-args>
Anders Carlsson201ce742009-09-17 03:17:01 +0000510 // ::= <local-name>
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000511 //
Anders Carlssond58d6f72009-09-17 16:12:20 +0000512 const DeclContext *DC = ND->getDeclContext();
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000513
Eli Friedman7facf842009-12-02 20:32:49 +0000514 // If this is an extern variable declared locally, the relevant DeclContext
515 // is that of the containing namespace, or the translation unit.
516 if (isa<FunctionDecl>(DC) && ND->hasLinkage())
517 while (!DC->isNamespace() && !DC->isTranslationUnit())
518 DC = DC->getParent();
John McCall82b7d7b2010-10-18 21:28:44 +0000519 else if (GetLocalClassDecl(ND)) {
520 mangleLocalName(ND);
521 return;
522 }
Eli Friedman7facf842009-12-02 20:32:49 +0000523
Anders Carlsson5cc58c62009-09-22 17:23:30 +0000524 while (isa<LinkageSpecDecl>(DC))
Anders Carlssond58d6f72009-09-17 16:12:20 +0000525 DC = DC->getParent();
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000526
Anders Carlssond58d6f72009-09-17 16:12:20 +0000527 if (DC->isTranslationUnit() || isStdNamespace(DC)) {
Anders Carlsson2744a062009-09-18 19:00:18 +0000528 // Check if we have a template.
529 const TemplateArgumentList *TemplateArgs = 0;
Anders Carlsson0fa6df42009-09-26 19:45:45 +0000530 if (const TemplateDecl *TD = isTemplate(ND, TemplateArgs)) {
Anders Carlsson2744a062009-09-18 19:00:18 +0000531 mangleUnscopedTemplateName(TD);
Rafael Espindolad9800722010-03-11 14:07:00 +0000532 TemplateParameterList *TemplateParameters = TD->getTemplateParameters();
533 mangleTemplateArgs(*TemplateParameters, *TemplateArgs);
Anders Carlsson2744a062009-09-18 19:00:18 +0000534 return;
Anders Carlsson7482e242009-09-18 04:29:09 +0000535 }
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000536
Anders Carlsson7482e242009-09-18 04:29:09 +0000537 mangleUnscopedName(ND);
538 return;
539 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000540
Anders Carlsson7b06f6c2009-12-10 03:14:39 +0000541 if (isa<FunctionDecl>(DC) || isa<ObjCMethodDecl>(DC)) {
Anders Carlsson7482e242009-09-18 04:29:09 +0000542 mangleLocalName(ND);
543 return;
544 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000545
Eli Friedman7facf842009-12-02 20:32:49 +0000546 mangleNestedName(ND, DC);
Anders Carlsson7482e242009-09-18 04:29:09 +0000547}
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000548void CXXNameMangler::mangleName(const TemplateDecl *TD,
Anders Carlsson7624f212009-09-18 02:42:01 +0000549 const TemplateArgument *TemplateArgs,
550 unsigned NumTemplateArgs) {
Anders Carlsson47846d22009-12-04 06:23:23 +0000551 const DeclContext *DC = IgnoreLinkageSpecDecls(TD->getDeclContext());
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000552
Anders Carlsson7624f212009-09-18 02:42:01 +0000553 if (DC->isTranslationUnit() || isStdNamespace(DC)) {
Anders Carlsson0fa6df42009-09-26 19:45:45 +0000554 mangleUnscopedTemplateName(TD);
Rafael Espindolad9800722010-03-11 14:07:00 +0000555 TemplateParameterList *TemplateParameters = TD->getTemplateParameters();
556 mangleTemplateArgs(*TemplateParameters, TemplateArgs, NumTemplateArgs);
Anders Carlsson7624f212009-09-18 02:42:01 +0000557 } else {
558 mangleNestedName(TD, TemplateArgs, NumTemplateArgs);
559 }
560}
561
Anders Carlsson201ce742009-09-17 03:17:01 +0000562void CXXNameMangler::mangleUnscopedName(const NamedDecl *ND) {
563 // <unscoped-name> ::= <unqualified-name>
564 // ::= St <unqualified-name> # ::std::
565 if (isStdNamespace(ND->getDeclContext()))
566 Out << "St";
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000567
Anders Carlsson201ce742009-09-17 03:17:01 +0000568 mangleUnqualifiedName(ND);
569}
570
Anders Carlsson0fa6df42009-09-26 19:45:45 +0000571void CXXNameMangler::mangleUnscopedTemplateName(const TemplateDecl *ND) {
Anders Carlsson201ce742009-09-17 03:17:01 +0000572 // <unscoped-template-name> ::= <unscoped-name>
573 // ::= <substitution>
Anders Carlsson7624f212009-09-18 02:42:01 +0000574 if (mangleSubstitution(ND))
Anders Carlsson03c9d532009-09-17 04:02:31 +0000575 return;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +0000576
Douglas Gregor32fb4e12010-02-05 20:45:00 +0000577 // <template-template-param> ::= <template-param>
578 if (const TemplateTemplateParmDecl *TTP
579 = dyn_cast<TemplateTemplateParmDecl>(ND)) {
580 mangleTemplateParameter(TTP->getIndex());
581 return;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000582 }
Douglas Gregor32fb4e12010-02-05 20:45:00 +0000583
Anders Carlsson1668f202009-09-26 20:13:56 +0000584 mangleUnscopedName(ND->getTemplatedDecl());
Anders Carlsson7624f212009-09-18 02:42:01 +0000585 addSubstitution(ND);
Anders Carlsson201ce742009-09-17 03:17:01 +0000586}
587
Douglas Gregor1e9268e2010-04-28 05:58:56 +0000588void CXXNameMangler::mangleUnscopedTemplateName(TemplateName Template) {
589 // <unscoped-template-name> ::= <unscoped-name>
590 // ::= <substitution>
591 if (TemplateDecl *TD = Template.getAsTemplateDecl())
592 return mangleUnscopedTemplateName(TD);
Sean Huntc3021132010-05-05 15:23:54 +0000593
Douglas Gregor1e9268e2010-04-28 05:58:56 +0000594 if (mangleSubstitution(Template))
595 return;
596
Douglas Gregor1e9268e2010-04-28 05:58:56 +0000597 DependentTemplateName *Dependent = Template.getAsDependentTemplateName();
598 assert(Dependent && "Not a dependent template name?");
Douglas Gregor19617912011-07-12 05:06:05 +0000599 if (const IdentifierInfo *Id = Dependent->getIdentifier())
600 mangleSourceName(Id);
601 else
602 mangleOperatorName(Dependent->getOperator(), UnknownArity);
Sean Huntc3021132010-05-05 15:23:54 +0000603
Douglas Gregor1e9268e2010-04-28 05:58:56 +0000604 addSubstitution(Template);
605}
606
John McCall1b600522011-04-24 03:07:16 +0000607void CXXNameMangler::mangleFloat(const llvm::APFloat &f) {
608 // ABI:
609 // Floating-point literals are encoded using a fixed-length
610 // lowercase hexadecimal string corresponding to the internal
611 // representation (IEEE on Itanium), high-order bytes first,
612 // without leading zeroes. For example: "Lf bf800000 E" is -1.0f
613 // on Itanium.
John McCall0c8731a2012-01-30 18:36:31 +0000614 // The 'without leading zeroes' thing seems to be an editorial
615 // mistake; see the discussion on cxx-abi-dev beginning on
616 // 2012-01-16.
John McCall1b600522011-04-24 03:07:16 +0000617
John McCall0c8731a2012-01-30 18:36:31 +0000618 // Our requirements here are just barely wierd enough to justify
619 // using a custom algorithm instead of post-processing APInt::toString().
John McCall1b600522011-04-24 03:07:16 +0000620
John McCall0c8731a2012-01-30 18:36:31 +0000621 llvm::APInt valueBits = f.bitcastToAPInt();
622 unsigned numCharacters = (valueBits.getBitWidth() + 3) / 4;
623 assert(numCharacters != 0);
624
625 // Allocate a buffer of the right number of characters.
626 llvm::SmallVector<char, 20> buffer;
627 buffer.set_size(numCharacters);
628
629 // Fill the buffer left-to-right.
630 for (unsigned stringIndex = 0; stringIndex != numCharacters; ++stringIndex) {
631 // The bit-index of the next hex digit.
632 unsigned digitBitIndex = 4 * (numCharacters - stringIndex - 1);
633
634 // Project out 4 bits starting at 'digitIndex'.
635 llvm::integerPart hexDigit
636 = valueBits.getRawData()[digitBitIndex / llvm::integerPartWidth];
637 hexDigit >>= (digitBitIndex % llvm::integerPartWidth);
638 hexDigit &= 0xF;
639
640 // Map that over to a lowercase hex digit.
641 static const char charForHex[16] = {
642 '0', '1', '2', '3', '4', '5', '6', '7',
643 '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'
644 };
645 buffer[stringIndex] = charForHex[hexDigit];
646 }
647
648 Out.write(buffer.data(), numCharacters);
John McCall0512e482010-07-14 04:20:34 +0000649}
650
651void CXXNameMangler::mangleNumber(const llvm::APSInt &Value) {
652 if (Value.isSigned() && Value.isNegative()) {
653 Out << 'n';
654 Value.abs().print(Out, true);
655 } else
656 Value.print(Out, Value.isSigned());
657}
658
Anders Carlssona94822e2009-11-26 02:32:05 +0000659void CXXNameMangler::mangleNumber(int64_t Number) {
660 // <number> ::= [n] <non-negative decimal integer>
661 if (Number < 0) {
662 Out << 'n';
663 Number = -Number;
664 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000665
Anders Carlssona94822e2009-11-26 02:32:05 +0000666 Out << Number;
667}
668
Anders Carlsson19879c92010-03-23 17:17:29 +0000669void CXXNameMangler::mangleCallOffset(int64_t NonVirtual, int64_t Virtual) {
Mike Stump141c5af2009-09-02 00:25:38 +0000670 // <call-offset> ::= h <nv-offset> _
671 // ::= v <v-offset> _
672 // <nv-offset> ::= <offset number> # non-virtual base override
Anders Carlssona94822e2009-11-26 02:32:05 +0000673 // <v-offset> ::= <offset number> _ <virtual offset number>
Mike Stump141c5af2009-09-02 00:25:38 +0000674 // # virtual base override, with vcall offset
Anders Carlsson19879c92010-03-23 17:17:29 +0000675 if (!Virtual) {
Anders Carlssona94822e2009-11-26 02:32:05 +0000676 Out << 'h';
Anders Carlsson19879c92010-03-23 17:17:29 +0000677 mangleNumber(NonVirtual);
Anders Carlssona94822e2009-11-26 02:32:05 +0000678 Out << '_';
679 return;
Mike Stump141c5af2009-09-02 00:25:38 +0000680 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000681
Anders Carlssona94822e2009-11-26 02:32:05 +0000682 Out << 'v';
Anders Carlsson19879c92010-03-23 17:17:29 +0000683 mangleNumber(NonVirtual);
Anders Carlssona94822e2009-11-26 02:32:05 +0000684 Out << '_';
Anders Carlsson19879c92010-03-23 17:17:29 +0000685 mangleNumber(Virtual);
Anders Carlssona94822e2009-11-26 02:32:05 +0000686 Out << '_';
Mike Stump9124bcc2009-09-02 00:56:18 +0000687}
688
John McCall4f4e4132011-05-04 01:45:19 +0000689void CXXNameMangler::manglePrefix(QualType type) {
John McCalla0ce15c2011-04-24 08:23:24 +0000690 if (const TemplateSpecializationType *TST =
691 type->getAs<TemplateSpecializationType>()) {
692 if (!mangleSubstitution(QualType(TST, 0))) {
693 mangleTemplatePrefix(TST->getTemplateName());
Sean Huntc3021132010-05-05 15:23:54 +0000694
Douglas Gregoraa2187d2011-02-28 00:04:36 +0000695 // FIXME: GCC does not appear to mangle the template arguments when
696 // the template in question is a dependent template name. Should we
697 // emulate that badness?
John McCalla0ce15c2011-04-24 08:23:24 +0000698 mangleTemplateArgs(TST->getTemplateName(), TST->getArgs(),
699 TST->getNumArgs());
700 addSubstitution(QualType(TST, 0));
Rafael Espindola9b35b252010-03-17 04:28:11 +0000701 }
John McCalla0ce15c2011-04-24 08:23:24 +0000702 } else if (const DependentTemplateSpecializationType *DTST
703 = type->getAs<DependentTemplateSpecializationType>()) {
704 TemplateName Template
705 = getASTContext().getDependentTemplateName(DTST->getQualifier(),
706 DTST->getIdentifier());
707 mangleTemplatePrefix(Template);
708
709 // FIXME: GCC does not appear to mangle the template arguments when
710 // the template in question is a dependent template name. Should we
711 // emulate that badness?
712 mangleTemplateArgs(Template, DTST->getArgs(), DTST->getNumArgs());
713 } else {
714 // We use the QualType mangle type variant here because it handles
715 // substitutions.
716 mangleType(type);
John McCall1dd73832010-02-04 01:42:13 +0000717 }
718}
719
John McCalla0ce15c2011-04-24 08:23:24 +0000720/// Mangle everything prior to the base-unresolved-name in an unresolved-name.
721///
722/// \param firstQualifierLookup - the entity found by unqualified lookup
723/// for the first name in the qualifier, if this is for a member expression
724/// \param recursive - true if this is being called recursively,
725/// i.e. if there is more prefix "to the right".
726void CXXNameMangler::mangleUnresolvedPrefix(NestedNameSpecifier *qualifier,
727 NamedDecl *firstQualifierLookup,
728 bool recursive) {
John McCall1dd73832010-02-04 01:42:13 +0000729
John McCalla0ce15c2011-04-24 08:23:24 +0000730 // x, ::x
731 // <unresolved-name> ::= [gs] <base-unresolved-name>
732
733 // T::x / decltype(p)::x
734 // <unresolved-name> ::= sr <unresolved-type> <base-unresolved-name>
735
736 // T::N::x /decltype(p)::N::x
737 // <unresolved-name> ::= srN <unresolved-type> <unresolved-qualifier-level>+ E
738 // <base-unresolved-name>
739
740 // A::x, N::y, A<T>::z; "gs" means leading "::"
741 // <unresolved-name> ::= [gs] sr <unresolved-qualifier-level>+ E
742 // <base-unresolved-name>
743
744 switch (qualifier->getKind()) {
745 case NestedNameSpecifier::Global:
746 Out << "gs";
747
748 // We want an 'sr' unless this is the entire NNS.
749 if (recursive)
750 Out << "sr";
751
752 // We never want an 'E' here.
753 return;
754
755 case NestedNameSpecifier::Namespace:
756 if (qualifier->getPrefix())
757 mangleUnresolvedPrefix(qualifier->getPrefix(), firstQualifierLookup,
758 /*recursive*/ true);
759 else
760 Out << "sr";
761 mangleSourceName(qualifier->getAsNamespace()->getIdentifier());
762 break;
763 case NestedNameSpecifier::NamespaceAlias:
764 if (qualifier->getPrefix())
765 mangleUnresolvedPrefix(qualifier->getPrefix(), firstQualifierLookup,
766 /*recursive*/ true);
767 else
768 Out << "sr";
769 mangleSourceName(qualifier->getAsNamespaceAlias()->getIdentifier());
770 break;
771
772 case NestedNameSpecifier::TypeSpec:
773 case NestedNameSpecifier::TypeSpecWithTemplate: {
John McCall4f4e4132011-05-04 01:45:19 +0000774 const Type *type = qualifier->getAsType();
John McCalla0ce15c2011-04-24 08:23:24 +0000775
John McCall4f4e4132011-05-04 01:45:19 +0000776 // We only want to use an unresolved-type encoding if this is one of:
777 // - a decltype
778 // - a template type parameter
779 // - a template template parameter with arguments
780 // In all of these cases, we should have no prefix.
781 if (qualifier->getPrefix()) {
782 mangleUnresolvedPrefix(qualifier->getPrefix(), firstQualifierLookup,
783 /*recursive*/ true);
784 } else {
785 // Otherwise, all the cases want this.
786 Out << "sr";
John McCall4f4e4132011-05-04 01:45:19 +0000787 }
788
John McCall4f4e4132011-05-04 01:45:19 +0000789 // Only certain other types are valid as prefixes; enumerate them.
John McCalld3d49bb2011-06-28 16:49:23 +0000790 switch (type->getTypeClass()) {
791 case Type::Builtin:
792 case Type::Complex:
793 case Type::Pointer:
794 case Type::BlockPointer:
795 case Type::LValueReference:
796 case Type::RValueReference:
797 case Type::MemberPointer:
798 case Type::ConstantArray:
799 case Type::IncompleteArray:
800 case Type::VariableArray:
801 case Type::DependentSizedArray:
802 case Type::DependentSizedExtVector:
803 case Type::Vector:
804 case Type::ExtVector:
805 case Type::FunctionProto:
806 case Type::FunctionNoProto:
807 case Type::Enum:
808 case Type::Paren:
809 case Type::Elaborated:
810 case Type::Attributed:
811 case Type::Auto:
812 case Type::PackExpansion:
John McCalld3d49bb2011-06-28 16:49:23 +0000813 case Type::ObjCObject:
814 case Type::ObjCInterface:
815 case Type::ObjCObjectPointer:
Eli Friedmanb001de72011-10-06 23:00:33 +0000816 case Type::Atomic:
John McCalld3d49bb2011-06-28 16:49:23 +0000817 llvm_unreachable("type is illegal as a nested name specifier");
818
John McCall68a51a72011-07-01 00:04:39 +0000819 case Type::SubstTemplateTypeParmPack:
820 // FIXME: not clear how to mangle this!
821 // template <class T...> class A {
822 // template <class U...> void foo(decltype(T::foo(U())) x...);
823 // };
824 Out << "_SUBSTPACK_";
825 break;
826
John McCalld3d49bb2011-06-28 16:49:23 +0000827 // <unresolved-type> ::= <template-param>
828 // ::= <decltype>
829 // ::= <template-template-param> <template-args>
830 // (this last is not official yet)
831 case Type::TypeOfExpr:
832 case Type::TypeOf:
833 case Type::Decltype:
834 case Type::TemplateTypeParm:
835 case Type::UnaryTransform:
John McCall35ee32e2011-07-01 02:19:08 +0000836 case Type::SubstTemplateTypeParm:
John McCalld3d49bb2011-06-28 16:49:23 +0000837 unresolvedType:
838 assert(!qualifier->getPrefix());
839
840 // We only get here recursively if we're followed by identifiers.
841 if (recursive) Out << 'N';
842
John McCall35ee32e2011-07-01 02:19:08 +0000843 // This seems to do everything we want. It's not really
844 // sanctioned for a substituted template parameter, though.
John McCalld3d49bb2011-06-28 16:49:23 +0000845 mangleType(QualType(type, 0));
846
847 // We never want to print 'E' directly after an unresolved-type,
848 // so we return directly.
849 return;
850
John McCalld3d49bb2011-06-28 16:49:23 +0000851 case Type::Typedef:
852 mangleSourceName(cast<TypedefType>(type)->getDecl()->getIdentifier());
853 break;
854
855 case Type::UnresolvedUsing:
856 mangleSourceName(cast<UnresolvedUsingType>(type)->getDecl()
857 ->getIdentifier());
858 break;
859
860 case Type::Record:
861 mangleSourceName(cast<RecordType>(type)->getDecl()->getIdentifier());
862 break;
863
864 case Type::TemplateSpecialization: {
865 const TemplateSpecializationType *tst
866 = cast<TemplateSpecializationType>(type);
John McCall68a51a72011-07-01 00:04:39 +0000867 TemplateName name = tst->getTemplateName();
868 switch (name.getKind()) {
869 case TemplateName::Template:
870 case TemplateName::QualifiedTemplate: {
871 TemplateDecl *temp = name.getAsTemplateDecl();
John McCalld3d49bb2011-06-28 16:49:23 +0000872
John McCall68a51a72011-07-01 00:04:39 +0000873 // If the base is a template template parameter, this is an
874 // unresolved type.
875 assert(temp && "no template for template specialization type");
876 if (isa<TemplateTemplateParmDecl>(temp)) goto unresolvedType;
John McCalld3d49bb2011-06-28 16:49:23 +0000877
John McCall68a51a72011-07-01 00:04:39 +0000878 mangleSourceName(temp->getIdentifier());
879 break;
880 }
881
882 case TemplateName::OverloadedTemplate:
883 case TemplateName::DependentTemplate:
884 llvm_unreachable("invalid base for a template specialization type");
885
886 case TemplateName::SubstTemplateTemplateParm: {
887 SubstTemplateTemplateParmStorage *subst
888 = name.getAsSubstTemplateTemplateParm();
889 mangleExistingSubstitution(subst->getReplacement());
890 break;
891 }
892
893 case TemplateName::SubstTemplateTemplateParmPack: {
894 // FIXME: not clear how to mangle this!
895 // template <template <class U> class T...> class A {
896 // template <class U...> void foo(decltype(T<U>::foo) x...);
897 // };
898 Out << "_SUBSTPACK_";
899 break;
900 }
901 }
902
John McCall4f4e4132011-05-04 01:45:19 +0000903 mangleUnresolvedTemplateArgs(tst->getArgs(), tst->getNumArgs());
John McCalld3d49bb2011-06-28 16:49:23 +0000904 break;
905 }
906
907 case Type::InjectedClassName:
908 mangleSourceName(cast<InjectedClassNameType>(type)->getDecl()
909 ->getIdentifier());
910 break;
911
912 case Type::DependentName:
913 mangleSourceName(cast<DependentNameType>(type)->getIdentifier());
914 break;
915
916 case Type::DependentTemplateSpecialization: {
917 const DependentTemplateSpecializationType *tst
918 = cast<DependentTemplateSpecializationType>(type);
John McCall4f4e4132011-05-04 01:45:19 +0000919 mangleSourceName(tst->getIdentifier());
920 mangleUnresolvedTemplateArgs(tst->getArgs(), tst->getNumArgs());
John McCalld3d49bb2011-06-28 16:49:23 +0000921 break;
922 }
John McCall4f4e4132011-05-04 01:45:19 +0000923 }
924 break;
John McCalla0ce15c2011-04-24 08:23:24 +0000925 }
926
927 case NestedNameSpecifier::Identifier:
928 // Member expressions can have these without prefixes.
929 if (qualifier->getPrefix()) {
930 mangleUnresolvedPrefix(qualifier->getPrefix(), firstQualifierLookup,
931 /*recursive*/ true);
932 } else if (firstQualifierLookup) {
933
934 // Try to make a proper qualifier out of the lookup result, and
935 // then just recurse on that.
936 NestedNameSpecifier *newQualifier;
937 if (TypeDecl *typeDecl = dyn_cast<TypeDecl>(firstQualifierLookup)) {
938 QualType type = getASTContext().getTypeDeclType(typeDecl);
939
940 // Pretend we had a different nested name specifier.
941 newQualifier = NestedNameSpecifier::Create(getASTContext(),
942 /*prefix*/ 0,
943 /*template*/ false,
944 type.getTypePtr());
945 } else if (NamespaceDecl *nspace =
946 dyn_cast<NamespaceDecl>(firstQualifierLookup)) {
947 newQualifier = NestedNameSpecifier::Create(getASTContext(),
948 /*prefix*/ 0,
949 nspace);
950 } else if (NamespaceAliasDecl *alias =
951 dyn_cast<NamespaceAliasDecl>(firstQualifierLookup)) {
952 newQualifier = NestedNameSpecifier::Create(getASTContext(),
953 /*prefix*/ 0,
954 alias);
955 } else {
956 // No sensible mangling to do here.
957 newQualifier = 0;
958 }
959
960 if (newQualifier)
961 return mangleUnresolvedPrefix(newQualifier, /*lookup*/ 0, recursive);
962
963 } else {
964 Out << "sr";
965 }
966
967 mangleSourceName(qualifier->getAsIdentifier());
968 break;
969 }
970
971 // If this was the innermost part of the NNS, and we fell out to
972 // here, append an 'E'.
973 if (!recursive)
974 Out << 'E';
975}
976
977/// Mangle an unresolved-name, which is generally used for names which
978/// weren't resolved to specific entities.
979void CXXNameMangler::mangleUnresolvedName(NestedNameSpecifier *qualifier,
980 NamedDecl *firstQualifierLookup,
981 DeclarationName name,
982 unsigned knownArity) {
983 if (qualifier) mangleUnresolvedPrefix(qualifier, firstQualifierLookup);
984 mangleUnqualifiedName(0, name, knownArity);
John McCall1dd73832010-02-04 01:42:13 +0000985}
986
Anders Carlsson6f7e2f42010-06-08 14:49:03 +0000987static const FieldDecl *FindFirstNamedDataMember(const RecordDecl *RD) {
988 assert(RD->isAnonymousStructOrUnion() &&
989 "Expected anonymous struct or union!");
990
991 for (RecordDecl::field_iterator I = RD->field_begin(), E = RD->field_end();
992 I != E; ++I) {
993 const FieldDecl *FD = *I;
994
995 if (FD->getIdentifier())
996 return FD;
997
998 if (const RecordType *RT = FD->getType()->getAs<RecordType>()) {
999 if (const FieldDecl *NamedDataMember =
1000 FindFirstNamedDataMember(RT->getDecl()))
1001 return NamedDataMember;
1002 }
1003 }
1004
1005 // We didn't find a named data member.
1006 return 0;
1007}
1008
John McCall1dd73832010-02-04 01:42:13 +00001009void CXXNameMangler::mangleUnqualifiedName(const NamedDecl *ND,
1010 DeclarationName Name,
1011 unsigned KnownArity) {
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001012 // <unqualified-name> ::= <operator-name>
Mike Stump1eb44332009-09-09 15:08:12 +00001013 // ::= <ctor-dtor-name>
1014 // ::= <source-name>
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001015 switch (Name.getNameKind()) {
Anders Carlssonc4355b62009-10-07 01:45:02 +00001016 case DeclarationName::Identifier: {
Anders Carlssonc4355b62009-10-07 01:45:02 +00001017 if (const IdentifierInfo *II = Name.getAsIdentifierInfo()) {
Sean Hunt31455252010-01-24 03:04:27 +00001018 // We must avoid conflicts between internally- and externally-
John McCall74990f42011-03-22 06:34:45 +00001019 // linked variable and function declaration names in the same TU:
1020 // void test() { extern void foo(); }
1021 // static void foo();
1022 // This naming convention is the same as that followed by GCC,
1023 // though it shouldn't actually matter.
1024 if (ND && ND->getLinkage() == InternalLinkage &&
Sean Hunt31455252010-01-24 03:04:27 +00001025 ND->getDeclContext()->isFileContext())
1026 Out << 'L';
1027
Anders Carlssonc4355b62009-10-07 01:45:02 +00001028 mangleSourceName(II);
1029 break;
1030 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00001031
John McCall1dd73832010-02-04 01:42:13 +00001032 // Otherwise, an anonymous entity. We must have a declaration.
1033 assert(ND && "mangling empty name without declaration");
1034
1035 if (const NamespaceDecl *NS = dyn_cast<NamespaceDecl>(ND)) {
1036 if (NS->isAnonymousNamespace()) {
1037 // This is how gcc mangles these names.
1038 Out << "12_GLOBAL__N_1";
1039 break;
1040 }
1041 }
1042
Anders Carlsson6f7e2f42010-06-08 14:49:03 +00001043 if (const VarDecl *VD = dyn_cast<VarDecl>(ND)) {
1044 // We must have an anonymous union or struct declaration.
1045 const RecordDecl *RD =
1046 cast<RecordDecl>(VD->getType()->getAs<RecordType>()->getDecl());
1047
1048 // Itanium C++ ABI 5.1.2:
1049 //
1050 // For the purposes of mangling, the name of an anonymous union is
1051 // considered to be the name of the first named data member found by a
1052 // pre-order, depth-first, declaration-order walk of the data members of
1053 // the anonymous union. If there is no such data member (i.e., if all of
1054 // the data members in the union are unnamed), then there is no way for
1055 // a program to refer to the anonymous union, and there is therefore no
1056 // need to mangle its name.
1057 const FieldDecl *FD = FindFirstNamedDataMember(RD);
John McCall7121c8f2010-08-05 22:02:13 +00001058
1059 // It's actually possible for various reasons for us to get here
1060 // with an empty anonymous struct / union. Fortunately, it
1061 // doesn't really matter what name we generate.
1062 if (!FD) break;
Anders Carlsson6f7e2f42010-06-08 14:49:03 +00001063 assert(FD->getIdentifier() && "Data member name isn't an identifier!");
1064
1065 mangleSourceName(FD->getIdentifier());
1066 break;
1067 }
1068
Anders Carlssonc4355b62009-10-07 01:45:02 +00001069 // We must have an anonymous struct.
1070 const TagDecl *TD = cast<TagDecl>(ND);
Richard Smith162e1c12011-04-15 14:24:37 +00001071 if (const TypedefNameDecl *D = TD->getTypedefNameForAnonDecl()) {
Anders Carlssonc4355b62009-10-07 01:45:02 +00001072 assert(TD->getDeclContext() == D->getDeclContext() &&
1073 "Typedef should not be in another decl context!");
1074 assert(D->getDeclName().getAsIdentifierInfo() &&
1075 "Typedef was not named!");
1076 mangleSourceName(D->getDeclName().getAsIdentifierInfo());
1077 break;
1078 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00001079
Anders Carlssonc4355b62009-10-07 01:45:02 +00001080 // Get a unique id for the anonymous struct.
1081 uint64_t AnonStructId = Context.getAnonymousStructId(TD);
1082
1083 // Mangle it as a source name in the form
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00001084 // [n] $_<id>
Anders Carlssonc4355b62009-10-07 01:45:02 +00001085 // where n is the length of the string.
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00001086 SmallString<8> Str;
Anders Carlssonc4355b62009-10-07 01:45:02 +00001087 Str += "$_";
1088 Str += llvm::utostr(AnonStructId);
1089
1090 Out << Str.size();
1091 Out << Str.str();
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001092 break;
Anders Carlssonc4355b62009-10-07 01:45:02 +00001093 }
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001094
1095 case DeclarationName::ObjCZeroArgSelector:
1096 case DeclarationName::ObjCOneArgSelector:
1097 case DeclarationName::ObjCMultiArgSelector:
David Blaikieb219cfc2011-09-23 05:06:16 +00001098 llvm_unreachable("Can't mangle Objective-C selector names here!");
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001099
1100 case DeclarationName::CXXConstructorName:
Anders Carlsson27ae5362009-04-17 01:58:57 +00001101 if (ND == Structor)
Mike Stump141c5af2009-09-02 00:25:38 +00001102 // If the named decl is the C++ constructor we're mangling, use the type
1103 // we were given.
Anders Carlsson27ae5362009-04-17 01:58:57 +00001104 mangleCXXCtorType(static_cast<CXXCtorType>(StructorType));
Anders Carlsson3ac86b52009-04-15 05:36:58 +00001105 else
1106 // Otherwise, use the complete constructor name. This is relevant if a
1107 // class with a constructor is declared within a constructor.
1108 mangleCXXCtorType(Ctor_Complete);
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001109 break;
1110
1111 case DeclarationName::CXXDestructorName:
Anders Carlsson27ae5362009-04-17 01:58:57 +00001112 if (ND == Structor)
Mike Stump141c5af2009-09-02 00:25:38 +00001113 // If the named decl is the C++ destructor we're mangling, use the type we
1114 // were given.
Anders Carlsson27ae5362009-04-17 01:58:57 +00001115 mangleCXXDtorType(static_cast<CXXDtorType>(StructorType));
1116 else
1117 // Otherwise, use the complete destructor name. This is relevant if a
1118 // class with a destructor is declared within a destructor.
1119 mangleCXXDtorType(Dtor_Complete);
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001120 break;
1121
1122 case DeclarationName::CXXConversionFunctionName:
Mike Stump1eb44332009-09-09 15:08:12 +00001123 // <operator-name> ::= cv <type> # (cast)
Douglas Gregor219cc612009-02-13 01:28:03 +00001124 Out << "cv";
Douglas Gregor79e6bd32011-07-12 04:42:08 +00001125 mangleType(Name.getCXXNameType());
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001126 break;
1127
Anders Carlsson8257d412009-12-22 06:36:32 +00001128 case DeclarationName::CXXOperatorName: {
John McCall1dd73832010-02-04 01:42:13 +00001129 unsigned Arity;
1130 if (ND) {
1131 Arity = cast<FunctionDecl>(ND)->getNumParams();
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001132
John McCall1dd73832010-02-04 01:42:13 +00001133 // If we have a C++ member function, we need to include the 'this' pointer.
1134 // FIXME: This does not make sense for operators that are static, but their
1135 // names stay the same regardless of the arity (operator new for instance).
1136 if (isa<CXXMethodDecl>(ND))
1137 Arity++;
1138 } else
1139 Arity = KnownArity;
1140
Anders Carlsson8257d412009-12-22 06:36:32 +00001141 mangleOperatorName(Name.getCXXOverloadedOperator(), Arity);
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001142 break;
Anders Carlsson8257d412009-12-22 06:36:32 +00001143 }
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001144
Sean Hunt3e518bd2009-11-29 07:34:05 +00001145 case DeclarationName::CXXLiteralOperatorName:
Sean Hunt5dd6b392009-12-04 21:11:13 +00001146 // FIXME: This mangling is not yet official.
Sean Hunt2421f662009-12-04 21:01:37 +00001147 Out << "li";
Sean Hunt3e518bd2009-11-29 07:34:05 +00001148 mangleSourceName(Name.getCXXLiteralIdentifier());
1149 break;
1150
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001151 case DeclarationName::CXXUsingDirective:
David Blaikieb219cfc2011-09-23 05:06:16 +00001152 llvm_unreachable("Can't mangle a using directive name!");
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001153 }
1154}
1155
1156void CXXNameMangler::mangleSourceName(const IdentifierInfo *II) {
1157 // <source-name> ::= <positive length number> <identifier>
1158 // <number> ::= [n] <non-negative decimal integer>
1159 // <identifier> ::= <unqualified source code identifier>
1160 Out << II->getLength() << II->getName();
1161}
1162
Eli Friedman7facf842009-12-02 20:32:49 +00001163void CXXNameMangler::mangleNestedName(const NamedDecl *ND,
Fariborz Jahanian57058532010-03-03 19:41:08 +00001164 const DeclContext *DC,
1165 bool NoFunction) {
Douglas Gregor0a9a6d62011-01-26 17:36:28 +00001166 // <nested-name>
1167 // ::= N [<CV-qualifiers>] [<ref-qualifier>] <prefix> <unqualified-name> E
1168 // ::= N [<CV-qualifiers>] [<ref-qualifier>] <template-prefix>
1169 // <template-args> E
Anders Carlssond99edc42009-09-26 03:55:37 +00001170
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001171 Out << 'N';
Douglas Gregor0a9a6d62011-01-26 17:36:28 +00001172 if (const CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(ND)) {
John McCall0953e762009-09-24 19:53:00 +00001173 mangleQualifiers(Qualifiers::fromCVRMask(Method->getTypeQualifiers()));
Douglas Gregor0a9a6d62011-01-26 17:36:28 +00001174 mangleRefQualifier(Method->getRefQualifier());
1175 }
1176
Anders Carlsson2744a062009-09-18 19:00:18 +00001177 // Check if we have a template.
1178 const TemplateArgumentList *TemplateArgs = 0;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00001179 if (const TemplateDecl *TD = isTemplate(ND, TemplateArgs)) {
Anders Carlsson2744a062009-09-18 19:00:18 +00001180 mangleTemplatePrefix(TD);
Rafael Espindolad9800722010-03-11 14:07:00 +00001181 TemplateParameterList *TemplateParameters = TD->getTemplateParameters();
1182 mangleTemplateArgs(*TemplateParameters, *TemplateArgs);
Fariborz Jahanian57058532010-03-03 19:41:08 +00001183 }
1184 else {
1185 manglePrefix(DC, NoFunction);
Anders Carlsson7482e242009-09-18 04:29:09 +00001186 mangleUnqualifiedName(ND);
1187 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00001188
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001189 Out << 'E';
1190}
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00001191void CXXNameMangler::mangleNestedName(const TemplateDecl *TD,
Anders Carlsson7624f212009-09-18 02:42:01 +00001192 const TemplateArgument *TemplateArgs,
1193 unsigned NumTemplateArgs) {
Anders Carlssone45117b2009-09-27 19:53:49 +00001194 // <nested-name> ::= N [<CV-qualifiers>] <template-prefix> <template-args> E
1195
Anders Carlsson7624f212009-09-18 02:42:01 +00001196 Out << 'N';
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00001197
Anders Carlssone45117b2009-09-27 19:53:49 +00001198 mangleTemplatePrefix(TD);
Rafael Espindolad9800722010-03-11 14:07:00 +00001199 TemplateParameterList *TemplateParameters = TD->getTemplateParameters();
1200 mangleTemplateArgs(*TemplateParameters, TemplateArgs, NumTemplateArgs);
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00001201
Anders Carlsson7624f212009-09-18 02:42:01 +00001202 Out << 'E';
1203}
1204
Anders Carlsson1b42c792009-04-02 16:24:45 +00001205void CXXNameMangler::mangleLocalName(const NamedDecl *ND) {
1206 // <local-name> := Z <function encoding> E <entity name> [<discriminator>]
1207 // := Z <function encoding> E s [<discriminator>]
Mike Stump1eb44332009-09-09 15:08:12 +00001208 // <discriminator> := _ <non-negative number>
Fariborz Jahanian57058532010-03-03 19:41:08 +00001209 const DeclContext *DC = ND->getDeclContext();
Fariborz Jahanian8805fe82011-06-09 19:25:01 +00001210 if (isa<ObjCMethodDecl>(DC) && isa<FunctionDecl>(ND)) {
1211 // Don't add objc method name mangling to locally declared function
1212 mangleUnqualifiedName(ND);
1213 return;
1214 }
1215
Anders Carlsson1b42c792009-04-02 16:24:45 +00001216 Out << 'Z';
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001217
Charles Davis685b1d92010-05-26 18:25:27 +00001218 if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(DC)) {
1219 mangleObjCMethodName(MD);
John McCall82b7d7b2010-10-18 21:28:44 +00001220 } else if (const CXXRecordDecl *RD = GetLocalClassDecl(ND)) {
1221 mangleFunctionEncoding(cast<FunctionDecl>(RD->getDeclContext()));
Fariborz Jahanian57058532010-03-03 19:41:08 +00001222 Out << 'E';
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001223
John McCall82b7d7b2010-10-18 21:28:44 +00001224 // Mangle the name relative to the closest enclosing function.
1225 if (ND == RD) // equality ok because RD derived from ND above
1226 mangleUnqualifiedName(ND);
1227 else
1228 mangleNestedName(ND, DC, true /*NoFunction*/);
1229
Fariborz Jahanian4819ac42010-03-04 01:02:03 +00001230 unsigned disc;
John McCall82b7d7b2010-10-18 21:28:44 +00001231 if (Context.getNextDiscriminator(RD, disc)) {
Fariborz Jahanian4819ac42010-03-04 01:02:03 +00001232 if (disc < 10)
1233 Out << '_' << disc;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001234 else
Fariborz Jahanian4819ac42010-03-04 01:02:03 +00001235 Out << "__" << disc << '_';
1236 }
Fariborz Jahanian57058532010-03-03 19:41:08 +00001237
1238 return;
1239 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001240 else
Fariborz Jahanian57058532010-03-03 19:41:08 +00001241 mangleFunctionEncoding(cast<FunctionDecl>(DC));
Anders Carlsson7b06f6c2009-12-10 03:14:39 +00001242
Anders Carlsson1b42c792009-04-02 16:24:45 +00001243 Out << 'E';
Eli Friedman6f9f25d2009-12-11 20:21:38 +00001244 mangleUnqualifiedName(ND);
Anders Carlsson1b42c792009-04-02 16:24:45 +00001245}
1246
John McCalla0ce15c2011-04-24 08:23:24 +00001247void CXXNameMangler::manglePrefix(NestedNameSpecifier *qualifier) {
1248 switch (qualifier->getKind()) {
1249 case NestedNameSpecifier::Global:
1250 // nothing
1251 return;
1252
1253 case NestedNameSpecifier::Namespace:
1254 mangleName(qualifier->getAsNamespace());
1255 return;
1256
1257 case NestedNameSpecifier::NamespaceAlias:
1258 mangleName(qualifier->getAsNamespaceAlias()->getNamespace());
1259 return;
1260
1261 case NestedNameSpecifier::TypeSpec:
1262 case NestedNameSpecifier::TypeSpecWithTemplate:
John McCall4f4e4132011-05-04 01:45:19 +00001263 manglePrefix(QualType(qualifier->getAsType(), 0));
John McCalla0ce15c2011-04-24 08:23:24 +00001264 return;
1265
1266 case NestedNameSpecifier::Identifier:
1267 // Member expressions can have these without prefixes, but that
1268 // should end up in mangleUnresolvedPrefix instead.
1269 assert(qualifier->getPrefix());
1270 manglePrefix(qualifier->getPrefix());
1271
1272 mangleSourceName(qualifier->getAsIdentifier());
1273 return;
1274 }
1275
1276 llvm_unreachable("unexpected nested name specifier");
1277}
1278
Fariborz Jahanian57058532010-03-03 19:41:08 +00001279void CXXNameMangler::manglePrefix(const DeclContext *DC, bool NoFunction) {
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001280 // <prefix> ::= <prefix> <unqualified-name>
1281 // ::= <template-prefix> <template-args>
1282 // ::= <template-param>
1283 // ::= # empty
1284 // ::= <substitution>
Anders Carlsson6862fc72009-09-17 04:16:28 +00001285
Anders Carlssonadd28822009-09-22 20:33:31 +00001286 while (isa<LinkageSpecDecl>(DC))
1287 DC = DC->getParent();
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00001288
Anders Carlsson9263e912009-09-18 18:39:58 +00001289 if (DC->isTranslationUnit())
1290 return;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00001291
Douglas Gregor35415f52010-05-25 17:04:15 +00001292 if (const BlockDecl *Block = dyn_cast<BlockDecl>(DC)) {
1293 manglePrefix(DC->getParent(), NoFunction);
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00001294 SmallString<64> Name;
Rafael Espindolac4850c22011-02-10 23:59:36 +00001295 llvm::raw_svector_ostream NameStream(Name);
1296 Context.mangleBlock(Block, NameStream);
1297 NameStream.flush();
Douglas Gregor35415f52010-05-25 17:04:15 +00001298 Out << Name.size() << Name;
1299 return;
1300 }
1301
Anders Carlsson6862fc72009-09-17 04:16:28 +00001302 if (mangleSubstitution(cast<NamedDecl>(DC)))
1303 return;
Anders Carlsson7482e242009-09-18 04:29:09 +00001304
Anders Carlsson2ee3fca2009-09-18 20:11:09 +00001305 // Check if we have a template.
1306 const TemplateArgumentList *TemplateArgs = 0;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00001307 if (const TemplateDecl *TD = isTemplate(cast<NamedDecl>(DC), TemplateArgs)) {
Anders Carlsson2ee3fca2009-09-18 20:11:09 +00001308 mangleTemplatePrefix(TD);
Rafael Espindolad9800722010-03-11 14:07:00 +00001309 TemplateParameterList *TemplateParameters = TD->getTemplateParameters();
1310 mangleTemplateArgs(*TemplateParameters, *TemplateArgs);
Fariborz Jahanian57058532010-03-03 19:41:08 +00001311 }
Douglas Gregor35415f52010-05-25 17:04:15 +00001312 else if(NoFunction && (isa<FunctionDecl>(DC) || isa<ObjCMethodDecl>(DC)))
Fariborz Jahanian57058532010-03-03 19:41:08 +00001313 return;
Douglas Gregor35415f52010-05-25 17:04:15 +00001314 else if (const ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(DC))
1315 mangleObjCMethodName(Method);
Fariborz Jahanian57058532010-03-03 19:41:08 +00001316 else {
1317 manglePrefix(DC->getParent(), NoFunction);
Anders Carlsson2ee3fca2009-09-18 20:11:09 +00001318 mangleUnqualifiedName(cast<NamedDecl>(DC));
1319 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00001320
Anders Carlsson6862fc72009-09-17 04:16:28 +00001321 addSubstitution(cast<NamedDecl>(DC));
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001322}
1323
Douglas Gregor20f0cc72010-04-23 03:10:43 +00001324void CXXNameMangler::mangleTemplatePrefix(TemplateName Template) {
1325 // <template-prefix> ::= <prefix> <template unqualified-name>
1326 // ::= <template-param>
1327 // ::= <substitution>
1328 if (TemplateDecl *TD = Template.getAsTemplateDecl())
1329 return mangleTemplatePrefix(TD);
1330
1331 if (QualifiedTemplateName *Qualified = Template.getAsQualifiedTemplateName())
John McCalla0ce15c2011-04-24 08:23:24 +00001332 manglePrefix(Qualified->getQualifier());
Sean Huntc3021132010-05-05 15:23:54 +00001333
Douglas Gregor20f0cc72010-04-23 03:10:43 +00001334 if (OverloadedTemplateStorage *Overloaded
1335 = Template.getAsOverloadedTemplate()) {
Sean Huntc3021132010-05-05 15:23:54 +00001336 mangleUnqualifiedName(0, (*Overloaded->begin())->getDeclName(),
Douglas Gregor20f0cc72010-04-23 03:10:43 +00001337 UnknownArity);
1338 return;
1339 }
Sean Huntc3021132010-05-05 15:23:54 +00001340
Douglas Gregor20f0cc72010-04-23 03:10:43 +00001341 DependentTemplateName *Dependent = Template.getAsDependentTemplateName();
1342 assert(Dependent && "Unknown template name kind?");
John McCalla0ce15c2011-04-24 08:23:24 +00001343 manglePrefix(Dependent->getQualifier());
Douglas Gregor1e9268e2010-04-28 05:58:56 +00001344 mangleUnscopedTemplateName(Template);
Douglas Gregor20f0cc72010-04-23 03:10:43 +00001345}
1346
Anders Carlsson0fa6df42009-09-26 19:45:45 +00001347void CXXNameMangler::mangleTemplatePrefix(const TemplateDecl *ND) {
Anders Carlsson7482e242009-09-18 04:29:09 +00001348 // <template-prefix> ::= <prefix> <template unqualified-name>
1349 // ::= <template-param>
1350 // ::= <substitution>
Douglas Gregor32fb4e12010-02-05 20:45:00 +00001351 // <template-template-param> ::= <template-param>
1352 // <substitution>
Anders Carlsson7482e242009-09-18 04:29:09 +00001353
Anders Carlssonaeb85372009-09-26 22:18:22 +00001354 if (mangleSubstitution(ND))
1355 return;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00001356
Douglas Gregor32fb4e12010-02-05 20:45:00 +00001357 // <template-template-param> ::= <template-param>
1358 if (const TemplateTemplateParmDecl *TTP
1359 = dyn_cast<TemplateTemplateParmDecl>(ND)) {
1360 mangleTemplateParameter(TTP->getIndex());
1361 return;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001362 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00001363
Anders Carlssonaa73ab12009-09-18 18:47:07 +00001364 manglePrefix(ND->getDeclContext());
Anders Carlsson1668f202009-09-26 20:13:56 +00001365 mangleUnqualifiedName(ND->getTemplatedDecl());
Anders Carlssonaeb85372009-09-26 22:18:22 +00001366 addSubstitution(ND);
Anders Carlsson7482e242009-09-18 04:29:09 +00001367}
1368
John McCallb6f532e2010-07-14 06:43:17 +00001369/// Mangles a template name under the production <type>. Required for
1370/// template template arguments.
1371/// <type> ::= <class-enum-type>
1372/// ::= <template-param>
1373/// ::= <substitution>
1374void CXXNameMangler::mangleType(TemplateName TN) {
1375 if (mangleSubstitution(TN))
1376 return;
1377
1378 TemplateDecl *TD = 0;
1379
1380 switch (TN.getKind()) {
1381 case TemplateName::QualifiedTemplate:
1382 TD = TN.getAsQualifiedTemplateName()->getTemplateDecl();
1383 goto HaveDecl;
1384
1385 case TemplateName::Template:
1386 TD = TN.getAsTemplateDecl();
1387 goto HaveDecl;
1388
1389 HaveDecl:
1390 if (isa<TemplateTemplateParmDecl>(TD))
1391 mangleTemplateParameter(cast<TemplateTemplateParmDecl>(TD)->getIndex());
1392 else
1393 mangleName(TD);
1394 break;
1395
1396 case TemplateName::OverloadedTemplate:
1397 llvm_unreachable("can't mangle an overloaded template name as a <type>");
John McCallb6f532e2010-07-14 06:43:17 +00001398
1399 case TemplateName::DependentTemplate: {
1400 const DependentTemplateName *Dependent = TN.getAsDependentTemplateName();
1401 assert(Dependent->isIdentifier());
1402
1403 // <class-enum-type> ::= <name>
1404 // <name> ::= <nested-name>
John McCalla0ce15c2011-04-24 08:23:24 +00001405 mangleUnresolvedPrefix(Dependent->getQualifier(), 0);
John McCallb6f532e2010-07-14 06:43:17 +00001406 mangleSourceName(Dependent->getIdentifier());
1407 break;
1408 }
1409
John McCallb44e0cf2011-06-30 21:59:02 +00001410 case TemplateName::SubstTemplateTemplateParm: {
1411 // Substituted template parameters are mangled as the substituted
1412 // template. This will check for the substitution twice, which is
1413 // fine, but we have to return early so that we don't try to *add*
1414 // the substitution twice.
1415 SubstTemplateTemplateParmStorage *subst
1416 = TN.getAsSubstTemplateTemplateParm();
1417 mangleType(subst->getReplacement());
1418 return;
1419 }
John McCall14606042011-06-30 08:33:18 +00001420
Douglas Gregor1aee05d2011-01-15 06:45:20 +00001421 case TemplateName::SubstTemplateTemplateParmPack: {
John McCall68a51a72011-07-01 00:04:39 +00001422 // FIXME: not clear how to mangle this!
1423 // template <template <class> class T...> class A {
1424 // template <template <class> class U...> void foo(B<T,U> x...);
1425 // };
1426 Out << "_SUBSTPACK_";
Douglas Gregor1aee05d2011-01-15 06:45:20 +00001427 break;
1428 }
John McCallb6f532e2010-07-14 06:43:17 +00001429 }
1430
1431 addSubstitution(TN);
1432}
1433
Mike Stump1eb44332009-09-09 15:08:12 +00001434void
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001435CXXNameMangler::mangleOperatorName(OverloadedOperatorKind OO, unsigned Arity) {
1436 switch (OO) {
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001437 // <operator-name> ::= nw # new
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001438 case OO_New: Out << "nw"; break;
1439 // ::= na # new[]
1440 case OO_Array_New: Out << "na"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001441 // ::= dl # delete
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001442 case OO_Delete: Out << "dl"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001443 // ::= da # delete[]
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001444 case OO_Array_Delete: Out << "da"; break;
1445 // ::= ps # + (unary)
John McCall5e1e89b2010-08-18 19:18:59 +00001446 // ::= pl # + (binary or unknown)
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001447 case OO_Plus:
Anders Carlsson8257d412009-12-22 06:36:32 +00001448 Out << (Arity == 1? "ps" : "pl"); break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001449 // ::= ng # - (unary)
John McCall5e1e89b2010-08-18 19:18:59 +00001450 // ::= mi # - (binary or unknown)
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001451 case OO_Minus:
Anders Carlsson8257d412009-12-22 06:36:32 +00001452 Out << (Arity == 1? "ng" : "mi"); break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001453 // ::= ad # & (unary)
John McCall5e1e89b2010-08-18 19:18:59 +00001454 // ::= an # & (binary or unknown)
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001455 case OO_Amp:
Anders Carlsson8257d412009-12-22 06:36:32 +00001456 Out << (Arity == 1? "ad" : "an"); break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001457 // ::= de # * (unary)
John McCall5e1e89b2010-08-18 19:18:59 +00001458 // ::= ml # * (binary or unknown)
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001459 case OO_Star:
John McCall5e1e89b2010-08-18 19:18:59 +00001460 // Use binary when unknown.
Anders Carlsson8257d412009-12-22 06:36:32 +00001461 Out << (Arity == 1? "de" : "ml"); break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001462 // ::= co # ~
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001463 case OO_Tilde: Out << "co"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001464 // ::= dv # /
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001465 case OO_Slash: Out << "dv"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001466 // ::= rm # %
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001467 case OO_Percent: Out << "rm"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001468 // ::= or # |
1469 case OO_Pipe: Out << "or"; break;
1470 // ::= eo # ^
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001471 case OO_Caret: Out << "eo"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001472 // ::= aS # =
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001473 case OO_Equal: Out << "aS"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001474 // ::= pL # +=
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001475 case OO_PlusEqual: Out << "pL"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001476 // ::= mI # -=
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001477 case OO_MinusEqual: Out << "mI"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001478 // ::= mL # *=
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001479 case OO_StarEqual: Out << "mL"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001480 // ::= dV # /=
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001481 case OO_SlashEqual: Out << "dV"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001482 // ::= rM # %=
1483 case OO_PercentEqual: Out << "rM"; break;
1484 // ::= aN # &=
1485 case OO_AmpEqual: Out << "aN"; break;
1486 // ::= oR # |=
1487 case OO_PipeEqual: Out << "oR"; break;
1488 // ::= eO # ^=
1489 case OO_CaretEqual: Out << "eO"; break;
1490 // ::= ls # <<
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001491 case OO_LessLess: Out << "ls"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001492 // ::= rs # >>
1493 case OO_GreaterGreater: Out << "rs"; break;
1494 // ::= lS # <<=
1495 case OO_LessLessEqual: Out << "lS"; break;
1496 // ::= rS # >>=
1497 case OO_GreaterGreaterEqual: Out << "rS"; break;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001498 // ::= eq # ==
1499 case OO_EqualEqual: Out << "eq"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001500 // ::= ne # !=
1501 case OO_ExclaimEqual: Out << "ne"; break;
1502 // ::= lt # <
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001503 case OO_Less: Out << "lt"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001504 // ::= gt # >
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001505 case OO_Greater: Out << "gt"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001506 // ::= le # <=
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001507 case OO_LessEqual: Out << "le"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001508 // ::= ge # >=
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001509 case OO_GreaterEqual: Out << "ge"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001510 // ::= nt # !
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001511 case OO_Exclaim: Out << "nt"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001512 // ::= aa # &&
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001513 case OO_AmpAmp: Out << "aa"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001514 // ::= oo # ||
1515 case OO_PipePipe: Out << "oo"; break;
1516 // ::= pp # ++
1517 case OO_PlusPlus: Out << "pp"; break;
1518 // ::= mm # --
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001519 case OO_MinusMinus: Out << "mm"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001520 // ::= cm # ,
1521 case OO_Comma: Out << "cm"; break;
1522 // ::= pm # ->*
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001523 case OO_ArrowStar: Out << "pm"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001524 // ::= pt # ->
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001525 case OO_Arrow: Out << "pt"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001526 // ::= cl # ()
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001527 case OO_Call: Out << "cl"; break;
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001528 // ::= ix # []
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001529 case OO_Subscript: Out << "ix"; break;
Anders Carlssone170ba72009-12-14 01:45:37 +00001530
1531 // ::= qu # ?
1532 // The conditional operator can't be overloaded, but we still handle it when
1533 // mangling expressions.
1534 case OO_Conditional: Out << "qu"; break;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001535
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001536 case OO_None:
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001537 case NUM_OVERLOADED_OPERATORS:
David Blaikieb219cfc2011-09-23 05:06:16 +00001538 llvm_unreachable("Not an overloaded operator");
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001539 }
1540}
1541
John McCall0953e762009-09-24 19:53:00 +00001542void CXXNameMangler::mangleQualifiers(Qualifiers Quals) {
Mike Stump1eb44332009-09-09 15:08:12 +00001543 // <CV-qualifiers> ::= [r] [V] [K] # restrict (C99), volatile, const
John McCall0953e762009-09-24 19:53:00 +00001544 if (Quals.hasRestrict())
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001545 Out << 'r';
John McCall0953e762009-09-24 19:53:00 +00001546 if (Quals.hasVolatile())
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001547 Out << 'V';
John McCall0953e762009-09-24 19:53:00 +00001548 if (Quals.hasConst())
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001549 Out << 'K';
John McCall0953e762009-09-24 19:53:00 +00001550
Douglas Gregor56079f72010-06-14 23:15:08 +00001551 if (Quals.hasAddressSpace()) {
1552 // Extension:
1553 //
1554 // <type> ::= U <address-space-number>
1555 //
1556 // where <address-space-number> is a source name consisting of 'AS'
1557 // followed by the address space <number>.
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00001558 SmallString<64> ASString;
Douglas Gregor56079f72010-06-14 23:15:08 +00001559 ASString = "AS" + llvm::utostr_32(Quals.getAddressSpace());
1560 Out << 'U' << ASString.size() << ASString;
1561 }
1562
Chris Lattner5f9e2722011-07-23 10:55:15 +00001563 StringRef LifetimeName;
John McCallf85e1932011-06-15 23:02:42 +00001564 switch (Quals.getObjCLifetime()) {
1565 // Objective-C ARC Extension:
1566 //
1567 // <type> ::= U "__strong"
1568 // <type> ::= U "__weak"
1569 // <type> ::= U "__autoreleasing"
John McCallf85e1932011-06-15 23:02:42 +00001570 case Qualifiers::OCL_None:
1571 break;
1572
1573 case Qualifiers::OCL_Weak:
1574 LifetimeName = "__weak";
1575 break;
1576
1577 case Qualifiers::OCL_Strong:
1578 LifetimeName = "__strong";
1579 break;
1580
1581 case Qualifiers::OCL_Autoreleasing:
1582 LifetimeName = "__autoreleasing";
1583 break;
1584
1585 case Qualifiers::OCL_ExplicitNone:
Douglas Gregorc22d6992011-06-17 22:26:49 +00001586 // The __unsafe_unretained qualifier is *not* mangled, so that
1587 // __unsafe_unretained types in ARC produce the same manglings as the
1588 // equivalent (but, naturally, unqualified) types in non-ARC, providing
1589 // better ABI compatibility.
1590 //
1591 // It's safe to do this because unqualified 'id' won't show up
1592 // in any type signatures that need to be mangled.
John McCallf85e1932011-06-15 23:02:42 +00001593 break;
1594 }
1595 if (!LifetimeName.empty())
1596 Out << 'U' << LifetimeName.size() << LifetimeName;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001597}
1598
Douglas Gregor0a9a6d62011-01-26 17:36:28 +00001599void CXXNameMangler::mangleRefQualifier(RefQualifierKind RefQualifier) {
1600 // <ref-qualifier> ::= R # lvalue reference
1601 // ::= O # rvalue-reference
1602 // Proposal to Itanium C++ ABI list on 1/26/11
1603 switch (RefQualifier) {
1604 case RQ_None:
1605 break;
1606
1607 case RQ_LValue:
1608 Out << 'R';
1609 break;
1610
1611 case RQ_RValue:
1612 Out << 'O';
1613 break;
1614 }
1615}
1616
Anders Carlsson7b06f6c2009-12-10 03:14:39 +00001617void CXXNameMangler::mangleObjCMethodName(const ObjCMethodDecl *MD) {
Rafael Espindolaf0be9792011-02-11 02:52:17 +00001618 Context.mangleObjCMethodName(MD, Out);
Anders Carlsson7b06f6c2009-12-10 03:14:39 +00001619}
1620
Douglas Gregorf1588662011-07-12 15:18:55 +00001621void CXXNameMangler::mangleType(QualType T) {
1622 // If our type is instantiation-dependent but not dependent, we mangle
1623 // it as it was written in the source, removing any top-level sugar.
1624 // Otherwise, use the canonical type.
1625 //
1626 // FIXME: This is an approximation of the instantiation-dependent name
1627 // mangling rules, since we should really be using the type as written and
1628 // augmented via semantic analysis (i.e., with implicit conversions and
1629 // default template arguments) for any instantiation-dependent type.
1630 // Unfortunately, that requires several changes to our AST:
1631 // - Instantiation-dependent TemplateSpecializationTypes will need to be
1632 // uniqued, so that we can handle substitutions properly
1633 // - Default template arguments will need to be represented in the
1634 // TemplateSpecializationType, since they need to be mangled even though
1635 // they aren't written.
1636 // - Conversions on non-type template arguments need to be expressed, since
1637 // they can affect the mangling of sizeof/alignof.
1638 if (!T->isInstantiationDependentType() || T->isDependentType())
1639 T = T.getCanonicalType();
1640 else {
1641 // Desugar any types that are purely sugar.
1642 do {
1643 // Don't desugar through template specialization types that aren't
1644 // type aliases. We need to mangle the template arguments as written.
1645 if (const TemplateSpecializationType *TST
1646 = dyn_cast<TemplateSpecializationType>(T))
1647 if (!TST->isTypeAlias())
1648 break;
Anders Carlsson4843e582009-03-10 17:07:44 +00001649
Douglas Gregorf1588662011-07-12 15:18:55 +00001650 QualType Desugared
1651 = T.getSingleStepDesugaredType(Context.getASTContext());
1652 if (Desugared == T)
1653 break;
1654
1655 T = Desugared;
1656 } while (true);
1657 }
1658 SplitQualType split = T.split();
John McCallb47f7482011-01-26 20:05:40 +00001659 Qualifiers quals = split.second;
1660 const Type *ty = split.first;
1661
Douglas Gregorf1588662011-07-12 15:18:55 +00001662 bool isSubstitutable = quals || !isa<BuiltinType>(T);
1663 if (isSubstitutable && mangleSubstitution(T))
Anders Carlsson76967372009-09-17 00:43:46 +00001664 return;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001665
John McCallb47f7482011-01-26 20:05:40 +00001666 // If we're mangling a qualified array type, push the qualifiers to
1667 // the element type.
Douglas Gregorf1588662011-07-12 15:18:55 +00001668 if (quals && isa<ArrayType>(T)) {
1669 ty = Context.getASTContext().getAsArrayType(T);
John McCallb47f7482011-01-26 20:05:40 +00001670 quals = Qualifiers();
1671
Douglas Gregorf1588662011-07-12 15:18:55 +00001672 // Note that we don't update T: we want to add the
1673 // substitution at the original type.
John McCallb47f7482011-01-26 20:05:40 +00001674 }
1675
1676 if (quals) {
1677 mangleQualifiers(quals);
John McCall0953e762009-09-24 19:53:00 +00001678 // Recurse: even if the qualified type isn't yet substitutable,
1679 // the unqualified type might be.
John McCallb47f7482011-01-26 20:05:40 +00001680 mangleType(QualType(ty, 0));
Anders Carlsson76967372009-09-17 00:43:46 +00001681 } else {
John McCallb47f7482011-01-26 20:05:40 +00001682 switch (ty->getTypeClass()) {
John McCallefe6aee2009-09-05 07:56:18 +00001683#define ABSTRACT_TYPE(CLASS, PARENT)
1684#define NON_CANONICAL_TYPE(CLASS, PARENT) \
Anders Carlsson76967372009-09-17 00:43:46 +00001685 case Type::CLASS: \
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +00001686 llvm_unreachable("can't mangle non-canonical type " #CLASS "Type"); \
Anders Carlsson76967372009-09-17 00:43:46 +00001687 return;
John McCallefe6aee2009-09-05 07:56:18 +00001688#define TYPE(CLASS, PARENT) \
Anders Carlsson76967372009-09-17 00:43:46 +00001689 case Type::CLASS: \
John McCallb47f7482011-01-26 20:05:40 +00001690 mangleType(static_cast<const CLASS##Type*>(ty)); \
Anders Carlsson76967372009-09-17 00:43:46 +00001691 break;
John McCallefe6aee2009-09-05 07:56:18 +00001692#include "clang/AST/TypeNodes.def"
Anders Carlsson76967372009-09-17 00:43:46 +00001693 }
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001694 }
Anders Carlsson76967372009-09-17 00:43:46 +00001695
1696 // Add the substitution.
John McCallb47f7482011-01-26 20:05:40 +00001697 if (isSubstitutable)
Douglas Gregorf1588662011-07-12 15:18:55 +00001698 addSubstitution(T);
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001699}
1700
Douglas Gregor1b12a3b2010-05-26 05:11:13 +00001701void CXXNameMangler::mangleNameOrStandardSubstitution(const NamedDecl *ND) {
1702 if (!mangleStandardSubstitution(ND))
1703 mangleName(ND);
1704}
1705
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001706void CXXNameMangler::mangleType(const BuiltinType *T) {
John McCallefe6aee2009-09-05 07:56:18 +00001707 // <type> ::= <builtin-type>
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001708 // <builtin-type> ::= v # void
1709 // ::= w # wchar_t
1710 // ::= b # bool
1711 // ::= c # char
1712 // ::= a # signed char
1713 // ::= h # unsigned char
1714 // ::= s # short
1715 // ::= t # unsigned short
1716 // ::= i # int
1717 // ::= j # unsigned int
1718 // ::= l # long
1719 // ::= m # unsigned long
1720 // ::= x # long long, __int64
1721 // ::= y # unsigned long long, __int64
1722 // ::= n # __int128
1723 // UNSUPPORTED: ::= o # unsigned __int128
1724 // ::= f # float
1725 // ::= d # double
1726 // ::= e # long double, __float80
1727 // UNSUPPORTED: ::= g # __float128
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001728 // UNSUPPORTED: ::= Dd # IEEE 754r decimal floating point (64 bits)
1729 // UNSUPPORTED: ::= De # IEEE 754r decimal floating point (128 bits)
1730 // UNSUPPORTED: ::= Df # IEEE 754r decimal floating point (32 bits)
Anton Korobeynikovaa4a99b2011-10-14 23:23:15 +00001731 // ::= Dh # IEEE 754r half-precision floating point (16 bits)
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001732 // ::= Di # char32_t
1733 // ::= Ds # char16_t
Anders Carlssone2923682010-11-04 04:31:32 +00001734 // ::= Dn # std::nullptr_t (i.e., decltype(nullptr))
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001735 // ::= u <source-name> # vendor extended type
1736 switch (T->getKind()) {
1737 case BuiltinType::Void: Out << 'v'; break;
1738 case BuiltinType::Bool: Out << 'b'; break;
1739 case BuiltinType::Char_U: case BuiltinType::Char_S: Out << 'c'; break;
1740 case BuiltinType::UChar: Out << 'h'; break;
1741 case BuiltinType::UShort: Out << 't'; break;
1742 case BuiltinType::UInt: Out << 'j'; break;
1743 case BuiltinType::ULong: Out << 'm'; break;
1744 case BuiltinType::ULongLong: Out << 'y'; break;
Chris Lattner2df9ced2009-04-30 02:43:43 +00001745 case BuiltinType::UInt128: Out << 'o'; break;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001746 case BuiltinType::SChar: Out << 'a'; break;
Chris Lattner3f59c972010-12-25 23:25:43 +00001747 case BuiltinType::WChar_S:
1748 case BuiltinType::WChar_U: Out << 'w'; break;
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001749 case BuiltinType::Char16: Out << "Ds"; break;
1750 case BuiltinType::Char32: Out << "Di"; break;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001751 case BuiltinType::Short: Out << 's'; break;
1752 case BuiltinType::Int: Out << 'i'; break;
1753 case BuiltinType::Long: Out << 'l'; break;
1754 case BuiltinType::LongLong: Out << 'x'; break;
Chris Lattner2df9ced2009-04-30 02:43:43 +00001755 case BuiltinType::Int128: Out << 'n'; break;
Anton Korobeynikovaa4a99b2011-10-14 23:23:15 +00001756 case BuiltinType::Half: Out << "Dh"; break;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001757 case BuiltinType::Float: Out << 'f'; break;
1758 case BuiltinType::Double: Out << 'd'; break;
1759 case BuiltinType::LongDouble: Out << 'e'; break;
Anders Carlssone2923682010-11-04 04:31:32 +00001760 case BuiltinType::NullPtr: Out << "Dn"; break;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001761
John McCalle0a22d02011-10-18 21:02:43 +00001762#define BUILTIN_TYPE(Id, SingletonId)
1763#define PLACEHOLDER_TYPE(Id, SingletonId) \
1764 case BuiltinType::Id:
1765#include "clang/AST/BuiltinTypes.def"
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001766 case BuiltinType::Dependent:
John McCallfb44de92011-05-01 22:35:37 +00001767 llvm_unreachable("mangling a placeholder type");
Steve Naroff9533a7f2009-07-22 17:14:51 +00001768 case BuiltinType::ObjCId: Out << "11objc_object"; break;
1769 case BuiltinType::ObjCClass: Out << "10objc_class"; break;
Fariborz Jahanian13dcd002009-11-21 19:53:08 +00001770 case BuiltinType::ObjCSel: Out << "13objc_selector"; break;
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001771 }
1772}
1773
John McCallefe6aee2009-09-05 07:56:18 +00001774// <type> ::= <function-type>
1775// <function-type> ::= F [Y] <bare-function-type> E
1776void CXXNameMangler::mangleType(const FunctionProtoType *T) {
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001777 Out << 'F';
Mike Stumpf5408fe2009-05-16 07:57:57 +00001778 // FIXME: We don't have enough information in the AST to produce the 'Y'
1779 // encoding for extern "C" function types.
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001780 mangleBareFunctionType(T, /*MangleReturnType=*/true);
1781 Out << 'E';
1782}
John McCallefe6aee2009-09-05 07:56:18 +00001783void CXXNameMangler::mangleType(const FunctionNoProtoType *T) {
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +00001784 llvm_unreachable("Can't mangle K&R function prototypes");
John McCallefe6aee2009-09-05 07:56:18 +00001785}
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001786void CXXNameMangler::mangleBareFunctionType(const FunctionType *T,
1787 bool MangleReturnType) {
John McCallefe6aee2009-09-05 07:56:18 +00001788 // We should never be mangling something without a prototype.
1789 const FunctionProtoType *Proto = cast<FunctionProtoType>(T);
1790
John McCallfb44de92011-05-01 22:35:37 +00001791 // Record that we're in a function type. See mangleFunctionParam
1792 // for details on what we're trying to achieve here.
1793 FunctionTypeDepthState saved = FunctionTypeDepth.push();
1794
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001795 // <bare-function-type> ::= <signature type>+
John McCallfb44de92011-05-01 22:35:37 +00001796 if (MangleReturnType) {
1797 FunctionTypeDepth.enterResultType();
John McCallefe6aee2009-09-05 07:56:18 +00001798 mangleType(Proto->getResultType());
John McCallfb44de92011-05-01 22:35:37 +00001799 FunctionTypeDepth.leaveResultType();
1800 }
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001801
Anders Carlsson93296682010-06-02 04:40:13 +00001802 if (Proto->getNumArgs() == 0 && !Proto->isVariadic()) {
Eli Friedmana7e68452010-08-22 01:00:03 +00001803 // <builtin-type> ::= v # void
Anders Carlssonc6c91bc2009-04-01 00:15:23 +00001804 Out << 'v';
John McCallfb44de92011-05-01 22:35:37 +00001805
1806 FunctionTypeDepth.pop(saved);
Anders Carlssonc6c91bc2009-04-01 00:15:23 +00001807 return;
1808 }
Mike Stump1eb44332009-09-09 15:08:12 +00001809
Douglas Gregor72564e72009-02-26 23:50:07 +00001810 for (FunctionProtoType::arg_type_iterator Arg = Proto->arg_type_begin(),
Mike Stump1eb44332009-09-09 15:08:12 +00001811 ArgEnd = Proto->arg_type_end();
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001812 Arg != ArgEnd; ++Arg)
Douglas Gregor79e6bd32011-07-12 04:42:08 +00001813 mangleType(Context.getASTContext().getSignatureParameterType(*Arg));
Douglas Gregor219cc612009-02-13 01:28:03 +00001814
John McCallfb44de92011-05-01 22:35:37 +00001815 FunctionTypeDepth.pop(saved);
1816
Douglas Gregor219cc612009-02-13 01:28:03 +00001817 // <builtin-type> ::= z # ellipsis
1818 if (Proto->isVariadic())
1819 Out << 'z';
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001820}
1821
John McCallefe6aee2009-09-05 07:56:18 +00001822// <type> ::= <class-enum-type>
Mike Stump1eb44332009-09-09 15:08:12 +00001823// <class-enum-type> ::= <name>
John McCalled976492009-12-04 22:46:56 +00001824void CXXNameMangler::mangleType(const UnresolvedUsingType *T) {
1825 mangleName(T->getDecl());
1826}
1827
1828// <type> ::= <class-enum-type>
1829// <class-enum-type> ::= <name>
John McCallefe6aee2009-09-05 07:56:18 +00001830void CXXNameMangler::mangleType(const EnumType *T) {
1831 mangleType(static_cast<const TagType*>(T));
1832}
1833void CXXNameMangler::mangleType(const RecordType *T) {
1834 mangleType(static_cast<const TagType*>(T));
1835}
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001836void CXXNameMangler::mangleType(const TagType *T) {
Eli Friedmanecb7e932009-12-11 18:00:57 +00001837 mangleName(T->getDecl());
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001838}
1839
John McCallefe6aee2009-09-05 07:56:18 +00001840// <type> ::= <array-type>
1841// <array-type> ::= A <positive dimension number> _ <element type>
1842// ::= A [<dimension expression>] _ <element type>
1843void CXXNameMangler::mangleType(const ConstantArrayType *T) {
1844 Out << 'A' << T->getSize() << '_';
1845 mangleType(T->getElementType());
1846}
1847void CXXNameMangler::mangleType(const VariableArrayType *T) {
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001848 Out << 'A';
Fariborz Jahanian7281d1f2010-11-02 16:54:00 +00001849 // decayed vla types (size 0) will just be skipped.
1850 if (T->getSizeExpr())
1851 mangleExpression(T->getSizeExpr());
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001852 Out << '_';
1853 mangleType(T->getElementType());
1854}
John McCallefe6aee2009-09-05 07:56:18 +00001855void CXXNameMangler::mangleType(const DependentSizedArrayType *T) {
1856 Out << 'A';
1857 mangleExpression(T->getSizeExpr());
1858 Out << '_';
1859 mangleType(T->getElementType());
1860}
1861void CXXNameMangler::mangleType(const IncompleteArrayType *T) {
Nick Lewycky271b6652010-09-05 03:40:33 +00001862 Out << "A_";
John McCallefe6aee2009-09-05 07:56:18 +00001863 mangleType(T->getElementType());
1864}
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001865
John McCallefe6aee2009-09-05 07:56:18 +00001866// <type> ::= <pointer-to-member-type>
1867// <pointer-to-member-type> ::= M <class type> <member type>
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001868void CXXNameMangler::mangleType(const MemberPointerType *T) {
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001869 Out << 'M';
1870 mangleType(QualType(T->getClass(), 0));
Anders Carlsson0e650012009-05-17 17:41:20 +00001871 QualType PointeeType = T->getPointeeType();
1872 if (const FunctionProtoType *FPT = dyn_cast<FunctionProtoType>(PointeeType)) {
John McCall0953e762009-09-24 19:53:00 +00001873 mangleQualifiers(Qualifiers::fromCVRMask(FPT->getTypeQuals()));
Douglas Gregor0a9a6d62011-01-26 17:36:28 +00001874 mangleRefQualifier(FPT->getRefQualifier());
Anders Carlsson0e650012009-05-17 17:41:20 +00001875 mangleType(FPT);
Anders Carlsson9d85b722010-06-02 04:29:50 +00001876
1877 // Itanium C++ ABI 5.1.8:
1878 //
1879 // The type of a non-static member function is considered to be different,
1880 // for the purposes of substitution, from the type of a namespace-scope or
1881 // static member function whose type appears similar. The types of two
1882 // non-static member functions are considered to be different, for the
1883 // purposes of substitution, if the functions are members of different
1884 // classes. In other words, for the purposes of substitution, the class of
1885 // which the function is a member is considered part of the type of
1886 // function.
1887
1888 // We increment the SeqID here to emulate adding an entry to the
1889 // substitution table. We can't actually add it because we don't want this
1890 // particular function type to be substituted.
1891 ++SeqID;
Mike Stump1eb44332009-09-09 15:08:12 +00001892 } else
Anders Carlsson0e650012009-05-17 17:41:20 +00001893 mangleType(PointeeType);
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001894}
1895
John McCallefe6aee2009-09-05 07:56:18 +00001896// <type> ::= <template-param>
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001897void CXXNameMangler::mangleType(const TemplateTypeParmType *T) {
Anders Carlsson0ccdf8d2009-09-27 00:38:53 +00001898 mangleTemplateParameter(T->getIndex());
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001899}
1900
Douglas Gregorc3069d62011-01-14 02:55:32 +00001901// <type> ::= <template-param>
1902void CXXNameMangler::mangleType(const SubstTemplateTypeParmPackType *T) {
John McCall68a51a72011-07-01 00:04:39 +00001903 // FIXME: not clear how to mangle this!
1904 // template <class T...> class A {
1905 // template <class U...> void foo(T(*)(U) x...);
1906 // };
1907 Out << "_SUBSTPACK_";
Douglas Gregorc3069d62011-01-14 02:55:32 +00001908}
1909
John McCallefe6aee2009-09-05 07:56:18 +00001910// <type> ::= P <type> # pointer-to
1911void CXXNameMangler::mangleType(const PointerType *T) {
1912 Out << 'P';
1913 mangleType(T->getPointeeType());
1914}
1915void CXXNameMangler::mangleType(const ObjCObjectPointerType *T) {
1916 Out << 'P';
1917 mangleType(T->getPointeeType());
1918}
1919
1920// <type> ::= R <type> # reference-to
1921void CXXNameMangler::mangleType(const LValueReferenceType *T) {
1922 Out << 'R';
1923 mangleType(T->getPointeeType());
1924}
1925
1926// <type> ::= O <type> # rvalue reference-to (C++0x)
1927void CXXNameMangler::mangleType(const RValueReferenceType *T) {
1928 Out << 'O';
1929 mangleType(T->getPointeeType());
1930}
1931
1932// <type> ::= C <type> # complex pair (C 2000)
1933void CXXNameMangler::mangleType(const ComplexType *T) {
1934 Out << 'C';
1935 mangleType(T->getElementType());
1936}
1937
Bob Wilsonc7df92d2010-11-12 17:24:43 +00001938// ARM's ABI for Neon vector types specifies that they should be mangled as
Bob Wilson57147a82010-11-16 00:32:18 +00001939// if they are structs (to match ARM's initial implementation). The
1940// vector type must be one of the special types predefined by ARM.
1941void CXXNameMangler::mangleNeonVectorType(const VectorType *T) {
Bob Wilsonc7df92d2010-11-12 17:24:43 +00001942 QualType EltType = T->getElementType();
Bob Wilson57147a82010-11-16 00:32:18 +00001943 assert(EltType->isBuiltinType() && "Neon vector element not a BuiltinType");
Bob Wilsonc7df92d2010-11-12 17:24:43 +00001944 const char *EltName = 0;
Bob Wilson491328c2010-11-12 17:24:46 +00001945 if (T->getVectorKind() == VectorType::NeonPolyVector) {
1946 switch (cast<BuiltinType>(EltType)->getKind()) {
Bob Wilson4cfaa5d2010-11-12 17:24:49 +00001947 case BuiltinType::SChar: EltName = "poly8_t"; break;
1948 case BuiltinType::Short: EltName = "poly16_t"; break;
Bob Wilson57147a82010-11-16 00:32:18 +00001949 default: llvm_unreachable("unexpected Neon polynomial vector element type");
Bob Wilson491328c2010-11-12 17:24:46 +00001950 }
1951 } else {
1952 switch (cast<BuiltinType>(EltType)->getKind()) {
Bob Wilson4cfaa5d2010-11-12 17:24:49 +00001953 case BuiltinType::SChar: EltName = "int8_t"; break;
1954 case BuiltinType::UChar: EltName = "uint8_t"; break;
1955 case BuiltinType::Short: EltName = "int16_t"; break;
1956 case BuiltinType::UShort: EltName = "uint16_t"; break;
1957 case BuiltinType::Int: EltName = "int32_t"; break;
1958 case BuiltinType::UInt: EltName = "uint32_t"; break;
1959 case BuiltinType::LongLong: EltName = "int64_t"; break;
1960 case BuiltinType::ULongLong: EltName = "uint64_t"; break;
1961 case BuiltinType::Float: EltName = "float32_t"; break;
Bob Wilson57147a82010-11-16 00:32:18 +00001962 default: llvm_unreachable("unexpected Neon vector element type");
Bob Wilson491328c2010-11-12 17:24:46 +00001963 }
Bob Wilsonc7df92d2010-11-12 17:24:43 +00001964 }
1965 const char *BaseName = 0;
Bob Wilson4cfaa5d2010-11-12 17:24:49 +00001966 unsigned BitSize = (T->getNumElements() *
Bob Wilson3a723022010-11-16 00:32:12 +00001967 getASTContext().getTypeSize(EltType));
Bob Wilsonc7df92d2010-11-12 17:24:43 +00001968 if (BitSize == 64)
1969 BaseName = "__simd64_";
Bob Wilson57147a82010-11-16 00:32:18 +00001970 else {
1971 assert(BitSize == 128 && "Neon vector type not 64 or 128 bits");
Bob Wilsonc7df92d2010-11-12 17:24:43 +00001972 BaseName = "__simd128_";
Bob Wilson57147a82010-11-16 00:32:18 +00001973 }
Bob Wilsonc7df92d2010-11-12 17:24:43 +00001974 Out << strlen(BaseName) + strlen(EltName);
1975 Out << BaseName << EltName;
Bob Wilsonc7df92d2010-11-12 17:24:43 +00001976}
1977
John McCallefe6aee2009-09-05 07:56:18 +00001978// GNU extension: vector types
Chris Lattner788b0fd2010-06-23 06:00:24 +00001979// <type> ::= <vector-type>
1980// <vector-type> ::= Dv <positive dimension number> _
1981// <extended element type>
1982// ::= Dv [<dimension expression>] _ <element type>
1983// <extended element type> ::= <element type>
1984// ::= p # AltiVec vector pixel
John McCallefe6aee2009-09-05 07:56:18 +00001985void CXXNameMangler::mangleType(const VectorType *T) {
Bob Wilson491328c2010-11-12 17:24:46 +00001986 if ((T->getVectorKind() == VectorType::NeonVector ||
Bob Wilson57147a82010-11-16 00:32:18 +00001987 T->getVectorKind() == VectorType::NeonPolyVector)) {
1988 mangleNeonVectorType(T);
Bob Wilsonc7df92d2010-11-12 17:24:43 +00001989 return;
Bob Wilson57147a82010-11-16 00:32:18 +00001990 }
Nick Lewycky0e5f0672010-03-26 07:18:04 +00001991 Out << "Dv" << T->getNumElements() << '_';
Bob Wilsone86d78c2010-11-10 21:56:12 +00001992 if (T->getVectorKind() == VectorType::AltiVecPixel)
Chris Lattner788b0fd2010-06-23 06:00:24 +00001993 Out << 'p';
Bob Wilsone86d78c2010-11-10 21:56:12 +00001994 else if (T->getVectorKind() == VectorType::AltiVecBool)
Chris Lattner788b0fd2010-06-23 06:00:24 +00001995 Out << 'b';
1996 else
1997 mangleType(T->getElementType());
John McCallefe6aee2009-09-05 07:56:18 +00001998}
1999void CXXNameMangler::mangleType(const ExtVectorType *T) {
2000 mangleType(static_cast<const VectorType*>(T));
2001}
2002void CXXNameMangler::mangleType(const DependentSizedExtVectorType *T) {
Nick Lewycky0e5f0672010-03-26 07:18:04 +00002003 Out << "Dv";
2004 mangleExpression(T->getSizeExpr());
2005 Out << '_';
John McCallefe6aee2009-09-05 07:56:18 +00002006 mangleType(T->getElementType());
2007}
2008
Douglas Gregor7536dd52010-12-20 02:24:11 +00002009void CXXNameMangler::mangleType(const PackExpansionType *T) {
Douglas Gregor4fc48662011-01-13 16:39:34 +00002010 // <type> ::= Dp <type> # pack expansion (C++0x)
Douglas Gregor255c2692011-01-13 17:44:36 +00002011 Out << "Dp";
Douglas Gregor7536dd52010-12-20 02:24:11 +00002012 mangleType(T->getPattern());
2013}
2014
Anders Carlssona40c5e42009-03-07 22:03:21 +00002015void CXXNameMangler::mangleType(const ObjCInterfaceType *T) {
2016 mangleSourceName(T->getDecl()->getIdentifier());
2017}
2018
John McCallc12c5bb2010-05-15 11:32:37 +00002019void CXXNameMangler::mangleType(const ObjCObjectType *T) {
John McCallc00c1f62010-05-15 17:06:29 +00002020 // We don't allow overloading by different protocol qualification,
2021 // so mangling them isn't necessary.
John McCallc12c5bb2010-05-15 11:32:37 +00002022 mangleType(T->getBaseType());
2023}
2024
John McCallefe6aee2009-09-05 07:56:18 +00002025void CXXNameMangler::mangleType(const BlockPointerType *T) {
Anders Carlssonf28c6872009-12-23 22:31:44 +00002026 Out << "U13block_pointer";
2027 mangleType(T->getPointeeType());
John McCallefe6aee2009-09-05 07:56:18 +00002028}
2029
John McCall31f17ec2010-04-27 00:57:59 +00002030void CXXNameMangler::mangleType(const InjectedClassNameType *T) {
2031 // Mangle injected class name types as if the user had written the
2032 // specialization out fully. It may not actually be possible to see
2033 // this mangling, though.
2034 mangleType(T->getInjectedSpecializationType());
2035}
2036
John McCallefe6aee2009-09-05 07:56:18 +00002037void CXXNameMangler::mangleType(const TemplateSpecializationType *T) {
Douglas Gregor1e9268e2010-04-28 05:58:56 +00002038 if (TemplateDecl *TD = T->getTemplateName().getAsTemplateDecl()) {
2039 mangleName(TD, T->getArgs(), T->getNumArgs());
2040 } else {
2041 if (mangleSubstitution(QualType(T, 0)))
2042 return;
Sean Huntc3021132010-05-05 15:23:54 +00002043
Douglas Gregor1e9268e2010-04-28 05:58:56 +00002044 mangleTemplatePrefix(T->getTemplateName());
Sean Huntc3021132010-05-05 15:23:54 +00002045
Douglas Gregor1e9268e2010-04-28 05:58:56 +00002046 // FIXME: GCC does not appear to mangle the template arguments when
2047 // the template in question is a dependent template name. Should we
2048 // emulate that badness?
2049 mangleTemplateArgs(T->getTemplateName(), T->getArgs(), T->getNumArgs());
2050 addSubstitution(QualType(T, 0));
2051 }
John McCallefe6aee2009-09-05 07:56:18 +00002052}
2053
Douglas Gregor4714c122010-03-31 17:34:00 +00002054void CXXNameMangler::mangleType(const DependentNameType *T) {
Anders Carlssonae352482009-09-26 02:26:02 +00002055 // Typename types are always nested
2056 Out << 'N';
John McCalla0ce15c2011-04-24 08:23:24 +00002057 manglePrefix(T->getQualifier());
John McCall33500952010-06-11 00:33:02 +00002058 mangleSourceName(T->getIdentifier());
2059 Out << 'E';
2060}
John McCall6ab30e02010-06-09 07:26:17 +00002061
John McCall33500952010-06-11 00:33:02 +00002062void CXXNameMangler::mangleType(const DependentTemplateSpecializationType *T) {
Douglas Gregoraa2187d2011-02-28 00:04:36 +00002063 // Dependently-scoped template types are nested if they have a prefix.
John McCall33500952010-06-11 00:33:02 +00002064 Out << 'N';
2065
2066 // TODO: avoid making this TemplateName.
2067 TemplateName Prefix =
2068 getASTContext().getDependentTemplateName(T->getQualifier(),
2069 T->getIdentifier());
2070 mangleTemplatePrefix(Prefix);
2071
2072 // FIXME: GCC does not appear to mangle the template arguments when
2073 // the template in question is a dependent template name. Should we
2074 // emulate that badness?
2075 mangleTemplateArgs(Prefix, T->getArgs(), T->getNumArgs());
Anders Carlssonae352482009-09-26 02:26:02 +00002076 Out << 'E';
John McCallefe6aee2009-09-05 07:56:18 +00002077}
2078
John McCallad5e7382010-03-01 23:49:17 +00002079void CXXNameMangler::mangleType(const TypeOfType *T) {
2080 // FIXME: this is pretty unsatisfactory, but there isn't an obvious
2081 // "extension with parameters" mangling.
2082 Out << "u6typeof";
2083}
2084
2085void CXXNameMangler::mangleType(const TypeOfExprType *T) {
2086 // FIXME: this is pretty unsatisfactory, but there isn't an obvious
2087 // "extension with parameters" mangling.
2088 Out << "u6typeof";
2089}
2090
2091void CXXNameMangler::mangleType(const DecltypeType *T) {
2092 Expr *E = T->getUnderlyingExpr();
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002093
John McCallad5e7382010-03-01 23:49:17 +00002094 // type ::= Dt <expression> E # decltype of an id-expression
2095 // # or class member access
2096 // ::= DT <expression> E # decltype of an expression
2097
2098 // This purports to be an exhaustive list of id-expressions and
2099 // class member accesses. Note that we do not ignore parentheses;
2100 // parentheses change the semantics of decltype for these
2101 // expressions (and cause the mangler to use the other form).
2102 if (isa<DeclRefExpr>(E) ||
2103 isa<MemberExpr>(E) ||
2104 isa<UnresolvedLookupExpr>(E) ||
2105 isa<DependentScopeDeclRefExpr>(E) ||
2106 isa<CXXDependentScopeMemberExpr>(E) ||
2107 isa<UnresolvedMemberExpr>(E))
2108 Out << "Dt";
2109 else
2110 Out << "DT";
2111 mangleExpression(E);
2112 Out << 'E';
2113}
2114
Sean Huntca63c202011-05-24 22:41:36 +00002115void CXXNameMangler::mangleType(const UnaryTransformType *T) {
2116 // If this is dependent, we need to record that. If not, we simply
2117 // mangle it as the underlying type since they are equivalent.
2118 if (T->isDependentType()) {
2119 Out << 'U';
2120
2121 switch (T->getUTTKind()) {
2122 case UnaryTransformType::EnumUnderlyingType:
2123 Out << "3eut";
2124 break;
2125 }
2126 }
2127
2128 mangleType(T->getUnderlyingType());
2129}
2130
Richard Smith34b41d92011-02-20 03:19:35 +00002131void CXXNameMangler::mangleType(const AutoType *T) {
2132 QualType D = T->getDeducedType();
Richard Smith967ecd32011-02-21 20:10:02 +00002133 // <builtin-type> ::= Da # dependent auto
2134 if (D.isNull())
2135 Out << "Da";
2136 else
2137 mangleType(D);
Richard Smith34b41d92011-02-20 03:19:35 +00002138}
2139
Eli Friedmanb001de72011-10-06 23:00:33 +00002140void CXXNameMangler::mangleType(const AtomicType *T) {
2141 // <type> ::= U <source-name> <type> # vendor extended type qualifier
2142 // (Until there's a standardized mangling...)
2143 Out << "U7_Atomic";
2144 mangleType(T->getValueType());
2145}
2146
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002147void CXXNameMangler::mangleIntegerLiteral(QualType T,
Anders Carlssone170ba72009-12-14 01:45:37 +00002148 const llvm::APSInt &Value) {
2149 // <expr-primary> ::= L <type> <value number> E # integer literal
2150 Out << 'L';
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002151
Anders Carlssone170ba72009-12-14 01:45:37 +00002152 mangleType(T);
2153 if (T->isBooleanType()) {
2154 // Boolean values are encoded as 0/1.
2155 Out << (Value.getBoolValue() ? '1' : '0');
2156 } else {
John McCall0512e482010-07-14 04:20:34 +00002157 mangleNumber(Value);
Anders Carlssone170ba72009-12-14 01:45:37 +00002158 }
2159 Out << 'E';
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002160
Anders Carlssone170ba72009-12-14 01:45:37 +00002161}
2162
John McCall2f27bf82010-02-04 02:56:29 +00002163/// Mangles a member expression. Implicit accesses are not handled,
2164/// but that should be okay, because you shouldn't be able to
2165/// make an implicit access in a function template declaration.
John McCalla0ce15c2011-04-24 08:23:24 +00002166void CXXNameMangler::mangleMemberExpr(const Expr *base,
2167 bool isArrow,
2168 NestedNameSpecifier *qualifier,
2169 NamedDecl *firstQualifierLookup,
2170 DeclarationName member,
2171 unsigned arity) {
2172 // <expression> ::= dt <expression> <unresolved-name>
2173 // ::= pt <expression> <unresolved-name>
2174 Out << (isArrow ? "pt" : "dt");
2175 mangleExpression(base);
2176 mangleUnresolvedName(qualifier, firstQualifierLookup, member, arity);
John McCall2f27bf82010-02-04 02:56:29 +00002177}
2178
John McCall5a7e6f72011-04-28 02:52:03 +00002179/// Look at the callee of the given call expression and determine if
2180/// it's a parenthesized id-expression which would have triggered ADL
2181/// otherwise.
2182static bool isParenthesizedADLCallee(const CallExpr *call) {
2183 const Expr *callee = call->getCallee();
2184 const Expr *fn = callee->IgnoreParens();
2185
2186 // Must be parenthesized. IgnoreParens() skips __extension__ nodes,
2187 // too, but for those to appear in the callee, it would have to be
2188 // parenthesized.
2189 if (callee == fn) return false;
2190
2191 // Must be an unresolved lookup.
2192 const UnresolvedLookupExpr *lookup = dyn_cast<UnresolvedLookupExpr>(fn);
2193 if (!lookup) return false;
2194
2195 assert(!lookup->requiresADL());
2196
2197 // Must be an unqualified lookup.
2198 if (lookup->getQualifier()) return false;
2199
2200 // Must not have found a class member. Note that if one is a class
2201 // member, they're all class members.
2202 if (lookup->getNumDecls() > 0 &&
2203 (*lookup->decls_begin())->isCXXClassMember())
2204 return false;
2205
2206 // Otherwise, ADL would have been triggered.
2207 return true;
2208}
2209
John McCall5e1e89b2010-08-18 19:18:59 +00002210void CXXNameMangler::mangleExpression(const Expr *E, unsigned Arity) {
Anders Carlssond553f8c2009-09-21 01:21:10 +00002211 // <expression> ::= <unary operator-name> <expression>
John McCall09cc1412010-02-03 00:55:45 +00002212 // ::= <binary operator-name> <expression> <expression>
2213 // ::= <trinary operator-name> <expression> <expression> <expression>
Anders Carlssond553f8c2009-09-21 01:21:10 +00002214 // ::= cv <type> expression # conversion with one argument
2215 // ::= cv <type> _ <expression>* E # conversion with a different number of arguments
Eli Friedmana7e68452010-08-22 01:00:03 +00002216 // ::= st <type> # sizeof (a type)
Anders Carlssond553f8c2009-09-21 01:21:10 +00002217 // ::= at <type> # alignof (a type)
2218 // ::= <template-param>
2219 // ::= <function-param>
2220 // ::= sr <type> <unqualified-name> # dependent name
2221 // ::= sr <type> <unqualified-name> <template-args> # dependent template-id
Douglas Gregor63f62df2011-06-05 05:27:58 +00002222 // ::= ds <expression> <expression> # expr.*expr
Anders Carlssond553f8c2009-09-21 01:21:10 +00002223 // ::= sZ <template-param> # size of a parameter pack
Douglas Gregor4fc48662011-01-13 16:39:34 +00002224 // ::= sZ <function-param> # size of a function parameter pack
John McCall09cc1412010-02-03 00:55:45 +00002225 // ::= <expr-primary>
John McCall1dd73832010-02-04 01:42:13 +00002226 // <expr-primary> ::= L <type> <value number> E # integer literal
2227 // ::= L <type <value float> E # floating literal
2228 // ::= L <mangled-name> E # external name
Douglas Gregoredee94b2011-07-12 04:47:20 +00002229 QualType ImplicitlyConvertedToType;
2230
2231recurse:
Anders Carlssond553f8c2009-09-21 01:21:10 +00002232 switch (E->getStmtClass()) {
John McCall6ae1f352010-04-09 22:26:14 +00002233 case Expr::NoStmtClass:
John McCall63c00d72011-02-09 08:16:59 +00002234#define ABSTRACT_STMT(Type)
John McCall6ae1f352010-04-09 22:26:14 +00002235#define EXPR(Type, Base)
2236#define STMT(Type, Base) \
2237 case Expr::Type##Class:
Sean Hunt4bfe1962010-05-05 15:24:00 +00002238#include "clang/AST/StmtNodes.inc"
John McCall0512e482010-07-14 04:20:34 +00002239 // fallthrough
2240
2241 // These all can only appear in local or variable-initialization
2242 // contexts and so should never appear in a mangling.
2243 case Expr::AddrLabelExprClass:
2244 case Expr::BlockDeclRefExprClass:
2245 case Expr::CXXThisExprClass:
2246 case Expr::DesignatedInitExprClass:
2247 case Expr::ImplicitValueInitExprClass:
2248 case Expr::InitListExprClass:
2249 case Expr::ParenListExprClass:
John McCall09cc1412010-02-03 00:55:45 +00002250 llvm_unreachable("unexpected statement kind");
John McCall09cc1412010-02-03 00:55:45 +00002251
John McCall0512e482010-07-14 04:20:34 +00002252 // FIXME: invent manglings for all these.
2253 case Expr::BlockExprClass:
2254 case Expr::CXXPseudoDestructorExprClass:
2255 case Expr::ChooseExprClass:
2256 case Expr::CompoundLiteralExprClass:
2257 case Expr::ExtVectorElementExprClass:
Peter Collingbournef111d932011-04-15 00:35:48 +00002258 case Expr::GenericSelectionExprClass:
John McCall0512e482010-07-14 04:20:34 +00002259 case Expr::ObjCEncodeExprClass:
John McCall0512e482010-07-14 04:20:34 +00002260 case Expr::ObjCIsaExprClass:
2261 case Expr::ObjCIvarRefExprClass:
2262 case Expr::ObjCMessageExprClass:
2263 case Expr::ObjCPropertyRefExprClass:
2264 case Expr::ObjCProtocolExprClass:
2265 case Expr::ObjCSelectorExprClass:
2266 case Expr::ObjCStringLiteralClass:
John McCallf85e1932011-06-15 23:02:42 +00002267 case Expr::ObjCIndirectCopyRestoreExprClass:
John McCall0512e482010-07-14 04:20:34 +00002268 case Expr::OffsetOfExprClass:
2269 case Expr::PredefinedExprClass:
2270 case Expr::ShuffleVectorExprClass:
2271 case Expr::StmtExprClass:
John McCall0512e482010-07-14 04:20:34 +00002272 case Expr::UnaryTypeTraitExprClass:
Francois Pichet6ad6f282010-12-07 00:08:36 +00002273 case Expr::BinaryTypeTraitExprClass:
John Wiegley21ff2e52011-04-28 00:16:57 +00002274 case Expr::ArrayTypeTraitExprClass:
John Wiegley55262202011-04-25 06:54:41 +00002275 case Expr::ExpressionTraitExprClass:
Francois Pichet9be88402010-09-08 23:47:05 +00002276 case Expr::VAArgExprClass:
Sebastian Redl2e156222010-09-10 20:55:43 +00002277 case Expr::CXXUuidofExprClass:
Peter Collingbournee08ce652011-02-09 21:07:24 +00002278 case Expr::CXXNoexceptExprClass:
Tanya Lattner61eee0c2011-06-04 00:47:47 +00002279 case Expr::CUDAKernelCallExprClass:
2280 case Expr::AsTypeExprClass:
John McCall4b9c2d22011-11-06 09:01:30 +00002281 case Expr::PseudoObjectExprClass:
Eli Friedman276b0612011-10-11 02:20:01 +00002282 case Expr::AtomicExprClass:
Tanya Lattner61eee0c2011-06-04 00:47:47 +00002283 {
John McCall6ae1f352010-04-09 22:26:14 +00002284 // As bad as this diagnostic is, it's better than crashing.
David Blaikied6471f72011-09-25 23:23:43 +00002285 DiagnosticsEngine &Diags = Context.getDiags();
2286 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
John McCall6ae1f352010-04-09 22:26:14 +00002287 "cannot yet mangle expression type %0");
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +00002288 Diags.Report(E->getExprLoc(), DiagID)
John McCall739bf092010-04-10 09:39:25 +00002289 << E->getStmtClassName() << E->getSourceRange();
John McCall6ae1f352010-04-09 22:26:14 +00002290 break;
2291 }
2292
John McCall56ca35d2011-02-17 10:25:35 +00002293 // Even gcc-4.5 doesn't mangle this.
2294 case Expr::BinaryConditionalOperatorClass: {
David Blaikied6471f72011-09-25 23:23:43 +00002295 DiagnosticsEngine &Diags = Context.getDiags();
John McCall56ca35d2011-02-17 10:25:35 +00002296 unsigned DiagID =
David Blaikied6471f72011-09-25 23:23:43 +00002297 Diags.getCustomDiagID(DiagnosticsEngine::Error,
John McCall56ca35d2011-02-17 10:25:35 +00002298 "?: operator with omitted middle operand cannot be mangled");
2299 Diags.Report(E->getExprLoc(), DiagID)
2300 << E->getStmtClassName() << E->getSourceRange();
2301 break;
2302 }
2303
2304 // These are used for internal purposes and cannot be meaningfully mangled.
John McCall7cd7d1a2010-11-15 23:31:06 +00002305 case Expr::OpaqueValueExprClass:
2306 llvm_unreachable("cannot mangle opaque value; mangling wrong thing?");
2307
John McCall0512e482010-07-14 04:20:34 +00002308 case Expr::CXXDefaultArgExprClass:
John McCall5e1e89b2010-08-18 19:18:59 +00002309 mangleExpression(cast<CXXDefaultArgExpr>(E)->getExpr(), Arity);
John McCall0512e482010-07-14 04:20:34 +00002310 break;
2311
John McCall91a57552011-07-15 05:09:51 +00002312 case Expr::SubstNonTypeTemplateParmExprClass:
2313 mangleExpression(cast<SubstNonTypeTemplateParmExpr>(E)->getReplacement(),
2314 Arity);
2315 break;
2316
John McCall0512e482010-07-14 04:20:34 +00002317 case Expr::CXXMemberCallExprClass: // fallthrough
John McCall1dd73832010-02-04 01:42:13 +00002318 case Expr::CallExprClass: {
2319 const CallExpr *CE = cast<CallExpr>(E);
John McCall5a7e6f72011-04-28 02:52:03 +00002320
2321 // <expression> ::= cp <simple-id> <expression>* E
2322 // We use this mangling only when the call would use ADL except
2323 // for being parenthesized. Per discussion with David
2324 // Vandervoorde, 2011.04.25.
2325 if (isParenthesizedADLCallee(CE)) {
2326 Out << "cp";
2327 // The callee here is a parenthesized UnresolvedLookupExpr with
2328 // no qualifier and should always get mangled as a <simple-id>
2329 // anyway.
2330
2331 // <expression> ::= cl <expression>* E
2332 } else {
2333 Out << "cl";
2334 }
2335
John McCall5e1e89b2010-08-18 19:18:59 +00002336 mangleExpression(CE->getCallee(), CE->getNumArgs());
John McCall1dd73832010-02-04 01:42:13 +00002337 for (unsigned I = 0, N = CE->getNumArgs(); I != N; ++I)
2338 mangleExpression(CE->getArg(I));
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002339 Out << 'E';
John McCall09cc1412010-02-03 00:55:45 +00002340 break;
John McCall1dd73832010-02-04 01:42:13 +00002341 }
John McCall09cc1412010-02-03 00:55:45 +00002342
John McCall0512e482010-07-14 04:20:34 +00002343 case Expr::CXXNewExprClass: {
2344 // Proposal from David Vandervoorde, 2010.06.30
2345 const CXXNewExpr *New = cast<CXXNewExpr>(E);
2346 if (New->isGlobalNew()) Out << "gs";
2347 Out << (New->isArray() ? "na" : "nw");
2348 for (CXXNewExpr::const_arg_iterator I = New->placement_arg_begin(),
2349 E = New->placement_arg_end(); I != E; ++I)
2350 mangleExpression(*I);
2351 Out << '_';
2352 mangleType(New->getAllocatedType());
2353 if (New->hasInitializer()) {
2354 Out << "pi";
2355 for (CXXNewExpr::const_arg_iterator I = New->constructor_arg_begin(),
2356 E = New->constructor_arg_end(); I != E; ++I)
2357 mangleExpression(*I);
2358 }
2359 Out << 'E';
2360 break;
2361 }
2362
John McCall2f27bf82010-02-04 02:56:29 +00002363 case Expr::MemberExprClass: {
2364 const MemberExpr *ME = cast<MemberExpr>(E);
2365 mangleMemberExpr(ME->getBase(), ME->isArrow(),
John McCalla0ce15c2011-04-24 08:23:24 +00002366 ME->getQualifier(), 0, ME->getMemberDecl()->getDeclName(),
John McCall5e1e89b2010-08-18 19:18:59 +00002367 Arity);
John McCall2f27bf82010-02-04 02:56:29 +00002368 break;
2369 }
2370
2371 case Expr::UnresolvedMemberExprClass: {
2372 const UnresolvedMemberExpr *ME = cast<UnresolvedMemberExpr>(E);
2373 mangleMemberExpr(ME->getBase(), ME->isArrow(),
John McCalla0ce15c2011-04-24 08:23:24 +00002374 ME->getQualifier(), 0, ME->getMemberName(),
John McCall5e1e89b2010-08-18 19:18:59 +00002375 Arity);
John McCall6dbce192010-08-20 00:17:19 +00002376 if (ME->hasExplicitTemplateArgs())
2377 mangleTemplateArgs(ME->getExplicitTemplateArgs());
John McCall2f27bf82010-02-04 02:56:29 +00002378 break;
2379 }
2380
2381 case Expr::CXXDependentScopeMemberExprClass: {
2382 const CXXDependentScopeMemberExpr *ME
2383 = cast<CXXDependentScopeMemberExpr>(E);
2384 mangleMemberExpr(ME->getBase(), ME->isArrow(),
John McCalla0ce15c2011-04-24 08:23:24 +00002385 ME->getQualifier(), ME->getFirstQualifierFoundInScope(),
2386 ME->getMember(), Arity);
John McCall6dbce192010-08-20 00:17:19 +00002387 if (ME->hasExplicitTemplateArgs())
2388 mangleTemplateArgs(ME->getExplicitTemplateArgs());
John McCall2f27bf82010-02-04 02:56:29 +00002389 break;
2390 }
2391
John McCall1dd73832010-02-04 01:42:13 +00002392 case Expr::UnresolvedLookupExprClass: {
2393 const UnresolvedLookupExpr *ULE = cast<UnresolvedLookupExpr>(E);
John McCalla0ce15c2011-04-24 08:23:24 +00002394 mangleUnresolvedName(ULE->getQualifier(), 0, ULE->getName(), Arity);
John McCall26a6ec72011-06-21 22:12:46 +00002395
2396 // All the <unresolved-name> productions end in a
2397 // base-unresolved-name, where <template-args> are just tacked
2398 // onto the end.
John McCall6dbce192010-08-20 00:17:19 +00002399 if (ULE->hasExplicitTemplateArgs())
2400 mangleTemplateArgs(ULE->getExplicitTemplateArgs());
John McCall1dd73832010-02-04 01:42:13 +00002401 break;
2402 }
2403
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002404 case Expr::CXXUnresolvedConstructExprClass: {
John McCall1dd73832010-02-04 01:42:13 +00002405 const CXXUnresolvedConstructExpr *CE = cast<CXXUnresolvedConstructExpr>(E);
2406 unsigned N = CE->arg_size();
2407
2408 Out << "cv";
2409 mangleType(CE->getType());
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002410 if (N != 1) Out << '_';
John McCall1dd73832010-02-04 01:42:13 +00002411 for (unsigned I = 0; I != N; ++I) mangleExpression(CE->getArg(I));
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002412 if (N != 1) Out << 'E';
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002413 break;
John McCall1dd73832010-02-04 01:42:13 +00002414 }
John McCall09cc1412010-02-03 00:55:45 +00002415
John McCall1dd73832010-02-04 01:42:13 +00002416 case Expr::CXXTemporaryObjectExprClass:
2417 case Expr::CXXConstructExprClass: {
2418 const CXXConstructExpr *CE = cast<CXXConstructExpr>(E);
2419 unsigned N = CE->getNumArgs();
2420
2421 Out << "cv";
2422 mangleType(CE->getType());
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002423 if (N != 1) Out << '_';
John McCall1dd73832010-02-04 01:42:13 +00002424 for (unsigned I = 0; I != N; ++I) mangleExpression(CE->getArg(I));
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002425 if (N != 1) Out << 'E';
John McCall09cc1412010-02-03 00:55:45 +00002426 break;
John McCall1dd73832010-02-04 01:42:13 +00002427 }
2428
Richard Smith41576d42012-02-06 02:54:51 +00002429 case Expr::CXXScalarValueInitExprClass:
2430 Out <<"cv";
2431 mangleType(E->getType());
2432 Out <<"_E";
2433 break;
2434
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +00002435 case Expr::UnaryExprOrTypeTraitExprClass: {
2436 const UnaryExprOrTypeTraitExpr *SAE = cast<UnaryExprOrTypeTraitExpr>(E);
Douglas Gregoredee94b2011-07-12 04:47:20 +00002437
2438 if (!SAE->isInstantiationDependent()) {
2439 // Itanium C++ ABI:
2440 // If the operand of a sizeof or alignof operator is not
2441 // instantiation-dependent it is encoded as an integer literal
2442 // reflecting the result of the operator.
2443 //
2444 // If the result of the operator is implicitly converted to a known
2445 // integer type, that type is used for the literal; otherwise, the type
2446 // of std::size_t or std::ptrdiff_t is used.
2447 QualType T = (ImplicitlyConvertedToType.isNull() ||
2448 !ImplicitlyConvertedToType->isIntegerType())? SAE->getType()
2449 : ImplicitlyConvertedToType;
Richard Smitha6b8b2c2011-10-10 18:28:20 +00002450 llvm::APSInt V = SAE->EvaluateKnownConstInt(Context.getASTContext());
2451 mangleIntegerLiteral(T, V);
Douglas Gregoredee94b2011-07-12 04:47:20 +00002452 break;
2453 }
2454
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +00002455 switch(SAE->getKind()) {
2456 case UETT_SizeOf:
2457 Out << 's';
2458 break;
2459 case UETT_AlignOf:
2460 Out << 'a';
2461 break;
2462 case UETT_VecStep:
David Blaikied6471f72011-09-25 23:23:43 +00002463 DiagnosticsEngine &Diags = Context.getDiags();
2464 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +00002465 "cannot yet mangle vec_step expression");
2466 Diags.Report(DiagID);
2467 return;
2468 }
John McCall1dd73832010-02-04 01:42:13 +00002469 if (SAE->isArgumentType()) {
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002470 Out << 't';
John McCall1dd73832010-02-04 01:42:13 +00002471 mangleType(SAE->getArgumentType());
2472 } else {
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002473 Out << 'z';
John McCall1dd73832010-02-04 01:42:13 +00002474 mangleExpression(SAE->getArgumentExpr());
2475 }
2476 break;
2477 }
Anders Carlssona7694082009-11-06 02:50:19 +00002478
John McCall0512e482010-07-14 04:20:34 +00002479 case Expr::CXXThrowExprClass: {
2480 const CXXThrowExpr *TE = cast<CXXThrowExpr>(E);
2481
2482 // Proposal from David Vandervoorde, 2010.06.30
2483 if (TE->getSubExpr()) {
2484 Out << "tw";
2485 mangleExpression(TE->getSubExpr());
2486 } else {
2487 Out << "tr";
2488 }
2489 break;
2490 }
2491
2492 case Expr::CXXTypeidExprClass: {
2493 const CXXTypeidExpr *TIE = cast<CXXTypeidExpr>(E);
2494
2495 // Proposal from David Vandervoorde, 2010.06.30
2496 if (TIE->isTypeOperand()) {
2497 Out << "ti";
2498 mangleType(TIE->getTypeOperand());
2499 } else {
2500 Out << "te";
2501 mangleExpression(TIE->getExprOperand());
2502 }
2503 break;
2504 }
2505
2506 case Expr::CXXDeleteExprClass: {
2507 const CXXDeleteExpr *DE = cast<CXXDeleteExpr>(E);
2508
2509 // Proposal from David Vandervoorde, 2010.06.30
2510 if (DE->isGlobalDelete()) Out << "gs";
2511 Out << (DE->isArrayForm() ? "da" : "dl");
2512 mangleExpression(DE->getArgument());
2513 break;
2514 }
2515
Anders Carlssone170ba72009-12-14 01:45:37 +00002516 case Expr::UnaryOperatorClass: {
2517 const UnaryOperator *UO = cast<UnaryOperator>(E);
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002518 mangleOperatorName(UnaryOperator::getOverloadedOperator(UO->getOpcode()),
Anders Carlssone170ba72009-12-14 01:45:37 +00002519 /*Arity=*/1);
2520 mangleExpression(UO->getSubExpr());
2521 break;
2522 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002523
John McCall0512e482010-07-14 04:20:34 +00002524 case Expr::ArraySubscriptExprClass: {
2525 const ArraySubscriptExpr *AE = cast<ArraySubscriptExpr>(E);
2526
Chris Lattnerfc8f0e12011-04-15 05:22:18 +00002527 // Array subscript is treated as a syntactically weird form of
John McCall0512e482010-07-14 04:20:34 +00002528 // binary operator.
2529 Out << "ix";
2530 mangleExpression(AE->getLHS());
2531 mangleExpression(AE->getRHS());
2532 break;
2533 }
2534
2535 case Expr::CompoundAssignOperatorClass: // fallthrough
Anders Carlssone170ba72009-12-14 01:45:37 +00002536 case Expr::BinaryOperatorClass: {
2537 const BinaryOperator *BO = cast<BinaryOperator>(E);
Douglas Gregor63f62df2011-06-05 05:27:58 +00002538 if (BO->getOpcode() == BO_PtrMemD)
2539 Out << "ds";
2540 else
2541 mangleOperatorName(BinaryOperator::getOverloadedOperator(BO->getOpcode()),
2542 /*Arity=*/2);
Anders Carlssone170ba72009-12-14 01:45:37 +00002543 mangleExpression(BO->getLHS());
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002544 mangleExpression(BO->getRHS());
Anders Carlssone170ba72009-12-14 01:45:37 +00002545 break;
John McCall2f27bf82010-02-04 02:56:29 +00002546 }
Anders Carlssone170ba72009-12-14 01:45:37 +00002547
2548 case Expr::ConditionalOperatorClass: {
2549 const ConditionalOperator *CO = cast<ConditionalOperator>(E);
2550 mangleOperatorName(OO_Conditional, /*Arity=*/3);
2551 mangleExpression(CO->getCond());
John McCall5e1e89b2010-08-18 19:18:59 +00002552 mangleExpression(CO->getLHS(), Arity);
2553 mangleExpression(CO->getRHS(), Arity);
Anders Carlssone170ba72009-12-14 01:45:37 +00002554 break;
2555 }
2556
Douglas Gregor46287c72010-01-29 16:37:09 +00002557 case Expr::ImplicitCastExprClass: {
Douglas Gregoredee94b2011-07-12 04:47:20 +00002558 ImplicitlyConvertedToType = E->getType();
2559 E = cast<ImplicitCastExpr>(E)->getSubExpr();
2560 goto recurse;
Douglas Gregor46287c72010-01-29 16:37:09 +00002561 }
John McCallf85e1932011-06-15 23:02:42 +00002562
2563 case Expr::ObjCBridgedCastExprClass: {
2564 // Mangle ownership casts as a vendor extended operator __bridge,
2565 // __bridge_transfer, or __bridge_retain.
Chris Lattner5f9e2722011-07-23 10:55:15 +00002566 StringRef Kind = cast<ObjCBridgedCastExpr>(E)->getBridgeKindName();
John McCallf85e1932011-06-15 23:02:42 +00002567 Out << "v1U" << Kind.size() << Kind;
2568 }
2569 // Fall through to mangle the cast itself.
2570
Douglas Gregor46287c72010-01-29 16:37:09 +00002571 case Expr::CStyleCastExprClass:
2572 case Expr::CXXStaticCastExprClass:
2573 case Expr::CXXDynamicCastExprClass:
2574 case Expr::CXXReinterpretCastExprClass:
2575 case Expr::CXXConstCastExprClass:
2576 case Expr::CXXFunctionalCastExprClass: {
2577 const ExplicitCastExpr *ECE = cast<ExplicitCastExpr>(E);
2578 Out << "cv";
2579 mangleType(ECE->getType());
2580 mangleExpression(ECE->getSubExpr());
2581 break;
2582 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002583
Anders Carlsson58040a52009-12-16 05:48:46 +00002584 case Expr::CXXOperatorCallExprClass: {
2585 const CXXOperatorCallExpr *CE = cast<CXXOperatorCallExpr>(E);
2586 unsigned NumArgs = CE->getNumArgs();
2587 mangleOperatorName(CE->getOperator(), /*Arity=*/NumArgs);
2588 // Mangle the arguments.
2589 for (unsigned i = 0; i != NumArgs; ++i)
2590 mangleExpression(CE->getArg(i));
2591 break;
2592 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002593
Anders Carlssona7694082009-11-06 02:50:19 +00002594 case Expr::ParenExprClass:
John McCall5e1e89b2010-08-18 19:18:59 +00002595 mangleExpression(cast<ParenExpr>(E)->getSubExpr(), Arity);
Anders Carlssona7694082009-11-06 02:50:19 +00002596 break;
2597
Anders Carlssond553f8c2009-09-21 01:21:10 +00002598 case Expr::DeclRefExprClass: {
Douglas Gregor5ed1bc32010-02-28 21:40:32 +00002599 const NamedDecl *D = cast<DeclRefExpr>(E)->getDecl();
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002600
Anders Carlssond553f8c2009-09-21 01:21:10 +00002601 switch (D->getKind()) {
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002602 default:
Douglas Gregor5ed1bc32010-02-28 21:40:32 +00002603 // <expr-primary> ::= L <mangled-name> E # external name
2604 Out << 'L';
2605 mangle(D, "_Z");
2606 Out << 'E';
2607 break;
2608
John McCallfb44de92011-05-01 22:35:37 +00002609 case Decl::ParmVar:
2610 mangleFunctionParam(cast<ParmVarDecl>(D));
2611 break;
2612
John McCall3dc7e7b2010-07-24 01:17:35 +00002613 case Decl::EnumConstant: {
2614 const EnumConstantDecl *ED = cast<EnumConstantDecl>(D);
2615 mangleIntegerLiteral(ED->getType(), ED->getInitVal());
2616 break;
2617 }
2618
Anders Carlssond553f8c2009-09-21 01:21:10 +00002619 case Decl::NonTypeTemplateParm: {
2620 const NonTypeTemplateParmDecl *PD = cast<NonTypeTemplateParmDecl>(D);
Anders Carlsson0ccdf8d2009-09-27 00:38:53 +00002621 mangleTemplateParameter(PD->getIndex());
Anders Carlssond553f8c2009-09-21 01:21:10 +00002622 break;
2623 }
2624
2625 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002626
Anders Carlsson50755b02009-09-27 20:11:34 +00002627 break;
Anders Carlssond553f8c2009-09-21 01:21:10 +00002628 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00002629
Douglas Gregorc7793c72011-01-15 01:15:58 +00002630 case Expr::SubstNonTypeTemplateParmPackExprClass:
John McCall68a51a72011-07-01 00:04:39 +00002631 // FIXME: not clear how to mangle this!
2632 // template <unsigned N...> class A {
2633 // template <class U...> void foo(U (&x)[N]...);
2634 // };
2635 Out << "_SUBSTPACK_";
Douglas Gregorc7793c72011-01-15 01:15:58 +00002636 break;
2637
John McCall865d4472009-11-19 22:55:06 +00002638 case Expr::DependentScopeDeclRefExprClass: {
2639 const DependentScopeDeclRefExpr *DRE = cast<DependentScopeDeclRefExpr>(E);
John McCall26a6ec72011-06-21 22:12:46 +00002640 mangleUnresolvedName(DRE->getQualifier(), 0, DRE->getDeclName(), Arity);
Douglas Gregor4b2ccfc2010-02-28 22:05:49 +00002641
John McCall26a6ec72011-06-21 22:12:46 +00002642 // All the <unresolved-name> productions end in a
2643 // base-unresolved-name, where <template-args> are just tacked
2644 // onto the end.
John McCall6dbce192010-08-20 00:17:19 +00002645 if (DRE->hasExplicitTemplateArgs())
2646 mangleTemplateArgs(DRE->getExplicitTemplateArgs());
Anders Carlsson50755b02009-09-27 20:11:34 +00002647 break;
2648 }
2649
John McCalld9307602010-04-09 22:54:09 +00002650 case Expr::CXXBindTemporaryExprClass:
2651 mangleExpression(cast<CXXBindTemporaryExpr>(E)->getSubExpr());
2652 break;
2653
John McCall4765fa02010-12-06 08:20:24 +00002654 case Expr::ExprWithCleanupsClass:
2655 mangleExpression(cast<ExprWithCleanups>(E)->getSubExpr(), Arity);
John McCalld9307602010-04-09 22:54:09 +00002656 break;
2657
John McCall1dd73832010-02-04 01:42:13 +00002658 case Expr::FloatingLiteralClass: {
2659 const FloatingLiteral *FL = cast<FloatingLiteral>(E);
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002660 Out << 'L';
John McCall1dd73832010-02-04 01:42:13 +00002661 mangleType(FL->getType());
John McCall0512e482010-07-14 04:20:34 +00002662 mangleFloat(FL->getValue());
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002663 Out << 'E';
John McCall1dd73832010-02-04 01:42:13 +00002664 break;
2665 }
2666
John McCallde810632010-04-09 21:48:08 +00002667 case Expr::CharacterLiteralClass:
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002668 Out << 'L';
John McCallde810632010-04-09 21:48:08 +00002669 mangleType(E->getType());
2670 Out << cast<CharacterLiteral>(E)->getValue();
2671 Out << 'E';
2672 break;
2673
2674 case Expr::CXXBoolLiteralExprClass:
2675 Out << "Lb";
2676 Out << (cast<CXXBoolLiteralExpr>(E)->getValue() ? '1' : '0');
2677 Out << 'E';
2678 break;
2679
John McCall0512e482010-07-14 04:20:34 +00002680 case Expr::IntegerLiteralClass: {
2681 llvm::APSInt Value(cast<IntegerLiteral>(E)->getValue());
2682 if (E->getType()->isSignedIntegerType())
2683 Value.setIsSigned(true);
2684 mangleIntegerLiteral(E->getType(), Value);
Anders Carlssone170ba72009-12-14 01:45:37 +00002685 break;
John McCall0512e482010-07-14 04:20:34 +00002686 }
2687
2688 case Expr::ImaginaryLiteralClass: {
2689 const ImaginaryLiteral *IE = cast<ImaginaryLiteral>(E);
2690 // Mangle as if a complex literal.
Nick Lewycky271b6652010-09-05 03:40:33 +00002691 // Proposal from David Vandevoorde, 2010.06.30.
John McCall0512e482010-07-14 04:20:34 +00002692 Out << 'L';
2693 mangleType(E->getType());
2694 if (const FloatingLiteral *Imag =
2695 dyn_cast<FloatingLiteral>(IE->getSubExpr())) {
2696 // Mangle a floating-point zero of the appropriate type.
2697 mangleFloat(llvm::APFloat(Imag->getValue().getSemantics()));
2698 Out << '_';
2699 mangleFloat(Imag->getValue());
2700 } else {
Nick Lewycky271b6652010-09-05 03:40:33 +00002701 Out << "0_";
John McCall0512e482010-07-14 04:20:34 +00002702 llvm::APSInt Value(cast<IntegerLiteral>(IE->getSubExpr())->getValue());
2703 if (IE->getSubExpr()->getType()->isSignedIntegerType())
2704 Value.setIsSigned(true);
2705 mangleNumber(Value);
2706 }
2707 Out << 'E';
2708 break;
2709 }
2710
2711 case Expr::StringLiteralClass: {
John McCall1658c392010-07-15 21:53:03 +00002712 // Revised proposal from David Vandervoorde, 2010.07.15.
John McCall0512e482010-07-14 04:20:34 +00002713 Out << 'L';
John McCall1658c392010-07-15 21:53:03 +00002714 assert(isa<ConstantArrayType>(E->getType()));
2715 mangleType(E->getType());
John McCall0512e482010-07-14 04:20:34 +00002716 Out << 'E';
2717 break;
2718 }
2719
2720 case Expr::GNUNullExprClass:
2721 // FIXME: should this really be mangled the same as nullptr?
2722 // fallthrough
2723
2724 case Expr::CXXNullPtrLiteralExprClass: {
2725 // Proposal from David Vandervoorde, 2010.06.30, as
2726 // modified by ABI list discussion.
2727 Out << "LDnE";
2728 break;
2729 }
Douglas Gregorbe230c32011-01-03 17:17:50 +00002730
2731 case Expr::PackExpansionExprClass:
2732 Out << "sp";
2733 mangleExpression(cast<PackExpansionExpr>(E)->getPattern());
2734 break;
Douglas Gregor2e774c42011-01-04 18:56:13 +00002735
2736 case Expr::SizeOfPackExprClass: {
Douglas Gregor2e774c42011-01-04 18:56:13 +00002737 Out << "sZ";
2738 const NamedDecl *Pack = cast<SizeOfPackExpr>(E)->getPack();
2739 if (const TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(Pack))
2740 mangleTemplateParameter(TTP->getIndex());
2741 else if (const NonTypeTemplateParmDecl *NTTP
2742 = dyn_cast<NonTypeTemplateParmDecl>(Pack))
2743 mangleTemplateParameter(NTTP->getIndex());
2744 else if (const TemplateTemplateParmDecl *TempTP
2745 = dyn_cast<TemplateTemplateParmDecl>(Pack))
2746 mangleTemplateParameter(TempTP->getIndex());
Douglas Gregor91832362011-07-12 07:03:48 +00002747 else
2748 mangleFunctionParam(cast<ParmVarDecl>(Pack));
Douglas Gregordfbbcf92011-03-03 02:20:19 +00002749 break;
Douglas Gregor2e774c42011-01-04 18:56:13 +00002750 }
Douglas Gregor03e80032011-06-21 17:03:29 +00002751
2752 case Expr::MaterializeTemporaryExprClass: {
2753 mangleExpression(cast<MaterializeTemporaryExpr>(E)->GetTemporaryExpr());
2754 break;
2755 }
Anders Carlssond553f8c2009-09-21 01:21:10 +00002756 }
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00002757}
2758
John McCallfb44de92011-05-01 22:35:37 +00002759/// Mangle an expression which refers to a parameter variable.
2760///
2761/// <expression> ::= <function-param>
2762/// <function-param> ::= fp <top-level CV-qualifiers> _ # L == 0, I == 0
2763/// <function-param> ::= fp <top-level CV-qualifiers>
2764/// <parameter-2 non-negative number> _ # L == 0, I > 0
2765/// <function-param> ::= fL <L-1 non-negative number>
2766/// p <top-level CV-qualifiers> _ # L > 0, I == 0
2767/// <function-param> ::= fL <L-1 non-negative number>
2768/// p <top-level CV-qualifiers>
2769/// <I-1 non-negative number> _ # L > 0, I > 0
2770///
2771/// L is the nesting depth of the parameter, defined as 1 if the
2772/// parameter comes from the innermost function prototype scope
2773/// enclosing the current context, 2 if from the next enclosing
2774/// function prototype scope, and so on, with one special case: if
2775/// we've processed the full parameter clause for the innermost
2776/// function type, then L is one less. This definition conveniently
2777/// makes it irrelevant whether a function's result type was written
2778/// trailing or leading, but is otherwise overly complicated; the
2779/// numbering was first designed without considering references to
2780/// parameter in locations other than return types, and then the
2781/// mangling had to be generalized without changing the existing
2782/// manglings.
2783///
2784/// I is the zero-based index of the parameter within its parameter
2785/// declaration clause. Note that the original ABI document describes
2786/// this using 1-based ordinals.
2787void CXXNameMangler::mangleFunctionParam(const ParmVarDecl *parm) {
2788 unsigned parmDepth = parm->getFunctionScopeDepth();
2789 unsigned parmIndex = parm->getFunctionScopeIndex();
2790
2791 // Compute 'L'.
2792 // parmDepth does not include the declaring function prototype.
2793 // FunctionTypeDepth does account for that.
2794 assert(parmDepth < FunctionTypeDepth.getDepth());
2795 unsigned nestingDepth = FunctionTypeDepth.getDepth() - parmDepth;
2796 if (FunctionTypeDepth.isInResultType())
2797 nestingDepth--;
2798
2799 if (nestingDepth == 0) {
2800 Out << "fp";
2801 } else {
2802 Out << "fL" << (nestingDepth - 1) << 'p';
2803 }
2804
2805 // Top-level qualifiers. We don't have to worry about arrays here,
2806 // because parameters declared as arrays should already have been
2807 // tranformed to have pointer type. FIXME: apparently these don't
2808 // get mangled if used as an rvalue of a known non-class type?
2809 assert(!parm->getType()->isArrayType()
2810 && "parameter's type is still an array type?");
2811 mangleQualifiers(parm->getType().getQualifiers());
2812
2813 // Parameter index.
2814 if (parmIndex != 0) {
2815 Out << (parmIndex - 1);
2816 }
2817 Out << '_';
2818}
2819
Anders Carlsson3ac86b52009-04-15 05:36:58 +00002820void CXXNameMangler::mangleCXXCtorType(CXXCtorType T) {
2821 // <ctor-dtor-name> ::= C1 # complete object constructor
2822 // ::= C2 # base object constructor
2823 // ::= C3 # complete object allocating constructor
2824 //
2825 switch (T) {
2826 case Ctor_Complete:
2827 Out << "C1";
2828 break;
2829 case Ctor_Base:
2830 Out << "C2";
2831 break;
2832 case Ctor_CompleteAllocating:
2833 Out << "C3";
2834 break;
2835 }
2836}
2837
Anders Carlsson27ae5362009-04-17 01:58:57 +00002838void CXXNameMangler::mangleCXXDtorType(CXXDtorType T) {
2839 // <ctor-dtor-name> ::= D0 # deleting destructor
2840 // ::= D1 # complete object destructor
2841 // ::= D2 # base object destructor
2842 //
2843 switch (T) {
2844 case Dtor_Deleting:
2845 Out << "D0";
2846 break;
2847 case Dtor_Complete:
2848 Out << "D1";
2849 break;
2850 case Dtor_Base:
2851 Out << "D2";
2852 break;
2853 }
2854}
2855
John McCall6dbce192010-08-20 00:17:19 +00002856void CXXNameMangler::mangleTemplateArgs(
Argyrios Kyrtzidisb0c3e092011-09-22 20:07:03 +00002857 const ASTTemplateArgumentListInfo &TemplateArgs) {
John McCall6dbce192010-08-20 00:17:19 +00002858 // <template-args> ::= I <template-arg>+ E
2859 Out << 'I';
John McCall4f4e4132011-05-04 01:45:19 +00002860 for (unsigned i = 0, e = TemplateArgs.NumTemplateArgs; i != e; ++i)
2861 mangleTemplateArg(0, TemplateArgs.getTemplateArgs()[i].getArgument());
John McCall6dbce192010-08-20 00:17:19 +00002862 Out << 'E';
2863}
2864
Douglas Gregor20f0cc72010-04-23 03:10:43 +00002865void CXXNameMangler::mangleTemplateArgs(TemplateName Template,
2866 const TemplateArgument *TemplateArgs,
2867 unsigned NumTemplateArgs) {
2868 if (TemplateDecl *TD = Template.getAsTemplateDecl())
2869 return mangleTemplateArgs(*TD->getTemplateParameters(), TemplateArgs,
2870 NumTemplateArgs);
Sean Huntc3021132010-05-05 15:23:54 +00002871
John McCall4f4e4132011-05-04 01:45:19 +00002872 mangleUnresolvedTemplateArgs(TemplateArgs, NumTemplateArgs);
2873}
2874
2875void CXXNameMangler::mangleUnresolvedTemplateArgs(const TemplateArgument *args,
2876 unsigned numArgs) {
Douglas Gregor20f0cc72010-04-23 03:10:43 +00002877 // <template-args> ::= I <template-arg>+ E
2878 Out << 'I';
John McCall4f4e4132011-05-04 01:45:19 +00002879 for (unsigned i = 0; i != numArgs; ++i)
2880 mangleTemplateArg(0, args[i]);
Douglas Gregor20f0cc72010-04-23 03:10:43 +00002881 Out << 'E';
2882}
2883
Rafael Espindolad9800722010-03-11 14:07:00 +00002884void CXXNameMangler::mangleTemplateArgs(const TemplateParameterList &PL,
2885 const TemplateArgumentList &AL) {
Anders Carlsson7a0ba872009-05-15 16:09:15 +00002886 // <template-args> ::= I <template-arg>+ E
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002887 Out << 'I';
Rafael Espindolad9800722010-03-11 14:07:00 +00002888 for (unsigned i = 0, e = AL.size(); i != e; ++i)
2889 mangleTemplateArg(PL.getParam(i), AL[i]);
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002890 Out << 'E';
Anders Carlsson7a0ba872009-05-15 16:09:15 +00002891}
2892
Rafael Espindolad9800722010-03-11 14:07:00 +00002893void CXXNameMangler::mangleTemplateArgs(const TemplateParameterList &PL,
2894 const TemplateArgument *TemplateArgs,
Anders Carlsson7624f212009-09-18 02:42:01 +00002895 unsigned NumTemplateArgs) {
2896 // <template-args> ::= I <template-arg>+ E
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002897 Out << 'I';
Daniel Dunbar7e0c1952009-11-21 09:17:15 +00002898 for (unsigned i = 0; i != NumTemplateArgs; ++i)
Rafael Espindolad9800722010-03-11 14:07:00 +00002899 mangleTemplateArg(PL.getParam(i), TemplateArgs[i]);
Benjamin Kramer35f59b62010-04-10 16:03:31 +00002900 Out << 'E';
Anders Carlsson7624f212009-09-18 02:42:01 +00002901}
2902
Rafael Espindolad9800722010-03-11 14:07:00 +00002903void CXXNameMangler::mangleTemplateArg(const NamedDecl *P,
Douglas Gregorf1588662011-07-12 15:18:55 +00002904 TemplateArgument A) {
Mike Stump1eb44332009-09-09 15:08:12 +00002905 // <template-arg> ::= <type> # type or template
Anders Carlsson7a0ba872009-05-15 16:09:15 +00002906 // ::= X <expression> E # expression
2907 // ::= <expr-primary> # simple expressions
Douglas Gregor4fc48662011-01-13 16:39:34 +00002908 // ::= J <template-arg>* E # argument pack
Douglas Gregorf1588662011-07-12 15:18:55 +00002909 // ::= sp <expression> # pack expansion of (C++0x)
2910 if (!A.isInstantiationDependent() || A.isDependent())
2911 A = Context.getASTContext().getCanonicalTemplateArgument(A);
2912
Anders Carlsson7a0ba872009-05-15 16:09:15 +00002913 switch (A.getKind()) {
Douglas Gregorf90b27a2011-01-03 22:36:02 +00002914 case TemplateArgument::Null:
2915 llvm_unreachable("Cannot mangle NULL template argument");
2916
Anders Carlsson7a0ba872009-05-15 16:09:15 +00002917 case TemplateArgument::Type:
2918 mangleType(A.getAsType());
2919 break;
Anders Carlsson9e85c742009-12-23 19:30:55 +00002920 case TemplateArgument::Template:
John McCallb6f532e2010-07-14 06:43:17 +00002921 // This is mangled as <type>.
2922 mangleType(A.getAsTemplate());
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002923 break;
Douglas Gregora7fc9012011-01-05 18:58:31 +00002924 case TemplateArgument::TemplateExpansion:
Douglas Gregor4fc48662011-01-13 16:39:34 +00002925 // <type> ::= Dp <type> # pack expansion (C++0x)
Douglas Gregora7fc9012011-01-05 18:58:31 +00002926 Out << "Dp";
2927 mangleType(A.getAsTemplateOrTemplatePattern());
2928 break;
John McCall092beef2012-01-06 05:06:35 +00002929 case TemplateArgument::Expression: {
2930 // It's possible to end up with a DeclRefExpr here in certain
2931 // dependent cases, in which case we should mangle as a
2932 // declaration.
2933 const Expr *E = A.getAsExpr()->IgnoreParens();
2934 if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E)) {
2935 const ValueDecl *D = DRE->getDecl();
2936 if (isa<VarDecl>(D) || isa<FunctionDecl>(D)) {
2937 Out << "L";
2938 mangle(D, "_Z");
2939 Out << 'E';
2940 break;
2941 }
2942 }
2943
Anders Carlssond553f8c2009-09-21 01:21:10 +00002944 Out << 'X';
John McCall092beef2012-01-06 05:06:35 +00002945 mangleExpression(E);
Anders Carlssond553f8c2009-09-21 01:21:10 +00002946 Out << 'E';
2947 break;
John McCall092beef2012-01-06 05:06:35 +00002948 }
Anders Carlssone170ba72009-12-14 01:45:37 +00002949 case TemplateArgument::Integral:
2950 mangleIntegerLiteral(A.getIntegralType(), *A.getAsIntegral());
Anders Carlsson7a0ba872009-05-15 16:09:15 +00002951 break;
Daniel Dunbar7e0c1952009-11-21 09:17:15 +00002952 case TemplateArgument::Declaration: {
Douglas Gregor20f0cc72010-04-23 03:10:43 +00002953 assert(P && "Missing template parameter for declaration argument");
Daniel Dunbar7e0c1952009-11-21 09:17:15 +00002954 // <expr-primary> ::= L <mangled-name> E # external name
2955
Rafael Espindolad9800722010-03-11 14:07:00 +00002956 // Clang produces AST's where pointer-to-member-function expressions
Daniel Dunbar7e0c1952009-11-21 09:17:15 +00002957 // and pointer-to-function expressions are represented as a declaration not
Rafael Espindolad9800722010-03-11 14:07:00 +00002958 // an expression. We compensate for it here to produce the correct mangling.
2959 NamedDecl *D = cast<NamedDecl>(A.getAsDecl());
2960 const NonTypeTemplateParmDecl *Parameter = cast<NonTypeTemplateParmDecl>(P);
John McCallc0a45592011-04-24 08:43:07 +00002961 bool compensateMangling = !Parameter->getType()->isReferenceType();
Rafael Espindolad9800722010-03-11 14:07:00 +00002962 if (compensateMangling) {
2963 Out << 'X';
2964 mangleOperatorName(OO_Amp, 1);
2965 }
2966
Daniel Dunbar7e0c1952009-11-21 09:17:15 +00002967 Out << 'L';
2968 // References to external entities use the mangled name; if the name would
2969 // not normally be manged then mangle it as unqualified.
2970 //
2971 // FIXME: The ABI specifies that external names here should have _Z, but
2972 // gcc leaves this off.
Rafael Espindolad9800722010-03-11 14:07:00 +00002973 if (compensateMangling)
2974 mangle(D, "_Z");
2975 else
2976 mangle(D, "Z");
Daniel Dunbar7e0c1952009-11-21 09:17:15 +00002977 Out << 'E';
Rafael Espindolad9800722010-03-11 14:07:00 +00002978
2979 if (compensateMangling)
2980 Out << 'E';
2981
Daniel Dunbar7e0c1952009-11-21 09:17:15 +00002982 break;
2983 }
Douglas Gregorf90b27a2011-01-03 22:36:02 +00002984
2985 case TemplateArgument::Pack: {
2986 // Note: proposal by Mike Herrick on 12/20/10
2987 Out << 'J';
2988 for (TemplateArgument::pack_iterator PA = A.pack_begin(),
2989 PAEnd = A.pack_end();
2990 PA != PAEnd; ++PA)
2991 mangleTemplateArg(P, *PA);
2992 Out << 'E';
2993 }
Daniel Dunbar7e0c1952009-11-21 09:17:15 +00002994 }
Anders Carlsson7a0ba872009-05-15 16:09:15 +00002995}
2996
Anders Carlsson0ccdf8d2009-09-27 00:38:53 +00002997void CXXNameMangler::mangleTemplateParameter(unsigned Index) {
2998 // <template-param> ::= T_ # first template parameter
2999 // ::= T <parameter-2 non-negative number> _
3000 if (Index == 0)
3001 Out << "T_";
3002 else
3003 Out << 'T' << (Index - 1) << '_';
3004}
3005
John McCall68a51a72011-07-01 00:04:39 +00003006void CXXNameMangler::mangleExistingSubstitution(QualType type) {
3007 bool result = mangleSubstitution(type);
3008 assert(result && "no existing substitution for type");
3009 (void) result;
3010}
3011
3012void CXXNameMangler::mangleExistingSubstitution(TemplateName tname) {
3013 bool result = mangleSubstitution(tname);
3014 assert(result && "no existing substitution for template name");
3015 (void) result;
3016}
3017
Anders Carlsson76967372009-09-17 00:43:46 +00003018// <substitution> ::= S <seq-id> _
3019// ::= S_
Anders Carlsson6862fc72009-09-17 04:16:28 +00003020bool CXXNameMangler::mangleSubstitution(const NamedDecl *ND) {
Anders Carlssone7c8cb62009-09-26 20:53:44 +00003021 // Try one of the standard substitutions first.
3022 if (mangleStandardSubstitution(ND))
3023 return true;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00003024
Anders Carlsson433d1372009-11-07 04:26:04 +00003025 ND = cast<NamedDecl>(ND->getCanonicalDecl());
Anders Carlsson6862fc72009-09-17 04:16:28 +00003026 return mangleSubstitution(reinterpret_cast<uintptr_t>(ND));
3027}
3028
Douglas Gregor14795c82011-12-03 18:24:43 +00003029/// \brief Determine whether the given type has any qualifiers that are
3030/// relevant for substitutions.
3031static bool hasMangledSubstitutionQualifiers(QualType T) {
3032 Qualifiers Qs = T.getQualifiers();
3033 return Qs.getCVRQualifiers() || Qs.hasAddressSpace();
3034}
3035
Anders Carlsson76967372009-09-17 00:43:46 +00003036bool CXXNameMangler::mangleSubstitution(QualType T) {
Douglas Gregor14795c82011-12-03 18:24:43 +00003037 if (!hasMangledSubstitutionQualifiers(T)) {
Anders Carlssond99edc42009-09-26 03:55:37 +00003038 if (const RecordType *RT = T->getAs<RecordType>())
3039 return mangleSubstitution(RT->getDecl());
3040 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00003041
Anders Carlsson76967372009-09-17 00:43:46 +00003042 uintptr_t TypePtr = reinterpret_cast<uintptr_t>(T.getAsOpaquePtr());
3043
Anders Carlssond3a932a2009-09-17 03:53:28 +00003044 return mangleSubstitution(TypePtr);
3045}
3046
Douglas Gregor1e9268e2010-04-28 05:58:56 +00003047bool CXXNameMangler::mangleSubstitution(TemplateName Template) {
3048 if (TemplateDecl *TD = Template.getAsTemplateDecl())
3049 return mangleSubstitution(TD);
Sean Huntc3021132010-05-05 15:23:54 +00003050
Douglas Gregor1e9268e2010-04-28 05:58:56 +00003051 Template = Context.getASTContext().getCanonicalTemplateName(Template);
3052 return mangleSubstitution(
3053 reinterpret_cast<uintptr_t>(Template.getAsVoidPointer()));
3054}
3055
Anders Carlssond3a932a2009-09-17 03:53:28 +00003056bool CXXNameMangler::mangleSubstitution(uintptr_t Ptr) {
Benjamin Kramer35f59b62010-04-10 16:03:31 +00003057 llvm::DenseMap<uintptr_t, unsigned>::iterator I = Substitutions.find(Ptr);
Anders Carlsson76967372009-09-17 00:43:46 +00003058 if (I == Substitutions.end())
3059 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00003060
Anders Carlsson76967372009-09-17 00:43:46 +00003061 unsigned SeqID = I->second;
3062 if (SeqID == 0)
3063 Out << "S_";
3064 else {
3065 SeqID--;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00003066
Anders Carlsson76967372009-09-17 00:43:46 +00003067 // <seq-id> is encoded in base-36, using digits and upper case letters.
3068 char Buffer[10];
Benjamin Kramer35f59b62010-04-10 16:03:31 +00003069 char *BufferPtr = llvm::array_endof(Buffer);
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00003070
Anders Carlsson76967372009-09-17 00:43:46 +00003071 if (SeqID == 0) *--BufferPtr = '0';
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00003072
Anders Carlsson76967372009-09-17 00:43:46 +00003073 while (SeqID) {
3074 assert(BufferPtr > Buffer && "Buffer overflow!");
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00003075
John McCall6ab30e02010-06-09 07:26:17 +00003076 char c = static_cast<char>(SeqID % 36);
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00003077
Anders Carlsson76967372009-09-17 00:43:46 +00003078 *--BufferPtr = (c < 10 ? '0' + c : 'A' + c - 10);
3079 SeqID /= 36;
3080 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00003081
Benjamin Kramer35f59b62010-04-10 16:03:31 +00003082 Out << 'S'
Chris Lattner5f9e2722011-07-23 10:55:15 +00003083 << StringRef(BufferPtr, llvm::array_endof(Buffer)-BufferPtr)
Benjamin Kramer35f59b62010-04-10 16:03:31 +00003084 << '_';
Anders Carlsson76967372009-09-17 00:43:46 +00003085 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00003086
Anders Carlsson76967372009-09-17 00:43:46 +00003087 return true;
3088}
3089
Anders Carlssonf514b542009-09-27 00:12:57 +00003090static bool isCharType(QualType T) {
3091 if (T.isNull())
3092 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00003093
Anders Carlssonf514b542009-09-27 00:12:57 +00003094 return T->isSpecificBuiltinType(BuiltinType::Char_S) ||
3095 T->isSpecificBuiltinType(BuiltinType::Char_U);
3096}
3097
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00003098/// isCharSpecialization - Returns whether a given type is a template
Anders Carlssonf514b542009-09-27 00:12:57 +00003099/// specialization of a given name with a single argument of type char.
3100static bool isCharSpecialization(QualType T, const char *Name) {
3101 if (T.isNull())
3102 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00003103
Anders Carlssonf514b542009-09-27 00:12:57 +00003104 const RecordType *RT = T->getAs<RecordType>();
3105 if (!RT)
3106 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00003107
3108 const ClassTemplateSpecializationDecl *SD =
Anders Carlssonf514b542009-09-27 00:12:57 +00003109 dyn_cast<ClassTemplateSpecializationDecl>(RT->getDecl());
3110 if (!SD)
3111 return false;
3112
3113 if (!isStdNamespace(SD->getDeclContext()))
3114 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00003115
Anders Carlssonf514b542009-09-27 00:12:57 +00003116 const TemplateArgumentList &TemplateArgs = SD->getTemplateArgs();
3117 if (TemplateArgs.size() != 1)
3118 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00003119
Anders Carlssonf514b542009-09-27 00:12:57 +00003120 if (!isCharType(TemplateArgs[0].getAsType()))
3121 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00003122
Daniel Dunbar01eb9b92009-10-18 21:17:35 +00003123 return SD->getIdentifier()->getName() == Name;
Anders Carlssonf514b542009-09-27 00:12:57 +00003124}
3125
Anders Carlsson91f88602009-12-07 19:56:42 +00003126template <std::size_t StrLen>
Benjamin Kramer54353f42010-11-25 18:29:30 +00003127static bool isStreamCharSpecialization(const ClassTemplateSpecializationDecl*SD,
3128 const char (&Str)[StrLen]) {
Anders Carlsson91f88602009-12-07 19:56:42 +00003129 if (!SD->getIdentifier()->isStr(Str))
3130 return false;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00003131
Anders Carlsson91f88602009-12-07 19:56:42 +00003132 const TemplateArgumentList &TemplateArgs = SD->getTemplateArgs();
3133 if (TemplateArgs.size() != 2)
3134 return false;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00003135
Anders Carlsson91f88602009-12-07 19:56:42 +00003136 if (!isCharType(TemplateArgs[0].getAsType()))
3137 return false;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00003138
Anders Carlsson91f88602009-12-07 19:56:42 +00003139 if (!isCharSpecialization(TemplateArgs[1].getAsType(), "char_traits"))
3140 return false;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00003141
Anders Carlsson91f88602009-12-07 19:56:42 +00003142 return true;
3143}
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00003144
Anders Carlssone7c8cb62009-09-26 20:53:44 +00003145bool CXXNameMangler::mangleStandardSubstitution(const NamedDecl *ND) {
3146 // <substitution> ::= St # ::std::
Anders Carlsson8c031552009-09-26 23:10:05 +00003147 if (const NamespaceDecl *NS = dyn_cast<NamespaceDecl>(ND)) {
Anders Carlsson47846d22009-12-04 06:23:23 +00003148 if (isStd(NS)) {
Anders Carlsson8c031552009-09-26 23:10:05 +00003149 Out << "St";
3150 return true;
3151 }
3152 }
3153
3154 if (const ClassTemplateDecl *TD = dyn_cast<ClassTemplateDecl>(ND)) {
3155 if (!isStdNamespace(TD->getDeclContext()))
3156 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00003157
Anders Carlsson8c031552009-09-26 23:10:05 +00003158 // <substitution> ::= Sa # ::std::allocator
3159 if (TD->getIdentifier()->isStr("allocator")) {
3160 Out << "Sa";
3161 return true;
3162 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00003163
Anders Carlsson189d59c2009-09-26 23:14:39 +00003164 // <<substitution> ::= Sb # ::std::basic_string
3165 if (TD->getIdentifier()->isStr("basic_string")) {
3166 Out << "Sb";
3167 return true;
3168 }
Anders Carlsson8c031552009-09-26 23:10:05 +00003169 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00003170
3171 if (const ClassTemplateSpecializationDecl *SD =
Anders Carlssonf514b542009-09-27 00:12:57 +00003172 dyn_cast<ClassTemplateSpecializationDecl>(ND)) {
Eli Friedman5370ee22010-02-23 18:25:09 +00003173 if (!isStdNamespace(SD->getDeclContext()))
3174 return false;
3175
Anders Carlssonf514b542009-09-27 00:12:57 +00003176 // <substitution> ::= Ss # ::std::basic_string<char,
3177 // ::std::char_traits<char>,
3178 // ::std::allocator<char> >
3179 if (SD->getIdentifier()->isStr("basic_string")) {
3180 const TemplateArgumentList &TemplateArgs = SD->getTemplateArgs();
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00003181
Anders Carlssonf514b542009-09-27 00:12:57 +00003182 if (TemplateArgs.size() != 3)
3183 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00003184
Anders Carlssonf514b542009-09-27 00:12:57 +00003185 if (!isCharType(TemplateArgs[0].getAsType()))
3186 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00003187
Anders Carlssonf514b542009-09-27 00:12:57 +00003188 if (!isCharSpecialization(TemplateArgs[1].getAsType(), "char_traits"))
3189 return false;
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00003190
Anders Carlssonf514b542009-09-27 00:12:57 +00003191 if (!isCharSpecialization(TemplateArgs[2].getAsType(), "allocator"))
3192 return false;
3193
3194 Out << "Ss";
3195 return true;
3196 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00003197
Anders Carlsson91f88602009-12-07 19:56:42 +00003198 // <substitution> ::= Si # ::std::basic_istream<char,
3199 // ::std::char_traits<char> >
3200 if (isStreamCharSpecialization(SD, "basic_istream")) {
3201 Out << "Si";
3202 return true;
3203 }
3204
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00003205 // <substitution> ::= So # ::std::basic_ostream<char,
Anders Carlsson8f8fd8e2009-10-08 17:20:26 +00003206 // ::std::char_traits<char> >
Anders Carlsson91f88602009-12-07 19:56:42 +00003207 if (isStreamCharSpecialization(SD, "basic_ostream")) {
Anders Carlsson8f8fd8e2009-10-08 17:20:26 +00003208 Out << "So";
3209 return true;
3210 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00003211
Anders Carlsson91f88602009-12-07 19:56:42 +00003212 // <substitution> ::= Sd # ::std::basic_iostream<char,
3213 // ::std::char_traits<char> >
3214 if (isStreamCharSpecialization(SD, "basic_iostream")) {
3215 Out << "Sd";
3216 return true;
3217 }
Anders Carlssonf514b542009-09-27 00:12:57 +00003218 }
Anders Carlsson8c031552009-09-26 23:10:05 +00003219 return false;
Anders Carlssone7c8cb62009-09-26 20:53:44 +00003220}
3221
Anders Carlsson76967372009-09-17 00:43:46 +00003222void CXXNameMangler::addSubstitution(QualType T) {
Douglas Gregor14795c82011-12-03 18:24:43 +00003223 if (!hasMangledSubstitutionQualifiers(T)) {
Anders Carlssond99edc42009-09-26 03:55:37 +00003224 if (const RecordType *RT = T->getAs<RecordType>()) {
3225 addSubstitution(RT->getDecl());
3226 return;
3227 }
3228 }
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00003229
Anders Carlsson76967372009-09-17 00:43:46 +00003230 uintptr_t TypePtr = reinterpret_cast<uintptr_t>(T.getAsOpaquePtr());
Anders Carlssond3a932a2009-09-17 03:53:28 +00003231 addSubstitution(TypePtr);
3232}
3233
Douglas Gregor1e9268e2010-04-28 05:58:56 +00003234void CXXNameMangler::addSubstitution(TemplateName Template) {
3235 if (TemplateDecl *TD = Template.getAsTemplateDecl())
3236 return addSubstitution(TD);
Sean Huntc3021132010-05-05 15:23:54 +00003237
Douglas Gregor1e9268e2010-04-28 05:58:56 +00003238 Template = Context.getASTContext().getCanonicalTemplateName(Template);
3239 addSubstitution(reinterpret_cast<uintptr_t>(Template.getAsVoidPointer()));
3240}
3241
Anders Carlssond3a932a2009-09-17 03:53:28 +00003242void CXXNameMangler::addSubstitution(uintptr_t Ptr) {
Anders Carlssond3a932a2009-09-17 03:53:28 +00003243 assert(!Substitutions.count(Ptr) && "Substitution already exists!");
Anders Carlsson9d85b722010-06-02 04:29:50 +00003244 Substitutions[Ptr] = SeqID++;
Anders Carlsson76967372009-09-17 00:43:46 +00003245}
3246
Daniel Dunbar1b077112009-11-21 09:06:10 +00003247//
Mike Stump1eb44332009-09-09 15:08:12 +00003248
Daniel Dunbar1b077112009-11-21 09:06:10 +00003249/// \brief Mangles the name of the declaration D and emits that name to the
3250/// given output stream.
3251///
3252/// If the declaration D requires a mangled name, this routine will emit that
3253/// mangled name to \p os and return true. Otherwise, \p os will be unchanged
3254/// and this routine will return false. In this case, the caller should just
3255/// emit the identifier of the declaration (\c D->getIdentifier()) as its
3256/// name.
Peter Collingbourne14110472011-01-13 18:57:25 +00003257void ItaniumMangleContext::mangleName(const NamedDecl *D,
Chris Lattner5f9e2722011-07-23 10:55:15 +00003258 raw_ostream &Out) {
Daniel Dunbarc02ab4c2009-11-21 09:14:44 +00003259 assert((isa<FunctionDecl>(D) || isa<VarDecl>(D)) &&
3260 "Invalid mangleName() call, argument is not a variable or function!");
3261 assert(!isa<CXXConstructorDecl>(D) && !isa<CXXDestructorDecl>(D) &&
3262 "Invalid mangleName() call on 'structor decl!");
Daniel Dunbar3c9e4632009-11-21 09:05:47 +00003263
Daniel Dunbar1b077112009-11-21 09:06:10 +00003264 PrettyStackTraceDecl CrashInfo(D, SourceLocation(),
3265 getASTContext().getSourceManager(),
3266 "Mangling declaration");
Mike Stump1eb44332009-09-09 15:08:12 +00003267
John McCallfb44de92011-05-01 22:35:37 +00003268 CXXNameMangler Mangler(*this, Out, D);
Daniel Dunbar94fd26d2009-11-21 09:06:22 +00003269 return Mangler.mangle(D);
Daniel Dunbar1b077112009-11-21 09:06:10 +00003270}
Mike Stump1eb44332009-09-09 15:08:12 +00003271
Peter Collingbourne14110472011-01-13 18:57:25 +00003272void ItaniumMangleContext::mangleCXXCtor(const CXXConstructorDecl *D,
3273 CXXCtorType Type,
Chris Lattner5f9e2722011-07-23 10:55:15 +00003274 raw_ostream &Out) {
Rafael Espindolac4850c22011-02-10 23:59:36 +00003275 CXXNameMangler Mangler(*this, Out, D, Type);
Daniel Dunbar77939c92009-11-21 09:06:31 +00003276 Mangler.mangle(D);
Daniel Dunbar1b077112009-11-21 09:06:10 +00003277}
Mike Stump1eb44332009-09-09 15:08:12 +00003278
Peter Collingbourne14110472011-01-13 18:57:25 +00003279void ItaniumMangleContext::mangleCXXDtor(const CXXDestructorDecl *D,
3280 CXXDtorType Type,
Chris Lattner5f9e2722011-07-23 10:55:15 +00003281 raw_ostream &Out) {
Rafael Espindolac4850c22011-02-10 23:59:36 +00003282 CXXNameMangler Mangler(*this, Out, D, Type);
Daniel Dunbar77939c92009-11-21 09:06:31 +00003283 Mangler.mangle(D);
Daniel Dunbar1b077112009-11-21 09:06:10 +00003284}
Mike Stumpf1216772009-07-31 18:25:34 +00003285
Peter Collingbourne14110472011-01-13 18:57:25 +00003286void ItaniumMangleContext::mangleThunk(const CXXMethodDecl *MD,
3287 const ThunkInfo &Thunk,
Chris Lattner5f9e2722011-07-23 10:55:15 +00003288 raw_ostream &Out) {
Anders Carlsson19879c92010-03-23 17:17:29 +00003289 // <special-name> ::= T <call-offset> <base encoding>
3290 // # base is the nominal target function of thunk
3291 // <special-name> ::= Tc <call-offset> <call-offset> <base encoding>
3292 // # base is the nominal target function of thunk
3293 // # first call-offset is 'this' adjustment
3294 // # second call-offset is result adjustment
Sean Huntc3021132010-05-05 15:23:54 +00003295
Anders Carlsson19879c92010-03-23 17:17:29 +00003296 assert(!isa<CXXDestructorDecl>(MD) &&
3297 "Use mangleCXXDtor for destructor decls!");
Rafael Espindolac4850c22011-02-10 23:59:36 +00003298 CXXNameMangler Mangler(*this, Out);
Anders Carlsson19879c92010-03-23 17:17:29 +00003299 Mangler.getStream() << "_ZT";
3300 if (!Thunk.Return.isEmpty())
3301 Mangler.getStream() << 'c';
Sean Huntc3021132010-05-05 15:23:54 +00003302
Anders Carlsson19879c92010-03-23 17:17:29 +00003303 // Mangle the 'this' pointer adjustment.
3304 Mangler.mangleCallOffset(Thunk.This.NonVirtual, Thunk.This.VCallOffsetOffset);
Sean Huntc3021132010-05-05 15:23:54 +00003305
Anders Carlsson19879c92010-03-23 17:17:29 +00003306 // Mangle the return pointer adjustment if there is one.
3307 if (!Thunk.Return.isEmpty())
3308 Mangler.mangleCallOffset(Thunk.Return.NonVirtual,
3309 Thunk.Return.VBaseOffsetOffset);
Sean Huntc3021132010-05-05 15:23:54 +00003310
Anders Carlsson19879c92010-03-23 17:17:29 +00003311 Mangler.mangleFunctionEncoding(MD);
3312}
3313
Sean Huntc3021132010-05-05 15:23:54 +00003314void
Peter Collingbourne14110472011-01-13 18:57:25 +00003315ItaniumMangleContext::mangleCXXDtorThunk(const CXXDestructorDecl *DD,
3316 CXXDtorType Type,
3317 const ThisAdjustment &ThisAdjustment,
Chris Lattner5f9e2722011-07-23 10:55:15 +00003318 raw_ostream &Out) {
Anders Carlsson19879c92010-03-23 17:17:29 +00003319 // <special-name> ::= T <call-offset> <base encoding>
3320 // # base is the nominal target function of thunk
Rafael Espindolac4850c22011-02-10 23:59:36 +00003321 CXXNameMangler Mangler(*this, Out, DD, Type);
Anders Carlsson19879c92010-03-23 17:17:29 +00003322 Mangler.getStream() << "_ZT";
3323
3324 // Mangle the 'this' pointer adjustment.
Sean Huntc3021132010-05-05 15:23:54 +00003325 Mangler.mangleCallOffset(ThisAdjustment.NonVirtual,
Anders Carlsson19879c92010-03-23 17:17:29 +00003326 ThisAdjustment.VCallOffsetOffset);
3327
3328 Mangler.mangleFunctionEncoding(DD);
3329}
3330
Daniel Dunbarc0747712009-11-21 09:12:13 +00003331/// mangleGuardVariable - Returns the mangled name for a guard variable
3332/// for the passed in VarDecl.
Peter Collingbourne14110472011-01-13 18:57:25 +00003333void ItaniumMangleContext::mangleItaniumGuardVariable(const VarDecl *D,
Chris Lattner5f9e2722011-07-23 10:55:15 +00003334 raw_ostream &Out) {
Daniel Dunbarc0747712009-11-21 09:12:13 +00003335 // <special-name> ::= GV <object name> # Guard variable for one-time
3336 // # initialization
Rafael Espindolac4850c22011-02-10 23:59:36 +00003337 CXXNameMangler Mangler(*this, Out);
Daniel Dunbarc0747712009-11-21 09:12:13 +00003338 Mangler.getStream() << "_ZGV";
3339 Mangler.mangleName(D);
3340}
3341
Peter Collingbourne14110472011-01-13 18:57:25 +00003342void ItaniumMangleContext::mangleReferenceTemporary(const VarDecl *D,
Chris Lattner5f9e2722011-07-23 10:55:15 +00003343 raw_ostream &Out) {
Anders Carlsson715edf22010-06-26 16:09:40 +00003344 // We match the GCC mangling here.
3345 // <special-name> ::= GR <object name>
Rafael Espindolac4850c22011-02-10 23:59:36 +00003346 CXXNameMangler Mangler(*this, Out);
Anders Carlsson715edf22010-06-26 16:09:40 +00003347 Mangler.getStream() << "_ZGR";
3348 Mangler.mangleName(D);
3349}
3350
Peter Collingbourne14110472011-01-13 18:57:25 +00003351void ItaniumMangleContext::mangleCXXVTable(const CXXRecordDecl *RD,
Chris Lattner5f9e2722011-07-23 10:55:15 +00003352 raw_ostream &Out) {
Daniel Dunbarc0747712009-11-21 09:12:13 +00003353 // <special-name> ::= TV <type> # virtual table
Rafael Espindolac4850c22011-02-10 23:59:36 +00003354 CXXNameMangler Mangler(*this, Out);
Daniel Dunbarc0747712009-11-21 09:12:13 +00003355 Mangler.getStream() << "_ZTV";
Douglas Gregor1b12a3b2010-05-26 05:11:13 +00003356 Mangler.mangleNameOrStandardSubstitution(RD);
Daniel Dunbar1b077112009-11-21 09:06:10 +00003357}
Mike Stump82d75b02009-11-10 01:58:37 +00003358
Peter Collingbourne14110472011-01-13 18:57:25 +00003359void ItaniumMangleContext::mangleCXXVTT(const CXXRecordDecl *RD,
Chris Lattner5f9e2722011-07-23 10:55:15 +00003360 raw_ostream &Out) {
Daniel Dunbarc0747712009-11-21 09:12:13 +00003361 // <special-name> ::= TT <type> # VTT structure
Rafael Espindolac4850c22011-02-10 23:59:36 +00003362 CXXNameMangler Mangler(*this, Out);
Daniel Dunbarc0747712009-11-21 09:12:13 +00003363 Mangler.getStream() << "_ZTT";
Douglas Gregor1b12a3b2010-05-26 05:11:13 +00003364 Mangler.mangleNameOrStandardSubstitution(RD);
Daniel Dunbar1b077112009-11-21 09:06:10 +00003365}
Mike Stumpab3f7e92009-11-10 01:41:59 +00003366
Peter Collingbourne14110472011-01-13 18:57:25 +00003367void ItaniumMangleContext::mangleCXXCtorVTable(const CXXRecordDecl *RD,
3368 int64_t Offset,
3369 const CXXRecordDecl *Type,
Chris Lattner5f9e2722011-07-23 10:55:15 +00003370 raw_ostream &Out) {
Daniel Dunbarc0747712009-11-21 09:12:13 +00003371 // <special-name> ::= TC <type> <offset number> _ <base type>
Rafael Espindolac4850c22011-02-10 23:59:36 +00003372 CXXNameMangler Mangler(*this, Out);
Daniel Dunbarc0747712009-11-21 09:12:13 +00003373 Mangler.getStream() << "_ZTC";
Douglas Gregor1b12a3b2010-05-26 05:11:13 +00003374 Mangler.mangleNameOrStandardSubstitution(RD);
Daniel Dunbarc0747712009-11-21 09:12:13 +00003375 Mangler.getStream() << Offset;
Benjamin Kramer35f59b62010-04-10 16:03:31 +00003376 Mangler.getStream() << '_';
Douglas Gregor1b12a3b2010-05-26 05:11:13 +00003377 Mangler.mangleNameOrStandardSubstitution(Type);
Daniel Dunbar1b077112009-11-21 09:06:10 +00003378}
Mike Stump738f8c22009-07-31 23:15:31 +00003379
Peter Collingbourne14110472011-01-13 18:57:25 +00003380void ItaniumMangleContext::mangleCXXRTTI(QualType Ty,
Chris Lattner5f9e2722011-07-23 10:55:15 +00003381 raw_ostream &Out) {
Daniel Dunbarc0747712009-11-21 09:12:13 +00003382 // <special-name> ::= TI <type> # typeinfo structure
Douglas Gregor154fe982009-12-23 22:04:40 +00003383 assert(!Ty.hasQualifiers() && "RTTI info cannot have top-level qualifiers");
Rafael Espindolac4850c22011-02-10 23:59:36 +00003384 CXXNameMangler Mangler(*this, Out);
Daniel Dunbarc0747712009-11-21 09:12:13 +00003385 Mangler.getStream() << "_ZTI";
3386 Mangler.mangleType(Ty);
Daniel Dunbar1b077112009-11-21 09:06:10 +00003387}
Mike Stump67795982009-11-14 00:14:13 +00003388
Peter Collingbourne14110472011-01-13 18:57:25 +00003389void ItaniumMangleContext::mangleCXXRTTIName(QualType Ty,
Chris Lattner5f9e2722011-07-23 10:55:15 +00003390 raw_ostream &Out) {
Daniel Dunbarc0747712009-11-21 09:12:13 +00003391 // <special-name> ::= TS <type> # typeinfo name (null terminated byte string)
Rafael Espindolac4850c22011-02-10 23:59:36 +00003392 CXXNameMangler Mangler(*this, Out);
Daniel Dunbarc0747712009-11-21 09:12:13 +00003393 Mangler.getStream() << "_ZTS";
3394 Mangler.mangleType(Ty);
Mike Stumpf1216772009-07-31 18:25:34 +00003395}
Peter Collingbourne14110472011-01-13 18:57:25 +00003396
3397MangleContext *clang::createItaniumMangleContext(ASTContext &Context,
David Blaikied6471f72011-09-25 23:23:43 +00003398 DiagnosticsEngine &Diags) {
Peter Collingbourne14110472011-01-13 18:57:25 +00003399 return new ItaniumMangleContext(Context, Diags);
3400}