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()),
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 ZoneList<Expression*>* arguments() const { return arguments_; }
2036 bool is_jsruntime() const { return function_ == NULL; }
2038 int context_index() const {
2039 DCHECK(is_jsruntime());
2040 return context_index_;
2042 const Runtime::Function* function() const {
2043 DCHECK(!is_jsruntime());
2047 static int num_ids() { return parent_num_ids() + 1; }
2048 BailoutId CallId() { return BailoutId(local_id(0)); }
2050 const char* debug_name() {
2051 return is_jsruntime() ? "(context function)" : function_->name;
2055 CallRuntime(Zone* zone, const Runtime::Function* function,
2056 ZoneList<Expression*>* arguments, int pos)
2057 : Expression(zone, pos), function_(function), arguments_(arguments) {}
2059 CallRuntime(Zone* zone, int context_index, ZoneList<Expression*>* arguments,
2061 : Expression(zone, pos),
2063 context_index_(context_index),
2064 arguments_(arguments) {}
2066 static int parent_num_ids() { return Expression::num_ids(); }
2069 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
2071 const Runtime::Function* function_;
2073 ZoneList<Expression*>* arguments_;
2077 class UnaryOperation final : public Expression {
2079 DECLARE_NODE_TYPE(UnaryOperation)
2081 Token::Value op() const { return op_; }
2082 Expression* expression() const { return expression_; }
2084 // For unary not (Token::NOT), the AST ids where true and false will
2085 // actually be materialized, respectively.
2086 static int num_ids() { return parent_num_ids() + 2; }
2087 BailoutId MaterializeTrueId() const { return BailoutId(local_id(0)); }
2088 BailoutId MaterializeFalseId() const { return BailoutId(local_id(1)); }
2090 virtual void RecordToBooleanTypeFeedback(TypeFeedbackOracle* oracle) override;
2093 UnaryOperation(Zone* zone, Token::Value op, Expression* expression, int pos)
2094 : Expression(zone, pos), op_(op), expression_(expression) {
2095 DCHECK(Token::IsUnaryOp(op));
2097 static int parent_num_ids() { return Expression::num_ids(); }
2100 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
2103 Expression* expression_;
2107 class BinaryOperation final : public Expression {
2109 DECLARE_NODE_TYPE(BinaryOperation)
2111 Token::Value op() const { return static_cast<Token::Value>(op_); }
2112 Expression* left() const { return left_; }
2113 Expression* right() const { return right_; }
2114 Handle<AllocationSite> allocation_site() const { return allocation_site_; }
2115 void set_allocation_site(Handle<AllocationSite> allocation_site) {
2116 allocation_site_ = allocation_site;
2119 // The short-circuit logical operations need an AST ID for their
2120 // right-hand subexpression.
2121 static int num_ids() { return parent_num_ids() + 2; }
2122 BailoutId RightId() const { return BailoutId(local_id(0)); }
2124 TypeFeedbackId BinaryOperationFeedbackId() const {
2125 return TypeFeedbackId(local_id(1));
2127 Maybe<int> fixed_right_arg() const {
2128 return has_fixed_right_arg_ ? Just(fixed_right_arg_value_) : Nothing<int>();
2130 void set_fixed_right_arg(Maybe<int> arg) {
2131 has_fixed_right_arg_ = arg.IsJust();
2132 if (arg.IsJust()) fixed_right_arg_value_ = arg.FromJust();
2135 virtual void RecordToBooleanTypeFeedback(TypeFeedbackOracle* oracle) override;
2138 BinaryOperation(Zone* zone, Token::Value op, Expression* left,
2139 Expression* right, int pos)
2140 : Expression(zone, pos),
2141 op_(static_cast<byte>(op)),
2142 has_fixed_right_arg_(false),
2143 fixed_right_arg_value_(0),
2146 DCHECK(Token::IsBinaryOp(op));
2148 static int parent_num_ids() { return Expression::num_ids(); }
2151 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
2153 const byte op_; // actually Token::Value
2154 // TODO(rossberg): the fixed arg should probably be represented as a Constant
2155 // type for the RHS. Currenty it's actually a Maybe<int>
2156 bool has_fixed_right_arg_;
2157 int fixed_right_arg_value_;
2160 Handle<AllocationSite> allocation_site_;
2164 class CountOperation final : public Expression {
2166 DECLARE_NODE_TYPE(CountOperation)
2168 bool is_prefix() const { return IsPrefixField::decode(bit_field_); }
2169 bool is_postfix() const { return !is_prefix(); }
2171 Token::Value op() const { return TokenField::decode(bit_field_); }
2172 Token::Value binary_op() {
2173 return (op() == Token::INC) ? Token::ADD : Token::SUB;
2176 Expression* expression() const { return expression_; }
2178 bool IsMonomorphic() override { return receiver_types_.length() == 1; }
2179 SmallMapList* GetReceiverTypes() override { return &receiver_types_; }
2180 IcCheckType GetKeyType() const override {
2181 return KeyTypeField::decode(bit_field_);
2183 KeyedAccessStoreMode GetStoreMode() const override {
2184 return StoreModeField::decode(bit_field_);
2186 Type* type() const { return type_; }
2187 void set_key_type(IcCheckType type) {
2188 bit_field_ = KeyTypeField::update(bit_field_, type);
2190 void set_store_mode(KeyedAccessStoreMode mode) {
2191 bit_field_ = StoreModeField::update(bit_field_, mode);
2193 void set_type(Type* type) { type_ = type; }
2195 static int num_ids() { return parent_num_ids() + 4; }
2196 BailoutId AssignmentId() const { return BailoutId(local_id(0)); }
2197 BailoutId ToNumberId() const { return BailoutId(local_id(1)); }
2198 TypeFeedbackId CountBinOpFeedbackId() const {
2199 return TypeFeedbackId(local_id(2));
2201 TypeFeedbackId CountStoreFeedbackId() const {
2202 return TypeFeedbackId(local_id(3));
2205 FeedbackVectorRequirements ComputeFeedbackRequirements(
2206 Isolate* isolate, const ICSlotCache* cache) override;
2207 void SetFirstFeedbackICSlot(FeedbackVectorICSlot slot,
2208 ICSlotCache* cache) override {
2211 Code::Kind FeedbackICSlotKind(int index) override;
2212 FeedbackVectorICSlot CountSlot() const { return slot_; }
2215 CountOperation(Zone* zone, Token::Value op, bool is_prefix, Expression* expr,
2217 : Expression(zone, pos),
2219 IsPrefixField::encode(is_prefix) | KeyTypeField::encode(ELEMENT) |
2220 StoreModeField::encode(STANDARD_STORE) | TokenField::encode(op)),
2223 slot_(FeedbackVectorICSlot::Invalid()) {}
2224 static int parent_num_ids() { return Expression::num_ids(); }
2227 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
2229 class IsPrefixField : public BitField16<bool, 0, 1> {};
2230 class KeyTypeField : public BitField16<IcCheckType, 1, 1> {};
2231 class StoreModeField : public BitField16<KeyedAccessStoreMode, 2, 3> {};
2232 class TokenField : public BitField16<Token::Value, 5, 8> {};
2234 // Starts with 16-bit field, which should get packed together with
2235 // Expression's trailing 16-bit field.
2236 uint16_t bit_field_;
2238 Expression* expression_;
2239 SmallMapList receiver_types_;
2240 FeedbackVectorICSlot slot_;
2244 class CompareOperation final : public Expression {
2246 DECLARE_NODE_TYPE(CompareOperation)
2248 Token::Value op() const { return op_; }
2249 Expression* left() const { return left_; }
2250 Expression* right() const { return right_; }
2252 // Type feedback information.
2253 static int num_ids() { return parent_num_ids() + 1; }
2254 TypeFeedbackId CompareOperationFeedbackId() const {
2255 return TypeFeedbackId(local_id(0));
2257 Type* combined_type() const { return combined_type_; }
2258 void set_combined_type(Type* type) { combined_type_ = type; }
2260 // Match special cases.
2261 bool IsLiteralCompareTypeof(Expression** expr, Handle<String>* check);
2262 bool IsLiteralCompareUndefined(Expression** expr, Isolate* isolate);
2263 bool IsLiteralCompareNull(Expression** expr);
2266 CompareOperation(Zone* zone, Token::Value op, Expression* left,
2267 Expression* right, int pos)
2268 : Expression(zone, pos),
2272 combined_type_(Type::None(zone)) {
2273 DCHECK(Token::IsCompareOp(op));
2275 static int parent_num_ids() { return Expression::num_ids(); }
2278 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
2284 Type* combined_type_;
2288 class Spread final : public Expression {
2290 DECLARE_NODE_TYPE(Spread)
2292 Expression* expression() const { return expression_; }
2294 static int num_ids() { return parent_num_ids(); }
2297 Spread(Zone* zone, Expression* expression, int pos)
2298 : Expression(zone, pos), expression_(expression) {}
2299 static int parent_num_ids() { return Expression::num_ids(); }
2302 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
2304 Expression* expression_;
2308 class Conditional final : public Expression {
2310 DECLARE_NODE_TYPE(Conditional)
2312 Expression* condition() const { return condition_; }
2313 Expression* then_expression() const { return then_expression_; }
2314 Expression* else_expression() const { return else_expression_; }
2316 static int num_ids() { return parent_num_ids() + 2; }
2317 BailoutId ThenId() const { return BailoutId(local_id(0)); }
2318 BailoutId ElseId() const { return BailoutId(local_id(1)); }
2321 Conditional(Zone* zone, Expression* condition, Expression* then_expression,
2322 Expression* else_expression, int position)
2323 : Expression(zone, position),
2324 condition_(condition),
2325 then_expression_(then_expression),
2326 else_expression_(else_expression) {}
2327 static int parent_num_ids() { return Expression::num_ids(); }
2330 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
2332 Expression* condition_;
2333 Expression* then_expression_;
2334 Expression* else_expression_;
2338 class Assignment final : public Expression {
2340 DECLARE_NODE_TYPE(Assignment)
2342 Assignment* AsSimpleAssignment() { return !is_compound() ? this : NULL; }
2344 Token::Value binary_op() const;
2346 Token::Value op() const { return TokenField::decode(bit_field_); }
2347 Expression* target() const { return target_; }
2348 Expression* value() const { return value_; }
2349 BinaryOperation* binary_operation() const { return binary_operation_; }
2351 // This check relies on the definition order of token in token.h.
2352 bool is_compound() const { return op() > Token::ASSIGN; }
2354 static int num_ids() { return parent_num_ids() + 2; }
2355 BailoutId AssignmentId() const { return BailoutId(local_id(0)); }
2357 // Type feedback information.
2358 TypeFeedbackId AssignmentFeedbackId() { return TypeFeedbackId(local_id(1)); }
2359 bool IsMonomorphic() override { return receiver_types_.length() == 1; }
2360 bool IsUninitialized() const {
2361 return IsUninitializedField::decode(bit_field_);
2363 bool HasNoTypeInformation() {
2364 return IsUninitializedField::decode(bit_field_);
2366 SmallMapList* GetReceiverTypes() override { return &receiver_types_; }
2367 IcCheckType GetKeyType() const override {
2368 return KeyTypeField::decode(bit_field_);
2370 KeyedAccessStoreMode GetStoreMode() const override {
2371 return StoreModeField::decode(bit_field_);
2373 void set_is_uninitialized(bool b) {
2374 bit_field_ = IsUninitializedField::update(bit_field_, b);
2376 void set_key_type(IcCheckType key_type) {
2377 bit_field_ = KeyTypeField::update(bit_field_, key_type);
2379 void set_store_mode(KeyedAccessStoreMode mode) {
2380 bit_field_ = StoreModeField::update(bit_field_, mode);
2383 FeedbackVectorRequirements ComputeFeedbackRequirements(
2384 Isolate* isolate, const ICSlotCache* cache) override;
2385 void SetFirstFeedbackICSlot(FeedbackVectorICSlot slot,
2386 ICSlotCache* cache) override {
2389 Code::Kind FeedbackICSlotKind(int index) override;
2390 FeedbackVectorICSlot AssignmentSlot() const { return slot_; }
2393 Assignment(Zone* zone, Token::Value op, Expression* target, Expression* value,
2395 static int parent_num_ids() { return Expression::num_ids(); }
2398 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
2400 class IsUninitializedField : public BitField16<bool, 0, 1> {};
2401 class KeyTypeField : public BitField16<IcCheckType, 1, 1> {};
2402 class StoreModeField : public BitField16<KeyedAccessStoreMode, 2, 3> {};
2403 class TokenField : public BitField16<Token::Value, 5, 8> {};
2405 // Starts with 16-bit field, which should get packed together with
2406 // Expression's trailing 16-bit field.
2407 uint16_t bit_field_;
2408 Expression* target_;
2410 BinaryOperation* binary_operation_;
2411 SmallMapList receiver_types_;
2412 FeedbackVectorICSlot slot_;
2416 class Yield final : public Expression {
2418 DECLARE_NODE_TYPE(Yield)
2421 kInitial, // The initial yield that returns the unboxed generator object.
2422 kSuspend, // A normal yield: { value: EXPRESSION, done: false }
2423 kDelegating, // A yield*.
2424 kFinal // A return: { value: EXPRESSION, done: true }
2427 Expression* generator_object() const { return generator_object_; }
2428 Expression* expression() const { return expression_; }
2429 Kind yield_kind() const { return yield_kind_; }
2431 // Type feedback information.
2432 bool HasFeedbackSlots() const { return yield_kind() == kDelegating; }
2433 virtual FeedbackVectorRequirements ComputeFeedbackRequirements(
2434 Isolate* isolate, const ICSlotCache* cache) override {
2435 return FeedbackVectorRequirements(0, HasFeedbackSlots() ? 3 : 0);
2437 void SetFirstFeedbackICSlot(FeedbackVectorICSlot slot,
2438 ICSlotCache* cache) override {
2439 yield_first_feedback_slot_ = slot;
2441 Code::Kind FeedbackICSlotKind(int index) override {
2442 return index == 0 ? Code::KEYED_LOAD_IC : Code::LOAD_IC;
2445 FeedbackVectorICSlot KeyedLoadFeedbackSlot() {
2446 DCHECK(!HasFeedbackSlots() || !yield_first_feedback_slot_.IsInvalid());
2447 return yield_first_feedback_slot_;
2450 FeedbackVectorICSlot DoneFeedbackSlot() {
2451 return KeyedLoadFeedbackSlot().next();
2454 FeedbackVectorICSlot ValueFeedbackSlot() { return DoneFeedbackSlot().next(); }
2457 Yield(Zone* zone, Expression* generator_object, Expression* expression,
2458 Kind yield_kind, int pos)
2459 : Expression(zone, pos),
2460 generator_object_(generator_object),
2461 expression_(expression),
2462 yield_kind_(yield_kind),
2463 yield_first_feedback_slot_(FeedbackVectorICSlot::Invalid()) {}
2466 Expression* generator_object_;
2467 Expression* expression_;
2469 FeedbackVectorICSlot yield_first_feedback_slot_;
2473 class Throw final : public Expression {
2475 DECLARE_NODE_TYPE(Throw)
2477 Expression* exception() const { return exception_; }
2480 Throw(Zone* zone, Expression* exception, int pos)
2481 : Expression(zone, pos), exception_(exception) {}
2484 Expression* exception_;
2488 class FunctionLiteral final : public Expression {
2491 ANONYMOUS_EXPRESSION,
2496 enum ParameterFlag {
2497 kNoDuplicateParameters = 0,
2498 kHasDuplicateParameters = 1
2501 enum IsFunctionFlag {
2506 enum EagerCompileHint { kShouldEagerCompile, kShouldLazyCompile };
2508 enum ShouldBeUsedOnceHint { kShouldBeUsedOnce, kDontKnowIfShouldBeUsedOnce };
2510 enum ArityRestriction {
2516 DECLARE_NODE_TYPE(FunctionLiteral)
2518 Handle<String> name() const { return raw_name_->string(); }
2519 const AstRawString* raw_name() const { return raw_name_; }
2520 Scope* scope() const { return scope_; }
2521 ZoneList<Statement*>* body() const { return body_; }
2522 void set_function_token_position(int pos) { function_token_position_ = pos; }
2523 int function_token_position() const { return function_token_position_; }
2524 int start_position() const;
2525 int end_position() const;
2526 int SourceSize() const { return end_position() - start_position(); }
2527 bool is_expression() const { return IsExpression::decode(bitfield_); }
2528 bool is_anonymous() const { return IsAnonymous::decode(bitfield_); }
2529 LanguageMode language_mode() const;
2531 static bool NeedsHomeObject(Expression* expr);
2533 int materialized_literal_count() { return materialized_literal_count_; }
2534 int expected_property_count() { return expected_property_count_; }
2535 int parameter_count() { return parameter_count_; }
2537 bool AllowsLazyCompilation();
2538 bool AllowsLazyCompilationWithoutContext();
2540 Handle<String> debug_name() const {
2541 if (raw_name_ != NULL && !raw_name_->IsEmpty()) {
2542 return raw_name_->string();
2544 return inferred_name();
2547 Handle<String> inferred_name() const {
2548 if (!inferred_name_.is_null()) {
2549 DCHECK(raw_inferred_name_ == NULL);
2550 return inferred_name_;
2552 if (raw_inferred_name_ != NULL) {
2553 return raw_inferred_name_->string();
2556 return Handle<String>();
2559 // Only one of {set_inferred_name, set_raw_inferred_name} should be called.
2560 void set_inferred_name(Handle<String> inferred_name) {
2561 DCHECK(!inferred_name.is_null());
2562 inferred_name_ = inferred_name;
2563 DCHECK(raw_inferred_name_== NULL || raw_inferred_name_->IsEmpty());
2564 raw_inferred_name_ = NULL;
2567 void set_raw_inferred_name(const AstString* raw_inferred_name) {
2568 DCHECK(raw_inferred_name != NULL);
2569 raw_inferred_name_ = raw_inferred_name;
2570 DCHECK(inferred_name_.is_null());
2571 inferred_name_ = Handle<String>();
2574 bool pretenure() { return Pretenure::decode(bitfield_); }
2575 void set_pretenure() { bitfield_ |= Pretenure::encode(true); }
2577 bool has_duplicate_parameters() {
2578 return HasDuplicateParameters::decode(bitfield_);
2581 bool is_function() { return IsFunction::decode(bitfield_) == kIsFunction; }
2583 // This is used as a heuristic on when to eagerly compile a function
2584 // literal. We consider the following constructs as hints that the
2585 // function will be called immediately:
2586 // - (function() { ... })();
2587 // - var x = function() { ... }();
2588 bool should_eager_compile() const {
2589 return EagerCompileHintBit::decode(bitfield_) == kShouldEagerCompile;
2591 void set_should_eager_compile() {
2592 bitfield_ = EagerCompileHintBit::update(bitfield_, kShouldEagerCompile);
2595 // A hint that we expect this function to be called (exactly) once,
2596 // i.e. we suspect it's an initialization function.
2597 bool should_be_used_once_hint() const {
2598 return ShouldBeUsedOnceHintBit::decode(bitfield_) == kShouldBeUsedOnce;
2600 void set_should_be_used_once_hint() {
2601 bitfield_ = ShouldBeUsedOnceHintBit::update(bitfield_, kShouldBeUsedOnce);
2604 FunctionKind kind() const { return FunctionKindBits::decode(bitfield_); }
2606 int ast_node_count() { return ast_properties_.node_count(); }
2607 AstProperties::Flags flags() const { return ast_properties_.flags(); }
2608 void set_ast_properties(AstProperties* ast_properties) {
2609 ast_properties_ = *ast_properties;
2611 const ZoneFeedbackVectorSpec* feedback_vector_spec() const {
2612 return ast_properties_.get_spec();
2614 bool dont_optimize() { return dont_optimize_reason_ != kNoReason; }
2615 BailoutReason dont_optimize_reason() { return dont_optimize_reason_; }
2616 void set_dont_optimize_reason(BailoutReason reason) {
2617 dont_optimize_reason_ = reason;
2621 FunctionLiteral(Zone* zone, const AstRawString* name,
2622 AstValueFactory* ast_value_factory, Scope* scope,
2623 ZoneList<Statement*>* body, int materialized_literal_count,
2624 int expected_property_count, int parameter_count,
2625 FunctionType function_type,
2626 ParameterFlag has_duplicate_parameters,
2627 IsFunctionFlag is_function,
2628 EagerCompileHint eager_compile_hint, FunctionKind kind,
2630 : Expression(zone, position),
2634 raw_inferred_name_(ast_value_factory->empty_string()),
2635 ast_properties_(zone),
2636 dont_optimize_reason_(kNoReason),
2637 materialized_literal_count_(materialized_literal_count),
2638 expected_property_count_(expected_property_count),
2639 parameter_count_(parameter_count),
2640 function_token_position_(RelocInfo::kNoPosition) {
2641 bitfield_ = IsExpression::encode(function_type != DECLARATION) |
2642 IsAnonymous::encode(function_type == ANONYMOUS_EXPRESSION) |
2643 Pretenure::encode(false) |
2644 HasDuplicateParameters::encode(has_duplicate_parameters) |
2645 IsFunction::encode(is_function) |
2646 EagerCompileHintBit::encode(eager_compile_hint) |
2647 FunctionKindBits::encode(kind) |
2648 ShouldBeUsedOnceHintBit::encode(kDontKnowIfShouldBeUsedOnce);
2649 DCHECK(IsValidFunctionKind(kind));
2653 const AstRawString* raw_name_;
2654 Handle<String> name_;
2656 ZoneList<Statement*>* body_;
2657 const AstString* raw_inferred_name_;
2658 Handle<String> inferred_name_;
2659 AstProperties ast_properties_;
2660 BailoutReason dont_optimize_reason_;
2662 int materialized_literal_count_;
2663 int expected_property_count_;
2664 int parameter_count_;
2665 int function_token_position_;
2668 class IsExpression : public BitField<bool, 0, 1> {};
2669 class IsAnonymous : public BitField<bool, 1, 1> {};
2670 class Pretenure : public BitField<bool, 2, 1> {};
2671 class HasDuplicateParameters : public BitField<ParameterFlag, 3, 1> {};
2672 class IsFunction : public BitField<IsFunctionFlag, 4, 1> {};
2673 class EagerCompileHintBit : public BitField<EagerCompileHint, 5, 1> {};
2674 class FunctionKindBits : public BitField<FunctionKind, 6, 8> {};
2675 class ShouldBeUsedOnceHintBit : public BitField<ShouldBeUsedOnceHint, 15, 1> {
2680 class ClassLiteral final : public Expression {
2682 typedef ObjectLiteralProperty Property;
2684 DECLARE_NODE_TYPE(ClassLiteral)
2686 Handle<String> name() const { return raw_name_->string(); }
2687 const AstRawString* raw_name() const { return raw_name_; }
2688 Scope* scope() const { return scope_; }
2689 VariableProxy* class_variable_proxy() const { return class_variable_proxy_; }
2690 Expression* extends() const { return extends_; }
2691 FunctionLiteral* constructor() const { return constructor_; }
2692 ZoneList<Property*>* properties() const { return properties_; }
2693 int start_position() const { return position(); }
2694 int end_position() const { return end_position_; }
2696 BailoutId EntryId() const { return BailoutId(local_id(0)); }
2697 BailoutId DeclsId() const { return BailoutId(local_id(1)); }
2698 BailoutId ExitId() { return BailoutId(local_id(2)); }
2699 BailoutId CreateLiteralId() const { return BailoutId(local_id(3)); }
2701 // Return an AST id for a property that is used in simulate instructions.
2702 BailoutId GetIdForProperty(int i) { return BailoutId(local_id(i + 4)); }
2704 // Unlike other AST nodes, this number of bailout IDs allocated for an
2705 // ClassLiteral can vary, so num_ids() is not a static method.
2706 int num_ids() const { return parent_num_ids() + 4 + properties()->length(); }
2708 // Object literals need one feedback slot for each non-trivial value, as well
2709 // as some slots for home objects.
2710 FeedbackVectorRequirements ComputeFeedbackRequirements(
2711 Isolate* isolate, const ICSlotCache* cache) override;
2712 void SetFirstFeedbackICSlot(FeedbackVectorICSlot slot,
2713 ICSlotCache* cache) override {
2716 Code::Kind FeedbackICSlotKind(int index) override { return Code::STORE_IC; }
2717 FeedbackVectorICSlot GetNthSlot(int n) const {
2718 return FeedbackVectorICSlot(slot_.ToInt() + n);
2721 // If value needs a home object, returns a valid feedback vector ic slot
2722 // given by slot_index, and increments slot_index.
2723 FeedbackVectorICSlot SlotForHomeObject(Expression* value,
2724 int* slot_index) const;
2727 int slot_count() const { return slot_count_; }
2731 ClassLiteral(Zone* zone, const AstRawString* name, Scope* scope,
2732 VariableProxy* class_variable_proxy, Expression* extends,
2733 FunctionLiteral* constructor, ZoneList<Property*>* properties,
2734 int start_position, int end_position)
2735 : Expression(zone, start_position),
2738 class_variable_proxy_(class_variable_proxy),
2740 constructor_(constructor),
2741 properties_(properties),
2742 end_position_(end_position),
2746 slot_(FeedbackVectorICSlot::Invalid()) {
2749 static int parent_num_ids() { return Expression::num_ids(); }
2752 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
2754 const AstRawString* raw_name_;
2756 VariableProxy* class_variable_proxy_;
2757 Expression* extends_;
2758 FunctionLiteral* constructor_;
2759 ZoneList<Property*>* properties_;
2762 // slot_count_ helps validate that the logic to allocate ic slots and the
2763 // logic to use them are in sync.
2766 FeedbackVectorICSlot slot_;
2770 class NativeFunctionLiteral final : public Expression {
2772 DECLARE_NODE_TYPE(NativeFunctionLiteral)
2774 Handle<String> name() const { return name_->string(); }
2775 v8::Extension* extension() const { return extension_; }
2778 NativeFunctionLiteral(Zone* zone, const AstRawString* name,
2779 v8::Extension* extension, int pos)
2780 : Expression(zone, pos), name_(name), extension_(extension) {}
2783 const AstRawString* name_;
2784 v8::Extension* extension_;
2788 class ThisFunction final : public Expression {
2790 DECLARE_NODE_TYPE(ThisFunction)
2793 ThisFunction(Zone* zone, int pos) : Expression(zone, pos) {}
2797 class SuperPropertyReference final : public Expression {
2799 DECLARE_NODE_TYPE(SuperPropertyReference)
2801 VariableProxy* this_var() const { return this_var_; }
2802 Expression* home_object() const { return home_object_; }
2805 SuperPropertyReference(Zone* zone, VariableProxy* this_var,
2806 Expression* home_object, int pos)
2807 : Expression(zone, pos), this_var_(this_var), home_object_(home_object) {
2808 DCHECK(this_var->is_this());
2809 DCHECK(home_object->IsProperty());
2813 VariableProxy* this_var_;
2814 Expression* home_object_;
2818 class SuperCallReference final : public Expression {
2820 DECLARE_NODE_TYPE(SuperCallReference)
2822 VariableProxy* this_var() const { return this_var_; }
2823 VariableProxy* new_target_var() const { return new_target_var_; }
2824 VariableProxy* this_function_var() const { return this_function_var_; }
2827 SuperCallReference(Zone* zone, VariableProxy* this_var,
2828 VariableProxy* new_target_var,
2829 VariableProxy* this_function_var, int pos)
2830 : Expression(zone, pos),
2831 this_var_(this_var),
2832 new_target_var_(new_target_var),
2833 this_function_var_(this_function_var) {
2834 DCHECK(this_var->is_this());
2835 DCHECK(new_target_var->raw_name()->IsOneByteEqualTo(".new.target"));
2836 DCHECK(this_function_var->raw_name()->IsOneByteEqualTo(".this_function"));
2840 VariableProxy* this_var_;
2841 VariableProxy* new_target_var_;
2842 VariableProxy* this_function_var_;
2846 // This class is produced when parsing the () in arrow functions without any
2847 // arguments and is not actually a valid expression.
2848 class EmptyParentheses final : public Expression {
2850 DECLARE_NODE_TYPE(EmptyParentheses)
2853 EmptyParentheses(Zone* zone, int pos) : Expression(zone, pos) {}
2857 #undef DECLARE_NODE_TYPE
2860 // ----------------------------------------------------------------------------
2861 // Regular expressions
2864 class RegExpVisitor BASE_EMBEDDED {
2866 virtual ~RegExpVisitor() { }
2867 #define MAKE_CASE(Name) \
2868 virtual void* Visit##Name(RegExp##Name*, void* data) = 0;
2869 FOR_EACH_REG_EXP_TREE_TYPE(MAKE_CASE)
2874 class RegExpTree : public ZoneObject {
2876 static const int kInfinity = kMaxInt;
2877 virtual ~RegExpTree() {}
2878 virtual void* Accept(RegExpVisitor* visitor, void* data) = 0;
2879 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
2880 RegExpNode* on_success) = 0;
2881 virtual bool IsTextElement() { return false; }
2882 virtual bool IsAnchoredAtStart() { return false; }
2883 virtual bool IsAnchoredAtEnd() { return false; }
2884 virtual int min_match() = 0;
2885 virtual int max_match() = 0;
2886 // Returns the interval of registers used for captures within this
2888 virtual Interval CaptureRegisters() { return Interval::Empty(); }
2889 virtual void AppendToText(RegExpText* text, Zone* zone);
2890 std::ostream& Print(std::ostream& os, Zone* zone); // NOLINT
2891 #define MAKE_ASTYPE(Name) \
2892 virtual RegExp##Name* As##Name(); \
2893 virtual bool Is##Name();
2894 FOR_EACH_REG_EXP_TREE_TYPE(MAKE_ASTYPE)
2899 class RegExpDisjunction final : public RegExpTree {
2901 explicit RegExpDisjunction(ZoneList<RegExpTree*>* alternatives);
2902 void* Accept(RegExpVisitor* visitor, void* data) override;
2903 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
2904 RegExpNode* on_success) override;
2905 RegExpDisjunction* AsDisjunction() override;
2906 Interval CaptureRegisters() override;
2907 bool IsDisjunction() override;
2908 bool IsAnchoredAtStart() override;
2909 bool IsAnchoredAtEnd() override;
2910 int min_match() override { return min_match_; }
2911 int max_match() override { return max_match_; }
2912 ZoneList<RegExpTree*>* alternatives() { return alternatives_; }
2914 bool SortConsecutiveAtoms(RegExpCompiler* compiler);
2915 void RationalizeConsecutiveAtoms(RegExpCompiler* compiler);
2916 void FixSingleCharacterDisjunctions(RegExpCompiler* compiler);
2917 ZoneList<RegExpTree*>* alternatives_;
2923 class RegExpAlternative final : public RegExpTree {
2925 explicit RegExpAlternative(ZoneList<RegExpTree*>* nodes);
2926 void* Accept(RegExpVisitor* visitor, void* data) override;
2927 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
2928 RegExpNode* on_success) override;
2929 RegExpAlternative* AsAlternative() override;
2930 Interval CaptureRegisters() override;
2931 bool IsAlternative() override;
2932 bool IsAnchoredAtStart() override;
2933 bool IsAnchoredAtEnd() override;
2934 int min_match() override { return min_match_; }
2935 int max_match() override { return max_match_; }
2936 ZoneList<RegExpTree*>* nodes() { return nodes_; }
2938 ZoneList<RegExpTree*>* nodes_;
2944 class RegExpAssertion final : public RegExpTree {
2946 enum AssertionType {
2954 explicit RegExpAssertion(AssertionType type) : assertion_type_(type) { }
2955 void* Accept(RegExpVisitor* visitor, void* data) override;
2956 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
2957 RegExpNode* on_success) override;
2958 RegExpAssertion* AsAssertion() override;
2959 bool IsAssertion() override;
2960 bool IsAnchoredAtStart() override;
2961 bool IsAnchoredAtEnd() override;
2962 int min_match() override { return 0; }
2963 int max_match() override { return 0; }
2964 AssertionType assertion_type() { return assertion_type_; }
2966 AssertionType assertion_type_;
2970 class CharacterSet final BASE_EMBEDDED {
2972 explicit CharacterSet(uc16 standard_set_type)
2974 standard_set_type_(standard_set_type) {}
2975 explicit CharacterSet(ZoneList<CharacterRange>* ranges)
2977 standard_set_type_(0) {}
2978 ZoneList<CharacterRange>* ranges(Zone* zone);
2979 uc16 standard_set_type() { return standard_set_type_; }
2980 void set_standard_set_type(uc16 special_set_type) {
2981 standard_set_type_ = special_set_type;
2983 bool is_standard() { return standard_set_type_ != 0; }
2984 void Canonicalize();
2986 ZoneList<CharacterRange>* ranges_;
2987 // If non-zero, the value represents a standard set (e.g., all whitespace
2988 // characters) without having to expand the ranges.
2989 uc16 standard_set_type_;
2993 class RegExpCharacterClass final : public RegExpTree {
2995 RegExpCharacterClass(ZoneList<CharacterRange>* ranges, bool is_negated)
2997 is_negated_(is_negated) { }
2998 explicit RegExpCharacterClass(uc16 type)
3000 is_negated_(false) { }
3001 void* Accept(RegExpVisitor* visitor, void* data) override;
3002 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
3003 RegExpNode* on_success) override;
3004 RegExpCharacterClass* AsCharacterClass() override;
3005 bool IsCharacterClass() override;
3006 bool IsTextElement() override { return true; }
3007 int min_match() override { return 1; }
3008 int max_match() override { return 1; }
3009 void AppendToText(RegExpText* text, Zone* zone) override;
3010 CharacterSet character_set() { return set_; }
3011 // TODO(lrn): Remove need for complex version if is_standard that
3012 // recognizes a mangled standard set and just do { return set_.is_special(); }
3013 bool is_standard(Zone* zone);
3014 // Returns a value representing the standard character set if is_standard()
3016 // Currently used values are:
3017 // s : unicode whitespace
3018 // S : unicode non-whitespace
3019 // w : ASCII word character (digit, letter, underscore)
3020 // W : non-ASCII word character
3022 // D : non-ASCII digit
3023 // . : non-unicode non-newline
3024 // * : All characters
3025 uc16 standard_type() { return set_.standard_set_type(); }
3026 ZoneList<CharacterRange>* ranges(Zone* zone) { return set_.ranges(zone); }
3027 bool is_negated() { return is_negated_; }
3035 class RegExpAtom final : public RegExpTree {
3037 explicit RegExpAtom(Vector<const uc16> data) : data_(data) { }
3038 void* Accept(RegExpVisitor* visitor, void* data) override;
3039 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
3040 RegExpNode* on_success) override;
3041 RegExpAtom* AsAtom() override;
3042 bool IsAtom() override;
3043 bool IsTextElement() override { return true; }
3044 int min_match() override { return data_.length(); }
3045 int max_match() override { return data_.length(); }
3046 void AppendToText(RegExpText* text, Zone* zone) override;
3047 Vector<const uc16> data() { return data_; }
3048 int length() { return data_.length(); }
3050 Vector<const uc16> data_;
3054 class RegExpText final : public RegExpTree {
3056 explicit RegExpText(Zone* zone) : elements_(2, zone), length_(0) {}
3057 void* Accept(RegExpVisitor* visitor, void* data) override;
3058 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
3059 RegExpNode* on_success) override;
3060 RegExpText* AsText() override;
3061 bool IsText() override;
3062 bool IsTextElement() override { return true; }
3063 int min_match() override { return length_; }
3064 int max_match() override { return length_; }
3065 void AppendToText(RegExpText* text, Zone* zone) override;
3066 void AddElement(TextElement elm, Zone* zone) {
3067 elements_.Add(elm, zone);
3068 length_ += elm.length();
3070 ZoneList<TextElement>* elements() { return &elements_; }
3072 ZoneList<TextElement> elements_;
3077 class RegExpQuantifier final : public RegExpTree {
3079 enum QuantifierType { GREEDY, NON_GREEDY, POSSESSIVE };
3080 RegExpQuantifier(int min, int max, QuantifierType type, RegExpTree* body)
3084 min_match_(min * body->min_match()),
3085 quantifier_type_(type) {
3086 if (max > 0 && body->max_match() > kInfinity / max) {
3087 max_match_ = kInfinity;
3089 max_match_ = max * body->max_match();
3092 void* Accept(RegExpVisitor* visitor, void* data) override;
3093 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
3094 RegExpNode* on_success) override;
3095 static RegExpNode* ToNode(int min,
3099 RegExpCompiler* compiler,
3100 RegExpNode* on_success,
3101 bool not_at_start = false);
3102 RegExpQuantifier* AsQuantifier() override;
3103 Interval CaptureRegisters() override;
3104 bool IsQuantifier() override;
3105 int min_match() override { return min_match_; }
3106 int max_match() override { return max_match_; }
3107 int min() { return min_; }
3108 int max() { return max_; }
3109 bool is_possessive() { return quantifier_type_ == POSSESSIVE; }
3110 bool is_non_greedy() { return quantifier_type_ == NON_GREEDY; }
3111 bool is_greedy() { return quantifier_type_ == GREEDY; }
3112 RegExpTree* body() { return body_; }
3120 QuantifierType quantifier_type_;
3124 class RegExpCapture final : public RegExpTree {
3126 explicit RegExpCapture(RegExpTree* body, int index)
3127 : body_(body), index_(index) { }
3128 void* Accept(RegExpVisitor* visitor, void* data) override;
3129 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
3130 RegExpNode* on_success) override;
3131 static RegExpNode* ToNode(RegExpTree* body,
3133 RegExpCompiler* compiler,
3134 RegExpNode* on_success);
3135 RegExpCapture* AsCapture() override;
3136 bool IsAnchoredAtStart() override;
3137 bool IsAnchoredAtEnd() override;
3138 Interval CaptureRegisters() override;
3139 bool IsCapture() override;
3140 int min_match() override { return body_->min_match(); }
3141 int max_match() override { return body_->max_match(); }
3142 RegExpTree* body() { return body_; }
3143 int index() { return index_; }
3144 static int StartRegister(int index) { return index * 2; }
3145 static int EndRegister(int index) { return index * 2 + 1; }
3153 class RegExpLookahead final : public RegExpTree {
3155 RegExpLookahead(RegExpTree* body,
3160 is_positive_(is_positive),
3161 capture_count_(capture_count),
3162 capture_from_(capture_from) { }
3164 void* Accept(RegExpVisitor* visitor, void* data) override;
3165 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
3166 RegExpNode* on_success) override;
3167 RegExpLookahead* AsLookahead() override;
3168 Interval CaptureRegisters() override;
3169 bool IsLookahead() override;
3170 bool IsAnchoredAtStart() override;
3171 int min_match() override { return 0; }
3172 int max_match() override { return 0; }
3173 RegExpTree* body() { return body_; }
3174 bool is_positive() { return is_positive_; }
3175 int capture_count() { return capture_count_; }
3176 int capture_from() { return capture_from_; }
3186 class RegExpBackReference final : public RegExpTree {
3188 explicit RegExpBackReference(RegExpCapture* capture)
3189 : capture_(capture) { }
3190 void* Accept(RegExpVisitor* visitor, void* data) override;
3191 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
3192 RegExpNode* on_success) override;
3193 RegExpBackReference* AsBackReference() override;
3194 bool IsBackReference() override;
3195 int min_match() override { return 0; }
3196 int max_match() override { return capture_->max_match(); }
3197 int index() { return capture_->index(); }
3198 RegExpCapture* capture() { return capture_; }
3200 RegExpCapture* capture_;
3204 class RegExpEmpty final : public RegExpTree {
3207 void* Accept(RegExpVisitor* visitor, void* data) override;
3208 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
3209 RegExpNode* on_success) override;
3210 RegExpEmpty* AsEmpty() override;
3211 bool IsEmpty() override;
3212 int min_match() override { return 0; }
3213 int max_match() override { return 0; }
3217 // ----------------------------------------------------------------------------
3219 // - leaf node visitors are abstract.
3221 class AstVisitor BASE_EMBEDDED {
3224 virtual ~AstVisitor() {}
3226 // Stack overflow check and dynamic dispatch.
3227 virtual void Visit(AstNode* node) = 0;
3229 // Iteration left-to-right.
3230 virtual void VisitDeclarations(ZoneList<Declaration*>* declarations);
3231 virtual void VisitStatements(ZoneList<Statement*>* statements);
3232 virtual void VisitExpressions(ZoneList<Expression*>* expressions);
3234 // Individual AST nodes.
3235 #define DEF_VISIT(type) \
3236 virtual void Visit##type(type* node) = 0;
3237 AST_NODE_LIST(DEF_VISIT)
3242 #define DEFINE_AST_VISITOR_SUBCLASS_MEMBERS() \
3244 void Visit(AstNode* node) final { \
3245 if (!CheckStackOverflow()) node->Accept(this); \
3248 void SetStackOverflow() { stack_overflow_ = true; } \
3249 void ClearStackOverflow() { stack_overflow_ = false; } \
3250 bool HasStackOverflow() const { return stack_overflow_; } \
3252 bool CheckStackOverflow() { \
3253 if (stack_overflow_) return true; \
3254 StackLimitCheck check(isolate_); \
3255 if (!check.HasOverflowed()) return false; \
3256 stack_overflow_ = true; \
3261 void InitializeAstVisitor(Isolate* isolate, Zone* zone) { \
3262 isolate_ = isolate; \
3264 stack_overflow_ = false; \
3266 Zone* zone() { return zone_; } \
3267 Isolate* isolate() { return isolate_; } \
3269 Isolate* isolate_; \
3271 bool stack_overflow_
3274 // ----------------------------------------------------------------------------
3277 class AstNodeFactory final BASE_EMBEDDED {
3279 explicit AstNodeFactory(AstValueFactory* ast_value_factory)
3280 : local_zone_(ast_value_factory->zone()),
3281 parser_zone_(ast_value_factory->zone()),
3282 ast_value_factory_(ast_value_factory) {}
3284 VariableDeclaration* NewVariableDeclaration(
3285 VariableProxy* proxy, VariableMode mode, Scope* scope, int pos,
3286 bool is_class_declaration = false, int declaration_group_start = -1) {
3287 return new (parser_zone_)
3288 VariableDeclaration(parser_zone_, proxy, mode, scope, pos,
3289 is_class_declaration, declaration_group_start);
3292 FunctionDeclaration* NewFunctionDeclaration(VariableProxy* proxy,
3294 FunctionLiteral* fun,
3297 return new (parser_zone_)
3298 FunctionDeclaration(parser_zone_, proxy, mode, fun, scope, pos);
3301 ImportDeclaration* NewImportDeclaration(VariableProxy* proxy,
3302 const AstRawString* import_name,
3303 const AstRawString* module_specifier,
3304 Scope* scope, int pos) {
3305 return new (parser_zone_) ImportDeclaration(
3306 parser_zone_, proxy, import_name, module_specifier, scope, pos);
3309 ExportDeclaration* NewExportDeclaration(VariableProxy* proxy,
3312 return new (parser_zone_)
3313 ExportDeclaration(parser_zone_, proxy, scope, pos);
3316 Block* NewBlock(ZoneList<const AstRawString*>* labels, int capacity,
3317 bool ignore_completion_value, int pos) {
3318 return new (local_zone_)
3319 Block(local_zone_, labels, capacity, ignore_completion_value, pos);
3322 #define STATEMENT_WITH_LABELS(NodeType) \
3323 NodeType* New##NodeType(ZoneList<const AstRawString*>* labels, int pos) { \
3324 return new (local_zone_) NodeType(local_zone_, labels, pos); \
3326 STATEMENT_WITH_LABELS(DoWhileStatement)
3327 STATEMENT_WITH_LABELS(WhileStatement)
3328 STATEMENT_WITH_LABELS(ForStatement)
3329 STATEMENT_WITH_LABELS(SwitchStatement)
3330 #undef STATEMENT_WITH_LABELS
3332 ForEachStatement* NewForEachStatement(ForEachStatement::VisitMode visit_mode,
3333 ZoneList<const AstRawString*>* labels,
3335 switch (visit_mode) {
3336 case ForEachStatement::ENUMERATE: {
3337 return new (local_zone_) ForInStatement(local_zone_, labels, pos);
3339 case ForEachStatement::ITERATE: {
3340 return new (local_zone_) ForOfStatement(local_zone_, labels, pos);
3347 ExpressionStatement* NewExpressionStatement(Expression* expression, int pos) {
3348 return new (local_zone_) ExpressionStatement(local_zone_, expression, pos);
3351 ContinueStatement* NewContinueStatement(IterationStatement* target, int pos) {
3352 return new (local_zone_) ContinueStatement(local_zone_, target, pos);
3355 BreakStatement* NewBreakStatement(BreakableStatement* target, int pos) {
3356 return new (local_zone_) BreakStatement(local_zone_, target, pos);
3359 ReturnStatement* NewReturnStatement(Expression* expression, int pos) {
3360 return new (local_zone_) ReturnStatement(local_zone_, expression, pos);
3363 WithStatement* NewWithStatement(Scope* scope,
3364 Expression* expression,
3365 Statement* statement,
3367 return new (local_zone_)
3368 WithStatement(local_zone_, scope, expression, statement, pos);
3371 IfStatement* NewIfStatement(Expression* condition,
3372 Statement* then_statement,
3373 Statement* else_statement,
3375 return new (local_zone_) IfStatement(local_zone_, condition, then_statement,
3376 else_statement, pos);
3379 TryCatchStatement* NewTryCatchStatement(Block* try_block, Scope* scope,
3381 Block* catch_block, int pos) {
3382 return new (local_zone_) TryCatchStatement(local_zone_, try_block, scope,
3383 variable, catch_block, pos);
3386 TryFinallyStatement* NewTryFinallyStatement(Block* try_block,
3387 Block* finally_block, int pos) {
3388 return new (local_zone_)
3389 TryFinallyStatement(local_zone_, try_block, finally_block, pos);
3392 DebuggerStatement* NewDebuggerStatement(int pos) {
3393 return new (local_zone_) DebuggerStatement(local_zone_, pos);
3396 EmptyStatement* NewEmptyStatement(int pos) {
3397 return new (local_zone_) EmptyStatement(local_zone_, pos);
3400 CaseClause* NewCaseClause(
3401 Expression* label, ZoneList<Statement*>* statements, int pos) {
3402 return new (local_zone_) CaseClause(local_zone_, label, statements, pos);
3405 Literal* NewStringLiteral(const AstRawString* string, int pos) {
3406 return new (local_zone_)
3407 Literal(local_zone_, ast_value_factory_->NewString(string), pos);
3410 // A JavaScript symbol (ECMA-262 edition 6).
3411 Literal* NewSymbolLiteral(const char* name, int pos) {
3412 return new (local_zone_)
3413 Literal(local_zone_, ast_value_factory_->NewSymbol(name), pos);
3416 Literal* NewNumberLiteral(double number, int pos, bool with_dot = false) {
3417 return new (local_zone_) Literal(
3418 local_zone_, ast_value_factory_->NewNumber(number, with_dot), pos);
3421 Literal* NewSmiLiteral(int number, int pos) {
3422 return new (local_zone_)
3423 Literal(local_zone_, ast_value_factory_->NewSmi(number), pos);
3426 Literal* NewBooleanLiteral(bool b, int pos) {
3427 return new (local_zone_)
3428 Literal(local_zone_, ast_value_factory_->NewBoolean(b), pos);
3431 Literal* NewNullLiteral(int pos) {
3432 return new (local_zone_)
3433 Literal(local_zone_, ast_value_factory_->NewNull(), pos);
3436 Literal* NewUndefinedLiteral(int pos) {
3437 return new (local_zone_)
3438 Literal(local_zone_, ast_value_factory_->NewUndefined(), pos);
3441 Literal* NewTheHoleLiteral(int pos) {
3442 return new (local_zone_)
3443 Literal(local_zone_, ast_value_factory_->NewTheHole(), pos);
3446 ObjectLiteral* NewObjectLiteral(
3447 ZoneList<ObjectLiteral::Property*>* properties,
3449 int boilerplate_properties,
3453 return new (local_zone_)
3454 ObjectLiteral(local_zone_, properties, literal_index,
3455 boilerplate_properties, has_function, is_strong, pos);
3458 ObjectLiteral::Property* NewObjectLiteralProperty(
3459 Expression* key, Expression* value, ObjectLiteralProperty::Kind kind,
3460 bool is_static, bool is_computed_name) {
3461 return new (local_zone_)
3462 ObjectLiteral::Property(key, value, kind, is_static, is_computed_name);
3465 ObjectLiteral::Property* NewObjectLiteralProperty(Expression* key,
3468 bool is_computed_name) {
3469 return new (local_zone_) ObjectLiteral::Property(
3470 ast_value_factory_, key, value, is_static, is_computed_name);
3473 RegExpLiteral* NewRegExpLiteral(const AstRawString* pattern,
3474 const AstRawString* flags,
3478 return new (local_zone_) RegExpLiteral(local_zone_, pattern, flags,
3479 literal_index, is_strong, pos);
3482 ArrayLiteral* NewArrayLiteral(ZoneList<Expression*>* values,
3486 return new (local_zone_)
3487 ArrayLiteral(local_zone_, values, -1, literal_index, is_strong, pos);
3490 ArrayLiteral* NewArrayLiteral(ZoneList<Expression*>* values,
3491 int first_spread_index, int literal_index,
3492 bool is_strong, int pos) {
3493 return new (local_zone_) ArrayLiteral(
3494 local_zone_, values, first_spread_index, literal_index, is_strong, pos);
3497 VariableProxy* NewVariableProxy(Variable* var,
3498 int start_position = RelocInfo::kNoPosition,
3499 int end_position = RelocInfo::kNoPosition) {
3500 return new (parser_zone_)
3501 VariableProxy(parser_zone_, var, start_position, end_position);
3504 VariableProxy* NewVariableProxy(const AstRawString* name,
3505 Variable::Kind variable_kind,
3506 int start_position = RelocInfo::kNoPosition,
3507 int end_position = RelocInfo::kNoPosition) {
3508 DCHECK_NOT_NULL(name);
3509 return new (parser_zone_) VariableProxy(parser_zone_, name, variable_kind,
3510 start_position, end_position);
3513 Property* NewProperty(Expression* obj, Expression* key, int pos) {
3514 return new (local_zone_) Property(local_zone_, obj, key, pos);
3517 Call* NewCall(Expression* expression,
3518 ZoneList<Expression*>* arguments,
3520 return new (local_zone_) Call(local_zone_, expression, arguments, pos);
3523 CallNew* NewCallNew(Expression* expression,
3524 ZoneList<Expression*>* arguments,
3526 return new (local_zone_) CallNew(local_zone_, expression, arguments, pos);
3529 CallRuntime* NewCallRuntime(Runtime::FunctionId id,
3530 ZoneList<Expression*>* arguments, int pos) {
3531 return new (local_zone_)
3532 CallRuntime(local_zone_, Runtime::FunctionForId(id), arguments, pos);
3535 CallRuntime* NewCallRuntime(const Runtime::Function* function,
3536 ZoneList<Expression*>* arguments, int pos) {
3537 return new (local_zone_) CallRuntime(local_zone_, function, arguments, pos);
3540 CallRuntime* NewCallRuntime(int context_index,
3541 ZoneList<Expression*>* arguments, int pos) {
3542 return new (local_zone_)
3543 CallRuntime(local_zone_, context_index, arguments, pos);
3546 UnaryOperation* NewUnaryOperation(Token::Value op,
3547 Expression* expression,
3549 return new (local_zone_) UnaryOperation(local_zone_, op, expression, pos);
3552 BinaryOperation* NewBinaryOperation(Token::Value op,
3556 return new (local_zone_) BinaryOperation(local_zone_, op, left, right, pos);
3559 CountOperation* NewCountOperation(Token::Value op,
3563 return new (local_zone_)
3564 CountOperation(local_zone_, op, is_prefix, expr, pos);
3567 CompareOperation* NewCompareOperation(Token::Value op,
3571 return new (local_zone_)
3572 CompareOperation(local_zone_, op, left, right, pos);
3575 Spread* NewSpread(Expression* expression, int pos) {
3576 return new (local_zone_) Spread(local_zone_, expression, pos);
3579 Conditional* NewConditional(Expression* condition,
3580 Expression* then_expression,
3581 Expression* else_expression,
3583 return new (local_zone_) Conditional(
3584 local_zone_, condition, then_expression, else_expression, position);
3587 Assignment* NewAssignment(Token::Value op,
3591 DCHECK(Token::IsAssignmentOp(op));
3592 Assignment* assign =
3593 new (local_zone_) Assignment(local_zone_, op, target, value, pos);
3594 if (assign->is_compound()) {
3595 DCHECK(Token::IsAssignmentOp(op));
3596 assign->binary_operation_ =
3597 NewBinaryOperation(assign->binary_op(), target, value, pos + 1);
3602 Yield* NewYield(Expression *generator_object,
3603 Expression* expression,
3604 Yield::Kind yield_kind,
3606 if (!expression) expression = NewUndefinedLiteral(pos);
3607 return new (local_zone_)
3608 Yield(local_zone_, generator_object, expression, yield_kind, pos);
3611 Throw* NewThrow(Expression* exception, int pos) {
3612 return new (local_zone_) Throw(local_zone_, exception, pos);
3615 FunctionLiteral* NewFunctionLiteral(
3616 const AstRawString* name, AstValueFactory* ast_value_factory,
3617 Scope* scope, ZoneList<Statement*>* body, int materialized_literal_count,
3618 int expected_property_count, int parameter_count,
3619 FunctionLiteral::ParameterFlag has_duplicate_parameters,
3620 FunctionLiteral::FunctionType function_type,
3621 FunctionLiteral::IsFunctionFlag is_function,
3622 FunctionLiteral::EagerCompileHint eager_compile_hint, FunctionKind kind,
3624 return new (parser_zone_) FunctionLiteral(
3625 parser_zone_, name, ast_value_factory, scope, body,
3626 materialized_literal_count, expected_property_count, parameter_count,
3627 function_type, has_duplicate_parameters, is_function,
3628 eager_compile_hint, kind, position);
3631 ClassLiteral* NewClassLiteral(const AstRawString* name, Scope* scope,
3632 VariableProxy* proxy, Expression* extends,
3633 FunctionLiteral* constructor,
3634 ZoneList<ObjectLiteral::Property*>* properties,
3635 int start_position, int end_position) {
3636 return new (parser_zone_)
3637 ClassLiteral(parser_zone_, name, scope, proxy, extends, constructor,
3638 properties, start_position, end_position);
3641 NativeFunctionLiteral* NewNativeFunctionLiteral(const AstRawString* name,
3642 v8::Extension* extension,
3644 return new (parser_zone_)
3645 NativeFunctionLiteral(parser_zone_, name, extension, pos);
3648 ThisFunction* NewThisFunction(int pos) {
3649 return new (local_zone_) ThisFunction(local_zone_, pos);
3652 SuperPropertyReference* NewSuperPropertyReference(VariableProxy* this_var,
3653 Expression* home_object,
3655 return new (parser_zone_)
3656 SuperPropertyReference(parser_zone_, this_var, home_object, pos);
3659 SuperCallReference* NewSuperCallReference(VariableProxy* this_var,
3660 VariableProxy* new_target_var,
3661 VariableProxy* this_function_var,
3663 return new (parser_zone_) SuperCallReference(
3664 parser_zone_, this_var, new_target_var, this_function_var, pos);
3667 EmptyParentheses* NewEmptyParentheses(int pos) {
3668 return new (local_zone_) EmptyParentheses(local_zone_, pos);
3671 Zone* zone() const { return local_zone_; }
3673 // Handles use of temporary zones when parsing inner function bodies.
3676 BodyScope(AstNodeFactory* factory, Zone* temp_zone, bool can_use_temp_zone)
3677 : factory_(factory), prev_zone_(factory->local_zone_) {
3678 if (can_use_temp_zone) {
3679 factory->local_zone_ = temp_zone;
3683 ~BodyScope() { factory_->local_zone_ = prev_zone_; }
3686 AstNodeFactory* factory_;
3691 // This zone may be deallocated upon returning from parsing a function body
3692 // which we can guarantee is not going to be compiled or have its AST
3694 // See ParseFunctionLiteral in parser.cc for preconditions.
3696 // ZoneObjects which need to persist until scope analysis must be allocated in
3697 // the parser-level zone.
3699 AstValueFactory* ast_value_factory_;
3703 } } // namespace v8::internal