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.
8 #include "src/assembler.h"
9 #include "src/ast-value-factory.h"
10 #include "src/bailout-reason.h"
11 #include "src/base/flags.h"
12 #include "src/base/smart-pointers.h"
13 #include "src/factory.h"
14 #include "src/isolate.h"
16 #include "src/modules.h"
17 #include "src/regexp/jsregexp.h"
18 #include "src/runtime/runtime.h"
19 #include "src/small-pointer-list.h"
20 #include "src/token.h"
21 #include "src/types.h"
22 #include "src/utils.h"
23 #include "src/variables.h"
28 // The abstract syntax tree is an intermediate, light-weight
29 // representation of the parsed JavaScript code suitable for
30 // compilation to native code.
32 // Nodes are allocated in a separate zone, which allows faster
33 // allocation and constant-time deallocation of the entire syntax
37 // ----------------------------------------------------------------------------
38 // Nodes of the abstract syntax tree. Only concrete classes are
41 #define DECLARATION_NODE_LIST(V) \
42 V(VariableDeclaration) \
43 V(FunctionDeclaration) \
44 V(ImportDeclaration) \
47 #define STATEMENT_NODE_LIST(V) \
49 V(ExpressionStatement) \
52 V(ContinueStatement) \
62 V(TryCatchStatement) \
63 V(TryFinallyStatement) \
66 #define EXPRESSION_NODE_LIST(V) \
69 V(NativeFunctionLiteral) \
89 V(SuperPropertyReference) \
90 V(SuperCallReference) \
94 #define AST_NODE_LIST(V) \
95 DECLARATION_NODE_LIST(V) \
96 STATEMENT_NODE_LIST(V) \
97 EXPRESSION_NODE_LIST(V)
99 // Forward declarations
100 class AstNodeFactory;
104 class BreakableStatement;
106 class IterationStatement;
107 class MaterializedLiteral;
109 class TypeFeedbackOracle;
111 class RegExpAlternative;
112 class RegExpAssertion;
114 class RegExpBackReference;
116 class RegExpCharacterClass;
117 class RegExpCompiler;
118 class RegExpDisjunction;
120 class RegExpLookahead;
121 class RegExpQuantifier;
124 #define DEF_FORWARD_DECLARATION(type) class type;
125 AST_NODE_LIST(DEF_FORWARD_DECLARATION)
126 #undef DEF_FORWARD_DECLARATION
129 // Typedef only introduced to avoid unreadable code.
130 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 class FeedbackVectorRequirements {
142 FeedbackVectorRequirements(int slots, int ic_slots)
143 : slots_(slots), ic_slots_(ic_slots) {}
145 int slots() const { return slots_; }
146 int ic_slots() const { return ic_slots_; }
156 explicit ICSlotCache(Zone* zone)
158 hash_map_(HashMap::PointersMatch, ZoneHashMap::kDefaultHashMapCapacity,
159 ZoneAllocationPolicy(zone)) {}
161 void Put(Variable* variable, FeedbackVectorICSlot slot) {
162 ZoneHashMap::Entry* entry = hash_map_.LookupOrInsert(
163 variable, ComputePointerHash(variable), ZoneAllocationPolicy(zone_));
164 entry->value = reinterpret_cast<void*>(slot.ToInt());
167 ZoneHashMap::Entry* Get(Variable* variable) const {
168 return hash_map_.Lookup(variable, ComputePointerHash(variable));
173 ZoneHashMap hash_map_;
177 class AstProperties final BASE_EMBEDDED {
181 kDontSelfOptimize = 1 << 0,
182 kDontCrankshaft = 1 << 1
185 typedef base::Flags<Flag> Flags;
187 explicit AstProperties(Zone* zone) : node_count_(0), spec_(zone) {}
189 Flags& flags() { return flags_; }
190 Flags flags() const { return flags_; }
191 int node_count() { return node_count_; }
192 void add_node_count(int count) { node_count_ += count; }
194 int slots() const { return spec_.slots(); }
195 void increase_slots(int count) { spec_.increase_slots(count); }
197 int ic_slots() const { return spec_.ic_slots(); }
198 void increase_ic_slots(int count) { spec_.increase_ic_slots(count); }
199 void SetKind(int ic_slot, Code::Kind kind) { spec_.SetKind(ic_slot, kind); }
200 const ZoneFeedbackVectorSpec* get_spec() const { return &spec_; }
205 ZoneFeedbackVectorSpec spec_;
208 DEFINE_OPERATORS_FOR_FLAGS(AstProperties::Flags)
211 class AstNode: public ZoneObject {
213 #define DECLARE_TYPE_ENUM(type) k##type,
215 AST_NODE_LIST(DECLARE_TYPE_ENUM)
218 #undef DECLARE_TYPE_ENUM
220 void* operator new(size_t size, Zone* zone) { return zone->New(size); }
222 explicit AstNode(int position): position_(position) {}
223 virtual ~AstNode() {}
225 virtual void Accept(AstVisitor* v) = 0;
226 virtual NodeType node_type() const = 0;
227 int position() const { return position_; }
229 // Type testing & conversion functions overridden by concrete subclasses.
230 #define DECLARE_NODE_FUNCTIONS(type) \
231 bool Is##type() const { return node_type() == AstNode::k##type; } \
233 return Is##type() ? reinterpret_cast<type*>(this) : NULL; \
235 const type* As##type() const { \
236 return Is##type() ? reinterpret_cast<const type*>(this) : NULL; \
238 AST_NODE_LIST(DECLARE_NODE_FUNCTIONS)
239 #undef DECLARE_NODE_FUNCTIONS
241 virtual BreakableStatement* AsBreakableStatement() { return NULL; }
242 virtual IterationStatement* AsIterationStatement() { return NULL; }
243 virtual MaterializedLiteral* AsMaterializedLiteral() { return NULL; }
245 // The interface for feedback slots, with default no-op implementations for
246 // node types which don't actually have this. Note that this is conceptually
247 // not really nice, but multiple inheritance would introduce yet another
248 // vtable entry per node, something we don't want for space reasons.
249 virtual FeedbackVectorRequirements ComputeFeedbackRequirements(
250 Isolate* isolate, const ICSlotCache* cache) {
251 return FeedbackVectorRequirements(0, 0);
253 virtual void SetFirstFeedbackSlot(FeedbackVectorSlot slot) { UNREACHABLE(); }
254 virtual void SetFirstFeedbackICSlot(FeedbackVectorICSlot slot,
255 ICSlotCache* cache) {
258 // Each ICSlot stores a kind of IC which the participating node should know.
259 virtual Code::Kind FeedbackICSlotKind(int index) {
261 return Code::NUMBER_OF_KINDS;
265 // Hidden to prevent accidental usage. It would have to load the
266 // current zone from the TLS.
267 void* operator new(size_t size);
269 friend class CaseClause; // Generates AST IDs.
275 class Statement : public AstNode {
277 explicit Statement(Zone* zone, int position) : AstNode(position) {}
279 bool IsEmpty() { return AsEmptyStatement() != NULL; }
280 virtual bool IsJump() const { return false; }
284 class SmallMapList final {
287 SmallMapList(int capacity, Zone* zone) : list_(capacity, zone) {}
289 void Reserve(int capacity, Zone* zone) { list_.Reserve(capacity, zone); }
290 void Clear() { list_.Clear(); }
291 void Sort() { list_.Sort(); }
293 bool is_empty() const { return list_.is_empty(); }
294 int length() const { return list_.length(); }
296 void AddMapIfMissing(Handle<Map> map, Zone* zone) {
297 if (!Map::TryUpdate(map).ToHandle(&map)) return;
298 for (int i = 0; i < length(); ++i) {
299 if (at(i).is_identical_to(map)) return;
304 void FilterForPossibleTransitions(Map* root_map) {
305 for (int i = list_.length() - 1; i >= 0; i--) {
306 if (at(i)->FindRootMap() != root_map) {
307 list_.RemoveElement(list_.at(i));
312 void Add(Handle<Map> handle, Zone* zone) {
313 list_.Add(handle.location(), zone);
316 Handle<Map> at(int i) const {
317 return Handle<Map>(list_.at(i));
320 Handle<Map> first() const { return at(0); }
321 Handle<Map> last() const { return at(length() - 1); }
324 // The list stores pointers to Map*, that is Map**, so it's GC safe.
325 SmallPointerList<Map*> list_;
327 DISALLOW_COPY_AND_ASSIGN(SmallMapList);
331 class Expression : public AstNode {
334 // Not assigned a context yet, or else will not be visited during
337 // Evaluated for its side effects.
339 // Evaluated for its value (and side effects).
341 // Evaluated for control flow (and side effects).
345 // True iff the expression is a valid reference expression.
346 virtual bool IsValidReferenceExpression() const { return false; }
348 // Helpers for ToBoolean conversion.
349 virtual bool ToBooleanIsTrue() const { return false; }
350 virtual bool ToBooleanIsFalse() const { return false; }
352 // Symbols that cannot be parsed as array indices are considered property
353 // names. We do not treat symbols that can be array indexes as property
354 // names because [] for string objects is handled only by keyed ICs.
355 virtual bool IsPropertyName() const { return false; }
357 // True iff the expression is a literal represented as a smi.
358 bool IsSmiLiteral() const;
360 // True iff the expression is a string literal.
361 bool IsStringLiteral() const;
363 // True iff the expression is the null literal.
364 bool IsNullLiteral() const;
366 // True if we can prove that the expression is the undefined literal.
367 bool IsUndefinedLiteral(Isolate* isolate) const;
369 // True iff the expression is a valid target for an assignment.
370 bool IsValidReferenceExpressionOrThis() const;
372 // Expression type bounds
373 Bounds bounds() const { return bounds_; }
374 void set_bounds(Bounds bounds) { bounds_ = bounds; }
376 // Type feedback information for assignments and properties.
377 virtual bool IsMonomorphic() {
381 virtual SmallMapList* GetReceiverTypes() {
385 virtual KeyedAccessStoreMode GetStoreMode() const {
387 return STANDARD_STORE;
389 virtual IcCheckType GetKeyType() const {
394 // TODO(rossberg): this should move to its own AST node eventually.
395 virtual void RecordToBooleanTypeFeedback(TypeFeedbackOracle* oracle);
396 uint16_t to_boolean_types() const {
397 return ToBooleanTypesField::decode(bit_field_);
400 void set_base_id(int id) { base_id_ = id; }
401 static int num_ids() { return parent_num_ids() + 2; }
402 BailoutId id() const { return BailoutId(local_id(0)); }
403 TypeFeedbackId test_id() const { return TypeFeedbackId(local_id(1)); }
406 Expression(Zone* zone, int pos)
408 base_id_(BailoutId::None().ToInt()),
409 bounds_(Bounds::Unbounded(zone)),
411 static int parent_num_ids() { return 0; }
412 void set_to_boolean_types(uint16_t types) {
413 bit_field_ = ToBooleanTypesField::update(bit_field_, types);
416 int base_id() const {
417 DCHECK(!BailoutId(base_id_).IsNone());
422 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
426 class ToBooleanTypesField : public BitField16<uint16_t, 0, 9> {};
428 // Ends with 16-bit field; deriving classes in turn begin with
429 // 16-bit fields for optimum packing efficiency.
433 class BreakableStatement : public Statement {
436 TARGET_FOR_ANONYMOUS,
437 TARGET_FOR_NAMED_ONLY
440 // The labels associated with this statement. May be NULL;
441 // if it is != NULL, guaranteed to contain at least one entry.
442 ZoneList<const AstRawString*>* labels() const { return labels_; }
444 // Type testing & conversion.
445 BreakableStatement* AsBreakableStatement() final { return this; }
448 Label* break_target() { return &break_target_; }
451 bool is_target_for_anonymous() const {
452 return breakable_type_ == TARGET_FOR_ANONYMOUS;
455 void set_base_id(int id) { base_id_ = id; }
456 static int num_ids() { return parent_num_ids() + 2; }
457 BailoutId EntryId() const { return BailoutId(local_id(0)); }
458 BailoutId ExitId() const { return BailoutId(local_id(1)); }
461 BreakableStatement(Zone* zone, ZoneList<const AstRawString*>* labels,
462 BreakableType breakable_type, int position)
463 : Statement(zone, position),
465 breakable_type_(breakable_type),
466 base_id_(BailoutId::None().ToInt()) {
467 DCHECK(labels == NULL || labels->length() > 0);
469 static int parent_num_ids() { return 0; }
471 int base_id() const {
472 DCHECK(!BailoutId(base_id_).IsNone());
477 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
479 ZoneList<const AstRawString*>* labels_;
480 BreakableType breakable_type_;
486 class Block final : public BreakableStatement {
488 DECLARE_NODE_TYPE(Block)
490 void AddStatement(Statement* statement, Zone* zone) {
491 statements_.Add(statement, zone);
494 ZoneList<Statement*>* statements() { return &statements_; }
495 bool ignore_completion_value() const { return ignore_completion_value_; }
497 static int num_ids() { return parent_num_ids() + 1; }
498 BailoutId DeclsId() const { return BailoutId(local_id(0)); }
500 bool IsJump() const override {
501 return !statements_.is_empty() && statements_.last()->IsJump()
502 && labels() == NULL; // Good enough as an approximation...
505 Scope* scope() const { return scope_; }
506 void set_scope(Scope* scope) { scope_ = scope; }
509 Block(Zone* zone, ZoneList<const AstRawString*>* labels, int capacity,
510 bool ignore_completion_value, int pos)
511 : BreakableStatement(zone, labels, TARGET_FOR_NAMED_ONLY, pos),
512 statements_(capacity, zone),
513 ignore_completion_value_(ignore_completion_value),
515 static int parent_num_ids() { return BreakableStatement::num_ids(); }
518 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
520 ZoneList<Statement*> statements_;
521 bool ignore_completion_value_;
526 class Declaration : public AstNode {
528 VariableProxy* proxy() const { return proxy_; }
529 VariableMode mode() const { return mode_; }
530 Scope* scope() const { return scope_; }
531 virtual InitializationFlag initialization() const = 0;
532 virtual bool IsInlineable() const;
535 Declaration(Zone* zone, VariableProxy* proxy, VariableMode mode, Scope* scope,
537 : AstNode(pos), mode_(mode), proxy_(proxy), scope_(scope) {
538 DCHECK(IsDeclaredVariableMode(mode));
543 VariableProxy* proxy_;
545 // Nested scope from which the declaration originated.
550 class VariableDeclaration final : public Declaration {
552 DECLARE_NODE_TYPE(VariableDeclaration)
554 InitializationFlag initialization() const override {
555 return mode() == VAR ? kCreatedInitialized : kNeedsInitialization;
558 bool is_class_declaration() const { return is_class_declaration_; }
560 // VariableDeclarations can be grouped into consecutive declaration
561 // groups. Each VariableDeclaration is associated with the start position of
562 // the group it belongs to. The positions are used for strong mode scope
563 // checks for classes and functions.
564 int declaration_group_start() const { return declaration_group_start_; }
567 VariableDeclaration(Zone* zone, VariableProxy* proxy, VariableMode mode,
568 Scope* scope, int pos, bool is_class_declaration = false,
569 int declaration_group_start = -1)
570 : Declaration(zone, proxy, mode, scope, pos),
571 is_class_declaration_(is_class_declaration),
572 declaration_group_start_(declaration_group_start) {}
574 bool is_class_declaration_;
575 int declaration_group_start_;
579 class FunctionDeclaration final : public Declaration {
581 DECLARE_NODE_TYPE(FunctionDeclaration)
583 FunctionLiteral* fun() const { return fun_; }
584 InitializationFlag initialization() const override {
585 return kCreatedInitialized;
587 bool IsInlineable() const override;
590 FunctionDeclaration(Zone* zone,
591 VariableProxy* proxy,
593 FunctionLiteral* fun,
596 : Declaration(zone, proxy, mode, scope, pos),
598 DCHECK(mode == VAR || mode == LET || mode == CONST);
603 FunctionLiteral* fun_;
607 class ImportDeclaration final : public Declaration {
609 DECLARE_NODE_TYPE(ImportDeclaration)
611 const AstRawString* import_name() const { return import_name_; }
612 const AstRawString* module_specifier() const { return module_specifier_; }
613 void set_module_specifier(const AstRawString* module_specifier) {
614 DCHECK(module_specifier_ == NULL);
615 module_specifier_ = module_specifier;
617 InitializationFlag initialization() const override {
618 return kNeedsInitialization;
622 ImportDeclaration(Zone* zone, VariableProxy* proxy,
623 const AstRawString* import_name,
624 const AstRawString* module_specifier, Scope* scope, int pos)
625 : Declaration(zone, proxy, IMPORT, scope, pos),
626 import_name_(import_name),
627 module_specifier_(module_specifier) {}
630 const AstRawString* import_name_;
631 const AstRawString* module_specifier_;
635 class ExportDeclaration final : public Declaration {
637 DECLARE_NODE_TYPE(ExportDeclaration)
639 InitializationFlag initialization() const override {
640 return kCreatedInitialized;
644 ExportDeclaration(Zone* zone, VariableProxy* proxy, Scope* scope, int pos)
645 : Declaration(zone, proxy, LET, scope, pos) {}
649 class Module : public AstNode {
651 ModuleDescriptor* descriptor() const { return descriptor_; }
652 Block* body() const { return body_; }
655 Module(Zone* zone, int pos)
656 : AstNode(pos), descriptor_(ModuleDescriptor::New(zone)), body_(NULL) {}
657 Module(Zone* zone, ModuleDescriptor* descriptor, int pos, Block* body = NULL)
658 : AstNode(pos), descriptor_(descriptor), body_(body) {}
661 ModuleDescriptor* descriptor_;
666 class IterationStatement : public BreakableStatement {
668 // Type testing & conversion.
669 IterationStatement* AsIterationStatement() final { return this; }
671 Statement* body() const { return body_; }
673 static int num_ids() { return parent_num_ids() + 1; }
674 BailoutId OsrEntryId() const { return BailoutId(local_id(0)); }
675 virtual BailoutId ContinueId() const = 0;
676 virtual BailoutId StackCheckId() const = 0;
679 Label* continue_target() { return &continue_target_; }
682 IterationStatement(Zone* zone, ZoneList<const AstRawString*>* labels, int pos)
683 : BreakableStatement(zone, labels, TARGET_FOR_ANONYMOUS, pos),
685 static int parent_num_ids() { return BreakableStatement::num_ids(); }
686 void Initialize(Statement* body) { body_ = body; }
689 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
692 Label continue_target_;
696 class DoWhileStatement final : public IterationStatement {
698 DECLARE_NODE_TYPE(DoWhileStatement)
700 void Initialize(Expression* cond, Statement* body) {
701 IterationStatement::Initialize(body);
705 Expression* cond() const { return cond_; }
707 static int num_ids() { return parent_num_ids() + 2; }
708 BailoutId ContinueId() const override { return BailoutId(local_id(0)); }
709 BailoutId StackCheckId() const override { return BackEdgeId(); }
710 BailoutId BackEdgeId() const { return BailoutId(local_id(1)); }
713 DoWhileStatement(Zone* zone, ZoneList<const AstRawString*>* labels, int pos)
714 : IterationStatement(zone, labels, pos), cond_(NULL) {}
715 static int parent_num_ids() { return IterationStatement::num_ids(); }
718 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
724 class WhileStatement final : public IterationStatement {
726 DECLARE_NODE_TYPE(WhileStatement)
728 void Initialize(Expression* cond, Statement* body) {
729 IterationStatement::Initialize(body);
733 Expression* cond() const { return cond_; }
735 static int num_ids() { return parent_num_ids() + 1; }
736 BailoutId ContinueId() const override { return EntryId(); }
737 BailoutId StackCheckId() const override { return BodyId(); }
738 BailoutId BodyId() const { return BailoutId(local_id(0)); }
741 WhileStatement(Zone* zone, ZoneList<const AstRawString*>* labels, int pos)
742 : IterationStatement(zone, labels, pos), cond_(NULL) {}
743 static int parent_num_ids() { return IterationStatement::num_ids(); }
746 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
752 class ForStatement final : public IterationStatement {
754 DECLARE_NODE_TYPE(ForStatement)
756 void Initialize(Statement* init,
760 IterationStatement::Initialize(body);
766 Statement* init() const { return init_; }
767 Expression* cond() const { return cond_; }
768 Statement* next() const { return next_; }
770 static int num_ids() { return parent_num_ids() + 2; }
771 BailoutId ContinueId() const override { return BailoutId(local_id(0)); }
772 BailoutId StackCheckId() const override { return BodyId(); }
773 BailoutId BodyId() const { return BailoutId(local_id(1)); }
776 ForStatement(Zone* zone, ZoneList<const AstRawString*>* labels, int pos)
777 : IterationStatement(zone, labels, pos),
781 static int parent_num_ids() { return IterationStatement::num_ids(); }
784 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
792 class ForEachStatement : public IterationStatement {
795 ENUMERATE, // for (each in subject) body;
796 ITERATE // for (each of subject) body;
799 void Initialize(Expression* each, Expression* subject, Statement* body) {
800 IterationStatement::Initialize(body);
805 Expression* each() const { return each_; }
806 Expression* subject() const { return subject_; }
808 FeedbackVectorRequirements ComputeFeedbackRequirements(
809 Isolate* isolate, const ICSlotCache* cache) override;
810 void SetFirstFeedbackICSlot(FeedbackVectorICSlot slot,
811 ICSlotCache* cache) override {
814 Code::Kind FeedbackICSlotKind(int index) override;
815 FeedbackVectorICSlot EachFeedbackSlot() const { return each_slot_; }
818 ForEachStatement(Zone* zone, ZoneList<const AstRawString*>* labels, int pos)
819 : IterationStatement(zone, labels, pos),
822 each_slot_(FeedbackVectorICSlot::Invalid()) {}
826 Expression* subject_;
827 FeedbackVectorICSlot each_slot_;
831 class ForInStatement final : public ForEachStatement {
833 DECLARE_NODE_TYPE(ForInStatement)
835 Expression* enumerable() const {
839 // Type feedback information.
840 FeedbackVectorRequirements ComputeFeedbackRequirements(
841 Isolate* isolate, const ICSlotCache* cache) override {
842 FeedbackVectorRequirements base =
843 ForEachStatement::ComputeFeedbackRequirements(isolate, cache);
844 DCHECK(base.slots() == 0 && base.ic_slots() <= 1);
845 return FeedbackVectorRequirements(1, base.ic_slots());
847 void SetFirstFeedbackSlot(FeedbackVectorSlot slot) override {
848 for_in_feedback_slot_ = slot;
851 FeedbackVectorSlot ForInFeedbackSlot() {
852 DCHECK(!for_in_feedback_slot_.IsInvalid());
853 return for_in_feedback_slot_;
856 enum ForInType { FAST_FOR_IN, SLOW_FOR_IN };
857 ForInType for_in_type() const { return for_in_type_; }
858 void set_for_in_type(ForInType type) { for_in_type_ = type; }
860 static int num_ids() { return parent_num_ids() + 6; }
861 BailoutId BodyId() const { return BailoutId(local_id(0)); }
862 BailoutId PrepareId() const { return BailoutId(local_id(1)); }
863 BailoutId EnumId() const { return BailoutId(local_id(2)); }
864 BailoutId ToObjectId() const { return BailoutId(local_id(3)); }
865 BailoutId FilterId() const { return BailoutId(local_id(4)); }
866 BailoutId AssignmentId() const { return BailoutId(local_id(5)); }
867 BailoutId ContinueId() const override { return EntryId(); }
868 BailoutId StackCheckId() const override { return BodyId(); }
871 ForInStatement(Zone* zone, ZoneList<const AstRawString*>* labels, int pos)
872 : ForEachStatement(zone, labels, pos),
873 for_in_type_(SLOW_FOR_IN),
874 for_in_feedback_slot_(FeedbackVectorSlot::Invalid()) {}
875 static int parent_num_ids() { return ForEachStatement::num_ids(); }
878 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
880 ForInType for_in_type_;
881 FeedbackVectorSlot for_in_feedback_slot_;
885 class ForOfStatement final : public ForEachStatement {
887 DECLARE_NODE_TYPE(ForOfStatement)
889 void Initialize(Expression* each,
892 Expression* assign_iterator,
893 Expression* next_result,
894 Expression* result_done,
895 Expression* assign_each) {
896 ForEachStatement::Initialize(each, subject, body);
897 assign_iterator_ = assign_iterator;
898 next_result_ = next_result;
899 result_done_ = result_done;
900 assign_each_ = assign_each;
903 Expression* iterable() const {
907 // iterator = subject[Symbol.iterator]()
908 Expression* assign_iterator() const {
909 return assign_iterator_;
912 // result = iterator.next() // with type check
913 Expression* next_result() const {
918 Expression* result_done() const {
922 // each = result.value
923 Expression* assign_each() const {
927 BailoutId ContinueId() const override { return EntryId(); }
928 BailoutId StackCheckId() const override { return BackEdgeId(); }
930 static int num_ids() { return parent_num_ids() + 1; }
931 BailoutId BackEdgeId() const { return BailoutId(local_id(0)); }
934 ForOfStatement(Zone* zone, ZoneList<const AstRawString*>* labels, int pos)
935 : ForEachStatement(zone, labels, pos),
936 assign_iterator_(NULL),
939 assign_each_(NULL) {}
940 static int parent_num_ids() { return ForEachStatement::num_ids(); }
943 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
945 Expression* assign_iterator_;
946 Expression* next_result_;
947 Expression* result_done_;
948 Expression* assign_each_;
952 class ExpressionStatement final : public Statement {
954 DECLARE_NODE_TYPE(ExpressionStatement)
956 void set_expression(Expression* e) { expression_ = e; }
957 Expression* expression() const { return expression_; }
958 bool IsJump() const override { return expression_->IsThrow(); }
961 ExpressionStatement(Zone* zone, Expression* expression, int pos)
962 : Statement(zone, pos), expression_(expression) { }
965 Expression* expression_;
969 class JumpStatement : public Statement {
971 bool IsJump() const final { return true; }
974 explicit JumpStatement(Zone* zone, int pos) : Statement(zone, pos) {}
978 class ContinueStatement final : public JumpStatement {
980 DECLARE_NODE_TYPE(ContinueStatement)
982 IterationStatement* target() const { return target_; }
985 explicit ContinueStatement(Zone* zone, IterationStatement* target, int pos)
986 : JumpStatement(zone, pos), target_(target) { }
989 IterationStatement* target_;
993 class BreakStatement final : public JumpStatement {
995 DECLARE_NODE_TYPE(BreakStatement)
997 BreakableStatement* target() const { return target_; }
1000 explicit BreakStatement(Zone* zone, BreakableStatement* target, int pos)
1001 : JumpStatement(zone, pos), target_(target) { }
1004 BreakableStatement* target_;
1008 class ReturnStatement final : public JumpStatement {
1010 DECLARE_NODE_TYPE(ReturnStatement)
1012 Expression* expression() const { return expression_; }
1015 explicit ReturnStatement(Zone* zone, Expression* expression, int pos)
1016 : JumpStatement(zone, pos), expression_(expression) { }
1019 Expression* expression_;
1023 class WithStatement final : public Statement {
1025 DECLARE_NODE_TYPE(WithStatement)
1027 Scope* scope() { return scope_; }
1028 Expression* expression() const { return expression_; }
1029 Statement* statement() const { return statement_; }
1031 void set_base_id(int id) { base_id_ = id; }
1032 static int num_ids() { return parent_num_ids() + 1; }
1033 BailoutId EntryId() const { return BailoutId(local_id(0)); }
1036 WithStatement(Zone* zone, Scope* scope, Expression* expression,
1037 Statement* statement, int pos)
1038 : Statement(zone, pos),
1040 expression_(expression),
1041 statement_(statement),
1042 base_id_(BailoutId::None().ToInt()) {}
1043 static int parent_num_ids() { return 0; }
1045 int base_id() const {
1046 DCHECK(!BailoutId(base_id_).IsNone());
1051 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
1054 Expression* expression_;
1055 Statement* statement_;
1060 class CaseClause final : public Expression {
1062 DECLARE_NODE_TYPE(CaseClause)
1064 bool is_default() const { return label_ == NULL; }
1065 Expression* label() const {
1066 CHECK(!is_default());
1069 Label* body_target() { return &body_target_; }
1070 ZoneList<Statement*>* statements() const { return statements_; }
1072 static int num_ids() { return parent_num_ids() + 2; }
1073 BailoutId EntryId() const { return BailoutId(local_id(0)); }
1074 TypeFeedbackId CompareId() { return TypeFeedbackId(local_id(1)); }
1076 Type* compare_type() { return compare_type_; }
1077 void set_compare_type(Type* type) { compare_type_ = type; }
1080 static int parent_num_ids() { return Expression::num_ids(); }
1083 CaseClause(Zone* zone, Expression* label, ZoneList<Statement*>* statements,
1085 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
1089 ZoneList<Statement*>* statements_;
1090 Type* compare_type_;
1094 class SwitchStatement final : public BreakableStatement {
1096 DECLARE_NODE_TYPE(SwitchStatement)
1098 void Initialize(Expression* tag, ZoneList<CaseClause*>* cases) {
1103 Expression* tag() const { return tag_; }
1104 ZoneList<CaseClause*>* cases() const { return cases_; }
1107 SwitchStatement(Zone* zone, ZoneList<const AstRawString*>* labels, int pos)
1108 : BreakableStatement(zone, labels, TARGET_FOR_ANONYMOUS, pos),
1114 ZoneList<CaseClause*>* cases_;
1118 // If-statements always have non-null references to their then- and
1119 // else-parts. When parsing if-statements with no explicit else-part,
1120 // the parser implicitly creates an empty statement. Use the
1121 // HasThenStatement() and HasElseStatement() functions to check if a
1122 // given if-statement has a then- or an else-part containing code.
1123 class IfStatement final : public Statement {
1125 DECLARE_NODE_TYPE(IfStatement)
1127 bool HasThenStatement() const { return !then_statement()->IsEmpty(); }
1128 bool HasElseStatement() const { return !else_statement()->IsEmpty(); }
1130 Expression* condition() const { return condition_; }
1131 Statement* then_statement() const { return then_statement_; }
1132 Statement* else_statement() const { return else_statement_; }
1134 bool IsJump() const override {
1135 return HasThenStatement() && then_statement()->IsJump()
1136 && HasElseStatement() && else_statement()->IsJump();
1139 void set_base_id(int id) { base_id_ = id; }
1140 static int num_ids() { return parent_num_ids() + 3; }
1141 BailoutId IfId() const { return BailoutId(local_id(0)); }
1142 BailoutId ThenId() const { return BailoutId(local_id(1)); }
1143 BailoutId ElseId() const { return BailoutId(local_id(2)); }
1146 IfStatement(Zone* zone, Expression* condition, Statement* then_statement,
1147 Statement* else_statement, int pos)
1148 : Statement(zone, pos),
1149 condition_(condition),
1150 then_statement_(then_statement),
1151 else_statement_(else_statement),
1152 base_id_(BailoutId::None().ToInt()) {}
1153 static int parent_num_ids() { return 0; }
1155 int base_id() const {
1156 DCHECK(!BailoutId(base_id_).IsNone());
1161 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
1163 Expression* condition_;
1164 Statement* then_statement_;
1165 Statement* else_statement_;
1170 class TryStatement : public Statement {
1172 Block* try_block() const { return try_block_; }
1174 void set_base_id(int id) { base_id_ = id; }
1175 static int num_ids() { return parent_num_ids() + 1; }
1176 BailoutId HandlerId() const { return BailoutId(local_id(0)); }
1179 TryStatement(Zone* zone, Block* try_block, int pos)
1180 : Statement(zone, pos),
1181 try_block_(try_block),
1182 base_id_(BailoutId::None().ToInt()) {}
1183 static int parent_num_ids() { return 0; }
1185 int base_id() const {
1186 DCHECK(!BailoutId(base_id_).IsNone());
1191 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
1198 class TryCatchStatement final : public TryStatement {
1200 DECLARE_NODE_TYPE(TryCatchStatement)
1202 Scope* scope() { return scope_; }
1203 Variable* variable() { return variable_; }
1204 Block* catch_block() const { return catch_block_; }
1207 TryCatchStatement(Zone* zone, Block* try_block, Scope* scope,
1208 Variable* variable, Block* catch_block, int pos)
1209 : TryStatement(zone, try_block, pos),
1211 variable_(variable),
1212 catch_block_(catch_block) {}
1216 Variable* variable_;
1217 Block* catch_block_;
1221 class TryFinallyStatement final : public TryStatement {
1223 DECLARE_NODE_TYPE(TryFinallyStatement)
1225 Block* finally_block() const { return finally_block_; }
1228 TryFinallyStatement(Zone* zone, Block* try_block, Block* finally_block,
1230 : TryStatement(zone, try_block, pos), finally_block_(finally_block) {}
1233 Block* finally_block_;
1237 class DebuggerStatement final : public Statement {
1239 DECLARE_NODE_TYPE(DebuggerStatement)
1241 void set_base_id(int id) { base_id_ = id; }
1242 static int num_ids() { return parent_num_ids() + 1; }
1243 BailoutId DebugBreakId() const { return BailoutId(local_id(0)); }
1246 explicit DebuggerStatement(Zone* zone, int pos)
1247 : Statement(zone, pos), base_id_(BailoutId::None().ToInt()) {}
1248 static int parent_num_ids() { return 0; }
1250 int base_id() const {
1251 DCHECK(!BailoutId(base_id_).IsNone());
1256 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
1262 class EmptyStatement final : public Statement {
1264 DECLARE_NODE_TYPE(EmptyStatement)
1267 explicit EmptyStatement(Zone* zone, int pos): Statement(zone, pos) {}
1271 class Literal final : public Expression {
1273 DECLARE_NODE_TYPE(Literal)
1275 bool IsPropertyName() const override { return value_->IsPropertyName(); }
1277 Handle<String> AsPropertyName() {
1278 DCHECK(IsPropertyName());
1279 return Handle<String>::cast(value());
1282 const AstRawString* AsRawPropertyName() {
1283 DCHECK(IsPropertyName());
1284 return value_->AsString();
1287 bool ToBooleanIsTrue() const override { return value()->BooleanValue(); }
1288 bool ToBooleanIsFalse() const override { return !value()->BooleanValue(); }
1290 Handle<Object> value() const { return value_->value(); }
1291 const AstValue* raw_value() const { return value_; }
1293 // Support for using Literal as a HashMap key. NOTE: Currently, this works
1294 // only for string and number literals!
1296 static bool Match(void* literal1, void* literal2);
1298 static int num_ids() { return parent_num_ids() + 1; }
1299 TypeFeedbackId LiteralFeedbackId() const {
1300 return TypeFeedbackId(local_id(0));
1304 Literal(Zone* zone, const AstValue* value, int position)
1305 : Expression(zone, position), value_(value) {}
1306 static int parent_num_ids() { return Expression::num_ids(); }
1309 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
1311 const AstValue* value_;
1315 class AstLiteralReindexer;
1317 // Base class for literals that needs space in the corresponding JSFunction.
1318 class MaterializedLiteral : public Expression {
1320 virtual MaterializedLiteral* AsMaterializedLiteral() { return this; }
1322 int literal_index() { return literal_index_; }
1325 // only callable after initialization.
1326 DCHECK(depth_ >= 1);
1330 bool is_strong() const { return is_strong_; }
1333 MaterializedLiteral(Zone* zone, int literal_index, bool is_strong, int pos)
1334 : Expression(zone, pos),
1335 literal_index_(literal_index),
1337 is_strong_(is_strong),
1340 // A materialized literal is simple if the values consist of only
1341 // constants and simple object and array literals.
1342 bool is_simple() const { return is_simple_; }
1343 void set_is_simple(bool is_simple) { is_simple_ = is_simple; }
1344 friend class CompileTimeValue;
1346 void set_depth(int depth) {
1351 // Populate the constant properties/elements fixed array.
1352 void BuildConstants(Isolate* isolate);
1353 friend class ArrayLiteral;
1354 friend class ObjectLiteral;
1356 // If the expression is a literal, return the literal value;
1357 // if the expression is a materialized literal and is simple return a
1358 // compile time value as encoded by CompileTimeValue::GetValue().
1359 // Otherwise, return undefined literal as the placeholder
1360 // in the object literal boilerplate.
1361 Handle<Object> GetBoilerplateValue(Expression* expression, Isolate* isolate);
1369 friend class AstLiteralReindexer;
1373 // Property is used for passing information
1374 // about an object literal's properties from the parser
1375 // to the code generator.
1376 class ObjectLiteralProperty final : public ZoneObject {
1379 CONSTANT, // Property with constant value (compile time).
1380 COMPUTED, // Property with computed value (execution time).
1381 MATERIALIZED_LITERAL, // Property value is a materialized literal.
1382 GETTER, SETTER, // Property is an accessor function.
1383 PROTOTYPE // Property is __proto__.
1386 Expression* key() { return key_; }
1387 Expression* value() { return value_; }
1388 Kind kind() { return kind_; }
1390 // Type feedback information.
1391 bool IsMonomorphic() { return !receiver_type_.is_null(); }
1392 Handle<Map> GetReceiverType() { return receiver_type_; }
1394 bool IsCompileTimeValue();
1396 void set_emit_store(bool emit_store);
1399 bool is_static() const { return is_static_; }
1400 bool is_computed_name() const { return is_computed_name_; }
1402 void set_receiver_type(Handle<Map> map) { receiver_type_ = map; }
1405 friend class AstNodeFactory;
1407 ObjectLiteralProperty(Expression* key, Expression* value, Kind kind,
1408 bool is_static, bool is_computed_name);
1409 ObjectLiteralProperty(AstValueFactory* ast_value_factory, Expression* key,
1410 Expression* value, bool is_static,
1411 bool is_computed_name);
1419 bool is_computed_name_;
1420 Handle<Map> receiver_type_;
1424 // An object literal has a boilerplate object that is used
1425 // for minimizing the work when constructing it at runtime.
1426 class ObjectLiteral final : public MaterializedLiteral {
1428 typedef ObjectLiteralProperty Property;
1430 DECLARE_NODE_TYPE(ObjectLiteral)
1432 Handle<FixedArray> constant_properties() const {
1433 return constant_properties_;
1435 int properties_count() const { return constant_properties_->length() / 2; }
1436 ZoneList<Property*>* properties() const { return properties_; }
1437 bool fast_elements() const { return fast_elements_; }
1438 bool may_store_doubles() const { return may_store_doubles_; }
1439 bool has_function() const { return has_function_; }
1440 bool has_elements() const { return has_elements_; }
1442 // Decide if a property should be in the object boilerplate.
1443 static bool IsBoilerplateProperty(Property* property);
1445 // Populate the constant properties fixed array.
1446 void BuildConstantProperties(Isolate* isolate);
1448 // Mark all computed expressions that are bound to a key that
1449 // is shadowed by a later occurrence of the same key. For the
1450 // marked expressions, no store code is emitted.
1451 void CalculateEmitStore(Zone* zone);
1453 // Assemble bitfield of flags for the CreateObjectLiteral helper.
1454 int ComputeFlags(bool disable_mementos = false) const {
1455 int flags = fast_elements() ? kFastElements : kNoFlags;
1456 flags |= has_function() ? kHasFunction : kNoFlags;
1457 if (depth() == 1 && !has_elements() && !may_store_doubles()) {
1458 flags |= kShallowProperties;
1460 if (disable_mementos) {
1461 flags |= kDisableMementos;
1472 kHasFunction = 1 << 1,
1473 kShallowProperties = 1 << 2,
1474 kDisableMementos = 1 << 3,
1478 struct Accessors: public ZoneObject {
1479 Accessors() : getter(NULL), setter(NULL) {}
1484 BailoutId CreateLiteralId() const { return BailoutId(local_id(0)); }
1486 // Return an AST id for a property that is used in simulate instructions.
1487 BailoutId GetIdForProperty(int i) { return BailoutId(local_id(i + 1)); }
1489 // Unlike other AST nodes, this number of bailout IDs allocated for an
1490 // ObjectLiteral can vary, so num_ids() is not a static method.
1491 int num_ids() const { return parent_num_ids() + 1 + properties()->length(); }
1493 // Object literals need one feedback slot for each non-trivial value, as well
1494 // as some slots for home objects.
1495 FeedbackVectorRequirements ComputeFeedbackRequirements(
1496 Isolate* isolate, const ICSlotCache* cache) override;
1497 void SetFirstFeedbackICSlot(FeedbackVectorICSlot slot,
1498 ICSlotCache* cache) override {
1501 Code::Kind FeedbackICSlotKind(int index) override { return Code::STORE_IC; }
1502 FeedbackVectorICSlot GetNthSlot(int n) const {
1503 return FeedbackVectorICSlot(slot_.ToInt() + n);
1506 // If value needs a home object, returns a valid feedback vector ic slot
1507 // given by slot_index, and increments slot_index.
1508 FeedbackVectorICSlot SlotForHomeObject(Expression* value,
1509 int* slot_index) const;
1512 int slot_count() const { return slot_count_; }
1516 ObjectLiteral(Zone* zone, ZoneList<Property*>* properties, int literal_index,
1517 int boilerplate_properties, bool has_function, bool is_strong,
1519 : MaterializedLiteral(zone, literal_index, is_strong, pos),
1520 properties_(properties),
1521 boilerplate_properties_(boilerplate_properties),
1522 fast_elements_(false),
1523 has_elements_(false),
1524 may_store_doubles_(false),
1525 has_function_(has_function),
1529 slot_(FeedbackVectorICSlot::Invalid()) {
1531 static int parent_num_ids() { return MaterializedLiteral::num_ids(); }
1534 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
1535 Handle<FixedArray> constant_properties_;
1536 ZoneList<Property*>* properties_;
1537 int boilerplate_properties_;
1538 bool fast_elements_;
1540 bool may_store_doubles_;
1543 // slot_count_ helps validate that the logic to allocate ic slots and the
1544 // logic to use them are in sync.
1547 FeedbackVectorICSlot slot_;
1551 // Node for capturing a regexp literal.
1552 class RegExpLiteral final : public MaterializedLiteral {
1554 DECLARE_NODE_TYPE(RegExpLiteral)
1556 Handle<String> pattern() const { return pattern_->string(); }
1557 Handle<String> flags() const { return flags_->string(); }
1560 RegExpLiteral(Zone* zone, const AstRawString* pattern,
1561 const AstRawString* flags, int literal_index, bool is_strong,
1563 : MaterializedLiteral(zone, literal_index, is_strong, pos),
1570 const AstRawString* pattern_;
1571 const AstRawString* flags_;
1575 // An array literal has a literals object that is used
1576 // for minimizing the work when constructing it at runtime.
1577 class ArrayLiteral final : public MaterializedLiteral {
1579 DECLARE_NODE_TYPE(ArrayLiteral)
1581 Handle<FixedArray> constant_elements() const { return constant_elements_; }
1582 ElementsKind constant_elements_kind() const {
1583 DCHECK_EQ(2, constant_elements_->length());
1584 return static_cast<ElementsKind>(
1585 Smi::cast(constant_elements_->get(0))->value());
1588 ZoneList<Expression*>* values() const { return values_; }
1590 BailoutId CreateLiteralId() const { return BailoutId(local_id(0)); }
1592 // Return an AST id for an element that is used in simulate instructions.
1593 BailoutId GetIdForElement(int i) { return BailoutId(local_id(i + 1)); }
1595 // Unlike other AST nodes, this number of bailout IDs allocated for an
1596 // ArrayLiteral can vary, so num_ids() is not a static method.
1597 int num_ids() const { return parent_num_ids() + 1 + values()->length(); }
1599 // Populate the constant elements fixed array.
1600 void BuildConstantElements(Isolate* isolate);
1602 // Assemble bitfield of flags for the CreateArrayLiteral helper.
1603 int ComputeFlags(bool disable_mementos = false) const {
1604 int flags = depth() == 1 ? kShallowElements : kNoFlags;
1605 if (disable_mementos) {
1606 flags |= kDisableMementos;
1616 kShallowElements = 1,
1617 kDisableMementos = 1 << 1,
1622 ArrayLiteral(Zone* zone, ZoneList<Expression*>* values,
1623 int first_spread_index, int literal_index, bool is_strong,
1625 : MaterializedLiteral(zone, literal_index, is_strong, pos),
1627 first_spread_index_(first_spread_index) {}
1628 static int parent_num_ids() { return MaterializedLiteral::num_ids(); }
1631 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
1633 Handle<FixedArray> constant_elements_;
1634 ZoneList<Expression*>* values_;
1635 int first_spread_index_;
1639 class VariableProxy final : public Expression {
1641 DECLARE_NODE_TYPE(VariableProxy)
1643 bool IsValidReferenceExpression() const override {
1644 return !is_this() && !is_new_target();
1647 bool IsArguments() const { return is_resolved() && var()->is_arguments(); }
1649 Handle<String> name() const { return raw_name()->string(); }
1650 const AstRawString* raw_name() const {
1651 return is_resolved() ? var_->raw_name() : raw_name_;
1654 Variable* var() const {
1655 DCHECK(is_resolved());
1658 void set_var(Variable* v) {
1659 DCHECK(!is_resolved());
1664 bool is_this() const { return IsThisField::decode(bit_field_); }
1666 bool is_assigned() const { return IsAssignedField::decode(bit_field_); }
1667 void set_is_assigned() {
1668 bit_field_ = IsAssignedField::update(bit_field_, true);
1671 bool is_resolved() const { return IsResolvedField::decode(bit_field_); }
1672 void set_is_resolved() {
1673 bit_field_ = IsResolvedField::update(bit_field_, true);
1676 bool is_new_target() const { return IsNewTargetField::decode(bit_field_); }
1677 void set_is_new_target() {
1678 bit_field_ = IsNewTargetField::update(bit_field_, true);
1681 int end_position() const { return end_position_; }
1683 // Bind this proxy to the variable var.
1684 void BindTo(Variable* var);
1686 bool UsesVariableFeedbackSlot() const {
1687 return var()->IsUnallocated() || var()->IsLookupSlot();
1690 virtual FeedbackVectorRequirements ComputeFeedbackRequirements(
1691 Isolate* isolate, const ICSlotCache* cache) override;
1693 void SetFirstFeedbackICSlot(FeedbackVectorICSlot slot,
1694 ICSlotCache* cache) override;
1695 Code::Kind FeedbackICSlotKind(int index) override { return Code::LOAD_IC; }
1696 FeedbackVectorICSlot VariableFeedbackSlot() {
1697 return variable_feedback_slot_;
1700 static int num_ids() { return parent_num_ids() + 1; }
1701 BailoutId BeforeId() const { return BailoutId(local_id(0)); }
1704 VariableProxy(Zone* zone, Variable* var, int start_position,
1707 VariableProxy(Zone* zone, const AstRawString* name,
1708 Variable::Kind variable_kind, int start_position,
1710 static int parent_num_ids() { return Expression::num_ids(); }
1711 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
1713 class IsThisField : public BitField8<bool, 0, 1> {};
1714 class IsAssignedField : public BitField8<bool, 1, 1> {};
1715 class IsResolvedField : public BitField8<bool, 2, 1> {};
1716 class IsNewTargetField : public BitField8<bool, 3, 1> {};
1718 // Start with 16-bit (or smaller) field, which should get packed together
1719 // with Expression's trailing 16-bit field.
1721 FeedbackVectorICSlot variable_feedback_slot_;
1723 const AstRawString* raw_name_; // if !is_resolved_
1724 Variable* var_; // if is_resolved_
1726 // Position is stored in the AstNode superclass, but VariableProxy needs to
1727 // know its end position too (for error messages). It cannot be inferred from
1728 // the variable name length because it can contain escapes.
1733 // Left-hand side can only be a property, a global or a (parameter or local)
1739 NAMED_SUPER_PROPERTY,
1740 KEYED_SUPER_PROPERTY
1744 class Property final : public Expression {
1746 DECLARE_NODE_TYPE(Property)
1748 bool IsValidReferenceExpression() const override { return true; }
1750 Expression* obj() const { return obj_; }
1751 Expression* key() const { return key_; }
1753 static int num_ids() { return parent_num_ids() + 1; }
1754 BailoutId LoadId() const { return BailoutId(local_id(0)); }
1756 bool IsStringAccess() const {
1757 return IsStringAccessField::decode(bit_field_);
1760 // Type feedback information.
1761 bool IsMonomorphic() override { return receiver_types_.length() == 1; }
1762 SmallMapList* GetReceiverTypes() override { return &receiver_types_; }
1763 KeyedAccessStoreMode GetStoreMode() const override { return STANDARD_STORE; }
1764 IcCheckType GetKeyType() const override {
1765 return KeyTypeField::decode(bit_field_);
1767 bool IsUninitialized() const {
1768 return !is_for_call() && HasNoTypeInformation();
1770 bool HasNoTypeInformation() const {
1771 return GetInlineCacheState() == UNINITIALIZED;
1773 InlineCacheState GetInlineCacheState() const {
1774 return InlineCacheStateField::decode(bit_field_);
1776 void set_is_string_access(bool b) {
1777 bit_field_ = IsStringAccessField::update(bit_field_, b);
1779 void set_key_type(IcCheckType key_type) {
1780 bit_field_ = KeyTypeField::update(bit_field_, key_type);
1782 void set_inline_cache_state(InlineCacheState state) {
1783 bit_field_ = InlineCacheStateField::update(bit_field_, state);
1785 void mark_for_call() {
1786 bit_field_ = IsForCallField::update(bit_field_, true);
1788 bool is_for_call() const { return IsForCallField::decode(bit_field_); }
1790 bool IsSuperAccess() { return obj()->IsSuperPropertyReference(); }
1792 virtual FeedbackVectorRequirements ComputeFeedbackRequirements(
1793 Isolate* isolate, const ICSlotCache* cache) override {
1794 return FeedbackVectorRequirements(0, 1);
1796 void SetFirstFeedbackICSlot(FeedbackVectorICSlot slot,
1797 ICSlotCache* cache) override {
1798 property_feedback_slot_ = slot;
1800 Code::Kind FeedbackICSlotKind(int index) override {
1801 return key()->IsPropertyName() ? Code::LOAD_IC : Code::KEYED_LOAD_IC;
1804 FeedbackVectorICSlot PropertyFeedbackSlot() const {
1805 return property_feedback_slot_;
1808 static LhsKind GetAssignType(Property* property) {
1809 if (property == NULL) return VARIABLE;
1810 bool super_access = property->IsSuperAccess();
1811 return (property->key()->IsPropertyName())
1812 ? (super_access ? NAMED_SUPER_PROPERTY : NAMED_PROPERTY)
1813 : (super_access ? KEYED_SUPER_PROPERTY : KEYED_PROPERTY);
1817 Property(Zone* zone, Expression* obj, Expression* key, int pos)
1818 : Expression(zone, pos),
1819 bit_field_(IsForCallField::encode(false) |
1820 IsStringAccessField::encode(false) |
1821 InlineCacheStateField::encode(UNINITIALIZED)),
1822 property_feedback_slot_(FeedbackVectorICSlot::Invalid()),
1825 static int parent_num_ids() { return Expression::num_ids(); }
1828 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
1830 class IsForCallField : public BitField8<bool, 0, 1> {};
1831 class IsStringAccessField : public BitField8<bool, 1, 1> {};
1832 class KeyTypeField : public BitField8<IcCheckType, 2, 1> {};
1833 class InlineCacheStateField : public BitField8<InlineCacheState, 3, 4> {};
1835 FeedbackVectorICSlot property_feedback_slot_;
1838 SmallMapList receiver_types_;
1842 class Call final : public Expression {
1844 DECLARE_NODE_TYPE(Call)
1846 Expression* expression() const { return expression_; }
1847 ZoneList<Expression*>* arguments() const { return arguments_; }
1849 // Type feedback information.
1850 virtual FeedbackVectorRequirements ComputeFeedbackRequirements(
1851 Isolate* isolate, const ICSlotCache* cache) override;
1852 void SetFirstFeedbackICSlot(FeedbackVectorICSlot slot,
1853 ICSlotCache* cache) override {
1856 void SetFirstFeedbackSlot(FeedbackVectorSlot slot) override { slot_ = slot; }
1857 Code::Kind FeedbackICSlotKind(int index) override { return Code::CALL_IC; }
1859 FeedbackVectorSlot CallFeedbackSlot() const { return slot_; }
1861 FeedbackVectorICSlot CallFeedbackICSlot() const { return ic_slot_; }
1863 SmallMapList* GetReceiverTypes() override {
1864 if (expression()->IsProperty()) {
1865 return expression()->AsProperty()->GetReceiverTypes();
1870 bool IsMonomorphic() override {
1871 if (expression()->IsProperty()) {
1872 return expression()->AsProperty()->IsMonomorphic();
1874 return !target_.is_null();
1877 bool global_call() const {
1878 VariableProxy* proxy = expression_->AsVariableProxy();
1879 return proxy != NULL && proxy->var()->IsUnallocatedOrGlobalSlot();
1882 bool known_global_function() const {
1883 return global_call() && !target_.is_null();
1886 Handle<JSFunction> target() { return target_; }
1888 Handle<AllocationSite> allocation_site() { return allocation_site_; }
1890 void SetKnownGlobalTarget(Handle<JSFunction> target) {
1892 set_is_uninitialized(false);
1894 void set_target(Handle<JSFunction> target) { target_ = target; }
1895 void set_allocation_site(Handle<AllocationSite> site) {
1896 allocation_site_ = site;
1899 static int num_ids() { return parent_num_ids() + 3; }
1900 BailoutId ReturnId() const { return BailoutId(local_id(0)); }
1901 BailoutId EvalId() const { return BailoutId(local_id(1)); }
1902 BailoutId LookupId() const { return BailoutId(local_id(2)); }
1904 bool is_uninitialized() const {
1905 return IsUninitializedField::decode(bit_field_);
1907 void set_is_uninitialized(bool b) {
1908 bit_field_ = IsUninitializedField::update(bit_field_, b);
1920 // Helpers to determine how to handle the call.
1921 CallType GetCallType(Isolate* isolate) const;
1922 bool IsUsingCallFeedbackSlot(Isolate* isolate) const;
1923 bool IsUsingCallFeedbackICSlot(Isolate* isolate) const;
1926 // Used to assert that the FullCodeGenerator records the return site.
1927 bool return_is_recorded_;
1931 Call(Zone* zone, Expression* expression, ZoneList<Expression*>* arguments,
1933 : Expression(zone, pos),
1934 ic_slot_(FeedbackVectorICSlot::Invalid()),
1935 slot_(FeedbackVectorSlot::Invalid()),
1936 expression_(expression),
1937 arguments_(arguments),
1938 bit_field_(IsUninitializedField::encode(false)) {
1939 if (expression->IsProperty()) {
1940 expression->AsProperty()->mark_for_call();
1943 static int parent_num_ids() { return Expression::num_ids(); }
1946 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
1948 FeedbackVectorICSlot ic_slot_;
1949 FeedbackVectorSlot slot_;
1950 Expression* expression_;
1951 ZoneList<Expression*>* arguments_;
1952 Handle<JSFunction> target_;
1953 Handle<AllocationSite> allocation_site_;
1954 class IsUninitializedField : public BitField8<bool, 0, 1> {};
1959 class CallNew final : public Expression {
1961 DECLARE_NODE_TYPE(CallNew)
1963 Expression* expression() const { return expression_; }
1964 ZoneList<Expression*>* arguments() const { return arguments_; }
1966 // Type feedback information.
1967 virtual FeedbackVectorRequirements ComputeFeedbackRequirements(
1968 Isolate* isolate, const ICSlotCache* cache) override {
1969 return FeedbackVectorRequirements(FLAG_pretenuring_call_new ? 2 : 1, 0);
1971 void SetFirstFeedbackSlot(FeedbackVectorSlot slot) override {
1972 callnew_feedback_slot_ = slot;
1975 FeedbackVectorSlot CallNewFeedbackSlot() {
1976 DCHECK(!callnew_feedback_slot_.IsInvalid());
1977 return callnew_feedback_slot_;
1979 FeedbackVectorSlot AllocationSiteFeedbackSlot() {
1980 DCHECK(FLAG_pretenuring_call_new);
1981 return CallNewFeedbackSlot().next();
1984 bool IsMonomorphic() override { return is_monomorphic_; }
1985 Handle<JSFunction> target() const { return target_; }
1986 Handle<AllocationSite> allocation_site() const {
1987 return allocation_site_;
1990 static int num_ids() { return parent_num_ids() + 1; }
1991 static int feedback_slots() { return 1; }
1992 BailoutId ReturnId() const { return BailoutId(local_id(0)); }
1994 void set_allocation_site(Handle<AllocationSite> site) {
1995 allocation_site_ = site;
1997 void set_is_monomorphic(bool monomorphic) { is_monomorphic_ = monomorphic; }
1998 void set_target(Handle<JSFunction> target) { target_ = target; }
1999 void SetKnownGlobalTarget(Handle<JSFunction> target) {
2001 is_monomorphic_ = true;
2005 CallNew(Zone* zone, Expression* expression, ZoneList<Expression*>* arguments,
2007 : Expression(zone, pos),
2008 expression_(expression),
2009 arguments_(arguments),
2010 is_monomorphic_(false),
2011 callnew_feedback_slot_(FeedbackVectorSlot::Invalid()) {}
2013 static int parent_num_ids() { return Expression::num_ids(); }
2016 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
2018 Expression* expression_;
2019 ZoneList<Expression*>* arguments_;
2020 bool is_monomorphic_;
2021 Handle<JSFunction> target_;
2022 Handle<AllocationSite> allocation_site_;
2023 FeedbackVectorSlot callnew_feedback_slot_;
2027 // The CallRuntime class does not represent any official JavaScript
2028 // language construct. Instead it is used to call a C or JS function
2029 // with a set of arguments. This is used from the builtins that are
2030 // implemented in JavaScript (see "v8natives.js").
2031 class CallRuntime final : public Expression {
2033 DECLARE_NODE_TYPE(CallRuntime)
2035 Handle<String> name() const { return raw_name_->string(); }
2036 const AstRawString* raw_name() const { return raw_name_; }
2037 const Runtime::Function* function() const { return function_; }
2038 ZoneList<Expression*>* arguments() const { return arguments_; }
2039 bool is_jsruntime() const { return function_ == NULL; }
2041 // Type feedback information.
2042 bool HasCallRuntimeFeedbackSlot() const { return is_jsruntime(); }
2043 virtual FeedbackVectorRequirements ComputeFeedbackRequirements(
2044 Isolate* isolate, const ICSlotCache* cache) override {
2045 return FeedbackVectorRequirements(0, HasCallRuntimeFeedbackSlot() ? 1 : 0);
2047 void SetFirstFeedbackICSlot(FeedbackVectorICSlot slot,
2048 ICSlotCache* cache) override {
2049 callruntime_feedback_slot_ = slot;
2051 Code::Kind FeedbackICSlotKind(int index) override { return Code::LOAD_IC; }
2053 FeedbackVectorICSlot CallRuntimeFeedbackSlot() {
2054 DCHECK(!HasCallRuntimeFeedbackSlot() ||
2055 !callruntime_feedback_slot_.IsInvalid());
2056 return callruntime_feedback_slot_;
2059 static int num_ids() { return parent_num_ids() + 1; }
2060 BailoutId CallId() { return BailoutId(local_id(0)); }
2063 CallRuntime(Zone* zone, const AstRawString* name,
2064 const Runtime::Function* function,
2065 ZoneList<Expression*>* arguments, int pos)
2066 : Expression(zone, pos),
2068 function_(function),
2069 arguments_(arguments),
2070 callruntime_feedback_slot_(FeedbackVectorICSlot::Invalid()) {}
2071 static int parent_num_ids() { return Expression::num_ids(); }
2074 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
2076 const AstRawString* raw_name_;
2077 const Runtime::Function* function_;
2078 ZoneList<Expression*>* arguments_;
2079 FeedbackVectorICSlot callruntime_feedback_slot_;
2083 class UnaryOperation final : public Expression {
2085 DECLARE_NODE_TYPE(UnaryOperation)
2087 Token::Value op() const { return op_; }
2088 Expression* expression() const { return expression_; }
2090 // For unary not (Token::NOT), the AST ids where true and false will
2091 // actually be materialized, respectively.
2092 static int num_ids() { return parent_num_ids() + 2; }
2093 BailoutId MaterializeTrueId() const { return BailoutId(local_id(0)); }
2094 BailoutId MaterializeFalseId() const { return BailoutId(local_id(1)); }
2096 virtual void RecordToBooleanTypeFeedback(TypeFeedbackOracle* oracle) override;
2099 UnaryOperation(Zone* zone, Token::Value op, Expression* expression, int pos)
2100 : Expression(zone, pos), op_(op), expression_(expression) {
2101 DCHECK(Token::IsUnaryOp(op));
2103 static int parent_num_ids() { return Expression::num_ids(); }
2106 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
2109 Expression* expression_;
2113 class BinaryOperation final : public Expression {
2115 DECLARE_NODE_TYPE(BinaryOperation)
2117 Token::Value op() const { return static_cast<Token::Value>(op_); }
2118 Expression* left() const { return left_; }
2119 Expression* right() const { return right_; }
2120 Handle<AllocationSite> allocation_site() const { return allocation_site_; }
2121 void set_allocation_site(Handle<AllocationSite> allocation_site) {
2122 allocation_site_ = allocation_site;
2125 // The short-circuit logical operations need an AST ID for their
2126 // right-hand subexpression.
2127 static int num_ids() { return parent_num_ids() + 2; }
2128 BailoutId RightId() const { return BailoutId(local_id(0)); }
2130 TypeFeedbackId BinaryOperationFeedbackId() const {
2131 return TypeFeedbackId(local_id(1));
2133 Maybe<int> fixed_right_arg() const {
2134 return has_fixed_right_arg_ ? Just(fixed_right_arg_value_) : Nothing<int>();
2136 void set_fixed_right_arg(Maybe<int> arg) {
2137 has_fixed_right_arg_ = arg.IsJust();
2138 if (arg.IsJust()) fixed_right_arg_value_ = arg.FromJust();
2141 virtual void RecordToBooleanTypeFeedback(TypeFeedbackOracle* oracle) override;
2144 BinaryOperation(Zone* zone, Token::Value op, Expression* left,
2145 Expression* right, int pos)
2146 : Expression(zone, pos),
2147 op_(static_cast<byte>(op)),
2148 has_fixed_right_arg_(false),
2149 fixed_right_arg_value_(0),
2152 DCHECK(Token::IsBinaryOp(op));
2154 static int parent_num_ids() { return Expression::num_ids(); }
2157 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
2159 const byte op_; // actually Token::Value
2160 // TODO(rossberg): the fixed arg should probably be represented as a Constant
2161 // type for the RHS. Currenty it's actually a Maybe<int>
2162 bool has_fixed_right_arg_;
2163 int fixed_right_arg_value_;
2166 Handle<AllocationSite> allocation_site_;
2170 class CountOperation final : public Expression {
2172 DECLARE_NODE_TYPE(CountOperation)
2174 bool is_prefix() const { return IsPrefixField::decode(bit_field_); }
2175 bool is_postfix() const { return !is_prefix(); }
2177 Token::Value op() const { return TokenField::decode(bit_field_); }
2178 Token::Value binary_op() {
2179 return (op() == Token::INC) ? Token::ADD : Token::SUB;
2182 Expression* expression() const { return expression_; }
2184 bool IsMonomorphic() override { return receiver_types_.length() == 1; }
2185 SmallMapList* GetReceiverTypes() override { return &receiver_types_; }
2186 IcCheckType GetKeyType() const override {
2187 return KeyTypeField::decode(bit_field_);
2189 KeyedAccessStoreMode GetStoreMode() const override {
2190 return StoreModeField::decode(bit_field_);
2192 Type* type() const { return type_; }
2193 void set_key_type(IcCheckType type) {
2194 bit_field_ = KeyTypeField::update(bit_field_, type);
2196 void set_store_mode(KeyedAccessStoreMode mode) {
2197 bit_field_ = StoreModeField::update(bit_field_, mode);
2199 void set_type(Type* type) { type_ = type; }
2201 static int num_ids() { return parent_num_ids() + 4; }
2202 BailoutId AssignmentId() const { return BailoutId(local_id(0)); }
2203 BailoutId ToNumberId() const { return BailoutId(local_id(1)); }
2204 TypeFeedbackId CountBinOpFeedbackId() const {
2205 return TypeFeedbackId(local_id(2));
2207 TypeFeedbackId CountStoreFeedbackId() const {
2208 return TypeFeedbackId(local_id(3));
2211 FeedbackVectorRequirements ComputeFeedbackRequirements(
2212 Isolate* isolate, const ICSlotCache* cache) override;
2213 void SetFirstFeedbackICSlot(FeedbackVectorICSlot slot,
2214 ICSlotCache* cache) override {
2217 Code::Kind FeedbackICSlotKind(int index) override;
2218 FeedbackVectorICSlot CountSlot() const { return slot_; }
2221 CountOperation(Zone* zone, Token::Value op, bool is_prefix, Expression* expr,
2223 : Expression(zone, pos),
2225 IsPrefixField::encode(is_prefix) | KeyTypeField::encode(ELEMENT) |
2226 StoreModeField::encode(STANDARD_STORE) | TokenField::encode(op)),
2229 slot_(FeedbackVectorICSlot::Invalid()) {}
2230 static int parent_num_ids() { return Expression::num_ids(); }
2233 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
2235 class IsPrefixField : public BitField16<bool, 0, 1> {};
2236 class KeyTypeField : public BitField16<IcCheckType, 1, 1> {};
2237 class StoreModeField : public BitField16<KeyedAccessStoreMode, 2, 3> {};
2238 class TokenField : public BitField16<Token::Value, 5, 8> {};
2240 // Starts with 16-bit field, which should get packed together with
2241 // Expression's trailing 16-bit field.
2242 uint16_t bit_field_;
2244 Expression* expression_;
2245 SmallMapList receiver_types_;
2246 FeedbackVectorICSlot slot_;
2250 class CompareOperation final : public Expression {
2252 DECLARE_NODE_TYPE(CompareOperation)
2254 Token::Value op() const { return op_; }
2255 Expression* left() const { return left_; }
2256 Expression* right() const { return right_; }
2258 // Type feedback information.
2259 static int num_ids() { return parent_num_ids() + 1; }
2260 TypeFeedbackId CompareOperationFeedbackId() const {
2261 return TypeFeedbackId(local_id(0));
2263 Type* combined_type() const { return combined_type_; }
2264 void set_combined_type(Type* type) { combined_type_ = type; }
2266 // Match special cases.
2267 bool IsLiteralCompareTypeof(Expression** expr, Handle<String>* check);
2268 bool IsLiteralCompareUndefined(Expression** expr, Isolate* isolate);
2269 bool IsLiteralCompareNull(Expression** expr);
2272 CompareOperation(Zone* zone, Token::Value op, Expression* left,
2273 Expression* right, int pos)
2274 : Expression(zone, pos),
2278 combined_type_(Type::None(zone)) {
2279 DCHECK(Token::IsCompareOp(op));
2281 static int parent_num_ids() { return Expression::num_ids(); }
2284 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
2290 Type* combined_type_;
2294 class Spread final : public Expression {
2296 DECLARE_NODE_TYPE(Spread)
2298 Expression* expression() const { return expression_; }
2300 static int num_ids() { return parent_num_ids(); }
2303 Spread(Zone* zone, Expression* expression, int pos)
2304 : Expression(zone, pos), expression_(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* expression_;
2314 class Conditional final : public Expression {
2316 DECLARE_NODE_TYPE(Conditional)
2318 Expression* condition() const { return condition_; }
2319 Expression* then_expression() const { return then_expression_; }
2320 Expression* else_expression() const { return else_expression_; }
2322 static int num_ids() { return parent_num_ids() + 2; }
2323 BailoutId ThenId() const { return BailoutId(local_id(0)); }
2324 BailoutId ElseId() const { return BailoutId(local_id(1)); }
2327 Conditional(Zone* zone, Expression* condition, Expression* then_expression,
2328 Expression* else_expression, int position)
2329 : Expression(zone, position),
2330 condition_(condition),
2331 then_expression_(then_expression),
2332 else_expression_(else_expression) {}
2333 static int parent_num_ids() { return Expression::num_ids(); }
2336 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
2338 Expression* condition_;
2339 Expression* then_expression_;
2340 Expression* else_expression_;
2344 class Assignment final : public Expression {
2346 DECLARE_NODE_TYPE(Assignment)
2348 Assignment* AsSimpleAssignment() { return !is_compound() ? this : NULL; }
2350 Token::Value binary_op() const;
2352 Token::Value op() const { return TokenField::decode(bit_field_); }
2353 Expression* target() const { return target_; }
2354 Expression* value() const { return value_; }
2355 BinaryOperation* binary_operation() const { return binary_operation_; }
2357 // This check relies on the definition order of token in token.h.
2358 bool is_compound() const { return op() > Token::ASSIGN; }
2360 static int num_ids() { return parent_num_ids() + 2; }
2361 BailoutId AssignmentId() const { return BailoutId(local_id(0)); }
2363 // Type feedback information.
2364 TypeFeedbackId AssignmentFeedbackId() { return TypeFeedbackId(local_id(1)); }
2365 bool IsMonomorphic() override { return receiver_types_.length() == 1; }
2366 bool IsUninitialized() const {
2367 return IsUninitializedField::decode(bit_field_);
2369 bool HasNoTypeInformation() {
2370 return IsUninitializedField::decode(bit_field_);
2372 SmallMapList* GetReceiverTypes() override { return &receiver_types_; }
2373 IcCheckType GetKeyType() const override {
2374 return KeyTypeField::decode(bit_field_);
2376 KeyedAccessStoreMode GetStoreMode() const override {
2377 return StoreModeField::decode(bit_field_);
2379 void set_is_uninitialized(bool b) {
2380 bit_field_ = IsUninitializedField::update(bit_field_, b);
2382 void set_key_type(IcCheckType key_type) {
2383 bit_field_ = KeyTypeField::update(bit_field_, key_type);
2385 void set_store_mode(KeyedAccessStoreMode mode) {
2386 bit_field_ = StoreModeField::update(bit_field_, mode);
2389 FeedbackVectorRequirements ComputeFeedbackRequirements(
2390 Isolate* isolate, const ICSlotCache* cache) override;
2391 void SetFirstFeedbackICSlot(FeedbackVectorICSlot slot,
2392 ICSlotCache* cache) override {
2395 Code::Kind FeedbackICSlotKind(int index) override;
2396 FeedbackVectorICSlot AssignmentSlot() const { return slot_; }
2399 Assignment(Zone* zone, Token::Value op, Expression* target, Expression* value,
2401 static int parent_num_ids() { return Expression::num_ids(); }
2404 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
2406 class IsUninitializedField : public BitField16<bool, 0, 1> {};
2407 class KeyTypeField : public BitField16<IcCheckType, 1, 1> {};
2408 class StoreModeField : public BitField16<KeyedAccessStoreMode, 2, 3> {};
2409 class TokenField : public BitField16<Token::Value, 5, 8> {};
2411 // Starts with 16-bit field, which should get packed together with
2412 // Expression's trailing 16-bit field.
2413 uint16_t bit_field_;
2414 Expression* target_;
2416 BinaryOperation* binary_operation_;
2417 SmallMapList receiver_types_;
2418 FeedbackVectorICSlot slot_;
2422 class Yield final : public Expression {
2424 DECLARE_NODE_TYPE(Yield)
2427 kInitial, // The initial yield that returns the unboxed generator object.
2428 kSuspend, // A normal yield: { value: EXPRESSION, done: false }
2429 kDelegating, // A yield*.
2430 kFinal // A return: { value: EXPRESSION, done: true }
2433 Expression* generator_object() const { return generator_object_; }
2434 Expression* expression() const { return expression_; }
2435 Kind yield_kind() const { return yield_kind_; }
2437 // Type feedback information.
2438 bool HasFeedbackSlots() const { return yield_kind() == kDelegating; }
2439 virtual FeedbackVectorRequirements ComputeFeedbackRequirements(
2440 Isolate* isolate, const ICSlotCache* cache) override {
2441 return FeedbackVectorRequirements(0, HasFeedbackSlots() ? 3 : 0);
2443 void SetFirstFeedbackICSlot(FeedbackVectorICSlot slot,
2444 ICSlotCache* cache) override {
2445 yield_first_feedback_slot_ = slot;
2447 Code::Kind FeedbackICSlotKind(int index) override {
2448 return index == 0 ? Code::KEYED_LOAD_IC : Code::LOAD_IC;
2451 FeedbackVectorICSlot KeyedLoadFeedbackSlot() {
2452 DCHECK(!HasFeedbackSlots() || !yield_first_feedback_slot_.IsInvalid());
2453 return yield_first_feedback_slot_;
2456 FeedbackVectorICSlot DoneFeedbackSlot() {
2457 return KeyedLoadFeedbackSlot().next();
2460 FeedbackVectorICSlot ValueFeedbackSlot() { return DoneFeedbackSlot().next(); }
2463 Yield(Zone* zone, Expression* generator_object, Expression* expression,
2464 Kind yield_kind, int pos)
2465 : Expression(zone, pos),
2466 generator_object_(generator_object),
2467 expression_(expression),
2468 yield_kind_(yield_kind),
2469 yield_first_feedback_slot_(FeedbackVectorICSlot::Invalid()) {}
2472 Expression* generator_object_;
2473 Expression* expression_;
2475 FeedbackVectorICSlot yield_first_feedback_slot_;
2479 class Throw final : public Expression {
2481 DECLARE_NODE_TYPE(Throw)
2483 Expression* exception() const { return exception_; }
2486 Throw(Zone* zone, Expression* exception, int pos)
2487 : Expression(zone, pos), exception_(exception) {}
2490 Expression* exception_;
2494 class FunctionLiteral final : public Expression {
2497 ANONYMOUS_EXPRESSION,
2502 enum ParameterFlag {
2503 kNoDuplicateParameters = 0,
2504 kHasDuplicateParameters = 1
2507 enum IsFunctionFlag {
2512 enum EagerCompileHint { kShouldEagerCompile, kShouldLazyCompile };
2514 enum ShouldBeUsedOnceHint { kShouldBeUsedOnce, kDontKnowIfShouldBeUsedOnce };
2516 enum ArityRestriction {
2522 DECLARE_NODE_TYPE(FunctionLiteral)
2524 Handle<String> name() const { return raw_name_->string(); }
2525 const AstRawString* raw_name() const { return raw_name_; }
2526 Scope* scope() const { return scope_; }
2527 ZoneList<Statement*>* body() const { return body_; }
2528 void set_function_token_position(int pos) { function_token_position_ = pos; }
2529 int function_token_position() const { return function_token_position_; }
2530 int start_position() const;
2531 int end_position() const;
2532 int SourceSize() const { return end_position() - start_position(); }
2533 bool is_expression() const { return IsExpression::decode(bitfield_); }
2534 bool is_anonymous() const { return IsAnonymous::decode(bitfield_); }
2535 LanguageMode language_mode() const;
2537 static bool NeedsHomeObject(Expression* expr);
2539 int materialized_literal_count() { return materialized_literal_count_; }
2540 int expected_property_count() { return expected_property_count_; }
2541 int parameter_count() { return parameter_count_; }
2543 bool AllowsLazyCompilation();
2544 bool AllowsLazyCompilationWithoutContext();
2546 Handle<String> debug_name() const {
2547 if (raw_name_ != NULL && !raw_name_->IsEmpty()) {
2548 return raw_name_->string();
2550 return inferred_name();
2553 Handle<String> inferred_name() const {
2554 if (!inferred_name_.is_null()) {
2555 DCHECK(raw_inferred_name_ == NULL);
2556 return inferred_name_;
2558 if (raw_inferred_name_ != NULL) {
2559 return raw_inferred_name_->string();
2562 return Handle<String>();
2565 // Only one of {set_inferred_name, set_raw_inferred_name} should be called.
2566 void set_inferred_name(Handle<String> inferred_name) {
2567 DCHECK(!inferred_name.is_null());
2568 inferred_name_ = inferred_name;
2569 DCHECK(raw_inferred_name_== NULL || raw_inferred_name_->IsEmpty());
2570 raw_inferred_name_ = NULL;
2573 void set_raw_inferred_name(const AstString* raw_inferred_name) {
2574 DCHECK(raw_inferred_name != NULL);
2575 raw_inferred_name_ = raw_inferred_name;
2576 DCHECK(inferred_name_.is_null());
2577 inferred_name_ = Handle<String>();
2580 bool pretenure() { return Pretenure::decode(bitfield_); }
2581 void set_pretenure() { bitfield_ |= Pretenure::encode(true); }
2583 bool has_duplicate_parameters() {
2584 return HasDuplicateParameters::decode(bitfield_);
2587 bool is_function() { return IsFunction::decode(bitfield_) == kIsFunction; }
2589 // This is used as a heuristic on when to eagerly compile a function
2590 // literal. We consider the following constructs as hints that the
2591 // function will be called immediately:
2592 // - (function() { ... })();
2593 // - var x = function() { ... }();
2594 bool should_eager_compile() const {
2595 return EagerCompileHintBit::decode(bitfield_) == kShouldEagerCompile;
2597 void set_should_eager_compile() {
2598 bitfield_ = EagerCompileHintBit::update(bitfield_, kShouldEagerCompile);
2601 // A hint that we expect this function to be called (exactly) once,
2602 // i.e. we suspect it's an initialization function.
2603 bool should_be_used_once_hint() const {
2604 return ShouldBeUsedOnceHintBit::decode(bitfield_) == kShouldBeUsedOnce;
2606 void set_should_be_used_once_hint() {
2607 bitfield_ = ShouldBeUsedOnceHintBit::update(bitfield_, kShouldBeUsedOnce);
2610 FunctionKind kind() const { return FunctionKindBits::decode(bitfield_); }
2612 int ast_node_count() { return ast_properties_.node_count(); }
2613 AstProperties::Flags flags() const { return ast_properties_.flags(); }
2614 void set_ast_properties(AstProperties* ast_properties) {
2615 ast_properties_ = *ast_properties;
2617 const ZoneFeedbackVectorSpec* feedback_vector_spec() const {
2618 return ast_properties_.get_spec();
2620 bool dont_optimize() { return dont_optimize_reason_ != kNoReason; }
2621 BailoutReason dont_optimize_reason() { return dont_optimize_reason_; }
2622 void set_dont_optimize_reason(BailoutReason reason) {
2623 dont_optimize_reason_ = reason;
2627 FunctionLiteral(Zone* zone, const AstRawString* name,
2628 AstValueFactory* ast_value_factory, Scope* scope,
2629 ZoneList<Statement*>* body, int materialized_literal_count,
2630 int expected_property_count, int parameter_count,
2631 FunctionType function_type,
2632 ParameterFlag has_duplicate_parameters,
2633 IsFunctionFlag is_function,
2634 EagerCompileHint eager_compile_hint, FunctionKind kind,
2636 : Expression(zone, position),
2640 raw_inferred_name_(ast_value_factory->empty_string()),
2641 ast_properties_(zone),
2642 dont_optimize_reason_(kNoReason),
2643 materialized_literal_count_(materialized_literal_count),
2644 expected_property_count_(expected_property_count),
2645 parameter_count_(parameter_count),
2646 function_token_position_(RelocInfo::kNoPosition) {
2647 bitfield_ = IsExpression::encode(function_type != DECLARATION) |
2648 IsAnonymous::encode(function_type == ANONYMOUS_EXPRESSION) |
2649 Pretenure::encode(false) |
2650 HasDuplicateParameters::encode(has_duplicate_parameters) |
2651 IsFunction::encode(is_function) |
2652 EagerCompileHintBit::encode(eager_compile_hint) |
2653 FunctionKindBits::encode(kind) |
2654 ShouldBeUsedOnceHintBit::encode(kDontKnowIfShouldBeUsedOnce);
2655 DCHECK(IsValidFunctionKind(kind));
2659 const AstRawString* raw_name_;
2660 Handle<String> name_;
2662 ZoneList<Statement*>* body_;
2663 const AstString* raw_inferred_name_;
2664 Handle<String> inferred_name_;
2665 AstProperties ast_properties_;
2666 BailoutReason dont_optimize_reason_;
2668 int materialized_literal_count_;
2669 int expected_property_count_;
2670 int parameter_count_;
2671 int function_token_position_;
2674 class IsExpression : public BitField<bool, 0, 1> {};
2675 class IsAnonymous : public BitField<bool, 1, 1> {};
2676 class Pretenure : public BitField<bool, 2, 1> {};
2677 class HasDuplicateParameters : public BitField<ParameterFlag, 3, 1> {};
2678 class IsFunction : public BitField<IsFunctionFlag, 4, 1> {};
2679 class EagerCompileHintBit : public BitField<EagerCompileHint, 5, 1> {};
2680 class FunctionKindBits : public BitField<FunctionKind, 6, 8> {};
2681 class ShouldBeUsedOnceHintBit : public BitField<ShouldBeUsedOnceHint, 15, 1> {
2686 class ClassLiteral final : public Expression {
2688 typedef ObjectLiteralProperty Property;
2690 DECLARE_NODE_TYPE(ClassLiteral)
2692 Handle<String> name() const { return raw_name_->string(); }
2693 const AstRawString* raw_name() const { return raw_name_; }
2694 Scope* scope() const { return scope_; }
2695 VariableProxy* class_variable_proxy() const { return class_variable_proxy_; }
2696 Expression* extends() const { return extends_; }
2697 FunctionLiteral* constructor() const { return constructor_; }
2698 ZoneList<Property*>* properties() const { return properties_; }
2699 int start_position() const { return position(); }
2700 int end_position() const { return end_position_; }
2702 BailoutId EntryId() const { return BailoutId(local_id(0)); }
2703 BailoutId DeclsId() const { return BailoutId(local_id(1)); }
2704 BailoutId ExitId() { return BailoutId(local_id(2)); }
2705 BailoutId CreateLiteralId() const { return BailoutId(local_id(3)); }
2707 // Return an AST id for a property that is used in simulate instructions.
2708 BailoutId GetIdForProperty(int i) { return BailoutId(local_id(i + 4)); }
2710 // Unlike other AST nodes, this number of bailout IDs allocated for an
2711 // ClassLiteral can vary, so num_ids() is not a static method.
2712 int num_ids() const { return parent_num_ids() + 4 + properties()->length(); }
2714 // Object literals need one feedback slot for each non-trivial value, as well
2715 // as some slots for home objects.
2716 FeedbackVectorRequirements ComputeFeedbackRequirements(
2717 Isolate* isolate, const ICSlotCache* cache) override;
2718 void SetFirstFeedbackICSlot(FeedbackVectorICSlot slot,
2719 ICSlotCache* cache) override {
2722 Code::Kind FeedbackICSlotKind(int index) override { return Code::STORE_IC; }
2723 FeedbackVectorICSlot GetNthSlot(int n) const {
2724 return FeedbackVectorICSlot(slot_.ToInt() + n);
2727 // If value needs a home object, returns a valid feedback vector ic slot
2728 // given by slot_index, and increments slot_index.
2729 FeedbackVectorICSlot SlotForHomeObject(Expression* value,
2730 int* slot_index) const;
2733 int slot_count() const { return slot_count_; }
2737 ClassLiteral(Zone* zone, const AstRawString* name, Scope* scope,
2738 VariableProxy* class_variable_proxy, Expression* extends,
2739 FunctionLiteral* constructor, ZoneList<Property*>* properties,
2740 int start_position, int end_position)
2741 : Expression(zone, start_position),
2744 class_variable_proxy_(class_variable_proxy),
2746 constructor_(constructor),
2747 properties_(properties),
2748 end_position_(end_position),
2752 slot_(FeedbackVectorICSlot::Invalid()) {
2755 static int parent_num_ids() { return Expression::num_ids(); }
2758 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
2760 const AstRawString* raw_name_;
2762 VariableProxy* class_variable_proxy_;
2763 Expression* extends_;
2764 FunctionLiteral* constructor_;
2765 ZoneList<Property*>* properties_;
2768 // slot_count_ helps validate that the logic to allocate ic slots and the
2769 // logic to use them are in sync.
2772 FeedbackVectorICSlot slot_;
2776 class NativeFunctionLiteral final : public Expression {
2778 DECLARE_NODE_TYPE(NativeFunctionLiteral)
2780 Handle<String> name() const { return name_->string(); }
2781 v8::Extension* extension() const { return extension_; }
2784 NativeFunctionLiteral(Zone* zone, const AstRawString* name,
2785 v8::Extension* extension, int pos)
2786 : Expression(zone, pos), name_(name), extension_(extension) {}
2789 const AstRawString* name_;
2790 v8::Extension* extension_;
2794 class ThisFunction final : public Expression {
2796 DECLARE_NODE_TYPE(ThisFunction)
2799 ThisFunction(Zone* zone, int pos) : Expression(zone, pos) {}
2803 class SuperPropertyReference final : public Expression {
2805 DECLARE_NODE_TYPE(SuperPropertyReference)
2807 VariableProxy* this_var() const { return this_var_; }
2808 Expression* home_object() const { return home_object_; }
2811 SuperPropertyReference(Zone* zone, VariableProxy* this_var,
2812 Expression* home_object, int pos)
2813 : Expression(zone, pos), this_var_(this_var), home_object_(home_object) {
2814 DCHECK(this_var->is_this());
2815 DCHECK(home_object->IsProperty());
2819 VariableProxy* this_var_;
2820 Expression* home_object_;
2824 class SuperCallReference final : public Expression {
2826 DECLARE_NODE_TYPE(SuperCallReference)
2828 VariableProxy* this_var() const { return this_var_; }
2829 VariableProxy* new_target_var() const { return new_target_var_; }
2830 VariableProxy* this_function_var() const { return this_function_var_; }
2833 SuperCallReference(Zone* zone, VariableProxy* this_var,
2834 VariableProxy* new_target_var,
2835 VariableProxy* this_function_var, int pos)
2836 : Expression(zone, pos),
2837 this_var_(this_var),
2838 new_target_var_(new_target_var),
2839 this_function_var_(this_function_var) {
2840 DCHECK(this_var->is_this());
2841 DCHECK(new_target_var->raw_name()->IsOneByteEqualTo(".new.target"));
2842 DCHECK(this_function_var->raw_name()->IsOneByteEqualTo(".this_function"));
2846 VariableProxy* this_var_;
2847 VariableProxy* new_target_var_;
2848 VariableProxy* this_function_var_;
2852 // This class is produced when parsing the () in arrow functions without any
2853 // arguments and is not actually a valid expression.
2854 class EmptyParentheses final : public Expression {
2856 DECLARE_NODE_TYPE(EmptyParentheses)
2859 EmptyParentheses(Zone* zone, int pos) : Expression(zone, pos) {}
2863 #undef DECLARE_NODE_TYPE
2866 // ----------------------------------------------------------------------------
2867 // Regular expressions
2870 class RegExpVisitor BASE_EMBEDDED {
2872 virtual ~RegExpVisitor() { }
2873 #define MAKE_CASE(Name) \
2874 virtual void* Visit##Name(RegExp##Name*, void* data) = 0;
2875 FOR_EACH_REG_EXP_TREE_TYPE(MAKE_CASE)
2880 class RegExpTree : public ZoneObject {
2882 static const int kInfinity = kMaxInt;
2883 virtual ~RegExpTree() {}
2884 virtual void* Accept(RegExpVisitor* visitor, void* data) = 0;
2885 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
2886 RegExpNode* on_success) = 0;
2887 virtual bool IsTextElement() { return false; }
2888 virtual bool IsAnchoredAtStart() { return false; }
2889 virtual bool IsAnchoredAtEnd() { return false; }
2890 virtual int min_match() = 0;
2891 virtual int max_match() = 0;
2892 // Returns the interval of registers used for captures within this
2894 virtual Interval CaptureRegisters() { return Interval::Empty(); }
2895 virtual void AppendToText(RegExpText* text, Zone* zone);
2896 std::ostream& Print(std::ostream& os, Zone* zone); // NOLINT
2897 #define MAKE_ASTYPE(Name) \
2898 virtual RegExp##Name* As##Name(); \
2899 virtual bool Is##Name();
2900 FOR_EACH_REG_EXP_TREE_TYPE(MAKE_ASTYPE)
2905 class RegExpDisjunction final : public RegExpTree {
2907 explicit RegExpDisjunction(ZoneList<RegExpTree*>* alternatives);
2908 void* Accept(RegExpVisitor* visitor, void* data) override;
2909 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
2910 RegExpNode* on_success) override;
2911 RegExpDisjunction* AsDisjunction() override;
2912 Interval CaptureRegisters() override;
2913 bool IsDisjunction() override;
2914 bool IsAnchoredAtStart() override;
2915 bool IsAnchoredAtEnd() override;
2916 int min_match() override { return min_match_; }
2917 int max_match() override { return max_match_; }
2918 ZoneList<RegExpTree*>* alternatives() { return alternatives_; }
2920 bool SortConsecutiveAtoms(RegExpCompiler* compiler);
2921 void RationalizeConsecutiveAtoms(RegExpCompiler* compiler);
2922 void FixSingleCharacterDisjunctions(RegExpCompiler* compiler);
2923 ZoneList<RegExpTree*>* alternatives_;
2929 class RegExpAlternative final : public RegExpTree {
2931 explicit RegExpAlternative(ZoneList<RegExpTree*>* nodes);
2932 void* Accept(RegExpVisitor* visitor, void* data) override;
2933 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
2934 RegExpNode* on_success) override;
2935 RegExpAlternative* AsAlternative() override;
2936 Interval CaptureRegisters() override;
2937 bool IsAlternative() override;
2938 bool IsAnchoredAtStart() override;
2939 bool IsAnchoredAtEnd() override;
2940 int min_match() override { return min_match_; }
2941 int max_match() override { return max_match_; }
2942 ZoneList<RegExpTree*>* nodes() { return nodes_; }
2944 ZoneList<RegExpTree*>* nodes_;
2950 class RegExpAssertion final : public RegExpTree {
2952 enum AssertionType {
2960 explicit RegExpAssertion(AssertionType type) : assertion_type_(type) { }
2961 void* Accept(RegExpVisitor* visitor, void* data) override;
2962 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
2963 RegExpNode* on_success) override;
2964 RegExpAssertion* AsAssertion() override;
2965 bool IsAssertion() override;
2966 bool IsAnchoredAtStart() override;
2967 bool IsAnchoredAtEnd() override;
2968 int min_match() override { return 0; }
2969 int max_match() override { return 0; }
2970 AssertionType assertion_type() { return assertion_type_; }
2972 AssertionType assertion_type_;
2976 class CharacterSet final BASE_EMBEDDED {
2978 explicit CharacterSet(uc16 standard_set_type)
2980 standard_set_type_(standard_set_type) {}
2981 explicit CharacterSet(ZoneList<CharacterRange>* ranges)
2983 standard_set_type_(0) {}
2984 ZoneList<CharacterRange>* ranges(Zone* zone);
2985 uc16 standard_set_type() { return standard_set_type_; }
2986 void set_standard_set_type(uc16 special_set_type) {
2987 standard_set_type_ = special_set_type;
2989 bool is_standard() { return standard_set_type_ != 0; }
2990 void Canonicalize();
2992 ZoneList<CharacterRange>* ranges_;
2993 // If non-zero, the value represents a standard set (e.g., all whitespace
2994 // characters) without having to expand the ranges.
2995 uc16 standard_set_type_;
2999 class RegExpCharacterClass final : public RegExpTree {
3001 RegExpCharacterClass(ZoneList<CharacterRange>* ranges, bool is_negated)
3003 is_negated_(is_negated) { }
3004 explicit RegExpCharacterClass(uc16 type)
3006 is_negated_(false) { }
3007 void* Accept(RegExpVisitor* visitor, void* data) override;
3008 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
3009 RegExpNode* on_success) override;
3010 RegExpCharacterClass* AsCharacterClass() override;
3011 bool IsCharacterClass() override;
3012 bool IsTextElement() override { return true; }
3013 int min_match() override { return 1; }
3014 int max_match() override { return 1; }
3015 void AppendToText(RegExpText* text, Zone* zone) override;
3016 CharacterSet character_set() { return set_; }
3017 // TODO(lrn): Remove need for complex version if is_standard that
3018 // recognizes a mangled standard set and just do { return set_.is_special(); }
3019 bool is_standard(Zone* zone);
3020 // Returns a value representing the standard character set if is_standard()
3022 // Currently used values are:
3023 // s : unicode whitespace
3024 // S : unicode non-whitespace
3025 // w : ASCII word character (digit, letter, underscore)
3026 // W : non-ASCII word character
3028 // D : non-ASCII digit
3029 // . : non-unicode non-newline
3030 // * : All characters
3031 uc16 standard_type() { return set_.standard_set_type(); }
3032 ZoneList<CharacterRange>* ranges(Zone* zone) { return set_.ranges(zone); }
3033 bool is_negated() { return is_negated_; }
3041 class RegExpAtom final : public RegExpTree {
3043 explicit RegExpAtom(Vector<const uc16> data) : data_(data) { }
3044 void* Accept(RegExpVisitor* visitor, void* data) override;
3045 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
3046 RegExpNode* on_success) override;
3047 RegExpAtom* AsAtom() override;
3048 bool IsAtom() override;
3049 bool IsTextElement() override { return true; }
3050 int min_match() override { return data_.length(); }
3051 int max_match() override { return data_.length(); }
3052 void AppendToText(RegExpText* text, Zone* zone) override;
3053 Vector<const uc16> data() { return data_; }
3054 int length() { return data_.length(); }
3056 Vector<const uc16> data_;
3060 class RegExpText final : public RegExpTree {
3062 explicit RegExpText(Zone* zone) : elements_(2, zone), length_(0) {}
3063 void* Accept(RegExpVisitor* visitor, void* data) override;
3064 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
3065 RegExpNode* on_success) override;
3066 RegExpText* AsText() override;
3067 bool IsText() override;
3068 bool IsTextElement() override { return true; }
3069 int min_match() override { return length_; }
3070 int max_match() override { return length_; }
3071 void AppendToText(RegExpText* text, Zone* zone) override;
3072 void AddElement(TextElement elm, Zone* zone) {
3073 elements_.Add(elm, zone);
3074 length_ += elm.length();
3076 ZoneList<TextElement>* elements() { return &elements_; }
3078 ZoneList<TextElement> elements_;
3083 class RegExpQuantifier final : public RegExpTree {
3085 enum QuantifierType { GREEDY, NON_GREEDY, POSSESSIVE };
3086 RegExpQuantifier(int min, int max, QuantifierType type, RegExpTree* body)
3090 min_match_(min * body->min_match()),
3091 quantifier_type_(type) {
3092 if (max > 0 && body->max_match() > kInfinity / max) {
3093 max_match_ = kInfinity;
3095 max_match_ = max * body->max_match();
3098 void* Accept(RegExpVisitor* visitor, void* data) override;
3099 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
3100 RegExpNode* on_success) override;
3101 static RegExpNode* ToNode(int min,
3105 RegExpCompiler* compiler,
3106 RegExpNode* on_success,
3107 bool not_at_start = false);
3108 RegExpQuantifier* AsQuantifier() override;
3109 Interval CaptureRegisters() override;
3110 bool IsQuantifier() override;
3111 int min_match() override { return min_match_; }
3112 int max_match() override { return max_match_; }
3113 int min() { return min_; }
3114 int max() { return max_; }
3115 bool is_possessive() { return quantifier_type_ == POSSESSIVE; }
3116 bool is_non_greedy() { return quantifier_type_ == NON_GREEDY; }
3117 bool is_greedy() { return quantifier_type_ == GREEDY; }
3118 RegExpTree* body() { return body_; }
3126 QuantifierType quantifier_type_;
3130 class RegExpCapture final : public RegExpTree {
3132 explicit RegExpCapture(RegExpTree* body, int index)
3133 : body_(body), index_(index) { }
3134 void* Accept(RegExpVisitor* visitor, void* data) override;
3135 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
3136 RegExpNode* on_success) override;
3137 static RegExpNode* ToNode(RegExpTree* body,
3139 RegExpCompiler* compiler,
3140 RegExpNode* on_success);
3141 RegExpCapture* AsCapture() override;
3142 bool IsAnchoredAtStart() override;
3143 bool IsAnchoredAtEnd() override;
3144 Interval CaptureRegisters() override;
3145 bool IsCapture() override;
3146 int min_match() override { return body_->min_match(); }
3147 int max_match() override { return body_->max_match(); }
3148 RegExpTree* body() { return body_; }
3149 int index() { return index_; }
3150 static int StartRegister(int index) { return index * 2; }
3151 static int EndRegister(int index) { return index * 2 + 1; }
3159 class RegExpLookahead final : public RegExpTree {
3161 RegExpLookahead(RegExpTree* body,
3166 is_positive_(is_positive),
3167 capture_count_(capture_count),
3168 capture_from_(capture_from) { }
3170 void* Accept(RegExpVisitor* visitor, void* data) override;
3171 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
3172 RegExpNode* on_success) override;
3173 RegExpLookahead* AsLookahead() override;
3174 Interval CaptureRegisters() override;
3175 bool IsLookahead() override;
3176 bool IsAnchoredAtStart() override;
3177 int min_match() override { return 0; }
3178 int max_match() override { return 0; }
3179 RegExpTree* body() { return body_; }
3180 bool is_positive() { return is_positive_; }
3181 int capture_count() { return capture_count_; }
3182 int capture_from() { return capture_from_; }
3192 class RegExpBackReference final : public RegExpTree {
3194 explicit RegExpBackReference(RegExpCapture* capture)
3195 : capture_(capture) { }
3196 void* Accept(RegExpVisitor* visitor, void* data) override;
3197 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
3198 RegExpNode* on_success) override;
3199 RegExpBackReference* AsBackReference() override;
3200 bool IsBackReference() override;
3201 int min_match() override { return 0; }
3202 int max_match() override { return capture_->max_match(); }
3203 int index() { return capture_->index(); }
3204 RegExpCapture* capture() { return capture_; }
3206 RegExpCapture* capture_;
3210 class RegExpEmpty final : public RegExpTree {
3213 void* Accept(RegExpVisitor* visitor, void* data) override;
3214 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
3215 RegExpNode* on_success) override;
3216 RegExpEmpty* AsEmpty() override;
3217 bool IsEmpty() override;
3218 int min_match() override { return 0; }
3219 int max_match() override { return 0; }
3223 // ----------------------------------------------------------------------------
3225 // - leaf node visitors are abstract.
3227 class AstVisitor BASE_EMBEDDED {
3230 virtual ~AstVisitor() {}
3232 // Stack overflow check and dynamic dispatch.
3233 virtual void Visit(AstNode* node) = 0;
3235 // Iteration left-to-right.
3236 virtual void VisitDeclarations(ZoneList<Declaration*>* declarations);
3237 virtual void VisitStatements(ZoneList<Statement*>* statements);
3238 virtual void VisitExpressions(ZoneList<Expression*>* expressions);
3240 // Individual AST nodes.
3241 #define DEF_VISIT(type) \
3242 virtual void Visit##type(type* node) = 0;
3243 AST_NODE_LIST(DEF_VISIT)
3248 #define DEFINE_AST_VISITOR_SUBCLASS_MEMBERS() \
3250 void Visit(AstNode* node) final { \
3251 if (!CheckStackOverflow()) node->Accept(this); \
3254 void SetStackOverflow() { stack_overflow_ = true; } \
3255 void ClearStackOverflow() { stack_overflow_ = false; } \
3256 bool HasStackOverflow() const { return stack_overflow_; } \
3258 bool CheckStackOverflow() { \
3259 if (stack_overflow_) return true; \
3260 StackLimitCheck check(isolate_); \
3261 if (!check.HasOverflowed()) return false; \
3262 stack_overflow_ = true; \
3267 void InitializeAstVisitor(Isolate* isolate, Zone* zone) { \
3268 isolate_ = isolate; \
3270 stack_overflow_ = false; \
3272 Zone* zone() { return zone_; } \
3273 Isolate* isolate() { return isolate_; } \
3275 Isolate* isolate_; \
3277 bool stack_overflow_
3280 // ----------------------------------------------------------------------------
3283 class AstNodeFactory final BASE_EMBEDDED {
3285 explicit AstNodeFactory(AstValueFactory* ast_value_factory)
3286 : zone_(ast_value_factory->zone()),
3287 ast_value_factory_(ast_value_factory) {}
3289 VariableDeclaration* NewVariableDeclaration(
3290 VariableProxy* proxy, VariableMode mode, Scope* scope, int pos,
3291 bool is_class_declaration = false, int declaration_group_start = -1) {
3293 VariableDeclaration(zone_, proxy, mode, scope, pos,
3294 is_class_declaration, declaration_group_start);
3297 FunctionDeclaration* NewFunctionDeclaration(VariableProxy* proxy,
3299 FunctionLiteral* fun,
3302 return new (zone_) FunctionDeclaration(zone_, proxy, mode, fun, scope, pos);
3305 ImportDeclaration* NewImportDeclaration(VariableProxy* proxy,
3306 const AstRawString* import_name,
3307 const AstRawString* module_specifier,
3308 Scope* scope, int pos) {
3309 return new (zone_) ImportDeclaration(zone_, proxy, import_name,
3310 module_specifier, scope, pos);
3313 ExportDeclaration* NewExportDeclaration(VariableProxy* proxy,
3316 return new (zone_) ExportDeclaration(zone_, proxy, scope, pos);
3319 Block* NewBlock(ZoneList<const AstRawString*>* labels, int capacity,
3320 bool ignore_completion_value, int pos) {
3322 Block(zone_, labels, capacity, ignore_completion_value, pos);
3325 #define STATEMENT_WITH_LABELS(NodeType) \
3326 NodeType* New##NodeType(ZoneList<const AstRawString*>* labels, int pos) { \
3327 return new (zone_) NodeType(zone_, labels, pos); \
3329 STATEMENT_WITH_LABELS(DoWhileStatement)
3330 STATEMENT_WITH_LABELS(WhileStatement)
3331 STATEMENT_WITH_LABELS(ForStatement)
3332 STATEMENT_WITH_LABELS(SwitchStatement)
3333 #undef STATEMENT_WITH_LABELS
3335 ForEachStatement* NewForEachStatement(ForEachStatement::VisitMode visit_mode,
3336 ZoneList<const AstRawString*>* labels,
3338 switch (visit_mode) {
3339 case ForEachStatement::ENUMERATE: {
3340 return new (zone_) ForInStatement(zone_, labels, pos);
3342 case ForEachStatement::ITERATE: {
3343 return new (zone_) ForOfStatement(zone_, labels, pos);
3350 ExpressionStatement* NewExpressionStatement(Expression* expression, int pos) {
3351 return new (zone_) ExpressionStatement(zone_, expression, pos);
3354 ContinueStatement* NewContinueStatement(IterationStatement* target, int pos) {
3355 return new (zone_) ContinueStatement(zone_, target, pos);
3358 BreakStatement* NewBreakStatement(BreakableStatement* target, int pos) {
3359 return new (zone_) BreakStatement(zone_, target, pos);
3362 ReturnStatement* NewReturnStatement(Expression* expression, int pos) {
3363 return new (zone_) ReturnStatement(zone_, expression, pos);
3366 WithStatement* NewWithStatement(Scope* scope,
3367 Expression* expression,
3368 Statement* statement,
3370 return new (zone_) WithStatement(zone_, scope, expression, statement, pos);
3373 IfStatement* NewIfStatement(Expression* condition,
3374 Statement* then_statement,
3375 Statement* else_statement,
3378 IfStatement(zone_, condition, then_statement, else_statement, pos);
3381 TryCatchStatement* NewTryCatchStatement(Block* try_block, Scope* scope,
3383 Block* catch_block, int pos) {
3385 TryCatchStatement(zone_, try_block, scope, variable, catch_block, pos);
3388 TryFinallyStatement* NewTryFinallyStatement(Block* try_block,
3389 Block* finally_block, int pos) {
3391 TryFinallyStatement(zone_, try_block, finally_block, pos);
3394 DebuggerStatement* NewDebuggerStatement(int pos) {
3395 return new (zone_) DebuggerStatement(zone_, pos);
3398 EmptyStatement* NewEmptyStatement(int pos) {
3399 return new(zone_) EmptyStatement(zone_, pos);
3402 CaseClause* NewCaseClause(
3403 Expression* label, ZoneList<Statement*>* statements, int pos) {
3404 return new (zone_) CaseClause(zone_, label, statements, pos);
3407 Literal* NewStringLiteral(const AstRawString* string, int pos) {
3409 Literal(zone_, ast_value_factory_->NewString(string), pos);
3412 // A JavaScript symbol (ECMA-262 edition 6).
3413 Literal* NewSymbolLiteral(const char* name, int pos) {
3414 return new (zone_) Literal(zone_, ast_value_factory_->NewSymbol(name), pos);
3417 Literal* NewNumberLiteral(double number, int pos, bool with_dot = false) {
3419 Literal(zone_, ast_value_factory_->NewNumber(number, with_dot), pos);
3422 Literal* NewSmiLiteral(int number, int pos) {
3423 return new (zone_) Literal(zone_, ast_value_factory_->NewSmi(number), pos);
3426 Literal* NewBooleanLiteral(bool b, int pos) {
3427 return new (zone_) Literal(zone_, ast_value_factory_->NewBoolean(b), pos);
3430 Literal* NewNullLiteral(int pos) {
3431 return new (zone_) Literal(zone_, ast_value_factory_->NewNull(), pos);
3434 Literal* NewUndefinedLiteral(int pos) {
3435 return new (zone_) Literal(zone_, ast_value_factory_->NewUndefined(), pos);
3438 Literal* NewTheHoleLiteral(int pos) {
3439 return new (zone_) Literal(zone_, ast_value_factory_->NewTheHole(), pos);
3442 ObjectLiteral* NewObjectLiteral(
3443 ZoneList<ObjectLiteral::Property*>* properties,
3445 int boilerplate_properties,
3449 return new (zone_) ObjectLiteral(zone_, properties, literal_index,
3450 boilerplate_properties, has_function,
3454 ObjectLiteral::Property* NewObjectLiteralProperty(
3455 Expression* key, Expression* value, ObjectLiteralProperty::Kind kind,
3456 bool is_static, bool is_computed_name) {
3458 ObjectLiteral::Property(key, value, kind, is_static, is_computed_name);
3461 ObjectLiteral::Property* NewObjectLiteralProperty(Expression* key,
3464 bool is_computed_name) {
3465 return new (zone_) ObjectLiteral::Property(ast_value_factory_, key, value,
3466 is_static, is_computed_name);
3469 RegExpLiteral* NewRegExpLiteral(const AstRawString* pattern,
3470 const AstRawString* flags,
3474 return new (zone_) RegExpLiteral(zone_, pattern, flags, literal_index,
3478 ArrayLiteral* NewArrayLiteral(ZoneList<Expression*>* values,
3483 ArrayLiteral(zone_, values, -1, literal_index, is_strong, pos);
3486 ArrayLiteral* NewArrayLiteral(ZoneList<Expression*>* values,
3487 int first_spread_index, int literal_index,
3488 bool is_strong, int pos) {
3489 return new (zone_) ArrayLiteral(zone_, values, first_spread_index,
3490 literal_index, is_strong, pos);
3493 VariableProxy* NewVariableProxy(Variable* var,
3494 int start_position = RelocInfo::kNoPosition,
3495 int end_position = RelocInfo::kNoPosition) {
3496 return new (zone_) VariableProxy(zone_, var, start_position, end_position);
3499 VariableProxy* NewVariableProxy(const AstRawString* name,
3500 Variable::Kind variable_kind,
3501 int start_position = RelocInfo::kNoPosition,
3502 int end_position = RelocInfo::kNoPosition) {
3503 DCHECK_NOT_NULL(name);
3505 VariableProxy(zone_, name, variable_kind, start_position, end_position);
3508 Property* NewProperty(Expression* obj, Expression* key, int pos) {
3509 return new (zone_) Property(zone_, obj, key, pos);
3512 Call* NewCall(Expression* expression,
3513 ZoneList<Expression*>* arguments,
3515 return new (zone_) Call(zone_, expression, arguments, pos);
3518 CallNew* NewCallNew(Expression* expression,
3519 ZoneList<Expression*>* arguments,
3521 return new (zone_) CallNew(zone_, expression, arguments, pos);
3524 CallRuntime* NewCallRuntime(const AstRawString* name,
3525 const Runtime::Function* function,
3526 ZoneList<Expression*>* arguments,
3528 return new (zone_) CallRuntime(zone_, name, function, arguments, pos);
3531 UnaryOperation* NewUnaryOperation(Token::Value op,
3532 Expression* expression,
3534 return new (zone_) UnaryOperation(zone_, op, expression, pos);
3537 BinaryOperation* NewBinaryOperation(Token::Value op,
3541 return new (zone_) BinaryOperation(zone_, op, left, right, pos);
3544 CountOperation* NewCountOperation(Token::Value op,
3548 return new (zone_) CountOperation(zone_, op, is_prefix, expr, pos);
3551 CompareOperation* NewCompareOperation(Token::Value op,
3555 return new (zone_) CompareOperation(zone_, op, left, right, pos);
3558 Spread* NewSpread(Expression* expression, int pos) {
3559 return new (zone_) Spread(zone_, expression, pos);
3562 Conditional* NewConditional(Expression* condition,
3563 Expression* then_expression,
3564 Expression* else_expression,
3566 return new (zone_) Conditional(zone_, condition, then_expression,
3567 else_expression, position);
3570 Assignment* NewAssignment(Token::Value op,
3574 DCHECK(Token::IsAssignmentOp(op));
3575 Assignment* assign = new (zone_) Assignment(zone_, op, target, value, pos);
3576 if (assign->is_compound()) {
3577 DCHECK(Token::IsAssignmentOp(op));
3578 assign->binary_operation_ =
3579 NewBinaryOperation(assign->binary_op(), target, value, pos + 1);
3584 Yield* NewYield(Expression *generator_object,
3585 Expression* expression,
3586 Yield::Kind yield_kind,
3588 if (!expression) expression = NewUndefinedLiteral(pos);
3590 Yield(zone_, generator_object, expression, yield_kind, pos);
3593 Throw* NewThrow(Expression* exception, int pos) {
3594 return new (zone_) Throw(zone_, exception, pos);
3597 FunctionLiteral* NewFunctionLiteral(
3598 const AstRawString* name, AstValueFactory* ast_value_factory,
3599 Scope* scope, ZoneList<Statement*>* body, int materialized_literal_count,
3600 int expected_property_count, int parameter_count,
3601 FunctionLiteral::ParameterFlag has_duplicate_parameters,
3602 FunctionLiteral::FunctionType function_type,
3603 FunctionLiteral::IsFunctionFlag is_function,
3604 FunctionLiteral::EagerCompileHint eager_compile_hint, FunctionKind kind,
3606 return new (zone_) FunctionLiteral(
3607 zone_, name, ast_value_factory, scope, body, materialized_literal_count,
3608 expected_property_count, parameter_count, function_type,
3609 has_duplicate_parameters, is_function, eager_compile_hint, kind,
3613 ClassLiteral* NewClassLiteral(const AstRawString* name, Scope* scope,
3614 VariableProxy* proxy, Expression* extends,
3615 FunctionLiteral* constructor,
3616 ZoneList<ObjectLiteral::Property*>* properties,
3617 int start_position, int end_position) {
3619 ClassLiteral(zone_, name, scope, proxy, extends, constructor,
3620 properties, start_position, end_position);
3623 NativeFunctionLiteral* NewNativeFunctionLiteral(const AstRawString* name,
3624 v8::Extension* extension,
3626 return new (zone_) NativeFunctionLiteral(zone_, name, extension, pos);
3629 ThisFunction* NewThisFunction(int pos) {
3630 return new (zone_) ThisFunction(zone_, pos);
3633 SuperPropertyReference* NewSuperPropertyReference(VariableProxy* this_var,
3634 Expression* home_object,
3637 SuperPropertyReference(zone_, this_var, home_object, pos);
3640 SuperCallReference* NewSuperCallReference(VariableProxy* this_var,
3641 VariableProxy* new_target_var,
3642 VariableProxy* this_function_var,
3644 return new (zone_) SuperCallReference(zone_, this_var, new_target_var,
3645 this_function_var, pos);
3648 EmptyParentheses* NewEmptyParentheses(int pos) {
3649 return new (zone_) EmptyParentheses(zone_, pos);
3654 AstValueFactory* ast_value_factory_;
3658 } } // namespace v8::internal