1 //===--- ItaniumMangle.cpp - Itanium C++ Name Mangling ----------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // Implements C++ name mangling according to the Itanium C++ ABI,
10 // which is used in GCC 3.2 and newer (and many compilers that are
11 // ABI-compatible with GCC):
12 //
13 //   http://itanium-cxx-abi.github.io/cxx-abi/abi.html#mangling
14 //
15 //===----------------------------------------------------------------------===//
16 
17 #include "clang/AST/ASTContext.h"
18 #include "clang/AST/Attr.h"
19 #include "clang/AST/Decl.h"
20 #include "clang/AST/DeclCXX.h"
21 #include "clang/AST/DeclObjC.h"
22 #include "clang/AST/DeclOpenMP.h"
23 #include "clang/AST/DeclTemplate.h"
24 #include "clang/AST/Expr.h"
25 #include "clang/AST/ExprCXX.h"
26 #include "clang/AST/ExprConcepts.h"
27 #include "clang/AST/ExprObjC.h"
28 #include "clang/AST/Mangle.h"
29 #include "clang/AST/TypeLoc.h"
30 #include "clang/Basic/ABI.h"
31 #include "clang/Basic/Module.h"
32 #include "clang/Basic/SourceManager.h"
33 #include "clang/Basic/TargetInfo.h"
34 #include "clang/Basic/Thunk.h"
35 #include "llvm/ADT/StringExtras.h"
36 #include "llvm/Support/ErrorHandling.h"
37 #include "llvm/Support/raw_ostream.h"
38 
39 using namespace clang;
40 
41 namespace {
42 
43 /// Retrieve the declaration context that should be used when mangling the given
44 /// declaration.
getEffectiveDeclContext(const Decl * D)45 static const DeclContext *getEffectiveDeclContext(const Decl *D) {
46   // The ABI assumes that lambda closure types that occur within
47   // default arguments live in the context of the function. However, due to
48   // the way in which Clang parses and creates function declarations, this is
49   // not the case: the lambda closure type ends up living in the context
50   // where the function itself resides, because the function declaration itself
51   // had not yet been created. Fix the context here.
52   if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D)) {
53     if (RD->isLambda())
54       if (ParmVarDecl *ContextParam
55             = dyn_cast_or_null<ParmVarDecl>(RD->getLambdaContextDecl()))
56         return ContextParam->getDeclContext();
57   }
58 
59   // Perform the same check for block literals.
60   if (const BlockDecl *BD = dyn_cast<BlockDecl>(D)) {
61     if (ParmVarDecl *ContextParam
62           = dyn_cast_or_null<ParmVarDecl>(BD->getBlockManglingContextDecl()))
63       return ContextParam->getDeclContext();
64   }
65 
66   const DeclContext *DC = D->getDeclContext();
67   if (isa<CapturedDecl>(DC) || isa<OMPDeclareReductionDecl>(DC) ||
68       isa<OMPDeclareMapperDecl>(DC)) {
69     return getEffectiveDeclContext(cast<Decl>(DC));
70   }
71 
72   if (const auto *VD = dyn_cast<VarDecl>(D))
73     if (VD->isExternC())
74       return VD->getASTContext().getTranslationUnitDecl();
75 
76   if (const auto *FD = dyn_cast<FunctionDecl>(D))
77     if (FD->isExternC())
78       return FD->getASTContext().getTranslationUnitDecl();
79 
80   return DC->getRedeclContext();
81 }
82 
getEffectiveParentContext(const DeclContext * DC)83 static const DeclContext *getEffectiveParentContext(const DeclContext *DC) {
84   return getEffectiveDeclContext(cast<Decl>(DC));
85 }
86 
isLocalContainerContext(const DeclContext * DC)87 static bool isLocalContainerContext(const DeclContext *DC) {
88   return isa<FunctionDecl>(DC) || isa<ObjCMethodDecl>(DC) || isa<BlockDecl>(DC);
89 }
90 
GetLocalClassDecl(const Decl * D)91 static const RecordDecl *GetLocalClassDecl(const Decl *D) {
92   const DeclContext *DC = getEffectiveDeclContext(D);
93   while (!DC->isNamespace() && !DC->isTranslationUnit()) {
94     if (isLocalContainerContext(DC))
95       return dyn_cast<RecordDecl>(D);
96     D = cast<Decl>(DC);
97     DC = getEffectiveDeclContext(D);
98   }
99   return nullptr;
100 }
101 
getStructor(const FunctionDecl * fn)102 static const FunctionDecl *getStructor(const FunctionDecl *fn) {
103   if (const FunctionTemplateDecl *ftd = fn->getPrimaryTemplate())
104     return ftd->getTemplatedDecl();
105 
106   return fn;
107 }
108 
getStructor(const NamedDecl * decl)109 static const NamedDecl *getStructor(const NamedDecl *decl) {
110   const FunctionDecl *fn = dyn_cast_or_null<FunctionDecl>(decl);
111   return (fn ? getStructor(fn) : decl);
112 }
113 
isLambda(const NamedDecl * ND)114 static bool isLambda(const NamedDecl *ND) {
115   const CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(ND);
116   if (!Record)
117     return false;
118 
119   return Record->isLambda();
120 }
121 
122 static const unsigned UnknownArity = ~0U;
123 
124 class ItaniumMangleContextImpl : public ItaniumMangleContext {
125   typedef std::pair<const DeclContext*, IdentifierInfo*> DiscriminatorKeyTy;
126   llvm::DenseMap<DiscriminatorKeyTy, unsigned> Discriminator;
127   llvm::DenseMap<const NamedDecl*, unsigned> Uniquifier;
128   const DiscriminatorOverrideTy DiscriminatorOverride = nullptr;
129 
130   bool NeedsUniqueInternalLinkageNames = false;
131 
132 public:
ItaniumMangleContextImpl(ASTContext & Context,DiagnosticsEngine & Diags,DiscriminatorOverrideTy DiscriminatorOverride)133   explicit ItaniumMangleContextImpl(
134       ASTContext &Context, DiagnosticsEngine &Diags,
135       DiscriminatorOverrideTy DiscriminatorOverride)
136       : ItaniumMangleContext(Context, Diags),
137         DiscriminatorOverride(DiscriminatorOverride) {}
138 
139   /// @name Mangler Entry Points
140   /// @{
141 
142   bool shouldMangleCXXName(const NamedDecl *D) override;
shouldMangleStringLiteral(const StringLiteral *)143   bool shouldMangleStringLiteral(const StringLiteral *) override {
144     return false;
145   }
146 
147   bool isUniqueInternalLinkageDecl(const NamedDecl *ND) override;
needsUniqueInternalLinkageNames()148   void needsUniqueInternalLinkageNames() override {
149     NeedsUniqueInternalLinkageNames = true;
150   }
151 
152   void mangleCXXName(GlobalDecl GD, raw_ostream &) override;
153   void mangleThunk(const CXXMethodDecl *MD, const ThunkInfo &Thunk,
154                    raw_ostream &) override;
155   void mangleCXXDtorThunk(const CXXDestructorDecl *DD, CXXDtorType Type,
156                           const ThisAdjustment &ThisAdjustment,
157                           raw_ostream &) override;
158   void mangleReferenceTemporary(const VarDecl *D, unsigned ManglingNumber,
159                                 raw_ostream &) override;
160   void mangleCXXVTable(const CXXRecordDecl *RD, raw_ostream &) override;
161   void mangleCXXVTT(const CXXRecordDecl *RD, raw_ostream &) override;
162   void mangleCXXCtorVTable(const CXXRecordDecl *RD, int64_t Offset,
163                            const CXXRecordDecl *Type, raw_ostream &) override;
164   void mangleCXXRTTI(QualType T, raw_ostream &) override;
165   void mangleCXXRTTIName(QualType T, raw_ostream &) override;
166   void mangleTypeName(QualType T, raw_ostream &) override;
167 
168   void mangleCXXCtorComdat(const CXXConstructorDecl *D, raw_ostream &) override;
169   void mangleCXXDtorComdat(const CXXDestructorDecl *D, raw_ostream &) override;
170   void mangleStaticGuardVariable(const VarDecl *D, raw_ostream &) override;
171   void mangleDynamicInitializer(const VarDecl *D, raw_ostream &Out) override;
172   void mangleDynamicAtExitDestructor(const VarDecl *D,
173                                      raw_ostream &Out) override;
174   void mangleDynamicStermFinalizer(const VarDecl *D, raw_ostream &Out) override;
175   void mangleSEHFilterExpression(const NamedDecl *EnclosingDecl,
176                                  raw_ostream &Out) override;
177   void mangleSEHFinallyBlock(const NamedDecl *EnclosingDecl,
178                              raw_ostream &Out) override;
179   void mangleItaniumThreadLocalInit(const VarDecl *D, raw_ostream &) override;
180   void mangleItaniumThreadLocalWrapper(const VarDecl *D,
181                                        raw_ostream &) override;
182 
183   void mangleStringLiteral(const StringLiteral *, raw_ostream &) override;
184 
185   void mangleLambdaSig(const CXXRecordDecl *Lambda, raw_ostream &) override;
186 
getNextDiscriminator(const NamedDecl * ND,unsigned & disc)187   bool getNextDiscriminator(const NamedDecl *ND, unsigned &disc) {
188     // Lambda closure types are already numbered.
189     if (isLambda(ND))
190       return false;
191 
192     // Anonymous tags are already numbered.
193     if (const TagDecl *Tag = dyn_cast<TagDecl>(ND)) {
194       if (Tag->getName().empty() && !Tag->getTypedefNameForAnonDecl())
195         return false;
196     }
197 
198     // Use the canonical number for externally visible decls.
199     if (ND->isExternallyVisible()) {
200       unsigned discriminator = getASTContext().getManglingNumber(ND);
201       if (discriminator == 1)
202         return false;
203       disc = discriminator - 2;
204       return true;
205     }
206 
207     // Make up a reasonable number for internal decls.
208     unsigned &discriminator = Uniquifier[ND];
209     if (!discriminator) {
210       const DeclContext *DC = getEffectiveDeclContext(ND);
211       discriminator = ++Discriminator[std::make_pair(DC, ND->getIdentifier())];
212     }
213     if (discriminator == 1)
214       return false;
215     disc = discriminator-2;
216     return true;
217   }
218 
getLambdaString(const CXXRecordDecl * Lambda)219   std::string getLambdaString(const CXXRecordDecl *Lambda) override {
220     // This function matches the one in MicrosoftMangle, which returns
221     // the string that is used in lambda mangled names.
222     assert(Lambda->isLambda() && "RD must be a lambda!");
223     std::string Name("<lambda");
224     Decl *LambdaContextDecl = Lambda->getLambdaContextDecl();
225     unsigned LambdaManglingNumber = Lambda->getLambdaManglingNumber();
226     unsigned LambdaId;
227     const ParmVarDecl *Parm = dyn_cast_or_null<ParmVarDecl>(LambdaContextDecl);
228     const FunctionDecl *Func =
229         Parm ? dyn_cast<FunctionDecl>(Parm->getDeclContext()) : nullptr;
230 
231     if (Func) {
232       unsigned DefaultArgNo =
233           Func->getNumParams() - Parm->getFunctionScopeIndex();
234       Name += llvm::utostr(DefaultArgNo);
235       Name += "_";
236     }
237 
238     if (LambdaManglingNumber)
239       LambdaId = LambdaManglingNumber;
240     else
241       LambdaId = getAnonymousStructIdForDebugInfo(Lambda);
242 
243     Name += llvm::utostr(LambdaId);
244     Name += '>';
245     return Name;
246   }
247 
getDiscriminatorOverride() const248   DiscriminatorOverrideTy getDiscriminatorOverride() const override {
249     return DiscriminatorOverride;
250   }
251 
252   /// @}
253 };
254 
255 /// Manage the mangling of a single name.
256 class CXXNameMangler {
257   ItaniumMangleContextImpl &Context;
258   raw_ostream &Out;
259   bool NullOut = false;
260   /// In the "DisableDerivedAbiTags" mode derived ABI tags are not calculated.
261   /// This mode is used when mangler creates another mangler recursively to
262   /// calculate ABI tags for the function return value or the variable type.
263   /// Also it is required to avoid infinite recursion in some cases.
264   bool DisableDerivedAbiTags = false;
265 
266   /// The "structor" is the top-level declaration being mangled, if
267   /// that's not a template specialization; otherwise it's the pattern
268   /// for that specialization.
269   const NamedDecl *Structor;
270   unsigned StructorType;
271 
272   /// The next substitution sequence number.
273   unsigned SeqID;
274 
275   class FunctionTypeDepthState {
276     unsigned Bits;
277 
278     enum { InResultTypeMask = 1 };
279 
280   public:
FunctionTypeDepthState()281     FunctionTypeDepthState() : Bits(0) {}
282 
283     /// The number of function types we're inside.
getDepth() const284     unsigned getDepth() const {
285       return Bits >> 1;
286     }
287 
288     /// True if we're in the return type of the innermost function type.
isInResultType() const289     bool isInResultType() const {
290       return Bits & InResultTypeMask;
291     }
292 
push()293     FunctionTypeDepthState push() {
294       FunctionTypeDepthState tmp = *this;
295       Bits = (Bits & ~InResultTypeMask) + 2;
296       return tmp;
297     }
298 
enterResultType()299     void enterResultType() {
300       Bits |= InResultTypeMask;
301     }
302 
leaveResultType()303     void leaveResultType() {
304       Bits &= ~InResultTypeMask;
305     }
306 
pop(FunctionTypeDepthState saved)307     void pop(FunctionTypeDepthState saved) {
308       assert(getDepth() == saved.getDepth() + 1);
309       Bits = saved.Bits;
310     }
311 
312   } FunctionTypeDepth;
313 
314   // abi_tag is a gcc attribute, taking one or more strings called "tags".
315   // The goal is to annotate against which version of a library an object was
316   // built and to be able to provide backwards compatibility ("dual abi").
317   // For more information see docs/ItaniumMangleAbiTags.rst.
318   typedef SmallVector<StringRef, 4> AbiTagList;
319 
320   // State to gather all implicit and explicit tags used in a mangled name.
321   // Must always have an instance of this while emitting any name to keep
322   // track.
323   class AbiTagState final {
324   public:
AbiTagState(AbiTagState * & Head)325     explicit AbiTagState(AbiTagState *&Head) : LinkHead(Head) {
326       Parent = LinkHead;
327       LinkHead = this;
328     }
329 
330     // No copy, no move.
331     AbiTagState(const AbiTagState &) = delete;
332     AbiTagState &operator=(const AbiTagState &) = delete;
333 
~AbiTagState()334     ~AbiTagState() { pop(); }
335 
write(raw_ostream & Out,const NamedDecl * ND,const AbiTagList * AdditionalAbiTags)336     void write(raw_ostream &Out, const NamedDecl *ND,
337                const AbiTagList *AdditionalAbiTags) {
338       ND = cast<NamedDecl>(ND->getCanonicalDecl());
339       if (!isa<FunctionDecl>(ND) && !isa<VarDecl>(ND)) {
340         assert(
341             !AdditionalAbiTags &&
342             "only function and variables need a list of additional abi tags");
343         if (const auto *NS = dyn_cast<NamespaceDecl>(ND)) {
344           if (const auto *AbiTag = NS->getAttr<AbiTagAttr>()) {
345             UsedAbiTags.insert(UsedAbiTags.end(), AbiTag->tags().begin(),
346                                AbiTag->tags().end());
347           }
348           // Don't emit abi tags for namespaces.
349           return;
350         }
351       }
352 
353       AbiTagList TagList;
354       if (const auto *AbiTag = ND->getAttr<AbiTagAttr>()) {
355         UsedAbiTags.insert(UsedAbiTags.end(), AbiTag->tags().begin(),
356                            AbiTag->tags().end());
357         TagList.insert(TagList.end(), AbiTag->tags().begin(),
358                        AbiTag->tags().end());
359       }
360 
361       if (AdditionalAbiTags) {
362         UsedAbiTags.insert(UsedAbiTags.end(), AdditionalAbiTags->begin(),
363                            AdditionalAbiTags->end());
364         TagList.insert(TagList.end(), AdditionalAbiTags->begin(),
365                        AdditionalAbiTags->end());
366       }
367 
368       llvm::sort(TagList);
369       TagList.erase(std::unique(TagList.begin(), TagList.end()), TagList.end());
370 
371       writeSortedUniqueAbiTags(Out, TagList);
372     }
373 
getUsedAbiTags() const374     const AbiTagList &getUsedAbiTags() const { return UsedAbiTags; }
setUsedAbiTags(const AbiTagList & AbiTags)375     void setUsedAbiTags(const AbiTagList &AbiTags) {
376       UsedAbiTags = AbiTags;
377     }
378 
getEmittedAbiTags() const379     const AbiTagList &getEmittedAbiTags() const {
380       return EmittedAbiTags;
381     }
382 
getSortedUniqueUsedAbiTags()383     const AbiTagList &getSortedUniqueUsedAbiTags() {
384       llvm::sort(UsedAbiTags);
385       UsedAbiTags.erase(std::unique(UsedAbiTags.begin(), UsedAbiTags.end()),
386                         UsedAbiTags.end());
387       return UsedAbiTags;
388     }
389 
390   private:
391     //! All abi tags used implicitly or explicitly.
392     AbiTagList UsedAbiTags;
393     //! All explicit abi tags (i.e. not from namespace).
394     AbiTagList EmittedAbiTags;
395 
396     AbiTagState *&LinkHead;
397     AbiTagState *Parent = nullptr;
398 
pop()399     void pop() {
400       assert(LinkHead == this &&
401              "abi tag link head must point to us on destruction");
402       if (Parent) {
403         Parent->UsedAbiTags.insert(Parent->UsedAbiTags.end(),
404                                    UsedAbiTags.begin(), UsedAbiTags.end());
405         Parent->EmittedAbiTags.insert(Parent->EmittedAbiTags.end(),
406                                       EmittedAbiTags.begin(),
407                                       EmittedAbiTags.end());
408       }
409       LinkHead = Parent;
410     }
411 
writeSortedUniqueAbiTags(raw_ostream & Out,const AbiTagList & AbiTags)412     void writeSortedUniqueAbiTags(raw_ostream &Out, const AbiTagList &AbiTags) {
413       for (const auto &Tag : AbiTags) {
414         EmittedAbiTags.push_back(Tag);
415         Out << "B";
416         Out << Tag.size();
417         Out << Tag;
418       }
419     }
420   };
421 
422   AbiTagState *AbiTags = nullptr;
423   AbiTagState AbiTagsRoot;
424 
425   llvm::DenseMap<uintptr_t, unsigned> Substitutions;
426   llvm::DenseMap<StringRef, unsigned> ModuleSubstitutions;
427 
getASTContext() const428   ASTContext &getASTContext() const { return Context.getASTContext(); }
429 
430 public:
CXXNameMangler(ItaniumMangleContextImpl & C,raw_ostream & Out_,const NamedDecl * D=nullptr,bool NullOut_=false)431   CXXNameMangler(ItaniumMangleContextImpl &C, raw_ostream &Out_,
432                  const NamedDecl *D = nullptr, bool NullOut_ = false)
433     : Context(C), Out(Out_), NullOut(NullOut_),  Structor(getStructor(D)),
434       StructorType(0), SeqID(0), AbiTagsRoot(AbiTags) {
435     // These can't be mangled without a ctor type or dtor type.
436     assert(!D || (!isa<CXXDestructorDecl>(D) &&
437                   !isa<CXXConstructorDecl>(D)));
438   }
CXXNameMangler(ItaniumMangleContextImpl & C,raw_ostream & Out_,const CXXConstructorDecl * D,CXXCtorType Type)439   CXXNameMangler(ItaniumMangleContextImpl &C, raw_ostream &Out_,
440                  const CXXConstructorDecl *D, CXXCtorType Type)
441     : Context(C), Out(Out_), Structor(getStructor(D)), StructorType(Type),
442       SeqID(0), AbiTagsRoot(AbiTags) { }
CXXNameMangler(ItaniumMangleContextImpl & C,raw_ostream & Out_,const CXXDestructorDecl * D,CXXDtorType Type)443   CXXNameMangler(ItaniumMangleContextImpl &C, raw_ostream &Out_,
444                  const CXXDestructorDecl *D, CXXDtorType Type)
445     : Context(C), Out(Out_), Structor(getStructor(D)), StructorType(Type),
446       SeqID(0), AbiTagsRoot(AbiTags) { }
447 
CXXNameMangler(CXXNameMangler & Outer,raw_ostream & Out_)448   CXXNameMangler(CXXNameMangler &Outer, raw_ostream &Out_)
449       : Context(Outer.Context), Out(Out_), NullOut(false),
450         Structor(Outer.Structor), StructorType(Outer.StructorType),
451         SeqID(Outer.SeqID), FunctionTypeDepth(Outer.FunctionTypeDepth),
452         AbiTagsRoot(AbiTags), Substitutions(Outer.Substitutions) {}
453 
CXXNameMangler(CXXNameMangler & Outer,llvm::raw_null_ostream & Out_)454   CXXNameMangler(CXXNameMangler &Outer, llvm::raw_null_ostream &Out_)
455       : Context(Outer.Context), Out(Out_), NullOut(true),
456         Structor(Outer.Structor), StructorType(Outer.StructorType),
457         SeqID(Outer.SeqID), FunctionTypeDepth(Outer.FunctionTypeDepth),
458         AbiTagsRoot(AbiTags), Substitutions(Outer.Substitutions) {}
459 
getStream()460   raw_ostream &getStream() { return Out; }
461 
disableDerivedAbiTags()462   void disableDerivedAbiTags() { DisableDerivedAbiTags = true; }
463   static bool shouldHaveAbiTags(ItaniumMangleContextImpl &C, const VarDecl *VD);
464 
465   void mangle(GlobalDecl GD);
466   void mangleCallOffset(int64_t NonVirtual, int64_t Virtual);
467   void mangleNumber(const llvm::APSInt &I);
468   void mangleNumber(int64_t Number);
469   void mangleFloat(const llvm::APFloat &F);
470   void mangleFunctionEncoding(GlobalDecl GD);
471   void mangleSeqID(unsigned SeqID);
472   void mangleName(GlobalDecl GD);
473   void mangleType(QualType T);
474   void mangleNameOrStandardSubstitution(const NamedDecl *ND);
475   void mangleLambdaSig(const CXXRecordDecl *Lambda);
476 
477 private:
478 
479   bool mangleSubstitution(const NamedDecl *ND);
480   bool mangleSubstitution(QualType T);
481   bool mangleSubstitution(TemplateName Template);
482   bool mangleSubstitution(uintptr_t Ptr);
483 
484   void mangleExistingSubstitution(TemplateName name);
485 
486   bool mangleStandardSubstitution(const NamedDecl *ND);
487 
addSubstitution(const NamedDecl * ND)488   void addSubstitution(const NamedDecl *ND) {
489     ND = cast<NamedDecl>(ND->getCanonicalDecl());
490 
491     addSubstitution(reinterpret_cast<uintptr_t>(ND));
492   }
493   void addSubstitution(QualType T);
494   void addSubstitution(TemplateName Template);
495   void addSubstitution(uintptr_t Ptr);
496   // Destructive copy substitutions from other mangler.
497   void extendSubstitutions(CXXNameMangler* Other);
498 
499   void mangleUnresolvedPrefix(NestedNameSpecifier *qualifier,
500                               bool recursive = false);
501   void mangleUnresolvedName(NestedNameSpecifier *qualifier,
502                             DeclarationName name,
503                             const TemplateArgumentLoc *TemplateArgs,
504                             unsigned NumTemplateArgs,
505                             unsigned KnownArity = UnknownArity);
506 
507   void mangleFunctionEncodingBareType(const FunctionDecl *FD);
508 
509   void mangleNameWithAbiTags(GlobalDecl GD,
510                              const AbiTagList *AdditionalAbiTags);
511   void mangleModuleName(const Module *M);
512   void mangleModuleNamePrefix(StringRef Name);
513   void mangleTemplateName(const TemplateDecl *TD,
514                           const TemplateArgument *TemplateArgs,
515                           unsigned NumTemplateArgs);
mangleUnqualifiedName(GlobalDecl GD,const AbiTagList * AdditionalAbiTags)516   void mangleUnqualifiedName(GlobalDecl GD,
517                              const AbiTagList *AdditionalAbiTags) {
518     mangleUnqualifiedName(GD, cast<NamedDecl>(GD.getDecl())->getDeclName(), UnknownArity,
519                           AdditionalAbiTags);
520   }
521   void mangleUnqualifiedName(GlobalDecl GD, DeclarationName Name,
522                              unsigned KnownArity,
523                              const AbiTagList *AdditionalAbiTags);
524   void mangleUnscopedName(GlobalDecl GD,
525                           const AbiTagList *AdditionalAbiTags);
526   void mangleUnscopedTemplateName(GlobalDecl GD,
527                                   const AbiTagList *AdditionalAbiTags);
528   void mangleSourceName(const IdentifierInfo *II);
529   void mangleRegCallName(const IdentifierInfo *II);
530   void mangleDeviceStubName(const IdentifierInfo *II);
531   void mangleSourceNameWithAbiTags(
532       const NamedDecl *ND, const AbiTagList *AdditionalAbiTags = nullptr);
533   void mangleLocalName(GlobalDecl GD,
534                        const AbiTagList *AdditionalAbiTags);
535   void mangleBlockForPrefix(const BlockDecl *Block);
536   void mangleUnqualifiedBlock(const BlockDecl *Block);
537   void mangleTemplateParamDecl(const NamedDecl *Decl);
538   void mangleLambda(const CXXRecordDecl *Lambda);
539   void mangleNestedName(GlobalDecl GD, const DeclContext *DC,
540                         const AbiTagList *AdditionalAbiTags,
541                         bool NoFunction=false);
542   void mangleNestedName(const TemplateDecl *TD,
543                         const TemplateArgument *TemplateArgs,
544                         unsigned NumTemplateArgs);
545   void mangleNestedNameWithClosurePrefix(GlobalDecl GD,
546                                          const NamedDecl *PrefixND,
547                                          const AbiTagList *AdditionalAbiTags);
548   void manglePrefix(NestedNameSpecifier *qualifier);
549   void manglePrefix(const DeclContext *DC, bool NoFunction=false);
550   void manglePrefix(QualType type);
551   void mangleTemplatePrefix(GlobalDecl GD, bool NoFunction=false);
552   void mangleTemplatePrefix(TemplateName Template);
553   const NamedDecl *getClosurePrefix(const Decl *ND);
554   void mangleClosurePrefix(const NamedDecl *ND, bool NoFunction = false);
555   bool mangleUnresolvedTypeOrSimpleId(QualType DestroyedType,
556                                       StringRef Prefix = "");
557   void mangleOperatorName(DeclarationName Name, unsigned Arity);
558   void mangleOperatorName(OverloadedOperatorKind OO, unsigned Arity);
559   void mangleVendorQualifier(StringRef qualifier);
560   void mangleQualifiers(Qualifiers Quals, const DependentAddressSpaceType *DAST = nullptr);
561   void mangleRefQualifier(RefQualifierKind RefQualifier);
562 
563   void mangleObjCMethodName(const ObjCMethodDecl *MD);
564 
565   // Declare manglers for every type class.
566 #define ABSTRACT_TYPE(CLASS, PARENT)
567 #define NON_CANONICAL_TYPE(CLASS, PARENT)
568 #define TYPE(CLASS, PARENT) void mangleType(const CLASS##Type *T);
569 #include "clang/AST/TypeNodes.inc"
570 
571   void mangleType(const TagType*);
572   void mangleType(TemplateName);
573   static StringRef getCallingConvQualifierName(CallingConv CC);
574   void mangleExtParameterInfo(FunctionProtoType::ExtParameterInfo info);
575   void mangleExtFunctionInfo(const FunctionType *T);
576   void mangleBareFunctionType(const FunctionProtoType *T, bool MangleReturnType,
577                               const FunctionDecl *FD = nullptr);
578   void mangleNeonVectorType(const VectorType *T);
579   void mangleNeonVectorType(const DependentVectorType *T);
580   void mangleAArch64NeonVectorType(const VectorType *T);
581   void mangleAArch64NeonVectorType(const DependentVectorType *T);
582   void mangleAArch64FixedSveVectorType(const VectorType *T);
583   void mangleAArch64FixedSveVectorType(const DependentVectorType *T);
584 
585   void mangleIntegerLiteral(QualType T, const llvm::APSInt &Value);
586   void mangleFloatLiteral(QualType T, const llvm::APFloat &V);
587   void mangleFixedPointLiteral();
588   void mangleNullPointer(QualType T);
589 
590   void mangleMemberExprBase(const Expr *base, bool isArrow);
591   void mangleMemberExpr(const Expr *base, bool isArrow,
592                         NestedNameSpecifier *qualifier,
593                         NamedDecl *firstQualifierLookup,
594                         DeclarationName name,
595                         const TemplateArgumentLoc *TemplateArgs,
596                         unsigned NumTemplateArgs,
597                         unsigned knownArity);
598   void mangleCastExpression(const Expr *E, StringRef CastEncoding);
599   void mangleInitListElements(const InitListExpr *InitList);
600   void mangleExpression(const Expr *E, unsigned Arity = UnknownArity,
601                         bool AsTemplateArg = false);
602   void mangleCXXCtorType(CXXCtorType T, const CXXRecordDecl *InheritedFrom);
603   void mangleCXXDtorType(CXXDtorType T);
604 
605   void mangleTemplateArgs(TemplateName TN,
606                           const TemplateArgumentLoc *TemplateArgs,
607                           unsigned NumTemplateArgs);
608   void mangleTemplateArgs(TemplateName TN, const TemplateArgument *TemplateArgs,
609                           unsigned NumTemplateArgs);
610   void mangleTemplateArgs(TemplateName TN, const TemplateArgumentList &AL);
611   void mangleTemplateArg(TemplateArgument A, bool NeedExactType);
612   void mangleTemplateArgExpr(const Expr *E);
613   void mangleValueInTemplateArg(QualType T, const APValue &V, bool TopLevel,
614                                 bool NeedExactType = false);
615 
616   void mangleTemplateParameter(unsigned Depth, unsigned Index);
617 
618   void mangleFunctionParam(const ParmVarDecl *parm);
619 
620   void writeAbiTags(const NamedDecl *ND,
621                     const AbiTagList *AdditionalAbiTags);
622 
623   // Returns sorted unique list of ABI tags.
624   AbiTagList makeFunctionReturnTypeTags(const FunctionDecl *FD);
625   // Returns sorted unique list of ABI tags.
626   AbiTagList makeVariableTypeTags(const VarDecl *VD);
627 };
628 
629 }
630 
isInternalLinkageDecl(const NamedDecl * ND)631 static bool isInternalLinkageDecl(const NamedDecl *ND) {
632   if (ND && ND->getFormalLinkage() == InternalLinkage &&
633       !ND->isExternallyVisible() &&
634       getEffectiveDeclContext(ND)->isFileContext() &&
635       !ND->isInAnonymousNamespace())
636     return true;
637   return false;
638 }
639 
640 // Check if this Function Decl needs a unique internal linkage name.
isUniqueInternalLinkageDecl(const NamedDecl * ND)641 bool ItaniumMangleContextImpl::isUniqueInternalLinkageDecl(
642     const NamedDecl *ND) {
643   if (!NeedsUniqueInternalLinkageNames || !ND)
644     return false;
645 
646   const auto *FD = dyn_cast<FunctionDecl>(ND);
647   if (!FD)
648     return false;
649 
650   // For C functions without prototypes, return false as their
651   // names should not be mangled.
652   if (!FD->getType()->getAs<FunctionProtoType>())
653     return false;
654 
655   if (isInternalLinkageDecl(ND))
656     return true;
657 
658   return false;
659 }
660 
shouldMangleCXXName(const NamedDecl * D)661 bool ItaniumMangleContextImpl::shouldMangleCXXName(const NamedDecl *D) {
662   const FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
663   if (FD) {
664     LanguageLinkage L = FD->getLanguageLinkage();
665     // Overloadable functions need mangling.
666     if (FD->hasAttr<OverloadableAttr>())
667       return true;
668 
669     // "main" is not mangled.
670     if (FD->isMain())
671       return false;
672 
673     // The Windows ABI expects that we would never mangle "typical"
674     // user-defined entry points regardless of visibility or freestanding-ness.
675     //
676     // N.B. This is distinct from asking about "main".  "main" has a lot of
677     // special rules associated with it in the standard while these
678     // user-defined entry points are outside of the purview of the standard.
679     // For example, there can be only one definition for "main" in a standards
680     // compliant program; however nothing forbids the existence of wmain and
681     // WinMain in the same translation unit.
682     if (FD->isMSVCRTEntryPoint())
683       return false;
684 
685     // C++ functions and those whose names are not a simple identifier need
686     // mangling.
687     if (!FD->getDeclName().isIdentifier() || L == CXXLanguageLinkage)
688       return true;
689 
690     // C functions are not mangled.
691     if (L == CLanguageLinkage)
692       return false;
693   }
694 
695   // Otherwise, no mangling is done outside C++ mode.
696   if (!getASTContext().getLangOpts().CPlusPlus)
697     return false;
698 
699   const VarDecl *VD = dyn_cast<VarDecl>(D);
700   if (VD && !isa<DecompositionDecl>(D)) {
701     // C variables are not mangled.
702     if (VD->isExternC())
703       return false;
704 
705     // Variables at global scope with non-internal linkage are not mangled
706     const DeclContext *DC = getEffectiveDeclContext(D);
707     // Check for extern variable declared locally.
708     if (DC->isFunctionOrMethod() && D->hasLinkage())
709       while (!DC->isNamespace() && !DC->isTranslationUnit())
710         DC = getEffectiveParentContext(DC);
711     if (DC->isTranslationUnit() && D->getFormalLinkage() != InternalLinkage &&
712         !CXXNameMangler::shouldHaveAbiTags(*this, VD) &&
713         !isa<VarTemplateSpecializationDecl>(D))
714       return false;
715   }
716 
717   return true;
718 }
719 
writeAbiTags(const NamedDecl * ND,const AbiTagList * AdditionalAbiTags)720 void CXXNameMangler::writeAbiTags(const NamedDecl *ND,
721                                   const AbiTagList *AdditionalAbiTags) {
722   assert(AbiTags && "require AbiTagState");
723   AbiTags->write(Out, ND, DisableDerivedAbiTags ? nullptr : AdditionalAbiTags);
724 }
725 
mangleSourceNameWithAbiTags(const NamedDecl * ND,const AbiTagList * AdditionalAbiTags)726 void CXXNameMangler::mangleSourceNameWithAbiTags(
727     const NamedDecl *ND, const AbiTagList *AdditionalAbiTags) {
728   mangleSourceName(ND->getIdentifier());
729   writeAbiTags(ND, AdditionalAbiTags);
730 }
731 
mangle(GlobalDecl GD)732 void CXXNameMangler::mangle(GlobalDecl GD) {
733   // <mangled-name> ::= _Z <encoding>
734   //            ::= <data name>
735   //            ::= <special-name>
736   Out << "_Z";
737   if (isa<FunctionDecl>(GD.getDecl()))
738     mangleFunctionEncoding(GD);
739   else if (isa<VarDecl, FieldDecl, MSGuidDecl, TemplateParamObjectDecl,
740                BindingDecl>(GD.getDecl()))
741     mangleName(GD);
742   else if (const IndirectFieldDecl *IFD =
743                dyn_cast<IndirectFieldDecl>(GD.getDecl()))
744     mangleName(IFD->getAnonField());
745   else
746     llvm_unreachable("unexpected kind of global decl");
747 }
748 
mangleFunctionEncoding(GlobalDecl GD)749 void CXXNameMangler::mangleFunctionEncoding(GlobalDecl GD) {
750   const FunctionDecl *FD = cast<FunctionDecl>(GD.getDecl());
751   // <encoding> ::= <function name> <bare-function-type>
752 
753   // Don't mangle in the type if this isn't a decl we should typically mangle.
754   if (!Context.shouldMangleDeclName(FD)) {
755     mangleName(GD);
756     return;
757   }
758 
759   AbiTagList ReturnTypeAbiTags = makeFunctionReturnTypeTags(FD);
760   if (ReturnTypeAbiTags.empty()) {
761     // There are no tags for return type, the simplest case.
762     mangleName(GD);
763     mangleFunctionEncodingBareType(FD);
764     return;
765   }
766 
767   // Mangle function name and encoding to temporary buffer.
768   // We have to output name and encoding to the same mangler to get the same
769   // substitution as it will be in final mangling.
770   SmallString<256> FunctionEncodingBuf;
771   llvm::raw_svector_ostream FunctionEncodingStream(FunctionEncodingBuf);
772   CXXNameMangler FunctionEncodingMangler(*this, FunctionEncodingStream);
773   // Output name of the function.
774   FunctionEncodingMangler.disableDerivedAbiTags();
775   FunctionEncodingMangler.mangleNameWithAbiTags(FD, nullptr);
776 
777   // Remember length of the function name in the buffer.
778   size_t EncodingPositionStart = FunctionEncodingStream.str().size();
779   FunctionEncodingMangler.mangleFunctionEncodingBareType(FD);
780 
781   // Get tags from return type that are not present in function name or
782   // encoding.
783   const AbiTagList &UsedAbiTags =
784       FunctionEncodingMangler.AbiTagsRoot.getSortedUniqueUsedAbiTags();
785   AbiTagList AdditionalAbiTags(ReturnTypeAbiTags.size());
786   AdditionalAbiTags.erase(
787       std::set_difference(ReturnTypeAbiTags.begin(), ReturnTypeAbiTags.end(),
788                           UsedAbiTags.begin(), UsedAbiTags.end(),
789                           AdditionalAbiTags.begin()),
790       AdditionalAbiTags.end());
791 
792   // Output name with implicit tags and function encoding from temporary buffer.
793   mangleNameWithAbiTags(FD, &AdditionalAbiTags);
794   Out << FunctionEncodingStream.str().substr(EncodingPositionStart);
795 
796   // Function encoding could create new substitutions so we have to add
797   // temp mangled substitutions to main mangler.
798   extendSubstitutions(&FunctionEncodingMangler);
799 }
800 
mangleFunctionEncodingBareType(const FunctionDecl * FD)801 void CXXNameMangler::mangleFunctionEncodingBareType(const FunctionDecl *FD) {
802   if (FD->hasAttr<EnableIfAttr>()) {
803     FunctionTypeDepthState Saved = FunctionTypeDepth.push();
804     Out << "Ua9enable_ifI";
805     for (AttrVec::const_iterator I = FD->getAttrs().begin(),
806                                  E = FD->getAttrs().end();
807          I != E; ++I) {
808       EnableIfAttr *EIA = dyn_cast<EnableIfAttr>(*I);
809       if (!EIA)
810         continue;
811       if (Context.getASTContext().getLangOpts().getClangABICompat() >
812           LangOptions::ClangABI::Ver11) {
813         mangleTemplateArgExpr(EIA->getCond());
814       } else {
815         // Prior to Clang 12, we hardcoded the X/E around enable-if's argument,
816         // even though <template-arg> should not include an X/E around
817         // <expr-primary>.
818         Out << 'X';
819         mangleExpression(EIA->getCond());
820         Out << 'E';
821       }
822     }
823     Out << 'E';
824     FunctionTypeDepth.pop(Saved);
825   }
826 
827   // When mangling an inheriting constructor, the bare function type used is
828   // that of the inherited constructor.
829   if (auto *CD = dyn_cast<CXXConstructorDecl>(FD))
830     if (auto Inherited = CD->getInheritedConstructor())
831       FD = Inherited.getConstructor();
832 
833   // Whether the mangling of a function type includes the return type depends on
834   // the context and the nature of the function. The rules for deciding whether
835   // the return type is included are:
836   //
837   //   1. Template functions (names or types) have return types encoded, with
838   //   the exceptions listed below.
839   //   2. Function types not appearing as part of a function name mangling,
840   //   e.g. parameters, pointer types, etc., have return type encoded, with the
841   //   exceptions listed below.
842   //   3. Non-template function names do not have return types encoded.
843   //
844   // The exceptions mentioned in (1) and (2) above, for which the return type is
845   // never included, are
846   //   1. Constructors.
847   //   2. Destructors.
848   //   3. Conversion operator functions, e.g. operator int.
849   bool MangleReturnType = false;
850   if (FunctionTemplateDecl *PrimaryTemplate = FD->getPrimaryTemplate()) {
851     if (!(isa<CXXConstructorDecl>(FD) || isa<CXXDestructorDecl>(FD) ||
852           isa<CXXConversionDecl>(FD)))
853       MangleReturnType = true;
854 
855     // Mangle the type of the primary template.
856     FD = PrimaryTemplate->getTemplatedDecl();
857   }
858 
859   mangleBareFunctionType(FD->getType()->castAs<FunctionProtoType>(),
860                          MangleReturnType, FD);
861 }
862 
IgnoreLinkageSpecDecls(const DeclContext * DC)863 static const DeclContext *IgnoreLinkageSpecDecls(const DeclContext *DC) {
864   while (isa<LinkageSpecDecl>(DC)) {
865     DC = getEffectiveParentContext(DC);
866   }
867 
868   return DC;
869 }
870 
871 /// Return whether a given namespace is the 'std' namespace.
isStd(const NamespaceDecl * NS)872 static bool isStd(const NamespaceDecl *NS) {
873   if (!IgnoreLinkageSpecDecls(getEffectiveParentContext(NS))
874                                 ->isTranslationUnit())
875     return false;
876 
877   const IdentifierInfo *II = NS->getOriginalNamespace()->getIdentifier();
878   return II && II->isStr("std");
879 }
880 
881 // isStdNamespace - Return whether a given decl context is a toplevel 'std'
882 // namespace.
isStdNamespace(const DeclContext * DC)883 static bool isStdNamespace(const DeclContext *DC) {
884   if (!DC->isNamespace())
885     return false;
886 
887   return isStd(cast<NamespaceDecl>(DC));
888 }
889 
890 static const GlobalDecl
isTemplate(GlobalDecl GD,const TemplateArgumentList * & TemplateArgs)891 isTemplate(GlobalDecl GD, const TemplateArgumentList *&TemplateArgs) {
892   const NamedDecl *ND = cast<NamedDecl>(GD.getDecl());
893   // Check if we have a function template.
894   if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) {
895     if (const TemplateDecl *TD = FD->getPrimaryTemplate()) {
896       TemplateArgs = FD->getTemplateSpecializationArgs();
897       return GD.getWithDecl(TD);
898     }
899   }
900 
901   // Check if we have a class template.
902   if (const ClassTemplateSpecializationDecl *Spec =
903         dyn_cast<ClassTemplateSpecializationDecl>(ND)) {
904     TemplateArgs = &Spec->getTemplateArgs();
905     return GD.getWithDecl(Spec->getSpecializedTemplate());
906   }
907 
908   // Check if we have a variable template.
909   if (const VarTemplateSpecializationDecl *Spec =
910           dyn_cast<VarTemplateSpecializationDecl>(ND)) {
911     TemplateArgs = &Spec->getTemplateArgs();
912     return GD.getWithDecl(Spec->getSpecializedTemplate());
913   }
914 
915   return GlobalDecl();
916 }
917 
asTemplateName(GlobalDecl GD)918 static TemplateName asTemplateName(GlobalDecl GD) {
919   const TemplateDecl *TD = dyn_cast_or_null<TemplateDecl>(GD.getDecl());
920   return TemplateName(const_cast<TemplateDecl*>(TD));
921 }
922 
mangleName(GlobalDecl GD)923 void CXXNameMangler::mangleName(GlobalDecl GD) {
924   const NamedDecl *ND = cast<NamedDecl>(GD.getDecl());
925   if (const VarDecl *VD = dyn_cast<VarDecl>(ND)) {
926     // Variables should have implicit tags from its type.
927     AbiTagList VariableTypeAbiTags = makeVariableTypeTags(VD);
928     if (VariableTypeAbiTags.empty()) {
929       // Simple case no variable type tags.
930       mangleNameWithAbiTags(VD, nullptr);
931       return;
932     }
933 
934     // Mangle variable name to null stream to collect tags.
935     llvm::raw_null_ostream NullOutStream;
936     CXXNameMangler VariableNameMangler(*this, NullOutStream);
937     VariableNameMangler.disableDerivedAbiTags();
938     VariableNameMangler.mangleNameWithAbiTags(VD, nullptr);
939 
940     // Get tags from variable type that are not present in its name.
941     const AbiTagList &UsedAbiTags =
942         VariableNameMangler.AbiTagsRoot.getSortedUniqueUsedAbiTags();
943     AbiTagList AdditionalAbiTags(VariableTypeAbiTags.size());
944     AdditionalAbiTags.erase(
945         std::set_difference(VariableTypeAbiTags.begin(),
946                             VariableTypeAbiTags.end(), UsedAbiTags.begin(),
947                             UsedAbiTags.end(), AdditionalAbiTags.begin()),
948         AdditionalAbiTags.end());
949 
950     // Output name with implicit tags.
951     mangleNameWithAbiTags(VD, &AdditionalAbiTags);
952   } else {
953     mangleNameWithAbiTags(GD, nullptr);
954   }
955 }
956 
mangleNameWithAbiTags(GlobalDecl GD,const AbiTagList * AdditionalAbiTags)957 void CXXNameMangler::mangleNameWithAbiTags(GlobalDecl GD,
958                                            const AbiTagList *AdditionalAbiTags) {
959   const NamedDecl *ND = cast<NamedDecl>(GD.getDecl());
960   //  <name> ::= [<module-name>] <nested-name>
961   //         ::= [<module-name>] <unscoped-name>
962   //         ::= [<module-name>] <unscoped-template-name> <template-args>
963   //         ::= <local-name>
964   //
965   const DeclContext *DC = getEffectiveDeclContext(ND);
966 
967   // If this is an extern variable declared locally, the relevant DeclContext
968   // is that of the containing namespace, or the translation unit.
969   // FIXME: This is a hack; extern variables declared locally should have
970   // a proper semantic declaration context!
971   if (isLocalContainerContext(DC) && ND->hasLinkage() && !isLambda(ND))
972     while (!DC->isNamespace() && !DC->isTranslationUnit())
973       DC = getEffectiveParentContext(DC);
974   else if (GetLocalClassDecl(ND)) {
975     mangleLocalName(GD, AdditionalAbiTags);
976     return;
977   }
978 
979   DC = IgnoreLinkageSpecDecls(DC);
980 
981   if (isLocalContainerContext(DC)) {
982     mangleLocalName(GD, AdditionalAbiTags);
983     return;
984   }
985 
986   // Do not mangle the owning module for an external linkage declaration.
987   // This enables backwards-compatibility with non-modular code, and is
988   // a valid choice since conflicts are not permitted by C++ Modules TS
989   // [basic.def.odr]/6.2.
990   if (!ND->hasExternalFormalLinkage())
991     if (Module *M = ND->getOwningModuleForLinkage())
992       mangleModuleName(M);
993 
994   // Closures can require a nested-name mangling even if they're semantically
995   // in the global namespace.
996   if (const NamedDecl *PrefixND = getClosurePrefix(ND)) {
997     mangleNestedNameWithClosurePrefix(GD, PrefixND, AdditionalAbiTags);
998     return;
999   }
1000 
1001   if (DC->isTranslationUnit() || isStdNamespace(DC)) {
1002     // Check if we have a template.
1003     const TemplateArgumentList *TemplateArgs = nullptr;
1004     if (GlobalDecl TD = isTemplate(GD, TemplateArgs)) {
1005       mangleUnscopedTemplateName(TD, AdditionalAbiTags);
1006       mangleTemplateArgs(asTemplateName(TD), *TemplateArgs);
1007       return;
1008     }
1009 
1010     mangleUnscopedName(GD, AdditionalAbiTags);
1011     return;
1012   }
1013 
1014   mangleNestedName(GD, DC, AdditionalAbiTags);
1015 }
1016 
mangleModuleName(const Module * M)1017 void CXXNameMangler::mangleModuleName(const Module *M) {
1018   // Implement the C++ Modules TS name mangling proposal; see
1019   //     https://gcc.gnu.org/wiki/cxx-modules?action=AttachFile
1020   //
1021   //   <module-name> ::= W <unscoped-name>+ E
1022   //                 ::= W <module-subst> <unscoped-name>* E
1023   Out << 'W';
1024   mangleModuleNamePrefix(M->Name);
1025   Out << 'E';
1026 }
1027 
mangleModuleNamePrefix(StringRef Name)1028 void CXXNameMangler::mangleModuleNamePrefix(StringRef Name) {
1029   //  <module-subst> ::= _ <seq-id>          # 0 < seq-id < 10
1030   //                 ::= W <seq-id - 10> _   # otherwise
1031   auto It = ModuleSubstitutions.find(Name);
1032   if (It != ModuleSubstitutions.end()) {
1033     if (It->second < 10)
1034       Out << '_' << static_cast<char>('0' + It->second);
1035     else
1036       Out << 'W' << (It->second - 10) << '_';
1037     return;
1038   }
1039 
1040   // FIXME: Preserve hierarchy in module names rather than flattening
1041   // them to strings; use Module*s as substitution keys.
1042   auto Parts = Name.rsplit('.');
1043   if (Parts.second.empty())
1044     Parts.second = Parts.first;
1045   else
1046     mangleModuleNamePrefix(Parts.first);
1047 
1048   Out << Parts.second.size() << Parts.second;
1049   ModuleSubstitutions.insert({Name, ModuleSubstitutions.size()});
1050 }
1051 
mangleTemplateName(const TemplateDecl * TD,const TemplateArgument * TemplateArgs,unsigned NumTemplateArgs)1052 void CXXNameMangler::mangleTemplateName(const TemplateDecl *TD,
1053                                         const TemplateArgument *TemplateArgs,
1054                                         unsigned NumTemplateArgs) {
1055   const DeclContext *DC = IgnoreLinkageSpecDecls(getEffectiveDeclContext(TD));
1056 
1057   if (DC->isTranslationUnit() || isStdNamespace(DC)) {
1058     mangleUnscopedTemplateName(TD, nullptr);
1059     mangleTemplateArgs(asTemplateName(TD), TemplateArgs, NumTemplateArgs);
1060   } else {
1061     mangleNestedName(TD, TemplateArgs, NumTemplateArgs);
1062   }
1063 }
1064 
mangleUnscopedName(GlobalDecl GD,const AbiTagList * AdditionalAbiTags)1065 void CXXNameMangler::mangleUnscopedName(GlobalDecl GD,
1066                                         const AbiTagList *AdditionalAbiTags) {
1067   const NamedDecl *ND = cast<NamedDecl>(GD.getDecl());
1068   //  <unscoped-name> ::= <unqualified-name>
1069   //                  ::= St <unqualified-name>   # ::std::
1070 
1071   if (isStdNamespace(IgnoreLinkageSpecDecls(getEffectiveDeclContext(ND))))
1072     Out << "St";
1073 
1074   mangleUnqualifiedName(GD, AdditionalAbiTags);
1075 }
1076 
mangleUnscopedTemplateName(GlobalDecl GD,const AbiTagList * AdditionalAbiTags)1077 void CXXNameMangler::mangleUnscopedTemplateName(
1078     GlobalDecl GD, const AbiTagList *AdditionalAbiTags) {
1079   const TemplateDecl *ND = cast<TemplateDecl>(GD.getDecl());
1080   //     <unscoped-template-name> ::= <unscoped-name>
1081   //                              ::= <substitution>
1082   if (mangleSubstitution(ND))
1083     return;
1084 
1085   // <template-template-param> ::= <template-param>
1086   if (const auto *TTP = dyn_cast<TemplateTemplateParmDecl>(ND)) {
1087     assert(!AdditionalAbiTags &&
1088            "template template param cannot have abi tags");
1089     mangleTemplateParameter(TTP->getDepth(), TTP->getIndex());
1090   } else if (isa<BuiltinTemplateDecl>(ND) || isa<ConceptDecl>(ND)) {
1091     mangleUnscopedName(GD, AdditionalAbiTags);
1092   } else {
1093     mangleUnscopedName(GD.getWithDecl(ND->getTemplatedDecl()), AdditionalAbiTags);
1094   }
1095 
1096   addSubstitution(ND);
1097 }
1098 
mangleFloat(const llvm::APFloat & f)1099 void CXXNameMangler::mangleFloat(const llvm::APFloat &f) {
1100   // ABI:
1101   //   Floating-point literals are encoded using a fixed-length
1102   //   lowercase hexadecimal string corresponding to the internal
1103   //   representation (IEEE on Itanium), high-order bytes first,
1104   //   without leading zeroes. For example: "Lf bf800000 E" is -1.0f
1105   //   on Itanium.
1106   // The 'without leading zeroes' thing seems to be an editorial
1107   // mistake; see the discussion on cxx-abi-dev beginning on
1108   // 2012-01-16.
1109 
1110   // Our requirements here are just barely weird enough to justify
1111   // using a custom algorithm instead of post-processing APInt::toString().
1112 
1113   llvm::APInt valueBits = f.bitcastToAPInt();
1114   unsigned numCharacters = (valueBits.getBitWidth() + 3) / 4;
1115   assert(numCharacters != 0);
1116 
1117   // Allocate a buffer of the right number of characters.
1118   SmallVector<char, 20> buffer(numCharacters);
1119 
1120   // Fill the buffer left-to-right.
1121   for (unsigned stringIndex = 0; stringIndex != numCharacters; ++stringIndex) {
1122     // The bit-index of the next hex digit.
1123     unsigned digitBitIndex = 4 * (numCharacters - stringIndex - 1);
1124 
1125     // Project out 4 bits starting at 'digitIndex'.
1126     uint64_t hexDigit = valueBits.getRawData()[digitBitIndex / 64];
1127     hexDigit >>= (digitBitIndex % 64);
1128     hexDigit &= 0xF;
1129 
1130     // Map that over to a lowercase hex digit.
1131     static const char charForHex[16] = {
1132       '0', '1', '2', '3', '4', '5', '6', '7',
1133       '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'
1134     };
1135     buffer[stringIndex] = charForHex[hexDigit];
1136   }
1137 
1138   Out.write(buffer.data(), numCharacters);
1139 }
1140 
mangleFloatLiteral(QualType T,const llvm::APFloat & V)1141 void CXXNameMangler::mangleFloatLiteral(QualType T, const llvm::APFloat &V) {
1142   Out << 'L';
1143   mangleType(T);
1144   mangleFloat(V);
1145   Out << 'E';
1146 }
1147 
mangleFixedPointLiteral()1148 void CXXNameMangler::mangleFixedPointLiteral() {
1149   DiagnosticsEngine &Diags = Context.getDiags();
1150   unsigned DiagID = Diags.getCustomDiagID(
1151       DiagnosticsEngine::Error, "cannot mangle fixed point literals yet");
1152   Diags.Report(DiagID);
1153 }
1154 
mangleNullPointer(QualType T)1155 void CXXNameMangler::mangleNullPointer(QualType T) {
1156   //  <expr-primary> ::= L <type> 0 E
1157   Out << 'L';
1158   mangleType(T);
1159   Out << "0E";
1160 }
1161 
mangleNumber(const llvm::APSInt & Value)1162 void CXXNameMangler::mangleNumber(const llvm::APSInt &Value) {
1163   if (Value.isSigned() && Value.isNegative()) {
1164     Out << 'n';
1165     Value.abs().print(Out, /*signed*/ false);
1166   } else {
1167     Value.print(Out, /*signed*/ false);
1168   }
1169 }
1170 
mangleNumber(int64_t Number)1171 void CXXNameMangler::mangleNumber(int64_t Number) {
1172   //  <number> ::= [n] <non-negative decimal integer>
1173   if (Number < 0) {
1174     Out << 'n';
1175     Number = -Number;
1176   }
1177 
1178   Out << Number;
1179 }
1180 
mangleCallOffset(int64_t NonVirtual,int64_t Virtual)1181 void CXXNameMangler::mangleCallOffset(int64_t NonVirtual, int64_t Virtual) {
1182   //  <call-offset>  ::= h <nv-offset> _
1183   //                 ::= v <v-offset> _
1184   //  <nv-offset>    ::= <offset number>        # non-virtual base override
1185   //  <v-offset>     ::= <offset number> _ <virtual offset number>
1186   //                      # virtual base override, with vcall offset
1187   if (!Virtual) {
1188     Out << 'h';
1189     mangleNumber(NonVirtual);
1190     Out << '_';
1191     return;
1192   }
1193 
1194   Out << 'v';
1195   mangleNumber(NonVirtual);
1196   Out << '_';
1197   mangleNumber(Virtual);
1198   Out << '_';
1199 }
1200 
manglePrefix(QualType type)1201 void CXXNameMangler::manglePrefix(QualType type) {
1202   if (const auto *TST = type->getAs<TemplateSpecializationType>()) {
1203     if (!mangleSubstitution(QualType(TST, 0))) {
1204       mangleTemplatePrefix(TST->getTemplateName());
1205 
1206       // FIXME: GCC does not appear to mangle the template arguments when
1207       // the template in question is a dependent template name. Should we
1208       // emulate that badness?
1209       mangleTemplateArgs(TST->getTemplateName(), TST->getArgs(),
1210                          TST->getNumArgs());
1211       addSubstitution(QualType(TST, 0));
1212     }
1213   } else if (const auto *DTST =
1214                  type->getAs<DependentTemplateSpecializationType>()) {
1215     if (!mangleSubstitution(QualType(DTST, 0))) {
1216       TemplateName Template = getASTContext().getDependentTemplateName(
1217           DTST->getQualifier(), DTST->getIdentifier());
1218       mangleTemplatePrefix(Template);
1219 
1220       // FIXME: GCC does not appear to mangle the template arguments when
1221       // the template in question is a dependent template name. Should we
1222       // emulate that badness?
1223       mangleTemplateArgs(Template, DTST->getArgs(), DTST->getNumArgs());
1224       addSubstitution(QualType(DTST, 0));
1225     }
1226   } else {
1227     // We use the QualType mangle type variant here because it handles
1228     // substitutions.
1229     mangleType(type);
1230   }
1231 }
1232 
1233 /// Mangle everything prior to the base-unresolved-name in an unresolved-name.
1234 ///
1235 /// \param recursive - true if this is being called recursively,
1236 ///   i.e. if there is more prefix "to the right".
mangleUnresolvedPrefix(NestedNameSpecifier * qualifier,bool recursive)1237 void CXXNameMangler::mangleUnresolvedPrefix(NestedNameSpecifier *qualifier,
1238                                             bool recursive) {
1239 
1240   // x, ::x
1241   // <unresolved-name> ::= [gs] <base-unresolved-name>
1242 
1243   // T::x / decltype(p)::x
1244   // <unresolved-name> ::= sr <unresolved-type> <base-unresolved-name>
1245 
1246   // T::N::x /decltype(p)::N::x
1247   // <unresolved-name> ::= srN <unresolved-type> <unresolved-qualifier-level>+ E
1248   //                       <base-unresolved-name>
1249 
1250   // A::x, N::y, A<T>::z; "gs" means leading "::"
1251   // <unresolved-name> ::= [gs] sr <unresolved-qualifier-level>+ E
1252   //                       <base-unresolved-name>
1253 
1254   switch (qualifier->getKind()) {
1255   case NestedNameSpecifier::Global:
1256     Out << "gs";
1257 
1258     // We want an 'sr' unless this is the entire NNS.
1259     if (recursive)
1260       Out << "sr";
1261 
1262     // We never want an 'E' here.
1263     return;
1264 
1265   case NestedNameSpecifier::Super:
1266     llvm_unreachable("Can't mangle __super specifier");
1267 
1268   case NestedNameSpecifier::Namespace:
1269     if (qualifier->getPrefix())
1270       mangleUnresolvedPrefix(qualifier->getPrefix(),
1271                              /*recursive*/ true);
1272     else
1273       Out << "sr";
1274     mangleSourceNameWithAbiTags(qualifier->getAsNamespace());
1275     break;
1276   case NestedNameSpecifier::NamespaceAlias:
1277     if (qualifier->getPrefix())
1278       mangleUnresolvedPrefix(qualifier->getPrefix(),
1279                              /*recursive*/ true);
1280     else
1281       Out << "sr";
1282     mangleSourceNameWithAbiTags(qualifier->getAsNamespaceAlias());
1283     break;
1284 
1285   case NestedNameSpecifier::TypeSpec:
1286   case NestedNameSpecifier::TypeSpecWithTemplate: {
1287     const Type *type = qualifier->getAsType();
1288 
1289     // We only want to use an unresolved-type encoding if this is one of:
1290     //   - a decltype
1291     //   - a template type parameter
1292     //   - a template template parameter with arguments
1293     // In all of these cases, we should have no prefix.
1294     if (qualifier->getPrefix()) {
1295       mangleUnresolvedPrefix(qualifier->getPrefix(),
1296                              /*recursive*/ true);
1297     } else {
1298       // Otherwise, all the cases want this.
1299       Out << "sr";
1300     }
1301 
1302     if (mangleUnresolvedTypeOrSimpleId(QualType(type, 0), recursive ? "N" : ""))
1303       return;
1304 
1305     break;
1306   }
1307 
1308   case NestedNameSpecifier::Identifier:
1309     // Member expressions can have these without prefixes.
1310     if (qualifier->getPrefix())
1311       mangleUnresolvedPrefix(qualifier->getPrefix(),
1312                              /*recursive*/ true);
1313     else
1314       Out << "sr";
1315 
1316     mangleSourceName(qualifier->getAsIdentifier());
1317     // An Identifier has no type information, so we can't emit abi tags for it.
1318     break;
1319   }
1320 
1321   // If this was the innermost part of the NNS, and we fell out to
1322   // here, append an 'E'.
1323   if (!recursive)
1324     Out << 'E';
1325 }
1326 
1327 /// Mangle an unresolved-name, which is generally used for names which
1328 /// weren't resolved to specific entities.
mangleUnresolvedName(NestedNameSpecifier * qualifier,DeclarationName name,const TemplateArgumentLoc * TemplateArgs,unsigned NumTemplateArgs,unsigned knownArity)1329 void CXXNameMangler::mangleUnresolvedName(
1330     NestedNameSpecifier *qualifier, DeclarationName name,
1331     const TemplateArgumentLoc *TemplateArgs, unsigned NumTemplateArgs,
1332     unsigned knownArity) {
1333   if (qualifier) mangleUnresolvedPrefix(qualifier);
1334   switch (name.getNameKind()) {
1335     // <base-unresolved-name> ::= <simple-id>
1336     case DeclarationName::Identifier:
1337       mangleSourceName(name.getAsIdentifierInfo());
1338       break;
1339     // <base-unresolved-name> ::= dn <destructor-name>
1340     case DeclarationName::CXXDestructorName:
1341       Out << "dn";
1342       mangleUnresolvedTypeOrSimpleId(name.getCXXNameType());
1343       break;
1344     // <base-unresolved-name> ::= on <operator-name>
1345     case DeclarationName::CXXConversionFunctionName:
1346     case DeclarationName::CXXLiteralOperatorName:
1347     case DeclarationName::CXXOperatorName:
1348       Out << "on";
1349       mangleOperatorName(name, knownArity);
1350       break;
1351     case DeclarationName::CXXConstructorName:
1352       llvm_unreachable("Can't mangle a constructor name!");
1353     case DeclarationName::CXXUsingDirective:
1354       llvm_unreachable("Can't mangle a using directive name!");
1355     case DeclarationName::CXXDeductionGuideName:
1356       llvm_unreachable("Can't mangle a deduction guide name!");
1357     case DeclarationName::ObjCMultiArgSelector:
1358     case DeclarationName::ObjCOneArgSelector:
1359     case DeclarationName::ObjCZeroArgSelector:
1360       llvm_unreachable("Can't mangle Objective-C selector names here!");
1361   }
1362 
1363   // The <simple-id> and on <operator-name> productions end in an optional
1364   // <template-args>.
1365   if (TemplateArgs)
1366     mangleTemplateArgs(TemplateName(), TemplateArgs, NumTemplateArgs);
1367 }
1368 
mangleUnqualifiedName(GlobalDecl GD,DeclarationName Name,unsigned KnownArity,const AbiTagList * AdditionalAbiTags)1369 void CXXNameMangler::mangleUnqualifiedName(GlobalDecl GD,
1370                                            DeclarationName Name,
1371                                            unsigned KnownArity,
1372                                            const AbiTagList *AdditionalAbiTags) {
1373   const NamedDecl *ND = cast_or_null<NamedDecl>(GD.getDecl());
1374   unsigned Arity = KnownArity;
1375   //  <unqualified-name> ::= <operator-name>
1376   //                     ::= <ctor-dtor-name>
1377   //                     ::= <source-name>
1378   switch (Name.getNameKind()) {
1379   case DeclarationName::Identifier: {
1380     const IdentifierInfo *II = Name.getAsIdentifierInfo();
1381 
1382     // We mangle decomposition declarations as the names of their bindings.
1383     if (auto *DD = dyn_cast<DecompositionDecl>(ND)) {
1384       // FIXME: Non-standard mangling for decomposition declarations:
1385       //
1386       //  <unqualified-name> ::= DC <source-name>* E
1387       //
1388       // These can never be referenced across translation units, so we do
1389       // not need a cross-vendor mangling for anything other than demanglers.
1390       // Proposed on cxx-abi-dev on 2016-08-12
1391       Out << "DC";
1392       for (auto *BD : DD->bindings())
1393         mangleSourceName(BD->getDeclName().getAsIdentifierInfo());
1394       Out << 'E';
1395       writeAbiTags(ND, AdditionalAbiTags);
1396       break;
1397     }
1398 
1399     if (auto *GD = dyn_cast<MSGuidDecl>(ND)) {
1400       // We follow MSVC in mangling GUID declarations as if they were variables
1401       // with a particular reserved name. Continue the pretense here.
1402       SmallString<sizeof("_GUID_12345678_1234_1234_1234_1234567890ab")> GUID;
1403       llvm::raw_svector_ostream GUIDOS(GUID);
1404       Context.mangleMSGuidDecl(GD, GUIDOS);
1405       Out << GUID.size() << GUID;
1406       break;
1407     }
1408 
1409     if (auto *TPO = dyn_cast<TemplateParamObjectDecl>(ND)) {
1410       // Proposed in https://github.com/itanium-cxx-abi/cxx-abi/issues/63.
1411       Out << "TA";
1412       mangleValueInTemplateArg(TPO->getType().getUnqualifiedType(),
1413                                TPO->getValue(), /*TopLevel=*/true);
1414       break;
1415     }
1416 
1417     if (II) {
1418       // Match GCC's naming convention for internal linkage symbols, for
1419       // symbols that are not actually visible outside of this TU. GCC
1420       // distinguishes between internal and external linkage symbols in
1421       // its mangling, to support cases like this that were valid C++ prior
1422       // to DR426:
1423       //
1424       //   void test() { extern void foo(); }
1425       //   static void foo();
1426       //
1427       // Don't bother with the L marker for names in anonymous namespaces; the
1428       // 12_GLOBAL__N_1 mangling is quite sufficient there, and this better
1429       // matches GCC anyway, because GCC does not treat anonymous namespaces as
1430       // implying internal linkage.
1431       if (isInternalLinkageDecl(ND))
1432         Out << 'L';
1433 
1434       auto *FD = dyn_cast<FunctionDecl>(ND);
1435       bool IsRegCall = FD &&
1436                        FD->getType()->castAs<FunctionType>()->getCallConv() ==
1437                            clang::CC_X86RegCall;
1438       bool IsDeviceStub =
1439           FD && FD->hasAttr<CUDAGlobalAttr>() &&
1440           GD.getKernelReferenceKind() == KernelReferenceKind::Stub;
1441       if (IsDeviceStub)
1442         mangleDeviceStubName(II);
1443       else if (IsRegCall)
1444         mangleRegCallName(II);
1445       else
1446         mangleSourceName(II);
1447 
1448       writeAbiTags(ND, AdditionalAbiTags);
1449       break;
1450     }
1451 
1452     // Otherwise, an anonymous entity.  We must have a declaration.
1453     assert(ND && "mangling empty name without declaration");
1454 
1455     if (const NamespaceDecl *NS = dyn_cast<NamespaceDecl>(ND)) {
1456       if (NS->isAnonymousNamespace()) {
1457         // This is how gcc mangles these names.
1458         Out << "12_GLOBAL__N_1";
1459         break;
1460       }
1461     }
1462 
1463     if (const VarDecl *VD = dyn_cast<VarDecl>(ND)) {
1464       // We must have an anonymous union or struct declaration.
1465       const RecordDecl *RD = VD->getType()->castAs<RecordType>()->getDecl();
1466 
1467       // Itanium C++ ABI 5.1.2:
1468       //
1469       //   For the purposes of mangling, the name of an anonymous union is
1470       //   considered to be the name of the first named data member found by a
1471       //   pre-order, depth-first, declaration-order walk of the data members of
1472       //   the anonymous union. If there is no such data member (i.e., if all of
1473       //   the data members in the union are unnamed), then there is no way for
1474       //   a program to refer to the anonymous union, and there is therefore no
1475       //   need to mangle its name.
1476       assert(RD->isAnonymousStructOrUnion()
1477              && "Expected anonymous struct or union!");
1478       const FieldDecl *FD = RD->findFirstNamedDataMember();
1479 
1480       // It's actually possible for various reasons for us to get here
1481       // with an empty anonymous struct / union.  Fortunately, it
1482       // doesn't really matter what name we generate.
1483       if (!FD) break;
1484       assert(FD->getIdentifier() && "Data member name isn't an identifier!");
1485 
1486       mangleSourceName(FD->getIdentifier());
1487       // Not emitting abi tags: internal name anyway.
1488       break;
1489     }
1490 
1491     // Class extensions have no name as a category, and it's possible
1492     // for them to be the semantic parent of certain declarations
1493     // (primarily, tag decls defined within declarations).  Such
1494     // declarations will always have internal linkage, so the name
1495     // doesn't really matter, but we shouldn't crash on them.  For
1496     // safety, just handle all ObjC containers here.
1497     if (isa<ObjCContainerDecl>(ND))
1498       break;
1499 
1500     // We must have an anonymous struct.
1501     const TagDecl *TD = cast<TagDecl>(ND);
1502     if (const TypedefNameDecl *D = TD->getTypedefNameForAnonDecl()) {
1503       assert(TD->getDeclContext() == D->getDeclContext() &&
1504              "Typedef should not be in another decl context!");
1505       assert(D->getDeclName().getAsIdentifierInfo() &&
1506              "Typedef was not named!");
1507       mangleSourceName(D->getDeclName().getAsIdentifierInfo());
1508       assert(!AdditionalAbiTags && "Type cannot have additional abi tags");
1509       // Explicit abi tags are still possible; take from underlying type, not
1510       // from typedef.
1511       writeAbiTags(TD, nullptr);
1512       break;
1513     }
1514 
1515     // <unnamed-type-name> ::= <closure-type-name>
1516     //
1517     // <closure-type-name> ::= Ul <lambda-sig> E [ <nonnegative number> ] _
1518     // <lambda-sig> ::= <template-param-decl>* <parameter-type>+
1519     //     # Parameter types or 'v' for 'void'.
1520     if (const CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(TD)) {
1521       if (Record->isLambda() && (Record->getLambdaManglingNumber() ||
1522                                  Context.getDiscriminatorOverride()(
1523                                      Context.getASTContext(), Record))) {
1524         assert(!AdditionalAbiTags &&
1525                "Lambda type cannot have additional abi tags");
1526         mangleLambda(Record);
1527         break;
1528       }
1529     }
1530 
1531     if (TD->isExternallyVisible()) {
1532       unsigned UnnamedMangle = getASTContext().getManglingNumber(TD);
1533       Out << "Ut";
1534       if (UnnamedMangle > 1)
1535         Out << UnnamedMangle - 2;
1536       Out << '_';
1537       writeAbiTags(TD, AdditionalAbiTags);
1538       break;
1539     }
1540 
1541     // Get a unique id for the anonymous struct. If it is not a real output
1542     // ID doesn't matter so use fake one.
1543     unsigned AnonStructId = NullOut ? 0 : Context.getAnonymousStructId(TD);
1544 
1545     // Mangle it as a source name in the form
1546     // [n] $_<id>
1547     // where n is the length of the string.
1548     SmallString<8> Str;
1549     Str += "$_";
1550     Str += llvm::utostr(AnonStructId);
1551 
1552     Out << Str.size();
1553     Out << Str;
1554     break;
1555   }
1556 
1557   case DeclarationName::ObjCZeroArgSelector:
1558   case DeclarationName::ObjCOneArgSelector:
1559   case DeclarationName::ObjCMultiArgSelector:
1560     llvm_unreachable("Can't mangle Objective-C selector names here!");
1561 
1562   case DeclarationName::CXXConstructorName: {
1563     const CXXRecordDecl *InheritedFrom = nullptr;
1564     TemplateName InheritedTemplateName;
1565     const TemplateArgumentList *InheritedTemplateArgs = nullptr;
1566     if (auto Inherited =
1567             cast<CXXConstructorDecl>(ND)->getInheritedConstructor()) {
1568       InheritedFrom = Inherited.getConstructor()->getParent();
1569       InheritedTemplateName =
1570           TemplateName(Inherited.getConstructor()->getPrimaryTemplate());
1571       InheritedTemplateArgs =
1572           Inherited.getConstructor()->getTemplateSpecializationArgs();
1573     }
1574 
1575     if (ND == Structor)
1576       // If the named decl is the C++ constructor we're mangling, use the type
1577       // we were given.
1578       mangleCXXCtorType(static_cast<CXXCtorType>(StructorType), InheritedFrom);
1579     else
1580       // Otherwise, use the complete constructor name. This is relevant if a
1581       // class with a constructor is declared within a constructor.
1582       mangleCXXCtorType(Ctor_Complete, InheritedFrom);
1583 
1584     // FIXME: The template arguments are part of the enclosing prefix or
1585     // nested-name, but it's more convenient to mangle them here.
1586     if (InheritedTemplateArgs)
1587       mangleTemplateArgs(InheritedTemplateName, *InheritedTemplateArgs);
1588 
1589     writeAbiTags(ND, AdditionalAbiTags);
1590     break;
1591   }
1592 
1593   case DeclarationName::CXXDestructorName:
1594     if (ND == Structor)
1595       // If the named decl is the C++ destructor we're mangling, use the type we
1596       // were given.
1597       mangleCXXDtorType(static_cast<CXXDtorType>(StructorType));
1598     else
1599       // Otherwise, use the complete destructor name. This is relevant if a
1600       // class with a destructor is declared within a destructor.
1601       mangleCXXDtorType(Dtor_Complete);
1602     writeAbiTags(ND, AdditionalAbiTags);
1603     break;
1604 
1605   case DeclarationName::CXXOperatorName:
1606     if (ND && Arity == UnknownArity) {
1607       Arity = cast<FunctionDecl>(ND)->getNumParams();
1608 
1609       // If we have a member function, we need to include the 'this' pointer.
1610       if (const auto *MD = dyn_cast<CXXMethodDecl>(ND))
1611         if (!MD->isStatic())
1612           Arity++;
1613     }
1614     LLVM_FALLTHROUGH;
1615   case DeclarationName::CXXConversionFunctionName:
1616   case DeclarationName::CXXLiteralOperatorName:
1617     mangleOperatorName(Name, Arity);
1618     writeAbiTags(ND, AdditionalAbiTags);
1619     break;
1620 
1621   case DeclarationName::CXXDeductionGuideName:
1622     llvm_unreachable("Can't mangle a deduction guide name!");
1623 
1624   case DeclarationName::CXXUsingDirective:
1625     llvm_unreachable("Can't mangle a using directive name!");
1626   }
1627 }
1628 
mangleRegCallName(const IdentifierInfo * II)1629 void CXXNameMangler::mangleRegCallName(const IdentifierInfo *II) {
1630   // <source-name> ::= <positive length number> __regcall3__ <identifier>
1631   // <number> ::= [n] <non-negative decimal integer>
1632   // <identifier> ::= <unqualified source code identifier>
1633   Out << II->getLength() + sizeof("__regcall3__") - 1 << "__regcall3__"
1634       << II->getName();
1635 }
1636 
mangleDeviceStubName(const IdentifierInfo * II)1637 void CXXNameMangler::mangleDeviceStubName(const IdentifierInfo *II) {
1638   // <source-name> ::= <positive length number> __device_stub__ <identifier>
1639   // <number> ::= [n] <non-negative decimal integer>
1640   // <identifier> ::= <unqualified source code identifier>
1641   Out << II->getLength() + sizeof("__device_stub__") - 1 << "__device_stub__"
1642       << II->getName();
1643 }
1644 
mangleSourceName(const IdentifierInfo * II)1645 void CXXNameMangler::mangleSourceName(const IdentifierInfo *II) {
1646   // <source-name> ::= <positive length number> <identifier>
1647   // <number> ::= [n] <non-negative decimal integer>
1648   // <identifier> ::= <unqualified source code identifier>
1649   Out << II->getLength() << II->getName();
1650 }
1651 
mangleNestedName(GlobalDecl GD,const DeclContext * DC,const AbiTagList * AdditionalAbiTags,bool NoFunction)1652 void CXXNameMangler::mangleNestedName(GlobalDecl GD,
1653                                       const DeclContext *DC,
1654                                       const AbiTagList *AdditionalAbiTags,
1655                                       bool NoFunction) {
1656   const NamedDecl *ND = cast<NamedDecl>(GD.getDecl());
1657   // <nested-name>
1658   //   ::= N [<CV-qualifiers>] [<ref-qualifier>] <prefix> <unqualified-name> E
1659   //   ::= N [<CV-qualifiers>] [<ref-qualifier>] <template-prefix>
1660   //       <template-args> E
1661 
1662   Out << 'N';
1663   if (const CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(ND)) {
1664     Qualifiers MethodQuals = Method->getMethodQualifiers();
1665     // We do not consider restrict a distinguishing attribute for overloading
1666     // purposes so we must not mangle it.
1667     MethodQuals.removeRestrict();
1668     mangleQualifiers(MethodQuals);
1669     mangleRefQualifier(Method->getRefQualifier());
1670   }
1671 
1672   // Check if we have a template.
1673   const TemplateArgumentList *TemplateArgs = nullptr;
1674   if (GlobalDecl TD = isTemplate(GD, TemplateArgs)) {
1675     mangleTemplatePrefix(TD, NoFunction);
1676     mangleTemplateArgs(asTemplateName(TD), *TemplateArgs);
1677   } else {
1678     manglePrefix(DC, NoFunction);
1679     mangleUnqualifiedName(GD, AdditionalAbiTags);
1680   }
1681 
1682   Out << 'E';
1683 }
mangleNestedName(const TemplateDecl * TD,const TemplateArgument * TemplateArgs,unsigned NumTemplateArgs)1684 void CXXNameMangler::mangleNestedName(const TemplateDecl *TD,
1685                                       const TemplateArgument *TemplateArgs,
1686                                       unsigned NumTemplateArgs) {
1687   // <nested-name> ::= N [<CV-qualifiers>] <template-prefix> <template-args> E
1688 
1689   Out << 'N';
1690 
1691   mangleTemplatePrefix(TD);
1692   mangleTemplateArgs(asTemplateName(TD), TemplateArgs, NumTemplateArgs);
1693 
1694   Out << 'E';
1695 }
1696 
mangleNestedNameWithClosurePrefix(GlobalDecl GD,const NamedDecl * PrefixND,const AbiTagList * AdditionalAbiTags)1697 void CXXNameMangler::mangleNestedNameWithClosurePrefix(
1698     GlobalDecl GD, const NamedDecl *PrefixND,
1699     const AbiTagList *AdditionalAbiTags) {
1700   // A <closure-prefix> represents a variable or field, not a regular
1701   // DeclContext, so needs special handling. In this case we're mangling a
1702   // limited form of <nested-name>:
1703   //
1704   // <nested-name> ::= N <closure-prefix> <closure-type-name> E
1705 
1706   Out << 'N';
1707 
1708   mangleClosurePrefix(PrefixND);
1709   mangleUnqualifiedName(GD, AdditionalAbiTags);
1710 
1711   Out << 'E';
1712 }
1713 
getParentOfLocalEntity(const DeclContext * DC)1714 static GlobalDecl getParentOfLocalEntity(const DeclContext *DC) {
1715   GlobalDecl GD;
1716   // The Itanium spec says:
1717   // For entities in constructors and destructors, the mangling of the
1718   // complete object constructor or destructor is used as the base function
1719   // name, i.e. the C1 or D1 version.
1720   if (auto *CD = dyn_cast<CXXConstructorDecl>(DC))
1721     GD = GlobalDecl(CD, Ctor_Complete);
1722   else if (auto *DD = dyn_cast<CXXDestructorDecl>(DC))
1723     GD = GlobalDecl(DD, Dtor_Complete);
1724   else
1725     GD = GlobalDecl(cast<FunctionDecl>(DC));
1726   return GD;
1727 }
1728 
mangleLocalName(GlobalDecl GD,const AbiTagList * AdditionalAbiTags)1729 void CXXNameMangler::mangleLocalName(GlobalDecl GD,
1730                                      const AbiTagList *AdditionalAbiTags) {
1731   const Decl *D = GD.getDecl();
1732   // <local-name> := Z <function encoding> E <entity name> [<discriminator>]
1733   //              := Z <function encoding> E s [<discriminator>]
1734   // <local-name> := Z <function encoding> E d [ <parameter number> ]
1735   //                 _ <entity name>
1736   // <discriminator> := _ <non-negative number>
1737   assert(isa<NamedDecl>(D) || isa<BlockDecl>(D));
1738   const RecordDecl *RD = GetLocalClassDecl(D);
1739   const DeclContext *DC = getEffectiveDeclContext(RD ? RD : D);
1740 
1741   Out << 'Z';
1742 
1743   {
1744     AbiTagState LocalAbiTags(AbiTags);
1745 
1746     if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(DC))
1747       mangleObjCMethodName(MD);
1748     else if (const BlockDecl *BD = dyn_cast<BlockDecl>(DC))
1749       mangleBlockForPrefix(BD);
1750     else
1751       mangleFunctionEncoding(getParentOfLocalEntity(DC));
1752 
1753     // Implicit ABI tags (from namespace) are not available in the following
1754     // entity; reset to actually emitted tags, which are available.
1755     LocalAbiTags.setUsedAbiTags(LocalAbiTags.getEmittedAbiTags());
1756   }
1757 
1758   Out << 'E';
1759 
1760   // GCC 5.3.0 doesn't emit derived ABI tags for local names but that seems to
1761   // be a bug that is fixed in trunk.
1762 
1763   if (RD) {
1764     // The parameter number is omitted for the last parameter, 0 for the
1765     // second-to-last parameter, 1 for the third-to-last parameter, etc. The
1766     // <entity name> will of course contain a <closure-type-name>: Its
1767     // numbering will be local to the particular argument in which it appears
1768     // -- other default arguments do not affect its encoding.
1769     const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD);
1770     if (CXXRD && CXXRD->isLambda()) {
1771       if (const ParmVarDecl *Parm
1772               = dyn_cast_or_null<ParmVarDecl>(CXXRD->getLambdaContextDecl())) {
1773         if (const FunctionDecl *Func
1774               = dyn_cast<FunctionDecl>(Parm->getDeclContext())) {
1775           Out << 'd';
1776           unsigned Num = Func->getNumParams() - Parm->getFunctionScopeIndex();
1777           if (Num > 1)
1778             mangleNumber(Num - 2);
1779           Out << '_';
1780         }
1781       }
1782     }
1783 
1784     // Mangle the name relative to the closest enclosing function.
1785     // equality ok because RD derived from ND above
1786     if (D == RD)  {
1787       mangleUnqualifiedName(RD, AdditionalAbiTags);
1788     } else if (const BlockDecl *BD = dyn_cast<BlockDecl>(D)) {
1789       if (const NamedDecl *PrefixND = getClosurePrefix(BD))
1790         mangleClosurePrefix(PrefixND, true /*NoFunction*/);
1791       else
1792         manglePrefix(getEffectiveDeclContext(BD), true /*NoFunction*/);
1793       assert(!AdditionalAbiTags && "Block cannot have additional abi tags");
1794       mangleUnqualifiedBlock(BD);
1795     } else {
1796       const NamedDecl *ND = cast<NamedDecl>(D);
1797       mangleNestedName(GD, getEffectiveDeclContext(ND), AdditionalAbiTags,
1798                        true /*NoFunction*/);
1799     }
1800   } else if (const BlockDecl *BD = dyn_cast<BlockDecl>(D)) {
1801     // Mangle a block in a default parameter; see above explanation for
1802     // lambdas.
1803     if (const ParmVarDecl *Parm
1804             = dyn_cast_or_null<ParmVarDecl>(BD->getBlockManglingContextDecl())) {
1805       if (const FunctionDecl *Func
1806             = dyn_cast<FunctionDecl>(Parm->getDeclContext())) {
1807         Out << 'd';
1808         unsigned Num = Func->getNumParams() - Parm->getFunctionScopeIndex();
1809         if (Num > 1)
1810           mangleNumber(Num - 2);
1811         Out << '_';
1812       }
1813     }
1814 
1815     assert(!AdditionalAbiTags && "Block cannot have additional abi tags");
1816     mangleUnqualifiedBlock(BD);
1817   } else {
1818     mangleUnqualifiedName(GD, AdditionalAbiTags);
1819   }
1820 
1821   if (const NamedDecl *ND = dyn_cast<NamedDecl>(RD ? RD : D)) {
1822     unsigned disc;
1823     if (Context.getNextDiscriminator(ND, disc)) {
1824       if (disc < 10)
1825         Out << '_' << disc;
1826       else
1827         Out << "__" << disc << '_';
1828     }
1829   }
1830 }
1831 
mangleBlockForPrefix(const BlockDecl * Block)1832 void CXXNameMangler::mangleBlockForPrefix(const BlockDecl *Block) {
1833   if (GetLocalClassDecl(Block)) {
1834     mangleLocalName(Block, /* AdditionalAbiTags */ nullptr);
1835     return;
1836   }
1837   const DeclContext *DC = getEffectiveDeclContext(Block);
1838   if (isLocalContainerContext(DC)) {
1839     mangleLocalName(Block, /* AdditionalAbiTags */ nullptr);
1840     return;
1841   }
1842   if (const NamedDecl *PrefixND = getClosurePrefix(Block))
1843     mangleClosurePrefix(PrefixND);
1844   else
1845     manglePrefix(DC);
1846   mangleUnqualifiedBlock(Block);
1847 }
1848 
mangleUnqualifiedBlock(const BlockDecl * Block)1849 void CXXNameMangler::mangleUnqualifiedBlock(const BlockDecl *Block) {
1850   // When trying to be ABI-compatibility with clang 12 and before, mangle a
1851   // <data-member-prefix> now, with no substitutions and no <template-args>.
1852   if (Decl *Context = Block->getBlockManglingContextDecl()) {
1853     if (getASTContext().getLangOpts().getClangABICompat() <=
1854             LangOptions::ClangABI::Ver12 &&
1855         (isa<VarDecl>(Context) || isa<FieldDecl>(Context)) &&
1856         Context->getDeclContext()->isRecord()) {
1857       const auto *ND = cast<NamedDecl>(Context);
1858       if (ND->getIdentifier()) {
1859         mangleSourceNameWithAbiTags(ND);
1860         Out << 'M';
1861       }
1862     }
1863   }
1864 
1865   // If we have a block mangling number, use it.
1866   unsigned Number = Block->getBlockManglingNumber();
1867   // Otherwise, just make up a number. It doesn't matter what it is because
1868   // the symbol in question isn't externally visible.
1869   if (!Number)
1870     Number = Context.getBlockId(Block, false);
1871   else {
1872     // Stored mangling numbers are 1-based.
1873     --Number;
1874   }
1875   Out << "Ub";
1876   if (Number > 0)
1877     Out << Number - 1;
1878   Out << '_';
1879 }
1880 
1881 // <template-param-decl>
1882 //   ::= Ty                              # template type parameter
1883 //   ::= Tn <type>                       # template non-type parameter
1884 //   ::= Tt <template-param-decl>* E     # template template parameter
1885 //   ::= Tp <template-param-decl>        # template parameter pack
mangleTemplateParamDecl(const NamedDecl * Decl)1886 void CXXNameMangler::mangleTemplateParamDecl(const NamedDecl *Decl) {
1887   if (auto *Ty = dyn_cast<TemplateTypeParmDecl>(Decl)) {
1888     if (Ty->isParameterPack())
1889       Out << "Tp";
1890     Out << "Ty";
1891   } else if (auto *Tn = dyn_cast<NonTypeTemplateParmDecl>(Decl)) {
1892     if (Tn->isExpandedParameterPack()) {
1893       for (unsigned I = 0, N = Tn->getNumExpansionTypes(); I != N; ++I) {
1894         Out << "Tn";
1895         mangleType(Tn->getExpansionType(I));
1896       }
1897     } else {
1898       QualType T = Tn->getType();
1899       if (Tn->isParameterPack()) {
1900         Out << "Tp";
1901         if (auto *PackExpansion = T->getAs<PackExpansionType>())
1902           T = PackExpansion->getPattern();
1903       }
1904       Out << "Tn";
1905       mangleType(T);
1906     }
1907   } else if (auto *Tt = dyn_cast<TemplateTemplateParmDecl>(Decl)) {
1908     if (Tt->isExpandedParameterPack()) {
1909       for (unsigned I = 0, N = Tt->getNumExpansionTemplateParameters(); I != N;
1910            ++I) {
1911         Out << "Tt";
1912         for (auto *Param : *Tt->getExpansionTemplateParameters(I))
1913           mangleTemplateParamDecl(Param);
1914         Out << "E";
1915       }
1916     } else {
1917       if (Tt->isParameterPack())
1918         Out << "Tp";
1919       Out << "Tt";
1920       for (auto *Param : *Tt->getTemplateParameters())
1921         mangleTemplateParamDecl(Param);
1922       Out << "E";
1923     }
1924   }
1925 }
1926 
mangleLambda(const CXXRecordDecl * Lambda)1927 void CXXNameMangler::mangleLambda(const CXXRecordDecl *Lambda) {
1928   // When trying to be ABI-compatibility with clang 12 and before, mangle a
1929   // <data-member-prefix> now, with no substitutions.
1930   if (Decl *Context = Lambda->getLambdaContextDecl()) {
1931     if (getASTContext().getLangOpts().getClangABICompat() <=
1932             LangOptions::ClangABI::Ver12 &&
1933         (isa<VarDecl>(Context) || isa<FieldDecl>(Context)) &&
1934         !isa<ParmVarDecl>(Context)) {
1935       if (const IdentifierInfo *Name
1936             = cast<NamedDecl>(Context)->getIdentifier()) {
1937         mangleSourceName(Name);
1938         const TemplateArgumentList *TemplateArgs = nullptr;
1939         if (GlobalDecl TD = isTemplate(cast<NamedDecl>(Context), TemplateArgs))
1940           mangleTemplateArgs(asTemplateName(TD), *TemplateArgs);
1941         Out << 'M';
1942       }
1943     }
1944   }
1945 
1946   Out << "Ul";
1947   mangleLambdaSig(Lambda);
1948   Out << "E";
1949 
1950   // The number is omitted for the first closure type with a given
1951   // <lambda-sig> in a given context; it is n-2 for the nth closure type
1952   // (in lexical order) with that same <lambda-sig> and context.
1953   //
1954   // The AST keeps track of the number for us.
1955   //
1956   // In CUDA/HIP, to ensure the consistent lamba numbering between the device-
1957   // and host-side compilations, an extra device mangle context may be created
1958   // if the host-side CXX ABI has different numbering for lambda. In such case,
1959   // if the mangle context is that device-side one, use the device-side lambda
1960   // mangling number for this lambda.
1961   llvm::Optional<unsigned> DeviceNumber =
1962       Context.getDiscriminatorOverride()(Context.getASTContext(), Lambda);
1963   unsigned Number = DeviceNumber.hasValue() ? *DeviceNumber
1964                                             : Lambda->getLambdaManglingNumber();
1965 
1966   assert(Number > 0 && "Lambda should be mangled as an unnamed class");
1967   if (Number > 1)
1968     mangleNumber(Number - 2);
1969   Out << '_';
1970 }
1971 
mangleLambdaSig(const CXXRecordDecl * Lambda)1972 void CXXNameMangler::mangleLambdaSig(const CXXRecordDecl *Lambda) {
1973   for (auto *D : Lambda->getLambdaExplicitTemplateParameters())
1974     mangleTemplateParamDecl(D);
1975   auto *Proto =
1976       Lambda->getLambdaTypeInfo()->getType()->castAs<FunctionProtoType>();
1977   mangleBareFunctionType(Proto, /*MangleReturnType=*/false,
1978                          Lambda->getLambdaStaticInvoker());
1979 }
1980 
manglePrefix(NestedNameSpecifier * qualifier)1981 void CXXNameMangler::manglePrefix(NestedNameSpecifier *qualifier) {
1982   switch (qualifier->getKind()) {
1983   case NestedNameSpecifier::Global:
1984     // nothing
1985     return;
1986 
1987   case NestedNameSpecifier::Super:
1988     llvm_unreachable("Can't mangle __super specifier");
1989 
1990   case NestedNameSpecifier::Namespace:
1991     mangleName(qualifier->getAsNamespace());
1992     return;
1993 
1994   case NestedNameSpecifier::NamespaceAlias:
1995     mangleName(qualifier->getAsNamespaceAlias()->getNamespace());
1996     return;
1997 
1998   case NestedNameSpecifier::TypeSpec:
1999   case NestedNameSpecifier::TypeSpecWithTemplate:
2000     manglePrefix(QualType(qualifier->getAsType(), 0));
2001     return;
2002 
2003   case NestedNameSpecifier::Identifier:
2004     // Member expressions can have these without prefixes, but that
2005     // should end up in mangleUnresolvedPrefix instead.
2006     assert(qualifier->getPrefix());
2007     manglePrefix(qualifier->getPrefix());
2008 
2009     mangleSourceName(qualifier->getAsIdentifier());
2010     return;
2011   }
2012 
2013   llvm_unreachable("unexpected nested name specifier");
2014 }
2015 
manglePrefix(const DeclContext * DC,bool NoFunction)2016 void CXXNameMangler::manglePrefix(const DeclContext *DC, bool NoFunction) {
2017   //  <prefix> ::= <prefix> <unqualified-name>
2018   //           ::= <template-prefix> <template-args>
2019   //           ::= <closure-prefix>
2020   //           ::= <template-param>
2021   //           ::= # empty
2022   //           ::= <substitution>
2023 
2024   DC = IgnoreLinkageSpecDecls(DC);
2025 
2026   if (DC->isTranslationUnit())
2027     return;
2028 
2029   if (NoFunction && isLocalContainerContext(DC))
2030     return;
2031 
2032   assert(!isLocalContainerContext(DC));
2033 
2034   const NamedDecl *ND = cast<NamedDecl>(DC);
2035   if (mangleSubstitution(ND))
2036     return;
2037 
2038   // Check if we have a template-prefix or a closure-prefix.
2039   const TemplateArgumentList *TemplateArgs = nullptr;
2040   if (GlobalDecl TD = isTemplate(ND, TemplateArgs)) {
2041     mangleTemplatePrefix(TD);
2042     mangleTemplateArgs(asTemplateName(TD), *TemplateArgs);
2043   } else if (const NamedDecl *PrefixND = getClosurePrefix(ND)) {
2044     mangleClosurePrefix(PrefixND, NoFunction);
2045     mangleUnqualifiedName(ND, nullptr);
2046   } else {
2047     manglePrefix(getEffectiveDeclContext(ND), NoFunction);
2048     mangleUnqualifiedName(ND, nullptr);
2049   }
2050 
2051   addSubstitution(ND);
2052 }
2053 
mangleTemplatePrefix(TemplateName Template)2054 void CXXNameMangler::mangleTemplatePrefix(TemplateName Template) {
2055   // <template-prefix> ::= <prefix> <template unqualified-name>
2056   //                   ::= <template-param>
2057   //                   ::= <substitution>
2058   if (TemplateDecl *TD = Template.getAsTemplateDecl())
2059     return mangleTemplatePrefix(TD);
2060 
2061   DependentTemplateName *Dependent = Template.getAsDependentTemplateName();
2062   assert(Dependent && "unexpected template name kind");
2063 
2064   // Clang 11 and before mangled the substitution for a dependent template name
2065   // after already having emitted (a substitution for) the prefix.
2066   bool Clang11Compat = getASTContext().getLangOpts().getClangABICompat() <=
2067                        LangOptions::ClangABI::Ver11;
2068   if (!Clang11Compat && mangleSubstitution(Template))
2069     return;
2070 
2071   if (NestedNameSpecifier *Qualifier = Dependent->getQualifier())
2072     manglePrefix(Qualifier);
2073 
2074   if (Clang11Compat && mangleSubstitution(Template))
2075     return;
2076 
2077   if (const IdentifierInfo *Id = Dependent->getIdentifier())
2078     mangleSourceName(Id);
2079   else
2080     mangleOperatorName(Dependent->getOperator(), UnknownArity);
2081 
2082   addSubstitution(Template);
2083 }
2084 
mangleTemplatePrefix(GlobalDecl GD,bool NoFunction)2085 void CXXNameMangler::mangleTemplatePrefix(GlobalDecl GD,
2086                                           bool NoFunction) {
2087   const TemplateDecl *ND = cast<TemplateDecl>(GD.getDecl());
2088   // <template-prefix> ::= <prefix> <template unqualified-name>
2089   //                   ::= <template-param>
2090   //                   ::= <substitution>
2091   // <template-template-param> ::= <template-param>
2092   //                               <substitution>
2093 
2094   if (mangleSubstitution(ND))
2095     return;
2096 
2097   // <template-template-param> ::= <template-param>
2098   if (const auto *TTP = dyn_cast<TemplateTemplateParmDecl>(ND)) {
2099     mangleTemplateParameter(TTP->getDepth(), TTP->getIndex());
2100   } else {
2101     manglePrefix(getEffectiveDeclContext(ND), NoFunction);
2102     if (isa<BuiltinTemplateDecl>(ND) || isa<ConceptDecl>(ND))
2103       mangleUnqualifiedName(GD, nullptr);
2104     else
2105       mangleUnqualifiedName(GD.getWithDecl(ND->getTemplatedDecl()), nullptr);
2106   }
2107 
2108   addSubstitution(ND);
2109 }
2110 
getClosurePrefix(const Decl * ND)2111 const NamedDecl *CXXNameMangler::getClosurePrefix(const Decl *ND) {
2112   if (getASTContext().getLangOpts().getClangABICompat() <=
2113       LangOptions::ClangABI::Ver12)
2114     return nullptr;
2115 
2116   const NamedDecl *Context = nullptr;
2117   if (auto *Block = dyn_cast<BlockDecl>(ND)) {
2118     Context = dyn_cast_or_null<NamedDecl>(Block->getBlockManglingContextDecl());
2119   } else if (auto *RD = dyn_cast<CXXRecordDecl>(ND)) {
2120     if (RD->isLambda())
2121       Context = dyn_cast_or_null<NamedDecl>(RD->getLambdaContextDecl());
2122   }
2123   if (!Context)
2124     return nullptr;
2125 
2126   // Only lambdas within the initializer of a non-local variable or non-static
2127   // data member get a <closure-prefix>.
2128   if ((isa<VarDecl>(Context) && cast<VarDecl>(Context)->hasGlobalStorage()) ||
2129       isa<FieldDecl>(Context))
2130     return Context;
2131 
2132   return nullptr;
2133 }
2134 
mangleClosurePrefix(const NamedDecl * ND,bool NoFunction)2135 void CXXNameMangler::mangleClosurePrefix(const NamedDecl *ND, bool NoFunction) {
2136   //  <closure-prefix> ::= [ <prefix> ] <unqualified-name> M
2137   //                   ::= <template-prefix> <template-args> M
2138   if (mangleSubstitution(ND))
2139     return;
2140 
2141   const TemplateArgumentList *TemplateArgs = nullptr;
2142   if (GlobalDecl TD = isTemplate(ND, TemplateArgs)) {
2143     mangleTemplatePrefix(TD, NoFunction);
2144     mangleTemplateArgs(asTemplateName(TD), *TemplateArgs);
2145   } else {
2146     manglePrefix(getEffectiveDeclContext(ND), NoFunction);
2147     mangleUnqualifiedName(ND, nullptr);
2148   }
2149 
2150   Out << 'M';
2151 
2152   addSubstitution(ND);
2153 }
2154 
2155 /// Mangles a template name under the production <type>.  Required for
2156 /// template template arguments.
2157 ///   <type> ::= <class-enum-type>
2158 ///          ::= <template-param>
2159 ///          ::= <substitution>
mangleType(TemplateName TN)2160 void CXXNameMangler::mangleType(TemplateName TN) {
2161   if (mangleSubstitution(TN))
2162     return;
2163 
2164   TemplateDecl *TD = nullptr;
2165 
2166   switch (TN.getKind()) {
2167   case TemplateName::QualifiedTemplate:
2168     TD = TN.getAsQualifiedTemplateName()->getTemplateDecl();
2169     goto HaveDecl;
2170 
2171   case TemplateName::Template:
2172     TD = TN.getAsTemplateDecl();
2173     goto HaveDecl;
2174 
2175   HaveDecl:
2176     if (auto *TTP = dyn_cast<TemplateTemplateParmDecl>(TD))
2177       mangleTemplateParameter(TTP->getDepth(), TTP->getIndex());
2178     else
2179       mangleName(TD);
2180     break;
2181 
2182   case TemplateName::OverloadedTemplate:
2183   case TemplateName::AssumedTemplate:
2184     llvm_unreachable("can't mangle an overloaded template name as a <type>");
2185 
2186   case TemplateName::DependentTemplate: {
2187     const DependentTemplateName *Dependent = TN.getAsDependentTemplateName();
2188     assert(Dependent->isIdentifier());
2189 
2190     // <class-enum-type> ::= <name>
2191     // <name> ::= <nested-name>
2192     mangleUnresolvedPrefix(Dependent->getQualifier());
2193     mangleSourceName(Dependent->getIdentifier());
2194     break;
2195   }
2196 
2197   case TemplateName::SubstTemplateTemplateParm: {
2198     // Substituted template parameters are mangled as the substituted
2199     // template.  This will check for the substitution twice, which is
2200     // fine, but we have to return early so that we don't try to *add*
2201     // the substitution twice.
2202     SubstTemplateTemplateParmStorage *subst
2203       = TN.getAsSubstTemplateTemplateParm();
2204     mangleType(subst->getReplacement());
2205     return;
2206   }
2207 
2208   case TemplateName::SubstTemplateTemplateParmPack: {
2209     // FIXME: not clear how to mangle this!
2210     // template <template <class> class T...> class A {
2211     //   template <template <class> class U...> void foo(B<T,U> x...);
2212     // };
2213     Out << "_SUBSTPACK_";
2214     break;
2215   }
2216   }
2217 
2218   addSubstitution(TN);
2219 }
2220 
mangleUnresolvedTypeOrSimpleId(QualType Ty,StringRef Prefix)2221 bool CXXNameMangler::mangleUnresolvedTypeOrSimpleId(QualType Ty,
2222                                                     StringRef Prefix) {
2223   // Only certain other types are valid as prefixes;  enumerate them.
2224   switch (Ty->getTypeClass()) {
2225   case Type::Builtin:
2226   case Type::Complex:
2227   case Type::Adjusted:
2228   case Type::Decayed:
2229   case Type::Pointer:
2230   case Type::BlockPointer:
2231   case Type::LValueReference:
2232   case Type::RValueReference:
2233   case Type::MemberPointer:
2234   case Type::ConstantArray:
2235   case Type::IncompleteArray:
2236   case Type::VariableArray:
2237   case Type::DependentSizedArray:
2238   case Type::DependentAddressSpace:
2239   case Type::DependentVector:
2240   case Type::DependentSizedExtVector:
2241   case Type::Vector:
2242   case Type::ExtVector:
2243   case Type::ConstantMatrix:
2244   case Type::DependentSizedMatrix:
2245   case Type::FunctionProto:
2246   case Type::FunctionNoProto:
2247   case Type::Paren:
2248   case Type::Attributed:
2249   case Type::Auto:
2250   case Type::DeducedTemplateSpecialization:
2251   case Type::PackExpansion:
2252   case Type::ObjCObject:
2253   case Type::ObjCInterface:
2254   case Type::ObjCObjectPointer:
2255   case Type::ObjCTypeParam:
2256   case Type::Atomic:
2257   case Type::Pipe:
2258   case Type::MacroQualified:
2259   case Type::ExtInt:
2260   case Type::DependentExtInt:
2261     llvm_unreachable("type is illegal as a nested name specifier");
2262 
2263   case Type::SubstTemplateTypeParmPack:
2264     // FIXME: not clear how to mangle this!
2265     // template <class T...> class A {
2266     //   template <class U...> void foo(decltype(T::foo(U())) x...);
2267     // };
2268     Out << "_SUBSTPACK_";
2269     break;
2270 
2271   // <unresolved-type> ::= <template-param>
2272   //                   ::= <decltype>
2273   //                   ::= <template-template-param> <template-args>
2274   // (this last is not official yet)
2275   case Type::TypeOfExpr:
2276   case Type::TypeOf:
2277   case Type::Decltype:
2278   case Type::TemplateTypeParm:
2279   case Type::UnaryTransform:
2280   case Type::SubstTemplateTypeParm:
2281   unresolvedType:
2282     // Some callers want a prefix before the mangled type.
2283     Out << Prefix;
2284 
2285     // This seems to do everything we want.  It's not really
2286     // sanctioned for a substituted template parameter, though.
2287     mangleType(Ty);
2288 
2289     // We never want to print 'E' directly after an unresolved-type,
2290     // so we return directly.
2291     return true;
2292 
2293   case Type::Typedef:
2294     mangleSourceNameWithAbiTags(cast<TypedefType>(Ty)->getDecl());
2295     break;
2296 
2297   case Type::UnresolvedUsing:
2298     mangleSourceNameWithAbiTags(
2299         cast<UnresolvedUsingType>(Ty)->getDecl());
2300     break;
2301 
2302   case Type::Enum:
2303   case Type::Record:
2304     mangleSourceNameWithAbiTags(cast<TagType>(Ty)->getDecl());
2305     break;
2306 
2307   case Type::TemplateSpecialization: {
2308     const TemplateSpecializationType *TST =
2309         cast<TemplateSpecializationType>(Ty);
2310     TemplateName TN = TST->getTemplateName();
2311     switch (TN.getKind()) {
2312     case TemplateName::Template:
2313     case TemplateName::QualifiedTemplate: {
2314       TemplateDecl *TD = TN.getAsTemplateDecl();
2315 
2316       // If the base is a template template parameter, this is an
2317       // unresolved type.
2318       assert(TD && "no template for template specialization type");
2319       if (isa<TemplateTemplateParmDecl>(TD))
2320         goto unresolvedType;
2321 
2322       mangleSourceNameWithAbiTags(TD);
2323       break;
2324     }
2325 
2326     case TemplateName::OverloadedTemplate:
2327     case TemplateName::AssumedTemplate:
2328     case TemplateName::DependentTemplate:
2329       llvm_unreachable("invalid base for a template specialization type");
2330 
2331     case TemplateName::SubstTemplateTemplateParm: {
2332       SubstTemplateTemplateParmStorage *subst =
2333           TN.getAsSubstTemplateTemplateParm();
2334       mangleExistingSubstitution(subst->getReplacement());
2335       break;
2336     }
2337 
2338     case TemplateName::SubstTemplateTemplateParmPack: {
2339       // FIXME: not clear how to mangle this!
2340       // template <template <class U> class T...> class A {
2341       //   template <class U...> void foo(decltype(T<U>::foo) x...);
2342       // };
2343       Out << "_SUBSTPACK_";
2344       break;
2345     }
2346     }
2347 
2348     // Note: we don't pass in the template name here. We are mangling the
2349     // original source-level template arguments, so we shouldn't consider
2350     // conversions to the corresponding template parameter.
2351     // FIXME: Other compilers mangle partially-resolved template arguments in
2352     // unresolved-qualifier-levels.
2353     mangleTemplateArgs(TemplateName(), TST->getArgs(), TST->getNumArgs());
2354     break;
2355   }
2356 
2357   case Type::InjectedClassName:
2358     mangleSourceNameWithAbiTags(
2359         cast<InjectedClassNameType>(Ty)->getDecl());
2360     break;
2361 
2362   case Type::DependentName:
2363     mangleSourceName(cast<DependentNameType>(Ty)->getIdentifier());
2364     break;
2365 
2366   case Type::DependentTemplateSpecialization: {
2367     const DependentTemplateSpecializationType *DTST =
2368         cast<DependentTemplateSpecializationType>(Ty);
2369     TemplateName Template = getASTContext().getDependentTemplateName(
2370         DTST->getQualifier(), DTST->getIdentifier());
2371     mangleSourceName(DTST->getIdentifier());
2372     mangleTemplateArgs(Template, DTST->getArgs(), DTST->getNumArgs());
2373     break;
2374   }
2375 
2376   case Type::Elaborated:
2377     return mangleUnresolvedTypeOrSimpleId(
2378         cast<ElaboratedType>(Ty)->getNamedType(), Prefix);
2379   }
2380 
2381   return false;
2382 }
2383 
mangleOperatorName(DeclarationName Name,unsigned Arity)2384 void CXXNameMangler::mangleOperatorName(DeclarationName Name, unsigned Arity) {
2385   switch (Name.getNameKind()) {
2386   case DeclarationName::CXXConstructorName:
2387   case DeclarationName::CXXDestructorName:
2388   case DeclarationName::CXXDeductionGuideName:
2389   case DeclarationName::CXXUsingDirective:
2390   case DeclarationName::Identifier:
2391   case DeclarationName::ObjCMultiArgSelector:
2392   case DeclarationName::ObjCOneArgSelector:
2393   case DeclarationName::ObjCZeroArgSelector:
2394     llvm_unreachable("Not an operator name");
2395 
2396   case DeclarationName::CXXConversionFunctionName:
2397     // <operator-name> ::= cv <type>    # (cast)
2398     Out << "cv";
2399     mangleType(Name.getCXXNameType());
2400     break;
2401 
2402   case DeclarationName::CXXLiteralOperatorName:
2403     Out << "li";
2404     mangleSourceName(Name.getCXXLiteralIdentifier());
2405     return;
2406 
2407   case DeclarationName::CXXOperatorName:
2408     mangleOperatorName(Name.getCXXOverloadedOperator(), Arity);
2409     break;
2410   }
2411 }
2412 
2413 void
mangleOperatorName(OverloadedOperatorKind OO,unsigned Arity)2414 CXXNameMangler::mangleOperatorName(OverloadedOperatorKind OO, unsigned Arity) {
2415   switch (OO) {
2416   // <operator-name> ::= nw     # new
2417   case OO_New: Out << "nw"; break;
2418   //              ::= na        # new[]
2419   case OO_Array_New: Out << "na"; break;
2420   //              ::= dl        # delete
2421   case OO_Delete: Out << "dl"; break;
2422   //              ::= da        # delete[]
2423   case OO_Array_Delete: Out << "da"; break;
2424   //              ::= ps        # + (unary)
2425   //              ::= pl        # + (binary or unknown)
2426   case OO_Plus:
2427     Out << (Arity == 1? "ps" : "pl"); break;
2428   //              ::= ng        # - (unary)
2429   //              ::= mi        # - (binary or unknown)
2430   case OO_Minus:
2431     Out << (Arity == 1? "ng" : "mi"); break;
2432   //              ::= ad        # & (unary)
2433   //              ::= an        # & (binary or unknown)
2434   case OO_Amp:
2435     Out << (Arity == 1? "ad" : "an"); break;
2436   //              ::= de        # * (unary)
2437   //              ::= ml        # * (binary or unknown)
2438   case OO_Star:
2439     // Use binary when unknown.
2440     Out << (Arity == 1? "de" : "ml"); break;
2441   //              ::= co        # ~
2442   case OO_Tilde: Out << "co"; break;
2443   //              ::= dv        # /
2444   case OO_Slash: Out << "dv"; break;
2445   //              ::= rm        # %
2446   case OO_Percent: Out << "rm"; break;
2447   //              ::= or        # |
2448   case OO_Pipe: Out << "or"; break;
2449   //              ::= eo        # ^
2450   case OO_Caret: Out << "eo"; break;
2451   //              ::= aS        # =
2452   case OO_Equal: Out << "aS"; break;
2453   //              ::= pL        # +=
2454   case OO_PlusEqual: Out << "pL"; break;
2455   //              ::= mI        # -=
2456   case OO_MinusEqual: Out << "mI"; break;
2457   //              ::= mL        # *=
2458   case OO_StarEqual: Out << "mL"; break;
2459   //              ::= dV        # /=
2460   case OO_SlashEqual: Out << "dV"; break;
2461   //              ::= rM        # %=
2462   case OO_PercentEqual: Out << "rM"; break;
2463   //              ::= aN        # &=
2464   case OO_AmpEqual: Out << "aN"; break;
2465   //              ::= oR        # |=
2466   case OO_PipeEqual: Out << "oR"; break;
2467   //              ::= eO        # ^=
2468   case OO_CaretEqual: Out << "eO"; break;
2469   //              ::= ls        # <<
2470   case OO_LessLess: Out << "ls"; break;
2471   //              ::= rs        # >>
2472   case OO_GreaterGreater: Out << "rs"; break;
2473   //              ::= lS        # <<=
2474   case OO_LessLessEqual: Out << "lS"; break;
2475   //              ::= rS        # >>=
2476   case OO_GreaterGreaterEqual: Out << "rS"; break;
2477   //              ::= eq        # ==
2478   case OO_EqualEqual: Out << "eq"; break;
2479   //              ::= ne        # !=
2480   case OO_ExclaimEqual: Out << "ne"; break;
2481   //              ::= lt        # <
2482   case OO_Less: Out << "lt"; break;
2483   //              ::= gt        # >
2484   case OO_Greater: Out << "gt"; break;
2485   //              ::= le        # <=
2486   case OO_LessEqual: Out << "le"; break;
2487   //              ::= ge        # >=
2488   case OO_GreaterEqual: Out << "ge"; break;
2489   //              ::= nt        # !
2490   case OO_Exclaim: Out << "nt"; break;
2491   //              ::= aa        # &&
2492   case OO_AmpAmp: Out << "aa"; break;
2493   //              ::= oo        # ||
2494   case OO_PipePipe: Out << "oo"; break;
2495   //              ::= pp        # ++
2496   case OO_PlusPlus: Out << "pp"; break;
2497   //              ::= mm        # --
2498   case OO_MinusMinus: Out << "mm"; break;
2499   //              ::= cm        # ,
2500   case OO_Comma: Out << "cm"; break;
2501   //              ::= pm        # ->*
2502   case OO_ArrowStar: Out << "pm"; break;
2503   //              ::= pt        # ->
2504   case OO_Arrow: Out << "pt"; break;
2505   //              ::= cl        # ()
2506   case OO_Call: Out << "cl"; break;
2507   //              ::= ix        # []
2508   case OO_Subscript: Out << "ix"; break;
2509 
2510   //              ::= qu        # ?
2511   // The conditional operator can't be overloaded, but we still handle it when
2512   // mangling expressions.
2513   case OO_Conditional: Out << "qu"; break;
2514   // Proposal on cxx-abi-dev, 2015-10-21.
2515   //              ::= aw        # co_await
2516   case OO_Coawait: Out << "aw"; break;
2517   // Proposed in cxx-abi github issue 43.
2518   //              ::= ss        # <=>
2519   case OO_Spaceship: Out << "ss"; break;
2520 
2521   case OO_None:
2522   case NUM_OVERLOADED_OPERATORS:
2523     llvm_unreachable("Not an overloaded operator");
2524   }
2525 }
2526 
mangleQualifiers(Qualifiers Quals,const DependentAddressSpaceType * DAST)2527 void CXXNameMangler::mangleQualifiers(Qualifiers Quals, const DependentAddressSpaceType *DAST) {
2528   // Vendor qualifiers come first and if they are order-insensitive they must
2529   // be emitted in reversed alphabetical order, see Itanium ABI 5.1.5.
2530 
2531   // <type> ::= U <addrspace-expr>
2532   if (DAST) {
2533     Out << "U2ASI";
2534     mangleExpression(DAST->getAddrSpaceExpr());
2535     Out << "E";
2536   }
2537 
2538   // Address space qualifiers start with an ordinary letter.
2539   if (Quals.hasAddressSpace()) {
2540     // Address space extension:
2541     //
2542     //   <type> ::= U <target-addrspace>
2543     //   <type> ::= U <OpenCL-addrspace>
2544     //   <type> ::= U <CUDA-addrspace>
2545 
2546     SmallString<64> ASString;
2547     LangAS AS = Quals.getAddressSpace();
2548 
2549     if (Context.getASTContext().addressSpaceMapManglingFor(AS)) {
2550       //  <target-addrspace> ::= "AS" <address-space-number>
2551       unsigned TargetAS = Context.getASTContext().getTargetAddressSpace(AS);
2552       if (TargetAS != 0 ||
2553           Context.getASTContext().getTargetAddressSpace(LangAS::Default) != 0)
2554         ASString = "AS" + llvm::utostr(TargetAS);
2555     } else {
2556       switch (AS) {
2557       default: llvm_unreachable("Not a language specific address space");
2558       //  <OpenCL-addrspace> ::= "CL" [ "global" | "local" | "constant" |
2559       //                                "private"| "generic" | "device" |
2560       //                                "host" ]
2561       case LangAS::opencl_global:
2562         ASString = "CLglobal";
2563         break;
2564       case LangAS::opencl_global_device:
2565         ASString = "CLdevice";
2566         break;
2567       case LangAS::opencl_global_host:
2568         ASString = "CLhost";
2569         break;
2570       case LangAS::opencl_local:
2571         ASString = "CLlocal";
2572         break;
2573       case LangAS::opencl_constant:
2574         ASString = "CLconstant";
2575         break;
2576       case LangAS::opencl_private:
2577         ASString = "CLprivate";
2578         break;
2579       case LangAS::opencl_generic:
2580         ASString = "CLgeneric";
2581         break;
2582       //  <SYCL-addrspace> ::= "SY" [ "global" | "local" | "private" |
2583       //                              "device" | "host" ]
2584       case LangAS::sycl_global:
2585         ASString = "SYglobal";
2586         break;
2587       case LangAS::sycl_global_device:
2588         ASString = "SYdevice";
2589         break;
2590       case LangAS::sycl_global_host:
2591         ASString = "SYhost";
2592         break;
2593       case LangAS::sycl_local:
2594         ASString = "SYlocal";
2595         break;
2596       case LangAS::sycl_private:
2597         ASString = "SYprivate";
2598         break;
2599       //  <CUDA-addrspace> ::= "CU" [ "device" | "constant" | "shared" ]
2600       case LangAS::cuda_device:
2601         ASString = "CUdevice";
2602         break;
2603       case LangAS::cuda_constant:
2604         ASString = "CUconstant";
2605         break;
2606       case LangAS::cuda_shared:
2607         ASString = "CUshared";
2608         break;
2609       //  <ptrsize-addrspace> ::= [ "ptr32_sptr" | "ptr32_uptr" | "ptr64" ]
2610       case LangAS::ptr32_sptr:
2611         ASString = "ptr32_sptr";
2612         break;
2613       case LangAS::ptr32_uptr:
2614         ASString = "ptr32_uptr";
2615         break;
2616       case LangAS::ptr64:
2617         ASString = "ptr64";
2618         break;
2619       }
2620     }
2621     if (!ASString.empty())
2622       mangleVendorQualifier(ASString);
2623   }
2624 
2625   // The ARC ownership qualifiers start with underscores.
2626   // Objective-C ARC Extension:
2627   //
2628   //   <type> ::= U "__strong"
2629   //   <type> ::= U "__weak"
2630   //   <type> ::= U "__autoreleasing"
2631   //
2632   // Note: we emit __weak first to preserve the order as
2633   // required by the Itanium ABI.
2634   if (Quals.getObjCLifetime() == Qualifiers::OCL_Weak)
2635     mangleVendorQualifier("__weak");
2636 
2637   // __unaligned (from -fms-extensions)
2638   if (Quals.hasUnaligned())
2639     mangleVendorQualifier("__unaligned");
2640 
2641   // Remaining ARC ownership qualifiers.
2642   switch (Quals.getObjCLifetime()) {
2643   case Qualifiers::OCL_None:
2644     break;
2645 
2646   case Qualifiers::OCL_Weak:
2647     // Do nothing as we already handled this case above.
2648     break;
2649 
2650   case Qualifiers::OCL_Strong:
2651     mangleVendorQualifier("__strong");
2652     break;
2653 
2654   case Qualifiers::OCL_Autoreleasing:
2655     mangleVendorQualifier("__autoreleasing");
2656     break;
2657 
2658   case Qualifiers::OCL_ExplicitNone:
2659     // The __unsafe_unretained qualifier is *not* mangled, so that
2660     // __unsafe_unretained types in ARC produce the same manglings as the
2661     // equivalent (but, naturally, unqualified) types in non-ARC, providing
2662     // better ABI compatibility.
2663     //
2664     // It's safe to do this because unqualified 'id' won't show up
2665     // in any type signatures that need to be mangled.
2666     break;
2667   }
2668 
2669   // <CV-qualifiers> ::= [r] [V] [K]    # restrict (C99), volatile, const
2670   if (Quals.hasRestrict())
2671     Out << 'r';
2672   if (Quals.hasVolatile())
2673     Out << 'V';
2674   if (Quals.hasConst())
2675     Out << 'K';
2676 }
2677 
mangleVendorQualifier(StringRef name)2678 void CXXNameMangler::mangleVendorQualifier(StringRef name) {
2679   Out << 'U' << name.size() << name;
2680 }
2681 
mangleRefQualifier(RefQualifierKind RefQualifier)2682 void CXXNameMangler::mangleRefQualifier(RefQualifierKind RefQualifier) {
2683   // <ref-qualifier> ::= R                # lvalue reference
2684   //                 ::= O                # rvalue-reference
2685   switch (RefQualifier) {
2686   case RQ_None:
2687     break;
2688 
2689   case RQ_LValue:
2690     Out << 'R';
2691     break;
2692 
2693   case RQ_RValue:
2694     Out << 'O';
2695     break;
2696   }
2697 }
2698 
mangleObjCMethodName(const ObjCMethodDecl * MD)2699 void CXXNameMangler::mangleObjCMethodName(const ObjCMethodDecl *MD) {
2700   Context.mangleObjCMethodNameAsSourceName(MD, Out);
2701 }
2702 
isTypeSubstitutable(Qualifiers Quals,const Type * Ty,ASTContext & Ctx)2703 static bool isTypeSubstitutable(Qualifiers Quals, const Type *Ty,
2704                                 ASTContext &Ctx) {
2705   if (Quals)
2706     return true;
2707   if (Ty->isSpecificBuiltinType(BuiltinType::ObjCSel))
2708     return true;
2709   if (Ty->isOpenCLSpecificType())
2710     return true;
2711   if (Ty->isBuiltinType())
2712     return false;
2713   // Through to Clang 6.0, we accidentally treated undeduced auto types as
2714   // substitution candidates.
2715   if (Ctx.getLangOpts().getClangABICompat() > LangOptions::ClangABI::Ver6 &&
2716       isa<AutoType>(Ty))
2717     return false;
2718   // A placeholder type for class template deduction is substitutable with
2719   // its corresponding template name; this is handled specially when mangling
2720   // the type.
2721   if (auto *DeducedTST = Ty->getAs<DeducedTemplateSpecializationType>())
2722     if (DeducedTST->getDeducedType().isNull())
2723       return false;
2724   return true;
2725 }
2726 
mangleType(QualType T)2727 void CXXNameMangler::mangleType(QualType T) {
2728   // If our type is instantiation-dependent but not dependent, we mangle
2729   // it as it was written in the source, removing any top-level sugar.
2730   // Otherwise, use the canonical type.
2731   //
2732   // FIXME: This is an approximation of the instantiation-dependent name
2733   // mangling rules, since we should really be using the type as written and
2734   // augmented via semantic analysis (i.e., with implicit conversions and
2735   // default template arguments) for any instantiation-dependent type.
2736   // Unfortunately, that requires several changes to our AST:
2737   //   - Instantiation-dependent TemplateSpecializationTypes will need to be
2738   //     uniqued, so that we can handle substitutions properly
2739   //   - Default template arguments will need to be represented in the
2740   //     TemplateSpecializationType, since they need to be mangled even though
2741   //     they aren't written.
2742   //   - Conversions on non-type template arguments need to be expressed, since
2743   //     they can affect the mangling of sizeof/alignof.
2744   //
2745   // FIXME: This is wrong when mapping to the canonical type for a dependent
2746   // type discards instantiation-dependent portions of the type, such as for:
2747   //
2748   //   template<typename T, int N> void f(T (&)[sizeof(N)]);
2749   //   template<typename T> void f(T() throw(typename T::type)); (pre-C++17)
2750   //
2751   // It's also wrong in the opposite direction when instantiation-dependent,
2752   // canonically-equivalent types differ in some irrelevant portion of inner
2753   // type sugar. In such cases, we fail to form correct substitutions, eg:
2754   //
2755   //   template<int N> void f(A<sizeof(N)> *, A<sizeof(N)> (*));
2756   //
2757   // We should instead canonicalize the non-instantiation-dependent parts,
2758   // regardless of whether the type as a whole is dependent or instantiation
2759   // dependent.
2760   if (!T->isInstantiationDependentType() || T->isDependentType())
2761     T = T.getCanonicalType();
2762   else {
2763     // Desugar any types that are purely sugar.
2764     do {
2765       // Don't desugar through template specialization types that aren't
2766       // type aliases. We need to mangle the template arguments as written.
2767       if (const TemplateSpecializationType *TST
2768                                       = dyn_cast<TemplateSpecializationType>(T))
2769         if (!TST->isTypeAlias())
2770           break;
2771 
2772       // FIXME: We presumably shouldn't strip off ElaboratedTypes with
2773       // instantation-dependent qualifiers. See
2774       // https://github.com/itanium-cxx-abi/cxx-abi/issues/114.
2775 
2776       QualType Desugared
2777         = T.getSingleStepDesugaredType(Context.getASTContext());
2778       if (Desugared == T)
2779         break;
2780 
2781       T = Desugared;
2782     } while (true);
2783   }
2784   SplitQualType split = T.split();
2785   Qualifiers quals = split.Quals;
2786   const Type *ty = split.Ty;
2787 
2788   bool isSubstitutable =
2789     isTypeSubstitutable(quals, ty, Context.getASTContext());
2790   if (isSubstitutable && mangleSubstitution(T))
2791     return;
2792 
2793   // If we're mangling a qualified array type, push the qualifiers to
2794   // the element type.
2795   if (quals && isa<ArrayType>(T)) {
2796     ty = Context.getASTContext().getAsArrayType(T);
2797     quals = Qualifiers();
2798 
2799     // Note that we don't update T: we want to add the
2800     // substitution at the original type.
2801   }
2802 
2803   if (quals || ty->isDependentAddressSpaceType()) {
2804     if (const DependentAddressSpaceType *DAST =
2805         dyn_cast<DependentAddressSpaceType>(ty)) {
2806       SplitQualType splitDAST = DAST->getPointeeType().split();
2807       mangleQualifiers(splitDAST.Quals, DAST);
2808       mangleType(QualType(splitDAST.Ty, 0));
2809     } else {
2810       mangleQualifiers(quals);
2811 
2812       // Recurse:  even if the qualified type isn't yet substitutable,
2813       // the unqualified type might be.
2814       mangleType(QualType(ty, 0));
2815     }
2816   } else {
2817     switch (ty->getTypeClass()) {
2818 #define ABSTRACT_TYPE(CLASS, PARENT)
2819 #define NON_CANONICAL_TYPE(CLASS, PARENT) \
2820     case Type::CLASS: \
2821       llvm_unreachable("can't mangle non-canonical type " #CLASS "Type"); \
2822       return;
2823 #define TYPE(CLASS, PARENT) \
2824     case Type::CLASS: \
2825       mangleType(static_cast<const CLASS##Type*>(ty)); \
2826       break;
2827 #include "clang/AST/TypeNodes.inc"
2828     }
2829   }
2830 
2831   // Add the substitution.
2832   if (isSubstitutable)
2833     addSubstitution(T);
2834 }
2835 
mangleNameOrStandardSubstitution(const NamedDecl * ND)2836 void CXXNameMangler::mangleNameOrStandardSubstitution(const NamedDecl *ND) {
2837   if (!mangleStandardSubstitution(ND))
2838     mangleName(ND);
2839 }
2840 
mangleType(const BuiltinType * T)2841 void CXXNameMangler::mangleType(const BuiltinType *T) {
2842   //  <type>         ::= <builtin-type>
2843   //  <builtin-type> ::= v  # void
2844   //                 ::= w  # wchar_t
2845   //                 ::= b  # bool
2846   //                 ::= c  # char
2847   //                 ::= a  # signed char
2848   //                 ::= h  # unsigned char
2849   //                 ::= s  # short
2850   //                 ::= t  # unsigned short
2851   //                 ::= i  # int
2852   //                 ::= j  # unsigned int
2853   //                 ::= l  # long
2854   //                 ::= m  # unsigned long
2855   //                 ::= x  # long long, __int64
2856   //                 ::= y  # unsigned long long, __int64
2857   //                 ::= n  # __int128
2858   //                 ::= o  # unsigned __int128
2859   //                 ::= f  # float
2860   //                 ::= d  # double
2861   //                 ::= e  # long double, __float80
2862   //                 ::= g  # __float128
2863   // UNSUPPORTED:    ::= Dd # IEEE 754r decimal floating point (64 bits)
2864   // UNSUPPORTED:    ::= De # IEEE 754r decimal floating point (128 bits)
2865   // UNSUPPORTED:    ::= Df # IEEE 754r decimal floating point (32 bits)
2866   //                 ::= Dh # IEEE 754r half-precision floating point (16 bits)
2867   //                 ::= DF <number> _ # ISO/IEC TS 18661 binary floating point type _FloatN (N bits);
2868   //                 ::= Di # char32_t
2869   //                 ::= Ds # char16_t
2870   //                 ::= Dn # std::nullptr_t (i.e., decltype(nullptr))
2871   //                 ::= u <source-name>    # vendor extended type
2872   std::string type_name;
2873   switch (T->getKind()) {
2874   case BuiltinType::Void:
2875     Out << 'v';
2876     break;
2877   case BuiltinType::Bool:
2878     Out << 'b';
2879     break;
2880   case BuiltinType::Char_U:
2881   case BuiltinType::Char_S:
2882     Out << 'c';
2883     break;
2884   case BuiltinType::UChar:
2885     Out << 'h';
2886     break;
2887   case BuiltinType::UShort:
2888     Out << 't';
2889     break;
2890   case BuiltinType::UInt:
2891     Out << 'j';
2892     break;
2893   case BuiltinType::ULong:
2894     Out << 'm';
2895     break;
2896   case BuiltinType::ULongLong:
2897     Out << 'y';
2898     break;
2899   case BuiltinType::UInt128:
2900     Out << 'o';
2901     break;
2902   case BuiltinType::SChar:
2903     Out << 'a';
2904     break;
2905   case BuiltinType::WChar_S:
2906   case BuiltinType::WChar_U:
2907     Out << 'w';
2908     break;
2909   case BuiltinType::Char8:
2910     Out << "Du";
2911     break;
2912   case BuiltinType::Char16:
2913     Out << "Ds";
2914     break;
2915   case BuiltinType::Char32:
2916     Out << "Di";
2917     break;
2918   case BuiltinType::Short:
2919     Out << 's';
2920     break;
2921   case BuiltinType::Int:
2922     Out << 'i';
2923     break;
2924   case BuiltinType::Long:
2925     Out << 'l';
2926     break;
2927   case BuiltinType::LongLong:
2928     Out << 'x';
2929     break;
2930   case BuiltinType::Int128:
2931     Out << 'n';
2932     break;
2933   case BuiltinType::Float16:
2934     Out << "DF16_";
2935     break;
2936   case BuiltinType::ShortAccum:
2937   case BuiltinType::Accum:
2938   case BuiltinType::LongAccum:
2939   case BuiltinType::UShortAccum:
2940   case BuiltinType::UAccum:
2941   case BuiltinType::ULongAccum:
2942   case BuiltinType::ShortFract:
2943   case BuiltinType::Fract:
2944   case BuiltinType::LongFract:
2945   case BuiltinType::UShortFract:
2946   case BuiltinType::UFract:
2947   case BuiltinType::ULongFract:
2948   case BuiltinType::SatShortAccum:
2949   case BuiltinType::SatAccum:
2950   case BuiltinType::SatLongAccum:
2951   case BuiltinType::SatUShortAccum:
2952   case BuiltinType::SatUAccum:
2953   case BuiltinType::SatULongAccum:
2954   case BuiltinType::SatShortFract:
2955   case BuiltinType::SatFract:
2956   case BuiltinType::SatLongFract:
2957   case BuiltinType::SatUShortFract:
2958   case BuiltinType::SatUFract:
2959   case BuiltinType::SatULongFract:
2960     llvm_unreachable("Fixed point types are disabled for c++");
2961   case BuiltinType::Half:
2962     Out << "Dh";
2963     break;
2964   case BuiltinType::Float:
2965     Out << 'f';
2966     break;
2967   case BuiltinType::Double:
2968     Out << 'd';
2969     break;
2970   case BuiltinType::LongDouble: {
2971     const TargetInfo *TI = getASTContext().getLangOpts().OpenMP &&
2972                                    getASTContext().getLangOpts().OpenMPIsDevice
2973                                ? getASTContext().getAuxTargetInfo()
2974                                : &getASTContext().getTargetInfo();
2975     Out << TI->getLongDoubleMangling();
2976     break;
2977   }
2978   case BuiltinType::Float128: {
2979     const TargetInfo *TI = getASTContext().getLangOpts().OpenMP &&
2980                                    getASTContext().getLangOpts().OpenMPIsDevice
2981                                ? getASTContext().getAuxTargetInfo()
2982                                : &getASTContext().getTargetInfo();
2983     Out << TI->getFloat128Mangling();
2984     break;
2985   }
2986   case BuiltinType::BFloat16: {
2987     const TargetInfo *TI = &getASTContext().getTargetInfo();
2988     Out << TI->getBFloat16Mangling();
2989     break;
2990   }
2991   case BuiltinType::NullPtr:
2992     Out << "Dn";
2993     break;
2994 
2995 #define BUILTIN_TYPE(Id, SingletonId)
2996 #define PLACEHOLDER_TYPE(Id, SingletonId) \
2997   case BuiltinType::Id:
2998 #include "clang/AST/BuiltinTypes.def"
2999   case BuiltinType::Dependent:
3000     if (!NullOut)
3001       llvm_unreachable("mangling a placeholder type");
3002     break;
3003   case BuiltinType::ObjCId:
3004     Out << "11objc_object";
3005     break;
3006   case BuiltinType::ObjCClass:
3007     Out << "10objc_class";
3008     break;
3009   case BuiltinType::ObjCSel:
3010     Out << "13objc_selector";
3011     break;
3012 #define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) \
3013   case BuiltinType::Id: \
3014     type_name = "ocl_" #ImgType "_" #Suffix; \
3015     Out << type_name.size() << type_name; \
3016     break;
3017 #include "clang/Basic/OpenCLImageTypes.def"
3018   case BuiltinType::OCLSampler:
3019     Out << "11ocl_sampler";
3020     break;
3021   case BuiltinType::OCLEvent:
3022     Out << "9ocl_event";
3023     break;
3024   case BuiltinType::OCLClkEvent:
3025     Out << "12ocl_clkevent";
3026     break;
3027   case BuiltinType::OCLQueue:
3028     Out << "9ocl_queue";
3029     break;
3030   case BuiltinType::OCLReserveID:
3031     Out << "13ocl_reserveid";
3032     break;
3033 #define EXT_OPAQUE_TYPE(ExtType, Id, Ext) \
3034   case BuiltinType::Id: \
3035     type_name = "ocl_" #ExtType; \
3036     Out << type_name.size() << type_name; \
3037     break;
3038 #include "clang/Basic/OpenCLExtensionTypes.def"
3039   // The SVE types are effectively target-specific.  The mangling scheme
3040   // is defined in the appendices to the Procedure Call Standard for the
3041   // Arm Architecture.
3042 #define SVE_VECTOR_TYPE(InternalName, MangledName, Id, SingletonId, NumEls,    \
3043                         ElBits, IsSigned, IsFP, IsBF)                          \
3044   case BuiltinType::Id:                                                        \
3045     type_name = MangledName;                                                   \
3046     Out << (type_name == InternalName ? "u" : "") << type_name.size()          \
3047         << type_name;                                                          \
3048     break;
3049 #define SVE_PREDICATE_TYPE(InternalName, MangledName, Id, SingletonId, NumEls) \
3050   case BuiltinType::Id:                                                        \
3051     type_name = MangledName;                                                   \
3052     Out << (type_name == InternalName ? "u" : "") << type_name.size()          \
3053         << type_name;                                                          \
3054     break;
3055 #include "clang/Basic/AArch64SVEACLETypes.def"
3056 #define PPC_VECTOR_TYPE(Name, Id, Size) \
3057   case BuiltinType::Id: \
3058     type_name = #Name; \
3059     Out << 'u' << type_name.size() << type_name; \
3060     break;
3061 #include "clang/Basic/PPCTypes.def"
3062     // TODO: Check the mangling scheme for RISC-V V.
3063 #define RVV_TYPE(Name, Id, SingletonId)                                        \
3064   case BuiltinType::Id:                                                        \
3065     type_name = Name;                                                          \
3066     Out << 'u' << type_name.size() << type_name;                               \
3067     break;
3068 #include "clang/Basic/RISCVVTypes.def"
3069   }
3070 }
3071 
getCallingConvQualifierName(CallingConv CC)3072 StringRef CXXNameMangler::getCallingConvQualifierName(CallingConv CC) {
3073   switch (CC) {
3074   case CC_C:
3075     return "";
3076 
3077   case CC_X86VectorCall:
3078   case CC_X86Pascal:
3079   case CC_X86RegCall:
3080   case CC_AAPCS:
3081   case CC_AAPCS_VFP:
3082   case CC_AArch64VectorCall:
3083   case CC_IntelOclBicc:
3084   case CC_SpirFunction:
3085   case CC_OpenCLKernel:
3086   case CC_PreserveMost:
3087   case CC_PreserveAll:
3088     // FIXME: we should be mangling all of the above.
3089     return "";
3090 
3091   case CC_X86ThisCall:
3092     // FIXME: To match mingw GCC, thiscall should only be mangled in when it is
3093     // used explicitly. At this point, we don't have that much information in
3094     // the AST, since clang tends to bake the convention into the canonical
3095     // function type. thiscall only rarely used explicitly, so don't mangle it
3096     // for now.
3097     return "";
3098 
3099   case CC_X86StdCall:
3100     return "stdcall";
3101   case CC_X86FastCall:
3102     return "fastcall";
3103   case CC_X86_64SysV:
3104     return "sysv_abi";
3105   case CC_Win64:
3106     return "ms_abi";
3107   case CC_Swift:
3108     return "swiftcall";
3109   case CC_SwiftAsync:
3110     return "swiftasynccall";
3111   }
3112   llvm_unreachable("bad calling convention");
3113 }
3114 
mangleExtFunctionInfo(const FunctionType * T)3115 void CXXNameMangler::mangleExtFunctionInfo(const FunctionType *T) {
3116   // Fast path.
3117   if (T->getExtInfo() == FunctionType::ExtInfo())
3118     return;
3119 
3120   // Vendor-specific qualifiers are emitted in reverse alphabetical order.
3121   // This will get more complicated in the future if we mangle other
3122   // things here; but for now, since we mangle ns_returns_retained as
3123   // a qualifier on the result type, we can get away with this:
3124   StringRef CCQualifier = getCallingConvQualifierName(T->getExtInfo().getCC());
3125   if (!CCQualifier.empty())
3126     mangleVendorQualifier(CCQualifier);
3127 
3128   // FIXME: regparm
3129   // FIXME: noreturn
3130 }
3131 
3132 void
mangleExtParameterInfo(FunctionProtoType::ExtParameterInfo PI)3133 CXXNameMangler::mangleExtParameterInfo(FunctionProtoType::ExtParameterInfo PI) {
3134   // Vendor-specific qualifiers are emitted in reverse alphabetical order.
3135 
3136   // Note that these are *not* substitution candidates.  Demanglers might
3137   // have trouble with this if the parameter type is fully substituted.
3138 
3139   switch (PI.getABI()) {
3140   case ParameterABI::Ordinary:
3141     break;
3142 
3143   // All of these start with "swift", so they come before "ns_consumed".
3144   case ParameterABI::SwiftContext:
3145   case ParameterABI::SwiftAsyncContext:
3146   case ParameterABI::SwiftErrorResult:
3147   case ParameterABI::SwiftIndirectResult:
3148     mangleVendorQualifier(getParameterABISpelling(PI.getABI()));
3149     break;
3150   }
3151 
3152   if (PI.isConsumed())
3153     mangleVendorQualifier("ns_consumed");
3154 
3155   if (PI.isNoEscape())
3156     mangleVendorQualifier("noescape");
3157 }
3158 
3159 // <type>          ::= <function-type>
3160 // <function-type> ::= [<CV-qualifiers>] F [Y]
3161 //                      <bare-function-type> [<ref-qualifier>] E
mangleType(const FunctionProtoType * T)3162 void CXXNameMangler::mangleType(const FunctionProtoType *T) {
3163   mangleExtFunctionInfo(T);
3164 
3165   // Mangle CV-qualifiers, if present.  These are 'this' qualifiers,
3166   // e.g. "const" in "int (A::*)() const".
3167   mangleQualifiers(T->getMethodQuals());
3168 
3169   // Mangle instantiation-dependent exception-specification, if present,
3170   // per cxx-abi-dev proposal on 2016-10-11.
3171   if (T->hasInstantiationDependentExceptionSpec()) {
3172     if (isComputedNoexcept(T->getExceptionSpecType())) {
3173       Out << "DO";
3174       mangleExpression(T->getNoexceptExpr());
3175       Out << "E";
3176     } else {
3177       assert(T->getExceptionSpecType() == EST_Dynamic);
3178       Out << "Dw";
3179       for (auto ExceptTy : T->exceptions())
3180         mangleType(ExceptTy);
3181       Out << "E";
3182     }
3183   } else if (T->isNothrow()) {
3184     Out << "Do";
3185   }
3186 
3187   Out << 'F';
3188 
3189   // FIXME: We don't have enough information in the AST to produce the 'Y'
3190   // encoding for extern "C" function types.
3191   mangleBareFunctionType(T, /*MangleReturnType=*/true);
3192 
3193   // Mangle the ref-qualifier, if present.
3194   mangleRefQualifier(T->getRefQualifier());
3195 
3196   Out << 'E';
3197 }
3198 
mangleType(const FunctionNoProtoType * T)3199 void CXXNameMangler::mangleType(const FunctionNoProtoType *T) {
3200   // Function types without prototypes can arise when mangling a function type
3201   // within an overloadable function in C. We mangle these as the absence of any
3202   // parameter types (not even an empty parameter list).
3203   Out << 'F';
3204 
3205   FunctionTypeDepthState saved = FunctionTypeDepth.push();
3206 
3207   FunctionTypeDepth.enterResultType();
3208   mangleType(T->getReturnType());
3209   FunctionTypeDepth.leaveResultType();
3210 
3211   FunctionTypeDepth.pop(saved);
3212   Out << 'E';
3213 }
3214 
mangleBareFunctionType(const FunctionProtoType * Proto,bool MangleReturnType,const FunctionDecl * FD)3215 void CXXNameMangler::mangleBareFunctionType(const FunctionProtoType *Proto,
3216                                             bool MangleReturnType,
3217                                             const FunctionDecl *FD) {
3218   // Record that we're in a function type.  See mangleFunctionParam
3219   // for details on what we're trying to achieve here.
3220   FunctionTypeDepthState saved = FunctionTypeDepth.push();
3221 
3222   // <bare-function-type> ::= <signature type>+
3223   if (MangleReturnType) {
3224     FunctionTypeDepth.enterResultType();
3225 
3226     // Mangle ns_returns_retained as an order-sensitive qualifier here.
3227     if (Proto->getExtInfo().getProducesResult() && FD == nullptr)
3228       mangleVendorQualifier("ns_returns_retained");
3229 
3230     // Mangle the return type without any direct ARC ownership qualifiers.
3231     QualType ReturnTy = Proto->getReturnType();
3232     if (ReturnTy.getObjCLifetime()) {
3233       auto SplitReturnTy = ReturnTy.split();
3234       SplitReturnTy.Quals.removeObjCLifetime();
3235       ReturnTy = getASTContext().getQualifiedType(SplitReturnTy);
3236     }
3237     mangleType(ReturnTy);
3238 
3239     FunctionTypeDepth.leaveResultType();
3240   }
3241 
3242   if (Proto->getNumParams() == 0 && !Proto->isVariadic()) {
3243     //   <builtin-type> ::= v   # void
3244     Out << 'v';
3245 
3246     FunctionTypeDepth.pop(saved);
3247     return;
3248   }
3249 
3250   assert(!FD || FD->getNumParams() == Proto->getNumParams());
3251   for (unsigned I = 0, E = Proto->getNumParams(); I != E; ++I) {
3252     // Mangle extended parameter info as order-sensitive qualifiers here.
3253     if (Proto->hasExtParameterInfos() && FD == nullptr) {
3254       mangleExtParameterInfo(Proto->getExtParameterInfo(I));
3255     }
3256 
3257     // Mangle the type.
3258     QualType ParamTy = Proto->getParamType(I);
3259     mangleType(Context.getASTContext().getSignatureParameterType(ParamTy));
3260 
3261     if (FD) {
3262       if (auto *Attr = FD->getParamDecl(I)->getAttr<PassObjectSizeAttr>()) {
3263         // Attr can only take 1 character, so we can hardcode the length below.
3264         assert(Attr->getType() <= 9 && Attr->getType() >= 0);
3265         if (Attr->isDynamic())
3266           Out << "U25pass_dynamic_object_size" << Attr->getType();
3267         else
3268           Out << "U17pass_object_size" << Attr->getType();
3269       }
3270     }
3271   }
3272 
3273   FunctionTypeDepth.pop(saved);
3274 
3275   // <builtin-type>      ::= z  # ellipsis
3276   if (Proto->isVariadic())
3277     Out << 'z';
3278 }
3279 
3280 // <type>            ::= <class-enum-type>
3281 // <class-enum-type> ::= <name>
mangleType(const UnresolvedUsingType * T)3282 void CXXNameMangler::mangleType(const UnresolvedUsingType *T) {
3283   mangleName(T->getDecl());
3284 }
3285 
3286 // <type>            ::= <class-enum-type>
3287 // <class-enum-type> ::= <name>
mangleType(const EnumType * T)3288 void CXXNameMangler::mangleType(const EnumType *T) {
3289   mangleType(static_cast<const TagType*>(T));
3290 }
mangleType(const RecordType * T)3291 void CXXNameMangler::mangleType(const RecordType *T) {
3292   mangleType(static_cast<const TagType*>(T));
3293 }
mangleType(const TagType * T)3294 void CXXNameMangler::mangleType(const TagType *T) {
3295   mangleName(T->getDecl());
3296 }
3297 
3298 // <type>       ::= <array-type>
3299 // <array-type> ::= A <positive dimension number> _ <element type>
3300 //              ::= A [<dimension expression>] _ <element type>
mangleType(const ConstantArrayType * T)3301 void CXXNameMangler::mangleType(const ConstantArrayType *T) {
3302   Out << 'A' << T->getSize() << '_';
3303   mangleType(T->getElementType());
3304 }
mangleType(const VariableArrayType * T)3305 void CXXNameMangler::mangleType(const VariableArrayType *T) {
3306   Out << 'A';
3307   // decayed vla types (size 0) will just be skipped.
3308   if (T->getSizeExpr())
3309     mangleExpression(T->getSizeExpr());
3310   Out << '_';
3311   mangleType(T->getElementType());
3312 }
mangleType(const DependentSizedArrayType * T)3313 void CXXNameMangler::mangleType(const DependentSizedArrayType *T) {
3314   Out << 'A';
3315   // A DependentSizedArrayType might not have size expression as below
3316   //
3317   // template<int ...N> int arr[] = {N...};
3318   if (T->getSizeExpr())
3319     mangleExpression(T->getSizeExpr());
3320   Out << '_';
3321   mangleType(T->getElementType());
3322 }
mangleType(const IncompleteArrayType * T)3323 void CXXNameMangler::mangleType(const IncompleteArrayType *T) {
3324   Out << "A_";
3325   mangleType(T->getElementType());
3326 }
3327 
3328 // <type>                   ::= <pointer-to-member-type>
3329 // <pointer-to-member-type> ::= M <class type> <member type>
mangleType(const MemberPointerType * T)3330 void CXXNameMangler::mangleType(const MemberPointerType *T) {
3331   Out << 'M';
3332   mangleType(QualType(T->getClass(), 0));
3333   QualType PointeeType = T->getPointeeType();
3334   if (const FunctionProtoType *FPT = dyn_cast<FunctionProtoType>(PointeeType)) {
3335     mangleType(FPT);
3336 
3337     // Itanium C++ ABI 5.1.8:
3338     //
3339     //   The type of a non-static member function is considered to be different,
3340     //   for the purposes of substitution, from the type of a namespace-scope or
3341     //   static member function whose type appears similar. The types of two
3342     //   non-static member functions are considered to be different, for the
3343     //   purposes of substitution, if the functions are members of different
3344     //   classes. In other words, for the purposes of substitution, the class of
3345     //   which the function is a member is considered part of the type of
3346     //   function.
3347 
3348     // Given that we already substitute member function pointers as a
3349     // whole, the net effect of this rule is just to unconditionally
3350     // suppress substitution on the function type in a member pointer.
3351     // We increment the SeqID here to emulate adding an entry to the
3352     // substitution table.
3353     ++SeqID;
3354   } else
3355     mangleType(PointeeType);
3356 }
3357 
3358 // <type>           ::= <template-param>
mangleType(const TemplateTypeParmType * T)3359 void CXXNameMangler::mangleType(const TemplateTypeParmType *T) {
3360   mangleTemplateParameter(T->getDepth(), T->getIndex());
3361 }
3362 
3363 // <type>           ::= <template-param>
mangleType(const SubstTemplateTypeParmPackType * T)3364 void CXXNameMangler::mangleType(const SubstTemplateTypeParmPackType *T) {
3365   // FIXME: not clear how to mangle this!
3366   // template <class T...> class A {
3367   //   template <class U...> void foo(T(*)(U) x...);
3368   // };
3369   Out << "_SUBSTPACK_";
3370 }
3371 
3372 // <type> ::= P <type>   # pointer-to
mangleType(const PointerType * T)3373 void CXXNameMangler::mangleType(const PointerType *T) {
3374   Out << 'P';
3375   mangleType(T->getPointeeType());
3376 }
mangleType(const ObjCObjectPointerType * T)3377 void CXXNameMangler::mangleType(const ObjCObjectPointerType *T) {
3378   Out << 'P';
3379   mangleType(T->getPointeeType());
3380 }
3381 
3382 // <type> ::= R <type>   # reference-to
mangleType(const LValueReferenceType * T)3383 void CXXNameMangler::mangleType(const LValueReferenceType *T) {
3384   Out << 'R';
3385   mangleType(T->getPointeeType());
3386 }
3387 
3388 // <type> ::= O <type>   # rvalue reference-to (C++0x)
mangleType(const RValueReferenceType * T)3389 void CXXNameMangler::mangleType(const RValueReferenceType *T) {
3390   Out << 'O';
3391   mangleType(T->getPointeeType());
3392 }
3393 
3394 // <type> ::= C <type>   # complex pair (C 2000)
mangleType(const ComplexType * T)3395 void CXXNameMangler::mangleType(const ComplexType *T) {
3396   Out << 'C';
3397   mangleType(T->getElementType());
3398 }
3399 
3400 // ARM's ABI for Neon vector types specifies that they should be mangled as
3401 // if they are structs (to match ARM's initial implementation).  The
3402 // vector type must be one of the special types predefined by ARM.
mangleNeonVectorType(const VectorType * T)3403 void CXXNameMangler::mangleNeonVectorType(const VectorType *T) {
3404   QualType EltType = T->getElementType();
3405   assert(EltType->isBuiltinType() && "Neon vector element not a BuiltinType");
3406   const char *EltName = nullptr;
3407   if (T->getVectorKind() == VectorType::NeonPolyVector) {
3408     switch (cast<BuiltinType>(EltType)->getKind()) {
3409     case BuiltinType::SChar:
3410     case BuiltinType::UChar:
3411       EltName = "poly8_t";
3412       break;
3413     case BuiltinType::Short:
3414     case BuiltinType::UShort:
3415       EltName = "poly16_t";
3416       break;
3417     case BuiltinType::LongLong:
3418     case BuiltinType::ULongLong:
3419       EltName = "poly64_t";
3420       break;
3421     default: llvm_unreachable("unexpected Neon polynomial vector element type");
3422     }
3423   } else {
3424     switch (cast<BuiltinType>(EltType)->getKind()) {
3425     case BuiltinType::SChar:     EltName = "int8_t"; break;
3426     case BuiltinType::UChar:     EltName = "uint8_t"; break;
3427     case BuiltinType::Short:     EltName = "int16_t"; break;
3428     case BuiltinType::UShort:    EltName = "uint16_t"; break;
3429     case BuiltinType::Int:       EltName = "int32_t"; break;
3430     case BuiltinType::UInt:      EltName = "uint32_t"; break;
3431     case BuiltinType::LongLong:  EltName = "int64_t"; break;
3432     case BuiltinType::ULongLong: EltName = "uint64_t"; break;
3433     case BuiltinType::Double:    EltName = "float64_t"; break;
3434     case BuiltinType::Float:     EltName = "float32_t"; break;
3435     case BuiltinType::Half:      EltName = "float16_t"; break;
3436     case BuiltinType::BFloat16:  EltName = "bfloat16_t"; break;
3437     default:
3438       llvm_unreachable("unexpected Neon vector element type");
3439     }
3440   }
3441   const char *BaseName = nullptr;
3442   unsigned BitSize = (T->getNumElements() *
3443                       getASTContext().getTypeSize(EltType));
3444   if (BitSize == 64)
3445     BaseName = "__simd64_";
3446   else {
3447     assert(BitSize == 128 && "Neon vector type not 64 or 128 bits");
3448     BaseName = "__simd128_";
3449   }
3450   Out << strlen(BaseName) + strlen(EltName);
3451   Out << BaseName << EltName;
3452 }
3453 
mangleNeonVectorType(const DependentVectorType * T)3454 void CXXNameMangler::mangleNeonVectorType(const DependentVectorType *T) {
3455   DiagnosticsEngine &Diags = Context.getDiags();
3456   unsigned DiagID = Diags.getCustomDiagID(
3457       DiagnosticsEngine::Error,
3458       "cannot mangle this dependent neon vector type yet");
3459   Diags.Report(T->getAttributeLoc(), DiagID);
3460 }
3461 
mangleAArch64VectorBase(const BuiltinType * EltType)3462 static StringRef mangleAArch64VectorBase(const BuiltinType *EltType) {
3463   switch (EltType->getKind()) {
3464   case BuiltinType::SChar:
3465     return "Int8";
3466   case BuiltinType::Short:
3467     return "Int16";
3468   case BuiltinType::Int:
3469     return "Int32";
3470   case BuiltinType::Long:
3471   case BuiltinType::LongLong:
3472     return "Int64";
3473   case BuiltinType::UChar:
3474     return "Uint8";
3475   case BuiltinType::UShort:
3476     return "Uint16";
3477   case BuiltinType::UInt:
3478     return "Uint32";
3479   case BuiltinType::ULong:
3480   case BuiltinType::ULongLong:
3481     return "Uint64";
3482   case BuiltinType::Half:
3483     return "Float16";
3484   case BuiltinType::Float:
3485     return "Float32";
3486   case BuiltinType::Double:
3487     return "Float64";
3488   case BuiltinType::BFloat16:
3489     return "Bfloat16";
3490   default:
3491     llvm_unreachable("Unexpected vector element base type");
3492   }
3493 }
3494 
3495 // AArch64's ABI for Neon vector types specifies that they should be mangled as
3496 // the equivalent internal name. The vector type must be one of the special
3497 // types predefined by ARM.
mangleAArch64NeonVectorType(const VectorType * T)3498 void CXXNameMangler::mangleAArch64NeonVectorType(const VectorType *T) {
3499   QualType EltType = T->getElementType();
3500   assert(EltType->isBuiltinType() && "Neon vector element not a BuiltinType");
3501   unsigned BitSize =
3502       (T->getNumElements() * getASTContext().getTypeSize(EltType));
3503   (void)BitSize; // Silence warning.
3504 
3505   assert((BitSize == 64 || BitSize == 128) &&
3506          "Neon vector type not 64 or 128 bits");
3507 
3508   StringRef EltName;
3509   if (T->getVectorKind() == VectorType::NeonPolyVector) {
3510     switch (cast<BuiltinType>(EltType)->getKind()) {
3511     case BuiltinType::UChar:
3512       EltName = "Poly8";
3513       break;
3514     case BuiltinType::UShort:
3515       EltName = "Poly16";
3516       break;
3517     case BuiltinType::ULong:
3518     case BuiltinType::ULongLong:
3519       EltName = "Poly64";
3520       break;
3521     default:
3522       llvm_unreachable("unexpected Neon polynomial vector element type");
3523     }
3524   } else
3525     EltName = mangleAArch64VectorBase(cast<BuiltinType>(EltType));
3526 
3527   std::string TypeName =
3528       ("__" + EltName + "x" + Twine(T->getNumElements()) + "_t").str();
3529   Out << TypeName.length() << TypeName;
3530 }
mangleAArch64NeonVectorType(const DependentVectorType * T)3531 void CXXNameMangler::mangleAArch64NeonVectorType(const DependentVectorType *T) {
3532   DiagnosticsEngine &Diags = Context.getDiags();
3533   unsigned DiagID = Diags.getCustomDiagID(
3534       DiagnosticsEngine::Error,
3535       "cannot mangle this dependent neon vector type yet");
3536   Diags.Report(T->getAttributeLoc(), DiagID);
3537 }
3538 
3539 // The AArch64 ACLE specifies that fixed-length SVE vector and predicate types
3540 // defined with the 'arm_sve_vector_bits' attribute map to the same AAPCS64
3541 // type as the sizeless variants.
3542 //
3543 // The mangling scheme for VLS types is implemented as a "pseudo" template:
3544 //
3545 //   '__SVE_VLS<<type>, <vector length>>'
3546 //
3547 // Combining the existing SVE type and a specific vector length (in bits).
3548 // For example:
3549 //
3550 //   typedef __SVInt32_t foo __attribute__((arm_sve_vector_bits(512)));
3551 //
3552 // is described as '__SVE_VLS<__SVInt32_t, 512u>' and mangled as:
3553 //
3554 //   "9__SVE_VLSI" + base type mangling + "Lj" + __ARM_FEATURE_SVE_BITS + "EE"
3555 //
3556 //   i.e. 9__SVE_VLSIu11__SVInt32_tLj512EE
3557 //
3558 // The latest ACLE specification (00bet5) does not contain details of this
3559 // mangling scheme, it will be specified in the next revision. The mangling
3560 // scheme is otherwise defined in the appendices to the Procedure Call Standard
3561 // for the Arm Architecture, see
3562 // https://github.com/ARM-software/abi-aa/blob/master/aapcs64/aapcs64.rst#appendix-c-mangling
mangleAArch64FixedSveVectorType(const VectorType * T)3563 void CXXNameMangler::mangleAArch64FixedSveVectorType(const VectorType *T) {
3564   assert((T->getVectorKind() == VectorType::SveFixedLengthDataVector ||
3565           T->getVectorKind() == VectorType::SveFixedLengthPredicateVector) &&
3566          "expected fixed-length SVE vector!");
3567 
3568   QualType EltType = T->getElementType();
3569   assert(EltType->isBuiltinType() &&
3570          "expected builtin type for fixed-length SVE vector!");
3571 
3572   StringRef TypeName;
3573   switch (cast<BuiltinType>(EltType)->getKind()) {
3574   case BuiltinType::SChar:
3575     TypeName = "__SVInt8_t";
3576     break;
3577   case BuiltinType::UChar: {
3578     if (T->getVectorKind() == VectorType::SveFixedLengthDataVector)
3579       TypeName = "__SVUint8_t";
3580     else
3581       TypeName = "__SVBool_t";
3582     break;
3583   }
3584   case BuiltinType::Short:
3585     TypeName = "__SVInt16_t";
3586     break;
3587   case BuiltinType::UShort:
3588     TypeName = "__SVUint16_t";
3589     break;
3590   case BuiltinType::Int:
3591     TypeName = "__SVInt32_t";
3592     break;
3593   case BuiltinType::UInt:
3594     TypeName = "__SVUint32_t";
3595     break;
3596   case BuiltinType::Long:
3597     TypeName = "__SVInt64_t";
3598     break;
3599   case BuiltinType::ULong:
3600     TypeName = "__SVUint64_t";
3601     break;
3602   case BuiltinType::Half:
3603     TypeName = "__SVFloat16_t";
3604     break;
3605   case BuiltinType::Float:
3606     TypeName = "__SVFloat32_t";
3607     break;
3608   case BuiltinType::Double:
3609     TypeName = "__SVFloat64_t";
3610     break;
3611   case BuiltinType::BFloat16:
3612     TypeName = "__SVBfloat16_t";
3613     break;
3614   default:
3615     llvm_unreachable("unexpected element type for fixed-length SVE vector!");
3616   }
3617 
3618   unsigned VecSizeInBits = getASTContext().getTypeInfo(T).Width;
3619 
3620   if (T->getVectorKind() == VectorType::SveFixedLengthPredicateVector)
3621     VecSizeInBits *= 8;
3622 
3623   Out << "9__SVE_VLSI" << 'u' << TypeName.size() << TypeName << "Lj"
3624       << VecSizeInBits << "EE";
3625 }
3626 
mangleAArch64FixedSveVectorType(const DependentVectorType * T)3627 void CXXNameMangler::mangleAArch64FixedSveVectorType(
3628     const DependentVectorType *T) {
3629   DiagnosticsEngine &Diags = Context.getDiags();
3630   unsigned DiagID = Diags.getCustomDiagID(
3631       DiagnosticsEngine::Error,
3632       "cannot mangle this dependent fixed-length SVE vector type yet");
3633   Diags.Report(T->getAttributeLoc(), DiagID);
3634 }
3635 
3636 // GNU extension: vector types
3637 // <type>                  ::= <vector-type>
3638 // <vector-type>           ::= Dv <positive dimension number> _
3639 //                                    <extended element type>
3640 //                         ::= Dv [<dimension expression>] _ <element type>
3641 // <extended element type> ::= <element type>
3642 //                         ::= p # AltiVec vector pixel
3643 //                         ::= b # Altivec vector bool
mangleType(const VectorType * T)3644 void CXXNameMangler::mangleType(const VectorType *T) {
3645   if ((T->getVectorKind() == VectorType::NeonVector ||
3646        T->getVectorKind() == VectorType::NeonPolyVector)) {
3647     llvm::Triple Target = getASTContext().getTargetInfo().getTriple();
3648     llvm::Triple::ArchType Arch =
3649         getASTContext().getTargetInfo().getTriple().getArch();
3650     if ((Arch == llvm::Triple::aarch64 ||
3651          Arch == llvm::Triple::aarch64_be) && !Target.isOSDarwin())
3652       mangleAArch64NeonVectorType(T);
3653     else
3654       mangleNeonVectorType(T);
3655     return;
3656   } else if (T->getVectorKind() == VectorType::SveFixedLengthDataVector ||
3657              T->getVectorKind() == VectorType::SveFixedLengthPredicateVector) {
3658     mangleAArch64FixedSveVectorType(T);
3659     return;
3660   }
3661   Out << "Dv" << T->getNumElements() << '_';
3662   if (T->getVectorKind() == VectorType::AltiVecPixel)
3663     Out << 'p';
3664   else if (T->getVectorKind() == VectorType::AltiVecBool)
3665     Out << 'b';
3666   else
3667     mangleType(T->getElementType());
3668 }
3669 
mangleType(const DependentVectorType * T)3670 void CXXNameMangler::mangleType(const DependentVectorType *T) {
3671   if ((T->getVectorKind() == VectorType::NeonVector ||
3672        T->getVectorKind() == VectorType::NeonPolyVector)) {
3673     llvm::Triple Target = getASTContext().getTargetInfo().getTriple();
3674     llvm::Triple::ArchType Arch =
3675         getASTContext().getTargetInfo().getTriple().getArch();
3676     if ((Arch == llvm::Triple::aarch64 || Arch == llvm::Triple::aarch64_be) &&
3677         !Target.isOSDarwin())
3678       mangleAArch64NeonVectorType(T);
3679     else
3680       mangleNeonVectorType(T);
3681     return;
3682   } else if (T->getVectorKind() == VectorType::SveFixedLengthDataVector ||
3683              T->getVectorKind() == VectorType::SveFixedLengthPredicateVector) {
3684     mangleAArch64FixedSveVectorType(T);
3685     return;
3686   }
3687 
3688   Out << "Dv";
3689   mangleExpression(T->getSizeExpr());
3690   Out << '_';
3691   if (T->getVectorKind() == VectorType::AltiVecPixel)
3692     Out << 'p';
3693   else if (T->getVectorKind() == VectorType::AltiVecBool)
3694     Out << 'b';
3695   else
3696     mangleType(T->getElementType());
3697 }
3698 
mangleType(const ExtVectorType * T)3699 void CXXNameMangler::mangleType(const ExtVectorType *T) {
3700   mangleType(static_cast<const VectorType*>(T));
3701 }
mangleType(const DependentSizedExtVectorType * T)3702 void CXXNameMangler::mangleType(const DependentSizedExtVectorType *T) {
3703   Out << "Dv";
3704   mangleExpression(T->getSizeExpr());
3705   Out << '_';
3706   mangleType(T->getElementType());
3707 }
3708 
mangleType(const ConstantMatrixType * T)3709 void CXXNameMangler::mangleType(const ConstantMatrixType *T) {
3710   // Mangle matrix types as a vendor extended type:
3711   // u<Len>matrix_typeI<Rows><Columns><element type>E
3712 
3713   StringRef VendorQualifier = "matrix_type";
3714   Out << "u" << VendorQualifier.size() << VendorQualifier;
3715 
3716   Out << "I";
3717   auto &ASTCtx = getASTContext();
3718   unsigned BitWidth = ASTCtx.getTypeSize(ASTCtx.getSizeType());
3719   llvm::APSInt Rows(BitWidth);
3720   Rows = T->getNumRows();
3721   mangleIntegerLiteral(ASTCtx.getSizeType(), Rows);
3722   llvm::APSInt Columns(BitWidth);
3723   Columns = T->getNumColumns();
3724   mangleIntegerLiteral(ASTCtx.getSizeType(), Columns);
3725   mangleType(T->getElementType());
3726   Out << "E";
3727 }
3728 
mangleType(const DependentSizedMatrixType * T)3729 void CXXNameMangler::mangleType(const DependentSizedMatrixType *T) {
3730   // Mangle matrix types as a vendor extended type:
3731   // u<Len>matrix_typeI<row expr><column expr><element type>E
3732   StringRef VendorQualifier = "matrix_type";
3733   Out << "u" << VendorQualifier.size() << VendorQualifier;
3734 
3735   Out << "I";
3736   mangleTemplateArgExpr(T->getRowExpr());
3737   mangleTemplateArgExpr(T->getColumnExpr());
3738   mangleType(T->getElementType());
3739   Out << "E";
3740 }
3741 
mangleType(const DependentAddressSpaceType * T)3742 void CXXNameMangler::mangleType(const DependentAddressSpaceType *T) {
3743   SplitQualType split = T->getPointeeType().split();
3744   mangleQualifiers(split.Quals, T);
3745   mangleType(QualType(split.Ty, 0));
3746 }
3747 
mangleType(const PackExpansionType * T)3748 void CXXNameMangler::mangleType(const PackExpansionType *T) {
3749   // <type>  ::= Dp <type>          # pack expansion (C++0x)
3750   Out << "Dp";
3751   mangleType(T->getPattern());
3752 }
3753 
mangleType(const ObjCInterfaceType * T)3754 void CXXNameMangler::mangleType(const ObjCInterfaceType *T) {
3755   mangleSourceName(T->getDecl()->getIdentifier());
3756 }
3757 
mangleType(const ObjCObjectType * T)3758 void CXXNameMangler::mangleType(const ObjCObjectType *T) {
3759   // Treat __kindof as a vendor extended type qualifier.
3760   if (T->isKindOfType())
3761     Out << "U8__kindof";
3762 
3763   if (!T->qual_empty()) {
3764     // Mangle protocol qualifiers.
3765     SmallString<64> QualStr;
3766     llvm::raw_svector_ostream QualOS(QualStr);
3767     QualOS << "objcproto";
3768     for (const auto *I : T->quals()) {
3769       StringRef name = I->getName();
3770       QualOS << name.size() << name;
3771     }
3772     Out << 'U' << QualStr.size() << QualStr;
3773   }
3774 
3775   mangleType(T->getBaseType());
3776 
3777   if (T->isSpecialized()) {
3778     // Mangle type arguments as I <type>+ E
3779     Out << 'I';
3780     for (auto typeArg : T->getTypeArgs())
3781       mangleType(typeArg);
3782     Out << 'E';
3783   }
3784 }
3785 
mangleType(const BlockPointerType * T)3786 void CXXNameMangler::mangleType(const BlockPointerType *T) {
3787   Out << "U13block_pointer";
3788   mangleType(T->getPointeeType());
3789 }
3790 
mangleType(const InjectedClassNameType * T)3791 void CXXNameMangler::mangleType(const InjectedClassNameType *T) {
3792   // Mangle injected class name types as if the user had written the
3793   // specialization out fully.  It may not actually be possible to see
3794   // this mangling, though.
3795   mangleType(T->getInjectedSpecializationType());
3796 }
3797 
mangleType(const TemplateSpecializationType * T)3798 void CXXNameMangler::mangleType(const TemplateSpecializationType *T) {
3799   if (TemplateDecl *TD = T->getTemplateName().getAsTemplateDecl()) {
3800     mangleTemplateName(TD, T->getArgs(), T->getNumArgs());
3801   } else {
3802     if (mangleSubstitution(QualType(T, 0)))
3803       return;
3804 
3805     mangleTemplatePrefix(T->getTemplateName());
3806 
3807     // FIXME: GCC does not appear to mangle the template arguments when
3808     // the template in question is a dependent template name. Should we
3809     // emulate that badness?
3810     mangleTemplateArgs(T->getTemplateName(), T->getArgs(), T->getNumArgs());
3811     addSubstitution(QualType(T, 0));
3812   }
3813 }
3814 
mangleType(const DependentNameType * T)3815 void CXXNameMangler::mangleType(const DependentNameType *T) {
3816   // Proposal by cxx-abi-dev, 2014-03-26
3817   // <class-enum-type> ::= <name>    # non-dependent or dependent type name or
3818   //                                 # dependent elaborated type specifier using
3819   //                                 # 'typename'
3820   //                   ::= Ts <name> # dependent elaborated type specifier using
3821   //                                 # 'struct' or 'class'
3822   //                   ::= Tu <name> # dependent elaborated type specifier using
3823   //                                 # 'union'
3824   //                   ::= Te <name> # dependent elaborated type specifier using
3825   //                                 # 'enum'
3826   switch (T->getKeyword()) {
3827     case ETK_None:
3828     case ETK_Typename:
3829       break;
3830     case ETK_Struct:
3831     case ETK_Class:
3832     case ETK_Interface:
3833       Out << "Ts";
3834       break;
3835     case ETK_Union:
3836       Out << "Tu";
3837       break;
3838     case ETK_Enum:
3839       Out << "Te";
3840       break;
3841   }
3842   // Typename types are always nested
3843   Out << 'N';
3844   manglePrefix(T->getQualifier());
3845   mangleSourceName(T->getIdentifier());
3846   Out << 'E';
3847 }
3848 
mangleType(const DependentTemplateSpecializationType * T)3849 void CXXNameMangler::mangleType(const DependentTemplateSpecializationType *T) {
3850   // Dependently-scoped template types are nested if they have a prefix.
3851   Out << 'N';
3852 
3853   // TODO: avoid making this TemplateName.
3854   TemplateName Prefix =
3855     getASTContext().getDependentTemplateName(T->getQualifier(),
3856                                              T->getIdentifier());
3857   mangleTemplatePrefix(Prefix);
3858 
3859   // FIXME: GCC does not appear to mangle the template arguments when
3860   // the template in question is a dependent template name. Should we
3861   // emulate that badness?
3862   mangleTemplateArgs(Prefix, T->getArgs(), T->getNumArgs());
3863   Out << 'E';
3864 }
3865 
mangleType(const TypeOfType * T)3866 void CXXNameMangler::mangleType(const TypeOfType *T) {
3867   // FIXME: this is pretty unsatisfactory, but there isn't an obvious
3868   // "extension with parameters" mangling.
3869   Out << "u6typeof";
3870 }
3871 
mangleType(const TypeOfExprType * T)3872 void CXXNameMangler::mangleType(const TypeOfExprType *T) {
3873   // FIXME: this is pretty unsatisfactory, but there isn't an obvious
3874   // "extension with parameters" mangling.
3875   Out << "u6typeof";
3876 }
3877 
mangleType(const DecltypeType * T)3878 void CXXNameMangler::mangleType(const DecltypeType *T) {
3879   Expr *E = T->getUnderlyingExpr();
3880 
3881   // type ::= Dt <expression> E  # decltype of an id-expression
3882   //                             #   or class member access
3883   //      ::= DT <expression> E  # decltype of an expression
3884 
3885   // This purports to be an exhaustive list of id-expressions and
3886   // class member accesses.  Note that we do not ignore parentheses;
3887   // parentheses change the semantics of decltype for these
3888   // expressions (and cause the mangler to use the other form).
3889   if (isa<DeclRefExpr>(E) ||
3890       isa<MemberExpr>(E) ||
3891       isa<UnresolvedLookupExpr>(E) ||
3892       isa<DependentScopeDeclRefExpr>(E) ||
3893       isa<CXXDependentScopeMemberExpr>(E) ||
3894       isa<UnresolvedMemberExpr>(E))
3895     Out << "Dt";
3896   else
3897     Out << "DT";
3898   mangleExpression(E);
3899   Out << 'E';
3900 }
3901 
mangleType(const UnaryTransformType * T)3902 void CXXNameMangler::mangleType(const UnaryTransformType *T) {
3903   // If this is dependent, we need to record that. If not, we simply
3904   // mangle it as the underlying type since they are equivalent.
3905   if (T->isDependentType()) {
3906     Out << 'U';
3907 
3908     switch (T->getUTTKind()) {
3909       case UnaryTransformType::EnumUnderlyingType:
3910         Out << "3eut";
3911         break;
3912     }
3913   }
3914 
3915   mangleType(T->getBaseType());
3916 }
3917 
mangleType(const AutoType * T)3918 void CXXNameMangler::mangleType(const AutoType *T) {
3919   assert(T->getDeducedType().isNull() &&
3920          "Deduced AutoType shouldn't be handled here!");
3921   assert(T->getKeyword() != AutoTypeKeyword::GNUAutoType &&
3922          "shouldn't need to mangle __auto_type!");
3923   // <builtin-type> ::= Da # auto
3924   //                ::= Dc # decltype(auto)
3925   Out << (T->isDecltypeAuto() ? "Dc" : "Da");
3926 }
3927 
mangleType(const DeducedTemplateSpecializationType * T)3928 void CXXNameMangler::mangleType(const DeducedTemplateSpecializationType *T) {
3929   QualType Deduced = T->getDeducedType();
3930   if (!Deduced.isNull())
3931     return mangleType(Deduced);
3932 
3933   TemplateDecl *TD = T->getTemplateName().getAsTemplateDecl();
3934   assert(TD && "shouldn't form deduced TST unless we know we have a template");
3935 
3936   if (mangleSubstitution(TD))
3937     return;
3938 
3939   mangleName(GlobalDecl(TD));
3940   addSubstitution(TD);
3941 }
3942 
mangleType(const AtomicType * T)3943 void CXXNameMangler::mangleType(const AtomicType *T) {
3944   // <type> ::= U <source-name> <type>  # vendor extended type qualifier
3945   // (Until there's a standardized mangling...)
3946   Out << "U7_Atomic";
3947   mangleType(T->getValueType());
3948 }
3949 
mangleType(const PipeType * T)3950 void CXXNameMangler::mangleType(const PipeType *T) {
3951   // Pipe type mangling rules are described in SPIR 2.0 specification
3952   // A.1 Data types and A.3 Summary of changes
3953   // <type> ::= 8ocl_pipe
3954   Out << "8ocl_pipe";
3955 }
3956 
mangleType(const ExtIntType * T)3957 void CXXNameMangler::mangleType(const ExtIntType *T) {
3958   Out << "U7_ExtInt";
3959   llvm::APSInt BW(32, true);
3960   BW = T->getNumBits();
3961   TemplateArgument TA(Context.getASTContext(), BW, getASTContext().IntTy);
3962   mangleTemplateArgs(TemplateName(), &TA, 1);
3963   if (T->isUnsigned())
3964     Out << "j";
3965   else
3966     Out << "i";
3967 }
3968 
mangleType(const DependentExtIntType * T)3969 void CXXNameMangler::mangleType(const DependentExtIntType *T) {
3970   Out << "U7_ExtInt";
3971   TemplateArgument TA(T->getNumBitsExpr());
3972   mangleTemplateArgs(TemplateName(), &TA, 1);
3973   if (T->isUnsigned())
3974     Out << "j";
3975   else
3976     Out << "i";
3977 }
3978 
mangleIntegerLiteral(QualType T,const llvm::APSInt & Value)3979 void CXXNameMangler::mangleIntegerLiteral(QualType T,
3980                                           const llvm::APSInt &Value) {
3981   //  <expr-primary> ::= L <type> <value number> E # integer literal
3982   Out << 'L';
3983 
3984   mangleType(T);
3985   if (T->isBooleanType()) {
3986     // Boolean values are encoded as 0/1.
3987     Out << (Value.getBoolValue() ? '1' : '0');
3988   } else {
3989     mangleNumber(Value);
3990   }
3991   Out << 'E';
3992 
3993 }
3994 
mangleMemberExprBase(const Expr * Base,bool IsArrow)3995 void CXXNameMangler::mangleMemberExprBase(const Expr *Base, bool IsArrow) {
3996   // Ignore member expressions involving anonymous unions.
3997   while (const auto *RT = Base->getType()->getAs<RecordType>()) {
3998     if (!RT->getDecl()->isAnonymousStructOrUnion())
3999       break;
4000     const auto *ME = dyn_cast<MemberExpr>(Base);
4001     if (!ME)
4002       break;
4003     Base = ME->getBase();
4004     IsArrow = ME->isArrow();
4005   }
4006 
4007   if (Base->isImplicitCXXThis()) {
4008     // Note: GCC mangles member expressions to the implicit 'this' as
4009     // *this., whereas we represent them as this->. The Itanium C++ ABI
4010     // does not specify anything here, so we follow GCC.
4011     Out << "dtdefpT";
4012   } else {
4013     Out << (IsArrow ? "pt" : "dt");
4014     mangleExpression(Base);
4015   }
4016 }
4017 
4018 /// Mangles a member expression.
mangleMemberExpr(const Expr * base,bool isArrow,NestedNameSpecifier * qualifier,NamedDecl * firstQualifierLookup,DeclarationName member,const TemplateArgumentLoc * TemplateArgs,unsigned NumTemplateArgs,unsigned arity)4019 void CXXNameMangler::mangleMemberExpr(const Expr *base,
4020                                       bool isArrow,
4021                                       NestedNameSpecifier *qualifier,
4022                                       NamedDecl *firstQualifierLookup,
4023                                       DeclarationName member,
4024                                       const TemplateArgumentLoc *TemplateArgs,
4025                                       unsigned NumTemplateArgs,
4026                                       unsigned arity) {
4027   // <expression> ::= dt <expression> <unresolved-name>
4028   //              ::= pt <expression> <unresolved-name>
4029   if (base)
4030     mangleMemberExprBase(base, isArrow);
4031   mangleUnresolvedName(qualifier, member, TemplateArgs, NumTemplateArgs, arity);
4032 }
4033 
4034 /// Look at the callee of the given call expression and determine if
4035 /// it's a parenthesized id-expression which would have triggered ADL
4036 /// otherwise.
isParenthesizedADLCallee(const CallExpr * call)4037 static bool isParenthesizedADLCallee(const CallExpr *call) {
4038   const Expr *callee = call->getCallee();
4039   const Expr *fn = callee->IgnoreParens();
4040 
4041   // Must be parenthesized.  IgnoreParens() skips __extension__ nodes,
4042   // too, but for those to appear in the callee, it would have to be
4043   // parenthesized.
4044   if (callee == fn) return false;
4045 
4046   // Must be an unresolved lookup.
4047   const UnresolvedLookupExpr *lookup = dyn_cast<UnresolvedLookupExpr>(fn);
4048   if (!lookup) return false;
4049 
4050   assert(!lookup->requiresADL());
4051 
4052   // Must be an unqualified lookup.
4053   if (lookup->getQualifier()) return false;
4054 
4055   // Must not have found a class member.  Note that if one is a class
4056   // member, they're all class members.
4057   if (lookup->getNumDecls() > 0 &&
4058       (*lookup->decls_begin())->isCXXClassMember())
4059     return false;
4060 
4061   // Otherwise, ADL would have been triggered.
4062   return true;
4063 }
4064 
mangleCastExpression(const Expr * E,StringRef CastEncoding)4065 void CXXNameMangler::mangleCastExpression(const Expr *E, StringRef CastEncoding) {
4066   const ExplicitCastExpr *ECE = cast<ExplicitCastExpr>(E);
4067   Out << CastEncoding;
4068   mangleType(ECE->getType());
4069   mangleExpression(ECE->getSubExpr());
4070 }
4071 
mangleInitListElements(const InitListExpr * InitList)4072 void CXXNameMangler::mangleInitListElements(const InitListExpr *InitList) {
4073   if (auto *Syntactic = InitList->getSyntacticForm())
4074     InitList = Syntactic;
4075   for (unsigned i = 0, e = InitList->getNumInits(); i != e; ++i)
4076     mangleExpression(InitList->getInit(i));
4077 }
4078 
mangleExpression(const Expr * E,unsigned Arity,bool AsTemplateArg)4079 void CXXNameMangler::mangleExpression(const Expr *E, unsigned Arity,
4080                                       bool AsTemplateArg) {
4081   // <expression> ::= <unary operator-name> <expression>
4082   //              ::= <binary operator-name> <expression> <expression>
4083   //              ::= <trinary operator-name> <expression> <expression> <expression>
4084   //              ::= cv <type> expression           # conversion with one argument
4085   //              ::= cv <type> _ <expression>* E # conversion with a different number of arguments
4086   //              ::= dc <type> <expression>         # dynamic_cast<type> (expression)
4087   //              ::= sc <type> <expression>         # static_cast<type> (expression)
4088   //              ::= cc <type> <expression>         # const_cast<type> (expression)
4089   //              ::= rc <type> <expression>         # reinterpret_cast<type> (expression)
4090   //              ::= st <type>                      # sizeof (a type)
4091   //              ::= at <type>                      # alignof (a type)
4092   //              ::= <template-param>
4093   //              ::= <function-param>
4094   //              ::= fpT                            # 'this' expression (part of <function-param>)
4095   //              ::= sr <type> <unqualified-name>                   # dependent name
4096   //              ::= sr <type> <unqualified-name> <template-args>   # dependent template-id
4097   //              ::= ds <expression> <expression>                   # expr.*expr
4098   //              ::= sZ <template-param>                            # size of a parameter pack
4099   //              ::= sZ <function-param>    # size of a function parameter pack
4100   //              ::= u <source-name> <template-arg>* E # vendor extended expression
4101   //              ::= <expr-primary>
4102   // <expr-primary> ::= L <type> <value number> E    # integer literal
4103   //                ::= L <type> <value float> E     # floating literal
4104   //                ::= L <type> <string type> E     # string literal
4105   //                ::= L <nullptr type> E           # nullptr literal "LDnE"
4106   //                ::= L <pointer type> 0 E         # null pointer template argument
4107   //                ::= L <type> <real-part float> _ <imag-part float> E    # complex floating point literal (C99); not used by clang
4108   //                ::= L <mangled-name> E           # external name
4109   QualType ImplicitlyConvertedToType;
4110 
4111   // A top-level expression that's not <expr-primary> needs to be wrapped in
4112   // X...E in a template arg.
4113   bool IsPrimaryExpr = true;
4114   auto NotPrimaryExpr = [&] {
4115     if (AsTemplateArg && IsPrimaryExpr)
4116       Out << 'X';
4117     IsPrimaryExpr = false;
4118   };
4119 
4120   auto MangleDeclRefExpr = [&](const NamedDecl *D) {
4121     switch (D->getKind()) {
4122     default:
4123       //  <expr-primary> ::= L <mangled-name> E # external name
4124       Out << 'L';
4125       mangle(D);
4126       Out << 'E';
4127       break;
4128 
4129     case Decl::ParmVar:
4130       NotPrimaryExpr();
4131       mangleFunctionParam(cast<ParmVarDecl>(D));
4132       break;
4133 
4134     case Decl::EnumConstant: {
4135       // <expr-primary>
4136       const EnumConstantDecl *ED = cast<EnumConstantDecl>(D);
4137       mangleIntegerLiteral(ED->getType(), ED->getInitVal());
4138       break;
4139     }
4140 
4141     case Decl::NonTypeTemplateParm:
4142       NotPrimaryExpr();
4143       const NonTypeTemplateParmDecl *PD = cast<NonTypeTemplateParmDecl>(D);
4144       mangleTemplateParameter(PD->getDepth(), PD->getIndex());
4145       break;
4146     }
4147   };
4148 
4149   // 'goto recurse' is used when handling a simple "unwrapping" node which
4150   // produces no output, where ImplicitlyConvertedToType and AsTemplateArg need
4151   // to be preserved.
4152 recurse:
4153   switch (E->getStmtClass()) {
4154   case Expr::NoStmtClass:
4155 #define ABSTRACT_STMT(Type)
4156 #define EXPR(Type, Base)
4157 #define STMT(Type, Base) \
4158   case Expr::Type##Class:
4159 #include "clang/AST/StmtNodes.inc"
4160     // fallthrough
4161 
4162   // These all can only appear in local or variable-initialization
4163   // contexts and so should never appear in a mangling.
4164   case Expr::AddrLabelExprClass:
4165   case Expr::DesignatedInitUpdateExprClass:
4166   case Expr::ImplicitValueInitExprClass:
4167   case Expr::ArrayInitLoopExprClass:
4168   case Expr::ArrayInitIndexExprClass:
4169   case Expr::NoInitExprClass:
4170   case Expr::ParenListExprClass:
4171   case Expr::LambdaExprClass:
4172   case Expr::MSPropertyRefExprClass:
4173   case Expr::MSPropertySubscriptExprClass:
4174   case Expr::TypoExprClass: // This should no longer exist in the AST by now.
4175   case Expr::RecoveryExprClass:
4176   case Expr::OMPArraySectionExprClass:
4177   case Expr::OMPArrayShapingExprClass:
4178   case Expr::OMPIteratorExprClass:
4179   case Expr::CXXInheritedCtorInitExprClass:
4180     llvm_unreachable("unexpected statement kind");
4181 
4182   case Expr::ConstantExprClass:
4183     E = cast<ConstantExpr>(E)->getSubExpr();
4184     goto recurse;
4185 
4186   // FIXME: invent manglings for all these.
4187   case Expr::BlockExprClass:
4188   case Expr::ChooseExprClass:
4189   case Expr::CompoundLiteralExprClass:
4190   case Expr::ExtVectorElementExprClass:
4191   case Expr::GenericSelectionExprClass:
4192   case Expr::ObjCEncodeExprClass:
4193   case Expr::ObjCIsaExprClass:
4194   case Expr::ObjCIvarRefExprClass:
4195   case Expr::ObjCMessageExprClass:
4196   case Expr::ObjCPropertyRefExprClass:
4197   case Expr::ObjCProtocolExprClass:
4198   case Expr::ObjCSelectorExprClass:
4199   case Expr::ObjCStringLiteralClass:
4200   case Expr::ObjCBoxedExprClass:
4201   case Expr::ObjCArrayLiteralClass:
4202   case Expr::ObjCDictionaryLiteralClass:
4203   case Expr::ObjCSubscriptRefExprClass:
4204   case Expr::ObjCIndirectCopyRestoreExprClass:
4205   case Expr::ObjCAvailabilityCheckExprClass:
4206   case Expr::OffsetOfExprClass:
4207   case Expr::PredefinedExprClass:
4208   case Expr::ShuffleVectorExprClass:
4209   case Expr::ConvertVectorExprClass:
4210   case Expr::StmtExprClass:
4211   case Expr::TypeTraitExprClass:
4212   case Expr::RequiresExprClass:
4213   case Expr::ArrayTypeTraitExprClass:
4214   case Expr::ExpressionTraitExprClass:
4215   case Expr::VAArgExprClass:
4216   case Expr::CUDAKernelCallExprClass:
4217   case Expr::AsTypeExprClass:
4218   case Expr::PseudoObjectExprClass:
4219   case Expr::AtomicExprClass:
4220   case Expr::SourceLocExprClass:
4221   case Expr::BuiltinBitCastExprClass:
4222   {
4223     NotPrimaryExpr();
4224     if (!NullOut) {
4225       // As bad as this diagnostic is, it's better than crashing.
4226       DiagnosticsEngine &Diags = Context.getDiags();
4227       unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
4228                                        "cannot yet mangle expression type %0");
4229       Diags.Report(E->getExprLoc(), DiagID)
4230         << E->getStmtClassName() << E->getSourceRange();
4231       return;
4232     }
4233     break;
4234   }
4235 
4236   case Expr::CXXUuidofExprClass: {
4237     NotPrimaryExpr();
4238     const CXXUuidofExpr *UE = cast<CXXUuidofExpr>(E);
4239     // As of clang 12, uuidof uses the vendor extended expression
4240     // mangling. Previously, it used a special-cased nonstandard extension.
4241     if (Context.getASTContext().getLangOpts().getClangABICompat() >
4242         LangOptions::ClangABI::Ver11) {
4243       Out << "u8__uuidof";
4244       if (UE->isTypeOperand())
4245         mangleType(UE->getTypeOperand(Context.getASTContext()));
4246       else
4247         mangleTemplateArgExpr(UE->getExprOperand());
4248       Out << 'E';
4249     } else {
4250       if (UE->isTypeOperand()) {
4251         QualType UuidT = UE->getTypeOperand(Context.getASTContext());
4252         Out << "u8__uuidoft";
4253         mangleType(UuidT);
4254       } else {
4255         Expr *UuidExp = UE->getExprOperand();
4256         Out << "u8__uuidofz";
4257         mangleExpression(UuidExp);
4258       }
4259     }
4260     break;
4261   }
4262 
4263   // Even gcc-4.5 doesn't mangle this.
4264   case Expr::BinaryConditionalOperatorClass: {
4265     NotPrimaryExpr();
4266     DiagnosticsEngine &Diags = Context.getDiags();
4267     unsigned DiagID =
4268       Diags.getCustomDiagID(DiagnosticsEngine::Error,
4269                 "?: operator with omitted middle operand cannot be mangled");
4270     Diags.Report(E->getExprLoc(), DiagID)
4271       << E->getStmtClassName() << E->getSourceRange();
4272     return;
4273   }
4274 
4275   // These are used for internal purposes and cannot be meaningfully mangled.
4276   case Expr::OpaqueValueExprClass:
4277     llvm_unreachable("cannot mangle opaque value; mangling wrong thing?");
4278 
4279   case Expr::InitListExprClass: {
4280     NotPrimaryExpr();
4281     Out << "il";
4282     mangleInitListElements(cast<InitListExpr>(E));
4283     Out << "E";
4284     break;
4285   }
4286 
4287   case Expr::DesignatedInitExprClass: {
4288     NotPrimaryExpr();
4289     auto *DIE = cast<DesignatedInitExpr>(E);
4290     for (const auto &Designator : DIE->designators()) {
4291       if (Designator.isFieldDesignator()) {
4292         Out << "di";
4293         mangleSourceName(Designator.getFieldName());
4294       } else if (Designator.isArrayDesignator()) {
4295         Out << "dx";
4296         mangleExpression(DIE->getArrayIndex(Designator));
4297       } else {
4298         assert(Designator.isArrayRangeDesignator() &&
4299                "unknown designator kind");
4300         Out << "dX";
4301         mangleExpression(DIE->getArrayRangeStart(Designator));
4302         mangleExpression(DIE->getArrayRangeEnd(Designator));
4303       }
4304     }
4305     mangleExpression(DIE->getInit());
4306     break;
4307   }
4308 
4309   case Expr::CXXDefaultArgExprClass:
4310     E = cast<CXXDefaultArgExpr>(E)->getExpr();
4311     goto recurse;
4312 
4313   case Expr::CXXDefaultInitExprClass:
4314     E = cast<CXXDefaultInitExpr>(E)->getExpr();
4315     goto recurse;
4316 
4317   case Expr::CXXStdInitializerListExprClass:
4318     E = cast<CXXStdInitializerListExpr>(E)->getSubExpr();
4319     goto recurse;
4320 
4321   case Expr::SubstNonTypeTemplateParmExprClass:
4322     E = cast<SubstNonTypeTemplateParmExpr>(E)->getReplacement();
4323     goto recurse;
4324 
4325   case Expr::UserDefinedLiteralClass:
4326     // We follow g++'s approach of mangling a UDL as a call to the literal
4327     // operator.
4328   case Expr::CXXMemberCallExprClass: // fallthrough
4329   case Expr::CallExprClass: {
4330     NotPrimaryExpr();
4331     const CallExpr *CE = cast<CallExpr>(E);
4332 
4333     // <expression> ::= cp <simple-id> <expression>* E
4334     // We use this mangling only when the call would use ADL except
4335     // for being parenthesized.  Per discussion with David
4336     // Vandervoorde, 2011.04.25.
4337     if (isParenthesizedADLCallee(CE)) {
4338       Out << "cp";
4339       // The callee here is a parenthesized UnresolvedLookupExpr with
4340       // no qualifier and should always get mangled as a <simple-id>
4341       // anyway.
4342 
4343     // <expression> ::= cl <expression>* E
4344     } else {
4345       Out << "cl";
4346     }
4347 
4348     unsigned CallArity = CE->getNumArgs();
4349     for (const Expr *Arg : CE->arguments())
4350       if (isa<PackExpansionExpr>(Arg))
4351         CallArity = UnknownArity;
4352 
4353     mangleExpression(CE->getCallee(), CallArity);
4354     for (const Expr *Arg : CE->arguments())
4355       mangleExpression(Arg);
4356     Out << 'E';
4357     break;
4358   }
4359 
4360   case Expr::CXXNewExprClass: {
4361     NotPrimaryExpr();
4362     const CXXNewExpr *New = cast<CXXNewExpr>(E);
4363     if (New->isGlobalNew()) Out << "gs";
4364     Out << (New->isArray() ? "na" : "nw");
4365     for (CXXNewExpr::const_arg_iterator I = New->placement_arg_begin(),
4366            E = New->placement_arg_end(); I != E; ++I)
4367       mangleExpression(*I);
4368     Out << '_';
4369     mangleType(New->getAllocatedType());
4370     if (New->hasInitializer()) {
4371       if (New->getInitializationStyle() == CXXNewExpr::ListInit)
4372         Out << "il";
4373       else
4374         Out << "pi";
4375       const Expr *Init = New->getInitializer();
4376       if (const CXXConstructExpr *CCE = dyn_cast<CXXConstructExpr>(Init)) {
4377         // Directly inline the initializers.
4378         for (CXXConstructExpr::const_arg_iterator I = CCE->arg_begin(),
4379                                                   E = CCE->arg_end();
4380              I != E; ++I)
4381           mangleExpression(*I);
4382       } else if (const ParenListExpr *PLE = dyn_cast<ParenListExpr>(Init)) {
4383         for (unsigned i = 0, e = PLE->getNumExprs(); i != e; ++i)
4384           mangleExpression(PLE->getExpr(i));
4385       } else if (New->getInitializationStyle() == CXXNewExpr::ListInit &&
4386                  isa<InitListExpr>(Init)) {
4387         // Only take InitListExprs apart for list-initialization.
4388         mangleInitListElements(cast<InitListExpr>(Init));
4389       } else
4390         mangleExpression(Init);
4391     }
4392     Out << 'E';
4393     break;
4394   }
4395 
4396   case Expr::CXXPseudoDestructorExprClass: {
4397     NotPrimaryExpr();
4398     const auto *PDE = cast<CXXPseudoDestructorExpr>(E);
4399     if (const Expr *Base = PDE->getBase())
4400       mangleMemberExprBase(Base, PDE->isArrow());
4401     NestedNameSpecifier *Qualifier = PDE->getQualifier();
4402     if (TypeSourceInfo *ScopeInfo = PDE->getScopeTypeInfo()) {
4403       if (Qualifier) {
4404         mangleUnresolvedPrefix(Qualifier,
4405                                /*recursive=*/true);
4406         mangleUnresolvedTypeOrSimpleId(ScopeInfo->getType());
4407         Out << 'E';
4408       } else {
4409         Out << "sr";
4410         if (!mangleUnresolvedTypeOrSimpleId(ScopeInfo->getType()))
4411           Out << 'E';
4412       }
4413     } else if (Qualifier) {
4414       mangleUnresolvedPrefix(Qualifier);
4415     }
4416     // <base-unresolved-name> ::= dn <destructor-name>
4417     Out << "dn";
4418     QualType DestroyedType = PDE->getDestroyedType();
4419     mangleUnresolvedTypeOrSimpleId(DestroyedType);
4420     break;
4421   }
4422 
4423   case Expr::MemberExprClass: {
4424     NotPrimaryExpr();
4425     const MemberExpr *ME = cast<MemberExpr>(E);
4426     mangleMemberExpr(ME->getBase(), ME->isArrow(),
4427                      ME->getQualifier(), nullptr,
4428                      ME->getMemberDecl()->getDeclName(),
4429                      ME->getTemplateArgs(), ME->getNumTemplateArgs(),
4430                      Arity);
4431     break;
4432   }
4433 
4434   case Expr::UnresolvedMemberExprClass: {
4435     NotPrimaryExpr();
4436     const UnresolvedMemberExpr *ME = cast<UnresolvedMemberExpr>(E);
4437     mangleMemberExpr(ME->isImplicitAccess() ? nullptr : ME->getBase(),
4438                      ME->isArrow(), ME->getQualifier(), nullptr,
4439                      ME->getMemberName(),
4440                      ME->getTemplateArgs(), ME->getNumTemplateArgs(),
4441                      Arity);
4442     break;
4443   }
4444 
4445   case Expr::CXXDependentScopeMemberExprClass: {
4446     NotPrimaryExpr();
4447     const CXXDependentScopeMemberExpr *ME
4448       = cast<CXXDependentScopeMemberExpr>(E);
4449     mangleMemberExpr(ME->isImplicitAccess() ? nullptr : ME->getBase(),
4450                      ME->isArrow(), ME->getQualifier(),
4451                      ME->getFirstQualifierFoundInScope(),
4452                      ME->getMember(),
4453                      ME->getTemplateArgs(), ME->getNumTemplateArgs(),
4454                      Arity);
4455     break;
4456   }
4457 
4458   case Expr::UnresolvedLookupExprClass: {
4459     NotPrimaryExpr();
4460     const UnresolvedLookupExpr *ULE = cast<UnresolvedLookupExpr>(E);
4461     mangleUnresolvedName(ULE->getQualifier(), ULE->getName(),
4462                          ULE->getTemplateArgs(), ULE->getNumTemplateArgs(),
4463                          Arity);
4464     break;
4465   }
4466 
4467   case Expr::CXXUnresolvedConstructExprClass: {
4468     NotPrimaryExpr();
4469     const CXXUnresolvedConstructExpr *CE = cast<CXXUnresolvedConstructExpr>(E);
4470     unsigned N = CE->getNumArgs();
4471 
4472     if (CE->isListInitialization()) {
4473       assert(N == 1 && "unexpected form for list initialization");
4474       auto *IL = cast<InitListExpr>(CE->getArg(0));
4475       Out << "tl";
4476       mangleType(CE->getType());
4477       mangleInitListElements(IL);
4478       Out << "E";
4479       break;
4480     }
4481 
4482     Out << "cv";
4483     mangleType(CE->getType());
4484     if (N != 1) Out << '_';
4485     for (unsigned I = 0; I != N; ++I) mangleExpression(CE->getArg(I));
4486     if (N != 1) Out << 'E';
4487     break;
4488   }
4489 
4490   case Expr::CXXConstructExprClass: {
4491     // An implicit cast is silent, thus may contain <expr-primary>.
4492     const auto *CE = cast<CXXConstructExpr>(E);
4493     if (!CE->isListInitialization() || CE->isStdInitListInitialization()) {
4494       assert(
4495           CE->getNumArgs() >= 1 &&
4496           (CE->getNumArgs() == 1 || isa<CXXDefaultArgExpr>(CE->getArg(1))) &&
4497           "implicit CXXConstructExpr must have one argument");
4498       E = cast<CXXConstructExpr>(E)->getArg(0);
4499       goto recurse;
4500     }
4501     NotPrimaryExpr();
4502     Out << "il";
4503     for (auto *E : CE->arguments())
4504       mangleExpression(E);
4505     Out << "E";
4506     break;
4507   }
4508 
4509   case Expr::CXXTemporaryObjectExprClass: {
4510     NotPrimaryExpr();
4511     const auto *CE = cast<CXXTemporaryObjectExpr>(E);
4512     unsigned N = CE->getNumArgs();
4513     bool List = CE->isListInitialization();
4514 
4515     if (List)
4516       Out << "tl";
4517     else
4518       Out << "cv";
4519     mangleType(CE->getType());
4520     if (!List && N != 1)
4521       Out << '_';
4522     if (CE->isStdInitListInitialization()) {
4523       // We implicitly created a std::initializer_list<T> for the first argument
4524       // of a constructor of type U in an expression of the form U{a, b, c}.
4525       // Strip all the semantic gunk off the initializer list.
4526       auto *SILE =
4527           cast<CXXStdInitializerListExpr>(CE->getArg(0)->IgnoreImplicit());
4528       auto *ILE = cast<InitListExpr>(SILE->getSubExpr()->IgnoreImplicit());
4529       mangleInitListElements(ILE);
4530     } else {
4531       for (auto *E : CE->arguments())
4532         mangleExpression(E);
4533     }
4534     if (List || N != 1)
4535       Out << 'E';
4536     break;
4537   }
4538 
4539   case Expr::CXXScalarValueInitExprClass:
4540     NotPrimaryExpr();
4541     Out << "cv";
4542     mangleType(E->getType());
4543     Out << "_E";
4544     break;
4545 
4546   case Expr::CXXNoexceptExprClass:
4547     NotPrimaryExpr();
4548     Out << "nx";
4549     mangleExpression(cast<CXXNoexceptExpr>(E)->getOperand());
4550     break;
4551 
4552   case Expr::UnaryExprOrTypeTraitExprClass: {
4553     // Non-instantiation-dependent traits are an <expr-primary> integer literal.
4554     const UnaryExprOrTypeTraitExpr *SAE = cast<UnaryExprOrTypeTraitExpr>(E);
4555 
4556     if (!SAE->isInstantiationDependent()) {
4557       // Itanium C++ ABI:
4558       //   If the operand of a sizeof or alignof operator is not
4559       //   instantiation-dependent it is encoded as an integer literal
4560       //   reflecting the result of the operator.
4561       //
4562       //   If the result of the operator is implicitly converted to a known
4563       //   integer type, that type is used for the literal; otherwise, the type
4564       //   of std::size_t or std::ptrdiff_t is used.
4565       QualType T = (ImplicitlyConvertedToType.isNull() ||
4566                     !ImplicitlyConvertedToType->isIntegerType())? SAE->getType()
4567                                                     : ImplicitlyConvertedToType;
4568       llvm::APSInt V = SAE->EvaluateKnownConstInt(Context.getASTContext());
4569       mangleIntegerLiteral(T, V);
4570       break;
4571     }
4572 
4573     NotPrimaryExpr(); // But otherwise, they are not.
4574 
4575     auto MangleAlignofSizeofArg = [&] {
4576       if (SAE->isArgumentType()) {
4577         Out << 't';
4578         mangleType(SAE->getArgumentType());
4579       } else {
4580         Out << 'z';
4581         mangleExpression(SAE->getArgumentExpr());
4582       }
4583     };
4584 
4585     switch(SAE->getKind()) {
4586     case UETT_SizeOf:
4587       Out << 's';
4588       MangleAlignofSizeofArg();
4589       break;
4590     case UETT_PreferredAlignOf:
4591       // As of clang 12, we mangle __alignof__ differently than alignof. (They
4592       // have acted differently since Clang 8, but were previously mangled the
4593       // same.)
4594       if (Context.getASTContext().getLangOpts().getClangABICompat() >
4595           LangOptions::ClangABI::Ver11) {
4596         Out << "u11__alignof__";
4597         if (SAE->isArgumentType())
4598           mangleType(SAE->getArgumentType());
4599         else
4600           mangleTemplateArgExpr(SAE->getArgumentExpr());
4601         Out << 'E';
4602         break;
4603       }
4604       LLVM_FALLTHROUGH;
4605     case UETT_AlignOf:
4606       Out << 'a';
4607       MangleAlignofSizeofArg();
4608       break;
4609     case UETT_VecStep: {
4610       DiagnosticsEngine &Diags = Context.getDiags();
4611       unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
4612                                      "cannot yet mangle vec_step expression");
4613       Diags.Report(DiagID);
4614       return;
4615     }
4616     case UETT_OpenMPRequiredSimdAlign: {
4617       DiagnosticsEngine &Diags = Context.getDiags();
4618       unsigned DiagID = Diags.getCustomDiagID(
4619           DiagnosticsEngine::Error,
4620           "cannot yet mangle __builtin_omp_required_simd_align expression");
4621       Diags.Report(DiagID);
4622       return;
4623     }
4624     }
4625     break;
4626   }
4627 
4628   case Expr::CXXThrowExprClass: {
4629     NotPrimaryExpr();
4630     const CXXThrowExpr *TE = cast<CXXThrowExpr>(E);
4631     //  <expression> ::= tw <expression>  # throw expression
4632     //               ::= tr               # rethrow
4633     if (TE->getSubExpr()) {
4634       Out << "tw";
4635       mangleExpression(TE->getSubExpr());
4636     } else {
4637       Out << "tr";
4638     }
4639     break;
4640   }
4641 
4642   case Expr::CXXTypeidExprClass: {
4643     NotPrimaryExpr();
4644     const CXXTypeidExpr *TIE = cast<CXXTypeidExpr>(E);
4645     //  <expression> ::= ti <type>        # typeid (type)
4646     //               ::= te <expression>  # typeid (expression)
4647     if (TIE->isTypeOperand()) {
4648       Out << "ti";
4649       mangleType(TIE->getTypeOperand(Context.getASTContext()));
4650     } else {
4651       Out << "te";
4652       mangleExpression(TIE->getExprOperand());
4653     }
4654     break;
4655   }
4656 
4657   case Expr::CXXDeleteExprClass: {
4658     NotPrimaryExpr();
4659     const CXXDeleteExpr *DE = cast<CXXDeleteExpr>(E);
4660     //  <expression> ::= [gs] dl <expression>  # [::] delete expr
4661     //               ::= [gs] da <expression>  # [::] delete [] expr
4662     if (DE->isGlobalDelete()) Out << "gs";
4663     Out << (DE->isArrayForm() ? "da" : "dl");
4664     mangleExpression(DE->getArgument());
4665     break;
4666   }
4667 
4668   case Expr::UnaryOperatorClass: {
4669     NotPrimaryExpr();
4670     const UnaryOperator *UO = cast<UnaryOperator>(E);
4671     mangleOperatorName(UnaryOperator::getOverloadedOperator(UO->getOpcode()),
4672                        /*Arity=*/1);
4673     mangleExpression(UO->getSubExpr());
4674     break;
4675   }
4676 
4677   case Expr::ArraySubscriptExprClass: {
4678     NotPrimaryExpr();
4679     const ArraySubscriptExpr *AE = cast<ArraySubscriptExpr>(E);
4680 
4681     // Array subscript is treated as a syntactically weird form of
4682     // binary operator.
4683     Out << "ix";
4684     mangleExpression(AE->getLHS());
4685     mangleExpression(AE->getRHS());
4686     break;
4687   }
4688 
4689   case Expr::MatrixSubscriptExprClass: {
4690     NotPrimaryExpr();
4691     const MatrixSubscriptExpr *ME = cast<MatrixSubscriptExpr>(E);
4692     Out << "ixix";
4693     mangleExpression(ME->getBase());
4694     mangleExpression(ME->getRowIdx());
4695     mangleExpression(ME->getColumnIdx());
4696     break;
4697   }
4698 
4699   case Expr::CompoundAssignOperatorClass: // fallthrough
4700   case Expr::BinaryOperatorClass: {
4701     NotPrimaryExpr();
4702     const BinaryOperator *BO = cast<BinaryOperator>(E);
4703     if (BO->getOpcode() == BO_PtrMemD)
4704       Out << "ds";
4705     else
4706       mangleOperatorName(BinaryOperator::getOverloadedOperator(BO->getOpcode()),
4707                          /*Arity=*/2);
4708     mangleExpression(BO->getLHS());
4709     mangleExpression(BO->getRHS());
4710     break;
4711   }
4712 
4713   case Expr::CXXRewrittenBinaryOperatorClass: {
4714     NotPrimaryExpr();
4715     // The mangled form represents the original syntax.
4716     CXXRewrittenBinaryOperator::DecomposedForm Decomposed =
4717         cast<CXXRewrittenBinaryOperator>(E)->getDecomposedForm();
4718     mangleOperatorName(BinaryOperator::getOverloadedOperator(Decomposed.Opcode),
4719                        /*Arity=*/2);
4720     mangleExpression(Decomposed.LHS);
4721     mangleExpression(Decomposed.RHS);
4722     break;
4723   }
4724 
4725   case Expr::ConditionalOperatorClass: {
4726     NotPrimaryExpr();
4727     const ConditionalOperator *CO = cast<ConditionalOperator>(E);
4728     mangleOperatorName(OO_Conditional, /*Arity=*/3);
4729     mangleExpression(CO->getCond());
4730     mangleExpression(CO->getLHS(), Arity);
4731     mangleExpression(CO->getRHS(), Arity);
4732     break;
4733   }
4734 
4735   case Expr::ImplicitCastExprClass: {
4736     ImplicitlyConvertedToType = E->getType();
4737     E = cast<ImplicitCastExpr>(E)->getSubExpr();
4738     goto recurse;
4739   }
4740 
4741   case Expr::ObjCBridgedCastExprClass: {
4742     NotPrimaryExpr();
4743     // Mangle ownership casts as a vendor extended operator __bridge,
4744     // __bridge_transfer, or __bridge_retain.
4745     StringRef Kind = cast<ObjCBridgedCastExpr>(E)->getBridgeKindName();
4746     Out << "v1U" << Kind.size() << Kind;
4747     mangleCastExpression(E, "cv");
4748     break;
4749   }
4750 
4751   case Expr::CStyleCastExprClass:
4752     NotPrimaryExpr();
4753     mangleCastExpression(E, "cv");
4754     break;
4755 
4756   case Expr::CXXFunctionalCastExprClass: {
4757     NotPrimaryExpr();
4758     auto *Sub = cast<ExplicitCastExpr>(E)->getSubExpr()->IgnoreImplicit();
4759     // FIXME: Add isImplicit to CXXConstructExpr.
4760     if (auto *CCE = dyn_cast<CXXConstructExpr>(Sub))
4761       if (CCE->getParenOrBraceRange().isInvalid())
4762         Sub = CCE->getArg(0)->IgnoreImplicit();
4763     if (auto *StdInitList = dyn_cast<CXXStdInitializerListExpr>(Sub))
4764       Sub = StdInitList->getSubExpr()->IgnoreImplicit();
4765     if (auto *IL = dyn_cast<InitListExpr>(Sub)) {
4766       Out << "tl";
4767       mangleType(E->getType());
4768       mangleInitListElements(IL);
4769       Out << "E";
4770     } else {
4771       mangleCastExpression(E, "cv");
4772     }
4773     break;
4774   }
4775 
4776   case Expr::CXXStaticCastExprClass:
4777     NotPrimaryExpr();
4778     mangleCastExpression(E, "sc");
4779     break;
4780   case Expr::CXXDynamicCastExprClass:
4781     NotPrimaryExpr();
4782     mangleCastExpression(E, "dc");
4783     break;
4784   case Expr::CXXReinterpretCastExprClass:
4785     NotPrimaryExpr();
4786     mangleCastExpression(E, "rc");
4787     break;
4788   case Expr::CXXConstCastExprClass:
4789     NotPrimaryExpr();
4790     mangleCastExpression(E, "cc");
4791     break;
4792   case Expr::CXXAddrspaceCastExprClass:
4793     NotPrimaryExpr();
4794     mangleCastExpression(E, "ac");
4795     break;
4796 
4797   case Expr::CXXOperatorCallExprClass: {
4798     NotPrimaryExpr();
4799     const CXXOperatorCallExpr *CE = cast<CXXOperatorCallExpr>(E);
4800     unsigned NumArgs = CE->getNumArgs();
4801     // A CXXOperatorCallExpr for OO_Arrow models only semantics, not syntax
4802     // (the enclosing MemberExpr covers the syntactic portion).
4803     if (CE->getOperator() != OO_Arrow)
4804       mangleOperatorName(CE->getOperator(), /*Arity=*/NumArgs);
4805     // Mangle the arguments.
4806     for (unsigned i = 0; i != NumArgs; ++i)
4807       mangleExpression(CE->getArg(i));
4808     break;
4809   }
4810 
4811   case Expr::ParenExprClass:
4812     E = cast<ParenExpr>(E)->getSubExpr();
4813     goto recurse;
4814 
4815   case Expr::ConceptSpecializationExprClass: {
4816     //  <expr-primary> ::= L <mangled-name> E # external name
4817     Out << "L_Z";
4818     auto *CSE = cast<ConceptSpecializationExpr>(E);
4819     mangleTemplateName(CSE->getNamedConcept(),
4820                        CSE->getTemplateArguments().data(),
4821                        CSE->getTemplateArguments().size());
4822     Out << 'E';
4823     break;
4824   }
4825 
4826   case Expr::DeclRefExprClass:
4827     // MangleDeclRefExpr helper handles primary-vs-nonprimary
4828     MangleDeclRefExpr(cast<DeclRefExpr>(E)->getDecl());
4829     break;
4830 
4831   case Expr::SubstNonTypeTemplateParmPackExprClass:
4832     NotPrimaryExpr();
4833     // FIXME: not clear how to mangle this!
4834     // template <unsigned N...> class A {
4835     //   template <class U...> void foo(U (&x)[N]...);
4836     // };
4837     Out << "_SUBSTPACK_";
4838     break;
4839 
4840   case Expr::FunctionParmPackExprClass: {
4841     NotPrimaryExpr();
4842     // FIXME: not clear how to mangle this!
4843     const FunctionParmPackExpr *FPPE = cast<FunctionParmPackExpr>(E);
4844     Out << "v110_SUBSTPACK";
4845     MangleDeclRefExpr(FPPE->getParameterPack());
4846     break;
4847   }
4848 
4849   case Expr::DependentScopeDeclRefExprClass: {
4850     NotPrimaryExpr();
4851     const DependentScopeDeclRefExpr *DRE = cast<DependentScopeDeclRefExpr>(E);
4852     mangleUnresolvedName(DRE->getQualifier(), DRE->getDeclName(),
4853                          DRE->getTemplateArgs(), DRE->getNumTemplateArgs(),
4854                          Arity);
4855     break;
4856   }
4857 
4858   case Expr::CXXBindTemporaryExprClass:
4859     E = cast<CXXBindTemporaryExpr>(E)->getSubExpr();
4860     goto recurse;
4861 
4862   case Expr::ExprWithCleanupsClass:
4863     E = cast<ExprWithCleanups>(E)->getSubExpr();
4864     goto recurse;
4865 
4866   case Expr::FloatingLiteralClass: {
4867     // <expr-primary>
4868     const FloatingLiteral *FL = cast<FloatingLiteral>(E);
4869     mangleFloatLiteral(FL->getType(), FL->getValue());
4870     break;
4871   }
4872 
4873   case Expr::FixedPointLiteralClass:
4874     // Currently unimplemented -- might be <expr-primary> in future?
4875     mangleFixedPointLiteral();
4876     break;
4877 
4878   case Expr::CharacterLiteralClass:
4879     // <expr-primary>
4880     Out << 'L';
4881     mangleType(E->getType());
4882     Out << cast<CharacterLiteral>(E)->getValue();
4883     Out << 'E';
4884     break;
4885 
4886   // FIXME. __objc_yes/__objc_no are mangled same as true/false
4887   case Expr::ObjCBoolLiteralExprClass:
4888     // <expr-primary>
4889     Out << "Lb";
4890     Out << (cast<ObjCBoolLiteralExpr>(E)->getValue() ? '1' : '0');
4891     Out << 'E';
4892     break;
4893 
4894   case Expr::CXXBoolLiteralExprClass:
4895     // <expr-primary>
4896     Out << "Lb";
4897     Out << (cast<CXXBoolLiteralExpr>(E)->getValue() ? '1' : '0');
4898     Out << 'E';
4899     break;
4900 
4901   case Expr::IntegerLiteralClass: {
4902     // <expr-primary>
4903     llvm::APSInt Value(cast<IntegerLiteral>(E)->getValue());
4904     if (E->getType()->isSignedIntegerType())
4905       Value.setIsSigned(true);
4906     mangleIntegerLiteral(E->getType(), Value);
4907     break;
4908   }
4909 
4910   case Expr::ImaginaryLiteralClass: {
4911     // <expr-primary>
4912     const ImaginaryLiteral *IE = cast<ImaginaryLiteral>(E);
4913     // Mangle as if a complex literal.
4914     // Proposal from David Vandevoorde, 2010.06.30.
4915     Out << 'L';
4916     mangleType(E->getType());
4917     if (const FloatingLiteral *Imag =
4918           dyn_cast<FloatingLiteral>(IE->getSubExpr())) {
4919       // Mangle a floating-point zero of the appropriate type.
4920       mangleFloat(llvm::APFloat(Imag->getValue().getSemantics()));
4921       Out << '_';
4922       mangleFloat(Imag->getValue());
4923     } else {
4924       Out << "0_";
4925       llvm::APSInt Value(cast<IntegerLiteral>(IE->getSubExpr())->getValue());
4926       if (IE->getSubExpr()->getType()->isSignedIntegerType())
4927         Value.setIsSigned(true);
4928       mangleNumber(Value);
4929     }
4930     Out << 'E';
4931     break;
4932   }
4933 
4934   case Expr::StringLiteralClass: {
4935     // <expr-primary>
4936     // Revised proposal from David Vandervoorde, 2010.07.15.
4937     Out << 'L';
4938     assert(isa<ConstantArrayType>(E->getType()));
4939     mangleType(E->getType());
4940     Out << 'E';
4941     break;
4942   }
4943 
4944   case Expr::GNUNullExprClass:
4945     // <expr-primary>
4946     // Mangle as if an integer literal 0.
4947     mangleIntegerLiteral(E->getType(), llvm::APSInt(32));
4948     break;
4949 
4950   case Expr::CXXNullPtrLiteralExprClass: {
4951     // <expr-primary>
4952     Out << "LDnE";
4953     break;
4954   }
4955 
4956   case Expr::PackExpansionExprClass:
4957     NotPrimaryExpr();
4958     Out << "sp";
4959     mangleExpression(cast<PackExpansionExpr>(E)->getPattern());
4960     break;
4961 
4962   case Expr::SizeOfPackExprClass: {
4963     NotPrimaryExpr();
4964     auto *SPE = cast<SizeOfPackExpr>(E);
4965     if (SPE->isPartiallySubstituted()) {
4966       Out << "sP";
4967       for (const auto &A : SPE->getPartialArguments())
4968         mangleTemplateArg(A, false);
4969       Out << "E";
4970       break;
4971     }
4972 
4973     Out << "sZ";
4974     const NamedDecl *Pack = SPE->getPack();
4975     if (const TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(Pack))
4976       mangleTemplateParameter(TTP->getDepth(), TTP->getIndex());
4977     else if (const NonTypeTemplateParmDecl *NTTP
4978                 = dyn_cast<NonTypeTemplateParmDecl>(Pack))
4979       mangleTemplateParameter(NTTP->getDepth(), NTTP->getIndex());
4980     else if (const TemplateTemplateParmDecl *TempTP
4981                                     = dyn_cast<TemplateTemplateParmDecl>(Pack))
4982       mangleTemplateParameter(TempTP->getDepth(), TempTP->getIndex());
4983     else
4984       mangleFunctionParam(cast<ParmVarDecl>(Pack));
4985     break;
4986   }
4987 
4988   case Expr::MaterializeTemporaryExprClass:
4989     E = cast<MaterializeTemporaryExpr>(E)->getSubExpr();
4990     goto recurse;
4991 
4992   case Expr::CXXFoldExprClass: {
4993     NotPrimaryExpr();
4994     auto *FE = cast<CXXFoldExpr>(E);
4995     if (FE->isLeftFold())
4996       Out << (FE->getInit() ? "fL" : "fl");
4997     else
4998       Out << (FE->getInit() ? "fR" : "fr");
4999 
5000     if (FE->getOperator() == BO_PtrMemD)
5001       Out << "ds";
5002     else
5003       mangleOperatorName(
5004           BinaryOperator::getOverloadedOperator(FE->getOperator()),
5005           /*Arity=*/2);
5006 
5007     if (FE->getLHS())
5008       mangleExpression(FE->getLHS());
5009     if (FE->getRHS())
5010       mangleExpression(FE->getRHS());
5011     break;
5012   }
5013 
5014   case Expr::CXXThisExprClass:
5015     NotPrimaryExpr();
5016     Out << "fpT";
5017     break;
5018 
5019   case Expr::CoawaitExprClass:
5020     // FIXME: Propose a non-vendor mangling.
5021     NotPrimaryExpr();
5022     Out << "v18co_await";
5023     mangleExpression(cast<CoawaitExpr>(E)->getOperand());
5024     break;
5025 
5026   case Expr::DependentCoawaitExprClass:
5027     // FIXME: Propose a non-vendor mangling.
5028     NotPrimaryExpr();
5029     Out << "v18co_await";
5030     mangleExpression(cast<DependentCoawaitExpr>(E)->getOperand());
5031     break;
5032 
5033   case Expr::CoyieldExprClass:
5034     // FIXME: Propose a non-vendor mangling.
5035     NotPrimaryExpr();
5036     Out << "v18co_yield";
5037     mangleExpression(cast<CoawaitExpr>(E)->getOperand());
5038     break;
5039   case Expr::SYCLUniqueStableNameExprClass: {
5040     const auto *USN = cast<SYCLUniqueStableNameExpr>(E);
5041     NotPrimaryExpr();
5042 
5043     Out << "u33__builtin_sycl_unique_stable_name";
5044     mangleType(USN->getTypeSourceInfo()->getType());
5045 
5046     Out << "E";
5047     break;
5048   }
5049   }
5050 
5051   if (AsTemplateArg && !IsPrimaryExpr)
5052     Out << 'E';
5053 }
5054 
5055 /// Mangle an expression which refers to a parameter variable.
5056 ///
5057 /// <expression>     ::= <function-param>
5058 /// <function-param> ::= fp <top-level CV-qualifiers> _      # L == 0, I == 0
5059 /// <function-param> ::= fp <top-level CV-qualifiers>
5060 ///                      <parameter-2 non-negative number> _ # L == 0, I > 0
5061 /// <function-param> ::= fL <L-1 non-negative number>
5062 ///                      p <top-level CV-qualifiers> _       # L > 0, I == 0
5063 /// <function-param> ::= fL <L-1 non-negative number>
5064 ///                      p <top-level CV-qualifiers>
5065 ///                      <I-1 non-negative number> _         # L > 0, I > 0
5066 ///
5067 /// L is the nesting depth of the parameter, defined as 1 if the
5068 /// parameter comes from the innermost function prototype scope
5069 /// enclosing the current context, 2 if from the next enclosing
5070 /// function prototype scope, and so on, with one special case: if
5071 /// we've processed the full parameter clause for the innermost
5072 /// function type, then L is one less.  This definition conveniently
5073 /// makes it irrelevant whether a function's result type was written
5074 /// trailing or leading, but is otherwise overly complicated; the
5075 /// numbering was first designed without considering references to
5076 /// parameter in locations other than return types, and then the
5077 /// mangling had to be generalized without changing the existing
5078 /// manglings.
5079 ///
5080 /// I is the zero-based index of the parameter within its parameter
5081 /// declaration clause.  Note that the original ABI document describes
5082 /// this using 1-based ordinals.
mangleFunctionParam(const ParmVarDecl * parm)5083 void CXXNameMangler::mangleFunctionParam(const ParmVarDecl *parm) {
5084   unsigned parmDepth = parm->getFunctionScopeDepth();
5085   unsigned parmIndex = parm->getFunctionScopeIndex();
5086 
5087   // Compute 'L'.
5088   // parmDepth does not include the declaring function prototype.
5089   // FunctionTypeDepth does account for that.
5090   assert(parmDepth < FunctionTypeDepth.getDepth());
5091   unsigned nestingDepth = FunctionTypeDepth.getDepth() - parmDepth;
5092   if (FunctionTypeDepth.isInResultType())
5093     nestingDepth--;
5094 
5095   if (nestingDepth == 0) {
5096     Out << "fp";
5097   } else {
5098     Out << "fL" << (nestingDepth - 1) << 'p';
5099   }
5100 
5101   // Top-level qualifiers.  We don't have to worry about arrays here,
5102   // because parameters declared as arrays should already have been
5103   // transformed to have pointer type. FIXME: apparently these don't
5104   // get mangled if used as an rvalue of a known non-class type?
5105   assert(!parm->getType()->isArrayType()
5106          && "parameter's type is still an array type?");
5107 
5108   if (const DependentAddressSpaceType *DAST =
5109       dyn_cast<DependentAddressSpaceType>(parm->getType())) {
5110     mangleQualifiers(DAST->getPointeeType().getQualifiers(), DAST);
5111   } else {
5112     mangleQualifiers(parm->getType().getQualifiers());
5113   }
5114 
5115   // Parameter index.
5116   if (parmIndex != 0) {
5117     Out << (parmIndex - 1);
5118   }
5119   Out << '_';
5120 }
5121 
mangleCXXCtorType(CXXCtorType T,const CXXRecordDecl * InheritedFrom)5122 void CXXNameMangler::mangleCXXCtorType(CXXCtorType T,
5123                                        const CXXRecordDecl *InheritedFrom) {
5124   // <ctor-dtor-name> ::= C1  # complete object constructor
5125   //                  ::= C2  # base object constructor
5126   //                  ::= CI1 <type> # complete inheriting constructor
5127   //                  ::= CI2 <type> # base inheriting constructor
5128   //
5129   // In addition, C5 is a comdat name with C1 and C2 in it.
5130   Out << 'C';
5131   if (InheritedFrom)
5132     Out << 'I';
5133   switch (T) {
5134   case Ctor_Complete:
5135     Out << '1';
5136     break;
5137   case Ctor_Base:
5138     Out << '2';
5139     break;
5140   case Ctor_Comdat:
5141     Out << '5';
5142     break;
5143   case Ctor_DefaultClosure:
5144   case Ctor_CopyingClosure:
5145     llvm_unreachable("closure constructors don't exist for the Itanium ABI!");
5146   }
5147   if (InheritedFrom)
5148     mangleName(InheritedFrom);
5149 }
5150 
mangleCXXDtorType(CXXDtorType T)5151 void CXXNameMangler::mangleCXXDtorType(CXXDtorType T) {
5152   // <ctor-dtor-name> ::= D0  # deleting destructor
5153   //                  ::= D1  # complete object destructor
5154   //                  ::= D2  # base object destructor
5155   //
5156   // In addition, D5 is a comdat name with D1, D2 and, if virtual, D0 in it.
5157   switch (T) {
5158   case Dtor_Deleting:
5159     Out << "D0";
5160     break;
5161   case Dtor_Complete:
5162     Out << "D1";
5163     break;
5164   case Dtor_Base:
5165     Out << "D2";
5166     break;
5167   case Dtor_Comdat:
5168     Out << "D5";
5169     break;
5170   }
5171 }
5172 
5173 namespace {
5174 // Helper to provide ancillary information on a template used to mangle its
5175 // arguments.
5176 struct TemplateArgManglingInfo {
5177   TemplateDecl *ResolvedTemplate = nullptr;
5178   bool SeenPackExpansionIntoNonPack = false;
5179   const NamedDecl *UnresolvedExpandedPack = nullptr;
5180 
TemplateArgManglingInfo__anon1a962c820611::TemplateArgManglingInfo5181   TemplateArgManglingInfo(TemplateName TN) {
5182     if (TemplateDecl *TD = TN.getAsTemplateDecl())
5183       ResolvedTemplate = TD;
5184   }
5185 
5186   /// Do we need to mangle template arguments with exactly correct types?
5187   ///
5188   /// This should be called exactly once for each parameter / argument pair, in
5189   /// order.
needExactType__anon1a962c820611::TemplateArgManglingInfo5190   bool needExactType(unsigned ParamIdx, const TemplateArgument &Arg) {
5191     // We need correct types when the template-name is unresolved or when it
5192     // names a template that is able to be overloaded.
5193     if (!ResolvedTemplate || SeenPackExpansionIntoNonPack)
5194       return true;
5195 
5196     // Move to the next parameter.
5197     const NamedDecl *Param = UnresolvedExpandedPack;
5198     if (!Param) {
5199       assert(ParamIdx < ResolvedTemplate->getTemplateParameters()->size() &&
5200              "no parameter for argument");
5201       Param = ResolvedTemplate->getTemplateParameters()->getParam(ParamIdx);
5202 
5203       // If we reach an expanded parameter pack whose argument isn't in pack
5204       // form, that means Sema couldn't figure out which arguments belonged to
5205       // it, because it contains a pack expansion. Track the expanded pack for
5206       // all further template arguments until we hit that pack expansion.
5207       if (Param->isParameterPack() && Arg.getKind() != TemplateArgument::Pack) {
5208         assert(getExpandedPackSize(Param) &&
5209                "failed to form pack argument for parameter pack");
5210         UnresolvedExpandedPack = Param;
5211       }
5212     }
5213 
5214     // If we encounter a pack argument that is expanded into a non-pack
5215     // parameter, we can no longer track parameter / argument correspondence,
5216     // and need to use exact types from this point onwards.
5217     if (Arg.isPackExpansion() &&
5218         (!Param->isParameterPack() || UnresolvedExpandedPack)) {
5219       SeenPackExpansionIntoNonPack = true;
5220       return true;
5221     }
5222 
5223     // We need exact types for function template arguments because they might be
5224     // overloaded on template parameter type. As a special case, a member
5225     // function template of a generic lambda is not overloadable.
5226     if (auto *FTD = dyn_cast<FunctionTemplateDecl>(ResolvedTemplate)) {
5227       auto *RD = dyn_cast<CXXRecordDecl>(FTD->getDeclContext());
5228       if (!RD || !RD->isGenericLambda())
5229         return true;
5230     }
5231 
5232     // Otherwise, we only need a correct type if the parameter has a deduced
5233     // type.
5234     //
5235     // Note: for an expanded parameter pack, getType() returns the type prior
5236     // to expansion. We could ask for the expanded type with getExpansionType(),
5237     // but it doesn't matter because substitution and expansion don't affect
5238     // whether a deduced type appears in the type.
5239     auto *NTTP = dyn_cast<NonTypeTemplateParmDecl>(Param);
5240     return NTTP && NTTP->getType()->getContainedDeducedType();
5241   }
5242 };
5243 }
5244 
mangleTemplateArgs(TemplateName TN,const TemplateArgumentLoc * TemplateArgs,unsigned NumTemplateArgs)5245 void CXXNameMangler::mangleTemplateArgs(TemplateName TN,
5246                                         const TemplateArgumentLoc *TemplateArgs,
5247                                         unsigned NumTemplateArgs) {
5248   // <template-args> ::= I <template-arg>+ E
5249   Out << 'I';
5250   TemplateArgManglingInfo Info(TN);
5251   for (unsigned i = 0; i != NumTemplateArgs; ++i)
5252     mangleTemplateArg(TemplateArgs[i].getArgument(),
5253                       Info.needExactType(i, TemplateArgs[i].getArgument()));
5254   Out << 'E';
5255 }
5256 
mangleTemplateArgs(TemplateName TN,const TemplateArgumentList & AL)5257 void CXXNameMangler::mangleTemplateArgs(TemplateName TN,
5258                                         const TemplateArgumentList &AL) {
5259   // <template-args> ::= I <template-arg>+ E
5260   Out << 'I';
5261   TemplateArgManglingInfo Info(TN);
5262   for (unsigned i = 0, e = AL.size(); i != e; ++i)
5263     mangleTemplateArg(AL[i], Info.needExactType(i, AL[i]));
5264   Out << 'E';
5265 }
5266 
mangleTemplateArgs(TemplateName TN,const TemplateArgument * TemplateArgs,unsigned NumTemplateArgs)5267 void CXXNameMangler::mangleTemplateArgs(TemplateName TN,
5268                                         const TemplateArgument *TemplateArgs,
5269                                         unsigned NumTemplateArgs) {
5270   // <template-args> ::= I <template-arg>+ E
5271   Out << 'I';
5272   TemplateArgManglingInfo Info(TN);
5273   for (unsigned i = 0; i != NumTemplateArgs; ++i)
5274     mangleTemplateArg(TemplateArgs[i], Info.needExactType(i, TemplateArgs[i]));
5275   Out << 'E';
5276 }
5277 
mangleTemplateArg(TemplateArgument A,bool NeedExactType)5278 void CXXNameMangler::mangleTemplateArg(TemplateArgument A, bool NeedExactType) {
5279   // <template-arg> ::= <type>              # type or template
5280   //                ::= X <expression> E    # expression
5281   //                ::= <expr-primary>      # simple expressions
5282   //                ::= J <template-arg>* E # argument pack
5283   if (!A.isInstantiationDependent() || A.isDependent())
5284     A = Context.getASTContext().getCanonicalTemplateArgument(A);
5285 
5286   switch (A.getKind()) {
5287   case TemplateArgument::Null:
5288     llvm_unreachable("Cannot mangle NULL template argument");
5289 
5290   case TemplateArgument::Type:
5291     mangleType(A.getAsType());
5292     break;
5293   case TemplateArgument::Template:
5294     // This is mangled as <type>.
5295     mangleType(A.getAsTemplate());
5296     break;
5297   case TemplateArgument::TemplateExpansion:
5298     // <type>  ::= Dp <type>          # pack expansion (C++0x)
5299     Out << "Dp";
5300     mangleType(A.getAsTemplateOrTemplatePattern());
5301     break;
5302   case TemplateArgument::Expression:
5303     mangleTemplateArgExpr(A.getAsExpr());
5304     break;
5305   case TemplateArgument::Integral:
5306     mangleIntegerLiteral(A.getIntegralType(), A.getAsIntegral());
5307     break;
5308   case TemplateArgument::Declaration: {
5309     //  <expr-primary> ::= L <mangled-name> E # external name
5310     ValueDecl *D = A.getAsDecl();
5311 
5312     // Template parameter objects are modeled by reproducing a source form
5313     // produced as if by aggregate initialization.
5314     if (A.getParamTypeForDecl()->isRecordType()) {
5315       auto *TPO = cast<TemplateParamObjectDecl>(D);
5316       mangleValueInTemplateArg(TPO->getType().getUnqualifiedType(),
5317                                TPO->getValue(), /*TopLevel=*/true,
5318                                NeedExactType);
5319       break;
5320     }
5321 
5322     ASTContext &Ctx = Context.getASTContext();
5323     APValue Value;
5324     if (D->isCXXInstanceMember())
5325       // Simple pointer-to-member with no conversion.
5326       Value = APValue(D, /*IsDerivedMember=*/false, /*Path=*/{});
5327     else if (D->getType()->isArrayType() &&
5328              Ctx.hasSimilarType(Ctx.getDecayedType(D->getType()),
5329                                 A.getParamTypeForDecl()) &&
5330              Ctx.getLangOpts().getClangABICompat() >
5331                  LangOptions::ClangABI::Ver11)
5332       // Build a value corresponding to this implicit array-to-pointer decay.
5333       Value = APValue(APValue::LValueBase(D), CharUnits::Zero(),
5334                       {APValue::LValuePathEntry::ArrayIndex(0)},
5335                       /*OnePastTheEnd=*/false);
5336     else
5337       // Regular pointer or reference to a declaration.
5338       Value = APValue(APValue::LValueBase(D), CharUnits::Zero(),
5339                       ArrayRef<APValue::LValuePathEntry>(),
5340                       /*OnePastTheEnd=*/false);
5341     mangleValueInTemplateArg(A.getParamTypeForDecl(), Value, /*TopLevel=*/true,
5342                              NeedExactType);
5343     break;
5344   }
5345   case TemplateArgument::NullPtr: {
5346     mangleNullPointer(A.getNullPtrType());
5347     break;
5348   }
5349   case TemplateArgument::Pack: {
5350     //  <template-arg> ::= J <template-arg>* E
5351     Out << 'J';
5352     for (const auto &P : A.pack_elements())
5353       mangleTemplateArg(P, NeedExactType);
5354     Out << 'E';
5355   }
5356   }
5357 }
5358 
mangleTemplateArgExpr(const Expr * E)5359 void CXXNameMangler::mangleTemplateArgExpr(const Expr *E) {
5360   ASTContext &Ctx = Context.getASTContext();
5361   if (Ctx.getLangOpts().getClangABICompat() > LangOptions::ClangABI::Ver11) {
5362     mangleExpression(E, UnknownArity, /*AsTemplateArg=*/true);
5363     return;
5364   }
5365 
5366   // Prior to Clang 12, we didn't omit the X .. E around <expr-primary>
5367   // correctly in cases where the template argument was
5368   // constructed from an expression rather than an already-evaluated
5369   // literal. In such a case, we would then e.g. emit 'XLi0EE' instead of
5370   // 'Li0E'.
5371   //
5372   // We did special-case DeclRefExpr to attempt to DTRT for that one
5373   // expression-kind, but while doing so, unfortunately handled ParmVarDecl
5374   // (subtype of VarDecl) _incorrectly_, and emitted 'L_Z .. E' instead of
5375   // the proper 'Xfp_E'.
5376   E = E->IgnoreParenImpCasts();
5377   if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E)) {
5378     const ValueDecl *D = DRE->getDecl();
5379     if (isa<VarDecl>(D) || isa<FunctionDecl>(D)) {
5380       Out << 'L';
5381       mangle(D);
5382       Out << 'E';
5383       return;
5384     }
5385   }
5386   Out << 'X';
5387   mangleExpression(E);
5388   Out << 'E';
5389 }
5390 
5391 /// Determine whether a given value is equivalent to zero-initialization for
5392 /// the purpose of discarding a trailing portion of a 'tl' mangling.
5393 ///
5394 /// Note that this is not in general equivalent to determining whether the
5395 /// value has an all-zeroes bit pattern.
isZeroInitialized(QualType T,const APValue & V)5396 static bool isZeroInitialized(QualType T, const APValue &V) {
5397   // FIXME: mangleValueInTemplateArg has quadratic time complexity in
5398   // pathological cases due to using this, but it's a little awkward
5399   // to do this in linear time in general.
5400   switch (V.getKind()) {
5401   case APValue::None:
5402   case APValue::Indeterminate:
5403   case APValue::AddrLabelDiff:
5404     return false;
5405 
5406   case APValue::Struct: {
5407     const CXXRecordDecl *RD = T->getAsCXXRecordDecl();
5408     assert(RD && "unexpected type for record value");
5409     unsigned I = 0;
5410     for (const CXXBaseSpecifier &BS : RD->bases()) {
5411       if (!isZeroInitialized(BS.getType(), V.getStructBase(I)))
5412         return false;
5413       ++I;
5414     }
5415     I = 0;
5416     for (const FieldDecl *FD : RD->fields()) {
5417       if (!FD->isUnnamedBitfield() &&
5418           !isZeroInitialized(FD->getType(), V.getStructField(I)))
5419         return false;
5420       ++I;
5421     }
5422     return true;
5423   }
5424 
5425   case APValue::Union: {
5426     const CXXRecordDecl *RD = T->getAsCXXRecordDecl();
5427     assert(RD && "unexpected type for union value");
5428     // Zero-initialization zeroes the first non-unnamed-bitfield field, if any.
5429     for (const FieldDecl *FD : RD->fields()) {
5430       if (!FD->isUnnamedBitfield())
5431         return V.getUnionField() && declaresSameEntity(FD, V.getUnionField()) &&
5432                isZeroInitialized(FD->getType(), V.getUnionValue());
5433     }
5434     // If there are no fields (other than unnamed bitfields), the value is
5435     // necessarily zero-initialized.
5436     return true;
5437   }
5438 
5439   case APValue::Array: {
5440     QualType ElemT(T->getArrayElementTypeNoTypeQual(), 0);
5441     for (unsigned I = 0, N = V.getArrayInitializedElts(); I != N; ++I)
5442       if (!isZeroInitialized(ElemT, V.getArrayInitializedElt(I)))
5443         return false;
5444     return !V.hasArrayFiller() || isZeroInitialized(ElemT, V.getArrayFiller());
5445   }
5446 
5447   case APValue::Vector: {
5448     const VectorType *VT = T->castAs<VectorType>();
5449     for (unsigned I = 0, N = V.getVectorLength(); I != N; ++I)
5450       if (!isZeroInitialized(VT->getElementType(), V.getVectorElt(I)))
5451         return false;
5452     return true;
5453   }
5454 
5455   case APValue::Int:
5456     return !V.getInt();
5457 
5458   case APValue::Float:
5459     return V.getFloat().isPosZero();
5460 
5461   case APValue::FixedPoint:
5462     return !V.getFixedPoint().getValue();
5463 
5464   case APValue::ComplexFloat:
5465     return V.getComplexFloatReal().isPosZero() &&
5466            V.getComplexFloatImag().isPosZero();
5467 
5468   case APValue::ComplexInt:
5469     return !V.getComplexIntReal() && !V.getComplexIntImag();
5470 
5471   case APValue::LValue:
5472     return V.isNullPointer();
5473 
5474   case APValue::MemberPointer:
5475     return !V.getMemberPointerDecl();
5476   }
5477 
5478   llvm_unreachable("Unhandled APValue::ValueKind enum");
5479 }
5480 
getLValueType(ASTContext & Ctx,const APValue & LV)5481 static QualType getLValueType(ASTContext &Ctx, const APValue &LV) {
5482   QualType T = LV.getLValueBase().getType();
5483   for (APValue::LValuePathEntry E : LV.getLValuePath()) {
5484     if (const ArrayType *AT = Ctx.getAsArrayType(T))
5485       T = AT->getElementType();
5486     else if (const FieldDecl *FD =
5487                  dyn_cast<FieldDecl>(E.getAsBaseOrMember().getPointer()))
5488       T = FD->getType();
5489     else
5490       T = Ctx.getRecordType(
5491           cast<CXXRecordDecl>(E.getAsBaseOrMember().getPointer()));
5492   }
5493   return T;
5494 }
5495 
mangleValueInTemplateArg(QualType T,const APValue & V,bool TopLevel,bool NeedExactType)5496 void CXXNameMangler::mangleValueInTemplateArg(QualType T, const APValue &V,
5497                                               bool TopLevel,
5498                                               bool NeedExactType) {
5499   // Ignore all top-level cv-qualifiers, to match GCC.
5500   Qualifiers Quals;
5501   T = getASTContext().getUnqualifiedArrayType(T, Quals);
5502 
5503   // A top-level expression that's not a primary expression is wrapped in X...E.
5504   bool IsPrimaryExpr = true;
5505   auto NotPrimaryExpr = [&] {
5506     if (TopLevel && IsPrimaryExpr)
5507       Out << 'X';
5508     IsPrimaryExpr = false;
5509   };
5510 
5511   // Proposed in https://github.com/itanium-cxx-abi/cxx-abi/issues/63.
5512   switch (V.getKind()) {
5513   case APValue::None:
5514   case APValue::Indeterminate:
5515     Out << 'L';
5516     mangleType(T);
5517     Out << 'E';
5518     break;
5519 
5520   case APValue::AddrLabelDiff:
5521     llvm_unreachable("unexpected value kind in template argument");
5522 
5523   case APValue::Struct: {
5524     const CXXRecordDecl *RD = T->getAsCXXRecordDecl();
5525     assert(RD && "unexpected type for record value");
5526 
5527     // Drop trailing zero-initialized elements.
5528     llvm::SmallVector<const FieldDecl *, 16> Fields(RD->field_begin(),
5529                                                     RD->field_end());
5530     while (
5531         !Fields.empty() &&
5532         (Fields.back()->isUnnamedBitfield() ||
5533          isZeroInitialized(Fields.back()->getType(),
5534                            V.getStructField(Fields.back()->getFieldIndex())))) {
5535       Fields.pop_back();
5536     }
5537     llvm::ArrayRef<CXXBaseSpecifier> Bases(RD->bases_begin(), RD->bases_end());
5538     if (Fields.empty()) {
5539       while (!Bases.empty() &&
5540              isZeroInitialized(Bases.back().getType(),
5541                                V.getStructBase(Bases.size() - 1)))
5542         Bases = Bases.drop_back();
5543     }
5544 
5545     // <expression> ::= tl <type> <braced-expression>* E
5546     NotPrimaryExpr();
5547     Out << "tl";
5548     mangleType(T);
5549     for (unsigned I = 0, N = Bases.size(); I != N; ++I)
5550       mangleValueInTemplateArg(Bases[I].getType(), V.getStructBase(I), false);
5551     for (unsigned I = 0, N = Fields.size(); I != N; ++I) {
5552       if (Fields[I]->isUnnamedBitfield())
5553         continue;
5554       mangleValueInTemplateArg(Fields[I]->getType(),
5555                                V.getStructField(Fields[I]->getFieldIndex()),
5556                                false);
5557     }
5558     Out << 'E';
5559     break;
5560   }
5561 
5562   case APValue::Union: {
5563     assert(T->getAsCXXRecordDecl() && "unexpected type for union value");
5564     const FieldDecl *FD = V.getUnionField();
5565 
5566     if (!FD) {
5567       Out << 'L';
5568       mangleType(T);
5569       Out << 'E';
5570       break;
5571     }
5572 
5573     // <braced-expression> ::= di <field source-name> <braced-expression>
5574     NotPrimaryExpr();
5575     Out << "tl";
5576     mangleType(T);
5577     if (!isZeroInitialized(T, V)) {
5578       Out << "di";
5579       mangleSourceName(FD->getIdentifier());
5580       mangleValueInTemplateArg(FD->getType(), V.getUnionValue(), false);
5581     }
5582     Out << 'E';
5583     break;
5584   }
5585 
5586   case APValue::Array: {
5587     QualType ElemT(T->getArrayElementTypeNoTypeQual(), 0);
5588 
5589     NotPrimaryExpr();
5590     Out << "tl";
5591     mangleType(T);
5592 
5593     // Drop trailing zero-initialized elements.
5594     unsigned N = V.getArraySize();
5595     if (!V.hasArrayFiller() || isZeroInitialized(ElemT, V.getArrayFiller())) {
5596       N = V.getArrayInitializedElts();
5597       while (N && isZeroInitialized(ElemT, V.getArrayInitializedElt(N - 1)))
5598         --N;
5599     }
5600 
5601     for (unsigned I = 0; I != N; ++I) {
5602       const APValue &Elem = I < V.getArrayInitializedElts()
5603                                 ? V.getArrayInitializedElt(I)
5604                                 : V.getArrayFiller();
5605       mangleValueInTemplateArg(ElemT, Elem, false);
5606     }
5607     Out << 'E';
5608     break;
5609   }
5610 
5611   case APValue::Vector: {
5612     const VectorType *VT = T->castAs<VectorType>();
5613 
5614     NotPrimaryExpr();
5615     Out << "tl";
5616     mangleType(T);
5617     unsigned N = V.getVectorLength();
5618     while (N && isZeroInitialized(VT->getElementType(), V.getVectorElt(N - 1)))
5619       --N;
5620     for (unsigned I = 0; I != N; ++I)
5621       mangleValueInTemplateArg(VT->getElementType(), V.getVectorElt(I), false);
5622     Out << 'E';
5623     break;
5624   }
5625 
5626   case APValue::Int:
5627     mangleIntegerLiteral(T, V.getInt());
5628     break;
5629 
5630   case APValue::Float:
5631     mangleFloatLiteral(T, V.getFloat());
5632     break;
5633 
5634   case APValue::FixedPoint:
5635     mangleFixedPointLiteral();
5636     break;
5637 
5638   case APValue::ComplexFloat: {
5639     const ComplexType *CT = T->castAs<ComplexType>();
5640     NotPrimaryExpr();
5641     Out << "tl";
5642     mangleType(T);
5643     if (!V.getComplexFloatReal().isPosZero() ||
5644         !V.getComplexFloatImag().isPosZero())
5645       mangleFloatLiteral(CT->getElementType(), V.getComplexFloatReal());
5646     if (!V.getComplexFloatImag().isPosZero())
5647       mangleFloatLiteral(CT->getElementType(), V.getComplexFloatImag());
5648     Out << 'E';
5649     break;
5650   }
5651 
5652   case APValue::ComplexInt: {
5653     const ComplexType *CT = T->castAs<ComplexType>();
5654     NotPrimaryExpr();
5655     Out << "tl";
5656     mangleType(T);
5657     if (V.getComplexIntReal().getBoolValue() ||
5658         V.getComplexIntImag().getBoolValue())
5659       mangleIntegerLiteral(CT->getElementType(), V.getComplexIntReal());
5660     if (V.getComplexIntImag().getBoolValue())
5661       mangleIntegerLiteral(CT->getElementType(), V.getComplexIntImag());
5662     Out << 'E';
5663     break;
5664   }
5665 
5666   case APValue::LValue: {
5667     // Proposed in https://github.com/itanium-cxx-abi/cxx-abi/issues/47.
5668     assert((T->isPointerType() || T->isReferenceType()) &&
5669            "unexpected type for LValue template arg");
5670 
5671     if (V.isNullPointer()) {
5672       mangleNullPointer(T);
5673       break;
5674     }
5675 
5676     APValue::LValueBase B = V.getLValueBase();
5677     if (!B) {
5678       // Non-standard mangling for integer cast to a pointer; this can only
5679       // occur as an extension.
5680       CharUnits Offset = V.getLValueOffset();
5681       if (Offset.isZero()) {
5682         // This is reinterpret_cast<T*>(0), not a null pointer. Mangle this as
5683         // a cast, because L <type> 0 E means something else.
5684         NotPrimaryExpr();
5685         Out << "rc";
5686         mangleType(T);
5687         Out << "Li0E";
5688         if (TopLevel)
5689           Out << 'E';
5690       } else {
5691         Out << "L";
5692         mangleType(T);
5693         Out << Offset.getQuantity() << 'E';
5694       }
5695       break;
5696     }
5697 
5698     ASTContext &Ctx = Context.getASTContext();
5699 
5700     enum { Base, Offset, Path } Kind;
5701     if (!V.hasLValuePath()) {
5702       // Mangle as (T*)((char*)&base + N).
5703       if (T->isReferenceType()) {
5704         NotPrimaryExpr();
5705         Out << "decvP";
5706         mangleType(T->getPointeeType());
5707       } else {
5708         NotPrimaryExpr();
5709         Out << "cv";
5710         mangleType(T);
5711       }
5712       Out << "plcvPcad";
5713       Kind = Offset;
5714     } else {
5715       if (!V.getLValuePath().empty() || V.isLValueOnePastTheEnd()) {
5716         NotPrimaryExpr();
5717         // A final conversion to the template parameter's type is usually
5718         // folded into the 'so' mangling, but we can't do that for 'void*'
5719         // parameters without introducing collisions.
5720         if (NeedExactType && T->isVoidPointerType()) {
5721           Out << "cv";
5722           mangleType(T);
5723         }
5724         if (T->isPointerType())
5725           Out << "ad";
5726         Out << "so";
5727         mangleType(T->isVoidPointerType()
5728                        ? getLValueType(Ctx, V).getUnqualifiedType()
5729                        : T->getPointeeType());
5730         Kind = Path;
5731       } else {
5732         if (NeedExactType &&
5733             !Ctx.hasSameType(T->getPointeeType(), getLValueType(Ctx, V)) &&
5734             Ctx.getLangOpts().getClangABICompat() >
5735                 LangOptions::ClangABI::Ver11) {
5736           NotPrimaryExpr();
5737           Out << "cv";
5738           mangleType(T);
5739         }
5740         if (T->isPointerType()) {
5741           NotPrimaryExpr();
5742           Out << "ad";
5743         }
5744         Kind = Base;
5745       }
5746     }
5747 
5748     QualType TypeSoFar = B.getType();
5749     if (auto *VD = B.dyn_cast<const ValueDecl*>()) {
5750       Out << 'L';
5751       mangle(VD);
5752       Out << 'E';
5753     } else if (auto *E = B.dyn_cast<const Expr*>()) {
5754       NotPrimaryExpr();
5755       mangleExpression(E);
5756     } else if (auto TI = B.dyn_cast<TypeInfoLValue>()) {
5757       NotPrimaryExpr();
5758       Out << "ti";
5759       mangleType(QualType(TI.getType(), 0));
5760     } else {
5761       // We should never see dynamic allocations here.
5762       llvm_unreachable("unexpected lvalue base kind in template argument");
5763     }
5764 
5765     switch (Kind) {
5766     case Base:
5767       break;
5768 
5769     case Offset:
5770       Out << 'L';
5771       mangleType(Ctx.getPointerDiffType());
5772       mangleNumber(V.getLValueOffset().getQuantity());
5773       Out << 'E';
5774       break;
5775 
5776     case Path:
5777       // <expression> ::= so <referent type> <expr> [<offset number>]
5778       //                  <union-selector>* [p] E
5779       if (!V.getLValueOffset().isZero())
5780         mangleNumber(V.getLValueOffset().getQuantity());
5781 
5782       // We model a past-the-end array pointer as array indexing with index N,
5783       // not with the "past the end" flag. Compensate for that.
5784       bool OnePastTheEnd = V.isLValueOnePastTheEnd();
5785 
5786       for (APValue::LValuePathEntry E : V.getLValuePath()) {
5787         if (auto *AT = TypeSoFar->getAsArrayTypeUnsafe()) {
5788           if (auto *CAT = dyn_cast<ConstantArrayType>(AT))
5789             OnePastTheEnd |= CAT->getSize() == E.getAsArrayIndex();
5790           TypeSoFar = AT->getElementType();
5791         } else {
5792           const Decl *D = E.getAsBaseOrMember().getPointer();
5793           if (auto *FD = dyn_cast<FieldDecl>(D)) {
5794             // <union-selector> ::= _ <number>
5795             if (FD->getParent()->isUnion()) {
5796               Out << '_';
5797               if (FD->getFieldIndex())
5798                 Out << (FD->getFieldIndex() - 1);
5799             }
5800             TypeSoFar = FD->getType();
5801           } else {
5802             TypeSoFar = Ctx.getRecordType(cast<CXXRecordDecl>(D));
5803           }
5804         }
5805       }
5806 
5807       if (OnePastTheEnd)
5808         Out << 'p';
5809       Out << 'E';
5810       break;
5811     }
5812 
5813     break;
5814   }
5815 
5816   case APValue::MemberPointer:
5817     // Proposed in https://github.com/itanium-cxx-abi/cxx-abi/issues/47.
5818     if (!V.getMemberPointerDecl()) {
5819       mangleNullPointer(T);
5820       break;
5821     }
5822 
5823     ASTContext &Ctx = Context.getASTContext();
5824 
5825     NotPrimaryExpr();
5826     if (!V.getMemberPointerPath().empty()) {
5827       Out << "mc";
5828       mangleType(T);
5829     } else if (NeedExactType &&
5830                !Ctx.hasSameType(
5831                    T->castAs<MemberPointerType>()->getPointeeType(),
5832                    V.getMemberPointerDecl()->getType()) &&
5833                Ctx.getLangOpts().getClangABICompat() >
5834                    LangOptions::ClangABI::Ver11) {
5835       Out << "cv";
5836       mangleType(T);
5837     }
5838     Out << "adL";
5839     mangle(V.getMemberPointerDecl());
5840     Out << 'E';
5841     if (!V.getMemberPointerPath().empty()) {
5842       CharUnits Offset =
5843           Context.getASTContext().getMemberPointerPathAdjustment(V);
5844       if (!Offset.isZero())
5845         mangleNumber(Offset.getQuantity());
5846       Out << 'E';
5847     }
5848     break;
5849   }
5850 
5851   if (TopLevel && !IsPrimaryExpr)
5852     Out << 'E';
5853 }
5854 
mangleTemplateParameter(unsigned Depth,unsigned Index)5855 void CXXNameMangler::mangleTemplateParameter(unsigned Depth, unsigned Index) {
5856   // <template-param> ::= T_    # first template parameter
5857   //                  ::= T <parameter-2 non-negative number> _
5858   //                  ::= TL <L-1 non-negative number> __
5859   //                  ::= TL <L-1 non-negative number> _
5860   //                         <parameter-2 non-negative number> _
5861   //
5862   // The latter two manglings are from a proposal here:
5863   // https://github.com/itanium-cxx-abi/cxx-abi/issues/31#issuecomment-528122117
5864   Out << 'T';
5865   if (Depth != 0)
5866     Out << 'L' << (Depth - 1) << '_';
5867   if (Index != 0)
5868     Out << (Index - 1);
5869   Out << '_';
5870 }
5871 
mangleSeqID(unsigned SeqID)5872 void CXXNameMangler::mangleSeqID(unsigned SeqID) {
5873   if (SeqID == 1)
5874     Out << '0';
5875   else if (SeqID > 1) {
5876     SeqID--;
5877 
5878     // <seq-id> is encoded in base-36, using digits and upper case letters.
5879     char Buffer[7]; // log(2**32) / log(36) ~= 7
5880     MutableArrayRef<char> BufferRef(Buffer);
5881     MutableArrayRef<char>::reverse_iterator I = BufferRef.rbegin();
5882 
5883     for (; SeqID != 0; SeqID /= 36) {
5884       unsigned C = SeqID % 36;
5885       *I++ = (C < 10 ? '0' + C : 'A' + C - 10);
5886     }
5887 
5888     Out.write(I.base(), I - BufferRef.rbegin());
5889   }
5890   Out << '_';
5891 }
5892 
mangleExistingSubstitution(TemplateName tname)5893 void CXXNameMangler::mangleExistingSubstitution(TemplateName tname) {
5894   bool result = mangleSubstitution(tname);
5895   assert(result && "no existing substitution for template name");
5896   (void) result;
5897 }
5898 
5899 // <substitution> ::= S <seq-id> _
5900 //                ::= S_
mangleSubstitution(const NamedDecl * ND)5901 bool CXXNameMangler::mangleSubstitution(const NamedDecl *ND) {
5902   // Try one of the standard substitutions first.
5903   if (mangleStandardSubstitution(ND))
5904     return true;
5905 
5906   ND = cast<NamedDecl>(ND->getCanonicalDecl());
5907   return mangleSubstitution(reinterpret_cast<uintptr_t>(ND));
5908 }
5909 
5910 /// Determine whether the given type has any qualifiers that are relevant for
5911 /// substitutions.
hasMangledSubstitutionQualifiers(QualType T)5912 static bool hasMangledSubstitutionQualifiers(QualType T) {
5913   Qualifiers Qs = T.getQualifiers();
5914   return Qs.getCVRQualifiers() || Qs.hasAddressSpace() || Qs.hasUnaligned();
5915 }
5916 
mangleSubstitution(QualType T)5917 bool CXXNameMangler::mangleSubstitution(QualType T) {
5918   if (!hasMangledSubstitutionQualifiers(T)) {
5919     if (const RecordType *RT = T->getAs<RecordType>())
5920       return mangleSubstitution(RT->getDecl());
5921   }
5922 
5923   uintptr_t TypePtr = reinterpret_cast<uintptr_t>(T.getAsOpaquePtr());
5924 
5925   return mangleSubstitution(TypePtr);
5926 }
5927 
mangleSubstitution(TemplateName Template)5928 bool CXXNameMangler::mangleSubstitution(TemplateName Template) {
5929   if (TemplateDecl *TD = Template.getAsTemplateDecl())
5930     return mangleSubstitution(TD);
5931 
5932   Template = Context.getASTContext().getCanonicalTemplateName(Template);
5933   return mangleSubstitution(
5934                       reinterpret_cast<uintptr_t>(Template.getAsVoidPointer()));
5935 }
5936 
mangleSubstitution(uintptr_t Ptr)5937 bool CXXNameMangler::mangleSubstitution(uintptr_t Ptr) {
5938   llvm::DenseMap<uintptr_t, unsigned>::iterator I = Substitutions.find(Ptr);
5939   if (I == Substitutions.end())
5940     return false;
5941 
5942   unsigned SeqID = I->second;
5943   Out << 'S';
5944   mangleSeqID(SeqID);
5945 
5946   return true;
5947 }
5948 
isCharType(QualType T)5949 static bool isCharType(QualType T) {
5950   if (T.isNull())
5951     return false;
5952 
5953   return T->isSpecificBuiltinType(BuiltinType::Char_S) ||
5954     T->isSpecificBuiltinType(BuiltinType::Char_U);
5955 }
5956 
5957 /// Returns whether a given type is a template specialization of a given name
5958 /// with a single argument of type char.
isCharSpecialization(QualType T,const char * Name)5959 static bool isCharSpecialization(QualType T, const char *Name) {
5960   if (T.isNull())
5961     return false;
5962 
5963   const RecordType *RT = T->getAs<RecordType>();
5964   if (!RT)
5965     return false;
5966 
5967   const ClassTemplateSpecializationDecl *SD =
5968     dyn_cast<ClassTemplateSpecializationDecl>(RT->getDecl());
5969   if (!SD)
5970     return false;
5971 
5972   if (!isStdNamespace(getEffectiveDeclContext(SD)))
5973     return false;
5974 
5975   const TemplateArgumentList &TemplateArgs = SD->getTemplateArgs();
5976   if (TemplateArgs.size() != 1)
5977     return false;
5978 
5979   if (!isCharType(TemplateArgs[0].getAsType()))
5980     return false;
5981 
5982   return SD->getIdentifier()->getName() == Name;
5983 }
5984 
5985 template <std::size_t StrLen>
isStreamCharSpecialization(const ClassTemplateSpecializationDecl * SD,const char (& Str)[StrLen])5986 static bool isStreamCharSpecialization(const ClassTemplateSpecializationDecl*SD,
5987                                        const char (&Str)[StrLen]) {
5988   if (!SD->getIdentifier()->isStr(Str))
5989     return false;
5990 
5991   const TemplateArgumentList &TemplateArgs = SD->getTemplateArgs();
5992   if (TemplateArgs.size() != 2)
5993     return false;
5994 
5995   if (!isCharType(TemplateArgs[0].getAsType()))
5996     return false;
5997 
5998   if (!isCharSpecialization(TemplateArgs[1].getAsType(), "char_traits"))
5999     return false;
6000 
6001   return true;
6002 }
6003 
mangleStandardSubstitution(const NamedDecl * ND)6004 bool CXXNameMangler::mangleStandardSubstitution(const NamedDecl *ND) {
6005   // <substitution> ::= St # ::std::
6006   if (const NamespaceDecl *NS = dyn_cast<NamespaceDecl>(ND)) {
6007     if (isStd(NS)) {
6008       Out << "St";
6009       return true;
6010     }
6011   }
6012 
6013   if (const ClassTemplateDecl *TD = dyn_cast<ClassTemplateDecl>(ND)) {
6014     if (!isStdNamespace(getEffectiveDeclContext(TD)))
6015       return false;
6016 
6017     // <substitution> ::= Sa # ::std::allocator
6018     if (TD->getIdentifier()->isStr("allocator")) {
6019       Out << "Sa";
6020       return true;
6021     }
6022 
6023     // <<substitution> ::= Sb # ::std::basic_string
6024     if (TD->getIdentifier()->isStr("basic_string")) {
6025       Out << "Sb";
6026       return true;
6027     }
6028   }
6029 
6030   if (const ClassTemplateSpecializationDecl *SD =
6031         dyn_cast<ClassTemplateSpecializationDecl>(ND)) {
6032     if (!isStdNamespace(getEffectiveDeclContext(SD)))
6033       return false;
6034 
6035     //    <substitution> ::= Ss # ::std::basic_string<char,
6036     //                            ::std::char_traits<char>,
6037     //                            ::std::allocator<char> >
6038     if (SD->getIdentifier()->isStr("basic_string")) {
6039       const TemplateArgumentList &TemplateArgs = SD->getTemplateArgs();
6040 
6041       if (TemplateArgs.size() != 3)
6042         return false;
6043 
6044       if (!isCharType(TemplateArgs[0].getAsType()))
6045         return false;
6046 
6047       if (!isCharSpecialization(TemplateArgs[1].getAsType(), "char_traits"))
6048         return false;
6049 
6050       if (!isCharSpecialization(TemplateArgs[2].getAsType(), "allocator"))
6051         return false;
6052 
6053       Out << "Ss";
6054       return true;
6055     }
6056 
6057     //    <substitution> ::= Si # ::std::basic_istream<char,
6058     //                            ::std::char_traits<char> >
6059     if (isStreamCharSpecialization(SD, "basic_istream")) {
6060       Out << "Si";
6061       return true;
6062     }
6063 
6064     //    <substitution> ::= So # ::std::basic_ostream<char,
6065     //                            ::std::char_traits<char> >
6066     if (isStreamCharSpecialization(SD, "basic_ostream")) {
6067       Out << "So";
6068       return true;
6069     }
6070 
6071     //    <substitution> ::= Sd # ::std::basic_iostream<char,
6072     //                            ::std::char_traits<char> >
6073     if (isStreamCharSpecialization(SD, "basic_iostream")) {
6074       Out << "Sd";
6075       return true;
6076     }
6077   }
6078   return false;
6079 }
6080 
addSubstitution(QualType T)6081 void CXXNameMangler::addSubstitution(QualType T) {
6082   if (!hasMangledSubstitutionQualifiers(T)) {
6083     if (const RecordType *RT = T->getAs<RecordType>()) {
6084       addSubstitution(RT->getDecl());
6085       return;
6086     }
6087   }
6088 
6089   uintptr_t TypePtr = reinterpret_cast<uintptr_t>(T.getAsOpaquePtr());
6090   addSubstitution(TypePtr);
6091 }
6092 
addSubstitution(TemplateName Template)6093 void CXXNameMangler::addSubstitution(TemplateName Template) {
6094   if (TemplateDecl *TD = Template.getAsTemplateDecl())
6095     return addSubstitution(TD);
6096 
6097   Template = Context.getASTContext().getCanonicalTemplateName(Template);
6098   addSubstitution(reinterpret_cast<uintptr_t>(Template.getAsVoidPointer()));
6099 }
6100 
addSubstitution(uintptr_t Ptr)6101 void CXXNameMangler::addSubstitution(uintptr_t Ptr) {
6102   assert(!Substitutions.count(Ptr) && "Substitution already exists!");
6103   Substitutions[Ptr] = SeqID++;
6104 }
6105 
extendSubstitutions(CXXNameMangler * Other)6106 void CXXNameMangler::extendSubstitutions(CXXNameMangler* Other) {
6107   assert(Other->SeqID >= SeqID && "Must be superset of substitutions!");
6108   if (Other->SeqID > SeqID) {
6109     Substitutions.swap(Other->Substitutions);
6110     SeqID = Other->SeqID;
6111   }
6112 }
6113 
6114 CXXNameMangler::AbiTagList
makeFunctionReturnTypeTags(const FunctionDecl * FD)6115 CXXNameMangler::makeFunctionReturnTypeTags(const FunctionDecl *FD) {
6116   // When derived abi tags are disabled there is no need to make any list.
6117   if (DisableDerivedAbiTags)
6118     return AbiTagList();
6119 
6120   llvm::raw_null_ostream NullOutStream;
6121   CXXNameMangler TrackReturnTypeTags(*this, NullOutStream);
6122   TrackReturnTypeTags.disableDerivedAbiTags();
6123 
6124   const FunctionProtoType *Proto =
6125       cast<FunctionProtoType>(FD->getType()->getAs<FunctionType>());
6126   FunctionTypeDepthState saved = TrackReturnTypeTags.FunctionTypeDepth.push();
6127   TrackReturnTypeTags.FunctionTypeDepth.enterResultType();
6128   TrackReturnTypeTags.mangleType(Proto->getReturnType());
6129   TrackReturnTypeTags.FunctionTypeDepth.leaveResultType();
6130   TrackReturnTypeTags.FunctionTypeDepth.pop(saved);
6131 
6132   return TrackReturnTypeTags.AbiTagsRoot.getSortedUniqueUsedAbiTags();
6133 }
6134 
6135 CXXNameMangler::AbiTagList
makeVariableTypeTags(const VarDecl * VD)6136 CXXNameMangler::makeVariableTypeTags(const VarDecl *VD) {
6137   // When derived abi tags are disabled there is no need to make any list.
6138   if (DisableDerivedAbiTags)
6139     return AbiTagList();
6140 
6141   llvm::raw_null_ostream NullOutStream;
6142   CXXNameMangler TrackVariableType(*this, NullOutStream);
6143   TrackVariableType.disableDerivedAbiTags();
6144 
6145   TrackVariableType.mangleType(VD->getType());
6146 
6147   return TrackVariableType.AbiTagsRoot.getSortedUniqueUsedAbiTags();
6148 }
6149 
shouldHaveAbiTags(ItaniumMangleContextImpl & C,const VarDecl * VD)6150 bool CXXNameMangler::shouldHaveAbiTags(ItaniumMangleContextImpl &C,
6151                                        const VarDecl *VD) {
6152   llvm::raw_null_ostream NullOutStream;
6153   CXXNameMangler TrackAbiTags(C, NullOutStream, nullptr, true);
6154   TrackAbiTags.mangle(VD);
6155   return TrackAbiTags.AbiTagsRoot.getUsedAbiTags().size();
6156 }
6157 
6158 //
6159 
6160 /// Mangles the name of the declaration D and emits that name to the given
6161 /// output stream.
6162 ///
6163 /// If the declaration D requires a mangled name, this routine will emit that
6164 /// mangled name to \p os and return true. Otherwise, \p os will be unchanged
6165 /// and this routine will return false. In this case, the caller should just
6166 /// emit the identifier of the declaration (\c D->getIdentifier()) as its
6167 /// name.
mangleCXXName(GlobalDecl GD,raw_ostream & Out)6168 void ItaniumMangleContextImpl::mangleCXXName(GlobalDecl GD,
6169                                              raw_ostream &Out) {
6170   const NamedDecl *D = cast<NamedDecl>(GD.getDecl());
6171   assert((isa<FunctionDecl, VarDecl, TemplateParamObjectDecl>(D)) &&
6172          "Invalid mangleName() call, argument is not a variable or function!");
6173 
6174   PrettyStackTraceDecl CrashInfo(D, SourceLocation(),
6175                                  getASTContext().getSourceManager(),
6176                                  "Mangling declaration");
6177 
6178   if (auto *CD = dyn_cast<CXXConstructorDecl>(D)) {
6179     auto Type = GD.getCtorType();
6180     CXXNameMangler Mangler(*this, Out, CD, Type);
6181     return Mangler.mangle(GlobalDecl(CD, Type));
6182   }
6183 
6184   if (auto *DD = dyn_cast<CXXDestructorDecl>(D)) {
6185     auto Type = GD.getDtorType();
6186     CXXNameMangler Mangler(*this, Out, DD, Type);
6187     return Mangler.mangle(GlobalDecl(DD, Type));
6188   }
6189 
6190   CXXNameMangler Mangler(*this, Out, D);
6191   Mangler.mangle(GD);
6192 }
6193 
mangleCXXCtorComdat(const CXXConstructorDecl * D,raw_ostream & Out)6194 void ItaniumMangleContextImpl::mangleCXXCtorComdat(const CXXConstructorDecl *D,
6195                                                    raw_ostream &Out) {
6196   CXXNameMangler Mangler(*this, Out, D, Ctor_Comdat);
6197   Mangler.mangle(GlobalDecl(D, Ctor_Comdat));
6198 }
6199 
mangleCXXDtorComdat(const CXXDestructorDecl * D,raw_ostream & Out)6200 void ItaniumMangleContextImpl::mangleCXXDtorComdat(const CXXDestructorDecl *D,
6201                                                    raw_ostream &Out) {
6202   CXXNameMangler Mangler(*this, Out, D, Dtor_Comdat);
6203   Mangler.mangle(GlobalDecl(D, Dtor_Comdat));
6204 }
6205 
mangleThunk(const CXXMethodDecl * MD,const ThunkInfo & Thunk,raw_ostream & Out)6206 void ItaniumMangleContextImpl::mangleThunk(const CXXMethodDecl *MD,
6207                                            const ThunkInfo &Thunk,
6208                                            raw_ostream &Out) {
6209   //  <special-name> ::= T <call-offset> <base encoding>
6210   //                      # base is the nominal target function of thunk
6211   //  <special-name> ::= Tc <call-offset> <call-offset> <base encoding>
6212   //                      # base is the nominal target function of thunk
6213   //                      # first call-offset is 'this' adjustment
6214   //                      # second call-offset is result adjustment
6215 
6216   assert(!isa<CXXDestructorDecl>(MD) &&
6217          "Use mangleCXXDtor for destructor decls!");
6218   CXXNameMangler Mangler(*this, Out);
6219   Mangler.getStream() << "_ZT";
6220   if (!Thunk.Return.isEmpty())
6221     Mangler.getStream() << 'c';
6222 
6223   // Mangle the 'this' pointer adjustment.
6224   Mangler.mangleCallOffset(Thunk.This.NonVirtual,
6225                            Thunk.This.Virtual.Itanium.VCallOffsetOffset);
6226 
6227   // Mangle the return pointer adjustment if there is one.
6228   if (!Thunk.Return.isEmpty())
6229     Mangler.mangleCallOffset(Thunk.Return.NonVirtual,
6230                              Thunk.Return.Virtual.Itanium.VBaseOffsetOffset);
6231 
6232   Mangler.mangleFunctionEncoding(MD);
6233 }
6234 
mangleCXXDtorThunk(const CXXDestructorDecl * DD,CXXDtorType Type,const ThisAdjustment & ThisAdjustment,raw_ostream & Out)6235 void ItaniumMangleContextImpl::mangleCXXDtorThunk(
6236     const CXXDestructorDecl *DD, CXXDtorType Type,
6237     const ThisAdjustment &ThisAdjustment, raw_ostream &Out) {
6238   //  <special-name> ::= T <call-offset> <base encoding>
6239   //                      # base is the nominal target function of thunk
6240   CXXNameMangler Mangler(*this, Out, DD, Type);
6241   Mangler.getStream() << "_ZT";
6242 
6243   // Mangle the 'this' pointer adjustment.
6244   Mangler.mangleCallOffset(ThisAdjustment.NonVirtual,
6245                            ThisAdjustment.Virtual.Itanium.VCallOffsetOffset);
6246 
6247   Mangler.mangleFunctionEncoding(GlobalDecl(DD, Type));
6248 }
6249 
6250 /// Returns the mangled name for a guard variable for the passed in VarDecl.
mangleStaticGuardVariable(const VarDecl * D,raw_ostream & Out)6251 void ItaniumMangleContextImpl::mangleStaticGuardVariable(const VarDecl *D,
6252                                                          raw_ostream &Out) {
6253   //  <special-name> ::= GV <object name>       # Guard variable for one-time
6254   //                                            # initialization
6255   CXXNameMangler Mangler(*this, Out);
6256   // GCC 5.3.0 doesn't emit derived ABI tags for local names but that seems to
6257   // be a bug that is fixed in trunk.
6258   Mangler.getStream() << "_ZGV";
6259   Mangler.mangleName(D);
6260 }
6261 
mangleDynamicInitializer(const VarDecl * MD,raw_ostream & Out)6262 void ItaniumMangleContextImpl::mangleDynamicInitializer(const VarDecl *MD,
6263                                                         raw_ostream &Out) {
6264   // These symbols are internal in the Itanium ABI, so the names don't matter.
6265   // Clang has traditionally used this symbol and allowed LLVM to adjust it to
6266   // avoid duplicate symbols.
6267   Out << "__cxx_global_var_init";
6268 }
6269 
mangleDynamicAtExitDestructor(const VarDecl * D,raw_ostream & Out)6270 void ItaniumMangleContextImpl::mangleDynamicAtExitDestructor(const VarDecl *D,
6271                                                              raw_ostream &Out) {
6272   // Prefix the mangling of D with __dtor_.
6273   CXXNameMangler Mangler(*this, Out);
6274   Mangler.getStream() << "__dtor_";
6275   if (shouldMangleDeclName(D))
6276     Mangler.mangle(D);
6277   else
6278     Mangler.getStream() << D->getName();
6279 }
6280 
mangleDynamicStermFinalizer(const VarDecl * D,raw_ostream & Out)6281 void ItaniumMangleContextImpl::mangleDynamicStermFinalizer(const VarDecl *D,
6282                                                            raw_ostream &Out) {
6283   // Clang generates these internal-linkage functions as part of its
6284   // implementation of the XL ABI.
6285   CXXNameMangler Mangler(*this, Out);
6286   Mangler.getStream() << "__finalize_";
6287   if (shouldMangleDeclName(D))
6288     Mangler.mangle(D);
6289   else
6290     Mangler.getStream() << D->getName();
6291 }
6292 
mangleSEHFilterExpression(const NamedDecl * EnclosingDecl,raw_ostream & Out)6293 void ItaniumMangleContextImpl::mangleSEHFilterExpression(
6294     const NamedDecl *EnclosingDecl, raw_ostream &Out) {
6295   CXXNameMangler Mangler(*this, Out);
6296   Mangler.getStream() << "__filt_";
6297   if (shouldMangleDeclName(EnclosingDecl))
6298     Mangler.mangle(EnclosingDecl);
6299   else
6300     Mangler.getStream() << EnclosingDecl->getName();
6301 }
6302 
mangleSEHFinallyBlock(const NamedDecl * EnclosingDecl,raw_ostream & Out)6303 void ItaniumMangleContextImpl::mangleSEHFinallyBlock(
6304     const NamedDecl *EnclosingDecl, raw_ostream &Out) {
6305   CXXNameMangler Mangler(*this, Out);
6306   Mangler.getStream() << "__fin_";
6307   if (shouldMangleDeclName(EnclosingDecl))
6308     Mangler.mangle(EnclosingDecl);
6309   else
6310     Mangler.getStream() << EnclosingDecl->getName();
6311 }
6312 
mangleItaniumThreadLocalInit(const VarDecl * D,raw_ostream & Out)6313 void ItaniumMangleContextImpl::mangleItaniumThreadLocalInit(const VarDecl *D,
6314                                                             raw_ostream &Out) {
6315   //  <special-name> ::= TH <object name>
6316   CXXNameMangler Mangler(*this, Out);
6317   Mangler.getStream() << "_ZTH";
6318   Mangler.mangleName(D);
6319 }
6320 
6321 void
mangleItaniumThreadLocalWrapper(const VarDecl * D,raw_ostream & Out)6322 ItaniumMangleContextImpl::mangleItaniumThreadLocalWrapper(const VarDecl *D,
6323                                                           raw_ostream &Out) {
6324   //  <special-name> ::= TW <object name>
6325   CXXNameMangler Mangler(*this, Out);
6326   Mangler.getStream() << "_ZTW";
6327   Mangler.mangleName(D);
6328 }
6329 
mangleReferenceTemporary(const VarDecl * D,unsigned ManglingNumber,raw_ostream & Out)6330 void ItaniumMangleContextImpl::mangleReferenceTemporary(const VarDecl *D,
6331                                                         unsigned ManglingNumber,
6332                                                         raw_ostream &Out) {
6333   // We match the GCC mangling here.
6334   //  <special-name> ::= GR <object name>
6335   CXXNameMangler Mangler(*this, Out);
6336   Mangler.getStream() << "_ZGR";
6337   Mangler.mangleName(D);
6338   assert(ManglingNumber > 0 && "Reference temporary mangling number is zero!");
6339   Mangler.mangleSeqID(ManglingNumber - 1);
6340 }
6341 
mangleCXXVTable(const CXXRecordDecl * RD,raw_ostream & Out)6342 void ItaniumMangleContextImpl::mangleCXXVTable(const CXXRecordDecl *RD,
6343                                                raw_ostream &Out) {
6344   // <special-name> ::= TV <type>  # virtual table
6345   CXXNameMangler Mangler(*this, Out);
6346   Mangler.getStream() << "_ZTV";
6347   Mangler.mangleNameOrStandardSubstitution(RD);
6348 }
6349 
mangleCXXVTT(const CXXRecordDecl * RD,raw_ostream & Out)6350 void ItaniumMangleContextImpl::mangleCXXVTT(const CXXRecordDecl *RD,
6351                                             raw_ostream &Out) {
6352   // <special-name> ::= TT <type>  # VTT structure
6353   CXXNameMangler Mangler(*this, Out);
6354   Mangler.getStream() << "_ZTT";
6355   Mangler.mangleNameOrStandardSubstitution(RD);
6356 }
6357 
mangleCXXCtorVTable(const CXXRecordDecl * RD,int64_t Offset,const CXXRecordDecl * Type,raw_ostream & Out)6358 void ItaniumMangleContextImpl::mangleCXXCtorVTable(const CXXRecordDecl *RD,
6359                                                    int64_t Offset,
6360                                                    const CXXRecordDecl *Type,
6361                                                    raw_ostream &Out) {
6362   // <special-name> ::= TC <type> <offset number> _ <base type>
6363   CXXNameMangler Mangler(*this, Out);
6364   Mangler.getStream() << "_ZTC";
6365   Mangler.mangleNameOrStandardSubstitution(RD);
6366   Mangler.getStream() << Offset;
6367   Mangler.getStream() << '_';
6368   Mangler.mangleNameOrStandardSubstitution(Type);
6369 }
6370 
mangleCXXRTTI(QualType Ty,raw_ostream & Out)6371 void ItaniumMangleContextImpl::mangleCXXRTTI(QualType Ty, raw_ostream &Out) {
6372   // <special-name> ::= TI <type>  # typeinfo structure
6373   assert(!Ty.hasQualifiers() && "RTTI info cannot have top-level qualifiers");
6374   CXXNameMangler Mangler(*this, Out);
6375   Mangler.getStream() << "_ZTI";
6376   Mangler.mangleType(Ty);
6377 }
6378 
mangleCXXRTTIName(QualType Ty,raw_ostream & Out)6379 void ItaniumMangleContextImpl::mangleCXXRTTIName(QualType Ty,
6380                                                  raw_ostream &Out) {
6381   // <special-name> ::= TS <type>  # typeinfo name (null terminated byte string)
6382   CXXNameMangler Mangler(*this, Out);
6383   Mangler.getStream() << "_ZTS";
6384   Mangler.mangleType(Ty);
6385 }
6386 
mangleTypeName(QualType Ty,raw_ostream & Out)6387 void ItaniumMangleContextImpl::mangleTypeName(QualType Ty, raw_ostream &Out) {
6388   mangleCXXRTTIName(Ty, Out);
6389 }
6390 
mangleStringLiteral(const StringLiteral *,raw_ostream &)6391 void ItaniumMangleContextImpl::mangleStringLiteral(const StringLiteral *, raw_ostream &) {
6392   llvm_unreachable("Can't mangle string literals");
6393 }
6394 
mangleLambdaSig(const CXXRecordDecl * Lambda,raw_ostream & Out)6395 void ItaniumMangleContextImpl::mangleLambdaSig(const CXXRecordDecl *Lambda,
6396                                                raw_ostream &Out) {
6397   CXXNameMangler Mangler(*this, Out);
6398   Mangler.mangleLambdaSig(Lambda);
6399 }
6400 
create(ASTContext & Context,DiagnosticsEngine & Diags)6401 ItaniumMangleContext *ItaniumMangleContext::create(ASTContext &Context,
6402                                                    DiagnosticsEngine &Diags) {
6403   return new ItaniumMangleContextImpl(
6404       Context, Diags,
6405       [](ASTContext &, const NamedDecl *) -> llvm::Optional<unsigned> {
6406         return llvm::None;
6407       });
6408 }
6409 
6410 ItaniumMangleContext *
create(ASTContext & Context,DiagnosticsEngine & Diags,DiscriminatorOverrideTy DiscriminatorOverride)6411 ItaniumMangleContext::create(ASTContext &Context, DiagnosticsEngine &Diags,
6412                              DiscriminatorOverrideTy DiscriminatorOverride) {
6413   return new ItaniumMangleContextImpl(Context, Diags, DiscriminatorOverride);
6414 }
6415