forked from sorbet/sorbet
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathTrees.h
1176 lines (879 loc) · 33.1 KB
/
Trees.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
#ifndef SORBET_TREES_H
#define SORBET_TREES_H
#include "common/common.h"
#include "core/Context.h"
#include "core/LocalVariable.h"
#include "core/SymbolRef.h"
#include "core/Types.h"
#include <memory>
#include <vector>
//
// This file defines the IR that most of the middle phases of Sorbet operate on
// and manipulate. It aims to be a middle ground between the parser output
// (very verbose and fine grained) and the CFG data structure (very easy to
// typecheck but very hard to do ad-hoc transformations on).
//
// This IR is best learned by example. Try using the `--print` option to sorbet
// on a handful of test/testdata files. Since there are multiple phases that
// return this IR, there are multiple valid print options which will show you
// an ast::Expression.
//
// Another good way to discover things is to grep for the class name in the
// various *-raw.exp snapshot tests to fine a test file that uses that node.
// Keep in mind that this IR is meant to be somewhat coarse grained, so one
// node type can likely have been created from multiple Ruby constructs.
//
namespace sorbet {
class WorkerPool;
}
namespace sorbet::ast {
enum class Tag {
EmptyTree = 1,
Send,
ClassDef,
MethodDef,
If,
While,
Break,
Retry,
Next,
Return,
RescueCase,
Rescue,
Local,
UnresolvedIdent,
RestArg,
KeywordArg,
OptionalArg,
BlockArg,
ShadowArg,
Assign,
Cast,
Hash,
Array,
Literal,
UnresolvedConstantLit,
ConstantLit,
ZSuperArgs,
Block,
InsSeq,
};
// A mapping from tree type to its corresponding tag.
template <typename T> struct ExpressionToTag;
class ExpressionPtr {
public:
// We store tagged pointers as 64-bit values.
using tagged_storage = uint64_t;
// Required for typecase
template <class To> static bool isa(const ExpressionPtr &tree);
template <class To> static const To &cast(const ExpressionPtr &tree);
template <class To> static To &cast(ExpressionPtr &tree) {
return const_cast<To &>(cast<To>(static_cast<const ExpressionPtr &>(tree)));
}
private:
static constexpr tagged_storage TAG_MASK = 0xffff;
static constexpr tagged_storage PTR_MASK = ~TAG_MASK;
tagged_storage ptr;
template <typename E, typename... Args> friend ExpressionPtr make_expression(Args &&...);
static tagged_storage tagPtr(Tag tag, void *expr) {
// Store the tag in the lower 16 bits of the pointer, regardless of size.
auto val = static_cast<tagged_storage>(tag);
auto maskedPtr = reinterpret_cast<tagged_storage>(expr) << 16;
return maskedPtr | val;
}
ExpressionPtr(Tag tag, void *expr) : ptr(tagPtr(tag, expr)) {}
static void deleteTagged(Tag tag, void *ptr) noexcept;
// A version of release that doesn't mask the tag bits
tagged_storage releaseTagged() noexcept {
auto saved = ptr;
ptr = 0;
return saved;
}
// A version of reset that expects the tagged bits to be set.
void resetTagged(tagged_storage expr) noexcept {
Tag tagVal;
void *saved = nullptr;
if (ptr != 0) {
tagVal = tag();
saved = get();
}
ptr = expr;
if (saved != nullptr) {
deleteTagged(tagVal, saved);
}
}
public:
constexpr ExpressionPtr() noexcept : ptr(0) {}
ExpressionPtr(std::nullptr_t) noexcept : ExpressionPtr() {}
// Construction from a tagged pointer. This is needed for:
// * ResolveConstantsWalk::isFullyResolved
explicit ExpressionPtr(tagged_storage ptr) : ptr(ptr) {}
~ExpressionPtr() {
if (ptr != 0) {
deleteTagged(tag(), get());
}
}
ExpressionPtr(const ExpressionPtr &) = delete;
ExpressionPtr &operator=(const ExpressionPtr &) = delete;
ExpressionPtr(ExpressionPtr &&other) noexcept {
ptr = other.releaseTagged();
}
ExpressionPtr &operator=(ExpressionPtr &&other) noexcept {
if (*this == other) {
return *this;
}
resetTagged(other.releaseTagged());
return *this;
}
void *release() noexcept {
auto *saved = get();
ptr = 0;
return saved;
}
void reset() noexcept {
resetTagged(0);
}
void reset(std::nullptr_t) noexcept {
resetTagged(0);
}
template <typename T> void reset(T *expr = nullptr) noexcept {
resetTagged(tagPtr(ExpressionToTag<T>::value, expr));
}
Tag tag() const noexcept {
ENFORCE(ptr != 0);
auto value = reinterpret_cast<tagged_storage>(ptr) & TAG_MASK;
return static_cast<Tag>(value);
}
void *get() const noexcept {
auto val = ptr & PTR_MASK;
return reinterpret_cast<void *>(val >> 16);
}
// Fetch the tagged pointer. This is needed for:
// * ResolveConstantsWalk::isFullyResolved
tagged_storage getTagged() const noexcept {
return ptr;
}
explicit operator bool() const noexcept {
return get() != nullptr;
}
bool operator==(const ExpressionPtr &other) const noexcept {
return get() == other.get();
}
bool operator!=(const ExpressionPtr &other) const noexcept {
return get() != other.get();
}
ExpressionPtr deepCopy() const;
std::string nodeName() const;
std::string showRaw(const core::GlobalState &gs, int tabs = 0);
bool isSelfReference() const;
void _sanityCheck() const;
core::LocOffsets loc() const;
std::string toStringWithTabs(const core::GlobalState &gs, int tabs = 0) const;
std::string toString(const core::GlobalState &gs) const {
return toStringWithTabs(gs);
}
};
template <class E, typename... Args> ExpressionPtr make_expression(Args &&...args) {
return ExpressionPtr(ExpressionToTag<E>::value, new E(std::forward<Args>(args)...));
}
struct ParsedFile {
ExpressionPtr tree;
core::FileRef file;
};
/**
* Stores a vector of `ParsedFile`s. May be empty if pass was canceled or encountered an error.
* TODO: Modify to store reason if we ever have multiple reasons for a pass to stop. Currently, it's only empty if the
* pass is canceled in LSP mode.
*/
class ParsedFilesOrCancelled final {
private:
std::optional<std::vector<ParsedFile>> trees;
ParsedFilesOrCancelled();
public:
ParsedFilesOrCancelled(std::vector<ParsedFile> &&trees);
// Produces a `ParsedFilesOrCancelled` in the cancelled state.
// Trees passed to this function will be destructed in parallel with `workers`, which improves LSP responsiveness on
// large projects.
static ParsedFilesOrCancelled cancel(std::vector<ParsedFile> &&trees, WorkerPool &workers);
bool hasResult() const;
std::vector<ParsedFile> &result();
};
template <class To> bool isa_tree(const ExpressionPtr &what) {
return what != nullptr && what.tag() == ExpressionToTag<To>::value;
}
bool isa_reference(const ExpressionPtr &what);
bool isa_declaration(const ExpressionPtr &what);
template <class To> To *cast_tree(ExpressionPtr &what) {
if (isa_tree<To>(what)) {
return reinterpret_cast<To *>(what.get());
} else {
return nullptr;
}
}
template <class To> const To *cast_tree(const ExpressionPtr &what) {
if (isa_tree<To>(what)) {
return reinterpret_cast<To *>(what.get());
} else {
return nullptr;
}
}
// We disallow casting on temporary values because the lifetime of the returned value is
// tied to the temporary, but it is possible for the temporary to be destroyed at the end
// of the current statement, leading to use-after-free bugs.
template <class To> To *cast_tree(ExpressionPtr &&what) = delete;
template <class To> To &cast_tree_nonnull(ExpressionPtr &what) {
ENFORCE(isa_tree<To>(what), "cast_tree_nonnull failed!");
return *reinterpret_cast<To *>(what.get());
}
template <class To> const To &cast_tree_nonnull(const ExpressionPtr &what) {
ENFORCE(isa_tree<To>(what), "cast_tree_nonnull failed!");
return *reinterpret_cast<To *>(what.get());
}
// We disallow casting on temporary values because the lifetime of the returned value is
// tied to the temporary, but it is possible for the temporary to be destroyed at the end
// of the current statement, leading to use-after-free bugs.
template <class To> To *cast_tree_nonnull(ExpressionPtr &&what) = delete;
template <class To> inline bool ExpressionPtr::isa(const ExpressionPtr &what) {
return isa_tree<To>(what);
}
template <class To> inline To const &ExpressionPtr::cast(const ExpressionPtr &what) {
return cast_tree_nonnull<To>(what);
}
template <> inline bool ExpressionPtr::isa<ExpressionPtr>(const ExpressionPtr &tree) {
return true;
}
template <> inline const ExpressionPtr &ExpressionPtr::cast<ExpressionPtr>(const ExpressionPtr &tree) {
return tree;
}
#define EXPRESSION(name) \
class name; \
template <> struct ExpressionToTag<name> { static constexpr Tag value = Tag::name; }; \
class __attribute__((aligned(8))) name final
EXPRESSION(ClassDef) {
public:
const core::LocOffsets loc;
core::LocOffsets declLoc;
core::ClassOrModuleRef symbol;
enum class Kind : uint8_t {
Module,
Class,
};
Kind kind;
static constexpr int EXPECTED_RHS_COUNT = 4;
using RHS_store = InlinedVector<ExpressionPtr, EXPECTED_RHS_COUNT>;
RHS_store rhs;
ExpressionPtr name;
// For unresolved names. Once they are typeAlias to Symbols they go into the Symbol
static constexpr int EXPECTED_ANCESTORS_COUNT = 2;
using ANCESTORS_store = InlinedVector<ExpressionPtr, EXPECTED_ANCESTORS_COUNT>;
ANCESTORS_store ancestors;
ANCESTORS_store singletonAncestors;
ClassDef(core::LocOffsets loc, core::LocOffsets declLoc, core::ClassOrModuleRef symbol, ExpressionPtr name,
ANCESTORS_store ancestors, RHS_store rhs, ClassDef::Kind kind);
ExpressionPtr deepCopy() const;
std::string toStringWithTabs(const core::GlobalState &gs, int tabs = 0) const;
std::string showRaw(const core::GlobalState &gs, int tabs = 0);
std::string nodeName();
void _sanityCheck();
};
CheckSize(ClassDef, 120, 8);
EXPRESSION(MethodDef) {
public:
const core::LocOffsets loc;
core::LocOffsets declLoc;
core::MethodRef symbol;
ExpressionPtr rhs;
using ARGS_store = InlinedVector<ExpressionPtr, core::SymbolRef::EXPECTED_METHOD_ARGS_COUNT>;
ARGS_store args;
core::NameRef name;
struct Flags {
bool isSelfMethod : 1;
bool isRewriterSynthesized : 1;
bool isAttrReader : 1;
bool discardDef : 1;
bool genericPropGetter : 1;
// In C++20 we can replace this with bit field initialzers
Flags()
: isSelfMethod(false), isRewriterSynthesized(false), isAttrReader(false), discardDef(false),
genericPropGetter(false) {}
};
CheckSize(Flags, 1, 1);
Flags flags;
MethodDef(core::LocOffsets loc, core::LocOffsets declLoc, core::MethodRef symbol, core::NameRef name,
ARGS_store args, ExpressionPtr rhs, Flags flags);
ExpressionPtr deepCopy() const;
std::string toStringWithTabs(const core::GlobalState &gs, int tabs = 0) const;
std::string showRaw(const core::GlobalState &gs, int tabs = 0);
std::string nodeName();
void _sanityCheck();
};
CheckSize(MethodDef, 64, 8);
EXPRESSION(If) {
public:
const core::LocOffsets loc;
ExpressionPtr cond;
ExpressionPtr thenp;
ExpressionPtr elsep;
If(core::LocOffsets loc, ExpressionPtr cond, ExpressionPtr thenp, ExpressionPtr elsep);
ExpressionPtr deepCopy() const;
std::string toStringWithTabs(const core::GlobalState &gs, int tabs = 0) const;
std::string showRaw(const core::GlobalState &gs, int tabs = 0);
std::string nodeName();
void _sanityCheck();
};
CheckSize(If, 32, 8);
EXPRESSION(While) {
public:
const core::LocOffsets loc;
ExpressionPtr cond;
ExpressionPtr body;
While(core::LocOffsets loc, ExpressionPtr cond, ExpressionPtr body);
ExpressionPtr deepCopy() const;
std::string toStringWithTabs(const core::GlobalState &gs, int tabs = 0) const;
std::string showRaw(const core::GlobalState &gs, int tabs = 0);
std::string nodeName();
void _sanityCheck();
};
CheckSize(While, 24, 8);
EXPRESSION(Break) {
public:
const core::LocOffsets loc;
ExpressionPtr expr;
Break(core::LocOffsets loc, ExpressionPtr expr);
ExpressionPtr deepCopy() const;
std::string toStringWithTabs(const core::GlobalState &gs, int tabs = 0) const;
std::string showRaw(const core::GlobalState &gs, int tabs = 0);
std::string nodeName();
void _sanityCheck();
};
CheckSize(Break, 16, 8);
EXPRESSION(Retry) {
public:
const core::LocOffsets loc;
Retry(core::LocOffsets loc);
ExpressionPtr deepCopy() const;
std::string toStringWithTabs(const core::GlobalState &gs, int tabs = 0) const;
std::string showRaw(const core::GlobalState &gs, int tabs = 0);
std::string nodeName();
void _sanityCheck();
};
CheckSize(Retry, 8, 8);
EXPRESSION(Next) {
public:
const core::LocOffsets loc;
ExpressionPtr expr;
Next(core::LocOffsets loc, ExpressionPtr expr);
ExpressionPtr deepCopy() const;
std::string toStringWithTabs(const core::GlobalState &gs, int tabs = 0) const;
std::string showRaw(const core::GlobalState &gs, int tabs = 0);
std::string nodeName();
void _sanityCheck();
};
CheckSize(Next, 16, 8);
EXPRESSION(Return) {
public:
const core::LocOffsets loc;
ExpressionPtr expr;
Return(core::LocOffsets loc, ExpressionPtr expr);
ExpressionPtr deepCopy() const;
std::string toStringWithTabs(const core::GlobalState &gs, int tabs = 0) const;
std::string showRaw(const core::GlobalState &gs, int tabs = 0);
std::string nodeName();
void _sanityCheck();
};
CheckSize(Return, 16, 8);
EXPRESSION(RescueCase) {
public:
const core::LocOffsets loc;
static constexpr int EXPECTED_EXCEPTION_COUNT = 2;
using EXCEPTION_store = InlinedVector<ExpressionPtr, EXPECTED_EXCEPTION_COUNT>;
EXCEPTION_store exceptions;
// If present, var is always an UnresolvedIdent[kind=Local] up until the
// namer, at which point it is a Local.
ExpressionPtr var;
ExpressionPtr body;
RescueCase(core::LocOffsets loc, EXCEPTION_store exceptions, ExpressionPtr var, ExpressionPtr body);
ExpressionPtr deepCopy() const;
std::string toStringWithTabs(const core::GlobalState &gs, int tabs = 0) const;
std::string showRaw(const core::GlobalState &gs, int tabs = 0);
std::string nodeName();
void _sanityCheck();
};
CheckSize(RescueCase, 48, 8);
EXPRESSION(Rescue) {
public:
const core::LocOffsets loc;
static constexpr int EXPECTED_RESCUE_CASE_COUNT = 2;
using RESCUE_CASE_store = InlinedVector<ExpressionPtr, EXPECTED_RESCUE_CASE_COUNT>;
ExpressionPtr body;
RESCUE_CASE_store rescueCases;
ExpressionPtr else_;
ExpressionPtr ensure;
Rescue(core::LocOffsets loc, ExpressionPtr body, RESCUE_CASE_store rescueCases, ExpressionPtr else_,
ExpressionPtr ensure);
ExpressionPtr deepCopy() const;
std::string toStringWithTabs(const core::GlobalState &gs, int tabs = 0) const;
std::string showRaw(const core::GlobalState &gs, int tabs = 0);
std::string nodeName();
void _sanityCheck();
};
CheckSize(Rescue, 56, 8);
EXPRESSION(Local) {
public:
const core::LocOffsets loc;
core::LocalVariable localVariable;
Local(core::LocOffsets loc, core::LocalVariable localVariable1);
ExpressionPtr deepCopy() const;
std::string toStringWithTabs(const core::GlobalState &gs, int tabs = 0) const;
std::string showRaw(const core::GlobalState &gs, int tabs = 0);
std::string nodeName();
void _sanityCheck();
};
CheckSize(Local, 16, 8);
EXPRESSION(UnresolvedIdent) {
public:
const core::LocOffsets loc;
enum class Kind : uint8_t {
Local,
Instance,
Class,
Global,
};
core::NameRef name;
Kind kind;
UnresolvedIdent(core::LocOffsets loc, Kind kind, core::NameRef name);
ExpressionPtr deepCopy() const;
std::string toStringWithTabs(const core::GlobalState &gs, int tabs = 0) const;
std::string showRaw(const core::GlobalState &gs, int tabs = 0);
std::string nodeName();
void _sanityCheck();
};
CheckSize(UnresolvedIdent, 16, 8);
EXPRESSION(RestArg) {
public:
const core::LocOffsets loc;
ExpressionPtr expr;
RestArg(core::LocOffsets loc, ExpressionPtr arg);
ExpressionPtr deepCopy() const;
std::string toStringWithTabs(const core::GlobalState &gs, int tabs = 0) const;
std::string showRaw(const core::GlobalState &gs, int tabs = 0);
std::string nodeName();
void _sanityCheck();
};
CheckSize(RestArg, 16, 8);
EXPRESSION(KeywordArg) {
public:
const core::LocOffsets loc;
ExpressionPtr expr;
KeywordArg(core::LocOffsets loc, ExpressionPtr expr);
ExpressionPtr deepCopy() const;
std::string toStringWithTabs(const core::GlobalState &gs, int tabs = 0) const;
std::string showRaw(const core::GlobalState &gs, int tabs = 0);
std::string nodeName();
void _sanityCheck();
};
CheckSize(KeywordArg, 16, 8);
EXPRESSION(OptionalArg) {
public:
const core::LocOffsets loc;
ExpressionPtr expr;
ExpressionPtr default_;
OptionalArg(core::LocOffsets loc, ExpressionPtr expr, ExpressionPtr default_);
ExpressionPtr deepCopy() const;
std::string toStringWithTabs(const core::GlobalState &gs, int tabs = 0) const;
std::string showRaw(const core::GlobalState &gs, int tabs = 0);
std::string nodeName();
void _sanityCheck();
};
CheckSize(OptionalArg, 24, 8);
EXPRESSION(BlockArg) {
public:
const core::LocOffsets loc;
ExpressionPtr expr;
BlockArg(core::LocOffsets loc, ExpressionPtr expr);
ExpressionPtr deepCopy() const;
std::string toStringWithTabs(const core::GlobalState &gs, int tabs = 0) const;
std::string showRaw(const core::GlobalState &gs, int tabs = 0);
std::string nodeName();
void _sanityCheck();
};
CheckSize(BlockArg, 16, 8);
EXPRESSION(ShadowArg) {
public:
const core::LocOffsets loc;
ExpressionPtr expr;
ShadowArg(core::LocOffsets loc, ExpressionPtr expr);
ExpressionPtr deepCopy() const;
std::string toStringWithTabs(const core::GlobalState &gs, int tabs = 0) const;
std::string showRaw(const core::GlobalState &gs, int tabs = 0);
std::string nodeName();
void _sanityCheck();
};
CheckSize(ShadowArg, 16, 8);
EXPRESSION(Assign) {
public:
const core::LocOffsets loc;
ExpressionPtr lhs;
ExpressionPtr rhs;
Assign(core::LocOffsets loc, ExpressionPtr lhs, ExpressionPtr rhs);
ExpressionPtr deepCopy() const;
std::string toStringWithTabs(const core::GlobalState &gs, int tabs = 0) const;
std::string showRaw(const core::GlobalState &gs, int tabs = 0);
std::string nodeName();
void _sanityCheck();
};
CheckSize(Assign, 24, 8);
class Block;
EXPRESSION(Send) {
public:
const core::LocOffsets loc;
core::NameRef fun;
const core::LocOffsets funLoc;
struct Flags {
bool isPrivateOk : 1;
bool isRewriterSynthesized : 1;
bool hasBlock : 1;
// In C++20 we can replace this with bit field initialzers
Flags() : isPrivateOk(false), isRewriterSynthesized(false), hasBlock(false) {}
};
CheckSize(Flags, 1, 1);
Flags flags;
private:
uint16_t numPosArgs_;
public:
ExpressionPtr recv;
static constexpr int EXPECTED_ARGS_COUNT = 2;
using ARGS_store = InlinedVector<ExpressionPtr, EXPECTED_ARGS_COUNT>;
private:
// The arguments vector has the following layout:
//
// for n = numPosArgs, m = number of keyword arg pairs
//
// +--------------------------+-------------------------------+------------------+----------------+
// | positional arguments | interleaved keyword arg pairs | optional kwsplat | optional block |
// +--------------------------+-------------------------------+------------------+----------------+
// | pos_0, ... , pos_(n - 1) | sym_0, val_0, .. sym_m, val_m | value | value |
// +--------------------------+-------------------------------+------------------+----------------+
//
// for the following send:
//
// > foo(a, b, c: 10, d: nil, &blk)
//
// the arguments vector would look like the following, with numPosArgs = 2:
//
// > <a, b, c, 10, d, nil, &blk>
//
// We make the args store private to avoid code that is tightly bound to this layout.
ARGS_store args;
public:
Send(core::LocOffsets loc, ExpressionPtr recv, core::NameRef fun, core::LocOffsets funLoc, uint16_t numPosArgs,
ARGS_store args, Flags flags = {});
ExpressionPtr deepCopy() const;
// Returns nullptr if no block present.
const ast::Block *block() const;
ast::Block *block();
// Returns nullptr if no block present.
const ExpressionPtr *rawBlock() const;
ExpressionPtr *rawBlock();
// Returns null if no kwsplat present.
const ExpressionPtr *kwSplat() const;
ExpressionPtr *kwSplat();
void setBlock(ExpressionPtr block);
void setKwSplat(ExpressionPtr splat);
// Add the given keyword argument to the end of the list of keyword arguments.
void addKwArg(ExpressionPtr key, ExpressionPtr value);
std::string toStringWithTabs(const core::GlobalState &gs, int tabs = 0) const;
std::string showRaw(const core::GlobalState &gs, int tabs = 0);
std::string nodeName();
// Add the given positional argument as the last positional argument.
void addPosArg(ExpressionPtr ptr);
// Insert the given positional argument at the given position, shifting existing arguments over.
void insertPosArg(uint16_t index, ExpressionPtr arg);
// Removes the position argument at the given index.
void removePosArg(uint16_t index);
// Reserve space for the given number of arguments.
void reserveArguments(size_t posArgs, size_t kwArgs, bool hasSplat, bool hasBlock) {
this->args.reserve(posArgs + (kwArgs * 2) + (hasSplat ? 1 : 0) + (hasBlock ? 1 : 0));
}
// Returns the raw arguments vector. Please avoid using unless absolutely necessary; it is easier to query
// arguments via methods on Send. It is a footgun.
const ARGS_store &rawArgsDoNotUse() const {
return args;
}
// Returns a loc covering the arguments in the send. Does not cover the block argument.
core::LocOffsets argsLoc() const;
uint16_t numPosArgs() const {
return numPosArgs_;
}
// The number of keyword arguments in the Send.
uint16_t numKwArgs() const {
uint16_t range = args.size() - numPosArgs_ - (hasKwSplat() ? 1 : 0) - (hasBlock() ? 1 : 0);
ENFORCE(range % 2 == 0);
return range / 2;
}
// Get the ith positional argument.
ExpressionPtr &getPosArg(uint16_t idx) {
ENFORCE(idx < numPosArgs_);
return args[idx];
}
const ExpressionPtr &getPosArg(uint16_t idx) const {
ENFORCE(idx < numPosArgs_);
return args[idx];
}
absl::Span<const ExpressionPtr> posArgs() const {
return absl::MakeSpan(args.begin(), numPosArgs_);
}
absl::Span<ExpressionPtr> posArgs() {
return absl::MakeSpan(args.begin(), numPosArgs_);
}
absl::Span<const ExpressionPtr> nonBlockArgs() const {
return absl::MakeSpan(args.begin(), numNonBlockArgs());
}
absl::Span<ExpressionPtr> nonBlockArgs() {
return absl::MakeSpan(args.begin(), numNonBlockArgs());
}
// Remove all arguments to the function, including the block argument.
void clearArgs();
// Get the ith keyword argument key. Indices begin at 0, regardless of the number of positional arguments.
ExpressionPtr &getKwKey(uint16_t argnum) {
ENFORCE(argnum < numKwArgs());
auto rawIdx = numPosArgs_ + (argnum * 2);
return args[rawIdx];
}
const ExpressionPtr &getKwKey(uint16_t argnum) const {
ENFORCE(argnum < numKwArgs());
auto rawIdx = numPosArgs_ + (argnum * 2);
return args[rawIdx];
}
// Get the ith keyword argument value. Indices begin at 0, regardless of the number of positional arguments.
ExpressionPtr &getKwValue(uint16_t argnum) {
ENFORCE(argnum < numKwArgs());
auto rawIdx = numPosArgs_ + (argnum * 2) + 1;
return args[rawIdx];
}
const ExpressionPtr &getKwValue(uint16_t argnum) const {
ENFORCE(argnum < numKwArgs());
auto rawIdx = numPosArgs_ + (argnum * 2) + 1;
return args[rawIdx];
}
// True when this send contains a block argument.
bool hasBlock() const {
return flags.hasBlock;
}
// True when this send contains at least 1 position argument.
bool hasPosArgs() const {
return numPosArgs_ > 0;
}
// True when there are either keyword args, or a keyword splat.
bool hasKwArgs() const {
return args.size() > (numPosArgs_ + (hasBlock() ? 1 : 0));
}
// True when there is a keyword args splat present. hasKwSplat -> hasKwArgs, but not the other way around.
bool hasKwSplat() const {
return (args.size() - numPosArgs_ - (hasBlock() ? 1 : 0)) & 0x1;
}
// True when this send contains at least one of the following:
// - a positional argument
// - a keyword argument
// - a keyword splat
bool hasNonBlockArgs() const {
return hasPosArgs() || hasKwArgs();
}
// Returns the number of non-block arguments. Keyword arguments are represented as two separate arguments (key and
// value).
uint16_t numNonBlockArgs() const {
return numPosArgs_ + (numKwArgs() * 2) + (hasKwSplat() ? 1 : 0);
}
const ExpressionPtr &getNonBlockArg(uint16_t idx) const {
ENFORCE(idx < args.size() - (hasBlock() ? 1 : 0));
return args[idx];
}
ExpressionPtr &getNonBlockArg(uint16_t idx) {
ENFORCE(idx < args.size() - (hasBlock() ? 1 : 0));
return args[idx];
}
// Returns a new ast::Send with a different loc, receiver, and function.
// _Moves_ the arguments from this Send into the new Send.
// The original send turns into a Send with 0 arguments.
ExpressionPtr withNewBody(core::LocOffsets loc, ExpressionPtr recv, core::NameRef fun);
void _sanityCheck();
};
CheckSize(Send, 56, 8);
EXPRESSION(Cast) {
public:
const core::LocOffsets loc;
// The name of the cast operator.
core::NameRef cast;
core::TypePtr type;
ExpressionPtr arg;
Cast(core::LocOffsets loc, core::TypePtr ty, ExpressionPtr arg, core::NameRef cast);
ExpressionPtr deepCopy() const;
std::string toStringWithTabs(const core::GlobalState &gs, int tabs = 0) const;
std::string showRaw(const core::GlobalState &gs, int tabs = 0);
std::string nodeName();
void _sanityCheck();
};
CheckSize(Cast, 40, 8);
EXPRESSION(Hash) {
public:
const core::LocOffsets loc;
static constexpr int EXPECTED_ENTRY_COUNT = 2;
using ENTRY_store = InlinedVector<ExpressionPtr, EXPECTED_ENTRY_COUNT>;
ENTRY_store keys;
ENTRY_store values;
Hash(core::LocOffsets loc, ENTRY_store keys, ENTRY_store values);
ExpressionPtr deepCopy() const;
std::string toStringWithTabs(const core::GlobalState &gs, int tabs = 0) const;
std::string showRaw(const core::GlobalState &gs, int tabs = 0);
std::string nodeName();
void _sanityCheck();
};
CheckSize(Hash, 56, 8);
EXPRESSION(Array) {
public:
const core::LocOffsets loc;
static constexpr int EXPECTED_ENTRY_COUNT = 4;
using ENTRY_store = InlinedVector<ExpressionPtr, EXPECTED_ENTRY_COUNT>;
ENTRY_store elems;
Array(core::LocOffsets loc, ENTRY_store elems);
ExpressionPtr deepCopy() const;
std::string toStringWithTabs(const core::GlobalState &gs, int tabs = 0) const;
std::string showRaw(const core::GlobalState &gs, int tabs = 0);
std::string nodeName();
void _sanityCheck();
};
CheckSize(Array, 48, 8);
EXPRESSION(Literal) {