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