forked from SerenityOS/serenity
-
Notifications
You must be signed in to change notification settings - Fork 0
/
AST.h
2246 lines (1807 loc) Β· 75.5 KB
/
AST.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
/*
* Copyright (c) 2020-2021, Andreas Kling <kling@serenityos.org>
* Copyright (c) 2020-2022, Linus Groh <linusg@serenityos.org>
* Copyright (c) 2021-2022, David Tuin <davidot@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <AK/DeprecatedFlyString.h>
#include <AK/DeprecatedString.h>
#include <AK/HashMap.h>
#include <AK/NonnullRefPtrVector.h>
#include <AK/OwnPtr.h>
#include <AK/RefPtr.h>
#include <AK/Variant.h>
#include <AK/Vector.h>
#include <LibJS/Bytecode/CodeGenerationError.h>
#include <LibJS/Forward.h>
#include <LibJS/Heap/Handle.h>
#include <LibJS/Runtime/ClassFieldDefinition.h>
#include <LibJS/Runtime/Completion.h>
#include <LibJS/Runtime/EnvironmentCoordinate.h>
#include <LibJS/Runtime/FunctionKind.h>
#include <LibJS/Runtime/ModuleRequest.h>
#include <LibJS/Runtime/PropertyKey.h>
#include <LibJS/Runtime/Reference.h>
#include <LibJS/Runtime/Value.h>
#include <LibJS/SourceRange.h>
#include <LibRegex/Regex.h>
namespace JS {
class Declaration;
class ClassDeclaration;
class FunctionDeclaration;
class Identifier;
class MemberExpression;
class VariableDeclaration;
template<class T, class... Args>
static inline NonnullRefPtr<T>
create_ast_node(SourceRange range, Args&&... args)
{
return adopt_ref(*new T(move(range), forward<Args>(args)...));
}
class ASTNode : public RefCounted<ASTNode> {
public:
virtual ~ASTNode() = default;
// NOTE: This is here to stop ASAN complaining about mismatch between new/delete sizes in ASTNodeWithTailArray.
void operator delete(void* ptr) { ::operator delete(ptr); }
virtual Completion execute(Interpreter&) const = 0;
virtual Bytecode::CodeGenerationErrorOr<void> generate_bytecode(Bytecode::Generator&) const;
virtual void dump(int indent) const;
[[nodiscard]] SourceRange source_range() const;
u32 start_offset() const { return m_start_offset; }
void set_end_offset(Badge<Parser>, u32 end_offset) { m_end_offset = end_offset; }
DeprecatedString class_name() const;
template<typename T>
bool fast_is() const = delete;
virtual bool is_new_expression() const { return false; }
virtual bool is_member_expression() const { return false; }
virtual bool is_super_expression() const { return false; }
virtual bool is_function_expression() const { return false; }
virtual bool is_class_expression() const { return false; }
virtual bool is_expression_statement() const { return false; }
virtual bool is_identifier() const { return false; }
virtual bool is_private_identifier() const { return false; }
virtual bool is_scope_node() const { return false; }
virtual bool is_program() const { return false; }
virtual bool is_class_declaration() const { return false; }
virtual bool is_function_declaration() const { return false; }
virtual bool is_variable_declaration() const { return false; }
virtual bool is_import_call() const { return false; }
virtual bool is_array_expression() const { return false; }
virtual bool is_object_expression() const { return false; }
virtual bool is_string_literal() const { return false; }
virtual bool is_update_expression() const { return false; }
virtual bool is_call_expression() const { return false; }
virtual bool is_labelled_statement() const { return false; }
virtual bool is_iteration_statement() const { return false; }
virtual bool is_class_method() const { return false; }
protected:
explicit ASTNode(SourceRange);
private:
// NOTE: These members are carefully ordered so that `m_start_offset` is packed with the padding after RefCounted::m_ref_count.
// This creates a 4-byte padding hole after `m_end_offset` which is used to pack subclasses better.
u32 m_start_offset { 0 };
RefPtr<SourceCode> m_source_code;
u32 m_end_offset { 0 };
};
// This is a helper class that packs an array of T after the AST node, all in the same allocation.
template<typename Derived, typename Base, typename T>
class ASTNodeWithTailArray : public Base {
public:
virtual ~ASTNodeWithTailArray() override
{
for (auto& value : tail_span())
value.~T();
}
Span<T const> tail_span() const { return { tail_data(), tail_size() }; }
T const* tail_data() const { return reinterpret_cast<T const*>(reinterpret_cast<uintptr_t>(this) + sizeof(Derived)); }
size_t tail_size() const { return m_tail_size; }
protected:
template<typename ActualDerived = Derived, typename... Args>
static NonnullRefPtr<ActualDerived> create(size_t tail_size, SourceRange source_range, Args&&... args)
{
static_assert(sizeof(ActualDerived) == sizeof(Derived), "This leaf class cannot add more members");
static_assert(alignof(ActualDerived) % alignof(T) == 0, "Need padding for tail array");
auto memory = ::operator new(sizeof(ActualDerived) + tail_size * sizeof(T));
return adopt_ref(*::new (memory) ActualDerived(move(source_range), forward<Args>(args)...));
}
ASTNodeWithTailArray(SourceRange source_range, Span<T const> values)
: Base(move(source_range))
, m_tail_size(values.size())
{
VERIFY(values.size() <= NumericLimits<u32>::max());
for (size_t i = 0; i < values.size(); ++i)
new (&tail_data()[i]) T(values[i]);
}
private:
T* tail_data() { return reinterpret_cast<T*>(reinterpret_cast<uintptr_t>(this) + sizeof(Derived)); }
u32 m_tail_size { 0 };
};
class Statement : public ASTNode {
public:
explicit Statement(SourceRange source_range)
: ASTNode(source_range)
{
}
};
// 14.13 Labelled Statements, https://tc39.es/ecma262/#sec-labelled-statements
class LabelledStatement : public Statement {
public:
LabelledStatement(SourceRange source_range, DeprecatedFlyString label, NonnullRefPtr<Statement> labelled_item)
: Statement(source_range)
, m_label(move(label))
, m_labelled_item(move(labelled_item))
{
}
virtual Completion execute(Interpreter&) const override;
virtual void dump(int indent) const override;
virtual Bytecode::CodeGenerationErrorOr<void> generate_bytecode(Bytecode::Generator&) const override;
virtual Bytecode::CodeGenerationErrorOr<void> generate_labelled_evaluation(Bytecode::Generator&, Vector<DeprecatedFlyString> const&) const;
DeprecatedFlyString const& label() const { return m_label; }
DeprecatedFlyString& label() { return m_label; }
NonnullRefPtr<Statement> const& labelled_item() const { return m_labelled_item; }
NonnullRefPtr<Statement>& labelled_item() { return m_labelled_item; }
private:
virtual bool is_labelled_statement() const final { return true; }
DeprecatedFlyString m_label;
NonnullRefPtr<Statement> m_labelled_item;
};
class LabelableStatement : public Statement {
public:
using Statement::Statement;
Vector<DeprecatedFlyString> const& labels() const { return m_labels; }
virtual void add_label(DeprecatedFlyString string) { m_labels.append(move(string)); }
protected:
Vector<DeprecatedFlyString> m_labels;
};
class IterationStatement : public Statement {
public:
using Statement::Statement;
virtual Completion loop_evaluation(Interpreter&, Vector<DeprecatedFlyString> const&) const = 0;
virtual Bytecode::CodeGenerationErrorOr<void> generate_labelled_evaluation(Bytecode::Generator&, Vector<DeprecatedFlyString> const&) const;
private:
virtual bool is_iteration_statement() const final { return true; }
};
class EmptyStatement final : public Statement {
public:
explicit EmptyStatement(SourceRange source_range)
: Statement(source_range)
{
}
Completion execute(Interpreter&) const override;
virtual Bytecode::CodeGenerationErrorOr<void> generate_bytecode(Bytecode::Generator&) const override;
};
class ErrorStatement final : public Statement {
public:
explicit ErrorStatement(SourceRange source_range)
: Statement(source_range)
{
}
Completion execute(Interpreter&) const override { return {}; }
};
class ExpressionStatement final : public Statement {
public:
ExpressionStatement(SourceRange source_range, NonnullRefPtr<Expression> expression)
: Statement(source_range)
, m_expression(move(expression))
{
}
virtual Completion execute(Interpreter&) const override;
virtual void dump(int indent) const override;
virtual Bytecode::CodeGenerationErrorOr<void> generate_bytecode(Bytecode::Generator&) const override;
Expression const& expression() const { return m_expression; };
private:
virtual bool is_expression_statement() const override { return true; }
NonnullRefPtr<Expression> m_expression;
};
template<typename Func, typename... Args>
concept ThrowCompletionOrVoidFunction = requires(Func func, Args... args) {
{
func(args...)
}
-> SameAs<ThrowCompletionOr<void>>;
};
template<typename... Args>
class ThrowCompletionOrVoidCallback : public Function<ThrowCompletionOr<void>(Args...)> {
public:
template<typename CallableType>
ThrowCompletionOrVoidCallback(CallableType&& callable)
requires(VoidFunction<CallableType, Args...>)
: Function<ThrowCompletionOr<void>(Args...)>([callable = forward<CallableType>(callable)](Args... args) {
callable(args...);
return ThrowCompletionOr<void> {};
})
{
}
template<typename CallableType>
ThrowCompletionOrVoidCallback(CallableType&& callable)
requires(ThrowCompletionOrVoidFunction<CallableType, Args...>)
: Function<ThrowCompletionOr<void>(Args...)>(forward<CallableType>(callable))
{
}
};
class ScopeNode : public Statement {
public:
template<typename T, typename... Args>
T& append(SourceRange range, Args&&... args)
{
auto child = create_ast_node<T>(range, forward<Args>(args)...);
m_children.append(move(child));
return static_cast<T&>(m_children.last());
}
void append(NonnullRefPtr<Statement> child)
{
m_children.append(move(child));
}
void shrink_to_fit()
{
m_children.shrink_to_fit();
m_lexical_declarations.shrink_to_fit();
m_var_declarations.shrink_to_fit();
m_functions_hoistable_with_annexB_extension.shrink_to_fit();
}
NonnullRefPtrVector<Statement> const& children() const { return m_children; }
virtual void dump(int indent) const override;
virtual Bytecode::CodeGenerationErrorOr<void> generate_bytecode(Bytecode::Generator&) const override;
Completion evaluate_statements(Interpreter&) const;
void add_var_scoped_declaration(NonnullRefPtr<Declaration> variables);
void add_lexical_declaration(NonnullRefPtr<Declaration> variables);
void add_hoisted_function(NonnullRefPtr<FunctionDeclaration> declaration);
[[nodiscard]] bool has_lexical_declarations() const { return !m_lexical_declarations.is_empty(); }
[[nodiscard]] bool has_var_declarations() const { return !m_var_declarations.is_empty(); }
[[nodiscard]] size_t var_declaration_count() const { return m_var_declarations.size(); }
[[nodiscard]] size_t lexical_declaration_count() const { return m_lexical_declarations.size(); }
ThrowCompletionOr<void> for_each_lexically_scoped_declaration(ThrowCompletionOrVoidCallback<Declaration const&>&& callback) const;
ThrowCompletionOr<void> for_each_lexically_declared_name(ThrowCompletionOrVoidCallback<DeprecatedFlyString const&>&& callback) const;
ThrowCompletionOr<void> for_each_var_declared_name(ThrowCompletionOrVoidCallback<DeprecatedFlyString const&>&& callback) const;
ThrowCompletionOr<void> for_each_var_function_declaration_in_reverse_order(ThrowCompletionOrVoidCallback<FunctionDeclaration const&>&& callback) const;
ThrowCompletionOr<void> for_each_var_scoped_variable_declaration(ThrowCompletionOrVoidCallback<VariableDeclaration const&>&& callback) const;
void block_declaration_instantiation(Interpreter&, Environment*) const;
ThrowCompletionOr<void> for_each_function_hoistable_with_annexB_extension(ThrowCompletionOrVoidCallback<FunctionDeclaration&>&& callback) const;
protected:
explicit ScopeNode(SourceRange source_range)
: Statement(source_range)
{
}
private:
virtual bool is_scope_node() const final { return true; }
NonnullRefPtrVector<Statement> m_children;
NonnullRefPtrVector<Declaration> m_lexical_declarations;
NonnullRefPtrVector<Declaration> m_var_declarations;
NonnullRefPtrVector<FunctionDeclaration> m_functions_hoistable_with_annexB_extension;
};
// ImportEntry Record, https://tc39.es/ecma262/#table-importentry-record-fields
struct ImportEntry {
DeprecatedFlyString import_name; // [[ImportName]] if a String
DeprecatedFlyString local_name; // [[LocalName]]
bool is_namespace { false }; // [[ImportName]] if `namespace-object`
ImportEntry(DeprecatedFlyString import_name_, DeprecatedFlyString local_name_, bool is_namespace_ = false)
: import_name(move(import_name_))
, local_name(move(local_name_))
, is_namespace(is_namespace_)
{
VERIFY(!is_namespace || import_name.is_null());
}
ModuleRequest const& module_request() const
{
VERIFY(m_module_request);
return *m_module_request;
}
private:
friend class ImportStatement;
ModuleRequest* m_module_request; // [[ModuleRequest]]
};
class ImportStatement final : public Statement {
public:
explicit ImportStatement(SourceRange source_range, ModuleRequest from_module, Vector<ImportEntry> entries = {})
: Statement(source_range)
, m_module_request(move(from_module))
, m_entries(move(entries))
{
for (auto& entry : m_entries)
entry.m_module_request = &m_module_request;
}
virtual Completion execute(Interpreter&) const override;
virtual void dump(int indent) const override;
bool has_bound_name(DeprecatedFlyString const& name) const;
Vector<ImportEntry> const& entries() const { return m_entries; }
ModuleRequest const& module_request() const { return m_module_request; }
ModuleRequest& module_request() { return m_module_request; }
private:
ModuleRequest m_module_request;
Vector<ImportEntry> m_entries;
};
// ExportEntry Record, https://tc39.es/ecma262/#table-exportentry-records
struct ExportEntry {
enum class Kind {
NamedExport,
ModuleRequestAll,
ModuleRequestAllButDefault,
// EmptyNamedExport is a special type for export {} from "module",
// which should import the module without getting any of the exports
// however we don't want give it a fake export name which may get
// duplicates
EmptyNamedExport,
} kind;
DeprecatedFlyString export_name; // [[ExportName]]
DeprecatedFlyString local_or_import_name; // Either [[ImportName]] or [[LocalName]]
ExportEntry(Kind export_kind, DeprecatedFlyString export_name_, DeprecatedFlyString local_or_import_name_)
: kind(export_kind)
, export_name(move(export_name_))
, local_or_import_name(move(local_or_import_name_))
{
}
bool is_module_request() const
{
return m_module_request != nullptr;
}
static ExportEntry indirect_export_entry(ModuleRequest const& module_request, DeprecatedFlyString export_name, DeprecatedFlyString import_name)
{
ExportEntry entry { Kind::NamedExport, move(export_name), move(import_name) };
entry.m_module_request = &module_request;
return entry;
}
ModuleRequest const& module_request() const
{
VERIFY(m_module_request);
return *m_module_request;
}
private:
ModuleRequest const* m_module_request { nullptr }; // [[ModuleRequest]]
friend class ExportStatement;
public:
static ExportEntry named_export(DeprecatedFlyString export_name, DeprecatedFlyString local_name)
{
return ExportEntry { Kind::NamedExport, move(export_name), move(local_name) };
}
static ExportEntry all_but_default_entry()
{
return ExportEntry { Kind::ModuleRequestAllButDefault, {}, {} };
}
static ExportEntry all_module_request(DeprecatedFlyString export_name)
{
return ExportEntry { Kind::ModuleRequestAll, move(export_name), {} };
}
static ExportEntry empty_named_export()
{
return ExportEntry { Kind::EmptyNamedExport, {}, {} };
}
};
class ExportStatement final : public Statement {
public:
static DeprecatedFlyString local_name_for_default;
ExportStatement(SourceRange source_range, RefPtr<ASTNode> statement, Vector<ExportEntry> entries, bool is_default_export, ModuleRequest module_request)
: Statement(source_range)
, m_statement(move(statement))
, m_entries(move(entries))
, m_is_default_export(is_default_export)
, m_module_request(move(module_request))
{
if (!m_module_request.module_specifier.is_null()) {
for (auto& entry : m_entries)
entry.m_module_request = &m_module_request;
}
}
virtual Completion execute(Interpreter&) const override;
virtual void dump(int indent) const override;
bool has_export(DeprecatedFlyString const& export_name) const;
bool has_statement() const { return m_statement; }
Vector<ExportEntry> const& entries() const { return m_entries; }
bool is_default_export() const { return m_is_default_export; }
ASTNode const& statement() const
{
VERIFY(m_statement);
return *m_statement;
}
ModuleRequest& module_request()
{
VERIFY(!m_module_request.module_specifier.is_null());
return m_module_request;
}
private:
RefPtr<ASTNode> m_statement;
Vector<ExportEntry> m_entries;
bool m_is_default_export { false };
ModuleRequest m_module_request;
};
class Program final : public ScopeNode {
public:
enum class Type {
Script,
Module
};
explicit Program(SourceRange source_range, Type program_type)
: ScopeNode(source_range)
, m_type(program_type)
{
}
virtual Completion execute(Interpreter&) const override;
bool is_strict_mode() const { return m_is_strict_mode; }
void set_strict_mode() { m_is_strict_mode = true; }
Type type() const { return m_type; }
void append_import(NonnullRefPtr<ImportStatement> import_statement)
{
m_imports.append(import_statement);
append(import_statement);
}
void append_export(NonnullRefPtr<ExportStatement> export_statement)
{
m_exports.append(export_statement);
append(export_statement);
}
NonnullRefPtrVector<ImportStatement> const& imports() const { return m_imports; }
NonnullRefPtrVector<ExportStatement> const& exports() const { return m_exports; }
NonnullRefPtrVector<ImportStatement>& imports() { return m_imports; }
NonnullRefPtrVector<ExportStatement>& exports() { return m_exports; }
bool has_top_level_await() const { return m_has_top_level_await; }
void set_has_top_level_await() { m_has_top_level_await = true; }
ThrowCompletionOr<void> global_declaration_instantiation(Interpreter&, GlobalEnvironment&) const;
private:
virtual bool is_program() const override { return true; }
bool m_is_strict_mode { false };
Type m_type { Type::Script };
NonnullRefPtrVector<ImportStatement> m_imports;
NonnullRefPtrVector<ExportStatement> m_exports;
bool m_has_top_level_await { false };
};
class BlockStatement final : public ScopeNode {
public:
explicit BlockStatement(SourceRange source_range)
: ScopeNode(source_range)
{
}
Completion execute(Interpreter&) const override;
};
class FunctionBody final : public ScopeNode {
public:
explicit FunctionBody(SourceRange source_range)
: ScopeNode(source_range)
{
}
void set_strict_mode() { m_in_strict_mode = true; }
bool in_strict_mode() const { return m_in_strict_mode; }
virtual Completion execute(Interpreter&) const override;
private:
bool m_in_strict_mode { false };
};
class Expression : public ASTNode {
public:
explicit Expression(SourceRange source_range)
: ASTNode(source_range)
{
}
virtual ThrowCompletionOr<Reference> to_reference(Interpreter&) const;
};
class Declaration : public Statement {
public:
explicit Declaration(SourceRange source_range)
: Statement(source_range)
{
}
virtual ThrowCompletionOr<void> for_each_bound_name(ThrowCompletionOrVoidCallback<DeprecatedFlyString const&>&& callback) const = 0;
// 8.1.3 Static Semantics: IsConstantDeclaration, https://tc39.es/ecma262/#sec-static-semantics-isconstantdeclaration
virtual bool is_constant_declaration() const { return false; }
virtual bool is_lexical_declaration() const { return false; }
};
class ErrorDeclaration final : public Declaration {
public:
explicit ErrorDeclaration(SourceRange source_range)
: Declaration(source_range)
{
}
Completion execute(Interpreter&) const override { return {}; }
ThrowCompletionOr<void> for_each_bound_name(ThrowCompletionOrVoidCallback<DeprecatedFlyString const&>&&) const override
{
VERIFY_NOT_REACHED();
}
};
struct BindingPattern : RefCounted<BindingPattern> {
// This covers both BindingProperty and BindingElement, hence the more generic name
struct BindingEntry {
// If this entry represents a BindingElement, then name will be Empty
Variant<NonnullRefPtr<Identifier>, NonnullRefPtr<Expression>, Empty> name {};
Variant<NonnullRefPtr<Identifier>, NonnullRefPtr<BindingPattern>, NonnullRefPtr<MemberExpression>, Empty> alias {};
RefPtr<Expression> initializer {};
bool is_rest { false };
bool is_elision() const { return name.has<Empty>() && alias.has<Empty>(); }
};
enum class Kind {
Array,
Object,
};
void dump(int indent) const;
ThrowCompletionOr<void> for_each_bound_name(ThrowCompletionOrVoidCallback<DeprecatedFlyString const&>&& callback) const;
bool contains_expression() const;
Vector<BindingEntry> entries;
Kind kind { Kind::Object };
};
struct FunctionParameter {
Variant<DeprecatedFlyString, NonnullRefPtr<BindingPattern>> binding;
RefPtr<Expression> default_value;
bool is_rest { false };
};
class FunctionNode {
public:
DeprecatedFlyString const& name() const { return m_name; }
DeprecatedString const& source_text() const { return m_source_text; }
Statement const& body() const { return *m_body; }
Vector<FunctionParameter> const& parameters() const { return m_parameters; };
i32 function_length() const { return m_function_length; }
bool is_strict_mode() const { return m_is_strict_mode; }
bool might_need_arguments_object() const { return m_might_need_arguments_object; }
bool contains_direct_call_to_eval() const { return m_contains_direct_call_to_eval; }
bool is_arrow_function() const { return m_is_arrow_function; }
FunctionKind kind() const { return m_kind; }
protected:
FunctionNode(DeprecatedFlyString name, DeprecatedString source_text, NonnullRefPtr<Statement> body, Vector<FunctionParameter> parameters, i32 function_length, FunctionKind kind, bool is_strict_mode, bool might_need_arguments_object, bool contains_direct_call_to_eval, bool is_arrow_function)
: m_name(move(name))
, m_source_text(move(source_text))
, m_body(move(body))
, m_parameters(move(parameters))
, m_function_length(function_length)
, m_kind(kind)
, m_is_strict_mode(is_strict_mode)
, m_might_need_arguments_object(might_need_arguments_object)
, m_contains_direct_call_to_eval(contains_direct_call_to_eval)
, m_is_arrow_function(is_arrow_function)
{
if (m_is_arrow_function)
VERIFY(!m_might_need_arguments_object);
}
void dump(int indent, DeprecatedString const& class_name) const;
private:
DeprecatedFlyString m_name;
DeprecatedString m_source_text;
NonnullRefPtr<Statement> m_body;
Vector<FunctionParameter> const m_parameters;
const i32 m_function_length;
FunctionKind m_kind;
bool m_is_strict_mode : 1 { false };
bool m_might_need_arguments_object : 1 { false };
bool m_contains_direct_call_to_eval : 1 { false };
bool m_is_arrow_function : 1 { false };
};
class FunctionDeclaration final
: public Declaration
, public FunctionNode {
public:
static bool must_have_name() { return true; }
FunctionDeclaration(SourceRange source_range, DeprecatedFlyString const& name, DeprecatedString source_text, NonnullRefPtr<Statement> body, Vector<FunctionParameter> parameters, i32 function_length, FunctionKind kind, bool is_strict_mode, bool might_need_arguments_object, bool contains_direct_call_to_eval)
: Declaration(source_range)
, FunctionNode(name, move(source_text), move(body), move(parameters), function_length, kind, is_strict_mode, might_need_arguments_object, contains_direct_call_to_eval, false)
{
}
virtual Completion execute(Interpreter&) const override;
virtual void dump(int indent) const override;
virtual Bytecode::CodeGenerationErrorOr<void> generate_bytecode(Bytecode::Generator&) const override;
virtual ThrowCompletionOr<void> for_each_bound_name(ThrowCompletionOrVoidCallback<DeprecatedFlyString const&>&& callback) const override;
virtual bool is_function_declaration() const override { return true; }
void set_should_do_additional_annexB_steps() { m_is_hoisted = true; }
private:
bool m_is_hoisted { false };
};
class FunctionExpression final
: public Expression
, public FunctionNode {
public:
static bool must_have_name() { return false; }
FunctionExpression(SourceRange source_range, DeprecatedFlyString const& name, DeprecatedString source_text, NonnullRefPtr<Statement> body, Vector<FunctionParameter> parameters, i32 function_length, FunctionKind kind, bool is_strict_mode, bool might_need_arguments_object, bool contains_direct_call_to_eval, bool is_arrow_function = false)
: Expression(source_range)
, FunctionNode(name, move(source_text), move(body), move(parameters), function_length, kind, is_strict_mode, might_need_arguments_object, contains_direct_call_to_eval, is_arrow_function)
{
}
virtual Completion execute(Interpreter&) const override;
virtual void dump(int indent) const override;
virtual Bytecode::CodeGenerationErrorOr<void> generate_bytecode(Bytecode::Generator&) const override;
bool has_name() const { return !name().is_empty(); }
Value instantiate_ordinary_function_expression(Interpreter&, DeprecatedFlyString given_name) const;
private:
virtual bool is_function_expression() const override { return true; }
};
class ErrorExpression final : public Expression {
public:
explicit ErrorExpression(SourceRange source_range)
: Expression(source_range)
{
}
Completion execute(Interpreter&) const override { return {}; }
};
class YieldExpression final : public Expression {
public:
explicit YieldExpression(SourceRange source_range, RefPtr<Expression> argument, bool is_yield_from)
: Expression(source_range)
, m_argument(move(argument))
, m_is_yield_from(is_yield_from)
{
}
Expression const* argument() const { return m_argument; }
bool is_yield_from() const { return m_is_yield_from; }
virtual Completion execute(Interpreter&) const override;
virtual void dump(int indent) const override;
virtual Bytecode::CodeGenerationErrorOr<void> generate_bytecode(Bytecode::Generator&) const override;
private:
RefPtr<Expression> m_argument;
bool m_is_yield_from { false };
};
class AwaitExpression final : public Expression {
public:
explicit AwaitExpression(SourceRange source_range, NonnullRefPtr<Expression> argument)
: Expression(source_range)
, m_argument(move(argument))
{
}
virtual Completion execute(Interpreter&) const override;
virtual void dump(int indent) const override;
virtual Bytecode::CodeGenerationErrorOr<void> generate_bytecode(Bytecode::Generator&) const override;
private:
NonnullRefPtr<Expression> m_argument;
};
class ReturnStatement final : public Statement {
public:
explicit ReturnStatement(SourceRange source_range, RefPtr<Expression> argument)
: Statement(source_range)
, m_argument(move(argument))
{
}
Expression const* argument() const { return m_argument; }
virtual Completion execute(Interpreter&) const override;
virtual void dump(int indent) const override;
virtual Bytecode::CodeGenerationErrorOr<void> generate_bytecode(Bytecode::Generator&) const override;
private:
RefPtr<Expression> m_argument;
};
class IfStatement final : public Statement {
public:
IfStatement(SourceRange source_range, NonnullRefPtr<Expression> predicate, NonnullRefPtr<Statement> consequent, RefPtr<Statement> alternate)
: Statement(source_range)
, m_predicate(move(predicate))
, m_consequent(move(consequent))
, m_alternate(move(alternate))
{
}
Expression const& predicate() const { return *m_predicate; }
Statement const& consequent() const { return *m_consequent; }
Statement const* alternate() const { return m_alternate; }
virtual Completion execute(Interpreter&) const override;
virtual void dump(int indent) const override;
virtual Bytecode::CodeGenerationErrorOr<void> generate_bytecode(Bytecode::Generator&) const override;
private:
NonnullRefPtr<Expression> m_predicate;
NonnullRefPtr<Statement> m_consequent;
RefPtr<Statement> m_alternate;
};
class WhileStatement final : public IterationStatement {
public:
WhileStatement(SourceRange source_range, NonnullRefPtr<Expression> test, NonnullRefPtr<Statement> body)
: IterationStatement(source_range)
, m_test(move(test))
, m_body(move(body))
{
}
Expression const& test() const { return *m_test; }
Statement const& body() const { return *m_body; }
virtual Completion execute(Interpreter&) const override;
virtual Completion loop_evaluation(Interpreter&, Vector<DeprecatedFlyString> const&) const override;
virtual void dump(int indent) const override;
virtual Bytecode::CodeGenerationErrorOr<void> generate_bytecode(Bytecode::Generator&) const override;
virtual Bytecode::CodeGenerationErrorOr<void> generate_labelled_evaluation(Bytecode::Generator&, Vector<DeprecatedFlyString> const&) const override;
private:
NonnullRefPtr<Expression> m_test;
NonnullRefPtr<Statement> m_body;
};
class DoWhileStatement final : public IterationStatement {
public:
DoWhileStatement(SourceRange source_range, NonnullRefPtr<Expression> test, NonnullRefPtr<Statement> body)
: IterationStatement(source_range)
, m_test(move(test))
, m_body(move(body))
{
}
Expression const& test() const { return *m_test; }
Statement const& body() const { return *m_body; }
virtual Completion execute(Interpreter&) const override;
virtual Completion loop_evaluation(Interpreter&, Vector<DeprecatedFlyString> const&) const override;
virtual void dump(int indent) const override;
virtual Bytecode::CodeGenerationErrorOr<void> generate_bytecode(Bytecode::Generator&) const override;
virtual Bytecode::CodeGenerationErrorOr<void> generate_labelled_evaluation(Bytecode::Generator&, Vector<DeprecatedFlyString> const&) const override;
private:
NonnullRefPtr<Expression> m_test;
NonnullRefPtr<Statement> m_body;
};
class WithStatement final : public Statement {
public:
WithStatement(SourceRange source_range, NonnullRefPtr<Expression> object, NonnullRefPtr<Statement> body)
: Statement(source_range)
, m_object(move(object))
, m_body(move(body))
{
}
Expression const& object() const { return *m_object; }
Statement const& body() const { return *m_body; }
virtual Completion execute(Interpreter&) const override;
virtual void dump(int indent) const override;
virtual Bytecode::CodeGenerationErrorOr<void> generate_bytecode(Bytecode::Generator&) const override;
private:
NonnullRefPtr<Expression> m_object;
NonnullRefPtr<Statement> m_body;
};
class ForStatement final : public IterationStatement {
public:
ForStatement(SourceRange source_range, RefPtr<ASTNode> init, RefPtr<Expression> test, RefPtr<Expression> update, NonnullRefPtr<Statement> body)
: IterationStatement(source_range)
, m_init(move(init))
, m_test(move(test))
, m_update(move(update))
, m_body(move(body))
{
}
ASTNode const* init() const { return m_init; }
Expression const* test() const { return m_test; }
Expression const* update() const { return m_update; }
Statement const& body() const { return *m_body; }
virtual Completion execute(Interpreter&) const override;
virtual Completion loop_evaluation(Interpreter&, Vector<DeprecatedFlyString> const&) const override;
virtual void dump(int indent) const override;
virtual Bytecode::CodeGenerationErrorOr<void> generate_bytecode(Bytecode::Generator&) const override;
virtual Bytecode::CodeGenerationErrorOr<void> generate_labelled_evaluation(Bytecode::Generator&, Vector<DeprecatedFlyString> const&) const override;
private:
Completion for_body_evaluation(Interpreter&, Vector<DeprecatedFlyString> const&, size_t per_iteration_bindings_size) const;
RefPtr<ASTNode> m_init;
RefPtr<Expression> m_test;
RefPtr<Expression> m_update;
NonnullRefPtr<Statement> m_body;
};
class ForInStatement final : public IterationStatement {
public:
ForInStatement(SourceRange source_range, Variant<NonnullRefPtr<ASTNode>, NonnullRefPtr<BindingPattern>> lhs, NonnullRefPtr<Expression> rhs, NonnullRefPtr<Statement> body)
: IterationStatement(source_range)
, m_lhs(move(lhs))
, m_rhs(move(rhs))
, m_body(move(body))
{
}
auto const& lhs() const { return m_lhs; }
Expression const& rhs() const { return *m_rhs; }
Statement const& body() const { return *m_body; }
virtual Completion execute(Interpreter&) const override;
virtual Bytecode::CodeGenerationErrorOr<void> generate_bytecode(Bytecode::Generator&) const override;
virtual Bytecode::CodeGenerationErrorOr<void> generate_labelled_evaluation(Bytecode::Generator&, Vector<DeprecatedFlyString> const&) const override;
virtual Completion loop_evaluation(Interpreter&, Vector<DeprecatedFlyString> const&) const override;
virtual void dump(int indent) const override;
private:
Variant<NonnullRefPtr<ASTNode>, NonnullRefPtr<BindingPattern>> m_lhs;
NonnullRefPtr<Expression> m_rhs;
NonnullRefPtr<Statement> m_body;
};
class ForOfStatement final : public IterationStatement {
public:
ForOfStatement(SourceRange source_range, Variant<NonnullRefPtr<ASTNode>, NonnullRefPtr<BindingPattern>> lhs, NonnullRefPtr<Expression> rhs, NonnullRefPtr<Statement> body)
: IterationStatement(source_range)
, m_lhs(move(lhs))
, m_rhs(move(rhs))
, m_body(move(body))
{
}
auto const& lhs() const { return m_lhs; }
Expression const& rhs() const { return *m_rhs; }
Statement const& body() const { return *m_body; }
virtual Completion execute(Interpreter&) const override;
virtual Bytecode::CodeGenerationErrorOr<void> generate_bytecode(Bytecode::Generator&) const override;
virtual Bytecode::CodeGenerationErrorOr<void> generate_labelled_evaluation(Bytecode::Generator&, Vector<DeprecatedFlyString> const&) const override;
virtual Completion loop_evaluation(Interpreter&, Vector<DeprecatedFlyString> const&) const override;
virtual void dump(int indent) const override;
private:
Variant<NonnullRefPtr<ASTNode>, NonnullRefPtr<BindingPattern>> m_lhs;
NonnullRefPtr<Expression> m_rhs;
NonnullRefPtr<Statement> m_body;
};
class ForAwaitOfStatement final : public IterationStatement {
public:
ForAwaitOfStatement(SourceRange source_range, Variant<NonnullRefPtr<ASTNode>, NonnullRefPtr<BindingPattern>> lhs, NonnullRefPtr<Expression> rhs, NonnullRefPtr<Statement> body)
: IterationStatement(source_range)
, m_lhs(move(lhs))
, m_rhs(move(rhs))
, m_body(move(body))
{
}
virtual Completion execute(Interpreter&) const override;
virtual Completion loop_evaluation(Interpreter&, Vector<DeprecatedFlyString> const&) const override;
virtual void dump(int indent) const override;
private:
Variant<NonnullRefPtr<ASTNode>, NonnullRefPtr<BindingPattern>> m_lhs;