1 // Copyright 2012 the V8 project authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
10 #include "src/assembler.h"
11 #include "src/ast-value-factory.h"
12 #include "src/bailout-reason.h"
13 #include "src/factory.h"
14 #include "src/isolate.h"
15 #include "src/jsregexp.h"
16 #include "src/list-inl.h"
17 #include "src/modules.h"
18 #include "src/runtime/runtime.h"
19 #include "src/small-pointer-list.h"
20 #include "src/smart-pointers.h"
21 #include "src/token.h"
22 #include "src/types.h"
23 #include "src/utils.h"
24 #include "src/variables.h"
29 // The abstract syntax tree is an intermediate, light-weight
30 // representation of the parsed JavaScript code suitable for
31 // compilation to native code.
33 // Nodes are allocated in a separate zone, which allows faster
34 // allocation and constant-time deallocation of the entire syntax
38 // ----------------------------------------------------------------------------
39 // Nodes of the abstract syntax tree. Only concrete classes are
42 #define DECLARATION_NODE_LIST(V) \
43 V(VariableDeclaration) \
44 V(FunctionDeclaration) \
45 V(ImportDeclaration) \
48 #define STATEMENT_NODE_LIST(V) \
50 V(ExpressionStatement) \
53 V(ContinueStatement) \
63 V(TryCatchStatement) \
64 V(TryFinallyStatement) \
67 #define EXPRESSION_NODE_LIST(V) \
70 V(NativeFunctionLiteral) \
90 V(SuperPropertyReference) \
91 V(SuperCallReference) \
94 #define AST_NODE_LIST(V) \
95 DECLARATION_NODE_LIST(V) \
96 STATEMENT_NODE_LIST(V) \
97 EXPRESSION_NODE_LIST(V)
99 // Forward declarations
100 class AstNodeFactory;
104 class BreakableStatement;
106 class IterationStatement;
107 class MaterializedLiteral;
109 class TypeFeedbackOracle;
111 class RegExpAlternative;
112 class RegExpAssertion;
114 class RegExpBackReference;
116 class RegExpCharacterClass;
117 class RegExpCompiler;
118 class RegExpDisjunction;
120 class RegExpLookahead;
121 class RegExpQuantifier;
124 #define DEF_FORWARD_DECLARATION(type) class type;
125 AST_NODE_LIST(DEF_FORWARD_DECLARATION)
126 #undef DEF_FORWARD_DECLARATION
129 // Typedef only introduced to avoid unreadable code.
130 // Please do appreciate the required space in "> >".
131 typedef ZoneList<Handle<String> > ZoneStringList;
132 typedef ZoneList<Handle<Object> > ZoneObjectList;
135 #define DECLARE_NODE_TYPE(type) \
136 void Accept(AstVisitor* v) override; \
137 AstNode::NodeType node_type() const final { return AstNode::k##type; } \
138 friend class AstNodeFactory;
141 enum AstPropertiesFlag {
149 class FeedbackVectorRequirements {
151 FeedbackVectorRequirements(int slots, int ic_slots)
152 : slots_(slots), ic_slots_(ic_slots) {}
154 int slots() const { return slots_; }
155 int ic_slots() const { return ic_slots_; }
163 class VariableICSlotPair final {
165 VariableICSlotPair(Variable* variable, FeedbackVectorICSlot slot)
166 : variable_(variable), slot_(slot) {}
168 : variable_(NULL), slot_(FeedbackVectorICSlot::Invalid()) {}
170 Variable* variable() const { return variable_; }
171 FeedbackVectorICSlot slot() const { return slot_; }
175 FeedbackVectorICSlot slot_;
179 typedef List<VariableICSlotPair> ICSlotCache;
182 class AstProperties final BASE_EMBEDDED {
184 class Flags : public EnumSet<AstPropertiesFlag, int> {};
186 explicit AstProperties(Zone* zone) : node_count_(0), spec_(zone) {}
188 Flags* flags() { return &flags_; }
189 int node_count() { return node_count_; }
190 void add_node_count(int count) { node_count_ += count; }
192 int slots() const { return spec_.slots(); }
193 void increase_slots(int count) { spec_.increase_slots(count); }
195 int ic_slots() const { return spec_.ic_slots(); }
196 void increase_ic_slots(int count) { spec_.increase_ic_slots(count); }
197 void SetKind(int ic_slot, Code::Kind kind) { spec_.SetKind(ic_slot, kind); }
198 const ZoneFeedbackVectorSpec* get_spec() const { return &spec_; }
203 ZoneFeedbackVectorSpec spec_;
207 class AstNode: public ZoneObject {
209 #define DECLARE_TYPE_ENUM(type) k##type,
211 AST_NODE_LIST(DECLARE_TYPE_ENUM)
214 #undef DECLARE_TYPE_ENUM
216 void* operator new(size_t size, Zone* zone) { return zone->New(size); }
218 explicit AstNode(int position): position_(position) {}
219 virtual ~AstNode() {}
221 virtual void Accept(AstVisitor* v) = 0;
222 virtual NodeType node_type() const = 0;
223 int position() const { return position_; }
225 // Type testing & conversion functions overridden by concrete subclasses.
226 #define DECLARE_NODE_FUNCTIONS(type) \
227 bool Is##type() const { return node_type() == AstNode::k##type; } \
229 return Is##type() ? reinterpret_cast<type*>(this) : NULL; \
231 const type* As##type() const { \
232 return Is##type() ? reinterpret_cast<const type*>(this) : NULL; \
234 AST_NODE_LIST(DECLARE_NODE_FUNCTIONS)
235 #undef DECLARE_NODE_FUNCTIONS
237 virtual BreakableStatement* AsBreakableStatement() { return NULL; }
238 virtual IterationStatement* AsIterationStatement() { return NULL; }
239 virtual MaterializedLiteral* AsMaterializedLiteral() { return NULL; }
241 // The interface for feedback slots, with default no-op implementations for
242 // node types which don't actually have this. Note that this is conceptually
243 // not really nice, but multiple inheritance would introduce yet another
244 // vtable entry per node, something we don't want for space reasons.
245 virtual FeedbackVectorRequirements ComputeFeedbackRequirements(
246 Isolate* isolate, const ICSlotCache* cache) {
247 return FeedbackVectorRequirements(0, 0);
249 virtual void SetFirstFeedbackSlot(FeedbackVectorSlot slot) { UNREACHABLE(); }
250 virtual void SetFirstFeedbackICSlot(FeedbackVectorICSlot slot,
251 ICSlotCache* cache) {
254 // Each ICSlot stores a kind of IC which the participating node should know.
255 virtual Code::Kind FeedbackICSlotKind(int index) {
257 return Code::NUMBER_OF_KINDS;
261 // Hidden to prevent accidental usage. It would have to load the
262 // current zone from the TLS.
263 void* operator new(size_t size);
265 friend class CaseClause; // Generates AST IDs.
271 class Statement : public AstNode {
273 explicit Statement(Zone* zone, int position) : AstNode(position) {}
275 bool IsEmpty() { return AsEmptyStatement() != NULL; }
276 virtual bool IsJump() const { return false; }
280 class SmallMapList final {
283 SmallMapList(int capacity, Zone* zone) : list_(capacity, zone) {}
285 void Reserve(int capacity, Zone* zone) { list_.Reserve(capacity, zone); }
286 void Clear() { list_.Clear(); }
287 void Sort() { list_.Sort(); }
289 bool is_empty() const { return list_.is_empty(); }
290 int length() const { return list_.length(); }
292 void AddMapIfMissing(Handle<Map> map, Zone* zone) {
293 if (!Map::TryUpdate(map).ToHandle(&map)) return;
294 for (int i = 0; i < length(); ++i) {
295 if (at(i).is_identical_to(map)) return;
300 void FilterForPossibleTransitions(Map* root_map) {
301 for (int i = list_.length() - 1; i >= 0; i--) {
302 if (at(i)->FindRootMap() != root_map) {
303 list_.RemoveElement(list_.at(i));
308 void Add(Handle<Map> handle, Zone* zone) {
309 list_.Add(handle.location(), zone);
312 Handle<Map> at(int i) const {
313 return Handle<Map>(list_.at(i));
316 Handle<Map> first() const { return at(0); }
317 Handle<Map> last() const { return at(length() - 1); }
320 // The list stores pointers to Map*, that is Map**, so it's GC safe.
321 SmallPointerList<Map*> list_;
323 DISALLOW_COPY_AND_ASSIGN(SmallMapList);
327 class Expression : public AstNode {
330 // Not assigned a context yet, or else will not be visited during
333 // Evaluated for its side effects.
335 // Evaluated for its value (and side effects).
337 // Evaluated for control flow (and side effects).
341 virtual bool IsValidReferenceExpression() const { return false; }
343 // Helpers for ToBoolean conversion.
344 virtual bool ToBooleanIsTrue() const { return false; }
345 virtual bool ToBooleanIsFalse() const { return false; }
347 // Symbols that cannot be parsed as array indices are considered property
348 // names. We do not treat symbols that can be array indexes as property
349 // names because [] for string objects is handled only by keyed ICs.
350 virtual bool IsPropertyName() const { return false; }
352 // True iff the expression is a literal represented as a smi.
353 bool IsSmiLiteral() const;
355 // True iff the expression is a string literal.
356 bool IsStringLiteral() const;
358 // True iff the expression is the null literal.
359 bool IsNullLiteral() const;
361 // True if we can prove that the expression is the undefined literal.
362 bool IsUndefinedLiteral(Isolate* isolate) const;
364 // Expression type bounds
365 Bounds bounds() const { return bounds_; }
366 void set_bounds(Bounds bounds) { bounds_ = bounds; }
368 // Type feedback information for assignments and properties.
369 virtual bool IsMonomorphic() {
373 virtual SmallMapList* GetReceiverTypes() {
377 virtual KeyedAccessStoreMode GetStoreMode() const {
379 return STANDARD_STORE;
381 virtual IcCheckType GetKeyType() const {
386 // TODO(rossberg): this should move to its own AST node eventually.
387 virtual void RecordToBooleanTypeFeedback(TypeFeedbackOracle* oracle);
388 byte to_boolean_types() const {
389 return ToBooleanTypesField::decode(bit_field_);
392 void set_base_id(int id) { base_id_ = id; }
393 static int num_ids() { return parent_num_ids() + 2; }
394 BailoutId id() const { return BailoutId(local_id(0)); }
395 TypeFeedbackId test_id() const { return TypeFeedbackId(local_id(1)); }
398 Expression(Zone* zone, int pos)
400 base_id_(BailoutId::None().ToInt()),
401 bounds_(Bounds::Unbounded(zone)),
403 static int parent_num_ids() { return 0; }
404 void set_to_boolean_types(byte types) {
405 bit_field_ = ToBooleanTypesField::update(bit_field_, types);
408 int base_id() const {
409 DCHECK(!BailoutId(base_id_).IsNone());
414 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
418 class ToBooleanTypesField : public BitField16<byte, 0, 8> {};
420 // Ends with 16-bit field; deriving classes in turn begin with
421 // 16-bit fields for optimum packing efficiency.
425 class BreakableStatement : public Statement {
428 TARGET_FOR_ANONYMOUS,
429 TARGET_FOR_NAMED_ONLY
432 // The labels associated with this statement. May be NULL;
433 // if it is != NULL, guaranteed to contain at least one entry.
434 ZoneList<const AstRawString*>* labels() const { return labels_; }
436 // Type testing & conversion.
437 BreakableStatement* AsBreakableStatement() final { return this; }
440 Label* break_target() { return &break_target_; }
443 bool is_target_for_anonymous() const {
444 return breakable_type_ == TARGET_FOR_ANONYMOUS;
447 void set_base_id(int id) { base_id_ = id; }
448 static int num_ids() { return parent_num_ids() + 2; }
449 BailoutId EntryId() const { return BailoutId(local_id(0)); }
450 BailoutId ExitId() const { return BailoutId(local_id(1)); }
453 BreakableStatement(Zone* zone, ZoneList<const AstRawString*>* labels,
454 BreakableType breakable_type, int position)
455 : Statement(zone, position),
457 breakable_type_(breakable_type),
458 base_id_(BailoutId::None().ToInt()) {
459 DCHECK(labels == NULL || labels->length() > 0);
461 static int parent_num_ids() { return 0; }
463 int base_id() const {
464 DCHECK(!BailoutId(base_id_).IsNone());
469 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
471 ZoneList<const AstRawString*>* labels_;
472 BreakableType breakable_type_;
478 class Block final : public BreakableStatement {
480 DECLARE_NODE_TYPE(Block)
482 void AddStatement(Statement* statement, Zone* zone) {
483 statements_.Add(statement, zone);
486 ZoneList<Statement*>* statements() { return &statements_; }
487 bool is_initializer_block() const { return is_initializer_block_; }
489 static int num_ids() { return parent_num_ids() + 1; }
490 BailoutId DeclsId() const { return BailoutId(local_id(0)); }
492 bool IsJump() const override {
493 return !statements_.is_empty() && statements_.last()->IsJump()
494 && labels() == NULL; // Good enough as an approximation...
497 Scope* scope() const { return scope_; }
498 void set_scope(Scope* scope) { scope_ = scope; }
501 Block(Zone* zone, ZoneList<const AstRawString*>* labels, int capacity,
502 bool is_initializer_block, int pos)
503 : BreakableStatement(zone, labels, TARGET_FOR_NAMED_ONLY, pos),
504 statements_(capacity, zone),
505 is_initializer_block_(is_initializer_block),
507 static int parent_num_ids() { return BreakableStatement::num_ids(); }
510 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
512 ZoneList<Statement*> statements_;
513 bool is_initializer_block_;
518 class Declaration : public AstNode {
520 VariableProxy* proxy() const { return proxy_; }
521 VariableMode mode() const { return mode_; }
522 Scope* scope() const { return scope_; }
523 virtual InitializationFlag initialization() const = 0;
524 virtual bool IsInlineable() const;
527 Declaration(Zone* zone, VariableProxy* proxy, VariableMode mode, Scope* scope,
529 : AstNode(pos), mode_(mode), proxy_(proxy), scope_(scope) {
530 DCHECK(IsDeclaredVariableMode(mode));
535 VariableProxy* proxy_;
537 // Nested scope from which the declaration originated.
542 class VariableDeclaration final : public Declaration {
544 DECLARE_NODE_TYPE(VariableDeclaration)
546 InitializationFlag initialization() const override {
547 return mode() == VAR ? kCreatedInitialized : kNeedsInitialization;
550 bool is_class_declaration() const { return is_class_declaration_; }
552 // VariableDeclarations can be grouped into consecutive declaration
553 // groups. Each VariableDeclaration is associated with the start position of
554 // the group it belongs to. The positions are used for strong mode scope
555 // checks for classes and functions.
556 int declaration_group_start() const { return declaration_group_start_; }
559 VariableDeclaration(Zone* zone, VariableProxy* proxy, VariableMode mode,
560 Scope* scope, int pos, bool is_class_declaration = false,
561 int declaration_group_start = -1)
562 : Declaration(zone, proxy, mode, scope, pos),
563 is_class_declaration_(is_class_declaration),
564 declaration_group_start_(declaration_group_start) {}
566 bool is_class_declaration_;
567 int declaration_group_start_;
571 class FunctionDeclaration final : public Declaration {
573 DECLARE_NODE_TYPE(FunctionDeclaration)
575 FunctionLiteral* fun() const { return fun_; }
576 InitializationFlag initialization() const override {
577 return kCreatedInitialized;
579 bool IsInlineable() const override;
582 FunctionDeclaration(Zone* zone,
583 VariableProxy* proxy,
585 FunctionLiteral* fun,
588 : Declaration(zone, proxy, mode, scope, pos),
590 DCHECK(mode == VAR || mode == LET || mode == CONST);
595 FunctionLiteral* fun_;
599 class ImportDeclaration final : public Declaration {
601 DECLARE_NODE_TYPE(ImportDeclaration)
603 const AstRawString* import_name() const { return import_name_; }
604 const AstRawString* module_specifier() const { return module_specifier_; }
605 void set_module_specifier(const AstRawString* module_specifier) {
606 DCHECK(module_specifier_ == NULL);
607 module_specifier_ = module_specifier;
609 InitializationFlag initialization() const override {
610 return kNeedsInitialization;
614 ImportDeclaration(Zone* zone, VariableProxy* proxy,
615 const AstRawString* import_name,
616 const AstRawString* module_specifier, Scope* scope, int pos)
617 : Declaration(zone, proxy, IMPORT, scope, pos),
618 import_name_(import_name),
619 module_specifier_(module_specifier) {}
622 const AstRawString* import_name_;
623 const AstRawString* module_specifier_;
627 class ExportDeclaration final : public Declaration {
629 DECLARE_NODE_TYPE(ExportDeclaration)
631 InitializationFlag initialization() const override {
632 return kCreatedInitialized;
636 ExportDeclaration(Zone* zone, VariableProxy* proxy, Scope* scope, int pos)
637 : Declaration(zone, proxy, LET, scope, pos) {}
641 class Module : public AstNode {
643 ModuleDescriptor* descriptor() const { return descriptor_; }
644 Block* body() const { return body_; }
647 Module(Zone* zone, int pos)
648 : AstNode(pos), descriptor_(ModuleDescriptor::New(zone)), body_(NULL) {}
649 Module(Zone* zone, ModuleDescriptor* descriptor, int pos, Block* body = NULL)
650 : AstNode(pos), descriptor_(descriptor), body_(body) {}
653 ModuleDescriptor* descriptor_;
658 class IterationStatement : public BreakableStatement {
660 // Type testing & conversion.
661 IterationStatement* AsIterationStatement() final { return this; }
663 Statement* body() const { return body_; }
665 static int num_ids() { return parent_num_ids() + 1; }
666 BailoutId OsrEntryId() const { return BailoutId(local_id(0)); }
667 virtual BailoutId ContinueId() const = 0;
668 virtual BailoutId StackCheckId() const = 0;
671 Label* continue_target() { return &continue_target_; }
674 IterationStatement(Zone* zone, ZoneList<const AstRawString*>* labels, int pos)
675 : BreakableStatement(zone, labels, TARGET_FOR_ANONYMOUS, pos),
677 static int parent_num_ids() { return BreakableStatement::num_ids(); }
678 void Initialize(Statement* body) { body_ = body; }
681 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
684 Label continue_target_;
688 class DoWhileStatement final : public IterationStatement {
690 DECLARE_NODE_TYPE(DoWhileStatement)
692 void Initialize(Expression* cond, Statement* body) {
693 IterationStatement::Initialize(body);
697 Expression* cond() const { return cond_; }
699 static int num_ids() { return parent_num_ids() + 2; }
700 BailoutId ContinueId() const override { return BailoutId(local_id(0)); }
701 BailoutId StackCheckId() const override { return BackEdgeId(); }
702 BailoutId BackEdgeId() const { return BailoutId(local_id(1)); }
705 DoWhileStatement(Zone* zone, ZoneList<const AstRawString*>* labels, int pos)
706 : IterationStatement(zone, labels, pos), cond_(NULL) {}
707 static int parent_num_ids() { return IterationStatement::num_ids(); }
710 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
716 class WhileStatement final : public IterationStatement {
718 DECLARE_NODE_TYPE(WhileStatement)
720 void Initialize(Expression* cond, Statement* body) {
721 IterationStatement::Initialize(body);
725 Expression* cond() const { return cond_; }
727 static int num_ids() { return parent_num_ids() + 1; }
728 BailoutId ContinueId() const override { return EntryId(); }
729 BailoutId StackCheckId() const override { return BodyId(); }
730 BailoutId BodyId() const { return BailoutId(local_id(0)); }
733 WhileStatement(Zone* zone, ZoneList<const AstRawString*>* labels, int pos)
734 : IterationStatement(zone, labels, pos), cond_(NULL) {}
735 static int parent_num_ids() { return IterationStatement::num_ids(); }
738 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
744 class ForStatement final : public IterationStatement {
746 DECLARE_NODE_TYPE(ForStatement)
748 void Initialize(Statement* init,
752 IterationStatement::Initialize(body);
758 Statement* init() const { return init_; }
759 Expression* cond() const { return cond_; }
760 Statement* next() const { return next_; }
762 static int num_ids() { return parent_num_ids() + 2; }
763 BailoutId ContinueId() const override { return BailoutId(local_id(0)); }
764 BailoutId StackCheckId() const override { return BodyId(); }
765 BailoutId BodyId() const { return BailoutId(local_id(1)); }
768 ForStatement(Zone* zone, ZoneList<const AstRawString*>* labels, int pos)
769 : IterationStatement(zone, labels, pos),
773 static int parent_num_ids() { return IterationStatement::num_ids(); }
776 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
784 class ForEachStatement : public IterationStatement {
787 ENUMERATE, // for (each in subject) body;
788 ITERATE // for (each of subject) body;
791 void Initialize(Expression* each, Expression* subject, Statement* body) {
792 IterationStatement::Initialize(body);
797 Expression* each() const { return each_; }
798 Expression* subject() const { return subject_; }
800 FeedbackVectorRequirements ComputeFeedbackRequirements(
801 Isolate* isolate, const ICSlotCache* cache) override;
802 void SetFirstFeedbackICSlot(FeedbackVectorICSlot slot,
803 ICSlotCache* cache) override {
806 Code::Kind FeedbackICSlotKind(int index) override;
807 FeedbackVectorICSlot EachFeedbackSlot() const { return each_slot_; }
810 ForEachStatement(Zone* zone, ZoneList<const AstRawString*>* labels, int pos)
811 : IterationStatement(zone, labels, pos),
814 each_slot_(FeedbackVectorICSlot::Invalid()) {}
818 Expression* subject_;
819 FeedbackVectorICSlot each_slot_;
823 class ForInStatement final : public ForEachStatement {
825 DECLARE_NODE_TYPE(ForInStatement)
827 Expression* enumerable() const {
831 // Type feedback information.
832 FeedbackVectorRequirements ComputeFeedbackRequirements(
833 Isolate* isolate, const ICSlotCache* cache) override {
834 FeedbackVectorRequirements base =
835 ForEachStatement::ComputeFeedbackRequirements(isolate, cache);
836 DCHECK(base.slots() == 0 && base.ic_slots() <= 1);
837 return FeedbackVectorRequirements(1, base.ic_slots());
839 void SetFirstFeedbackSlot(FeedbackVectorSlot slot) override {
840 for_in_feedback_slot_ = slot;
843 FeedbackVectorSlot ForInFeedbackSlot() {
844 DCHECK(!for_in_feedback_slot_.IsInvalid());
845 return for_in_feedback_slot_;
848 enum ForInType { FAST_FOR_IN, SLOW_FOR_IN };
849 ForInType for_in_type() const { return for_in_type_; }
850 void set_for_in_type(ForInType type) { for_in_type_ = type; }
852 static int num_ids() { return parent_num_ids() + 6; }
853 BailoutId BodyId() const { return BailoutId(local_id(0)); }
854 BailoutId PrepareId() const { return BailoutId(local_id(1)); }
855 BailoutId EnumId() const { return BailoutId(local_id(2)); }
856 BailoutId ToObjectId() const { return BailoutId(local_id(3)); }
857 BailoutId FilterId() const { return BailoutId(local_id(4)); }
858 BailoutId AssignmentId() const { return BailoutId(local_id(5)); }
859 BailoutId ContinueId() const override { return EntryId(); }
860 BailoutId StackCheckId() const override { return BodyId(); }
863 ForInStatement(Zone* zone, ZoneList<const AstRawString*>* labels, int pos)
864 : ForEachStatement(zone, labels, pos),
865 for_in_type_(SLOW_FOR_IN),
866 for_in_feedback_slot_(FeedbackVectorSlot::Invalid()) {}
867 static int parent_num_ids() { return ForEachStatement::num_ids(); }
870 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
872 ForInType for_in_type_;
873 FeedbackVectorSlot for_in_feedback_slot_;
877 class ForOfStatement final : public ForEachStatement {
879 DECLARE_NODE_TYPE(ForOfStatement)
881 void Initialize(Expression* each,
884 Expression* assign_iterator,
885 Expression* next_result,
886 Expression* result_done,
887 Expression* assign_each) {
888 ForEachStatement::Initialize(each, subject, body);
889 assign_iterator_ = assign_iterator;
890 next_result_ = next_result;
891 result_done_ = result_done;
892 assign_each_ = assign_each;
895 Expression* iterable() const {
899 // iterator = subject[Symbol.iterator]()
900 Expression* assign_iterator() const {
901 return assign_iterator_;
904 // result = iterator.next() // with type check
905 Expression* next_result() const {
910 Expression* result_done() const {
914 // each = result.value
915 Expression* assign_each() const {
919 BailoutId ContinueId() const override { return EntryId(); }
920 BailoutId StackCheckId() const override { return BackEdgeId(); }
922 static int num_ids() { return parent_num_ids() + 1; }
923 BailoutId BackEdgeId() const { return BailoutId(local_id(0)); }
926 ForOfStatement(Zone* zone, ZoneList<const AstRawString*>* labels, int pos)
927 : ForEachStatement(zone, labels, pos),
928 assign_iterator_(NULL),
931 assign_each_(NULL) {}
932 static int parent_num_ids() { return ForEachStatement::num_ids(); }
935 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
937 Expression* assign_iterator_;
938 Expression* next_result_;
939 Expression* result_done_;
940 Expression* assign_each_;
944 class ExpressionStatement final : public Statement {
946 DECLARE_NODE_TYPE(ExpressionStatement)
948 void set_expression(Expression* e) { expression_ = e; }
949 Expression* expression() const { return expression_; }
950 bool IsJump() const override { return expression_->IsThrow(); }
953 ExpressionStatement(Zone* zone, Expression* expression, int pos)
954 : Statement(zone, pos), expression_(expression) { }
957 Expression* expression_;
961 class JumpStatement : public Statement {
963 bool IsJump() const final { return true; }
966 explicit JumpStatement(Zone* zone, int pos) : Statement(zone, pos) {}
970 class ContinueStatement final : public JumpStatement {
972 DECLARE_NODE_TYPE(ContinueStatement)
974 IterationStatement* target() const { return target_; }
977 explicit ContinueStatement(Zone* zone, IterationStatement* target, int pos)
978 : JumpStatement(zone, pos), target_(target) { }
981 IterationStatement* target_;
985 class BreakStatement final : public JumpStatement {
987 DECLARE_NODE_TYPE(BreakStatement)
989 BreakableStatement* target() const { return target_; }
992 explicit BreakStatement(Zone* zone, BreakableStatement* target, int pos)
993 : JumpStatement(zone, pos), target_(target) { }
996 BreakableStatement* target_;
1000 class ReturnStatement final : public JumpStatement {
1002 DECLARE_NODE_TYPE(ReturnStatement)
1004 Expression* expression() const { return expression_; }
1007 explicit ReturnStatement(Zone* zone, Expression* expression, int pos)
1008 : JumpStatement(zone, pos), expression_(expression) { }
1011 Expression* expression_;
1015 class WithStatement final : public Statement {
1017 DECLARE_NODE_TYPE(WithStatement)
1019 Scope* scope() { return scope_; }
1020 Expression* expression() const { return expression_; }
1021 Statement* statement() const { return statement_; }
1023 void set_base_id(int id) { base_id_ = id; }
1024 static int num_ids() { return parent_num_ids() + 1; }
1025 BailoutId EntryId() const { return BailoutId(local_id(0)); }
1028 WithStatement(Zone* zone, Scope* scope, Expression* expression,
1029 Statement* statement, int pos)
1030 : Statement(zone, pos),
1032 expression_(expression),
1033 statement_(statement),
1034 base_id_(BailoutId::None().ToInt()) {}
1035 static int parent_num_ids() { return 0; }
1037 int base_id() const {
1038 DCHECK(!BailoutId(base_id_).IsNone());
1043 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
1046 Expression* expression_;
1047 Statement* statement_;
1052 class CaseClause final : public Expression {
1054 DECLARE_NODE_TYPE(CaseClause)
1056 bool is_default() const { return label_ == NULL; }
1057 Expression* label() const {
1058 CHECK(!is_default());
1061 Label* body_target() { return &body_target_; }
1062 ZoneList<Statement*>* statements() const { return statements_; }
1064 static int num_ids() { return parent_num_ids() + 2; }
1065 BailoutId EntryId() const { return BailoutId(local_id(0)); }
1066 TypeFeedbackId CompareId() { return TypeFeedbackId(local_id(1)); }
1068 Type* compare_type() { return compare_type_; }
1069 void set_compare_type(Type* type) { compare_type_ = type; }
1072 static int parent_num_ids() { return Expression::num_ids(); }
1075 CaseClause(Zone* zone, Expression* label, ZoneList<Statement*>* statements,
1077 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
1081 ZoneList<Statement*>* statements_;
1082 Type* compare_type_;
1086 class SwitchStatement final : public BreakableStatement {
1088 DECLARE_NODE_TYPE(SwitchStatement)
1090 void Initialize(Expression* tag, ZoneList<CaseClause*>* cases) {
1095 Expression* tag() const { return tag_; }
1096 ZoneList<CaseClause*>* cases() const { return cases_; }
1099 SwitchStatement(Zone* zone, ZoneList<const AstRawString*>* labels, int pos)
1100 : BreakableStatement(zone, labels, TARGET_FOR_ANONYMOUS, pos),
1106 ZoneList<CaseClause*>* cases_;
1110 // If-statements always have non-null references to their then- and
1111 // else-parts. When parsing if-statements with no explicit else-part,
1112 // the parser implicitly creates an empty statement. Use the
1113 // HasThenStatement() and HasElseStatement() functions to check if a
1114 // given if-statement has a then- or an else-part containing code.
1115 class IfStatement final : public Statement {
1117 DECLARE_NODE_TYPE(IfStatement)
1119 bool HasThenStatement() const { return !then_statement()->IsEmpty(); }
1120 bool HasElseStatement() const { return !else_statement()->IsEmpty(); }
1122 Expression* condition() const { return condition_; }
1123 Statement* then_statement() const { return then_statement_; }
1124 Statement* else_statement() const { return else_statement_; }
1126 bool IsJump() const override {
1127 return HasThenStatement() && then_statement()->IsJump()
1128 && HasElseStatement() && else_statement()->IsJump();
1131 void set_base_id(int id) { base_id_ = id; }
1132 static int num_ids() { return parent_num_ids() + 3; }
1133 BailoutId IfId() const { return BailoutId(local_id(0)); }
1134 BailoutId ThenId() const { return BailoutId(local_id(1)); }
1135 BailoutId ElseId() const { return BailoutId(local_id(2)); }
1138 IfStatement(Zone* zone, Expression* condition, Statement* then_statement,
1139 Statement* else_statement, int pos)
1140 : Statement(zone, pos),
1141 condition_(condition),
1142 then_statement_(then_statement),
1143 else_statement_(else_statement),
1144 base_id_(BailoutId::None().ToInt()) {}
1145 static int parent_num_ids() { return 0; }
1147 int base_id() const {
1148 DCHECK(!BailoutId(base_id_).IsNone());
1153 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
1155 Expression* condition_;
1156 Statement* then_statement_;
1157 Statement* else_statement_;
1162 class TryStatement : public Statement {
1164 int index() const { return index_; }
1165 Block* try_block() const { return try_block_; }
1168 TryStatement(Zone* zone, int index, Block* try_block, int pos)
1169 : Statement(zone, pos), index_(index), try_block_(try_block) {}
1172 // Unique (per-function) index of this handler. This is not an AST ID.
1179 class TryCatchStatement final : public TryStatement {
1181 DECLARE_NODE_TYPE(TryCatchStatement)
1183 Scope* scope() { return scope_; }
1184 Variable* variable() { return variable_; }
1185 Block* catch_block() const { return catch_block_; }
1188 TryCatchStatement(Zone* zone,
1195 : TryStatement(zone, index, try_block, pos),
1197 variable_(variable),
1198 catch_block_(catch_block) {
1203 Variable* variable_;
1204 Block* catch_block_;
1208 class TryFinallyStatement final : public TryStatement {
1210 DECLARE_NODE_TYPE(TryFinallyStatement)
1212 Block* finally_block() const { return finally_block_; }
1215 TryFinallyStatement(
1216 Zone* zone, int index, Block* try_block, Block* finally_block, int pos)
1217 : TryStatement(zone, index, try_block, pos),
1218 finally_block_(finally_block) { }
1221 Block* finally_block_;
1225 class DebuggerStatement final : public Statement {
1227 DECLARE_NODE_TYPE(DebuggerStatement)
1229 void set_base_id(int id) { base_id_ = id; }
1230 static int num_ids() { return parent_num_ids() + 1; }
1231 BailoutId DebugBreakId() const { return BailoutId(local_id(0)); }
1234 explicit DebuggerStatement(Zone* zone, int pos)
1235 : Statement(zone, pos), base_id_(BailoutId::None().ToInt()) {}
1236 static int parent_num_ids() { return 0; }
1238 int base_id() const {
1239 DCHECK(!BailoutId(base_id_).IsNone());
1244 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
1250 class EmptyStatement final : public Statement {
1252 DECLARE_NODE_TYPE(EmptyStatement)
1255 explicit EmptyStatement(Zone* zone, int pos): Statement(zone, pos) {}
1259 class Literal final : public Expression {
1261 DECLARE_NODE_TYPE(Literal)
1263 bool IsPropertyName() const override { return value_->IsPropertyName(); }
1265 Handle<String> AsPropertyName() {
1266 DCHECK(IsPropertyName());
1267 return Handle<String>::cast(value());
1270 const AstRawString* AsRawPropertyName() {
1271 DCHECK(IsPropertyName());
1272 return value_->AsString();
1275 bool ToBooleanIsTrue() const override { return value()->BooleanValue(); }
1276 bool ToBooleanIsFalse() const override { return !value()->BooleanValue(); }
1278 Handle<Object> value() const { return value_->value(); }
1279 const AstValue* raw_value() const { return value_; }
1281 // Support for using Literal as a HashMap key. NOTE: Currently, this works
1282 // only for string and number literals!
1284 static bool Match(void* literal1, void* literal2);
1286 static int num_ids() { return parent_num_ids() + 1; }
1287 TypeFeedbackId LiteralFeedbackId() const {
1288 return TypeFeedbackId(local_id(0));
1292 Literal(Zone* zone, const AstValue* value, int position)
1293 : Expression(zone, position), value_(value) {}
1294 static int parent_num_ids() { return Expression::num_ids(); }
1297 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
1299 const AstValue* value_;
1303 // Base class for literals that needs space in the corresponding JSFunction.
1304 class MaterializedLiteral : public Expression {
1306 virtual MaterializedLiteral* AsMaterializedLiteral() { return this; }
1308 int literal_index() { return literal_index_; }
1311 // only callable after initialization.
1312 DCHECK(depth_ >= 1);
1316 bool is_strong() const { return is_strong_; }
1319 MaterializedLiteral(Zone* zone, int literal_index, bool is_strong, int pos)
1320 : Expression(zone, pos),
1321 literal_index_(literal_index),
1323 is_strong_(is_strong),
1326 // A materialized literal is simple if the values consist of only
1327 // constants and simple object and array literals.
1328 bool is_simple() const { return is_simple_; }
1329 void set_is_simple(bool is_simple) { is_simple_ = is_simple; }
1330 friend class CompileTimeValue;
1332 void set_depth(int depth) {
1337 // Populate the constant properties/elements fixed array.
1338 void BuildConstants(Isolate* isolate);
1339 friend class ArrayLiteral;
1340 friend class ObjectLiteral;
1342 // If the expression is a literal, return the literal value;
1343 // if the expression is a materialized literal and is simple return a
1344 // compile time value as encoded by CompileTimeValue::GetValue().
1345 // Otherwise, return undefined literal as the placeholder
1346 // in the object literal boilerplate.
1347 Handle<Object> GetBoilerplateValue(Expression* expression, Isolate* isolate);
1357 // Property is used for passing information
1358 // about an object literal's properties from the parser
1359 // to the code generator.
1360 class ObjectLiteralProperty final : public ZoneObject {
1363 CONSTANT, // Property with constant value (compile time).
1364 COMPUTED, // Property with computed value (execution time).
1365 MATERIALIZED_LITERAL, // Property value is a materialized literal.
1366 GETTER, SETTER, // Property is an accessor function.
1367 PROTOTYPE // Property is __proto__.
1370 Expression* key() { return key_; }
1371 Expression* value() { return value_; }
1372 Kind kind() { return kind_; }
1374 // Type feedback information.
1375 bool IsMonomorphic() { return !receiver_type_.is_null(); }
1376 Handle<Map> GetReceiverType() { return receiver_type_; }
1378 bool IsCompileTimeValue();
1380 void set_emit_store(bool emit_store);
1383 bool is_static() const { return is_static_; }
1384 bool is_computed_name() const { return is_computed_name_; }
1386 void set_receiver_type(Handle<Map> map) { receiver_type_ = map; }
1389 friend class AstNodeFactory;
1391 ObjectLiteralProperty(Expression* key, Expression* value, Kind kind,
1392 bool is_static, bool is_computed_name);
1393 ObjectLiteralProperty(AstValueFactory* ast_value_factory, Expression* key,
1394 Expression* value, bool is_static,
1395 bool is_computed_name);
1403 bool is_computed_name_;
1404 Handle<Map> receiver_type_;
1408 // An object literal has a boilerplate object that is used
1409 // for minimizing the work when constructing it at runtime.
1410 class ObjectLiteral final : public MaterializedLiteral {
1412 typedef ObjectLiteralProperty Property;
1414 DECLARE_NODE_TYPE(ObjectLiteral)
1416 Handle<FixedArray> constant_properties() const {
1417 return constant_properties_;
1419 int properties_count() const { return constant_properties_->length() / 2; }
1420 ZoneList<Property*>* properties() const { return properties_; }
1421 bool fast_elements() const { return fast_elements_; }
1422 bool may_store_doubles() const { return may_store_doubles_; }
1423 bool has_function() const { return has_function_; }
1424 bool has_elements() const { return has_elements_; }
1426 // Decide if a property should be in the object boilerplate.
1427 static bool IsBoilerplateProperty(Property* property);
1429 // Populate the constant properties fixed array.
1430 void BuildConstantProperties(Isolate* isolate);
1432 // Mark all computed expressions that are bound to a key that
1433 // is shadowed by a later occurrence of the same key. For the
1434 // marked expressions, no store code is emitted.
1435 void CalculateEmitStore(Zone* zone);
1437 // Assemble bitfield of flags for the CreateObjectLiteral helper.
1438 int ComputeFlags(bool disable_mementos = false) const {
1439 int flags = fast_elements() ? kFastElements : kNoFlags;
1440 flags |= has_function() ? kHasFunction : kNoFlags;
1441 if (depth() == 1 && !has_elements() && !may_store_doubles()) {
1442 flags |= kShallowProperties;
1444 if (disable_mementos) {
1445 flags |= kDisableMementos;
1456 kHasFunction = 1 << 1,
1457 kShallowProperties = 1 << 2,
1458 kDisableMementos = 1 << 3,
1462 struct Accessors: public ZoneObject {
1463 Accessors() : getter(NULL), setter(NULL) {}
1468 BailoutId CreateLiteralId() const { return BailoutId(local_id(0)); }
1470 // Return an AST id for a property that is used in simulate instructions.
1471 BailoutId GetIdForProperty(int i) { return BailoutId(local_id(i + 1)); }
1473 // Unlike other AST nodes, this number of bailout IDs allocated for an
1474 // ObjectLiteral can vary, so num_ids() is not a static method.
1475 int num_ids() const { return parent_num_ids() + 1 + properties()->length(); }
1477 // Object literals need one feedback slot for each non-trivial value, as well
1478 // as some slots for home objects.
1479 FeedbackVectorRequirements ComputeFeedbackRequirements(
1480 Isolate* isolate, const ICSlotCache* cache) override;
1481 void SetFirstFeedbackICSlot(FeedbackVectorICSlot slot,
1482 ICSlotCache* cache) override {
1485 Code::Kind FeedbackICSlotKind(int index) override { return Code::STORE_IC; }
1486 FeedbackVectorICSlot GetNthSlot(int n) const {
1487 return FeedbackVectorICSlot(slot_.ToInt() + n);
1490 // If value needs a home object, returns a valid feedback vector ic slot
1491 // given by slot_index, and increments slot_index.
1492 FeedbackVectorICSlot SlotForHomeObject(Expression* value,
1493 int* slot_index) const;
1496 int slot_count() const { return slot_count_; }
1500 ObjectLiteral(Zone* zone, ZoneList<Property*>* properties, int literal_index,
1501 int boilerplate_properties, bool has_function, bool is_strong,
1503 : MaterializedLiteral(zone, literal_index, is_strong, pos),
1504 properties_(properties),
1505 boilerplate_properties_(boilerplate_properties),
1506 fast_elements_(false),
1507 has_elements_(false),
1508 may_store_doubles_(false),
1509 has_function_(has_function),
1513 slot_(FeedbackVectorICSlot::Invalid()) {
1515 static int parent_num_ids() { return MaterializedLiteral::num_ids(); }
1518 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
1519 Handle<FixedArray> constant_properties_;
1520 ZoneList<Property*>* properties_;
1521 int boilerplate_properties_;
1522 bool fast_elements_;
1524 bool may_store_doubles_;
1527 // slot_count_ helps validate that the logic to allocate ic slots and the
1528 // logic to use them are in sync.
1531 FeedbackVectorICSlot slot_;
1535 // Node for capturing a regexp literal.
1536 class RegExpLiteral final : public MaterializedLiteral {
1538 DECLARE_NODE_TYPE(RegExpLiteral)
1540 Handle<String> pattern() const { return pattern_->string(); }
1541 Handle<String> flags() const { return flags_->string(); }
1544 RegExpLiteral(Zone* zone, const AstRawString* pattern,
1545 const AstRawString* flags, int literal_index, bool is_strong,
1547 : MaterializedLiteral(zone, literal_index, is_strong, pos),
1554 const AstRawString* pattern_;
1555 const AstRawString* flags_;
1559 // An array literal has a literals object that is used
1560 // for minimizing the work when constructing it at runtime.
1561 class ArrayLiteral final : public MaterializedLiteral {
1563 DECLARE_NODE_TYPE(ArrayLiteral)
1565 Handle<FixedArray> constant_elements() const { return constant_elements_; }
1566 ElementsKind constant_elements_kind() const {
1567 DCHECK_EQ(2, constant_elements_->length());
1568 return static_cast<ElementsKind>(
1569 Smi::cast(constant_elements_->get(0))->value());
1572 ZoneList<Expression*>* values() const { return values_; }
1574 BailoutId CreateLiteralId() const { return BailoutId(local_id(0)); }
1576 // Return an AST id for an element that is used in simulate instructions.
1577 BailoutId GetIdForElement(int i) { return BailoutId(local_id(i + 1)); }
1579 // Unlike other AST nodes, this number of bailout IDs allocated for an
1580 // ArrayLiteral can vary, so num_ids() is not a static method.
1581 int num_ids() const { return parent_num_ids() + 1 + values()->length(); }
1583 // Populate the constant elements fixed array.
1584 void BuildConstantElements(Isolate* isolate);
1586 // Assemble bitfield of flags for the CreateArrayLiteral helper.
1587 int ComputeFlags(bool disable_mementos = false) const {
1588 int flags = depth() == 1 ? kShallowElements : kNoFlags;
1589 if (disable_mementos) {
1590 flags |= kDisableMementos;
1600 kShallowElements = 1,
1601 kDisableMementos = 1 << 1,
1606 ArrayLiteral(Zone* zone, ZoneList<Expression*>* values, int literal_index,
1607 bool is_strong, int pos)
1608 : MaterializedLiteral(zone, literal_index, is_strong, pos),
1610 static int parent_num_ids() { return MaterializedLiteral::num_ids(); }
1613 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
1615 Handle<FixedArray> constant_elements_;
1616 ZoneList<Expression*>* values_;
1620 class VariableProxy final : public Expression {
1622 DECLARE_NODE_TYPE(VariableProxy)
1624 bool IsValidReferenceExpression() const override { return !is_this(); }
1626 bool IsArguments() const { return is_resolved() && var()->is_arguments(); }
1628 Handle<String> name() const { return raw_name()->string(); }
1629 const AstRawString* raw_name() const {
1630 return is_resolved() ? var_->raw_name() : raw_name_;
1633 Variable* var() const {
1634 DCHECK(is_resolved());
1637 void set_var(Variable* v) {
1638 DCHECK(!is_resolved());
1643 bool is_this() const { return IsThisField::decode(bit_field_); }
1645 bool is_assigned() const { return IsAssignedField::decode(bit_field_); }
1646 void set_is_assigned() {
1647 bit_field_ = IsAssignedField::update(bit_field_, true);
1650 bool is_resolved() const { return IsResolvedField::decode(bit_field_); }
1651 void set_is_resolved() {
1652 bit_field_ = IsResolvedField::update(bit_field_, true);
1655 int end_position() const { return end_position_; }
1657 // Bind this proxy to the variable var.
1658 void BindTo(Variable* var);
1660 bool UsesVariableFeedbackSlot() const {
1661 return var()->IsUnallocated() || var()->IsLookupSlot();
1664 virtual FeedbackVectorRequirements ComputeFeedbackRequirements(
1665 Isolate* isolate, const ICSlotCache* cache) override;
1667 void SetFirstFeedbackICSlot(FeedbackVectorICSlot slot,
1668 ICSlotCache* cache) override;
1669 Code::Kind FeedbackICSlotKind(int index) override { return Code::LOAD_IC; }
1670 FeedbackVectorICSlot VariableFeedbackSlot() {
1671 DCHECK(!UsesVariableFeedbackSlot() || !variable_feedback_slot_.IsInvalid());
1672 return variable_feedback_slot_;
1675 static int num_ids() { return parent_num_ids() + 1; }
1676 BailoutId BeforeId() const { return BailoutId(local_id(0)); }
1679 VariableProxy(Zone* zone, Variable* var, int start_position,
1682 VariableProxy(Zone* zone, const AstRawString* name,
1683 Variable::Kind variable_kind, int start_position,
1685 static int parent_num_ids() { return Expression::num_ids(); }
1686 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
1688 class IsThisField : public BitField8<bool, 0, 1> {};
1689 class IsAssignedField : public BitField8<bool, 1, 1> {};
1690 class IsResolvedField : public BitField8<bool, 2, 1> {};
1692 // Start with 16-bit (or smaller) field, which should get packed together
1693 // with Expression's trailing 16-bit field.
1695 FeedbackVectorICSlot variable_feedback_slot_;
1697 const AstRawString* raw_name_; // if !is_resolved_
1698 Variable* var_; // if is_resolved_
1700 // Position is stored in the AstNode superclass, but VariableProxy needs to
1701 // know its end position too (for error messages). It cannot be inferred from
1702 // the variable name length because it can contain escapes.
1707 // Left-hand side can only be a property, a global or a (parameter or local)
1713 NAMED_SUPER_PROPERTY,
1714 KEYED_SUPER_PROPERTY
1718 class Property final : public Expression {
1720 DECLARE_NODE_TYPE(Property)
1722 bool IsValidReferenceExpression() const override { return true; }
1724 Expression* obj() const { return obj_; }
1725 Expression* key() const { return key_; }
1727 static int num_ids() { return parent_num_ids() + 1; }
1728 BailoutId LoadId() const { return BailoutId(local_id(0)); }
1730 bool IsStringAccess() const {
1731 return IsStringAccessField::decode(bit_field_);
1734 // Type feedback information.
1735 bool IsMonomorphic() override { return receiver_types_.length() == 1; }
1736 SmallMapList* GetReceiverTypes() override { return &receiver_types_; }
1737 KeyedAccessStoreMode GetStoreMode() const override { return STANDARD_STORE; }
1738 IcCheckType GetKeyType() const override {
1739 return KeyTypeField::decode(bit_field_);
1741 bool IsUninitialized() const {
1742 return !is_for_call() && HasNoTypeInformation();
1744 bool HasNoTypeInformation() const {
1745 return GetInlineCacheState() == UNINITIALIZED;
1747 InlineCacheState GetInlineCacheState() const {
1748 return InlineCacheStateField::decode(bit_field_);
1750 void set_is_string_access(bool b) {
1751 bit_field_ = IsStringAccessField::update(bit_field_, b);
1753 void set_key_type(IcCheckType key_type) {
1754 bit_field_ = KeyTypeField::update(bit_field_, key_type);
1756 void set_inline_cache_state(InlineCacheState state) {
1757 bit_field_ = InlineCacheStateField::update(bit_field_, state);
1759 void mark_for_call() {
1760 bit_field_ = IsForCallField::update(bit_field_, true);
1762 bool is_for_call() const { return IsForCallField::decode(bit_field_); }
1764 bool IsSuperAccess() { return obj()->IsSuperPropertyReference(); }
1766 virtual FeedbackVectorRequirements ComputeFeedbackRequirements(
1767 Isolate* isolate, const ICSlotCache* cache) override {
1768 return FeedbackVectorRequirements(0, 1);
1770 void SetFirstFeedbackICSlot(FeedbackVectorICSlot slot,
1771 ICSlotCache* cache) override {
1772 property_feedback_slot_ = slot;
1774 Code::Kind FeedbackICSlotKind(int index) override {
1775 return key()->IsPropertyName() ? Code::LOAD_IC : Code::KEYED_LOAD_IC;
1778 FeedbackVectorICSlot PropertyFeedbackSlot() const {
1779 DCHECK(!property_feedback_slot_.IsInvalid());
1780 return property_feedback_slot_;
1783 static LhsKind GetAssignType(Property* property) {
1784 if (property == NULL) return VARIABLE;
1785 bool super_access = property->IsSuperAccess();
1786 return (property->key()->IsPropertyName())
1787 ? (super_access ? NAMED_SUPER_PROPERTY : NAMED_PROPERTY)
1788 : (super_access ? KEYED_SUPER_PROPERTY : KEYED_PROPERTY);
1792 Property(Zone* zone, Expression* obj, Expression* key, int pos)
1793 : Expression(zone, pos),
1794 bit_field_(IsForCallField::encode(false) |
1795 IsStringAccessField::encode(false) |
1796 InlineCacheStateField::encode(UNINITIALIZED)),
1797 property_feedback_slot_(FeedbackVectorICSlot::Invalid()),
1800 static int parent_num_ids() { return Expression::num_ids(); }
1803 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
1805 class IsForCallField : public BitField8<bool, 0, 1> {};
1806 class IsStringAccessField : public BitField8<bool, 1, 1> {};
1807 class KeyTypeField : public BitField8<IcCheckType, 2, 1> {};
1808 class InlineCacheStateField : public BitField8<InlineCacheState, 3, 4> {};
1810 FeedbackVectorICSlot property_feedback_slot_;
1813 SmallMapList receiver_types_;
1817 class Call final : public Expression {
1819 DECLARE_NODE_TYPE(Call)
1821 Expression* expression() const { return expression_; }
1822 ZoneList<Expression*>* arguments() const { return arguments_; }
1824 // Type feedback information.
1825 virtual FeedbackVectorRequirements ComputeFeedbackRequirements(
1826 Isolate* isolate, const ICSlotCache* cache) override;
1827 void SetFirstFeedbackICSlot(FeedbackVectorICSlot slot,
1828 ICSlotCache* cache) override {
1831 void SetFirstFeedbackSlot(FeedbackVectorSlot slot) override { slot_ = slot; }
1832 Code::Kind FeedbackICSlotKind(int index) override { return Code::CALL_IC; }
1834 FeedbackVectorSlot CallFeedbackSlot() const { return slot_; }
1836 FeedbackVectorICSlot CallFeedbackICSlot() const { return ic_slot_; }
1838 SmallMapList* GetReceiverTypes() override {
1839 if (expression()->IsProperty()) {
1840 return expression()->AsProperty()->GetReceiverTypes();
1845 bool IsMonomorphic() override {
1846 if (expression()->IsProperty()) {
1847 return expression()->AsProperty()->IsMonomorphic();
1849 return !target_.is_null();
1852 bool global_call() const {
1853 VariableProxy* proxy = expression_->AsVariableProxy();
1854 return proxy != NULL && proxy->var()->IsUnallocated();
1857 bool known_global_function() const {
1858 return global_call() && !target_.is_null();
1861 Handle<JSFunction> target() { return target_; }
1863 Handle<AllocationSite> allocation_site() { return allocation_site_; }
1865 void SetKnownGlobalTarget(Handle<JSFunction> target) {
1867 set_is_uninitialized(false);
1869 void set_target(Handle<JSFunction> target) { target_ = target; }
1870 void set_allocation_site(Handle<AllocationSite> site) {
1871 allocation_site_ = site;
1874 static int num_ids() { return parent_num_ids() + 2; }
1875 BailoutId ReturnId() const { return BailoutId(local_id(0)); }
1876 BailoutId EvalOrLookupId() const { return BailoutId(local_id(1)); }
1878 bool is_uninitialized() const {
1879 return IsUninitializedField::decode(bit_field_);
1881 void set_is_uninitialized(bool b) {
1882 bit_field_ = IsUninitializedField::update(bit_field_, b);
1894 // Helpers to determine how to handle the call.
1895 CallType GetCallType(Isolate* isolate) const;
1896 bool IsUsingCallFeedbackSlot(Isolate* isolate) const;
1897 bool IsUsingCallFeedbackICSlot(Isolate* isolate) const;
1900 // Used to assert that the FullCodeGenerator records the return site.
1901 bool return_is_recorded_;
1905 Call(Zone* zone, Expression* expression, ZoneList<Expression*>* arguments,
1907 : Expression(zone, pos),
1908 ic_slot_(FeedbackVectorICSlot::Invalid()),
1909 slot_(FeedbackVectorSlot::Invalid()),
1910 expression_(expression),
1911 arguments_(arguments),
1912 bit_field_(IsUninitializedField::encode(false)) {
1913 if (expression->IsProperty()) {
1914 expression->AsProperty()->mark_for_call();
1917 static int parent_num_ids() { return Expression::num_ids(); }
1920 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
1922 FeedbackVectorICSlot ic_slot_;
1923 FeedbackVectorSlot slot_;
1924 Expression* expression_;
1925 ZoneList<Expression*>* arguments_;
1926 Handle<JSFunction> target_;
1927 Handle<AllocationSite> allocation_site_;
1928 class IsUninitializedField : public BitField8<bool, 0, 1> {};
1933 class CallNew final : public Expression {
1935 DECLARE_NODE_TYPE(CallNew)
1937 Expression* expression() const { return expression_; }
1938 ZoneList<Expression*>* arguments() const { return arguments_; }
1940 // Type feedback information.
1941 virtual FeedbackVectorRequirements ComputeFeedbackRequirements(
1942 Isolate* isolate, const ICSlotCache* cache) override {
1943 return FeedbackVectorRequirements(FLAG_pretenuring_call_new ? 2 : 1, 0);
1945 void SetFirstFeedbackSlot(FeedbackVectorSlot slot) override {
1946 callnew_feedback_slot_ = slot;
1949 FeedbackVectorSlot CallNewFeedbackSlot() {
1950 DCHECK(!callnew_feedback_slot_.IsInvalid());
1951 return callnew_feedback_slot_;
1953 FeedbackVectorSlot AllocationSiteFeedbackSlot() {
1954 DCHECK(FLAG_pretenuring_call_new);
1955 return CallNewFeedbackSlot().next();
1958 bool IsMonomorphic() override { return is_monomorphic_; }
1959 Handle<JSFunction> target() const { return target_; }
1960 Handle<AllocationSite> allocation_site() const {
1961 return allocation_site_;
1964 static int num_ids() { return parent_num_ids() + 1; }
1965 static int feedback_slots() { return 1; }
1966 BailoutId ReturnId() const { return BailoutId(local_id(0)); }
1968 void set_allocation_site(Handle<AllocationSite> site) {
1969 allocation_site_ = site;
1971 void set_is_monomorphic(bool monomorphic) { is_monomorphic_ = monomorphic; }
1972 void set_target(Handle<JSFunction> target) { target_ = target; }
1973 void SetKnownGlobalTarget(Handle<JSFunction> target) {
1975 is_monomorphic_ = true;
1979 CallNew(Zone* zone, Expression* expression, ZoneList<Expression*>* arguments,
1981 : Expression(zone, pos),
1982 expression_(expression),
1983 arguments_(arguments),
1984 is_monomorphic_(false),
1985 callnew_feedback_slot_(FeedbackVectorSlot::Invalid()) {}
1987 static int parent_num_ids() { return Expression::num_ids(); }
1990 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
1992 Expression* expression_;
1993 ZoneList<Expression*>* arguments_;
1994 bool is_monomorphic_;
1995 Handle<JSFunction> target_;
1996 Handle<AllocationSite> allocation_site_;
1997 FeedbackVectorSlot callnew_feedback_slot_;
2001 // The CallRuntime class does not represent any official JavaScript
2002 // language construct. Instead it is used to call a C or JS function
2003 // with a set of arguments. This is used from the builtins that are
2004 // implemented in JavaScript (see "v8natives.js").
2005 class CallRuntime final : public Expression {
2007 DECLARE_NODE_TYPE(CallRuntime)
2009 Handle<String> name() const { return raw_name_->string(); }
2010 const AstRawString* raw_name() const { return raw_name_; }
2011 const Runtime::Function* function() const { return function_; }
2012 ZoneList<Expression*>* arguments() const { return arguments_; }
2013 bool is_jsruntime() const { return function_ == NULL; }
2015 // Type feedback information.
2016 bool HasCallRuntimeFeedbackSlot() const { return is_jsruntime(); }
2017 virtual FeedbackVectorRequirements ComputeFeedbackRequirements(
2018 Isolate* isolate, const ICSlotCache* cache) override {
2019 return FeedbackVectorRequirements(0, HasCallRuntimeFeedbackSlot() ? 1 : 0);
2021 void SetFirstFeedbackICSlot(FeedbackVectorICSlot slot,
2022 ICSlotCache* cache) override {
2023 callruntime_feedback_slot_ = slot;
2025 Code::Kind FeedbackICSlotKind(int index) override { return Code::LOAD_IC; }
2027 FeedbackVectorICSlot CallRuntimeFeedbackSlot() {
2028 DCHECK(!HasCallRuntimeFeedbackSlot() ||
2029 !callruntime_feedback_slot_.IsInvalid());
2030 return callruntime_feedback_slot_;
2033 static int num_ids() { return parent_num_ids(); }
2036 CallRuntime(Zone* zone, const AstRawString* name,
2037 const Runtime::Function* function,
2038 ZoneList<Expression*>* arguments, int pos)
2039 : Expression(zone, pos),
2041 function_(function),
2042 arguments_(arguments),
2043 callruntime_feedback_slot_(FeedbackVectorICSlot::Invalid()) {}
2044 static int parent_num_ids() { return Expression::num_ids(); }
2047 const AstRawString* raw_name_;
2048 const Runtime::Function* function_;
2049 ZoneList<Expression*>* arguments_;
2050 FeedbackVectorICSlot callruntime_feedback_slot_;
2054 class UnaryOperation final : public Expression {
2056 DECLARE_NODE_TYPE(UnaryOperation)
2058 Token::Value op() const { return op_; }
2059 Expression* expression() const { return expression_; }
2061 // For unary not (Token::NOT), the AST ids where true and false will
2062 // actually be materialized, respectively.
2063 static int num_ids() { return parent_num_ids() + 2; }
2064 BailoutId MaterializeTrueId() const { return BailoutId(local_id(0)); }
2065 BailoutId MaterializeFalseId() const { return BailoutId(local_id(1)); }
2067 virtual void RecordToBooleanTypeFeedback(TypeFeedbackOracle* oracle) override;
2070 UnaryOperation(Zone* zone, Token::Value op, Expression* expression, int pos)
2071 : Expression(zone, pos), op_(op), expression_(expression) {
2072 DCHECK(Token::IsUnaryOp(op));
2074 static int parent_num_ids() { return Expression::num_ids(); }
2077 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
2080 Expression* expression_;
2084 class BinaryOperation final : public Expression {
2086 DECLARE_NODE_TYPE(BinaryOperation)
2088 Token::Value op() const { return static_cast<Token::Value>(op_); }
2089 Expression* left() const { return left_; }
2090 Expression* right() const { return right_; }
2091 Handle<AllocationSite> allocation_site() const { return allocation_site_; }
2092 void set_allocation_site(Handle<AllocationSite> allocation_site) {
2093 allocation_site_ = allocation_site;
2096 // The short-circuit logical operations need an AST ID for their
2097 // right-hand subexpression.
2098 static int num_ids() { return parent_num_ids() + 2; }
2099 BailoutId RightId() const { return BailoutId(local_id(0)); }
2101 TypeFeedbackId BinaryOperationFeedbackId() const {
2102 return TypeFeedbackId(local_id(1));
2104 Maybe<int> fixed_right_arg() const {
2105 return has_fixed_right_arg_ ? Just(fixed_right_arg_value_) : Nothing<int>();
2107 void set_fixed_right_arg(Maybe<int> arg) {
2108 has_fixed_right_arg_ = arg.IsJust();
2109 if (arg.IsJust()) fixed_right_arg_value_ = arg.FromJust();
2112 virtual void RecordToBooleanTypeFeedback(TypeFeedbackOracle* oracle) override;
2115 BinaryOperation(Zone* zone, Token::Value op, Expression* left,
2116 Expression* right, int pos)
2117 : Expression(zone, pos),
2118 op_(static_cast<byte>(op)),
2119 has_fixed_right_arg_(false),
2120 fixed_right_arg_value_(0),
2123 DCHECK(Token::IsBinaryOp(op));
2125 static int parent_num_ids() { return Expression::num_ids(); }
2128 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
2130 const byte op_; // actually Token::Value
2131 // TODO(rossberg): the fixed arg should probably be represented as a Constant
2132 // type for the RHS. Currenty it's actually a Maybe<int>
2133 bool has_fixed_right_arg_;
2134 int fixed_right_arg_value_;
2137 Handle<AllocationSite> allocation_site_;
2141 class CountOperation final : public Expression {
2143 DECLARE_NODE_TYPE(CountOperation)
2145 bool is_prefix() const { return IsPrefixField::decode(bit_field_); }
2146 bool is_postfix() const { return !is_prefix(); }
2148 Token::Value op() const { return TokenField::decode(bit_field_); }
2149 Token::Value binary_op() {
2150 return (op() == Token::INC) ? Token::ADD : Token::SUB;
2153 Expression* expression() const { return expression_; }
2155 bool IsMonomorphic() override { return receiver_types_.length() == 1; }
2156 SmallMapList* GetReceiverTypes() override { return &receiver_types_; }
2157 IcCheckType GetKeyType() const override {
2158 return KeyTypeField::decode(bit_field_);
2160 KeyedAccessStoreMode GetStoreMode() const override {
2161 return StoreModeField::decode(bit_field_);
2163 Type* type() const { return type_; }
2164 void set_key_type(IcCheckType type) {
2165 bit_field_ = KeyTypeField::update(bit_field_, type);
2167 void set_store_mode(KeyedAccessStoreMode mode) {
2168 bit_field_ = StoreModeField::update(bit_field_, mode);
2170 void set_type(Type* type) { type_ = type; }
2172 static int num_ids() { return parent_num_ids() + 4; }
2173 BailoutId AssignmentId() const { return BailoutId(local_id(0)); }
2174 BailoutId ToNumberId() const { return BailoutId(local_id(1)); }
2175 TypeFeedbackId CountBinOpFeedbackId() const {
2176 return TypeFeedbackId(local_id(2));
2178 TypeFeedbackId CountStoreFeedbackId() const {
2179 return TypeFeedbackId(local_id(3));
2182 FeedbackVectorRequirements ComputeFeedbackRequirements(
2183 Isolate* isolate, const ICSlotCache* cache) override;
2184 void SetFirstFeedbackICSlot(FeedbackVectorICSlot slot,
2185 ICSlotCache* cache) override {
2188 Code::Kind FeedbackICSlotKind(int index) override;
2189 FeedbackVectorICSlot CountSlot() const { return slot_; }
2192 CountOperation(Zone* zone, Token::Value op, bool is_prefix, Expression* expr,
2194 : Expression(zone, pos),
2196 IsPrefixField::encode(is_prefix) | KeyTypeField::encode(ELEMENT) |
2197 StoreModeField::encode(STANDARD_STORE) | TokenField::encode(op)),
2200 slot_(FeedbackVectorICSlot::Invalid()) {}
2201 static int parent_num_ids() { return Expression::num_ids(); }
2204 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
2206 class IsPrefixField : public BitField16<bool, 0, 1> {};
2207 class KeyTypeField : public BitField16<IcCheckType, 1, 1> {};
2208 class StoreModeField : public BitField16<KeyedAccessStoreMode, 2, 4> {};
2209 class TokenField : public BitField16<Token::Value, 6, 8> {};
2211 // Starts with 16-bit field, which should get packed together with
2212 // Expression's trailing 16-bit field.
2213 uint16_t bit_field_;
2215 Expression* expression_;
2216 SmallMapList receiver_types_;
2217 FeedbackVectorICSlot slot_;
2221 class CompareOperation final : public Expression {
2223 DECLARE_NODE_TYPE(CompareOperation)
2225 Token::Value op() const { return op_; }
2226 Expression* left() const { return left_; }
2227 Expression* right() const { return right_; }
2229 // Type feedback information.
2230 static int num_ids() { return parent_num_ids() + 1; }
2231 TypeFeedbackId CompareOperationFeedbackId() const {
2232 return TypeFeedbackId(local_id(0));
2234 Type* combined_type() const { return combined_type_; }
2235 void set_combined_type(Type* type) { combined_type_ = type; }
2237 // Match special cases.
2238 bool IsLiteralCompareTypeof(Expression** expr, Handle<String>* check);
2239 bool IsLiteralCompareUndefined(Expression** expr, Isolate* isolate);
2240 bool IsLiteralCompareNull(Expression** expr);
2243 CompareOperation(Zone* zone, Token::Value op, Expression* left,
2244 Expression* right, int pos)
2245 : Expression(zone, pos),
2249 combined_type_(Type::None(zone)) {
2250 DCHECK(Token::IsCompareOp(op));
2252 static int parent_num_ids() { return Expression::num_ids(); }
2255 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
2261 Type* combined_type_;
2265 class Spread final : public Expression {
2267 DECLARE_NODE_TYPE(Spread)
2269 Expression* expression() const { return expression_; }
2271 static int num_ids() { return parent_num_ids(); }
2274 Spread(Zone* zone, Expression* expression, int pos)
2275 : Expression(zone, pos), expression_(expression) {}
2276 static int parent_num_ids() { return Expression::num_ids(); }
2279 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
2281 Expression* expression_;
2285 class Conditional final : public Expression {
2287 DECLARE_NODE_TYPE(Conditional)
2289 Expression* condition() const { return condition_; }
2290 Expression* then_expression() const { return then_expression_; }
2291 Expression* else_expression() const { return else_expression_; }
2293 static int num_ids() { return parent_num_ids() + 2; }
2294 BailoutId ThenId() const { return BailoutId(local_id(0)); }
2295 BailoutId ElseId() const { return BailoutId(local_id(1)); }
2298 Conditional(Zone* zone, Expression* condition, Expression* then_expression,
2299 Expression* else_expression, int position)
2300 : Expression(zone, position),
2301 condition_(condition),
2302 then_expression_(then_expression),
2303 else_expression_(else_expression) {}
2304 static int parent_num_ids() { return Expression::num_ids(); }
2307 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
2309 Expression* condition_;
2310 Expression* then_expression_;
2311 Expression* else_expression_;
2315 class Assignment final : public Expression {
2317 DECLARE_NODE_TYPE(Assignment)
2319 Assignment* AsSimpleAssignment() { return !is_compound() ? this : NULL; }
2321 Token::Value binary_op() const;
2323 Token::Value op() const { return TokenField::decode(bit_field_); }
2324 Expression* target() const { return target_; }
2325 Expression* value() const { return value_; }
2326 BinaryOperation* binary_operation() const { return binary_operation_; }
2328 // This check relies on the definition order of token in token.h.
2329 bool is_compound() const { return op() > Token::ASSIGN; }
2331 static int num_ids() { return parent_num_ids() + 2; }
2332 BailoutId AssignmentId() const { return BailoutId(local_id(0)); }
2334 // Type feedback information.
2335 TypeFeedbackId AssignmentFeedbackId() { return TypeFeedbackId(local_id(1)); }
2336 bool IsMonomorphic() override { return receiver_types_.length() == 1; }
2337 bool IsUninitialized() const {
2338 return IsUninitializedField::decode(bit_field_);
2340 bool HasNoTypeInformation() {
2341 return IsUninitializedField::decode(bit_field_);
2343 SmallMapList* GetReceiverTypes() override { return &receiver_types_; }
2344 IcCheckType GetKeyType() const override {
2345 return KeyTypeField::decode(bit_field_);
2347 KeyedAccessStoreMode GetStoreMode() const override {
2348 return StoreModeField::decode(bit_field_);
2350 void set_is_uninitialized(bool b) {
2351 bit_field_ = IsUninitializedField::update(bit_field_, b);
2353 void set_key_type(IcCheckType key_type) {
2354 bit_field_ = KeyTypeField::update(bit_field_, key_type);
2356 void set_store_mode(KeyedAccessStoreMode mode) {
2357 bit_field_ = StoreModeField::update(bit_field_, mode);
2360 FeedbackVectorRequirements ComputeFeedbackRequirements(
2361 Isolate* isolate, const ICSlotCache* cache) override;
2362 void SetFirstFeedbackICSlot(FeedbackVectorICSlot slot,
2363 ICSlotCache* cache) override {
2366 Code::Kind FeedbackICSlotKind(int index) override;
2367 FeedbackVectorICSlot AssignmentSlot() const { return slot_; }
2370 Assignment(Zone* zone, Token::Value op, Expression* target, Expression* value,
2372 static int parent_num_ids() { return Expression::num_ids(); }
2375 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
2377 class IsUninitializedField : public BitField16<bool, 0, 1> {};
2378 class KeyTypeField : public BitField16<IcCheckType, 1, 1> {};
2379 class StoreModeField : public BitField16<KeyedAccessStoreMode, 2, 4> {};
2380 class TokenField : public BitField16<Token::Value, 6, 8> {};
2382 // Starts with 16-bit field, which should get packed together with
2383 // Expression's trailing 16-bit field.
2384 uint16_t bit_field_;
2385 Expression* target_;
2387 BinaryOperation* binary_operation_;
2388 SmallMapList receiver_types_;
2389 FeedbackVectorICSlot slot_;
2393 class Yield final : public Expression {
2395 DECLARE_NODE_TYPE(Yield)
2398 kInitial, // The initial yield that returns the unboxed generator object.
2399 kSuspend, // A normal yield: { value: EXPRESSION, done: false }
2400 kDelegating, // A yield*.
2401 kFinal // A return: { value: EXPRESSION, done: true }
2404 Expression* generator_object() const { return generator_object_; }
2405 Expression* expression() const { return expression_; }
2406 Kind yield_kind() const { return yield_kind_; }
2408 // Delegating yield surrounds the "yield" in a "try/catch". This index
2409 // locates the catch handler in the handler table, and is equivalent to
2410 // TryCatchStatement::index().
2412 DCHECK_EQ(kDelegating, yield_kind());
2415 void set_index(int index) {
2416 DCHECK_EQ(kDelegating, yield_kind());
2420 // Type feedback information.
2421 bool HasFeedbackSlots() const { return yield_kind() == kDelegating; }
2422 virtual FeedbackVectorRequirements ComputeFeedbackRequirements(
2423 Isolate* isolate, const ICSlotCache* cache) override {
2424 return FeedbackVectorRequirements(0, HasFeedbackSlots() ? 3 : 0);
2426 void SetFirstFeedbackICSlot(FeedbackVectorICSlot slot,
2427 ICSlotCache* cache) override {
2428 yield_first_feedback_slot_ = slot;
2430 Code::Kind FeedbackICSlotKind(int index) override {
2431 return index == 0 ? Code::KEYED_LOAD_IC : Code::LOAD_IC;
2434 FeedbackVectorICSlot KeyedLoadFeedbackSlot() {
2435 DCHECK(!HasFeedbackSlots() || !yield_first_feedback_slot_.IsInvalid());
2436 return yield_first_feedback_slot_;
2439 FeedbackVectorICSlot DoneFeedbackSlot() {
2440 return KeyedLoadFeedbackSlot().next();
2443 FeedbackVectorICSlot ValueFeedbackSlot() { return DoneFeedbackSlot().next(); }
2446 Yield(Zone* zone, Expression* generator_object, Expression* expression,
2447 Kind yield_kind, int pos)
2448 : Expression(zone, pos),
2449 generator_object_(generator_object),
2450 expression_(expression),
2451 yield_kind_(yield_kind),
2453 yield_first_feedback_slot_(FeedbackVectorICSlot::Invalid()) {}
2456 Expression* generator_object_;
2457 Expression* expression_;
2460 FeedbackVectorICSlot yield_first_feedback_slot_;
2464 class Throw final : public Expression {
2466 DECLARE_NODE_TYPE(Throw)
2468 Expression* exception() const { return exception_; }
2471 Throw(Zone* zone, Expression* exception, int pos)
2472 : Expression(zone, pos), exception_(exception) {}
2475 Expression* exception_;
2479 class FunctionLiteral final : public Expression {
2482 ANONYMOUS_EXPRESSION,
2487 enum ParameterFlag {
2488 kNoDuplicateParameters = 0,
2489 kHasDuplicateParameters = 1
2492 enum IsFunctionFlag {
2497 enum EagerCompileHint { kShouldEagerCompile, kShouldLazyCompile };
2499 enum ShouldBeUsedOnceHint { kShouldBeUsedOnce, kDontKnowIfShouldBeUsedOnce };
2501 enum ArityRestriction {
2507 DECLARE_NODE_TYPE(FunctionLiteral)
2509 Handle<String> name() const { return raw_name_->string(); }
2510 const AstRawString* raw_name() const { return raw_name_; }
2511 Scope* scope() const { return scope_; }
2512 ZoneList<Statement*>* body() const { return body_; }
2513 void set_function_token_position(int pos) { function_token_position_ = pos; }
2514 int function_token_position() const { return function_token_position_; }
2515 int start_position() const;
2516 int end_position() const;
2517 int SourceSize() const { return end_position() - start_position(); }
2518 bool is_expression() const { return IsExpression::decode(bitfield_); }
2519 bool is_anonymous() const { return IsAnonymous::decode(bitfield_); }
2520 LanguageMode language_mode() const;
2522 static bool NeedsHomeObject(Expression* expr);
2524 int materialized_literal_count() { return materialized_literal_count_; }
2525 int expected_property_count() { return expected_property_count_; }
2526 int handler_count() { return handler_count_; }
2527 int parameter_count() { return parameter_count_; }
2529 bool AllowsLazyCompilation();
2530 bool AllowsLazyCompilationWithoutContext();
2532 void InitializeSharedInfo(Handle<Code> code);
2534 Handle<String> debug_name() const {
2535 if (raw_name_ != NULL && !raw_name_->IsEmpty()) {
2536 return raw_name_->string();
2538 return inferred_name();
2541 Handle<String> inferred_name() const {
2542 if (!inferred_name_.is_null()) {
2543 DCHECK(raw_inferred_name_ == NULL);
2544 return inferred_name_;
2546 if (raw_inferred_name_ != NULL) {
2547 return raw_inferred_name_->string();
2550 return Handle<String>();
2553 // Only one of {set_inferred_name, set_raw_inferred_name} should be called.
2554 void set_inferred_name(Handle<String> inferred_name) {
2555 DCHECK(!inferred_name.is_null());
2556 inferred_name_ = inferred_name;
2557 DCHECK(raw_inferred_name_== NULL || raw_inferred_name_->IsEmpty());
2558 raw_inferred_name_ = NULL;
2561 void set_raw_inferred_name(const AstString* raw_inferred_name) {
2562 DCHECK(raw_inferred_name != NULL);
2563 raw_inferred_name_ = raw_inferred_name;
2564 DCHECK(inferred_name_.is_null());
2565 inferred_name_ = Handle<String>();
2568 // shared_info may be null if it's not cached in full code.
2569 Handle<SharedFunctionInfo> shared_info() { return shared_info_; }
2571 bool pretenure() { return Pretenure::decode(bitfield_); }
2572 void set_pretenure() { bitfield_ |= Pretenure::encode(true); }
2574 bool has_duplicate_parameters() {
2575 return HasDuplicateParameters::decode(bitfield_);
2578 bool is_function() { return IsFunction::decode(bitfield_) == kIsFunction; }
2580 // This is used as a heuristic on when to eagerly compile a function
2581 // literal. We consider the following constructs as hints that the
2582 // function will be called immediately:
2583 // - (function() { ... })();
2584 // - var x = function() { ... }();
2585 bool should_eager_compile() const {
2586 return EagerCompileHintBit::decode(bitfield_) == kShouldEagerCompile;
2588 void set_should_eager_compile() {
2589 bitfield_ = EagerCompileHintBit::update(bitfield_, kShouldEagerCompile);
2592 // A hint that we expect this function to be called (exactly) once,
2593 // i.e. we suspect it's an initialization function.
2594 bool should_be_used_once_hint() const {
2595 return ShouldBeUsedOnceHintBit::decode(bitfield_) == kShouldBeUsedOnce;
2597 void set_should_be_used_once_hint() {
2598 bitfield_ = ShouldBeUsedOnceHintBit::update(bitfield_, kShouldBeUsedOnce);
2601 FunctionKind kind() const { return FunctionKindBits::decode(bitfield_); }
2603 int ast_node_count() { return ast_properties_.node_count(); }
2604 AstProperties::Flags* flags() { return ast_properties_.flags(); }
2605 void set_ast_properties(AstProperties* ast_properties) {
2606 ast_properties_ = *ast_properties;
2608 const ZoneFeedbackVectorSpec* feedback_vector_spec() const {
2609 return ast_properties_.get_spec();
2611 bool dont_optimize() { return dont_optimize_reason_ != kNoReason; }
2612 BailoutReason dont_optimize_reason() { return dont_optimize_reason_; }
2613 void set_dont_optimize_reason(BailoutReason reason) {
2614 dont_optimize_reason_ = reason;
2618 FunctionLiteral(Zone* zone, const AstRawString* name,
2619 AstValueFactory* ast_value_factory, Scope* scope,
2620 ZoneList<Statement*>* body, int materialized_literal_count,
2621 int expected_property_count, int handler_count,
2622 int parameter_count, FunctionType function_type,
2623 ParameterFlag has_duplicate_parameters,
2624 IsFunctionFlag is_function,
2625 EagerCompileHint eager_compile_hint, FunctionKind kind,
2627 : Expression(zone, position),
2631 raw_inferred_name_(ast_value_factory->empty_string()),
2632 ast_properties_(zone),
2633 dont_optimize_reason_(kNoReason),
2634 materialized_literal_count_(materialized_literal_count),
2635 expected_property_count_(expected_property_count),
2636 handler_count_(handler_count),
2637 parameter_count_(parameter_count),
2638 function_token_position_(RelocInfo::kNoPosition) {
2639 bitfield_ = IsExpression::encode(function_type != DECLARATION) |
2640 IsAnonymous::encode(function_type == ANONYMOUS_EXPRESSION) |
2641 Pretenure::encode(false) |
2642 HasDuplicateParameters::encode(has_duplicate_parameters) |
2643 IsFunction::encode(is_function) |
2644 EagerCompileHintBit::encode(eager_compile_hint) |
2645 FunctionKindBits::encode(kind) |
2646 ShouldBeUsedOnceHintBit::encode(kDontKnowIfShouldBeUsedOnce);
2647 DCHECK(IsValidFunctionKind(kind));
2651 const AstRawString* raw_name_;
2652 Handle<String> name_;
2653 Handle<SharedFunctionInfo> shared_info_;
2655 ZoneList<Statement*>* body_;
2656 const AstString* raw_inferred_name_;
2657 Handle<String> inferred_name_;
2658 AstProperties ast_properties_;
2659 BailoutReason dont_optimize_reason_;
2661 int materialized_literal_count_;
2662 int expected_property_count_;
2664 int parameter_count_;
2665 int function_token_position_;
2668 class IsExpression : public BitField<bool, 0, 1> {};
2669 class IsAnonymous : public BitField<bool, 1, 1> {};
2670 class Pretenure : public BitField<bool, 2, 1> {};
2671 class HasDuplicateParameters : public BitField<ParameterFlag, 3, 1> {};
2672 class IsFunction : public BitField<IsFunctionFlag, 4, 1> {};
2673 class EagerCompileHintBit : public BitField<EagerCompileHint, 5, 1> {};
2674 class FunctionKindBits : public BitField<FunctionKind, 6, 8> {};
2675 class ShouldBeUsedOnceHintBit : public BitField<ShouldBeUsedOnceHint, 15, 1> {
2680 class ClassLiteral final : public Expression {
2682 typedef ObjectLiteralProperty Property;
2684 DECLARE_NODE_TYPE(ClassLiteral)
2686 Handle<String> name() const { return raw_name_->string(); }
2687 const AstRawString* raw_name() const { return raw_name_; }
2688 Scope* scope() const { return scope_; }
2689 VariableProxy* class_variable_proxy() const { return class_variable_proxy_; }
2690 Expression* extends() const { return extends_; }
2691 FunctionLiteral* constructor() const { return constructor_; }
2692 ZoneList<Property*>* properties() const { return properties_; }
2693 int start_position() const { return position(); }
2694 int end_position() const { return end_position_; }
2696 BailoutId EntryId() const { return BailoutId(local_id(0)); }
2697 BailoutId DeclsId() const { return BailoutId(local_id(1)); }
2698 BailoutId ExitId() { return BailoutId(local_id(2)); }
2699 BailoutId CreateLiteralId() const { return BailoutId(local_id(3)); }
2701 // Return an AST id for a property that is used in simulate instructions.
2702 BailoutId GetIdForProperty(int i) { return BailoutId(local_id(i + 4)); }
2704 // Unlike other AST nodes, this number of bailout IDs allocated for an
2705 // ClassLiteral can vary, so num_ids() is not a static method.
2706 int num_ids() const { return parent_num_ids() + 4 + properties()->length(); }
2709 ClassLiteral(Zone* zone, const AstRawString* name, Scope* scope,
2710 VariableProxy* class_variable_proxy, Expression* extends,
2711 FunctionLiteral* constructor, ZoneList<Property*>* properties,
2712 int start_position, int end_position)
2713 : Expression(zone, start_position),
2716 class_variable_proxy_(class_variable_proxy),
2718 constructor_(constructor),
2719 properties_(properties),
2720 end_position_(end_position) {}
2721 static int parent_num_ids() { return Expression::num_ids(); }
2724 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
2726 const AstRawString* raw_name_;
2728 VariableProxy* class_variable_proxy_;
2729 Expression* extends_;
2730 FunctionLiteral* constructor_;
2731 ZoneList<Property*>* properties_;
2736 class NativeFunctionLiteral final : public Expression {
2738 DECLARE_NODE_TYPE(NativeFunctionLiteral)
2740 Handle<String> name() const { return name_->string(); }
2741 v8::Extension* extension() const { return extension_; }
2744 NativeFunctionLiteral(Zone* zone, const AstRawString* name,
2745 v8::Extension* extension, int pos)
2746 : Expression(zone, pos), name_(name), extension_(extension) {}
2749 const AstRawString* name_;
2750 v8::Extension* extension_;
2754 class ThisFunction final : public Expression {
2756 DECLARE_NODE_TYPE(ThisFunction)
2759 ThisFunction(Zone* zone, int pos) : Expression(zone, pos) {}
2763 class SuperPropertyReference final : public Expression {
2765 DECLARE_NODE_TYPE(SuperPropertyReference)
2767 VariableProxy* this_var() const { return this_var_; }
2768 Expression* home_object() const { return home_object_; }
2771 SuperPropertyReference(Zone* zone, VariableProxy* this_var,
2772 Expression* home_object, int pos)
2773 : Expression(zone, pos), this_var_(this_var), home_object_(home_object) {
2774 DCHECK(this_var->is_this());
2775 DCHECK(home_object->IsProperty());
2779 VariableProxy* this_var_;
2780 Expression* home_object_;
2784 class SuperCallReference final : public Expression {
2786 DECLARE_NODE_TYPE(SuperCallReference)
2788 VariableProxy* this_var() const { return this_var_; }
2789 VariableProxy* new_target_var() const { return new_target_var_; }
2790 VariableProxy* this_function_var() const { return this_function_var_; }
2793 SuperCallReference(Zone* zone, VariableProxy* this_var,
2794 VariableProxy* new_target_var,
2795 VariableProxy* this_function_var, int pos)
2796 : Expression(zone, pos),
2797 this_var_(this_var),
2798 new_target_var_(new_target_var),
2799 this_function_var_(this_function_var) {
2800 DCHECK(this_var->is_this());
2801 DCHECK(new_target_var->raw_name()->IsOneByteEqualTo("new.target"));
2802 DCHECK(this_function_var->raw_name()->IsOneByteEqualTo(".this_function"));
2806 VariableProxy* this_var_;
2807 VariableProxy* new_target_var_;
2808 VariableProxy* this_function_var_;
2812 #undef DECLARE_NODE_TYPE
2815 // ----------------------------------------------------------------------------
2816 // Regular expressions
2819 class RegExpVisitor BASE_EMBEDDED {
2821 virtual ~RegExpVisitor() { }
2822 #define MAKE_CASE(Name) \
2823 virtual void* Visit##Name(RegExp##Name*, void* data) = 0;
2824 FOR_EACH_REG_EXP_TREE_TYPE(MAKE_CASE)
2829 class RegExpTree : public ZoneObject {
2831 static const int kInfinity = kMaxInt;
2832 virtual ~RegExpTree() {}
2833 virtual void* Accept(RegExpVisitor* visitor, void* data) = 0;
2834 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
2835 RegExpNode* on_success) = 0;
2836 virtual bool IsTextElement() { return false; }
2837 virtual bool IsAnchoredAtStart() { return false; }
2838 virtual bool IsAnchoredAtEnd() { return false; }
2839 virtual int min_match() = 0;
2840 virtual int max_match() = 0;
2841 // Returns the interval of registers used for captures within this
2843 virtual Interval CaptureRegisters() { return Interval::Empty(); }
2844 virtual void AppendToText(RegExpText* text, Zone* zone);
2845 std::ostream& Print(std::ostream& os, Zone* zone); // NOLINT
2846 #define MAKE_ASTYPE(Name) \
2847 virtual RegExp##Name* As##Name(); \
2848 virtual bool Is##Name();
2849 FOR_EACH_REG_EXP_TREE_TYPE(MAKE_ASTYPE)
2854 class RegExpDisjunction final : public RegExpTree {
2856 explicit RegExpDisjunction(ZoneList<RegExpTree*>* alternatives);
2857 void* Accept(RegExpVisitor* visitor, void* data) override;
2858 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
2859 RegExpNode* on_success) override;
2860 RegExpDisjunction* AsDisjunction() override;
2861 Interval CaptureRegisters() override;
2862 bool IsDisjunction() override;
2863 bool IsAnchoredAtStart() override;
2864 bool IsAnchoredAtEnd() override;
2865 int min_match() override { return min_match_; }
2866 int max_match() override { return max_match_; }
2867 ZoneList<RegExpTree*>* alternatives() { return alternatives_; }
2869 ZoneList<RegExpTree*>* alternatives_;
2875 class RegExpAlternative final : public RegExpTree {
2877 explicit RegExpAlternative(ZoneList<RegExpTree*>* nodes);
2878 void* Accept(RegExpVisitor* visitor, void* data) override;
2879 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
2880 RegExpNode* on_success) override;
2881 RegExpAlternative* AsAlternative() override;
2882 Interval CaptureRegisters() override;
2883 bool IsAlternative() override;
2884 bool IsAnchoredAtStart() override;
2885 bool IsAnchoredAtEnd() override;
2886 int min_match() override { return min_match_; }
2887 int max_match() override { return max_match_; }
2888 ZoneList<RegExpTree*>* nodes() { return nodes_; }
2890 ZoneList<RegExpTree*>* nodes_;
2896 class RegExpAssertion final : public RegExpTree {
2898 enum AssertionType {
2906 explicit RegExpAssertion(AssertionType type) : assertion_type_(type) { }
2907 void* Accept(RegExpVisitor* visitor, void* data) override;
2908 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
2909 RegExpNode* on_success) override;
2910 RegExpAssertion* AsAssertion() override;
2911 bool IsAssertion() override;
2912 bool IsAnchoredAtStart() override;
2913 bool IsAnchoredAtEnd() override;
2914 int min_match() override { return 0; }
2915 int max_match() override { return 0; }
2916 AssertionType assertion_type() { return assertion_type_; }
2918 AssertionType assertion_type_;
2922 class CharacterSet final BASE_EMBEDDED {
2924 explicit CharacterSet(uc16 standard_set_type)
2926 standard_set_type_(standard_set_type) {}
2927 explicit CharacterSet(ZoneList<CharacterRange>* ranges)
2929 standard_set_type_(0) {}
2930 ZoneList<CharacterRange>* ranges(Zone* zone);
2931 uc16 standard_set_type() { return standard_set_type_; }
2932 void set_standard_set_type(uc16 special_set_type) {
2933 standard_set_type_ = special_set_type;
2935 bool is_standard() { return standard_set_type_ != 0; }
2936 void Canonicalize();
2938 ZoneList<CharacterRange>* ranges_;
2939 // If non-zero, the value represents a standard set (e.g., all whitespace
2940 // characters) without having to expand the ranges.
2941 uc16 standard_set_type_;
2945 class RegExpCharacterClass final : public RegExpTree {
2947 RegExpCharacterClass(ZoneList<CharacterRange>* ranges, bool is_negated)
2949 is_negated_(is_negated) { }
2950 explicit RegExpCharacterClass(uc16 type)
2952 is_negated_(false) { }
2953 void* Accept(RegExpVisitor* visitor, void* data) override;
2954 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
2955 RegExpNode* on_success) override;
2956 RegExpCharacterClass* AsCharacterClass() override;
2957 bool IsCharacterClass() override;
2958 bool IsTextElement() override { return true; }
2959 int min_match() override { return 1; }
2960 int max_match() override { return 1; }
2961 void AppendToText(RegExpText* text, Zone* zone) override;
2962 CharacterSet character_set() { return set_; }
2963 // TODO(lrn): Remove need for complex version if is_standard that
2964 // recognizes a mangled standard set and just do { return set_.is_special(); }
2965 bool is_standard(Zone* zone);
2966 // Returns a value representing the standard character set if is_standard()
2968 // Currently used values are:
2969 // s : unicode whitespace
2970 // S : unicode non-whitespace
2971 // w : ASCII word character (digit, letter, underscore)
2972 // W : non-ASCII word character
2974 // D : non-ASCII digit
2975 // . : non-unicode non-newline
2976 // * : All characters
2977 uc16 standard_type() { return set_.standard_set_type(); }
2978 ZoneList<CharacterRange>* ranges(Zone* zone) { return set_.ranges(zone); }
2979 bool is_negated() { return is_negated_; }
2987 class RegExpAtom final : public RegExpTree {
2989 explicit RegExpAtom(Vector<const uc16> data) : data_(data) { }
2990 void* Accept(RegExpVisitor* visitor, void* data) override;
2991 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
2992 RegExpNode* on_success) override;
2993 RegExpAtom* AsAtom() override;
2994 bool IsAtom() override;
2995 bool IsTextElement() override { return true; }
2996 int min_match() override { return data_.length(); }
2997 int max_match() override { return data_.length(); }
2998 void AppendToText(RegExpText* text, Zone* zone) override;
2999 Vector<const uc16> data() { return data_; }
3000 int length() { return data_.length(); }
3002 Vector<const uc16> data_;
3006 class RegExpText final : public RegExpTree {
3008 explicit RegExpText(Zone* zone) : elements_(2, zone), length_(0) {}
3009 void* Accept(RegExpVisitor* visitor, void* data) override;
3010 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
3011 RegExpNode* on_success) override;
3012 RegExpText* AsText() override;
3013 bool IsText() override;
3014 bool IsTextElement() override { return true; }
3015 int min_match() override { return length_; }
3016 int max_match() override { return length_; }
3017 void AppendToText(RegExpText* text, Zone* zone) override;
3018 void AddElement(TextElement elm, Zone* zone) {
3019 elements_.Add(elm, zone);
3020 length_ += elm.length();
3022 ZoneList<TextElement>* elements() { return &elements_; }
3024 ZoneList<TextElement> elements_;
3029 class RegExpQuantifier final : public RegExpTree {
3031 enum QuantifierType { GREEDY, NON_GREEDY, POSSESSIVE };
3032 RegExpQuantifier(int min, int max, QuantifierType type, RegExpTree* body)
3036 min_match_(min * body->min_match()),
3037 quantifier_type_(type) {
3038 if (max > 0 && body->max_match() > kInfinity / max) {
3039 max_match_ = kInfinity;
3041 max_match_ = max * body->max_match();
3044 void* Accept(RegExpVisitor* visitor, void* data) override;
3045 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
3046 RegExpNode* on_success) override;
3047 static RegExpNode* ToNode(int min,
3051 RegExpCompiler* compiler,
3052 RegExpNode* on_success,
3053 bool not_at_start = false);
3054 RegExpQuantifier* AsQuantifier() override;
3055 Interval CaptureRegisters() override;
3056 bool IsQuantifier() override;
3057 int min_match() override { return min_match_; }
3058 int max_match() override { return max_match_; }
3059 int min() { return min_; }
3060 int max() { return max_; }
3061 bool is_possessive() { return quantifier_type_ == POSSESSIVE; }
3062 bool is_non_greedy() { return quantifier_type_ == NON_GREEDY; }
3063 bool is_greedy() { return quantifier_type_ == GREEDY; }
3064 RegExpTree* body() { return body_; }
3072 QuantifierType quantifier_type_;
3076 class RegExpCapture final : public RegExpTree {
3078 explicit RegExpCapture(RegExpTree* body, int index)
3079 : body_(body), index_(index) { }
3080 void* Accept(RegExpVisitor* visitor, void* data) override;
3081 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
3082 RegExpNode* on_success) override;
3083 static RegExpNode* ToNode(RegExpTree* body,
3085 RegExpCompiler* compiler,
3086 RegExpNode* on_success);
3087 RegExpCapture* AsCapture() override;
3088 bool IsAnchoredAtStart() override;
3089 bool IsAnchoredAtEnd() override;
3090 Interval CaptureRegisters() override;
3091 bool IsCapture() override;
3092 int min_match() override { return body_->min_match(); }
3093 int max_match() override { return body_->max_match(); }
3094 RegExpTree* body() { return body_; }
3095 int index() { return index_; }
3096 static int StartRegister(int index) { return index * 2; }
3097 static int EndRegister(int index) { return index * 2 + 1; }
3105 class RegExpLookahead final : public RegExpTree {
3107 RegExpLookahead(RegExpTree* body,
3112 is_positive_(is_positive),
3113 capture_count_(capture_count),
3114 capture_from_(capture_from) { }
3116 void* Accept(RegExpVisitor* visitor, void* data) override;
3117 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
3118 RegExpNode* on_success) override;
3119 RegExpLookahead* AsLookahead() override;
3120 Interval CaptureRegisters() override;
3121 bool IsLookahead() override;
3122 bool IsAnchoredAtStart() override;
3123 int min_match() override { return 0; }
3124 int max_match() override { return 0; }
3125 RegExpTree* body() { return body_; }
3126 bool is_positive() { return is_positive_; }
3127 int capture_count() { return capture_count_; }
3128 int capture_from() { return capture_from_; }
3138 class RegExpBackReference final : public RegExpTree {
3140 explicit RegExpBackReference(RegExpCapture* capture)
3141 : capture_(capture) { }
3142 void* Accept(RegExpVisitor* visitor, void* data) override;
3143 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
3144 RegExpNode* on_success) override;
3145 RegExpBackReference* AsBackReference() override;
3146 bool IsBackReference() override;
3147 int min_match() override { return 0; }
3148 int max_match() override { return capture_->max_match(); }
3149 int index() { return capture_->index(); }
3150 RegExpCapture* capture() { return capture_; }
3152 RegExpCapture* capture_;
3156 class RegExpEmpty final : public RegExpTree {
3159 void* Accept(RegExpVisitor* visitor, void* data) override;
3160 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
3161 RegExpNode* on_success) override;
3162 RegExpEmpty* AsEmpty() override;
3163 bool IsEmpty() override;
3164 int min_match() override { return 0; }
3165 int max_match() override { return 0; }
3169 // ----------------------------------------------------------------------------
3171 // - leaf node visitors are abstract.
3173 class AstVisitor BASE_EMBEDDED {
3176 virtual ~AstVisitor() {}
3178 // Stack overflow check and dynamic dispatch.
3179 virtual void Visit(AstNode* node) = 0;
3181 // Iteration left-to-right.
3182 virtual void VisitDeclarations(ZoneList<Declaration*>* declarations);
3183 virtual void VisitStatements(ZoneList<Statement*>* statements);
3184 virtual void VisitExpressions(ZoneList<Expression*>* expressions);
3186 // Individual AST nodes.
3187 #define DEF_VISIT(type) \
3188 virtual void Visit##type(type* node) = 0;
3189 AST_NODE_LIST(DEF_VISIT)
3194 #define DEFINE_AST_VISITOR_SUBCLASS_MEMBERS() \
3196 void Visit(AstNode* node) final { \
3197 if (!CheckStackOverflow()) node->Accept(this); \
3200 void SetStackOverflow() { stack_overflow_ = true; } \
3201 void ClearStackOverflow() { stack_overflow_ = false; } \
3202 bool HasStackOverflow() const { return stack_overflow_; } \
3204 bool CheckStackOverflow() { \
3205 if (stack_overflow_) return true; \
3206 StackLimitCheck check(isolate_); \
3207 if (!check.HasOverflowed()) return false; \
3208 stack_overflow_ = true; \
3213 void InitializeAstVisitor(Isolate* isolate, Zone* zone) { \
3214 isolate_ = isolate; \
3216 stack_overflow_ = false; \
3218 Zone* zone() { return zone_; } \
3219 Isolate* isolate() { return isolate_; } \
3221 Isolate* isolate_; \
3223 bool stack_overflow_
3226 // ----------------------------------------------------------------------------
3229 class AstNodeFactory final BASE_EMBEDDED {
3231 explicit AstNodeFactory(AstValueFactory* ast_value_factory)
3232 : zone_(ast_value_factory->zone()),
3233 ast_value_factory_(ast_value_factory) {}
3235 VariableDeclaration* NewVariableDeclaration(
3236 VariableProxy* proxy, VariableMode mode, Scope* scope, int pos,
3237 bool is_class_declaration = false, int declaration_group_start = -1) {
3239 VariableDeclaration(zone_, proxy, mode, scope, pos,
3240 is_class_declaration, declaration_group_start);
3243 FunctionDeclaration* NewFunctionDeclaration(VariableProxy* proxy,
3245 FunctionLiteral* fun,
3248 return new (zone_) FunctionDeclaration(zone_, proxy, mode, fun, scope, pos);
3251 ImportDeclaration* NewImportDeclaration(VariableProxy* proxy,
3252 const AstRawString* import_name,
3253 const AstRawString* module_specifier,
3254 Scope* scope, int pos) {
3255 return new (zone_) ImportDeclaration(zone_, proxy, import_name,
3256 module_specifier, scope, pos);
3259 ExportDeclaration* NewExportDeclaration(VariableProxy* proxy,
3262 return new (zone_) ExportDeclaration(zone_, proxy, scope, pos);
3265 Block* NewBlock(ZoneList<const AstRawString*>* labels,
3267 bool is_initializer_block,
3270 Block(zone_, labels, capacity, is_initializer_block, pos);
3273 #define STATEMENT_WITH_LABELS(NodeType) \
3274 NodeType* New##NodeType(ZoneList<const AstRawString*>* labels, int pos) { \
3275 return new (zone_) NodeType(zone_, labels, pos); \
3277 STATEMENT_WITH_LABELS(DoWhileStatement)
3278 STATEMENT_WITH_LABELS(WhileStatement)
3279 STATEMENT_WITH_LABELS(ForStatement)
3280 STATEMENT_WITH_LABELS(SwitchStatement)
3281 #undef STATEMENT_WITH_LABELS
3283 ForEachStatement* NewForEachStatement(ForEachStatement::VisitMode visit_mode,
3284 ZoneList<const AstRawString*>* labels,
3286 switch (visit_mode) {
3287 case ForEachStatement::ENUMERATE: {
3288 return new (zone_) ForInStatement(zone_, labels, pos);
3290 case ForEachStatement::ITERATE: {
3291 return new (zone_) ForOfStatement(zone_, labels, pos);
3298 ExpressionStatement* NewExpressionStatement(Expression* expression, int pos) {
3299 return new (zone_) ExpressionStatement(zone_, expression, pos);
3302 ContinueStatement* NewContinueStatement(IterationStatement* target, int pos) {
3303 return new (zone_) ContinueStatement(zone_, target, pos);
3306 BreakStatement* NewBreakStatement(BreakableStatement* target, int pos) {
3307 return new (zone_) BreakStatement(zone_, target, pos);
3310 ReturnStatement* NewReturnStatement(Expression* expression, int pos) {
3311 return new (zone_) ReturnStatement(zone_, expression, pos);
3314 WithStatement* NewWithStatement(Scope* scope,
3315 Expression* expression,
3316 Statement* statement,
3318 return new (zone_) WithStatement(zone_, scope, expression, statement, pos);
3321 IfStatement* NewIfStatement(Expression* condition,
3322 Statement* then_statement,
3323 Statement* else_statement,
3326 IfStatement(zone_, condition, then_statement, else_statement, pos);
3329 TryCatchStatement* NewTryCatchStatement(int index,
3335 return new (zone_) TryCatchStatement(zone_, index, try_block, scope,
3336 variable, catch_block, pos);
3339 TryFinallyStatement* NewTryFinallyStatement(int index,
3341 Block* finally_block,
3344 TryFinallyStatement(zone_, index, try_block, finally_block, pos);
3347 DebuggerStatement* NewDebuggerStatement(int pos) {
3348 return new (zone_) DebuggerStatement(zone_, pos);
3351 EmptyStatement* NewEmptyStatement(int pos) {
3352 return new(zone_) EmptyStatement(zone_, pos);
3355 CaseClause* NewCaseClause(
3356 Expression* label, ZoneList<Statement*>* statements, int pos) {
3357 return new (zone_) CaseClause(zone_, label, statements, pos);
3360 Literal* NewStringLiteral(const AstRawString* string, int pos) {
3362 Literal(zone_, ast_value_factory_->NewString(string), pos);
3365 // A JavaScript symbol (ECMA-262 edition 6).
3366 Literal* NewSymbolLiteral(const char* name, int pos) {
3367 return new (zone_) Literal(zone_, ast_value_factory_->NewSymbol(name), pos);
3370 Literal* NewNumberLiteral(double number, int pos) {
3372 Literal(zone_, ast_value_factory_->NewNumber(number), pos);
3375 Literal* NewSmiLiteral(int number, int pos) {
3376 return new (zone_) Literal(zone_, ast_value_factory_->NewSmi(number), pos);
3379 Literal* NewBooleanLiteral(bool b, int pos) {
3380 return new (zone_) Literal(zone_, ast_value_factory_->NewBoolean(b), pos);
3383 Literal* NewNullLiteral(int pos) {
3384 return new (zone_) Literal(zone_, ast_value_factory_->NewNull(), pos);
3387 Literal* NewUndefinedLiteral(int pos) {
3388 return new (zone_) Literal(zone_, ast_value_factory_->NewUndefined(), pos);
3391 Literal* NewTheHoleLiteral(int pos) {
3392 return new (zone_) Literal(zone_, ast_value_factory_->NewTheHole(), pos);
3395 ObjectLiteral* NewObjectLiteral(
3396 ZoneList<ObjectLiteral::Property*>* properties,
3398 int boilerplate_properties,
3402 return new (zone_) ObjectLiteral(zone_, properties, literal_index,
3403 boilerplate_properties, has_function,
3407 ObjectLiteral::Property* NewObjectLiteralProperty(
3408 Expression* key, Expression* value, ObjectLiteralProperty::Kind kind,
3409 bool is_static, bool is_computed_name) {
3411 ObjectLiteral::Property(key, value, kind, is_static, is_computed_name);
3414 ObjectLiteral::Property* NewObjectLiteralProperty(Expression* key,
3417 bool is_computed_name) {
3418 return new (zone_) ObjectLiteral::Property(ast_value_factory_, key, value,
3419 is_static, is_computed_name);
3422 RegExpLiteral* NewRegExpLiteral(const AstRawString* pattern,
3423 const AstRawString* flags,
3427 return new (zone_) RegExpLiteral(zone_, pattern, flags, literal_index,
3431 ArrayLiteral* NewArrayLiteral(ZoneList<Expression*>* values,
3435 return new (zone_) ArrayLiteral(zone_, values, literal_index, is_strong,
3439 VariableProxy* NewVariableProxy(Variable* var,
3440 int start_position = RelocInfo::kNoPosition,
3441 int end_position = RelocInfo::kNoPosition) {
3442 return new (zone_) VariableProxy(zone_, var, start_position, end_position);
3445 VariableProxy* NewVariableProxy(const AstRawString* name,
3446 Variable::Kind variable_kind,
3447 int start_position = RelocInfo::kNoPosition,
3448 int end_position = RelocInfo::kNoPosition) {
3449 DCHECK_NOT_NULL(name);
3451 VariableProxy(zone_, name, variable_kind, start_position, end_position);
3454 Property* NewProperty(Expression* obj, Expression* key, int pos) {
3455 return new (zone_) Property(zone_, obj, key, pos);
3458 Call* NewCall(Expression* expression,
3459 ZoneList<Expression*>* arguments,
3461 return new (zone_) Call(zone_, expression, arguments, pos);
3464 CallNew* NewCallNew(Expression* expression,
3465 ZoneList<Expression*>* arguments,
3467 return new (zone_) CallNew(zone_, expression, arguments, pos);
3470 CallRuntime* NewCallRuntime(const AstRawString* name,
3471 const Runtime::Function* function,
3472 ZoneList<Expression*>* arguments,
3474 return new (zone_) CallRuntime(zone_, name, function, arguments, pos);
3477 UnaryOperation* NewUnaryOperation(Token::Value op,
3478 Expression* expression,
3480 return new (zone_) UnaryOperation(zone_, op, expression, pos);
3483 BinaryOperation* NewBinaryOperation(Token::Value op,
3487 return new (zone_) BinaryOperation(zone_, op, left, right, pos);
3490 CountOperation* NewCountOperation(Token::Value op,
3494 return new (zone_) CountOperation(zone_, op, is_prefix, expr, pos);
3497 CompareOperation* NewCompareOperation(Token::Value op,
3501 return new (zone_) CompareOperation(zone_, op, left, right, pos);
3504 Spread* NewSpread(Expression* expression, int pos) {
3505 return new (zone_) Spread(zone_, expression, pos);
3508 Conditional* NewConditional(Expression* condition,
3509 Expression* then_expression,
3510 Expression* else_expression,
3512 return new (zone_) Conditional(zone_, condition, then_expression,
3513 else_expression, position);
3516 Assignment* NewAssignment(Token::Value op,
3520 DCHECK(Token::IsAssignmentOp(op));
3521 Assignment* assign = new (zone_) Assignment(zone_, op, target, value, pos);
3522 if (assign->is_compound()) {
3523 DCHECK(Token::IsAssignmentOp(op));
3524 assign->binary_operation_ =
3525 NewBinaryOperation(assign->binary_op(), target, value, pos + 1);
3530 Yield* NewYield(Expression *generator_object,
3531 Expression* expression,
3532 Yield::Kind yield_kind,
3534 if (!expression) expression = NewUndefinedLiteral(pos);
3536 Yield(zone_, generator_object, expression, yield_kind, pos);
3539 Throw* NewThrow(Expression* exception, int pos) {
3540 return new (zone_) Throw(zone_, exception, pos);
3543 FunctionLiteral* NewFunctionLiteral(
3544 const AstRawString* name, AstValueFactory* ast_value_factory,
3545 Scope* scope, ZoneList<Statement*>* body, int materialized_literal_count,
3546 int expected_property_count, int handler_count, int parameter_count,
3547 FunctionLiteral::ParameterFlag has_duplicate_parameters,
3548 FunctionLiteral::FunctionType function_type,
3549 FunctionLiteral::IsFunctionFlag is_function,
3550 FunctionLiteral::EagerCompileHint eager_compile_hint, FunctionKind kind,
3552 return new (zone_) FunctionLiteral(
3553 zone_, name, ast_value_factory, scope, body, materialized_literal_count,
3554 expected_property_count, handler_count, parameter_count, function_type,
3555 has_duplicate_parameters, is_function, eager_compile_hint, kind,
3559 ClassLiteral* NewClassLiteral(const AstRawString* name, Scope* scope,
3560 VariableProxy* proxy, Expression* extends,
3561 FunctionLiteral* constructor,
3562 ZoneList<ObjectLiteral::Property*>* properties,
3563 int start_position, int end_position) {
3565 ClassLiteral(zone_, name, scope, proxy, extends, constructor,
3566 properties, start_position, end_position);
3569 NativeFunctionLiteral* NewNativeFunctionLiteral(const AstRawString* name,
3570 v8::Extension* extension,
3572 return new (zone_) NativeFunctionLiteral(zone_, name, extension, pos);
3575 ThisFunction* NewThisFunction(int pos) {
3576 return new (zone_) ThisFunction(zone_, pos);
3579 SuperPropertyReference* NewSuperPropertyReference(VariableProxy* this_var,
3580 Expression* home_object,
3583 SuperPropertyReference(zone_, this_var, home_object, pos);
3586 SuperCallReference* NewSuperCallReference(VariableProxy* this_var,
3587 VariableProxy* new_target_var,
3588 VariableProxy* this_function_var,
3590 return new (zone_) SuperCallReference(zone_, this_var, new_target_var,
3591 this_function_var, pos);
3596 AstValueFactory* ast_value_factory_;
3600 } } // namespace v8::internal