1 //===------------------------- ItaniumDemangle.h ----------------*- 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 // Generic itanium demangler library.
10 // There are two copies of this file in the source tree.  The one under
11 // libcxxabi is the original and the one under llvm is the copy.  Use
12 // cp-to-llvm.sh to update the copy.  See README.txt for more details.
13 //
14 //===----------------------------------------------------------------------===//
15 
16 #ifndef DEMANGLE_ITANIUMDEMANGLE_H
17 #define DEMANGLE_ITANIUMDEMANGLE_H
18 
19 #include "DemangleConfig.h"
20 #include "StringView.h"
21 #include "Utility.h"
22 #include <algorithm>
23 #include <cassert>
24 #include <cctype>
25 #include <cstdio>
26 #include <cstdlib>
27 #include <cstring>
28 #include <limits>
29 #include <utility>
30 
31 DEMANGLE_NAMESPACE_BEGIN
32 
33 template <class T, size_t N> class PODSmallVector {
34   static_assert(std::is_pod<T>::value,
35                 "T is required to be a plain old data type");
36 
37   T *First = nullptr;
38   T *Last = nullptr;
39   T *Cap = nullptr;
40   T Inline[N] = {0};
41 
42   bool isInline() const { return First == Inline; }
43 
44   void clearInline() {
45     First = Inline;
46     Last = Inline;
47     Cap = Inline + N;
48   }
49 
50   void reserve(size_t NewCap) {
51     size_t S = size();
52     if (isInline()) {
53       auto *Tmp = static_cast<T *>(std::malloc(NewCap * sizeof(T)));
54       if (Tmp == nullptr)
55         std::terminate();
56       std::copy(First, Last, Tmp);
57       First = Tmp;
58     } else {
59       First = static_cast<T *>(std::realloc(First, NewCap * sizeof(T)));
60       if (First == nullptr)
61         std::terminate();
62     }
63     Last = First + S;
64     Cap = First + NewCap;
65   }
66 
67 public:
68   PODSmallVector() : First(Inline), Last(First), Cap(Inline + N) {}
69 
70   PODSmallVector(const PODSmallVector &) = delete;
71   PODSmallVector &operator=(const PODSmallVector &) = delete;
72 
73   PODSmallVector(PODSmallVector &&Other) : PODSmallVector() {
74     if (Other.isInline()) {
75       std::copy(Other.begin(), Other.end(), First);
76       Last = First + Other.size();
77       Other.clear();
78       return;
79     }
80 
81     First = Other.First;
82     Last = Other.Last;
83     Cap = Other.Cap;
84     Other.clearInline();
85   }
86 
87   PODSmallVector &operator=(PODSmallVector &&Other) {
88     if (Other.isInline()) {
89       if (!isInline()) {
90         std::free(First);
91         clearInline();
92       }
93       std::copy(Other.begin(), Other.end(), First);
94       Last = First + Other.size();
95       Other.clear();
96       return *this;
97     }
98 
99     if (isInline()) {
100       First = Other.First;
101       Last = Other.Last;
102       Cap = Other.Cap;
103       Other.clearInline();
104       return *this;
105     }
106 
107     std::swap(First, Other.First);
108     std::swap(Last, Other.Last);
109     std::swap(Cap, Other.Cap);
110     Other.clear();
111     return *this;
112   }
113 
114   // NOLINTNEXTLINE(readability-identifier-naming)
115   void push_back(const T &Elem) {
116     if (Last == Cap)
117       reserve(size() * 2);
118     *Last++ = Elem;
119   }
120 
121   // NOLINTNEXTLINE(readability-identifier-naming)
122   void pop_back() {
123     assert(Last != First && "Popping empty vector!");
124     --Last;
125   }
126 
127   void dropBack(size_t Index) {
128     assert(Index <= size() && "dropBack() can't expand!");
129     Last = First + Index;
130   }
131 
132   T *begin() { return First; }
133   T *end() { return Last; }
134 
135   bool empty() const { return First == Last; }
136   size_t size() const { return static_cast<size_t>(Last - First); }
137   T &back() {
138     assert(Last != First && "Calling back() on empty vector!");
139     return *(Last - 1);
140   }
141   T &operator[](size_t Index) {
142     assert(Index < size() && "Invalid access!");
143     return *(begin() + Index);
144   }
145   void clear() { Last = First; }
146 
147   ~PODSmallVector() {
148     if (!isInline())
149       std::free(First);
150   }
151 };
152 
153 // Base class of all AST nodes. The AST is built by the parser, then is
154 // traversed by the printLeft/Right functions to produce a demangled string.
155 class Node {
156 public:
157   enum Kind : unsigned char {
158 #define NODE(NodeKind) K##NodeKind,
159 #include "ItaniumNodes.def"
160   };
161 
162   /// Three-way bool to track a cached value. Unknown is possible if this node
163   /// has an unexpanded parameter pack below it that may affect this cache.
164   enum class Cache : unsigned char { Yes, No, Unknown, };
165 
166   /// Operator precedence for expression nodes. Used to determine required
167   /// parens in expression emission.
168   enum class Prec {
169     Primary,
170     Postfix,
171     Unary,
172     Cast,
173     PtrMem,
174     Multiplicative,
175     Additive,
176     Shift,
177     Spaceship,
178     Relational,
179     Equality,
180     And,
181     Xor,
182     Ior,
183     AndIf,
184     OrIf,
185     Conditional,
186     Assign,
187     Comma,
188     Default,
189   };
190 
191 private:
192   Kind K;
193 
194   Prec Precedence : 6;
195 
196   // FIXME: Make these protected.
197 public:
198   /// Tracks if this node has a component on its right side, in which case we
199   /// need to call printRight.
200   Cache RHSComponentCache : 2;
201 
202   /// Track if this node is a (possibly qualified) array type. This can affect
203   /// how we format the output string.
204   Cache ArrayCache : 2;
205 
206   /// Track if this node is a (possibly qualified) function type. This can
207   /// affect how we format the output string.
208   Cache FunctionCache : 2;
209 
210 public:
211   Node(Kind K_, Prec Precedence_ = Prec::Primary,
212        Cache RHSComponentCache_ = Cache::No, Cache ArrayCache_ = Cache::No,
213        Cache FunctionCache_ = Cache::No)
214       : K(K_), Precedence(Precedence_), RHSComponentCache(RHSComponentCache_),
215         ArrayCache(ArrayCache_), FunctionCache(FunctionCache_) {}
216   Node(Kind K_, Cache RHSComponentCache_, Cache ArrayCache_ = Cache::No,
217        Cache FunctionCache_ = Cache::No)
218       : Node(K_, Prec::Primary, RHSComponentCache_, ArrayCache_,
219              FunctionCache_) {}
220 
221   /// Visit the most-derived object corresponding to this object.
222   template<typename Fn> void visit(Fn F) const;
223 
224   // The following function is provided by all derived classes:
225   //
226   // Call F with arguments that, when passed to the constructor of this node,
227   // would construct an equivalent node.
228   //template<typename Fn> void match(Fn F) const;
229 
230   bool hasRHSComponent(OutputBuffer &OB) const {
231     if (RHSComponentCache != Cache::Unknown)
232       return RHSComponentCache == Cache::Yes;
233     return hasRHSComponentSlow(OB);
234   }
235 
236   bool hasArray(OutputBuffer &OB) const {
237     if (ArrayCache != Cache::Unknown)
238       return ArrayCache == Cache::Yes;
239     return hasArraySlow(OB);
240   }
241 
242   bool hasFunction(OutputBuffer &OB) const {
243     if (FunctionCache != Cache::Unknown)
244       return FunctionCache == Cache::Yes;
245     return hasFunctionSlow(OB);
246   }
247 
248   Kind getKind() const { return K; }
249 
250   Prec getPrecedence() const { return Precedence; }
251 
252   virtual bool hasRHSComponentSlow(OutputBuffer &) const { return false; }
253   virtual bool hasArraySlow(OutputBuffer &) const { return false; }
254   virtual bool hasFunctionSlow(OutputBuffer &) const { return false; }
255 
256   // Dig through "glue" nodes like ParameterPack and ForwardTemplateReference to
257   // get at a node that actually represents some concrete syntax.
258   virtual const Node *getSyntaxNode(OutputBuffer &) const { return this; }
259 
260   // Print this node as an expression operand, surrounding it in parentheses if
261   // its precedence is [Strictly] weaker than P.
262   void printAsOperand(OutputBuffer &OB, Prec P = Prec::Default,
263                       bool StrictlyWorse = false) const {
264     bool Paren =
265         unsigned(getPrecedence()) >= unsigned(P) + unsigned(StrictlyWorse);
266     if (Paren)
267       OB.printOpen();
268     print(OB);
269     if (Paren)
270       OB.printClose();
271   }
272 
273   void print(OutputBuffer &OB) const {
274     printLeft(OB);
275     if (RHSComponentCache != Cache::No)
276       printRight(OB);
277   }
278 
279   // Print the "left" side of this Node into OutputBuffer.
280   virtual void printLeft(OutputBuffer &) const = 0;
281 
282   // Print the "right". This distinction is necessary to represent C++ types
283   // that appear on the RHS of their subtype, such as arrays or functions.
284   // Since most types don't have such a component, provide a default
285   // implementation.
286   virtual void printRight(OutputBuffer &) const {}
287 
288   virtual StringView getBaseName() const { return StringView(); }
289 
290   // Silence compiler warnings, this dtor will never be called.
291   virtual ~Node() = default;
292 
293 #ifndef NDEBUG
294   DEMANGLE_DUMP_METHOD void dump() const;
295 #endif
296 };
297 
298 class NodeArray {
299   Node **Elements;
300   size_t NumElements;
301 
302 public:
303   NodeArray() : Elements(nullptr), NumElements(0) {}
304   NodeArray(Node **Elements_, size_t NumElements_)
305       : Elements(Elements_), NumElements(NumElements_) {}
306 
307   bool empty() const { return NumElements == 0; }
308   size_t size() const { return NumElements; }
309 
310   Node **begin() const { return Elements; }
311   Node **end() const { return Elements + NumElements; }
312 
313   Node *operator[](size_t Idx) const { return Elements[Idx]; }
314 
315   void printWithComma(OutputBuffer &OB) const {
316     bool FirstElement = true;
317     for (size_t Idx = 0; Idx != NumElements; ++Idx) {
318       size_t BeforeComma = OB.getCurrentPosition();
319       if (!FirstElement)
320         OB += ", ";
321       size_t AfterComma = OB.getCurrentPosition();
322       Elements[Idx]->printAsOperand(OB, Node::Prec::Comma);
323 
324       // Elements[Idx] is an empty parameter pack expansion, we should erase the
325       // comma we just printed.
326       if (AfterComma == OB.getCurrentPosition()) {
327         OB.setCurrentPosition(BeforeComma);
328         continue;
329       }
330 
331       FirstElement = false;
332     }
333   }
334 };
335 
336 struct NodeArrayNode : Node {
337   NodeArray Array;
338   NodeArrayNode(NodeArray Array_) : Node(KNodeArrayNode), Array(Array_) {}
339 
340   template<typename Fn> void match(Fn F) const { F(Array); }
341 
342   void printLeft(OutputBuffer &OB) const override { Array.printWithComma(OB); }
343 };
344 
345 class DotSuffix final : public Node {
346   const Node *Prefix;
347   const StringView Suffix;
348 
349 public:
350   DotSuffix(const Node *Prefix_, StringView Suffix_)
351       : Node(KDotSuffix), Prefix(Prefix_), Suffix(Suffix_) {}
352 
353   template<typename Fn> void match(Fn F) const { F(Prefix, Suffix); }
354 
355   void printLeft(OutputBuffer &OB) const override {
356     Prefix->print(OB);
357     OB += " (";
358     OB += Suffix;
359     OB += ")";
360   }
361 };
362 
363 class VendorExtQualType final : public Node {
364   const Node *Ty;
365   StringView Ext;
366   const Node *TA;
367 
368 public:
369   VendorExtQualType(const Node *Ty_, StringView Ext_, const Node *TA_)
370       : Node(KVendorExtQualType), Ty(Ty_), Ext(Ext_), TA(TA_) {}
371 
372   template <typename Fn> void match(Fn F) const { F(Ty, Ext, TA); }
373 
374   void printLeft(OutputBuffer &OB) const override {
375     Ty->print(OB);
376     OB += " ";
377     OB += Ext;
378     if (TA != nullptr)
379       TA->print(OB);
380   }
381 };
382 
383 enum FunctionRefQual : unsigned char {
384   FrefQualNone,
385   FrefQualLValue,
386   FrefQualRValue,
387 };
388 
389 enum Qualifiers {
390   QualNone = 0,
391   QualConst = 0x1,
392   QualVolatile = 0x2,
393   QualRestrict = 0x4,
394 };
395 
396 inline Qualifiers operator|=(Qualifiers &Q1, Qualifiers Q2) {
397   return Q1 = static_cast<Qualifiers>(Q1 | Q2);
398 }
399 
400 class QualType final : public Node {
401 protected:
402   const Qualifiers Quals;
403   const Node *Child;
404 
405   void printQuals(OutputBuffer &OB) const {
406     if (Quals & QualConst)
407       OB += " const";
408     if (Quals & QualVolatile)
409       OB += " volatile";
410     if (Quals & QualRestrict)
411       OB += " restrict";
412   }
413 
414 public:
415   QualType(const Node *Child_, Qualifiers Quals_)
416       : Node(KQualType, Child_->RHSComponentCache,
417              Child_->ArrayCache, Child_->FunctionCache),
418         Quals(Quals_), Child(Child_) {}
419 
420   template<typename Fn> void match(Fn F) const { F(Child, Quals); }
421 
422   bool hasRHSComponentSlow(OutputBuffer &OB) const override {
423     return Child->hasRHSComponent(OB);
424   }
425   bool hasArraySlow(OutputBuffer &OB) const override {
426     return Child->hasArray(OB);
427   }
428   bool hasFunctionSlow(OutputBuffer &OB) const override {
429     return Child->hasFunction(OB);
430   }
431 
432   void printLeft(OutputBuffer &OB) const override {
433     Child->printLeft(OB);
434     printQuals(OB);
435   }
436 
437   void printRight(OutputBuffer &OB) const override { Child->printRight(OB); }
438 };
439 
440 class ConversionOperatorType final : public Node {
441   const Node *Ty;
442 
443 public:
444   ConversionOperatorType(const Node *Ty_)
445       : Node(KConversionOperatorType), Ty(Ty_) {}
446 
447   template<typename Fn> void match(Fn F) const { F(Ty); }
448 
449   void printLeft(OutputBuffer &OB) const override {
450     OB += "operator ";
451     Ty->print(OB);
452   }
453 };
454 
455 class PostfixQualifiedType final : public Node {
456   const Node *Ty;
457   const StringView Postfix;
458 
459 public:
460   PostfixQualifiedType(const Node *Ty_, StringView Postfix_)
461       : Node(KPostfixQualifiedType), Ty(Ty_), Postfix(Postfix_) {}
462 
463   template<typename Fn> void match(Fn F) const { F(Ty, Postfix); }
464 
465   void printLeft(OutputBuffer &OB) const override {
466     Ty->printLeft(OB);
467     OB += Postfix;
468   }
469 };
470 
471 class NameType final : public Node {
472   const StringView Name;
473 
474 public:
475   NameType(StringView Name_) : Node(KNameType), Name(Name_) {}
476 
477   template<typename Fn> void match(Fn F) const { F(Name); }
478 
479   StringView getName() const { return Name; }
480   StringView getBaseName() const override { return Name; }
481 
482   void printLeft(OutputBuffer &OB) const override { OB += Name; }
483 };
484 
485 class BitIntType final : public Node {
486   const Node *Size;
487   bool Signed;
488 
489 public:
490   BitIntType(const Node *Size_, bool Signed_)
491       : Node(KBitIntType), Size(Size_), Signed(Signed_) {}
492 
493   template <typename Fn> void match(Fn F) const { F(Size, Signed); }
494 
495   void printLeft(OutputBuffer &OB) const override {
496     if (!Signed)
497       OB += "unsigned ";
498     OB += "_BitInt";
499     OB.printOpen();
500     Size->printAsOperand(OB);
501     OB.printClose();
502   }
503 };
504 
505 class ElaboratedTypeSpefType : public Node {
506   StringView Kind;
507   Node *Child;
508 public:
509   ElaboratedTypeSpefType(StringView Kind_, Node *Child_)
510       : Node(KElaboratedTypeSpefType), Kind(Kind_), Child(Child_) {}
511 
512   template<typename Fn> void match(Fn F) const { F(Kind, Child); }
513 
514   void printLeft(OutputBuffer &OB) const override {
515     OB += Kind;
516     OB += ' ';
517     Child->print(OB);
518   }
519 };
520 
521 struct AbiTagAttr : Node {
522   Node *Base;
523   StringView Tag;
524 
525   AbiTagAttr(Node* Base_, StringView Tag_)
526       : Node(KAbiTagAttr, Base_->RHSComponentCache,
527              Base_->ArrayCache, Base_->FunctionCache),
528         Base(Base_), Tag(Tag_) {}
529 
530   template<typename Fn> void match(Fn F) const { F(Base, Tag); }
531 
532   void printLeft(OutputBuffer &OB) const override {
533     Base->printLeft(OB);
534     OB += "[abi:";
535     OB += Tag;
536     OB += "]";
537   }
538 };
539 
540 class EnableIfAttr : public Node {
541   NodeArray Conditions;
542 public:
543   EnableIfAttr(NodeArray Conditions_)
544       : Node(KEnableIfAttr), Conditions(Conditions_) {}
545 
546   template<typename Fn> void match(Fn F) const { F(Conditions); }
547 
548   void printLeft(OutputBuffer &OB) const override {
549     OB += " [enable_if:";
550     Conditions.printWithComma(OB);
551     OB += ']';
552   }
553 };
554 
555 class ObjCProtoName : public Node {
556   const Node *Ty;
557   StringView Protocol;
558 
559   friend class PointerType;
560 
561 public:
562   ObjCProtoName(const Node *Ty_, StringView Protocol_)
563       : Node(KObjCProtoName), Ty(Ty_), Protocol(Protocol_) {}
564 
565   template<typename Fn> void match(Fn F) const { F(Ty, Protocol); }
566 
567   bool isObjCObject() const {
568     return Ty->getKind() == KNameType &&
569            static_cast<const NameType *>(Ty)->getName() == "objc_object";
570   }
571 
572   void printLeft(OutputBuffer &OB) const override {
573     Ty->print(OB);
574     OB += "<";
575     OB += Protocol;
576     OB += ">";
577   }
578 };
579 
580 class PointerType final : public Node {
581   const Node *Pointee;
582 
583 public:
584   PointerType(const Node *Pointee_)
585       : Node(KPointerType, Pointee_->RHSComponentCache),
586         Pointee(Pointee_) {}
587 
588   template<typename Fn> void match(Fn F) const { F(Pointee); }
589 
590   bool hasRHSComponentSlow(OutputBuffer &OB) const override {
591     return Pointee->hasRHSComponent(OB);
592   }
593 
594   void printLeft(OutputBuffer &OB) const override {
595     // We rewrite objc_object<SomeProtocol>* into id<SomeProtocol>.
596     if (Pointee->getKind() != KObjCProtoName ||
597         !static_cast<const ObjCProtoName *>(Pointee)->isObjCObject()) {
598       Pointee->printLeft(OB);
599       if (Pointee->hasArray(OB))
600         OB += " ";
601       if (Pointee->hasArray(OB) || Pointee->hasFunction(OB))
602         OB += "(";
603       OB += "*";
604     } else {
605       const auto *objcProto = static_cast<const ObjCProtoName *>(Pointee);
606       OB += "id<";
607       OB += objcProto->Protocol;
608       OB += ">";
609     }
610   }
611 
612   void printRight(OutputBuffer &OB) const override {
613     if (Pointee->getKind() != KObjCProtoName ||
614         !static_cast<const ObjCProtoName *>(Pointee)->isObjCObject()) {
615       if (Pointee->hasArray(OB) || Pointee->hasFunction(OB))
616         OB += ")";
617       Pointee->printRight(OB);
618     }
619   }
620 };
621 
622 enum class ReferenceKind {
623   LValue,
624   RValue,
625 };
626 
627 // Represents either a LValue or an RValue reference type.
628 class ReferenceType : public Node {
629   const Node *Pointee;
630   ReferenceKind RK;
631 
632   mutable bool Printing = false;
633 
634   // Dig through any refs to refs, collapsing the ReferenceTypes as we go. The
635   // rule here is rvalue ref to rvalue ref collapses to a rvalue ref, and any
636   // other combination collapses to a lvalue ref.
637   //
638   // A combination of a TemplateForwardReference and a back-ref Substitution
639   // from an ill-formed string may have created a cycle; use cycle detection to
640   // avoid looping forever.
641   std::pair<ReferenceKind, const Node *> collapse(OutputBuffer &OB) const {
642     auto SoFar = std::make_pair(RK, Pointee);
643     // Track the chain of nodes for the Floyd's 'tortoise and hare'
644     // cycle-detection algorithm, since getSyntaxNode(S) is impure
645     PODSmallVector<const Node *, 8> Prev;
646     for (;;) {
647       const Node *SN = SoFar.second->getSyntaxNode(OB);
648       if (SN->getKind() != KReferenceType)
649         break;
650       auto *RT = static_cast<const ReferenceType *>(SN);
651       SoFar.second = RT->Pointee;
652       SoFar.first = std::min(SoFar.first, RT->RK);
653 
654       // The middle of Prev is the 'slow' pointer moving at half speed
655       Prev.push_back(SoFar.second);
656       if (Prev.size() > 1 && SoFar.second == Prev[(Prev.size() - 1) / 2]) {
657         // Cycle detected
658         SoFar.second = nullptr;
659         break;
660       }
661     }
662     return SoFar;
663   }
664 
665 public:
666   ReferenceType(const Node *Pointee_, ReferenceKind RK_)
667       : Node(KReferenceType, Pointee_->RHSComponentCache),
668         Pointee(Pointee_), RK(RK_) {}
669 
670   template<typename Fn> void match(Fn F) const { F(Pointee, RK); }
671 
672   bool hasRHSComponentSlow(OutputBuffer &OB) const override {
673     return Pointee->hasRHSComponent(OB);
674   }
675 
676   void printLeft(OutputBuffer &OB) const override {
677     if (Printing)
678       return;
679     ScopedOverride<bool> SavePrinting(Printing, true);
680     std::pair<ReferenceKind, const Node *> Collapsed = collapse(OB);
681     if (!Collapsed.second)
682       return;
683     Collapsed.second->printLeft(OB);
684     if (Collapsed.second->hasArray(OB))
685       OB += " ";
686     if (Collapsed.second->hasArray(OB) || Collapsed.second->hasFunction(OB))
687       OB += "(";
688 
689     OB += (Collapsed.first == ReferenceKind::LValue ? "&" : "&&");
690   }
691   void printRight(OutputBuffer &OB) const override {
692     if (Printing)
693       return;
694     ScopedOverride<bool> SavePrinting(Printing, true);
695     std::pair<ReferenceKind, const Node *> Collapsed = collapse(OB);
696     if (!Collapsed.second)
697       return;
698     if (Collapsed.second->hasArray(OB) || Collapsed.second->hasFunction(OB))
699       OB += ")";
700     Collapsed.second->printRight(OB);
701   }
702 };
703 
704 class PointerToMemberType final : public Node {
705   const Node *ClassType;
706   const Node *MemberType;
707 
708 public:
709   PointerToMemberType(const Node *ClassType_, const Node *MemberType_)
710       : Node(KPointerToMemberType, MemberType_->RHSComponentCache),
711         ClassType(ClassType_), MemberType(MemberType_) {}
712 
713   template<typename Fn> void match(Fn F) const { F(ClassType, MemberType); }
714 
715   bool hasRHSComponentSlow(OutputBuffer &OB) const override {
716     return MemberType->hasRHSComponent(OB);
717   }
718 
719   void printLeft(OutputBuffer &OB) const override {
720     MemberType->printLeft(OB);
721     if (MemberType->hasArray(OB) || MemberType->hasFunction(OB))
722       OB += "(";
723     else
724       OB += " ";
725     ClassType->print(OB);
726     OB += "::*";
727   }
728 
729   void printRight(OutputBuffer &OB) const override {
730     if (MemberType->hasArray(OB) || MemberType->hasFunction(OB))
731       OB += ")";
732     MemberType->printRight(OB);
733   }
734 };
735 
736 class ArrayType final : public Node {
737   const Node *Base;
738   Node *Dimension;
739 
740 public:
741   ArrayType(const Node *Base_, Node *Dimension_)
742       : Node(KArrayType,
743              /*RHSComponentCache=*/Cache::Yes,
744              /*ArrayCache=*/Cache::Yes),
745         Base(Base_), Dimension(Dimension_) {}
746 
747   template<typename Fn> void match(Fn F) const { F(Base, Dimension); }
748 
749   bool hasRHSComponentSlow(OutputBuffer &) const override { return true; }
750   bool hasArraySlow(OutputBuffer &) const override { return true; }
751 
752   void printLeft(OutputBuffer &OB) const override { Base->printLeft(OB); }
753 
754   void printRight(OutputBuffer &OB) const override {
755     if (OB.back() != ']')
756       OB += " ";
757     OB += "[";
758     if (Dimension)
759       Dimension->print(OB);
760     OB += "]";
761     Base->printRight(OB);
762   }
763 };
764 
765 class FunctionType final : public Node {
766   const Node *Ret;
767   NodeArray Params;
768   Qualifiers CVQuals;
769   FunctionRefQual RefQual;
770   const Node *ExceptionSpec;
771 
772 public:
773   FunctionType(const Node *Ret_, NodeArray Params_, Qualifiers CVQuals_,
774                FunctionRefQual RefQual_, const Node *ExceptionSpec_)
775       : Node(KFunctionType,
776              /*RHSComponentCache=*/Cache::Yes, /*ArrayCache=*/Cache::No,
777              /*FunctionCache=*/Cache::Yes),
778         Ret(Ret_), Params(Params_), CVQuals(CVQuals_), RefQual(RefQual_),
779         ExceptionSpec(ExceptionSpec_) {}
780 
781   template<typename Fn> void match(Fn F) const {
782     F(Ret, Params, CVQuals, RefQual, ExceptionSpec);
783   }
784 
785   bool hasRHSComponentSlow(OutputBuffer &) const override { return true; }
786   bool hasFunctionSlow(OutputBuffer &) const override { return true; }
787 
788   // Handle C++'s ... quirky decl grammar by using the left & right
789   // distinction. Consider:
790   //   int (*f(float))(char) {}
791   // f is a function that takes a float and returns a pointer to a function
792   // that takes a char and returns an int. If we're trying to print f, start
793   // by printing out the return types's left, then print our parameters, then
794   // finally print right of the return type.
795   void printLeft(OutputBuffer &OB) const override {
796     Ret->printLeft(OB);
797     OB += " ";
798   }
799 
800   void printRight(OutputBuffer &OB) const override {
801     OB.printOpen();
802     Params.printWithComma(OB);
803     OB.printClose();
804     Ret->printRight(OB);
805 
806     if (CVQuals & QualConst)
807       OB += " const";
808     if (CVQuals & QualVolatile)
809       OB += " volatile";
810     if (CVQuals & QualRestrict)
811       OB += " restrict";
812 
813     if (RefQual == FrefQualLValue)
814       OB += " &";
815     else if (RefQual == FrefQualRValue)
816       OB += " &&";
817 
818     if (ExceptionSpec != nullptr) {
819       OB += ' ';
820       ExceptionSpec->print(OB);
821     }
822   }
823 };
824 
825 class NoexceptSpec : public Node {
826   const Node *E;
827 public:
828   NoexceptSpec(const Node *E_) : Node(KNoexceptSpec), E(E_) {}
829 
830   template<typename Fn> void match(Fn F) const { F(E); }
831 
832   void printLeft(OutputBuffer &OB) const override {
833     OB += "noexcept";
834     OB.printOpen();
835     E->printAsOperand(OB);
836     OB.printClose();
837   }
838 };
839 
840 class DynamicExceptionSpec : public Node {
841   NodeArray Types;
842 public:
843   DynamicExceptionSpec(NodeArray Types_)
844       : Node(KDynamicExceptionSpec), Types(Types_) {}
845 
846   template<typename Fn> void match(Fn F) const { F(Types); }
847 
848   void printLeft(OutputBuffer &OB) const override {
849     OB += "throw";
850     OB.printOpen();
851     Types.printWithComma(OB);
852     OB.printClose();
853   }
854 };
855 
856 class FunctionEncoding final : public Node {
857   const Node *Ret;
858   const Node *Name;
859   NodeArray Params;
860   const Node *Attrs;
861   Qualifiers CVQuals;
862   FunctionRefQual RefQual;
863 
864 public:
865   FunctionEncoding(const Node *Ret_, const Node *Name_, NodeArray Params_,
866                    const Node *Attrs_, Qualifiers CVQuals_,
867                    FunctionRefQual RefQual_)
868       : Node(KFunctionEncoding,
869              /*RHSComponentCache=*/Cache::Yes, /*ArrayCache=*/Cache::No,
870              /*FunctionCache=*/Cache::Yes),
871         Ret(Ret_), Name(Name_), Params(Params_), Attrs(Attrs_),
872         CVQuals(CVQuals_), RefQual(RefQual_) {}
873 
874   template<typename Fn> void match(Fn F) const {
875     F(Ret, Name, Params, Attrs, CVQuals, RefQual);
876   }
877 
878   Qualifiers getCVQuals() const { return CVQuals; }
879   FunctionRefQual getRefQual() const { return RefQual; }
880   NodeArray getParams() const { return Params; }
881   const Node *getReturnType() const { return Ret; }
882 
883   bool hasRHSComponentSlow(OutputBuffer &) const override { return true; }
884   bool hasFunctionSlow(OutputBuffer &) const override { return true; }
885 
886   const Node *getName() const { return Name; }
887 
888   void printLeft(OutputBuffer &OB) const override {
889     if (Ret) {
890       Ret->printLeft(OB);
891       if (!Ret->hasRHSComponent(OB))
892         OB += " ";
893     }
894     Name->print(OB);
895   }
896 
897   void printRight(OutputBuffer &OB) const override {
898     OB.printOpen();
899     Params.printWithComma(OB);
900     OB.printClose();
901     if (Ret)
902       Ret->printRight(OB);
903 
904     if (CVQuals & QualConst)
905       OB += " const";
906     if (CVQuals & QualVolatile)
907       OB += " volatile";
908     if (CVQuals & QualRestrict)
909       OB += " restrict";
910 
911     if (RefQual == FrefQualLValue)
912       OB += " &";
913     else if (RefQual == FrefQualRValue)
914       OB += " &&";
915 
916     if (Attrs != nullptr)
917       Attrs->print(OB);
918   }
919 };
920 
921 class LiteralOperator : public Node {
922   const Node *OpName;
923 
924 public:
925   LiteralOperator(const Node *OpName_)
926       : Node(KLiteralOperator), OpName(OpName_) {}
927 
928   template<typename Fn> void match(Fn F) const { F(OpName); }
929 
930   void printLeft(OutputBuffer &OB) const override {
931     OB += "operator\"\" ";
932     OpName->print(OB);
933   }
934 };
935 
936 class SpecialName final : public Node {
937   const StringView Special;
938   const Node *Child;
939 
940 public:
941   SpecialName(StringView Special_, const Node *Child_)
942       : Node(KSpecialName), Special(Special_), Child(Child_) {}
943 
944   template<typename Fn> void match(Fn F) const { F(Special, Child); }
945 
946   void printLeft(OutputBuffer &OB) const override {
947     OB += Special;
948     Child->print(OB);
949   }
950 };
951 
952 class CtorVtableSpecialName final : public Node {
953   const Node *FirstType;
954   const Node *SecondType;
955 
956 public:
957   CtorVtableSpecialName(const Node *FirstType_, const Node *SecondType_)
958       : Node(KCtorVtableSpecialName),
959         FirstType(FirstType_), SecondType(SecondType_) {}
960 
961   template<typename Fn> void match(Fn F) const { F(FirstType, SecondType); }
962 
963   void printLeft(OutputBuffer &OB) const override {
964     OB += "construction vtable for ";
965     FirstType->print(OB);
966     OB += "-in-";
967     SecondType->print(OB);
968   }
969 };
970 
971 struct NestedName : Node {
972   Node *Qual;
973   Node *Name;
974 
975   NestedName(Node *Qual_, Node *Name_)
976       : Node(KNestedName), Qual(Qual_), Name(Name_) {}
977 
978   template<typename Fn> void match(Fn F) const { F(Qual, Name); }
979 
980   StringView getBaseName() const override { return Name->getBaseName(); }
981 
982   void printLeft(OutputBuffer &OB) const override {
983     Qual->print(OB);
984     OB += "::";
985     Name->print(OB);
986   }
987 };
988 
989 struct ModuleName : Node {
990   ModuleName *Parent;
991   Node *Name;
992   bool IsPartition;
993 
994   ModuleName(ModuleName *Parent_, Node *Name_, bool IsPartition_ = false)
995       : Node(KModuleName), Parent(Parent_), Name(Name_),
996         IsPartition(IsPartition_) {}
997 
998   template <typename Fn> void match(Fn F) const {
999     F(Parent, Name, IsPartition);
1000   }
1001 
1002   void printLeft(OutputBuffer &OB) const override {
1003     if (Parent)
1004       Parent->print(OB);
1005     if (Parent || IsPartition)
1006       OB += IsPartition ? ':' : '.';
1007     Name->print(OB);
1008   }
1009 };
1010 
1011 struct ModuleEntity : Node {
1012   ModuleName *Module;
1013   Node *Name;
1014 
1015   ModuleEntity(ModuleName *Module_, Node *Name_)
1016       : Node(KModuleEntity), Module(Module_), Name(Name_) {}
1017 
1018   template <typename Fn> void match(Fn F) const { F(Module, Name); }
1019 
1020   StringView getBaseName() const override { return Name->getBaseName(); }
1021 
1022   void printLeft(OutputBuffer &OB) const override {
1023     Name->print(OB);
1024     OB += '@';
1025     Module->print(OB);
1026   }
1027 };
1028 
1029 struct LocalName : Node {
1030   Node *Encoding;
1031   Node *Entity;
1032 
1033   LocalName(Node *Encoding_, Node *Entity_)
1034       : Node(KLocalName), Encoding(Encoding_), Entity(Entity_) {}
1035 
1036   template<typename Fn> void match(Fn F) const { F(Encoding, Entity); }
1037 
1038   void printLeft(OutputBuffer &OB) const override {
1039     Encoding->print(OB);
1040     OB += "::";
1041     Entity->print(OB);
1042   }
1043 };
1044 
1045 class QualifiedName final : public Node {
1046   // qualifier::name
1047   const Node *Qualifier;
1048   const Node *Name;
1049 
1050 public:
1051   QualifiedName(const Node *Qualifier_, const Node *Name_)
1052       : Node(KQualifiedName), Qualifier(Qualifier_), Name(Name_) {}
1053 
1054   template<typename Fn> void match(Fn F) const { F(Qualifier, Name); }
1055 
1056   StringView getBaseName() const override { return Name->getBaseName(); }
1057 
1058   void printLeft(OutputBuffer &OB) const override {
1059     Qualifier->print(OB);
1060     OB += "::";
1061     Name->print(OB);
1062   }
1063 };
1064 
1065 class VectorType final : public Node {
1066   const Node *BaseType;
1067   const Node *Dimension;
1068 
1069 public:
1070   VectorType(const Node *BaseType_, const Node *Dimension_)
1071       : Node(KVectorType), BaseType(BaseType_), Dimension(Dimension_) {}
1072 
1073   template<typename Fn> void match(Fn F) const { F(BaseType, Dimension); }
1074 
1075   void printLeft(OutputBuffer &OB) const override {
1076     BaseType->print(OB);
1077     OB += " vector[";
1078     if (Dimension)
1079       Dimension->print(OB);
1080     OB += "]";
1081   }
1082 };
1083 
1084 class PixelVectorType final : public Node {
1085   const Node *Dimension;
1086 
1087 public:
1088   PixelVectorType(const Node *Dimension_)
1089       : Node(KPixelVectorType), Dimension(Dimension_) {}
1090 
1091   template<typename Fn> void match(Fn F) const { F(Dimension); }
1092 
1093   void printLeft(OutputBuffer &OB) const override {
1094     // FIXME: This should demangle as "vector pixel".
1095     OB += "pixel vector[";
1096     Dimension->print(OB);
1097     OB += "]";
1098   }
1099 };
1100 
1101 class BinaryFPType final : public Node {
1102   const Node *Dimension;
1103 
1104 public:
1105   BinaryFPType(const Node *Dimension_)
1106       : Node(KBinaryFPType), Dimension(Dimension_) {}
1107 
1108   template<typename Fn> void match(Fn F) const { F(Dimension); }
1109 
1110   void printLeft(OutputBuffer &OB) const override {
1111     OB += "_Float";
1112     Dimension->print(OB);
1113   }
1114 };
1115 
1116 enum class TemplateParamKind { Type, NonType, Template };
1117 
1118 /// An invented name for a template parameter for which we don't have a
1119 /// corresponding template argument.
1120 ///
1121 /// This node is created when parsing the <lambda-sig> for a lambda with
1122 /// explicit template arguments, which might be referenced in the parameter
1123 /// types appearing later in the <lambda-sig>.
1124 class SyntheticTemplateParamName final : public Node {
1125   TemplateParamKind Kind;
1126   unsigned Index;
1127 
1128 public:
1129   SyntheticTemplateParamName(TemplateParamKind Kind_, unsigned Index_)
1130       : Node(KSyntheticTemplateParamName), Kind(Kind_), Index(Index_) {}
1131 
1132   template<typename Fn> void match(Fn F) const { F(Kind, Index); }
1133 
1134   void printLeft(OutputBuffer &OB) const override {
1135     switch (Kind) {
1136     case TemplateParamKind::Type:
1137       OB += "$T";
1138       break;
1139     case TemplateParamKind::NonType:
1140       OB += "$N";
1141       break;
1142     case TemplateParamKind::Template:
1143       OB += "$TT";
1144       break;
1145     }
1146     if (Index > 0)
1147       OB << Index - 1;
1148   }
1149 };
1150 
1151 /// A template type parameter declaration, 'typename T'.
1152 class TypeTemplateParamDecl final : public Node {
1153   Node *Name;
1154 
1155 public:
1156   TypeTemplateParamDecl(Node *Name_)
1157       : Node(KTypeTemplateParamDecl, Cache::Yes), Name(Name_) {}
1158 
1159   template<typename Fn> void match(Fn F) const { F(Name); }
1160 
1161   void printLeft(OutputBuffer &OB) const override { OB += "typename "; }
1162 
1163   void printRight(OutputBuffer &OB) const override { Name->print(OB); }
1164 };
1165 
1166 /// A non-type template parameter declaration, 'int N'.
1167 class NonTypeTemplateParamDecl final : public Node {
1168   Node *Name;
1169   Node *Type;
1170 
1171 public:
1172   NonTypeTemplateParamDecl(Node *Name_, Node *Type_)
1173       : Node(KNonTypeTemplateParamDecl, Cache::Yes), Name(Name_), Type(Type_) {}
1174 
1175   template<typename Fn> void match(Fn F) const { F(Name, Type); }
1176 
1177   void printLeft(OutputBuffer &OB) const override {
1178     Type->printLeft(OB);
1179     if (!Type->hasRHSComponent(OB))
1180       OB += " ";
1181   }
1182 
1183   void printRight(OutputBuffer &OB) const override {
1184     Name->print(OB);
1185     Type->printRight(OB);
1186   }
1187 };
1188 
1189 /// A template template parameter declaration,
1190 /// 'template<typename T> typename N'.
1191 class TemplateTemplateParamDecl final : public Node {
1192   Node *Name;
1193   NodeArray Params;
1194 
1195 public:
1196   TemplateTemplateParamDecl(Node *Name_, NodeArray Params_)
1197       : Node(KTemplateTemplateParamDecl, Cache::Yes), Name(Name_),
1198         Params(Params_) {}
1199 
1200   template<typename Fn> void match(Fn F) const { F(Name, Params); }
1201 
1202   void printLeft(OutputBuffer &OB) const override {
1203     ScopedOverride<unsigned> LT(OB.GtIsGt, 0);
1204     OB += "template<";
1205     Params.printWithComma(OB);
1206     OB += "> typename ";
1207   }
1208 
1209   void printRight(OutputBuffer &OB) const override { Name->print(OB); }
1210 };
1211 
1212 /// A template parameter pack declaration, 'typename ...T'.
1213 class TemplateParamPackDecl final : public Node {
1214   Node *Param;
1215 
1216 public:
1217   TemplateParamPackDecl(Node *Param_)
1218       : Node(KTemplateParamPackDecl, Cache::Yes), Param(Param_) {}
1219 
1220   template<typename Fn> void match(Fn F) const { F(Param); }
1221 
1222   void printLeft(OutputBuffer &OB) const override {
1223     Param->printLeft(OB);
1224     OB += "...";
1225   }
1226 
1227   void printRight(OutputBuffer &OB) const override { Param->printRight(OB); }
1228 };
1229 
1230 /// An unexpanded parameter pack (either in the expression or type context). If
1231 /// this AST is correct, this node will have a ParameterPackExpansion node above
1232 /// it.
1233 ///
1234 /// This node is created when some <template-args> are found that apply to an
1235 /// <encoding>, and is stored in the TemplateParams table. In order for this to
1236 /// appear in the final AST, it has to referenced via a <template-param> (ie,
1237 /// T_).
1238 class ParameterPack final : public Node {
1239   NodeArray Data;
1240 
1241   // Setup OutputBuffer for a pack expansion, unless we're already expanding
1242   // one.
1243   void initializePackExpansion(OutputBuffer &OB) const {
1244     if (OB.CurrentPackMax == std::numeric_limits<unsigned>::max()) {
1245       OB.CurrentPackMax = static_cast<unsigned>(Data.size());
1246       OB.CurrentPackIndex = 0;
1247     }
1248   }
1249 
1250 public:
1251   ParameterPack(NodeArray Data_) : Node(KParameterPack), Data(Data_) {
1252     ArrayCache = FunctionCache = RHSComponentCache = Cache::Unknown;
1253     if (std::all_of(Data.begin(), Data.end(), [](Node* P) {
1254           return P->ArrayCache == Cache::No;
1255         }))
1256       ArrayCache = Cache::No;
1257     if (std::all_of(Data.begin(), Data.end(), [](Node* P) {
1258           return P->FunctionCache == Cache::No;
1259         }))
1260       FunctionCache = Cache::No;
1261     if (std::all_of(Data.begin(), Data.end(), [](Node* P) {
1262           return P->RHSComponentCache == Cache::No;
1263         }))
1264       RHSComponentCache = Cache::No;
1265   }
1266 
1267   template<typename Fn> void match(Fn F) const { F(Data); }
1268 
1269   bool hasRHSComponentSlow(OutputBuffer &OB) const override {
1270     initializePackExpansion(OB);
1271     size_t Idx = OB.CurrentPackIndex;
1272     return Idx < Data.size() && Data[Idx]->hasRHSComponent(OB);
1273   }
1274   bool hasArraySlow(OutputBuffer &OB) const override {
1275     initializePackExpansion(OB);
1276     size_t Idx = OB.CurrentPackIndex;
1277     return Idx < Data.size() && Data[Idx]->hasArray(OB);
1278   }
1279   bool hasFunctionSlow(OutputBuffer &OB) const override {
1280     initializePackExpansion(OB);
1281     size_t Idx = OB.CurrentPackIndex;
1282     return Idx < Data.size() && Data[Idx]->hasFunction(OB);
1283   }
1284   const Node *getSyntaxNode(OutputBuffer &OB) const override {
1285     initializePackExpansion(OB);
1286     size_t Idx = OB.CurrentPackIndex;
1287     return Idx < Data.size() ? Data[Idx]->getSyntaxNode(OB) : this;
1288   }
1289 
1290   void printLeft(OutputBuffer &OB) const override {
1291     initializePackExpansion(OB);
1292     size_t Idx = OB.CurrentPackIndex;
1293     if (Idx < Data.size())
1294       Data[Idx]->printLeft(OB);
1295   }
1296   void printRight(OutputBuffer &OB) const override {
1297     initializePackExpansion(OB);
1298     size_t Idx = OB.CurrentPackIndex;
1299     if (Idx < Data.size())
1300       Data[Idx]->printRight(OB);
1301   }
1302 };
1303 
1304 /// A variadic template argument. This node represents an occurrence of
1305 /// J<something>E in some <template-args>. It isn't itself unexpanded, unless
1306 /// one of it's Elements is. The parser inserts a ParameterPack into the
1307 /// TemplateParams table if the <template-args> this pack belongs to apply to an
1308 /// <encoding>.
1309 class TemplateArgumentPack final : public Node {
1310   NodeArray Elements;
1311 public:
1312   TemplateArgumentPack(NodeArray Elements_)
1313       : Node(KTemplateArgumentPack), Elements(Elements_) {}
1314 
1315   template<typename Fn> void match(Fn F) const { F(Elements); }
1316 
1317   NodeArray getElements() const { return Elements; }
1318 
1319   void printLeft(OutputBuffer &OB) const override {
1320     Elements.printWithComma(OB);
1321   }
1322 };
1323 
1324 /// A pack expansion. Below this node, there are some unexpanded ParameterPacks
1325 /// which each have Child->ParameterPackSize elements.
1326 class ParameterPackExpansion final : public Node {
1327   const Node *Child;
1328 
1329 public:
1330   ParameterPackExpansion(const Node *Child_)
1331       : Node(KParameterPackExpansion), Child(Child_) {}
1332 
1333   template<typename Fn> void match(Fn F) const { F(Child); }
1334 
1335   const Node *getChild() const { return Child; }
1336 
1337   void printLeft(OutputBuffer &OB) const override {
1338     constexpr unsigned Max = std::numeric_limits<unsigned>::max();
1339     ScopedOverride<unsigned> SavePackIdx(OB.CurrentPackIndex, Max);
1340     ScopedOverride<unsigned> SavePackMax(OB.CurrentPackMax, Max);
1341     size_t StreamPos = OB.getCurrentPosition();
1342 
1343     // Print the first element in the pack. If Child contains a ParameterPack,
1344     // it will set up S.CurrentPackMax and print the first element.
1345     Child->print(OB);
1346 
1347     // No ParameterPack was found in Child. This can occur if we've found a pack
1348     // expansion on a <function-param>.
1349     if (OB.CurrentPackMax == Max) {
1350       OB += "...";
1351       return;
1352     }
1353 
1354     // We found a ParameterPack, but it has no elements. Erase whatever we may
1355     // of printed.
1356     if (OB.CurrentPackMax == 0) {
1357       OB.setCurrentPosition(StreamPos);
1358       return;
1359     }
1360 
1361     // Else, iterate through the rest of the elements in the pack.
1362     for (unsigned I = 1, E = OB.CurrentPackMax; I < E; ++I) {
1363       OB += ", ";
1364       OB.CurrentPackIndex = I;
1365       Child->print(OB);
1366     }
1367   }
1368 };
1369 
1370 class TemplateArgs final : public Node {
1371   NodeArray Params;
1372 
1373 public:
1374   TemplateArgs(NodeArray Params_) : Node(KTemplateArgs), Params(Params_) {}
1375 
1376   template<typename Fn> void match(Fn F) const { F(Params); }
1377 
1378   NodeArray getParams() { return Params; }
1379 
1380   void printLeft(OutputBuffer &OB) const override {
1381     ScopedOverride<unsigned> LT(OB.GtIsGt, 0);
1382     OB += "<";
1383     Params.printWithComma(OB);
1384     if (OB.back() == '>')
1385       OB += " ";
1386     OB += ">";
1387   }
1388 };
1389 
1390 /// A forward-reference to a template argument that was not known at the point
1391 /// where the template parameter name was parsed in a mangling.
1392 ///
1393 /// This is created when demangling the name of a specialization of a
1394 /// conversion function template:
1395 ///
1396 /// \code
1397 /// struct A {
1398 ///   template<typename T> operator T*();
1399 /// };
1400 /// \endcode
1401 ///
1402 /// When demangling a specialization of the conversion function template, we
1403 /// encounter the name of the template (including the \c T) before we reach
1404 /// the template argument list, so we cannot substitute the parameter name
1405 /// for the corresponding argument while parsing. Instead, we create a
1406 /// \c ForwardTemplateReference node that is resolved after we parse the
1407 /// template arguments.
1408 struct ForwardTemplateReference : Node {
1409   size_t Index;
1410   Node *Ref = nullptr;
1411 
1412   // If we're currently printing this node. It is possible (though invalid) for
1413   // a forward template reference to refer to itself via a substitution. This
1414   // creates a cyclic AST, which will stack overflow printing. To fix this, bail
1415   // out if more than one print* function is active.
1416   mutable bool Printing = false;
1417 
1418   ForwardTemplateReference(size_t Index_)
1419       : Node(KForwardTemplateReference, Cache::Unknown, Cache::Unknown,
1420              Cache::Unknown),
1421         Index(Index_) {}
1422 
1423   // We don't provide a matcher for these, because the value of the node is
1424   // not determined by its construction parameters, and it generally needs
1425   // special handling.
1426   template<typename Fn> void match(Fn F) const = delete;
1427 
1428   bool hasRHSComponentSlow(OutputBuffer &OB) const override {
1429     if (Printing)
1430       return false;
1431     ScopedOverride<bool> SavePrinting(Printing, true);
1432     return Ref->hasRHSComponent(OB);
1433   }
1434   bool hasArraySlow(OutputBuffer &OB) const override {
1435     if (Printing)
1436       return false;
1437     ScopedOverride<bool> SavePrinting(Printing, true);
1438     return Ref->hasArray(OB);
1439   }
1440   bool hasFunctionSlow(OutputBuffer &OB) const override {
1441     if (Printing)
1442       return false;
1443     ScopedOverride<bool> SavePrinting(Printing, true);
1444     return Ref->hasFunction(OB);
1445   }
1446   const Node *getSyntaxNode(OutputBuffer &OB) const override {
1447     if (Printing)
1448       return this;
1449     ScopedOverride<bool> SavePrinting(Printing, true);
1450     return Ref->getSyntaxNode(OB);
1451   }
1452 
1453   void printLeft(OutputBuffer &OB) const override {
1454     if (Printing)
1455       return;
1456     ScopedOverride<bool> SavePrinting(Printing, true);
1457     Ref->printLeft(OB);
1458   }
1459   void printRight(OutputBuffer &OB) const override {
1460     if (Printing)
1461       return;
1462     ScopedOverride<bool> SavePrinting(Printing, true);
1463     Ref->printRight(OB);
1464   }
1465 };
1466 
1467 struct NameWithTemplateArgs : Node {
1468   // name<template_args>
1469   Node *Name;
1470   Node *TemplateArgs;
1471 
1472   NameWithTemplateArgs(Node *Name_, Node *TemplateArgs_)
1473       : Node(KNameWithTemplateArgs), Name(Name_), TemplateArgs(TemplateArgs_) {}
1474 
1475   template<typename Fn> void match(Fn F) const { F(Name, TemplateArgs); }
1476 
1477   StringView getBaseName() const override { return Name->getBaseName(); }
1478 
1479   void printLeft(OutputBuffer &OB) const override {
1480     Name->print(OB);
1481     TemplateArgs->print(OB);
1482   }
1483 };
1484 
1485 class GlobalQualifiedName final : public Node {
1486   Node *Child;
1487 
1488 public:
1489   GlobalQualifiedName(Node* Child_)
1490       : Node(KGlobalQualifiedName), Child(Child_) {}
1491 
1492   template<typename Fn> void match(Fn F) const { F(Child); }
1493 
1494   StringView getBaseName() const override { return Child->getBaseName(); }
1495 
1496   void printLeft(OutputBuffer &OB) const override {
1497     OB += "::";
1498     Child->print(OB);
1499   }
1500 };
1501 
1502 enum class SpecialSubKind {
1503   allocator,
1504   basic_string,
1505   string,
1506   istream,
1507   ostream,
1508   iostream,
1509 };
1510 
1511 class ExpandedSpecialSubstitution final : public Node {
1512   SpecialSubKind SSK;
1513 
1514 public:
1515   ExpandedSpecialSubstitution(SpecialSubKind SSK_)
1516       : Node(KExpandedSpecialSubstitution), SSK(SSK_) {}
1517 
1518   template<typename Fn> void match(Fn F) const { F(SSK); }
1519 
1520   StringView getBaseName() const override {
1521     switch (SSK) {
1522     case SpecialSubKind::allocator:
1523       return StringView("allocator");
1524     case SpecialSubKind::basic_string:
1525       return StringView("basic_string");
1526     case SpecialSubKind::string:
1527       return StringView("basic_string");
1528     case SpecialSubKind::istream:
1529       return StringView("basic_istream");
1530     case SpecialSubKind::ostream:
1531       return StringView("basic_ostream");
1532     case SpecialSubKind::iostream:
1533       return StringView("basic_iostream");
1534     }
1535     DEMANGLE_UNREACHABLE;
1536   }
1537 
1538   void printLeft(OutputBuffer &OB) const override {
1539     switch (SSK) {
1540     case SpecialSubKind::allocator:
1541       OB += "std::allocator";
1542       break;
1543     case SpecialSubKind::basic_string:
1544       OB += "std::basic_string";
1545       break;
1546     case SpecialSubKind::string:
1547       OB += "std::basic_string<char, std::char_traits<char>, "
1548             "std::allocator<char> >";
1549       break;
1550     case SpecialSubKind::istream:
1551       OB += "std::basic_istream<char, std::char_traits<char> >";
1552       break;
1553     case SpecialSubKind::ostream:
1554       OB += "std::basic_ostream<char, std::char_traits<char> >";
1555       break;
1556     case SpecialSubKind::iostream:
1557       OB += "std::basic_iostream<char, std::char_traits<char> >";
1558       break;
1559     }
1560   }
1561 };
1562 
1563 class SpecialSubstitution final : public Node {
1564 public:
1565   SpecialSubKind SSK;
1566 
1567   SpecialSubstitution(SpecialSubKind SSK_)
1568       : Node(KSpecialSubstitution), SSK(SSK_) {}
1569 
1570   template<typename Fn> void match(Fn F) const { F(SSK); }
1571 
1572   StringView getBaseName() const override {
1573     switch (SSK) {
1574     case SpecialSubKind::allocator:
1575       return StringView("allocator");
1576     case SpecialSubKind::basic_string:
1577       return StringView("basic_string");
1578     case SpecialSubKind::string:
1579       return StringView("string");
1580     case SpecialSubKind::istream:
1581       return StringView("istream");
1582     case SpecialSubKind::ostream:
1583       return StringView("ostream");
1584     case SpecialSubKind::iostream:
1585       return StringView("iostream");
1586     }
1587     DEMANGLE_UNREACHABLE;
1588   }
1589 
1590   void printLeft(OutputBuffer &OB) const override {
1591     switch (SSK) {
1592     case SpecialSubKind::allocator:
1593       OB += "std::allocator";
1594       break;
1595     case SpecialSubKind::basic_string:
1596       OB += "std::basic_string";
1597       break;
1598     case SpecialSubKind::string:
1599       OB += "std::string";
1600       break;
1601     case SpecialSubKind::istream:
1602       OB += "std::istream";
1603       break;
1604     case SpecialSubKind::ostream:
1605       OB += "std::ostream";
1606       break;
1607     case SpecialSubKind::iostream:
1608       OB += "std::iostream";
1609       break;
1610     }
1611   }
1612 };
1613 
1614 class CtorDtorName final : public Node {
1615   const Node *Basename;
1616   const bool IsDtor;
1617   const int Variant;
1618 
1619 public:
1620   CtorDtorName(const Node *Basename_, bool IsDtor_, int Variant_)
1621       : Node(KCtorDtorName), Basename(Basename_), IsDtor(IsDtor_),
1622         Variant(Variant_) {}
1623 
1624   template<typename Fn> void match(Fn F) const { F(Basename, IsDtor, Variant); }
1625 
1626   void printLeft(OutputBuffer &OB) const override {
1627     if (IsDtor)
1628       OB += "~";
1629     OB += Basename->getBaseName();
1630   }
1631 };
1632 
1633 class DtorName : public Node {
1634   const Node *Base;
1635 
1636 public:
1637   DtorName(const Node *Base_) : Node(KDtorName), Base(Base_) {}
1638 
1639   template<typename Fn> void match(Fn F) const { F(Base); }
1640 
1641   void printLeft(OutputBuffer &OB) const override {
1642     OB += "~";
1643     Base->printLeft(OB);
1644   }
1645 };
1646 
1647 class UnnamedTypeName : public Node {
1648   const StringView Count;
1649 
1650 public:
1651   UnnamedTypeName(StringView Count_) : Node(KUnnamedTypeName), Count(Count_) {}
1652 
1653   template<typename Fn> void match(Fn F) const { F(Count); }
1654 
1655   void printLeft(OutputBuffer &OB) const override {
1656     OB += "'unnamed";
1657     OB += Count;
1658     OB += "\'";
1659   }
1660 };
1661 
1662 class ClosureTypeName : public Node {
1663   NodeArray TemplateParams;
1664   NodeArray Params;
1665   StringView Count;
1666 
1667 public:
1668   ClosureTypeName(NodeArray TemplateParams_, NodeArray Params_,
1669                   StringView Count_)
1670       : Node(KClosureTypeName), TemplateParams(TemplateParams_),
1671         Params(Params_), Count(Count_) {}
1672 
1673   template<typename Fn> void match(Fn F) const {
1674     F(TemplateParams, Params, Count);
1675   }
1676 
1677   void printDeclarator(OutputBuffer &OB) const {
1678     if (!TemplateParams.empty()) {
1679       ScopedOverride<unsigned> LT(OB.GtIsGt, 0);
1680       OB += "<";
1681       TemplateParams.printWithComma(OB);
1682       OB += ">";
1683     }
1684     OB.printOpen();
1685     Params.printWithComma(OB);
1686     OB.printClose();
1687   }
1688 
1689   void printLeft(OutputBuffer &OB) const override {
1690     OB += "\'lambda";
1691     OB += Count;
1692     OB += "\'";
1693     printDeclarator(OB);
1694   }
1695 };
1696 
1697 class StructuredBindingName : public Node {
1698   NodeArray Bindings;
1699 public:
1700   StructuredBindingName(NodeArray Bindings_)
1701       : Node(KStructuredBindingName), Bindings(Bindings_) {}
1702 
1703   template<typename Fn> void match(Fn F) const { F(Bindings); }
1704 
1705   void printLeft(OutputBuffer &OB) const override {
1706     OB.printOpen('[');
1707     Bindings.printWithComma(OB);
1708     OB.printClose(']');
1709   }
1710 };
1711 
1712 // -- Expression Nodes --
1713 
1714 class BinaryExpr : public Node {
1715   const Node *LHS;
1716   const StringView InfixOperator;
1717   const Node *RHS;
1718 
1719 public:
1720   BinaryExpr(const Node *LHS_, StringView InfixOperator_, const Node *RHS_,
1721              Prec Prec_)
1722       : Node(KBinaryExpr, Prec_), LHS(LHS_), InfixOperator(InfixOperator_),
1723         RHS(RHS_) {}
1724 
1725   template <typename Fn> void match(Fn F) const {
1726     F(LHS, InfixOperator, RHS, getPrecedence());
1727   }
1728 
1729   void printLeft(OutputBuffer &OB) const override {
1730     bool ParenAll = OB.isGtInsideTemplateArgs() &&
1731                     (InfixOperator == ">" || InfixOperator == ">>");
1732     if (ParenAll)
1733       OB.printOpen();
1734     // Assignment is right associative, with special LHS precedence.
1735     bool IsAssign = getPrecedence() == Prec::Assign;
1736     LHS->printAsOperand(OB, IsAssign ? Prec::OrIf : getPrecedence(), !IsAssign);
1737     // No space before comma operator
1738     if (!(InfixOperator == ","))
1739       OB += " ";
1740     OB += InfixOperator;
1741     OB += " ";
1742     RHS->printAsOperand(OB, getPrecedence(), IsAssign);
1743     if (ParenAll)
1744       OB.printClose();
1745   }
1746 };
1747 
1748 class ArraySubscriptExpr : public Node {
1749   const Node *Op1;
1750   const Node *Op2;
1751 
1752 public:
1753   ArraySubscriptExpr(const Node *Op1_, const Node *Op2_, Prec Prec_)
1754       : Node(KArraySubscriptExpr, Prec_), Op1(Op1_), Op2(Op2_) {}
1755 
1756   template <typename Fn> void match(Fn F) const {
1757     F(Op1, Op2, getPrecedence());
1758   }
1759 
1760   void printLeft(OutputBuffer &OB) const override {
1761     Op1->printAsOperand(OB, getPrecedence());
1762     OB.printOpen('[');
1763     Op2->printAsOperand(OB);
1764     OB.printClose(']');
1765   }
1766 };
1767 
1768 class PostfixExpr : public Node {
1769   const Node *Child;
1770   const StringView Operator;
1771 
1772 public:
1773   PostfixExpr(const Node *Child_, StringView Operator_, Prec Prec_)
1774       : Node(KPostfixExpr, Prec_), Child(Child_), Operator(Operator_) {}
1775 
1776   template <typename Fn> void match(Fn F) const {
1777     F(Child, Operator, getPrecedence());
1778   }
1779 
1780   void printLeft(OutputBuffer &OB) const override {
1781     Child->printAsOperand(OB, getPrecedence(), true);
1782     OB += Operator;
1783   }
1784 };
1785 
1786 class ConditionalExpr : public Node {
1787   const Node *Cond;
1788   const Node *Then;
1789   const Node *Else;
1790 
1791 public:
1792   ConditionalExpr(const Node *Cond_, const Node *Then_, const Node *Else_,
1793                   Prec Prec_)
1794       : Node(KConditionalExpr, Prec_), Cond(Cond_), Then(Then_), Else(Else_) {}
1795 
1796   template <typename Fn> void match(Fn F) const {
1797     F(Cond, Then, Else, getPrecedence());
1798   }
1799 
1800   void printLeft(OutputBuffer &OB) const override {
1801     Cond->printAsOperand(OB, getPrecedence());
1802     OB += " ? ";
1803     Then->printAsOperand(OB);
1804     OB += " : ";
1805     Else->printAsOperand(OB, Prec::Assign, true);
1806   }
1807 };
1808 
1809 class MemberExpr : public Node {
1810   const Node *LHS;
1811   const StringView Kind;
1812   const Node *RHS;
1813 
1814 public:
1815   MemberExpr(const Node *LHS_, StringView Kind_, const Node *RHS_, Prec Prec_)
1816       : Node(KMemberExpr, Prec_), LHS(LHS_), Kind(Kind_), RHS(RHS_) {}
1817 
1818   template <typename Fn> void match(Fn F) const {
1819     F(LHS, Kind, RHS, getPrecedence());
1820   }
1821 
1822   void printLeft(OutputBuffer &OB) const override {
1823     LHS->printAsOperand(OB, getPrecedence(), true);
1824     OB += Kind;
1825     RHS->printAsOperand(OB, getPrecedence(), false);
1826   }
1827 };
1828 
1829 class SubobjectExpr : public Node {
1830   const Node *Type;
1831   const Node *SubExpr;
1832   StringView Offset;
1833   NodeArray UnionSelectors;
1834   bool OnePastTheEnd;
1835 
1836 public:
1837   SubobjectExpr(const Node *Type_, const Node *SubExpr_, StringView Offset_,
1838                 NodeArray UnionSelectors_, bool OnePastTheEnd_)
1839       : Node(KSubobjectExpr), Type(Type_), SubExpr(SubExpr_), Offset(Offset_),
1840         UnionSelectors(UnionSelectors_), OnePastTheEnd(OnePastTheEnd_) {}
1841 
1842   template<typename Fn> void match(Fn F) const {
1843     F(Type, SubExpr, Offset, UnionSelectors, OnePastTheEnd);
1844   }
1845 
1846   void printLeft(OutputBuffer &OB) const override {
1847     SubExpr->print(OB);
1848     OB += ".<";
1849     Type->print(OB);
1850     OB += " at offset ";
1851     if (Offset.empty()) {
1852       OB += "0";
1853     } else if (Offset[0] == 'n') {
1854       OB += "-";
1855       OB += Offset.dropFront();
1856     } else {
1857       OB += Offset;
1858     }
1859     OB += ">";
1860   }
1861 };
1862 
1863 class EnclosingExpr : public Node {
1864   const StringView Prefix;
1865   const Node *Infix;
1866   const StringView Postfix;
1867 
1868 public:
1869   EnclosingExpr(StringView Prefix_, const Node *Infix_,
1870                 Prec Prec_ = Prec::Primary)
1871       : Node(KEnclosingExpr, Prec_), Prefix(Prefix_), Infix(Infix_) {}
1872 
1873   template <typename Fn> void match(Fn F) const {
1874     F(Prefix, Infix, getPrecedence());
1875   }
1876 
1877   void printLeft(OutputBuffer &OB) const override {
1878     OB += Prefix;
1879     OB.printOpen();
1880     Infix->print(OB);
1881     OB.printClose();
1882     OB += Postfix;
1883   }
1884 };
1885 
1886 class CastExpr : public Node {
1887   // cast_kind<to>(from)
1888   const StringView CastKind;
1889   const Node *To;
1890   const Node *From;
1891 
1892 public:
1893   CastExpr(StringView CastKind_, const Node *To_, const Node *From_, Prec Prec_)
1894       : Node(KCastExpr, Prec_), CastKind(CastKind_), To(To_), From(From_) {}
1895 
1896   template <typename Fn> void match(Fn F) const {
1897     F(CastKind, To, From, getPrecedence());
1898   }
1899 
1900   void printLeft(OutputBuffer &OB) const override {
1901     OB += CastKind;
1902     {
1903       ScopedOverride<unsigned> LT(OB.GtIsGt, 0);
1904       OB += "<";
1905       To->printLeft(OB);
1906       if (OB.back() == '>')
1907         OB += " ";
1908       OB += ">";
1909     }
1910     OB.printOpen();
1911     From->printAsOperand(OB);
1912     OB.printClose();
1913   }
1914 };
1915 
1916 class SizeofParamPackExpr : public Node {
1917   const Node *Pack;
1918 
1919 public:
1920   SizeofParamPackExpr(const Node *Pack_)
1921       : Node(KSizeofParamPackExpr), Pack(Pack_) {}
1922 
1923   template<typename Fn> void match(Fn F) const { F(Pack); }
1924 
1925   void printLeft(OutputBuffer &OB) const override {
1926     OB += "sizeof...";
1927     OB.printOpen();
1928     ParameterPackExpansion PPE(Pack);
1929     PPE.printLeft(OB);
1930     OB.printClose();
1931   }
1932 };
1933 
1934 class CallExpr : public Node {
1935   const Node *Callee;
1936   NodeArray Args;
1937 
1938 public:
1939   CallExpr(const Node *Callee_, NodeArray Args_, Prec Prec_)
1940       : Node(KCallExpr, Prec_), Callee(Callee_), Args(Args_) {}
1941 
1942   template <typename Fn> void match(Fn F) const {
1943     F(Callee, Args, getPrecedence());
1944   }
1945 
1946   void printLeft(OutputBuffer &OB) const override {
1947     Callee->print(OB);
1948     OB.printOpen();
1949     Args.printWithComma(OB);
1950     OB.printClose();
1951   }
1952 };
1953 
1954 class NewExpr : public Node {
1955   // new (expr_list) type(init_list)
1956   NodeArray ExprList;
1957   Node *Type;
1958   NodeArray InitList;
1959   bool IsGlobal; // ::operator new ?
1960   bool IsArray;  // new[] ?
1961 public:
1962   NewExpr(NodeArray ExprList_, Node *Type_, NodeArray InitList_, bool IsGlobal_,
1963           bool IsArray_, Prec Prec_)
1964       : Node(KNewExpr, Prec_), ExprList(ExprList_), Type(Type_),
1965         InitList(InitList_), IsGlobal(IsGlobal_), IsArray(IsArray_) {}
1966 
1967   template<typename Fn> void match(Fn F) const {
1968     F(ExprList, Type, InitList, IsGlobal, IsArray, getPrecedence());
1969   }
1970 
1971   void printLeft(OutputBuffer &OB) const override {
1972     if (IsGlobal)
1973       OB += "::";
1974     OB += "new";
1975     if (IsArray)
1976       OB += "[]";
1977     if (!ExprList.empty()) {
1978       OB.printOpen();
1979       ExprList.printWithComma(OB);
1980       OB.printClose();
1981     }
1982     OB += " ";
1983     Type->print(OB);
1984     if (!InitList.empty()) {
1985       OB.printOpen();
1986       InitList.printWithComma(OB);
1987       OB.printClose();
1988     }
1989   }
1990 };
1991 
1992 class DeleteExpr : public Node {
1993   Node *Op;
1994   bool IsGlobal;
1995   bool IsArray;
1996 
1997 public:
1998   DeleteExpr(Node *Op_, bool IsGlobal_, bool IsArray_, Prec Prec_)
1999       : Node(KDeleteExpr, Prec_), Op(Op_), IsGlobal(IsGlobal_),
2000         IsArray(IsArray_) {}
2001 
2002   template <typename Fn> void match(Fn F) const {
2003     F(Op, IsGlobal, IsArray, getPrecedence());
2004   }
2005 
2006   void printLeft(OutputBuffer &OB) const override {
2007     if (IsGlobal)
2008       OB += "::";
2009     OB += "delete";
2010     if (IsArray)
2011       OB += "[]";
2012     OB += ' ';
2013     Op->print(OB);
2014   }
2015 };
2016 
2017 class PrefixExpr : public Node {
2018   StringView Prefix;
2019   Node *Child;
2020 
2021 public:
2022   PrefixExpr(StringView Prefix_, Node *Child_, Prec Prec_)
2023       : Node(KPrefixExpr, Prec_), Prefix(Prefix_), Child(Child_) {}
2024 
2025   template <typename Fn> void match(Fn F) const {
2026     F(Prefix, Child, getPrecedence());
2027   }
2028 
2029   void printLeft(OutputBuffer &OB) const override {
2030     OB += Prefix;
2031     Child->printAsOperand(OB, getPrecedence());
2032   }
2033 };
2034 
2035 class FunctionParam : public Node {
2036   StringView Number;
2037 
2038 public:
2039   FunctionParam(StringView Number_) : Node(KFunctionParam), Number(Number_) {}
2040 
2041   template<typename Fn> void match(Fn F) const { F(Number); }
2042 
2043   void printLeft(OutputBuffer &OB) const override {
2044     OB += "fp";
2045     OB += Number;
2046   }
2047 };
2048 
2049 class ConversionExpr : public Node {
2050   const Node *Type;
2051   NodeArray Expressions;
2052 
2053 public:
2054   ConversionExpr(const Node *Type_, NodeArray Expressions_, Prec Prec_)
2055       : Node(KConversionExpr, Prec_), Type(Type_), Expressions(Expressions_) {}
2056 
2057   template <typename Fn> void match(Fn F) const {
2058     F(Type, Expressions, getPrecedence());
2059   }
2060 
2061   void printLeft(OutputBuffer &OB) const override {
2062     OB.printOpen();
2063     Type->print(OB);
2064     OB.printClose();
2065     OB.printOpen();
2066     Expressions.printWithComma(OB);
2067     OB.printClose();
2068   }
2069 };
2070 
2071 class PointerToMemberConversionExpr : public Node {
2072   const Node *Type;
2073   const Node *SubExpr;
2074   StringView Offset;
2075 
2076 public:
2077   PointerToMemberConversionExpr(const Node *Type_, const Node *SubExpr_,
2078                                 StringView Offset_, Prec Prec_)
2079       : Node(KPointerToMemberConversionExpr, Prec_), Type(Type_),
2080         SubExpr(SubExpr_), Offset(Offset_) {}
2081 
2082   template <typename Fn> void match(Fn F) const {
2083     F(Type, SubExpr, Offset, getPrecedence());
2084   }
2085 
2086   void printLeft(OutputBuffer &OB) const override {
2087     OB.printOpen();
2088     Type->print(OB);
2089     OB.printClose();
2090     OB.printOpen();
2091     SubExpr->print(OB);
2092     OB.printClose();
2093   }
2094 };
2095 
2096 class InitListExpr : public Node {
2097   const Node *Ty;
2098   NodeArray Inits;
2099 public:
2100   InitListExpr(const Node *Ty_, NodeArray Inits_)
2101       : Node(KInitListExpr), Ty(Ty_), Inits(Inits_) {}
2102 
2103   template<typename Fn> void match(Fn F) const { F(Ty, Inits); }
2104 
2105   void printLeft(OutputBuffer &OB) const override {
2106     if (Ty)
2107       Ty->print(OB);
2108     OB += '{';
2109     Inits.printWithComma(OB);
2110     OB += '}';
2111   }
2112 };
2113 
2114 class BracedExpr : public Node {
2115   const Node *Elem;
2116   const Node *Init;
2117   bool IsArray;
2118 public:
2119   BracedExpr(const Node *Elem_, const Node *Init_, bool IsArray_)
2120       : Node(KBracedExpr), Elem(Elem_), Init(Init_), IsArray(IsArray_) {}
2121 
2122   template<typename Fn> void match(Fn F) const { F(Elem, Init, IsArray); }
2123 
2124   void printLeft(OutputBuffer &OB) const override {
2125     if (IsArray) {
2126       OB += '[';
2127       Elem->print(OB);
2128       OB += ']';
2129     } else {
2130       OB += '.';
2131       Elem->print(OB);
2132     }
2133     if (Init->getKind() != KBracedExpr && Init->getKind() != KBracedRangeExpr)
2134       OB += " = ";
2135     Init->print(OB);
2136   }
2137 };
2138 
2139 class BracedRangeExpr : public Node {
2140   const Node *First;
2141   const Node *Last;
2142   const Node *Init;
2143 public:
2144   BracedRangeExpr(const Node *First_, const Node *Last_, const Node *Init_)
2145       : Node(KBracedRangeExpr), First(First_), Last(Last_), Init(Init_) {}
2146 
2147   template<typename Fn> void match(Fn F) const { F(First, Last, Init); }
2148 
2149   void printLeft(OutputBuffer &OB) const override {
2150     OB += '[';
2151     First->print(OB);
2152     OB += " ... ";
2153     Last->print(OB);
2154     OB += ']';
2155     if (Init->getKind() != KBracedExpr && Init->getKind() != KBracedRangeExpr)
2156       OB += " = ";
2157     Init->print(OB);
2158   }
2159 };
2160 
2161 class FoldExpr : public Node {
2162   const Node *Pack, *Init;
2163   StringView OperatorName;
2164   bool IsLeftFold;
2165 
2166 public:
2167   FoldExpr(bool IsLeftFold_, StringView OperatorName_, const Node *Pack_,
2168            const Node *Init_)
2169       : Node(KFoldExpr), Pack(Pack_), Init(Init_), OperatorName(OperatorName_),
2170         IsLeftFold(IsLeftFold_) {}
2171 
2172   template<typename Fn> void match(Fn F) const {
2173     F(IsLeftFold, OperatorName, Pack, Init);
2174   }
2175 
2176   void printLeft(OutputBuffer &OB) const override {
2177     auto PrintPack = [&] {
2178       OB.printOpen();
2179       ParameterPackExpansion(Pack).print(OB);
2180       OB.printClose();
2181     };
2182 
2183     OB.printOpen();
2184     // Either '[init op ]... op pack' or 'pack op ...[ op init]'
2185     // Refactored to '[(init|pack) op ]...[ op (pack|init)]'
2186     // Fold expr operands are cast-expressions
2187     if (!IsLeftFold || Init != nullptr) {
2188       // '(init|pack) op '
2189       if (IsLeftFold)
2190         Init->printAsOperand(OB, Prec::Cast, true);
2191       else
2192         PrintPack();
2193       OB << " " << OperatorName << " ";
2194     }
2195     OB << "...";
2196     if (IsLeftFold || Init != nullptr) {
2197       // ' op (init|pack)'
2198       OB << " " << OperatorName << " ";
2199       if (IsLeftFold)
2200         PrintPack();
2201       else
2202         Init->printAsOperand(OB, Prec::Cast, true);
2203     }
2204     OB.printClose();
2205   }
2206 };
2207 
2208 class ThrowExpr : public Node {
2209   const Node *Op;
2210 
2211 public:
2212   ThrowExpr(const Node *Op_) : Node(KThrowExpr), Op(Op_) {}
2213 
2214   template<typename Fn> void match(Fn F) const { F(Op); }
2215 
2216   void printLeft(OutputBuffer &OB) const override {
2217     OB += "throw ";
2218     Op->print(OB);
2219   }
2220 };
2221 
2222 class BoolExpr : public Node {
2223   bool Value;
2224 
2225 public:
2226   BoolExpr(bool Value_) : Node(KBoolExpr), Value(Value_) {}
2227 
2228   template<typename Fn> void match(Fn F) const { F(Value); }
2229 
2230   void printLeft(OutputBuffer &OB) const override {
2231     OB += Value ? StringView("true") : StringView("false");
2232   }
2233 };
2234 
2235 class StringLiteral : public Node {
2236   const Node *Type;
2237 
2238 public:
2239   StringLiteral(const Node *Type_) : Node(KStringLiteral), Type(Type_) {}
2240 
2241   template<typename Fn> void match(Fn F) const { F(Type); }
2242 
2243   void printLeft(OutputBuffer &OB) const override {
2244     OB += "\"<";
2245     Type->print(OB);
2246     OB += ">\"";
2247   }
2248 };
2249 
2250 class LambdaExpr : public Node {
2251   const Node *Type;
2252 
2253 public:
2254   LambdaExpr(const Node *Type_) : Node(KLambdaExpr), Type(Type_) {}
2255 
2256   template<typename Fn> void match(Fn F) const { F(Type); }
2257 
2258   void printLeft(OutputBuffer &OB) const override {
2259     OB += "[]";
2260     if (Type->getKind() == KClosureTypeName)
2261       static_cast<const ClosureTypeName *>(Type)->printDeclarator(OB);
2262     OB += "{...}";
2263   }
2264 };
2265 
2266 class EnumLiteral : public Node {
2267   // ty(integer)
2268   const Node *Ty;
2269   StringView Integer;
2270 
2271 public:
2272   EnumLiteral(const Node *Ty_, StringView Integer_)
2273       : Node(KEnumLiteral), Ty(Ty_), Integer(Integer_) {}
2274 
2275   template<typename Fn> void match(Fn F) const { F(Ty, Integer); }
2276 
2277   void printLeft(OutputBuffer &OB) const override {
2278     OB.printOpen();
2279     Ty->print(OB);
2280     OB.printClose();
2281 
2282     if (Integer[0] == 'n')
2283       OB << "-" << Integer.dropFront(1);
2284     else
2285       OB << Integer;
2286   }
2287 };
2288 
2289 class IntegerLiteral : public Node {
2290   StringView Type;
2291   StringView Value;
2292 
2293 public:
2294   IntegerLiteral(StringView Type_, StringView Value_)
2295       : Node(KIntegerLiteral), Type(Type_), Value(Value_) {}
2296 
2297   template<typename Fn> void match(Fn F) const { F(Type, Value); }
2298 
2299   void printLeft(OutputBuffer &OB) const override {
2300     if (Type.size() > 3) {
2301       OB.printOpen();
2302       OB += Type;
2303       OB.printClose();
2304     }
2305 
2306     if (Value[0] == 'n') {
2307       OB += '-';
2308       OB += Value.dropFront(1);
2309     } else
2310       OB += Value;
2311 
2312     if (Type.size() <= 3)
2313       OB += Type;
2314   }
2315 };
2316 
2317 template <class Float> struct FloatData;
2318 
2319 namespace float_literal_impl {
2320 constexpr Node::Kind getFloatLiteralKind(float *) {
2321   return Node::KFloatLiteral;
2322 }
2323 constexpr Node::Kind getFloatLiteralKind(double *) {
2324   return Node::KDoubleLiteral;
2325 }
2326 constexpr Node::Kind getFloatLiteralKind(long double *) {
2327   return Node::KLongDoubleLiteral;
2328 }
2329 }
2330 
2331 template <class Float> class FloatLiteralImpl : public Node {
2332   const StringView Contents;
2333 
2334   static constexpr Kind KindForClass =
2335       float_literal_impl::getFloatLiteralKind((Float *)nullptr);
2336 
2337 public:
2338   FloatLiteralImpl(StringView Contents_)
2339       : Node(KindForClass), Contents(Contents_) {}
2340 
2341   template<typename Fn> void match(Fn F) const { F(Contents); }
2342 
2343   void printLeft(OutputBuffer &OB) const override {
2344     const char *first = Contents.begin();
2345     const char *last = Contents.end() + 1;
2346 
2347     const size_t N = FloatData<Float>::mangled_size;
2348     if (static_cast<std::size_t>(last - first) > N) {
2349       last = first + N;
2350       union {
2351         Float value;
2352         char buf[sizeof(Float)];
2353       };
2354       const char *t = first;
2355       char *e = buf;
2356       for (; t != last; ++t, ++e) {
2357         unsigned d1 = isdigit(*t) ? static_cast<unsigned>(*t - '0')
2358                                   : static_cast<unsigned>(*t - 'a' + 10);
2359         ++t;
2360         unsigned d0 = isdigit(*t) ? static_cast<unsigned>(*t - '0')
2361                                   : static_cast<unsigned>(*t - 'a' + 10);
2362         *e = static_cast<char>((d1 << 4) + d0);
2363       }
2364 #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
2365       std::reverse(buf, e);
2366 #endif
2367       char num[FloatData<Float>::max_demangled_size] = {0};
2368       int n = snprintf(num, sizeof(num), FloatData<Float>::spec, value);
2369       OB += StringView(num, num + n);
2370     }
2371   }
2372 };
2373 
2374 using FloatLiteral = FloatLiteralImpl<float>;
2375 using DoubleLiteral = FloatLiteralImpl<double>;
2376 using LongDoubleLiteral = FloatLiteralImpl<long double>;
2377 
2378 /// Visit the node. Calls \c F(P), where \c P is the node cast to the
2379 /// appropriate derived class.
2380 template<typename Fn>
2381 void Node::visit(Fn F) const {
2382   switch (K) {
2383 #define NODE(X)                                                                \
2384   case K##X:                                                                   \
2385     return F(static_cast<const X *>(this));
2386 #include "ItaniumNodes.def"
2387   }
2388   assert(0 && "unknown mangling node kind");
2389 }
2390 
2391 /// Determine the kind of a node from its type.
2392 template<typename NodeT> struct NodeKind;
2393 #define NODE(X)                                                                \
2394   template <> struct NodeKind<X> {                                             \
2395     static constexpr Node::Kind Kind = Node::K##X;                             \
2396     static constexpr const char *name() { return #X; }                         \
2397   };
2398 #include "ItaniumNodes.def"
2399 
2400 template <typename Derived, typename Alloc> struct AbstractManglingParser {
2401   const char *First;
2402   const char *Last;
2403 
2404   // Name stack, this is used by the parser to hold temporary names that were
2405   // parsed. The parser collapses multiple names into new nodes to construct
2406   // the AST. Once the parser is finished, names.size() == 1.
2407   PODSmallVector<Node *, 32> Names;
2408 
2409   // Substitution table. Itanium supports name substitutions as a means of
2410   // compression. The string "S42_" refers to the 44nd entry (base-36) in this
2411   // table.
2412   PODSmallVector<Node *, 32> Subs;
2413 
2414   using TemplateParamList = PODSmallVector<Node *, 8>;
2415 
2416   class ScopedTemplateParamList {
2417     AbstractManglingParser *Parser;
2418     size_t OldNumTemplateParamLists;
2419     TemplateParamList Params;
2420 
2421   public:
2422     ScopedTemplateParamList(AbstractManglingParser *TheParser)
2423         : Parser(TheParser),
2424           OldNumTemplateParamLists(TheParser->TemplateParams.size()) {
2425       Parser->TemplateParams.push_back(&Params);
2426     }
2427     ~ScopedTemplateParamList() {
2428       assert(Parser->TemplateParams.size() >= OldNumTemplateParamLists);
2429       Parser->TemplateParams.dropBack(OldNumTemplateParamLists);
2430     }
2431   };
2432 
2433   // Template parameter table. Like the above, but referenced like "T42_".
2434   // This has a smaller size compared to Subs and Names because it can be
2435   // stored on the stack.
2436   TemplateParamList OuterTemplateParams;
2437 
2438   // Lists of template parameters indexed by template parameter depth,
2439   // referenced like "TL2_4_". If nonempty, element 0 is always
2440   // OuterTemplateParams; inner elements are always template parameter lists of
2441   // lambda expressions. For a generic lambda with no explicit template
2442   // parameter list, the corresponding parameter list pointer will be null.
2443   PODSmallVector<TemplateParamList *, 4> TemplateParams;
2444 
2445   // Set of unresolved forward <template-param> references. These can occur in a
2446   // conversion operator's type, and are resolved in the enclosing <encoding>.
2447   PODSmallVector<ForwardTemplateReference *, 4> ForwardTemplateRefs;
2448 
2449   bool TryToParseTemplateArgs = true;
2450   bool PermitForwardTemplateReferences = false;
2451   size_t ParsingLambdaParamsAtLevel = (size_t)-1;
2452 
2453   unsigned NumSyntheticTemplateParameters[3] = {};
2454 
2455   Alloc ASTAllocator;
2456 
2457   AbstractManglingParser(const char *First_, const char *Last_)
2458       : First(First_), Last(Last_) {}
2459 
2460   Derived &getDerived() { return static_cast<Derived &>(*this); }
2461 
2462   void reset(const char *First_, const char *Last_) {
2463     First = First_;
2464     Last = Last_;
2465     Names.clear();
2466     Subs.clear();
2467     TemplateParams.clear();
2468     ParsingLambdaParamsAtLevel = (size_t)-1;
2469     TryToParseTemplateArgs = true;
2470     PermitForwardTemplateReferences = false;
2471     for (int I = 0; I != 3; ++I)
2472       NumSyntheticTemplateParameters[I] = 0;
2473     ASTAllocator.reset();
2474   }
2475 
2476   template <class T, class... Args> Node *make(Args &&... args) {
2477     return ASTAllocator.template makeNode<T>(std::forward<Args>(args)...);
2478   }
2479 
2480   template <class It> NodeArray makeNodeArray(It begin, It end) {
2481     size_t sz = static_cast<size_t>(end - begin);
2482     void *mem = ASTAllocator.allocateNodeArray(sz);
2483     Node **data = new (mem) Node *[sz];
2484     std::copy(begin, end, data);
2485     return NodeArray(data, sz);
2486   }
2487 
2488   NodeArray popTrailingNodeArray(size_t FromPosition) {
2489     assert(FromPosition <= Names.size());
2490     NodeArray res =
2491         makeNodeArray(Names.begin() + (long)FromPosition, Names.end());
2492     Names.dropBack(FromPosition);
2493     return res;
2494   }
2495 
2496   bool consumeIf(StringView S) {
2497     if (StringView(First, Last).startsWith(S)) {
2498       First += S.size();
2499       return true;
2500     }
2501     return false;
2502   }
2503 
2504   bool consumeIf(char C) {
2505     if (First != Last && *First == C) {
2506       ++First;
2507       return true;
2508     }
2509     return false;
2510   }
2511 
2512   char consume() { return First != Last ? *First++ : '\0'; }
2513 
2514   char look(unsigned Lookahead = 0) const {
2515     if (static_cast<size_t>(Last - First) <= Lookahead)
2516       return '\0';
2517     return First[Lookahead];
2518   }
2519 
2520   size_t numLeft() const { return static_cast<size_t>(Last - First); }
2521 
2522   StringView parseNumber(bool AllowNegative = false);
2523   Qualifiers parseCVQualifiers();
2524   bool parsePositiveInteger(size_t *Out);
2525   StringView parseBareSourceName();
2526 
2527   bool parseSeqId(size_t *Out);
2528   Node *parseSubstitution();
2529   Node *parseTemplateParam();
2530   Node *parseTemplateParamDecl();
2531   Node *parseTemplateArgs(bool TagTemplates = false);
2532   Node *parseTemplateArg();
2533 
2534   /// Parse the <expr> production.
2535   Node *parseExpr();
2536   Node *parsePrefixExpr(StringView Kind, Node::Prec Prec);
2537   Node *parseBinaryExpr(StringView Kind, Node::Prec Prec);
2538   Node *parseIntegerLiteral(StringView Lit);
2539   Node *parseExprPrimary();
2540   template <class Float> Node *parseFloatingLiteral();
2541   Node *parseFunctionParam();
2542   Node *parseConversionExpr();
2543   Node *parseBracedExpr();
2544   Node *parseFoldExpr();
2545   Node *parsePointerToMemberConversionExpr(Node::Prec Prec);
2546   Node *parseSubobjectExpr();
2547 
2548   /// Parse the <type> production.
2549   Node *parseType();
2550   Node *parseFunctionType();
2551   Node *parseVectorType();
2552   Node *parseDecltype();
2553   Node *parseArrayType();
2554   Node *parsePointerToMemberType();
2555   Node *parseClassEnumType();
2556   Node *parseQualifiedType();
2557 
2558   Node *parseEncoding();
2559   bool parseCallOffset();
2560   Node *parseSpecialName();
2561 
2562   /// Holds some extra information about a <name> that is being parsed. This
2563   /// information is only pertinent if the <name> refers to an <encoding>.
2564   struct NameState {
2565     bool CtorDtorConversion = false;
2566     bool EndsWithTemplateArgs = false;
2567     Qualifiers CVQualifiers = QualNone;
2568     FunctionRefQual ReferenceQualifier = FrefQualNone;
2569     size_t ForwardTemplateRefsBegin;
2570 
2571     NameState(AbstractManglingParser *Enclosing)
2572         : ForwardTemplateRefsBegin(Enclosing->ForwardTemplateRefs.size()) {}
2573   };
2574 
2575   bool resolveForwardTemplateRefs(NameState &State) {
2576     size_t I = State.ForwardTemplateRefsBegin;
2577     size_t E = ForwardTemplateRefs.size();
2578     for (; I < E; ++I) {
2579       size_t Idx = ForwardTemplateRefs[I]->Index;
2580       if (TemplateParams.empty() || !TemplateParams[0] ||
2581           Idx >= TemplateParams[0]->size())
2582         return true;
2583       ForwardTemplateRefs[I]->Ref = (*TemplateParams[0])[Idx];
2584     }
2585     ForwardTemplateRefs.dropBack(State.ForwardTemplateRefsBegin);
2586     return false;
2587   }
2588 
2589   /// Parse the <name> production>
2590   Node *parseName(NameState *State = nullptr);
2591   Node *parseLocalName(NameState *State);
2592   Node *parseOperatorName(NameState *State);
2593   bool parseModuleNameOpt(ModuleName *&Module);
2594   Node *parseUnqualifiedName(NameState *State, Node *Scope, ModuleName *Module);
2595   Node *parseUnnamedTypeName(NameState *State);
2596   Node *parseSourceName(NameState *State);
2597   Node *parseUnscopedName(NameState *State, bool *isSubstName);
2598   Node *parseNestedName(NameState *State);
2599   Node *parseCtorDtorName(Node *&SoFar, NameState *State);
2600 
2601   Node *parseAbiTags(Node *N);
2602 
2603   struct OperatorInfo {
2604     enum OIKind : unsigned char {
2605       Prefix,      // Prefix unary: @ expr
2606       Postfix,     // Postfix unary: expr @
2607       Binary,      // Binary: lhs @ rhs
2608       Array,       // Array index:  lhs [ rhs ]
2609       Member,      // Member access: lhs @ rhs
2610       New,         // New
2611       Del,         // Delete
2612       Call,        // Function call: expr (expr*)
2613       CCast,       // C cast: (type)expr
2614       Conditional, // Conditional: expr ? expr : expr
2615       NameOnly,    // Overload only, not allowed in expression.
2616       // Below do not have operator names
2617       NamedCast, // Named cast, @<type>(expr)
2618       OfIdOp,    // alignof, sizeof, typeid
2619 
2620       Unnameable = NamedCast,
2621     };
2622     char Enc[2];      // Encoding
2623     OIKind Kind;      // Kind of operator
2624     bool Flag : 1;    // Entry-specific flag
2625     Node::Prec Prec : 7; // Precedence
2626     const char *Name; // Spelling
2627 
2628   public:
2629     constexpr OperatorInfo(const char (&E)[3], OIKind K, bool F, Node::Prec P,
2630                            const char *N)
2631         : Enc{E[0], E[1]}, Kind{K}, Flag{F}, Prec{P}, Name{N} {}
2632 
2633   public:
2634     bool operator<(const OperatorInfo &Other) const {
2635       return *this < Other.Enc;
2636     }
2637     bool operator<(const char *Peek) const {
2638       return Enc[0] < Peek[0] || (Enc[0] == Peek[0] && Enc[1] < Peek[1]);
2639     }
2640     bool operator==(const char *Peek) const {
2641       return Enc[0] == Peek[0] && Enc[1] == Peek[1];
2642     }
2643     bool operator!=(const char *Peek) const { return !this->operator==(Peek); }
2644 
2645   public:
2646     StringView getSymbol() const {
2647       StringView Res = Name;
2648       if (Kind < Unnameable) {
2649         assert(Res.startsWith("operator") &&
2650                "operator name does not start with 'operator'");
2651         Res = Res.dropFront(sizeof("operator") - 1);
2652         Res.consumeFront(' ');
2653       }
2654       return Res;
2655     }
2656     StringView getName() const { return Name; }
2657     OIKind getKind() const { return Kind; }
2658     bool getFlag() const { return Flag; }
2659     Node::Prec getPrecedence() const { return Prec; }
2660   };
2661   static const OperatorInfo Ops[];
2662   static const size_t NumOps;
2663   const OperatorInfo *parseOperatorEncoding();
2664 
2665   /// Parse the <unresolved-name> production.
2666   Node *parseUnresolvedName(bool Global);
2667   Node *parseSimpleId();
2668   Node *parseBaseUnresolvedName();
2669   Node *parseUnresolvedType();
2670   Node *parseDestructorName();
2671 
2672   /// Top-level entry point into the parser.
2673   Node *parse();
2674 };
2675 
2676 const char* parse_discriminator(const char* first, const char* last);
2677 
2678 // <name> ::= <nested-name> // N
2679 //        ::= <local-name> # See Scope Encoding below  // Z
2680 //        ::= <unscoped-template-name> <template-args>
2681 //        ::= <unscoped-name>
2682 //
2683 // <unscoped-template-name> ::= <unscoped-name>
2684 //                          ::= <substitution>
2685 template <typename Derived, typename Alloc>
2686 Node *AbstractManglingParser<Derived, Alloc>::parseName(NameState *State) {
2687   if (look() == 'N')
2688     return getDerived().parseNestedName(State);
2689   if (look() == 'Z')
2690     return getDerived().parseLocalName(State);
2691 
2692   Node *Result = nullptr;
2693   bool IsSubst = false;
2694 
2695   Result = getDerived().parseUnscopedName(State, &IsSubst);
2696   if (!Result)
2697     return nullptr;
2698 
2699   if (look() == 'I') {
2700     //        ::= <unscoped-template-name> <template-args>
2701     if (!IsSubst)
2702       // An unscoped-template-name is substitutable.
2703       Subs.push_back(Result);
2704     Node *TA = getDerived().parseTemplateArgs(State != nullptr);
2705     if (TA == nullptr)
2706       return nullptr;
2707     if (State)
2708       State->EndsWithTemplateArgs = true;
2709     Result = make<NameWithTemplateArgs>(Result, TA);
2710   } else if (IsSubst) {
2711     // The substitution case must be followed by <template-args>.
2712     return nullptr;
2713   }
2714 
2715   return Result;
2716 }
2717 
2718 // <local-name> := Z <function encoding> E <entity name> [<discriminator>]
2719 //              := Z <function encoding> E s [<discriminator>]
2720 //              := Z <function encoding> Ed [ <parameter number> ] _ <entity name>
2721 template <typename Derived, typename Alloc>
2722 Node *AbstractManglingParser<Derived, Alloc>::parseLocalName(NameState *State) {
2723   if (!consumeIf('Z'))
2724     return nullptr;
2725   Node *Encoding = getDerived().parseEncoding();
2726   if (Encoding == nullptr || !consumeIf('E'))
2727     return nullptr;
2728 
2729   if (consumeIf('s')) {
2730     First = parse_discriminator(First, Last);
2731     auto *StringLitName = make<NameType>("string literal");
2732     if (!StringLitName)
2733       return nullptr;
2734     return make<LocalName>(Encoding, StringLitName);
2735   }
2736 
2737   if (consumeIf('d')) {
2738     parseNumber(true);
2739     if (!consumeIf('_'))
2740       return nullptr;
2741     Node *N = getDerived().parseName(State);
2742     if (N == nullptr)
2743       return nullptr;
2744     return make<LocalName>(Encoding, N);
2745   }
2746 
2747   Node *Entity = getDerived().parseName(State);
2748   if (Entity == nullptr)
2749     return nullptr;
2750   First = parse_discriminator(First, Last);
2751   return make<LocalName>(Encoding, Entity);
2752 }
2753 
2754 // <unscoped-name> ::= <unqualified-name>
2755 //                 ::= St <unqualified-name>   # ::std::
2756 // [*] extension
2757 template <typename Derived, typename Alloc>
2758 Node *
2759 AbstractManglingParser<Derived, Alloc>::parseUnscopedName(NameState *State,
2760                                                           bool *IsSubst) {
2761 
2762   Node *Std = nullptr;
2763   if (consumeIf("St")) {
2764     Std = make<NameType>("std");
2765     if (Std == nullptr)
2766       return nullptr;
2767   }
2768 
2769   Node *Res = nullptr;
2770   ModuleName *Module = nullptr;
2771   if (look() == 'S') {
2772     Node *S = getDerived().parseSubstitution();
2773     if (!S)
2774       return nullptr;
2775     if (S->getKind() == Node::KModuleName)
2776       Module = static_cast<ModuleName *>(S);
2777     else if (IsSubst && Std == nullptr) {
2778       Res = S;
2779       *IsSubst = true;
2780     } else {
2781       return nullptr;
2782     }
2783   }
2784 
2785   if (Res == nullptr || Std != nullptr) {
2786     Res = getDerived().parseUnqualifiedName(State, Std, Module);
2787   }
2788 
2789   return Res;
2790 }
2791 
2792 // <unqualified-name> ::= [<module-name>] L? <operator-name> [<abi-tags>]
2793 //                    ::= [<module-name>] <ctor-dtor-name> [<abi-tags>]
2794 //                    ::= [<module-name>] L? <source-name> [<abi-tags>]
2795 //                    ::= [<module-name>] L? <unnamed-type-name> [<abi-tags>]
2796 //			# structured binding declaration
2797 //                    ::= [<module-name>] L? DC <source-name>+ E
2798 template <typename Derived, typename Alloc>
2799 Node *AbstractManglingParser<Derived, Alloc>::parseUnqualifiedName(
2800     NameState *State, Node *Scope, ModuleName *Module) {
2801   if (getDerived().parseModuleNameOpt(Module))
2802     return nullptr;
2803 
2804   consumeIf('L');
2805 
2806   Node *Result;
2807   if (look() >= '1' && look() <= '9') {
2808     Result = getDerived().parseSourceName(State);
2809   } else if (look() == 'U') {
2810     Result = getDerived().parseUnnamedTypeName(State);
2811   } else if (consumeIf("DC")) {
2812     // Structured binding
2813     size_t BindingsBegin = Names.size();
2814     do {
2815       Node *Binding = getDerived().parseSourceName(State);
2816       if (Binding == nullptr)
2817         return nullptr;
2818       Names.push_back(Binding);
2819     } while (!consumeIf('E'));
2820     Result = make<StructuredBindingName>(popTrailingNodeArray(BindingsBegin));
2821   } else if (look() == 'C' || look() == 'D') {
2822     // A <ctor-dtor-name>.
2823     if (Scope == nullptr || Module != nullptr)
2824       return nullptr;
2825     Result = getDerived().parseCtorDtorName(Scope, State);
2826   } else {
2827     Result = getDerived().parseOperatorName(State);
2828   }
2829 
2830   if (Result != nullptr && Module != nullptr)
2831     Result = make<ModuleEntity>(Module, Result);
2832   if (Result != nullptr)
2833     Result = getDerived().parseAbiTags(Result);
2834   if (Result != nullptr && Scope != nullptr)
2835     Result = make<NestedName>(Scope, Result);
2836 
2837   return Result;
2838 }
2839 
2840 // <module-name> ::= <module-subname>
2841 // 	 	 ::= <module-name> <module-subname>
2842 //		 ::= <substitution>  # passed in by caller
2843 // <module-subname> ::= W <source-name>
2844 //		    ::= W P <source-name>
2845 template <typename Derived, typename Alloc>
2846 bool AbstractManglingParser<Derived, Alloc>::parseModuleNameOpt(
2847     ModuleName *&Module) {
2848   while (consumeIf('W')) {
2849     bool IsPartition = consumeIf('P');
2850     Node *Sub = getDerived().parseSourceName(nullptr);
2851     if (!Sub)
2852       return true;
2853     Module =
2854         static_cast<ModuleName *>(make<ModuleName>(Module, Sub, IsPartition));
2855     Subs.push_back(Module);
2856   }
2857 
2858   return false;
2859 }
2860 
2861 // <unnamed-type-name> ::= Ut [<nonnegative number>] _
2862 //                     ::= <closure-type-name>
2863 //
2864 // <closure-type-name> ::= Ul <lambda-sig> E [ <nonnegative number> ] _
2865 //
2866 // <lambda-sig> ::= <parameter type>+  # Parameter types or "v" if the lambda has no parameters
2867 template <typename Derived, typename Alloc>
2868 Node *
2869 AbstractManglingParser<Derived, Alloc>::parseUnnamedTypeName(NameState *State) {
2870   // <template-params> refer to the innermost <template-args>. Clear out any
2871   // outer args that we may have inserted into TemplateParams.
2872   if (State != nullptr)
2873     TemplateParams.clear();
2874 
2875   if (consumeIf("Ut")) {
2876     StringView Count = parseNumber();
2877     if (!consumeIf('_'))
2878       return nullptr;
2879     return make<UnnamedTypeName>(Count);
2880   }
2881   if (consumeIf("Ul")) {
2882     ScopedOverride<size_t> SwapParams(ParsingLambdaParamsAtLevel,
2883                                       TemplateParams.size());
2884     ScopedTemplateParamList LambdaTemplateParams(this);
2885 
2886     size_t ParamsBegin = Names.size();
2887     while (look() == 'T' &&
2888            StringView("yptn").find(look(1)) != StringView::npos) {
2889       Node *T = parseTemplateParamDecl();
2890       if (!T)
2891         return nullptr;
2892       Names.push_back(T);
2893     }
2894     NodeArray TempParams = popTrailingNodeArray(ParamsBegin);
2895 
2896     // FIXME: If TempParams is empty and none of the function parameters
2897     // includes 'auto', we should remove LambdaTemplateParams from the
2898     // TemplateParams list. Unfortunately, we don't find out whether there are
2899     // any 'auto' parameters until too late in an example such as:
2900     //
2901     //   template<typename T> void f(
2902     //       decltype([](decltype([]<typename T>(T v) {}),
2903     //                   auto) {})) {}
2904     //   template<typename T> void f(
2905     //       decltype([](decltype([]<typename T>(T w) {}),
2906     //                   int) {})) {}
2907     //
2908     // Here, the type of v is at level 2 but the type of w is at level 1. We
2909     // don't find this out until we encounter the type of the next parameter.
2910     //
2911     // However, compilers can't actually cope with the former example in
2912     // practice, and it's likely to be made ill-formed in future, so we don't
2913     // need to support it here.
2914     //
2915     // If we encounter an 'auto' in the function parameter types, we will
2916     // recreate a template parameter scope for it, but any intervening lambdas
2917     // will be parsed in the 'wrong' template parameter depth.
2918     if (TempParams.empty())
2919       TemplateParams.pop_back();
2920 
2921     if (!consumeIf("vE")) {
2922       do {
2923         Node *P = getDerived().parseType();
2924         if (P == nullptr)
2925           return nullptr;
2926         Names.push_back(P);
2927       } while (!consumeIf('E'));
2928     }
2929     NodeArray Params = popTrailingNodeArray(ParamsBegin);
2930 
2931     StringView Count = parseNumber();
2932     if (!consumeIf('_'))
2933       return nullptr;
2934     return make<ClosureTypeName>(TempParams, Params, Count);
2935   }
2936   if (consumeIf("Ub")) {
2937     (void)parseNumber();
2938     if (!consumeIf('_'))
2939       return nullptr;
2940     return make<NameType>("'block-literal'");
2941   }
2942   return nullptr;
2943 }
2944 
2945 // <source-name> ::= <positive length number> <identifier>
2946 template <typename Derived, typename Alloc>
2947 Node *AbstractManglingParser<Derived, Alloc>::parseSourceName(NameState *) {
2948   size_t Length = 0;
2949   if (parsePositiveInteger(&Length))
2950     return nullptr;
2951   if (numLeft() < Length || Length == 0)
2952     return nullptr;
2953   StringView Name(First, First + Length);
2954   First += Length;
2955   if (Name.startsWith("_GLOBAL__N"))
2956     return make<NameType>("(anonymous namespace)");
2957   return make<NameType>(Name);
2958 }
2959 
2960 // Operator encodings
2961 template <typename Derived, typename Alloc>
2962 const typename AbstractManglingParser<
2963     Derived, Alloc>::OperatorInfo AbstractManglingParser<Derived,
2964                                                          Alloc>::Ops[] = {
2965     // Keep ordered by encoding
2966     {"aN", OperatorInfo::Binary, false, Node::Prec::Assign, "operator&="},
2967     {"aS", OperatorInfo::Binary, false, Node::Prec::Assign, "operator="},
2968     {"aa", OperatorInfo::Binary, false, Node::Prec::AndIf, "operator&&"},
2969     {"ad", OperatorInfo::Prefix, false, Node::Prec::Unary, "operator&"},
2970     {"an", OperatorInfo::Binary, false, Node::Prec::And, "operator&"},
2971     {"at", OperatorInfo::OfIdOp, /*Type*/ true, Node::Prec::Unary, "alignof "},
2972     {"aw", OperatorInfo::NameOnly, false, Node::Prec::Primary,
2973      "operator co_await"},
2974     {"az", OperatorInfo::OfIdOp, /*Type*/ false, Node::Prec::Unary, "alignof "},
2975     {"cc", OperatorInfo::NamedCast, false, Node::Prec::Postfix, "const_cast"},
2976     {"cl", OperatorInfo::Call, false, Node::Prec::Postfix, "operator()"},
2977     {"cm", OperatorInfo::Binary, false, Node::Prec::Comma, "operator,"},
2978     {"co", OperatorInfo::Prefix, false, Node::Prec::Unary, "operator~"},
2979     {"cv", OperatorInfo::CCast, false, Node::Prec::Cast, "operator"}, // C Cast
2980     {"dV", OperatorInfo::Binary, false, Node::Prec::Assign, "operator/="},
2981     {"da", OperatorInfo::Del, /*Ary*/ true, Node::Prec::Unary,
2982      "operator delete[]"},
2983     {"dc", OperatorInfo::NamedCast, false, Node::Prec::Postfix, "dynamic_cast"},
2984     {"de", OperatorInfo::Prefix, false, Node::Prec::Unary, "operator*"},
2985     {"dl", OperatorInfo::Del, /*Ary*/ false, Node::Prec::Unary,
2986      "operator delete"},
2987     {"ds", OperatorInfo::Member, /*Named*/ false, Node::Prec::PtrMem,
2988      "operator.*"},
2989     {"dt", OperatorInfo::Member, /*Named*/ false, Node::Prec::Postfix,
2990      "operator."},
2991     {"dv", OperatorInfo::Binary, false, Node::Prec::Assign, "operator/"},
2992     {"eO", OperatorInfo::Binary, false, Node::Prec::Assign, "operator^="},
2993     {"eo", OperatorInfo::Binary, false, Node::Prec::Xor, "operator^"},
2994     {"eq", OperatorInfo::Binary, false, Node::Prec::Equality, "operator=="},
2995     {"ge", OperatorInfo::Binary, false, Node::Prec::Relational, "operator>="},
2996     {"gt", OperatorInfo::Binary, false, Node::Prec::Relational, "operator>"},
2997     {"ix", OperatorInfo::Array, false, Node::Prec::Postfix, "operator[]"},
2998     {"lS", OperatorInfo::Binary, false, Node::Prec::Assign, "operator<<="},
2999     {"le", OperatorInfo::Binary, false, Node::Prec::Relational, "operator<="},
3000     {"ls", OperatorInfo::Binary, false, Node::Prec::Shift, "operator<<"},
3001     {"lt", OperatorInfo::Binary, false, Node::Prec::Relational, "operator<"},
3002     {"mI", OperatorInfo::Binary, false, Node::Prec::Assign, "operator-="},
3003     {"mL", OperatorInfo::Binary, false, Node::Prec::Assign, "operator*="},
3004     {"mi", OperatorInfo::Binary, false, Node::Prec::Additive, "operator-"},
3005     {"ml", OperatorInfo::Binary, false, Node::Prec::Multiplicative,
3006      "operator*"},
3007     {"mm", OperatorInfo::Postfix, false, Node::Prec::Postfix, "operator--"},
3008     {"na", OperatorInfo::New, /*Ary*/ true, Node::Prec::Unary,
3009      "operator new[]"},
3010     {"ne", OperatorInfo::Binary, false, Node::Prec::Equality, "operator!="},
3011     {"ng", OperatorInfo::Prefix, false, Node::Prec::Unary, "operator-"},
3012     {"nt", OperatorInfo::Prefix, false, Node::Prec::Unary, "operator!"},
3013     {"nw", OperatorInfo::New, /*Ary*/ false, Node::Prec::Unary, "operator new"},
3014     {"oR", OperatorInfo::Binary, false, Node::Prec::Assign, "operator|="},
3015     {"oo", OperatorInfo::Binary, false, Node::Prec::OrIf, "operator||"},
3016     {"or", OperatorInfo::Binary, false, Node::Prec::Ior, "operator|"},
3017     {"pL", OperatorInfo::Binary, false, Node::Prec::Assign, "operator+="},
3018     {"pl", OperatorInfo::Binary, false, Node::Prec::Additive, "operator+"},
3019     {"pm", OperatorInfo::Member, /*Named*/ false, Node::Prec::PtrMem,
3020      "operator->*"},
3021     {"pp", OperatorInfo::Postfix, false, Node::Prec::Postfix, "operator++"},
3022     {"ps", OperatorInfo::Prefix, false, Node::Prec::Unary, "operator+"},
3023     {"pt", OperatorInfo::Member, /*Named*/ true, Node::Prec::Postfix,
3024      "operator->"},
3025     {"qu", OperatorInfo::Conditional, false, Node::Prec::Conditional,
3026      "operator?"},
3027     {"rM", OperatorInfo::Binary, false, Node::Prec::Assign, "operator%="},
3028     {"rS", OperatorInfo::Binary, false, Node::Prec::Assign, "operator>>="},
3029     {"rc", OperatorInfo::NamedCast, false, Node::Prec::Postfix,
3030      "reinterpret_cast"},
3031     {"rm", OperatorInfo::Binary, false, Node::Prec::Multiplicative,
3032      "operator%"},
3033     {"rs", OperatorInfo::Binary, false, Node::Prec::Shift, "operator>>"},
3034     {"sc", OperatorInfo::NamedCast, false, Node::Prec::Postfix, "static_cast"},
3035     {"ss", OperatorInfo::Binary, false, Node::Prec::Spaceship, "operator<=>"},
3036     {"st", OperatorInfo::OfIdOp, /*Type*/ true, Node::Prec::Unary, "sizeof "},
3037     {"sz", OperatorInfo::OfIdOp, /*Type*/ false, Node::Prec::Unary, "sizeof "},
3038     {"te", OperatorInfo::OfIdOp, /*Type*/ false, Node::Prec::Postfix,
3039      "typeid "},
3040     {"ti", OperatorInfo::OfIdOp, /*Type*/ true, Node::Prec::Postfix, "typeid "},
3041 };
3042 template <typename Derived, typename Alloc>
3043 const size_t AbstractManglingParser<Derived, Alloc>::NumOps = sizeof(Ops) /
3044                                                               sizeof(Ops[0]);
3045 
3046 // If the next 2 chars are an operator encoding, consume them and return their
3047 // OperatorInfo.  Otherwise return nullptr.
3048 template <typename Derived, typename Alloc>
3049 const typename AbstractManglingParser<Derived, Alloc>::OperatorInfo *
3050 AbstractManglingParser<Derived, Alloc>::parseOperatorEncoding() {
3051   if (numLeft() < 2)
3052     return nullptr;
3053 
3054   auto Op = std::lower_bound(
3055       &Ops[0], &Ops[NumOps], First,
3056       [](const OperatorInfo &Op_, const char *Enc_) { return Op_ < Enc_; });
3057   if (Op == &Ops[NumOps] || *Op != First)
3058     return nullptr;
3059 
3060   First += 2;
3061   return Op;
3062 }
3063 
3064 //   <operator-name> ::= See parseOperatorEncoding()
3065 //                   ::= li <source-name>  # operator ""
3066 //                   ::= v <digit> <source-name>  # vendor extended operator
3067 template <typename Derived, typename Alloc>
3068 Node *
3069 AbstractManglingParser<Derived, Alloc>::parseOperatorName(NameState *State) {
3070   if (const auto *Op = parseOperatorEncoding()) {
3071     if (Op->getKind() == OperatorInfo::CCast) {
3072       //              ::= cv <type>    # (cast)
3073       ScopedOverride<bool> SaveTemplate(TryToParseTemplateArgs, false);
3074       // If we're parsing an encoding, State != nullptr and the conversion
3075       // operators' <type> could have a <template-param> that refers to some
3076       // <template-arg>s further ahead in the mangled name.
3077       ScopedOverride<bool> SavePermit(PermitForwardTemplateReferences,
3078                                       PermitForwardTemplateReferences ||
3079                                           State != nullptr);
3080       Node *Ty = getDerived().parseType();
3081       if (Ty == nullptr)
3082         return nullptr;
3083       if (State) State->CtorDtorConversion = true;
3084       return make<ConversionOperatorType>(Ty);
3085     }
3086 
3087     if (Op->getKind() >= OperatorInfo::Unnameable)
3088       /* Not a nameable operator.  */
3089       return nullptr;
3090     if (Op->getKind() == OperatorInfo::Member && !Op->getFlag())
3091       /* Not a nameable MemberExpr */
3092       return nullptr;
3093 
3094     return make<NameType>(Op->getName());
3095   }
3096 
3097   if (consumeIf("li")) {
3098     //                   ::= li <source-name>  # operator ""
3099     Node *SN = getDerived().parseSourceName(State);
3100     if (SN == nullptr)
3101       return nullptr;
3102     return make<LiteralOperator>(SN);
3103   }
3104 
3105   if (consumeIf('v')) {
3106     // ::= v <digit> <source-name>        # vendor extended operator
3107     if (look() >= '0' && look() <= '9') {
3108       First++;
3109       Node *SN = getDerived().parseSourceName(State);
3110       if (SN == nullptr)
3111         return nullptr;
3112       return make<ConversionOperatorType>(SN);
3113     }
3114     return nullptr;
3115   }
3116 
3117   return nullptr;
3118 }
3119 
3120 // <ctor-dtor-name> ::= C1  # complete object constructor
3121 //                  ::= C2  # base object constructor
3122 //                  ::= C3  # complete object allocating constructor
3123 //   extension      ::= C4  # gcc old-style "[unified]" constructor
3124 //   extension      ::= C5  # the COMDAT used for ctors
3125 //                  ::= D0  # deleting destructor
3126 //                  ::= D1  # complete object destructor
3127 //                  ::= D2  # base object destructor
3128 //   extension      ::= D4  # gcc old-style "[unified]" destructor
3129 //   extension      ::= D5  # the COMDAT used for dtors
3130 template <typename Derived, typename Alloc>
3131 Node *
3132 AbstractManglingParser<Derived, Alloc>::parseCtorDtorName(Node *&SoFar,
3133                                                           NameState *State) {
3134   if (SoFar->getKind() == Node::KSpecialSubstitution) {
3135     auto SSK = static_cast<SpecialSubstitution *>(SoFar)->SSK;
3136     switch (SSK) {
3137     case SpecialSubKind::string:
3138     case SpecialSubKind::istream:
3139     case SpecialSubKind::ostream:
3140     case SpecialSubKind::iostream:
3141       SoFar = make<ExpandedSpecialSubstitution>(SSK);
3142       if (!SoFar)
3143         return nullptr;
3144       break;
3145     default:
3146       break;
3147     }
3148   }
3149 
3150   if (consumeIf('C')) {
3151     bool IsInherited = consumeIf('I');
3152     if (look() != '1' && look() != '2' && look() != '3' && look() != '4' &&
3153         look() != '5')
3154       return nullptr;
3155     int Variant = look() - '0';
3156     ++First;
3157     if (State) State->CtorDtorConversion = true;
3158     if (IsInherited) {
3159       if (getDerived().parseName(State) == nullptr)
3160         return nullptr;
3161     }
3162     return make<CtorDtorName>(SoFar, /*IsDtor=*/false, Variant);
3163   }
3164 
3165   if (look() == 'D' && (look(1) == '0' || look(1) == '1' || look(1) == '2' ||
3166                         look(1) == '4' || look(1) == '5')) {
3167     int Variant = look(1) - '0';
3168     First += 2;
3169     if (State) State->CtorDtorConversion = true;
3170     return make<CtorDtorName>(SoFar, /*IsDtor=*/true, Variant);
3171   }
3172 
3173   return nullptr;
3174 }
3175 
3176 // <nested-name> ::= N [<CV-Qualifiers>] [<ref-qualifier>] <prefix>
3177 // 			<unqualified-name> E
3178 //               ::= N [<CV-Qualifiers>] [<ref-qualifier>] <template-prefix>
3179 //               	<template-args> E
3180 //
3181 // <prefix> ::= <prefix> <unqualified-name>
3182 //          ::= <template-prefix> <template-args>
3183 //          ::= <template-param>
3184 //          ::= <decltype>
3185 //          ::= # empty
3186 //          ::= <substitution>
3187 //          ::= <prefix> <data-member-prefix>
3188 // [*] extension
3189 //
3190 // <data-member-prefix> := <member source-name> [<template-args>] M
3191 //
3192 // <template-prefix> ::= <prefix> <template unqualified-name>
3193 //                   ::= <template-param>
3194 //                   ::= <substitution>
3195 template <typename Derived, typename Alloc>
3196 Node *
3197 AbstractManglingParser<Derived, Alloc>::parseNestedName(NameState *State) {
3198   if (!consumeIf('N'))
3199     return nullptr;
3200 
3201   Qualifiers CVTmp = parseCVQualifiers();
3202   if (State) State->CVQualifiers = CVTmp;
3203 
3204   if (consumeIf('O')) {
3205     if (State) State->ReferenceQualifier = FrefQualRValue;
3206   } else if (consumeIf('R')) {
3207     if (State) State->ReferenceQualifier = FrefQualLValue;
3208   } else {
3209     if (State) State->ReferenceQualifier = FrefQualNone;
3210   }
3211 
3212   Node *SoFar = nullptr;
3213   while (!consumeIf('E')) {
3214     if (State)
3215       // Only set end-with-template on the case that does that.
3216       State->EndsWithTemplateArgs = false;
3217 
3218     if (look() == 'T') {
3219       //          ::= <template-param>
3220       if (SoFar != nullptr)
3221         return nullptr; // Cannot have a prefix.
3222       SoFar = getDerived().parseTemplateParam();
3223     } else if (look() == 'I') {
3224       //          ::= <template-prefix> <template-args>
3225       if (SoFar == nullptr)
3226         return nullptr; // Must have a prefix.
3227       Node *TA = getDerived().parseTemplateArgs(State != nullptr);
3228       if (TA == nullptr)
3229         return nullptr;
3230       if (SoFar->getKind() == Node::KNameWithTemplateArgs)
3231         // Semantically <template-args> <template-args> cannot be generated by a
3232         // C++ entity.  There will always be [something like] a name between
3233         // them.
3234         return nullptr;
3235       if (State)
3236         State->EndsWithTemplateArgs = true;
3237       SoFar = make<NameWithTemplateArgs>(SoFar, TA);
3238     } else if (look() == 'D' && (look(1) == 't' || look(1) == 'T')) {
3239       //          ::= <decltype>
3240       if (SoFar != nullptr)
3241         return nullptr; // Cannot have a prefix.
3242       SoFar = getDerived().parseDecltype();
3243     } else {
3244       ModuleName *Module = nullptr;
3245 
3246       if (look() == 'S') {
3247         //          ::= <substitution>
3248         Node *S = nullptr;
3249         if (look(1) == 't') {
3250           First += 2;
3251           S = make<NameType>("std");
3252         } else {
3253           S = getDerived().parseSubstitution();
3254         }
3255         if (!S)
3256           return nullptr;
3257         if (S->getKind() == Node::KModuleName) {
3258           Module = static_cast<ModuleName *>(S);
3259         } else if (SoFar != nullptr) {
3260           return nullptr; // Cannot have a prefix.
3261         } else {
3262           SoFar = S;
3263           continue; // Do not push a new substitution.
3264         }
3265       }
3266 
3267       //          ::= [<prefix>] <unqualified-name>
3268       SoFar = getDerived().parseUnqualifiedName(State, SoFar, Module);
3269     }
3270 
3271     if (SoFar == nullptr)
3272       return nullptr;
3273     Subs.push_back(SoFar);
3274 
3275     // No longer used.
3276     // <data-member-prefix> := <member source-name> [<template-args>] M
3277     consumeIf('M');
3278   }
3279 
3280   if (SoFar == nullptr || Subs.empty())
3281     return nullptr;
3282 
3283   Subs.pop_back();
3284   return SoFar;
3285 }
3286 
3287 // <simple-id> ::= <source-name> [ <template-args> ]
3288 template <typename Derived, typename Alloc>
3289 Node *AbstractManglingParser<Derived, Alloc>::parseSimpleId() {
3290   Node *SN = getDerived().parseSourceName(/*NameState=*/nullptr);
3291   if (SN == nullptr)
3292     return nullptr;
3293   if (look() == 'I') {
3294     Node *TA = getDerived().parseTemplateArgs();
3295     if (TA == nullptr)
3296       return nullptr;
3297     return make<NameWithTemplateArgs>(SN, TA);
3298   }
3299   return SN;
3300 }
3301 
3302 // <destructor-name> ::= <unresolved-type>  # e.g., ~T or ~decltype(f())
3303 //                   ::= <simple-id>        # e.g., ~A<2*N>
3304 template <typename Derived, typename Alloc>
3305 Node *AbstractManglingParser<Derived, Alloc>::parseDestructorName() {
3306   Node *Result;
3307   if (std::isdigit(look()))
3308     Result = getDerived().parseSimpleId();
3309   else
3310     Result = getDerived().parseUnresolvedType();
3311   if (Result == nullptr)
3312     return nullptr;
3313   return make<DtorName>(Result);
3314 }
3315 
3316 // <unresolved-type> ::= <template-param>
3317 //                   ::= <decltype>
3318 //                   ::= <substitution>
3319 template <typename Derived, typename Alloc>
3320 Node *AbstractManglingParser<Derived, Alloc>::parseUnresolvedType() {
3321   if (look() == 'T') {
3322     Node *TP = getDerived().parseTemplateParam();
3323     if (TP == nullptr)
3324       return nullptr;
3325     Subs.push_back(TP);
3326     return TP;
3327   }
3328   if (look() == 'D') {
3329     Node *DT = getDerived().parseDecltype();
3330     if (DT == nullptr)
3331       return nullptr;
3332     Subs.push_back(DT);
3333     return DT;
3334   }
3335   return getDerived().parseSubstitution();
3336 }
3337 
3338 // <base-unresolved-name> ::= <simple-id>                                # unresolved name
3339 //          extension     ::= <operator-name>                            # unresolved operator-function-id
3340 //          extension     ::= <operator-name> <template-args>            # unresolved operator template-id
3341 //                        ::= on <operator-name>                         # unresolved operator-function-id
3342 //                        ::= on <operator-name> <template-args>         # unresolved operator template-id
3343 //                        ::= dn <destructor-name>                       # destructor or pseudo-destructor;
3344 //                                                                         # e.g. ~X or ~X<N-1>
3345 template <typename Derived, typename Alloc>
3346 Node *AbstractManglingParser<Derived, Alloc>::parseBaseUnresolvedName() {
3347   if (std::isdigit(look()))
3348     return getDerived().parseSimpleId();
3349 
3350   if (consumeIf("dn"))
3351     return getDerived().parseDestructorName();
3352 
3353   consumeIf("on");
3354 
3355   Node *Oper = getDerived().parseOperatorName(/*NameState=*/nullptr);
3356   if (Oper == nullptr)
3357     return nullptr;
3358   if (look() == 'I') {
3359     Node *TA = getDerived().parseTemplateArgs();
3360     if (TA == nullptr)
3361       return nullptr;
3362     return make<NameWithTemplateArgs>(Oper, TA);
3363   }
3364   return Oper;
3365 }
3366 
3367 // <unresolved-name>
3368 //  extension        ::= srN <unresolved-type> [<template-args>] <unresolved-qualifier-level>* E <base-unresolved-name>
3369 //                   ::= [gs] <base-unresolved-name>                     # x or (with "gs") ::x
3370 //                   ::= [gs] sr <unresolved-qualifier-level>+ E <base-unresolved-name>
3371 //                                                                       # A::x, N::y, A<T>::z; "gs" means leading "::"
3372 // [gs] has been parsed by caller.
3373 //                   ::= sr <unresolved-type> <base-unresolved-name>     # T::x / decltype(p)::x
3374 //  extension        ::= sr <unresolved-type> <template-args> <base-unresolved-name>
3375 //                                                                       # T::N::x /decltype(p)::N::x
3376 //  (ignored)        ::= srN <unresolved-type>  <unresolved-qualifier-level>+ E <base-unresolved-name>
3377 //
3378 // <unresolved-qualifier-level> ::= <simple-id>
3379 template <typename Derived, typename Alloc>
3380 Node *AbstractManglingParser<Derived, Alloc>::parseUnresolvedName(bool Global) {
3381   Node *SoFar = nullptr;
3382 
3383   // srN <unresolved-type> [<template-args>] <unresolved-qualifier-level>* E <base-unresolved-name>
3384   // srN <unresolved-type>                   <unresolved-qualifier-level>+ E <base-unresolved-name>
3385   if (consumeIf("srN")) {
3386     SoFar = getDerived().parseUnresolvedType();
3387     if (SoFar == nullptr)
3388       return nullptr;
3389 
3390     if (look() == 'I') {
3391       Node *TA = getDerived().parseTemplateArgs();
3392       if (TA == nullptr)
3393         return nullptr;
3394       SoFar = make<NameWithTemplateArgs>(SoFar, TA);
3395       if (!SoFar)
3396         return nullptr;
3397     }
3398 
3399     while (!consumeIf('E')) {
3400       Node *Qual = getDerived().parseSimpleId();
3401       if (Qual == nullptr)
3402         return nullptr;
3403       SoFar = make<QualifiedName>(SoFar, Qual);
3404       if (!SoFar)
3405         return nullptr;
3406     }
3407 
3408     Node *Base = getDerived().parseBaseUnresolvedName();
3409     if (Base == nullptr)
3410       return nullptr;
3411     return make<QualifiedName>(SoFar, Base);
3412   }
3413 
3414   // [gs] <base-unresolved-name>                     # x or (with "gs") ::x
3415   if (!consumeIf("sr")) {
3416     SoFar = getDerived().parseBaseUnresolvedName();
3417     if (SoFar == nullptr)
3418       return nullptr;
3419     if (Global)
3420       SoFar = make<GlobalQualifiedName>(SoFar);
3421     return SoFar;
3422   }
3423 
3424   // [gs] sr <unresolved-qualifier-level>+ E   <base-unresolved-name>
3425   if (std::isdigit(look())) {
3426     do {
3427       Node *Qual = getDerived().parseSimpleId();
3428       if (Qual == nullptr)
3429         return nullptr;
3430       if (SoFar)
3431         SoFar = make<QualifiedName>(SoFar, Qual);
3432       else if (Global)
3433         SoFar = make<GlobalQualifiedName>(Qual);
3434       else
3435         SoFar = Qual;
3436       if (!SoFar)
3437         return nullptr;
3438     } while (!consumeIf('E'));
3439   }
3440   //      sr <unresolved-type>                 <base-unresolved-name>
3441   //      sr <unresolved-type> <template-args> <base-unresolved-name>
3442   else {
3443     SoFar = getDerived().parseUnresolvedType();
3444     if (SoFar == nullptr)
3445       return nullptr;
3446 
3447     if (look() == 'I') {
3448       Node *TA = getDerived().parseTemplateArgs();
3449       if (TA == nullptr)
3450         return nullptr;
3451       SoFar = make<NameWithTemplateArgs>(SoFar, TA);
3452       if (!SoFar)
3453         return nullptr;
3454     }
3455   }
3456 
3457   assert(SoFar != nullptr);
3458 
3459   Node *Base = getDerived().parseBaseUnresolvedName();
3460   if (Base == nullptr)
3461     return nullptr;
3462   return make<QualifiedName>(SoFar, Base);
3463 }
3464 
3465 // <abi-tags> ::= <abi-tag> [<abi-tags>]
3466 // <abi-tag> ::= B <source-name>
3467 template <typename Derived, typename Alloc>
3468 Node *AbstractManglingParser<Derived, Alloc>::parseAbiTags(Node *N) {
3469   while (consumeIf('B')) {
3470     StringView SN = parseBareSourceName();
3471     if (SN.empty())
3472       return nullptr;
3473     N = make<AbiTagAttr>(N, SN);
3474     if (!N)
3475       return nullptr;
3476   }
3477   return N;
3478 }
3479 
3480 // <number> ::= [n] <non-negative decimal integer>
3481 template <typename Alloc, typename Derived>
3482 StringView
3483 AbstractManglingParser<Alloc, Derived>::parseNumber(bool AllowNegative) {
3484   const char *Tmp = First;
3485   if (AllowNegative)
3486     consumeIf('n');
3487   if (numLeft() == 0 || !std::isdigit(*First))
3488     return StringView();
3489   while (numLeft() != 0 && std::isdigit(*First))
3490     ++First;
3491   return StringView(Tmp, First);
3492 }
3493 
3494 // <positive length number> ::= [0-9]*
3495 template <typename Alloc, typename Derived>
3496 bool AbstractManglingParser<Alloc, Derived>::parsePositiveInteger(size_t *Out) {
3497   *Out = 0;
3498   if (look() < '0' || look() > '9')
3499     return true;
3500   while (look() >= '0' && look() <= '9') {
3501     *Out *= 10;
3502     *Out += static_cast<size_t>(consume() - '0');
3503   }
3504   return false;
3505 }
3506 
3507 template <typename Alloc, typename Derived>
3508 StringView AbstractManglingParser<Alloc, Derived>::parseBareSourceName() {
3509   size_t Int = 0;
3510   if (parsePositiveInteger(&Int) || numLeft() < Int)
3511     return StringView();
3512   StringView R(First, First + Int);
3513   First += Int;
3514   return R;
3515 }
3516 
3517 // <function-type> ::= [<CV-qualifiers>] [<exception-spec>] [Dx] F [Y] <bare-function-type> [<ref-qualifier>] E
3518 //
3519 // <exception-spec> ::= Do                # non-throwing exception-specification (e.g., noexcept, throw())
3520 //                  ::= DO <expression> E # computed (instantiation-dependent) noexcept
3521 //                  ::= Dw <type>+ E      # dynamic exception specification with instantiation-dependent types
3522 //
3523 // <ref-qualifier> ::= R                   # & ref-qualifier
3524 // <ref-qualifier> ::= O                   # && ref-qualifier
3525 template <typename Derived, typename Alloc>
3526 Node *AbstractManglingParser<Derived, Alloc>::parseFunctionType() {
3527   Qualifiers CVQuals = parseCVQualifiers();
3528 
3529   Node *ExceptionSpec = nullptr;
3530   if (consumeIf("Do")) {
3531     ExceptionSpec = make<NameType>("noexcept");
3532     if (!ExceptionSpec)
3533       return nullptr;
3534   } else if (consumeIf("DO")) {
3535     Node *E = getDerived().parseExpr();
3536     if (E == nullptr || !consumeIf('E'))
3537       return nullptr;
3538     ExceptionSpec = make<NoexceptSpec>(E);
3539     if (!ExceptionSpec)
3540       return nullptr;
3541   } else if (consumeIf("Dw")) {
3542     size_t SpecsBegin = Names.size();
3543     while (!consumeIf('E')) {
3544       Node *T = getDerived().parseType();
3545       if (T == nullptr)
3546         return nullptr;
3547       Names.push_back(T);
3548     }
3549     ExceptionSpec =
3550       make<DynamicExceptionSpec>(popTrailingNodeArray(SpecsBegin));
3551     if (!ExceptionSpec)
3552       return nullptr;
3553   }
3554 
3555   consumeIf("Dx"); // transaction safe
3556 
3557   if (!consumeIf('F'))
3558     return nullptr;
3559   consumeIf('Y'); // extern "C"
3560   Node *ReturnType = getDerived().parseType();
3561   if (ReturnType == nullptr)
3562     return nullptr;
3563 
3564   FunctionRefQual ReferenceQualifier = FrefQualNone;
3565   size_t ParamsBegin = Names.size();
3566   while (true) {
3567     if (consumeIf('E'))
3568       break;
3569     if (consumeIf('v'))
3570       continue;
3571     if (consumeIf("RE")) {
3572       ReferenceQualifier = FrefQualLValue;
3573       break;
3574     }
3575     if (consumeIf("OE")) {
3576       ReferenceQualifier = FrefQualRValue;
3577       break;
3578     }
3579     Node *T = getDerived().parseType();
3580     if (T == nullptr)
3581       return nullptr;
3582     Names.push_back(T);
3583   }
3584 
3585   NodeArray Params = popTrailingNodeArray(ParamsBegin);
3586   return make<FunctionType>(ReturnType, Params, CVQuals,
3587                             ReferenceQualifier, ExceptionSpec);
3588 }
3589 
3590 // extension:
3591 // <vector-type>           ::= Dv <positive dimension number> _ <extended element type>
3592 //                         ::= Dv [<dimension expression>] _ <element type>
3593 // <extended element type> ::= <element type>
3594 //                         ::= p # AltiVec vector pixel
3595 template <typename Derived, typename Alloc>
3596 Node *AbstractManglingParser<Derived, Alloc>::parseVectorType() {
3597   if (!consumeIf("Dv"))
3598     return nullptr;
3599   if (look() >= '1' && look() <= '9') {
3600     Node *DimensionNumber = make<NameType>(parseNumber());
3601     if (!DimensionNumber)
3602       return nullptr;
3603     if (!consumeIf('_'))
3604       return nullptr;
3605     if (consumeIf('p'))
3606       return make<PixelVectorType>(DimensionNumber);
3607     Node *ElemType = getDerived().parseType();
3608     if (ElemType == nullptr)
3609       return nullptr;
3610     return make<VectorType>(ElemType, DimensionNumber);
3611   }
3612 
3613   if (!consumeIf('_')) {
3614     Node *DimExpr = getDerived().parseExpr();
3615     if (!DimExpr)
3616       return nullptr;
3617     if (!consumeIf('_'))
3618       return nullptr;
3619     Node *ElemType = getDerived().parseType();
3620     if (!ElemType)
3621       return nullptr;
3622     return make<VectorType>(ElemType, DimExpr);
3623   }
3624   Node *ElemType = getDerived().parseType();
3625   if (!ElemType)
3626     return nullptr;
3627   return make<VectorType>(ElemType, /*Dimension=*/nullptr);
3628 }
3629 
3630 // <decltype>  ::= Dt <expression> E  # decltype of an id-expression or class member access (C++0x)
3631 //             ::= DT <expression> E  # decltype of an expression (C++0x)
3632 template <typename Derived, typename Alloc>
3633 Node *AbstractManglingParser<Derived, Alloc>::parseDecltype() {
3634   if (!consumeIf('D'))
3635     return nullptr;
3636   if (!consumeIf('t') && !consumeIf('T'))
3637     return nullptr;
3638   Node *E = getDerived().parseExpr();
3639   if (E == nullptr)
3640     return nullptr;
3641   if (!consumeIf('E'))
3642     return nullptr;
3643   return make<EnclosingExpr>("decltype", E);
3644 }
3645 
3646 // <array-type> ::= A <positive dimension number> _ <element type>
3647 //              ::= A [<dimension expression>] _ <element type>
3648 template <typename Derived, typename Alloc>
3649 Node *AbstractManglingParser<Derived, Alloc>::parseArrayType() {
3650   if (!consumeIf('A'))
3651     return nullptr;
3652 
3653   Node *Dimension = nullptr;
3654 
3655   if (std::isdigit(look())) {
3656     Dimension = make<NameType>(parseNumber());
3657     if (!Dimension)
3658       return nullptr;
3659     if (!consumeIf('_'))
3660       return nullptr;
3661   } else if (!consumeIf('_')) {
3662     Node *DimExpr = getDerived().parseExpr();
3663     if (DimExpr == nullptr)
3664       return nullptr;
3665     if (!consumeIf('_'))
3666       return nullptr;
3667     Dimension = DimExpr;
3668   }
3669 
3670   Node *Ty = getDerived().parseType();
3671   if (Ty == nullptr)
3672     return nullptr;
3673   return make<ArrayType>(Ty, Dimension);
3674 }
3675 
3676 // <pointer-to-member-type> ::= M <class type> <member type>
3677 template <typename Derived, typename Alloc>
3678 Node *AbstractManglingParser<Derived, Alloc>::parsePointerToMemberType() {
3679   if (!consumeIf('M'))
3680     return nullptr;
3681   Node *ClassType = getDerived().parseType();
3682   if (ClassType == nullptr)
3683     return nullptr;
3684   Node *MemberType = getDerived().parseType();
3685   if (MemberType == nullptr)
3686     return nullptr;
3687   return make<PointerToMemberType>(ClassType, MemberType);
3688 }
3689 
3690 // <class-enum-type> ::= <name>     # non-dependent type name, dependent type name, or dependent typename-specifier
3691 //                   ::= Ts <name>  # dependent elaborated type specifier using 'struct' or 'class'
3692 //                   ::= Tu <name>  # dependent elaborated type specifier using 'union'
3693 //                   ::= Te <name>  # dependent elaborated type specifier using 'enum'
3694 template <typename Derived, typename Alloc>
3695 Node *AbstractManglingParser<Derived, Alloc>::parseClassEnumType() {
3696   StringView ElabSpef;
3697   if (consumeIf("Ts"))
3698     ElabSpef = "struct";
3699   else if (consumeIf("Tu"))
3700     ElabSpef = "union";
3701   else if (consumeIf("Te"))
3702     ElabSpef = "enum";
3703 
3704   Node *Name = getDerived().parseName();
3705   if (Name == nullptr)
3706     return nullptr;
3707 
3708   if (!ElabSpef.empty())
3709     return make<ElaboratedTypeSpefType>(ElabSpef, Name);
3710 
3711   return Name;
3712 }
3713 
3714 // <qualified-type>     ::= <qualifiers> <type>
3715 // <qualifiers> ::= <extended-qualifier>* <CV-qualifiers>
3716 // <extended-qualifier> ::= U <source-name> [<template-args>] # vendor extended type qualifier
3717 template <typename Derived, typename Alloc>
3718 Node *AbstractManglingParser<Derived, Alloc>::parseQualifiedType() {
3719   if (consumeIf('U')) {
3720     StringView Qual = parseBareSourceName();
3721     if (Qual.empty())
3722       return nullptr;
3723 
3724     // extension            ::= U <objc-name> <objc-type>  # objc-type<identifier>
3725     if (Qual.startsWith("objcproto")) {
3726       StringView ProtoSourceName = Qual.dropFront(std::strlen("objcproto"));
3727       StringView Proto;
3728       {
3729         ScopedOverride<const char *> SaveFirst(First, ProtoSourceName.begin()),
3730             SaveLast(Last, ProtoSourceName.end());
3731         Proto = parseBareSourceName();
3732       }
3733       if (Proto.empty())
3734         return nullptr;
3735       Node *Child = getDerived().parseQualifiedType();
3736       if (Child == nullptr)
3737         return nullptr;
3738       return make<ObjCProtoName>(Child, Proto);
3739     }
3740 
3741     Node *TA = nullptr;
3742     if (look() == 'I') {
3743       TA = getDerived().parseTemplateArgs();
3744       if (TA == nullptr)
3745         return nullptr;
3746     }
3747 
3748     Node *Child = getDerived().parseQualifiedType();
3749     if (Child == nullptr)
3750       return nullptr;
3751     return make<VendorExtQualType>(Child, Qual, TA);
3752   }
3753 
3754   Qualifiers Quals = parseCVQualifiers();
3755   Node *Ty = getDerived().parseType();
3756   if (Ty == nullptr)
3757     return nullptr;
3758   if (Quals != QualNone)
3759     Ty = make<QualType>(Ty, Quals);
3760   return Ty;
3761 }
3762 
3763 // <type>      ::= <builtin-type>
3764 //             ::= <qualified-type>
3765 //             ::= <function-type>
3766 //             ::= <class-enum-type>
3767 //             ::= <array-type>
3768 //             ::= <pointer-to-member-type>
3769 //             ::= <template-param>
3770 //             ::= <template-template-param> <template-args>
3771 //             ::= <decltype>
3772 //             ::= P <type>        # pointer
3773 //             ::= R <type>        # l-value reference
3774 //             ::= O <type>        # r-value reference (C++11)
3775 //             ::= C <type>        # complex pair (C99)
3776 //             ::= G <type>        # imaginary (C99)
3777 //             ::= <substitution>  # See Compression below
3778 // extension   ::= U <objc-name> <objc-type>  # objc-type<identifier>
3779 // extension   ::= <vector-type> # <vector-type> starts with Dv
3780 //
3781 // <objc-name> ::= <k0 number> objcproto <k1 number> <identifier>  # k0 = 9 + <number of digits in k1> + k1
3782 // <objc-type> ::= <source-name>  # PU<11+>objcproto 11objc_object<source-name> 11objc_object -> id<source-name>
3783 template <typename Derived, typename Alloc>
3784 Node *AbstractManglingParser<Derived, Alloc>::parseType() {
3785   Node *Result = nullptr;
3786 
3787   switch (look()) {
3788   //             ::= <qualified-type>
3789   case 'r':
3790   case 'V':
3791   case 'K': {
3792     unsigned AfterQuals = 0;
3793     if (look(AfterQuals) == 'r') ++AfterQuals;
3794     if (look(AfterQuals) == 'V') ++AfterQuals;
3795     if (look(AfterQuals) == 'K') ++AfterQuals;
3796 
3797     if (look(AfterQuals) == 'F' ||
3798         (look(AfterQuals) == 'D' &&
3799          (look(AfterQuals + 1) == 'o' || look(AfterQuals + 1) == 'O' ||
3800           look(AfterQuals + 1) == 'w' || look(AfterQuals + 1) == 'x'))) {
3801       Result = getDerived().parseFunctionType();
3802       break;
3803     }
3804     DEMANGLE_FALLTHROUGH;
3805   }
3806   case 'U': {
3807     Result = getDerived().parseQualifiedType();
3808     break;
3809   }
3810   // <builtin-type> ::= v    # void
3811   case 'v':
3812     ++First;
3813     return make<NameType>("void");
3814   //                ::= w    # wchar_t
3815   case 'w':
3816     ++First;
3817     return make<NameType>("wchar_t");
3818   //                ::= b    # bool
3819   case 'b':
3820     ++First;
3821     return make<NameType>("bool");
3822   //                ::= c    # char
3823   case 'c':
3824     ++First;
3825     return make<NameType>("char");
3826   //                ::= a    # signed char
3827   case 'a':
3828     ++First;
3829     return make<NameType>("signed char");
3830   //                ::= h    # unsigned char
3831   case 'h':
3832     ++First;
3833     return make<NameType>("unsigned char");
3834   //                ::= s    # short
3835   case 's':
3836     ++First;
3837     return make<NameType>("short");
3838   //                ::= t    # unsigned short
3839   case 't':
3840     ++First;
3841     return make<NameType>("unsigned short");
3842   //                ::= i    # int
3843   case 'i':
3844     ++First;
3845     return make<NameType>("int");
3846   //                ::= j    # unsigned int
3847   case 'j':
3848     ++First;
3849     return make<NameType>("unsigned int");
3850   //                ::= l    # long
3851   case 'l':
3852     ++First;
3853     return make<NameType>("long");
3854   //                ::= m    # unsigned long
3855   case 'm':
3856     ++First;
3857     return make<NameType>("unsigned long");
3858   //                ::= x    # long long, __int64
3859   case 'x':
3860     ++First;
3861     return make<NameType>("long long");
3862   //                ::= y    # unsigned long long, __int64
3863   case 'y':
3864     ++First;
3865     return make<NameType>("unsigned long long");
3866   //                ::= n    # __int128
3867   case 'n':
3868     ++First;
3869     return make<NameType>("__int128");
3870   //                ::= o    # unsigned __int128
3871   case 'o':
3872     ++First;
3873     return make<NameType>("unsigned __int128");
3874   //                ::= f    # float
3875   case 'f':
3876     ++First;
3877     return make<NameType>("float");
3878   //                ::= d    # double
3879   case 'd':
3880     ++First;
3881     return make<NameType>("double");
3882   //                ::= e    # long double, __float80
3883   case 'e':
3884     ++First;
3885     return make<NameType>("long double");
3886   //                ::= g    # __float128
3887   case 'g':
3888     ++First;
3889     return make<NameType>("__float128");
3890   //                ::= z    # ellipsis
3891   case 'z':
3892     ++First;
3893     return make<NameType>("...");
3894 
3895   // <builtin-type> ::= u <source-name>    # vendor extended type
3896   case 'u': {
3897     ++First;
3898     StringView Res = parseBareSourceName();
3899     if (Res.empty())
3900       return nullptr;
3901     // Typically, <builtin-type>s are not considered substitution candidates,
3902     // but the exception to that exception is vendor extended types (Itanium C++
3903     // ABI 5.9.1).
3904     Result = make<NameType>(Res);
3905     break;
3906   }
3907   case 'D':
3908     switch (look(1)) {
3909     //                ::= Dd   # IEEE 754r decimal floating point (64 bits)
3910     case 'd':
3911       First += 2;
3912       return make<NameType>("decimal64");
3913     //                ::= De   # IEEE 754r decimal floating point (128 bits)
3914     case 'e':
3915       First += 2;
3916       return make<NameType>("decimal128");
3917     //                ::= Df   # IEEE 754r decimal floating point (32 bits)
3918     case 'f':
3919       First += 2;
3920       return make<NameType>("decimal32");
3921     //                ::= Dh   # IEEE 754r half-precision floating point (16 bits)
3922     case 'h':
3923       First += 2;
3924       return make<NameType>("half");
3925     //                ::= DF <number> _ # ISO/IEC TS 18661 binary floating point (N bits)
3926     case 'F': {
3927       First += 2;
3928       Node *DimensionNumber = make<NameType>(parseNumber());
3929       if (!DimensionNumber)
3930         return nullptr;
3931       if (!consumeIf('_'))
3932         return nullptr;
3933       return make<BinaryFPType>(DimensionNumber);
3934     }
3935     //                ::= DB <number> _                             # C23 signed _BitInt(N)
3936     //                ::= DB <instantiation-dependent expression> _ # C23 signed _BitInt(N)
3937     //                ::= DU <number> _                             # C23 unsigned _BitInt(N)
3938     //                ::= DU <instantiation-dependent expression> _ # C23 unsigned _BitInt(N)
3939     case 'B':
3940     case 'U': {
3941       bool Signed = look(1) == 'B';
3942       First += 2;
3943       Node *Size = std::isdigit(look()) ? make<NameType>(parseNumber())
3944                                         : getDerived().parseExpr();
3945       if (!Size)
3946         return nullptr;
3947       if (!consumeIf('_'))
3948         return nullptr;
3949       return make<BitIntType>(Size, Signed);
3950     }
3951     //                ::= Di   # char32_t
3952     case 'i':
3953       First += 2;
3954       return make<NameType>("char32_t");
3955     //                ::= Ds   # char16_t
3956     case 's':
3957       First += 2;
3958       return make<NameType>("char16_t");
3959     //                ::= Du   # char8_t (C++2a, not yet in the Itanium spec)
3960     case 'u':
3961       First += 2;
3962       return make<NameType>("char8_t");
3963     //                ::= Da   # auto (in dependent new-expressions)
3964     case 'a':
3965       First += 2;
3966       return make<NameType>("auto");
3967     //                ::= Dc   # decltype(auto)
3968     case 'c':
3969       First += 2;
3970       return make<NameType>("decltype(auto)");
3971     //                ::= Dn   # std::nullptr_t (i.e., decltype(nullptr))
3972     case 'n':
3973       First += 2;
3974       return make<NameType>("std::nullptr_t");
3975 
3976     //             ::= <decltype>
3977     case 't':
3978     case 'T': {
3979       Result = getDerived().parseDecltype();
3980       break;
3981     }
3982     // extension   ::= <vector-type> # <vector-type> starts with Dv
3983     case 'v': {
3984       Result = getDerived().parseVectorType();
3985       break;
3986     }
3987     //           ::= Dp <type>       # pack expansion (C++0x)
3988     case 'p': {
3989       First += 2;
3990       Node *Child = getDerived().parseType();
3991       if (!Child)
3992         return nullptr;
3993       Result = make<ParameterPackExpansion>(Child);
3994       break;
3995     }
3996     // Exception specifier on a function type.
3997     case 'o':
3998     case 'O':
3999     case 'w':
4000     // Transaction safe function type.
4001     case 'x':
4002       Result = getDerived().parseFunctionType();
4003       break;
4004     }
4005     break;
4006   //             ::= <function-type>
4007   case 'F': {
4008     Result = getDerived().parseFunctionType();
4009     break;
4010   }
4011   //             ::= <array-type>
4012   case 'A': {
4013     Result = getDerived().parseArrayType();
4014     break;
4015   }
4016   //             ::= <pointer-to-member-type>
4017   case 'M': {
4018     Result = getDerived().parsePointerToMemberType();
4019     break;
4020   }
4021   //             ::= <template-param>
4022   case 'T': {
4023     // This could be an elaborate type specifier on a <class-enum-type>.
4024     if (look(1) == 's' || look(1) == 'u' || look(1) == 'e') {
4025       Result = getDerived().parseClassEnumType();
4026       break;
4027     }
4028 
4029     Result = getDerived().parseTemplateParam();
4030     if (Result == nullptr)
4031       return nullptr;
4032 
4033     // Result could be either of:
4034     //   <type>        ::= <template-param>
4035     //   <type>        ::= <template-template-param> <template-args>
4036     //
4037     //   <template-template-param> ::= <template-param>
4038     //                             ::= <substitution>
4039     //
4040     // If this is followed by some <template-args>, and we're permitted to
4041     // parse them, take the second production.
4042 
4043     if (TryToParseTemplateArgs && look() == 'I') {
4044       Node *TA = getDerived().parseTemplateArgs();
4045       if (TA == nullptr)
4046         return nullptr;
4047       Result = make<NameWithTemplateArgs>(Result, TA);
4048     }
4049     break;
4050   }
4051   //             ::= P <type>        # pointer
4052   case 'P': {
4053     ++First;
4054     Node *Ptr = getDerived().parseType();
4055     if (Ptr == nullptr)
4056       return nullptr;
4057     Result = make<PointerType>(Ptr);
4058     break;
4059   }
4060   //             ::= R <type>        # l-value reference
4061   case 'R': {
4062     ++First;
4063     Node *Ref = getDerived().parseType();
4064     if (Ref == nullptr)
4065       return nullptr;
4066     Result = make<ReferenceType>(Ref, ReferenceKind::LValue);
4067     break;
4068   }
4069   //             ::= O <type>        # r-value reference (C++11)
4070   case 'O': {
4071     ++First;
4072     Node *Ref = getDerived().parseType();
4073     if (Ref == nullptr)
4074       return nullptr;
4075     Result = make<ReferenceType>(Ref, ReferenceKind::RValue);
4076     break;
4077   }
4078   //             ::= C <type>        # complex pair (C99)
4079   case 'C': {
4080     ++First;
4081     Node *P = getDerived().parseType();
4082     if (P == nullptr)
4083       return nullptr;
4084     Result = make<PostfixQualifiedType>(P, " complex");
4085     break;
4086   }
4087   //             ::= G <type>        # imaginary (C99)
4088   case 'G': {
4089     ++First;
4090     Node *P = getDerived().parseType();
4091     if (P == nullptr)
4092       return P;
4093     Result = make<PostfixQualifiedType>(P, " imaginary");
4094     break;
4095   }
4096   //             ::= <substitution>  # See Compression below
4097   case 'S': {
4098     if (look(1) != 't') {
4099       bool IsSubst = false;
4100       Result = getDerived().parseUnscopedName(nullptr, &IsSubst);
4101       if (!Result)
4102         return nullptr;
4103 
4104       // Sub could be either of:
4105       //   <type>        ::= <substitution>
4106       //   <type>        ::= <template-template-param> <template-args>
4107       //
4108       //   <template-template-param> ::= <template-param>
4109       //                             ::= <substitution>
4110       //
4111       // If this is followed by some <template-args>, and we're permitted to
4112       // parse them, take the second production.
4113 
4114       if (look() == 'I' && (!IsSubst || TryToParseTemplateArgs)) {
4115         if (!IsSubst)
4116           Subs.push_back(Result);
4117         Node *TA = getDerived().parseTemplateArgs();
4118         if (TA == nullptr)
4119           return nullptr;
4120         Result = make<NameWithTemplateArgs>(Result, TA);
4121       } else if (IsSubst) {
4122         // If all we parsed was a substitution, don't re-insert into the
4123         // substitution table.
4124         return Result;
4125       }
4126       break;
4127     }
4128     DEMANGLE_FALLTHROUGH;
4129   }
4130   //        ::= <class-enum-type>
4131   default: {
4132     Result = getDerived().parseClassEnumType();
4133     break;
4134   }
4135   }
4136 
4137   // If we parsed a type, insert it into the substitution table. Note that all
4138   // <builtin-type>s and <substitution>s have already bailed out, because they
4139   // don't get substitutions.
4140   if (Result != nullptr)
4141     Subs.push_back(Result);
4142   return Result;
4143 }
4144 
4145 template <typename Derived, typename Alloc>
4146 Node *AbstractManglingParser<Derived, Alloc>::parsePrefixExpr(StringView Kind,
4147                                                               Node::Prec Prec) {
4148   Node *E = getDerived().parseExpr();
4149   if (E == nullptr)
4150     return nullptr;
4151   return make<PrefixExpr>(Kind, E, Prec);
4152 }
4153 
4154 template <typename Derived, typename Alloc>
4155 Node *AbstractManglingParser<Derived, Alloc>::parseBinaryExpr(StringView Kind,
4156                                                               Node::Prec Prec) {
4157   Node *LHS = getDerived().parseExpr();
4158   if (LHS == nullptr)
4159     return nullptr;
4160   Node *RHS = getDerived().parseExpr();
4161   if (RHS == nullptr)
4162     return nullptr;
4163   return make<BinaryExpr>(LHS, Kind, RHS, Prec);
4164 }
4165 
4166 template <typename Derived, typename Alloc>
4167 Node *
4168 AbstractManglingParser<Derived, Alloc>::parseIntegerLiteral(StringView Lit) {
4169   StringView Tmp = parseNumber(true);
4170   if (!Tmp.empty() && consumeIf('E'))
4171     return make<IntegerLiteral>(Lit, Tmp);
4172   return nullptr;
4173 }
4174 
4175 // <CV-Qualifiers> ::= [r] [V] [K]
4176 template <typename Alloc, typename Derived>
4177 Qualifiers AbstractManglingParser<Alloc, Derived>::parseCVQualifiers() {
4178   Qualifiers CVR = QualNone;
4179   if (consumeIf('r'))
4180     CVR |= QualRestrict;
4181   if (consumeIf('V'))
4182     CVR |= QualVolatile;
4183   if (consumeIf('K'))
4184     CVR |= QualConst;
4185   return CVR;
4186 }
4187 
4188 // <function-param> ::= fp <top-level CV-Qualifiers> _                                     # L == 0, first parameter
4189 //                  ::= fp <top-level CV-Qualifiers> <parameter-2 non-negative number> _   # L == 0, second and later parameters
4190 //                  ::= fL <L-1 non-negative number> p <top-level CV-Qualifiers> _         # L > 0, first parameter
4191 //                  ::= fL <L-1 non-negative number> p <top-level CV-Qualifiers> <parameter-2 non-negative number> _   # L > 0, second and later parameters
4192 //                  ::= fpT      # 'this' expression (not part of standard?)
4193 template <typename Derived, typename Alloc>
4194 Node *AbstractManglingParser<Derived, Alloc>::parseFunctionParam() {
4195   if (consumeIf("fpT"))
4196     return make<NameType>("this");
4197   if (consumeIf("fp")) {
4198     parseCVQualifiers();
4199     StringView Num = parseNumber();
4200     if (!consumeIf('_'))
4201       return nullptr;
4202     return make<FunctionParam>(Num);
4203   }
4204   if (consumeIf("fL")) {
4205     if (parseNumber().empty())
4206       return nullptr;
4207     if (!consumeIf('p'))
4208       return nullptr;
4209     parseCVQualifiers();
4210     StringView Num = parseNumber();
4211     if (!consumeIf('_'))
4212       return nullptr;
4213     return make<FunctionParam>(Num);
4214   }
4215   return nullptr;
4216 }
4217 
4218 // cv <type> <expression>                               # conversion with one argument
4219 // cv <type> _ <expression>* E                          # conversion with a different number of arguments
4220 template <typename Derived, typename Alloc>
4221 Node *AbstractManglingParser<Derived, Alloc>::parseConversionExpr() {
4222   if (!consumeIf("cv"))
4223     return nullptr;
4224   Node *Ty;
4225   {
4226     ScopedOverride<bool> SaveTemp(TryToParseTemplateArgs, false);
4227     Ty = getDerived().parseType();
4228   }
4229 
4230   if (Ty == nullptr)
4231     return nullptr;
4232 
4233   if (consumeIf('_')) {
4234     size_t ExprsBegin = Names.size();
4235     while (!consumeIf('E')) {
4236       Node *E = getDerived().parseExpr();
4237       if (E == nullptr)
4238         return E;
4239       Names.push_back(E);
4240     }
4241     NodeArray Exprs = popTrailingNodeArray(ExprsBegin);
4242     return make<ConversionExpr>(Ty, Exprs);
4243   }
4244 
4245   Node *E[1] = {getDerived().parseExpr()};
4246   if (E[0] == nullptr)
4247     return nullptr;
4248   return make<ConversionExpr>(Ty, makeNodeArray(E, E + 1));
4249 }
4250 
4251 // <expr-primary> ::= L <type> <value number> E                          # integer literal
4252 //                ::= L <type> <value float> E                           # floating literal
4253 //                ::= L <string type> E                                  # string literal
4254 //                ::= L <nullptr type> E                                 # nullptr literal (i.e., "LDnE")
4255 //                ::= L <lambda type> E                                  # lambda expression
4256 // FIXME:         ::= L <type> <real-part float> _ <imag-part float> E   # complex floating point literal (C 2000)
4257 //                ::= L <mangled-name> E                                 # external name
4258 template <typename Derived, typename Alloc>
4259 Node *AbstractManglingParser<Derived, Alloc>::parseExprPrimary() {
4260   if (!consumeIf('L'))
4261     return nullptr;
4262   switch (look()) {
4263   case 'w':
4264     ++First;
4265     return getDerived().parseIntegerLiteral("wchar_t");
4266   case 'b':
4267     if (consumeIf("b0E"))
4268       return make<BoolExpr>(0);
4269     if (consumeIf("b1E"))
4270       return make<BoolExpr>(1);
4271     return nullptr;
4272   case 'c':
4273     ++First;
4274     return getDerived().parseIntegerLiteral("char");
4275   case 'a':
4276     ++First;
4277     return getDerived().parseIntegerLiteral("signed char");
4278   case 'h':
4279     ++First;
4280     return getDerived().parseIntegerLiteral("unsigned char");
4281   case 's':
4282     ++First;
4283     return getDerived().parseIntegerLiteral("short");
4284   case 't':
4285     ++First;
4286     return getDerived().parseIntegerLiteral("unsigned short");
4287   case 'i':
4288     ++First;
4289     return getDerived().parseIntegerLiteral("");
4290   case 'j':
4291     ++First;
4292     return getDerived().parseIntegerLiteral("u");
4293   case 'l':
4294     ++First;
4295     return getDerived().parseIntegerLiteral("l");
4296   case 'm':
4297     ++First;
4298     return getDerived().parseIntegerLiteral("ul");
4299   case 'x':
4300     ++First;
4301     return getDerived().parseIntegerLiteral("ll");
4302   case 'y':
4303     ++First;
4304     return getDerived().parseIntegerLiteral("ull");
4305   case 'n':
4306     ++First;
4307     return getDerived().parseIntegerLiteral("__int128");
4308   case 'o':
4309     ++First;
4310     return getDerived().parseIntegerLiteral("unsigned __int128");
4311   case 'f':
4312     ++First;
4313     return getDerived().template parseFloatingLiteral<float>();
4314   case 'd':
4315     ++First;
4316     return getDerived().template parseFloatingLiteral<double>();
4317   case 'e':
4318     ++First;
4319 #if defined(__powerpc__) || defined(__s390__)
4320     // Handle cases where long doubles encoded with e have the same size
4321     // and representation as doubles.
4322     return getDerived().template parseFloatingLiteral<double>();
4323 #else
4324     return getDerived().template parseFloatingLiteral<long double>();
4325 #endif
4326   case '_':
4327     if (consumeIf("_Z")) {
4328       Node *R = getDerived().parseEncoding();
4329       if (R != nullptr && consumeIf('E'))
4330         return R;
4331     }
4332     return nullptr;
4333   case 'A': {
4334     Node *T = getDerived().parseType();
4335     if (T == nullptr)
4336       return nullptr;
4337     // FIXME: We need to include the string contents in the mangling.
4338     if (consumeIf('E'))
4339       return make<StringLiteral>(T);
4340     return nullptr;
4341   }
4342   case 'D':
4343     if (consumeIf("DnE"))
4344       return make<NameType>("nullptr");
4345     return nullptr;
4346   case 'T':
4347     // Invalid mangled name per
4348     //   http://sourcerytools.com/pipermail/cxx-abi-dev/2011-August/002422.html
4349     return nullptr;
4350   case 'U': {
4351     // FIXME: Should we support LUb... for block literals?
4352     if (look(1) != 'l')
4353       return nullptr;
4354     Node *T = parseUnnamedTypeName(nullptr);
4355     if (!T || !consumeIf('E'))
4356       return nullptr;
4357     return make<LambdaExpr>(T);
4358   }
4359   default: {
4360     // might be named type
4361     Node *T = getDerived().parseType();
4362     if (T == nullptr)
4363       return nullptr;
4364     StringView N = parseNumber(/*AllowNegative=*/true);
4365     if (N.empty())
4366       return nullptr;
4367     if (!consumeIf('E'))
4368       return nullptr;
4369     return make<EnumLiteral>(T, N);
4370   }
4371   }
4372 }
4373 
4374 // <braced-expression> ::= <expression>
4375 //                     ::= di <field source-name> <braced-expression>    # .name = expr
4376 //                     ::= dx <index expression> <braced-expression>     # [expr] = expr
4377 //                     ::= dX <range begin expression> <range end expression> <braced-expression>
4378 template <typename Derived, typename Alloc>
4379 Node *AbstractManglingParser<Derived, Alloc>::parseBracedExpr() {
4380   if (look() == 'd') {
4381     switch (look(1)) {
4382     case 'i': {
4383       First += 2;
4384       Node *Field = getDerived().parseSourceName(/*NameState=*/nullptr);
4385       if (Field == nullptr)
4386         return nullptr;
4387       Node *Init = getDerived().parseBracedExpr();
4388       if (Init == nullptr)
4389         return nullptr;
4390       return make<BracedExpr>(Field, Init, /*isArray=*/false);
4391     }
4392     case 'x': {
4393       First += 2;
4394       Node *Index = getDerived().parseExpr();
4395       if (Index == nullptr)
4396         return nullptr;
4397       Node *Init = getDerived().parseBracedExpr();
4398       if (Init == nullptr)
4399         return nullptr;
4400       return make<BracedExpr>(Index, Init, /*isArray=*/true);
4401     }
4402     case 'X': {
4403       First += 2;
4404       Node *RangeBegin = getDerived().parseExpr();
4405       if (RangeBegin == nullptr)
4406         return nullptr;
4407       Node *RangeEnd = getDerived().parseExpr();
4408       if (RangeEnd == nullptr)
4409         return nullptr;
4410       Node *Init = getDerived().parseBracedExpr();
4411       if (Init == nullptr)
4412         return nullptr;
4413       return make<BracedRangeExpr>(RangeBegin, RangeEnd, Init);
4414     }
4415     }
4416   }
4417   return getDerived().parseExpr();
4418 }
4419 
4420 // (not yet in the spec)
4421 // <fold-expr> ::= fL <binary-operator-name> <expression> <expression>
4422 //             ::= fR <binary-operator-name> <expression> <expression>
4423 //             ::= fl <binary-operator-name> <expression>
4424 //             ::= fr <binary-operator-name> <expression>
4425 template <typename Derived, typename Alloc>
4426 Node *AbstractManglingParser<Derived, Alloc>::parseFoldExpr() {
4427   if (!consumeIf('f'))
4428     return nullptr;
4429 
4430   bool IsLeftFold = false, HasInitializer = false;
4431   switch (look()) {
4432   default:
4433     return nullptr;
4434   case 'L':
4435     IsLeftFold = true;
4436     HasInitializer = true;
4437     break;
4438   case 'R':
4439     HasInitializer = true;
4440     break;
4441   case 'l':
4442     IsLeftFold = true;
4443     break;
4444   case 'r':
4445     break;
4446   }
4447   ++First;
4448 
4449   const auto *Op = parseOperatorEncoding();
4450   if (!Op || Op->getKind() != OperatorInfo::Binary)
4451     return nullptr;
4452 
4453   Node *Pack = getDerived().parseExpr();
4454   if (Pack == nullptr)
4455     return nullptr;
4456 
4457   Node *Init = nullptr;
4458   if (HasInitializer) {
4459     Init = getDerived().parseExpr();
4460     if (Init == nullptr)
4461       return nullptr;
4462   }
4463 
4464   if (IsLeftFold && Init)
4465     std::swap(Pack, Init);
4466 
4467   return make<FoldExpr>(IsLeftFold, Op->getSymbol(), Pack, Init);
4468 }
4469 
4470 // <expression> ::= mc <parameter type> <expr> [<offset number>] E
4471 //
4472 // Not yet in the spec: https://github.com/itanium-cxx-abi/cxx-abi/issues/47
4473 template <typename Derived, typename Alloc>
4474 Node *
4475 AbstractManglingParser<Derived, Alloc>::parsePointerToMemberConversionExpr(
4476     Node::Prec Prec) {
4477   Node *Ty = getDerived().parseType();
4478   if (!Ty)
4479     return nullptr;
4480   Node *Expr = getDerived().parseExpr();
4481   if (!Expr)
4482     return nullptr;
4483   StringView Offset = getDerived().parseNumber(true);
4484   if (!consumeIf('E'))
4485     return nullptr;
4486   return make<PointerToMemberConversionExpr>(Ty, Expr, Offset, Prec);
4487 }
4488 
4489 // <expression> ::= so <referent type> <expr> [<offset number>] <union-selector>* [p] E
4490 // <union-selector> ::= _ [<number>]
4491 //
4492 // Not yet in the spec: https://github.com/itanium-cxx-abi/cxx-abi/issues/47
4493 template <typename Derived, typename Alloc>
4494 Node *AbstractManglingParser<Derived, Alloc>::parseSubobjectExpr() {
4495   Node *Ty = getDerived().parseType();
4496   if (!Ty)
4497     return nullptr;
4498   Node *Expr = getDerived().parseExpr();
4499   if (!Expr)
4500     return nullptr;
4501   StringView Offset = getDerived().parseNumber(true);
4502   size_t SelectorsBegin = Names.size();
4503   while (consumeIf('_')) {
4504     Node *Selector = make<NameType>(parseNumber());
4505     if (!Selector)
4506       return nullptr;
4507     Names.push_back(Selector);
4508   }
4509   bool OnePastTheEnd = consumeIf('p');
4510   if (!consumeIf('E'))
4511     return nullptr;
4512   return make<SubobjectExpr>(
4513       Ty, Expr, Offset, popTrailingNodeArray(SelectorsBegin), OnePastTheEnd);
4514 }
4515 
4516 // <expression> ::= <unary operator-name> <expression>
4517 //              ::= <binary operator-name> <expression> <expression>
4518 //              ::= <ternary operator-name> <expression> <expression> <expression>
4519 //              ::= cl <expression>+ E                                   # call
4520 //              ::= cv <type> <expression>                               # conversion with one argument
4521 //              ::= cv <type> _ <expression>* E                          # conversion with a different number of arguments
4522 //              ::= [gs] nw <expression>* _ <type> E                     # new (expr-list) type
4523 //              ::= [gs] nw <expression>* _ <type> <initializer>         # new (expr-list) type (init)
4524 //              ::= [gs] na <expression>* _ <type> E                     # new[] (expr-list) type
4525 //              ::= [gs] na <expression>* _ <type> <initializer>         # new[] (expr-list) type (init)
4526 //              ::= [gs] dl <expression>                                 # delete expression
4527 //              ::= [gs] da <expression>                                 # delete[] expression
4528 //              ::= pp_ <expression>                                     # prefix ++
4529 //              ::= mm_ <expression>                                     # prefix --
4530 //              ::= ti <type>                                            # typeid (type)
4531 //              ::= te <expression>                                      # typeid (expression)
4532 //              ::= dc <type> <expression>                               # dynamic_cast<type> (expression)
4533 //              ::= sc <type> <expression>                               # static_cast<type> (expression)
4534 //              ::= cc <type> <expression>                               # const_cast<type> (expression)
4535 //              ::= rc <type> <expression>                               # reinterpret_cast<type> (expression)
4536 //              ::= st <type>                                            # sizeof (a type)
4537 //              ::= sz <expression>                                      # sizeof (an expression)
4538 //              ::= at <type>                                            # alignof (a type)
4539 //              ::= az <expression>                                      # alignof (an expression)
4540 //              ::= nx <expression>                                      # noexcept (expression)
4541 //              ::= <template-param>
4542 //              ::= <function-param>
4543 //              ::= dt <expression> <unresolved-name>                    # expr.name
4544 //              ::= pt <expression> <unresolved-name>                    # expr->name
4545 //              ::= ds <expression> <expression>                         # expr.*expr
4546 //              ::= sZ <template-param>                                  # size of a parameter pack
4547 //              ::= sZ <function-param>                                  # size of a function parameter pack
4548 //              ::= sP <template-arg>* E                                 # sizeof...(T), size of a captured template parameter pack from an alias template
4549 //              ::= sp <expression>                                      # pack expansion
4550 //              ::= tw <expression>                                      # throw expression
4551 //              ::= tr                                                   # throw with no operand (rethrow)
4552 //              ::= <unresolved-name>                                    # f(p), N::f(p), ::f(p),
4553 //                                                                       # freestanding dependent name (e.g., T::x),
4554 //                                                                       # objectless nonstatic member reference
4555 //              ::= fL <binary-operator-name> <expression> <expression>
4556 //              ::= fR <binary-operator-name> <expression> <expression>
4557 //              ::= fl <binary-operator-name> <expression>
4558 //              ::= fr <binary-operator-name> <expression>
4559 //              ::= <expr-primary>
4560 template <typename Derived, typename Alloc>
4561 Node *AbstractManglingParser<Derived, Alloc>::parseExpr() {
4562   bool Global = consumeIf("gs");
4563 
4564   const auto *Op = parseOperatorEncoding();
4565   if (Op) {
4566     auto Sym = Op->getSymbol();
4567     switch (Op->getKind()) {
4568     case OperatorInfo::Binary:
4569       // Binary operator: lhs @ rhs
4570       return getDerived().parseBinaryExpr(Sym, Op->getPrecedence());
4571     case OperatorInfo::Prefix:
4572       // Prefix unary operator: @ expr
4573       return getDerived().parsePrefixExpr(Sym, Op->getPrecedence());
4574     case OperatorInfo::Postfix: {
4575       // Postfix unary operator: expr @
4576       if (consumeIf('_'))
4577         return getDerived().parsePrefixExpr(Sym, Op->getPrecedence());
4578       Node *Ex = getDerived().parseExpr();
4579       if (Ex == nullptr)
4580         return nullptr;
4581       return make<PostfixExpr>(Ex, Sym, Op->getPrecedence());
4582     }
4583     case OperatorInfo::Array: {
4584       // Array Index:  lhs [ rhs ]
4585       Node *Base = getDerived().parseExpr();
4586       if (Base == nullptr)
4587         return nullptr;
4588       Node *Index = getDerived().parseExpr();
4589       if (Index == nullptr)
4590         return nullptr;
4591       return make<ArraySubscriptExpr>(Base, Index, Op->getPrecedence());
4592     }
4593     case OperatorInfo::Member: {
4594       // Member access lhs @ rhs
4595       Node *LHS = getDerived().parseExpr();
4596       if (LHS == nullptr)
4597         return nullptr;
4598       Node *RHS = getDerived().parseExpr();
4599       if (RHS == nullptr)
4600         return nullptr;
4601       return make<MemberExpr>(LHS, Sym, RHS, Op->getPrecedence());
4602     }
4603     case OperatorInfo::New: {
4604       // New
4605       // # new (expr-list) type [(init)]
4606       // [gs] nw <expression>* _ <type> [pi <expression>*] E
4607       // # new[] (expr-list) type [(init)]
4608       // [gs] na <expression>* _ <type> [pi <expression>*] E
4609       size_t Exprs = Names.size();
4610       while (!consumeIf('_')) {
4611         Node *Ex = getDerived().parseExpr();
4612         if (Ex == nullptr)
4613           return nullptr;
4614         Names.push_back(Ex);
4615       }
4616       NodeArray ExprList = popTrailingNodeArray(Exprs);
4617       Node *Ty = getDerived().parseType();
4618       if (Ty == nullptr)
4619         return nullptr;
4620       bool HaveInits = consumeIf("pi");
4621       size_t InitsBegin = Names.size();
4622       while (!consumeIf('E')) {
4623         if (!HaveInits)
4624           return nullptr;
4625         Node *Init = getDerived().parseExpr();
4626         if (Init == nullptr)
4627           return Init;
4628         Names.push_back(Init);
4629       }
4630       NodeArray Inits = popTrailingNodeArray(InitsBegin);
4631       return make<NewExpr>(ExprList, Ty, Inits, Global,
4632                            /*IsArray=*/Op->getFlag(), Op->getPrecedence());
4633     }
4634     case OperatorInfo::Del: {
4635       // Delete
4636       Node *Ex = getDerived().parseExpr();
4637       if (Ex == nullptr)
4638         return nullptr;
4639       return make<DeleteExpr>(Ex, Global, /*IsArray=*/Op->getFlag(),
4640                               Op->getPrecedence());
4641     }
4642     case OperatorInfo::Call: {
4643       // Function Call
4644       Node *Callee = getDerived().parseExpr();
4645       if (Callee == nullptr)
4646         return nullptr;
4647       size_t ExprsBegin = Names.size();
4648       while (!consumeIf('E')) {
4649         Node *E = getDerived().parseExpr();
4650         if (E == nullptr)
4651           return nullptr;
4652         Names.push_back(E);
4653       }
4654       return make<CallExpr>(Callee, popTrailingNodeArray(ExprsBegin),
4655                             Op->getPrecedence());
4656     }
4657     case OperatorInfo::CCast: {
4658       // C Cast: (type)expr
4659       Node *Ty;
4660       {
4661         ScopedOverride<bool> SaveTemp(TryToParseTemplateArgs, false);
4662         Ty = getDerived().parseType();
4663       }
4664       if (Ty == nullptr)
4665         return nullptr;
4666 
4667       size_t ExprsBegin = Names.size();
4668       bool IsMany = consumeIf('_');
4669       while (!consumeIf('E')) {
4670         Node *E = getDerived().parseExpr();
4671         if (E == nullptr)
4672           return E;
4673         Names.push_back(E);
4674         if (!IsMany)
4675           break;
4676       }
4677       NodeArray Exprs = popTrailingNodeArray(ExprsBegin);
4678       if (!IsMany && Exprs.size() != 1)
4679         return nullptr;
4680       return make<ConversionExpr>(Ty, Exprs, Op->getPrecedence());
4681     }
4682     case OperatorInfo::Conditional: {
4683       // Conditional operator: expr ? expr : expr
4684       Node *Cond = getDerived().parseExpr();
4685       if (Cond == nullptr)
4686         return nullptr;
4687       Node *LHS = getDerived().parseExpr();
4688       if (LHS == nullptr)
4689         return nullptr;
4690       Node *RHS = getDerived().parseExpr();
4691       if (RHS == nullptr)
4692         return nullptr;
4693       return make<ConditionalExpr>(Cond, LHS, RHS, Op->getPrecedence());
4694     }
4695     case OperatorInfo::NamedCast: {
4696       // Named cast operation, @<type>(expr)
4697       Node *Ty = getDerived().parseType();
4698       if (Ty == nullptr)
4699         return nullptr;
4700       Node *Ex = getDerived().parseExpr();
4701       if (Ex == nullptr)
4702         return nullptr;
4703       return make<CastExpr>(Sym, Ty, Ex, Op->getPrecedence());
4704     }
4705     case OperatorInfo::OfIdOp: {
4706       // [sizeof/alignof/typeid] ( <type>|<expr> )
4707       Node *Arg =
4708           Op->getFlag() ? getDerived().parseType() : getDerived().parseExpr();
4709       if (!Arg)
4710         return nullptr;
4711       return make<EnclosingExpr>(Sym, Arg, Op->getPrecedence());
4712     }
4713     case OperatorInfo::NameOnly: {
4714       // Not valid as an expression operand.
4715       return nullptr;
4716     }
4717     }
4718     DEMANGLE_UNREACHABLE;
4719   }
4720 
4721   if (numLeft() < 2)
4722     return nullptr;
4723 
4724   if (look() == 'L')
4725     return getDerived().parseExprPrimary();
4726   if (look() == 'T')
4727     return getDerived().parseTemplateParam();
4728   if (look() == 'f') {
4729     // Disambiguate a fold expression from a <function-param>.
4730     if (look(1) == 'p' || (look(1) == 'L' && std::isdigit(look(2))))
4731       return getDerived().parseFunctionParam();
4732     return getDerived().parseFoldExpr();
4733   }
4734   if (consumeIf("il")) {
4735     size_t InitsBegin = Names.size();
4736     while (!consumeIf('E')) {
4737       Node *E = getDerived().parseBracedExpr();
4738       if (E == nullptr)
4739         return nullptr;
4740       Names.push_back(E);
4741     }
4742     return make<InitListExpr>(nullptr, popTrailingNodeArray(InitsBegin));
4743   }
4744   if (consumeIf("mc"))
4745     return parsePointerToMemberConversionExpr(Node::Prec::Unary);
4746   if (consumeIf("nx")) {
4747     Node *Ex = getDerived().parseExpr();
4748     if (Ex == nullptr)
4749       return Ex;
4750     return make<EnclosingExpr>("noexcept ", Ex, Node::Prec::Unary);
4751   }
4752   if (consumeIf("so"))
4753     return parseSubobjectExpr();
4754   if (consumeIf("sp")) {
4755     Node *Child = getDerived().parseExpr();
4756     if (Child == nullptr)
4757       return nullptr;
4758     return make<ParameterPackExpansion>(Child);
4759   }
4760   if (consumeIf("sZ")) {
4761     if (look() == 'T') {
4762       Node *R = getDerived().parseTemplateParam();
4763       if (R == nullptr)
4764         return nullptr;
4765       return make<SizeofParamPackExpr>(R);
4766     }
4767     Node *FP = getDerived().parseFunctionParam();
4768     if (FP == nullptr)
4769       return nullptr;
4770     return make<EnclosingExpr>("sizeof... ", FP);
4771   }
4772   if (consumeIf("sP")) {
4773     size_t ArgsBegin = Names.size();
4774     while (!consumeIf('E')) {
4775       Node *Arg = getDerived().parseTemplateArg();
4776       if (Arg == nullptr)
4777         return nullptr;
4778       Names.push_back(Arg);
4779     }
4780     auto *Pack = make<NodeArrayNode>(popTrailingNodeArray(ArgsBegin));
4781     if (!Pack)
4782       return nullptr;
4783     return make<EnclosingExpr>("sizeof... ", Pack);
4784   }
4785   if (consumeIf("tl")) {
4786     Node *Ty = getDerived().parseType();
4787     if (Ty == nullptr)
4788       return nullptr;
4789     size_t InitsBegin = Names.size();
4790     while (!consumeIf('E')) {
4791       Node *E = getDerived().parseBracedExpr();
4792       if (E == nullptr)
4793         return nullptr;
4794       Names.push_back(E);
4795     }
4796     return make<InitListExpr>(Ty, popTrailingNodeArray(InitsBegin));
4797   }
4798   if (consumeIf("tr"))
4799     return make<NameType>("throw");
4800   if (consumeIf("tw")) {
4801     Node *Ex = getDerived().parseExpr();
4802     if (Ex == nullptr)
4803       return nullptr;
4804     return make<ThrowExpr>(Ex);
4805   }
4806   if (consumeIf('u')) {
4807     Node *Name = getDerived().parseSourceName(/*NameState=*/nullptr);
4808     if (!Name)
4809       return nullptr;
4810     // Special case legacy __uuidof mangling. The 't' and 'z' appear where the
4811     // standard encoding expects a <template-arg>, and would be otherwise be
4812     // interpreted as <type> node 'short' or 'ellipsis'. However, neither
4813     // __uuidof(short) nor __uuidof(...) can actually appear, so there is no
4814     // actual conflict here.
4815     bool IsUUID = false;
4816     Node *UUID = nullptr;
4817     if (Name->getBaseName() == "__uuidof") {
4818       if (consumeIf('t')) {
4819         UUID = getDerived().parseType();
4820         IsUUID = true;
4821       } else if (consumeIf('z')) {
4822         UUID = getDerived().parseExpr();
4823         IsUUID = true;
4824       }
4825     }
4826     size_t ExprsBegin = Names.size();
4827     if (IsUUID) {
4828       if (UUID == nullptr)
4829         return nullptr;
4830       Names.push_back(UUID);
4831     } else {
4832       while (!consumeIf('E')) {
4833         Node *E = getDerived().parseTemplateArg();
4834         if (E == nullptr)
4835           return E;
4836         Names.push_back(E);
4837       }
4838     }
4839     return make<CallExpr>(Name, popTrailingNodeArray(ExprsBegin),
4840                           Node::Prec::Postfix);
4841   }
4842 
4843   // Only unresolved names remain.
4844   return getDerived().parseUnresolvedName(Global);
4845 }
4846 
4847 // <call-offset> ::= h <nv-offset> _
4848 //               ::= v <v-offset> _
4849 //
4850 // <nv-offset> ::= <offset number>
4851 //               # non-virtual base override
4852 //
4853 // <v-offset>  ::= <offset number> _ <virtual offset number>
4854 //               # virtual base override, with vcall offset
4855 template <typename Alloc, typename Derived>
4856 bool AbstractManglingParser<Alloc, Derived>::parseCallOffset() {
4857   // Just scan through the call offset, we never add this information into the
4858   // output.
4859   if (consumeIf('h'))
4860     return parseNumber(true).empty() || !consumeIf('_');
4861   if (consumeIf('v'))
4862     return parseNumber(true).empty() || !consumeIf('_') ||
4863            parseNumber(true).empty() || !consumeIf('_');
4864   return true;
4865 }
4866 
4867 // <special-name> ::= TV <type>    # virtual table
4868 //                ::= TT <type>    # VTT structure (construction vtable index)
4869 //                ::= TI <type>    # typeinfo structure
4870 //                ::= TS <type>    # typeinfo name (null-terminated byte string)
4871 //                ::= Tc <call-offset> <call-offset> <base encoding>
4872 //                    # base is the nominal target function of thunk
4873 //                    # first call-offset is 'this' adjustment
4874 //                    # second call-offset is result adjustment
4875 //                ::= T <call-offset> <base encoding>
4876 //                    # base is the nominal target function of thunk
4877 //                # Guard variable for one-time initialization
4878 //                ::= GV <object name>
4879 //                                     # No <type>
4880 //                ::= TW <object name> # Thread-local wrapper
4881 //                ::= TH <object name> # Thread-local initialization
4882 //                ::= GR <object name> _             # First temporary
4883 //                ::= GR <object name> <seq-id> _    # Subsequent temporaries
4884 //                # construction vtable for second-in-first
4885 //      extension ::= TC <first type> <number> _ <second type>
4886 //      extension ::= GR <object name> # reference temporary for object
4887 //      extension ::= GI <module name> # module global initializer
4888 template <typename Derived, typename Alloc>
4889 Node *AbstractManglingParser<Derived, Alloc>::parseSpecialName() {
4890   switch (look()) {
4891   case 'T':
4892     switch (look(1)) {
4893     // TA <template-arg>    # template parameter object
4894     //
4895     // Not yet in the spec: https://github.com/itanium-cxx-abi/cxx-abi/issues/63
4896     case 'A': {
4897       First += 2;
4898       Node *Arg = getDerived().parseTemplateArg();
4899       if (Arg == nullptr)
4900         return nullptr;
4901       return make<SpecialName>("template parameter object for ", Arg);
4902     }
4903     // TV <type>    # virtual table
4904     case 'V': {
4905       First += 2;
4906       Node *Ty = getDerived().parseType();
4907       if (Ty == nullptr)
4908         return nullptr;
4909       return make<SpecialName>("vtable for ", Ty);
4910     }
4911     // TT <type>    # VTT structure (construction vtable index)
4912     case 'T': {
4913       First += 2;
4914       Node *Ty = getDerived().parseType();
4915       if (Ty == nullptr)
4916         return nullptr;
4917       return make<SpecialName>("VTT for ", Ty);
4918     }
4919     // TI <type>    # typeinfo structure
4920     case 'I': {
4921       First += 2;
4922       Node *Ty = getDerived().parseType();
4923       if (Ty == nullptr)
4924         return nullptr;
4925       return make<SpecialName>("typeinfo for ", Ty);
4926     }
4927     // TS <type>    # typeinfo name (null-terminated byte string)
4928     case 'S': {
4929       First += 2;
4930       Node *Ty = getDerived().parseType();
4931       if (Ty == nullptr)
4932         return nullptr;
4933       return make<SpecialName>("typeinfo name for ", Ty);
4934     }
4935     // Tc <call-offset> <call-offset> <base encoding>
4936     case 'c': {
4937       First += 2;
4938       if (parseCallOffset() || parseCallOffset())
4939         return nullptr;
4940       Node *Encoding = getDerived().parseEncoding();
4941       if (Encoding == nullptr)
4942         return nullptr;
4943       return make<SpecialName>("covariant return thunk to ", Encoding);
4944     }
4945     // extension ::= TC <first type> <number> _ <second type>
4946     //               # construction vtable for second-in-first
4947     case 'C': {
4948       First += 2;
4949       Node *FirstType = getDerived().parseType();
4950       if (FirstType == nullptr)
4951         return nullptr;
4952       if (parseNumber(true).empty() || !consumeIf('_'))
4953         return nullptr;
4954       Node *SecondType = getDerived().parseType();
4955       if (SecondType == nullptr)
4956         return nullptr;
4957       return make<CtorVtableSpecialName>(SecondType, FirstType);
4958     }
4959     // TW <object name> # Thread-local wrapper
4960     case 'W': {
4961       First += 2;
4962       Node *Name = getDerived().parseName();
4963       if (Name == nullptr)
4964         return nullptr;
4965       return make<SpecialName>("thread-local wrapper routine for ", Name);
4966     }
4967     // TH <object name> # Thread-local initialization
4968     case 'H': {
4969       First += 2;
4970       Node *Name = getDerived().parseName();
4971       if (Name == nullptr)
4972         return nullptr;
4973       return make<SpecialName>("thread-local initialization routine for ", Name);
4974     }
4975     // T <call-offset> <base encoding>
4976     default: {
4977       ++First;
4978       bool IsVirt = look() == 'v';
4979       if (parseCallOffset())
4980         return nullptr;
4981       Node *BaseEncoding = getDerived().parseEncoding();
4982       if (BaseEncoding == nullptr)
4983         return nullptr;
4984       if (IsVirt)
4985         return make<SpecialName>("virtual thunk to ", BaseEncoding);
4986       else
4987         return make<SpecialName>("non-virtual thunk to ", BaseEncoding);
4988     }
4989     }
4990   case 'G':
4991     switch (look(1)) {
4992     // GV <object name> # Guard variable for one-time initialization
4993     case 'V': {
4994       First += 2;
4995       Node *Name = getDerived().parseName();
4996       if (Name == nullptr)
4997         return nullptr;
4998       return make<SpecialName>("guard variable for ", Name);
4999     }
5000     // GR <object name> # reference temporary for object
5001     // GR <object name> _             # First temporary
5002     // GR <object name> <seq-id> _    # Subsequent temporaries
5003     case 'R': {
5004       First += 2;
5005       Node *Name = getDerived().parseName();
5006       if (Name == nullptr)
5007         return nullptr;
5008       size_t Count;
5009       bool ParsedSeqId = !parseSeqId(&Count);
5010       if (!consumeIf('_') && ParsedSeqId)
5011         return nullptr;
5012       return make<SpecialName>("reference temporary for ", Name);
5013     }
5014     // GI <module-name> v
5015     case 'I': {
5016       First += 2;
5017       ModuleName *Module = nullptr;
5018       if (getDerived().parseModuleNameOpt(Module))
5019         return nullptr;
5020       if (Module == nullptr)
5021         return nullptr;
5022       return make<SpecialName>("initializer for module ", Module);
5023     }
5024     }
5025   }
5026   return nullptr;
5027 }
5028 
5029 // <encoding> ::= <function name> <bare-function-type>
5030 //            ::= <data name>
5031 //            ::= <special-name>
5032 template <typename Derived, typename Alloc>
5033 Node *AbstractManglingParser<Derived, Alloc>::parseEncoding() {
5034   // The template parameters of an encoding are unrelated to those of the
5035   // enclosing context.
5036   class SaveTemplateParams {
5037     AbstractManglingParser *Parser;
5038     decltype(TemplateParams) OldParams;
5039     decltype(OuterTemplateParams) OldOuterParams;
5040 
5041   public:
5042     SaveTemplateParams(AbstractManglingParser *TheParser) : Parser(TheParser) {
5043       OldParams = std::move(Parser->TemplateParams);
5044       OldOuterParams = std::move(Parser->OuterTemplateParams);
5045       Parser->TemplateParams.clear();
5046       Parser->OuterTemplateParams.clear();
5047     }
5048     ~SaveTemplateParams() {
5049       Parser->TemplateParams = std::move(OldParams);
5050       Parser->OuterTemplateParams = std::move(OldOuterParams);
5051     }
5052   } SaveTemplateParams(this);
5053 
5054   if (look() == 'G' || look() == 'T')
5055     return getDerived().parseSpecialName();
5056 
5057   auto IsEndOfEncoding = [&] {
5058     // The set of chars that can potentially follow an <encoding> (none of which
5059     // can start a <type>). Enumerating these allows us to avoid speculative
5060     // parsing.
5061     return numLeft() == 0 || look() == 'E' || look() == '.' || look() == '_';
5062   };
5063 
5064   NameState NameInfo(this);
5065   Node *Name = getDerived().parseName(&NameInfo);
5066   if (Name == nullptr)
5067     return nullptr;
5068 
5069   if (resolveForwardTemplateRefs(NameInfo))
5070     return nullptr;
5071 
5072   if (IsEndOfEncoding())
5073     return Name;
5074 
5075   Node *Attrs = nullptr;
5076   if (consumeIf("Ua9enable_ifI")) {
5077     size_t BeforeArgs = Names.size();
5078     while (!consumeIf('E')) {
5079       Node *Arg = getDerived().parseTemplateArg();
5080       if (Arg == nullptr)
5081         return nullptr;
5082       Names.push_back(Arg);
5083     }
5084     Attrs = make<EnableIfAttr>(popTrailingNodeArray(BeforeArgs));
5085     if (!Attrs)
5086       return nullptr;
5087   }
5088 
5089   Node *ReturnType = nullptr;
5090   if (!NameInfo.CtorDtorConversion && NameInfo.EndsWithTemplateArgs) {
5091     ReturnType = getDerived().parseType();
5092     if (ReturnType == nullptr)
5093       return nullptr;
5094   }
5095 
5096   if (consumeIf('v'))
5097     return make<FunctionEncoding>(ReturnType, Name, NodeArray(),
5098                                   Attrs, NameInfo.CVQualifiers,
5099                                   NameInfo.ReferenceQualifier);
5100 
5101   size_t ParamsBegin = Names.size();
5102   do {
5103     Node *Ty = getDerived().parseType();
5104     if (Ty == nullptr)
5105       return nullptr;
5106     Names.push_back(Ty);
5107   } while (!IsEndOfEncoding());
5108 
5109   return make<FunctionEncoding>(ReturnType, Name,
5110                                 popTrailingNodeArray(ParamsBegin),
5111                                 Attrs, NameInfo.CVQualifiers,
5112                                 NameInfo.ReferenceQualifier);
5113 }
5114 
5115 template <class Float>
5116 struct FloatData;
5117 
5118 template <>
5119 struct FloatData<float>
5120 {
5121     static const size_t mangled_size = 8;
5122     static const size_t max_demangled_size = 24;
5123     static constexpr const char* spec = "%af";
5124 };
5125 
5126 template <>
5127 struct FloatData<double>
5128 {
5129     static const size_t mangled_size = 16;
5130     static const size_t max_demangled_size = 32;
5131     static constexpr const char* spec = "%a";
5132 };
5133 
5134 template <>
5135 struct FloatData<long double>
5136 {
5137 #if defined(__mips__) && defined(__mips_n64) || defined(__aarch64__) || \
5138     defined(__wasm__)
5139     static const size_t mangled_size = 32;
5140 #elif defined(__arm__) || defined(__mips__) || defined(__hexagon__)
5141     static const size_t mangled_size = 16;
5142 #else
5143     static const size_t mangled_size = 20;  // May need to be adjusted to 16 or 24 on other platforms
5144 #endif
5145     // `-0x1.ffffffffffffffffffffffffffffp+16383` + 'L' + '\0' == 42 bytes.
5146     // 28 'f's * 4 bits == 112 bits, which is the number of mantissa bits.
5147     // Negatives are one character longer than positives.
5148     // `0x1.` and `p` are constant, and exponents `+16383` and `-16382` are the
5149     // same length. 1 sign bit, 112 mantissa bits, and 15 exponent bits == 128.
5150     static const size_t max_demangled_size = 42;
5151     static constexpr const char *spec = "%LaL";
5152 };
5153 
5154 template <typename Alloc, typename Derived>
5155 template <class Float>
5156 Node *AbstractManglingParser<Alloc, Derived>::parseFloatingLiteral() {
5157   const size_t N = FloatData<Float>::mangled_size;
5158   if (numLeft() <= N)
5159     return nullptr;
5160   StringView Data(First, First + N);
5161   for (char C : Data)
5162     if (!std::isxdigit(C))
5163       return nullptr;
5164   First += N;
5165   if (!consumeIf('E'))
5166     return nullptr;
5167   return make<FloatLiteralImpl<Float>>(Data);
5168 }
5169 
5170 // <seq-id> ::= <0-9A-Z>+
5171 template <typename Alloc, typename Derived>
5172 bool AbstractManglingParser<Alloc, Derived>::parseSeqId(size_t *Out) {
5173   if (!(look() >= '0' && look() <= '9') &&
5174       !(look() >= 'A' && look() <= 'Z'))
5175     return true;
5176 
5177   size_t Id = 0;
5178   while (true) {
5179     if (look() >= '0' && look() <= '9') {
5180       Id *= 36;
5181       Id += static_cast<size_t>(look() - '0');
5182     } else if (look() >= 'A' && look() <= 'Z') {
5183       Id *= 36;
5184       Id += static_cast<size_t>(look() - 'A') + 10;
5185     } else {
5186       *Out = Id;
5187       return false;
5188     }
5189     ++First;
5190   }
5191 }
5192 
5193 // <substitution> ::= S <seq-id> _
5194 //                ::= S_
5195 // <substitution> ::= Sa # ::std::allocator
5196 // <substitution> ::= Sb # ::std::basic_string
5197 // <substitution> ::= Ss # ::std::basic_string < char,
5198 //                                               ::std::char_traits<char>,
5199 //                                               ::std::allocator<char> >
5200 // <substitution> ::= Si # ::std::basic_istream<char,  std::char_traits<char> >
5201 // <substitution> ::= So # ::std::basic_ostream<char,  std::char_traits<char> >
5202 // <substitution> ::= Sd # ::std::basic_iostream<char, std::char_traits<char> >
5203 // The St case is handled specially in parseNestedName.
5204 template <typename Derived, typename Alloc>
5205 Node *AbstractManglingParser<Derived, Alloc>::parseSubstitution() {
5206   if (!consumeIf('S'))
5207     return nullptr;
5208 
5209   if (look() >= 'a' && look() <= 'z') {
5210     SpecialSubKind Kind;
5211     switch (look()) {
5212     case 'a':
5213       Kind = SpecialSubKind::allocator;
5214       break;
5215     case 'b':
5216       Kind = SpecialSubKind::basic_string;
5217       break;
5218     case 'd':
5219       Kind = SpecialSubKind::iostream;
5220       break;
5221     case 'i':
5222       Kind = SpecialSubKind::istream;
5223       break;
5224     case 'o':
5225       Kind = SpecialSubKind::ostream;
5226       break;
5227     case 's':
5228       Kind = SpecialSubKind::string;
5229       break;
5230     default:
5231       return nullptr;
5232     }
5233     ++First;
5234     auto *SpecialSub = make<SpecialSubstitution>(Kind);
5235     if (!SpecialSub)
5236       return nullptr;
5237 
5238     // Itanium C++ ABI 5.1.2: If a name that would use a built-in <substitution>
5239     // has ABI tags, the tags are appended to the substitution; the result is a
5240     // substitutable component.
5241     Node *WithTags = getDerived().parseAbiTags(SpecialSub);
5242     if (WithTags != SpecialSub) {
5243       Subs.push_back(WithTags);
5244       SpecialSub = WithTags;
5245     }
5246     return SpecialSub;
5247   }
5248 
5249   //                ::= S_
5250   if (consumeIf('_')) {
5251     if (Subs.empty())
5252       return nullptr;
5253     return Subs[0];
5254   }
5255 
5256   //                ::= S <seq-id> _
5257   size_t Index = 0;
5258   if (parseSeqId(&Index))
5259     return nullptr;
5260   ++Index;
5261   if (!consumeIf('_') || Index >= Subs.size())
5262     return nullptr;
5263   return Subs[Index];
5264 }
5265 
5266 // <template-param> ::= T_    # first template parameter
5267 //                  ::= T <parameter-2 non-negative number> _
5268 //                  ::= TL <level-1> __
5269 //                  ::= TL <level-1> _ <parameter-2 non-negative number> _
5270 template <typename Derived, typename Alloc>
5271 Node *AbstractManglingParser<Derived, Alloc>::parseTemplateParam() {
5272   if (!consumeIf('T'))
5273     return nullptr;
5274 
5275   size_t Level = 0;
5276   if (consumeIf('L')) {
5277     if (parsePositiveInteger(&Level))
5278       return nullptr;
5279     ++Level;
5280     if (!consumeIf('_'))
5281       return nullptr;
5282   }
5283 
5284   size_t Index = 0;
5285   if (!consumeIf('_')) {
5286     if (parsePositiveInteger(&Index))
5287       return nullptr;
5288     ++Index;
5289     if (!consumeIf('_'))
5290       return nullptr;
5291   }
5292 
5293   // If we're in a context where this <template-param> refers to a
5294   // <template-arg> further ahead in the mangled name (currently just conversion
5295   // operator types), then we should only look it up in the right context.
5296   // This can only happen at the outermost level.
5297   if (PermitForwardTemplateReferences && Level == 0) {
5298     Node *ForwardRef = make<ForwardTemplateReference>(Index);
5299     if (!ForwardRef)
5300       return nullptr;
5301     assert(ForwardRef->getKind() == Node::KForwardTemplateReference);
5302     ForwardTemplateRefs.push_back(
5303         static_cast<ForwardTemplateReference *>(ForwardRef));
5304     return ForwardRef;
5305   }
5306 
5307   if (Level >= TemplateParams.size() || !TemplateParams[Level] ||
5308       Index >= TemplateParams[Level]->size()) {
5309     // Itanium ABI 5.1.8: In a generic lambda, uses of auto in the parameter
5310     // list are mangled as the corresponding artificial template type parameter.
5311     if (ParsingLambdaParamsAtLevel == Level && Level <= TemplateParams.size()) {
5312       // This will be popped by the ScopedTemplateParamList in
5313       // parseUnnamedTypeName.
5314       if (Level == TemplateParams.size())
5315         TemplateParams.push_back(nullptr);
5316       return make<NameType>("auto");
5317     }
5318 
5319     return nullptr;
5320   }
5321 
5322   return (*TemplateParams[Level])[Index];
5323 }
5324 
5325 // <template-param-decl> ::= Ty                          # type parameter
5326 //                       ::= Tn <type>                   # non-type parameter
5327 //                       ::= Tt <template-param-decl>* E # template parameter
5328 //                       ::= Tp <template-param-decl>    # parameter pack
5329 template <typename Derived, typename Alloc>
5330 Node *AbstractManglingParser<Derived, Alloc>::parseTemplateParamDecl() {
5331   auto InventTemplateParamName = [&](TemplateParamKind Kind) {
5332     unsigned Index = NumSyntheticTemplateParameters[(int)Kind]++;
5333     Node *N = make<SyntheticTemplateParamName>(Kind, Index);
5334     if (N) TemplateParams.back()->push_back(N);
5335     return N;
5336   };
5337 
5338   if (consumeIf("Ty")) {
5339     Node *Name = InventTemplateParamName(TemplateParamKind::Type);
5340     if (!Name)
5341       return nullptr;
5342     return make<TypeTemplateParamDecl>(Name);
5343   }
5344 
5345   if (consumeIf("Tn")) {
5346     Node *Name = InventTemplateParamName(TemplateParamKind::NonType);
5347     if (!Name)
5348       return nullptr;
5349     Node *Type = parseType();
5350     if (!Type)
5351       return nullptr;
5352     return make<NonTypeTemplateParamDecl>(Name, Type);
5353   }
5354 
5355   if (consumeIf("Tt")) {
5356     Node *Name = InventTemplateParamName(TemplateParamKind::Template);
5357     if (!Name)
5358       return nullptr;
5359     size_t ParamsBegin = Names.size();
5360     ScopedTemplateParamList TemplateTemplateParamParams(this);
5361     while (!consumeIf("E")) {
5362       Node *P = parseTemplateParamDecl();
5363       if (!P)
5364         return nullptr;
5365       Names.push_back(P);
5366     }
5367     NodeArray Params = popTrailingNodeArray(ParamsBegin);
5368     return make<TemplateTemplateParamDecl>(Name, Params);
5369   }
5370 
5371   if (consumeIf("Tp")) {
5372     Node *P = parseTemplateParamDecl();
5373     if (!P)
5374       return nullptr;
5375     return make<TemplateParamPackDecl>(P);
5376   }
5377 
5378   return nullptr;
5379 }
5380 
5381 // <template-arg> ::= <type>                    # type or template
5382 //                ::= X <expression> E          # expression
5383 //                ::= <expr-primary>            # simple expressions
5384 //                ::= J <template-arg>* E       # argument pack
5385 //                ::= LZ <encoding> E           # extension
5386 template <typename Derived, typename Alloc>
5387 Node *AbstractManglingParser<Derived, Alloc>::parseTemplateArg() {
5388   switch (look()) {
5389   case 'X': {
5390     ++First;
5391     Node *Arg = getDerived().parseExpr();
5392     if (Arg == nullptr || !consumeIf('E'))
5393       return nullptr;
5394     return Arg;
5395   }
5396   case 'J': {
5397     ++First;
5398     size_t ArgsBegin = Names.size();
5399     while (!consumeIf('E')) {
5400       Node *Arg = getDerived().parseTemplateArg();
5401       if (Arg == nullptr)
5402         return nullptr;
5403       Names.push_back(Arg);
5404     }
5405     NodeArray Args = popTrailingNodeArray(ArgsBegin);
5406     return make<TemplateArgumentPack>(Args);
5407   }
5408   case 'L': {
5409     //                ::= LZ <encoding> E           # extension
5410     if (look(1) == 'Z') {
5411       First += 2;
5412       Node *Arg = getDerived().parseEncoding();
5413       if (Arg == nullptr || !consumeIf('E'))
5414         return nullptr;
5415       return Arg;
5416     }
5417     //                ::= <expr-primary>            # simple expressions
5418     return getDerived().parseExprPrimary();
5419   }
5420   default:
5421     return getDerived().parseType();
5422   }
5423 }
5424 
5425 // <template-args> ::= I <template-arg>* E
5426 //     extension, the abi says <template-arg>+
5427 template <typename Derived, typename Alloc>
5428 Node *
5429 AbstractManglingParser<Derived, Alloc>::parseTemplateArgs(bool TagTemplates) {
5430   if (!consumeIf('I'))
5431     return nullptr;
5432 
5433   // <template-params> refer to the innermost <template-args>. Clear out any
5434   // outer args that we may have inserted into TemplateParams.
5435   if (TagTemplates) {
5436     TemplateParams.clear();
5437     TemplateParams.push_back(&OuterTemplateParams);
5438     OuterTemplateParams.clear();
5439   }
5440 
5441   size_t ArgsBegin = Names.size();
5442   while (!consumeIf('E')) {
5443     if (TagTemplates) {
5444       auto OldParams = std::move(TemplateParams);
5445       Node *Arg = getDerived().parseTemplateArg();
5446       TemplateParams = std::move(OldParams);
5447       if (Arg == nullptr)
5448         return nullptr;
5449       Names.push_back(Arg);
5450       Node *TableEntry = Arg;
5451       if (Arg->getKind() == Node::KTemplateArgumentPack) {
5452         TableEntry = make<ParameterPack>(
5453             static_cast<TemplateArgumentPack*>(TableEntry)->getElements());
5454         if (!TableEntry)
5455           return nullptr;
5456       }
5457       TemplateParams.back()->push_back(TableEntry);
5458     } else {
5459       Node *Arg = getDerived().parseTemplateArg();
5460       if (Arg == nullptr)
5461         return nullptr;
5462       Names.push_back(Arg);
5463     }
5464   }
5465   return make<TemplateArgs>(popTrailingNodeArray(ArgsBegin));
5466 }
5467 
5468 // <mangled-name> ::= _Z <encoding>
5469 //                ::= <type>
5470 // extension      ::= ___Z <encoding> _block_invoke
5471 // extension      ::= ___Z <encoding> _block_invoke<decimal-digit>+
5472 // extension      ::= ___Z <encoding> _block_invoke_<decimal-digit>+
5473 template <typename Derived, typename Alloc>
5474 Node *AbstractManglingParser<Derived, Alloc>::parse() {
5475   if (consumeIf("_Z") || consumeIf("__Z")) {
5476     Node *Encoding = getDerived().parseEncoding();
5477     if (Encoding == nullptr)
5478       return nullptr;
5479     if (look() == '.') {
5480       Encoding = make<DotSuffix>(Encoding, StringView(First, Last));
5481       First = Last;
5482     }
5483     if (numLeft() != 0)
5484       return nullptr;
5485     return Encoding;
5486   }
5487 
5488   if (consumeIf("___Z") || consumeIf("____Z")) {
5489     Node *Encoding = getDerived().parseEncoding();
5490     if (Encoding == nullptr || !consumeIf("_block_invoke"))
5491       return nullptr;
5492     bool RequireNumber = consumeIf('_');
5493     if (parseNumber().empty() && RequireNumber)
5494       return nullptr;
5495     if (look() == '.')
5496       First = Last;
5497     if (numLeft() != 0)
5498       return nullptr;
5499     return make<SpecialName>("invocation function for block in ", Encoding);
5500   }
5501 
5502   Node *Ty = getDerived().parseType();
5503   if (numLeft() != 0)
5504     return nullptr;
5505   return Ty;
5506 }
5507 
5508 template <typename Alloc>
5509 struct ManglingParser : AbstractManglingParser<ManglingParser<Alloc>, Alloc> {
5510   using AbstractManglingParser<ManglingParser<Alloc>,
5511                                Alloc>::AbstractManglingParser;
5512 };
5513 
5514 DEMANGLE_NAMESPACE_END
5515 
5516 #endif // DEMANGLE_ITANIUMDEMANGLE_H
5517