blob: 94c26ea2105238e3cfc3b52c8baa25df8128b4d0 [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.
654 std::pair<ReferenceKind, const Node *> collapse(OutputStream &S) const {
655 auto SoFar = std::make_pair(RK, Pointee);
656 for (;;) {
657 const Node *SN = SoFar.second->getSyntaxNode(S);
658 if (SN->getKind() != KReferenceType)
659 break;
660 auto *RT = static_cast<const ReferenceType *>(SN);
661 SoFar.second = RT->Pointee;
662 SoFar.first = std::min(SoFar.first, RT->RK);
663 }
664 return SoFar;
665 }
666
667public:
668 ReferenceType(const Node *Pointee_, ReferenceKind RK_)
669 : Node(KReferenceType, Pointee_->RHSComponentCache),
670 Pointee(Pointee_), RK(RK_) {}
671
672 template<typename Fn> void match(Fn F) const { F(Pointee, RK); }
673
674 bool hasRHSComponentSlow(OutputStream &S) const override {
675 return Pointee->hasRHSComponent(S);
676 }
677
678 void printLeft(OutputStream &s) const override {
679 if (Printing)
680 return;
681 SwapAndRestore<bool> SavePrinting(Printing, true);
682 std::pair<ReferenceKind, const Node *> Collapsed = collapse(s);
683 Collapsed.second->printLeft(s);
684 if (Collapsed.second->hasArray(s))
685 s += " ";
686 if (Collapsed.second->hasArray(s) || Collapsed.second->hasFunction(s))
687 s += "(";
688
689 s += (Collapsed.first == ReferenceKind::LValue ? "&" : "&&");
690 }
691 void printRight(OutputStream &s) const override {
692 if (Printing)
693 return;
694 SwapAndRestore<bool> SavePrinting(Printing, true);
695 std::pair<ReferenceKind, const Node *> Collapsed = collapse(s);
696 if (Collapsed.second->hasArray(s) || Collapsed.second->hasFunction(s))
697 s += ")";
698 Collapsed.second->printRight(s);
699 }
700};
701
702class PointerToMemberType final : public Node {
703 const Node *ClassType;
704 const Node *MemberType;
705
706public:
707 PointerToMemberType(const Node *ClassType_, const Node *MemberType_)
708 : Node(KPointerToMemberType, MemberType_->RHSComponentCache),
709 ClassType(ClassType_), MemberType(MemberType_) {}
710
711 template<typename Fn> void match(Fn F) const { F(ClassType, MemberType); }
712
713 bool hasRHSComponentSlow(OutputStream &S) const override {
714 return MemberType->hasRHSComponent(S);
715 }
716
717 void printLeft(OutputStream &s) const override {
718 MemberType->printLeft(s);
719 if (MemberType->hasArray(s) || MemberType->hasFunction(s))
720 s += "(";
721 else
722 s += " ";
723 ClassType->print(s);
724 s += "::*";
725 }
726
727 void printRight(OutputStream &s) const override {
728 if (MemberType->hasArray(s) || MemberType->hasFunction(s))
729 s += ")";
730 MemberType->printRight(s);
731 }
732};
733
Richard Smithc20d1442018-08-20 20:14:49 +0000734class ArrayType final : public Node {
735 const Node *Base;
Erik Pilkingtond7555e32019-11-04 10:47:44 -0800736 Node *Dimension;
Richard Smithc20d1442018-08-20 20:14:49 +0000737
738public:
Erik Pilkingtond7555e32019-11-04 10:47:44 -0800739 ArrayType(const Node *Base_, Node *Dimension_)
Richard Smithc20d1442018-08-20 20:14:49 +0000740 : Node(KArrayType,
741 /*RHSComponentCache=*/Cache::Yes,
742 /*ArrayCache=*/Cache::Yes),
743 Base(Base_), Dimension(Dimension_) {}
744
745 template<typename Fn> void match(Fn F) const { F(Base, Dimension); }
746
747 bool hasRHSComponentSlow(OutputStream &) const override { return true; }
748 bool hasArraySlow(OutputStream &) const override { return true; }
749
750 void printLeft(OutputStream &S) const override { Base->printLeft(S); }
751
752 void printRight(OutputStream &S) const override {
753 if (S.back() != ']')
754 S += " ";
755 S += "[";
Erik Pilkingtond7555e32019-11-04 10:47:44 -0800756 if (Dimension)
757 Dimension->print(S);
Richard Smithc20d1442018-08-20 20:14:49 +0000758 S += "]";
759 Base->printRight(S);
760 }
761};
762
763class FunctionType final : public Node {
764 const Node *Ret;
765 NodeArray Params;
766 Qualifiers CVQuals;
767 FunctionRefQual RefQual;
768 const Node *ExceptionSpec;
769
770public:
771 FunctionType(const Node *Ret_, NodeArray Params_, Qualifiers CVQuals_,
772 FunctionRefQual RefQual_, const Node *ExceptionSpec_)
773 : Node(KFunctionType,
774 /*RHSComponentCache=*/Cache::Yes, /*ArrayCache=*/Cache::No,
775 /*FunctionCache=*/Cache::Yes),
776 Ret(Ret_), Params(Params_), CVQuals(CVQuals_), RefQual(RefQual_),
777 ExceptionSpec(ExceptionSpec_) {}
778
779 template<typename Fn> void match(Fn F) const {
780 F(Ret, Params, CVQuals, RefQual, ExceptionSpec);
781 }
782
783 bool hasRHSComponentSlow(OutputStream &) const override { return true; }
784 bool hasFunctionSlow(OutputStream &) const override { return true; }
785
786 // Handle C++'s ... quirky decl grammar by using the left & right
787 // distinction. Consider:
788 // int (*f(float))(char) {}
789 // f is a function that takes a float and returns a pointer to a function
790 // that takes a char and returns an int. If we're trying to print f, start
791 // by printing out the return types's left, then print our parameters, then
792 // finally print right of the return type.
793 void printLeft(OutputStream &S) const override {
794 Ret->printLeft(S);
795 S += " ";
796 }
797
798 void printRight(OutputStream &S) const override {
799 S += "(";
800 Params.printWithComma(S);
801 S += ")";
802 Ret->printRight(S);
803
804 if (CVQuals & QualConst)
805 S += " const";
806 if (CVQuals & QualVolatile)
807 S += " volatile";
808 if (CVQuals & QualRestrict)
809 S += " restrict";
810
811 if (RefQual == FrefQualLValue)
812 S += " &";
813 else if (RefQual == FrefQualRValue)
814 S += " &&";
815
816 if (ExceptionSpec != nullptr) {
817 S += ' ';
818 ExceptionSpec->print(S);
819 }
820 }
821};
822
823class NoexceptSpec : public Node {
824 const Node *E;
825public:
826 NoexceptSpec(const Node *E_) : Node(KNoexceptSpec), E(E_) {}
827
828 template<typename Fn> void match(Fn F) const { F(E); }
829
830 void printLeft(OutputStream &S) const override {
831 S += "noexcept(";
832 E->print(S);
833 S += ")";
834 }
835};
836
837class DynamicExceptionSpec : public Node {
838 NodeArray Types;
839public:
840 DynamicExceptionSpec(NodeArray Types_)
841 : Node(KDynamicExceptionSpec), Types(Types_) {}
842
843 template<typename Fn> void match(Fn F) const { F(Types); }
844
845 void printLeft(OutputStream &S) const override {
846 S += "throw(";
847 Types.printWithComma(S);
848 S += ')';
849 }
850};
851
852class FunctionEncoding final : public Node {
853 const Node *Ret;
854 const Node *Name;
855 NodeArray Params;
856 const Node *Attrs;
857 Qualifiers CVQuals;
858 FunctionRefQual RefQual;
859
860public:
861 FunctionEncoding(const Node *Ret_, const Node *Name_, NodeArray Params_,
862 const Node *Attrs_, Qualifiers CVQuals_,
863 FunctionRefQual RefQual_)
864 : Node(KFunctionEncoding,
865 /*RHSComponentCache=*/Cache::Yes, /*ArrayCache=*/Cache::No,
866 /*FunctionCache=*/Cache::Yes),
867 Ret(Ret_), Name(Name_), Params(Params_), Attrs(Attrs_),
868 CVQuals(CVQuals_), RefQual(RefQual_) {}
869
870 template<typename Fn> void match(Fn F) const {
871 F(Ret, Name, Params, Attrs, CVQuals, RefQual);
872 }
873
874 Qualifiers getCVQuals() const { return CVQuals; }
875 FunctionRefQual getRefQual() const { return RefQual; }
876 NodeArray getParams() const { return Params; }
877 const Node *getReturnType() const { return Ret; }
878
879 bool hasRHSComponentSlow(OutputStream &) const override { return true; }
880 bool hasFunctionSlow(OutputStream &) const override { return true; }
881
882 const Node *getName() const { return Name; }
883
884 void printLeft(OutputStream &S) const override {
885 if (Ret) {
886 Ret->printLeft(S);
887 if (!Ret->hasRHSComponent(S))
888 S += " ";
889 }
890 Name->print(S);
891 }
892
893 void printRight(OutputStream &S) const override {
894 S += "(";
895 Params.printWithComma(S);
896 S += ")";
897 if (Ret)
898 Ret->printRight(S);
899
900 if (CVQuals & QualConst)
901 S += " const";
902 if (CVQuals & QualVolatile)
903 S += " volatile";
904 if (CVQuals & QualRestrict)
905 S += " restrict";
906
907 if (RefQual == FrefQualLValue)
908 S += " &";
909 else if (RefQual == FrefQualRValue)
910 S += " &&";
911
912 if (Attrs != nullptr)
913 Attrs->print(S);
914 }
915};
916
917class LiteralOperator : public Node {
918 const Node *OpName;
919
920public:
921 LiteralOperator(const Node *OpName_)
922 : Node(KLiteralOperator), OpName(OpName_) {}
923
924 template<typename Fn> void match(Fn F) const { F(OpName); }
925
926 void printLeft(OutputStream &S) const override {
927 S += "operator\"\" ";
928 OpName->print(S);
929 }
930};
931
932class SpecialName final : public Node {
933 const StringView Special;
934 const Node *Child;
935
936public:
937 SpecialName(StringView Special_, const Node *Child_)
938 : Node(KSpecialName), Special(Special_), Child(Child_) {}
939
940 template<typename Fn> void match(Fn F) const { F(Special, Child); }
941
942 void printLeft(OutputStream &S) const override {
943 S += Special;
944 Child->print(S);
945 }
946};
947
948class CtorVtableSpecialName final : public Node {
949 const Node *FirstType;
950 const Node *SecondType;
951
952public:
953 CtorVtableSpecialName(const Node *FirstType_, const Node *SecondType_)
954 : Node(KCtorVtableSpecialName),
955 FirstType(FirstType_), SecondType(SecondType_) {}
956
957 template<typename Fn> void match(Fn F) const { F(FirstType, SecondType); }
958
959 void printLeft(OutputStream &S) const override {
960 S += "construction vtable for ";
961 FirstType->print(S);
962 S += "-in-";
963 SecondType->print(S);
964 }
965};
966
967struct NestedName : Node {
968 Node *Qual;
969 Node *Name;
970
971 NestedName(Node *Qual_, Node *Name_)
972 : Node(KNestedName), Qual(Qual_), Name(Name_) {}
973
974 template<typename Fn> void match(Fn F) const { F(Qual, Name); }
975
976 StringView getBaseName() const override { return Name->getBaseName(); }
977
978 void printLeft(OutputStream &S) const override {
979 Qual->print(S);
980 S += "::";
981 Name->print(S);
982 }
983};
984
985struct LocalName : Node {
986 Node *Encoding;
987 Node *Entity;
988
989 LocalName(Node *Encoding_, Node *Entity_)
990 : Node(KLocalName), Encoding(Encoding_), Entity(Entity_) {}
991
992 template<typename Fn> void match(Fn F) const { F(Encoding, Entity); }
993
994 void printLeft(OutputStream &S) const override {
995 Encoding->print(S);
996 S += "::";
997 Entity->print(S);
998 }
999};
1000
1001class QualifiedName final : public Node {
1002 // qualifier::name
1003 const Node *Qualifier;
1004 const Node *Name;
1005
1006public:
1007 QualifiedName(const Node *Qualifier_, const Node *Name_)
1008 : Node(KQualifiedName), Qualifier(Qualifier_), Name(Name_) {}
1009
1010 template<typename Fn> void match(Fn F) const { F(Qualifier, Name); }
1011
1012 StringView getBaseName() const override { return Name->getBaseName(); }
1013
1014 void printLeft(OutputStream &S) const override {
1015 Qualifier->print(S);
1016 S += "::";
1017 Name->print(S);
1018 }
1019};
1020
1021class VectorType final : public Node {
1022 const Node *BaseType;
Erik Pilkingtond7555e32019-11-04 10:47:44 -08001023 const Node *Dimension;
Richard Smithc20d1442018-08-20 20:14:49 +00001024
1025public:
Erik Pilkingtond7555e32019-11-04 10:47:44 -08001026 VectorType(const Node *BaseType_, Node *Dimension_)
Richard Smithc20d1442018-08-20 20:14:49 +00001027 : Node(KVectorType), BaseType(BaseType_),
1028 Dimension(Dimension_) {}
1029
1030 template<typename Fn> void match(Fn F) const { F(BaseType, Dimension); }
1031
1032 void printLeft(OutputStream &S) const override {
1033 BaseType->print(S);
1034 S += " vector[";
Erik Pilkingtond7555e32019-11-04 10:47:44 -08001035 if (Dimension)
1036 Dimension->print(S);
Richard Smithc20d1442018-08-20 20:14:49 +00001037 S += "]";
1038 }
1039};
1040
1041class PixelVectorType final : public Node {
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 PixelVectorType(const Node *Dimension_)
Richard Smithc20d1442018-08-20 20:14:49 +00001046 : Node(KPixelVectorType), Dimension(Dimension_) {}
1047
1048 template<typename Fn> void match(Fn F) const { F(Dimension); }
1049
1050 void printLeft(OutputStream &S) const override {
1051 // FIXME: This should demangle as "vector pixel".
1052 S += "pixel vector[";
Erik Pilkingtond7555e32019-11-04 10:47:44 -08001053 Dimension->print(S);
Richard Smithc20d1442018-08-20 20:14:49 +00001054 S += "]";
1055 }
1056};
1057
Richard Smithdf1c14c2019-09-06 23:53:21 +00001058enum class TemplateParamKind { Type, NonType, Template };
1059
1060/// An invented name for a template parameter for which we don't have a
1061/// corresponding template argument.
1062///
1063/// This node is created when parsing the <lambda-sig> for a lambda with
1064/// explicit template arguments, which might be referenced in the parameter
1065/// types appearing later in the <lambda-sig>.
1066class SyntheticTemplateParamName final : public Node {
1067 TemplateParamKind Kind;
1068 unsigned Index;
1069
1070public:
1071 SyntheticTemplateParamName(TemplateParamKind Kind_, unsigned Index_)
1072 : Node(KSyntheticTemplateParamName), Kind(Kind_), Index(Index_) {}
1073
1074 template<typename Fn> void match(Fn F) const { F(Kind, Index); }
1075
1076 void printLeft(OutputStream &S) const override {
1077 switch (Kind) {
1078 case TemplateParamKind::Type:
1079 S += "$T";
1080 break;
1081 case TemplateParamKind::NonType:
1082 S += "$N";
1083 break;
1084 case TemplateParamKind::Template:
1085 S += "$TT";
1086 break;
1087 }
1088 if (Index > 0)
1089 S << Index - 1;
1090 }
1091};
1092
1093/// A template type parameter declaration, 'typename T'.
1094class TypeTemplateParamDecl final : public Node {
1095 Node *Name;
1096
1097public:
1098 TypeTemplateParamDecl(Node *Name_)
1099 : Node(KTypeTemplateParamDecl, Cache::Yes), Name(Name_) {}
1100
1101 template<typename Fn> void match(Fn F) const { F(Name); }
1102
1103 void printLeft(OutputStream &S) const override {
1104 S += "typename ";
1105 }
1106
1107 void printRight(OutputStream &S) const override {
1108 Name->print(S);
1109 }
1110};
1111
1112/// A non-type template parameter declaration, 'int N'.
1113class NonTypeTemplateParamDecl final : public Node {
1114 Node *Name;
1115 Node *Type;
1116
1117public:
1118 NonTypeTemplateParamDecl(Node *Name_, Node *Type_)
1119 : Node(KNonTypeTemplateParamDecl, Cache::Yes), Name(Name_), Type(Type_) {}
1120
1121 template<typename Fn> void match(Fn F) const { F(Name, Type); }
1122
1123 void printLeft(OutputStream &S) const override {
1124 Type->printLeft(S);
1125 if (!Type->hasRHSComponent(S))
1126 S += " ";
1127 }
1128
1129 void printRight(OutputStream &S) const override {
1130 Name->print(S);
1131 Type->printRight(S);
1132 }
1133};
1134
1135/// A template template parameter declaration,
1136/// 'template<typename T> typename N'.
1137class TemplateTemplateParamDecl final : public Node {
1138 Node *Name;
1139 NodeArray Params;
1140
1141public:
1142 TemplateTemplateParamDecl(Node *Name_, NodeArray Params_)
1143 : Node(KTemplateTemplateParamDecl, Cache::Yes), Name(Name_),
1144 Params(Params_) {}
1145
1146 template<typename Fn> void match(Fn F) const { F(Name, Params); }
1147
1148 void printLeft(OutputStream &S) const override {
1149 S += "template<";
1150 Params.printWithComma(S);
1151 S += "> typename ";
1152 }
1153
1154 void printRight(OutputStream &S) const override {
1155 Name->print(S);
1156 }
1157};
1158
1159/// A template parameter pack declaration, 'typename ...T'.
1160class TemplateParamPackDecl final : public Node {
1161 Node *Param;
1162
1163public:
1164 TemplateParamPackDecl(Node *Param_)
1165 : Node(KTemplateParamPackDecl, Cache::Yes), Param(Param_) {}
1166
1167 template<typename Fn> void match(Fn F) const { F(Param); }
1168
1169 void printLeft(OutputStream &S) const override {
1170 Param->printLeft(S);
1171 S += "...";
1172 }
1173
1174 void printRight(OutputStream &S) const override {
1175 Param->printRight(S);
1176 }
1177};
1178
Richard Smithc20d1442018-08-20 20:14:49 +00001179/// An unexpanded parameter pack (either in the expression or type context). If
1180/// this AST is correct, this node will have a ParameterPackExpansion node above
1181/// it.
1182///
1183/// This node is created when some <template-args> are found that apply to an
1184/// <encoding>, and is stored in the TemplateParams table. In order for this to
1185/// appear in the final AST, it has to referenced via a <template-param> (ie,
1186/// T_).
1187class ParameterPack final : public Node {
1188 NodeArray Data;
1189
1190 // Setup OutputStream for a pack expansion unless we're already expanding one.
1191 void initializePackExpansion(OutputStream &S) const {
1192 if (S.CurrentPackMax == std::numeric_limits<unsigned>::max()) {
1193 S.CurrentPackMax = static_cast<unsigned>(Data.size());
1194 S.CurrentPackIndex = 0;
1195 }
1196 }
1197
1198public:
1199 ParameterPack(NodeArray Data_) : Node(KParameterPack), Data(Data_) {
1200 ArrayCache = FunctionCache = RHSComponentCache = Cache::Unknown;
1201 if (std::all_of(Data.begin(), Data.end(), [](Node* P) {
1202 return P->ArrayCache == Cache::No;
1203 }))
1204 ArrayCache = Cache::No;
1205 if (std::all_of(Data.begin(), Data.end(), [](Node* P) {
1206 return P->FunctionCache == Cache::No;
1207 }))
1208 FunctionCache = Cache::No;
1209 if (std::all_of(Data.begin(), Data.end(), [](Node* P) {
1210 return P->RHSComponentCache == Cache::No;
1211 }))
1212 RHSComponentCache = Cache::No;
1213 }
1214
1215 template<typename Fn> void match(Fn F) const { F(Data); }
1216
1217 bool hasRHSComponentSlow(OutputStream &S) const override {
1218 initializePackExpansion(S);
1219 size_t Idx = S.CurrentPackIndex;
1220 return Idx < Data.size() && Data[Idx]->hasRHSComponent(S);
1221 }
1222 bool hasArraySlow(OutputStream &S) const override {
1223 initializePackExpansion(S);
1224 size_t Idx = S.CurrentPackIndex;
1225 return Idx < Data.size() && Data[Idx]->hasArray(S);
1226 }
1227 bool hasFunctionSlow(OutputStream &S) const override {
1228 initializePackExpansion(S);
1229 size_t Idx = S.CurrentPackIndex;
1230 return Idx < Data.size() && Data[Idx]->hasFunction(S);
1231 }
1232 const Node *getSyntaxNode(OutputStream &S) const override {
1233 initializePackExpansion(S);
1234 size_t Idx = S.CurrentPackIndex;
1235 return Idx < Data.size() ? Data[Idx]->getSyntaxNode(S) : this;
1236 }
1237
1238 void printLeft(OutputStream &S) const override {
1239 initializePackExpansion(S);
1240 size_t Idx = S.CurrentPackIndex;
1241 if (Idx < Data.size())
1242 Data[Idx]->printLeft(S);
1243 }
1244 void printRight(OutputStream &S) const override {
1245 initializePackExpansion(S);
1246 size_t Idx = S.CurrentPackIndex;
1247 if (Idx < Data.size())
1248 Data[Idx]->printRight(S);
1249 }
1250};
1251
1252/// A variadic template argument. This node represents an occurrence of
1253/// J<something>E in some <template-args>. It isn't itself unexpanded, unless
1254/// one of it's Elements is. The parser inserts a ParameterPack into the
1255/// TemplateParams table if the <template-args> this pack belongs to apply to an
1256/// <encoding>.
1257class TemplateArgumentPack final : public Node {
1258 NodeArray Elements;
1259public:
1260 TemplateArgumentPack(NodeArray Elements_)
1261 : Node(KTemplateArgumentPack), Elements(Elements_) {}
1262
1263 template<typename Fn> void match(Fn F) const { F(Elements); }
1264
1265 NodeArray getElements() const { return Elements; }
1266
1267 void printLeft(OutputStream &S) const override {
1268 Elements.printWithComma(S);
1269 }
1270};
1271
1272/// A pack expansion. Below this node, there are some unexpanded ParameterPacks
1273/// which each have Child->ParameterPackSize elements.
1274class ParameterPackExpansion final : public Node {
1275 const Node *Child;
1276
1277public:
1278 ParameterPackExpansion(const Node *Child_)
1279 : Node(KParameterPackExpansion), Child(Child_) {}
1280
1281 template<typename Fn> void match(Fn F) const { F(Child); }
1282
1283 const Node *getChild() const { return Child; }
1284
1285 void printLeft(OutputStream &S) const override {
1286 constexpr unsigned Max = std::numeric_limits<unsigned>::max();
1287 SwapAndRestore<unsigned> SavePackIdx(S.CurrentPackIndex, Max);
1288 SwapAndRestore<unsigned> SavePackMax(S.CurrentPackMax, Max);
1289 size_t StreamPos = S.getCurrentPosition();
1290
1291 // Print the first element in the pack. If Child contains a ParameterPack,
1292 // it will set up S.CurrentPackMax and print the first element.
1293 Child->print(S);
1294
1295 // No ParameterPack was found in Child. This can occur if we've found a pack
1296 // expansion on a <function-param>.
1297 if (S.CurrentPackMax == Max) {
1298 S += "...";
1299 return;
1300 }
1301
1302 // We found a ParameterPack, but it has no elements. Erase whatever we may
1303 // of printed.
1304 if (S.CurrentPackMax == 0) {
1305 S.setCurrentPosition(StreamPos);
1306 return;
1307 }
1308
1309 // Else, iterate through the rest of the elements in the pack.
1310 for (unsigned I = 1, E = S.CurrentPackMax; I < E; ++I) {
1311 S += ", ";
1312 S.CurrentPackIndex = I;
1313 Child->print(S);
1314 }
1315 }
1316};
1317
1318class TemplateArgs final : public Node {
1319 NodeArray Params;
1320
1321public:
1322 TemplateArgs(NodeArray Params_) : Node(KTemplateArgs), Params(Params_) {}
1323
1324 template<typename Fn> void match(Fn F) const { F(Params); }
1325
1326 NodeArray getParams() { return Params; }
1327
1328 void printLeft(OutputStream &S) const override {
1329 S += "<";
1330 Params.printWithComma(S);
1331 if (S.back() == '>')
1332 S += " ";
1333 S += ">";
1334 }
1335};
1336
Richard Smithb485b352018-08-24 23:30:26 +00001337/// A forward-reference to a template argument that was not known at the point
1338/// where the template parameter name was parsed in a mangling.
1339///
1340/// This is created when demangling the name of a specialization of a
1341/// conversion function template:
1342///
1343/// \code
1344/// struct A {
1345/// template<typename T> operator T*();
1346/// };
1347/// \endcode
1348///
1349/// When demangling a specialization of the conversion function template, we
1350/// encounter the name of the template (including the \c T) before we reach
1351/// the template argument list, so we cannot substitute the parameter name
1352/// for the corresponding argument while parsing. Instead, we create a
1353/// \c ForwardTemplateReference node that is resolved after we parse the
1354/// template arguments.
Richard Smithc20d1442018-08-20 20:14:49 +00001355struct ForwardTemplateReference : Node {
1356 size_t Index;
1357 Node *Ref = nullptr;
1358
1359 // If we're currently printing this node. It is possible (though invalid) for
1360 // a forward template reference to refer to itself via a substitution. This
1361 // creates a cyclic AST, which will stack overflow printing. To fix this, bail
1362 // out if more than one print* function is active.
1363 mutable bool Printing = false;
1364
1365 ForwardTemplateReference(size_t Index_)
1366 : Node(KForwardTemplateReference, Cache::Unknown, Cache::Unknown,
1367 Cache::Unknown),
1368 Index(Index_) {}
1369
1370 // We don't provide a matcher for these, because the value of the node is
1371 // not determined by its construction parameters, and it generally needs
1372 // special handling.
1373 template<typename Fn> void match(Fn F) const = delete;
1374
1375 bool hasRHSComponentSlow(OutputStream &S) const override {
1376 if (Printing)
1377 return false;
1378 SwapAndRestore<bool> SavePrinting(Printing, true);
1379 return Ref->hasRHSComponent(S);
1380 }
1381 bool hasArraySlow(OutputStream &S) const override {
1382 if (Printing)
1383 return false;
1384 SwapAndRestore<bool> SavePrinting(Printing, true);
1385 return Ref->hasArray(S);
1386 }
1387 bool hasFunctionSlow(OutputStream &S) const override {
1388 if (Printing)
1389 return false;
1390 SwapAndRestore<bool> SavePrinting(Printing, true);
1391 return Ref->hasFunction(S);
1392 }
1393 const Node *getSyntaxNode(OutputStream &S) const override {
1394 if (Printing)
1395 return this;
1396 SwapAndRestore<bool> SavePrinting(Printing, true);
1397 return Ref->getSyntaxNode(S);
1398 }
1399
1400 void printLeft(OutputStream &S) const override {
1401 if (Printing)
1402 return;
1403 SwapAndRestore<bool> SavePrinting(Printing, true);
1404 Ref->printLeft(S);
1405 }
1406 void printRight(OutputStream &S) const override {
1407 if (Printing)
1408 return;
1409 SwapAndRestore<bool> SavePrinting(Printing, true);
1410 Ref->printRight(S);
1411 }
1412};
1413
1414struct NameWithTemplateArgs : Node {
1415 // name<template_args>
1416 Node *Name;
1417 Node *TemplateArgs;
1418
1419 NameWithTemplateArgs(Node *Name_, Node *TemplateArgs_)
1420 : Node(KNameWithTemplateArgs), Name(Name_), TemplateArgs(TemplateArgs_) {}
1421
1422 template<typename Fn> void match(Fn F) const { F(Name, TemplateArgs); }
1423
1424 StringView getBaseName() const override { return Name->getBaseName(); }
1425
1426 void printLeft(OutputStream &S) const override {
1427 Name->print(S);
1428 TemplateArgs->print(S);
1429 }
1430};
1431
1432class GlobalQualifiedName final : public Node {
1433 Node *Child;
1434
1435public:
1436 GlobalQualifiedName(Node* Child_)
1437 : Node(KGlobalQualifiedName), Child(Child_) {}
1438
1439 template<typename Fn> void match(Fn F) const { F(Child); }
1440
1441 StringView getBaseName() const override { return Child->getBaseName(); }
1442
1443 void printLeft(OutputStream &S) const override {
1444 S += "::";
1445 Child->print(S);
1446 }
1447};
1448
1449struct StdQualifiedName : Node {
1450 Node *Child;
1451
1452 StdQualifiedName(Node *Child_) : Node(KStdQualifiedName), Child(Child_) {}
1453
1454 template<typename Fn> void match(Fn F) const { F(Child); }
1455
1456 StringView getBaseName() const override { return Child->getBaseName(); }
1457
1458 void printLeft(OutputStream &S) const override {
1459 S += "std::";
1460 Child->print(S);
1461 }
1462};
1463
1464enum class SpecialSubKind {
1465 allocator,
1466 basic_string,
1467 string,
1468 istream,
1469 ostream,
1470 iostream,
1471};
1472
1473class ExpandedSpecialSubstitution final : public Node {
1474 SpecialSubKind SSK;
1475
1476public:
1477 ExpandedSpecialSubstitution(SpecialSubKind SSK_)
1478 : Node(KExpandedSpecialSubstitution), SSK(SSK_) {}
1479
1480 template<typename Fn> void match(Fn F) const { F(SSK); }
1481
1482 StringView getBaseName() const override {
1483 switch (SSK) {
1484 case SpecialSubKind::allocator:
1485 return StringView("allocator");
1486 case SpecialSubKind::basic_string:
1487 return StringView("basic_string");
1488 case SpecialSubKind::string:
1489 return StringView("basic_string");
1490 case SpecialSubKind::istream:
1491 return StringView("basic_istream");
1492 case SpecialSubKind::ostream:
1493 return StringView("basic_ostream");
1494 case SpecialSubKind::iostream:
1495 return StringView("basic_iostream");
1496 }
Erik Pilkingtonf70e4d82019-01-17 20:37:51 +00001497 DEMANGLE_UNREACHABLE;
Richard Smithc20d1442018-08-20 20:14:49 +00001498 }
1499
1500 void printLeft(OutputStream &S) const override {
1501 switch (SSK) {
1502 case SpecialSubKind::allocator:
Richard Smithb485b352018-08-24 23:30:26 +00001503 S += "std::allocator";
Richard Smithc20d1442018-08-20 20:14:49 +00001504 break;
1505 case SpecialSubKind::basic_string:
Richard Smithb485b352018-08-24 23:30:26 +00001506 S += "std::basic_string";
1507 break;
Richard Smithc20d1442018-08-20 20:14:49 +00001508 case SpecialSubKind::string:
1509 S += "std::basic_string<char, std::char_traits<char>, "
1510 "std::allocator<char> >";
1511 break;
1512 case SpecialSubKind::istream:
1513 S += "std::basic_istream<char, std::char_traits<char> >";
1514 break;
1515 case SpecialSubKind::ostream:
1516 S += "std::basic_ostream<char, std::char_traits<char> >";
1517 break;
1518 case SpecialSubKind::iostream:
1519 S += "std::basic_iostream<char, std::char_traits<char> >";
1520 break;
1521 }
1522 }
1523};
1524
1525class SpecialSubstitution final : public Node {
1526public:
1527 SpecialSubKind SSK;
1528
1529 SpecialSubstitution(SpecialSubKind SSK_)
1530 : Node(KSpecialSubstitution), SSK(SSK_) {}
1531
1532 template<typename Fn> void match(Fn F) const { F(SSK); }
1533
1534 StringView getBaseName() const override {
1535 switch (SSK) {
1536 case SpecialSubKind::allocator:
1537 return StringView("allocator");
1538 case SpecialSubKind::basic_string:
1539 return StringView("basic_string");
1540 case SpecialSubKind::string:
1541 return StringView("string");
1542 case SpecialSubKind::istream:
1543 return StringView("istream");
1544 case SpecialSubKind::ostream:
1545 return StringView("ostream");
1546 case SpecialSubKind::iostream:
1547 return StringView("iostream");
1548 }
Erik Pilkingtonf70e4d82019-01-17 20:37:51 +00001549 DEMANGLE_UNREACHABLE;
Richard Smithc20d1442018-08-20 20:14:49 +00001550 }
1551
1552 void printLeft(OutputStream &S) const override {
1553 switch (SSK) {
1554 case SpecialSubKind::allocator:
1555 S += "std::allocator";
1556 break;
1557 case SpecialSubKind::basic_string:
1558 S += "std::basic_string";
1559 break;
1560 case SpecialSubKind::string:
1561 S += "std::string";
1562 break;
1563 case SpecialSubKind::istream:
1564 S += "std::istream";
1565 break;
1566 case SpecialSubKind::ostream:
1567 S += "std::ostream";
1568 break;
1569 case SpecialSubKind::iostream:
1570 S += "std::iostream";
1571 break;
1572 }
1573 }
1574};
1575
1576class CtorDtorName final : public Node {
1577 const Node *Basename;
1578 const bool IsDtor;
Pavel Labathf4e67eb2018-10-10 08:39:16 +00001579 const int Variant;
Richard Smithc20d1442018-08-20 20:14:49 +00001580
1581public:
Pavel Labathf4e67eb2018-10-10 08:39:16 +00001582 CtorDtorName(const Node *Basename_, bool IsDtor_, int Variant_)
1583 : Node(KCtorDtorName), Basename(Basename_), IsDtor(IsDtor_),
1584 Variant(Variant_) {}
Richard Smithc20d1442018-08-20 20:14:49 +00001585
Pavel Labathf4e67eb2018-10-10 08:39:16 +00001586 template<typename Fn> void match(Fn F) const { F(Basename, IsDtor, Variant); }
Richard Smithc20d1442018-08-20 20:14:49 +00001587
1588 void printLeft(OutputStream &S) const override {
1589 if (IsDtor)
1590 S += "~";
1591 S += Basename->getBaseName();
1592 }
1593};
1594
1595class DtorName : public Node {
1596 const Node *Base;
1597
1598public:
1599 DtorName(const Node *Base_) : Node(KDtorName), Base(Base_) {}
1600
1601 template<typename Fn> void match(Fn F) const { F(Base); }
1602
1603 void printLeft(OutputStream &S) const override {
1604 S += "~";
1605 Base->printLeft(S);
1606 }
1607};
1608
1609class UnnamedTypeName : public Node {
1610 const StringView Count;
1611
1612public:
1613 UnnamedTypeName(StringView Count_) : Node(KUnnamedTypeName), Count(Count_) {}
1614
1615 template<typename Fn> void match(Fn F) const { F(Count); }
1616
1617 void printLeft(OutputStream &S) const override {
1618 S += "'unnamed";
1619 S += Count;
1620 S += "\'";
1621 }
1622};
1623
1624class ClosureTypeName : public Node {
Richard Smithdf1c14c2019-09-06 23:53:21 +00001625 NodeArray TemplateParams;
Richard Smithc20d1442018-08-20 20:14:49 +00001626 NodeArray Params;
1627 StringView Count;
1628
1629public:
Richard Smithdf1c14c2019-09-06 23:53:21 +00001630 ClosureTypeName(NodeArray TemplateParams_, NodeArray Params_,
1631 StringView Count_)
1632 : Node(KClosureTypeName), TemplateParams(TemplateParams_),
1633 Params(Params_), Count(Count_) {}
Richard Smithc20d1442018-08-20 20:14:49 +00001634
Richard Smithdf1c14c2019-09-06 23:53:21 +00001635 template<typename Fn> void match(Fn F) const {
1636 F(TemplateParams, Params, Count);
1637 }
1638
1639 void printDeclarator(OutputStream &S) const {
1640 if (!TemplateParams.empty()) {
1641 S += "<";
1642 TemplateParams.printWithComma(S);
1643 S += ">";
1644 }
1645 S += "(";
1646 Params.printWithComma(S);
1647 S += ")";
1648 }
Richard Smithc20d1442018-08-20 20:14:49 +00001649
1650 void printLeft(OutputStream &S) const override {
1651 S += "\'lambda";
1652 S += Count;
Richard Smithdf1c14c2019-09-06 23:53:21 +00001653 S += "\'";
1654 printDeclarator(S);
Richard Smithc20d1442018-08-20 20:14:49 +00001655 }
1656};
1657
1658class StructuredBindingName : public Node {
1659 NodeArray Bindings;
1660public:
1661 StructuredBindingName(NodeArray Bindings_)
1662 : Node(KStructuredBindingName), Bindings(Bindings_) {}
1663
1664 template<typename Fn> void match(Fn F) const { F(Bindings); }
1665
1666 void printLeft(OutputStream &S) const override {
1667 S += '[';
1668 Bindings.printWithComma(S);
1669 S += ']';
1670 }
1671};
1672
1673// -- Expression Nodes --
1674
1675class BinaryExpr : public Node {
1676 const Node *LHS;
1677 const StringView InfixOperator;
1678 const Node *RHS;
1679
1680public:
1681 BinaryExpr(const Node *LHS_, StringView InfixOperator_, const Node *RHS_)
1682 : Node(KBinaryExpr), LHS(LHS_), InfixOperator(InfixOperator_), RHS(RHS_) {
1683 }
1684
1685 template<typename Fn> void match(Fn F) const { F(LHS, InfixOperator, RHS); }
1686
1687 void printLeft(OutputStream &S) const override {
1688 // might be a template argument expression, then we need to disambiguate
1689 // with parens.
1690 if (InfixOperator == ">")
1691 S += "(";
1692
1693 S += "(";
1694 LHS->print(S);
1695 S += ") ";
1696 S += InfixOperator;
1697 S += " (";
1698 RHS->print(S);
1699 S += ")";
1700
1701 if (InfixOperator == ">")
1702 S += ")";
1703 }
1704};
1705
1706class ArraySubscriptExpr : public Node {
1707 const Node *Op1;
1708 const Node *Op2;
1709
1710public:
1711 ArraySubscriptExpr(const Node *Op1_, const Node *Op2_)
1712 : Node(KArraySubscriptExpr), Op1(Op1_), Op2(Op2_) {}
1713
1714 template<typename Fn> void match(Fn F) const { F(Op1, Op2); }
1715
1716 void printLeft(OutputStream &S) const override {
1717 S += "(";
1718 Op1->print(S);
1719 S += ")[";
1720 Op2->print(S);
1721 S += "]";
1722 }
1723};
1724
1725class PostfixExpr : public Node {
1726 const Node *Child;
1727 const StringView Operator;
1728
1729public:
1730 PostfixExpr(const Node *Child_, StringView Operator_)
1731 : Node(KPostfixExpr), Child(Child_), Operator(Operator_) {}
1732
1733 template<typename Fn> void match(Fn F) const { F(Child, Operator); }
1734
1735 void printLeft(OutputStream &S) const override {
1736 S += "(";
1737 Child->print(S);
1738 S += ")";
1739 S += Operator;
1740 }
1741};
1742
1743class ConditionalExpr : public Node {
1744 const Node *Cond;
1745 const Node *Then;
1746 const Node *Else;
1747
1748public:
1749 ConditionalExpr(const Node *Cond_, const Node *Then_, const Node *Else_)
1750 : Node(KConditionalExpr), Cond(Cond_), Then(Then_), Else(Else_) {}
1751
1752 template<typename Fn> void match(Fn F) const { F(Cond, Then, Else); }
1753
1754 void printLeft(OutputStream &S) const override {
1755 S += "(";
1756 Cond->print(S);
1757 S += ") ? (";
1758 Then->print(S);
1759 S += ") : (";
1760 Else->print(S);
1761 S += ")";
1762 }
1763};
1764
1765class MemberExpr : public Node {
1766 const Node *LHS;
1767 const StringView Kind;
1768 const Node *RHS;
1769
1770public:
1771 MemberExpr(const Node *LHS_, StringView Kind_, const Node *RHS_)
1772 : Node(KMemberExpr), LHS(LHS_), Kind(Kind_), RHS(RHS_) {}
1773
1774 template<typename Fn> void match(Fn F) const { F(LHS, Kind, RHS); }
1775
1776 void printLeft(OutputStream &S) const override {
1777 LHS->print(S);
1778 S += Kind;
1779 RHS->print(S);
1780 }
1781};
1782
Richard Smith1865d2f2020-10-22 19:29:36 -07001783class SubobjectExpr : public Node {
1784 const Node *Type;
1785 const Node *SubExpr;
1786 StringView Offset;
1787 NodeArray UnionSelectors;
1788 bool OnePastTheEnd;
1789
1790public:
1791 SubobjectExpr(const Node *Type_, const Node *SubExpr_, StringView Offset_,
1792 NodeArray UnionSelectors_, bool OnePastTheEnd_)
1793 : Node(KSubobjectExpr), Type(Type_), SubExpr(SubExpr_), Offset(Offset_),
1794 UnionSelectors(UnionSelectors_), OnePastTheEnd(OnePastTheEnd_) {}
1795
1796 template<typename Fn> void match(Fn F) const {
1797 F(Type, SubExpr, Offset, UnionSelectors, OnePastTheEnd);
1798 }
1799
1800 void printLeft(OutputStream &S) const override {
1801 SubExpr->print(S);
1802 S += ".<";
1803 Type->print(S);
1804 S += " at offset ";
1805 if (Offset.empty()) {
1806 S += "0";
1807 } else if (Offset[0] == 'n') {
1808 S += "-";
1809 S += Offset.dropFront();
1810 } else {
1811 S += Offset;
1812 }
1813 S += ">";
1814 }
1815};
1816
Richard Smithc20d1442018-08-20 20:14:49 +00001817class EnclosingExpr : public Node {
1818 const StringView Prefix;
1819 const Node *Infix;
1820 const StringView Postfix;
1821
1822public:
1823 EnclosingExpr(StringView Prefix_, Node *Infix_, StringView Postfix_)
1824 : Node(KEnclosingExpr), Prefix(Prefix_), Infix(Infix_),
1825 Postfix(Postfix_) {}
1826
1827 template<typename Fn> void match(Fn F) const { F(Prefix, Infix, Postfix); }
1828
1829 void printLeft(OutputStream &S) const override {
1830 S += Prefix;
1831 Infix->print(S);
1832 S += Postfix;
1833 }
1834};
1835
1836class CastExpr : public Node {
1837 // cast_kind<to>(from)
1838 const StringView CastKind;
1839 const Node *To;
1840 const Node *From;
1841
1842public:
1843 CastExpr(StringView CastKind_, const Node *To_, const Node *From_)
1844 : Node(KCastExpr), CastKind(CastKind_), To(To_), From(From_) {}
1845
1846 template<typename Fn> void match(Fn F) const { F(CastKind, To, From); }
1847
1848 void printLeft(OutputStream &S) const override {
1849 S += CastKind;
1850 S += "<";
1851 To->printLeft(S);
1852 S += ">(";
1853 From->printLeft(S);
1854 S += ")";
1855 }
1856};
1857
1858class SizeofParamPackExpr : public Node {
1859 const Node *Pack;
1860
1861public:
1862 SizeofParamPackExpr(const Node *Pack_)
1863 : Node(KSizeofParamPackExpr), Pack(Pack_) {}
1864
1865 template<typename Fn> void match(Fn F) const { F(Pack); }
1866
1867 void printLeft(OutputStream &S) const override {
1868 S += "sizeof...(";
1869 ParameterPackExpansion PPE(Pack);
1870 PPE.printLeft(S);
1871 S += ")";
1872 }
1873};
1874
1875class CallExpr : public Node {
1876 const Node *Callee;
1877 NodeArray Args;
1878
1879public:
1880 CallExpr(const Node *Callee_, NodeArray Args_)
1881 : Node(KCallExpr), Callee(Callee_), Args(Args_) {}
1882
1883 template<typename Fn> void match(Fn F) const { F(Callee, Args); }
1884
1885 void printLeft(OutputStream &S) const override {
1886 Callee->print(S);
1887 S += "(";
1888 Args.printWithComma(S);
1889 S += ")";
1890 }
1891};
1892
1893class NewExpr : public Node {
1894 // new (expr_list) type(init_list)
1895 NodeArray ExprList;
1896 Node *Type;
1897 NodeArray InitList;
1898 bool IsGlobal; // ::operator new ?
1899 bool IsArray; // new[] ?
1900public:
1901 NewExpr(NodeArray ExprList_, Node *Type_, NodeArray InitList_, bool IsGlobal_,
1902 bool IsArray_)
1903 : Node(KNewExpr), ExprList(ExprList_), Type(Type_), InitList(InitList_),
1904 IsGlobal(IsGlobal_), IsArray(IsArray_) {}
1905
1906 template<typename Fn> void match(Fn F) const {
1907 F(ExprList, Type, InitList, IsGlobal, IsArray);
1908 }
1909
1910 void printLeft(OutputStream &S) const override {
1911 if (IsGlobal)
1912 S += "::operator ";
1913 S += "new";
1914 if (IsArray)
1915 S += "[]";
1916 S += ' ';
1917 if (!ExprList.empty()) {
1918 S += "(";
1919 ExprList.printWithComma(S);
1920 S += ")";
1921 }
1922 Type->print(S);
1923 if (!InitList.empty()) {
1924 S += "(";
1925 InitList.printWithComma(S);
1926 S += ")";
1927 }
1928
1929 }
1930};
1931
1932class DeleteExpr : public Node {
1933 Node *Op;
1934 bool IsGlobal;
1935 bool IsArray;
1936
1937public:
1938 DeleteExpr(Node *Op_, bool IsGlobal_, bool IsArray_)
1939 : Node(KDeleteExpr), Op(Op_), IsGlobal(IsGlobal_), IsArray(IsArray_) {}
1940
1941 template<typename Fn> void match(Fn F) const { F(Op, IsGlobal, IsArray); }
1942
1943 void printLeft(OutputStream &S) const override {
1944 if (IsGlobal)
1945 S += "::";
1946 S += "delete";
1947 if (IsArray)
1948 S += "[] ";
1949 Op->print(S);
1950 }
1951};
1952
1953class PrefixExpr : public Node {
1954 StringView Prefix;
1955 Node *Child;
1956
1957public:
1958 PrefixExpr(StringView Prefix_, Node *Child_)
1959 : Node(KPrefixExpr), Prefix(Prefix_), Child(Child_) {}
1960
1961 template<typename Fn> void match(Fn F) const { F(Prefix, Child); }
1962
1963 void printLeft(OutputStream &S) const override {
1964 S += Prefix;
1965 S += "(";
1966 Child->print(S);
1967 S += ")";
1968 }
1969};
1970
1971class FunctionParam : public Node {
1972 StringView Number;
1973
1974public:
1975 FunctionParam(StringView Number_) : Node(KFunctionParam), Number(Number_) {}
1976
1977 template<typename Fn> void match(Fn F) const { F(Number); }
1978
1979 void printLeft(OutputStream &S) const override {
1980 S += "fp";
1981 S += Number;
1982 }
1983};
1984
1985class ConversionExpr : public Node {
1986 const Node *Type;
1987 NodeArray Expressions;
1988
1989public:
1990 ConversionExpr(const Node *Type_, NodeArray Expressions_)
1991 : Node(KConversionExpr), Type(Type_), Expressions(Expressions_) {}
1992
1993 template<typename Fn> void match(Fn F) const { F(Type, Expressions); }
1994
1995 void printLeft(OutputStream &S) const override {
1996 S += "(";
1997 Type->print(S);
1998 S += ")(";
1999 Expressions.printWithComma(S);
2000 S += ")";
2001 }
2002};
2003
Richard Smith1865d2f2020-10-22 19:29:36 -07002004class PointerToMemberConversionExpr : public Node {
2005 const Node *Type;
2006 const Node *SubExpr;
2007 StringView Offset;
2008
2009public:
2010 PointerToMemberConversionExpr(const Node *Type_, const Node *SubExpr_,
2011 StringView Offset_)
2012 : Node(KPointerToMemberConversionExpr), Type(Type_), SubExpr(SubExpr_),
2013 Offset(Offset_) {}
2014
2015 template<typename Fn> void match(Fn F) const { F(Type, SubExpr, Offset); }
2016
2017 void printLeft(OutputStream &S) const override {
2018 S += "(";
2019 Type->print(S);
2020 S += ")(";
2021 SubExpr->print(S);
2022 S += ")";
2023 }
2024};
2025
Richard Smithc20d1442018-08-20 20:14:49 +00002026class InitListExpr : public Node {
2027 const Node *Ty;
2028 NodeArray Inits;
2029public:
2030 InitListExpr(const Node *Ty_, NodeArray Inits_)
2031 : Node(KInitListExpr), Ty(Ty_), Inits(Inits_) {}
2032
2033 template<typename Fn> void match(Fn F) const { F(Ty, Inits); }
2034
2035 void printLeft(OutputStream &S) const override {
2036 if (Ty)
2037 Ty->print(S);
2038 S += '{';
2039 Inits.printWithComma(S);
2040 S += '}';
2041 }
2042};
2043
2044class BracedExpr : public Node {
2045 const Node *Elem;
2046 const Node *Init;
2047 bool IsArray;
2048public:
2049 BracedExpr(const Node *Elem_, const Node *Init_, bool IsArray_)
2050 : Node(KBracedExpr), Elem(Elem_), Init(Init_), IsArray(IsArray_) {}
2051
2052 template<typename Fn> void match(Fn F) const { F(Elem, Init, IsArray); }
2053
2054 void printLeft(OutputStream &S) const override {
2055 if (IsArray) {
2056 S += '[';
2057 Elem->print(S);
2058 S += ']';
2059 } else {
2060 S += '.';
2061 Elem->print(S);
2062 }
2063 if (Init->getKind() != KBracedExpr && Init->getKind() != KBracedRangeExpr)
2064 S += " = ";
2065 Init->print(S);
2066 }
2067};
2068
2069class BracedRangeExpr : public Node {
2070 const Node *First;
2071 const Node *Last;
2072 const Node *Init;
2073public:
2074 BracedRangeExpr(const Node *First_, const Node *Last_, const Node *Init_)
2075 : Node(KBracedRangeExpr), First(First_), Last(Last_), Init(Init_) {}
2076
2077 template<typename Fn> void match(Fn F) const { F(First, Last, Init); }
2078
2079 void printLeft(OutputStream &S) const override {
2080 S += '[';
2081 First->print(S);
2082 S += " ... ";
2083 Last->print(S);
2084 S += ']';
2085 if (Init->getKind() != KBracedExpr && Init->getKind() != KBracedRangeExpr)
2086 S += " = ";
2087 Init->print(S);
2088 }
2089};
2090
2091class FoldExpr : public Node {
2092 const Node *Pack, *Init;
2093 StringView OperatorName;
2094 bool IsLeftFold;
2095
2096public:
2097 FoldExpr(bool IsLeftFold_, StringView OperatorName_, const Node *Pack_,
2098 const Node *Init_)
2099 : Node(KFoldExpr), Pack(Pack_), Init(Init_), OperatorName(OperatorName_),
2100 IsLeftFold(IsLeftFold_) {}
2101
2102 template<typename Fn> void match(Fn F) const {
2103 F(IsLeftFold, OperatorName, Pack, Init);
2104 }
2105
2106 void printLeft(OutputStream &S) const override {
2107 auto PrintPack = [&] {
2108 S += '(';
2109 ParameterPackExpansion(Pack).print(S);
2110 S += ')';
2111 };
2112
2113 S += '(';
2114
2115 if (IsLeftFold) {
2116 // init op ... op pack
2117 if (Init != nullptr) {
2118 Init->print(S);
2119 S += ' ';
2120 S += OperatorName;
2121 S += ' ';
2122 }
2123 // ... op pack
2124 S += "... ";
2125 S += OperatorName;
2126 S += ' ';
2127 PrintPack();
2128 } else { // !IsLeftFold
2129 // pack op ...
2130 PrintPack();
2131 S += ' ';
2132 S += OperatorName;
2133 S += " ...";
2134 // pack op ... op init
2135 if (Init != nullptr) {
2136 S += ' ';
2137 S += OperatorName;
2138 S += ' ';
2139 Init->print(S);
2140 }
2141 }
2142 S += ')';
2143 }
2144};
2145
2146class ThrowExpr : public Node {
2147 const Node *Op;
2148
2149public:
2150 ThrowExpr(const Node *Op_) : Node(KThrowExpr), Op(Op_) {}
2151
2152 template<typename Fn> void match(Fn F) const { F(Op); }
2153
2154 void printLeft(OutputStream &S) const override {
2155 S += "throw ";
2156 Op->print(S);
2157 }
2158};
2159
2160class BoolExpr : public Node {
2161 bool Value;
2162
2163public:
2164 BoolExpr(bool Value_) : Node(KBoolExpr), Value(Value_) {}
2165
2166 template<typename Fn> void match(Fn F) const { F(Value); }
2167
2168 void printLeft(OutputStream &S) const override {
2169 S += Value ? StringView("true") : StringView("false");
2170 }
2171};
2172
Richard Smithdf1c14c2019-09-06 23:53:21 +00002173class StringLiteral : public Node {
2174 const Node *Type;
2175
2176public:
2177 StringLiteral(const Node *Type_) : Node(KStringLiteral), Type(Type_) {}
2178
2179 template<typename Fn> void match(Fn F) const { F(Type); }
2180
2181 void printLeft(OutputStream &S) const override {
2182 S += "\"<";
2183 Type->print(S);
2184 S += ">\"";
2185 }
2186};
2187
2188class LambdaExpr : public Node {
2189 const Node *Type;
2190
Richard Smithdf1c14c2019-09-06 23:53:21 +00002191public:
2192 LambdaExpr(const Node *Type_) : Node(KLambdaExpr), Type(Type_) {}
2193
2194 template<typename Fn> void match(Fn F) const { F(Type); }
2195
2196 void printLeft(OutputStream &S) const override {
2197 S += "[]";
Richard Smithfb917462019-09-09 22:26:04 +00002198 if (Type->getKind() == KClosureTypeName)
2199 static_cast<const ClosureTypeName *>(Type)->printDeclarator(S);
Richard Smithdf1c14c2019-09-06 23:53:21 +00002200 S += "{...}";
2201 }
2202};
2203
Erik Pilkington0a170f12020-05-13 14:13:37 -04002204class EnumLiteral : public Node {
Richard Smithc20d1442018-08-20 20:14:49 +00002205 // ty(integer)
2206 const Node *Ty;
2207 StringView Integer;
2208
2209public:
Erik Pilkington0a170f12020-05-13 14:13:37 -04002210 EnumLiteral(const Node *Ty_, StringView Integer_)
2211 : Node(KEnumLiteral), Ty(Ty_), Integer(Integer_) {}
Richard Smithc20d1442018-08-20 20:14:49 +00002212
2213 template<typename Fn> void match(Fn F) const { F(Ty, Integer); }
2214
2215 void printLeft(OutputStream &S) const override {
Erik Pilkington0a170f12020-05-13 14:13:37 -04002216 S << "(";
Richard Smithc20d1442018-08-20 20:14:49 +00002217 Ty->print(S);
Erik Pilkington0a170f12020-05-13 14:13:37 -04002218 S << ")";
2219
2220 if (Integer[0] == 'n')
2221 S << "-" << Integer.dropFront(1);
2222 else
2223 S << Integer;
Richard Smithc20d1442018-08-20 20:14:49 +00002224 }
2225};
2226
2227class IntegerLiteral : public Node {
2228 StringView Type;
2229 StringView Value;
2230
2231public:
2232 IntegerLiteral(StringView Type_, StringView Value_)
2233 : Node(KIntegerLiteral), Type(Type_), Value(Value_) {}
2234
2235 template<typename Fn> void match(Fn F) const { F(Type, Value); }
2236
2237 void printLeft(OutputStream &S) const override {
2238 if (Type.size() > 3) {
2239 S += "(";
2240 S += Type;
2241 S += ")";
2242 }
2243
2244 if (Value[0] == 'n') {
2245 S += "-";
2246 S += Value.dropFront(1);
2247 } else
2248 S += Value;
2249
2250 if (Type.size() <= 3)
2251 S += Type;
2252 }
2253};
2254
2255template <class Float> struct FloatData;
2256
2257namespace float_literal_impl {
2258constexpr Node::Kind getFloatLiteralKind(float *) {
2259 return Node::KFloatLiteral;
2260}
2261constexpr Node::Kind getFloatLiteralKind(double *) {
2262 return Node::KDoubleLiteral;
2263}
2264constexpr Node::Kind getFloatLiteralKind(long double *) {
2265 return Node::KLongDoubleLiteral;
2266}
2267}
2268
2269template <class Float> class FloatLiteralImpl : public Node {
2270 const StringView Contents;
2271
2272 static constexpr Kind KindForClass =
2273 float_literal_impl::getFloatLiteralKind((Float *)nullptr);
2274
2275public:
2276 FloatLiteralImpl(StringView Contents_)
2277 : Node(KindForClass), Contents(Contents_) {}
2278
2279 template<typename Fn> void match(Fn F) const { F(Contents); }
2280
2281 void printLeft(OutputStream &s) const override {
2282 const char *first = Contents.begin();
2283 const char *last = Contents.end() + 1;
2284
2285 const size_t N = FloatData<Float>::mangled_size;
2286 if (static_cast<std::size_t>(last - first) > N) {
2287 last = first + N;
2288 union {
2289 Float value;
2290 char buf[sizeof(Float)];
2291 };
2292 const char *t = first;
2293 char *e = buf;
2294 for (; t != last; ++t, ++e) {
2295 unsigned d1 = isdigit(*t) ? static_cast<unsigned>(*t - '0')
2296 : static_cast<unsigned>(*t - 'a' + 10);
2297 ++t;
2298 unsigned d0 = isdigit(*t) ? static_cast<unsigned>(*t - '0')
2299 : static_cast<unsigned>(*t - 'a' + 10);
2300 *e = static_cast<char>((d1 << 4) + d0);
2301 }
2302#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
2303 std::reverse(buf, e);
2304#endif
2305 char num[FloatData<Float>::max_demangled_size] = {0};
2306 int n = snprintf(num, sizeof(num), FloatData<Float>::spec, value);
2307 s += StringView(num, num + n);
2308 }
2309 }
2310};
2311
2312using FloatLiteral = FloatLiteralImpl<float>;
2313using DoubleLiteral = FloatLiteralImpl<double>;
2314using LongDoubleLiteral = FloatLiteralImpl<long double>;
2315
2316/// Visit the node. Calls \c F(P), where \c P is the node cast to the
2317/// appropriate derived class.
2318template<typename Fn>
2319void Node::visit(Fn F) const {
2320 switch (K) {
2321#define CASE(X) case K ## X: return F(static_cast<const X*>(this));
2322 FOR_EACH_NODE_KIND(CASE)
2323#undef CASE
2324 }
2325 assert(0 && "unknown mangling node kind");
2326}
2327
2328/// Determine the kind of a node from its type.
2329template<typename NodeT> struct NodeKind;
2330#define SPECIALIZATION(X) \
2331 template<> struct NodeKind<X> { \
2332 static constexpr Node::Kind Kind = Node::K##X; \
2333 static constexpr const char *name() { return #X; } \
2334 };
2335FOR_EACH_NODE_KIND(SPECIALIZATION)
2336#undef SPECIALIZATION
2337
2338#undef FOR_EACH_NODE_KIND
2339
Pavel Labathba825192018-10-16 14:29:14 +00002340template <typename Derived, typename Alloc> struct AbstractManglingParser {
Richard Smithc20d1442018-08-20 20:14:49 +00002341 const char *First;
2342 const char *Last;
2343
2344 // Name stack, this is used by the parser to hold temporary names that were
2345 // parsed. The parser collapses multiple names into new nodes to construct
2346 // the AST. Once the parser is finished, names.size() == 1.
2347 PODSmallVector<Node *, 32> Names;
2348
2349 // Substitution table. Itanium supports name substitutions as a means of
2350 // compression. The string "S42_" refers to the 44nd entry (base-36) in this
2351 // table.
2352 PODSmallVector<Node *, 32> Subs;
2353
Richard Smithdf1c14c2019-09-06 23:53:21 +00002354 using TemplateParamList = PODSmallVector<Node *, 8>;
2355
2356 class ScopedTemplateParamList {
2357 AbstractManglingParser *Parser;
2358 size_t OldNumTemplateParamLists;
2359 TemplateParamList Params;
2360
2361 public:
Louis Dionnec1fe8672020-10-30 17:33:02 -04002362 ScopedTemplateParamList(AbstractManglingParser *TheParser)
2363 : Parser(TheParser),
2364 OldNumTemplateParamLists(TheParser->TemplateParams.size()) {
Richard Smithdf1c14c2019-09-06 23:53:21 +00002365 Parser->TemplateParams.push_back(&Params);
2366 }
2367 ~ScopedTemplateParamList() {
2368 assert(Parser->TemplateParams.size() >= OldNumTemplateParamLists);
2369 Parser->TemplateParams.dropBack(OldNumTemplateParamLists);
2370 }
Richard Smithdf1c14c2019-09-06 23:53:21 +00002371 };
2372
Richard Smithc20d1442018-08-20 20:14:49 +00002373 // Template parameter table. Like the above, but referenced like "T42_".
2374 // This has a smaller size compared to Subs and Names because it can be
2375 // stored on the stack.
Richard Smithdf1c14c2019-09-06 23:53:21 +00002376 TemplateParamList OuterTemplateParams;
2377
2378 // Lists of template parameters indexed by template parameter depth,
2379 // referenced like "TL2_4_". If nonempty, element 0 is always
2380 // OuterTemplateParams; inner elements are always template parameter lists of
2381 // lambda expressions. For a generic lambda with no explicit template
2382 // parameter list, the corresponding parameter list pointer will be null.
2383 PODSmallVector<TemplateParamList *, 4> TemplateParams;
Richard Smithc20d1442018-08-20 20:14:49 +00002384
2385 // Set of unresolved forward <template-param> references. These can occur in a
2386 // conversion operator's type, and are resolved in the enclosing <encoding>.
2387 PODSmallVector<ForwardTemplateReference *, 4> ForwardTemplateRefs;
2388
Richard Smithc20d1442018-08-20 20:14:49 +00002389 bool TryToParseTemplateArgs = true;
2390 bool PermitForwardTemplateReferences = false;
Richard Smithdf1c14c2019-09-06 23:53:21 +00002391 size_t ParsingLambdaParamsAtLevel = (size_t)-1;
2392
2393 unsigned NumSyntheticTemplateParameters[3] = {};
Richard Smithc20d1442018-08-20 20:14:49 +00002394
2395 Alloc ASTAllocator;
2396
Pavel Labathba825192018-10-16 14:29:14 +00002397 AbstractManglingParser(const char *First_, const char *Last_)
2398 : First(First_), Last(Last_) {}
2399
2400 Derived &getDerived() { return static_cast<Derived &>(*this); }
Richard Smithc20d1442018-08-20 20:14:49 +00002401
2402 void reset(const char *First_, const char *Last_) {
2403 First = First_;
2404 Last = Last_;
2405 Names.clear();
2406 Subs.clear();
2407 TemplateParams.clear();
Richard Smithdf1c14c2019-09-06 23:53:21 +00002408 ParsingLambdaParamsAtLevel = (size_t)-1;
Richard Smithc20d1442018-08-20 20:14:49 +00002409 TryToParseTemplateArgs = true;
2410 PermitForwardTemplateReferences = false;
Richard Smith9a2307a2019-09-07 00:11:53 +00002411 for (int I = 0; I != 3; ++I)
2412 NumSyntheticTemplateParameters[I] = 0;
Richard Smithc20d1442018-08-20 20:14:49 +00002413 ASTAllocator.reset();
2414 }
2415
Richard Smithb485b352018-08-24 23:30:26 +00002416 template <class T, class... Args> Node *make(Args &&... args) {
Richard Smithc20d1442018-08-20 20:14:49 +00002417 return ASTAllocator.template makeNode<T>(std::forward<Args>(args)...);
2418 }
2419
2420 template <class It> NodeArray makeNodeArray(It begin, It end) {
2421 size_t sz = static_cast<size_t>(end - begin);
2422 void *mem = ASTAllocator.allocateNodeArray(sz);
2423 Node **data = new (mem) Node *[sz];
2424 std::copy(begin, end, data);
2425 return NodeArray(data, sz);
2426 }
2427
2428 NodeArray popTrailingNodeArray(size_t FromPosition) {
2429 assert(FromPosition <= Names.size());
2430 NodeArray res =
2431 makeNodeArray(Names.begin() + (long)FromPosition, Names.end());
2432 Names.dropBack(FromPosition);
2433 return res;
2434 }
2435
2436 bool consumeIf(StringView S) {
2437 if (StringView(First, Last).startsWith(S)) {
2438 First += S.size();
2439 return true;
2440 }
2441 return false;
2442 }
2443
2444 bool consumeIf(char C) {
2445 if (First != Last && *First == C) {
2446 ++First;
2447 return true;
2448 }
2449 return false;
2450 }
2451
2452 char consume() { return First != Last ? *First++ : '\0'; }
2453
2454 char look(unsigned Lookahead = 0) {
2455 if (static_cast<size_t>(Last - First) <= Lookahead)
2456 return '\0';
2457 return First[Lookahead];
2458 }
2459
2460 size_t numLeft() const { return static_cast<size_t>(Last - First); }
2461
2462 StringView parseNumber(bool AllowNegative = false);
2463 Qualifiers parseCVQualifiers();
2464 bool parsePositiveInteger(size_t *Out);
2465 StringView parseBareSourceName();
2466
2467 bool parseSeqId(size_t *Out);
2468 Node *parseSubstitution();
2469 Node *parseTemplateParam();
Richard Smithdf1c14c2019-09-06 23:53:21 +00002470 Node *parseTemplateParamDecl();
Richard Smithc20d1442018-08-20 20:14:49 +00002471 Node *parseTemplateArgs(bool TagTemplates = false);
2472 Node *parseTemplateArg();
2473
2474 /// Parse the <expr> production.
2475 Node *parseExpr();
2476 Node *parsePrefixExpr(StringView Kind);
2477 Node *parseBinaryExpr(StringView Kind);
2478 Node *parseIntegerLiteral(StringView Lit);
2479 Node *parseExprPrimary();
2480 template <class Float> Node *parseFloatingLiteral();
2481 Node *parseFunctionParam();
2482 Node *parseNewExpr();
2483 Node *parseConversionExpr();
2484 Node *parseBracedExpr();
2485 Node *parseFoldExpr();
Richard Smith1865d2f2020-10-22 19:29:36 -07002486 Node *parsePointerToMemberConversionExpr();
2487 Node *parseSubobjectExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00002488
2489 /// Parse the <type> production.
2490 Node *parseType();
2491 Node *parseFunctionType();
2492 Node *parseVectorType();
2493 Node *parseDecltype();
2494 Node *parseArrayType();
2495 Node *parsePointerToMemberType();
2496 Node *parseClassEnumType();
2497 Node *parseQualifiedType();
2498
2499 Node *parseEncoding();
2500 bool parseCallOffset();
2501 Node *parseSpecialName();
2502
2503 /// Holds some extra information about a <name> that is being parsed. This
2504 /// information is only pertinent if the <name> refers to an <encoding>.
2505 struct NameState {
2506 bool CtorDtorConversion = false;
2507 bool EndsWithTemplateArgs = false;
2508 Qualifiers CVQualifiers = QualNone;
2509 FunctionRefQual ReferenceQualifier = FrefQualNone;
2510 size_t ForwardTemplateRefsBegin;
2511
Pavel Labathba825192018-10-16 14:29:14 +00002512 NameState(AbstractManglingParser *Enclosing)
Richard Smithc20d1442018-08-20 20:14:49 +00002513 : ForwardTemplateRefsBegin(Enclosing->ForwardTemplateRefs.size()) {}
2514 };
2515
2516 bool resolveForwardTemplateRefs(NameState &State) {
2517 size_t I = State.ForwardTemplateRefsBegin;
2518 size_t E = ForwardTemplateRefs.size();
2519 for (; I < E; ++I) {
2520 size_t Idx = ForwardTemplateRefs[I]->Index;
Richard Smithdf1c14c2019-09-06 23:53:21 +00002521 if (TemplateParams.empty() || !TemplateParams[0] ||
2522 Idx >= TemplateParams[0]->size())
Richard Smithc20d1442018-08-20 20:14:49 +00002523 return true;
Richard Smithdf1c14c2019-09-06 23:53:21 +00002524 ForwardTemplateRefs[I]->Ref = (*TemplateParams[0])[Idx];
Richard Smithc20d1442018-08-20 20:14:49 +00002525 }
2526 ForwardTemplateRefs.dropBack(State.ForwardTemplateRefsBegin);
2527 return false;
2528 }
2529
2530 /// Parse the <name> production>
2531 Node *parseName(NameState *State = nullptr);
2532 Node *parseLocalName(NameState *State);
2533 Node *parseOperatorName(NameState *State);
2534 Node *parseUnqualifiedName(NameState *State);
2535 Node *parseUnnamedTypeName(NameState *State);
2536 Node *parseSourceName(NameState *State);
2537 Node *parseUnscopedName(NameState *State);
2538 Node *parseNestedName(NameState *State);
2539 Node *parseCtorDtorName(Node *&SoFar, NameState *State);
2540
2541 Node *parseAbiTags(Node *N);
2542
2543 /// Parse the <unresolved-name> production.
2544 Node *parseUnresolvedName();
2545 Node *parseSimpleId();
2546 Node *parseBaseUnresolvedName();
2547 Node *parseUnresolvedType();
2548 Node *parseDestructorName();
2549
2550 /// Top-level entry point into the parser.
2551 Node *parse();
2552};
2553
2554const char* parse_discriminator(const char* first, const char* last);
2555
2556// <name> ::= <nested-name> // N
2557// ::= <local-name> # See Scope Encoding below // Z
2558// ::= <unscoped-template-name> <template-args>
2559// ::= <unscoped-name>
2560//
2561// <unscoped-template-name> ::= <unscoped-name>
2562// ::= <substitution>
Pavel Labathba825192018-10-16 14:29:14 +00002563template <typename Derived, typename Alloc>
2564Node *AbstractManglingParser<Derived, Alloc>::parseName(NameState *State) {
Richard Smithc20d1442018-08-20 20:14:49 +00002565 consumeIf('L'); // extension
2566
2567 if (look() == 'N')
Pavel Labathba825192018-10-16 14:29:14 +00002568 return getDerived().parseNestedName(State);
Richard Smithc20d1442018-08-20 20:14:49 +00002569 if (look() == 'Z')
Pavel Labathba825192018-10-16 14:29:14 +00002570 return getDerived().parseLocalName(State);
Richard Smithc20d1442018-08-20 20:14:49 +00002571
2572 // ::= <unscoped-template-name> <template-args>
2573 if (look() == 'S' && look(1) != 't') {
Pavel Labathba825192018-10-16 14:29:14 +00002574 Node *S = getDerived().parseSubstitution();
Richard Smithc20d1442018-08-20 20:14:49 +00002575 if (S == nullptr)
2576 return nullptr;
2577 if (look() != 'I')
2578 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00002579 Node *TA = getDerived().parseTemplateArgs(State != nullptr);
Richard Smithc20d1442018-08-20 20:14:49 +00002580 if (TA == nullptr)
2581 return nullptr;
2582 if (State) State->EndsWithTemplateArgs = true;
2583 return make<NameWithTemplateArgs>(S, TA);
2584 }
2585
Pavel Labathba825192018-10-16 14:29:14 +00002586 Node *N = getDerived().parseUnscopedName(State);
Richard Smithc20d1442018-08-20 20:14:49 +00002587 if (N == nullptr)
2588 return nullptr;
2589 // ::= <unscoped-template-name> <template-args>
2590 if (look() == 'I') {
2591 Subs.push_back(N);
Pavel Labathba825192018-10-16 14:29:14 +00002592 Node *TA = getDerived().parseTemplateArgs(State != nullptr);
Richard Smithc20d1442018-08-20 20:14:49 +00002593 if (TA == nullptr)
2594 return nullptr;
2595 if (State) State->EndsWithTemplateArgs = true;
2596 return make<NameWithTemplateArgs>(N, TA);
2597 }
2598 // ::= <unscoped-name>
2599 return N;
2600}
2601
2602// <local-name> := Z <function encoding> E <entity name> [<discriminator>]
2603// := Z <function encoding> E s [<discriminator>]
2604// := Z <function encoding> Ed [ <parameter number> ] _ <entity name>
Pavel Labathba825192018-10-16 14:29:14 +00002605template <typename Derived, typename Alloc>
2606Node *AbstractManglingParser<Derived, Alloc>::parseLocalName(NameState *State) {
Richard Smithc20d1442018-08-20 20:14:49 +00002607 if (!consumeIf('Z'))
2608 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00002609 Node *Encoding = getDerived().parseEncoding();
Richard Smithc20d1442018-08-20 20:14:49 +00002610 if (Encoding == nullptr || !consumeIf('E'))
2611 return nullptr;
2612
2613 if (consumeIf('s')) {
2614 First = parse_discriminator(First, Last);
Richard Smithb485b352018-08-24 23:30:26 +00002615 auto *StringLitName = make<NameType>("string literal");
2616 if (!StringLitName)
2617 return nullptr;
2618 return make<LocalName>(Encoding, StringLitName);
Richard Smithc20d1442018-08-20 20:14:49 +00002619 }
2620
2621 if (consumeIf('d')) {
2622 parseNumber(true);
2623 if (!consumeIf('_'))
2624 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00002625 Node *N = getDerived().parseName(State);
Richard Smithc20d1442018-08-20 20:14:49 +00002626 if (N == nullptr)
2627 return nullptr;
2628 return make<LocalName>(Encoding, N);
2629 }
2630
Pavel Labathba825192018-10-16 14:29:14 +00002631 Node *Entity = getDerived().parseName(State);
Richard Smithc20d1442018-08-20 20:14:49 +00002632 if (Entity == nullptr)
2633 return nullptr;
2634 First = parse_discriminator(First, Last);
2635 return make<LocalName>(Encoding, Entity);
2636}
2637
2638// <unscoped-name> ::= <unqualified-name>
2639// ::= St <unqualified-name> # ::std::
2640// extension ::= StL<unqualified-name>
Pavel Labathba825192018-10-16 14:29:14 +00002641template <typename Derived, typename Alloc>
2642Node *
2643AbstractManglingParser<Derived, Alloc>::parseUnscopedName(NameState *State) {
2644 if (consumeIf("StL") || consumeIf("St")) {
2645 Node *R = getDerived().parseUnqualifiedName(State);
2646 if (R == nullptr)
2647 return nullptr;
2648 return make<StdQualifiedName>(R);
2649 }
2650 return getDerived().parseUnqualifiedName(State);
Richard Smithc20d1442018-08-20 20:14:49 +00002651}
2652
2653// <unqualified-name> ::= <operator-name> [abi-tags]
2654// ::= <ctor-dtor-name>
2655// ::= <source-name>
2656// ::= <unnamed-type-name>
2657// ::= DC <source-name>+ E # structured binding declaration
Pavel Labathba825192018-10-16 14:29:14 +00002658template <typename Derived, typename Alloc>
2659Node *
2660AbstractManglingParser<Derived, Alloc>::parseUnqualifiedName(NameState *State) {
Richard Smithc20d1442018-08-20 20:14:49 +00002661 // <ctor-dtor-name>s are special-cased in parseNestedName().
2662 Node *Result;
2663 if (look() == 'U')
Pavel Labathba825192018-10-16 14:29:14 +00002664 Result = getDerived().parseUnnamedTypeName(State);
Richard Smithc20d1442018-08-20 20:14:49 +00002665 else if (look() >= '1' && look() <= '9')
Pavel Labathba825192018-10-16 14:29:14 +00002666 Result = getDerived().parseSourceName(State);
Richard Smithc20d1442018-08-20 20:14:49 +00002667 else if (consumeIf("DC")) {
2668 size_t BindingsBegin = Names.size();
2669 do {
Pavel Labathba825192018-10-16 14:29:14 +00002670 Node *Binding = getDerived().parseSourceName(State);
Richard Smithc20d1442018-08-20 20:14:49 +00002671 if (Binding == nullptr)
2672 return nullptr;
2673 Names.push_back(Binding);
2674 } while (!consumeIf('E'));
2675 Result = make<StructuredBindingName>(popTrailingNodeArray(BindingsBegin));
2676 } else
Pavel Labathba825192018-10-16 14:29:14 +00002677 Result = getDerived().parseOperatorName(State);
Richard Smithc20d1442018-08-20 20:14:49 +00002678 if (Result != nullptr)
Pavel Labathba825192018-10-16 14:29:14 +00002679 Result = getDerived().parseAbiTags(Result);
Richard Smithc20d1442018-08-20 20:14:49 +00002680 return Result;
2681}
2682
2683// <unnamed-type-name> ::= Ut [<nonnegative number>] _
2684// ::= <closure-type-name>
2685//
2686// <closure-type-name> ::= Ul <lambda-sig> E [ <nonnegative number> ] _
2687//
2688// <lambda-sig> ::= <parameter type>+ # Parameter types or "v" if the lambda has no parameters
Pavel Labathba825192018-10-16 14:29:14 +00002689template <typename Derived, typename Alloc>
2690Node *
Richard Smithdf1c14c2019-09-06 23:53:21 +00002691AbstractManglingParser<Derived, Alloc>::parseUnnamedTypeName(NameState *State) {
2692 // <template-params> refer to the innermost <template-args>. Clear out any
2693 // outer args that we may have inserted into TemplateParams.
2694 if (State != nullptr)
2695 TemplateParams.clear();
2696
Richard Smithc20d1442018-08-20 20:14:49 +00002697 if (consumeIf("Ut")) {
2698 StringView Count = parseNumber();
2699 if (!consumeIf('_'))
2700 return nullptr;
2701 return make<UnnamedTypeName>(Count);
2702 }
2703 if (consumeIf("Ul")) {
Richard Smithdf1c14c2019-09-06 23:53:21 +00002704 SwapAndRestore<size_t> SwapParams(ParsingLambdaParamsAtLevel,
2705 TemplateParams.size());
2706 ScopedTemplateParamList LambdaTemplateParams(this);
2707
2708 size_t ParamsBegin = Names.size();
2709 while (look() == 'T' &&
2710 StringView("yptn").find(look(1)) != StringView::npos) {
2711 Node *T = parseTemplateParamDecl();
2712 if (!T)
2713 return nullptr;
Richard Smithdf1c14c2019-09-06 23:53:21 +00002714 Names.push_back(T);
2715 }
2716 NodeArray TempParams = popTrailingNodeArray(ParamsBegin);
2717
2718 // FIXME: If TempParams is empty and none of the function parameters
2719 // includes 'auto', we should remove LambdaTemplateParams from the
2720 // TemplateParams list. Unfortunately, we don't find out whether there are
2721 // any 'auto' parameters until too late in an example such as:
2722 //
2723 // template<typename T> void f(
2724 // decltype([](decltype([]<typename T>(T v) {}),
2725 // auto) {})) {}
2726 // template<typename T> void f(
2727 // decltype([](decltype([]<typename T>(T w) {}),
2728 // int) {})) {}
2729 //
2730 // Here, the type of v is at level 2 but the type of w is at level 1. We
2731 // don't find this out until we encounter the type of the next parameter.
2732 //
2733 // However, compilers can't actually cope with the former example in
2734 // practice, and it's likely to be made ill-formed in future, so we don't
2735 // need to support it here.
2736 //
2737 // If we encounter an 'auto' in the function parameter types, we will
2738 // recreate a template parameter scope for it, but any intervening lambdas
2739 // will be parsed in the 'wrong' template parameter depth.
2740 if (TempParams.empty())
2741 TemplateParams.pop_back();
2742
Richard Smithc20d1442018-08-20 20:14:49 +00002743 if (!consumeIf("vE")) {
Richard Smithc20d1442018-08-20 20:14:49 +00002744 do {
Pavel Labathba825192018-10-16 14:29:14 +00002745 Node *P = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00002746 if (P == nullptr)
2747 return nullptr;
2748 Names.push_back(P);
2749 } while (!consumeIf('E'));
Richard Smithc20d1442018-08-20 20:14:49 +00002750 }
Richard Smithdf1c14c2019-09-06 23:53:21 +00002751 NodeArray Params = popTrailingNodeArray(ParamsBegin);
2752
Richard Smithc20d1442018-08-20 20:14:49 +00002753 StringView Count = parseNumber();
2754 if (!consumeIf('_'))
2755 return nullptr;
Richard Smithdf1c14c2019-09-06 23:53:21 +00002756 return make<ClosureTypeName>(TempParams, Params, Count);
Richard Smithc20d1442018-08-20 20:14:49 +00002757 }
Erik Pilkington974b6542019-01-17 21:37:51 +00002758 if (consumeIf("Ub")) {
2759 (void)parseNumber();
2760 if (!consumeIf('_'))
2761 return nullptr;
2762 return make<NameType>("'block-literal'");
2763 }
Richard Smithc20d1442018-08-20 20:14:49 +00002764 return nullptr;
2765}
2766
2767// <source-name> ::= <positive length number> <identifier>
Pavel Labathba825192018-10-16 14:29:14 +00002768template <typename Derived, typename Alloc>
2769Node *AbstractManglingParser<Derived, Alloc>::parseSourceName(NameState *) {
Richard Smithc20d1442018-08-20 20:14:49 +00002770 size_t Length = 0;
2771 if (parsePositiveInteger(&Length))
2772 return nullptr;
2773 if (numLeft() < Length || Length == 0)
2774 return nullptr;
2775 StringView Name(First, First + Length);
2776 First += Length;
2777 if (Name.startsWith("_GLOBAL__N"))
2778 return make<NameType>("(anonymous namespace)");
2779 return make<NameType>(Name);
2780}
2781
2782// <operator-name> ::= aa # &&
2783// ::= ad # & (unary)
2784// ::= an # &
2785// ::= aN # &=
2786// ::= aS # =
2787// ::= cl # ()
2788// ::= cm # ,
2789// ::= co # ~
2790// ::= cv <type> # (cast)
2791// ::= da # delete[]
2792// ::= de # * (unary)
2793// ::= dl # delete
2794// ::= dv # /
2795// ::= dV # /=
2796// ::= eo # ^
2797// ::= eO # ^=
2798// ::= eq # ==
2799// ::= ge # >=
2800// ::= gt # >
2801// ::= ix # []
2802// ::= le # <=
2803// ::= li <source-name> # operator ""
2804// ::= ls # <<
2805// ::= lS # <<=
2806// ::= lt # <
2807// ::= mi # -
2808// ::= mI # -=
2809// ::= ml # *
2810// ::= mL # *=
2811// ::= mm # -- (postfix in <expression> context)
2812// ::= na # new[]
2813// ::= ne # !=
2814// ::= ng # - (unary)
2815// ::= nt # !
2816// ::= nw # new
2817// ::= oo # ||
2818// ::= or # |
2819// ::= oR # |=
2820// ::= pm # ->*
2821// ::= pl # +
2822// ::= pL # +=
2823// ::= pp # ++ (postfix in <expression> context)
2824// ::= ps # + (unary)
2825// ::= pt # ->
2826// ::= qu # ?
2827// ::= rm # %
2828// ::= rM # %=
2829// ::= rs # >>
2830// ::= rS # >>=
2831// ::= ss # <=> C++2a
2832// ::= v <digit> <source-name> # vendor extended operator
Pavel Labathba825192018-10-16 14:29:14 +00002833template <typename Derived, typename Alloc>
2834Node *
2835AbstractManglingParser<Derived, Alloc>::parseOperatorName(NameState *State) {
Richard Smithc20d1442018-08-20 20:14:49 +00002836 switch (look()) {
2837 case 'a':
2838 switch (look(1)) {
2839 case 'a':
2840 First += 2;
2841 return make<NameType>("operator&&");
2842 case 'd':
2843 case 'n':
2844 First += 2;
2845 return make<NameType>("operator&");
2846 case 'N':
2847 First += 2;
2848 return make<NameType>("operator&=");
2849 case 'S':
2850 First += 2;
2851 return make<NameType>("operator=");
2852 }
2853 return nullptr;
2854 case 'c':
2855 switch (look(1)) {
2856 case 'l':
2857 First += 2;
2858 return make<NameType>("operator()");
2859 case 'm':
2860 First += 2;
2861 return make<NameType>("operator,");
2862 case 'o':
2863 First += 2;
2864 return make<NameType>("operator~");
2865 // ::= cv <type> # (cast)
2866 case 'v': {
2867 First += 2;
2868 SwapAndRestore<bool> SaveTemplate(TryToParseTemplateArgs, false);
2869 // If we're parsing an encoding, State != nullptr and the conversion
2870 // operators' <type> could have a <template-param> that refers to some
2871 // <template-arg>s further ahead in the mangled name.
2872 SwapAndRestore<bool> SavePermit(PermitForwardTemplateReferences,
2873 PermitForwardTemplateReferences ||
2874 State != nullptr);
Pavel Labathba825192018-10-16 14:29:14 +00002875 Node *Ty = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00002876 if (Ty == nullptr)
2877 return nullptr;
2878 if (State) State->CtorDtorConversion = true;
2879 return make<ConversionOperatorType>(Ty);
2880 }
2881 }
2882 return nullptr;
2883 case 'd':
2884 switch (look(1)) {
2885 case 'a':
2886 First += 2;
2887 return make<NameType>("operator delete[]");
2888 case 'e':
2889 First += 2;
2890 return make<NameType>("operator*");
2891 case 'l':
2892 First += 2;
2893 return make<NameType>("operator delete");
2894 case 'v':
2895 First += 2;
2896 return make<NameType>("operator/");
2897 case 'V':
2898 First += 2;
2899 return make<NameType>("operator/=");
2900 }
2901 return nullptr;
2902 case 'e':
2903 switch (look(1)) {
2904 case 'o':
2905 First += 2;
2906 return make<NameType>("operator^");
2907 case 'O':
2908 First += 2;
2909 return make<NameType>("operator^=");
2910 case 'q':
2911 First += 2;
2912 return make<NameType>("operator==");
2913 }
2914 return nullptr;
2915 case 'g':
2916 switch (look(1)) {
2917 case 'e':
2918 First += 2;
2919 return make<NameType>("operator>=");
2920 case 't':
2921 First += 2;
2922 return make<NameType>("operator>");
2923 }
2924 return nullptr;
2925 case 'i':
2926 if (look(1) == 'x') {
2927 First += 2;
2928 return make<NameType>("operator[]");
2929 }
2930 return nullptr;
2931 case 'l':
2932 switch (look(1)) {
2933 case 'e':
2934 First += 2;
2935 return make<NameType>("operator<=");
2936 // ::= li <source-name> # operator ""
2937 case 'i': {
2938 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00002939 Node *SN = getDerived().parseSourceName(State);
Richard Smithc20d1442018-08-20 20:14:49 +00002940 if (SN == nullptr)
2941 return nullptr;
2942 return make<LiteralOperator>(SN);
2943 }
2944 case 's':
2945 First += 2;
2946 return make<NameType>("operator<<");
2947 case 'S':
2948 First += 2;
2949 return make<NameType>("operator<<=");
2950 case 't':
2951 First += 2;
2952 return make<NameType>("operator<");
2953 }
2954 return nullptr;
2955 case 'm':
2956 switch (look(1)) {
2957 case 'i':
2958 First += 2;
2959 return make<NameType>("operator-");
2960 case 'I':
2961 First += 2;
2962 return make<NameType>("operator-=");
2963 case 'l':
2964 First += 2;
2965 return make<NameType>("operator*");
2966 case 'L':
2967 First += 2;
2968 return make<NameType>("operator*=");
2969 case 'm':
2970 First += 2;
2971 return make<NameType>("operator--");
2972 }
2973 return nullptr;
2974 case 'n':
2975 switch (look(1)) {
2976 case 'a':
2977 First += 2;
2978 return make<NameType>("operator new[]");
2979 case 'e':
2980 First += 2;
2981 return make<NameType>("operator!=");
2982 case 'g':
2983 First += 2;
2984 return make<NameType>("operator-");
2985 case 't':
2986 First += 2;
2987 return make<NameType>("operator!");
2988 case 'w':
2989 First += 2;
2990 return make<NameType>("operator new");
2991 }
2992 return nullptr;
2993 case 'o':
2994 switch (look(1)) {
2995 case 'o':
2996 First += 2;
2997 return make<NameType>("operator||");
2998 case 'r':
2999 First += 2;
3000 return make<NameType>("operator|");
3001 case 'R':
3002 First += 2;
3003 return make<NameType>("operator|=");
3004 }
3005 return nullptr;
3006 case 'p':
3007 switch (look(1)) {
3008 case 'm':
3009 First += 2;
3010 return make<NameType>("operator->*");
3011 case 'l':
3012 First += 2;
3013 return make<NameType>("operator+");
3014 case 'L':
3015 First += 2;
3016 return make<NameType>("operator+=");
3017 case 'p':
3018 First += 2;
3019 return make<NameType>("operator++");
3020 case 's':
3021 First += 2;
3022 return make<NameType>("operator+");
3023 case 't':
3024 First += 2;
3025 return make<NameType>("operator->");
3026 }
3027 return nullptr;
3028 case 'q':
3029 if (look(1) == 'u') {
3030 First += 2;
3031 return make<NameType>("operator?");
3032 }
3033 return nullptr;
3034 case 'r':
3035 switch (look(1)) {
3036 case 'm':
3037 First += 2;
3038 return make<NameType>("operator%");
3039 case 'M':
3040 First += 2;
3041 return make<NameType>("operator%=");
3042 case 's':
3043 First += 2;
3044 return make<NameType>("operator>>");
3045 case 'S':
3046 First += 2;
3047 return make<NameType>("operator>>=");
3048 }
3049 return nullptr;
3050 case 's':
3051 if (look(1) == 's') {
3052 First += 2;
3053 return make<NameType>("operator<=>");
3054 }
3055 return nullptr;
3056 // ::= v <digit> <source-name> # vendor extended operator
3057 case 'v':
3058 if (std::isdigit(look(1))) {
3059 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00003060 Node *SN = getDerived().parseSourceName(State);
Richard Smithc20d1442018-08-20 20:14:49 +00003061 if (SN == nullptr)
3062 return nullptr;
3063 return make<ConversionOperatorType>(SN);
3064 }
3065 return nullptr;
3066 }
3067 return nullptr;
3068}
3069
3070// <ctor-dtor-name> ::= C1 # complete object constructor
3071// ::= C2 # base object constructor
3072// ::= C3 # complete object allocating constructor
Nico Weber29294792019-04-03 23:14:33 +00003073// extension ::= C4 # gcc old-style "[unified]" constructor
3074// extension ::= C5 # the COMDAT used for ctors
Richard Smithc20d1442018-08-20 20:14:49 +00003075// ::= D0 # deleting destructor
3076// ::= D1 # complete object destructor
3077// ::= D2 # base object destructor
Nico Weber29294792019-04-03 23:14:33 +00003078// extension ::= D4 # gcc old-style "[unified]" destructor
3079// extension ::= D5 # the COMDAT used for dtors
Pavel Labathba825192018-10-16 14:29:14 +00003080template <typename Derived, typename Alloc>
3081Node *
3082AbstractManglingParser<Derived, Alloc>::parseCtorDtorName(Node *&SoFar,
3083 NameState *State) {
Richard Smithc20d1442018-08-20 20:14:49 +00003084 if (SoFar->getKind() == Node::KSpecialSubstitution) {
3085 auto SSK = static_cast<SpecialSubstitution *>(SoFar)->SSK;
3086 switch (SSK) {
3087 case SpecialSubKind::string:
3088 case SpecialSubKind::istream:
3089 case SpecialSubKind::ostream:
3090 case SpecialSubKind::iostream:
3091 SoFar = make<ExpandedSpecialSubstitution>(SSK);
Richard Smithb485b352018-08-24 23:30:26 +00003092 if (!SoFar)
3093 return nullptr;
Reid Klecknere76aabe2018-11-01 18:24:03 +00003094 break;
Richard Smithc20d1442018-08-20 20:14:49 +00003095 default:
3096 break;
3097 }
3098 }
3099
3100 if (consumeIf('C')) {
3101 bool IsInherited = consumeIf('I');
Nico Weber29294792019-04-03 23:14:33 +00003102 if (look() != '1' && look() != '2' && look() != '3' && look() != '4' &&
3103 look() != '5')
Richard Smithc20d1442018-08-20 20:14:49 +00003104 return nullptr;
Pavel Labathf4e67eb2018-10-10 08:39:16 +00003105 int Variant = look() - '0';
Richard Smithc20d1442018-08-20 20:14:49 +00003106 ++First;
3107 if (State) State->CtorDtorConversion = true;
3108 if (IsInherited) {
Pavel Labathba825192018-10-16 14:29:14 +00003109 if (getDerived().parseName(State) == nullptr)
Richard Smithc20d1442018-08-20 20:14:49 +00003110 return nullptr;
3111 }
Nico Weber29294792019-04-03 23:14:33 +00003112 return make<CtorDtorName>(SoFar, /*IsDtor=*/false, Variant);
Richard Smithc20d1442018-08-20 20:14:49 +00003113 }
3114
Nico Weber29294792019-04-03 23:14:33 +00003115 if (look() == 'D' && (look(1) == '0' || look(1) == '1' || look(1) == '2' ||
3116 look(1) == '4' || look(1) == '5')) {
Pavel Labathf4e67eb2018-10-10 08:39:16 +00003117 int Variant = look(1) - '0';
Richard Smithc20d1442018-08-20 20:14:49 +00003118 First += 2;
3119 if (State) State->CtorDtorConversion = true;
Nico Weber29294792019-04-03 23:14:33 +00003120 return make<CtorDtorName>(SoFar, /*IsDtor=*/true, Variant);
Richard Smithc20d1442018-08-20 20:14:49 +00003121 }
3122
3123 return nullptr;
3124}
3125
3126// <nested-name> ::= N [<CV-Qualifiers>] [<ref-qualifier>] <prefix> <unqualified-name> E
3127// ::= N [<CV-Qualifiers>] [<ref-qualifier>] <template-prefix> <template-args> E
3128//
3129// <prefix> ::= <prefix> <unqualified-name>
3130// ::= <template-prefix> <template-args>
3131// ::= <template-param>
3132// ::= <decltype>
3133// ::= # empty
3134// ::= <substitution>
3135// ::= <prefix> <data-member-prefix>
3136// extension ::= L
3137//
3138// <data-member-prefix> := <member source-name> [<template-args>] M
3139//
3140// <template-prefix> ::= <prefix> <template unqualified-name>
3141// ::= <template-param>
3142// ::= <substitution>
Pavel Labathba825192018-10-16 14:29:14 +00003143template <typename Derived, typename Alloc>
3144Node *
3145AbstractManglingParser<Derived, Alloc>::parseNestedName(NameState *State) {
Richard Smithc20d1442018-08-20 20:14:49 +00003146 if (!consumeIf('N'))
3147 return nullptr;
3148
3149 Qualifiers CVTmp = parseCVQualifiers();
3150 if (State) State->CVQualifiers = CVTmp;
3151
3152 if (consumeIf('O')) {
3153 if (State) State->ReferenceQualifier = FrefQualRValue;
3154 } else if (consumeIf('R')) {
3155 if (State) State->ReferenceQualifier = FrefQualLValue;
3156 } else
3157 if (State) State->ReferenceQualifier = FrefQualNone;
3158
3159 Node *SoFar = nullptr;
3160 auto PushComponent = [&](Node *Comp) {
Richard Smithb485b352018-08-24 23:30:26 +00003161 if (!Comp) return false;
Richard Smithc20d1442018-08-20 20:14:49 +00003162 if (SoFar) SoFar = make<NestedName>(SoFar, Comp);
3163 else SoFar = Comp;
3164 if (State) State->EndsWithTemplateArgs = false;
Richard Smithb485b352018-08-24 23:30:26 +00003165 return SoFar != nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00003166 };
3167
Richard Smithb485b352018-08-24 23:30:26 +00003168 if (consumeIf("St")) {
Richard Smithc20d1442018-08-20 20:14:49 +00003169 SoFar = make<NameType>("std");
Richard Smithb485b352018-08-24 23:30:26 +00003170 if (!SoFar)
3171 return nullptr;
3172 }
Richard Smithc20d1442018-08-20 20:14:49 +00003173
3174 while (!consumeIf('E')) {
3175 consumeIf('L'); // extension
3176
3177 // <data-member-prefix> := <member source-name> [<template-args>] M
3178 if (consumeIf('M')) {
3179 if (SoFar == nullptr)
3180 return nullptr;
3181 continue;
3182 }
3183
3184 // ::= <template-param>
3185 if (look() == 'T') {
Pavel Labathba825192018-10-16 14:29:14 +00003186 if (!PushComponent(getDerived().parseTemplateParam()))
Richard Smithc20d1442018-08-20 20:14:49 +00003187 return nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00003188 Subs.push_back(SoFar);
3189 continue;
3190 }
3191
3192 // ::= <template-prefix> <template-args>
3193 if (look() == 'I') {
Pavel Labathba825192018-10-16 14:29:14 +00003194 Node *TA = getDerived().parseTemplateArgs(State != nullptr);
Richard Smithc20d1442018-08-20 20:14:49 +00003195 if (TA == nullptr || SoFar == nullptr)
3196 return nullptr;
3197 SoFar = make<NameWithTemplateArgs>(SoFar, TA);
Richard Smithb485b352018-08-24 23:30:26 +00003198 if (!SoFar)
3199 return nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00003200 if (State) State->EndsWithTemplateArgs = true;
3201 Subs.push_back(SoFar);
3202 continue;
3203 }
3204
3205 // ::= <decltype>
3206 if (look() == 'D' && (look(1) == 't' || look(1) == 'T')) {
Pavel Labathba825192018-10-16 14:29:14 +00003207 if (!PushComponent(getDerived().parseDecltype()))
Richard Smithc20d1442018-08-20 20:14:49 +00003208 return nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00003209 Subs.push_back(SoFar);
3210 continue;
3211 }
3212
3213 // ::= <substitution>
3214 if (look() == 'S' && look(1) != 't') {
Pavel Labathba825192018-10-16 14:29:14 +00003215 Node *S = getDerived().parseSubstitution();
Richard Smithb485b352018-08-24 23:30:26 +00003216 if (!PushComponent(S))
Richard Smithc20d1442018-08-20 20:14:49 +00003217 return nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00003218 if (SoFar != S)
3219 Subs.push_back(S);
3220 continue;
3221 }
3222
3223 // Parse an <unqualified-name> thats actually a <ctor-dtor-name>.
3224 if (look() == 'C' || (look() == 'D' && look(1) != 'C')) {
3225 if (SoFar == nullptr)
3226 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00003227 if (!PushComponent(getDerived().parseCtorDtorName(SoFar, State)))
Richard Smithc20d1442018-08-20 20:14:49 +00003228 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00003229 SoFar = getDerived().parseAbiTags(SoFar);
Richard Smithc20d1442018-08-20 20:14:49 +00003230 if (SoFar == nullptr)
3231 return nullptr;
3232 Subs.push_back(SoFar);
3233 continue;
3234 }
3235
3236 // ::= <prefix> <unqualified-name>
Pavel Labathba825192018-10-16 14:29:14 +00003237 if (!PushComponent(getDerived().parseUnqualifiedName(State)))
Richard Smithc20d1442018-08-20 20:14:49 +00003238 return nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00003239 Subs.push_back(SoFar);
3240 }
3241
3242 if (SoFar == nullptr || Subs.empty())
3243 return nullptr;
3244
3245 Subs.pop_back();
3246 return SoFar;
3247}
3248
3249// <simple-id> ::= <source-name> [ <template-args> ]
Pavel Labathba825192018-10-16 14:29:14 +00003250template <typename Derived, typename Alloc>
3251Node *AbstractManglingParser<Derived, Alloc>::parseSimpleId() {
3252 Node *SN = getDerived().parseSourceName(/*NameState=*/nullptr);
Richard Smithc20d1442018-08-20 20:14:49 +00003253 if (SN == nullptr)
3254 return nullptr;
3255 if (look() == 'I') {
Pavel Labathba825192018-10-16 14:29:14 +00003256 Node *TA = getDerived().parseTemplateArgs();
Richard Smithc20d1442018-08-20 20:14:49 +00003257 if (TA == nullptr)
3258 return nullptr;
3259 return make<NameWithTemplateArgs>(SN, TA);
3260 }
3261 return SN;
3262}
3263
3264// <destructor-name> ::= <unresolved-type> # e.g., ~T or ~decltype(f())
3265// ::= <simple-id> # e.g., ~A<2*N>
Pavel Labathba825192018-10-16 14:29:14 +00003266template <typename Derived, typename Alloc>
3267Node *AbstractManglingParser<Derived, Alloc>::parseDestructorName() {
Richard Smithc20d1442018-08-20 20:14:49 +00003268 Node *Result;
3269 if (std::isdigit(look()))
Pavel Labathba825192018-10-16 14:29:14 +00003270 Result = getDerived().parseSimpleId();
Richard Smithc20d1442018-08-20 20:14:49 +00003271 else
Pavel Labathba825192018-10-16 14:29:14 +00003272 Result = getDerived().parseUnresolvedType();
Richard Smithc20d1442018-08-20 20:14:49 +00003273 if (Result == nullptr)
3274 return nullptr;
3275 return make<DtorName>(Result);
3276}
3277
3278// <unresolved-type> ::= <template-param>
3279// ::= <decltype>
3280// ::= <substitution>
Pavel Labathba825192018-10-16 14:29:14 +00003281template <typename Derived, typename Alloc>
3282Node *AbstractManglingParser<Derived, Alloc>::parseUnresolvedType() {
Richard Smithc20d1442018-08-20 20:14:49 +00003283 if (look() == 'T') {
Pavel Labathba825192018-10-16 14:29:14 +00003284 Node *TP = getDerived().parseTemplateParam();
Richard Smithc20d1442018-08-20 20:14:49 +00003285 if (TP == nullptr)
3286 return nullptr;
3287 Subs.push_back(TP);
3288 return TP;
3289 }
3290 if (look() == 'D') {
Pavel Labathba825192018-10-16 14:29:14 +00003291 Node *DT = getDerived().parseDecltype();
Richard Smithc20d1442018-08-20 20:14:49 +00003292 if (DT == nullptr)
3293 return nullptr;
3294 Subs.push_back(DT);
3295 return DT;
3296 }
Pavel Labathba825192018-10-16 14:29:14 +00003297 return getDerived().parseSubstitution();
Richard Smithc20d1442018-08-20 20:14:49 +00003298}
3299
3300// <base-unresolved-name> ::= <simple-id> # unresolved name
3301// extension ::= <operator-name> # unresolved operator-function-id
3302// extension ::= <operator-name> <template-args> # unresolved operator template-id
3303// ::= on <operator-name> # unresolved operator-function-id
3304// ::= on <operator-name> <template-args> # unresolved operator template-id
3305// ::= dn <destructor-name> # destructor or pseudo-destructor;
3306// # e.g. ~X or ~X<N-1>
Pavel Labathba825192018-10-16 14:29:14 +00003307template <typename Derived, typename Alloc>
3308Node *AbstractManglingParser<Derived, Alloc>::parseBaseUnresolvedName() {
Richard Smithc20d1442018-08-20 20:14:49 +00003309 if (std::isdigit(look()))
Pavel Labathba825192018-10-16 14:29:14 +00003310 return getDerived().parseSimpleId();
Richard Smithc20d1442018-08-20 20:14:49 +00003311
3312 if (consumeIf("dn"))
Pavel Labathba825192018-10-16 14:29:14 +00003313 return getDerived().parseDestructorName();
Richard Smithc20d1442018-08-20 20:14:49 +00003314
3315 consumeIf("on");
3316
Pavel Labathba825192018-10-16 14:29:14 +00003317 Node *Oper = getDerived().parseOperatorName(/*NameState=*/nullptr);
Richard Smithc20d1442018-08-20 20:14:49 +00003318 if (Oper == nullptr)
3319 return nullptr;
3320 if (look() == 'I') {
Pavel Labathba825192018-10-16 14:29:14 +00003321 Node *TA = getDerived().parseTemplateArgs();
Richard Smithc20d1442018-08-20 20:14:49 +00003322 if (TA == nullptr)
3323 return nullptr;
3324 return make<NameWithTemplateArgs>(Oper, TA);
3325 }
3326 return Oper;
3327}
3328
3329// <unresolved-name>
3330// extension ::= srN <unresolved-type> [<template-args>] <unresolved-qualifier-level>* E <base-unresolved-name>
3331// ::= [gs] <base-unresolved-name> # x or (with "gs") ::x
3332// ::= [gs] sr <unresolved-qualifier-level>+ E <base-unresolved-name>
3333// # A::x, N::y, A<T>::z; "gs" means leading "::"
3334// ::= sr <unresolved-type> <base-unresolved-name> # T::x / decltype(p)::x
3335// extension ::= sr <unresolved-type> <template-args> <base-unresolved-name>
3336// # T::N::x /decltype(p)::N::x
3337// (ignored) ::= srN <unresolved-type> <unresolved-qualifier-level>+ E <base-unresolved-name>
3338//
3339// <unresolved-qualifier-level> ::= <simple-id>
Pavel Labathba825192018-10-16 14:29:14 +00003340template <typename Derived, typename Alloc>
3341Node *AbstractManglingParser<Derived, Alloc>::parseUnresolvedName() {
Richard Smithc20d1442018-08-20 20:14:49 +00003342 Node *SoFar = nullptr;
3343
3344 // srN <unresolved-type> [<template-args>] <unresolved-qualifier-level>* E <base-unresolved-name>
3345 // srN <unresolved-type> <unresolved-qualifier-level>+ E <base-unresolved-name>
3346 if (consumeIf("srN")) {
Pavel Labathba825192018-10-16 14:29:14 +00003347 SoFar = getDerived().parseUnresolvedType();
Richard Smithc20d1442018-08-20 20:14:49 +00003348 if (SoFar == nullptr)
3349 return nullptr;
3350
3351 if (look() == 'I') {
Pavel Labathba825192018-10-16 14:29:14 +00003352 Node *TA = getDerived().parseTemplateArgs();
Richard Smithc20d1442018-08-20 20:14:49 +00003353 if (TA == nullptr)
3354 return nullptr;
3355 SoFar = make<NameWithTemplateArgs>(SoFar, TA);
Richard Smithb485b352018-08-24 23:30:26 +00003356 if (!SoFar)
3357 return nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00003358 }
3359
3360 while (!consumeIf('E')) {
Pavel Labathba825192018-10-16 14:29:14 +00003361 Node *Qual = getDerived().parseSimpleId();
Richard Smithc20d1442018-08-20 20:14:49 +00003362 if (Qual == nullptr)
3363 return nullptr;
3364 SoFar = make<QualifiedName>(SoFar, Qual);
Richard Smithb485b352018-08-24 23:30:26 +00003365 if (!SoFar)
3366 return nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00003367 }
3368
Pavel Labathba825192018-10-16 14:29:14 +00003369 Node *Base = getDerived().parseBaseUnresolvedName();
Richard Smithc20d1442018-08-20 20:14:49 +00003370 if (Base == nullptr)
3371 return nullptr;
3372 return make<QualifiedName>(SoFar, Base);
3373 }
3374
3375 bool Global = consumeIf("gs");
3376
3377 // [gs] <base-unresolved-name> # x or (with "gs") ::x
3378 if (!consumeIf("sr")) {
Pavel Labathba825192018-10-16 14:29:14 +00003379 SoFar = getDerived().parseBaseUnresolvedName();
Richard Smithc20d1442018-08-20 20:14:49 +00003380 if (SoFar == nullptr)
3381 return nullptr;
3382 if (Global)
3383 SoFar = make<GlobalQualifiedName>(SoFar);
3384 return SoFar;
3385 }
3386
3387 // [gs] sr <unresolved-qualifier-level>+ E <base-unresolved-name>
3388 if (std::isdigit(look())) {
3389 do {
Pavel Labathba825192018-10-16 14:29:14 +00003390 Node *Qual = getDerived().parseSimpleId();
Richard Smithc20d1442018-08-20 20:14:49 +00003391 if (Qual == nullptr)
3392 return nullptr;
3393 if (SoFar)
3394 SoFar = make<QualifiedName>(SoFar, Qual);
3395 else if (Global)
3396 SoFar = make<GlobalQualifiedName>(Qual);
3397 else
3398 SoFar = Qual;
Richard Smithb485b352018-08-24 23:30:26 +00003399 if (!SoFar)
3400 return nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00003401 } while (!consumeIf('E'));
3402 }
3403 // sr <unresolved-type> <base-unresolved-name>
3404 // sr <unresolved-type> <template-args> <base-unresolved-name>
3405 else {
Pavel Labathba825192018-10-16 14:29:14 +00003406 SoFar = getDerived().parseUnresolvedType();
Richard Smithc20d1442018-08-20 20:14:49 +00003407 if (SoFar == nullptr)
3408 return nullptr;
3409
3410 if (look() == 'I') {
Pavel Labathba825192018-10-16 14:29:14 +00003411 Node *TA = getDerived().parseTemplateArgs();
Richard Smithc20d1442018-08-20 20:14:49 +00003412 if (TA == nullptr)
3413 return nullptr;
3414 SoFar = make<NameWithTemplateArgs>(SoFar, TA);
Richard Smithb485b352018-08-24 23:30:26 +00003415 if (!SoFar)
3416 return nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00003417 }
3418 }
3419
3420 assert(SoFar != nullptr);
3421
Pavel Labathba825192018-10-16 14:29:14 +00003422 Node *Base = getDerived().parseBaseUnresolvedName();
Richard Smithc20d1442018-08-20 20:14:49 +00003423 if (Base == nullptr)
3424 return nullptr;
3425 return make<QualifiedName>(SoFar, Base);
3426}
3427
3428// <abi-tags> ::= <abi-tag> [<abi-tags>]
3429// <abi-tag> ::= B <source-name>
Pavel Labathba825192018-10-16 14:29:14 +00003430template <typename Derived, typename Alloc>
3431Node *AbstractManglingParser<Derived, Alloc>::parseAbiTags(Node *N) {
Richard Smithc20d1442018-08-20 20:14:49 +00003432 while (consumeIf('B')) {
3433 StringView SN = parseBareSourceName();
3434 if (SN.empty())
3435 return nullptr;
3436 N = make<AbiTagAttr>(N, SN);
Richard Smithb485b352018-08-24 23:30:26 +00003437 if (!N)
3438 return nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00003439 }
3440 return N;
3441}
3442
3443// <number> ::= [n] <non-negative decimal integer>
Pavel Labathba825192018-10-16 14:29:14 +00003444template <typename Alloc, typename Derived>
3445StringView
3446AbstractManglingParser<Alloc, Derived>::parseNumber(bool AllowNegative) {
Richard Smithc20d1442018-08-20 20:14:49 +00003447 const char *Tmp = First;
3448 if (AllowNegative)
3449 consumeIf('n');
3450 if (numLeft() == 0 || !std::isdigit(*First))
3451 return StringView();
3452 while (numLeft() != 0 && std::isdigit(*First))
3453 ++First;
3454 return StringView(Tmp, First);
3455}
3456
3457// <positive length number> ::= [0-9]*
Pavel Labathba825192018-10-16 14:29:14 +00003458template <typename Alloc, typename Derived>
3459bool AbstractManglingParser<Alloc, Derived>::parsePositiveInteger(size_t *Out) {
Richard Smithc20d1442018-08-20 20:14:49 +00003460 *Out = 0;
3461 if (look() < '0' || look() > '9')
3462 return true;
3463 while (look() >= '0' && look() <= '9') {
3464 *Out *= 10;
3465 *Out += static_cast<size_t>(consume() - '0');
3466 }
3467 return false;
3468}
3469
Pavel Labathba825192018-10-16 14:29:14 +00003470template <typename Alloc, typename Derived>
3471StringView AbstractManglingParser<Alloc, Derived>::parseBareSourceName() {
Richard Smithc20d1442018-08-20 20:14:49 +00003472 size_t Int = 0;
3473 if (parsePositiveInteger(&Int) || numLeft() < Int)
3474 return StringView();
3475 StringView R(First, First + Int);
3476 First += Int;
3477 return R;
3478}
3479
3480// <function-type> ::= [<CV-qualifiers>] [<exception-spec>] [Dx] F [Y] <bare-function-type> [<ref-qualifier>] E
3481//
3482// <exception-spec> ::= Do # non-throwing exception-specification (e.g., noexcept, throw())
3483// ::= DO <expression> E # computed (instantiation-dependent) noexcept
3484// ::= Dw <type>+ E # dynamic exception specification with instantiation-dependent types
3485//
3486// <ref-qualifier> ::= R # & ref-qualifier
3487// <ref-qualifier> ::= O # && ref-qualifier
Pavel Labathba825192018-10-16 14:29:14 +00003488template <typename Derived, typename Alloc>
3489Node *AbstractManglingParser<Derived, Alloc>::parseFunctionType() {
Richard Smithc20d1442018-08-20 20:14:49 +00003490 Qualifiers CVQuals = parseCVQualifiers();
3491
3492 Node *ExceptionSpec = nullptr;
3493 if (consumeIf("Do")) {
3494 ExceptionSpec = make<NameType>("noexcept");
Richard Smithb485b352018-08-24 23:30:26 +00003495 if (!ExceptionSpec)
3496 return nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00003497 } else if (consumeIf("DO")) {
Pavel Labathba825192018-10-16 14:29:14 +00003498 Node *E = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00003499 if (E == nullptr || !consumeIf('E'))
3500 return nullptr;
3501 ExceptionSpec = make<NoexceptSpec>(E);
Richard Smithb485b352018-08-24 23:30:26 +00003502 if (!ExceptionSpec)
3503 return nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00003504 } else if (consumeIf("Dw")) {
3505 size_t SpecsBegin = Names.size();
3506 while (!consumeIf('E')) {
Pavel Labathba825192018-10-16 14:29:14 +00003507 Node *T = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00003508 if (T == nullptr)
3509 return nullptr;
3510 Names.push_back(T);
3511 }
3512 ExceptionSpec =
3513 make<DynamicExceptionSpec>(popTrailingNodeArray(SpecsBegin));
Richard Smithb485b352018-08-24 23:30:26 +00003514 if (!ExceptionSpec)
3515 return nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00003516 }
3517
3518 consumeIf("Dx"); // transaction safe
3519
3520 if (!consumeIf('F'))
3521 return nullptr;
3522 consumeIf('Y'); // extern "C"
Pavel Labathba825192018-10-16 14:29:14 +00003523 Node *ReturnType = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00003524 if (ReturnType == nullptr)
3525 return nullptr;
3526
3527 FunctionRefQual ReferenceQualifier = FrefQualNone;
3528 size_t ParamsBegin = Names.size();
3529 while (true) {
3530 if (consumeIf('E'))
3531 break;
3532 if (consumeIf('v'))
3533 continue;
3534 if (consumeIf("RE")) {
3535 ReferenceQualifier = FrefQualLValue;
3536 break;
3537 }
3538 if (consumeIf("OE")) {
3539 ReferenceQualifier = FrefQualRValue;
3540 break;
3541 }
Pavel Labathba825192018-10-16 14:29:14 +00003542 Node *T = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00003543 if (T == nullptr)
3544 return nullptr;
3545 Names.push_back(T);
3546 }
3547
3548 NodeArray Params = popTrailingNodeArray(ParamsBegin);
3549 return make<FunctionType>(ReturnType, Params, CVQuals,
3550 ReferenceQualifier, ExceptionSpec);
3551}
3552
3553// extension:
3554// <vector-type> ::= Dv <positive dimension number> _ <extended element type>
3555// ::= Dv [<dimension expression>] _ <element type>
3556// <extended element type> ::= <element type>
3557// ::= p # AltiVec vector pixel
Pavel Labathba825192018-10-16 14:29:14 +00003558template <typename Derived, typename Alloc>
3559Node *AbstractManglingParser<Derived, Alloc>::parseVectorType() {
Richard Smithc20d1442018-08-20 20:14:49 +00003560 if (!consumeIf("Dv"))
3561 return nullptr;
3562 if (look() >= '1' && look() <= '9') {
Erik Pilkingtond7555e32019-11-04 10:47:44 -08003563 Node *DimensionNumber = make<NameType>(parseNumber());
3564 if (!DimensionNumber)
3565 return nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00003566 if (!consumeIf('_'))
3567 return nullptr;
3568 if (consumeIf('p'))
3569 return make<PixelVectorType>(DimensionNumber);
Pavel Labathba825192018-10-16 14:29:14 +00003570 Node *ElemType = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00003571 if (ElemType == nullptr)
3572 return nullptr;
3573 return make<VectorType>(ElemType, DimensionNumber);
3574 }
3575
3576 if (!consumeIf('_')) {
Pavel Labathba825192018-10-16 14:29:14 +00003577 Node *DimExpr = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00003578 if (!DimExpr)
3579 return nullptr;
3580 if (!consumeIf('_'))
3581 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00003582 Node *ElemType = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00003583 if (!ElemType)
3584 return nullptr;
3585 return make<VectorType>(ElemType, DimExpr);
3586 }
Pavel Labathba825192018-10-16 14:29:14 +00003587 Node *ElemType = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00003588 if (!ElemType)
3589 return nullptr;
Erik Pilkingtond7555e32019-11-04 10:47:44 -08003590 return make<VectorType>(ElemType, /*Dimension=*/nullptr);
Richard Smithc20d1442018-08-20 20:14:49 +00003591}
3592
3593// <decltype> ::= Dt <expression> E # decltype of an id-expression or class member access (C++0x)
3594// ::= DT <expression> E # decltype of an expression (C++0x)
Pavel Labathba825192018-10-16 14:29:14 +00003595template <typename Derived, typename Alloc>
3596Node *AbstractManglingParser<Derived, Alloc>::parseDecltype() {
Richard Smithc20d1442018-08-20 20:14:49 +00003597 if (!consumeIf('D'))
3598 return nullptr;
3599 if (!consumeIf('t') && !consumeIf('T'))
3600 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00003601 Node *E = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00003602 if (E == nullptr)
3603 return nullptr;
3604 if (!consumeIf('E'))
3605 return nullptr;
3606 return make<EnclosingExpr>("decltype(", E, ")");
3607}
3608
3609// <array-type> ::= A <positive dimension number> _ <element type>
3610// ::= A [<dimension expression>] _ <element type>
Pavel Labathba825192018-10-16 14:29:14 +00003611template <typename Derived, typename Alloc>
3612Node *AbstractManglingParser<Derived, Alloc>::parseArrayType() {
Richard Smithc20d1442018-08-20 20:14:49 +00003613 if (!consumeIf('A'))
3614 return nullptr;
3615
Erik Pilkingtond7555e32019-11-04 10:47:44 -08003616 Node *Dimension = nullptr;
Pavel Labathf4e67eb2018-10-10 08:39:16 +00003617
Richard Smithc20d1442018-08-20 20:14:49 +00003618 if (std::isdigit(look())) {
Erik Pilkingtond7555e32019-11-04 10:47:44 -08003619 Dimension = make<NameType>(parseNumber());
3620 if (!Dimension)
3621 return nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00003622 if (!consumeIf('_'))
3623 return nullptr;
Pavel Labathf4e67eb2018-10-10 08:39:16 +00003624 } else if (!consumeIf('_')) {
Pavel Labathba825192018-10-16 14:29:14 +00003625 Node *DimExpr = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00003626 if (DimExpr == nullptr)
3627 return nullptr;
3628 if (!consumeIf('_'))
3629 return nullptr;
Pavel Labathf4e67eb2018-10-10 08:39:16 +00003630 Dimension = DimExpr;
Richard Smithc20d1442018-08-20 20:14:49 +00003631 }
3632
Pavel Labathba825192018-10-16 14:29:14 +00003633 Node *Ty = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00003634 if (Ty == nullptr)
3635 return nullptr;
Pavel Labathf4e67eb2018-10-10 08:39:16 +00003636 return make<ArrayType>(Ty, Dimension);
Richard Smithc20d1442018-08-20 20:14:49 +00003637}
3638
3639// <pointer-to-member-type> ::= M <class type> <member type>
Pavel Labathba825192018-10-16 14:29:14 +00003640template <typename Derived, typename Alloc>
3641Node *AbstractManglingParser<Derived, Alloc>::parsePointerToMemberType() {
Richard Smithc20d1442018-08-20 20:14:49 +00003642 if (!consumeIf('M'))
3643 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00003644 Node *ClassType = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00003645 if (ClassType == nullptr)
3646 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00003647 Node *MemberType = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00003648 if (MemberType == nullptr)
3649 return nullptr;
3650 return make<PointerToMemberType>(ClassType, MemberType);
3651}
3652
3653// <class-enum-type> ::= <name> # non-dependent type name, dependent type name, or dependent typename-specifier
3654// ::= Ts <name> # dependent elaborated type specifier using 'struct' or 'class'
3655// ::= Tu <name> # dependent elaborated type specifier using 'union'
3656// ::= Te <name> # dependent elaborated type specifier using 'enum'
Pavel Labathba825192018-10-16 14:29:14 +00003657template <typename Derived, typename Alloc>
3658Node *AbstractManglingParser<Derived, Alloc>::parseClassEnumType() {
Richard Smithc20d1442018-08-20 20:14:49 +00003659 StringView ElabSpef;
3660 if (consumeIf("Ts"))
3661 ElabSpef = "struct";
3662 else if (consumeIf("Tu"))
3663 ElabSpef = "union";
3664 else if (consumeIf("Te"))
3665 ElabSpef = "enum";
3666
Pavel Labathba825192018-10-16 14:29:14 +00003667 Node *Name = getDerived().parseName();
Richard Smithc20d1442018-08-20 20:14:49 +00003668 if (Name == nullptr)
3669 return nullptr;
3670
3671 if (!ElabSpef.empty())
3672 return make<ElaboratedTypeSpefType>(ElabSpef, Name);
3673
3674 return Name;
3675}
3676
3677// <qualified-type> ::= <qualifiers> <type>
3678// <qualifiers> ::= <extended-qualifier>* <CV-qualifiers>
3679// <extended-qualifier> ::= U <source-name> [<template-args>] # vendor extended type qualifier
Pavel Labathba825192018-10-16 14:29:14 +00003680template <typename Derived, typename Alloc>
3681Node *AbstractManglingParser<Derived, Alloc>::parseQualifiedType() {
Richard Smithc20d1442018-08-20 20:14:49 +00003682 if (consumeIf('U')) {
3683 StringView Qual = parseBareSourceName();
3684 if (Qual.empty())
3685 return nullptr;
3686
Richard Smithc20d1442018-08-20 20:14:49 +00003687 // extension ::= U <objc-name> <objc-type> # objc-type<identifier>
3688 if (Qual.startsWith("objcproto")) {
3689 StringView ProtoSourceName = Qual.dropFront(std::strlen("objcproto"));
3690 StringView Proto;
3691 {
3692 SwapAndRestore<const char *> SaveFirst(First, ProtoSourceName.begin()),
3693 SaveLast(Last, ProtoSourceName.end());
3694 Proto = parseBareSourceName();
3695 }
3696 if (Proto.empty())
3697 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00003698 Node *Child = getDerived().parseQualifiedType();
Richard Smithc20d1442018-08-20 20:14:49 +00003699 if (Child == nullptr)
3700 return nullptr;
3701 return make<ObjCProtoName>(Child, Proto);
3702 }
3703
Alex Orlovf50df922021-03-24 10:21:32 +04003704 Node *TA = nullptr;
3705 if (look() == 'I') {
3706 TA = getDerived().parseTemplateArgs();
3707 if (TA == nullptr)
3708 return nullptr;
3709 }
3710
Pavel Labathba825192018-10-16 14:29:14 +00003711 Node *Child = getDerived().parseQualifiedType();
Richard Smithc20d1442018-08-20 20:14:49 +00003712 if (Child == nullptr)
3713 return nullptr;
Alex Orlovf50df922021-03-24 10:21:32 +04003714 return make<VendorExtQualType>(Child, Qual, TA);
Richard Smithc20d1442018-08-20 20:14:49 +00003715 }
3716
3717 Qualifiers Quals = parseCVQualifiers();
Pavel Labathba825192018-10-16 14:29:14 +00003718 Node *Ty = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00003719 if (Ty == nullptr)
3720 return nullptr;
3721 if (Quals != QualNone)
3722 Ty = make<QualType>(Ty, Quals);
3723 return Ty;
3724}
3725
3726// <type> ::= <builtin-type>
3727// ::= <qualified-type>
3728// ::= <function-type>
3729// ::= <class-enum-type>
3730// ::= <array-type>
3731// ::= <pointer-to-member-type>
3732// ::= <template-param>
3733// ::= <template-template-param> <template-args>
3734// ::= <decltype>
3735// ::= P <type> # pointer
3736// ::= R <type> # l-value reference
3737// ::= O <type> # r-value reference (C++11)
3738// ::= C <type> # complex pair (C99)
3739// ::= G <type> # imaginary (C99)
3740// ::= <substitution> # See Compression below
3741// extension ::= U <objc-name> <objc-type> # objc-type<identifier>
3742// extension ::= <vector-type> # <vector-type> starts with Dv
3743//
3744// <objc-name> ::= <k0 number> objcproto <k1 number> <identifier> # k0 = 9 + <number of digits in k1> + k1
3745// <objc-type> ::= <source-name> # PU<11+>objcproto 11objc_object<source-name> 11objc_object -> id<source-name>
Pavel Labathba825192018-10-16 14:29:14 +00003746template <typename Derived, typename Alloc>
3747Node *AbstractManglingParser<Derived, Alloc>::parseType() {
Richard Smithc20d1442018-08-20 20:14:49 +00003748 Node *Result = nullptr;
3749
Richard Smithc20d1442018-08-20 20:14:49 +00003750 switch (look()) {
3751 // ::= <qualified-type>
3752 case 'r':
3753 case 'V':
3754 case 'K': {
3755 unsigned AfterQuals = 0;
3756 if (look(AfterQuals) == 'r') ++AfterQuals;
3757 if (look(AfterQuals) == 'V') ++AfterQuals;
3758 if (look(AfterQuals) == 'K') ++AfterQuals;
3759
3760 if (look(AfterQuals) == 'F' ||
3761 (look(AfterQuals) == 'D' &&
3762 (look(AfterQuals + 1) == 'o' || look(AfterQuals + 1) == 'O' ||
3763 look(AfterQuals + 1) == 'w' || look(AfterQuals + 1) == 'x'))) {
Pavel Labathba825192018-10-16 14:29:14 +00003764 Result = getDerived().parseFunctionType();
Richard Smithc20d1442018-08-20 20:14:49 +00003765 break;
3766 }
Erik Pilkingtonf70e4d82019-01-17 20:37:51 +00003767 DEMANGLE_FALLTHROUGH;
Richard Smithc20d1442018-08-20 20:14:49 +00003768 }
3769 case 'U': {
Pavel Labathba825192018-10-16 14:29:14 +00003770 Result = getDerived().parseQualifiedType();
Richard Smithc20d1442018-08-20 20:14:49 +00003771 break;
3772 }
3773 // <builtin-type> ::= v # void
3774 case 'v':
3775 ++First;
3776 return make<NameType>("void");
3777 // ::= w # wchar_t
3778 case 'w':
3779 ++First;
3780 return make<NameType>("wchar_t");
3781 // ::= b # bool
3782 case 'b':
3783 ++First;
3784 return make<NameType>("bool");
3785 // ::= c # char
3786 case 'c':
3787 ++First;
3788 return make<NameType>("char");
3789 // ::= a # signed char
3790 case 'a':
3791 ++First;
3792 return make<NameType>("signed char");
3793 // ::= h # unsigned char
3794 case 'h':
3795 ++First;
3796 return make<NameType>("unsigned char");
3797 // ::= s # short
3798 case 's':
3799 ++First;
3800 return make<NameType>("short");
3801 // ::= t # unsigned short
3802 case 't':
3803 ++First;
3804 return make<NameType>("unsigned short");
3805 // ::= i # int
3806 case 'i':
3807 ++First;
3808 return make<NameType>("int");
3809 // ::= j # unsigned int
3810 case 'j':
3811 ++First;
3812 return make<NameType>("unsigned int");
3813 // ::= l # long
3814 case 'l':
3815 ++First;
3816 return make<NameType>("long");
3817 // ::= m # unsigned long
3818 case 'm':
3819 ++First;
3820 return make<NameType>("unsigned long");
3821 // ::= x # long long, __int64
3822 case 'x':
3823 ++First;
3824 return make<NameType>("long long");
3825 // ::= y # unsigned long long, __int64
3826 case 'y':
3827 ++First;
3828 return make<NameType>("unsigned long long");
3829 // ::= n # __int128
3830 case 'n':
3831 ++First;
3832 return make<NameType>("__int128");
3833 // ::= o # unsigned __int128
3834 case 'o':
3835 ++First;
3836 return make<NameType>("unsigned __int128");
3837 // ::= f # float
3838 case 'f':
3839 ++First;
3840 return make<NameType>("float");
3841 // ::= d # double
3842 case 'd':
3843 ++First;
3844 return make<NameType>("double");
3845 // ::= e # long double, __float80
3846 case 'e':
3847 ++First;
3848 return make<NameType>("long double");
3849 // ::= g # __float128
3850 case 'g':
3851 ++First;
3852 return make<NameType>("__float128");
3853 // ::= z # ellipsis
3854 case 'z':
3855 ++First;
3856 return make<NameType>("...");
3857
3858 // <builtin-type> ::= u <source-name> # vendor extended type
3859 case 'u': {
3860 ++First;
3861 StringView Res = parseBareSourceName();
3862 if (Res.empty())
3863 return nullptr;
Erik Pilkingtonb94a1f42019-06-10 21:02:39 +00003864 // Typically, <builtin-type>s are not considered substitution candidates,
3865 // but the exception to that exception is vendor extended types (Itanium C++
3866 // ABI 5.9.1).
3867 Result = make<NameType>(Res);
3868 break;
Richard Smithc20d1442018-08-20 20:14:49 +00003869 }
3870 case 'D':
3871 switch (look(1)) {
3872 // ::= Dd # IEEE 754r decimal floating point (64 bits)
3873 case 'd':
3874 First += 2;
3875 return make<NameType>("decimal64");
3876 // ::= De # IEEE 754r decimal floating point (128 bits)
3877 case 'e':
3878 First += 2;
3879 return make<NameType>("decimal128");
3880 // ::= Df # IEEE 754r decimal floating point (32 bits)
3881 case 'f':
3882 First += 2;
3883 return make<NameType>("decimal32");
3884 // ::= Dh # IEEE 754r half-precision floating point (16 bits)
3885 case 'h':
3886 First += 2;
Stuart Bradye8bf5772021-06-07 16:30:22 +01003887 return make<NameType>("half");
Richard Smithc20d1442018-08-20 20:14:49 +00003888 // ::= Di # char32_t
3889 case 'i':
3890 First += 2;
3891 return make<NameType>("char32_t");
3892 // ::= Ds # char16_t
3893 case 's':
3894 First += 2;
3895 return make<NameType>("char16_t");
Erik Pilkingtonc3780e82019-06-28 19:54:19 +00003896 // ::= Du # char8_t (C++2a, not yet in the Itanium spec)
3897 case 'u':
3898 First += 2;
3899 return make<NameType>("char8_t");
Richard Smithc20d1442018-08-20 20:14:49 +00003900 // ::= Da # auto (in dependent new-expressions)
3901 case 'a':
3902 First += 2;
3903 return make<NameType>("auto");
3904 // ::= Dc # decltype(auto)
3905 case 'c':
3906 First += 2;
3907 return make<NameType>("decltype(auto)");
3908 // ::= Dn # std::nullptr_t (i.e., decltype(nullptr))
3909 case 'n':
3910 First += 2;
3911 return make<NameType>("std::nullptr_t");
3912
3913 // ::= <decltype>
3914 case 't':
3915 case 'T': {
Pavel Labathba825192018-10-16 14:29:14 +00003916 Result = getDerived().parseDecltype();
Richard Smithc20d1442018-08-20 20:14:49 +00003917 break;
3918 }
3919 // extension ::= <vector-type> # <vector-type> starts with Dv
3920 case 'v': {
Pavel Labathba825192018-10-16 14:29:14 +00003921 Result = getDerived().parseVectorType();
Richard Smithc20d1442018-08-20 20:14:49 +00003922 break;
3923 }
3924 // ::= Dp <type> # pack expansion (C++0x)
3925 case 'p': {
3926 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00003927 Node *Child = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00003928 if (!Child)
3929 return nullptr;
3930 Result = make<ParameterPackExpansion>(Child);
3931 break;
3932 }
3933 // Exception specifier on a function type.
3934 case 'o':
3935 case 'O':
3936 case 'w':
3937 // Transaction safe function type.
3938 case 'x':
Pavel Labathba825192018-10-16 14:29:14 +00003939 Result = getDerived().parseFunctionType();
Richard Smithc20d1442018-08-20 20:14:49 +00003940 break;
3941 }
3942 break;
3943 // ::= <function-type>
3944 case 'F': {
Pavel Labathba825192018-10-16 14:29:14 +00003945 Result = getDerived().parseFunctionType();
Richard Smithc20d1442018-08-20 20:14:49 +00003946 break;
3947 }
3948 // ::= <array-type>
3949 case 'A': {
Pavel Labathba825192018-10-16 14:29:14 +00003950 Result = getDerived().parseArrayType();
Richard Smithc20d1442018-08-20 20:14:49 +00003951 break;
3952 }
3953 // ::= <pointer-to-member-type>
3954 case 'M': {
Pavel Labathba825192018-10-16 14:29:14 +00003955 Result = getDerived().parsePointerToMemberType();
Richard Smithc20d1442018-08-20 20:14:49 +00003956 break;
3957 }
3958 // ::= <template-param>
3959 case 'T': {
3960 // This could be an elaborate type specifier on a <class-enum-type>.
3961 if (look(1) == 's' || look(1) == 'u' || look(1) == 'e') {
Pavel Labathba825192018-10-16 14:29:14 +00003962 Result = getDerived().parseClassEnumType();
Richard Smithc20d1442018-08-20 20:14:49 +00003963 break;
3964 }
3965
Pavel Labathba825192018-10-16 14:29:14 +00003966 Result = getDerived().parseTemplateParam();
Richard Smithc20d1442018-08-20 20:14:49 +00003967 if (Result == nullptr)
3968 return nullptr;
3969
3970 // Result could be either of:
3971 // <type> ::= <template-param>
3972 // <type> ::= <template-template-param> <template-args>
3973 //
3974 // <template-template-param> ::= <template-param>
3975 // ::= <substitution>
3976 //
3977 // If this is followed by some <template-args>, and we're permitted to
3978 // parse them, take the second production.
3979
3980 if (TryToParseTemplateArgs && look() == 'I') {
Pavel Labathba825192018-10-16 14:29:14 +00003981 Node *TA = getDerived().parseTemplateArgs();
Richard Smithc20d1442018-08-20 20:14:49 +00003982 if (TA == nullptr)
3983 return nullptr;
3984 Result = make<NameWithTemplateArgs>(Result, TA);
3985 }
3986 break;
3987 }
3988 // ::= P <type> # pointer
3989 case 'P': {
3990 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00003991 Node *Ptr = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00003992 if (Ptr == nullptr)
3993 return nullptr;
3994 Result = make<PointerType>(Ptr);
3995 break;
3996 }
3997 // ::= R <type> # l-value reference
3998 case 'R': {
3999 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00004000 Node *Ref = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00004001 if (Ref == nullptr)
4002 return nullptr;
4003 Result = make<ReferenceType>(Ref, ReferenceKind::LValue);
4004 break;
4005 }
4006 // ::= O <type> # r-value reference (C++11)
4007 case 'O': {
4008 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00004009 Node *Ref = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00004010 if (Ref == nullptr)
4011 return nullptr;
4012 Result = make<ReferenceType>(Ref, ReferenceKind::RValue);
4013 break;
4014 }
4015 // ::= C <type> # complex pair (C99)
4016 case 'C': {
4017 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00004018 Node *P = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00004019 if (P == nullptr)
4020 return nullptr;
4021 Result = make<PostfixQualifiedType>(P, " complex");
4022 break;
4023 }
4024 // ::= G <type> # imaginary (C99)
4025 case 'G': {
4026 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00004027 Node *P = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00004028 if (P == nullptr)
4029 return P;
4030 Result = make<PostfixQualifiedType>(P, " imaginary");
4031 break;
4032 }
4033 // ::= <substitution> # See Compression below
4034 case 'S': {
4035 if (look(1) && look(1) != 't') {
Pavel Labathba825192018-10-16 14:29:14 +00004036 Node *Sub = getDerived().parseSubstitution();
Richard Smithc20d1442018-08-20 20:14:49 +00004037 if (Sub == nullptr)
4038 return nullptr;
4039
4040 // Sub could be either of:
4041 // <type> ::= <substitution>
4042 // <type> ::= <template-template-param> <template-args>
4043 //
4044 // <template-template-param> ::= <template-param>
4045 // ::= <substitution>
4046 //
4047 // If this is followed by some <template-args>, and we're permitted to
4048 // parse them, take the second production.
4049
4050 if (TryToParseTemplateArgs && look() == 'I') {
Pavel Labathba825192018-10-16 14:29:14 +00004051 Node *TA = getDerived().parseTemplateArgs();
Richard Smithc20d1442018-08-20 20:14:49 +00004052 if (TA == nullptr)
4053 return nullptr;
4054 Result = make<NameWithTemplateArgs>(Sub, TA);
4055 break;
4056 }
4057
4058 // If all we parsed was a substitution, don't re-insert into the
4059 // substitution table.
4060 return Sub;
4061 }
Erik Pilkingtonf70e4d82019-01-17 20:37:51 +00004062 DEMANGLE_FALLTHROUGH;
Richard Smithc20d1442018-08-20 20:14:49 +00004063 }
4064 // ::= <class-enum-type>
4065 default: {
Pavel Labathba825192018-10-16 14:29:14 +00004066 Result = getDerived().parseClassEnumType();
Richard Smithc20d1442018-08-20 20:14:49 +00004067 break;
4068 }
4069 }
4070
4071 // If we parsed a type, insert it into the substitution table. Note that all
4072 // <builtin-type>s and <substitution>s have already bailed out, because they
4073 // don't get substitutions.
4074 if (Result != nullptr)
4075 Subs.push_back(Result);
4076 return Result;
4077}
4078
Pavel Labathba825192018-10-16 14:29:14 +00004079template <typename Derived, typename Alloc>
4080Node *AbstractManglingParser<Derived, Alloc>::parsePrefixExpr(StringView Kind) {
4081 Node *E = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004082 if (E == nullptr)
4083 return nullptr;
4084 return make<PrefixExpr>(Kind, E);
4085}
4086
Pavel Labathba825192018-10-16 14:29:14 +00004087template <typename Derived, typename Alloc>
4088Node *AbstractManglingParser<Derived, Alloc>::parseBinaryExpr(StringView Kind) {
4089 Node *LHS = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004090 if (LHS == nullptr)
4091 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00004092 Node *RHS = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004093 if (RHS == nullptr)
4094 return nullptr;
4095 return make<BinaryExpr>(LHS, Kind, RHS);
4096}
4097
Pavel Labathba825192018-10-16 14:29:14 +00004098template <typename Derived, typename Alloc>
4099Node *
4100AbstractManglingParser<Derived, Alloc>::parseIntegerLiteral(StringView Lit) {
Richard Smithc20d1442018-08-20 20:14:49 +00004101 StringView Tmp = parseNumber(true);
4102 if (!Tmp.empty() && consumeIf('E'))
4103 return make<IntegerLiteral>(Lit, Tmp);
4104 return nullptr;
4105}
4106
4107// <CV-Qualifiers> ::= [r] [V] [K]
Pavel Labathba825192018-10-16 14:29:14 +00004108template <typename Alloc, typename Derived>
4109Qualifiers AbstractManglingParser<Alloc, Derived>::parseCVQualifiers() {
Richard Smithc20d1442018-08-20 20:14:49 +00004110 Qualifiers CVR = QualNone;
4111 if (consumeIf('r'))
4112 CVR |= QualRestrict;
4113 if (consumeIf('V'))
4114 CVR |= QualVolatile;
4115 if (consumeIf('K'))
4116 CVR |= QualConst;
4117 return CVR;
4118}
4119
4120// <function-param> ::= fp <top-level CV-Qualifiers> _ # L == 0, first parameter
4121// ::= fp <top-level CV-Qualifiers> <parameter-2 non-negative number> _ # L == 0, second and later parameters
4122// ::= fL <L-1 non-negative number> p <top-level CV-Qualifiers> _ # L > 0, first parameter
4123// ::= 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 -04004124// ::= fpT # 'this' expression (not part of standard?)
Pavel Labathba825192018-10-16 14:29:14 +00004125template <typename Derived, typename Alloc>
4126Node *AbstractManglingParser<Derived, Alloc>::parseFunctionParam() {
Erik Pilkington91c24af2020-05-13 22:19:45 -04004127 if (consumeIf("fpT"))
4128 return make<NameType>("this");
Richard Smithc20d1442018-08-20 20:14:49 +00004129 if (consumeIf("fp")) {
4130 parseCVQualifiers();
4131 StringView Num = parseNumber();
4132 if (!consumeIf('_'))
4133 return nullptr;
4134 return make<FunctionParam>(Num);
4135 }
4136 if (consumeIf("fL")) {
4137 if (parseNumber().empty())
4138 return nullptr;
4139 if (!consumeIf('p'))
4140 return nullptr;
4141 parseCVQualifiers();
4142 StringView Num = parseNumber();
4143 if (!consumeIf('_'))
4144 return nullptr;
4145 return make<FunctionParam>(Num);
4146 }
4147 return nullptr;
4148}
4149
4150// [gs] nw <expression>* _ <type> E # new (expr-list) type
4151// [gs] nw <expression>* _ <type> <initializer> # new (expr-list) type (init)
4152// [gs] na <expression>* _ <type> E # new[] (expr-list) type
4153// [gs] na <expression>* _ <type> <initializer> # new[] (expr-list) type (init)
4154// <initializer> ::= pi <expression>* E # parenthesized initialization
Pavel Labathba825192018-10-16 14:29:14 +00004155template <typename Derived, typename Alloc>
4156Node *AbstractManglingParser<Derived, Alloc>::parseNewExpr() {
Richard Smithc20d1442018-08-20 20:14:49 +00004157 bool Global = consumeIf("gs");
4158 bool IsArray = look(1) == 'a';
4159 if (!consumeIf("nw") && !consumeIf("na"))
4160 return nullptr;
4161 size_t Exprs = Names.size();
4162 while (!consumeIf('_')) {
Pavel Labathba825192018-10-16 14:29:14 +00004163 Node *Ex = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004164 if (Ex == nullptr)
4165 return nullptr;
4166 Names.push_back(Ex);
4167 }
4168 NodeArray ExprList = popTrailingNodeArray(Exprs);
Pavel Labathba825192018-10-16 14:29:14 +00004169 Node *Ty = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00004170 if (Ty == nullptr)
4171 return Ty;
4172 if (consumeIf("pi")) {
4173 size_t InitsBegin = Names.size();
4174 while (!consumeIf('E')) {
Pavel Labathba825192018-10-16 14:29:14 +00004175 Node *Init = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004176 if (Init == nullptr)
4177 return Init;
4178 Names.push_back(Init);
4179 }
4180 NodeArray Inits = popTrailingNodeArray(InitsBegin);
4181 return make<NewExpr>(ExprList, Ty, Inits, Global, IsArray);
4182 } else if (!consumeIf('E'))
4183 return nullptr;
4184 return make<NewExpr>(ExprList, Ty, NodeArray(), Global, IsArray);
4185}
4186
4187// cv <type> <expression> # conversion with one argument
4188// cv <type> _ <expression>* E # conversion with a different number of arguments
Pavel Labathba825192018-10-16 14:29:14 +00004189template <typename Derived, typename Alloc>
4190Node *AbstractManglingParser<Derived, Alloc>::parseConversionExpr() {
Richard Smithc20d1442018-08-20 20:14:49 +00004191 if (!consumeIf("cv"))
4192 return nullptr;
4193 Node *Ty;
4194 {
4195 SwapAndRestore<bool> SaveTemp(TryToParseTemplateArgs, false);
Pavel Labathba825192018-10-16 14:29:14 +00004196 Ty = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00004197 }
4198
4199 if (Ty == nullptr)
4200 return nullptr;
4201
4202 if (consumeIf('_')) {
4203 size_t ExprsBegin = Names.size();
4204 while (!consumeIf('E')) {
Pavel Labathba825192018-10-16 14:29:14 +00004205 Node *E = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004206 if (E == nullptr)
4207 return E;
4208 Names.push_back(E);
4209 }
4210 NodeArray Exprs = popTrailingNodeArray(ExprsBegin);
4211 return make<ConversionExpr>(Ty, Exprs);
4212 }
4213
Pavel Labathba825192018-10-16 14:29:14 +00004214 Node *E[1] = {getDerived().parseExpr()};
Richard Smithc20d1442018-08-20 20:14:49 +00004215 if (E[0] == nullptr)
4216 return nullptr;
4217 return make<ConversionExpr>(Ty, makeNodeArray(E, E + 1));
4218}
4219
4220// <expr-primary> ::= L <type> <value number> E # integer literal
4221// ::= L <type> <value float> E # floating literal
4222// ::= L <string type> E # string literal
4223// ::= L <nullptr type> E # nullptr literal (i.e., "LDnE")
Richard Smithdf1c14c2019-09-06 23:53:21 +00004224// ::= L <lambda type> E # lambda expression
Richard Smithc20d1442018-08-20 20:14:49 +00004225// FIXME: ::= L <type> <real-part float> _ <imag-part float> E # complex floating point literal (C 2000)
4226// ::= L <mangled-name> E # external name
Pavel Labathba825192018-10-16 14:29:14 +00004227template <typename Derived, typename Alloc>
4228Node *AbstractManglingParser<Derived, Alloc>::parseExprPrimary() {
Richard Smithc20d1442018-08-20 20:14:49 +00004229 if (!consumeIf('L'))
4230 return nullptr;
4231 switch (look()) {
4232 case 'w':
4233 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00004234 return getDerived().parseIntegerLiteral("wchar_t");
Richard Smithc20d1442018-08-20 20:14:49 +00004235 case 'b':
4236 if (consumeIf("b0E"))
4237 return make<BoolExpr>(0);
4238 if (consumeIf("b1E"))
4239 return make<BoolExpr>(1);
4240 return nullptr;
4241 case 'c':
4242 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00004243 return getDerived().parseIntegerLiteral("char");
Richard Smithc20d1442018-08-20 20:14:49 +00004244 case 'a':
4245 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00004246 return getDerived().parseIntegerLiteral("signed char");
Richard Smithc20d1442018-08-20 20:14:49 +00004247 case 'h':
4248 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00004249 return getDerived().parseIntegerLiteral("unsigned char");
Richard Smithc20d1442018-08-20 20:14:49 +00004250 case 's':
4251 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00004252 return getDerived().parseIntegerLiteral("short");
Richard Smithc20d1442018-08-20 20:14:49 +00004253 case 't':
4254 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00004255 return getDerived().parseIntegerLiteral("unsigned short");
Richard Smithc20d1442018-08-20 20:14:49 +00004256 case 'i':
4257 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00004258 return getDerived().parseIntegerLiteral("");
Richard Smithc20d1442018-08-20 20:14:49 +00004259 case 'j':
4260 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00004261 return getDerived().parseIntegerLiteral("u");
Richard Smithc20d1442018-08-20 20:14:49 +00004262 case 'l':
4263 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00004264 return getDerived().parseIntegerLiteral("l");
Richard Smithc20d1442018-08-20 20:14:49 +00004265 case 'm':
4266 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00004267 return getDerived().parseIntegerLiteral("ul");
Richard Smithc20d1442018-08-20 20:14:49 +00004268 case 'x':
4269 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00004270 return getDerived().parseIntegerLiteral("ll");
Richard Smithc20d1442018-08-20 20:14:49 +00004271 case 'y':
4272 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00004273 return getDerived().parseIntegerLiteral("ull");
Richard Smithc20d1442018-08-20 20:14:49 +00004274 case 'n':
4275 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00004276 return getDerived().parseIntegerLiteral("__int128");
Richard Smithc20d1442018-08-20 20:14:49 +00004277 case 'o':
4278 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00004279 return getDerived().parseIntegerLiteral("unsigned __int128");
Richard Smithc20d1442018-08-20 20:14:49 +00004280 case 'f':
4281 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00004282 return getDerived().template parseFloatingLiteral<float>();
Richard Smithc20d1442018-08-20 20:14:49 +00004283 case 'd':
4284 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00004285 return getDerived().template parseFloatingLiteral<double>();
Richard Smithc20d1442018-08-20 20:14:49 +00004286 case 'e':
4287 ++First;
Xing Xue3dc5e082020-04-15 09:59:06 -04004288#if defined(__powerpc__) || defined(__s390__)
4289 // Handle cases where long doubles encoded with e have the same size
4290 // and representation as doubles.
4291 return getDerived().template parseFloatingLiteral<double>();
4292#else
Pavel Labathba825192018-10-16 14:29:14 +00004293 return getDerived().template parseFloatingLiteral<long double>();
Xing Xue3dc5e082020-04-15 09:59:06 -04004294#endif
Richard Smithc20d1442018-08-20 20:14:49 +00004295 case '_':
4296 if (consumeIf("_Z")) {
Pavel Labathba825192018-10-16 14:29:14 +00004297 Node *R = getDerived().parseEncoding();
Richard Smithc20d1442018-08-20 20:14:49 +00004298 if (R != nullptr && consumeIf('E'))
4299 return R;
4300 }
4301 return nullptr;
Richard Smithdf1c14c2019-09-06 23:53:21 +00004302 case 'A': {
4303 Node *T = getDerived().parseType();
4304 if (T == nullptr)
4305 return nullptr;
4306 // FIXME: We need to include the string contents in the mangling.
4307 if (consumeIf('E'))
4308 return make<StringLiteral>(T);
4309 return nullptr;
4310 }
4311 case 'D':
4312 if (consumeIf("DnE"))
4313 return make<NameType>("nullptr");
4314 return nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00004315 case 'T':
4316 // Invalid mangled name per
4317 // http://sourcerytools.com/pipermail/cxx-abi-dev/2011-August/002422.html
4318 return nullptr;
Richard Smithfb917462019-09-09 22:26:04 +00004319 case 'U': {
4320 // FIXME: Should we support LUb... for block literals?
4321 if (look(1) != 'l')
4322 return nullptr;
4323 Node *T = parseUnnamedTypeName(nullptr);
4324 if (!T || !consumeIf('E'))
4325 return nullptr;
4326 return make<LambdaExpr>(T);
4327 }
Richard Smithc20d1442018-08-20 20:14:49 +00004328 default: {
4329 // might be named type
Pavel Labathba825192018-10-16 14:29:14 +00004330 Node *T = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00004331 if (T == nullptr)
4332 return nullptr;
Erik Pilkington0a170f12020-05-13 14:13:37 -04004333 StringView N = parseNumber(/*AllowNegative=*/true);
Richard Smithfb917462019-09-09 22:26:04 +00004334 if (N.empty())
4335 return nullptr;
4336 if (!consumeIf('E'))
4337 return nullptr;
Erik Pilkington0a170f12020-05-13 14:13:37 -04004338 return make<EnumLiteral>(T, N);
Richard Smithc20d1442018-08-20 20:14:49 +00004339 }
4340 }
4341}
4342
4343// <braced-expression> ::= <expression>
4344// ::= di <field source-name> <braced-expression> # .name = expr
4345// ::= dx <index expression> <braced-expression> # [expr] = expr
4346// ::= dX <range begin expression> <range end expression> <braced-expression>
Pavel Labathba825192018-10-16 14:29:14 +00004347template <typename Derived, typename Alloc>
4348Node *AbstractManglingParser<Derived, Alloc>::parseBracedExpr() {
Richard Smithc20d1442018-08-20 20:14:49 +00004349 if (look() == 'd') {
4350 switch (look(1)) {
4351 case 'i': {
4352 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004353 Node *Field = getDerived().parseSourceName(/*NameState=*/nullptr);
Richard Smithc20d1442018-08-20 20:14:49 +00004354 if (Field == nullptr)
4355 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00004356 Node *Init = getDerived().parseBracedExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004357 if (Init == nullptr)
4358 return nullptr;
4359 return make<BracedExpr>(Field, Init, /*isArray=*/false);
4360 }
4361 case 'x': {
4362 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004363 Node *Index = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004364 if (Index == nullptr)
4365 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00004366 Node *Init = getDerived().parseBracedExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004367 if (Init == nullptr)
4368 return nullptr;
4369 return make<BracedExpr>(Index, Init, /*isArray=*/true);
4370 }
4371 case 'X': {
4372 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004373 Node *RangeBegin = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004374 if (RangeBegin == nullptr)
4375 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00004376 Node *RangeEnd = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004377 if (RangeEnd == nullptr)
4378 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00004379 Node *Init = getDerived().parseBracedExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004380 if (Init == nullptr)
4381 return nullptr;
4382 return make<BracedRangeExpr>(RangeBegin, RangeEnd, Init);
4383 }
4384 }
4385 }
Pavel Labathba825192018-10-16 14:29:14 +00004386 return getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004387}
4388
4389// (not yet in the spec)
4390// <fold-expr> ::= fL <binary-operator-name> <expression> <expression>
4391// ::= fR <binary-operator-name> <expression> <expression>
4392// ::= fl <binary-operator-name> <expression>
4393// ::= fr <binary-operator-name> <expression>
Pavel Labathba825192018-10-16 14:29:14 +00004394template <typename Derived, typename Alloc>
4395Node *AbstractManglingParser<Derived, Alloc>::parseFoldExpr() {
Richard Smithc20d1442018-08-20 20:14:49 +00004396 if (!consumeIf('f'))
4397 return nullptr;
4398
4399 char FoldKind = look();
4400 bool IsLeftFold, HasInitializer;
4401 HasInitializer = FoldKind == 'L' || FoldKind == 'R';
4402 if (FoldKind == 'l' || FoldKind == 'L')
4403 IsLeftFold = true;
4404 else if (FoldKind == 'r' || FoldKind == 'R')
4405 IsLeftFold = false;
4406 else
4407 return nullptr;
4408 ++First;
4409
4410 // FIXME: This map is duplicated in parseOperatorName and parseExpr.
4411 StringView OperatorName;
4412 if (consumeIf("aa")) OperatorName = "&&";
4413 else if (consumeIf("an")) OperatorName = "&";
4414 else if (consumeIf("aN")) OperatorName = "&=";
4415 else if (consumeIf("aS")) OperatorName = "=";
4416 else if (consumeIf("cm")) OperatorName = ",";
4417 else if (consumeIf("ds")) OperatorName = ".*";
4418 else if (consumeIf("dv")) OperatorName = "/";
4419 else if (consumeIf("dV")) OperatorName = "/=";
4420 else if (consumeIf("eo")) OperatorName = "^";
4421 else if (consumeIf("eO")) OperatorName = "^=";
4422 else if (consumeIf("eq")) OperatorName = "==";
4423 else if (consumeIf("ge")) OperatorName = ">=";
4424 else if (consumeIf("gt")) OperatorName = ">";
4425 else if (consumeIf("le")) OperatorName = "<=";
4426 else if (consumeIf("ls")) OperatorName = "<<";
4427 else if (consumeIf("lS")) OperatorName = "<<=";
4428 else if (consumeIf("lt")) OperatorName = "<";
4429 else if (consumeIf("mi")) OperatorName = "-";
4430 else if (consumeIf("mI")) OperatorName = "-=";
4431 else if (consumeIf("ml")) OperatorName = "*";
4432 else if (consumeIf("mL")) OperatorName = "*=";
4433 else if (consumeIf("ne")) OperatorName = "!=";
4434 else if (consumeIf("oo")) OperatorName = "||";
4435 else if (consumeIf("or")) OperatorName = "|";
4436 else if (consumeIf("oR")) OperatorName = "|=";
4437 else if (consumeIf("pl")) OperatorName = "+";
4438 else if (consumeIf("pL")) OperatorName = "+=";
4439 else if (consumeIf("rm")) OperatorName = "%";
4440 else if (consumeIf("rM")) OperatorName = "%=";
4441 else if (consumeIf("rs")) OperatorName = ">>";
4442 else if (consumeIf("rS")) OperatorName = ">>=";
4443 else return nullptr;
4444
Pavel Labathba825192018-10-16 14:29:14 +00004445 Node *Pack = getDerived().parseExpr(), *Init = nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00004446 if (Pack == nullptr)
4447 return nullptr;
4448 if (HasInitializer) {
Pavel Labathba825192018-10-16 14:29:14 +00004449 Init = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004450 if (Init == nullptr)
4451 return nullptr;
4452 }
4453
4454 if (IsLeftFold && Init)
4455 std::swap(Pack, Init);
4456
4457 return make<FoldExpr>(IsLeftFold, OperatorName, Pack, Init);
4458}
4459
Richard Smith1865d2f2020-10-22 19:29:36 -07004460// <expression> ::= mc <parameter type> <expr> [<offset number>] E
4461//
4462// Not yet in the spec: https://github.com/itanium-cxx-abi/cxx-abi/issues/47
4463template <typename Derived, typename Alloc>
4464Node *AbstractManglingParser<Derived, Alloc>::parsePointerToMemberConversionExpr() {
4465 Node *Ty = getDerived().parseType();
4466 if (!Ty)
4467 return nullptr;
4468 Node *Expr = getDerived().parseExpr();
4469 if (!Expr)
4470 return nullptr;
4471 StringView Offset = getDerived().parseNumber(true);
4472 if (!consumeIf('E'))
4473 return nullptr;
4474 return make<PointerToMemberConversionExpr>(Ty, Expr, Offset);
4475}
4476
4477// <expression> ::= so <referent type> <expr> [<offset number>] <union-selector>* [p] E
4478// <union-selector> ::= _ [<number>]
4479//
4480// Not yet in the spec: https://github.com/itanium-cxx-abi/cxx-abi/issues/47
4481template <typename Derived, typename Alloc>
4482Node *AbstractManglingParser<Derived, Alloc>::parseSubobjectExpr() {
4483 Node *Ty = getDerived().parseType();
4484 if (!Ty)
4485 return nullptr;
4486 Node *Expr = getDerived().parseExpr();
4487 if (!Expr)
4488 return nullptr;
4489 StringView Offset = getDerived().parseNumber(true);
4490 size_t SelectorsBegin = Names.size();
4491 while (consumeIf('_')) {
4492 Node *Selector = make<NameType>(parseNumber());
4493 if (!Selector)
4494 return nullptr;
4495 Names.push_back(Selector);
4496 }
4497 bool OnePastTheEnd = consumeIf('p');
4498 if (!consumeIf('E'))
4499 return nullptr;
4500 return make<SubobjectExpr>(
4501 Ty, Expr, Offset, popTrailingNodeArray(SelectorsBegin), OnePastTheEnd);
4502}
4503
Richard Smithc20d1442018-08-20 20:14:49 +00004504// <expression> ::= <unary operator-name> <expression>
4505// ::= <binary operator-name> <expression> <expression>
4506// ::= <ternary operator-name> <expression> <expression> <expression>
4507// ::= cl <expression>+ E # call
4508// ::= cv <type> <expression> # conversion with one argument
4509// ::= cv <type> _ <expression>* E # conversion with a different number of arguments
4510// ::= [gs] nw <expression>* _ <type> E # new (expr-list) type
4511// ::= [gs] nw <expression>* _ <type> <initializer> # new (expr-list) type (init)
4512// ::= [gs] na <expression>* _ <type> E # new[] (expr-list) type
4513// ::= [gs] na <expression>* _ <type> <initializer> # new[] (expr-list) type (init)
4514// ::= [gs] dl <expression> # delete expression
4515// ::= [gs] da <expression> # delete[] expression
4516// ::= pp_ <expression> # prefix ++
4517// ::= mm_ <expression> # prefix --
4518// ::= ti <type> # typeid (type)
4519// ::= te <expression> # typeid (expression)
4520// ::= dc <type> <expression> # dynamic_cast<type> (expression)
4521// ::= sc <type> <expression> # static_cast<type> (expression)
4522// ::= cc <type> <expression> # const_cast<type> (expression)
4523// ::= rc <type> <expression> # reinterpret_cast<type> (expression)
4524// ::= st <type> # sizeof (a type)
4525// ::= sz <expression> # sizeof (an expression)
4526// ::= at <type> # alignof (a type)
4527// ::= az <expression> # alignof (an expression)
4528// ::= nx <expression> # noexcept (expression)
4529// ::= <template-param>
4530// ::= <function-param>
4531// ::= dt <expression> <unresolved-name> # expr.name
4532// ::= pt <expression> <unresolved-name> # expr->name
4533// ::= ds <expression> <expression> # expr.*expr
4534// ::= sZ <template-param> # size of a parameter pack
4535// ::= sZ <function-param> # size of a function parameter pack
4536// ::= sP <template-arg>* E # sizeof...(T), size of a captured template parameter pack from an alias template
4537// ::= sp <expression> # pack expansion
4538// ::= tw <expression> # throw expression
4539// ::= tr # throw with no operand (rethrow)
4540// ::= <unresolved-name> # f(p), N::f(p), ::f(p),
4541// # freestanding dependent name (e.g., T::x),
4542// # objectless nonstatic member reference
4543// ::= fL <binary-operator-name> <expression> <expression>
4544// ::= fR <binary-operator-name> <expression> <expression>
4545// ::= fl <binary-operator-name> <expression>
4546// ::= fr <binary-operator-name> <expression>
4547// ::= <expr-primary>
Pavel Labathba825192018-10-16 14:29:14 +00004548template <typename Derived, typename Alloc>
4549Node *AbstractManglingParser<Derived, Alloc>::parseExpr() {
Richard Smithc20d1442018-08-20 20:14:49 +00004550 bool Global = consumeIf("gs");
4551 if (numLeft() < 2)
4552 return nullptr;
4553
4554 switch (*First) {
4555 case 'L':
Pavel Labathba825192018-10-16 14:29:14 +00004556 return getDerived().parseExprPrimary();
Richard Smithc20d1442018-08-20 20:14:49 +00004557 case 'T':
Pavel Labathba825192018-10-16 14:29:14 +00004558 return getDerived().parseTemplateParam();
Richard Smithc20d1442018-08-20 20:14:49 +00004559 case 'f': {
4560 // Disambiguate a fold expression from a <function-param>.
4561 if (look(1) == 'p' || (look(1) == 'L' && std::isdigit(look(2))))
Pavel Labathba825192018-10-16 14:29:14 +00004562 return getDerived().parseFunctionParam();
4563 return getDerived().parseFoldExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004564 }
4565 case 'a':
4566 switch (First[1]) {
4567 case 'a':
4568 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004569 return getDerived().parseBinaryExpr("&&");
Richard Smithc20d1442018-08-20 20:14:49 +00004570 case 'd':
4571 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004572 return getDerived().parsePrefixExpr("&");
Richard Smithc20d1442018-08-20 20:14:49 +00004573 case 'n':
4574 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004575 return getDerived().parseBinaryExpr("&");
Richard Smithc20d1442018-08-20 20:14:49 +00004576 case 'N':
4577 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004578 return getDerived().parseBinaryExpr("&=");
Richard Smithc20d1442018-08-20 20:14:49 +00004579 case 'S':
4580 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004581 return getDerived().parseBinaryExpr("=");
Richard Smithc20d1442018-08-20 20:14:49 +00004582 case 't': {
4583 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004584 Node *Ty = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00004585 if (Ty == nullptr)
4586 return nullptr;
4587 return make<EnclosingExpr>("alignof (", Ty, ")");
4588 }
4589 case 'z': {
4590 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004591 Node *Ty = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004592 if (Ty == nullptr)
4593 return nullptr;
4594 return make<EnclosingExpr>("alignof (", Ty, ")");
4595 }
4596 }
4597 return nullptr;
4598 case 'c':
4599 switch (First[1]) {
4600 // cc <type> <expression> # const_cast<type>(expression)
4601 case 'c': {
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 Ty;
Pavel Labathba825192018-10-16 14:29:14 +00004606 Node *Ex = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004607 if (Ex == nullptr)
4608 return Ex;
4609 return make<CastExpr>("const_cast", Ty, Ex);
4610 }
4611 // cl <expression>+ E # call
4612 case 'l': {
4613 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004614 Node *Callee = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004615 if (Callee == nullptr)
4616 return Callee;
4617 size_t ExprsBegin = Names.size();
4618 while (!consumeIf('E')) {
Pavel Labathba825192018-10-16 14:29:14 +00004619 Node *E = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004620 if (E == nullptr)
4621 return E;
4622 Names.push_back(E);
4623 }
4624 return make<CallExpr>(Callee, popTrailingNodeArray(ExprsBegin));
4625 }
4626 case 'm':
4627 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004628 return getDerived().parseBinaryExpr(",");
Richard Smithc20d1442018-08-20 20:14:49 +00004629 case 'o':
4630 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004631 return getDerived().parsePrefixExpr("~");
Richard Smithc20d1442018-08-20 20:14:49 +00004632 case 'v':
Pavel Labathba825192018-10-16 14:29:14 +00004633 return getDerived().parseConversionExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004634 }
4635 return nullptr;
4636 case 'd':
4637 switch (First[1]) {
4638 case 'a': {
4639 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004640 Node *Ex = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004641 if (Ex == nullptr)
4642 return Ex;
4643 return make<DeleteExpr>(Ex, Global, /*is_array=*/true);
4644 }
4645 case 'c': {
4646 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004647 Node *T = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00004648 if (T == nullptr)
4649 return T;
Pavel Labathba825192018-10-16 14:29:14 +00004650 Node *Ex = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004651 if (Ex == nullptr)
4652 return Ex;
4653 return make<CastExpr>("dynamic_cast", T, Ex);
4654 }
4655 case 'e':
4656 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004657 return getDerived().parsePrefixExpr("*");
Richard Smithc20d1442018-08-20 20:14:49 +00004658 case 'l': {
4659 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004660 Node *E = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004661 if (E == nullptr)
4662 return E;
4663 return make<DeleteExpr>(E, Global, /*is_array=*/false);
4664 }
4665 case 'n':
Pavel Labathba825192018-10-16 14:29:14 +00004666 return getDerived().parseUnresolvedName();
Richard Smithc20d1442018-08-20 20:14:49 +00004667 case 's': {
4668 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004669 Node *LHS = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004670 if (LHS == nullptr)
4671 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00004672 Node *RHS = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004673 if (RHS == nullptr)
4674 return nullptr;
4675 return make<MemberExpr>(LHS, ".*", RHS);
4676 }
4677 case 't': {
4678 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004679 Node *LHS = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004680 if (LHS == nullptr)
4681 return LHS;
Pavel Labathba825192018-10-16 14:29:14 +00004682 Node *RHS = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004683 if (RHS == nullptr)
4684 return nullptr;
4685 return make<MemberExpr>(LHS, ".", RHS);
4686 }
4687 case 'v':
4688 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004689 return getDerived().parseBinaryExpr("/");
Richard Smithc20d1442018-08-20 20:14:49 +00004690 case 'V':
4691 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004692 return getDerived().parseBinaryExpr("/=");
Richard Smithc20d1442018-08-20 20:14:49 +00004693 }
4694 return nullptr;
4695 case 'e':
4696 switch (First[1]) {
4697 case 'o':
4698 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004699 return getDerived().parseBinaryExpr("^");
Richard Smithc20d1442018-08-20 20:14:49 +00004700 case 'O':
4701 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004702 return getDerived().parseBinaryExpr("^=");
Richard Smithc20d1442018-08-20 20:14:49 +00004703 case 'q':
4704 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004705 return getDerived().parseBinaryExpr("==");
Richard Smithc20d1442018-08-20 20:14:49 +00004706 }
4707 return nullptr;
4708 case 'g':
4709 switch (First[1]) {
4710 case 'e':
4711 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004712 return getDerived().parseBinaryExpr(">=");
Richard Smithc20d1442018-08-20 20:14:49 +00004713 case 't':
4714 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004715 return getDerived().parseBinaryExpr(">");
Richard Smithc20d1442018-08-20 20:14:49 +00004716 }
4717 return nullptr;
4718 case 'i':
4719 switch (First[1]) {
4720 case 'x': {
4721 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004722 Node *Base = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004723 if (Base == nullptr)
4724 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00004725 Node *Index = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004726 if (Index == nullptr)
4727 return Index;
4728 return make<ArraySubscriptExpr>(Base, Index);
4729 }
4730 case 'l': {
4731 First += 2;
4732 size_t InitsBegin = Names.size();
4733 while (!consumeIf('E')) {
Pavel Labathba825192018-10-16 14:29:14 +00004734 Node *E = getDerived().parseBracedExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004735 if (E == nullptr)
4736 return nullptr;
4737 Names.push_back(E);
4738 }
4739 return make<InitListExpr>(nullptr, popTrailingNodeArray(InitsBegin));
4740 }
4741 }
4742 return nullptr;
4743 case 'l':
4744 switch (First[1]) {
4745 case 'e':
4746 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004747 return getDerived().parseBinaryExpr("<=");
Richard Smithc20d1442018-08-20 20:14:49 +00004748 case 's':
4749 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004750 return getDerived().parseBinaryExpr("<<");
Richard Smithc20d1442018-08-20 20:14:49 +00004751 case 'S':
4752 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004753 return getDerived().parseBinaryExpr("<<=");
Richard Smithc20d1442018-08-20 20:14:49 +00004754 case 't':
4755 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004756 return getDerived().parseBinaryExpr("<");
Richard Smithc20d1442018-08-20 20:14:49 +00004757 }
4758 return nullptr;
4759 case 'm':
4760 switch (First[1]) {
Richard Smith1865d2f2020-10-22 19:29:36 -07004761 case 'c':
4762 First += 2;
4763 return parsePointerToMemberConversionExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004764 case 'i':
4765 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004766 return getDerived().parseBinaryExpr("-");
Richard Smithc20d1442018-08-20 20:14:49 +00004767 case 'I':
4768 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004769 return getDerived().parseBinaryExpr("-=");
Richard Smithc20d1442018-08-20 20:14:49 +00004770 case 'l':
4771 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004772 return getDerived().parseBinaryExpr("*");
Richard Smithc20d1442018-08-20 20:14:49 +00004773 case 'L':
4774 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004775 return getDerived().parseBinaryExpr("*=");
Richard Smithc20d1442018-08-20 20:14:49 +00004776 case 'm':
4777 First += 2;
4778 if (consumeIf('_'))
Pavel Labathba825192018-10-16 14:29:14 +00004779 return getDerived().parsePrefixExpr("--");
4780 Node *Ex = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004781 if (Ex == nullptr)
4782 return nullptr;
4783 return make<PostfixExpr>(Ex, "--");
4784 }
4785 return nullptr;
4786 case 'n':
4787 switch (First[1]) {
4788 case 'a':
4789 case 'w':
Pavel Labathba825192018-10-16 14:29:14 +00004790 return getDerived().parseNewExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004791 case 'e':
4792 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004793 return getDerived().parseBinaryExpr("!=");
Richard Smithc20d1442018-08-20 20:14:49 +00004794 case 'g':
4795 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004796 return getDerived().parsePrefixExpr("-");
Richard Smithc20d1442018-08-20 20:14:49 +00004797 case 't':
4798 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004799 return getDerived().parsePrefixExpr("!");
Richard Smithc20d1442018-08-20 20:14:49 +00004800 case 'x':
4801 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004802 Node *Ex = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004803 if (Ex == nullptr)
4804 return Ex;
4805 return make<EnclosingExpr>("noexcept (", Ex, ")");
4806 }
4807 return nullptr;
4808 case 'o':
4809 switch (First[1]) {
4810 case 'n':
Pavel Labathba825192018-10-16 14:29:14 +00004811 return getDerived().parseUnresolvedName();
Richard Smithc20d1442018-08-20 20:14:49 +00004812 case 'o':
4813 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004814 return getDerived().parseBinaryExpr("||");
Richard Smithc20d1442018-08-20 20:14:49 +00004815 case 'r':
4816 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004817 return getDerived().parseBinaryExpr("|");
Richard Smithc20d1442018-08-20 20:14:49 +00004818 case 'R':
4819 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004820 return getDerived().parseBinaryExpr("|=");
Richard Smithc20d1442018-08-20 20:14:49 +00004821 }
4822 return nullptr;
4823 case 'p':
4824 switch (First[1]) {
4825 case 'm':
4826 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004827 return getDerived().parseBinaryExpr("->*");
Richard Smithc20d1442018-08-20 20:14:49 +00004828 case 'l':
4829 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004830 return getDerived().parseBinaryExpr("+");
Richard Smithc20d1442018-08-20 20:14:49 +00004831 case 'L':
4832 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004833 return getDerived().parseBinaryExpr("+=");
Richard Smithc20d1442018-08-20 20:14:49 +00004834 case 'p': {
4835 First += 2;
4836 if (consumeIf('_'))
Pavel Labathba825192018-10-16 14:29:14 +00004837 return getDerived().parsePrefixExpr("++");
4838 Node *Ex = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004839 if (Ex == nullptr)
4840 return Ex;
4841 return make<PostfixExpr>(Ex, "++");
4842 }
4843 case 's':
4844 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004845 return getDerived().parsePrefixExpr("+");
Richard Smithc20d1442018-08-20 20:14:49 +00004846 case 't': {
4847 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004848 Node *L = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004849 if (L == nullptr)
4850 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00004851 Node *R = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004852 if (R == nullptr)
4853 return nullptr;
4854 return make<MemberExpr>(L, "->", R);
4855 }
4856 }
4857 return nullptr;
4858 case 'q':
4859 if (First[1] == 'u') {
4860 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004861 Node *Cond = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004862 if (Cond == nullptr)
4863 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00004864 Node *LHS = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004865 if (LHS == nullptr)
4866 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00004867 Node *RHS = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004868 if (RHS == nullptr)
4869 return nullptr;
4870 return make<ConditionalExpr>(Cond, LHS, RHS);
4871 }
4872 return nullptr;
4873 case 'r':
4874 switch (First[1]) {
4875 case 'c': {
4876 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004877 Node *T = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00004878 if (T == nullptr)
4879 return T;
Pavel Labathba825192018-10-16 14:29:14 +00004880 Node *Ex = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004881 if (Ex == nullptr)
4882 return Ex;
4883 return make<CastExpr>("reinterpret_cast", T, Ex);
4884 }
4885 case 'm':
4886 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004887 return getDerived().parseBinaryExpr("%");
Richard Smithc20d1442018-08-20 20:14:49 +00004888 case 'M':
4889 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004890 return getDerived().parseBinaryExpr("%=");
Richard Smithc20d1442018-08-20 20:14:49 +00004891 case 's':
4892 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004893 return getDerived().parseBinaryExpr(">>");
Richard Smithc20d1442018-08-20 20:14:49 +00004894 case 'S':
4895 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004896 return getDerived().parseBinaryExpr(">>=");
Richard Smithc20d1442018-08-20 20:14:49 +00004897 }
4898 return nullptr;
4899 case 's':
4900 switch (First[1]) {
4901 case 'c': {
4902 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004903 Node *T = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00004904 if (T == nullptr)
4905 return T;
Pavel Labathba825192018-10-16 14:29:14 +00004906 Node *Ex = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004907 if (Ex == nullptr)
4908 return Ex;
4909 return make<CastExpr>("static_cast", T, Ex);
4910 }
Richard Smith1865d2f2020-10-22 19:29:36 -07004911 case 'o':
4912 First += 2;
4913 return parseSubobjectExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004914 case 'p': {
4915 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004916 Node *Child = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004917 if (Child == nullptr)
4918 return nullptr;
4919 return make<ParameterPackExpansion>(Child);
4920 }
4921 case 'r':
Pavel Labathba825192018-10-16 14:29:14 +00004922 return getDerived().parseUnresolvedName();
Richard Smithc20d1442018-08-20 20:14:49 +00004923 case 't': {
4924 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004925 Node *Ty = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00004926 if (Ty == nullptr)
4927 return Ty;
4928 return make<EnclosingExpr>("sizeof (", Ty, ")");
4929 }
4930 case 'z': {
4931 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004932 Node *Ex = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004933 if (Ex == nullptr)
4934 return Ex;
4935 return make<EnclosingExpr>("sizeof (", Ex, ")");
4936 }
4937 case 'Z':
4938 First += 2;
4939 if (look() == 'T') {
Pavel Labathba825192018-10-16 14:29:14 +00004940 Node *R = getDerived().parseTemplateParam();
Richard Smithc20d1442018-08-20 20:14:49 +00004941 if (R == nullptr)
4942 return nullptr;
4943 return make<SizeofParamPackExpr>(R);
4944 } else if (look() == 'f') {
Pavel Labathba825192018-10-16 14:29:14 +00004945 Node *FP = getDerived().parseFunctionParam();
Richard Smithc20d1442018-08-20 20:14:49 +00004946 if (FP == nullptr)
4947 return nullptr;
4948 return make<EnclosingExpr>("sizeof... (", FP, ")");
4949 }
4950 return nullptr;
4951 case 'P': {
4952 First += 2;
4953 size_t ArgsBegin = Names.size();
4954 while (!consumeIf('E')) {
Pavel Labathba825192018-10-16 14:29:14 +00004955 Node *Arg = getDerived().parseTemplateArg();
Richard Smithc20d1442018-08-20 20:14:49 +00004956 if (Arg == nullptr)
4957 return nullptr;
4958 Names.push_back(Arg);
4959 }
Richard Smithb485b352018-08-24 23:30:26 +00004960 auto *Pack = make<NodeArrayNode>(popTrailingNodeArray(ArgsBegin));
4961 if (!Pack)
4962 return nullptr;
4963 return make<EnclosingExpr>("sizeof... (", Pack, ")");
Richard Smithc20d1442018-08-20 20:14:49 +00004964 }
4965 }
4966 return nullptr;
4967 case 't':
4968 switch (First[1]) {
4969 case 'e': {
4970 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004971 Node *Ex = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004972 if (Ex == nullptr)
4973 return Ex;
4974 return make<EnclosingExpr>("typeid (", Ex, ")");
4975 }
4976 case 'i': {
4977 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004978 Node *Ty = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00004979 if (Ty == nullptr)
4980 return Ty;
4981 return make<EnclosingExpr>("typeid (", Ty, ")");
4982 }
4983 case 'l': {
4984 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004985 Node *Ty = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00004986 if (Ty == nullptr)
4987 return nullptr;
4988 size_t InitsBegin = Names.size();
4989 while (!consumeIf('E')) {
Pavel Labathba825192018-10-16 14:29:14 +00004990 Node *E = getDerived().parseBracedExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004991 if (E == nullptr)
4992 return nullptr;
4993 Names.push_back(E);
4994 }
4995 return make<InitListExpr>(Ty, popTrailingNodeArray(InitsBegin));
4996 }
4997 case 'r':
4998 First += 2;
4999 return make<NameType>("throw");
5000 case 'w': {
5001 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00005002 Node *Ex = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00005003 if (Ex == nullptr)
5004 return nullptr;
5005 return make<ThrowExpr>(Ex);
5006 }
5007 }
5008 return nullptr;
James Y Knight4a60efc2020-12-07 10:26:49 -05005009 case 'u': {
5010 ++First;
5011 Node *Name = getDerived().parseSourceName(/*NameState=*/nullptr);
5012 if (!Name)
5013 return nullptr;
5014 // Special case legacy __uuidof mangling. The 't' and 'z' appear where the
5015 // standard encoding expects a <template-arg>, and would be otherwise be
5016 // interpreted as <type> node 'short' or 'ellipsis'. However, neither
5017 // __uuidof(short) nor __uuidof(...) can actually appear, so there is no
5018 // actual conflict here.
5019 if (Name->getBaseName() == "__uuidof") {
5020 if (numLeft() < 2)
5021 return nullptr;
5022 if (*First == 't') {
5023 ++First;
5024 Node *Ty = getDerived().parseType();
5025 if (!Ty)
5026 return nullptr;
5027 return make<CallExpr>(Name, makeNodeArray(&Ty, &Ty + 1));
5028 }
5029 if (*First == 'z') {
5030 ++First;
5031 Node *Ex = getDerived().parseExpr();
5032 if (!Ex)
5033 return nullptr;
5034 return make<CallExpr>(Name, makeNodeArray(&Ex, &Ex + 1));
5035 }
5036 }
5037 size_t ExprsBegin = Names.size();
5038 while (!consumeIf('E')) {
5039 Node *E = getDerived().parseTemplateArg();
5040 if (E == nullptr)
5041 return E;
5042 Names.push_back(E);
5043 }
5044 return make<CallExpr>(Name, popTrailingNodeArray(ExprsBegin));
5045 }
Richard Smithc20d1442018-08-20 20:14:49 +00005046 case '1':
5047 case '2':
5048 case '3':
5049 case '4':
5050 case '5':
5051 case '6':
5052 case '7':
5053 case '8':
5054 case '9':
Pavel Labathba825192018-10-16 14:29:14 +00005055 return getDerived().parseUnresolvedName();
Richard Smithc20d1442018-08-20 20:14:49 +00005056 }
5057 return nullptr;
5058}
5059
5060// <call-offset> ::= h <nv-offset> _
5061// ::= v <v-offset> _
5062//
5063// <nv-offset> ::= <offset number>
5064// # non-virtual base override
5065//
5066// <v-offset> ::= <offset number> _ <virtual offset number>
5067// # virtual base override, with vcall offset
Pavel Labathba825192018-10-16 14:29:14 +00005068template <typename Alloc, typename Derived>
5069bool AbstractManglingParser<Alloc, Derived>::parseCallOffset() {
Richard Smithc20d1442018-08-20 20:14:49 +00005070 // Just scan through the call offset, we never add this information into the
5071 // output.
5072 if (consumeIf('h'))
5073 return parseNumber(true).empty() || !consumeIf('_');
5074 if (consumeIf('v'))
5075 return parseNumber(true).empty() || !consumeIf('_') ||
5076 parseNumber(true).empty() || !consumeIf('_');
5077 return true;
5078}
5079
5080// <special-name> ::= TV <type> # virtual table
5081// ::= TT <type> # VTT structure (construction vtable index)
5082// ::= TI <type> # typeinfo structure
5083// ::= TS <type> # typeinfo name (null-terminated byte string)
5084// ::= Tc <call-offset> <call-offset> <base encoding>
5085// # base is the nominal target function of thunk
5086// # first call-offset is 'this' adjustment
5087// # second call-offset is result adjustment
5088// ::= T <call-offset> <base encoding>
5089// # base is the nominal target function of thunk
5090// ::= GV <object name> # Guard variable for one-time initialization
5091// # No <type>
5092// ::= TW <object name> # Thread-local wrapper
5093// ::= TH <object name> # Thread-local initialization
5094// ::= GR <object name> _ # First temporary
5095// ::= GR <object name> <seq-id> _ # Subsequent temporaries
5096// extension ::= TC <first type> <number> _ <second type> # construction vtable for second-in-first
5097// extension ::= GR <object name> # reference temporary for object
Pavel Labathba825192018-10-16 14:29:14 +00005098template <typename Derived, typename Alloc>
5099Node *AbstractManglingParser<Derived, Alloc>::parseSpecialName() {
Richard Smithc20d1442018-08-20 20:14:49 +00005100 switch (look()) {
5101 case 'T':
5102 switch (look(1)) {
Richard Smith1865d2f2020-10-22 19:29:36 -07005103 // TA <template-arg> # template parameter object
5104 //
5105 // Not yet in the spec: https://github.com/itanium-cxx-abi/cxx-abi/issues/63
5106 case 'A': {
5107 First += 2;
5108 Node *Arg = getDerived().parseTemplateArg();
5109 if (Arg == nullptr)
5110 return nullptr;
5111 return make<SpecialName>("template parameter object for ", Arg);
5112 }
Richard Smithc20d1442018-08-20 20:14:49 +00005113 // TV <type> # virtual table
5114 case 'V': {
5115 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00005116 Node *Ty = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00005117 if (Ty == nullptr)
5118 return nullptr;
5119 return make<SpecialName>("vtable for ", Ty);
5120 }
5121 // TT <type> # VTT structure (construction vtable index)
5122 case 'T': {
5123 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00005124 Node *Ty = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00005125 if (Ty == nullptr)
5126 return nullptr;
5127 return make<SpecialName>("VTT for ", Ty);
5128 }
5129 // TI <type> # typeinfo structure
5130 case 'I': {
5131 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00005132 Node *Ty = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00005133 if (Ty == nullptr)
5134 return nullptr;
5135 return make<SpecialName>("typeinfo for ", Ty);
5136 }
5137 // TS <type> # typeinfo name (null-terminated byte string)
5138 case 'S': {
5139 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00005140 Node *Ty = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00005141 if (Ty == nullptr)
5142 return nullptr;
5143 return make<SpecialName>("typeinfo name for ", Ty);
5144 }
5145 // Tc <call-offset> <call-offset> <base encoding>
5146 case 'c': {
5147 First += 2;
5148 if (parseCallOffset() || parseCallOffset())
5149 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00005150 Node *Encoding = getDerived().parseEncoding();
Richard Smithc20d1442018-08-20 20:14:49 +00005151 if (Encoding == nullptr)
5152 return nullptr;
5153 return make<SpecialName>("covariant return thunk to ", Encoding);
5154 }
5155 // extension ::= TC <first type> <number> _ <second type>
5156 // # construction vtable for second-in-first
5157 case 'C': {
5158 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00005159 Node *FirstType = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00005160 if (FirstType == nullptr)
5161 return nullptr;
5162 if (parseNumber(true).empty() || !consumeIf('_'))
5163 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00005164 Node *SecondType = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00005165 if (SecondType == nullptr)
5166 return nullptr;
5167 return make<CtorVtableSpecialName>(SecondType, FirstType);
5168 }
5169 // TW <object name> # Thread-local wrapper
5170 case 'W': {
5171 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00005172 Node *Name = getDerived().parseName();
Richard Smithc20d1442018-08-20 20:14:49 +00005173 if (Name == nullptr)
5174 return nullptr;
5175 return make<SpecialName>("thread-local wrapper routine for ", Name);
5176 }
5177 // TH <object name> # Thread-local initialization
5178 case 'H': {
5179 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00005180 Node *Name = getDerived().parseName();
Richard Smithc20d1442018-08-20 20:14:49 +00005181 if (Name == nullptr)
5182 return nullptr;
5183 return make<SpecialName>("thread-local initialization routine for ", Name);
5184 }
5185 // T <call-offset> <base encoding>
5186 default: {
5187 ++First;
5188 bool IsVirt = look() == 'v';
5189 if (parseCallOffset())
5190 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00005191 Node *BaseEncoding = getDerived().parseEncoding();
Richard Smithc20d1442018-08-20 20:14:49 +00005192 if (BaseEncoding == nullptr)
5193 return nullptr;
5194 if (IsVirt)
5195 return make<SpecialName>("virtual thunk to ", BaseEncoding);
5196 else
5197 return make<SpecialName>("non-virtual thunk to ", BaseEncoding);
5198 }
5199 }
5200 case 'G':
5201 switch (look(1)) {
5202 // GV <object name> # Guard variable for one-time initialization
5203 case 'V': {
5204 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00005205 Node *Name = getDerived().parseName();
Richard Smithc20d1442018-08-20 20:14:49 +00005206 if (Name == nullptr)
5207 return nullptr;
5208 return make<SpecialName>("guard variable for ", Name);
5209 }
5210 // GR <object name> # reference temporary for object
5211 // GR <object name> _ # First temporary
5212 // GR <object name> <seq-id> _ # Subsequent temporaries
5213 case 'R': {
5214 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00005215 Node *Name = getDerived().parseName();
Richard Smithc20d1442018-08-20 20:14:49 +00005216 if (Name == nullptr)
5217 return nullptr;
5218 size_t Count;
5219 bool ParsedSeqId = !parseSeqId(&Count);
5220 if (!consumeIf('_') && ParsedSeqId)
5221 return nullptr;
5222 return make<SpecialName>("reference temporary for ", Name);
5223 }
5224 }
5225 }
5226 return nullptr;
5227}
5228
5229// <encoding> ::= <function name> <bare-function-type>
5230// ::= <data name>
5231// ::= <special-name>
Pavel Labathba825192018-10-16 14:29:14 +00005232template <typename Derived, typename Alloc>
5233Node *AbstractManglingParser<Derived, Alloc>::parseEncoding() {
Richard Smithfac39712020-07-09 21:08:39 -07005234 // The template parameters of an encoding are unrelated to those of the
5235 // enclosing context.
5236 class SaveTemplateParams {
5237 AbstractManglingParser *Parser;
5238 decltype(TemplateParams) OldParams;
Justin Lebar2c536232021-06-09 16:57:22 -07005239 decltype(OuterTemplateParams) OldOuterParams;
Richard Smithfac39712020-07-09 21:08:39 -07005240
5241 public:
Louis Dionnec1fe8672020-10-30 17:33:02 -04005242 SaveTemplateParams(AbstractManglingParser *TheParser) : Parser(TheParser) {
Richard Smithfac39712020-07-09 21:08:39 -07005243 OldParams = std::move(Parser->TemplateParams);
Justin Lebar2c536232021-06-09 16:57:22 -07005244 OldOuterParams = std::move(Parser->OuterTemplateParams);
Richard Smithfac39712020-07-09 21:08:39 -07005245 Parser->TemplateParams.clear();
Justin Lebar2c536232021-06-09 16:57:22 -07005246 Parser->OuterTemplateParams.clear();
Richard Smithfac39712020-07-09 21:08:39 -07005247 }
5248 ~SaveTemplateParams() {
5249 Parser->TemplateParams = std::move(OldParams);
Justin Lebar2c536232021-06-09 16:57:22 -07005250 Parser->OuterTemplateParams = std::move(OldOuterParams);
Richard Smithfac39712020-07-09 21:08:39 -07005251 }
5252 } SaveTemplateParams(this);
Richard Smithfd434322020-07-09 20:36:04 -07005253
Richard Smithc20d1442018-08-20 20:14:49 +00005254 if (look() == 'G' || look() == 'T')
Pavel Labathba825192018-10-16 14:29:14 +00005255 return getDerived().parseSpecialName();
Richard Smithc20d1442018-08-20 20:14:49 +00005256
5257 auto IsEndOfEncoding = [&] {
5258 // The set of chars that can potentially follow an <encoding> (none of which
5259 // can start a <type>). Enumerating these allows us to avoid speculative
5260 // parsing.
5261 return numLeft() == 0 || look() == 'E' || look() == '.' || look() == '_';
5262 };
5263
5264 NameState NameInfo(this);
Pavel Labathba825192018-10-16 14:29:14 +00005265 Node *Name = getDerived().parseName(&NameInfo);
Richard Smithc20d1442018-08-20 20:14:49 +00005266 if (Name == nullptr)
5267 return nullptr;
5268
5269 if (resolveForwardTemplateRefs(NameInfo))
5270 return nullptr;
5271
5272 if (IsEndOfEncoding())
5273 return Name;
5274
5275 Node *Attrs = nullptr;
5276 if (consumeIf("Ua9enable_ifI")) {
5277 size_t BeforeArgs = Names.size();
5278 while (!consumeIf('E')) {
Pavel Labathba825192018-10-16 14:29:14 +00005279 Node *Arg = getDerived().parseTemplateArg();
Richard Smithc20d1442018-08-20 20:14:49 +00005280 if (Arg == nullptr)
5281 return nullptr;
5282 Names.push_back(Arg);
5283 }
5284 Attrs = make<EnableIfAttr>(popTrailingNodeArray(BeforeArgs));
Richard Smithb485b352018-08-24 23:30:26 +00005285 if (!Attrs)
5286 return nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00005287 }
5288
5289 Node *ReturnType = nullptr;
5290 if (!NameInfo.CtorDtorConversion && NameInfo.EndsWithTemplateArgs) {
Pavel Labathba825192018-10-16 14:29:14 +00005291 ReturnType = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00005292 if (ReturnType == nullptr)
5293 return nullptr;
5294 }
5295
5296 if (consumeIf('v'))
5297 return make<FunctionEncoding>(ReturnType, Name, NodeArray(),
5298 Attrs, NameInfo.CVQualifiers,
5299 NameInfo.ReferenceQualifier);
5300
5301 size_t ParamsBegin = Names.size();
5302 do {
Pavel Labathba825192018-10-16 14:29:14 +00005303 Node *Ty = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00005304 if (Ty == nullptr)
5305 return nullptr;
5306 Names.push_back(Ty);
5307 } while (!IsEndOfEncoding());
5308
5309 return make<FunctionEncoding>(ReturnType, Name,
5310 popTrailingNodeArray(ParamsBegin),
5311 Attrs, NameInfo.CVQualifiers,
5312 NameInfo.ReferenceQualifier);
5313}
5314
5315template <class Float>
5316struct FloatData;
5317
5318template <>
5319struct FloatData<float>
5320{
5321 static const size_t mangled_size = 8;
5322 static const size_t max_demangled_size = 24;
5323 static constexpr const char* spec = "%af";
5324};
5325
5326template <>
5327struct FloatData<double>
5328{
5329 static const size_t mangled_size = 16;
5330 static const size_t max_demangled_size = 32;
5331 static constexpr const char* spec = "%a";
5332};
5333
5334template <>
5335struct FloatData<long double>
5336{
5337#if defined(__mips__) && defined(__mips_n64) || defined(__aarch64__) || \
5338 defined(__wasm__)
5339 static const size_t mangled_size = 32;
5340#elif defined(__arm__) || defined(__mips__) || defined(__hexagon__)
5341 static const size_t mangled_size = 16;
5342#else
5343 static const size_t mangled_size = 20; // May need to be adjusted to 16 or 24 on other platforms
5344#endif
Elliott Hughes5a360ea2020-04-10 17:42:00 -07005345 // `-0x1.ffffffffffffffffffffffffffffp+16383` + 'L' + '\0' == 42 bytes.
5346 // 28 'f's * 4 bits == 112 bits, which is the number of mantissa bits.
5347 // Negatives are one character longer than positives.
5348 // `0x1.` and `p` are constant, and exponents `+16383` and `-16382` are the
5349 // same length. 1 sign bit, 112 mantissa bits, and 15 exponent bits == 128.
5350 static const size_t max_demangled_size = 42;
Richard Smithc20d1442018-08-20 20:14:49 +00005351 static constexpr const char *spec = "%LaL";
5352};
5353
Pavel Labathba825192018-10-16 14:29:14 +00005354template <typename Alloc, typename Derived>
5355template <class Float>
5356Node *AbstractManglingParser<Alloc, Derived>::parseFloatingLiteral() {
Richard Smithc20d1442018-08-20 20:14:49 +00005357 const size_t N = FloatData<Float>::mangled_size;
5358 if (numLeft() <= N)
5359 return nullptr;
5360 StringView Data(First, First + N);
5361 for (char C : Data)
5362 if (!std::isxdigit(C))
5363 return nullptr;
5364 First += N;
5365 if (!consumeIf('E'))
5366 return nullptr;
5367 return make<FloatLiteralImpl<Float>>(Data);
5368}
5369
5370// <seq-id> ::= <0-9A-Z>+
Pavel Labathba825192018-10-16 14:29:14 +00005371template <typename Alloc, typename Derived>
5372bool AbstractManglingParser<Alloc, Derived>::parseSeqId(size_t *Out) {
Richard Smithc20d1442018-08-20 20:14:49 +00005373 if (!(look() >= '0' && look() <= '9') &&
5374 !(look() >= 'A' && look() <= 'Z'))
5375 return true;
5376
5377 size_t Id = 0;
5378 while (true) {
5379 if (look() >= '0' && look() <= '9') {
5380 Id *= 36;
5381 Id += static_cast<size_t>(look() - '0');
5382 } else if (look() >= 'A' && look() <= 'Z') {
5383 Id *= 36;
5384 Id += static_cast<size_t>(look() - 'A') + 10;
5385 } else {
5386 *Out = Id;
5387 return false;
5388 }
5389 ++First;
5390 }
5391}
5392
5393// <substitution> ::= S <seq-id> _
5394// ::= S_
5395// <substitution> ::= Sa # ::std::allocator
5396// <substitution> ::= Sb # ::std::basic_string
5397// <substitution> ::= Ss # ::std::basic_string < char,
5398// ::std::char_traits<char>,
5399// ::std::allocator<char> >
5400// <substitution> ::= Si # ::std::basic_istream<char, std::char_traits<char> >
5401// <substitution> ::= So # ::std::basic_ostream<char, std::char_traits<char> >
5402// <substitution> ::= Sd # ::std::basic_iostream<char, std::char_traits<char> >
Pavel Labathba825192018-10-16 14:29:14 +00005403template <typename Derived, typename Alloc>
5404Node *AbstractManglingParser<Derived, Alloc>::parseSubstitution() {
Richard Smithc20d1442018-08-20 20:14:49 +00005405 if (!consumeIf('S'))
5406 return nullptr;
5407
5408 if (std::islower(look())) {
5409 Node *SpecialSub;
5410 switch (look()) {
5411 case 'a':
5412 ++First;
5413 SpecialSub = make<SpecialSubstitution>(SpecialSubKind::allocator);
5414 break;
5415 case 'b':
5416 ++First;
5417 SpecialSub = make<SpecialSubstitution>(SpecialSubKind::basic_string);
5418 break;
5419 case 's':
5420 ++First;
5421 SpecialSub = make<SpecialSubstitution>(SpecialSubKind::string);
5422 break;
5423 case 'i':
5424 ++First;
5425 SpecialSub = make<SpecialSubstitution>(SpecialSubKind::istream);
5426 break;
5427 case 'o':
5428 ++First;
5429 SpecialSub = make<SpecialSubstitution>(SpecialSubKind::ostream);
5430 break;
5431 case 'd':
5432 ++First;
5433 SpecialSub = make<SpecialSubstitution>(SpecialSubKind::iostream);
5434 break;
5435 default:
5436 return nullptr;
5437 }
Richard Smithb485b352018-08-24 23:30:26 +00005438 if (!SpecialSub)
5439 return nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00005440 // Itanium C++ ABI 5.1.2: If a name that would use a built-in <substitution>
5441 // has ABI tags, the tags are appended to the substitution; the result is a
5442 // substitutable component.
Pavel Labathba825192018-10-16 14:29:14 +00005443 Node *WithTags = getDerived().parseAbiTags(SpecialSub);
Richard Smithc20d1442018-08-20 20:14:49 +00005444 if (WithTags != SpecialSub) {
5445 Subs.push_back(WithTags);
5446 SpecialSub = WithTags;
5447 }
5448 return SpecialSub;
5449 }
5450
5451 // ::= S_
5452 if (consumeIf('_')) {
5453 if (Subs.empty())
5454 return nullptr;
5455 return Subs[0];
5456 }
5457
5458 // ::= S <seq-id> _
5459 size_t Index = 0;
5460 if (parseSeqId(&Index))
5461 return nullptr;
5462 ++Index;
5463 if (!consumeIf('_') || Index >= Subs.size())
5464 return nullptr;
5465 return Subs[Index];
5466}
5467
5468// <template-param> ::= T_ # first template parameter
5469// ::= T <parameter-2 non-negative number> _
Richard Smithdf1c14c2019-09-06 23:53:21 +00005470// ::= TL <level-1> __
5471// ::= TL <level-1> _ <parameter-2 non-negative number> _
Pavel Labathba825192018-10-16 14:29:14 +00005472template <typename Derived, typename Alloc>
5473Node *AbstractManglingParser<Derived, Alloc>::parseTemplateParam() {
Richard Smithc20d1442018-08-20 20:14:49 +00005474 if (!consumeIf('T'))
5475 return nullptr;
5476
Richard Smithdf1c14c2019-09-06 23:53:21 +00005477 size_t Level = 0;
5478 if (consumeIf('L')) {
5479 if (parsePositiveInteger(&Level))
5480 return nullptr;
5481 ++Level;
5482 if (!consumeIf('_'))
5483 return nullptr;
5484 }
5485
Richard Smithc20d1442018-08-20 20:14:49 +00005486 size_t Index = 0;
5487 if (!consumeIf('_')) {
5488 if (parsePositiveInteger(&Index))
5489 return nullptr;
5490 ++Index;
5491 if (!consumeIf('_'))
5492 return nullptr;
5493 }
5494
Richard Smithc20d1442018-08-20 20:14:49 +00005495 // If we're in a context where this <template-param> refers to a
5496 // <template-arg> further ahead in the mangled name (currently just conversion
5497 // operator types), then we should only look it up in the right context.
Richard Smithdf1c14c2019-09-06 23:53:21 +00005498 // This can only happen at the outermost level.
5499 if (PermitForwardTemplateReferences && Level == 0) {
Richard Smithb485b352018-08-24 23:30:26 +00005500 Node *ForwardRef = make<ForwardTemplateReference>(Index);
5501 if (!ForwardRef)
5502 return nullptr;
5503 assert(ForwardRef->getKind() == Node::KForwardTemplateReference);
5504 ForwardTemplateRefs.push_back(
5505 static_cast<ForwardTemplateReference *>(ForwardRef));
5506 return ForwardRef;
Richard Smithc20d1442018-08-20 20:14:49 +00005507 }
5508
Richard Smithdf1c14c2019-09-06 23:53:21 +00005509 if (Level >= TemplateParams.size() || !TemplateParams[Level] ||
5510 Index >= TemplateParams[Level]->size()) {
5511 // Itanium ABI 5.1.8: In a generic lambda, uses of auto in the parameter
5512 // list are mangled as the corresponding artificial template type parameter.
5513 if (ParsingLambdaParamsAtLevel == Level && Level <= TemplateParams.size()) {
5514 // This will be popped by the ScopedTemplateParamList in
5515 // parseUnnamedTypeName.
5516 if (Level == TemplateParams.size())
5517 TemplateParams.push_back(nullptr);
5518 return make<NameType>("auto");
5519 }
5520
Richard Smithc20d1442018-08-20 20:14:49 +00005521 return nullptr;
Richard Smithdf1c14c2019-09-06 23:53:21 +00005522 }
5523
5524 return (*TemplateParams[Level])[Index];
5525}
5526
5527// <template-param-decl> ::= Ty # type parameter
5528// ::= Tn <type> # non-type parameter
5529// ::= Tt <template-param-decl>* E # template parameter
5530// ::= Tp <template-param-decl> # parameter pack
5531template <typename Derived, typename Alloc>
5532Node *AbstractManglingParser<Derived, Alloc>::parseTemplateParamDecl() {
5533 auto InventTemplateParamName = [&](TemplateParamKind Kind) {
5534 unsigned Index = NumSyntheticTemplateParameters[(int)Kind]++;
5535 Node *N = make<SyntheticTemplateParamName>(Kind, Index);
5536 if (N) TemplateParams.back()->push_back(N);
5537 return N;
5538 };
5539
5540 if (consumeIf("Ty")) {
5541 Node *Name = InventTemplateParamName(TemplateParamKind::Type);
5542 if (!Name)
5543 return nullptr;
5544 return make<TypeTemplateParamDecl>(Name);
5545 }
5546
5547 if (consumeIf("Tn")) {
5548 Node *Name = InventTemplateParamName(TemplateParamKind::NonType);
5549 if (!Name)
5550 return nullptr;
5551 Node *Type = parseType();
5552 if (!Type)
5553 return nullptr;
5554 return make<NonTypeTemplateParamDecl>(Name, Type);
5555 }
5556
5557 if (consumeIf("Tt")) {
5558 Node *Name = InventTemplateParamName(TemplateParamKind::Template);
5559 if (!Name)
5560 return nullptr;
5561 size_t ParamsBegin = Names.size();
5562 ScopedTemplateParamList TemplateTemplateParamParams(this);
5563 while (!consumeIf("E")) {
5564 Node *P = parseTemplateParamDecl();
5565 if (!P)
5566 return nullptr;
5567 Names.push_back(P);
5568 }
5569 NodeArray Params = popTrailingNodeArray(ParamsBegin);
5570 return make<TemplateTemplateParamDecl>(Name, Params);
5571 }
5572
5573 if (consumeIf("Tp")) {
5574 Node *P = parseTemplateParamDecl();
5575 if (!P)
5576 return nullptr;
5577 return make<TemplateParamPackDecl>(P);
5578 }
5579
5580 return nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00005581}
5582
5583// <template-arg> ::= <type> # type or template
5584// ::= X <expression> E # expression
5585// ::= <expr-primary> # simple expressions
5586// ::= J <template-arg>* E # argument pack
5587// ::= LZ <encoding> E # extension
Pavel Labathba825192018-10-16 14:29:14 +00005588template <typename Derived, typename Alloc>
5589Node *AbstractManglingParser<Derived, Alloc>::parseTemplateArg() {
Richard Smithc20d1442018-08-20 20:14:49 +00005590 switch (look()) {
5591 case 'X': {
5592 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00005593 Node *Arg = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00005594 if (Arg == nullptr || !consumeIf('E'))
5595 return nullptr;
5596 return Arg;
5597 }
5598 case 'J': {
5599 ++First;
5600 size_t ArgsBegin = Names.size();
5601 while (!consumeIf('E')) {
Pavel Labathba825192018-10-16 14:29:14 +00005602 Node *Arg = getDerived().parseTemplateArg();
Richard Smithc20d1442018-08-20 20:14:49 +00005603 if (Arg == nullptr)
5604 return nullptr;
5605 Names.push_back(Arg);
5606 }
5607 NodeArray Args = popTrailingNodeArray(ArgsBegin);
5608 return make<TemplateArgumentPack>(Args);
5609 }
5610 case 'L': {
5611 // ::= LZ <encoding> E # extension
5612 if (look(1) == 'Z') {
5613 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00005614 Node *Arg = getDerived().parseEncoding();
Richard Smithc20d1442018-08-20 20:14:49 +00005615 if (Arg == nullptr || !consumeIf('E'))
5616 return nullptr;
5617 return Arg;
5618 }
5619 // ::= <expr-primary> # simple expressions
Pavel Labathba825192018-10-16 14:29:14 +00005620 return getDerived().parseExprPrimary();
Richard Smithc20d1442018-08-20 20:14:49 +00005621 }
5622 default:
Pavel Labathba825192018-10-16 14:29:14 +00005623 return getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00005624 }
5625}
5626
5627// <template-args> ::= I <template-arg>* E
5628// extension, the abi says <template-arg>+
Pavel Labathba825192018-10-16 14:29:14 +00005629template <typename Derived, typename Alloc>
5630Node *
5631AbstractManglingParser<Derived, Alloc>::parseTemplateArgs(bool TagTemplates) {
Richard Smithc20d1442018-08-20 20:14:49 +00005632 if (!consumeIf('I'))
5633 return nullptr;
5634
5635 // <template-params> refer to the innermost <template-args>. Clear out any
5636 // outer args that we may have inserted into TemplateParams.
Richard Smithdf1c14c2019-09-06 23:53:21 +00005637 if (TagTemplates) {
Richard Smithc20d1442018-08-20 20:14:49 +00005638 TemplateParams.clear();
Richard Smithdf1c14c2019-09-06 23:53:21 +00005639 TemplateParams.push_back(&OuterTemplateParams);
5640 OuterTemplateParams.clear();
5641 }
Richard Smithc20d1442018-08-20 20:14:49 +00005642
5643 size_t ArgsBegin = Names.size();
5644 while (!consumeIf('E')) {
5645 if (TagTemplates) {
5646 auto OldParams = std::move(TemplateParams);
Pavel Labathba825192018-10-16 14:29:14 +00005647 Node *Arg = getDerived().parseTemplateArg();
Richard Smithc20d1442018-08-20 20:14:49 +00005648 TemplateParams = std::move(OldParams);
5649 if (Arg == nullptr)
5650 return nullptr;
5651 Names.push_back(Arg);
5652 Node *TableEntry = Arg;
5653 if (Arg->getKind() == Node::KTemplateArgumentPack) {
5654 TableEntry = make<ParameterPack>(
5655 static_cast<TemplateArgumentPack*>(TableEntry)->getElements());
Richard Smithb485b352018-08-24 23:30:26 +00005656 if (!TableEntry)
5657 return nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00005658 }
Richard Smithdf1c14c2019-09-06 23:53:21 +00005659 TemplateParams.back()->push_back(TableEntry);
Richard Smithc20d1442018-08-20 20:14:49 +00005660 } else {
Pavel Labathba825192018-10-16 14:29:14 +00005661 Node *Arg = getDerived().parseTemplateArg();
Richard Smithc20d1442018-08-20 20:14:49 +00005662 if (Arg == nullptr)
5663 return nullptr;
5664 Names.push_back(Arg);
5665 }
5666 }
5667 return make<TemplateArgs>(popTrailingNodeArray(ArgsBegin));
5668}
5669
5670// <mangled-name> ::= _Z <encoding>
5671// ::= <type>
5672// extension ::= ___Z <encoding> _block_invoke
5673// extension ::= ___Z <encoding> _block_invoke<decimal-digit>+
5674// extension ::= ___Z <encoding> _block_invoke_<decimal-digit>+
Pavel Labathba825192018-10-16 14:29:14 +00005675template <typename Derived, typename Alloc>
5676Node *AbstractManglingParser<Derived, Alloc>::parse() {
Erik Pilkingtonc0df1582019-01-17 21:37:36 +00005677 if (consumeIf("_Z") || consumeIf("__Z")) {
Pavel Labathba825192018-10-16 14:29:14 +00005678 Node *Encoding = getDerived().parseEncoding();
Richard Smithc20d1442018-08-20 20:14:49 +00005679 if (Encoding == nullptr)
5680 return nullptr;
5681 if (look() == '.') {
5682 Encoding = make<DotSuffix>(Encoding, StringView(First, Last));
5683 First = Last;
5684 }
5685 if (numLeft() != 0)
5686 return nullptr;
5687 return Encoding;
5688 }
5689
Erik Pilkingtonc0df1582019-01-17 21:37:36 +00005690 if (consumeIf("___Z") || consumeIf("____Z")) {
Pavel Labathba825192018-10-16 14:29:14 +00005691 Node *Encoding = getDerived().parseEncoding();
Richard Smithc20d1442018-08-20 20:14:49 +00005692 if (Encoding == nullptr || !consumeIf("_block_invoke"))
5693 return nullptr;
5694 bool RequireNumber = consumeIf('_');
5695 if (parseNumber().empty() && RequireNumber)
5696 return nullptr;
5697 if (look() == '.')
5698 First = Last;
5699 if (numLeft() != 0)
5700 return nullptr;
5701 return make<SpecialName>("invocation function for block in ", Encoding);
5702 }
5703
Pavel Labathba825192018-10-16 14:29:14 +00005704 Node *Ty = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00005705 if (numLeft() != 0)
5706 return nullptr;
5707 return Ty;
5708}
5709
Pavel Labathba825192018-10-16 14:29:14 +00005710template <typename Alloc>
5711struct ManglingParser : AbstractManglingParser<ManglingParser<Alloc>, Alloc> {
5712 using AbstractManglingParser<ManglingParser<Alloc>,
5713 Alloc>::AbstractManglingParser;
5714};
5715
Erik Pilkingtonf70e4d82019-01-17 20:37:51 +00005716DEMANGLE_NAMESPACE_END
Richard Smithc20d1442018-08-20 20:14:49 +00005717
Erik Pilkingtonf70e4d82019-01-17 20:37:51 +00005718#endif // DEMANGLE_ITANIUMDEMANGLE_H