1 //===------------------------- ItaniumDemangle.h ----------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is dual licensed under the MIT and the University of Illinois Open
6 // Source Licenses. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #ifndef LLVM_DEMANGLE_ITANIUMDEMANGLE_H
11 #define LLVM_DEMANGLE_ITANIUMDEMANGLE_H
13 // FIXME: (possibly) incomplete list of features that clang mangles that this
14 // file does not yet support:
17 #include "llvm/Demangle/Compiler.h"
18 #include "llvm/Demangle/StringView.h"
19 #include "llvm/Demangle/Utility.h"
29 #define FOR_EACH_NODE_KIND(X) \
32 X(VendorExtQualType) \
34 X(ConversionOperatorType) \
35 X(PostfixQualifiedType) \
36 X(ElaboratedTypeSpefType) \
43 X(PointerToMemberType) \
47 X(DynamicExceptionSpec) \
51 X(CtorVtableSpecialName) \
58 X(TemplateArgumentPack) \
59 X(ParameterPackExpansion) \
61 X(ForwardTemplateReference) \
62 X(NameWithTemplateArgs) \
63 X(GlobalQualifiedName) \
65 X(ExpandedSpecialSubstitution) \
66 X(SpecialSubstitution) \
71 X(StructuredBindingName) \
73 X(ArraySubscriptExpr) \
79 X(SizeofParamPackExpr) \
94 X(LongDoubleLiteral) \
99 namespace itanium_demangle {
100 // Base class of all AST nodes. The AST is built by the parser, then is
101 // traversed by the printLeft/Right functions to produce a demangled string.
104 enum Kind : unsigned char {
105 #define ENUMERATOR(NodeKind) K ## NodeKind,
106 FOR_EACH_NODE_KIND(ENUMERATOR)
110 /// Three-way bool to track a cached value. Unknown is possible if this node
111 /// has an unexpanded parameter pack below it that may affect this cache.
112 enum class Cache : unsigned char { Yes, No, Unknown, };
117 // FIXME: Make these protected.
119 /// Tracks if this node has a component on its right side, in which case we
120 /// need to call printRight.
121 Cache RHSComponentCache;
123 /// Track if this node is a (possibly qualified) array type. This can affect
124 /// how we format the output string.
127 /// Track if this node is a (possibly qualified) function type. This can
128 /// affect how we format the output string.
132 Node(Kind K_, Cache RHSComponentCache_ = Cache::No,
133 Cache ArrayCache_ = Cache::No, Cache FunctionCache_ = Cache::No)
134 : K(K_), RHSComponentCache(RHSComponentCache_), ArrayCache(ArrayCache_),
135 FunctionCache(FunctionCache_) {}
137 /// Visit the most-derived object corresponding to this object.
138 template<typename Fn> void visit(Fn F) const;
140 // The following function is provided by all derived classes:
142 // Call F with arguments that, when passed to the constructor of this node,
143 // would construct an equivalent node.
144 //template<typename Fn> void match(Fn F) const;
146 bool hasRHSComponent(OutputStream &S) const {
147 if (RHSComponentCache != Cache::Unknown)
148 return RHSComponentCache == Cache::Yes;
149 return hasRHSComponentSlow(S);
152 bool hasArray(OutputStream &S) const {
153 if (ArrayCache != Cache::Unknown)
154 return ArrayCache == Cache::Yes;
155 return hasArraySlow(S);
158 bool hasFunction(OutputStream &S) const {
159 if (FunctionCache != Cache::Unknown)
160 return FunctionCache == Cache::Yes;
161 return hasFunctionSlow(S);
164 Kind getKind() const { return K; }
166 virtual bool hasRHSComponentSlow(OutputStream &) const { return false; }
167 virtual bool hasArraySlow(OutputStream &) const { return false; }
168 virtual bool hasFunctionSlow(OutputStream &) const { return false; }
170 // Dig through "glue" nodes like ParameterPack and ForwardTemplateReference to
171 // get at a node that actually represents some concrete syntax.
172 virtual const Node *getSyntaxNode(OutputStream &) const {
176 void print(OutputStream &S) const {
178 if (RHSComponentCache != Cache::No)
182 // Print the "left" side of this Node into OutputStream.
183 virtual void printLeft(OutputStream &) const = 0;
185 // Print the "right". This distinction is necessary to represent C++ types
186 // that appear on the RHS of their subtype, such as arrays or functions.
187 // Since most types don't have such a component, provide a default
189 virtual void printRight(OutputStream &) const {}
191 virtual StringView getBaseName() const { return StringView(); }
193 // Silence compiler warnings, this dtor will never be called.
194 virtual ~Node() = default;
197 LLVM_DUMP_METHOD void dump() const;
206 NodeArray() : Elements(nullptr), NumElements(0) {}
207 NodeArray(Node **Elements_, size_t NumElements_)
208 : Elements(Elements_), NumElements(NumElements_) {}
210 bool empty() const { return NumElements == 0; }
211 size_t size() const { return NumElements; }
213 Node **begin() const { return Elements; }
214 Node **end() const { return Elements + NumElements; }
216 Node *operator[](size_t Idx) const { return Elements[Idx]; }
218 void printWithComma(OutputStream &S) const {
219 bool FirstElement = true;
220 for (size_t Idx = 0; Idx != NumElements; ++Idx) {
221 size_t BeforeComma = S.getCurrentPosition();
224 size_t AfterComma = S.getCurrentPosition();
225 Elements[Idx]->print(S);
227 // Elements[Idx] is an empty parameter pack expansion, we should erase the
228 // comma we just printed.
229 if (AfterComma == S.getCurrentPosition()) {
230 S.setCurrentPosition(BeforeComma);
234 FirstElement = false;
239 struct NodeArrayNode : Node {
241 NodeArrayNode(NodeArray Array_) : Node(KNodeArrayNode), Array(Array_) {}
243 template<typename Fn> void match(Fn F) const { F(Array); }
245 void printLeft(OutputStream &S) const override {
246 Array.printWithComma(S);
250 class DotSuffix final : public Node {
252 const StringView Suffix;
255 DotSuffix(const Node *Prefix_, StringView Suffix_)
256 : Node(KDotSuffix), Prefix(Prefix_), Suffix(Suffix_) {}
258 template<typename Fn> void match(Fn F) const { F(Prefix, Suffix); }
260 void printLeft(OutputStream &s) const override {
268 class VendorExtQualType final : public Node {
273 VendorExtQualType(const Node *Ty_, StringView Ext_)
274 : Node(KVendorExtQualType), Ty(Ty_), Ext(Ext_) {}
276 template<typename Fn> void match(Fn F) const { F(Ty, Ext); }
278 void printLeft(OutputStream &S) const override {
285 enum FunctionRefQual : unsigned char {
298 inline Qualifiers operator|=(Qualifiers &Q1, Qualifiers Q2) {
299 return Q1 = static_cast<Qualifiers>(Q1 | Q2);
302 class QualType : public Node {
304 const Qualifiers Quals;
307 void printQuals(OutputStream &S) const {
308 if (Quals & QualConst)
310 if (Quals & QualVolatile)
312 if (Quals & QualRestrict)
317 QualType(const Node *Child_, Qualifiers Quals_)
318 : Node(KQualType, Child_->RHSComponentCache,
319 Child_->ArrayCache, Child_->FunctionCache),
320 Quals(Quals_), Child(Child_) {}
322 template<typename Fn> void match(Fn F) const { F(Child, Quals); }
324 bool hasRHSComponentSlow(OutputStream &S) const override {
325 return Child->hasRHSComponent(S);
327 bool hasArraySlow(OutputStream &S) const override {
328 return Child->hasArray(S);
330 bool hasFunctionSlow(OutputStream &S) const override {
331 return Child->hasFunction(S);
334 void printLeft(OutputStream &S) const override {
339 void printRight(OutputStream &S) const override { Child->printRight(S); }
342 class ConversionOperatorType final : public Node {
346 ConversionOperatorType(const Node *Ty_)
347 : Node(KConversionOperatorType), Ty(Ty_) {}
349 template<typename Fn> void match(Fn F) const { F(Ty); }
351 void printLeft(OutputStream &S) const override {
357 class PostfixQualifiedType final : public Node {
359 const StringView Postfix;
362 PostfixQualifiedType(Node *Ty_, StringView Postfix_)
363 : Node(KPostfixQualifiedType), Ty(Ty_), Postfix(Postfix_) {}
365 template<typename Fn> void match(Fn F) const { F(Ty, Postfix); }
367 void printLeft(OutputStream &s) const override {
373 class NameType final : public Node {
374 const StringView Name;
377 NameType(StringView Name_) : Node(KNameType), Name(Name_) {}
379 template<typename Fn> void match(Fn F) const { F(Name); }
381 StringView getName() const { return Name; }
382 StringView getBaseName() const override { return Name; }
384 void printLeft(OutputStream &s) const override { s += Name; }
387 class ElaboratedTypeSpefType : public Node {
391 ElaboratedTypeSpefType(StringView Kind_, Node *Child_)
392 : Node(KElaboratedTypeSpefType), Kind(Kind_), Child(Child_) {}
394 template<typename Fn> void match(Fn F) const { F(Kind, Child); }
396 void printLeft(OutputStream &S) const override {
403 struct AbiTagAttr : Node {
407 AbiTagAttr(Node* Base_, StringView Tag_)
408 : Node(KAbiTagAttr, Base_->RHSComponentCache,
409 Base_->ArrayCache, Base_->FunctionCache),
410 Base(Base_), Tag(Tag_) {}
412 template<typename Fn> void match(Fn F) const { F(Base, Tag); }
414 void printLeft(OutputStream &S) const override {
422 class EnableIfAttr : public Node {
423 NodeArray Conditions;
425 EnableIfAttr(NodeArray Conditions_)
426 : Node(KEnableIfAttr), Conditions(Conditions_) {}
428 template<typename Fn> void match(Fn F) const { F(Conditions); }
430 void printLeft(OutputStream &S) const override {
432 Conditions.printWithComma(S);
437 class ObjCProtoName : public Node {
441 friend class PointerType;
444 ObjCProtoName(const Node *Ty_, StringView Protocol_)
445 : Node(KObjCProtoName), Ty(Ty_), Protocol(Protocol_) {}
447 template<typename Fn> void match(Fn F) const { F(Ty, Protocol); }
449 bool isObjCObject() const {
450 return Ty->getKind() == KNameType &&
451 static_cast<const NameType *>(Ty)->getName() == "objc_object";
454 void printLeft(OutputStream &S) const override {
462 class PointerType final : public Node {
466 PointerType(const Node *Pointee_)
467 : Node(KPointerType, Pointee_->RHSComponentCache),
470 template<typename Fn> void match(Fn F) const { F(Pointee); }
472 bool hasRHSComponentSlow(OutputStream &S) const override {
473 return Pointee->hasRHSComponent(S);
476 void printLeft(OutputStream &s) const override {
477 // We rewrite objc_object<SomeProtocol>* into id<SomeProtocol>.
478 if (Pointee->getKind() != KObjCProtoName ||
479 !static_cast<const ObjCProtoName *>(Pointee)->isObjCObject()) {
480 Pointee->printLeft(s);
481 if (Pointee->hasArray(s))
483 if (Pointee->hasArray(s) || Pointee->hasFunction(s))
487 const auto *objcProto = static_cast<const ObjCProtoName *>(Pointee);
489 s += objcProto->Protocol;
494 void printRight(OutputStream &s) const override {
495 if (Pointee->getKind() != KObjCProtoName ||
496 !static_cast<const ObjCProtoName *>(Pointee)->isObjCObject()) {
497 if (Pointee->hasArray(s) || Pointee->hasFunction(s))
499 Pointee->printRight(s);
504 enum class ReferenceKind {
509 // Represents either a LValue or an RValue reference type.
510 class ReferenceType : public Node {
514 mutable bool Printing = false;
516 // Dig through any refs to refs, collapsing the ReferenceTypes as we go. The
517 // rule here is rvalue ref to rvalue ref collapses to a rvalue ref, and any
518 // other combination collapses to a lvalue ref.
519 std::pair<ReferenceKind, const Node *> collapse(OutputStream &S) const {
520 auto SoFar = std::make_pair(RK, Pointee);
522 const Node *SN = SoFar.second->getSyntaxNode(S);
523 if (SN->getKind() != KReferenceType)
525 auto *RT = static_cast<const ReferenceType *>(SN);
526 SoFar.second = RT->Pointee;
527 SoFar.first = std::min(SoFar.first, RT->RK);
533 ReferenceType(const Node *Pointee_, ReferenceKind RK_)
534 : Node(KReferenceType, Pointee_->RHSComponentCache),
535 Pointee(Pointee_), RK(RK_) {}
537 template<typename Fn> void match(Fn F) const { F(Pointee, RK); }
539 bool hasRHSComponentSlow(OutputStream &S) const override {
540 return Pointee->hasRHSComponent(S);
543 void printLeft(OutputStream &s) const override {
546 SwapAndRestore<bool> SavePrinting(Printing, true);
547 std::pair<ReferenceKind, const Node *> Collapsed = collapse(s);
548 Collapsed.second->printLeft(s);
549 if (Collapsed.second->hasArray(s))
551 if (Collapsed.second->hasArray(s) || Collapsed.second->hasFunction(s))
554 s += (Collapsed.first == ReferenceKind::LValue ? "&" : "&&");
556 void printRight(OutputStream &s) const override {
559 SwapAndRestore<bool> SavePrinting(Printing, true);
560 std::pair<ReferenceKind, const Node *> Collapsed = collapse(s);
561 if (Collapsed.second->hasArray(s) || Collapsed.second->hasFunction(s))
563 Collapsed.second->printRight(s);
567 class PointerToMemberType final : public Node {
568 const Node *ClassType;
569 const Node *MemberType;
572 PointerToMemberType(const Node *ClassType_, const Node *MemberType_)
573 : Node(KPointerToMemberType, MemberType_->RHSComponentCache),
574 ClassType(ClassType_), MemberType(MemberType_) {}
576 template<typename Fn> void match(Fn F) const { F(ClassType, MemberType); }
578 bool hasRHSComponentSlow(OutputStream &S) const override {
579 return MemberType->hasRHSComponent(S);
582 void printLeft(OutputStream &s) const override {
583 MemberType->printLeft(s);
584 if (MemberType->hasArray(s) || MemberType->hasFunction(s))
592 void printRight(OutputStream &s) const override {
593 if (MemberType->hasArray(s) || MemberType->hasFunction(s))
595 MemberType->printRight(s);
604 /* implicit */ NodeOrString(StringView Str) {
605 const char *FirstChar = Str.begin();
606 const char *SecondChar = Str.end();
607 if (SecondChar == nullptr) {
608 assert(FirstChar == SecondChar);
609 ++FirstChar, ++SecondChar;
611 First = static_cast<const void *>(FirstChar);
612 Second = static_cast<const void *>(SecondChar);
615 /* implicit */ NodeOrString(Node *N)
616 : First(static_cast<const void *>(N)), Second(nullptr) {}
617 NodeOrString() : First(nullptr), Second(nullptr) {}
619 bool isString() const { return Second && First; }
620 bool isNode() const { return First && !Second; }
621 bool isEmpty() const { return !First && !Second; }
623 StringView asString() const {
625 return StringView(static_cast<const char *>(First),
626 static_cast<const char *>(Second));
629 const Node *asNode() const {
631 return static_cast<const Node *>(First);
635 class ArrayType final : public Node {
637 NodeOrString Dimension;
640 ArrayType(const Node *Base_, NodeOrString Dimension_)
642 /*RHSComponentCache=*/Cache::Yes,
643 /*ArrayCache=*/Cache::Yes),
644 Base(Base_), Dimension(Dimension_) {}
646 template<typename Fn> void match(Fn F) const { F(Base, Dimension); }
648 bool hasRHSComponentSlow(OutputStream &) const override { return true; }
649 bool hasArraySlow(OutputStream &) const override { return true; }
651 void printLeft(OutputStream &S) const override { Base->printLeft(S); }
653 void printRight(OutputStream &S) const override {
657 if (Dimension.isString())
658 S += Dimension.asString();
659 else if (Dimension.isNode())
660 Dimension.asNode()->print(S);
666 class FunctionType final : public Node {
670 FunctionRefQual RefQual;
671 const Node *ExceptionSpec;
674 FunctionType(const Node *Ret_, NodeArray Params_, Qualifiers CVQuals_,
675 FunctionRefQual RefQual_, const Node *ExceptionSpec_)
676 : Node(KFunctionType,
677 /*RHSComponentCache=*/Cache::Yes, /*ArrayCache=*/Cache::No,
678 /*FunctionCache=*/Cache::Yes),
679 Ret(Ret_), Params(Params_), CVQuals(CVQuals_), RefQual(RefQual_),
680 ExceptionSpec(ExceptionSpec_) {}
682 template<typename Fn> void match(Fn F) const {
683 F(Ret, Params, CVQuals, RefQual, ExceptionSpec);
686 bool hasRHSComponentSlow(OutputStream &) const override { return true; }
687 bool hasFunctionSlow(OutputStream &) const override { return true; }
689 // Handle C++'s ... quirky decl grammar by using the left & right
690 // distinction. Consider:
691 // int (*f(float))(char) {}
692 // f is a function that takes a float and returns a pointer to a function
693 // that takes a char and returns an int. If we're trying to print f, start
694 // by printing out the return types's left, then print our parameters, then
695 // finally print right of the return type.
696 void printLeft(OutputStream &S) const override {
701 void printRight(OutputStream &S) const override {
703 Params.printWithComma(S);
707 if (CVQuals & QualConst)
709 if (CVQuals & QualVolatile)
711 if (CVQuals & QualRestrict)
714 if (RefQual == FrefQualLValue)
716 else if (RefQual == FrefQualRValue)
719 if (ExceptionSpec != nullptr) {
721 ExceptionSpec->print(S);
726 class NoexceptSpec : public Node {
729 NoexceptSpec(const Node *E_) : Node(KNoexceptSpec), E(E_) {}
731 template<typename Fn> void match(Fn F) const { F(E); }
733 void printLeft(OutputStream &S) const override {
740 class DynamicExceptionSpec : public Node {
743 DynamicExceptionSpec(NodeArray Types_)
744 : Node(KDynamicExceptionSpec), Types(Types_) {}
746 template<typename Fn> void match(Fn F) const { F(Types); }
748 void printLeft(OutputStream &S) const override {
750 Types.printWithComma(S);
755 class FunctionEncoding final : public Node {
761 FunctionRefQual RefQual;
764 FunctionEncoding(const Node *Ret_, const Node *Name_, NodeArray Params_,
765 const Node *Attrs_, Qualifiers CVQuals_,
766 FunctionRefQual RefQual_)
767 : Node(KFunctionEncoding,
768 /*RHSComponentCache=*/Cache::Yes, /*ArrayCache=*/Cache::No,
769 /*FunctionCache=*/Cache::Yes),
770 Ret(Ret_), Name(Name_), Params(Params_), Attrs(Attrs_),
771 CVQuals(CVQuals_), RefQual(RefQual_) {}
773 template<typename Fn> void match(Fn F) const {
774 F(Ret, Name, Params, Attrs, CVQuals, RefQual);
777 Qualifiers getCVQuals() const { return CVQuals; }
778 FunctionRefQual getRefQual() const { return RefQual; }
779 NodeArray getParams() const { return Params; }
780 const Node *getReturnType() const { return Ret; }
782 bool hasRHSComponentSlow(OutputStream &) const override { return true; }
783 bool hasFunctionSlow(OutputStream &) const override { return true; }
785 const Node *getName() const { return Name; }
787 void printLeft(OutputStream &S) const override {
790 if (!Ret->hasRHSComponent(S))
796 void printRight(OutputStream &S) const override {
798 Params.printWithComma(S);
803 if (CVQuals & QualConst)
805 if (CVQuals & QualVolatile)
807 if (CVQuals & QualRestrict)
810 if (RefQual == FrefQualLValue)
812 else if (RefQual == FrefQualRValue)
815 if (Attrs != nullptr)
820 class LiteralOperator : public Node {
824 LiteralOperator(const Node *OpName_)
825 : Node(KLiteralOperator), OpName(OpName_) {}
827 template<typename Fn> void match(Fn F) const { F(OpName); }
829 void printLeft(OutputStream &S) const override {
830 S += "operator\"\" ";
835 class SpecialName final : public Node {
836 const StringView Special;
840 SpecialName(StringView Special_, const Node *Child_)
841 : Node(KSpecialName), Special(Special_), Child(Child_) {}
843 template<typename Fn> void match(Fn F) const { F(Special, Child); }
845 void printLeft(OutputStream &S) const override {
851 class CtorVtableSpecialName final : public Node {
852 const Node *FirstType;
853 const Node *SecondType;
856 CtorVtableSpecialName(const Node *FirstType_, const Node *SecondType_)
857 : Node(KCtorVtableSpecialName),
858 FirstType(FirstType_), SecondType(SecondType_) {}
860 template<typename Fn> void match(Fn F) const { F(FirstType, SecondType); }
862 void printLeft(OutputStream &S) const override {
863 S += "construction vtable for ";
866 SecondType->print(S);
870 struct NestedName : Node {
874 NestedName(Node *Qual_, Node *Name_)
875 : Node(KNestedName), Qual(Qual_), Name(Name_) {}
877 template<typename Fn> void match(Fn F) const { F(Qual, Name); }
879 StringView getBaseName() const override { return Name->getBaseName(); }
881 void printLeft(OutputStream &S) const override {
888 struct LocalName : Node {
892 LocalName(Node *Encoding_, Node *Entity_)
893 : Node(KLocalName), Encoding(Encoding_), Entity(Entity_) {}
895 template<typename Fn> void match(Fn F) const { F(Encoding, Entity); }
897 void printLeft(OutputStream &S) const override {
904 class QualifiedName final : public Node {
906 const Node *Qualifier;
910 QualifiedName(const Node *Qualifier_, const Node *Name_)
911 : Node(KQualifiedName), Qualifier(Qualifier_), Name(Name_) {}
913 template<typename Fn> void match(Fn F) const { F(Qualifier, Name); }
915 StringView getBaseName() const override { return Name->getBaseName(); }
917 void printLeft(OutputStream &S) const override {
924 class VectorType final : public Node {
925 const Node *BaseType;
926 const NodeOrString Dimension;
929 VectorType(const Node *BaseType_, NodeOrString Dimension_)
930 : Node(KVectorType), BaseType(BaseType_),
931 Dimension(Dimension_) {}
933 template<typename Fn> void match(Fn F) const { F(BaseType, Dimension); }
935 void printLeft(OutputStream &S) const override {
938 if (Dimension.isNode())
939 Dimension.asNode()->print(S);
940 else if (Dimension.isString())
941 S += Dimension.asString();
946 class PixelVectorType final : public Node {
947 const NodeOrString Dimension;
950 PixelVectorType(NodeOrString Dimension_)
951 : Node(KPixelVectorType), Dimension(Dimension_) {}
953 template<typename Fn> void match(Fn F) const { F(Dimension); }
955 void printLeft(OutputStream &S) const override {
956 // FIXME: This should demangle as "vector pixel".
957 S += "pixel vector[";
958 S += Dimension.asString();
963 /// An unexpanded parameter pack (either in the expression or type context). If
964 /// this AST is correct, this node will have a ParameterPackExpansion node above
967 /// This node is created when some <template-args> are found that apply to an
968 /// <encoding>, and is stored in the TemplateParams table. In order for this to
969 /// appear in the final AST, it has to referenced via a <template-param> (ie,
971 class ParameterPack final : public Node {
974 // Setup OutputStream for a pack expansion unless we're already expanding one.
975 void initializePackExpansion(OutputStream &S) const {
976 if (S.CurrentPackMax == std::numeric_limits<unsigned>::max()) {
977 S.CurrentPackMax = static_cast<unsigned>(Data.size());
978 S.CurrentPackIndex = 0;
983 ParameterPack(NodeArray Data_) : Node(KParameterPack), Data(Data_) {
984 ArrayCache = FunctionCache = RHSComponentCache = Cache::Unknown;
985 if (std::all_of(Data.begin(), Data.end(), [](Node* P) {
986 return P->ArrayCache == Cache::No;
988 ArrayCache = Cache::No;
989 if (std::all_of(Data.begin(), Data.end(), [](Node* P) {
990 return P->FunctionCache == Cache::No;
992 FunctionCache = Cache::No;
993 if (std::all_of(Data.begin(), Data.end(), [](Node* P) {
994 return P->RHSComponentCache == Cache::No;
996 RHSComponentCache = Cache::No;
999 template<typename Fn> void match(Fn F) const { F(Data); }
1001 bool hasRHSComponentSlow(OutputStream &S) const override {
1002 initializePackExpansion(S);
1003 size_t Idx = S.CurrentPackIndex;
1004 return Idx < Data.size() && Data[Idx]->hasRHSComponent(S);
1006 bool hasArraySlow(OutputStream &S) const override {
1007 initializePackExpansion(S);
1008 size_t Idx = S.CurrentPackIndex;
1009 return Idx < Data.size() && Data[Idx]->hasArray(S);
1011 bool hasFunctionSlow(OutputStream &S) const override {
1012 initializePackExpansion(S);
1013 size_t Idx = S.CurrentPackIndex;
1014 return Idx < Data.size() && Data[Idx]->hasFunction(S);
1016 const Node *getSyntaxNode(OutputStream &S) const override {
1017 initializePackExpansion(S);
1018 size_t Idx = S.CurrentPackIndex;
1019 return Idx < Data.size() ? Data[Idx]->getSyntaxNode(S) : this;
1022 void printLeft(OutputStream &S) const override {
1023 initializePackExpansion(S);
1024 size_t Idx = S.CurrentPackIndex;
1025 if (Idx < Data.size())
1026 Data[Idx]->printLeft(S);
1028 void printRight(OutputStream &S) const override {
1029 initializePackExpansion(S);
1030 size_t Idx = S.CurrentPackIndex;
1031 if (Idx < Data.size())
1032 Data[Idx]->printRight(S);
1036 /// A variadic template argument. This node represents an occurrence of
1037 /// J<something>E in some <template-args>. It isn't itself unexpanded, unless
1038 /// one of it's Elements is. The parser inserts a ParameterPack into the
1039 /// TemplateParams table if the <template-args> this pack belongs to apply to an
1041 class TemplateArgumentPack final : public Node {
1044 TemplateArgumentPack(NodeArray Elements_)
1045 : Node(KTemplateArgumentPack), Elements(Elements_) {}
1047 template<typename Fn> void match(Fn F) const { F(Elements); }
1049 NodeArray getElements() const { return Elements; }
1051 void printLeft(OutputStream &S) const override {
1052 Elements.printWithComma(S);
1056 /// A pack expansion. Below this node, there are some unexpanded ParameterPacks
1057 /// which each have Child->ParameterPackSize elements.
1058 class ParameterPackExpansion final : public Node {
1062 ParameterPackExpansion(const Node *Child_)
1063 : Node(KParameterPackExpansion), Child(Child_) {}
1065 template<typename Fn> void match(Fn F) const { F(Child); }
1067 const Node *getChild() const { return Child; }
1069 void printLeft(OutputStream &S) const override {
1070 constexpr unsigned Max = std::numeric_limits<unsigned>::max();
1071 SwapAndRestore<unsigned> SavePackIdx(S.CurrentPackIndex, Max);
1072 SwapAndRestore<unsigned> SavePackMax(S.CurrentPackMax, Max);
1073 size_t StreamPos = S.getCurrentPosition();
1075 // Print the first element in the pack. If Child contains a ParameterPack,
1076 // it will set up S.CurrentPackMax and print the first element.
1079 // No ParameterPack was found in Child. This can occur if we've found a pack
1080 // expansion on a <function-param>.
1081 if (S.CurrentPackMax == Max) {
1086 // We found a ParameterPack, but it has no elements. Erase whatever we may
1088 if (S.CurrentPackMax == 0) {
1089 S.setCurrentPosition(StreamPos);
1093 // Else, iterate through the rest of the elements in the pack.
1094 for (unsigned I = 1, E = S.CurrentPackMax; I < E; ++I) {
1096 S.CurrentPackIndex = I;
1102 class TemplateArgs final : public Node {
1106 TemplateArgs(NodeArray Params_) : Node(KTemplateArgs), Params(Params_) {}
1108 template<typename Fn> void match(Fn F) const { F(Params); }
1110 NodeArray getParams() { return Params; }
1112 void printLeft(OutputStream &S) const override {
1114 Params.printWithComma(S);
1115 if (S.back() == '>')
1121 /// A forward-reference to a template argument that was not known at the point
1122 /// where the template parameter name was parsed in a mangling.
1124 /// This is created when demangling the name of a specialization of a
1125 /// conversion function template:
1129 /// template<typename T> operator T*();
1133 /// When demangling a specialization of the conversion function template, we
1134 /// encounter the name of the template (including the \c T) before we reach
1135 /// the template argument list, so we cannot substitute the parameter name
1136 /// for the corresponding argument while parsing. Instead, we create a
1137 /// \c ForwardTemplateReference node that is resolved after we parse the
1138 /// template arguments.
1139 struct ForwardTemplateReference : Node {
1141 Node *Ref = nullptr;
1143 // If we're currently printing this node. It is possible (though invalid) for
1144 // a forward template reference to refer to itself via a substitution. This
1145 // creates a cyclic AST, which will stack overflow printing. To fix this, bail
1146 // out if more than one print* function is active.
1147 mutable bool Printing = false;
1149 ForwardTemplateReference(size_t Index_)
1150 : Node(KForwardTemplateReference, Cache::Unknown, Cache::Unknown,
1154 // We don't provide a matcher for these, because the value of the node is
1155 // not determined by its construction parameters, and it generally needs
1156 // special handling.
1157 template<typename Fn> void match(Fn F) const = delete;
1159 bool hasRHSComponentSlow(OutputStream &S) const override {
1162 SwapAndRestore<bool> SavePrinting(Printing, true);
1163 return Ref->hasRHSComponent(S);
1165 bool hasArraySlow(OutputStream &S) const override {
1168 SwapAndRestore<bool> SavePrinting(Printing, true);
1169 return Ref->hasArray(S);
1171 bool hasFunctionSlow(OutputStream &S) const override {
1174 SwapAndRestore<bool> SavePrinting(Printing, true);
1175 return Ref->hasFunction(S);
1177 const Node *getSyntaxNode(OutputStream &S) const override {
1180 SwapAndRestore<bool> SavePrinting(Printing, true);
1181 return Ref->getSyntaxNode(S);
1184 void printLeft(OutputStream &S) const override {
1187 SwapAndRestore<bool> SavePrinting(Printing, true);
1190 void printRight(OutputStream &S) const override {
1193 SwapAndRestore<bool> SavePrinting(Printing, true);
1198 struct NameWithTemplateArgs : Node {
1199 // name<template_args>
1203 NameWithTemplateArgs(Node *Name_, Node *TemplateArgs_)
1204 : Node(KNameWithTemplateArgs), Name(Name_), TemplateArgs(TemplateArgs_) {}
1206 template<typename Fn> void match(Fn F) const { F(Name, TemplateArgs); }
1208 StringView getBaseName() const override { return Name->getBaseName(); }
1210 void printLeft(OutputStream &S) const override {
1212 TemplateArgs->print(S);
1216 class GlobalQualifiedName final : public Node {
1220 GlobalQualifiedName(Node* Child_)
1221 : Node(KGlobalQualifiedName), Child(Child_) {}
1223 template<typename Fn> void match(Fn F) const { F(Child); }
1225 StringView getBaseName() const override { return Child->getBaseName(); }
1227 void printLeft(OutputStream &S) const override {
1233 struct StdQualifiedName : Node {
1236 StdQualifiedName(Node *Child_) : Node(KStdQualifiedName), Child(Child_) {}
1238 template<typename Fn> void match(Fn F) const { F(Child); }
1240 StringView getBaseName() const override { return Child->getBaseName(); }
1242 void printLeft(OutputStream &S) const override {
1248 enum class SpecialSubKind {
1257 class ExpandedSpecialSubstitution final : public Node {
1261 ExpandedSpecialSubstitution(SpecialSubKind SSK_)
1262 : Node(KExpandedSpecialSubstitution), SSK(SSK_) {}
1264 template<typename Fn> void match(Fn F) const { F(SSK); }
1266 StringView getBaseName() const override {
1268 case SpecialSubKind::allocator:
1269 return StringView("allocator");
1270 case SpecialSubKind::basic_string:
1271 return StringView("basic_string");
1272 case SpecialSubKind::string:
1273 return StringView("basic_string");
1274 case SpecialSubKind::istream:
1275 return StringView("basic_istream");
1276 case SpecialSubKind::ostream:
1277 return StringView("basic_ostream");
1278 case SpecialSubKind::iostream:
1279 return StringView("basic_iostream");
1281 LLVM_BUILTIN_UNREACHABLE;
1284 void printLeft(OutputStream &S) const override {
1286 case SpecialSubKind::allocator:
1287 S += "std::allocator";
1289 case SpecialSubKind::basic_string:
1290 S += "std::basic_string";
1292 case SpecialSubKind::string:
1293 S += "std::basic_string<char, std::char_traits<char>, "
1294 "std::allocator<char> >";
1296 case SpecialSubKind::istream:
1297 S += "std::basic_istream<char, std::char_traits<char> >";
1299 case SpecialSubKind::ostream:
1300 S += "std::basic_ostream<char, std::char_traits<char> >";
1302 case SpecialSubKind::iostream:
1303 S += "std::basic_iostream<char, std::char_traits<char> >";
1309 class SpecialSubstitution final : public Node {
1313 SpecialSubstitution(SpecialSubKind SSK_)
1314 : Node(KSpecialSubstitution), SSK(SSK_) {}
1316 template<typename Fn> void match(Fn F) const { F(SSK); }
1318 StringView getBaseName() const override {
1320 case SpecialSubKind::allocator:
1321 return StringView("allocator");
1322 case SpecialSubKind::basic_string:
1323 return StringView("basic_string");
1324 case SpecialSubKind::string:
1325 return StringView("string");
1326 case SpecialSubKind::istream:
1327 return StringView("istream");
1328 case SpecialSubKind::ostream:
1329 return StringView("ostream");
1330 case SpecialSubKind::iostream:
1331 return StringView("iostream");
1333 LLVM_BUILTIN_UNREACHABLE;
1336 void printLeft(OutputStream &S) const override {
1338 case SpecialSubKind::allocator:
1339 S += "std::allocator";
1341 case SpecialSubKind::basic_string:
1342 S += "std::basic_string";
1344 case SpecialSubKind::string:
1347 case SpecialSubKind::istream:
1348 S += "std::istream";
1350 case SpecialSubKind::ostream:
1351 S += "std::ostream";
1353 case SpecialSubKind::iostream:
1354 S += "std::iostream";
1360 class CtorDtorName final : public Node {
1361 const Node *Basename;
1366 CtorDtorName(const Node *Basename_, bool IsDtor_, int Variant_)
1367 : Node(KCtorDtorName), Basename(Basename_), IsDtor(IsDtor_),
1368 Variant(Variant_) {}
1370 template<typename Fn> void match(Fn F) const { F(Basename, IsDtor, Variant); }
1372 void printLeft(OutputStream &S) const override {
1375 S += Basename->getBaseName();
1379 class DtorName : public Node {
1383 DtorName(const Node *Base_) : Node(KDtorName), Base(Base_) {}
1385 template<typename Fn> void match(Fn F) const { F(Base); }
1387 void printLeft(OutputStream &S) const override {
1393 class UnnamedTypeName : public Node {
1394 const StringView Count;
1397 UnnamedTypeName(StringView Count_) : Node(KUnnamedTypeName), Count(Count_) {}
1399 template<typename Fn> void match(Fn F) const { F(Count); }
1401 void printLeft(OutputStream &S) const override {
1408 class ClosureTypeName : public Node {
1413 ClosureTypeName(NodeArray Params_, StringView Count_)
1414 : Node(KClosureTypeName), Params(Params_), Count(Count_) {}
1416 template<typename Fn> void match(Fn F) const { F(Params, Count); }
1418 void printLeft(OutputStream &S) const override {
1422 Params.printWithComma(S);
1427 class StructuredBindingName : public Node {
1430 StructuredBindingName(NodeArray Bindings_)
1431 : Node(KStructuredBindingName), Bindings(Bindings_) {}
1433 template<typename Fn> void match(Fn F) const { F(Bindings); }
1435 void printLeft(OutputStream &S) const override {
1437 Bindings.printWithComma(S);
1442 // -- Expression Nodes --
1444 class BinaryExpr : public Node {
1446 const StringView InfixOperator;
1450 BinaryExpr(const Node *LHS_, StringView InfixOperator_, const Node *RHS_)
1451 : Node(KBinaryExpr), LHS(LHS_), InfixOperator(InfixOperator_), RHS(RHS_) {
1454 template<typename Fn> void match(Fn F) const { F(LHS, InfixOperator, RHS); }
1456 void printLeft(OutputStream &S) const override {
1457 // might be a template argument expression, then we need to disambiguate
1459 if (InfixOperator == ">")
1470 if (InfixOperator == ">")
1475 class ArraySubscriptExpr : public Node {
1480 ArraySubscriptExpr(const Node *Op1_, const Node *Op2_)
1481 : Node(KArraySubscriptExpr), Op1(Op1_), Op2(Op2_) {}
1483 template<typename Fn> void match(Fn F) const { F(Op1, Op2); }
1485 void printLeft(OutputStream &S) const override {
1494 class PostfixExpr : public Node {
1496 const StringView Operator;
1499 PostfixExpr(const Node *Child_, StringView Operator_)
1500 : Node(KPostfixExpr), Child(Child_), Operator(Operator_) {}
1502 template<typename Fn> void match(Fn F) const { F(Child, Operator); }
1504 void printLeft(OutputStream &S) const override {
1512 class ConditionalExpr : public Node {
1518 ConditionalExpr(const Node *Cond_, const Node *Then_, const Node *Else_)
1519 : Node(KConditionalExpr), Cond(Cond_), Then(Then_), Else(Else_) {}
1521 template<typename Fn> void match(Fn F) const { F(Cond, Then, Else); }
1523 void printLeft(OutputStream &S) const override {
1534 class MemberExpr : public Node {
1536 const StringView Kind;
1540 MemberExpr(const Node *LHS_, StringView Kind_, const Node *RHS_)
1541 : Node(KMemberExpr), LHS(LHS_), Kind(Kind_), RHS(RHS_) {}
1543 template<typename Fn> void match(Fn F) const { F(LHS, Kind, RHS); }
1545 void printLeft(OutputStream &S) const override {
1552 class EnclosingExpr : public Node {
1553 const StringView Prefix;
1555 const StringView Postfix;
1558 EnclosingExpr(StringView Prefix_, Node *Infix_, StringView Postfix_)
1559 : Node(KEnclosingExpr), Prefix(Prefix_), Infix(Infix_),
1560 Postfix(Postfix_) {}
1562 template<typename Fn> void match(Fn F) const { F(Prefix, Infix, Postfix); }
1564 void printLeft(OutputStream &S) const override {
1571 class CastExpr : public Node {
1572 // cast_kind<to>(from)
1573 const StringView CastKind;
1578 CastExpr(StringView CastKind_, const Node *To_, const Node *From_)
1579 : Node(KCastExpr), CastKind(CastKind_), To(To_), From(From_) {}
1581 template<typename Fn> void match(Fn F) const { F(CastKind, To, From); }
1583 void printLeft(OutputStream &S) const override {
1593 class SizeofParamPackExpr : public Node {
1597 SizeofParamPackExpr(const Node *Pack_)
1598 : Node(KSizeofParamPackExpr), Pack(Pack_) {}
1600 template<typename Fn> void match(Fn F) const { F(Pack); }
1602 void printLeft(OutputStream &S) const override {
1604 ParameterPackExpansion PPE(Pack);
1610 class CallExpr : public Node {
1615 CallExpr(const Node *Callee_, NodeArray Args_)
1616 : Node(KCallExpr), Callee(Callee_), Args(Args_) {}
1618 template<typename Fn> void match(Fn F) const { F(Callee, Args); }
1620 void printLeft(OutputStream &S) const override {
1623 Args.printWithComma(S);
1628 class NewExpr : public Node {
1629 // new (expr_list) type(init_list)
1633 bool IsGlobal; // ::operator new ?
1634 bool IsArray; // new[] ?
1636 NewExpr(NodeArray ExprList_, Node *Type_, NodeArray InitList_, bool IsGlobal_,
1638 : Node(KNewExpr), ExprList(ExprList_), Type(Type_), InitList(InitList_),
1639 IsGlobal(IsGlobal_), IsArray(IsArray_) {}
1641 template<typename Fn> void match(Fn F) const {
1642 F(ExprList, Type, InitList, IsGlobal, IsArray);
1645 void printLeft(OutputStream &S) const override {
1652 if (!ExprList.empty()) {
1654 ExprList.printWithComma(S);
1658 if (!InitList.empty()) {
1660 InitList.printWithComma(S);
1667 class DeleteExpr : public Node {
1673 DeleteExpr(Node *Op_, bool IsGlobal_, bool IsArray_)
1674 : Node(KDeleteExpr), Op(Op_), IsGlobal(IsGlobal_), IsArray(IsArray_) {}
1676 template<typename Fn> void match(Fn F) const { F(Op, IsGlobal, IsArray); }
1678 void printLeft(OutputStream &S) const override {
1688 class PrefixExpr : public Node {
1693 PrefixExpr(StringView Prefix_, Node *Child_)
1694 : Node(KPrefixExpr), Prefix(Prefix_), Child(Child_) {}
1696 template<typename Fn> void match(Fn F) const { F(Prefix, Child); }
1698 void printLeft(OutputStream &S) const override {
1706 class FunctionParam : public Node {
1710 FunctionParam(StringView Number_) : Node(KFunctionParam), Number(Number_) {}
1712 template<typename Fn> void match(Fn F) const { F(Number); }
1714 void printLeft(OutputStream &S) const override {
1720 class ConversionExpr : public Node {
1722 NodeArray Expressions;
1725 ConversionExpr(const Node *Type_, NodeArray Expressions_)
1726 : Node(KConversionExpr), Type(Type_), Expressions(Expressions_) {}
1728 template<typename Fn> void match(Fn F) const { F(Type, Expressions); }
1730 void printLeft(OutputStream &S) const override {
1734 Expressions.printWithComma(S);
1739 class InitListExpr : public Node {
1743 InitListExpr(const Node *Ty_, NodeArray Inits_)
1744 : Node(KInitListExpr), Ty(Ty_), Inits(Inits_) {}
1746 template<typename Fn> void match(Fn F) const { F(Ty, Inits); }
1748 void printLeft(OutputStream &S) const override {
1752 Inits.printWithComma(S);
1757 class BracedExpr : public Node {
1762 BracedExpr(const Node *Elem_, const Node *Init_, bool IsArray_)
1763 : Node(KBracedExpr), Elem(Elem_), Init(Init_), IsArray(IsArray_) {}
1765 template<typename Fn> void match(Fn F) const { F(Elem, Init, IsArray); }
1767 void printLeft(OutputStream &S) const override {
1776 if (Init->getKind() != KBracedExpr && Init->getKind() != KBracedRangeExpr)
1782 class BracedRangeExpr : public Node {
1787 BracedRangeExpr(const Node *First_, const Node *Last_, const Node *Init_)
1788 : Node(KBracedRangeExpr), First(First_), Last(Last_), Init(Init_) {}
1790 template<typename Fn> void match(Fn F) const { F(First, Last, Init); }
1792 void printLeft(OutputStream &S) const override {
1798 if (Init->getKind() != KBracedExpr && Init->getKind() != KBracedRangeExpr)
1804 class FoldExpr : public Node {
1805 const Node *Pack, *Init;
1806 StringView OperatorName;
1810 FoldExpr(bool IsLeftFold_, StringView OperatorName_, const Node *Pack_,
1812 : Node(KFoldExpr), Pack(Pack_), Init(Init_), OperatorName(OperatorName_),
1813 IsLeftFold(IsLeftFold_) {}
1815 template<typename Fn> void match(Fn F) const {
1816 F(IsLeftFold, OperatorName, Pack, Init);
1819 void printLeft(OutputStream &S) const override {
1820 auto PrintPack = [&] {
1822 ParameterPackExpansion(Pack).print(S);
1829 // init op ... op pack
1830 if (Init != nullptr) {
1841 } else { // !IsLeftFold
1847 // pack op ... op init
1848 if (Init != nullptr) {
1859 class ThrowExpr : public Node {
1863 ThrowExpr(const Node *Op_) : Node(KThrowExpr), Op(Op_) {}
1865 template<typename Fn> void match(Fn F) const { F(Op); }
1867 void printLeft(OutputStream &S) const override {
1873 class BoolExpr : public Node {
1877 BoolExpr(bool Value_) : Node(KBoolExpr), Value(Value_) {}
1879 template<typename Fn> void match(Fn F) const { F(Value); }
1881 void printLeft(OutputStream &S) const override {
1882 S += Value ? StringView("true") : StringView("false");
1886 class IntegerCastExpr : public Node {
1892 IntegerCastExpr(const Node *Ty_, StringView Integer_)
1893 : Node(KIntegerCastExpr), Ty(Ty_), Integer(Integer_) {}
1895 template<typename Fn> void match(Fn F) const { F(Ty, Integer); }
1897 void printLeft(OutputStream &S) const override {
1905 class IntegerLiteral : public Node {
1910 IntegerLiteral(StringView Type_, StringView Value_)
1911 : Node(KIntegerLiteral), Type(Type_), Value(Value_) {}
1913 template<typename Fn> void match(Fn F) const { F(Type, Value); }
1915 void printLeft(OutputStream &S) const override {
1916 if (Type.size() > 3) {
1922 if (Value[0] == 'n') {
1924 S += Value.dropFront(1);
1928 if (Type.size() <= 3)
1933 template <class Float> struct FloatData;
1935 namespace float_literal_impl {
1936 constexpr Node::Kind getFloatLiteralKind(float *) {
1937 return Node::KFloatLiteral;
1939 constexpr Node::Kind getFloatLiteralKind(double *) {
1940 return Node::KDoubleLiteral;
1942 constexpr Node::Kind getFloatLiteralKind(long double *) {
1943 return Node::KLongDoubleLiteral;
1947 template <class Float> class FloatLiteralImpl : public Node {
1948 const StringView Contents;
1950 static constexpr Kind KindForClass =
1951 float_literal_impl::getFloatLiteralKind((Float *)nullptr);
1954 FloatLiteralImpl(StringView Contents_)
1955 : Node(KindForClass), Contents(Contents_) {}
1957 template<typename Fn> void match(Fn F) const { F(Contents); }
1959 void printLeft(OutputStream &s) const override {
1960 const char *first = Contents.begin();
1961 const char *last = Contents.end() + 1;
1963 const size_t N = FloatData<Float>::mangled_size;
1964 if (static_cast<std::size_t>(last - first) > N) {
1968 char buf[sizeof(Float)];
1970 const char *t = first;
1972 for (; t != last; ++t, ++e) {
1973 unsigned d1 = isdigit(*t) ? static_cast<unsigned>(*t - '0')
1974 : static_cast<unsigned>(*t - 'a' + 10);
1976 unsigned d0 = isdigit(*t) ? static_cast<unsigned>(*t - '0')
1977 : static_cast<unsigned>(*t - 'a' + 10);
1978 *e = static_cast<char>((d1 << 4) + d0);
1980 #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
1981 std::reverse(buf, e);
1983 char num[FloatData<Float>::max_demangled_size] = {0};
1984 int n = snprintf(num, sizeof(num), FloatData<Float>::spec, value);
1985 s += StringView(num, num + n);
1990 using FloatLiteral = FloatLiteralImpl<float>;
1991 using DoubleLiteral = FloatLiteralImpl<double>;
1992 using LongDoubleLiteral = FloatLiteralImpl<long double>;
1994 /// Visit the node. Calls \c F(P), where \c P is the node cast to the
1995 /// appropriate derived class.
1996 template<typename Fn>
1997 void Node::visit(Fn F) const {
1999 #define CASE(X) case K ## X: return F(static_cast<const X*>(this));
2000 FOR_EACH_NODE_KIND(CASE)
2003 assert(0 && "unknown mangling node kind");
2006 /// Determine the kind of a node from its type.
2007 template<typename NodeT> struct NodeKind;
2008 #define SPECIALIZATION(X) \
2009 template<> struct NodeKind<X> { \
2010 static constexpr Node::Kind Kind = Node::K##X; \
2011 static constexpr const char *name() { return #X; } \
2013 FOR_EACH_NODE_KIND(SPECIALIZATION)
2014 #undef SPECIALIZATION
2016 #undef FOR_EACH_NODE_KIND
2018 template <class T, size_t N>
2019 class PODSmallVector {
2020 static_assert(std::is_pod<T>::value,
2021 "T is required to be a plain old data type");
2028 bool isInline() const { return First == Inline; }
2030 void clearInline() {
2036 void reserve(size_t NewCap) {
2039 auto* Tmp = static_cast<T*>(std::malloc(NewCap * sizeof(T)));
2042 std::copy(First, Last, Tmp);
2045 First = static_cast<T*>(std::realloc(First, NewCap * sizeof(T)));
2046 if (First == nullptr)
2050 Cap = First + NewCap;
2054 PODSmallVector() : First(Inline), Last(First), Cap(Inline + N) {}
2056 PODSmallVector(const PODSmallVector&) = delete;
2057 PODSmallVector& operator=(const PODSmallVector&) = delete;
2059 PODSmallVector(PODSmallVector&& Other) : PODSmallVector() {
2060 if (Other.isInline()) {
2061 std::copy(Other.begin(), Other.end(), First);
2062 Last = First + Other.size();
2067 First = Other.First;
2070 Other.clearInline();
2073 PODSmallVector& operator=(PODSmallVector&& Other) {
2074 if (Other.isInline()) {
2079 std::copy(Other.begin(), Other.end(), First);
2080 Last = First + Other.size();
2086 First = Other.First;
2089 Other.clearInline();
2093 std::swap(First, Other.First);
2094 std::swap(Last, Other.Last);
2095 std::swap(Cap, Other.Cap);
2100 void push_back(const T& Elem) {
2102 reserve(size() * 2);
2107 assert(Last != First && "Popping empty vector!");
2111 void dropBack(size_t Index) {
2112 assert(Index <= size() && "dropBack() can't expand!");
2113 Last = First + Index;
2116 T* begin() { return First; }
2117 T* end() { return Last; }
2119 bool empty() const { return First == Last; }
2120 size_t size() const { return static_cast<size_t>(Last - First); }
2122 assert(Last != First && "Calling back() on empty vector!");
2125 T& operator[](size_t Index) {
2126 assert(Index < size() && "Invalid access!");
2127 return *(begin() + Index);
2129 void clear() { Last = First; }
2137 template <typename Derived, typename Alloc> struct AbstractManglingParser {
2141 // Name stack, this is used by the parser to hold temporary names that were
2142 // parsed. The parser collapses multiple names into new nodes to construct
2143 // the AST. Once the parser is finished, names.size() == 1.
2144 PODSmallVector<Node *, 32> Names;
2146 // Substitution table. Itanium supports name substitutions as a means of
2147 // compression. The string "S42_" refers to the 44nd entry (base-36) in this
2149 PODSmallVector<Node *, 32> Subs;
2151 // Template parameter table. Like the above, but referenced like "T42_".
2152 // This has a smaller size compared to Subs and Names because it can be
2153 // stored on the stack.
2154 PODSmallVector<Node *, 8> TemplateParams;
2156 // Set of unresolved forward <template-param> references. These can occur in a
2157 // conversion operator's type, and are resolved in the enclosing <encoding>.
2158 PODSmallVector<ForwardTemplateReference *, 4> ForwardTemplateRefs;
2160 bool TryToParseTemplateArgs = true;
2161 bool PermitForwardTemplateReferences = false;
2162 bool ParsingLambdaParams = false;
2166 AbstractManglingParser(const char *First_, const char *Last_)
2167 : First(First_), Last(Last_) {}
2169 Derived &getDerived() { return static_cast<Derived &>(*this); }
2171 void reset(const char *First_, const char *Last_) {
2176 TemplateParams.clear();
2177 ParsingLambdaParams = false;
2178 TryToParseTemplateArgs = true;
2179 PermitForwardTemplateReferences = false;
2180 ASTAllocator.reset();
2183 template <class T, class... Args> Node *make(Args &&... args) {
2184 return ASTAllocator.template makeNode<T>(std::forward<Args>(args)...);
2187 template <class It> NodeArray makeNodeArray(It begin, It end) {
2188 size_t sz = static_cast<size_t>(end - begin);
2189 void *mem = ASTAllocator.allocateNodeArray(sz);
2190 Node **data = new (mem) Node *[sz];
2191 std::copy(begin, end, data);
2192 return NodeArray(data, sz);
2195 NodeArray popTrailingNodeArray(size_t FromPosition) {
2196 assert(FromPosition <= Names.size());
2198 makeNodeArray(Names.begin() + (long)FromPosition, Names.end());
2199 Names.dropBack(FromPosition);
2203 bool consumeIf(StringView S) {
2204 if (StringView(First, Last).startsWith(S)) {
2211 bool consumeIf(char C) {
2212 if (First != Last && *First == C) {
2219 char consume() { return First != Last ? *First++ : '\0'; }
2221 char look(unsigned Lookahead = 0) {
2222 if (static_cast<size_t>(Last - First) <= Lookahead)
2224 return First[Lookahead];
2227 size_t numLeft() const { return static_cast<size_t>(Last - First); }
2229 StringView parseNumber(bool AllowNegative = false);
2230 Qualifiers parseCVQualifiers();
2231 bool parsePositiveInteger(size_t *Out);
2232 StringView parseBareSourceName();
2234 bool parseSeqId(size_t *Out);
2235 Node *parseSubstitution();
2236 Node *parseTemplateParam();
2237 Node *parseTemplateArgs(bool TagTemplates = false);
2238 Node *parseTemplateArg();
2240 /// Parse the <expr> production.
2242 Node *parsePrefixExpr(StringView Kind);
2243 Node *parseBinaryExpr(StringView Kind);
2244 Node *parseIntegerLiteral(StringView Lit);
2245 Node *parseExprPrimary();
2246 template <class Float> Node *parseFloatingLiteral();
2247 Node *parseFunctionParam();
2248 Node *parseNewExpr();
2249 Node *parseConversionExpr();
2250 Node *parseBracedExpr();
2251 Node *parseFoldExpr();
2253 /// Parse the <type> production.
2255 Node *parseFunctionType();
2256 Node *parseVectorType();
2257 Node *parseDecltype();
2258 Node *parseArrayType();
2259 Node *parsePointerToMemberType();
2260 Node *parseClassEnumType();
2261 Node *parseQualifiedType();
2263 Node *parseEncoding();
2264 bool parseCallOffset();
2265 Node *parseSpecialName();
2267 /// Holds some extra information about a <name> that is being parsed. This
2268 /// information is only pertinent if the <name> refers to an <encoding>.
2270 bool CtorDtorConversion = false;
2271 bool EndsWithTemplateArgs = false;
2272 Qualifiers CVQualifiers = QualNone;
2273 FunctionRefQual ReferenceQualifier = FrefQualNone;
2274 size_t ForwardTemplateRefsBegin;
2276 NameState(AbstractManglingParser *Enclosing)
2277 : ForwardTemplateRefsBegin(Enclosing->ForwardTemplateRefs.size()) {}
2280 bool resolveForwardTemplateRefs(NameState &State) {
2281 size_t I = State.ForwardTemplateRefsBegin;
2282 size_t E = ForwardTemplateRefs.size();
2283 for (; I < E; ++I) {
2284 size_t Idx = ForwardTemplateRefs[I]->Index;
2285 if (Idx >= TemplateParams.size())
2287 ForwardTemplateRefs[I]->Ref = TemplateParams[Idx];
2289 ForwardTemplateRefs.dropBack(State.ForwardTemplateRefsBegin);
2293 /// Parse the <name> production>
2294 Node *parseName(NameState *State = nullptr);
2295 Node *parseLocalName(NameState *State);
2296 Node *parseOperatorName(NameState *State);
2297 Node *parseUnqualifiedName(NameState *State);
2298 Node *parseUnnamedTypeName(NameState *State);
2299 Node *parseSourceName(NameState *State);
2300 Node *parseUnscopedName(NameState *State);
2301 Node *parseNestedName(NameState *State);
2302 Node *parseCtorDtorName(Node *&SoFar, NameState *State);
2304 Node *parseAbiTags(Node *N);
2306 /// Parse the <unresolved-name> production.
2307 Node *parseUnresolvedName();
2308 Node *parseSimpleId();
2309 Node *parseBaseUnresolvedName();
2310 Node *parseUnresolvedType();
2311 Node *parseDestructorName();
2313 /// Top-level entry point into the parser.
2317 const char* parse_discriminator(const char* first, const char* last);
2319 // <name> ::= <nested-name> // N
2320 // ::= <local-name> # See Scope Encoding below // Z
2321 // ::= <unscoped-template-name> <template-args>
2322 // ::= <unscoped-name>
2324 // <unscoped-template-name> ::= <unscoped-name>
2325 // ::= <substitution>
2326 template <typename Derived, typename Alloc>
2327 Node *AbstractManglingParser<Derived, Alloc>::parseName(NameState *State) {
2328 consumeIf('L'); // extension
2331 return getDerived().parseNestedName(State);
2333 return getDerived().parseLocalName(State);
2335 // ::= <unscoped-template-name> <template-args>
2336 if (look() == 'S' && look(1) != 't') {
2337 Node *S = getDerived().parseSubstitution();
2342 Node *TA = getDerived().parseTemplateArgs(State != nullptr);
2345 if (State) State->EndsWithTemplateArgs = true;
2346 return make<NameWithTemplateArgs>(S, TA);
2349 Node *N = getDerived().parseUnscopedName(State);
2352 // ::= <unscoped-template-name> <template-args>
2353 if (look() == 'I') {
2355 Node *TA = getDerived().parseTemplateArgs(State != nullptr);
2358 if (State) State->EndsWithTemplateArgs = true;
2359 return make<NameWithTemplateArgs>(N, TA);
2361 // ::= <unscoped-name>
2365 // <local-name> := Z <function encoding> E <entity name> [<discriminator>]
2366 // := Z <function encoding> E s [<discriminator>]
2367 // := Z <function encoding> Ed [ <parameter number> ] _ <entity name>
2368 template <typename Derived, typename Alloc>
2369 Node *AbstractManglingParser<Derived, Alloc>::parseLocalName(NameState *State) {
2370 if (!consumeIf('Z'))
2372 Node *Encoding = getDerived().parseEncoding();
2373 if (Encoding == nullptr || !consumeIf('E'))
2376 if (consumeIf('s')) {
2377 First = parse_discriminator(First, Last);
2378 auto *StringLitName = make<NameType>("string literal");
2381 return make<LocalName>(Encoding, StringLitName);
2384 if (consumeIf('d')) {
2386 if (!consumeIf('_'))
2388 Node *N = getDerived().parseName(State);
2391 return make<LocalName>(Encoding, N);
2394 Node *Entity = getDerived().parseName(State);
2395 if (Entity == nullptr)
2397 First = parse_discriminator(First, Last);
2398 return make<LocalName>(Encoding, Entity);
2401 // <unscoped-name> ::= <unqualified-name>
2402 // ::= St <unqualified-name> # ::std::
2403 // extension ::= StL<unqualified-name>
2404 template <typename Derived, typename Alloc>
2406 AbstractManglingParser<Derived, Alloc>::parseUnscopedName(NameState *State) {
2407 if (consumeIf("StL") || consumeIf("St")) {
2408 Node *R = getDerived().parseUnqualifiedName(State);
2411 return make<StdQualifiedName>(R);
2413 return getDerived().parseUnqualifiedName(State);
2416 // <unqualified-name> ::= <operator-name> [abi-tags]
2417 // ::= <ctor-dtor-name>
2418 // ::= <source-name>
2419 // ::= <unnamed-type-name>
2420 // ::= DC <source-name>+ E # structured binding declaration
2421 template <typename Derived, typename Alloc>
2423 AbstractManglingParser<Derived, Alloc>::parseUnqualifiedName(NameState *State) {
2424 // <ctor-dtor-name>s are special-cased in parseNestedName().
2427 Result = getDerived().parseUnnamedTypeName(State);
2428 else if (look() >= '1' && look() <= '9')
2429 Result = getDerived().parseSourceName(State);
2430 else if (consumeIf("DC")) {
2431 size_t BindingsBegin = Names.size();
2433 Node *Binding = getDerived().parseSourceName(State);
2434 if (Binding == nullptr)
2436 Names.push_back(Binding);
2437 } while (!consumeIf('E'));
2438 Result = make<StructuredBindingName>(popTrailingNodeArray(BindingsBegin));
2440 Result = getDerived().parseOperatorName(State);
2441 if (Result != nullptr)
2442 Result = getDerived().parseAbiTags(Result);
2446 // <unnamed-type-name> ::= Ut [<nonnegative number>] _
2447 // ::= <closure-type-name>
2449 // <closure-type-name> ::= Ul <lambda-sig> E [ <nonnegative number> ] _
2451 // <lambda-sig> ::= <parameter type>+ # Parameter types or "v" if the lambda has no parameters
2452 template <typename Derived, typename Alloc>
2454 AbstractManglingParser<Derived, Alloc>::parseUnnamedTypeName(NameState *) {
2455 if (consumeIf("Ut")) {
2456 StringView Count = parseNumber();
2457 if (!consumeIf('_'))
2459 return make<UnnamedTypeName>(Count);
2461 if (consumeIf("Ul")) {
2463 SwapAndRestore<bool> SwapParams(ParsingLambdaParams, true);
2464 if (!consumeIf("vE")) {
2465 size_t ParamsBegin = Names.size();
2467 Node *P = getDerived().parseType();
2471 } while (!consumeIf('E'));
2472 Params = popTrailingNodeArray(ParamsBegin);
2474 StringView Count = parseNumber();
2475 if (!consumeIf('_'))
2477 return make<ClosureTypeName>(Params, Count);
2482 // <source-name> ::= <positive length number> <identifier>
2483 template <typename Derived, typename Alloc>
2484 Node *AbstractManglingParser<Derived, Alloc>::parseSourceName(NameState *) {
2486 if (parsePositiveInteger(&Length))
2488 if (numLeft() < Length || Length == 0)
2490 StringView Name(First, First + Length);
2492 if (Name.startsWith("_GLOBAL__N"))
2493 return make<NameType>("(anonymous namespace)");
2494 return make<NameType>(Name);
2497 // <operator-name> ::= aa # &&
2498 // ::= ad # & (unary)
2505 // ::= cv <type> # (cast)
2506 // ::= da # delete[]
2507 // ::= de # * (unary)
2518 // ::= li <source-name> # operator ""
2526 // ::= mm # -- (postfix in <expression> context)
2529 // ::= ng # - (unary)
2538 // ::= pp # ++ (postfix in <expression> context)
2539 // ::= ps # + (unary)
2546 // ::= ss # <=> C++2a
2547 // ::= v <digit> <source-name> # vendor extended operator
2548 template <typename Derived, typename Alloc>
2550 AbstractManglingParser<Derived, Alloc>::parseOperatorName(NameState *State) {
2556 return make<NameType>("operator&&");
2560 return make<NameType>("operator&");
2563 return make<NameType>("operator&=");
2566 return make<NameType>("operator=");
2573 return make<NameType>("operator()");
2576 return make<NameType>("operator,");
2579 return make<NameType>("operator~");
2580 // ::= cv <type> # (cast)
2583 SwapAndRestore<bool> SaveTemplate(TryToParseTemplateArgs, false);
2584 // If we're parsing an encoding, State != nullptr and the conversion
2585 // operators' <type> could have a <template-param> that refers to some
2586 // <template-arg>s further ahead in the mangled name.
2587 SwapAndRestore<bool> SavePermit(PermitForwardTemplateReferences,
2588 PermitForwardTemplateReferences ||
2590 Node *Ty = getDerived().parseType();
2593 if (State) State->CtorDtorConversion = true;
2594 return make<ConversionOperatorType>(Ty);
2602 return make<NameType>("operator delete[]");
2605 return make<NameType>("operator*");
2608 return make<NameType>("operator delete");
2611 return make<NameType>("operator/");
2614 return make<NameType>("operator/=");
2621 return make<NameType>("operator^");
2624 return make<NameType>("operator^=");
2627 return make<NameType>("operator==");
2634 return make<NameType>("operator>=");
2637 return make<NameType>("operator>");
2641 if (look(1) == 'x') {
2643 return make<NameType>("operator[]");
2650 return make<NameType>("operator<=");
2651 // ::= li <source-name> # operator ""
2654 Node *SN = getDerived().parseSourceName(State);
2657 return make<LiteralOperator>(SN);
2661 return make<NameType>("operator<<");
2664 return make<NameType>("operator<<=");
2667 return make<NameType>("operator<");
2674 return make<NameType>("operator-");
2677 return make<NameType>("operator-=");
2680 return make<NameType>("operator*");
2683 return make<NameType>("operator*=");
2686 return make<NameType>("operator--");
2693 return make<NameType>("operator new[]");
2696 return make<NameType>("operator!=");
2699 return make<NameType>("operator-");
2702 return make<NameType>("operator!");
2705 return make<NameType>("operator new");
2712 return make<NameType>("operator||");
2715 return make<NameType>("operator|");
2718 return make<NameType>("operator|=");
2725 return make<NameType>("operator->*");
2728 return make<NameType>("operator+");
2731 return make<NameType>("operator+=");
2734 return make<NameType>("operator++");
2737 return make<NameType>("operator+");
2740 return make<NameType>("operator->");
2744 if (look(1) == 'u') {
2746 return make<NameType>("operator?");
2753 return make<NameType>("operator%");
2756 return make<NameType>("operator%=");
2759 return make<NameType>("operator>>");
2762 return make<NameType>("operator>>=");
2766 if (look(1) == 's') {
2768 return make<NameType>("operator<=>");
2771 // ::= v <digit> <source-name> # vendor extended operator
2773 if (std::isdigit(look(1))) {
2775 Node *SN = getDerived().parseSourceName(State);
2778 return make<ConversionOperatorType>(SN);
2785 // <ctor-dtor-name> ::= C1 # complete object constructor
2786 // ::= C2 # base object constructor
2787 // ::= C3 # complete object allocating constructor
2788 // extension ::= C5 # ?
2789 // ::= D0 # deleting destructor
2790 // ::= D1 # complete object destructor
2791 // ::= D2 # base object destructor
2792 // extension ::= D5 # ?
2793 template <typename Derived, typename Alloc>
2795 AbstractManglingParser<Derived, Alloc>::parseCtorDtorName(Node *&SoFar,
2797 if (SoFar->getKind() == Node::KSpecialSubstitution) {
2798 auto SSK = static_cast<SpecialSubstitution *>(SoFar)->SSK;
2800 case SpecialSubKind::string:
2801 case SpecialSubKind::istream:
2802 case SpecialSubKind::ostream:
2803 case SpecialSubKind::iostream:
2804 SoFar = make<ExpandedSpecialSubstitution>(SSK);
2813 if (consumeIf('C')) {
2814 bool IsInherited = consumeIf('I');
2815 if (look() != '1' && look() != '2' && look() != '3' && look() != '5')
2817 int Variant = look() - '0';
2819 if (State) State->CtorDtorConversion = true;
2821 if (getDerived().parseName(State) == nullptr)
2824 return make<CtorDtorName>(SoFar, false, Variant);
2827 if (look() == 'D' &&
2828 (look(1) == '0' || look(1) == '1' || look(1) == '2' || look(1) == '5')) {
2829 int Variant = look(1) - '0';
2831 if (State) State->CtorDtorConversion = true;
2832 return make<CtorDtorName>(SoFar, true, Variant);
2838 // <nested-name> ::= N [<CV-Qualifiers>] [<ref-qualifier>] <prefix> <unqualified-name> E
2839 // ::= N [<CV-Qualifiers>] [<ref-qualifier>] <template-prefix> <template-args> E
2841 // <prefix> ::= <prefix> <unqualified-name>
2842 // ::= <template-prefix> <template-args>
2843 // ::= <template-param>
2846 // ::= <substitution>
2847 // ::= <prefix> <data-member-prefix>
2850 // <data-member-prefix> := <member source-name> [<template-args>] M
2852 // <template-prefix> ::= <prefix> <template unqualified-name>
2853 // ::= <template-param>
2854 // ::= <substitution>
2855 template <typename Derived, typename Alloc>
2857 AbstractManglingParser<Derived, Alloc>::parseNestedName(NameState *State) {
2858 if (!consumeIf('N'))
2861 Qualifiers CVTmp = parseCVQualifiers();
2862 if (State) State->CVQualifiers = CVTmp;
2864 if (consumeIf('O')) {
2865 if (State) State->ReferenceQualifier = FrefQualRValue;
2866 } else if (consumeIf('R')) {
2867 if (State) State->ReferenceQualifier = FrefQualLValue;
2869 if (State) State->ReferenceQualifier = FrefQualNone;
2871 Node *SoFar = nullptr;
2872 auto PushComponent = [&](Node *Comp) {
2873 if (!Comp) return false;
2874 if (SoFar) SoFar = make<NestedName>(SoFar, Comp);
2876 if (State) State->EndsWithTemplateArgs = false;
2877 return SoFar != nullptr;
2880 if (consumeIf("St")) {
2881 SoFar = make<NameType>("std");
2886 while (!consumeIf('E')) {
2887 consumeIf('L'); // extension
2889 // <data-member-prefix> := <member source-name> [<template-args>] M
2890 if (consumeIf('M')) {
2891 if (SoFar == nullptr)
2896 // ::= <template-param>
2897 if (look() == 'T') {
2898 if (!PushComponent(getDerived().parseTemplateParam()))
2900 Subs.push_back(SoFar);
2904 // ::= <template-prefix> <template-args>
2905 if (look() == 'I') {
2906 Node *TA = getDerived().parseTemplateArgs(State != nullptr);
2907 if (TA == nullptr || SoFar == nullptr)
2909 SoFar = make<NameWithTemplateArgs>(SoFar, TA);
2912 if (State) State->EndsWithTemplateArgs = true;
2913 Subs.push_back(SoFar);
2918 if (look() == 'D' && (look(1) == 't' || look(1) == 'T')) {
2919 if (!PushComponent(getDerived().parseDecltype()))
2921 Subs.push_back(SoFar);
2925 // ::= <substitution>
2926 if (look() == 'S' && look(1) != 't') {
2927 Node *S = getDerived().parseSubstitution();
2928 if (!PushComponent(S))
2935 // Parse an <unqualified-name> thats actually a <ctor-dtor-name>.
2936 if (look() == 'C' || (look() == 'D' && look(1) != 'C')) {
2937 if (SoFar == nullptr)
2939 if (!PushComponent(getDerived().parseCtorDtorName(SoFar, State)))
2941 SoFar = getDerived().parseAbiTags(SoFar);
2942 if (SoFar == nullptr)
2944 Subs.push_back(SoFar);
2948 // ::= <prefix> <unqualified-name>
2949 if (!PushComponent(getDerived().parseUnqualifiedName(State)))
2951 Subs.push_back(SoFar);
2954 if (SoFar == nullptr || Subs.empty())
2961 // <simple-id> ::= <source-name> [ <template-args> ]
2962 template <typename Derived, typename Alloc>
2963 Node *AbstractManglingParser<Derived, Alloc>::parseSimpleId() {
2964 Node *SN = getDerived().parseSourceName(/*NameState=*/nullptr);
2967 if (look() == 'I') {
2968 Node *TA = getDerived().parseTemplateArgs();
2971 return make<NameWithTemplateArgs>(SN, TA);
2976 // <destructor-name> ::= <unresolved-type> # e.g., ~T or ~decltype(f())
2977 // ::= <simple-id> # e.g., ~A<2*N>
2978 template <typename Derived, typename Alloc>
2979 Node *AbstractManglingParser<Derived, Alloc>::parseDestructorName() {
2981 if (std::isdigit(look()))
2982 Result = getDerived().parseSimpleId();
2984 Result = getDerived().parseUnresolvedType();
2985 if (Result == nullptr)
2987 return make<DtorName>(Result);
2990 // <unresolved-type> ::= <template-param>
2992 // ::= <substitution>
2993 template <typename Derived, typename Alloc>
2994 Node *AbstractManglingParser<Derived, Alloc>::parseUnresolvedType() {
2995 if (look() == 'T') {
2996 Node *TP = getDerived().parseTemplateParam();
3002 if (look() == 'D') {
3003 Node *DT = getDerived().parseDecltype();
3009 return getDerived().parseSubstitution();
3012 // <base-unresolved-name> ::= <simple-id> # unresolved name
3013 // extension ::= <operator-name> # unresolved operator-function-id
3014 // extension ::= <operator-name> <template-args> # unresolved operator template-id
3015 // ::= on <operator-name> # unresolved operator-function-id
3016 // ::= on <operator-name> <template-args> # unresolved operator template-id
3017 // ::= dn <destructor-name> # destructor or pseudo-destructor;
3018 // # e.g. ~X or ~X<N-1>
3019 template <typename Derived, typename Alloc>
3020 Node *AbstractManglingParser<Derived, Alloc>::parseBaseUnresolvedName() {
3021 if (std::isdigit(look()))
3022 return getDerived().parseSimpleId();
3024 if (consumeIf("dn"))
3025 return getDerived().parseDestructorName();
3029 Node *Oper = getDerived().parseOperatorName(/*NameState=*/nullptr);
3030 if (Oper == nullptr)
3032 if (look() == 'I') {
3033 Node *TA = getDerived().parseTemplateArgs();
3036 return make<NameWithTemplateArgs>(Oper, TA);
3041 // <unresolved-name>
3042 // extension ::= srN <unresolved-type> [<template-args>] <unresolved-qualifier-level>* E <base-unresolved-name>
3043 // ::= [gs] <base-unresolved-name> # x or (with "gs") ::x
3044 // ::= [gs] sr <unresolved-qualifier-level>+ E <base-unresolved-name>
3045 // # A::x, N::y, A<T>::z; "gs" means leading "::"
3046 // ::= sr <unresolved-type> <base-unresolved-name> # T::x / decltype(p)::x
3047 // extension ::= sr <unresolved-type> <template-args> <base-unresolved-name>
3048 // # T::N::x /decltype(p)::N::x
3049 // (ignored) ::= srN <unresolved-type> <unresolved-qualifier-level>+ E <base-unresolved-name>
3051 // <unresolved-qualifier-level> ::= <simple-id>
3052 template <typename Derived, typename Alloc>
3053 Node *AbstractManglingParser<Derived, Alloc>::parseUnresolvedName() {
3054 Node *SoFar = nullptr;
3056 // srN <unresolved-type> [<template-args>] <unresolved-qualifier-level>* E <base-unresolved-name>
3057 // srN <unresolved-type> <unresolved-qualifier-level>+ E <base-unresolved-name>
3058 if (consumeIf("srN")) {
3059 SoFar = getDerived().parseUnresolvedType();
3060 if (SoFar == nullptr)
3063 if (look() == 'I') {
3064 Node *TA = getDerived().parseTemplateArgs();
3067 SoFar = make<NameWithTemplateArgs>(SoFar, TA);
3072 while (!consumeIf('E')) {
3073 Node *Qual = getDerived().parseSimpleId();
3074 if (Qual == nullptr)
3076 SoFar = make<QualifiedName>(SoFar, Qual);
3081 Node *Base = getDerived().parseBaseUnresolvedName();
3082 if (Base == nullptr)
3084 return make<QualifiedName>(SoFar, Base);
3087 bool Global = consumeIf("gs");
3089 // [gs] <base-unresolved-name> # x or (with "gs") ::x
3090 if (!consumeIf("sr")) {
3091 SoFar = getDerived().parseBaseUnresolvedName();
3092 if (SoFar == nullptr)
3095 SoFar = make<GlobalQualifiedName>(SoFar);
3099 // [gs] sr <unresolved-qualifier-level>+ E <base-unresolved-name>
3100 if (std::isdigit(look())) {
3102 Node *Qual = getDerived().parseSimpleId();
3103 if (Qual == nullptr)
3106 SoFar = make<QualifiedName>(SoFar, Qual);
3108 SoFar = make<GlobalQualifiedName>(Qual);
3113 } while (!consumeIf('E'));
3115 // sr <unresolved-type> <base-unresolved-name>
3116 // sr <unresolved-type> <template-args> <base-unresolved-name>
3118 SoFar = getDerived().parseUnresolvedType();
3119 if (SoFar == nullptr)
3122 if (look() == 'I') {
3123 Node *TA = getDerived().parseTemplateArgs();
3126 SoFar = make<NameWithTemplateArgs>(SoFar, TA);
3132 assert(SoFar != nullptr);
3134 Node *Base = getDerived().parseBaseUnresolvedName();
3135 if (Base == nullptr)
3137 return make<QualifiedName>(SoFar, Base);
3140 // <abi-tags> ::= <abi-tag> [<abi-tags>]
3141 // <abi-tag> ::= B <source-name>
3142 template <typename Derived, typename Alloc>
3143 Node *AbstractManglingParser<Derived, Alloc>::parseAbiTags(Node *N) {
3144 while (consumeIf('B')) {
3145 StringView SN = parseBareSourceName();
3148 N = make<AbiTagAttr>(N, SN);
3155 // <number> ::= [n] <non-negative decimal integer>
3156 template <typename Alloc, typename Derived>
3158 AbstractManglingParser<Alloc, Derived>::parseNumber(bool AllowNegative) {
3159 const char *Tmp = First;
3162 if (numLeft() == 0 || !std::isdigit(*First))
3163 return StringView();
3164 while (numLeft() != 0 && std::isdigit(*First))
3166 return StringView(Tmp, First);
3169 // <positive length number> ::= [0-9]*
3170 template <typename Alloc, typename Derived>
3171 bool AbstractManglingParser<Alloc, Derived>::parsePositiveInteger(size_t *Out) {
3173 if (look() < '0' || look() > '9')
3175 while (look() >= '0' && look() <= '9') {
3177 *Out += static_cast<size_t>(consume() - '0');
3182 template <typename Alloc, typename Derived>
3183 StringView AbstractManglingParser<Alloc, Derived>::parseBareSourceName() {
3185 if (parsePositiveInteger(&Int) || numLeft() < Int)
3186 return StringView();
3187 StringView R(First, First + Int);
3192 // <function-type> ::= [<CV-qualifiers>] [<exception-spec>] [Dx] F [Y] <bare-function-type> [<ref-qualifier>] E
3194 // <exception-spec> ::= Do # non-throwing exception-specification (e.g., noexcept, throw())
3195 // ::= DO <expression> E # computed (instantiation-dependent) noexcept
3196 // ::= Dw <type>+ E # dynamic exception specification with instantiation-dependent types
3198 // <ref-qualifier> ::= R # & ref-qualifier
3199 // <ref-qualifier> ::= O # && ref-qualifier
3200 template <typename Derived, typename Alloc>
3201 Node *AbstractManglingParser<Derived, Alloc>::parseFunctionType() {
3202 Qualifiers CVQuals = parseCVQualifiers();
3204 Node *ExceptionSpec = nullptr;
3205 if (consumeIf("Do")) {
3206 ExceptionSpec = make<NameType>("noexcept");
3209 } else if (consumeIf("DO")) {
3210 Node *E = getDerived().parseExpr();
3211 if (E == nullptr || !consumeIf('E'))
3213 ExceptionSpec = make<NoexceptSpec>(E);
3216 } else if (consumeIf("Dw")) {
3217 size_t SpecsBegin = Names.size();
3218 while (!consumeIf('E')) {
3219 Node *T = getDerived().parseType();
3225 make<DynamicExceptionSpec>(popTrailingNodeArray(SpecsBegin));
3230 consumeIf("Dx"); // transaction safe
3232 if (!consumeIf('F'))
3234 consumeIf('Y'); // extern "C"
3235 Node *ReturnType = getDerived().parseType();
3236 if (ReturnType == nullptr)
3239 FunctionRefQual ReferenceQualifier = FrefQualNone;
3240 size_t ParamsBegin = Names.size();
3246 if (consumeIf("RE")) {
3247 ReferenceQualifier = FrefQualLValue;
3250 if (consumeIf("OE")) {
3251 ReferenceQualifier = FrefQualRValue;
3254 Node *T = getDerived().parseType();
3260 NodeArray Params = popTrailingNodeArray(ParamsBegin);
3261 return make<FunctionType>(ReturnType, Params, CVQuals,
3262 ReferenceQualifier, ExceptionSpec);
3266 // <vector-type> ::= Dv <positive dimension number> _ <extended element type>
3267 // ::= Dv [<dimension expression>] _ <element type>
3268 // <extended element type> ::= <element type>
3269 // ::= p # AltiVec vector pixel
3270 template <typename Derived, typename Alloc>
3271 Node *AbstractManglingParser<Derived, Alloc>::parseVectorType() {
3272 if (!consumeIf("Dv"))
3274 if (look() >= '1' && look() <= '9') {
3275 StringView DimensionNumber = parseNumber();
3276 if (!consumeIf('_'))
3279 return make<PixelVectorType>(DimensionNumber);
3280 Node *ElemType = getDerived().parseType();
3281 if (ElemType == nullptr)
3283 return make<VectorType>(ElemType, DimensionNumber);
3286 if (!consumeIf('_')) {
3287 Node *DimExpr = getDerived().parseExpr();
3290 if (!consumeIf('_'))
3292 Node *ElemType = getDerived().parseType();
3295 return make<VectorType>(ElemType, DimExpr);
3297 Node *ElemType = getDerived().parseType();
3300 return make<VectorType>(ElemType, StringView());
3303 // <decltype> ::= Dt <expression> E # decltype of an id-expression or class member access (C++0x)
3304 // ::= DT <expression> E # decltype of an expression (C++0x)
3305 template <typename Derived, typename Alloc>
3306 Node *AbstractManglingParser<Derived, Alloc>::parseDecltype() {
3307 if (!consumeIf('D'))
3309 if (!consumeIf('t') && !consumeIf('T'))
3311 Node *E = getDerived().parseExpr();
3314 if (!consumeIf('E'))
3316 return make<EnclosingExpr>("decltype(", E, ")");
3319 // <array-type> ::= A <positive dimension number> _ <element type>
3320 // ::= A [<dimension expression>] _ <element type>
3321 template <typename Derived, typename Alloc>
3322 Node *AbstractManglingParser<Derived, Alloc>::parseArrayType() {
3323 if (!consumeIf('A'))
3326 NodeOrString Dimension;
3328 if (std::isdigit(look())) {
3329 Dimension = parseNumber();
3330 if (!consumeIf('_'))
3332 } else if (!consumeIf('_')) {
3333 Node *DimExpr = getDerived().parseExpr();
3334 if (DimExpr == nullptr)
3336 if (!consumeIf('_'))
3338 Dimension = DimExpr;
3341 Node *Ty = getDerived().parseType();
3344 return make<ArrayType>(Ty, Dimension);
3347 // <pointer-to-member-type> ::= M <class type> <member type>
3348 template <typename Derived, typename Alloc>
3349 Node *AbstractManglingParser<Derived, Alloc>::parsePointerToMemberType() {
3350 if (!consumeIf('M'))
3352 Node *ClassType = getDerived().parseType();
3353 if (ClassType == nullptr)
3355 Node *MemberType = getDerived().parseType();
3356 if (MemberType == nullptr)
3358 return make<PointerToMemberType>(ClassType, MemberType);
3361 // <class-enum-type> ::= <name> # non-dependent type name, dependent type name, or dependent typename-specifier
3362 // ::= Ts <name> # dependent elaborated type specifier using 'struct' or 'class'
3363 // ::= Tu <name> # dependent elaborated type specifier using 'union'
3364 // ::= Te <name> # dependent elaborated type specifier using 'enum'
3365 template <typename Derived, typename Alloc>
3366 Node *AbstractManglingParser<Derived, Alloc>::parseClassEnumType() {
3367 StringView ElabSpef;
3368 if (consumeIf("Ts"))
3369 ElabSpef = "struct";
3370 else if (consumeIf("Tu"))
3372 else if (consumeIf("Te"))
3375 Node *Name = getDerived().parseName();
3376 if (Name == nullptr)
3379 if (!ElabSpef.empty())
3380 return make<ElaboratedTypeSpefType>(ElabSpef, Name);
3385 // <qualified-type> ::= <qualifiers> <type>
3386 // <qualifiers> ::= <extended-qualifier>* <CV-qualifiers>
3387 // <extended-qualifier> ::= U <source-name> [<template-args>] # vendor extended type qualifier
3388 template <typename Derived, typename Alloc>
3389 Node *AbstractManglingParser<Derived, Alloc>::parseQualifiedType() {
3390 if (consumeIf('U')) {
3391 StringView Qual = parseBareSourceName();
3395 // FIXME parse the optional <template-args> here!
3397 // extension ::= U <objc-name> <objc-type> # objc-type<identifier>
3398 if (Qual.startsWith("objcproto")) {
3399 StringView ProtoSourceName = Qual.dropFront(std::strlen("objcproto"));
3402 SwapAndRestore<const char *> SaveFirst(First, ProtoSourceName.begin()),
3403 SaveLast(Last, ProtoSourceName.end());
3404 Proto = parseBareSourceName();
3408 Node *Child = getDerived().parseQualifiedType();
3409 if (Child == nullptr)
3411 return make<ObjCProtoName>(Child, Proto);
3414 Node *Child = getDerived().parseQualifiedType();
3415 if (Child == nullptr)
3417 return make<VendorExtQualType>(Child, Qual);
3420 Qualifiers Quals = parseCVQualifiers();
3421 Node *Ty = getDerived().parseType();
3424 if (Quals != QualNone)
3425 Ty = make<QualType>(Ty, Quals);
3429 // <type> ::= <builtin-type>
3430 // ::= <qualified-type>
3431 // ::= <function-type>
3432 // ::= <class-enum-type>
3434 // ::= <pointer-to-member-type>
3435 // ::= <template-param>
3436 // ::= <template-template-param> <template-args>
3438 // ::= P <type> # pointer
3439 // ::= R <type> # l-value reference
3440 // ::= O <type> # r-value reference (C++11)
3441 // ::= C <type> # complex pair (C99)
3442 // ::= G <type> # imaginary (C99)
3443 // ::= <substitution> # See Compression below
3444 // extension ::= U <objc-name> <objc-type> # objc-type<identifier>
3445 // extension ::= <vector-type> # <vector-type> starts with Dv
3447 // <objc-name> ::= <k0 number> objcproto <k1 number> <identifier> # k0 = 9 + <number of digits in k1> + k1
3448 // <objc-type> ::= <source-name> # PU<11+>objcproto 11objc_object<source-name> 11objc_object -> id<source-name>
3449 template <typename Derived, typename Alloc>
3450 Node *AbstractManglingParser<Derived, Alloc>::parseType() {
3451 Node *Result = nullptr;
3454 // ::= <qualified-type>
3458 unsigned AfterQuals = 0;
3459 if (look(AfterQuals) == 'r') ++AfterQuals;
3460 if (look(AfterQuals) == 'V') ++AfterQuals;
3461 if (look(AfterQuals) == 'K') ++AfterQuals;
3463 if (look(AfterQuals) == 'F' ||
3464 (look(AfterQuals) == 'D' &&
3465 (look(AfterQuals + 1) == 'o' || look(AfterQuals + 1) == 'O' ||
3466 look(AfterQuals + 1) == 'w' || look(AfterQuals + 1) == 'x'))) {
3467 Result = getDerived().parseFunctionType();
3473 Result = getDerived().parseQualifiedType();
3476 // <builtin-type> ::= v # void
3479 return make<NameType>("void");
3483 return make<NameType>("wchar_t");
3487 return make<NameType>("bool");
3491 return make<NameType>("char");
3492 // ::= a # signed char
3495 return make<NameType>("signed char");
3496 // ::= h # unsigned char
3499 return make<NameType>("unsigned char");
3503 return make<NameType>("short");
3504 // ::= t # unsigned short
3507 return make<NameType>("unsigned short");
3511 return make<NameType>("int");
3512 // ::= j # unsigned int
3515 return make<NameType>("unsigned int");
3519 return make<NameType>("long");
3520 // ::= m # unsigned long
3523 return make<NameType>("unsigned long");
3524 // ::= x # long long, __int64
3527 return make<NameType>("long long");
3528 // ::= y # unsigned long long, __int64
3531 return make<NameType>("unsigned long long");
3535 return make<NameType>("__int128");
3536 // ::= o # unsigned __int128
3539 return make<NameType>("unsigned __int128");
3543 return make<NameType>("float");
3547 return make<NameType>("double");
3548 // ::= e # long double, __float80
3551 return make<NameType>("long double");
3552 // ::= g # __float128
3555 return make<NameType>("__float128");
3559 return make<NameType>("...");
3561 // <builtin-type> ::= u <source-name> # vendor extended type
3564 StringView Res = parseBareSourceName();
3567 return make<NameType>(Res);
3571 // ::= Dd # IEEE 754r decimal floating point (64 bits)
3574 return make<NameType>("decimal64");
3575 // ::= De # IEEE 754r decimal floating point (128 bits)
3578 return make<NameType>("decimal128");
3579 // ::= Df # IEEE 754r decimal floating point (32 bits)
3582 return make<NameType>("decimal32");
3583 // ::= Dh # IEEE 754r half-precision floating point (16 bits)
3586 return make<NameType>("decimal16");
3587 // ::= Di # char32_t
3590 return make<NameType>("char32_t");
3591 // ::= Ds # char16_t
3594 return make<NameType>("char16_t");
3595 // ::= Da # auto (in dependent new-expressions)
3598 return make<NameType>("auto");
3599 // ::= Dc # decltype(auto)
3602 return make<NameType>("decltype(auto)");
3603 // ::= Dn # std::nullptr_t (i.e., decltype(nullptr))
3606 return make<NameType>("std::nullptr_t");
3611 Result = getDerived().parseDecltype();
3614 // extension ::= <vector-type> # <vector-type> starts with Dv
3616 Result = getDerived().parseVectorType();
3619 // ::= Dp <type> # pack expansion (C++0x)
3622 Node *Child = getDerived().parseType();
3625 Result = make<ParameterPackExpansion>(Child);
3628 // Exception specifier on a function type.
3632 // Transaction safe function type.
3634 Result = getDerived().parseFunctionType();
3638 // ::= <function-type>
3640 Result = getDerived().parseFunctionType();
3645 Result = getDerived().parseArrayType();
3648 // ::= <pointer-to-member-type>
3650 Result = getDerived().parsePointerToMemberType();
3653 // ::= <template-param>
3655 // This could be an elaborate type specifier on a <class-enum-type>.
3656 if (look(1) == 's' || look(1) == 'u' || look(1) == 'e') {
3657 Result = getDerived().parseClassEnumType();
3661 Result = getDerived().parseTemplateParam();
3662 if (Result == nullptr)
3665 // Result could be either of:
3666 // <type> ::= <template-param>
3667 // <type> ::= <template-template-param> <template-args>
3669 // <template-template-param> ::= <template-param>
3670 // ::= <substitution>
3672 // If this is followed by some <template-args>, and we're permitted to
3673 // parse them, take the second production.
3675 if (TryToParseTemplateArgs && look() == 'I') {
3676 Node *TA = getDerived().parseTemplateArgs();
3679 Result = make<NameWithTemplateArgs>(Result, TA);
3683 // ::= P <type> # pointer
3686 Node *Ptr = getDerived().parseType();
3689 Result = make<PointerType>(Ptr);
3692 // ::= R <type> # l-value reference
3695 Node *Ref = getDerived().parseType();
3698 Result = make<ReferenceType>(Ref, ReferenceKind::LValue);
3701 // ::= O <type> # r-value reference (C++11)
3704 Node *Ref = getDerived().parseType();
3707 Result = make<ReferenceType>(Ref, ReferenceKind::RValue);
3710 // ::= C <type> # complex pair (C99)
3713 Node *P = getDerived().parseType();
3716 Result = make<PostfixQualifiedType>(P, " complex");
3719 // ::= G <type> # imaginary (C99)
3722 Node *P = getDerived().parseType();
3725 Result = make<PostfixQualifiedType>(P, " imaginary");
3728 // ::= <substitution> # See Compression below
3730 if (look(1) && look(1) != 't') {
3731 Node *Sub = getDerived().parseSubstitution();
3735 // Sub could be either of:
3736 // <type> ::= <substitution>
3737 // <type> ::= <template-template-param> <template-args>
3739 // <template-template-param> ::= <template-param>
3740 // ::= <substitution>
3742 // If this is followed by some <template-args>, and we're permitted to
3743 // parse them, take the second production.
3745 if (TryToParseTemplateArgs && look() == 'I') {
3746 Node *TA = getDerived().parseTemplateArgs();
3749 Result = make<NameWithTemplateArgs>(Sub, TA);
3753 // If all we parsed was a substitution, don't re-insert into the
3754 // substitution table.
3759 // ::= <class-enum-type>
3761 Result = getDerived().parseClassEnumType();
3766 // If we parsed a type, insert it into the substitution table. Note that all
3767 // <builtin-type>s and <substitution>s have already bailed out, because they
3768 // don't get substitutions.
3769 if (Result != nullptr)
3770 Subs.push_back(Result);
3774 template <typename Derived, typename Alloc>
3775 Node *AbstractManglingParser<Derived, Alloc>::parsePrefixExpr(StringView Kind) {
3776 Node *E = getDerived().parseExpr();
3779 return make<PrefixExpr>(Kind, E);
3782 template <typename Derived, typename Alloc>
3783 Node *AbstractManglingParser<Derived, Alloc>::parseBinaryExpr(StringView Kind) {
3784 Node *LHS = getDerived().parseExpr();
3787 Node *RHS = getDerived().parseExpr();
3790 return make<BinaryExpr>(LHS, Kind, RHS);
3793 template <typename Derived, typename Alloc>
3795 AbstractManglingParser<Derived, Alloc>::parseIntegerLiteral(StringView Lit) {
3796 StringView Tmp = parseNumber(true);
3797 if (!Tmp.empty() && consumeIf('E'))
3798 return make<IntegerLiteral>(Lit, Tmp);
3802 // <CV-Qualifiers> ::= [r] [V] [K]
3803 template <typename Alloc, typename Derived>
3804 Qualifiers AbstractManglingParser<Alloc, Derived>::parseCVQualifiers() {
3805 Qualifiers CVR = QualNone;
3807 CVR |= QualRestrict;
3809 CVR |= QualVolatile;
3815 // <function-param> ::= fp <top-level CV-Qualifiers> _ # L == 0, first parameter
3816 // ::= fp <top-level CV-Qualifiers> <parameter-2 non-negative number> _ # L == 0, second and later parameters
3817 // ::= fL <L-1 non-negative number> p <top-level CV-Qualifiers> _ # L > 0, first parameter
3818 // ::= fL <L-1 non-negative number> p <top-level CV-Qualifiers> <parameter-2 non-negative number> _ # L > 0, second and later parameters
3819 template <typename Derived, typename Alloc>
3820 Node *AbstractManglingParser<Derived, Alloc>::parseFunctionParam() {
3821 if (consumeIf("fp")) {
3822 parseCVQualifiers();
3823 StringView Num = parseNumber();
3824 if (!consumeIf('_'))
3826 return make<FunctionParam>(Num);
3828 if (consumeIf("fL")) {
3829 if (parseNumber().empty())
3831 if (!consumeIf('p'))
3833 parseCVQualifiers();
3834 StringView Num = parseNumber();
3835 if (!consumeIf('_'))
3837 return make<FunctionParam>(Num);
3842 // [gs] nw <expression>* _ <type> E # new (expr-list) type
3843 // [gs] nw <expression>* _ <type> <initializer> # new (expr-list) type (init)
3844 // [gs] na <expression>* _ <type> E # new[] (expr-list) type
3845 // [gs] na <expression>* _ <type> <initializer> # new[] (expr-list) type (init)
3846 // <initializer> ::= pi <expression>* E # parenthesized initialization
3847 template <typename Derived, typename Alloc>
3848 Node *AbstractManglingParser<Derived, Alloc>::parseNewExpr() {
3849 bool Global = consumeIf("gs");
3850 bool IsArray = look(1) == 'a';
3851 if (!consumeIf("nw") && !consumeIf("na"))
3853 size_t Exprs = Names.size();
3854 while (!consumeIf('_')) {
3855 Node *Ex = getDerived().parseExpr();
3858 Names.push_back(Ex);
3860 NodeArray ExprList = popTrailingNodeArray(Exprs);
3861 Node *Ty = getDerived().parseType();
3864 if (consumeIf("pi")) {
3865 size_t InitsBegin = Names.size();
3866 while (!consumeIf('E')) {
3867 Node *Init = getDerived().parseExpr();
3868 if (Init == nullptr)
3870 Names.push_back(Init);
3872 NodeArray Inits = popTrailingNodeArray(InitsBegin);
3873 return make<NewExpr>(ExprList, Ty, Inits, Global, IsArray);
3874 } else if (!consumeIf('E'))
3876 return make<NewExpr>(ExprList, Ty, NodeArray(), Global, IsArray);
3879 // cv <type> <expression> # conversion with one argument
3880 // cv <type> _ <expression>* E # conversion with a different number of arguments
3881 template <typename Derived, typename Alloc>
3882 Node *AbstractManglingParser<Derived, Alloc>::parseConversionExpr() {
3883 if (!consumeIf("cv"))
3887 SwapAndRestore<bool> SaveTemp(TryToParseTemplateArgs, false);
3888 Ty = getDerived().parseType();
3894 if (consumeIf('_')) {
3895 size_t ExprsBegin = Names.size();
3896 while (!consumeIf('E')) {
3897 Node *E = getDerived().parseExpr();
3902 NodeArray Exprs = popTrailingNodeArray(ExprsBegin);
3903 return make<ConversionExpr>(Ty, Exprs);
3906 Node *E[1] = {getDerived().parseExpr()};
3907 if (E[0] == nullptr)
3909 return make<ConversionExpr>(Ty, makeNodeArray(E, E + 1));
3912 // <expr-primary> ::= L <type> <value number> E # integer literal
3913 // ::= L <type> <value float> E # floating literal
3914 // ::= L <string type> E # string literal
3915 // ::= L <nullptr type> E # nullptr literal (i.e., "LDnE")
3916 // FIXME: ::= L <type> <real-part float> _ <imag-part float> E # complex floating point literal (C 2000)
3917 // ::= L <mangled-name> E # external name
3918 template <typename Derived, typename Alloc>
3919 Node *AbstractManglingParser<Derived, Alloc>::parseExprPrimary() {
3920 if (!consumeIf('L'))
3925 return getDerived().parseIntegerLiteral("wchar_t");
3927 if (consumeIf("b0E"))
3928 return make<BoolExpr>(0);
3929 if (consumeIf("b1E"))
3930 return make<BoolExpr>(1);
3934 return getDerived().parseIntegerLiteral("char");
3937 return getDerived().parseIntegerLiteral("signed char");
3940 return getDerived().parseIntegerLiteral("unsigned char");
3943 return getDerived().parseIntegerLiteral("short");
3946 return getDerived().parseIntegerLiteral("unsigned short");
3949 return getDerived().parseIntegerLiteral("");
3952 return getDerived().parseIntegerLiteral("u");
3955 return getDerived().parseIntegerLiteral("l");
3958 return getDerived().parseIntegerLiteral("ul");
3961 return getDerived().parseIntegerLiteral("ll");
3964 return getDerived().parseIntegerLiteral("ull");
3967 return getDerived().parseIntegerLiteral("__int128");
3970 return getDerived().parseIntegerLiteral("unsigned __int128");
3973 return getDerived().template parseFloatingLiteral<float>();
3976 return getDerived().template parseFloatingLiteral<double>();
3979 return getDerived().template parseFloatingLiteral<long double>();
3981 if (consumeIf("_Z")) {
3982 Node *R = getDerived().parseEncoding();
3983 if (R != nullptr && consumeIf('E'))
3988 // Invalid mangled name per
3989 // http://sourcerytools.com/pipermail/cxx-abi-dev/2011-August/002422.html
3992 // might be named type
3993 Node *T = getDerived().parseType();
3996 StringView N = parseNumber();
3998 if (!consumeIf('E'))
4000 return make<IntegerCastExpr>(T, N);
4009 // <braced-expression> ::= <expression>
4010 // ::= di <field source-name> <braced-expression> # .name = expr
4011 // ::= dx <index expression> <braced-expression> # [expr] = expr
4012 // ::= dX <range begin expression> <range end expression> <braced-expression>
4013 template <typename Derived, typename Alloc>
4014 Node *AbstractManglingParser<Derived, Alloc>::parseBracedExpr() {
4015 if (look() == 'd') {
4019 Node *Field = getDerived().parseSourceName(/*NameState=*/nullptr);
4020 if (Field == nullptr)
4022 Node *Init = getDerived().parseBracedExpr();
4023 if (Init == nullptr)
4025 return make<BracedExpr>(Field, Init, /*isArray=*/false);
4029 Node *Index = getDerived().parseExpr();
4030 if (Index == nullptr)
4032 Node *Init = getDerived().parseBracedExpr();
4033 if (Init == nullptr)
4035 return make<BracedExpr>(Index, Init, /*isArray=*/true);
4039 Node *RangeBegin = getDerived().parseExpr();
4040 if (RangeBegin == nullptr)
4042 Node *RangeEnd = getDerived().parseExpr();
4043 if (RangeEnd == nullptr)
4045 Node *Init = getDerived().parseBracedExpr();
4046 if (Init == nullptr)
4048 return make<BracedRangeExpr>(RangeBegin, RangeEnd, Init);
4052 return getDerived().parseExpr();
4055 // (not yet in the spec)
4056 // <fold-expr> ::= fL <binary-operator-name> <expression> <expression>
4057 // ::= fR <binary-operator-name> <expression> <expression>
4058 // ::= fl <binary-operator-name> <expression>
4059 // ::= fr <binary-operator-name> <expression>
4060 template <typename Derived, typename Alloc>
4061 Node *AbstractManglingParser<Derived, Alloc>::parseFoldExpr() {
4062 if (!consumeIf('f'))
4065 char FoldKind = look();
4066 bool IsLeftFold, HasInitializer;
4067 HasInitializer = FoldKind == 'L' || FoldKind == 'R';
4068 if (FoldKind == 'l' || FoldKind == 'L')
4070 else if (FoldKind == 'r' || FoldKind == 'R')
4076 // FIXME: This map is duplicated in parseOperatorName and parseExpr.
4077 StringView OperatorName;
4078 if (consumeIf("aa")) OperatorName = "&&";
4079 else if (consumeIf("an")) OperatorName = "&";
4080 else if (consumeIf("aN")) OperatorName = "&=";
4081 else if (consumeIf("aS")) OperatorName = "=";
4082 else if (consumeIf("cm")) OperatorName = ",";
4083 else if (consumeIf("ds")) OperatorName = ".*";
4084 else if (consumeIf("dv")) OperatorName = "/";
4085 else if (consumeIf("dV")) OperatorName = "/=";
4086 else if (consumeIf("eo")) OperatorName = "^";
4087 else if (consumeIf("eO")) OperatorName = "^=";
4088 else if (consumeIf("eq")) OperatorName = "==";
4089 else if (consumeIf("ge")) OperatorName = ">=";
4090 else if (consumeIf("gt")) OperatorName = ">";
4091 else if (consumeIf("le")) OperatorName = "<=";
4092 else if (consumeIf("ls")) OperatorName = "<<";
4093 else if (consumeIf("lS")) OperatorName = "<<=";
4094 else if (consumeIf("lt")) OperatorName = "<";
4095 else if (consumeIf("mi")) OperatorName = "-";
4096 else if (consumeIf("mI")) OperatorName = "-=";
4097 else if (consumeIf("ml")) OperatorName = "*";
4098 else if (consumeIf("mL")) OperatorName = "*=";
4099 else if (consumeIf("ne")) OperatorName = "!=";
4100 else if (consumeIf("oo")) OperatorName = "||";
4101 else if (consumeIf("or")) OperatorName = "|";
4102 else if (consumeIf("oR")) OperatorName = "|=";
4103 else if (consumeIf("pl")) OperatorName = "+";
4104 else if (consumeIf("pL")) OperatorName = "+=";
4105 else if (consumeIf("rm")) OperatorName = "%";
4106 else if (consumeIf("rM")) OperatorName = "%=";
4107 else if (consumeIf("rs")) OperatorName = ">>";
4108 else if (consumeIf("rS")) OperatorName = ">>=";
4109 else return nullptr;
4111 Node *Pack = getDerived().parseExpr(), *Init = nullptr;
4112 if (Pack == nullptr)
4114 if (HasInitializer) {
4115 Init = getDerived().parseExpr();
4116 if (Init == nullptr)
4120 if (IsLeftFold && Init)
4121 std::swap(Pack, Init);
4123 return make<FoldExpr>(IsLeftFold, OperatorName, Pack, Init);
4126 // <expression> ::= <unary operator-name> <expression>
4127 // ::= <binary operator-name> <expression> <expression>
4128 // ::= <ternary operator-name> <expression> <expression> <expression>
4129 // ::= cl <expression>+ E # call
4130 // ::= cv <type> <expression> # conversion with one argument
4131 // ::= cv <type> _ <expression>* E # conversion with a different number of arguments
4132 // ::= [gs] nw <expression>* _ <type> E # new (expr-list) type
4133 // ::= [gs] nw <expression>* _ <type> <initializer> # new (expr-list) type (init)
4134 // ::= [gs] na <expression>* _ <type> E # new[] (expr-list) type
4135 // ::= [gs] na <expression>* _ <type> <initializer> # new[] (expr-list) type (init)
4136 // ::= [gs] dl <expression> # delete expression
4137 // ::= [gs] da <expression> # delete[] expression
4138 // ::= pp_ <expression> # prefix ++
4139 // ::= mm_ <expression> # prefix --
4140 // ::= ti <type> # typeid (type)
4141 // ::= te <expression> # typeid (expression)
4142 // ::= dc <type> <expression> # dynamic_cast<type> (expression)
4143 // ::= sc <type> <expression> # static_cast<type> (expression)
4144 // ::= cc <type> <expression> # const_cast<type> (expression)
4145 // ::= rc <type> <expression> # reinterpret_cast<type> (expression)
4146 // ::= st <type> # sizeof (a type)
4147 // ::= sz <expression> # sizeof (an expression)
4148 // ::= at <type> # alignof (a type)
4149 // ::= az <expression> # alignof (an expression)
4150 // ::= nx <expression> # noexcept (expression)
4151 // ::= <template-param>
4152 // ::= <function-param>
4153 // ::= dt <expression> <unresolved-name> # expr.name
4154 // ::= pt <expression> <unresolved-name> # expr->name
4155 // ::= ds <expression> <expression> # expr.*expr
4156 // ::= sZ <template-param> # size of a parameter pack
4157 // ::= sZ <function-param> # size of a function parameter pack
4158 // ::= sP <template-arg>* E # sizeof...(T), size of a captured template parameter pack from an alias template
4159 // ::= sp <expression> # pack expansion
4160 // ::= tw <expression> # throw expression
4161 // ::= tr # throw with no operand (rethrow)
4162 // ::= <unresolved-name> # f(p), N::f(p), ::f(p),
4163 // # freestanding dependent name (e.g., T::x),
4164 // # objectless nonstatic member reference
4165 // ::= fL <binary-operator-name> <expression> <expression>
4166 // ::= fR <binary-operator-name> <expression> <expression>
4167 // ::= fl <binary-operator-name> <expression>
4168 // ::= fr <binary-operator-name> <expression>
4169 // ::= <expr-primary>
4170 template <typename Derived, typename Alloc>
4171 Node *AbstractManglingParser<Derived, Alloc>::parseExpr() {
4172 bool Global = consumeIf("gs");
4178 return getDerived().parseExprPrimary();
4180 return getDerived().parseTemplateParam();
4182 // Disambiguate a fold expression from a <function-param>.
4183 if (look(1) == 'p' || (look(1) == 'L' && std::isdigit(look(2))))
4184 return getDerived().parseFunctionParam();
4185 return getDerived().parseFoldExpr();
4191 return getDerived().parseBinaryExpr("&&");
4194 return getDerived().parsePrefixExpr("&");
4197 return getDerived().parseBinaryExpr("&");
4200 return getDerived().parseBinaryExpr("&=");
4203 return getDerived().parseBinaryExpr("=");
4206 Node *Ty = getDerived().parseType();
4209 return make<EnclosingExpr>("alignof (", Ty, ")");
4213 Node *Ty = getDerived().parseExpr();
4216 return make<EnclosingExpr>("alignof (", Ty, ")");
4222 // cc <type> <expression> # const_cast<type>(expression)
4225 Node *Ty = getDerived().parseType();
4228 Node *Ex = getDerived().parseExpr();
4231 return make<CastExpr>("const_cast", Ty, Ex);
4233 // cl <expression>+ E # call
4236 Node *Callee = getDerived().parseExpr();
4237 if (Callee == nullptr)
4239 size_t ExprsBegin = Names.size();
4240 while (!consumeIf('E')) {
4241 Node *E = getDerived().parseExpr();
4246 return make<CallExpr>(Callee, popTrailingNodeArray(ExprsBegin));
4250 return getDerived().parseBinaryExpr(",");
4253 return getDerived().parsePrefixExpr("~");
4255 return getDerived().parseConversionExpr();
4262 Node *Ex = getDerived().parseExpr();
4265 return make<DeleteExpr>(Ex, Global, /*is_array=*/true);
4269 Node *T = getDerived().parseType();
4272 Node *Ex = getDerived().parseExpr();
4275 return make<CastExpr>("dynamic_cast", T, Ex);
4279 return getDerived().parsePrefixExpr("*");
4282 Node *E = getDerived().parseExpr();
4285 return make<DeleteExpr>(E, Global, /*is_array=*/false);
4288 return getDerived().parseUnresolvedName();
4291 Node *LHS = getDerived().parseExpr();
4294 Node *RHS = getDerived().parseExpr();
4297 return make<MemberExpr>(LHS, ".*", RHS);
4301 Node *LHS = getDerived().parseExpr();
4304 Node *RHS = getDerived().parseExpr();
4307 return make<MemberExpr>(LHS, ".", RHS);
4311 return getDerived().parseBinaryExpr("/");
4314 return getDerived().parseBinaryExpr("/=");
4321 return getDerived().parseBinaryExpr("^");
4324 return getDerived().parseBinaryExpr("^=");
4327 return getDerived().parseBinaryExpr("==");
4334 return getDerived().parseBinaryExpr(">=");
4337 return getDerived().parseBinaryExpr(">");
4344 Node *Base = getDerived().parseExpr();
4345 if (Base == nullptr)
4347 Node *Index = getDerived().parseExpr();
4348 if (Index == nullptr)
4350 return make<ArraySubscriptExpr>(Base, Index);
4354 size_t InitsBegin = Names.size();
4355 while (!consumeIf('E')) {
4356 Node *E = getDerived().parseBracedExpr();
4361 return make<InitListExpr>(nullptr, popTrailingNodeArray(InitsBegin));
4369 return getDerived().parseBinaryExpr("<=");
4372 return getDerived().parseBinaryExpr("<<");
4375 return getDerived().parseBinaryExpr("<<=");
4378 return getDerived().parseBinaryExpr("<");
4385 return getDerived().parseBinaryExpr("-");
4388 return getDerived().parseBinaryExpr("-=");
4391 return getDerived().parseBinaryExpr("*");
4394 return getDerived().parseBinaryExpr("*=");
4398 return getDerived().parsePrefixExpr("--");
4399 Node *Ex = getDerived().parseExpr();
4402 return make<PostfixExpr>(Ex, "--");
4409 return getDerived().parseNewExpr();
4412 return getDerived().parseBinaryExpr("!=");
4415 return getDerived().parsePrefixExpr("-");
4418 return getDerived().parsePrefixExpr("!");
4421 Node *Ex = getDerived().parseExpr();
4424 return make<EnclosingExpr>("noexcept (", Ex, ")");
4430 return getDerived().parseUnresolvedName();
4433 return getDerived().parseBinaryExpr("||");
4436 return getDerived().parseBinaryExpr("|");
4439 return getDerived().parseBinaryExpr("|=");
4446 return getDerived().parseBinaryExpr("->*");
4449 return getDerived().parseBinaryExpr("+");
4452 return getDerived().parseBinaryExpr("+=");
4456 return getDerived().parsePrefixExpr("++");
4457 Node *Ex = getDerived().parseExpr();
4460 return make<PostfixExpr>(Ex, "++");
4464 return getDerived().parsePrefixExpr("+");
4467 Node *L = getDerived().parseExpr();
4470 Node *R = getDerived().parseExpr();
4473 return make<MemberExpr>(L, "->", R);
4478 if (First[1] == 'u') {
4480 Node *Cond = getDerived().parseExpr();
4481 if (Cond == nullptr)
4483 Node *LHS = getDerived().parseExpr();
4486 Node *RHS = getDerived().parseExpr();
4489 return make<ConditionalExpr>(Cond, LHS, RHS);
4496 Node *T = getDerived().parseType();
4499 Node *Ex = getDerived().parseExpr();
4502 return make<CastExpr>("reinterpret_cast", T, Ex);
4506 return getDerived().parseBinaryExpr("%");
4509 return getDerived().parseBinaryExpr("%=");
4512 return getDerived().parseBinaryExpr(">>");
4515 return getDerived().parseBinaryExpr(">>=");
4522 Node *T = getDerived().parseType();
4525 Node *Ex = getDerived().parseExpr();
4528 return make<CastExpr>("static_cast", T, Ex);
4532 Node *Child = getDerived().parseExpr();
4533 if (Child == nullptr)
4535 return make<ParameterPackExpansion>(Child);
4538 return getDerived().parseUnresolvedName();
4541 Node *Ty = getDerived().parseType();
4544 return make<EnclosingExpr>("sizeof (", Ty, ")");
4548 Node *Ex = getDerived().parseExpr();
4551 return make<EnclosingExpr>("sizeof (", Ex, ")");
4555 if (look() == 'T') {
4556 Node *R = getDerived().parseTemplateParam();
4559 return make<SizeofParamPackExpr>(R);
4560 } else if (look() == 'f') {
4561 Node *FP = getDerived().parseFunctionParam();
4564 return make<EnclosingExpr>("sizeof... (", FP, ")");
4569 size_t ArgsBegin = Names.size();
4570 while (!consumeIf('E')) {
4571 Node *Arg = getDerived().parseTemplateArg();
4574 Names.push_back(Arg);
4576 auto *Pack = make<NodeArrayNode>(popTrailingNodeArray(ArgsBegin));
4579 return make<EnclosingExpr>("sizeof... (", Pack, ")");
4587 Node *Ex = getDerived().parseExpr();
4590 return make<EnclosingExpr>("typeid (", Ex, ")");
4594 Node *Ty = getDerived().parseType();
4597 return make<EnclosingExpr>("typeid (", Ty, ")");
4601 Node *Ty = getDerived().parseType();
4604 size_t InitsBegin = Names.size();
4605 while (!consumeIf('E')) {
4606 Node *E = getDerived().parseBracedExpr();
4611 return make<InitListExpr>(Ty, popTrailingNodeArray(InitsBegin));
4615 return make<NameType>("throw");
4618 Node *Ex = getDerived().parseExpr();
4621 return make<ThrowExpr>(Ex);
4634 return getDerived().parseUnresolvedName();
4639 // <call-offset> ::= h <nv-offset> _
4640 // ::= v <v-offset> _
4642 // <nv-offset> ::= <offset number>
4643 // # non-virtual base override
4645 // <v-offset> ::= <offset number> _ <virtual offset number>
4646 // # virtual base override, with vcall offset
4647 template <typename Alloc, typename Derived>
4648 bool AbstractManglingParser<Alloc, Derived>::parseCallOffset() {
4649 // Just scan through the call offset, we never add this information into the
4652 return parseNumber(true).empty() || !consumeIf('_');
4654 return parseNumber(true).empty() || !consumeIf('_') ||
4655 parseNumber(true).empty() || !consumeIf('_');
4659 // <special-name> ::= TV <type> # virtual table
4660 // ::= TT <type> # VTT structure (construction vtable index)
4661 // ::= TI <type> # typeinfo structure
4662 // ::= TS <type> # typeinfo name (null-terminated byte string)
4663 // ::= Tc <call-offset> <call-offset> <base encoding>
4664 // # base is the nominal target function of thunk
4665 // # first call-offset is 'this' adjustment
4666 // # second call-offset is result adjustment
4667 // ::= T <call-offset> <base encoding>
4668 // # base is the nominal target function of thunk
4669 // ::= GV <object name> # Guard variable for one-time initialization
4671 // ::= TW <object name> # Thread-local wrapper
4672 // ::= TH <object name> # Thread-local initialization
4673 // ::= GR <object name> _ # First temporary
4674 // ::= GR <object name> <seq-id> _ # Subsequent temporaries
4675 // extension ::= TC <first type> <number> _ <second type> # construction vtable for second-in-first
4676 // extension ::= GR <object name> # reference temporary for object
4677 template <typename Derived, typename Alloc>
4678 Node *AbstractManglingParser<Derived, Alloc>::parseSpecialName() {
4682 // TV <type> # virtual table
4685 Node *Ty = getDerived().parseType();
4688 return make<SpecialName>("vtable for ", Ty);
4690 // TT <type> # VTT structure (construction vtable index)
4693 Node *Ty = getDerived().parseType();
4696 return make<SpecialName>("VTT for ", Ty);
4698 // TI <type> # typeinfo structure
4701 Node *Ty = getDerived().parseType();
4704 return make<SpecialName>("typeinfo for ", Ty);
4706 // TS <type> # typeinfo name (null-terminated byte string)
4709 Node *Ty = getDerived().parseType();
4712 return make<SpecialName>("typeinfo name for ", Ty);
4714 // Tc <call-offset> <call-offset> <base encoding>
4717 if (parseCallOffset() || parseCallOffset())
4719 Node *Encoding = getDerived().parseEncoding();
4720 if (Encoding == nullptr)
4722 return make<SpecialName>("covariant return thunk to ", Encoding);
4724 // extension ::= TC <first type> <number> _ <second type>
4725 // # construction vtable for second-in-first
4728 Node *FirstType = getDerived().parseType();
4729 if (FirstType == nullptr)
4731 if (parseNumber(true).empty() || !consumeIf('_'))
4733 Node *SecondType = getDerived().parseType();
4734 if (SecondType == nullptr)
4736 return make<CtorVtableSpecialName>(SecondType, FirstType);
4738 // TW <object name> # Thread-local wrapper
4741 Node *Name = getDerived().parseName();
4742 if (Name == nullptr)
4744 return make<SpecialName>("thread-local wrapper routine for ", Name);
4746 // TH <object name> # Thread-local initialization
4749 Node *Name = getDerived().parseName();
4750 if (Name == nullptr)
4752 return make<SpecialName>("thread-local initialization routine for ", Name);
4754 // T <call-offset> <base encoding>
4757 bool IsVirt = look() == 'v';
4758 if (parseCallOffset())
4760 Node *BaseEncoding = getDerived().parseEncoding();
4761 if (BaseEncoding == nullptr)
4764 return make<SpecialName>("virtual thunk to ", BaseEncoding);
4766 return make<SpecialName>("non-virtual thunk to ", BaseEncoding);
4771 // GV <object name> # Guard variable for one-time initialization
4774 Node *Name = getDerived().parseName();
4775 if (Name == nullptr)
4777 return make<SpecialName>("guard variable for ", Name);
4779 // GR <object name> # reference temporary for object
4780 // GR <object name> _ # First temporary
4781 // GR <object name> <seq-id> _ # Subsequent temporaries
4784 Node *Name = getDerived().parseName();
4785 if (Name == nullptr)
4788 bool ParsedSeqId = !parseSeqId(&Count);
4789 if (!consumeIf('_') && ParsedSeqId)
4791 return make<SpecialName>("reference temporary for ", Name);
4798 // <encoding> ::= <function name> <bare-function-type>
4800 // ::= <special-name>
4801 template <typename Derived, typename Alloc>
4802 Node *AbstractManglingParser<Derived, Alloc>::parseEncoding() {
4803 if (look() == 'G' || look() == 'T')
4804 return getDerived().parseSpecialName();
4806 auto IsEndOfEncoding = [&] {
4807 // The set of chars that can potentially follow an <encoding> (none of which
4808 // can start a <type>). Enumerating these allows us to avoid speculative
4810 return numLeft() == 0 || look() == 'E' || look() == '.' || look() == '_';
4813 NameState NameInfo(this);
4814 Node *Name = getDerived().parseName(&NameInfo);
4815 if (Name == nullptr)
4818 if (resolveForwardTemplateRefs(NameInfo))
4821 if (IsEndOfEncoding())
4824 Node *Attrs = nullptr;
4825 if (consumeIf("Ua9enable_ifI")) {
4826 size_t BeforeArgs = Names.size();
4827 while (!consumeIf('E')) {
4828 Node *Arg = getDerived().parseTemplateArg();
4831 Names.push_back(Arg);
4833 Attrs = make<EnableIfAttr>(popTrailingNodeArray(BeforeArgs));
4838 Node *ReturnType = nullptr;
4839 if (!NameInfo.CtorDtorConversion && NameInfo.EndsWithTemplateArgs) {
4840 ReturnType = getDerived().parseType();
4841 if (ReturnType == nullptr)
4846 return make<FunctionEncoding>(ReturnType, Name, NodeArray(),
4847 Attrs, NameInfo.CVQualifiers,
4848 NameInfo.ReferenceQualifier);
4850 size_t ParamsBegin = Names.size();
4852 Node *Ty = getDerived().parseType();
4855 Names.push_back(Ty);
4856 } while (!IsEndOfEncoding());
4858 return make<FunctionEncoding>(ReturnType, Name,
4859 popTrailingNodeArray(ParamsBegin),
4860 Attrs, NameInfo.CVQualifiers,
4861 NameInfo.ReferenceQualifier);
4864 template <class Float>
4868 struct FloatData<float>
4870 static const size_t mangled_size = 8;
4871 static const size_t max_demangled_size = 24;
4872 static constexpr const char* spec = "%af";
4876 struct FloatData<double>
4878 static const size_t mangled_size = 16;
4879 static const size_t max_demangled_size = 32;
4880 static constexpr const char* spec = "%a";
4884 struct FloatData<long double>
4886 #if defined(__mips__) && defined(__mips_n64) || defined(__aarch64__) || \
4888 static const size_t mangled_size = 32;
4889 #elif defined(__arm__) || defined(__mips__) || defined(__hexagon__)
4890 static const size_t mangled_size = 16;
4892 static const size_t mangled_size = 20; // May need to be adjusted to 16 or 24 on other platforms
4894 static const size_t max_demangled_size = 40;
4895 static constexpr const char *spec = "%LaL";
4898 template <typename Alloc, typename Derived>
4899 template <class Float>
4900 Node *AbstractManglingParser<Alloc, Derived>::parseFloatingLiteral() {
4901 const size_t N = FloatData<Float>::mangled_size;
4904 StringView Data(First, First + N);
4906 if (!std::isxdigit(C))
4909 if (!consumeIf('E'))
4911 return make<FloatLiteralImpl<Float>>(Data);
4914 // <seq-id> ::= <0-9A-Z>+
4915 template <typename Alloc, typename Derived>
4916 bool AbstractManglingParser<Alloc, Derived>::parseSeqId(size_t *Out) {
4917 if (!(look() >= '0' && look() <= '9') &&
4918 !(look() >= 'A' && look() <= 'Z'))
4923 if (look() >= '0' && look() <= '9') {
4925 Id += static_cast<size_t>(look() - '0');
4926 } else if (look() >= 'A' && look() <= 'Z') {
4928 Id += static_cast<size_t>(look() - 'A') + 10;
4937 // <substitution> ::= S <seq-id> _
4939 // <substitution> ::= Sa # ::std::allocator
4940 // <substitution> ::= Sb # ::std::basic_string
4941 // <substitution> ::= Ss # ::std::basic_string < char,
4942 // ::std::char_traits<char>,
4943 // ::std::allocator<char> >
4944 // <substitution> ::= Si # ::std::basic_istream<char, std::char_traits<char> >
4945 // <substitution> ::= So # ::std::basic_ostream<char, std::char_traits<char> >
4946 // <substitution> ::= Sd # ::std::basic_iostream<char, std::char_traits<char> >
4947 template <typename Derived, typename Alloc>
4948 Node *AbstractManglingParser<Derived, Alloc>::parseSubstitution() {
4949 if (!consumeIf('S'))
4952 if (std::islower(look())) {
4957 SpecialSub = make<SpecialSubstitution>(SpecialSubKind::allocator);
4961 SpecialSub = make<SpecialSubstitution>(SpecialSubKind::basic_string);
4965 SpecialSub = make<SpecialSubstitution>(SpecialSubKind::string);
4969 SpecialSub = make<SpecialSubstitution>(SpecialSubKind::istream);
4973 SpecialSub = make<SpecialSubstitution>(SpecialSubKind::ostream);
4977 SpecialSub = make<SpecialSubstitution>(SpecialSubKind::iostream);
4984 // Itanium C++ ABI 5.1.2: If a name that would use a built-in <substitution>
4985 // has ABI tags, the tags are appended to the substitution; the result is a
4986 // substitutable component.
4987 Node *WithTags = getDerived().parseAbiTags(SpecialSub);
4988 if (WithTags != SpecialSub) {
4989 Subs.push_back(WithTags);
4990 SpecialSub = WithTags;
4996 if (consumeIf('_')) {
5004 if (parseSeqId(&Index))
5007 if (!consumeIf('_') || Index >= Subs.size())
5012 // <template-param> ::= T_ # first template parameter
5013 // ::= T <parameter-2 non-negative number> _
5014 template <typename Derived, typename Alloc>
5015 Node *AbstractManglingParser<Derived, Alloc>::parseTemplateParam() {
5016 if (!consumeIf('T'))
5020 if (!consumeIf('_')) {
5021 if (parsePositiveInteger(&Index))
5024 if (!consumeIf('_'))
5028 // Itanium ABI 5.1.8: In a generic lambda, uses of auto in the parameter list
5029 // are mangled as the corresponding artificial template type parameter.
5030 if (ParsingLambdaParams)
5031 return make<NameType>("auto");
5033 // If we're in a context where this <template-param> refers to a
5034 // <template-arg> further ahead in the mangled name (currently just conversion
5035 // operator types), then we should only look it up in the right context.
5036 if (PermitForwardTemplateReferences) {
5037 Node *ForwardRef = make<ForwardTemplateReference>(Index);
5040 assert(ForwardRef->getKind() == Node::KForwardTemplateReference);
5041 ForwardTemplateRefs.push_back(
5042 static_cast<ForwardTemplateReference *>(ForwardRef));
5046 if (Index >= TemplateParams.size())
5048 return TemplateParams[Index];
5051 // <template-arg> ::= <type> # type or template
5052 // ::= X <expression> E # expression
5053 // ::= <expr-primary> # simple expressions
5054 // ::= J <template-arg>* E # argument pack
5055 // ::= LZ <encoding> E # extension
5056 template <typename Derived, typename Alloc>
5057 Node *AbstractManglingParser<Derived, Alloc>::parseTemplateArg() {
5061 Node *Arg = getDerived().parseExpr();
5062 if (Arg == nullptr || !consumeIf('E'))
5068 size_t ArgsBegin = Names.size();
5069 while (!consumeIf('E')) {
5070 Node *Arg = getDerived().parseTemplateArg();
5073 Names.push_back(Arg);
5075 NodeArray Args = popTrailingNodeArray(ArgsBegin);
5076 return make<TemplateArgumentPack>(Args);
5079 // ::= LZ <encoding> E # extension
5080 if (look(1) == 'Z') {
5082 Node *Arg = getDerived().parseEncoding();
5083 if (Arg == nullptr || !consumeIf('E'))
5087 // ::= <expr-primary> # simple expressions
5088 return getDerived().parseExprPrimary();
5091 return getDerived().parseType();
5095 // <template-args> ::= I <template-arg>* E
5096 // extension, the abi says <template-arg>+
5097 template <typename Derived, typename Alloc>
5099 AbstractManglingParser<Derived, Alloc>::parseTemplateArgs(bool TagTemplates) {
5100 if (!consumeIf('I'))
5103 // <template-params> refer to the innermost <template-args>. Clear out any
5104 // outer args that we may have inserted into TemplateParams.
5106 TemplateParams.clear();
5108 size_t ArgsBegin = Names.size();
5109 while (!consumeIf('E')) {
5111 auto OldParams = std::move(TemplateParams);
5112 Node *Arg = getDerived().parseTemplateArg();
5113 TemplateParams = std::move(OldParams);
5116 Names.push_back(Arg);
5117 Node *TableEntry = Arg;
5118 if (Arg->getKind() == Node::KTemplateArgumentPack) {
5119 TableEntry = make<ParameterPack>(
5120 static_cast<TemplateArgumentPack*>(TableEntry)->getElements());
5124 TemplateParams.push_back(TableEntry);
5126 Node *Arg = getDerived().parseTemplateArg();
5129 Names.push_back(Arg);
5132 return make<TemplateArgs>(popTrailingNodeArray(ArgsBegin));
5135 // <mangled-name> ::= _Z <encoding>
5137 // extension ::= ___Z <encoding> _block_invoke
5138 // extension ::= ___Z <encoding> _block_invoke<decimal-digit>+
5139 // extension ::= ___Z <encoding> _block_invoke_<decimal-digit>+
5140 template <typename Derived, typename Alloc>
5141 Node *AbstractManglingParser<Derived, Alloc>::parse() {
5142 if (consumeIf("_Z")) {
5143 Node *Encoding = getDerived().parseEncoding();
5144 if (Encoding == nullptr)
5146 if (look() == '.') {
5147 Encoding = make<DotSuffix>(Encoding, StringView(First, Last));
5155 if (consumeIf("___Z")) {
5156 Node *Encoding = getDerived().parseEncoding();
5157 if (Encoding == nullptr || !consumeIf("_block_invoke"))
5159 bool RequireNumber = consumeIf('_');
5160 if (parseNumber().empty() && RequireNumber)
5166 return make<SpecialName>("invocation function for block in ", Encoding);
5169 Node *Ty = getDerived().parseType();
5175 template <typename Alloc>
5176 struct ManglingParser : AbstractManglingParser<ManglingParser<Alloc>, Alloc> {
5177 using AbstractManglingParser<ManglingParser<Alloc>,
5178 Alloc>::AbstractManglingParser;
5181 } // namespace itanium_demangle
5184 #endif // LLVM_DEMANGLE_ITANIUMDEMANGLE_H