-
Notifications
You must be signed in to change notification settings - Fork 13.2k
/
Copy pathItaniumDemangle.h
6189 lines (5473 loc) · 188 KB
/
ItaniumDemangle.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//===------------------------- ItaniumDemangle.h ----------------*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
//
// Generic itanium demangler library.
// There are two copies of this file in the source tree. The one under
// libcxxabi is the original and the one under llvm is the copy. Use
// cp-to-llvm.sh to update the copy. See README.txt for more details.
//
//===----------------------------------------------------------------------===//
#ifndef DEMANGLE_ITANIUMDEMANGLE_H
#define DEMANGLE_ITANIUMDEMANGLE_H
#include "DemangleConfig.h"
#include "StringViewExtras.h"
#include "Utility.h"
#include <algorithm>
#include <cctype>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <limits>
#include <new>
#include <string_view>
#include <type_traits>
#include <utility>
#if defined(__clang__)
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wunused-template"
#endif
DEMANGLE_NAMESPACE_BEGIN
template <class T, size_t N> class PODSmallVector {
static_assert(std::is_trivially_copyable<T>::value,
"T is required to be a trivially copyable type");
static_assert(std::is_trivially_default_constructible<T>::value,
"T is required to be trivially default constructible");
T *First = nullptr;
T *Last = nullptr;
T *Cap = nullptr;
T Inline[N] = {};
bool isInline() const { return First == Inline; }
void clearInline() {
First = Inline;
Last = Inline;
Cap = Inline + N;
}
void reserve(size_t NewCap) {
size_t S = size();
if (isInline()) {
auto *Tmp = static_cast<T *>(std::malloc(NewCap * sizeof(T)));
if (Tmp == nullptr)
std::abort();
std::copy(First, Last, Tmp);
First = Tmp;
} else {
First = static_cast<T *>(std::realloc(First, NewCap * sizeof(T)));
if (First == nullptr)
std::abort();
}
Last = First + S;
Cap = First + NewCap;
}
public:
PODSmallVector() : First(Inline), Last(First), Cap(Inline + N) {}
PODSmallVector(const PODSmallVector &) = delete;
PODSmallVector &operator=(const PODSmallVector &) = delete;
PODSmallVector(PODSmallVector &&Other) : PODSmallVector() {
if (Other.isInline()) {
std::copy(Other.begin(), Other.end(), First);
Last = First + Other.size();
Other.clear();
return;
}
First = Other.First;
Last = Other.Last;
Cap = Other.Cap;
Other.clearInline();
}
PODSmallVector &operator=(PODSmallVector &&Other) {
if (Other.isInline()) {
if (!isInline()) {
std::free(First);
clearInline();
}
std::copy(Other.begin(), Other.end(), First);
Last = First + Other.size();
Other.clear();
return *this;
}
if (isInline()) {
First = Other.First;
Last = Other.Last;
Cap = Other.Cap;
Other.clearInline();
return *this;
}
std::swap(First, Other.First);
std::swap(Last, Other.Last);
std::swap(Cap, Other.Cap);
Other.clear();
return *this;
}
// NOLINTNEXTLINE(readability-identifier-naming)
void push_back(const T &Elem) {
if (Last == Cap)
reserve(size() * 2);
*Last++ = Elem;
}
// NOLINTNEXTLINE(readability-identifier-naming)
void pop_back() {
DEMANGLE_ASSERT(Last != First, "Popping empty vector!");
--Last;
}
void shrinkToSize(size_t Index) {
DEMANGLE_ASSERT(Index <= size(), "shrinkToSize() can't expand!");
Last = First + Index;
}
T *begin() { return First; }
T *end() { return Last; }
bool empty() const { return First == Last; }
size_t size() const { return static_cast<size_t>(Last - First); }
T &back() {
DEMANGLE_ASSERT(Last != First, "Calling back() on empty vector!");
return *(Last - 1);
}
T &operator[](size_t Index) {
DEMANGLE_ASSERT(Index < size(), "Invalid access!");
return *(begin() + Index);
}
void clear() { Last = First; }
~PODSmallVector() {
if (!isInline())
std::free(First);
}
};
class NodeArray;
// Base class of all AST nodes. The AST is built by the parser, then is
// traversed by the printLeft/Right functions to produce a demangled string.
class Node {
public:
enum Kind : unsigned char {
#define NODE(NodeKind) K##NodeKind,
#include "ItaniumNodes.def"
};
/// Three-way bool to track a cached value. Unknown is possible if this node
/// has an unexpanded parameter pack below it that may affect this cache.
enum class Cache : unsigned char { Yes, No, Unknown, };
/// Operator precedence for expression nodes. Used to determine required
/// parens in expression emission.
enum class Prec {
Primary,
Postfix,
Unary,
Cast,
PtrMem,
Multiplicative,
Additive,
Shift,
Spaceship,
Relational,
Equality,
And,
Xor,
Ior,
AndIf,
OrIf,
Conditional,
Assign,
Comma,
Default,
};
private:
Kind K;
Prec Precedence : 6;
protected:
/// Tracks if this node has a component on its right side, in which case we
/// need to call printRight.
Cache RHSComponentCache : 2;
/// Track if this node is a (possibly qualified) array type. This can affect
/// how we format the output string.
Cache ArrayCache : 2;
/// Track if this node is a (possibly qualified) function type. This can
/// affect how we format the output string.
Cache FunctionCache : 2;
public:
Node(Kind K_, Prec Precedence_ = Prec::Primary,
Cache RHSComponentCache_ = Cache::No, Cache ArrayCache_ = Cache::No,
Cache FunctionCache_ = Cache::No)
: K(K_), Precedence(Precedence_), RHSComponentCache(RHSComponentCache_),
ArrayCache(ArrayCache_), FunctionCache(FunctionCache_) {}
Node(Kind K_, Cache RHSComponentCache_, Cache ArrayCache_ = Cache::No,
Cache FunctionCache_ = Cache::No)
: Node(K_, Prec::Primary, RHSComponentCache_, ArrayCache_,
FunctionCache_) {}
/// Visit the most-derived object corresponding to this object.
template<typename Fn> void visit(Fn F) const;
// The following function is provided by all derived classes:
//
// Call F with arguments that, when passed to the constructor of this node,
// would construct an equivalent node.
//template<typename Fn> void match(Fn F) const;
bool hasRHSComponent(OutputBuffer &OB) const {
if (RHSComponentCache != Cache::Unknown)
return RHSComponentCache == Cache::Yes;
return hasRHSComponentSlow(OB);
}
bool hasArray(OutputBuffer &OB) const {
if (ArrayCache != Cache::Unknown)
return ArrayCache == Cache::Yes;
return hasArraySlow(OB);
}
bool hasFunction(OutputBuffer &OB) const {
if (FunctionCache != Cache::Unknown)
return FunctionCache == Cache::Yes;
return hasFunctionSlow(OB);
}
Kind getKind() const { return K; }
Prec getPrecedence() const { return Precedence; }
Cache getRHSComponentCache() const { return RHSComponentCache; }
Cache getArrayCache() const { return ArrayCache; }
Cache getFunctionCache() const { return FunctionCache; }
virtual bool hasRHSComponentSlow(OutputBuffer &) const { return false; }
virtual bool hasArraySlow(OutputBuffer &) const { return false; }
virtual bool hasFunctionSlow(OutputBuffer &) const { return false; }
// Dig through "glue" nodes like ParameterPack and ForwardTemplateReference to
// get at a node that actually represents some concrete syntax.
virtual const Node *getSyntaxNode(OutputBuffer &) const { return this; }
// Print this node as an expression operand, surrounding it in parentheses if
// its precedence is [Strictly] weaker than P.
void printAsOperand(OutputBuffer &OB, Prec P = Prec::Default,
bool StrictlyWorse = false) const {
bool Paren =
unsigned(getPrecedence()) >= unsigned(P) + unsigned(StrictlyWorse);
if (Paren)
OB.printOpen();
print(OB);
if (Paren)
OB.printClose();
}
void print(OutputBuffer &OB) const {
printLeft(OB);
if (RHSComponentCache != Cache::No)
printRight(OB);
}
// Print the "left" side of this Node into OutputBuffer.
virtual void printLeft(OutputBuffer &) const = 0;
// Print the "right". This distinction is necessary to represent C++ types
// that appear on the RHS of their subtype, such as arrays or functions.
// Since most types don't have such a component, provide a default
// implementation.
virtual void printRight(OutputBuffer &) const {}
// Print an initializer list of this type. Returns true if we printed a custom
// representation, false if nothing has been printed and the default
// representation should be used.
virtual bool printInitListAsType(OutputBuffer &, const NodeArray &) const {
return false;
}
virtual std::string_view getBaseName() const { return {}; }
// Silence compiler warnings, this dtor will never be called.
virtual ~Node() = default;
#ifndef NDEBUG
DEMANGLE_DUMP_METHOD void dump() const;
#endif
};
class NodeArray {
Node **Elements;
size_t NumElements;
public:
NodeArray() : Elements(nullptr), NumElements(0) {}
NodeArray(Node **Elements_, size_t NumElements_)
: Elements(Elements_), NumElements(NumElements_) {}
bool empty() const { return NumElements == 0; }
size_t size() const { return NumElements; }
Node **begin() const { return Elements; }
Node **end() const { return Elements + NumElements; }
Node *operator[](size_t Idx) const { return Elements[Idx]; }
void printWithComma(OutputBuffer &OB) const {
bool FirstElement = true;
for (size_t Idx = 0; Idx != NumElements; ++Idx) {
size_t BeforeComma = OB.getCurrentPosition();
if (!FirstElement)
OB += ", ";
size_t AfterComma = OB.getCurrentPosition();
Elements[Idx]->printAsOperand(OB, Node::Prec::Comma);
// Elements[Idx] is an empty parameter pack expansion, we should erase the
// comma we just printed.
if (AfterComma == OB.getCurrentPosition()) {
OB.setCurrentPosition(BeforeComma);
continue;
}
FirstElement = false;
}
}
// Print an array of integer literals as a string literal. Returns whether we
// could do so.
bool printAsString(OutputBuffer &OB) const;
};
struct NodeArrayNode : Node {
NodeArray Array;
NodeArrayNode(NodeArray Array_) : Node(KNodeArrayNode), Array(Array_) {}
template<typename Fn> void match(Fn F) const { F(Array); }
void printLeft(OutputBuffer &OB) const override { Array.printWithComma(OB); }
};
class DotSuffix final : public Node {
const Node *Prefix;
const std::string_view Suffix;
public:
DotSuffix(const Node *Prefix_, std::string_view Suffix_)
: Node(KDotSuffix), Prefix(Prefix_), Suffix(Suffix_) {}
template<typename Fn> void match(Fn F) const { F(Prefix, Suffix); }
void printLeft(OutputBuffer &OB) const override {
Prefix->print(OB);
OB += " (";
OB += Suffix;
OB += ")";
}
};
class VendorExtQualType final : public Node {
const Node *Ty;
std::string_view Ext;
const Node *TA;
public:
VendorExtQualType(const Node *Ty_, std::string_view Ext_, const Node *TA_)
: Node(KVendorExtQualType), Ty(Ty_), Ext(Ext_), TA(TA_) {}
const Node *getTy() const { return Ty; }
std::string_view getExt() const { return Ext; }
const Node *getTA() const { return TA; }
template <typename Fn> void match(Fn F) const { F(Ty, Ext, TA); }
void printLeft(OutputBuffer &OB) const override {
Ty->print(OB);
OB += " ";
OB += Ext;
if (TA != nullptr)
TA->print(OB);
}
};
enum FunctionRefQual : unsigned char {
FrefQualNone,
FrefQualLValue,
FrefQualRValue,
};
enum Qualifiers {
QualNone = 0,
QualConst = 0x1,
QualVolatile = 0x2,
QualRestrict = 0x4,
};
inline Qualifiers operator|=(Qualifiers &Q1, Qualifiers Q2) {
return Q1 = static_cast<Qualifiers>(Q1 | Q2);
}
class QualType final : public Node {
protected:
const Qualifiers Quals;
const Node *Child;
void printQuals(OutputBuffer &OB) const {
if (Quals & QualConst)
OB += " const";
if (Quals & QualVolatile)
OB += " volatile";
if (Quals & QualRestrict)
OB += " restrict";
}
public:
QualType(const Node *Child_, Qualifiers Quals_)
: Node(KQualType, Child_->getRHSComponentCache(), Child_->getArrayCache(),
Child_->getFunctionCache()),
Quals(Quals_), Child(Child_) {}
Qualifiers getQuals() const { return Quals; }
const Node *getChild() const { return Child; }
template<typename Fn> void match(Fn F) const { F(Child, Quals); }
bool hasRHSComponentSlow(OutputBuffer &OB) const override {
return Child->hasRHSComponent(OB);
}
bool hasArraySlow(OutputBuffer &OB) const override {
return Child->hasArray(OB);
}
bool hasFunctionSlow(OutputBuffer &OB) const override {
return Child->hasFunction(OB);
}
void printLeft(OutputBuffer &OB) const override {
Child->printLeft(OB);
printQuals(OB);
}
void printRight(OutputBuffer &OB) const override { Child->printRight(OB); }
};
class ConversionOperatorType final : public Node {
const Node *Ty;
public:
ConversionOperatorType(const Node *Ty_)
: Node(KConversionOperatorType), Ty(Ty_) {}
template<typename Fn> void match(Fn F) const { F(Ty); }
void printLeft(OutputBuffer &OB) const override {
OB += "operator ";
Ty->print(OB);
}
};
class PostfixQualifiedType final : public Node {
const Node *Ty;
const std::string_view Postfix;
public:
PostfixQualifiedType(const Node *Ty_, std::string_view Postfix_)
: Node(KPostfixQualifiedType), Ty(Ty_), Postfix(Postfix_) {}
template<typename Fn> void match(Fn F) const { F(Ty, Postfix); }
void printLeft(OutputBuffer &OB) const override {
Ty->printLeft(OB);
OB += Postfix;
}
};
class NameType final : public Node {
const std::string_view Name;
public:
NameType(std::string_view Name_) : Node(KNameType), Name(Name_) {}
template<typename Fn> void match(Fn F) const { F(Name); }
std::string_view getName() const { return Name; }
std::string_view getBaseName() const override { return Name; }
void printLeft(OutputBuffer &OB) const override { OB += Name; }
};
class BitIntType final : public Node {
const Node *Size;
bool Signed;
public:
BitIntType(const Node *Size_, bool Signed_)
: Node(KBitIntType), Size(Size_), Signed(Signed_) {}
template <typename Fn> void match(Fn F) const { F(Size, Signed); }
void printLeft(OutputBuffer &OB) const override {
if (!Signed)
OB += "unsigned ";
OB += "_BitInt";
OB.printOpen();
Size->printAsOperand(OB);
OB.printClose();
}
};
class ElaboratedTypeSpefType : public Node {
std::string_view Kind;
Node *Child;
public:
ElaboratedTypeSpefType(std::string_view Kind_, Node *Child_)
: Node(KElaboratedTypeSpefType), Kind(Kind_), Child(Child_) {}
template<typename Fn> void match(Fn F) const { F(Kind, Child); }
void printLeft(OutputBuffer &OB) const override {
OB += Kind;
OB += ' ';
Child->print(OB);
}
};
class TransformedType : public Node {
std::string_view Transform;
Node *BaseType;
public:
TransformedType(std::string_view Transform_, Node *BaseType_)
: Node(KTransformedType), Transform(Transform_), BaseType(BaseType_) {}
template<typename Fn> void match(Fn F) const { F(Transform, BaseType); }
void printLeft(OutputBuffer &OB) const override {
OB += Transform;
OB += '(';
BaseType->print(OB);
OB += ')';
}
};
struct AbiTagAttr : Node {
Node *Base;
std::string_view Tag;
AbiTagAttr(Node *Base_, std::string_view Tag_)
: Node(KAbiTagAttr, Base_->getRHSComponentCache(), Base_->getArrayCache(),
Base_->getFunctionCache()),
Base(Base_), Tag(Tag_) {}
template<typename Fn> void match(Fn F) const { F(Base, Tag); }
std::string_view getBaseName() const override { return Base->getBaseName(); }
void printLeft(OutputBuffer &OB) const override {
Base->printLeft(OB);
OB += "[abi:";
OB += Tag;
OB += "]";
}
};
class EnableIfAttr : public Node {
NodeArray Conditions;
public:
EnableIfAttr(NodeArray Conditions_)
: Node(KEnableIfAttr), Conditions(Conditions_) {}
template<typename Fn> void match(Fn F) const { F(Conditions); }
void printLeft(OutputBuffer &OB) const override {
OB += " [enable_if:";
Conditions.printWithComma(OB);
OB += ']';
}
};
class ObjCProtoName : public Node {
const Node *Ty;
std::string_view Protocol;
friend class PointerType;
public:
ObjCProtoName(const Node *Ty_, std::string_view Protocol_)
: Node(KObjCProtoName), Ty(Ty_), Protocol(Protocol_) {}
template<typename Fn> void match(Fn F) const { F(Ty, Protocol); }
bool isObjCObject() const {
return Ty->getKind() == KNameType &&
static_cast<const NameType *>(Ty)->getName() == "objc_object";
}
void printLeft(OutputBuffer &OB) const override {
Ty->print(OB);
OB += "<";
OB += Protocol;
OB += ">";
}
};
class PointerType final : public Node {
const Node *Pointee;
public:
PointerType(const Node *Pointee_)
: Node(KPointerType, Pointee_->getRHSComponentCache()),
Pointee(Pointee_) {}
const Node *getPointee() const { return Pointee; }
template<typename Fn> void match(Fn F) const { F(Pointee); }
bool hasRHSComponentSlow(OutputBuffer &OB) const override {
return Pointee->hasRHSComponent(OB);
}
void printLeft(OutputBuffer &OB) const override {
// We rewrite objc_object<SomeProtocol>* into id<SomeProtocol>.
if (Pointee->getKind() != KObjCProtoName ||
!static_cast<const ObjCProtoName *>(Pointee)->isObjCObject()) {
Pointee->printLeft(OB);
if (Pointee->hasArray(OB))
OB += " ";
if (Pointee->hasArray(OB) || Pointee->hasFunction(OB))
OB += "(";
OB += "*";
} else {
const auto *objcProto = static_cast<const ObjCProtoName *>(Pointee);
OB += "id<";
OB += objcProto->Protocol;
OB += ">";
}
}
void printRight(OutputBuffer &OB) const override {
if (Pointee->getKind() != KObjCProtoName ||
!static_cast<const ObjCProtoName *>(Pointee)->isObjCObject()) {
if (Pointee->hasArray(OB) || Pointee->hasFunction(OB))
OB += ")";
Pointee->printRight(OB);
}
}
};
enum class ReferenceKind {
LValue,
RValue,
};
// Represents either a LValue or an RValue reference type.
class ReferenceType : public Node {
const Node *Pointee;
ReferenceKind RK;
mutable bool Printing = false;
// Dig through any refs to refs, collapsing the ReferenceTypes as we go. The
// rule here is rvalue ref to rvalue ref collapses to a rvalue ref, and any
// other combination collapses to a lvalue ref.
//
// A combination of a TemplateForwardReference and a back-ref Substitution
// from an ill-formed string may have created a cycle; use cycle detection to
// avoid looping forever.
std::pair<ReferenceKind, const Node *> collapse(OutputBuffer &OB) const {
auto SoFar = std::make_pair(RK, Pointee);
// Track the chain of nodes for the Floyd's 'tortoise and hare'
// cycle-detection algorithm, since getSyntaxNode(S) is impure
PODSmallVector<const Node *, 8> Prev;
for (;;) {
const Node *SN = SoFar.second->getSyntaxNode(OB);
if (SN->getKind() != KReferenceType)
break;
auto *RT = static_cast<const ReferenceType *>(SN);
SoFar.second = RT->Pointee;
SoFar.first = std::min(SoFar.first, RT->RK);
// The middle of Prev is the 'slow' pointer moving at half speed
Prev.push_back(SoFar.second);
if (Prev.size() > 1 && SoFar.second == Prev[(Prev.size() - 1) / 2]) {
// Cycle detected
SoFar.second = nullptr;
break;
}
}
return SoFar;
}
public:
ReferenceType(const Node *Pointee_, ReferenceKind RK_)
: Node(KReferenceType, Pointee_->getRHSComponentCache()),
Pointee(Pointee_), RK(RK_) {}
template<typename Fn> void match(Fn F) const { F(Pointee, RK); }
bool hasRHSComponentSlow(OutputBuffer &OB) const override {
return Pointee->hasRHSComponent(OB);
}
void printLeft(OutputBuffer &OB) const override {
if (Printing)
return;
ScopedOverride<bool> SavePrinting(Printing, true);
std::pair<ReferenceKind, const Node *> Collapsed = collapse(OB);
if (!Collapsed.second)
return;
Collapsed.second->printLeft(OB);
if (Collapsed.second->hasArray(OB))
OB += " ";
if (Collapsed.second->hasArray(OB) || Collapsed.second->hasFunction(OB))
OB += "(";
OB += (Collapsed.first == ReferenceKind::LValue ? "&" : "&&");
}
void printRight(OutputBuffer &OB) const override {
if (Printing)
return;
ScopedOverride<bool> SavePrinting(Printing, true);
std::pair<ReferenceKind, const Node *> Collapsed = collapse(OB);
if (!Collapsed.second)
return;
if (Collapsed.second->hasArray(OB) || Collapsed.second->hasFunction(OB))
OB += ")";
Collapsed.second->printRight(OB);
}
};
class PointerToMemberType final : public Node {
const Node *ClassType;
const Node *MemberType;
public:
PointerToMemberType(const Node *ClassType_, const Node *MemberType_)
: Node(KPointerToMemberType, MemberType_->getRHSComponentCache()),
ClassType(ClassType_), MemberType(MemberType_) {}
template<typename Fn> void match(Fn F) const { F(ClassType, MemberType); }
bool hasRHSComponentSlow(OutputBuffer &OB) const override {
return MemberType->hasRHSComponent(OB);
}
void printLeft(OutputBuffer &OB) const override {
MemberType->printLeft(OB);
if (MemberType->hasArray(OB) || MemberType->hasFunction(OB))
OB += "(";
else
OB += " ";
ClassType->print(OB);
OB += "::*";
}
void printRight(OutputBuffer &OB) const override {
if (MemberType->hasArray(OB) || MemberType->hasFunction(OB))
OB += ")";
MemberType->printRight(OB);
}
};
class ArrayType final : public Node {
const Node *Base;
Node *Dimension;
public:
ArrayType(const Node *Base_, Node *Dimension_)
: Node(KArrayType,
/*RHSComponentCache=*/Cache::Yes,
/*ArrayCache=*/Cache::Yes),
Base(Base_), Dimension(Dimension_) {}
template<typename Fn> void match(Fn F) const { F(Base, Dimension); }
bool hasRHSComponentSlow(OutputBuffer &) const override { return true; }
bool hasArraySlow(OutputBuffer &) const override { return true; }
void printLeft(OutputBuffer &OB) const override { Base->printLeft(OB); }
void printRight(OutputBuffer &OB) const override {
if (OB.back() != ']')
OB += " ";
OB += "[";
if (Dimension)
Dimension->print(OB);
OB += "]";
Base->printRight(OB);
}
bool printInitListAsType(OutputBuffer &OB,
const NodeArray &Elements) const override {
if (Base->getKind() == KNameType &&
static_cast<const NameType *>(Base)->getName() == "char") {
return Elements.printAsString(OB);
}
return false;
}
};
class FunctionType final : public Node {
const Node *Ret;
NodeArray Params;
Qualifiers CVQuals;
FunctionRefQual RefQual;
const Node *ExceptionSpec;
public:
FunctionType(const Node *Ret_, NodeArray Params_, Qualifiers CVQuals_,
FunctionRefQual RefQual_, const Node *ExceptionSpec_)
: Node(KFunctionType,
/*RHSComponentCache=*/Cache::Yes, /*ArrayCache=*/Cache::No,
/*FunctionCache=*/Cache::Yes),
Ret(Ret_), Params(Params_), CVQuals(CVQuals_), RefQual(RefQual_),
ExceptionSpec(ExceptionSpec_) {}
template<typename Fn> void match(Fn F) const {
F(Ret, Params, CVQuals, RefQual, ExceptionSpec);
}
bool hasRHSComponentSlow(OutputBuffer &) const override { return true; }
bool hasFunctionSlow(OutputBuffer &) const override { return true; }
// Handle C++'s ... quirky decl grammar by using the left & right
// distinction. Consider:
// int (*f(float))(char) {}
// f is a function that takes a float and returns a pointer to a function
// that takes a char and returns an int. If we're trying to print f, start
// by printing out the return types's left, then print our parameters, then
// finally print right of the return type.
void printLeft(OutputBuffer &OB) const override {
Ret->printLeft(OB);
OB += " ";
}
void printRight(OutputBuffer &OB) const override {
OB.printOpen();
Params.printWithComma(OB);
OB.printClose();
Ret->printRight(OB);
if (CVQuals & QualConst)
OB += " const";
if (CVQuals & QualVolatile)
OB += " volatile";
if (CVQuals & QualRestrict)
OB += " restrict";
if (RefQual == FrefQualLValue)
OB += " &";
else if (RefQual == FrefQualRValue)
OB += " &&";
if (ExceptionSpec != nullptr) {
OB += ' ';
ExceptionSpec->print(OB);
}
}
};
class NoexceptSpec : public Node {
const Node *E;
public:
NoexceptSpec(const Node *E_) : Node(KNoexceptSpec), E(E_) {}
template<typename Fn> void match(Fn F) const { F(E); }
void printLeft(OutputBuffer &OB) const override {
OB += "noexcept";
OB.printOpen();
E->printAsOperand(OB);
OB.printClose();
}
};
class DynamicExceptionSpec : public Node {
NodeArray Types;
public:
DynamicExceptionSpec(NodeArray Types_)
: Node(KDynamicExceptionSpec), Types(Types_) {}
template<typename Fn> void match(Fn F) const { F(Types); }
void printLeft(OutputBuffer &OB) const override {
OB += "throw";
OB.printOpen();
Types.printWithComma(OB);
OB.printClose();
}
};
/// Represents the explicitly named object parameter.
/// E.g.,
/// \code{.cpp}
/// struct Foo {
/// void bar(this Foo && self);
/// };
/// \endcode
class ExplicitObjectParameter final : public Node {
Node *Base;
public:
ExplicitObjectParameter(Node *Base_)
: Node(KExplicitObjectParameter), Base(Base_) {
DEMANGLE_ASSERT(
Base != nullptr,
"Creating an ExplicitObjectParameter without a valid Base Node.");
}
template <typename Fn> void match(Fn F) const { F(Base); }
void printLeft(OutputBuffer &OB) const override {
OB += "this ";
Base->print(OB);
}
};
class FunctionEncoding final : public Node {
const Node *Ret;
const Node *Name;
NodeArray Params;
const Node *Attrs;
const Node *Requires;
Qualifiers CVQuals;
FunctionRefQual RefQual;
public:
FunctionEncoding(const Node *Ret_, const Node *Name_, NodeArray Params_,
const Node *Attrs_, const Node *Requires_,
Qualifiers CVQuals_, FunctionRefQual RefQual_)
: Node(KFunctionEncoding,
/*RHSComponentCache=*/Cache::Yes, /*ArrayCache=*/Cache::No,
/*FunctionCache=*/Cache::Yes),
Ret(Ret_), Name(Name_), Params(Params_), Attrs(Attrs_),
Requires(Requires_), CVQuals(CVQuals_), RefQual(RefQual_) {}
template<typename Fn> void match(Fn F) const {
F(Ret, Name, Params, Attrs, Requires, CVQuals, RefQual);
}
Qualifiers getCVQuals() const { return CVQuals; }
FunctionRefQual getRefQual() const { return RefQual; }
NodeArray getParams() const { return Params; }
const Node *getReturnType() const { return Ret; }
bool hasRHSComponentSlow(OutputBuffer &) const override { return true; }
bool hasFunctionSlow(OutputBuffer &) const override { return true; }
const Node *getName() const { return Name; }
void printLeft(OutputBuffer &OB) const override {
if (Ret) {
Ret->printLeft(OB);
if (!Ret->hasRHSComponent(OB))
OB += " ";
}
Name->print(OB);
}
void printRight(OutputBuffer &OB) const override {
OB.printOpen();
Params.printWithComma(OB);
OB.printClose();
if (Ret)
Ret->printRight(OB);
if (CVQuals & QualConst)
OB += " const";
if (CVQuals & QualVolatile)
OB += " volatile";
if (CVQuals & QualRestrict)
OB += " restrict";
if (RefQual == FrefQualLValue)
OB += " &";
else if (RefQual == FrefQualRValue)