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(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_;
1996 bool IsMonomorphic() override { return is_monomorphic_; }
1997 Handle<JSFunction> target() const { return target_; }
1998 Handle<AllocationSite> allocation_site() const {
1999 return allocation_site_;
2002 static int num_ids() { return parent_num_ids() + 1; }
2003 static int feedback_slots() { return 1; }
2004 BailoutId ReturnId() const { return BailoutId(local_id(0)); }
2006 void set_allocation_site(Handle<AllocationSite> site) {
2007 allocation_site_ = site;
2009 void set_is_monomorphic(bool monomorphic) { is_monomorphic_ = monomorphic; }
2010 void set_target(Handle<JSFunction> target) { target_ = target; }
2011 void SetKnownGlobalTarget(Handle<JSFunction> target) {
2013 is_monomorphic_ = true;
2017 CallNew(Zone* zone, Expression* expression, ZoneList<Expression*>* arguments,
2019 : Expression(zone, pos),
2020 expression_(expression),
2021 arguments_(arguments),
2022 is_monomorphic_(false),
2023 callnew_feedback_slot_(FeedbackVectorSlot::Invalid()) {}
2025 static int parent_num_ids() { return Expression::num_ids(); }
2028 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
2030 Expression* expression_;
2031 ZoneList<Expression*>* arguments_;
2032 bool is_monomorphic_;
2033 Handle<JSFunction> target_;
2034 Handle<AllocationSite> allocation_site_;
2035 FeedbackVectorSlot callnew_feedback_slot_;
2039 // The CallRuntime class does not represent any official JavaScript
2040 // language construct. Instead it is used to call a C or JS function
2041 // with a set of arguments. This is used from the builtins that are
2042 // implemented in JavaScript (see "v8natives.js").
2043 class CallRuntime final : public Expression {
2045 DECLARE_NODE_TYPE(CallRuntime)
2047 ZoneList<Expression*>* arguments() const { return arguments_; }
2048 bool is_jsruntime() const { return function_ == NULL; }
2050 int context_index() const {
2051 DCHECK(is_jsruntime());
2052 return context_index_;
2054 const Runtime::Function* function() const {
2055 DCHECK(!is_jsruntime());
2059 static int num_ids() { return parent_num_ids() + 1; }
2060 BailoutId CallId() { return BailoutId(local_id(0)); }
2062 const char* debug_name() {
2063 return is_jsruntime() ? "(context function)" : function_->name;
2067 CallRuntime(Zone* zone, const Runtime::Function* function,
2068 ZoneList<Expression*>* arguments, int pos)
2069 : Expression(zone, pos), function_(function), arguments_(arguments) {}
2071 CallRuntime(Zone* zone, int context_index, ZoneList<Expression*>* arguments,
2073 : Expression(zone, pos),
2075 context_index_(context_index),
2076 arguments_(arguments) {}
2078 static int parent_num_ids() { return Expression::num_ids(); }
2081 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
2083 const Runtime::Function* function_;
2085 ZoneList<Expression*>* arguments_;
2089 class UnaryOperation final : public Expression {
2091 DECLARE_NODE_TYPE(UnaryOperation)
2093 Token::Value op() const { return op_; }
2094 Expression* expression() const { return expression_; }
2096 // For unary not (Token::NOT), the AST ids where true and false will
2097 // actually be materialized, respectively.
2098 static int num_ids() { return parent_num_ids() + 2; }
2099 BailoutId MaterializeTrueId() const { return BailoutId(local_id(0)); }
2100 BailoutId MaterializeFalseId() const { return BailoutId(local_id(1)); }
2102 virtual void RecordToBooleanTypeFeedback(TypeFeedbackOracle* oracle) override;
2105 UnaryOperation(Zone* zone, Token::Value op, Expression* expression, int pos)
2106 : Expression(zone, pos), op_(op), expression_(expression) {
2107 DCHECK(Token::IsUnaryOp(op));
2109 static int parent_num_ids() { return Expression::num_ids(); }
2112 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
2115 Expression* expression_;
2119 class BinaryOperation final : public Expression {
2121 DECLARE_NODE_TYPE(BinaryOperation)
2123 Token::Value op() const { return static_cast<Token::Value>(op_); }
2124 Expression* left() const { return left_; }
2125 Expression* right() const { return right_; }
2126 Handle<AllocationSite> allocation_site() const { return allocation_site_; }
2127 void set_allocation_site(Handle<AllocationSite> allocation_site) {
2128 allocation_site_ = allocation_site;
2131 // The short-circuit logical operations need an AST ID for their
2132 // right-hand subexpression.
2133 static int num_ids() { return parent_num_ids() + 2; }
2134 BailoutId RightId() const { return BailoutId(local_id(0)); }
2136 TypeFeedbackId BinaryOperationFeedbackId() const {
2137 return TypeFeedbackId(local_id(1));
2139 Maybe<int> fixed_right_arg() const {
2140 return has_fixed_right_arg_ ? Just(fixed_right_arg_value_) : Nothing<int>();
2142 void set_fixed_right_arg(Maybe<int> arg) {
2143 has_fixed_right_arg_ = arg.IsJust();
2144 if (arg.IsJust()) fixed_right_arg_value_ = arg.FromJust();
2147 virtual void RecordToBooleanTypeFeedback(TypeFeedbackOracle* oracle) override;
2150 BinaryOperation(Zone* zone, Token::Value op, Expression* left,
2151 Expression* right, int pos)
2152 : Expression(zone, pos),
2153 op_(static_cast<byte>(op)),
2154 has_fixed_right_arg_(false),
2155 fixed_right_arg_value_(0),
2158 DCHECK(Token::IsBinaryOp(op));
2160 static int parent_num_ids() { return Expression::num_ids(); }
2163 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
2165 const byte op_; // actually Token::Value
2166 // TODO(rossberg): the fixed arg should probably be represented as a Constant
2167 // type for the RHS. Currenty it's actually a Maybe<int>
2168 bool has_fixed_right_arg_;
2169 int fixed_right_arg_value_;
2172 Handle<AllocationSite> allocation_site_;
2176 class CountOperation final : public Expression {
2178 DECLARE_NODE_TYPE(CountOperation)
2180 bool is_prefix() const { return IsPrefixField::decode(bit_field_); }
2181 bool is_postfix() const { return !is_prefix(); }
2183 Token::Value op() const { return TokenField::decode(bit_field_); }
2184 Token::Value binary_op() {
2185 return (op() == Token::INC) ? Token::ADD : Token::SUB;
2188 Expression* expression() const { return expression_; }
2190 bool IsMonomorphic() override { return receiver_types_.length() == 1; }
2191 SmallMapList* GetReceiverTypes() override { return &receiver_types_; }
2192 IcCheckType GetKeyType() const override {
2193 return KeyTypeField::decode(bit_field_);
2195 KeyedAccessStoreMode GetStoreMode() const override {
2196 return StoreModeField::decode(bit_field_);
2198 Type* type() const { return type_; }
2199 void set_key_type(IcCheckType type) {
2200 bit_field_ = KeyTypeField::update(bit_field_, type);
2202 void set_store_mode(KeyedAccessStoreMode mode) {
2203 bit_field_ = StoreModeField::update(bit_field_, mode);
2205 void set_type(Type* type) { type_ = type; }
2207 static int num_ids() { return parent_num_ids() + 4; }
2208 BailoutId AssignmentId() const { return BailoutId(local_id(0)); }
2209 BailoutId ToNumberId() const { return BailoutId(local_id(1)); }
2210 TypeFeedbackId CountBinOpFeedbackId() const {
2211 return TypeFeedbackId(local_id(2));
2213 TypeFeedbackId CountStoreFeedbackId() const {
2214 return TypeFeedbackId(local_id(3));
2217 FeedbackVectorRequirements ComputeFeedbackRequirements(
2218 Isolate* isolate, const ICSlotCache* cache) override;
2219 void SetFirstFeedbackICSlot(FeedbackVectorICSlot slot,
2220 ICSlotCache* cache) override {
2223 Code::Kind FeedbackICSlotKind(int index) override;
2224 FeedbackVectorICSlot CountSlot() const { return slot_; }
2227 CountOperation(Zone* zone, Token::Value op, bool is_prefix, Expression* expr,
2229 : Expression(zone, pos),
2231 IsPrefixField::encode(is_prefix) | KeyTypeField::encode(ELEMENT) |
2232 StoreModeField::encode(STANDARD_STORE) | TokenField::encode(op)),
2235 slot_(FeedbackVectorICSlot::Invalid()) {}
2236 static int parent_num_ids() { return Expression::num_ids(); }
2239 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
2241 class IsPrefixField : public BitField16<bool, 0, 1> {};
2242 class KeyTypeField : public BitField16<IcCheckType, 1, 1> {};
2243 class StoreModeField : public BitField16<KeyedAccessStoreMode, 2, 3> {};
2244 class TokenField : public BitField16<Token::Value, 5, 8> {};
2246 // Starts with 16-bit field, which should get packed together with
2247 // Expression's trailing 16-bit field.
2248 uint16_t bit_field_;
2250 Expression* expression_;
2251 SmallMapList receiver_types_;
2252 FeedbackVectorICSlot slot_;
2256 class CompareOperation final : public Expression {
2258 DECLARE_NODE_TYPE(CompareOperation)
2260 Token::Value op() const { return op_; }
2261 Expression* left() const { return left_; }
2262 Expression* right() const { return right_; }
2264 // Type feedback information.
2265 static int num_ids() { return parent_num_ids() + 1; }
2266 TypeFeedbackId CompareOperationFeedbackId() const {
2267 return TypeFeedbackId(local_id(0));
2269 Type* combined_type() const { return combined_type_; }
2270 void set_combined_type(Type* type) { combined_type_ = type; }
2272 // Match special cases.
2273 bool IsLiteralCompareTypeof(Expression** expr, Handle<String>* check);
2274 bool IsLiteralCompareUndefined(Expression** expr, Isolate* isolate);
2275 bool IsLiteralCompareNull(Expression** expr);
2278 CompareOperation(Zone* zone, Token::Value op, Expression* left,
2279 Expression* right, int pos)
2280 : Expression(zone, pos),
2284 combined_type_(Type::None(zone)) {
2285 DCHECK(Token::IsCompareOp(op));
2287 static int parent_num_ids() { return Expression::num_ids(); }
2290 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
2296 Type* combined_type_;
2300 class Spread final : public Expression {
2302 DECLARE_NODE_TYPE(Spread)
2304 Expression* expression() const { return expression_; }
2306 static int num_ids() { return parent_num_ids(); }
2309 Spread(Zone* zone, Expression* expression, int pos)
2310 : Expression(zone, pos), expression_(expression) {}
2311 static int parent_num_ids() { return Expression::num_ids(); }
2314 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
2316 Expression* expression_;
2320 class Conditional final : public Expression {
2322 DECLARE_NODE_TYPE(Conditional)
2324 Expression* condition() const { return condition_; }
2325 Expression* then_expression() const { return then_expression_; }
2326 Expression* else_expression() const { return else_expression_; }
2328 static int num_ids() { return parent_num_ids() + 2; }
2329 BailoutId ThenId() const { return BailoutId(local_id(0)); }
2330 BailoutId ElseId() const { return BailoutId(local_id(1)); }
2333 Conditional(Zone* zone, Expression* condition, Expression* then_expression,
2334 Expression* else_expression, int position)
2335 : Expression(zone, position),
2336 condition_(condition),
2337 then_expression_(then_expression),
2338 else_expression_(else_expression) {}
2339 static int parent_num_ids() { return Expression::num_ids(); }
2342 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
2344 Expression* condition_;
2345 Expression* then_expression_;
2346 Expression* else_expression_;
2350 class Assignment final : public Expression {
2352 DECLARE_NODE_TYPE(Assignment)
2354 Assignment* AsSimpleAssignment() { return !is_compound() ? this : NULL; }
2356 Token::Value binary_op() const;
2358 Token::Value op() const { return TokenField::decode(bit_field_); }
2359 Expression* target() const { return target_; }
2360 Expression* value() const { return value_; }
2361 BinaryOperation* binary_operation() const { return binary_operation_; }
2363 // This check relies on the definition order of token in token.h.
2364 bool is_compound() const { return op() > Token::ASSIGN; }
2366 static int num_ids() { return parent_num_ids() + 2; }
2367 BailoutId AssignmentId() const { return BailoutId(local_id(0)); }
2369 // Type feedback information.
2370 TypeFeedbackId AssignmentFeedbackId() { return TypeFeedbackId(local_id(1)); }
2371 bool IsMonomorphic() override { return receiver_types_.length() == 1; }
2372 bool IsUninitialized() const {
2373 return IsUninitializedField::decode(bit_field_);
2375 bool HasNoTypeInformation() {
2376 return IsUninitializedField::decode(bit_field_);
2378 SmallMapList* GetReceiverTypes() override { return &receiver_types_; }
2379 IcCheckType GetKeyType() const override {
2380 return KeyTypeField::decode(bit_field_);
2382 KeyedAccessStoreMode GetStoreMode() const override {
2383 return StoreModeField::decode(bit_field_);
2385 void set_is_uninitialized(bool b) {
2386 bit_field_ = IsUninitializedField::update(bit_field_, b);
2388 void set_key_type(IcCheckType key_type) {
2389 bit_field_ = KeyTypeField::update(bit_field_, key_type);
2391 void set_store_mode(KeyedAccessStoreMode mode) {
2392 bit_field_ = StoreModeField::update(bit_field_, mode);
2395 FeedbackVectorRequirements ComputeFeedbackRequirements(
2396 Isolate* isolate, const ICSlotCache* cache) override;
2397 void SetFirstFeedbackICSlot(FeedbackVectorICSlot slot,
2398 ICSlotCache* cache) override {
2401 Code::Kind FeedbackICSlotKind(int index) override;
2402 FeedbackVectorICSlot AssignmentSlot() const { return slot_; }
2405 Assignment(Zone* zone, Token::Value op, Expression* target, Expression* value,
2407 static int parent_num_ids() { return Expression::num_ids(); }
2410 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
2412 class IsUninitializedField : public BitField16<bool, 0, 1> {};
2413 class KeyTypeField : public BitField16<IcCheckType, 1, 1> {};
2414 class StoreModeField : public BitField16<KeyedAccessStoreMode, 2, 3> {};
2415 class TokenField : public BitField16<Token::Value, 5, 8> {};
2417 // Starts with 16-bit field, which should get packed together with
2418 // Expression's trailing 16-bit field.
2419 uint16_t bit_field_;
2420 Expression* target_;
2422 BinaryOperation* binary_operation_;
2423 SmallMapList receiver_types_;
2424 FeedbackVectorICSlot slot_;
2428 class Yield final : public Expression {
2430 DECLARE_NODE_TYPE(Yield)
2433 kInitial, // The initial yield that returns the unboxed generator object.
2434 kSuspend, // A normal yield: { value: EXPRESSION, done: false }
2435 kDelegating, // A yield*.
2436 kFinal // A return: { value: EXPRESSION, done: true }
2439 Expression* generator_object() const { return generator_object_; }
2440 Expression* expression() const { return expression_; }
2441 Kind yield_kind() const { return yield_kind_; }
2443 // Type feedback information.
2444 bool HasFeedbackSlots() const { return yield_kind() == kDelegating; }
2445 virtual FeedbackVectorRequirements ComputeFeedbackRequirements(
2446 Isolate* isolate, const ICSlotCache* cache) override {
2447 return FeedbackVectorRequirements(0, HasFeedbackSlots() ? 3 : 0);
2449 void SetFirstFeedbackICSlot(FeedbackVectorICSlot slot,
2450 ICSlotCache* cache) override {
2451 yield_first_feedback_slot_ = slot;
2453 Code::Kind FeedbackICSlotKind(int index) override {
2454 return index == 0 ? Code::KEYED_LOAD_IC : Code::LOAD_IC;
2457 FeedbackVectorICSlot KeyedLoadFeedbackSlot() {
2458 DCHECK(!HasFeedbackSlots() || !yield_first_feedback_slot_.IsInvalid());
2459 return yield_first_feedback_slot_;
2462 FeedbackVectorICSlot DoneFeedbackSlot() {
2463 return KeyedLoadFeedbackSlot().next();
2466 FeedbackVectorICSlot ValueFeedbackSlot() { return DoneFeedbackSlot().next(); }
2469 Yield(Zone* zone, Expression* generator_object, Expression* expression,
2470 Kind yield_kind, int pos)
2471 : Expression(zone, pos),
2472 generator_object_(generator_object),
2473 expression_(expression),
2474 yield_kind_(yield_kind),
2475 yield_first_feedback_slot_(FeedbackVectorICSlot::Invalid()) {}
2478 Expression* generator_object_;
2479 Expression* expression_;
2481 FeedbackVectorICSlot yield_first_feedback_slot_;
2485 class Throw final : public Expression {
2487 DECLARE_NODE_TYPE(Throw)
2489 Expression* exception() const { return exception_; }
2492 Throw(Zone* zone, Expression* exception, int pos)
2493 : Expression(zone, pos), exception_(exception) {}
2496 Expression* exception_;
2500 class FunctionLiteral final : public Expression {
2503 ANONYMOUS_EXPRESSION,
2508 enum ParameterFlag {
2509 kNoDuplicateParameters = 0,
2510 kHasDuplicateParameters = 1
2513 enum IsFunctionFlag {
2518 enum EagerCompileHint { kShouldEagerCompile, kShouldLazyCompile };
2520 enum ShouldBeUsedOnceHint { kShouldBeUsedOnce, kDontKnowIfShouldBeUsedOnce };
2522 enum ArityRestriction {
2528 DECLARE_NODE_TYPE(FunctionLiteral)
2530 Handle<String> name() const { return raw_name_->string(); }
2531 const AstRawString* raw_name() const { return raw_name_; }
2532 Scope* scope() const { return scope_; }
2533 ZoneList<Statement*>* body() const { return body_; }
2534 void set_function_token_position(int pos) { function_token_position_ = pos; }
2535 int function_token_position() const { return function_token_position_; }
2536 int start_position() const;
2537 int end_position() const;
2538 int SourceSize() const { return end_position() - start_position(); }
2539 bool is_expression() const { return IsExpression::decode(bitfield_); }
2540 bool is_anonymous() const { return IsAnonymous::decode(bitfield_); }
2541 LanguageMode language_mode() const;
2543 static bool NeedsHomeObject(Expression* expr);
2545 int materialized_literal_count() { return materialized_literal_count_; }
2546 int expected_property_count() { return expected_property_count_; }
2547 int parameter_count() { return parameter_count_; }
2549 bool AllowsLazyCompilation();
2550 bool AllowsLazyCompilationWithoutContext();
2552 Handle<String> debug_name() const {
2553 if (raw_name_ != NULL && !raw_name_->IsEmpty()) {
2554 return raw_name_->string();
2556 return inferred_name();
2559 Handle<String> inferred_name() const {
2560 if (!inferred_name_.is_null()) {
2561 DCHECK(raw_inferred_name_ == NULL);
2562 return inferred_name_;
2564 if (raw_inferred_name_ != NULL) {
2565 return raw_inferred_name_->string();
2568 return Handle<String>();
2571 // Only one of {set_inferred_name, set_raw_inferred_name} should be called.
2572 void set_inferred_name(Handle<String> inferred_name) {
2573 DCHECK(!inferred_name.is_null());
2574 inferred_name_ = inferred_name;
2575 DCHECK(raw_inferred_name_== NULL || raw_inferred_name_->IsEmpty());
2576 raw_inferred_name_ = NULL;
2579 void set_raw_inferred_name(const AstString* raw_inferred_name) {
2580 DCHECK(raw_inferred_name != NULL);
2581 raw_inferred_name_ = raw_inferred_name;
2582 DCHECK(inferred_name_.is_null());
2583 inferred_name_ = Handle<String>();
2586 bool pretenure() { return Pretenure::decode(bitfield_); }
2587 void set_pretenure() { bitfield_ |= Pretenure::encode(true); }
2589 bool has_duplicate_parameters() {
2590 return HasDuplicateParameters::decode(bitfield_);
2593 bool is_function() { return IsFunction::decode(bitfield_) == kIsFunction; }
2595 // This is used as a heuristic on when to eagerly compile a function
2596 // literal. We consider the following constructs as hints that the
2597 // function will be called immediately:
2598 // - (function() { ... })();
2599 // - var x = function() { ... }();
2600 bool should_eager_compile() const {
2601 return EagerCompileHintBit::decode(bitfield_) == kShouldEagerCompile;
2603 void set_should_eager_compile() {
2604 bitfield_ = EagerCompileHintBit::update(bitfield_, kShouldEagerCompile);
2607 // A hint that we expect this function to be called (exactly) once,
2608 // i.e. we suspect it's an initialization function.
2609 bool should_be_used_once_hint() const {
2610 return ShouldBeUsedOnceHintBit::decode(bitfield_) == kShouldBeUsedOnce;
2612 void set_should_be_used_once_hint() {
2613 bitfield_ = ShouldBeUsedOnceHintBit::update(bitfield_, kShouldBeUsedOnce);
2616 FunctionKind kind() const { return FunctionKindBits::decode(bitfield_); }
2618 int ast_node_count() { return ast_properties_.node_count(); }
2619 AstProperties::Flags flags() const { return ast_properties_.flags(); }
2620 void set_ast_properties(AstProperties* ast_properties) {
2621 ast_properties_ = *ast_properties;
2623 const ZoneFeedbackVectorSpec* feedback_vector_spec() const {
2624 return ast_properties_.get_spec();
2626 bool dont_optimize() { return dont_optimize_reason_ != kNoReason; }
2627 BailoutReason dont_optimize_reason() { return dont_optimize_reason_; }
2628 void set_dont_optimize_reason(BailoutReason reason) {
2629 dont_optimize_reason_ = reason;
2633 FunctionLiteral(Zone* zone, const AstRawString* name,
2634 AstValueFactory* ast_value_factory, Scope* scope,
2635 ZoneList<Statement*>* body, int materialized_literal_count,
2636 int expected_property_count, int parameter_count,
2637 FunctionType function_type,
2638 ParameterFlag has_duplicate_parameters,
2639 IsFunctionFlag is_function,
2640 EagerCompileHint eager_compile_hint, FunctionKind kind,
2642 : Expression(zone, position),
2646 raw_inferred_name_(ast_value_factory->empty_string()),
2647 ast_properties_(zone),
2648 dont_optimize_reason_(kNoReason),
2649 materialized_literal_count_(materialized_literal_count),
2650 expected_property_count_(expected_property_count),
2651 parameter_count_(parameter_count),
2652 function_token_position_(RelocInfo::kNoPosition) {
2653 bitfield_ = IsExpression::encode(function_type != DECLARATION) |
2654 IsAnonymous::encode(function_type == ANONYMOUS_EXPRESSION) |
2655 Pretenure::encode(false) |
2656 HasDuplicateParameters::encode(has_duplicate_parameters) |
2657 IsFunction::encode(is_function) |
2658 EagerCompileHintBit::encode(eager_compile_hint) |
2659 FunctionKindBits::encode(kind) |
2660 ShouldBeUsedOnceHintBit::encode(kDontKnowIfShouldBeUsedOnce);
2661 DCHECK(IsValidFunctionKind(kind));
2665 const AstRawString* raw_name_;
2666 Handle<String> name_;
2668 ZoneList<Statement*>* body_;
2669 const AstString* raw_inferred_name_;
2670 Handle<String> inferred_name_;
2671 AstProperties ast_properties_;
2672 BailoutReason dont_optimize_reason_;
2674 int materialized_literal_count_;
2675 int expected_property_count_;
2676 int parameter_count_;
2677 int function_token_position_;
2680 class IsExpression : public BitField<bool, 0, 1> {};
2681 class IsAnonymous : public BitField<bool, 1, 1> {};
2682 class Pretenure : public BitField<bool, 2, 1> {};
2683 class HasDuplicateParameters : public BitField<ParameterFlag, 3, 1> {};
2684 class IsFunction : public BitField<IsFunctionFlag, 4, 1> {};
2685 class EagerCompileHintBit : public BitField<EagerCompileHint, 5, 1> {};
2686 class FunctionKindBits : public BitField<FunctionKind, 6, 8> {};
2687 class ShouldBeUsedOnceHintBit : public BitField<ShouldBeUsedOnceHint, 15, 1> {
2692 class ClassLiteral final : public Expression {
2694 typedef ObjectLiteralProperty Property;
2696 DECLARE_NODE_TYPE(ClassLiteral)
2698 Handle<String> name() const { return raw_name_->string(); }
2699 const AstRawString* raw_name() const { return raw_name_; }
2700 Scope* scope() const { return scope_; }
2701 VariableProxy* class_variable_proxy() const { return class_variable_proxy_; }
2702 Expression* extends() const { return extends_; }
2703 FunctionLiteral* constructor() const { return constructor_; }
2704 ZoneList<Property*>* properties() const { return properties_; }
2705 int start_position() const { return position(); }
2706 int end_position() const { return end_position_; }
2708 BailoutId EntryId() const { return BailoutId(local_id(0)); }
2709 BailoutId DeclsId() const { return BailoutId(local_id(1)); }
2710 BailoutId ExitId() { return BailoutId(local_id(2)); }
2711 BailoutId CreateLiteralId() const { return BailoutId(local_id(3)); }
2713 // Return an AST id for a property that is used in simulate instructions.
2714 BailoutId GetIdForProperty(int i) { return BailoutId(local_id(i + 4)); }
2716 // Unlike other AST nodes, this number of bailout IDs allocated for an
2717 // ClassLiteral can vary, so num_ids() is not a static method.
2718 int num_ids() const { return parent_num_ids() + 4 + properties()->length(); }
2720 // Object literals need one feedback slot for each non-trivial value, as well
2721 // as some slots for home objects.
2722 FeedbackVectorRequirements ComputeFeedbackRequirements(
2723 Isolate* isolate, const ICSlotCache* cache) override;
2724 void SetFirstFeedbackICSlot(FeedbackVectorICSlot slot,
2725 ICSlotCache* cache) override {
2728 Code::Kind FeedbackICSlotKind(int index) override { return Code::STORE_IC; }
2730 bool NeedsProxySlot() const {
2731 return FLAG_vector_stores && scope() != NULL &&
2732 class_variable_proxy()->var()->IsUnallocated();
2735 FeedbackVectorICSlot ProxySlot() const { return slot_; }
2737 // After feedback slots were assigned, propagate information to the properties
2739 void LayoutFeedbackSlots();
2742 ClassLiteral(Zone* zone, const AstRawString* name, Scope* scope,
2743 VariableProxy* class_variable_proxy, Expression* extends,
2744 FunctionLiteral* constructor, ZoneList<Property*>* properties,
2745 int start_position, int end_position)
2746 : Expression(zone, start_position),
2749 class_variable_proxy_(class_variable_proxy),
2751 constructor_(constructor),
2752 properties_(properties),
2753 end_position_(end_position),
2754 slot_(FeedbackVectorICSlot::Invalid()) {
2757 static int parent_num_ids() { return Expression::num_ids(); }
2760 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
2762 const AstRawString* raw_name_;
2764 VariableProxy* class_variable_proxy_;
2765 Expression* extends_;
2766 FunctionLiteral* constructor_;
2767 ZoneList<Property*>* properties_;
2769 FeedbackVectorICSlot slot_;
2773 class NativeFunctionLiteral final : public Expression {
2775 DECLARE_NODE_TYPE(NativeFunctionLiteral)
2777 Handle<String> name() const { return name_->string(); }
2778 v8::Extension* extension() const { return extension_; }
2781 NativeFunctionLiteral(Zone* zone, const AstRawString* name,
2782 v8::Extension* extension, int pos)
2783 : Expression(zone, pos), name_(name), extension_(extension) {}
2786 const AstRawString* name_;
2787 v8::Extension* extension_;
2791 class ThisFunction final : public Expression {
2793 DECLARE_NODE_TYPE(ThisFunction)
2796 ThisFunction(Zone* zone, int pos) : Expression(zone, pos) {}
2800 class SuperPropertyReference final : public Expression {
2802 DECLARE_NODE_TYPE(SuperPropertyReference)
2804 VariableProxy* this_var() const { return this_var_; }
2805 Expression* home_object() const { return home_object_; }
2808 SuperPropertyReference(Zone* zone, VariableProxy* this_var,
2809 Expression* home_object, int pos)
2810 : Expression(zone, pos), this_var_(this_var), home_object_(home_object) {
2811 DCHECK(this_var->is_this());
2812 DCHECK(home_object->IsProperty());
2816 VariableProxy* this_var_;
2817 Expression* home_object_;
2821 class SuperCallReference final : public Expression {
2823 DECLARE_NODE_TYPE(SuperCallReference)
2825 VariableProxy* this_var() const { return this_var_; }
2826 VariableProxy* new_target_var() const { return new_target_var_; }
2827 VariableProxy* this_function_var() const { return this_function_var_; }
2830 SuperCallReference(Zone* zone, VariableProxy* this_var,
2831 VariableProxy* new_target_var,
2832 VariableProxy* this_function_var, int pos)
2833 : Expression(zone, pos),
2834 this_var_(this_var),
2835 new_target_var_(new_target_var),
2836 this_function_var_(this_function_var) {
2837 DCHECK(this_var->is_this());
2838 DCHECK(new_target_var->raw_name()->IsOneByteEqualTo(".new.target"));
2839 DCHECK(this_function_var->raw_name()->IsOneByteEqualTo(".this_function"));
2843 VariableProxy* this_var_;
2844 VariableProxy* new_target_var_;
2845 VariableProxy* this_function_var_;
2849 // This class is produced when parsing the () in arrow functions without any
2850 // arguments and is not actually a valid expression.
2851 class EmptyParentheses final : public Expression {
2853 DECLARE_NODE_TYPE(EmptyParentheses)
2856 EmptyParentheses(Zone* zone, int pos) : Expression(zone, pos) {}
2860 #undef DECLARE_NODE_TYPE
2863 // ----------------------------------------------------------------------------
2864 // Regular expressions
2867 class RegExpVisitor BASE_EMBEDDED {
2869 virtual ~RegExpVisitor() { }
2870 #define MAKE_CASE(Name) \
2871 virtual void* Visit##Name(RegExp##Name*, void* data) = 0;
2872 FOR_EACH_REG_EXP_TREE_TYPE(MAKE_CASE)
2877 class RegExpTree : public ZoneObject {
2879 static const int kInfinity = kMaxInt;
2880 virtual ~RegExpTree() {}
2881 virtual void* Accept(RegExpVisitor* visitor, void* data) = 0;
2882 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
2883 RegExpNode* on_success) = 0;
2884 virtual bool IsTextElement() { return false; }
2885 virtual bool IsAnchoredAtStart() { return false; }
2886 virtual bool IsAnchoredAtEnd() { return false; }
2887 virtual int min_match() = 0;
2888 virtual int max_match() = 0;
2889 // Returns the interval of registers used for captures within this
2891 virtual Interval CaptureRegisters() { return Interval::Empty(); }
2892 virtual void AppendToText(RegExpText* text, Zone* zone);
2893 std::ostream& Print(std::ostream& os, Zone* zone); // NOLINT
2894 #define MAKE_ASTYPE(Name) \
2895 virtual RegExp##Name* As##Name(); \
2896 virtual bool Is##Name();
2897 FOR_EACH_REG_EXP_TREE_TYPE(MAKE_ASTYPE)
2902 class RegExpDisjunction final : public RegExpTree {
2904 explicit RegExpDisjunction(ZoneList<RegExpTree*>* alternatives);
2905 void* Accept(RegExpVisitor* visitor, void* data) override;
2906 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
2907 RegExpNode* on_success) override;
2908 RegExpDisjunction* AsDisjunction() override;
2909 Interval CaptureRegisters() override;
2910 bool IsDisjunction() override;
2911 bool IsAnchoredAtStart() override;
2912 bool IsAnchoredAtEnd() override;
2913 int min_match() override { return min_match_; }
2914 int max_match() override { return max_match_; }
2915 ZoneList<RegExpTree*>* alternatives() { return alternatives_; }
2917 bool SortConsecutiveAtoms(RegExpCompiler* compiler);
2918 void RationalizeConsecutiveAtoms(RegExpCompiler* compiler);
2919 void FixSingleCharacterDisjunctions(RegExpCompiler* compiler);
2920 ZoneList<RegExpTree*>* alternatives_;
2926 class RegExpAlternative final : public RegExpTree {
2928 explicit RegExpAlternative(ZoneList<RegExpTree*>* nodes);
2929 void* Accept(RegExpVisitor* visitor, void* data) override;
2930 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
2931 RegExpNode* on_success) override;
2932 RegExpAlternative* AsAlternative() override;
2933 Interval CaptureRegisters() override;
2934 bool IsAlternative() override;
2935 bool IsAnchoredAtStart() override;
2936 bool IsAnchoredAtEnd() override;
2937 int min_match() override { return min_match_; }
2938 int max_match() override { return max_match_; }
2939 ZoneList<RegExpTree*>* nodes() { return nodes_; }
2941 ZoneList<RegExpTree*>* nodes_;
2947 class RegExpAssertion final : public RegExpTree {
2949 enum AssertionType {
2957 explicit RegExpAssertion(AssertionType type) : assertion_type_(type) { }
2958 void* Accept(RegExpVisitor* visitor, void* data) override;
2959 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
2960 RegExpNode* on_success) override;
2961 RegExpAssertion* AsAssertion() override;
2962 bool IsAssertion() override;
2963 bool IsAnchoredAtStart() override;
2964 bool IsAnchoredAtEnd() override;
2965 int min_match() override { return 0; }
2966 int max_match() override { return 0; }
2967 AssertionType assertion_type() { return assertion_type_; }
2969 AssertionType assertion_type_;
2973 class CharacterSet final BASE_EMBEDDED {
2975 explicit CharacterSet(uc16 standard_set_type)
2977 standard_set_type_(standard_set_type) {}
2978 explicit CharacterSet(ZoneList<CharacterRange>* ranges)
2980 standard_set_type_(0) {}
2981 ZoneList<CharacterRange>* ranges(Zone* zone);
2982 uc16 standard_set_type() { return standard_set_type_; }
2983 void set_standard_set_type(uc16 special_set_type) {
2984 standard_set_type_ = special_set_type;
2986 bool is_standard() { return standard_set_type_ != 0; }
2987 void Canonicalize();
2989 ZoneList<CharacterRange>* ranges_;
2990 // If non-zero, the value represents a standard set (e.g., all whitespace
2991 // characters) without having to expand the ranges.
2992 uc16 standard_set_type_;
2996 class RegExpCharacterClass final : public RegExpTree {
2998 RegExpCharacterClass(ZoneList<CharacterRange>* ranges, bool is_negated)
3000 is_negated_(is_negated) { }
3001 explicit RegExpCharacterClass(uc16 type)
3003 is_negated_(false) { }
3004 void* Accept(RegExpVisitor* visitor, void* data) override;
3005 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
3006 RegExpNode* on_success) override;
3007 RegExpCharacterClass* AsCharacterClass() override;
3008 bool IsCharacterClass() override;
3009 bool IsTextElement() override { return true; }
3010 int min_match() override { return 1; }
3011 int max_match() override { return 1; }
3012 void AppendToText(RegExpText* text, Zone* zone) override;
3013 CharacterSet character_set() { return set_; }
3014 // TODO(lrn): Remove need for complex version if is_standard that
3015 // recognizes a mangled standard set and just do { return set_.is_special(); }
3016 bool is_standard(Zone* zone);
3017 // Returns a value representing the standard character set if is_standard()
3019 // Currently used values are:
3020 // s : unicode whitespace
3021 // S : unicode non-whitespace
3022 // w : ASCII word character (digit, letter, underscore)
3023 // W : non-ASCII word character
3025 // D : non-ASCII digit
3026 // . : non-unicode non-newline
3027 // * : All characters
3028 uc16 standard_type() { return set_.standard_set_type(); }
3029 ZoneList<CharacterRange>* ranges(Zone* zone) { return set_.ranges(zone); }
3030 bool is_negated() { return is_negated_; }
3038 class RegExpAtom final : public RegExpTree {
3040 explicit RegExpAtom(Vector<const uc16> data) : data_(data) { }
3041 void* Accept(RegExpVisitor* visitor, void* data) override;
3042 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
3043 RegExpNode* on_success) override;
3044 RegExpAtom* AsAtom() override;
3045 bool IsAtom() override;
3046 bool IsTextElement() override { return true; }
3047 int min_match() override { return data_.length(); }
3048 int max_match() override { return data_.length(); }
3049 void AppendToText(RegExpText* text, Zone* zone) override;
3050 Vector<const uc16> data() { return data_; }
3051 int length() { return data_.length(); }
3053 Vector<const uc16> data_;
3057 class RegExpText final : public RegExpTree {
3059 explicit RegExpText(Zone* zone) : elements_(2, zone), length_(0) {}
3060 void* Accept(RegExpVisitor* visitor, void* data) override;
3061 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
3062 RegExpNode* on_success) override;
3063 RegExpText* AsText() override;
3064 bool IsText() override;
3065 bool IsTextElement() override { return true; }
3066 int min_match() override { return length_; }
3067 int max_match() override { return length_; }
3068 void AppendToText(RegExpText* text, Zone* zone) override;
3069 void AddElement(TextElement elm, Zone* zone) {
3070 elements_.Add(elm, zone);
3071 length_ += elm.length();
3073 ZoneList<TextElement>* elements() { return &elements_; }
3075 ZoneList<TextElement> elements_;
3080 class RegExpQuantifier final : public RegExpTree {
3082 enum QuantifierType { GREEDY, NON_GREEDY, POSSESSIVE };
3083 RegExpQuantifier(int min, int max, QuantifierType type, RegExpTree* body)
3087 min_match_(min * body->min_match()),
3088 quantifier_type_(type) {
3089 if (max > 0 && body->max_match() > kInfinity / max) {
3090 max_match_ = kInfinity;
3092 max_match_ = max * body->max_match();
3095 void* Accept(RegExpVisitor* visitor, void* data) override;
3096 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
3097 RegExpNode* on_success) override;
3098 static RegExpNode* ToNode(int min,
3102 RegExpCompiler* compiler,
3103 RegExpNode* on_success,
3104 bool not_at_start = false);
3105 RegExpQuantifier* AsQuantifier() override;
3106 Interval CaptureRegisters() override;
3107 bool IsQuantifier() override;
3108 int min_match() override { return min_match_; }
3109 int max_match() override { return max_match_; }
3110 int min() { return min_; }
3111 int max() { return max_; }
3112 bool is_possessive() { return quantifier_type_ == POSSESSIVE; }
3113 bool is_non_greedy() { return quantifier_type_ == NON_GREEDY; }
3114 bool is_greedy() { return quantifier_type_ == GREEDY; }
3115 RegExpTree* body() { return body_; }
3123 QuantifierType quantifier_type_;
3127 class RegExpCapture final : public RegExpTree {
3129 explicit RegExpCapture(RegExpTree* body, int index)
3130 : body_(body), index_(index) { }
3131 void* Accept(RegExpVisitor* visitor, void* data) override;
3132 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
3133 RegExpNode* on_success) override;
3134 static RegExpNode* ToNode(RegExpTree* body,
3136 RegExpCompiler* compiler,
3137 RegExpNode* on_success);
3138 RegExpCapture* AsCapture() override;
3139 bool IsAnchoredAtStart() override;
3140 bool IsAnchoredAtEnd() override;
3141 Interval CaptureRegisters() override;
3142 bool IsCapture() override;
3143 int min_match() override { return body_->min_match(); }
3144 int max_match() override { return body_->max_match(); }
3145 RegExpTree* body() { return body_; }
3146 int index() { return index_; }
3147 static int StartRegister(int index) { return index * 2; }
3148 static int EndRegister(int index) { return index * 2 + 1; }
3156 class RegExpLookahead final : public RegExpTree {
3158 RegExpLookahead(RegExpTree* body,
3163 is_positive_(is_positive),
3164 capture_count_(capture_count),
3165 capture_from_(capture_from) { }
3167 void* Accept(RegExpVisitor* visitor, void* data) override;
3168 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
3169 RegExpNode* on_success) override;
3170 RegExpLookahead* AsLookahead() override;
3171 Interval CaptureRegisters() override;
3172 bool IsLookahead() override;
3173 bool IsAnchoredAtStart() override;
3174 int min_match() override { return 0; }
3175 int max_match() override { return 0; }
3176 RegExpTree* body() { return body_; }
3177 bool is_positive() { return is_positive_; }
3178 int capture_count() { return capture_count_; }
3179 int capture_from() { return capture_from_; }
3189 class RegExpBackReference final : public RegExpTree {
3191 explicit RegExpBackReference(RegExpCapture* capture)
3192 : capture_(capture) { }
3193 void* Accept(RegExpVisitor* visitor, void* data) override;
3194 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
3195 RegExpNode* on_success) override;
3196 RegExpBackReference* AsBackReference() override;
3197 bool IsBackReference() override;
3198 int min_match() override { return 0; }
3199 int max_match() override { return capture_->max_match(); }
3200 int index() { return capture_->index(); }
3201 RegExpCapture* capture() { return capture_; }
3203 RegExpCapture* capture_;
3207 class RegExpEmpty final : public RegExpTree {
3210 void* Accept(RegExpVisitor* visitor, void* data) override;
3211 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
3212 RegExpNode* on_success) override;
3213 RegExpEmpty* AsEmpty() override;
3214 bool IsEmpty() override;
3215 int min_match() override { return 0; }
3216 int max_match() override { return 0; }
3220 // ----------------------------------------------------------------------------
3222 // - leaf node visitors are abstract.
3224 class AstVisitor BASE_EMBEDDED {
3227 virtual ~AstVisitor() {}
3229 // Stack overflow check and dynamic dispatch.
3230 virtual void Visit(AstNode* node) = 0;
3232 // Iteration left-to-right.
3233 virtual void VisitDeclarations(ZoneList<Declaration*>* declarations);
3234 virtual void VisitStatements(ZoneList<Statement*>* statements);
3235 virtual void VisitExpressions(ZoneList<Expression*>* expressions);
3237 // Individual AST nodes.
3238 #define DEF_VISIT(type) \
3239 virtual void Visit##type(type* node) = 0;
3240 AST_NODE_LIST(DEF_VISIT)
3245 #define DEFINE_AST_VISITOR_SUBCLASS_MEMBERS() \
3247 void Visit(AstNode* node) final { \
3248 if (!CheckStackOverflow()) node->Accept(this); \
3251 void SetStackOverflow() { stack_overflow_ = true; } \
3252 void ClearStackOverflow() { stack_overflow_ = false; } \
3253 bool HasStackOverflow() const { return stack_overflow_; } \
3255 bool CheckStackOverflow() { \
3256 if (stack_overflow_) return true; \
3257 StackLimitCheck check(isolate_); \
3258 if (!check.HasOverflowed()) return false; \
3259 stack_overflow_ = true; \
3264 void InitializeAstVisitor(Isolate* isolate, Zone* zone) { \
3265 isolate_ = isolate; \
3267 stack_overflow_ = false; \
3269 Zone* zone() { return zone_; } \
3270 Isolate* isolate() { return isolate_; } \
3272 Isolate* isolate_; \
3274 bool stack_overflow_
3277 // ----------------------------------------------------------------------------
3280 class AstNodeFactory final BASE_EMBEDDED {
3282 explicit AstNodeFactory(AstValueFactory* ast_value_factory)
3283 : local_zone_(ast_value_factory->zone()),
3284 parser_zone_(ast_value_factory->zone()),
3285 ast_value_factory_(ast_value_factory) {}
3287 VariableDeclaration* NewVariableDeclaration(
3288 VariableProxy* proxy, VariableMode mode, Scope* scope, int pos,
3289 bool is_class_declaration = false, int declaration_group_start = -1) {
3290 return new (parser_zone_)
3291 VariableDeclaration(parser_zone_, proxy, mode, scope, pos,
3292 is_class_declaration, declaration_group_start);
3295 FunctionDeclaration* NewFunctionDeclaration(VariableProxy* proxy,
3297 FunctionLiteral* fun,
3300 return new (parser_zone_)
3301 FunctionDeclaration(parser_zone_, proxy, mode, fun, scope, pos);
3304 ImportDeclaration* NewImportDeclaration(VariableProxy* proxy,
3305 const AstRawString* import_name,
3306 const AstRawString* module_specifier,
3307 Scope* scope, int pos) {
3308 return new (parser_zone_) ImportDeclaration(
3309 parser_zone_, proxy, import_name, module_specifier, scope, pos);
3312 ExportDeclaration* NewExportDeclaration(VariableProxy* proxy,
3315 return new (parser_zone_)
3316 ExportDeclaration(parser_zone_, proxy, scope, pos);
3319 Block* NewBlock(ZoneList<const AstRawString*>* labels, int capacity,
3320 bool ignore_completion_value, int pos) {
3321 return new (local_zone_)
3322 Block(local_zone_, labels, capacity, ignore_completion_value, pos);
3325 #define STATEMENT_WITH_LABELS(NodeType) \
3326 NodeType* New##NodeType(ZoneList<const AstRawString*>* labels, int pos) { \
3327 return new (local_zone_) NodeType(local_zone_, labels, pos); \
3329 STATEMENT_WITH_LABELS(DoWhileStatement)
3330 STATEMENT_WITH_LABELS(WhileStatement)
3331 STATEMENT_WITH_LABELS(ForStatement)
3332 STATEMENT_WITH_LABELS(SwitchStatement)
3333 #undef STATEMENT_WITH_LABELS
3335 ForEachStatement* NewForEachStatement(ForEachStatement::VisitMode visit_mode,
3336 ZoneList<const AstRawString*>* labels,
3338 switch (visit_mode) {
3339 case ForEachStatement::ENUMERATE: {
3340 return new (local_zone_) ForInStatement(local_zone_, labels, pos);
3342 case ForEachStatement::ITERATE: {
3343 return new (local_zone_) ForOfStatement(local_zone_, labels, pos);
3350 ExpressionStatement* NewExpressionStatement(Expression* expression, int pos) {
3351 return new (local_zone_) ExpressionStatement(local_zone_, expression, pos);
3354 ContinueStatement* NewContinueStatement(IterationStatement* target, int pos) {
3355 return new (local_zone_) ContinueStatement(local_zone_, target, pos);
3358 BreakStatement* NewBreakStatement(BreakableStatement* target, int pos) {
3359 return new (local_zone_) BreakStatement(local_zone_, target, pos);
3362 ReturnStatement* NewReturnStatement(Expression* expression, int pos) {
3363 return new (local_zone_) ReturnStatement(local_zone_, expression, pos);
3366 WithStatement* NewWithStatement(Scope* scope,
3367 Expression* expression,
3368 Statement* statement,
3370 return new (local_zone_)
3371 WithStatement(local_zone_, scope, expression, statement, pos);
3374 IfStatement* NewIfStatement(Expression* condition,
3375 Statement* then_statement,
3376 Statement* else_statement,
3378 return new (local_zone_) IfStatement(local_zone_, condition, then_statement,
3379 else_statement, pos);
3382 TryCatchStatement* NewTryCatchStatement(Block* try_block, Scope* scope,
3384 Block* catch_block, int pos) {
3385 return new (local_zone_) TryCatchStatement(local_zone_, try_block, scope,
3386 variable, catch_block, pos);
3389 TryFinallyStatement* NewTryFinallyStatement(Block* try_block,
3390 Block* finally_block, int pos) {
3391 return new (local_zone_)
3392 TryFinallyStatement(local_zone_, try_block, finally_block, pos);
3395 DebuggerStatement* NewDebuggerStatement(int pos) {
3396 return new (local_zone_) DebuggerStatement(local_zone_, pos);
3399 EmptyStatement* NewEmptyStatement(int pos) {
3400 return new (local_zone_) EmptyStatement(local_zone_, pos);
3403 CaseClause* NewCaseClause(
3404 Expression* label, ZoneList<Statement*>* statements, int pos) {
3405 return new (local_zone_) CaseClause(local_zone_, label, statements, pos);
3408 Literal* NewStringLiteral(const AstRawString* string, int pos) {
3409 return new (local_zone_)
3410 Literal(local_zone_, ast_value_factory_->NewString(string), pos);
3413 // A JavaScript symbol (ECMA-262 edition 6).
3414 Literal* NewSymbolLiteral(const char* name, int pos) {
3415 return new (local_zone_)
3416 Literal(local_zone_, ast_value_factory_->NewSymbol(name), pos);
3419 Literal* NewNumberLiteral(double number, int pos, bool with_dot = false) {
3420 return new (local_zone_) Literal(
3421 local_zone_, ast_value_factory_->NewNumber(number, with_dot), pos);
3424 Literal* NewSmiLiteral(int number, int pos) {
3425 return new (local_zone_)
3426 Literal(local_zone_, ast_value_factory_->NewSmi(number), pos);
3429 Literal* NewBooleanLiteral(bool b, int pos) {
3430 return new (local_zone_)
3431 Literal(local_zone_, ast_value_factory_->NewBoolean(b), pos);
3434 Literal* NewNullLiteral(int pos) {
3435 return new (local_zone_)
3436 Literal(local_zone_, ast_value_factory_->NewNull(), pos);
3439 Literal* NewUndefinedLiteral(int pos) {
3440 return new (local_zone_)
3441 Literal(local_zone_, ast_value_factory_->NewUndefined(), pos);
3444 Literal* NewTheHoleLiteral(int pos) {
3445 return new (local_zone_)
3446 Literal(local_zone_, ast_value_factory_->NewTheHole(), pos);
3449 ObjectLiteral* NewObjectLiteral(
3450 ZoneList<ObjectLiteral::Property*>* properties,
3452 int boilerplate_properties,
3456 return new (local_zone_)
3457 ObjectLiteral(local_zone_, properties, literal_index,
3458 boilerplate_properties, has_function, is_strong, pos);
3461 ObjectLiteral::Property* NewObjectLiteralProperty(
3462 Expression* key, Expression* value, ObjectLiteralProperty::Kind kind,
3463 bool is_static, bool is_computed_name) {
3464 return new (local_zone_)
3465 ObjectLiteral::Property(key, value, kind, is_static, is_computed_name);
3468 ObjectLiteral::Property* NewObjectLiteralProperty(Expression* key,
3471 bool is_computed_name) {
3472 return new (local_zone_) ObjectLiteral::Property(
3473 ast_value_factory_, key, value, is_static, is_computed_name);
3476 RegExpLiteral* NewRegExpLiteral(const AstRawString* pattern,
3477 const AstRawString* flags,
3481 return new (local_zone_) RegExpLiteral(local_zone_, pattern, flags,
3482 literal_index, is_strong, pos);
3485 ArrayLiteral* NewArrayLiteral(ZoneList<Expression*>* values,
3489 return new (local_zone_)
3490 ArrayLiteral(local_zone_, values, -1, literal_index, is_strong, pos);
3493 ArrayLiteral* NewArrayLiteral(ZoneList<Expression*>* values,
3494 int first_spread_index, int literal_index,
3495 bool is_strong, int pos) {
3496 return new (local_zone_) ArrayLiteral(
3497 local_zone_, values, first_spread_index, literal_index, is_strong, pos);
3500 VariableProxy* NewVariableProxy(Variable* var,
3501 int start_position = RelocInfo::kNoPosition,
3502 int end_position = RelocInfo::kNoPosition) {
3503 return new (parser_zone_)
3504 VariableProxy(parser_zone_, var, start_position, end_position);
3507 VariableProxy* NewVariableProxy(const AstRawString* name,
3508 Variable::Kind variable_kind,
3509 int start_position = RelocInfo::kNoPosition,
3510 int end_position = RelocInfo::kNoPosition) {
3511 DCHECK_NOT_NULL(name);
3512 return new (parser_zone_) VariableProxy(parser_zone_, name, variable_kind,
3513 start_position, end_position);
3516 Property* NewProperty(Expression* obj, Expression* key, int pos) {
3517 return new (local_zone_) Property(local_zone_, obj, key, pos);
3520 Call* NewCall(Expression* expression,
3521 ZoneList<Expression*>* arguments,
3523 return new (local_zone_) Call(local_zone_, expression, arguments, pos);
3526 CallNew* NewCallNew(Expression* expression,
3527 ZoneList<Expression*>* arguments,
3529 return new (local_zone_) CallNew(local_zone_, expression, arguments, pos);
3532 CallRuntime* NewCallRuntime(Runtime::FunctionId id,
3533 ZoneList<Expression*>* arguments, int pos) {
3534 return new (local_zone_)
3535 CallRuntime(local_zone_, Runtime::FunctionForId(id), arguments, pos);
3538 CallRuntime* NewCallRuntime(const Runtime::Function* function,
3539 ZoneList<Expression*>* arguments, int pos) {
3540 return new (local_zone_) CallRuntime(local_zone_, function, arguments, pos);
3543 CallRuntime* NewCallRuntime(int context_index,
3544 ZoneList<Expression*>* arguments, int pos) {
3545 return new (local_zone_)
3546 CallRuntime(local_zone_, context_index, arguments, pos);
3549 UnaryOperation* NewUnaryOperation(Token::Value op,
3550 Expression* expression,
3552 return new (local_zone_) UnaryOperation(local_zone_, op, expression, pos);
3555 BinaryOperation* NewBinaryOperation(Token::Value op,
3559 return new (local_zone_) BinaryOperation(local_zone_, op, left, right, pos);
3562 CountOperation* NewCountOperation(Token::Value op,
3566 return new (local_zone_)
3567 CountOperation(local_zone_, op, is_prefix, expr, pos);
3570 CompareOperation* NewCompareOperation(Token::Value op,
3574 return new (local_zone_)
3575 CompareOperation(local_zone_, op, left, right, pos);
3578 Spread* NewSpread(Expression* expression, int pos) {
3579 return new (local_zone_) Spread(local_zone_, expression, pos);
3582 Conditional* NewConditional(Expression* condition,
3583 Expression* then_expression,
3584 Expression* else_expression,
3586 return new (local_zone_) Conditional(
3587 local_zone_, condition, then_expression, else_expression, position);
3590 Assignment* NewAssignment(Token::Value op,
3594 DCHECK(Token::IsAssignmentOp(op));
3595 Assignment* assign =
3596 new (local_zone_) Assignment(local_zone_, op, target, value, pos);
3597 if (assign->is_compound()) {
3598 DCHECK(Token::IsAssignmentOp(op));
3599 assign->binary_operation_ =
3600 NewBinaryOperation(assign->binary_op(), target, value, pos + 1);
3605 Yield* NewYield(Expression *generator_object,
3606 Expression* expression,
3607 Yield::Kind yield_kind,
3609 if (!expression) expression = NewUndefinedLiteral(pos);
3610 return new (local_zone_)
3611 Yield(local_zone_, generator_object, expression, yield_kind, pos);
3614 Throw* NewThrow(Expression* exception, int pos) {
3615 return new (local_zone_) Throw(local_zone_, exception, pos);
3618 FunctionLiteral* NewFunctionLiteral(
3619 const AstRawString* name, AstValueFactory* ast_value_factory,
3620 Scope* scope, ZoneList<Statement*>* body, int materialized_literal_count,
3621 int expected_property_count, int parameter_count,
3622 FunctionLiteral::ParameterFlag has_duplicate_parameters,
3623 FunctionLiteral::FunctionType function_type,
3624 FunctionLiteral::IsFunctionFlag is_function,
3625 FunctionLiteral::EagerCompileHint eager_compile_hint, FunctionKind kind,
3627 return new (parser_zone_) FunctionLiteral(
3628 parser_zone_, name, ast_value_factory, scope, body,
3629 materialized_literal_count, expected_property_count, parameter_count,
3630 function_type, has_duplicate_parameters, is_function,
3631 eager_compile_hint, kind, position);
3634 ClassLiteral* NewClassLiteral(const AstRawString* name, Scope* scope,
3635 VariableProxy* proxy, Expression* extends,
3636 FunctionLiteral* constructor,
3637 ZoneList<ObjectLiteral::Property*>* properties,
3638 int start_position, int end_position) {
3639 return new (parser_zone_)
3640 ClassLiteral(parser_zone_, name, scope, proxy, extends, constructor,
3641 properties, start_position, end_position);
3644 NativeFunctionLiteral* NewNativeFunctionLiteral(const AstRawString* name,
3645 v8::Extension* extension,
3647 return new (parser_zone_)
3648 NativeFunctionLiteral(parser_zone_, name, extension, pos);
3651 ThisFunction* NewThisFunction(int pos) {
3652 return new (local_zone_) ThisFunction(local_zone_, pos);
3655 SuperPropertyReference* NewSuperPropertyReference(VariableProxy* this_var,
3656 Expression* home_object,
3658 return new (parser_zone_)
3659 SuperPropertyReference(parser_zone_, this_var, home_object, pos);
3662 SuperCallReference* NewSuperCallReference(VariableProxy* this_var,
3663 VariableProxy* new_target_var,
3664 VariableProxy* this_function_var,
3666 return new (parser_zone_) SuperCallReference(
3667 parser_zone_, this_var, new_target_var, this_function_var, pos);
3670 EmptyParentheses* NewEmptyParentheses(int pos) {
3671 return new (local_zone_) EmptyParentheses(local_zone_, pos);
3674 Zone* zone() const { return local_zone_; }
3676 // Handles use of temporary zones when parsing inner function bodies.
3679 BodyScope(AstNodeFactory* factory, Zone* temp_zone, bool can_use_temp_zone)
3680 : factory_(factory), prev_zone_(factory->local_zone_) {
3681 if (can_use_temp_zone) {
3682 factory->local_zone_ = temp_zone;
3686 ~BodyScope() { factory_->local_zone_ = prev_zone_; }
3689 AstNodeFactory* factory_;
3694 // This zone may be deallocated upon returning from parsing a function body
3695 // which we can guarantee is not going to be compiled or have its AST
3697 // See ParseFunctionLiteral in parser.cc for preconditions.
3699 // ZoneObjects which need to persist until scope analysis must be allocated in
3700 // the parser-level zone.
3702 AstValueFactory* ast_value_factory_;
3706 } } // namespace v8::internal