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 FeedbackVectorICSlot GetSlot(int offset = 0) const {
1403 if (ic_slot_or_count_ == FeedbackVectorICSlot::Invalid().ToInt()) {
1404 return FeedbackVectorICSlot::Invalid();
1406 return FeedbackVectorICSlot(ic_slot_or_count_ + offset);
1409 int ic_slot_count() const {
1410 if (ic_slot_or_count_ == FeedbackVectorICSlot::Invalid().ToInt()) {
1413 return ic_slot_or_count_;
1416 void set_receiver_type(Handle<Map> map) { receiver_type_ = map; }
1417 void set_ic_slot_count(int count) {
1418 // Should only be called once.
1420 ic_slot_or_count_ = FeedbackVectorICSlot::Invalid().ToInt();
1422 ic_slot_or_count_ = count;
1426 int set_base_slot(int slot) {
1427 if (ic_slot_count() > 0) {
1428 int count = ic_slot_count();
1429 ic_slot_or_count_ = slot;
1436 friend class AstNodeFactory;
1438 ObjectLiteralProperty(Expression* key, Expression* value, Kind kind,
1439 bool is_static, bool is_computed_name);
1440 ObjectLiteralProperty(AstValueFactory* ast_value_factory, Expression* key,
1441 Expression* value, bool is_static,
1442 bool is_computed_name);
1447 int ic_slot_or_count_;
1451 bool is_computed_name_;
1452 Handle<Map> receiver_type_;
1456 // An object literal has a boilerplate object that is used
1457 // for minimizing the work when constructing it at runtime.
1458 class ObjectLiteral final : public MaterializedLiteral {
1460 typedef ObjectLiteralProperty Property;
1462 DECLARE_NODE_TYPE(ObjectLiteral)
1464 Handle<FixedArray> constant_properties() const {
1465 return constant_properties_;
1467 int properties_count() const { return constant_properties_->length() / 2; }
1468 ZoneList<Property*>* properties() const { return properties_; }
1469 bool fast_elements() const { return fast_elements_; }
1470 bool may_store_doubles() const { return may_store_doubles_; }
1471 bool has_function() const { return has_function_; }
1472 bool has_elements() const { return has_elements_; }
1474 // Decide if a property should be in the object boilerplate.
1475 static bool IsBoilerplateProperty(Property* property);
1477 // Populate the constant properties fixed array.
1478 void BuildConstantProperties(Isolate* isolate);
1480 // Mark all computed expressions that are bound to a key that
1481 // is shadowed by a later occurrence of the same key. For the
1482 // marked expressions, no store code is emitted.
1483 void CalculateEmitStore(Zone* zone);
1485 // Assemble bitfield of flags for the CreateObjectLiteral helper.
1486 int ComputeFlags(bool disable_mementos = false) const {
1487 int flags = fast_elements() ? kFastElements : kNoFlags;
1488 flags |= has_function() ? kHasFunction : kNoFlags;
1489 if (depth() == 1 && !has_elements() && !may_store_doubles()) {
1490 flags |= kShallowProperties;
1492 if (disable_mementos) {
1493 flags |= kDisableMementos;
1504 kHasFunction = 1 << 1,
1505 kShallowProperties = 1 << 2,
1506 kDisableMementos = 1 << 3,
1510 struct Accessors: public ZoneObject {
1511 Accessors() : getter(NULL), setter(NULL) {}
1512 ObjectLiteralProperty* getter;
1513 ObjectLiteralProperty* setter;
1516 BailoutId CreateLiteralId() const { return BailoutId(local_id(0)); }
1518 // Return an AST id for a property that is used in simulate instructions.
1519 BailoutId GetIdForProperty(int i) { return BailoutId(local_id(i + 1)); }
1521 // Unlike other AST nodes, this number of bailout IDs allocated for an
1522 // ObjectLiteral can vary, so num_ids() is not a static method.
1523 int num_ids() const { return parent_num_ids() + 1 + properties()->length(); }
1525 // Object literals need one feedback slot for each non-trivial value, as well
1526 // as some slots for home objects.
1527 FeedbackVectorRequirements ComputeFeedbackRequirements(
1528 Isolate* isolate, const ICSlotCache* cache) override;
1529 void SetFirstFeedbackICSlot(FeedbackVectorICSlot slot,
1530 ICSlotCache* cache) override {
1533 Code::Kind FeedbackICSlotKind(int index) override { return Code::STORE_IC; }
1535 // After feedback slots were assigned, propagate information to the properties
1537 void LayoutFeedbackSlots();
1540 ObjectLiteral(Zone* zone, ZoneList<Property*>* properties, int literal_index,
1541 int boilerplate_properties, bool has_function, bool is_strong,
1543 : MaterializedLiteral(zone, literal_index, is_strong, pos),
1544 properties_(properties),
1545 boilerplate_properties_(boilerplate_properties),
1546 fast_elements_(false),
1547 has_elements_(false),
1548 may_store_doubles_(false),
1549 has_function_(has_function),
1550 slot_(FeedbackVectorICSlot::Invalid()) {
1552 static int parent_num_ids() { return MaterializedLiteral::num_ids(); }
1555 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
1556 Handle<FixedArray> constant_properties_;
1557 ZoneList<Property*>* properties_;
1558 int boilerplate_properties_;
1559 bool fast_elements_;
1561 bool may_store_doubles_;
1563 FeedbackVectorICSlot slot_;
1567 // Node for capturing a regexp literal.
1568 class RegExpLiteral final : public MaterializedLiteral {
1570 DECLARE_NODE_TYPE(RegExpLiteral)
1572 Handle<String> pattern() const { return pattern_->string(); }
1573 Handle<String> flags() const { return flags_->string(); }
1576 RegExpLiteral(Zone* zone, const AstRawString* pattern,
1577 const AstRawString* flags, int literal_index, bool is_strong,
1579 : MaterializedLiteral(zone, literal_index, is_strong, pos),
1586 const AstRawString* pattern_;
1587 const AstRawString* flags_;
1591 // An array literal has a literals object that is used
1592 // for minimizing the work when constructing it at runtime.
1593 class ArrayLiteral final : public MaterializedLiteral {
1595 DECLARE_NODE_TYPE(ArrayLiteral)
1597 Handle<FixedArray> constant_elements() const { return constant_elements_; }
1598 ElementsKind constant_elements_kind() const {
1599 DCHECK_EQ(2, constant_elements_->length());
1600 return static_cast<ElementsKind>(
1601 Smi::cast(constant_elements_->get(0))->value());
1604 ZoneList<Expression*>* values() const { return values_; }
1606 BailoutId CreateLiteralId() const { return BailoutId(local_id(0)); }
1608 // Return an AST id for an element that is used in simulate instructions.
1609 BailoutId GetIdForElement(int i) { return BailoutId(local_id(i + 1)); }
1611 // Unlike other AST nodes, this number of bailout IDs allocated for an
1612 // ArrayLiteral can vary, so num_ids() is not a static method.
1613 int num_ids() const { return parent_num_ids() + 1 + values()->length(); }
1615 // Populate the constant elements fixed array.
1616 void BuildConstantElements(Isolate* isolate);
1618 // Assemble bitfield of flags for the CreateArrayLiteral helper.
1619 int ComputeFlags(bool disable_mementos = false) const {
1620 int flags = depth() == 1 ? kShallowElements : kNoFlags;
1621 if (disable_mementos) {
1622 flags |= kDisableMementos;
1632 kShallowElements = 1,
1633 kDisableMementos = 1 << 1,
1638 ArrayLiteral(Zone* zone, ZoneList<Expression*>* values,
1639 int first_spread_index, int literal_index, bool is_strong,
1641 : MaterializedLiteral(zone, literal_index, is_strong, pos),
1643 first_spread_index_(first_spread_index) {}
1644 static int parent_num_ids() { return MaterializedLiteral::num_ids(); }
1647 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
1649 Handle<FixedArray> constant_elements_;
1650 ZoneList<Expression*>* values_;
1651 int first_spread_index_;
1655 class VariableProxy final : public Expression {
1657 DECLARE_NODE_TYPE(VariableProxy)
1659 bool IsValidReferenceExpression() const override {
1660 return !is_this() && !is_new_target();
1663 bool IsArguments() const { return is_resolved() && var()->is_arguments(); }
1665 Handle<String> name() const { return raw_name()->string(); }
1666 const AstRawString* raw_name() const {
1667 return is_resolved() ? var_->raw_name() : raw_name_;
1670 Variable* var() const {
1671 DCHECK(is_resolved());
1674 void set_var(Variable* v) {
1675 DCHECK(!is_resolved());
1680 bool is_this() const { return IsThisField::decode(bit_field_); }
1682 bool is_assigned() const { return IsAssignedField::decode(bit_field_); }
1683 void set_is_assigned() {
1684 bit_field_ = IsAssignedField::update(bit_field_, true);
1687 bool is_resolved() const { return IsResolvedField::decode(bit_field_); }
1688 void set_is_resolved() {
1689 bit_field_ = IsResolvedField::update(bit_field_, true);
1692 bool is_new_target() const { return IsNewTargetField::decode(bit_field_); }
1693 void set_is_new_target() {
1694 bit_field_ = IsNewTargetField::update(bit_field_, true);
1697 int end_position() const { return end_position_; }
1699 // Bind this proxy to the variable var.
1700 void BindTo(Variable* var);
1702 bool UsesVariableFeedbackSlot() const {
1703 return var()->IsUnallocated() || var()->IsLookupSlot();
1706 virtual FeedbackVectorRequirements ComputeFeedbackRequirements(
1707 Isolate* isolate, const ICSlotCache* cache) override;
1709 void SetFirstFeedbackICSlot(FeedbackVectorICSlot slot,
1710 ICSlotCache* cache) override;
1711 Code::Kind FeedbackICSlotKind(int index) override { return Code::LOAD_IC; }
1712 FeedbackVectorICSlot VariableFeedbackSlot() {
1713 return variable_feedback_slot_;
1716 static int num_ids() { return parent_num_ids() + 1; }
1717 BailoutId BeforeId() const { return BailoutId(local_id(0)); }
1720 VariableProxy(Zone* zone, Variable* var, int start_position,
1723 VariableProxy(Zone* zone, const AstRawString* name,
1724 Variable::Kind variable_kind, int start_position,
1726 static int parent_num_ids() { return Expression::num_ids(); }
1727 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
1729 class IsThisField : public BitField8<bool, 0, 1> {};
1730 class IsAssignedField : public BitField8<bool, 1, 1> {};
1731 class IsResolvedField : public BitField8<bool, 2, 1> {};
1732 class IsNewTargetField : public BitField8<bool, 3, 1> {};
1734 // Start with 16-bit (or smaller) field, which should get packed together
1735 // with Expression's trailing 16-bit field.
1737 FeedbackVectorICSlot variable_feedback_slot_;
1739 const AstRawString* raw_name_; // if !is_resolved_
1740 Variable* var_; // if is_resolved_
1742 // Position is stored in the AstNode superclass, but VariableProxy needs to
1743 // know its end position too (for error messages). It cannot be inferred from
1744 // the variable name length because it can contain escapes.
1749 // Left-hand side can only be a property, a global or a (parameter or local)
1755 NAMED_SUPER_PROPERTY,
1756 KEYED_SUPER_PROPERTY
1760 class Property final : public Expression {
1762 DECLARE_NODE_TYPE(Property)
1764 bool IsValidReferenceExpression() const override { return true; }
1766 Expression* obj() const { return obj_; }
1767 Expression* key() const { return key_; }
1769 static int num_ids() { return parent_num_ids() + 1; }
1770 BailoutId LoadId() const { return BailoutId(local_id(0)); }
1772 bool IsStringAccess() const {
1773 return IsStringAccessField::decode(bit_field_);
1776 // Type feedback information.
1777 bool IsMonomorphic() override { return receiver_types_.length() == 1; }
1778 SmallMapList* GetReceiverTypes() override { return &receiver_types_; }
1779 KeyedAccessStoreMode GetStoreMode() const override { return STANDARD_STORE; }
1780 IcCheckType GetKeyType() const override {
1781 return KeyTypeField::decode(bit_field_);
1783 bool IsUninitialized() const {
1784 return !is_for_call() && HasNoTypeInformation();
1786 bool HasNoTypeInformation() const {
1787 return GetInlineCacheState() == UNINITIALIZED;
1789 InlineCacheState GetInlineCacheState() const {
1790 return InlineCacheStateField::decode(bit_field_);
1792 void set_is_string_access(bool b) {
1793 bit_field_ = IsStringAccessField::update(bit_field_, b);
1795 void set_key_type(IcCheckType key_type) {
1796 bit_field_ = KeyTypeField::update(bit_field_, key_type);
1798 void set_inline_cache_state(InlineCacheState state) {
1799 bit_field_ = InlineCacheStateField::update(bit_field_, state);
1801 void mark_for_call() {
1802 bit_field_ = IsForCallField::update(bit_field_, true);
1804 bool is_for_call() const { return IsForCallField::decode(bit_field_); }
1806 bool IsSuperAccess() { return obj()->IsSuperPropertyReference(); }
1808 virtual FeedbackVectorRequirements ComputeFeedbackRequirements(
1809 Isolate* isolate, const ICSlotCache* cache) override {
1810 return FeedbackVectorRequirements(0, 1);
1812 void SetFirstFeedbackICSlot(FeedbackVectorICSlot slot,
1813 ICSlotCache* cache) override {
1814 property_feedback_slot_ = slot;
1816 Code::Kind FeedbackICSlotKind(int index) override {
1817 return key()->IsPropertyName() ? Code::LOAD_IC : Code::KEYED_LOAD_IC;
1820 FeedbackVectorICSlot PropertyFeedbackSlot() const {
1821 return property_feedback_slot_;
1824 static LhsKind GetAssignType(Property* property) {
1825 if (property == NULL) return VARIABLE;
1826 bool super_access = property->IsSuperAccess();
1827 return (property->key()->IsPropertyName())
1828 ? (super_access ? NAMED_SUPER_PROPERTY : NAMED_PROPERTY)
1829 : (super_access ? KEYED_SUPER_PROPERTY : KEYED_PROPERTY);
1833 Property(Zone* zone, Expression* obj, Expression* key, int pos)
1834 : Expression(zone, pos),
1835 bit_field_(IsForCallField::encode(false) |
1836 IsStringAccessField::encode(false) |
1837 InlineCacheStateField::encode(UNINITIALIZED)),
1838 property_feedback_slot_(FeedbackVectorICSlot::Invalid()),
1841 static int parent_num_ids() { return Expression::num_ids(); }
1844 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
1846 class IsForCallField : public BitField8<bool, 0, 1> {};
1847 class IsStringAccessField : public BitField8<bool, 1, 1> {};
1848 class KeyTypeField : public BitField8<IcCheckType, 2, 1> {};
1849 class InlineCacheStateField : public BitField8<InlineCacheState, 3, 4> {};
1851 FeedbackVectorICSlot property_feedback_slot_;
1854 SmallMapList receiver_types_;
1858 class Call final : public Expression {
1860 DECLARE_NODE_TYPE(Call)
1862 Expression* expression() const { return expression_; }
1863 ZoneList<Expression*>* arguments() const { return arguments_; }
1865 // Type feedback information.
1866 virtual FeedbackVectorRequirements ComputeFeedbackRequirements(
1867 Isolate* isolate, const ICSlotCache* cache) override;
1868 void SetFirstFeedbackICSlot(FeedbackVectorICSlot slot,
1869 ICSlotCache* cache) override {
1872 void SetFirstFeedbackSlot(FeedbackVectorSlot slot) override { slot_ = slot; }
1873 Code::Kind FeedbackICSlotKind(int index) override { return Code::CALL_IC; }
1875 FeedbackVectorSlot CallFeedbackSlot() const { return slot_; }
1877 FeedbackVectorICSlot CallFeedbackICSlot() const { return ic_slot_; }
1879 SmallMapList* GetReceiverTypes() override {
1880 if (expression()->IsProperty()) {
1881 return expression()->AsProperty()->GetReceiverTypes();
1886 bool IsMonomorphic() override {
1887 if (expression()->IsProperty()) {
1888 return expression()->AsProperty()->IsMonomorphic();
1890 return !target_.is_null();
1893 bool global_call() const {
1894 VariableProxy* proxy = expression_->AsVariableProxy();
1895 return proxy != NULL && proxy->var()->IsUnallocatedOrGlobalSlot();
1898 bool known_global_function() const {
1899 return global_call() && !target_.is_null();
1902 Handle<JSFunction> target() { return target_; }
1904 Handle<AllocationSite> allocation_site() { return allocation_site_; }
1906 void SetKnownGlobalTarget(Handle<JSFunction> target) {
1908 set_is_uninitialized(false);
1910 void set_target(Handle<JSFunction> target) { target_ = target; }
1911 void set_allocation_site(Handle<AllocationSite> site) {
1912 allocation_site_ = site;
1915 static int num_ids() { return parent_num_ids() + 3; }
1916 BailoutId ReturnId() const { return BailoutId(local_id(0)); }
1917 BailoutId EvalId() const { return BailoutId(local_id(1)); }
1918 BailoutId LookupId() const { return BailoutId(local_id(2)); }
1920 bool is_uninitialized() const {
1921 return IsUninitializedField::decode(bit_field_);
1923 void set_is_uninitialized(bool b) {
1924 bit_field_ = IsUninitializedField::update(bit_field_, b);
1936 // Helpers to determine how to handle the call.
1937 CallType GetCallType(Isolate* isolate) const;
1938 bool IsUsingCallFeedbackSlot(Isolate* isolate) const;
1939 bool IsUsingCallFeedbackICSlot(Isolate* isolate) const;
1942 // Used to assert that the FullCodeGenerator records the return site.
1943 bool return_is_recorded_;
1947 Call(Zone* zone, Expression* expression, ZoneList<Expression*>* arguments,
1949 : Expression(zone, pos),
1950 ic_slot_(FeedbackVectorICSlot::Invalid()),
1951 slot_(FeedbackVectorSlot::Invalid()),
1952 expression_(expression),
1953 arguments_(arguments),
1954 bit_field_(IsUninitializedField::encode(false)) {
1955 if (expression->IsProperty()) {
1956 expression->AsProperty()->mark_for_call();
1959 static int parent_num_ids() { return Expression::num_ids(); }
1962 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
1964 FeedbackVectorICSlot ic_slot_;
1965 FeedbackVectorSlot slot_;
1966 Expression* expression_;
1967 ZoneList<Expression*>* arguments_;
1968 Handle<JSFunction> target_;
1969 Handle<AllocationSite> allocation_site_;
1970 class IsUninitializedField : public BitField8<bool, 0, 1> {};
1975 class CallNew final : public Expression {
1977 DECLARE_NODE_TYPE(CallNew)
1979 Expression* expression() const { return expression_; }
1980 ZoneList<Expression*>* arguments() const { return arguments_; }
1982 // Type feedback information.
1983 virtual FeedbackVectorRequirements ComputeFeedbackRequirements(
1984 Isolate* isolate, const ICSlotCache* cache) override {
1985 return FeedbackVectorRequirements(FLAG_pretenuring_call_new ? 2 : 1, 0);
1987 void SetFirstFeedbackSlot(FeedbackVectorSlot slot) override {
1988 callnew_feedback_slot_ = slot;
1991 FeedbackVectorSlot CallNewFeedbackSlot() {
1992 DCHECK(!callnew_feedback_slot_.IsInvalid());
1993 return callnew_feedback_slot_;
1995 FeedbackVectorSlot AllocationSiteFeedbackSlot() {
1996 DCHECK(FLAG_pretenuring_call_new);
1997 return CallNewFeedbackSlot().next();
2000 bool IsMonomorphic() override { return is_monomorphic_; }
2001 Handle<JSFunction> target() const { return target_; }
2002 Handle<AllocationSite> allocation_site() const {
2003 return allocation_site_;
2006 static int num_ids() { return parent_num_ids() + 1; }
2007 static int feedback_slots() { return 1; }
2008 BailoutId ReturnId() const { return BailoutId(local_id(0)); }
2010 void set_allocation_site(Handle<AllocationSite> site) {
2011 allocation_site_ = site;
2013 void set_is_monomorphic(bool monomorphic) { is_monomorphic_ = monomorphic; }
2014 void set_target(Handle<JSFunction> target) { target_ = target; }
2015 void SetKnownGlobalTarget(Handle<JSFunction> target) {
2017 is_monomorphic_ = true;
2021 CallNew(Zone* zone, Expression* expression, ZoneList<Expression*>* arguments,
2023 : Expression(zone, pos),
2024 expression_(expression),
2025 arguments_(arguments),
2026 is_monomorphic_(false),
2027 callnew_feedback_slot_(FeedbackVectorSlot::Invalid()) {}
2029 static int parent_num_ids() { return Expression::num_ids(); }
2032 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
2034 Expression* expression_;
2035 ZoneList<Expression*>* arguments_;
2036 bool is_monomorphic_;
2037 Handle<JSFunction> target_;
2038 Handle<AllocationSite> allocation_site_;
2039 FeedbackVectorSlot callnew_feedback_slot_;
2043 // The CallRuntime class does not represent any official JavaScript
2044 // language construct. Instead it is used to call a C or JS function
2045 // with a set of arguments. This is used from the builtins that are
2046 // implemented in JavaScript (see "v8natives.js").
2047 class CallRuntime final : public Expression {
2049 DECLARE_NODE_TYPE(CallRuntime)
2051 ZoneList<Expression*>* arguments() const { return arguments_; }
2052 bool is_jsruntime() const { return function_ == NULL; }
2054 int context_index() const {
2055 DCHECK(is_jsruntime());
2056 return context_index_;
2058 const Runtime::Function* function() const {
2059 DCHECK(!is_jsruntime());
2063 static int num_ids() { return parent_num_ids() + 1; }
2064 BailoutId CallId() { return BailoutId(local_id(0)); }
2066 const char* debug_name() {
2067 return is_jsruntime() ? "(context function)" : function_->name;
2071 CallRuntime(Zone* zone, const Runtime::Function* function,
2072 ZoneList<Expression*>* arguments, int pos)
2073 : Expression(zone, pos), function_(function), arguments_(arguments) {}
2075 CallRuntime(Zone* zone, int context_index, ZoneList<Expression*>* arguments,
2077 : Expression(zone, pos),
2079 context_index_(context_index),
2080 arguments_(arguments) {}
2082 static int parent_num_ids() { return Expression::num_ids(); }
2085 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
2087 const Runtime::Function* function_;
2089 ZoneList<Expression*>* arguments_;
2093 class UnaryOperation final : public Expression {
2095 DECLARE_NODE_TYPE(UnaryOperation)
2097 Token::Value op() const { return op_; }
2098 Expression* expression() const { return expression_; }
2100 // For unary not (Token::NOT), the AST ids where true and false will
2101 // actually be materialized, respectively.
2102 static int num_ids() { return parent_num_ids() + 2; }
2103 BailoutId MaterializeTrueId() const { return BailoutId(local_id(0)); }
2104 BailoutId MaterializeFalseId() const { return BailoutId(local_id(1)); }
2106 virtual void RecordToBooleanTypeFeedback(TypeFeedbackOracle* oracle) override;
2109 UnaryOperation(Zone* zone, Token::Value op, Expression* expression, int pos)
2110 : Expression(zone, pos), op_(op), expression_(expression) {
2111 DCHECK(Token::IsUnaryOp(op));
2113 static int parent_num_ids() { return Expression::num_ids(); }
2116 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
2119 Expression* expression_;
2123 class BinaryOperation final : public Expression {
2125 DECLARE_NODE_TYPE(BinaryOperation)
2127 Token::Value op() const { return static_cast<Token::Value>(op_); }
2128 Expression* left() const { return left_; }
2129 Expression* right() const { return right_; }
2130 Handle<AllocationSite> allocation_site() const { return allocation_site_; }
2131 void set_allocation_site(Handle<AllocationSite> allocation_site) {
2132 allocation_site_ = allocation_site;
2135 // The short-circuit logical operations need an AST ID for their
2136 // right-hand subexpression.
2137 static int num_ids() { return parent_num_ids() + 2; }
2138 BailoutId RightId() const { return BailoutId(local_id(0)); }
2140 TypeFeedbackId BinaryOperationFeedbackId() const {
2141 return TypeFeedbackId(local_id(1));
2143 Maybe<int> fixed_right_arg() const {
2144 return has_fixed_right_arg_ ? Just(fixed_right_arg_value_) : Nothing<int>();
2146 void set_fixed_right_arg(Maybe<int> arg) {
2147 has_fixed_right_arg_ = arg.IsJust();
2148 if (arg.IsJust()) fixed_right_arg_value_ = arg.FromJust();
2151 virtual void RecordToBooleanTypeFeedback(TypeFeedbackOracle* oracle) override;
2154 BinaryOperation(Zone* zone, Token::Value op, Expression* left,
2155 Expression* right, int pos)
2156 : Expression(zone, pos),
2157 op_(static_cast<byte>(op)),
2158 has_fixed_right_arg_(false),
2159 fixed_right_arg_value_(0),
2162 DCHECK(Token::IsBinaryOp(op));
2164 static int parent_num_ids() { return Expression::num_ids(); }
2167 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
2169 const byte op_; // actually Token::Value
2170 // TODO(rossberg): the fixed arg should probably be represented as a Constant
2171 // type for the RHS. Currenty it's actually a Maybe<int>
2172 bool has_fixed_right_arg_;
2173 int fixed_right_arg_value_;
2176 Handle<AllocationSite> allocation_site_;
2180 class CountOperation final : public Expression {
2182 DECLARE_NODE_TYPE(CountOperation)
2184 bool is_prefix() const { return IsPrefixField::decode(bit_field_); }
2185 bool is_postfix() const { return !is_prefix(); }
2187 Token::Value op() const { return TokenField::decode(bit_field_); }
2188 Token::Value binary_op() {
2189 return (op() == Token::INC) ? Token::ADD : Token::SUB;
2192 Expression* expression() const { return expression_; }
2194 bool IsMonomorphic() override { return receiver_types_.length() == 1; }
2195 SmallMapList* GetReceiverTypes() override { return &receiver_types_; }
2196 IcCheckType GetKeyType() const override {
2197 return KeyTypeField::decode(bit_field_);
2199 KeyedAccessStoreMode GetStoreMode() const override {
2200 return StoreModeField::decode(bit_field_);
2202 Type* type() const { return type_; }
2203 void set_key_type(IcCheckType type) {
2204 bit_field_ = KeyTypeField::update(bit_field_, type);
2206 void set_store_mode(KeyedAccessStoreMode mode) {
2207 bit_field_ = StoreModeField::update(bit_field_, mode);
2209 void set_type(Type* type) { type_ = type; }
2211 static int num_ids() { return parent_num_ids() + 4; }
2212 BailoutId AssignmentId() const { return BailoutId(local_id(0)); }
2213 BailoutId ToNumberId() const { return BailoutId(local_id(1)); }
2214 TypeFeedbackId CountBinOpFeedbackId() const {
2215 return TypeFeedbackId(local_id(2));
2217 TypeFeedbackId CountStoreFeedbackId() const {
2218 return TypeFeedbackId(local_id(3));
2221 FeedbackVectorRequirements ComputeFeedbackRequirements(
2222 Isolate* isolate, const ICSlotCache* cache) override;
2223 void SetFirstFeedbackICSlot(FeedbackVectorICSlot slot,
2224 ICSlotCache* cache) override {
2227 Code::Kind FeedbackICSlotKind(int index) override;
2228 FeedbackVectorICSlot CountSlot() const { return slot_; }
2231 CountOperation(Zone* zone, Token::Value op, bool is_prefix, Expression* expr,
2233 : Expression(zone, pos),
2235 IsPrefixField::encode(is_prefix) | KeyTypeField::encode(ELEMENT) |
2236 StoreModeField::encode(STANDARD_STORE) | TokenField::encode(op)),
2239 slot_(FeedbackVectorICSlot::Invalid()) {}
2240 static int parent_num_ids() { return Expression::num_ids(); }
2243 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
2245 class IsPrefixField : public BitField16<bool, 0, 1> {};
2246 class KeyTypeField : public BitField16<IcCheckType, 1, 1> {};
2247 class StoreModeField : public BitField16<KeyedAccessStoreMode, 2, 3> {};
2248 class TokenField : public BitField16<Token::Value, 5, 8> {};
2250 // Starts with 16-bit field, which should get packed together with
2251 // Expression's trailing 16-bit field.
2252 uint16_t bit_field_;
2254 Expression* expression_;
2255 SmallMapList receiver_types_;
2256 FeedbackVectorICSlot slot_;
2260 class CompareOperation final : public Expression {
2262 DECLARE_NODE_TYPE(CompareOperation)
2264 Token::Value op() const { return op_; }
2265 Expression* left() const { return left_; }
2266 Expression* right() const { return right_; }
2268 // Type feedback information.
2269 static int num_ids() { return parent_num_ids() + 1; }
2270 TypeFeedbackId CompareOperationFeedbackId() const {
2271 return TypeFeedbackId(local_id(0));
2273 Type* combined_type() const { return combined_type_; }
2274 void set_combined_type(Type* type) { combined_type_ = type; }
2276 // Match special cases.
2277 bool IsLiteralCompareTypeof(Expression** expr, Handle<String>* check);
2278 bool IsLiteralCompareUndefined(Expression** expr, Isolate* isolate);
2279 bool IsLiteralCompareNull(Expression** expr);
2282 CompareOperation(Zone* zone, Token::Value op, Expression* left,
2283 Expression* right, int pos)
2284 : Expression(zone, pos),
2288 combined_type_(Type::None(zone)) {
2289 DCHECK(Token::IsCompareOp(op));
2291 static int parent_num_ids() { return Expression::num_ids(); }
2294 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
2300 Type* combined_type_;
2304 class Spread final : public Expression {
2306 DECLARE_NODE_TYPE(Spread)
2308 Expression* expression() const { return expression_; }
2310 static int num_ids() { return parent_num_ids(); }
2313 Spread(Zone* zone, Expression* expression, int pos)
2314 : Expression(zone, pos), expression_(expression) {}
2315 static int parent_num_ids() { return Expression::num_ids(); }
2318 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
2320 Expression* expression_;
2324 class Conditional final : public Expression {
2326 DECLARE_NODE_TYPE(Conditional)
2328 Expression* condition() const { return condition_; }
2329 Expression* then_expression() const { return then_expression_; }
2330 Expression* else_expression() const { return else_expression_; }
2332 static int num_ids() { return parent_num_ids() + 2; }
2333 BailoutId ThenId() const { return BailoutId(local_id(0)); }
2334 BailoutId ElseId() const { return BailoutId(local_id(1)); }
2337 Conditional(Zone* zone, Expression* condition, Expression* then_expression,
2338 Expression* else_expression, int position)
2339 : Expression(zone, position),
2340 condition_(condition),
2341 then_expression_(then_expression),
2342 else_expression_(else_expression) {}
2343 static int parent_num_ids() { return Expression::num_ids(); }
2346 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
2348 Expression* condition_;
2349 Expression* then_expression_;
2350 Expression* else_expression_;
2354 class Assignment final : public Expression {
2356 DECLARE_NODE_TYPE(Assignment)
2358 Assignment* AsSimpleAssignment() { return !is_compound() ? this : NULL; }
2360 Token::Value binary_op() const;
2362 Token::Value op() const { return TokenField::decode(bit_field_); }
2363 Expression* target() const { return target_; }
2364 Expression* value() const { return value_; }
2365 BinaryOperation* binary_operation() const { return binary_operation_; }
2367 // This check relies on the definition order of token in token.h.
2368 bool is_compound() const { return op() > Token::ASSIGN; }
2370 static int num_ids() { return parent_num_ids() + 2; }
2371 BailoutId AssignmentId() const { return BailoutId(local_id(0)); }
2373 // Type feedback information.
2374 TypeFeedbackId AssignmentFeedbackId() { return TypeFeedbackId(local_id(1)); }
2375 bool IsMonomorphic() override { return receiver_types_.length() == 1; }
2376 bool IsUninitialized() const {
2377 return IsUninitializedField::decode(bit_field_);
2379 bool HasNoTypeInformation() {
2380 return IsUninitializedField::decode(bit_field_);
2382 SmallMapList* GetReceiverTypes() override { return &receiver_types_; }
2383 IcCheckType GetKeyType() const override {
2384 return KeyTypeField::decode(bit_field_);
2386 KeyedAccessStoreMode GetStoreMode() const override {
2387 return StoreModeField::decode(bit_field_);
2389 void set_is_uninitialized(bool b) {
2390 bit_field_ = IsUninitializedField::update(bit_field_, b);
2392 void set_key_type(IcCheckType key_type) {
2393 bit_field_ = KeyTypeField::update(bit_field_, key_type);
2395 void set_store_mode(KeyedAccessStoreMode mode) {
2396 bit_field_ = StoreModeField::update(bit_field_, mode);
2399 FeedbackVectorRequirements ComputeFeedbackRequirements(
2400 Isolate* isolate, const ICSlotCache* cache) override;
2401 void SetFirstFeedbackICSlot(FeedbackVectorICSlot slot,
2402 ICSlotCache* cache) override {
2405 Code::Kind FeedbackICSlotKind(int index) override;
2406 FeedbackVectorICSlot AssignmentSlot() const { return slot_; }
2409 Assignment(Zone* zone, Token::Value op, Expression* target, Expression* value,
2411 static int parent_num_ids() { return Expression::num_ids(); }
2414 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
2416 class IsUninitializedField : public BitField16<bool, 0, 1> {};
2417 class KeyTypeField : public BitField16<IcCheckType, 1, 1> {};
2418 class StoreModeField : public BitField16<KeyedAccessStoreMode, 2, 3> {};
2419 class TokenField : public BitField16<Token::Value, 5, 8> {};
2421 // Starts with 16-bit field, which should get packed together with
2422 // Expression's trailing 16-bit field.
2423 uint16_t bit_field_;
2424 Expression* target_;
2426 BinaryOperation* binary_operation_;
2427 SmallMapList receiver_types_;
2428 FeedbackVectorICSlot slot_;
2432 class Yield final : public Expression {
2434 DECLARE_NODE_TYPE(Yield)
2437 kInitial, // The initial yield that returns the unboxed generator object.
2438 kSuspend, // A normal yield: { value: EXPRESSION, done: false }
2439 kDelegating, // A yield*.
2440 kFinal // A return: { value: EXPRESSION, done: true }
2443 Expression* generator_object() const { return generator_object_; }
2444 Expression* expression() const { return expression_; }
2445 Kind yield_kind() const { return yield_kind_; }
2447 // Type feedback information.
2448 bool HasFeedbackSlots() const { return yield_kind() == kDelegating; }
2449 virtual FeedbackVectorRequirements ComputeFeedbackRequirements(
2450 Isolate* isolate, const ICSlotCache* cache) override {
2451 return FeedbackVectorRequirements(0, HasFeedbackSlots() ? 3 : 0);
2453 void SetFirstFeedbackICSlot(FeedbackVectorICSlot slot,
2454 ICSlotCache* cache) override {
2455 yield_first_feedback_slot_ = slot;
2457 Code::Kind FeedbackICSlotKind(int index) override {
2458 return index == 0 ? Code::KEYED_LOAD_IC : Code::LOAD_IC;
2461 FeedbackVectorICSlot KeyedLoadFeedbackSlot() {
2462 DCHECK(!HasFeedbackSlots() || !yield_first_feedback_slot_.IsInvalid());
2463 return yield_first_feedback_slot_;
2466 FeedbackVectorICSlot DoneFeedbackSlot() {
2467 return KeyedLoadFeedbackSlot().next();
2470 FeedbackVectorICSlot ValueFeedbackSlot() { return DoneFeedbackSlot().next(); }
2473 Yield(Zone* zone, Expression* generator_object, Expression* expression,
2474 Kind yield_kind, int pos)
2475 : Expression(zone, pos),
2476 generator_object_(generator_object),
2477 expression_(expression),
2478 yield_kind_(yield_kind),
2479 yield_first_feedback_slot_(FeedbackVectorICSlot::Invalid()) {}
2482 Expression* generator_object_;
2483 Expression* expression_;
2485 FeedbackVectorICSlot yield_first_feedback_slot_;
2489 class Throw final : public Expression {
2491 DECLARE_NODE_TYPE(Throw)
2493 Expression* exception() const { return exception_; }
2496 Throw(Zone* zone, Expression* exception, int pos)
2497 : Expression(zone, pos), exception_(exception) {}
2500 Expression* exception_;
2504 class FunctionLiteral final : public Expression {
2507 ANONYMOUS_EXPRESSION,
2512 enum ParameterFlag {
2513 kNoDuplicateParameters = 0,
2514 kHasDuplicateParameters = 1
2517 enum IsFunctionFlag {
2522 enum EagerCompileHint { kShouldEagerCompile, kShouldLazyCompile };
2524 enum ShouldBeUsedOnceHint { kShouldBeUsedOnce, kDontKnowIfShouldBeUsedOnce };
2526 enum ArityRestriction {
2532 DECLARE_NODE_TYPE(FunctionLiteral)
2534 Handle<String> name() const { return raw_name_->string(); }
2535 const AstRawString* raw_name() const { return raw_name_; }
2536 Scope* scope() const { return scope_; }
2537 ZoneList<Statement*>* body() const { return body_; }
2538 void set_function_token_position(int pos) { function_token_position_ = pos; }
2539 int function_token_position() const { return function_token_position_; }
2540 int start_position() const;
2541 int end_position() const;
2542 int SourceSize() const { return end_position() - start_position(); }
2543 bool is_expression() const { return IsExpression::decode(bitfield_); }
2544 bool is_anonymous() const { return IsAnonymous::decode(bitfield_); }
2545 LanguageMode language_mode() const;
2547 static bool NeedsHomeObject(Expression* expr);
2549 int materialized_literal_count() { return materialized_literal_count_; }
2550 int expected_property_count() { return expected_property_count_; }
2551 int parameter_count() { return parameter_count_; }
2553 bool AllowsLazyCompilation();
2554 bool AllowsLazyCompilationWithoutContext();
2556 Handle<String> debug_name() const {
2557 if (raw_name_ != NULL && !raw_name_->IsEmpty()) {
2558 return raw_name_->string();
2560 return inferred_name();
2563 Handle<String> inferred_name() const {
2564 if (!inferred_name_.is_null()) {
2565 DCHECK(raw_inferred_name_ == NULL);
2566 return inferred_name_;
2568 if (raw_inferred_name_ != NULL) {
2569 return raw_inferred_name_->string();
2572 return Handle<String>();
2575 // Only one of {set_inferred_name, set_raw_inferred_name} should be called.
2576 void set_inferred_name(Handle<String> inferred_name) {
2577 DCHECK(!inferred_name.is_null());
2578 inferred_name_ = inferred_name;
2579 DCHECK(raw_inferred_name_== NULL || raw_inferred_name_->IsEmpty());
2580 raw_inferred_name_ = NULL;
2583 void set_raw_inferred_name(const AstString* raw_inferred_name) {
2584 DCHECK(raw_inferred_name != NULL);
2585 raw_inferred_name_ = raw_inferred_name;
2586 DCHECK(inferred_name_.is_null());
2587 inferred_name_ = Handle<String>();
2590 bool pretenure() { return Pretenure::decode(bitfield_); }
2591 void set_pretenure() { bitfield_ |= Pretenure::encode(true); }
2593 bool has_duplicate_parameters() {
2594 return HasDuplicateParameters::decode(bitfield_);
2597 bool is_function() { return IsFunction::decode(bitfield_) == kIsFunction; }
2599 // This is used as a heuristic on when to eagerly compile a function
2600 // literal. We consider the following constructs as hints that the
2601 // function will be called immediately:
2602 // - (function() { ... })();
2603 // - var x = function() { ... }();
2604 bool should_eager_compile() const {
2605 return EagerCompileHintBit::decode(bitfield_) == kShouldEagerCompile;
2607 void set_should_eager_compile() {
2608 bitfield_ = EagerCompileHintBit::update(bitfield_, kShouldEagerCompile);
2611 // A hint that we expect this function to be called (exactly) once,
2612 // i.e. we suspect it's an initialization function.
2613 bool should_be_used_once_hint() const {
2614 return ShouldBeUsedOnceHintBit::decode(bitfield_) == kShouldBeUsedOnce;
2616 void set_should_be_used_once_hint() {
2617 bitfield_ = ShouldBeUsedOnceHintBit::update(bitfield_, kShouldBeUsedOnce);
2620 FunctionKind kind() const { return FunctionKindBits::decode(bitfield_); }
2622 int ast_node_count() { return ast_properties_.node_count(); }
2623 AstProperties::Flags flags() const { return ast_properties_.flags(); }
2624 void set_ast_properties(AstProperties* ast_properties) {
2625 ast_properties_ = *ast_properties;
2627 const ZoneFeedbackVectorSpec* feedback_vector_spec() const {
2628 return ast_properties_.get_spec();
2630 bool dont_optimize() { return dont_optimize_reason_ != kNoReason; }
2631 BailoutReason dont_optimize_reason() { return dont_optimize_reason_; }
2632 void set_dont_optimize_reason(BailoutReason reason) {
2633 dont_optimize_reason_ = reason;
2637 FunctionLiteral(Zone* zone, const AstRawString* name,
2638 AstValueFactory* ast_value_factory, Scope* scope,
2639 ZoneList<Statement*>* body, int materialized_literal_count,
2640 int expected_property_count, int parameter_count,
2641 FunctionType function_type,
2642 ParameterFlag has_duplicate_parameters,
2643 IsFunctionFlag is_function,
2644 EagerCompileHint eager_compile_hint, FunctionKind kind,
2646 : Expression(zone, position),
2650 raw_inferred_name_(ast_value_factory->empty_string()),
2651 ast_properties_(zone),
2652 dont_optimize_reason_(kNoReason),
2653 materialized_literal_count_(materialized_literal_count),
2654 expected_property_count_(expected_property_count),
2655 parameter_count_(parameter_count),
2656 function_token_position_(RelocInfo::kNoPosition) {
2657 bitfield_ = IsExpression::encode(function_type != DECLARATION) |
2658 IsAnonymous::encode(function_type == ANONYMOUS_EXPRESSION) |
2659 Pretenure::encode(false) |
2660 HasDuplicateParameters::encode(has_duplicate_parameters) |
2661 IsFunction::encode(is_function) |
2662 EagerCompileHintBit::encode(eager_compile_hint) |
2663 FunctionKindBits::encode(kind) |
2664 ShouldBeUsedOnceHintBit::encode(kDontKnowIfShouldBeUsedOnce);
2665 DCHECK(IsValidFunctionKind(kind));
2669 const AstRawString* raw_name_;
2670 Handle<String> name_;
2672 ZoneList<Statement*>* body_;
2673 const AstString* raw_inferred_name_;
2674 Handle<String> inferred_name_;
2675 AstProperties ast_properties_;
2676 BailoutReason dont_optimize_reason_;
2678 int materialized_literal_count_;
2679 int expected_property_count_;
2680 int parameter_count_;
2681 int function_token_position_;
2684 class IsExpression : public BitField<bool, 0, 1> {};
2685 class IsAnonymous : public BitField<bool, 1, 1> {};
2686 class Pretenure : public BitField<bool, 2, 1> {};
2687 class HasDuplicateParameters : public BitField<ParameterFlag, 3, 1> {};
2688 class IsFunction : public BitField<IsFunctionFlag, 4, 1> {};
2689 class EagerCompileHintBit : public BitField<EagerCompileHint, 5, 1> {};
2690 class FunctionKindBits : public BitField<FunctionKind, 6, 8> {};
2691 class ShouldBeUsedOnceHintBit : public BitField<ShouldBeUsedOnceHint, 15, 1> {
2696 class ClassLiteral final : public Expression {
2698 typedef ObjectLiteralProperty Property;
2700 DECLARE_NODE_TYPE(ClassLiteral)
2702 Handle<String> name() const { return raw_name_->string(); }
2703 const AstRawString* raw_name() const { return raw_name_; }
2704 Scope* scope() const { return scope_; }
2705 VariableProxy* class_variable_proxy() const { return class_variable_proxy_; }
2706 Expression* extends() const { return extends_; }
2707 FunctionLiteral* constructor() const { return constructor_; }
2708 ZoneList<Property*>* properties() const { return properties_; }
2709 int start_position() const { return position(); }
2710 int end_position() const { return end_position_; }
2712 BailoutId EntryId() const { return BailoutId(local_id(0)); }
2713 BailoutId DeclsId() const { return BailoutId(local_id(1)); }
2714 BailoutId ExitId() { return BailoutId(local_id(2)); }
2715 BailoutId CreateLiteralId() const { return BailoutId(local_id(3)); }
2717 // Return an AST id for a property that is used in simulate instructions.
2718 BailoutId GetIdForProperty(int i) { return BailoutId(local_id(i + 4)); }
2720 // Unlike other AST nodes, this number of bailout IDs allocated for an
2721 // ClassLiteral can vary, so num_ids() is not a static method.
2722 int num_ids() const { return parent_num_ids() + 4 + properties()->length(); }
2724 // Object literals need one feedback slot for each non-trivial value, as well
2725 // as some slots for home objects.
2726 FeedbackVectorRequirements ComputeFeedbackRequirements(
2727 Isolate* isolate, const ICSlotCache* cache) override;
2728 void SetFirstFeedbackICSlot(FeedbackVectorICSlot slot,
2729 ICSlotCache* cache) override {
2732 Code::Kind FeedbackICSlotKind(int index) override { return Code::STORE_IC; }
2734 bool NeedsProxySlot() const {
2735 return FLAG_vector_stores && scope() != NULL &&
2736 class_variable_proxy()->var()->IsUnallocated();
2739 FeedbackVectorICSlot ProxySlot() const { return slot_; }
2741 // After feedback slots were assigned, propagate information to the properties
2743 void LayoutFeedbackSlots();
2746 ClassLiteral(Zone* zone, const AstRawString* name, Scope* scope,
2747 VariableProxy* class_variable_proxy, Expression* extends,
2748 FunctionLiteral* constructor, ZoneList<Property*>* properties,
2749 int start_position, int end_position)
2750 : Expression(zone, start_position),
2753 class_variable_proxy_(class_variable_proxy),
2755 constructor_(constructor),
2756 properties_(properties),
2757 end_position_(end_position),
2758 slot_(FeedbackVectorICSlot::Invalid()) {
2761 static int parent_num_ids() { return Expression::num_ids(); }
2764 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
2766 const AstRawString* raw_name_;
2768 VariableProxy* class_variable_proxy_;
2769 Expression* extends_;
2770 FunctionLiteral* constructor_;
2771 ZoneList<Property*>* properties_;
2773 FeedbackVectorICSlot slot_;
2777 class NativeFunctionLiteral final : public Expression {
2779 DECLARE_NODE_TYPE(NativeFunctionLiteral)
2781 Handle<String> name() const { return name_->string(); }
2782 v8::Extension* extension() const { return extension_; }
2785 NativeFunctionLiteral(Zone* zone, const AstRawString* name,
2786 v8::Extension* extension, int pos)
2787 : Expression(zone, pos), name_(name), extension_(extension) {}
2790 const AstRawString* name_;
2791 v8::Extension* extension_;
2795 class ThisFunction final : public Expression {
2797 DECLARE_NODE_TYPE(ThisFunction)
2800 ThisFunction(Zone* zone, int pos) : Expression(zone, pos) {}
2804 class SuperPropertyReference final : public Expression {
2806 DECLARE_NODE_TYPE(SuperPropertyReference)
2808 VariableProxy* this_var() const { return this_var_; }
2809 Expression* home_object() const { return home_object_; }
2812 SuperPropertyReference(Zone* zone, VariableProxy* this_var,
2813 Expression* home_object, int pos)
2814 : Expression(zone, pos), this_var_(this_var), home_object_(home_object) {
2815 DCHECK(this_var->is_this());
2816 DCHECK(home_object->IsProperty());
2820 VariableProxy* this_var_;
2821 Expression* home_object_;
2825 class SuperCallReference final : public Expression {
2827 DECLARE_NODE_TYPE(SuperCallReference)
2829 VariableProxy* this_var() const { return this_var_; }
2830 VariableProxy* new_target_var() const { return new_target_var_; }
2831 VariableProxy* this_function_var() const { return this_function_var_; }
2834 SuperCallReference(Zone* zone, VariableProxy* this_var,
2835 VariableProxy* new_target_var,
2836 VariableProxy* this_function_var, int pos)
2837 : Expression(zone, pos),
2838 this_var_(this_var),
2839 new_target_var_(new_target_var),
2840 this_function_var_(this_function_var) {
2841 DCHECK(this_var->is_this());
2842 DCHECK(new_target_var->raw_name()->IsOneByteEqualTo(".new.target"));
2843 DCHECK(this_function_var->raw_name()->IsOneByteEqualTo(".this_function"));
2847 VariableProxy* this_var_;
2848 VariableProxy* new_target_var_;
2849 VariableProxy* this_function_var_;
2853 // This class is produced when parsing the () in arrow functions without any
2854 // arguments and is not actually a valid expression.
2855 class EmptyParentheses final : public Expression {
2857 DECLARE_NODE_TYPE(EmptyParentheses)
2860 EmptyParentheses(Zone* zone, int pos) : Expression(zone, pos) {}
2864 #undef DECLARE_NODE_TYPE
2867 // ----------------------------------------------------------------------------
2868 // Regular expressions
2871 class RegExpVisitor BASE_EMBEDDED {
2873 virtual ~RegExpVisitor() { }
2874 #define MAKE_CASE(Name) \
2875 virtual void* Visit##Name(RegExp##Name*, void* data) = 0;
2876 FOR_EACH_REG_EXP_TREE_TYPE(MAKE_CASE)
2881 class RegExpTree : public ZoneObject {
2883 static const int kInfinity = kMaxInt;
2884 virtual ~RegExpTree() {}
2885 virtual void* Accept(RegExpVisitor* visitor, void* data) = 0;
2886 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
2887 RegExpNode* on_success) = 0;
2888 virtual bool IsTextElement() { return false; }
2889 virtual bool IsAnchoredAtStart() { return false; }
2890 virtual bool IsAnchoredAtEnd() { return false; }
2891 virtual int min_match() = 0;
2892 virtual int max_match() = 0;
2893 // Returns the interval of registers used for captures within this
2895 virtual Interval CaptureRegisters() { return Interval::Empty(); }
2896 virtual void AppendToText(RegExpText* text, Zone* zone);
2897 std::ostream& Print(std::ostream& os, Zone* zone); // NOLINT
2898 #define MAKE_ASTYPE(Name) \
2899 virtual RegExp##Name* As##Name(); \
2900 virtual bool Is##Name();
2901 FOR_EACH_REG_EXP_TREE_TYPE(MAKE_ASTYPE)
2906 class RegExpDisjunction final : public RegExpTree {
2908 explicit RegExpDisjunction(ZoneList<RegExpTree*>* alternatives);
2909 void* Accept(RegExpVisitor* visitor, void* data) override;
2910 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
2911 RegExpNode* on_success) override;
2912 RegExpDisjunction* AsDisjunction() override;
2913 Interval CaptureRegisters() override;
2914 bool IsDisjunction() override;
2915 bool IsAnchoredAtStart() override;
2916 bool IsAnchoredAtEnd() override;
2917 int min_match() override { return min_match_; }
2918 int max_match() override { return max_match_; }
2919 ZoneList<RegExpTree*>* alternatives() { return alternatives_; }
2921 bool SortConsecutiveAtoms(RegExpCompiler* compiler);
2922 void RationalizeConsecutiveAtoms(RegExpCompiler* compiler);
2923 void FixSingleCharacterDisjunctions(RegExpCompiler* compiler);
2924 ZoneList<RegExpTree*>* alternatives_;
2930 class RegExpAlternative final : public RegExpTree {
2932 explicit RegExpAlternative(ZoneList<RegExpTree*>* nodes);
2933 void* Accept(RegExpVisitor* visitor, void* data) override;
2934 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
2935 RegExpNode* on_success) override;
2936 RegExpAlternative* AsAlternative() override;
2937 Interval CaptureRegisters() override;
2938 bool IsAlternative() override;
2939 bool IsAnchoredAtStart() override;
2940 bool IsAnchoredAtEnd() override;
2941 int min_match() override { return min_match_; }
2942 int max_match() override { return max_match_; }
2943 ZoneList<RegExpTree*>* nodes() { return nodes_; }
2945 ZoneList<RegExpTree*>* nodes_;
2951 class RegExpAssertion final : public RegExpTree {
2953 enum AssertionType {
2961 explicit RegExpAssertion(AssertionType type) : assertion_type_(type) { }
2962 void* Accept(RegExpVisitor* visitor, void* data) override;
2963 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
2964 RegExpNode* on_success) override;
2965 RegExpAssertion* AsAssertion() override;
2966 bool IsAssertion() override;
2967 bool IsAnchoredAtStart() override;
2968 bool IsAnchoredAtEnd() override;
2969 int min_match() override { return 0; }
2970 int max_match() override { return 0; }
2971 AssertionType assertion_type() { return assertion_type_; }
2973 AssertionType assertion_type_;
2977 class CharacterSet final BASE_EMBEDDED {
2979 explicit CharacterSet(uc16 standard_set_type)
2981 standard_set_type_(standard_set_type) {}
2982 explicit CharacterSet(ZoneList<CharacterRange>* ranges)
2984 standard_set_type_(0) {}
2985 ZoneList<CharacterRange>* ranges(Zone* zone);
2986 uc16 standard_set_type() { return standard_set_type_; }
2987 void set_standard_set_type(uc16 special_set_type) {
2988 standard_set_type_ = special_set_type;
2990 bool is_standard() { return standard_set_type_ != 0; }
2991 void Canonicalize();
2993 ZoneList<CharacterRange>* ranges_;
2994 // If non-zero, the value represents a standard set (e.g., all whitespace
2995 // characters) without having to expand the ranges.
2996 uc16 standard_set_type_;
3000 class RegExpCharacterClass final : public RegExpTree {
3002 RegExpCharacterClass(ZoneList<CharacterRange>* ranges, bool is_negated)
3004 is_negated_(is_negated) { }
3005 explicit RegExpCharacterClass(uc16 type)
3007 is_negated_(false) { }
3008 void* Accept(RegExpVisitor* visitor, void* data) override;
3009 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
3010 RegExpNode* on_success) override;
3011 RegExpCharacterClass* AsCharacterClass() override;
3012 bool IsCharacterClass() override;
3013 bool IsTextElement() override { return true; }
3014 int min_match() override { return 1; }
3015 int max_match() override { return 1; }
3016 void AppendToText(RegExpText* text, Zone* zone) override;
3017 CharacterSet character_set() { return set_; }
3018 // TODO(lrn): Remove need for complex version if is_standard that
3019 // recognizes a mangled standard set and just do { return set_.is_special(); }
3020 bool is_standard(Zone* zone);
3021 // Returns a value representing the standard character set if is_standard()
3023 // Currently used values are:
3024 // s : unicode whitespace
3025 // S : unicode non-whitespace
3026 // w : ASCII word character (digit, letter, underscore)
3027 // W : non-ASCII word character
3029 // D : non-ASCII digit
3030 // . : non-unicode non-newline
3031 // * : All characters
3032 uc16 standard_type() { return set_.standard_set_type(); }
3033 ZoneList<CharacterRange>* ranges(Zone* zone) { return set_.ranges(zone); }
3034 bool is_negated() { return is_negated_; }
3042 class RegExpAtom final : public RegExpTree {
3044 explicit RegExpAtom(Vector<const uc16> data) : data_(data) { }
3045 void* Accept(RegExpVisitor* visitor, void* data) override;
3046 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
3047 RegExpNode* on_success) override;
3048 RegExpAtom* AsAtom() override;
3049 bool IsAtom() override;
3050 bool IsTextElement() override { return true; }
3051 int min_match() override { return data_.length(); }
3052 int max_match() override { return data_.length(); }
3053 void AppendToText(RegExpText* text, Zone* zone) override;
3054 Vector<const uc16> data() { return data_; }
3055 int length() { return data_.length(); }
3057 Vector<const uc16> data_;
3061 class RegExpText final : public RegExpTree {
3063 explicit RegExpText(Zone* zone) : elements_(2, zone), length_(0) {}
3064 void* Accept(RegExpVisitor* visitor, void* data) override;
3065 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
3066 RegExpNode* on_success) override;
3067 RegExpText* AsText() override;
3068 bool IsText() override;
3069 bool IsTextElement() override { return true; }
3070 int min_match() override { return length_; }
3071 int max_match() override { return length_; }
3072 void AppendToText(RegExpText* text, Zone* zone) override;
3073 void AddElement(TextElement elm, Zone* zone) {
3074 elements_.Add(elm, zone);
3075 length_ += elm.length();
3077 ZoneList<TextElement>* elements() { return &elements_; }
3079 ZoneList<TextElement> elements_;
3084 class RegExpQuantifier final : public RegExpTree {
3086 enum QuantifierType { GREEDY, NON_GREEDY, POSSESSIVE };
3087 RegExpQuantifier(int min, int max, QuantifierType type, RegExpTree* body)
3091 min_match_(min * body->min_match()),
3092 quantifier_type_(type) {
3093 if (max > 0 && body->max_match() > kInfinity / max) {
3094 max_match_ = kInfinity;
3096 max_match_ = max * body->max_match();
3099 void* Accept(RegExpVisitor* visitor, void* data) override;
3100 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
3101 RegExpNode* on_success) override;
3102 static RegExpNode* ToNode(int min,
3106 RegExpCompiler* compiler,
3107 RegExpNode* on_success,
3108 bool not_at_start = false);
3109 RegExpQuantifier* AsQuantifier() override;
3110 Interval CaptureRegisters() override;
3111 bool IsQuantifier() override;
3112 int min_match() override { return min_match_; }
3113 int max_match() override { return max_match_; }
3114 int min() { return min_; }
3115 int max() { return max_; }
3116 bool is_possessive() { return quantifier_type_ == POSSESSIVE; }
3117 bool is_non_greedy() { return quantifier_type_ == NON_GREEDY; }
3118 bool is_greedy() { return quantifier_type_ == GREEDY; }
3119 RegExpTree* body() { return body_; }
3127 QuantifierType quantifier_type_;
3131 class RegExpCapture final : public RegExpTree {
3133 explicit RegExpCapture(RegExpTree* body, int index)
3134 : body_(body), index_(index) { }
3135 void* Accept(RegExpVisitor* visitor, void* data) override;
3136 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
3137 RegExpNode* on_success) override;
3138 static RegExpNode* ToNode(RegExpTree* body,
3140 RegExpCompiler* compiler,
3141 RegExpNode* on_success);
3142 RegExpCapture* AsCapture() override;
3143 bool IsAnchoredAtStart() override;
3144 bool IsAnchoredAtEnd() override;
3145 Interval CaptureRegisters() override;
3146 bool IsCapture() override;
3147 int min_match() override { return body_->min_match(); }
3148 int max_match() override { return body_->max_match(); }
3149 RegExpTree* body() { return body_; }
3150 int index() { return index_; }
3151 static int StartRegister(int index) { return index * 2; }
3152 static int EndRegister(int index) { return index * 2 + 1; }
3160 class RegExpLookahead final : public RegExpTree {
3162 RegExpLookahead(RegExpTree* body,
3167 is_positive_(is_positive),
3168 capture_count_(capture_count),
3169 capture_from_(capture_from) { }
3171 void* Accept(RegExpVisitor* visitor, void* data) override;
3172 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
3173 RegExpNode* on_success) override;
3174 RegExpLookahead* AsLookahead() override;
3175 Interval CaptureRegisters() override;
3176 bool IsLookahead() override;
3177 bool IsAnchoredAtStart() override;
3178 int min_match() override { return 0; }
3179 int max_match() override { return 0; }
3180 RegExpTree* body() { return body_; }
3181 bool is_positive() { return is_positive_; }
3182 int capture_count() { return capture_count_; }
3183 int capture_from() { return capture_from_; }
3193 class RegExpBackReference final : public RegExpTree {
3195 explicit RegExpBackReference(RegExpCapture* capture)
3196 : capture_(capture) { }
3197 void* Accept(RegExpVisitor* visitor, void* data) override;
3198 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
3199 RegExpNode* on_success) override;
3200 RegExpBackReference* AsBackReference() override;
3201 bool IsBackReference() override;
3202 int min_match() override { return 0; }
3203 int max_match() override { return capture_->max_match(); }
3204 int index() { return capture_->index(); }
3205 RegExpCapture* capture() { return capture_; }
3207 RegExpCapture* capture_;
3211 class RegExpEmpty final : public RegExpTree {
3214 void* Accept(RegExpVisitor* visitor, void* data) override;
3215 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
3216 RegExpNode* on_success) override;
3217 RegExpEmpty* AsEmpty() override;
3218 bool IsEmpty() override;
3219 int min_match() override { return 0; }
3220 int max_match() override { return 0; }
3224 // ----------------------------------------------------------------------------
3226 // - leaf node visitors are abstract.
3228 class AstVisitor BASE_EMBEDDED {
3231 virtual ~AstVisitor() {}
3233 // Stack overflow check and dynamic dispatch.
3234 virtual void Visit(AstNode* node) = 0;
3236 // Iteration left-to-right.
3237 virtual void VisitDeclarations(ZoneList<Declaration*>* declarations);
3238 virtual void VisitStatements(ZoneList<Statement*>* statements);
3239 virtual void VisitExpressions(ZoneList<Expression*>* expressions);
3241 // Individual AST nodes.
3242 #define DEF_VISIT(type) \
3243 virtual void Visit##type(type* node) = 0;
3244 AST_NODE_LIST(DEF_VISIT)
3249 #define DEFINE_AST_VISITOR_SUBCLASS_MEMBERS() \
3251 void Visit(AstNode* node) final { \
3252 if (!CheckStackOverflow()) node->Accept(this); \
3255 void SetStackOverflow() { stack_overflow_ = true; } \
3256 void ClearStackOverflow() { stack_overflow_ = false; } \
3257 bool HasStackOverflow() const { return stack_overflow_; } \
3259 bool CheckStackOverflow() { \
3260 if (stack_overflow_) return true; \
3261 StackLimitCheck check(isolate_); \
3262 if (!check.HasOverflowed()) return false; \
3263 stack_overflow_ = true; \
3268 void InitializeAstVisitor(Isolate* isolate, Zone* zone) { \
3269 isolate_ = isolate; \
3271 stack_overflow_ = false; \
3273 Zone* zone() { return zone_; } \
3274 Isolate* isolate() { return isolate_; } \
3276 Isolate* isolate_; \
3278 bool stack_overflow_
3281 // ----------------------------------------------------------------------------
3284 class AstNodeFactory final BASE_EMBEDDED {
3286 explicit AstNodeFactory(AstValueFactory* ast_value_factory)
3287 : local_zone_(ast_value_factory->zone()),
3288 parser_zone_(ast_value_factory->zone()),
3289 ast_value_factory_(ast_value_factory) {}
3291 VariableDeclaration* NewVariableDeclaration(
3292 VariableProxy* proxy, VariableMode mode, Scope* scope, int pos,
3293 bool is_class_declaration = false, int declaration_group_start = -1) {
3294 return new (parser_zone_)
3295 VariableDeclaration(parser_zone_, proxy, mode, scope, pos,
3296 is_class_declaration, declaration_group_start);
3299 FunctionDeclaration* NewFunctionDeclaration(VariableProxy* proxy,
3301 FunctionLiteral* fun,
3304 return new (parser_zone_)
3305 FunctionDeclaration(parser_zone_, proxy, mode, fun, scope, pos);
3308 ImportDeclaration* NewImportDeclaration(VariableProxy* proxy,
3309 const AstRawString* import_name,
3310 const AstRawString* module_specifier,
3311 Scope* scope, int pos) {
3312 return new (parser_zone_) ImportDeclaration(
3313 parser_zone_, proxy, import_name, module_specifier, scope, pos);
3316 ExportDeclaration* NewExportDeclaration(VariableProxy* proxy,
3319 return new (parser_zone_)
3320 ExportDeclaration(parser_zone_, proxy, scope, pos);
3323 Block* NewBlock(ZoneList<const AstRawString*>* labels, int capacity,
3324 bool ignore_completion_value, int pos) {
3325 return new (local_zone_)
3326 Block(local_zone_, labels, capacity, ignore_completion_value, pos);
3329 #define STATEMENT_WITH_LABELS(NodeType) \
3330 NodeType* New##NodeType(ZoneList<const AstRawString*>* labels, int pos) { \
3331 return new (local_zone_) NodeType(local_zone_, labels, pos); \
3333 STATEMENT_WITH_LABELS(DoWhileStatement)
3334 STATEMENT_WITH_LABELS(WhileStatement)
3335 STATEMENT_WITH_LABELS(ForStatement)
3336 STATEMENT_WITH_LABELS(SwitchStatement)
3337 #undef STATEMENT_WITH_LABELS
3339 ForEachStatement* NewForEachStatement(ForEachStatement::VisitMode visit_mode,
3340 ZoneList<const AstRawString*>* labels,
3342 switch (visit_mode) {
3343 case ForEachStatement::ENUMERATE: {
3344 return new (local_zone_) ForInStatement(local_zone_, labels, pos);
3346 case ForEachStatement::ITERATE: {
3347 return new (local_zone_) ForOfStatement(local_zone_, labels, pos);
3354 ExpressionStatement* NewExpressionStatement(Expression* expression, int pos) {
3355 return new (local_zone_) ExpressionStatement(local_zone_, expression, pos);
3358 ContinueStatement* NewContinueStatement(IterationStatement* target, int pos) {
3359 return new (local_zone_) ContinueStatement(local_zone_, target, pos);
3362 BreakStatement* NewBreakStatement(BreakableStatement* target, int pos) {
3363 return new (local_zone_) BreakStatement(local_zone_, target, pos);
3366 ReturnStatement* NewReturnStatement(Expression* expression, int pos) {
3367 return new (local_zone_) ReturnStatement(local_zone_, expression, pos);
3370 WithStatement* NewWithStatement(Scope* scope,
3371 Expression* expression,
3372 Statement* statement,
3374 return new (local_zone_)
3375 WithStatement(local_zone_, scope, expression, statement, pos);
3378 IfStatement* NewIfStatement(Expression* condition,
3379 Statement* then_statement,
3380 Statement* else_statement,
3382 return new (local_zone_) IfStatement(local_zone_, condition, then_statement,
3383 else_statement, pos);
3386 TryCatchStatement* NewTryCatchStatement(Block* try_block, Scope* scope,
3388 Block* catch_block, int pos) {
3389 return new (local_zone_) TryCatchStatement(local_zone_, try_block, scope,
3390 variable, catch_block, pos);
3393 TryFinallyStatement* NewTryFinallyStatement(Block* try_block,
3394 Block* finally_block, int pos) {
3395 return new (local_zone_)
3396 TryFinallyStatement(local_zone_, try_block, finally_block, pos);
3399 DebuggerStatement* NewDebuggerStatement(int pos) {
3400 return new (local_zone_) DebuggerStatement(local_zone_, pos);
3403 EmptyStatement* NewEmptyStatement(int pos) {
3404 return new (local_zone_) EmptyStatement(local_zone_, pos);
3407 CaseClause* NewCaseClause(
3408 Expression* label, ZoneList<Statement*>* statements, int pos) {
3409 return new (local_zone_) CaseClause(local_zone_, label, statements, pos);
3412 Literal* NewStringLiteral(const AstRawString* string, int pos) {
3413 return new (local_zone_)
3414 Literal(local_zone_, ast_value_factory_->NewString(string), pos);
3417 // A JavaScript symbol (ECMA-262 edition 6).
3418 Literal* NewSymbolLiteral(const char* name, int pos) {
3419 return new (local_zone_)
3420 Literal(local_zone_, ast_value_factory_->NewSymbol(name), pos);
3423 Literal* NewNumberLiteral(double number, int pos, bool with_dot = false) {
3424 return new (local_zone_) Literal(
3425 local_zone_, ast_value_factory_->NewNumber(number, with_dot), pos);
3428 Literal* NewSmiLiteral(int number, int pos) {
3429 return new (local_zone_)
3430 Literal(local_zone_, ast_value_factory_->NewSmi(number), pos);
3433 Literal* NewBooleanLiteral(bool b, int pos) {
3434 return new (local_zone_)
3435 Literal(local_zone_, ast_value_factory_->NewBoolean(b), pos);
3438 Literal* NewNullLiteral(int pos) {
3439 return new (local_zone_)
3440 Literal(local_zone_, ast_value_factory_->NewNull(), pos);
3443 Literal* NewUndefinedLiteral(int pos) {
3444 return new (local_zone_)
3445 Literal(local_zone_, ast_value_factory_->NewUndefined(), pos);
3448 Literal* NewTheHoleLiteral(int pos) {
3449 return new (local_zone_)
3450 Literal(local_zone_, ast_value_factory_->NewTheHole(), pos);
3453 ObjectLiteral* NewObjectLiteral(
3454 ZoneList<ObjectLiteral::Property*>* properties,
3456 int boilerplate_properties,
3460 return new (local_zone_)
3461 ObjectLiteral(local_zone_, properties, literal_index,
3462 boilerplate_properties, has_function, is_strong, pos);
3465 ObjectLiteral::Property* NewObjectLiteralProperty(
3466 Expression* key, Expression* value, ObjectLiteralProperty::Kind kind,
3467 bool is_static, bool is_computed_name) {
3468 return new (local_zone_)
3469 ObjectLiteral::Property(key, value, kind, is_static, is_computed_name);
3472 ObjectLiteral::Property* NewObjectLiteralProperty(Expression* key,
3475 bool is_computed_name) {
3476 return new (local_zone_) ObjectLiteral::Property(
3477 ast_value_factory_, key, value, is_static, is_computed_name);
3480 RegExpLiteral* NewRegExpLiteral(const AstRawString* pattern,
3481 const AstRawString* flags,
3485 return new (local_zone_) RegExpLiteral(local_zone_, pattern, flags,
3486 literal_index, is_strong, pos);
3489 ArrayLiteral* NewArrayLiteral(ZoneList<Expression*>* values,
3493 return new (local_zone_)
3494 ArrayLiteral(local_zone_, values, -1, literal_index, is_strong, pos);
3497 ArrayLiteral* NewArrayLiteral(ZoneList<Expression*>* values,
3498 int first_spread_index, int literal_index,
3499 bool is_strong, int pos) {
3500 return new (local_zone_) ArrayLiteral(
3501 local_zone_, values, first_spread_index, literal_index, is_strong, pos);
3504 VariableProxy* NewVariableProxy(Variable* var,
3505 int start_position = RelocInfo::kNoPosition,
3506 int end_position = RelocInfo::kNoPosition) {
3507 return new (parser_zone_)
3508 VariableProxy(parser_zone_, var, start_position, end_position);
3511 VariableProxy* NewVariableProxy(const AstRawString* name,
3512 Variable::Kind variable_kind,
3513 int start_position = RelocInfo::kNoPosition,
3514 int end_position = RelocInfo::kNoPosition) {
3515 DCHECK_NOT_NULL(name);
3516 return new (parser_zone_) VariableProxy(parser_zone_, name, variable_kind,
3517 start_position, end_position);
3520 Property* NewProperty(Expression* obj, Expression* key, int pos) {
3521 return new (local_zone_) Property(local_zone_, obj, key, pos);
3524 Call* NewCall(Expression* expression,
3525 ZoneList<Expression*>* arguments,
3527 return new (local_zone_) Call(local_zone_, expression, arguments, pos);
3530 CallNew* NewCallNew(Expression* expression,
3531 ZoneList<Expression*>* arguments,
3533 return new (local_zone_) CallNew(local_zone_, expression, arguments, pos);
3536 CallRuntime* NewCallRuntime(Runtime::FunctionId id,
3537 ZoneList<Expression*>* arguments, int pos) {
3538 return new (local_zone_)
3539 CallRuntime(local_zone_, Runtime::FunctionForId(id), arguments, pos);
3542 CallRuntime* NewCallRuntime(const Runtime::Function* function,
3543 ZoneList<Expression*>* arguments, int pos) {
3544 return new (local_zone_) CallRuntime(local_zone_, function, arguments, pos);
3547 CallRuntime* NewCallRuntime(int context_index,
3548 ZoneList<Expression*>* arguments, int pos) {
3549 return new (local_zone_)
3550 CallRuntime(local_zone_, context_index, arguments, pos);
3553 UnaryOperation* NewUnaryOperation(Token::Value op,
3554 Expression* expression,
3556 return new (local_zone_) UnaryOperation(local_zone_, op, expression, pos);
3559 BinaryOperation* NewBinaryOperation(Token::Value op,
3563 return new (local_zone_) BinaryOperation(local_zone_, op, left, right, pos);
3566 CountOperation* NewCountOperation(Token::Value op,
3570 return new (local_zone_)
3571 CountOperation(local_zone_, op, is_prefix, expr, pos);
3574 CompareOperation* NewCompareOperation(Token::Value op,
3578 return new (local_zone_)
3579 CompareOperation(local_zone_, op, left, right, pos);
3582 Spread* NewSpread(Expression* expression, int pos) {
3583 return new (local_zone_) Spread(local_zone_, expression, pos);
3586 Conditional* NewConditional(Expression* condition,
3587 Expression* then_expression,
3588 Expression* else_expression,
3590 return new (local_zone_) Conditional(
3591 local_zone_, condition, then_expression, else_expression, position);
3594 Assignment* NewAssignment(Token::Value op,
3598 DCHECK(Token::IsAssignmentOp(op));
3599 Assignment* assign =
3600 new (local_zone_) Assignment(local_zone_, op, target, value, pos);
3601 if (assign->is_compound()) {
3602 DCHECK(Token::IsAssignmentOp(op));
3603 assign->binary_operation_ =
3604 NewBinaryOperation(assign->binary_op(), target, value, pos + 1);
3609 Yield* NewYield(Expression *generator_object,
3610 Expression* expression,
3611 Yield::Kind yield_kind,
3613 if (!expression) expression = NewUndefinedLiteral(pos);
3614 return new (local_zone_)
3615 Yield(local_zone_, generator_object, expression, yield_kind, pos);
3618 Throw* NewThrow(Expression* exception, int pos) {
3619 return new (local_zone_) Throw(local_zone_, exception, pos);
3622 FunctionLiteral* NewFunctionLiteral(
3623 const AstRawString* name, AstValueFactory* ast_value_factory,
3624 Scope* scope, ZoneList<Statement*>* body, int materialized_literal_count,
3625 int expected_property_count, int parameter_count,
3626 FunctionLiteral::ParameterFlag has_duplicate_parameters,
3627 FunctionLiteral::FunctionType function_type,
3628 FunctionLiteral::IsFunctionFlag is_function,
3629 FunctionLiteral::EagerCompileHint eager_compile_hint, FunctionKind kind,
3631 return new (parser_zone_) FunctionLiteral(
3632 parser_zone_, name, ast_value_factory, scope, body,
3633 materialized_literal_count, expected_property_count, parameter_count,
3634 function_type, has_duplicate_parameters, is_function,
3635 eager_compile_hint, kind, position);
3638 ClassLiteral* NewClassLiteral(const AstRawString* name, Scope* scope,
3639 VariableProxy* proxy, Expression* extends,
3640 FunctionLiteral* constructor,
3641 ZoneList<ObjectLiteral::Property*>* properties,
3642 int start_position, int end_position) {
3643 return new (parser_zone_)
3644 ClassLiteral(parser_zone_, name, scope, proxy, extends, constructor,
3645 properties, start_position, end_position);
3648 NativeFunctionLiteral* NewNativeFunctionLiteral(const AstRawString* name,
3649 v8::Extension* extension,
3651 return new (parser_zone_)
3652 NativeFunctionLiteral(parser_zone_, name, extension, pos);
3655 ThisFunction* NewThisFunction(int pos) {
3656 return new (local_zone_) ThisFunction(local_zone_, pos);
3659 SuperPropertyReference* NewSuperPropertyReference(VariableProxy* this_var,
3660 Expression* home_object,
3662 return new (parser_zone_)
3663 SuperPropertyReference(parser_zone_, this_var, home_object, pos);
3666 SuperCallReference* NewSuperCallReference(VariableProxy* this_var,
3667 VariableProxy* new_target_var,
3668 VariableProxy* this_function_var,
3670 return new (parser_zone_) SuperCallReference(
3671 parser_zone_, this_var, new_target_var, this_function_var, pos);
3674 EmptyParentheses* NewEmptyParentheses(int pos) {
3675 return new (local_zone_) EmptyParentheses(local_zone_, pos);
3678 Zone* zone() const { return local_zone_; }
3680 // Handles use of temporary zones when parsing inner function bodies.
3683 BodyScope(AstNodeFactory* factory, Zone* temp_zone, bool can_use_temp_zone)
3684 : factory_(factory), prev_zone_(factory->local_zone_) {
3685 if (can_use_temp_zone) {
3686 factory->local_zone_ = temp_zone;
3690 ~BodyScope() { factory_->local_zone_ = prev_zone_; }
3693 AstNodeFactory* factory_;
3698 // This zone may be deallocated upon returning from parsing a function body
3699 // which we can guarantee is not going to be compiled or have its AST
3701 // See ParseFunctionLiteral in parser.cc for preconditions.
3703 // ZoneObjects which need to persist until scope analysis must be allocated in
3704 // the parser-level zone.
3706 AstValueFactory* ast_value_factory_;
3710 } } // namespace v8::internal