blob: c581ed7274764288d1f029e3415cb0b72805b767 [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) \
Pengfei Wang50e90b82021-09-23 11:02:25 +080060 X(BinaryFPType) \
Richard Smithdf1c14c2019-09-06 23:53:21 +000061 X(SyntheticTemplateParamName) \
62 X(TypeTemplateParamDecl) \
63 X(NonTypeTemplateParamDecl) \
64 X(TemplateTemplateParamDecl) \
65 X(TemplateParamPackDecl) \
Richard Smithc20d1442018-08-20 20:14:49 +000066 X(ParameterPack) \
67 X(TemplateArgumentPack) \
68 X(ParameterPackExpansion) \
69 X(TemplateArgs) \
70 X(ForwardTemplateReference) \
71 X(NameWithTemplateArgs) \
72 X(GlobalQualifiedName) \
73 X(StdQualifiedName) \
74 X(ExpandedSpecialSubstitution) \
75 X(SpecialSubstitution) \
76 X(CtorDtorName) \
77 X(DtorName) \
78 X(UnnamedTypeName) \
79 X(ClosureTypeName) \
80 X(StructuredBindingName) \
81 X(BinaryExpr) \
82 X(ArraySubscriptExpr) \
83 X(PostfixExpr) \
84 X(ConditionalExpr) \
85 X(MemberExpr) \
Richard Smith1865d2f2020-10-22 19:29:36 -070086 X(SubobjectExpr) \
Richard Smithc20d1442018-08-20 20:14:49 +000087 X(EnclosingExpr) \
88 X(CastExpr) \
89 X(SizeofParamPackExpr) \
90 X(CallExpr) \
91 X(NewExpr) \
92 X(DeleteExpr) \
93 X(PrefixExpr) \
94 X(FunctionParam) \
95 X(ConversionExpr) \
Richard Smith1865d2f2020-10-22 19:29:36 -070096 X(PointerToMemberConversionExpr) \
Richard Smithc20d1442018-08-20 20:14:49 +000097 X(InitListExpr) \
98 X(FoldExpr) \
99 X(ThrowExpr) \
100 X(BoolExpr) \
Richard Smithdf1c14c2019-09-06 23:53:21 +0000101 X(StringLiteral) \
102 X(LambdaExpr) \
Erik Pilkington0a170f12020-05-13 14:13:37 -0400103 X(EnumLiteral) \
Richard Smithc20d1442018-08-20 20:14:49 +0000104 X(IntegerLiteral) \
105 X(FloatLiteral) \
106 X(DoubleLiteral) \
107 X(LongDoubleLiteral) \
108 X(BracedExpr) \
109 X(BracedRangeExpr)
110
Erik Pilkingtonf70e4d82019-01-17 20:37:51 +0000111DEMANGLE_NAMESPACE_BEGIN
112
Mikhail Borisov8452f062021-08-17 18:06:53 -0400113template <class T, size_t N> class PODSmallVector {
114 static_assert(std::is_pod<T>::value,
115 "T is required to be a plain old data type");
116
117 T *First = nullptr;
118 T *Last = nullptr;
119 T *Cap = nullptr;
120 T Inline[N] = {0};
121
122 bool isInline() const { return First == Inline; }
123
124 void clearInline() {
125 First = Inline;
126 Last = Inline;
127 Cap = Inline + N;
128 }
129
130 void reserve(size_t NewCap) {
131 size_t S = size();
132 if (isInline()) {
133 auto *Tmp = static_cast<T *>(std::malloc(NewCap * sizeof(T)));
134 if (Tmp == nullptr)
135 std::terminate();
136 std::copy(First, Last, Tmp);
137 First = Tmp;
138 } else {
139 First = static_cast<T *>(std::realloc(First, NewCap * sizeof(T)));
140 if (First == nullptr)
141 std::terminate();
142 }
143 Last = First + S;
144 Cap = First + NewCap;
145 }
146
147public:
148 PODSmallVector() : First(Inline), Last(First), Cap(Inline + N) {}
149
150 PODSmallVector(const PODSmallVector &) = delete;
151 PODSmallVector &operator=(const PODSmallVector &) = delete;
152
153 PODSmallVector(PODSmallVector &&Other) : PODSmallVector() {
154 if (Other.isInline()) {
155 std::copy(Other.begin(), Other.end(), First);
156 Last = First + Other.size();
157 Other.clear();
158 return;
159 }
160
161 First = Other.First;
162 Last = Other.Last;
163 Cap = Other.Cap;
164 Other.clearInline();
165 }
166
167 PODSmallVector &operator=(PODSmallVector &&Other) {
168 if (Other.isInline()) {
169 if (!isInline()) {
170 std::free(First);
171 clearInline();
172 }
173 std::copy(Other.begin(), Other.end(), First);
174 Last = First + Other.size();
175 Other.clear();
176 return *this;
177 }
178
179 if (isInline()) {
180 First = Other.First;
181 Last = Other.Last;
182 Cap = Other.Cap;
183 Other.clearInline();
184 return *this;
185 }
186
187 std::swap(First, Other.First);
188 std::swap(Last, Other.Last);
189 std::swap(Cap, Other.Cap);
190 Other.clear();
191 return *this;
192 }
193
194 // NOLINTNEXTLINE(readability-identifier-naming)
195 void push_back(const T &Elem) {
196 if (Last == Cap)
197 reserve(size() * 2);
198 *Last++ = Elem;
199 }
200
201 // NOLINTNEXTLINE(readability-identifier-naming)
202 void pop_back() {
203 assert(Last != First && "Popping empty vector!");
204 --Last;
205 }
206
207 void dropBack(size_t Index) {
208 assert(Index <= size() && "dropBack() can't expand!");
209 Last = First + Index;
210 }
211
212 T *begin() { return First; }
213 T *end() { return Last; }
214
215 bool empty() const { return First == Last; }
216 size_t size() const { return static_cast<size_t>(Last - First); }
217 T &back() {
218 assert(Last != First && "Calling back() on empty vector!");
219 return *(Last - 1);
220 }
221 T &operator[](size_t Index) {
222 assert(Index < size() && "Invalid access!");
223 return *(begin() + Index);
224 }
225 void clear() { Last = First; }
226
227 ~PODSmallVector() {
228 if (!isInline())
229 std::free(First);
230 }
231};
232
Richard Smithc20d1442018-08-20 20:14:49 +0000233// Base class of all AST nodes. The AST is built by the parser, then is
234// traversed by the printLeft/Right functions to produce a demangled string.
235class Node {
236public:
237 enum Kind : unsigned char {
238#define ENUMERATOR(NodeKind) K ## NodeKind,
239 FOR_EACH_NODE_KIND(ENUMERATOR)
240#undef ENUMERATOR
241 };
242
243 /// Three-way bool to track a cached value. Unknown is possible if this node
244 /// has an unexpanded parameter pack below it that may affect this cache.
245 enum class Cache : unsigned char { Yes, No, Unknown, };
246
247private:
248 Kind K;
249
250 // FIXME: Make these protected.
251public:
252 /// Tracks if this node has a component on its right side, in which case we
253 /// need to call printRight.
254 Cache RHSComponentCache;
255
256 /// Track if this node is a (possibly qualified) array type. This can affect
257 /// how we format the output string.
258 Cache ArrayCache;
259
260 /// Track if this node is a (possibly qualified) function type. This can
261 /// affect how we format the output string.
262 Cache FunctionCache;
263
264public:
265 Node(Kind K_, Cache RHSComponentCache_ = Cache::No,
266 Cache ArrayCache_ = Cache::No, Cache FunctionCache_ = Cache::No)
267 : K(K_), RHSComponentCache(RHSComponentCache_), ArrayCache(ArrayCache_),
268 FunctionCache(FunctionCache_) {}
269
270 /// Visit the most-derived object corresponding to this object.
271 template<typename Fn> void visit(Fn F) const;
272
273 // The following function is provided by all derived classes:
274 //
275 // Call F with arguments that, when passed to the constructor of this node,
276 // would construct an equivalent node.
277 //template<typename Fn> void match(Fn F) const;
278
279 bool hasRHSComponent(OutputStream &S) const {
280 if (RHSComponentCache != Cache::Unknown)
281 return RHSComponentCache == Cache::Yes;
282 return hasRHSComponentSlow(S);
283 }
284
285 bool hasArray(OutputStream &S) const {
286 if (ArrayCache != Cache::Unknown)
287 return ArrayCache == Cache::Yes;
288 return hasArraySlow(S);
289 }
290
291 bool hasFunction(OutputStream &S) const {
292 if (FunctionCache != Cache::Unknown)
293 return FunctionCache == Cache::Yes;
294 return hasFunctionSlow(S);
295 }
296
297 Kind getKind() const { return K; }
298
299 virtual bool hasRHSComponentSlow(OutputStream &) const { return false; }
300 virtual bool hasArraySlow(OutputStream &) const { return false; }
301 virtual bool hasFunctionSlow(OutputStream &) const { return false; }
302
303 // Dig through "glue" nodes like ParameterPack and ForwardTemplateReference to
304 // get at a node that actually represents some concrete syntax.
305 virtual const Node *getSyntaxNode(OutputStream &) const {
306 return this;
307 }
308
309 void print(OutputStream &S) const {
310 printLeft(S);
311 if (RHSComponentCache != Cache::No)
312 printRight(S);
313 }
314
315 // Print the "left" side of this Node into OutputStream.
316 virtual void printLeft(OutputStream &) const = 0;
317
318 // Print the "right". This distinction is necessary to represent C++ types
319 // that appear on the RHS of their subtype, such as arrays or functions.
320 // Since most types don't have such a component, provide a default
321 // implementation.
322 virtual void printRight(OutputStream &) const {}
323
324 virtual StringView getBaseName() const { return StringView(); }
325
326 // Silence compiler warnings, this dtor will never be called.
327 virtual ~Node() = default;
328
329#ifndef NDEBUG
Erik Pilkingtonf70e4d82019-01-17 20:37:51 +0000330 DEMANGLE_DUMP_METHOD void dump() const;
Richard Smithc20d1442018-08-20 20:14:49 +0000331#endif
332};
333
334class NodeArray {
335 Node **Elements;
336 size_t NumElements;
337
338public:
339 NodeArray() : Elements(nullptr), NumElements(0) {}
340 NodeArray(Node **Elements_, size_t NumElements_)
341 : Elements(Elements_), NumElements(NumElements_) {}
342
343 bool empty() const { return NumElements == 0; }
344 size_t size() const { return NumElements; }
345
346 Node **begin() const { return Elements; }
347 Node **end() const { return Elements + NumElements; }
348
349 Node *operator[](size_t Idx) const { return Elements[Idx]; }
350
351 void printWithComma(OutputStream &S) const {
352 bool FirstElement = true;
353 for (size_t Idx = 0; Idx != NumElements; ++Idx) {
354 size_t BeforeComma = S.getCurrentPosition();
355 if (!FirstElement)
356 S += ", ";
357 size_t AfterComma = S.getCurrentPosition();
358 Elements[Idx]->print(S);
359
360 // Elements[Idx] is an empty parameter pack expansion, we should erase the
361 // comma we just printed.
362 if (AfterComma == S.getCurrentPosition()) {
363 S.setCurrentPosition(BeforeComma);
364 continue;
365 }
366
367 FirstElement = false;
368 }
369 }
370};
371
372struct NodeArrayNode : Node {
373 NodeArray Array;
374 NodeArrayNode(NodeArray Array_) : Node(KNodeArrayNode), Array(Array_) {}
375
376 template<typename Fn> void match(Fn F) const { F(Array); }
377
378 void printLeft(OutputStream &S) const override {
379 Array.printWithComma(S);
380 }
381};
382
383class DotSuffix final : public Node {
384 const Node *Prefix;
385 const StringView Suffix;
386
387public:
388 DotSuffix(const Node *Prefix_, StringView Suffix_)
389 : Node(KDotSuffix), Prefix(Prefix_), Suffix(Suffix_) {}
390
391 template<typename Fn> void match(Fn F) const { F(Prefix, Suffix); }
392
393 void printLeft(OutputStream &s) const override {
394 Prefix->print(s);
395 s += " (";
396 s += Suffix;
397 s += ")";
398 }
399};
400
401class VendorExtQualType final : public Node {
402 const Node *Ty;
403 StringView Ext;
Alex Orlovf50df922021-03-24 10:21:32 +0400404 const Node *TA;
Richard Smithc20d1442018-08-20 20:14:49 +0000405
406public:
Alex Orlovf50df922021-03-24 10:21:32 +0400407 VendorExtQualType(const Node *Ty_, StringView Ext_, const Node *TA_)
408 : Node(KVendorExtQualType), Ty(Ty_), Ext(Ext_), TA(TA_) {}
Richard Smithc20d1442018-08-20 20:14:49 +0000409
Alex Orlovf50df922021-03-24 10:21:32 +0400410 template <typename Fn> void match(Fn F) const { F(Ty, Ext, TA); }
Richard Smithc20d1442018-08-20 20:14:49 +0000411
412 void printLeft(OutputStream &S) const override {
413 Ty->print(S);
414 S += " ";
415 S += Ext;
Alex Orlovf50df922021-03-24 10:21:32 +0400416 if (TA != nullptr)
417 TA->print(S);
Richard Smithc20d1442018-08-20 20:14:49 +0000418 }
419};
420
421enum FunctionRefQual : unsigned char {
422 FrefQualNone,
423 FrefQualLValue,
424 FrefQualRValue,
425};
426
427enum Qualifiers {
428 QualNone = 0,
429 QualConst = 0x1,
430 QualVolatile = 0x2,
431 QualRestrict = 0x4,
432};
433
434inline Qualifiers operator|=(Qualifiers &Q1, Qualifiers Q2) {
435 return Q1 = static_cast<Qualifiers>(Q1 | Q2);
436}
437
Richard Smithdf1c14c2019-09-06 23:53:21 +0000438class QualType final : public Node {
Richard Smithc20d1442018-08-20 20:14:49 +0000439protected:
440 const Qualifiers Quals;
441 const Node *Child;
442
443 void printQuals(OutputStream &S) const {
444 if (Quals & QualConst)
445 S += " const";
446 if (Quals & QualVolatile)
447 S += " volatile";
448 if (Quals & QualRestrict)
449 S += " restrict";
450 }
451
452public:
453 QualType(const Node *Child_, Qualifiers Quals_)
454 : Node(KQualType, Child_->RHSComponentCache,
455 Child_->ArrayCache, Child_->FunctionCache),
456 Quals(Quals_), Child(Child_) {}
457
458 template<typename Fn> void match(Fn F) const { F(Child, Quals); }
459
460 bool hasRHSComponentSlow(OutputStream &S) const override {
461 return Child->hasRHSComponent(S);
462 }
463 bool hasArraySlow(OutputStream &S) const override {
464 return Child->hasArray(S);
465 }
466 bool hasFunctionSlow(OutputStream &S) const override {
467 return Child->hasFunction(S);
468 }
469
470 void printLeft(OutputStream &S) const override {
471 Child->printLeft(S);
472 printQuals(S);
473 }
474
475 void printRight(OutputStream &S) const override { Child->printRight(S); }
476};
477
478class ConversionOperatorType final : public Node {
479 const Node *Ty;
480
481public:
482 ConversionOperatorType(const Node *Ty_)
483 : Node(KConversionOperatorType), Ty(Ty_) {}
484
485 template<typename Fn> void match(Fn F) const { F(Ty); }
486
487 void printLeft(OutputStream &S) const override {
488 S += "operator ";
489 Ty->print(S);
490 }
491};
492
493class PostfixQualifiedType final : public Node {
494 const Node *Ty;
495 const StringView Postfix;
496
497public:
498 PostfixQualifiedType(Node *Ty_, StringView Postfix_)
499 : Node(KPostfixQualifiedType), Ty(Ty_), Postfix(Postfix_) {}
500
501 template<typename Fn> void match(Fn F) const { F(Ty, Postfix); }
502
503 void printLeft(OutputStream &s) const override {
504 Ty->printLeft(s);
505 s += Postfix;
506 }
507};
508
509class NameType final : public Node {
510 const StringView Name;
511
512public:
513 NameType(StringView Name_) : Node(KNameType), Name(Name_) {}
514
515 template<typename Fn> void match(Fn F) const { F(Name); }
516
517 StringView getName() const { return Name; }
518 StringView getBaseName() const override { return Name; }
519
520 void printLeft(OutputStream &s) const override { s += Name; }
521};
522
523class ElaboratedTypeSpefType : public Node {
524 StringView Kind;
525 Node *Child;
526public:
527 ElaboratedTypeSpefType(StringView Kind_, Node *Child_)
528 : Node(KElaboratedTypeSpefType), Kind(Kind_), Child(Child_) {}
529
530 template<typename Fn> void match(Fn F) const { F(Kind, Child); }
531
532 void printLeft(OutputStream &S) const override {
533 S += Kind;
534 S += ' ';
535 Child->print(S);
536 }
537};
538
539struct AbiTagAttr : Node {
540 Node *Base;
541 StringView Tag;
542
543 AbiTagAttr(Node* Base_, StringView Tag_)
544 : Node(KAbiTagAttr, Base_->RHSComponentCache,
545 Base_->ArrayCache, Base_->FunctionCache),
546 Base(Base_), Tag(Tag_) {}
547
548 template<typename Fn> void match(Fn F) const { F(Base, Tag); }
549
550 void printLeft(OutputStream &S) const override {
551 Base->printLeft(S);
552 S += "[abi:";
553 S += Tag;
554 S += "]";
555 }
556};
557
558class EnableIfAttr : public Node {
559 NodeArray Conditions;
560public:
561 EnableIfAttr(NodeArray Conditions_)
562 : Node(KEnableIfAttr), Conditions(Conditions_) {}
563
564 template<typename Fn> void match(Fn F) const { F(Conditions); }
565
566 void printLeft(OutputStream &S) const override {
567 S += " [enable_if:";
568 Conditions.printWithComma(S);
569 S += ']';
570 }
571};
572
573class ObjCProtoName : public Node {
574 const Node *Ty;
575 StringView Protocol;
576
577 friend class PointerType;
578
579public:
580 ObjCProtoName(const Node *Ty_, StringView Protocol_)
581 : Node(KObjCProtoName), Ty(Ty_), Protocol(Protocol_) {}
582
583 template<typename Fn> void match(Fn F) const { F(Ty, Protocol); }
584
585 bool isObjCObject() const {
586 return Ty->getKind() == KNameType &&
587 static_cast<const NameType *>(Ty)->getName() == "objc_object";
588 }
589
590 void printLeft(OutputStream &S) const override {
591 Ty->print(S);
592 S += "<";
593 S += Protocol;
594 S += ">";
595 }
596};
597
598class PointerType final : public Node {
599 const Node *Pointee;
600
601public:
602 PointerType(const Node *Pointee_)
603 : Node(KPointerType, Pointee_->RHSComponentCache),
604 Pointee(Pointee_) {}
605
606 template<typename Fn> void match(Fn F) const { F(Pointee); }
607
608 bool hasRHSComponentSlow(OutputStream &S) const override {
609 return Pointee->hasRHSComponent(S);
610 }
611
612 void printLeft(OutputStream &s) const override {
613 // We rewrite objc_object<SomeProtocol>* into id<SomeProtocol>.
614 if (Pointee->getKind() != KObjCProtoName ||
615 !static_cast<const ObjCProtoName *>(Pointee)->isObjCObject()) {
616 Pointee->printLeft(s);
617 if (Pointee->hasArray(s))
618 s += " ";
619 if (Pointee->hasArray(s) || Pointee->hasFunction(s))
620 s += "(";
621 s += "*";
622 } else {
623 const auto *objcProto = static_cast<const ObjCProtoName *>(Pointee);
624 s += "id<";
625 s += objcProto->Protocol;
626 s += ">";
627 }
628 }
629
630 void printRight(OutputStream &s) const override {
631 if (Pointee->getKind() != KObjCProtoName ||
632 !static_cast<const ObjCProtoName *>(Pointee)->isObjCObject()) {
633 if (Pointee->hasArray(s) || Pointee->hasFunction(s))
634 s += ")";
635 Pointee->printRight(s);
636 }
637 }
638};
639
640enum class ReferenceKind {
641 LValue,
642 RValue,
643};
644
645// Represents either a LValue or an RValue reference type.
646class ReferenceType : public Node {
647 const Node *Pointee;
648 ReferenceKind RK;
649
650 mutable bool Printing = false;
651
652 // Dig through any refs to refs, collapsing the ReferenceTypes as we go. The
653 // rule here is rvalue ref to rvalue ref collapses to a rvalue ref, and any
654 // other combination collapses to a lvalue ref.
Mikhail Borisov05f77222021-08-17 18:10:57 -0400655 //
656 // A combination of a TemplateForwardReference and a back-ref Substitution
657 // from an ill-formed string may have created a cycle; use cycle detection to
658 // avoid looping forever.
Richard Smithc20d1442018-08-20 20:14:49 +0000659 std::pair<ReferenceKind, const Node *> collapse(OutputStream &S) const {
660 auto SoFar = std::make_pair(RK, Pointee);
Mikhail Borisov05f77222021-08-17 18:10:57 -0400661 // Track the chain of nodes for the Floyd's 'tortoise and hare'
662 // cycle-detection algorithm, since getSyntaxNode(S) is impure
663 PODSmallVector<const Node *, 8> Prev;
Richard Smithc20d1442018-08-20 20:14:49 +0000664 for (;;) {
665 const Node *SN = SoFar.second->getSyntaxNode(S);
666 if (SN->getKind() != KReferenceType)
667 break;
668 auto *RT = static_cast<const ReferenceType *>(SN);
669 SoFar.second = RT->Pointee;
670 SoFar.first = std::min(SoFar.first, RT->RK);
Mikhail Borisov05f77222021-08-17 18:10:57 -0400671
672 // The middle of Prev is the 'slow' pointer moving at half speed
673 Prev.push_back(SoFar.second);
674 if (Prev.size() > 1 && SoFar.second == Prev[(Prev.size() - 1) / 2]) {
675 // Cycle detected
676 SoFar.second = nullptr;
677 break;
678 }
Richard Smithc20d1442018-08-20 20:14:49 +0000679 }
680 return SoFar;
681 }
682
683public:
684 ReferenceType(const Node *Pointee_, ReferenceKind RK_)
685 : Node(KReferenceType, Pointee_->RHSComponentCache),
686 Pointee(Pointee_), RK(RK_) {}
687
688 template<typename Fn> void match(Fn F) const { F(Pointee, RK); }
689
690 bool hasRHSComponentSlow(OutputStream &S) const override {
691 return Pointee->hasRHSComponent(S);
692 }
693
694 void printLeft(OutputStream &s) const override {
695 if (Printing)
696 return;
697 SwapAndRestore<bool> SavePrinting(Printing, true);
698 std::pair<ReferenceKind, const Node *> Collapsed = collapse(s);
Mikhail Borisov05f77222021-08-17 18:10:57 -0400699 if (!Collapsed.second)
700 return;
Richard Smithc20d1442018-08-20 20:14:49 +0000701 Collapsed.second->printLeft(s);
702 if (Collapsed.second->hasArray(s))
703 s += " ";
704 if (Collapsed.second->hasArray(s) || Collapsed.second->hasFunction(s))
705 s += "(";
706
707 s += (Collapsed.first == ReferenceKind::LValue ? "&" : "&&");
708 }
709 void printRight(OutputStream &s) const override {
710 if (Printing)
711 return;
712 SwapAndRestore<bool> SavePrinting(Printing, true);
713 std::pair<ReferenceKind, const Node *> Collapsed = collapse(s);
Mikhail Borisov05f77222021-08-17 18:10:57 -0400714 if (!Collapsed.second)
715 return;
Richard Smithc20d1442018-08-20 20:14:49 +0000716 if (Collapsed.second->hasArray(s) || Collapsed.second->hasFunction(s))
717 s += ")";
718 Collapsed.second->printRight(s);
719 }
720};
721
722class PointerToMemberType final : public Node {
723 const Node *ClassType;
724 const Node *MemberType;
725
726public:
727 PointerToMemberType(const Node *ClassType_, const Node *MemberType_)
728 : Node(KPointerToMemberType, MemberType_->RHSComponentCache),
729 ClassType(ClassType_), MemberType(MemberType_) {}
730
731 template<typename Fn> void match(Fn F) const { F(ClassType, MemberType); }
732
733 bool hasRHSComponentSlow(OutputStream &S) const override {
734 return MemberType->hasRHSComponent(S);
735 }
736
737 void printLeft(OutputStream &s) const override {
738 MemberType->printLeft(s);
739 if (MemberType->hasArray(s) || MemberType->hasFunction(s))
740 s += "(";
741 else
742 s += " ";
743 ClassType->print(s);
744 s += "::*";
745 }
746
747 void printRight(OutputStream &s) const override {
748 if (MemberType->hasArray(s) || MemberType->hasFunction(s))
749 s += ")";
750 MemberType->printRight(s);
751 }
752};
753
Richard Smithc20d1442018-08-20 20:14:49 +0000754class ArrayType final : public Node {
755 const Node *Base;
Erik Pilkingtond7555e32019-11-04 10:47:44 -0800756 Node *Dimension;
Richard Smithc20d1442018-08-20 20:14:49 +0000757
758public:
Erik Pilkingtond7555e32019-11-04 10:47:44 -0800759 ArrayType(const Node *Base_, Node *Dimension_)
Richard Smithc20d1442018-08-20 20:14:49 +0000760 : Node(KArrayType,
761 /*RHSComponentCache=*/Cache::Yes,
762 /*ArrayCache=*/Cache::Yes),
763 Base(Base_), Dimension(Dimension_) {}
764
765 template<typename Fn> void match(Fn F) const { F(Base, Dimension); }
766
767 bool hasRHSComponentSlow(OutputStream &) const override { return true; }
768 bool hasArraySlow(OutputStream &) const override { return true; }
769
770 void printLeft(OutputStream &S) const override { Base->printLeft(S); }
771
772 void printRight(OutputStream &S) const override {
773 if (S.back() != ']')
774 S += " ";
775 S += "[";
Erik Pilkingtond7555e32019-11-04 10:47:44 -0800776 if (Dimension)
777 Dimension->print(S);
Richard Smithc20d1442018-08-20 20:14:49 +0000778 S += "]";
779 Base->printRight(S);
780 }
781};
782
783class FunctionType final : public Node {
784 const Node *Ret;
785 NodeArray Params;
786 Qualifiers CVQuals;
787 FunctionRefQual RefQual;
788 const Node *ExceptionSpec;
789
790public:
791 FunctionType(const Node *Ret_, NodeArray Params_, Qualifiers CVQuals_,
792 FunctionRefQual RefQual_, const Node *ExceptionSpec_)
793 : Node(KFunctionType,
794 /*RHSComponentCache=*/Cache::Yes, /*ArrayCache=*/Cache::No,
795 /*FunctionCache=*/Cache::Yes),
796 Ret(Ret_), Params(Params_), CVQuals(CVQuals_), RefQual(RefQual_),
797 ExceptionSpec(ExceptionSpec_) {}
798
799 template<typename Fn> void match(Fn F) const {
800 F(Ret, Params, CVQuals, RefQual, ExceptionSpec);
801 }
802
803 bool hasRHSComponentSlow(OutputStream &) const override { return true; }
804 bool hasFunctionSlow(OutputStream &) const override { return true; }
805
806 // Handle C++'s ... quirky decl grammar by using the left & right
807 // distinction. Consider:
808 // int (*f(float))(char) {}
809 // f is a function that takes a float and returns a pointer to a function
810 // that takes a char and returns an int. If we're trying to print f, start
811 // by printing out the return types's left, then print our parameters, then
812 // finally print right of the return type.
813 void printLeft(OutputStream &S) const override {
814 Ret->printLeft(S);
815 S += " ";
816 }
817
818 void printRight(OutputStream &S) const override {
819 S += "(";
820 Params.printWithComma(S);
821 S += ")";
822 Ret->printRight(S);
823
824 if (CVQuals & QualConst)
825 S += " const";
826 if (CVQuals & QualVolatile)
827 S += " volatile";
828 if (CVQuals & QualRestrict)
829 S += " restrict";
830
831 if (RefQual == FrefQualLValue)
832 S += " &";
833 else if (RefQual == FrefQualRValue)
834 S += " &&";
835
836 if (ExceptionSpec != nullptr) {
837 S += ' ';
838 ExceptionSpec->print(S);
839 }
840 }
841};
842
843class NoexceptSpec : public Node {
844 const Node *E;
845public:
846 NoexceptSpec(const Node *E_) : Node(KNoexceptSpec), E(E_) {}
847
848 template<typename Fn> void match(Fn F) const { F(E); }
849
850 void printLeft(OutputStream &S) const override {
851 S += "noexcept(";
852 E->print(S);
853 S += ")";
854 }
855};
856
857class DynamicExceptionSpec : public Node {
858 NodeArray Types;
859public:
860 DynamicExceptionSpec(NodeArray Types_)
861 : Node(KDynamicExceptionSpec), Types(Types_) {}
862
863 template<typename Fn> void match(Fn F) const { F(Types); }
864
865 void printLeft(OutputStream &S) const override {
866 S += "throw(";
867 Types.printWithComma(S);
868 S += ')';
869 }
870};
871
872class FunctionEncoding final : public Node {
873 const Node *Ret;
874 const Node *Name;
875 NodeArray Params;
876 const Node *Attrs;
877 Qualifiers CVQuals;
878 FunctionRefQual RefQual;
879
880public:
881 FunctionEncoding(const Node *Ret_, const Node *Name_, NodeArray Params_,
882 const Node *Attrs_, Qualifiers CVQuals_,
883 FunctionRefQual RefQual_)
884 : Node(KFunctionEncoding,
885 /*RHSComponentCache=*/Cache::Yes, /*ArrayCache=*/Cache::No,
886 /*FunctionCache=*/Cache::Yes),
887 Ret(Ret_), Name(Name_), Params(Params_), Attrs(Attrs_),
888 CVQuals(CVQuals_), RefQual(RefQual_) {}
889
890 template<typename Fn> void match(Fn F) const {
891 F(Ret, Name, Params, Attrs, CVQuals, RefQual);
892 }
893
894 Qualifiers getCVQuals() const { return CVQuals; }
895 FunctionRefQual getRefQual() const { return RefQual; }
896 NodeArray getParams() const { return Params; }
897 const Node *getReturnType() const { return Ret; }
898
899 bool hasRHSComponentSlow(OutputStream &) const override { return true; }
900 bool hasFunctionSlow(OutputStream &) const override { return true; }
901
902 const Node *getName() const { return Name; }
903
904 void printLeft(OutputStream &S) const override {
905 if (Ret) {
906 Ret->printLeft(S);
907 if (!Ret->hasRHSComponent(S))
908 S += " ";
909 }
910 Name->print(S);
911 }
912
913 void printRight(OutputStream &S) const override {
914 S += "(";
915 Params.printWithComma(S);
916 S += ")";
917 if (Ret)
918 Ret->printRight(S);
919
920 if (CVQuals & QualConst)
921 S += " const";
922 if (CVQuals & QualVolatile)
923 S += " volatile";
924 if (CVQuals & QualRestrict)
925 S += " restrict";
926
927 if (RefQual == FrefQualLValue)
928 S += " &";
929 else if (RefQual == FrefQualRValue)
930 S += " &&";
931
932 if (Attrs != nullptr)
933 Attrs->print(S);
934 }
935};
936
937class LiteralOperator : public Node {
938 const Node *OpName;
939
940public:
941 LiteralOperator(const Node *OpName_)
942 : Node(KLiteralOperator), OpName(OpName_) {}
943
944 template<typename Fn> void match(Fn F) const { F(OpName); }
945
946 void printLeft(OutputStream &S) const override {
947 S += "operator\"\" ";
948 OpName->print(S);
949 }
950};
951
952class SpecialName final : public Node {
953 const StringView Special;
954 const Node *Child;
955
956public:
957 SpecialName(StringView Special_, const Node *Child_)
958 : Node(KSpecialName), Special(Special_), Child(Child_) {}
959
960 template<typename Fn> void match(Fn F) const { F(Special, Child); }
961
962 void printLeft(OutputStream &S) const override {
963 S += Special;
964 Child->print(S);
965 }
966};
967
968class CtorVtableSpecialName final : public Node {
969 const Node *FirstType;
970 const Node *SecondType;
971
972public:
973 CtorVtableSpecialName(const Node *FirstType_, const Node *SecondType_)
974 : Node(KCtorVtableSpecialName),
975 FirstType(FirstType_), SecondType(SecondType_) {}
976
977 template<typename Fn> void match(Fn F) const { F(FirstType, SecondType); }
978
979 void printLeft(OutputStream &S) const override {
980 S += "construction vtable for ";
981 FirstType->print(S);
982 S += "-in-";
983 SecondType->print(S);
984 }
985};
986
987struct NestedName : Node {
988 Node *Qual;
989 Node *Name;
990
991 NestedName(Node *Qual_, Node *Name_)
992 : Node(KNestedName), Qual(Qual_), Name(Name_) {}
993
994 template<typename Fn> void match(Fn F) const { F(Qual, Name); }
995
996 StringView getBaseName() const override { return Name->getBaseName(); }
997
998 void printLeft(OutputStream &S) const override {
999 Qual->print(S);
1000 S += "::";
1001 Name->print(S);
1002 }
1003};
1004
1005struct LocalName : Node {
1006 Node *Encoding;
1007 Node *Entity;
1008
1009 LocalName(Node *Encoding_, Node *Entity_)
1010 : Node(KLocalName), Encoding(Encoding_), Entity(Entity_) {}
1011
1012 template<typename Fn> void match(Fn F) const { F(Encoding, Entity); }
1013
1014 void printLeft(OutputStream &S) const override {
1015 Encoding->print(S);
1016 S += "::";
1017 Entity->print(S);
1018 }
1019};
1020
1021class QualifiedName final : public Node {
1022 // qualifier::name
1023 const Node *Qualifier;
1024 const Node *Name;
1025
1026public:
1027 QualifiedName(const Node *Qualifier_, const Node *Name_)
1028 : Node(KQualifiedName), Qualifier(Qualifier_), Name(Name_) {}
1029
1030 template<typename Fn> void match(Fn F) const { F(Qualifier, Name); }
1031
1032 StringView getBaseName() const override { return Name->getBaseName(); }
1033
1034 void printLeft(OutputStream &S) const override {
1035 Qualifier->print(S);
1036 S += "::";
1037 Name->print(S);
1038 }
1039};
1040
1041class VectorType final : public Node {
1042 const Node *BaseType;
Erik Pilkingtond7555e32019-11-04 10:47:44 -08001043 const Node *Dimension;
Richard Smithc20d1442018-08-20 20:14:49 +00001044
1045public:
Erik Pilkingtond7555e32019-11-04 10:47:44 -08001046 VectorType(const Node *BaseType_, Node *Dimension_)
Richard Smithc20d1442018-08-20 20:14:49 +00001047 : Node(KVectorType), BaseType(BaseType_),
1048 Dimension(Dimension_) {}
1049
1050 template<typename Fn> void match(Fn F) const { F(BaseType, Dimension); }
1051
1052 void printLeft(OutputStream &S) const override {
1053 BaseType->print(S);
1054 S += " vector[";
Erik Pilkingtond7555e32019-11-04 10:47:44 -08001055 if (Dimension)
1056 Dimension->print(S);
Richard Smithc20d1442018-08-20 20:14:49 +00001057 S += "]";
1058 }
1059};
1060
1061class PixelVectorType final : public Node {
Erik Pilkingtond7555e32019-11-04 10:47:44 -08001062 const Node *Dimension;
Richard Smithc20d1442018-08-20 20:14:49 +00001063
1064public:
Erik Pilkingtond7555e32019-11-04 10:47:44 -08001065 PixelVectorType(const Node *Dimension_)
Richard Smithc20d1442018-08-20 20:14:49 +00001066 : Node(KPixelVectorType), Dimension(Dimension_) {}
1067
1068 template<typename Fn> void match(Fn F) const { F(Dimension); }
1069
1070 void printLeft(OutputStream &S) const override {
1071 // FIXME: This should demangle as "vector pixel".
1072 S += "pixel vector[";
Erik Pilkingtond7555e32019-11-04 10:47:44 -08001073 Dimension->print(S);
Richard Smithc20d1442018-08-20 20:14:49 +00001074 S += "]";
1075 }
1076};
1077
Pengfei Wang50e90b82021-09-23 11:02:25 +08001078class BinaryFPType final : public Node {
1079 const Node *Dimension;
1080
1081public:
1082 BinaryFPType(const Node *Dimension_)
1083 : Node(KBinaryFPType), Dimension(Dimension_) {}
1084
1085 template<typename Fn> void match(Fn F) const { F(Dimension); }
1086
1087 void printLeft(OutputStream &S) const override {
1088 S += "_Float";
1089 Dimension->print(S);
1090 }
1091};
1092
Richard Smithdf1c14c2019-09-06 23:53:21 +00001093enum class TemplateParamKind { Type, NonType, Template };
1094
1095/// An invented name for a template parameter for which we don't have a
1096/// corresponding template argument.
1097///
1098/// This node is created when parsing the <lambda-sig> for a lambda with
1099/// explicit template arguments, which might be referenced in the parameter
1100/// types appearing later in the <lambda-sig>.
1101class SyntheticTemplateParamName final : public Node {
1102 TemplateParamKind Kind;
1103 unsigned Index;
1104
1105public:
1106 SyntheticTemplateParamName(TemplateParamKind Kind_, unsigned Index_)
1107 : Node(KSyntheticTemplateParamName), Kind(Kind_), Index(Index_) {}
1108
1109 template<typename Fn> void match(Fn F) const { F(Kind, Index); }
1110
1111 void printLeft(OutputStream &S) const override {
1112 switch (Kind) {
1113 case TemplateParamKind::Type:
1114 S += "$T";
1115 break;
1116 case TemplateParamKind::NonType:
1117 S += "$N";
1118 break;
1119 case TemplateParamKind::Template:
1120 S += "$TT";
1121 break;
1122 }
1123 if (Index > 0)
1124 S << Index - 1;
1125 }
1126};
1127
1128/// A template type parameter declaration, 'typename T'.
1129class TypeTemplateParamDecl final : public Node {
1130 Node *Name;
1131
1132public:
1133 TypeTemplateParamDecl(Node *Name_)
1134 : Node(KTypeTemplateParamDecl, Cache::Yes), Name(Name_) {}
1135
1136 template<typename Fn> void match(Fn F) const { F(Name); }
1137
1138 void printLeft(OutputStream &S) const override {
1139 S += "typename ";
1140 }
1141
1142 void printRight(OutputStream &S) const override {
1143 Name->print(S);
1144 }
1145};
1146
1147/// A non-type template parameter declaration, 'int N'.
1148class NonTypeTemplateParamDecl final : public Node {
1149 Node *Name;
1150 Node *Type;
1151
1152public:
1153 NonTypeTemplateParamDecl(Node *Name_, Node *Type_)
1154 : Node(KNonTypeTemplateParamDecl, Cache::Yes), Name(Name_), Type(Type_) {}
1155
1156 template<typename Fn> void match(Fn F) const { F(Name, Type); }
1157
1158 void printLeft(OutputStream &S) const override {
1159 Type->printLeft(S);
1160 if (!Type->hasRHSComponent(S))
1161 S += " ";
1162 }
1163
1164 void printRight(OutputStream &S) const override {
1165 Name->print(S);
1166 Type->printRight(S);
1167 }
1168};
1169
1170/// A template template parameter declaration,
1171/// 'template<typename T> typename N'.
1172class TemplateTemplateParamDecl final : public Node {
1173 Node *Name;
1174 NodeArray Params;
1175
1176public:
1177 TemplateTemplateParamDecl(Node *Name_, NodeArray Params_)
1178 : Node(KTemplateTemplateParamDecl, Cache::Yes), Name(Name_),
1179 Params(Params_) {}
1180
1181 template<typename Fn> void match(Fn F) const { F(Name, Params); }
1182
1183 void printLeft(OutputStream &S) const override {
1184 S += "template<";
1185 Params.printWithComma(S);
1186 S += "> typename ";
1187 }
1188
1189 void printRight(OutputStream &S) const override {
1190 Name->print(S);
1191 }
1192};
1193
1194/// A template parameter pack declaration, 'typename ...T'.
1195class TemplateParamPackDecl final : public Node {
1196 Node *Param;
1197
1198public:
1199 TemplateParamPackDecl(Node *Param_)
1200 : Node(KTemplateParamPackDecl, Cache::Yes), Param(Param_) {}
1201
1202 template<typename Fn> void match(Fn F) const { F(Param); }
1203
1204 void printLeft(OutputStream &S) const override {
1205 Param->printLeft(S);
1206 S += "...";
1207 }
1208
1209 void printRight(OutputStream &S) const override {
1210 Param->printRight(S);
1211 }
1212};
1213
Richard Smithc20d1442018-08-20 20:14:49 +00001214/// An unexpanded parameter pack (either in the expression or type context). If
1215/// this AST is correct, this node will have a ParameterPackExpansion node above
1216/// it.
1217///
1218/// This node is created when some <template-args> are found that apply to an
1219/// <encoding>, and is stored in the TemplateParams table. In order for this to
1220/// appear in the final AST, it has to referenced via a <template-param> (ie,
1221/// T_).
1222class ParameterPack final : public Node {
1223 NodeArray Data;
1224
1225 // Setup OutputStream for a pack expansion unless we're already expanding one.
1226 void initializePackExpansion(OutputStream &S) const {
1227 if (S.CurrentPackMax == std::numeric_limits<unsigned>::max()) {
1228 S.CurrentPackMax = static_cast<unsigned>(Data.size());
1229 S.CurrentPackIndex = 0;
1230 }
1231 }
1232
1233public:
1234 ParameterPack(NodeArray Data_) : Node(KParameterPack), Data(Data_) {
1235 ArrayCache = FunctionCache = RHSComponentCache = Cache::Unknown;
1236 if (std::all_of(Data.begin(), Data.end(), [](Node* P) {
1237 return P->ArrayCache == Cache::No;
1238 }))
1239 ArrayCache = Cache::No;
1240 if (std::all_of(Data.begin(), Data.end(), [](Node* P) {
1241 return P->FunctionCache == Cache::No;
1242 }))
1243 FunctionCache = Cache::No;
1244 if (std::all_of(Data.begin(), Data.end(), [](Node* P) {
1245 return P->RHSComponentCache == Cache::No;
1246 }))
1247 RHSComponentCache = Cache::No;
1248 }
1249
1250 template<typename Fn> void match(Fn F) const { F(Data); }
1251
1252 bool hasRHSComponentSlow(OutputStream &S) const override {
1253 initializePackExpansion(S);
1254 size_t Idx = S.CurrentPackIndex;
1255 return Idx < Data.size() && Data[Idx]->hasRHSComponent(S);
1256 }
1257 bool hasArraySlow(OutputStream &S) const override {
1258 initializePackExpansion(S);
1259 size_t Idx = S.CurrentPackIndex;
1260 return Idx < Data.size() && Data[Idx]->hasArray(S);
1261 }
1262 bool hasFunctionSlow(OutputStream &S) const override {
1263 initializePackExpansion(S);
1264 size_t Idx = S.CurrentPackIndex;
1265 return Idx < Data.size() && Data[Idx]->hasFunction(S);
1266 }
1267 const Node *getSyntaxNode(OutputStream &S) const override {
1268 initializePackExpansion(S);
1269 size_t Idx = S.CurrentPackIndex;
1270 return Idx < Data.size() ? Data[Idx]->getSyntaxNode(S) : this;
1271 }
1272
1273 void printLeft(OutputStream &S) const override {
1274 initializePackExpansion(S);
1275 size_t Idx = S.CurrentPackIndex;
1276 if (Idx < Data.size())
1277 Data[Idx]->printLeft(S);
1278 }
1279 void printRight(OutputStream &S) const override {
1280 initializePackExpansion(S);
1281 size_t Idx = S.CurrentPackIndex;
1282 if (Idx < Data.size())
1283 Data[Idx]->printRight(S);
1284 }
1285};
1286
1287/// A variadic template argument. This node represents an occurrence of
1288/// J<something>E in some <template-args>. It isn't itself unexpanded, unless
1289/// one of it's Elements is. The parser inserts a ParameterPack into the
1290/// TemplateParams table if the <template-args> this pack belongs to apply to an
1291/// <encoding>.
1292class TemplateArgumentPack final : public Node {
1293 NodeArray Elements;
1294public:
1295 TemplateArgumentPack(NodeArray Elements_)
1296 : Node(KTemplateArgumentPack), Elements(Elements_) {}
1297
1298 template<typename Fn> void match(Fn F) const { F(Elements); }
1299
1300 NodeArray getElements() const { return Elements; }
1301
1302 void printLeft(OutputStream &S) const override {
1303 Elements.printWithComma(S);
1304 }
1305};
1306
1307/// A pack expansion. Below this node, there are some unexpanded ParameterPacks
1308/// which each have Child->ParameterPackSize elements.
1309class ParameterPackExpansion final : public Node {
1310 const Node *Child;
1311
1312public:
1313 ParameterPackExpansion(const Node *Child_)
1314 : Node(KParameterPackExpansion), Child(Child_) {}
1315
1316 template<typename Fn> void match(Fn F) const { F(Child); }
1317
1318 const Node *getChild() const { return Child; }
1319
1320 void printLeft(OutputStream &S) const override {
1321 constexpr unsigned Max = std::numeric_limits<unsigned>::max();
1322 SwapAndRestore<unsigned> SavePackIdx(S.CurrentPackIndex, Max);
1323 SwapAndRestore<unsigned> SavePackMax(S.CurrentPackMax, Max);
1324 size_t StreamPos = S.getCurrentPosition();
1325
1326 // Print the first element in the pack. If Child contains a ParameterPack,
1327 // it will set up S.CurrentPackMax and print the first element.
1328 Child->print(S);
1329
1330 // No ParameterPack was found in Child. This can occur if we've found a pack
1331 // expansion on a <function-param>.
1332 if (S.CurrentPackMax == Max) {
1333 S += "...";
1334 return;
1335 }
1336
1337 // We found a ParameterPack, but it has no elements. Erase whatever we may
1338 // of printed.
1339 if (S.CurrentPackMax == 0) {
1340 S.setCurrentPosition(StreamPos);
1341 return;
1342 }
1343
1344 // Else, iterate through the rest of the elements in the pack.
1345 for (unsigned I = 1, E = S.CurrentPackMax; I < E; ++I) {
1346 S += ", ";
1347 S.CurrentPackIndex = I;
1348 Child->print(S);
1349 }
1350 }
1351};
1352
1353class TemplateArgs final : public Node {
1354 NodeArray Params;
1355
1356public:
1357 TemplateArgs(NodeArray Params_) : Node(KTemplateArgs), Params(Params_) {}
1358
1359 template<typename Fn> void match(Fn F) const { F(Params); }
1360
1361 NodeArray getParams() { return Params; }
1362
1363 void printLeft(OutputStream &S) const override {
1364 S += "<";
1365 Params.printWithComma(S);
1366 if (S.back() == '>')
1367 S += " ";
1368 S += ">";
1369 }
1370};
1371
Richard Smithb485b352018-08-24 23:30:26 +00001372/// A forward-reference to a template argument that was not known at the point
1373/// where the template parameter name was parsed in a mangling.
1374///
1375/// This is created when demangling the name of a specialization of a
1376/// conversion function template:
1377///
1378/// \code
1379/// struct A {
1380/// template<typename T> operator T*();
1381/// };
1382/// \endcode
1383///
1384/// When demangling a specialization of the conversion function template, we
1385/// encounter the name of the template (including the \c T) before we reach
1386/// the template argument list, so we cannot substitute the parameter name
1387/// for the corresponding argument while parsing. Instead, we create a
1388/// \c ForwardTemplateReference node that is resolved after we parse the
1389/// template arguments.
Richard Smithc20d1442018-08-20 20:14:49 +00001390struct ForwardTemplateReference : Node {
1391 size_t Index;
1392 Node *Ref = nullptr;
1393
1394 // If we're currently printing this node. It is possible (though invalid) for
1395 // a forward template reference to refer to itself via a substitution. This
1396 // creates a cyclic AST, which will stack overflow printing. To fix this, bail
1397 // out if more than one print* function is active.
1398 mutable bool Printing = false;
1399
1400 ForwardTemplateReference(size_t Index_)
1401 : Node(KForwardTemplateReference, Cache::Unknown, Cache::Unknown,
1402 Cache::Unknown),
1403 Index(Index_) {}
1404
1405 // We don't provide a matcher for these, because the value of the node is
1406 // not determined by its construction parameters, and it generally needs
1407 // special handling.
1408 template<typename Fn> void match(Fn F) const = delete;
1409
1410 bool hasRHSComponentSlow(OutputStream &S) const override {
1411 if (Printing)
1412 return false;
1413 SwapAndRestore<bool> SavePrinting(Printing, true);
1414 return Ref->hasRHSComponent(S);
1415 }
1416 bool hasArraySlow(OutputStream &S) const override {
1417 if (Printing)
1418 return false;
1419 SwapAndRestore<bool> SavePrinting(Printing, true);
1420 return Ref->hasArray(S);
1421 }
1422 bool hasFunctionSlow(OutputStream &S) const override {
1423 if (Printing)
1424 return false;
1425 SwapAndRestore<bool> SavePrinting(Printing, true);
1426 return Ref->hasFunction(S);
1427 }
1428 const Node *getSyntaxNode(OutputStream &S) const override {
1429 if (Printing)
1430 return this;
1431 SwapAndRestore<bool> SavePrinting(Printing, true);
1432 return Ref->getSyntaxNode(S);
1433 }
1434
1435 void printLeft(OutputStream &S) const override {
1436 if (Printing)
1437 return;
1438 SwapAndRestore<bool> SavePrinting(Printing, true);
1439 Ref->printLeft(S);
1440 }
1441 void printRight(OutputStream &S) const override {
1442 if (Printing)
1443 return;
1444 SwapAndRestore<bool> SavePrinting(Printing, true);
1445 Ref->printRight(S);
1446 }
1447};
1448
1449struct NameWithTemplateArgs : Node {
1450 // name<template_args>
1451 Node *Name;
1452 Node *TemplateArgs;
1453
1454 NameWithTemplateArgs(Node *Name_, Node *TemplateArgs_)
1455 : Node(KNameWithTemplateArgs), Name(Name_), TemplateArgs(TemplateArgs_) {}
1456
1457 template<typename Fn> void match(Fn F) const { F(Name, TemplateArgs); }
1458
1459 StringView getBaseName() const override { return Name->getBaseName(); }
1460
1461 void printLeft(OutputStream &S) const override {
1462 Name->print(S);
1463 TemplateArgs->print(S);
1464 }
1465};
1466
1467class GlobalQualifiedName final : public Node {
1468 Node *Child;
1469
1470public:
1471 GlobalQualifiedName(Node* Child_)
1472 : Node(KGlobalQualifiedName), Child(Child_) {}
1473
1474 template<typename Fn> void match(Fn F) const { F(Child); }
1475
1476 StringView getBaseName() const override { return Child->getBaseName(); }
1477
1478 void printLeft(OutputStream &S) const override {
1479 S += "::";
1480 Child->print(S);
1481 }
1482};
1483
1484struct StdQualifiedName : Node {
1485 Node *Child;
1486
1487 StdQualifiedName(Node *Child_) : Node(KStdQualifiedName), Child(Child_) {}
1488
1489 template<typename Fn> void match(Fn F) const { F(Child); }
1490
1491 StringView getBaseName() const override { return Child->getBaseName(); }
1492
1493 void printLeft(OutputStream &S) const override {
1494 S += "std::";
1495 Child->print(S);
1496 }
1497};
1498
1499enum class SpecialSubKind {
1500 allocator,
1501 basic_string,
1502 string,
1503 istream,
1504 ostream,
1505 iostream,
1506};
1507
1508class ExpandedSpecialSubstitution final : public Node {
1509 SpecialSubKind SSK;
1510
1511public:
1512 ExpandedSpecialSubstitution(SpecialSubKind SSK_)
1513 : Node(KExpandedSpecialSubstitution), SSK(SSK_) {}
1514
1515 template<typename Fn> void match(Fn F) const { F(SSK); }
1516
1517 StringView getBaseName() const override {
1518 switch (SSK) {
1519 case SpecialSubKind::allocator:
1520 return StringView("allocator");
1521 case SpecialSubKind::basic_string:
1522 return StringView("basic_string");
1523 case SpecialSubKind::string:
1524 return StringView("basic_string");
1525 case SpecialSubKind::istream:
1526 return StringView("basic_istream");
1527 case SpecialSubKind::ostream:
1528 return StringView("basic_ostream");
1529 case SpecialSubKind::iostream:
1530 return StringView("basic_iostream");
1531 }
Erik Pilkingtonf70e4d82019-01-17 20:37:51 +00001532 DEMANGLE_UNREACHABLE;
Richard Smithc20d1442018-08-20 20:14:49 +00001533 }
1534
1535 void printLeft(OutputStream &S) const override {
1536 switch (SSK) {
1537 case SpecialSubKind::allocator:
Richard Smithb485b352018-08-24 23:30:26 +00001538 S += "std::allocator";
Richard Smithc20d1442018-08-20 20:14:49 +00001539 break;
1540 case SpecialSubKind::basic_string:
Richard Smithb485b352018-08-24 23:30:26 +00001541 S += "std::basic_string";
1542 break;
Richard Smithc20d1442018-08-20 20:14:49 +00001543 case SpecialSubKind::string:
1544 S += "std::basic_string<char, std::char_traits<char>, "
1545 "std::allocator<char> >";
1546 break;
1547 case SpecialSubKind::istream:
1548 S += "std::basic_istream<char, std::char_traits<char> >";
1549 break;
1550 case SpecialSubKind::ostream:
1551 S += "std::basic_ostream<char, std::char_traits<char> >";
1552 break;
1553 case SpecialSubKind::iostream:
1554 S += "std::basic_iostream<char, std::char_traits<char> >";
1555 break;
1556 }
1557 }
1558};
1559
1560class SpecialSubstitution final : public Node {
1561public:
1562 SpecialSubKind SSK;
1563
1564 SpecialSubstitution(SpecialSubKind SSK_)
1565 : Node(KSpecialSubstitution), SSK(SSK_) {}
1566
1567 template<typename Fn> void match(Fn F) const { F(SSK); }
1568
1569 StringView getBaseName() const override {
1570 switch (SSK) {
1571 case SpecialSubKind::allocator:
1572 return StringView("allocator");
1573 case SpecialSubKind::basic_string:
1574 return StringView("basic_string");
1575 case SpecialSubKind::string:
1576 return StringView("string");
1577 case SpecialSubKind::istream:
1578 return StringView("istream");
1579 case SpecialSubKind::ostream:
1580 return StringView("ostream");
1581 case SpecialSubKind::iostream:
1582 return StringView("iostream");
1583 }
Erik Pilkingtonf70e4d82019-01-17 20:37:51 +00001584 DEMANGLE_UNREACHABLE;
Richard Smithc20d1442018-08-20 20:14:49 +00001585 }
1586
1587 void printLeft(OutputStream &S) const override {
1588 switch (SSK) {
1589 case SpecialSubKind::allocator:
1590 S += "std::allocator";
1591 break;
1592 case SpecialSubKind::basic_string:
1593 S += "std::basic_string";
1594 break;
1595 case SpecialSubKind::string:
1596 S += "std::string";
1597 break;
1598 case SpecialSubKind::istream:
1599 S += "std::istream";
1600 break;
1601 case SpecialSubKind::ostream:
1602 S += "std::ostream";
1603 break;
1604 case SpecialSubKind::iostream:
1605 S += "std::iostream";
1606 break;
1607 }
1608 }
1609};
1610
1611class CtorDtorName final : public Node {
1612 const Node *Basename;
1613 const bool IsDtor;
Pavel Labathf4e67eb2018-10-10 08:39:16 +00001614 const int Variant;
Richard Smithc20d1442018-08-20 20:14:49 +00001615
1616public:
Pavel Labathf4e67eb2018-10-10 08:39:16 +00001617 CtorDtorName(const Node *Basename_, bool IsDtor_, int Variant_)
1618 : Node(KCtorDtorName), Basename(Basename_), IsDtor(IsDtor_),
1619 Variant(Variant_) {}
Richard Smithc20d1442018-08-20 20:14:49 +00001620
Pavel Labathf4e67eb2018-10-10 08:39:16 +00001621 template<typename Fn> void match(Fn F) const { F(Basename, IsDtor, Variant); }
Richard Smithc20d1442018-08-20 20:14:49 +00001622
1623 void printLeft(OutputStream &S) const override {
1624 if (IsDtor)
1625 S += "~";
1626 S += Basename->getBaseName();
1627 }
1628};
1629
1630class DtorName : public Node {
1631 const Node *Base;
1632
1633public:
1634 DtorName(const Node *Base_) : Node(KDtorName), Base(Base_) {}
1635
1636 template<typename Fn> void match(Fn F) const { F(Base); }
1637
1638 void printLeft(OutputStream &S) const override {
1639 S += "~";
1640 Base->printLeft(S);
1641 }
1642};
1643
1644class UnnamedTypeName : public Node {
1645 const StringView Count;
1646
1647public:
1648 UnnamedTypeName(StringView Count_) : Node(KUnnamedTypeName), Count(Count_) {}
1649
1650 template<typename Fn> void match(Fn F) const { F(Count); }
1651
1652 void printLeft(OutputStream &S) const override {
1653 S += "'unnamed";
1654 S += Count;
1655 S += "\'";
1656 }
1657};
1658
1659class ClosureTypeName : public Node {
Richard Smithdf1c14c2019-09-06 23:53:21 +00001660 NodeArray TemplateParams;
Richard Smithc20d1442018-08-20 20:14:49 +00001661 NodeArray Params;
1662 StringView Count;
1663
1664public:
Richard Smithdf1c14c2019-09-06 23:53:21 +00001665 ClosureTypeName(NodeArray TemplateParams_, NodeArray Params_,
1666 StringView Count_)
1667 : Node(KClosureTypeName), TemplateParams(TemplateParams_),
1668 Params(Params_), Count(Count_) {}
Richard Smithc20d1442018-08-20 20:14:49 +00001669
Richard Smithdf1c14c2019-09-06 23:53:21 +00001670 template<typename Fn> void match(Fn F) const {
1671 F(TemplateParams, Params, Count);
1672 }
1673
1674 void printDeclarator(OutputStream &S) const {
1675 if (!TemplateParams.empty()) {
1676 S += "<";
1677 TemplateParams.printWithComma(S);
1678 S += ">";
1679 }
1680 S += "(";
1681 Params.printWithComma(S);
1682 S += ")";
1683 }
Richard Smithc20d1442018-08-20 20:14:49 +00001684
1685 void printLeft(OutputStream &S) const override {
1686 S += "\'lambda";
1687 S += Count;
Richard Smithdf1c14c2019-09-06 23:53:21 +00001688 S += "\'";
1689 printDeclarator(S);
Richard Smithc20d1442018-08-20 20:14:49 +00001690 }
1691};
1692
1693class StructuredBindingName : public Node {
1694 NodeArray Bindings;
1695public:
1696 StructuredBindingName(NodeArray Bindings_)
1697 : Node(KStructuredBindingName), Bindings(Bindings_) {}
1698
1699 template<typename Fn> void match(Fn F) const { F(Bindings); }
1700
1701 void printLeft(OutputStream &S) const override {
1702 S += '[';
1703 Bindings.printWithComma(S);
1704 S += ']';
1705 }
1706};
1707
1708// -- Expression Nodes --
1709
1710class BinaryExpr : public Node {
1711 const Node *LHS;
1712 const StringView InfixOperator;
1713 const Node *RHS;
1714
1715public:
1716 BinaryExpr(const Node *LHS_, StringView InfixOperator_, const Node *RHS_)
1717 : Node(KBinaryExpr), LHS(LHS_), InfixOperator(InfixOperator_), RHS(RHS_) {
1718 }
1719
1720 template<typename Fn> void match(Fn F) const { F(LHS, InfixOperator, RHS); }
1721
1722 void printLeft(OutputStream &S) const override {
1723 // might be a template argument expression, then we need to disambiguate
1724 // with parens.
1725 if (InfixOperator == ">")
1726 S += "(";
1727
1728 S += "(";
1729 LHS->print(S);
1730 S += ") ";
1731 S += InfixOperator;
1732 S += " (";
1733 RHS->print(S);
1734 S += ")";
1735
1736 if (InfixOperator == ">")
1737 S += ")";
1738 }
1739};
1740
1741class ArraySubscriptExpr : public Node {
1742 const Node *Op1;
1743 const Node *Op2;
1744
1745public:
1746 ArraySubscriptExpr(const Node *Op1_, const Node *Op2_)
1747 : Node(KArraySubscriptExpr), Op1(Op1_), Op2(Op2_) {}
1748
1749 template<typename Fn> void match(Fn F) const { F(Op1, Op2); }
1750
1751 void printLeft(OutputStream &S) const override {
1752 S += "(";
1753 Op1->print(S);
1754 S += ")[";
1755 Op2->print(S);
1756 S += "]";
1757 }
1758};
1759
1760class PostfixExpr : public Node {
1761 const Node *Child;
1762 const StringView Operator;
1763
1764public:
1765 PostfixExpr(const Node *Child_, StringView Operator_)
1766 : Node(KPostfixExpr), Child(Child_), Operator(Operator_) {}
1767
1768 template<typename Fn> void match(Fn F) const { F(Child, Operator); }
1769
1770 void printLeft(OutputStream &S) const override {
1771 S += "(";
1772 Child->print(S);
1773 S += ")";
1774 S += Operator;
1775 }
1776};
1777
1778class ConditionalExpr : public Node {
1779 const Node *Cond;
1780 const Node *Then;
1781 const Node *Else;
1782
1783public:
1784 ConditionalExpr(const Node *Cond_, const Node *Then_, const Node *Else_)
1785 : Node(KConditionalExpr), Cond(Cond_), Then(Then_), Else(Else_) {}
1786
1787 template<typename Fn> void match(Fn F) const { F(Cond, Then, Else); }
1788
1789 void printLeft(OutputStream &S) const override {
1790 S += "(";
1791 Cond->print(S);
1792 S += ") ? (";
1793 Then->print(S);
1794 S += ") : (";
1795 Else->print(S);
1796 S += ")";
1797 }
1798};
1799
1800class MemberExpr : public Node {
1801 const Node *LHS;
1802 const StringView Kind;
1803 const Node *RHS;
1804
1805public:
1806 MemberExpr(const Node *LHS_, StringView Kind_, const Node *RHS_)
1807 : Node(KMemberExpr), LHS(LHS_), Kind(Kind_), RHS(RHS_) {}
1808
1809 template<typename Fn> void match(Fn F) const { F(LHS, Kind, RHS); }
1810
1811 void printLeft(OutputStream &S) const override {
1812 LHS->print(S);
1813 S += Kind;
1814 RHS->print(S);
1815 }
1816};
1817
Richard Smith1865d2f2020-10-22 19:29:36 -07001818class SubobjectExpr : public Node {
1819 const Node *Type;
1820 const Node *SubExpr;
1821 StringView Offset;
1822 NodeArray UnionSelectors;
1823 bool OnePastTheEnd;
1824
1825public:
1826 SubobjectExpr(const Node *Type_, const Node *SubExpr_, StringView Offset_,
1827 NodeArray UnionSelectors_, bool OnePastTheEnd_)
1828 : Node(KSubobjectExpr), Type(Type_), SubExpr(SubExpr_), Offset(Offset_),
1829 UnionSelectors(UnionSelectors_), OnePastTheEnd(OnePastTheEnd_) {}
1830
1831 template<typename Fn> void match(Fn F) const {
1832 F(Type, SubExpr, Offset, UnionSelectors, OnePastTheEnd);
1833 }
1834
1835 void printLeft(OutputStream &S) const override {
1836 SubExpr->print(S);
1837 S += ".<";
1838 Type->print(S);
1839 S += " at offset ";
1840 if (Offset.empty()) {
1841 S += "0";
1842 } else if (Offset[0] == 'n') {
1843 S += "-";
1844 S += Offset.dropFront();
1845 } else {
1846 S += Offset;
1847 }
1848 S += ">";
1849 }
1850};
1851
Richard Smithc20d1442018-08-20 20:14:49 +00001852class EnclosingExpr : public Node {
1853 const StringView Prefix;
1854 const Node *Infix;
1855 const StringView Postfix;
1856
1857public:
1858 EnclosingExpr(StringView Prefix_, Node *Infix_, StringView Postfix_)
1859 : Node(KEnclosingExpr), Prefix(Prefix_), Infix(Infix_),
1860 Postfix(Postfix_) {}
1861
1862 template<typename Fn> void match(Fn F) const { F(Prefix, Infix, Postfix); }
1863
1864 void printLeft(OutputStream &S) const override {
1865 S += Prefix;
1866 Infix->print(S);
1867 S += Postfix;
1868 }
1869};
1870
1871class CastExpr : public Node {
1872 // cast_kind<to>(from)
1873 const StringView CastKind;
1874 const Node *To;
1875 const Node *From;
1876
1877public:
1878 CastExpr(StringView CastKind_, const Node *To_, const Node *From_)
1879 : Node(KCastExpr), CastKind(CastKind_), To(To_), From(From_) {}
1880
1881 template<typename Fn> void match(Fn F) const { F(CastKind, To, From); }
1882
1883 void printLeft(OutputStream &S) const override {
1884 S += CastKind;
1885 S += "<";
1886 To->printLeft(S);
1887 S += ">(";
1888 From->printLeft(S);
1889 S += ")";
1890 }
1891};
1892
1893class SizeofParamPackExpr : public Node {
1894 const Node *Pack;
1895
1896public:
1897 SizeofParamPackExpr(const Node *Pack_)
1898 : Node(KSizeofParamPackExpr), Pack(Pack_) {}
1899
1900 template<typename Fn> void match(Fn F) const { F(Pack); }
1901
1902 void printLeft(OutputStream &S) const override {
1903 S += "sizeof...(";
1904 ParameterPackExpansion PPE(Pack);
1905 PPE.printLeft(S);
1906 S += ")";
1907 }
1908};
1909
1910class CallExpr : public Node {
1911 const Node *Callee;
1912 NodeArray Args;
1913
1914public:
1915 CallExpr(const Node *Callee_, NodeArray Args_)
1916 : Node(KCallExpr), Callee(Callee_), Args(Args_) {}
1917
1918 template<typename Fn> void match(Fn F) const { F(Callee, Args); }
1919
1920 void printLeft(OutputStream &S) const override {
1921 Callee->print(S);
1922 S += "(";
1923 Args.printWithComma(S);
1924 S += ")";
1925 }
1926};
1927
1928class NewExpr : public Node {
1929 // new (expr_list) type(init_list)
1930 NodeArray ExprList;
1931 Node *Type;
1932 NodeArray InitList;
1933 bool IsGlobal; // ::operator new ?
1934 bool IsArray; // new[] ?
1935public:
1936 NewExpr(NodeArray ExprList_, Node *Type_, NodeArray InitList_, bool IsGlobal_,
1937 bool IsArray_)
1938 : Node(KNewExpr), ExprList(ExprList_), Type(Type_), InitList(InitList_),
1939 IsGlobal(IsGlobal_), IsArray(IsArray_) {}
1940
1941 template<typename Fn> void match(Fn F) const {
1942 F(ExprList, Type, InitList, IsGlobal, IsArray);
1943 }
1944
1945 void printLeft(OutputStream &S) const override {
1946 if (IsGlobal)
1947 S += "::operator ";
1948 S += "new";
1949 if (IsArray)
1950 S += "[]";
1951 S += ' ';
1952 if (!ExprList.empty()) {
1953 S += "(";
1954 ExprList.printWithComma(S);
1955 S += ")";
1956 }
1957 Type->print(S);
1958 if (!InitList.empty()) {
1959 S += "(";
1960 InitList.printWithComma(S);
1961 S += ")";
1962 }
1963
1964 }
1965};
1966
1967class DeleteExpr : public Node {
1968 Node *Op;
1969 bool IsGlobal;
1970 bool IsArray;
1971
1972public:
1973 DeleteExpr(Node *Op_, bool IsGlobal_, bool IsArray_)
1974 : Node(KDeleteExpr), Op(Op_), IsGlobal(IsGlobal_), IsArray(IsArray_) {}
1975
1976 template<typename Fn> void match(Fn F) const { F(Op, IsGlobal, IsArray); }
1977
1978 void printLeft(OutputStream &S) const override {
1979 if (IsGlobal)
1980 S += "::";
1981 S += "delete";
1982 if (IsArray)
1983 S += "[] ";
1984 Op->print(S);
1985 }
1986};
1987
1988class PrefixExpr : public Node {
1989 StringView Prefix;
1990 Node *Child;
1991
1992public:
1993 PrefixExpr(StringView Prefix_, Node *Child_)
1994 : Node(KPrefixExpr), Prefix(Prefix_), Child(Child_) {}
1995
1996 template<typename Fn> void match(Fn F) const { F(Prefix, Child); }
1997
1998 void printLeft(OutputStream &S) const override {
1999 S += Prefix;
2000 S += "(";
2001 Child->print(S);
2002 S += ")";
2003 }
2004};
2005
2006class FunctionParam : public Node {
2007 StringView Number;
2008
2009public:
2010 FunctionParam(StringView Number_) : Node(KFunctionParam), Number(Number_) {}
2011
2012 template<typename Fn> void match(Fn F) const { F(Number); }
2013
2014 void printLeft(OutputStream &S) const override {
2015 S += "fp";
2016 S += Number;
2017 }
2018};
2019
2020class ConversionExpr : public Node {
2021 const Node *Type;
2022 NodeArray Expressions;
2023
2024public:
2025 ConversionExpr(const Node *Type_, NodeArray Expressions_)
2026 : Node(KConversionExpr), Type(Type_), Expressions(Expressions_) {}
2027
2028 template<typename Fn> void match(Fn F) const { F(Type, Expressions); }
2029
2030 void printLeft(OutputStream &S) const override {
2031 S += "(";
2032 Type->print(S);
2033 S += ")(";
2034 Expressions.printWithComma(S);
2035 S += ")";
2036 }
2037};
2038
Richard Smith1865d2f2020-10-22 19:29:36 -07002039class PointerToMemberConversionExpr : public Node {
2040 const Node *Type;
2041 const Node *SubExpr;
2042 StringView Offset;
2043
2044public:
2045 PointerToMemberConversionExpr(const Node *Type_, const Node *SubExpr_,
2046 StringView Offset_)
2047 : Node(KPointerToMemberConversionExpr), Type(Type_), SubExpr(SubExpr_),
2048 Offset(Offset_) {}
2049
2050 template<typename Fn> void match(Fn F) const { F(Type, SubExpr, Offset); }
2051
2052 void printLeft(OutputStream &S) const override {
2053 S += "(";
2054 Type->print(S);
2055 S += ")(";
2056 SubExpr->print(S);
2057 S += ")";
2058 }
2059};
2060
Richard Smithc20d1442018-08-20 20:14:49 +00002061class InitListExpr : public Node {
2062 const Node *Ty;
2063 NodeArray Inits;
2064public:
2065 InitListExpr(const Node *Ty_, NodeArray Inits_)
2066 : Node(KInitListExpr), Ty(Ty_), Inits(Inits_) {}
2067
2068 template<typename Fn> void match(Fn F) const { F(Ty, Inits); }
2069
2070 void printLeft(OutputStream &S) const override {
2071 if (Ty)
2072 Ty->print(S);
2073 S += '{';
2074 Inits.printWithComma(S);
2075 S += '}';
2076 }
2077};
2078
2079class BracedExpr : public Node {
2080 const Node *Elem;
2081 const Node *Init;
2082 bool IsArray;
2083public:
2084 BracedExpr(const Node *Elem_, const Node *Init_, bool IsArray_)
2085 : Node(KBracedExpr), Elem(Elem_), Init(Init_), IsArray(IsArray_) {}
2086
2087 template<typename Fn> void match(Fn F) const { F(Elem, Init, IsArray); }
2088
2089 void printLeft(OutputStream &S) const override {
2090 if (IsArray) {
2091 S += '[';
2092 Elem->print(S);
2093 S += ']';
2094 } else {
2095 S += '.';
2096 Elem->print(S);
2097 }
2098 if (Init->getKind() != KBracedExpr && Init->getKind() != KBracedRangeExpr)
2099 S += " = ";
2100 Init->print(S);
2101 }
2102};
2103
2104class BracedRangeExpr : public Node {
2105 const Node *First;
2106 const Node *Last;
2107 const Node *Init;
2108public:
2109 BracedRangeExpr(const Node *First_, const Node *Last_, const Node *Init_)
2110 : Node(KBracedRangeExpr), First(First_), Last(Last_), Init(Init_) {}
2111
2112 template<typename Fn> void match(Fn F) const { F(First, Last, Init); }
2113
2114 void printLeft(OutputStream &S) const override {
2115 S += '[';
2116 First->print(S);
2117 S += " ... ";
2118 Last->print(S);
2119 S += ']';
2120 if (Init->getKind() != KBracedExpr && Init->getKind() != KBracedRangeExpr)
2121 S += " = ";
2122 Init->print(S);
2123 }
2124};
2125
2126class FoldExpr : public Node {
2127 const Node *Pack, *Init;
2128 StringView OperatorName;
2129 bool IsLeftFold;
2130
2131public:
2132 FoldExpr(bool IsLeftFold_, StringView OperatorName_, const Node *Pack_,
2133 const Node *Init_)
2134 : Node(KFoldExpr), Pack(Pack_), Init(Init_), OperatorName(OperatorName_),
2135 IsLeftFold(IsLeftFold_) {}
2136
2137 template<typename Fn> void match(Fn F) const {
2138 F(IsLeftFold, OperatorName, Pack, Init);
2139 }
2140
2141 void printLeft(OutputStream &S) const override {
2142 auto PrintPack = [&] {
2143 S += '(';
2144 ParameterPackExpansion(Pack).print(S);
2145 S += ')';
2146 };
2147
2148 S += '(';
2149
2150 if (IsLeftFold) {
2151 // init op ... op pack
2152 if (Init != nullptr) {
2153 Init->print(S);
2154 S += ' ';
2155 S += OperatorName;
2156 S += ' ';
2157 }
2158 // ... op pack
2159 S += "... ";
2160 S += OperatorName;
2161 S += ' ';
2162 PrintPack();
2163 } else { // !IsLeftFold
2164 // pack op ...
2165 PrintPack();
2166 S += ' ';
2167 S += OperatorName;
2168 S += " ...";
2169 // pack op ... op init
2170 if (Init != nullptr) {
2171 S += ' ';
2172 S += OperatorName;
2173 S += ' ';
2174 Init->print(S);
2175 }
2176 }
2177 S += ')';
2178 }
2179};
2180
2181class ThrowExpr : public Node {
2182 const Node *Op;
2183
2184public:
2185 ThrowExpr(const Node *Op_) : Node(KThrowExpr), Op(Op_) {}
2186
2187 template<typename Fn> void match(Fn F) const { F(Op); }
2188
2189 void printLeft(OutputStream &S) const override {
2190 S += "throw ";
2191 Op->print(S);
2192 }
2193};
2194
2195class BoolExpr : public Node {
2196 bool Value;
2197
2198public:
2199 BoolExpr(bool Value_) : Node(KBoolExpr), Value(Value_) {}
2200
2201 template<typename Fn> void match(Fn F) const { F(Value); }
2202
2203 void printLeft(OutputStream &S) const override {
2204 S += Value ? StringView("true") : StringView("false");
2205 }
2206};
2207
Richard Smithdf1c14c2019-09-06 23:53:21 +00002208class StringLiteral : public Node {
2209 const Node *Type;
2210
2211public:
2212 StringLiteral(const Node *Type_) : Node(KStringLiteral), Type(Type_) {}
2213
2214 template<typename Fn> void match(Fn F) const { F(Type); }
2215
2216 void printLeft(OutputStream &S) const override {
2217 S += "\"<";
2218 Type->print(S);
2219 S += ">\"";
2220 }
2221};
2222
2223class LambdaExpr : public Node {
2224 const Node *Type;
2225
Richard Smithdf1c14c2019-09-06 23:53:21 +00002226public:
2227 LambdaExpr(const Node *Type_) : Node(KLambdaExpr), Type(Type_) {}
2228
2229 template<typename Fn> void match(Fn F) const { F(Type); }
2230
2231 void printLeft(OutputStream &S) const override {
2232 S += "[]";
Richard Smithfb917462019-09-09 22:26:04 +00002233 if (Type->getKind() == KClosureTypeName)
2234 static_cast<const ClosureTypeName *>(Type)->printDeclarator(S);
Richard Smithdf1c14c2019-09-06 23:53:21 +00002235 S += "{...}";
2236 }
2237};
2238
Erik Pilkington0a170f12020-05-13 14:13:37 -04002239class EnumLiteral : public Node {
Richard Smithc20d1442018-08-20 20:14:49 +00002240 // ty(integer)
2241 const Node *Ty;
2242 StringView Integer;
2243
2244public:
Erik Pilkington0a170f12020-05-13 14:13:37 -04002245 EnumLiteral(const Node *Ty_, StringView Integer_)
2246 : Node(KEnumLiteral), Ty(Ty_), Integer(Integer_) {}
Richard Smithc20d1442018-08-20 20:14:49 +00002247
2248 template<typename Fn> void match(Fn F) const { F(Ty, Integer); }
2249
2250 void printLeft(OutputStream &S) const override {
Erik Pilkington0a170f12020-05-13 14:13:37 -04002251 S << "(";
Richard Smithc20d1442018-08-20 20:14:49 +00002252 Ty->print(S);
Erik Pilkington0a170f12020-05-13 14:13:37 -04002253 S << ")";
2254
2255 if (Integer[0] == 'n')
2256 S << "-" << Integer.dropFront(1);
2257 else
2258 S << Integer;
Richard Smithc20d1442018-08-20 20:14:49 +00002259 }
2260};
2261
2262class IntegerLiteral : public Node {
2263 StringView Type;
2264 StringView Value;
2265
2266public:
2267 IntegerLiteral(StringView Type_, StringView Value_)
2268 : Node(KIntegerLiteral), Type(Type_), Value(Value_) {}
2269
2270 template<typename Fn> void match(Fn F) const { F(Type, Value); }
2271
2272 void printLeft(OutputStream &S) const override {
2273 if (Type.size() > 3) {
2274 S += "(";
2275 S += Type;
2276 S += ")";
2277 }
2278
2279 if (Value[0] == 'n') {
2280 S += "-";
2281 S += Value.dropFront(1);
2282 } else
2283 S += Value;
2284
2285 if (Type.size() <= 3)
2286 S += Type;
2287 }
2288};
2289
2290template <class Float> struct FloatData;
2291
2292namespace float_literal_impl {
2293constexpr Node::Kind getFloatLiteralKind(float *) {
2294 return Node::KFloatLiteral;
2295}
2296constexpr Node::Kind getFloatLiteralKind(double *) {
2297 return Node::KDoubleLiteral;
2298}
2299constexpr Node::Kind getFloatLiteralKind(long double *) {
2300 return Node::KLongDoubleLiteral;
2301}
2302}
2303
2304template <class Float> class FloatLiteralImpl : public Node {
2305 const StringView Contents;
2306
2307 static constexpr Kind KindForClass =
2308 float_literal_impl::getFloatLiteralKind((Float *)nullptr);
2309
2310public:
2311 FloatLiteralImpl(StringView Contents_)
2312 : Node(KindForClass), Contents(Contents_) {}
2313
2314 template<typename Fn> void match(Fn F) const { F(Contents); }
2315
2316 void printLeft(OutputStream &s) const override {
2317 const char *first = Contents.begin();
2318 const char *last = Contents.end() + 1;
2319
2320 const size_t N = FloatData<Float>::mangled_size;
2321 if (static_cast<std::size_t>(last - first) > N) {
2322 last = first + N;
2323 union {
2324 Float value;
2325 char buf[sizeof(Float)];
2326 };
2327 const char *t = first;
2328 char *e = buf;
2329 for (; t != last; ++t, ++e) {
2330 unsigned d1 = isdigit(*t) ? static_cast<unsigned>(*t - '0')
2331 : static_cast<unsigned>(*t - 'a' + 10);
2332 ++t;
2333 unsigned d0 = isdigit(*t) ? static_cast<unsigned>(*t - '0')
2334 : static_cast<unsigned>(*t - 'a' + 10);
2335 *e = static_cast<char>((d1 << 4) + d0);
2336 }
2337#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
2338 std::reverse(buf, e);
2339#endif
2340 char num[FloatData<Float>::max_demangled_size] = {0};
2341 int n = snprintf(num, sizeof(num), FloatData<Float>::spec, value);
2342 s += StringView(num, num + n);
2343 }
2344 }
2345};
2346
2347using FloatLiteral = FloatLiteralImpl<float>;
2348using DoubleLiteral = FloatLiteralImpl<double>;
2349using LongDoubleLiteral = FloatLiteralImpl<long double>;
2350
2351/// Visit the node. Calls \c F(P), where \c P is the node cast to the
2352/// appropriate derived class.
2353template<typename Fn>
2354void Node::visit(Fn F) const {
2355 switch (K) {
2356#define CASE(X) case K ## X: return F(static_cast<const X*>(this));
2357 FOR_EACH_NODE_KIND(CASE)
2358#undef CASE
2359 }
2360 assert(0 && "unknown mangling node kind");
2361}
2362
2363/// Determine the kind of a node from its type.
2364template<typename NodeT> struct NodeKind;
2365#define SPECIALIZATION(X) \
2366 template<> struct NodeKind<X> { \
2367 static constexpr Node::Kind Kind = Node::K##X; \
2368 static constexpr const char *name() { return #X; } \
2369 };
2370FOR_EACH_NODE_KIND(SPECIALIZATION)
2371#undef SPECIALIZATION
2372
2373#undef FOR_EACH_NODE_KIND
2374
Pavel Labathba825192018-10-16 14:29:14 +00002375template <typename Derived, typename Alloc> struct AbstractManglingParser {
Richard Smithc20d1442018-08-20 20:14:49 +00002376 const char *First;
2377 const char *Last;
2378
2379 // Name stack, this is used by the parser to hold temporary names that were
2380 // parsed. The parser collapses multiple names into new nodes to construct
2381 // the AST. Once the parser is finished, names.size() == 1.
2382 PODSmallVector<Node *, 32> Names;
2383
2384 // Substitution table. Itanium supports name substitutions as a means of
2385 // compression. The string "S42_" refers to the 44nd entry (base-36) in this
2386 // table.
2387 PODSmallVector<Node *, 32> Subs;
2388
Richard Smithdf1c14c2019-09-06 23:53:21 +00002389 using TemplateParamList = PODSmallVector<Node *, 8>;
2390
2391 class ScopedTemplateParamList {
2392 AbstractManglingParser *Parser;
2393 size_t OldNumTemplateParamLists;
2394 TemplateParamList Params;
2395
2396 public:
Louis Dionnec1fe8672020-10-30 17:33:02 -04002397 ScopedTemplateParamList(AbstractManglingParser *TheParser)
2398 : Parser(TheParser),
2399 OldNumTemplateParamLists(TheParser->TemplateParams.size()) {
Richard Smithdf1c14c2019-09-06 23:53:21 +00002400 Parser->TemplateParams.push_back(&Params);
2401 }
2402 ~ScopedTemplateParamList() {
2403 assert(Parser->TemplateParams.size() >= OldNumTemplateParamLists);
2404 Parser->TemplateParams.dropBack(OldNumTemplateParamLists);
2405 }
Richard Smithdf1c14c2019-09-06 23:53:21 +00002406 };
2407
Richard Smithc20d1442018-08-20 20:14:49 +00002408 // Template parameter table. Like the above, but referenced like "T42_".
2409 // This has a smaller size compared to Subs and Names because it can be
2410 // stored on the stack.
Richard Smithdf1c14c2019-09-06 23:53:21 +00002411 TemplateParamList OuterTemplateParams;
2412
2413 // Lists of template parameters indexed by template parameter depth,
2414 // referenced like "TL2_4_". If nonempty, element 0 is always
2415 // OuterTemplateParams; inner elements are always template parameter lists of
2416 // lambda expressions. For a generic lambda with no explicit template
2417 // parameter list, the corresponding parameter list pointer will be null.
2418 PODSmallVector<TemplateParamList *, 4> TemplateParams;
Richard Smithc20d1442018-08-20 20:14:49 +00002419
2420 // Set of unresolved forward <template-param> references. These can occur in a
2421 // conversion operator's type, and are resolved in the enclosing <encoding>.
2422 PODSmallVector<ForwardTemplateReference *, 4> ForwardTemplateRefs;
2423
Richard Smithc20d1442018-08-20 20:14:49 +00002424 bool TryToParseTemplateArgs = true;
2425 bool PermitForwardTemplateReferences = false;
Richard Smithdf1c14c2019-09-06 23:53:21 +00002426 size_t ParsingLambdaParamsAtLevel = (size_t)-1;
2427
2428 unsigned NumSyntheticTemplateParameters[3] = {};
Richard Smithc20d1442018-08-20 20:14:49 +00002429
2430 Alloc ASTAllocator;
2431
Pavel Labathba825192018-10-16 14:29:14 +00002432 AbstractManglingParser(const char *First_, const char *Last_)
2433 : First(First_), Last(Last_) {}
2434
2435 Derived &getDerived() { return static_cast<Derived &>(*this); }
Richard Smithc20d1442018-08-20 20:14:49 +00002436
2437 void reset(const char *First_, const char *Last_) {
2438 First = First_;
2439 Last = Last_;
2440 Names.clear();
2441 Subs.clear();
2442 TemplateParams.clear();
Richard Smithdf1c14c2019-09-06 23:53:21 +00002443 ParsingLambdaParamsAtLevel = (size_t)-1;
Richard Smithc20d1442018-08-20 20:14:49 +00002444 TryToParseTemplateArgs = true;
2445 PermitForwardTemplateReferences = false;
Richard Smith9a2307a2019-09-07 00:11:53 +00002446 for (int I = 0; I != 3; ++I)
2447 NumSyntheticTemplateParameters[I] = 0;
Richard Smithc20d1442018-08-20 20:14:49 +00002448 ASTAllocator.reset();
2449 }
2450
Richard Smithb485b352018-08-24 23:30:26 +00002451 template <class T, class... Args> Node *make(Args &&... args) {
Richard Smithc20d1442018-08-20 20:14:49 +00002452 return ASTAllocator.template makeNode<T>(std::forward<Args>(args)...);
2453 }
2454
2455 template <class It> NodeArray makeNodeArray(It begin, It end) {
2456 size_t sz = static_cast<size_t>(end - begin);
2457 void *mem = ASTAllocator.allocateNodeArray(sz);
2458 Node **data = new (mem) Node *[sz];
2459 std::copy(begin, end, data);
2460 return NodeArray(data, sz);
2461 }
2462
2463 NodeArray popTrailingNodeArray(size_t FromPosition) {
2464 assert(FromPosition <= Names.size());
2465 NodeArray res =
2466 makeNodeArray(Names.begin() + (long)FromPosition, Names.end());
2467 Names.dropBack(FromPosition);
2468 return res;
2469 }
2470
2471 bool consumeIf(StringView S) {
2472 if (StringView(First, Last).startsWith(S)) {
2473 First += S.size();
2474 return true;
2475 }
2476 return false;
2477 }
2478
2479 bool consumeIf(char C) {
2480 if (First != Last && *First == C) {
2481 ++First;
2482 return true;
2483 }
2484 return false;
2485 }
2486
2487 char consume() { return First != Last ? *First++ : '\0'; }
2488
2489 char look(unsigned Lookahead = 0) {
2490 if (static_cast<size_t>(Last - First) <= Lookahead)
2491 return '\0';
2492 return First[Lookahead];
2493 }
2494
2495 size_t numLeft() const { return static_cast<size_t>(Last - First); }
2496
2497 StringView parseNumber(bool AllowNegative = false);
2498 Qualifiers parseCVQualifiers();
2499 bool parsePositiveInteger(size_t *Out);
2500 StringView parseBareSourceName();
2501
2502 bool parseSeqId(size_t *Out);
2503 Node *parseSubstitution();
2504 Node *parseTemplateParam();
Richard Smithdf1c14c2019-09-06 23:53:21 +00002505 Node *parseTemplateParamDecl();
Richard Smithc20d1442018-08-20 20:14:49 +00002506 Node *parseTemplateArgs(bool TagTemplates = false);
2507 Node *parseTemplateArg();
2508
2509 /// Parse the <expr> production.
2510 Node *parseExpr();
2511 Node *parsePrefixExpr(StringView Kind);
2512 Node *parseBinaryExpr(StringView Kind);
2513 Node *parseIntegerLiteral(StringView Lit);
2514 Node *parseExprPrimary();
2515 template <class Float> Node *parseFloatingLiteral();
2516 Node *parseFunctionParam();
2517 Node *parseNewExpr();
2518 Node *parseConversionExpr();
2519 Node *parseBracedExpr();
2520 Node *parseFoldExpr();
Richard Smith1865d2f2020-10-22 19:29:36 -07002521 Node *parsePointerToMemberConversionExpr();
2522 Node *parseSubobjectExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00002523
2524 /// Parse the <type> production.
2525 Node *parseType();
2526 Node *parseFunctionType();
2527 Node *parseVectorType();
2528 Node *parseDecltype();
2529 Node *parseArrayType();
2530 Node *parsePointerToMemberType();
2531 Node *parseClassEnumType();
2532 Node *parseQualifiedType();
2533
2534 Node *parseEncoding();
2535 bool parseCallOffset();
2536 Node *parseSpecialName();
2537
2538 /// Holds some extra information about a <name> that is being parsed. This
2539 /// information is only pertinent if the <name> refers to an <encoding>.
2540 struct NameState {
2541 bool CtorDtorConversion = false;
2542 bool EndsWithTemplateArgs = false;
2543 Qualifiers CVQualifiers = QualNone;
2544 FunctionRefQual ReferenceQualifier = FrefQualNone;
2545 size_t ForwardTemplateRefsBegin;
2546
Pavel Labathba825192018-10-16 14:29:14 +00002547 NameState(AbstractManglingParser *Enclosing)
Richard Smithc20d1442018-08-20 20:14:49 +00002548 : ForwardTemplateRefsBegin(Enclosing->ForwardTemplateRefs.size()) {}
2549 };
2550
2551 bool resolveForwardTemplateRefs(NameState &State) {
2552 size_t I = State.ForwardTemplateRefsBegin;
2553 size_t E = ForwardTemplateRefs.size();
2554 for (; I < E; ++I) {
2555 size_t Idx = ForwardTemplateRefs[I]->Index;
Richard Smithdf1c14c2019-09-06 23:53:21 +00002556 if (TemplateParams.empty() || !TemplateParams[0] ||
2557 Idx >= TemplateParams[0]->size())
Richard Smithc20d1442018-08-20 20:14:49 +00002558 return true;
Richard Smithdf1c14c2019-09-06 23:53:21 +00002559 ForwardTemplateRefs[I]->Ref = (*TemplateParams[0])[Idx];
Richard Smithc20d1442018-08-20 20:14:49 +00002560 }
2561 ForwardTemplateRefs.dropBack(State.ForwardTemplateRefsBegin);
2562 return false;
2563 }
2564
2565 /// Parse the <name> production>
2566 Node *parseName(NameState *State = nullptr);
2567 Node *parseLocalName(NameState *State);
2568 Node *parseOperatorName(NameState *State);
2569 Node *parseUnqualifiedName(NameState *State);
2570 Node *parseUnnamedTypeName(NameState *State);
2571 Node *parseSourceName(NameState *State);
2572 Node *parseUnscopedName(NameState *State);
2573 Node *parseNestedName(NameState *State);
2574 Node *parseCtorDtorName(Node *&SoFar, NameState *State);
2575
2576 Node *parseAbiTags(Node *N);
2577
2578 /// Parse the <unresolved-name> production.
2579 Node *parseUnresolvedName();
2580 Node *parseSimpleId();
2581 Node *parseBaseUnresolvedName();
2582 Node *parseUnresolvedType();
2583 Node *parseDestructorName();
2584
2585 /// Top-level entry point into the parser.
2586 Node *parse();
2587};
2588
2589const char* parse_discriminator(const char* first, const char* last);
2590
2591// <name> ::= <nested-name> // N
2592// ::= <local-name> # See Scope Encoding below // Z
2593// ::= <unscoped-template-name> <template-args>
2594// ::= <unscoped-name>
2595//
2596// <unscoped-template-name> ::= <unscoped-name>
2597// ::= <substitution>
Pavel Labathba825192018-10-16 14:29:14 +00002598template <typename Derived, typename Alloc>
2599Node *AbstractManglingParser<Derived, Alloc>::parseName(NameState *State) {
Richard Smithc20d1442018-08-20 20:14:49 +00002600 consumeIf('L'); // extension
2601
2602 if (look() == 'N')
Pavel Labathba825192018-10-16 14:29:14 +00002603 return getDerived().parseNestedName(State);
Richard Smithc20d1442018-08-20 20:14:49 +00002604 if (look() == 'Z')
Pavel Labathba825192018-10-16 14:29:14 +00002605 return getDerived().parseLocalName(State);
Richard Smithc20d1442018-08-20 20:14:49 +00002606
2607 // ::= <unscoped-template-name> <template-args>
2608 if (look() == 'S' && look(1) != 't') {
Pavel Labathba825192018-10-16 14:29:14 +00002609 Node *S = getDerived().parseSubstitution();
Richard Smithc20d1442018-08-20 20:14:49 +00002610 if (S == nullptr)
2611 return nullptr;
2612 if (look() != 'I')
2613 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00002614 Node *TA = getDerived().parseTemplateArgs(State != nullptr);
Richard Smithc20d1442018-08-20 20:14:49 +00002615 if (TA == nullptr)
2616 return nullptr;
2617 if (State) State->EndsWithTemplateArgs = true;
2618 return make<NameWithTemplateArgs>(S, TA);
2619 }
2620
Pavel Labathba825192018-10-16 14:29:14 +00002621 Node *N = getDerived().parseUnscopedName(State);
Richard Smithc20d1442018-08-20 20:14:49 +00002622 if (N == nullptr)
2623 return nullptr;
2624 // ::= <unscoped-template-name> <template-args>
2625 if (look() == 'I') {
2626 Subs.push_back(N);
Pavel Labathba825192018-10-16 14:29:14 +00002627 Node *TA = getDerived().parseTemplateArgs(State != nullptr);
Richard Smithc20d1442018-08-20 20:14:49 +00002628 if (TA == nullptr)
2629 return nullptr;
2630 if (State) State->EndsWithTemplateArgs = true;
2631 return make<NameWithTemplateArgs>(N, TA);
2632 }
2633 // ::= <unscoped-name>
2634 return N;
2635}
2636
2637// <local-name> := Z <function encoding> E <entity name> [<discriminator>]
2638// := Z <function encoding> E s [<discriminator>]
2639// := Z <function encoding> Ed [ <parameter number> ] _ <entity name>
Pavel Labathba825192018-10-16 14:29:14 +00002640template <typename Derived, typename Alloc>
2641Node *AbstractManglingParser<Derived, Alloc>::parseLocalName(NameState *State) {
Richard Smithc20d1442018-08-20 20:14:49 +00002642 if (!consumeIf('Z'))
2643 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00002644 Node *Encoding = getDerived().parseEncoding();
Richard Smithc20d1442018-08-20 20:14:49 +00002645 if (Encoding == nullptr || !consumeIf('E'))
2646 return nullptr;
2647
2648 if (consumeIf('s')) {
2649 First = parse_discriminator(First, Last);
Richard Smithb485b352018-08-24 23:30:26 +00002650 auto *StringLitName = make<NameType>("string literal");
2651 if (!StringLitName)
2652 return nullptr;
2653 return make<LocalName>(Encoding, StringLitName);
Richard Smithc20d1442018-08-20 20:14:49 +00002654 }
2655
2656 if (consumeIf('d')) {
2657 parseNumber(true);
2658 if (!consumeIf('_'))
2659 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00002660 Node *N = getDerived().parseName(State);
Richard Smithc20d1442018-08-20 20:14:49 +00002661 if (N == nullptr)
2662 return nullptr;
2663 return make<LocalName>(Encoding, N);
2664 }
2665
Pavel Labathba825192018-10-16 14:29:14 +00002666 Node *Entity = getDerived().parseName(State);
Richard Smithc20d1442018-08-20 20:14:49 +00002667 if (Entity == nullptr)
2668 return nullptr;
2669 First = parse_discriminator(First, Last);
2670 return make<LocalName>(Encoding, Entity);
2671}
2672
2673// <unscoped-name> ::= <unqualified-name>
2674// ::= St <unqualified-name> # ::std::
2675// extension ::= StL<unqualified-name>
Pavel Labathba825192018-10-16 14:29:14 +00002676template <typename Derived, typename Alloc>
2677Node *
2678AbstractManglingParser<Derived, Alloc>::parseUnscopedName(NameState *State) {
2679 if (consumeIf("StL") || consumeIf("St")) {
2680 Node *R = getDerived().parseUnqualifiedName(State);
2681 if (R == nullptr)
2682 return nullptr;
2683 return make<StdQualifiedName>(R);
2684 }
2685 return getDerived().parseUnqualifiedName(State);
Richard Smithc20d1442018-08-20 20:14:49 +00002686}
2687
2688// <unqualified-name> ::= <operator-name> [abi-tags]
2689// ::= <ctor-dtor-name>
2690// ::= <source-name>
2691// ::= <unnamed-type-name>
2692// ::= DC <source-name>+ E # structured binding declaration
Pavel Labathba825192018-10-16 14:29:14 +00002693template <typename Derived, typename Alloc>
2694Node *
2695AbstractManglingParser<Derived, Alloc>::parseUnqualifiedName(NameState *State) {
Richard Smithc20d1442018-08-20 20:14:49 +00002696 // <ctor-dtor-name>s are special-cased in parseNestedName().
2697 Node *Result;
2698 if (look() == 'U')
Pavel Labathba825192018-10-16 14:29:14 +00002699 Result = getDerived().parseUnnamedTypeName(State);
Richard Smithc20d1442018-08-20 20:14:49 +00002700 else if (look() >= '1' && look() <= '9')
Pavel Labathba825192018-10-16 14:29:14 +00002701 Result = getDerived().parseSourceName(State);
Richard Smithc20d1442018-08-20 20:14:49 +00002702 else if (consumeIf("DC")) {
2703 size_t BindingsBegin = Names.size();
2704 do {
Pavel Labathba825192018-10-16 14:29:14 +00002705 Node *Binding = getDerived().parseSourceName(State);
Richard Smithc20d1442018-08-20 20:14:49 +00002706 if (Binding == nullptr)
2707 return nullptr;
2708 Names.push_back(Binding);
2709 } while (!consumeIf('E'));
2710 Result = make<StructuredBindingName>(popTrailingNodeArray(BindingsBegin));
2711 } else
Pavel Labathba825192018-10-16 14:29:14 +00002712 Result = getDerived().parseOperatorName(State);
Richard Smithc20d1442018-08-20 20:14:49 +00002713 if (Result != nullptr)
Pavel Labathba825192018-10-16 14:29:14 +00002714 Result = getDerived().parseAbiTags(Result);
Richard Smithc20d1442018-08-20 20:14:49 +00002715 return Result;
2716}
2717
2718// <unnamed-type-name> ::= Ut [<nonnegative number>] _
2719// ::= <closure-type-name>
2720//
2721// <closure-type-name> ::= Ul <lambda-sig> E [ <nonnegative number> ] _
2722//
2723// <lambda-sig> ::= <parameter type>+ # Parameter types or "v" if the lambda has no parameters
Pavel Labathba825192018-10-16 14:29:14 +00002724template <typename Derived, typename Alloc>
2725Node *
Richard Smithdf1c14c2019-09-06 23:53:21 +00002726AbstractManglingParser<Derived, Alloc>::parseUnnamedTypeName(NameState *State) {
2727 // <template-params> refer to the innermost <template-args>. Clear out any
2728 // outer args that we may have inserted into TemplateParams.
2729 if (State != nullptr)
2730 TemplateParams.clear();
2731
Richard Smithc20d1442018-08-20 20:14:49 +00002732 if (consumeIf("Ut")) {
2733 StringView Count = parseNumber();
2734 if (!consumeIf('_'))
2735 return nullptr;
2736 return make<UnnamedTypeName>(Count);
2737 }
2738 if (consumeIf("Ul")) {
Richard Smithdf1c14c2019-09-06 23:53:21 +00002739 SwapAndRestore<size_t> SwapParams(ParsingLambdaParamsAtLevel,
2740 TemplateParams.size());
2741 ScopedTemplateParamList LambdaTemplateParams(this);
2742
2743 size_t ParamsBegin = Names.size();
2744 while (look() == 'T' &&
2745 StringView("yptn").find(look(1)) != StringView::npos) {
2746 Node *T = parseTemplateParamDecl();
2747 if (!T)
2748 return nullptr;
Richard Smithdf1c14c2019-09-06 23:53:21 +00002749 Names.push_back(T);
2750 }
2751 NodeArray TempParams = popTrailingNodeArray(ParamsBegin);
2752
2753 // FIXME: If TempParams is empty and none of the function parameters
2754 // includes 'auto', we should remove LambdaTemplateParams from the
2755 // TemplateParams list. Unfortunately, we don't find out whether there are
2756 // any 'auto' parameters until too late in an example such as:
2757 //
2758 // template<typename T> void f(
2759 // decltype([](decltype([]<typename T>(T v) {}),
2760 // auto) {})) {}
2761 // template<typename T> void f(
2762 // decltype([](decltype([]<typename T>(T w) {}),
2763 // int) {})) {}
2764 //
2765 // Here, the type of v is at level 2 but the type of w is at level 1. We
2766 // don't find this out until we encounter the type of the next parameter.
2767 //
2768 // However, compilers can't actually cope with the former example in
2769 // practice, and it's likely to be made ill-formed in future, so we don't
2770 // need to support it here.
2771 //
2772 // If we encounter an 'auto' in the function parameter types, we will
2773 // recreate a template parameter scope for it, but any intervening lambdas
2774 // will be parsed in the 'wrong' template parameter depth.
2775 if (TempParams.empty())
2776 TemplateParams.pop_back();
2777
Richard Smithc20d1442018-08-20 20:14:49 +00002778 if (!consumeIf("vE")) {
Richard Smithc20d1442018-08-20 20:14:49 +00002779 do {
Pavel Labathba825192018-10-16 14:29:14 +00002780 Node *P = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00002781 if (P == nullptr)
2782 return nullptr;
2783 Names.push_back(P);
2784 } while (!consumeIf('E'));
Richard Smithc20d1442018-08-20 20:14:49 +00002785 }
Richard Smithdf1c14c2019-09-06 23:53:21 +00002786 NodeArray Params = popTrailingNodeArray(ParamsBegin);
2787
Richard Smithc20d1442018-08-20 20:14:49 +00002788 StringView Count = parseNumber();
2789 if (!consumeIf('_'))
2790 return nullptr;
Richard Smithdf1c14c2019-09-06 23:53:21 +00002791 return make<ClosureTypeName>(TempParams, Params, Count);
Richard Smithc20d1442018-08-20 20:14:49 +00002792 }
Erik Pilkington974b6542019-01-17 21:37:51 +00002793 if (consumeIf("Ub")) {
2794 (void)parseNumber();
2795 if (!consumeIf('_'))
2796 return nullptr;
2797 return make<NameType>("'block-literal'");
2798 }
Richard Smithc20d1442018-08-20 20:14:49 +00002799 return nullptr;
2800}
2801
2802// <source-name> ::= <positive length number> <identifier>
Pavel Labathba825192018-10-16 14:29:14 +00002803template <typename Derived, typename Alloc>
2804Node *AbstractManglingParser<Derived, Alloc>::parseSourceName(NameState *) {
Richard Smithc20d1442018-08-20 20:14:49 +00002805 size_t Length = 0;
2806 if (parsePositiveInteger(&Length))
2807 return nullptr;
2808 if (numLeft() < Length || Length == 0)
2809 return nullptr;
2810 StringView Name(First, First + Length);
2811 First += Length;
2812 if (Name.startsWith("_GLOBAL__N"))
2813 return make<NameType>("(anonymous namespace)");
2814 return make<NameType>(Name);
2815}
2816
2817// <operator-name> ::= aa # &&
2818// ::= ad # & (unary)
2819// ::= an # &
2820// ::= aN # &=
2821// ::= aS # =
2822// ::= cl # ()
2823// ::= cm # ,
2824// ::= co # ~
2825// ::= cv <type> # (cast)
2826// ::= da # delete[]
2827// ::= de # * (unary)
2828// ::= dl # delete
2829// ::= dv # /
2830// ::= dV # /=
2831// ::= eo # ^
2832// ::= eO # ^=
2833// ::= eq # ==
2834// ::= ge # >=
2835// ::= gt # >
2836// ::= ix # []
2837// ::= le # <=
2838// ::= li <source-name> # operator ""
2839// ::= ls # <<
2840// ::= lS # <<=
2841// ::= lt # <
2842// ::= mi # -
2843// ::= mI # -=
2844// ::= ml # *
2845// ::= mL # *=
2846// ::= mm # -- (postfix in <expression> context)
2847// ::= na # new[]
2848// ::= ne # !=
2849// ::= ng # - (unary)
2850// ::= nt # !
2851// ::= nw # new
2852// ::= oo # ||
2853// ::= or # |
2854// ::= oR # |=
2855// ::= pm # ->*
2856// ::= pl # +
2857// ::= pL # +=
2858// ::= pp # ++ (postfix in <expression> context)
2859// ::= ps # + (unary)
2860// ::= pt # ->
2861// ::= qu # ?
2862// ::= rm # %
2863// ::= rM # %=
2864// ::= rs # >>
2865// ::= rS # >>=
2866// ::= ss # <=> C++2a
2867// ::= v <digit> <source-name> # vendor extended operator
Pavel Labathba825192018-10-16 14:29:14 +00002868template <typename Derived, typename Alloc>
2869Node *
2870AbstractManglingParser<Derived, Alloc>::parseOperatorName(NameState *State) {
Richard Smithc20d1442018-08-20 20:14:49 +00002871 switch (look()) {
2872 case 'a':
2873 switch (look(1)) {
2874 case 'a':
2875 First += 2;
2876 return make<NameType>("operator&&");
2877 case 'd':
2878 case 'n':
2879 First += 2;
2880 return make<NameType>("operator&");
2881 case 'N':
2882 First += 2;
2883 return make<NameType>("operator&=");
2884 case 'S':
2885 First += 2;
2886 return make<NameType>("operator=");
2887 }
2888 return nullptr;
2889 case 'c':
2890 switch (look(1)) {
2891 case 'l':
2892 First += 2;
2893 return make<NameType>("operator()");
2894 case 'm':
2895 First += 2;
2896 return make<NameType>("operator,");
2897 case 'o':
2898 First += 2;
2899 return make<NameType>("operator~");
2900 // ::= cv <type> # (cast)
2901 case 'v': {
2902 First += 2;
2903 SwapAndRestore<bool> SaveTemplate(TryToParseTemplateArgs, false);
2904 // If we're parsing an encoding, State != nullptr and the conversion
2905 // operators' <type> could have a <template-param> that refers to some
2906 // <template-arg>s further ahead in the mangled name.
2907 SwapAndRestore<bool> SavePermit(PermitForwardTemplateReferences,
2908 PermitForwardTemplateReferences ||
2909 State != nullptr);
Pavel Labathba825192018-10-16 14:29:14 +00002910 Node *Ty = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00002911 if (Ty == nullptr)
2912 return nullptr;
2913 if (State) State->CtorDtorConversion = true;
2914 return make<ConversionOperatorType>(Ty);
2915 }
2916 }
2917 return nullptr;
2918 case 'd':
2919 switch (look(1)) {
2920 case 'a':
2921 First += 2;
2922 return make<NameType>("operator delete[]");
2923 case 'e':
2924 First += 2;
2925 return make<NameType>("operator*");
2926 case 'l':
2927 First += 2;
2928 return make<NameType>("operator delete");
2929 case 'v':
2930 First += 2;
2931 return make<NameType>("operator/");
2932 case 'V':
2933 First += 2;
2934 return make<NameType>("operator/=");
2935 }
2936 return nullptr;
2937 case 'e':
2938 switch (look(1)) {
2939 case 'o':
2940 First += 2;
2941 return make<NameType>("operator^");
2942 case 'O':
2943 First += 2;
2944 return make<NameType>("operator^=");
2945 case 'q':
2946 First += 2;
2947 return make<NameType>("operator==");
2948 }
2949 return nullptr;
2950 case 'g':
2951 switch (look(1)) {
2952 case 'e':
2953 First += 2;
2954 return make<NameType>("operator>=");
2955 case 't':
2956 First += 2;
2957 return make<NameType>("operator>");
2958 }
2959 return nullptr;
2960 case 'i':
2961 if (look(1) == 'x') {
2962 First += 2;
2963 return make<NameType>("operator[]");
2964 }
2965 return nullptr;
2966 case 'l':
2967 switch (look(1)) {
2968 case 'e':
2969 First += 2;
2970 return make<NameType>("operator<=");
2971 // ::= li <source-name> # operator ""
2972 case 'i': {
2973 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00002974 Node *SN = getDerived().parseSourceName(State);
Richard Smithc20d1442018-08-20 20:14:49 +00002975 if (SN == nullptr)
2976 return nullptr;
2977 return make<LiteralOperator>(SN);
2978 }
2979 case 's':
2980 First += 2;
2981 return make<NameType>("operator<<");
2982 case 'S':
2983 First += 2;
2984 return make<NameType>("operator<<=");
2985 case 't':
2986 First += 2;
2987 return make<NameType>("operator<");
2988 }
2989 return nullptr;
2990 case 'm':
2991 switch (look(1)) {
2992 case 'i':
2993 First += 2;
2994 return make<NameType>("operator-");
2995 case 'I':
2996 First += 2;
2997 return make<NameType>("operator-=");
2998 case 'l':
2999 First += 2;
3000 return make<NameType>("operator*");
3001 case 'L':
3002 First += 2;
3003 return make<NameType>("operator*=");
3004 case 'm':
3005 First += 2;
3006 return make<NameType>("operator--");
3007 }
3008 return nullptr;
3009 case 'n':
3010 switch (look(1)) {
3011 case 'a':
3012 First += 2;
3013 return make<NameType>("operator new[]");
3014 case 'e':
3015 First += 2;
3016 return make<NameType>("operator!=");
3017 case 'g':
3018 First += 2;
3019 return make<NameType>("operator-");
3020 case 't':
3021 First += 2;
3022 return make<NameType>("operator!");
3023 case 'w':
3024 First += 2;
3025 return make<NameType>("operator new");
3026 }
3027 return nullptr;
3028 case 'o':
3029 switch (look(1)) {
3030 case 'o':
3031 First += 2;
3032 return make<NameType>("operator||");
3033 case 'r':
3034 First += 2;
3035 return make<NameType>("operator|");
3036 case 'R':
3037 First += 2;
3038 return make<NameType>("operator|=");
3039 }
3040 return nullptr;
3041 case 'p':
3042 switch (look(1)) {
3043 case 'm':
3044 First += 2;
3045 return make<NameType>("operator->*");
3046 case 'l':
3047 First += 2;
3048 return make<NameType>("operator+");
3049 case 'L':
3050 First += 2;
3051 return make<NameType>("operator+=");
3052 case 'p':
3053 First += 2;
3054 return make<NameType>("operator++");
3055 case 's':
3056 First += 2;
3057 return make<NameType>("operator+");
3058 case 't':
3059 First += 2;
3060 return make<NameType>("operator->");
3061 }
3062 return nullptr;
3063 case 'q':
3064 if (look(1) == 'u') {
3065 First += 2;
3066 return make<NameType>("operator?");
3067 }
3068 return nullptr;
3069 case 'r':
3070 switch (look(1)) {
3071 case 'm':
3072 First += 2;
3073 return make<NameType>("operator%");
3074 case 'M':
3075 First += 2;
3076 return make<NameType>("operator%=");
3077 case 's':
3078 First += 2;
3079 return make<NameType>("operator>>");
3080 case 'S':
3081 First += 2;
3082 return make<NameType>("operator>>=");
3083 }
3084 return nullptr;
3085 case 's':
3086 if (look(1) == 's') {
3087 First += 2;
3088 return make<NameType>("operator<=>");
3089 }
3090 return nullptr;
3091 // ::= v <digit> <source-name> # vendor extended operator
3092 case 'v':
3093 if (std::isdigit(look(1))) {
3094 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00003095 Node *SN = getDerived().parseSourceName(State);
Richard Smithc20d1442018-08-20 20:14:49 +00003096 if (SN == nullptr)
3097 return nullptr;
3098 return make<ConversionOperatorType>(SN);
3099 }
3100 return nullptr;
3101 }
3102 return nullptr;
3103}
3104
3105// <ctor-dtor-name> ::= C1 # complete object constructor
3106// ::= C2 # base object constructor
3107// ::= C3 # complete object allocating constructor
Nico Weber29294792019-04-03 23:14:33 +00003108// extension ::= C4 # gcc old-style "[unified]" constructor
3109// extension ::= C5 # the COMDAT used for ctors
Richard Smithc20d1442018-08-20 20:14:49 +00003110// ::= D0 # deleting destructor
3111// ::= D1 # complete object destructor
3112// ::= D2 # base object destructor
Nico Weber29294792019-04-03 23:14:33 +00003113// extension ::= D4 # gcc old-style "[unified]" destructor
3114// extension ::= D5 # the COMDAT used for dtors
Pavel Labathba825192018-10-16 14:29:14 +00003115template <typename Derived, typename Alloc>
3116Node *
3117AbstractManglingParser<Derived, Alloc>::parseCtorDtorName(Node *&SoFar,
3118 NameState *State) {
Richard Smithc20d1442018-08-20 20:14:49 +00003119 if (SoFar->getKind() == Node::KSpecialSubstitution) {
3120 auto SSK = static_cast<SpecialSubstitution *>(SoFar)->SSK;
3121 switch (SSK) {
3122 case SpecialSubKind::string:
3123 case SpecialSubKind::istream:
3124 case SpecialSubKind::ostream:
3125 case SpecialSubKind::iostream:
3126 SoFar = make<ExpandedSpecialSubstitution>(SSK);
Richard Smithb485b352018-08-24 23:30:26 +00003127 if (!SoFar)
3128 return nullptr;
Reid Klecknere76aabe2018-11-01 18:24:03 +00003129 break;
Richard Smithc20d1442018-08-20 20:14:49 +00003130 default:
3131 break;
3132 }
3133 }
3134
3135 if (consumeIf('C')) {
3136 bool IsInherited = consumeIf('I');
Nico Weber29294792019-04-03 23:14:33 +00003137 if (look() != '1' && look() != '2' && look() != '3' && look() != '4' &&
3138 look() != '5')
Richard Smithc20d1442018-08-20 20:14:49 +00003139 return nullptr;
Pavel Labathf4e67eb2018-10-10 08:39:16 +00003140 int Variant = look() - '0';
Richard Smithc20d1442018-08-20 20:14:49 +00003141 ++First;
3142 if (State) State->CtorDtorConversion = true;
3143 if (IsInherited) {
Pavel Labathba825192018-10-16 14:29:14 +00003144 if (getDerived().parseName(State) == nullptr)
Richard Smithc20d1442018-08-20 20:14:49 +00003145 return nullptr;
3146 }
Nico Weber29294792019-04-03 23:14:33 +00003147 return make<CtorDtorName>(SoFar, /*IsDtor=*/false, Variant);
Richard Smithc20d1442018-08-20 20:14:49 +00003148 }
3149
Nico Weber29294792019-04-03 23:14:33 +00003150 if (look() == 'D' && (look(1) == '0' || look(1) == '1' || look(1) == '2' ||
3151 look(1) == '4' || look(1) == '5')) {
Pavel Labathf4e67eb2018-10-10 08:39:16 +00003152 int Variant = look(1) - '0';
Richard Smithc20d1442018-08-20 20:14:49 +00003153 First += 2;
3154 if (State) State->CtorDtorConversion = true;
Nico Weber29294792019-04-03 23:14:33 +00003155 return make<CtorDtorName>(SoFar, /*IsDtor=*/true, Variant);
Richard Smithc20d1442018-08-20 20:14:49 +00003156 }
3157
3158 return nullptr;
3159}
3160
3161// <nested-name> ::= N [<CV-Qualifiers>] [<ref-qualifier>] <prefix> <unqualified-name> E
3162// ::= N [<CV-Qualifiers>] [<ref-qualifier>] <template-prefix> <template-args> E
3163//
3164// <prefix> ::= <prefix> <unqualified-name>
3165// ::= <template-prefix> <template-args>
3166// ::= <template-param>
3167// ::= <decltype>
3168// ::= # empty
3169// ::= <substitution>
3170// ::= <prefix> <data-member-prefix>
3171// extension ::= L
3172//
3173// <data-member-prefix> := <member source-name> [<template-args>] M
3174//
3175// <template-prefix> ::= <prefix> <template unqualified-name>
3176// ::= <template-param>
3177// ::= <substitution>
Pavel Labathba825192018-10-16 14:29:14 +00003178template <typename Derived, typename Alloc>
3179Node *
3180AbstractManglingParser<Derived, Alloc>::parseNestedName(NameState *State) {
Richard Smithc20d1442018-08-20 20:14:49 +00003181 if (!consumeIf('N'))
3182 return nullptr;
3183
3184 Qualifiers CVTmp = parseCVQualifiers();
3185 if (State) State->CVQualifiers = CVTmp;
3186
3187 if (consumeIf('O')) {
3188 if (State) State->ReferenceQualifier = FrefQualRValue;
3189 } else if (consumeIf('R')) {
3190 if (State) State->ReferenceQualifier = FrefQualLValue;
3191 } else
3192 if (State) State->ReferenceQualifier = FrefQualNone;
3193
3194 Node *SoFar = nullptr;
3195 auto PushComponent = [&](Node *Comp) {
Richard Smithb485b352018-08-24 23:30:26 +00003196 if (!Comp) return false;
Richard Smithc20d1442018-08-20 20:14:49 +00003197 if (SoFar) SoFar = make<NestedName>(SoFar, Comp);
3198 else SoFar = Comp;
3199 if (State) State->EndsWithTemplateArgs = false;
Richard Smithb485b352018-08-24 23:30:26 +00003200 return SoFar != nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00003201 };
3202
Richard Smithb485b352018-08-24 23:30:26 +00003203 if (consumeIf("St")) {
Richard Smithc20d1442018-08-20 20:14:49 +00003204 SoFar = make<NameType>("std");
Richard Smithb485b352018-08-24 23:30:26 +00003205 if (!SoFar)
3206 return nullptr;
3207 }
Richard Smithc20d1442018-08-20 20:14:49 +00003208
3209 while (!consumeIf('E')) {
3210 consumeIf('L'); // extension
3211
3212 // <data-member-prefix> := <member source-name> [<template-args>] M
3213 if (consumeIf('M')) {
3214 if (SoFar == nullptr)
3215 return nullptr;
3216 continue;
3217 }
3218
3219 // ::= <template-param>
3220 if (look() == 'T') {
Pavel Labathba825192018-10-16 14:29:14 +00003221 if (!PushComponent(getDerived().parseTemplateParam()))
Richard Smithc20d1442018-08-20 20:14:49 +00003222 return nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00003223 Subs.push_back(SoFar);
3224 continue;
3225 }
3226
3227 // ::= <template-prefix> <template-args>
3228 if (look() == 'I') {
Pavel Labathba825192018-10-16 14:29:14 +00003229 Node *TA = getDerived().parseTemplateArgs(State != nullptr);
Richard Smithc20d1442018-08-20 20:14:49 +00003230 if (TA == nullptr || SoFar == nullptr)
3231 return nullptr;
3232 SoFar = make<NameWithTemplateArgs>(SoFar, TA);
Richard Smithb485b352018-08-24 23:30:26 +00003233 if (!SoFar)
3234 return nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00003235 if (State) State->EndsWithTemplateArgs = true;
3236 Subs.push_back(SoFar);
3237 continue;
3238 }
3239
3240 // ::= <decltype>
3241 if (look() == 'D' && (look(1) == 't' || look(1) == 'T')) {
Pavel Labathba825192018-10-16 14:29:14 +00003242 if (!PushComponent(getDerived().parseDecltype()))
Richard Smithc20d1442018-08-20 20:14:49 +00003243 return nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00003244 Subs.push_back(SoFar);
3245 continue;
3246 }
3247
3248 // ::= <substitution>
3249 if (look() == 'S' && look(1) != 't') {
Pavel Labathba825192018-10-16 14:29:14 +00003250 Node *S = getDerived().parseSubstitution();
Richard Smithb485b352018-08-24 23:30:26 +00003251 if (!PushComponent(S))
Richard Smithc20d1442018-08-20 20:14:49 +00003252 return nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00003253 if (SoFar != S)
3254 Subs.push_back(S);
3255 continue;
3256 }
3257
3258 // Parse an <unqualified-name> thats actually a <ctor-dtor-name>.
3259 if (look() == 'C' || (look() == 'D' && look(1) != 'C')) {
3260 if (SoFar == nullptr)
3261 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00003262 if (!PushComponent(getDerived().parseCtorDtorName(SoFar, State)))
Richard Smithc20d1442018-08-20 20:14:49 +00003263 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00003264 SoFar = getDerived().parseAbiTags(SoFar);
Richard Smithc20d1442018-08-20 20:14:49 +00003265 if (SoFar == nullptr)
3266 return nullptr;
3267 Subs.push_back(SoFar);
3268 continue;
3269 }
3270
3271 // ::= <prefix> <unqualified-name>
Pavel Labathba825192018-10-16 14:29:14 +00003272 if (!PushComponent(getDerived().parseUnqualifiedName(State)))
Richard Smithc20d1442018-08-20 20:14:49 +00003273 return nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00003274 Subs.push_back(SoFar);
3275 }
3276
3277 if (SoFar == nullptr || Subs.empty())
3278 return nullptr;
3279
3280 Subs.pop_back();
3281 return SoFar;
3282}
3283
3284// <simple-id> ::= <source-name> [ <template-args> ]
Pavel Labathba825192018-10-16 14:29:14 +00003285template <typename Derived, typename Alloc>
3286Node *AbstractManglingParser<Derived, Alloc>::parseSimpleId() {
3287 Node *SN = getDerived().parseSourceName(/*NameState=*/nullptr);
Richard Smithc20d1442018-08-20 20:14:49 +00003288 if (SN == nullptr)
3289 return nullptr;
3290 if (look() == 'I') {
Pavel Labathba825192018-10-16 14:29:14 +00003291 Node *TA = getDerived().parseTemplateArgs();
Richard Smithc20d1442018-08-20 20:14:49 +00003292 if (TA == nullptr)
3293 return nullptr;
3294 return make<NameWithTemplateArgs>(SN, TA);
3295 }
3296 return SN;
3297}
3298
3299// <destructor-name> ::= <unresolved-type> # e.g., ~T or ~decltype(f())
3300// ::= <simple-id> # e.g., ~A<2*N>
Pavel Labathba825192018-10-16 14:29:14 +00003301template <typename Derived, typename Alloc>
3302Node *AbstractManglingParser<Derived, Alloc>::parseDestructorName() {
Richard Smithc20d1442018-08-20 20:14:49 +00003303 Node *Result;
3304 if (std::isdigit(look()))
Pavel Labathba825192018-10-16 14:29:14 +00003305 Result = getDerived().parseSimpleId();
Richard Smithc20d1442018-08-20 20:14:49 +00003306 else
Pavel Labathba825192018-10-16 14:29:14 +00003307 Result = getDerived().parseUnresolvedType();
Richard Smithc20d1442018-08-20 20:14:49 +00003308 if (Result == nullptr)
3309 return nullptr;
3310 return make<DtorName>(Result);
3311}
3312
3313// <unresolved-type> ::= <template-param>
3314// ::= <decltype>
3315// ::= <substitution>
Pavel Labathba825192018-10-16 14:29:14 +00003316template <typename Derived, typename Alloc>
3317Node *AbstractManglingParser<Derived, Alloc>::parseUnresolvedType() {
Richard Smithc20d1442018-08-20 20:14:49 +00003318 if (look() == 'T') {
Pavel Labathba825192018-10-16 14:29:14 +00003319 Node *TP = getDerived().parseTemplateParam();
Richard Smithc20d1442018-08-20 20:14:49 +00003320 if (TP == nullptr)
3321 return nullptr;
3322 Subs.push_back(TP);
3323 return TP;
3324 }
3325 if (look() == 'D') {
Pavel Labathba825192018-10-16 14:29:14 +00003326 Node *DT = getDerived().parseDecltype();
Richard Smithc20d1442018-08-20 20:14:49 +00003327 if (DT == nullptr)
3328 return nullptr;
3329 Subs.push_back(DT);
3330 return DT;
3331 }
Pavel Labathba825192018-10-16 14:29:14 +00003332 return getDerived().parseSubstitution();
Richard Smithc20d1442018-08-20 20:14:49 +00003333}
3334
3335// <base-unresolved-name> ::= <simple-id> # unresolved name
3336// extension ::= <operator-name> # unresolved operator-function-id
3337// extension ::= <operator-name> <template-args> # unresolved operator template-id
3338// ::= on <operator-name> # unresolved operator-function-id
3339// ::= on <operator-name> <template-args> # unresolved operator template-id
3340// ::= dn <destructor-name> # destructor or pseudo-destructor;
3341// # e.g. ~X or ~X<N-1>
Pavel Labathba825192018-10-16 14:29:14 +00003342template <typename Derived, typename Alloc>
3343Node *AbstractManglingParser<Derived, Alloc>::parseBaseUnresolvedName() {
Richard Smithc20d1442018-08-20 20:14:49 +00003344 if (std::isdigit(look()))
Pavel Labathba825192018-10-16 14:29:14 +00003345 return getDerived().parseSimpleId();
Richard Smithc20d1442018-08-20 20:14:49 +00003346
3347 if (consumeIf("dn"))
Pavel Labathba825192018-10-16 14:29:14 +00003348 return getDerived().parseDestructorName();
Richard Smithc20d1442018-08-20 20:14:49 +00003349
3350 consumeIf("on");
3351
Pavel Labathba825192018-10-16 14:29:14 +00003352 Node *Oper = getDerived().parseOperatorName(/*NameState=*/nullptr);
Richard Smithc20d1442018-08-20 20:14:49 +00003353 if (Oper == nullptr)
3354 return nullptr;
3355 if (look() == 'I') {
Pavel Labathba825192018-10-16 14:29:14 +00003356 Node *TA = getDerived().parseTemplateArgs();
Richard Smithc20d1442018-08-20 20:14:49 +00003357 if (TA == nullptr)
3358 return nullptr;
3359 return make<NameWithTemplateArgs>(Oper, TA);
3360 }
3361 return Oper;
3362}
3363
3364// <unresolved-name>
3365// extension ::= srN <unresolved-type> [<template-args>] <unresolved-qualifier-level>* E <base-unresolved-name>
3366// ::= [gs] <base-unresolved-name> # x or (with "gs") ::x
3367// ::= [gs] sr <unresolved-qualifier-level>+ E <base-unresolved-name>
3368// # A::x, N::y, A<T>::z; "gs" means leading "::"
3369// ::= sr <unresolved-type> <base-unresolved-name> # T::x / decltype(p)::x
3370// extension ::= sr <unresolved-type> <template-args> <base-unresolved-name>
3371// # T::N::x /decltype(p)::N::x
3372// (ignored) ::= srN <unresolved-type> <unresolved-qualifier-level>+ E <base-unresolved-name>
3373//
3374// <unresolved-qualifier-level> ::= <simple-id>
Pavel Labathba825192018-10-16 14:29:14 +00003375template <typename Derived, typename Alloc>
3376Node *AbstractManglingParser<Derived, Alloc>::parseUnresolvedName() {
Richard Smithc20d1442018-08-20 20:14:49 +00003377 Node *SoFar = nullptr;
3378
3379 // srN <unresolved-type> [<template-args>] <unresolved-qualifier-level>* E <base-unresolved-name>
3380 // srN <unresolved-type> <unresolved-qualifier-level>+ E <base-unresolved-name>
3381 if (consumeIf("srN")) {
Pavel Labathba825192018-10-16 14:29:14 +00003382 SoFar = getDerived().parseUnresolvedType();
Richard Smithc20d1442018-08-20 20:14:49 +00003383 if (SoFar == nullptr)
3384 return nullptr;
3385
3386 if (look() == 'I') {
Pavel Labathba825192018-10-16 14:29:14 +00003387 Node *TA = getDerived().parseTemplateArgs();
Richard Smithc20d1442018-08-20 20:14:49 +00003388 if (TA == nullptr)
3389 return nullptr;
3390 SoFar = make<NameWithTemplateArgs>(SoFar, TA);
Richard Smithb485b352018-08-24 23:30:26 +00003391 if (!SoFar)
3392 return nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00003393 }
3394
3395 while (!consumeIf('E')) {
Pavel Labathba825192018-10-16 14:29:14 +00003396 Node *Qual = getDerived().parseSimpleId();
Richard Smithc20d1442018-08-20 20:14:49 +00003397 if (Qual == nullptr)
3398 return nullptr;
3399 SoFar = make<QualifiedName>(SoFar, Qual);
Richard Smithb485b352018-08-24 23:30:26 +00003400 if (!SoFar)
3401 return nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00003402 }
3403
Pavel Labathba825192018-10-16 14:29:14 +00003404 Node *Base = getDerived().parseBaseUnresolvedName();
Richard Smithc20d1442018-08-20 20:14:49 +00003405 if (Base == nullptr)
3406 return nullptr;
3407 return make<QualifiedName>(SoFar, Base);
3408 }
3409
3410 bool Global = consumeIf("gs");
3411
3412 // [gs] <base-unresolved-name> # x or (with "gs") ::x
3413 if (!consumeIf("sr")) {
Pavel Labathba825192018-10-16 14:29:14 +00003414 SoFar = getDerived().parseBaseUnresolvedName();
Richard Smithc20d1442018-08-20 20:14:49 +00003415 if (SoFar == nullptr)
3416 return nullptr;
3417 if (Global)
3418 SoFar = make<GlobalQualifiedName>(SoFar);
3419 return SoFar;
3420 }
3421
3422 // [gs] sr <unresolved-qualifier-level>+ E <base-unresolved-name>
3423 if (std::isdigit(look())) {
3424 do {
Pavel Labathba825192018-10-16 14:29:14 +00003425 Node *Qual = getDerived().parseSimpleId();
Richard Smithc20d1442018-08-20 20:14:49 +00003426 if (Qual == nullptr)
3427 return nullptr;
3428 if (SoFar)
3429 SoFar = make<QualifiedName>(SoFar, Qual);
3430 else if (Global)
3431 SoFar = make<GlobalQualifiedName>(Qual);
3432 else
3433 SoFar = Qual;
Richard Smithb485b352018-08-24 23:30:26 +00003434 if (!SoFar)
3435 return nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00003436 } while (!consumeIf('E'));
3437 }
3438 // sr <unresolved-type> <base-unresolved-name>
3439 // sr <unresolved-type> <template-args> <base-unresolved-name>
3440 else {
Pavel Labathba825192018-10-16 14:29:14 +00003441 SoFar = getDerived().parseUnresolvedType();
Richard Smithc20d1442018-08-20 20:14:49 +00003442 if (SoFar == nullptr)
3443 return nullptr;
3444
3445 if (look() == 'I') {
Pavel Labathba825192018-10-16 14:29:14 +00003446 Node *TA = getDerived().parseTemplateArgs();
Richard Smithc20d1442018-08-20 20:14:49 +00003447 if (TA == nullptr)
3448 return nullptr;
3449 SoFar = make<NameWithTemplateArgs>(SoFar, TA);
Richard Smithb485b352018-08-24 23:30:26 +00003450 if (!SoFar)
3451 return nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00003452 }
3453 }
3454
3455 assert(SoFar != nullptr);
3456
Pavel Labathba825192018-10-16 14:29:14 +00003457 Node *Base = getDerived().parseBaseUnresolvedName();
Richard Smithc20d1442018-08-20 20:14:49 +00003458 if (Base == nullptr)
3459 return nullptr;
3460 return make<QualifiedName>(SoFar, Base);
3461}
3462
3463// <abi-tags> ::= <abi-tag> [<abi-tags>]
3464// <abi-tag> ::= B <source-name>
Pavel Labathba825192018-10-16 14:29:14 +00003465template <typename Derived, typename Alloc>
3466Node *AbstractManglingParser<Derived, Alloc>::parseAbiTags(Node *N) {
Richard Smithc20d1442018-08-20 20:14:49 +00003467 while (consumeIf('B')) {
3468 StringView SN = parseBareSourceName();
3469 if (SN.empty())
3470 return nullptr;
3471 N = make<AbiTagAttr>(N, SN);
Richard Smithb485b352018-08-24 23:30:26 +00003472 if (!N)
3473 return nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00003474 }
3475 return N;
3476}
3477
3478// <number> ::= [n] <non-negative decimal integer>
Pavel Labathba825192018-10-16 14:29:14 +00003479template <typename Alloc, typename Derived>
3480StringView
3481AbstractManglingParser<Alloc, Derived>::parseNumber(bool AllowNegative) {
Richard Smithc20d1442018-08-20 20:14:49 +00003482 const char *Tmp = First;
3483 if (AllowNegative)
3484 consumeIf('n');
3485 if (numLeft() == 0 || !std::isdigit(*First))
3486 return StringView();
3487 while (numLeft() != 0 && std::isdigit(*First))
3488 ++First;
3489 return StringView(Tmp, First);
3490}
3491
3492// <positive length number> ::= [0-9]*
Pavel Labathba825192018-10-16 14:29:14 +00003493template <typename Alloc, typename Derived>
3494bool AbstractManglingParser<Alloc, Derived>::parsePositiveInteger(size_t *Out) {
Richard Smithc20d1442018-08-20 20:14:49 +00003495 *Out = 0;
3496 if (look() < '0' || look() > '9')
3497 return true;
3498 while (look() >= '0' && look() <= '9') {
3499 *Out *= 10;
3500 *Out += static_cast<size_t>(consume() - '0');
3501 }
3502 return false;
3503}
3504
Pavel Labathba825192018-10-16 14:29:14 +00003505template <typename Alloc, typename Derived>
3506StringView AbstractManglingParser<Alloc, Derived>::parseBareSourceName() {
Richard Smithc20d1442018-08-20 20:14:49 +00003507 size_t Int = 0;
3508 if (parsePositiveInteger(&Int) || numLeft() < Int)
3509 return StringView();
3510 StringView R(First, First + Int);
3511 First += Int;
3512 return R;
3513}
3514
3515// <function-type> ::= [<CV-qualifiers>] [<exception-spec>] [Dx] F [Y] <bare-function-type> [<ref-qualifier>] E
3516//
3517// <exception-spec> ::= Do # non-throwing exception-specification (e.g., noexcept, throw())
3518// ::= DO <expression> E # computed (instantiation-dependent) noexcept
3519// ::= Dw <type>+ E # dynamic exception specification with instantiation-dependent types
3520//
3521// <ref-qualifier> ::= R # & ref-qualifier
3522// <ref-qualifier> ::= O # && ref-qualifier
Pavel Labathba825192018-10-16 14:29:14 +00003523template <typename Derived, typename Alloc>
3524Node *AbstractManglingParser<Derived, Alloc>::parseFunctionType() {
Richard Smithc20d1442018-08-20 20:14:49 +00003525 Qualifiers CVQuals = parseCVQualifiers();
3526
3527 Node *ExceptionSpec = nullptr;
3528 if (consumeIf("Do")) {
3529 ExceptionSpec = make<NameType>("noexcept");
Richard Smithb485b352018-08-24 23:30:26 +00003530 if (!ExceptionSpec)
3531 return nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00003532 } else if (consumeIf("DO")) {
Pavel Labathba825192018-10-16 14:29:14 +00003533 Node *E = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00003534 if (E == nullptr || !consumeIf('E'))
3535 return nullptr;
3536 ExceptionSpec = make<NoexceptSpec>(E);
Richard Smithb485b352018-08-24 23:30:26 +00003537 if (!ExceptionSpec)
3538 return nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00003539 } else if (consumeIf("Dw")) {
3540 size_t SpecsBegin = Names.size();
3541 while (!consumeIf('E')) {
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 ExceptionSpec =
3548 make<DynamicExceptionSpec>(popTrailingNodeArray(SpecsBegin));
Richard Smithb485b352018-08-24 23:30:26 +00003549 if (!ExceptionSpec)
3550 return nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00003551 }
3552
3553 consumeIf("Dx"); // transaction safe
3554
3555 if (!consumeIf('F'))
3556 return nullptr;
3557 consumeIf('Y'); // extern "C"
Pavel Labathba825192018-10-16 14:29:14 +00003558 Node *ReturnType = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00003559 if (ReturnType == nullptr)
3560 return nullptr;
3561
3562 FunctionRefQual ReferenceQualifier = FrefQualNone;
3563 size_t ParamsBegin = Names.size();
3564 while (true) {
3565 if (consumeIf('E'))
3566 break;
3567 if (consumeIf('v'))
3568 continue;
3569 if (consumeIf("RE")) {
3570 ReferenceQualifier = FrefQualLValue;
3571 break;
3572 }
3573 if (consumeIf("OE")) {
3574 ReferenceQualifier = FrefQualRValue;
3575 break;
3576 }
Pavel Labathba825192018-10-16 14:29:14 +00003577 Node *T = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00003578 if (T == nullptr)
3579 return nullptr;
3580 Names.push_back(T);
3581 }
3582
3583 NodeArray Params = popTrailingNodeArray(ParamsBegin);
3584 return make<FunctionType>(ReturnType, Params, CVQuals,
3585 ReferenceQualifier, ExceptionSpec);
3586}
3587
3588// extension:
3589// <vector-type> ::= Dv <positive dimension number> _ <extended element type>
3590// ::= Dv [<dimension expression>] _ <element type>
3591// <extended element type> ::= <element type>
3592// ::= p # AltiVec vector pixel
Pavel Labathba825192018-10-16 14:29:14 +00003593template <typename Derived, typename Alloc>
3594Node *AbstractManglingParser<Derived, Alloc>::parseVectorType() {
Richard Smithc20d1442018-08-20 20:14:49 +00003595 if (!consumeIf("Dv"))
3596 return nullptr;
3597 if (look() >= '1' && look() <= '9') {
Erik Pilkingtond7555e32019-11-04 10:47:44 -08003598 Node *DimensionNumber = make<NameType>(parseNumber());
3599 if (!DimensionNumber)
3600 return nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00003601 if (!consumeIf('_'))
3602 return nullptr;
3603 if (consumeIf('p'))
3604 return make<PixelVectorType>(DimensionNumber);
Pavel Labathba825192018-10-16 14:29:14 +00003605 Node *ElemType = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00003606 if (ElemType == nullptr)
3607 return nullptr;
3608 return make<VectorType>(ElemType, DimensionNumber);
3609 }
3610
3611 if (!consumeIf('_')) {
Pavel Labathba825192018-10-16 14:29:14 +00003612 Node *DimExpr = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00003613 if (!DimExpr)
3614 return nullptr;
3615 if (!consumeIf('_'))
3616 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00003617 Node *ElemType = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00003618 if (!ElemType)
3619 return nullptr;
3620 return make<VectorType>(ElemType, DimExpr);
3621 }
Pavel Labathba825192018-10-16 14:29:14 +00003622 Node *ElemType = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00003623 if (!ElemType)
3624 return nullptr;
Erik Pilkingtond7555e32019-11-04 10:47:44 -08003625 return make<VectorType>(ElemType, /*Dimension=*/nullptr);
Richard Smithc20d1442018-08-20 20:14:49 +00003626}
3627
3628// <decltype> ::= Dt <expression> E # decltype of an id-expression or class member access (C++0x)
3629// ::= DT <expression> E # decltype of an expression (C++0x)
Pavel Labathba825192018-10-16 14:29:14 +00003630template <typename Derived, typename Alloc>
3631Node *AbstractManglingParser<Derived, Alloc>::parseDecltype() {
Richard Smithc20d1442018-08-20 20:14:49 +00003632 if (!consumeIf('D'))
3633 return nullptr;
3634 if (!consumeIf('t') && !consumeIf('T'))
3635 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00003636 Node *E = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00003637 if (E == nullptr)
3638 return nullptr;
3639 if (!consumeIf('E'))
3640 return nullptr;
3641 return make<EnclosingExpr>("decltype(", E, ")");
3642}
3643
3644// <array-type> ::= A <positive dimension number> _ <element type>
3645// ::= A [<dimension expression>] _ <element type>
Pavel Labathba825192018-10-16 14:29:14 +00003646template <typename Derived, typename Alloc>
3647Node *AbstractManglingParser<Derived, Alloc>::parseArrayType() {
Richard Smithc20d1442018-08-20 20:14:49 +00003648 if (!consumeIf('A'))
3649 return nullptr;
3650
Erik Pilkingtond7555e32019-11-04 10:47:44 -08003651 Node *Dimension = nullptr;
Pavel Labathf4e67eb2018-10-10 08:39:16 +00003652
Richard Smithc20d1442018-08-20 20:14:49 +00003653 if (std::isdigit(look())) {
Erik Pilkingtond7555e32019-11-04 10:47:44 -08003654 Dimension = make<NameType>(parseNumber());
3655 if (!Dimension)
3656 return nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00003657 if (!consumeIf('_'))
3658 return nullptr;
Pavel Labathf4e67eb2018-10-10 08:39:16 +00003659 } else if (!consumeIf('_')) {
Pavel Labathba825192018-10-16 14:29:14 +00003660 Node *DimExpr = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00003661 if (DimExpr == nullptr)
3662 return nullptr;
3663 if (!consumeIf('_'))
3664 return nullptr;
Pavel Labathf4e67eb2018-10-10 08:39:16 +00003665 Dimension = DimExpr;
Richard Smithc20d1442018-08-20 20:14:49 +00003666 }
3667
Pavel Labathba825192018-10-16 14:29:14 +00003668 Node *Ty = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00003669 if (Ty == nullptr)
3670 return nullptr;
Pavel Labathf4e67eb2018-10-10 08:39:16 +00003671 return make<ArrayType>(Ty, Dimension);
Richard Smithc20d1442018-08-20 20:14:49 +00003672}
3673
3674// <pointer-to-member-type> ::= M <class type> <member type>
Pavel Labathba825192018-10-16 14:29:14 +00003675template <typename Derived, typename Alloc>
3676Node *AbstractManglingParser<Derived, Alloc>::parsePointerToMemberType() {
Richard Smithc20d1442018-08-20 20:14:49 +00003677 if (!consumeIf('M'))
3678 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00003679 Node *ClassType = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00003680 if (ClassType == nullptr)
3681 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00003682 Node *MemberType = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00003683 if (MemberType == nullptr)
3684 return nullptr;
3685 return make<PointerToMemberType>(ClassType, MemberType);
3686}
3687
3688// <class-enum-type> ::= <name> # non-dependent type name, dependent type name, or dependent typename-specifier
3689// ::= Ts <name> # dependent elaborated type specifier using 'struct' or 'class'
3690// ::= Tu <name> # dependent elaborated type specifier using 'union'
3691// ::= Te <name> # dependent elaborated type specifier using 'enum'
Pavel Labathba825192018-10-16 14:29:14 +00003692template <typename Derived, typename Alloc>
3693Node *AbstractManglingParser<Derived, Alloc>::parseClassEnumType() {
Richard Smithc20d1442018-08-20 20:14:49 +00003694 StringView ElabSpef;
3695 if (consumeIf("Ts"))
3696 ElabSpef = "struct";
3697 else if (consumeIf("Tu"))
3698 ElabSpef = "union";
3699 else if (consumeIf("Te"))
3700 ElabSpef = "enum";
3701
Pavel Labathba825192018-10-16 14:29:14 +00003702 Node *Name = getDerived().parseName();
Richard Smithc20d1442018-08-20 20:14:49 +00003703 if (Name == nullptr)
3704 return nullptr;
3705
3706 if (!ElabSpef.empty())
3707 return make<ElaboratedTypeSpefType>(ElabSpef, Name);
3708
3709 return Name;
3710}
3711
3712// <qualified-type> ::= <qualifiers> <type>
3713// <qualifiers> ::= <extended-qualifier>* <CV-qualifiers>
3714// <extended-qualifier> ::= U <source-name> [<template-args>] # vendor extended type qualifier
Pavel Labathba825192018-10-16 14:29:14 +00003715template <typename Derived, typename Alloc>
3716Node *AbstractManglingParser<Derived, Alloc>::parseQualifiedType() {
Richard Smithc20d1442018-08-20 20:14:49 +00003717 if (consumeIf('U')) {
3718 StringView Qual = parseBareSourceName();
3719 if (Qual.empty())
3720 return nullptr;
3721
Richard Smithc20d1442018-08-20 20:14:49 +00003722 // extension ::= U <objc-name> <objc-type> # objc-type<identifier>
3723 if (Qual.startsWith("objcproto")) {
3724 StringView ProtoSourceName = Qual.dropFront(std::strlen("objcproto"));
3725 StringView Proto;
3726 {
3727 SwapAndRestore<const char *> SaveFirst(First, ProtoSourceName.begin()),
3728 SaveLast(Last, ProtoSourceName.end());
3729 Proto = parseBareSourceName();
3730 }
3731 if (Proto.empty())
3732 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00003733 Node *Child = getDerived().parseQualifiedType();
Richard Smithc20d1442018-08-20 20:14:49 +00003734 if (Child == nullptr)
3735 return nullptr;
3736 return make<ObjCProtoName>(Child, Proto);
3737 }
3738
Alex Orlovf50df922021-03-24 10:21:32 +04003739 Node *TA = nullptr;
3740 if (look() == 'I') {
3741 TA = getDerived().parseTemplateArgs();
3742 if (TA == nullptr)
3743 return nullptr;
3744 }
3745
Pavel Labathba825192018-10-16 14:29:14 +00003746 Node *Child = getDerived().parseQualifiedType();
Richard Smithc20d1442018-08-20 20:14:49 +00003747 if (Child == nullptr)
3748 return nullptr;
Alex Orlovf50df922021-03-24 10:21:32 +04003749 return make<VendorExtQualType>(Child, Qual, TA);
Richard Smithc20d1442018-08-20 20:14:49 +00003750 }
3751
3752 Qualifiers Quals = parseCVQualifiers();
Pavel Labathba825192018-10-16 14:29:14 +00003753 Node *Ty = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00003754 if (Ty == nullptr)
3755 return nullptr;
3756 if (Quals != QualNone)
3757 Ty = make<QualType>(Ty, Quals);
3758 return Ty;
3759}
3760
3761// <type> ::= <builtin-type>
3762// ::= <qualified-type>
3763// ::= <function-type>
3764// ::= <class-enum-type>
3765// ::= <array-type>
3766// ::= <pointer-to-member-type>
3767// ::= <template-param>
3768// ::= <template-template-param> <template-args>
3769// ::= <decltype>
3770// ::= P <type> # pointer
3771// ::= R <type> # l-value reference
3772// ::= O <type> # r-value reference (C++11)
3773// ::= C <type> # complex pair (C99)
3774// ::= G <type> # imaginary (C99)
3775// ::= <substitution> # See Compression below
3776// extension ::= U <objc-name> <objc-type> # objc-type<identifier>
3777// extension ::= <vector-type> # <vector-type> starts with Dv
3778//
3779// <objc-name> ::= <k0 number> objcproto <k1 number> <identifier> # k0 = 9 + <number of digits in k1> + k1
3780// <objc-type> ::= <source-name> # PU<11+>objcproto 11objc_object<source-name> 11objc_object -> id<source-name>
Pavel Labathba825192018-10-16 14:29:14 +00003781template <typename Derived, typename Alloc>
3782Node *AbstractManglingParser<Derived, Alloc>::parseType() {
Richard Smithc20d1442018-08-20 20:14:49 +00003783 Node *Result = nullptr;
3784
Richard Smithc20d1442018-08-20 20:14:49 +00003785 switch (look()) {
3786 // ::= <qualified-type>
3787 case 'r':
3788 case 'V':
3789 case 'K': {
3790 unsigned AfterQuals = 0;
3791 if (look(AfterQuals) == 'r') ++AfterQuals;
3792 if (look(AfterQuals) == 'V') ++AfterQuals;
3793 if (look(AfterQuals) == 'K') ++AfterQuals;
3794
3795 if (look(AfterQuals) == 'F' ||
3796 (look(AfterQuals) == 'D' &&
3797 (look(AfterQuals + 1) == 'o' || look(AfterQuals + 1) == 'O' ||
3798 look(AfterQuals + 1) == 'w' || look(AfterQuals + 1) == 'x'))) {
Pavel Labathba825192018-10-16 14:29:14 +00003799 Result = getDerived().parseFunctionType();
Richard Smithc20d1442018-08-20 20:14:49 +00003800 break;
3801 }
Erik Pilkingtonf70e4d82019-01-17 20:37:51 +00003802 DEMANGLE_FALLTHROUGH;
Richard Smithc20d1442018-08-20 20:14:49 +00003803 }
3804 case 'U': {
Pavel Labathba825192018-10-16 14:29:14 +00003805 Result = getDerived().parseQualifiedType();
Richard Smithc20d1442018-08-20 20:14:49 +00003806 break;
3807 }
3808 // <builtin-type> ::= v # void
3809 case 'v':
3810 ++First;
3811 return make<NameType>("void");
3812 // ::= w # wchar_t
3813 case 'w':
3814 ++First;
3815 return make<NameType>("wchar_t");
3816 // ::= b # bool
3817 case 'b':
3818 ++First;
3819 return make<NameType>("bool");
3820 // ::= c # char
3821 case 'c':
3822 ++First;
3823 return make<NameType>("char");
3824 // ::= a # signed char
3825 case 'a':
3826 ++First;
3827 return make<NameType>("signed char");
3828 // ::= h # unsigned char
3829 case 'h':
3830 ++First;
3831 return make<NameType>("unsigned char");
3832 // ::= s # short
3833 case 's':
3834 ++First;
3835 return make<NameType>("short");
3836 // ::= t # unsigned short
3837 case 't':
3838 ++First;
3839 return make<NameType>("unsigned short");
3840 // ::= i # int
3841 case 'i':
3842 ++First;
3843 return make<NameType>("int");
3844 // ::= j # unsigned int
3845 case 'j':
3846 ++First;
3847 return make<NameType>("unsigned int");
3848 // ::= l # long
3849 case 'l':
3850 ++First;
3851 return make<NameType>("long");
3852 // ::= m # unsigned long
3853 case 'm':
3854 ++First;
3855 return make<NameType>("unsigned long");
3856 // ::= x # long long, __int64
3857 case 'x':
3858 ++First;
3859 return make<NameType>("long long");
3860 // ::= y # unsigned long long, __int64
3861 case 'y':
3862 ++First;
3863 return make<NameType>("unsigned long long");
3864 // ::= n # __int128
3865 case 'n':
3866 ++First;
3867 return make<NameType>("__int128");
3868 // ::= o # unsigned __int128
3869 case 'o':
3870 ++First;
3871 return make<NameType>("unsigned __int128");
3872 // ::= f # float
3873 case 'f':
3874 ++First;
3875 return make<NameType>("float");
3876 // ::= d # double
3877 case 'd':
3878 ++First;
3879 return make<NameType>("double");
3880 // ::= e # long double, __float80
3881 case 'e':
3882 ++First;
3883 return make<NameType>("long double");
3884 // ::= g # __float128
3885 case 'g':
3886 ++First;
3887 return make<NameType>("__float128");
3888 // ::= z # ellipsis
3889 case 'z':
3890 ++First;
3891 return make<NameType>("...");
3892
3893 // <builtin-type> ::= u <source-name> # vendor extended type
3894 case 'u': {
3895 ++First;
3896 StringView Res = parseBareSourceName();
3897 if (Res.empty())
3898 return nullptr;
Erik Pilkingtonb94a1f42019-06-10 21:02:39 +00003899 // Typically, <builtin-type>s are not considered substitution candidates,
3900 // but the exception to that exception is vendor extended types (Itanium C++
3901 // ABI 5.9.1).
3902 Result = make<NameType>(Res);
3903 break;
Richard Smithc20d1442018-08-20 20:14:49 +00003904 }
3905 case 'D':
3906 switch (look(1)) {
3907 // ::= Dd # IEEE 754r decimal floating point (64 bits)
3908 case 'd':
3909 First += 2;
3910 return make<NameType>("decimal64");
3911 // ::= De # IEEE 754r decimal floating point (128 bits)
3912 case 'e':
3913 First += 2;
3914 return make<NameType>("decimal128");
3915 // ::= Df # IEEE 754r decimal floating point (32 bits)
3916 case 'f':
3917 First += 2;
3918 return make<NameType>("decimal32");
3919 // ::= Dh # IEEE 754r half-precision floating point (16 bits)
3920 case 'h':
3921 First += 2;
Stuart Bradye8bf5772021-06-07 16:30:22 +01003922 return make<NameType>("half");
Pengfei Wang50e90b82021-09-23 11:02:25 +08003923 // ::= DF <number> _ # ISO/IEC TS 18661 binary floating point (N bits)
3924 case 'F': {
3925 First += 2;
3926 Node *DimensionNumber = make<NameType>(parseNumber());
3927 if (!DimensionNumber)
3928 return nullptr;
3929 if (!consumeIf('_'))
3930 return nullptr;
3931 return make<BinaryFPType>(DimensionNumber);
3932 }
Richard Smithc20d1442018-08-20 20:14:49 +00003933 // ::= Di # char32_t
3934 case 'i':
3935 First += 2;
3936 return make<NameType>("char32_t");
3937 // ::= Ds # char16_t
3938 case 's':
3939 First += 2;
3940 return make<NameType>("char16_t");
Erik Pilkingtonc3780e82019-06-28 19:54:19 +00003941 // ::= Du # char8_t (C++2a, not yet in the Itanium spec)
3942 case 'u':
3943 First += 2;
3944 return make<NameType>("char8_t");
Richard Smithc20d1442018-08-20 20:14:49 +00003945 // ::= Da # auto (in dependent new-expressions)
3946 case 'a':
3947 First += 2;
3948 return make<NameType>("auto");
3949 // ::= Dc # decltype(auto)
3950 case 'c':
3951 First += 2;
3952 return make<NameType>("decltype(auto)");
3953 // ::= Dn # std::nullptr_t (i.e., decltype(nullptr))
3954 case 'n':
3955 First += 2;
3956 return make<NameType>("std::nullptr_t");
3957
3958 // ::= <decltype>
3959 case 't':
3960 case 'T': {
Pavel Labathba825192018-10-16 14:29:14 +00003961 Result = getDerived().parseDecltype();
Richard Smithc20d1442018-08-20 20:14:49 +00003962 break;
3963 }
3964 // extension ::= <vector-type> # <vector-type> starts with Dv
3965 case 'v': {
Pavel Labathba825192018-10-16 14:29:14 +00003966 Result = getDerived().parseVectorType();
Richard Smithc20d1442018-08-20 20:14:49 +00003967 break;
3968 }
3969 // ::= Dp <type> # pack expansion (C++0x)
3970 case 'p': {
3971 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00003972 Node *Child = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00003973 if (!Child)
3974 return nullptr;
3975 Result = make<ParameterPackExpansion>(Child);
3976 break;
3977 }
3978 // Exception specifier on a function type.
3979 case 'o':
3980 case 'O':
3981 case 'w':
3982 // Transaction safe function type.
3983 case 'x':
Pavel Labathba825192018-10-16 14:29:14 +00003984 Result = getDerived().parseFunctionType();
Richard Smithc20d1442018-08-20 20:14:49 +00003985 break;
3986 }
3987 break;
3988 // ::= <function-type>
3989 case 'F': {
Pavel Labathba825192018-10-16 14:29:14 +00003990 Result = getDerived().parseFunctionType();
Richard Smithc20d1442018-08-20 20:14:49 +00003991 break;
3992 }
3993 // ::= <array-type>
3994 case 'A': {
Pavel Labathba825192018-10-16 14:29:14 +00003995 Result = getDerived().parseArrayType();
Richard Smithc20d1442018-08-20 20:14:49 +00003996 break;
3997 }
3998 // ::= <pointer-to-member-type>
3999 case 'M': {
Pavel Labathba825192018-10-16 14:29:14 +00004000 Result = getDerived().parsePointerToMemberType();
Richard Smithc20d1442018-08-20 20:14:49 +00004001 break;
4002 }
4003 // ::= <template-param>
4004 case 'T': {
4005 // This could be an elaborate type specifier on a <class-enum-type>.
4006 if (look(1) == 's' || look(1) == 'u' || look(1) == 'e') {
Pavel Labathba825192018-10-16 14:29:14 +00004007 Result = getDerived().parseClassEnumType();
Richard Smithc20d1442018-08-20 20:14:49 +00004008 break;
4009 }
4010
Pavel Labathba825192018-10-16 14:29:14 +00004011 Result = getDerived().parseTemplateParam();
Richard Smithc20d1442018-08-20 20:14:49 +00004012 if (Result == nullptr)
4013 return nullptr;
4014
4015 // Result could be either of:
4016 // <type> ::= <template-param>
4017 // <type> ::= <template-template-param> <template-args>
4018 //
4019 // <template-template-param> ::= <template-param>
4020 // ::= <substitution>
4021 //
4022 // If this is followed by some <template-args>, and we're permitted to
4023 // parse them, take the second production.
4024
4025 if (TryToParseTemplateArgs && look() == 'I') {
Pavel Labathba825192018-10-16 14:29:14 +00004026 Node *TA = getDerived().parseTemplateArgs();
Richard Smithc20d1442018-08-20 20:14:49 +00004027 if (TA == nullptr)
4028 return nullptr;
4029 Result = make<NameWithTemplateArgs>(Result, TA);
4030 }
4031 break;
4032 }
4033 // ::= P <type> # pointer
4034 case 'P': {
4035 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00004036 Node *Ptr = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00004037 if (Ptr == nullptr)
4038 return nullptr;
4039 Result = make<PointerType>(Ptr);
4040 break;
4041 }
4042 // ::= R <type> # l-value reference
4043 case 'R': {
4044 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00004045 Node *Ref = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00004046 if (Ref == nullptr)
4047 return nullptr;
4048 Result = make<ReferenceType>(Ref, ReferenceKind::LValue);
4049 break;
4050 }
4051 // ::= O <type> # r-value reference (C++11)
4052 case 'O': {
4053 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00004054 Node *Ref = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00004055 if (Ref == nullptr)
4056 return nullptr;
4057 Result = make<ReferenceType>(Ref, ReferenceKind::RValue);
4058 break;
4059 }
4060 // ::= C <type> # complex pair (C99)
4061 case 'C': {
4062 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00004063 Node *P = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00004064 if (P == nullptr)
4065 return nullptr;
4066 Result = make<PostfixQualifiedType>(P, " complex");
4067 break;
4068 }
4069 // ::= G <type> # imaginary (C99)
4070 case 'G': {
4071 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00004072 Node *P = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00004073 if (P == nullptr)
4074 return P;
4075 Result = make<PostfixQualifiedType>(P, " imaginary");
4076 break;
4077 }
4078 // ::= <substitution> # See Compression below
4079 case 'S': {
4080 if (look(1) && look(1) != 't') {
Pavel Labathba825192018-10-16 14:29:14 +00004081 Node *Sub = getDerived().parseSubstitution();
Richard Smithc20d1442018-08-20 20:14:49 +00004082 if (Sub == nullptr)
4083 return nullptr;
4084
4085 // Sub could be either of:
4086 // <type> ::= <substitution>
4087 // <type> ::= <template-template-param> <template-args>
4088 //
4089 // <template-template-param> ::= <template-param>
4090 // ::= <substitution>
4091 //
4092 // If this is followed by some <template-args>, and we're permitted to
4093 // parse them, take the second production.
4094
4095 if (TryToParseTemplateArgs && look() == 'I') {
Pavel Labathba825192018-10-16 14:29:14 +00004096 Node *TA = getDerived().parseTemplateArgs();
Richard Smithc20d1442018-08-20 20:14:49 +00004097 if (TA == nullptr)
4098 return nullptr;
4099 Result = make<NameWithTemplateArgs>(Sub, TA);
4100 break;
4101 }
4102
4103 // If all we parsed was a substitution, don't re-insert into the
4104 // substitution table.
4105 return Sub;
4106 }
Erik Pilkingtonf70e4d82019-01-17 20:37:51 +00004107 DEMANGLE_FALLTHROUGH;
Richard Smithc20d1442018-08-20 20:14:49 +00004108 }
4109 // ::= <class-enum-type>
4110 default: {
Pavel Labathba825192018-10-16 14:29:14 +00004111 Result = getDerived().parseClassEnumType();
Richard Smithc20d1442018-08-20 20:14:49 +00004112 break;
4113 }
4114 }
4115
4116 // If we parsed a type, insert it into the substitution table. Note that all
4117 // <builtin-type>s and <substitution>s have already bailed out, because they
4118 // don't get substitutions.
4119 if (Result != nullptr)
4120 Subs.push_back(Result);
4121 return Result;
4122}
4123
Pavel Labathba825192018-10-16 14:29:14 +00004124template <typename Derived, typename Alloc>
4125Node *AbstractManglingParser<Derived, Alloc>::parsePrefixExpr(StringView Kind) {
4126 Node *E = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004127 if (E == nullptr)
4128 return nullptr;
4129 return make<PrefixExpr>(Kind, E);
4130}
4131
Pavel Labathba825192018-10-16 14:29:14 +00004132template <typename Derived, typename Alloc>
4133Node *AbstractManglingParser<Derived, Alloc>::parseBinaryExpr(StringView Kind) {
4134 Node *LHS = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004135 if (LHS == nullptr)
4136 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00004137 Node *RHS = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004138 if (RHS == nullptr)
4139 return nullptr;
4140 return make<BinaryExpr>(LHS, Kind, RHS);
4141}
4142
Pavel Labathba825192018-10-16 14:29:14 +00004143template <typename Derived, typename Alloc>
4144Node *
4145AbstractManglingParser<Derived, Alloc>::parseIntegerLiteral(StringView Lit) {
Richard Smithc20d1442018-08-20 20:14:49 +00004146 StringView Tmp = parseNumber(true);
4147 if (!Tmp.empty() && consumeIf('E'))
4148 return make<IntegerLiteral>(Lit, Tmp);
4149 return nullptr;
4150}
4151
4152// <CV-Qualifiers> ::= [r] [V] [K]
Pavel Labathba825192018-10-16 14:29:14 +00004153template <typename Alloc, typename Derived>
4154Qualifiers AbstractManglingParser<Alloc, Derived>::parseCVQualifiers() {
Richard Smithc20d1442018-08-20 20:14:49 +00004155 Qualifiers CVR = QualNone;
4156 if (consumeIf('r'))
4157 CVR |= QualRestrict;
4158 if (consumeIf('V'))
4159 CVR |= QualVolatile;
4160 if (consumeIf('K'))
4161 CVR |= QualConst;
4162 return CVR;
4163}
4164
4165// <function-param> ::= fp <top-level CV-Qualifiers> _ # L == 0, first parameter
4166// ::= fp <top-level CV-Qualifiers> <parameter-2 non-negative number> _ # L == 0, second and later parameters
4167// ::= fL <L-1 non-negative number> p <top-level CV-Qualifiers> _ # L > 0, first parameter
4168// ::= 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 -04004169// ::= fpT # 'this' expression (not part of standard?)
Pavel Labathba825192018-10-16 14:29:14 +00004170template <typename Derived, typename Alloc>
4171Node *AbstractManglingParser<Derived, Alloc>::parseFunctionParam() {
Erik Pilkington91c24af2020-05-13 22:19:45 -04004172 if (consumeIf("fpT"))
4173 return make<NameType>("this");
Richard Smithc20d1442018-08-20 20:14:49 +00004174 if (consumeIf("fp")) {
4175 parseCVQualifiers();
4176 StringView Num = parseNumber();
4177 if (!consumeIf('_'))
4178 return nullptr;
4179 return make<FunctionParam>(Num);
4180 }
4181 if (consumeIf("fL")) {
4182 if (parseNumber().empty())
4183 return nullptr;
4184 if (!consumeIf('p'))
4185 return nullptr;
4186 parseCVQualifiers();
4187 StringView Num = parseNumber();
4188 if (!consumeIf('_'))
4189 return nullptr;
4190 return make<FunctionParam>(Num);
4191 }
4192 return nullptr;
4193}
4194
4195// [gs] nw <expression>* _ <type> E # new (expr-list) type
4196// [gs] nw <expression>* _ <type> <initializer> # new (expr-list) type (init)
4197// [gs] na <expression>* _ <type> E # new[] (expr-list) type
4198// [gs] na <expression>* _ <type> <initializer> # new[] (expr-list) type (init)
4199// <initializer> ::= pi <expression>* E # parenthesized initialization
Pavel Labathba825192018-10-16 14:29:14 +00004200template <typename Derived, typename Alloc>
4201Node *AbstractManglingParser<Derived, Alloc>::parseNewExpr() {
Richard Smithc20d1442018-08-20 20:14:49 +00004202 bool Global = consumeIf("gs");
4203 bool IsArray = look(1) == 'a';
4204 if (!consumeIf("nw") && !consumeIf("na"))
4205 return nullptr;
4206 size_t Exprs = Names.size();
4207 while (!consumeIf('_')) {
Pavel Labathba825192018-10-16 14:29:14 +00004208 Node *Ex = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004209 if (Ex == nullptr)
4210 return nullptr;
4211 Names.push_back(Ex);
4212 }
4213 NodeArray ExprList = popTrailingNodeArray(Exprs);
Pavel Labathba825192018-10-16 14:29:14 +00004214 Node *Ty = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00004215 if (Ty == nullptr)
4216 return Ty;
4217 if (consumeIf("pi")) {
4218 size_t InitsBegin = Names.size();
4219 while (!consumeIf('E')) {
Pavel Labathba825192018-10-16 14:29:14 +00004220 Node *Init = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004221 if (Init == nullptr)
4222 return Init;
4223 Names.push_back(Init);
4224 }
4225 NodeArray Inits = popTrailingNodeArray(InitsBegin);
4226 return make<NewExpr>(ExprList, Ty, Inits, Global, IsArray);
4227 } else if (!consumeIf('E'))
4228 return nullptr;
4229 return make<NewExpr>(ExprList, Ty, NodeArray(), Global, IsArray);
4230}
4231
4232// cv <type> <expression> # conversion with one argument
4233// cv <type> _ <expression>* E # conversion with a different number of arguments
Pavel Labathba825192018-10-16 14:29:14 +00004234template <typename Derived, typename Alloc>
4235Node *AbstractManglingParser<Derived, Alloc>::parseConversionExpr() {
Richard Smithc20d1442018-08-20 20:14:49 +00004236 if (!consumeIf("cv"))
4237 return nullptr;
4238 Node *Ty;
4239 {
4240 SwapAndRestore<bool> SaveTemp(TryToParseTemplateArgs, false);
Pavel Labathba825192018-10-16 14:29:14 +00004241 Ty = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00004242 }
4243
4244 if (Ty == nullptr)
4245 return nullptr;
4246
4247 if (consumeIf('_')) {
4248 size_t ExprsBegin = Names.size();
4249 while (!consumeIf('E')) {
Pavel Labathba825192018-10-16 14:29:14 +00004250 Node *E = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004251 if (E == nullptr)
4252 return E;
4253 Names.push_back(E);
4254 }
4255 NodeArray Exprs = popTrailingNodeArray(ExprsBegin);
4256 return make<ConversionExpr>(Ty, Exprs);
4257 }
4258
Pavel Labathba825192018-10-16 14:29:14 +00004259 Node *E[1] = {getDerived().parseExpr()};
Richard Smithc20d1442018-08-20 20:14:49 +00004260 if (E[0] == nullptr)
4261 return nullptr;
4262 return make<ConversionExpr>(Ty, makeNodeArray(E, E + 1));
4263}
4264
4265// <expr-primary> ::= L <type> <value number> E # integer literal
4266// ::= L <type> <value float> E # floating literal
4267// ::= L <string type> E # string literal
4268// ::= L <nullptr type> E # nullptr literal (i.e., "LDnE")
Richard Smithdf1c14c2019-09-06 23:53:21 +00004269// ::= L <lambda type> E # lambda expression
Richard Smithc20d1442018-08-20 20:14:49 +00004270// FIXME: ::= L <type> <real-part float> _ <imag-part float> E # complex floating point literal (C 2000)
4271// ::= L <mangled-name> E # external name
Pavel Labathba825192018-10-16 14:29:14 +00004272template <typename Derived, typename Alloc>
4273Node *AbstractManglingParser<Derived, Alloc>::parseExprPrimary() {
Richard Smithc20d1442018-08-20 20:14:49 +00004274 if (!consumeIf('L'))
4275 return nullptr;
4276 switch (look()) {
4277 case 'w':
4278 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00004279 return getDerived().parseIntegerLiteral("wchar_t");
Richard Smithc20d1442018-08-20 20:14:49 +00004280 case 'b':
4281 if (consumeIf("b0E"))
4282 return make<BoolExpr>(0);
4283 if (consumeIf("b1E"))
4284 return make<BoolExpr>(1);
4285 return nullptr;
4286 case 'c':
4287 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00004288 return getDerived().parseIntegerLiteral("char");
Richard Smithc20d1442018-08-20 20:14:49 +00004289 case 'a':
4290 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00004291 return getDerived().parseIntegerLiteral("signed char");
Richard Smithc20d1442018-08-20 20:14:49 +00004292 case 'h':
4293 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00004294 return getDerived().parseIntegerLiteral("unsigned char");
Richard Smithc20d1442018-08-20 20:14:49 +00004295 case 's':
4296 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00004297 return getDerived().parseIntegerLiteral("short");
Richard Smithc20d1442018-08-20 20:14:49 +00004298 case 't':
4299 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00004300 return getDerived().parseIntegerLiteral("unsigned short");
Richard Smithc20d1442018-08-20 20:14:49 +00004301 case 'i':
4302 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00004303 return getDerived().parseIntegerLiteral("");
Richard Smithc20d1442018-08-20 20:14:49 +00004304 case 'j':
4305 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00004306 return getDerived().parseIntegerLiteral("u");
Richard Smithc20d1442018-08-20 20:14:49 +00004307 case 'l':
4308 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00004309 return getDerived().parseIntegerLiteral("l");
Richard Smithc20d1442018-08-20 20:14:49 +00004310 case 'm':
4311 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00004312 return getDerived().parseIntegerLiteral("ul");
Richard Smithc20d1442018-08-20 20:14:49 +00004313 case 'x':
4314 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00004315 return getDerived().parseIntegerLiteral("ll");
Richard Smithc20d1442018-08-20 20:14:49 +00004316 case 'y':
4317 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00004318 return getDerived().parseIntegerLiteral("ull");
Richard Smithc20d1442018-08-20 20:14:49 +00004319 case 'n':
4320 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00004321 return getDerived().parseIntegerLiteral("__int128");
Richard Smithc20d1442018-08-20 20:14:49 +00004322 case 'o':
4323 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00004324 return getDerived().parseIntegerLiteral("unsigned __int128");
Richard Smithc20d1442018-08-20 20:14:49 +00004325 case 'f':
4326 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00004327 return getDerived().template parseFloatingLiteral<float>();
Richard Smithc20d1442018-08-20 20:14:49 +00004328 case 'd':
4329 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00004330 return getDerived().template parseFloatingLiteral<double>();
Richard Smithc20d1442018-08-20 20:14:49 +00004331 case 'e':
4332 ++First;
Xing Xue3dc5e082020-04-15 09:59:06 -04004333#if defined(__powerpc__) || defined(__s390__)
4334 // Handle cases where long doubles encoded with e have the same size
4335 // and representation as doubles.
4336 return getDerived().template parseFloatingLiteral<double>();
4337#else
Pavel Labathba825192018-10-16 14:29:14 +00004338 return getDerived().template parseFloatingLiteral<long double>();
Xing Xue3dc5e082020-04-15 09:59:06 -04004339#endif
Richard Smithc20d1442018-08-20 20:14:49 +00004340 case '_':
4341 if (consumeIf("_Z")) {
Pavel Labathba825192018-10-16 14:29:14 +00004342 Node *R = getDerived().parseEncoding();
Richard Smithc20d1442018-08-20 20:14:49 +00004343 if (R != nullptr && consumeIf('E'))
4344 return R;
4345 }
4346 return nullptr;
Richard Smithdf1c14c2019-09-06 23:53:21 +00004347 case 'A': {
4348 Node *T = getDerived().parseType();
4349 if (T == nullptr)
4350 return nullptr;
4351 // FIXME: We need to include the string contents in the mangling.
4352 if (consumeIf('E'))
4353 return make<StringLiteral>(T);
4354 return nullptr;
4355 }
4356 case 'D':
4357 if (consumeIf("DnE"))
4358 return make<NameType>("nullptr");
4359 return nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00004360 case 'T':
4361 // Invalid mangled name per
4362 // http://sourcerytools.com/pipermail/cxx-abi-dev/2011-August/002422.html
4363 return nullptr;
Richard Smithfb917462019-09-09 22:26:04 +00004364 case 'U': {
4365 // FIXME: Should we support LUb... for block literals?
4366 if (look(1) != 'l')
4367 return nullptr;
4368 Node *T = parseUnnamedTypeName(nullptr);
4369 if (!T || !consumeIf('E'))
4370 return nullptr;
4371 return make<LambdaExpr>(T);
4372 }
Richard Smithc20d1442018-08-20 20:14:49 +00004373 default: {
4374 // might be named type
Pavel Labathba825192018-10-16 14:29:14 +00004375 Node *T = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00004376 if (T == nullptr)
4377 return nullptr;
Erik Pilkington0a170f12020-05-13 14:13:37 -04004378 StringView N = parseNumber(/*AllowNegative=*/true);
Richard Smithfb917462019-09-09 22:26:04 +00004379 if (N.empty())
4380 return nullptr;
4381 if (!consumeIf('E'))
4382 return nullptr;
Erik Pilkington0a170f12020-05-13 14:13:37 -04004383 return make<EnumLiteral>(T, N);
Richard Smithc20d1442018-08-20 20:14:49 +00004384 }
4385 }
4386}
4387
4388// <braced-expression> ::= <expression>
4389// ::= di <field source-name> <braced-expression> # .name = expr
4390// ::= dx <index expression> <braced-expression> # [expr] = expr
4391// ::= dX <range begin expression> <range end expression> <braced-expression>
Pavel Labathba825192018-10-16 14:29:14 +00004392template <typename Derived, typename Alloc>
4393Node *AbstractManglingParser<Derived, Alloc>::parseBracedExpr() {
Richard Smithc20d1442018-08-20 20:14:49 +00004394 if (look() == 'd') {
4395 switch (look(1)) {
4396 case 'i': {
4397 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004398 Node *Field = getDerived().parseSourceName(/*NameState=*/nullptr);
Richard Smithc20d1442018-08-20 20:14:49 +00004399 if (Field == nullptr)
4400 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00004401 Node *Init = getDerived().parseBracedExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004402 if (Init == nullptr)
4403 return nullptr;
4404 return make<BracedExpr>(Field, Init, /*isArray=*/false);
4405 }
4406 case 'x': {
4407 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004408 Node *Index = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004409 if (Index == nullptr)
4410 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00004411 Node *Init = getDerived().parseBracedExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004412 if (Init == nullptr)
4413 return nullptr;
4414 return make<BracedExpr>(Index, Init, /*isArray=*/true);
4415 }
4416 case 'X': {
4417 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004418 Node *RangeBegin = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004419 if (RangeBegin == nullptr)
4420 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00004421 Node *RangeEnd = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004422 if (RangeEnd == nullptr)
4423 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00004424 Node *Init = getDerived().parseBracedExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004425 if (Init == nullptr)
4426 return nullptr;
4427 return make<BracedRangeExpr>(RangeBegin, RangeEnd, Init);
4428 }
4429 }
4430 }
Pavel Labathba825192018-10-16 14:29:14 +00004431 return getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004432}
4433
4434// (not yet in the spec)
4435// <fold-expr> ::= fL <binary-operator-name> <expression> <expression>
4436// ::= fR <binary-operator-name> <expression> <expression>
4437// ::= fl <binary-operator-name> <expression>
4438// ::= fr <binary-operator-name> <expression>
Pavel Labathba825192018-10-16 14:29:14 +00004439template <typename Derived, typename Alloc>
4440Node *AbstractManglingParser<Derived, Alloc>::parseFoldExpr() {
Richard Smithc20d1442018-08-20 20:14:49 +00004441 if (!consumeIf('f'))
4442 return nullptr;
4443
4444 char FoldKind = look();
4445 bool IsLeftFold, HasInitializer;
4446 HasInitializer = FoldKind == 'L' || FoldKind == 'R';
4447 if (FoldKind == 'l' || FoldKind == 'L')
4448 IsLeftFold = true;
4449 else if (FoldKind == 'r' || FoldKind == 'R')
4450 IsLeftFold = false;
4451 else
4452 return nullptr;
4453 ++First;
4454
4455 // FIXME: This map is duplicated in parseOperatorName and parseExpr.
4456 StringView OperatorName;
4457 if (consumeIf("aa")) OperatorName = "&&";
4458 else if (consumeIf("an")) OperatorName = "&";
4459 else if (consumeIf("aN")) OperatorName = "&=";
4460 else if (consumeIf("aS")) OperatorName = "=";
4461 else if (consumeIf("cm")) OperatorName = ",";
4462 else if (consumeIf("ds")) OperatorName = ".*";
4463 else if (consumeIf("dv")) OperatorName = "/";
4464 else if (consumeIf("dV")) OperatorName = "/=";
4465 else if (consumeIf("eo")) OperatorName = "^";
4466 else if (consumeIf("eO")) OperatorName = "^=";
4467 else if (consumeIf("eq")) OperatorName = "==";
4468 else if (consumeIf("ge")) OperatorName = ">=";
4469 else if (consumeIf("gt")) OperatorName = ">";
4470 else if (consumeIf("le")) OperatorName = "<=";
4471 else if (consumeIf("ls")) OperatorName = "<<";
4472 else if (consumeIf("lS")) OperatorName = "<<=";
4473 else if (consumeIf("lt")) OperatorName = "<";
4474 else if (consumeIf("mi")) OperatorName = "-";
4475 else if (consumeIf("mI")) OperatorName = "-=";
4476 else if (consumeIf("ml")) OperatorName = "*";
4477 else if (consumeIf("mL")) OperatorName = "*=";
4478 else if (consumeIf("ne")) OperatorName = "!=";
4479 else if (consumeIf("oo")) OperatorName = "||";
4480 else if (consumeIf("or")) OperatorName = "|";
4481 else if (consumeIf("oR")) OperatorName = "|=";
4482 else if (consumeIf("pl")) OperatorName = "+";
4483 else if (consumeIf("pL")) OperatorName = "+=";
4484 else if (consumeIf("rm")) OperatorName = "%";
4485 else if (consumeIf("rM")) OperatorName = "%=";
4486 else if (consumeIf("rs")) OperatorName = ">>";
4487 else if (consumeIf("rS")) OperatorName = ">>=";
4488 else return nullptr;
4489
Pavel Labathba825192018-10-16 14:29:14 +00004490 Node *Pack = getDerived().parseExpr(), *Init = nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00004491 if (Pack == nullptr)
4492 return nullptr;
4493 if (HasInitializer) {
Pavel Labathba825192018-10-16 14:29:14 +00004494 Init = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004495 if (Init == nullptr)
4496 return nullptr;
4497 }
4498
4499 if (IsLeftFold && Init)
4500 std::swap(Pack, Init);
4501
4502 return make<FoldExpr>(IsLeftFold, OperatorName, Pack, Init);
4503}
4504
Richard Smith1865d2f2020-10-22 19:29:36 -07004505// <expression> ::= mc <parameter type> <expr> [<offset number>] E
4506//
4507// Not yet in the spec: https://github.com/itanium-cxx-abi/cxx-abi/issues/47
4508template <typename Derived, typename Alloc>
4509Node *AbstractManglingParser<Derived, Alloc>::parsePointerToMemberConversionExpr() {
4510 Node *Ty = getDerived().parseType();
4511 if (!Ty)
4512 return nullptr;
4513 Node *Expr = getDerived().parseExpr();
4514 if (!Expr)
4515 return nullptr;
4516 StringView Offset = getDerived().parseNumber(true);
4517 if (!consumeIf('E'))
4518 return nullptr;
4519 return make<PointerToMemberConversionExpr>(Ty, Expr, Offset);
4520}
4521
4522// <expression> ::= so <referent type> <expr> [<offset number>] <union-selector>* [p] E
4523// <union-selector> ::= _ [<number>]
4524//
4525// Not yet in the spec: https://github.com/itanium-cxx-abi/cxx-abi/issues/47
4526template <typename Derived, typename Alloc>
4527Node *AbstractManglingParser<Derived, Alloc>::parseSubobjectExpr() {
4528 Node *Ty = getDerived().parseType();
4529 if (!Ty)
4530 return nullptr;
4531 Node *Expr = getDerived().parseExpr();
4532 if (!Expr)
4533 return nullptr;
4534 StringView Offset = getDerived().parseNumber(true);
4535 size_t SelectorsBegin = Names.size();
4536 while (consumeIf('_')) {
4537 Node *Selector = make<NameType>(parseNumber());
4538 if (!Selector)
4539 return nullptr;
4540 Names.push_back(Selector);
4541 }
4542 bool OnePastTheEnd = consumeIf('p');
4543 if (!consumeIf('E'))
4544 return nullptr;
4545 return make<SubobjectExpr>(
4546 Ty, Expr, Offset, popTrailingNodeArray(SelectorsBegin), OnePastTheEnd);
4547}
4548
Richard Smithc20d1442018-08-20 20:14:49 +00004549// <expression> ::= <unary operator-name> <expression>
4550// ::= <binary operator-name> <expression> <expression>
4551// ::= <ternary operator-name> <expression> <expression> <expression>
4552// ::= cl <expression>+ E # call
4553// ::= cv <type> <expression> # conversion with one argument
4554// ::= cv <type> _ <expression>* E # conversion with a different number of arguments
4555// ::= [gs] nw <expression>* _ <type> E # new (expr-list) type
4556// ::= [gs] nw <expression>* _ <type> <initializer> # new (expr-list) type (init)
4557// ::= [gs] na <expression>* _ <type> E # new[] (expr-list) type
4558// ::= [gs] na <expression>* _ <type> <initializer> # new[] (expr-list) type (init)
4559// ::= [gs] dl <expression> # delete expression
4560// ::= [gs] da <expression> # delete[] expression
4561// ::= pp_ <expression> # prefix ++
4562// ::= mm_ <expression> # prefix --
4563// ::= ti <type> # typeid (type)
4564// ::= te <expression> # typeid (expression)
4565// ::= dc <type> <expression> # dynamic_cast<type> (expression)
4566// ::= sc <type> <expression> # static_cast<type> (expression)
4567// ::= cc <type> <expression> # const_cast<type> (expression)
4568// ::= rc <type> <expression> # reinterpret_cast<type> (expression)
4569// ::= st <type> # sizeof (a type)
4570// ::= sz <expression> # sizeof (an expression)
4571// ::= at <type> # alignof (a type)
4572// ::= az <expression> # alignof (an expression)
4573// ::= nx <expression> # noexcept (expression)
4574// ::= <template-param>
4575// ::= <function-param>
4576// ::= dt <expression> <unresolved-name> # expr.name
4577// ::= pt <expression> <unresolved-name> # expr->name
4578// ::= ds <expression> <expression> # expr.*expr
4579// ::= sZ <template-param> # size of a parameter pack
4580// ::= sZ <function-param> # size of a function parameter pack
4581// ::= sP <template-arg>* E # sizeof...(T), size of a captured template parameter pack from an alias template
4582// ::= sp <expression> # pack expansion
4583// ::= tw <expression> # throw expression
4584// ::= tr # throw with no operand (rethrow)
4585// ::= <unresolved-name> # f(p), N::f(p), ::f(p),
4586// # freestanding dependent name (e.g., T::x),
4587// # objectless nonstatic member reference
4588// ::= fL <binary-operator-name> <expression> <expression>
4589// ::= fR <binary-operator-name> <expression> <expression>
4590// ::= fl <binary-operator-name> <expression>
4591// ::= fr <binary-operator-name> <expression>
4592// ::= <expr-primary>
Pavel Labathba825192018-10-16 14:29:14 +00004593template <typename Derived, typename Alloc>
4594Node *AbstractManglingParser<Derived, Alloc>::parseExpr() {
Richard Smithc20d1442018-08-20 20:14:49 +00004595 bool Global = consumeIf("gs");
4596 if (numLeft() < 2)
4597 return nullptr;
4598
4599 switch (*First) {
4600 case 'L':
Pavel Labathba825192018-10-16 14:29:14 +00004601 return getDerived().parseExprPrimary();
Richard Smithc20d1442018-08-20 20:14:49 +00004602 case 'T':
Pavel Labathba825192018-10-16 14:29:14 +00004603 return getDerived().parseTemplateParam();
Richard Smithc20d1442018-08-20 20:14:49 +00004604 case 'f': {
4605 // Disambiguate a fold expression from a <function-param>.
4606 if (look(1) == 'p' || (look(1) == 'L' && std::isdigit(look(2))))
Pavel Labathba825192018-10-16 14:29:14 +00004607 return getDerived().parseFunctionParam();
4608 return getDerived().parseFoldExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004609 }
4610 case 'a':
4611 switch (First[1]) {
4612 case 'a':
4613 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004614 return getDerived().parseBinaryExpr("&&");
Richard Smithc20d1442018-08-20 20:14:49 +00004615 case 'd':
4616 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004617 return getDerived().parsePrefixExpr("&");
Richard Smithc20d1442018-08-20 20:14:49 +00004618 case 'n':
4619 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004620 return getDerived().parseBinaryExpr("&");
Richard Smithc20d1442018-08-20 20:14:49 +00004621 case 'N':
4622 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004623 return getDerived().parseBinaryExpr("&=");
Richard Smithc20d1442018-08-20 20:14:49 +00004624 case 'S':
4625 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004626 return getDerived().parseBinaryExpr("=");
Richard Smithc20d1442018-08-20 20:14:49 +00004627 case 't': {
4628 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004629 Node *Ty = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00004630 if (Ty == nullptr)
4631 return nullptr;
4632 return make<EnclosingExpr>("alignof (", Ty, ")");
4633 }
4634 case 'z': {
4635 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004636 Node *Ty = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004637 if (Ty == nullptr)
4638 return nullptr;
4639 return make<EnclosingExpr>("alignof (", Ty, ")");
4640 }
4641 }
4642 return nullptr;
4643 case 'c':
4644 switch (First[1]) {
4645 // cc <type> <expression> # const_cast<type>(expression)
4646 case 'c': {
4647 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004648 Node *Ty = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00004649 if (Ty == nullptr)
4650 return Ty;
Pavel Labathba825192018-10-16 14:29:14 +00004651 Node *Ex = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004652 if (Ex == nullptr)
4653 return Ex;
4654 return make<CastExpr>("const_cast", Ty, Ex);
4655 }
4656 // cl <expression>+ E # call
4657 case 'l': {
4658 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004659 Node *Callee = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004660 if (Callee == nullptr)
4661 return Callee;
4662 size_t ExprsBegin = Names.size();
4663 while (!consumeIf('E')) {
Pavel Labathba825192018-10-16 14:29:14 +00004664 Node *E = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004665 if (E == nullptr)
4666 return E;
4667 Names.push_back(E);
4668 }
4669 return make<CallExpr>(Callee, popTrailingNodeArray(ExprsBegin));
4670 }
4671 case 'm':
4672 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004673 return getDerived().parseBinaryExpr(",");
Richard Smithc20d1442018-08-20 20:14:49 +00004674 case 'o':
4675 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004676 return getDerived().parsePrefixExpr("~");
Richard Smithc20d1442018-08-20 20:14:49 +00004677 case 'v':
Pavel Labathba825192018-10-16 14:29:14 +00004678 return getDerived().parseConversionExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004679 }
4680 return nullptr;
4681 case 'd':
4682 switch (First[1]) {
4683 case 'a': {
4684 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004685 Node *Ex = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004686 if (Ex == nullptr)
4687 return Ex;
4688 return make<DeleteExpr>(Ex, Global, /*is_array=*/true);
4689 }
4690 case 'c': {
4691 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004692 Node *T = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00004693 if (T == nullptr)
4694 return T;
Pavel Labathba825192018-10-16 14:29:14 +00004695 Node *Ex = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004696 if (Ex == nullptr)
4697 return Ex;
4698 return make<CastExpr>("dynamic_cast", T, Ex);
4699 }
4700 case 'e':
4701 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004702 return getDerived().parsePrefixExpr("*");
Richard Smithc20d1442018-08-20 20:14:49 +00004703 case 'l': {
4704 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004705 Node *E = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004706 if (E == nullptr)
4707 return E;
4708 return make<DeleteExpr>(E, Global, /*is_array=*/false);
4709 }
4710 case 'n':
Pavel Labathba825192018-10-16 14:29:14 +00004711 return getDerived().parseUnresolvedName();
Richard Smithc20d1442018-08-20 20:14:49 +00004712 case 's': {
4713 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004714 Node *LHS = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004715 if (LHS == nullptr)
4716 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00004717 Node *RHS = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004718 if (RHS == nullptr)
4719 return nullptr;
4720 return make<MemberExpr>(LHS, ".*", RHS);
4721 }
4722 case 't': {
4723 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004724 Node *LHS = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004725 if (LHS == nullptr)
4726 return LHS;
Pavel Labathba825192018-10-16 14:29:14 +00004727 Node *RHS = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004728 if (RHS == nullptr)
4729 return nullptr;
4730 return make<MemberExpr>(LHS, ".", RHS);
4731 }
4732 case 'v':
4733 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004734 return getDerived().parseBinaryExpr("/");
Richard Smithc20d1442018-08-20 20:14:49 +00004735 case 'V':
4736 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004737 return getDerived().parseBinaryExpr("/=");
Richard Smithc20d1442018-08-20 20:14:49 +00004738 }
4739 return nullptr;
4740 case 'e':
4741 switch (First[1]) {
4742 case 'o':
4743 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004744 return getDerived().parseBinaryExpr("^");
Richard Smithc20d1442018-08-20 20:14:49 +00004745 case 'O':
4746 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004747 return getDerived().parseBinaryExpr("^=");
Richard Smithc20d1442018-08-20 20:14:49 +00004748 case 'q':
4749 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004750 return getDerived().parseBinaryExpr("==");
Richard Smithc20d1442018-08-20 20:14:49 +00004751 }
4752 return nullptr;
4753 case 'g':
4754 switch (First[1]) {
4755 case 'e':
4756 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004757 return getDerived().parseBinaryExpr(">=");
Richard Smithc20d1442018-08-20 20:14:49 +00004758 case 't':
4759 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004760 return getDerived().parseBinaryExpr(">");
Richard Smithc20d1442018-08-20 20:14:49 +00004761 }
4762 return nullptr;
4763 case 'i':
4764 switch (First[1]) {
4765 case 'x': {
4766 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004767 Node *Base = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004768 if (Base == nullptr)
4769 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00004770 Node *Index = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004771 if (Index == nullptr)
4772 return Index;
4773 return make<ArraySubscriptExpr>(Base, Index);
4774 }
4775 case 'l': {
4776 First += 2;
4777 size_t InitsBegin = Names.size();
4778 while (!consumeIf('E')) {
Pavel Labathba825192018-10-16 14:29:14 +00004779 Node *E = getDerived().parseBracedExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004780 if (E == nullptr)
4781 return nullptr;
4782 Names.push_back(E);
4783 }
4784 return make<InitListExpr>(nullptr, popTrailingNodeArray(InitsBegin));
4785 }
4786 }
4787 return nullptr;
4788 case 'l':
4789 switch (First[1]) {
4790 case 'e':
4791 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004792 return getDerived().parseBinaryExpr("<=");
Richard Smithc20d1442018-08-20 20:14:49 +00004793 case 's':
4794 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004795 return getDerived().parseBinaryExpr("<<");
Richard Smithc20d1442018-08-20 20:14:49 +00004796 case 'S':
4797 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004798 return getDerived().parseBinaryExpr("<<=");
Richard Smithc20d1442018-08-20 20:14:49 +00004799 case 't':
4800 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004801 return getDerived().parseBinaryExpr("<");
Richard Smithc20d1442018-08-20 20:14:49 +00004802 }
4803 return nullptr;
4804 case 'm':
4805 switch (First[1]) {
Richard Smith1865d2f2020-10-22 19:29:36 -07004806 case 'c':
4807 First += 2;
4808 return parsePointerToMemberConversionExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004809 case 'i':
4810 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004811 return getDerived().parseBinaryExpr("-");
Richard Smithc20d1442018-08-20 20:14:49 +00004812 case 'I':
4813 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004814 return getDerived().parseBinaryExpr("-=");
Richard Smithc20d1442018-08-20 20:14:49 +00004815 case 'l':
4816 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004817 return getDerived().parseBinaryExpr("*");
Richard Smithc20d1442018-08-20 20:14:49 +00004818 case 'L':
4819 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004820 return getDerived().parseBinaryExpr("*=");
Richard Smithc20d1442018-08-20 20:14:49 +00004821 case 'm':
4822 First += 2;
4823 if (consumeIf('_'))
Pavel Labathba825192018-10-16 14:29:14 +00004824 return getDerived().parsePrefixExpr("--");
4825 Node *Ex = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004826 if (Ex == nullptr)
4827 return nullptr;
4828 return make<PostfixExpr>(Ex, "--");
4829 }
4830 return nullptr;
4831 case 'n':
4832 switch (First[1]) {
4833 case 'a':
4834 case 'w':
Pavel Labathba825192018-10-16 14:29:14 +00004835 return getDerived().parseNewExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004836 case 'e':
4837 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004838 return getDerived().parseBinaryExpr("!=");
Richard Smithc20d1442018-08-20 20:14:49 +00004839 case 'g':
4840 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004841 return getDerived().parsePrefixExpr("-");
Richard Smithc20d1442018-08-20 20:14:49 +00004842 case 't':
4843 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004844 return getDerived().parsePrefixExpr("!");
Richard Smithc20d1442018-08-20 20:14:49 +00004845 case 'x':
4846 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004847 Node *Ex = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004848 if (Ex == nullptr)
4849 return Ex;
4850 return make<EnclosingExpr>("noexcept (", Ex, ")");
4851 }
4852 return nullptr;
4853 case 'o':
4854 switch (First[1]) {
4855 case 'n':
Pavel Labathba825192018-10-16 14:29:14 +00004856 return getDerived().parseUnresolvedName();
Richard Smithc20d1442018-08-20 20:14:49 +00004857 case 'o':
4858 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004859 return getDerived().parseBinaryExpr("||");
Richard Smithc20d1442018-08-20 20:14:49 +00004860 case 'r':
4861 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004862 return getDerived().parseBinaryExpr("|");
Richard Smithc20d1442018-08-20 20:14:49 +00004863 case 'R':
4864 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004865 return getDerived().parseBinaryExpr("|=");
Richard Smithc20d1442018-08-20 20:14:49 +00004866 }
4867 return nullptr;
4868 case 'p':
4869 switch (First[1]) {
4870 case 'm':
4871 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004872 return getDerived().parseBinaryExpr("->*");
Richard Smithc20d1442018-08-20 20:14:49 +00004873 case 'l':
4874 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004875 return getDerived().parseBinaryExpr("+");
Richard Smithc20d1442018-08-20 20:14:49 +00004876 case 'L':
4877 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004878 return getDerived().parseBinaryExpr("+=");
Richard Smithc20d1442018-08-20 20:14:49 +00004879 case 'p': {
4880 First += 2;
4881 if (consumeIf('_'))
Pavel Labathba825192018-10-16 14:29:14 +00004882 return getDerived().parsePrefixExpr("++");
4883 Node *Ex = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004884 if (Ex == nullptr)
4885 return Ex;
4886 return make<PostfixExpr>(Ex, "++");
4887 }
4888 case 's':
4889 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004890 return getDerived().parsePrefixExpr("+");
Richard Smithc20d1442018-08-20 20:14:49 +00004891 case 't': {
4892 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004893 Node *L = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004894 if (L == nullptr)
4895 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00004896 Node *R = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004897 if (R == nullptr)
4898 return nullptr;
4899 return make<MemberExpr>(L, "->", R);
4900 }
4901 }
4902 return nullptr;
4903 case 'q':
4904 if (First[1] == 'u') {
4905 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004906 Node *Cond = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004907 if (Cond == nullptr)
4908 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00004909 Node *LHS = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004910 if (LHS == nullptr)
4911 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00004912 Node *RHS = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004913 if (RHS == nullptr)
4914 return nullptr;
4915 return make<ConditionalExpr>(Cond, LHS, RHS);
4916 }
4917 return nullptr;
4918 case 'r':
4919 switch (First[1]) {
4920 case 'c': {
4921 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004922 Node *T = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00004923 if (T == nullptr)
4924 return T;
Pavel Labathba825192018-10-16 14:29:14 +00004925 Node *Ex = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004926 if (Ex == nullptr)
4927 return Ex;
4928 return make<CastExpr>("reinterpret_cast", T, Ex);
4929 }
4930 case 'm':
4931 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004932 return getDerived().parseBinaryExpr("%");
Richard Smithc20d1442018-08-20 20:14:49 +00004933 case 'M':
4934 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004935 return getDerived().parseBinaryExpr("%=");
Richard Smithc20d1442018-08-20 20:14:49 +00004936 case 's':
4937 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004938 return getDerived().parseBinaryExpr(">>");
Richard Smithc20d1442018-08-20 20:14:49 +00004939 case 'S':
4940 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004941 return getDerived().parseBinaryExpr(">>=");
Richard Smithc20d1442018-08-20 20:14:49 +00004942 }
4943 return nullptr;
4944 case 's':
4945 switch (First[1]) {
4946 case 'c': {
4947 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004948 Node *T = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00004949 if (T == nullptr)
4950 return T;
Pavel Labathba825192018-10-16 14:29:14 +00004951 Node *Ex = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004952 if (Ex == nullptr)
4953 return Ex;
4954 return make<CastExpr>("static_cast", T, Ex);
4955 }
Richard Smith1865d2f2020-10-22 19:29:36 -07004956 case 'o':
4957 First += 2;
4958 return parseSubobjectExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004959 case 'p': {
4960 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004961 Node *Child = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004962 if (Child == nullptr)
4963 return nullptr;
4964 return make<ParameterPackExpansion>(Child);
4965 }
4966 case 'r':
Pavel Labathba825192018-10-16 14:29:14 +00004967 return getDerived().parseUnresolvedName();
Richard Smithc20d1442018-08-20 20:14:49 +00004968 case 't': {
4969 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004970 Node *Ty = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00004971 if (Ty == nullptr)
4972 return Ty;
4973 return make<EnclosingExpr>("sizeof (", Ty, ")");
4974 }
4975 case 'z': {
4976 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004977 Node *Ex = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004978 if (Ex == nullptr)
4979 return Ex;
4980 return make<EnclosingExpr>("sizeof (", Ex, ")");
4981 }
4982 case 'Z':
4983 First += 2;
4984 if (look() == 'T') {
Pavel Labathba825192018-10-16 14:29:14 +00004985 Node *R = getDerived().parseTemplateParam();
Richard Smithc20d1442018-08-20 20:14:49 +00004986 if (R == nullptr)
4987 return nullptr;
4988 return make<SizeofParamPackExpr>(R);
4989 } else if (look() == 'f') {
Pavel Labathba825192018-10-16 14:29:14 +00004990 Node *FP = getDerived().parseFunctionParam();
Richard Smithc20d1442018-08-20 20:14:49 +00004991 if (FP == nullptr)
4992 return nullptr;
4993 return make<EnclosingExpr>("sizeof... (", FP, ")");
4994 }
4995 return nullptr;
4996 case 'P': {
4997 First += 2;
4998 size_t ArgsBegin = Names.size();
4999 while (!consumeIf('E')) {
Pavel Labathba825192018-10-16 14:29:14 +00005000 Node *Arg = getDerived().parseTemplateArg();
Richard Smithc20d1442018-08-20 20:14:49 +00005001 if (Arg == nullptr)
5002 return nullptr;
5003 Names.push_back(Arg);
5004 }
Richard Smithb485b352018-08-24 23:30:26 +00005005 auto *Pack = make<NodeArrayNode>(popTrailingNodeArray(ArgsBegin));
5006 if (!Pack)
5007 return nullptr;
5008 return make<EnclosingExpr>("sizeof... (", Pack, ")");
Richard Smithc20d1442018-08-20 20:14:49 +00005009 }
5010 }
5011 return nullptr;
5012 case 't':
5013 switch (First[1]) {
5014 case 'e': {
5015 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00005016 Node *Ex = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00005017 if (Ex == nullptr)
5018 return Ex;
5019 return make<EnclosingExpr>("typeid (", Ex, ")");
5020 }
5021 case 'i': {
5022 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00005023 Node *Ty = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00005024 if (Ty == nullptr)
5025 return Ty;
5026 return make<EnclosingExpr>("typeid (", Ty, ")");
5027 }
5028 case 'l': {
5029 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00005030 Node *Ty = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00005031 if (Ty == nullptr)
5032 return nullptr;
5033 size_t InitsBegin = Names.size();
5034 while (!consumeIf('E')) {
Pavel Labathba825192018-10-16 14:29:14 +00005035 Node *E = getDerived().parseBracedExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00005036 if (E == nullptr)
5037 return nullptr;
5038 Names.push_back(E);
5039 }
5040 return make<InitListExpr>(Ty, popTrailingNodeArray(InitsBegin));
5041 }
5042 case 'r':
5043 First += 2;
5044 return make<NameType>("throw");
5045 case 'w': {
5046 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00005047 Node *Ex = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00005048 if (Ex == nullptr)
5049 return nullptr;
5050 return make<ThrowExpr>(Ex);
5051 }
5052 }
5053 return nullptr;
James Y Knight4a60efc2020-12-07 10:26:49 -05005054 case 'u': {
5055 ++First;
5056 Node *Name = getDerived().parseSourceName(/*NameState=*/nullptr);
5057 if (!Name)
5058 return nullptr;
5059 // Special case legacy __uuidof mangling. The 't' and 'z' appear where the
5060 // standard encoding expects a <template-arg>, and would be otherwise be
5061 // interpreted as <type> node 'short' or 'ellipsis'. However, neither
5062 // __uuidof(short) nor __uuidof(...) can actually appear, so there is no
5063 // actual conflict here.
5064 if (Name->getBaseName() == "__uuidof") {
5065 if (numLeft() < 2)
5066 return nullptr;
5067 if (*First == 't') {
5068 ++First;
5069 Node *Ty = getDerived().parseType();
5070 if (!Ty)
5071 return nullptr;
5072 return make<CallExpr>(Name, makeNodeArray(&Ty, &Ty + 1));
5073 }
5074 if (*First == 'z') {
5075 ++First;
5076 Node *Ex = getDerived().parseExpr();
5077 if (!Ex)
5078 return nullptr;
5079 return make<CallExpr>(Name, makeNodeArray(&Ex, &Ex + 1));
5080 }
5081 }
5082 size_t ExprsBegin = Names.size();
5083 while (!consumeIf('E')) {
5084 Node *E = getDerived().parseTemplateArg();
5085 if (E == nullptr)
5086 return E;
5087 Names.push_back(E);
5088 }
5089 return make<CallExpr>(Name, popTrailingNodeArray(ExprsBegin));
5090 }
Richard Smithc20d1442018-08-20 20:14:49 +00005091 case '1':
5092 case '2':
5093 case '3':
5094 case '4':
5095 case '5':
5096 case '6':
5097 case '7':
5098 case '8':
5099 case '9':
Pavel Labathba825192018-10-16 14:29:14 +00005100 return getDerived().parseUnresolvedName();
Richard Smithc20d1442018-08-20 20:14:49 +00005101 }
5102 return nullptr;
5103}
5104
5105// <call-offset> ::= h <nv-offset> _
5106// ::= v <v-offset> _
5107//
5108// <nv-offset> ::= <offset number>
5109// # non-virtual base override
5110//
5111// <v-offset> ::= <offset number> _ <virtual offset number>
5112// # virtual base override, with vcall offset
Pavel Labathba825192018-10-16 14:29:14 +00005113template <typename Alloc, typename Derived>
5114bool AbstractManglingParser<Alloc, Derived>::parseCallOffset() {
Richard Smithc20d1442018-08-20 20:14:49 +00005115 // Just scan through the call offset, we never add this information into the
5116 // output.
5117 if (consumeIf('h'))
5118 return parseNumber(true).empty() || !consumeIf('_');
5119 if (consumeIf('v'))
5120 return parseNumber(true).empty() || !consumeIf('_') ||
5121 parseNumber(true).empty() || !consumeIf('_');
5122 return true;
5123}
5124
5125// <special-name> ::= TV <type> # virtual table
5126// ::= TT <type> # VTT structure (construction vtable index)
5127// ::= TI <type> # typeinfo structure
5128// ::= TS <type> # typeinfo name (null-terminated byte string)
5129// ::= Tc <call-offset> <call-offset> <base encoding>
5130// # base is the nominal target function of thunk
5131// # first call-offset is 'this' adjustment
5132// # second call-offset is result adjustment
5133// ::= T <call-offset> <base encoding>
5134// # base is the nominal target function of thunk
5135// ::= GV <object name> # Guard variable for one-time initialization
5136// # No <type>
5137// ::= TW <object name> # Thread-local wrapper
5138// ::= TH <object name> # Thread-local initialization
5139// ::= GR <object name> _ # First temporary
5140// ::= GR <object name> <seq-id> _ # Subsequent temporaries
5141// extension ::= TC <first type> <number> _ <second type> # construction vtable for second-in-first
5142// extension ::= GR <object name> # reference temporary for object
Pavel Labathba825192018-10-16 14:29:14 +00005143template <typename Derived, typename Alloc>
5144Node *AbstractManglingParser<Derived, Alloc>::parseSpecialName() {
Richard Smithc20d1442018-08-20 20:14:49 +00005145 switch (look()) {
5146 case 'T':
5147 switch (look(1)) {
Richard Smith1865d2f2020-10-22 19:29:36 -07005148 // TA <template-arg> # template parameter object
5149 //
5150 // Not yet in the spec: https://github.com/itanium-cxx-abi/cxx-abi/issues/63
5151 case 'A': {
5152 First += 2;
5153 Node *Arg = getDerived().parseTemplateArg();
5154 if (Arg == nullptr)
5155 return nullptr;
5156 return make<SpecialName>("template parameter object for ", Arg);
5157 }
Richard Smithc20d1442018-08-20 20:14:49 +00005158 // TV <type> # virtual table
5159 case 'V': {
5160 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00005161 Node *Ty = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00005162 if (Ty == nullptr)
5163 return nullptr;
5164 return make<SpecialName>("vtable for ", Ty);
5165 }
5166 // TT <type> # VTT structure (construction vtable index)
5167 case 'T': {
5168 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00005169 Node *Ty = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00005170 if (Ty == nullptr)
5171 return nullptr;
5172 return make<SpecialName>("VTT for ", Ty);
5173 }
5174 // TI <type> # typeinfo structure
5175 case 'I': {
5176 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00005177 Node *Ty = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00005178 if (Ty == nullptr)
5179 return nullptr;
5180 return make<SpecialName>("typeinfo for ", Ty);
5181 }
5182 // TS <type> # typeinfo name (null-terminated byte string)
5183 case 'S': {
5184 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00005185 Node *Ty = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00005186 if (Ty == nullptr)
5187 return nullptr;
5188 return make<SpecialName>("typeinfo name for ", Ty);
5189 }
5190 // Tc <call-offset> <call-offset> <base encoding>
5191 case 'c': {
5192 First += 2;
5193 if (parseCallOffset() || parseCallOffset())
5194 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00005195 Node *Encoding = getDerived().parseEncoding();
Richard Smithc20d1442018-08-20 20:14:49 +00005196 if (Encoding == nullptr)
5197 return nullptr;
5198 return make<SpecialName>("covariant return thunk to ", Encoding);
5199 }
5200 // extension ::= TC <first type> <number> _ <second type>
5201 // # construction vtable for second-in-first
5202 case 'C': {
5203 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00005204 Node *FirstType = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00005205 if (FirstType == nullptr)
5206 return nullptr;
5207 if (parseNumber(true).empty() || !consumeIf('_'))
5208 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00005209 Node *SecondType = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00005210 if (SecondType == nullptr)
5211 return nullptr;
5212 return make<CtorVtableSpecialName>(SecondType, FirstType);
5213 }
5214 // TW <object name> # Thread-local wrapper
5215 case 'W': {
5216 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00005217 Node *Name = getDerived().parseName();
Richard Smithc20d1442018-08-20 20:14:49 +00005218 if (Name == nullptr)
5219 return nullptr;
5220 return make<SpecialName>("thread-local wrapper routine for ", Name);
5221 }
5222 // TH <object name> # Thread-local initialization
5223 case 'H': {
5224 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00005225 Node *Name = getDerived().parseName();
Richard Smithc20d1442018-08-20 20:14:49 +00005226 if (Name == nullptr)
5227 return nullptr;
5228 return make<SpecialName>("thread-local initialization routine for ", Name);
5229 }
5230 // T <call-offset> <base encoding>
5231 default: {
5232 ++First;
5233 bool IsVirt = look() == 'v';
5234 if (parseCallOffset())
5235 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00005236 Node *BaseEncoding = getDerived().parseEncoding();
Richard Smithc20d1442018-08-20 20:14:49 +00005237 if (BaseEncoding == nullptr)
5238 return nullptr;
5239 if (IsVirt)
5240 return make<SpecialName>("virtual thunk to ", BaseEncoding);
5241 else
5242 return make<SpecialName>("non-virtual thunk to ", BaseEncoding);
5243 }
5244 }
5245 case 'G':
5246 switch (look(1)) {
5247 // GV <object name> # Guard variable for one-time initialization
5248 case 'V': {
5249 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00005250 Node *Name = getDerived().parseName();
Richard Smithc20d1442018-08-20 20:14:49 +00005251 if (Name == nullptr)
5252 return nullptr;
5253 return make<SpecialName>("guard variable for ", Name);
5254 }
5255 // GR <object name> # reference temporary for object
5256 // GR <object name> _ # First temporary
5257 // GR <object name> <seq-id> _ # Subsequent temporaries
5258 case 'R': {
5259 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00005260 Node *Name = getDerived().parseName();
Richard Smithc20d1442018-08-20 20:14:49 +00005261 if (Name == nullptr)
5262 return nullptr;
5263 size_t Count;
5264 bool ParsedSeqId = !parseSeqId(&Count);
5265 if (!consumeIf('_') && ParsedSeqId)
5266 return nullptr;
5267 return make<SpecialName>("reference temporary for ", Name);
5268 }
5269 }
5270 }
5271 return nullptr;
5272}
5273
5274// <encoding> ::= <function name> <bare-function-type>
5275// ::= <data name>
5276// ::= <special-name>
Pavel Labathba825192018-10-16 14:29:14 +00005277template <typename Derived, typename Alloc>
5278Node *AbstractManglingParser<Derived, Alloc>::parseEncoding() {
Richard Smithfac39712020-07-09 21:08:39 -07005279 // The template parameters of an encoding are unrelated to those of the
5280 // enclosing context.
5281 class SaveTemplateParams {
5282 AbstractManglingParser *Parser;
5283 decltype(TemplateParams) OldParams;
Justin Lebar2c536232021-06-09 16:57:22 -07005284 decltype(OuterTemplateParams) OldOuterParams;
Richard Smithfac39712020-07-09 21:08:39 -07005285
5286 public:
Louis Dionnec1fe8672020-10-30 17:33:02 -04005287 SaveTemplateParams(AbstractManglingParser *TheParser) : Parser(TheParser) {
Richard Smithfac39712020-07-09 21:08:39 -07005288 OldParams = std::move(Parser->TemplateParams);
Justin Lebar2c536232021-06-09 16:57:22 -07005289 OldOuterParams = std::move(Parser->OuterTemplateParams);
Richard Smithfac39712020-07-09 21:08:39 -07005290 Parser->TemplateParams.clear();
Justin Lebar2c536232021-06-09 16:57:22 -07005291 Parser->OuterTemplateParams.clear();
Richard Smithfac39712020-07-09 21:08:39 -07005292 }
5293 ~SaveTemplateParams() {
5294 Parser->TemplateParams = std::move(OldParams);
Justin Lebar2c536232021-06-09 16:57:22 -07005295 Parser->OuterTemplateParams = std::move(OldOuterParams);
Richard Smithfac39712020-07-09 21:08:39 -07005296 }
5297 } SaveTemplateParams(this);
Richard Smithfd434322020-07-09 20:36:04 -07005298
Richard Smithc20d1442018-08-20 20:14:49 +00005299 if (look() == 'G' || look() == 'T')
Pavel Labathba825192018-10-16 14:29:14 +00005300 return getDerived().parseSpecialName();
Richard Smithc20d1442018-08-20 20:14:49 +00005301
5302 auto IsEndOfEncoding = [&] {
5303 // The set of chars that can potentially follow an <encoding> (none of which
5304 // can start a <type>). Enumerating these allows us to avoid speculative
5305 // parsing.
5306 return numLeft() == 0 || look() == 'E' || look() == '.' || look() == '_';
5307 };
5308
5309 NameState NameInfo(this);
Pavel Labathba825192018-10-16 14:29:14 +00005310 Node *Name = getDerived().parseName(&NameInfo);
Richard Smithc20d1442018-08-20 20:14:49 +00005311 if (Name == nullptr)
5312 return nullptr;
5313
5314 if (resolveForwardTemplateRefs(NameInfo))
5315 return nullptr;
5316
5317 if (IsEndOfEncoding())
5318 return Name;
5319
5320 Node *Attrs = nullptr;
5321 if (consumeIf("Ua9enable_ifI")) {
5322 size_t BeforeArgs = Names.size();
5323 while (!consumeIf('E')) {
Pavel Labathba825192018-10-16 14:29:14 +00005324 Node *Arg = getDerived().parseTemplateArg();
Richard Smithc20d1442018-08-20 20:14:49 +00005325 if (Arg == nullptr)
5326 return nullptr;
5327 Names.push_back(Arg);
5328 }
5329 Attrs = make<EnableIfAttr>(popTrailingNodeArray(BeforeArgs));
Richard Smithb485b352018-08-24 23:30:26 +00005330 if (!Attrs)
5331 return nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00005332 }
5333
5334 Node *ReturnType = nullptr;
5335 if (!NameInfo.CtorDtorConversion && NameInfo.EndsWithTemplateArgs) {
Pavel Labathba825192018-10-16 14:29:14 +00005336 ReturnType = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00005337 if (ReturnType == nullptr)
5338 return nullptr;
5339 }
5340
5341 if (consumeIf('v'))
5342 return make<FunctionEncoding>(ReturnType, Name, NodeArray(),
5343 Attrs, NameInfo.CVQualifiers,
5344 NameInfo.ReferenceQualifier);
5345
5346 size_t ParamsBegin = Names.size();
5347 do {
Pavel Labathba825192018-10-16 14:29:14 +00005348 Node *Ty = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00005349 if (Ty == nullptr)
5350 return nullptr;
5351 Names.push_back(Ty);
5352 } while (!IsEndOfEncoding());
5353
5354 return make<FunctionEncoding>(ReturnType, Name,
5355 popTrailingNodeArray(ParamsBegin),
5356 Attrs, NameInfo.CVQualifiers,
5357 NameInfo.ReferenceQualifier);
5358}
5359
5360template <class Float>
5361struct FloatData;
5362
5363template <>
5364struct FloatData<float>
5365{
5366 static const size_t mangled_size = 8;
5367 static const size_t max_demangled_size = 24;
5368 static constexpr const char* spec = "%af";
5369};
5370
5371template <>
5372struct FloatData<double>
5373{
5374 static const size_t mangled_size = 16;
5375 static const size_t max_demangled_size = 32;
5376 static constexpr const char* spec = "%a";
5377};
5378
5379template <>
5380struct FloatData<long double>
5381{
5382#if defined(__mips__) && defined(__mips_n64) || defined(__aarch64__) || \
5383 defined(__wasm__)
5384 static const size_t mangled_size = 32;
5385#elif defined(__arm__) || defined(__mips__) || defined(__hexagon__)
5386 static const size_t mangled_size = 16;
5387#else
5388 static const size_t mangled_size = 20; // May need to be adjusted to 16 or 24 on other platforms
5389#endif
Elliott Hughes5a360ea2020-04-10 17:42:00 -07005390 // `-0x1.ffffffffffffffffffffffffffffp+16383` + 'L' + '\0' == 42 bytes.
5391 // 28 'f's * 4 bits == 112 bits, which is the number of mantissa bits.
5392 // Negatives are one character longer than positives.
5393 // `0x1.` and `p` are constant, and exponents `+16383` and `-16382` are the
5394 // same length. 1 sign bit, 112 mantissa bits, and 15 exponent bits == 128.
5395 static const size_t max_demangled_size = 42;
Richard Smithc20d1442018-08-20 20:14:49 +00005396 static constexpr const char *spec = "%LaL";
5397};
5398
Pavel Labathba825192018-10-16 14:29:14 +00005399template <typename Alloc, typename Derived>
5400template <class Float>
5401Node *AbstractManglingParser<Alloc, Derived>::parseFloatingLiteral() {
Richard Smithc20d1442018-08-20 20:14:49 +00005402 const size_t N = FloatData<Float>::mangled_size;
5403 if (numLeft() <= N)
5404 return nullptr;
5405 StringView Data(First, First + N);
5406 for (char C : Data)
5407 if (!std::isxdigit(C))
5408 return nullptr;
5409 First += N;
5410 if (!consumeIf('E'))
5411 return nullptr;
5412 return make<FloatLiteralImpl<Float>>(Data);
5413}
5414
5415// <seq-id> ::= <0-9A-Z>+
Pavel Labathba825192018-10-16 14:29:14 +00005416template <typename Alloc, typename Derived>
5417bool AbstractManglingParser<Alloc, Derived>::parseSeqId(size_t *Out) {
Richard Smithc20d1442018-08-20 20:14:49 +00005418 if (!(look() >= '0' && look() <= '9') &&
5419 !(look() >= 'A' && look() <= 'Z'))
5420 return true;
5421
5422 size_t Id = 0;
5423 while (true) {
5424 if (look() >= '0' && look() <= '9') {
5425 Id *= 36;
5426 Id += static_cast<size_t>(look() - '0');
5427 } else if (look() >= 'A' && look() <= 'Z') {
5428 Id *= 36;
5429 Id += static_cast<size_t>(look() - 'A') + 10;
5430 } else {
5431 *Out = Id;
5432 return false;
5433 }
5434 ++First;
5435 }
5436}
5437
5438// <substitution> ::= S <seq-id> _
5439// ::= S_
5440// <substitution> ::= Sa # ::std::allocator
5441// <substitution> ::= Sb # ::std::basic_string
5442// <substitution> ::= Ss # ::std::basic_string < char,
5443// ::std::char_traits<char>,
5444// ::std::allocator<char> >
5445// <substitution> ::= Si # ::std::basic_istream<char, std::char_traits<char> >
5446// <substitution> ::= So # ::std::basic_ostream<char, std::char_traits<char> >
5447// <substitution> ::= Sd # ::std::basic_iostream<char, std::char_traits<char> >
Pavel Labathba825192018-10-16 14:29:14 +00005448template <typename Derived, typename Alloc>
5449Node *AbstractManglingParser<Derived, Alloc>::parseSubstitution() {
Richard Smithc20d1442018-08-20 20:14:49 +00005450 if (!consumeIf('S'))
5451 return nullptr;
5452
5453 if (std::islower(look())) {
5454 Node *SpecialSub;
5455 switch (look()) {
5456 case 'a':
5457 ++First;
5458 SpecialSub = make<SpecialSubstitution>(SpecialSubKind::allocator);
5459 break;
5460 case 'b':
5461 ++First;
5462 SpecialSub = make<SpecialSubstitution>(SpecialSubKind::basic_string);
5463 break;
5464 case 's':
5465 ++First;
5466 SpecialSub = make<SpecialSubstitution>(SpecialSubKind::string);
5467 break;
5468 case 'i':
5469 ++First;
5470 SpecialSub = make<SpecialSubstitution>(SpecialSubKind::istream);
5471 break;
5472 case 'o':
5473 ++First;
5474 SpecialSub = make<SpecialSubstitution>(SpecialSubKind::ostream);
5475 break;
5476 case 'd':
5477 ++First;
5478 SpecialSub = make<SpecialSubstitution>(SpecialSubKind::iostream);
5479 break;
5480 default:
5481 return nullptr;
5482 }
Richard Smithb485b352018-08-24 23:30:26 +00005483 if (!SpecialSub)
5484 return nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00005485 // Itanium C++ ABI 5.1.2: If a name that would use a built-in <substitution>
5486 // has ABI tags, the tags are appended to the substitution; the result is a
5487 // substitutable component.
Pavel Labathba825192018-10-16 14:29:14 +00005488 Node *WithTags = getDerived().parseAbiTags(SpecialSub);
Richard Smithc20d1442018-08-20 20:14:49 +00005489 if (WithTags != SpecialSub) {
5490 Subs.push_back(WithTags);
5491 SpecialSub = WithTags;
5492 }
5493 return SpecialSub;
5494 }
5495
5496 // ::= S_
5497 if (consumeIf('_')) {
5498 if (Subs.empty())
5499 return nullptr;
5500 return Subs[0];
5501 }
5502
5503 // ::= S <seq-id> _
5504 size_t Index = 0;
5505 if (parseSeqId(&Index))
5506 return nullptr;
5507 ++Index;
5508 if (!consumeIf('_') || Index >= Subs.size())
5509 return nullptr;
5510 return Subs[Index];
5511}
5512
5513// <template-param> ::= T_ # first template parameter
5514// ::= T <parameter-2 non-negative number> _
Richard Smithdf1c14c2019-09-06 23:53:21 +00005515// ::= TL <level-1> __
5516// ::= TL <level-1> _ <parameter-2 non-negative number> _
Pavel Labathba825192018-10-16 14:29:14 +00005517template <typename Derived, typename Alloc>
5518Node *AbstractManglingParser<Derived, Alloc>::parseTemplateParam() {
Richard Smithc20d1442018-08-20 20:14:49 +00005519 if (!consumeIf('T'))
5520 return nullptr;
5521
Richard Smithdf1c14c2019-09-06 23:53:21 +00005522 size_t Level = 0;
5523 if (consumeIf('L')) {
5524 if (parsePositiveInteger(&Level))
5525 return nullptr;
5526 ++Level;
5527 if (!consumeIf('_'))
5528 return nullptr;
5529 }
5530
Richard Smithc20d1442018-08-20 20:14:49 +00005531 size_t Index = 0;
5532 if (!consumeIf('_')) {
5533 if (parsePositiveInteger(&Index))
5534 return nullptr;
5535 ++Index;
5536 if (!consumeIf('_'))
5537 return nullptr;
5538 }
5539
Richard Smithc20d1442018-08-20 20:14:49 +00005540 // If we're in a context where this <template-param> refers to a
5541 // <template-arg> further ahead in the mangled name (currently just conversion
5542 // operator types), then we should only look it up in the right context.
Richard Smithdf1c14c2019-09-06 23:53:21 +00005543 // This can only happen at the outermost level.
5544 if (PermitForwardTemplateReferences && Level == 0) {
Richard Smithb485b352018-08-24 23:30:26 +00005545 Node *ForwardRef = make<ForwardTemplateReference>(Index);
5546 if (!ForwardRef)
5547 return nullptr;
5548 assert(ForwardRef->getKind() == Node::KForwardTemplateReference);
5549 ForwardTemplateRefs.push_back(
5550 static_cast<ForwardTemplateReference *>(ForwardRef));
5551 return ForwardRef;
Richard Smithc20d1442018-08-20 20:14:49 +00005552 }
5553
Richard Smithdf1c14c2019-09-06 23:53:21 +00005554 if (Level >= TemplateParams.size() || !TemplateParams[Level] ||
5555 Index >= TemplateParams[Level]->size()) {
5556 // Itanium ABI 5.1.8: In a generic lambda, uses of auto in the parameter
5557 // list are mangled as the corresponding artificial template type parameter.
5558 if (ParsingLambdaParamsAtLevel == Level && Level <= TemplateParams.size()) {
5559 // This will be popped by the ScopedTemplateParamList in
5560 // parseUnnamedTypeName.
5561 if (Level == TemplateParams.size())
5562 TemplateParams.push_back(nullptr);
5563 return make<NameType>("auto");
5564 }
5565
Richard Smithc20d1442018-08-20 20:14:49 +00005566 return nullptr;
Richard Smithdf1c14c2019-09-06 23:53:21 +00005567 }
5568
5569 return (*TemplateParams[Level])[Index];
5570}
5571
5572// <template-param-decl> ::= Ty # type parameter
5573// ::= Tn <type> # non-type parameter
5574// ::= Tt <template-param-decl>* E # template parameter
5575// ::= Tp <template-param-decl> # parameter pack
5576template <typename Derived, typename Alloc>
5577Node *AbstractManglingParser<Derived, Alloc>::parseTemplateParamDecl() {
5578 auto InventTemplateParamName = [&](TemplateParamKind Kind) {
5579 unsigned Index = NumSyntheticTemplateParameters[(int)Kind]++;
5580 Node *N = make<SyntheticTemplateParamName>(Kind, Index);
5581 if (N) TemplateParams.back()->push_back(N);
5582 return N;
5583 };
5584
5585 if (consumeIf("Ty")) {
5586 Node *Name = InventTemplateParamName(TemplateParamKind::Type);
5587 if (!Name)
5588 return nullptr;
5589 return make<TypeTemplateParamDecl>(Name);
5590 }
5591
5592 if (consumeIf("Tn")) {
5593 Node *Name = InventTemplateParamName(TemplateParamKind::NonType);
5594 if (!Name)
5595 return nullptr;
5596 Node *Type = parseType();
5597 if (!Type)
5598 return nullptr;
5599 return make<NonTypeTemplateParamDecl>(Name, Type);
5600 }
5601
5602 if (consumeIf("Tt")) {
5603 Node *Name = InventTemplateParamName(TemplateParamKind::Template);
5604 if (!Name)
5605 return nullptr;
5606 size_t ParamsBegin = Names.size();
5607 ScopedTemplateParamList TemplateTemplateParamParams(this);
5608 while (!consumeIf("E")) {
5609 Node *P = parseTemplateParamDecl();
5610 if (!P)
5611 return nullptr;
5612 Names.push_back(P);
5613 }
5614 NodeArray Params = popTrailingNodeArray(ParamsBegin);
5615 return make<TemplateTemplateParamDecl>(Name, Params);
5616 }
5617
5618 if (consumeIf("Tp")) {
5619 Node *P = parseTemplateParamDecl();
5620 if (!P)
5621 return nullptr;
5622 return make<TemplateParamPackDecl>(P);
5623 }
5624
5625 return nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00005626}
5627
5628// <template-arg> ::= <type> # type or template
5629// ::= X <expression> E # expression
5630// ::= <expr-primary> # simple expressions
5631// ::= J <template-arg>* E # argument pack
5632// ::= LZ <encoding> E # extension
Pavel Labathba825192018-10-16 14:29:14 +00005633template <typename Derived, typename Alloc>
5634Node *AbstractManglingParser<Derived, Alloc>::parseTemplateArg() {
Richard Smithc20d1442018-08-20 20:14:49 +00005635 switch (look()) {
5636 case 'X': {
5637 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00005638 Node *Arg = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00005639 if (Arg == nullptr || !consumeIf('E'))
5640 return nullptr;
5641 return Arg;
5642 }
5643 case 'J': {
5644 ++First;
5645 size_t ArgsBegin = Names.size();
5646 while (!consumeIf('E')) {
Pavel Labathba825192018-10-16 14:29:14 +00005647 Node *Arg = getDerived().parseTemplateArg();
Richard Smithc20d1442018-08-20 20:14:49 +00005648 if (Arg == nullptr)
5649 return nullptr;
5650 Names.push_back(Arg);
5651 }
5652 NodeArray Args = popTrailingNodeArray(ArgsBegin);
5653 return make<TemplateArgumentPack>(Args);
5654 }
5655 case 'L': {
5656 // ::= LZ <encoding> E # extension
5657 if (look(1) == 'Z') {
5658 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00005659 Node *Arg = getDerived().parseEncoding();
Richard Smithc20d1442018-08-20 20:14:49 +00005660 if (Arg == nullptr || !consumeIf('E'))
5661 return nullptr;
5662 return Arg;
5663 }
5664 // ::= <expr-primary> # simple expressions
Pavel Labathba825192018-10-16 14:29:14 +00005665 return getDerived().parseExprPrimary();
Richard Smithc20d1442018-08-20 20:14:49 +00005666 }
5667 default:
Pavel Labathba825192018-10-16 14:29:14 +00005668 return getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00005669 }
5670}
5671
5672// <template-args> ::= I <template-arg>* E
5673// extension, the abi says <template-arg>+
Pavel Labathba825192018-10-16 14:29:14 +00005674template <typename Derived, typename Alloc>
5675Node *
5676AbstractManglingParser<Derived, Alloc>::parseTemplateArgs(bool TagTemplates) {
Richard Smithc20d1442018-08-20 20:14:49 +00005677 if (!consumeIf('I'))
5678 return nullptr;
5679
5680 // <template-params> refer to the innermost <template-args>. Clear out any
5681 // outer args that we may have inserted into TemplateParams.
Richard Smithdf1c14c2019-09-06 23:53:21 +00005682 if (TagTemplates) {
Richard Smithc20d1442018-08-20 20:14:49 +00005683 TemplateParams.clear();
Richard Smithdf1c14c2019-09-06 23:53:21 +00005684 TemplateParams.push_back(&OuterTemplateParams);
5685 OuterTemplateParams.clear();
5686 }
Richard Smithc20d1442018-08-20 20:14:49 +00005687
5688 size_t ArgsBegin = Names.size();
5689 while (!consumeIf('E')) {
5690 if (TagTemplates) {
5691 auto OldParams = std::move(TemplateParams);
Pavel Labathba825192018-10-16 14:29:14 +00005692 Node *Arg = getDerived().parseTemplateArg();
Richard Smithc20d1442018-08-20 20:14:49 +00005693 TemplateParams = std::move(OldParams);
5694 if (Arg == nullptr)
5695 return nullptr;
5696 Names.push_back(Arg);
5697 Node *TableEntry = Arg;
5698 if (Arg->getKind() == Node::KTemplateArgumentPack) {
5699 TableEntry = make<ParameterPack>(
5700 static_cast<TemplateArgumentPack*>(TableEntry)->getElements());
Richard Smithb485b352018-08-24 23:30:26 +00005701 if (!TableEntry)
5702 return nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00005703 }
Richard Smithdf1c14c2019-09-06 23:53:21 +00005704 TemplateParams.back()->push_back(TableEntry);
Richard Smithc20d1442018-08-20 20:14:49 +00005705 } else {
Pavel Labathba825192018-10-16 14:29:14 +00005706 Node *Arg = getDerived().parseTemplateArg();
Richard Smithc20d1442018-08-20 20:14:49 +00005707 if (Arg == nullptr)
5708 return nullptr;
5709 Names.push_back(Arg);
5710 }
5711 }
5712 return make<TemplateArgs>(popTrailingNodeArray(ArgsBegin));
5713}
5714
5715// <mangled-name> ::= _Z <encoding>
5716// ::= <type>
5717// extension ::= ___Z <encoding> _block_invoke
5718// extension ::= ___Z <encoding> _block_invoke<decimal-digit>+
5719// extension ::= ___Z <encoding> _block_invoke_<decimal-digit>+
Pavel Labathba825192018-10-16 14:29:14 +00005720template <typename Derived, typename Alloc>
5721Node *AbstractManglingParser<Derived, Alloc>::parse() {
Erik Pilkingtonc0df1582019-01-17 21:37:36 +00005722 if (consumeIf("_Z") || consumeIf("__Z")) {
Pavel Labathba825192018-10-16 14:29:14 +00005723 Node *Encoding = getDerived().parseEncoding();
Richard Smithc20d1442018-08-20 20:14:49 +00005724 if (Encoding == nullptr)
5725 return nullptr;
5726 if (look() == '.') {
5727 Encoding = make<DotSuffix>(Encoding, StringView(First, Last));
5728 First = Last;
5729 }
5730 if (numLeft() != 0)
5731 return nullptr;
5732 return Encoding;
5733 }
5734
Erik Pilkingtonc0df1582019-01-17 21:37:36 +00005735 if (consumeIf("___Z") || consumeIf("____Z")) {
Pavel Labathba825192018-10-16 14:29:14 +00005736 Node *Encoding = getDerived().parseEncoding();
Richard Smithc20d1442018-08-20 20:14:49 +00005737 if (Encoding == nullptr || !consumeIf("_block_invoke"))
5738 return nullptr;
5739 bool RequireNumber = consumeIf('_');
5740 if (parseNumber().empty() && RequireNumber)
5741 return nullptr;
5742 if (look() == '.')
5743 First = Last;
5744 if (numLeft() != 0)
5745 return nullptr;
5746 return make<SpecialName>("invocation function for block in ", Encoding);
5747 }
5748
Pavel Labathba825192018-10-16 14:29:14 +00005749 Node *Ty = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00005750 if (numLeft() != 0)
5751 return nullptr;
5752 return Ty;
5753}
5754
Pavel Labathba825192018-10-16 14:29:14 +00005755template <typename Alloc>
5756struct ManglingParser : AbstractManglingParser<ManglingParser<Alloc>, Alloc> {
5757 using AbstractManglingParser<ManglingParser<Alloc>,
5758 Alloc>::AbstractManglingParser;
5759};
5760
Erik Pilkingtonf70e4d82019-01-17 20:37:51 +00005761DEMANGLE_NAMESPACE_END
Richard Smithc20d1442018-08-20 20:14:49 +00005762
Erik Pilkingtonf70e4d82019-01-17 20:37:51 +00005763#endif // DEMANGLE_ITANIUMDEMANGLE_H