blob: a237cf724df0a892cda150ad9ff86ecc73520e3e [file] [log] [blame]
Richard Smithc20d1442018-08-20 20:14:49 +00001//===------------------------- ItaniumDemangle.h ----------------*- C++ -*-===//
2//
Chandler Carruth8ee27c32019-01-19 10:56:40 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Richard Smithc20d1442018-08-20 20:14:49 +00006//
7//===----------------------------------------------------------------------===//
8//
Erik Pilkingtonf70e4d82019-01-17 20:37:51 +00009// Generic itanium demangler library. This file has two byte-per-byte identical
10// copies in the source tree, one in libcxxabi, and the other in llvm.
Richard Smithc20d1442018-08-20 20:14:49 +000011//
12//===----------------------------------------------------------------------===//
13
Erik Pilkingtonf70e4d82019-01-17 20:37:51 +000014#ifndef DEMANGLE_ITANIUMDEMANGLE_H
15#define DEMANGLE_ITANIUMDEMANGLE_H
Richard Smithc20d1442018-08-20 20:14:49 +000016
17// FIXME: (possibly) incomplete list of features that clang mangles that this
18// file does not yet support:
19// - C++ modules TS
20
Erik Pilkingtonf70e4d82019-01-17 20:37:51 +000021#include "DemangleConfig.h"
Richard Smithc20d1442018-08-20 20:14:49 +000022#include "StringView.h"
23#include "Utility.h"
Richard Smithc20d1442018-08-20 20:14:49 +000024#include <cassert>
25#include <cctype>
26#include <cstdio>
27#include <cstdlib>
28#include <cstring>
29#include <numeric>
30#include <utility>
31
32#define FOR_EACH_NODE_KIND(X) \
33 X(NodeArrayNode) \
34 X(DotSuffix) \
35 X(VendorExtQualType) \
36 X(QualType) \
37 X(ConversionOperatorType) \
38 X(PostfixQualifiedType) \
39 X(ElaboratedTypeSpefType) \
40 X(NameType) \
41 X(AbiTagAttr) \
42 X(EnableIfAttr) \
43 X(ObjCProtoName) \
44 X(PointerType) \
45 X(ReferenceType) \
46 X(PointerToMemberType) \
47 X(ArrayType) \
48 X(FunctionType) \
49 X(NoexceptSpec) \
50 X(DynamicExceptionSpec) \
51 X(FunctionEncoding) \
52 X(LiteralOperator) \
53 X(SpecialName) \
54 X(CtorVtableSpecialName) \
55 X(QualifiedName) \
56 X(NestedName) \
57 X(LocalName) \
58 X(VectorType) \
59 X(PixelVectorType) \
60 X(ParameterPack) \
61 X(TemplateArgumentPack) \
62 X(ParameterPackExpansion) \
63 X(TemplateArgs) \
64 X(ForwardTemplateReference) \
65 X(NameWithTemplateArgs) \
66 X(GlobalQualifiedName) \
67 X(StdQualifiedName) \
68 X(ExpandedSpecialSubstitution) \
69 X(SpecialSubstitution) \
70 X(CtorDtorName) \
71 X(DtorName) \
72 X(UnnamedTypeName) \
73 X(ClosureTypeName) \
74 X(StructuredBindingName) \
75 X(BinaryExpr) \
76 X(ArraySubscriptExpr) \
77 X(PostfixExpr) \
78 X(ConditionalExpr) \
79 X(MemberExpr) \
80 X(EnclosingExpr) \
81 X(CastExpr) \
82 X(SizeofParamPackExpr) \
83 X(CallExpr) \
84 X(NewExpr) \
85 X(DeleteExpr) \
86 X(PrefixExpr) \
87 X(FunctionParam) \
88 X(ConversionExpr) \
89 X(InitListExpr) \
90 X(FoldExpr) \
91 X(ThrowExpr) \
92 X(BoolExpr) \
93 X(IntegerCastExpr) \
94 X(IntegerLiteral) \
95 X(FloatLiteral) \
96 X(DoubleLiteral) \
97 X(LongDoubleLiteral) \
98 X(BracedExpr) \
99 X(BracedRangeExpr)
100
Erik Pilkingtonf70e4d82019-01-17 20:37:51 +0000101DEMANGLE_NAMESPACE_BEGIN
102
Richard Smithc20d1442018-08-20 20:14:49 +0000103// Base class of all AST nodes. The AST is built by the parser, then is
104// traversed by the printLeft/Right functions to produce a demangled string.
105class Node {
106public:
107 enum Kind : unsigned char {
108#define ENUMERATOR(NodeKind) K ## NodeKind,
109 FOR_EACH_NODE_KIND(ENUMERATOR)
110#undef ENUMERATOR
111 };
112
113 /// Three-way bool to track a cached value. Unknown is possible if this node
114 /// has an unexpanded parameter pack below it that may affect this cache.
115 enum class Cache : unsigned char { Yes, No, Unknown, };
116
117private:
118 Kind K;
119
120 // FIXME: Make these protected.
121public:
122 /// Tracks if this node has a component on its right side, in which case we
123 /// need to call printRight.
124 Cache RHSComponentCache;
125
126 /// Track if this node is a (possibly qualified) array type. This can affect
127 /// how we format the output string.
128 Cache ArrayCache;
129
130 /// Track if this node is a (possibly qualified) function type. This can
131 /// affect how we format the output string.
132 Cache FunctionCache;
133
134public:
135 Node(Kind K_, Cache RHSComponentCache_ = Cache::No,
136 Cache ArrayCache_ = Cache::No, Cache FunctionCache_ = Cache::No)
137 : K(K_), RHSComponentCache(RHSComponentCache_), ArrayCache(ArrayCache_),
138 FunctionCache(FunctionCache_) {}
139
140 /// Visit the most-derived object corresponding to this object.
141 template<typename Fn> void visit(Fn F) const;
142
143 // The following function is provided by all derived classes:
144 //
145 // Call F with arguments that, when passed to the constructor of this node,
146 // would construct an equivalent node.
147 //template<typename Fn> void match(Fn F) const;
148
149 bool hasRHSComponent(OutputStream &S) const {
150 if (RHSComponentCache != Cache::Unknown)
151 return RHSComponentCache == Cache::Yes;
152 return hasRHSComponentSlow(S);
153 }
154
155 bool hasArray(OutputStream &S) const {
156 if (ArrayCache != Cache::Unknown)
157 return ArrayCache == Cache::Yes;
158 return hasArraySlow(S);
159 }
160
161 bool hasFunction(OutputStream &S) const {
162 if (FunctionCache != Cache::Unknown)
163 return FunctionCache == Cache::Yes;
164 return hasFunctionSlow(S);
165 }
166
167 Kind getKind() const { return K; }
168
169 virtual bool hasRHSComponentSlow(OutputStream &) const { return false; }
170 virtual bool hasArraySlow(OutputStream &) const { return false; }
171 virtual bool hasFunctionSlow(OutputStream &) const { return false; }
172
173 // Dig through "glue" nodes like ParameterPack and ForwardTemplateReference to
174 // get at a node that actually represents some concrete syntax.
175 virtual const Node *getSyntaxNode(OutputStream &) const {
176 return this;
177 }
178
179 void print(OutputStream &S) const {
180 printLeft(S);
181 if (RHSComponentCache != Cache::No)
182 printRight(S);
183 }
184
185 // Print the "left" side of this Node into OutputStream.
186 virtual void printLeft(OutputStream &) const = 0;
187
188 // Print the "right". This distinction is necessary to represent C++ types
189 // that appear on the RHS of their subtype, such as arrays or functions.
190 // Since most types don't have such a component, provide a default
191 // implementation.
192 virtual void printRight(OutputStream &) const {}
193
194 virtual StringView getBaseName() const { return StringView(); }
195
196 // Silence compiler warnings, this dtor will never be called.
197 virtual ~Node() = default;
198
199#ifndef NDEBUG
Erik Pilkingtonf70e4d82019-01-17 20:37:51 +0000200 DEMANGLE_DUMP_METHOD void dump() const;
Richard Smithc20d1442018-08-20 20:14:49 +0000201#endif
202};
203
204class NodeArray {
205 Node **Elements;
206 size_t NumElements;
207
208public:
209 NodeArray() : Elements(nullptr), NumElements(0) {}
210 NodeArray(Node **Elements_, size_t NumElements_)
211 : Elements(Elements_), NumElements(NumElements_) {}
212
213 bool empty() const { return NumElements == 0; }
214 size_t size() const { return NumElements; }
215
216 Node **begin() const { return Elements; }
217 Node **end() const { return Elements + NumElements; }
218
219 Node *operator[](size_t Idx) const { return Elements[Idx]; }
220
221 void printWithComma(OutputStream &S) const {
222 bool FirstElement = true;
223 for (size_t Idx = 0; Idx != NumElements; ++Idx) {
224 size_t BeforeComma = S.getCurrentPosition();
225 if (!FirstElement)
226 S += ", ";
227 size_t AfterComma = S.getCurrentPosition();
228 Elements[Idx]->print(S);
229
230 // Elements[Idx] is an empty parameter pack expansion, we should erase the
231 // comma we just printed.
232 if (AfterComma == S.getCurrentPosition()) {
233 S.setCurrentPosition(BeforeComma);
234 continue;
235 }
236
237 FirstElement = false;
238 }
239 }
240};
241
242struct NodeArrayNode : Node {
243 NodeArray Array;
244 NodeArrayNode(NodeArray Array_) : Node(KNodeArrayNode), Array(Array_) {}
245
246 template<typename Fn> void match(Fn F) const { F(Array); }
247
248 void printLeft(OutputStream &S) const override {
249 Array.printWithComma(S);
250 }
251};
252
253class DotSuffix final : public Node {
254 const Node *Prefix;
255 const StringView Suffix;
256
257public:
258 DotSuffix(const Node *Prefix_, StringView Suffix_)
259 : Node(KDotSuffix), Prefix(Prefix_), Suffix(Suffix_) {}
260
261 template<typename Fn> void match(Fn F) const { F(Prefix, Suffix); }
262
263 void printLeft(OutputStream &s) const override {
264 Prefix->print(s);
265 s += " (";
266 s += Suffix;
267 s += ")";
268 }
269};
270
271class VendorExtQualType final : public Node {
272 const Node *Ty;
273 StringView Ext;
274
275public:
276 VendorExtQualType(const Node *Ty_, StringView Ext_)
277 : Node(KVendorExtQualType), Ty(Ty_), Ext(Ext_) {}
278
279 template<typename Fn> void match(Fn F) const { F(Ty, Ext); }
280
281 void printLeft(OutputStream &S) const override {
282 Ty->print(S);
283 S += " ";
284 S += Ext;
285 }
286};
287
288enum FunctionRefQual : unsigned char {
289 FrefQualNone,
290 FrefQualLValue,
291 FrefQualRValue,
292};
293
294enum Qualifiers {
295 QualNone = 0,
296 QualConst = 0x1,
297 QualVolatile = 0x2,
298 QualRestrict = 0x4,
299};
300
301inline Qualifiers operator|=(Qualifiers &Q1, Qualifiers Q2) {
302 return Q1 = static_cast<Qualifiers>(Q1 | Q2);
303}
304
305class QualType : public Node {
306protected:
307 const Qualifiers Quals;
308 const Node *Child;
309
310 void printQuals(OutputStream &S) const {
311 if (Quals & QualConst)
312 S += " const";
313 if (Quals & QualVolatile)
314 S += " volatile";
315 if (Quals & QualRestrict)
316 S += " restrict";
317 }
318
319public:
320 QualType(const Node *Child_, Qualifiers Quals_)
321 : Node(KQualType, Child_->RHSComponentCache,
322 Child_->ArrayCache, Child_->FunctionCache),
323 Quals(Quals_), Child(Child_) {}
324
325 template<typename Fn> void match(Fn F) const { F(Child, Quals); }
326
327 bool hasRHSComponentSlow(OutputStream &S) const override {
328 return Child->hasRHSComponent(S);
329 }
330 bool hasArraySlow(OutputStream &S) const override {
331 return Child->hasArray(S);
332 }
333 bool hasFunctionSlow(OutputStream &S) const override {
334 return Child->hasFunction(S);
335 }
336
337 void printLeft(OutputStream &S) const override {
338 Child->printLeft(S);
339 printQuals(S);
340 }
341
342 void printRight(OutputStream &S) const override { Child->printRight(S); }
343};
344
345class ConversionOperatorType final : public Node {
346 const Node *Ty;
347
348public:
349 ConversionOperatorType(const Node *Ty_)
350 : Node(KConversionOperatorType), Ty(Ty_) {}
351
352 template<typename Fn> void match(Fn F) const { F(Ty); }
353
354 void printLeft(OutputStream &S) const override {
355 S += "operator ";
356 Ty->print(S);
357 }
358};
359
360class PostfixQualifiedType final : public Node {
361 const Node *Ty;
362 const StringView Postfix;
363
364public:
365 PostfixQualifiedType(Node *Ty_, StringView Postfix_)
366 : Node(KPostfixQualifiedType), Ty(Ty_), Postfix(Postfix_) {}
367
368 template<typename Fn> void match(Fn F) const { F(Ty, Postfix); }
369
370 void printLeft(OutputStream &s) const override {
371 Ty->printLeft(s);
372 s += Postfix;
373 }
374};
375
376class NameType final : public Node {
377 const StringView Name;
378
379public:
380 NameType(StringView Name_) : Node(KNameType), Name(Name_) {}
381
382 template<typename Fn> void match(Fn F) const { F(Name); }
383
384 StringView getName() const { return Name; }
385 StringView getBaseName() const override { return Name; }
386
387 void printLeft(OutputStream &s) const override { s += Name; }
388};
389
390class ElaboratedTypeSpefType : public Node {
391 StringView Kind;
392 Node *Child;
393public:
394 ElaboratedTypeSpefType(StringView Kind_, Node *Child_)
395 : Node(KElaboratedTypeSpefType), Kind(Kind_), Child(Child_) {}
396
397 template<typename Fn> void match(Fn F) const { F(Kind, Child); }
398
399 void printLeft(OutputStream &S) const override {
400 S += Kind;
401 S += ' ';
402 Child->print(S);
403 }
404};
405
406struct AbiTagAttr : Node {
407 Node *Base;
408 StringView Tag;
409
410 AbiTagAttr(Node* Base_, StringView Tag_)
411 : Node(KAbiTagAttr, Base_->RHSComponentCache,
412 Base_->ArrayCache, Base_->FunctionCache),
413 Base(Base_), Tag(Tag_) {}
414
415 template<typename Fn> void match(Fn F) const { F(Base, Tag); }
416
417 void printLeft(OutputStream &S) const override {
418 Base->printLeft(S);
419 S += "[abi:";
420 S += Tag;
421 S += "]";
422 }
423};
424
425class EnableIfAttr : public Node {
426 NodeArray Conditions;
427public:
428 EnableIfAttr(NodeArray Conditions_)
429 : Node(KEnableIfAttr), Conditions(Conditions_) {}
430
431 template<typename Fn> void match(Fn F) const { F(Conditions); }
432
433 void printLeft(OutputStream &S) const override {
434 S += " [enable_if:";
435 Conditions.printWithComma(S);
436 S += ']';
437 }
438};
439
440class ObjCProtoName : public Node {
441 const Node *Ty;
442 StringView Protocol;
443
444 friend class PointerType;
445
446public:
447 ObjCProtoName(const Node *Ty_, StringView Protocol_)
448 : Node(KObjCProtoName), Ty(Ty_), Protocol(Protocol_) {}
449
450 template<typename Fn> void match(Fn F) const { F(Ty, Protocol); }
451
452 bool isObjCObject() const {
453 return Ty->getKind() == KNameType &&
454 static_cast<const NameType *>(Ty)->getName() == "objc_object";
455 }
456
457 void printLeft(OutputStream &S) const override {
458 Ty->print(S);
459 S += "<";
460 S += Protocol;
461 S += ">";
462 }
463};
464
465class PointerType final : public Node {
466 const Node *Pointee;
467
468public:
469 PointerType(const Node *Pointee_)
470 : Node(KPointerType, Pointee_->RHSComponentCache),
471 Pointee(Pointee_) {}
472
473 template<typename Fn> void match(Fn F) const { F(Pointee); }
474
475 bool hasRHSComponentSlow(OutputStream &S) const override {
476 return Pointee->hasRHSComponent(S);
477 }
478
479 void printLeft(OutputStream &s) const override {
480 // We rewrite objc_object<SomeProtocol>* into id<SomeProtocol>.
481 if (Pointee->getKind() != KObjCProtoName ||
482 !static_cast<const ObjCProtoName *>(Pointee)->isObjCObject()) {
483 Pointee->printLeft(s);
484 if (Pointee->hasArray(s))
485 s += " ";
486 if (Pointee->hasArray(s) || Pointee->hasFunction(s))
487 s += "(";
488 s += "*";
489 } else {
490 const auto *objcProto = static_cast<const ObjCProtoName *>(Pointee);
491 s += "id<";
492 s += objcProto->Protocol;
493 s += ">";
494 }
495 }
496
497 void printRight(OutputStream &s) const override {
498 if (Pointee->getKind() != KObjCProtoName ||
499 !static_cast<const ObjCProtoName *>(Pointee)->isObjCObject()) {
500 if (Pointee->hasArray(s) || Pointee->hasFunction(s))
501 s += ")";
502 Pointee->printRight(s);
503 }
504 }
505};
506
507enum class ReferenceKind {
508 LValue,
509 RValue,
510};
511
512// Represents either a LValue or an RValue reference type.
513class ReferenceType : public Node {
514 const Node *Pointee;
515 ReferenceKind RK;
516
517 mutable bool Printing = false;
518
519 // Dig through any refs to refs, collapsing the ReferenceTypes as we go. The
520 // rule here is rvalue ref to rvalue ref collapses to a rvalue ref, and any
521 // other combination collapses to a lvalue ref.
522 std::pair<ReferenceKind, const Node *> collapse(OutputStream &S) const {
523 auto SoFar = std::make_pair(RK, Pointee);
524 for (;;) {
525 const Node *SN = SoFar.second->getSyntaxNode(S);
526 if (SN->getKind() != KReferenceType)
527 break;
528 auto *RT = static_cast<const ReferenceType *>(SN);
529 SoFar.second = RT->Pointee;
530 SoFar.first = std::min(SoFar.first, RT->RK);
531 }
532 return SoFar;
533 }
534
535public:
536 ReferenceType(const Node *Pointee_, ReferenceKind RK_)
537 : Node(KReferenceType, Pointee_->RHSComponentCache),
538 Pointee(Pointee_), RK(RK_) {}
539
540 template<typename Fn> void match(Fn F) const { F(Pointee, RK); }
541
542 bool hasRHSComponentSlow(OutputStream &S) const override {
543 return Pointee->hasRHSComponent(S);
544 }
545
546 void printLeft(OutputStream &s) const override {
547 if (Printing)
548 return;
549 SwapAndRestore<bool> SavePrinting(Printing, true);
550 std::pair<ReferenceKind, const Node *> Collapsed = collapse(s);
551 Collapsed.second->printLeft(s);
552 if (Collapsed.second->hasArray(s))
553 s += " ";
554 if (Collapsed.second->hasArray(s) || Collapsed.second->hasFunction(s))
555 s += "(";
556
557 s += (Collapsed.first == ReferenceKind::LValue ? "&" : "&&");
558 }
559 void printRight(OutputStream &s) const override {
560 if (Printing)
561 return;
562 SwapAndRestore<bool> SavePrinting(Printing, true);
563 std::pair<ReferenceKind, const Node *> Collapsed = collapse(s);
564 if (Collapsed.second->hasArray(s) || Collapsed.second->hasFunction(s))
565 s += ")";
566 Collapsed.second->printRight(s);
567 }
568};
569
570class PointerToMemberType final : public Node {
571 const Node *ClassType;
572 const Node *MemberType;
573
574public:
575 PointerToMemberType(const Node *ClassType_, const Node *MemberType_)
576 : Node(KPointerToMemberType, MemberType_->RHSComponentCache),
577 ClassType(ClassType_), MemberType(MemberType_) {}
578
579 template<typename Fn> void match(Fn F) const { F(ClassType, MemberType); }
580
581 bool hasRHSComponentSlow(OutputStream &S) const override {
582 return MemberType->hasRHSComponent(S);
583 }
584
585 void printLeft(OutputStream &s) const override {
586 MemberType->printLeft(s);
587 if (MemberType->hasArray(s) || MemberType->hasFunction(s))
588 s += "(";
589 else
590 s += " ";
591 ClassType->print(s);
592 s += "::*";
593 }
594
595 void printRight(OutputStream &s) const override {
596 if (MemberType->hasArray(s) || MemberType->hasFunction(s))
597 s += ")";
598 MemberType->printRight(s);
599 }
600};
601
602class NodeOrString {
603 const void *First;
604 const void *Second;
605
606public:
607 /* implicit */ NodeOrString(StringView Str) {
608 const char *FirstChar = Str.begin();
609 const char *SecondChar = Str.end();
610 if (SecondChar == nullptr) {
611 assert(FirstChar == SecondChar);
612 ++FirstChar, ++SecondChar;
613 }
614 First = static_cast<const void *>(FirstChar);
615 Second = static_cast<const void *>(SecondChar);
616 }
617
618 /* implicit */ NodeOrString(Node *N)
619 : First(static_cast<const void *>(N)), Second(nullptr) {}
620 NodeOrString() : First(nullptr), Second(nullptr) {}
621
622 bool isString() const { return Second && First; }
623 bool isNode() const { return First && !Second; }
624 bool isEmpty() const { return !First && !Second; }
625
626 StringView asString() const {
627 assert(isString());
628 return StringView(static_cast<const char *>(First),
629 static_cast<const char *>(Second));
630 }
631
632 const Node *asNode() const {
633 assert(isNode());
634 return static_cast<const Node *>(First);
635 }
636};
637
638class ArrayType final : public Node {
639 const Node *Base;
640 NodeOrString Dimension;
641
642public:
Pavel Labathf4e67eb2018-10-10 08:39:16 +0000643 ArrayType(const Node *Base_, NodeOrString Dimension_)
Richard Smithc20d1442018-08-20 20:14:49 +0000644 : Node(KArrayType,
645 /*RHSComponentCache=*/Cache::Yes,
646 /*ArrayCache=*/Cache::Yes),
647 Base(Base_), Dimension(Dimension_) {}
648
649 template<typename Fn> void match(Fn F) const { F(Base, Dimension); }
650
651 bool hasRHSComponentSlow(OutputStream &) const override { return true; }
652 bool hasArraySlow(OutputStream &) const override { return true; }
653
654 void printLeft(OutputStream &S) const override { Base->printLeft(S); }
655
656 void printRight(OutputStream &S) const override {
657 if (S.back() != ']')
658 S += " ";
659 S += "[";
660 if (Dimension.isString())
661 S += Dimension.asString();
662 else if (Dimension.isNode())
663 Dimension.asNode()->print(S);
664 S += "]";
665 Base->printRight(S);
666 }
667};
668
669class FunctionType final : public Node {
670 const Node *Ret;
671 NodeArray Params;
672 Qualifiers CVQuals;
673 FunctionRefQual RefQual;
674 const Node *ExceptionSpec;
675
676public:
677 FunctionType(const Node *Ret_, NodeArray Params_, Qualifiers CVQuals_,
678 FunctionRefQual RefQual_, const Node *ExceptionSpec_)
679 : Node(KFunctionType,
680 /*RHSComponentCache=*/Cache::Yes, /*ArrayCache=*/Cache::No,
681 /*FunctionCache=*/Cache::Yes),
682 Ret(Ret_), Params(Params_), CVQuals(CVQuals_), RefQual(RefQual_),
683 ExceptionSpec(ExceptionSpec_) {}
684
685 template<typename Fn> void match(Fn F) const {
686 F(Ret, Params, CVQuals, RefQual, ExceptionSpec);
687 }
688
689 bool hasRHSComponentSlow(OutputStream &) const override { return true; }
690 bool hasFunctionSlow(OutputStream &) const override { return true; }
691
692 // Handle C++'s ... quirky decl grammar by using the left & right
693 // distinction. Consider:
694 // int (*f(float))(char) {}
695 // f is a function that takes a float and returns a pointer to a function
696 // that takes a char and returns an int. If we're trying to print f, start
697 // by printing out the return types's left, then print our parameters, then
698 // finally print right of the return type.
699 void printLeft(OutputStream &S) const override {
700 Ret->printLeft(S);
701 S += " ";
702 }
703
704 void printRight(OutputStream &S) const override {
705 S += "(";
706 Params.printWithComma(S);
707 S += ")";
708 Ret->printRight(S);
709
710 if (CVQuals & QualConst)
711 S += " const";
712 if (CVQuals & QualVolatile)
713 S += " volatile";
714 if (CVQuals & QualRestrict)
715 S += " restrict";
716
717 if (RefQual == FrefQualLValue)
718 S += " &";
719 else if (RefQual == FrefQualRValue)
720 S += " &&";
721
722 if (ExceptionSpec != nullptr) {
723 S += ' ';
724 ExceptionSpec->print(S);
725 }
726 }
727};
728
729class NoexceptSpec : public Node {
730 const Node *E;
731public:
732 NoexceptSpec(const Node *E_) : Node(KNoexceptSpec), E(E_) {}
733
734 template<typename Fn> void match(Fn F) const { F(E); }
735
736 void printLeft(OutputStream &S) const override {
737 S += "noexcept(";
738 E->print(S);
739 S += ")";
740 }
741};
742
743class DynamicExceptionSpec : public Node {
744 NodeArray Types;
745public:
746 DynamicExceptionSpec(NodeArray Types_)
747 : Node(KDynamicExceptionSpec), Types(Types_) {}
748
749 template<typename Fn> void match(Fn F) const { F(Types); }
750
751 void printLeft(OutputStream &S) const override {
752 S += "throw(";
753 Types.printWithComma(S);
754 S += ')';
755 }
756};
757
758class FunctionEncoding final : public Node {
759 const Node *Ret;
760 const Node *Name;
761 NodeArray Params;
762 const Node *Attrs;
763 Qualifiers CVQuals;
764 FunctionRefQual RefQual;
765
766public:
767 FunctionEncoding(const Node *Ret_, const Node *Name_, NodeArray Params_,
768 const Node *Attrs_, Qualifiers CVQuals_,
769 FunctionRefQual RefQual_)
770 : Node(KFunctionEncoding,
771 /*RHSComponentCache=*/Cache::Yes, /*ArrayCache=*/Cache::No,
772 /*FunctionCache=*/Cache::Yes),
773 Ret(Ret_), Name(Name_), Params(Params_), Attrs(Attrs_),
774 CVQuals(CVQuals_), RefQual(RefQual_) {}
775
776 template<typename Fn> void match(Fn F) const {
777 F(Ret, Name, Params, Attrs, CVQuals, RefQual);
778 }
779
780 Qualifiers getCVQuals() const { return CVQuals; }
781 FunctionRefQual getRefQual() const { return RefQual; }
782 NodeArray getParams() const { return Params; }
783 const Node *getReturnType() const { return Ret; }
784
785 bool hasRHSComponentSlow(OutputStream &) const override { return true; }
786 bool hasFunctionSlow(OutputStream &) const override { return true; }
787
788 const Node *getName() const { return Name; }
789
790 void printLeft(OutputStream &S) const override {
791 if (Ret) {
792 Ret->printLeft(S);
793 if (!Ret->hasRHSComponent(S))
794 S += " ";
795 }
796 Name->print(S);
797 }
798
799 void printRight(OutputStream &S) const override {
800 S += "(";
801 Params.printWithComma(S);
802 S += ")";
803 if (Ret)
804 Ret->printRight(S);
805
806 if (CVQuals & QualConst)
807 S += " const";
808 if (CVQuals & QualVolatile)
809 S += " volatile";
810 if (CVQuals & QualRestrict)
811 S += " restrict";
812
813 if (RefQual == FrefQualLValue)
814 S += " &";
815 else if (RefQual == FrefQualRValue)
816 S += " &&";
817
818 if (Attrs != nullptr)
819 Attrs->print(S);
820 }
821};
822
823class LiteralOperator : public Node {
824 const Node *OpName;
825
826public:
827 LiteralOperator(const Node *OpName_)
828 : Node(KLiteralOperator), OpName(OpName_) {}
829
830 template<typename Fn> void match(Fn F) const { F(OpName); }
831
832 void printLeft(OutputStream &S) const override {
833 S += "operator\"\" ";
834 OpName->print(S);
835 }
836};
837
838class SpecialName final : public Node {
839 const StringView Special;
840 const Node *Child;
841
842public:
843 SpecialName(StringView Special_, const Node *Child_)
844 : Node(KSpecialName), Special(Special_), Child(Child_) {}
845
846 template<typename Fn> void match(Fn F) const { F(Special, Child); }
847
848 void printLeft(OutputStream &S) const override {
849 S += Special;
850 Child->print(S);
851 }
852};
853
854class CtorVtableSpecialName final : public Node {
855 const Node *FirstType;
856 const Node *SecondType;
857
858public:
859 CtorVtableSpecialName(const Node *FirstType_, const Node *SecondType_)
860 : Node(KCtorVtableSpecialName),
861 FirstType(FirstType_), SecondType(SecondType_) {}
862
863 template<typename Fn> void match(Fn F) const { F(FirstType, SecondType); }
864
865 void printLeft(OutputStream &S) const override {
866 S += "construction vtable for ";
867 FirstType->print(S);
868 S += "-in-";
869 SecondType->print(S);
870 }
871};
872
873struct NestedName : Node {
874 Node *Qual;
875 Node *Name;
876
877 NestedName(Node *Qual_, Node *Name_)
878 : Node(KNestedName), Qual(Qual_), Name(Name_) {}
879
880 template<typename Fn> void match(Fn F) const { F(Qual, Name); }
881
882 StringView getBaseName() const override { return Name->getBaseName(); }
883
884 void printLeft(OutputStream &S) const override {
885 Qual->print(S);
886 S += "::";
887 Name->print(S);
888 }
889};
890
891struct LocalName : Node {
892 Node *Encoding;
893 Node *Entity;
894
895 LocalName(Node *Encoding_, Node *Entity_)
896 : Node(KLocalName), Encoding(Encoding_), Entity(Entity_) {}
897
898 template<typename Fn> void match(Fn F) const { F(Encoding, Entity); }
899
900 void printLeft(OutputStream &S) const override {
901 Encoding->print(S);
902 S += "::";
903 Entity->print(S);
904 }
905};
906
907class QualifiedName final : public Node {
908 // qualifier::name
909 const Node *Qualifier;
910 const Node *Name;
911
912public:
913 QualifiedName(const Node *Qualifier_, const Node *Name_)
914 : Node(KQualifiedName), Qualifier(Qualifier_), Name(Name_) {}
915
916 template<typename Fn> void match(Fn F) const { F(Qualifier, Name); }
917
918 StringView getBaseName() const override { return Name->getBaseName(); }
919
920 void printLeft(OutputStream &S) const override {
921 Qualifier->print(S);
922 S += "::";
923 Name->print(S);
924 }
925};
926
927class VectorType final : public Node {
928 const Node *BaseType;
929 const NodeOrString Dimension;
930
931public:
932 VectorType(const Node *BaseType_, NodeOrString Dimension_)
933 : Node(KVectorType), BaseType(BaseType_),
934 Dimension(Dimension_) {}
935
936 template<typename Fn> void match(Fn F) const { F(BaseType, Dimension); }
937
938 void printLeft(OutputStream &S) const override {
939 BaseType->print(S);
940 S += " vector[";
941 if (Dimension.isNode())
942 Dimension.asNode()->print(S);
943 else if (Dimension.isString())
944 S += Dimension.asString();
945 S += "]";
946 }
947};
948
949class PixelVectorType final : public Node {
950 const NodeOrString Dimension;
951
952public:
953 PixelVectorType(NodeOrString Dimension_)
954 : Node(KPixelVectorType), Dimension(Dimension_) {}
955
956 template<typename Fn> void match(Fn F) const { F(Dimension); }
957
958 void printLeft(OutputStream &S) const override {
959 // FIXME: This should demangle as "vector pixel".
960 S += "pixel vector[";
961 S += Dimension.asString();
962 S += "]";
963 }
964};
965
966/// An unexpanded parameter pack (either in the expression or type context). If
967/// this AST is correct, this node will have a ParameterPackExpansion node above
968/// it.
969///
970/// This node is created when some <template-args> are found that apply to an
971/// <encoding>, and is stored in the TemplateParams table. In order for this to
972/// appear in the final AST, it has to referenced via a <template-param> (ie,
973/// T_).
974class ParameterPack final : public Node {
975 NodeArray Data;
976
977 // Setup OutputStream for a pack expansion unless we're already expanding one.
978 void initializePackExpansion(OutputStream &S) const {
979 if (S.CurrentPackMax == std::numeric_limits<unsigned>::max()) {
980 S.CurrentPackMax = static_cast<unsigned>(Data.size());
981 S.CurrentPackIndex = 0;
982 }
983 }
984
985public:
986 ParameterPack(NodeArray Data_) : Node(KParameterPack), Data(Data_) {
987 ArrayCache = FunctionCache = RHSComponentCache = Cache::Unknown;
988 if (std::all_of(Data.begin(), Data.end(), [](Node* P) {
989 return P->ArrayCache == Cache::No;
990 }))
991 ArrayCache = Cache::No;
992 if (std::all_of(Data.begin(), Data.end(), [](Node* P) {
993 return P->FunctionCache == Cache::No;
994 }))
995 FunctionCache = Cache::No;
996 if (std::all_of(Data.begin(), Data.end(), [](Node* P) {
997 return P->RHSComponentCache == Cache::No;
998 }))
999 RHSComponentCache = Cache::No;
1000 }
1001
1002 template<typename Fn> void match(Fn F) const { F(Data); }
1003
1004 bool hasRHSComponentSlow(OutputStream &S) const override {
1005 initializePackExpansion(S);
1006 size_t Idx = S.CurrentPackIndex;
1007 return Idx < Data.size() && Data[Idx]->hasRHSComponent(S);
1008 }
1009 bool hasArraySlow(OutputStream &S) const override {
1010 initializePackExpansion(S);
1011 size_t Idx = S.CurrentPackIndex;
1012 return Idx < Data.size() && Data[Idx]->hasArray(S);
1013 }
1014 bool hasFunctionSlow(OutputStream &S) const override {
1015 initializePackExpansion(S);
1016 size_t Idx = S.CurrentPackIndex;
1017 return Idx < Data.size() && Data[Idx]->hasFunction(S);
1018 }
1019 const Node *getSyntaxNode(OutputStream &S) const override {
1020 initializePackExpansion(S);
1021 size_t Idx = S.CurrentPackIndex;
1022 return Idx < Data.size() ? Data[Idx]->getSyntaxNode(S) : this;
1023 }
1024
1025 void printLeft(OutputStream &S) const override {
1026 initializePackExpansion(S);
1027 size_t Idx = S.CurrentPackIndex;
1028 if (Idx < Data.size())
1029 Data[Idx]->printLeft(S);
1030 }
1031 void printRight(OutputStream &S) const override {
1032 initializePackExpansion(S);
1033 size_t Idx = S.CurrentPackIndex;
1034 if (Idx < Data.size())
1035 Data[Idx]->printRight(S);
1036 }
1037};
1038
1039/// A variadic template argument. This node represents an occurrence of
1040/// J<something>E in some <template-args>. It isn't itself unexpanded, unless
1041/// one of it's Elements is. The parser inserts a ParameterPack into the
1042/// TemplateParams table if the <template-args> this pack belongs to apply to an
1043/// <encoding>.
1044class TemplateArgumentPack final : public Node {
1045 NodeArray Elements;
1046public:
1047 TemplateArgumentPack(NodeArray Elements_)
1048 : Node(KTemplateArgumentPack), Elements(Elements_) {}
1049
1050 template<typename Fn> void match(Fn F) const { F(Elements); }
1051
1052 NodeArray getElements() const { return Elements; }
1053
1054 void printLeft(OutputStream &S) const override {
1055 Elements.printWithComma(S);
1056 }
1057};
1058
1059/// A pack expansion. Below this node, there are some unexpanded ParameterPacks
1060/// which each have Child->ParameterPackSize elements.
1061class ParameterPackExpansion final : public Node {
1062 const Node *Child;
1063
1064public:
1065 ParameterPackExpansion(const Node *Child_)
1066 : Node(KParameterPackExpansion), Child(Child_) {}
1067
1068 template<typename Fn> void match(Fn F) const { F(Child); }
1069
1070 const Node *getChild() const { return Child; }
1071
1072 void printLeft(OutputStream &S) const override {
1073 constexpr unsigned Max = std::numeric_limits<unsigned>::max();
1074 SwapAndRestore<unsigned> SavePackIdx(S.CurrentPackIndex, Max);
1075 SwapAndRestore<unsigned> SavePackMax(S.CurrentPackMax, Max);
1076 size_t StreamPos = S.getCurrentPosition();
1077
1078 // Print the first element in the pack. If Child contains a ParameterPack,
1079 // it will set up S.CurrentPackMax and print the first element.
1080 Child->print(S);
1081
1082 // No ParameterPack was found in Child. This can occur if we've found a pack
1083 // expansion on a <function-param>.
1084 if (S.CurrentPackMax == Max) {
1085 S += "...";
1086 return;
1087 }
1088
1089 // We found a ParameterPack, but it has no elements. Erase whatever we may
1090 // of printed.
1091 if (S.CurrentPackMax == 0) {
1092 S.setCurrentPosition(StreamPos);
1093 return;
1094 }
1095
1096 // Else, iterate through the rest of the elements in the pack.
1097 for (unsigned I = 1, E = S.CurrentPackMax; I < E; ++I) {
1098 S += ", ";
1099 S.CurrentPackIndex = I;
1100 Child->print(S);
1101 }
1102 }
1103};
1104
1105class TemplateArgs final : public Node {
1106 NodeArray Params;
1107
1108public:
1109 TemplateArgs(NodeArray Params_) : Node(KTemplateArgs), Params(Params_) {}
1110
1111 template<typename Fn> void match(Fn F) const { F(Params); }
1112
1113 NodeArray getParams() { return Params; }
1114
1115 void printLeft(OutputStream &S) const override {
1116 S += "<";
1117 Params.printWithComma(S);
1118 if (S.back() == '>')
1119 S += " ";
1120 S += ">";
1121 }
1122};
1123
Richard Smithb485b352018-08-24 23:30:26 +00001124/// A forward-reference to a template argument that was not known at the point
1125/// where the template parameter name was parsed in a mangling.
1126///
1127/// This is created when demangling the name of a specialization of a
1128/// conversion function template:
1129///
1130/// \code
1131/// struct A {
1132/// template<typename T> operator T*();
1133/// };
1134/// \endcode
1135///
1136/// When demangling a specialization of the conversion function template, we
1137/// encounter the name of the template (including the \c T) before we reach
1138/// the template argument list, so we cannot substitute the parameter name
1139/// for the corresponding argument while parsing. Instead, we create a
1140/// \c ForwardTemplateReference node that is resolved after we parse the
1141/// template arguments.
Richard Smithc20d1442018-08-20 20:14:49 +00001142struct ForwardTemplateReference : Node {
1143 size_t Index;
1144 Node *Ref = nullptr;
1145
1146 // If we're currently printing this node. It is possible (though invalid) for
1147 // a forward template reference to refer to itself via a substitution. This
1148 // creates a cyclic AST, which will stack overflow printing. To fix this, bail
1149 // out if more than one print* function is active.
1150 mutable bool Printing = false;
1151
1152 ForwardTemplateReference(size_t Index_)
1153 : Node(KForwardTemplateReference, Cache::Unknown, Cache::Unknown,
1154 Cache::Unknown),
1155 Index(Index_) {}
1156
1157 // We don't provide a matcher for these, because the value of the node is
1158 // not determined by its construction parameters, and it generally needs
1159 // special handling.
1160 template<typename Fn> void match(Fn F) const = delete;
1161
1162 bool hasRHSComponentSlow(OutputStream &S) const override {
1163 if (Printing)
1164 return false;
1165 SwapAndRestore<bool> SavePrinting(Printing, true);
1166 return Ref->hasRHSComponent(S);
1167 }
1168 bool hasArraySlow(OutputStream &S) const override {
1169 if (Printing)
1170 return false;
1171 SwapAndRestore<bool> SavePrinting(Printing, true);
1172 return Ref->hasArray(S);
1173 }
1174 bool hasFunctionSlow(OutputStream &S) const override {
1175 if (Printing)
1176 return false;
1177 SwapAndRestore<bool> SavePrinting(Printing, true);
1178 return Ref->hasFunction(S);
1179 }
1180 const Node *getSyntaxNode(OutputStream &S) const override {
1181 if (Printing)
1182 return this;
1183 SwapAndRestore<bool> SavePrinting(Printing, true);
1184 return Ref->getSyntaxNode(S);
1185 }
1186
1187 void printLeft(OutputStream &S) const override {
1188 if (Printing)
1189 return;
1190 SwapAndRestore<bool> SavePrinting(Printing, true);
1191 Ref->printLeft(S);
1192 }
1193 void printRight(OutputStream &S) const override {
1194 if (Printing)
1195 return;
1196 SwapAndRestore<bool> SavePrinting(Printing, true);
1197 Ref->printRight(S);
1198 }
1199};
1200
1201struct NameWithTemplateArgs : Node {
1202 // name<template_args>
1203 Node *Name;
1204 Node *TemplateArgs;
1205
1206 NameWithTemplateArgs(Node *Name_, Node *TemplateArgs_)
1207 : Node(KNameWithTemplateArgs), Name(Name_), TemplateArgs(TemplateArgs_) {}
1208
1209 template<typename Fn> void match(Fn F) const { F(Name, TemplateArgs); }
1210
1211 StringView getBaseName() const override { return Name->getBaseName(); }
1212
1213 void printLeft(OutputStream &S) const override {
1214 Name->print(S);
1215 TemplateArgs->print(S);
1216 }
1217};
1218
1219class GlobalQualifiedName final : public Node {
1220 Node *Child;
1221
1222public:
1223 GlobalQualifiedName(Node* Child_)
1224 : Node(KGlobalQualifiedName), Child(Child_) {}
1225
1226 template<typename Fn> void match(Fn F) const { F(Child); }
1227
1228 StringView getBaseName() const override { return Child->getBaseName(); }
1229
1230 void printLeft(OutputStream &S) const override {
1231 S += "::";
1232 Child->print(S);
1233 }
1234};
1235
1236struct StdQualifiedName : Node {
1237 Node *Child;
1238
1239 StdQualifiedName(Node *Child_) : Node(KStdQualifiedName), Child(Child_) {}
1240
1241 template<typename Fn> void match(Fn F) const { F(Child); }
1242
1243 StringView getBaseName() const override { return Child->getBaseName(); }
1244
1245 void printLeft(OutputStream &S) const override {
1246 S += "std::";
1247 Child->print(S);
1248 }
1249};
1250
1251enum class SpecialSubKind {
1252 allocator,
1253 basic_string,
1254 string,
1255 istream,
1256 ostream,
1257 iostream,
1258};
1259
1260class ExpandedSpecialSubstitution final : public Node {
1261 SpecialSubKind SSK;
1262
1263public:
1264 ExpandedSpecialSubstitution(SpecialSubKind SSK_)
1265 : Node(KExpandedSpecialSubstitution), SSK(SSK_) {}
1266
1267 template<typename Fn> void match(Fn F) const { F(SSK); }
1268
1269 StringView getBaseName() const override {
1270 switch (SSK) {
1271 case SpecialSubKind::allocator:
1272 return StringView("allocator");
1273 case SpecialSubKind::basic_string:
1274 return StringView("basic_string");
1275 case SpecialSubKind::string:
1276 return StringView("basic_string");
1277 case SpecialSubKind::istream:
1278 return StringView("basic_istream");
1279 case SpecialSubKind::ostream:
1280 return StringView("basic_ostream");
1281 case SpecialSubKind::iostream:
1282 return StringView("basic_iostream");
1283 }
Erik Pilkingtonf70e4d82019-01-17 20:37:51 +00001284 DEMANGLE_UNREACHABLE;
Richard Smithc20d1442018-08-20 20:14:49 +00001285 }
1286
1287 void printLeft(OutputStream &S) const override {
1288 switch (SSK) {
1289 case SpecialSubKind::allocator:
Richard Smithb485b352018-08-24 23:30:26 +00001290 S += "std::allocator";
Richard Smithc20d1442018-08-20 20:14:49 +00001291 break;
1292 case SpecialSubKind::basic_string:
Richard Smithb485b352018-08-24 23:30:26 +00001293 S += "std::basic_string";
1294 break;
Richard Smithc20d1442018-08-20 20:14:49 +00001295 case SpecialSubKind::string:
1296 S += "std::basic_string<char, std::char_traits<char>, "
1297 "std::allocator<char> >";
1298 break;
1299 case SpecialSubKind::istream:
1300 S += "std::basic_istream<char, std::char_traits<char> >";
1301 break;
1302 case SpecialSubKind::ostream:
1303 S += "std::basic_ostream<char, std::char_traits<char> >";
1304 break;
1305 case SpecialSubKind::iostream:
1306 S += "std::basic_iostream<char, std::char_traits<char> >";
1307 break;
1308 }
1309 }
1310};
1311
1312class SpecialSubstitution final : public Node {
1313public:
1314 SpecialSubKind SSK;
1315
1316 SpecialSubstitution(SpecialSubKind SSK_)
1317 : Node(KSpecialSubstitution), SSK(SSK_) {}
1318
1319 template<typename Fn> void match(Fn F) const { F(SSK); }
1320
1321 StringView getBaseName() const override {
1322 switch (SSK) {
1323 case SpecialSubKind::allocator:
1324 return StringView("allocator");
1325 case SpecialSubKind::basic_string:
1326 return StringView("basic_string");
1327 case SpecialSubKind::string:
1328 return StringView("string");
1329 case SpecialSubKind::istream:
1330 return StringView("istream");
1331 case SpecialSubKind::ostream:
1332 return StringView("ostream");
1333 case SpecialSubKind::iostream:
1334 return StringView("iostream");
1335 }
Erik Pilkingtonf70e4d82019-01-17 20:37:51 +00001336 DEMANGLE_UNREACHABLE;
Richard Smithc20d1442018-08-20 20:14:49 +00001337 }
1338
1339 void printLeft(OutputStream &S) const override {
1340 switch (SSK) {
1341 case SpecialSubKind::allocator:
1342 S += "std::allocator";
1343 break;
1344 case SpecialSubKind::basic_string:
1345 S += "std::basic_string";
1346 break;
1347 case SpecialSubKind::string:
1348 S += "std::string";
1349 break;
1350 case SpecialSubKind::istream:
1351 S += "std::istream";
1352 break;
1353 case SpecialSubKind::ostream:
1354 S += "std::ostream";
1355 break;
1356 case SpecialSubKind::iostream:
1357 S += "std::iostream";
1358 break;
1359 }
1360 }
1361};
1362
1363class CtorDtorName final : public Node {
1364 const Node *Basename;
1365 const bool IsDtor;
Pavel Labathf4e67eb2018-10-10 08:39:16 +00001366 const int Variant;
Richard Smithc20d1442018-08-20 20:14:49 +00001367
1368public:
Pavel Labathf4e67eb2018-10-10 08:39:16 +00001369 CtorDtorName(const Node *Basename_, bool IsDtor_, int Variant_)
1370 : Node(KCtorDtorName), Basename(Basename_), IsDtor(IsDtor_),
1371 Variant(Variant_) {}
Richard Smithc20d1442018-08-20 20:14:49 +00001372
Pavel Labathf4e67eb2018-10-10 08:39:16 +00001373 template<typename Fn> void match(Fn F) const { F(Basename, IsDtor, Variant); }
Richard Smithc20d1442018-08-20 20:14:49 +00001374
1375 void printLeft(OutputStream &S) const override {
1376 if (IsDtor)
1377 S += "~";
1378 S += Basename->getBaseName();
1379 }
1380};
1381
1382class DtorName : public Node {
1383 const Node *Base;
1384
1385public:
1386 DtorName(const Node *Base_) : Node(KDtorName), Base(Base_) {}
1387
1388 template<typename Fn> void match(Fn F) const { F(Base); }
1389
1390 void printLeft(OutputStream &S) const override {
1391 S += "~";
1392 Base->printLeft(S);
1393 }
1394};
1395
1396class UnnamedTypeName : public Node {
1397 const StringView Count;
1398
1399public:
1400 UnnamedTypeName(StringView Count_) : Node(KUnnamedTypeName), Count(Count_) {}
1401
1402 template<typename Fn> void match(Fn F) const { F(Count); }
1403
1404 void printLeft(OutputStream &S) const override {
1405 S += "'unnamed";
1406 S += Count;
1407 S += "\'";
1408 }
1409};
1410
1411class ClosureTypeName : public Node {
1412 NodeArray Params;
1413 StringView Count;
1414
1415public:
1416 ClosureTypeName(NodeArray Params_, StringView Count_)
1417 : Node(KClosureTypeName), Params(Params_), Count(Count_) {}
1418
1419 template<typename Fn> void match(Fn F) const { F(Params, Count); }
1420
1421 void printLeft(OutputStream &S) const override {
1422 S += "\'lambda";
1423 S += Count;
1424 S += "\'(";
1425 Params.printWithComma(S);
1426 S += ")";
1427 }
1428};
1429
1430class StructuredBindingName : public Node {
1431 NodeArray Bindings;
1432public:
1433 StructuredBindingName(NodeArray Bindings_)
1434 : Node(KStructuredBindingName), Bindings(Bindings_) {}
1435
1436 template<typename Fn> void match(Fn F) const { F(Bindings); }
1437
1438 void printLeft(OutputStream &S) const override {
1439 S += '[';
1440 Bindings.printWithComma(S);
1441 S += ']';
1442 }
1443};
1444
1445// -- Expression Nodes --
1446
1447class BinaryExpr : public Node {
1448 const Node *LHS;
1449 const StringView InfixOperator;
1450 const Node *RHS;
1451
1452public:
1453 BinaryExpr(const Node *LHS_, StringView InfixOperator_, const Node *RHS_)
1454 : Node(KBinaryExpr), LHS(LHS_), InfixOperator(InfixOperator_), RHS(RHS_) {
1455 }
1456
1457 template<typename Fn> void match(Fn F) const { F(LHS, InfixOperator, RHS); }
1458
1459 void printLeft(OutputStream &S) const override {
1460 // might be a template argument expression, then we need to disambiguate
1461 // with parens.
1462 if (InfixOperator == ">")
1463 S += "(";
1464
1465 S += "(";
1466 LHS->print(S);
1467 S += ") ";
1468 S += InfixOperator;
1469 S += " (";
1470 RHS->print(S);
1471 S += ")";
1472
1473 if (InfixOperator == ">")
1474 S += ")";
1475 }
1476};
1477
1478class ArraySubscriptExpr : public Node {
1479 const Node *Op1;
1480 const Node *Op2;
1481
1482public:
1483 ArraySubscriptExpr(const Node *Op1_, const Node *Op2_)
1484 : Node(KArraySubscriptExpr), Op1(Op1_), Op2(Op2_) {}
1485
1486 template<typename Fn> void match(Fn F) const { F(Op1, Op2); }
1487
1488 void printLeft(OutputStream &S) const override {
1489 S += "(";
1490 Op1->print(S);
1491 S += ")[";
1492 Op2->print(S);
1493 S += "]";
1494 }
1495};
1496
1497class PostfixExpr : public Node {
1498 const Node *Child;
1499 const StringView Operator;
1500
1501public:
1502 PostfixExpr(const Node *Child_, StringView Operator_)
1503 : Node(KPostfixExpr), Child(Child_), Operator(Operator_) {}
1504
1505 template<typename Fn> void match(Fn F) const { F(Child, Operator); }
1506
1507 void printLeft(OutputStream &S) const override {
1508 S += "(";
1509 Child->print(S);
1510 S += ")";
1511 S += Operator;
1512 }
1513};
1514
1515class ConditionalExpr : public Node {
1516 const Node *Cond;
1517 const Node *Then;
1518 const Node *Else;
1519
1520public:
1521 ConditionalExpr(const Node *Cond_, const Node *Then_, const Node *Else_)
1522 : Node(KConditionalExpr), Cond(Cond_), Then(Then_), Else(Else_) {}
1523
1524 template<typename Fn> void match(Fn F) const { F(Cond, Then, Else); }
1525
1526 void printLeft(OutputStream &S) const override {
1527 S += "(";
1528 Cond->print(S);
1529 S += ") ? (";
1530 Then->print(S);
1531 S += ") : (";
1532 Else->print(S);
1533 S += ")";
1534 }
1535};
1536
1537class MemberExpr : public Node {
1538 const Node *LHS;
1539 const StringView Kind;
1540 const Node *RHS;
1541
1542public:
1543 MemberExpr(const Node *LHS_, StringView Kind_, const Node *RHS_)
1544 : Node(KMemberExpr), LHS(LHS_), Kind(Kind_), RHS(RHS_) {}
1545
1546 template<typename Fn> void match(Fn F) const { F(LHS, Kind, RHS); }
1547
1548 void printLeft(OutputStream &S) const override {
1549 LHS->print(S);
1550 S += Kind;
1551 RHS->print(S);
1552 }
1553};
1554
1555class EnclosingExpr : public Node {
1556 const StringView Prefix;
1557 const Node *Infix;
1558 const StringView Postfix;
1559
1560public:
1561 EnclosingExpr(StringView Prefix_, Node *Infix_, StringView Postfix_)
1562 : Node(KEnclosingExpr), Prefix(Prefix_), Infix(Infix_),
1563 Postfix(Postfix_) {}
1564
1565 template<typename Fn> void match(Fn F) const { F(Prefix, Infix, Postfix); }
1566
1567 void printLeft(OutputStream &S) const override {
1568 S += Prefix;
1569 Infix->print(S);
1570 S += Postfix;
1571 }
1572};
1573
1574class CastExpr : public Node {
1575 // cast_kind<to>(from)
1576 const StringView CastKind;
1577 const Node *To;
1578 const Node *From;
1579
1580public:
1581 CastExpr(StringView CastKind_, const Node *To_, const Node *From_)
1582 : Node(KCastExpr), CastKind(CastKind_), To(To_), From(From_) {}
1583
1584 template<typename Fn> void match(Fn F) const { F(CastKind, To, From); }
1585
1586 void printLeft(OutputStream &S) const override {
1587 S += CastKind;
1588 S += "<";
1589 To->printLeft(S);
1590 S += ">(";
1591 From->printLeft(S);
1592 S += ")";
1593 }
1594};
1595
1596class SizeofParamPackExpr : public Node {
1597 const Node *Pack;
1598
1599public:
1600 SizeofParamPackExpr(const Node *Pack_)
1601 : Node(KSizeofParamPackExpr), Pack(Pack_) {}
1602
1603 template<typename Fn> void match(Fn F) const { F(Pack); }
1604
1605 void printLeft(OutputStream &S) const override {
1606 S += "sizeof...(";
1607 ParameterPackExpansion PPE(Pack);
1608 PPE.printLeft(S);
1609 S += ")";
1610 }
1611};
1612
1613class CallExpr : public Node {
1614 const Node *Callee;
1615 NodeArray Args;
1616
1617public:
1618 CallExpr(const Node *Callee_, NodeArray Args_)
1619 : Node(KCallExpr), Callee(Callee_), Args(Args_) {}
1620
1621 template<typename Fn> void match(Fn F) const { F(Callee, Args); }
1622
1623 void printLeft(OutputStream &S) const override {
1624 Callee->print(S);
1625 S += "(";
1626 Args.printWithComma(S);
1627 S += ")";
1628 }
1629};
1630
1631class NewExpr : public Node {
1632 // new (expr_list) type(init_list)
1633 NodeArray ExprList;
1634 Node *Type;
1635 NodeArray InitList;
1636 bool IsGlobal; // ::operator new ?
1637 bool IsArray; // new[] ?
1638public:
1639 NewExpr(NodeArray ExprList_, Node *Type_, NodeArray InitList_, bool IsGlobal_,
1640 bool IsArray_)
1641 : Node(KNewExpr), ExprList(ExprList_), Type(Type_), InitList(InitList_),
1642 IsGlobal(IsGlobal_), IsArray(IsArray_) {}
1643
1644 template<typename Fn> void match(Fn F) const {
1645 F(ExprList, Type, InitList, IsGlobal, IsArray);
1646 }
1647
1648 void printLeft(OutputStream &S) const override {
1649 if (IsGlobal)
1650 S += "::operator ";
1651 S += "new";
1652 if (IsArray)
1653 S += "[]";
1654 S += ' ';
1655 if (!ExprList.empty()) {
1656 S += "(";
1657 ExprList.printWithComma(S);
1658 S += ")";
1659 }
1660 Type->print(S);
1661 if (!InitList.empty()) {
1662 S += "(";
1663 InitList.printWithComma(S);
1664 S += ")";
1665 }
1666
1667 }
1668};
1669
1670class DeleteExpr : public Node {
1671 Node *Op;
1672 bool IsGlobal;
1673 bool IsArray;
1674
1675public:
1676 DeleteExpr(Node *Op_, bool IsGlobal_, bool IsArray_)
1677 : Node(KDeleteExpr), Op(Op_), IsGlobal(IsGlobal_), IsArray(IsArray_) {}
1678
1679 template<typename Fn> void match(Fn F) const { F(Op, IsGlobal, IsArray); }
1680
1681 void printLeft(OutputStream &S) const override {
1682 if (IsGlobal)
1683 S += "::";
1684 S += "delete";
1685 if (IsArray)
1686 S += "[] ";
1687 Op->print(S);
1688 }
1689};
1690
1691class PrefixExpr : public Node {
1692 StringView Prefix;
1693 Node *Child;
1694
1695public:
1696 PrefixExpr(StringView Prefix_, Node *Child_)
1697 : Node(KPrefixExpr), Prefix(Prefix_), Child(Child_) {}
1698
1699 template<typename Fn> void match(Fn F) const { F(Prefix, Child); }
1700
1701 void printLeft(OutputStream &S) const override {
1702 S += Prefix;
1703 S += "(";
1704 Child->print(S);
1705 S += ")";
1706 }
1707};
1708
1709class FunctionParam : public Node {
1710 StringView Number;
1711
1712public:
1713 FunctionParam(StringView Number_) : Node(KFunctionParam), Number(Number_) {}
1714
1715 template<typename Fn> void match(Fn F) const { F(Number); }
1716
1717 void printLeft(OutputStream &S) const override {
1718 S += "fp";
1719 S += Number;
1720 }
1721};
1722
1723class ConversionExpr : public Node {
1724 const Node *Type;
1725 NodeArray Expressions;
1726
1727public:
1728 ConversionExpr(const Node *Type_, NodeArray Expressions_)
1729 : Node(KConversionExpr), Type(Type_), Expressions(Expressions_) {}
1730
1731 template<typename Fn> void match(Fn F) const { F(Type, Expressions); }
1732
1733 void printLeft(OutputStream &S) const override {
1734 S += "(";
1735 Type->print(S);
1736 S += ")(";
1737 Expressions.printWithComma(S);
1738 S += ")";
1739 }
1740};
1741
1742class InitListExpr : public Node {
1743 const Node *Ty;
1744 NodeArray Inits;
1745public:
1746 InitListExpr(const Node *Ty_, NodeArray Inits_)
1747 : Node(KInitListExpr), Ty(Ty_), Inits(Inits_) {}
1748
1749 template<typename Fn> void match(Fn F) const { F(Ty, Inits); }
1750
1751 void printLeft(OutputStream &S) const override {
1752 if (Ty)
1753 Ty->print(S);
1754 S += '{';
1755 Inits.printWithComma(S);
1756 S += '}';
1757 }
1758};
1759
1760class BracedExpr : public Node {
1761 const Node *Elem;
1762 const Node *Init;
1763 bool IsArray;
1764public:
1765 BracedExpr(const Node *Elem_, const Node *Init_, bool IsArray_)
1766 : Node(KBracedExpr), Elem(Elem_), Init(Init_), IsArray(IsArray_) {}
1767
1768 template<typename Fn> void match(Fn F) const { F(Elem, Init, IsArray); }
1769
1770 void printLeft(OutputStream &S) const override {
1771 if (IsArray) {
1772 S += '[';
1773 Elem->print(S);
1774 S += ']';
1775 } else {
1776 S += '.';
1777 Elem->print(S);
1778 }
1779 if (Init->getKind() != KBracedExpr && Init->getKind() != KBracedRangeExpr)
1780 S += " = ";
1781 Init->print(S);
1782 }
1783};
1784
1785class BracedRangeExpr : public Node {
1786 const Node *First;
1787 const Node *Last;
1788 const Node *Init;
1789public:
1790 BracedRangeExpr(const Node *First_, const Node *Last_, const Node *Init_)
1791 : Node(KBracedRangeExpr), First(First_), Last(Last_), Init(Init_) {}
1792
1793 template<typename Fn> void match(Fn F) const { F(First, Last, Init); }
1794
1795 void printLeft(OutputStream &S) const override {
1796 S += '[';
1797 First->print(S);
1798 S += " ... ";
1799 Last->print(S);
1800 S += ']';
1801 if (Init->getKind() != KBracedExpr && Init->getKind() != KBracedRangeExpr)
1802 S += " = ";
1803 Init->print(S);
1804 }
1805};
1806
1807class FoldExpr : public Node {
1808 const Node *Pack, *Init;
1809 StringView OperatorName;
1810 bool IsLeftFold;
1811
1812public:
1813 FoldExpr(bool IsLeftFold_, StringView OperatorName_, const Node *Pack_,
1814 const Node *Init_)
1815 : Node(KFoldExpr), Pack(Pack_), Init(Init_), OperatorName(OperatorName_),
1816 IsLeftFold(IsLeftFold_) {}
1817
1818 template<typename Fn> void match(Fn F) const {
1819 F(IsLeftFold, OperatorName, Pack, Init);
1820 }
1821
1822 void printLeft(OutputStream &S) const override {
1823 auto PrintPack = [&] {
1824 S += '(';
1825 ParameterPackExpansion(Pack).print(S);
1826 S += ')';
1827 };
1828
1829 S += '(';
1830
1831 if (IsLeftFold) {
1832 // init op ... op pack
1833 if (Init != nullptr) {
1834 Init->print(S);
1835 S += ' ';
1836 S += OperatorName;
1837 S += ' ';
1838 }
1839 // ... op pack
1840 S += "... ";
1841 S += OperatorName;
1842 S += ' ';
1843 PrintPack();
1844 } else { // !IsLeftFold
1845 // pack op ...
1846 PrintPack();
1847 S += ' ';
1848 S += OperatorName;
1849 S += " ...";
1850 // pack op ... op init
1851 if (Init != nullptr) {
1852 S += ' ';
1853 S += OperatorName;
1854 S += ' ';
1855 Init->print(S);
1856 }
1857 }
1858 S += ')';
1859 }
1860};
1861
1862class ThrowExpr : public Node {
1863 const Node *Op;
1864
1865public:
1866 ThrowExpr(const Node *Op_) : Node(KThrowExpr), Op(Op_) {}
1867
1868 template<typename Fn> void match(Fn F) const { F(Op); }
1869
1870 void printLeft(OutputStream &S) const override {
1871 S += "throw ";
1872 Op->print(S);
1873 }
1874};
1875
1876class BoolExpr : public Node {
1877 bool Value;
1878
1879public:
1880 BoolExpr(bool Value_) : Node(KBoolExpr), Value(Value_) {}
1881
1882 template<typename Fn> void match(Fn F) const { F(Value); }
1883
1884 void printLeft(OutputStream &S) const override {
1885 S += Value ? StringView("true") : StringView("false");
1886 }
1887};
1888
1889class IntegerCastExpr : public Node {
1890 // ty(integer)
1891 const Node *Ty;
1892 StringView Integer;
1893
1894public:
1895 IntegerCastExpr(const Node *Ty_, StringView Integer_)
1896 : Node(KIntegerCastExpr), Ty(Ty_), Integer(Integer_) {}
1897
1898 template<typename Fn> void match(Fn F) const { F(Ty, Integer); }
1899
1900 void printLeft(OutputStream &S) const override {
1901 S += "(";
1902 Ty->print(S);
1903 S += ")";
1904 S += Integer;
1905 }
1906};
1907
1908class IntegerLiteral : public Node {
1909 StringView Type;
1910 StringView Value;
1911
1912public:
1913 IntegerLiteral(StringView Type_, StringView Value_)
1914 : Node(KIntegerLiteral), Type(Type_), Value(Value_) {}
1915
1916 template<typename Fn> void match(Fn F) const { F(Type, Value); }
1917
1918 void printLeft(OutputStream &S) const override {
1919 if (Type.size() > 3) {
1920 S += "(";
1921 S += Type;
1922 S += ")";
1923 }
1924
1925 if (Value[0] == 'n') {
1926 S += "-";
1927 S += Value.dropFront(1);
1928 } else
1929 S += Value;
1930
1931 if (Type.size() <= 3)
1932 S += Type;
1933 }
1934};
1935
1936template <class Float> struct FloatData;
1937
1938namespace float_literal_impl {
1939constexpr Node::Kind getFloatLiteralKind(float *) {
1940 return Node::KFloatLiteral;
1941}
1942constexpr Node::Kind getFloatLiteralKind(double *) {
1943 return Node::KDoubleLiteral;
1944}
1945constexpr Node::Kind getFloatLiteralKind(long double *) {
1946 return Node::KLongDoubleLiteral;
1947}
1948}
1949
1950template <class Float> class FloatLiteralImpl : public Node {
1951 const StringView Contents;
1952
1953 static constexpr Kind KindForClass =
1954 float_literal_impl::getFloatLiteralKind((Float *)nullptr);
1955
1956public:
1957 FloatLiteralImpl(StringView Contents_)
1958 : Node(KindForClass), Contents(Contents_) {}
1959
1960 template<typename Fn> void match(Fn F) const { F(Contents); }
1961
1962 void printLeft(OutputStream &s) const override {
1963 const char *first = Contents.begin();
1964 const char *last = Contents.end() + 1;
1965
1966 const size_t N = FloatData<Float>::mangled_size;
1967 if (static_cast<std::size_t>(last - first) > N) {
1968 last = first + N;
1969 union {
1970 Float value;
1971 char buf[sizeof(Float)];
1972 };
1973 const char *t = first;
1974 char *e = buf;
1975 for (; t != last; ++t, ++e) {
1976 unsigned d1 = isdigit(*t) ? static_cast<unsigned>(*t - '0')
1977 : static_cast<unsigned>(*t - 'a' + 10);
1978 ++t;
1979 unsigned d0 = isdigit(*t) ? static_cast<unsigned>(*t - '0')
1980 : static_cast<unsigned>(*t - 'a' + 10);
1981 *e = static_cast<char>((d1 << 4) + d0);
1982 }
1983#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
1984 std::reverse(buf, e);
1985#endif
1986 char num[FloatData<Float>::max_demangled_size] = {0};
1987 int n = snprintf(num, sizeof(num), FloatData<Float>::spec, value);
1988 s += StringView(num, num + n);
1989 }
1990 }
1991};
1992
1993using FloatLiteral = FloatLiteralImpl<float>;
1994using DoubleLiteral = FloatLiteralImpl<double>;
1995using LongDoubleLiteral = FloatLiteralImpl<long double>;
1996
1997/// Visit the node. Calls \c F(P), where \c P is the node cast to the
1998/// appropriate derived class.
1999template<typename Fn>
2000void Node::visit(Fn F) const {
2001 switch (K) {
2002#define CASE(X) case K ## X: return F(static_cast<const X*>(this));
2003 FOR_EACH_NODE_KIND(CASE)
2004#undef CASE
2005 }
2006 assert(0 && "unknown mangling node kind");
2007}
2008
2009/// Determine the kind of a node from its type.
2010template<typename NodeT> struct NodeKind;
2011#define SPECIALIZATION(X) \
2012 template<> struct NodeKind<X> { \
2013 static constexpr Node::Kind Kind = Node::K##X; \
2014 static constexpr const char *name() { return #X; } \
2015 };
2016FOR_EACH_NODE_KIND(SPECIALIZATION)
2017#undef SPECIALIZATION
2018
2019#undef FOR_EACH_NODE_KIND
2020
2021template <class T, size_t N>
2022class PODSmallVector {
2023 static_assert(std::is_pod<T>::value,
2024 "T is required to be a plain old data type");
2025
2026 T* First;
2027 T* Last;
2028 T* Cap;
2029 T Inline[N];
2030
2031 bool isInline() const { return First == Inline; }
2032
2033 void clearInline() {
2034 First = Inline;
2035 Last = Inline;
2036 Cap = Inline + N;
2037 }
2038
2039 void reserve(size_t NewCap) {
2040 size_t S = size();
2041 if (isInline()) {
2042 auto* Tmp = static_cast<T*>(std::malloc(NewCap * sizeof(T)));
2043 if (Tmp == nullptr)
2044 std::terminate();
2045 std::copy(First, Last, Tmp);
2046 First = Tmp;
2047 } else {
2048 First = static_cast<T*>(std::realloc(First, NewCap * sizeof(T)));
2049 if (First == nullptr)
2050 std::terminate();
2051 }
2052 Last = First + S;
2053 Cap = First + NewCap;
2054 }
2055
2056public:
2057 PODSmallVector() : First(Inline), Last(First), Cap(Inline + N) {}
2058
2059 PODSmallVector(const PODSmallVector&) = delete;
2060 PODSmallVector& operator=(const PODSmallVector&) = delete;
2061
2062 PODSmallVector(PODSmallVector&& Other) : PODSmallVector() {
2063 if (Other.isInline()) {
2064 std::copy(Other.begin(), Other.end(), First);
2065 Last = First + Other.size();
2066 Other.clear();
2067 return;
2068 }
2069
2070 First = Other.First;
2071 Last = Other.Last;
2072 Cap = Other.Cap;
2073 Other.clearInline();
2074 }
2075
2076 PODSmallVector& operator=(PODSmallVector&& Other) {
2077 if (Other.isInline()) {
2078 if (!isInline()) {
2079 std::free(First);
2080 clearInline();
2081 }
2082 std::copy(Other.begin(), Other.end(), First);
2083 Last = First + Other.size();
2084 Other.clear();
2085 return *this;
2086 }
2087
2088 if (isInline()) {
2089 First = Other.First;
2090 Last = Other.Last;
2091 Cap = Other.Cap;
2092 Other.clearInline();
2093 return *this;
2094 }
2095
2096 std::swap(First, Other.First);
2097 std::swap(Last, Other.Last);
2098 std::swap(Cap, Other.Cap);
2099 Other.clear();
2100 return *this;
2101 }
2102
2103 void push_back(const T& Elem) {
2104 if (Last == Cap)
2105 reserve(size() * 2);
2106 *Last++ = Elem;
2107 }
2108
2109 void pop_back() {
2110 assert(Last != First && "Popping empty vector!");
2111 --Last;
2112 }
2113
2114 void dropBack(size_t Index) {
2115 assert(Index <= size() && "dropBack() can't expand!");
2116 Last = First + Index;
2117 }
2118
2119 T* begin() { return First; }
2120 T* end() { return Last; }
2121
2122 bool empty() const { return First == Last; }
2123 size_t size() const { return static_cast<size_t>(Last - First); }
2124 T& back() {
2125 assert(Last != First && "Calling back() on empty vector!");
2126 return *(Last - 1);
2127 }
2128 T& operator[](size_t Index) {
2129 assert(Index < size() && "Invalid access!");
2130 return *(begin() + Index);
2131 }
2132 void clear() { Last = First; }
2133
2134 ~PODSmallVector() {
2135 if (!isInline())
2136 std::free(First);
2137 }
2138};
2139
Pavel Labathba825192018-10-16 14:29:14 +00002140template <typename Derived, typename Alloc> struct AbstractManglingParser {
Richard Smithc20d1442018-08-20 20:14:49 +00002141 const char *First;
2142 const char *Last;
2143
2144 // Name stack, this is used by the parser to hold temporary names that were
2145 // parsed. The parser collapses multiple names into new nodes to construct
2146 // the AST. Once the parser is finished, names.size() == 1.
2147 PODSmallVector<Node *, 32> Names;
2148
2149 // Substitution table. Itanium supports name substitutions as a means of
2150 // compression. The string "S42_" refers to the 44nd entry (base-36) in this
2151 // table.
2152 PODSmallVector<Node *, 32> Subs;
2153
2154 // Template parameter table. Like the above, but referenced like "T42_".
2155 // This has a smaller size compared to Subs and Names because it can be
2156 // stored on the stack.
2157 PODSmallVector<Node *, 8> TemplateParams;
2158
2159 // Set of unresolved forward <template-param> references. These can occur in a
2160 // conversion operator's type, and are resolved in the enclosing <encoding>.
2161 PODSmallVector<ForwardTemplateReference *, 4> ForwardTemplateRefs;
2162
Richard Smithc20d1442018-08-20 20:14:49 +00002163 bool TryToParseTemplateArgs = true;
2164 bool PermitForwardTemplateReferences = false;
2165 bool ParsingLambdaParams = false;
2166
2167 Alloc ASTAllocator;
2168
Pavel Labathba825192018-10-16 14:29:14 +00002169 AbstractManglingParser(const char *First_, const char *Last_)
2170 : First(First_), Last(Last_) {}
2171
2172 Derived &getDerived() { return static_cast<Derived &>(*this); }
Richard Smithc20d1442018-08-20 20:14:49 +00002173
2174 void reset(const char *First_, const char *Last_) {
2175 First = First_;
2176 Last = Last_;
2177 Names.clear();
2178 Subs.clear();
2179 TemplateParams.clear();
2180 ParsingLambdaParams = false;
2181 TryToParseTemplateArgs = true;
2182 PermitForwardTemplateReferences = false;
2183 ASTAllocator.reset();
2184 }
2185
Richard Smithb485b352018-08-24 23:30:26 +00002186 template <class T, class... Args> Node *make(Args &&... args) {
Richard Smithc20d1442018-08-20 20:14:49 +00002187 return ASTAllocator.template makeNode<T>(std::forward<Args>(args)...);
2188 }
2189
2190 template <class It> NodeArray makeNodeArray(It begin, It end) {
2191 size_t sz = static_cast<size_t>(end - begin);
2192 void *mem = ASTAllocator.allocateNodeArray(sz);
2193 Node **data = new (mem) Node *[sz];
2194 std::copy(begin, end, data);
2195 return NodeArray(data, sz);
2196 }
2197
2198 NodeArray popTrailingNodeArray(size_t FromPosition) {
2199 assert(FromPosition <= Names.size());
2200 NodeArray res =
2201 makeNodeArray(Names.begin() + (long)FromPosition, Names.end());
2202 Names.dropBack(FromPosition);
2203 return res;
2204 }
2205
2206 bool consumeIf(StringView S) {
2207 if (StringView(First, Last).startsWith(S)) {
2208 First += S.size();
2209 return true;
2210 }
2211 return false;
2212 }
2213
2214 bool consumeIf(char C) {
2215 if (First != Last && *First == C) {
2216 ++First;
2217 return true;
2218 }
2219 return false;
2220 }
2221
2222 char consume() { return First != Last ? *First++ : '\0'; }
2223
2224 char look(unsigned Lookahead = 0) {
2225 if (static_cast<size_t>(Last - First) <= Lookahead)
2226 return '\0';
2227 return First[Lookahead];
2228 }
2229
2230 size_t numLeft() const { return static_cast<size_t>(Last - First); }
2231
2232 StringView parseNumber(bool AllowNegative = false);
2233 Qualifiers parseCVQualifiers();
2234 bool parsePositiveInteger(size_t *Out);
2235 StringView parseBareSourceName();
2236
2237 bool parseSeqId(size_t *Out);
2238 Node *parseSubstitution();
2239 Node *parseTemplateParam();
2240 Node *parseTemplateArgs(bool TagTemplates = false);
2241 Node *parseTemplateArg();
2242
2243 /// Parse the <expr> production.
2244 Node *parseExpr();
2245 Node *parsePrefixExpr(StringView Kind);
2246 Node *parseBinaryExpr(StringView Kind);
2247 Node *parseIntegerLiteral(StringView Lit);
2248 Node *parseExprPrimary();
2249 template <class Float> Node *parseFloatingLiteral();
2250 Node *parseFunctionParam();
2251 Node *parseNewExpr();
2252 Node *parseConversionExpr();
2253 Node *parseBracedExpr();
2254 Node *parseFoldExpr();
2255
2256 /// Parse the <type> production.
2257 Node *parseType();
2258 Node *parseFunctionType();
2259 Node *parseVectorType();
2260 Node *parseDecltype();
2261 Node *parseArrayType();
2262 Node *parsePointerToMemberType();
2263 Node *parseClassEnumType();
2264 Node *parseQualifiedType();
2265
2266 Node *parseEncoding();
2267 bool parseCallOffset();
2268 Node *parseSpecialName();
2269
2270 /// Holds some extra information about a <name> that is being parsed. This
2271 /// information is only pertinent if the <name> refers to an <encoding>.
2272 struct NameState {
2273 bool CtorDtorConversion = false;
2274 bool EndsWithTemplateArgs = false;
2275 Qualifiers CVQualifiers = QualNone;
2276 FunctionRefQual ReferenceQualifier = FrefQualNone;
2277 size_t ForwardTemplateRefsBegin;
2278
Pavel Labathba825192018-10-16 14:29:14 +00002279 NameState(AbstractManglingParser *Enclosing)
Richard Smithc20d1442018-08-20 20:14:49 +00002280 : ForwardTemplateRefsBegin(Enclosing->ForwardTemplateRefs.size()) {}
2281 };
2282
2283 bool resolveForwardTemplateRefs(NameState &State) {
2284 size_t I = State.ForwardTemplateRefsBegin;
2285 size_t E = ForwardTemplateRefs.size();
2286 for (; I < E; ++I) {
2287 size_t Idx = ForwardTemplateRefs[I]->Index;
2288 if (Idx >= TemplateParams.size())
2289 return true;
2290 ForwardTemplateRefs[I]->Ref = TemplateParams[Idx];
2291 }
2292 ForwardTemplateRefs.dropBack(State.ForwardTemplateRefsBegin);
2293 return false;
2294 }
2295
2296 /// Parse the <name> production>
2297 Node *parseName(NameState *State = nullptr);
2298 Node *parseLocalName(NameState *State);
2299 Node *parseOperatorName(NameState *State);
2300 Node *parseUnqualifiedName(NameState *State);
2301 Node *parseUnnamedTypeName(NameState *State);
2302 Node *parseSourceName(NameState *State);
2303 Node *parseUnscopedName(NameState *State);
2304 Node *parseNestedName(NameState *State);
2305 Node *parseCtorDtorName(Node *&SoFar, NameState *State);
2306
2307 Node *parseAbiTags(Node *N);
2308
2309 /// Parse the <unresolved-name> production.
2310 Node *parseUnresolvedName();
2311 Node *parseSimpleId();
2312 Node *parseBaseUnresolvedName();
2313 Node *parseUnresolvedType();
2314 Node *parseDestructorName();
2315
2316 /// Top-level entry point into the parser.
2317 Node *parse();
2318};
2319
2320const char* parse_discriminator(const char* first, const char* last);
2321
2322// <name> ::= <nested-name> // N
2323// ::= <local-name> # See Scope Encoding below // Z
2324// ::= <unscoped-template-name> <template-args>
2325// ::= <unscoped-name>
2326//
2327// <unscoped-template-name> ::= <unscoped-name>
2328// ::= <substitution>
Pavel Labathba825192018-10-16 14:29:14 +00002329template <typename Derived, typename Alloc>
2330Node *AbstractManglingParser<Derived, Alloc>::parseName(NameState *State) {
Richard Smithc20d1442018-08-20 20:14:49 +00002331 consumeIf('L'); // extension
2332
2333 if (look() == 'N')
Pavel Labathba825192018-10-16 14:29:14 +00002334 return getDerived().parseNestedName(State);
Richard Smithc20d1442018-08-20 20:14:49 +00002335 if (look() == 'Z')
Pavel Labathba825192018-10-16 14:29:14 +00002336 return getDerived().parseLocalName(State);
Richard Smithc20d1442018-08-20 20:14:49 +00002337
2338 // ::= <unscoped-template-name> <template-args>
2339 if (look() == 'S' && look(1) != 't') {
Pavel Labathba825192018-10-16 14:29:14 +00002340 Node *S = getDerived().parseSubstitution();
Richard Smithc20d1442018-08-20 20:14:49 +00002341 if (S == nullptr)
2342 return nullptr;
2343 if (look() != 'I')
2344 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00002345 Node *TA = getDerived().parseTemplateArgs(State != nullptr);
Richard Smithc20d1442018-08-20 20:14:49 +00002346 if (TA == nullptr)
2347 return nullptr;
2348 if (State) State->EndsWithTemplateArgs = true;
2349 return make<NameWithTemplateArgs>(S, TA);
2350 }
2351
Pavel Labathba825192018-10-16 14:29:14 +00002352 Node *N = getDerived().parseUnscopedName(State);
Richard Smithc20d1442018-08-20 20:14:49 +00002353 if (N == nullptr)
2354 return nullptr;
2355 // ::= <unscoped-template-name> <template-args>
2356 if (look() == 'I') {
2357 Subs.push_back(N);
Pavel Labathba825192018-10-16 14:29:14 +00002358 Node *TA = getDerived().parseTemplateArgs(State != nullptr);
Richard Smithc20d1442018-08-20 20:14:49 +00002359 if (TA == nullptr)
2360 return nullptr;
2361 if (State) State->EndsWithTemplateArgs = true;
2362 return make<NameWithTemplateArgs>(N, TA);
2363 }
2364 // ::= <unscoped-name>
2365 return N;
2366}
2367
2368// <local-name> := Z <function encoding> E <entity name> [<discriminator>]
2369// := Z <function encoding> E s [<discriminator>]
2370// := Z <function encoding> Ed [ <parameter number> ] _ <entity name>
Pavel Labathba825192018-10-16 14:29:14 +00002371template <typename Derived, typename Alloc>
2372Node *AbstractManglingParser<Derived, Alloc>::parseLocalName(NameState *State) {
Richard Smithc20d1442018-08-20 20:14:49 +00002373 if (!consumeIf('Z'))
2374 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00002375 Node *Encoding = getDerived().parseEncoding();
Richard Smithc20d1442018-08-20 20:14:49 +00002376 if (Encoding == nullptr || !consumeIf('E'))
2377 return nullptr;
2378
2379 if (consumeIf('s')) {
2380 First = parse_discriminator(First, Last);
Richard Smithb485b352018-08-24 23:30:26 +00002381 auto *StringLitName = make<NameType>("string literal");
2382 if (!StringLitName)
2383 return nullptr;
2384 return make<LocalName>(Encoding, StringLitName);
Richard Smithc20d1442018-08-20 20:14:49 +00002385 }
2386
2387 if (consumeIf('d')) {
2388 parseNumber(true);
2389 if (!consumeIf('_'))
2390 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00002391 Node *N = getDerived().parseName(State);
Richard Smithc20d1442018-08-20 20:14:49 +00002392 if (N == nullptr)
2393 return nullptr;
2394 return make<LocalName>(Encoding, N);
2395 }
2396
Pavel Labathba825192018-10-16 14:29:14 +00002397 Node *Entity = getDerived().parseName(State);
Richard Smithc20d1442018-08-20 20:14:49 +00002398 if (Entity == nullptr)
2399 return nullptr;
2400 First = parse_discriminator(First, Last);
2401 return make<LocalName>(Encoding, Entity);
2402}
2403
2404// <unscoped-name> ::= <unqualified-name>
2405// ::= St <unqualified-name> # ::std::
2406// extension ::= StL<unqualified-name>
Pavel Labathba825192018-10-16 14:29:14 +00002407template <typename Derived, typename Alloc>
2408Node *
2409AbstractManglingParser<Derived, Alloc>::parseUnscopedName(NameState *State) {
2410 if (consumeIf("StL") || consumeIf("St")) {
2411 Node *R = getDerived().parseUnqualifiedName(State);
2412 if (R == nullptr)
2413 return nullptr;
2414 return make<StdQualifiedName>(R);
2415 }
2416 return getDerived().parseUnqualifiedName(State);
Richard Smithc20d1442018-08-20 20:14:49 +00002417}
2418
2419// <unqualified-name> ::= <operator-name> [abi-tags]
2420// ::= <ctor-dtor-name>
2421// ::= <source-name>
2422// ::= <unnamed-type-name>
2423// ::= DC <source-name>+ E # structured binding declaration
Pavel Labathba825192018-10-16 14:29:14 +00002424template <typename Derived, typename Alloc>
2425Node *
2426AbstractManglingParser<Derived, Alloc>::parseUnqualifiedName(NameState *State) {
Richard Smithc20d1442018-08-20 20:14:49 +00002427 // <ctor-dtor-name>s are special-cased in parseNestedName().
2428 Node *Result;
2429 if (look() == 'U')
Pavel Labathba825192018-10-16 14:29:14 +00002430 Result = getDerived().parseUnnamedTypeName(State);
Richard Smithc20d1442018-08-20 20:14:49 +00002431 else if (look() >= '1' && look() <= '9')
Pavel Labathba825192018-10-16 14:29:14 +00002432 Result = getDerived().parseSourceName(State);
Richard Smithc20d1442018-08-20 20:14:49 +00002433 else if (consumeIf("DC")) {
2434 size_t BindingsBegin = Names.size();
2435 do {
Pavel Labathba825192018-10-16 14:29:14 +00002436 Node *Binding = getDerived().parseSourceName(State);
Richard Smithc20d1442018-08-20 20:14:49 +00002437 if (Binding == nullptr)
2438 return nullptr;
2439 Names.push_back(Binding);
2440 } while (!consumeIf('E'));
2441 Result = make<StructuredBindingName>(popTrailingNodeArray(BindingsBegin));
2442 } else
Pavel Labathba825192018-10-16 14:29:14 +00002443 Result = getDerived().parseOperatorName(State);
Richard Smithc20d1442018-08-20 20:14:49 +00002444 if (Result != nullptr)
Pavel Labathba825192018-10-16 14:29:14 +00002445 Result = getDerived().parseAbiTags(Result);
Richard Smithc20d1442018-08-20 20:14:49 +00002446 return Result;
2447}
2448
2449// <unnamed-type-name> ::= Ut [<nonnegative number>] _
2450// ::= <closure-type-name>
2451//
2452// <closure-type-name> ::= Ul <lambda-sig> E [ <nonnegative number> ] _
2453//
2454// <lambda-sig> ::= <parameter type>+ # Parameter types or "v" if the lambda has no parameters
Pavel Labathba825192018-10-16 14:29:14 +00002455template <typename Derived, typename Alloc>
2456Node *
2457AbstractManglingParser<Derived, Alloc>::parseUnnamedTypeName(NameState *) {
Richard Smithc20d1442018-08-20 20:14:49 +00002458 if (consumeIf("Ut")) {
2459 StringView Count = parseNumber();
2460 if (!consumeIf('_'))
2461 return nullptr;
2462 return make<UnnamedTypeName>(Count);
2463 }
2464 if (consumeIf("Ul")) {
2465 NodeArray Params;
2466 SwapAndRestore<bool> SwapParams(ParsingLambdaParams, true);
2467 if (!consumeIf("vE")) {
2468 size_t ParamsBegin = Names.size();
2469 do {
Pavel Labathba825192018-10-16 14:29:14 +00002470 Node *P = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00002471 if (P == nullptr)
2472 return nullptr;
2473 Names.push_back(P);
2474 } while (!consumeIf('E'));
2475 Params = popTrailingNodeArray(ParamsBegin);
2476 }
2477 StringView Count = parseNumber();
2478 if (!consumeIf('_'))
2479 return nullptr;
2480 return make<ClosureTypeName>(Params, Count);
2481 }
Erik Pilkington974b6542019-01-17 21:37:51 +00002482 if (consumeIf("Ub")) {
2483 (void)parseNumber();
2484 if (!consumeIf('_'))
2485 return nullptr;
2486 return make<NameType>("'block-literal'");
2487 }
Richard Smithc20d1442018-08-20 20:14:49 +00002488 return nullptr;
2489}
2490
2491// <source-name> ::= <positive length number> <identifier>
Pavel Labathba825192018-10-16 14:29:14 +00002492template <typename Derived, typename Alloc>
2493Node *AbstractManglingParser<Derived, Alloc>::parseSourceName(NameState *) {
Richard Smithc20d1442018-08-20 20:14:49 +00002494 size_t Length = 0;
2495 if (parsePositiveInteger(&Length))
2496 return nullptr;
2497 if (numLeft() < Length || Length == 0)
2498 return nullptr;
2499 StringView Name(First, First + Length);
2500 First += Length;
2501 if (Name.startsWith("_GLOBAL__N"))
2502 return make<NameType>("(anonymous namespace)");
2503 return make<NameType>(Name);
2504}
2505
2506// <operator-name> ::= aa # &&
2507// ::= ad # & (unary)
2508// ::= an # &
2509// ::= aN # &=
2510// ::= aS # =
2511// ::= cl # ()
2512// ::= cm # ,
2513// ::= co # ~
2514// ::= cv <type> # (cast)
2515// ::= da # delete[]
2516// ::= de # * (unary)
2517// ::= dl # delete
2518// ::= dv # /
2519// ::= dV # /=
2520// ::= eo # ^
2521// ::= eO # ^=
2522// ::= eq # ==
2523// ::= ge # >=
2524// ::= gt # >
2525// ::= ix # []
2526// ::= le # <=
2527// ::= li <source-name> # operator ""
2528// ::= ls # <<
2529// ::= lS # <<=
2530// ::= lt # <
2531// ::= mi # -
2532// ::= mI # -=
2533// ::= ml # *
2534// ::= mL # *=
2535// ::= mm # -- (postfix in <expression> context)
2536// ::= na # new[]
2537// ::= ne # !=
2538// ::= ng # - (unary)
2539// ::= nt # !
2540// ::= nw # new
2541// ::= oo # ||
2542// ::= or # |
2543// ::= oR # |=
2544// ::= pm # ->*
2545// ::= pl # +
2546// ::= pL # +=
2547// ::= pp # ++ (postfix in <expression> context)
2548// ::= ps # + (unary)
2549// ::= pt # ->
2550// ::= qu # ?
2551// ::= rm # %
2552// ::= rM # %=
2553// ::= rs # >>
2554// ::= rS # >>=
2555// ::= ss # <=> C++2a
2556// ::= v <digit> <source-name> # vendor extended operator
Pavel Labathba825192018-10-16 14:29:14 +00002557template <typename Derived, typename Alloc>
2558Node *
2559AbstractManglingParser<Derived, Alloc>::parseOperatorName(NameState *State) {
Richard Smithc20d1442018-08-20 20:14:49 +00002560 switch (look()) {
2561 case 'a':
2562 switch (look(1)) {
2563 case 'a':
2564 First += 2;
2565 return make<NameType>("operator&&");
2566 case 'd':
2567 case 'n':
2568 First += 2;
2569 return make<NameType>("operator&");
2570 case 'N':
2571 First += 2;
2572 return make<NameType>("operator&=");
2573 case 'S':
2574 First += 2;
2575 return make<NameType>("operator=");
2576 }
2577 return nullptr;
2578 case 'c':
2579 switch (look(1)) {
2580 case 'l':
2581 First += 2;
2582 return make<NameType>("operator()");
2583 case 'm':
2584 First += 2;
2585 return make<NameType>("operator,");
2586 case 'o':
2587 First += 2;
2588 return make<NameType>("operator~");
2589 // ::= cv <type> # (cast)
2590 case 'v': {
2591 First += 2;
2592 SwapAndRestore<bool> SaveTemplate(TryToParseTemplateArgs, false);
2593 // If we're parsing an encoding, State != nullptr and the conversion
2594 // operators' <type> could have a <template-param> that refers to some
2595 // <template-arg>s further ahead in the mangled name.
2596 SwapAndRestore<bool> SavePermit(PermitForwardTemplateReferences,
2597 PermitForwardTemplateReferences ||
2598 State != nullptr);
Pavel Labathba825192018-10-16 14:29:14 +00002599 Node *Ty = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00002600 if (Ty == nullptr)
2601 return nullptr;
2602 if (State) State->CtorDtorConversion = true;
2603 return make<ConversionOperatorType>(Ty);
2604 }
2605 }
2606 return nullptr;
2607 case 'd':
2608 switch (look(1)) {
2609 case 'a':
2610 First += 2;
2611 return make<NameType>("operator delete[]");
2612 case 'e':
2613 First += 2;
2614 return make<NameType>("operator*");
2615 case 'l':
2616 First += 2;
2617 return make<NameType>("operator delete");
2618 case 'v':
2619 First += 2;
2620 return make<NameType>("operator/");
2621 case 'V':
2622 First += 2;
2623 return make<NameType>("operator/=");
2624 }
2625 return nullptr;
2626 case 'e':
2627 switch (look(1)) {
2628 case 'o':
2629 First += 2;
2630 return make<NameType>("operator^");
2631 case 'O':
2632 First += 2;
2633 return make<NameType>("operator^=");
2634 case 'q':
2635 First += 2;
2636 return make<NameType>("operator==");
2637 }
2638 return nullptr;
2639 case 'g':
2640 switch (look(1)) {
2641 case 'e':
2642 First += 2;
2643 return make<NameType>("operator>=");
2644 case 't':
2645 First += 2;
2646 return make<NameType>("operator>");
2647 }
2648 return nullptr;
2649 case 'i':
2650 if (look(1) == 'x') {
2651 First += 2;
2652 return make<NameType>("operator[]");
2653 }
2654 return nullptr;
2655 case 'l':
2656 switch (look(1)) {
2657 case 'e':
2658 First += 2;
2659 return make<NameType>("operator<=");
2660 // ::= li <source-name> # operator ""
2661 case 'i': {
2662 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00002663 Node *SN = getDerived().parseSourceName(State);
Richard Smithc20d1442018-08-20 20:14:49 +00002664 if (SN == nullptr)
2665 return nullptr;
2666 return make<LiteralOperator>(SN);
2667 }
2668 case 's':
2669 First += 2;
2670 return make<NameType>("operator<<");
2671 case 'S':
2672 First += 2;
2673 return make<NameType>("operator<<=");
2674 case 't':
2675 First += 2;
2676 return make<NameType>("operator<");
2677 }
2678 return nullptr;
2679 case 'm':
2680 switch (look(1)) {
2681 case 'i':
2682 First += 2;
2683 return make<NameType>("operator-");
2684 case 'I':
2685 First += 2;
2686 return make<NameType>("operator-=");
2687 case 'l':
2688 First += 2;
2689 return make<NameType>("operator*");
2690 case 'L':
2691 First += 2;
2692 return make<NameType>("operator*=");
2693 case 'm':
2694 First += 2;
2695 return make<NameType>("operator--");
2696 }
2697 return nullptr;
2698 case 'n':
2699 switch (look(1)) {
2700 case 'a':
2701 First += 2;
2702 return make<NameType>("operator new[]");
2703 case 'e':
2704 First += 2;
2705 return make<NameType>("operator!=");
2706 case 'g':
2707 First += 2;
2708 return make<NameType>("operator-");
2709 case 't':
2710 First += 2;
2711 return make<NameType>("operator!");
2712 case 'w':
2713 First += 2;
2714 return make<NameType>("operator new");
2715 }
2716 return nullptr;
2717 case 'o':
2718 switch (look(1)) {
2719 case 'o':
2720 First += 2;
2721 return make<NameType>("operator||");
2722 case 'r':
2723 First += 2;
2724 return make<NameType>("operator|");
2725 case 'R':
2726 First += 2;
2727 return make<NameType>("operator|=");
2728 }
2729 return nullptr;
2730 case 'p':
2731 switch (look(1)) {
2732 case 'm':
2733 First += 2;
2734 return make<NameType>("operator->*");
2735 case 'l':
2736 First += 2;
2737 return make<NameType>("operator+");
2738 case 'L':
2739 First += 2;
2740 return make<NameType>("operator+=");
2741 case 'p':
2742 First += 2;
2743 return make<NameType>("operator++");
2744 case 's':
2745 First += 2;
2746 return make<NameType>("operator+");
2747 case 't':
2748 First += 2;
2749 return make<NameType>("operator->");
2750 }
2751 return nullptr;
2752 case 'q':
2753 if (look(1) == 'u') {
2754 First += 2;
2755 return make<NameType>("operator?");
2756 }
2757 return nullptr;
2758 case 'r':
2759 switch (look(1)) {
2760 case 'm':
2761 First += 2;
2762 return make<NameType>("operator%");
2763 case 'M':
2764 First += 2;
2765 return make<NameType>("operator%=");
2766 case 's':
2767 First += 2;
2768 return make<NameType>("operator>>");
2769 case 'S':
2770 First += 2;
2771 return make<NameType>("operator>>=");
2772 }
2773 return nullptr;
2774 case 's':
2775 if (look(1) == 's') {
2776 First += 2;
2777 return make<NameType>("operator<=>");
2778 }
2779 return nullptr;
2780 // ::= v <digit> <source-name> # vendor extended operator
2781 case 'v':
2782 if (std::isdigit(look(1))) {
2783 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00002784 Node *SN = getDerived().parseSourceName(State);
Richard Smithc20d1442018-08-20 20:14:49 +00002785 if (SN == nullptr)
2786 return nullptr;
2787 return make<ConversionOperatorType>(SN);
2788 }
2789 return nullptr;
2790 }
2791 return nullptr;
2792}
2793
2794// <ctor-dtor-name> ::= C1 # complete object constructor
2795// ::= C2 # base object constructor
2796// ::= C3 # complete object allocating constructor
Nico Weber29294792019-04-03 23:14:33 +00002797// extension ::= C4 # gcc old-style "[unified]" constructor
2798// extension ::= C5 # the COMDAT used for ctors
Richard Smithc20d1442018-08-20 20:14:49 +00002799// ::= D0 # deleting destructor
2800// ::= D1 # complete object destructor
2801// ::= D2 # base object destructor
Nico Weber29294792019-04-03 23:14:33 +00002802// extension ::= D4 # gcc old-style "[unified]" destructor
2803// extension ::= D5 # the COMDAT used for dtors
Pavel Labathba825192018-10-16 14:29:14 +00002804template <typename Derived, typename Alloc>
2805Node *
2806AbstractManglingParser<Derived, Alloc>::parseCtorDtorName(Node *&SoFar,
2807 NameState *State) {
Richard Smithc20d1442018-08-20 20:14:49 +00002808 if (SoFar->getKind() == Node::KSpecialSubstitution) {
2809 auto SSK = static_cast<SpecialSubstitution *>(SoFar)->SSK;
2810 switch (SSK) {
2811 case SpecialSubKind::string:
2812 case SpecialSubKind::istream:
2813 case SpecialSubKind::ostream:
2814 case SpecialSubKind::iostream:
2815 SoFar = make<ExpandedSpecialSubstitution>(SSK);
Richard Smithb485b352018-08-24 23:30:26 +00002816 if (!SoFar)
2817 return nullptr;
Reid Klecknere76aabe2018-11-01 18:24:03 +00002818 break;
Richard Smithc20d1442018-08-20 20:14:49 +00002819 default:
2820 break;
2821 }
2822 }
2823
2824 if (consumeIf('C')) {
2825 bool IsInherited = consumeIf('I');
Nico Weber29294792019-04-03 23:14:33 +00002826 if (look() != '1' && look() != '2' && look() != '3' && look() != '4' &&
2827 look() != '5')
Richard Smithc20d1442018-08-20 20:14:49 +00002828 return nullptr;
Pavel Labathf4e67eb2018-10-10 08:39:16 +00002829 int Variant = look() - '0';
Richard Smithc20d1442018-08-20 20:14:49 +00002830 ++First;
2831 if (State) State->CtorDtorConversion = true;
2832 if (IsInherited) {
Pavel Labathba825192018-10-16 14:29:14 +00002833 if (getDerived().parseName(State) == nullptr)
Richard Smithc20d1442018-08-20 20:14:49 +00002834 return nullptr;
2835 }
Nico Weber29294792019-04-03 23:14:33 +00002836 return make<CtorDtorName>(SoFar, /*IsDtor=*/false, Variant);
Richard Smithc20d1442018-08-20 20:14:49 +00002837 }
2838
Nico Weber29294792019-04-03 23:14:33 +00002839 if (look() == 'D' && (look(1) == '0' || look(1) == '1' || look(1) == '2' ||
2840 look(1) == '4' || look(1) == '5')) {
Pavel Labathf4e67eb2018-10-10 08:39:16 +00002841 int Variant = look(1) - '0';
Richard Smithc20d1442018-08-20 20:14:49 +00002842 First += 2;
2843 if (State) State->CtorDtorConversion = true;
Nico Weber29294792019-04-03 23:14:33 +00002844 return make<CtorDtorName>(SoFar, /*IsDtor=*/true, Variant);
Richard Smithc20d1442018-08-20 20:14:49 +00002845 }
2846
2847 return nullptr;
2848}
2849
2850// <nested-name> ::= N [<CV-Qualifiers>] [<ref-qualifier>] <prefix> <unqualified-name> E
2851// ::= N [<CV-Qualifiers>] [<ref-qualifier>] <template-prefix> <template-args> E
2852//
2853// <prefix> ::= <prefix> <unqualified-name>
2854// ::= <template-prefix> <template-args>
2855// ::= <template-param>
2856// ::= <decltype>
2857// ::= # empty
2858// ::= <substitution>
2859// ::= <prefix> <data-member-prefix>
2860// extension ::= L
2861//
2862// <data-member-prefix> := <member source-name> [<template-args>] M
2863//
2864// <template-prefix> ::= <prefix> <template unqualified-name>
2865// ::= <template-param>
2866// ::= <substitution>
Pavel Labathba825192018-10-16 14:29:14 +00002867template <typename Derived, typename Alloc>
2868Node *
2869AbstractManglingParser<Derived, Alloc>::parseNestedName(NameState *State) {
Richard Smithc20d1442018-08-20 20:14:49 +00002870 if (!consumeIf('N'))
2871 return nullptr;
2872
2873 Qualifiers CVTmp = parseCVQualifiers();
2874 if (State) State->CVQualifiers = CVTmp;
2875
2876 if (consumeIf('O')) {
2877 if (State) State->ReferenceQualifier = FrefQualRValue;
2878 } else if (consumeIf('R')) {
2879 if (State) State->ReferenceQualifier = FrefQualLValue;
2880 } else
2881 if (State) State->ReferenceQualifier = FrefQualNone;
2882
2883 Node *SoFar = nullptr;
2884 auto PushComponent = [&](Node *Comp) {
Richard Smithb485b352018-08-24 23:30:26 +00002885 if (!Comp) return false;
Richard Smithc20d1442018-08-20 20:14:49 +00002886 if (SoFar) SoFar = make<NestedName>(SoFar, Comp);
2887 else SoFar = Comp;
2888 if (State) State->EndsWithTemplateArgs = false;
Richard Smithb485b352018-08-24 23:30:26 +00002889 return SoFar != nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00002890 };
2891
Richard Smithb485b352018-08-24 23:30:26 +00002892 if (consumeIf("St")) {
Richard Smithc20d1442018-08-20 20:14:49 +00002893 SoFar = make<NameType>("std");
Richard Smithb485b352018-08-24 23:30:26 +00002894 if (!SoFar)
2895 return nullptr;
2896 }
Richard Smithc20d1442018-08-20 20:14:49 +00002897
2898 while (!consumeIf('E')) {
2899 consumeIf('L'); // extension
2900
2901 // <data-member-prefix> := <member source-name> [<template-args>] M
2902 if (consumeIf('M')) {
2903 if (SoFar == nullptr)
2904 return nullptr;
2905 continue;
2906 }
2907
2908 // ::= <template-param>
2909 if (look() == 'T') {
Pavel Labathba825192018-10-16 14:29:14 +00002910 if (!PushComponent(getDerived().parseTemplateParam()))
Richard Smithc20d1442018-08-20 20:14:49 +00002911 return nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00002912 Subs.push_back(SoFar);
2913 continue;
2914 }
2915
2916 // ::= <template-prefix> <template-args>
2917 if (look() == 'I') {
Pavel Labathba825192018-10-16 14:29:14 +00002918 Node *TA = getDerived().parseTemplateArgs(State != nullptr);
Richard Smithc20d1442018-08-20 20:14:49 +00002919 if (TA == nullptr || SoFar == nullptr)
2920 return nullptr;
2921 SoFar = make<NameWithTemplateArgs>(SoFar, TA);
Richard Smithb485b352018-08-24 23:30:26 +00002922 if (!SoFar)
2923 return nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00002924 if (State) State->EndsWithTemplateArgs = true;
2925 Subs.push_back(SoFar);
2926 continue;
2927 }
2928
2929 // ::= <decltype>
2930 if (look() == 'D' && (look(1) == 't' || look(1) == 'T')) {
Pavel Labathba825192018-10-16 14:29:14 +00002931 if (!PushComponent(getDerived().parseDecltype()))
Richard Smithc20d1442018-08-20 20:14:49 +00002932 return nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00002933 Subs.push_back(SoFar);
2934 continue;
2935 }
2936
2937 // ::= <substitution>
2938 if (look() == 'S' && look(1) != 't') {
Pavel Labathba825192018-10-16 14:29:14 +00002939 Node *S = getDerived().parseSubstitution();
Richard Smithb485b352018-08-24 23:30:26 +00002940 if (!PushComponent(S))
Richard Smithc20d1442018-08-20 20:14:49 +00002941 return nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00002942 if (SoFar != S)
2943 Subs.push_back(S);
2944 continue;
2945 }
2946
2947 // Parse an <unqualified-name> thats actually a <ctor-dtor-name>.
2948 if (look() == 'C' || (look() == 'D' && look(1) != 'C')) {
2949 if (SoFar == nullptr)
2950 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00002951 if (!PushComponent(getDerived().parseCtorDtorName(SoFar, State)))
Richard Smithc20d1442018-08-20 20:14:49 +00002952 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00002953 SoFar = getDerived().parseAbiTags(SoFar);
Richard Smithc20d1442018-08-20 20:14:49 +00002954 if (SoFar == nullptr)
2955 return nullptr;
2956 Subs.push_back(SoFar);
2957 continue;
2958 }
2959
2960 // ::= <prefix> <unqualified-name>
Pavel Labathba825192018-10-16 14:29:14 +00002961 if (!PushComponent(getDerived().parseUnqualifiedName(State)))
Richard Smithc20d1442018-08-20 20:14:49 +00002962 return nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00002963 Subs.push_back(SoFar);
2964 }
2965
2966 if (SoFar == nullptr || Subs.empty())
2967 return nullptr;
2968
2969 Subs.pop_back();
2970 return SoFar;
2971}
2972
2973// <simple-id> ::= <source-name> [ <template-args> ]
Pavel Labathba825192018-10-16 14:29:14 +00002974template <typename Derived, typename Alloc>
2975Node *AbstractManglingParser<Derived, Alloc>::parseSimpleId() {
2976 Node *SN = getDerived().parseSourceName(/*NameState=*/nullptr);
Richard Smithc20d1442018-08-20 20:14:49 +00002977 if (SN == nullptr)
2978 return nullptr;
2979 if (look() == 'I') {
Pavel Labathba825192018-10-16 14:29:14 +00002980 Node *TA = getDerived().parseTemplateArgs();
Richard Smithc20d1442018-08-20 20:14:49 +00002981 if (TA == nullptr)
2982 return nullptr;
2983 return make<NameWithTemplateArgs>(SN, TA);
2984 }
2985 return SN;
2986}
2987
2988// <destructor-name> ::= <unresolved-type> # e.g., ~T or ~decltype(f())
2989// ::= <simple-id> # e.g., ~A<2*N>
Pavel Labathba825192018-10-16 14:29:14 +00002990template <typename Derived, typename Alloc>
2991Node *AbstractManglingParser<Derived, Alloc>::parseDestructorName() {
Richard Smithc20d1442018-08-20 20:14:49 +00002992 Node *Result;
2993 if (std::isdigit(look()))
Pavel Labathba825192018-10-16 14:29:14 +00002994 Result = getDerived().parseSimpleId();
Richard Smithc20d1442018-08-20 20:14:49 +00002995 else
Pavel Labathba825192018-10-16 14:29:14 +00002996 Result = getDerived().parseUnresolvedType();
Richard Smithc20d1442018-08-20 20:14:49 +00002997 if (Result == nullptr)
2998 return nullptr;
2999 return make<DtorName>(Result);
3000}
3001
3002// <unresolved-type> ::= <template-param>
3003// ::= <decltype>
3004// ::= <substitution>
Pavel Labathba825192018-10-16 14:29:14 +00003005template <typename Derived, typename Alloc>
3006Node *AbstractManglingParser<Derived, Alloc>::parseUnresolvedType() {
Richard Smithc20d1442018-08-20 20:14:49 +00003007 if (look() == 'T') {
Pavel Labathba825192018-10-16 14:29:14 +00003008 Node *TP = getDerived().parseTemplateParam();
Richard Smithc20d1442018-08-20 20:14:49 +00003009 if (TP == nullptr)
3010 return nullptr;
3011 Subs.push_back(TP);
3012 return TP;
3013 }
3014 if (look() == 'D') {
Pavel Labathba825192018-10-16 14:29:14 +00003015 Node *DT = getDerived().parseDecltype();
Richard Smithc20d1442018-08-20 20:14:49 +00003016 if (DT == nullptr)
3017 return nullptr;
3018 Subs.push_back(DT);
3019 return DT;
3020 }
Pavel Labathba825192018-10-16 14:29:14 +00003021 return getDerived().parseSubstitution();
Richard Smithc20d1442018-08-20 20:14:49 +00003022}
3023
3024// <base-unresolved-name> ::= <simple-id> # unresolved name
3025// extension ::= <operator-name> # unresolved operator-function-id
3026// extension ::= <operator-name> <template-args> # unresolved operator template-id
3027// ::= on <operator-name> # unresolved operator-function-id
3028// ::= on <operator-name> <template-args> # unresolved operator template-id
3029// ::= dn <destructor-name> # destructor or pseudo-destructor;
3030// # e.g. ~X or ~X<N-1>
Pavel Labathba825192018-10-16 14:29:14 +00003031template <typename Derived, typename Alloc>
3032Node *AbstractManglingParser<Derived, Alloc>::parseBaseUnresolvedName() {
Richard Smithc20d1442018-08-20 20:14:49 +00003033 if (std::isdigit(look()))
Pavel Labathba825192018-10-16 14:29:14 +00003034 return getDerived().parseSimpleId();
Richard Smithc20d1442018-08-20 20:14:49 +00003035
3036 if (consumeIf("dn"))
Pavel Labathba825192018-10-16 14:29:14 +00003037 return getDerived().parseDestructorName();
Richard Smithc20d1442018-08-20 20:14:49 +00003038
3039 consumeIf("on");
3040
Pavel Labathba825192018-10-16 14:29:14 +00003041 Node *Oper = getDerived().parseOperatorName(/*NameState=*/nullptr);
Richard Smithc20d1442018-08-20 20:14:49 +00003042 if (Oper == nullptr)
3043 return nullptr;
3044 if (look() == 'I') {
Pavel Labathba825192018-10-16 14:29:14 +00003045 Node *TA = getDerived().parseTemplateArgs();
Richard Smithc20d1442018-08-20 20:14:49 +00003046 if (TA == nullptr)
3047 return nullptr;
3048 return make<NameWithTemplateArgs>(Oper, TA);
3049 }
3050 return Oper;
3051}
3052
3053// <unresolved-name>
3054// extension ::= srN <unresolved-type> [<template-args>] <unresolved-qualifier-level>* E <base-unresolved-name>
3055// ::= [gs] <base-unresolved-name> # x or (with "gs") ::x
3056// ::= [gs] sr <unresolved-qualifier-level>+ E <base-unresolved-name>
3057// # A::x, N::y, A<T>::z; "gs" means leading "::"
3058// ::= sr <unresolved-type> <base-unresolved-name> # T::x / decltype(p)::x
3059// extension ::= sr <unresolved-type> <template-args> <base-unresolved-name>
3060// # T::N::x /decltype(p)::N::x
3061// (ignored) ::= srN <unresolved-type> <unresolved-qualifier-level>+ E <base-unresolved-name>
3062//
3063// <unresolved-qualifier-level> ::= <simple-id>
Pavel Labathba825192018-10-16 14:29:14 +00003064template <typename Derived, typename Alloc>
3065Node *AbstractManglingParser<Derived, Alloc>::parseUnresolvedName() {
Richard Smithc20d1442018-08-20 20:14:49 +00003066 Node *SoFar = nullptr;
3067
3068 // srN <unresolved-type> [<template-args>] <unresolved-qualifier-level>* E <base-unresolved-name>
3069 // srN <unresolved-type> <unresolved-qualifier-level>+ E <base-unresolved-name>
3070 if (consumeIf("srN")) {
Pavel Labathba825192018-10-16 14:29:14 +00003071 SoFar = getDerived().parseUnresolvedType();
Richard Smithc20d1442018-08-20 20:14:49 +00003072 if (SoFar == nullptr)
3073 return nullptr;
3074
3075 if (look() == 'I') {
Pavel Labathba825192018-10-16 14:29:14 +00003076 Node *TA = getDerived().parseTemplateArgs();
Richard Smithc20d1442018-08-20 20:14:49 +00003077 if (TA == nullptr)
3078 return nullptr;
3079 SoFar = make<NameWithTemplateArgs>(SoFar, TA);
Richard Smithb485b352018-08-24 23:30:26 +00003080 if (!SoFar)
3081 return nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00003082 }
3083
3084 while (!consumeIf('E')) {
Pavel Labathba825192018-10-16 14:29:14 +00003085 Node *Qual = getDerived().parseSimpleId();
Richard Smithc20d1442018-08-20 20:14:49 +00003086 if (Qual == nullptr)
3087 return nullptr;
3088 SoFar = make<QualifiedName>(SoFar, Qual);
Richard Smithb485b352018-08-24 23:30:26 +00003089 if (!SoFar)
3090 return nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00003091 }
3092
Pavel Labathba825192018-10-16 14:29:14 +00003093 Node *Base = getDerived().parseBaseUnresolvedName();
Richard Smithc20d1442018-08-20 20:14:49 +00003094 if (Base == nullptr)
3095 return nullptr;
3096 return make<QualifiedName>(SoFar, Base);
3097 }
3098
3099 bool Global = consumeIf("gs");
3100
3101 // [gs] <base-unresolved-name> # x or (with "gs") ::x
3102 if (!consumeIf("sr")) {
Pavel Labathba825192018-10-16 14:29:14 +00003103 SoFar = getDerived().parseBaseUnresolvedName();
Richard Smithc20d1442018-08-20 20:14:49 +00003104 if (SoFar == nullptr)
3105 return nullptr;
3106 if (Global)
3107 SoFar = make<GlobalQualifiedName>(SoFar);
3108 return SoFar;
3109 }
3110
3111 // [gs] sr <unresolved-qualifier-level>+ E <base-unresolved-name>
3112 if (std::isdigit(look())) {
3113 do {
Pavel Labathba825192018-10-16 14:29:14 +00003114 Node *Qual = getDerived().parseSimpleId();
Richard Smithc20d1442018-08-20 20:14:49 +00003115 if (Qual == nullptr)
3116 return nullptr;
3117 if (SoFar)
3118 SoFar = make<QualifiedName>(SoFar, Qual);
3119 else if (Global)
3120 SoFar = make<GlobalQualifiedName>(Qual);
3121 else
3122 SoFar = Qual;
Richard Smithb485b352018-08-24 23:30:26 +00003123 if (!SoFar)
3124 return nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00003125 } while (!consumeIf('E'));
3126 }
3127 // sr <unresolved-type> <base-unresolved-name>
3128 // sr <unresolved-type> <template-args> <base-unresolved-name>
3129 else {
Pavel Labathba825192018-10-16 14:29:14 +00003130 SoFar = getDerived().parseUnresolvedType();
Richard Smithc20d1442018-08-20 20:14:49 +00003131 if (SoFar == nullptr)
3132 return nullptr;
3133
3134 if (look() == 'I') {
Pavel Labathba825192018-10-16 14:29:14 +00003135 Node *TA = getDerived().parseTemplateArgs();
Richard Smithc20d1442018-08-20 20:14:49 +00003136 if (TA == nullptr)
3137 return nullptr;
3138 SoFar = make<NameWithTemplateArgs>(SoFar, TA);
Richard Smithb485b352018-08-24 23:30:26 +00003139 if (!SoFar)
3140 return nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00003141 }
3142 }
3143
3144 assert(SoFar != nullptr);
3145
Pavel Labathba825192018-10-16 14:29:14 +00003146 Node *Base = getDerived().parseBaseUnresolvedName();
Richard Smithc20d1442018-08-20 20:14:49 +00003147 if (Base == nullptr)
3148 return nullptr;
3149 return make<QualifiedName>(SoFar, Base);
3150}
3151
3152// <abi-tags> ::= <abi-tag> [<abi-tags>]
3153// <abi-tag> ::= B <source-name>
Pavel Labathba825192018-10-16 14:29:14 +00003154template <typename Derived, typename Alloc>
3155Node *AbstractManglingParser<Derived, Alloc>::parseAbiTags(Node *N) {
Richard Smithc20d1442018-08-20 20:14:49 +00003156 while (consumeIf('B')) {
3157 StringView SN = parseBareSourceName();
3158 if (SN.empty())
3159 return nullptr;
3160 N = make<AbiTagAttr>(N, SN);
Richard Smithb485b352018-08-24 23:30:26 +00003161 if (!N)
3162 return nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00003163 }
3164 return N;
3165}
3166
3167// <number> ::= [n] <non-negative decimal integer>
Pavel Labathba825192018-10-16 14:29:14 +00003168template <typename Alloc, typename Derived>
3169StringView
3170AbstractManglingParser<Alloc, Derived>::parseNumber(bool AllowNegative) {
Richard Smithc20d1442018-08-20 20:14:49 +00003171 const char *Tmp = First;
3172 if (AllowNegative)
3173 consumeIf('n');
3174 if (numLeft() == 0 || !std::isdigit(*First))
3175 return StringView();
3176 while (numLeft() != 0 && std::isdigit(*First))
3177 ++First;
3178 return StringView(Tmp, First);
3179}
3180
3181// <positive length number> ::= [0-9]*
Pavel Labathba825192018-10-16 14:29:14 +00003182template <typename Alloc, typename Derived>
3183bool AbstractManglingParser<Alloc, Derived>::parsePositiveInteger(size_t *Out) {
Richard Smithc20d1442018-08-20 20:14:49 +00003184 *Out = 0;
3185 if (look() < '0' || look() > '9')
3186 return true;
3187 while (look() >= '0' && look() <= '9') {
3188 *Out *= 10;
3189 *Out += static_cast<size_t>(consume() - '0');
3190 }
3191 return false;
3192}
3193
Pavel Labathba825192018-10-16 14:29:14 +00003194template <typename Alloc, typename Derived>
3195StringView AbstractManglingParser<Alloc, Derived>::parseBareSourceName() {
Richard Smithc20d1442018-08-20 20:14:49 +00003196 size_t Int = 0;
3197 if (parsePositiveInteger(&Int) || numLeft() < Int)
3198 return StringView();
3199 StringView R(First, First + Int);
3200 First += Int;
3201 return R;
3202}
3203
3204// <function-type> ::= [<CV-qualifiers>] [<exception-spec>] [Dx] F [Y] <bare-function-type> [<ref-qualifier>] E
3205//
3206// <exception-spec> ::= Do # non-throwing exception-specification (e.g., noexcept, throw())
3207// ::= DO <expression> E # computed (instantiation-dependent) noexcept
3208// ::= Dw <type>+ E # dynamic exception specification with instantiation-dependent types
3209//
3210// <ref-qualifier> ::= R # & ref-qualifier
3211// <ref-qualifier> ::= O # && ref-qualifier
Pavel Labathba825192018-10-16 14:29:14 +00003212template <typename Derived, typename Alloc>
3213Node *AbstractManglingParser<Derived, Alloc>::parseFunctionType() {
Richard Smithc20d1442018-08-20 20:14:49 +00003214 Qualifiers CVQuals = parseCVQualifiers();
3215
3216 Node *ExceptionSpec = nullptr;
3217 if (consumeIf("Do")) {
3218 ExceptionSpec = make<NameType>("noexcept");
Richard Smithb485b352018-08-24 23:30:26 +00003219 if (!ExceptionSpec)
3220 return nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00003221 } else if (consumeIf("DO")) {
Pavel Labathba825192018-10-16 14:29:14 +00003222 Node *E = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00003223 if (E == nullptr || !consumeIf('E'))
3224 return nullptr;
3225 ExceptionSpec = make<NoexceptSpec>(E);
Richard Smithb485b352018-08-24 23:30:26 +00003226 if (!ExceptionSpec)
3227 return nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00003228 } else if (consumeIf("Dw")) {
3229 size_t SpecsBegin = Names.size();
3230 while (!consumeIf('E')) {
Pavel Labathba825192018-10-16 14:29:14 +00003231 Node *T = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00003232 if (T == nullptr)
3233 return nullptr;
3234 Names.push_back(T);
3235 }
3236 ExceptionSpec =
3237 make<DynamicExceptionSpec>(popTrailingNodeArray(SpecsBegin));
Richard Smithb485b352018-08-24 23:30:26 +00003238 if (!ExceptionSpec)
3239 return nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00003240 }
3241
3242 consumeIf("Dx"); // transaction safe
3243
3244 if (!consumeIf('F'))
3245 return nullptr;
3246 consumeIf('Y'); // extern "C"
Pavel Labathba825192018-10-16 14:29:14 +00003247 Node *ReturnType = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00003248 if (ReturnType == nullptr)
3249 return nullptr;
3250
3251 FunctionRefQual ReferenceQualifier = FrefQualNone;
3252 size_t ParamsBegin = Names.size();
3253 while (true) {
3254 if (consumeIf('E'))
3255 break;
3256 if (consumeIf('v'))
3257 continue;
3258 if (consumeIf("RE")) {
3259 ReferenceQualifier = FrefQualLValue;
3260 break;
3261 }
3262 if (consumeIf("OE")) {
3263 ReferenceQualifier = FrefQualRValue;
3264 break;
3265 }
Pavel Labathba825192018-10-16 14:29:14 +00003266 Node *T = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00003267 if (T == nullptr)
3268 return nullptr;
3269 Names.push_back(T);
3270 }
3271
3272 NodeArray Params = popTrailingNodeArray(ParamsBegin);
3273 return make<FunctionType>(ReturnType, Params, CVQuals,
3274 ReferenceQualifier, ExceptionSpec);
3275}
3276
3277// extension:
3278// <vector-type> ::= Dv <positive dimension number> _ <extended element type>
3279// ::= Dv [<dimension expression>] _ <element type>
3280// <extended element type> ::= <element type>
3281// ::= p # AltiVec vector pixel
Pavel Labathba825192018-10-16 14:29:14 +00003282template <typename Derived, typename Alloc>
3283Node *AbstractManglingParser<Derived, Alloc>::parseVectorType() {
Richard Smithc20d1442018-08-20 20:14:49 +00003284 if (!consumeIf("Dv"))
3285 return nullptr;
3286 if (look() >= '1' && look() <= '9') {
3287 StringView DimensionNumber = parseNumber();
3288 if (!consumeIf('_'))
3289 return nullptr;
3290 if (consumeIf('p'))
3291 return make<PixelVectorType>(DimensionNumber);
Pavel Labathba825192018-10-16 14:29:14 +00003292 Node *ElemType = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00003293 if (ElemType == nullptr)
3294 return nullptr;
3295 return make<VectorType>(ElemType, DimensionNumber);
3296 }
3297
3298 if (!consumeIf('_')) {
Pavel Labathba825192018-10-16 14:29:14 +00003299 Node *DimExpr = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00003300 if (!DimExpr)
3301 return nullptr;
3302 if (!consumeIf('_'))
3303 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00003304 Node *ElemType = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00003305 if (!ElemType)
3306 return nullptr;
3307 return make<VectorType>(ElemType, DimExpr);
3308 }
Pavel Labathba825192018-10-16 14:29:14 +00003309 Node *ElemType = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00003310 if (!ElemType)
3311 return nullptr;
3312 return make<VectorType>(ElemType, StringView());
3313}
3314
3315// <decltype> ::= Dt <expression> E # decltype of an id-expression or class member access (C++0x)
3316// ::= DT <expression> E # decltype of an expression (C++0x)
Pavel Labathba825192018-10-16 14:29:14 +00003317template <typename Derived, typename Alloc>
3318Node *AbstractManglingParser<Derived, Alloc>::parseDecltype() {
Richard Smithc20d1442018-08-20 20:14:49 +00003319 if (!consumeIf('D'))
3320 return nullptr;
3321 if (!consumeIf('t') && !consumeIf('T'))
3322 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00003323 Node *E = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00003324 if (E == nullptr)
3325 return nullptr;
3326 if (!consumeIf('E'))
3327 return nullptr;
3328 return make<EnclosingExpr>("decltype(", E, ")");
3329}
3330
3331// <array-type> ::= A <positive dimension number> _ <element type>
3332// ::= A [<dimension expression>] _ <element type>
Pavel Labathba825192018-10-16 14:29:14 +00003333template <typename Derived, typename Alloc>
3334Node *AbstractManglingParser<Derived, Alloc>::parseArrayType() {
Richard Smithc20d1442018-08-20 20:14:49 +00003335 if (!consumeIf('A'))
3336 return nullptr;
3337
Pavel Labathf4e67eb2018-10-10 08:39:16 +00003338 NodeOrString Dimension;
3339
Richard Smithc20d1442018-08-20 20:14:49 +00003340 if (std::isdigit(look())) {
Pavel Labathf4e67eb2018-10-10 08:39:16 +00003341 Dimension = parseNumber();
Richard Smithc20d1442018-08-20 20:14:49 +00003342 if (!consumeIf('_'))
3343 return nullptr;
Pavel Labathf4e67eb2018-10-10 08:39:16 +00003344 } else if (!consumeIf('_')) {
Pavel Labathba825192018-10-16 14:29:14 +00003345 Node *DimExpr = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00003346 if (DimExpr == nullptr)
3347 return nullptr;
3348 if (!consumeIf('_'))
3349 return nullptr;
Pavel Labathf4e67eb2018-10-10 08:39:16 +00003350 Dimension = DimExpr;
Richard Smithc20d1442018-08-20 20:14:49 +00003351 }
3352
Pavel Labathba825192018-10-16 14:29:14 +00003353 Node *Ty = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00003354 if (Ty == nullptr)
3355 return nullptr;
Pavel Labathf4e67eb2018-10-10 08:39:16 +00003356 return make<ArrayType>(Ty, Dimension);
Richard Smithc20d1442018-08-20 20:14:49 +00003357}
3358
3359// <pointer-to-member-type> ::= M <class type> <member type>
Pavel Labathba825192018-10-16 14:29:14 +00003360template <typename Derived, typename Alloc>
3361Node *AbstractManglingParser<Derived, Alloc>::parsePointerToMemberType() {
Richard Smithc20d1442018-08-20 20:14:49 +00003362 if (!consumeIf('M'))
3363 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00003364 Node *ClassType = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00003365 if (ClassType == nullptr)
3366 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00003367 Node *MemberType = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00003368 if (MemberType == nullptr)
3369 return nullptr;
3370 return make<PointerToMemberType>(ClassType, MemberType);
3371}
3372
3373// <class-enum-type> ::= <name> # non-dependent type name, dependent type name, or dependent typename-specifier
3374// ::= Ts <name> # dependent elaborated type specifier using 'struct' or 'class'
3375// ::= Tu <name> # dependent elaborated type specifier using 'union'
3376// ::= Te <name> # dependent elaborated type specifier using 'enum'
Pavel Labathba825192018-10-16 14:29:14 +00003377template <typename Derived, typename Alloc>
3378Node *AbstractManglingParser<Derived, Alloc>::parseClassEnumType() {
Richard Smithc20d1442018-08-20 20:14:49 +00003379 StringView ElabSpef;
3380 if (consumeIf("Ts"))
3381 ElabSpef = "struct";
3382 else if (consumeIf("Tu"))
3383 ElabSpef = "union";
3384 else if (consumeIf("Te"))
3385 ElabSpef = "enum";
3386
Pavel Labathba825192018-10-16 14:29:14 +00003387 Node *Name = getDerived().parseName();
Richard Smithc20d1442018-08-20 20:14:49 +00003388 if (Name == nullptr)
3389 return nullptr;
3390
3391 if (!ElabSpef.empty())
3392 return make<ElaboratedTypeSpefType>(ElabSpef, Name);
3393
3394 return Name;
3395}
3396
3397// <qualified-type> ::= <qualifiers> <type>
3398// <qualifiers> ::= <extended-qualifier>* <CV-qualifiers>
3399// <extended-qualifier> ::= U <source-name> [<template-args>] # vendor extended type qualifier
Pavel Labathba825192018-10-16 14:29:14 +00003400template <typename Derived, typename Alloc>
3401Node *AbstractManglingParser<Derived, Alloc>::parseQualifiedType() {
Richard Smithc20d1442018-08-20 20:14:49 +00003402 if (consumeIf('U')) {
3403 StringView Qual = parseBareSourceName();
3404 if (Qual.empty())
3405 return nullptr;
3406
3407 // FIXME parse the optional <template-args> here!
3408
3409 // extension ::= U <objc-name> <objc-type> # objc-type<identifier>
3410 if (Qual.startsWith("objcproto")) {
3411 StringView ProtoSourceName = Qual.dropFront(std::strlen("objcproto"));
3412 StringView Proto;
3413 {
3414 SwapAndRestore<const char *> SaveFirst(First, ProtoSourceName.begin()),
3415 SaveLast(Last, ProtoSourceName.end());
3416 Proto = parseBareSourceName();
3417 }
3418 if (Proto.empty())
3419 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00003420 Node *Child = getDerived().parseQualifiedType();
Richard Smithc20d1442018-08-20 20:14:49 +00003421 if (Child == nullptr)
3422 return nullptr;
3423 return make<ObjCProtoName>(Child, Proto);
3424 }
3425
Pavel Labathba825192018-10-16 14:29:14 +00003426 Node *Child = getDerived().parseQualifiedType();
Richard Smithc20d1442018-08-20 20:14:49 +00003427 if (Child == nullptr)
3428 return nullptr;
3429 return make<VendorExtQualType>(Child, Qual);
3430 }
3431
3432 Qualifiers Quals = parseCVQualifiers();
Pavel Labathba825192018-10-16 14:29:14 +00003433 Node *Ty = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00003434 if (Ty == nullptr)
3435 return nullptr;
3436 if (Quals != QualNone)
3437 Ty = make<QualType>(Ty, Quals);
3438 return Ty;
3439}
3440
3441// <type> ::= <builtin-type>
3442// ::= <qualified-type>
3443// ::= <function-type>
3444// ::= <class-enum-type>
3445// ::= <array-type>
3446// ::= <pointer-to-member-type>
3447// ::= <template-param>
3448// ::= <template-template-param> <template-args>
3449// ::= <decltype>
3450// ::= P <type> # pointer
3451// ::= R <type> # l-value reference
3452// ::= O <type> # r-value reference (C++11)
3453// ::= C <type> # complex pair (C99)
3454// ::= G <type> # imaginary (C99)
3455// ::= <substitution> # See Compression below
3456// extension ::= U <objc-name> <objc-type> # objc-type<identifier>
3457// extension ::= <vector-type> # <vector-type> starts with Dv
3458//
3459// <objc-name> ::= <k0 number> objcproto <k1 number> <identifier> # k0 = 9 + <number of digits in k1> + k1
3460// <objc-type> ::= <source-name> # PU<11+>objcproto 11objc_object<source-name> 11objc_object -> id<source-name>
Pavel Labathba825192018-10-16 14:29:14 +00003461template <typename Derived, typename Alloc>
3462Node *AbstractManglingParser<Derived, Alloc>::parseType() {
Richard Smithc20d1442018-08-20 20:14:49 +00003463 Node *Result = nullptr;
3464
Richard Smithc20d1442018-08-20 20:14:49 +00003465 switch (look()) {
3466 // ::= <qualified-type>
3467 case 'r':
3468 case 'V':
3469 case 'K': {
3470 unsigned AfterQuals = 0;
3471 if (look(AfterQuals) == 'r') ++AfterQuals;
3472 if (look(AfterQuals) == 'V') ++AfterQuals;
3473 if (look(AfterQuals) == 'K') ++AfterQuals;
3474
3475 if (look(AfterQuals) == 'F' ||
3476 (look(AfterQuals) == 'D' &&
3477 (look(AfterQuals + 1) == 'o' || look(AfterQuals + 1) == 'O' ||
3478 look(AfterQuals + 1) == 'w' || look(AfterQuals + 1) == 'x'))) {
Pavel Labathba825192018-10-16 14:29:14 +00003479 Result = getDerived().parseFunctionType();
Richard Smithc20d1442018-08-20 20:14:49 +00003480 break;
3481 }
Erik Pilkingtonf70e4d82019-01-17 20:37:51 +00003482 DEMANGLE_FALLTHROUGH;
Richard Smithc20d1442018-08-20 20:14:49 +00003483 }
3484 case 'U': {
Pavel Labathba825192018-10-16 14:29:14 +00003485 Result = getDerived().parseQualifiedType();
Richard Smithc20d1442018-08-20 20:14:49 +00003486 break;
3487 }
3488 // <builtin-type> ::= v # void
3489 case 'v':
3490 ++First;
3491 return make<NameType>("void");
3492 // ::= w # wchar_t
3493 case 'w':
3494 ++First;
3495 return make<NameType>("wchar_t");
3496 // ::= b # bool
3497 case 'b':
3498 ++First;
3499 return make<NameType>("bool");
3500 // ::= c # char
3501 case 'c':
3502 ++First;
3503 return make<NameType>("char");
3504 // ::= a # signed char
3505 case 'a':
3506 ++First;
3507 return make<NameType>("signed char");
3508 // ::= h # unsigned char
3509 case 'h':
3510 ++First;
3511 return make<NameType>("unsigned char");
3512 // ::= s # short
3513 case 's':
3514 ++First;
3515 return make<NameType>("short");
3516 // ::= t # unsigned short
3517 case 't':
3518 ++First;
3519 return make<NameType>("unsigned short");
3520 // ::= i # int
3521 case 'i':
3522 ++First;
3523 return make<NameType>("int");
3524 // ::= j # unsigned int
3525 case 'j':
3526 ++First;
3527 return make<NameType>("unsigned int");
3528 // ::= l # long
3529 case 'l':
3530 ++First;
3531 return make<NameType>("long");
3532 // ::= m # unsigned long
3533 case 'm':
3534 ++First;
3535 return make<NameType>("unsigned long");
3536 // ::= x # long long, __int64
3537 case 'x':
3538 ++First;
3539 return make<NameType>("long long");
3540 // ::= y # unsigned long long, __int64
3541 case 'y':
3542 ++First;
3543 return make<NameType>("unsigned long long");
3544 // ::= n # __int128
3545 case 'n':
3546 ++First;
3547 return make<NameType>("__int128");
3548 // ::= o # unsigned __int128
3549 case 'o':
3550 ++First;
3551 return make<NameType>("unsigned __int128");
3552 // ::= f # float
3553 case 'f':
3554 ++First;
3555 return make<NameType>("float");
3556 // ::= d # double
3557 case 'd':
3558 ++First;
3559 return make<NameType>("double");
3560 // ::= e # long double, __float80
3561 case 'e':
3562 ++First;
3563 return make<NameType>("long double");
3564 // ::= g # __float128
3565 case 'g':
3566 ++First;
3567 return make<NameType>("__float128");
3568 // ::= z # ellipsis
3569 case 'z':
3570 ++First;
3571 return make<NameType>("...");
3572
3573 // <builtin-type> ::= u <source-name> # vendor extended type
3574 case 'u': {
3575 ++First;
3576 StringView Res = parseBareSourceName();
3577 if (Res.empty())
3578 return nullptr;
Erik Pilkingtonb94a1f42019-06-10 21:02:39 +00003579 // Typically, <builtin-type>s are not considered substitution candidates,
3580 // but the exception to that exception is vendor extended types (Itanium C++
3581 // ABI 5.9.1).
3582 Result = make<NameType>(Res);
3583 break;
Richard Smithc20d1442018-08-20 20:14:49 +00003584 }
3585 case 'D':
3586 switch (look(1)) {
3587 // ::= Dd # IEEE 754r decimal floating point (64 bits)
3588 case 'd':
3589 First += 2;
3590 return make<NameType>("decimal64");
3591 // ::= De # IEEE 754r decimal floating point (128 bits)
3592 case 'e':
3593 First += 2;
3594 return make<NameType>("decimal128");
3595 // ::= Df # IEEE 754r decimal floating point (32 bits)
3596 case 'f':
3597 First += 2;
3598 return make<NameType>("decimal32");
3599 // ::= Dh # IEEE 754r half-precision floating point (16 bits)
3600 case 'h':
3601 First += 2;
3602 return make<NameType>("decimal16");
3603 // ::= Di # char32_t
3604 case 'i':
3605 First += 2;
3606 return make<NameType>("char32_t");
3607 // ::= Ds # char16_t
3608 case 's':
3609 First += 2;
3610 return make<NameType>("char16_t");
3611 // ::= Da # auto (in dependent new-expressions)
3612 case 'a':
3613 First += 2;
3614 return make<NameType>("auto");
3615 // ::= Dc # decltype(auto)
3616 case 'c':
3617 First += 2;
3618 return make<NameType>("decltype(auto)");
3619 // ::= Dn # std::nullptr_t (i.e., decltype(nullptr))
3620 case 'n':
3621 First += 2;
3622 return make<NameType>("std::nullptr_t");
3623
3624 // ::= <decltype>
3625 case 't':
3626 case 'T': {
Pavel Labathba825192018-10-16 14:29:14 +00003627 Result = getDerived().parseDecltype();
Richard Smithc20d1442018-08-20 20:14:49 +00003628 break;
3629 }
3630 // extension ::= <vector-type> # <vector-type> starts with Dv
3631 case 'v': {
Pavel Labathba825192018-10-16 14:29:14 +00003632 Result = getDerived().parseVectorType();
Richard Smithc20d1442018-08-20 20:14:49 +00003633 break;
3634 }
3635 // ::= Dp <type> # pack expansion (C++0x)
3636 case 'p': {
3637 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00003638 Node *Child = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00003639 if (!Child)
3640 return nullptr;
3641 Result = make<ParameterPackExpansion>(Child);
3642 break;
3643 }
3644 // Exception specifier on a function type.
3645 case 'o':
3646 case 'O':
3647 case 'w':
3648 // Transaction safe function type.
3649 case 'x':
Pavel Labathba825192018-10-16 14:29:14 +00003650 Result = getDerived().parseFunctionType();
Richard Smithc20d1442018-08-20 20:14:49 +00003651 break;
3652 }
3653 break;
3654 // ::= <function-type>
3655 case 'F': {
Pavel Labathba825192018-10-16 14:29:14 +00003656 Result = getDerived().parseFunctionType();
Richard Smithc20d1442018-08-20 20:14:49 +00003657 break;
3658 }
3659 // ::= <array-type>
3660 case 'A': {
Pavel Labathba825192018-10-16 14:29:14 +00003661 Result = getDerived().parseArrayType();
Richard Smithc20d1442018-08-20 20:14:49 +00003662 break;
3663 }
3664 // ::= <pointer-to-member-type>
3665 case 'M': {
Pavel Labathba825192018-10-16 14:29:14 +00003666 Result = getDerived().parsePointerToMemberType();
Richard Smithc20d1442018-08-20 20:14:49 +00003667 break;
3668 }
3669 // ::= <template-param>
3670 case 'T': {
3671 // This could be an elaborate type specifier on a <class-enum-type>.
3672 if (look(1) == 's' || look(1) == 'u' || look(1) == 'e') {
Pavel Labathba825192018-10-16 14:29:14 +00003673 Result = getDerived().parseClassEnumType();
Richard Smithc20d1442018-08-20 20:14:49 +00003674 break;
3675 }
3676
Pavel Labathba825192018-10-16 14:29:14 +00003677 Result = getDerived().parseTemplateParam();
Richard Smithc20d1442018-08-20 20:14:49 +00003678 if (Result == nullptr)
3679 return nullptr;
3680
3681 // Result could be either of:
3682 // <type> ::= <template-param>
3683 // <type> ::= <template-template-param> <template-args>
3684 //
3685 // <template-template-param> ::= <template-param>
3686 // ::= <substitution>
3687 //
3688 // If this is followed by some <template-args>, and we're permitted to
3689 // parse them, take the second production.
3690
3691 if (TryToParseTemplateArgs && look() == 'I') {
Pavel Labathba825192018-10-16 14:29:14 +00003692 Node *TA = getDerived().parseTemplateArgs();
Richard Smithc20d1442018-08-20 20:14:49 +00003693 if (TA == nullptr)
3694 return nullptr;
3695 Result = make<NameWithTemplateArgs>(Result, TA);
3696 }
3697 break;
3698 }
3699 // ::= P <type> # pointer
3700 case 'P': {
3701 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00003702 Node *Ptr = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00003703 if (Ptr == nullptr)
3704 return nullptr;
3705 Result = make<PointerType>(Ptr);
3706 break;
3707 }
3708 // ::= R <type> # l-value reference
3709 case 'R': {
3710 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00003711 Node *Ref = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00003712 if (Ref == nullptr)
3713 return nullptr;
3714 Result = make<ReferenceType>(Ref, ReferenceKind::LValue);
3715 break;
3716 }
3717 // ::= O <type> # r-value reference (C++11)
3718 case 'O': {
3719 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00003720 Node *Ref = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00003721 if (Ref == nullptr)
3722 return nullptr;
3723 Result = make<ReferenceType>(Ref, ReferenceKind::RValue);
3724 break;
3725 }
3726 // ::= C <type> # complex pair (C99)
3727 case 'C': {
3728 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00003729 Node *P = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00003730 if (P == nullptr)
3731 return nullptr;
3732 Result = make<PostfixQualifiedType>(P, " complex");
3733 break;
3734 }
3735 // ::= G <type> # imaginary (C99)
3736 case 'G': {
3737 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00003738 Node *P = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00003739 if (P == nullptr)
3740 return P;
3741 Result = make<PostfixQualifiedType>(P, " imaginary");
3742 break;
3743 }
3744 // ::= <substitution> # See Compression below
3745 case 'S': {
3746 if (look(1) && look(1) != 't') {
Pavel Labathba825192018-10-16 14:29:14 +00003747 Node *Sub = getDerived().parseSubstitution();
Richard Smithc20d1442018-08-20 20:14:49 +00003748 if (Sub == nullptr)
3749 return nullptr;
3750
3751 // Sub could be either of:
3752 // <type> ::= <substitution>
3753 // <type> ::= <template-template-param> <template-args>
3754 //
3755 // <template-template-param> ::= <template-param>
3756 // ::= <substitution>
3757 //
3758 // If this is followed by some <template-args>, and we're permitted to
3759 // parse them, take the second production.
3760
3761 if (TryToParseTemplateArgs && look() == 'I') {
Pavel Labathba825192018-10-16 14:29:14 +00003762 Node *TA = getDerived().parseTemplateArgs();
Richard Smithc20d1442018-08-20 20:14:49 +00003763 if (TA == nullptr)
3764 return nullptr;
3765 Result = make<NameWithTemplateArgs>(Sub, TA);
3766 break;
3767 }
3768
3769 // If all we parsed was a substitution, don't re-insert into the
3770 // substitution table.
3771 return Sub;
3772 }
Erik Pilkingtonf70e4d82019-01-17 20:37:51 +00003773 DEMANGLE_FALLTHROUGH;
Richard Smithc20d1442018-08-20 20:14:49 +00003774 }
3775 // ::= <class-enum-type>
3776 default: {
Pavel Labathba825192018-10-16 14:29:14 +00003777 Result = getDerived().parseClassEnumType();
Richard Smithc20d1442018-08-20 20:14:49 +00003778 break;
3779 }
3780 }
3781
3782 // If we parsed a type, insert it into the substitution table. Note that all
3783 // <builtin-type>s and <substitution>s have already bailed out, because they
3784 // don't get substitutions.
3785 if (Result != nullptr)
3786 Subs.push_back(Result);
3787 return Result;
3788}
3789
Pavel Labathba825192018-10-16 14:29:14 +00003790template <typename Derived, typename Alloc>
3791Node *AbstractManglingParser<Derived, Alloc>::parsePrefixExpr(StringView Kind) {
3792 Node *E = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00003793 if (E == nullptr)
3794 return nullptr;
3795 return make<PrefixExpr>(Kind, E);
3796}
3797
Pavel Labathba825192018-10-16 14:29:14 +00003798template <typename Derived, typename Alloc>
3799Node *AbstractManglingParser<Derived, Alloc>::parseBinaryExpr(StringView Kind) {
3800 Node *LHS = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00003801 if (LHS == nullptr)
3802 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00003803 Node *RHS = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00003804 if (RHS == nullptr)
3805 return nullptr;
3806 return make<BinaryExpr>(LHS, Kind, RHS);
3807}
3808
Pavel Labathba825192018-10-16 14:29:14 +00003809template <typename Derived, typename Alloc>
3810Node *
3811AbstractManglingParser<Derived, Alloc>::parseIntegerLiteral(StringView Lit) {
Richard Smithc20d1442018-08-20 20:14:49 +00003812 StringView Tmp = parseNumber(true);
3813 if (!Tmp.empty() && consumeIf('E'))
3814 return make<IntegerLiteral>(Lit, Tmp);
3815 return nullptr;
3816}
3817
3818// <CV-Qualifiers> ::= [r] [V] [K]
Pavel Labathba825192018-10-16 14:29:14 +00003819template <typename Alloc, typename Derived>
3820Qualifiers AbstractManglingParser<Alloc, Derived>::parseCVQualifiers() {
Richard Smithc20d1442018-08-20 20:14:49 +00003821 Qualifiers CVR = QualNone;
3822 if (consumeIf('r'))
3823 CVR |= QualRestrict;
3824 if (consumeIf('V'))
3825 CVR |= QualVolatile;
3826 if (consumeIf('K'))
3827 CVR |= QualConst;
3828 return CVR;
3829}
3830
3831// <function-param> ::= fp <top-level CV-Qualifiers> _ # L == 0, first parameter
3832// ::= fp <top-level CV-Qualifiers> <parameter-2 non-negative number> _ # L == 0, second and later parameters
3833// ::= fL <L-1 non-negative number> p <top-level CV-Qualifiers> _ # L > 0, first parameter
3834// ::= fL <L-1 non-negative number> p <top-level CV-Qualifiers> <parameter-2 non-negative number> _ # L > 0, second and later parameters
Pavel Labathba825192018-10-16 14:29:14 +00003835template <typename Derived, typename Alloc>
3836Node *AbstractManglingParser<Derived, Alloc>::parseFunctionParam() {
Richard Smithc20d1442018-08-20 20:14:49 +00003837 if (consumeIf("fp")) {
3838 parseCVQualifiers();
3839 StringView Num = parseNumber();
3840 if (!consumeIf('_'))
3841 return nullptr;
3842 return make<FunctionParam>(Num);
3843 }
3844 if (consumeIf("fL")) {
3845 if (parseNumber().empty())
3846 return nullptr;
3847 if (!consumeIf('p'))
3848 return nullptr;
3849 parseCVQualifiers();
3850 StringView Num = parseNumber();
3851 if (!consumeIf('_'))
3852 return nullptr;
3853 return make<FunctionParam>(Num);
3854 }
3855 return nullptr;
3856}
3857
3858// [gs] nw <expression>* _ <type> E # new (expr-list) type
3859// [gs] nw <expression>* _ <type> <initializer> # new (expr-list) type (init)
3860// [gs] na <expression>* _ <type> E # new[] (expr-list) type
3861// [gs] na <expression>* _ <type> <initializer> # new[] (expr-list) type (init)
3862// <initializer> ::= pi <expression>* E # parenthesized initialization
Pavel Labathba825192018-10-16 14:29:14 +00003863template <typename Derived, typename Alloc>
3864Node *AbstractManglingParser<Derived, Alloc>::parseNewExpr() {
Richard Smithc20d1442018-08-20 20:14:49 +00003865 bool Global = consumeIf("gs");
3866 bool IsArray = look(1) == 'a';
3867 if (!consumeIf("nw") && !consumeIf("na"))
3868 return nullptr;
3869 size_t Exprs = Names.size();
3870 while (!consumeIf('_')) {
Pavel Labathba825192018-10-16 14:29:14 +00003871 Node *Ex = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00003872 if (Ex == nullptr)
3873 return nullptr;
3874 Names.push_back(Ex);
3875 }
3876 NodeArray ExprList = popTrailingNodeArray(Exprs);
Pavel Labathba825192018-10-16 14:29:14 +00003877 Node *Ty = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00003878 if (Ty == nullptr)
3879 return Ty;
3880 if (consumeIf("pi")) {
3881 size_t InitsBegin = Names.size();
3882 while (!consumeIf('E')) {
Pavel Labathba825192018-10-16 14:29:14 +00003883 Node *Init = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00003884 if (Init == nullptr)
3885 return Init;
3886 Names.push_back(Init);
3887 }
3888 NodeArray Inits = popTrailingNodeArray(InitsBegin);
3889 return make<NewExpr>(ExprList, Ty, Inits, Global, IsArray);
3890 } else if (!consumeIf('E'))
3891 return nullptr;
3892 return make<NewExpr>(ExprList, Ty, NodeArray(), Global, IsArray);
3893}
3894
3895// cv <type> <expression> # conversion with one argument
3896// cv <type> _ <expression>* E # conversion with a different number of arguments
Pavel Labathba825192018-10-16 14:29:14 +00003897template <typename Derived, typename Alloc>
3898Node *AbstractManglingParser<Derived, Alloc>::parseConversionExpr() {
Richard Smithc20d1442018-08-20 20:14:49 +00003899 if (!consumeIf("cv"))
3900 return nullptr;
3901 Node *Ty;
3902 {
3903 SwapAndRestore<bool> SaveTemp(TryToParseTemplateArgs, false);
Pavel Labathba825192018-10-16 14:29:14 +00003904 Ty = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00003905 }
3906
3907 if (Ty == nullptr)
3908 return nullptr;
3909
3910 if (consumeIf('_')) {
3911 size_t ExprsBegin = Names.size();
3912 while (!consumeIf('E')) {
Pavel Labathba825192018-10-16 14:29:14 +00003913 Node *E = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00003914 if (E == nullptr)
3915 return E;
3916 Names.push_back(E);
3917 }
3918 NodeArray Exprs = popTrailingNodeArray(ExprsBegin);
3919 return make<ConversionExpr>(Ty, Exprs);
3920 }
3921
Pavel Labathba825192018-10-16 14:29:14 +00003922 Node *E[1] = {getDerived().parseExpr()};
Richard Smithc20d1442018-08-20 20:14:49 +00003923 if (E[0] == nullptr)
3924 return nullptr;
3925 return make<ConversionExpr>(Ty, makeNodeArray(E, E + 1));
3926}
3927
3928// <expr-primary> ::= L <type> <value number> E # integer literal
3929// ::= L <type> <value float> E # floating literal
3930// ::= L <string type> E # string literal
3931// ::= L <nullptr type> E # nullptr literal (i.e., "LDnE")
3932// FIXME: ::= L <type> <real-part float> _ <imag-part float> E # complex floating point literal (C 2000)
3933// ::= L <mangled-name> E # external name
Pavel Labathba825192018-10-16 14:29:14 +00003934template <typename Derived, typename Alloc>
3935Node *AbstractManglingParser<Derived, Alloc>::parseExprPrimary() {
Richard Smithc20d1442018-08-20 20:14:49 +00003936 if (!consumeIf('L'))
3937 return nullptr;
3938 switch (look()) {
3939 case 'w':
3940 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00003941 return getDerived().parseIntegerLiteral("wchar_t");
Richard Smithc20d1442018-08-20 20:14:49 +00003942 case 'b':
3943 if (consumeIf("b0E"))
3944 return make<BoolExpr>(0);
3945 if (consumeIf("b1E"))
3946 return make<BoolExpr>(1);
3947 return nullptr;
3948 case 'c':
3949 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00003950 return getDerived().parseIntegerLiteral("char");
Richard Smithc20d1442018-08-20 20:14:49 +00003951 case 'a':
3952 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00003953 return getDerived().parseIntegerLiteral("signed char");
Richard Smithc20d1442018-08-20 20:14:49 +00003954 case 'h':
3955 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00003956 return getDerived().parseIntegerLiteral("unsigned char");
Richard Smithc20d1442018-08-20 20:14:49 +00003957 case 's':
3958 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00003959 return getDerived().parseIntegerLiteral("short");
Richard Smithc20d1442018-08-20 20:14:49 +00003960 case 't':
3961 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00003962 return getDerived().parseIntegerLiteral("unsigned short");
Richard Smithc20d1442018-08-20 20:14:49 +00003963 case 'i':
3964 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00003965 return getDerived().parseIntegerLiteral("");
Richard Smithc20d1442018-08-20 20:14:49 +00003966 case 'j':
3967 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00003968 return getDerived().parseIntegerLiteral("u");
Richard Smithc20d1442018-08-20 20:14:49 +00003969 case 'l':
3970 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00003971 return getDerived().parseIntegerLiteral("l");
Richard Smithc20d1442018-08-20 20:14:49 +00003972 case 'm':
3973 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00003974 return getDerived().parseIntegerLiteral("ul");
Richard Smithc20d1442018-08-20 20:14:49 +00003975 case 'x':
3976 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00003977 return getDerived().parseIntegerLiteral("ll");
Richard Smithc20d1442018-08-20 20:14:49 +00003978 case 'y':
3979 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00003980 return getDerived().parseIntegerLiteral("ull");
Richard Smithc20d1442018-08-20 20:14:49 +00003981 case 'n':
3982 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00003983 return getDerived().parseIntegerLiteral("__int128");
Richard Smithc20d1442018-08-20 20:14:49 +00003984 case 'o':
3985 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00003986 return getDerived().parseIntegerLiteral("unsigned __int128");
Richard Smithc20d1442018-08-20 20:14:49 +00003987 case 'f':
3988 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00003989 return getDerived().template parseFloatingLiteral<float>();
Richard Smithc20d1442018-08-20 20:14:49 +00003990 case 'd':
3991 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00003992 return getDerived().template parseFloatingLiteral<double>();
Richard Smithc20d1442018-08-20 20:14:49 +00003993 case 'e':
3994 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00003995 return getDerived().template parseFloatingLiteral<long double>();
Richard Smithc20d1442018-08-20 20:14:49 +00003996 case '_':
3997 if (consumeIf("_Z")) {
Pavel Labathba825192018-10-16 14:29:14 +00003998 Node *R = getDerived().parseEncoding();
Richard Smithc20d1442018-08-20 20:14:49 +00003999 if (R != nullptr && consumeIf('E'))
4000 return R;
4001 }
4002 return nullptr;
4003 case 'T':
4004 // Invalid mangled name per
4005 // http://sourcerytools.com/pipermail/cxx-abi-dev/2011-August/002422.html
4006 return nullptr;
4007 default: {
4008 // might be named type
Pavel Labathba825192018-10-16 14:29:14 +00004009 Node *T = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00004010 if (T == nullptr)
4011 return nullptr;
4012 StringView N = parseNumber();
4013 if (!N.empty()) {
4014 if (!consumeIf('E'))
4015 return nullptr;
4016 return make<IntegerCastExpr>(T, N);
4017 }
4018 if (consumeIf('E'))
4019 return T;
4020 return nullptr;
4021 }
4022 }
4023}
4024
4025// <braced-expression> ::= <expression>
4026// ::= di <field source-name> <braced-expression> # .name = expr
4027// ::= dx <index expression> <braced-expression> # [expr] = expr
4028// ::= dX <range begin expression> <range end expression> <braced-expression>
Pavel Labathba825192018-10-16 14:29:14 +00004029template <typename Derived, typename Alloc>
4030Node *AbstractManglingParser<Derived, Alloc>::parseBracedExpr() {
Richard Smithc20d1442018-08-20 20:14:49 +00004031 if (look() == 'd') {
4032 switch (look(1)) {
4033 case 'i': {
4034 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004035 Node *Field = getDerived().parseSourceName(/*NameState=*/nullptr);
Richard Smithc20d1442018-08-20 20:14:49 +00004036 if (Field == nullptr)
4037 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00004038 Node *Init = getDerived().parseBracedExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004039 if (Init == nullptr)
4040 return nullptr;
4041 return make<BracedExpr>(Field, Init, /*isArray=*/false);
4042 }
4043 case 'x': {
4044 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004045 Node *Index = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004046 if (Index == nullptr)
4047 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00004048 Node *Init = getDerived().parseBracedExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004049 if (Init == nullptr)
4050 return nullptr;
4051 return make<BracedExpr>(Index, Init, /*isArray=*/true);
4052 }
4053 case 'X': {
4054 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004055 Node *RangeBegin = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004056 if (RangeBegin == nullptr)
4057 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00004058 Node *RangeEnd = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004059 if (RangeEnd == nullptr)
4060 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00004061 Node *Init = getDerived().parseBracedExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004062 if (Init == nullptr)
4063 return nullptr;
4064 return make<BracedRangeExpr>(RangeBegin, RangeEnd, Init);
4065 }
4066 }
4067 }
Pavel Labathba825192018-10-16 14:29:14 +00004068 return getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004069}
4070
4071// (not yet in the spec)
4072// <fold-expr> ::= fL <binary-operator-name> <expression> <expression>
4073// ::= fR <binary-operator-name> <expression> <expression>
4074// ::= fl <binary-operator-name> <expression>
4075// ::= fr <binary-operator-name> <expression>
Pavel Labathba825192018-10-16 14:29:14 +00004076template <typename Derived, typename Alloc>
4077Node *AbstractManglingParser<Derived, Alloc>::parseFoldExpr() {
Richard Smithc20d1442018-08-20 20:14:49 +00004078 if (!consumeIf('f'))
4079 return nullptr;
4080
4081 char FoldKind = look();
4082 bool IsLeftFold, HasInitializer;
4083 HasInitializer = FoldKind == 'L' || FoldKind == 'R';
4084 if (FoldKind == 'l' || FoldKind == 'L')
4085 IsLeftFold = true;
4086 else if (FoldKind == 'r' || FoldKind == 'R')
4087 IsLeftFold = false;
4088 else
4089 return nullptr;
4090 ++First;
4091
4092 // FIXME: This map is duplicated in parseOperatorName and parseExpr.
4093 StringView OperatorName;
4094 if (consumeIf("aa")) OperatorName = "&&";
4095 else if (consumeIf("an")) OperatorName = "&";
4096 else if (consumeIf("aN")) OperatorName = "&=";
4097 else if (consumeIf("aS")) OperatorName = "=";
4098 else if (consumeIf("cm")) OperatorName = ",";
4099 else if (consumeIf("ds")) OperatorName = ".*";
4100 else if (consumeIf("dv")) OperatorName = "/";
4101 else if (consumeIf("dV")) OperatorName = "/=";
4102 else if (consumeIf("eo")) OperatorName = "^";
4103 else if (consumeIf("eO")) OperatorName = "^=";
4104 else if (consumeIf("eq")) OperatorName = "==";
4105 else if (consumeIf("ge")) OperatorName = ">=";
4106 else if (consumeIf("gt")) OperatorName = ">";
4107 else if (consumeIf("le")) OperatorName = "<=";
4108 else if (consumeIf("ls")) OperatorName = "<<";
4109 else if (consumeIf("lS")) OperatorName = "<<=";
4110 else if (consumeIf("lt")) OperatorName = "<";
4111 else if (consumeIf("mi")) OperatorName = "-";
4112 else if (consumeIf("mI")) OperatorName = "-=";
4113 else if (consumeIf("ml")) OperatorName = "*";
4114 else if (consumeIf("mL")) OperatorName = "*=";
4115 else if (consumeIf("ne")) OperatorName = "!=";
4116 else if (consumeIf("oo")) OperatorName = "||";
4117 else if (consumeIf("or")) OperatorName = "|";
4118 else if (consumeIf("oR")) OperatorName = "|=";
4119 else if (consumeIf("pl")) OperatorName = "+";
4120 else if (consumeIf("pL")) OperatorName = "+=";
4121 else if (consumeIf("rm")) OperatorName = "%";
4122 else if (consumeIf("rM")) OperatorName = "%=";
4123 else if (consumeIf("rs")) OperatorName = ">>";
4124 else if (consumeIf("rS")) OperatorName = ">>=";
4125 else return nullptr;
4126
Pavel Labathba825192018-10-16 14:29:14 +00004127 Node *Pack = getDerived().parseExpr(), *Init = nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00004128 if (Pack == nullptr)
4129 return nullptr;
4130 if (HasInitializer) {
Pavel Labathba825192018-10-16 14:29:14 +00004131 Init = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004132 if (Init == nullptr)
4133 return nullptr;
4134 }
4135
4136 if (IsLeftFold && Init)
4137 std::swap(Pack, Init);
4138
4139 return make<FoldExpr>(IsLeftFold, OperatorName, Pack, Init);
4140}
4141
4142// <expression> ::= <unary operator-name> <expression>
4143// ::= <binary operator-name> <expression> <expression>
4144// ::= <ternary operator-name> <expression> <expression> <expression>
4145// ::= cl <expression>+ E # call
4146// ::= cv <type> <expression> # conversion with one argument
4147// ::= cv <type> _ <expression>* E # conversion with a different number of arguments
4148// ::= [gs] nw <expression>* _ <type> E # new (expr-list) type
4149// ::= [gs] nw <expression>* _ <type> <initializer> # new (expr-list) type (init)
4150// ::= [gs] na <expression>* _ <type> E # new[] (expr-list) type
4151// ::= [gs] na <expression>* _ <type> <initializer> # new[] (expr-list) type (init)
4152// ::= [gs] dl <expression> # delete expression
4153// ::= [gs] da <expression> # delete[] expression
4154// ::= pp_ <expression> # prefix ++
4155// ::= mm_ <expression> # prefix --
4156// ::= ti <type> # typeid (type)
4157// ::= te <expression> # typeid (expression)
4158// ::= dc <type> <expression> # dynamic_cast<type> (expression)
4159// ::= sc <type> <expression> # static_cast<type> (expression)
4160// ::= cc <type> <expression> # const_cast<type> (expression)
4161// ::= rc <type> <expression> # reinterpret_cast<type> (expression)
4162// ::= st <type> # sizeof (a type)
4163// ::= sz <expression> # sizeof (an expression)
4164// ::= at <type> # alignof (a type)
4165// ::= az <expression> # alignof (an expression)
4166// ::= nx <expression> # noexcept (expression)
4167// ::= <template-param>
4168// ::= <function-param>
4169// ::= dt <expression> <unresolved-name> # expr.name
4170// ::= pt <expression> <unresolved-name> # expr->name
4171// ::= ds <expression> <expression> # expr.*expr
4172// ::= sZ <template-param> # size of a parameter pack
4173// ::= sZ <function-param> # size of a function parameter pack
4174// ::= sP <template-arg>* E # sizeof...(T), size of a captured template parameter pack from an alias template
4175// ::= sp <expression> # pack expansion
4176// ::= tw <expression> # throw expression
4177// ::= tr # throw with no operand (rethrow)
4178// ::= <unresolved-name> # f(p), N::f(p), ::f(p),
4179// # freestanding dependent name (e.g., T::x),
4180// # objectless nonstatic member reference
4181// ::= fL <binary-operator-name> <expression> <expression>
4182// ::= fR <binary-operator-name> <expression> <expression>
4183// ::= fl <binary-operator-name> <expression>
4184// ::= fr <binary-operator-name> <expression>
4185// ::= <expr-primary>
Pavel Labathba825192018-10-16 14:29:14 +00004186template <typename Derived, typename Alloc>
4187Node *AbstractManglingParser<Derived, Alloc>::parseExpr() {
Richard Smithc20d1442018-08-20 20:14:49 +00004188 bool Global = consumeIf("gs");
4189 if (numLeft() < 2)
4190 return nullptr;
4191
4192 switch (*First) {
4193 case 'L':
Pavel Labathba825192018-10-16 14:29:14 +00004194 return getDerived().parseExprPrimary();
Richard Smithc20d1442018-08-20 20:14:49 +00004195 case 'T':
Pavel Labathba825192018-10-16 14:29:14 +00004196 return getDerived().parseTemplateParam();
Richard Smithc20d1442018-08-20 20:14:49 +00004197 case 'f': {
4198 // Disambiguate a fold expression from a <function-param>.
4199 if (look(1) == 'p' || (look(1) == 'L' && std::isdigit(look(2))))
Pavel Labathba825192018-10-16 14:29:14 +00004200 return getDerived().parseFunctionParam();
4201 return getDerived().parseFoldExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004202 }
4203 case 'a':
4204 switch (First[1]) {
4205 case 'a':
4206 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004207 return getDerived().parseBinaryExpr("&&");
Richard Smithc20d1442018-08-20 20:14:49 +00004208 case 'd':
4209 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004210 return getDerived().parsePrefixExpr("&");
Richard Smithc20d1442018-08-20 20:14:49 +00004211 case 'n':
4212 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004213 return getDerived().parseBinaryExpr("&");
Richard Smithc20d1442018-08-20 20:14:49 +00004214 case 'N':
4215 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004216 return getDerived().parseBinaryExpr("&=");
Richard Smithc20d1442018-08-20 20:14:49 +00004217 case 'S':
4218 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004219 return getDerived().parseBinaryExpr("=");
Richard Smithc20d1442018-08-20 20:14:49 +00004220 case 't': {
4221 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004222 Node *Ty = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00004223 if (Ty == nullptr)
4224 return nullptr;
4225 return make<EnclosingExpr>("alignof (", Ty, ")");
4226 }
4227 case 'z': {
4228 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004229 Node *Ty = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004230 if (Ty == nullptr)
4231 return nullptr;
4232 return make<EnclosingExpr>("alignof (", Ty, ")");
4233 }
4234 }
4235 return nullptr;
4236 case 'c':
4237 switch (First[1]) {
4238 // cc <type> <expression> # const_cast<type>(expression)
4239 case 'c': {
4240 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004241 Node *Ty = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00004242 if (Ty == nullptr)
4243 return Ty;
Pavel Labathba825192018-10-16 14:29:14 +00004244 Node *Ex = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004245 if (Ex == nullptr)
4246 return Ex;
4247 return make<CastExpr>("const_cast", Ty, Ex);
4248 }
4249 // cl <expression>+ E # call
4250 case 'l': {
4251 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004252 Node *Callee = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004253 if (Callee == nullptr)
4254 return Callee;
4255 size_t ExprsBegin = Names.size();
4256 while (!consumeIf('E')) {
Pavel Labathba825192018-10-16 14:29:14 +00004257 Node *E = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004258 if (E == nullptr)
4259 return E;
4260 Names.push_back(E);
4261 }
4262 return make<CallExpr>(Callee, popTrailingNodeArray(ExprsBegin));
4263 }
4264 case 'm':
4265 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004266 return getDerived().parseBinaryExpr(",");
Richard Smithc20d1442018-08-20 20:14:49 +00004267 case 'o':
4268 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004269 return getDerived().parsePrefixExpr("~");
Richard Smithc20d1442018-08-20 20:14:49 +00004270 case 'v':
Pavel Labathba825192018-10-16 14:29:14 +00004271 return getDerived().parseConversionExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004272 }
4273 return nullptr;
4274 case 'd':
4275 switch (First[1]) {
4276 case 'a': {
4277 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004278 Node *Ex = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004279 if (Ex == nullptr)
4280 return Ex;
4281 return make<DeleteExpr>(Ex, Global, /*is_array=*/true);
4282 }
4283 case 'c': {
4284 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004285 Node *T = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00004286 if (T == nullptr)
4287 return T;
Pavel Labathba825192018-10-16 14:29:14 +00004288 Node *Ex = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004289 if (Ex == nullptr)
4290 return Ex;
4291 return make<CastExpr>("dynamic_cast", T, Ex);
4292 }
4293 case 'e':
4294 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004295 return getDerived().parsePrefixExpr("*");
Richard Smithc20d1442018-08-20 20:14:49 +00004296 case 'l': {
4297 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004298 Node *E = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004299 if (E == nullptr)
4300 return E;
4301 return make<DeleteExpr>(E, Global, /*is_array=*/false);
4302 }
4303 case 'n':
Pavel Labathba825192018-10-16 14:29:14 +00004304 return getDerived().parseUnresolvedName();
Richard Smithc20d1442018-08-20 20:14:49 +00004305 case 's': {
4306 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004307 Node *LHS = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004308 if (LHS == nullptr)
4309 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00004310 Node *RHS = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004311 if (RHS == nullptr)
4312 return nullptr;
4313 return make<MemberExpr>(LHS, ".*", RHS);
4314 }
4315 case 't': {
4316 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004317 Node *LHS = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004318 if (LHS == nullptr)
4319 return LHS;
Pavel Labathba825192018-10-16 14:29:14 +00004320 Node *RHS = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004321 if (RHS == nullptr)
4322 return nullptr;
4323 return make<MemberExpr>(LHS, ".", RHS);
4324 }
4325 case 'v':
4326 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004327 return getDerived().parseBinaryExpr("/");
Richard Smithc20d1442018-08-20 20:14:49 +00004328 case 'V':
4329 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004330 return getDerived().parseBinaryExpr("/=");
Richard Smithc20d1442018-08-20 20:14:49 +00004331 }
4332 return nullptr;
4333 case 'e':
4334 switch (First[1]) {
4335 case 'o':
4336 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004337 return getDerived().parseBinaryExpr("^");
Richard Smithc20d1442018-08-20 20:14:49 +00004338 case 'O':
4339 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004340 return getDerived().parseBinaryExpr("^=");
Richard Smithc20d1442018-08-20 20:14:49 +00004341 case 'q':
4342 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004343 return getDerived().parseBinaryExpr("==");
Richard Smithc20d1442018-08-20 20:14:49 +00004344 }
4345 return nullptr;
4346 case 'g':
4347 switch (First[1]) {
4348 case 'e':
4349 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004350 return getDerived().parseBinaryExpr(">=");
Richard Smithc20d1442018-08-20 20:14:49 +00004351 case 't':
4352 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004353 return getDerived().parseBinaryExpr(">");
Richard Smithc20d1442018-08-20 20:14:49 +00004354 }
4355 return nullptr;
4356 case 'i':
4357 switch (First[1]) {
4358 case 'x': {
4359 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004360 Node *Base = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004361 if (Base == nullptr)
4362 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00004363 Node *Index = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004364 if (Index == nullptr)
4365 return Index;
4366 return make<ArraySubscriptExpr>(Base, Index);
4367 }
4368 case 'l': {
4369 First += 2;
4370 size_t InitsBegin = Names.size();
4371 while (!consumeIf('E')) {
Pavel Labathba825192018-10-16 14:29:14 +00004372 Node *E = getDerived().parseBracedExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004373 if (E == nullptr)
4374 return nullptr;
4375 Names.push_back(E);
4376 }
4377 return make<InitListExpr>(nullptr, popTrailingNodeArray(InitsBegin));
4378 }
4379 }
4380 return nullptr;
4381 case 'l':
4382 switch (First[1]) {
4383 case 'e':
4384 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004385 return getDerived().parseBinaryExpr("<=");
Richard Smithc20d1442018-08-20 20:14:49 +00004386 case 's':
4387 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004388 return getDerived().parseBinaryExpr("<<");
Richard Smithc20d1442018-08-20 20:14:49 +00004389 case 'S':
4390 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004391 return getDerived().parseBinaryExpr("<<=");
Richard Smithc20d1442018-08-20 20:14:49 +00004392 case 't':
4393 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004394 return getDerived().parseBinaryExpr("<");
Richard Smithc20d1442018-08-20 20:14:49 +00004395 }
4396 return nullptr;
4397 case 'm':
4398 switch (First[1]) {
4399 case 'i':
4400 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004401 return getDerived().parseBinaryExpr("-");
Richard Smithc20d1442018-08-20 20:14:49 +00004402 case 'I':
4403 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004404 return getDerived().parseBinaryExpr("-=");
Richard Smithc20d1442018-08-20 20:14:49 +00004405 case 'l':
4406 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004407 return getDerived().parseBinaryExpr("*");
Richard Smithc20d1442018-08-20 20:14:49 +00004408 case 'L':
4409 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004410 return getDerived().parseBinaryExpr("*=");
Richard Smithc20d1442018-08-20 20:14:49 +00004411 case 'm':
4412 First += 2;
4413 if (consumeIf('_'))
Pavel Labathba825192018-10-16 14:29:14 +00004414 return getDerived().parsePrefixExpr("--");
4415 Node *Ex = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004416 if (Ex == nullptr)
4417 return nullptr;
4418 return make<PostfixExpr>(Ex, "--");
4419 }
4420 return nullptr;
4421 case 'n':
4422 switch (First[1]) {
4423 case 'a':
4424 case 'w':
Pavel Labathba825192018-10-16 14:29:14 +00004425 return getDerived().parseNewExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004426 case 'e':
4427 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004428 return getDerived().parseBinaryExpr("!=");
Richard Smithc20d1442018-08-20 20:14:49 +00004429 case 'g':
4430 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004431 return getDerived().parsePrefixExpr("-");
Richard Smithc20d1442018-08-20 20:14:49 +00004432 case 't':
4433 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004434 return getDerived().parsePrefixExpr("!");
Richard Smithc20d1442018-08-20 20:14:49 +00004435 case 'x':
4436 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004437 Node *Ex = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004438 if (Ex == nullptr)
4439 return Ex;
4440 return make<EnclosingExpr>("noexcept (", Ex, ")");
4441 }
4442 return nullptr;
4443 case 'o':
4444 switch (First[1]) {
4445 case 'n':
Pavel Labathba825192018-10-16 14:29:14 +00004446 return getDerived().parseUnresolvedName();
Richard Smithc20d1442018-08-20 20:14:49 +00004447 case 'o':
4448 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004449 return getDerived().parseBinaryExpr("||");
Richard Smithc20d1442018-08-20 20:14:49 +00004450 case 'r':
4451 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004452 return getDerived().parseBinaryExpr("|");
Richard Smithc20d1442018-08-20 20:14:49 +00004453 case 'R':
4454 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004455 return getDerived().parseBinaryExpr("|=");
Richard Smithc20d1442018-08-20 20:14:49 +00004456 }
4457 return nullptr;
4458 case 'p':
4459 switch (First[1]) {
4460 case 'm':
4461 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004462 return getDerived().parseBinaryExpr("->*");
Richard Smithc20d1442018-08-20 20:14:49 +00004463 case 'l':
4464 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004465 return getDerived().parseBinaryExpr("+");
Richard Smithc20d1442018-08-20 20:14:49 +00004466 case 'L':
4467 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004468 return getDerived().parseBinaryExpr("+=");
Richard Smithc20d1442018-08-20 20:14:49 +00004469 case 'p': {
4470 First += 2;
4471 if (consumeIf('_'))
Pavel Labathba825192018-10-16 14:29:14 +00004472 return getDerived().parsePrefixExpr("++");
4473 Node *Ex = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004474 if (Ex == nullptr)
4475 return Ex;
4476 return make<PostfixExpr>(Ex, "++");
4477 }
4478 case 's':
4479 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004480 return getDerived().parsePrefixExpr("+");
Richard Smithc20d1442018-08-20 20:14:49 +00004481 case 't': {
4482 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004483 Node *L = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004484 if (L == nullptr)
4485 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00004486 Node *R = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004487 if (R == nullptr)
4488 return nullptr;
4489 return make<MemberExpr>(L, "->", R);
4490 }
4491 }
4492 return nullptr;
4493 case 'q':
4494 if (First[1] == 'u') {
4495 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004496 Node *Cond = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004497 if (Cond == nullptr)
4498 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00004499 Node *LHS = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004500 if (LHS == nullptr)
4501 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00004502 Node *RHS = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004503 if (RHS == nullptr)
4504 return nullptr;
4505 return make<ConditionalExpr>(Cond, LHS, RHS);
4506 }
4507 return nullptr;
4508 case 'r':
4509 switch (First[1]) {
4510 case 'c': {
4511 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004512 Node *T = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00004513 if (T == nullptr)
4514 return T;
Pavel Labathba825192018-10-16 14:29:14 +00004515 Node *Ex = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004516 if (Ex == nullptr)
4517 return Ex;
4518 return make<CastExpr>("reinterpret_cast", T, Ex);
4519 }
4520 case 'm':
4521 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004522 return getDerived().parseBinaryExpr("%");
Richard Smithc20d1442018-08-20 20:14:49 +00004523 case 'M':
4524 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004525 return getDerived().parseBinaryExpr("%=");
Richard Smithc20d1442018-08-20 20:14:49 +00004526 case 's':
4527 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004528 return getDerived().parseBinaryExpr(">>");
Richard Smithc20d1442018-08-20 20:14:49 +00004529 case 'S':
4530 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004531 return getDerived().parseBinaryExpr(">>=");
Richard Smithc20d1442018-08-20 20:14:49 +00004532 }
4533 return nullptr;
4534 case 's':
4535 switch (First[1]) {
4536 case 'c': {
4537 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004538 Node *T = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00004539 if (T == nullptr)
4540 return T;
Pavel Labathba825192018-10-16 14:29:14 +00004541 Node *Ex = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004542 if (Ex == nullptr)
4543 return Ex;
4544 return make<CastExpr>("static_cast", T, Ex);
4545 }
4546 case 'p': {
4547 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004548 Node *Child = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004549 if (Child == nullptr)
4550 return nullptr;
4551 return make<ParameterPackExpansion>(Child);
4552 }
4553 case 'r':
Pavel Labathba825192018-10-16 14:29:14 +00004554 return getDerived().parseUnresolvedName();
Richard Smithc20d1442018-08-20 20:14:49 +00004555 case 't': {
4556 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004557 Node *Ty = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00004558 if (Ty == nullptr)
4559 return Ty;
4560 return make<EnclosingExpr>("sizeof (", Ty, ")");
4561 }
4562 case 'z': {
4563 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004564 Node *Ex = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004565 if (Ex == nullptr)
4566 return Ex;
4567 return make<EnclosingExpr>("sizeof (", Ex, ")");
4568 }
4569 case 'Z':
4570 First += 2;
4571 if (look() == 'T') {
Pavel Labathba825192018-10-16 14:29:14 +00004572 Node *R = getDerived().parseTemplateParam();
Richard Smithc20d1442018-08-20 20:14:49 +00004573 if (R == nullptr)
4574 return nullptr;
4575 return make<SizeofParamPackExpr>(R);
4576 } else if (look() == 'f') {
Pavel Labathba825192018-10-16 14:29:14 +00004577 Node *FP = getDerived().parseFunctionParam();
Richard Smithc20d1442018-08-20 20:14:49 +00004578 if (FP == nullptr)
4579 return nullptr;
4580 return make<EnclosingExpr>("sizeof... (", FP, ")");
4581 }
4582 return nullptr;
4583 case 'P': {
4584 First += 2;
4585 size_t ArgsBegin = Names.size();
4586 while (!consumeIf('E')) {
Pavel Labathba825192018-10-16 14:29:14 +00004587 Node *Arg = getDerived().parseTemplateArg();
Richard Smithc20d1442018-08-20 20:14:49 +00004588 if (Arg == nullptr)
4589 return nullptr;
4590 Names.push_back(Arg);
4591 }
Richard Smithb485b352018-08-24 23:30:26 +00004592 auto *Pack = make<NodeArrayNode>(popTrailingNodeArray(ArgsBegin));
4593 if (!Pack)
4594 return nullptr;
4595 return make<EnclosingExpr>("sizeof... (", Pack, ")");
Richard Smithc20d1442018-08-20 20:14:49 +00004596 }
4597 }
4598 return nullptr;
4599 case 't':
4600 switch (First[1]) {
4601 case 'e': {
4602 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004603 Node *Ex = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004604 if (Ex == nullptr)
4605 return Ex;
4606 return make<EnclosingExpr>("typeid (", Ex, ")");
4607 }
4608 case 'i': {
4609 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004610 Node *Ty = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00004611 if (Ty == nullptr)
4612 return Ty;
4613 return make<EnclosingExpr>("typeid (", Ty, ")");
4614 }
4615 case 'l': {
4616 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004617 Node *Ty = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00004618 if (Ty == nullptr)
4619 return nullptr;
4620 size_t InitsBegin = Names.size();
4621 while (!consumeIf('E')) {
Pavel Labathba825192018-10-16 14:29:14 +00004622 Node *E = getDerived().parseBracedExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004623 if (E == nullptr)
4624 return nullptr;
4625 Names.push_back(E);
4626 }
4627 return make<InitListExpr>(Ty, popTrailingNodeArray(InitsBegin));
4628 }
4629 case 'r':
4630 First += 2;
4631 return make<NameType>("throw");
4632 case 'w': {
4633 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004634 Node *Ex = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00004635 if (Ex == nullptr)
4636 return nullptr;
4637 return make<ThrowExpr>(Ex);
4638 }
4639 }
4640 return nullptr;
4641 case '1':
4642 case '2':
4643 case '3':
4644 case '4':
4645 case '5':
4646 case '6':
4647 case '7':
4648 case '8':
4649 case '9':
Pavel Labathba825192018-10-16 14:29:14 +00004650 return getDerived().parseUnresolvedName();
Richard Smithc20d1442018-08-20 20:14:49 +00004651 }
4652 return nullptr;
4653}
4654
4655// <call-offset> ::= h <nv-offset> _
4656// ::= v <v-offset> _
4657//
4658// <nv-offset> ::= <offset number>
4659// # non-virtual base override
4660//
4661// <v-offset> ::= <offset number> _ <virtual offset number>
4662// # virtual base override, with vcall offset
Pavel Labathba825192018-10-16 14:29:14 +00004663template <typename Alloc, typename Derived>
4664bool AbstractManglingParser<Alloc, Derived>::parseCallOffset() {
Richard Smithc20d1442018-08-20 20:14:49 +00004665 // Just scan through the call offset, we never add this information into the
4666 // output.
4667 if (consumeIf('h'))
4668 return parseNumber(true).empty() || !consumeIf('_');
4669 if (consumeIf('v'))
4670 return parseNumber(true).empty() || !consumeIf('_') ||
4671 parseNumber(true).empty() || !consumeIf('_');
4672 return true;
4673}
4674
4675// <special-name> ::= TV <type> # virtual table
4676// ::= TT <type> # VTT structure (construction vtable index)
4677// ::= TI <type> # typeinfo structure
4678// ::= TS <type> # typeinfo name (null-terminated byte string)
4679// ::= Tc <call-offset> <call-offset> <base encoding>
4680// # base is the nominal target function of thunk
4681// # first call-offset is 'this' adjustment
4682// # second call-offset is result adjustment
4683// ::= T <call-offset> <base encoding>
4684// # base is the nominal target function of thunk
4685// ::= GV <object name> # Guard variable for one-time initialization
4686// # No <type>
4687// ::= TW <object name> # Thread-local wrapper
4688// ::= TH <object name> # Thread-local initialization
4689// ::= GR <object name> _ # First temporary
4690// ::= GR <object name> <seq-id> _ # Subsequent temporaries
4691// extension ::= TC <first type> <number> _ <second type> # construction vtable for second-in-first
4692// extension ::= GR <object name> # reference temporary for object
Pavel Labathba825192018-10-16 14:29:14 +00004693template <typename Derived, typename Alloc>
4694Node *AbstractManglingParser<Derived, Alloc>::parseSpecialName() {
Richard Smithc20d1442018-08-20 20:14:49 +00004695 switch (look()) {
4696 case 'T':
4697 switch (look(1)) {
4698 // TV <type> # virtual table
4699 case 'V': {
4700 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004701 Node *Ty = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00004702 if (Ty == nullptr)
4703 return nullptr;
4704 return make<SpecialName>("vtable for ", Ty);
4705 }
4706 // TT <type> # VTT structure (construction vtable index)
4707 case 'T': {
4708 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004709 Node *Ty = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00004710 if (Ty == nullptr)
4711 return nullptr;
4712 return make<SpecialName>("VTT for ", Ty);
4713 }
4714 // TI <type> # typeinfo structure
4715 case 'I': {
4716 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004717 Node *Ty = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00004718 if (Ty == nullptr)
4719 return nullptr;
4720 return make<SpecialName>("typeinfo for ", Ty);
4721 }
4722 // TS <type> # typeinfo name (null-terminated byte string)
4723 case 'S': {
4724 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004725 Node *Ty = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00004726 if (Ty == nullptr)
4727 return nullptr;
4728 return make<SpecialName>("typeinfo name for ", Ty);
4729 }
4730 // Tc <call-offset> <call-offset> <base encoding>
4731 case 'c': {
4732 First += 2;
4733 if (parseCallOffset() || parseCallOffset())
4734 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00004735 Node *Encoding = getDerived().parseEncoding();
Richard Smithc20d1442018-08-20 20:14:49 +00004736 if (Encoding == nullptr)
4737 return nullptr;
4738 return make<SpecialName>("covariant return thunk to ", Encoding);
4739 }
4740 // extension ::= TC <first type> <number> _ <second type>
4741 // # construction vtable for second-in-first
4742 case 'C': {
4743 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004744 Node *FirstType = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00004745 if (FirstType == nullptr)
4746 return nullptr;
4747 if (parseNumber(true).empty() || !consumeIf('_'))
4748 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00004749 Node *SecondType = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00004750 if (SecondType == nullptr)
4751 return nullptr;
4752 return make<CtorVtableSpecialName>(SecondType, FirstType);
4753 }
4754 // TW <object name> # Thread-local wrapper
4755 case 'W': {
4756 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004757 Node *Name = getDerived().parseName();
Richard Smithc20d1442018-08-20 20:14:49 +00004758 if (Name == nullptr)
4759 return nullptr;
4760 return make<SpecialName>("thread-local wrapper routine for ", Name);
4761 }
4762 // TH <object name> # Thread-local initialization
4763 case 'H': {
4764 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004765 Node *Name = getDerived().parseName();
Richard Smithc20d1442018-08-20 20:14:49 +00004766 if (Name == nullptr)
4767 return nullptr;
4768 return make<SpecialName>("thread-local initialization routine for ", Name);
4769 }
4770 // T <call-offset> <base encoding>
4771 default: {
4772 ++First;
4773 bool IsVirt = look() == 'v';
4774 if (parseCallOffset())
4775 return nullptr;
Pavel Labathba825192018-10-16 14:29:14 +00004776 Node *BaseEncoding = getDerived().parseEncoding();
Richard Smithc20d1442018-08-20 20:14:49 +00004777 if (BaseEncoding == nullptr)
4778 return nullptr;
4779 if (IsVirt)
4780 return make<SpecialName>("virtual thunk to ", BaseEncoding);
4781 else
4782 return make<SpecialName>("non-virtual thunk to ", BaseEncoding);
4783 }
4784 }
4785 case 'G':
4786 switch (look(1)) {
4787 // GV <object name> # Guard variable for one-time initialization
4788 case 'V': {
4789 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004790 Node *Name = getDerived().parseName();
Richard Smithc20d1442018-08-20 20:14:49 +00004791 if (Name == nullptr)
4792 return nullptr;
4793 return make<SpecialName>("guard variable for ", Name);
4794 }
4795 // GR <object name> # reference temporary for object
4796 // GR <object name> _ # First temporary
4797 // GR <object name> <seq-id> _ # Subsequent temporaries
4798 case 'R': {
4799 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00004800 Node *Name = getDerived().parseName();
Richard Smithc20d1442018-08-20 20:14:49 +00004801 if (Name == nullptr)
4802 return nullptr;
4803 size_t Count;
4804 bool ParsedSeqId = !parseSeqId(&Count);
4805 if (!consumeIf('_') && ParsedSeqId)
4806 return nullptr;
4807 return make<SpecialName>("reference temporary for ", Name);
4808 }
4809 }
4810 }
4811 return nullptr;
4812}
4813
4814// <encoding> ::= <function name> <bare-function-type>
4815// ::= <data name>
4816// ::= <special-name>
Pavel Labathba825192018-10-16 14:29:14 +00004817template <typename Derived, typename Alloc>
4818Node *AbstractManglingParser<Derived, Alloc>::parseEncoding() {
Richard Smithc20d1442018-08-20 20:14:49 +00004819 if (look() == 'G' || look() == 'T')
Pavel Labathba825192018-10-16 14:29:14 +00004820 return getDerived().parseSpecialName();
Richard Smithc20d1442018-08-20 20:14:49 +00004821
4822 auto IsEndOfEncoding = [&] {
4823 // The set of chars that can potentially follow an <encoding> (none of which
4824 // can start a <type>). Enumerating these allows us to avoid speculative
4825 // parsing.
4826 return numLeft() == 0 || look() == 'E' || look() == '.' || look() == '_';
4827 };
4828
4829 NameState NameInfo(this);
Pavel Labathba825192018-10-16 14:29:14 +00004830 Node *Name = getDerived().parseName(&NameInfo);
Richard Smithc20d1442018-08-20 20:14:49 +00004831 if (Name == nullptr)
4832 return nullptr;
4833
4834 if (resolveForwardTemplateRefs(NameInfo))
4835 return nullptr;
4836
4837 if (IsEndOfEncoding())
4838 return Name;
4839
4840 Node *Attrs = nullptr;
4841 if (consumeIf("Ua9enable_ifI")) {
4842 size_t BeforeArgs = Names.size();
4843 while (!consumeIf('E')) {
Pavel Labathba825192018-10-16 14:29:14 +00004844 Node *Arg = getDerived().parseTemplateArg();
Richard Smithc20d1442018-08-20 20:14:49 +00004845 if (Arg == nullptr)
4846 return nullptr;
4847 Names.push_back(Arg);
4848 }
4849 Attrs = make<EnableIfAttr>(popTrailingNodeArray(BeforeArgs));
Richard Smithb485b352018-08-24 23:30:26 +00004850 if (!Attrs)
4851 return nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00004852 }
4853
4854 Node *ReturnType = nullptr;
4855 if (!NameInfo.CtorDtorConversion && NameInfo.EndsWithTemplateArgs) {
Pavel Labathba825192018-10-16 14:29:14 +00004856 ReturnType = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00004857 if (ReturnType == nullptr)
4858 return nullptr;
4859 }
4860
4861 if (consumeIf('v'))
4862 return make<FunctionEncoding>(ReturnType, Name, NodeArray(),
4863 Attrs, NameInfo.CVQualifiers,
4864 NameInfo.ReferenceQualifier);
4865
4866 size_t ParamsBegin = Names.size();
4867 do {
Pavel Labathba825192018-10-16 14:29:14 +00004868 Node *Ty = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00004869 if (Ty == nullptr)
4870 return nullptr;
4871 Names.push_back(Ty);
4872 } while (!IsEndOfEncoding());
4873
4874 return make<FunctionEncoding>(ReturnType, Name,
4875 popTrailingNodeArray(ParamsBegin),
4876 Attrs, NameInfo.CVQualifiers,
4877 NameInfo.ReferenceQualifier);
4878}
4879
4880template <class Float>
4881struct FloatData;
4882
4883template <>
4884struct FloatData<float>
4885{
4886 static const size_t mangled_size = 8;
4887 static const size_t max_demangled_size = 24;
4888 static constexpr const char* spec = "%af";
4889};
4890
4891template <>
4892struct FloatData<double>
4893{
4894 static const size_t mangled_size = 16;
4895 static const size_t max_demangled_size = 32;
4896 static constexpr const char* spec = "%a";
4897};
4898
4899template <>
4900struct FloatData<long double>
4901{
4902#if defined(__mips__) && defined(__mips_n64) || defined(__aarch64__) || \
4903 defined(__wasm__)
4904 static const size_t mangled_size = 32;
4905#elif defined(__arm__) || defined(__mips__) || defined(__hexagon__)
4906 static const size_t mangled_size = 16;
4907#else
4908 static const size_t mangled_size = 20; // May need to be adjusted to 16 or 24 on other platforms
4909#endif
4910 static const size_t max_demangled_size = 40;
4911 static constexpr const char *spec = "%LaL";
4912};
4913
Pavel Labathba825192018-10-16 14:29:14 +00004914template <typename Alloc, typename Derived>
4915template <class Float>
4916Node *AbstractManglingParser<Alloc, Derived>::parseFloatingLiteral() {
Richard Smithc20d1442018-08-20 20:14:49 +00004917 const size_t N = FloatData<Float>::mangled_size;
4918 if (numLeft() <= N)
4919 return nullptr;
4920 StringView Data(First, First + N);
4921 for (char C : Data)
4922 if (!std::isxdigit(C))
4923 return nullptr;
4924 First += N;
4925 if (!consumeIf('E'))
4926 return nullptr;
4927 return make<FloatLiteralImpl<Float>>(Data);
4928}
4929
4930// <seq-id> ::= <0-9A-Z>+
Pavel Labathba825192018-10-16 14:29:14 +00004931template <typename Alloc, typename Derived>
4932bool AbstractManglingParser<Alloc, Derived>::parseSeqId(size_t *Out) {
Richard Smithc20d1442018-08-20 20:14:49 +00004933 if (!(look() >= '0' && look() <= '9') &&
4934 !(look() >= 'A' && look() <= 'Z'))
4935 return true;
4936
4937 size_t Id = 0;
4938 while (true) {
4939 if (look() >= '0' && look() <= '9') {
4940 Id *= 36;
4941 Id += static_cast<size_t>(look() - '0');
4942 } else if (look() >= 'A' && look() <= 'Z') {
4943 Id *= 36;
4944 Id += static_cast<size_t>(look() - 'A') + 10;
4945 } else {
4946 *Out = Id;
4947 return false;
4948 }
4949 ++First;
4950 }
4951}
4952
4953// <substitution> ::= S <seq-id> _
4954// ::= S_
4955// <substitution> ::= Sa # ::std::allocator
4956// <substitution> ::= Sb # ::std::basic_string
4957// <substitution> ::= Ss # ::std::basic_string < char,
4958// ::std::char_traits<char>,
4959// ::std::allocator<char> >
4960// <substitution> ::= Si # ::std::basic_istream<char, std::char_traits<char> >
4961// <substitution> ::= So # ::std::basic_ostream<char, std::char_traits<char> >
4962// <substitution> ::= Sd # ::std::basic_iostream<char, std::char_traits<char> >
Pavel Labathba825192018-10-16 14:29:14 +00004963template <typename Derived, typename Alloc>
4964Node *AbstractManglingParser<Derived, Alloc>::parseSubstitution() {
Richard Smithc20d1442018-08-20 20:14:49 +00004965 if (!consumeIf('S'))
4966 return nullptr;
4967
4968 if (std::islower(look())) {
4969 Node *SpecialSub;
4970 switch (look()) {
4971 case 'a':
4972 ++First;
4973 SpecialSub = make<SpecialSubstitution>(SpecialSubKind::allocator);
4974 break;
4975 case 'b':
4976 ++First;
4977 SpecialSub = make<SpecialSubstitution>(SpecialSubKind::basic_string);
4978 break;
4979 case 's':
4980 ++First;
4981 SpecialSub = make<SpecialSubstitution>(SpecialSubKind::string);
4982 break;
4983 case 'i':
4984 ++First;
4985 SpecialSub = make<SpecialSubstitution>(SpecialSubKind::istream);
4986 break;
4987 case 'o':
4988 ++First;
4989 SpecialSub = make<SpecialSubstitution>(SpecialSubKind::ostream);
4990 break;
4991 case 'd':
4992 ++First;
4993 SpecialSub = make<SpecialSubstitution>(SpecialSubKind::iostream);
4994 break;
4995 default:
4996 return nullptr;
4997 }
Richard Smithb485b352018-08-24 23:30:26 +00004998 if (!SpecialSub)
4999 return nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00005000 // Itanium C++ ABI 5.1.2: If a name that would use a built-in <substitution>
5001 // has ABI tags, the tags are appended to the substitution; the result is a
5002 // substitutable component.
Pavel Labathba825192018-10-16 14:29:14 +00005003 Node *WithTags = getDerived().parseAbiTags(SpecialSub);
Richard Smithc20d1442018-08-20 20:14:49 +00005004 if (WithTags != SpecialSub) {
5005 Subs.push_back(WithTags);
5006 SpecialSub = WithTags;
5007 }
5008 return SpecialSub;
5009 }
5010
5011 // ::= S_
5012 if (consumeIf('_')) {
5013 if (Subs.empty())
5014 return nullptr;
5015 return Subs[0];
5016 }
5017
5018 // ::= S <seq-id> _
5019 size_t Index = 0;
5020 if (parseSeqId(&Index))
5021 return nullptr;
5022 ++Index;
5023 if (!consumeIf('_') || Index >= Subs.size())
5024 return nullptr;
5025 return Subs[Index];
5026}
5027
5028// <template-param> ::= T_ # first template parameter
5029// ::= T <parameter-2 non-negative number> _
Pavel Labathba825192018-10-16 14:29:14 +00005030template <typename Derived, typename Alloc>
5031Node *AbstractManglingParser<Derived, Alloc>::parseTemplateParam() {
Richard Smithc20d1442018-08-20 20:14:49 +00005032 if (!consumeIf('T'))
5033 return nullptr;
5034
5035 size_t Index = 0;
5036 if (!consumeIf('_')) {
5037 if (parsePositiveInteger(&Index))
5038 return nullptr;
5039 ++Index;
5040 if (!consumeIf('_'))
5041 return nullptr;
5042 }
5043
5044 // Itanium ABI 5.1.8: In a generic lambda, uses of auto in the parameter list
5045 // are mangled as the corresponding artificial template type parameter.
5046 if (ParsingLambdaParams)
5047 return make<NameType>("auto");
5048
5049 // If we're in a context where this <template-param> refers to a
5050 // <template-arg> further ahead in the mangled name (currently just conversion
5051 // operator types), then we should only look it up in the right context.
5052 if (PermitForwardTemplateReferences) {
Richard Smithb485b352018-08-24 23:30:26 +00005053 Node *ForwardRef = make<ForwardTemplateReference>(Index);
5054 if (!ForwardRef)
5055 return nullptr;
5056 assert(ForwardRef->getKind() == Node::KForwardTemplateReference);
5057 ForwardTemplateRefs.push_back(
5058 static_cast<ForwardTemplateReference *>(ForwardRef));
5059 return ForwardRef;
Richard Smithc20d1442018-08-20 20:14:49 +00005060 }
5061
5062 if (Index >= TemplateParams.size())
5063 return nullptr;
5064 return TemplateParams[Index];
5065}
5066
5067// <template-arg> ::= <type> # type or template
5068// ::= X <expression> E # expression
5069// ::= <expr-primary> # simple expressions
5070// ::= J <template-arg>* E # argument pack
5071// ::= LZ <encoding> E # extension
Pavel Labathba825192018-10-16 14:29:14 +00005072template <typename Derived, typename Alloc>
5073Node *AbstractManglingParser<Derived, Alloc>::parseTemplateArg() {
Richard Smithc20d1442018-08-20 20:14:49 +00005074 switch (look()) {
5075 case 'X': {
5076 ++First;
Pavel Labathba825192018-10-16 14:29:14 +00005077 Node *Arg = getDerived().parseExpr();
Richard Smithc20d1442018-08-20 20:14:49 +00005078 if (Arg == nullptr || !consumeIf('E'))
5079 return nullptr;
5080 return Arg;
5081 }
5082 case 'J': {
5083 ++First;
5084 size_t ArgsBegin = Names.size();
5085 while (!consumeIf('E')) {
Pavel Labathba825192018-10-16 14:29:14 +00005086 Node *Arg = getDerived().parseTemplateArg();
Richard Smithc20d1442018-08-20 20:14:49 +00005087 if (Arg == nullptr)
5088 return nullptr;
5089 Names.push_back(Arg);
5090 }
5091 NodeArray Args = popTrailingNodeArray(ArgsBegin);
5092 return make<TemplateArgumentPack>(Args);
5093 }
5094 case 'L': {
5095 // ::= LZ <encoding> E # extension
5096 if (look(1) == 'Z') {
5097 First += 2;
Pavel Labathba825192018-10-16 14:29:14 +00005098 Node *Arg = getDerived().parseEncoding();
Richard Smithc20d1442018-08-20 20:14:49 +00005099 if (Arg == nullptr || !consumeIf('E'))
5100 return nullptr;
5101 return Arg;
5102 }
5103 // ::= <expr-primary> # simple expressions
Pavel Labathba825192018-10-16 14:29:14 +00005104 return getDerived().parseExprPrimary();
Richard Smithc20d1442018-08-20 20:14:49 +00005105 }
5106 default:
Pavel Labathba825192018-10-16 14:29:14 +00005107 return getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00005108 }
5109}
5110
5111// <template-args> ::= I <template-arg>* E
5112// extension, the abi says <template-arg>+
Pavel Labathba825192018-10-16 14:29:14 +00005113template <typename Derived, typename Alloc>
5114Node *
5115AbstractManglingParser<Derived, Alloc>::parseTemplateArgs(bool TagTemplates) {
Richard Smithc20d1442018-08-20 20:14:49 +00005116 if (!consumeIf('I'))
5117 return nullptr;
5118
5119 // <template-params> refer to the innermost <template-args>. Clear out any
5120 // outer args that we may have inserted into TemplateParams.
5121 if (TagTemplates)
5122 TemplateParams.clear();
5123
5124 size_t ArgsBegin = Names.size();
5125 while (!consumeIf('E')) {
5126 if (TagTemplates) {
5127 auto OldParams = std::move(TemplateParams);
Pavel Labathba825192018-10-16 14:29:14 +00005128 Node *Arg = getDerived().parseTemplateArg();
Richard Smithc20d1442018-08-20 20:14:49 +00005129 TemplateParams = std::move(OldParams);
5130 if (Arg == nullptr)
5131 return nullptr;
5132 Names.push_back(Arg);
5133 Node *TableEntry = Arg;
5134 if (Arg->getKind() == Node::KTemplateArgumentPack) {
5135 TableEntry = make<ParameterPack>(
5136 static_cast<TemplateArgumentPack*>(TableEntry)->getElements());
Richard Smithb485b352018-08-24 23:30:26 +00005137 if (!TableEntry)
5138 return nullptr;
Richard Smithc20d1442018-08-20 20:14:49 +00005139 }
5140 TemplateParams.push_back(TableEntry);
5141 } else {
Pavel Labathba825192018-10-16 14:29:14 +00005142 Node *Arg = getDerived().parseTemplateArg();
Richard Smithc20d1442018-08-20 20:14:49 +00005143 if (Arg == nullptr)
5144 return nullptr;
5145 Names.push_back(Arg);
5146 }
5147 }
5148 return make<TemplateArgs>(popTrailingNodeArray(ArgsBegin));
5149}
5150
5151// <mangled-name> ::= _Z <encoding>
5152// ::= <type>
5153// extension ::= ___Z <encoding> _block_invoke
5154// extension ::= ___Z <encoding> _block_invoke<decimal-digit>+
5155// extension ::= ___Z <encoding> _block_invoke_<decimal-digit>+
Pavel Labathba825192018-10-16 14:29:14 +00005156template <typename Derived, typename Alloc>
5157Node *AbstractManglingParser<Derived, Alloc>::parse() {
Erik Pilkingtonc0df1582019-01-17 21:37:36 +00005158 if (consumeIf("_Z") || consumeIf("__Z")) {
Pavel Labathba825192018-10-16 14:29:14 +00005159 Node *Encoding = getDerived().parseEncoding();
Richard Smithc20d1442018-08-20 20:14:49 +00005160 if (Encoding == nullptr)
5161 return nullptr;
5162 if (look() == '.') {
5163 Encoding = make<DotSuffix>(Encoding, StringView(First, Last));
5164 First = Last;
5165 }
5166 if (numLeft() != 0)
5167 return nullptr;
5168 return Encoding;
5169 }
5170
Erik Pilkingtonc0df1582019-01-17 21:37:36 +00005171 if (consumeIf("___Z") || consumeIf("____Z")) {
Pavel Labathba825192018-10-16 14:29:14 +00005172 Node *Encoding = getDerived().parseEncoding();
Richard Smithc20d1442018-08-20 20:14:49 +00005173 if (Encoding == nullptr || !consumeIf("_block_invoke"))
5174 return nullptr;
5175 bool RequireNumber = consumeIf('_');
5176 if (parseNumber().empty() && RequireNumber)
5177 return nullptr;
5178 if (look() == '.')
5179 First = Last;
5180 if (numLeft() != 0)
5181 return nullptr;
5182 return make<SpecialName>("invocation function for block in ", Encoding);
5183 }
5184
Pavel Labathba825192018-10-16 14:29:14 +00005185 Node *Ty = getDerived().parseType();
Richard Smithc20d1442018-08-20 20:14:49 +00005186 if (numLeft() != 0)
5187 return nullptr;
5188 return Ty;
5189}
5190
Pavel Labathba825192018-10-16 14:29:14 +00005191template <typename Alloc>
5192struct ManglingParser : AbstractManglingParser<ManglingParser<Alloc>, Alloc> {
5193 using AbstractManglingParser<ManglingParser<Alloc>,
5194 Alloc>::AbstractManglingParser;
5195};
5196
Erik Pilkingtonf70e4d82019-01-17 20:37:51 +00005197DEMANGLE_NAMESPACE_END
Richard Smithc20d1442018-08-20 20:14:49 +00005198
Erik Pilkingtonf70e4d82019-01-17 20:37:51 +00005199#endif // DEMANGLE_ITANIUMDEMANGLE_H