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) \
93 #define AST_NODE_LIST(V) \
94 DECLARATION_NODE_LIST(V) \
95 STATEMENT_NODE_LIST(V) \
96 EXPRESSION_NODE_LIST(V)
98 // Forward declarations
103 class BreakableStatement;
105 class IterationStatement;
106 class MaterializedLiteral;
108 class TypeFeedbackOracle;
110 class RegExpAlternative;
111 class RegExpAssertion;
113 class RegExpBackReference;
115 class RegExpCharacterClass;
116 class RegExpCompiler;
117 class RegExpDisjunction;
119 class RegExpLookahead;
120 class RegExpQuantifier;
123 #define DEF_FORWARD_DECLARATION(type) class type;
124 AST_NODE_LIST(DEF_FORWARD_DECLARATION)
125 #undef DEF_FORWARD_DECLARATION
128 // Typedef only introduced to avoid unreadable code.
129 // Please do appreciate the required space in "> >".
130 typedef ZoneList<Handle<String> > ZoneStringList;
131 typedef ZoneList<Handle<Object> > ZoneObjectList;
134 #define DECLARE_NODE_TYPE(type) \
135 void Accept(AstVisitor* v) override; \
136 AstNode::NodeType node_type() const final { return AstNode::k##type; } \
137 friend class AstNodeFactory;
140 enum AstPropertiesFlag {
148 class FeedbackVectorRequirements {
150 FeedbackVectorRequirements(int slots, int ic_slots)
151 : slots_(slots), ic_slots_(ic_slots) {}
153 int slots() const { return slots_; }
154 int ic_slots() const { return ic_slots_; }
162 class VariableICSlotPair final {
164 VariableICSlotPair(Variable* variable, FeedbackVectorICSlot slot)
165 : variable_(variable), slot_(slot) {}
167 : variable_(NULL), slot_(FeedbackVectorICSlot::Invalid()) {}
169 Variable* variable() const { return variable_; }
170 FeedbackVectorICSlot slot() const { return slot_; }
174 FeedbackVectorICSlot slot_;
178 typedef List<VariableICSlotPair> ICSlotCache;
181 class AstProperties final BASE_EMBEDDED {
183 class Flags : public EnumSet<AstPropertiesFlag, int> {};
185 explicit AstProperties(Zone* zone) : node_count_(0), spec_(zone) {}
187 Flags* flags() { return &flags_; }
188 int node_count() { return node_count_; }
189 void add_node_count(int count) { node_count_ += count; }
191 int slots() const { return spec_.slots(); }
192 void increase_slots(int count) { spec_.increase_slots(count); }
194 int ic_slots() const { return spec_.ic_slots(); }
195 void increase_ic_slots(int count) { spec_.increase_ic_slots(count); }
196 void SetKind(int ic_slot, Code::Kind kind) { spec_.SetKind(ic_slot, kind); }
197 const ZoneFeedbackVectorSpec* get_spec() const { return &spec_; }
202 ZoneFeedbackVectorSpec spec_;
206 class AstNode: public ZoneObject {
208 #define DECLARE_TYPE_ENUM(type) k##type,
210 AST_NODE_LIST(DECLARE_TYPE_ENUM)
213 #undef DECLARE_TYPE_ENUM
215 void* operator new(size_t size, Zone* zone) { return zone->New(size); }
217 explicit AstNode(int position): position_(position) {}
218 virtual ~AstNode() {}
220 virtual void Accept(AstVisitor* v) = 0;
221 virtual NodeType node_type() const = 0;
222 int position() const { return position_; }
224 // Type testing & conversion functions overridden by concrete subclasses.
225 #define DECLARE_NODE_FUNCTIONS(type) \
226 bool Is##type() const { return node_type() == AstNode::k##type; } \
228 return Is##type() ? reinterpret_cast<type*>(this) : NULL; \
230 const type* As##type() const { \
231 return Is##type() ? reinterpret_cast<const type*>(this) : NULL; \
233 AST_NODE_LIST(DECLARE_NODE_FUNCTIONS)
234 #undef DECLARE_NODE_FUNCTIONS
236 virtual BreakableStatement* AsBreakableStatement() { return NULL; }
237 virtual IterationStatement* AsIterationStatement() { return NULL; }
238 virtual MaterializedLiteral* AsMaterializedLiteral() { return NULL; }
240 // The interface for feedback slots, with default no-op implementations for
241 // node types which don't actually have this. Note that this is conceptually
242 // not really nice, but multiple inheritance would introduce yet another
243 // vtable entry per node, something we don't want for space reasons.
244 virtual FeedbackVectorRequirements ComputeFeedbackRequirements(
245 Isolate* isolate, const ICSlotCache* cache) {
246 return FeedbackVectorRequirements(0, 0);
248 virtual void SetFirstFeedbackSlot(FeedbackVectorSlot slot) { UNREACHABLE(); }
249 virtual void SetFirstFeedbackICSlot(FeedbackVectorICSlot slot,
250 ICSlotCache* cache) {
253 // Each ICSlot stores a kind of IC which the participating node should know.
254 virtual Code::Kind FeedbackICSlotKind(int index) {
256 return Code::NUMBER_OF_KINDS;
260 // Hidden to prevent accidental usage. It would have to load the
261 // current zone from the TLS.
262 void* operator new(size_t size);
264 friend class CaseClause; // Generates AST IDs.
270 class Statement : public AstNode {
272 explicit Statement(Zone* zone, int position) : AstNode(position) {}
274 bool IsEmpty() { return AsEmptyStatement() != NULL; }
275 virtual bool IsJump() const { return false; }
279 class SmallMapList final {
282 SmallMapList(int capacity, Zone* zone) : list_(capacity, zone) {}
284 void Reserve(int capacity, Zone* zone) { list_.Reserve(capacity, zone); }
285 void Clear() { list_.Clear(); }
286 void Sort() { list_.Sort(); }
288 bool is_empty() const { return list_.is_empty(); }
289 int length() const { return list_.length(); }
291 void AddMapIfMissing(Handle<Map> map, Zone* zone) {
292 if (!Map::TryUpdate(map).ToHandle(&map)) return;
293 for (int i = 0; i < length(); ++i) {
294 if (at(i).is_identical_to(map)) return;
299 void FilterForPossibleTransitions(Map* root_map) {
300 for (int i = list_.length() - 1; i >= 0; i--) {
301 if (at(i)->FindRootMap() != root_map) {
302 list_.RemoveElement(list_.at(i));
307 void Add(Handle<Map> handle, Zone* zone) {
308 list_.Add(handle.location(), zone);
311 Handle<Map> at(int i) const {
312 return Handle<Map>(list_.at(i));
315 Handle<Map> first() const { return at(0); }
316 Handle<Map> last() const { return at(length() - 1); }
319 // The list stores pointers to Map*, that is Map**, so it's GC safe.
320 SmallPointerList<Map*> list_;
322 DISALLOW_COPY_AND_ASSIGN(SmallMapList);
326 class Expression : public AstNode {
329 // Not assigned a context yet, or else will not be visited during
332 // Evaluated for its side effects.
334 // Evaluated for its value (and side effects).
336 // Evaluated for control flow (and side effects).
340 virtual bool IsValidReferenceExpression() const { return false; }
342 // Helpers for ToBoolean conversion.
343 virtual bool ToBooleanIsTrue() const { return false; }
344 virtual bool ToBooleanIsFalse() const { return false; }
346 // Symbols that cannot be parsed as array indices are considered property
347 // names. We do not treat symbols that can be array indexes as property
348 // names because [] for string objects is handled only by keyed ICs.
349 virtual bool IsPropertyName() const { return false; }
351 // True iff the expression is a literal represented as a smi.
352 bool IsSmiLiteral() const;
354 // True iff the expression is a string literal.
355 bool IsStringLiteral() const;
357 // True iff the expression is the null literal.
358 bool IsNullLiteral() const;
360 // True if we can prove that the expression is the undefined literal.
361 bool IsUndefinedLiteral(Isolate* isolate) const;
363 // Expression type bounds
364 Bounds bounds() const { return bounds_; }
365 void set_bounds(Bounds bounds) { bounds_ = bounds; }
367 // Type feedback information for assignments and properties.
368 virtual bool IsMonomorphic() {
372 virtual SmallMapList* GetReceiverTypes() {
376 virtual KeyedAccessStoreMode GetStoreMode() const {
378 return STANDARD_STORE;
380 virtual IcCheckType GetKeyType() const {
385 // TODO(rossberg): this should move to its own AST node eventually.
386 virtual void RecordToBooleanTypeFeedback(TypeFeedbackOracle* oracle);
387 byte to_boolean_types() const {
388 return ToBooleanTypesField::decode(bit_field_);
391 void set_base_id(int id) { base_id_ = id; }
392 static int num_ids() { return parent_num_ids() + 2; }
393 BailoutId id() const { return BailoutId(local_id(0)); }
394 TypeFeedbackId test_id() const { return TypeFeedbackId(local_id(1)); }
397 Expression(Zone* zone, int pos)
399 base_id_(BailoutId::None().ToInt()),
400 bounds_(Bounds::Unbounded(zone)),
402 static int parent_num_ids() { return 0; }
403 void set_to_boolean_types(byte types) {
404 bit_field_ = ToBooleanTypesField::update(bit_field_, types);
407 int base_id() const {
408 DCHECK(!BailoutId(base_id_).IsNone());
413 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
417 class ToBooleanTypesField : public BitField16<byte, 0, 8> {};
419 // Ends with 16-bit field; deriving classes in turn begin with
420 // 16-bit fields for optimum packing efficiency.
424 class BreakableStatement : public Statement {
427 TARGET_FOR_ANONYMOUS,
428 TARGET_FOR_NAMED_ONLY
431 // The labels associated with this statement. May be NULL;
432 // if it is != NULL, guaranteed to contain at least one entry.
433 ZoneList<const AstRawString*>* labels() const { return labels_; }
435 // Type testing & conversion.
436 BreakableStatement* AsBreakableStatement() final { return this; }
439 Label* break_target() { return &break_target_; }
442 bool is_target_for_anonymous() const {
443 return breakable_type_ == TARGET_FOR_ANONYMOUS;
446 void set_base_id(int id) { base_id_ = id; }
447 static int num_ids() { return parent_num_ids() + 2; }
448 BailoutId EntryId() const { return BailoutId(local_id(0)); }
449 BailoutId ExitId() const { return BailoutId(local_id(1)); }
452 BreakableStatement(Zone* zone, ZoneList<const AstRawString*>* labels,
453 BreakableType breakable_type, int position)
454 : Statement(zone, position),
456 breakable_type_(breakable_type),
457 base_id_(BailoutId::None().ToInt()) {
458 DCHECK(labels == NULL || labels->length() > 0);
460 static int parent_num_ids() { return 0; }
462 int base_id() const {
463 DCHECK(!BailoutId(base_id_).IsNone());
468 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
470 ZoneList<const AstRawString*>* labels_;
471 BreakableType breakable_type_;
477 class Block final : public BreakableStatement {
479 DECLARE_NODE_TYPE(Block)
481 void AddStatement(Statement* statement, Zone* zone) {
482 statements_.Add(statement, zone);
485 ZoneList<Statement*>* statements() { return &statements_; }
486 bool is_initializer_block() const { return is_initializer_block_; }
488 static int num_ids() { return parent_num_ids() + 1; }
489 BailoutId DeclsId() const { return BailoutId(local_id(0)); }
491 bool IsJump() const override {
492 return !statements_.is_empty() && statements_.last()->IsJump()
493 && labels() == NULL; // Good enough as an approximation...
496 Scope* scope() const { return scope_; }
497 void set_scope(Scope* scope) { scope_ = scope; }
500 Block(Zone* zone, ZoneList<const AstRawString*>* labels, int capacity,
501 bool is_initializer_block, int pos)
502 : BreakableStatement(zone, labels, TARGET_FOR_NAMED_ONLY, pos),
503 statements_(capacity, zone),
504 is_initializer_block_(is_initializer_block),
506 static int parent_num_ids() { return BreakableStatement::num_ids(); }
509 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
511 ZoneList<Statement*> statements_;
512 bool is_initializer_block_;
517 class Declaration : public AstNode {
519 VariableProxy* proxy() const { return proxy_; }
520 VariableMode mode() const { return mode_; }
521 Scope* scope() const { return scope_; }
522 virtual InitializationFlag initialization() const = 0;
523 virtual bool IsInlineable() const;
526 Declaration(Zone* zone, VariableProxy* proxy, VariableMode mode, Scope* scope,
528 : AstNode(pos), mode_(mode), proxy_(proxy), scope_(scope) {
529 DCHECK(IsDeclaredVariableMode(mode));
534 VariableProxy* proxy_;
536 // Nested scope from which the declaration originated.
541 class VariableDeclaration final : public Declaration {
543 DECLARE_NODE_TYPE(VariableDeclaration)
545 InitializationFlag initialization() const override {
546 return mode() == VAR ? kCreatedInitialized : kNeedsInitialization;
549 bool is_class_declaration() const { return is_class_declaration_; }
551 // VariableDeclarations can be grouped into consecutive declaration
552 // groups. Each VariableDeclaration is associated with the start position of
553 // the group it belongs to. The positions are used for strong mode scope
554 // checks for classes and functions.
555 int declaration_group_start() const { return declaration_group_start_; }
558 VariableDeclaration(Zone* zone, VariableProxy* proxy, VariableMode mode,
559 Scope* scope, int pos, bool is_class_declaration = false,
560 int declaration_group_start = -1)
561 : Declaration(zone, proxy, mode, scope, pos),
562 is_class_declaration_(is_class_declaration),
563 declaration_group_start_(declaration_group_start) {}
565 bool is_class_declaration_;
566 int declaration_group_start_;
570 class FunctionDeclaration final : public Declaration {
572 DECLARE_NODE_TYPE(FunctionDeclaration)
574 FunctionLiteral* fun() const { return fun_; }
575 InitializationFlag initialization() const override {
576 return kCreatedInitialized;
578 bool IsInlineable() const override;
581 FunctionDeclaration(Zone* zone,
582 VariableProxy* proxy,
584 FunctionLiteral* fun,
587 : Declaration(zone, proxy, mode, scope, pos),
589 DCHECK(mode == VAR || mode == LET || mode == CONST);
594 FunctionLiteral* fun_;
598 class ImportDeclaration final : public Declaration {
600 DECLARE_NODE_TYPE(ImportDeclaration)
602 const AstRawString* import_name() const { return import_name_; }
603 const AstRawString* module_specifier() const { return module_specifier_; }
604 void set_module_specifier(const AstRawString* module_specifier) {
605 DCHECK(module_specifier_ == NULL);
606 module_specifier_ = module_specifier;
608 InitializationFlag initialization() const override {
609 return kNeedsInitialization;
613 ImportDeclaration(Zone* zone, VariableProxy* proxy,
614 const AstRawString* import_name,
615 const AstRawString* module_specifier, Scope* scope, int pos)
616 : Declaration(zone, proxy, IMPORT, scope, pos),
617 import_name_(import_name),
618 module_specifier_(module_specifier) {}
621 const AstRawString* import_name_;
622 const AstRawString* module_specifier_;
626 class ExportDeclaration final : public Declaration {
628 DECLARE_NODE_TYPE(ExportDeclaration)
630 InitializationFlag initialization() const override {
631 return kCreatedInitialized;
635 ExportDeclaration(Zone* zone, VariableProxy* proxy, Scope* scope, int pos)
636 : Declaration(zone, proxy, LET, scope, pos) {}
640 class Module : public AstNode {
642 ModuleDescriptor* descriptor() const { return descriptor_; }
643 Block* body() const { return body_; }
646 Module(Zone* zone, int pos)
647 : AstNode(pos), descriptor_(ModuleDescriptor::New(zone)), body_(NULL) {}
648 Module(Zone* zone, ModuleDescriptor* descriptor, int pos, Block* body = NULL)
649 : AstNode(pos), descriptor_(descriptor), body_(body) {}
652 ModuleDescriptor* descriptor_;
657 class IterationStatement : public BreakableStatement {
659 // Type testing & conversion.
660 IterationStatement* AsIterationStatement() final { return this; }
662 Statement* body() const { return body_; }
664 static int num_ids() { return parent_num_ids() + 1; }
665 BailoutId OsrEntryId() const { return BailoutId(local_id(0)); }
666 virtual BailoutId ContinueId() const = 0;
667 virtual BailoutId StackCheckId() const = 0;
670 Label* continue_target() { return &continue_target_; }
673 IterationStatement(Zone* zone, ZoneList<const AstRawString*>* labels, int pos)
674 : BreakableStatement(zone, labels, TARGET_FOR_ANONYMOUS, pos),
676 static int parent_num_ids() { return BreakableStatement::num_ids(); }
677 void Initialize(Statement* body) { body_ = body; }
680 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
683 Label continue_target_;
687 class DoWhileStatement final : public IterationStatement {
689 DECLARE_NODE_TYPE(DoWhileStatement)
691 void Initialize(Expression* cond, Statement* body) {
692 IterationStatement::Initialize(body);
696 Expression* cond() const { return cond_; }
698 static int num_ids() { return parent_num_ids() + 2; }
699 BailoutId ContinueId() const override { return BailoutId(local_id(0)); }
700 BailoutId StackCheckId() const override { return BackEdgeId(); }
701 BailoutId BackEdgeId() const { return BailoutId(local_id(1)); }
704 DoWhileStatement(Zone* zone, ZoneList<const AstRawString*>* labels, int pos)
705 : IterationStatement(zone, labels, pos), cond_(NULL) {}
706 static int parent_num_ids() { return IterationStatement::num_ids(); }
709 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
715 class WhileStatement final : public IterationStatement {
717 DECLARE_NODE_TYPE(WhileStatement)
719 void Initialize(Expression* cond, Statement* body) {
720 IterationStatement::Initialize(body);
724 Expression* cond() const { return cond_; }
726 static int num_ids() { return parent_num_ids() + 1; }
727 BailoutId ContinueId() const override { return EntryId(); }
728 BailoutId StackCheckId() const override { return BodyId(); }
729 BailoutId BodyId() const { return BailoutId(local_id(0)); }
732 WhileStatement(Zone* zone, ZoneList<const AstRawString*>* labels, int pos)
733 : IterationStatement(zone, labels, pos), cond_(NULL) {}
734 static int parent_num_ids() { return IterationStatement::num_ids(); }
737 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
743 class ForStatement final : public IterationStatement {
745 DECLARE_NODE_TYPE(ForStatement)
747 void Initialize(Statement* init,
751 IterationStatement::Initialize(body);
757 Statement* init() const { return init_; }
758 Expression* cond() const { return cond_; }
759 Statement* next() const { return next_; }
761 static int num_ids() { return parent_num_ids() + 2; }
762 BailoutId ContinueId() const override { return BailoutId(local_id(0)); }
763 BailoutId StackCheckId() const override { return BodyId(); }
764 BailoutId BodyId() const { return BailoutId(local_id(1)); }
767 ForStatement(Zone* zone, ZoneList<const AstRawString*>* labels, int pos)
768 : IterationStatement(zone, labels, pos),
772 static int parent_num_ids() { return IterationStatement::num_ids(); }
775 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
783 class ForEachStatement : public IterationStatement {
786 ENUMERATE, // for (each in subject) body;
787 ITERATE // for (each of subject) body;
790 void Initialize(Expression* each, Expression* subject, Statement* body) {
791 IterationStatement::Initialize(body);
796 Expression* each() const { return each_; }
797 Expression* subject() const { return subject_; }
799 FeedbackVectorRequirements ComputeFeedbackRequirements(
800 Isolate* isolate, const ICSlotCache* cache) override;
801 void SetFirstFeedbackICSlot(FeedbackVectorICSlot slot,
802 ICSlotCache* cache) override {
805 Code::Kind FeedbackICSlotKind(int index) override;
806 FeedbackVectorICSlot EachFeedbackSlot() const { return each_slot_; }
809 ForEachStatement(Zone* zone, ZoneList<const AstRawString*>* labels, int pos)
810 : IterationStatement(zone, labels, pos),
813 each_slot_(FeedbackVectorICSlot::Invalid()) {}
817 Expression* subject_;
818 FeedbackVectorICSlot each_slot_;
822 class ForInStatement final : public ForEachStatement {
824 DECLARE_NODE_TYPE(ForInStatement)
826 Expression* enumerable() const {
830 // Type feedback information.
831 FeedbackVectorRequirements ComputeFeedbackRequirements(
832 Isolate* isolate, const ICSlotCache* cache) override {
833 FeedbackVectorRequirements base =
834 ForEachStatement::ComputeFeedbackRequirements(isolate, cache);
835 DCHECK(base.slots() == 0 && base.ic_slots() <= 1);
836 return FeedbackVectorRequirements(1, base.ic_slots());
838 void SetFirstFeedbackSlot(FeedbackVectorSlot slot) override {
839 for_in_feedback_slot_ = slot;
842 FeedbackVectorSlot ForInFeedbackSlot() {
843 DCHECK(!for_in_feedback_slot_.IsInvalid());
844 return for_in_feedback_slot_;
847 enum ForInType { FAST_FOR_IN, SLOW_FOR_IN };
848 ForInType for_in_type() const { return for_in_type_; }
849 void set_for_in_type(ForInType type) { for_in_type_ = type; }
851 static int num_ids() { return parent_num_ids() + 6; }
852 BailoutId BodyId() const { return BailoutId(local_id(0)); }
853 BailoutId PrepareId() const { return BailoutId(local_id(1)); }
854 BailoutId EnumId() const { return BailoutId(local_id(2)); }
855 BailoutId ToObjectId() const { return BailoutId(local_id(3)); }
856 BailoutId FilterId() const { return BailoutId(local_id(4)); }
857 BailoutId AssignmentId() const { return BailoutId(local_id(5)); }
858 BailoutId ContinueId() const override { return EntryId(); }
859 BailoutId StackCheckId() const override { return BodyId(); }
862 ForInStatement(Zone* zone, ZoneList<const AstRawString*>* labels, int pos)
863 : ForEachStatement(zone, labels, pos),
864 for_in_type_(SLOW_FOR_IN),
865 for_in_feedback_slot_(FeedbackVectorSlot::Invalid()) {}
866 static int parent_num_ids() { return ForEachStatement::num_ids(); }
869 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
871 ForInType for_in_type_;
872 FeedbackVectorSlot for_in_feedback_slot_;
876 class ForOfStatement final : public ForEachStatement {
878 DECLARE_NODE_TYPE(ForOfStatement)
880 void Initialize(Expression* each,
883 Expression* assign_iterator,
884 Expression* next_result,
885 Expression* result_done,
886 Expression* assign_each) {
887 ForEachStatement::Initialize(each, subject, body);
888 assign_iterator_ = assign_iterator;
889 next_result_ = next_result;
890 result_done_ = result_done;
891 assign_each_ = assign_each;
894 Expression* iterable() const {
898 // iterator = subject[Symbol.iterator]()
899 Expression* assign_iterator() const {
900 return assign_iterator_;
903 // result = iterator.next() // with type check
904 Expression* next_result() const {
909 Expression* result_done() const {
913 // each = result.value
914 Expression* assign_each() const {
918 BailoutId ContinueId() const override { return EntryId(); }
919 BailoutId StackCheckId() const override { return BackEdgeId(); }
921 static int num_ids() { return parent_num_ids() + 1; }
922 BailoutId BackEdgeId() const { return BailoutId(local_id(0)); }
925 ForOfStatement(Zone* zone, ZoneList<const AstRawString*>* labels, int pos)
926 : ForEachStatement(zone, labels, pos),
927 assign_iterator_(NULL),
930 assign_each_(NULL) {}
931 static int parent_num_ids() { return ForEachStatement::num_ids(); }
934 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
936 Expression* assign_iterator_;
937 Expression* next_result_;
938 Expression* result_done_;
939 Expression* assign_each_;
943 class ExpressionStatement final : public Statement {
945 DECLARE_NODE_TYPE(ExpressionStatement)
947 void set_expression(Expression* e) { expression_ = e; }
948 Expression* expression() const { return expression_; }
949 bool IsJump() const override { return expression_->IsThrow(); }
952 ExpressionStatement(Zone* zone, Expression* expression, int pos)
953 : Statement(zone, pos), expression_(expression) { }
956 Expression* expression_;
960 class JumpStatement : public Statement {
962 bool IsJump() const final { return true; }
965 explicit JumpStatement(Zone* zone, int pos) : Statement(zone, pos) {}
969 class ContinueStatement final : public JumpStatement {
971 DECLARE_NODE_TYPE(ContinueStatement)
973 IterationStatement* target() const { return target_; }
976 explicit ContinueStatement(Zone* zone, IterationStatement* target, int pos)
977 : JumpStatement(zone, pos), target_(target) { }
980 IterationStatement* target_;
984 class BreakStatement final : public JumpStatement {
986 DECLARE_NODE_TYPE(BreakStatement)
988 BreakableStatement* target() const { return target_; }
991 explicit BreakStatement(Zone* zone, BreakableStatement* target, int pos)
992 : JumpStatement(zone, pos), target_(target) { }
995 BreakableStatement* target_;
999 class ReturnStatement final : public JumpStatement {
1001 DECLARE_NODE_TYPE(ReturnStatement)
1003 Expression* expression() const { return expression_; }
1006 explicit ReturnStatement(Zone* zone, Expression* expression, int pos)
1007 : JumpStatement(zone, pos), expression_(expression) { }
1010 Expression* expression_;
1014 class WithStatement final : public Statement {
1016 DECLARE_NODE_TYPE(WithStatement)
1018 Scope* scope() { return scope_; }
1019 Expression* expression() const { return expression_; }
1020 Statement* statement() const { return statement_; }
1022 void set_base_id(int id) { base_id_ = id; }
1023 static int num_ids() { return parent_num_ids() + 1; }
1024 BailoutId EntryId() const { return BailoutId(local_id(0)); }
1027 WithStatement(Zone* zone, Scope* scope, Expression* expression,
1028 Statement* statement, int pos)
1029 : Statement(zone, pos),
1031 expression_(expression),
1032 statement_(statement),
1033 base_id_(BailoutId::None().ToInt()) {}
1034 static int parent_num_ids() { return 0; }
1036 int base_id() const {
1037 DCHECK(!BailoutId(base_id_).IsNone());
1042 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
1045 Expression* expression_;
1046 Statement* statement_;
1051 class CaseClause final : public Expression {
1053 DECLARE_NODE_TYPE(CaseClause)
1055 bool is_default() const { return label_ == NULL; }
1056 Expression* label() const {
1057 CHECK(!is_default());
1060 Label* body_target() { return &body_target_; }
1061 ZoneList<Statement*>* statements() const { return statements_; }
1063 static int num_ids() { return parent_num_ids() + 2; }
1064 BailoutId EntryId() const { return BailoutId(local_id(0)); }
1065 TypeFeedbackId CompareId() { return TypeFeedbackId(local_id(1)); }
1067 Type* compare_type() { return compare_type_; }
1068 void set_compare_type(Type* type) { compare_type_ = type; }
1071 static int parent_num_ids() { return Expression::num_ids(); }
1074 CaseClause(Zone* zone, Expression* label, ZoneList<Statement*>* statements,
1076 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
1080 ZoneList<Statement*>* statements_;
1081 Type* compare_type_;
1085 class SwitchStatement final : public BreakableStatement {
1087 DECLARE_NODE_TYPE(SwitchStatement)
1089 void Initialize(Expression* tag, ZoneList<CaseClause*>* cases) {
1094 Expression* tag() const { return tag_; }
1095 ZoneList<CaseClause*>* cases() const { return cases_; }
1098 SwitchStatement(Zone* zone, ZoneList<const AstRawString*>* labels, int pos)
1099 : BreakableStatement(zone, labels, TARGET_FOR_ANONYMOUS, pos),
1105 ZoneList<CaseClause*>* cases_;
1109 // If-statements always have non-null references to their then- and
1110 // else-parts. When parsing if-statements with no explicit else-part,
1111 // the parser implicitly creates an empty statement. Use the
1112 // HasThenStatement() and HasElseStatement() functions to check if a
1113 // given if-statement has a then- or an else-part containing code.
1114 class IfStatement final : public Statement {
1116 DECLARE_NODE_TYPE(IfStatement)
1118 bool HasThenStatement() const { return !then_statement()->IsEmpty(); }
1119 bool HasElseStatement() const { return !else_statement()->IsEmpty(); }
1121 Expression* condition() const { return condition_; }
1122 Statement* then_statement() const { return then_statement_; }
1123 Statement* else_statement() const { return else_statement_; }
1125 bool IsJump() const override {
1126 return HasThenStatement() && then_statement()->IsJump()
1127 && HasElseStatement() && else_statement()->IsJump();
1130 void set_base_id(int id) { base_id_ = id; }
1131 static int num_ids() { return parent_num_ids() + 3; }
1132 BailoutId IfId() const { return BailoutId(local_id(0)); }
1133 BailoutId ThenId() const { return BailoutId(local_id(1)); }
1134 BailoutId ElseId() const { return BailoutId(local_id(2)); }
1137 IfStatement(Zone* zone, Expression* condition, Statement* then_statement,
1138 Statement* else_statement, int pos)
1139 : Statement(zone, pos),
1140 condition_(condition),
1141 then_statement_(then_statement),
1142 else_statement_(else_statement),
1143 base_id_(BailoutId::None().ToInt()) {}
1144 static int parent_num_ids() { return 0; }
1146 int base_id() const {
1147 DCHECK(!BailoutId(base_id_).IsNone());
1152 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
1154 Expression* condition_;
1155 Statement* then_statement_;
1156 Statement* else_statement_;
1161 class TryStatement : public Statement {
1163 int index() const { return index_; }
1164 Block* try_block() const { return try_block_; }
1167 TryStatement(Zone* zone, int index, Block* try_block, int pos)
1168 : Statement(zone, pos), index_(index), try_block_(try_block) {}
1171 // Unique (per-function) index of this handler. This is not an AST ID.
1178 class TryCatchStatement final : public TryStatement {
1180 DECLARE_NODE_TYPE(TryCatchStatement)
1182 Scope* scope() { return scope_; }
1183 Variable* variable() { return variable_; }
1184 Block* catch_block() const { return catch_block_; }
1187 TryCatchStatement(Zone* zone,
1194 : TryStatement(zone, index, try_block, pos),
1196 variable_(variable),
1197 catch_block_(catch_block) {
1202 Variable* variable_;
1203 Block* catch_block_;
1207 class TryFinallyStatement final : public TryStatement {
1209 DECLARE_NODE_TYPE(TryFinallyStatement)
1211 Block* finally_block() const { return finally_block_; }
1214 TryFinallyStatement(
1215 Zone* zone, int index, Block* try_block, Block* finally_block, int pos)
1216 : TryStatement(zone, index, try_block, pos),
1217 finally_block_(finally_block) { }
1220 Block* finally_block_;
1224 class DebuggerStatement final : public Statement {
1226 DECLARE_NODE_TYPE(DebuggerStatement)
1228 void set_base_id(int id) { base_id_ = id; }
1229 static int num_ids() { return parent_num_ids() + 1; }
1230 BailoutId DebugBreakId() const { return BailoutId(local_id(0)); }
1233 explicit DebuggerStatement(Zone* zone, int pos)
1234 : Statement(zone, pos), base_id_(BailoutId::None().ToInt()) {}
1235 static int parent_num_ids() { return 0; }
1237 int base_id() const {
1238 DCHECK(!BailoutId(base_id_).IsNone());
1243 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
1249 class EmptyStatement final : public Statement {
1251 DECLARE_NODE_TYPE(EmptyStatement)
1254 explicit EmptyStatement(Zone* zone, int pos): Statement(zone, pos) {}
1258 class Literal final : public Expression {
1260 DECLARE_NODE_TYPE(Literal)
1262 bool IsPropertyName() const override { return value_->IsPropertyName(); }
1264 Handle<String> AsPropertyName() {
1265 DCHECK(IsPropertyName());
1266 return Handle<String>::cast(value());
1269 const AstRawString* AsRawPropertyName() {
1270 DCHECK(IsPropertyName());
1271 return value_->AsString();
1274 bool ToBooleanIsTrue() const override { return value()->BooleanValue(); }
1275 bool ToBooleanIsFalse() const override { return !value()->BooleanValue(); }
1277 Handle<Object> value() const { return value_->value(); }
1278 const AstValue* raw_value() const { return value_; }
1280 // Support for using Literal as a HashMap key. NOTE: Currently, this works
1281 // only for string and number literals!
1283 static bool Match(void* literal1, void* literal2);
1285 static int num_ids() { return parent_num_ids() + 1; }
1286 TypeFeedbackId LiteralFeedbackId() const {
1287 return TypeFeedbackId(local_id(0));
1291 Literal(Zone* zone, const AstValue* value, int position)
1292 : Expression(zone, position), value_(value) {}
1293 static int parent_num_ids() { return Expression::num_ids(); }
1296 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
1298 const AstValue* value_;
1302 // Base class for literals that needs space in the corresponding JSFunction.
1303 class MaterializedLiteral : public Expression {
1305 virtual MaterializedLiteral* AsMaterializedLiteral() { return this; }
1307 int literal_index() { return literal_index_; }
1310 // only callable after initialization.
1311 DCHECK(depth_ >= 1);
1315 bool is_strong() const { return is_strong_; }
1318 MaterializedLiteral(Zone* zone, int literal_index, bool is_strong, int pos)
1319 : Expression(zone, pos),
1320 literal_index_(literal_index),
1322 is_strong_(is_strong),
1325 // A materialized literal is simple if the values consist of only
1326 // constants and simple object and array literals.
1327 bool is_simple() const { return is_simple_; }
1328 void set_is_simple(bool is_simple) { is_simple_ = is_simple; }
1329 friend class CompileTimeValue;
1331 void set_depth(int depth) {
1336 // Populate the constant properties/elements fixed array.
1337 void BuildConstants(Isolate* isolate);
1338 friend class ArrayLiteral;
1339 friend class ObjectLiteral;
1341 // If the expression is a literal, return the literal value;
1342 // if the expression is a materialized literal and is simple return a
1343 // compile time value as encoded by CompileTimeValue::GetValue().
1344 // Otherwise, return undefined literal as the placeholder
1345 // in the object literal boilerplate.
1346 Handle<Object> GetBoilerplateValue(Expression* expression, Isolate* isolate);
1356 // Property is used for passing information
1357 // about an object literal's properties from the parser
1358 // to the code generator.
1359 class ObjectLiteralProperty final : public ZoneObject {
1362 CONSTANT, // Property with constant value (compile time).
1363 COMPUTED, // Property with computed value (execution time).
1364 MATERIALIZED_LITERAL, // Property value is a materialized literal.
1365 GETTER, SETTER, // Property is an accessor function.
1366 PROTOTYPE // Property is __proto__.
1369 Expression* key() { return key_; }
1370 Expression* value() { return value_; }
1371 Kind kind() { return kind_; }
1373 // Type feedback information.
1374 bool IsMonomorphic() { return !receiver_type_.is_null(); }
1375 Handle<Map> GetReceiverType() { return receiver_type_; }
1377 bool IsCompileTimeValue();
1379 void set_emit_store(bool emit_store);
1382 bool is_static() const { return is_static_; }
1383 bool is_computed_name() const { return is_computed_name_; }
1385 void set_receiver_type(Handle<Map> map) { receiver_type_ = map; }
1388 friend class AstNodeFactory;
1390 ObjectLiteralProperty(Expression* key, Expression* value, Kind kind,
1391 bool is_static, bool is_computed_name);
1392 ObjectLiteralProperty(AstValueFactory* ast_value_factory, Expression* key,
1393 Expression* value, bool is_static,
1394 bool is_computed_name);
1402 bool is_computed_name_;
1403 Handle<Map> receiver_type_;
1407 // An object literal has a boilerplate object that is used
1408 // for minimizing the work when constructing it at runtime.
1409 class ObjectLiteral final : public MaterializedLiteral {
1411 typedef ObjectLiteralProperty Property;
1413 DECLARE_NODE_TYPE(ObjectLiteral)
1415 Handle<FixedArray> constant_properties() const {
1416 return constant_properties_;
1418 int properties_count() const { return constant_properties_->length() / 2; }
1419 ZoneList<Property*>* properties() const { return properties_; }
1420 bool fast_elements() const { return fast_elements_; }
1421 bool may_store_doubles() const { return may_store_doubles_; }
1422 bool has_function() const { return has_function_; }
1423 bool has_elements() const { return has_elements_; }
1425 // Decide if a property should be in the object boilerplate.
1426 static bool IsBoilerplateProperty(Property* property);
1428 // Populate the constant properties fixed array.
1429 void BuildConstantProperties(Isolate* isolate);
1431 // Mark all computed expressions that are bound to a key that
1432 // is shadowed by a later occurrence of the same key. For the
1433 // marked expressions, no store code is emitted.
1434 void CalculateEmitStore(Zone* zone);
1436 // Assemble bitfield of flags for the CreateObjectLiteral helper.
1437 int ComputeFlags(bool disable_mementos = false) const {
1438 int flags = fast_elements() ? kFastElements : kNoFlags;
1439 flags |= has_function() ? kHasFunction : kNoFlags;
1440 if (depth() == 1 && !has_elements() && !may_store_doubles()) {
1441 flags |= kShallowProperties;
1443 if (disable_mementos) {
1444 flags |= kDisableMementos;
1455 kHasFunction = 1 << 1,
1456 kShallowProperties = 1 << 2,
1457 kDisableMementos = 1 << 3,
1461 struct Accessors: public ZoneObject {
1462 Accessors() : getter(NULL), setter(NULL) {}
1467 BailoutId CreateLiteralId() const { return BailoutId(local_id(0)); }
1469 // Return an AST id for a property that is used in simulate instructions.
1470 BailoutId GetIdForProperty(int i) { return BailoutId(local_id(i + 1)); }
1472 // Unlike other AST nodes, this number of bailout IDs allocated for an
1473 // ObjectLiteral can vary, so num_ids() is not a static method.
1474 int num_ids() const { return parent_num_ids() + 1 + properties()->length(); }
1476 // Object literals need one feedback slot for each non-trivial value, as well
1477 // as some slots for home objects.
1478 FeedbackVectorRequirements ComputeFeedbackRequirements(
1479 Isolate* isolate, const ICSlotCache* cache) override;
1480 void SetFirstFeedbackICSlot(FeedbackVectorICSlot slot,
1481 ICSlotCache* cache) override {
1484 Code::Kind FeedbackICSlotKind(int index) override { return Code::STORE_IC; }
1485 FeedbackVectorICSlot GetNthSlot(int n) const {
1486 return FeedbackVectorICSlot(slot_.ToInt() + n);
1489 // If value needs a home object, returns a valid feedback vector ic slot
1490 // given by slot_index, and increments slot_index.
1491 FeedbackVectorICSlot SlotForHomeObject(Expression* value,
1492 int* slot_index) const;
1495 int slot_count() const { return slot_count_; }
1499 ObjectLiteral(Zone* zone, ZoneList<Property*>* properties, int literal_index,
1500 int boilerplate_properties, bool has_function, bool is_strong,
1502 : MaterializedLiteral(zone, literal_index, is_strong, pos),
1503 properties_(properties),
1504 boilerplate_properties_(boilerplate_properties),
1505 fast_elements_(false),
1506 has_elements_(false),
1507 may_store_doubles_(false),
1508 has_function_(has_function),
1512 slot_(FeedbackVectorICSlot::Invalid()) {
1514 static int parent_num_ids() { return MaterializedLiteral::num_ids(); }
1517 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
1518 Handle<FixedArray> constant_properties_;
1519 ZoneList<Property*>* properties_;
1520 int boilerplate_properties_;
1521 bool fast_elements_;
1523 bool may_store_doubles_;
1526 // slot_count_ helps validate that the logic to allocate ic slots and the
1527 // logic to use them are in sync.
1530 FeedbackVectorICSlot slot_;
1534 // Node for capturing a regexp literal.
1535 class RegExpLiteral final : public MaterializedLiteral {
1537 DECLARE_NODE_TYPE(RegExpLiteral)
1539 Handle<String> pattern() const { return pattern_->string(); }
1540 Handle<String> flags() const { return flags_->string(); }
1543 RegExpLiteral(Zone* zone, const AstRawString* pattern,
1544 const AstRawString* flags, int literal_index, bool is_strong,
1546 : MaterializedLiteral(zone, literal_index, is_strong, pos),
1553 const AstRawString* pattern_;
1554 const AstRawString* flags_;
1558 // An array literal has a literals object that is used
1559 // for minimizing the work when constructing it at runtime.
1560 class ArrayLiteral final : public MaterializedLiteral {
1562 DECLARE_NODE_TYPE(ArrayLiteral)
1564 Handle<FixedArray> constant_elements() const { return constant_elements_; }
1565 ElementsKind constant_elements_kind() const {
1566 DCHECK_EQ(2, constant_elements_->length());
1567 return static_cast<ElementsKind>(
1568 Smi::cast(constant_elements_->get(0))->value());
1571 ZoneList<Expression*>* values() const { return values_; }
1573 BailoutId CreateLiteralId() const { return BailoutId(local_id(0)); }
1575 // Return an AST id for an element that is used in simulate instructions.
1576 BailoutId GetIdForElement(int i) { return BailoutId(local_id(i + 1)); }
1578 // Unlike other AST nodes, this number of bailout IDs allocated for an
1579 // ArrayLiteral can vary, so num_ids() is not a static method.
1580 int num_ids() const { return parent_num_ids() + 1 + values()->length(); }
1582 // Populate the constant elements fixed array.
1583 void BuildConstantElements(Isolate* isolate);
1585 // Assemble bitfield of flags for the CreateArrayLiteral helper.
1586 int ComputeFlags(bool disable_mementos = false) const {
1587 int flags = depth() == 1 ? kShallowElements : kNoFlags;
1588 if (disable_mementos) {
1589 flags |= kDisableMementos;
1599 kShallowElements = 1,
1600 kDisableMementos = 1 << 1,
1605 ArrayLiteral(Zone* zone, ZoneList<Expression*>* values, int literal_index,
1606 bool is_strong, int pos)
1607 : MaterializedLiteral(zone, literal_index, is_strong, pos),
1609 static int parent_num_ids() { return MaterializedLiteral::num_ids(); }
1612 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
1614 Handle<FixedArray> constant_elements_;
1615 ZoneList<Expression*>* values_;
1619 class VariableProxy final : public Expression {
1621 DECLARE_NODE_TYPE(VariableProxy)
1623 bool IsValidReferenceExpression() const override { return !is_this(); }
1625 bool IsArguments() const { return is_resolved() && var()->is_arguments(); }
1627 Handle<String> name() const { return raw_name()->string(); }
1628 const AstRawString* raw_name() const {
1629 return is_resolved() ? var_->raw_name() : raw_name_;
1632 Variable* var() const {
1633 DCHECK(is_resolved());
1636 void set_var(Variable* v) {
1637 DCHECK(!is_resolved());
1642 bool is_this() const { return IsThisField::decode(bit_field_); }
1644 bool is_assigned() const { return IsAssignedField::decode(bit_field_); }
1645 void set_is_assigned() {
1646 bit_field_ = IsAssignedField::update(bit_field_, true);
1649 bool is_resolved() const { return IsResolvedField::decode(bit_field_); }
1650 void set_is_resolved() {
1651 bit_field_ = IsResolvedField::update(bit_field_, true);
1654 int end_position() const { return end_position_; }
1656 // Bind this proxy to the variable var.
1657 void BindTo(Variable* var);
1659 bool UsesVariableFeedbackSlot() const {
1660 return var()->IsUnallocated() || var()->IsLookupSlot();
1663 virtual FeedbackVectorRequirements ComputeFeedbackRequirements(
1664 Isolate* isolate, const ICSlotCache* cache) override;
1666 void SetFirstFeedbackICSlot(FeedbackVectorICSlot slot,
1667 ICSlotCache* cache) override;
1668 Code::Kind FeedbackICSlotKind(int index) override { return Code::LOAD_IC; }
1669 FeedbackVectorICSlot VariableFeedbackSlot() {
1670 DCHECK(!UsesVariableFeedbackSlot() || !variable_feedback_slot_.IsInvalid());
1671 return variable_feedback_slot_;
1674 static int num_ids() { return parent_num_ids() + 1; }
1675 BailoutId BeforeId() const { return BailoutId(local_id(0)); }
1678 VariableProxy(Zone* zone, Variable* var, int start_position,
1681 VariableProxy(Zone* zone, const AstRawString* name,
1682 Variable::Kind variable_kind, int start_position,
1684 static int parent_num_ids() { return Expression::num_ids(); }
1685 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
1687 class IsThisField : public BitField8<bool, 0, 1> {};
1688 class IsAssignedField : public BitField8<bool, 1, 1> {};
1689 class IsResolvedField : public BitField8<bool, 2, 1> {};
1691 // Start with 16-bit (or smaller) field, which should get packed together
1692 // with Expression's trailing 16-bit field.
1694 FeedbackVectorICSlot variable_feedback_slot_;
1696 const AstRawString* raw_name_; // if !is_resolved_
1697 Variable* var_; // if is_resolved_
1699 // Position is stored in the AstNode superclass, but VariableProxy needs to
1700 // know its end position too (for error messages). It cannot be inferred from
1701 // the variable name length because it can contain escapes.
1706 // Left-hand side can only be a property, a global or a (parameter or local)
1712 NAMED_SUPER_PROPERTY,
1713 KEYED_SUPER_PROPERTY
1717 class Property final : public Expression {
1719 DECLARE_NODE_TYPE(Property)
1721 bool IsValidReferenceExpression() const override { return true; }
1723 Expression* obj() const { return obj_; }
1724 Expression* key() const { return key_; }
1726 static int num_ids() { return parent_num_ids() + 1; }
1727 BailoutId LoadId() const { return BailoutId(local_id(0)); }
1729 bool IsStringAccess() const {
1730 return IsStringAccessField::decode(bit_field_);
1733 // Type feedback information.
1734 bool IsMonomorphic() override { return receiver_types_.length() == 1; }
1735 SmallMapList* GetReceiverTypes() override { return &receiver_types_; }
1736 KeyedAccessStoreMode GetStoreMode() const override { return STANDARD_STORE; }
1737 IcCheckType GetKeyType() const override {
1738 return KeyTypeField::decode(bit_field_);
1740 bool IsUninitialized() const {
1741 return !is_for_call() && HasNoTypeInformation();
1743 bool HasNoTypeInformation() const {
1744 return GetInlineCacheState() == UNINITIALIZED;
1746 InlineCacheState GetInlineCacheState() const {
1747 return InlineCacheStateField::decode(bit_field_);
1749 void set_is_string_access(bool b) {
1750 bit_field_ = IsStringAccessField::update(bit_field_, b);
1752 void set_key_type(IcCheckType key_type) {
1753 bit_field_ = KeyTypeField::update(bit_field_, key_type);
1755 void set_inline_cache_state(InlineCacheState state) {
1756 bit_field_ = InlineCacheStateField::update(bit_field_, state);
1758 void mark_for_call() {
1759 bit_field_ = IsForCallField::update(bit_field_, true);
1761 bool is_for_call() const { return IsForCallField::decode(bit_field_); }
1763 bool IsSuperAccess() {
1764 return obj()->IsSuperReference();
1767 virtual FeedbackVectorRequirements ComputeFeedbackRequirements(
1768 Isolate* isolate, const ICSlotCache* cache) override {
1769 return FeedbackVectorRequirements(0, 1);
1771 void SetFirstFeedbackICSlot(FeedbackVectorICSlot slot,
1772 ICSlotCache* cache) override {
1773 property_feedback_slot_ = slot;
1775 Code::Kind FeedbackICSlotKind(int index) override {
1776 return key()->IsPropertyName() ? Code::LOAD_IC : Code::KEYED_LOAD_IC;
1779 FeedbackVectorICSlot PropertyFeedbackSlot() const {
1780 DCHECK(!property_feedback_slot_.IsInvalid());
1781 return property_feedback_slot_;
1784 static LhsKind GetAssignType(Property* property) {
1785 if (property == NULL) return VARIABLE;
1786 bool super_access = property->IsSuperAccess();
1787 return (property->key()->IsPropertyName())
1788 ? (super_access ? NAMED_SUPER_PROPERTY : NAMED_PROPERTY)
1789 : (super_access ? KEYED_SUPER_PROPERTY : KEYED_PROPERTY);
1793 Property(Zone* zone, Expression* obj, Expression* key, int pos)
1794 : Expression(zone, pos),
1795 bit_field_(IsForCallField::encode(false) |
1796 IsStringAccessField::encode(false) |
1797 InlineCacheStateField::encode(UNINITIALIZED)),
1798 property_feedback_slot_(FeedbackVectorICSlot::Invalid()),
1801 static int parent_num_ids() { return Expression::num_ids(); }
1804 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
1806 class IsForCallField : public BitField8<bool, 0, 1> {};
1807 class IsStringAccessField : public BitField8<bool, 1, 1> {};
1808 class KeyTypeField : public BitField8<IcCheckType, 2, 1> {};
1809 class InlineCacheStateField : public BitField8<InlineCacheState, 3, 4> {};
1811 FeedbackVectorICSlot property_feedback_slot_;
1814 SmallMapList receiver_types_;
1818 class Call final : public Expression {
1820 DECLARE_NODE_TYPE(Call)
1822 Expression* expression() const { return expression_; }
1823 ZoneList<Expression*>* arguments() const { return arguments_; }
1825 // Type feedback information.
1826 virtual FeedbackVectorRequirements ComputeFeedbackRequirements(
1827 Isolate* isolate, const ICSlotCache* cache) override;
1828 void SetFirstFeedbackICSlot(FeedbackVectorICSlot slot,
1829 ICSlotCache* cache) override {
1832 void SetFirstFeedbackSlot(FeedbackVectorSlot slot) override { slot_ = slot; }
1833 Code::Kind FeedbackICSlotKind(int index) override { return Code::CALL_IC; }
1835 FeedbackVectorSlot CallFeedbackSlot() const { return slot_; }
1837 FeedbackVectorICSlot CallFeedbackICSlot() const { return ic_slot_; }
1839 SmallMapList* GetReceiverTypes() override {
1840 if (expression()->IsProperty()) {
1841 return expression()->AsProperty()->GetReceiverTypes();
1846 bool IsMonomorphic() override {
1847 if (expression()->IsProperty()) {
1848 return expression()->AsProperty()->IsMonomorphic();
1850 return !target_.is_null();
1853 bool global_call() const {
1854 VariableProxy* proxy = expression_->AsVariableProxy();
1855 return proxy != NULL && proxy->var()->IsUnallocated();
1858 bool known_global_function() const {
1859 return global_call() && !target_.is_null();
1862 Handle<JSFunction> target() { return target_; }
1864 Handle<AllocationSite> allocation_site() { return allocation_site_; }
1866 void SetKnownGlobalTarget(Handle<JSFunction> target) {
1868 set_is_uninitialized(false);
1870 void set_target(Handle<JSFunction> target) { target_ = target; }
1871 void set_allocation_site(Handle<AllocationSite> site) {
1872 allocation_site_ = site;
1875 static int num_ids() { return parent_num_ids() + 2; }
1876 BailoutId ReturnId() const { return BailoutId(local_id(0)); }
1877 BailoutId EvalOrLookupId() const { return BailoutId(local_id(1)); }
1879 bool is_uninitialized() const {
1880 return IsUninitializedField::decode(bit_field_);
1882 void set_is_uninitialized(bool b) {
1883 bit_field_ = IsUninitializedField::update(bit_field_, b);
1895 // Helpers to determine how to handle the call.
1896 CallType GetCallType(Isolate* isolate) const;
1897 bool IsUsingCallFeedbackSlot(Isolate* isolate) const;
1898 bool IsUsingCallFeedbackICSlot(Isolate* isolate) const;
1901 // Used to assert that the FullCodeGenerator records the return site.
1902 bool return_is_recorded_;
1906 Call(Zone* zone, Expression* expression, ZoneList<Expression*>* arguments,
1908 : Expression(zone, pos),
1909 ic_slot_(FeedbackVectorICSlot::Invalid()),
1910 slot_(FeedbackVectorSlot::Invalid()),
1911 expression_(expression),
1912 arguments_(arguments),
1913 bit_field_(IsUninitializedField::encode(false)) {
1914 if (expression->IsProperty()) {
1915 expression->AsProperty()->mark_for_call();
1918 static int parent_num_ids() { return Expression::num_ids(); }
1921 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
1923 FeedbackVectorICSlot ic_slot_;
1924 FeedbackVectorSlot slot_;
1925 Expression* expression_;
1926 ZoneList<Expression*>* arguments_;
1927 Handle<JSFunction> target_;
1928 Handle<AllocationSite> allocation_site_;
1929 class IsUninitializedField : public BitField8<bool, 0, 1> {};
1934 class CallNew final : public Expression {
1936 DECLARE_NODE_TYPE(CallNew)
1938 Expression* expression() const { return expression_; }
1939 ZoneList<Expression*>* arguments() const { return arguments_; }
1941 // Type feedback information.
1942 virtual FeedbackVectorRequirements ComputeFeedbackRequirements(
1943 Isolate* isolate, const ICSlotCache* cache) override {
1944 return FeedbackVectorRequirements(FLAG_pretenuring_call_new ? 2 : 1, 0);
1946 void SetFirstFeedbackSlot(FeedbackVectorSlot slot) override {
1947 callnew_feedback_slot_ = slot;
1950 FeedbackVectorSlot CallNewFeedbackSlot() {
1951 DCHECK(!callnew_feedback_slot_.IsInvalid());
1952 return callnew_feedback_slot_;
1954 FeedbackVectorSlot AllocationSiteFeedbackSlot() {
1955 DCHECK(FLAG_pretenuring_call_new);
1956 return CallNewFeedbackSlot().next();
1959 bool IsMonomorphic() override { return is_monomorphic_; }
1960 Handle<JSFunction> target() const { return target_; }
1961 Handle<AllocationSite> allocation_site() const {
1962 return allocation_site_;
1965 static int num_ids() { return parent_num_ids() + 1; }
1966 static int feedback_slots() { return 1; }
1967 BailoutId ReturnId() const { return BailoutId(local_id(0)); }
1969 void set_allocation_site(Handle<AllocationSite> site) {
1970 allocation_site_ = site;
1972 void set_is_monomorphic(bool monomorphic) { is_monomorphic_ = monomorphic; }
1973 void set_target(Handle<JSFunction> target) { target_ = target; }
1974 void SetKnownGlobalTarget(Handle<JSFunction> target) {
1976 is_monomorphic_ = true;
1980 CallNew(Zone* zone, Expression* expression, ZoneList<Expression*>* arguments,
1982 : Expression(zone, pos),
1983 expression_(expression),
1984 arguments_(arguments),
1985 is_monomorphic_(false),
1986 callnew_feedback_slot_(FeedbackVectorSlot::Invalid()) {}
1988 static int parent_num_ids() { return Expression::num_ids(); }
1991 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
1993 Expression* expression_;
1994 ZoneList<Expression*>* arguments_;
1995 bool is_monomorphic_;
1996 Handle<JSFunction> target_;
1997 Handle<AllocationSite> allocation_site_;
1998 FeedbackVectorSlot callnew_feedback_slot_;
2002 // The CallRuntime class does not represent any official JavaScript
2003 // language construct. Instead it is used to call a C or JS function
2004 // with a set of arguments. This is used from the builtins that are
2005 // implemented in JavaScript (see "v8natives.js").
2006 class CallRuntime final : public Expression {
2008 DECLARE_NODE_TYPE(CallRuntime)
2010 Handle<String> name() const { return raw_name_->string(); }
2011 const AstRawString* raw_name() const { return raw_name_; }
2012 const Runtime::Function* function() const { return function_; }
2013 ZoneList<Expression*>* arguments() const { return arguments_; }
2014 bool is_jsruntime() const { return function_ == NULL; }
2016 // Type feedback information.
2017 bool HasCallRuntimeFeedbackSlot() const { return is_jsruntime(); }
2018 virtual FeedbackVectorRequirements ComputeFeedbackRequirements(
2019 Isolate* isolate, const ICSlotCache* cache) override {
2020 return FeedbackVectorRequirements(0, HasCallRuntimeFeedbackSlot() ? 1 : 0);
2022 void SetFirstFeedbackICSlot(FeedbackVectorICSlot slot,
2023 ICSlotCache* cache) override {
2024 callruntime_feedback_slot_ = slot;
2026 Code::Kind FeedbackICSlotKind(int index) override { return Code::LOAD_IC; }
2028 FeedbackVectorICSlot CallRuntimeFeedbackSlot() {
2029 DCHECK(!HasCallRuntimeFeedbackSlot() ||
2030 !callruntime_feedback_slot_.IsInvalid());
2031 return callruntime_feedback_slot_;
2034 static int num_ids() { return parent_num_ids(); }
2037 CallRuntime(Zone* zone, const AstRawString* name,
2038 const Runtime::Function* function,
2039 ZoneList<Expression*>* arguments, int pos)
2040 : Expression(zone, pos),
2042 function_(function),
2043 arguments_(arguments),
2044 callruntime_feedback_slot_(FeedbackVectorICSlot::Invalid()) {}
2045 static int parent_num_ids() { return Expression::num_ids(); }
2048 const AstRawString* raw_name_;
2049 const Runtime::Function* function_;
2050 ZoneList<Expression*>* arguments_;
2051 FeedbackVectorICSlot callruntime_feedback_slot_;
2055 class UnaryOperation final : public Expression {
2057 DECLARE_NODE_TYPE(UnaryOperation)
2059 Token::Value op() const { return op_; }
2060 Expression* expression() const { return expression_; }
2062 // For unary not (Token::NOT), the AST ids where true and false will
2063 // actually be materialized, respectively.
2064 static int num_ids() { return parent_num_ids() + 2; }
2065 BailoutId MaterializeTrueId() const { return BailoutId(local_id(0)); }
2066 BailoutId MaterializeFalseId() const { return BailoutId(local_id(1)); }
2068 virtual void RecordToBooleanTypeFeedback(TypeFeedbackOracle* oracle) override;
2071 UnaryOperation(Zone* zone, Token::Value op, Expression* expression, int pos)
2072 : Expression(zone, pos), op_(op), expression_(expression) {
2073 DCHECK(Token::IsUnaryOp(op));
2075 static int parent_num_ids() { return Expression::num_ids(); }
2078 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
2081 Expression* expression_;
2085 class BinaryOperation final : public Expression {
2087 DECLARE_NODE_TYPE(BinaryOperation)
2089 Token::Value op() const { return static_cast<Token::Value>(op_); }
2090 Expression* left() const { return left_; }
2091 Expression* right() const { return right_; }
2092 Handle<AllocationSite> allocation_site() const { return allocation_site_; }
2093 void set_allocation_site(Handle<AllocationSite> allocation_site) {
2094 allocation_site_ = allocation_site;
2097 // The short-circuit logical operations need an AST ID for their
2098 // right-hand subexpression.
2099 static int num_ids() { return parent_num_ids() + 2; }
2100 BailoutId RightId() const { return BailoutId(local_id(0)); }
2102 TypeFeedbackId BinaryOperationFeedbackId() const {
2103 return TypeFeedbackId(local_id(1));
2105 Maybe<int> fixed_right_arg() const {
2106 return has_fixed_right_arg_ ? Just(fixed_right_arg_value_) : Nothing<int>();
2108 void set_fixed_right_arg(Maybe<int> arg) {
2109 has_fixed_right_arg_ = arg.IsJust();
2110 if (arg.IsJust()) fixed_right_arg_value_ = arg.FromJust();
2113 virtual void RecordToBooleanTypeFeedback(TypeFeedbackOracle* oracle) override;
2116 BinaryOperation(Zone* zone, Token::Value op, Expression* left,
2117 Expression* right, int pos)
2118 : Expression(zone, pos),
2119 op_(static_cast<byte>(op)),
2120 has_fixed_right_arg_(false),
2121 fixed_right_arg_value_(0),
2124 DCHECK(Token::IsBinaryOp(op));
2126 static int parent_num_ids() { return Expression::num_ids(); }
2129 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
2131 const byte op_; // actually Token::Value
2132 // TODO(rossberg): the fixed arg should probably be represented as a Constant
2133 // type for the RHS. Currenty it's actually a Maybe<int>
2134 bool has_fixed_right_arg_;
2135 int fixed_right_arg_value_;
2138 Handle<AllocationSite> allocation_site_;
2142 class CountOperation final : public Expression {
2144 DECLARE_NODE_TYPE(CountOperation)
2146 bool is_prefix() const { return IsPrefixField::decode(bit_field_); }
2147 bool is_postfix() const { return !is_prefix(); }
2149 Token::Value op() const { return TokenField::decode(bit_field_); }
2150 Token::Value binary_op() {
2151 return (op() == Token::INC) ? Token::ADD : Token::SUB;
2154 Expression* expression() const { return expression_; }
2156 bool IsMonomorphic() override { return receiver_types_.length() == 1; }
2157 SmallMapList* GetReceiverTypes() override { return &receiver_types_; }
2158 IcCheckType GetKeyType() const override {
2159 return KeyTypeField::decode(bit_field_);
2161 KeyedAccessStoreMode GetStoreMode() const override {
2162 return StoreModeField::decode(bit_field_);
2164 Type* type() const { return type_; }
2165 void set_key_type(IcCheckType type) {
2166 bit_field_ = KeyTypeField::update(bit_field_, type);
2168 void set_store_mode(KeyedAccessStoreMode mode) {
2169 bit_field_ = StoreModeField::update(bit_field_, mode);
2171 void set_type(Type* type) { type_ = type; }
2173 static int num_ids() { return parent_num_ids() + 4; }
2174 BailoutId AssignmentId() const { return BailoutId(local_id(0)); }
2175 BailoutId ToNumberId() const { return BailoutId(local_id(1)); }
2176 TypeFeedbackId CountBinOpFeedbackId() const {
2177 return TypeFeedbackId(local_id(2));
2179 TypeFeedbackId CountStoreFeedbackId() const {
2180 return TypeFeedbackId(local_id(3));
2183 FeedbackVectorRequirements ComputeFeedbackRequirements(
2184 Isolate* isolate, const ICSlotCache* cache) override;
2185 void SetFirstFeedbackICSlot(FeedbackVectorICSlot slot,
2186 ICSlotCache* cache) override {
2189 Code::Kind FeedbackICSlotKind(int index) override;
2190 FeedbackVectorICSlot CountSlot() const { return slot_; }
2193 CountOperation(Zone* zone, Token::Value op, bool is_prefix, Expression* expr,
2195 : Expression(zone, pos),
2197 IsPrefixField::encode(is_prefix) | KeyTypeField::encode(ELEMENT) |
2198 StoreModeField::encode(STANDARD_STORE) | TokenField::encode(op)),
2201 slot_(FeedbackVectorICSlot::Invalid()) {}
2202 static int parent_num_ids() { return Expression::num_ids(); }
2205 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
2207 class IsPrefixField : public BitField16<bool, 0, 1> {};
2208 class KeyTypeField : public BitField16<IcCheckType, 1, 1> {};
2209 class StoreModeField : public BitField16<KeyedAccessStoreMode, 2, 4> {};
2210 class TokenField : public BitField16<Token::Value, 6, 8> {};
2212 // Starts with 16-bit field, which should get packed together with
2213 // Expression's trailing 16-bit field.
2214 uint16_t bit_field_;
2216 Expression* expression_;
2217 SmallMapList receiver_types_;
2218 FeedbackVectorICSlot slot_;
2222 class CompareOperation final : public Expression {
2224 DECLARE_NODE_TYPE(CompareOperation)
2226 Token::Value op() const { return op_; }
2227 Expression* left() const { return left_; }
2228 Expression* right() const { return right_; }
2230 // Type feedback information.
2231 static int num_ids() { return parent_num_ids() + 1; }
2232 TypeFeedbackId CompareOperationFeedbackId() const {
2233 return TypeFeedbackId(local_id(0));
2235 Type* combined_type() const { return combined_type_; }
2236 void set_combined_type(Type* type) { combined_type_ = type; }
2238 // Match special cases.
2239 bool IsLiteralCompareTypeof(Expression** expr, Handle<String>* check);
2240 bool IsLiteralCompareUndefined(Expression** expr, Isolate* isolate);
2241 bool IsLiteralCompareNull(Expression** expr);
2244 CompareOperation(Zone* zone, Token::Value op, Expression* left,
2245 Expression* right, int pos)
2246 : Expression(zone, pos),
2250 combined_type_(Type::None(zone)) {
2251 DCHECK(Token::IsCompareOp(op));
2253 static int parent_num_ids() { return Expression::num_ids(); }
2256 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
2262 Type* combined_type_;
2266 class Spread final : public Expression {
2268 DECLARE_NODE_TYPE(Spread)
2270 Expression* expression() const { return expression_; }
2272 static int num_ids() { return parent_num_ids(); }
2275 Spread(Zone* zone, Expression* expression, int pos)
2276 : Expression(zone, pos), expression_(expression) {}
2277 static int parent_num_ids() { return Expression::num_ids(); }
2280 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
2282 Expression* expression_;
2286 class Conditional final : public Expression {
2288 DECLARE_NODE_TYPE(Conditional)
2290 Expression* condition() const { return condition_; }
2291 Expression* then_expression() const { return then_expression_; }
2292 Expression* else_expression() const { return else_expression_; }
2294 static int num_ids() { return parent_num_ids() + 2; }
2295 BailoutId ThenId() const { return BailoutId(local_id(0)); }
2296 BailoutId ElseId() const { return BailoutId(local_id(1)); }
2299 Conditional(Zone* zone, Expression* condition, Expression* then_expression,
2300 Expression* else_expression, int position)
2301 : Expression(zone, position),
2302 condition_(condition),
2303 then_expression_(then_expression),
2304 else_expression_(else_expression) {}
2305 static int parent_num_ids() { return Expression::num_ids(); }
2308 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
2310 Expression* condition_;
2311 Expression* then_expression_;
2312 Expression* else_expression_;
2316 class Assignment final : public Expression {
2318 DECLARE_NODE_TYPE(Assignment)
2320 Assignment* AsSimpleAssignment() { return !is_compound() ? this : NULL; }
2322 Token::Value binary_op() const;
2324 Token::Value op() const { return TokenField::decode(bit_field_); }
2325 Expression* target() const { return target_; }
2326 Expression* value() const { return value_; }
2327 BinaryOperation* binary_operation() const { return binary_operation_; }
2329 // This check relies on the definition order of token in token.h.
2330 bool is_compound() const { return op() > Token::ASSIGN; }
2332 static int num_ids() { return parent_num_ids() + 2; }
2333 BailoutId AssignmentId() const { return BailoutId(local_id(0)); }
2335 // Type feedback information.
2336 TypeFeedbackId AssignmentFeedbackId() { return TypeFeedbackId(local_id(1)); }
2337 bool IsMonomorphic() override { return receiver_types_.length() == 1; }
2338 bool IsUninitialized() const {
2339 return IsUninitializedField::decode(bit_field_);
2341 bool HasNoTypeInformation() {
2342 return IsUninitializedField::decode(bit_field_);
2344 SmallMapList* GetReceiverTypes() override { return &receiver_types_; }
2345 IcCheckType GetKeyType() const override {
2346 return KeyTypeField::decode(bit_field_);
2348 KeyedAccessStoreMode GetStoreMode() const override {
2349 return StoreModeField::decode(bit_field_);
2351 void set_is_uninitialized(bool b) {
2352 bit_field_ = IsUninitializedField::update(bit_field_, b);
2354 void set_key_type(IcCheckType key_type) {
2355 bit_field_ = KeyTypeField::update(bit_field_, key_type);
2357 void set_store_mode(KeyedAccessStoreMode mode) {
2358 bit_field_ = StoreModeField::update(bit_field_, mode);
2361 FeedbackVectorRequirements ComputeFeedbackRequirements(
2362 Isolate* isolate, const ICSlotCache* cache) override;
2363 void SetFirstFeedbackICSlot(FeedbackVectorICSlot slot,
2364 ICSlotCache* cache) override {
2367 Code::Kind FeedbackICSlotKind(int index) override;
2368 FeedbackVectorICSlot AssignmentSlot() const { return slot_; }
2371 Assignment(Zone* zone, Token::Value op, Expression* target, Expression* value,
2373 static int parent_num_ids() { return Expression::num_ids(); }
2376 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
2378 class IsUninitializedField : public BitField16<bool, 0, 1> {};
2379 class KeyTypeField : public BitField16<IcCheckType, 1, 1> {};
2380 class StoreModeField : public BitField16<KeyedAccessStoreMode, 2, 4> {};
2381 class TokenField : public BitField16<Token::Value, 6, 8> {};
2383 // Starts with 16-bit field, which should get packed together with
2384 // Expression's trailing 16-bit field.
2385 uint16_t bit_field_;
2386 Expression* target_;
2388 BinaryOperation* binary_operation_;
2389 SmallMapList receiver_types_;
2390 FeedbackVectorICSlot slot_;
2394 class Yield final : public Expression {
2396 DECLARE_NODE_TYPE(Yield)
2399 kInitial, // The initial yield that returns the unboxed generator object.
2400 kSuspend, // A normal yield: { value: EXPRESSION, done: false }
2401 kDelegating, // A yield*.
2402 kFinal // A return: { value: EXPRESSION, done: true }
2405 Expression* generator_object() const { return generator_object_; }
2406 Expression* expression() const { return expression_; }
2407 Kind yield_kind() const { return yield_kind_; }
2409 // Delegating yield surrounds the "yield" in a "try/catch". This index
2410 // locates the catch handler in the handler table, and is equivalent to
2411 // TryCatchStatement::index().
2413 DCHECK_EQ(kDelegating, yield_kind());
2416 void set_index(int index) {
2417 DCHECK_EQ(kDelegating, yield_kind());
2421 // Type feedback information.
2422 bool HasFeedbackSlots() const { return yield_kind() == kDelegating; }
2423 virtual FeedbackVectorRequirements ComputeFeedbackRequirements(
2424 Isolate* isolate, const ICSlotCache* cache) override {
2425 return FeedbackVectorRequirements(0, HasFeedbackSlots() ? 3 : 0);
2427 void SetFirstFeedbackICSlot(FeedbackVectorICSlot slot,
2428 ICSlotCache* cache) override {
2429 yield_first_feedback_slot_ = slot;
2431 Code::Kind FeedbackICSlotKind(int index) override {
2432 return index == 0 ? Code::KEYED_LOAD_IC : Code::LOAD_IC;
2435 FeedbackVectorICSlot KeyedLoadFeedbackSlot() {
2436 DCHECK(!HasFeedbackSlots() || !yield_first_feedback_slot_.IsInvalid());
2437 return yield_first_feedback_slot_;
2440 FeedbackVectorICSlot DoneFeedbackSlot() {
2441 return KeyedLoadFeedbackSlot().next();
2444 FeedbackVectorICSlot ValueFeedbackSlot() { return DoneFeedbackSlot().next(); }
2447 Yield(Zone* zone, Expression* generator_object, Expression* expression,
2448 Kind yield_kind, int pos)
2449 : Expression(zone, pos),
2450 generator_object_(generator_object),
2451 expression_(expression),
2452 yield_kind_(yield_kind),
2454 yield_first_feedback_slot_(FeedbackVectorICSlot::Invalid()) {}
2457 Expression* generator_object_;
2458 Expression* expression_;
2461 FeedbackVectorICSlot yield_first_feedback_slot_;
2465 class Throw final : public Expression {
2467 DECLARE_NODE_TYPE(Throw)
2469 Expression* exception() const { return exception_; }
2472 Throw(Zone* zone, Expression* exception, int pos)
2473 : Expression(zone, pos), exception_(exception) {}
2476 Expression* exception_;
2480 class FunctionLiteral final : public Expression {
2483 ANONYMOUS_EXPRESSION,
2488 enum ParameterFlag {
2489 kNoDuplicateParameters = 0,
2490 kHasDuplicateParameters = 1
2493 enum IsFunctionFlag {
2498 enum EagerCompileHint { kShouldEagerCompile, kShouldLazyCompile };
2500 enum ShouldBeUsedOnceHint { kShouldBeUsedOnce, kDontKnowIfShouldBeUsedOnce };
2502 enum ArityRestriction {
2508 DECLARE_NODE_TYPE(FunctionLiteral)
2510 Handle<String> name() const { return raw_name_->string(); }
2511 const AstRawString* raw_name() const { return raw_name_; }
2512 Scope* scope() const { return scope_; }
2513 ZoneList<Statement*>* body() const { return body_; }
2514 void set_function_token_position(int pos) { function_token_position_ = pos; }
2515 int function_token_position() const { return function_token_position_; }
2516 int start_position() const;
2517 int end_position() const;
2518 int SourceSize() const { return end_position() - start_position(); }
2519 bool is_expression() const { return IsExpression::decode(bitfield_); }
2520 bool is_anonymous() const { return IsAnonymous::decode(bitfield_); }
2521 LanguageMode language_mode() const;
2522 bool uses_super_property() const;
2524 static bool NeedsHomeObject(Expression* literal) {
2525 return literal != NULL && literal->IsFunctionLiteral() &&
2526 literal->AsFunctionLiteral()->uses_super_property();
2529 int materialized_literal_count() { return materialized_literal_count_; }
2530 int expected_property_count() { return expected_property_count_; }
2531 int handler_count() { return handler_count_; }
2532 int parameter_count() { return parameter_count_; }
2534 bool AllowsLazyCompilation();
2535 bool AllowsLazyCompilationWithoutContext();
2537 void InitializeSharedInfo(Handle<Code> code);
2539 Handle<String> debug_name() const {
2540 if (raw_name_ != NULL && !raw_name_->IsEmpty()) {
2541 return raw_name_->string();
2543 return inferred_name();
2546 Handle<String> inferred_name() const {
2547 if (!inferred_name_.is_null()) {
2548 DCHECK(raw_inferred_name_ == NULL);
2549 return inferred_name_;
2551 if (raw_inferred_name_ != NULL) {
2552 return raw_inferred_name_->string();
2555 return Handle<String>();
2558 // Only one of {set_inferred_name, set_raw_inferred_name} should be called.
2559 void set_inferred_name(Handle<String> inferred_name) {
2560 DCHECK(!inferred_name.is_null());
2561 inferred_name_ = inferred_name;
2562 DCHECK(raw_inferred_name_== NULL || raw_inferred_name_->IsEmpty());
2563 raw_inferred_name_ = NULL;
2566 void set_raw_inferred_name(const AstString* raw_inferred_name) {
2567 DCHECK(raw_inferred_name != NULL);
2568 raw_inferred_name_ = raw_inferred_name;
2569 DCHECK(inferred_name_.is_null());
2570 inferred_name_ = Handle<String>();
2573 // shared_info may be null if it's not cached in full code.
2574 Handle<SharedFunctionInfo> shared_info() { return shared_info_; }
2576 bool pretenure() { return Pretenure::decode(bitfield_); }
2577 void set_pretenure() { bitfield_ |= Pretenure::encode(true); }
2579 bool has_duplicate_parameters() {
2580 return HasDuplicateParameters::decode(bitfield_);
2583 bool is_function() { return IsFunction::decode(bitfield_) == kIsFunction; }
2585 // This is used as a heuristic on when to eagerly compile a function
2586 // literal. We consider the following constructs as hints that the
2587 // function will be called immediately:
2588 // - (function() { ... })();
2589 // - var x = function() { ... }();
2590 bool should_eager_compile() const {
2591 return EagerCompileHintBit::decode(bitfield_) == kShouldEagerCompile;
2593 void set_should_eager_compile() {
2594 bitfield_ = EagerCompileHintBit::update(bitfield_, kShouldEagerCompile);
2597 // A hint that we expect this function to be called (exactly) once,
2598 // i.e. we suspect it's an initialization function.
2599 bool should_be_used_once_hint() const {
2600 return ShouldBeUsedOnceHintBit::decode(bitfield_) == kShouldBeUsedOnce;
2602 void set_should_be_used_once_hint() {
2603 bitfield_ = ShouldBeUsedOnceHintBit::update(bitfield_, kShouldBeUsedOnce);
2606 FunctionKind kind() { return FunctionKindBits::decode(bitfield_); }
2608 int ast_node_count() { return ast_properties_.node_count(); }
2609 AstProperties::Flags* flags() { return ast_properties_.flags(); }
2610 void set_ast_properties(AstProperties* ast_properties) {
2611 ast_properties_ = *ast_properties;
2613 const ZoneFeedbackVectorSpec* feedback_vector_spec() const {
2614 return ast_properties_.get_spec();
2616 bool dont_optimize() { return dont_optimize_reason_ != kNoReason; }
2617 BailoutReason dont_optimize_reason() { return dont_optimize_reason_; }
2618 void set_dont_optimize_reason(BailoutReason reason) {
2619 dont_optimize_reason_ = reason;
2622 static int num_ids() { return parent_num_ids() + 1; }
2623 TypeFeedbackId HomeObjectFeedbackId() { return TypeFeedbackId(local_id(0)); }
2625 // Type feedback information.
2626 virtual FeedbackVectorRequirements ComputeFeedbackRequirements(
2627 Isolate* isolate, const ICSlotCache* cache) override {
2628 return FeedbackVectorRequirements(0, 1);
2630 void SetFirstFeedbackICSlot(FeedbackVectorICSlot slot,
2631 ICSlotCache* cache) override {
2632 DCHECK(!slot.IsInvalid());
2633 home_object_feedback_slot_ = slot;
2635 Code::Kind FeedbackICSlotKind(int index) override { return Code::LOAD_IC; }
2637 FeedbackVectorICSlot HomeObjectFeedbackSlot() {
2638 DCHECK(!home_object_feedback_slot_.IsInvalid());
2639 return home_object_feedback_slot_;
2643 FunctionLiteral(Zone* zone, const AstRawString* name,
2644 AstValueFactory* ast_value_factory, Scope* scope,
2645 ZoneList<Statement*>* body, int materialized_literal_count,
2646 int expected_property_count, int handler_count,
2647 int parameter_count, FunctionType function_type,
2648 ParameterFlag has_duplicate_parameters,
2649 IsFunctionFlag is_function,
2650 EagerCompileHint eager_compile_hint, FunctionKind kind,
2652 : Expression(zone, position),
2656 raw_inferred_name_(ast_value_factory->empty_string()),
2657 ast_properties_(zone),
2658 dont_optimize_reason_(kNoReason),
2659 materialized_literal_count_(materialized_literal_count),
2660 expected_property_count_(expected_property_count),
2661 handler_count_(handler_count),
2662 parameter_count_(parameter_count),
2663 function_token_position_(RelocInfo::kNoPosition),
2664 home_object_feedback_slot_(FeedbackVectorICSlot::Invalid()) {
2665 bitfield_ = IsExpression::encode(function_type != DECLARATION) |
2666 IsAnonymous::encode(function_type == ANONYMOUS_EXPRESSION) |
2667 Pretenure::encode(false) |
2668 HasDuplicateParameters::encode(has_duplicate_parameters) |
2669 IsFunction::encode(is_function) |
2670 EagerCompileHintBit::encode(eager_compile_hint) |
2671 FunctionKindBits::encode(kind) |
2672 ShouldBeUsedOnceHintBit::encode(kDontKnowIfShouldBeUsedOnce);
2673 DCHECK(IsValidFunctionKind(kind));
2676 static int parent_num_ids() { return Expression::num_ids(); }
2679 const AstRawString* raw_name_;
2680 Handle<String> name_;
2681 Handle<SharedFunctionInfo> shared_info_;
2683 ZoneList<Statement*>* body_;
2684 const AstString* raw_inferred_name_;
2685 Handle<String> inferred_name_;
2686 AstProperties ast_properties_;
2687 BailoutReason dont_optimize_reason_;
2689 int materialized_literal_count_;
2690 int expected_property_count_;
2692 int parameter_count_;
2693 int function_token_position_;
2695 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
2696 FeedbackVectorICSlot home_object_feedback_slot_;
2699 class IsExpression : public BitField<bool, 0, 1> {};
2700 class IsAnonymous : public BitField<bool, 1, 1> {};
2701 class Pretenure : public BitField<bool, 2, 1> {};
2702 class HasDuplicateParameters : public BitField<ParameterFlag, 3, 1> {};
2703 class IsFunction : public BitField<IsFunctionFlag, 4, 1> {};
2704 class EagerCompileHintBit : public BitField<EagerCompileHint, 5, 1> {};
2705 class FunctionKindBits : public BitField<FunctionKind, 6, 8> {};
2706 class ShouldBeUsedOnceHintBit : public BitField<ShouldBeUsedOnceHint, 15, 1> {
2711 class ClassLiteral final : public Expression {
2713 typedef ObjectLiteralProperty Property;
2715 DECLARE_NODE_TYPE(ClassLiteral)
2717 Handle<String> name() const { return raw_name_->string(); }
2718 const AstRawString* raw_name() const { return raw_name_; }
2719 Scope* scope() const { return scope_; }
2720 VariableProxy* class_variable_proxy() const { return class_variable_proxy_; }
2721 Expression* extends() const { return extends_; }
2722 FunctionLiteral* constructor() const { return constructor_; }
2723 ZoneList<Property*>* properties() const { return properties_; }
2724 int start_position() const { return position(); }
2725 int end_position() const { return end_position_; }
2727 BailoutId EntryId() const { return BailoutId(local_id(0)); }
2728 BailoutId DeclsId() const { return BailoutId(local_id(1)); }
2729 BailoutId ExitId() { return BailoutId(local_id(2)); }
2730 BailoutId CreateLiteralId() const { return BailoutId(local_id(3)); }
2732 // Return an AST id for a property that is used in simulate instructions.
2733 BailoutId GetIdForProperty(int i) { return BailoutId(local_id(i + 4)); }
2735 // Unlike other AST nodes, this number of bailout IDs allocated for an
2736 // ClassLiteral can vary, so num_ids() is not a static method.
2737 int num_ids() const { return parent_num_ids() + 4 + properties()->length(); }
2740 ClassLiteral(Zone* zone, const AstRawString* name, Scope* scope,
2741 VariableProxy* class_variable_proxy, Expression* extends,
2742 FunctionLiteral* constructor, ZoneList<Property*>* properties,
2743 int start_position, int end_position)
2744 : Expression(zone, start_position),
2747 class_variable_proxy_(class_variable_proxy),
2749 constructor_(constructor),
2750 properties_(properties),
2751 end_position_(end_position) {}
2752 static int parent_num_ids() { return Expression::num_ids(); }
2755 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
2757 const AstRawString* raw_name_;
2759 VariableProxy* class_variable_proxy_;
2760 Expression* extends_;
2761 FunctionLiteral* constructor_;
2762 ZoneList<Property*>* properties_;
2767 class NativeFunctionLiteral final : public Expression {
2769 DECLARE_NODE_TYPE(NativeFunctionLiteral)
2771 Handle<String> name() const { return name_->string(); }
2772 v8::Extension* extension() const { return extension_; }
2775 NativeFunctionLiteral(Zone* zone, const AstRawString* name,
2776 v8::Extension* extension, int pos)
2777 : Expression(zone, pos), name_(name), extension_(extension) {}
2780 const AstRawString* name_;
2781 v8::Extension* extension_;
2785 class ThisFunction final : public Expression {
2787 DECLARE_NODE_TYPE(ThisFunction)
2790 ThisFunction(Zone* zone, int pos) : Expression(zone, pos) {}
2794 class SuperReference final : public Expression {
2796 DECLARE_NODE_TYPE(SuperReference)
2798 VariableProxy* this_var() const { return this_var_; }
2799 VariableProxy* home_object_var() const { return home_object_var_; }
2802 SuperReference(Zone* zone, VariableProxy* this_var,
2803 VariableProxy* home_object_var, int pos)
2804 : Expression(zone, pos),
2805 this_var_(this_var),
2806 home_object_var_(home_object_var) {
2807 DCHECK(this_var->is_this());
2808 DCHECK(home_object_var->raw_name()->IsOneByteEqualTo(".home_object"));
2812 VariableProxy* this_var_;
2813 VariableProxy* home_object_var_;
2817 #undef DECLARE_NODE_TYPE
2820 // ----------------------------------------------------------------------------
2821 // Regular expressions
2824 class RegExpVisitor BASE_EMBEDDED {
2826 virtual ~RegExpVisitor() { }
2827 #define MAKE_CASE(Name) \
2828 virtual void* Visit##Name(RegExp##Name*, void* data) = 0;
2829 FOR_EACH_REG_EXP_TREE_TYPE(MAKE_CASE)
2834 class RegExpTree : public ZoneObject {
2836 static const int kInfinity = kMaxInt;
2837 virtual ~RegExpTree() {}
2838 virtual void* Accept(RegExpVisitor* visitor, void* data) = 0;
2839 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
2840 RegExpNode* on_success) = 0;
2841 virtual bool IsTextElement() { return false; }
2842 virtual bool IsAnchoredAtStart() { return false; }
2843 virtual bool IsAnchoredAtEnd() { return false; }
2844 virtual int min_match() = 0;
2845 virtual int max_match() = 0;
2846 // Returns the interval of registers used for captures within this
2848 virtual Interval CaptureRegisters() { return Interval::Empty(); }
2849 virtual void AppendToText(RegExpText* text, Zone* zone);
2850 std::ostream& Print(std::ostream& os, Zone* zone); // NOLINT
2851 #define MAKE_ASTYPE(Name) \
2852 virtual RegExp##Name* As##Name(); \
2853 virtual bool Is##Name();
2854 FOR_EACH_REG_EXP_TREE_TYPE(MAKE_ASTYPE)
2859 class RegExpDisjunction final : public RegExpTree {
2861 explicit RegExpDisjunction(ZoneList<RegExpTree*>* alternatives);
2862 void* Accept(RegExpVisitor* visitor, void* data) override;
2863 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
2864 RegExpNode* on_success) override;
2865 RegExpDisjunction* AsDisjunction() override;
2866 Interval CaptureRegisters() override;
2867 bool IsDisjunction() override;
2868 bool IsAnchoredAtStart() override;
2869 bool IsAnchoredAtEnd() override;
2870 int min_match() override { return min_match_; }
2871 int max_match() override { return max_match_; }
2872 ZoneList<RegExpTree*>* alternatives() { return alternatives_; }
2874 ZoneList<RegExpTree*>* alternatives_;
2880 class RegExpAlternative final : public RegExpTree {
2882 explicit RegExpAlternative(ZoneList<RegExpTree*>* nodes);
2883 void* Accept(RegExpVisitor* visitor, void* data) override;
2884 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
2885 RegExpNode* on_success) override;
2886 RegExpAlternative* AsAlternative() override;
2887 Interval CaptureRegisters() override;
2888 bool IsAlternative() override;
2889 bool IsAnchoredAtStart() override;
2890 bool IsAnchoredAtEnd() override;
2891 int min_match() override { return min_match_; }
2892 int max_match() override { return max_match_; }
2893 ZoneList<RegExpTree*>* nodes() { return nodes_; }
2895 ZoneList<RegExpTree*>* nodes_;
2901 class RegExpAssertion final : public RegExpTree {
2903 enum AssertionType {
2911 explicit RegExpAssertion(AssertionType type) : assertion_type_(type) { }
2912 void* Accept(RegExpVisitor* visitor, void* data) override;
2913 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
2914 RegExpNode* on_success) override;
2915 RegExpAssertion* AsAssertion() override;
2916 bool IsAssertion() override;
2917 bool IsAnchoredAtStart() override;
2918 bool IsAnchoredAtEnd() override;
2919 int min_match() override { return 0; }
2920 int max_match() override { return 0; }
2921 AssertionType assertion_type() { return assertion_type_; }
2923 AssertionType assertion_type_;
2927 class CharacterSet final BASE_EMBEDDED {
2929 explicit CharacterSet(uc16 standard_set_type)
2931 standard_set_type_(standard_set_type) {}
2932 explicit CharacterSet(ZoneList<CharacterRange>* ranges)
2934 standard_set_type_(0) {}
2935 ZoneList<CharacterRange>* ranges(Zone* zone);
2936 uc16 standard_set_type() { return standard_set_type_; }
2937 void set_standard_set_type(uc16 special_set_type) {
2938 standard_set_type_ = special_set_type;
2940 bool is_standard() { return standard_set_type_ != 0; }
2941 void Canonicalize();
2943 ZoneList<CharacterRange>* ranges_;
2944 // If non-zero, the value represents a standard set (e.g., all whitespace
2945 // characters) without having to expand the ranges.
2946 uc16 standard_set_type_;
2950 class RegExpCharacterClass final : public RegExpTree {
2952 RegExpCharacterClass(ZoneList<CharacterRange>* ranges, bool is_negated)
2954 is_negated_(is_negated) { }
2955 explicit RegExpCharacterClass(uc16 type)
2957 is_negated_(false) { }
2958 void* Accept(RegExpVisitor* visitor, void* data) override;
2959 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
2960 RegExpNode* on_success) override;
2961 RegExpCharacterClass* AsCharacterClass() override;
2962 bool IsCharacterClass() override;
2963 bool IsTextElement() override { return true; }
2964 int min_match() override { return 1; }
2965 int max_match() override { return 1; }
2966 void AppendToText(RegExpText* text, Zone* zone) override;
2967 CharacterSet character_set() { return set_; }
2968 // TODO(lrn): Remove need for complex version if is_standard that
2969 // recognizes a mangled standard set and just do { return set_.is_special(); }
2970 bool is_standard(Zone* zone);
2971 // Returns a value representing the standard character set if is_standard()
2973 // Currently used values are:
2974 // s : unicode whitespace
2975 // S : unicode non-whitespace
2976 // w : ASCII word character (digit, letter, underscore)
2977 // W : non-ASCII word character
2979 // D : non-ASCII digit
2980 // . : non-unicode non-newline
2981 // * : All characters
2982 uc16 standard_type() { return set_.standard_set_type(); }
2983 ZoneList<CharacterRange>* ranges(Zone* zone) { return set_.ranges(zone); }
2984 bool is_negated() { return is_negated_; }
2992 class RegExpAtom final : public RegExpTree {
2994 explicit RegExpAtom(Vector<const uc16> data) : data_(data) { }
2995 void* Accept(RegExpVisitor* visitor, void* data) override;
2996 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
2997 RegExpNode* on_success) override;
2998 RegExpAtom* AsAtom() override;
2999 bool IsAtom() override;
3000 bool IsTextElement() override { return true; }
3001 int min_match() override { return data_.length(); }
3002 int max_match() override { return data_.length(); }
3003 void AppendToText(RegExpText* text, Zone* zone) override;
3004 Vector<const uc16> data() { return data_; }
3005 int length() { return data_.length(); }
3007 Vector<const uc16> data_;
3011 class RegExpText final : public RegExpTree {
3013 explicit RegExpText(Zone* zone) : elements_(2, zone), length_(0) {}
3014 void* Accept(RegExpVisitor* visitor, void* data) override;
3015 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
3016 RegExpNode* on_success) override;
3017 RegExpText* AsText() override;
3018 bool IsText() override;
3019 bool IsTextElement() override { return true; }
3020 int min_match() override { return length_; }
3021 int max_match() override { return length_; }
3022 void AppendToText(RegExpText* text, Zone* zone) override;
3023 void AddElement(TextElement elm, Zone* zone) {
3024 elements_.Add(elm, zone);
3025 length_ += elm.length();
3027 ZoneList<TextElement>* elements() { return &elements_; }
3029 ZoneList<TextElement> elements_;
3034 class RegExpQuantifier final : public RegExpTree {
3036 enum QuantifierType { GREEDY, NON_GREEDY, POSSESSIVE };
3037 RegExpQuantifier(int min, int max, QuantifierType type, RegExpTree* body)
3041 min_match_(min * body->min_match()),
3042 quantifier_type_(type) {
3043 if (max > 0 && body->max_match() > kInfinity / max) {
3044 max_match_ = kInfinity;
3046 max_match_ = max * body->max_match();
3049 void* Accept(RegExpVisitor* visitor, void* data) override;
3050 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
3051 RegExpNode* on_success) override;
3052 static RegExpNode* ToNode(int min,
3056 RegExpCompiler* compiler,
3057 RegExpNode* on_success,
3058 bool not_at_start = false);
3059 RegExpQuantifier* AsQuantifier() override;
3060 Interval CaptureRegisters() override;
3061 bool IsQuantifier() override;
3062 int min_match() override { return min_match_; }
3063 int max_match() override { return max_match_; }
3064 int min() { return min_; }
3065 int max() { return max_; }
3066 bool is_possessive() { return quantifier_type_ == POSSESSIVE; }
3067 bool is_non_greedy() { return quantifier_type_ == NON_GREEDY; }
3068 bool is_greedy() { return quantifier_type_ == GREEDY; }
3069 RegExpTree* body() { return body_; }
3077 QuantifierType quantifier_type_;
3081 class RegExpCapture final : public RegExpTree {
3083 explicit RegExpCapture(RegExpTree* body, int index)
3084 : body_(body), index_(index) { }
3085 void* Accept(RegExpVisitor* visitor, void* data) override;
3086 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
3087 RegExpNode* on_success) override;
3088 static RegExpNode* ToNode(RegExpTree* body,
3090 RegExpCompiler* compiler,
3091 RegExpNode* on_success);
3092 RegExpCapture* AsCapture() override;
3093 bool IsAnchoredAtStart() override;
3094 bool IsAnchoredAtEnd() override;
3095 Interval CaptureRegisters() override;
3096 bool IsCapture() override;
3097 int min_match() override { return body_->min_match(); }
3098 int max_match() override { return body_->max_match(); }
3099 RegExpTree* body() { return body_; }
3100 int index() { return index_; }
3101 static int StartRegister(int index) { return index * 2; }
3102 static int EndRegister(int index) { return index * 2 + 1; }
3110 class RegExpLookahead final : public RegExpTree {
3112 RegExpLookahead(RegExpTree* body,
3117 is_positive_(is_positive),
3118 capture_count_(capture_count),
3119 capture_from_(capture_from) { }
3121 void* Accept(RegExpVisitor* visitor, void* data) override;
3122 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
3123 RegExpNode* on_success) override;
3124 RegExpLookahead* AsLookahead() override;
3125 Interval CaptureRegisters() override;
3126 bool IsLookahead() override;
3127 bool IsAnchoredAtStart() override;
3128 int min_match() override { return 0; }
3129 int max_match() override { return 0; }
3130 RegExpTree* body() { return body_; }
3131 bool is_positive() { return is_positive_; }
3132 int capture_count() { return capture_count_; }
3133 int capture_from() { return capture_from_; }
3143 class RegExpBackReference final : public RegExpTree {
3145 explicit RegExpBackReference(RegExpCapture* capture)
3146 : capture_(capture) { }
3147 void* Accept(RegExpVisitor* visitor, void* data) override;
3148 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
3149 RegExpNode* on_success) override;
3150 RegExpBackReference* AsBackReference() override;
3151 bool IsBackReference() override;
3152 int min_match() override { return 0; }
3153 int max_match() override { return capture_->max_match(); }
3154 int index() { return capture_->index(); }
3155 RegExpCapture* capture() { return capture_; }
3157 RegExpCapture* capture_;
3161 class RegExpEmpty final : public RegExpTree {
3164 void* Accept(RegExpVisitor* visitor, void* data) override;
3165 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
3166 RegExpNode* on_success) override;
3167 RegExpEmpty* AsEmpty() override;
3168 bool IsEmpty() override;
3169 int min_match() override { return 0; }
3170 int max_match() override { return 0; }
3174 // ----------------------------------------------------------------------------
3176 // - leaf node visitors are abstract.
3178 class AstVisitor BASE_EMBEDDED {
3181 virtual ~AstVisitor() {}
3183 // Stack overflow check and dynamic dispatch.
3184 virtual void Visit(AstNode* node) = 0;
3186 // Iteration left-to-right.
3187 virtual void VisitDeclarations(ZoneList<Declaration*>* declarations);
3188 virtual void VisitStatements(ZoneList<Statement*>* statements);
3189 virtual void VisitExpressions(ZoneList<Expression*>* expressions);
3191 // Individual AST nodes.
3192 #define DEF_VISIT(type) \
3193 virtual void Visit##type(type* node) = 0;
3194 AST_NODE_LIST(DEF_VISIT)
3199 #define DEFINE_AST_VISITOR_SUBCLASS_MEMBERS() \
3201 void Visit(AstNode* node) final { \
3202 if (!CheckStackOverflow()) node->Accept(this); \
3205 void SetStackOverflow() { stack_overflow_ = true; } \
3206 void ClearStackOverflow() { stack_overflow_ = false; } \
3207 bool HasStackOverflow() const { return stack_overflow_; } \
3209 bool CheckStackOverflow() { \
3210 if (stack_overflow_) return true; \
3211 StackLimitCheck check(isolate_); \
3212 if (!check.HasOverflowed()) return false; \
3213 stack_overflow_ = true; \
3218 void InitializeAstVisitor(Isolate* isolate, Zone* zone) { \
3219 isolate_ = isolate; \
3221 stack_overflow_ = false; \
3223 Zone* zone() { return zone_; } \
3224 Isolate* isolate() { return isolate_; } \
3226 Isolate* isolate_; \
3228 bool stack_overflow_
3231 // ----------------------------------------------------------------------------
3234 class AstNodeFactory final BASE_EMBEDDED {
3236 explicit AstNodeFactory(AstValueFactory* ast_value_factory)
3237 : zone_(ast_value_factory->zone()),
3238 ast_value_factory_(ast_value_factory) {}
3240 VariableDeclaration* NewVariableDeclaration(
3241 VariableProxy* proxy, VariableMode mode, Scope* scope, int pos,
3242 bool is_class_declaration = false, int declaration_group_start = -1) {
3244 VariableDeclaration(zone_, proxy, mode, scope, pos,
3245 is_class_declaration, declaration_group_start);
3248 FunctionDeclaration* NewFunctionDeclaration(VariableProxy* proxy,
3250 FunctionLiteral* fun,
3253 return new (zone_) FunctionDeclaration(zone_, proxy, mode, fun, scope, pos);
3256 ImportDeclaration* NewImportDeclaration(VariableProxy* proxy,
3257 const AstRawString* import_name,
3258 const AstRawString* module_specifier,
3259 Scope* scope, int pos) {
3260 return new (zone_) ImportDeclaration(zone_, proxy, import_name,
3261 module_specifier, scope, pos);
3264 ExportDeclaration* NewExportDeclaration(VariableProxy* proxy,
3267 return new (zone_) ExportDeclaration(zone_, proxy, scope, pos);
3270 Block* NewBlock(ZoneList<const AstRawString*>* labels,
3272 bool is_initializer_block,
3275 Block(zone_, labels, capacity, is_initializer_block, pos);
3278 #define STATEMENT_WITH_LABELS(NodeType) \
3279 NodeType* New##NodeType(ZoneList<const AstRawString*>* labels, int pos) { \
3280 return new (zone_) NodeType(zone_, labels, pos); \
3282 STATEMENT_WITH_LABELS(DoWhileStatement)
3283 STATEMENT_WITH_LABELS(WhileStatement)
3284 STATEMENT_WITH_LABELS(ForStatement)
3285 STATEMENT_WITH_LABELS(SwitchStatement)
3286 #undef STATEMENT_WITH_LABELS
3288 ForEachStatement* NewForEachStatement(ForEachStatement::VisitMode visit_mode,
3289 ZoneList<const AstRawString*>* labels,
3291 switch (visit_mode) {
3292 case ForEachStatement::ENUMERATE: {
3293 return new (zone_) ForInStatement(zone_, labels, pos);
3295 case ForEachStatement::ITERATE: {
3296 return new (zone_) ForOfStatement(zone_, labels, pos);
3303 ExpressionStatement* NewExpressionStatement(Expression* expression, int pos) {
3304 return new (zone_) ExpressionStatement(zone_, expression, pos);
3307 ContinueStatement* NewContinueStatement(IterationStatement* target, int pos) {
3308 return new (zone_) ContinueStatement(zone_, target, pos);
3311 BreakStatement* NewBreakStatement(BreakableStatement* target, int pos) {
3312 return new (zone_) BreakStatement(zone_, target, pos);
3315 ReturnStatement* NewReturnStatement(Expression* expression, int pos) {
3316 return new (zone_) ReturnStatement(zone_, expression, pos);
3319 WithStatement* NewWithStatement(Scope* scope,
3320 Expression* expression,
3321 Statement* statement,
3323 return new (zone_) WithStatement(zone_, scope, expression, statement, pos);
3326 IfStatement* NewIfStatement(Expression* condition,
3327 Statement* then_statement,
3328 Statement* else_statement,
3331 IfStatement(zone_, condition, then_statement, else_statement, pos);
3334 TryCatchStatement* NewTryCatchStatement(int index,
3340 return new (zone_) TryCatchStatement(zone_, index, try_block, scope,
3341 variable, catch_block, pos);
3344 TryFinallyStatement* NewTryFinallyStatement(int index,
3346 Block* finally_block,
3349 TryFinallyStatement(zone_, index, try_block, finally_block, pos);
3352 DebuggerStatement* NewDebuggerStatement(int pos) {
3353 return new (zone_) DebuggerStatement(zone_, pos);
3356 EmptyStatement* NewEmptyStatement(int pos) {
3357 return new(zone_) EmptyStatement(zone_, pos);
3360 CaseClause* NewCaseClause(
3361 Expression* label, ZoneList<Statement*>* statements, int pos) {
3362 return new (zone_) CaseClause(zone_, label, statements, pos);
3365 Literal* NewStringLiteral(const AstRawString* string, int pos) {
3367 Literal(zone_, ast_value_factory_->NewString(string), pos);
3370 // A JavaScript symbol (ECMA-262 edition 6).
3371 Literal* NewSymbolLiteral(const char* name, int pos) {
3372 return new (zone_) Literal(zone_, ast_value_factory_->NewSymbol(name), pos);
3375 Literal* NewNumberLiteral(double number, int pos) {
3377 Literal(zone_, ast_value_factory_->NewNumber(number), pos);
3380 Literal* NewSmiLiteral(int number, int pos) {
3381 return new (zone_) Literal(zone_, ast_value_factory_->NewSmi(number), pos);
3384 Literal* NewBooleanLiteral(bool b, int pos) {
3385 return new (zone_) Literal(zone_, ast_value_factory_->NewBoolean(b), pos);
3388 Literal* NewNullLiteral(int pos) {
3389 return new (zone_) Literal(zone_, ast_value_factory_->NewNull(), pos);
3392 Literal* NewUndefinedLiteral(int pos) {
3393 return new (zone_) Literal(zone_, ast_value_factory_->NewUndefined(), pos);
3396 Literal* NewTheHoleLiteral(int pos) {
3397 return new (zone_) Literal(zone_, ast_value_factory_->NewTheHole(), pos);
3400 ObjectLiteral* NewObjectLiteral(
3401 ZoneList<ObjectLiteral::Property*>* properties,
3403 int boilerplate_properties,
3407 return new (zone_) ObjectLiteral(zone_, properties, literal_index,
3408 boilerplate_properties, has_function,
3412 ObjectLiteral::Property* NewObjectLiteralProperty(
3413 Expression* key, Expression* value, ObjectLiteralProperty::Kind kind,
3414 bool is_static, bool is_computed_name) {
3416 ObjectLiteral::Property(key, value, kind, is_static, is_computed_name);
3419 ObjectLiteral::Property* NewObjectLiteralProperty(Expression* key,
3422 bool is_computed_name) {
3423 return new (zone_) ObjectLiteral::Property(ast_value_factory_, key, value,
3424 is_static, is_computed_name);
3427 RegExpLiteral* NewRegExpLiteral(const AstRawString* pattern,
3428 const AstRawString* flags,
3432 return new (zone_) RegExpLiteral(zone_, pattern, flags, literal_index,
3436 ArrayLiteral* NewArrayLiteral(ZoneList<Expression*>* values,
3440 return new (zone_) ArrayLiteral(zone_, values, literal_index, is_strong,
3444 VariableProxy* NewVariableProxy(Variable* var,
3445 int start_position = RelocInfo::kNoPosition,
3446 int end_position = RelocInfo::kNoPosition) {
3447 return new (zone_) VariableProxy(zone_, var, start_position, end_position);
3450 VariableProxy* NewVariableProxy(const AstRawString* name,
3451 Variable::Kind variable_kind,
3452 int start_position = RelocInfo::kNoPosition,
3453 int end_position = RelocInfo::kNoPosition) {
3454 DCHECK_NOT_NULL(name);
3456 VariableProxy(zone_, name, variable_kind, start_position, end_position);
3459 Property* NewProperty(Expression* obj, Expression* key, int pos) {
3460 return new (zone_) Property(zone_, obj, key, pos);
3463 Call* NewCall(Expression* expression,
3464 ZoneList<Expression*>* arguments,
3466 return new (zone_) Call(zone_, expression, arguments, pos);
3469 CallNew* NewCallNew(Expression* expression,
3470 ZoneList<Expression*>* arguments,
3472 return new (zone_) CallNew(zone_, expression, arguments, pos);
3475 CallRuntime* NewCallRuntime(const AstRawString* name,
3476 const Runtime::Function* function,
3477 ZoneList<Expression*>* arguments,
3479 return new (zone_) CallRuntime(zone_, name, function, arguments, pos);
3482 UnaryOperation* NewUnaryOperation(Token::Value op,
3483 Expression* expression,
3485 return new (zone_) UnaryOperation(zone_, op, expression, pos);
3488 BinaryOperation* NewBinaryOperation(Token::Value op,
3492 return new (zone_) BinaryOperation(zone_, op, left, right, pos);
3495 CountOperation* NewCountOperation(Token::Value op,
3499 return new (zone_) CountOperation(zone_, op, is_prefix, expr, pos);
3502 CompareOperation* NewCompareOperation(Token::Value op,
3506 return new (zone_) CompareOperation(zone_, op, left, right, pos);
3509 Spread* NewSpread(Expression* expression, int pos) {
3510 return new (zone_) Spread(zone_, expression, pos);
3513 Conditional* NewConditional(Expression* condition,
3514 Expression* then_expression,
3515 Expression* else_expression,
3517 return new (zone_) Conditional(zone_, condition, then_expression,
3518 else_expression, position);
3521 Assignment* NewAssignment(Token::Value op,
3525 DCHECK(Token::IsAssignmentOp(op));
3526 Assignment* assign = new (zone_) Assignment(zone_, op, target, value, pos);
3527 if (assign->is_compound()) {
3528 DCHECK(Token::IsAssignmentOp(op));
3529 assign->binary_operation_ =
3530 NewBinaryOperation(assign->binary_op(), target, value, pos + 1);
3535 Yield* NewYield(Expression *generator_object,
3536 Expression* expression,
3537 Yield::Kind yield_kind,
3539 if (!expression) expression = NewUndefinedLiteral(pos);
3541 Yield(zone_, generator_object, expression, yield_kind, pos);
3544 Throw* NewThrow(Expression* exception, int pos) {
3545 return new (zone_) Throw(zone_, exception, pos);
3548 FunctionLiteral* NewFunctionLiteral(
3549 const AstRawString* name, AstValueFactory* ast_value_factory,
3550 Scope* scope, ZoneList<Statement*>* body, int materialized_literal_count,
3551 int expected_property_count, int handler_count, int parameter_count,
3552 FunctionLiteral::ParameterFlag has_duplicate_parameters,
3553 FunctionLiteral::FunctionType function_type,
3554 FunctionLiteral::IsFunctionFlag is_function,
3555 FunctionLiteral::EagerCompileHint eager_compile_hint, FunctionKind kind,
3557 return new (zone_) FunctionLiteral(
3558 zone_, name, ast_value_factory, scope, body, materialized_literal_count,
3559 expected_property_count, handler_count, parameter_count, function_type,
3560 has_duplicate_parameters, is_function, eager_compile_hint, kind,
3564 ClassLiteral* NewClassLiteral(const AstRawString* name, Scope* scope,
3565 VariableProxy* proxy, Expression* extends,
3566 FunctionLiteral* constructor,
3567 ZoneList<ObjectLiteral::Property*>* properties,
3568 int start_position, int end_position) {
3570 ClassLiteral(zone_, name, scope, proxy, extends, constructor,
3571 properties, start_position, end_position);
3574 NativeFunctionLiteral* NewNativeFunctionLiteral(const AstRawString* name,
3575 v8::Extension* extension,
3577 return new (zone_) NativeFunctionLiteral(zone_, name, extension, pos);
3580 ThisFunction* NewThisFunction(int pos) {
3581 return new (zone_) ThisFunction(zone_, pos);
3584 SuperReference* NewSuperReference(VariableProxy* this_var,
3585 VariableProxy* home_object_var, int pos) {
3586 return new (zone_) SuperReference(zone_, this_var, home_object_var, pos);
3591 AstValueFactory* ast_value_factory_;
3595 } } // namespace v8::internal