This repository was archived by the owner on May 28, 2021. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathast.h
More file actions
3565 lines (2885 loc) · 118 KB
/
Copy pathast.h
File metadata and controls
3565 lines (2885 loc) · 118 KB
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 2012 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef V8_AST_AST_H_
#define V8_AST_AST_H_
#include "src/ast/ast-value-factory.h"
#include "src/ast/modules.h"
#include "src/ast/variables.h"
#include "src/bailout-reason.h"
#include "src/factory.h"
#include "src/globals.h"
#include "src/isolate.h"
#include "src/label.h"
#include "src/list.h"
#include "src/objects/literal-objects.h"
#include "src/parsing/token.h"
#include "src/runtime/runtime.h"
#include "src/small-pointer-list.h"
namespace v8 {
namespace internal {
// The abstract syntax tree is an intermediate, light-weight
// representation of the parsed JavaScript code suitable for
// compilation to native code.
// Nodes are allocated in a separate zone, which allows faster
// allocation and constant-time deallocation of the entire syntax
// tree.
// ----------------------------------------------------------------------------
// Nodes of the abstract syntax tree. Only concrete classes are
// enumerated here.
#define DECLARATION_NODE_LIST(V) \
V(VariableDeclaration) \
V(FunctionDeclaration)
#define ITERATION_NODE_LIST(V) \
V(DoWhileStatement) \
V(WhileStatement) \
V(ForStatement) \
V(ForInStatement) \
V(ForOfStatement)
#define BREAKABLE_NODE_LIST(V) \
V(Block) \
V(SwitchStatement)
#define STATEMENT_NODE_LIST(V) \
ITERATION_NODE_LIST(V) \
BREAKABLE_NODE_LIST(V) \
V(ExpressionStatement) \
V(EmptyStatement) \
V(SloppyBlockFunctionStatement) \
V(IfStatement) \
V(ContinueStatement) \
V(BreakStatement) \
V(ReturnStatement) \
V(WithStatement) \
V(TryCatchStatement) \
V(TryFinallyStatement) \
V(DebuggerStatement)
#define LITERAL_NODE_LIST(V) \
V(RegExpLiteral) \
V(ObjectLiteral) \
V(ArrayLiteral)
#define PROPERTY_NODE_LIST(V) \
V(Assignment) \
V(CountOperation) \
V(Property)
#define CALL_NODE_LIST(V) \
V(Call) \
V(CallNew)
#define EXPRESSION_NODE_LIST(V) \
LITERAL_NODE_LIST(V) \
PROPERTY_NODE_LIST(V) \
CALL_NODE_LIST(V) \
V(FunctionLiteral) \
V(ClassLiteral) \
V(NativeFunctionLiteral) \
V(Conditional) \
V(VariableProxy) \
V(Literal) \
V(Yield) \
V(YieldStar) \
V(Await) \
V(Throw) \
V(CallRuntime) \
V(UnaryOperation) \
V(BinaryOperation) \
V(CompareOperation) \
V(Spread) \
V(ThisFunction) \
V(SuperPropertyReference) \
V(SuperCallReference) \
V(CaseClause) \
V(EmptyParentheses) \
V(GetIterator) \
V(DoExpression) \
V(RewritableExpression) \
V(ImportCallExpression)
#define AST_NODE_LIST(V) \
DECLARATION_NODE_LIST(V) \
STATEMENT_NODE_LIST(V) \
EXPRESSION_NODE_LIST(V)
// Forward declarations
class AstNodeFactory;
class Declaration;
class Module;
class BreakableStatement;
class Expression;
class IterationStatement;
class MaterializedLiteral;
class ProducedPreParsedScopeData;
class Statement;
#define DEF_FORWARD_DECLARATION(type) class type;
AST_NODE_LIST(DEF_FORWARD_DECLARATION)
#undef DEF_FORWARD_DECLARATION
class FeedbackSlotCache {
public:
typedef std::pair<TypeofMode, Variable*> Key;
explicit FeedbackSlotCache(Zone* zone) : map_(zone) {}
void Put(TypeofMode typeof_mode, Variable* variable, FeedbackSlot slot) {
Key key = std::make_pair(typeof_mode, variable);
auto entry = std::make_pair(key, slot);
map_.insert(entry);
}
FeedbackSlot Get(TypeofMode typeof_mode, Variable* variable) const {
Key key = std::make_pair(typeof_mode, variable);
auto iter = map_.find(key);
if (iter != map_.end()) {
return iter->second;
}
return FeedbackSlot();
}
private:
ZoneMap<Key, FeedbackSlot> map_;
};
class AstProperties final BASE_EMBEDDED {
public:
explicit AstProperties(Zone* zone) : node_count_(0), spec_(zone) {}
int node_count() { return node_count_; }
void add_node_count(int count) { node_count_ += count; }
const FeedbackVectorSpec* get_spec() const { return &spec_; }
FeedbackVectorSpec* get_spec() { return &spec_; }
private:
int node_count_;
FeedbackVectorSpec spec_;
};
class AstNode: public ZoneObject {
public:
#define DECLARE_TYPE_ENUM(type) k##type,
enum NodeType : uint8_t { AST_NODE_LIST(DECLARE_TYPE_ENUM) };
#undef DECLARE_TYPE_ENUM
void* operator new(size_t size, Zone* zone) { return zone->New(size); }
NodeType node_type() const { return NodeTypeField::decode(bit_field_); }
int position() const { return position_; }
#ifdef DEBUG
void Print();
void Print(Isolate* isolate);
#endif // DEBUG
// Type testing & conversion functions overridden by concrete subclasses.
#define DECLARE_NODE_FUNCTIONS(type) \
V8_INLINE bool Is##type() const; \
V8_INLINE type* As##type(); \
V8_INLINE const type* As##type() const;
AST_NODE_LIST(DECLARE_NODE_FUNCTIONS)
#undef DECLARE_NODE_FUNCTIONS
BreakableStatement* AsBreakableStatement();
IterationStatement* AsIterationStatement();
MaterializedLiteral* AsMaterializedLiteral();
private:
// Hidden to prevent accidental usage. It would have to load the
// current zone from the TLS.
void* operator new(size_t size);
int position_;
class NodeTypeField : public BitField<NodeType, 0, 6> {};
protected:
uint32_t bit_field_;
static const uint8_t kNextBitFieldIndex = NodeTypeField::kNext;
AstNode(int position, NodeType type)
: position_(position), bit_field_(NodeTypeField::encode(type)) {}
};
class Statement : public AstNode {
public:
bool IsEmpty() { return AsEmptyStatement() != NULL; }
bool IsJump() const;
protected:
Statement(int position, NodeType type) : AstNode(position, type) {}
static const uint8_t kNextBitFieldIndex = AstNode::kNextBitFieldIndex;
};
class SmallMapList final {
public:
SmallMapList() {}
SmallMapList(int capacity, Zone* zone) : list_(capacity, zone) {}
void Reserve(int capacity, Zone* zone) { list_.Reserve(capacity, zone); }
void Clear() { list_.Clear(); }
void Sort() { list_.Sort(); }
bool is_empty() const { return list_.is_empty(); }
int length() const { return list_.length(); }
void Add(Handle<Map> handle, Zone* zone) {
list_.Add(handle.location(), zone);
}
Handle<Map> at(int i) const;
Handle<Map> first() const { return at(0); }
Handle<Map> last() const { return at(length() - 1); }
private:
// The list stores pointers to Map*, that is Map**, so it's GC safe.
SmallPointerList<Map*> list_;
DISALLOW_COPY_AND_ASSIGN(SmallMapList);
};
class Expression : public AstNode {
public:
enum Context {
// Not assigned a context yet, or else will not be visited during
// code generation.
kUninitialized,
// Evaluated for its side effects.
kEffect,
// Evaluated for its value (and side effects).
kValue,
// Evaluated for control flow (and side effects).
kTest
};
// True iff the expression is a valid reference expression.
bool IsValidReferenceExpression() const;
// Helpers for ToBoolean conversion.
bool ToBooleanIsTrue() const;
bool ToBooleanIsFalse() const;
// Symbols that cannot be parsed as array indices are considered property
// names. We do not treat symbols that can be array indexes as property
// names because [] for string objects is handled only by keyed ICs.
bool IsPropertyName() const;
// True iff the expression is a class or function expression without
// a syntactic name.
bool IsAnonymousFunctionDefinition() const;
// True iff the expression is a concise method definition.
bool IsConciseMethodDefinition() const;
// True iff the expression is an accessor function definition.
bool IsAccessorFunctionDefinition() const;
// True iff the expression is a literal represented as a smi.
bool IsSmiLiteral() const;
// True iff the expression is a literal represented as a number.
bool IsNumberLiteral() const;
// True iff the expression is a string literal.
bool IsStringLiteral() const;
// True iff the expression is the null literal.
bool IsNullLiteral() const;
// True if we can prove that the expression is the undefined literal. Note
// that this also checks for loads of the global "undefined" variable.
bool IsUndefinedLiteral() const;
// True iff the expression is a valid target for an assignment.
bool IsValidReferenceExpressionOrThis() const;
SmallMapList* GetReceiverTypes();
KeyedAccessStoreMode GetStoreMode() const;
IcCheckType GetKeyType() const;
bool IsMonomorphic() const;
protected:
Expression(int pos, NodeType type) : AstNode(pos, type) {}
static const uint8_t kNextBitFieldIndex = AstNode::kNextBitFieldIndex;
};
class BreakableStatement : public Statement {
public:
enum BreakableType {
TARGET_FOR_ANONYMOUS,
TARGET_FOR_NAMED_ONLY
};
// The labels associated with this statement. May be NULL;
// if it is != NULL, guaranteed to contain at least one entry.
ZoneList<const AstRawString*>* labels() const { return labels_; }
// Testers.
bool is_target_for_anonymous() const {
return BreakableTypeField::decode(bit_field_) == TARGET_FOR_ANONYMOUS;
}
private:
BreakableType breakableType() const {
return BreakableTypeField::decode(bit_field_);
}
ZoneList<const AstRawString*>* labels_;
class BreakableTypeField
: public BitField<BreakableType, Statement::kNextBitFieldIndex, 1> {};
protected:
BreakableStatement(ZoneList<const AstRawString*>* labels,
BreakableType breakable_type, int position, NodeType type)
: Statement(position, type),
labels_(labels) {
DCHECK(labels == NULL || labels->length() > 0);
bit_field_ |= BreakableTypeField::encode(breakable_type);
}
static const uint8_t kNextBitFieldIndex = BreakableTypeField::kNext;
};
class Block final : public BreakableStatement {
public:
ZoneList<Statement*>* statements() { return &statements_; }
bool ignore_completion_value() const {
return IgnoreCompletionField::decode(bit_field_);
}
bool IsJump() const {
return !statements_.is_empty() && statements_.last()->IsJump()
&& labels() == NULL; // Good enough as an approximation...
}
Scope* scope() const { return scope_; }
void set_scope(Scope* scope) { scope_ = scope; }
private:
friend class AstNodeFactory;
Block(Zone* zone, ZoneList<const AstRawString*>* labels, int capacity,
bool ignore_completion_value, int pos)
: BreakableStatement(labels, TARGET_FOR_NAMED_ONLY, pos, kBlock),
statements_(capacity, zone),
scope_(NULL) {
bit_field_ |= IgnoreCompletionField::encode(ignore_completion_value);
}
ZoneList<Statement*> statements_;
Scope* scope_;
class IgnoreCompletionField
: public BitField<bool, BreakableStatement::kNextBitFieldIndex, 1> {};
};
class DoExpression final : public Expression {
public:
Block* block() { return block_; }
void set_block(Block* b) { block_ = b; }
VariableProxy* result() { return result_; }
void set_result(VariableProxy* v) { result_ = v; }
private:
friend class AstNodeFactory;
DoExpression(Block* block, VariableProxy* result, int pos)
: Expression(pos, kDoExpression), block_(block), result_(result) {
DCHECK_NOT_NULL(block_);
DCHECK_NOT_NULL(result_);
}
Block* block_;
VariableProxy* result_;
};
class Declaration : public AstNode {
public:
typedef ThreadedList<Declaration> List;
VariableProxy* proxy() const { return proxy_; }
Scope* scope() const { return scope_; }
protected:
Declaration(VariableProxy* proxy, Scope* scope, int pos, NodeType type)
: AstNode(pos, type), proxy_(proxy), scope_(scope), next_(nullptr) {}
private:
VariableProxy* proxy_;
// Nested scope from which the declaration originated.
Scope* scope_;
// Declarations list threaded through the declarations.
Declaration** next() { return &next_; }
Declaration* next_;
friend List;
};
class VariableDeclaration final : public Declaration {
private:
friend class AstNodeFactory;
VariableDeclaration(VariableProxy* proxy, Scope* scope, int pos)
: Declaration(proxy, scope, pos, kVariableDeclaration) {}
};
class FunctionDeclaration final : public Declaration {
public:
FunctionLiteral* fun() const { return fun_; }
void set_fun(FunctionLiteral* f) { fun_ = f; }
private:
friend class AstNodeFactory;
FunctionDeclaration(VariableProxy* proxy, FunctionLiteral* fun, Scope* scope,
int pos)
: Declaration(proxy, scope, pos, kFunctionDeclaration), fun_(fun) {
DCHECK(fun != NULL);
}
FunctionLiteral* fun_;
};
class IterationStatement : public BreakableStatement {
public:
Statement* body() const { return body_; }
void set_body(Statement* s) { body_ = s; }
int suspend_count() const { return suspend_count_; }
int first_suspend_id() const { return first_suspend_id_; }
void set_suspend_count(int suspend_count) { suspend_count_ = suspend_count; }
void set_first_suspend_id(int first_suspend_id) {
first_suspend_id_ = first_suspend_id;
}
void set_osr_id(int id) { osr_id_ = BailoutId(id); }
BailoutId OsrEntryId() const {
DCHECK(!osr_id_.IsNone());
return osr_id_;
}
protected:
IterationStatement(ZoneList<const AstRawString*>* labels, int pos,
NodeType type)
: BreakableStatement(labels, TARGET_FOR_ANONYMOUS, pos, type),
osr_id_(BailoutId::None()),
body_(NULL),
suspend_count_(0),
first_suspend_id_(0) {}
void Initialize(Statement* body) { body_ = body; }
static const uint8_t kNextBitFieldIndex =
BreakableStatement::kNextBitFieldIndex;
private:
BailoutId osr_id_;
Statement* body_;
int suspend_count_;
int first_suspend_id_;
};
class DoWhileStatement final : public IterationStatement {
public:
void Initialize(Expression* cond, Statement* body) {
IterationStatement::Initialize(body);
cond_ = cond;
}
Expression* cond() const { return cond_; }
void set_cond(Expression* e) { cond_ = e; }
private:
friend class AstNodeFactory;
DoWhileStatement(ZoneList<const AstRawString*>* labels, int pos)
: IterationStatement(labels, pos, kDoWhileStatement), cond_(NULL) {}
Expression* cond_;
};
class WhileStatement final : public IterationStatement {
public:
void Initialize(Expression* cond, Statement* body) {
IterationStatement::Initialize(body);
cond_ = cond;
}
Expression* cond() const { return cond_; }
void set_cond(Expression* e) { cond_ = e; }
private:
friend class AstNodeFactory;
WhileStatement(ZoneList<const AstRawString*>* labels, int pos)
: IterationStatement(labels, pos, kWhileStatement), cond_(NULL) {}
Expression* cond_;
};
class ForStatement final : public IterationStatement {
public:
void Initialize(Statement* init, Expression* cond, Statement* next,
Statement* body) {
IterationStatement::Initialize(body);
init_ = init;
cond_ = cond;
next_ = next;
}
Statement* init() const { return init_; }
Expression* cond() const { return cond_; }
Statement* next() const { return next_; }
void set_init(Statement* s) { init_ = s; }
void set_cond(Expression* e) { cond_ = e; }
void set_next(Statement* s) { next_ = s; }
private:
friend class AstNodeFactory;
ForStatement(ZoneList<const AstRawString*>* labels, int pos)
: IterationStatement(labels, pos, kForStatement),
init_(NULL),
cond_(NULL),
next_(NULL) {}
Statement* init_;
Expression* cond_;
Statement* next_;
};
class ForEachStatement : public IterationStatement {
public:
enum VisitMode {
ENUMERATE, // for (each in subject) body;
ITERATE // for (each of subject) body;
};
using IterationStatement::Initialize;
static const char* VisitModeString(VisitMode mode) {
return mode == ITERATE ? "for-of" : "for-in";
}
protected:
ForEachStatement(ZoneList<const AstRawString*>* labels, int pos,
NodeType type)
: IterationStatement(labels, pos, type) {}
};
class ForInStatement final : public ForEachStatement {
public:
void Initialize(Expression* each, Expression* subject, Statement* body) {
ForEachStatement::Initialize(body);
each_ = each;
subject_ = subject;
}
Expression* enumerable() const {
return subject();
}
Expression* each() const { return each_; }
Expression* subject() const { return subject_; }
void set_each(Expression* e) { each_ = e; }
void set_subject(Expression* e) { subject_ = e; }
// Type feedback information.
void AssignFeedbackSlots(FeedbackVectorSpec* spec, LanguageMode language_mode,
FeedbackSlotCache* cache);
FeedbackSlot EachFeedbackSlot() const { return each_slot_; }
FeedbackSlot ForInFeedbackSlot() {
DCHECK(!for_in_feedback_slot_.IsInvalid());
return for_in_feedback_slot_;
}
enum ForInType { FAST_FOR_IN, SLOW_FOR_IN };
ForInType for_in_type() const { return ForInTypeField::decode(bit_field_); }
void set_for_in_type(ForInType type) {
bit_field_ = ForInTypeField::update(bit_field_, type);
}
private:
friend class AstNodeFactory;
ForInStatement(ZoneList<const AstRawString*>* labels, int pos)
: ForEachStatement(labels, pos, kForInStatement),
each_(nullptr),
subject_(nullptr) {
bit_field_ = ForInTypeField::update(bit_field_, SLOW_FOR_IN);
}
Expression* each_;
Expression* subject_;
FeedbackSlot each_slot_;
FeedbackSlot for_in_feedback_slot_;
class ForInTypeField
: public BitField<ForInType, ForEachStatement::kNextBitFieldIndex, 1> {};
};
class ForOfStatement final : public ForEachStatement {
public:
void Initialize(Statement* body, Variable* iterator,
Expression* assign_iterator, Expression* next_result,
Expression* result_done, Expression* assign_each) {
ForEachStatement::Initialize(body);
iterator_ = iterator;
assign_iterator_ = assign_iterator;
next_result_ = next_result;
result_done_ = result_done;
assign_each_ = assign_each;
}
Variable* iterator() const {
return iterator_;
}
// iterator = subject[Symbol.iterator]()
Expression* assign_iterator() const {
return assign_iterator_;
}
// result = iterator.next() // with type check
Expression* next_result() const {
return next_result_;
}
// result.done
Expression* result_done() const {
return result_done_;
}
// each = result.value
Expression* assign_each() const {
return assign_each_;
}
void set_assign_iterator(Expression* e) { assign_iterator_ = e; }
void set_next_result(Expression* e) { next_result_ = e; }
void set_result_done(Expression* e) { result_done_ = e; }
void set_assign_each(Expression* e) { assign_each_ = e; }
private:
friend class AstNodeFactory;
ForOfStatement(ZoneList<const AstRawString*>* labels, int pos)
: ForEachStatement(labels, pos, kForOfStatement),
iterator_(NULL),
assign_iterator_(NULL),
next_result_(NULL),
result_done_(NULL),
assign_each_(NULL) {}
Variable* iterator_;
Expression* assign_iterator_;
Expression* next_result_;
Expression* result_done_;
Expression* assign_each_;
};
class ExpressionStatement final : public Statement {
public:
void set_expression(Expression* e) { expression_ = e; }
Expression* expression() const { return expression_; }
bool IsJump() const { return expression_->IsThrow(); }
private:
friend class AstNodeFactory;
ExpressionStatement(Expression* expression, int pos)
: Statement(pos, kExpressionStatement), expression_(expression) {}
Expression* expression_;
};
class JumpStatement : public Statement {
public:
bool IsJump() const { return true; }
protected:
JumpStatement(int pos, NodeType type) : Statement(pos, type) {}
};
class ContinueStatement final : public JumpStatement {
public:
IterationStatement* target() const { return target_; }
private:
friend class AstNodeFactory;
ContinueStatement(IterationStatement* target, int pos)
: JumpStatement(pos, kContinueStatement), target_(target) {}
IterationStatement* target_;
};
class BreakStatement final : public JumpStatement {
public:
BreakableStatement* target() const { return target_; }
private:
friend class AstNodeFactory;
BreakStatement(BreakableStatement* target, int pos)
: JumpStatement(pos, kBreakStatement), target_(target) {}
BreakableStatement* target_;
};
class ReturnStatement final : public JumpStatement {
public:
enum Type { kNormal, kAsyncReturn };
Expression* expression() const { return expression_; }
void set_expression(Expression* e) { expression_ = e; }
Type type() const { return TypeField::decode(bit_field_); }
bool is_async_return() const { return type() == kAsyncReturn; }
int end_position() const { return end_position_; }
private:
friend class AstNodeFactory;
ReturnStatement(Expression* expression, Type type, int pos, int end_position)
: JumpStatement(pos, kReturnStatement),
expression_(expression),
end_position_(end_position) {
bit_field_ |= TypeField::encode(type);
}
Expression* expression_;
int end_position_;
class TypeField
: public BitField<Type, JumpStatement::kNextBitFieldIndex, 1> {};
};
class WithStatement final : public Statement {
public:
Scope* scope() { return scope_; }
Expression* expression() const { return expression_; }
void set_expression(Expression* e) { expression_ = e; }
Statement* statement() const { return statement_; }
void set_statement(Statement* s) { statement_ = s; }
private:
friend class AstNodeFactory;
WithStatement(Scope* scope, Expression* expression, Statement* statement,
int pos)
: Statement(pos, kWithStatement),
scope_(scope),
expression_(expression),
statement_(statement) {}
Scope* scope_;
Expression* expression_;
Statement* statement_;
};
class CaseClause final : public Expression {
public:
bool is_default() const { return label_ == NULL; }
Expression* label() const {
CHECK(!is_default());
return label_;
}
void set_label(Expression* e) { label_ = e; }
Label* body_target() { return &body_target_; }
ZoneList<Statement*>* statements() const { return statements_; }
void AssignFeedbackSlots(FeedbackVectorSpec* spec, LanguageMode language_mode,
FeedbackSlotCache* cache);
FeedbackSlot CompareOperationFeedbackSlot() { return feedback_slot_; }
private:
friend class AstNodeFactory;
CaseClause(Expression* label, ZoneList<Statement*>* statements, int pos);
FeedbackSlot feedback_slot_;
Expression* label_;
Label body_target_;
ZoneList<Statement*>* statements_;
};
class SwitchStatement final : public BreakableStatement {
public:
void Initialize(Expression* tag, ZoneList<CaseClause*>* cases) {
tag_ = tag;
cases_ = cases;
}
Expression* tag() const { return tag_; }
ZoneList<CaseClause*>* cases() const { return cases_; }
void set_tag(Expression* t) { tag_ = t; }
private:
friend class AstNodeFactory;
SwitchStatement(ZoneList<const AstRawString*>* labels, int pos)
: BreakableStatement(labels, TARGET_FOR_ANONYMOUS, pos, kSwitchStatement),
tag_(NULL),
cases_(NULL) {}
Expression* tag_;
ZoneList<CaseClause*>* cases_;
};
// If-statements always have non-null references to their then- and
// else-parts. When parsing if-statements with no explicit else-part,
// the parser implicitly creates an empty statement. Use the
// HasThenStatement() and HasElseStatement() functions to check if a
// given if-statement has a then- or an else-part containing code.
class IfStatement final : public Statement {
public:
bool HasThenStatement() const { return !then_statement()->IsEmpty(); }
bool HasElseStatement() const { return !else_statement()->IsEmpty(); }
Expression* condition() const { return condition_; }
Statement* then_statement() const { return then_statement_; }
Statement* else_statement() const { return else_statement_; }
void set_condition(Expression* e) { condition_ = e; }
void set_then_statement(Statement* s) { then_statement_ = s; }
void set_else_statement(Statement* s) { else_statement_ = s; }
bool IsJump() const {
return HasThenStatement() && then_statement()->IsJump()
&& HasElseStatement() && else_statement()->IsJump();
}
private:
friend class AstNodeFactory;
IfStatement(Expression* condition, Statement* then_statement,
Statement* else_statement, int pos)
: Statement(pos, kIfStatement),
condition_(condition),
then_statement_(then_statement),
else_statement_(else_statement) {}
Expression* condition_;
Statement* then_statement_;
Statement* else_statement_;
};
class TryStatement : public Statement {
public:
Block* try_block() const { return try_block_; }
void set_try_block(Block* b) { try_block_ = b; }
protected:
TryStatement(Block* try_block, int pos, NodeType type)
: Statement(pos, type), try_block_(try_block) {}
private:
Block* try_block_;
};
class TryCatchStatement final : public TryStatement {
public:
Scope* scope() { return scope_; }
Block* catch_block() const { return catch_block_; }
void set_catch_block(Block* b) { catch_block_ = b; }
// Prediction of whether exceptions thrown into the handler for this try block
// will be caught.
//
// BytecodeGenerator tracks the state of catch prediction, which can change
// with each TryCatchStatement encountered. The tracked catch prediction is
// later compiled into the code's handler table. The runtime uses this
// information to implement a feature that notifies the debugger when an
// uncaught exception is thrown, _before_ the exception propagates to the top.
//
// If this try/catch statement is meant to rethrow (HandlerTable::UNCAUGHT),
// the catch prediction value is set to the same value as the surrounding
// catch prediction.
//
// Since it's generally undecidable whether an exception will be caught, our
// prediction is only an approximation.
// ---------------------------------------------------------------------------
inline HandlerTable::CatchPrediction GetCatchPrediction(
HandlerTable::CatchPrediction outer_catch_prediction) const {
if (catch_prediction_ == HandlerTable::UNCAUGHT) {
return outer_catch_prediction;
}
return catch_prediction_;
}
// Indicates whether or not code should be generated to clear the pending
// exception. The pending exception is cleared for cases where the exception
// is not guaranteed to be rethrown, indicated by the value
// HandlerTable::UNCAUGHT. If both the current and surrounding catch handler's
// are predicted uncaught, the exception is not cleared.
//
// If this handler is not going to simply rethrow the exception, this method
// indicates that the isolate's pending exception message should be cleared
// before executing the catch_block.
// In the normal use case, this flag is always on because the message object
// is not needed anymore when entering the catch block and should not be
// kept alive.
// The use case where the flag is off is when the catch block is guaranteed
// to rethrow the caught exception (using %ReThrow), which reuses the
// pending message instead of generating a new one.
// (When the catch block doesn't rethrow but is guaranteed to perform an
// ordinary throw, not clearing the old message is safe but not very
// useful.)
inline bool ShouldClearPendingException(
HandlerTable::CatchPrediction outer_catch_prediction) const {
return catch_prediction_ != HandlerTable::UNCAUGHT ||
outer_catch_prediction != HandlerTable::UNCAUGHT;
}
private:
friend class AstNodeFactory;
TryCatchStatement(Block* try_block, Scope* scope, Block* catch_block,
HandlerTable::CatchPrediction catch_prediction, int pos)
: TryStatement(try_block, pos, kTryCatchStatement),
scope_(scope),
catch_block_(catch_block),
catch_prediction_(catch_prediction) {}
Scope* scope_;
Block* catch_block_;
HandlerTable::CatchPrediction catch_prediction_;
};
class TryFinallyStatement final : public TryStatement {
public:
Block* finally_block() const { return finally_block_; }
void set_finally_block(Block* b) { finally_block_ = b; }