blob: ad1034f0fba3f397a4cf08f0959cbda5db041ac5 [file] [log] [blame]
Richard Smithc20d1442018-08-20 20:14:49 +00001//===------------------------- ItaniumDemangle.h ----------------*- C++ -*-===//
2//
Chandler Carruth8ee27c32019-01-19 10:56:40 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Richard Smithc20d1442018-08-20 20:14:49 +00006//
7//===----------------------------------------------------------------------===//
8//
Erik Pilkingtonf70e4d82019-01-17 20:37:51 +00009// Generic itanium demangler library. This file has two byte-per-byte identical
10// copies in the source tree, one in libcxxabi, and the other in llvm.
Richard Smithc20d1442018-08-20 20:14:49 +000011//
12//===----------------------------------------------------------------------===//
13
Erik Pilkingtonf70e4d82019-01-17 20:37:51 +000014#ifndef DEMANGLE_ITANIUMDEMANGLE_H
15#define DEMANGLE_ITANIUMDEMANGLE_H
Richard Smithc20d1442018-08-20 20:14:49 +000016
17// FIXME: (possibly) incomplete list of features that clang mangles that this
18// file does not yet support:
19// - C++ modules TS
20
Erik Pilkingtonf70e4d82019-01-17 20:37:51 +000021#include "DemangleConfig.h"
Richard Smithc20d1442018-08-20 20:14:49 +000022#include "StringView.h"
23#include "Utility.h"
Richard Smithc20d1442018-08-20 20:14:49 +000024#include <cassert>
25#include <cctype>
26#include <cstdio>
27#include <cstdlib>
28#include <cstring>
29#include <numeric>
30#include <utility>
31
32#define FOR_EACH_NODE_KIND(X) \
33 X(NodeArrayNode) \
34 X(DotSuffix) \
35 X(VendorExtQualType) \
36 X(QualType) \
37 X(ConversionOperatorType) \
38 X(PostfixQualifiedType) \
39 X(ElaboratedTypeSpefType) \
40 X(NameType) \
41 X(AbiTagAttr) \
42 X(EnableIfAttr) \
43 X(ObjCProtoName) \
44 X(PointerType) \
45 X(ReferenceType) \
46 X(PointerToMemberType) \
47 X(ArrayType) \
48 X(FunctionType) \
49 X(NoexceptSpec) \
50 X(DynamicExceptionSpec) \
51 X(FunctionEncoding) \
52 X(LiteralOperator) \
53 X(SpecialName) \
54 X(CtorVtableSpecialName) \
55 X(QualifiedName) \
56 X(NestedName) \
57 X(LocalName) \
58 X(VectorType) \
59 X(PixelVectorType) \
60 X(ParameterPack) \
61 X(TemplateArgumentPack) \
62 X(ParameterPackExpansion) \
63 X(TemplateArgs) \
64 X(ForwardTemplateReference) \
65 X(NameWithTemplateArgs) \
66 X(GlobalQualifiedName) \
67 X(StdQualifiedName) \
68 X(ExpandedSpecialSubstitution) \
69 X(SpecialSubstitution) \
70 X(CtorDtorName) \
71 X(DtorName) \
72 X(UnnamedTypeName) \
73 X(ClosureTypeName) \
74 X(StructuredBindingName) \
75 X(BinaryExpr) \
76 X(ArraySubscriptExpr) \
77 X(PostfixExpr) \
78 X(ConditionalExpr) \
79 X(MemberExpr) \
80 X(EnclosingExpr) \
81 X(CastExpr) \
82 X(SizeofParamPackExpr) \
83 X(CallExpr) \
84 X(NewExpr) \
85 X(DeleteExpr) \
86 X(PrefixExpr) \
87 X(FunctionParam) \
88 X(ConversionExpr) \
89 X(InitListExpr) \
90 X(FoldExpr) \
91 X(ThrowExpr) \
Erik Pilkingtone8457c62019-06-18 23:34:09 +000092 X(UUIDOfExpr) \
Richard Smithc20d1442018-08-20 20:14:49 +000093 X(BoolExpr) \
94 X(IntegerCastExpr) \
95 X(IntegerLiteral) \
96 X(FloatLiteral) \
97 X(DoubleLiteral) \
98 X(LongDoubleLiteral) \
99 X(BracedExpr) \
100 X(BracedRangeExpr)
101
Erik Pilkingtonf70e4d82019-01-17 20:37:51 +0000102DEMANGLE_NAMESPACE_BEGIN
103
Richard Smithc20d1442018-08-20 20:14:49 +0000104// Base class of all AST nodes. The AST is built by the parser, then is
105// traversed by the printLeft/Right functions to produce a demangled string.
106class Node {
107public:
108 enum Kind : unsigned char {
109#define ENUMERATOR(NodeKind) K ## NodeKind,
110 FOR_EACH_NODE_KIND(ENUMERATOR)
111#undef ENUMERATOR
112 };
113
114 /// Three-way bool to track a cached value. Unknown is possible if this node
115 /// has an unexpanded parameter pack below it that may affect this cache.
116 enum class Cache : unsigned char { Yes, No, Unknown, };
117
118private:
119 Kind K;
120
121 // FIXME: Make these protected.
122public:
123 /// Tracks if this node has a component on its right side, in which case we
124 /// need to call printRight.
125 Cache RHSComponentCache;
126
127 /// Track if this node is a (possibly qualified) array type. This can affect
128 /// how we format the output string.
129 Cache ArrayCache;
130
131 /// Track if this node is a (possibly qualified) function type. This can
132 /// affect how we format the output string.
133 Cache FunctionCache;
134
135public:
136 Node(Kind K_, Cache RHSComponentCache_ = Cache::No,
137 Cache ArrayCache_ = Cache::No, Cache FunctionCache_ = Cache::No)
138 : K(K_), RHSComponentCache(RHSComponentCache_), ArrayCache(ArrayCache_),
139 FunctionCache(FunctionCache_) {}
140
141 /// Visit the most-derived object corresponding to this object.
142 template<typename Fn> void visit(Fn F) const;
143
144 // The following function is provided by all derived classes:
145 //
146 // Call F with arguments that, when passed to the constructor of this node,
147 // would construct an equivalent node.
148 //template<typename Fn> void match(Fn F) const;
149
150 bool hasRHSComponent(OutputStream &S) const {
151 if (RHSComponentCache != Cache::Unknown)
152 return RHSComponentCache == Cache::Yes;
153 return hasRHSComponentSlow(S);
154 }
155
156 bool hasArray(OutputStream &S) const {
157 if (ArrayCache != Cache::Unknown)
158 return ArrayCache == Cache::Yes;
159 return hasArraySlow(S);
160 }
161
162 bool hasFunction(OutputStream &S) const {
163 if (FunctionCache != Cache::Unknown)
164 return FunctionCache == Cache::Yes;
165 return hasFunctionSlow(S);
166 }
167
168 Kind getKind() const { return K; }
169
170 virtual bool hasRHSComponentSlow(OutputStream &) const { return false; }
171 virtual bool hasArraySlow(OutputStream &) const { return false; }
172 virtual bool hasFunctionSlow(OutputStream &) const { return false; }
173
174 // Dig through "glue" nodes like ParameterPack and ForwardTemplateReference to
175 // get at a node that actually represents some concrete syntax.
176 virtual const Node *getSyntaxNode(OutputStream &) const {
177 return this;
178 }
179
180 void print(OutputStream &S) const {
181 printLeft(S);
182 if (RHSComponentCache != Cache::No)
183 printRight(S);
184 }
185
186 // Print the "left" side of this Node into OutputStream.
187 virtual void printLeft(OutputStream &) const = 0;
188
189 // Print the "right". This distinction is necessary to represent C++ types
190 // that appear on the RHS of their subtype, such as arrays or functions.
191 // Since most types don't have such a component, provide a default
192 // implementation.
193 virtual void printRight(OutputStream &) const {}
194
195 virtual StringView getBaseName() const { return StringView(); }
196
197 // Silence compiler warnings, this dtor will never be called.
198 virtual ~Node() = default;
199
200#ifndef NDEBUG
Erik Pilkingtonf70e4d82019-01-17 20:37:51 +0000201 DEMANGLE_DUMP_METHOD void dump() const;
Richard Smithc20d1442018-08-20 20:14:49 +0000202#endif
203};
204
205class NodeArray {
206 Node **Elements;
207 size_t NumElements;
208
209public:
210 NodeArray() : Elements(nullptr), NumElements(0) {}
211 NodeArray(Node **Elements_, size_t NumElements_)
212 : Elements(Elements_), NumElements(NumElements_) {}
213
214 bool empty() const { return NumElements == 0; }
215 size_t size() const { return NumElements; }
216
217 Node **begin() const { return Elements; }
218 Node **end() const { return Elements + NumElements; }
219
220 Node *operator[](size_t Idx) const { return Elements[Idx]; }
221
222 void printWithComma(OutputStream &S) const {
223 bool FirstElement = true;
224 for (size_t Idx = 0; Idx != NumElements; ++Idx) {
225 size_t BeforeComma = S.getCurrentPosition();
226 if (!FirstElement)
227 S += ", ";
228 size_t AfterComma = S.getCurrentPosition();
229 Elements[Idx]->print(S);
230
231 // Elements[Idx] is an empty parameter pack expansion, we should erase the
232 // comma we just printed.
233 if (AfterComma == S.getCurrentPosition()) {
234 S.setCurrentPosition(BeforeComma);
235 continue;
236 }
237
238 FirstElement = false;
239 }
240 }
241};
242
243struct NodeArrayNode : Node {
244 NodeArray Array;
245 NodeArrayNode(NodeArray Array_) : Node(KNodeArrayNode), Array(Array_) {}
246
247 template<typename Fn> void match(Fn F) const { F(Array); }
248
249 void printLeft(OutputStream &S) const override {
250 Array.printWithComma(S);
251 }
252};
253
254class DotSuffix final : public Node {
255 const Node *Prefix;
256 const StringView Suffix;
257
258public:
259 DotSuffix(const Node *Prefix_, StringView Suffix_)
260 : Node(KDotSuffix), Prefix(Prefix_), Suffix(Suffix_) {}
261
262 template<typename Fn> void match(Fn F) const { F(Prefix, Suffix); }
263
264 void printLeft(OutputStream &s) const override {
265 Prefix->print(s);
266 s += " (";
267 s += Suffix;
268 s += ")";
269 }
270};
271
272class VendorExtQualType final : public Node {
273 const Node *Ty;
274 StringView Ext;
275
276public:
277 VendorExtQualType(const Node *Ty_, StringView Ext_)
278 : Node(KVendorExtQualType), Ty(Ty_), Ext(Ext_) {}
279
280 template<typename Fn> void match(Fn F) const { F(Ty, Ext); }
281
282 void printLeft(OutputStream &S) const override {
283 Ty->print(S);
284 S += " ";
285 S += Ext;
286 }
287};
288
289enum FunctionRefQual : unsigned char {
290 FrefQualNone,
291 FrefQualLValue,
292 FrefQualRValue,
293};
294
295enum Qualifiers {
296 QualNone = 0,
297 QualConst = 0x1,
298 QualVolatile = 0x2,
299 QualRestrict = 0x4,
300};
301
302inline Qualifiers operator|=(Qualifiers &Q1, Qualifiers Q2) {
303 return Q1 = static_cast<Qualifiers>(Q1 | Q2);
304}
305
306class QualType : public Node {
307protected:
308 const Qualifiers Quals;
309 const Node *Child;
310
311 void printQuals(OutputStream &S) const {
312 if (Quals & QualConst)
313 S += " const";
314 if (Quals & QualVolatile)
315 S += " volatile";
316 if (Quals & QualRestrict)
317 S += " restrict";
318 }
319
320public:
321 QualType(const Node *Child_, Qualifiers Quals_)
322 : Node(KQualType, Child_->RHSComponentCache,
323 Child_->ArrayCache, Child_->FunctionCache),
324 Quals(Quals_), Child(Child_) {}
325
326 template<typename Fn> void match(Fn F) const { F(Child, Quals); }
327
328 bool hasRHSComponentSlow(OutputStream &S) const override {
329 return Child->hasRHSComponent(S);
330 }
331 bool hasArraySlow(OutputStream &S) const override {
332 return Child->hasArray(S);
333 }
334 bool hasFunctionSlow(OutputStream &S) const override {
335 return Child->hasFunction(S);
336 }
337
338 void printLeft(OutputStream &S) const override {
339 Child->printLeft(S);
340 printQuals(S);
341 }
342
343 void printRight(OutputStream &S) const override { Child->printRight(S); }
344};
345
346class ConversionOperatorType final : public Node {
347 const Node *Ty;
348
349public:
350 ConversionOperatorType(const Node *Ty_)
351 : Node(KConversionOperatorType), Ty(Ty_) {}
352
353 template<typename Fn> void match(Fn F) const { F(Ty); }
354
355 void printLeft(OutputStream &S) const override {
356 S += "operator ";
357 Ty->print(S);
358 }
359};
360
361class PostfixQualifiedType final : public Node {
362 const Node *Ty;
363 const StringView Postfix;
364
365public:
366 PostfixQualifiedType(Node *Ty_, StringView Postfix_)
367 : Node(KPostfixQualifiedType), Ty(Ty_), Postfix(Postfix_) {}
368
369 template<typename Fn> void match(Fn F) const { F(Ty, Postfix); }
370
371 void printLeft(OutputStream &s) const override {
372 Ty->printLeft(s);
373 s += Postfix;
374 }
375};
376
377class NameType final : public Node {
378 const StringView Name;
379
380public:
381 NameType(StringView Name_) : Node(KNameType), Name(Name_) {}
382
383 template<typename Fn> void match(Fn F) const { F(Name); }
384
385 StringView getName() const { return Name; }
386 StringView getBaseName() const override { return Name; }
387
388 void printLeft(OutputStream &s) const override { s += Name; }
389};
390
391class ElaboratedTypeSpefType : public Node {
392 StringView Kind;
393 Node *Child;
394public:
395 ElaboratedTypeSpefType(StringView Kind_, Node *Child_)
396 : Node(KElaboratedTypeSpefType), Kind(Kind_), Child(Child_) {}
397
398 template<typename Fn> void match(Fn F) const { F(Kind, Child); }
399
400 void printLeft(OutputStream &S) const override {
401 S += Kind;
402 S += ' ';
403 Child->print(S);
404 }
405};
406
407struct AbiTagAttr : Node {
408 Node *Base;
409 StringView Tag;
410
411 AbiTagAttr(Node* Base_, StringView Tag_)
412 : Node(KAbiTagAttr, Base_->RHSComponentCache,
413 Base_->ArrayCache, Base_->FunctionCache),
414 Base(Base_), Tag(Tag_) {}
415
416 template<typename Fn> void match(Fn F) const { F(Base, Tag); }
417
418 void printLeft(OutputStream &S) const override {
419 Base->printLeft(S);
420 S += "[abi:";
421 S += Tag;
422 S += "]";
423 }
424};
425
426class EnableIfAttr : public Node {
427 NodeArray Conditions;
428public:
429 EnableIfAttr(NodeArray Conditions_)
430 : Node(KEnableIfAttr), Conditions(Conditions_) {}
431
432 template<typename Fn> void match(Fn F) const { F(Conditions); }
433
434 void printLeft(OutputStream &S) const override {
435 S += " [enable_if:";
436 Conditions.printWithComma(S);
437 S += ']';
438 }
439};
440
441class ObjCProtoName : public Node {
442 const Node *Ty;
443 StringView Protocol;
444
445 friend class PointerType;
446
447public:
448 ObjCProtoName(const Node *Ty_, StringView Protocol_)
449 : Node(KObjCProtoName), Ty(Ty_), Protocol(Protocol_) {}
450
451 template<typename Fn> void match(Fn F) const { F(Ty, Protocol); }
452
453 bool isObjCObject() const {
454 return Ty->getKind() == KNameType &&
455 static_cast<const NameType *>(Ty)->getName() == "objc_object";
456 }
457
458 void printLeft(OutputStream &S) const override {
459 Ty->print(S);
460 S += "<";
461 S += Protocol;
462 S += ">";
463 }
464};
465
466class PointerType final : public Node {
467 const Node *Pointee;
468
469public:
470 PointerType(const Node *Pointee_)
471 : Node(KPointerType, Pointee_->RHSComponentCache),
472 Pointee(Pointee_) {}
473
474 template<typename Fn> void match(Fn F) const { F(Pointee); }
475
476 bool hasRHSComponentSlow(OutputStream &S) const override {
477 return Pointee->hasRHSComponent(S);
478 }
479
480 void printLeft(OutputStream &s) const override {
481 // We rewrite objc_object<SomeProtocol>* into id<SomeProtocol>.
482 if (Pointee->getKind() != KObjCProtoName ||
483 !static_cast<const ObjCProtoName *>(Pointee)->isObjCObject()) {
484 Pointee->printLeft(s);
485 if (Pointee->hasArray(s))
486 s += " ";
487 if (Pointee->hasArray(s) || Pointee->hasFunction(s))
488 s += "(";
489 s += "*";
490 } else {
491 const auto *objcProto = static_cast<const ObjCProtoName *>(Pointee);
492 s += "id<";
493 s += objcProto->Protocol;
494 s += ">";
495 }
496 }
497
498 void printRight(OutputStream &s) const override {
499 if (Pointee->getKind() != KObjCProtoName ||
500 !static_cast<const ObjCProtoName *>(Pointee)->isObjCObject()) {
501 if (Pointee->hasArray(s) || Pointee->hasFunction(s))
502 s += ")";
503 Pointee->printRight(s);
504 }
505 }
506};
507
508enum class ReferenceKind {
509 LValue,
510 RValue,
511};
512
513// Represents either a LValue or an RValue reference type.
514class ReferenceType : public Node {
515 const Node *Pointee;
516 ReferenceKind RK;
517
518 mutable bool Printing = false;
519
520 // Dig through any refs to refs, collapsing the ReferenceTypes as we go. The
521 // rule here is rvalue ref to rvalue ref collapses to a rvalue ref, and any
522 // other combination collapses to a lvalue ref.
523 std::pair<ReferenceKind, const Node *> collapse(OutputStream &S) const {
524 auto SoFar = std::make_pair(RK, Pointee);
525 for (;;) {
526 const Node *SN = SoFar.second->getSyntaxNode(S);
527 if (SN->getKind() != KReferenceType)
528 break;
529 auto *RT = static_cast<const ReferenceType *>(SN);
530 SoFar.second = RT->Pointee;
531 SoFar.first = std::min(SoFar.first, RT->RK);
532 }
533 return SoFar;
534 }
535
536public:
537 ReferenceType(const Node *Pointee_, ReferenceKind RK_)
538 : Node(KReferenceType, Pointee_->RHSComponentCache),
539 Pointee(Pointee_), RK(RK_) {}
540
541 template<typename Fn> void match(Fn F) const { F(Pointee, RK); }
542
543 bool hasRHSComponentSlow(OutputStream &S) const override {
544 return Pointee->hasRHSComponent(S);
545 }
546
547 void printLeft(OutputStream &s) const override {
548 if (Printing)
549 return;
550 SwapAndRestore<bool> SavePrinting(Printing, true);
551 std::pair<ReferenceKind, const Node *> Collapsed = collapse(s);
552 Collapsed.second->printLeft(s);
553 if (Collapsed.second->hasArray(s))
554 s += " ";
555 if (Collapsed.second->hasArray(s) || Collapsed.second->hasFunction(s))
556 s += "(";
557
558 s += (Collapsed.first == ReferenceKind::LValue ? "&" : "&&");
559 }
560 void printRight(OutputStream &s) const override {
561 if (Printing)
562 return;
563 SwapAndRestore<bool> SavePrinting(Printing, true);
564 std::pair<ReferenceKind, const Node *> Collapsed = collapse(s);
565 if (Collapsed.second->hasArray(s) || Collapsed.second->hasFunction(s))
566 s += ")";
567 Collapsed.second->printRight(s);
568 }
569};
570
571class PointerToMemberType final : public Node {
572 const Node *ClassType;
573 const Node *MemberType;
574
575public:
576 PointerToMemberType(const Node *ClassType_, const Node *MemberType_)
577 : Node(KPointerToMemberType, MemberType_->RHSComponentCache),
578 ClassType(ClassType_), MemberType(MemberType_) {}
579
580 template<typename Fn> void match(Fn F) const { F(ClassType, MemberType); }
581
582 bool hasRHSComponentSlow(OutputStream &S) const override {
583 return MemberType->hasRHSComponent(S);
584 }
585
586 void printLeft(OutputStream &s) const override {
587 MemberType->printLeft(s);
588 if (MemberType->hasArray(s) || MemberType->hasFunction(s))
589 s += "(";
590 else
591 s += " ";
592 ClassType->print(s);
593 s += "::*";
594 }
595
596 void printRight(OutputStream &s) const override {
597 if (MemberType->hasArray(s) || MemberType->hasFunction(s))
598 s += ")";
599 MemberType->printRight(s);
600 }
601};
602
603class NodeOrString {
604 const void *First;
605 const void *Second;
606
607public:
608 /* implicit */ NodeOrString(StringView Str) {
609 const char *FirstChar = Str.begin();
610 const char *SecondChar = Str.end();
611 if (SecondChar == nullptr) {
612 assert(FirstChar == SecondChar);
613 ++FirstChar, ++SecondChar;
614 }
615 First = static_cast<const void *>(FirstChar);
616 Second = static_cast<const void *>(SecondChar);
617 }
618
619 /* implicit */ NodeOrString(Node *N)
620 : First(static_cast<const void *>(N)), Second(nullptr) {}
621 NodeOrString() : First(nullptr), Second(nullptr) {}
622
623 bool isString() const { return Second && First; }
624 bool isNode() const { return First && !Second; }
625 bool isEmpty() const { return !First && !Second; }
626
627 StringView asString() const {
628 assert(isString());
629 return StringView(static_cast<const char *>(First),
630 static_cast<const char *>(Second));
631 }
632
633 const Node *asNode() const {
634 assert(isNode());
635 return static_cast<const Node *>(First);
636 }
637};
638
639class ArrayType final : public Node {
640 const Node *Base;
641 NodeOrString Dimension;
642
643public:
Pavel Labathf4e67eb2018-10-10 08:39:16 +0000644 ArrayType(const Node *Base_, NodeOrString Dimension_)
Richard Smithc20d1442018-08-20 20:14:49 +0000645 : Node(KArrayType,
646 /*RHSComponentCache=*/Cache::Yes,
647 /*ArrayCache=*/Cache::Yes),
648 Base(Base_), Dimension(Dimension_) {}
649
650 template<typename Fn> void match(Fn F) const { F(Base, Dimension); }
651
652 bool hasRHSComponentSlow(OutputStream &) const override { return true; }
653 bool hasArraySlow(OutputStream &) const override { return true; }
654
655 void printLeft(OutputStream &S) const override { Base->printLeft(S); }
656
657 void printRight(OutputStream &S) const override {
658 if (S.back() != ']')
659 S += " ";
660 S += "[";
661 if (Dimension.isString())
662 S += Dimension.asString();
663 else if (Dimension.isNode())
664 Dimension.asNode()->print(S);
665 S += "]";
666 Base->printRight(S);
667 }
668};
669
670class FunctionType final : public Node {
671 const Node *Ret;
672 NodeArray Params;
673 Qualifiers CVQuals;
674 FunctionRefQual RefQual;
675 const Node *ExceptionSpec;
676
677public:
678 FunctionType(const Node *Ret_, NodeArray Params_, Qualifiers CVQuals_,
679 FunctionRefQual RefQual_, const Node *ExceptionSpec_)
680 : Node(KFunctionType,
681 /*RHSComponentCache=*/Cache::Yes, /*ArrayCache=*/Cache::No,
682 /*FunctionCache=*/Cache::Yes),
683 Ret(Ret_), Params(Params_), CVQuals(CVQuals_), RefQual(RefQual_),
684 ExceptionSpec(ExceptionSpec_) {}
685
686 template<typename Fn> void match(Fn F) const {
687 F(Ret, Params, CVQuals, RefQual, ExceptionSpec);
688 }
689
690 bool hasRHSComponentSlow(OutputStream &) const override { return true; }
691 bool hasFunctionSlow(OutputStream &) const override { return true; }
692
693 // Handle C++'s ... quirky decl grammar by using the left & right
694 // distinction. Consider:
695 // int (*f(float))(char) {}
696 // f is a function that takes a float and returns a pointer to a function
697 // that takes a char and returns an int. If we're trying to print f, start
698 // by printing out the return types's left, then print our parameters, then
699 // finally print right of the return type.
700 void printLeft(OutputStream &S) const override {
701 Ret->printLeft(S);
702 S += " ";
703 }
704
705 void printRight(OutputStream &S) const override {
706 S += "(";
707 Params.printWithComma(S);
708 S += ")";
709 Ret->printRight(S);
710
711 if (CVQuals & QualConst)
712 S += " const";
713 if (CVQuals & QualVolatile)
714 S += " volatile";
715 if (CVQuals & QualRestrict)
716 S += " restrict";
717
718 if (RefQual == FrefQualLValue)
719 S += " &";
720 else if (RefQual == FrefQualRValue)
721 S += " &&";
722
723 if (ExceptionSpec != nullptr) {
724 S += ' ';
725 ExceptionSpec->print(S);
726 }
727 }
728};
729
730class NoexceptSpec : public Node {
731 const Node *E;
732public:
733 NoexceptSpec(const Node *E_) : Node(KNoexceptSpec), E(E_) {}
734
735 template<typename Fn> void match(Fn F) const { F(E); }
736
737 void printLeft(OutputStream &S) const override {
738 S += "noexcept(";
739 E->print(S);
740 S += ")";
741 }
742};
743
744class DynamicExceptionSpec : public Node {
745 NodeArray Types;
746public:
747 DynamicExceptionSpec(NodeArray Types_)
748 : Node(KDynamicExceptionSpec), Types(Types_) {}
749
750 template<typename Fn> void match(Fn F) const { F(Types); }
751
752 void printLeft(OutputStream &S) const override {
753 S += "throw(";
754 Types.printWithComma(S);
755 S += ')';
756 }
757};
758
759class FunctionEncoding final : public Node {
760 const Node *Ret;
761 const Node *Name;
762 NodeArray Params;
763 const Node *Attrs;
764 Qualifiers CVQuals;
765 FunctionRefQual RefQual;
766
767public:
768 FunctionEncoding(const Node *Ret_, const Node *Name_, NodeArray Params_,
769 const Node *Attrs_, Qualifiers CVQuals_,
770 FunctionRefQual RefQual_)
771 : Node(KFunctionEncoding,
772 /*RHSComponentCache=*/Cache::Yes, /*ArrayCache=*/Cache::No,
773 /*FunctionCache=*/Cache::Yes),
774 Ret(Ret_), Name(Name_), Params(Params_), Attrs(Attrs_),
775 CVQuals(CVQuals_), RefQual(RefQual_) {}
776
777 template<typename Fn> void match(Fn F) const {
778 F(Ret, Name, Params, Attrs, CVQuals, RefQual);
779 }
780
781 Qualifiers getCVQuals() const { return CVQuals; }
782 FunctionRefQual getRefQual() const { return RefQual; }
783 NodeArray getParams() const { return Params; }
784 const Node *getReturnType() const { return Ret; }
785
786 bool hasRHSComponentSlow(OutputStream &) const override { return true; }
787 bool hasFunctionSlow(OutputStream &) const override { return true; }
788
789 const Node *getName() const { return Name; }
790
791 void printLeft(OutputStream &S) const override {
792 if (Ret) {
793 Ret->printLeft(S);
794 if (!Ret->hasRHSComponent(S))
795 S += " ";
796 }
797 Name->print(S);
798 }
799
800 void printRight(OutputStream &S) const override {
801 S += "(";
802 Params.printWithComma(S);
803 S += ")";
804 if (Ret)
805 Ret->printRight(S);
806
807 if (CVQuals & QualConst)
808 S += " const";
809 if (CVQuals & QualVolatile)
810 S += " volatile";
811 if (CVQuals & QualRestrict)
812 S += " restrict";
813
814 if (RefQual == FrefQualLValue)
815 S += " &";
816 else if (RefQual == FrefQualRValue)
817 S += " &&";
818
819 if (Attrs != nullptr)
820 Attrs->print(S);
821 }
822};
823
824class LiteralOperator : public Node {
825 const Node *OpName;
826
827public:
828 LiteralOperator(const Node *OpName_)
829 : Node(KLiteralOperator), OpName(OpName_) {}
830
831 template<typename Fn> void match(Fn F) const { F(OpName); }
832
833 void printLeft(OutputStream &S) const override {
834 S += "operator\"\" ";
835 OpName->print(S);
836 }
837};
838
839class SpecialName final : public Node {
840 const StringView Special;
841 const Node *Child;
842
843public:
844 SpecialName(StringView Special_, const Node *Child_)
845 : Node(KSpecialName), Special(Special_), Child(Child_) {}
846
847 template<typename Fn> void match(Fn F) const { F(Special, Child); }
848
849 void printLeft(OutputStream &S) const override {
850 S += Special;
851 Child->print(S);
852 }
853};
854
855class CtorVtableSpecialName final : public Node {
856 const Node *FirstType;
857 const Node *SecondType;
858
859public:
860 CtorVtableSpecialName(const Node *FirstType_, const Node *SecondType_)
861 : Node(KCtorVtableSpecialName),
862 FirstType(FirstType_), SecondType(SecondType_) {}
863
864 template<typename Fn> void match(Fn F) const { F(FirstType, SecondType); }
865
866 void printLeft(OutputStream &S) const override {
867 S += "construction vtable for ";
868 FirstType->print(S);
869 S += "-in-";
870 SecondType->print(S);
871 }
872};
873
874struct NestedName : Node {
875 Node *Qual;
876 Node *Name;
877
878 NestedName(Node *Qual_, Node *Name_)
879 : Node(KNestedName), Qual(Qual_), Name(Name_) {}
880
881 template<typename Fn> void match(Fn F) const { F(Qual, Name); }
882
883 StringView getBaseName() const override { return Name->getBaseName(); }
884
885 void printLeft(OutputStream &S) const override {
886 Qual->print(S);
887 S += "::";
888 Name->print(S);
889 }
890};
891
892struct LocalName : Node {
893 Node *Encoding;
894 Node *Entity;
895
896 LocalName(Node *Encoding_, Node *Entity_)
897 : Node(KLocalName), Encoding(Encoding_), Entity(Entity_) {}
898
899 template<typename Fn> void match(Fn F) const { F(Encoding, Entity); }
900
901 void printLeft(OutputStream &S) const override {
902 Encoding->print(S);
903 S += "::";
904 Entity->print(S);
905 }
906};
907
908class QualifiedName final : public Node {
909 // qualifier::name
910 const Node *Qualifier;
911 const Node *Name;
912
913public:
914 QualifiedName(const Node *Qualifier_, const Node *Name_)
915 : Node(KQualifiedName), Qualifier(Qualifier_), Name(Name_) {}
916
917 template<typename Fn> void match(Fn F) const { F(Qualifier, Name); }
918
919 StringView getBaseName() const override { return Name->getBaseName(); }
920
921 void printLeft(OutputStream &S) const override {
922 Qualifier->print(S);
923 S += "::";
924 Name->print(S);
925 }
926};
927
928class VectorType final : public Node {
929 const Node *BaseType;
930 const NodeOrString Dimension;
931
932public:
933 VectorType(const Node *BaseType_, NodeOrString Dimension_)
934 : Node(KVectorType), BaseType(BaseType_),
935 Dimension(Dimension_) {}
936
937 template<typename Fn> void match(Fn F) const { F(BaseType, Dimension); }
938
939 void printLeft(OutputStream &S) const override {
940 BaseType->print(S);
941 S += " vector[";
942 if (Dimension.isNode())
943 Dimension.asNode()->print(S);
944 else if (Dimension.isString())
945 S += Dimension.asString();
946 S += "]";
947 }
948};
949
950class PixelVectorType final : public Node {
951 const NodeOrString Dimension;
952
953public:
954 PixelVectorType(NodeOrString Dimension_)
955 : Node(KPixelVectorType), Dimension(Dimension_) {}
956
957 template<typename Fn> void match(Fn F) const { F(Dimension); }
958
959 void printLeft(OutputStream &S) const override {
960 // FIXME: This should demangle as "vector pixel".
961 S += "pixel vector[";
962 S += Dimension.asString();
963 S += "]";
964 }
965};
966
967/// An unexpanded parameter pack (either in the expression or type context). If
968/// this AST is correct, this node will have a ParameterPackExpansion node above
969/// it.
970///
971/// This node is created when some <template-args> are found that apply to an
972/// <encoding>, and is stored in the TemplateParams table. In order for this to
973/// appear in the final AST, it has to referenced via a <template-param> (ie,
974/// T_).
975class ParameterPack final : public Node {
976 NodeArray Data;
977
978 // Setup OutputStream for a pack expansion unless we're already expanding one.
979 void initializePackExpansion(OutputStream &S) const {
980 if (S.CurrentPackMax == std::numeric_limits<unsigned>::max()) {
981 S.CurrentPackMax = static_cast<unsigned>(Data.size());
982 S.CurrentPackIndex = 0;
983 }
984 }
985
986public:
987 ParameterPack(NodeArray Data_) : Node(KParameterPack), Data(Data_) {
988 ArrayCache = FunctionCache = RHSComponentCache = Cache::Unknown;
989 if (std::all_of(Data.begin(), Data.end(), [](Node* P) {
990 return P->ArrayCache == Cache::No;
991 }))
992 ArrayCache = Cache::No;
993 if (std::all_of(Data.begin(), Data.end(), [](Node* P) {
994 return P->FunctionCache == Cache::No;
995 }))
996 FunctionCache = Cache::No;
997 if (std::all_of(Data.begin(), Data.end(), [](Node* P) {
998 return P->RHSComponentCache == Cache::No;
999 }))
1000 RHSComponentCache = Cache::No;
1001 }
1002
1003 template<typename Fn> void match(Fn F) const { F(Data); }
1004
1005 bool hasRHSComponentSlow(OutputStream &S) const override {
1006 initializePackExpansion(S);
1007 size_t Idx = S.CurrentPackIndex;
1008 return Idx < Data.size() && Data[Idx]->hasRHSComponent(S);
1009 }
1010 bool hasArraySlow(OutputStream &S) const override {
1011 initializePackExpansion(S);
1012 size_t Idx = S.CurrentPackIndex;
1013 return Idx < Data.size() && Data[Idx]->hasArray(S);
1014 }
1015 bool hasFunctionSlow(OutputStream &S) const override {
1016 initializePackExpansion(S);
1017 size_t Idx = S.CurrentPackIndex;
1018 return Idx < Data.size() && Data[Idx]->hasFunction(S);
1019 }
1020 const Node *getSyntaxNode(OutputStream &S) const override {
1021 initializePackExpansion(S);
1022 size_t Idx = S.CurrentPackIndex;
1023 return Idx < Data.size() ? Data[Idx]->getSyntaxNode(S) : this;
1024 }
1025
1026 void printLeft(OutputStream &S) const override {
1027 initializePackExpansion(S);
1028 size_t Idx = S.CurrentPackIndex;
1029 if (Idx < Data.size())
1030 Data[Idx]->printLeft(S);
1031 }
1032 void printRight(OutputStream &S) const override {
1033 initializePackExpansion(S);
1034 size_t Idx = S.CurrentPackIndex;
1035 if (Idx < Data.size())
1036 Data[Idx]->printRight(S);
1037 }
1038};
1039
1040/// A variadic template argument. This node represents an occurrence of
1041/// J<something>E in some <template-args>. It isn't itself unexpanded, unless
1042/// one of it's Elements is. The parser inserts a ParameterPack into the
1043/// TemplateParams table if the <template-args> this pack belongs to apply to an
1044/// <encoding>.
1045class TemplateArgumentPack final : public Node {
1046 NodeArray Elements;
1047public:
1048 TemplateArgumentPack(NodeArray Elements_)
1049 : Node(KTemplateArgumentPack), Elements(Elements_) {}
1050
1051 template<typename Fn> void match(Fn F) const { F(Elements); }
1052
1053 NodeArray getElements() const { return Elements; }
1054
1055 void printLeft(OutputStream &S) const override {
1056 Elements.printWithComma(S);
1057 }
1058};
1059
1060/// A pack expansion. Below this node, there are some unexpanded ParameterPacks
1061/// which each have Child->ParameterPackSize elements.
1062class ParameterPackExpansion final : public Node {
1063 const Node *Child;
1064
1065public:
1066 ParameterPackExpansion(const Node *Child_)
1067 : Node(KParameterPackExpansion), Child(Child_) {}
1068
1069 template<typename Fn> void match(Fn F) const { F(Child); }
1070
1071 const Node *getChild() const { return Child; }
1072
1073 void printLeft(OutputStream &S) const override {
1074 constexpr unsigned Max = std::numeric_limits<unsigned>::max();
1075 SwapAndRestore<unsigned> SavePackIdx(S.CurrentPackIndex, Max);
1076 SwapAndRestore<unsigned> SavePackMax(S.CurrentPackMax, Max);
1077 size_t StreamPos = S.getCurrentPosition();
1078
1079 // Print the first element in the pack. If Child contains a ParameterPack,
1080 // it will set up S.CurrentPackMax and print the first element.
1081 Child->print(S);
1082
1083 // No ParameterPack was found in Child. This can occur if we've found a pack
1084 // expansion on a <function-param>.
1085 if (S.CurrentPackMax == Max) {
1086 S += "...";
1087 return;
1088 }
1089
1090 // We found a ParameterPack, but it has no elements. Erase whatever we may
1091 // of printed.
1092 if (S.CurrentPackMax == 0) {
1093 S.setCurrentPosition(StreamPos);
1094 return;
1095 }
1096
1097 // Else, iterate through the rest of the elements in the pack.
1098 for (unsigned I = 1, E = S.CurrentPackMax; I < E; ++I) {
1099 S += ", ";
1100 S.CurrentPackIndex = I;
1101 Child->print(S);
1102 }
1103 }
1104};
1105
1106class TemplateArgs final : public Node {
1107 NodeArray Params;
1108
1109public:
1110 TemplateArgs(NodeArray Params_) : Node(KTemplateArgs), Params(Params_) {}
1111
1112 template<typename Fn> void match(Fn F) const { F(Params); }
1113
1114 NodeArray getParams() { return Params; }
1115
1116 void printLeft(OutputStream &S) const override {
1117 S += "<";
1118 Params.printWithComma(S);
1119 if (S.back() == '>')
1120 S += " ";
1121 S += ">";
1122 }
1123};
1124
Richard Smithb485b352018-08-24 23:30:26 +00001125/// A forward-reference to a template argument that was not known at the point
1126/// where the template parameter name was parsed in a mangling.
1127///
1128/// This is created when demangling the name of a specialization of a
1129/// conversion function template:
1130///
1131/// \code
1132/// struct A {
1133/// template<typename T> operator T*();
1134/// };
1135/// \endcode
1136///
1137/// When demangling a specialization of the conversion function template, we
1138/// encounter the name of the template (including the \c T) before we reach
1139/// the template argument list, so we cannot substitute the parameter name
1140/// for the corresponding argument while parsing. Instead, we create a
1141/// \c ForwardTemplateReference node that is resolved after we parse the
1142/// template arguments.
Richard Smithc20d1442018-08-20 20:14:49 +00001143struct ForwardTemplateReference : Node {
1144 size_t Index;
1145 Node *Ref = nullptr;
1146
1147 // If we're currently printing this node. It is possible (though invalid) for
1148 // a forward template reference to refer to itself via a substitution. This
1149 // creates a cyclic AST, which will stack overflow printing. To fix this, bail
1150 // out if more than one print* function is active.
1151 mutable bool Printing = false;
1152
1153 ForwardTemplateReference(size_t Index_)
1154 : Node(KForwardTemplateReference, Cache::Unknown, Cache::Unknown,
1155 Cache::Unknown),
1156 Index(Index_) {}
1157
1158 // We don't provide a matcher for these, because the value of the node is
1159 // not determined by its construction parameters, and it generally needs
1160 // special handling.
1161 template<typename Fn> void match(Fn F) const = delete;
1162
1163 bool hasRHSComponentSlow(OutputStream &S) const override {
1164 if (Printing)
1165 return false;
1166 SwapAndRestore<bool> SavePrinting(Printing, true);
1167 return Ref->hasRHSComponent(S);
1168 }
1169 bool hasArraySlow(OutputStream &S) const override {
1170 if (Printing)
1171 return false;
1172 SwapAndRestore<bool> SavePrinting(Printing, true);
1173 return Ref->hasArray(S);
1174 }
1175 bool hasFunctionSlow(OutputStream &S) const override {
1176 if (Printing)
1177 return false;
1178 SwapAndRestore<bool> SavePrinting(Printing, true);
1179 return Ref->hasFunction(S);
1180 }
1181 const Node *getSyntaxNode(OutputStream &S) const override {
1182 if (Printing)
1183 return this;
1184 SwapAndRestore<bool> SavePrinting(Printing, true);
1185 return Ref->getSyntaxNode(S);
1186 }
1187
1188 void printLeft(OutputStream &S) const override {
1189 if (Printing)
1190 return;
1191 SwapAndRestore<bool> SavePrinting(Printing, true);
1192 Ref->printLeft(S);
1193 }
1194 void printRight(OutputStream &S) const override {
1195 if (Printing)
1196 return;
1197 SwapAndRestore<bool> SavePrinting(Printing, true);
1198 Ref->printRight(S);
1199 }
1200};
1201
1202struct NameWithTemplateArgs : Node {
1203 // name<template_args>
1204 Node *Name;
1205 Node *TemplateArgs;
1206
1207 NameWithTemplateArgs(Node *Name_, Node *TemplateArgs_)
1208 : Node(KNameWithTemplateArgs), Name(Name_), TemplateArgs(TemplateArgs_) {}
1209
1210 template<typename Fn> void match(Fn F) const { F(Name, TemplateArgs); }
1211
1212 StringView getBaseName() const override { return Name->getBaseName(); }
1213
1214 void printLeft(OutputStream &S) const override {
1215 Name->print(S);
1216 TemplateArgs->print(S);
1217 }
1218};
1219
1220class GlobalQualifiedName final : public Node {
1221 Node *Child;
1222
1223public:
1224 GlobalQualifiedName(Node* Child_)
1225 : Node(KGlobalQualifiedName), Child(Child_) {}
1226
1227 template<typename Fn> void match(Fn F) const { F(Child); }
1228
1229 StringView getBaseName() const override { return Child->getBaseName(); }
1230
1231 void printLeft(OutputStream &S) const override {
1232 S += "::";
1233 Child->print(S);
1234 }
1235};
1236
1237struct StdQualifiedName : Node {
1238 Node *Child;
1239
1240 StdQualifiedName(Node *Child_) : Node(KStdQualifiedName), Child(Child_) {}
1241
1242 template<typename Fn> void match(Fn F) const { F(Child); }
1243
1244 StringView getBaseName() const override { return Child->getBaseName(); }
1245
1246 void printLeft(OutputStream &S) const override {
1247 S += "std::";
1248 Child->print(S);
1249 }
1250};
1251
1252enum class SpecialSubKind {
1253 allocator,
1254 basic_string,
1255 string,
1256 istream,
1257 ostream,
1258 iostream,
1259};
1260
1261class ExpandedSpecialSubstitution final : public Node {
1262 SpecialSubKind SSK;
1263
1264public:
1265 ExpandedSpecialSubstitution(SpecialSubKind SSK_)
1266 : Node(KExpandedSpecialSubstitution), SSK(SSK_) {}
1267
1268 template<typename Fn> void match(Fn F) const { F(SSK); }
1269
1270 StringView getBaseName() const override {
1271 switch (SSK) {
1272 case SpecialSubKind::allocator:
1273 return StringView("allocator");
1274 case SpecialSubKind::basic_string:
1275 return StringView("basic_string");
1276 case SpecialSubKind::string:
1277 return StringView("basic_string");
1278 case SpecialSubKind::istream:
1279 return StringView("basic_istream");
1280 case SpecialSubKind::ostream:
1281 return StringView("basic_ostream");
1282 case SpecialSubKind::iostream:
1283 return StringView("basic_iostream");
1284 }
Erik Pilkingtonf70e4d82019-01-17 20:37:51 +00001285 DEMANGLE_UNREACHABLE;
Richard Smithc20d1442018-08-20 20:14:49 +00001286 }
1287
1288 void printLeft(OutputStream &S) const override {
1289 switch (SSK) {
1290 case SpecialSubKind::allocator:
Richard Smithb485b352018-08-24 23:30:26 +00001291 S += "std::allocator";
Richard Smithc20d1442018-08-20 20:14:49 +00001292 break;
1293 case SpecialSubKind::basic_string:
Richard Smithb485b352018-08-24 23:30:26 +00001294 S += "std::basic_string";
1295 break;
Richard Smithc20d1442018-08-20 20:14:49 +00001296 case SpecialSubKind::string:
1297 S += "std::basic_string<char, std::char_traits<char>, "
1298 "std::allocator<char> >";
1299 break;
1300 case SpecialSubKind::istream:
1301 S += "std::basic_istream<char, std::char_traits<char> >";
1302 break;
1303 case SpecialSubKind::ostream:
1304 S += "std::basic_ostream<char, std::char_traits<char> >";
1305 break;
1306 case SpecialSubKind::iostream:
1307 S += "std::basic_iostream<char, std::char_traits<char> >";
1308 break;
1309 }
1310 }
1311};
1312
1313class SpecialSubstitution final : public Node {
1314public:
1315 SpecialSubKind SSK;
1316
1317 SpecialSubstitution(SpecialSubKind SSK_)
1318 : Node(KSpecialSubstitution), SSK(SSK_) {}
1319
1320 template<typename Fn> void match(Fn F) const { F(SSK); }
1321
1322 StringView getBaseName() const override {
1323 switch (SSK) {
1324 case SpecialSubKind::allocator:
1325 return StringView("allocator");
1326 case SpecialSubKind::basic_string:
1327 return StringView("basic_string");
1328 case SpecialSubKind::string:
1329 return StringView("string");
1330 case SpecialSubKind::istream:
1331 return StringView("istream");
1332 case SpecialSubKind::ostream:
1333 return StringView("ostream");
1334 case SpecialSubKind::iostream:
1335 return StringView("iostream");
1336 }
Erik Pilkingtonf70e4d82019-01-17 20:37:51 +00001337 DEMANGLE_UNREACHABLE;
Richard Smithc20d1442018-08-20 20:14:49 +00001338 }
1339
1340 void printLeft(OutputStream &S) const override {
1341 switch (SSK) {
1342 case SpecialSubKind::allocator:
1343 S += "std::allocator";
1344 break;
1345 case SpecialSubKind::basic_string:
1346 S += "std::basic_string";
1347 break;
1348 case SpecialSubKind::string:
1349 S += "std::string";
1350 break;
1351 case SpecialSubKind::istream:
1352 S += "std::istream";
1353 break;
1354 case SpecialSubKind::ostream:
1355 S += "std::ostream";
1356 break;
1357 case SpecialSubKind::iostream:
1358 S += "std::iostream";
1359 break;
1360 }
1361 }
1362};
1363
1364class CtorDtorName final : public Node {
1365 const Node *Basename;
1366 const bool IsDtor;
Pavel Labathf4e67eb2018-10-10 08:39:16 +00001367 const int Variant;
Richard Smithc20d1442018-08-20 20:14:49 +00001368
1369public:
Pavel Labathf4e67eb2018-10-10 08:39:16 +00001370 CtorDtorName(const Node *Basename_, bool IsDtor_, int Variant_)
1371 : Node(KCtorDtorName), Basename(Basename_), IsDtor(IsDtor_),
1372 Variant(Variant_) {}
Richard Smithc20d1442018-08-20 20:14:49 +00001373
Pavel Labathf4e67eb2018-10-10 08:39:16 +00001374 template<typename Fn> void match(Fn F) const { F(Basename, IsDtor, Variant); }
Richard Smithc20d1442018-08-20 20:14:49 +00001375
1376 void printLeft(OutputStream &S) const override {
1377 if (IsDtor)
1378 S += "~";
1379 S += Basename->getBaseName();
1380 }
1381};
1382
1383class DtorName : public Node {
1384 const Node *Base;
1385
1386public:
1387 DtorName(const Node *Base_) : Node(KDtorName), Base(Base_) {}
1388
1389 template<typename Fn> void match(Fn F) const { F(Base); }
1390
1391 void printLeft(OutputStream &S) const override {
1392 S += "~";
1393 Base->printLeft(S);
1394 }
1395};
1396
1397class UnnamedTypeName : public Node {
1398 const StringView Count;
1399
1400public:
1401 UnnamedTypeName(StringView Count_) : Node(KUnnamedTypeName), Count(Count_) {}
1402
1403 template<typename Fn> void match(Fn F) const { F(Count); }
1404
1405 void printLeft(OutputStream &S) const override {
1406 S += "'unnamed";
1407 S += Count;
1408 S += "\'";
1409 }
1410};
1411
1412class ClosureTypeName : public Node {
1413 NodeArray Params;
1414 StringView Count;
1415
1416public:
1417 ClosureTypeName(NodeArray Params_, StringView Count_)
1418 : Node(KClosureTypeName), Params(Params_), Count(Count_) {}
1419
1420 template<typename Fn> void match(Fn F) const { F(Params, Count); }
1421
1422 void printLeft(OutputStream &S) const override {
1423 S += "\'lambda";
1424 S += Count;
1425 S += "\'(";
1426 Params.printWithComma(S);
1427 S += ")";
1428 }
1429};
1430
1431class StructuredBindingName : public Node {
1432 NodeArray Bindings;
1433public:
1434 StructuredBindingName(NodeArray Bindings_)
1435 : Node(KStructuredBindingName), Bindings(Bindings_) {}
1436
1437 template<typename Fn> void match(Fn F) const { F(Bindings); }
1438
1439 void printLeft(OutputStream &S) const override {
1440 S += '[';
1441 Bindings.printWithComma(S);
1442 S += ']';
1443 }
1444};
1445
1446// -- Expression Nodes --
1447
1448class BinaryExpr : public Node {
1449 const Node *LHS;
1450 const StringView InfixOperator;
1451 const Node *RHS;
1452
1453public:
1454 BinaryExpr(const Node *LHS_, StringView InfixOperator_, const Node *RHS_)
1455 : Node(KBinaryExpr), LHS(LHS_), InfixOperator(InfixOperator_), RHS(RHS_) {
1456 }
1457
1458 template<typename Fn> void match(Fn F) const { F(LHS, InfixOperator, RHS); }
1459
1460 void printLeft(OutputStream &S) const override {
1461 // might be a template argument expression, then we need to disambiguate
1462 // with parens.
1463 if (InfixOperator == ">")
1464 S += "(";
1465
1466 S += "(";
1467 LHS->print(S);
1468 S += ") ";
1469 S += InfixOperator;
1470 S += " (";
1471 RHS->print(S);
1472 S += ")";
1473
1474 if (InfixOperator == ">")
1475 S += ")";
1476 }
1477};
1478
1479class ArraySubscriptExpr : public Node {
1480 const Node *Op1;
1481 const Node *Op2;
1482
1483public:
1484 ArraySubscriptExpr(const Node *Op1_, const Node *Op2_)
1485 : Node(KArraySubscriptExpr), Op1(Op1_), Op2(Op2_) {}
1486
1487 template<typename Fn> void match(Fn F) const { F(Op1, Op2); }
1488
1489 void printLeft(OutputStream &S) const override {
1490 S += "(";
1491 Op1->print(S);
1492 S += ")[";
1493 Op2->print(S);
1494 S += "]";
1495 }
1496};
1497
1498class PostfixExpr : public Node {
1499 const Node *Child;
1500 const StringView Operator;
1501
1502public:
1503 PostfixExpr(const Node *Child_, StringView Operator_)
1504 : Node(KPostfixExpr), Child(Child_), Operator(Operator_) {}
1505
1506 template<typename Fn> void match(Fn F) const { F(Child, Operator); }
1507
1508 void printLeft(OutputStream &S) const override {
1509 S += "(";
1510 Child->print(S);
1511 S += ")";
1512 S += Operator;
1513 }
1514};
1515
1516class ConditionalExpr : public Node {
1517 const Node *Cond;
1518 const Node *Then;
1519 const Node *Else;
1520
1521public:
1522 ConditionalExpr(const Node *Cond_, const Node *Then_, const Node *Else_)
1523 : Node(KConditionalExpr), Cond(Cond_), Then(Then_), Else(Else_) {}
1524
1525 template<typename Fn> void match(Fn F) const { F(Cond, Then, Else); }
1526
1527 void printLeft(OutputStream &S) const override {
1528 S += "(";
1529 Cond->print(S);
1530 S += ") ? (";
1531 Then->print(S);
1532 S += ") : (";
1533 Else->print(S);
1534 S += ")";
1535 }
1536};
1537
1538class MemberExpr : public Node {
1539 const Node *LHS;
1540 const StringView Kind;
1541 const Node *RHS;
1542
1543public:
1544 MemberExpr(const Node *LHS_, StringView Kind_, const Node *RHS_)
1545 : Node(KMemberExpr), LHS(LHS_), Kind(Kind_), RHS(RHS_) {}
1546
1547 template<typename Fn> void match(Fn F) const { F(LHS, Kind, RHS); }
1548
1549 void printLeft(OutputStream &S) const override {
1550 LHS->print(S);
1551 S += Kind;
1552 RHS->print(S);
1553 }
1554};
1555
1556class EnclosingExpr : public Node {
1557 const StringView Prefix;
1558 const Node *Infix;
1559 const StringView Postfix;
1560
1561public:
1562 EnclosingExpr(StringView Prefix_, Node *Infix_, StringView Postfix_)
1563 : Node(KEnclosingExpr), Prefix(Prefix_), Infix(Infix_),
1564 Postfix(Postfix_) {}
1565
1566 template<typename Fn> void match(Fn F) const { F(Prefix, Infix, Postfix); }
1567
1568 void printLeft(OutputStream &S) const override {
1569 S += Prefix;
1570 Infix->print(S);
1571 S += Postfix;
1572 }
1573};
1574
1575class CastExpr : public Node {
1576 // cast_kind<to>(from)
1577 const StringView CastKind;
1578 const Node *To;
1579 const Node *From;
1580
1581public:
1582 CastExpr(StringView CastKind_, const Node *To_, const Node *From_)
1583 : Node(KCastExpr), CastKind(CastKind_), To(To_), From(From_) {}
1584
1585 template<typename Fn> void match(Fn F) const { F(CastKind, To, From); }
1586
1587 void printLeft(OutputStream &S) const override {
1588 S += CastKind;
1589 S += "<";
1590 To->printLeft(S);
1591 S += ">(";
1592 From->printLeft(S);
1593 S += ")";
1594 }
1595};
1596
1597class SizeofParamPackExpr : public Node {
1598 const Node *Pack;
1599
1600public:
1601 SizeofParamPackExpr(const Node *Pack_)
1602 : Node(KSizeofParamPackExpr), Pack(Pack_) {}
1603
1604 template<typename Fn> void match(Fn F) const { F(Pack); }
1605
1606 void printLeft(OutputStream &S) const override {
1607 S += "sizeof...(";
1608 ParameterPackExpansion PPE(Pack);
1609 PPE.printLeft(S);
1610 S += ")";
1611 }
1612};
1613
1614class CallExpr : public Node {
1615 const Node *Callee;
1616 NodeArray Args;
1617
1618public:
1619 CallExpr(const Node *Callee_, NodeArray Args_)
1620 : Node(KCallExpr), Callee(Callee_), Args(Args_) {}
1621
1622 template<typename Fn> void match(Fn F) const { F(Callee, Args); }
1623
1624 void printLeft(OutputStream &S) const override {
1625 Callee->print(S);
1626 S += "(";
1627 Args.printWithComma(S);
1628 S += ")";
1629 }
1630};
1631
1632class NewExpr : public Node {
1633 // new (expr_list) type(init_list)
1634 NodeArray ExprList;
1635 Node *Type;
1636 NodeArray InitList;
1637 bool IsGlobal; // ::operator new ?
1638 bool IsArray; // new[] ?
1639public:
1640 NewExpr(NodeArray ExprList_, Node *Type_, NodeArray InitList_, bool IsGlobal_,
1641 bool IsArray_)
1642 : Node(KNewExpr), ExprList(ExprList_), Type(Type_), InitList(InitList_),
1643 IsGlobal(IsGlobal_), IsArray(IsArray_) {}
1644
1645 template<typename Fn> void match(Fn F) const {
1646 F(ExprList, Type, InitList, IsGlobal, IsArray);
1647 }
1648
1649 void printLeft(OutputStream &S) const override {
1650 if (IsGlobal)
1651 S += "::operator ";
1652 S += "new";
1653 if (IsArray)
1654 S += "[]";
1655 S += ' ';
1656 if (!ExprList.empty()) {
1657 S += "(";
1658 ExprList.printWithComma(S);
1659 S += ")";
1660 }
1661 Type->print(S);
1662 if (!InitList.empty()) {
1663 S += "(";
1664 InitList.printWithComma(S);
1665 S += ")";
1666 }
1667
1668 }
1669};
1670
1671class DeleteExpr : public Node {
1672 Node *Op;
1673 bool IsGlobal;
1674 bool IsArray;
1675
1676public:
1677 DeleteExpr(Node *Op_, bool IsGlobal_, bool IsArray_)
1678 : Node(KDeleteExpr), Op(Op_), IsGlobal(IsGlobal_), IsArray(IsArray_) {}
1679
1680 template<typename Fn> void match(Fn F) const { F(Op, IsGlobal, IsArray); }
1681
1682 void printLeft(OutputStream &S) const override {
1683 if (IsGlobal)
1684 S += "::";
1685 S += "delete";
1686 if (IsArray)
1687 S += "[] ";
1688 Op->print(S);
1689 }
1690};
1691
1692class PrefixExpr : public Node {
1693 StringView Prefix;
1694 Node *Child;
1695
1696public:
1697 PrefixExpr(StringView Prefix_, Node *Child_)
1698 : Node(KPrefixExpr), Prefix(Prefix_), Child(Child_) {}
1699
1700 template<typename Fn> void match(Fn F) const { F(Prefix, Child); }
1701
1702 void printLeft(OutputStream &S) const override {
1703 S += Prefix;
1704 S += "(";
1705 Child->print(S);
1706 S += ")";
1707 }
1708};
1709
1710class FunctionParam : public Node {
1711 StringView Number;
1712
1713public:
1714 FunctionParam(StringView Number_) : Node(KFunctionParam), Number(Number_) {}
1715
1716 template<typename Fn> void match(Fn F) const { F(Number); }
1717
1718 void printLeft(OutputStream &S) const override {
1719 S += "fp";
1720 S += Number;
1721 }
1722};
1723
1724class ConversionExpr : public Node {
1725 const Node *Type;
1726 NodeArray Expressions;
1727
1728public:
1729 ConversionExpr(const Node *Type_, NodeArray Expressions_)
1730 : Node(KConversionExpr), Type(Type_), Expressions(Expressions_) {}
1731
1732 template<typename Fn> void match(Fn F) const { F(Type, Expressions); }
1733
1734 void printLeft(OutputStream &S) const override {
1735 S += "(";
1736 Type->print(S);
1737 S += ")(";
1738 Expressions.printWithComma(S);
1739 S += ")";
1740 }
1741};
1742
1743class InitListExpr : public Node {
1744 const Node *Ty;
1745 NodeArray Inits;
1746public:
1747 InitListExpr(const Node *Ty_, NodeArray Inits_)
1748 : Node(KInitListExpr), Ty(Ty_), Inits(Inits_) {}
1749
1750 template<typename Fn> void match(Fn F) const { F(Ty, Inits); }
1751
1752 void printLeft(OutputStream &S) const override {
1753 if (Ty)
1754 Ty->print(S);
1755 S += '{';
1756 Inits.printWithComma(S);
1757 S += '}';
1758 }
1759};
1760
1761class BracedExpr : public Node {
1762 const Node *Elem;
1763 const Node *Init;
1764 bool IsArray;
1765public:
1766 BracedExpr(const Node *Elem_, const Node *Init_, bool IsArray_)
1767 : Node(KBracedExpr), Elem(Elem_), Init(Init_), IsArray(IsArray_) {}
1768
1769 template<typename Fn> void match(Fn F) const { F(Elem, Init, IsArray); }
1770
1771 void printLeft(OutputStream &S) const override {
1772 if (IsArray) {
1773 S += '[';
1774 Elem->print(S);
1775 S += ']';
1776 } else {
1777 S += '.';
1778 Elem->print(S);
1779 }
1780 if (Init->getKind() != KBracedExpr && Init->getKind() != KBracedRangeExpr)
1781 S += " = ";
1782 Init->print(S);
1783 }
1784};
1785
1786class BracedRangeExpr : public Node {
1787 const Node *First;
1788 const Node *Last;
1789 const Node *Init;
1790public:
1791 BracedRangeExpr(const Node *First_, const Node *Last_, const Node *Init_)
1792 : Node(KBracedRangeExpr), First(First_), Last(Last_), Init(Init_) {}
1793
1794 template<typename Fn> void match(Fn F) const { F(First, Last, Init); }
1795
1796 void printLeft(OutputStream &S) const override {
1797 S += '[';
1798 First->print(S);
1799 S += " ... ";
1800 Last->print(S);
1801 S += ']';
1802 if (Init->getKind() != KBracedExpr && Init->getKind() != KBracedRangeExpr)
1803 S += " = ";
1804 Init->print(S);
1805 }
1806};
1807
1808class FoldExpr : public Node {
1809 const Node *Pack, *Init;
1810 StringView OperatorName;
1811 bool IsLeftFold;
1812
1813public:
1814 FoldExpr(bool IsLeftFold_, StringView OperatorName_, const Node *Pack_,
1815 const Node *Init_)
1816 : Node(KFoldExpr), Pack(Pack_), Init(Init_), OperatorName(OperatorName_),
1817 IsLeftFold(IsLeftFold_) {}
1818
1819 template<typename Fn> void match(Fn F) const {
1820 F(IsLeftFold, OperatorName, Pack, Init);
1821 }
1822
1823 void printLeft(OutputStream &S) const override {
1824 auto PrintPack = [&] {
1825 S += '(';
1826 ParameterPackExpansion(Pack).print(S);
1827 S += ')';
1828 };
1829
1830 S += '(';
1831
1832 if (IsLeftFold) {
1833 // init op ... op pack
1834 if (Init != nullptr) {
1835 Init->print(S);
1836 S += ' ';
1837 S += OperatorName;
1838 S += ' ';
1839 }
1840 // ... op pack
1841 S += "... ";
1842 S += OperatorName;
1843 S += ' ';
1844 PrintPack();
1845 } else { // !IsLeftFold
1846 // pack op ...
1847 PrintPack();
1848 S += ' ';
1849 S += OperatorName;
1850 S += " ...";
1851 // pack op ... op init
1852 if (Init != nullptr) {
1853 S += ' ';
1854 S += OperatorName;
1855 S += ' ';
1856 Init->print(S);
1857 }
1858 }
1859 S += ')';
1860 }
1861};
1862
1863class ThrowExpr : public Node {
1864 const Node *Op;
1865
1866public:
1867 ThrowExpr(const Node *Op_) : Node(KThrowExpr), Op(Op_) {}
1868
1869 template<typename Fn> void match(Fn F) const { F(Op); }
1870
1871 void printLeft(OutputStream &S) const override {
1872 S += "throw ";
1873 Op->print(S);
1874 }
1875};
1876
Erik Pilkingtone8457c62019-06-18 23:34:09 +00001877// MSVC __uuidof extension, generated by clang in -fms-extensions mode.
1878class UUIDOfExpr : public Node {
1879 Node *Operand;
1880public:
1881 UUIDOfExpr(Node *Operand_) : Node(KUUIDOfExpr), Operand(Operand_) {}
1882
1883 template<typename Fn> void match(Fn F) const { F(Operand); }
1884
1885 void printLeft(OutputStream &S) const override {
1886 S << "__uuidof(";
1887 Operand->print(S);
1888 S << ")";
1889 }
1890};
1891
Richard Smithc20d1442018-08-20 20:14:49 +00001892class BoolExpr : public Node {
1893 bool Value;
1894
1895public:
1896 BoolExpr(bool Value_) : Node(KBoolExpr), Value(Value_) {}
1897
1898 template<typename Fn> void match(Fn F) const { F(Value); }
1899
1900 void printLeft(OutputStream &S) const override {
1901 S += Value ? StringView("true") : StringView("false");
1902 }
1903};
1904
1905class IntegerCastExpr : public Node {
1906 // ty(integer)
1907 const Node *Ty;
1908 StringView Integer;
1909
1910public:
1911 IntegerCastExpr(const Node *Ty_, StringView Integer_)
1912 : Node(KIntegerCastExpr), Ty(Ty_), Integer(Integer_) {}
1913
1914 template<typename Fn> void match(Fn F) const { F(Ty, Integer); }
1915
1916 void printLeft(OutputStream &S) const override {
1917 S += "(";
1918 Ty->print(S);
1919 S += ")";
1920 S += Integer;
1921 }
1922};
1923
1924class IntegerLiteral : public Node {
1925 StringView Type;
1926 StringView Value;
1927
1928public:
1929 IntegerLiteral(StringView Type_, StringView Value_)
1930 : Node(KIntegerLiteral), Type(Type_), Value(Value_) {}
1931
1932 template<typename Fn> void match(Fn F) const { F(Type, Value); }
1933
1934 void printLeft(OutputStream &S) const override {
1935 if (Type.size() > 3) {
1936 S += "(";
1937 S += Type;
1938 S += ")";
1939 }
1940
1941 if (Value[0] == 'n') {
1942 S += "-";
1943 S += Value.dropFront(1);
1944 } else
1945 S += Value;
1946
1947 if (Type.size() <= 3)
1948 S += Type;
1949 }
1950};
1951
1952template <class Float> struct FloatData;
1953
1954namespace float_literal_impl {
1955constexpr Node::Kind getFloatLiteralKind(float *) {
1956 return Node::KFloatLiteral;
1957}
1958constexpr Node::Kind getFloatLiteralKind(double *) {
1959 return Node::KDoubleLiteral;
1960}
1961constexpr Node::Kind getFloatLiteralKind(long double *) {
1962 return Node::KLongDoubleLiteral;
1963}
1964}
1965
1966template <class Float> class FloatLiteralImpl : public Node {
1967 const StringView Contents;
1968
1969 static constexpr Kind KindForClass =
1970 float_literal_impl::getFloatLiteralKind((Float *)nullptr);
1971
1972public:
1973 FloatLiteralImpl(StringView Contents_)
1974 : Node(KindForClass), Contents(Contents_) {}
1975
1976 template<typename Fn> void match(Fn F) const { F(Contents); }
1977
1978 void printLeft(OutputStream &s) const override {
1979 const char *first = Contents.begin();
1980 const char *last = Contents.end() + 1;
1981
1982 const size_t N = FloatData<Float>::mangled_size;
1983 if (static_cast<std::size_t>(last - first) > N) {
1984 last = first + N;
1985 union {
1986 Float value;
1987 char buf[sizeof(Float)];
1988 };
1989 const char *t = first;
1990 char *e = buf;
1991 for (; t != last; ++t, ++e) {
1992 unsigned d1 = isdigit(*t) ? static_cast<unsigned>(*t - '0')
1993 : static_cast<unsigned>(*t - 'a' + 10);
1994 ++t;
1995 unsigned d0 = isdigit(*t) ? static_cast<unsigned>(*t - '0')
1996 : static_cast<unsigned>(*t - 'a' + 10);
1997 *e = static_cast<char>((d1 << 4) + d0);
1998 }
1999#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
2000 std::reverse(buf, e);
2001#endif
2002 char num[FloatData<Float>::max_demangled_size] = {0};
2003 int n = snprintf(num, sizeof(num), FloatData<Float>::spec, value);
2004 s += StringView(num, num + n);
2005 }
2006 }
2007};
2008
2009using FloatLiteral = FloatLiteralImpl<float>;
2010using DoubleLiteral = FloatLiteralImpl<double>;
2011using LongDoubleLiteral = FloatLiteralImpl<long double>;
2012
2013/// Visit the node. Calls \c F(P), where \c P is the node cast to the
2014/// appropriate derived class.
2015template<typename Fn>
2016void Node::visit(Fn F) const {
2017 switch (K) {
2018#define CASE(X) case K ## X: return F(static_cast<const X*>(this));
2019 FOR_EACH_NODE_KIND(CASE)
2020#undef CASE
2021 }
2022 assert(0 && "unknown mangling node kind");
2023}
2024
2025/// Determine the kind of a node from its type.
2026template<typename NodeT> struct NodeKind;
2027#define SPECIALIZATION(X) \
2028 template<> struct NodeKind<X> { \
2029 static constexpr Node::Kind Kind = Node::K##X; \
2030 static constexpr const char *name() { return #X; } \
2031 };
2032FOR_EACH_NODE_KIND(SPECIALIZATION)
2033#undef SPECIALIZATION
2034
2035#undef FOR_EACH_NODE_KIND
2036
2037template <class T, size_t N>
2038class PODSmallVector {
2039 static_assert(std::is_pod<T>::value,
2040 "T is required to be a plain old data type");
2041
2042 T* First;
2043 T* Last;
2044 T* Cap;
2045 T Inline[N];
2046
2047 bool isInline() const { return First == Inline; }
2048
2049 void clearInline() {
2050 First = Inline;
2051 Last = Inline;
2052 Cap = Inline + N;
2053 }
2054
2055 void reserve(size_t NewCap) {
2056 size_t S = size();
2057 if (isInline()) {
2058 auto* Tmp = static_cast<T*>(std::malloc(NewCap * sizeof(T)));
2059 if (Tmp == nullptr)
2060 std::terminate();
2061 std::copy(First, Last, Tmp);
2062 First = Tmp;
2063 } else {
2064 First = static_cast<T*>(std::realloc(First, NewCap * sizeof(T)));
2065 if (First == nullptr)
2066 std::terminate();
2067 }
2068 Last = First + S;
2069 Cap = First + NewCap;
2070 }
2071
2072public:
2073 PODSmallVector() : First(Inline), Last(First), Cap(Inline + N) {}
2074
2075 PODSmallVector(const PODSmallVector&) = delete;
2076 PODSmallVector& operator=(const PODSmallVector&) = delete;
2077
2078 PODSmallVector(PODSmallVector&& Other) : PODSmallVector() {
2079 if (Other.isInline()) {
2080 std::copy(Other.begin(), Other.end(), First);
2081 Last = First + Other.size();
2082 Other.clear();
2083 return;
2084 }
2085
2086 First = Other.First;
2087 Last = Other.Last;
2088 Cap = Other.Cap;
2089 Other.clearInline();
2090 }
2091
2092 PODSmallVector& operator=(PODSmallVector&& Other) {
2093 if (Other.isInline()) {
2094 if (!isInline()) {
2095 std::free(First);
2096 clearInline();
2097 }
2098 std::copy(Other.begin(), Other.end(), First);
2099 Last = First + Other.size();
2100 Other.clear();
2101 return *this;
2102 }
2103
2104 if (isInline()) {
2105 First = Other.First;
2106 Last = Other.Last;
2107 Cap = Other.Cap;
2108 Other.clearInline();
2109 return *this;
2110 }
2111
2112 std::swap(First, Other.First);
2113 std::swap(Last, Other.Last);
2114 std::swap(Cap, Other.Cap);
2115 Other.clear();
2116 return *this;
2117 }
2118
2119 void push_back(const T& Elem) {
2120 if (Last == Cap)
2121 reserve(size() * 2);
2122 *Last++ = Elem;
2123 }
2124
2125 void pop_back() {
2126 assert(Last != First && "Popping empty vector!");
2127 --Last;
2128 }
2129
2130 void dropBack(size_t Index) {
2131 assert(Index <= size() && "dropBack() can't expand!");
2132 Last = First + Index;
2133 }
2134
2135 T* begin() { return First; }
2136 T* end() { return Last; }
2137
2138 bool empty() const { return First == Last; }
2139 size_t size() const { return static_cast<size_t>(Last - First); }
2140 T& back() {
2141 assert(Last != First && "Calling back() on empty vector!");
2142 return *(Last - 1);
2143 }
2144 T& operator[](size_t Index) {
2145 assert(Index < size() && "Invalid access!");
2146 return *(begin() + Index);
2147 }
2148 void clear() { Last = First; }
2149
2150 ~PODSmallVector() {
2151 if (!isInline())
2152 std::free(First);
2153 }
2154};
2155
Pavel Labathba825192018-10-16 14:29:14 +00002156template <typename Derived, typename Alloc> struct AbstractManglingParser {
Richard Smithc20d1442018-08-20 20:14:49 +00002157 const char *First;
2158 const char *Last;
2159
2160 // Name stack, this is used by the parser to hold temporary names that were
2161 // parsed. The parser collapses multiple names into new nodes to construct
2162 // the AST. Once the parser is finished, names.size() == 1.
2163 PODSmallVector<Node *, 32> Names;
2164
2165 // Substitution table. Itanium supports name substitutions as a means of
2166 // compression. The string "S42_" refers to the 44nd entry (base-36) in this
2167 // table.
2168 PODSmallVector<Node *, 32> Subs;
2169
2170 // Template parameter table. Like the above, but referenced like "T42_".
2171 // This has a smaller size compared to Subs and Names because it can be
2172 // stored on the stack.
2173 PODSmallVector<Node *, 8> TemplateParams;
2174
2175 // Set of unresolved forward <template-param> references. These can occur in a
2176 // conversion operator's type, and are resolved in the enclosing <encoding>.
2177 PODSmallVector<ForwardTemplateReference *, 4> ForwardTemplateRefs;
2178
Richard Smithc20d1442018-08-20 20:14:49 +00002179 bool TryToParseTemplateArgs = true;
2180 bool PermitForwardTemplateReferences = false;
2181 bool ParsingLambdaParams = false;
2182
2183 Alloc ASTAllocator;
2184
Pavel Labathba825192018-10-16 14:29:14 +00002185 AbstractManglingParser(const char *First_, const char *Last_)
2186 : First(First_), Last(Last_) {}
2187
2188 Derived &getDerived() { return static_cast<Derived &>(*this); }
Richard Smithc20d1442018-08-20 20:14:49 +00002189
2190 void reset(const char *First_, const char *Last_) {
2191 First = First_;
2192 Last = Last_;
2193 Names.clear();
2194 Subs.clear();
2195 TemplateParams.clear();
2196 ParsingLambdaParams = false;
2197 TryToParseTemplateArgs = true;
2198 PermitForwardTemplateReferences = false;
2199 ASTAllocator.reset();
2200 }
2201
Richard Smithb485b352018-08-24 23:30:26 +00002202 template <class T, class... Args> Node *make(Args &&... args) {
Richard Smithc20d1442018-08-20 20:14:49 +00002203 return ASTAllocator.template makeNode<T>(std::forward<Args>(args)...);
2204 }
2205
2206 template <class It> NodeArray makeNodeArray(It begin, It end) {
2207 size_t sz = static_cast<size_t>(end - begin);
2208 void *mem = ASTAllocator.allocateNodeArray(sz);
2209 Node **data = new (mem) Node *[sz];
2210 std::copy(begin, end, data);
2211 return NodeArray(data, sz);
2212 }
2213
2214 NodeArray popTrailingNodeArray(size_t FromPosition) {
2215 assert(FromPosition <= Names.size());
2216 NodeArray res =
2217 makeNodeArray(Names.begin() + (long)FromPosition, Names.end());
2218 Names.dropBack(FromPosition);
2219 return res;
2220 }
2221
2222 bool consumeIf(StringView S) {
2223 if (StringView(First, Last).startsWith(S)) {
2224 First += S.size();
2225 return true;
2226 }
2227 return false;
2228 }
2229
2230 bool consumeIf(char C) {
2231 if (First != Last && *First == C) {
2232 ++First;
2233 return true;
2234 }
2235 return false;
2236 }
2237
2238 char consume() { return First != Last ? *First++ : '\0'; }
2239
2240 char look(unsigned Lookahead = 0) {
2241 if (static_cast<size_t>(Last - First) <= Lookahead)
2242 return '\0';
2243 return First[Lookahead];
2244 }
2245
2246 size_t numLeft() const { return static_cast<size_t>(Last - First); }
2247
2248 StringView parseNumber(bool AllowNegative = false);
2249 Qualifiers parseCVQualifiers();
2250 bool parsePositiveInteger(size_t *Out);
2251 StringView parseBareSourceName();
2252
2253 bool parseSeqId(size_t *Out);
2254 Node *parseSubstitution();
2255 Node *parseTemplateParam();
2256 Node *parseTemplateArgs(bool TagTemplates = false);
2257 Node *parseTemplateArg();
2258
2259 /// Parse the <expr> production.
2260 Node *parseExpr();
2261 Node *parsePrefixExpr(StringView Kind);
2262 Node *parseBinaryExpr(StringView Kind);
2263 Node *parseIntegerLiteral(StringView Lit);
2264 Node *parseExprPrimary();
2265 template <class Float> Node *parseFloatingLiteral();
2266 Node *parseFunctionParam();
2267 Node *parseNewExpr();
2268 Node *parseConversionExpr();
2269 Node *parseBracedExpr();
2270 Node *parseFoldExpr();
2271
2272 /// Parse the <type> production.
2273 Node *parseType();
2274 Node *parseFunctionType();
2275 Node *parseVectorType();
2276 Node *parseDecltype();
2277 Node *parseArrayType();
2278 Node *parsePointerToMemberType();
2279 Node *parseClassEnumType();
2280 Node *parseQualifiedType();
2281
2282 Node *parseEncoding();
2283 bool parseCallOffset();
2284 Node *parseSpecialName();
2285
2286 /// Holds some extra information about a <name> that is being parsed. This
2287 /// information is only pertinent if the <name> refers to an <encoding>.
2288 struct NameState {
2289 bool CtorDtorConversion = false;
2290 bool EndsWithTemplateArgs = false;
2291 Qualifiers CVQualifiers = QualNone;
2292 FunctionRefQual ReferenceQualifier = FrefQualNone;
2293 size_t ForwardTemplateRefsBegin;
2294
Pavel Labathba825192018-10-16 14:29:14 +00002295 NameState(AbstractManglingParser *Enclosing)
Richard Smithc20d1442018-08-20 20:14:49 +00002296 : ForwardTemplateRefsBegin(Enclosing->ForwardTemplateRefs.size()) {}
2297 };
2298
2299 bool resolveForwardTemplateRefs(NameState &State) {
2300 size_t I = State.ForwardTemplateRefsBegin;
2301 size_t E = ForwardTemplateRefs.size();
2302 for (; I < E; ++I) {
2303 size_t Idx = ForwardTemplateRefs[I]->Index;
2304 if (Idx >= TemplateParams.size())
2305 return true;
2306 ForwardTemplateRefs[I]->Ref = TemplateParams[Idx];
2307 }
2308 ForwardTemplateRefs.dropBack(State.ForwardTemplateRefsBegin);
2309 return false;
2310 }
2311
2312 /// Parse the <name> production>
2313 Node *parseName(NameState *State = nullptr);
2314 Node *parseLocalName(NameState *State);
2315 Node *parseOperatorName(NameState *State);
2316 Node *parseUnqualifiedName(NameState *State);
2317 Node *parseUnnamedTypeName(NameState *State);
2318 Node *parseSourceName(NameState *State);
2319 Node *parseUnscopedName(NameState *State);
2320 Node *parseNestedName(NameState *State);
2321 Node *parseCtorDtorName(Node *&SoFar, NameState *State);
2322
2323 Node *parseAbiTags(Node *N);
2324
2325 /// Parse the <unresolved-name> production.
2326 Node *parseUnresolvedName();
2327 Node *parseSimpleId();
2328 Node *parseBaseUnresolvedName();
2329 Node *parseUnresolvedType();
2330 Node *parseDestructorName();
2331
2332 /// Top-level entry point into the parser.
2333 Node *parse();
2334};
2335
2336const char* parse_discriminator(const char* first, const char* last);
2337
2338// <name> ::= <nested-name> // N
2339// ::= <local-name> # See Scope Encoding below // Z
2340// ::= <unscoped-template-name> <template-args>
2341// ::= <unscoped-name>
2342//
2343// <unscoped-template-name> ::= <unscoped-name>
2344// ::= <substitution>
Pavel Labathba825192018-10-16 14:29:14 +00002345template <typename Derived, typename Alloc>
2346Node *AbstractManglingParser<Derived, Alloc>::parseName(NameState *State) {
Richard Smithc20d1442018-08-20 20:14:49 +00002347 consumeIf('L'); // extension
2348
2349 if (look() == 'N')
Pavel Labathba825192018-10-16 14:29:14 +00002350 return getDerived().parseNestedName(State);
Richard Smithc20d1442018-08-20 20:14:49 +00002351 if (look() == 'Z')
Pavel Labathba825192018-10-16 14:29:14 +00002352 return getDerived().parseLocalName(State);
Richard Smithc20d1442018-08-20 20:14:49 +00002353
2354 // ::= <unscoped-template-name> <template-args>
2355 if (look() == 'S' && look(1) != 't') {
Pavel Labathba825192018-10-16 14:29:14 +00002356 Node *S = getDerived().parseSubstitution();
Richard Smithc20d1442018-08-20 20:14:49 +00002357 if (S == nullptr)
2358 return nullptr;
2359 if (look() != 'I')
2360 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00002361 Node *TA = getDerived().parseTemplateArgs(State != nullptr);
Richard Smithc20d1442018-08-20 20:14:49 +00002362 if (TA == nullptr)
2363 return nullptr;
2364 if (State) State->EndsWithTemplateArgs = true;
2365 return make<NameWithTemplateArgs>(S, TA);
2366 }
2367
Pavel Labathba825192018-10-16 14:29:14 +00002368 Node *N = getDerived().parseUnscopedName(State);
Richard Smithc20d1442018-08-20 20:14:49 +00002369 if (N == nullptr)
2370 return nullptr;
2371 // ::= <unscoped-template-name> <template-args>
2372 if (look() == 'I') {
2373 Subs.push_back(N);
Pavel Labathba825192018-10-16 14:29:14 +00002374 Node *TA = getDerived().parseTemplateArgs(State != nullptr);
Richard Smithc20d1442018-08-20 20:14:49 +00002375 if (TA == nullptr)
2376 return nullptr;
2377 if (State) State->EndsWithTemplateArgs = true;
2378 return make<NameWithTemplateArgs>(N, TA);
2379 }
2380 // ::= <unscoped-name>
2381 return N;
2382}
2383
2384// <local-name> := Z <function encoding> E <entity name> [<discriminator>]
2385// := Z <function encoding> E s [<discriminator>]
2386// := Z <function encoding> Ed [ <parameter number> ] _ <entity name>
Pavel Labathba825192018-10-16 14:29:14 +00002387template <typename Derived, typename Alloc>
2388Node *AbstractManglingParser<Derived, Alloc>::parseLocalName(NameState *State) {
Richard Smithc20d1442018-08-20 20:14:49 +00002389 if (!consumeIf('Z'))
2390 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00002391 Node *Encoding = getDerived().parseEncoding();
Richard Smithc20d1442018-08-20 20:14:49 +00002392 if (Encoding == nullptr || !consumeIf('E'))
2393 return nullptr;
2394
2395 if (consumeIf('s')) {
2396 First = parse_discriminator(First, Last);
Richard Smithb485b352018-08-24 23:30:26 +00002397 auto *StringLitName = make<NameType>("string literal");
2398 if (!StringLitName)
2399 return nullptr;
2400 return make<LocalName>(Encoding, StringLitName);
Richard Smithc20d1442018-08-20 20:14:49 +00002401 }
2402
2403 if (consumeIf('d')) {
2404 parseNumber(true);
2405 if (!consumeIf('_'))
2406 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00002407 Node *N = getDerived().parseName(State);
Richard Smithc20d1442018-08-20 20:14:49 +00002408 if (N == nullptr)
2409 return nullptr;
2410 return make<LocalName>(Encoding, N);
2411 }
2412
Pavel Labathba825192018-10-16 14:29:14 +00002413 Node *Entity = getDerived().parseName(State);
Richard Smithc20d1442018-08-20 20:14:49 +00002414 if (Entity == nullptr)
2415 return nullptr;
2416 First = parse_discriminator(First, Last);
2417 return make<LocalName>(Encoding, Entity);
2418}
2419
2420// <unscoped-name> ::= <unqualified-name>
2421// ::= St <unqualified-name> # ::std::
2422// extension ::= StL<unqualified-name>
Pavel Labathba825192018-10-16 14:29:14 +00002423template <typename Derived, typename Alloc>
2424Node *
2425AbstractManglingParser<Derived, Alloc>::parseUnscopedName(NameState *State) {
2426 if (consumeIf("StL") || consumeIf("St")) {
2427 Node *R = getDerived().parseUnqualifiedName(State);
2428 if (R == nullptr)
2429 return nullptr;
2430 return make<StdQualifiedName>(R);
2431 }
2432 return getDerived().parseUnqualifiedName(State);
Richard Smithc20d1442018-08-20 20:14:49 +00002433}
2434
2435// <unqualified-name> ::= <operator-name> [abi-tags]
2436// ::= <ctor-dtor-name>
2437// ::= <source-name>
2438// ::= <unnamed-type-name>
2439// ::= DC <source-name>+ E # structured binding declaration
Pavel Labathba825192018-10-16 14:29:14 +00002440template <typename Derived, typename Alloc>
2441Node *
2442AbstractManglingParser<Derived, Alloc>::parseUnqualifiedName(NameState *State) {
Richard Smithc20d1442018-08-20 20:14:49 +00002443 // <ctor-dtor-name>s are special-cased in parseNestedName().
2444 Node *Result;
2445 if (look() == 'U')
Pavel Labathba825192018-10-16 14:29:14 +00002446 Result = getDerived().parseUnnamedTypeName(State);
Richard Smithc20d1442018-08-20 20:14:49 +00002447 else if (look() >= '1' && look() <= '9')
Pavel Labathba825192018-10-16 14:29:14 +00002448 Result = getDerived().parseSourceName(State);
Richard Smithc20d1442018-08-20 20:14:49 +00002449 else if (consumeIf("DC")) {
2450 size_t BindingsBegin = Names.size();
2451 do {
Pavel Labathba825192018-10-16 14:29:14 +00002452 Node *Binding = getDerived().parseSourceName(State);
Richard Smithc20d1442018-08-20 20:14:49 +00002453 if (Binding == nullptr)
2454 return nullptr;
2455 Names.push_back(Binding);
2456 } while (!consumeIf('E'));
2457 Result = make<StructuredBindingName>(popTrailingNodeArray(BindingsBegin));
2458 } else
Pavel Labathba825192018-10-16 14:29:14 +00002459 Result = getDerived().parseOperatorName(State);
Richard Smithc20d1442018-08-20 20:14:49 +00002460 if (Result != nullptr)
Pavel Labathba825192018-10-16 14:29:14 +00002461 Result = getDerived().parseAbiTags(Result);
Richard Smithc20d1442018-08-20 20:14:49 +00002462 return Result;
2463}
2464
2465// <unnamed-type-name> ::= Ut [<nonnegative number>] _
2466// ::= <closure-type-name>
2467//
2468// <closure-type-name> ::= Ul <lambda-sig> E [ <nonnegative number> ] _
2469//
2470// <lambda-sig> ::= <parameter type>+ # Parameter types or "v" if the lambda has no parameters
Pavel Labathba825192018-10-16 14:29:14 +00002471template <typename Derived, typename Alloc>
2472Node *
2473AbstractManglingParser<Derived, Alloc>::parseUnnamedTypeName(NameState *) {
Richard Smithc20d1442018-08-20 20:14:49 +00002474 if (consumeIf("Ut")) {
2475 StringView Count = parseNumber();
2476 if (!consumeIf('_'))
2477 return nullptr;
2478 return make<UnnamedTypeName>(Count);
2479 }
2480 if (consumeIf("Ul")) {
2481 NodeArray Params;
2482 SwapAndRestore<bool> SwapParams(ParsingLambdaParams, true);
2483 if (!consumeIf("vE")) {
2484 size_t ParamsBegin = Names.size();
2485 do {
Pavel Labathba825192018-10-16 14:29:14 +00002486 Node *P = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00002487 if (P == nullptr)
2488 return nullptr;
2489 Names.push_back(P);
2490 } while (!consumeIf('E'));
2491 Params = popTrailingNodeArray(ParamsBegin);
2492 }
2493 StringView Count = parseNumber();
2494 if (!consumeIf('_'))
2495 return nullptr;
2496 return make<ClosureTypeName>(Params, Count);
2497 }
Erik Pilkington974b6542019-01-17 21:37:51 +00002498 if (consumeIf("Ub")) {
2499 (void)parseNumber();
2500 if (!consumeIf('_'))
2501 return nullptr;
2502 return make<NameType>("'block-literal'");
2503 }
Richard Smithc20d1442018-08-20 20:14:49 +00002504 return nullptr;
2505}
2506
2507// <source-name> ::= <positive length number> <identifier>
Pavel Labathba825192018-10-16 14:29:14 +00002508template <typename Derived, typename Alloc>
2509Node *AbstractManglingParser<Derived, Alloc>::parseSourceName(NameState *) {
Richard Smithc20d1442018-08-20 20:14:49 +00002510 size_t Length = 0;
2511 if (parsePositiveInteger(&Length))
2512 return nullptr;
2513 if (numLeft() < Length || Length == 0)
2514 return nullptr;
2515 StringView Name(First, First + Length);
2516 First += Length;
2517 if (Name.startsWith("_GLOBAL__N"))
2518 return make<NameType>("(anonymous namespace)");
2519 return make<NameType>(Name);
2520}
2521
2522// <operator-name> ::= aa # &&
2523// ::= ad # & (unary)
2524// ::= an # &
2525// ::= aN # &=
2526// ::= aS # =
2527// ::= cl # ()
2528// ::= cm # ,
2529// ::= co # ~
2530// ::= cv <type> # (cast)
2531// ::= da # delete[]
2532// ::= de # * (unary)
2533// ::= dl # delete
2534// ::= dv # /
2535// ::= dV # /=
2536// ::= eo # ^
2537// ::= eO # ^=
2538// ::= eq # ==
2539// ::= ge # >=
2540// ::= gt # >
2541// ::= ix # []
2542// ::= le # <=
2543// ::= li <source-name> # operator ""
2544// ::= ls # <<
2545// ::= lS # <<=
2546// ::= lt # <
2547// ::= mi # -
2548// ::= mI # -=
2549// ::= ml # *
2550// ::= mL # *=
2551// ::= mm # -- (postfix in <expression> context)
2552// ::= na # new[]
2553// ::= ne # !=
2554// ::= ng # - (unary)
2555// ::= nt # !
2556// ::= nw # new
2557// ::= oo # ||
2558// ::= or # |
2559// ::= oR # |=
2560// ::= pm # ->*
2561// ::= pl # +
2562// ::= pL # +=
2563// ::= pp # ++ (postfix in <expression> context)
2564// ::= ps # + (unary)
2565// ::= pt # ->
2566// ::= qu # ?
2567// ::= rm # %
2568// ::= rM # %=
2569// ::= rs # >>
2570// ::= rS # >>=
2571// ::= ss # <=> C++2a
2572// ::= v <digit> <source-name> # vendor extended operator
Pavel Labathba825192018-10-16 14:29:14 +00002573template <typename Derived, typename Alloc>
2574Node *
2575AbstractManglingParser<Derived, Alloc>::parseOperatorName(NameState *State) {
Richard Smithc20d1442018-08-20 20:14:49 +00002576 switch (look()) {
2577 case 'a':
2578 switch (look(1)) {
2579 case 'a':
2580 First += 2;
2581 return make<NameType>("operator&&");
2582 case 'd':
2583 case 'n':
2584 First += 2;
2585 return make<NameType>("operator&");
2586 case 'N':
2587 First += 2;
2588 return make<NameType>("operator&=");
2589 case 'S':
2590 First += 2;
2591 return make<NameType>("operator=");
2592 }
2593 return nullptr;
2594 case 'c':
2595 switch (look(1)) {
2596 case 'l':
2597 First += 2;
2598 return make<NameType>("operator()");
2599 case 'm':
2600 First += 2;
2601 return make<NameType>("operator,");
2602 case 'o':
2603 First += 2;
2604 return make<NameType>("operator~");
2605 // ::= cv <type> # (cast)
2606 case 'v': {
2607 First += 2;
2608 SwapAndRestore<bool> SaveTemplate(TryToParseTemplateArgs, false);
2609 // If we're parsing an encoding, State != nullptr and the conversion
2610 // operators' <type> could have a <template-param> that refers to some
2611 // <template-arg>s further ahead in the mangled name.
2612 SwapAndRestore<bool> SavePermit(PermitForwardTemplateReferences,
2613 PermitForwardTemplateReferences ||
2614 State != nullptr);
Pavel Labathba825192018-10-16 14:29:14 +00002615 Node *Ty = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00002616 if (Ty == nullptr)
2617 return nullptr;
2618 if (State) State->CtorDtorConversion = true;
2619 return make<ConversionOperatorType>(Ty);
2620 }
2621 }
2622 return nullptr;
2623 case 'd':
2624 switch (look(1)) {
2625 case 'a':
2626 First += 2;
2627 return make<NameType>("operator delete[]");
2628 case 'e':
2629 First += 2;
2630 return make<NameType>("operator*");
2631 case 'l':
2632 First += 2;
2633 return make<NameType>("operator delete");
2634 case 'v':
2635 First += 2;
2636 return make<NameType>("operator/");
2637 case 'V':
2638 First += 2;
2639 return make<NameType>("operator/=");
2640 }
2641 return nullptr;
2642 case 'e':
2643 switch (look(1)) {
2644 case 'o':
2645 First += 2;
2646 return make<NameType>("operator^");
2647 case 'O':
2648 First += 2;
2649 return make<NameType>("operator^=");
2650 case 'q':
2651 First += 2;
2652 return make<NameType>("operator==");
2653 }
2654 return nullptr;
2655 case 'g':
2656 switch (look(1)) {
2657 case 'e':
2658 First += 2;
2659 return make<NameType>("operator>=");
2660 case 't':
2661 First += 2;
2662 return make<NameType>("operator>");
2663 }
2664 return nullptr;
2665 case 'i':
2666 if (look(1) == 'x') {
2667 First += 2;
2668 return make<NameType>("operator[]");
2669 }
2670 return nullptr;
2671 case 'l':
2672 switch (look(1)) {
2673 case 'e':
2674 First += 2;
2675 return make<NameType>("operator<=");
2676 // ::= li <source-name> # operator ""
2677 case 'i': {
2678 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00002679 Node *SN = getDerived().parseSourceName(State);
Richard Smithc20d1442018-08-20 20:14:49 +00002680 if (SN == nullptr)
2681 return nullptr;
2682 return make<LiteralOperator>(SN);
2683 }
2684 case 's':
2685 First += 2;
2686 return make<NameType>("operator<<");
2687 case 'S':
2688 First += 2;
2689 return make<NameType>("operator<<=");
2690 case 't':
2691 First += 2;
2692 return make<NameType>("operator<");
2693 }
2694 return nullptr;
2695 case 'm':
2696 switch (look(1)) {
2697 case 'i':
2698 First += 2;
2699 return make<NameType>("operator-");
2700 case 'I':
2701 First += 2;
2702 return make<NameType>("operator-=");
2703 case 'l':
2704 First += 2;
2705 return make<NameType>("operator*");
2706 case 'L':
2707 First += 2;
2708 return make<NameType>("operator*=");
2709 case 'm':
2710 First += 2;
2711 return make<NameType>("operator--");
2712 }
2713 return nullptr;
2714 case 'n':
2715 switch (look(1)) {
2716 case 'a':
2717 First += 2;
2718 return make<NameType>("operator new[]");
2719 case 'e':
2720 First += 2;
2721 return make<NameType>("operator!=");
2722 case 'g':
2723 First += 2;
2724 return make<NameType>("operator-");
2725 case 't':
2726 First += 2;
2727 return make<NameType>("operator!");
2728 case 'w':
2729 First += 2;
2730 return make<NameType>("operator new");
2731 }
2732 return nullptr;
2733 case 'o':
2734 switch (look(1)) {
2735 case 'o':
2736 First += 2;
2737 return make<NameType>("operator||");
2738 case 'r':
2739 First += 2;
2740 return make<NameType>("operator|");
2741 case 'R':
2742 First += 2;
2743 return make<NameType>("operator|=");
2744 }
2745 return nullptr;
2746 case 'p':
2747 switch (look(1)) {
2748 case 'm':
2749 First += 2;
2750 return make<NameType>("operator->*");
2751 case 'l':
2752 First += 2;
2753 return make<NameType>("operator+");
2754 case 'L':
2755 First += 2;
2756 return make<NameType>("operator+=");
2757 case 'p':
2758 First += 2;
2759 return make<NameType>("operator++");
2760 case 's':
2761 First += 2;
2762 return make<NameType>("operator+");
2763 case 't':
2764 First += 2;
2765 return make<NameType>("operator->");
2766 }
2767 return nullptr;
2768 case 'q':
2769 if (look(1) == 'u') {
2770 First += 2;
2771 return make<NameType>("operator?");
2772 }
2773 return nullptr;
2774 case 'r':
2775 switch (look(1)) {
2776 case 'm':
2777 First += 2;
2778 return make<NameType>("operator%");
2779 case 'M':
2780 First += 2;
2781 return make<NameType>("operator%=");
2782 case 's':
2783 First += 2;
2784 return make<NameType>("operator>>");
2785 case 'S':
2786 First += 2;
2787 return make<NameType>("operator>>=");
2788 }
2789 return nullptr;
2790 case 's':
2791 if (look(1) == 's') {
2792 First += 2;
2793 return make<NameType>("operator<=>");
2794 }
2795 return nullptr;
2796 // ::= v <digit> <source-name> # vendor extended operator
2797 case 'v':
2798 if (std::isdigit(look(1))) {
2799 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00002800 Node *SN = getDerived().parseSourceName(State);
Richard Smithc20d1442018-08-20 20:14:49 +00002801 if (SN == nullptr)
2802 return nullptr;
2803 return make<ConversionOperatorType>(SN);
2804 }
2805 return nullptr;
2806 }
2807 return nullptr;
2808}
2809
2810// <ctor-dtor-name> ::= C1 # complete object constructor
2811// ::= C2 # base object constructor
2812// ::= C3 # complete object allocating constructor
Nico Weber29294792019-04-03 23:14:33 +00002813// extension ::= C4 # gcc old-style "[unified]" constructor
2814// extension ::= C5 # the COMDAT used for ctors
Richard Smithc20d1442018-08-20 20:14:49 +00002815// ::= D0 # deleting destructor
2816// ::= D1 # complete object destructor
2817// ::= D2 # base object destructor
Nico Weber29294792019-04-03 23:14:33 +00002818// extension ::= D4 # gcc old-style "[unified]" destructor
2819// extension ::= D5 # the COMDAT used for dtors
Pavel Labathba825192018-10-16 14:29:14 +00002820template <typename Derived, typename Alloc>
2821Node *
2822AbstractManglingParser<Derived, Alloc>::parseCtorDtorName(Node *&SoFar,
2823 NameState *State) {
Richard Smithc20d1442018-08-20 20:14:49 +00002824 if (SoFar->getKind() == Node::KSpecialSubstitution) {
2825 auto SSK = static_cast<SpecialSubstitution *>(SoFar)->SSK;
2826 switch (SSK) {
2827 case SpecialSubKind::string:
2828 case SpecialSubKind::istream:
2829 case SpecialSubKind::ostream:
2830 case SpecialSubKind::iostream:
2831 SoFar = make<ExpandedSpecialSubstitution>(SSK);
Richard Smithb485b352018-08-24 23:30:26 +00002832 if (!SoFar)
2833 return nullptr;
Reid Klecknere76aabe2018-11-01 18:24:03 +00002834 break;
Richard Smithc20d1442018-08-20 20:14:49 +00002835 default:
2836 break;
2837 }
2838 }
2839
2840 if (consumeIf('C')) {
2841 bool IsInherited = consumeIf('I');
Nico Weber29294792019-04-03 23:14:33 +00002842 if (look() != '1' && look() != '2' && look() != '3' && look() != '4' &&
2843 look() != '5')
Richard Smithc20d1442018-08-20 20:14:49 +00002844 return nullptr;
Pavel Labathf4e67eb2018-10-10 08:39:16 +00002845 int Variant = look() - '0';
Richard Smithc20d1442018-08-20 20:14:49 +00002846 ++First;
2847 if (State) State->CtorDtorConversion = true;
2848 if (IsInherited) {
Pavel Labathba825192018-10-16 14:29:14 +00002849 if (getDerived().parseName(State) == nullptr)
Richard Smithc20d1442018-08-20 20:14:49 +00002850 return nullptr;
2851 }
Nico Weber29294792019-04-03 23:14:33 +00002852 return make<CtorDtorName>(SoFar, /*IsDtor=*/false, Variant);
Richard Smithc20d1442018-08-20 20:14:49 +00002853 }
2854
Nico Weber29294792019-04-03 23:14:33 +00002855 if (look() == 'D' && (look(1) == '0' || look(1) == '1' || look(1) == '2' ||
2856 look(1) == '4' || look(1) == '5')) {
Pavel Labathf4e67eb2018-10-10 08:39:16 +00002857 int Variant = look(1) - '0';
Richard Smithc20d1442018-08-20 20:14:49 +00002858 First += 2;
2859 if (State) State->CtorDtorConversion = true;
Nico Weber29294792019-04-03 23:14:33 +00002860 return make<CtorDtorName>(SoFar, /*IsDtor=*/true, Variant);
Richard Smithc20d1442018-08-20 20:14:49 +00002861 }
2862
2863 return nullptr;
2864}
2865
2866// <nested-name> ::= N [<CV-Qualifiers>] [<ref-qualifier>] <prefix> <unqualified-name> E
2867// ::= N [<CV-Qualifiers>] [<ref-qualifier>] <template-prefix> <template-args> E
2868//
2869// <prefix> ::= <prefix> <unqualified-name>
2870// ::= <template-prefix> <template-args>
2871// ::= <template-param>
2872// ::= <decltype>
2873// ::= # empty
2874// ::= <substitution>
2875// ::= <prefix> <data-member-prefix>
2876// extension ::= L
2877//
2878// <data-member-prefix> := <member source-name> [<template-args>] M
2879//
2880// <template-prefix> ::= <prefix> <template unqualified-name>
2881// ::= <template-param>
2882// ::= <substitution>
Pavel Labathba825192018-10-16 14:29:14 +00002883template <typename Derived, typename Alloc>
2884Node *
2885AbstractManglingParser<Derived, Alloc>::parseNestedName(NameState *State) {
Richard Smithc20d1442018-08-20 20:14:49 +00002886 if (!consumeIf('N'))
2887 return nullptr;
2888
2889 Qualifiers CVTmp = parseCVQualifiers();
2890 if (State) State->CVQualifiers = CVTmp;
2891
2892 if (consumeIf('O')) {
2893 if (State) State->ReferenceQualifier = FrefQualRValue;
2894 } else if (consumeIf('R')) {
2895 if (State) State->ReferenceQualifier = FrefQualLValue;
2896 } else
2897 if (State) State->ReferenceQualifier = FrefQualNone;
2898
2899 Node *SoFar = nullptr;
2900 auto PushComponent = [&](Node *Comp) {
Richard Smithb485b352018-08-24 23:30:26 +00002901 if (!Comp) return false;
Richard Smithc20d1442018-08-20 20:14:49 +00002902 if (SoFar) SoFar = make<NestedName>(SoFar, Comp);
2903 else SoFar = Comp;
2904 if (State) State->EndsWithTemplateArgs = false;
Richard Smithb485b352018-08-24 23:30:26 +00002905 return SoFar != nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00002906 };
2907
Richard Smithb485b352018-08-24 23:30:26 +00002908 if (consumeIf("St")) {
Richard Smithc20d1442018-08-20 20:14:49 +00002909 SoFar = make<NameType>("std");
Richard Smithb485b352018-08-24 23:30:26 +00002910 if (!SoFar)
2911 return nullptr;
2912 }
Richard Smithc20d1442018-08-20 20:14:49 +00002913
2914 while (!consumeIf('E')) {
2915 consumeIf('L'); // extension
2916
2917 // <data-member-prefix> := <member source-name> [<template-args>] M
2918 if (consumeIf('M')) {
2919 if (SoFar == nullptr)
2920 return nullptr;
2921 continue;
2922 }
2923
2924 // ::= <template-param>
2925 if (look() == 'T') {
Pavel Labathba825192018-10-16 14:29:14 +00002926 if (!PushComponent(getDerived().parseTemplateParam()))
Richard Smithc20d1442018-08-20 20:14:49 +00002927 return nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00002928 Subs.push_back(SoFar);
2929 continue;
2930 }
2931
2932 // ::= <template-prefix> <template-args>
2933 if (look() == 'I') {
Pavel Labathba825192018-10-16 14:29:14 +00002934 Node *TA = getDerived().parseTemplateArgs(State != nullptr);
Richard Smithc20d1442018-08-20 20:14:49 +00002935 if (TA == nullptr || SoFar == nullptr)
2936 return nullptr;
2937 SoFar = make<NameWithTemplateArgs>(SoFar, TA);
Richard Smithb485b352018-08-24 23:30:26 +00002938 if (!SoFar)
2939 return nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00002940 if (State) State->EndsWithTemplateArgs = true;
2941 Subs.push_back(SoFar);
2942 continue;
2943 }
2944
2945 // ::= <decltype>
2946 if (look() == 'D' && (look(1) == 't' || look(1) == 'T')) {
Pavel Labathba825192018-10-16 14:29:14 +00002947 if (!PushComponent(getDerived().parseDecltype()))
Richard Smithc20d1442018-08-20 20:14:49 +00002948 return nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00002949 Subs.push_back(SoFar);
2950 continue;
2951 }
2952
2953 // ::= <substitution>
2954 if (look() == 'S' && look(1) != 't') {
Pavel Labathba825192018-10-16 14:29:14 +00002955 Node *S = getDerived().parseSubstitution();
Richard Smithb485b352018-08-24 23:30:26 +00002956 if (!PushComponent(S))
Richard Smithc20d1442018-08-20 20:14:49 +00002957 return nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00002958 if (SoFar != S)
2959 Subs.push_back(S);
2960 continue;
2961 }
2962
2963 // Parse an <unqualified-name> thats actually a <ctor-dtor-name>.
2964 if (look() == 'C' || (look() == 'D' && look(1) != 'C')) {
2965 if (SoFar == nullptr)
2966 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00002967 if (!PushComponent(getDerived().parseCtorDtorName(SoFar, State)))
Richard Smithc20d1442018-08-20 20:14:49 +00002968 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00002969 SoFar = getDerived().parseAbiTags(SoFar);
Richard Smithc20d1442018-08-20 20:14:49 +00002970 if (SoFar == nullptr)
2971 return nullptr;
2972 Subs.push_back(SoFar);
2973 continue;
2974 }
2975
2976 // ::= <prefix> <unqualified-name>
Pavel Labathba825192018-10-16 14:29:14 +00002977 if (!PushComponent(getDerived().parseUnqualifiedName(State)))
Richard Smithc20d1442018-08-20 20:14:49 +00002978 return nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00002979 Subs.push_back(SoFar);
2980 }
2981
2982 if (SoFar == nullptr || Subs.empty())
2983 return nullptr;
2984
2985 Subs.pop_back();
2986 return SoFar;
2987}
2988
2989// <simple-id> ::= <source-name> [ <template-args> ]
Pavel Labathba825192018-10-16 14:29:14 +00002990template <typename Derived, typename Alloc>
2991Node *AbstractManglingParser<Derived, Alloc>::parseSimpleId() {
2992 Node *SN = getDerived().parseSourceName(/*NameState=*/nullptr);
Richard Smithc20d1442018-08-20 20:14:49 +00002993 if (SN == nullptr)
2994 return nullptr;
2995 if (look() == 'I') {
Pavel Labathba825192018-10-16 14:29:14 +00002996 Node *TA = getDerived().parseTemplateArgs();
Richard Smithc20d1442018-08-20 20:14:49 +00002997 if (TA == nullptr)
2998 return nullptr;
2999 return make<NameWithTemplateArgs>(SN, TA);
3000 }
3001 return SN;
3002}
3003
3004// <destructor-name> ::= <unresolved-type> # e.g., ~T or ~decltype(f())
3005// ::= <simple-id> # e.g., ~A<2*N>
Pavel Labathba825192018-10-16 14:29:14 +00003006template <typename Derived, typename Alloc>
3007Node *AbstractManglingParser<Derived, Alloc>::parseDestructorName() {
Richard Smithc20d1442018-08-20 20:14:49 +00003008 Node *Result;
3009 if (std::isdigit(look()))
Pavel Labathba825192018-10-16 14:29:14 +00003010 Result = getDerived().parseSimpleId();
Richard Smithc20d1442018-08-20 20:14:49 +00003011 else
Pavel Labathba825192018-10-16 14:29:14 +00003012 Result = getDerived().parseUnresolvedType();
Richard Smithc20d1442018-08-20 20:14:49 +00003013 if (Result == nullptr)
3014 return nullptr;
3015 return make<DtorName>(Result);
3016}
3017
3018// <unresolved-type> ::= <template-param>
3019// ::= <decltype>
3020// ::= <substitution>
Pavel Labathba825192018-10-16 14:29:14 +00003021template <typename Derived, typename Alloc>
3022Node *AbstractManglingParser<Derived, Alloc>::parseUnresolvedType() {
Richard Smithc20d1442018-08-20 20:14:49 +00003023 if (look() == 'T') {
Pavel Labathba825192018-10-16 14:29:14 +00003024 Node *TP = getDerived().parseTemplateParam();
Richard Smithc20d1442018-08-20 20:14:49 +00003025 if (TP == nullptr)
3026 return nullptr;
3027 Subs.push_back(TP);
3028 return TP;
3029 }
3030 if (look() == 'D') {
Pavel Labathba825192018-10-16 14:29:14 +00003031 Node *DT = getDerived().parseDecltype();
Richard Smithc20d1442018-08-20 20:14:49 +00003032 if (DT == nullptr)
3033 return nullptr;
3034 Subs.push_back(DT);
3035 return DT;
3036 }
Pavel Labathba825192018-10-16 14:29:14 +00003037 return getDerived().parseSubstitution();
Richard Smithc20d1442018-08-20 20:14:49 +00003038}
3039
3040// <base-unresolved-name> ::= <simple-id> # unresolved name
3041// extension ::= <operator-name> # unresolved operator-function-id
3042// extension ::= <operator-name> <template-args> # unresolved operator template-id
3043// ::= on <operator-name> # unresolved operator-function-id
3044// ::= on <operator-name> <template-args> # unresolved operator template-id
3045// ::= dn <destructor-name> # destructor or pseudo-destructor;
3046// # e.g. ~X or ~X<N-1>
Pavel Labathba825192018-10-16 14:29:14 +00003047template <typename Derived, typename Alloc>
3048Node *AbstractManglingParser<Derived, Alloc>::parseBaseUnresolvedName() {
Richard Smithc20d1442018-08-20 20:14:49 +00003049 if (std::isdigit(look()))
Pavel Labathba825192018-10-16 14:29:14 +00003050 return getDerived().parseSimpleId();
Richard Smithc20d1442018-08-20 20:14:49 +00003051
3052 if (consumeIf("dn"))
Pavel Labathba825192018-10-16 14:29:14 +00003053 return getDerived().parseDestructorName();
Richard Smithc20d1442018-08-20 20:14:49 +00003054
3055 consumeIf("on");
3056
Pavel Labathba825192018-10-16 14:29:14 +00003057 Node *Oper = getDerived().parseOperatorName(/*NameState=*/nullptr);
Richard Smithc20d1442018-08-20 20:14:49 +00003058 if (Oper == nullptr)
3059 return nullptr;
3060 if (look() == 'I') {
Pavel Labathba825192018-10-16 14:29:14 +00003061 Node *TA = getDerived().parseTemplateArgs();
Richard Smithc20d1442018-08-20 20:14:49 +00003062 if (TA == nullptr)
3063 return nullptr;
3064 return make<NameWithTemplateArgs>(Oper, TA);
3065 }
3066 return Oper;
3067}
3068
3069// <unresolved-name>
3070// extension ::= srN <unresolved-type> [<template-args>] <unresolved-qualifier-level>* E <base-unresolved-name>
3071// ::= [gs] <base-unresolved-name> # x or (with "gs") ::x
3072// ::= [gs] sr <unresolved-qualifier-level>+ E <base-unresolved-name>
3073// # A::x, N::y, A<T>::z; "gs" means leading "::"
3074// ::= sr <unresolved-type> <base-unresolved-name> # T::x / decltype(p)::x
3075// extension ::= sr <unresolved-type> <template-args> <base-unresolved-name>
3076// # T::N::x /decltype(p)::N::x
3077// (ignored) ::= srN <unresolved-type> <unresolved-qualifier-level>+ E <base-unresolved-name>
3078//
3079// <unresolved-qualifier-level> ::= <simple-id>
Pavel Labathba825192018-10-16 14:29:14 +00003080template <typename Derived, typename Alloc>
3081Node *AbstractManglingParser<Derived, Alloc>::parseUnresolvedName() {
Richard Smithc20d1442018-08-20 20:14:49 +00003082 Node *SoFar = nullptr;
3083
3084 // srN <unresolved-type> [<template-args>] <unresolved-qualifier-level>* E <base-unresolved-name>
3085 // srN <unresolved-type> <unresolved-qualifier-level>+ E <base-unresolved-name>
3086 if (consumeIf("srN")) {
Pavel Labathba825192018-10-16 14:29:14 +00003087 SoFar = getDerived().parseUnresolvedType();
Richard Smithc20d1442018-08-20 20:14:49 +00003088 if (SoFar == nullptr)
3089 return nullptr;
3090
3091 if (look() == 'I') {
Pavel Labathba825192018-10-16 14:29:14 +00003092 Node *TA = getDerived().parseTemplateArgs();
Richard Smithc20d1442018-08-20 20:14:49 +00003093 if (TA == nullptr)
3094 return nullptr;
3095 SoFar = make<NameWithTemplateArgs>(SoFar, TA);
Richard Smithb485b352018-08-24 23:30:26 +00003096 if (!SoFar)
3097 return nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00003098 }
3099
3100 while (!consumeIf('E')) {
Pavel Labathba825192018-10-16 14:29:14 +00003101 Node *Qual = getDerived().parseSimpleId();
Richard Smithc20d1442018-08-20 20:14:49 +00003102 if (Qual == nullptr)
3103 return nullptr;
3104 SoFar = make<QualifiedName>(SoFar, Qual);
Richard Smithb485b352018-08-24 23:30:26 +00003105 if (!SoFar)
3106 return nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00003107 }
3108
Pavel Labathba825192018-10-16 14:29:14 +00003109 Node *Base = getDerived().parseBaseUnresolvedName();
Richard Smithc20d1442018-08-20 20:14:49 +00003110 if (Base == nullptr)
3111 return nullptr;
3112 return make<QualifiedName>(SoFar, Base);
3113 }
3114
3115 bool Global = consumeIf("gs");
3116
3117 // [gs] <base-unresolved-name> # x or (with "gs") ::x
3118 if (!consumeIf("sr")) {
Pavel Labathba825192018-10-16 14:29:14 +00003119 SoFar = getDerived().parseBaseUnresolvedName();
Richard Smithc20d1442018-08-20 20:14:49 +00003120 if (SoFar == nullptr)
3121 return nullptr;
3122 if (Global)
3123 SoFar = make<GlobalQualifiedName>(SoFar);
3124 return SoFar;
3125 }
3126
3127 // [gs] sr <unresolved-qualifier-level>+ E <base-unresolved-name>
3128 if (std::isdigit(look())) {
3129 do {
Pavel Labathba825192018-10-16 14:29:14 +00003130 Node *Qual = getDerived().parseSimpleId();
Richard Smithc20d1442018-08-20 20:14:49 +00003131 if (Qual == nullptr)
3132 return nullptr;
3133 if (SoFar)
3134 SoFar = make<QualifiedName>(SoFar, Qual);
3135 else if (Global)
3136 SoFar = make<GlobalQualifiedName>(Qual);
3137 else
3138 SoFar = Qual;
Richard Smithb485b352018-08-24 23:30:26 +00003139 if (!SoFar)
3140 return nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00003141 } while (!consumeIf('E'));
3142 }
3143 // sr <unresolved-type> <base-unresolved-name>
3144 // sr <unresolved-type> <template-args> <base-unresolved-name>
3145 else {
Pavel Labathba825192018-10-16 14:29:14 +00003146 SoFar = getDerived().parseUnresolvedType();
Richard Smithc20d1442018-08-20 20:14:49 +00003147 if (SoFar == nullptr)
3148 return nullptr;
3149
3150 if (look() == 'I') {
Pavel Labathba825192018-10-16 14:29:14 +00003151 Node *TA = getDerived().parseTemplateArgs();
Richard Smithc20d1442018-08-20 20:14:49 +00003152 if (TA == nullptr)
3153 return nullptr;
3154 SoFar = make<NameWithTemplateArgs>(SoFar, TA);
Richard Smithb485b352018-08-24 23:30:26 +00003155 if (!SoFar)
3156 return nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00003157 }
3158 }
3159
3160 assert(SoFar != nullptr);
3161
Pavel Labathba825192018-10-16 14:29:14 +00003162 Node *Base = getDerived().parseBaseUnresolvedName();
Richard Smithc20d1442018-08-20 20:14:49 +00003163 if (Base == nullptr)
3164 return nullptr;
3165 return make<QualifiedName>(SoFar, Base);
3166}
3167
3168// <abi-tags> ::= <abi-tag> [<abi-tags>]
3169// <abi-tag> ::= B <source-name>
Pavel Labathba825192018-10-16 14:29:14 +00003170template <typename Derived, typename Alloc>
3171Node *AbstractManglingParser<Derived, Alloc>::parseAbiTags(Node *N) {
Richard Smithc20d1442018-08-20 20:14:49 +00003172 while (consumeIf('B')) {
3173 StringView SN = parseBareSourceName();
3174 if (SN.empty())
3175 return nullptr;
3176 N = make<AbiTagAttr>(N, SN);
Richard Smithb485b352018-08-24 23:30:26 +00003177 if (!N)
3178 return nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00003179 }
3180 return N;
3181}
3182
3183// <number> ::= [n] <non-negative decimal integer>
Pavel Labathba825192018-10-16 14:29:14 +00003184template <typename Alloc, typename Derived>
3185StringView
3186AbstractManglingParser<Alloc, Derived>::parseNumber(bool AllowNegative) {
Richard Smithc20d1442018-08-20 20:14:49 +00003187 const char *Tmp = First;
3188 if (AllowNegative)
3189 consumeIf('n');
3190 if (numLeft() == 0 || !std::isdigit(*First))
3191 return StringView();
3192 while (numLeft() != 0 && std::isdigit(*First))
3193 ++First;
3194 return StringView(Tmp, First);
3195}
3196
3197// <positive length number> ::= [0-9]*
Pavel Labathba825192018-10-16 14:29:14 +00003198template <typename Alloc, typename Derived>
3199bool AbstractManglingParser<Alloc, Derived>::parsePositiveInteger(size_t *Out) {
Richard Smithc20d1442018-08-20 20:14:49 +00003200 *Out = 0;
3201 if (look() < '0' || look() > '9')
3202 return true;
3203 while (look() >= '0' && look() <= '9') {
3204 *Out *= 10;
3205 *Out += static_cast<size_t>(consume() - '0');
3206 }
3207 return false;
3208}
3209
Pavel Labathba825192018-10-16 14:29:14 +00003210template <typename Alloc, typename Derived>
3211StringView AbstractManglingParser<Alloc, Derived>::parseBareSourceName() {
Richard Smithc20d1442018-08-20 20:14:49 +00003212 size_t Int = 0;
3213 if (parsePositiveInteger(&Int) || numLeft() < Int)
3214 return StringView();
3215 StringView R(First, First + Int);
3216 First += Int;
3217 return R;
3218}
3219
3220// <function-type> ::= [<CV-qualifiers>] [<exception-spec>] [Dx] F [Y] <bare-function-type> [<ref-qualifier>] E
3221//
3222// <exception-spec> ::= Do # non-throwing exception-specification (e.g., noexcept, throw())
3223// ::= DO <expression> E # computed (instantiation-dependent) noexcept
3224// ::= Dw <type>+ E # dynamic exception specification with instantiation-dependent types
3225//
3226// <ref-qualifier> ::= R # & ref-qualifier
3227// <ref-qualifier> ::= O # && ref-qualifier
Pavel Labathba825192018-10-16 14:29:14 +00003228template <typename Derived, typename Alloc>
3229Node *AbstractManglingParser<Derived, Alloc>::parseFunctionType() {
Richard Smithc20d1442018-08-20 20:14:49 +00003230 Qualifiers CVQuals = parseCVQualifiers();
3231
3232 Node *ExceptionSpec = nullptr;
3233 if (consumeIf("Do")) {
3234 ExceptionSpec = make<NameType>("noexcept");
Richard Smithb485b352018-08-24 23:30:26 +00003235 if (!ExceptionSpec)
3236 return nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00003237 } else if (consumeIf("DO")) {
Pavel Labathba825192018-10-16 14:29:14 +00003238 Node *E = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00003239 if (E == nullptr || !consumeIf('E'))
3240 return nullptr;
3241 ExceptionSpec = make<NoexceptSpec>(E);
Richard Smithb485b352018-08-24 23:30:26 +00003242 if (!ExceptionSpec)
3243 return nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00003244 } else if (consumeIf("Dw")) {
3245 size_t SpecsBegin = Names.size();
3246 while (!consumeIf('E')) {
Pavel Labathba825192018-10-16 14:29:14 +00003247 Node *T = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00003248 if (T == nullptr)
3249 return nullptr;
3250 Names.push_back(T);
3251 }
3252 ExceptionSpec =
3253 make<DynamicExceptionSpec>(popTrailingNodeArray(SpecsBegin));
Richard Smithb485b352018-08-24 23:30:26 +00003254 if (!ExceptionSpec)
3255 return nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00003256 }
3257
3258 consumeIf("Dx"); // transaction safe
3259
3260 if (!consumeIf('F'))
3261 return nullptr;
3262 consumeIf('Y'); // extern "C"
Pavel Labathba825192018-10-16 14:29:14 +00003263 Node *ReturnType = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00003264 if (ReturnType == nullptr)
3265 return nullptr;
3266
3267 FunctionRefQual ReferenceQualifier = FrefQualNone;
3268 size_t ParamsBegin = Names.size();
3269 while (true) {
3270 if (consumeIf('E'))
3271 break;
3272 if (consumeIf('v'))
3273 continue;
3274 if (consumeIf("RE")) {
3275 ReferenceQualifier = FrefQualLValue;
3276 break;
3277 }
3278 if (consumeIf("OE")) {
3279 ReferenceQualifier = FrefQualRValue;
3280 break;
3281 }
Pavel Labathba825192018-10-16 14:29:14 +00003282 Node *T = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00003283 if (T == nullptr)
3284 return nullptr;
3285 Names.push_back(T);
3286 }
3287
3288 NodeArray Params = popTrailingNodeArray(ParamsBegin);
3289 return make<FunctionType>(ReturnType, Params, CVQuals,
3290 ReferenceQualifier, ExceptionSpec);
3291}
3292
3293// extension:
3294// <vector-type> ::= Dv <positive dimension number> _ <extended element type>
3295// ::= Dv [<dimension expression>] _ <element type>
3296// <extended element type> ::= <element type>
3297// ::= p # AltiVec vector pixel
Pavel Labathba825192018-10-16 14:29:14 +00003298template <typename Derived, typename Alloc>
3299Node *AbstractManglingParser<Derived, Alloc>::parseVectorType() {
Richard Smithc20d1442018-08-20 20:14:49 +00003300 if (!consumeIf("Dv"))
3301 return nullptr;
3302 if (look() >= '1' && look() <= '9') {
3303 StringView DimensionNumber = parseNumber();
3304 if (!consumeIf('_'))
3305 return nullptr;
3306 if (consumeIf('p'))
3307 return make<PixelVectorType>(DimensionNumber);
Pavel Labathba825192018-10-16 14:29:14 +00003308 Node *ElemType = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00003309 if (ElemType == nullptr)
3310 return nullptr;
3311 return make<VectorType>(ElemType, DimensionNumber);
3312 }
3313
3314 if (!consumeIf('_')) {
Pavel Labathba825192018-10-16 14:29:14 +00003315 Node *DimExpr = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00003316 if (!DimExpr)
3317 return nullptr;
3318 if (!consumeIf('_'))
3319 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00003320 Node *ElemType = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00003321 if (!ElemType)
3322 return nullptr;
3323 return make<VectorType>(ElemType, DimExpr);
3324 }
Pavel Labathba825192018-10-16 14:29:14 +00003325 Node *ElemType = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00003326 if (!ElemType)
3327 return nullptr;
3328 return make<VectorType>(ElemType, StringView());
3329}
3330
3331// <decltype> ::= Dt <expression> E # decltype of an id-expression or class member access (C++0x)
3332// ::= DT <expression> E # decltype of an expression (C++0x)
Pavel Labathba825192018-10-16 14:29:14 +00003333template <typename Derived, typename Alloc>
3334Node *AbstractManglingParser<Derived, Alloc>::parseDecltype() {
Richard Smithc20d1442018-08-20 20:14:49 +00003335 if (!consumeIf('D'))
3336 return nullptr;
3337 if (!consumeIf('t') && !consumeIf('T'))
3338 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00003339 Node *E = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00003340 if (E == nullptr)
3341 return nullptr;
3342 if (!consumeIf('E'))
3343 return nullptr;
3344 return make<EnclosingExpr>("decltype(", E, ")");
3345}
3346
3347// <array-type> ::= A <positive dimension number> _ <element type>
3348// ::= A [<dimension expression>] _ <element type>
Pavel Labathba825192018-10-16 14:29:14 +00003349template <typename Derived, typename Alloc>
3350Node *AbstractManglingParser<Derived, Alloc>::parseArrayType() {
Richard Smithc20d1442018-08-20 20:14:49 +00003351 if (!consumeIf('A'))
3352 return nullptr;
3353
Pavel Labathf4e67eb2018-10-10 08:39:16 +00003354 NodeOrString Dimension;
3355
Richard Smithc20d1442018-08-20 20:14:49 +00003356 if (std::isdigit(look())) {
Pavel Labathf4e67eb2018-10-10 08:39:16 +00003357 Dimension = parseNumber();
Richard Smithc20d1442018-08-20 20:14:49 +00003358 if (!consumeIf('_'))
3359 return nullptr;
Pavel Labathf4e67eb2018-10-10 08:39:16 +00003360 } else if (!consumeIf('_')) {
Pavel Labathba825192018-10-16 14:29:14 +00003361 Node *DimExpr = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00003362 if (DimExpr == nullptr)
3363 return nullptr;
3364 if (!consumeIf('_'))
3365 return nullptr;
Pavel Labathf4e67eb2018-10-10 08:39:16 +00003366 Dimension = DimExpr;
Richard Smithc20d1442018-08-20 20:14:49 +00003367 }
3368
Pavel Labathba825192018-10-16 14:29:14 +00003369 Node *Ty = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00003370 if (Ty == nullptr)
3371 return nullptr;
Pavel Labathf4e67eb2018-10-10 08:39:16 +00003372 return make<ArrayType>(Ty, Dimension);
Richard Smithc20d1442018-08-20 20:14:49 +00003373}
3374
3375// <pointer-to-member-type> ::= M <class type> <member type>
Pavel Labathba825192018-10-16 14:29:14 +00003376template <typename Derived, typename Alloc>
3377Node *AbstractManglingParser<Derived, Alloc>::parsePointerToMemberType() {
Richard Smithc20d1442018-08-20 20:14:49 +00003378 if (!consumeIf('M'))
3379 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00003380 Node *ClassType = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00003381 if (ClassType == nullptr)
3382 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00003383 Node *MemberType = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00003384 if (MemberType == nullptr)
3385 return nullptr;
3386 return make<PointerToMemberType>(ClassType, MemberType);
3387}
3388
3389// <class-enum-type> ::= <name> # non-dependent type name, dependent type name, or dependent typename-specifier
3390// ::= Ts <name> # dependent elaborated type specifier using 'struct' or 'class'
3391// ::= Tu <name> # dependent elaborated type specifier using 'union'
3392// ::= Te <name> # dependent elaborated type specifier using 'enum'
Pavel Labathba825192018-10-16 14:29:14 +00003393template <typename Derived, typename Alloc>
3394Node *AbstractManglingParser<Derived, Alloc>::parseClassEnumType() {
Richard Smithc20d1442018-08-20 20:14:49 +00003395 StringView ElabSpef;
3396 if (consumeIf("Ts"))
3397 ElabSpef = "struct";
3398 else if (consumeIf("Tu"))
3399 ElabSpef = "union";
3400 else if (consumeIf("Te"))
3401 ElabSpef = "enum";
3402
Pavel Labathba825192018-10-16 14:29:14 +00003403 Node *Name = getDerived().parseName();
Richard Smithc20d1442018-08-20 20:14:49 +00003404 if (Name == nullptr)
3405 return nullptr;
3406
3407 if (!ElabSpef.empty())
3408 return make<ElaboratedTypeSpefType>(ElabSpef, Name);
3409
3410 return Name;
3411}
3412
3413// <qualified-type> ::= <qualifiers> <type>
3414// <qualifiers> ::= <extended-qualifier>* <CV-qualifiers>
3415// <extended-qualifier> ::= U <source-name> [<template-args>] # vendor extended type qualifier
Pavel Labathba825192018-10-16 14:29:14 +00003416template <typename Derived, typename Alloc>
3417Node *AbstractManglingParser<Derived, Alloc>::parseQualifiedType() {
Richard Smithc20d1442018-08-20 20:14:49 +00003418 if (consumeIf('U')) {
3419 StringView Qual = parseBareSourceName();
3420 if (Qual.empty())
3421 return nullptr;
3422
3423 // FIXME parse the optional <template-args> here!
3424
3425 // extension ::= U <objc-name> <objc-type> # objc-type<identifier>
3426 if (Qual.startsWith("objcproto")) {
3427 StringView ProtoSourceName = Qual.dropFront(std::strlen("objcproto"));
3428 StringView Proto;
3429 {
3430 SwapAndRestore<const char *> SaveFirst(First, ProtoSourceName.begin()),
3431 SaveLast(Last, ProtoSourceName.end());
3432 Proto = parseBareSourceName();
3433 }
3434 if (Proto.empty())
3435 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00003436 Node *Child = getDerived().parseQualifiedType();
Richard Smithc20d1442018-08-20 20:14:49 +00003437 if (Child == nullptr)
3438 return nullptr;
3439 return make<ObjCProtoName>(Child, Proto);
3440 }
3441
Pavel Labathba825192018-10-16 14:29:14 +00003442 Node *Child = getDerived().parseQualifiedType();
Richard Smithc20d1442018-08-20 20:14:49 +00003443 if (Child == nullptr)
3444 return nullptr;
3445 return make<VendorExtQualType>(Child, Qual);
3446 }
3447
3448 Qualifiers Quals = parseCVQualifiers();
Pavel Labathba825192018-10-16 14:29:14 +00003449 Node *Ty = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00003450 if (Ty == nullptr)
3451 return nullptr;
3452 if (Quals != QualNone)
3453 Ty = make<QualType>(Ty, Quals);
3454 return Ty;
3455}
3456
3457// <type> ::= <builtin-type>
3458// ::= <qualified-type>
3459// ::= <function-type>
3460// ::= <class-enum-type>
3461// ::= <array-type>
3462// ::= <pointer-to-member-type>
3463// ::= <template-param>
3464// ::= <template-template-param> <template-args>
3465// ::= <decltype>
3466// ::= P <type> # pointer
3467// ::= R <type> # l-value reference
3468// ::= O <type> # r-value reference (C++11)
3469// ::= C <type> # complex pair (C99)
3470// ::= G <type> # imaginary (C99)
3471// ::= <substitution> # See Compression below
3472// extension ::= U <objc-name> <objc-type> # objc-type<identifier>
3473// extension ::= <vector-type> # <vector-type> starts with Dv
3474//
3475// <objc-name> ::= <k0 number> objcproto <k1 number> <identifier> # k0 = 9 + <number of digits in k1> + k1
3476// <objc-type> ::= <source-name> # PU<11+>objcproto 11objc_object<source-name> 11objc_object -> id<source-name>
Pavel Labathba825192018-10-16 14:29:14 +00003477template <typename Derived, typename Alloc>
3478Node *AbstractManglingParser<Derived, Alloc>::parseType() {
Richard Smithc20d1442018-08-20 20:14:49 +00003479 Node *Result = nullptr;
3480
Richard Smithc20d1442018-08-20 20:14:49 +00003481 switch (look()) {
3482 // ::= <qualified-type>
3483 case 'r':
3484 case 'V':
3485 case 'K': {
3486 unsigned AfterQuals = 0;
3487 if (look(AfterQuals) == 'r') ++AfterQuals;
3488 if (look(AfterQuals) == 'V') ++AfterQuals;
3489 if (look(AfterQuals) == 'K') ++AfterQuals;
3490
3491 if (look(AfterQuals) == 'F' ||
3492 (look(AfterQuals) == 'D' &&
3493 (look(AfterQuals + 1) == 'o' || look(AfterQuals + 1) == 'O' ||
3494 look(AfterQuals + 1) == 'w' || look(AfterQuals + 1) == 'x'))) {
Pavel Labathba825192018-10-16 14:29:14 +00003495 Result = getDerived().parseFunctionType();
Richard Smithc20d1442018-08-20 20:14:49 +00003496 break;
3497 }
Erik Pilkingtonf70e4d82019-01-17 20:37:51 +00003498 DEMANGLE_FALLTHROUGH;
Richard Smithc20d1442018-08-20 20:14:49 +00003499 }
3500 case 'U': {
Pavel Labathba825192018-10-16 14:29:14 +00003501 Result = getDerived().parseQualifiedType();
Richard Smithc20d1442018-08-20 20:14:49 +00003502 break;
3503 }
3504 // <builtin-type> ::= v # void
3505 case 'v':
3506 ++First;
3507 return make<NameType>("void");
3508 // ::= w # wchar_t
3509 case 'w':
3510 ++First;
3511 return make<NameType>("wchar_t");
3512 // ::= b # bool
3513 case 'b':
3514 ++First;
3515 return make<NameType>("bool");
3516 // ::= c # char
3517 case 'c':
3518 ++First;
3519 return make<NameType>("char");
3520 // ::= a # signed char
3521 case 'a':
3522 ++First;
3523 return make<NameType>("signed char");
3524 // ::= h # unsigned char
3525 case 'h':
3526 ++First;
3527 return make<NameType>("unsigned char");
3528 // ::= s # short
3529 case 's':
3530 ++First;
3531 return make<NameType>("short");
3532 // ::= t # unsigned short
3533 case 't':
3534 ++First;
3535 return make<NameType>("unsigned short");
3536 // ::= i # int
3537 case 'i':
3538 ++First;
3539 return make<NameType>("int");
3540 // ::= j # unsigned int
3541 case 'j':
3542 ++First;
3543 return make<NameType>("unsigned int");
3544 // ::= l # long
3545 case 'l':
3546 ++First;
3547 return make<NameType>("long");
3548 // ::= m # unsigned long
3549 case 'm':
3550 ++First;
3551 return make<NameType>("unsigned long");
3552 // ::= x # long long, __int64
3553 case 'x':
3554 ++First;
3555 return make<NameType>("long long");
3556 // ::= y # unsigned long long, __int64
3557 case 'y':
3558 ++First;
3559 return make<NameType>("unsigned long long");
3560 // ::= n # __int128
3561 case 'n':
3562 ++First;
3563 return make<NameType>("__int128");
3564 // ::= o # unsigned __int128
3565 case 'o':
3566 ++First;
3567 return make<NameType>("unsigned __int128");
3568 // ::= f # float
3569 case 'f':
3570 ++First;
3571 return make<NameType>("float");
3572 // ::= d # double
3573 case 'd':
3574 ++First;
3575 return make<NameType>("double");
3576 // ::= e # long double, __float80
3577 case 'e':
3578 ++First;
3579 return make<NameType>("long double");
3580 // ::= g # __float128
3581 case 'g':
3582 ++First;
3583 return make<NameType>("__float128");
3584 // ::= z # ellipsis
3585 case 'z':
3586 ++First;
3587 return make<NameType>("...");
3588
3589 // <builtin-type> ::= u <source-name> # vendor extended type
3590 case 'u': {
3591 ++First;
3592 StringView Res = parseBareSourceName();
3593 if (Res.empty())
3594 return nullptr;
Erik Pilkingtonb94a1f42019-06-10 21:02:39 +00003595 // Typically, <builtin-type>s are not considered substitution candidates,
3596 // but the exception to that exception is vendor extended types (Itanium C++
3597 // ABI 5.9.1).
3598 Result = make<NameType>(Res);
3599 break;
Richard Smithc20d1442018-08-20 20:14:49 +00003600 }
3601 case 'D':
3602 switch (look(1)) {
3603 // ::= Dd # IEEE 754r decimal floating point (64 bits)
3604 case 'd':
3605 First += 2;
3606 return make<NameType>("decimal64");
3607 // ::= De # IEEE 754r decimal floating point (128 bits)
3608 case 'e':
3609 First += 2;
3610 return make<NameType>("decimal128");
3611 // ::= Df # IEEE 754r decimal floating point (32 bits)
3612 case 'f':
3613 First += 2;
3614 return make<NameType>("decimal32");
3615 // ::= Dh # IEEE 754r half-precision floating point (16 bits)
3616 case 'h':
3617 First += 2;
3618 return make<NameType>("decimal16");
3619 // ::= Di # char32_t
3620 case 'i':
3621 First += 2;
3622 return make<NameType>("char32_t");
3623 // ::= Ds # char16_t
3624 case 's':
3625 First += 2;
3626 return make<NameType>("char16_t");
3627 // ::= Da # auto (in dependent new-expressions)
3628 case 'a':
3629 First += 2;
3630 return make<NameType>("auto");
3631 // ::= Dc # decltype(auto)
3632 case 'c':
3633 First += 2;
3634 return make<NameType>("decltype(auto)");
3635 // ::= Dn # std::nullptr_t (i.e., decltype(nullptr))
3636 case 'n':
3637 First += 2;
3638 return make<NameType>("std::nullptr_t");
3639
3640 // ::= <decltype>
3641 case 't':
3642 case 'T': {
Pavel Labathba825192018-10-16 14:29:14 +00003643 Result = getDerived().parseDecltype();
Richard Smithc20d1442018-08-20 20:14:49 +00003644 break;
3645 }
3646 // extension ::= <vector-type> # <vector-type> starts with Dv
3647 case 'v': {
Pavel Labathba825192018-10-16 14:29:14 +00003648 Result = getDerived().parseVectorType();
Richard Smithc20d1442018-08-20 20:14:49 +00003649 break;
3650 }
3651 // ::= Dp <type> # pack expansion (C++0x)
3652 case 'p': {
3653 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00003654 Node *Child = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00003655 if (!Child)
3656 return nullptr;
3657 Result = make<ParameterPackExpansion>(Child);
3658 break;
3659 }
3660 // Exception specifier on a function type.
3661 case 'o':
3662 case 'O':
3663 case 'w':
3664 // Transaction safe function type.
3665 case 'x':
Pavel Labathba825192018-10-16 14:29:14 +00003666 Result = getDerived().parseFunctionType();
Richard Smithc20d1442018-08-20 20:14:49 +00003667 break;
3668 }
3669 break;
3670 // ::= <function-type>
3671 case 'F': {
Pavel Labathba825192018-10-16 14:29:14 +00003672 Result = getDerived().parseFunctionType();
Richard Smithc20d1442018-08-20 20:14:49 +00003673 break;
3674 }
3675 // ::= <array-type>
3676 case 'A': {
Pavel Labathba825192018-10-16 14:29:14 +00003677 Result = getDerived().parseArrayType();
Richard Smithc20d1442018-08-20 20:14:49 +00003678 break;
3679 }
3680 // ::= <pointer-to-member-type>
3681 case 'M': {
Pavel Labathba825192018-10-16 14:29:14 +00003682 Result = getDerived().parsePointerToMemberType();
Richard Smithc20d1442018-08-20 20:14:49 +00003683 break;
3684 }
3685 // ::= <template-param>
3686 case 'T': {
3687 // This could be an elaborate type specifier on a <class-enum-type>.
3688 if (look(1) == 's' || look(1) == 'u' || look(1) == 'e') {
Pavel Labathba825192018-10-16 14:29:14 +00003689 Result = getDerived().parseClassEnumType();
Richard Smithc20d1442018-08-20 20:14:49 +00003690 break;
3691 }
3692
Pavel Labathba825192018-10-16 14:29:14 +00003693 Result = getDerived().parseTemplateParam();
Richard Smithc20d1442018-08-20 20:14:49 +00003694 if (Result == nullptr)
3695 return nullptr;
3696
3697 // Result could be either of:
3698 // <type> ::= <template-param>
3699 // <type> ::= <template-template-param> <template-args>
3700 //
3701 // <template-template-param> ::= <template-param>
3702 // ::= <substitution>
3703 //
3704 // If this is followed by some <template-args>, and we're permitted to
3705 // parse them, take the second production.
3706
3707 if (TryToParseTemplateArgs && look() == 'I') {
Pavel Labathba825192018-10-16 14:29:14 +00003708 Node *TA = getDerived().parseTemplateArgs();
Richard Smithc20d1442018-08-20 20:14:49 +00003709 if (TA == nullptr)
3710 return nullptr;
3711 Result = make<NameWithTemplateArgs>(Result, TA);
3712 }
3713 break;
3714 }
3715 // ::= P <type> # pointer
3716 case 'P': {
3717 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00003718 Node *Ptr = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00003719 if (Ptr == nullptr)
3720 return nullptr;
3721 Result = make<PointerType>(Ptr);
3722 break;
3723 }
3724 // ::= R <type> # l-value reference
3725 case 'R': {
3726 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00003727 Node *Ref = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00003728 if (Ref == nullptr)
3729 return nullptr;
3730 Result = make<ReferenceType>(Ref, ReferenceKind::LValue);
3731 break;
3732 }
3733 // ::= O <type> # r-value reference (C++11)
3734 case 'O': {
3735 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00003736 Node *Ref = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00003737 if (Ref == nullptr)
3738 return nullptr;
3739 Result = make<ReferenceType>(Ref, ReferenceKind::RValue);
3740 break;
3741 }
3742 // ::= C <type> # complex pair (C99)
3743 case 'C': {
3744 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00003745 Node *P = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00003746 if (P == nullptr)
3747 return nullptr;
3748 Result = make<PostfixQualifiedType>(P, " complex");
3749 break;
3750 }
3751 // ::= G <type> # imaginary (C99)
3752 case 'G': {
3753 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00003754 Node *P = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00003755 if (P == nullptr)
3756 return P;
3757 Result = make<PostfixQualifiedType>(P, " imaginary");
3758 break;
3759 }
3760 // ::= <substitution> # See Compression below
3761 case 'S': {
3762 if (look(1) && look(1) != 't') {
Pavel Labathba825192018-10-16 14:29:14 +00003763 Node *Sub = getDerived().parseSubstitution();
Richard Smithc20d1442018-08-20 20:14:49 +00003764 if (Sub == nullptr)
3765 return nullptr;
3766
3767 // Sub could be either of:
3768 // <type> ::= <substitution>
3769 // <type> ::= <template-template-param> <template-args>
3770 //
3771 // <template-template-param> ::= <template-param>
3772 // ::= <substitution>
3773 //
3774 // If this is followed by some <template-args>, and we're permitted to
3775 // parse them, take the second production.
3776
3777 if (TryToParseTemplateArgs && look() == 'I') {
Pavel Labathba825192018-10-16 14:29:14 +00003778 Node *TA = getDerived().parseTemplateArgs();
Richard Smithc20d1442018-08-20 20:14:49 +00003779 if (TA == nullptr)
3780 return nullptr;
3781 Result = make<NameWithTemplateArgs>(Sub, TA);
3782 break;
3783 }
3784
3785 // If all we parsed was a substitution, don't re-insert into the
3786 // substitution table.
3787 return Sub;
3788 }
Erik Pilkingtonf70e4d82019-01-17 20:37:51 +00003789 DEMANGLE_FALLTHROUGH;
Richard Smithc20d1442018-08-20 20:14:49 +00003790 }
3791 // ::= <class-enum-type>
3792 default: {
Pavel Labathba825192018-10-16 14:29:14 +00003793 Result = getDerived().parseClassEnumType();
Richard Smithc20d1442018-08-20 20:14:49 +00003794 break;
3795 }
3796 }
3797
3798 // If we parsed a type, insert it into the substitution table. Note that all
3799 // <builtin-type>s and <substitution>s have already bailed out, because they
3800 // don't get substitutions.
3801 if (Result != nullptr)
3802 Subs.push_back(Result);
3803 return Result;
3804}
3805
Pavel Labathba825192018-10-16 14:29:14 +00003806template <typename Derived, typename Alloc>
3807Node *AbstractManglingParser<Derived, Alloc>::parsePrefixExpr(StringView Kind) {
3808 Node *E = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00003809 if (E == nullptr)
3810 return nullptr;
3811 return make<PrefixExpr>(Kind, E);
3812}
3813
Pavel Labathba825192018-10-16 14:29:14 +00003814template <typename Derived, typename Alloc>
3815Node *AbstractManglingParser<Derived, Alloc>::parseBinaryExpr(StringView Kind) {
3816 Node *LHS = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00003817 if (LHS == nullptr)
3818 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00003819 Node *RHS = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00003820 if (RHS == nullptr)
3821 return nullptr;
3822 return make<BinaryExpr>(LHS, Kind, RHS);
3823}
3824
Pavel Labathba825192018-10-16 14:29:14 +00003825template <typename Derived, typename Alloc>
3826Node *
3827AbstractManglingParser<Derived, Alloc>::parseIntegerLiteral(StringView Lit) {
Richard Smithc20d1442018-08-20 20:14:49 +00003828 StringView Tmp = parseNumber(true);
3829 if (!Tmp.empty() && consumeIf('E'))
3830 return make<IntegerLiteral>(Lit, Tmp);
3831 return nullptr;
3832}
3833
3834// <CV-Qualifiers> ::= [r] [V] [K]
Pavel Labathba825192018-10-16 14:29:14 +00003835template <typename Alloc, typename Derived>
3836Qualifiers AbstractManglingParser<Alloc, Derived>::parseCVQualifiers() {
Richard Smithc20d1442018-08-20 20:14:49 +00003837 Qualifiers CVR = QualNone;
3838 if (consumeIf('r'))
3839 CVR |= QualRestrict;
3840 if (consumeIf('V'))
3841 CVR |= QualVolatile;
3842 if (consumeIf('K'))
3843 CVR |= QualConst;
3844 return CVR;
3845}
3846
3847// <function-param> ::= fp <top-level CV-Qualifiers> _ # L == 0, first parameter
3848// ::= fp <top-level CV-Qualifiers> <parameter-2 non-negative number> _ # L == 0, second and later parameters
3849// ::= fL <L-1 non-negative number> p <top-level CV-Qualifiers> _ # L > 0, first parameter
3850// ::= fL <L-1 non-negative number> p <top-level CV-Qualifiers> <parameter-2 non-negative number> _ # L > 0, second and later parameters
Pavel Labathba825192018-10-16 14:29:14 +00003851template <typename Derived, typename Alloc>
3852Node *AbstractManglingParser<Derived, Alloc>::parseFunctionParam() {
Richard Smithc20d1442018-08-20 20:14:49 +00003853 if (consumeIf("fp")) {
3854 parseCVQualifiers();
3855 StringView Num = parseNumber();
3856 if (!consumeIf('_'))
3857 return nullptr;
3858 return make<FunctionParam>(Num);
3859 }
3860 if (consumeIf("fL")) {
3861 if (parseNumber().empty())
3862 return nullptr;
3863 if (!consumeIf('p'))
3864 return nullptr;
3865 parseCVQualifiers();
3866 StringView Num = parseNumber();
3867 if (!consumeIf('_'))
3868 return nullptr;
3869 return make<FunctionParam>(Num);
3870 }
3871 return nullptr;
3872}
3873
3874// [gs] nw <expression>* _ <type> E # new (expr-list) type
3875// [gs] nw <expression>* _ <type> <initializer> # new (expr-list) type (init)
3876// [gs] na <expression>* _ <type> E # new[] (expr-list) type
3877// [gs] na <expression>* _ <type> <initializer> # new[] (expr-list) type (init)
3878// <initializer> ::= pi <expression>* E # parenthesized initialization
Pavel Labathba825192018-10-16 14:29:14 +00003879template <typename Derived, typename Alloc>
3880Node *AbstractManglingParser<Derived, Alloc>::parseNewExpr() {
Richard Smithc20d1442018-08-20 20:14:49 +00003881 bool Global = consumeIf("gs");
3882 bool IsArray = look(1) == 'a';
3883 if (!consumeIf("nw") && !consumeIf("na"))
3884 return nullptr;
3885 size_t Exprs = Names.size();
3886 while (!consumeIf('_')) {
Pavel Labathba825192018-10-16 14:29:14 +00003887 Node *Ex = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00003888 if (Ex == nullptr)
3889 return nullptr;
3890 Names.push_back(Ex);
3891 }
3892 NodeArray ExprList = popTrailingNodeArray(Exprs);
Pavel Labathba825192018-10-16 14:29:14 +00003893 Node *Ty = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00003894 if (Ty == nullptr)
3895 return Ty;
3896 if (consumeIf("pi")) {
3897 size_t InitsBegin = Names.size();
3898 while (!consumeIf('E')) {
Pavel Labathba825192018-10-16 14:29:14 +00003899 Node *Init = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00003900 if (Init == nullptr)
3901 return Init;
3902 Names.push_back(Init);
3903 }
3904 NodeArray Inits = popTrailingNodeArray(InitsBegin);
3905 return make<NewExpr>(ExprList, Ty, Inits, Global, IsArray);
3906 } else if (!consumeIf('E'))
3907 return nullptr;
3908 return make<NewExpr>(ExprList, Ty, NodeArray(), Global, IsArray);
3909}
3910
3911// cv <type> <expression> # conversion with one argument
3912// cv <type> _ <expression>* E # conversion with a different number of arguments
Pavel Labathba825192018-10-16 14:29:14 +00003913template <typename Derived, typename Alloc>
3914Node *AbstractManglingParser<Derived, Alloc>::parseConversionExpr() {
Richard Smithc20d1442018-08-20 20:14:49 +00003915 if (!consumeIf("cv"))
3916 return nullptr;
3917 Node *Ty;
3918 {
3919 SwapAndRestore<bool> SaveTemp(TryToParseTemplateArgs, false);
Pavel Labathba825192018-10-16 14:29:14 +00003920 Ty = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00003921 }
3922
3923 if (Ty == nullptr)
3924 return nullptr;
3925
3926 if (consumeIf('_')) {
3927 size_t ExprsBegin = Names.size();
3928 while (!consumeIf('E')) {
Pavel Labathba825192018-10-16 14:29:14 +00003929 Node *E = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00003930 if (E == nullptr)
3931 return E;
3932 Names.push_back(E);
3933 }
3934 NodeArray Exprs = popTrailingNodeArray(ExprsBegin);
3935 return make<ConversionExpr>(Ty, Exprs);
3936 }
3937
Pavel Labathba825192018-10-16 14:29:14 +00003938 Node *E[1] = {getDerived().parseExpr()};
Richard Smithc20d1442018-08-20 20:14:49 +00003939 if (E[0] == nullptr)
3940 return nullptr;
3941 return make<ConversionExpr>(Ty, makeNodeArray(E, E + 1));
3942}
3943
3944// <expr-primary> ::= L <type> <value number> E # integer literal
3945// ::= L <type> <value float> E # floating literal
3946// ::= L <string type> E # string literal
3947// ::= L <nullptr type> E # nullptr literal (i.e., "LDnE")
3948// FIXME: ::= L <type> <real-part float> _ <imag-part float> E # complex floating point literal (C 2000)
3949// ::= L <mangled-name> E # external name
Pavel Labathba825192018-10-16 14:29:14 +00003950template <typename Derived, typename Alloc>
3951Node *AbstractManglingParser<Derived, Alloc>::parseExprPrimary() {
Richard Smithc20d1442018-08-20 20:14:49 +00003952 if (!consumeIf('L'))
3953 return nullptr;
3954 switch (look()) {
3955 case 'w':
3956 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00003957 return getDerived().parseIntegerLiteral("wchar_t");
Richard Smithc20d1442018-08-20 20:14:49 +00003958 case 'b':
3959 if (consumeIf("b0E"))
3960 return make<BoolExpr>(0);
3961 if (consumeIf("b1E"))
3962 return make<BoolExpr>(1);
3963 return nullptr;
3964 case 'c':
3965 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00003966 return getDerived().parseIntegerLiteral("char");
Richard Smithc20d1442018-08-20 20:14:49 +00003967 case 'a':
3968 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00003969 return getDerived().parseIntegerLiteral("signed char");
Richard Smithc20d1442018-08-20 20:14:49 +00003970 case 'h':
3971 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00003972 return getDerived().parseIntegerLiteral("unsigned char");
Richard Smithc20d1442018-08-20 20:14:49 +00003973 case 's':
3974 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00003975 return getDerived().parseIntegerLiteral("short");
Richard Smithc20d1442018-08-20 20:14:49 +00003976 case 't':
3977 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00003978 return getDerived().parseIntegerLiteral("unsigned short");
Richard Smithc20d1442018-08-20 20:14:49 +00003979 case 'i':
3980 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00003981 return getDerived().parseIntegerLiteral("");
Richard Smithc20d1442018-08-20 20:14:49 +00003982 case 'j':
3983 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00003984 return getDerived().parseIntegerLiteral("u");
Richard Smithc20d1442018-08-20 20:14:49 +00003985 case 'l':
3986 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00003987 return getDerived().parseIntegerLiteral("l");
Richard Smithc20d1442018-08-20 20:14:49 +00003988 case 'm':
3989 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00003990 return getDerived().parseIntegerLiteral("ul");
Richard Smithc20d1442018-08-20 20:14:49 +00003991 case 'x':
3992 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00003993 return getDerived().parseIntegerLiteral("ll");
Richard Smithc20d1442018-08-20 20:14:49 +00003994 case 'y':
3995 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00003996 return getDerived().parseIntegerLiteral("ull");
Richard Smithc20d1442018-08-20 20:14:49 +00003997 case 'n':
3998 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00003999 return getDerived().parseIntegerLiteral("__int128");
Richard Smithc20d1442018-08-20 20:14:49 +00004000 case 'o':
4001 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00004002 return getDerived().parseIntegerLiteral("unsigned __int128");
Richard Smithc20d1442018-08-20 20:14:49 +00004003 case 'f':
4004 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00004005 return getDerived().template parseFloatingLiteral<float>();
Richard Smithc20d1442018-08-20 20:14:49 +00004006 case 'd':
4007 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00004008 return getDerived().template parseFloatingLiteral<double>();
Richard Smithc20d1442018-08-20 20:14:49 +00004009 case 'e':
4010 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00004011 return getDerived().template parseFloatingLiteral<long double>();
Richard Smithc20d1442018-08-20 20:14:49 +00004012 case '_':
4013 if (consumeIf("_Z")) {
Pavel Labathba825192018-10-16 14:29:14 +00004014 Node *R = getDerived().parseEncoding();
Richard Smithc20d1442018-08-20 20:14:49 +00004015 if (R != nullptr && consumeIf('E'))
4016 return R;
4017 }
4018 return nullptr;
4019 case 'T':
4020 // Invalid mangled name per
4021 // http://sourcerytools.com/pipermail/cxx-abi-dev/2011-August/002422.html
4022 return nullptr;
4023 default: {
4024 // might be named type
Pavel Labathba825192018-10-16 14:29:14 +00004025 Node *T = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00004026 if (T == nullptr)
4027 return nullptr;
4028 StringView N = parseNumber();
4029 if (!N.empty()) {
4030 if (!consumeIf('E'))
4031 return nullptr;
4032 return make<IntegerCastExpr>(T, N);
4033 }
4034 if (consumeIf('E'))
4035 return T;
4036 return nullptr;
4037 }
4038 }
4039}
4040
4041// <braced-expression> ::= <expression>
4042// ::= di <field source-name> <braced-expression> # .name = expr
4043// ::= dx <index expression> <braced-expression> # [expr] = expr
4044// ::= dX <range begin expression> <range end expression> <braced-expression>
Pavel Labathba825192018-10-16 14:29:14 +00004045template <typename Derived, typename Alloc>
4046Node *AbstractManglingParser<Derived, Alloc>::parseBracedExpr() {
Richard Smithc20d1442018-08-20 20:14:49 +00004047 if (look() == 'd') {
4048 switch (look(1)) {
4049 case 'i': {
4050 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004051 Node *Field = getDerived().parseSourceName(/*NameState=*/nullptr);
Richard Smithc20d1442018-08-20 20:14:49 +00004052 if (Field == nullptr)
4053 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00004054 Node *Init = getDerived().parseBracedExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004055 if (Init == nullptr)
4056 return nullptr;
4057 return make<BracedExpr>(Field, Init, /*isArray=*/false);
4058 }
4059 case 'x': {
4060 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004061 Node *Index = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004062 if (Index == nullptr)
4063 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00004064 Node *Init = getDerived().parseBracedExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004065 if (Init == nullptr)
4066 return nullptr;
4067 return make<BracedExpr>(Index, Init, /*isArray=*/true);
4068 }
4069 case 'X': {
4070 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004071 Node *RangeBegin = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004072 if (RangeBegin == nullptr)
4073 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00004074 Node *RangeEnd = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004075 if (RangeEnd == nullptr)
4076 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00004077 Node *Init = getDerived().parseBracedExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004078 if (Init == nullptr)
4079 return nullptr;
4080 return make<BracedRangeExpr>(RangeBegin, RangeEnd, Init);
4081 }
4082 }
4083 }
Pavel Labathba825192018-10-16 14:29:14 +00004084 return getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004085}
4086
4087// (not yet in the spec)
4088// <fold-expr> ::= fL <binary-operator-name> <expression> <expression>
4089// ::= fR <binary-operator-name> <expression> <expression>
4090// ::= fl <binary-operator-name> <expression>
4091// ::= fr <binary-operator-name> <expression>
Pavel Labathba825192018-10-16 14:29:14 +00004092template <typename Derived, typename Alloc>
4093Node *AbstractManglingParser<Derived, Alloc>::parseFoldExpr() {
Richard Smithc20d1442018-08-20 20:14:49 +00004094 if (!consumeIf('f'))
4095 return nullptr;
4096
4097 char FoldKind = look();
4098 bool IsLeftFold, HasInitializer;
4099 HasInitializer = FoldKind == 'L' || FoldKind == 'R';
4100 if (FoldKind == 'l' || FoldKind == 'L')
4101 IsLeftFold = true;
4102 else if (FoldKind == 'r' || FoldKind == 'R')
4103 IsLeftFold = false;
4104 else
4105 return nullptr;
4106 ++First;
4107
4108 // FIXME: This map is duplicated in parseOperatorName and parseExpr.
4109 StringView OperatorName;
4110 if (consumeIf("aa")) OperatorName = "&&";
4111 else if (consumeIf("an")) OperatorName = "&";
4112 else if (consumeIf("aN")) OperatorName = "&=";
4113 else if (consumeIf("aS")) OperatorName = "=";
4114 else if (consumeIf("cm")) OperatorName = ",";
4115 else if (consumeIf("ds")) OperatorName = ".*";
4116 else if (consumeIf("dv")) OperatorName = "/";
4117 else if (consumeIf("dV")) OperatorName = "/=";
4118 else if (consumeIf("eo")) OperatorName = "^";
4119 else if (consumeIf("eO")) OperatorName = "^=";
4120 else if (consumeIf("eq")) OperatorName = "==";
4121 else if (consumeIf("ge")) OperatorName = ">=";
4122 else if (consumeIf("gt")) OperatorName = ">";
4123 else if (consumeIf("le")) OperatorName = "<=";
4124 else if (consumeIf("ls")) OperatorName = "<<";
4125 else if (consumeIf("lS")) OperatorName = "<<=";
4126 else if (consumeIf("lt")) OperatorName = "<";
4127 else if (consumeIf("mi")) OperatorName = "-";
4128 else if (consumeIf("mI")) OperatorName = "-=";
4129 else if (consumeIf("ml")) OperatorName = "*";
4130 else if (consumeIf("mL")) OperatorName = "*=";
4131 else if (consumeIf("ne")) OperatorName = "!=";
4132 else if (consumeIf("oo")) OperatorName = "||";
4133 else if (consumeIf("or")) OperatorName = "|";
4134 else if (consumeIf("oR")) OperatorName = "|=";
4135 else if (consumeIf("pl")) OperatorName = "+";
4136 else if (consumeIf("pL")) OperatorName = "+=";
4137 else if (consumeIf("rm")) OperatorName = "%";
4138 else if (consumeIf("rM")) OperatorName = "%=";
4139 else if (consumeIf("rs")) OperatorName = ">>";
4140 else if (consumeIf("rS")) OperatorName = ">>=";
4141 else return nullptr;
4142
Pavel Labathba825192018-10-16 14:29:14 +00004143 Node *Pack = getDerived().parseExpr(), *Init = nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00004144 if (Pack == nullptr)
4145 return nullptr;
4146 if (HasInitializer) {
Pavel Labathba825192018-10-16 14:29:14 +00004147 Init = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004148 if (Init == nullptr)
4149 return nullptr;
4150 }
4151
4152 if (IsLeftFold && Init)
4153 std::swap(Pack, Init);
4154
4155 return make<FoldExpr>(IsLeftFold, OperatorName, Pack, Init);
4156}
4157
4158// <expression> ::= <unary operator-name> <expression>
4159// ::= <binary operator-name> <expression> <expression>
4160// ::= <ternary operator-name> <expression> <expression> <expression>
4161// ::= cl <expression>+ E # call
4162// ::= cv <type> <expression> # conversion with one argument
4163// ::= cv <type> _ <expression>* E # conversion with a different number of arguments
4164// ::= [gs] nw <expression>* _ <type> E # new (expr-list) type
4165// ::= [gs] nw <expression>* _ <type> <initializer> # new (expr-list) type (init)
4166// ::= [gs] na <expression>* _ <type> E # new[] (expr-list) type
4167// ::= [gs] na <expression>* _ <type> <initializer> # new[] (expr-list) type (init)
4168// ::= [gs] dl <expression> # delete expression
4169// ::= [gs] da <expression> # delete[] expression
4170// ::= pp_ <expression> # prefix ++
4171// ::= mm_ <expression> # prefix --
4172// ::= ti <type> # typeid (type)
4173// ::= te <expression> # typeid (expression)
4174// ::= dc <type> <expression> # dynamic_cast<type> (expression)
4175// ::= sc <type> <expression> # static_cast<type> (expression)
4176// ::= cc <type> <expression> # const_cast<type> (expression)
4177// ::= rc <type> <expression> # reinterpret_cast<type> (expression)
4178// ::= st <type> # sizeof (a type)
4179// ::= sz <expression> # sizeof (an expression)
4180// ::= at <type> # alignof (a type)
4181// ::= az <expression> # alignof (an expression)
4182// ::= nx <expression> # noexcept (expression)
4183// ::= <template-param>
4184// ::= <function-param>
4185// ::= dt <expression> <unresolved-name> # expr.name
4186// ::= pt <expression> <unresolved-name> # expr->name
4187// ::= ds <expression> <expression> # expr.*expr
4188// ::= sZ <template-param> # size of a parameter pack
4189// ::= sZ <function-param> # size of a function parameter pack
4190// ::= sP <template-arg>* E # sizeof...(T), size of a captured template parameter pack from an alias template
4191// ::= sp <expression> # pack expansion
4192// ::= tw <expression> # throw expression
4193// ::= tr # throw with no operand (rethrow)
4194// ::= <unresolved-name> # f(p), N::f(p), ::f(p),
4195// # freestanding dependent name (e.g., T::x),
4196// # objectless nonstatic member reference
4197// ::= fL <binary-operator-name> <expression> <expression>
4198// ::= fR <binary-operator-name> <expression> <expression>
4199// ::= fl <binary-operator-name> <expression>
4200// ::= fr <binary-operator-name> <expression>
4201// ::= <expr-primary>
Pavel Labathba825192018-10-16 14:29:14 +00004202template <typename Derived, typename Alloc>
4203Node *AbstractManglingParser<Derived, Alloc>::parseExpr() {
Richard Smithc20d1442018-08-20 20:14:49 +00004204 bool Global = consumeIf("gs");
4205 if (numLeft() < 2)
4206 return nullptr;
4207
4208 switch (*First) {
4209 case 'L':
Pavel Labathba825192018-10-16 14:29:14 +00004210 return getDerived().parseExprPrimary();
Richard Smithc20d1442018-08-20 20:14:49 +00004211 case 'T':
Pavel Labathba825192018-10-16 14:29:14 +00004212 return getDerived().parseTemplateParam();
Richard Smithc20d1442018-08-20 20:14:49 +00004213 case 'f': {
4214 // Disambiguate a fold expression from a <function-param>.
4215 if (look(1) == 'p' || (look(1) == 'L' && std::isdigit(look(2))))
Pavel Labathba825192018-10-16 14:29:14 +00004216 return getDerived().parseFunctionParam();
4217 return getDerived().parseFoldExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004218 }
4219 case 'a':
4220 switch (First[1]) {
4221 case 'a':
4222 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004223 return getDerived().parseBinaryExpr("&&");
Richard Smithc20d1442018-08-20 20:14:49 +00004224 case 'd':
4225 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004226 return getDerived().parsePrefixExpr("&");
Richard Smithc20d1442018-08-20 20:14:49 +00004227 case 'n':
4228 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004229 return getDerived().parseBinaryExpr("&");
Richard Smithc20d1442018-08-20 20:14:49 +00004230 case 'N':
4231 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004232 return getDerived().parseBinaryExpr("&=");
Richard Smithc20d1442018-08-20 20:14:49 +00004233 case 'S':
4234 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004235 return getDerived().parseBinaryExpr("=");
Richard Smithc20d1442018-08-20 20:14:49 +00004236 case 't': {
4237 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004238 Node *Ty = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00004239 if (Ty == nullptr)
4240 return nullptr;
4241 return make<EnclosingExpr>("alignof (", Ty, ")");
4242 }
4243 case 'z': {
4244 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004245 Node *Ty = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004246 if (Ty == nullptr)
4247 return nullptr;
4248 return make<EnclosingExpr>("alignof (", Ty, ")");
4249 }
4250 }
4251 return nullptr;
4252 case 'c':
4253 switch (First[1]) {
4254 // cc <type> <expression> # const_cast<type>(expression)
4255 case 'c': {
4256 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004257 Node *Ty = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00004258 if (Ty == nullptr)
4259 return Ty;
Pavel Labathba825192018-10-16 14:29:14 +00004260 Node *Ex = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004261 if (Ex == nullptr)
4262 return Ex;
4263 return make<CastExpr>("const_cast", Ty, Ex);
4264 }
4265 // cl <expression>+ E # call
4266 case 'l': {
4267 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004268 Node *Callee = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004269 if (Callee == nullptr)
4270 return Callee;
4271 size_t ExprsBegin = Names.size();
4272 while (!consumeIf('E')) {
Pavel Labathba825192018-10-16 14:29:14 +00004273 Node *E = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004274 if (E == nullptr)
4275 return E;
4276 Names.push_back(E);
4277 }
4278 return make<CallExpr>(Callee, popTrailingNodeArray(ExprsBegin));
4279 }
4280 case 'm':
4281 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004282 return getDerived().parseBinaryExpr(",");
Richard Smithc20d1442018-08-20 20:14:49 +00004283 case 'o':
4284 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004285 return getDerived().parsePrefixExpr("~");
Richard Smithc20d1442018-08-20 20:14:49 +00004286 case 'v':
Pavel Labathba825192018-10-16 14:29:14 +00004287 return getDerived().parseConversionExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004288 }
4289 return nullptr;
4290 case 'd':
4291 switch (First[1]) {
4292 case 'a': {
4293 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004294 Node *Ex = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004295 if (Ex == nullptr)
4296 return Ex;
4297 return make<DeleteExpr>(Ex, Global, /*is_array=*/true);
4298 }
4299 case 'c': {
4300 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004301 Node *T = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00004302 if (T == nullptr)
4303 return T;
Pavel Labathba825192018-10-16 14:29:14 +00004304 Node *Ex = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004305 if (Ex == nullptr)
4306 return Ex;
4307 return make<CastExpr>("dynamic_cast", T, Ex);
4308 }
4309 case 'e':
4310 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004311 return getDerived().parsePrefixExpr("*");
Richard Smithc20d1442018-08-20 20:14:49 +00004312 case 'l': {
4313 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004314 Node *E = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004315 if (E == nullptr)
4316 return E;
4317 return make<DeleteExpr>(E, Global, /*is_array=*/false);
4318 }
4319 case 'n':
Pavel Labathba825192018-10-16 14:29:14 +00004320 return getDerived().parseUnresolvedName();
Richard Smithc20d1442018-08-20 20:14:49 +00004321 case 's': {
4322 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004323 Node *LHS = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004324 if (LHS == nullptr)
4325 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00004326 Node *RHS = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004327 if (RHS == nullptr)
4328 return nullptr;
4329 return make<MemberExpr>(LHS, ".*", RHS);
4330 }
4331 case 't': {
4332 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004333 Node *LHS = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004334 if (LHS == nullptr)
4335 return LHS;
Pavel Labathba825192018-10-16 14:29:14 +00004336 Node *RHS = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004337 if (RHS == nullptr)
4338 return nullptr;
4339 return make<MemberExpr>(LHS, ".", RHS);
4340 }
4341 case 'v':
4342 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004343 return getDerived().parseBinaryExpr("/");
Richard Smithc20d1442018-08-20 20:14:49 +00004344 case 'V':
4345 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004346 return getDerived().parseBinaryExpr("/=");
Richard Smithc20d1442018-08-20 20:14:49 +00004347 }
4348 return nullptr;
4349 case 'e':
4350 switch (First[1]) {
4351 case 'o':
4352 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004353 return getDerived().parseBinaryExpr("^");
Richard Smithc20d1442018-08-20 20:14:49 +00004354 case 'O':
4355 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004356 return getDerived().parseBinaryExpr("^=");
Richard Smithc20d1442018-08-20 20:14:49 +00004357 case 'q':
4358 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004359 return getDerived().parseBinaryExpr("==");
Richard Smithc20d1442018-08-20 20:14:49 +00004360 }
4361 return nullptr;
4362 case 'g':
4363 switch (First[1]) {
4364 case 'e':
4365 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004366 return getDerived().parseBinaryExpr(">=");
Richard Smithc20d1442018-08-20 20:14:49 +00004367 case 't':
4368 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004369 return getDerived().parseBinaryExpr(">");
Richard Smithc20d1442018-08-20 20:14:49 +00004370 }
4371 return nullptr;
4372 case 'i':
4373 switch (First[1]) {
4374 case 'x': {
4375 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004376 Node *Base = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004377 if (Base == nullptr)
4378 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00004379 Node *Index = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004380 if (Index == nullptr)
4381 return Index;
4382 return make<ArraySubscriptExpr>(Base, Index);
4383 }
4384 case 'l': {
4385 First += 2;
4386 size_t InitsBegin = Names.size();
4387 while (!consumeIf('E')) {
Pavel Labathba825192018-10-16 14:29:14 +00004388 Node *E = getDerived().parseBracedExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004389 if (E == nullptr)
4390 return nullptr;
4391 Names.push_back(E);
4392 }
4393 return make<InitListExpr>(nullptr, popTrailingNodeArray(InitsBegin));
4394 }
4395 }
4396 return nullptr;
4397 case 'l':
4398 switch (First[1]) {
4399 case 'e':
4400 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004401 return getDerived().parseBinaryExpr("<=");
Richard Smithc20d1442018-08-20 20:14:49 +00004402 case 's':
4403 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004404 return getDerived().parseBinaryExpr("<<");
Richard Smithc20d1442018-08-20 20:14:49 +00004405 case 'S':
4406 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004407 return getDerived().parseBinaryExpr("<<=");
Richard Smithc20d1442018-08-20 20:14:49 +00004408 case 't':
4409 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004410 return getDerived().parseBinaryExpr("<");
Richard Smithc20d1442018-08-20 20:14:49 +00004411 }
4412 return nullptr;
4413 case 'm':
4414 switch (First[1]) {
4415 case 'i':
4416 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004417 return getDerived().parseBinaryExpr("-");
Richard Smithc20d1442018-08-20 20:14:49 +00004418 case 'I':
4419 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004420 return getDerived().parseBinaryExpr("-=");
Richard Smithc20d1442018-08-20 20:14:49 +00004421 case 'l':
4422 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004423 return getDerived().parseBinaryExpr("*");
Richard Smithc20d1442018-08-20 20:14:49 +00004424 case 'L':
4425 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004426 return getDerived().parseBinaryExpr("*=");
Richard Smithc20d1442018-08-20 20:14:49 +00004427 case 'm':
4428 First += 2;
4429 if (consumeIf('_'))
Pavel Labathba825192018-10-16 14:29:14 +00004430 return getDerived().parsePrefixExpr("--");
4431 Node *Ex = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004432 if (Ex == nullptr)
4433 return nullptr;
4434 return make<PostfixExpr>(Ex, "--");
4435 }
4436 return nullptr;
4437 case 'n':
4438 switch (First[1]) {
4439 case 'a':
4440 case 'w':
Pavel Labathba825192018-10-16 14:29:14 +00004441 return getDerived().parseNewExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004442 case 'e':
4443 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004444 return getDerived().parseBinaryExpr("!=");
Richard Smithc20d1442018-08-20 20:14:49 +00004445 case 'g':
4446 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004447 return getDerived().parsePrefixExpr("-");
Richard Smithc20d1442018-08-20 20:14:49 +00004448 case 't':
4449 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004450 return getDerived().parsePrefixExpr("!");
Richard Smithc20d1442018-08-20 20:14:49 +00004451 case 'x':
4452 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004453 Node *Ex = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004454 if (Ex == nullptr)
4455 return Ex;
4456 return make<EnclosingExpr>("noexcept (", Ex, ")");
4457 }
4458 return nullptr;
4459 case 'o':
4460 switch (First[1]) {
4461 case 'n':
Pavel Labathba825192018-10-16 14:29:14 +00004462 return getDerived().parseUnresolvedName();
Richard Smithc20d1442018-08-20 20:14:49 +00004463 case 'o':
4464 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004465 return getDerived().parseBinaryExpr("||");
Richard Smithc20d1442018-08-20 20:14:49 +00004466 case 'r':
4467 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004468 return getDerived().parseBinaryExpr("|");
Richard Smithc20d1442018-08-20 20:14:49 +00004469 case 'R':
4470 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004471 return getDerived().parseBinaryExpr("|=");
Richard Smithc20d1442018-08-20 20:14:49 +00004472 }
4473 return nullptr;
4474 case 'p':
4475 switch (First[1]) {
4476 case 'm':
4477 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004478 return getDerived().parseBinaryExpr("->*");
Richard Smithc20d1442018-08-20 20:14:49 +00004479 case 'l':
4480 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004481 return getDerived().parseBinaryExpr("+");
Richard Smithc20d1442018-08-20 20:14:49 +00004482 case 'L':
4483 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004484 return getDerived().parseBinaryExpr("+=");
Richard Smithc20d1442018-08-20 20:14:49 +00004485 case 'p': {
4486 First += 2;
4487 if (consumeIf('_'))
Pavel Labathba825192018-10-16 14:29:14 +00004488 return getDerived().parsePrefixExpr("++");
4489 Node *Ex = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004490 if (Ex == nullptr)
4491 return Ex;
4492 return make<PostfixExpr>(Ex, "++");
4493 }
4494 case 's':
4495 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004496 return getDerived().parsePrefixExpr("+");
Richard Smithc20d1442018-08-20 20:14:49 +00004497 case 't': {
4498 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004499 Node *L = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004500 if (L == nullptr)
4501 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00004502 Node *R = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004503 if (R == nullptr)
4504 return nullptr;
4505 return make<MemberExpr>(L, "->", R);
4506 }
4507 }
4508 return nullptr;
4509 case 'q':
4510 if (First[1] == 'u') {
4511 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004512 Node *Cond = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004513 if (Cond == nullptr)
4514 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00004515 Node *LHS = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004516 if (LHS == nullptr)
4517 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00004518 Node *RHS = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004519 if (RHS == nullptr)
4520 return nullptr;
4521 return make<ConditionalExpr>(Cond, LHS, RHS);
4522 }
4523 return nullptr;
4524 case 'r':
4525 switch (First[1]) {
4526 case 'c': {
4527 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004528 Node *T = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00004529 if (T == nullptr)
4530 return T;
Pavel Labathba825192018-10-16 14:29:14 +00004531 Node *Ex = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004532 if (Ex == nullptr)
4533 return Ex;
4534 return make<CastExpr>("reinterpret_cast", T, Ex);
4535 }
4536 case 'm':
4537 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004538 return getDerived().parseBinaryExpr("%");
Richard Smithc20d1442018-08-20 20:14:49 +00004539 case 'M':
4540 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004541 return getDerived().parseBinaryExpr("%=");
Richard Smithc20d1442018-08-20 20:14:49 +00004542 case 's':
4543 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004544 return getDerived().parseBinaryExpr(">>");
Richard Smithc20d1442018-08-20 20:14:49 +00004545 case 'S':
4546 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004547 return getDerived().parseBinaryExpr(">>=");
Richard Smithc20d1442018-08-20 20:14:49 +00004548 }
4549 return nullptr;
4550 case 's':
4551 switch (First[1]) {
4552 case 'c': {
4553 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004554 Node *T = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00004555 if (T == nullptr)
4556 return T;
Pavel Labathba825192018-10-16 14:29:14 +00004557 Node *Ex = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004558 if (Ex == nullptr)
4559 return Ex;
4560 return make<CastExpr>("static_cast", T, Ex);
4561 }
4562 case 'p': {
4563 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004564 Node *Child = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004565 if (Child == nullptr)
4566 return nullptr;
4567 return make<ParameterPackExpansion>(Child);
4568 }
4569 case 'r':
Pavel Labathba825192018-10-16 14:29:14 +00004570 return getDerived().parseUnresolvedName();
Richard Smithc20d1442018-08-20 20:14:49 +00004571 case 't': {
4572 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004573 Node *Ty = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00004574 if (Ty == nullptr)
4575 return Ty;
4576 return make<EnclosingExpr>("sizeof (", Ty, ")");
4577 }
4578 case 'z': {
4579 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004580 Node *Ex = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004581 if (Ex == nullptr)
4582 return Ex;
4583 return make<EnclosingExpr>("sizeof (", Ex, ")");
4584 }
4585 case 'Z':
4586 First += 2;
4587 if (look() == 'T') {
Pavel Labathba825192018-10-16 14:29:14 +00004588 Node *R = getDerived().parseTemplateParam();
Richard Smithc20d1442018-08-20 20:14:49 +00004589 if (R == nullptr)
4590 return nullptr;
4591 return make<SizeofParamPackExpr>(R);
4592 } else if (look() == 'f') {
Pavel Labathba825192018-10-16 14:29:14 +00004593 Node *FP = getDerived().parseFunctionParam();
Richard Smithc20d1442018-08-20 20:14:49 +00004594 if (FP == nullptr)
4595 return nullptr;
4596 return make<EnclosingExpr>("sizeof... (", FP, ")");
4597 }
4598 return nullptr;
4599 case 'P': {
4600 First += 2;
4601 size_t ArgsBegin = Names.size();
4602 while (!consumeIf('E')) {
Pavel Labathba825192018-10-16 14:29:14 +00004603 Node *Arg = getDerived().parseTemplateArg();
Richard Smithc20d1442018-08-20 20:14:49 +00004604 if (Arg == nullptr)
4605 return nullptr;
4606 Names.push_back(Arg);
4607 }
Richard Smithb485b352018-08-24 23:30:26 +00004608 auto *Pack = make<NodeArrayNode>(popTrailingNodeArray(ArgsBegin));
4609 if (!Pack)
4610 return nullptr;
4611 return make<EnclosingExpr>("sizeof... (", Pack, ")");
Richard Smithc20d1442018-08-20 20:14:49 +00004612 }
4613 }
4614 return nullptr;
4615 case 't':
4616 switch (First[1]) {
4617 case 'e': {
4618 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004619 Node *Ex = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004620 if (Ex == nullptr)
4621 return Ex;
4622 return make<EnclosingExpr>("typeid (", Ex, ")");
4623 }
4624 case 'i': {
4625 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004626 Node *Ty = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00004627 if (Ty == nullptr)
4628 return Ty;
4629 return make<EnclosingExpr>("typeid (", Ty, ")");
4630 }
4631 case 'l': {
4632 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004633 Node *Ty = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00004634 if (Ty == nullptr)
4635 return nullptr;
4636 size_t InitsBegin = Names.size();
4637 while (!consumeIf('E')) {
Pavel Labathba825192018-10-16 14:29:14 +00004638 Node *E = getDerived().parseBracedExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004639 if (E == nullptr)
4640 return nullptr;
4641 Names.push_back(E);
4642 }
4643 return make<InitListExpr>(Ty, popTrailingNodeArray(InitsBegin));
4644 }
4645 case 'r':
4646 First += 2;
4647 return make<NameType>("throw");
4648 case 'w': {
4649 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004650 Node *Ex = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004651 if (Ex == nullptr)
4652 return nullptr;
4653 return make<ThrowExpr>(Ex);
4654 }
4655 }
4656 return nullptr;
4657 case '1':
4658 case '2':
4659 case '3':
4660 case '4':
4661 case '5':
4662 case '6':
4663 case '7':
4664 case '8':
4665 case '9':
Pavel Labathba825192018-10-16 14:29:14 +00004666 return getDerived().parseUnresolvedName();
Richard Smithc20d1442018-08-20 20:14:49 +00004667 }
Erik Pilkingtone8457c62019-06-18 23:34:09 +00004668
4669 if (consumeIf("u8__uuidoft")) {
4670 Node *Ty = getDerived().parseType();
4671 if (!Ty)
4672 return nullptr;
4673 return make<UUIDOfExpr>(Ty);
4674 }
4675
4676 if (consumeIf("u8__uuidofz")) {
4677 Node *Ex = getDerived().parseExpr();
4678 if (!Ex)
4679 return nullptr;
4680 return make<UUIDOfExpr>(Ex);
4681 }
4682
Richard Smithc20d1442018-08-20 20:14:49 +00004683 return nullptr;
4684}
4685
4686// <call-offset> ::= h <nv-offset> _
4687// ::= v <v-offset> _
4688//
4689// <nv-offset> ::= <offset number>
4690// # non-virtual base override
4691//
4692// <v-offset> ::= <offset number> _ <virtual offset number>
4693// # virtual base override, with vcall offset
Pavel Labathba825192018-10-16 14:29:14 +00004694template <typename Alloc, typename Derived>
4695bool AbstractManglingParser<Alloc, Derived>::parseCallOffset() {
Richard Smithc20d1442018-08-20 20:14:49 +00004696 // Just scan through the call offset, we never add this information into the
4697 // output.
4698 if (consumeIf('h'))
4699 return parseNumber(true).empty() || !consumeIf('_');
4700 if (consumeIf('v'))
4701 return parseNumber(true).empty() || !consumeIf('_') ||
4702 parseNumber(true).empty() || !consumeIf('_');
4703 return true;
4704}
4705
4706// <special-name> ::= TV <type> # virtual table
4707// ::= TT <type> # VTT structure (construction vtable index)
4708// ::= TI <type> # typeinfo structure
4709// ::= TS <type> # typeinfo name (null-terminated byte string)
4710// ::= Tc <call-offset> <call-offset> <base encoding>
4711// # base is the nominal target function of thunk
4712// # first call-offset is 'this' adjustment
4713// # second call-offset is result adjustment
4714// ::= T <call-offset> <base encoding>
4715// # base is the nominal target function of thunk
4716// ::= GV <object name> # Guard variable for one-time initialization
4717// # No <type>
4718// ::= TW <object name> # Thread-local wrapper
4719// ::= TH <object name> # Thread-local initialization
4720// ::= GR <object name> _ # First temporary
4721// ::= GR <object name> <seq-id> _ # Subsequent temporaries
4722// extension ::= TC <first type> <number> _ <second type> # construction vtable for second-in-first
4723// extension ::= GR <object name> # reference temporary for object
Pavel Labathba825192018-10-16 14:29:14 +00004724template <typename Derived, typename Alloc>
4725Node *AbstractManglingParser<Derived, Alloc>::parseSpecialName() {
Richard Smithc20d1442018-08-20 20:14:49 +00004726 switch (look()) {
4727 case 'T':
4728 switch (look(1)) {
4729 // TV <type> # virtual table
4730 case 'V': {
4731 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004732 Node *Ty = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00004733 if (Ty == nullptr)
4734 return nullptr;
4735 return make<SpecialName>("vtable for ", Ty);
4736 }
4737 // TT <type> # VTT structure (construction vtable index)
4738 case 'T': {
4739 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004740 Node *Ty = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00004741 if (Ty == nullptr)
4742 return nullptr;
4743 return make<SpecialName>("VTT for ", Ty);
4744 }
4745 // TI <type> # typeinfo structure
4746 case 'I': {
4747 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004748 Node *Ty = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00004749 if (Ty == nullptr)
4750 return nullptr;
4751 return make<SpecialName>("typeinfo for ", Ty);
4752 }
4753 // TS <type> # typeinfo name (null-terminated byte string)
4754 case 'S': {
4755 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004756 Node *Ty = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00004757 if (Ty == nullptr)
4758 return nullptr;
4759 return make<SpecialName>("typeinfo name for ", Ty);
4760 }
4761 // Tc <call-offset> <call-offset> <base encoding>
4762 case 'c': {
4763 First += 2;
4764 if (parseCallOffset() || parseCallOffset())
4765 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00004766 Node *Encoding = getDerived().parseEncoding();
Richard Smithc20d1442018-08-20 20:14:49 +00004767 if (Encoding == nullptr)
4768 return nullptr;
4769 return make<SpecialName>("covariant return thunk to ", Encoding);
4770 }
4771 // extension ::= TC <first type> <number> _ <second type>
4772 // # construction vtable for second-in-first
4773 case 'C': {
4774 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004775 Node *FirstType = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00004776 if (FirstType == nullptr)
4777 return nullptr;
4778 if (parseNumber(true).empty() || !consumeIf('_'))
4779 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00004780 Node *SecondType = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00004781 if (SecondType == nullptr)
4782 return nullptr;
4783 return make<CtorVtableSpecialName>(SecondType, FirstType);
4784 }
4785 // TW <object name> # Thread-local wrapper
4786 case 'W': {
4787 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004788 Node *Name = getDerived().parseName();
Richard Smithc20d1442018-08-20 20:14:49 +00004789 if (Name == nullptr)
4790 return nullptr;
4791 return make<SpecialName>("thread-local wrapper routine for ", Name);
4792 }
4793 // TH <object name> # Thread-local initialization
4794 case 'H': {
4795 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004796 Node *Name = getDerived().parseName();
Richard Smithc20d1442018-08-20 20:14:49 +00004797 if (Name == nullptr)
4798 return nullptr;
4799 return make<SpecialName>("thread-local initialization routine for ", Name);
4800 }
4801 // T <call-offset> <base encoding>
4802 default: {
4803 ++First;
4804 bool IsVirt = look() == 'v';
4805 if (parseCallOffset())
4806 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00004807 Node *BaseEncoding = getDerived().parseEncoding();
Richard Smithc20d1442018-08-20 20:14:49 +00004808 if (BaseEncoding == nullptr)
4809 return nullptr;
4810 if (IsVirt)
4811 return make<SpecialName>("virtual thunk to ", BaseEncoding);
4812 else
4813 return make<SpecialName>("non-virtual thunk to ", BaseEncoding);
4814 }
4815 }
4816 case 'G':
4817 switch (look(1)) {
4818 // GV <object name> # Guard variable for one-time initialization
4819 case 'V': {
4820 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004821 Node *Name = getDerived().parseName();
Richard Smithc20d1442018-08-20 20:14:49 +00004822 if (Name == nullptr)
4823 return nullptr;
4824 return make<SpecialName>("guard variable for ", Name);
4825 }
4826 // GR <object name> # reference temporary for object
4827 // GR <object name> _ # First temporary
4828 // GR <object name> <seq-id> _ # Subsequent temporaries
4829 case 'R': {
4830 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004831 Node *Name = getDerived().parseName();
Richard Smithc20d1442018-08-20 20:14:49 +00004832 if (Name == nullptr)
4833 return nullptr;
4834 size_t Count;
4835 bool ParsedSeqId = !parseSeqId(&Count);
4836 if (!consumeIf('_') && ParsedSeqId)
4837 return nullptr;
4838 return make<SpecialName>("reference temporary for ", Name);
4839 }
4840 }
4841 }
4842 return nullptr;
4843}
4844
4845// <encoding> ::= <function name> <bare-function-type>
4846// ::= <data name>
4847// ::= <special-name>
Pavel Labathba825192018-10-16 14:29:14 +00004848template <typename Derived, typename Alloc>
4849Node *AbstractManglingParser<Derived, Alloc>::parseEncoding() {
Richard Smithc20d1442018-08-20 20:14:49 +00004850 if (look() == 'G' || look() == 'T')
Pavel Labathba825192018-10-16 14:29:14 +00004851 return getDerived().parseSpecialName();
Richard Smithc20d1442018-08-20 20:14:49 +00004852
4853 auto IsEndOfEncoding = [&] {
4854 // The set of chars that can potentially follow an <encoding> (none of which
4855 // can start a <type>). Enumerating these allows us to avoid speculative
4856 // parsing.
4857 return numLeft() == 0 || look() == 'E' || look() == '.' || look() == '_';
4858 };
4859
4860 NameState NameInfo(this);
Pavel Labathba825192018-10-16 14:29:14 +00004861 Node *Name = getDerived().parseName(&NameInfo);
Richard Smithc20d1442018-08-20 20:14:49 +00004862 if (Name == nullptr)
4863 return nullptr;
4864
4865 if (resolveForwardTemplateRefs(NameInfo))
4866 return nullptr;
4867
4868 if (IsEndOfEncoding())
4869 return Name;
4870
4871 Node *Attrs = nullptr;
4872 if (consumeIf("Ua9enable_ifI")) {
4873 size_t BeforeArgs = Names.size();
4874 while (!consumeIf('E')) {
Pavel Labathba825192018-10-16 14:29:14 +00004875 Node *Arg = getDerived().parseTemplateArg();
Richard Smithc20d1442018-08-20 20:14:49 +00004876 if (Arg == nullptr)
4877 return nullptr;
4878 Names.push_back(Arg);
4879 }
4880 Attrs = make<EnableIfAttr>(popTrailingNodeArray(BeforeArgs));
Richard Smithb485b352018-08-24 23:30:26 +00004881 if (!Attrs)
4882 return nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00004883 }
4884
4885 Node *ReturnType = nullptr;
4886 if (!NameInfo.CtorDtorConversion && NameInfo.EndsWithTemplateArgs) {
Pavel Labathba825192018-10-16 14:29:14 +00004887 ReturnType = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00004888 if (ReturnType == nullptr)
4889 return nullptr;
4890 }
4891
4892 if (consumeIf('v'))
4893 return make<FunctionEncoding>(ReturnType, Name, NodeArray(),
4894 Attrs, NameInfo.CVQualifiers,
4895 NameInfo.ReferenceQualifier);
4896
4897 size_t ParamsBegin = Names.size();
4898 do {
Pavel Labathba825192018-10-16 14:29:14 +00004899 Node *Ty = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00004900 if (Ty == nullptr)
4901 return nullptr;
4902 Names.push_back(Ty);
4903 } while (!IsEndOfEncoding());
4904
4905 return make<FunctionEncoding>(ReturnType, Name,
4906 popTrailingNodeArray(ParamsBegin),
4907 Attrs, NameInfo.CVQualifiers,
4908 NameInfo.ReferenceQualifier);
4909}
4910
4911template <class Float>
4912struct FloatData;
4913
4914template <>
4915struct FloatData<float>
4916{
4917 static const size_t mangled_size = 8;
4918 static const size_t max_demangled_size = 24;
4919 static constexpr const char* spec = "%af";
4920};
4921
4922template <>
4923struct FloatData<double>
4924{
4925 static const size_t mangled_size = 16;
4926 static const size_t max_demangled_size = 32;
4927 static constexpr const char* spec = "%a";
4928};
4929
4930template <>
4931struct FloatData<long double>
4932{
4933#if defined(__mips__) && defined(__mips_n64) || defined(__aarch64__) || \
4934 defined(__wasm__)
4935 static const size_t mangled_size = 32;
4936#elif defined(__arm__) || defined(__mips__) || defined(__hexagon__)
4937 static const size_t mangled_size = 16;
4938#else
4939 static const size_t mangled_size = 20; // May need to be adjusted to 16 or 24 on other platforms
4940#endif
4941 static const size_t max_demangled_size = 40;
4942 static constexpr const char *spec = "%LaL";
4943};
4944
Pavel Labathba825192018-10-16 14:29:14 +00004945template <typename Alloc, typename Derived>
4946template <class Float>
4947Node *AbstractManglingParser<Alloc, Derived>::parseFloatingLiteral() {
Richard Smithc20d1442018-08-20 20:14:49 +00004948 const size_t N = FloatData<Float>::mangled_size;
4949 if (numLeft() <= N)
4950 return nullptr;
4951 StringView Data(First, First + N);
4952 for (char C : Data)
4953 if (!std::isxdigit(C))
4954 return nullptr;
4955 First += N;
4956 if (!consumeIf('E'))
4957 return nullptr;
4958 return make<FloatLiteralImpl<Float>>(Data);
4959}
4960
4961// <seq-id> ::= <0-9A-Z>+
Pavel Labathba825192018-10-16 14:29:14 +00004962template <typename Alloc, typename Derived>
4963bool AbstractManglingParser<Alloc, Derived>::parseSeqId(size_t *Out) {
Richard Smithc20d1442018-08-20 20:14:49 +00004964 if (!(look() >= '0' && look() <= '9') &&
4965 !(look() >= 'A' && look() <= 'Z'))
4966 return true;
4967
4968 size_t Id = 0;
4969 while (true) {
4970 if (look() >= '0' && look() <= '9') {
4971 Id *= 36;
4972 Id += static_cast<size_t>(look() - '0');
4973 } else if (look() >= 'A' && look() <= 'Z') {
4974 Id *= 36;
4975 Id += static_cast<size_t>(look() - 'A') + 10;
4976 } else {
4977 *Out = Id;
4978 return false;
4979 }
4980 ++First;
4981 }
4982}
4983
4984// <substitution> ::= S <seq-id> _
4985// ::= S_
4986// <substitution> ::= Sa # ::std::allocator
4987// <substitution> ::= Sb # ::std::basic_string
4988// <substitution> ::= Ss # ::std::basic_string < char,
4989// ::std::char_traits<char>,
4990// ::std::allocator<char> >
4991// <substitution> ::= Si # ::std::basic_istream<char, std::char_traits<char> >
4992// <substitution> ::= So # ::std::basic_ostream<char, std::char_traits<char> >
4993// <substitution> ::= Sd # ::std::basic_iostream<char, std::char_traits<char> >
Pavel Labathba825192018-10-16 14:29:14 +00004994template <typename Derived, typename Alloc>
4995Node *AbstractManglingParser<Derived, Alloc>::parseSubstitution() {
Richard Smithc20d1442018-08-20 20:14:49 +00004996 if (!consumeIf('S'))
4997 return nullptr;
4998
4999 if (std::islower(look())) {
5000 Node *SpecialSub;
5001 switch (look()) {
5002 case 'a':
5003 ++First;
5004 SpecialSub = make<SpecialSubstitution>(SpecialSubKind::allocator);
5005 break;
5006 case 'b':
5007 ++First;
5008 SpecialSub = make<SpecialSubstitution>(SpecialSubKind::basic_string);
5009 break;
5010 case 's':
5011 ++First;
5012 SpecialSub = make<SpecialSubstitution>(SpecialSubKind::string);
5013 break;
5014 case 'i':
5015 ++First;
5016 SpecialSub = make<SpecialSubstitution>(SpecialSubKind::istream);
5017 break;
5018 case 'o':
5019 ++First;
5020 SpecialSub = make<SpecialSubstitution>(SpecialSubKind::ostream);
5021 break;
5022 case 'd':
5023 ++First;
5024 SpecialSub = make<SpecialSubstitution>(SpecialSubKind::iostream);
5025 break;
5026 default:
5027 return nullptr;
5028 }
Richard Smithb485b352018-08-24 23:30:26 +00005029 if (!SpecialSub)
5030 return nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00005031 // Itanium C++ ABI 5.1.2: If a name that would use a built-in <substitution>
5032 // has ABI tags, the tags are appended to the substitution; the result is a
5033 // substitutable component.
Pavel Labathba825192018-10-16 14:29:14 +00005034 Node *WithTags = getDerived().parseAbiTags(SpecialSub);
Richard Smithc20d1442018-08-20 20:14:49 +00005035 if (WithTags != SpecialSub) {
5036 Subs.push_back(WithTags);
5037 SpecialSub = WithTags;
5038 }
5039 return SpecialSub;
5040 }
5041
5042 // ::= S_
5043 if (consumeIf('_')) {
5044 if (Subs.empty())
5045 return nullptr;
5046 return Subs[0];
5047 }
5048
5049 // ::= S <seq-id> _
5050 size_t Index = 0;
5051 if (parseSeqId(&Index))
5052 return nullptr;
5053 ++Index;
5054 if (!consumeIf('_') || Index >= Subs.size())
5055 return nullptr;
5056 return Subs[Index];
5057}
5058
5059// <template-param> ::= T_ # first template parameter
5060// ::= T <parameter-2 non-negative number> _
Pavel Labathba825192018-10-16 14:29:14 +00005061template <typename Derived, typename Alloc>
5062Node *AbstractManglingParser<Derived, Alloc>::parseTemplateParam() {
Richard Smithc20d1442018-08-20 20:14:49 +00005063 if (!consumeIf('T'))
5064 return nullptr;
5065
5066 size_t Index = 0;
5067 if (!consumeIf('_')) {
5068 if (parsePositiveInteger(&Index))
5069 return nullptr;
5070 ++Index;
5071 if (!consumeIf('_'))
5072 return nullptr;
5073 }
5074
5075 // Itanium ABI 5.1.8: In a generic lambda, uses of auto in the parameter list
5076 // are mangled as the corresponding artificial template type parameter.
5077 if (ParsingLambdaParams)
5078 return make<NameType>("auto");
5079
5080 // If we're in a context where this <template-param> refers to a
5081 // <template-arg> further ahead in the mangled name (currently just conversion
5082 // operator types), then we should only look it up in the right context.
5083 if (PermitForwardTemplateReferences) {
Richard Smithb485b352018-08-24 23:30:26 +00005084 Node *ForwardRef = make<ForwardTemplateReference>(Index);
5085 if (!ForwardRef)
5086 return nullptr;
5087 assert(ForwardRef->getKind() == Node::KForwardTemplateReference);
5088 ForwardTemplateRefs.push_back(
5089 static_cast<ForwardTemplateReference *>(ForwardRef));
5090 return ForwardRef;
Richard Smithc20d1442018-08-20 20:14:49 +00005091 }
5092
5093 if (Index >= TemplateParams.size())
5094 return nullptr;
5095 return TemplateParams[Index];
5096}
5097
5098// <template-arg> ::= <type> # type or template
5099// ::= X <expression> E # expression
5100// ::= <expr-primary> # simple expressions
5101// ::= J <template-arg>* E # argument pack
5102// ::= LZ <encoding> E # extension
Pavel Labathba825192018-10-16 14:29:14 +00005103template <typename Derived, typename Alloc>
5104Node *AbstractManglingParser<Derived, Alloc>::parseTemplateArg() {
Richard Smithc20d1442018-08-20 20:14:49 +00005105 switch (look()) {
5106 case 'X': {
5107 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00005108 Node *Arg = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00005109 if (Arg == nullptr || !consumeIf('E'))
5110 return nullptr;
5111 return Arg;
5112 }
5113 case 'J': {
5114 ++First;
5115 size_t ArgsBegin = Names.size();
5116 while (!consumeIf('E')) {
Pavel Labathba825192018-10-16 14:29:14 +00005117 Node *Arg = getDerived().parseTemplateArg();
Richard Smithc20d1442018-08-20 20:14:49 +00005118 if (Arg == nullptr)
5119 return nullptr;
5120 Names.push_back(Arg);
5121 }
5122 NodeArray Args = popTrailingNodeArray(ArgsBegin);
5123 return make<TemplateArgumentPack>(Args);
5124 }
5125 case 'L': {
5126 // ::= LZ <encoding> E # extension
5127 if (look(1) == 'Z') {
5128 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00005129 Node *Arg = getDerived().parseEncoding();
Richard Smithc20d1442018-08-20 20:14:49 +00005130 if (Arg == nullptr || !consumeIf('E'))
5131 return nullptr;
5132 return Arg;
5133 }
5134 // ::= <expr-primary> # simple expressions
Pavel Labathba825192018-10-16 14:29:14 +00005135 return getDerived().parseExprPrimary();
Richard Smithc20d1442018-08-20 20:14:49 +00005136 }
5137 default:
Pavel Labathba825192018-10-16 14:29:14 +00005138 return getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00005139 }
5140}
5141
5142// <template-args> ::= I <template-arg>* E
5143// extension, the abi says <template-arg>+
Pavel Labathba825192018-10-16 14:29:14 +00005144template <typename Derived, typename Alloc>
5145Node *
5146AbstractManglingParser<Derived, Alloc>::parseTemplateArgs(bool TagTemplates) {
Richard Smithc20d1442018-08-20 20:14:49 +00005147 if (!consumeIf('I'))
5148 return nullptr;
5149
5150 // <template-params> refer to the innermost <template-args>. Clear out any
5151 // outer args that we may have inserted into TemplateParams.
5152 if (TagTemplates)
5153 TemplateParams.clear();
5154
5155 size_t ArgsBegin = Names.size();
5156 while (!consumeIf('E')) {
5157 if (TagTemplates) {
5158 auto OldParams = std::move(TemplateParams);
Pavel Labathba825192018-10-16 14:29:14 +00005159 Node *Arg = getDerived().parseTemplateArg();
Richard Smithc20d1442018-08-20 20:14:49 +00005160 TemplateParams = std::move(OldParams);
5161 if (Arg == nullptr)
5162 return nullptr;
5163 Names.push_back(Arg);
5164 Node *TableEntry = Arg;
5165 if (Arg->getKind() == Node::KTemplateArgumentPack) {
5166 TableEntry = make<ParameterPack>(
5167 static_cast<TemplateArgumentPack*>(TableEntry)->getElements());
Richard Smithb485b352018-08-24 23:30:26 +00005168 if (!TableEntry)
5169 return nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00005170 }
5171 TemplateParams.push_back(TableEntry);
5172 } else {
Pavel Labathba825192018-10-16 14:29:14 +00005173 Node *Arg = getDerived().parseTemplateArg();
Richard Smithc20d1442018-08-20 20:14:49 +00005174 if (Arg == nullptr)
5175 return nullptr;
5176 Names.push_back(Arg);
5177 }
5178 }
5179 return make<TemplateArgs>(popTrailingNodeArray(ArgsBegin));
5180}
5181
5182// <mangled-name> ::= _Z <encoding>
5183// ::= <type>
5184// extension ::= ___Z <encoding> _block_invoke
5185// extension ::= ___Z <encoding> _block_invoke<decimal-digit>+
5186// extension ::= ___Z <encoding> _block_invoke_<decimal-digit>+
Pavel Labathba825192018-10-16 14:29:14 +00005187template <typename Derived, typename Alloc>
5188Node *AbstractManglingParser<Derived, Alloc>::parse() {
Erik Pilkingtonc0df1582019-01-17 21:37:36 +00005189 if (consumeIf("_Z") || consumeIf("__Z")) {
Pavel Labathba825192018-10-16 14:29:14 +00005190 Node *Encoding = getDerived().parseEncoding();
Richard Smithc20d1442018-08-20 20:14:49 +00005191 if (Encoding == nullptr)
5192 return nullptr;
5193 if (look() == '.') {
5194 Encoding = make<DotSuffix>(Encoding, StringView(First, Last));
5195 First = Last;
5196 }
5197 if (numLeft() != 0)
5198 return nullptr;
5199 return Encoding;
5200 }
5201
Erik Pilkingtonc0df1582019-01-17 21:37:36 +00005202 if (consumeIf("___Z") || consumeIf("____Z")) {
Pavel Labathba825192018-10-16 14:29:14 +00005203 Node *Encoding = getDerived().parseEncoding();
Richard Smithc20d1442018-08-20 20:14:49 +00005204 if (Encoding == nullptr || !consumeIf("_block_invoke"))
5205 return nullptr;
5206 bool RequireNumber = consumeIf('_');
5207 if (parseNumber().empty() && RequireNumber)
5208 return nullptr;
5209 if (look() == '.')
5210 First = Last;
5211 if (numLeft() != 0)
5212 return nullptr;
5213 return make<SpecialName>("invocation function for block in ", Encoding);
5214 }
5215
Pavel Labathba825192018-10-16 14:29:14 +00005216 Node *Ty = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00005217 if (numLeft() != 0)
5218 return nullptr;
5219 return Ty;
5220}
5221
Pavel Labathba825192018-10-16 14:29:14 +00005222template <typename Alloc>
5223struct ManglingParser : AbstractManglingParser<ManglingParser<Alloc>, Alloc> {
5224 using AbstractManglingParser<ManglingParser<Alloc>,
5225 Alloc>::AbstractManglingParser;
5226};
5227
Erik Pilkingtonf70e4d82019-01-17 20:37:51 +00005228DEMANGLE_NAMESPACE_END
Richard Smithc20d1442018-08-20 20:14:49 +00005229
Erik Pilkingtonf70e4d82019-01-17 20:37:51 +00005230#endif // DEMANGLE_ITANIUMDEMANGLE_H