This repository has been archived by the owner on Apr 22, 2023. It is now read-only.
/
ast.h
2149 lines (1723 loc) · 62.4 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 2011 the V8 project authors. All rights reserved.
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following
// disclaimer in the documentation and/or other materials provided
// with the distribution.
// * Neither the name of Google Inc. nor the names of its
// contributors may be used to endorse or promote products derived
// from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#ifndef V8_AST_H_
#define V8_AST_H_
#include "allocation.h"
#include "execution.h"
#include "factory.h"
#include "jsregexp.h"
#include "runtime.h"
#include "small-pointer-list.h"
#include "token.h"
#include "variables.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 STATEMENT_NODE_LIST(V) \
V(Block) \
V(ExpressionStatement) \
V(EmptyStatement) \
V(IfStatement) \
V(ContinueStatement) \
V(BreakStatement) \
V(ReturnStatement) \
V(WithStatement) \
V(SwitchStatement) \
V(DoWhileStatement) \
V(WhileStatement) \
V(ForStatement) \
V(ForInStatement) \
V(TryCatchStatement) \
V(TryFinallyStatement) \
V(DebuggerStatement)
#define EXPRESSION_NODE_LIST(V) \
V(FunctionLiteral) \
V(SharedFunctionInfoLiteral) \
V(Conditional) \
V(VariableProxy) \
V(Literal) \
V(RegExpLiteral) \
V(ObjectLiteral) \
V(ArrayLiteral) \
V(Assignment) \
V(Throw) \
V(Property) \
V(Call) \
V(CallNew) \
V(CallRuntime) \
V(UnaryOperation) \
V(CountOperation) \
V(BinaryOperation) \
V(CompareOperation) \
V(CompareToNull) \
V(ThisFunction)
#define AST_NODE_LIST(V) \
V(Declaration) \
STATEMENT_NODE_LIST(V) \
EXPRESSION_NODE_LIST(V)
// Forward declarations
class BitVector;
class DefinitionInfo;
class MaterializedLiteral;
class TargetCollector;
class TypeFeedbackOracle;
#define DEF_FORWARD_DECLARATION(type) class type;
AST_NODE_LIST(DEF_FORWARD_DECLARATION)
#undef DEF_FORWARD_DECLARATION
// Typedef only introduced to avoid unreadable code.
// Please do appreciate the required space in "> >".
typedef ZoneList<Handle<String> > ZoneStringList;
typedef ZoneList<Handle<Object> > ZoneObjectList;
#define DECLARE_NODE_TYPE(type) \
virtual void Accept(AstVisitor* v); \
virtual AstNode::Type node_type() const { return AstNode::k##type; } \
virtual type* As##type() { return this; }
class AstNode: public ZoneObject {
public:
#define DECLARE_TYPE_ENUM(type) k##type,
enum Type {
AST_NODE_LIST(DECLARE_TYPE_ENUM)
kInvalid = -1
};
#undef DECLARE_TYPE_ENUM
static const int kNoNumber = -1;
static const int kFunctionEntryId = 2; // Using 0 could disguise errors.
// This AST id identifies the point after the declarations have been
// visited. We need it to capture the environment effects of declarations
// that emit code (function declarations).
static const int kDeclarationsId = 3;
// Override ZoneObject's new to count allocated AST nodes.
void* operator new(size_t size, Zone* zone) {
Isolate* isolate = zone->isolate();
isolate->set_ast_node_count(isolate->ast_node_count() + 1);
return zone->New(static_cast<int>(size));
}
AstNode() {}
virtual ~AstNode() { }
virtual void Accept(AstVisitor* v) = 0;
virtual Type node_type() const { return kInvalid; }
// Type testing & conversion functions overridden by concrete subclasses.
#define DECLARE_NODE_FUNCTIONS(type) \
virtual type* As##type() { return NULL; }
AST_NODE_LIST(DECLARE_NODE_FUNCTIONS)
#undef DECLARE_NODE_FUNCTIONS
virtual Statement* AsStatement() { return NULL; }
virtual Expression* AsExpression() { return NULL; }
virtual TargetCollector* AsTargetCollector() { return NULL; }
virtual BreakableStatement* AsBreakableStatement() { return NULL; }
virtual IterationStatement* AsIterationStatement() { return NULL; }
virtual MaterializedLiteral* AsMaterializedLiteral() { return NULL; }
// True if the node is simple enough for us to inline calls containing it.
virtual bool IsInlineable() const = 0;
static int Count() { return Isolate::Current()->ast_node_count(); }
static void ResetIds() { Isolate::Current()->set_ast_node_id(0); }
protected:
static unsigned GetNextId(Isolate* isolate) {
return ReserveIdRange(isolate, 1);
}
static unsigned ReserveIdRange(Isolate* isolate, int n) {
unsigned tmp = isolate->ast_node_id();
isolate->set_ast_node_id(tmp + n);
return tmp;
}
private:
// Hidden to prevent accidental usage. It would have to load the
// current zone from the TLS.
void* operator new(size_t size);
friend class CaseClause; // Generates AST IDs.
};
class Statement: public AstNode {
public:
Statement() : statement_pos_(RelocInfo::kNoPosition) {}
virtual Statement* AsStatement() { return this; }
virtual Assignment* StatementAsSimpleAssignment() { return NULL; }
virtual CountOperation* StatementAsCountOperation() { return NULL; }
bool IsEmpty() { return AsEmptyStatement() != NULL; }
void set_statement_pos(int statement_pos) { statement_pos_ = statement_pos; }
int statement_pos() const { return statement_pos_; }
private:
int statement_pos_;
};
class SmallMapList {
public:
SmallMapList() {}
explicit SmallMapList(int capacity) : list_(capacity) {}
void Reserve(int capacity) { list_.Reserve(capacity); }
void Clear() { list_.Clear(); }
bool is_empty() const { return list_.is_empty(); }
int length() const { return list_.length(); }
void Add(Handle<Map> handle) {
list_.Add(handle.location());
}
Handle<Map> at(int i) const {
return Handle<Map>(list_.at(i));
}
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
};
explicit Expression(Isolate* isolate)
: id_(GetNextId(isolate)),
test_id_(GetNextId(isolate)) {}
virtual int position() const {
UNREACHABLE();
return 0;
}
virtual Expression* AsExpression() { return this; }
virtual bool IsTrivial() { return false; }
virtual bool IsValidLeftHandSide() { return false; }
// Helpers for ToBoolean conversion.
virtual bool ToBooleanIsTrue() { return false; }
virtual bool ToBooleanIsFalse() { return false; }
// 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.
virtual bool IsPropertyName() { return false; }
// Mark the expression as being compiled as an expression
// statement. This is used to transform postfix increments to
// (faster) prefix increments.
virtual void MarkAsStatement() { /* do nothing */ }
// True iff the result can be safely overwritten (to avoid allocation).
// False for operations that can return one of their operands.
virtual bool ResultOverwriteAllowed() { return false; }
// True iff the expression is a literal represented as a smi.
virtual bool IsSmiLiteral() { return false; }
// Type feedback information for assignments and properties.
virtual bool IsMonomorphic() {
UNREACHABLE();
return false;
}
virtual bool IsArrayLength() {
UNREACHABLE();
return false;
}
virtual SmallMapList* GetReceiverTypes() {
UNREACHABLE();
return NULL;
}
Handle<Map> GetMonomorphicReceiverType() {
ASSERT(IsMonomorphic());
SmallMapList* types = GetReceiverTypes();
ASSERT(types != NULL && types->length() == 1);
return types->at(0);
}
unsigned id() const { return id_; }
unsigned test_id() const { return test_id_; }
private:
unsigned id_;
unsigned test_id_;
};
class BreakableStatement: public Statement {
public:
enum Type {
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.
ZoneStringList* labels() const { return labels_; }
// Type testing & conversion.
virtual BreakableStatement* AsBreakableStatement() { return this; }
// Code generation
Label* break_target() { return &break_target_; }
// Testers.
bool is_target_for_anonymous() const { return type_ == TARGET_FOR_ANONYMOUS; }
// Bailout support.
int EntryId() const { return entry_id_; }
int ExitId() const { return exit_id_; }
protected:
BreakableStatement(Isolate* isolate, ZoneStringList* labels, Type type);
private:
ZoneStringList* labels_;
Type type_;
Label break_target_;
int entry_id_;
int exit_id_;
};
class Block: public BreakableStatement {
public:
inline Block(Isolate* isolate,
ZoneStringList* labels,
int capacity,
bool is_initializer_block);
DECLARE_NODE_TYPE(Block)
virtual Assignment* StatementAsSimpleAssignment() {
if (statements_.length() != 1) return NULL;
return statements_[0]->StatementAsSimpleAssignment();
}
virtual CountOperation* StatementAsCountOperation() {
if (statements_.length() != 1) return NULL;
return statements_[0]->StatementAsCountOperation();
}
virtual bool IsInlineable() const;
void AddStatement(Statement* statement) { statements_.Add(statement); }
ZoneList<Statement*>* statements() { return &statements_; }
bool is_initializer_block() const { return is_initializer_block_; }
Scope* block_scope() const { return block_scope_; }
void set_block_scope(Scope* block_scope) { block_scope_ = block_scope; }
private:
ZoneList<Statement*> statements_;
bool is_initializer_block_;
Scope* block_scope_;
};
class Declaration: public AstNode {
public:
Declaration(VariableProxy* proxy,
Variable::Mode mode,
FunctionLiteral* fun,
Scope* scope)
: proxy_(proxy),
mode_(mode),
fun_(fun),
scope_(scope) {
ASSERT(mode == Variable::VAR ||
mode == Variable::CONST ||
mode == Variable::LET);
// At the moment there are no "const functions"'s in JavaScript...
ASSERT(fun == NULL || mode == Variable::VAR || mode == Variable::LET);
}
DECLARE_NODE_TYPE(Declaration)
VariableProxy* proxy() const { return proxy_; }
Variable::Mode mode() const { return mode_; }
FunctionLiteral* fun() const { return fun_; } // may be NULL
virtual bool IsInlineable() const;
Scope* scope() const { return scope_; }
private:
VariableProxy* proxy_;
Variable::Mode mode_;
FunctionLiteral* fun_;
// Nested scope from which the declaration originated.
Scope* scope_;
};
class IterationStatement: public BreakableStatement {
public:
// Type testing & conversion.
virtual IterationStatement* AsIterationStatement() { return this; }
Statement* body() const { return body_; }
// Bailout support.
int OsrEntryId() const { return osr_entry_id_; }
virtual int ContinueId() const = 0;
virtual int StackCheckId() const = 0;
// Code generation
Label* continue_target() { return &continue_target_; }
protected:
inline IterationStatement(Isolate* isolate, ZoneStringList* labels);
void Initialize(Statement* body) {
body_ = body;
}
private:
Statement* body_;
Label continue_target_;
int osr_entry_id_;
};
class DoWhileStatement: public IterationStatement {
public:
inline DoWhileStatement(Isolate* isolate, ZoneStringList* labels);
DECLARE_NODE_TYPE(DoWhileStatement)
void Initialize(Expression* cond, Statement* body) {
IterationStatement::Initialize(body);
cond_ = cond;
}
Expression* cond() const { return cond_; }
// Position where condition expression starts. We need it to make
// the loop's condition a breakable location.
int condition_position() { return condition_position_; }
void set_condition_position(int pos) { condition_position_ = pos; }
// Bailout support.
virtual int ContinueId() const { return continue_id_; }
virtual int StackCheckId() const { return back_edge_id_; }
int BackEdgeId() const { return back_edge_id_; }
virtual bool IsInlineable() const;
private:
Expression* cond_;
int condition_position_;
int continue_id_;
int back_edge_id_;
};
class WhileStatement: public IterationStatement {
public:
inline WhileStatement(Isolate* isolate, ZoneStringList* labels);
DECLARE_NODE_TYPE(WhileStatement)
void Initialize(Expression* cond, Statement* body) {
IterationStatement::Initialize(body);
cond_ = cond;
}
Expression* cond() const { return cond_; }
bool may_have_function_literal() const {
return may_have_function_literal_;
}
void set_may_have_function_literal(bool value) {
may_have_function_literal_ = value;
}
virtual bool IsInlineable() const;
// Bailout support.
virtual int ContinueId() const { return EntryId(); }
virtual int StackCheckId() const { return body_id_; }
int BodyId() const { return body_id_; }
private:
Expression* cond_;
// True if there is a function literal subexpression in the condition.
bool may_have_function_literal_;
int body_id_;
};
class ForStatement: public IterationStatement {
public:
inline ForStatement(Isolate* isolate, ZoneStringList* labels);
DECLARE_NODE_TYPE(ForStatement)
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_; }
bool may_have_function_literal() const {
return may_have_function_literal_;
}
void set_may_have_function_literal(bool value) {
may_have_function_literal_ = value;
}
// Bailout support.
virtual int ContinueId() const { return continue_id_; }
virtual int StackCheckId() const { return body_id_; }
int BodyId() const { return body_id_; }
bool is_fast_smi_loop() { return loop_variable_ != NULL; }
Variable* loop_variable() { return loop_variable_; }
void set_loop_variable(Variable* var) { loop_variable_ = var; }
virtual bool IsInlineable() const;
private:
Statement* init_;
Expression* cond_;
Statement* next_;
// True if there is a function literal subexpression in the condition.
bool may_have_function_literal_;
Variable* loop_variable_;
int continue_id_;
int body_id_;
};
class ForInStatement: public IterationStatement {
public:
inline ForInStatement(Isolate* isolate, ZoneStringList* labels);
DECLARE_NODE_TYPE(ForInStatement)
void Initialize(Expression* each, Expression* enumerable, Statement* body) {
IterationStatement::Initialize(body);
each_ = each;
enumerable_ = enumerable;
}
Expression* each() const { return each_; }
Expression* enumerable() const { return enumerable_; }
virtual bool IsInlineable() const;
// Bailout support.
int AssignmentId() const { return assignment_id_; }
virtual int ContinueId() const { return EntryId(); }
virtual int StackCheckId() const { return EntryId(); }
private:
Expression* each_;
Expression* enumerable_;
int assignment_id_;
};
class ExpressionStatement: public Statement {
public:
explicit ExpressionStatement(Expression* expression)
: expression_(expression) { }
DECLARE_NODE_TYPE(ExpressionStatement)
virtual bool IsInlineable() const;
virtual Assignment* StatementAsSimpleAssignment();
virtual CountOperation* StatementAsCountOperation();
void set_expression(Expression* e) { expression_ = e; }
Expression* expression() const { return expression_; }
private:
Expression* expression_;
};
class ContinueStatement: public Statement {
public:
explicit ContinueStatement(IterationStatement* target)
: target_(target) { }
DECLARE_NODE_TYPE(ContinueStatement)
IterationStatement* target() const { return target_; }
virtual bool IsInlineable() const;
private:
IterationStatement* target_;
};
class BreakStatement: public Statement {
public:
explicit BreakStatement(BreakableStatement* target)
: target_(target) { }
DECLARE_NODE_TYPE(BreakStatement)
BreakableStatement* target() const { return target_; }
virtual bool IsInlineable() const;
private:
BreakableStatement* target_;
};
class ReturnStatement: public Statement {
public:
explicit ReturnStatement(Expression* expression)
: expression_(expression) { }
DECLARE_NODE_TYPE(ReturnStatement)
Expression* expression() const { return expression_; }
virtual bool IsInlineable() const;
private:
Expression* expression_;
};
class WithStatement: public Statement {
public:
WithStatement(Expression* expression, Statement* statement)
: expression_(expression), statement_(statement) { }
DECLARE_NODE_TYPE(WithStatement)
Expression* expression() const { return expression_; }
Statement* statement() const { return statement_; }
virtual bool IsInlineable() const;
private:
Expression* expression_;
Statement* statement_;
};
class CaseClause: public ZoneObject {
public:
CaseClause(Isolate* isolate,
Expression* label,
ZoneList<Statement*>* statements,
int pos);
bool is_default() const { return label_ == NULL; }
Expression* label() const {
CHECK(!is_default());
return label_;
}
Label* body_target() { return &body_target_; }
ZoneList<Statement*>* statements() const { return statements_; }
int position() const { return position_; }
void set_position(int pos) { position_ = pos; }
int EntryId() { return entry_id_; }
int CompareId() { return compare_id_; }
// Type feedback information.
void RecordTypeFeedback(TypeFeedbackOracle* oracle);
bool IsSmiCompare() { return compare_type_ == SMI_ONLY; }
bool IsObjectCompare() { return compare_type_ == OBJECT_ONLY; }
private:
Expression* label_;
Label body_target_;
ZoneList<Statement*>* statements_;
int position_;
enum CompareTypeFeedback { NONE, SMI_ONLY, OBJECT_ONLY };
CompareTypeFeedback compare_type_;
int compare_id_;
int entry_id_;
};
class SwitchStatement: public BreakableStatement {
public:
inline SwitchStatement(Isolate* isolate, ZoneStringList* labels);
DECLARE_NODE_TYPE(SwitchStatement)
void Initialize(Expression* tag, ZoneList<CaseClause*>* cases) {
tag_ = tag;
cases_ = cases;
}
Expression* tag() const { return tag_; }
ZoneList<CaseClause*>* cases() const { return cases_; }
virtual bool IsInlineable() const;
private:
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: public Statement {
public:
IfStatement(Isolate* isolate,
Expression* condition,
Statement* then_statement,
Statement* else_statement)
: condition_(condition),
then_statement_(then_statement),
else_statement_(else_statement),
if_id_(GetNextId(isolate)),
then_id_(GetNextId(isolate)),
else_id_(GetNextId(isolate)) {
}
DECLARE_NODE_TYPE(IfStatement)
virtual bool IsInlineable() const;
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_; }
int IfId() const { return if_id_; }
int ThenId() const { return then_id_; }
int ElseId() const { return else_id_; }
private:
Expression* condition_;
Statement* then_statement_;
Statement* else_statement_;
int if_id_;
int then_id_;
int else_id_;
};
// NOTE: TargetCollectors are represented as nodes to fit in the target
// stack in the compiler; this should probably be reworked.
class TargetCollector: public AstNode {
public:
TargetCollector(): targets_(0) { }
// Adds a jump target to the collector. The collector stores a pointer not
// a copy of the target to make binding work, so make sure not to pass in
// references to something on the stack.
void AddTarget(Label* target);
// Virtual behaviour. TargetCollectors are never part of the AST.
virtual void Accept(AstVisitor* v) { UNREACHABLE(); }
virtual TargetCollector* AsTargetCollector() { return this; }
ZoneList<Label*>* targets() { return &targets_; }
virtual bool IsInlineable() const;
private:
ZoneList<Label*> targets_;
};
class TryStatement: public Statement {
public:
explicit TryStatement(Block* try_block)
: try_block_(try_block), escaping_targets_(NULL) { }
void set_escaping_targets(ZoneList<Label*>* targets) {
escaping_targets_ = targets;
}
Block* try_block() const { return try_block_; }
ZoneList<Label*>* escaping_targets() const { return escaping_targets_; }
virtual bool IsInlineable() const;
private:
Block* try_block_;
ZoneList<Label*>* escaping_targets_;
};
class TryCatchStatement: public TryStatement {
public:
TryCatchStatement(Block* try_block,
Scope* scope,
Variable* variable,
Block* catch_block)
: TryStatement(try_block),
scope_(scope),
variable_(variable),
catch_block_(catch_block) {
}
DECLARE_NODE_TYPE(TryCatchStatement)
Scope* scope() { return scope_; }
Variable* variable() { return variable_; }
Block* catch_block() const { return catch_block_; }
virtual bool IsInlineable() const;
private:
Scope* scope_;
Variable* variable_;
Block* catch_block_;
};
class TryFinallyStatement: public TryStatement {
public:
TryFinallyStatement(Block* try_block, Block* finally_block)
: TryStatement(try_block),
finally_block_(finally_block) { }
DECLARE_NODE_TYPE(TryFinallyStatement)
Block* finally_block() const { return finally_block_; }
virtual bool IsInlineable() const;
private:
Block* finally_block_;
};
class DebuggerStatement: public Statement {
public:
DECLARE_NODE_TYPE(DebuggerStatement)
virtual bool IsInlineable() const;
};
class EmptyStatement: public Statement {
public:
DECLARE_NODE_TYPE(EmptyStatement)
virtual bool IsInlineable() const;
};
class Literal: public Expression {
public:
Literal(Isolate* isolate, Handle<Object> handle)
: Expression(isolate), handle_(handle) { }
DECLARE_NODE_TYPE(Literal)
virtual bool IsTrivial() { return true; }
virtual bool IsSmiLiteral() { return handle_->IsSmi(); }
// Check if this literal is identical to the other literal.
bool IsIdenticalTo(const Literal* other) const {
return handle_.is_identical_to(other->handle_);
}
virtual bool IsPropertyName() {
if (handle_->IsSymbol()) {
uint32_t ignored;
return !String::cast(*handle_)->AsArrayIndex(&ignored);
}
return false;
}
Handle<String> AsPropertyName() {
ASSERT(IsPropertyName());
return Handle<String>::cast(handle_);
}
virtual bool ToBooleanIsTrue() { return handle_->ToBoolean()->IsTrue(); }
virtual bool ToBooleanIsFalse() { return handle_->ToBoolean()->IsFalse(); }
// Identity testers.
bool IsNull() const {
ASSERT(!handle_.is_null());
return handle_->IsNull();
}
bool IsTrue() const {
ASSERT(!handle_.is_null());
return handle_->IsTrue();
}
bool IsFalse() const {
ASSERT(!handle_.is_null());
return handle_->IsFalse();
}
Handle<Object> handle() const { return handle_; }
virtual bool IsInlineable() const;
private:
Handle<Object> handle_;
};
// Base class for literals that needs space in the corresponding JSFunction.
class MaterializedLiteral: public Expression {
public:
MaterializedLiteral(Isolate* isolate,
int literal_index,
bool is_simple,
int depth)
: Expression(isolate),
literal_index_(literal_index),
is_simple_(is_simple),
depth_(depth) {}
virtual MaterializedLiteral* AsMaterializedLiteral() { return this; }
int literal_index() { return literal_index_; }
// A materialized literal is simple if the values consist of only
// constants and simple object and array literals.
bool is_simple() const { return is_simple_; }
int depth() const { return depth_; }
virtual bool IsInlineable() const;
private:
int literal_index_;
bool is_simple_;
int depth_;
};
// An object literal has a boilerplate object that is used
// for minimizing the work when constructing it at runtime.
class ObjectLiteral: public MaterializedLiteral {
public:
// Property is used for passing information
// about an object literal's properties from the parser
// to the code generator.
class Property: public ZoneObject {
public:
enum Kind {
CONSTANT, // Property with constant value (compile time).
COMPUTED, // Property with computed value (execution time).
MATERIALIZED_LITERAL, // Property value is a materialized literal.
GETTER, SETTER, // Property is an accessor function.
PROTOTYPE // Property is __proto__.
};
Property(Literal* key, Expression* value);
Property(bool is_getter, FunctionLiteral* value);
Literal* key() { return key_; }
Expression* value() { return value_; }
Kind kind() { return kind_; }
bool IsCompileTimeValue();
void set_emit_store(bool emit_store);
bool emit_store();
private:
Literal* key_;
Expression* value_;
Kind kind_;