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 Block* try_block() const { return try_block_; }
1167 TryStatement(Zone* zone, Block* try_block, int pos)
1168 : Statement(zone, pos), try_block_(try_block) {}
1175 class TryCatchStatement final : public TryStatement {
1177 DECLARE_NODE_TYPE(TryCatchStatement)
1179 Scope* scope() { return scope_; }
1180 Variable* variable() { return variable_; }
1181 Block* catch_block() const { return catch_block_; }
1184 TryCatchStatement(Zone* zone, Block* try_block, Scope* scope,
1185 Variable* variable, Block* catch_block, int pos)
1186 : TryStatement(zone, try_block, pos),
1188 variable_(variable),
1189 catch_block_(catch_block) {}
1193 Variable* variable_;
1194 Block* catch_block_;
1198 class TryFinallyStatement final : public TryStatement {
1200 DECLARE_NODE_TYPE(TryFinallyStatement)
1202 Block* finally_block() const { return finally_block_; }
1205 TryFinallyStatement(Zone* zone, Block* try_block, Block* finally_block,
1207 : TryStatement(zone, try_block, pos), finally_block_(finally_block) {}
1210 Block* finally_block_;
1214 class DebuggerStatement final : public Statement {
1216 DECLARE_NODE_TYPE(DebuggerStatement)
1218 void set_base_id(int id) { base_id_ = id; }
1219 static int num_ids() { return parent_num_ids() + 1; }
1220 BailoutId DebugBreakId() const { return BailoutId(local_id(0)); }
1223 explicit DebuggerStatement(Zone* zone, int pos)
1224 : Statement(zone, pos), base_id_(BailoutId::None().ToInt()) {}
1225 static int parent_num_ids() { return 0; }
1227 int base_id() const {
1228 DCHECK(!BailoutId(base_id_).IsNone());
1233 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
1239 class EmptyStatement final : public Statement {
1241 DECLARE_NODE_TYPE(EmptyStatement)
1244 explicit EmptyStatement(Zone* zone, int pos): Statement(zone, pos) {}
1248 class Literal final : public Expression {
1250 DECLARE_NODE_TYPE(Literal)
1252 bool IsPropertyName() const override { return value_->IsPropertyName(); }
1254 Handle<String> AsPropertyName() {
1255 DCHECK(IsPropertyName());
1256 return Handle<String>::cast(value());
1259 const AstRawString* AsRawPropertyName() {
1260 DCHECK(IsPropertyName());
1261 return value_->AsString();
1264 bool ToBooleanIsTrue() const override { return value()->BooleanValue(); }
1265 bool ToBooleanIsFalse() const override { return !value()->BooleanValue(); }
1267 Handle<Object> value() const { return value_->value(); }
1268 const AstValue* raw_value() const { return value_; }
1270 // Support for using Literal as a HashMap key. NOTE: Currently, this works
1271 // only for string and number literals!
1273 static bool Match(void* literal1, void* literal2);
1275 static int num_ids() { return parent_num_ids() + 1; }
1276 TypeFeedbackId LiteralFeedbackId() const {
1277 return TypeFeedbackId(local_id(0));
1281 Literal(Zone* zone, const AstValue* value, int position)
1282 : Expression(zone, position), value_(value) {}
1283 static int parent_num_ids() { return Expression::num_ids(); }
1286 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
1288 const AstValue* value_;
1292 // Base class for literals that needs space in the corresponding JSFunction.
1293 class MaterializedLiteral : public Expression {
1295 virtual MaterializedLiteral* AsMaterializedLiteral() { return this; }
1297 int literal_index() { return literal_index_; }
1300 // only callable after initialization.
1301 DCHECK(depth_ >= 1);
1305 bool is_strong() const { return is_strong_; }
1308 MaterializedLiteral(Zone* zone, int literal_index, bool is_strong, int pos)
1309 : Expression(zone, pos),
1310 literal_index_(literal_index),
1312 is_strong_(is_strong),
1315 // A materialized literal is simple if the values consist of only
1316 // constants and simple object and array literals.
1317 bool is_simple() const { return is_simple_; }
1318 void set_is_simple(bool is_simple) { is_simple_ = is_simple; }
1319 friend class CompileTimeValue;
1321 void set_depth(int depth) {
1326 // Populate the constant properties/elements fixed array.
1327 void BuildConstants(Isolate* isolate);
1328 friend class ArrayLiteral;
1329 friend class ObjectLiteral;
1331 // If the expression is a literal, return the literal value;
1332 // if the expression is a materialized literal and is simple return a
1333 // compile time value as encoded by CompileTimeValue::GetValue().
1334 // Otherwise, return undefined literal as the placeholder
1335 // in the object literal boilerplate.
1336 Handle<Object> GetBoilerplateValue(Expression* expression, Isolate* isolate);
1346 // Property is used for passing information
1347 // about an object literal's properties from the parser
1348 // to the code generator.
1349 class ObjectLiteralProperty final : public ZoneObject {
1352 CONSTANT, // Property with constant value (compile time).
1353 COMPUTED, // Property with computed value (execution time).
1354 MATERIALIZED_LITERAL, // Property value is a materialized literal.
1355 GETTER, SETTER, // Property is an accessor function.
1356 PROTOTYPE // Property is __proto__.
1359 Expression* key() { return key_; }
1360 Expression* value() { return value_; }
1361 Kind kind() { return kind_; }
1363 // Type feedback information.
1364 bool IsMonomorphic() { return !receiver_type_.is_null(); }
1365 Handle<Map> GetReceiverType() { return receiver_type_; }
1367 bool IsCompileTimeValue();
1369 void set_emit_store(bool emit_store);
1372 bool is_static() const { return is_static_; }
1373 bool is_computed_name() const { return is_computed_name_; }
1375 void set_receiver_type(Handle<Map> map) { receiver_type_ = map; }
1378 friend class AstNodeFactory;
1380 ObjectLiteralProperty(Expression* key, Expression* value, Kind kind,
1381 bool is_static, bool is_computed_name);
1382 ObjectLiteralProperty(AstValueFactory* ast_value_factory, Expression* key,
1383 Expression* value, bool is_static,
1384 bool is_computed_name);
1392 bool is_computed_name_;
1393 Handle<Map> receiver_type_;
1397 // An object literal has a boilerplate object that is used
1398 // for minimizing the work when constructing it at runtime.
1399 class ObjectLiteral final : public MaterializedLiteral {
1401 typedef ObjectLiteralProperty Property;
1403 DECLARE_NODE_TYPE(ObjectLiteral)
1405 Handle<FixedArray> constant_properties() const {
1406 return constant_properties_;
1408 int properties_count() const { return constant_properties_->length() / 2; }
1409 ZoneList<Property*>* properties() const { return properties_; }
1410 bool fast_elements() const { return fast_elements_; }
1411 bool may_store_doubles() const { return may_store_doubles_; }
1412 bool has_function() const { return has_function_; }
1413 bool has_elements() const { return has_elements_; }
1415 // Decide if a property should be in the object boilerplate.
1416 static bool IsBoilerplateProperty(Property* property);
1418 // Populate the constant properties fixed array.
1419 void BuildConstantProperties(Isolate* isolate);
1421 // Mark all computed expressions that are bound to a key that
1422 // is shadowed by a later occurrence of the same key. For the
1423 // marked expressions, no store code is emitted.
1424 void CalculateEmitStore(Zone* zone);
1426 // Assemble bitfield of flags for the CreateObjectLiteral helper.
1427 int ComputeFlags(bool disable_mementos = false) const {
1428 int flags = fast_elements() ? kFastElements : kNoFlags;
1429 flags |= has_function() ? kHasFunction : kNoFlags;
1430 if (depth() == 1 && !has_elements() && !may_store_doubles()) {
1431 flags |= kShallowProperties;
1433 if (disable_mementos) {
1434 flags |= kDisableMementos;
1445 kHasFunction = 1 << 1,
1446 kShallowProperties = 1 << 2,
1447 kDisableMementos = 1 << 3,
1451 struct Accessors: public ZoneObject {
1452 Accessors() : getter(NULL), setter(NULL) {}
1457 BailoutId CreateLiteralId() const { return BailoutId(local_id(0)); }
1459 // Return an AST id for a property that is used in simulate instructions.
1460 BailoutId GetIdForProperty(int i) { return BailoutId(local_id(i + 1)); }
1462 // Unlike other AST nodes, this number of bailout IDs allocated for an
1463 // ObjectLiteral can vary, so num_ids() is not a static method.
1464 int num_ids() const { return parent_num_ids() + 1 + properties()->length(); }
1466 // Object literals need one feedback slot for each non-trivial value, as well
1467 // as some slots for home objects.
1468 FeedbackVectorRequirements ComputeFeedbackRequirements(
1469 Isolate* isolate, const ICSlotCache* cache) override;
1470 void SetFirstFeedbackICSlot(FeedbackVectorICSlot slot,
1471 ICSlotCache* cache) override {
1474 Code::Kind FeedbackICSlotKind(int index) override { return Code::STORE_IC; }
1475 FeedbackVectorICSlot GetNthSlot(int n) const {
1476 return FeedbackVectorICSlot(slot_.ToInt() + n);
1479 // If value needs a home object, returns a valid feedback vector ic slot
1480 // given by slot_index, and increments slot_index.
1481 FeedbackVectorICSlot SlotForHomeObject(Expression* value,
1482 int* slot_index) const;
1485 int slot_count() const { return slot_count_; }
1489 ObjectLiteral(Zone* zone, ZoneList<Property*>* properties, int literal_index,
1490 int boilerplate_properties, bool has_function, bool is_strong,
1492 : MaterializedLiteral(zone, literal_index, is_strong, pos),
1493 properties_(properties),
1494 boilerplate_properties_(boilerplate_properties),
1495 fast_elements_(false),
1496 has_elements_(false),
1497 may_store_doubles_(false),
1498 has_function_(has_function),
1502 slot_(FeedbackVectorICSlot::Invalid()) {
1504 static int parent_num_ids() { return MaterializedLiteral::num_ids(); }
1507 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
1508 Handle<FixedArray> constant_properties_;
1509 ZoneList<Property*>* properties_;
1510 int boilerplate_properties_;
1511 bool fast_elements_;
1513 bool may_store_doubles_;
1516 // slot_count_ helps validate that the logic to allocate ic slots and the
1517 // logic to use them are in sync.
1520 FeedbackVectorICSlot slot_;
1524 // Node for capturing a regexp literal.
1525 class RegExpLiteral final : public MaterializedLiteral {
1527 DECLARE_NODE_TYPE(RegExpLiteral)
1529 Handle<String> pattern() const { return pattern_->string(); }
1530 Handle<String> flags() const { return flags_->string(); }
1533 RegExpLiteral(Zone* zone, const AstRawString* pattern,
1534 const AstRawString* flags, int literal_index, bool is_strong,
1536 : MaterializedLiteral(zone, literal_index, is_strong, pos),
1543 const AstRawString* pattern_;
1544 const AstRawString* flags_;
1548 // An array literal has a literals object that is used
1549 // for minimizing the work when constructing it at runtime.
1550 class ArrayLiteral final : public MaterializedLiteral {
1552 DECLARE_NODE_TYPE(ArrayLiteral)
1554 Handle<FixedArray> constant_elements() const { return constant_elements_; }
1555 ElementsKind constant_elements_kind() const {
1556 DCHECK_EQ(2, constant_elements_->length());
1557 return static_cast<ElementsKind>(
1558 Smi::cast(constant_elements_->get(0))->value());
1561 ZoneList<Expression*>* values() const { return values_; }
1563 BailoutId CreateLiteralId() const { return BailoutId(local_id(0)); }
1565 // Return an AST id for an element that is used in simulate instructions.
1566 BailoutId GetIdForElement(int i) { return BailoutId(local_id(i + 1)); }
1568 // Unlike other AST nodes, this number of bailout IDs allocated for an
1569 // ArrayLiteral can vary, so num_ids() is not a static method.
1570 int num_ids() const { return parent_num_ids() + 1 + values()->length(); }
1572 // Populate the constant elements fixed array.
1573 void BuildConstantElements(Isolate* isolate);
1575 // Assemble bitfield of flags for the CreateArrayLiteral helper.
1576 int ComputeFlags(bool disable_mementos = false) const {
1577 int flags = depth() == 1 ? kShallowElements : kNoFlags;
1578 if (disable_mementos) {
1579 flags |= kDisableMementos;
1589 kShallowElements = 1,
1590 kDisableMementos = 1 << 1,
1595 ArrayLiteral(Zone* zone, ZoneList<Expression*>* values, int literal_index,
1596 bool is_strong, int pos)
1597 : MaterializedLiteral(zone, literal_index, is_strong, pos),
1599 static int parent_num_ids() { return MaterializedLiteral::num_ids(); }
1602 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
1604 Handle<FixedArray> constant_elements_;
1605 ZoneList<Expression*>* values_;
1609 class VariableProxy final : public Expression {
1611 DECLARE_NODE_TYPE(VariableProxy)
1613 bool IsValidReferenceExpression() const override { return !is_this(); }
1615 bool IsArguments() const { return is_resolved() && var()->is_arguments(); }
1617 Handle<String> name() const { return raw_name()->string(); }
1618 const AstRawString* raw_name() const {
1619 return is_resolved() ? var_->raw_name() : raw_name_;
1622 Variable* var() const {
1623 DCHECK(is_resolved());
1626 void set_var(Variable* v) {
1627 DCHECK(!is_resolved());
1632 bool is_this() const { return IsThisField::decode(bit_field_); }
1634 bool is_assigned() const { return IsAssignedField::decode(bit_field_); }
1635 void set_is_assigned() {
1636 bit_field_ = IsAssignedField::update(bit_field_, true);
1639 bool is_resolved() const { return IsResolvedField::decode(bit_field_); }
1640 void set_is_resolved() {
1641 bit_field_ = IsResolvedField::update(bit_field_, true);
1644 int end_position() const { return end_position_; }
1646 // Bind this proxy to the variable var.
1647 void BindTo(Variable* var);
1649 bool UsesVariableFeedbackSlot() const {
1650 return var()->IsUnallocated() || var()->IsLookupSlot();
1653 virtual FeedbackVectorRequirements ComputeFeedbackRequirements(
1654 Isolate* isolate, const ICSlotCache* cache) override;
1656 void SetFirstFeedbackICSlot(FeedbackVectorICSlot slot,
1657 ICSlotCache* cache) override;
1658 Code::Kind FeedbackICSlotKind(int index) override { return Code::LOAD_IC; }
1659 FeedbackVectorICSlot VariableFeedbackSlot() {
1660 DCHECK(!UsesVariableFeedbackSlot() || !variable_feedback_slot_.IsInvalid());
1661 return variable_feedback_slot_;
1664 static int num_ids() { return parent_num_ids() + 1; }
1665 BailoutId BeforeId() const { return BailoutId(local_id(0)); }
1668 VariableProxy(Zone* zone, Variable* var, int start_position,
1671 VariableProxy(Zone* zone, const AstRawString* name,
1672 Variable::Kind variable_kind, int start_position,
1674 static int parent_num_ids() { return Expression::num_ids(); }
1675 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
1677 class IsThisField : public BitField8<bool, 0, 1> {};
1678 class IsAssignedField : public BitField8<bool, 1, 1> {};
1679 class IsResolvedField : public BitField8<bool, 2, 1> {};
1681 // Start with 16-bit (or smaller) field, which should get packed together
1682 // with Expression's trailing 16-bit field.
1684 FeedbackVectorICSlot variable_feedback_slot_;
1686 const AstRawString* raw_name_; // if !is_resolved_
1687 Variable* var_; // if is_resolved_
1689 // Position is stored in the AstNode superclass, but VariableProxy needs to
1690 // know its end position too (for error messages). It cannot be inferred from
1691 // the variable name length because it can contain escapes.
1696 // Left-hand side can only be a property, a global or a (parameter or local)
1702 NAMED_SUPER_PROPERTY,
1703 KEYED_SUPER_PROPERTY
1707 class Property final : public Expression {
1709 DECLARE_NODE_TYPE(Property)
1711 bool IsValidReferenceExpression() const override { return true; }
1713 Expression* obj() const { return obj_; }
1714 Expression* key() const { return key_; }
1716 static int num_ids() { return parent_num_ids() + 1; }
1717 BailoutId LoadId() const { return BailoutId(local_id(0)); }
1719 bool IsStringAccess() const {
1720 return IsStringAccessField::decode(bit_field_);
1723 // Type feedback information.
1724 bool IsMonomorphic() override { return receiver_types_.length() == 1; }
1725 SmallMapList* GetReceiverTypes() override { return &receiver_types_; }
1726 KeyedAccessStoreMode GetStoreMode() const override { return STANDARD_STORE; }
1727 IcCheckType GetKeyType() const override {
1728 return KeyTypeField::decode(bit_field_);
1730 bool IsUninitialized() const {
1731 return !is_for_call() && HasNoTypeInformation();
1733 bool HasNoTypeInformation() const {
1734 return GetInlineCacheState() == UNINITIALIZED;
1736 InlineCacheState GetInlineCacheState() const {
1737 return InlineCacheStateField::decode(bit_field_);
1739 void set_is_string_access(bool b) {
1740 bit_field_ = IsStringAccessField::update(bit_field_, b);
1742 void set_key_type(IcCheckType key_type) {
1743 bit_field_ = KeyTypeField::update(bit_field_, key_type);
1745 void set_inline_cache_state(InlineCacheState state) {
1746 bit_field_ = InlineCacheStateField::update(bit_field_, state);
1748 void mark_for_call() {
1749 bit_field_ = IsForCallField::update(bit_field_, true);
1751 bool is_for_call() const { return IsForCallField::decode(bit_field_); }
1753 bool IsSuperAccess() { return obj()->IsSuperPropertyReference(); }
1755 virtual FeedbackVectorRequirements ComputeFeedbackRequirements(
1756 Isolate* isolate, const ICSlotCache* cache) override {
1757 return FeedbackVectorRequirements(0, 1);
1759 void SetFirstFeedbackICSlot(FeedbackVectorICSlot slot,
1760 ICSlotCache* cache) override {
1761 property_feedback_slot_ = slot;
1763 Code::Kind FeedbackICSlotKind(int index) override {
1764 return key()->IsPropertyName() ? Code::LOAD_IC : Code::KEYED_LOAD_IC;
1767 FeedbackVectorICSlot PropertyFeedbackSlot() const {
1768 DCHECK(!property_feedback_slot_.IsInvalid());
1769 return property_feedback_slot_;
1772 static LhsKind GetAssignType(Property* property) {
1773 if (property == NULL) return VARIABLE;
1774 bool super_access = property->IsSuperAccess();
1775 return (property->key()->IsPropertyName())
1776 ? (super_access ? NAMED_SUPER_PROPERTY : NAMED_PROPERTY)
1777 : (super_access ? KEYED_SUPER_PROPERTY : KEYED_PROPERTY);
1781 Property(Zone* zone, Expression* obj, Expression* key, int pos)
1782 : Expression(zone, pos),
1783 bit_field_(IsForCallField::encode(false) |
1784 IsStringAccessField::encode(false) |
1785 InlineCacheStateField::encode(UNINITIALIZED)),
1786 property_feedback_slot_(FeedbackVectorICSlot::Invalid()),
1789 static int parent_num_ids() { return Expression::num_ids(); }
1792 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
1794 class IsForCallField : public BitField8<bool, 0, 1> {};
1795 class IsStringAccessField : public BitField8<bool, 1, 1> {};
1796 class KeyTypeField : public BitField8<IcCheckType, 2, 1> {};
1797 class InlineCacheStateField : public BitField8<InlineCacheState, 3, 4> {};
1799 FeedbackVectorICSlot property_feedback_slot_;
1802 SmallMapList receiver_types_;
1806 class Call final : public Expression {
1808 DECLARE_NODE_TYPE(Call)
1810 Expression* expression() const { return expression_; }
1811 ZoneList<Expression*>* arguments() const { return arguments_; }
1813 // Type feedback information.
1814 virtual FeedbackVectorRequirements ComputeFeedbackRequirements(
1815 Isolate* isolate, const ICSlotCache* cache) override;
1816 void SetFirstFeedbackICSlot(FeedbackVectorICSlot slot,
1817 ICSlotCache* cache) override {
1820 void SetFirstFeedbackSlot(FeedbackVectorSlot slot) override { slot_ = slot; }
1821 Code::Kind FeedbackICSlotKind(int index) override { return Code::CALL_IC; }
1823 FeedbackVectorSlot CallFeedbackSlot() const { return slot_; }
1825 FeedbackVectorICSlot CallFeedbackICSlot() const { return ic_slot_; }
1827 SmallMapList* GetReceiverTypes() override {
1828 if (expression()->IsProperty()) {
1829 return expression()->AsProperty()->GetReceiverTypes();
1834 bool IsMonomorphic() override {
1835 if (expression()->IsProperty()) {
1836 return expression()->AsProperty()->IsMonomorphic();
1838 return !target_.is_null();
1841 bool global_call() const {
1842 VariableProxy* proxy = expression_->AsVariableProxy();
1843 return proxy != NULL && proxy->var()->IsUnallocated();
1846 bool known_global_function() const {
1847 return global_call() && !target_.is_null();
1850 Handle<JSFunction> target() { return target_; }
1852 Handle<AllocationSite> allocation_site() { return allocation_site_; }
1854 void SetKnownGlobalTarget(Handle<JSFunction> target) {
1856 set_is_uninitialized(false);
1858 void set_target(Handle<JSFunction> target) { target_ = target; }
1859 void set_allocation_site(Handle<AllocationSite> site) {
1860 allocation_site_ = site;
1863 static int num_ids() { return parent_num_ids() + 2; }
1864 BailoutId ReturnId() const { return BailoutId(local_id(0)); }
1865 BailoutId EvalOrLookupId() const { return BailoutId(local_id(1)); }
1867 bool is_uninitialized() const {
1868 return IsUninitializedField::decode(bit_field_);
1870 void set_is_uninitialized(bool b) {
1871 bit_field_ = IsUninitializedField::update(bit_field_, b);
1883 // Helpers to determine how to handle the call.
1884 CallType GetCallType(Isolate* isolate) const;
1885 bool IsUsingCallFeedbackSlot(Isolate* isolate) const;
1886 bool IsUsingCallFeedbackICSlot(Isolate* isolate) const;
1889 // Used to assert that the FullCodeGenerator records the return site.
1890 bool return_is_recorded_;
1894 Call(Zone* zone, Expression* expression, ZoneList<Expression*>* arguments,
1896 : Expression(zone, pos),
1897 ic_slot_(FeedbackVectorICSlot::Invalid()),
1898 slot_(FeedbackVectorSlot::Invalid()),
1899 expression_(expression),
1900 arguments_(arguments),
1901 bit_field_(IsUninitializedField::encode(false)) {
1902 if (expression->IsProperty()) {
1903 expression->AsProperty()->mark_for_call();
1906 static int parent_num_ids() { return Expression::num_ids(); }
1909 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
1911 FeedbackVectorICSlot ic_slot_;
1912 FeedbackVectorSlot slot_;
1913 Expression* expression_;
1914 ZoneList<Expression*>* arguments_;
1915 Handle<JSFunction> target_;
1916 Handle<AllocationSite> allocation_site_;
1917 class IsUninitializedField : public BitField8<bool, 0, 1> {};
1922 class CallNew final : public Expression {
1924 DECLARE_NODE_TYPE(CallNew)
1926 Expression* expression() const { return expression_; }
1927 ZoneList<Expression*>* arguments() const { return arguments_; }
1929 // Type feedback information.
1930 virtual FeedbackVectorRequirements ComputeFeedbackRequirements(
1931 Isolate* isolate, const ICSlotCache* cache) override {
1932 return FeedbackVectorRequirements(FLAG_pretenuring_call_new ? 2 : 1, 0);
1934 void SetFirstFeedbackSlot(FeedbackVectorSlot slot) override {
1935 callnew_feedback_slot_ = slot;
1938 FeedbackVectorSlot CallNewFeedbackSlot() {
1939 DCHECK(!callnew_feedback_slot_.IsInvalid());
1940 return callnew_feedback_slot_;
1942 FeedbackVectorSlot AllocationSiteFeedbackSlot() {
1943 DCHECK(FLAG_pretenuring_call_new);
1944 return CallNewFeedbackSlot().next();
1947 bool IsMonomorphic() override { return is_monomorphic_; }
1948 Handle<JSFunction> target() const { return target_; }
1949 Handle<AllocationSite> allocation_site() const {
1950 return allocation_site_;
1953 static int num_ids() { return parent_num_ids() + 1; }
1954 static int feedback_slots() { return 1; }
1955 BailoutId ReturnId() const { return BailoutId(local_id(0)); }
1957 void set_allocation_site(Handle<AllocationSite> site) {
1958 allocation_site_ = site;
1960 void set_is_monomorphic(bool monomorphic) { is_monomorphic_ = monomorphic; }
1961 void set_target(Handle<JSFunction> target) { target_ = target; }
1962 void SetKnownGlobalTarget(Handle<JSFunction> target) {
1964 is_monomorphic_ = true;
1968 CallNew(Zone* zone, Expression* expression, ZoneList<Expression*>* arguments,
1970 : Expression(zone, pos),
1971 expression_(expression),
1972 arguments_(arguments),
1973 is_monomorphic_(false),
1974 callnew_feedback_slot_(FeedbackVectorSlot::Invalid()) {}
1976 static int parent_num_ids() { return Expression::num_ids(); }
1979 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
1981 Expression* expression_;
1982 ZoneList<Expression*>* arguments_;
1983 bool is_monomorphic_;
1984 Handle<JSFunction> target_;
1985 Handle<AllocationSite> allocation_site_;
1986 FeedbackVectorSlot callnew_feedback_slot_;
1990 // The CallRuntime class does not represent any official JavaScript
1991 // language construct. Instead it is used to call a C or JS function
1992 // with a set of arguments. This is used from the builtins that are
1993 // implemented in JavaScript (see "v8natives.js").
1994 class CallRuntime final : public Expression {
1996 DECLARE_NODE_TYPE(CallRuntime)
1998 Handle<String> name() const { return raw_name_->string(); }
1999 const AstRawString* raw_name() const { return raw_name_; }
2000 const Runtime::Function* function() const { return function_; }
2001 ZoneList<Expression*>* arguments() const { return arguments_; }
2002 bool is_jsruntime() const { return function_ == NULL; }
2004 // Type feedback information.
2005 bool HasCallRuntimeFeedbackSlot() const { return is_jsruntime(); }
2006 virtual FeedbackVectorRequirements ComputeFeedbackRequirements(
2007 Isolate* isolate, const ICSlotCache* cache) override {
2008 return FeedbackVectorRequirements(0, HasCallRuntimeFeedbackSlot() ? 1 : 0);
2010 void SetFirstFeedbackICSlot(FeedbackVectorICSlot slot,
2011 ICSlotCache* cache) override {
2012 callruntime_feedback_slot_ = slot;
2014 Code::Kind FeedbackICSlotKind(int index) override { return Code::LOAD_IC; }
2016 FeedbackVectorICSlot CallRuntimeFeedbackSlot() {
2017 DCHECK(!HasCallRuntimeFeedbackSlot() ||
2018 !callruntime_feedback_slot_.IsInvalid());
2019 return callruntime_feedback_slot_;
2022 static int num_ids() { return parent_num_ids(); }
2025 CallRuntime(Zone* zone, const AstRawString* name,
2026 const Runtime::Function* function,
2027 ZoneList<Expression*>* arguments, int pos)
2028 : Expression(zone, pos),
2030 function_(function),
2031 arguments_(arguments),
2032 callruntime_feedback_slot_(FeedbackVectorICSlot::Invalid()) {}
2033 static int parent_num_ids() { return Expression::num_ids(); }
2036 const AstRawString* raw_name_;
2037 const Runtime::Function* function_;
2038 ZoneList<Expression*>* arguments_;
2039 FeedbackVectorICSlot callruntime_feedback_slot_;
2043 class UnaryOperation final : public Expression {
2045 DECLARE_NODE_TYPE(UnaryOperation)
2047 Token::Value op() const { return op_; }
2048 Expression* expression() const { return expression_; }
2050 // For unary not (Token::NOT), the AST ids where true and false will
2051 // actually be materialized, respectively.
2052 static int num_ids() { return parent_num_ids() + 2; }
2053 BailoutId MaterializeTrueId() const { return BailoutId(local_id(0)); }
2054 BailoutId MaterializeFalseId() const { return BailoutId(local_id(1)); }
2056 virtual void RecordToBooleanTypeFeedback(TypeFeedbackOracle* oracle) override;
2059 UnaryOperation(Zone* zone, Token::Value op, Expression* expression, int pos)
2060 : Expression(zone, pos), op_(op), expression_(expression) {
2061 DCHECK(Token::IsUnaryOp(op));
2063 static int parent_num_ids() { return Expression::num_ids(); }
2066 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
2069 Expression* expression_;
2073 class BinaryOperation final : public Expression {
2075 DECLARE_NODE_TYPE(BinaryOperation)
2077 Token::Value op() const { return static_cast<Token::Value>(op_); }
2078 Expression* left() const { return left_; }
2079 Expression* right() const { return right_; }
2080 Handle<AllocationSite> allocation_site() const { return allocation_site_; }
2081 void set_allocation_site(Handle<AllocationSite> allocation_site) {
2082 allocation_site_ = allocation_site;
2085 // The short-circuit logical operations need an AST ID for their
2086 // right-hand subexpression.
2087 static int num_ids() { return parent_num_ids() + 2; }
2088 BailoutId RightId() const { return BailoutId(local_id(0)); }
2090 TypeFeedbackId BinaryOperationFeedbackId() const {
2091 return TypeFeedbackId(local_id(1));
2093 Maybe<int> fixed_right_arg() const {
2094 return has_fixed_right_arg_ ? Just(fixed_right_arg_value_) : Nothing<int>();
2096 void set_fixed_right_arg(Maybe<int> arg) {
2097 has_fixed_right_arg_ = arg.IsJust();
2098 if (arg.IsJust()) fixed_right_arg_value_ = arg.FromJust();
2101 virtual void RecordToBooleanTypeFeedback(TypeFeedbackOracle* oracle) override;
2104 BinaryOperation(Zone* zone, Token::Value op, Expression* left,
2105 Expression* right, int pos)
2106 : Expression(zone, pos),
2107 op_(static_cast<byte>(op)),
2108 has_fixed_right_arg_(false),
2109 fixed_right_arg_value_(0),
2112 DCHECK(Token::IsBinaryOp(op));
2114 static int parent_num_ids() { return Expression::num_ids(); }
2117 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
2119 const byte op_; // actually Token::Value
2120 // TODO(rossberg): the fixed arg should probably be represented as a Constant
2121 // type for the RHS. Currenty it's actually a Maybe<int>
2122 bool has_fixed_right_arg_;
2123 int fixed_right_arg_value_;
2126 Handle<AllocationSite> allocation_site_;
2130 class CountOperation final : public Expression {
2132 DECLARE_NODE_TYPE(CountOperation)
2134 bool is_prefix() const { return IsPrefixField::decode(bit_field_); }
2135 bool is_postfix() const { return !is_prefix(); }
2137 Token::Value op() const { return TokenField::decode(bit_field_); }
2138 Token::Value binary_op() {
2139 return (op() == Token::INC) ? Token::ADD : Token::SUB;
2142 Expression* expression() const { return expression_; }
2144 bool IsMonomorphic() override { return receiver_types_.length() == 1; }
2145 SmallMapList* GetReceiverTypes() override { return &receiver_types_; }
2146 IcCheckType GetKeyType() const override {
2147 return KeyTypeField::decode(bit_field_);
2149 KeyedAccessStoreMode GetStoreMode() const override {
2150 return StoreModeField::decode(bit_field_);
2152 Type* type() const { return type_; }
2153 void set_key_type(IcCheckType type) {
2154 bit_field_ = KeyTypeField::update(bit_field_, type);
2156 void set_store_mode(KeyedAccessStoreMode mode) {
2157 bit_field_ = StoreModeField::update(bit_field_, mode);
2159 void set_type(Type* type) { type_ = type; }
2161 static int num_ids() { return parent_num_ids() + 4; }
2162 BailoutId AssignmentId() const { return BailoutId(local_id(0)); }
2163 BailoutId ToNumberId() const { return BailoutId(local_id(1)); }
2164 TypeFeedbackId CountBinOpFeedbackId() const {
2165 return TypeFeedbackId(local_id(2));
2167 TypeFeedbackId CountStoreFeedbackId() const {
2168 return TypeFeedbackId(local_id(3));
2171 FeedbackVectorRequirements ComputeFeedbackRequirements(
2172 Isolate* isolate, const ICSlotCache* cache) override;
2173 void SetFirstFeedbackICSlot(FeedbackVectorICSlot slot,
2174 ICSlotCache* cache) override {
2177 Code::Kind FeedbackICSlotKind(int index) override;
2178 FeedbackVectorICSlot CountSlot() const { return slot_; }
2181 CountOperation(Zone* zone, Token::Value op, bool is_prefix, Expression* expr,
2183 : Expression(zone, pos),
2185 IsPrefixField::encode(is_prefix) | KeyTypeField::encode(ELEMENT) |
2186 StoreModeField::encode(STANDARD_STORE) | TokenField::encode(op)),
2189 slot_(FeedbackVectorICSlot::Invalid()) {}
2190 static int parent_num_ids() { return Expression::num_ids(); }
2193 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
2195 class IsPrefixField : public BitField16<bool, 0, 1> {};
2196 class KeyTypeField : public BitField16<IcCheckType, 1, 1> {};
2197 class StoreModeField : public BitField16<KeyedAccessStoreMode, 2, 4> {};
2198 class TokenField : public BitField16<Token::Value, 6, 8> {};
2200 // Starts with 16-bit field, which should get packed together with
2201 // Expression's trailing 16-bit field.
2202 uint16_t bit_field_;
2204 Expression* expression_;
2205 SmallMapList receiver_types_;
2206 FeedbackVectorICSlot slot_;
2210 class CompareOperation final : public Expression {
2212 DECLARE_NODE_TYPE(CompareOperation)
2214 Token::Value op() const { return op_; }
2215 Expression* left() const { return left_; }
2216 Expression* right() const { return right_; }
2218 // Type feedback information.
2219 static int num_ids() { return parent_num_ids() + 1; }
2220 TypeFeedbackId CompareOperationFeedbackId() const {
2221 return TypeFeedbackId(local_id(0));
2223 Type* combined_type() const { return combined_type_; }
2224 void set_combined_type(Type* type) { combined_type_ = type; }
2226 // Match special cases.
2227 bool IsLiteralCompareTypeof(Expression** expr, Handle<String>* check);
2228 bool IsLiteralCompareUndefined(Expression** expr, Isolate* isolate);
2229 bool IsLiteralCompareNull(Expression** expr);
2232 CompareOperation(Zone* zone, Token::Value op, Expression* left,
2233 Expression* right, int pos)
2234 : Expression(zone, pos),
2238 combined_type_(Type::None(zone)) {
2239 DCHECK(Token::IsCompareOp(op));
2241 static int parent_num_ids() { return Expression::num_ids(); }
2244 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
2250 Type* combined_type_;
2254 class Spread final : public Expression {
2256 DECLARE_NODE_TYPE(Spread)
2258 Expression* expression() const { return expression_; }
2260 static int num_ids() { return parent_num_ids(); }
2263 Spread(Zone* zone, Expression* expression, int pos)
2264 : Expression(zone, pos), expression_(expression) {}
2265 static int parent_num_ids() { return Expression::num_ids(); }
2268 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
2270 Expression* expression_;
2274 class Conditional final : public Expression {
2276 DECLARE_NODE_TYPE(Conditional)
2278 Expression* condition() const { return condition_; }
2279 Expression* then_expression() const { return then_expression_; }
2280 Expression* else_expression() const { return else_expression_; }
2282 static int num_ids() { return parent_num_ids() + 2; }
2283 BailoutId ThenId() const { return BailoutId(local_id(0)); }
2284 BailoutId ElseId() const { return BailoutId(local_id(1)); }
2287 Conditional(Zone* zone, Expression* condition, Expression* then_expression,
2288 Expression* else_expression, int position)
2289 : Expression(zone, position),
2290 condition_(condition),
2291 then_expression_(then_expression),
2292 else_expression_(else_expression) {}
2293 static int parent_num_ids() { return Expression::num_ids(); }
2296 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
2298 Expression* condition_;
2299 Expression* then_expression_;
2300 Expression* else_expression_;
2304 class Assignment final : public Expression {
2306 DECLARE_NODE_TYPE(Assignment)
2308 Assignment* AsSimpleAssignment() { return !is_compound() ? this : NULL; }
2310 Token::Value binary_op() const;
2312 Token::Value op() const { return TokenField::decode(bit_field_); }
2313 Expression* target() const { return target_; }
2314 Expression* value() const { return value_; }
2315 BinaryOperation* binary_operation() const { return binary_operation_; }
2317 // This check relies on the definition order of token in token.h.
2318 bool is_compound() const { return op() > Token::ASSIGN; }
2320 static int num_ids() { return parent_num_ids() + 2; }
2321 BailoutId AssignmentId() const { return BailoutId(local_id(0)); }
2323 // Type feedback information.
2324 TypeFeedbackId AssignmentFeedbackId() { return TypeFeedbackId(local_id(1)); }
2325 bool IsMonomorphic() override { return receiver_types_.length() == 1; }
2326 bool IsUninitialized() const {
2327 return IsUninitializedField::decode(bit_field_);
2329 bool HasNoTypeInformation() {
2330 return IsUninitializedField::decode(bit_field_);
2332 SmallMapList* GetReceiverTypes() override { return &receiver_types_; }
2333 IcCheckType GetKeyType() const override {
2334 return KeyTypeField::decode(bit_field_);
2336 KeyedAccessStoreMode GetStoreMode() const override {
2337 return StoreModeField::decode(bit_field_);
2339 void set_is_uninitialized(bool b) {
2340 bit_field_ = IsUninitializedField::update(bit_field_, b);
2342 void set_key_type(IcCheckType key_type) {
2343 bit_field_ = KeyTypeField::update(bit_field_, key_type);
2345 void set_store_mode(KeyedAccessStoreMode mode) {
2346 bit_field_ = StoreModeField::update(bit_field_, mode);
2349 FeedbackVectorRequirements ComputeFeedbackRequirements(
2350 Isolate* isolate, const ICSlotCache* cache) override;
2351 void SetFirstFeedbackICSlot(FeedbackVectorICSlot slot,
2352 ICSlotCache* cache) override {
2355 Code::Kind FeedbackICSlotKind(int index) override;
2356 FeedbackVectorICSlot AssignmentSlot() const { return slot_; }
2359 Assignment(Zone* zone, Token::Value op, Expression* target, Expression* value,
2361 static int parent_num_ids() { return Expression::num_ids(); }
2364 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
2366 class IsUninitializedField : public BitField16<bool, 0, 1> {};
2367 class KeyTypeField : public BitField16<IcCheckType, 1, 1> {};
2368 class StoreModeField : public BitField16<KeyedAccessStoreMode, 2, 4> {};
2369 class TokenField : public BitField16<Token::Value, 6, 8> {};
2371 // Starts with 16-bit field, which should get packed together with
2372 // Expression's trailing 16-bit field.
2373 uint16_t bit_field_;
2374 Expression* target_;
2376 BinaryOperation* binary_operation_;
2377 SmallMapList receiver_types_;
2378 FeedbackVectorICSlot slot_;
2382 class Yield final : public Expression {
2384 DECLARE_NODE_TYPE(Yield)
2387 kInitial, // The initial yield that returns the unboxed generator object.
2388 kSuspend, // A normal yield: { value: EXPRESSION, done: false }
2389 kDelegating, // A yield*.
2390 kFinal // A return: { value: EXPRESSION, done: true }
2393 Expression* generator_object() const { return generator_object_; }
2394 Expression* expression() const { return expression_; }
2395 Kind yield_kind() const { return yield_kind_; }
2397 // Type feedback information.
2398 bool HasFeedbackSlots() const { return yield_kind() == kDelegating; }
2399 virtual FeedbackVectorRequirements ComputeFeedbackRequirements(
2400 Isolate* isolate, const ICSlotCache* cache) override {
2401 return FeedbackVectorRequirements(0, HasFeedbackSlots() ? 3 : 0);
2403 void SetFirstFeedbackICSlot(FeedbackVectorICSlot slot,
2404 ICSlotCache* cache) override {
2405 yield_first_feedback_slot_ = slot;
2407 Code::Kind FeedbackICSlotKind(int index) override {
2408 return index == 0 ? Code::KEYED_LOAD_IC : Code::LOAD_IC;
2411 FeedbackVectorICSlot KeyedLoadFeedbackSlot() {
2412 DCHECK(!HasFeedbackSlots() || !yield_first_feedback_slot_.IsInvalid());
2413 return yield_first_feedback_slot_;
2416 FeedbackVectorICSlot DoneFeedbackSlot() {
2417 return KeyedLoadFeedbackSlot().next();
2420 FeedbackVectorICSlot ValueFeedbackSlot() { return DoneFeedbackSlot().next(); }
2423 Yield(Zone* zone, Expression* generator_object, Expression* expression,
2424 Kind yield_kind, int pos)
2425 : Expression(zone, pos),
2426 generator_object_(generator_object),
2427 expression_(expression),
2428 yield_kind_(yield_kind),
2429 yield_first_feedback_slot_(FeedbackVectorICSlot::Invalid()) {}
2432 Expression* generator_object_;
2433 Expression* expression_;
2435 FeedbackVectorICSlot yield_first_feedback_slot_;
2439 class Throw final : public Expression {
2441 DECLARE_NODE_TYPE(Throw)
2443 Expression* exception() const { return exception_; }
2446 Throw(Zone* zone, Expression* exception, int pos)
2447 : Expression(zone, pos), exception_(exception) {}
2450 Expression* exception_;
2454 class FunctionLiteral final : public Expression {
2457 ANONYMOUS_EXPRESSION,
2462 enum ParameterFlag {
2463 kNoDuplicateParameters = 0,
2464 kHasDuplicateParameters = 1
2467 enum IsFunctionFlag {
2472 enum EagerCompileHint { kShouldEagerCompile, kShouldLazyCompile };
2474 enum ShouldBeUsedOnceHint { kShouldBeUsedOnce, kDontKnowIfShouldBeUsedOnce };
2476 enum ArityRestriction {
2482 DECLARE_NODE_TYPE(FunctionLiteral)
2484 Handle<String> name() const { return raw_name_->string(); }
2485 const AstRawString* raw_name() const { return raw_name_; }
2486 Scope* scope() const { return scope_; }
2487 ZoneList<Statement*>* body() const { return body_; }
2488 void set_function_token_position(int pos) { function_token_position_ = pos; }
2489 int function_token_position() const { return function_token_position_; }
2490 int start_position() const;
2491 int end_position() const;
2492 int SourceSize() const { return end_position() - start_position(); }
2493 bool is_expression() const { return IsExpression::decode(bitfield_); }
2494 bool is_anonymous() const { return IsAnonymous::decode(bitfield_); }
2495 LanguageMode language_mode() const;
2497 static bool NeedsHomeObject(Expression* expr);
2499 int materialized_literal_count() { return materialized_literal_count_; }
2500 int expected_property_count() { return expected_property_count_; }
2501 int parameter_count() { return parameter_count_; }
2503 bool AllowsLazyCompilation();
2504 bool AllowsLazyCompilationWithoutContext();
2506 void InitializeSharedInfo(Handle<Code> code);
2508 Handle<String> debug_name() const {
2509 if (raw_name_ != NULL && !raw_name_->IsEmpty()) {
2510 return raw_name_->string();
2512 return inferred_name();
2515 Handle<String> inferred_name() const {
2516 if (!inferred_name_.is_null()) {
2517 DCHECK(raw_inferred_name_ == NULL);
2518 return inferred_name_;
2520 if (raw_inferred_name_ != NULL) {
2521 return raw_inferred_name_->string();
2524 return Handle<String>();
2527 // Only one of {set_inferred_name, set_raw_inferred_name} should be called.
2528 void set_inferred_name(Handle<String> inferred_name) {
2529 DCHECK(!inferred_name.is_null());
2530 inferred_name_ = inferred_name;
2531 DCHECK(raw_inferred_name_== NULL || raw_inferred_name_->IsEmpty());
2532 raw_inferred_name_ = NULL;
2535 void set_raw_inferred_name(const AstString* raw_inferred_name) {
2536 DCHECK(raw_inferred_name != NULL);
2537 raw_inferred_name_ = raw_inferred_name;
2538 DCHECK(inferred_name_.is_null());
2539 inferred_name_ = Handle<String>();
2542 // shared_info may be null if it's not cached in full code.
2543 Handle<SharedFunctionInfo> shared_info() { return shared_info_; }
2545 bool pretenure() { return Pretenure::decode(bitfield_); }
2546 void set_pretenure() { bitfield_ |= Pretenure::encode(true); }
2548 bool has_duplicate_parameters() {
2549 return HasDuplicateParameters::decode(bitfield_);
2552 bool is_function() { return IsFunction::decode(bitfield_) == kIsFunction; }
2554 // This is used as a heuristic on when to eagerly compile a function
2555 // literal. We consider the following constructs as hints that the
2556 // function will be called immediately:
2557 // - (function() { ... })();
2558 // - var x = function() { ... }();
2559 bool should_eager_compile() const {
2560 return EagerCompileHintBit::decode(bitfield_) == kShouldEagerCompile;
2562 void set_should_eager_compile() {
2563 bitfield_ = EagerCompileHintBit::update(bitfield_, kShouldEagerCompile);
2566 // A hint that we expect this function to be called (exactly) once,
2567 // i.e. we suspect it's an initialization function.
2568 bool should_be_used_once_hint() const {
2569 return ShouldBeUsedOnceHintBit::decode(bitfield_) == kShouldBeUsedOnce;
2571 void set_should_be_used_once_hint() {
2572 bitfield_ = ShouldBeUsedOnceHintBit::update(bitfield_, kShouldBeUsedOnce);
2575 FunctionKind kind() const { return FunctionKindBits::decode(bitfield_); }
2577 int ast_node_count() { return ast_properties_.node_count(); }
2578 AstProperties::Flags* flags() { return ast_properties_.flags(); }
2579 void set_ast_properties(AstProperties* ast_properties) {
2580 ast_properties_ = *ast_properties;
2582 const ZoneFeedbackVectorSpec* feedback_vector_spec() const {
2583 return ast_properties_.get_spec();
2585 bool dont_optimize() { return dont_optimize_reason_ != kNoReason; }
2586 BailoutReason dont_optimize_reason() { return dont_optimize_reason_; }
2587 void set_dont_optimize_reason(BailoutReason reason) {
2588 dont_optimize_reason_ = reason;
2592 FunctionLiteral(Zone* zone, const AstRawString* name,
2593 AstValueFactory* ast_value_factory, Scope* scope,
2594 ZoneList<Statement*>* body, int materialized_literal_count,
2595 int expected_property_count, int parameter_count,
2596 FunctionType function_type,
2597 ParameterFlag has_duplicate_parameters,
2598 IsFunctionFlag is_function,
2599 EagerCompileHint eager_compile_hint, FunctionKind kind,
2601 : Expression(zone, position),
2605 raw_inferred_name_(ast_value_factory->empty_string()),
2606 ast_properties_(zone),
2607 dont_optimize_reason_(kNoReason),
2608 materialized_literal_count_(materialized_literal_count),
2609 expected_property_count_(expected_property_count),
2610 parameter_count_(parameter_count),
2611 function_token_position_(RelocInfo::kNoPosition) {
2612 bitfield_ = IsExpression::encode(function_type != DECLARATION) |
2613 IsAnonymous::encode(function_type == ANONYMOUS_EXPRESSION) |
2614 Pretenure::encode(false) |
2615 HasDuplicateParameters::encode(has_duplicate_parameters) |
2616 IsFunction::encode(is_function) |
2617 EagerCompileHintBit::encode(eager_compile_hint) |
2618 FunctionKindBits::encode(kind) |
2619 ShouldBeUsedOnceHintBit::encode(kDontKnowIfShouldBeUsedOnce);
2620 DCHECK(IsValidFunctionKind(kind));
2624 const AstRawString* raw_name_;
2625 Handle<String> name_;
2626 Handle<SharedFunctionInfo> shared_info_;
2628 ZoneList<Statement*>* body_;
2629 const AstString* raw_inferred_name_;
2630 Handle<String> inferred_name_;
2631 AstProperties ast_properties_;
2632 BailoutReason dont_optimize_reason_;
2634 int materialized_literal_count_;
2635 int expected_property_count_;
2636 int parameter_count_;
2637 int function_token_position_;
2640 class IsExpression : public BitField<bool, 0, 1> {};
2641 class IsAnonymous : public BitField<bool, 1, 1> {};
2642 class Pretenure : public BitField<bool, 2, 1> {};
2643 class HasDuplicateParameters : public BitField<ParameterFlag, 3, 1> {};
2644 class IsFunction : public BitField<IsFunctionFlag, 4, 1> {};
2645 class EagerCompileHintBit : public BitField<EagerCompileHint, 5, 1> {};
2646 class FunctionKindBits : public BitField<FunctionKind, 6, 8> {};
2647 class ShouldBeUsedOnceHintBit : public BitField<ShouldBeUsedOnceHint, 15, 1> {
2652 class ClassLiteral final : public Expression {
2654 typedef ObjectLiteralProperty Property;
2656 DECLARE_NODE_TYPE(ClassLiteral)
2658 Handle<String> name() const { return raw_name_->string(); }
2659 const AstRawString* raw_name() const { return raw_name_; }
2660 Scope* scope() const { return scope_; }
2661 VariableProxy* class_variable_proxy() const { return class_variable_proxy_; }
2662 Expression* extends() const { return extends_; }
2663 FunctionLiteral* constructor() const { return constructor_; }
2664 ZoneList<Property*>* properties() const { return properties_; }
2665 int start_position() const { return position(); }
2666 int end_position() const { return end_position_; }
2668 BailoutId EntryId() const { return BailoutId(local_id(0)); }
2669 BailoutId DeclsId() const { return BailoutId(local_id(1)); }
2670 BailoutId ExitId() { return BailoutId(local_id(2)); }
2671 BailoutId CreateLiteralId() const { return BailoutId(local_id(3)); }
2673 // Return an AST id for a property that is used in simulate instructions.
2674 BailoutId GetIdForProperty(int i) { return BailoutId(local_id(i + 4)); }
2676 // Unlike other AST nodes, this number of bailout IDs allocated for an
2677 // ClassLiteral can vary, so num_ids() is not a static method.
2678 int num_ids() const { return parent_num_ids() + 4 + properties()->length(); }
2680 // Object literals need one feedback slot for each non-trivial value, as well
2681 // as some slots for home objects.
2682 FeedbackVectorRequirements ComputeFeedbackRequirements(
2683 Isolate* isolate, const ICSlotCache* cache) override;
2684 void SetFirstFeedbackICSlot(FeedbackVectorICSlot slot,
2685 ICSlotCache* cache) override {
2688 Code::Kind FeedbackICSlotKind(int index) override { return Code::STORE_IC; }
2689 FeedbackVectorICSlot GetNthSlot(int n) const {
2690 return FeedbackVectorICSlot(slot_.ToInt() + n);
2693 // If value needs a home object, returns a valid feedback vector ic slot
2694 // given by slot_index, and increments slot_index.
2695 FeedbackVectorICSlot SlotForHomeObject(Expression* value,
2696 int* slot_index) const;
2699 int slot_count() const { return slot_count_; }
2703 ClassLiteral(Zone* zone, const AstRawString* name, Scope* scope,
2704 VariableProxy* class_variable_proxy, Expression* extends,
2705 FunctionLiteral* constructor, ZoneList<Property*>* properties,
2706 int start_position, int end_position)
2707 : Expression(zone, start_position),
2710 class_variable_proxy_(class_variable_proxy),
2712 constructor_(constructor),
2713 properties_(properties),
2714 end_position_(end_position),
2718 slot_(FeedbackVectorICSlot::Invalid()) {
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_;
2734 // slot_count_ helps validate that the logic to allocate ic slots and the
2735 // logic to use them are in sync.
2738 FeedbackVectorICSlot slot_;
2742 class NativeFunctionLiteral final : public Expression {
2744 DECLARE_NODE_TYPE(NativeFunctionLiteral)
2746 Handle<String> name() const { return name_->string(); }
2747 v8::Extension* extension() const { return extension_; }
2750 NativeFunctionLiteral(Zone* zone, const AstRawString* name,
2751 v8::Extension* extension, int pos)
2752 : Expression(zone, pos), name_(name), extension_(extension) {}
2755 const AstRawString* name_;
2756 v8::Extension* extension_;
2760 class ThisFunction final : public Expression {
2762 DECLARE_NODE_TYPE(ThisFunction)
2765 ThisFunction(Zone* zone, int pos) : Expression(zone, pos) {}
2769 class SuperPropertyReference final : public Expression {
2771 DECLARE_NODE_TYPE(SuperPropertyReference)
2773 VariableProxy* this_var() const { return this_var_; }
2774 Expression* home_object() const { return home_object_; }
2777 SuperPropertyReference(Zone* zone, VariableProxy* this_var,
2778 Expression* home_object, int pos)
2779 : Expression(zone, pos), this_var_(this_var), home_object_(home_object) {
2780 DCHECK(this_var->is_this());
2781 DCHECK(home_object->IsProperty());
2785 VariableProxy* this_var_;
2786 Expression* home_object_;
2790 class SuperCallReference final : public Expression {
2792 DECLARE_NODE_TYPE(SuperCallReference)
2794 VariableProxy* this_var() const { return this_var_; }
2795 VariableProxy* new_target_var() const { return new_target_var_; }
2796 VariableProxy* this_function_var() const { return this_function_var_; }
2799 SuperCallReference(Zone* zone, VariableProxy* this_var,
2800 VariableProxy* new_target_var,
2801 VariableProxy* this_function_var, int pos)
2802 : Expression(zone, pos),
2803 this_var_(this_var),
2804 new_target_var_(new_target_var),
2805 this_function_var_(this_function_var) {
2806 DCHECK(this_var->is_this());
2807 DCHECK(new_target_var->raw_name()->IsOneByteEqualTo("new.target"));
2808 DCHECK(this_function_var->raw_name()->IsOneByteEqualTo(".this_function"));
2812 VariableProxy* this_var_;
2813 VariableProxy* new_target_var_;
2814 VariableProxy* this_function_var_;
2818 #undef DECLARE_NODE_TYPE
2821 // ----------------------------------------------------------------------------
2822 // Regular expressions
2825 class RegExpVisitor BASE_EMBEDDED {
2827 virtual ~RegExpVisitor() { }
2828 #define MAKE_CASE(Name) \
2829 virtual void* Visit##Name(RegExp##Name*, void* data) = 0;
2830 FOR_EACH_REG_EXP_TREE_TYPE(MAKE_CASE)
2835 class RegExpTree : public ZoneObject {
2837 static const int kInfinity = kMaxInt;
2838 virtual ~RegExpTree() {}
2839 virtual void* Accept(RegExpVisitor* visitor, void* data) = 0;
2840 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
2841 RegExpNode* on_success) = 0;
2842 virtual bool IsTextElement() { return false; }
2843 virtual bool IsAnchoredAtStart() { return false; }
2844 virtual bool IsAnchoredAtEnd() { return false; }
2845 virtual int min_match() = 0;
2846 virtual int max_match() = 0;
2847 // Returns the interval of registers used for captures within this
2849 virtual Interval CaptureRegisters() { return Interval::Empty(); }
2850 virtual void AppendToText(RegExpText* text, Zone* zone);
2851 std::ostream& Print(std::ostream& os, Zone* zone); // NOLINT
2852 #define MAKE_ASTYPE(Name) \
2853 virtual RegExp##Name* As##Name(); \
2854 virtual bool Is##Name();
2855 FOR_EACH_REG_EXP_TREE_TYPE(MAKE_ASTYPE)
2860 class RegExpDisjunction final : public RegExpTree {
2862 explicit RegExpDisjunction(ZoneList<RegExpTree*>* alternatives);
2863 void* Accept(RegExpVisitor* visitor, void* data) override;
2864 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
2865 RegExpNode* on_success) override;
2866 RegExpDisjunction* AsDisjunction() override;
2867 Interval CaptureRegisters() override;
2868 bool IsDisjunction() override;
2869 bool IsAnchoredAtStart() override;
2870 bool IsAnchoredAtEnd() override;
2871 int min_match() override { return min_match_; }
2872 int max_match() override { return max_match_; }
2873 ZoneList<RegExpTree*>* alternatives() { return alternatives_; }
2875 ZoneList<RegExpTree*>* alternatives_;
2881 class RegExpAlternative final : public RegExpTree {
2883 explicit RegExpAlternative(ZoneList<RegExpTree*>* nodes);
2884 void* Accept(RegExpVisitor* visitor, void* data) override;
2885 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
2886 RegExpNode* on_success) override;
2887 RegExpAlternative* AsAlternative() override;
2888 Interval CaptureRegisters() override;
2889 bool IsAlternative() override;
2890 bool IsAnchoredAtStart() override;
2891 bool IsAnchoredAtEnd() override;
2892 int min_match() override { return min_match_; }
2893 int max_match() override { return max_match_; }
2894 ZoneList<RegExpTree*>* nodes() { return nodes_; }
2896 ZoneList<RegExpTree*>* nodes_;
2902 class RegExpAssertion final : public RegExpTree {
2904 enum AssertionType {
2912 explicit RegExpAssertion(AssertionType type) : assertion_type_(type) { }
2913 void* Accept(RegExpVisitor* visitor, void* data) override;
2914 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
2915 RegExpNode* on_success) override;
2916 RegExpAssertion* AsAssertion() override;
2917 bool IsAssertion() override;
2918 bool IsAnchoredAtStart() override;
2919 bool IsAnchoredAtEnd() override;
2920 int min_match() override { return 0; }
2921 int max_match() override { return 0; }
2922 AssertionType assertion_type() { return assertion_type_; }
2924 AssertionType assertion_type_;
2928 class CharacterSet final BASE_EMBEDDED {
2930 explicit CharacterSet(uc16 standard_set_type)
2932 standard_set_type_(standard_set_type) {}
2933 explicit CharacterSet(ZoneList<CharacterRange>* ranges)
2935 standard_set_type_(0) {}
2936 ZoneList<CharacterRange>* ranges(Zone* zone);
2937 uc16 standard_set_type() { return standard_set_type_; }
2938 void set_standard_set_type(uc16 special_set_type) {
2939 standard_set_type_ = special_set_type;
2941 bool is_standard() { return standard_set_type_ != 0; }
2942 void Canonicalize();
2944 ZoneList<CharacterRange>* ranges_;
2945 // If non-zero, the value represents a standard set (e.g., all whitespace
2946 // characters) without having to expand the ranges.
2947 uc16 standard_set_type_;
2951 class RegExpCharacterClass final : public RegExpTree {
2953 RegExpCharacterClass(ZoneList<CharacterRange>* ranges, bool is_negated)
2955 is_negated_(is_negated) { }
2956 explicit RegExpCharacterClass(uc16 type)
2958 is_negated_(false) { }
2959 void* Accept(RegExpVisitor* visitor, void* data) override;
2960 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
2961 RegExpNode* on_success) override;
2962 RegExpCharacterClass* AsCharacterClass() override;
2963 bool IsCharacterClass() override;
2964 bool IsTextElement() override { return true; }
2965 int min_match() override { return 1; }
2966 int max_match() override { return 1; }
2967 void AppendToText(RegExpText* text, Zone* zone) override;
2968 CharacterSet character_set() { return set_; }
2969 // TODO(lrn): Remove need for complex version if is_standard that
2970 // recognizes a mangled standard set and just do { return set_.is_special(); }
2971 bool is_standard(Zone* zone);
2972 // Returns a value representing the standard character set if is_standard()
2974 // Currently used values are:
2975 // s : unicode whitespace
2976 // S : unicode non-whitespace
2977 // w : ASCII word character (digit, letter, underscore)
2978 // W : non-ASCII word character
2980 // D : non-ASCII digit
2981 // . : non-unicode non-newline
2982 // * : All characters
2983 uc16 standard_type() { return set_.standard_set_type(); }
2984 ZoneList<CharacterRange>* ranges(Zone* zone) { return set_.ranges(zone); }
2985 bool is_negated() { return is_negated_; }
2993 class RegExpAtom final : public RegExpTree {
2995 explicit RegExpAtom(Vector<const uc16> data) : data_(data) { }
2996 void* Accept(RegExpVisitor* visitor, void* data) override;
2997 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
2998 RegExpNode* on_success) override;
2999 RegExpAtom* AsAtom() override;
3000 bool IsAtom() override;
3001 bool IsTextElement() override { return true; }
3002 int min_match() override { return data_.length(); }
3003 int max_match() override { return data_.length(); }
3004 void AppendToText(RegExpText* text, Zone* zone) override;
3005 Vector<const uc16> data() { return data_; }
3006 int length() { return data_.length(); }
3008 Vector<const uc16> data_;
3012 class RegExpText final : public RegExpTree {
3014 explicit RegExpText(Zone* zone) : elements_(2, zone), length_(0) {}
3015 void* Accept(RegExpVisitor* visitor, void* data) override;
3016 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
3017 RegExpNode* on_success) override;
3018 RegExpText* AsText() override;
3019 bool IsText() override;
3020 bool IsTextElement() override { return true; }
3021 int min_match() override { return length_; }
3022 int max_match() override { return length_; }
3023 void AppendToText(RegExpText* text, Zone* zone) override;
3024 void AddElement(TextElement elm, Zone* zone) {
3025 elements_.Add(elm, zone);
3026 length_ += elm.length();
3028 ZoneList<TextElement>* elements() { return &elements_; }
3030 ZoneList<TextElement> elements_;
3035 class RegExpQuantifier final : public RegExpTree {
3037 enum QuantifierType { GREEDY, NON_GREEDY, POSSESSIVE };
3038 RegExpQuantifier(int min, int max, QuantifierType type, RegExpTree* body)
3042 min_match_(min * body->min_match()),
3043 quantifier_type_(type) {
3044 if (max > 0 && body->max_match() > kInfinity / max) {
3045 max_match_ = kInfinity;
3047 max_match_ = max * body->max_match();
3050 void* Accept(RegExpVisitor* visitor, void* data) override;
3051 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
3052 RegExpNode* on_success) override;
3053 static RegExpNode* ToNode(int min,
3057 RegExpCompiler* compiler,
3058 RegExpNode* on_success,
3059 bool not_at_start = false);
3060 RegExpQuantifier* AsQuantifier() override;
3061 Interval CaptureRegisters() override;
3062 bool IsQuantifier() override;
3063 int min_match() override { return min_match_; }
3064 int max_match() override { return max_match_; }
3065 int min() { return min_; }
3066 int max() { return max_; }
3067 bool is_possessive() { return quantifier_type_ == POSSESSIVE; }
3068 bool is_non_greedy() { return quantifier_type_ == NON_GREEDY; }
3069 bool is_greedy() { return quantifier_type_ == GREEDY; }
3070 RegExpTree* body() { return body_; }
3078 QuantifierType quantifier_type_;
3082 class RegExpCapture final : public RegExpTree {
3084 explicit RegExpCapture(RegExpTree* body, int index)
3085 : body_(body), index_(index) { }
3086 void* Accept(RegExpVisitor* visitor, void* data) override;
3087 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
3088 RegExpNode* on_success) override;
3089 static RegExpNode* ToNode(RegExpTree* body,
3091 RegExpCompiler* compiler,
3092 RegExpNode* on_success);
3093 RegExpCapture* AsCapture() override;
3094 bool IsAnchoredAtStart() override;
3095 bool IsAnchoredAtEnd() override;
3096 Interval CaptureRegisters() override;
3097 bool IsCapture() override;
3098 int min_match() override { return body_->min_match(); }
3099 int max_match() override { return body_->max_match(); }
3100 RegExpTree* body() { return body_; }
3101 int index() { return index_; }
3102 static int StartRegister(int index) { return index * 2; }
3103 static int EndRegister(int index) { return index * 2 + 1; }
3111 class RegExpLookahead final : public RegExpTree {
3113 RegExpLookahead(RegExpTree* body,
3118 is_positive_(is_positive),
3119 capture_count_(capture_count),
3120 capture_from_(capture_from) { }
3122 void* Accept(RegExpVisitor* visitor, void* data) override;
3123 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
3124 RegExpNode* on_success) override;
3125 RegExpLookahead* AsLookahead() override;
3126 Interval CaptureRegisters() override;
3127 bool IsLookahead() override;
3128 bool IsAnchoredAtStart() override;
3129 int min_match() override { return 0; }
3130 int max_match() override { return 0; }
3131 RegExpTree* body() { return body_; }
3132 bool is_positive() { return is_positive_; }
3133 int capture_count() { return capture_count_; }
3134 int capture_from() { return capture_from_; }
3144 class RegExpBackReference final : public RegExpTree {
3146 explicit RegExpBackReference(RegExpCapture* capture)
3147 : capture_(capture) { }
3148 void* Accept(RegExpVisitor* visitor, void* data) override;
3149 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
3150 RegExpNode* on_success) override;
3151 RegExpBackReference* AsBackReference() override;
3152 bool IsBackReference() override;
3153 int min_match() override { return 0; }
3154 int max_match() override { return capture_->max_match(); }
3155 int index() { return capture_->index(); }
3156 RegExpCapture* capture() { return capture_; }
3158 RegExpCapture* capture_;
3162 class RegExpEmpty final : public RegExpTree {
3165 void* Accept(RegExpVisitor* visitor, void* data) override;
3166 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
3167 RegExpNode* on_success) override;
3168 RegExpEmpty* AsEmpty() override;
3169 bool IsEmpty() override;
3170 int min_match() override { return 0; }
3171 int max_match() override { return 0; }
3175 // ----------------------------------------------------------------------------
3177 // - leaf node visitors are abstract.
3179 class AstVisitor BASE_EMBEDDED {
3182 virtual ~AstVisitor() {}
3184 // Stack overflow check and dynamic dispatch.
3185 virtual void Visit(AstNode* node) = 0;
3187 // Iteration left-to-right.
3188 virtual void VisitDeclarations(ZoneList<Declaration*>* declarations);
3189 virtual void VisitStatements(ZoneList<Statement*>* statements);
3190 virtual void VisitExpressions(ZoneList<Expression*>* expressions);
3192 // Individual AST nodes.
3193 #define DEF_VISIT(type) \
3194 virtual void Visit##type(type* node) = 0;
3195 AST_NODE_LIST(DEF_VISIT)
3200 #define DEFINE_AST_VISITOR_SUBCLASS_MEMBERS() \
3202 void Visit(AstNode* node) final { \
3203 if (!CheckStackOverflow()) node->Accept(this); \
3206 void SetStackOverflow() { stack_overflow_ = true; } \
3207 void ClearStackOverflow() { stack_overflow_ = false; } \
3208 bool HasStackOverflow() const { return stack_overflow_; } \
3210 bool CheckStackOverflow() { \
3211 if (stack_overflow_) return true; \
3212 StackLimitCheck check(isolate_); \
3213 if (!check.HasOverflowed()) return false; \
3214 stack_overflow_ = true; \
3219 void InitializeAstVisitor(Isolate* isolate, Zone* zone) { \
3220 isolate_ = isolate; \
3222 stack_overflow_ = false; \
3224 Zone* zone() { return zone_; } \
3225 Isolate* isolate() { return isolate_; } \
3227 Isolate* isolate_; \
3229 bool stack_overflow_
3232 // ----------------------------------------------------------------------------
3235 class AstNodeFactory final BASE_EMBEDDED {
3237 explicit AstNodeFactory(AstValueFactory* ast_value_factory)
3238 : zone_(ast_value_factory->zone()),
3239 ast_value_factory_(ast_value_factory) {}
3241 VariableDeclaration* NewVariableDeclaration(
3242 VariableProxy* proxy, VariableMode mode, Scope* scope, int pos,
3243 bool is_class_declaration = false, int declaration_group_start = -1) {
3245 VariableDeclaration(zone_, proxy, mode, scope, pos,
3246 is_class_declaration, declaration_group_start);
3249 FunctionDeclaration* NewFunctionDeclaration(VariableProxy* proxy,
3251 FunctionLiteral* fun,
3254 return new (zone_) FunctionDeclaration(zone_, proxy, mode, fun, scope, pos);
3257 ImportDeclaration* NewImportDeclaration(VariableProxy* proxy,
3258 const AstRawString* import_name,
3259 const AstRawString* module_specifier,
3260 Scope* scope, int pos) {
3261 return new (zone_) ImportDeclaration(zone_, proxy, import_name,
3262 module_specifier, scope, pos);
3265 ExportDeclaration* NewExportDeclaration(VariableProxy* proxy,
3268 return new (zone_) ExportDeclaration(zone_, proxy, scope, pos);
3271 Block* NewBlock(ZoneList<const AstRawString*>* labels,
3273 bool is_initializer_block,
3276 Block(zone_, labels, capacity, is_initializer_block, pos);
3279 #define STATEMENT_WITH_LABELS(NodeType) \
3280 NodeType* New##NodeType(ZoneList<const AstRawString*>* labels, int pos) { \
3281 return new (zone_) NodeType(zone_, labels, pos); \
3283 STATEMENT_WITH_LABELS(DoWhileStatement)
3284 STATEMENT_WITH_LABELS(WhileStatement)
3285 STATEMENT_WITH_LABELS(ForStatement)
3286 STATEMENT_WITH_LABELS(SwitchStatement)
3287 #undef STATEMENT_WITH_LABELS
3289 ForEachStatement* NewForEachStatement(ForEachStatement::VisitMode visit_mode,
3290 ZoneList<const AstRawString*>* labels,
3292 switch (visit_mode) {
3293 case ForEachStatement::ENUMERATE: {
3294 return new (zone_) ForInStatement(zone_, labels, pos);
3296 case ForEachStatement::ITERATE: {
3297 return new (zone_) ForOfStatement(zone_, labels, pos);
3304 ExpressionStatement* NewExpressionStatement(Expression* expression, int pos) {
3305 return new (zone_) ExpressionStatement(zone_, expression, pos);
3308 ContinueStatement* NewContinueStatement(IterationStatement* target, int pos) {
3309 return new (zone_) ContinueStatement(zone_, target, pos);
3312 BreakStatement* NewBreakStatement(BreakableStatement* target, int pos) {
3313 return new (zone_) BreakStatement(zone_, target, pos);
3316 ReturnStatement* NewReturnStatement(Expression* expression, int pos) {
3317 return new (zone_) ReturnStatement(zone_, expression, pos);
3320 WithStatement* NewWithStatement(Scope* scope,
3321 Expression* expression,
3322 Statement* statement,
3324 return new (zone_) WithStatement(zone_, scope, expression, statement, pos);
3327 IfStatement* NewIfStatement(Expression* condition,
3328 Statement* then_statement,
3329 Statement* else_statement,
3332 IfStatement(zone_, condition, then_statement, else_statement, pos);
3335 TryCatchStatement* NewTryCatchStatement(Block* try_block, Scope* scope,
3337 Block* catch_block, int pos) {
3339 TryCatchStatement(zone_, try_block, scope, variable, catch_block, pos);
3342 TryFinallyStatement* NewTryFinallyStatement(Block* try_block,
3343 Block* finally_block, int pos) {
3345 TryFinallyStatement(zone_, try_block, finally_block, pos);
3348 DebuggerStatement* NewDebuggerStatement(int pos) {
3349 return new (zone_) DebuggerStatement(zone_, pos);
3352 EmptyStatement* NewEmptyStatement(int pos) {
3353 return new(zone_) EmptyStatement(zone_, pos);
3356 CaseClause* NewCaseClause(
3357 Expression* label, ZoneList<Statement*>* statements, int pos) {
3358 return new (zone_) CaseClause(zone_, label, statements, pos);
3361 Literal* NewStringLiteral(const AstRawString* string, int pos) {
3363 Literal(zone_, ast_value_factory_->NewString(string), pos);
3366 // A JavaScript symbol (ECMA-262 edition 6).
3367 Literal* NewSymbolLiteral(const char* name, int pos) {
3368 return new (zone_) Literal(zone_, ast_value_factory_->NewSymbol(name), pos);
3371 Literal* NewNumberLiteral(double number, int pos) {
3373 Literal(zone_, ast_value_factory_->NewNumber(number), pos);
3376 Literal* NewSmiLiteral(int number, int pos) {
3377 return new (zone_) Literal(zone_, ast_value_factory_->NewSmi(number), pos);
3380 Literal* NewBooleanLiteral(bool b, int pos) {
3381 return new (zone_) Literal(zone_, ast_value_factory_->NewBoolean(b), pos);
3384 Literal* NewNullLiteral(int pos) {
3385 return new (zone_) Literal(zone_, ast_value_factory_->NewNull(), pos);
3388 Literal* NewUndefinedLiteral(int pos) {
3389 return new (zone_) Literal(zone_, ast_value_factory_->NewUndefined(), pos);
3392 Literal* NewTheHoleLiteral(int pos) {
3393 return new (zone_) Literal(zone_, ast_value_factory_->NewTheHole(), pos);
3396 ObjectLiteral* NewObjectLiteral(
3397 ZoneList<ObjectLiteral::Property*>* properties,
3399 int boilerplate_properties,
3403 return new (zone_) ObjectLiteral(zone_, properties, literal_index,
3404 boilerplate_properties, has_function,
3408 ObjectLiteral::Property* NewObjectLiteralProperty(
3409 Expression* key, Expression* value, ObjectLiteralProperty::Kind kind,
3410 bool is_static, bool is_computed_name) {
3412 ObjectLiteral::Property(key, value, kind, is_static, is_computed_name);
3415 ObjectLiteral::Property* NewObjectLiteralProperty(Expression* key,
3418 bool is_computed_name) {
3419 return new (zone_) ObjectLiteral::Property(ast_value_factory_, key, value,
3420 is_static, is_computed_name);
3423 RegExpLiteral* NewRegExpLiteral(const AstRawString* pattern,
3424 const AstRawString* flags,
3428 return new (zone_) RegExpLiteral(zone_, pattern, flags, literal_index,
3432 ArrayLiteral* NewArrayLiteral(ZoneList<Expression*>* values,
3436 return new (zone_) ArrayLiteral(zone_, values, literal_index, is_strong,
3440 VariableProxy* NewVariableProxy(Variable* var,
3441 int start_position = RelocInfo::kNoPosition,
3442 int end_position = RelocInfo::kNoPosition) {
3443 return new (zone_) VariableProxy(zone_, var, start_position, end_position);
3446 VariableProxy* NewVariableProxy(const AstRawString* name,
3447 Variable::Kind variable_kind,
3448 int start_position = RelocInfo::kNoPosition,
3449 int end_position = RelocInfo::kNoPosition) {
3450 DCHECK_NOT_NULL(name);
3452 VariableProxy(zone_, name, variable_kind, start_position, end_position);
3455 Property* NewProperty(Expression* obj, Expression* key, int pos) {
3456 return new (zone_) Property(zone_, obj, key, pos);
3459 Call* NewCall(Expression* expression,
3460 ZoneList<Expression*>* arguments,
3462 return new (zone_) Call(zone_, expression, arguments, pos);
3465 CallNew* NewCallNew(Expression* expression,
3466 ZoneList<Expression*>* arguments,
3468 return new (zone_) CallNew(zone_, expression, arguments, pos);
3471 CallRuntime* NewCallRuntime(const AstRawString* name,
3472 const Runtime::Function* function,
3473 ZoneList<Expression*>* arguments,
3475 return new (zone_) CallRuntime(zone_, name, function, arguments, pos);
3478 UnaryOperation* NewUnaryOperation(Token::Value op,
3479 Expression* expression,
3481 return new (zone_) UnaryOperation(zone_, op, expression, pos);
3484 BinaryOperation* NewBinaryOperation(Token::Value op,
3488 return new (zone_) BinaryOperation(zone_, op, left, right, pos);
3491 CountOperation* NewCountOperation(Token::Value op,
3495 return new (zone_) CountOperation(zone_, op, is_prefix, expr, pos);
3498 CompareOperation* NewCompareOperation(Token::Value op,
3502 return new (zone_) CompareOperation(zone_, op, left, right, pos);
3505 Spread* NewSpread(Expression* expression, int pos) {
3506 return new (zone_) Spread(zone_, expression, pos);
3509 Conditional* NewConditional(Expression* condition,
3510 Expression* then_expression,
3511 Expression* else_expression,
3513 return new (zone_) Conditional(zone_, condition, then_expression,
3514 else_expression, position);
3517 Assignment* NewAssignment(Token::Value op,
3521 DCHECK(Token::IsAssignmentOp(op));
3522 Assignment* assign = new (zone_) Assignment(zone_, op, target, value, pos);
3523 if (assign->is_compound()) {
3524 DCHECK(Token::IsAssignmentOp(op));
3525 assign->binary_operation_ =
3526 NewBinaryOperation(assign->binary_op(), target, value, pos + 1);
3531 Yield* NewYield(Expression *generator_object,
3532 Expression* expression,
3533 Yield::Kind yield_kind,
3535 if (!expression) expression = NewUndefinedLiteral(pos);
3537 Yield(zone_, generator_object, expression, yield_kind, pos);
3540 Throw* NewThrow(Expression* exception, int pos) {
3541 return new (zone_) Throw(zone_, exception, pos);
3544 FunctionLiteral* NewFunctionLiteral(
3545 const AstRawString* name, AstValueFactory* ast_value_factory,
3546 Scope* scope, ZoneList<Statement*>* body, int materialized_literal_count,
3547 int expected_property_count, int parameter_count,
3548 FunctionLiteral::ParameterFlag has_duplicate_parameters,
3549 FunctionLiteral::FunctionType function_type,
3550 FunctionLiteral::IsFunctionFlag is_function,
3551 FunctionLiteral::EagerCompileHint eager_compile_hint, FunctionKind kind,
3553 return new (zone_) FunctionLiteral(
3554 zone_, name, ast_value_factory, scope, body, materialized_literal_count,
3555 expected_property_count, parameter_count, function_type,
3556 has_duplicate_parameters, is_function, eager_compile_hint, kind,
3560 ClassLiteral* NewClassLiteral(const AstRawString* name, Scope* scope,
3561 VariableProxy* proxy, Expression* extends,
3562 FunctionLiteral* constructor,
3563 ZoneList<ObjectLiteral::Property*>* properties,
3564 int start_position, int end_position) {
3566 ClassLiteral(zone_, name, scope, proxy, extends, constructor,
3567 properties, start_position, end_position);
3570 NativeFunctionLiteral* NewNativeFunctionLiteral(const AstRawString* name,
3571 v8::Extension* extension,
3573 return new (zone_) NativeFunctionLiteral(zone_, name, extension, pos);
3576 ThisFunction* NewThisFunction(int pos) {
3577 return new (zone_) ThisFunction(zone_, pos);
3580 SuperPropertyReference* NewSuperPropertyReference(VariableProxy* this_var,
3581 Expression* home_object,
3584 SuperPropertyReference(zone_, this_var, home_object, pos);
3587 SuperCallReference* NewSuperCallReference(VariableProxy* this_var,
3588 VariableProxy* new_target_var,
3589 VariableProxy* this_function_var,
3591 return new (zone_) SuperCallReference(zone_, this_var, new_target_var,
3592 this_function_var, pos);
3597 AstValueFactory* ast_value_factory_;
3601 } } // namespace v8::internal