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