blob: f73959868d7061cea9d4ab3dda306aa6884efc9f [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) \
Richard Smithdf1c14c2019-09-06 23:53:21 +000060 X(SyntheticTemplateParamName) \
61 X(TypeTemplateParamDecl) \
62 X(NonTypeTemplateParamDecl) \
63 X(TemplateTemplateParamDecl) \
64 X(TemplateParamPackDecl) \
Richard Smithc20d1442018-08-20 20:14:49 +000065 X(ParameterPack) \
66 X(TemplateArgumentPack) \
67 X(ParameterPackExpansion) \
68 X(TemplateArgs) \
69 X(ForwardTemplateReference) \
70 X(NameWithTemplateArgs) \
71 X(GlobalQualifiedName) \
72 X(StdQualifiedName) \
73 X(ExpandedSpecialSubstitution) \
74 X(SpecialSubstitution) \
75 X(CtorDtorName) \
76 X(DtorName) \
77 X(UnnamedTypeName) \
78 X(ClosureTypeName) \
79 X(StructuredBindingName) \
80 X(BinaryExpr) \
81 X(ArraySubscriptExpr) \
82 X(PostfixExpr) \
83 X(ConditionalExpr) \
84 X(MemberExpr) \
Richard Smith1865d2f2020-10-22 19:29:36 -070085 X(SubobjectExpr) \
Richard Smithc20d1442018-08-20 20:14:49 +000086 X(EnclosingExpr) \
87 X(CastExpr) \
88 X(SizeofParamPackExpr) \
89 X(CallExpr) \
90 X(NewExpr) \
91 X(DeleteExpr) \
92 X(PrefixExpr) \
93 X(FunctionParam) \
94 X(ConversionExpr) \
Richard Smith1865d2f2020-10-22 19:29:36 -070095 X(PointerToMemberConversionExpr) \
Richard Smithc20d1442018-08-20 20:14:49 +000096 X(InitListExpr) \
97 X(FoldExpr) \
98 X(ThrowExpr) \
99 X(BoolExpr) \
Richard Smithdf1c14c2019-09-06 23:53:21 +0000100 X(StringLiteral) \
101 X(LambdaExpr) \
Erik Pilkington0a170f12020-05-13 14:13:37 -0400102 X(EnumLiteral) \
Richard Smithc20d1442018-08-20 20:14:49 +0000103 X(IntegerLiteral) \
104 X(FloatLiteral) \
105 X(DoubleLiteral) \
106 X(LongDoubleLiteral) \
107 X(BracedExpr) \
108 X(BracedRangeExpr)
109
Erik Pilkingtonf70e4d82019-01-17 20:37:51 +0000110DEMANGLE_NAMESPACE_BEGIN
111
Mikhail Borisov8452f062021-08-17 18:06:53 -0400112template <class T, size_t N> class PODSmallVector {
113 static_assert(std::is_pod<T>::value,
114 "T is required to be a plain old data type");
115
116 T *First = nullptr;
117 T *Last = nullptr;
118 T *Cap = nullptr;
119 T Inline[N] = {0};
120
121 bool isInline() const { return First == Inline; }
122
123 void clearInline() {
124 First = Inline;
125 Last = Inline;
126 Cap = Inline + N;
127 }
128
129 void reserve(size_t NewCap) {
130 size_t S = size();
131 if (isInline()) {
132 auto *Tmp = static_cast<T *>(std::malloc(NewCap * sizeof(T)));
133 if (Tmp == nullptr)
134 std::terminate();
135 std::copy(First, Last, Tmp);
136 First = Tmp;
137 } else {
138 First = static_cast<T *>(std::realloc(First, NewCap * sizeof(T)));
139 if (First == nullptr)
140 std::terminate();
141 }
142 Last = First + S;
143 Cap = First + NewCap;
144 }
145
146public:
147 PODSmallVector() : First(Inline), Last(First), Cap(Inline + N) {}
148
149 PODSmallVector(const PODSmallVector &) = delete;
150 PODSmallVector &operator=(const PODSmallVector &) = delete;
151
152 PODSmallVector(PODSmallVector &&Other) : PODSmallVector() {
153 if (Other.isInline()) {
154 std::copy(Other.begin(), Other.end(), First);
155 Last = First + Other.size();
156 Other.clear();
157 return;
158 }
159
160 First = Other.First;
161 Last = Other.Last;
162 Cap = Other.Cap;
163 Other.clearInline();
164 }
165
166 PODSmallVector &operator=(PODSmallVector &&Other) {
167 if (Other.isInline()) {
168 if (!isInline()) {
169 std::free(First);
170 clearInline();
171 }
172 std::copy(Other.begin(), Other.end(), First);
173 Last = First + Other.size();
174 Other.clear();
175 return *this;
176 }
177
178 if (isInline()) {
179 First = Other.First;
180 Last = Other.Last;
181 Cap = Other.Cap;
182 Other.clearInline();
183 return *this;
184 }
185
186 std::swap(First, Other.First);
187 std::swap(Last, Other.Last);
188 std::swap(Cap, Other.Cap);
189 Other.clear();
190 return *this;
191 }
192
193 // NOLINTNEXTLINE(readability-identifier-naming)
194 void push_back(const T &Elem) {
195 if (Last == Cap)
196 reserve(size() * 2);
197 *Last++ = Elem;
198 }
199
200 // NOLINTNEXTLINE(readability-identifier-naming)
201 void pop_back() {
202 assert(Last != First && "Popping empty vector!");
203 --Last;
204 }
205
206 void dropBack(size_t Index) {
207 assert(Index <= size() && "dropBack() can't expand!");
208 Last = First + Index;
209 }
210
211 T *begin() { return First; }
212 T *end() { return Last; }
213
214 bool empty() const { return First == Last; }
215 size_t size() const { return static_cast<size_t>(Last - First); }
216 T &back() {
217 assert(Last != First && "Calling back() on empty vector!");
218 return *(Last - 1);
219 }
220 T &operator[](size_t Index) {
221 assert(Index < size() && "Invalid access!");
222 return *(begin() + Index);
223 }
224 void clear() { Last = First; }
225
226 ~PODSmallVector() {
227 if (!isInline())
228 std::free(First);
229 }
230};
231
Richard Smithc20d1442018-08-20 20:14:49 +0000232// Base class of all AST nodes. The AST is built by the parser, then is
233// traversed by the printLeft/Right functions to produce a demangled string.
234class Node {
235public:
236 enum Kind : unsigned char {
237#define ENUMERATOR(NodeKind) K ## NodeKind,
238 FOR_EACH_NODE_KIND(ENUMERATOR)
239#undef ENUMERATOR
240 };
241
242 /// Three-way bool to track a cached value. Unknown is possible if this node
243 /// has an unexpanded parameter pack below it that may affect this cache.
244 enum class Cache : unsigned char { Yes, No, Unknown, };
245
246private:
247 Kind K;
248
249 // FIXME: Make these protected.
250public:
251 /// Tracks if this node has a component on its right side, in which case we
252 /// need to call printRight.
253 Cache RHSComponentCache;
254
255 /// Track if this node is a (possibly qualified) array type. This can affect
256 /// how we format the output string.
257 Cache ArrayCache;
258
259 /// Track if this node is a (possibly qualified) function type. This can
260 /// affect how we format the output string.
261 Cache FunctionCache;
262
263public:
264 Node(Kind K_, Cache RHSComponentCache_ = Cache::No,
265 Cache ArrayCache_ = Cache::No, Cache FunctionCache_ = Cache::No)
266 : K(K_), RHSComponentCache(RHSComponentCache_), ArrayCache(ArrayCache_),
267 FunctionCache(FunctionCache_) {}
268
269 /// Visit the most-derived object corresponding to this object.
270 template<typename Fn> void visit(Fn F) const;
271
272 // The following function is provided by all derived classes:
273 //
274 // Call F with arguments that, when passed to the constructor of this node,
275 // would construct an equivalent node.
276 //template<typename Fn> void match(Fn F) const;
277
278 bool hasRHSComponent(OutputStream &S) const {
279 if (RHSComponentCache != Cache::Unknown)
280 return RHSComponentCache == Cache::Yes;
281 return hasRHSComponentSlow(S);
282 }
283
284 bool hasArray(OutputStream &S) const {
285 if (ArrayCache != Cache::Unknown)
286 return ArrayCache == Cache::Yes;
287 return hasArraySlow(S);
288 }
289
290 bool hasFunction(OutputStream &S) const {
291 if (FunctionCache != Cache::Unknown)
292 return FunctionCache == Cache::Yes;
293 return hasFunctionSlow(S);
294 }
295
296 Kind getKind() const { return K; }
297
298 virtual bool hasRHSComponentSlow(OutputStream &) const { return false; }
299 virtual bool hasArraySlow(OutputStream &) const { return false; }
300 virtual bool hasFunctionSlow(OutputStream &) const { return false; }
301
302 // Dig through "glue" nodes like ParameterPack and ForwardTemplateReference to
303 // get at a node that actually represents some concrete syntax.
304 virtual const Node *getSyntaxNode(OutputStream &) const {
305 return this;
306 }
307
308 void print(OutputStream &S) const {
309 printLeft(S);
310 if (RHSComponentCache != Cache::No)
311 printRight(S);
312 }
313
314 // Print the "left" side of this Node into OutputStream.
315 virtual void printLeft(OutputStream &) const = 0;
316
317 // Print the "right". This distinction is necessary to represent C++ types
318 // that appear on the RHS of their subtype, such as arrays or functions.
319 // Since most types don't have such a component, provide a default
320 // implementation.
321 virtual void printRight(OutputStream &) const {}
322
323 virtual StringView getBaseName() const { return StringView(); }
324
325 // Silence compiler warnings, this dtor will never be called.
326 virtual ~Node() = default;
327
328#ifndef NDEBUG
Erik Pilkingtonf70e4d82019-01-17 20:37:51 +0000329 DEMANGLE_DUMP_METHOD void dump() const;
Richard Smithc20d1442018-08-20 20:14:49 +0000330#endif
331};
332
333class NodeArray {
334 Node **Elements;
335 size_t NumElements;
336
337public:
338 NodeArray() : Elements(nullptr), NumElements(0) {}
339 NodeArray(Node **Elements_, size_t NumElements_)
340 : Elements(Elements_), NumElements(NumElements_) {}
341
342 bool empty() const { return NumElements == 0; }
343 size_t size() const { return NumElements; }
344
345 Node **begin() const { return Elements; }
346 Node **end() const { return Elements + NumElements; }
347
348 Node *operator[](size_t Idx) const { return Elements[Idx]; }
349
350 void printWithComma(OutputStream &S) const {
351 bool FirstElement = true;
352 for (size_t Idx = 0; Idx != NumElements; ++Idx) {
353 size_t BeforeComma = S.getCurrentPosition();
354 if (!FirstElement)
355 S += ", ";
356 size_t AfterComma = S.getCurrentPosition();
357 Elements[Idx]->print(S);
358
359 // Elements[Idx] is an empty parameter pack expansion, we should erase the
360 // comma we just printed.
361 if (AfterComma == S.getCurrentPosition()) {
362 S.setCurrentPosition(BeforeComma);
363 continue;
364 }
365
366 FirstElement = false;
367 }
368 }
369};
370
371struct NodeArrayNode : Node {
372 NodeArray Array;
373 NodeArrayNode(NodeArray Array_) : Node(KNodeArrayNode), Array(Array_) {}
374
375 template<typename Fn> void match(Fn F) const { F(Array); }
376
377 void printLeft(OutputStream &S) const override {
378 Array.printWithComma(S);
379 }
380};
381
382class DotSuffix final : public Node {
383 const Node *Prefix;
384 const StringView Suffix;
385
386public:
387 DotSuffix(const Node *Prefix_, StringView Suffix_)
388 : Node(KDotSuffix), Prefix(Prefix_), Suffix(Suffix_) {}
389
390 template<typename Fn> void match(Fn F) const { F(Prefix, Suffix); }
391
392 void printLeft(OutputStream &s) const override {
393 Prefix->print(s);
394 s += " (";
395 s += Suffix;
396 s += ")";
397 }
398};
399
400class VendorExtQualType final : public Node {
401 const Node *Ty;
402 StringView Ext;
Alex Orlovf50df922021-03-24 10:21:32 +0400403 const Node *TA;
Richard Smithc20d1442018-08-20 20:14:49 +0000404
405public:
Alex Orlovf50df922021-03-24 10:21:32 +0400406 VendorExtQualType(const Node *Ty_, StringView Ext_, const Node *TA_)
407 : Node(KVendorExtQualType), Ty(Ty_), Ext(Ext_), TA(TA_) {}
Richard Smithc20d1442018-08-20 20:14:49 +0000408
Alex Orlovf50df922021-03-24 10:21:32 +0400409 template <typename Fn> void match(Fn F) const { F(Ty, Ext, TA); }
Richard Smithc20d1442018-08-20 20:14:49 +0000410
411 void printLeft(OutputStream &S) const override {
412 Ty->print(S);
413 S += " ";
414 S += Ext;
Alex Orlovf50df922021-03-24 10:21:32 +0400415 if (TA != nullptr)
416 TA->print(S);
Richard Smithc20d1442018-08-20 20:14:49 +0000417 }
418};
419
420enum FunctionRefQual : unsigned char {
421 FrefQualNone,
422 FrefQualLValue,
423 FrefQualRValue,
424};
425
426enum Qualifiers {
427 QualNone = 0,
428 QualConst = 0x1,
429 QualVolatile = 0x2,
430 QualRestrict = 0x4,
431};
432
433inline Qualifiers operator|=(Qualifiers &Q1, Qualifiers Q2) {
434 return Q1 = static_cast<Qualifiers>(Q1 | Q2);
435}
436
Richard Smithdf1c14c2019-09-06 23:53:21 +0000437class QualType final : public Node {
Richard Smithc20d1442018-08-20 20:14:49 +0000438protected:
439 const Qualifiers Quals;
440 const Node *Child;
441
442 void printQuals(OutputStream &S) const {
443 if (Quals & QualConst)
444 S += " const";
445 if (Quals & QualVolatile)
446 S += " volatile";
447 if (Quals & QualRestrict)
448 S += " restrict";
449 }
450
451public:
452 QualType(const Node *Child_, Qualifiers Quals_)
453 : Node(KQualType, Child_->RHSComponentCache,
454 Child_->ArrayCache, Child_->FunctionCache),
455 Quals(Quals_), Child(Child_) {}
456
457 template<typename Fn> void match(Fn F) const { F(Child, Quals); }
458
459 bool hasRHSComponentSlow(OutputStream &S) const override {
460 return Child->hasRHSComponent(S);
461 }
462 bool hasArraySlow(OutputStream &S) const override {
463 return Child->hasArray(S);
464 }
465 bool hasFunctionSlow(OutputStream &S) const override {
466 return Child->hasFunction(S);
467 }
468
469 void printLeft(OutputStream &S) const override {
470 Child->printLeft(S);
471 printQuals(S);
472 }
473
474 void printRight(OutputStream &S) const override { Child->printRight(S); }
475};
476
477class ConversionOperatorType final : public Node {
478 const Node *Ty;
479
480public:
481 ConversionOperatorType(const Node *Ty_)
482 : Node(KConversionOperatorType), Ty(Ty_) {}
483
484 template<typename Fn> void match(Fn F) const { F(Ty); }
485
486 void printLeft(OutputStream &S) const override {
487 S += "operator ";
488 Ty->print(S);
489 }
490};
491
492class PostfixQualifiedType final : public Node {
493 const Node *Ty;
494 const StringView Postfix;
495
496public:
497 PostfixQualifiedType(Node *Ty_, StringView Postfix_)
498 : Node(KPostfixQualifiedType), Ty(Ty_), Postfix(Postfix_) {}
499
500 template<typename Fn> void match(Fn F) const { F(Ty, Postfix); }
501
502 void printLeft(OutputStream &s) const override {
503 Ty->printLeft(s);
504 s += Postfix;
505 }
506};
507
508class NameType final : public Node {
509 const StringView Name;
510
511public:
512 NameType(StringView Name_) : Node(KNameType), Name(Name_) {}
513
514 template<typename Fn> void match(Fn F) const { F(Name); }
515
516 StringView getName() const { return Name; }
517 StringView getBaseName() const override { return Name; }
518
519 void printLeft(OutputStream &s) const override { s += Name; }
520};
521
522class ElaboratedTypeSpefType : public Node {
523 StringView Kind;
524 Node *Child;
525public:
526 ElaboratedTypeSpefType(StringView Kind_, Node *Child_)
527 : Node(KElaboratedTypeSpefType), Kind(Kind_), Child(Child_) {}
528
529 template<typename Fn> void match(Fn F) const { F(Kind, Child); }
530
531 void printLeft(OutputStream &S) const override {
532 S += Kind;
533 S += ' ';
534 Child->print(S);
535 }
536};
537
538struct AbiTagAttr : Node {
539 Node *Base;
540 StringView Tag;
541
542 AbiTagAttr(Node* Base_, StringView Tag_)
543 : Node(KAbiTagAttr, Base_->RHSComponentCache,
544 Base_->ArrayCache, Base_->FunctionCache),
545 Base(Base_), Tag(Tag_) {}
546
547 template<typename Fn> void match(Fn F) const { F(Base, Tag); }
548
549 void printLeft(OutputStream &S) const override {
550 Base->printLeft(S);
551 S += "[abi:";
552 S += Tag;
553 S += "]";
554 }
555};
556
557class EnableIfAttr : public Node {
558 NodeArray Conditions;
559public:
560 EnableIfAttr(NodeArray Conditions_)
561 : Node(KEnableIfAttr), Conditions(Conditions_) {}
562
563 template<typename Fn> void match(Fn F) const { F(Conditions); }
564
565 void printLeft(OutputStream &S) const override {
566 S += " [enable_if:";
567 Conditions.printWithComma(S);
568 S += ']';
569 }
570};
571
572class ObjCProtoName : public Node {
573 const Node *Ty;
574 StringView Protocol;
575
576 friend class PointerType;
577
578public:
579 ObjCProtoName(const Node *Ty_, StringView Protocol_)
580 : Node(KObjCProtoName), Ty(Ty_), Protocol(Protocol_) {}
581
582 template<typename Fn> void match(Fn F) const { F(Ty, Protocol); }
583
584 bool isObjCObject() const {
585 return Ty->getKind() == KNameType &&
586 static_cast<const NameType *>(Ty)->getName() == "objc_object";
587 }
588
589 void printLeft(OutputStream &S) const override {
590 Ty->print(S);
591 S += "<";
592 S += Protocol;
593 S += ">";
594 }
595};
596
597class PointerType final : public Node {
598 const Node *Pointee;
599
600public:
601 PointerType(const Node *Pointee_)
602 : Node(KPointerType, Pointee_->RHSComponentCache),
603 Pointee(Pointee_) {}
604
605 template<typename Fn> void match(Fn F) const { F(Pointee); }
606
607 bool hasRHSComponentSlow(OutputStream &S) const override {
608 return Pointee->hasRHSComponent(S);
609 }
610
611 void printLeft(OutputStream &s) const override {
612 // We rewrite objc_object<SomeProtocol>* into id<SomeProtocol>.
613 if (Pointee->getKind() != KObjCProtoName ||
614 !static_cast<const ObjCProtoName *>(Pointee)->isObjCObject()) {
615 Pointee->printLeft(s);
616 if (Pointee->hasArray(s))
617 s += " ";
618 if (Pointee->hasArray(s) || Pointee->hasFunction(s))
619 s += "(";
620 s += "*";
621 } else {
622 const auto *objcProto = static_cast<const ObjCProtoName *>(Pointee);
623 s += "id<";
624 s += objcProto->Protocol;
625 s += ">";
626 }
627 }
628
629 void printRight(OutputStream &s) const override {
630 if (Pointee->getKind() != KObjCProtoName ||
631 !static_cast<const ObjCProtoName *>(Pointee)->isObjCObject()) {
632 if (Pointee->hasArray(s) || Pointee->hasFunction(s))
633 s += ")";
634 Pointee->printRight(s);
635 }
636 }
637};
638
639enum class ReferenceKind {
640 LValue,
641 RValue,
642};
643
644// Represents either a LValue or an RValue reference type.
645class ReferenceType : public Node {
646 const Node *Pointee;
647 ReferenceKind RK;
648
649 mutable bool Printing = false;
650
651 // Dig through any refs to refs, collapsing the ReferenceTypes as we go. The
652 // rule here is rvalue ref to rvalue ref collapses to a rvalue ref, and any
653 // other combination collapses to a lvalue ref.
Mikhail Borisov05f77222021-08-17 18:10:57 -0400654 //
655 // A combination of a TemplateForwardReference and a back-ref Substitution
656 // from an ill-formed string may have created a cycle; use cycle detection to
657 // avoid looping forever.
Richard Smithc20d1442018-08-20 20:14:49 +0000658 std::pair<ReferenceKind, const Node *> collapse(OutputStream &S) const {
659 auto SoFar = std::make_pair(RK, Pointee);
Mikhail Borisov05f77222021-08-17 18:10:57 -0400660 // Track the chain of nodes for the Floyd's 'tortoise and hare'
661 // cycle-detection algorithm, since getSyntaxNode(S) is impure
662 PODSmallVector<const Node *, 8> Prev;
Richard Smithc20d1442018-08-20 20:14:49 +0000663 for (;;) {
664 const Node *SN = SoFar.second->getSyntaxNode(S);
665 if (SN->getKind() != KReferenceType)
666 break;
667 auto *RT = static_cast<const ReferenceType *>(SN);
668 SoFar.second = RT->Pointee;
669 SoFar.first = std::min(SoFar.first, RT->RK);
Mikhail Borisov05f77222021-08-17 18:10:57 -0400670
671 // The middle of Prev is the 'slow' pointer moving at half speed
672 Prev.push_back(SoFar.second);
673 if (Prev.size() > 1 && SoFar.second == Prev[(Prev.size() - 1) / 2]) {
674 // Cycle detected
675 SoFar.second = nullptr;
676 break;
677 }
Richard Smithc20d1442018-08-20 20:14:49 +0000678 }
679 return SoFar;
680 }
681
682public:
683 ReferenceType(const Node *Pointee_, ReferenceKind RK_)
684 : Node(KReferenceType, Pointee_->RHSComponentCache),
685 Pointee(Pointee_), RK(RK_) {}
686
687 template<typename Fn> void match(Fn F) const { F(Pointee, RK); }
688
689 bool hasRHSComponentSlow(OutputStream &S) const override {
690 return Pointee->hasRHSComponent(S);
691 }
692
693 void printLeft(OutputStream &s) const override {
694 if (Printing)
695 return;
696 SwapAndRestore<bool> SavePrinting(Printing, true);
697 std::pair<ReferenceKind, const Node *> Collapsed = collapse(s);
Mikhail Borisov05f77222021-08-17 18:10:57 -0400698 if (!Collapsed.second)
699 return;
Richard Smithc20d1442018-08-20 20:14:49 +0000700 Collapsed.second->printLeft(s);
701 if (Collapsed.second->hasArray(s))
702 s += " ";
703 if (Collapsed.second->hasArray(s) || Collapsed.second->hasFunction(s))
704 s += "(";
705
706 s += (Collapsed.first == ReferenceKind::LValue ? "&" : "&&");
707 }
708 void printRight(OutputStream &s) const override {
709 if (Printing)
710 return;
711 SwapAndRestore<bool> SavePrinting(Printing, true);
712 std::pair<ReferenceKind, const Node *> Collapsed = collapse(s);
Mikhail Borisov05f77222021-08-17 18:10:57 -0400713 if (!Collapsed.second)
714 return;
Richard Smithc20d1442018-08-20 20:14:49 +0000715 if (Collapsed.second->hasArray(s) || Collapsed.second->hasFunction(s))
716 s += ")";
717 Collapsed.second->printRight(s);
718 }
719};
720
721class PointerToMemberType final : public Node {
722 const Node *ClassType;
723 const Node *MemberType;
724
725public:
726 PointerToMemberType(const Node *ClassType_, const Node *MemberType_)
727 : Node(KPointerToMemberType, MemberType_->RHSComponentCache),
728 ClassType(ClassType_), MemberType(MemberType_) {}
729
730 template<typename Fn> void match(Fn F) const { F(ClassType, MemberType); }
731
732 bool hasRHSComponentSlow(OutputStream &S) const override {
733 return MemberType->hasRHSComponent(S);
734 }
735
736 void printLeft(OutputStream &s) const override {
737 MemberType->printLeft(s);
738 if (MemberType->hasArray(s) || MemberType->hasFunction(s))
739 s += "(";
740 else
741 s += " ";
742 ClassType->print(s);
743 s += "::*";
744 }
745
746 void printRight(OutputStream &s) const override {
747 if (MemberType->hasArray(s) || MemberType->hasFunction(s))
748 s += ")";
749 MemberType->printRight(s);
750 }
751};
752
Richard Smithc20d1442018-08-20 20:14:49 +0000753class ArrayType final : public Node {
754 const Node *Base;
Erik Pilkingtond7555e32019-11-04 10:47:44 -0800755 Node *Dimension;
Richard Smithc20d1442018-08-20 20:14:49 +0000756
757public:
Erik Pilkingtond7555e32019-11-04 10:47:44 -0800758 ArrayType(const Node *Base_, Node *Dimension_)
Richard Smithc20d1442018-08-20 20:14:49 +0000759 : Node(KArrayType,
760 /*RHSComponentCache=*/Cache::Yes,
761 /*ArrayCache=*/Cache::Yes),
762 Base(Base_), Dimension(Dimension_) {}
763
764 template<typename Fn> void match(Fn F) const { F(Base, Dimension); }
765
766 bool hasRHSComponentSlow(OutputStream &) const override { return true; }
767 bool hasArraySlow(OutputStream &) const override { return true; }
768
769 void printLeft(OutputStream &S) const override { Base->printLeft(S); }
770
771 void printRight(OutputStream &S) const override {
772 if (S.back() != ']')
773 S += " ";
774 S += "[";
Erik Pilkingtond7555e32019-11-04 10:47:44 -0800775 if (Dimension)
776 Dimension->print(S);
Richard Smithc20d1442018-08-20 20:14:49 +0000777 S += "]";
778 Base->printRight(S);
779 }
780};
781
782class FunctionType final : public Node {
783 const Node *Ret;
784 NodeArray Params;
785 Qualifiers CVQuals;
786 FunctionRefQual RefQual;
787 const Node *ExceptionSpec;
788
789public:
790 FunctionType(const Node *Ret_, NodeArray Params_, Qualifiers CVQuals_,
791 FunctionRefQual RefQual_, const Node *ExceptionSpec_)
792 : Node(KFunctionType,
793 /*RHSComponentCache=*/Cache::Yes, /*ArrayCache=*/Cache::No,
794 /*FunctionCache=*/Cache::Yes),
795 Ret(Ret_), Params(Params_), CVQuals(CVQuals_), RefQual(RefQual_),
796 ExceptionSpec(ExceptionSpec_) {}
797
798 template<typename Fn> void match(Fn F) const {
799 F(Ret, Params, CVQuals, RefQual, ExceptionSpec);
800 }
801
802 bool hasRHSComponentSlow(OutputStream &) const override { return true; }
803 bool hasFunctionSlow(OutputStream &) const override { return true; }
804
805 // Handle C++'s ... quirky decl grammar by using the left & right
806 // distinction. Consider:
807 // int (*f(float))(char) {}
808 // f is a function that takes a float and returns a pointer to a function
809 // that takes a char and returns an int. If we're trying to print f, start
810 // by printing out the return types's left, then print our parameters, then
811 // finally print right of the return type.
812 void printLeft(OutputStream &S) const override {
813 Ret->printLeft(S);
814 S += " ";
815 }
816
817 void printRight(OutputStream &S) const override {
818 S += "(";
819 Params.printWithComma(S);
820 S += ")";
821 Ret->printRight(S);
822
823 if (CVQuals & QualConst)
824 S += " const";
825 if (CVQuals & QualVolatile)
826 S += " volatile";
827 if (CVQuals & QualRestrict)
828 S += " restrict";
829
830 if (RefQual == FrefQualLValue)
831 S += " &";
832 else if (RefQual == FrefQualRValue)
833 S += " &&";
834
835 if (ExceptionSpec != nullptr) {
836 S += ' ';
837 ExceptionSpec->print(S);
838 }
839 }
840};
841
842class NoexceptSpec : public Node {
843 const Node *E;
844public:
845 NoexceptSpec(const Node *E_) : Node(KNoexceptSpec), E(E_) {}
846
847 template<typename Fn> void match(Fn F) const { F(E); }
848
849 void printLeft(OutputStream &S) const override {
850 S += "noexcept(";
851 E->print(S);
852 S += ")";
853 }
854};
855
856class DynamicExceptionSpec : public Node {
857 NodeArray Types;
858public:
859 DynamicExceptionSpec(NodeArray Types_)
860 : Node(KDynamicExceptionSpec), Types(Types_) {}
861
862 template<typename Fn> void match(Fn F) const { F(Types); }
863
864 void printLeft(OutputStream &S) const override {
865 S += "throw(";
866 Types.printWithComma(S);
867 S += ')';
868 }
869};
870
871class FunctionEncoding final : public Node {
872 const Node *Ret;
873 const Node *Name;
874 NodeArray Params;
875 const Node *Attrs;
876 Qualifiers CVQuals;
877 FunctionRefQual RefQual;
878
879public:
880 FunctionEncoding(const Node *Ret_, const Node *Name_, NodeArray Params_,
881 const Node *Attrs_, Qualifiers CVQuals_,
882 FunctionRefQual RefQual_)
883 : Node(KFunctionEncoding,
884 /*RHSComponentCache=*/Cache::Yes, /*ArrayCache=*/Cache::No,
885 /*FunctionCache=*/Cache::Yes),
886 Ret(Ret_), Name(Name_), Params(Params_), Attrs(Attrs_),
887 CVQuals(CVQuals_), RefQual(RefQual_) {}
888
889 template<typename Fn> void match(Fn F) const {
890 F(Ret, Name, Params, Attrs, CVQuals, RefQual);
891 }
892
893 Qualifiers getCVQuals() const { return CVQuals; }
894 FunctionRefQual getRefQual() const { return RefQual; }
895 NodeArray getParams() const { return Params; }
896 const Node *getReturnType() const { return Ret; }
897
898 bool hasRHSComponentSlow(OutputStream &) const override { return true; }
899 bool hasFunctionSlow(OutputStream &) const override { return true; }
900
901 const Node *getName() const { return Name; }
902
903 void printLeft(OutputStream &S) const override {
904 if (Ret) {
905 Ret->printLeft(S);
906 if (!Ret->hasRHSComponent(S))
907 S += " ";
908 }
909 Name->print(S);
910 }
911
912 void printRight(OutputStream &S) const override {
913 S += "(";
914 Params.printWithComma(S);
915 S += ")";
916 if (Ret)
917 Ret->printRight(S);
918
919 if (CVQuals & QualConst)
920 S += " const";
921 if (CVQuals & QualVolatile)
922 S += " volatile";
923 if (CVQuals & QualRestrict)
924 S += " restrict";
925
926 if (RefQual == FrefQualLValue)
927 S += " &";
928 else if (RefQual == FrefQualRValue)
929 S += " &&";
930
931 if (Attrs != nullptr)
932 Attrs->print(S);
933 }
934};
935
936class LiteralOperator : public Node {
937 const Node *OpName;
938
939public:
940 LiteralOperator(const Node *OpName_)
941 : Node(KLiteralOperator), OpName(OpName_) {}
942
943 template<typename Fn> void match(Fn F) const { F(OpName); }
944
945 void printLeft(OutputStream &S) const override {
946 S += "operator\"\" ";
947 OpName->print(S);
948 }
949};
950
951class SpecialName final : public Node {
952 const StringView Special;
953 const Node *Child;
954
955public:
956 SpecialName(StringView Special_, const Node *Child_)
957 : Node(KSpecialName), Special(Special_), Child(Child_) {}
958
959 template<typename Fn> void match(Fn F) const { F(Special, Child); }
960
961 void printLeft(OutputStream &S) const override {
962 S += Special;
963 Child->print(S);
964 }
965};
966
967class CtorVtableSpecialName final : public Node {
968 const Node *FirstType;
969 const Node *SecondType;
970
971public:
972 CtorVtableSpecialName(const Node *FirstType_, const Node *SecondType_)
973 : Node(KCtorVtableSpecialName),
974 FirstType(FirstType_), SecondType(SecondType_) {}
975
976 template<typename Fn> void match(Fn F) const { F(FirstType, SecondType); }
977
978 void printLeft(OutputStream &S) const override {
979 S += "construction vtable for ";
980 FirstType->print(S);
981 S += "-in-";
982 SecondType->print(S);
983 }
984};
985
986struct NestedName : Node {
987 Node *Qual;
988 Node *Name;
989
990 NestedName(Node *Qual_, Node *Name_)
991 : Node(KNestedName), Qual(Qual_), Name(Name_) {}
992
993 template<typename Fn> void match(Fn F) const { F(Qual, Name); }
994
995 StringView getBaseName() const override { return Name->getBaseName(); }
996
997 void printLeft(OutputStream &S) const override {
998 Qual->print(S);
999 S += "::";
1000 Name->print(S);
1001 }
1002};
1003
1004struct LocalName : Node {
1005 Node *Encoding;
1006 Node *Entity;
1007
1008 LocalName(Node *Encoding_, Node *Entity_)
1009 : Node(KLocalName), Encoding(Encoding_), Entity(Entity_) {}
1010
1011 template<typename Fn> void match(Fn F) const { F(Encoding, Entity); }
1012
1013 void printLeft(OutputStream &S) const override {
1014 Encoding->print(S);
1015 S += "::";
1016 Entity->print(S);
1017 }
1018};
1019
1020class QualifiedName final : public Node {
1021 // qualifier::name
1022 const Node *Qualifier;
1023 const Node *Name;
1024
1025public:
1026 QualifiedName(const Node *Qualifier_, const Node *Name_)
1027 : Node(KQualifiedName), Qualifier(Qualifier_), Name(Name_) {}
1028
1029 template<typename Fn> void match(Fn F) const { F(Qualifier, Name); }
1030
1031 StringView getBaseName() const override { return Name->getBaseName(); }
1032
1033 void printLeft(OutputStream &S) const override {
1034 Qualifier->print(S);
1035 S += "::";
1036 Name->print(S);
1037 }
1038};
1039
1040class VectorType final : public Node {
1041 const Node *BaseType;
Erik Pilkingtond7555e32019-11-04 10:47:44 -08001042 const Node *Dimension;
Richard Smithc20d1442018-08-20 20:14:49 +00001043
1044public:
Erik Pilkingtond7555e32019-11-04 10:47:44 -08001045 VectorType(const Node *BaseType_, Node *Dimension_)
Richard Smithc20d1442018-08-20 20:14:49 +00001046 : Node(KVectorType), BaseType(BaseType_),
1047 Dimension(Dimension_) {}
1048
1049 template<typename Fn> void match(Fn F) const { F(BaseType, Dimension); }
1050
1051 void printLeft(OutputStream &S) const override {
1052 BaseType->print(S);
1053 S += " vector[";
Erik Pilkingtond7555e32019-11-04 10:47:44 -08001054 if (Dimension)
1055 Dimension->print(S);
Richard Smithc20d1442018-08-20 20:14:49 +00001056 S += "]";
1057 }
1058};
1059
1060class PixelVectorType final : public Node {
Erik Pilkingtond7555e32019-11-04 10:47:44 -08001061 const Node *Dimension;
Richard Smithc20d1442018-08-20 20:14:49 +00001062
1063public:
Erik Pilkingtond7555e32019-11-04 10:47:44 -08001064 PixelVectorType(const Node *Dimension_)
Richard Smithc20d1442018-08-20 20:14:49 +00001065 : Node(KPixelVectorType), Dimension(Dimension_) {}
1066
1067 template<typename Fn> void match(Fn F) const { F(Dimension); }
1068
1069 void printLeft(OutputStream &S) const override {
1070 // FIXME: This should demangle as "vector pixel".
1071 S += "pixel vector[";
Erik Pilkingtond7555e32019-11-04 10:47:44 -08001072 Dimension->print(S);
Richard Smithc20d1442018-08-20 20:14:49 +00001073 S += "]";
1074 }
1075};
1076
Richard Smithdf1c14c2019-09-06 23:53:21 +00001077enum class TemplateParamKind { Type, NonType, Template };
1078
1079/// An invented name for a template parameter for which we don't have a
1080/// corresponding template argument.
1081///
1082/// This node is created when parsing the <lambda-sig> for a lambda with
1083/// explicit template arguments, which might be referenced in the parameter
1084/// types appearing later in the <lambda-sig>.
1085class SyntheticTemplateParamName final : public Node {
1086 TemplateParamKind Kind;
1087 unsigned Index;
1088
1089public:
1090 SyntheticTemplateParamName(TemplateParamKind Kind_, unsigned Index_)
1091 : Node(KSyntheticTemplateParamName), Kind(Kind_), Index(Index_) {}
1092
1093 template<typename Fn> void match(Fn F) const { F(Kind, Index); }
1094
1095 void printLeft(OutputStream &S) const override {
1096 switch (Kind) {
1097 case TemplateParamKind::Type:
1098 S += "$T";
1099 break;
1100 case TemplateParamKind::NonType:
1101 S += "$N";
1102 break;
1103 case TemplateParamKind::Template:
1104 S += "$TT";
1105 break;
1106 }
1107 if (Index > 0)
1108 S << Index - 1;
1109 }
1110};
1111
1112/// A template type parameter declaration, 'typename T'.
1113class TypeTemplateParamDecl final : public Node {
1114 Node *Name;
1115
1116public:
1117 TypeTemplateParamDecl(Node *Name_)
1118 : Node(KTypeTemplateParamDecl, Cache::Yes), Name(Name_) {}
1119
1120 template<typename Fn> void match(Fn F) const { F(Name); }
1121
1122 void printLeft(OutputStream &S) const override {
1123 S += "typename ";
1124 }
1125
1126 void printRight(OutputStream &S) const override {
1127 Name->print(S);
1128 }
1129};
1130
1131/// A non-type template parameter declaration, 'int N'.
1132class NonTypeTemplateParamDecl final : public Node {
1133 Node *Name;
1134 Node *Type;
1135
1136public:
1137 NonTypeTemplateParamDecl(Node *Name_, Node *Type_)
1138 : Node(KNonTypeTemplateParamDecl, Cache::Yes), Name(Name_), Type(Type_) {}
1139
1140 template<typename Fn> void match(Fn F) const { F(Name, Type); }
1141
1142 void printLeft(OutputStream &S) const override {
1143 Type->printLeft(S);
1144 if (!Type->hasRHSComponent(S))
1145 S += " ";
1146 }
1147
1148 void printRight(OutputStream &S) const override {
1149 Name->print(S);
1150 Type->printRight(S);
1151 }
1152};
1153
1154/// A template template parameter declaration,
1155/// 'template<typename T> typename N'.
1156class TemplateTemplateParamDecl final : public Node {
1157 Node *Name;
1158 NodeArray Params;
1159
1160public:
1161 TemplateTemplateParamDecl(Node *Name_, NodeArray Params_)
1162 : Node(KTemplateTemplateParamDecl, Cache::Yes), Name(Name_),
1163 Params(Params_) {}
1164
1165 template<typename Fn> void match(Fn F) const { F(Name, Params); }
1166
1167 void printLeft(OutputStream &S) const override {
1168 S += "template<";
1169 Params.printWithComma(S);
1170 S += "> typename ";
1171 }
1172
1173 void printRight(OutputStream &S) const override {
1174 Name->print(S);
1175 }
1176};
1177
1178/// A template parameter pack declaration, 'typename ...T'.
1179class TemplateParamPackDecl final : public Node {
1180 Node *Param;
1181
1182public:
1183 TemplateParamPackDecl(Node *Param_)
1184 : Node(KTemplateParamPackDecl, Cache::Yes), Param(Param_) {}
1185
1186 template<typename Fn> void match(Fn F) const { F(Param); }
1187
1188 void printLeft(OutputStream &S) const override {
1189 Param->printLeft(S);
1190 S += "...";
1191 }
1192
1193 void printRight(OutputStream &S) const override {
1194 Param->printRight(S);
1195 }
1196};
1197
Richard Smithc20d1442018-08-20 20:14:49 +00001198/// An unexpanded parameter pack (either in the expression or type context). If
1199/// this AST is correct, this node will have a ParameterPackExpansion node above
1200/// it.
1201///
1202/// This node is created when some <template-args> are found that apply to an
1203/// <encoding>, and is stored in the TemplateParams table. In order for this to
1204/// appear in the final AST, it has to referenced via a <template-param> (ie,
1205/// T_).
1206class ParameterPack final : public Node {
1207 NodeArray Data;
1208
1209 // Setup OutputStream for a pack expansion unless we're already expanding one.
1210 void initializePackExpansion(OutputStream &S) const {
1211 if (S.CurrentPackMax == std::numeric_limits<unsigned>::max()) {
1212 S.CurrentPackMax = static_cast<unsigned>(Data.size());
1213 S.CurrentPackIndex = 0;
1214 }
1215 }
1216
1217public:
1218 ParameterPack(NodeArray Data_) : Node(KParameterPack), Data(Data_) {
1219 ArrayCache = FunctionCache = RHSComponentCache = Cache::Unknown;
1220 if (std::all_of(Data.begin(), Data.end(), [](Node* P) {
1221 return P->ArrayCache == Cache::No;
1222 }))
1223 ArrayCache = Cache::No;
1224 if (std::all_of(Data.begin(), Data.end(), [](Node* P) {
1225 return P->FunctionCache == Cache::No;
1226 }))
1227 FunctionCache = Cache::No;
1228 if (std::all_of(Data.begin(), Data.end(), [](Node* P) {
1229 return P->RHSComponentCache == Cache::No;
1230 }))
1231 RHSComponentCache = Cache::No;
1232 }
1233
1234 template<typename Fn> void match(Fn F) const { F(Data); }
1235
1236 bool hasRHSComponentSlow(OutputStream &S) const override {
1237 initializePackExpansion(S);
1238 size_t Idx = S.CurrentPackIndex;
1239 return Idx < Data.size() && Data[Idx]->hasRHSComponent(S);
1240 }
1241 bool hasArraySlow(OutputStream &S) const override {
1242 initializePackExpansion(S);
1243 size_t Idx = S.CurrentPackIndex;
1244 return Idx < Data.size() && Data[Idx]->hasArray(S);
1245 }
1246 bool hasFunctionSlow(OutputStream &S) const override {
1247 initializePackExpansion(S);
1248 size_t Idx = S.CurrentPackIndex;
1249 return Idx < Data.size() && Data[Idx]->hasFunction(S);
1250 }
1251 const Node *getSyntaxNode(OutputStream &S) const override {
1252 initializePackExpansion(S);
1253 size_t Idx = S.CurrentPackIndex;
1254 return Idx < Data.size() ? Data[Idx]->getSyntaxNode(S) : this;
1255 }
1256
1257 void printLeft(OutputStream &S) const override {
1258 initializePackExpansion(S);
1259 size_t Idx = S.CurrentPackIndex;
1260 if (Idx < Data.size())
1261 Data[Idx]->printLeft(S);
1262 }
1263 void printRight(OutputStream &S) const override {
1264 initializePackExpansion(S);
1265 size_t Idx = S.CurrentPackIndex;
1266 if (Idx < Data.size())
1267 Data[Idx]->printRight(S);
1268 }
1269};
1270
1271/// A variadic template argument. This node represents an occurrence of
1272/// J<something>E in some <template-args>. It isn't itself unexpanded, unless
1273/// one of it's Elements is. The parser inserts a ParameterPack into the
1274/// TemplateParams table if the <template-args> this pack belongs to apply to an
1275/// <encoding>.
1276class TemplateArgumentPack final : public Node {
1277 NodeArray Elements;
1278public:
1279 TemplateArgumentPack(NodeArray Elements_)
1280 : Node(KTemplateArgumentPack), Elements(Elements_) {}
1281
1282 template<typename Fn> void match(Fn F) const { F(Elements); }
1283
1284 NodeArray getElements() const { return Elements; }
1285
1286 void printLeft(OutputStream &S) const override {
1287 Elements.printWithComma(S);
1288 }
1289};
1290
1291/// A pack expansion. Below this node, there are some unexpanded ParameterPacks
1292/// which each have Child->ParameterPackSize elements.
1293class ParameterPackExpansion final : public Node {
1294 const Node *Child;
1295
1296public:
1297 ParameterPackExpansion(const Node *Child_)
1298 : Node(KParameterPackExpansion), Child(Child_) {}
1299
1300 template<typename Fn> void match(Fn F) const { F(Child); }
1301
1302 const Node *getChild() const { return Child; }
1303
1304 void printLeft(OutputStream &S) const override {
1305 constexpr unsigned Max = std::numeric_limits<unsigned>::max();
1306 SwapAndRestore<unsigned> SavePackIdx(S.CurrentPackIndex, Max);
1307 SwapAndRestore<unsigned> SavePackMax(S.CurrentPackMax, Max);
1308 size_t StreamPos = S.getCurrentPosition();
1309
1310 // Print the first element in the pack. If Child contains a ParameterPack,
1311 // it will set up S.CurrentPackMax and print the first element.
1312 Child->print(S);
1313
1314 // No ParameterPack was found in Child. This can occur if we've found a pack
1315 // expansion on a <function-param>.
1316 if (S.CurrentPackMax == Max) {
1317 S += "...";
1318 return;
1319 }
1320
1321 // We found a ParameterPack, but it has no elements. Erase whatever we may
1322 // of printed.
1323 if (S.CurrentPackMax == 0) {
1324 S.setCurrentPosition(StreamPos);
1325 return;
1326 }
1327
1328 // Else, iterate through the rest of the elements in the pack.
1329 for (unsigned I = 1, E = S.CurrentPackMax; I < E; ++I) {
1330 S += ", ";
1331 S.CurrentPackIndex = I;
1332 Child->print(S);
1333 }
1334 }
1335};
1336
1337class TemplateArgs final : public Node {
1338 NodeArray Params;
1339
1340public:
1341 TemplateArgs(NodeArray Params_) : Node(KTemplateArgs), Params(Params_) {}
1342
1343 template<typename Fn> void match(Fn F) const { F(Params); }
1344
1345 NodeArray getParams() { return Params; }
1346
1347 void printLeft(OutputStream &S) const override {
1348 S += "<";
1349 Params.printWithComma(S);
1350 if (S.back() == '>')
1351 S += " ";
1352 S += ">";
1353 }
1354};
1355
Richard Smithb485b352018-08-24 23:30:26 +00001356/// A forward-reference to a template argument that was not known at the point
1357/// where the template parameter name was parsed in a mangling.
1358///
1359/// This is created when demangling the name of a specialization of a
1360/// conversion function template:
1361///
1362/// \code
1363/// struct A {
1364/// template<typename T> operator T*();
1365/// };
1366/// \endcode
1367///
1368/// When demangling a specialization of the conversion function template, we
1369/// encounter the name of the template (including the \c T) before we reach
1370/// the template argument list, so we cannot substitute the parameter name
1371/// for the corresponding argument while parsing. Instead, we create a
1372/// \c ForwardTemplateReference node that is resolved after we parse the
1373/// template arguments.
Richard Smithc20d1442018-08-20 20:14:49 +00001374struct ForwardTemplateReference : Node {
1375 size_t Index;
1376 Node *Ref = nullptr;
1377
1378 // If we're currently printing this node. It is possible (though invalid) for
1379 // a forward template reference to refer to itself via a substitution. This
1380 // creates a cyclic AST, which will stack overflow printing. To fix this, bail
1381 // out if more than one print* function is active.
1382 mutable bool Printing = false;
1383
1384 ForwardTemplateReference(size_t Index_)
1385 : Node(KForwardTemplateReference, Cache::Unknown, Cache::Unknown,
1386 Cache::Unknown),
1387 Index(Index_) {}
1388
1389 // We don't provide a matcher for these, because the value of the node is
1390 // not determined by its construction parameters, and it generally needs
1391 // special handling.
1392 template<typename Fn> void match(Fn F) const = delete;
1393
1394 bool hasRHSComponentSlow(OutputStream &S) const override {
1395 if (Printing)
1396 return false;
1397 SwapAndRestore<bool> SavePrinting(Printing, true);
1398 return Ref->hasRHSComponent(S);
1399 }
1400 bool hasArraySlow(OutputStream &S) const override {
1401 if (Printing)
1402 return false;
1403 SwapAndRestore<bool> SavePrinting(Printing, true);
1404 return Ref->hasArray(S);
1405 }
1406 bool hasFunctionSlow(OutputStream &S) const override {
1407 if (Printing)
1408 return false;
1409 SwapAndRestore<bool> SavePrinting(Printing, true);
1410 return Ref->hasFunction(S);
1411 }
1412 const Node *getSyntaxNode(OutputStream &S) const override {
1413 if (Printing)
1414 return this;
1415 SwapAndRestore<bool> SavePrinting(Printing, true);
1416 return Ref->getSyntaxNode(S);
1417 }
1418
1419 void printLeft(OutputStream &S) const override {
1420 if (Printing)
1421 return;
1422 SwapAndRestore<bool> SavePrinting(Printing, true);
1423 Ref->printLeft(S);
1424 }
1425 void printRight(OutputStream &S) const override {
1426 if (Printing)
1427 return;
1428 SwapAndRestore<bool> SavePrinting(Printing, true);
1429 Ref->printRight(S);
1430 }
1431};
1432
1433struct NameWithTemplateArgs : Node {
1434 // name<template_args>
1435 Node *Name;
1436 Node *TemplateArgs;
1437
1438 NameWithTemplateArgs(Node *Name_, Node *TemplateArgs_)
1439 : Node(KNameWithTemplateArgs), Name(Name_), TemplateArgs(TemplateArgs_) {}
1440
1441 template<typename Fn> void match(Fn F) const { F(Name, TemplateArgs); }
1442
1443 StringView getBaseName() const override { return Name->getBaseName(); }
1444
1445 void printLeft(OutputStream &S) const override {
1446 Name->print(S);
1447 TemplateArgs->print(S);
1448 }
1449};
1450
1451class GlobalQualifiedName final : public Node {
1452 Node *Child;
1453
1454public:
1455 GlobalQualifiedName(Node* Child_)
1456 : Node(KGlobalQualifiedName), Child(Child_) {}
1457
1458 template<typename Fn> void match(Fn F) const { F(Child); }
1459
1460 StringView getBaseName() const override { return Child->getBaseName(); }
1461
1462 void printLeft(OutputStream &S) const override {
1463 S += "::";
1464 Child->print(S);
1465 }
1466};
1467
1468struct StdQualifiedName : Node {
1469 Node *Child;
1470
1471 StdQualifiedName(Node *Child_) : Node(KStdQualifiedName), Child(Child_) {}
1472
1473 template<typename Fn> void match(Fn F) const { F(Child); }
1474
1475 StringView getBaseName() const override { return Child->getBaseName(); }
1476
1477 void printLeft(OutputStream &S) const override {
1478 S += "std::";
1479 Child->print(S);
1480 }
1481};
1482
1483enum class SpecialSubKind {
1484 allocator,
1485 basic_string,
1486 string,
1487 istream,
1488 ostream,
1489 iostream,
1490};
1491
1492class ExpandedSpecialSubstitution final : public Node {
1493 SpecialSubKind SSK;
1494
1495public:
1496 ExpandedSpecialSubstitution(SpecialSubKind SSK_)
1497 : Node(KExpandedSpecialSubstitution), SSK(SSK_) {}
1498
1499 template<typename Fn> void match(Fn F) const { F(SSK); }
1500
1501 StringView getBaseName() const override {
1502 switch (SSK) {
1503 case SpecialSubKind::allocator:
1504 return StringView("allocator");
1505 case SpecialSubKind::basic_string:
1506 return StringView("basic_string");
1507 case SpecialSubKind::string:
1508 return StringView("basic_string");
1509 case SpecialSubKind::istream:
1510 return StringView("basic_istream");
1511 case SpecialSubKind::ostream:
1512 return StringView("basic_ostream");
1513 case SpecialSubKind::iostream:
1514 return StringView("basic_iostream");
1515 }
Erik Pilkingtonf70e4d82019-01-17 20:37:51 +00001516 DEMANGLE_UNREACHABLE;
Richard Smithc20d1442018-08-20 20:14:49 +00001517 }
1518
1519 void printLeft(OutputStream &S) const override {
1520 switch (SSK) {
1521 case SpecialSubKind::allocator:
Richard Smithb485b352018-08-24 23:30:26 +00001522 S += "std::allocator";
Richard Smithc20d1442018-08-20 20:14:49 +00001523 break;
1524 case SpecialSubKind::basic_string:
Richard Smithb485b352018-08-24 23:30:26 +00001525 S += "std::basic_string";
1526 break;
Richard Smithc20d1442018-08-20 20:14:49 +00001527 case SpecialSubKind::string:
1528 S += "std::basic_string<char, std::char_traits<char>, "
1529 "std::allocator<char> >";
1530 break;
1531 case SpecialSubKind::istream:
1532 S += "std::basic_istream<char, std::char_traits<char> >";
1533 break;
1534 case SpecialSubKind::ostream:
1535 S += "std::basic_ostream<char, std::char_traits<char> >";
1536 break;
1537 case SpecialSubKind::iostream:
1538 S += "std::basic_iostream<char, std::char_traits<char> >";
1539 break;
1540 }
1541 }
1542};
1543
1544class SpecialSubstitution final : public Node {
1545public:
1546 SpecialSubKind SSK;
1547
1548 SpecialSubstitution(SpecialSubKind SSK_)
1549 : Node(KSpecialSubstitution), SSK(SSK_) {}
1550
1551 template<typename Fn> void match(Fn F) const { F(SSK); }
1552
1553 StringView getBaseName() const override {
1554 switch (SSK) {
1555 case SpecialSubKind::allocator:
1556 return StringView("allocator");
1557 case SpecialSubKind::basic_string:
1558 return StringView("basic_string");
1559 case SpecialSubKind::string:
1560 return StringView("string");
1561 case SpecialSubKind::istream:
1562 return StringView("istream");
1563 case SpecialSubKind::ostream:
1564 return StringView("ostream");
1565 case SpecialSubKind::iostream:
1566 return StringView("iostream");
1567 }
Erik Pilkingtonf70e4d82019-01-17 20:37:51 +00001568 DEMANGLE_UNREACHABLE;
Richard Smithc20d1442018-08-20 20:14:49 +00001569 }
1570
1571 void printLeft(OutputStream &S) const override {
1572 switch (SSK) {
1573 case SpecialSubKind::allocator:
1574 S += "std::allocator";
1575 break;
1576 case SpecialSubKind::basic_string:
1577 S += "std::basic_string";
1578 break;
1579 case SpecialSubKind::string:
1580 S += "std::string";
1581 break;
1582 case SpecialSubKind::istream:
1583 S += "std::istream";
1584 break;
1585 case SpecialSubKind::ostream:
1586 S += "std::ostream";
1587 break;
1588 case SpecialSubKind::iostream:
1589 S += "std::iostream";
1590 break;
1591 }
1592 }
1593};
1594
1595class CtorDtorName final : public Node {
1596 const Node *Basename;
1597 const bool IsDtor;
Pavel Labathf4e67eb2018-10-10 08:39:16 +00001598 const int Variant;
Richard Smithc20d1442018-08-20 20:14:49 +00001599
1600public:
Pavel Labathf4e67eb2018-10-10 08:39:16 +00001601 CtorDtorName(const Node *Basename_, bool IsDtor_, int Variant_)
1602 : Node(KCtorDtorName), Basename(Basename_), IsDtor(IsDtor_),
1603 Variant(Variant_) {}
Richard Smithc20d1442018-08-20 20:14:49 +00001604
Pavel Labathf4e67eb2018-10-10 08:39:16 +00001605 template<typename Fn> void match(Fn F) const { F(Basename, IsDtor, Variant); }
Richard Smithc20d1442018-08-20 20:14:49 +00001606
1607 void printLeft(OutputStream &S) const override {
1608 if (IsDtor)
1609 S += "~";
1610 S += Basename->getBaseName();
1611 }
1612};
1613
1614class DtorName : public Node {
1615 const Node *Base;
1616
1617public:
1618 DtorName(const Node *Base_) : Node(KDtorName), Base(Base_) {}
1619
1620 template<typename Fn> void match(Fn F) const { F(Base); }
1621
1622 void printLeft(OutputStream &S) const override {
1623 S += "~";
1624 Base->printLeft(S);
1625 }
1626};
1627
1628class UnnamedTypeName : public Node {
1629 const StringView Count;
1630
1631public:
1632 UnnamedTypeName(StringView Count_) : Node(KUnnamedTypeName), Count(Count_) {}
1633
1634 template<typename Fn> void match(Fn F) const { F(Count); }
1635
1636 void printLeft(OutputStream &S) const override {
1637 S += "'unnamed";
1638 S += Count;
1639 S += "\'";
1640 }
1641};
1642
1643class ClosureTypeName : public Node {
Richard Smithdf1c14c2019-09-06 23:53:21 +00001644 NodeArray TemplateParams;
Richard Smithc20d1442018-08-20 20:14:49 +00001645 NodeArray Params;
1646 StringView Count;
1647
1648public:
Richard Smithdf1c14c2019-09-06 23:53:21 +00001649 ClosureTypeName(NodeArray TemplateParams_, NodeArray Params_,
1650 StringView Count_)
1651 : Node(KClosureTypeName), TemplateParams(TemplateParams_),
1652 Params(Params_), Count(Count_) {}
Richard Smithc20d1442018-08-20 20:14:49 +00001653
Richard Smithdf1c14c2019-09-06 23:53:21 +00001654 template<typename Fn> void match(Fn F) const {
1655 F(TemplateParams, Params, Count);
1656 }
1657
1658 void printDeclarator(OutputStream &S) const {
1659 if (!TemplateParams.empty()) {
1660 S += "<";
1661 TemplateParams.printWithComma(S);
1662 S += ">";
1663 }
1664 S += "(";
1665 Params.printWithComma(S);
1666 S += ")";
1667 }
Richard Smithc20d1442018-08-20 20:14:49 +00001668
1669 void printLeft(OutputStream &S) const override {
1670 S += "\'lambda";
1671 S += Count;
Richard Smithdf1c14c2019-09-06 23:53:21 +00001672 S += "\'";
1673 printDeclarator(S);
Richard Smithc20d1442018-08-20 20:14:49 +00001674 }
1675};
1676
1677class StructuredBindingName : public Node {
1678 NodeArray Bindings;
1679public:
1680 StructuredBindingName(NodeArray Bindings_)
1681 : Node(KStructuredBindingName), Bindings(Bindings_) {}
1682
1683 template<typename Fn> void match(Fn F) const { F(Bindings); }
1684
1685 void printLeft(OutputStream &S) const override {
1686 S += '[';
1687 Bindings.printWithComma(S);
1688 S += ']';
1689 }
1690};
1691
1692// -- Expression Nodes --
1693
1694class BinaryExpr : public Node {
1695 const Node *LHS;
1696 const StringView InfixOperator;
1697 const Node *RHS;
1698
1699public:
1700 BinaryExpr(const Node *LHS_, StringView InfixOperator_, const Node *RHS_)
1701 : Node(KBinaryExpr), LHS(LHS_), InfixOperator(InfixOperator_), RHS(RHS_) {
1702 }
1703
1704 template<typename Fn> void match(Fn F) const { F(LHS, InfixOperator, RHS); }
1705
1706 void printLeft(OutputStream &S) const override {
1707 // might be a template argument expression, then we need to disambiguate
1708 // with parens.
1709 if (InfixOperator == ">")
1710 S += "(";
1711
1712 S += "(";
1713 LHS->print(S);
1714 S += ") ";
1715 S += InfixOperator;
1716 S += " (";
1717 RHS->print(S);
1718 S += ")";
1719
1720 if (InfixOperator == ">")
1721 S += ")";
1722 }
1723};
1724
1725class ArraySubscriptExpr : public Node {
1726 const Node *Op1;
1727 const Node *Op2;
1728
1729public:
1730 ArraySubscriptExpr(const Node *Op1_, const Node *Op2_)
1731 : Node(KArraySubscriptExpr), Op1(Op1_), Op2(Op2_) {}
1732
1733 template<typename Fn> void match(Fn F) const { F(Op1, Op2); }
1734
1735 void printLeft(OutputStream &S) const override {
1736 S += "(";
1737 Op1->print(S);
1738 S += ")[";
1739 Op2->print(S);
1740 S += "]";
1741 }
1742};
1743
1744class PostfixExpr : public Node {
1745 const Node *Child;
1746 const StringView Operator;
1747
1748public:
1749 PostfixExpr(const Node *Child_, StringView Operator_)
1750 : Node(KPostfixExpr), Child(Child_), Operator(Operator_) {}
1751
1752 template<typename Fn> void match(Fn F) const { F(Child, Operator); }
1753
1754 void printLeft(OutputStream &S) const override {
1755 S += "(";
1756 Child->print(S);
1757 S += ")";
1758 S += Operator;
1759 }
1760};
1761
1762class ConditionalExpr : public Node {
1763 const Node *Cond;
1764 const Node *Then;
1765 const Node *Else;
1766
1767public:
1768 ConditionalExpr(const Node *Cond_, const Node *Then_, const Node *Else_)
1769 : Node(KConditionalExpr), Cond(Cond_), Then(Then_), Else(Else_) {}
1770
1771 template<typename Fn> void match(Fn F) const { F(Cond, Then, Else); }
1772
1773 void printLeft(OutputStream &S) const override {
1774 S += "(";
1775 Cond->print(S);
1776 S += ") ? (";
1777 Then->print(S);
1778 S += ") : (";
1779 Else->print(S);
1780 S += ")";
1781 }
1782};
1783
1784class MemberExpr : public Node {
1785 const Node *LHS;
1786 const StringView Kind;
1787 const Node *RHS;
1788
1789public:
1790 MemberExpr(const Node *LHS_, StringView Kind_, const Node *RHS_)
1791 : Node(KMemberExpr), LHS(LHS_), Kind(Kind_), RHS(RHS_) {}
1792
1793 template<typename Fn> void match(Fn F) const { F(LHS, Kind, RHS); }
1794
1795 void printLeft(OutputStream &S) const override {
1796 LHS->print(S);
1797 S += Kind;
1798 RHS->print(S);
1799 }
1800};
1801
Richard Smith1865d2f2020-10-22 19:29:36 -07001802class SubobjectExpr : public Node {
1803 const Node *Type;
1804 const Node *SubExpr;
1805 StringView Offset;
1806 NodeArray UnionSelectors;
1807 bool OnePastTheEnd;
1808
1809public:
1810 SubobjectExpr(const Node *Type_, const Node *SubExpr_, StringView Offset_,
1811 NodeArray UnionSelectors_, bool OnePastTheEnd_)
1812 : Node(KSubobjectExpr), Type(Type_), SubExpr(SubExpr_), Offset(Offset_),
1813 UnionSelectors(UnionSelectors_), OnePastTheEnd(OnePastTheEnd_) {}
1814
1815 template<typename Fn> void match(Fn F) const {
1816 F(Type, SubExpr, Offset, UnionSelectors, OnePastTheEnd);
1817 }
1818
1819 void printLeft(OutputStream &S) const override {
1820 SubExpr->print(S);
1821 S += ".<";
1822 Type->print(S);
1823 S += " at offset ";
1824 if (Offset.empty()) {
1825 S += "0";
1826 } else if (Offset[0] == 'n') {
1827 S += "-";
1828 S += Offset.dropFront();
1829 } else {
1830 S += Offset;
1831 }
1832 S += ">";
1833 }
1834};
1835
Richard Smithc20d1442018-08-20 20:14:49 +00001836class EnclosingExpr : public Node {
1837 const StringView Prefix;
1838 const Node *Infix;
1839 const StringView Postfix;
1840
1841public:
1842 EnclosingExpr(StringView Prefix_, Node *Infix_, StringView Postfix_)
1843 : Node(KEnclosingExpr), Prefix(Prefix_), Infix(Infix_),
1844 Postfix(Postfix_) {}
1845
1846 template<typename Fn> void match(Fn F) const { F(Prefix, Infix, Postfix); }
1847
1848 void printLeft(OutputStream &S) const override {
1849 S += Prefix;
1850 Infix->print(S);
1851 S += Postfix;
1852 }
1853};
1854
1855class CastExpr : public Node {
1856 // cast_kind<to>(from)
1857 const StringView CastKind;
1858 const Node *To;
1859 const Node *From;
1860
1861public:
1862 CastExpr(StringView CastKind_, const Node *To_, const Node *From_)
1863 : Node(KCastExpr), CastKind(CastKind_), To(To_), From(From_) {}
1864
1865 template<typename Fn> void match(Fn F) const { F(CastKind, To, From); }
1866
1867 void printLeft(OutputStream &S) const override {
1868 S += CastKind;
1869 S += "<";
1870 To->printLeft(S);
1871 S += ">(";
1872 From->printLeft(S);
1873 S += ")";
1874 }
1875};
1876
1877class SizeofParamPackExpr : public Node {
1878 const Node *Pack;
1879
1880public:
1881 SizeofParamPackExpr(const Node *Pack_)
1882 : Node(KSizeofParamPackExpr), Pack(Pack_) {}
1883
1884 template<typename Fn> void match(Fn F) const { F(Pack); }
1885
1886 void printLeft(OutputStream &S) const override {
1887 S += "sizeof...(";
1888 ParameterPackExpansion PPE(Pack);
1889 PPE.printLeft(S);
1890 S += ")";
1891 }
1892};
1893
1894class CallExpr : public Node {
1895 const Node *Callee;
1896 NodeArray Args;
1897
1898public:
1899 CallExpr(const Node *Callee_, NodeArray Args_)
1900 : Node(KCallExpr), Callee(Callee_), Args(Args_) {}
1901
1902 template<typename Fn> void match(Fn F) const { F(Callee, Args); }
1903
1904 void printLeft(OutputStream &S) const override {
1905 Callee->print(S);
1906 S += "(";
1907 Args.printWithComma(S);
1908 S += ")";
1909 }
1910};
1911
1912class NewExpr : public Node {
1913 // new (expr_list) type(init_list)
1914 NodeArray ExprList;
1915 Node *Type;
1916 NodeArray InitList;
1917 bool IsGlobal; // ::operator new ?
1918 bool IsArray; // new[] ?
1919public:
1920 NewExpr(NodeArray ExprList_, Node *Type_, NodeArray InitList_, bool IsGlobal_,
1921 bool IsArray_)
1922 : Node(KNewExpr), ExprList(ExprList_), Type(Type_), InitList(InitList_),
1923 IsGlobal(IsGlobal_), IsArray(IsArray_) {}
1924
1925 template<typename Fn> void match(Fn F) const {
1926 F(ExprList, Type, InitList, IsGlobal, IsArray);
1927 }
1928
1929 void printLeft(OutputStream &S) const override {
1930 if (IsGlobal)
1931 S += "::operator ";
1932 S += "new";
1933 if (IsArray)
1934 S += "[]";
1935 S += ' ';
1936 if (!ExprList.empty()) {
1937 S += "(";
1938 ExprList.printWithComma(S);
1939 S += ")";
1940 }
1941 Type->print(S);
1942 if (!InitList.empty()) {
1943 S += "(";
1944 InitList.printWithComma(S);
1945 S += ")";
1946 }
1947
1948 }
1949};
1950
1951class DeleteExpr : public Node {
1952 Node *Op;
1953 bool IsGlobal;
1954 bool IsArray;
1955
1956public:
1957 DeleteExpr(Node *Op_, bool IsGlobal_, bool IsArray_)
1958 : Node(KDeleteExpr), Op(Op_), IsGlobal(IsGlobal_), IsArray(IsArray_) {}
1959
1960 template<typename Fn> void match(Fn F) const { F(Op, IsGlobal, IsArray); }
1961
1962 void printLeft(OutputStream &S) const override {
1963 if (IsGlobal)
1964 S += "::";
1965 S += "delete";
1966 if (IsArray)
1967 S += "[] ";
1968 Op->print(S);
1969 }
1970};
1971
1972class PrefixExpr : public Node {
1973 StringView Prefix;
1974 Node *Child;
1975
1976public:
1977 PrefixExpr(StringView Prefix_, Node *Child_)
1978 : Node(KPrefixExpr), Prefix(Prefix_), Child(Child_) {}
1979
1980 template<typename Fn> void match(Fn F) const { F(Prefix, Child); }
1981
1982 void printLeft(OutputStream &S) const override {
1983 S += Prefix;
1984 S += "(";
1985 Child->print(S);
1986 S += ")";
1987 }
1988};
1989
1990class FunctionParam : public Node {
1991 StringView Number;
1992
1993public:
1994 FunctionParam(StringView Number_) : Node(KFunctionParam), Number(Number_) {}
1995
1996 template<typename Fn> void match(Fn F) const { F(Number); }
1997
1998 void printLeft(OutputStream &S) const override {
1999 S += "fp";
2000 S += Number;
2001 }
2002};
2003
2004class ConversionExpr : public Node {
2005 const Node *Type;
2006 NodeArray Expressions;
2007
2008public:
2009 ConversionExpr(const Node *Type_, NodeArray Expressions_)
2010 : Node(KConversionExpr), Type(Type_), Expressions(Expressions_) {}
2011
2012 template<typename Fn> void match(Fn F) const { F(Type, Expressions); }
2013
2014 void printLeft(OutputStream &S) const override {
2015 S += "(";
2016 Type->print(S);
2017 S += ")(";
2018 Expressions.printWithComma(S);
2019 S += ")";
2020 }
2021};
2022
Richard Smith1865d2f2020-10-22 19:29:36 -07002023class PointerToMemberConversionExpr : public Node {
2024 const Node *Type;
2025 const Node *SubExpr;
2026 StringView Offset;
2027
2028public:
2029 PointerToMemberConversionExpr(const Node *Type_, const Node *SubExpr_,
2030 StringView Offset_)
2031 : Node(KPointerToMemberConversionExpr), Type(Type_), SubExpr(SubExpr_),
2032 Offset(Offset_) {}
2033
2034 template<typename Fn> void match(Fn F) const { F(Type, SubExpr, Offset); }
2035
2036 void printLeft(OutputStream &S) const override {
2037 S += "(";
2038 Type->print(S);
2039 S += ")(";
2040 SubExpr->print(S);
2041 S += ")";
2042 }
2043};
2044
Richard Smithc20d1442018-08-20 20:14:49 +00002045class InitListExpr : public Node {
2046 const Node *Ty;
2047 NodeArray Inits;
2048public:
2049 InitListExpr(const Node *Ty_, NodeArray Inits_)
2050 : Node(KInitListExpr), Ty(Ty_), Inits(Inits_) {}
2051
2052 template<typename Fn> void match(Fn F) const { F(Ty, Inits); }
2053
2054 void printLeft(OutputStream &S) const override {
2055 if (Ty)
2056 Ty->print(S);
2057 S += '{';
2058 Inits.printWithComma(S);
2059 S += '}';
2060 }
2061};
2062
2063class BracedExpr : public Node {
2064 const Node *Elem;
2065 const Node *Init;
2066 bool IsArray;
2067public:
2068 BracedExpr(const Node *Elem_, const Node *Init_, bool IsArray_)
2069 : Node(KBracedExpr), Elem(Elem_), Init(Init_), IsArray(IsArray_) {}
2070
2071 template<typename Fn> void match(Fn F) const { F(Elem, Init, IsArray); }
2072
2073 void printLeft(OutputStream &S) const override {
2074 if (IsArray) {
2075 S += '[';
2076 Elem->print(S);
2077 S += ']';
2078 } else {
2079 S += '.';
2080 Elem->print(S);
2081 }
2082 if (Init->getKind() != KBracedExpr && Init->getKind() != KBracedRangeExpr)
2083 S += " = ";
2084 Init->print(S);
2085 }
2086};
2087
2088class BracedRangeExpr : public Node {
2089 const Node *First;
2090 const Node *Last;
2091 const Node *Init;
2092public:
2093 BracedRangeExpr(const Node *First_, const Node *Last_, const Node *Init_)
2094 : Node(KBracedRangeExpr), First(First_), Last(Last_), Init(Init_) {}
2095
2096 template<typename Fn> void match(Fn F) const { F(First, Last, Init); }
2097
2098 void printLeft(OutputStream &S) const override {
2099 S += '[';
2100 First->print(S);
2101 S += " ... ";
2102 Last->print(S);
2103 S += ']';
2104 if (Init->getKind() != KBracedExpr && Init->getKind() != KBracedRangeExpr)
2105 S += " = ";
2106 Init->print(S);
2107 }
2108};
2109
2110class FoldExpr : public Node {
2111 const Node *Pack, *Init;
2112 StringView OperatorName;
2113 bool IsLeftFold;
2114
2115public:
2116 FoldExpr(bool IsLeftFold_, StringView OperatorName_, const Node *Pack_,
2117 const Node *Init_)
2118 : Node(KFoldExpr), Pack(Pack_), Init(Init_), OperatorName(OperatorName_),
2119 IsLeftFold(IsLeftFold_) {}
2120
2121 template<typename Fn> void match(Fn F) const {
2122 F(IsLeftFold, OperatorName, Pack, Init);
2123 }
2124
2125 void printLeft(OutputStream &S) const override {
2126 auto PrintPack = [&] {
2127 S += '(';
2128 ParameterPackExpansion(Pack).print(S);
2129 S += ')';
2130 };
2131
2132 S += '(';
2133
2134 if (IsLeftFold) {
2135 // init op ... op pack
2136 if (Init != nullptr) {
2137 Init->print(S);
2138 S += ' ';
2139 S += OperatorName;
2140 S += ' ';
2141 }
2142 // ... op pack
2143 S += "... ";
2144 S += OperatorName;
2145 S += ' ';
2146 PrintPack();
2147 } else { // !IsLeftFold
2148 // pack op ...
2149 PrintPack();
2150 S += ' ';
2151 S += OperatorName;
2152 S += " ...";
2153 // pack op ... op init
2154 if (Init != nullptr) {
2155 S += ' ';
2156 S += OperatorName;
2157 S += ' ';
2158 Init->print(S);
2159 }
2160 }
2161 S += ')';
2162 }
2163};
2164
2165class ThrowExpr : public Node {
2166 const Node *Op;
2167
2168public:
2169 ThrowExpr(const Node *Op_) : Node(KThrowExpr), Op(Op_) {}
2170
2171 template<typename Fn> void match(Fn F) const { F(Op); }
2172
2173 void printLeft(OutputStream &S) const override {
2174 S += "throw ";
2175 Op->print(S);
2176 }
2177};
2178
2179class BoolExpr : public Node {
2180 bool Value;
2181
2182public:
2183 BoolExpr(bool Value_) : Node(KBoolExpr), Value(Value_) {}
2184
2185 template<typename Fn> void match(Fn F) const { F(Value); }
2186
2187 void printLeft(OutputStream &S) const override {
2188 S += Value ? StringView("true") : StringView("false");
2189 }
2190};
2191
Richard Smithdf1c14c2019-09-06 23:53:21 +00002192class StringLiteral : public Node {
2193 const Node *Type;
2194
2195public:
2196 StringLiteral(const Node *Type_) : Node(KStringLiteral), Type(Type_) {}
2197
2198 template<typename Fn> void match(Fn F) const { F(Type); }
2199
2200 void printLeft(OutputStream &S) const override {
2201 S += "\"<";
2202 Type->print(S);
2203 S += ">\"";
2204 }
2205};
2206
2207class LambdaExpr : public Node {
2208 const Node *Type;
2209
Richard Smithdf1c14c2019-09-06 23:53:21 +00002210public:
2211 LambdaExpr(const Node *Type_) : Node(KLambdaExpr), Type(Type_) {}
2212
2213 template<typename Fn> void match(Fn F) const { F(Type); }
2214
2215 void printLeft(OutputStream &S) const override {
2216 S += "[]";
Richard Smithfb917462019-09-09 22:26:04 +00002217 if (Type->getKind() == KClosureTypeName)
2218 static_cast<const ClosureTypeName *>(Type)->printDeclarator(S);
Richard Smithdf1c14c2019-09-06 23:53:21 +00002219 S += "{...}";
2220 }
2221};
2222
Erik Pilkington0a170f12020-05-13 14:13:37 -04002223class EnumLiteral : public Node {
Richard Smithc20d1442018-08-20 20:14:49 +00002224 // ty(integer)
2225 const Node *Ty;
2226 StringView Integer;
2227
2228public:
Erik Pilkington0a170f12020-05-13 14:13:37 -04002229 EnumLiteral(const Node *Ty_, StringView Integer_)
2230 : Node(KEnumLiteral), Ty(Ty_), Integer(Integer_) {}
Richard Smithc20d1442018-08-20 20:14:49 +00002231
2232 template<typename Fn> void match(Fn F) const { F(Ty, Integer); }
2233
2234 void printLeft(OutputStream &S) const override {
Erik Pilkington0a170f12020-05-13 14:13:37 -04002235 S << "(";
Richard Smithc20d1442018-08-20 20:14:49 +00002236 Ty->print(S);
Erik Pilkington0a170f12020-05-13 14:13:37 -04002237 S << ")";
2238
2239 if (Integer[0] == 'n')
2240 S << "-" << Integer.dropFront(1);
2241 else
2242 S << Integer;
Richard Smithc20d1442018-08-20 20:14:49 +00002243 }
2244};
2245
2246class IntegerLiteral : public Node {
2247 StringView Type;
2248 StringView Value;
2249
2250public:
2251 IntegerLiteral(StringView Type_, StringView Value_)
2252 : Node(KIntegerLiteral), Type(Type_), Value(Value_) {}
2253
2254 template<typename Fn> void match(Fn F) const { F(Type, Value); }
2255
2256 void printLeft(OutputStream &S) const override {
2257 if (Type.size() > 3) {
2258 S += "(";
2259 S += Type;
2260 S += ")";
2261 }
2262
2263 if (Value[0] == 'n') {
2264 S += "-";
2265 S += Value.dropFront(1);
2266 } else
2267 S += Value;
2268
2269 if (Type.size() <= 3)
2270 S += Type;
2271 }
2272};
2273
2274template <class Float> struct FloatData;
2275
2276namespace float_literal_impl {
2277constexpr Node::Kind getFloatLiteralKind(float *) {
2278 return Node::KFloatLiteral;
2279}
2280constexpr Node::Kind getFloatLiteralKind(double *) {
2281 return Node::KDoubleLiteral;
2282}
2283constexpr Node::Kind getFloatLiteralKind(long double *) {
2284 return Node::KLongDoubleLiteral;
2285}
2286}
2287
2288template <class Float> class FloatLiteralImpl : public Node {
2289 const StringView Contents;
2290
2291 static constexpr Kind KindForClass =
2292 float_literal_impl::getFloatLiteralKind((Float *)nullptr);
2293
2294public:
2295 FloatLiteralImpl(StringView Contents_)
2296 : Node(KindForClass), Contents(Contents_) {}
2297
2298 template<typename Fn> void match(Fn F) const { F(Contents); }
2299
2300 void printLeft(OutputStream &s) const override {
2301 const char *first = Contents.begin();
2302 const char *last = Contents.end() + 1;
2303
2304 const size_t N = FloatData<Float>::mangled_size;
2305 if (static_cast<std::size_t>(last - first) > N) {
2306 last = first + N;
2307 union {
2308 Float value;
2309 char buf[sizeof(Float)];
2310 };
2311 const char *t = first;
2312 char *e = buf;
2313 for (; t != last; ++t, ++e) {
2314 unsigned d1 = isdigit(*t) ? static_cast<unsigned>(*t - '0')
2315 : static_cast<unsigned>(*t - 'a' + 10);
2316 ++t;
2317 unsigned d0 = isdigit(*t) ? static_cast<unsigned>(*t - '0')
2318 : static_cast<unsigned>(*t - 'a' + 10);
2319 *e = static_cast<char>((d1 << 4) + d0);
2320 }
2321#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
2322 std::reverse(buf, e);
2323#endif
2324 char num[FloatData<Float>::max_demangled_size] = {0};
2325 int n = snprintf(num, sizeof(num), FloatData<Float>::spec, value);
2326 s += StringView(num, num + n);
2327 }
2328 }
2329};
2330
2331using FloatLiteral = FloatLiteralImpl<float>;
2332using DoubleLiteral = FloatLiteralImpl<double>;
2333using LongDoubleLiteral = FloatLiteralImpl<long double>;
2334
2335/// Visit the node. Calls \c F(P), where \c P is the node cast to the
2336/// appropriate derived class.
2337template<typename Fn>
2338void Node::visit(Fn F) const {
2339 switch (K) {
2340#define CASE(X) case K ## X: return F(static_cast<const X*>(this));
2341 FOR_EACH_NODE_KIND(CASE)
2342#undef CASE
2343 }
2344 assert(0 && "unknown mangling node kind");
2345}
2346
2347/// Determine the kind of a node from its type.
2348template<typename NodeT> struct NodeKind;
2349#define SPECIALIZATION(X) \
2350 template<> struct NodeKind<X> { \
2351 static constexpr Node::Kind Kind = Node::K##X; \
2352 static constexpr const char *name() { return #X; } \
2353 };
2354FOR_EACH_NODE_KIND(SPECIALIZATION)
2355#undef SPECIALIZATION
2356
2357#undef FOR_EACH_NODE_KIND
2358
Pavel Labathba825192018-10-16 14:29:14 +00002359template <typename Derived, typename Alloc> struct AbstractManglingParser {
Richard Smithc20d1442018-08-20 20:14:49 +00002360 const char *First;
2361 const char *Last;
2362
2363 // Name stack, this is used by the parser to hold temporary names that were
2364 // parsed. The parser collapses multiple names into new nodes to construct
2365 // the AST. Once the parser is finished, names.size() == 1.
2366 PODSmallVector<Node *, 32> Names;
2367
2368 // Substitution table. Itanium supports name substitutions as a means of
2369 // compression. The string "S42_" refers to the 44nd entry (base-36) in this
2370 // table.
2371 PODSmallVector<Node *, 32> Subs;
2372
Richard Smithdf1c14c2019-09-06 23:53:21 +00002373 using TemplateParamList = PODSmallVector<Node *, 8>;
2374
2375 class ScopedTemplateParamList {
2376 AbstractManglingParser *Parser;
2377 size_t OldNumTemplateParamLists;
2378 TemplateParamList Params;
2379
2380 public:
Louis Dionnec1fe8672020-10-30 17:33:02 -04002381 ScopedTemplateParamList(AbstractManglingParser *TheParser)
2382 : Parser(TheParser),
2383 OldNumTemplateParamLists(TheParser->TemplateParams.size()) {
Richard Smithdf1c14c2019-09-06 23:53:21 +00002384 Parser->TemplateParams.push_back(&Params);
2385 }
2386 ~ScopedTemplateParamList() {
2387 assert(Parser->TemplateParams.size() >= OldNumTemplateParamLists);
2388 Parser->TemplateParams.dropBack(OldNumTemplateParamLists);
2389 }
Richard Smithdf1c14c2019-09-06 23:53:21 +00002390 };
2391
Richard Smithc20d1442018-08-20 20:14:49 +00002392 // Template parameter table. Like the above, but referenced like "T42_".
2393 // This has a smaller size compared to Subs and Names because it can be
2394 // stored on the stack.
Richard Smithdf1c14c2019-09-06 23:53:21 +00002395 TemplateParamList OuterTemplateParams;
2396
2397 // Lists of template parameters indexed by template parameter depth,
2398 // referenced like "TL2_4_". If nonempty, element 0 is always
2399 // OuterTemplateParams; inner elements are always template parameter lists of
2400 // lambda expressions. For a generic lambda with no explicit template
2401 // parameter list, the corresponding parameter list pointer will be null.
2402 PODSmallVector<TemplateParamList *, 4> TemplateParams;
Richard Smithc20d1442018-08-20 20:14:49 +00002403
2404 // Set of unresolved forward <template-param> references. These can occur in a
2405 // conversion operator's type, and are resolved in the enclosing <encoding>.
2406 PODSmallVector<ForwardTemplateReference *, 4> ForwardTemplateRefs;
2407
Richard Smithc20d1442018-08-20 20:14:49 +00002408 bool TryToParseTemplateArgs = true;
2409 bool PermitForwardTemplateReferences = false;
Richard Smithdf1c14c2019-09-06 23:53:21 +00002410 size_t ParsingLambdaParamsAtLevel = (size_t)-1;
2411
2412 unsigned NumSyntheticTemplateParameters[3] = {};
Richard Smithc20d1442018-08-20 20:14:49 +00002413
2414 Alloc ASTAllocator;
2415
Pavel Labathba825192018-10-16 14:29:14 +00002416 AbstractManglingParser(const char *First_, const char *Last_)
2417 : First(First_), Last(Last_) {}
2418
2419 Derived &getDerived() { return static_cast<Derived &>(*this); }
Richard Smithc20d1442018-08-20 20:14:49 +00002420
2421 void reset(const char *First_, const char *Last_) {
2422 First = First_;
2423 Last = Last_;
2424 Names.clear();
2425 Subs.clear();
2426 TemplateParams.clear();
Richard Smithdf1c14c2019-09-06 23:53:21 +00002427 ParsingLambdaParamsAtLevel = (size_t)-1;
Richard Smithc20d1442018-08-20 20:14:49 +00002428 TryToParseTemplateArgs = true;
2429 PermitForwardTemplateReferences = false;
Richard Smith9a2307a2019-09-07 00:11:53 +00002430 for (int I = 0; I != 3; ++I)
2431 NumSyntheticTemplateParameters[I] = 0;
Richard Smithc20d1442018-08-20 20:14:49 +00002432 ASTAllocator.reset();
2433 }
2434
Richard Smithb485b352018-08-24 23:30:26 +00002435 template <class T, class... Args> Node *make(Args &&... args) {
Richard Smithc20d1442018-08-20 20:14:49 +00002436 return ASTAllocator.template makeNode<T>(std::forward<Args>(args)...);
2437 }
2438
2439 template <class It> NodeArray makeNodeArray(It begin, It end) {
2440 size_t sz = static_cast<size_t>(end - begin);
2441 void *mem = ASTAllocator.allocateNodeArray(sz);
2442 Node **data = new (mem) Node *[sz];
2443 std::copy(begin, end, data);
2444 return NodeArray(data, sz);
2445 }
2446
2447 NodeArray popTrailingNodeArray(size_t FromPosition) {
2448 assert(FromPosition <= Names.size());
2449 NodeArray res =
2450 makeNodeArray(Names.begin() + (long)FromPosition, Names.end());
2451 Names.dropBack(FromPosition);
2452 return res;
2453 }
2454
2455 bool consumeIf(StringView S) {
2456 if (StringView(First, Last).startsWith(S)) {
2457 First += S.size();
2458 return true;
2459 }
2460 return false;
2461 }
2462
2463 bool consumeIf(char C) {
2464 if (First != Last && *First == C) {
2465 ++First;
2466 return true;
2467 }
2468 return false;
2469 }
2470
2471 char consume() { return First != Last ? *First++ : '\0'; }
2472
2473 char look(unsigned Lookahead = 0) {
2474 if (static_cast<size_t>(Last - First) <= Lookahead)
2475 return '\0';
2476 return First[Lookahead];
2477 }
2478
2479 size_t numLeft() const { return static_cast<size_t>(Last - First); }
2480
2481 StringView parseNumber(bool AllowNegative = false);
2482 Qualifiers parseCVQualifiers();
2483 bool parsePositiveInteger(size_t *Out);
2484 StringView parseBareSourceName();
2485
2486 bool parseSeqId(size_t *Out);
2487 Node *parseSubstitution();
2488 Node *parseTemplateParam();
Richard Smithdf1c14c2019-09-06 23:53:21 +00002489 Node *parseTemplateParamDecl();
Richard Smithc20d1442018-08-20 20:14:49 +00002490 Node *parseTemplateArgs(bool TagTemplates = false);
2491 Node *parseTemplateArg();
2492
2493 /// Parse the <expr> production.
2494 Node *parseExpr();
2495 Node *parsePrefixExpr(StringView Kind);
2496 Node *parseBinaryExpr(StringView Kind);
2497 Node *parseIntegerLiteral(StringView Lit);
2498 Node *parseExprPrimary();
2499 template <class Float> Node *parseFloatingLiteral();
2500 Node *parseFunctionParam();
2501 Node *parseNewExpr();
2502 Node *parseConversionExpr();
2503 Node *parseBracedExpr();
2504 Node *parseFoldExpr();
Richard Smith1865d2f2020-10-22 19:29:36 -07002505 Node *parsePointerToMemberConversionExpr();
2506 Node *parseSubobjectExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00002507
2508 /// Parse the <type> production.
2509 Node *parseType();
2510 Node *parseFunctionType();
2511 Node *parseVectorType();
2512 Node *parseDecltype();
2513 Node *parseArrayType();
2514 Node *parsePointerToMemberType();
2515 Node *parseClassEnumType();
2516 Node *parseQualifiedType();
2517
2518 Node *parseEncoding();
2519 bool parseCallOffset();
2520 Node *parseSpecialName();
2521
2522 /// Holds some extra information about a <name> that is being parsed. This
2523 /// information is only pertinent if the <name> refers to an <encoding>.
2524 struct NameState {
2525 bool CtorDtorConversion = false;
2526 bool EndsWithTemplateArgs = false;
2527 Qualifiers CVQualifiers = QualNone;
2528 FunctionRefQual ReferenceQualifier = FrefQualNone;
2529 size_t ForwardTemplateRefsBegin;
2530
Pavel Labathba825192018-10-16 14:29:14 +00002531 NameState(AbstractManglingParser *Enclosing)
Richard Smithc20d1442018-08-20 20:14:49 +00002532 : ForwardTemplateRefsBegin(Enclosing->ForwardTemplateRefs.size()) {}
2533 };
2534
2535 bool resolveForwardTemplateRefs(NameState &State) {
2536 size_t I = State.ForwardTemplateRefsBegin;
2537 size_t E = ForwardTemplateRefs.size();
2538 for (; I < E; ++I) {
2539 size_t Idx = ForwardTemplateRefs[I]->Index;
Richard Smithdf1c14c2019-09-06 23:53:21 +00002540 if (TemplateParams.empty() || !TemplateParams[0] ||
2541 Idx >= TemplateParams[0]->size())
Richard Smithc20d1442018-08-20 20:14:49 +00002542 return true;
Richard Smithdf1c14c2019-09-06 23:53:21 +00002543 ForwardTemplateRefs[I]->Ref = (*TemplateParams[0])[Idx];
Richard Smithc20d1442018-08-20 20:14:49 +00002544 }
2545 ForwardTemplateRefs.dropBack(State.ForwardTemplateRefsBegin);
2546 return false;
2547 }
2548
2549 /// Parse the <name> production>
2550 Node *parseName(NameState *State = nullptr);
2551 Node *parseLocalName(NameState *State);
2552 Node *parseOperatorName(NameState *State);
2553 Node *parseUnqualifiedName(NameState *State);
2554 Node *parseUnnamedTypeName(NameState *State);
2555 Node *parseSourceName(NameState *State);
2556 Node *parseUnscopedName(NameState *State);
2557 Node *parseNestedName(NameState *State);
2558 Node *parseCtorDtorName(Node *&SoFar, NameState *State);
2559
2560 Node *parseAbiTags(Node *N);
2561
2562 /// Parse the <unresolved-name> production.
2563 Node *parseUnresolvedName();
2564 Node *parseSimpleId();
2565 Node *parseBaseUnresolvedName();
2566 Node *parseUnresolvedType();
2567 Node *parseDestructorName();
2568
2569 /// Top-level entry point into the parser.
2570 Node *parse();
2571};
2572
2573const char* parse_discriminator(const char* first, const char* last);
2574
2575// <name> ::= <nested-name> // N
2576// ::= <local-name> # See Scope Encoding below // Z
2577// ::= <unscoped-template-name> <template-args>
2578// ::= <unscoped-name>
2579//
2580// <unscoped-template-name> ::= <unscoped-name>
2581// ::= <substitution>
Pavel Labathba825192018-10-16 14:29:14 +00002582template <typename Derived, typename Alloc>
2583Node *AbstractManglingParser<Derived, Alloc>::parseName(NameState *State) {
Richard Smithc20d1442018-08-20 20:14:49 +00002584 consumeIf('L'); // extension
2585
2586 if (look() == 'N')
Pavel Labathba825192018-10-16 14:29:14 +00002587 return getDerived().parseNestedName(State);
Richard Smithc20d1442018-08-20 20:14:49 +00002588 if (look() == 'Z')
Pavel Labathba825192018-10-16 14:29:14 +00002589 return getDerived().parseLocalName(State);
Richard Smithc20d1442018-08-20 20:14:49 +00002590
2591 // ::= <unscoped-template-name> <template-args>
2592 if (look() == 'S' && look(1) != 't') {
Pavel Labathba825192018-10-16 14:29:14 +00002593 Node *S = getDerived().parseSubstitution();
Richard Smithc20d1442018-08-20 20:14:49 +00002594 if (S == nullptr)
2595 return nullptr;
2596 if (look() != 'I')
2597 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00002598 Node *TA = getDerived().parseTemplateArgs(State != nullptr);
Richard Smithc20d1442018-08-20 20:14:49 +00002599 if (TA == nullptr)
2600 return nullptr;
2601 if (State) State->EndsWithTemplateArgs = true;
2602 return make<NameWithTemplateArgs>(S, TA);
2603 }
2604
Pavel Labathba825192018-10-16 14:29:14 +00002605 Node *N = getDerived().parseUnscopedName(State);
Richard Smithc20d1442018-08-20 20:14:49 +00002606 if (N == nullptr)
2607 return nullptr;
2608 // ::= <unscoped-template-name> <template-args>
2609 if (look() == 'I') {
2610 Subs.push_back(N);
Pavel Labathba825192018-10-16 14:29:14 +00002611 Node *TA = getDerived().parseTemplateArgs(State != nullptr);
Richard Smithc20d1442018-08-20 20:14:49 +00002612 if (TA == nullptr)
2613 return nullptr;
2614 if (State) State->EndsWithTemplateArgs = true;
2615 return make<NameWithTemplateArgs>(N, TA);
2616 }
2617 // ::= <unscoped-name>
2618 return N;
2619}
2620
2621// <local-name> := Z <function encoding> E <entity name> [<discriminator>]
2622// := Z <function encoding> E s [<discriminator>]
2623// := Z <function encoding> Ed [ <parameter number> ] _ <entity name>
Pavel Labathba825192018-10-16 14:29:14 +00002624template <typename Derived, typename Alloc>
2625Node *AbstractManglingParser<Derived, Alloc>::parseLocalName(NameState *State) {
Richard Smithc20d1442018-08-20 20:14:49 +00002626 if (!consumeIf('Z'))
2627 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00002628 Node *Encoding = getDerived().parseEncoding();
Richard Smithc20d1442018-08-20 20:14:49 +00002629 if (Encoding == nullptr || !consumeIf('E'))
2630 return nullptr;
2631
2632 if (consumeIf('s')) {
2633 First = parse_discriminator(First, Last);
Richard Smithb485b352018-08-24 23:30:26 +00002634 auto *StringLitName = make<NameType>("string literal");
2635 if (!StringLitName)
2636 return nullptr;
2637 return make<LocalName>(Encoding, StringLitName);
Richard Smithc20d1442018-08-20 20:14:49 +00002638 }
2639
2640 if (consumeIf('d')) {
2641 parseNumber(true);
2642 if (!consumeIf('_'))
2643 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00002644 Node *N = getDerived().parseName(State);
Richard Smithc20d1442018-08-20 20:14:49 +00002645 if (N == nullptr)
2646 return nullptr;
2647 return make<LocalName>(Encoding, N);
2648 }
2649
Pavel Labathba825192018-10-16 14:29:14 +00002650 Node *Entity = getDerived().parseName(State);
Richard Smithc20d1442018-08-20 20:14:49 +00002651 if (Entity == nullptr)
2652 return nullptr;
2653 First = parse_discriminator(First, Last);
2654 return make<LocalName>(Encoding, Entity);
2655}
2656
2657// <unscoped-name> ::= <unqualified-name>
2658// ::= St <unqualified-name> # ::std::
2659// extension ::= StL<unqualified-name>
Pavel Labathba825192018-10-16 14:29:14 +00002660template <typename Derived, typename Alloc>
2661Node *
2662AbstractManglingParser<Derived, Alloc>::parseUnscopedName(NameState *State) {
2663 if (consumeIf("StL") || consumeIf("St")) {
2664 Node *R = getDerived().parseUnqualifiedName(State);
2665 if (R == nullptr)
2666 return nullptr;
2667 return make<StdQualifiedName>(R);
2668 }
2669 return getDerived().parseUnqualifiedName(State);
Richard Smithc20d1442018-08-20 20:14:49 +00002670}
2671
2672// <unqualified-name> ::= <operator-name> [abi-tags]
2673// ::= <ctor-dtor-name>
2674// ::= <source-name>
2675// ::= <unnamed-type-name>
2676// ::= DC <source-name>+ E # structured binding declaration
Pavel Labathba825192018-10-16 14:29:14 +00002677template <typename Derived, typename Alloc>
2678Node *
2679AbstractManglingParser<Derived, Alloc>::parseUnqualifiedName(NameState *State) {
Richard Smithc20d1442018-08-20 20:14:49 +00002680 // <ctor-dtor-name>s are special-cased in parseNestedName().
2681 Node *Result;
2682 if (look() == 'U')
Pavel Labathba825192018-10-16 14:29:14 +00002683 Result = getDerived().parseUnnamedTypeName(State);
Richard Smithc20d1442018-08-20 20:14:49 +00002684 else if (look() >= '1' && look() <= '9')
Pavel Labathba825192018-10-16 14:29:14 +00002685 Result = getDerived().parseSourceName(State);
Richard Smithc20d1442018-08-20 20:14:49 +00002686 else if (consumeIf("DC")) {
2687 size_t BindingsBegin = Names.size();
2688 do {
Pavel Labathba825192018-10-16 14:29:14 +00002689 Node *Binding = getDerived().parseSourceName(State);
Richard Smithc20d1442018-08-20 20:14:49 +00002690 if (Binding == nullptr)
2691 return nullptr;
2692 Names.push_back(Binding);
2693 } while (!consumeIf('E'));
2694 Result = make<StructuredBindingName>(popTrailingNodeArray(BindingsBegin));
2695 } else
Pavel Labathba825192018-10-16 14:29:14 +00002696 Result = getDerived().parseOperatorName(State);
Richard Smithc20d1442018-08-20 20:14:49 +00002697 if (Result != nullptr)
Pavel Labathba825192018-10-16 14:29:14 +00002698 Result = getDerived().parseAbiTags(Result);
Richard Smithc20d1442018-08-20 20:14:49 +00002699 return Result;
2700}
2701
2702// <unnamed-type-name> ::= Ut [<nonnegative number>] _
2703// ::= <closure-type-name>
2704//
2705// <closure-type-name> ::= Ul <lambda-sig> E [ <nonnegative number> ] _
2706//
2707// <lambda-sig> ::= <parameter type>+ # Parameter types or "v" if the lambda has no parameters
Pavel Labathba825192018-10-16 14:29:14 +00002708template <typename Derived, typename Alloc>
2709Node *
Richard Smithdf1c14c2019-09-06 23:53:21 +00002710AbstractManglingParser<Derived, Alloc>::parseUnnamedTypeName(NameState *State) {
2711 // <template-params> refer to the innermost <template-args>. Clear out any
2712 // outer args that we may have inserted into TemplateParams.
2713 if (State != nullptr)
2714 TemplateParams.clear();
2715
Richard Smithc20d1442018-08-20 20:14:49 +00002716 if (consumeIf("Ut")) {
2717 StringView Count = parseNumber();
2718 if (!consumeIf('_'))
2719 return nullptr;
2720 return make<UnnamedTypeName>(Count);
2721 }
2722 if (consumeIf("Ul")) {
Richard Smithdf1c14c2019-09-06 23:53:21 +00002723 SwapAndRestore<size_t> SwapParams(ParsingLambdaParamsAtLevel,
2724 TemplateParams.size());
2725 ScopedTemplateParamList LambdaTemplateParams(this);
2726
2727 size_t ParamsBegin = Names.size();
2728 while (look() == 'T' &&
2729 StringView("yptn").find(look(1)) != StringView::npos) {
2730 Node *T = parseTemplateParamDecl();
2731 if (!T)
2732 return nullptr;
Richard Smithdf1c14c2019-09-06 23:53:21 +00002733 Names.push_back(T);
2734 }
2735 NodeArray TempParams = popTrailingNodeArray(ParamsBegin);
2736
2737 // FIXME: If TempParams is empty and none of the function parameters
2738 // includes 'auto', we should remove LambdaTemplateParams from the
2739 // TemplateParams list. Unfortunately, we don't find out whether there are
2740 // any 'auto' parameters until too late in an example such as:
2741 //
2742 // template<typename T> void f(
2743 // decltype([](decltype([]<typename T>(T v) {}),
2744 // auto) {})) {}
2745 // template<typename T> void f(
2746 // decltype([](decltype([]<typename T>(T w) {}),
2747 // int) {})) {}
2748 //
2749 // Here, the type of v is at level 2 but the type of w is at level 1. We
2750 // don't find this out until we encounter the type of the next parameter.
2751 //
2752 // However, compilers can't actually cope with the former example in
2753 // practice, and it's likely to be made ill-formed in future, so we don't
2754 // need to support it here.
2755 //
2756 // If we encounter an 'auto' in the function parameter types, we will
2757 // recreate a template parameter scope for it, but any intervening lambdas
2758 // will be parsed in the 'wrong' template parameter depth.
2759 if (TempParams.empty())
2760 TemplateParams.pop_back();
2761
Richard Smithc20d1442018-08-20 20:14:49 +00002762 if (!consumeIf("vE")) {
Richard Smithc20d1442018-08-20 20:14:49 +00002763 do {
Pavel Labathba825192018-10-16 14:29:14 +00002764 Node *P = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00002765 if (P == nullptr)
2766 return nullptr;
2767 Names.push_back(P);
2768 } while (!consumeIf('E'));
Richard Smithc20d1442018-08-20 20:14:49 +00002769 }
Richard Smithdf1c14c2019-09-06 23:53:21 +00002770 NodeArray Params = popTrailingNodeArray(ParamsBegin);
2771
Richard Smithc20d1442018-08-20 20:14:49 +00002772 StringView Count = parseNumber();
2773 if (!consumeIf('_'))
2774 return nullptr;
Richard Smithdf1c14c2019-09-06 23:53:21 +00002775 return make<ClosureTypeName>(TempParams, Params, Count);
Richard Smithc20d1442018-08-20 20:14:49 +00002776 }
Erik Pilkington974b6542019-01-17 21:37:51 +00002777 if (consumeIf("Ub")) {
2778 (void)parseNumber();
2779 if (!consumeIf('_'))
2780 return nullptr;
2781 return make<NameType>("'block-literal'");
2782 }
Richard Smithc20d1442018-08-20 20:14:49 +00002783 return nullptr;
2784}
2785
2786// <source-name> ::= <positive length number> <identifier>
Pavel Labathba825192018-10-16 14:29:14 +00002787template <typename Derived, typename Alloc>
2788Node *AbstractManglingParser<Derived, Alloc>::parseSourceName(NameState *) {
Richard Smithc20d1442018-08-20 20:14:49 +00002789 size_t Length = 0;
2790 if (parsePositiveInteger(&Length))
2791 return nullptr;
2792 if (numLeft() < Length || Length == 0)
2793 return nullptr;
2794 StringView Name(First, First + Length);
2795 First += Length;
2796 if (Name.startsWith("_GLOBAL__N"))
2797 return make<NameType>("(anonymous namespace)");
2798 return make<NameType>(Name);
2799}
2800
2801// <operator-name> ::= aa # &&
2802// ::= ad # & (unary)
2803// ::= an # &
2804// ::= aN # &=
2805// ::= aS # =
2806// ::= cl # ()
2807// ::= cm # ,
2808// ::= co # ~
2809// ::= cv <type> # (cast)
2810// ::= da # delete[]
2811// ::= de # * (unary)
2812// ::= dl # delete
2813// ::= dv # /
2814// ::= dV # /=
2815// ::= eo # ^
2816// ::= eO # ^=
2817// ::= eq # ==
2818// ::= ge # >=
2819// ::= gt # >
2820// ::= ix # []
2821// ::= le # <=
2822// ::= li <source-name> # operator ""
2823// ::= ls # <<
2824// ::= lS # <<=
2825// ::= lt # <
2826// ::= mi # -
2827// ::= mI # -=
2828// ::= ml # *
2829// ::= mL # *=
2830// ::= mm # -- (postfix in <expression> context)
2831// ::= na # new[]
2832// ::= ne # !=
2833// ::= ng # - (unary)
2834// ::= nt # !
2835// ::= nw # new
2836// ::= oo # ||
2837// ::= or # |
2838// ::= oR # |=
2839// ::= pm # ->*
2840// ::= pl # +
2841// ::= pL # +=
2842// ::= pp # ++ (postfix in <expression> context)
2843// ::= ps # + (unary)
2844// ::= pt # ->
2845// ::= qu # ?
2846// ::= rm # %
2847// ::= rM # %=
2848// ::= rs # >>
2849// ::= rS # >>=
2850// ::= ss # <=> C++2a
2851// ::= v <digit> <source-name> # vendor extended operator
Pavel Labathba825192018-10-16 14:29:14 +00002852template <typename Derived, typename Alloc>
2853Node *
2854AbstractManglingParser<Derived, Alloc>::parseOperatorName(NameState *State) {
Richard Smithc20d1442018-08-20 20:14:49 +00002855 switch (look()) {
2856 case 'a':
2857 switch (look(1)) {
2858 case 'a':
2859 First += 2;
2860 return make<NameType>("operator&&");
2861 case 'd':
2862 case 'n':
2863 First += 2;
2864 return make<NameType>("operator&");
2865 case 'N':
2866 First += 2;
2867 return make<NameType>("operator&=");
2868 case 'S':
2869 First += 2;
2870 return make<NameType>("operator=");
2871 }
2872 return nullptr;
2873 case 'c':
2874 switch (look(1)) {
2875 case 'l':
2876 First += 2;
2877 return make<NameType>("operator()");
2878 case 'm':
2879 First += 2;
2880 return make<NameType>("operator,");
2881 case 'o':
2882 First += 2;
2883 return make<NameType>("operator~");
2884 // ::= cv <type> # (cast)
2885 case 'v': {
2886 First += 2;
2887 SwapAndRestore<bool> SaveTemplate(TryToParseTemplateArgs, false);
2888 // If we're parsing an encoding, State != nullptr and the conversion
2889 // operators' <type> could have a <template-param> that refers to some
2890 // <template-arg>s further ahead in the mangled name.
2891 SwapAndRestore<bool> SavePermit(PermitForwardTemplateReferences,
2892 PermitForwardTemplateReferences ||
2893 State != nullptr);
Pavel Labathba825192018-10-16 14:29:14 +00002894 Node *Ty = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00002895 if (Ty == nullptr)
2896 return nullptr;
2897 if (State) State->CtorDtorConversion = true;
2898 return make<ConversionOperatorType>(Ty);
2899 }
2900 }
2901 return nullptr;
2902 case 'd':
2903 switch (look(1)) {
2904 case 'a':
2905 First += 2;
2906 return make<NameType>("operator delete[]");
2907 case 'e':
2908 First += 2;
2909 return make<NameType>("operator*");
2910 case 'l':
2911 First += 2;
2912 return make<NameType>("operator delete");
2913 case 'v':
2914 First += 2;
2915 return make<NameType>("operator/");
2916 case 'V':
2917 First += 2;
2918 return make<NameType>("operator/=");
2919 }
2920 return nullptr;
2921 case 'e':
2922 switch (look(1)) {
2923 case 'o':
2924 First += 2;
2925 return make<NameType>("operator^");
2926 case 'O':
2927 First += 2;
2928 return make<NameType>("operator^=");
2929 case 'q':
2930 First += 2;
2931 return make<NameType>("operator==");
2932 }
2933 return nullptr;
2934 case 'g':
2935 switch (look(1)) {
2936 case 'e':
2937 First += 2;
2938 return make<NameType>("operator>=");
2939 case 't':
2940 First += 2;
2941 return make<NameType>("operator>");
2942 }
2943 return nullptr;
2944 case 'i':
2945 if (look(1) == 'x') {
2946 First += 2;
2947 return make<NameType>("operator[]");
2948 }
2949 return nullptr;
2950 case 'l':
2951 switch (look(1)) {
2952 case 'e':
2953 First += 2;
2954 return make<NameType>("operator<=");
2955 // ::= li <source-name> # operator ""
2956 case 'i': {
2957 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00002958 Node *SN = getDerived().parseSourceName(State);
Richard Smithc20d1442018-08-20 20:14:49 +00002959 if (SN == nullptr)
2960 return nullptr;
2961 return make<LiteralOperator>(SN);
2962 }
2963 case 's':
2964 First += 2;
2965 return make<NameType>("operator<<");
2966 case 'S':
2967 First += 2;
2968 return make<NameType>("operator<<=");
2969 case 't':
2970 First += 2;
2971 return make<NameType>("operator<");
2972 }
2973 return nullptr;
2974 case 'm':
2975 switch (look(1)) {
2976 case 'i':
2977 First += 2;
2978 return make<NameType>("operator-");
2979 case 'I':
2980 First += 2;
2981 return make<NameType>("operator-=");
2982 case 'l':
2983 First += 2;
2984 return make<NameType>("operator*");
2985 case 'L':
2986 First += 2;
2987 return make<NameType>("operator*=");
2988 case 'm':
2989 First += 2;
2990 return make<NameType>("operator--");
2991 }
2992 return nullptr;
2993 case 'n':
2994 switch (look(1)) {
2995 case 'a':
2996 First += 2;
2997 return make<NameType>("operator new[]");
2998 case 'e':
2999 First += 2;
3000 return make<NameType>("operator!=");
3001 case 'g':
3002 First += 2;
3003 return make<NameType>("operator-");
3004 case 't':
3005 First += 2;
3006 return make<NameType>("operator!");
3007 case 'w':
3008 First += 2;
3009 return make<NameType>("operator new");
3010 }
3011 return nullptr;
3012 case 'o':
3013 switch (look(1)) {
3014 case 'o':
3015 First += 2;
3016 return make<NameType>("operator||");
3017 case 'r':
3018 First += 2;
3019 return make<NameType>("operator|");
3020 case 'R':
3021 First += 2;
3022 return make<NameType>("operator|=");
3023 }
3024 return nullptr;
3025 case 'p':
3026 switch (look(1)) {
3027 case 'm':
3028 First += 2;
3029 return make<NameType>("operator->*");
3030 case 'l':
3031 First += 2;
3032 return make<NameType>("operator+");
3033 case 'L':
3034 First += 2;
3035 return make<NameType>("operator+=");
3036 case 'p':
3037 First += 2;
3038 return make<NameType>("operator++");
3039 case 's':
3040 First += 2;
3041 return make<NameType>("operator+");
3042 case 't':
3043 First += 2;
3044 return make<NameType>("operator->");
3045 }
3046 return nullptr;
3047 case 'q':
3048 if (look(1) == 'u') {
3049 First += 2;
3050 return make<NameType>("operator?");
3051 }
3052 return nullptr;
3053 case 'r':
3054 switch (look(1)) {
3055 case 'm':
3056 First += 2;
3057 return make<NameType>("operator%");
3058 case 'M':
3059 First += 2;
3060 return make<NameType>("operator%=");
3061 case 's':
3062 First += 2;
3063 return make<NameType>("operator>>");
3064 case 'S':
3065 First += 2;
3066 return make<NameType>("operator>>=");
3067 }
3068 return nullptr;
3069 case 's':
3070 if (look(1) == 's') {
3071 First += 2;
3072 return make<NameType>("operator<=>");
3073 }
3074 return nullptr;
3075 // ::= v <digit> <source-name> # vendor extended operator
3076 case 'v':
3077 if (std::isdigit(look(1))) {
3078 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00003079 Node *SN = getDerived().parseSourceName(State);
Richard Smithc20d1442018-08-20 20:14:49 +00003080 if (SN == nullptr)
3081 return nullptr;
3082 return make<ConversionOperatorType>(SN);
3083 }
3084 return nullptr;
3085 }
3086 return nullptr;
3087}
3088
3089// <ctor-dtor-name> ::= C1 # complete object constructor
3090// ::= C2 # base object constructor
3091// ::= C3 # complete object allocating constructor
Nico Weber29294792019-04-03 23:14:33 +00003092// extension ::= C4 # gcc old-style "[unified]" constructor
3093// extension ::= C5 # the COMDAT used for ctors
Richard Smithc20d1442018-08-20 20:14:49 +00003094// ::= D0 # deleting destructor
3095// ::= D1 # complete object destructor
3096// ::= D2 # base object destructor
Nico Weber29294792019-04-03 23:14:33 +00003097// extension ::= D4 # gcc old-style "[unified]" destructor
3098// extension ::= D5 # the COMDAT used for dtors
Pavel Labathba825192018-10-16 14:29:14 +00003099template <typename Derived, typename Alloc>
3100Node *
3101AbstractManglingParser<Derived, Alloc>::parseCtorDtorName(Node *&SoFar,
3102 NameState *State) {
Richard Smithc20d1442018-08-20 20:14:49 +00003103 if (SoFar->getKind() == Node::KSpecialSubstitution) {
3104 auto SSK = static_cast<SpecialSubstitution *>(SoFar)->SSK;
3105 switch (SSK) {
3106 case SpecialSubKind::string:
3107 case SpecialSubKind::istream:
3108 case SpecialSubKind::ostream:
3109 case SpecialSubKind::iostream:
3110 SoFar = make<ExpandedSpecialSubstitution>(SSK);
Richard Smithb485b352018-08-24 23:30:26 +00003111 if (!SoFar)
3112 return nullptr;
Reid Klecknere76aabe2018-11-01 18:24:03 +00003113 break;
Richard Smithc20d1442018-08-20 20:14:49 +00003114 default:
3115 break;
3116 }
3117 }
3118
3119 if (consumeIf('C')) {
3120 bool IsInherited = consumeIf('I');
Nico Weber29294792019-04-03 23:14:33 +00003121 if (look() != '1' && look() != '2' && look() != '3' && look() != '4' &&
3122 look() != '5')
Richard Smithc20d1442018-08-20 20:14:49 +00003123 return nullptr;
Pavel Labathf4e67eb2018-10-10 08:39:16 +00003124 int Variant = look() - '0';
Richard Smithc20d1442018-08-20 20:14:49 +00003125 ++First;
3126 if (State) State->CtorDtorConversion = true;
3127 if (IsInherited) {
Pavel Labathba825192018-10-16 14:29:14 +00003128 if (getDerived().parseName(State) == nullptr)
Richard Smithc20d1442018-08-20 20:14:49 +00003129 return nullptr;
3130 }
Nico Weber29294792019-04-03 23:14:33 +00003131 return make<CtorDtorName>(SoFar, /*IsDtor=*/false, Variant);
Richard Smithc20d1442018-08-20 20:14:49 +00003132 }
3133
Nico Weber29294792019-04-03 23:14:33 +00003134 if (look() == 'D' && (look(1) == '0' || look(1) == '1' || look(1) == '2' ||
3135 look(1) == '4' || look(1) == '5')) {
Pavel Labathf4e67eb2018-10-10 08:39:16 +00003136 int Variant = look(1) - '0';
Richard Smithc20d1442018-08-20 20:14:49 +00003137 First += 2;
3138 if (State) State->CtorDtorConversion = true;
Nico Weber29294792019-04-03 23:14:33 +00003139 return make<CtorDtorName>(SoFar, /*IsDtor=*/true, Variant);
Richard Smithc20d1442018-08-20 20:14:49 +00003140 }
3141
3142 return nullptr;
3143}
3144
3145// <nested-name> ::= N [<CV-Qualifiers>] [<ref-qualifier>] <prefix> <unqualified-name> E
3146// ::= N [<CV-Qualifiers>] [<ref-qualifier>] <template-prefix> <template-args> E
3147//
3148// <prefix> ::= <prefix> <unqualified-name>
3149// ::= <template-prefix> <template-args>
3150// ::= <template-param>
3151// ::= <decltype>
3152// ::= # empty
3153// ::= <substitution>
3154// ::= <prefix> <data-member-prefix>
3155// extension ::= L
3156//
3157// <data-member-prefix> := <member source-name> [<template-args>] M
3158//
3159// <template-prefix> ::= <prefix> <template unqualified-name>
3160// ::= <template-param>
3161// ::= <substitution>
Pavel Labathba825192018-10-16 14:29:14 +00003162template <typename Derived, typename Alloc>
3163Node *
3164AbstractManglingParser<Derived, Alloc>::parseNestedName(NameState *State) {
Richard Smithc20d1442018-08-20 20:14:49 +00003165 if (!consumeIf('N'))
3166 return nullptr;
3167
3168 Qualifiers CVTmp = parseCVQualifiers();
3169 if (State) State->CVQualifiers = CVTmp;
3170
3171 if (consumeIf('O')) {
3172 if (State) State->ReferenceQualifier = FrefQualRValue;
3173 } else if (consumeIf('R')) {
3174 if (State) State->ReferenceQualifier = FrefQualLValue;
3175 } else
3176 if (State) State->ReferenceQualifier = FrefQualNone;
3177
3178 Node *SoFar = nullptr;
3179 auto PushComponent = [&](Node *Comp) {
Richard Smithb485b352018-08-24 23:30:26 +00003180 if (!Comp) return false;
Richard Smithc20d1442018-08-20 20:14:49 +00003181 if (SoFar) SoFar = make<NestedName>(SoFar, Comp);
3182 else SoFar = Comp;
3183 if (State) State->EndsWithTemplateArgs = false;
Richard Smithb485b352018-08-24 23:30:26 +00003184 return SoFar != nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00003185 };
3186
Richard Smithb485b352018-08-24 23:30:26 +00003187 if (consumeIf("St")) {
Richard Smithc20d1442018-08-20 20:14:49 +00003188 SoFar = make<NameType>("std");
Richard Smithb485b352018-08-24 23:30:26 +00003189 if (!SoFar)
3190 return nullptr;
3191 }
Richard Smithc20d1442018-08-20 20:14:49 +00003192
3193 while (!consumeIf('E')) {
3194 consumeIf('L'); // extension
3195
3196 // <data-member-prefix> := <member source-name> [<template-args>] M
3197 if (consumeIf('M')) {
3198 if (SoFar == nullptr)
3199 return nullptr;
3200 continue;
3201 }
3202
3203 // ::= <template-param>
3204 if (look() == 'T') {
Pavel Labathba825192018-10-16 14:29:14 +00003205 if (!PushComponent(getDerived().parseTemplateParam()))
Richard Smithc20d1442018-08-20 20:14:49 +00003206 return nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00003207 Subs.push_back(SoFar);
3208 continue;
3209 }
3210
3211 // ::= <template-prefix> <template-args>
3212 if (look() == 'I') {
Pavel Labathba825192018-10-16 14:29:14 +00003213 Node *TA = getDerived().parseTemplateArgs(State != nullptr);
Richard Smithc20d1442018-08-20 20:14:49 +00003214 if (TA == nullptr || SoFar == nullptr)
3215 return nullptr;
3216 SoFar = make<NameWithTemplateArgs>(SoFar, TA);
Richard Smithb485b352018-08-24 23:30:26 +00003217 if (!SoFar)
3218 return nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00003219 if (State) State->EndsWithTemplateArgs = true;
3220 Subs.push_back(SoFar);
3221 continue;
3222 }
3223
3224 // ::= <decltype>
3225 if (look() == 'D' && (look(1) == 't' || look(1) == 'T')) {
Pavel Labathba825192018-10-16 14:29:14 +00003226 if (!PushComponent(getDerived().parseDecltype()))
Richard Smithc20d1442018-08-20 20:14:49 +00003227 return nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00003228 Subs.push_back(SoFar);
3229 continue;
3230 }
3231
3232 // ::= <substitution>
3233 if (look() == 'S' && look(1) != 't') {
Pavel Labathba825192018-10-16 14:29:14 +00003234 Node *S = getDerived().parseSubstitution();
Richard Smithb485b352018-08-24 23:30:26 +00003235 if (!PushComponent(S))
Richard Smithc20d1442018-08-20 20:14:49 +00003236 return nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00003237 if (SoFar != S)
3238 Subs.push_back(S);
3239 continue;
3240 }
3241
3242 // Parse an <unqualified-name> thats actually a <ctor-dtor-name>.
3243 if (look() == 'C' || (look() == 'D' && look(1) != 'C')) {
3244 if (SoFar == nullptr)
3245 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00003246 if (!PushComponent(getDerived().parseCtorDtorName(SoFar, State)))
Richard Smithc20d1442018-08-20 20:14:49 +00003247 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00003248 SoFar = getDerived().parseAbiTags(SoFar);
Richard Smithc20d1442018-08-20 20:14:49 +00003249 if (SoFar == nullptr)
3250 return nullptr;
3251 Subs.push_back(SoFar);
3252 continue;
3253 }
3254
3255 // ::= <prefix> <unqualified-name>
Pavel Labathba825192018-10-16 14:29:14 +00003256 if (!PushComponent(getDerived().parseUnqualifiedName(State)))
Richard Smithc20d1442018-08-20 20:14:49 +00003257 return nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00003258 Subs.push_back(SoFar);
3259 }
3260
3261 if (SoFar == nullptr || Subs.empty())
3262 return nullptr;
3263
3264 Subs.pop_back();
3265 return SoFar;
3266}
3267
3268// <simple-id> ::= <source-name> [ <template-args> ]
Pavel Labathba825192018-10-16 14:29:14 +00003269template <typename Derived, typename Alloc>
3270Node *AbstractManglingParser<Derived, Alloc>::parseSimpleId() {
3271 Node *SN = getDerived().parseSourceName(/*NameState=*/nullptr);
Richard Smithc20d1442018-08-20 20:14:49 +00003272 if (SN == nullptr)
3273 return nullptr;
3274 if (look() == 'I') {
Pavel Labathba825192018-10-16 14:29:14 +00003275 Node *TA = getDerived().parseTemplateArgs();
Richard Smithc20d1442018-08-20 20:14:49 +00003276 if (TA == nullptr)
3277 return nullptr;
3278 return make<NameWithTemplateArgs>(SN, TA);
3279 }
3280 return SN;
3281}
3282
3283// <destructor-name> ::= <unresolved-type> # e.g., ~T or ~decltype(f())
3284// ::= <simple-id> # e.g., ~A<2*N>
Pavel Labathba825192018-10-16 14:29:14 +00003285template <typename Derived, typename Alloc>
3286Node *AbstractManglingParser<Derived, Alloc>::parseDestructorName() {
Richard Smithc20d1442018-08-20 20:14:49 +00003287 Node *Result;
3288 if (std::isdigit(look()))
Pavel Labathba825192018-10-16 14:29:14 +00003289 Result = getDerived().parseSimpleId();
Richard Smithc20d1442018-08-20 20:14:49 +00003290 else
Pavel Labathba825192018-10-16 14:29:14 +00003291 Result = getDerived().parseUnresolvedType();
Richard Smithc20d1442018-08-20 20:14:49 +00003292 if (Result == nullptr)
3293 return nullptr;
3294 return make<DtorName>(Result);
3295}
3296
3297// <unresolved-type> ::= <template-param>
3298// ::= <decltype>
3299// ::= <substitution>
Pavel Labathba825192018-10-16 14:29:14 +00003300template <typename Derived, typename Alloc>
3301Node *AbstractManglingParser<Derived, Alloc>::parseUnresolvedType() {
Richard Smithc20d1442018-08-20 20:14:49 +00003302 if (look() == 'T') {
Pavel Labathba825192018-10-16 14:29:14 +00003303 Node *TP = getDerived().parseTemplateParam();
Richard Smithc20d1442018-08-20 20:14:49 +00003304 if (TP == nullptr)
3305 return nullptr;
3306 Subs.push_back(TP);
3307 return TP;
3308 }
3309 if (look() == 'D') {
Pavel Labathba825192018-10-16 14:29:14 +00003310 Node *DT = getDerived().parseDecltype();
Richard Smithc20d1442018-08-20 20:14:49 +00003311 if (DT == nullptr)
3312 return nullptr;
3313 Subs.push_back(DT);
3314 return DT;
3315 }
Pavel Labathba825192018-10-16 14:29:14 +00003316 return getDerived().parseSubstitution();
Richard Smithc20d1442018-08-20 20:14:49 +00003317}
3318
3319// <base-unresolved-name> ::= <simple-id> # unresolved name
3320// extension ::= <operator-name> # unresolved operator-function-id
3321// extension ::= <operator-name> <template-args> # unresolved operator template-id
3322// ::= on <operator-name> # unresolved operator-function-id
3323// ::= on <operator-name> <template-args> # unresolved operator template-id
3324// ::= dn <destructor-name> # destructor or pseudo-destructor;
3325// # e.g. ~X or ~X<N-1>
Pavel Labathba825192018-10-16 14:29:14 +00003326template <typename Derived, typename Alloc>
3327Node *AbstractManglingParser<Derived, Alloc>::parseBaseUnresolvedName() {
Richard Smithc20d1442018-08-20 20:14:49 +00003328 if (std::isdigit(look()))
Pavel Labathba825192018-10-16 14:29:14 +00003329 return getDerived().parseSimpleId();
Richard Smithc20d1442018-08-20 20:14:49 +00003330
3331 if (consumeIf("dn"))
Pavel Labathba825192018-10-16 14:29:14 +00003332 return getDerived().parseDestructorName();
Richard Smithc20d1442018-08-20 20:14:49 +00003333
3334 consumeIf("on");
3335
Pavel Labathba825192018-10-16 14:29:14 +00003336 Node *Oper = getDerived().parseOperatorName(/*NameState=*/nullptr);
Richard Smithc20d1442018-08-20 20:14:49 +00003337 if (Oper == nullptr)
3338 return nullptr;
3339 if (look() == 'I') {
Pavel Labathba825192018-10-16 14:29:14 +00003340 Node *TA = getDerived().parseTemplateArgs();
Richard Smithc20d1442018-08-20 20:14:49 +00003341 if (TA == nullptr)
3342 return nullptr;
3343 return make<NameWithTemplateArgs>(Oper, TA);
3344 }
3345 return Oper;
3346}
3347
3348// <unresolved-name>
3349// extension ::= srN <unresolved-type> [<template-args>] <unresolved-qualifier-level>* E <base-unresolved-name>
3350// ::= [gs] <base-unresolved-name> # x or (with "gs") ::x
3351// ::= [gs] sr <unresolved-qualifier-level>+ E <base-unresolved-name>
3352// # A::x, N::y, A<T>::z; "gs" means leading "::"
3353// ::= sr <unresolved-type> <base-unresolved-name> # T::x / decltype(p)::x
3354// extension ::= sr <unresolved-type> <template-args> <base-unresolved-name>
3355// # T::N::x /decltype(p)::N::x
3356// (ignored) ::= srN <unresolved-type> <unresolved-qualifier-level>+ E <base-unresolved-name>
3357//
3358// <unresolved-qualifier-level> ::= <simple-id>
Pavel Labathba825192018-10-16 14:29:14 +00003359template <typename Derived, typename Alloc>
3360Node *AbstractManglingParser<Derived, Alloc>::parseUnresolvedName() {
Richard Smithc20d1442018-08-20 20:14:49 +00003361 Node *SoFar = nullptr;
3362
3363 // srN <unresolved-type> [<template-args>] <unresolved-qualifier-level>* E <base-unresolved-name>
3364 // srN <unresolved-type> <unresolved-qualifier-level>+ E <base-unresolved-name>
3365 if (consumeIf("srN")) {
Pavel Labathba825192018-10-16 14:29:14 +00003366 SoFar = getDerived().parseUnresolvedType();
Richard Smithc20d1442018-08-20 20:14:49 +00003367 if (SoFar == nullptr)
3368 return nullptr;
3369
3370 if (look() == 'I') {
Pavel Labathba825192018-10-16 14:29:14 +00003371 Node *TA = getDerived().parseTemplateArgs();
Richard Smithc20d1442018-08-20 20:14:49 +00003372 if (TA == nullptr)
3373 return nullptr;
3374 SoFar = make<NameWithTemplateArgs>(SoFar, TA);
Richard Smithb485b352018-08-24 23:30:26 +00003375 if (!SoFar)
3376 return nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00003377 }
3378
3379 while (!consumeIf('E')) {
Pavel Labathba825192018-10-16 14:29:14 +00003380 Node *Qual = getDerived().parseSimpleId();
Richard Smithc20d1442018-08-20 20:14:49 +00003381 if (Qual == nullptr)
3382 return nullptr;
3383 SoFar = make<QualifiedName>(SoFar, Qual);
Richard Smithb485b352018-08-24 23:30:26 +00003384 if (!SoFar)
3385 return nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00003386 }
3387
Pavel Labathba825192018-10-16 14:29:14 +00003388 Node *Base = getDerived().parseBaseUnresolvedName();
Richard Smithc20d1442018-08-20 20:14:49 +00003389 if (Base == nullptr)
3390 return nullptr;
3391 return make<QualifiedName>(SoFar, Base);
3392 }
3393
3394 bool Global = consumeIf("gs");
3395
3396 // [gs] <base-unresolved-name> # x or (with "gs") ::x
3397 if (!consumeIf("sr")) {
Pavel Labathba825192018-10-16 14:29:14 +00003398 SoFar = getDerived().parseBaseUnresolvedName();
Richard Smithc20d1442018-08-20 20:14:49 +00003399 if (SoFar == nullptr)
3400 return nullptr;
3401 if (Global)
3402 SoFar = make<GlobalQualifiedName>(SoFar);
3403 return SoFar;
3404 }
3405
3406 // [gs] sr <unresolved-qualifier-level>+ E <base-unresolved-name>
3407 if (std::isdigit(look())) {
3408 do {
Pavel Labathba825192018-10-16 14:29:14 +00003409 Node *Qual = getDerived().parseSimpleId();
Richard Smithc20d1442018-08-20 20:14:49 +00003410 if (Qual == nullptr)
3411 return nullptr;
3412 if (SoFar)
3413 SoFar = make<QualifiedName>(SoFar, Qual);
3414 else if (Global)
3415 SoFar = make<GlobalQualifiedName>(Qual);
3416 else
3417 SoFar = Qual;
Richard Smithb485b352018-08-24 23:30:26 +00003418 if (!SoFar)
3419 return nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00003420 } while (!consumeIf('E'));
3421 }
3422 // sr <unresolved-type> <base-unresolved-name>
3423 // sr <unresolved-type> <template-args> <base-unresolved-name>
3424 else {
Pavel Labathba825192018-10-16 14:29:14 +00003425 SoFar = getDerived().parseUnresolvedType();
Richard Smithc20d1442018-08-20 20:14:49 +00003426 if (SoFar == nullptr)
3427 return nullptr;
3428
3429 if (look() == 'I') {
Pavel Labathba825192018-10-16 14:29:14 +00003430 Node *TA = getDerived().parseTemplateArgs();
Richard Smithc20d1442018-08-20 20:14:49 +00003431 if (TA == nullptr)
3432 return nullptr;
3433 SoFar = make<NameWithTemplateArgs>(SoFar, TA);
Richard Smithb485b352018-08-24 23:30:26 +00003434 if (!SoFar)
3435 return nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00003436 }
3437 }
3438
3439 assert(SoFar != nullptr);
3440
Pavel Labathba825192018-10-16 14:29:14 +00003441 Node *Base = getDerived().parseBaseUnresolvedName();
Richard Smithc20d1442018-08-20 20:14:49 +00003442 if (Base == nullptr)
3443 return nullptr;
3444 return make<QualifiedName>(SoFar, Base);
3445}
3446
3447// <abi-tags> ::= <abi-tag> [<abi-tags>]
3448// <abi-tag> ::= B <source-name>
Pavel Labathba825192018-10-16 14:29:14 +00003449template <typename Derived, typename Alloc>
3450Node *AbstractManglingParser<Derived, Alloc>::parseAbiTags(Node *N) {
Richard Smithc20d1442018-08-20 20:14:49 +00003451 while (consumeIf('B')) {
3452 StringView SN = parseBareSourceName();
3453 if (SN.empty())
3454 return nullptr;
3455 N = make<AbiTagAttr>(N, SN);
Richard Smithb485b352018-08-24 23:30:26 +00003456 if (!N)
3457 return nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00003458 }
3459 return N;
3460}
3461
3462// <number> ::= [n] <non-negative decimal integer>
Pavel Labathba825192018-10-16 14:29:14 +00003463template <typename Alloc, typename Derived>
3464StringView
3465AbstractManglingParser<Alloc, Derived>::parseNumber(bool AllowNegative) {
Richard Smithc20d1442018-08-20 20:14:49 +00003466 const char *Tmp = First;
3467 if (AllowNegative)
3468 consumeIf('n');
3469 if (numLeft() == 0 || !std::isdigit(*First))
3470 return StringView();
3471 while (numLeft() != 0 && std::isdigit(*First))
3472 ++First;
3473 return StringView(Tmp, First);
3474}
3475
3476// <positive length number> ::= [0-9]*
Pavel Labathba825192018-10-16 14:29:14 +00003477template <typename Alloc, typename Derived>
3478bool AbstractManglingParser<Alloc, Derived>::parsePositiveInteger(size_t *Out) {
Richard Smithc20d1442018-08-20 20:14:49 +00003479 *Out = 0;
3480 if (look() < '0' || look() > '9')
3481 return true;
3482 while (look() >= '0' && look() <= '9') {
3483 *Out *= 10;
3484 *Out += static_cast<size_t>(consume() - '0');
3485 }
3486 return false;
3487}
3488
Pavel Labathba825192018-10-16 14:29:14 +00003489template <typename Alloc, typename Derived>
3490StringView AbstractManglingParser<Alloc, Derived>::parseBareSourceName() {
Richard Smithc20d1442018-08-20 20:14:49 +00003491 size_t Int = 0;
3492 if (parsePositiveInteger(&Int) || numLeft() < Int)
3493 return StringView();
3494 StringView R(First, First + Int);
3495 First += Int;
3496 return R;
3497}
3498
3499// <function-type> ::= [<CV-qualifiers>] [<exception-spec>] [Dx] F [Y] <bare-function-type> [<ref-qualifier>] E
3500//
3501// <exception-spec> ::= Do # non-throwing exception-specification (e.g., noexcept, throw())
3502// ::= DO <expression> E # computed (instantiation-dependent) noexcept
3503// ::= Dw <type>+ E # dynamic exception specification with instantiation-dependent types
3504//
3505// <ref-qualifier> ::= R # & ref-qualifier
3506// <ref-qualifier> ::= O # && ref-qualifier
Pavel Labathba825192018-10-16 14:29:14 +00003507template <typename Derived, typename Alloc>
3508Node *AbstractManglingParser<Derived, Alloc>::parseFunctionType() {
Richard Smithc20d1442018-08-20 20:14:49 +00003509 Qualifiers CVQuals = parseCVQualifiers();
3510
3511 Node *ExceptionSpec = nullptr;
3512 if (consumeIf("Do")) {
3513 ExceptionSpec = make<NameType>("noexcept");
Richard Smithb485b352018-08-24 23:30:26 +00003514 if (!ExceptionSpec)
3515 return nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00003516 } else if (consumeIf("DO")) {
Pavel Labathba825192018-10-16 14:29:14 +00003517 Node *E = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00003518 if (E == nullptr || !consumeIf('E'))
3519 return nullptr;
3520 ExceptionSpec = make<NoexceptSpec>(E);
Richard Smithb485b352018-08-24 23:30:26 +00003521 if (!ExceptionSpec)
3522 return nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00003523 } else if (consumeIf("Dw")) {
3524 size_t SpecsBegin = Names.size();
3525 while (!consumeIf('E')) {
Pavel Labathba825192018-10-16 14:29:14 +00003526 Node *T = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00003527 if (T == nullptr)
3528 return nullptr;
3529 Names.push_back(T);
3530 }
3531 ExceptionSpec =
3532 make<DynamicExceptionSpec>(popTrailingNodeArray(SpecsBegin));
Richard Smithb485b352018-08-24 23:30:26 +00003533 if (!ExceptionSpec)
3534 return nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00003535 }
3536
3537 consumeIf("Dx"); // transaction safe
3538
3539 if (!consumeIf('F'))
3540 return nullptr;
3541 consumeIf('Y'); // extern "C"
Pavel Labathba825192018-10-16 14:29:14 +00003542 Node *ReturnType = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00003543 if (ReturnType == nullptr)
3544 return nullptr;
3545
3546 FunctionRefQual ReferenceQualifier = FrefQualNone;
3547 size_t ParamsBegin = Names.size();
3548 while (true) {
3549 if (consumeIf('E'))
3550 break;
3551 if (consumeIf('v'))
3552 continue;
3553 if (consumeIf("RE")) {
3554 ReferenceQualifier = FrefQualLValue;
3555 break;
3556 }
3557 if (consumeIf("OE")) {
3558 ReferenceQualifier = FrefQualRValue;
3559 break;
3560 }
Pavel Labathba825192018-10-16 14:29:14 +00003561 Node *T = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00003562 if (T == nullptr)
3563 return nullptr;
3564 Names.push_back(T);
3565 }
3566
3567 NodeArray Params = popTrailingNodeArray(ParamsBegin);
3568 return make<FunctionType>(ReturnType, Params, CVQuals,
3569 ReferenceQualifier, ExceptionSpec);
3570}
3571
3572// extension:
3573// <vector-type> ::= Dv <positive dimension number> _ <extended element type>
3574// ::= Dv [<dimension expression>] _ <element type>
3575// <extended element type> ::= <element type>
3576// ::= p # AltiVec vector pixel
Pavel Labathba825192018-10-16 14:29:14 +00003577template <typename Derived, typename Alloc>
3578Node *AbstractManglingParser<Derived, Alloc>::parseVectorType() {
Richard Smithc20d1442018-08-20 20:14:49 +00003579 if (!consumeIf("Dv"))
3580 return nullptr;
3581 if (look() >= '1' && look() <= '9') {
Erik Pilkingtond7555e32019-11-04 10:47:44 -08003582 Node *DimensionNumber = make<NameType>(parseNumber());
3583 if (!DimensionNumber)
3584 return nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00003585 if (!consumeIf('_'))
3586 return nullptr;
3587 if (consumeIf('p'))
3588 return make<PixelVectorType>(DimensionNumber);
Pavel Labathba825192018-10-16 14:29:14 +00003589 Node *ElemType = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00003590 if (ElemType == nullptr)
3591 return nullptr;
3592 return make<VectorType>(ElemType, DimensionNumber);
3593 }
3594
3595 if (!consumeIf('_')) {
Pavel Labathba825192018-10-16 14:29:14 +00003596 Node *DimExpr = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00003597 if (!DimExpr)
3598 return nullptr;
3599 if (!consumeIf('_'))
3600 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00003601 Node *ElemType = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00003602 if (!ElemType)
3603 return nullptr;
3604 return make<VectorType>(ElemType, DimExpr);
3605 }
Pavel Labathba825192018-10-16 14:29:14 +00003606 Node *ElemType = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00003607 if (!ElemType)
3608 return nullptr;
Erik Pilkingtond7555e32019-11-04 10:47:44 -08003609 return make<VectorType>(ElemType, /*Dimension=*/nullptr);
Richard Smithc20d1442018-08-20 20:14:49 +00003610}
3611
3612// <decltype> ::= Dt <expression> E # decltype of an id-expression or class member access (C++0x)
3613// ::= DT <expression> E # decltype of an expression (C++0x)
Pavel Labathba825192018-10-16 14:29:14 +00003614template <typename Derived, typename Alloc>
3615Node *AbstractManglingParser<Derived, Alloc>::parseDecltype() {
Richard Smithc20d1442018-08-20 20:14:49 +00003616 if (!consumeIf('D'))
3617 return nullptr;
3618 if (!consumeIf('t') && !consumeIf('T'))
3619 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00003620 Node *E = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00003621 if (E == nullptr)
3622 return nullptr;
3623 if (!consumeIf('E'))
3624 return nullptr;
3625 return make<EnclosingExpr>("decltype(", E, ")");
3626}
3627
3628// <array-type> ::= A <positive dimension number> _ <element type>
3629// ::= A [<dimension expression>] _ <element type>
Pavel Labathba825192018-10-16 14:29:14 +00003630template <typename Derived, typename Alloc>
3631Node *AbstractManglingParser<Derived, Alloc>::parseArrayType() {
Richard Smithc20d1442018-08-20 20:14:49 +00003632 if (!consumeIf('A'))
3633 return nullptr;
3634
Erik Pilkingtond7555e32019-11-04 10:47:44 -08003635 Node *Dimension = nullptr;
Pavel Labathf4e67eb2018-10-10 08:39:16 +00003636
Richard Smithc20d1442018-08-20 20:14:49 +00003637 if (std::isdigit(look())) {
Erik Pilkingtond7555e32019-11-04 10:47:44 -08003638 Dimension = make<NameType>(parseNumber());
3639 if (!Dimension)
3640 return nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00003641 if (!consumeIf('_'))
3642 return nullptr;
Pavel Labathf4e67eb2018-10-10 08:39:16 +00003643 } else if (!consumeIf('_')) {
Pavel Labathba825192018-10-16 14:29:14 +00003644 Node *DimExpr = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00003645 if (DimExpr == nullptr)
3646 return nullptr;
3647 if (!consumeIf('_'))
3648 return nullptr;
Pavel Labathf4e67eb2018-10-10 08:39:16 +00003649 Dimension = DimExpr;
Richard Smithc20d1442018-08-20 20:14:49 +00003650 }
3651
Pavel Labathba825192018-10-16 14:29:14 +00003652 Node *Ty = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00003653 if (Ty == nullptr)
3654 return nullptr;
Pavel Labathf4e67eb2018-10-10 08:39:16 +00003655 return make<ArrayType>(Ty, Dimension);
Richard Smithc20d1442018-08-20 20:14:49 +00003656}
3657
3658// <pointer-to-member-type> ::= M <class type> <member type>
Pavel Labathba825192018-10-16 14:29:14 +00003659template <typename Derived, typename Alloc>
3660Node *AbstractManglingParser<Derived, Alloc>::parsePointerToMemberType() {
Richard Smithc20d1442018-08-20 20:14:49 +00003661 if (!consumeIf('M'))
3662 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00003663 Node *ClassType = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00003664 if (ClassType == nullptr)
3665 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00003666 Node *MemberType = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00003667 if (MemberType == nullptr)
3668 return nullptr;
3669 return make<PointerToMemberType>(ClassType, MemberType);
3670}
3671
3672// <class-enum-type> ::= <name> # non-dependent type name, dependent type name, or dependent typename-specifier
3673// ::= Ts <name> # dependent elaborated type specifier using 'struct' or 'class'
3674// ::= Tu <name> # dependent elaborated type specifier using 'union'
3675// ::= Te <name> # dependent elaborated type specifier using 'enum'
Pavel Labathba825192018-10-16 14:29:14 +00003676template <typename Derived, typename Alloc>
3677Node *AbstractManglingParser<Derived, Alloc>::parseClassEnumType() {
Richard Smithc20d1442018-08-20 20:14:49 +00003678 StringView ElabSpef;
3679 if (consumeIf("Ts"))
3680 ElabSpef = "struct";
3681 else if (consumeIf("Tu"))
3682 ElabSpef = "union";
3683 else if (consumeIf("Te"))
3684 ElabSpef = "enum";
3685
Pavel Labathba825192018-10-16 14:29:14 +00003686 Node *Name = getDerived().parseName();
Richard Smithc20d1442018-08-20 20:14:49 +00003687 if (Name == nullptr)
3688 return nullptr;
3689
3690 if (!ElabSpef.empty())
3691 return make<ElaboratedTypeSpefType>(ElabSpef, Name);
3692
3693 return Name;
3694}
3695
3696// <qualified-type> ::= <qualifiers> <type>
3697// <qualifiers> ::= <extended-qualifier>* <CV-qualifiers>
3698// <extended-qualifier> ::= U <source-name> [<template-args>] # vendor extended type qualifier
Pavel Labathba825192018-10-16 14:29:14 +00003699template <typename Derived, typename Alloc>
3700Node *AbstractManglingParser<Derived, Alloc>::parseQualifiedType() {
Richard Smithc20d1442018-08-20 20:14:49 +00003701 if (consumeIf('U')) {
3702 StringView Qual = parseBareSourceName();
3703 if (Qual.empty())
3704 return nullptr;
3705
Richard Smithc20d1442018-08-20 20:14:49 +00003706 // extension ::= U <objc-name> <objc-type> # objc-type<identifier>
3707 if (Qual.startsWith("objcproto")) {
3708 StringView ProtoSourceName = Qual.dropFront(std::strlen("objcproto"));
3709 StringView Proto;
3710 {
3711 SwapAndRestore<const char *> SaveFirst(First, ProtoSourceName.begin()),
3712 SaveLast(Last, ProtoSourceName.end());
3713 Proto = parseBareSourceName();
3714 }
3715 if (Proto.empty())
3716 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00003717 Node *Child = getDerived().parseQualifiedType();
Richard Smithc20d1442018-08-20 20:14:49 +00003718 if (Child == nullptr)
3719 return nullptr;
3720 return make<ObjCProtoName>(Child, Proto);
3721 }
3722
Alex Orlovf50df922021-03-24 10:21:32 +04003723 Node *TA = nullptr;
3724 if (look() == 'I') {
3725 TA = getDerived().parseTemplateArgs();
3726 if (TA == nullptr)
3727 return nullptr;
3728 }
3729
Pavel Labathba825192018-10-16 14:29:14 +00003730 Node *Child = getDerived().parseQualifiedType();
Richard Smithc20d1442018-08-20 20:14:49 +00003731 if (Child == nullptr)
3732 return nullptr;
Alex Orlovf50df922021-03-24 10:21:32 +04003733 return make<VendorExtQualType>(Child, Qual, TA);
Richard Smithc20d1442018-08-20 20:14:49 +00003734 }
3735
3736 Qualifiers Quals = parseCVQualifiers();
Pavel Labathba825192018-10-16 14:29:14 +00003737 Node *Ty = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00003738 if (Ty == nullptr)
3739 return nullptr;
3740 if (Quals != QualNone)
3741 Ty = make<QualType>(Ty, Quals);
3742 return Ty;
3743}
3744
3745// <type> ::= <builtin-type>
3746// ::= <qualified-type>
3747// ::= <function-type>
3748// ::= <class-enum-type>
3749// ::= <array-type>
3750// ::= <pointer-to-member-type>
3751// ::= <template-param>
3752// ::= <template-template-param> <template-args>
3753// ::= <decltype>
3754// ::= P <type> # pointer
3755// ::= R <type> # l-value reference
3756// ::= O <type> # r-value reference (C++11)
3757// ::= C <type> # complex pair (C99)
3758// ::= G <type> # imaginary (C99)
3759// ::= <substitution> # See Compression below
3760// extension ::= U <objc-name> <objc-type> # objc-type<identifier>
3761// extension ::= <vector-type> # <vector-type> starts with Dv
3762//
3763// <objc-name> ::= <k0 number> objcproto <k1 number> <identifier> # k0 = 9 + <number of digits in k1> + k1
3764// <objc-type> ::= <source-name> # PU<11+>objcproto 11objc_object<source-name> 11objc_object -> id<source-name>
Pavel Labathba825192018-10-16 14:29:14 +00003765template <typename Derived, typename Alloc>
3766Node *AbstractManglingParser<Derived, Alloc>::parseType() {
Richard Smithc20d1442018-08-20 20:14:49 +00003767 Node *Result = nullptr;
3768
Richard Smithc20d1442018-08-20 20:14:49 +00003769 switch (look()) {
3770 // ::= <qualified-type>
3771 case 'r':
3772 case 'V':
3773 case 'K': {
3774 unsigned AfterQuals = 0;
3775 if (look(AfterQuals) == 'r') ++AfterQuals;
3776 if (look(AfterQuals) == 'V') ++AfterQuals;
3777 if (look(AfterQuals) == 'K') ++AfterQuals;
3778
3779 if (look(AfterQuals) == 'F' ||
3780 (look(AfterQuals) == 'D' &&
3781 (look(AfterQuals + 1) == 'o' || look(AfterQuals + 1) == 'O' ||
3782 look(AfterQuals + 1) == 'w' || look(AfterQuals + 1) == 'x'))) {
Pavel Labathba825192018-10-16 14:29:14 +00003783 Result = getDerived().parseFunctionType();
Richard Smithc20d1442018-08-20 20:14:49 +00003784 break;
3785 }
Erik Pilkingtonf70e4d82019-01-17 20:37:51 +00003786 DEMANGLE_FALLTHROUGH;
Richard Smithc20d1442018-08-20 20:14:49 +00003787 }
3788 case 'U': {
Pavel Labathba825192018-10-16 14:29:14 +00003789 Result = getDerived().parseQualifiedType();
Richard Smithc20d1442018-08-20 20:14:49 +00003790 break;
3791 }
3792 // <builtin-type> ::= v # void
3793 case 'v':
3794 ++First;
3795 return make<NameType>("void");
3796 // ::= w # wchar_t
3797 case 'w':
3798 ++First;
3799 return make<NameType>("wchar_t");
3800 // ::= b # bool
3801 case 'b':
3802 ++First;
3803 return make<NameType>("bool");
3804 // ::= c # char
3805 case 'c':
3806 ++First;
3807 return make<NameType>("char");
3808 // ::= a # signed char
3809 case 'a':
3810 ++First;
3811 return make<NameType>("signed char");
3812 // ::= h # unsigned char
3813 case 'h':
3814 ++First;
3815 return make<NameType>("unsigned char");
3816 // ::= s # short
3817 case 's':
3818 ++First;
3819 return make<NameType>("short");
3820 // ::= t # unsigned short
3821 case 't':
3822 ++First;
3823 return make<NameType>("unsigned short");
3824 // ::= i # int
3825 case 'i':
3826 ++First;
3827 return make<NameType>("int");
3828 // ::= j # unsigned int
3829 case 'j':
3830 ++First;
3831 return make<NameType>("unsigned int");
3832 // ::= l # long
3833 case 'l':
3834 ++First;
3835 return make<NameType>("long");
3836 // ::= m # unsigned long
3837 case 'm':
3838 ++First;
3839 return make<NameType>("unsigned long");
3840 // ::= x # long long, __int64
3841 case 'x':
3842 ++First;
3843 return make<NameType>("long long");
3844 // ::= y # unsigned long long, __int64
3845 case 'y':
3846 ++First;
3847 return make<NameType>("unsigned long long");
3848 // ::= n # __int128
3849 case 'n':
3850 ++First;
3851 return make<NameType>("__int128");
3852 // ::= o # unsigned __int128
3853 case 'o':
3854 ++First;
3855 return make<NameType>("unsigned __int128");
3856 // ::= f # float
3857 case 'f':
3858 ++First;
3859 return make<NameType>("float");
3860 // ::= d # double
3861 case 'd':
3862 ++First;
3863 return make<NameType>("double");
3864 // ::= e # long double, __float80
3865 case 'e':
3866 ++First;
3867 return make<NameType>("long double");
3868 // ::= g # __float128
3869 case 'g':
3870 ++First;
3871 return make<NameType>("__float128");
3872 // ::= z # ellipsis
3873 case 'z':
3874 ++First;
3875 return make<NameType>("...");
3876
3877 // <builtin-type> ::= u <source-name> # vendor extended type
3878 case 'u': {
3879 ++First;
3880 StringView Res = parseBareSourceName();
3881 if (Res.empty())
3882 return nullptr;
Erik Pilkingtonb94a1f42019-06-10 21:02:39 +00003883 // Typically, <builtin-type>s are not considered substitution candidates,
3884 // but the exception to that exception is vendor extended types (Itanium C++
3885 // ABI 5.9.1).
3886 Result = make<NameType>(Res);
3887 break;
Richard Smithc20d1442018-08-20 20:14:49 +00003888 }
3889 case 'D':
3890 switch (look(1)) {
3891 // ::= Dd # IEEE 754r decimal floating point (64 bits)
3892 case 'd':
3893 First += 2;
3894 return make<NameType>("decimal64");
3895 // ::= De # IEEE 754r decimal floating point (128 bits)
3896 case 'e':
3897 First += 2;
3898 return make<NameType>("decimal128");
3899 // ::= Df # IEEE 754r decimal floating point (32 bits)
3900 case 'f':
3901 First += 2;
3902 return make<NameType>("decimal32");
3903 // ::= Dh # IEEE 754r half-precision floating point (16 bits)
3904 case 'h':
3905 First += 2;
Stuart Bradye8bf5772021-06-07 16:30:22 +01003906 return make<NameType>("half");
Richard Smithc20d1442018-08-20 20:14:49 +00003907 // ::= Di # char32_t
3908 case 'i':
3909 First += 2;
3910 return make<NameType>("char32_t");
3911 // ::= Ds # char16_t
3912 case 's':
3913 First += 2;
3914 return make<NameType>("char16_t");
Erik Pilkingtonc3780e82019-06-28 19:54:19 +00003915 // ::= Du # char8_t (C++2a, not yet in the Itanium spec)
3916 case 'u':
3917 First += 2;
3918 return make<NameType>("char8_t");
Richard Smithc20d1442018-08-20 20:14:49 +00003919 // ::= Da # auto (in dependent new-expressions)
3920 case 'a':
3921 First += 2;
3922 return make<NameType>("auto");
3923 // ::= Dc # decltype(auto)
3924 case 'c':
3925 First += 2;
3926 return make<NameType>("decltype(auto)");
3927 // ::= Dn # std::nullptr_t (i.e., decltype(nullptr))
3928 case 'n':
3929 First += 2;
3930 return make<NameType>("std::nullptr_t");
3931
3932 // ::= <decltype>
3933 case 't':
3934 case 'T': {
Pavel Labathba825192018-10-16 14:29:14 +00003935 Result = getDerived().parseDecltype();
Richard Smithc20d1442018-08-20 20:14:49 +00003936 break;
3937 }
3938 // extension ::= <vector-type> # <vector-type> starts with Dv
3939 case 'v': {
Pavel Labathba825192018-10-16 14:29:14 +00003940 Result = getDerived().parseVectorType();
Richard Smithc20d1442018-08-20 20:14:49 +00003941 break;
3942 }
3943 // ::= Dp <type> # pack expansion (C++0x)
3944 case 'p': {
3945 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00003946 Node *Child = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00003947 if (!Child)
3948 return nullptr;
3949 Result = make<ParameterPackExpansion>(Child);
3950 break;
3951 }
3952 // Exception specifier on a function type.
3953 case 'o':
3954 case 'O':
3955 case 'w':
3956 // Transaction safe function type.
3957 case 'x':
Pavel Labathba825192018-10-16 14:29:14 +00003958 Result = getDerived().parseFunctionType();
Richard Smithc20d1442018-08-20 20:14:49 +00003959 break;
3960 }
3961 break;
3962 // ::= <function-type>
3963 case 'F': {
Pavel Labathba825192018-10-16 14:29:14 +00003964 Result = getDerived().parseFunctionType();
Richard Smithc20d1442018-08-20 20:14:49 +00003965 break;
3966 }
3967 // ::= <array-type>
3968 case 'A': {
Pavel Labathba825192018-10-16 14:29:14 +00003969 Result = getDerived().parseArrayType();
Richard Smithc20d1442018-08-20 20:14:49 +00003970 break;
3971 }
3972 // ::= <pointer-to-member-type>
3973 case 'M': {
Pavel Labathba825192018-10-16 14:29:14 +00003974 Result = getDerived().parsePointerToMemberType();
Richard Smithc20d1442018-08-20 20:14:49 +00003975 break;
3976 }
3977 // ::= <template-param>
3978 case 'T': {
3979 // This could be an elaborate type specifier on a <class-enum-type>.
3980 if (look(1) == 's' || look(1) == 'u' || look(1) == 'e') {
Pavel Labathba825192018-10-16 14:29:14 +00003981 Result = getDerived().parseClassEnumType();
Richard Smithc20d1442018-08-20 20:14:49 +00003982 break;
3983 }
3984
Pavel Labathba825192018-10-16 14:29:14 +00003985 Result = getDerived().parseTemplateParam();
Richard Smithc20d1442018-08-20 20:14:49 +00003986 if (Result == nullptr)
3987 return nullptr;
3988
3989 // Result could be either of:
3990 // <type> ::= <template-param>
3991 // <type> ::= <template-template-param> <template-args>
3992 //
3993 // <template-template-param> ::= <template-param>
3994 // ::= <substitution>
3995 //
3996 // If this is followed by some <template-args>, and we're permitted to
3997 // parse them, take the second production.
3998
3999 if (TryToParseTemplateArgs && look() == 'I') {
Pavel Labathba825192018-10-16 14:29:14 +00004000 Node *TA = getDerived().parseTemplateArgs();
Richard Smithc20d1442018-08-20 20:14:49 +00004001 if (TA == nullptr)
4002 return nullptr;
4003 Result = make<NameWithTemplateArgs>(Result, TA);
4004 }
4005 break;
4006 }
4007 // ::= P <type> # pointer
4008 case 'P': {
4009 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00004010 Node *Ptr = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00004011 if (Ptr == nullptr)
4012 return nullptr;
4013 Result = make<PointerType>(Ptr);
4014 break;
4015 }
4016 // ::= R <type> # l-value reference
4017 case 'R': {
4018 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00004019 Node *Ref = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00004020 if (Ref == nullptr)
4021 return nullptr;
4022 Result = make<ReferenceType>(Ref, ReferenceKind::LValue);
4023 break;
4024 }
4025 // ::= O <type> # r-value reference (C++11)
4026 case 'O': {
4027 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00004028 Node *Ref = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00004029 if (Ref == nullptr)
4030 return nullptr;
4031 Result = make<ReferenceType>(Ref, ReferenceKind::RValue);
4032 break;
4033 }
4034 // ::= C <type> # complex pair (C99)
4035 case 'C': {
4036 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00004037 Node *P = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00004038 if (P == nullptr)
4039 return nullptr;
4040 Result = make<PostfixQualifiedType>(P, " complex");
4041 break;
4042 }
4043 // ::= G <type> # imaginary (C99)
4044 case 'G': {
4045 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00004046 Node *P = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00004047 if (P == nullptr)
4048 return P;
4049 Result = make<PostfixQualifiedType>(P, " imaginary");
4050 break;
4051 }
4052 // ::= <substitution> # See Compression below
4053 case 'S': {
4054 if (look(1) && look(1) != 't') {
Pavel Labathba825192018-10-16 14:29:14 +00004055 Node *Sub = getDerived().parseSubstitution();
Richard Smithc20d1442018-08-20 20:14:49 +00004056 if (Sub == nullptr)
4057 return nullptr;
4058
4059 // Sub could be either of:
4060 // <type> ::= <substitution>
4061 // <type> ::= <template-template-param> <template-args>
4062 //
4063 // <template-template-param> ::= <template-param>
4064 // ::= <substitution>
4065 //
4066 // If this is followed by some <template-args>, and we're permitted to
4067 // parse them, take the second production.
4068
4069 if (TryToParseTemplateArgs && look() == 'I') {
Pavel Labathba825192018-10-16 14:29:14 +00004070 Node *TA = getDerived().parseTemplateArgs();
Richard Smithc20d1442018-08-20 20:14:49 +00004071 if (TA == nullptr)
4072 return nullptr;
4073 Result = make<NameWithTemplateArgs>(Sub, TA);
4074 break;
4075 }
4076
4077 // If all we parsed was a substitution, don't re-insert into the
4078 // substitution table.
4079 return Sub;
4080 }
Erik Pilkingtonf70e4d82019-01-17 20:37:51 +00004081 DEMANGLE_FALLTHROUGH;
Richard Smithc20d1442018-08-20 20:14:49 +00004082 }
4083 // ::= <class-enum-type>
4084 default: {
Pavel Labathba825192018-10-16 14:29:14 +00004085 Result = getDerived().parseClassEnumType();
Richard Smithc20d1442018-08-20 20:14:49 +00004086 break;
4087 }
4088 }
4089
4090 // If we parsed a type, insert it into the substitution table. Note that all
4091 // <builtin-type>s and <substitution>s have already bailed out, because they
4092 // don't get substitutions.
4093 if (Result != nullptr)
4094 Subs.push_back(Result);
4095 return Result;
4096}
4097
Pavel Labathba825192018-10-16 14:29:14 +00004098template <typename Derived, typename Alloc>
4099Node *AbstractManglingParser<Derived, Alloc>::parsePrefixExpr(StringView Kind) {
4100 Node *E = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004101 if (E == nullptr)
4102 return nullptr;
4103 return make<PrefixExpr>(Kind, E);
4104}
4105
Pavel Labathba825192018-10-16 14:29:14 +00004106template <typename Derived, typename Alloc>
4107Node *AbstractManglingParser<Derived, Alloc>::parseBinaryExpr(StringView Kind) {
4108 Node *LHS = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004109 if (LHS == nullptr)
4110 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00004111 Node *RHS = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004112 if (RHS == nullptr)
4113 return nullptr;
4114 return make<BinaryExpr>(LHS, Kind, RHS);
4115}
4116
Pavel Labathba825192018-10-16 14:29:14 +00004117template <typename Derived, typename Alloc>
4118Node *
4119AbstractManglingParser<Derived, Alloc>::parseIntegerLiteral(StringView Lit) {
Richard Smithc20d1442018-08-20 20:14:49 +00004120 StringView Tmp = parseNumber(true);
4121 if (!Tmp.empty() && consumeIf('E'))
4122 return make<IntegerLiteral>(Lit, Tmp);
4123 return nullptr;
4124}
4125
4126// <CV-Qualifiers> ::= [r] [V] [K]
Pavel Labathba825192018-10-16 14:29:14 +00004127template <typename Alloc, typename Derived>
4128Qualifiers AbstractManglingParser<Alloc, Derived>::parseCVQualifiers() {
Richard Smithc20d1442018-08-20 20:14:49 +00004129 Qualifiers CVR = QualNone;
4130 if (consumeIf('r'))
4131 CVR |= QualRestrict;
4132 if (consumeIf('V'))
4133 CVR |= QualVolatile;
4134 if (consumeIf('K'))
4135 CVR |= QualConst;
4136 return CVR;
4137}
4138
4139// <function-param> ::= fp <top-level CV-Qualifiers> _ # L == 0, first parameter
4140// ::= fp <top-level CV-Qualifiers> <parameter-2 non-negative number> _ # L == 0, second and later parameters
4141// ::= fL <L-1 non-negative number> p <top-level CV-Qualifiers> _ # L > 0, first parameter
4142// ::= fL <L-1 non-negative number> p <top-level CV-Qualifiers> <parameter-2 non-negative number> _ # L > 0, second and later parameters
Erik Pilkington91c24af2020-05-13 22:19:45 -04004143// ::= fpT # 'this' expression (not part of standard?)
Pavel Labathba825192018-10-16 14:29:14 +00004144template <typename Derived, typename Alloc>
4145Node *AbstractManglingParser<Derived, Alloc>::parseFunctionParam() {
Erik Pilkington91c24af2020-05-13 22:19:45 -04004146 if (consumeIf("fpT"))
4147 return make<NameType>("this");
Richard Smithc20d1442018-08-20 20:14:49 +00004148 if (consumeIf("fp")) {
4149 parseCVQualifiers();
4150 StringView Num = parseNumber();
4151 if (!consumeIf('_'))
4152 return nullptr;
4153 return make<FunctionParam>(Num);
4154 }
4155 if (consumeIf("fL")) {
4156 if (parseNumber().empty())
4157 return nullptr;
4158 if (!consumeIf('p'))
4159 return nullptr;
4160 parseCVQualifiers();
4161 StringView Num = parseNumber();
4162 if (!consumeIf('_'))
4163 return nullptr;
4164 return make<FunctionParam>(Num);
4165 }
4166 return nullptr;
4167}
4168
4169// [gs] nw <expression>* _ <type> E # new (expr-list) type
4170// [gs] nw <expression>* _ <type> <initializer> # new (expr-list) type (init)
4171// [gs] na <expression>* _ <type> E # new[] (expr-list) type
4172// [gs] na <expression>* _ <type> <initializer> # new[] (expr-list) type (init)
4173// <initializer> ::= pi <expression>* E # parenthesized initialization
Pavel Labathba825192018-10-16 14:29:14 +00004174template <typename Derived, typename Alloc>
4175Node *AbstractManglingParser<Derived, Alloc>::parseNewExpr() {
Richard Smithc20d1442018-08-20 20:14:49 +00004176 bool Global = consumeIf("gs");
4177 bool IsArray = look(1) == 'a';
4178 if (!consumeIf("nw") && !consumeIf("na"))
4179 return nullptr;
4180 size_t Exprs = Names.size();
4181 while (!consumeIf('_')) {
Pavel Labathba825192018-10-16 14:29:14 +00004182 Node *Ex = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004183 if (Ex == nullptr)
4184 return nullptr;
4185 Names.push_back(Ex);
4186 }
4187 NodeArray ExprList = popTrailingNodeArray(Exprs);
Pavel Labathba825192018-10-16 14:29:14 +00004188 Node *Ty = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00004189 if (Ty == nullptr)
4190 return Ty;
4191 if (consumeIf("pi")) {
4192 size_t InitsBegin = Names.size();
4193 while (!consumeIf('E')) {
Pavel Labathba825192018-10-16 14:29:14 +00004194 Node *Init = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004195 if (Init == nullptr)
4196 return Init;
4197 Names.push_back(Init);
4198 }
4199 NodeArray Inits = popTrailingNodeArray(InitsBegin);
4200 return make<NewExpr>(ExprList, Ty, Inits, Global, IsArray);
4201 } else if (!consumeIf('E'))
4202 return nullptr;
4203 return make<NewExpr>(ExprList, Ty, NodeArray(), Global, IsArray);
4204}
4205
4206// cv <type> <expression> # conversion with one argument
4207// cv <type> _ <expression>* E # conversion with a different number of arguments
Pavel Labathba825192018-10-16 14:29:14 +00004208template <typename Derived, typename Alloc>
4209Node *AbstractManglingParser<Derived, Alloc>::parseConversionExpr() {
Richard Smithc20d1442018-08-20 20:14:49 +00004210 if (!consumeIf("cv"))
4211 return nullptr;
4212 Node *Ty;
4213 {
4214 SwapAndRestore<bool> SaveTemp(TryToParseTemplateArgs, false);
Pavel Labathba825192018-10-16 14:29:14 +00004215 Ty = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00004216 }
4217
4218 if (Ty == nullptr)
4219 return nullptr;
4220
4221 if (consumeIf('_')) {
4222 size_t ExprsBegin = Names.size();
4223 while (!consumeIf('E')) {
Pavel Labathba825192018-10-16 14:29:14 +00004224 Node *E = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004225 if (E == nullptr)
4226 return E;
4227 Names.push_back(E);
4228 }
4229 NodeArray Exprs = popTrailingNodeArray(ExprsBegin);
4230 return make<ConversionExpr>(Ty, Exprs);
4231 }
4232
Pavel Labathba825192018-10-16 14:29:14 +00004233 Node *E[1] = {getDerived().parseExpr()};
Richard Smithc20d1442018-08-20 20:14:49 +00004234 if (E[0] == nullptr)
4235 return nullptr;
4236 return make<ConversionExpr>(Ty, makeNodeArray(E, E + 1));
4237}
4238
4239// <expr-primary> ::= L <type> <value number> E # integer literal
4240// ::= L <type> <value float> E # floating literal
4241// ::= L <string type> E # string literal
4242// ::= L <nullptr type> E # nullptr literal (i.e., "LDnE")
Richard Smithdf1c14c2019-09-06 23:53:21 +00004243// ::= L <lambda type> E # lambda expression
Richard Smithc20d1442018-08-20 20:14:49 +00004244// FIXME: ::= L <type> <real-part float> _ <imag-part float> E # complex floating point literal (C 2000)
4245// ::= L <mangled-name> E # external name
Pavel Labathba825192018-10-16 14:29:14 +00004246template <typename Derived, typename Alloc>
4247Node *AbstractManglingParser<Derived, Alloc>::parseExprPrimary() {
Richard Smithc20d1442018-08-20 20:14:49 +00004248 if (!consumeIf('L'))
4249 return nullptr;
4250 switch (look()) {
4251 case 'w':
4252 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00004253 return getDerived().parseIntegerLiteral("wchar_t");
Richard Smithc20d1442018-08-20 20:14:49 +00004254 case 'b':
4255 if (consumeIf("b0E"))
4256 return make<BoolExpr>(0);
4257 if (consumeIf("b1E"))
4258 return make<BoolExpr>(1);
4259 return nullptr;
4260 case 'c':
4261 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00004262 return getDerived().parseIntegerLiteral("char");
Richard Smithc20d1442018-08-20 20:14:49 +00004263 case 'a':
4264 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00004265 return getDerived().parseIntegerLiteral("signed char");
Richard Smithc20d1442018-08-20 20:14:49 +00004266 case 'h':
4267 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00004268 return getDerived().parseIntegerLiteral("unsigned char");
Richard Smithc20d1442018-08-20 20:14:49 +00004269 case 's':
4270 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00004271 return getDerived().parseIntegerLiteral("short");
Richard Smithc20d1442018-08-20 20:14:49 +00004272 case 't':
4273 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00004274 return getDerived().parseIntegerLiteral("unsigned short");
Richard Smithc20d1442018-08-20 20:14:49 +00004275 case 'i':
4276 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00004277 return getDerived().parseIntegerLiteral("");
Richard Smithc20d1442018-08-20 20:14:49 +00004278 case 'j':
4279 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00004280 return getDerived().parseIntegerLiteral("u");
Richard Smithc20d1442018-08-20 20:14:49 +00004281 case 'l':
4282 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00004283 return getDerived().parseIntegerLiteral("l");
Richard Smithc20d1442018-08-20 20:14:49 +00004284 case 'm':
4285 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00004286 return getDerived().parseIntegerLiteral("ul");
Richard Smithc20d1442018-08-20 20:14:49 +00004287 case 'x':
4288 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00004289 return getDerived().parseIntegerLiteral("ll");
Richard Smithc20d1442018-08-20 20:14:49 +00004290 case 'y':
4291 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00004292 return getDerived().parseIntegerLiteral("ull");
Richard Smithc20d1442018-08-20 20:14:49 +00004293 case 'n':
4294 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00004295 return getDerived().parseIntegerLiteral("__int128");
Richard Smithc20d1442018-08-20 20:14:49 +00004296 case 'o':
4297 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00004298 return getDerived().parseIntegerLiteral("unsigned __int128");
Richard Smithc20d1442018-08-20 20:14:49 +00004299 case 'f':
4300 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00004301 return getDerived().template parseFloatingLiteral<float>();
Richard Smithc20d1442018-08-20 20:14:49 +00004302 case 'd':
4303 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00004304 return getDerived().template parseFloatingLiteral<double>();
Richard Smithc20d1442018-08-20 20:14:49 +00004305 case 'e':
4306 ++First;
Xing Xue3dc5e082020-04-15 09:59:06 -04004307#if defined(__powerpc__) || defined(__s390__)
4308 // Handle cases where long doubles encoded with e have the same size
4309 // and representation as doubles.
4310 return getDerived().template parseFloatingLiteral<double>();
4311#else
Pavel Labathba825192018-10-16 14:29:14 +00004312 return getDerived().template parseFloatingLiteral<long double>();
Xing Xue3dc5e082020-04-15 09:59:06 -04004313#endif
Richard Smithc20d1442018-08-20 20:14:49 +00004314 case '_':
4315 if (consumeIf("_Z")) {
Pavel Labathba825192018-10-16 14:29:14 +00004316 Node *R = getDerived().parseEncoding();
Richard Smithc20d1442018-08-20 20:14:49 +00004317 if (R != nullptr && consumeIf('E'))
4318 return R;
4319 }
4320 return nullptr;
Richard Smithdf1c14c2019-09-06 23:53:21 +00004321 case 'A': {
4322 Node *T = getDerived().parseType();
4323 if (T == nullptr)
4324 return nullptr;
4325 // FIXME: We need to include the string contents in the mangling.
4326 if (consumeIf('E'))
4327 return make<StringLiteral>(T);
4328 return nullptr;
4329 }
4330 case 'D':
4331 if (consumeIf("DnE"))
4332 return make<NameType>("nullptr");
4333 return nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00004334 case 'T':
4335 // Invalid mangled name per
4336 // http://sourcerytools.com/pipermail/cxx-abi-dev/2011-August/002422.html
4337 return nullptr;
Richard Smithfb917462019-09-09 22:26:04 +00004338 case 'U': {
4339 // FIXME: Should we support LUb... for block literals?
4340 if (look(1) != 'l')
4341 return nullptr;
4342 Node *T = parseUnnamedTypeName(nullptr);
4343 if (!T || !consumeIf('E'))
4344 return nullptr;
4345 return make<LambdaExpr>(T);
4346 }
Richard Smithc20d1442018-08-20 20:14:49 +00004347 default: {
4348 // might be named type
Pavel Labathba825192018-10-16 14:29:14 +00004349 Node *T = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00004350 if (T == nullptr)
4351 return nullptr;
Erik Pilkington0a170f12020-05-13 14:13:37 -04004352 StringView N = parseNumber(/*AllowNegative=*/true);
Richard Smithfb917462019-09-09 22:26:04 +00004353 if (N.empty())
4354 return nullptr;
4355 if (!consumeIf('E'))
4356 return nullptr;
Erik Pilkington0a170f12020-05-13 14:13:37 -04004357 return make<EnumLiteral>(T, N);
Richard Smithc20d1442018-08-20 20:14:49 +00004358 }
4359 }
4360}
4361
4362// <braced-expression> ::= <expression>
4363// ::= di <field source-name> <braced-expression> # .name = expr
4364// ::= dx <index expression> <braced-expression> # [expr] = expr
4365// ::= dX <range begin expression> <range end expression> <braced-expression>
Pavel Labathba825192018-10-16 14:29:14 +00004366template <typename Derived, typename Alloc>
4367Node *AbstractManglingParser<Derived, Alloc>::parseBracedExpr() {
Richard Smithc20d1442018-08-20 20:14:49 +00004368 if (look() == 'd') {
4369 switch (look(1)) {
4370 case 'i': {
4371 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004372 Node *Field = getDerived().parseSourceName(/*NameState=*/nullptr);
Richard Smithc20d1442018-08-20 20:14:49 +00004373 if (Field == nullptr)
4374 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00004375 Node *Init = getDerived().parseBracedExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004376 if (Init == nullptr)
4377 return nullptr;
4378 return make<BracedExpr>(Field, Init, /*isArray=*/false);
4379 }
4380 case 'x': {
4381 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004382 Node *Index = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004383 if (Index == nullptr)
4384 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00004385 Node *Init = getDerived().parseBracedExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004386 if (Init == nullptr)
4387 return nullptr;
4388 return make<BracedExpr>(Index, Init, /*isArray=*/true);
4389 }
4390 case 'X': {
4391 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004392 Node *RangeBegin = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004393 if (RangeBegin == nullptr)
4394 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00004395 Node *RangeEnd = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004396 if (RangeEnd == nullptr)
4397 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00004398 Node *Init = getDerived().parseBracedExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004399 if (Init == nullptr)
4400 return nullptr;
4401 return make<BracedRangeExpr>(RangeBegin, RangeEnd, Init);
4402 }
4403 }
4404 }
Pavel Labathba825192018-10-16 14:29:14 +00004405 return getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004406}
4407
4408// (not yet in the spec)
4409// <fold-expr> ::= fL <binary-operator-name> <expression> <expression>
4410// ::= fR <binary-operator-name> <expression> <expression>
4411// ::= fl <binary-operator-name> <expression>
4412// ::= fr <binary-operator-name> <expression>
Pavel Labathba825192018-10-16 14:29:14 +00004413template <typename Derived, typename Alloc>
4414Node *AbstractManglingParser<Derived, Alloc>::parseFoldExpr() {
Richard Smithc20d1442018-08-20 20:14:49 +00004415 if (!consumeIf('f'))
4416 return nullptr;
4417
4418 char FoldKind = look();
4419 bool IsLeftFold, HasInitializer;
4420 HasInitializer = FoldKind == 'L' || FoldKind == 'R';
4421 if (FoldKind == 'l' || FoldKind == 'L')
4422 IsLeftFold = true;
4423 else if (FoldKind == 'r' || FoldKind == 'R')
4424 IsLeftFold = false;
4425 else
4426 return nullptr;
4427 ++First;
4428
4429 // FIXME: This map is duplicated in parseOperatorName and parseExpr.
4430 StringView OperatorName;
4431 if (consumeIf("aa")) OperatorName = "&&";
4432 else if (consumeIf("an")) OperatorName = "&";
4433 else if (consumeIf("aN")) OperatorName = "&=";
4434 else if (consumeIf("aS")) OperatorName = "=";
4435 else if (consumeIf("cm")) OperatorName = ",";
4436 else if (consumeIf("ds")) OperatorName = ".*";
4437 else if (consumeIf("dv")) OperatorName = "/";
4438 else if (consumeIf("dV")) OperatorName = "/=";
4439 else if (consumeIf("eo")) OperatorName = "^";
4440 else if (consumeIf("eO")) OperatorName = "^=";
4441 else if (consumeIf("eq")) OperatorName = "==";
4442 else if (consumeIf("ge")) OperatorName = ">=";
4443 else if (consumeIf("gt")) OperatorName = ">";
4444 else if (consumeIf("le")) OperatorName = "<=";
4445 else if (consumeIf("ls")) OperatorName = "<<";
4446 else if (consumeIf("lS")) OperatorName = "<<=";
4447 else if (consumeIf("lt")) OperatorName = "<";
4448 else if (consumeIf("mi")) OperatorName = "-";
4449 else if (consumeIf("mI")) OperatorName = "-=";
4450 else if (consumeIf("ml")) OperatorName = "*";
4451 else if (consumeIf("mL")) OperatorName = "*=";
4452 else if (consumeIf("ne")) OperatorName = "!=";
4453 else if (consumeIf("oo")) OperatorName = "||";
4454 else if (consumeIf("or")) OperatorName = "|";
4455 else if (consumeIf("oR")) OperatorName = "|=";
4456 else if (consumeIf("pl")) OperatorName = "+";
4457 else if (consumeIf("pL")) OperatorName = "+=";
4458 else if (consumeIf("rm")) OperatorName = "%";
4459 else if (consumeIf("rM")) OperatorName = "%=";
4460 else if (consumeIf("rs")) OperatorName = ">>";
4461 else if (consumeIf("rS")) OperatorName = ">>=";
4462 else return nullptr;
4463
Pavel Labathba825192018-10-16 14:29:14 +00004464 Node *Pack = getDerived().parseExpr(), *Init = nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00004465 if (Pack == nullptr)
4466 return nullptr;
4467 if (HasInitializer) {
Pavel Labathba825192018-10-16 14:29:14 +00004468 Init = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004469 if (Init == nullptr)
4470 return nullptr;
4471 }
4472
4473 if (IsLeftFold && Init)
4474 std::swap(Pack, Init);
4475
4476 return make<FoldExpr>(IsLeftFold, OperatorName, Pack, Init);
4477}
4478
Richard Smith1865d2f2020-10-22 19:29:36 -07004479// <expression> ::= mc <parameter type> <expr> [<offset number>] E
4480//
4481// Not yet in the spec: https://github.com/itanium-cxx-abi/cxx-abi/issues/47
4482template <typename Derived, typename Alloc>
4483Node *AbstractManglingParser<Derived, Alloc>::parsePointerToMemberConversionExpr() {
4484 Node *Ty = getDerived().parseType();
4485 if (!Ty)
4486 return nullptr;
4487 Node *Expr = getDerived().parseExpr();
4488 if (!Expr)
4489 return nullptr;
4490 StringView Offset = getDerived().parseNumber(true);
4491 if (!consumeIf('E'))
4492 return nullptr;
4493 return make<PointerToMemberConversionExpr>(Ty, Expr, Offset);
4494}
4495
4496// <expression> ::= so <referent type> <expr> [<offset number>] <union-selector>* [p] E
4497// <union-selector> ::= _ [<number>]
4498//
4499// Not yet in the spec: https://github.com/itanium-cxx-abi/cxx-abi/issues/47
4500template <typename Derived, typename Alloc>
4501Node *AbstractManglingParser<Derived, Alloc>::parseSubobjectExpr() {
4502 Node *Ty = getDerived().parseType();
4503 if (!Ty)
4504 return nullptr;
4505 Node *Expr = getDerived().parseExpr();
4506 if (!Expr)
4507 return nullptr;
4508 StringView Offset = getDerived().parseNumber(true);
4509 size_t SelectorsBegin = Names.size();
4510 while (consumeIf('_')) {
4511 Node *Selector = make<NameType>(parseNumber());
4512 if (!Selector)
4513 return nullptr;
4514 Names.push_back(Selector);
4515 }
4516 bool OnePastTheEnd = consumeIf('p');
4517 if (!consumeIf('E'))
4518 return nullptr;
4519 return make<SubobjectExpr>(
4520 Ty, Expr, Offset, popTrailingNodeArray(SelectorsBegin), OnePastTheEnd);
4521}
4522
Richard Smithc20d1442018-08-20 20:14:49 +00004523// <expression> ::= <unary operator-name> <expression>
4524// ::= <binary operator-name> <expression> <expression>
4525// ::= <ternary operator-name> <expression> <expression> <expression>
4526// ::= cl <expression>+ E # call
4527// ::= cv <type> <expression> # conversion with one argument
4528// ::= cv <type> _ <expression>* E # conversion with a different number of arguments
4529// ::= [gs] nw <expression>* _ <type> E # new (expr-list) type
4530// ::= [gs] nw <expression>* _ <type> <initializer> # new (expr-list) type (init)
4531// ::= [gs] na <expression>* _ <type> E # new[] (expr-list) type
4532// ::= [gs] na <expression>* _ <type> <initializer> # new[] (expr-list) type (init)
4533// ::= [gs] dl <expression> # delete expression
4534// ::= [gs] da <expression> # delete[] expression
4535// ::= pp_ <expression> # prefix ++
4536// ::= mm_ <expression> # prefix --
4537// ::= ti <type> # typeid (type)
4538// ::= te <expression> # typeid (expression)
4539// ::= dc <type> <expression> # dynamic_cast<type> (expression)
4540// ::= sc <type> <expression> # static_cast<type> (expression)
4541// ::= cc <type> <expression> # const_cast<type> (expression)
4542// ::= rc <type> <expression> # reinterpret_cast<type> (expression)
4543// ::= st <type> # sizeof (a type)
4544// ::= sz <expression> # sizeof (an expression)
4545// ::= at <type> # alignof (a type)
4546// ::= az <expression> # alignof (an expression)
4547// ::= nx <expression> # noexcept (expression)
4548// ::= <template-param>
4549// ::= <function-param>
4550// ::= dt <expression> <unresolved-name> # expr.name
4551// ::= pt <expression> <unresolved-name> # expr->name
4552// ::= ds <expression> <expression> # expr.*expr
4553// ::= sZ <template-param> # size of a parameter pack
4554// ::= sZ <function-param> # size of a function parameter pack
4555// ::= sP <template-arg>* E # sizeof...(T), size of a captured template parameter pack from an alias template
4556// ::= sp <expression> # pack expansion
4557// ::= tw <expression> # throw expression
4558// ::= tr # throw with no operand (rethrow)
4559// ::= <unresolved-name> # f(p), N::f(p), ::f(p),
4560// # freestanding dependent name (e.g., T::x),
4561// # objectless nonstatic member reference
4562// ::= fL <binary-operator-name> <expression> <expression>
4563// ::= fR <binary-operator-name> <expression> <expression>
4564// ::= fl <binary-operator-name> <expression>
4565// ::= fr <binary-operator-name> <expression>
4566// ::= <expr-primary>
Pavel Labathba825192018-10-16 14:29:14 +00004567template <typename Derived, typename Alloc>
4568Node *AbstractManglingParser<Derived, Alloc>::parseExpr() {
Richard Smithc20d1442018-08-20 20:14:49 +00004569 bool Global = consumeIf("gs");
4570 if (numLeft() < 2)
4571 return nullptr;
4572
4573 switch (*First) {
4574 case 'L':
Pavel Labathba825192018-10-16 14:29:14 +00004575 return getDerived().parseExprPrimary();
Richard Smithc20d1442018-08-20 20:14:49 +00004576 case 'T':
Pavel Labathba825192018-10-16 14:29:14 +00004577 return getDerived().parseTemplateParam();
Richard Smithc20d1442018-08-20 20:14:49 +00004578 case 'f': {
4579 // Disambiguate a fold expression from a <function-param>.
4580 if (look(1) == 'p' || (look(1) == 'L' && std::isdigit(look(2))))
Pavel Labathba825192018-10-16 14:29:14 +00004581 return getDerived().parseFunctionParam();
4582 return getDerived().parseFoldExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004583 }
4584 case 'a':
4585 switch (First[1]) {
4586 case 'a':
4587 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004588 return getDerived().parseBinaryExpr("&&");
Richard Smithc20d1442018-08-20 20:14:49 +00004589 case 'd':
4590 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004591 return getDerived().parsePrefixExpr("&");
Richard Smithc20d1442018-08-20 20:14:49 +00004592 case 'n':
4593 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004594 return getDerived().parseBinaryExpr("&");
Richard Smithc20d1442018-08-20 20:14:49 +00004595 case 'N':
4596 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004597 return getDerived().parseBinaryExpr("&=");
Richard Smithc20d1442018-08-20 20:14:49 +00004598 case 'S':
4599 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004600 return getDerived().parseBinaryExpr("=");
Richard Smithc20d1442018-08-20 20:14:49 +00004601 case 't': {
4602 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004603 Node *Ty = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00004604 if (Ty == nullptr)
4605 return nullptr;
4606 return make<EnclosingExpr>("alignof (", Ty, ")");
4607 }
4608 case 'z': {
4609 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004610 Node *Ty = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004611 if (Ty == nullptr)
4612 return nullptr;
4613 return make<EnclosingExpr>("alignof (", Ty, ")");
4614 }
4615 }
4616 return nullptr;
4617 case 'c':
4618 switch (First[1]) {
4619 // cc <type> <expression> # const_cast<type>(expression)
4620 case 'c': {
4621 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004622 Node *Ty = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00004623 if (Ty == nullptr)
4624 return Ty;
Pavel Labathba825192018-10-16 14:29:14 +00004625 Node *Ex = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004626 if (Ex == nullptr)
4627 return Ex;
4628 return make<CastExpr>("const_cast", Ty, Ex);
4629 }
4630 // cl <expression>+ E # call
4631 case 'l': {
4632 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004633 Node *Callee = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004634 if (Callee == nullptr)
4635 return Callee;
4636 size_t ExprsBegin = Names.size();
4637 while (!consumeIf('E')) {
Pavel Labathba825192018-10-16 14:29:14 +00004638 Node *E = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004639 if (E == nullptr)
4640 return E;
4641 Names.push_back(E);
4642 }
4643 return make<CallExpr>(Callee, popTrailingNodeArray(ExprsBegin));
4644 }
4645 case 'm':
4646 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004647 return getDerived().parseBinaryExpr(",");
Richard Smithc20d1442018-08-20 20:14:49 +00004648 case 'o':
4649 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004650 return getDerived().parsePrefixExpr("~");
Richard Smithc20d1442018-08-20 20:14:49 +00004651 case 'v':
Pavel Labathba825192018-10-16 14:29:14 +00004652 return getDerived().parseConversionExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004653 }
4654 return nullptr;
4655 case 'd':
4656 switch (First[1]) {
4657 case 'a': {
4658 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004659 Node *Ex = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004660 if (Ex == nullptr)
4661 return Ex;
4662 return make<DeleteExpr>(Ex, Global, /*is_array=*/true);
4663 }
4664 case 'c': {
4665 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004666 Node *T = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00004667 if (T == nullptr)
4668 return T;
Pavel Labathba825192018-10-16 14:29:14 +00004669 Node *Ex = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004670 if (Ex == nullptr)
4671 return Ex;
4672 return make<CastExpr>("dynamic_cast", T, Ex);
4673 }
4674 case 'e':
4675 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004676 return getDerived().parsePrefixExpr("*");
Richard Smithc20d1442018-08-20 20:14:49 +00004677 case 'l': {
4678 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004679 Node *E = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004680 if (E == nullptr)
4681 return E;
4682 return make<DeleteExpr>(E, Global, /*is_array=*/false);
4683 }
4684 case 'n':
Pavel Labathba825192018-10-16 14:29:14 +00004685 return getDerived().parseUnresolvedName();
Richard Smithc20d1442018-08-20 20:14:49 +00004686 case 's': {
4687 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004688 Node *LHS = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004689 if (LHS == nullptr)
4690 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00004691 Node *RHS = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004692 if (RHS == nullptr)
4693 return nullptr;
4694 return make<MemberExpr>(LHS, ".*", RHS);
4695 }
4696 case 't': {
4697 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004698 Node *LHS = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004699 if (LHS == nullptr)
4700 return LHS;
Pavel Labathba825192018-10-16 14:29:14 +00004701 Node *RHS = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004702 if (RHS == nullptr)
4703 return nullptr;
4704 return make<MemberExpr>(LHS, ".", RHS);
4705 }
4706 case 'v':
4707 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004708 return getDerived().parseBinaryExpr("/");
Richard Smithc20d1442018-08-20 20:14:49 +00004709 case 'V':
4710 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004711 return getDerived().parseBinaryExpr("/=");
Richard Smithc20d1442018-08-20 20:14:49 +00004712 }
4713 return nullptr;
4714 case 'e':
4715 switch (First[1]) {
4716 case 'o':
4717 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004718 return getDerived().parseBinaryExpr("^");
Richard Smithc20d1442018-08-20 20:14:49 +00004719 case 'O':
4720 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004721 return getDerived().parseBinaryExpr("^=");
Richard Smithc20d1442018-08-20 20:14:49 +00004722 case 'q':
4723 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004724 return getDerived().parseBinaryExpr("==");
Richard Smithc20d1442018-08-20 20:14:49 +00004725 }
4726 return nullptr;
4727 case 'g':
4728 switch (First[1]) {
4729 case 'e':
4730 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004731 return getDerived().parseBinaryExpr(">=");
Richard Smithc20d1442018-08-20 20:14:49 +00004732 case 't':
4733 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004734 return getDerived().parseBinaryExpr(">");
Richard Smithc20d1442018-08-20 20:14:49 +00004735 }
4736 return nullptr;
4737 case 'i':
4738 switch (First[1]) {
4739 case 'x': {
4740 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004741 Node *Base = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004742 if (Base == nullptr)
4743 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00004744 Node *Index = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004745 if (Index == nullptr)
4746 return Index;
4747 return make<ArraySubscriptExpr>(Base, Index);
4748 }
4749 case 'l': {
4750 First += 2;
4751 size_t InitsBegin = Names.size();
4752 while (!consumeIf('E')) {
Pavel Labathba825192018-10-16 14:29:14 +00004753 Node *E = getDerived().parseBracedExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004754 if (E == nullptr)
4755 return nullptr;
4756 Names.push_back(E);
4757 }
4758 return make<InitListExpr>(nullptr, popTrailingNodeArray(InitsBegin));
4759 }
4760 }
4761 return nullptr;
4762 case 'l':
4763 switch (First[1]) {
4764 case 'e':
4765 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004766 return getDerived().parseBinaryExpr("<=");
Richard Smithc20d1442018-08-20 20:14:49 +00004767 case 's':
4768 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004769 return getDerived().parseBinaryExpr("<<");
Richard Smithc20d1442018-08-20 20:14:49 +00004770 case 'S':
4771 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004772 return getDerived().parseBinaryExpr("<<=");
Richard Smithc20d1442018-08-20 20:14:49 +00004773 case 't':
4774 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004775 return getDerived().parseBinaryExpr("<");
Richard Smithc20d1442018-08-20 20:14:49 +00004776 }
4777 return nullptr;
4778 case 'm':
4779 switch (First[1]) {
Richard Smith1865d2f2020-10-22 19:29:36 -07004780 case 'c':
4781 First += 2;
4782 return parsePointerToMemberConversionExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004783 case 'i':
4784 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004785 return getDerived().parseBinaryExpr("-");
Richard Smithc20d1442018-08-20 20:14:49 +00004786 case 'I':
4787 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004788 return getDerived().parseBinaryExpr("-=");
Richard Smithc20d1442018-08-20 20:14:49 +00004789 case 'l':
4790 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004791 return getDerived().parseBinaryExpr("*");
Richard Smithc20d1442018-08-20 20:14:49 +00004792 case 'L':
4793 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004794 return getDerived().parseBinaryExpr("*=");
Richard Smithc20d1442018-08-20 20:14:49 +00004795 case 'm':
4796 First += 2;
4797 if (consumeIf('_'))
Pavel Labathba825192018-10-16 14:29:14 +00004798 return getDerived().parsePrefixExpr("--");
4799 Node *Ex = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004800 if (Ex == nullptr)
4801 return nullptr;
4802 return make<PostfixExpr>(Ex, "--");
4803 }
4804 return nullptr;
4805 case 'n':
4806 switch (First[1]) {
4807 case 'a':
4808 case 'w':
Pavel Labathba825192018-10-16 14:29:14 +00004809 return getDerived().parseNewExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004810 case 'e':
4811 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004812 return getDerived().parseBinaryExpr("!=");
Richard Smithc20d1442018-08-20 20:14:49 +00004813 case 'g':
4814 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004815 return getDerived().parsePrefixExpr("-");
Richard Smithc20d1442018-08-20 20:14:49 +00004816 case 't':
4817 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004818 return getDerived().parsePrefixExpr("!");
Richard Smithc20d1442018-08-20 20:14:49 +00004819 case 'x':
4820 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004821 Node *Ex = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004822 if (Ex == nullptr)
4823 return Ex;
4824 return make<EnclosingExpr>("noexcept (", Ex, ")");
4825 }
4826 return nullptr;
4827 case 'o':
4828 switch (First[1]) {
4829 case 'n':
Pavel Labathba825192018-10-16 14:29:14 +00004830 return getDerived().parseUnresolvedName();
Richard Smithc20d1442018-08-20 20:14:49 +00004831 case 'o':
4832 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004833 return getDerived().parseBinaryExpr("||");
Richard Smithc20d1442018-08-20 20:14:49 +00004834 case 'r':
4835 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004836 return getDerived().parseBinaryExpr("|");
Richard Smithc20d1442018-08-20 20:14:49 +00004837 case 'R':
4838 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004839 return getDerived().parseBinaryExpr("|=");
Richard Smithc20d1442018-08-20 20:14:49 +00004840 }
4841 return nullptr;
4842 case 'p':
4843 switch (First[1]) {
4844 case 'm':
4845 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004846 return getDerived().parseBinaryExpr("->*");
Richard Smithc20d1442018-08-20 20:14:49 +00004847 case 'l':
4848 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004849 return getDerived().parseBinaryExpr("+");
Richard Smithc20d1442018-08-20 20:14:49 +00004850 case 'L':
4851 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004852 return getDerived().parseBinaryExpr("+=");
Richard Smithc20d1442018-08-20 20:14:49 +00004853 case 'p': {
4854 First += 2;
4855 if (consumeIf('_'))
Pavel Labathba825192018-10-16 14:29:14 +00004856 return getDerived().parsePrefixExpr("++");
4857 Node *Ex = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004858 if (Ex == nullptr)
4859 return Ex;
4860 return make<PostfixExpr>(Ex, "++");
4861 }
4862 case 's':
4863 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004864 return getDerived().parsePrefixExpr("+");
Richard Smithc20d1442018-08-20 20:14:49 +00004865 case 't': {
4866 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004867 Node *L = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004868 if (L == nullptr)
4869 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00004870 Node *R = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004871 if (R == nullptr)
4872 return nullptr;
4873 return make<MemberExpr>(L, "->", R);
4874 }
4875 }
4876 return nullptr;
4877 case 'q':
4878 if (First[1] == 'u') {
4879 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004880 Node *Cond = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004881 if (Cond == nullptr)
4882 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00004883 Node *LHS = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004884 if (LHS == nullptr)
4885 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00004886 Node *RHS = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004887 if (RHS == nullptr)
4888 return nullptr;
4889 return make<ConditionalExpr>(Cond, LHS, RHS);
4890 }
4891 return nullptr;
4892 case 'r':
4893 switch (First[1]) {
4894 case 'c': {
4895 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004896 Node *T = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00004897 if (T == nullptr)
4898 return T;
Pavel Labathba825192018-10-16 14:29:14 +00004899 Node *Ex = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004900 if (Ex == nullptr)
4901 return Ex;
4902 return make<CastExpr>("reinterpret_cast", T, Ex);
4903 }
4904 case 'm':
4905 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004906 return getDerived().parseBinaryExpr("%");
Richard Smithc20d1442018-08-20 20:14:49 +00004907 case 'M':
4908 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004909 return getDerived().parseBinaryExpr("%=");
Richard Smithc20d1442018-08-20 20:14:49 +00004910 case 's':
4911 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004912 return getDerived().parseBinaryExpr(">>");
Richard Smithc20d1442018-08-20 20:14:49 +00004913 case 'S':
4914 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004915 return getDerived().parseBinaryExpr(">>=");
Richard Smithc20d1442018-08-20 20:14:49 +00004916 }
4917 return nullptr;
4918 case 's':
4919 switch (First[1]) {
4920 case 'c': {
4921 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004922 Node *T = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00004923 if (T == nullptr)
4924 return T;
Pavel Labathba825192018-10-16 14:29:14 +00004925 Node *Ex = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004926 if (Ex == nullptr)
4927 return Ex;
4928 return make<CastExpr>("static_cast", T, Ex);
4929 }
Richard Smith1865d2f2020-10-22 19:29:36 -07004930 case 'o':
4931 First += 2;
4932 return parseSubobjectExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004933 case 'p': {
4934 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004935 Node *Child = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004936 if (Child == nullptr)
4937 return nullptr;
4938 return make<ParameterPackExpansion>(Child);
4939 }
4940 case 'r':
Pavel Labathba825192018-10-16 14:29:14 +00004941 return getDerived().parseUnresolvedName();
Richard Smithc20d1442018-08-20 20:14:49 +00004942 case 't': {
4943 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004944 Node *Ty = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00004945 if (Ty == nullptr)
4946 return Ty;
4947 return make<EnclosingExpr>("sizeof (", Ty, ")");
4948 }
4949 case 'z': {
4950 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004951 Node *Ex = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004952 if (Ex == nullptr)
4953 return Ex;
4954 return make<EnclosingExpr>("sizeof (", Ex, ")");
4955 }
4956 case 'Z':
4957 First += 2;
4958 if (look() == 'T') {
Pavel Labathba825192018-10-16 14:29:14 +00004959 Node *R = getDerived().parseTemplateParam();
Richard Smithc20d1442018-08-20 20:14:49 +00004960 if (R == nullptr)
4961 return nullptr;
4962 return make<SizeofParamPackExpr>(R);
4963 } else if (look() == 'f') {
Pavel Labathba825192018-10-16 14:29:14 +00004964 Node *FP = getDerived().parseFunctionParam();
Richard Smithc20d1442018-08-20 20:14:49 +00004965 if (FP == nullptr)
4966 return nullptr;
4967 return make<EnclosingExpr>("sizeof... (", FP, ")");
4968 }
4969 return nullptr;
4970 case 'P': {
4971 First += 2;
4972 size_t ArgsBegin = Names.size();
4973 while (!consumeIf('E')) {
Pavel Labathba825192018-10-16 14:29:14 +00004974 Node *Arg = getDerived().parseTemplateArg();
Richard Smithc20d1442018-08-20 20:14:49 +00004975 if (Arg == nullptr)
4976 return nullptr;
4977 Names.push_back(Arg);
4978 }
Richard Smithb485b352018-08-24 23:30:26 +00004979 auto *Pack = make<NodeArrayNode>(popTrailingNodeArray(ArgsBegin));
4980 if (!Pack)
4981 return nullptr;
4982 return make<EnclosingExpr>("sizeof... (", Pack, ")");
Richard Smithc20d1442018-08-20 20:14:49 +00004983 }
4984 }
4985 return nullptr;
4986 case 't':
4987 switch (First[1]) {
4988 case 'e': {
4989 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004990 Node *Ex = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004991 if (Ex == nullptr)
4992 return Ex;
4993 return make<EnclosingExpr>("typeid (", Ex, ")");
4994 }
4995 case 'i': {
4996 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004997 Node *Ty = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00004998 if (Ty == nullptr)
4999 return Ty;
5000 return make<EnclosingExpr>("typeid (", Ty, ")");
5001 }
5002 case 'l': {
5003 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00005004 Node *Ty = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00005005 if (Ty == nullptr)
5006 return nullptr;
5007 size_t InitsBegin = Names.size();
5008 while (!consumeIf('E')) {
Pavel Labathba825192018-10-16 14:29:14 +00005009 Node *E = getDerived().parseBracedExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00005010 if (E == nullptr)
5011 return nullptr;
5012 Names.push_back(E);
5013 }
5014 return make<InitListExpr>(Ty, popTrailingNodeArray(InitsBegin));
5015 }
5016 case 'r':
5017 First += 2;
5018 return make<NameType>("throw");
5019 case 'w': {
5020 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00005021 Node *Ex = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00005022 if (Ex == nullptr)
5023 return nullptr;
5024 return make<ThrowExpr>(Ex);
5025 }
5026 }
5027 return nullptr;
James Y Knight4a60efc2020-12-07 10:26:49 -05005028 case 'u': {
5029 ++First;
5030 Node *Name = getDerived().parseSourceName(/*NameState=*/nullptr);
5031 if (!Name)
5032 return nullptr;
5033 // Special case legacy __uuidof mangling. The 't' and 'z' appear where the
5034 // standard encoding expects a <template-arg>, and would be otherwise be
5035 // interpreted as <type> node 'short' or 'ellipsis'. However, neither
5036 // __uuidof(short) nor __uuidof(...) can actually appear, so there is no
5037 // actual conflict here.
5038 if (Name->getBaseName() == "__uuidof") {
5039 if (numLeft() < 2)
5040 return nullptr;
5041 if (*First == 't') {
5042 ++First;
5043 Node *Ty = getDerived().parseType();
5044 if (!Ty)
5045 return nullptr;
5046 return make<CallExpr>(Name, makeNodeArray(&Ty, &Ty + 1));
5047 }
5048 if (*First == 'z') {
5049 ++First;
5050 Node *Ex = getDerived().parseExpr();
5051 if (!Ex)
5052 return nullptr;
5053 return make<CallExpr>(Name, makeNodeArray(&Ex, &Ex + 1));
5054 }
5055 }
5056 size_t ExprsBegin = Names.size();
5057 while (!consumeIf('E')) {
5058 Node *E = getDerived().parseTemplateArg();
5059 if (E == nullptr)
5060 return E;
5061 Names.push_back(E);
5062 }
5063 return make<CallExpr>(Name, popTrailingNodeArray(ExprsBegin));
5064 }
Richard Smithc20d1442018-08-20 20:14:49 +00005065 case '1':
5066 case '2':
5067 case '3':
5068 case '4':
5069 case '5':
5070 case '6':
5071 case '7':
5072 case '8':
5073 case '9':
Pavel Labathba825192018-10-16 14:29:14 +00005074 return getDerived().parseUnresolvedName();
Richard Smithc20d1442018-08-20 20:14:49 +00005075 }
5076 return nullptr;
5077}
5078
5079// <call-offset> ::= h <nv-offset> _
5080// ::= v <v-offset> _
5081//
5082// <nv-offset> ::= <offset number>
5083// # non-virtual base override
5084//
5085// <v-offset> ::= <offset number> _ <virtual offset number>
5086// # virtual base override, with vcall offset
Pavel Labathba825192018-10-16 14:29:14 +00005087template <typename Alloc, typename Derived>
5088bool AbstractManglingParser<Alloc, Derived>::parseCallOffset() {
Richard Smithc20d1442018-08-20 20:14:49 +00005089 // Just scan through the call offset, we never add this information into the
5090 // output.
5091 if (consumeIf('h'))
5092 return parseNumber(true).empty() || !consumeIf('_');
5093 if (consumeIf('v'))
5094 return parseNumber(true).empty() || !consumeIf('_') ||
5095 parseNumber(true).empty() || !consumeIf('_');
5096 return true;
5097}
5098
5099// <special-name> ::= TV <type> # virtual table
5100// ::= TT <type> # VTT structure (construction vtable index)
5101// ::= TI <type> # typeinfo structure
5102// ::= TS <type> # typeinfo name (null-terminated byte string)
5103// ::= Tc <call-offset> <call-offset> <base encoding>
5104// # base is the nominal target function of thunk
5105// # first call-offset is 'this' adjustment
5106// # second call-offset is result adjustment
5107// ::= T <call-offset> <base encoding>
5108// # base is the nominal target function of thunk
5109// ::= GV <object name> # Guard variable for one-time initialization
5110// # No <type>
5111// ::= TW <object name> # Thread-local wrapper
5112// ::= TH <object name> # Thread-local initialization
5113// ::= GR <object name> _ # First temporary
5114// ::= GR <object name> <seq-id> _ # Subsequent temporaries
5115// extension ::= TC <first type> <number> _ <second type> # construction vtable for second-in-first
5116// extension ::= GR <object name> # reference temporary for object
Pavel Labathba825192018-10-16 14:29:14 +00005117template <typename Derived, typename Alloc>
5118Node *AbstractManglingParser<Derived, Alloc>::parseSpecialName() {
Richard Smithc20d1442018-08-20 20:14:49 +00005119 switch (look()) {
5120 case 'T':
5121 switch (look(1)) {
Richard Smith1865d2f2020-10-22 19:29:36 -07005122 // TA <template-arg> # template parameter object
5123 //
5124 // Not yet in the spec: https://github.com/itanium-cxx-abi/cxx-abi/issues/63
5125 case 'A': {
5126 First += 2;
5127 Node *Arg = getDerived().parseTemplateArg();
5128 if (Arg == nullptr)
5129 return nullptr;
5130 return make<SpecialName>("template parameter object for ", Arg);
5131 }
Richard Smithc20d1442018-08-20 20:14:49 +00005132 // TV <type> # virtual table
5133 case 'V': {
5134 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00005135 Node *Ty = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00005136 if (Ty == nullptr)
5137 return nullptr;
5138 return make<SpecialName>("vtable for ", Ty);
5139 }
5140 // TT <type> # VTT structure (construction vtable index)
5141 case 'T': {
5142 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00005143 Node *Ty = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00005144 if (Ty == nullptr)
5145 return nullptr;
5146 return make<SpecialName>("VTT for ", Ty);
5147 }
5148 // TI <type> # typeinfo structure
5149 case 'I': {
5150 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00005151 Node *Ty = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00005152 if (Ty == nullptr)
5153 return nullptr;
5154 return make<SpecialName>("typeinfo for ", Ty);
5155 }
5156 // TS <type> # typeinfo name (null-terminated byte string)
5157 case 'S': {
5158 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00005159 Node *Ty = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00005160 if (Ty == nullptr)
5161 return nullptr;
5162 return make<SpecialName>("typeinfo name for ", Ty);
5163 }
5164 // Tc <call-offset> <call-offset> <base encoding>
5165 case 'c': {
5166 First += 2;
5167 if (parseCallOffset() || parseCallOffset())
5168 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00005169 Node *Encoding = getDerived().parseEncoding();
Richard Smithc20d1442018-08-20 20:14:49 +00005170 if (Encoding == nullptr)
5171 return nullptr;
5172 return make<SpecialName>("covariant return thunk to ", Encoding);
5173 }
5174 // extension ::= TC <first type> <number> _ <second type>
5175 // # construction vtable for second-in-first
5176 case 'C': {
5177 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00005178 Node *FirstType = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00005179 if (FirstType == nullptr)
5180 return nullptr;
5181 if (parseNumber(true).empty() || !consumeIf('_'))
5182 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00005183 Node *SecondType = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00005184 if (SecondType == nullptr)
5185 return nullptr;
5186 return make<CtorVtableSpecialName>(SecondType, FirstType);
5187 }
5188 // TW <object name> # Thread-local wrapper
5189 case 'W': {
5190 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00005191 Node *Name = getDerived().parseName();
Richard Smithc20d1442018-08-20 20:14:49 +00005192 if (Name == nullptr)
5193 return nullptr;
5194 return make<SpecialName>("thread-local wrapper routine for ", Name);
5195 }
5196 // TH <object name> # Thread-local initialization
5197 case 'H': {
5198 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00005199 Node *Name = getDerived().parseName();
Richard Smithc20d1442018-08-20 20:14:49 +00005200 if (Name == nullptr)
5201 return nullptr;
5202 return make<SpecialName>("thread-local initialization routine for ", Name);
5203 }
5204 // T <call-offset> <base encoding>
5205 default: {
5206 ++First;
5207 bool IsVirt = look() == 'v';
5208 if (parseCallOffset())
5209 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00005210 Node *BaseEncoding = getDerived().parseEncoding();
Richard Smithc20d1442018-08-20 20:14:49 +00005211 if (BaseEncoding == nullptr)
5212 return nullptr;
5213 if (IsVirt)
5214 return make<SpecialName>("virtual thunk to ", BaseEncoding);
5215 else
5216 return make<SpecialName>("non-virtual thunk to ", BaseEncoding);
5217 }
5218 }
5219 case 'G':
5220 switch (look(1)) {
5221 // GV <object name> # Guard variable for one-time initialization
5222 case 'V': {
5223 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00005224 Node *Name = getDerived().parseName();
Richard Smithc20d1442018-08-20 20:14:49 +00005225 if (Name == nullptr)
5226 return nullptr;
5227 return make<SpecialName>("guard variable for ", Name);
5228 }
5229 // GR <object name> # reference temporary for object
5230 // GR <object name> _ # First temporary
5231 // GR <object name> <seq-id> _ # Subsequent temporaries
5232 case 'R': {
5233 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00005234 Node *Name = getDerived().parseName();
Richard Smithc20d1442018-08-20 20:14:49 +00005235 if (Name == nullptr)
5236 return nullptr;
5237 size_t Count;
5238 bool ParsedSeqId = !parseSeqId(&Count);
5239 if (!consumeIf('_') && ParsedSeqId)
5240 return nullptr;
5241 return make<SpecialName>("reference temporary for ", Name);
5242 }
5243 }
5244 }
5245 return nullptr;
5246}
5247
5248// <encoding> ::= <function name> <bare-function-type>
5249// ::= <data name>
5250// ::= <special-name>
Pavel Labathba825192018-10-16 14:29:14 +00005251template <typename Derived, typename Alloc>
5252Node *AbstractManglingParser<Derived, Alloc>::parseEncoding() {
Richard Smithfac39712020-07-09 21:08:39 -07005253 // The template parameters of an encoding are unrelated to those of the
5254 // enclosing context.
5255 class SaveTemplateParams {
5256 AbstractManglingParser *Parser;
5257 decltype(TemplateParams) OldParams;
Justin Lebar2c536232021-06-09 16:57:22 -07005258 decltype(OuterTemplateParams) OldOuterParams;
Richard Smithfac39712020-07-09 21:08:39 -07005259
5260 public:
Louis Dionnec1fe8672020-10-30 17:33:02 -04005261 SaveTemplateParams(AbstractManglingParser *TheParser) : Parser(TheParser) {
Richard Smithfac39712020-07-09 21:08:39 -07005262 OldParams = std::move(Parser->TemplateParams);
Justin Lebar2c536232021-06-09 16:57:22 -07005263 OldOuterParams = std::move(Parser->OuterTemplateParams);
Richard Smithfac39712020-07-09 21:08:39 -07005264 Parser->TemplateParams.clear();
Justin Lebar2c536232021-06-09 16:57:22 -07005265 Parser->OuterTemplateParams.clear();
Richard Smithfac39712020-07-09 21:08:39 -07005266 }
5267 ~SaveTemplateParams() {
5268 Parser->TemplateParams = std::move(OldParams);
Justin Lebar2c536232021-06-09 16:57:22 -07005269 Parser->OuterTemplateParams = std::move(OldOuterParams);
Richard Smithfac39712020-07-09 21:08:39 -07005270 }
5271 } SaveTemplateParams(this);
Richard Smithfd434322020-07-09 20:36:04 -07005272
Richard Smithc20d1442018-08-20 20:14:49 +00005273 if (look() == 'G' || look() == 'T')
Pavel Labathba825192018-10-16 14:29:14 +00005274 return getDerived().parseSpecialName();
Richard Smithc20d1442018-08-20 20:14:49 +00005275
5276 auto IsEndOfEncoding = [&] {
5277 // The set of chars that can potentially follow an <encoding> (none of which
5278 // can start a <type>). Enumerating these allows us to avoid speculative
5279 // parsing.
5280 return numLeft() == 0 || look() == 'E' || look() == '.' || look() == '_';
5281 };
5282
5283 NameState NameInfo(this);
Pavel Labathba825192018-10-16 14:29:14 +00005284 Node *Name = getDerived().parseName(&NameInfo);
Richard Smithc20d1442018-08-20 20:14:49 +00005285 if (Name == nullptr)
5286 return nullptr;
5287
5288 if (resolveForwardTemplateRefs(NameInfo))
5289 return nullptr;
5290
5291 if (IsEndOfEncoding())
5292 return Name;
5293
5294 Node *Attrs = nullptr;
5295 if (consumeIf("Ua9enable_ifI")) {
5296 size_t BeforeArgs = Names.size();
5297 while (!consumeIf('E')) {
Pavel Labathba825192018-10-16 14:29:14 +00005298 Node *Arg = getDerived().parseTemplateArg();
Richard Smithc20d1442018-08-20 20:14:49 +00005299 if (Arg == nullptr)
5300 return nullptr;
5301 Names.push_back(Arg);
5302 }
5303 Attrs = make<EnableIfAttr>(popTrailingNodeArray(BeforeArgs));
Richard Smithb485b352018-08-24 23:30:26 +00005304 if (!Attrs)
5305 return nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00005306 }
5307
5308 Node *ReturnType = nullptr;
5309 if (!NameInfo.CtorDtorConversion && NameInfo.EndsWithTemplateArgs) {
Pavel Labathba825192018-10-16 14:29:14 +00005310 ReturnType = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00005311 if (ReturnType == nullptr)
5312 return nullptr;
5313 }
5314
5315 if (consumeIf('v'))
5316 return make<FunctionEncoding>(ReturnType, Name, NodeArray(),
5317 Attrs, NameInfo.CVQualifiers,
5318 NameInfo.ReferenceQualifier);
5319
5320 size_t ParamsBegin = Names.size();
5321 do {
Pavel Labathba825192018-10-16 14:29:14 +00005322 Node *Ty = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00005323 if (Ty == nullptr)
5324 return nullptr;
5325 Names.push_back(Ty);
5326 } while (!IsEndOfEncoding());
5327
5328 return make<FunctionEncoding>(ReturnType, Name,
5329 popTrailingNodeArray(ParamsBegin),
5330 Attrs, NameInfo.CVQualifiers,
5331 NameInfo.ReferenceQualifier);
5332}
5333
5334template <class Float>
5335struct FloatData;
5336
5337template <>
5338struct FloatData<float>
5339{
5340 static const size_t mangled_size = 8;
5341 static const size_t max_demangled_size = 24;
5342 static constexpr const char* spec = "%af";
5343};
5344
5345template <>
5346struct FloatData<double>
5347{
5348 static const size_t mangled_size = 16;
5349 static const size_t max_demangled_size = 32;
5350 static constexpr const char* spec = "%a";
5351};
5352
5353template <>
5354struct FloatData<long double>
5355{
5356#if defined(__mips__) && defined(__mips_n64) || defined(__aarch64__) || \
5357 defined(__wasm__)
5358 static const size_t mangled_size = 32;
5359#elif defined(__arm__) || defined(__mips__) || defined(__hexagon__)
5360 static const size_t mangled_size = 16;
5361#else
5362 static const size_t mangled_size = 20; // May need to be adjusted to 16 or 24 on other platforms
5363#endif
Elliott Hughes5a360ea2020-04-10 17:42:00 -07005364 // `-0x1.ffffffffffffffffffffffffffffp+16383` + 'L' + '\0' == 42 bytes.
5365 // 28 'f's * 4 bits == 112 bits, which is the number of mantissa bits.
5366 // Negatives are one character longer than positives.
5367 // `0x1.` and `p` are constant, and exponents `+16383` and `-16382` are the
5368 // same length. 1 sign bit, 112 mantissa bits, and 15 exponent bits == 128.
5369 static const size_t max_demangled_size = 42;
Richard Smithc20d1442018-08-20 20:14:49 +00005370 static constexpr const char *spec = "%LaL";
5371};
5372
Pavel Labathba825192018-10-16 14:29:14 +00005373template <typename Alloc, typename Derived>
5374template <class Float>
5375Node *AbstractManglingParser<Alloc, Derived>::parseFloatingLiteral() {
Richard Smithc20d1442018-08-20 20:14:49 +00005376 const size_t N = FloatData<Float>::mangled_size;
5377 if (numLeft() <= N)
5378 return nullptr;
5379 StringView Data(First, First + N);
5380 for (char C : Data)
5381 if (!std::isxdigit(C))
5382 return nullptr;
5383 First += N;
5384 if (!consumeIf('E'))
5385 return nullptr;
5386 return make<FloatLiteralImpl<Float>>(Data);
5387}
5388
5389// <seq-id> ::= <0-9A-Z>+
Pavel Labathba825192018-10-16 14:29:14 +00005390template <typename Alloc, typename Derived>
5391bool AbstractManglingParser<Alloc, Derived>::parseSeqId(size_t *Out) {
Richard Smithc20d1442018-08-20 20:14:49 +00005392 if (!(look() >= '0' && look() <= '9') &&
5393 !(look() >= 'A' && look() <= 'Z'))
5394 return true;
5395
5396 size_t Id = 0;
5397 while (true) {
5398 if (look() >= '0' && look() <= '9') {
5399 Id *= 36;
5400 Id += static_cast<size_t>(look() - '0');
5401 } else if (look() >= 'A' && look() <= 'Z') {
5402 Id *= 36;
5403 Id += static_cast<size_t>(look() - 'A') + 10;
5404 } else {
5405 *Out = Id;
5406 return false;
5407 }
5408 ++First;
5409 }
5410}
5411
5412// <substitution> ::= S <seq-id> _
5413// ::= S_
5414// <substitution> ::= Sa # ::std::allocator
5415// <substitution> ::= Sb # ::std::basic_string
5416// <substitution> ::= Ss # ::std::basic_string < char,
5417// ::std::char_traits<char>,
5418// ::std::allocator<char> >
5419// <substitution> ::= Si # ::std::basic_istream<char, std::char_traits<char> >
5420// <substitution> ::= So # ::std::basic_ostream<char, std::char_traits<char> >
5421// <substitution> ::= Sd # ::std::basic_iostream<char, std::char_traits<char> >
Pavel Labathba825192018-10-16 14:29:14 +00005422template <typename Derived, typename Alloc>
5423Node *AbstractManglingParser<Derived, Alloc>::parseSubstitution() {
Richard Smithc20d1442018-08-20 20:14:49 +00005424 if (!consumeIf('S'))
5425 return nullptr;
5426
5427 if (std::islower(look())) {
5428 Node *SpecialSub;
5429 switch (look()) {
5430 case 'a':
5431 ++First;
5432 SpecialSub = make<SpecialSubstitution>(SpecialSubKind::allocator);
5433 break;
5434 case 'b':
5435 ++First;
5436 SpecialSub = make<SpecialSubstitution>(SpecialSubKind::basic_string);
5437 break;
5438 case 's':
5439 ++First;
5440 SpecialSub = make<SpecialSubstitution>(SpecialSubKind::string);
5441 break;
5442 case 'i':
5443 ++First;
5444 SpecialSub = make<SpecialSubstitution>(SpecialSubKind::istream);
5445 break;
5446 case 'o':
5447 ++First;
5448 SpecialSub = make<SpecialSubstitution>(SpecialSubKind::ostream);
5449 break;
5450 case 'd':
5451 ++First;
5452 SpecialSub = make<SpecialSubstitution>(SpecialSubKind::iostream);
5453 break;
5454 default:
5455 return nullptr;
5456 }
Richard Smithb485b352018-08-24 23:30:26 +00005457 if (!SpecialSub)
5458 return nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00005459 // Itanium C++ ABI 5.1.2: If a name that would use a built-in <substitution>
5460 // has ABI tags, the tags are appended to the substitution; the result is a
5461 // substitutable component.
Pavel Labathba825192018-10-16 14:29:14 +00005462 Node *WithTags = getDerived().parseAbiTags(SpecialSub);
Richard Smithc20d1442018-08-20 20:14:49 +00005463 if (WithTags != SpecialSub) {
5464 Subs.push_back(WithTags);
5465 SpecialSub = WithTags;
5466 }
5467 return SpecialSub;
5468 }
5469
5470 // ::= S_
5471 if (consumeIf('_')) {
5472 if (Subs.empty())
5473 return nullptr;
5474 return Subs[0];
5475 }
5476
5477 // ::= S <seq-id> _
5478 size_t Index = 0;
5479 if (parseSeqId(&Index))
5480 return nullptr;
5481 ++Index;
5482 if (!consumeIf('_') || Index >= Subs.size())
5483 return nullptr;
5484 return Subs[Index];
5485}
5486
5487// <template-param> ::= T_ # first template parameter
5488// ::= T <parameter-2 non-negative number> _
Richard Smithdf1c14c2019-09-06 23:53:21 +00005489// ::= TL <level-1> __
5490// ::= TL <level-1> _ <parameter-2 non-negative number> _
Pavel Labathba825192018-10-16 14:29:14 +00005491template <typename Derived, typename Alloc>
5492Node *AbstractManglingParser<Derived, Alloc>::parseTemplateParam() {
Richard Smithc20d1442018-08-20 20:14:49 +00005493 if (!consumeIf('T'))
5494 return nullptr;
5495
Richard Smithdf1c14c2019-09-06 23:53:21 +00005496 size_t Level = 0;
5497 if (consumeIf('L')) {
5498 if (parsePositiveInteger(&Level))
5499 return nullptr;
5500 ++Level;
5501 if (!consumeIf('_'))
5502 return nullptr;
5503 }
5504
Richard Smithc20d1442018-08-20 20:14:49 +00005505 size_t Index = 0;
5506 if (!consumeIf('_')) {
5507 if (parsePositiveInteger(&Index))
5508 return nullptr;
5509 ++Index;
5510 if (!consumeIf('_'))
5511 return nullptr;
5512 }
5513
Richard Smithc20d1442018-08-20 20:14:49 +00005514 // If we're in a context where this <template-param> refers to a
5515 // <template-arg> further ahead in the mangled name (currently just conversion
5516 // operator types), then we should only look it up in the right context.
Richard Smithdf1c14c2019-09-06 23:53:21 +00005517 // This can only happen at the outermost level.
5518 if (PermitForwardTemplateReferences && Level == 0) {
Richard Smithb485b352018-08-24 23:30:26 +00005519 Node *ForwardRef = make<ForwardTemplateReference>(Index);
5520 if (!ForwardRef)
5521 return nullptr;
5522 assert(ForwardRef->getKind() == Node::KForwardTemplateReference);
5523 ForwardTemplateRefs.push_back(
5524 static_cast<ForwardTemplateReference *>(ForwardRef));
5525 return ForwardRef;
Richard Smithc20d1442018-08-20 20:14:49 +00005526 }
5527
Richard Smithdf1c14c2019-09-06 23:53:21 +00005528 if (Level >= TemplateParams.size() || !TemplateParams[Level] ||
5529 Index >= TemplateParams[Level]->size()) {
5530 // Itanium ABI 5.1.8: In a generic lambda, uses of auto in the parameter
5531 // list are mangled as the corresponding artificial template type parameter.
5532 if (ParsingLambdaParamsAtLevel == Level && Level <= TemplateParams.size()) {
5533 // This will be popped by the ScopedTemplateParamList in
5534 // parseUnnamedTypeName.
5535 if (Level == TemplateParams.size())
5536 TemplateParams.push_back(nullptr);
5537 return make<NameType>("auto");
5538 }
5539
Richard Smithc20d1442018-08-20 20:14:49 +00005540 return nullptr;
Richard Smithdf1c14c2019-09-06 23:53:21 +00005541 }
5542
5543 return (*TemplateParams[Level])[Index];
5544}
5545
5546// <template-param-decl> ::= Ty # type parameter
5547// ::= Tn <type> # non-type parameter
5548// ::= Tt <template-param-decl>* E # template parameter
5549// ::= Tp <template-param-decl> # parameter pack
5550template <typename Derived, typename Alloc>
5551Node *AbstractManglingParser<Derived, Alloc>::parseTemplateParamDecl() {
5552 auto InventTemplateParamName = [&](TemplateParamKind Kind) {
5553 unsigned Index = NumSyntheticTemplateParameters[(int)Kind]++;
5554 Node *N = make<SyntheticTemplateParamName>(Kind, Index);
5555 if (N) TemplateParams.back()->push_back(N);
5556 return N;
5557 };
5558
5559 if (consumeIf("Ty")) {
5560 Node *Name = InventTemplateParamName(TemplateParamKind::Type);
5561 if (!Name)
5562 return nullptr;
5563 return make<TypeTemplateParamDecl>(Name);
5564 }
5565
5566 if (consumeIf("Tn")) {
5567 Node *Name = InventTemplateParamName(TemplateParamKind::NonType);
5568 if (!Name)
5569 return nullptr;
5570 Node *Type = parseType();
5571 if (!Type)
5572 return nullptr;
5573 return make<NonTypeTemplateParamDecl>(Name, Type);
5574 }
5575
5576 if (consumeIf("Tt")) {
5577 Node *Name = InventTemplateParamName(TemplateParamKind::Template);
5578 if (!Name)
5579 return nullptr;
5580 size_t ParamsBegin = Names.size();
5581 ScopedTemplateParamList TemplateTemplateParamParams(this);
5582 while (!consumeIf("E")) {
5583 Node *P = parseTemplateParamDecl();
5584 if (!P)
5585 return nullptr;
5586 Names.push_back(P);
5587 }
5588 NodeArray Params = popTrailingNodeArray(ParamsBegin);
5589 return make<TemplateTemplateParamDecl>(Name, Params);
5590 }
5591
5592 if (consumeIf("Tp")) {
5593 Node *P = parseTemplateParamDecl();
5594 if (!P)
5595 return nullptr;
5596 return make<TemplateParamPackDecl>(P);
5597 }
5598
5599 return nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00005600}
5601
5602// <template-arg> ::= <type> # type or template
5603// ::= X <expression> E # expression
5604// ::= <expr-primary> # simple expressions
5605// ::= J <template-arg>* E # argument pack
5606// ::= LZ <encoding> E # extension
Pavel Labathba825192018-10-16 14:29:14 +00005607template <typename Derived, typename Alloc>
5608Node *AbstractManglingParser<Derived, Alloc>::parseTemplateArg() {
Richard Smithc20d1442018-08-20 20:14:49 +00005609 switch (look()) {
5610 case 'X': {
5611 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00005612 Node *Arg = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00005613 if (Arg == nullptr || !consumeIf('E'))
5614 return nullptr;
5615 return Arg;
5616 }
5617 case 'J': {
5618 ++First;
5619 size_t ArgsBegin = Names.size();
5620 while (!consumeIf('E')) {
Pavel Labathba825192018-10-16 14:29:14 +00005621 Node *Arg = getDerived().parseTemplateArg();
Richard Smithc20d1442018-08-20 20:14:49 +00005622 if (Arg == nullptr)
5623 return nullptr;
5624 Names.push_back(Arg);
5625 }
5626 NodeArray Args = popTrailingNodeArray(ArgsBegin);
5627 return make<TemplateArgumentPack>(Args);
5628 }
5629 case 'L': {
5630 // ::= LZ <encoding> E # extension
5631 if (look(1) == 'Z') {
5632 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00005633 Node *Arg = getDerived().parseEncoding();
Richard Smithc20d1442018-08-20 20:14:49 +00005634 if (Arg == nullptr || !consumeIf('E'))
5635 return nullptr;
5636 return Arg;
5637 }
5638 // ::= <expr-primary> # simple expressions
Pavel Labathba825192018-10-16 14:29:14 +00005639 return getDerived().parseExprPrimary();
Richard Smithc20d1442018-08-20 20:14:49 +00005640 }
5641 default:
Pavel Labathba825192018-10-16 14:29:14 +00005642 return getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00005643 }
5644}
5645
5646// <template-args> ::= I <template-arg>* E
5647// extension, the abi says <template-arg>+
Pavel Labathba825192018-10-16 14:29:14 +00005648template <typename Derived, typename Alloc>
5649Node *
5650AbstractManglingParser<Derived, Alloc>::parseTemplateArgs(bool TagTemplates) {
Richard Smithc20d1442018-08-20 20:14:49 +00005651 if (!consumeIf('I'))
5652 return nullptr;
5653
5654 // <template-params> refer to the innermost <template-args>. Clear out any
5655 // outer args that we may have inserted into TemplateParams.
Richard Smithdf1c14c2019-09-06 23:53:21 +00005656 if (TagTemplates) {
Richard Smithc20d1442018-08-20 20:14:49 +00005657 TemplateParams.clear();
Richard Smithdf1c14c2019-09-06 23:53:21 +00005658 TemplateParams.push_back(&OuterTemplateParams);
5659 OuterTemplateParams.clear();
5660 }
Richard Smithc20d1442018-08-20 20:14:49 +00005661
5662 size_t ArgsBegin = Names.size();
5663 while (!consumeIf('E')) {
5664 if (TagTemplates) {
5665 auto OldParams = std::move(TemplateParams);
Pavel Labathba825192018-10-16 14:29:14 +00005666 Node *Arg = getDerived().parseTemplateArg();
Richard Smithc20d1442018-08-20 20:14:49 +00005667 TemplateParams = std::move(OldParams);
5668 if (Arg == nullptr)
5669 return nullptr;
5670 Names.push_back(Arg);
5671 Node *TableEntry = Arg;
5672 if (Arg->getKind() == Node::KTemplateArgumentPack) {
5673 TableEntry = make<ParameterPack>(
5674 static_cast<TemplateArgumentPack*>(TableEntry)->getElements());
Richard Smithb485b352018-08-24 23:30:26 +00005675 if (!TableEntry)
5676 return nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00005677 }
Richard Smithdf1c14c2019-09-06 23:53:21 +00005678 TemplateParams.back()->push_back(TableEntry);
Richard Smithc20d1442018-08-20 20:14:49 +00005679 } else {
Pavel Labathba825192018-10-16 14:29:14 +00005680 Node *Arg = getDerived().parseTemplateArg();
Richard Smithc20d1442018-08-20 20:14:49 +00005681 if (Arg == nullptr)
5682 return nullptr;
5683 Names.push_back(Arg);
5684 }
5685 }
5686 return make<TemplateArgs>(popTrailingNodeArray(ArgsBegin));
5687}
5688
5689// <mangled-name> ::= _Z <encoding>
5690// ::= <type>
5691// extension ::= ___Z <encoding> _block_invoke
5692// extension ::= ___Z <encoding> _block_invoke<decimal-digit>+
5693// extension ::= ___Z <encoding> _block_invoke_<decimal-digit>+
Pavel Labathba825192018-10-16 14:29:14 +00005694template <typename Derived, typename Alloc>
5695Node *AbstractManglingParser<Derived, Alloc>::parse() {
Erik Pilkingtonc0df1582019-01-17 21:37:36 +00005696 if (consumeIf("_Z") || consumeIf("__Z")) {
Pavel Labathba825192018-10-16 14:29:14 +00005697 Node *Encoding = getDerived().parseEncoding();
Richard Smithc20d1442018-08-20 20:14:49 +00005698 if (Encoding == nullptr)
5699 return nullptr;
5700 if (look() == '.') {
5701 Encoding = make<DotSuffix>(Encoding, StringView(First, Last));
5702 First = Last;
5703 }
5704 if (numLeft() != 0)
5705 return nullptr;
5706 return Encoding;
5707 }
5708
Erik Pilkingtonc0df1582019-01-17 21:37:36 +00005709 if (consumeIf("___Z") || consumeIf("____Z")) {
Pavel Labathba825192018-10-16 14:29:14 +00005710 Node *Encoding = getDerived().parseEncoding();
Richard Smithc20d1442018-08-20 20:14:49 +00005711 if (Encoding == nullptr || !consumeIf("_block_invoke"))
5712 return nullptr;
5713 bool RequireNumber = consumeIf('_');
5714 if (parseNumber().empty() && RequireNumber)
5715 return nullptr;
5716 if (look() == '.')
5717 First = Last;
5718 if (numLeft() != 0)
5719 return nullptr;
5720 return make<SpecialName>("invocation function for block in ", Encoding);
5721 }
5722
Pavel Labathba825192018-10-16 14:29:14 +00005723 Node *Ty = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00005724 if (numLeft() != 0)
5725 return nullptr;
5726 return Ty;
5727}
5728
Pavel Labathba825192018-10-16 14:29:14 +00005729template <typename Alloc>
5730struct ManglingParser : AbstractManglingParser<ManglingParser<Alloc>, Alloc> {
5731 using AbstractManglingParser<ManglingParser<Alloc>,
5732 Alloc>::AbstractManglingParser;
5733};
5734
Erik Pilkingtonf70e4d82019-01-17 20:37:51 +00005735DEMANGLE_NAMESPACE_END
Richard Smithc20d1442018-08-20 20:14:49 +00005736
Erik Pilkingtonf70e4d82019-01-17 20:37:51 +00005737#endif // DEMANGLE_ITANIUMDEMANGLE_H