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 ignore_completion_value() const { return ignore_completion_value_; }
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 ignore_completion_value, int pos)
503 : BreakableStatement(zone, labels, TARGET_FOR_NAMED_ONLY, pos),
504 statements_(capacity, zone),
505 ignore_completion_value_(ignore_completion_value),
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 ignore_completion_value_;
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_; }
1166 void set_base_id(int id) { base_id_ = id; }
1167 static int num_ids() { return parent_num_ids() + 1; }
1168 BailoutId HandlerId() const { return BailoutId(local_id(0)); }
1171 TryStatement(Zone* zone, Block* try_block, int pos)
1172 : Statement(zone, pos),
1173 try_block_(try_block),
1174 base_id_(BailoutId::None().ToInt()) {}
1175 static int parent_num_ids() { return 0; }
1177 int base_id() const {
1178 DCHECK(!BailoutId(base_id_).IsNone());
1183 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
1190 class TryCatchStatement final : public TryStatement {
1192 DECLARE_NODE_TYPE(TryCatchStatement)
1194 Scope* scope() { return scope_; }
1195 Variable* variable() { return variable_; }
1196 Block* catch_block() const { return catch_block_; }
1199 TryCatchStatement(Zone* zone, Block* try_block, Scope* scope,
1200 Variable* variable, Block* catch_block, int pos)
1201 : TryStatement(zone, try_block, pos),
1203 variable_(variable),
1204 catch_block_(catch_block) {}
1208 Variable* variable_;
1209 Block* catch_block_;
1213 class TryFinallyStatement final : public TryStatement {
1215 DECLARE_NODE_TYPE(TryFinallyStatement)
1217 Block* finally_block() const { return finally_block_; }
1220 TryFinallyStatement(Zone* zone, Block* try_block, Block* finally_block,
1222 : TryStatement(zone, try_block, pos), finally_block_(finally_block) {}
1225 Block* finally_block_;
1229 class DebuggerStatement final : public Statement {
1231 DECLARE_NODE_TYPE(DebuggerStatement)
1233 void set_base_id(int id) { base_id_ = id; }
1234 static int num_ids() { return parent_num_ids() + 1; }
1235 BailoutId DebugBreakId() const { return BailoutId(local_id(0)); }
1238 explicit DebuggerStatement(Zone* zone, int pos)
1239 : Statement(zone, pos), base_id_(BailoutId::None().ToInt()) {}
1240 static int parent_num_ids() { return 0; }
1242 int base_id() const {
1243 DCHECK(!BailoutId(base_id_).IsNone());
1248 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
1254 class EmptyStatement final : public Statement {
1256 DECLARE_NODE_TYPE(EmptyStatement)
1259 explicit EmptyStatement(Zone* zone, int pos): Statement(zone, pos) {}
1263 class Literal final : public Expression {
1265 DECLARE_NODE_TYPE(Literal)
1267 bool IsPropertyName() const override { return value_->IsPropertyName(); }
1269 Handle<String> AsPropertyName() {
1270 DCHECK(IsPropertyName());
1271 return Handle<String>::cast(value());
1274 const AstRawString* AsRawPropertyName() {
1275 DCHECK(IsPropertyName());
1276 return value_->AsString();
1279 bool ToBooleanIsTrue() const override { return value()->BooleanValue(); }
1280 bool ToBooleanIsFalse() const override { return !value()->BooleanValue(); }
1282 Handle<Object> value() const { return value_->value(); }
1283 const AstValue* raw_value() const { return value_; }
1285 // Support for using Literal as a HashMap key. NOTE: Currently, this works
1286 // only for string and number literals!
1288 static bool Match(void* literal1, void* literal2);
1290 static int num_ids() { return parent_num_ids() + 1; }
1291 TypeFeedbackId LiteralFeedbackId() const {
1292 return TypeFeedbackId(local_id(0));
1296 Literal(Zone* zone, const AstValue* value, int position)
1297 : Expression(zone, position), value_(value) {}
1298 static int parent_num_ids() { return Expression::num_ids(); }
1301 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
1303 const AstValue* value_;
1307 // Base class for literals that needs space in the corresponding JSFunction.
1308 class MaterializedLiteral : public Expression {
1310 virtual MaterializedLiteral* AsMaterializedLiteral() { return this; }
1312 int literal_index() { return literal_index_; }
1315 // only callable after initialization.
1316 DCHECK(depth_ >= 1);
1320 bool is_strong() const { return is_strong_; }
1323 MaterializedLiteral(Zone* zone, int literal_index, bool is_strong, int pos)
1324 : Expression(zone, pos),
1325 literal_index_(literal_index),
1327 is_strong_(is_strong),
1330 // A materialized literal is simple if the values consist of only
1331 // constants and simple object and array literals.
1332 bool is_simple() const { return is_simple_; }
1333 void set_is_simple(bool is_simple) { is_simple_ = is_simple; }
1334 friend class CompileTimeValue;
1336 void set_depth(int depth) {
1341 // Populate the constant properties/elements fixed array.
1342 void BuildConstants(Isolate* isolate);
1343 friend class ArrayLiteral;
1344 friend class ObjectLiteral;
1346 // If the expression is a literal, return the literal value;
1347 // if the expression is a materialized literal and is simple return a
1348 // compile time value as encoded by CompileTimeValue::GetValue().
1349 // Otherwise, return undefined literal as the placeholder
1350 // in the object literal boilerplate.
1351 Handle<Object> GetBoilerplateValue(Expression* expression, Isolate* isolate);
1361 // Property is used for passing information
1362 // about an object literal's properties from the parser
1363 // to the code generator.
1364 class ObjectLiteralProperty final : public ZoneObject {
1367 CONSTANT, // Property with constant value (compile time).
1368 COMPUTED, // Property with computed value (execution time).
1369 MATERIALIZED_LITERAL, // Property value is a materialized literal.
1370 GETTER, SETTER, // Property is an accessor function.
1371 PROTOTYPE // Property is __proto__.
1374 Expression* key() { return key_; }
1375 Expression* value() { return value_; }
1376 Kind kind() { return kind_; }
1378 // Type feedback information.
1379 bool IsMonomorphic() { return !receiver_type_.is_null(); }
1380 Handle<Map> GetReceiverType() { return receiver_type_; }
1382 bool IsCompileTimeValue();
1384 void set_emit_store(bool emit_store);
1387 bool is_static() const { return is_static_; }
1388 bool is_computed_name() const { return is_computed_name_; }
1390 void set_receiver_type(Handle<Map> map) { receiver_type_ = map; }
1393 friend class AstNodeFactory;
1395 ObjectLiteralProperty(Expression* key, Expression* value, Kind kind,
1396 bool is_static, bool is_computed_name);
1397 ObjectLiteralProperty(AstValueFactory* ast_value_factory, Expression* key,
1398 Expression* value, bool is_static,
1399 bool is_computed_name);
1407 bool is_computed_name_;
1408 Handle<Map> receiver_type_;
1412 // An object literal has a boilerplate object that is used
1413 // for minimizing the work when constructing it at runtime.
1414 class ObjectLiteral final : public MaterializedLiteral {
1416 typedef ObjectLiteralProperty Property;
1418 DECLARE_NODE_TYPE(ObjectLiteral)
1420 Handle<FixedArray> constant_properties() const {
1421 return constant_properties_;
1423 int properties_count() const { return constant_properties_->length() / 2; }
1424 ZoneList<Property*>* properties() const { return properties_; }
1425 bool fast_elements() const { return fast_elements_; }
1426 bool may_store_doubles() const { return may_store_doubles_; }
1427 bool has_function() const { return has_function_; }
1428 bool has_elements() const { return has_elements_; }
1430 // Decide if a property should be in the object boilerplate.
1431 static bool IsBoilerplateProperty(Property* property);
1433 // Populate the constant properties fixed array.
1434 void BuildConstantProperties(Isolate* isolate);
1436 // Mark all computed expressions that are bound to a key that
1437 // is shadowed by a later occurrence of the same key. For the
1438 // marked expressions, no store code is emitted.
1439 void CalculateEmitStore(Zone* zone);
1441 // Assemble bitfield of flags for the CreateObjectLiteral helper.
1442 int ComputeFlags(bool disable_mementos = false) const {
1443 int flags = fast_elements() ? kFastElements : kNoFlags;
1444 flags |= has_function() ? kHasFunction : kNoFlags;
1445 if (depth() == 1 && !has_elements() && !may_store_doubles()) {
1446 flags |= kShallowProperties;
1448 if (disable_mementos) {
1449 flags |= kDisableMementos;
1460 kHasFunction = 1 << 1,
1461 kShallowProperties = 1 << 2,
1462 kDisableMementos = 1 << 3,
1466 struct Accessors: public ZoneObject {
1467 Accessors() : getter(NULL), setter(NULL) {}
1472 BailoutId CreateLiteralId() const { return BailoutId(local_id(0)); }
1474 // Return an AST id for a property that is used in simulate instructions.
1475 BailoutId GetIdForProperty(int i) { return BailoutId(local_id(i + 1)); }
1477 // Unlike other AST nodes, this number of bailout IDs allocated for an
1478 // ObjectLiteral can vary, so num_ids() is not a static method.
1479 int num_ids() const { return parent_num_ids() + 1 + properties()->length(); }
1481 // Object literals need one feedback slot for each non-trivial value, as well
1482 // as some slots for home objects.
1483 FeedbackVectorRequirements ComputeFeedbackRequirements(
1484 Isolate* isolate, const ICSlotCache* cache) override;
1485 void SetFirstFeedbackICSlot(FeedbackVectorICSlot slot,
1486 ICSlotCache* cache) override {
1489 Code::Kind FeedbackICSlotKind(int index) override { return Code::STORE_IC; }
1490 FeedbackVectorICSlot GetNthSlot(int n) const {
1491 return FeedbackVectorICSlot(slot_.ToInt() + n);
1494 // If value needs a home object, returns a valid feedback vector ic slot
1495 // given by slot_index, and increments slot_index.
1496 FeedbackVectorICSlot SlotForHomeObject(Expression* value,
1497 int* slot_index) const;
1500 int slot_count() const { return slot_count_; }
1504 ObjectLiteral(Zone* zone, ZoneList<Property*>* properties, int literal_index,
1505 int boilerplate_properties, bool has_function, bool is_strong,
1507 : MaterializedLiteral(zone, literal_index, is_strong, pos),
1508 properties_(properties),
1509 boilerplate_properties_(boilerplate_properties),
1510 fast_elements_(false),
1511 has_elements_(false),
1512 may_store_doubles_(false),
1513 has_function_(has_function),
1517 slot_(FeedbackVectorICSlot::Invalid()) {
1519 static int parent_num_ids() { return MaterializedLiteral::num_ids(); }
1522 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
1523 Handle<FixedArray> constant_properties_;
1524 ZoneList<Property*>* properties_;
1525 int boilerplate_properties_;
1526 bool fast_elements_;
1528 bool may_store_doubles_;
1531 // slot_count_ helps validate that the logic to allocate ic slots and the
1532 // logic to use them are in sync.
1535 FeedbackVectorICSlot slot_;
1539 // Node for capturing a regexp literal.
1540 class RegExpLiteral final : public MaterializedLiteral {
1542 DECLARE_NODE_TYPE(RegExpLiteral)
1544 Handle<String> pattern() const { return pattern_->string(); }
1545 Handle<String> flags() const { return flags_->string(); }
1548 RegExpLiteral(Zone* zone, const AstRawString* pattern,
1549 const AstRawString* flags, int literal_index, bool is_strong,
1551 : MaterializedLiteral(zone, literal_index, is_strong, pos),
1558 const AstRawString* pattern_;
1559 const AstRawString* flags_;
1563 // An array literal has a literals object that is used
1564 // for minimizing the work when constructing it at runtime.
1565 class ArrayLiteral final : public MaterializedLiteral {
1567 DECLARE_NODE_TYPE(ArrayLiteral)
1569 Handle<FixedArray> constant_elements() const { return constant_elements_; }
1570 ElementsKind constant_elements_kind() const {
1571 DCHECK_EQ(2, constant_elements_->length());
1572 return static_cast<ElementsKind>(
1573 Smi::cast(constant_elements_->get(0))->value());
1576 ZoneList<Expression*>* values() const { return values_; }
1578 BailoutId CreateLiteralId() const { return BailoutId(local_id(0)); }
1580 // Return an AST id for an element that is used in simulate instructions.
1581 BailoutId GetIdForElement(int i) { return BailoutId(local_id(i + 1)); }
1583 // Unlike other AST nodes, this number of bailout IDs allocated for an
1584 // ArrayLiteral can vary, so num_ids() is not a static method.
1585 int num_ids() const { return parent_num_ids() + 1 + values()->length(); }
1587 // Populate the constant elements fixed array.
1588 void BuildConstantElements(Isolate* isolate);
1590 // Assemble bitfield of flags for the CreateArrayLiteral helper.
1591 int ComputeFlags(bool disable_mementos = false) const {
1592 int flags = depth() == 1 ? kShallowElements : kNoFlags;
1593 if (disable_mementos) {
1594 flags |= kDisableMementos;
1604 kShallowElements = 1,
1605 kDisableMementos = 1 << 1,
1610 ArrayLiteral(Zone* zone, ZoneList<Expression*>* values, int literal_index,
1611 bool is_strong, int pos)
1612 : MaterializedLiteral(zone, literal_index, is_strong, pos),
1614 static int parent_num_ids() { return MaterializedLiteral::num_ids(); }
1617 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
1619 Handle<FixedArray> constant_elements_;
1620 ZoneList<Expression*>* values_;
1624 class VariableProxy final : public Expression {
1626 DECLARE_NODE_TYPE(VariableProxy)
1628 bool IsValidReferenceExpression() const override { return !is_this(); }
1630 bool IsArguments() const { return is_resolved() && var()->is_arguments(); }
1632 Handle<String> name() const { return raw_name()->string(); }
1633 const AstRawString* raw_name() const {
1634 return is_resolved() ? var_->raw_name() : raw_name_;
1637 Variable* var() const {
1638 DCHECK(is_resolved());
1641 void set_var(Variable* v) {
1642 DCHECK(!is_resolved());
1647 bool is_this() const { return IsThisField::decode(bit_field_); }
1649 bool is_assigned() const { return IsAssignedField::decode(bit_field_); }
1650 void set_is_assigned() {
1651 bit_field_ = IsAssignedField::update(bit_field_, true);
1654 bool is_resolved() const { return IsResolvedField::decode(bit_field_); }
1655 void set_is_resolved() {
1656 bit_field_ = IsResolvedField::update(bit_field_, true);
1659 int end_position() const { return end_position_; }
1661 // Bind this proxy to the variable var.
1662 void BindTo(Variable* var);
1664 bool UsesVariableFeedbackSlot() const {
1665 return var()->IsUnallocated() || var()->IsLookupSlot();
1668 virtual FeedbackVectorRequirements ComputeFeedbackRequirements(
1669 Isolate* isolate, const ICSlotCache* cache) override;
1671 void SetFirstFeedbackICSlot(FeedbackVectorICSlot slot,
1672 ICSlotCache* cache) override;
1673 Code::Kind FeedbackICSlotKind(int index) override { return Code::LOAD_IC; }
1674 FeedbackVectorICSlot VariableFeedbackSlot() {
1675 DCHECK(!UsesVariableFeedbackSlot() || !variable_feedback_slot_.IsInvalid());
1676 return variable_feedback_slot_;
1679 static int num_ids() { return parent_num_ids() + 1; }
1680 BailoutId BeforeId() const { return BailoutId(local_id(0)); }
1683 VariableProxy(Zone* zone, Variable* var, int start_position,
1686 VariableProxy(Zone* zone, const AstRawString* name,
1687 Variable::Kind variable_kind, int start_position,
1689 static int parent_num_ids() { return Expression::num_ids(); }
1690 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
1692 class IsThisField : public BitField8<bool, 0, 1> {};
1693 class IsAssignedField : public BitField8<bool, 1, 1> {};
1694 class IsResolvedField : public BitField8<bool, 2, 1> {};
1696 // Start with 16-bit (or smaller) field, which should get packed together
1697 // with Expression's trailing 16-bit field.
1699 FeedbackVectorICSlot variable_feedback_slot_;
1701 const AstRawString* raw_name_; // if !is_resolved_
1702 Variable* var_; // if is_resolved_
1704 // Position is stored in the AstNode superclass, but VariableProxy needs to
1705 // know its end position too (for error messages). It cannot be inferred from
1706 // the variable name length because it can contain escapes.
1711 // Left-hand side can only be a property, a global or a (parameter or local)
1717 NAMED_SUPER_PROPERTY,
1718 KEYED_SUPER_PROPERTY
1722 class Property final : public Expression {
1724 DECLARE_NODE_TYPE(Property)
1726 bool IsValidReferenceExpression() const override { return true; }
1728 Expression* obj() const { return obj_; }
1729 Expression* key() const { return key_; }
1731 static int num_ids() { return parent_num_ids() + 1; }
1732 BailoutId LoadId() const { return BailoutId(local_id(0)); }
1734 bool IsStringAccess() const {
1735 return IsStringAccessField::decode(bit_field_);
1738 // Type feedback information.
1739 bool IsMonomorphic() override { return receiver_types_.length() == 1; }
1740 SmallMapList* GetReceiverTypes() override { return &receiver_types_; }
1741 KeyedAccessStoreMode GetStoreMode() const override { return STANDARD_STORE; }
1742 IcCheckType GetKeyType() const override {
1743 return KeyTypeField::decode(bit_field_);
1745 bool IsUninitialized() const {
1746 return !is_for_call() && HasNoTypeInformation();
1748 bool HasNoTypeInformation() const {
1749 return GetInlineCacheState() == UNINITIALIZED;
1751 InlineCacheState GetInlineCacheState() const {
1752 return InlineCacheStateField::decode(bit_field_);
1754 void set_is_string_access(bool b) {
1755 bit_field_ = IsStringAccessField::update(bit_field_, b);
1757 void set_key_type(IcCheckType key_type) {
1758 bit_field_ = KeyTypeField::update(bit_field_, key_type);
1760 void set_inline_cache_state(InlineCacheState state) {
1761 bit_field_ = InlineCacheStateField::update(bit_field_, state);
1763 void mark_for_call() {
1764 bit_field_ = IsForCallField::update(bit_field_, true);
1766 bool is_for_call() const { return IsForCallField::decode(bit_field_); }
1768 bool IsSuperAccess() { return obj()->IsSuperPropertyReference(); }
1770 virtual FeedbackVectorRequirements ComputeFeedbackRequirements(
1771 Isolate* isolate, const ICSlotCache* cache) override {
1772 return FeedbackVectorRequirements(0, 1);
1774 void SetFirstFeedbackICSlot(FeedbackVectorICSlot slot,
1775 ICSlotCache* cache) override {
1776 property_feedback_slot_ = slot;
1778 Code::Kind FeedbackICSlotKind(int index) override {
1779 return key()->IsPropertyName() ? Code::LOAD_IC : Code::KEYED_LOAD_IC;
1782 FeedbackVectorICSlot PropertyFeedbackSlot() const {
1783 DCHECK(!property_feedback_slot_.IsInvalid());
1784 return property_feedback_slot_;
1787 static LhsKind GetAssignType(Property* property) {
1788 if (property == NULL) return VARIABLE;
1789 bool super_access = property->IsSuperAccess();
1790 return (property->key()->IsPropertyName())
1791 ? (super_access ? NAMED_SUPER_PROPERTY : NAMED_PROPERTY)
1792 : (super_access ? KEYED_SUPER_PROPERTY : KEYED_PROPERTY);
1796 Property(Zone* zone, Expression* obj, Expression* key, int pos)
1797 : Expression(zone, pos),
1798 bit_field_(IsForCallField::encode(false) |
1799 IsStringAccessField::encode(false) |
1800 InlineCacheStateField::encode(UNINITIALIZED)),
1801 property_feedback_slot_(FeedbackVectorICSlot::Invalid()),
1804 static int parent_num_ids() { return Expression::num_ids(); }
1807 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
1809 class IsForCallField : public BitField8<bool, 0, 1> {};
1810 class IsStringAccessField : public BitField8<bool, 1, 1> {};
1811 class KeyTypeField : public BitField8<IcCheckType, 2, 1> {};
1812 class InlineCacheStateField : public BitField8<InlineCacheState, 3, 4> {};
1814 FeedbackVectorICSlot property_feedback_slot_;
1817 SmallMapList receiver_types_;
1821 class Call final : public Expression {
1823 DECLARE_NODE_TYPE(Call)
1825 Expression* expression() const { return expression_; }
1826 ZoneList<Expression*>* arguments() const { return arguments_; }
1828 // Type feedback information.
1829 virtual FeedbackVectorRequirements ComputeFeedbackRequirements(
1830 Isolate* isolate, const ICSlotCache* cache) override;
1831 void SetFirstFeedbackICSlot(FeedbackVectorICSlot slot,
1832 ICSlotCache* cache) override {
1835 void SetFirstFeedbackSlot(FeedbackVectorSlot slot) override { slot_ = slot; }
1836 Code::Kind FeedbackICSlotKind(int index) override { return Code::CALL_IC; }
1838 FeedbackVectorSlot CallFeedbackSlot() const { return slot_; }
1840 FeedbackVectorICSlot CallFeedbackICSlot() const { return ic_slot_; }
1842 SmallMapList* GetReceiverTypes() override {
1843 if (expression()->IsProperty()) {
1844 return expression()->AsProperty()->GetReceiverTypes();
1849 bool IsMonomorphic() override {
1850 if (expression()->IsProperty()) {
1851 return expression()->AsProperty()->IsMonomorphic();
1853 return !target_.is_null();
1856 bool global_call() const {
1857 VariableProxy* proxy = expression_->AsVariableProxy();
1858 return proxy != NULL && proxy->var()->IsUnallocated();
1861 bool known_global_function() const {
1862 return global_call() && !target_.is_null();
1865 Handle<JSFunction> target() { return target_; }
1867 Handle<AllocationSite> allocation_site() { return allocation_site_; }
1869 void SetKnownGlobalTarget(Handle<JSFunction> target) {
1871 set_is_uninitialized(false);
1873 void set_target(Handle<JSFunction> target) { target_ = target; }
1874 void set_allocation_site(Handle<AllocationSite> site) {
1875 allocation_site_ = site;
1878 static int num_ids() { return parent_num_ids() + 2; }
1879 BailoutId ReturnId() const { return BailoutId(local_id(0)); }
1880 BailoutId EvalOrLookupId() const { return BailoutId(local_id(1)); }
1882 bool is_uninitialized() const {
1883 return IsUninitializedField::decode(bit_field_);
1885 void set_is_uninitialized(bool b) {
1886 bit_field_ = IsUninitializedField::update(bit_field_, b);
1898 // Helpers to determine how to handle the call.
1899 CallType GetCallType(Isolate* isolate) const;
1900 bool IsUsingCallFeedbackSlot(Isolate* isolate) const;
1901 bool IsUsingCallFeedbackICSlot(Isolate* isolate) const;
1904 // Used to assert that the FullCodeGenerator records the return site.
1905 bool return_is_recorded_;
1909 Call(Zone* zone, Expression* expression, ZoneList<Expression*>* arguments,
1911 : Expression(zone, pos),
1912 ic_slot_(FeedbackVectorICSlot::Invalid()),
1913 slot_(FeedbackVectorSlot::Invalid()),
1914 expression_(expression),
1915 arguments_(arguments),
1916 bit_field_(IsUninitializedField::encode(false)) {
1917 if (expression->IsProperty()) {
1918 expression->AsProperty()->mark_for_call();
1921 static int parent_num_ids() { return Expression::num_ids(); }
1924 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
1926 FeedbackVectorICSlot ic_slot_;
1927 FeedbackVectorSlot slot_;
1928 Expression* expression_;
1929 ZoneList<Expression*>* arguments_;
1930 Handle<JSFunction> target_;
1931 Handle<AllocationSite> allocation_site_;
1932 class IsUninitializedField : public BitField8<bool, 0, 1> {};
1937 class CallNew final : public Expression {
1939 DECLARE_NODE_TYPE(CallNew)
1941 Expression* expression() const { return expression_; }
1942 ZoneList<Expression*>* arguments() const { return arguments_; }
1944 // Type feedback information.
1945 virtual FeedbackVectorRequirements ComputeFeedbackRequirements(
1946 Isolate* isolate, const ICSlotCache* cache) override {
1947 return FeedbackVectorRequirements(FLAG_pretenuring_call_new ? 2 : 1, 0);
1949 void SetFirstFeedbackSlot(FeedbackVectorSlot slot) override {
1950 callnew_feedback_slot_ = slot;
1953 FeedbackVectorSlot CallNewFeedbackSlot() {
1954 DCHECK(!callnew_feedback_slot_.IsInvalid());
1955 return callnew_feedback_slot_;
1957 FeedbackVectorSlot AllocationSiteFeedbackSlot() {
1958 DCHECK(FLAG_pretenuring_call_new);
1959 return CallNewFeedbackSlot().next();
1962 bool IsMonomorphic() override { return is_monomorphic_; }
1963 Handle<JSFunction> target() const { return target_; }
1964 Handle<AllocationSite> allocation_site() const {
1965 return allocation_site_;
1968 static int num_ids() { return parent_num_ids() + 1; }
1969 static int feedback_slots() { return 1; }
1970 BailoutId ReturnId() const { return BailoutId(local_id(0)); }
1972 void set_allocation_site(Handle<AllocationSite> site) {
1973 allocation_site_ = site;
1975 void set_is_monomorphic(bool monomorphic) { is_monomorphic_ = monomorphic; }
1976 void set_target(Handle<JSFunction> target) { target_ = target; }
1977 void SetKnownGlobalTarget(Handle<JSFunction> target) {
1979 is_monomorphic_ = true;
1983 CallNew(Zone* zone, Expression* expression, ZoneList<Expression*>* arguments,
1985 : Expression(zone, pos),
1986 expression_(expression),
1987 arguments_(arguments),
1988 is_monomorphic_(false),
1989 callnew_feedback_slot_(FeedbackVectorSlot::Invalid()) {}
1991 static int parent_num_ids() { return Expression::num_ids(); }
1994 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
1996 Expression* expression_;
1997 ZoneList<Expression*>* arguments_;
1998 bool is_monomorphic_;
1999 Handle<JSFunction> target_;
2000 Handle<AllocationSite> allocation_site_;
2001 FeedbackVectorSlot callnew_feedback_slot_;
2005 // The CallRuntime class does not represent any official JavaScript
2006 // language construct. Instead it is used to call a C or JS function
2007 // with a set of arguments. This is used from the builtins that are
2008 // implemented in JavaScript (see "v8natives.js").
2009 class CallRuntime final : public Expression {
2011 DECLARE_NODE_TYPE(CallRuntime)
2013 Handle<String> name() const { return raw_name_->string(); }
2014 const AstRawString* raw_name() const { return raw_name_; }
2015 const Runtime::Function* function() const { return function_; }
2016 ZoneList<Expression*>* arguments() const { return arguments_; }
2017 bool is_jsruntime() const { return function_ == NULL; }
2019 // Type feedback information.
2020 bool HasCallRuntimeFeedbackSlot() const { return is_jsruntime(); }
2021 virtual FeedbackVectorRequirements ComputeFeedbackRequirements(
2022 Isolate* isolate, const ICSlotCache* cache) override {
2023 return FeedbackVectorRequirements(0, HasCallRuntimeFeedbackSlot() ? 1 : 0);
2025 void SetFirstFeedbackICSlot(FeedbackVectorICSlot slot,
2026 ICSlotCache* cache) override {
2027 callruntime_feedback_slot_ = slot;
2029 Code::Kind FeedbackICSlotKind(int index) override { return Code::LOAD_IC; }
2031 FeedbackVectorICSlot CallRuntimeFeedbackSlot() {
2032 DCHECK(!HasCallRuntimeFeedbackSlot() ||
2033 !callruntime_feedback_slot_.IsInvalid());
2034 return callruntime_feedback_slot_;
2037 static int num_ids() { return parent_num_ids(); }
2040 CallRuntime(Zone* zone, const AstRawString* name,
2041 const Runtime::Function* function,
2042 ZoneList<Expression*>* arguments, int pos)
2043 : Expression(zone, pos),
2045 function_(function),
2046 arguments_(arguments),
2047 callruntime_feedback_slot_(FeedbackVectorICSlot::Invalid()) {}
2048 static int parent_num_ids() { return Expression::num_ids(); }
2051 const AstRawString* raw_name_;
2052 const Runtime::Function* function_;
2053 ZoneList<Expression*>* arguments_;
2054 FeedbackVectorICSlot callruntime_feedback_slot_;
2058 class UnaryOperation final : public Expression {
2060 DECLARE_NODE_TYPE(UnaryOperation)
2062 Token::Value op() const { return op_; }
2063 Expression* expression() const { return expression_; }
2065 // For unary not (Token::NOT), the AST ids where true and false will
2066 // actually be materialized, respectively.
2067 static int num_ids() { return parent_num_ids() + 2; }
2068 BailoutId MaterializeTrueId() const { return BailoutId(local_id(0)); }
2069 BailoutId MaterializeFalseId() const { return BailoutId(local_id(1)); }
2071 virtual void RecordToBooleanTypeFeedback(TypeFeedbackOracle* oracle) override;
2074 UnaryOperation(Zone* zone, Token::Value op, Expression* expression, int pos)
2075 : Expression(zone, pos), op_(op), expression_(expression) {
2076 DCHECK(Token::IsUnaryOp(op));
2078 static int parent_num_ids() { return Expression::num_ids(); }
2081 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
2084 Expression* expression_;
2088 class BinaryOperation final : public Expression {
2090 DECLARE_NODE_TYPE(BinaryOperation)
2092 Token::Value op() const { return static_cast<Token::Value>(op_); }
2093 Expression* left() const { return left_; }
2094 Expression* right() const { return right_; }
2095 Handle<AllocationSite> allocation_site() const { return allocation_site_; }
2096 void set_allocation_site(Handle<AllocationSite> allocation_site) {
2097 allocation_site_ = allocation_site;
2100 // The short-circuit logical operations need an AST ID for their
2101 // right-hand subexpression.
2102 static int num_ids() { return parent_num_ids() + 2; }
2103 BailoutId RightId() const { return BailoutId(local_id(0)); }
2105 TypeFeedbackId BinaryOperationFeedbackId() const {
2106 return TypeFeedbackId(local_id(1));
2108 Maybe<int> fixed_right_arg() const {
2109 return has_fixed_right_arg_ ? Just(fixed_right_arg_value_) : Nothing<int>();
2111 void set_fixed_right_arg(Maybe<int> arg) {
2112 has_fixed_right_arg_ = arg.IsJust();
2113 if (arg.IsJust()) fixed_right_arg_value_ = arg.FromJust();
2116 virtual void RecordToBooleanTypeFeedback(TypeFeedbackOracle* oracle) override;
2119 BinaryOperation(Zone* zone, Token::Value op, Expression* left,
2120 Expression* right, int pos)
2121 : Expression(zone, pos),
2122 op_(static_cast<byte>(op)),
2123 has_fixed_right_arg_(false),
2124 fixed_right_arg_value_(0),
2127 DCHECK(Token::IsBinaryOp(op));
2129 static int parent_num_ids() { return Expression::num_ids(); }
2132 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
2134 const byte op_; // actually Token::Value
2135 // TODO(rossberg): the fixed arg should probably be represented as a Constant
2136 // type for the RHS. Currenty it's actually a Maybe<int>
2137 bool has_fixed_right_arg_;
2138 int fixed_right_arg_value_;
2141 Handle<AllocationSite> allocation_site_;
2145 class CountOperation final : public Expression {
2147 DECLARE_NODE_TYPE(CountOperation)
2149 bool is_prefix() const { return IsPrefixField::decode(bit_field_); }
2150 bool is_postfix() const { return !is_prefix(); }
2152 Token::Value op() const { return TokenField::decode(bit_field_); }
2153 Token::Value binary_op() {
2154 return (op() == Token::INC) ? Token::ADD : Token::SUB;
2157 Expression* expression() const { return expression_; }
2159 bool IsMonomorphic() override { return receiver_types_.length() == 1; }
2160 SmallMapList* GetReceiverTypes() override { return &receiver_types_; }
2161 IcCheckType GetKeyType() const override {
2162 return KeyTypeField::decode(bit_field_);
2164 KeyedAccessStoreMode GetStoreMode() const override {
2165 return StoreModeField::decode(bit_field_);
2167 Type* type() const { return type_; }
2168 void set_key_type(IcCheckType type) {
2169 bit_field_ = KeyTypeField::update(bit_field_, type);
2171 void set_store_mode(KeyedAccessStoreMode mode) {
2172 bit_field_ = StoreModeField::update(bit_field_, mode);
2174 void set_type(Type* type) { type_ = type; }
2176 static int num_ids() { return parent_num_ids() + 4; }
2177 BailoutId AssignmentId() const { return BailoutId(local_id(0)); }
2178 BailoutId ToNumberId() const { return BailoutId(local_id(1)); }
2179 TypeFeedbackId CountBinOpFeedbackId() const {
2180 return TypeFeedbackId(local_id(2));
2182 TypeFeedbackId CountStoreFeedbackId() const {
2183 return TypeFeedbackId(local_id(3));
2186 FeedbackVectorRequirements ComputeFeedbackRequirements(
2187 Isolate* isolate, const ICSlotCache* cache) override;
2188 void SetFirstFeedbackICSlot(FeedbackVectorICSlot slot,
2189 ICSlotCache* cache) override {
2192 Code::Kind FeedbackICSlotKind(int index) override;
2193 FeedbackVectorICSlot CountSlot() const { return slot_; }
2196 CountOperation(Zone* zone, Token::Value op, bool is_prefix, Expression* expr,
2198 : Expression(zone, pos),
2200 IsPrefixField::encode(is_prefix) | KeyTypeField::encode(ELEMENT) |
2201 StoreModeField::encode(STANDARD_STORE) | TokenField::encode(op)),
2204 slot_(FeedbackVectorICSlot::Invalid()) {}
2205 static int parent_num_ids() { return Expression::num_ids(); }
2208 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
2210 class IsPrefixField : public BitField16<bool, 0, 1> {};
2211 class KeyTypeField : public BitField16<IcCheckType, 1, 1> {};
2212 class StoreModeField : public BitField16<KeyedAccessStoreMode, 2, 4> {};
2213 class TokenField : public BitField16<Token::Value, 6, 8> {};
2215 // Starts with 16-bit field, which should get packed together with
2216 // Expression's trailing 16-bit field.
2217 uint16_t bit_field_;
2219 Expression* expression_;
2220 SmallMapList receiver_types_;
2221 FeedbackVectorICSlot slot_;
2225 class CompareOperation final : public Expression {
2227 DECLARE_NODE_TYPE(CompareOperation)
2229 Token::Value op() const { return op_; }
2230 Expression* left() const { return left_; }
2231 Expression* right() const { return right_; }
2233 // Type feedback information.
2234 static int num_ids() { return parent_num_ids() + 1; }
2235 TypeFeedbackId CompareOperationFeedbackId() const {
2236 return TypeFeedbackId(local_id(0));
2238 Type* combined_type() const { return combined_type_; }
2239 void set_combined_type(Type* type) { combined_type_ = type; }
2241 // Match special cases.
2242 bool IsLiteralCompareTypeof(Expression** expr, Handle<String>* check);
2243 bool IsLiteralCompareUndefined(Expression** expr, Isolate* isolate);
2244 bool IsLiteralCompareNull(Expression** expr);
2247 CompareOperation(Zone* zone, Token::Value op, Expression* left,
2248 Expression* right, int pos)
2249 : Expression(zone, pos),
2253 combined_type_(Type::None(zone)) {
2254 DCHECK(Token::IsCompareOp(op));
2256 static int parent_num_ids() { return Expression::num_ids(); }
2259 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
2265 Type* combined_type_;
2269 class Spread final : public Expression {
2271 DECLARE_NODE_TYPE(Spread)
2273 Expression* expression() const { return expression_; }
2275 static int num_ids() { return parent_num_ids(); }
2278 Spread(Zone* zone, Expression* expression, int pos)
2279 : Expression(zone, pos), expression_(expression) {}
2280 static int parent_num_ids() { return Expression::num_ids(); }
2283 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
2285 Expression* expression_;
2289 class Conditional final : public Expression {
2291 DECLARE_NODE_TYPE(Conditional)
2293 Expression* condition() const { return condition_; }
2294 Expression* then_expression() const { return then_expression_; }
2295 Expression* else_expression() const { return else_expression_; }
2297 static int num_ids() { return parent_num_ids() + 2; }
2298 BailoutId ThenId() const { return BailoutId(local_id(0)); }
2299 BailoutId ElseId() const { return BailoutId(local_id(1)); }
2302 Conditional(Zone* zone, Expression* condition, Expression* then_expression,
2303 Expression* else_expression, int position)
2304 : Expression(zone, position),
2305 condition_(condition),
2306 then_expression_(then_expression),
2307 else_expression_(else_expression) {}
2308 static int parent_num_ids() { return Expression::num_ids(); }
2311 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
2313 Expression* condition_;
2314 Expression* then_expression_;
2315 Expression* else_expression_;
2319 class Assignment final : public Expression {
2321 DECLARE_NODE_TYPE(Assignment)
2323 Assignment* AsSimpleAssignment() { return !is_compound() ? this : NULL; }
2325 Token::Value binary_op() const;
2327 Token::Value op() const { return TokenField::decode(bit_field_); }
2328 Expression* target() const { return target_; }
2329 Expression* value() const { return value_; }
2330 BinaryOperation* binary_operation() const { return binary_operation_; }
2332 // This check relies on the definition order of token in token.h.
2333 bool is_compound() const { return op() > Token::ASSIGN; }
2335 static int num_ids() { return parent_num_ids() + 2; }
2336 BailoutId AssignmentId() const { return BailoutId(local_id(0)); }
2338 // Type feedback information.
2339 TypeFeedbackId AssignmentFeedbackId() { return TypeFeedbackId(local_id(1)); }
2340 bool IsMonomorphic() override { return receiver_types_.length() == 1; }
2341 bool IsUninitialized() const {
2342 return IsUninitializedField::decode(bit_field_);
2344 bool HasNoTypeInformation() {
2345 return IsUninitializedField::decode(bit_field_);
2347 SmallMapList* GetReceiverTypes() override { return &receiver_types_; }
2348 IcCheckType GetKeyType() const override {
2349 return KeyTypeField::decode(bit_field_);
2351 KeyedAccessStoreMode GetStoreMode() const override {
2352 return StoreModeField::decode(bit_field_);
2354 void set_is_uninitialized(bool b) {
2355 bit_field_ = IsUninitializedField::update(bit_field_, b);
2357 void set_key_type(IcCheckType key_type) {
2358 bit_field_ = KeyTypeField::update(bit_field_, key_type);
2360 void set_store_mode(KeyedAccessStoreMode mode) {
2361 bit_field_ = StoreModeField::update(bit_field_, mode);
2364 FeedbackVectorRequirements ComputeFeedbackRequirements(
2365 Isolate* isolate, const ICSlotCache* cache) override;
2366 void SetFirstFeedbackICSlot(FeedbackVectorICSlot slot,
2367 ICSlotCache* cache) override {
2370 Code::Kind FeedbackICSlotKind(int index) override;
2371 FeedbackVectorICSlot AssignmentSlot() const { return slot_; }
2374 Assignment(Zone* zone, Token::Value op, Expression* target, Expression* value,
2376 static int parent_num_ids() { return Expression::num_ids(); }
2379 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
2381 class IsUninitializedField : public BitField16<bool, 0, 1> {};
2382 class KeyTypeField : public BitField16<IcCheckType, 1, 1> {};
2383 class StoreModeField : public BitField16<KeyedAccessStoreMode, 2, 4> {};
2384 class TokenField : public BitField16<Token::Value, 6, 8> {};
2386 // Starts with 16-bit field, which should get packed together with
2387 // Expression's trailing 16-bit field.
2388 uint16_t bit_field_;
2389 Expression* target_;
2391 BinaryOperation* binary_operation_;
2392 SmallMapList receiver_types_;
2393 FeedbackVectorICSlot slot_;
2397 class Yield final : public Expression {
2399 DECLARE_NODE_TYPE(Yield)
2402 kInitial, // The initial yield that returns the unboxed generator object.
2403 kSuspend, // A normal yield: { value: EXPRESSION, done: false }
2404 kDelegating, // A yield*.
2405 kFinal // A return: { value: EXPRESSION, done: true }
2408 Expression* generator_object() const { return generator_object_; }
2409 Expression* expression() const { return expression_; }
2410 Kind yield_kind() const { return yield_kind_; }
2412 // Type feedback information.
2413 bool HasFeedbackSlots() const { return yield_kind() == kDelegating; }
2414 virtual FeedbackVectorRequirements ComputeFeedbackRequirements(
2415 Isolate* isolate, const ICSlotCache* cache) override {
2416 return FeedbackVectorRequirements(0, HasFeedbackSlots() ? 3 : 0);
2418 void SetFirstFeedbackICSlot(FeedbackVectorICSlot slot,
2419 ICSlotCache* cache) override {
2420 yield_first_feedback_slot_ = slot;
2422 Code::Kind FeedbackICSlotKind(int index) override {
2423 return index == 0 ? Code::KEYED_LOAD_IC : Code::LOAD_IC;
2426 FeedbackVectorICSlot KeyedLoadFeedbackSlot() {
2427 DCHECK(!HasFeedbackSlots() || !yield_first_feedback_slot_.IsInvalid());
2428 return yield_first_feedback_slot_;
2431 FeedbackVectorICSlot DoneFeedbackSlot() {
2432 return KeyedLoadFeedbackSlot().next();
2435 FeedbackVectorICSlot ValueFeedbackSlot() { return DoneFeedbackSlot().next(); }
2438 Yield(Zone* zone, Expression* generator_object, Expression* expression,
2439 Kind yield_kind, int pos)
2440 : Expression(zone, pos),
2441 generator_object_(generator_object),
2442 expression_(expression),
2443 yield_kind_(yield_kind),
2444 yield_first_feedback_slot_(FeedbackVectorICSlot::Invalid()) {}
2447 Expression* generator_object_;
2448 Expression* expression_;
2450 FeedbackVectorICSlot yield_first_feedback_slot_;
2454 class Throw final : public Expression {
2456 DECLARE_NODE_TYPE(Throw)
2458 Expression* exception() const { return exception_; }
2461 Throw(Zone* zone, Expression* exception, int pos)
2462 : Expression(zone, pos), exception_(exception) {}
2465 Expression* exception_;
2469 class FunctionLiteral final : public Expression {
2472 ANONYMOUS_EXPRESSION,
2477 enum ParameterFlag {
2478 kNoDuplicateParameters = 0,
2479 kHasDuplicateParameters = 1
2482 enum IsFunctionFlag {
2487 enum EagerCompileHint { kShouldEagerCompile, kShouldLazyCompile };
2489 enum ShouldBeUsedOnceHint { kShouldBeUsedOnce, kDontKnowIfShouldBeUsedOnce };
2491 enum ArityRestriction {
2497 DECLARE_NODE_TYPE(FunctionLiteral)
2499 Handle<String> name() const { return raw_name_->string(); }
2500 const AstRawString* raw_name() const { return raw_name_; }
2501 Scope* scope() const { return scope_; }
2502 ZoneList<Statement*>* body() const { return body_; }
2503 void set_function_token_position(int pos) { function_token_position_ = pos; }
2504 int function_token_position() const { return function_token_position_; }
2505 int start_position() const;
2506 int end_position() const;
2507 int SourceSize() const { return end_position() - start_position(); }
2508 bool is_expression() const { return IsExpression::decode(bitfield_); }
2509 bool is_anonymous() const { return IsAnonymous::decode(bitfield_); }
2510 LanguageMode language_mode() const;
2512 static bool NeedsHomeObject(Expression* expr);
2514 int materialized_literal_count() { return materialized_literal_count_; }
2515 int expected_property_count() { return expected_property_count_; }
2516 int parameter_count() { return parameter_count_; }
2518 bool AllowsLazyCompilation();
2519 bool AllowsLazyCompilationWithoutContext();
2521 Handle<String> debug_name() const {
2522 if (raw_name_ != NULL && !raw_name_->IsEmpty()) {
2523 return raw_name_->string();
2525 return inferred_name();
2528 Handle<String> inferred_name() const {
2529 if (!inferred_name_.is_null()) {
2530 DCHECK(raw_inferred_name_ == NULL);
2531 return inferred_name_;
2533 if (raw_inferred_name_ != NULL) {
2534 return raw_inferred_name_->string();
2537 return Handle<String>();
2540 // Only one of {set_inferred_name, set_raw_inferred_name} should be called.
2541 void set_inferred_name(Handle<String> inferred_name) {
2542 DCHECK(!inferred_name.is_null());
2543 inferred_name_ = inferred_name;
2544 DCHECK(raw_inferred_name_== NULL || raw_inferred_name_->IsEmpty());
2545 raw_inferred_name_ = NULL;
2548 void set_raw_inferred_name(const AstString* raw_inferred_name) {
2549 DCHECK(raw_inferred_name != NULL);
2550 raw_inferred_name_ = raw_inferred_name;
2551 DCHECK(inferred_name_.is_null());
2552 inferred_name_ = Handle<String>();
2555 bool pretenure() { return Pretenure::decode(bitfield_); }
2556 void set_pretenure() { bitfield_ |= Pretenure::encode(true); }
2558 bool has_duplicate_parameters() {
2559 return HasDuplicateParameters::decode(bitfield_);
2562 bool is_function() { return IsFunction::decode(bitfield_) == kIsFunction; }
2564 // This is used as a heuristic on when to eagerly compile a function
2565 // literal. We consider the following constructs as hints that the
2566 // function will be called immediately:
2567 // - (function() { ... })();
2568 // - var x = function() { ... }();
2569 bool should_eager_compile() const {
2570 return EagerCompileHintBit::decode(bitfield_) == kShouldEagerCompile;
2572 void set_should_eager_compile() {
2573 bitfield_ = EagerCompileHintBit::update(bitfield_, kShouldEagerCompile);
2576 // A hint that we expect this function to be called (exactly) once,
2577 // i.e. we suspect it's an initialization function.
2578 bool should_be_used_once_hint() const {
2579 return ShouldBeUsedOnceHintBit::decode(bitfield_) == kShouldBeUsedOnce;
2581 void set_should_be_used_once_hint() {
2582 bitfield_ = ShouldBeUsedOnceHintBit::update(bitfield_, kShouldBeUsedOnce);
2585 FunctionKind kind() const { return FunctionKindBits::decode(bitfield_); }
2587 int ast_node_count() { return ast_properties_.node_count(); }
2588 AstProperties::Flags* flags() { return ast_properties_.flags(); }
2589 void set_ast_properties(AstProperties* ast_properties) {
2590 ast_properties_ = *ast_properties;
2592 const ZoneFeedbackVectorSpec* feedback_vector_spec() const {
2593 return ast_properties_.get_spec();
2595 bool dont_optimize() { return dont_optimize_reason_ != kNoReason; }
2596 BailoutReason dont_optimize_reason() { return dont_optimize_reason_; }
2597 void set_dont_optimize_reason(BailoutReason reason) {
2598 dont_optimize_reason_ = reason;
2602 FunctionLiteral(Zone* zone, const AstRawString* name,
2603 AstValueFactory* ast_value_factory, Scope* scope,
2604 ZoneList<Statement*>* body, int materialized_literal_count,
2605 int expected_property_count, int parameter_count,
2606 FunctionType function_type,
2607 ParameterFlag has_duplicate_parameters,
2608 IsFunctionFlag is_function,
2609 EagerCompileHint eager_compile_hint, FunctionKind kind,
2611 : Expression(zone, position),
2615 raw_inferred_name_(ast_value_factory->empty_string()),
2616 ast_properties_(zone),
2617 dont_optimize_reason_(kNoReason),
2618 materialized_literal_count_(materialized_literal_count),
2619 expected_property_count_(expected_property_count),
2620 parameter_count_(parameter_count),
2621 function_token_position_(RelocInfo::kNoPosition) {
2622 bitfield_ = IsExpression::encode(function_type != DECLARATION) |
2623 IsAnonymous::encode(function_type == ANONYMOUS_EXPRESSION) |
2624 Pretenure::encode(false) |
2625 HasDuplicateParameters::encode(has_duplicate_parameters) |
2626 IsFunction::encode(is_function) |
2627 EagerCompileHintBit::encode(eager_compile_hint) |
2628 FunctionKindBits::encode(kind) |
2629 ShouldBeUsedOnceHintBit::encode(kDontKnowIfShouldBeUsedOnce);
2630 DCHECK(IsValidFunctionKind(kind));
2634 const AstRawString* raw_name_;
2635 Handle<String> name_;
2637 ZoneList<Statement*>* body_;
2638 const AstString* raw_inferred_name_;
2639 Handle<String> inferred_name_;
2640 AstProperties ast_properties_;
2641 BailoutReason dont_optimize_reason_;
2643 int materialized_literal_count_;
2644 int expected_property_count_;
2645 int parameter_count_;
2646 int function_token_position_;
2649 class IsExpression : public BitField<bool, 0, 1> {};
2650 class IsAnonymous : public BitField<bool, 1, 1> {};
2651 class Pretenure : public BitField<bool, 2, 1> {};
2652 class HasDuplicateParameters : public BitField<ParameterFlag, 3, 1> {};
2653 class IsFunction : public BitField<IsFunctionFlag, 4, 1> {};
2654 class EagerCompileHintBit : public BitField<EagerCompileHint, 5, 1> {};
2655 class FunctionKindBits : public BitField<FunctionKind, 6, 8> {};
2656 class ShouldBeUsedOnceHintBit : public BitField<ShouldBeUsedOnceHint, 15, 1> {
2661 class ClassLiteral final : public Expression {
2663 typedef ObjectLiteralProperty Property;
2665 DECLARE_NODE_TYPE(ClassLiteral)
2667 Handle<String> name() const { return raw_name_->string(); }
2668 const AstRawString* raw_name() const { return raw_name_; }
2669 Scope* scope() const { return scope_; }
2670 VariableProxy* class_variable_proxy() const { return class_variable_proxy_; }
2671 Expression* extends() const { return extends_; }
2672 FunctionLiteral* constructor() const { return constructor_; }
2673 ZoneList<Property*>* properties() const { return properties_; }
2674 int start_position() const { return position(); }
2675 int end_position() const { return end_position_; }
2677 BailoutId EntryId() const { return BailoutId(local_id(0)); }
2678 BailoutId DeclsId() const { return BailoutId(local_id(1)); }
2679 BailoutId ExitId() { return BailoutId(local_id(2)); }
2680 BailoutId CreateLiteralId() const { return BailoutId(local_id(3)); }
2682 // Return an AST id for a property that is used in simulate instructions.
2683 BailoutId GetIdForProperty(int i) { return BailoutId(local_id(i + 4)); }
2685 // Unlike other AST nodes, this number of bailout IDs allocated for an
2686 // ClassLiteral can vary, so num_ids() is not a static method.
2687 int num_ids() const { return parent_num_ids() + 4 + properties()->length(); }
2689 // Object literals need one feedback slot for each non-trivial value, as well
2690 // as some slots for home objects.
2691 FeedbackVectorRequirements ComputeFeedbackRequirements(
2692 Isolate* isolate, const ICSlotCache* cache) override;
2693 void SetFirstFeedbackICSlot(FeedbackVectorICSlot slot,
2694 ICSlotCache* cache) override {
2697 Code::Kind FeedbackICSlotKind(int index) override { return Code::STORE_IC; }
2698 FeedbackVectorICSlot GetNthSlot(int n) const {
2699 return FeedbackVectorICSlot(slot_.ToInt() + n);
2702 // If value needs a home object, returns a valid feedback vector ic slot
2703 // given by slot_index, and increments slot_index.
2704 FeedbackVectorICSlot SlotForHomeObject(Expression* value,
2705 int* slot_index) const;
2708 int slot_count() const { return slot_count_; }
2712 ClassLiteral(Zone* zone, const AstRawString* name, Scope* scope,
2713 VariableProxy* class_variable_proxy, Expression* extends,
2714 FunctionLiteral* constructor, ZoneList<Property*>* properties,
2715 int start_position, int end_position)
2716 : Expression(zone, start_position),
2719 class_variable_proxy_(class_variable_proxy),
2721 constructor_(constructor),
2722 properties_(properties),
2723 end_position_(end_position),
2727 slot_(FeedbackVectorICSlot::Invalid()) {
2730 static int parent_num_ids() { return Expression::num_ids(); }
2733 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
2735 const AstRawString* raw_name_;
2737 VariableProxy* class_variable_proxy_;
2738 Expression* extends_;
2739 FunctionLiteral* constructor_;
2740 ZoneList<Property*>* properties_;
2743 // slot_count_ helps validate that the logic to allocate ic slots and the
2744 // logic to use them are in sync.
2747 FeedbackVectorICSlot slot_;
2751 class NativeFunctionLiteral final : public Expression {
2753 DECLARE_NODE_TYPE(NativeFunctionLiteral)
2755 Handle<String> name() const { return name_->string(); }
2756 v8::Extension* extension() const { return extension_; }
2759 NativeFunctionLiteral(Zone* zone, const AstRawString* name,
2760 v8::Extension* extension, int pos)
2761 : Expression(zone, pos), name_(name), extension_(extension) {}
2764 const AstRawString* name_;
2765 v8::Extension* extension_;
2769 class ThisFunction final : public Expression {
2771 DECLARE_NODE_TYPE(ThisFunction)
2774 ThisFunction(Zone* zone, int pos) : Expression(zone, pos) {}
2778 class SuperPropertyReference final : public Expression {
2780 DECLARE_NODE_TYPE(SuperPropertyReference)
2782 VariableProxy* this_var() const { return this_var_; }
2783 Expression* home_object() const { return home_object_; }
2786 SuperPropertyReference(Zone* zone, VariableProxy* this_var,
2787 Expression* home_object, int pos)
2788 : Expression(zone, pos), this_var_(this_var), home_object_(home_object) {
2789 DCHECK(this_var->is_this());
2790 DCHECK(home_object->IsProperty());
2794 VariableProxy* this_var_;
2795 Expression* home_object_;
2799 class SuperCallReference final : public Expression {
2801 DECLARE_NODE_TYPE(SuperCallReference)
2803 VariableProxy* this_var() const { return this_var_; }
2804 VariableProxy* new_target_var() const { return new_target_var_; }
2805 VariableProxy* this_function_var() const { return this_function_var_; }
2808 SuperCallReference(Zone* zone, VariableProxy* this_var,
2809 VariableProxy* new_target_var,
2810 VariableProxy* this_function_var, int pos)
2811 : Expression(zone, pos),
2812 this_var_(this_var),
2813 new_target_var_(new_target_var),
2814 this_function_var_(this_function_var) {
2815 DCHECK(this_var->is_this());
2816 DCHECK(new_target_var->raw_name()->IsOneByteEqualTo("new.target"));
2817 DCHECK(this_function_var->raw_name()->IsOneByteEqualTo(".this_function"));
2821 VariableProxy* this_var_;
2822 VariableProxy* new_target_var_;
2823 VariableProxy* this_function_var_;
2827 #undef DECLARE_NODE_TYPE
2830 // ----------------------------------------------------------------------------
2831 // Regular expressions
2834 class RegExpVisitor BASE_EMBEDDED {
2836 virtual ~RegExpVisitor() { }
2837 #define MAKE_CASE(Name) \
2838 virtual void* Visit##Name(RegExp##Name*, void* data) = 0;
2839 FOR_EACH_REG_EXP_TREE_TYPE(MAKE_CASE)
2844 class RegExpTree : public ZoneObject {
2846 static const int kInfinity = kMaxInt;
2847 virtual ~RegExpTree() {}
2848 virtual void* Accept(RegExpVisitor* visitor, void* data) = 0;
2849 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
2850 RegExpNode* on_success) = 0;
2851 virtual bool IsTextElement() { return false; }
2852 virtual bool IsAnchoredAtStart() { return false; }
2853 virtual bool IsAnchoredAtEnd() { return false; }
2854 virtual int min_match() = 0;
2855 virtual int max_match() = 0;
2856 // Returns the interval of registers used for captures within this
2858 virtual Interval CaptureRegisters() { return Interval::Empty(); }
2859 virtual void AppendToText(RegExpText* text, Zone* zone);
2860 std::ostream& Print(std::ostream& os, Zone* zone); // NOLINT
2861 #define MAKE_ASTYPE(Name) \
2862 virtual RegExp##Name* As##Name(); \
2863 virtual bool Is##Name();
2864 FOR_EACH_REG_EXP_TREE_TYPE(MAKE_ASTYPE)
2869 class RegExpDisjunction final : public RegExpTree {
2871 explicit RegExpDisjunction(ZoneList<RegExpTree*>* alternatives);
2872 void* Accept(RegExpVisitor* visitor, void* data) override;
2873 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
2874 RegExpNode* on_success) override;
2875 RegExpDisjunction* AsDisjunction() override;
2876 Interval CaptureRegisters() override;
2877 bool IsDisjunction() override;
2878 bool IsAnchoredAtStart() override;
2879 bool IsAnchoredAtEnd() override;
2880 int min_match() override { return min_match_; }
2881 int max_match() override { return max_match_; }
2882 ZoneList<RegExpTree*>* alternatives() { return alternatives_; }
2884 bool SortConsecutiveAtoms(RegExpCompiler* compiler);
2885 void RationalizeConsecutiveAtoms(RegExpCompiler* compiler);
2886 void FixSingleCharacterDisjunctions(RegExpCompiler* compiler);
2887 ZoneList<RegExpTree*>* alternatives_;
2893 class RegExpAlternative final : public RegExpTree {
2895 explicit RegExpAlternative(ZoneList<RegExpTree*>* nodes);
2896 void* Accept(RegExpVisitor* visitor, void* data) override;
2897 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
2898 RegExpNode* on_success) override;
2899 RegExpAlternative* AsAlternative() override;
2900 Interval CaptureRegisters() override;
2901 bool IsAlternative() override;
2902 bool IsAnchoredAtStart() override;
2903 bool IsAnchoredAtEnd() override;
2904 int min_match() override { return min_match_; }
2905 int max_match() override { return max_match_; }
2906 ZoneList<RegExpTree*>* nodes() { return nodes_; }
2908 ZoneList<RegExpTree*>* nodes_;
2914 class RegExpAssertion final : public RegExpTree {
2916 enum AssertionType {
2924 explicit RegExpAssertion(AssertionType type) : assertion_type_(type) { }
2925 void* Accept(RegExpVisitor* visitor, void* data) override;
2926 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
2927 RegExpNode* on_success) override;
2928 RegExpAssertion* AsAssertion() override;
2929 bool IsAssertion() override;
2930 bool IsAnchoredAtStart() override;
2931 bool IsAnchoredAtEnd() override;
2932 int min_match() override { return 0; }
2933 int max_match() override { return 0; }
2934 AssertionType assertion_type() { return assertion_type_; }
2936 AssertionType assertion_type_;
2940 class CharacterSet final BASE_EMBEDDED {
2942 explicit CharacterSet(uc16 standard_set_type)
2944 standard_set_type_(standard_set_type) {}
2945 explicit CharacterSet(ZoneList<CharacterRange>* ranges)
2947 standard_set_type_(0) {}
2948 ZoneList<CharacterRange>* ranges(Zone* zone);
2949 uc16 standard_set_type() { return standard_set_type_; }
2950 void set_standard_set_type(uc16 special_set_type) {
2951 standard_set_type_ = special_set_type;
2953 bool is_standard() { return standard_set_type_ != 0; }
2954 void Canonicalize();
2956 ZoneList<CharacterRange>* ranges_;
2957 // If non-zero, the value represents a standard set (e.g., all whitespace
2958 // characters) without having to expand the ranges.
2959 uc16 standard_set_type_;
2963 class RegExpCharacterClass final : public RegExpTree {
2965 RegExpCharacterClass(ZoneList<CharacterRange>* ranges, bool is_negated)
2967 is_negated_(is_negated) { }
2968 explicit RegExpCharacterClass(uc16 type)
2970 is_negated_(false) { }
2971 void* Accept(RegExpVisitor* visitor, void* data) override;
2972 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
2973 RegExpNode* on_success) override;
2974 RegExpCharacterClass* AsCharacterClass() override;
2975 bool IsCharacterClass() override;
2976 bool IsTextElement() override { return true; }
2977 int min_match() override { return 1; }
2978 int max_match() override { return 1; }
2979 void AppendToText(RegExpText* text, Zone* zone) override;
2980 CharacterSet character_set() { return set_; }
2981 // TODO(lrn): Remove need for complex version if is_standard that
2982 // recognizes a mangled standard set and just do { return set_.is_special(); }
2983 bool is_standard(Zone* zone);
2984 // Returns a value representing the standard character set if is_standard()
2986 // Currently used values are:
2987 // s : unicode whitespace
2988 // S : unicode non-whitespace
2989 // w : ASCII word character (digit, letter, underscore)
2990 // W : non-ASCII word character
2992 // D : non-ASCII digit
2993 // . : non-unicode non-newline
2994 // * : All characters
2995 uc16 standard_type() { return set_.standard_set_type(); }
2996 ZoneList<CharacterRange>* ranges(Zone* zone) { return set_.ranges(zone); }
2997 bool is_negated() { return is_negated_; }
3005 class RegExpAtom final : public RegExpTree {
3007 explicit RegExpAtom(Vector<const uc16> data) : data_(data) { }
3008 void* Accept(RegExpVisitor* visitor, void* data) override;
3009 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
3010 RegExpNode* on_success) override;
3011 RegExpAtom* AsAtom() override;
3012 bool IsAtom() override;
3013 bool IsTextElement() override { return true; }
3014 int min_match() override { return data_.length(); }
3015 int max_match() override { return data_.length(); }
3016 void AppendToText(RegExpText* text, Zone* zone) override;
3017 Vector<const uc16> data() { return data_; }
3018 int length() { return data_.length(); }
3020 Vector<const uc16> data_;
3024 class RegExpText final : public RegExpTree {
3026 explicit RegExpText(Zone* zone) : elements_(2, zone), length_(0) {}
3027 void* Accept(RegExpVisitor* visitor, void* data) override;
3028 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
3029 RegExpNode* on_success) override;
3030 RegExpText* AsText() override;
3031 bool IsText() override;
3032 bool IsTextElement() override { return true; }
3033 int min_match() override { return length_; }
3034 int max_match() override { return length_; }
3035 void AppendToText(RegExpText* text, Zone* zone) override;
3036 void AddElement(TextElement elm, Zone* zone) {
3037 elements_.Add(elm, zone);
3038 length_ += elm.length();
3040 ZoneList<TextElement>* elements() { return &elements_; }
3042 ZoneList<TextElement> elements_;
3047 class RegExpQuantifier final : public RegExpTree {
3049 enum QuantifierType { GREEDY, NON_GREEDY, POSSESSIVE };
3050 RegExpQuantifier(int min, int max, QuantifierType type, RegExpTree* body)
3054 min_match_(min * body->min_match()),
3055 quantifier_type_(type) {
3056 if (max > 0 && body->max_match() > kInfinity / max) {
3057 max_match_ = kInfinity;
3059 max_match_ = max * body->max_match();
3062 void* Accept(RegExpVisitor* visitor, void* data) override;
3063 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
3064 RegExpNode* on_success) override;
3065 static RegExpNode* ToNode(int min,
3069 RegExpCompiler* compiler,
3070 RegExpNode* on_success,
3071 bool not_at_start = false);
3072 RegExpQuantifier* AsQuantifier() override;
3073 Interval CaptureRegisters() override;
3074 bool IsQuantifier() override;
3075 int min_match() override { return min_match_; }
3076 int max_match() override { return max_match_; }
3077 int min() { return min_; }
3078 int max() { return max_; }
3079 bool is_possessive() { return quantifier_type_ == POSSESSIVE; }
3080 bool is_non_greedy() { return quantifier_type_ == NON_GREEDY; }
3081 bool is_greedy() { return quantifier_type_ == GREEDY; }
3082 RegExpTree* body() { return body_; }
3090 QuantifierType quantifier_type_;
3094 class RegExpCapture final : public RegExpTree {
3096 explicit RegExpCapture(RegExpTree* body, int index)
3097 : body_(body), index_(index) { }
3098 void* Accept(RegExpVisitor* visitor, void* data) override;
3099 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
3100 RegExpNode* on_success) override;
3101 static RegExpNode* ToNode(RegExpTree* body,
3103 RegExpCompiler* compiler,
3104 RegExpNode* on_success);
3105 RegExpCapture* AsCapture() override;
3106 bool IsAnchoredAtStart() override;
3107 bool IsAnchoredAtEnd() override;
3108 Interval CaptureRegisters() override;
3109 bool IsCapture() override;
3110 int min_match() override { return body_->min_match(); }
3111 int max_match() override { return body_->max_match(); }
3112 RegExpTree* body() { return body_; }
3113 int index() { return index_; }
3114 static int StartRegister(int index) { return index * 2; }
3115 static int EndRegister(int index) { return index * 2 + 1; }
3123 class RegExpLookahead final : public RegExpTree {
3125 RegExpLookahead(RegExpTree* body,
3130 is_positive_(is_positive),
3131 capture_count_(capture_count),
3132 capture_from_(capture_from) { }
3134 void* Accept(RegExpVisitor* visitor, void* data) override;
3135 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
3136 RegExpNode* on_success) override;
3137 RegExpLookahead* AsLookahead() override;
3138 Interval CaptureRegisters() override;
3139 bool IsLookahead() override;
3140 bool IsAnchoredAtStart() override;
3141 int min_match() override { return 0; }
3142 int max_match() override { return 0; }
3143 RegExpTree* body() { return body_; }
3144 bool is_positive() { return is_positive_; }
3145 int capture_count() { return capture_count_; }
3146 int capture_from() { return capture_from_; }
3156 class RegExpBackReference final : public RegExpTree {
3158 explicit RegExpBackReference(RegExpCapture* capture)
3159 : capture_(capture) { }
3160 void* Accept(RegExpVisitor* visitor, void* data) override;
3161 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
3162 RegExpNode* on_success) override;
3163 RegExpBackReference* AsBackReference() override;
3164 bool IsBackReference() override;
3165 int min_match() override { return 0; }
3166 int max_match() override { return capture_->max_match(); }
3167 int index() { return capture_->index(); }
3168 RegExpCapture* capture() { return capture_; }
3170 RegExpCapture* capture_;
3174 class RegExpEmpty final : public RegExpTree {
3177 void* Accept(RegExpVisitor* visitor, void* data) override;
3178 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
3179 RegExpNode* on_success) override;
3180 RegExpEmpty* AsEmpty() override;
3181 bool IsEmpty() override;
3182 int min_match() override { return 0; }
3183 int max_match() override { return 0; }
3187 // ----------------------------------------------------------------------------
3189 // - leaf node visitors are abstract.
3191 class AstVisitor BASE_EMBEDDED {
3194 virtual ~AstVisitor() {}
3196 // Stack overflow check and dynamic dispatch.
3197 virtual void Visit(AstNode* node) = 0;
3199 // Iteration left-to-right.
3200 virtual void VisitDeclarations(ZoneList<Declaration*>* declarations);
3201 virtual void VisitStatements(ZoneList<Statement*>* statements);
3202 virtual void VisitExpressions(ZoneList<Expression*>* expressions);
3204 // Individual AST nodes.
3205 #define DEF_VISIT(type) \
3206 virtual void Visit##type(type* node) = 0;
3207 AST_NODE_LIST(DEF_VISIT)
3212 #define DEFINE_AST_VISITOR_SUBCLASS_MEMBERS() \
3214 void Visit(AstNode* node) final { \
3215 if (!CheckStackOverflow()) node->Accept(this); \
3218 void SetStackOverflow() { stack_overflow_ = true; } \
3219 void ClearStackOverflow() { stack_overflow_ = false; } \
3220 bool HasStackOverflow() const { return stack_overflow_; } \
3222 bool CheckStackOverflow() { \
3223 if (stack_overflow_) return true; \
3224 StackLimitCheck check(isolate_); \
3225 if (!check.HasOverflowed()) return false; \
3226 stack_overflow_ = true; \
3231 void InitializeAstVisitor(Isolate* isolate, Zone* zone) { \
3232 isolate_ = isolate; \
3234 stack_overflow_ = false; \
3236 Zone* zone() { return zone_; } \
3237 Isolate* isolate() { return isolate_; } \
3239 Isolate* isolate_; \
3241 bool stack_overflow_
3244 // ----------------------------------------------------------------------------
3247 class AstNodeFactory final BASE_EMBEDDED {
3249 explicit AstNodeFactory(AstValueFactory* ast_value_factory)
3250 : zone_(ast_value_factory->zone()),
3251 ast_value_factory_(ast_value_factory) {}
3253 VariableDeclaration* NewVariableDeclaration(
3254 VariableProxy* proxy, VariableMode mode, Scope* scope, int pos,
3255 bool is_class_declaration = false, int declaration_group_start = -1) {
3257 VariableDeclaration(zone_, proxy, mode, scope, pos,
3258 is_class_declaration, declaration_group_start);
3261 FunctionDeclaration* NewFunctionDeclaration(VariableProxy* proxy,
3263 FunctionLiteral* fun,
3266 return new (zone_) FunctionDeclaration(zone_, proxy, mode, fun, scope, pos);
3269 ImportDeclaration* NewImportDeclaration(VariableProxy* proxy,
3270 const AstRawString* import_name,
3271 const AstRawString* module_specifier,
3272 Scope* scope, int pos) {
3273 return new (zone_) ImportDeclaration(zone_, proxy, import_name,
3274 module_specifier, scope, pos);
3277 ExportDeclaration* NewExportDeclaration(VariableProxy* proxy,
3280 return new (zone_) ExportDeclaration(zone_, proxy, scope, pos);
3283 Block* NewBlock(ZoneList<const AstRawString*>* labels, int capacity,
3284 bool ignore_completion_value, int pos) {
3286 Block(zone_, labels, capacity, ignore_completion_value, pos);
3289 #define STATEMENT_WITH_LABELS(NodeType) \
3290 NodeType* New##NodeType(ZoneList<const AstRawString*>* labels, int pos) { \
3291 return new (zone_) NodeType(zone_, labels, pos); \
3293 STATEMENT_WITH_LABELS(DoWhileStatement)
3294 STATEMENT_WITH_LABELS(WhileStatement)
3295 STATEMENT_WITH_LABELS(ForStatement)
3296 STATEMENT_WITH_LABELS(SwitchStatement)
3297 #undef STATEMENT_WITH_LABELS
3299 ForEachStatement* NewForEachStatement(ForEachStatement::VisitMode visit_mode,
3300 ZoneList<const AstRawString*>* labels,
3302 switch (visit_mode) {
3303 case ForEachStatement::ENUMERATE: {
3304 return new (zone_) ForInStatement(zone_, labels, pos);
3306 case ForEachStatement::ITERATE: {
3307 return new (zone_) ForOfStatement(zone_, labels, pos);
3314 ExpressionStatement* NewExpressionStatement(Expression* expression, int pos) {
3315 return new (zone_) ExpressionStatement(zone_, expression, pos);
3318 ContinueStatement* NewContinueStatement(IterationStatement* target, int pos) {
3319 return new (zone_) ContinueStatement(zone_, target, pos);
3322 BreakStatement* NewBreakStatement(BreakableStatement* target, int pos) {
3323 return new (zone_) BreakStatement(zone_, target, pos);
3326 ReturnStatement* NewReturnStatement(Expression* expression, int pos) {
3327 return new (zone_) ReturnStatement(zone_, expression, pos);
3330 WithStatement* NewWithStatement(Scope* scope,
3331 Expression* expression,
3332 Statement* statement,
3334 return new (zone_) WithStatement(zone_, scope, expression, statement, pos);
3337 IfStatement* NewIfStatement(Expression* condition,
3338 Statement* then_statement,
3339 Statement* else_statement,
3342 IfStatement(zone_, condition, then_statement, else_statement, pos);
3345 TryCatchStatement* NewTryCatchStatement(Block* try_block, Scope* scope,
3347 Block* catch_block, int pos) {
3349 TryCatchStatement(zone_, try_block, scope, variable, catch_block, pos);
3352 TryFinallyStatement* NewTryFinallyStatement(Block* try_block,
3353 Block* finally_block, int pos) {
3355 TryFinallyStatement(zone_, try_block, finally_block, pos);
3358 DebuggerStatement* NewDebuggerStatement(int pos) {
3359 return new (zone_) DebuggerStatement(zone_, pos);
3362 EmptyStatement* NewEmptyStatement(int pos) {
3363 return new(zone_) EmptyStatement(zone_, pos);
3366 CaseClause* NewCaseClause(
3367 Expression* label, ZoneList<Statement*>* statements, int pos) {
3368 return new (zone_) CaseClause(zone_, label, statements, pos);
3371 Literal* NewStringLiteral(const AstRawString* string, int pos) {
3373 Literal(zone_, ast_value_factory_->NewString(string), pos);
3376 // A JavaScript symbol (ECMA-262 edition 6).
3377 Literal* NewSymbolLiteral(const char* name, int pos) {
3378 return new (zone_) Literal(zone_, ast_value_factory_->NewSymbol(name), pos);
3381 Literal* NewNumberLiteral(double number, int pos) {
3383 Literal(zone_, ast_value_factory_->NewNumber(number), pos);
3386 Literal* NewSmiLiteral(int number, int pos) {
3387 return new (zone_) Literal(zone_, ast_value_factory_->NewSmi(number), pos);
3390 Literal* NewBooleanLiteral(bool b, int pos) {
3391 return new (zone_) Literal(zone_, ast_value_factory_->NewBoolean(b), pos);
3394 Literal* NewNullLiteral(int pos) {
3395 return new (zone_) Literal(zone_, ast_value_factory_->NewNull(), pos);
3398 Literal* NewUndefinedLiteral(int pos) {
3399 return new (zone_) Literal(zone_, ast_value_factory_->NewUndefined(), pos);
3402 Literal* NewTheHoleLiteral(int pos) {
3403 return new (zone_) Literal(zone_, ast_value_factory_->NewTheHole(), pos);
3406 ObjectLiteral* NewObjectLiteral(
3407 ZoneList<ObjectLiteral::Property*>* properties,
3409 int boilerplate_properties,
3413 return new (zone_) ObjectLiteral(zone_, properties, literal_index,
3414 boilerplate_properties, has_function,
3418 ObjectLiteral::Property* NewObjectLiteralProperty(
3419 Expression* key, Expression* value, ObjectLiteralProperty::Kind kind,
3420 bool is_static, bool is_computed_name) {
3422 ObjectLiteral::Property(key, value, kind, is_static, is_computed_name);
3425 ObjectLiteral::Property* NewObjectLiteralProperty(Expression* key,
3428 bool is_computed_name) {
3429 return new (zone_) ObjectLiteral::Property(ast_value_factory_, key, value,
3430 is_static, is_computed_name);
3433 RegExpLiteral* NewRegExpLiteral(const AstRawString* pattern,
3434 const AstRawString* flags,
3438 return new (zone_) RegExpLiteral(zone_, pattern, flags, literal_index,
3442 ArrayLiteral* NewArrayLiteral(ZoneList<Expression*>* values,
3446 return new (zone_) ArrayLiteral(zone_, values, literal_index, is_strong,
3450 VariableProxy* NewVariableProxy(Variable* var,
3451 int start_position = RelocInfo::kNoPosition,
3452 int end_position = RelocInfo::kNoPosition) {
3453 return new (zone_) VariableProxy(zone_, var, start_position, end_position);
3456 VariableProxy* NewVariableProxy(const AstRawString* name,
3457 Variable::Kind variable_kind,
3458 int start_position = RelocInfo::kNoPosition,
3459 int end_position = RelocInfo::kNoPosition) {
3460 DCHECK_NOT_NULL(name);
3462 VariableProxy(zone_, name, variable_kind, start_position, end_position);
3465 Property* NewProperty(Expression* obj, Expression* key, int pos) {
3466 return new (zone_) Property(zone_, obj, key, pos);
3469 Call* NewCall(Expression* expression,
3470 ZoneList<Expression*>* arguments,
3472 return new (zone_) Call(zone_, expression, arguments, pos);
3475 CallNew* NewCallNew(Expression* expression,
3476 ZoneList<Expression*>* arguments,
3478 return new (zone_) CallNew(zone_, expression, arguments, pos);
3481 CallRuntime* NewCallRuntime(const AstRawString* name,
3482 const Runtime::Function* function,
3483 ZoneList<Expression*>* arguments,
3485 return new (zone_) CallRuntime(zone_, name, function, arguments, pos);
3488 UnaryOperation* NewUnaryOperation(Token::Value op,
3489 Expression* expression,
3491 return new (zone_) UnaryOperation(zone_, op, expression, pos);
3494 BinaryOperation* NewBinaryOperation(Token::Value op,
3498 return new (zone_) BinaryOperation(zone_, op, left, right, pos);
3501 CountOperation* NewCountOperation(Token::Value op,
3505 return new (zone_) CountOperation(zone_, op, is_prefix, expr, pos);
3508 CompareOperation* NewCompareOperation(Token::Value op,
3512 return new (zone_) CompareOperation(zone_, op, left, right, pos);
3515 Spread* NewSpread(Expression* expression, int pos) {
3516 return new (zone_) Spread(zone_, expression, pos);
3519 Conditional* NewConditional(Expression* condition,
3520 Expression* then_expression,
3521 Expression* else_expression,
3523 return new (zone_) Conditional(zone_, condition, then_expression,
3524 else_expression, position);
3527 Assignment* NewAssignment(Token::Value op,
3531 DCHECK(Token::IsAssignmentOp(op));
3532 Assignment* assign = new (zone_) Assignment(zone_, op, target, value, pos);
3533 if (assign->is_compound()) {
3534 DCHECK(Token::IsAssignmentOp(op));
3535 assign->binary_operation_ =
3536 NewBinaryOperation(assign->binary_op(), target, value, pos + 1);
3541 Yield* NewYield(Expression *generator_object,
3542 Expression* expression,
3543 Yield::Kind yield_kind,
3545 if (!expression) expression = NewUndefinedLiteral(pos);
3547 Yield(zone_, generator_object, expression, yield_kind, pos);
3550 Throw* NewThrow(Expression* exception, int pos) {
3551 return new (zone_) Throw(zone_, exception, pos);
3554 FunctionLiteral* NewFunctionLiteral(
3555 const AstRawString* name, AstValueFactory* ast_value_factory,
3556 Scope* scope, ZoneList<Statement*>* body, int materialized_literal_count,
3557 int expected_property_count, int parameter_count,
3558 FunctionLiteral::ParameterFlag has_duplicate_parameters,
3559 FunctionLiteral::FunctionType function_type,
3560 FunctionLiteral::IsFunctionFlag is_function,
3561 FunctionLiteral::EagerCompileHint eager_compile_hint, FunctionKind kind,
3563 return new (zone_) FunctionLiteral(
3564 zone_, name, ast_value_factory, scope, body, materialized_literal_count,
3565 expected_property_count, parameter_count, function_type,
3566 has_duplicate_parameters, is_function, eager_compile_hint, kind,
3570 ClassLiteral* NewClassLiteral(const AstRawString* name, Scope* scope,
3571 VariableProxy* proxy, Expression* extends,
3572 FunctionLiteral* constructor,
3573 ZoneList<ObjectLiteral::Property*>* properties,
3574 int start_position, int end_position) {
3576 ClassLiteral(zone_, name, scope, proxy, extends, constructor,
3577 properties, start_position, end_position);
3580 NativeFunctionLiteral* NewNativeFunctionLiteral(const AstRawString* name,
3581 v8::Extension* extension,
3583 return new (zone_) NativeFunctionLiteral(zone_, name, extension, pos);
3586 ThisFunction* NewThisFunction(int pos) {
3587 return new (zone_) ThisFunction(zone_, pos);
3590 SuperPropertyReference* NewSuperPropertyReference(VariableProxy* this_var,
3591 Expression* home_object,
3594 SuperPropertyReference(zone_, this_var, home_object, pos);
3597 SuperCallReference* NewSuperCallReference(VariableProxy* this_var,
3598 VariableProxy* new_target_var,
3599 VariableProxy* this_function_var,
3601 return new (zone_) SuperCallReference(zone_, this_var, new_target_var,
3602 this_function_var, pos);
3607 AstValueFactory* ast_value_factory_;
3611 } } // namespace v8::internal