blob: 0e332a6f2de252477f193a1f56e13ed5bf3983ae [file] [log] [blame]
Guy Benyei11169dd2012-12-18 14:30:41 +00001//===--- ItaniumMangle.cpp - Itanium C++ Name Mangling ----------*- C++ -*-===//
2//
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//
David Majnemer98559942013-12-13 00:54:42 +000014// http://mentorembedded.github.io/cxx-abi/abi.html#mangling
Guy Benyei11169dd2012-12-18 14:30:41 +000015//
16//===----------------------------------------------------------------------===//
17#include "clang/AST/Mangle.h"
18#include "clang/AST/ASTContext.h"
19#include "clang/AST/Attr.h"
20#include "clang/AST/Decl.h"
21#include "clang/AST/DeclCXX.h"
22#include "clang/AST/DeclObjC.h"
23#include "clang/AST/DeclTemplate.h"
David Majnemer58e5bee2014-03-24 21:43:36 +000024#include "clang/AST/Expr.h"
Guy Benyei11169dd2012-12-18 14:30:41 +000025#include "clang/AST/ExprCXX.h"
26#include "clang/AST/ExprObjC.h"
27#include "clang/AST/TypeLoc.h"
28#include "clang/Basic/ABI.h"
29#include "clang/Basic/SourceManager.h"
30#include "clang/Basic/TargetInfo.h"
31#include "llvm/ADT/StringExtras.h"
32#include "llvm/Support/ErrorHandling.h"
33#include "llvm/Support/raw_ostream.h"
34
35#define MANGLE_CHECKER 0
36
37#if MANGLE_CHECKER
38#include <cxxabi.h>
39#endif
40
41using namespace clang;
42
43namespace {
44
45/// \brief Retrieve the declaration context that should be used when mangling
46/// the given declaration.
47static const DeclContext *getEffectiveDeclContext(const Decl *D) {
48 // The ABI assumes that lambda closure types that occur within
49 // default arguments live in the context of the function. However, due to
50 // the way in which Clang parses and creates function declarations, this is
51 // not the case: the lambda closure type ends up living in the context
52 // where the function itself resides, because the function declaration itself
53 // had not yet been created. Fix the context here.
54 if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D)) {
55 if (RD->isLambda())
56 if (ParmVarDecl *ContextParam
57 = dyn_cast_or_null<ParmVarDecl>(RD->getLambdaContextDecl()))
58 return ContextParam->getDeclContext();
59 }
Eli Friedman0cd23352013-07-10 01:33:19 +000060
61 // Perform the same check for block literals.
62 if (const BlockDecl *BD = dyn_cast<BlockDecl>(D)) {
63 if (ParmVarDecl *ContextParam
64 = dyn_cast_or_null<ParmVarDecl>(BD->getBlockManglingContextDecl()))
65 return ContextParam->getDeclContext();
66 }
Guy Benyei11169dd2012-12-18 14:30:41 +000067
Eli Friedman95f50122013-07-02 17:52:28 +000068 const DeclContext *DC = D->getDeclContext();
69 if (const CapturedDecl *CD = dyn_cast<CapturedDecl>(DC))
70 return getEffectiveDeclContext(CD);
71
72 return DC;
Guy Benyei11169dd2012-12-18 14:30:41 +000073}
74
75static const DeclContext *getEffectiveParentContext(const DeclContext *DC) {
76 return getEffectiveDeclContext(cast<Decl>(DC));
77}
Eli Friedman95f50122013-07-02 17:52:28 +000078
79static bool isLocalContainerContext(const DeclContext *DC) {
80 return isa<FunctionDecl>(DC) || isa<ObjCMethodDecl>(DC) || isa<BlockDecl>(DC);
81}
82
Eli Friedmaneecc09a2013-07-05 20:27:40 +000083static const RecordDecl *GetLocalClassDecl(const Decl *D) {
Eli Friedman92821742013-07-02 02:01:18 +000084 const DeclContext *DC = getEffectiveDeclContext(D);
Guy Benyei11169dd2012-12-18 14:30:41 +000085 while (!DC->isNamespace() && !DC->isTranslationUnit()) {
Eli Friedman95f50122013-07-02 17:52:28 +000086 if (isLocalContainerContext(DC))
Eli Friedmaneecc09a2013-07-05 20:27:40 +000087 return dyn_cast<RecordDecl>(D);
Eli Friedman92821742013-07-02 02:01:18 +000088 D = cast<Decl>(DC);
89 DC = getEffectiveDeclContext(D);
Guy Benyei11169dd2012-12-18 14:30:41 +000090 }
91 return 0;
92}
93
94static const FunctionDecl *getStructor(const FunctionDecl *fn) {
95 if (const FunctionTemplateDecl *ftd = fn->getPrimaryTemplate())
96 return ftd->getTemplatedDecl();
97
98 return fn;
99}
100
101static const NamedDecl *getStructor(const NamedDecl *decl) {
102 const FunctionDecl *fn = dyn_cast_or_null<FunctionDecl>(decl);
103 return (fn ? getStructor(fn) : decl);
104}
David Majnemer2206bf52014-03-05 08:57:59 +0000105
106static bool isLambda(const NamedDecl *ND) {
107 const CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(ND);
108 if (!Record)
109 return false;
110
111 return Record->isLambda();
112}
113
Guy Benyei11169dd2012-12-18 14:30:41 +0000114static const unsigned UnknownArity = ~0U;
115
Timur Iskhodzhanov67455222013-10-03 06:26:13 +0000116class ItaniumMangleContextImpl : public ItaniumMangleContext {
Eli Friedman3b7d46c2013-07-10 00:30:46 +0000117 typedef std::pair<const DeclContext*, IdentifierInfo*> DiscriminatorKeyTy;
118 llvm::DenseMap<DiscriminatorKeyTy, unsigned> Discriminator;
Guy Benyei11169dd2012-12-18 14:30:41 +0000119 llvm::DenseMap<const NamedDecl*, unsigned> Uniquifier;
120
121public:
Timur Iskhodzhanov67455222013-10-03 06:26:13 +0000122 explicit ItaniumMangleContextImpl(ASTContext &Context,
123 DiagnosticsEngine &Diags)
124 : ItaniumMangleContext(Context, Diags) {}
Guy Benyei11169dd2012-12-18 14:30:41 +0000125
Guy Benyei11169dd2012-12-18 14:30:41 +0000126 /// @name Mangler Entry Points
127 /// @{
128
Craig Toppercbce6e92014-03-11 06:22:39 +0000129 bool shouldMangleCXXName(const NamedDecl *D) override;
David Majnemer58e5bee2014-03-24 21:43:36 +0000130 bool shouldMangleStringLiteral(const StringLiteral *) override {
131 return false;
132 }
Craig Toppercbce6e92014-03-11 06:22:39 +0000133 void mangleCXXName(const NamedDecl *D, raw_ostream &) override;
134 void mangleThunk(const CXXMethodDecl *MD, const ThunkInfo &Thunk,
135 raw_ostream &) override;
Guy Benyei11169dd2012-12-18 14:30:41 +0000136 void mangleCXXDtorThunk(const CXXDestructorDecl *DD, CXXDtorType Type,
137 const ThisAdjustment &ThisAdjustment,
Craig Toppercbce6e92014-03-11 06:22:39 +0000138 raw_ostream &) override;
139 void mangleReferenceTemporary(const VarDecl *D, raw_ostream &) override;
140 void mangleCXXVTable(const CXXRecordDecl *RD, raw_ostream &) override;
141 void mangleCXXVTT(const CXXRecordDecl *RD, raw_ostream &) override;
Guy Benyei11169dd2012-12-18 14:30:41 +0000142 void mangleCXXCtorVTable(const CXXRecordDecl *RD, int64_t Offset,
Craig Toppercbce6e92014-03-11 06:22:39 +0000143 const CXXRecordDecl *Type, raw_ostream &) override;
144 void mangleCXXRTTI(QualType T, raw_ostream &) override;
145 void mangleCXXRTTIName(QualType T, raw_ostream &) override;
146 void mangleTypeName(QualType T, raw_ostream &) override;
Guy Benyei11169dd2012-12-18 14:30:41 +0000147 void mangleCXXCtor(const CXXConstructorDecl *D, CXXCtorType Type,
Craig Toppercbce6e92014-03-11 06:22:39 +0000148 raw_ostream &) override;
Guy Benyei11169dd2012-12-18 14:30:41 +0000149 void mangleCXXDtor(const CXXDestructorDecl *D, CXXDtorType Type,
Craig Toppercbce6e92014-03-11 06:22:39 +0000150 raw_ostream &) override;
Guy Benyei11169dd2012-12-18 14:30:41 +0000151
Craig Toppercbce6e92014-03-11 06:22:39 +0000152 void mangleStaticGuardVariable(const VarDecl *D, raw_ostream &) override;
153 void mangleDynamicInitializer(const VarDecl *D, raw_ostream &Out) override;
154 void mangleDynamicAtExitDestructor(const VarDecl *D,
155 raw_ostream &Out) override;
156 void mangleItaniumThreadLocalInit(const VarDecl *D, raw_ostream &) override;
157 void mangleItaniumThreadLocalWrapper(const VarDecl *D,
158 raw_ostream &) override;
Guy Benyei11169dd2012-12-18 14:30:41 +0000159
David Majnemer58e5bee2014-03-24 21:43:36 +0000160 void mangleStringLiteral(const StringLiteral *, raw_ostream &) override;
161
Guy Benyei11169dd2012-12-18 14:30:41 +0000162 bool getNextDiscriminator(const NamedDecl *ND, unsigned &disc) {
Eli Friedman3b7d46c2013-07-10 00:30:46 +0000163 // Lambda closure types are already numbered.
David Majnemer2206bf52014-03-05 08:57:59 +0000164 if (isLambda(ND))
165 return false;
Eli Friedman3b7d46c2013-07-10 00:30:46 +0000166
167 // Anonymous tags are already numbered.
168 if (const TagDecl *Tag = dyn_cast<TagDecl>(ND)) {
169 if (Tag->getName().empty() && !Tag->getTypedefNameForAnonDecl())
170 return false;
171 }
172
173 // Use the canonical number for externally visible decls.
174 if (ND->isExternallyVisible()) {
175 unsigned discriminator = getASTContext().getManglingNumber(ND);
176 if (discriminator == 1)
177 return false;
178 disc = discriminator - 2;
179 return true;
180 }
181
182 // Make up a reasonable number for internal decls.
Guy Benyei11169dd2012-12-18 14:30:41 +0000183 unsigned &discriminator = Uniquifier[ND];
Eli Friedman3b7d46c2013-07-10 00:30:46 +0000184 if (!discriminator) {
185 const DeclContext *DC = getEffectiveDeclContext(ND);
186 discriminator = ++Discriminator[std::make_pair(DC, ND->getIdentifier())];
187 }
Guy Benyei11169dd2012-12-18 14:30:41 +0000188 if (discriminator == 1)
189 return false;
190 disc = discriminator-2;
191 return true;
192 }
193 /// @}
194};
195
196/// CXXNameMangler - Manage the mangling of a single name.
197class CXXNameMangler {
Timur Iskhodzhanov67455222013-10-03 06:26:13 +0000198 ItaniumMangleContextImpl &Context;
Guy Benyei11169dd2012-12-18 14:30:41 +0000199 raw_ostream &Out;
200
201 /// The "structor" is the top-level declaration being mangled, if
202 /// that's not a template specialization; otherwise it's the pattern
203 /// for that specialization.
204 const NamedDecl *Structor;
205 unsigned StructorType;
206
207 /// SeqID - The next subsitution sequence number.
208 unsigned SeqID;
209
210 class FunctionTypeDepthState {
211 unsigned Bits;
212
213 enum { InResultTypeMask = 1 };
214
215 public:
216 FunctionTypeDepthState() : Bits(0) {}
217
218 /// The number of function types we're inside.
219 unsigned getDepth() const {
220 return Bits >> 1;
221 }
222
223 /// True if we're in the return type of the innermost function type.
224 bool isInResultType() const {
225 return Bits & InResultTypeMask;
226 }
227
228 FunctionTypeDepthState push() {
229 FunctionTypeDepthState tmp = *this;
230 Bits = (Bits & ~InResultTypeMask) + 2;
231 return tmp;
232 }
233
234 void enterResultType() {
235 Bits |= InResultTypeMask;
236 }
237
238 void leaveResultType() {
239 Bits &= ~InResultTypeMask;
240 }
241
242 void pop(FunctionTypeDepthState saved) {
243 assert(getDepth() == saved.getDepth() + 1);
244 Bits = saved.Bits;
245 }
246
247 } FunctionTypeDepth;
248
249 llvm::DenseMap<uintptr_t, unsigned> Substitutions;
250
251 ASTContext &getASTContext() const { return Context.getASTContext(); }
252
253public:
Timur Iskhodzhanov67455222013-10-03 06:26:13 +0000254 CXXNameMangler(ItaniumMangleContextImpl &C, raw_ostream &Out_,
Guy Benyei11169dd2012-12-18 14:30:41 +0000255 const NamedDecl *D = 0)
256 : Context(C), Out(Out_), Structor(getStructor(D)), StructorType(0),
257 SeqID(0) {
258 // These can't be mangled without a ctor type or dtor type.
259 assert(!D || (!isa<CXXDestructorDecl>(D) &&
260 !isa<CXXConstructorDecl>(D)));
261 }
Timur Iskhodzhanov67455222013-10-03 06:26:13 +0000262 CXXNameMangler(ItaniumMangleContextImpl &C, raw_ostream &Out_,
Guy Benyei11169dd2012-12-18 14:30:41 +0000263 const CXXConstructorDecl *D, CXXCtorType Type)
264 : Context(C), Out(Out_), Structor(getStructor(D)), StructorType(Type),
265 SeqID(0) { }
Timur Iskhodzhanov67455222013-10-03 06:26:13 +0000266 CXXNameMangler(ItaniumMangleContextImpl &C, raw_ostream &Out_,
Guy Benyei11169dd2012-12-18 14:30:41 +0000267 const CXXDestructorDecl *D, CXXDtorType Type)
268 : Context(C), Out(Out_), Structor(getStructor(D)), StructorType(Type),
269 SeqID(0) { }
270
271#if MANGLE_CHECKER
272 ~CXXNameMangler() {
273 if (Out.str()[0] == '\01')
274 return;
275
276 int status = 0;
277 char *result = abi::__cxa_demangle(Out.str().str().c_str(), 0, 0, &status);
278 assert(status == 0 && "Could not demangle mangled name!");
279 free(result);
280 }
281#endif
282 raw_ostream &getStream() { return Out; }
283
284 void mangle(const NamedDecl *D, StringRef Prefix = "_Z");
285 void mangleCallOffset(int64_t NonVirtual, int64_t Virtual);
286 void mangleNumber(const llvm::APSInt &I);
287 void mangleNumber(int64_t Number);
288 void mangleFloat(const llvm::APFloat &F);
289 void mangleFunctionEncoding(const FunctionDecl *FD);
290 void mangleName(const NamedDecl *ND);
291 void mangleType(QualType T);
292 void mangleNameOrStandardSubstitution(const NamedDecl *ND);
293
294private:
295 bool mangleSubstitution(const NamedDecl *ND);
296 bool mangleSubstitution(QualType T);
297 bool mangleSubstitution(TemplateName Template);
298 bool mangleSubstitution(uintptr_t Ptr);
299
300 void mangleExistingSubstitution(QualType type);
301 void mangleExistingSubstitution(TemplateName name);
302
303 bool mangleStandardSubstitution(const NamedDecl *ND);
304
305 void addSubstitution(const NamedDecl *ND) {
306 ND = cast<NamedDecl>(ND->getCanonicalDecl());
307
308 addSubstitution(reinterpret_cast<uintptr_t>(ND));
309 }
310 void addSubstitution(QualType T);
311 void addSubstitution(TemplateName Template);
312 void addSubstitution(uintptr_t Ptr);
313
314 void mangleUnresolvedPrefix(NestedNameSpecifier *qualifier,
315 NamedDecl *firstQualifierLookup,
316 bool recursive = false);
317 void mangleUnresolvedName(NestedNameSpecifier *qualifier,
318 NamedDecl *firstQualifierLookup,
319 DeclarationName name,
320 unsigned KnownArity = UnknownArity);
321
322 void mangleName(const TemplateDecl *TD,
323 const TemplateArgument *TemplateArgs,
324 unsigned NumTemplateArgs);
325 void mangleUnqualifiedName(const NamedDecl *ND) {
326 mangleUnqualifiedName(ND, ND->getDeclName(), UnknownArity);
327 }
328 void mangleUnqualifiedName(const NamedDecl *ND, DeclarationName Name,
329 unsigned KnownArity);
330 void mangleUnscopedName(const NamedDecl *ND);
331 void mangleUnscopedTemplateName(const TemplateDecl *ND);
332 void mangleUnscopedTemplateName(TemplateName);
333 void mangleSourceName(const IdentifierInfo *II);
Eli Friedman95f50122013-07-02 17:52:28 +0000334 void mangleLocalName(const Decl *D);
335 void mangleBlockForPrefix(const BlockDecl *Block);
336 void mangleUnqualifiedBlock(const BlockDecl *Block);
Guy Benyei11169dd2012-12-18 14:30:41 +0000337 void mangleLambda(const CXXRecordDecl *Lambda);
338 void mangleNestedName(const NamedDecl *ND, const DeclContext *DC,
339 bool NoFunction=false);
340 void mangleNestedName(const TemplateDecl *TD,
341 const TemplateArgument *TemplateArgs,
342 unsigned NumTemplateArgs);
343 void manglePrefix(NestedNameSpecifier *qualifier);
344 void manglePrefix(const DeclContext *DC, bool NoFunction=false);
345 void manglePrefix(QualType type);
Eli Friedman86af13f02013-07-05 18:41:30 +0000346 void mangleTemplatePrefix(const TemplateDecl *ND, bool NoFunction=false);
Guy Benyei11169dd2012-12-18 14:30:41 +0000347 void mangleTemplatePrefix(TemplateName Template);
348 void mangleOperatorName(OverloadedOperatorKind OO, unsigned Arity);
349 void mangleQualifiers(Qualifiers Quals);
350 void mangleRefQualifier(RefQualifierKind RefQualifier);
351
352 void mangleObjCMethodName(const ObjCMethodDecl *MD);
353
354 // Declare manglers for every type class.
355#define ABSTRACT_TYPE(CLASS, PARENT)
356#define NON_CANONICAL_TYPE(CLASS, PARENT)
357#define TYPE(CLASS, PARENT) void mangleType(const CLASS##Type *T);
358#include "clang/AST/TypeNodes.def"
359
360 void mangleType(const TagType*);
361 void mangleType(TemplateName);
362 void mangleBareFunctionType(const FunctionType *T,
363 bool MangleReturnType);
364 void mangleNeonVectorType(const VectorType *T);
Tim Northover2fe823a2013-08-01 09:23:19 +0000365 void mangleAArch64NeonVectorType(const VectorType *T);
Guy Benyei11169dd2012-12-18 14:30:41 +0000366
367 void mangleIntegerLiteral(QualType T, const llvm::APSInt &Value);
368 void mangleMemberExpr(const Expr *base, bool isArrow,
369 NestedNameSpecifier *qualifier,
370 NamedDecl *firstQualifierLookup,
371 DeclarationName name,
372 unsigned knownArity);
373 void mangleExpression(const Expr *E, unsigned Arity = UnknownArity);
374 void mangleCXXCtorType(CXXCtorType T);
375 void mangleCXXDtorType(CXXDtorType T);
376
377 void mangleTemplateArgs(const ASTTemplateArgumentListInfo &TemplateArgs);
378 void mangleTemplateArgs(const TemplateArgument *TemplateArgs,
379 unsigned NumTemplateArgs);
380 void mangleTemplateArgs(const TemplateArgumentList &AL);
381 void mangleTemplateArg(TemplateArgument A);
382
383 void mangleTemplateParameter(unsigned Index);
384
385 void mangleFunctionParam(const ParmVarDecl *parm);
386};
387
388}
389
Rafael Espindola002667c2013-10-16 01:40:34 +0000390bool ItaniumMangleContextImpl::shouldMangleCXXName(const NamedDecl *D) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000391 const FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
Rafael Espindola5bda63f2013-02-14 01:47:04 +0000392 if (FD) {
393 LanguageLinkage L = FD->getLanguageLinkage();
394 // Overloadable functions need mangling.
395 if (FD->hasAttr<OverloadableAttr>())
396 return true;
397
Rafael Espindola3e0e33d2013-02-14 15:38:59 +0000398 // "main" is not mangled.
399 if (FD->isMain())
Rafael Espindola5bda63f2013-02-14 01:47:04 +0000400 return false;
401
402 // C++ functions and those whose names are not a simple identifier need
403 // mangling.
404 if (!FD->getDeclName().isIdentifier() || L == CXXLanguageLinkage)
405 return true;
Rafael Espindola46d2b6b2013-02-14 03:31:26 +0000406
Rafael Espindola3e0e33d2013-02-14 15:38:59 +0000407 // C functions are not mangled.
408 if (L == CLanguageLinkage)
409 return false;
Rafael Espindola5bda63f2013-02-14 01:47:04 +0000410 }
Guy Benyei11169dd2012-12-18 14:30:41 +0000411
412 // Otherwise, no mangling is done outside C++ mode.
413 if (!getASTContext().getLangOpts().CPlusPlus)
414 return false;
415
Rafael Espindola5bda63f2013-02-14 01:47:04 +0000416 const VarDecl *VD = dyn_cast<VarDecl>(D);
417 if (VD) {
418 // C variables are not mangled.
419 if (VD->isExternC())
420 return false;
421
422 // Variables at global scope with non-internal linkage are not mangled
Guy Benyei11169dd2012-12-18 14:30:41 +0000423 const DeclContext *DC = getEffectiveDeclContext(D);
424 // Check for extern variable declared locally.
425 if (DC->isFunctionOrMethod() && D->hasLinkage())
426 while (!DC->isNamespace() && !DC->isTranslationUnit())
427 DC = getEffectiveParentContext(DC);
Larisse Voufo39a1e502013-08-06 01:03:05 +0000428 if (DC->isTranslationUnit() && D->getFormalLinkage() != InternalLinkage &&
429 !isa<VarTemplateSpecializationDecl>(D))
Guy Benyei11169dd2012-12-18 14:30:41 +0000430 return false;
431 }
432
Guy Benyei11169dd2012-12-18 14:30:41 +0000433 return true;
434}
435
436void CXXNameMangler::mangle(const NamedDecl *D, StringRef Prefix) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000437 // <mangled-name> ::= _Z <encoding>
438 // ::= <data name>
439 // ::= <special-name>
440 Out << Prefix;
441 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
442 mangleFunctionEncoding(FD);
443 else if (const VarDecl *VD = dyn_cast<VarDecl>(D))
444 mangleName(VD);
David Majnemer0eb8bbd2013-10-23 20:52:43 +0000445 else if (const IndirectFieldDecl *IFD = dyn_cast<IndirectFieldDecl>(D))
446 mangleName(IFD->getAnonField());
Guy Benyei11169dd2012-12-18 14:30:41 +0000447 else
448 mangleName(cast<FieldDecl>(D));
449}
450
451void CXXNameMangler::mangleFunctionEncoding(const FunctionDecl *FD) {
452 // <encoding> ::= <function name> <bare-function-type>
453 mangleName(FD);
454
455 // Don't mangle in the type if this isn't a decl we should typically mangle.
456 if (!Context.shouldMangleDeclName(FD))
457 return;
458
Nick Lewycky0c2986f2014-04-26 00:14:00 +0000459 if (FD->hasAttr<EnableIfAttr>()) {
460 FunctionTypeDepthState Saved = FunctionTypeDepth.push();
461 Out << "Ua9enable_ifI";
462 // FIXME: specific_attr_iterator iterates in reverse order. Fix that and use
463 // it here.
464 for (AttrVec::const_reverse_iterator I = FD->getAttrs().rbegin(),
465 E = FD->getAttrs().rend();
466 I != E; ++I) {
467 EnableIfAttr *EIA = dyn_cast<EnableIfAttr>(*I);
468 if (!EIA)
469 continue;
470 Out << 'X';
471 mangleExpression(EIA->getCond());
472 Out << 'E';
473 }
474 Out << 'E';
475 FunctionTypeDepth.pop(Saved);
476 }
477
Guy Benyei11169dd2012-12-18 14:30:41 +0000478 // Whether the mangling of a function type includes the return type depends on
479 // the context and the nature of the function. The rules for deciding whether
480 // the return type is included are:
481 //
482 // 1. Template functions (names or types) have return types encoded, with
483 // the exceptions listed below.
484 // 2. Function types not appearing as part of a function name mangling,
485 // e.g. parameters, pointer types, etc., have return type encoded, with the
486 // exceptions listed below.
487 // 3. Non-template function names do not have return types encoded.
488 //
489 // The exceptions mentioned in (1) and (2) above, for which the return type is
490 // never included, are
491 // 1. Constructors.
492 // 2. Destructors.
493 // 3. Conversion operator functions, e.g. operator int.
494 bool MangleReturnType = false;
495 if (FunctionTemplateDecl *PrimaryTemplate = FD->getPrimaryTemplate()) {
496 if (!(isa<CXXConstructorDecl>(FD) || isa<CXXDestructorDecl>(FD) ||
497 isa<CXXConversionDecl>(FD)))
498 MangleReturnType = true;
499
500 // Mangle the type of the primary template.
501 FD = PrimaryTemplate->getTemplatedDecl();
502 }
503
504 mangleBareFunctionType(FD->getType()->getAs<FunctionType>(),
505 MangleReturnType);
506}
507
508static const DeclContext *IgnoreLinkageSpecDecls(const DeclContext *DC) {
509 while (isa<LinkageSpecDecl>(DC)) {
510 DC = getEffectiveParentContext(DC);
511 }
512
513 return DC;
514}
515
516/// isStd - Return whether a given namespace is the 'std' namespace.
517static bool isStd(const NamespaceDecl *NS) {
518 if (!IgnoreLinkageSpecDecls(getEffectiveParentContext(NS))
519 ->isTranslationUnit())
520 return false;
521
522 const IdentifierInfo *II = NS->getOriginalNamespace()->getIdentifier();
523 return II && II->isStr("std");
524}
525
526// isStdNamespace - Return whether a given decl context is a toplevel 'std'
527// namespace.
528static bool isStdNamespace(const DeclContext *DC) {
529 if (!DC->isNamespace())
530 return false;
531
532 return isStd(cast<NamespaceDecl>(DC));
533}
534
535static const TemplateDecl *
536isTemplate(const NamedDecl *ND, const TemplateArgumentList *&TemplateArgs) {
537 // Check if we have a function template.
538 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)){
539 if (const TemplateDecl *TD = FD->getPrimaryTemplate()) {
540 TemplateArgs = FD->getTemplateSpecializationArgs();
541 return TD;
542 }
543 }
544
545 // Check if we have a class template.
546 if (const ClassTemplateSpecializationDecl *Spec =
547 dyn_cast<ClassTemplateSpecializationDecl>(ND)) {
548 TemplateArgs = &Spec->getTemplateArgs();
549 return Spec->getSpecializedTemplate();
550 }
551
Larisse Voufo39a1e502013-08-06 01:03:05 +0000552 // Check if we have a variable template.
553 if (const VarTemplateSpecializationDecl *Spec =
554 dyn_cast<VarTemplateSpecializationDecl>(ND)) {
555 TemplateArgs = &Spec->getTemplateArgs();
556 return Spec->getSpecializedTemplate();
557 }
558
Guy Benyei11169dd2012-12-18 14:30:41 +0000559 return 0;
560}
561
Guy Benyei11169dd2012-12-18 14:30:41 +0000562void CXXNameMangler::mangleName(const NamedDecl *ND) {
563 // <name> ::= <nested-name>
564 // ::= <unscoped-name>
565 // ::= <unscoped-template-name> <template-args>
566 // ::= <local-name>
567 //
568 const DeclContext *DC = getEffectiveDeclContext(ND);
569
570 // If this is an extern variable declared locally, the relevant DeclContext
571 // is that of the containing namespace, or the translation unit.
572 // FIXME: This is a hack; extern variables declared locally should have
573 // a proper semantic declaration context!
Eli Friedman95f50122013-07-02 17:52:28 +0000574 if (isLocalContainerContext(DC) && ND->hasLinkage() && !isLambda(ND))
Guy Benyei11169dd2012-12-18 14:30:41 +0000575 while (!DC->isNamespace() && !DC->isTranslationUnit())
576 DC = getEffectiveParentContext(DC);
577 else if (GetLocalClassDecl(ND)) {
578 mangleLocalName(ND);
579 return;
580 }
581
582 DC = IgnoreLinkageSpecDecls(DC);
583
584 if (DC->isTranslationUnit() || isStdNamespace(DC)) {
585 // Check if we have a template.
586 const TemplateArgumentList *TemplateArgs = 0;
587 if (const TemplateDecl *TD = isTemplate(ND, TemplateArgs)) {
588 mangleUnscopedTemplateName(TD);
589 mangleTemplateArgs(*TemplateArgs);
590 return;
591 }
592
593 mangleUnscopedName(ND);
594 return;
595 }
596
Eli Friedman95f50122013-07-02 17:52:28 +0000597 if (isLocalContainerContext(DC)) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000598 mangleLocalName(ND);
599 return;
600 }
601
602 mangleNestedName(ND, DC);
603}
604void CXXNameMangler::mangleName(const TemplateDecl *TD,
605 const TemplateArgument *TemplateArgs,
606 unsigned NumTemplateArgs) {
607 const DeclContext *DC = IgnoreLinkageSpecDecls(getEffectiveDeclContext(TD));
608
609 if (DC->isTranslationUnit() || isStdNamespace(DC)) {
610 mangleUnscopedTemplateName(TD);
611 mangleTemplateArgs(TemplateArgs, NumTemplateArgs);
612 } else {
613 mangleNestedName(TD, TemplateArgs, NumTemplateArgs);
614 }
615}
616
617void CXXNameMangler::mangleUnscopedName(const NamedDecl *ND) {
618 // <unscoped-name> ::= <unqualified-name>
619 // ::= St <unqualified-name> # ::std::
620
621 if (isStdNamespace(IgnoreLinkageSpecDecls(getEffectiveDeclContext(ND))))
622 Out << "St";
623
624 mangleUnqualifiedName(ND);
625}
626
627void CXXNameMangler::mangleUnscopedTemplateName(const TemplateDecl *ND) {
628 // <unscoped-template-name> ::= <unscoped-name>
629 // ::= <substitution>
630 if (mangleSubstitution(ND))
631 return;
632
633 // <template-template-param> ::= <template-param>
634 if (const TemplateTemplateParmDecl *TTP
635 = dyn_cast<TemplateTemplateParmDecl>(ND)) {
636 mangleTemplateParameter(TTP->getIndex());
637 return;
638 }
639
640 mangleUnscopedName(ND->getTemplatedDecl());
641 addSubstitution(ND);
642}
643
644void CXXNameMangler::mangleUnscopedTemplateName(TemplateName Template) {
645 // <unscoped-template-name> ::= <unscoped-name>
646 // ::= <substitution>
647 if (TemplateDecl *TD = Template.getAsTemplateDecl())
648 return mangleUnscopedTemplateName(TD);
649
650 if (mangleSubstitution(Template))
651 return;
652
653 DependentTemplateName *Dependent = Template.getAsDependentTemplateName();
654 assert(Dependent && "Not a dependent template name?");
655 if (const IdentifierInfo *Id = Dependent->getIdentifier())
656 mangleSourceName(Id);
657 else
658 mangleOperatorName(Dependent->getOperator(), UnknownArity);
659
660 addSubstitution(Template);
661}
662
663void CXXNameMangler::mangleFloat(const llvm::APFloat &f) {
664 // ABI:
665 // Floating-point literals are encoded using a fixed-length
666 // lowercase hexadecimal string corresponding to the internal
667 // representation (IEEE on Itanium), high-order bytes first,
668 // without leading zeroes. For example: "Lf bf800000 E" is -1.0f
669 // on Itanium.
670 // The 'without leading zeroes' thing seems to be an editorial
671 // mistake; see the discussion on cxx-abi-dev beginning on
672 // 2012-01-16.
673
674 // Our requirements here are just barely weird enough to justify
675 // using a custom algorithm instead of post-processing APInt::toString().
676
677 llvm::APInt valueBits = f.bitcastToAPInt();
678 unsigned numCharacters = (valueBits.getBitWidth() + 3) / 4;
679 assert(numCharacters != 0);
680
681 // Allocate a buffer of the right number of characters.
Dmitri Gribenkof8579502013-01-12 19:30:44 +0000682 SmallVector<char, 20> buffer;
Guy Benyei11169dd2012-12-18 14:30:41 +0000683 buffer.set_size(numCharacters);
684
685 // Fill the buffer left-to-right.
686 for (unsigned stringIndex = 0; stringIndex != numCharacters; ++stringIndex) {
687 // The bit-index of the next hex digit.
688 unsigned digitBitIndex = 4 * (numCharacters - stringIndex - 1);
689
690 // Project out 4 bits starting at 'digitIndex'.
691 llvm::integerPart hexDigit
692 = valueBits.getRawData()[digitBitIndex / llvm::integerPartWidth];
693 hexDigit >>= (digitBitIndex % llvm::integerPartWidth);
694 hexDigit &= 0xF;
695
696 // Map that over to a lowercase hex digit.
697 static const char charForHex[16] = {
698 '0', '1', '2', '3', '4', '5', '6', '7',
699 '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'
700 };
701 buffer[stringIndex] = charForHex[hexDigit];
702 }
703
704 Out.write(buffer.data(), numCharacters);
705}
706
707void CXXNameMangler::mangleNumber(const llvm::APSInt &Value) {
708 if (Value.isSigned() && Value.isNegative()) {
709 Out << 'n';
710 Value.abs().print(Out, /*signed*/ false);
711 } else {
712 Value.print(Out, /*signed*/ false);
713 }
714}
715
716void CXXNameMangler::mangleNumber(int64_t Number) {
717 // <number> ::= [n] <non-negative decimal integer>
718 if (Number < 0) {
719 Out << 'n';
720 Number = -Number;
721 }
722
723 Out << Number;
724}
725
726void CXXNameMangler::mangleCallOffset(int64_t NonVirtual, int64_t Virtual) {
727 // <call-offset> ::= h <nv-offset> _
728 // ::= v <v-offset> _
729 // <nv-offset> ::= <offset number> # non-virtual base override
730 // <v-offset> ::= <offset number> _ <virtual offset number>
731 // # virtual base override, with vcall offset
732 if (!Virtual) {
733 Out << 'h';
734 mangleNumber(NonVirtual);
735 Out << '_';
736 return;
737 }
738
739 Out << 'v';
740 mangleNumber(NonVirtual);
741 Out << '_';
742 mangleNumber(Virtual);
743 Out << '_';
744}
745
746void CXXNameMangler::manglePrefix(QualType type) {
747 if (const TemplateSpecializationType *TST =
748 type->getAs<TemplateSpecializationType>()) {
749 if (!mangleSubstitution(QualType(TST, 0))) {
750 mangleTemplatePrefix(TST->getTemplateName());
751
752 // FIXME: GCC does not appear to mangle the template arguments when
753 // the template in question is a dependent template name. Should we
754 // emulate that badness?
755 mangleTemplateArgs(TST->getArgs(), TST->getNumArgs());
756 addSubstitution(QualType(TST, 0));
757 }
758 } else if (const DependentTemplateSpecializationType *DTST
759 = type->getAs<DependentTemplateSpecializationType>()) {
760 TemplateName Template
761 = getASTContext().getDependentTemplateName(DTST->getQualifier(),
762 DTST->getIdentifier());
763 mangleTemplatePrefix(Template);
764
765 // FIXME: GCC does not appear to mangle the template arguments when
766 // the template in question is a dependent template name. Should we
767 // emulate that badness?
768 mangleTemplateArgs(DTST->getArgs(), DTST->getNumArgs());
769 } else {
770 // We use the QualType mangle type variant here because it handles
771 // substitutions.
772 mangleType(type);
773 }
774}
775
776/// Mangle everything prior to the base-unresolved-name in an unresolved-name.
777///
778/// \param firstQualifierLookup - the entity found by unqualified lookup
779/// for the first name in the qualifier, if this is for a member expression
780/// \param recursive - true if this is being called recursively,
781/// i.e. if there is more prefix "to the right".
782void CXXNameMangler::mangleUnresolvedPrefix(NestedNameSpecifier *qualifier,
783 NamedDecl *firstQualifierLookup,
784 bool recursive) {
785
786 // x, ::x
787 // <unresolved-name> ::= [gs] <base-unresolved-name>
788
789 // T::x / decltype(p)::x
790 // <unresolved-name> ::= sr <unresolved-type> <base-unresolved-name>
791
792 // T::N::x /decltype(p)::N::x
793 // <unresolved-name> ::= srN <unresolved-type> <unresolved-qualifier-level>+ E
794 // <base-unresolved-name>
795
796 // A::x, N::y, A<T>::z; "gs" means leading "::"
797 // <unresolved-name> ::= [gs] sr <unresolved-qualifier-level>+ E
798 // <base-unresolved-name>
799
800 switch (qualifier->getKind()) {
801 case NestedNameSpecifier::Global:
802 Out << "gs";
803
804 // We want an 'sr' unless this is the entire NNS.
805 if (recursive)
806 Out << "sr";
807
808 // We never want an 'E' here.
809 return;
810
811 case NestedNameSpecifier::Namespace:
812 if (qualifier->getPrefix())
813 mangleUnresolvedPrefix(qualifier->getPrefix(), firstQualifierLookup,
814 /*recursive*/ true);
815 else
816 Out << "sr";
817 mangleSourceName(qualifier->getAsNamespace()->getIdentifier());
818 break;
819 case NestedNameSpecifier::NamespaceAlias:
820 if (qualifier->getPrefix())
821 mangleUnresolvedPrefix(qualifier->getPrefix(), firstQualifierLookup,
822 /*recursive*/ true);
823 else
824 Out << "sr";
825 mangleSourceName(qualifier->getAsNamespaceAlias()->getIdentifier());
826 break;
827
828 case NestedNameSpecifier::TypeSpec:
829 case NestedNameSpecifier::TypeSpecWithTemplate: {
830 const Type *type = qualifier->getAsType();
831
832 // We only want to use an unresolved-type encoding if this is one of:
833 // - a decltype
834 // - a template type parameter
835 // - a template template parameter with arguments
836 // In all of these cases, we should have no prefix.
837 if (qualifier->getPrefix()) {
838 mangleUnresolvedPrefix(qualifier->getPrefix(), firstQualifierLookup,
839 /*recursive*/ true);
840 } else {
841 // Otherwise, all the cases want this.
842 Out << "sr";
843 }
844
845 // Only certain other types are valid as prefixes; enumerate them.
846 switch (type->getTypeClass()) {
847 case Type::Builtin:
848 case Type::Complex:
Reid Kleckner0503a872013-12-05 01:23:43 +0000849 case Type::Adjusted:
Reid Kleckner8a365022013-06-24 17:51:48 +0000850 case Type::Decayed:
Guy Benyei11169dd2012-12-18 14:30:41 +0000851 case Type::Pointer:
852 case Type::BlockPointer:
853 case Type::LValueReference:
854 case Type::RValueReference:
855 case Type::MemberPointer:
856 case Type::ConstantArray:
857 case Type::IncompleteArray:
858 case Type::VariableArray:
859 case Type::DependentSizedArray:
860 case Type::DependentSizedExtVector:
861 case Type::Vector:
862 case Type::ExtVector:
863 case Type::FunctionProto:
864 case Type::FunctionNoProto:
865 case Type::Enum:
866 case Type::Paren:
867 case Type::Elaborated:
868 case Type::Attributed:
869 case Type::Auto:
870 case Type::PackExpansion:
871 case Type::ObjCObject:
872 case Type::ObjCInterface:
873 case Type::ObjCObjectPointer:
874 case Type::Atomic:
875 llvm_unreachable("type is illegal as a nested name specifier");
876
877 case Type::SubstTemplateTypeParmPack:
878 // FIXME: not clear how to mangle this!
879 // template <class T...> class A {
880 // template <class U...> void foo(decltype(T::foo(U())) x...);
881 // };
882 Out << "_SUBSTPACK_";
883 break;
884
885 // <unresolved-type> ::= <template-param>
886 // ::= <decltype>
887 // ::= <template-template-param> <template-args>
888 // (this last is not official yet)
889 case Type::TypeOfExpr:
890 case Type::TypeOf:
891 case Type::Decltype:
892 case Type::TemplateTypeParm:
893 case Type::UnaryTransform:
894 case Type::SubstTemplateTypeParm:
895 unresolvedType:
896 assert(!qualifier->getPrefix());
897
898 // We only get here recursively if we're followed by identifiers.
899 if (recursive) Out << 'N';
900
901 // This seems to do everything we want. It's not really
902 // sanctioned for a substituted template parameter, though.
903 mangleType(QualType(type, 0));
904
905 // We never want to print 'E' directly after an unresolved-type,
906 // so we return directly.
907 return;
908
909 case Type::Typedef:
910 mangleSourceName(cast<TypedefType>(type)->getDecl()->getIdentifier());
911 break;
912
913 case Type::UnresolvedUsing:
914 mangleSourceName(cast<UnresolvedUsingType>(type)->getDecl()
915 ->getIdentifier());
916 break;
917
918 case Type::Record:
919 mangleSourceName(cast<RecordType>(type)->getDecl()->getIdentifier());
920 break;
921
922 case Type::TemplateSpecialization: {
923 const TemplateSpecializationType *tst
924 = cast<TemplateSpecializationType>(type);
925 TemplateName name = tst->getTemplateName();
926 switch (name.getKind()) {
927 case TemplateName::Template:
928 case TemplateName::QualifiedTemplate: {
929 TemplateDecl *temp = name.getAsTemplateDecl();
930
931 // If the base is a template template parameter, this is an
932 // unresolved type.
933 assert(temp && "no template for template specialization type");
934 if (isa<TemplateTemplateParmDecl>(temp)) goto unresolvedType;
935
936 mangleSourceName(temp->getIdentifier());
937 break;
938 }
939
940 case TemplateName::OverloadedTemplate:
941 case TemplateName::DependentTemplate:
942 llvm_unreachable("invalid base for a template specialization type");
943
944 case TemplateName::SubstTemplateTemplateParm: {
945 SubstTemplateTemplateParmStorage *subst
946 = name.getAsSubstTemplateTemplateParm();
947 mangleExistingSubstitution(subst->getReplacement());
948 break;
949 }
950
951 case TemplateName::SubstTemplateTemplateParmPack: {
952 // FIXME: not clear how to mangle this!
953 // template <template <class U> class T...> class A {
954 // template <class U...> void foo(decltype(T<U>::foo) x...);
955 // };
956 Out << "_SUBSTPACK_";
957 break;
958 }
959 }
960
961 mangleTemplateArgs(tst->getArgs(), tst->getNumArgs());
962 break;
963 }
964
965 case Type::InjectedClassName:
966 mangleSourceName(cast<InjectedClassNameType>(type)->getDecl()
967 ->getIdentifier());
968 break;
969
970 case Type::DependentName:
971 mangleSourceName(cast<DependentNameType>(type)->getIdentifier());
972 break;
973
974 case Type::DependentTemplateSpecialization: {
975 const DependentTemplateSpecializationType *tst
976 = cast<DependentTemplateSpecializationType>(type);
977 mangleSourceName(tst->getIdentifier());
978 mangleTemplateArgs(tst->getArgs(), tst->getNumArgs());
979 break;
980 }
981 }
982 break;
983 }
984
985 case NestedNameSpecifier::Identifier:
986 // Member expressions can have these without prefixes.
987 if (qualifier->getPrefix()) {
988 mangleUnresolvedPrefix(qualifier->getPrefix(), firstQualifierLookup,
989 /*recursive*/ true);
990 } else if (firstQualifierLookup) {
991
992 // Try to make a proper qualifier out of the lookup result, and
993 // then just recurse on that.
994 NestedNameSpecifier *newQualifier;
995 if (TypeDecl *typeDecl = dyn_cast<TypeDecl>(firstQualifierLookup)) {
996 QualType type = getASTContext().getTypeDeclType(typeDecl);
997
998 // Pretend we had a different nested name specifier.
999 newQualifier = NestedNameSpecifier::Create(getASTContext(),
1000 /*prefix*/ 0,
1001 /*template*/ false,
1002 type.getTypePtr());
1003 } else if (NamespaceDecl *nspace =
1004 dyn_cast<NamespaceDecl>(firstQualifierLookup)) {
1005 newQualifier = NestedNameSpecifier::Create(getASTContext(),
1006 /*prefix*/ 0,
1007 nspace);
1008 } else if (NamespaceAliasDecl *alias =
1009 dyn_cast<NamespaceAliasDecl>(firstQualifierLookup)) {
1010 newQualifier = NestedNameSpecifier::Create(getASTContext(),
1011 /*prefix*/ 0,
1012 alias);
1013 } else {
1014 // No sensible mangling to do here.
1015 newQualifier = 0;
1016 }
1017
1018 if (newQualifier)
1019 return mangleUnresolvedPrefix(newQualifier, /*lookup*/ 0, recursive);
1020
1021 } else {
1022 Out << "sr";
1023 }
1024
1025 mangleSourceName(qualifier->getAsIdentifier());
1026 break;
1027 }
1028
1029 // If this was the innermost part of the NNS, and we fell out to
1030 // here, append an 'E'.
1031 if (!recursive)
1032 Out << 'E';
1033}
1034
1035/// Mangle an unresolved-name, which is generally used for names which
1036/// weren't resolved to specific entities.
1037void CXXNameMangler::mangleUnresolvedName(NestedNameSpecifier *qualifier,
1038 NamedDecl *firstQualifierLookup,
1039 DeclarationName name,
1040 unsigned knownArity) {
1041 if (qualifier) mangleUnresolvedPrefix(qualifier, firstQualifierLookup);
1042 mangleUnqualifiedName(0, name, knownArity);
1043}
1044
1045static const FieldDecl *FindFirstNamedDataMember(const RecordDecl *RD) {
1046 assert(RD->isAnonymousStructOrUnion() &&
1047 "Expected anonymous struct or union!");
1048
Aaron Ballmane8a8bae2014-03-08 20:12:42 +00001049 for (const auto *I : RD->fields()) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001050 if (I->getIdentifier())
Aaron Ballmane8a8bae2014-03-08 20:12:42 +00001051 return I;
Guy Benyei11169dd2012-12-18 14:30:41 +00001052
1053 if (const RecordType *RT = I->getType()->getAs<RecordType>())
1054 if (const FieldDecl *NamedDataMember =
1055 FindFirstNamedDataMember(RT->getDecl()))
1056 return NamedDataMember;
1057 }
1058
1059 // We didn't find a named data member.
1060 return 0;
1061}
1062
1063void CXXNameMangler::mangleUnqualifiedName(const NamedDecl *ND,
1064 DeclarationName Name,
1065 unsigned KnownArity) {
1066 // <unqualified-name> ::= <operator-name>
1067 // ::= <ctor-dtor-name>
1068 // ::= <source-name>
1069 switch (Name.getNameKind()) {
1070 case DeclarationName::Identifier: {
1071 if (const IdentifierInfo *II = Name.getAsIdentifierInfo()) {
1072 // We must avoid conflicts between internally- and externally-
1073 // linked variable and function declaration names in the same TU:
1074 // void test() { extern void foo(); }
1075 // static void foo();
1076 // This naming convention is the same as that followed by GCC,
1077 // though it shouldn't actually matter.
Rafael Espindola3ae00052013-05-13 00:12:11 +00001078 if (ND && ND->getFormalLinkage() == InternalLinkage &&
Guy Benyei11169dd2012-12-18 14:30:41 +00001079 getEffectiveDeclContext(ND)->isFileContext())
1080 Out << 'L';
1081
1082 mangleSourceName(II);
1083 break;
1084 }
1085
1086 // Otherwise, an anonymous entity. We must have a declaration.
1087 assert(ND && "mangling empty name without declaration");
1088
1089 if (const NamespaceDecl *NS = dyn_cast<NamespaceDecl>(ND)) {
1090 if (NS->isAnonymousNamespace()) {
1091 // This is how gcc mangles these names.
1092 Out << "12_GLOBAL__N_1";
1093 break;
1094 }
1095 }
1096
1097 if (const VarDecl *VD = dyn_cast<VarDecl>(ND)) {
1098 // We must have an anonymous union or struct declaration.
1099 const RecordDecl *RD =
1100 cast<RecordDecl>(VD->getType()->getAs<RecordType>()->getDecl());
1101
1102 // Itanium C++ ABI 5.1.2:
1103 //
1104 // For the purposes of mangling, the name of an anonymous union is
1105 // considered to be the name of the first named data member found by a
1106 // pre-order, depth-first, declaration-order walk of the data members of
1107 // the anonymous union. If there is no such data member (i.e., if all of
1108 // the data members in the union are unnamed), then there is no way for
1109 // a program to refer to the anonymous union, and there is therefore no
1110 // need to mangle its name.
1111 const FieldDecl *FD = FindFirstNamedDataMember(RD);
1112
1113 // It's actually possible for various reasons for us to get here
1114 // with an empty anonymous struct / union. Fortunately, it
1115 // doesn't really matter what name we generate.
1116 if (!FD) break;
1117 assert(FD->getIdentifier() && "Data member name isn't an identifier!");
1118
1119 mangleSourceName(FD->getIdentifier());
1120 break;
1121 }
John McCall924046f2013-04-10 06:08:21 +00001122
1123 // Class extensions have no name as a category, and it's possible
1124 // for them to be the semantic parent of certain declarations
1125 // (primarily, tag decls defined within declarations). Such
1126 // declarations will always have internal linkage, so the name
1127 // doesn't really matter, but we shouldn't crash on them. For
1128 // safety, just handle all ObjC containers here.
1129 if (isa<ObjCContainerDecl>(ND))
1130 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00001131
1132 // We must have an anonymous struct.
1133 const TagDecl *TD = cast<TagDecl>(ND);
1134 if (const TypedefNameDecl *D = TD->getTypedefNameForAnonDecl()) {
1135 assert(TD->getDeclContext() == D->getDeclContext() &&
1136 "Typedef should not be in another decl context!");
1137 assert(D->getDeclName().getAsIdentifierInfo() &&
1138 "Typedef was not named!");
1139 mangleSourceName(D->getDeclName().getAsIdentifierInfo());
1140 break;
1141 }
1142
1143 // <unnamed-type-name> ::= <closure-type-name>
1144 //
1145 // <closure-type-name> ::= Ul <lambda-sig> E [ <nonnegative number> ] _
1146 // <lambda-sig> ::= <parameter-type>+ # Parameter types or 'v' for 'void'.
1147 if (const CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(TD)) {
1148 if (Record->isLambda() && Record->getLambdaManglingNumber()) {
1149 mangleLambda(Record);
1150 break;
1151 }
1152 }
1153
Eli Friedman3b7d46c2013-07-10 00:30:46 +00001154 if (TD->isExternallyVisible()) {
1155 unsigned UnnamedMangle = getASTContext().getManglingNumber(TD);
Guy Benyei11169dd2012-12-18 14:30:41 +00001156 Out << "Ut";
Eli Friedman3b7d46c2013-07-10 00:30:46 +00001157 if (UnnamedMangle > 1)
1158 Out << llvm::utostr(UnnamedMangle - 2);
Guy Benyei11169dd2012-12-18 14:30:41 +00001159 Out << '_';
1160 break;
1161 }
1162
1163 // Get a unique id for the anonymous struct.
David Majnemer2206bf52014-03-05 08:57:59 +00001164 unsigned AnonStructId = Context.getAnonymousStructId(TD);
Guy Benyei11169dd2012-12-18 14:30:41 +00001165
1166 // Mangle it as a source name in the form
1167 // [n] $_<id>
1168 // where n is the length of the string.
1169 SmallString<8> Str;
1170 Str += "$_";
1171 Str += llvm::utostr(AnonStructId);
1172
1173 Out << Str.size();
1174 Out << Str.str();
1175 break;
1176 }
1177
1178 case DeclarationName::ObjCZeroArgSelector:
1179 case DeclarationName::ObjCOneArgSelector:
1180 case DeclarationName::ObjCMultiArgSelector:
1181 llvm_unreachable("Can't mangle Objective-C selector names here!");
1182
1183 case DeclarationName::CXXConstructorName:
1184 if (ND == Structor)
1185 // If the named decl is the C++ constructor we're mangling, use the type
1186 // we were given.
1187 mangleCXXCtorType(static_cast<CXXCtorType>(StructorType));
1188 else
1189 // Otherwise, use the complete constructor name. This is relevant if a
1190 // class with a constructor is declared within a constructor.
1191 mangleCXXCtorType(Ctor_Complete);
1192 break;
1193
1194 case DeclarationName::CXXDestructorName:
1195 if (ND == Structor)
1196 // If the named decl is the C++ destructor we're mangling, use the type we
1197 // were given.
1198 mangleCXXDtorType(static_cast<CXXDtorType>(StructorType));
1199 else
1200 // Otherwise, use the complete destructor name. This is relevant if a
1201 // class with a destructor is declared within a destructor.
1202 mangleCXXDtorType(Dtor_Complete);
1203 break;
1204
1205 case DeclarationName::CXXConversionFunctionName:
1206 // <operator-name> ::= cv <type> # (cast)
1207 Out << "cv";
1208 mangleType(Name.getCXXNameType());
1209 break;
1210
1211 case DeclarationName::CXXOperatorName: {
1212 unsigned Arity;
1213 if (ND) {
1214 Arity = cast<FunctionDecl>(ND)->getNumParams();
1215
1216 // If we have a C++ member function, we need to include the 'this' pointer.
1217 // FIXME: This does not make sense for operators that are static, but their
1218 // names stay the same regardless of the arity (operator new for instance).
1219 if (isa<CXXMethodDecl>(ND))
1220 Arity++;
1221 } else
1222 Arity = KnownArity;
1223
1224 mangleOperatorName(Name.getCXXOverloadedOperator(), Arity);
1225 break;
1226 }
1227
1228 case DeclarationName::CXXLiteralOperatorName:
1229 // FIXME: This mangling is not yet official.
1230 Out << "li";
1231 mangleSourceName(Name.getCXXLiteralIdentifier());
1232 break;
1233
1234 case DeclarationName::CXXUsingDirective:
1235 llvm_unreachable("Can't mangle a using directive name!");
1236 }
1237}
1238
1239void CXXNameMangler::mangleSourceName(const IdentifierInfo *II) {
1240 // <source-name> ::= <positive length number> <identifier>
1241 // <number> ::= [n] <non-negative decimal integer>
1242 // <identifier> ::= <unqualified source code identifier>
1243 Out << II->getLength() << II->getName();
1244}
1245
1246void CXXNameMangler::mangleNestedName(const NamedDecl *ND,
1247 const DeclContext *DC,
1248 bool NoFunction) {
1249 // <nested-name>
1250 // ::= N [<CV-qualifiers>] [<ref-qualifier>] <prefix> <unqualified-name> E
1251 // ::= N [<CV-qualifiers>] [<ref-qualifier>] <template-prefix>
1252 // <template-args> E
1253
1254 Out << 'N';
1255 if (const CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(ND)) {
David Majnemer42350df2013-11-03 23:51:28 +00001256 Qualifiers MethodQuals =
1257 Qualifiers::fromCVRMask(Method->getTypeQualifiers());
1258 // We do not consider restrict a distinguishing attribute for overloading
1259 // purposes so we must not mangle it.
1260 MethodQuals.removeRestrict();
1261 mangleQualifiers(MethodQuals);
Guy Benyei11169dd2012-12-18 14:30:41 +00001262 mangleRefQualifier(Method->getRefQualifier());
1263 }
1264
1265 // Check if we have a template.
1266 const TemplateArgumentList *TemplateArgs = 0;
1267 if (const TemplateDecl *TD = isTemplate(ND, TemplateArgs)) {
Eli Friedman86af13f02013-07-05 18:41:30 +00001268 mangleTemplatePrefix(TD, NoFunction);
Guy Benyei11169dd2012-12-18 14:30:41 +00001269 mangleTemplateArgs(*TemplateArgs);
1270 }
1271 else {
1272 manglePrefix(DC, NoFunction);
1273 mangleUnqualifiedName(ND);
1274 }
1275
1276 Out << 'E';
1277}
1278void CXXNameMangler::mangleNestedName(const TemplateDecl *TD,
1279 const TemplateArgument *TemplateArgs,
1280 unsigned NumTemplateArgs) {
1281 // <nested-name> ::= N [<CV-qualifiers>] <template-prefix> <template-args> E
1282
1283 Out << 'N';
1284
1285 mangleTemplatePrefix(TD);
1286 mangleTemplateArgs(TemplateArgs, NumTemplateArgs);
1287
1288 Out << 'E';
1289}
1290
Eli Friedman95f50122013-07-02 17:52:28 +00001291void CXXNameMangler::mangleLocalName(const Decl *D) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001292 // <local-name> := Z <function encoding> E <entity name> [<discriminator>]
1293 // := Z <function encoding> E s [<discriminator>]
1294 // <local-name> := Z <function encoding> E d [ <parameter number> ]
1295 // _ <entity name>
1296 // <discriminator> := _ <non-negative number>
Eli Friedman95f50122013-07-02 17:52:28 +00001297 assert(isa<NamedDecl>(D) || isa<BlockDecl>(D));
Eli Friedmaneecc09a2013-07-05 20:27:40 +00001298 const RecordDecl *RD = GetLocalClassDecl(D);
Eli Friedman95f50122013-07-02 17:52:28 +00001299 const DeclContext *DC = getEffectiveDeclContext(RD ? RD : D);
Guy Benyei11169dd2012-12-18 14:30:41 +00001300
1301 Out << 'Z';
1302
Eli Friedman92821742013-07-02 02:01:18 +00001303 if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(DC))
1304 mangleObjCMethodName(MD);
1305 else if (const BlockDecl *BD = dyn_cast<BlockDecl>(DC))
Eli Friedman95f50122013-07-02 17:52:28 +00001306 mangleBlockForPrefix(BD);
Eli Friedman92821742013-07-02 02:01:18 +00001307 else
1308 mangleFunctionEncoding(cast<FunctionDecl>(DC));
Guy Benyei11169dd2012-12-18 14:30:41 +00001309
Eli Friedman92821742013-07-02 02:01:18 +00001310 Out << 'E';
1311
1312 if (RD) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001313 // The parameter number is omitted for the last parameter, 0 for the
1314 // second-to-last parameter, 1 for the third-to-last parameter, etc. The
1315 // <entity name> will of course contain a <closure-type-name>: Its
1316 // numbering will be local to the particular argument in which it appears
1317 // -- other default arguments do not affect its encoding.
Eli Friedmaneecc09a2013-07-05 20:27:40 +00001318 const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD);
1319 if (CXXRD->isLambda()) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001320 if (const ParmVarDecl *Parm
Eli Friedmaneecc09a2013-07-05 20:27:40 +00001321 = dyn_cast_or_null<ParmVarDecl>(CXXRD->getLambdaContextDecl())) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001322 if (const FunctionDecl *Func
1323 = dyn_cast<FunctionDecl>(Parm->getDeclContext())) {
1324 Out << 'd';
1325 unsigned Num = Func->getNumParams() - Parm->getFunctionScopeIndex();
1326 if (Num > 1)
1327 mangleNumber(Num - 2);
1328 Out << '_';
Guy Benyei11169dd2012-12-18 14:30:41 +00001329 }
1330 }
1331 }
1332
1333 // Mangle the name relative to the closest enclosing function.
Eli Friedman95f50122013-07-02 17:52:28 +00001334 // equality ok because RD derived from ND above
1335 if (D == RD) {
1336 mangleUnqualifiedName(RD);
1337 } else if (const BlockDecl *BD = dyn_cast<BlockDecl>(D)) {
1338 manglePrefix(getEffectiveDeclContext(BD), true /*NoFunction*/);
1339 mangleUnqualifiedBlock(BD);
1340 } else {
1341 const NamedDecl *ND = cast<NamedDecl>(D);
Eli Friedman92821742013-07-02 02:01:18 +00001342 mangleNestedName(ND, getEffectiveDeclContext(ND), true /*NoFunction*/);
Eli Friedman95f50122013-07-02 17:52:28 +00001343 }
Eli Friedman0cd23352013-07-10 01:33:19 +00001344 } else if (const BlockDecl *BD = dyn_cast<BlockDecl>(D)) {
1345 // Mangle a block in a default parameter; see above explanation for
1346 // lambdas.
1347 if (const ParmVarDecl *Parm
1348 = dyn_cast_or_null<ParmVarDecl>(BD->getBlockManglingContextDecl())) {
1349 if (const FunctionDecl *Func
1350 = dyn_cast<FunctionDecl>(Parm->getDeclContext())) {
1351 Out << 'd';
1352 unsigned Num = Func->getNumParams() - Parm->getFunctionScopeIndex();
1353 if (Num > 1)
1354 mangleNumber(Num - 2);
1355 Out << '_';
1356 }
1357 }
1358
1359 mangleUnqualifiedBlock(BD);
Eli Friedman3b7d46c2013-07-10 00:30:46 +00001360 } else {
Eli Friedman0cd23352013-07-10 01:33:19 +00001361 mangleUnqualifiedName(cast<NamedDecl>(D));
Guy Benyei11169dd2012-12-18 14:30:41 +00001362 }
Eli Friedman0cd23352013-07-10 01:33:19 +00001363
Eli Friedman3b7d46c2013-07-10 00:30:46 +00001364 if (const NamedDecl *ND = dyn_cast<NamedDecl>(RD ? RD : D)) {
1365 unsigned disc;
1366 if (Context.getNextDiscriminator(ND, disc)) {
1367 if (disc < 10)
1368 Out << '_' << disc;
1369 else
1370 Out << "__" << disc << '_';
1371 }
1372 }
Eli Friedman95f50122013-07-02 17:52:28 +00001373}
1374
1375void CXXNameMangler::mangleBlockForPrefix(const BlockDecl *Block) {
1376 if (GetLocalClassDecl(Block)) {
1377 mangleLocalName(Block);
1378 return;
1379 }
1380 const DeclContext *DC = getEffectiveDeclContext(Block);
1381 if (isLocalContainerContext(DC)) {
1382 mangleLocalName(Block);
1383 return;
1384 }
1385 manglePrefix(getEffectiveDeclContext(Block));
1386 mangleUnqualifiedBlock(Block);
1387}
1388
1389void CXXNameMangler::mangleUnqualifiedBlock(const BlockDecl *Block) {
1390 if (Decl *Context = Block->getBlockManglingContextDecl()) {
1391 if ((isa<VarDecl>(Context) || isa<FieldDecl>(Context)) &&
1392 Context->getDeclContext()->isRecord()) {
1393 if (const IdentifierInfo *Name
1394 = cast<NamedDecl>(Context)->getIdentifier()) {
1395 mangleSourceName(Name);
1396 Out << 'M';
1397 }
1398 }
1399 }
1400
1401 // If we have a block mangling number, use it.
1402 unsigned Number = Block->getBlockManglingNumber();
1403 // Otherwise, just make up a number. It doesn't matter what it is because
1404 // the symbol in question isn't externally visible.
1405 if (!Number)
1406 Number = Context.getBlockId(Block, false);
1407 Out << "Ub";
1408 if (Number > 1)
1409 Out << Number - 2;
1410 Out << '_';
Guy Benyei11169dd2012-12-18 14:30:41 +00001411}
1412
1413void CXXNameMangler::mangleLambda(const CXXRecordDecl *Lambda) {
1414 // If the context of a closure type is an initializer for a class member
1415 // (static or nonstatic), it is encoded in a qualified name with a final
1416 // <prefix> of the form:
1417 //
1418 // <data-member-prefix> := <member source-name> M
1419 //
1420 // Technically, the data-member-prefix is part of the <prefix>. However,
1421 // since a closure type will always be mangled with a prefix, it's easier
1422 // to emit that last part of the prefix here.
1423 if (Decl *Context = Lambda->getLambdaContextDecl()) {
1424 if ((isa<VarDecl>(Context) || isa<FieldDecl>(Context)) &&
1425 Context->getDeclContext()->isRecord()) {
1426 if (const IdentifierInfo *Name
1427 = cast<NamedDecl>(Context)->getIdentifier()) {
1428 mangleSourceName(Name);
1429 Out << 'M';
1430 }
1431 }
1432 }
1433
1434 Out << "Ul";
1435 const FunctionProtoType *Proto = Lambda->getLambdaTypeInfo()->getType()->
1436 getAs<FunctionProtoType>();
1437 mangleBareFunctionType(Proto, /*MangleReturnType=*/false);
1438 Out << "E";
1439
1440 // The number is omitted for the first closure type with a given
1441 // <lambda-sig> in a given context; it is n-2 for the nth closure type
1442 // (in lexical order) with that same <lambda-sig> and context.
1443 //
1444 // The AST keeps track of the number for us.
1445 unsigned Number = Lambda->getLambdaManglingNumber();
1446 assert(Number > 0 && "Lambda should be mangled as an unnamed class");
1447 if (Number > 1)
1448 mangleNumber(Number - 2);
1449 Out << '_';
1450}
1451
1452void CXXNameMangler::manglePrefix(NestedNameSpecifier *qualifier) {
1453 switch (qualifier->getKind()) {
1454 case NestedNameSpecifier::Global:
1455 // nothing
1456 return;
1457
1458 case NestedNameSpecifier::Namespace:
1459 mangleName(qualifier->getAsNamespace());
1460 return;
1461
1462 case NestedNameSpecifier::NamespaceAlias:
1463 mangleName(qualifier->getAsNamespaceAlias()->getNamespace());
1464 return;
1465
1466 case NestedNameSpecifier::TypeSpec:
1467 case NestedNameSpecifier::TypeSpecWithTemplate:
1468 manglePrefix(QualType(qualifier->getAsType(), 0));
1469 return;
1470
1471 case NestedNameSpecifier::Identifier:
1472 // Member expressions can have these without prefixes, but that
1473 // should end up in mangleUnresolvedPrefix instead.
1474 assert(qualifier->getPrefix());
1475 manglePrefix(qualifier->getPrefix());
1476
1477 mangleSourceName(qualifier->getAsIdentifier());
1478 return;
1479 }
1480
1481 llvm_unreachable("unexpected nested name specifier");
1482}
1483
1484void CXXNameMangler::manglePrefix(const DeclContext *DC, bool NoFunction) {
1485 // <prefix> ::= <prefix> <unqualified-name>
1486 // ::= <template-prefix> <template-args>
1487 // ::= <template-param>
1488 // ::= # empty
1489 // ::= <substitution>
1490
1491 DC = IgnoreLinkageSpecDecls(DC);
1492
1493 if (DC->isTranslationUnit())
1494 return;
1495
Eli Friedman95f50122013-07-02 17:52:28 +00001496 if (NoFunction && isLocalContainerContext(DC))
1497 return;
Eli Friedman7e346a82013-07-01 20:22:57 +00001498
Eli Friedman95f50122013-07-02 17:52:28 +00001499 assert(!isLocalContainerContext(DC));
1500
Guy Benyei11169dd2012-12-18 14:30:41 +00001501 const NamedDecl *ND = cast<NamedDecl>(DC);
1502 if (mangleSubstitution(ND))
1503 return;
1504
1505 // Check if we have a template.
1506 const TemplateArgumentList *TemplateArgs = 0;
1507 if (const TemplateDecl *TD = isTemplate(ND, TemplateArgs)) {
1508 mangleTemplatePrefix(TD);
1509 mangleTemplateArgs(*TemplateArgs);
Eli Friedman95f50122013-07-02 17:52:28 +00001510 } else {
Guy Benyei11169dd2012-12-18 14:30:41 +00001511 manglePrefix(getEffectiveDeclContext(ND), NoFunction);
1512 mangleUnqualifiedName(ND);
1513 }
1514
1515 addSubstitution(ND);
1516}
1517
1518void CXXNameMangler::mangleTemplatePrefix(TemplateName Template) {
1519 // <template-prefix> ::= <prefix> <template unqualified-name>
1520 // ::= <template-param>
1521 // ::= <substitution>
1522 if (TemplateDecl *TD = Template.getAsTemplateDecl())
1523 return mangleTemplatePrefix(TD);
1524
1525 if (QualifiedTemplateName *Qualified = Template.getAsQualifiedTemplateName())
1526 manglePrefix(Qualified->getQualifier());
1527
1528 if (OverloadedTemplateStorage *Overloaded
1529 = Template.getAsOverloadedTemplate()) {
1530 mangleUnqualifiedName(0, (*Overloaded->begin())->getDeclName(),
1531 UnknownArity);
1532 return;
1533 }
1534
1535 DependentTemplateName *Dependent = Template.getAsDependentTemplateName();
1536 assert(Dependent && "Unknown template name kind?");
1537 manglePrefix(Dependent->getQualifier());
1538 mangleUnscopedTemplateName(Template);
1539}
1540
Eli Friedman86af13f02013-07-05 18:41:30 +00001541void CXXNameMangler::mangleTemplatePrefix(const TemplateDecl *ND,
1542 bool NoFunction) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001543 // <template-prefix> ::= <prefix> <template unqualified-name>
1544 // ::= <template-param>
1545 // ::= <substitution>
1546 // <template-template-param> ::= <template-param>
1547 // <substitution>
1548
1549 if (mangleSubstitution(ND))
1550 return;
1551
1552 // <template-template-param> ::= <template-param>
1553 if (const TemplateTemplateParmDecl *TTP
1554 = dyn_cast<TemplateTemplateParmDecl>(ND)) {
1555 mangleTemplateParameter(TTP->getIndex());
1556 return;
1557 }
1558
Eli Friedman86af13f02013-07-05 18:41:30 +00001559 manglePrefix(getEffectiveDeclContext(ND), NoFunction);
Guy Benyei11169dd2012-12-18 14:30:41 +00001560 mangleUnqualifiedName(ND->getTemplatedDecl());
1561 addSubstitution(ND);
1562}
1563
1564/// Mangles a template name under the production <type>. Required for
1565/// template template arguments.
1566/// <type> ::= <class-enum-type>
1567/// ::= <template-param>
1568/// ::= <substitution>
1569void CXXNameMangler::mangleType(TemplateName TN) {
1570 if (mangleSubstitution(TN))
1571 return;
1572
1573 TemplateDecl *TD = 0;
1574
1575 switch (TN.getKind()) {
1576 case TemplateName::QualifiedTemplate:
1577 TD = TN.getAsQualifiedTemplateName()->getTemplateDecl();
1578 goto HaveDecl;
1579
1580 case TemplateName::Template:
1581 TD = TN.getAsTemplateDecl();
1582 goto HaveDecl;
1583
1584 HaveDecl:
1585 if (isa<TemplateTemplateParmDecl>(TD))
1586 mangleTemplateParameter(cast<TemplateTemplateParmDecl>(TD)->getIndex());
1587 else
1588 mangleName(TD);
1589 break;
1590
1591 case TemplateName::OverloadedTemplate:
1592 llvm_unreachable("can't mangle an overloaded template name as a <type>");
1593
1594 case TemplateName::DependentTemplate: {
1595 const DependentTemplateName *Dependent = TN.getAsDependentTemplateName();
1596 assert(Dependent->isIdentifier());
1597
1598 // <class-enum-type> ::= <name>
1599 // <name> ::= <nested-name>
1600 mangleUnresolvedPrefix(Dependent->getQualifier(), 0);
1601 mangleSourceName(Dependent->getIdentifier());
1602 break;
1603 }
1604
1605 case TemplateName::SubstTemplateTemplateParm: {
1606 // Substituted template parameters are mangled as the substituted
1607 // template. This will check for the substitution twice, which is
1608 // fine, but we have to return early so that we don't try to *add*
1609 // the substitution twice.
1610 SubstTemplateTemplateParmStorage *subst
1611 = TN.getAsSubstTemplateTemplateParm();
1612 mangleType(subst->getReplacement());
1613 return;
1614 }
1615
1616 case TemplateName::SubstTemplateTemplateParmPack: {
1617 // FIXME: not clear how to mangle this!
1618 // template <template <class> class T...> class A {
1619 // template <template <class> class U...> void foo(B<T,U> x...);
1620 // };
1621 Out << "_SUBSTPACK_";
1622 break;
1623 }
1624 }
1625
1626 addSubstitution(TN);
1627}
1628
1629void
1630CXXNameMangler::mangleOperatorName(OverloadedOperatorKind OO, unsigned Arity) {
1631 switch (OO) {
1632 // <operator-name> ::= nw # new
1633 case OO_New: Out << "nw"; break;
1634 // ::= na # new[]
1635 case OO_Array_New: Out << "na"; break;
1636 // ::= dl # delete
1637 case OO_Delete: Out << "dl"; break;
1638 // ::= da # delete[]
1639 case OO_Array_Delete: Out << "da"; break;
1640 // ::= ps # + (unary)
1641 // ::= pl # + (binary or unknown)
1642 case OO_Plus:
1643 Out << (Arity == 1? "ps" : "pl"); break;
1644 // ::= ng # - (unary)
1645 // ::= mi # - (binary or unknown)
1646 case OO_Minus:
1647 Out << (Arity == 1? "ng" : "mi"); break;
1648 // ::= ad # & (unary)
1649 // ::= an # & (binary or unknown)
1650 case OO_Amp:
1651 Out << (Arity == 1? "ad" : "an"); break;
1652 // ::= de # * (unary)
1653 // ::= ml # * (binary or unknown)
1654 case OO_Star:
1655 // Use binary when unknown.
1656 Out << (Arity == 1? "de" : "ml"); break;
1657 // ::= co # ~
1658 case OO_Tilde: Out << "co"; break;
1659 // ::= dv # /
1660 case OO_Slash: Out << "dv"; break;
1661 // ::= rm # %
1662 case OO_Percent: Out << "rm"; break;
1663 // ::= or # |
1664 case OO_Pipe: Out << "or"; break;
1665 // ::= eo # ^
1666 case OO_Caret: Out << "eo"; break;
1667 // ::= aS # =
1668 case OO_Equal: Out << "aS"; break;
1669 // ::= pL # +=
1670 case OO_PlusEqual: Out << "pL"; break;
1671 // ::= mI # -=
1672 case OO_MinusEqual: Out << "mI"; break;
1673 // ::= mL # *=
1674 case OO_StarEqual: Out << "mL"; break;
1675 // ::= dV # /=
1676 case OO_SlashEqual: Out << "dV"; break;
1677 // ::= rM # %=
1678 case OO_PercentEqual: Out << "rM"; break;
1679 // ::= aN # &=
1680 case OO_AmpEqual: Out << "aN"; break;
1681 // ::= oR # |=
1682 case OO_PipeEqual: Out << "oR"; break;
1683 // ::= eO # ^=
1684 case OO_CaretEqual: Out << "eO"; break;
1685 // ::= ls # <<
1686 case OO_LessLess: Out << "ls"; break;
1687 // ::= rs # >>
1688 case OO_GreaterGreater: Out << "rs"; break;
1689 // ::= lS # <<=
1690 case OO_LessLessEqual: Out << "lS"; break;
1691 // ::= rS # >>=
1692 case OO_GreaterGreaterEqual: Out << "rS"; break;
1693 // ::= eq # ==
1694 case OO_EqualEqual: Out << "eq"; break;
1695 // ::= ne # !=
1696 case OO_ExclaimEqual: Out << "ne"; break;
1697 // ::= lt # <
1698 case OO_Less: Out << "lt"; break;
1699 // ::= gt # >
1700 case OO_Greater: Out << "gt"; break;
1701 // ::= le # <=
1702 case OO_LessEqual: Out << "le"; break;
1703 // ::= ge # >=
1704 case OO_GreaterEqual: Out << "ge"; break;
1705 // ::= nt # !
1706 case OO_Exclaim: Out << "nt"; break;
1707 // ::= aa # &&
1708 case OO_AmpAmp: Out << "aa"; break;
1709 // ::= oo # ||
1710 case OO_PipePipe: Out << "oo"; break;
1711 // ::= pp # ++
1712 case OO_PlusPlus: Out << "pp"; break;
1713 // ::= mm # --
1714 case OO_MinusMinus: Out << "mm"; break;
1715 // ::= cm # ,
1716 case OO_Comma: Out << "cm"; break;
1717 // ::= pm # ->*
1718 case OO_ArrowStar: Out << "pm"; break;
1719 // ::= pt # ->
1720 case OO_Arrow: Out << "pt"; break;
1721 // ::= cl # ()
1722 case OO_Call: Out << "cl"; break;
1723 // ::= ix # []
1724 case OO_Subscript: Out << "ix"; break;
1725
1726 // ::= qu # ?
1727 // The conditional operator can't be overloaded, but we still handle it when
1728 // mangling expressions.
1729 case OO_Conditional: Out << "qu"; break;
1730
1731 case OO_None:
1732 case NUM_OVERLOADED_OPERATORS:
1733 llvm_unreachable("Not an overloaded operator");
1734 }
1735}
1736
1737void CXXNameMangler::mangleQualifiers(Qualifiers Quals) {
1738 // <CV-qualifiers> ::= [r] [V] [K] # restrict (C99), volatile, const
1739 if (Quals.hasRestrict())
1740 Out << 'r';
1741 if (Quals.hasVolatile())
1742 Out << 'V';
1743 if (Quals.hasConst())
1744 Out << 'K';
1745
1746 if (Quals.hasAddressSpace()) {
David Tweed31d09b02013-09-13 12:04:22 +00001747 // Address space extension:
Guy Benyei11169dd2012-12-18 14:30:41 +00001748 //
David Tweed31d09b02013-09-13 12:04:22 +00001749 // <type> ::= U <target-addrspace>
1750 // <type> ::= U <OpenCL-addrspace>
1751 // <type> ::= U <CUDA-addrspace>
1752
Guy Benyei11169dd2012-12-18 14:30:41 +00001753 SmallString<64> ASString;
David Tweed31d09b02013-09-13 12:04:22 +00001754 unsigned AS = Quals.getAddressSpace();
David Tweed31d09b02013-09-13 12:04:22 +00001755
1756 if (Context.getASTContext().addressSpaceMapManglingFor(AS)) {
1757 // <target-addrspace> ::= "AS" <address-space-number>
1758 unsigned TargetAS = Context.getASTContext().getTargetAddressSpace(AS);
1759 ASString = "AS" + llvm::utostr_32(TargetAS);
1760 } else {
1761 switch (AS) {
1762 default: llvm_unreachable("Not a language specific address space");
1763 // <OpenCL-addrspace> ::= "CL" [ "global" | "local" | "constant" ]
1764 case LangAS::opencl_global: ASString = "CLglobal"; break;
1765 case LangAS::opencl_local: ASString = "CLlocal"; break;
1766 case LangAS::opencl_constant: ASString = "CLconstant"; break;
1767 // <CUDA-addrspace> ::= "CU" [ "device" | "constant" | "shared" ]
1768 case LangAS::cuda_device: ASString = "CUdevice"; break;
1769 case LangAS::cuda_constant: ASString = "CUconstant"; break;
1770 case LangAS::cuda_shared: ASString = "CUshared"; break;
1771 }
1772 }
Guy Benyei11169dd2012-12-18 14:30:41 +00001773 Out << 'U' << ASString.size() << ASString;
1774 }
1775
1776 StringRef LifetimeName;
1777 switch (Quals.getObjCLifetime()) {
1778 // Objective-C ARC Extension:
1779 //
1780 // <type> ::= U "__strong"
1781 // <type> ::= U "__weak"
1782 // <type> ::= U "__autoreleasing"
1783 case Qualifiers::OCL_None:
1784 break;
1785
1786 case Qualifiers::OCL_Weak:
1787 LifetimeName = "__weak";
1788 break;
1789
1790 case Qualifiers::OCL_Strong:
1791 LifetimeName = "__strong";
1792 break;
1793
1794 case Qualifiers::OCL_Autoreleasing:
1795 LifetimeName = "__autoreleasing";
1796 break;
1797
1798 case Qualifiers::OCL_ExplicitNone:
1799 // The __unsafe_unretained qualifier is *not* mangled, so that
1800 // __unsafe_unretained types in ARC produce the same manglings as the
1801 // equivalent (but, naturally, unqualified) types in non-ARC, providing
1802 // better ABI compatibility.
1803 //
1804 // It's safe to do this because unqualified 'id' won't show up
1805 // in any type signatures that need to be mangled.
1806 break;
1807 }
1808 if (!LifetimeName.empty())
1809 Out << 'U' << LifetimeName.size() << LifetimeName;
1810}
1811
1812void CXXNameMangler::mangleRefQualifier(RefQualifierKind RefQualifier) {
1813 // <ref-qualifier> ::= R # lvalue reference
1814 // ::= O # rvalue-reference
Guy Benyei11169dd2012-12-18 14:30:41 +00001815 switch (RefQualifier) {
1816 case RQ_None:
1817 break;
1818
1819 case RQ_LValue:
1820 Out << 'R';
1821 break;
1822
1823 case RQ_RValue:
1824 Out << 'O';
1825 break;
1826 }
1827}
1828
1829void CXXNameMangler::mangleObjCMethodName(const ObjCMethodDecl *MD) {
1830 Context.mangleObjCMethodName(MD, Out);
1831}
1832
1833void CXXNameMangler::mangleType(QualType T) {
1834 // If our type is instantiation-dependent but not dependent, we mangle
1835 // it as it was written in the source, removing any top-level sugar.
1836 // Otherwise, use the canonical type.
1837 //
1838 // FIXME: This is an approximation of the instantiation-dependent name
1839 // mangling rules, since we should really be using the type as written and
1840 // augmented via semantic analysis (i.e., with implicit conversions and
1841 // default template arguments) for any instantiation-dependent type.
1842 // Unfortunately, that requires several changes to our AST:
1843 // - Instantiation-dependent TemplateSpecializationTypes will need to be
1844 // uniqued, so that we can handle substitutions properly
1845 // - Default template arguments will need to be represented in the
1846 // TemplateSpecializationType, since they need to be mangled even though
1847 // they aren't written.
1848 // - Conversions on non-type template arguments need to be expressed, since
1849 // they can affect the mangling of sizeof/alignof.
1850 if (!T->isInstantiationDependentType() || T->isDependentType())
1851 T = T.getCanonicalType();
1852 else {
1853 // Desugar any types that are purely sugar.
1854 do {
1855 // Don't desugar through template specialization types that aren't
1856 // type aliases. We need to mangle the template arguments as written.
1857 if (const TemplateSpecializationType *TST
1858 = dyn_cast<TemplateSpecializationType>(T))
1859 if (!TST->isTypeAlias())
1860 break;
1861
1862 QualType Desugared
1863 = T.getSingleStepDesugaredType(Context.getASTContext());
1864 if (Desugared == T)
1865 break;
1866
1867 T = Desugared;
1868 } while (true);
1869 }
1870 SplitQualType split = T.split();
1871 Qualifiers quals = split.Quals;
1872 const Type *ty = split.Ty;
1873
1874 bool isSubstitutable = quals || !isa<BuiltinType>(T);
1875 if (isSubstitutable && mangleSubstitution(T))
1876 return;
1877
1878 // If we're mangling a qualified array type, push the qualifiers to
1879 // the element type.
1880 if (quals && isa<ArrayType>(T)) {
1881 ty = Context.getASTContext().getAsArrayType(T);
1882 quals = Qualifiers();
1883
1884 // Note that we don't update T: we want to add the
1885 // substitution at the original type.
1886 }
1887
1888 if (quals) {
1889 mangleQualifiers(quals);
1890 // Recurse: even if the qualified type isn't yet substitutable,
1891 // the unqualified type might be.
1892 mangleType(QualType(ty, 0));
1893 } else {
1894 switch (ty->getTypeClass()) {
1895#define ABSTRACT_TYPE(CLASS, PARENT)
1896#define NON_CANONICAL_TYPE(CLASS, PARENT) \
1897 case Type::CLASS: \
1898 llvm_unreachable("can't mangle non-canonical type " #CLASS "Type"); \
1899 return;
1900#define TYPE(CLASS, PARENT) \
1901 case Type::CLASS: \
1902 mangleType(static_cast<const CLASS##Type*>(ty)); \
1903 break;
1904#include "clang/AST/TypeNodes.def"
1905 }
1906 }
1907
1908 // Add the substitution.
1909 if (isSubstitutable)
1910 addSubstitution(T);
1911}
1912
1913void CXXNameMangler::mangleNameOrStandardSubstitution(const NamedDecl *ND) {
1914 if (!mangleStandardSubstitution(ND))
1915 mangleName(ND);
1916}
1917
1918void CXXNameMangler::mangleType(const BuiltinType *T) {
1919 // <type> ::= <builtin-type>
1920 // <builtin-type> ::= v # void
1921 // ::= w # wchar_t
1922 // ::= b # bool
1923 // ::= c # char
1924 // ::= a # signed char
1925 // ::= h # unsigned char
1926 // ::= s # short
1927 // ::= t # unsigned short
1928 // ::= i # int
1929 // ::= j # unsigned int
1930 // ::= l # long
1931 // ::= m # unsigned long
1932 // ::= x # long long, __int64
1933 // ::= y # unsigned long long, __int64
1934 // ::= n # __int128
Ekaterina Romanova91b655b2013-11-21 22:25:24 +00001935 // ::= o # unsigned __int128
Guy Benyei11169dd2012-12-18 14:30:41 +00001936 // ::= f # float
1937 // ::= d # double
1938 // ::= e # long double, __float80
1939 // UNSUPPORTED: ::= g # __float128
1940 // UNSUPPORTED: ::= Dd # IEEE 754r decimal floating point (64 bits)
1941 // UNSUPPORTED: ::= De # IEEE 754r decimal floating point (128 bits)
1942 // UNSUPPORTED: ::= Df # IEEE 754r decimal floating point (32 bits)
1943 // ::= Dh # IEEE 754r half-precision floating point (16 bits)
1944 // ::= Di # char32_t
1945 // ::= Ds # char16_t
1946 // ::= Dn # std::nullptr_t (i.e., decltype(nullptr))
1947 // ::= u <source-name> # vendor extended type
1948 switch (T->getKind()) {
1949 case BuiltinType::Void: Out << 'v'; break;
1950 case BuiltinType::Bool: Out << 'b'; break;
1951 case BuiltinType::Char_U: case BuiltinType::Char_S: Out << 'c'; break;
1952 case BuiltinType::UChar: Out << 'h'; break;
1953 case BuiltinType::UShort: Out << 't'; break;
1954 case BuiltinType::UInt: Out << 'j'; break;
1955 case BuiltinType::ULong: Out << 'm'; break;
1956 case BuiltinType::ULongLong: Out << 'y'; break;
1957 case BuiltinType::UInt128: Out << 'o'; break;
1958 case BuiltinType::SChar: Out << 'a'; break;
1959 case BuiltinType::WChar_S:
1960 case BuiltinType::WChar_U: Out << 'w'; break;
1961 case BuiltinType::Char16: Out << "Ds"; break;
1962 case BuiltinType::Char32: Out << "Di"; break;
1963 case BuiltinType::Short: Out << 's'; break;
1964 case BuiltinType::Int: Out << 'i'; break;
1965 case BuiltinType::Long: Out << 'l'; break;
1966 case BuiltinType::LongLong: Out << 'x'; break;
1967 case BuiltinType::Int128: Out << 'n'; break;
1968 case BuiltinType::Half: Out << "Dh"; break;
1969 case BuiltinType::Float: Out << 'f'; break;
1970 case BuiltinType::Double: Out << 'd'; break;
1971 case BuiltinType::LongDouble: Out << 'e'; break;
1972 case BuiltinType::NullPtr: Out << "Dn"; break;
1973
1974#define BUILTIN_TYPE(Id, SingletonId)
1975#define PLACEHOLDER_TYPE(Id, SingletonId) \
1976 case BuiltinType::Id:
1977#include "clang/AST/BuiltinTypes.def"
1978 case BuiltinType::Dependent:
1979 llvm_unreachable("mangling a placeholder type");
1980 case BuiltinType::ObjCId: Out << "11objc_object"; break;
1981 case BuiltinType::ObjCClass: Out << "10objc_class"; break;
1982 case BuiltinType::ObjCSel: Out << "13objc_selector"; break;
Guy Benyeid8a08ea2012-12-18 14:38:23 +00001983 case BuiltinType::OCLImage1d: Out << "11ocl_image1d"; break;
1984 case BuiltinType::OCLImage1dArray: Out << "16ocl_image1darray"; break;
1985 case BuiltinType::OCLImage1dBuffer: Out << "17ocl_image1dbuffer"; break;
1986 case BuiltinType::OCLImage2d: Out << "11ocl_image2d"; break;
1987 case BuiltinType::OCLImage2dArray: Out << "16ocl_image2darray"; break;
1988 case BuiltinType::OCLImage3d: Out << "11ocl_image3d"; break;
Guy Benyei61054192013-02-07 10:55:47 +00001989 case BuiltinType::OCLSampler: Out << "11ocl_sampler"; break;
Guy Benyei1b4fb3e2013-01-20 12:31:11 +00001990 case BuiltinType::OCLEvent: Out << "9ocl_event"; break;
Guy Benyei11169dd2012-12-18 14:30:41 +00001991 }
1992}
1993
1994// <type> ::= <function-type>
1995// <function-type> ::= [<CV-qualifiers>] F [Y]
1996// <bare-function-type> [<ref-qualifier>] E
Guy Benyei11169dd2012-12-18 14:30:41 +00001997void CXXNameMangler::mangleType(const FunctionProtoType *T) {
1998 // Mangle CV-qualifiers, if present. These are 'this' qualifiers,
1999 // e.g. "const" in "int (A::*)() const".
2000 mangleQualifiers(Qualifiers::fromCVRMask(T->getTypeQuals()));
2001
2002 Out << 'F';
2003
2004 // FIXME: We don't have enough information in the AST to produce the 'Y'
2005 // encoding for extern "C" function types.
2006 mangleBareFunctionType(T, /*MangleReturnType=*/true);
2007
2008 // Mangle the ref-qualifier, if present.
2009 mangleRefQualifier(T->getRefQualifier());
2010
2011 Out << 'E';
2012}
2013void CXXNameMangler::mangleType(const FunctionNoProtoType *T) {
2014 llvm_unreachable("Can't mangle K&R function prototypes");
2015}
2016void CXXNameMangler::mangleBareFunctionType(const FunctionType *T,
2017 bool MangleReturnType) {
2018 // We should never be mangling something without a prototype.
2019 const FunctionProtoType *Proto = cast<FunctionProtoType>(T);
2020
2021 // Record that we're in a function type. See mangleFunctionParam
2022 // for details on what we're trying to achieve here.
2023 FunctionTypeDepthState saved = FunctionTypeDepth.push();
2024
2025 // <bare-function-type> ::= <signature type>+
2026 if (MangleReturnType) {
2027 FunctionTypeDepth.enterResultType();
Alp Toker314cc812014-01-25 16:55:45 +00002028 mangleType(Proto->getReturnType());
Guy Benyei11169dd2012-12-18 14:30:41 +00002029 FunctionTypeDepth.leaveResultType();
2030 }
2031
Alp Toker9cacbab2014-01-20 20:26:09 +00002032 if (Proto->getNumParams() == 0 && !Proto->isVariadic()) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002033 // <builtin-type> ::= v # void
2034 Out << 'v';
2035
2036 FunctionTypeDepth.pop(saved);
2037 return;
2038 }
2039
Aaron Ballman40bd0aa2014-03-17 15:23:01 +00002040 for (const auto &Arg : Proto->param_types())
2041 mangleType(Context.getASTContext().getSignatureParameterType(Arg));
Guy Benyei11169dd2012-12-18 14:30:41 +00002042
2043 FunctionTypeDepth.pop(saved);
2044
2045 // <builtin-type> ::= z # ellipsis
2046 if (Proto->isVariadic())
2047 Out << 'z';
2048}
2049
2050// <type> ::= <class-enum-type>
2051// <class-enum-type> ::= <name>
2052void CXXNameMangler::mangleType(const UnresolvedUsingType *T) {
2053 mangleName(T->getDecl());
2054}
2055
2056// <type> ::= <class-enum-type>
2057// <class-enum-type> ::= <name>
2058void CXXNameMangler::mangleType(const EnumType *T) {
2059 mangleType(static_cast<const TagType*>(T));
2060}
2061void CXXNameMangler::mangleType(const RecordType *T) {
2062 mangleType(static_cast<const TagType*>(T));
2063}
2064void CXXNameMangler::mangleType(const TagType *T) {
2065 mangleName(T->getDecl());
2066}
2067
2068// <type> ::= <array-type>
2069// <array-type> ::= A <positive dimension number> _ <element type>
2070// ::= A [<dimension expression>] _ <element type>
2071void CXXNameMangler::mangleType(const ConstantArrayType *T) {
2072 Out << 'A' << T->getSize() << '_';
2073 mangleType(T->getElementType());
2074}
2075void CXXNameMangler::mangleType(const VariableArrayType *T) {
2076 Out << 'A';
2077 // decayed vla types (size 0) will just be skipped.
2078 if (T->getSizeExpr())
2079 mangleExpression(T->getSizeExpr());
2080 Out << '_';
2081 mangleType(T->getElementType());
2082}
2083void CXXNameMangler::mangleType(const DependentSizedArrayType *T) {
2084 Out << 'A';
2085 mangleExpression(T->getSizeExpr());
2086 Out << '_';
2087 mangleType(T->getElementType());
2088}
2089void CXXNameMangler::mangleType(const IncompleteArrayType *T) {
2090 Out << "A_";
2091 mangleType(T->getElementType());
2092}
2093
2094// <type> ::= <pointer-to-member-type>
2095// <pointer-to-member-type> ::= M <class type> <member type>
2096void CXXNameMangler::mangleType(const MemberPointerType *T) {
2097 Out << 'M';
2098 mangleType(QualType(T->getClass(), 0));
2099 QualType PointeeType = T->getPointeeType();
2100 if (const FunctionProtoType *FPT = dyn_cast<FunctionProtoType>(PointeeType)) {
2101 mangleType(FPT);
2102
2103 // Itanium C++ ABI 5.1.8:
2104 //
2105 // The type of a non-static member function is considered to be different,
2106 // for the purposes of substitution, from the type of a namespace-scope or
2107 // static member function whose type appears similar. The types of two
2108 // non-static member functions are considered to be different, for the
2109 // purposes of substitution, if the functions are members of different
2110 // classes. In other words, for the purposes of substitution, the class of
2111 // which the function is a member is considered part of the type of
2112 // function.
2113
2114 // Given that we already substitute member function pointers as a
2115 // whole, the net effect of this rule is just to unconditionally
2116 // suppress substitution on the function type in a member pointer.
2117 // We increment the SeqID here to emulate adding an entry to the
2118 // substitution table.
2119 ++SeqID;
2120 } else
2121 mangleType(PointeeType);
2122}
2123
2124// <type> ::= <template-param>
2125void CXXNameMangler::mangleType(const TemplateTypeParmType *T) {
2126 mangleTemplateParameter(T->getIndex());
2127}
2128
2129// <type> ::= <template-param>
2130void CXXNameMangler::mangleType(const SubstTemplateTypeParmPackType *T) {
2131 // FIXME: not clear how to mangle this!
2132 // template <class T...> class A {
2133 // template <class U...> void foo(T(*)(U) x...);
2134 // };
2135 Out << "_SUBSTPACK_";
2136}
2137
2138// <type> ::= P <type> # pointer-to
2139void CXXNameMangler::mangleType(const PointerType *T) {
2140 Out << 'P';
2141 mangleType(T->getPointeeType());
2142}
2143void CXXNameMangler::mangleType(const ObjCObjectPointerType *T) {
2144 Out << 'P';
2145 mangleType(T->getPointeeType());
2146}
2147
2148// <type> ::= R <type> # reference-to
2149void CXXNameMangler::mangleType(const LValueReferenceType *T) {
2150 Out << 'R';
2151 mangleType(T->getPointeeType());
2152}
2153
2154// <type> ::= O <type> # rvalue reference-to (C++0x)
2155void CXXNameMangler::mangleType(const RValueReferenceType *T) {
2156 Out << 'O';
2157 mangleType(T->getPointeeType());
2158}
2159
2160// <type> ::= C <type> # complex pair (C 2000)
2161void CXXNameMangler::mangleType(const ComplexType *T) {
2162 Out << 'C';
2163 mangleType(T->getElementType());
2164}
2165
2166// ARM's ABI for Neon vector types specifies that they should be mangled as
2167// if they are structs (to match ARM's initial implementation). The
2168// vector type must be one of the special types predefined by ARM.
2169void CXXNameMangler::mangleNeonVectorType(const VectorType *T) {
2170 QualType EltType = T->getElementType();
2171 assert(EltType->isBuiltinType() && "Neon vector element not a BuiltinType");
2172 const char *EltName = 0;
2173 if (T->getVectorKind() == VectorType::NeonPolyVector) {
2174 switch (cast<BuiltinType>(EltType)->getKind()) {
Tim Northovera2ee4332014-03-29 15:09:45 +00002175 case BuiltinType::SChar:
2176 case BuiltinType::UChar:
2177 EltName = "poly8_t";
2178 break;
2179 case BuiltinType::Short:
2180 case BuiltinType::UShort:
2181 EltName = "poly16_t";
2182 break;
2183 case BuiltinType::ULongLong:
2184 EltName = "poly64_t";
2185 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00002186 default: llvm_unreachable("unexpected Neon polynomial vector element type");
2187 }
2188 } else {
2189 switch (cast<BuiltinType>(EltType)->getKind()) {
2190 case BuiltinType::SChar: EltName = "int8_t"; break;
2191 case BuiltinType::UChar: EltName = "uint8_t"; break;
2192 case BuiltinType::Short: EltName = "int16_t"; break;
2193 case BuiltinType::UShort: EltName = "uint16_t"; break;
2194 case BuiltinType::Int: EltName = "int32_t"; break;
2195 case BuiltinType::UInt: EltName = "uint32_t"; break;
2196 case BuiltinType::LongLong: EltName = "int64_t"; break;
2197 case BuiltinType::ULongLong: EltName = "uint64_t"; break;
Tim Northovera2ee4332014-03-29 15:09:45 +00002198 case BuiltinType::Double: EltName = "float64_t"; break;
Guy Benyei11169dd2012-12-18 14:30:41 +00002199 case BuiltinType::Float: EltName = "float32_t"; break;
Tim Northover2fe823a2013-08-01 09:23:19 +00002200 case BuiltinType::Half: EltName = "float16_t";break;
2201 default:
2202 llvm_unreachable("unexpected Neon vector element type");
Guy Benyei11169dd2012-12-18 14:30:41 +00002203 }
2204 }
2205 const char *BaseName = 0;
2206 unsigned BitSize = (T->getNumElements() *
2207 getASTContext().getTypeSize(EltType));
2208 if (BitSize == 64)
2209 BaseName = "__simd64_";
2210 else {
2211 assert(BitSize == 128 && "Neon vector type not 64 or 128 bits");
2212 BaseName = "__simd128_";
2213 }
2214 Out << strlen(BaseName) + strlen(EltName);
2215 Out << BaseName << EltName;
2216}
2217
Tim Northover2fe823a2013-08-01 09:23:19 +00002218static StringRef mangleAArch64VectorBase(const BuiltinType *EltType) {
2219 switch (EltType->getKind()) {
2220 case BuiltinType::SChar:
2221 return "Int8";
2222 case BuiltinType::Short:
2223 return "Int16";
2224 case BuiltinType::Int:
2225 return "Int32";
Kevin Qinad64f6d2014-02-24 02:45:03 +00002226 case BuiltinType::Long:
Tim Northovera2ee4332014-03-29 15:09:45 +00002227 case BuiltinType::LongLong:
Tim Northover2fe823a2013-08-01 09:23:19 +00002228 return "Int64";
2229 case BuiltinType::UChar:
2230 return "Uint8";
2231 case BuiltinType::UShort:
2232 return "Uint16";
2233 case BuiltinType::UInt:
2234 return "Uint32";
Kevin Qinad64f6d2014-02-24 02:45:03 +00002235 case BuiltinType::ULong:
Tim Northovera2ee4332014-03-29 15:09:45 +00002236 case BuiltinType::ULongLong:
Tim Northover2fe823a2013-08-01 09:23:19 +00002237 return "Uint64";
2238 case BuiltinType::Half:
2239 return "Float16";
2240 case BuiltinType::Float:
2241 return "Float32";
2242 case BuiltinType::Double:
2243 return "Float64";
2244 default:
2245 llvm_unreachable("Unexpected vector element base type");
2246 }
2247}
2248
2249// AArch64's ABI for Neon vector types specifies that they should be mangled as
2250// the equivalent internal name. The vector type must be one of the special
2251// types predefined by ARM.
2252void CXXNameMangler::mangleAArch64NeonVectorType(const VectorType *T) {
2253 QualType EltType = T->getElementType();
2254 assert(EltType->isBuiltinType() && "Neon vector element not a BuiltinType");
2255 unsigned BitSize =
2256 (T->getNumElements() * getASTContext().getTypeSize(EltType));
Daniel Jasper8698af42013-08-01 10:30:11 +00002257 (void)BitSize; // Silence warning.
Tim Northover2fe823a2013-08-01 09:23:19 +00002258
2259 assert((BitSize == 64 || BitSize == 128) &&
2260 "Neon vector type not 64 or 128 bits");
2261
Tim Northover2fe823a2013-08-01 09:23:19 +00002262 StringRef EltName;
2263 if (T->getVectorKind() == VectorType::NeonPolyVector) {
2264 switch (cast<BuiltinType>(EltType)->getKind()) {
2265 case BuiltinType::UChar:
2266 EltName = "Poly8";
2267 break;
2268 case BuiltinType::UShort:
2269 EltName = "Poly16";
2270 break;
Kevin Qinad64f6d2014-02-24 02:45:03 +00002271 case BuiltinType::ULong:
Hao Liu90ee2f12013-11-17 09:14:46 +00002272 EltName = "Poly64";
2273 break;
Tim Northover2fe823a2013-08-01 09:23:19 +00002274 default:
2275 llvm_unreachable("unexpected Neon polynomial vector element type");
2276 }
2277 } else
2278 EltName = mangleAArch64VectorBase(cast<BuiltinType>(EltType));
2279
2280 std::string TypeName =
2281 ("__" + EltName + "x" + llvm::utostr(T->getNumElements()) + "_t").str();
2282 Out << TypeName.length() << TypeName;
2283}
2284
Guy Benyei11169dd2012-12-18 14:30:41 +00002285// GNU extension: vector types
2286// <type> ::= <vector-type>
2287// <vector-type> ::= Dv <positive dimension number> _
2288// <extended element type>
2289// ::= Dv [<dimension expression>] _ <element type>
2290// <extended element type> ::= <element type>
2291// ::= p # AltiVec vector pixel
2292// ::= b # Altivec vector bool
2293void CXXNameMangler::mangleType(const VectorType *T) {
2294 if ((T->getVectorKind() == VectorType::NeonVector ||
2295 T->getVectorKind() == VectorType::NeonPolyVector)) {
Tim Northovera2ee4332014-03-29 15:09:45 +00002296 llvm::Triple Target = getASTContext().getTargetInfo().getTriple();
Christian Pirker9b019ae2014-02-25 13:51:00 +00002297 llvm::Triple::ArchType Arch =
2298 getASTContext().getTargetInfo().getTriple().getArch();
Tim Northovera2ee4332014-03-29 15:09:45 +00002299 if (Arch == llvm::Triple::aarch64 ||
2300 Arch == llvm::Triple::aarch64_be ||
2301 (Arch == llvm::Triple::arm64 && !Target.isOSDarwin()))
Tim Northover2fe823a2013-08-01 09:23:19 +00002302 mangleAArch64NeonVectorType(T);
2303 else
2304 mangleNeonVectorType(T);
Guy Benyei11169dd2012-12-18 14:30:41 +00002305 return;
2306 }
2307 Out << "Dv" << T->getNumElements() << '_';
2308 if (T->getVectorKind() == VectorType::AltiVecPixel)
2309 Out << 'p';
2310 else if (T->getVectorKind() == VectorType::AltiVecBool)
2311 Out << 'b';
2312 else
2313 mangleType(T->getElementType());
2314}
2315void CXXNameMangler::mangleType(const ExtVectorType *T) {
2316 mangleType(static_cast<const VectorType*>(T));
2317}
2318void CXXNameMangler::mangleType(const DependentSizedExtVectorType *T) {
2319 Out << "Dv";
2320 mangleExpression(T->getSizeExpr());
2321 Out << '_';
2322 mangleType(T->getElementType());
2323}
2324
2325void CXXNameMangler::mangleType(const PackExpansionType *T) {
2326 // <type> ::= Dp <type> # pack expansion (C++0x)
2327 Out << "Dp";
2328 mangleType(T->getPattern());
2329}
2330
2331void CXXNameMangler::mangleType(const ObjCInterfaceType *T) {
2332 mangleSourceName(T->getDecl()->getIdentifier());
2333}
2334
2335void CXXNameMangler::mangleType(const ObjCObjectType *T) {
Eli Friedman5f508952013-06-18 22:41:37 +00002336 if (!T->qual_empty()) {
2337 // Mangle protocol qualifiers.
2338 SmallString<64> QualStr;
2339 llvm::raw_svector_ostream QualOS(QualStr);
2340 QualOS << "objcproto";
Aaron Ballman1683f7b2014-03-17 15:55:30 +00002341 for (const auto *I : T->quals()) {
2342 StringRef name = I->getName();
Eli Friedman5f508952013-06-18 22:41:37 +00002343 QualOS << name.size() << name;
2344 }
2345 QualOS.flush();
2346 Out << 'U' << QualStr.size() << QualStr;
2347 }
Guy Benyei11169dd2012-12-18 14:30:41 +00002348 mangleType(T->getBaseType());
2349}
2350
2351void CXXNameMangler::mangleType(const BlockPointerType *T) {
2352 Out << "U13block_pointer";
2353 mangleType(T->getPointeeType());
2354}
2355
2356void CXXNameMangler::mangleType(const InjectedClassNameType *T) {
2357 // Mangle injected class name types as if the user had written the
2358 // specialization out fully. It may not actually be possible to see
2359 // this mangling, though.
2360 mangleType(T->getInjectedSpecializationType());
2361}
2362
2363void CXXNameMangler::mangleType(const TemplateSpecializationType *T) {
2364 if (TemplateDecl *TD = T->getTemplateName().getAsTemplateDecl()) {
2365 mangleName(TD, T->getArgs(), T->getNumArgs());
2366 } else {
2367 if (mangleSubstitution(QualType(T, 0)))
2368 return;
2369
2370 mangleTemplatePrefix(T->getTemplateName());
2371
2372 // FIXME: GCC does not appear to mangle the template arguments when
2373 // the template in question is a dependent template name. Should we
2374 // emulate that badness?
2375 mangleTemplateArgs(T->getArgs(), T->getNumArgs());
2376 addSubstitution(QualType(T, 0));
2377 }
2378}
2379
2380void CXXNameMangler::mangleType(const DependentNameType *T) {
David Majnemer64e40c52014-04-10 00:49:24 +00002381 // Proposal by cxx-abi-dev, 2014-03-26
2382 // <class-enum-type> ::= <name> # non-dependent or dependent type name or
2383 // # dependent elaborated type specifier using
David Majnemer61182a82014-04-10 00:59:44 +00002384 // # 'typename'
David Majnemer64e40c52014-04-10 00:49:24 +00002385 // ::= Ts <name> # dependent elaborated type specifier using
David Majnemer61182a82014-04-10 00:59:44 +00002386 // # 'struct' or 'class'
David Majnemer64e40c52014-04-10 00:49:24 +00002387 // ::= Tu <name> # dependent elaborated type specifier using
David Majnemer61182a82014-04-10 00:59:44 +00002388 // # 'union'
David Majnemer64e40c52014-04-10 00:49:24 +00002389 // ::= Te <name> # dependent elaborated type specifier using
David Majnemer61182a82014-04-10 00:59:44 +00002390 // # 'enum'
David Majnemer64e40c52014-04-10 00:49:24 +00002391 switch (T->getKeyword()) {
2392 case ETK_Typename:
2393 break;
2394 case ETK_Struct:
2395 case ETK_Class:
2396 case ETK_Interface:
2397 Out << "Ts";
2398 break;
2399 case ETK_Union:
2400 Out << "Tu";
2401 break;
2402 case ETK_Enum:
2403 Out << "Te";
2404 break;
2405 default:
2406 llvm_unreachable("unexpected keyword for dependent type name");
2407 }
David Majnemer2e159fb2014-04-15 05:51:25 +00002408 // Typename types are always nested
2409 Out << 'N';
Guy Benyei11169dd2012-12-18 14:30:41 +00002410 manglePrefix(T->getQualifier());
David Majnemer64e40c52014-04-10 00:49:24 +00002411 mangleSourceName(T->getIdentifier());
Guy Benyei11169dd2012-12-18 14:30:41 +00002412 Out << 'E';
2413}
2414
2415void CXXNameMangler::mangleType(const DependentTemplateSpecializationType *T) {
2416 // Dependently-scoped template types are nested if they have a prefix.
2417 Out << 'N';
2418
2419 // TODO: avoid making this TemplateName.
2420 TemplateName Prefix =
2421 getASTContext().getDependentTemplateName(T->getQualifier(),
2422 T->getIdentifier());
2423 mangleTemplatePrefix(Prefix);
2424
2425 // FIXME: GCC does not appear to mangle the template arguments when
2426 // the template in question is a dependent template name. Should we
2427 // emulate that badness?
2428 mangleTemplateArgs(T->getArgs(), T->getNumArgs());
2429 Out << 'E';
2430}
2431
2432void CXXNameMangler::mangleType(const TypeOfType *T) {
2433 // FIXME: this is pretty unsatisfactory, but there isn't an obvious
2434 // "extension with parameters" mangling.
2435 Out << "u6typeof";
2436}
2437
2438void CXXNameMangler::mangleType(const TypeOfExprType *T) {
2439 // FIXME: this is pretty unsatisfactory, but there isn't an obvious
2440 // "extension with parameters" mangling.
2441 Out << "u6typeof";
2442}
2443
2444void CXXNameMangler::mangleType(const DecltypeType *T) {
2445 Expr *E = T->getUnderlyingExpr();
2446
2447 // type ::= Dt <expression> E # decltype of an id-expression
2448 // # or class member access
2449 // ::= DT <expression> E # decltype of an expression
2450
2451 // This purports to be an exhaustive list of id-expressions and
2452 // class member accesses. Note that we do not ignore parentheses;
2453 // parentheses change the semantics of decltype for these
2454 // expressions (and cause the mangler to use the other form).
2455 if (isa<DeclRefExpr>(E) ||
2456 isa<MemberExpr>(E) ||
2457 isa<UnresolvedLookupExpr>(E) ||
2458 isa<DependentScopeDeclRefExpr>(E) ||
2459 isa<CXXDependentScopeMemberExpr>(E) ||
2460 isa<UnresolvedMemberExpr>(E))
2461 Out << "Dt";
2462 else
2463 Out << "DT";
2464 mangleExpression(E);
2465 Out << 'E';
2466}
2467
2468void CXXNameMangler::mangleType(const UnaryTransformType *T) {
2469 // If this is dependent, we need to record that. If not, we simply
2470 // mangle it as the underlying type since they are equivalent.
2471 if (T->isDependentType()) {
2472 Out << 'U';
2473
2474 switch (T->getUTTKind()) {
2475 case UnaryTransformType::EnumUnderlyingType:
2476 Out << "3eut";
2477 break;
2478 }
2479 }
2480
2481 mangleType(T->getUnderlyingType());
2482}
2483
2484void CXXNameMangler::mangleType(const AutoType *T) {
2485 QualType D = T->getDeducedType();
2486 // <builtin-type> ::= Da # dependent auto
2487 if (D.isNull())
Richard Smith74aeef52013-04-26 16:15:35 +00002488 Out << (T->isDecltypeAuto() ? "Dc" : "Da");
Guy Benyei11169dd2012-12-18 14:30:41 +00002489 else
2490 mangleType(D);
2491}
2492
2493void CXXNameMangler::mangleType(const AtomicType *T) {
Nick Lewycky206cc2d2014-03-09 17:09:28 +00002494 // <type> ::= U <source-name> <type> # vendor extended type qualifier
Guy Benyei11169dd2012-12-18 14:30:41 +00002495 // (Until there's a standardized mangling...)
2496 Out << "U7_Atomic";
2497 mangleType(T->getValueType());
2498}
2499
2500void CXXNameMangler::mangleIntegerLiteral(QualType T,
2501 const llvm::APSInt &Value) {
2502 // <expr-primary> ::= L <type> <value number> E # integer literal
2503 Out << 'L';
2504
2505 mangleType(T);
2506 if (T->isBooleanType()) {
2507 // Boolean values are encoded as 0/1.
2508 Out << (Value.getBoolValue() ? '1' : '0');
2509 } else {
2510 mangleNumber(Value);
2511 }
2512 Out << 'E';
2513
2514}
2515
2516/// Mangles a member expression.
2517void CXXNameMangler::mangleMemberExpr(const Expr *base,
2518 bool isArrow,
2519 NestedNameSpecifier *qualifier,
2520 NamedDecl *firstQualifierLookup,
2521 DeclarationName member,
2522 unsigned arity) {
2523 // <expression> ::= dt <expression> <unresolved-name>
2524 // ::= pt <expression> <unresolved-name>
2525 if (base) {
2526 if (base->isImplicitCXXThis()) {
2527 // Note: GCC mangles member expressions to the implicit 'this' as
2528 // *this., whereas we represent them as this->. The Itanium C++ ABI
2529 // does not specify anything here, so we follow GCC.
2530 Out << "dtdefpT";
2531 } else {
2532 Out << (isArrow ? "pt" : "dt");
2533 mangleExpression(base);
2534 }
2535 }
2536 mangleUnresolvedName(qualifier, firstQualifierLookup, member, arity);
2537}
2538
2539/// Look at the callee of the given call expression and determine if
2540/// it's a parenthesized id-expression which would have triggered ADL
2541/// otherwise.
2542static bool isParenthesizedADLCallee(const CallExpr *call) {
2543 const Expr *callee = call->getCallee();
2544 const Expr *fn = callee->IgnoreParens();
2545
2546 // Must be parenthesized. IgnoreParens() skips __extension__ nodes,
2547 // too, but for those to appear in the callee, it would have to be
2548 // parenthesized.
2549 if (callee == fn) return false;
2550
2551 // Must be an unresolved lookup.
2552 const UnresolvedLookupExpr *lookup = dyn_cast<UnresolvedLookupExpr>(fn);
2553 if (!lookup) return false;
2554
2555 assert(!lookup->requiresADL());
2556
2557 // Must be an unqualified lookup.
2558 if (lookup->getQualifier()) return false;
2559
2560 // Must not have found a class member. Note that if one is a class
2561 // member, they're all class members.
2562 if (lookup->getNumDecls() > 0 &&
2563 (*lookup->decls_begin())->isCXXClassMember())
2564 return false;
2565
2566 // Otherwise, ADL would have been triggered.
2567 return true;
2568}
2569
2570void CXXNameMangler::mangleExpression(const Expr *E, unsigned Arity) {
2571 // <expression> ::= <unary operator-name> <expression>
2572 // ::= <binary operator-name> <expression> <expression>
2573 // ::= <trinary operator-name> <expression> <expression> <expression>
2574 // ::= cv <type> expression # conversion with one argument
2575 // ::= cv <type> _ <expression>* E # conversion with a different number of arguments
2576 // ::= st <type> # sizeof (a type)
2577 // ::= at <type> # alignof (a type)
2578 // ::= <template-param>
2579 // ::= <function-param>
2580 // ::= sr <type> <unqualified-name> # dependent name
2581 // ::= sr <type> <unqualified-name> <template-args> # dependent template-id
2582 // ::= ds <expression> <expression> # expr.*expr
2583 // ::= sZ <template-param> # size of a parameter pack
2584 // ::= sZ <function-param> # size of a function parameter pack
2585 // ::= <expr-primary>
2586 // <expr-primary> ::= L <type> <value number> E # integer literal
2587 // ::= L <type <value float> E # floating literal
2588 // ::= L <mangled-name> E # external name
2589 // ::= fpT # 'this' expression
2590 QualType ImplicitlyConvertedToType;
2591
2592recurse:
2593 switch (E->getStmtClass()) {
2594 case Expr::NoStmtClass:
2595#define ABSTRACT_STMT(Type)
2596#define EXPR(Type, Base)
2597#define STMT(Type, Base) \
2598 case Expr::Type##Class:
2599#include "clang/AST/StmtNodes.inc"
2600 // fallthrough
2601
2602 // These all can only appear in local or variable-initialization
2603 // contexts and so should never appear in a mangling.
2604 case Expr::AddrLabelExprClass:
2605 case Expr::DesignatedInitExprClass:
2606 case Expr::ImplicitValueInitExprClass:
2607 case Expr::ParenListExprClass:
2608 case Expr::LambdaExprClass:
John McCall5e77d762013-04-16 07:28:30 +00002609 case Expr::MSPropertyRefExprClass:
Guy Benyei11169dd2012-12-18 14:30:41 +00002610 llvm_unreachable("unexpected statement kind");
2611
2612 // FIXME: invent manglings for all these.
2613 case Expr::BlockExprClass:
2614 case Expr::CXXPseudoDestructorExprClass:
2615 case Expr::ChooseExprClass:
2616 case Expr::CompoundLiteralExprClass:
2617 case Expr::ExtVectorElementExprClass:
2618 case Expr::GenericSelectionExprClass:
2619 case Expr::ObjCEncodeExprClass:
2620 case Expr::ObjCIsaExprClass:
2621 case Expr::ObjCIvarRefExprClass:
2622 case Expr::ObjCMessageExprClass:
2623 case Expr::ObjCPropertyRefExprClass:
2624 case Expr::ObjCProtocolExprClass:
2625 case Expr::ObjCSelectorExprClass:
2626 case Expr::ObjCStringLiteralClass:
2627 case Expr::ObjCBoxedExprClass:
2628 case Expr::ObjCArrayLiteralClass:
2629 case Expr::ObjCDictionaryLiteralClass:
2630 case Expr::ObjCSubscriptRefExprClass:
2631 case Expr::ObjCIndirectCopyRestoreExprClass:
2632 case Expr::OffsetOfExprClass:
2633 case Expr::PredefinedExprClass:
2634 case Expr::ShuffleVectorExprClass:
Hal Finkelc4d7c822013-09-18 03:29:45 +00002635 case Expr::ConvertVectorExprClass:
Guy Benyei11169dd2012-12-18 14:30:41 +00002636 case Expr::StmtExprClass:
Guy Benyei11169dd2012-12-18 14:30:41 +00002637 case Expr::TypeTraitExprClass:
2638 case Expr::ArrayTypeTraitExprClass:
2639 case Expr::ExpressionTraitExprClass:
2640 case Expr::VAArgExprClass:
2641 case Expr::CXXUuidofExprClass:
2642 case Expr::CUDAKernelCallExprClass:
2643 case Expr::AsTypeExprClass:
2644 case Expr::PseudoObjectExprClass:
2645 case Expr::AtomicExprClass:
2646 {
2647 // As bad as this diagnostic is, it's better than crashing.
2648 DiagnosticsEngine &Diags = Context.getDiags();
2649 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
2650 "cannot yet mangle expression type %0");
2651 Diags.Report(E->getExprLoc(), DiagID)
2652 << E->getStmtClassName() << E->getSourceRange();
2653 break;
2654 }
2655
2656 // Even gcc-4.5 doesn't mangle this.
2657 case Expr::BinaryConditionalOperatorClass: {
2658 DiagnosticsEngine &Diags = Context.getDiags();
2659 unsigned DiagID =
2660 Diags.getCustomDiagID(DiagnosticsEngine::Error,
2661 "?: operator with omitted middle operand cannot be mangled");
2662 Diags.Report(E->getExprLoc(), DiagID)
2663 << E->getStmtClassName() << E->getSourceRange();
2664 break;
2665 }
2666
2667 // These are used for internal purposes and cannot be meaningfully mangled.
2668 case Expr::OpaqueValueExprClass:
2669 llvm_unreachable("cannot mangle opaque value; mangling wrong thing?");
2670
2671 case Expr::InitListExprClass: {
2672 // Proposal by Jason Merrill, 2012-01-03
2673 Out << "il";
2674 const InitListExpr *InitList = cast<InitListExpr>(E);
2675 for (unsigned i = 0, e = InitList->getNumInits(); i != e; ++i)
2676 mangleExpression(InitList->getInit(i));
2677 Out << "E";
2678 break;
2679 }
2680
2681 case Expr::CXXDefaultArgExprClass:
2682 mangleExpression(cast<CXXDefaultArgExpr>(E)->getExpr(), Arity);
2683 break;
2684
Richard Smith852c9db2013-04-20 22:23:05 +00002685 case Expr::CXXDefaultInitExprClass:
2686 mangleExpression(cast<CXXDefaultInitExpr>(E)->getExpr(), Arity);
2687 break;
2688
Richard Smithcc1b96d2013-06-12 22:31:48 +00002689 case Expr::CXXStdInitializerListExprClass:
2690 mangleExpression(cast<CXXStdInitializerListExpr>(E)->getSubExpr(), Arity);
2691 break;
2692
Guy Benyei11169dd2012-12-18 14:30:41 +00002693 case Expr::SubstNonTypeTemplateParmExprClass:
2694 mangleExpression(cast<SubstNonTypeTemplateParmExpr>(E)->getReplacement(),
2695 Arity);
2696 break;
2697
2698 case Expr::UserDefinedLiteralClass:
2699 // We follow g++'s approach of mangling a UDL as a call to the literal
2700 // operator.
2701 case Expr::CXXMemberCallExprClass: // fallthrough
2702 case Expr::CallExprClass: {
2703 const CallExpr *CE = cast<CallExpr>(E);
2704
2705 // <expression> ::= cp <simple-id> <expression>* E
2706 // We use this mangling only when the call would use ADL except
2707 // for being parenthesized. Per discussion with David
2708 // Vandervoorde, 2011.04.25.
2709 if (isParenthesizedADLCallee(CE)) {
2710 Out << "cp";
2711 // The callee here is a parenthesized UnresolvedLookupExpr with
2712 // no qualifier and should always get mangled as a <simple-id>
2713 // anyway.
2714
2715 // <expression> ::= cl <expression>* E
2716 } else {
2717 Out << "cl";
2718 }
2719
2720 mangleExpression(CE->getCallee(), CE->getNumArgs());
2721 for (unsigned I = 0, N = CE->getNumArgs(); I != N; ++I)
2722 mangleExpression(CE->getArg(I));
2723 Out << 'E';
2724 break;
2725 }
2726
2727 case Expr::CXXNewExprClass: {
2728 const CXXNewExpr *New = cast<CXXNewExpr>(E);
2729 if (New->isGlobalNew()) Out << "gs";
2730 Out << (New->isArray() ? "na" : "nw");
2731 for (CXXNewExpr::const_arg_iterator I = New->placement_arg_begin(),
2732 E = New->placement_arg_end(); I != E; ++I)
2733 mangleExpression(*I);
2734 Out << '_';
2735 mangleType(New->getAllocatedType());
2736 if (New->hasInitializer()) {
2737 // Proposal by Jason Merrill, 2012-01-03
2738 if (New->getInitializationStyle() == CXXNewExpr::ListInit)
2739 Out << "il";
2740 else
2741 Out << "pi";
2742 const Expr *Init = New->getInitializer();
2743 if (const CXXConstructExpr *CCE = dyn_cast<CXXConstructExpr>(Init)) {
2744 // Directly inline the initializers.
2745 for (CXXConstructExpr::const_arg_iterator I = CCE->arg_begin(),
2746 E = CCE->arg_end();
2747 I != E; ++I)
2748 mangleExpression(*I);
2749 } else if (const ParenListExpr *PLE = dyn_cast<ParenListExpr>(Init)) {
2750 for (unsigned i = 0, e = PLE->getNumExprs(); i != e; ++i)
2751 mangleExpression(PLE->getExpr(i));
2752 } else if (New->getInitializationStyle() == CXXNewExpr::ListInit &&
2753 isa<InitListExpr>(Init)) {
2754 // Only take InitListExprs apart for list-initialization.
2755 const InitListExpr *InitList = cast<InitListExpr>(Init);
2756 for (unsigned i = 0, e = InitList->getNumInits(); i != e; ++i)
2757 mangleExpression(InitList->getInit(i));
2758 } else
2759 mangleExpression(Init);
2760 }
2761 Out << 'E';
2762 break;
2763 }
2764
2765 case Expr::MemberExprClass: {
2766 const MemberExpr *ME = cast<MemberExpr>(E);
2767 mangleMemberExpr(ME->getBase(), ME->isArrow(),
2768 ME->getQualifier(), 0, ME->getMemberDecl()->getDeclName(),
2769 Arity);
2770 break;
2771 }
2772
2773 case Expr::UnresolvedMemberExprClass: {
2774 const UnresolvedMemberExpr *ME = cast<UnresolvedMemberExpr>(E);
2775 mangleMemberExpr(ME->getBase(), ME->isArrow(),
2776 ME->getQualifier(), 0, ME->getMemberName(),
2777 Arity);
2778 if (ME->hasExplicitTemplateArgs())
2779 mangleTemplateArgs(ME->getExplicitTemplateArgs());
2780 break;
2781 }
2782
2783 case Expr::CXXDependentScopeMemberExprClass: {
2784 const CXXDependentScopeMemberExpr *ME
2785 = cast<CXXDependentScopeMemberExpr>(E);
2786 mangleMemberExpr(ME->getBase(), ME->isArrow(),
2787 ME->getQualifier(), ME->getFirstQualifierFoundInScope(),
2788 ME->getMember(), Arity);
2789 if (ME->hasExplicitTemplateArgs())
2790 mangleTemplateArgs(ME->getExplicitTemplateArgs());
2791 break;
2792 }
2793
2794 case Expr::UnresolvedLookupExprClass: {
2795 const UnresolvedLookupExpr *ULE = cast<UnresolvedLookupExpr>(E);
2796 mangleUnresolvedName(ULE->getQualifier(), 0, ULE->getName(), Arity);
2797
2798 // All the <unresolved-name> productions end in a
2799 // base-unresolved-name, where <template-args> are just tacked
2800 // onto the end.
2801 if (ULE->hasExplicitTemplateArgs())
2802 mangleTemplateArgs(ULE->getExplicitTemplateArgs());
2803 break;
2804 }
2805
2806 case Expr::CXXUnresolvedConstructExprClass: {
2807 const CXXUnresolvedConstructExpr *CE = cast<CXXUnresolvedConstructExpr>(E);
2808 unsigned N = CE->arg_size();
2809
2810 Out << "cv";
2811 mangleType(CE->getType());
2812 if (N != 1) Out << '_';
2813 for (unsigned I = 0; I != N; ++I) mangleExpression(CE->getArg(I));
2814 if (N != 1) Out << 'E';
2815 break;
2816 }
2817
2818 case Expr::CXXTemporaryObjectExprClass:
2819 case Expr::CXXConstructExprClass: {
2820 const CXXConstructExpr *CE = cast<CXXConstructExpr>(E);
2821 unsigned N = CE->getNumArgs();
2822
2823 // Proposal by Jason Merrill, 2012-01-03
2824 if (CE->isListInitialization())
2825 Out << "tl";
2826 else
2827 Out << "cv";
2828 mangleType(CE->getType());
2829 if (N != 1) Out << '_';
2830 for (unsigned I = 0; I != N; ++I) mangleExpression(CE->getArg(I));
2831 if (N != 1) Out << 'E';
2832 break;
2833 }
2834
2835 case Expr::CXXScalarValueInitExprClass:
2836 Out <<"cv";
2837 mangleType(E->getType());
2838 Out <<"_E";
2839 break;
2840
2841 case Expr::CXXNoexceptExprClass:
2842 Out << "nx";
2843 mangleExpression(cast<CXXNoexceptExpr>(E)->getOperand());
2844 break;
2845
2846 case Expr::UnaryExprOrTypeTraitExprClass: {
2847 const UnaryExprOrTypeTraitExpr *SAE = cast<UnaryExprOrTypeTraitExpr>(E);
2848
2849 if (!SAE->isInstantiationDependent()) {
2850 // Itanium C++ ABI:
2851 // If the operand of a sizeof or alignof operator is not
2852 // instantiation-dependent it is encoded as an integer literal
2853 // reflecting the result of the operator.
2854 //
2855 // If the result of the operator is implicitly converted to a known
2856 // integer type, that type is used for the literal; otherwise, the type
2857 // of std::size_t or std::ptrdiff_t is used.
2858 QualType T = (ImplicitlyConvertedToType.isNull() ||
2859 !ImplicitlyConvertedToType->isIntegerType())? SAE->getType()
2860 : ImplicitlyConvertedToType;
2861 llvm::APSInt V = SAE->EvaluateKnownConstInt(Context.getASTContext());
2862 mangleIntegerLiteral(T, V);
2863 break;
2864 }
2865
2866 switch(SAE->getKind()) {
2867 case UETT_SizeOf:
2868 Out << 's';
2869 break;
2870 case UETT_AlignOf:
2871 Out << 'a';
2872 break;
2873 case UETT_VecStep:
2874 DiagnosticsEngine &Diags = Context.getDiags();
2875 unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
2876 "cannot yet mangle vec_step expression");
2877 Diags.Report(DiagID);
2878 return;
2879 }
2880 if (SAE->isArgumentType()) {
2881 Out << 't';
2882 mangleType(SAE->getArgumentType());
2883 } else {
2884 Out << 'z';
2885 mangleExpression(SAE->getArgumentExpr());
2886 }
2887 break;
2888 }
2889
2890 case Expr::CXXThrowExprClass: {
2891 const CXXThrowExpr *TE = cast<CXXThrowExpr>(E);
Richard Smitheb0133c2013-08-27 01:03:46 +00002892 // <expression> ::= tw <expression> # throw expression
2893 // ::= tr # rethrow
Guy Benyei11169dd2012-12-18 14:30:41 +00002894 if (TE->getSubExpr()) {
2895 Out << "tw";
2896 mangleExpression(TE->getSubExpr());
2897 } else {
2898 Out << "tr";
2899 }
2900 break;
2901 }
2902
2903 case Expr::CXXTypeidExprClass: {
2904 const CXXTypeidExpr *TIE = cast<CXXTypeidExpr>(E);
Richard Smitheb0133c2013-08-27 01:03:46 +00002905 // <expression> ::= ti <type> # typeid (type)
2906 // ::= te <expression> # typeid (expression)
Guy Benyei11169dd2012-12-18 14:30:41 +00002907 if (TIE->isTypeOperand()) {
2908 Out << "ti";
David Majnemer143c55e2013-09-27 07:04:31 +00002909 mangleType(TIE->getTypeOperand(Context.getASTContext()));
Guy Benyei11169dd2012-12-18 14:30:41 +00002910 } else {
2911 Out << "te";
2912 mangleExpression(TIE->getExprOperand());
2913 }
2914 break;
2915 }
2916
2917 case Expr::CXXDeleteExprClass: {
2918 const CXXDeleteExpr *DE = cast<CXXDeleteExpr>(E);
Richard Smitheb0133c2013-08-27 01:03:46 +00002919 // <expression> ::= [gs] dl <expression> # [::] delete expr
2920 // ::= [gs] da <expression> # [::] delete [] expr
Guy Benyei11169dd2012-12-18 14:30:41 +00002921 if (DE->isGlobalDelete()) Out << "gs";
2922 Out << (DE->isArrayForm() ? "da" : "dl");
2923 mangleExpression(DE->getArgument());
2924 break;
2925 }
2926
2927 case Expr::UnaryOperatorClass: {
2928 const UnaryOperator *UO = cast<UnaryOperator>(E);
2929 mangleOperatorName(UnaryOperator::getOverloadedOperator(UO->getOpcode()),
2930 /*Arity=*/1);
2931 mangleExpression(UO->getSubExpr());
2932 break;
2933 }
2934
2935 case Expr::ArraySubscriptExprClass: {
2936 const ArraySubscriptExpr *AE = cast<ArraySubscriptExpr>(E);
2937
2938 // Array subscript is treated as a syntactically weird form of
2939 // binary operator.
2940 Out << "ix";
2941 mangleExpression(AE->getLHS());
2942 mangleExpression(AE->getRHS());
2943 break;
2944 }
2945
2946 case Expr::CompoundAssignOperatorClass: // fallthrough
2947 case Expr::BinaryOperatorClass: {
2948 const BinaryOperator *BO = cast<BinaryOperator>(E);
2949 if (BO->getOpcode() == BO_PtrMemD)
2950 Out << "ds";
2951 else
2952 mangleOperatorName(BinaryOperator::getOverloadedOperator(BO->getOpcode()),
2953 /*Arity=*/2);
2954 mangleExpression(BO->getLHS());
2955 mangleExpression(BO->getRHS());
2956 break;
2957 }
2958
2959 case Expr::ConditionalOperatorClass: {
2960 const ConditionalOperator *CO = cast<ConditionalOperator>(E);
2961 mangleOperatorName(OO_Conditional, /*Arity=*/3);
2962 mangleExpression(CO->getCond());
2963 mangleExpression(CO->getLHS(), Arity);
2964 mangleExpression(CO->getRHS(), Arity);
2965 break;
2966 }
2967
2968 case Expr::ImplicitCastExprClass: {
2969 ImplicitlyConvertedToType = E->getType();
2970 E = cast<ImplicitCastExpr>(E)->getSubExpr();
2971 goto recurse;
2972 }
2973
2974 case Expr::ObjCBridgedCastExprClass: {
2975 // Mangle ownership casts as a vendor extended operator __bridge,
2976 // __bridge_transfer, or __bridge_retain.
2977 StringRef Kind = cast<ObjCBridgedCastExpr>(E)->getBridgeKindName();
2978 Out << "v1U" << Kind.size() << Kind;
2979 }
2980 // Fall through to mangle the cast itself.
2981
2982 case Expr::CStyleCastExprClass:
2983 case Expr::CXXStaticCastExprClass:
2984 case Expr::CXXDynamicCastExprClass:
2985 case Expr::CXXReinterpretCastExprClass:
2986 case Expr::CXXConstCastExprClass:
2987 case Expr::CXXFunctionalCastExprClass: {
2988 const ExplicitCastExpr *ECE = cast<ExplicitCastExpr>(E);
2989 Out << "cv";
2990 mangleType(ECE->getType());
2991 mangleExpression(ECE->getSubExpr());
2992 break;
2993 }
2994
2995 case Expr::CXXOperatorCallExprClass: {
2996 const CXXOperatorCallExpr *CE = cast<CXXOperatorCallExpr>(E);
2997 unsigned NumArgs = CE->getNumArgs();
2998 mangleOperatorName(CE->getOperator(), /*Arity=*/NumArgs);
2999 // Mangle the arguments.
3000 for (unsigned i = 0; i != NumArgs; ++i)
3001 mangleExpression(CE->getArg(i));
3002 break;
3003 }
3004
3005 case Expr::ParenExprClass:
3006 mangleExpression(cast<ParenExpr>(E)->getSubExpr(), Arity);
3007 break;
3008
3009 case Expr::DeclRefExprClass: {
3010 const NamedDecl *D = cast<DeclRefExpr>(E)->getDecl();
3011
3012 switch (D->getKind()) {
3013 default:
3014 // <expr-primary> ::= L <mangled-name> E # external name
3015 Out << 'L';
3016 mangle(D, "_Z");
3017 Out << 'E';
3018 break;
3019
3020 case Decl::ParmVar:
3021 mangleFunctionParam(cast<ParmVarDecl>(D));
3022 break;
3023
3024 case Decl::EnumConstant: {
3025 const EnumConstantDecl *ED = cast<EnumConstantDecl>(D);
3026 mangleIntegerLiteral(ED->getType(), ED->getInitVal());
3027 break;
3028 }
3029
3030 case Decl::NonTypeTemplateParm: {
3031 const NonTypeTemplateParmDecl *PD = cast<NonTypeTemplateParmDecl>(D);
3032 mangleTemplateParameter(PD->getIndex());
3033 break;
3034 }
3035
3036 }
3037
3038 break;
3039 }
3040
3041 case Expr::SubstNonTypeTemplateParmPackExprClass:
3042 // FIXME: not clear how to mangle this!
3043 // template <unsigned N...> class A {
3044 // template <class U...> void foo(U (&x)[N]...);
3045 // };
3046 Out << "_SUBSTPACK_";
3047 break;
3048
3049 case Expr::FunctionParmPackExprClass: {
3050 // FIXME: not clear how to mangle this!
3051 const FunctionParmPackExpr *FPPE = cast<FunctionParmPackExpr>(E);
3052 Out << "v110_SUBSTPACK";
3053 mangleFunctionParam(FPPE->getParameterPack());
3054 break;
3055 }
3056
3057 case Expr::DependentScopeDeclRefExprClass: {
3058 const DependentScopeDeclRefExpr *DRE = cast<DependentScopeDeclRefExpr>(E);
3059 mangleUnresolvedName(DRE->getQualifier(), 0, DRE->getDeclName(), Arity);
3060
3061 // All the <unresolved-name> productions end in a
3062 // base-unresolved-name, where <template-args> are just tacked
3063 // onto the end.
3064 if (DRE->hasExplicitTemplateArgs())
3065 mangleTemplateArgs(DRE->getExplicitTemplateArgs());
3066 break;
3067 }
3068
3069 case Expr::CXXBindTemporaryExprClass:
3070 mangleExpression(cast<CXXBindTemporaryExpr>(E)->getSubExpr());
3071 break;
3072
3073 case Expr::ExprWithCleanupsClass:
3074 mangleExpression(cast<ExprWithCleanups>(E)->getSubExpr(), Arity);
3075 break;
3076
3077 case Expr::FloatingLiteralClass: {
3078 const FloatingLiteral *FL = cast<FloatingLiteral>(E);
3079 Out << 'L';
3080 mangleType(FL->getType());
3081 mangleFloat(FL->getValue());
3082 Out << 'E';
3083 break;
3084 }
3085
3086 case Expr::CharacterLiteralClass:
3087 Out << 'L';
3088 mangleType(E->getType());
3089 Out << cast<CharacterLiteral>(E)->getValue();
3090 Out << 'E';
3091 break;
3092
3093 // FIXME. __objc_yes/__objc_no are mangled same as true/false
3094 case Expr::ObjCBoolLiteralExprClass:
3095 Out << "Lb";
3096 Out << (cast<ObjCBoolLiteralExpr>(E)->getValue() ? '1' : '0');
3097 Out << 'E';
3098 break;
3099
3100 case Expr::CXXBoolLiteralExprClass:
3101 Out << "Lb";
3102 Out << (cast<CXXBoolLiteralExpr>(E)->getValue() ? '1' : '0');
3103 Out << 'E';
3104 break;
3105
3106 case Expr::IntegerLiteralClass: {
3107 llvm::APSInt Value(cast<IntegerLiteral>(E)->getValue());
3108 if (E->getType()->isSignedIntegerType())
3109 Value.setIsSigned(true);
3110 mangleIntegerLiteral(E->getType(), Value);
3111 break;
3112 }
3113
3114 case Expr::ImaginaryLiteralClass: {
3115 const ImaginaryLiteral *IE = cast<ImaginaryLiteral>(E);
3116 // Mangle as if a complex literal.
3117 // Proposal from David Vandevoorde, 2010.06.30.
3118 Out << 'L';
3119 mangleType(E->getType());
3120 if (const FloatingLiteral *Imag =
3121 dyn_cast<FloatingLiteral>(IE->getSubExpr())) {
3122 // Mangle a floating-point zero of the appropriate type.
3123 mangleFloat(llvm::APFloat(Imag->getValue().getSemantics()));
3124 Out << '_';
3125 mangleFloat(Imag->getValue());
3126 } else {
3127 Out << "0_";
3128 llvm::APSInt Value(cast<IntegerLiteral>(IE->getSubExpr())->getValue());
3129 if (IE->getSubExpr()->getType()->isSignedIntegerType())
3130 Value.setIsSigned(true);
3131 mangleNumber(Value);
3132 }
3133 Out << 'E';
3134 break;
3135 }
3136
3137 case Expr::StringLiteralClass: {
3138 // Revised proposal from David Vandervoorde, 2010.07.15.
3139 Out << 'L';
3140 assert(isa<ConstantArrayType>(E->getType()));
3141 mangleType(E->getType());
3142 Out << 'E';
3143 break;
3144 }
3145
3146 case Expr::GNUNullExprClass:
3147 // FIXME: should this really be mangled the same as nullptr?
3148 // fallthrough
3149
3150 case Expr::CXXNullPtrLiteralExprClass: {
Guy Benyei11169dd2012-12-18 14:30:41 +00003151 Out << "LDnE";
3152 break;
3153 }
3154
3155 case Expr::PackExpansionExprClass:
3156 Out << "sp";
3157 mangleExpression(cast<PackExpansionExpr>(E)->getPattern());
3158 break;
3159
3160 case Expr::SizeOfPackExprClass: {
3161 Out << "sZ";
3162 const NamedDecl *Pack = cast<SizeOfPackExpr>(E)->getPack();
3163 if (const TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(Pack))
3164 mangleTemplateParameter(TTP->getIndex());
3165 else if (const NonTypeTemplateParmDecl *NTTP
3166 = dyn_cast<NonTypeTemplateParmDecl>(Pack))
3167 mangleTemplateParameter(NTTP->getIndex());
3168 else if (const TemplateTemplateParmDecl *TempTP
3169 = dyn_cast<TemplateTemplateParmDecl>(Pack))
3170 mangleTemplateParameter(TempTP->getIndex());
3171 else
3172 mangleFunctionParam(cast<ParmVarDecl>(Pack));
3173 break;
3174 }
3175
3176 case Expr::MaterializeTemporaryExprClass: {
3177 mangleExpression(cast<MaterializeTemporaryExpr>(E)->GetTemporaryExpr());
3178 break;
3179 }
3180
3181 case Expr::CXXThisExprClass:
3182 Out << "fpT";
3183 break;
3184 }
3185}
3186
3187/// Mangle an expression which refers to a parameter variable.
3188///
3189/// <expression> ::= <function-param>
3190/// <function-param> ::= fp <top-level CV-qualifiers> _ # L == 0, I == 0
3191/// <function-param> ::= fp <top-level CV-qualifiers>
3192/// <parameter-2 non-negative number> _ # L == 0, I > 0
3193/// <function-param> ::= fL <L-1 non-negative number>
3194/// p <top-level CV-qualifiers> _ # L > 0, I == 0
3195/// <function-param> ::= fL <L-1 non-negative number>
3196/// p <top-level CV-qualifiers>
3197/// <I-1 non-negative number> _ # L > 0, I > 0
3198///
3199/// L is the nesting depth of the parameter, defined as 1 if the
3200/// parameter comes from the innermost function prototype scope
3201/// enclosing the current context, 2 if from the next enclosing
3202/// function prototype scope, and so on, with one special case: if
3203/// we've processed the full parameter clause for the innermost
3204/// function type, then L is one less. This definition conveniently
3205/// makes it irrelevant whether a function's result type was written
3206/// trailing or leading, but is otherwise overly complicated; the
3207/// numbering was first designed without considering references to
3208/// parameter in locations other than return types, and then the
3209/// mangling had to be generalized without changing the existing
3210/// manglings.
3211///
3212/// I is the zero-based index of the parameter within its parameter
3213/// declaration clause. Note that the original ABI document describes
3214/// this using 1-based ordinals.
3215void CXXNameMangler::mangleFunctionParam(const ParmVarDecl *parm) {
3216 unsigned parmDepth = parm->getFunctionScopeDepth();
3217 unsigned parmIndex = parm->getFunctionScopeIndex();
3218
3219 // Compute 'L'.
3220 // parmDepth does not include the declaring function prototype.
3221 // FunctionTypeDepth does account for that.
3222 assert(parmDepth < FunctionTypeDepth.getDepth());
3223 unsigned nestingDepth = FunctionTypeDepth.getDepth() - parmDepth;
3224 if (FunctionTypeDepth.isInResultType())
3225 nestingDepth--;
3226
3227 if (nestingDepth == 0) {
3228 Out << "fp";
3229 } else {
3230 Out << "fL" << (nestingDepth - 1) << 'p';
3231 }
3232
3233 // Top-level qualifiers. We don't have to worry about arrays here,
3234 // because parameters declared as arrays should already have been
3235 // transformed to have pointer type. FIXME: apparently these don't
3236 // get mangled if used as an rvalue of a known non-class type?
3237 assert(!parm->getType()->isArrayType()
3238 && "parameter's type is still an array type?");
3239 mangleQualifiers(parm->getType().getQualifiers());
3240
3241 // Parameter index.
3242 if (parmIndex != 0) {
3243 Out << (parmIndex - 1);
3244 }
3245 Out << '_';
3246}
3247
3248void CXXNameMangler::mangleCXXCtorType(CXXCtorType T) {
3249 // <ctor-dtor-name> ::= C1 # complete object constructor
3250 // ::= C2 # base object constructor
3251 // ::= C3 # complete object allocating constructor
3252 //
3253 switch (T) {
3254 case Ctor_Complete:
3255 Out << "C1";
3256 break;
3257 case Ctor_Base:
3258 Out << "C2";
3259 break;
3260 case Ctor_CompleteAllocating:
3261 Out << "C3";
3262 break;
3263 }
3264}
3265
3266void CXXNameMangler::mangleCXXDtorType(CXXDtorType T) {
3267 // <ctor-dtor-name> ::= D0 # deleting destructor
3268 // ::= D1 # complete object destructor
3269 // ::= D2 # base object destructor
3270 //
3271 switch (T) {
3272 case Dtor_Deleting:
3273 Out << "D0";
3274 break;
3275 case Dtor_Complete:
3276 Out << "D1";
3277 break;
3278 case Dtor_Base:
3279 Out << "D2";
3280 break;
3281 }
3282}
3283
3284void CXXNameMangler::mangleTemplateArgs(
3285 const ASTTemplateArgumentListInfo &TemplateArgs) {
3286 // <template-args> ::= I <template-arg>+ E
3287 Out << 'I';
3288 for (unsigned i = 0, e = TemplateArgs.NumTemplateArgs; i != e; ++i)
3289 mangleTemplateArg(TemplateArgs.getTemplateArgs()[i].getArgument());
3290 Out << 'E';
3291}
3292
3293void CXXNameMangler::mangleTemplateArgs(const TemplateArgumentList &AL) {
3294 // <template-args> ::= I <template-arg>+ E
3295 Out << 'I';
3296 for (unsigned i = 0, e = AL.size(); i != e; ++i)
3297 mangleTemplateArg(AL[i]);
3298 Out << 'E';
3299}
3300
3301void CXXNameMangler::mangleTemplateArgs(const TemplateArgument *TemplateArgs,
3302 unsigned NumTemplateArgs) {
3303 // <template-args> ::= I <template-arg>+ E
3304 Out << 'I';
3305 for (unsigned i = 0; i != NumTemplateArgs; ++i)
3306 mangleTemplateArg(TemplateArgs[i]);
3307 Out << 'E';
3308}
3309
3310void CXXNameMangler::mangleTemplateArg(TemplateArgument A) {
3311 // <template-arg> ::= <type> # type or template
3312 // ::= X <expression> E # expression
3313 // ::= <expr-primary> # simple expressions
3314 // ::= J <template-arg>* E # argument pack
Guy Benyei11169dd2012-12-18 14:30:41 +00003315 if (!A.isInstantiationDependent() || A.isDependent())
3316 A = Context.getASTContext().getCanonicalTemplateArgument(A);
3317
3318 switch (A.getKind()) {
3319 case TemplateArgument::Null:
3320 llvm_unreachable("Cannot mangle NULL template argument");
3321
3322 case TemplateArgument::Type:
3323 mangleType(A.getAsType());
3324 break;
3325 case TemplateArgument::Template:
3326 // This is mangled as <type>.
3327 mangleType(A.getAsTemplate());
3328 break;
3329 case TemplateArgument::TemplateExpansion:
3330 // <type> ::= Dp <type> # pack expansion (C++0x)
3331 Out << "Dp";
3332 mangleType(A.getAsTemplateOrTemplatePattern());
3333 break;
3334 case TemplateArgument::Expression: {
3335 // It's possible to end up with a DeclRefExpr here in certain
3336 // dependent cases, in which case we should mangle as a
3337 // declaration.
3338 const Expr *E = A.getAsExpr()->IgnoreParens();
3339 if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E)) {
3340 const ValueDecl *D = DRE->getDecl();
3341 if (isa<VarDecl>(D) || isa<FunctionDecl>(D)) {
3342 Out << "L";
3343 mangle(D, "_Z");
3344 Out << 'E';
3345 break;
3346 }
3347 }
3348
3349 Out << 'X';
3350 mangleExpression(E);
3351 Out << 'E';
3352 break;
3353 }
3354 case TemplateArgument::Integral:
3355 mangleIntegerLiteral(A.getIntegralType(), A.getAsIntegral());
3356 break;
3357 case TemplateArgument::Declaration: {
3358 // <expr-primary> ::= L <mangled-name> E # external name
3359 // Clang produces AST's where pointer-to-member-function expressions
3360 // and pointer-to-function expressions are represented as a declaration not
3361 // an expression. We compensate for it here to produce the correct mangling.
3362 ValueDecl *D = A.getAsDecl();
3363 bool compensateMangling = !A.isDeclForReferenceParam();
3364 if (compensateMangling) {
3365 Out << 'X';
3366 mangleOperatorName(OO_Amp, 1);
3367 }
3368
3369 Out << 'L';
3370 // References to external entities use the mangled name; if the name would
3371 // not normally be manged then mangle it as unqualified.
3372 //
3373 // FIXME: The ABI specifies that external names here should have _Z, but
3374 // gcc leaves this off.
3375 if (compensateMangling)
3376 mangle(D, "_Z");
3377 else
3378 mangle(D, "Z");
3379 Out << 'E';
3380
3381 if (compensateMangling)
3382 Out << 'E';
3383
3384 break;
3385 }
3386 case TemplateArgument::NullPtr: {
3387 // <expr-primary> ::= L <type> 0 E
3388 Out << 'L';
3389 mangleType(A.getNullPtrType());
3390 Out << "0E";
3391 break;
3392 }
3393 case TemplateArgument::Pack: {
Richard Smitheb0133c2013-08-27 01:03:46 +00003394 // <template-arg> ::= J <template-arg>* E
Guy Benyei11169dd2012-12-18 14:30:41 +00003395 Out << 'J';
Richard Smitheb0133c2013-08-27 01:03:46 +00003396 for (TemplateArgument::pack_iterator PA = A.pack_begin(),
Guy Benyei11169dd2012-12-18 14:30:41 +00003397 PAEnd = A.pack_end();
3398 PA != PAEnd; ++PA)
3399 mangleTemplateArg(*PA);
3400 Out << 'E';
3401 }
3402 }
3403}
3404
3405void CXXNameMangler::mangleTemplateParameter(unsigned Index) {
3406 // <template-param> ::= T_ # first template parameter
3407 // ::= T <parameter-2 non-negative number> _
3408 if (Index == 0)
3409 Out << "T_";
3410 else
3411 Out << 'T' << (Index - 1) << '_';
3412}
3413
3414void CXXNameMangler::mangleExistingSubstitution(QualType type) {
3415 bool result = mangleSubstitution(type);
3416 assert(result && "no existing substitution for type");
3417 (void) result;
3418}
3419
3420void CXXNameMangler::mangleExistingSubstitution(TemplateName tname) {
3421 bool result = mangleSubstitution(tname);
3422 assert(result && "no existing substitution for template name");
3423 (void) result;
3424}
3425
3426// <substitution> ::= S <seq-id> _
3427// ::= S_
3428bool CXXNameMangler::mangleSubstitution(const NamedDecl *ND) {
3429 // Try one of the standard substitutions first.
3430 if (mangleStandardSubstitution(ND))
3431 return true;
3432
3433 ND = cast<NamedDecl>(ND->getCanonicalDecl());
3434 return mangleSubstitution(reinterpret_cast<uintptr_t>(ND));
3435}
3436
3437/// \brief Determine whether the given type has any qualifiers that are
3438/// relevant for substitutions.
3439static bool hasMangledSubstitutionQualifiers(QualType T) {
3440 Qualifiers Qs = T.getQualifiers();
3441 return Qs.getCVRQualifiers() || Qs.hasAddressSpace();
3442}
3443
3444bool CXXNameMangler::mangleSubstitution(QualType T) {
3445 if (!hasMangledSubstitutionQualifiers(T)) {
3446 if (const RecordType *RT = T->getAs<RecordType>())
3447 return mangleSubstitution(RT->getDecl());
3448 }
3449
3450 uintptr_t TypePtr = reinterpret_cast<uintptr_t>(T.getAsOpaquePtr());
3451
3452 return mangleSubstitution(TypePtr);
3453}
3454
3455bool CXXNameMangler::mangleSubstitution(TemplateName Template) {
3456 if (TemplateDecl *TD = Template.getAsTemplateDecl())
3457 return mangleSubstitution(TD);
3458
3459 Template = Context.getASTContext().getCanonicalTemplateName(Template);
3460 return mangleSubstitution(
3461 reinterpret_cast<uintptr_t>(Template.getAsVoidPointer()));
3462}
3463
3464bool CXXNameMangler::mangleSubstitution(uintptr_t Ptr) {
3465 llvm::DenseMap<uintptr_t, unsigned>::iterator I = Substitutions.find(Ptr);
3466 if (I == Substitutions.end())
3467 return false;
3468
3469 unsigned SeqID = I->second;
3470 if (SeqID == 0)
3471 Out << "S_";
3472 else {
3473 SeqID--;
3474
3475 // <seq-id> is encoded in base-36, using digits and upper case letters.
3476 char Buffer[10];
Benjamin Kramer5c323b82014-04-12 15:42:48 +00003477 char *BufferPtr = std::end(Buffer);
Guy Benyei11169dd2012-12-18 14:30:41 +00003478
3479 if (SeqID == 0) *--BufferPtr = '0';
3480
3481 while (SeqID) {
3482 assert(BufferPtr > Buffer && "Buffer overflow!");
3483
3484 char c = static_cast<char>(SeqID % 36);
3485
3486 *--BufferPtr = (c < 10 ? '0' + c : 'A' + c - 10);
3487 SeqID /= 36;
3488 }
3489
Benjamin Kramer5c323b82014-04-12 15:42:48 +00003490 Out << 'S' << StringRef(BufferPtr, std::end(Buffer) - BufferPtr) << '_';
Guy Benyei11169dd2012-12-18 14:30:41 +00003491 }
3492
3493 return true;
3494}
3495
3496static bool isCharType(QualType T) {
3497 if (T.isNull())
3498 return false;
3499
3500 return T->isSpecificBuiltinType(BuiltinType::Char_S) ||
3501 T->isSpecificBuiltinType(BuiltinType::Char_U);
3502}
3503
3504/// isCharSpecialization - Returns whether a given type is a template
3505/// specialization of a given name with a single argument of type char.
3506static bool isCharSpecialization(QualType T, const char *Name) {
3507 if (T.isNull())
3508 return false;
3509
3510 const RecordType *RT = T->getAs<RecordType>();
3511 if (!RT)
3512 return false;
3513
3514 const ClassTemplateSpecializationDecl *SD =
3515 dyn_cast<ClassTemplateSpecializationDecl>(RT->getDecl());
3516 if (!SD)
3517 return false;
3518
3519 if (!isStdNamespace(getEffectiveDeclContext(SD)))
3520 return false;
3521
3522 const TemplateArgumentList &TemplateArgs = SD->getTemplateArgs();
3523 if (TemplateArgs.size() != 1)
3524 return false;
3525
3526 if (!isCharType(TemplateArgs[0].getAsType()))
3527 return false;
3528
3529 return SD->getIdentifier()->getName() == Name;
3530}
3531
3532template <std::size_t StrLen>
3533static bool isStreamCharSpecialization(const ClassTemplateSpecializationDecl*SD,
3534 const char (&Str)[StrLen]) {
3535 if (!SD->getIdentifier()->isStr(Str))
3536 return false;
3537
3538 const TemplateArgumentList &TemplateArgs = SD->getTemplateArgs();
3539 if (TemplateArgs.size() != 2)
3540 return false;
3541
3542 if (!isCharType(TemplateArgs[0].getAsType()))
3543 return false;
3544
3545 if (!isCharSpecialization(TemplateArgs[1].getAsType(), "char_traits"))
3546 return false;
3547
3548 return true;
3549}
3550
3551bool CXXNameMangler::mangleStandardSubstitution(const NamedDecl *ND) {
3552 // <substitution> ::= St # ::std::
3553 if (const NamespaceDecl *NS = dyn_cast<NamespaceDecl>(ND)) {
3554 if (isStd(NS)) {
3555 Out << "St";
3556 return true;
3557 }
3558 }
3559
3560 if (const ClassTemplateDecl *TD = dyn_cast<ClassTemplateDecl>(ND)) {
3561 if (!isStdNamespace(getEffectiveDeclContext(TD)))
3562 return false;
3563
3564 // <substitution> ::= Sa # ::std::allocator
3565 if (TD->getIdentifier()->isStr("allocator")) {
3566 Out << "Sa";
3567 return true;
3568 }
3569
3570 // <<substitution> ::= Sb # ::std::basic_string
3571 if (TD->getIdentifier()->isStr("basic_string")) {
3572 Out << "Sb";
3573 return true;
3574 }
3575 }
3576
3577 if (const ClassTemplateSpecializationDecl *SD =
3578 dyn_cast<ClassTemplateSpecializationDecl>(ND)) {
3579 if (!isStdNamespace(getEffectiveDeclContext(SD)))
3580 return false;
3581
3582 // <substitution> ::= Ss # ::std::basic_string<char,
3583 // ::std::char_traits<char>,
3584 // ::std::allocator<char> >
3585 if (SD->getIdentifier()->isStr("basic_string")) {
3586 const TemplateArgumentList &TemplateArgs = SD->getTemplateArgs();
3587
3588 if (TemplateArgs.size() != 3)
3589 return false;
3590
3591 if (!isCharType(TemplateArgs[0].getAsType()))
3592 return false;
3593
3594 if (!isCharSpecialization(TemplateArgs[1].getAsType(), "char_traits"))
3595 return false;
3596
3597 if (!isCharSpecialization(TemplateArgs[2].getAsType(), "allocator"))
3598 return false;
3599
3600 Out << "Ss";
3601 return true;
3602 }
3603
3604 // <substitution> ::= Si # ::std::basic_istream<char,
3605 // ::std::char_traits<char> >
3606 if (isStreamCharSpecialization(SD, "basic_istream")) {
3607 Out << "Si";
3608 return true;
3609 }
3610
3611 // <substitution> ::= So # ::std::basic_ostream<char,
3612 // ::std::char_traits<char> >
3613 if (isStreamCharSpecialization(SD, "basic_ostream")) {
3614 Out << "So";
3615 return true;
3616 }
3617
3618 // <substitution> ::= Sd # ::std::basic_iostream<char,
3619 // ::std::char_traits<char> >
3620 if (isStreamCharSpecialization(SD, "basic_iostream")) {
3621 Out << "Sd";
3622 return true;
3623 }
3624 }
3625 return false;
3626}
3627
3628void CXXNameMangler::addSubstitution(QualType T) {
3629 if (!hasMangledSubstitutionQualifiers(T)) {
3630 if (const RecordType *RT = T->getAs<RecordType>()) {
3631 addSubstitution(RT->getDecl());
3632 return;
3633 }
3634 }
3635
3636 uintptr_t TypePtr = reinterpret_cast<uintptr_t>(T.getAsOpaquePtr());
3637 addSubstitution(TypePtr);
3638}
3639
3640void CXXNameMangler::addSubstitution(TemplateName Template) {
3641 if (TemplateDecl *TD = Template.getAsTemplateDecl())
3642 return addSubstitution(TD);
3643
3644 Template = Context.getASTContext().getCanonicalTemplateName(Template);
3645 addSubstitution(reinterpret_cast<uintptr_t>(Template.getAsVoidPointer()));
3646}
3647
3648void CXXNameMangler::addSubstitution(uintptr_t Ptr) {
3649 assert(!Substitutions.count(Ptr) && "Substitution already exists!");
3650 Substitutions[Ptr] = SeqID++;
3651}
3652
3653//
3654
3655/// \brief Mangles the name of the declaration D and emits that name to the
3656/// given output stream.
3657///
3658/// If the declaration D requires a mangled name, this routine will emit that
3659/// mangled name to \p os and return true. Otherwise, \p os will be unchanged
3660/// and this routine will return false. In this case, the caller should just
3661/// emit the identifier of the declaration (\c D->getIdentifier()) as its
3662/// name.
Rafael Espindola002667c2013-10-16 01:40:34 +00003663void ItaniumMangleContextImpl::mangleCXXName(const NamedDecl *D,
3664 raw_ostream &Out) {
Guy Benyei11169dd2012-12-18 14:30:41 +00003665 assert((isa<FunctionDecl>(D) || isa<VarDecl>(D)) &&
3666 "Invalid mangleName() call, argument is not a variable or function!");
3667 assert(!isa<CXXConstructorDecl>(D) && !isa<CXXDestructorDecl>(D) &&
3668 "Invalid mangleName() call on 'structor decl!");
3669
3670 PrettyStackTraceDecl CrashInfo(D, SourceLocation(),
3671 getASTContext().getSourceManager(),
3672 "Mangling declaration");
3673
3674 CXXNameMangler Mangler(*this, Out, D);
3675 return Mangler.mangle(D);
3676}
3677
Timur Iskhodzhanov67455222013-10-03 06:26:13 +00003678void ItaniumMangleContextImpl::mangleCXXCtor(const CXXConstructorDecl *D,
3679 CXXCtorType Type,
3680 raw_ostream &Out) {
Guy Benyei11169dd2012-12-18 14:30:41 +00003681 CXXNameMangler Mangler(*this, Out, D, Type);
3682 Mangler.mangle(D);
3683}
3684
Timur Iskhodzhanov67455222013-10-03 06:26:13 +00003685void ItaniumMangleContextImpl::mangleCXXDtor(const CXXDestructorDecl *D,
3686 CXXDtorType Type,
3687 raw_ostream &Out) {
Guy Benyei11169dd2012-12-18 14:30:41 +00003688 CXXNameMangler Mangler(*this, Out, D, Type);
3689 Mangler.mangle(D);
3690}
3691
Timur Iskhodzhanov67455222013-10-03 06:26:13 +00003692void ItaniumMangleContextImpl::mangleThunk(const CXXMethodDecl *MD,
3693 const ThunkInfo &Thunk,
3694 raw_ostream &Out) {
Guy Benyei11169dd2012-12-18 14:30:41 +00003695 // <special-name> ::= T <call-offset> <base encoding>
3696 // # base is the nominal target function of thunk
3697 // <special-name> ::= Tc <call-offset> <call-offset> <base encoding>
3698 // # base is the nominal target function of thunk
3699 // # first call-offset is 'this' adjustment
3700 // # second call-offset is result adjustment
3701
3702 assert(!isa<CXXDestructorDecl>(MD) &&
3703 "Use mangleCXXDtor for destructor decls!");
3704 CXXNameMangler Mangler(*this, Out);
3705 Mangler.getStream() << "_ZT";
3706 if (!Thunk.Return.isEmpty())
3707 Mangler.getStream() << 'c';
3708
3709 // Mangle the 'this' pointer adjustment.
Timur Iskhodzhanov053142a2013-11-06 06:24:31 +00003710 Mangler.mangleCallOffset(Thunk.This.NonVirtual,
3711 Thunk.This.Virtual.Itanium.VCallOffsetOffset);
3712
Guy Benyei11169dd2012-12-18 14:30:41 +00003713 // Mangle the return pointer adjustment if there is one.
3714 if (!Thunk.Return.isEmpty())
3715 Mangler.mangleCallOffset(Thunk.Return.NonVirtual,
Timur Iskhodzhanov02014322013-10-30 11:55:43 +00003716 Thunk.Return.Virtual.Itanium.VBaseOffsetOffset);
3717
Guy Benyei11169dd2012-12-18 14:30:41 +00003718 Mangler.mangleFunctionEncoding(MD);
3719}
3720
Timur Iskhodzhanov67455222013-10-03 06:26:13 +00003721void ItaniumMangleContextImpl::mangleCXXDtorThunk(
3722 const CXXDestructorDecl *DD, CXXDtorType Type,
3723 const ThisAdjustment &ThisAdjustment, raw_ostream &Out) {
Guy Benyei11169dd2012-12-18 14:30:41 +00003724 // <special-name> ::= T <call-offset> <base encoding>
3725 // # base is the nominal target function of thunk
3726 CXXNameMangler Mangler(*this, Out, DD, Type);
3727 Mangler.getStream() << "_ZT";
3728
3729 // Mangle the 'this' pointer adjustment.
3730 Mangler.mangleCallOffset(ThisAdjustment.NonVirtual,
Timur Iskhodzhanov053142a2013-11-06 06:24:31 +00003731 ThisAdjustment.Virtual.Itanium.VCallOffsetOffset);
Guy Benyei11169dd2012-12-18 14:30:41 +00003732
3733 Mangler.mangleFunctionEncoding(DD);
3734}
3735
3736/// mangleGuardVariable - Returns the mangled name for a guard variable
3737/// for the passed in VarDecl.
Timur Iskhodzhanov67455222013-10-03 06:26:13 +00003738void ItaniumMangleContextImpl::mangleStaticGuardVariable(const VarDecl *D,
3739 raw_ostream &Out) {
Guy Benyei11169dd2012-12-18 14:30:41 +00003740 // <special-name> ::= GV <object name> # Guard variable for one-time
3741 // # initialization
3742 CXXNameMangler Mangler(*this, Out);
3743 Mangler.getStream() << "_ZGV";
3744 Mangler.mangleName(D);
3745}
3746
Timur Iskhodzhanov67455222013-10-03 06:26:13 +00003747void ItaniumMangleContextImpl::mangleDynamicInitializer(const VarDecl *MD,
3748 raw_ostream &Out) {
Reid Kleckner1ece9fc2013-09-10 20:43:12 +00003749 // These symbols are internal in the Itanium ABI, so the names don't matter.
3750 // Clang has traditionally used this symbol and allowed LLVM to adjust it to
3751 // avoid duplicate symbols.
3752 Out << "__cxx_global_var_init";
3753}
3754
Timur Iskhodzhanov67455222013-10-03 06:26:13 +00003755void ItaniumMangleContextImpl::mangleDynamicAtExitDestructor(const VarDecl *D,
3756 raw_ostream &Out) {
Reid Klecknerd8110b62013-09-10 20:14:30 +00003757 // Prefix the mangling of D with __dtor_.
3758 CXXNameMangler Mangler(*this, Out);
3759 Mangler.getStream() << "__dtor_";
3760 if (shouldMangleDeclName(D))
3761 Mangler.mangle(D);
3762 else
3763 Mangler.getStream() << D->getName();
3764}
3765
Timur Iskhodzhanov67455222013-10-03 06:26:13 +00003766void ItaniumMangleContextImpl::mangleItaniumThreadLocalInit(const VarDecl *D,
3767 raw_ostream &Out) {
Richard Smith2fd1d7a2013-04-19 16:42:07 +00003768 // <special-name> ::= TH <object name>
3769 CXXNameMangler Mangler(*this, Out);
3770 Mangler.getStream() << "_ZTH";
3771 Mangler.mangleName(D);
3772}
3773
Timur Iskhodzhanov67455222013-10-03 06:26:13 +00003774void
3775ItaniumMangleContextImpl::mangleItaniumThreadLocalWrapper(const VarDecl *D,
3776 raw_ostream &Out) {
Richard Smith2fd1d7a2013-04-19 16:42:07 +00003777 // <special-name> ::= TW <object name>
3778 CXXNameMangler Mangler(*this, Out);
3779 Mangler.getStream() << "_ZTW";
3780 Mangler.mangleName(D);
3781}
3782
Timur Iskhodzhanov67455222013-10-03 06:26:13 +00003783void ItaniumMangleContextImpl::mangleReferenceTemporary(const VarDecl *D,
3784 raw_ostream &Out) {
Guy Benyei11169dd2012-12-18 14:30:41 +00003785 // We match the GCC mangling here.
3786 // <special-name> ::= GR <object name>
3787 CXXNameMangler Mangler(*this, Out);
3788 Mangler.getStream() << "_ZGR";
3789 Mangler.mangleName(D);
3790}
3791
Timur Iskhodzhanov67455222013-10-03 06:26:13 +00003792void ItaniumMangleContextImpl::mangleCXXVTable(const CXXRecordDecl *RD,
3793 raw_ostream &Out) {
Guy Benyei11169dd2012-12-18 14:30:41 +00003794 // <special-name> ::= TV <type> # virtual table
3795 CXXNameMangler Mangler(*this, Out);
3796 Mangler.getStream() << "_ZTV";
3797 Mangler.mangleNameOrStandardSubstitution(RD);
3798}
3799
Timur Iskhodzhanov67455222013-10-03 06:26:13 +00003800void ItaniumMangleContextImpl::mangleCXXVTT(const CXXRecordDecl *RD,
3801 raw_ostream &Out) {
Guy Benyei11169dd2012-12-18 14:30:41 +00003802 // <special-name> ::= TT <type> # VTT structure
3803 CXXNameMangler Mangler(*this, Out);
3804 Mangler.getStream() << "_ZTT";
3805 Mangler.mangleNameOrStandardSubstitution(RD);
3806}
3807
Timur Iskhodzhanov67455222013-10-03 06:26:13 +00003808void ItaniumMangleContextImpl::mangleCXXCtorVTable(const CXXRecordDecl *RD,
3809 int64_t Offset,
3810 const CXXRecordDecl *Type,
3811 raw_ostream &Out) {
Guy Benyei11169dd2012-12-18 14:30:41 +00003812 // <special-name> ::= TC <type> <offset number> _ <base type>
3813 CXXNameMangler Mangler(*this, Out);
3814 Mangler.getStream() << "_ZTC";
3815 Mangler.mangleNameOrStandardSubstitution(RD);
3816 Mangler.getStream() << Offset;
3817 Mangler.getStream() << '_';
3818 Mangler.mangleNameOrStandardSubstitution(Type);
3819}
3820
Timur Iskhodzhanov67455222013-10-03 06:26:13 +00003821void ItaniumMangleContextImpl::mangleCXXRTTI(QualType Ty, raw_ostream &Out) {
Guy Benyei11169dd2012-12-18 14:30:41 +00003822 // <special-name> ::= TI <type> # typeinfo structure
3823 assert(!Ty.hasQualifiers() && "RTTI info cannot have top-level qualifiers");
3824 CXXNameMangler Mangler(*this, Out);
3825 Mangler.getStream() << "_ZTI";
3826 Mangler.mangleType(Ty);
3827}
3828
Timur Iskhodzhanov67455222013-10-03 06:26:13 +00003829void ItaniumMangleContextImpl::mangleCXXRTTIName(QualType Ty,
3830 raw_ostream &Out) {
Guy Benyei11169dd2012-12-18 14:30:41 +00003831 // <special-name> ::= TS <type> # typeinfo name (null terminated byte string)
3832 CXXNameMangler Mangler(*this, Out);
3833 Mangler.getStream() << "_ZTS";
3834 Mangler.mangleType(Ty);
3835}
3836
Reid Klecknercc99e262013-11-19 23:23:00 +00003837void ItaniumMangleContextImpl::mangleTypeName(QualType Ty, raw_ostream &Out) {
3838 mangleCXXRTTIName(Ty, Out);
3839}
3840
David Majnemer58e5bee2014-03-24 21:43:36 +00003841void ItaniumMangleContextImpl::mangleStringLiteral(const StringLiteral *, raw_ostream &) {
3842 llvm_unreachable("Can't mangle string literals");
3843}
3844
Timur Iskhodzhanov67455222013-10-03 06:26:13 +00003845ItaniumMangleContext *
3846ItaniumMangleContext::create(ASTContext &Context, DiagnosticsEngine &Diags) {
3847 return new ItaniumMangleContextImpl(Context, Diags);
Guy Benyei11169dd2012-12-18 14:30:41 +00003848}