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