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