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) \
51 V(SloppyBlockFunctionStatement) \
53 V(ContinueStatement) \
63 V(TryCatchStatement) \
64 V(TryFinallyStatement) \
67 #define EXPRESSION_NODE_LIST(V) \
70 V(NativeFunctionLiteral) \
90 V(SuperPropertyReference) \
91 V(SuperCallReference) \
95 #define AST_NODE_LIST(V) \
96 DECLARATION_NODE_LIST(V) \
97 STATEMENT_NODE_LIST(V) \
98 EXPRESSION_NODE_LIST(V)
100 // Forward declarations
101 class AstNodeFactory;
105 class BreakableStatement;
107 class IterationStatement;
108 class MaterializedLiteral;
110 class TypeFeedbackOracle;
112 class RegExpAlternative;
113 class RegExpAssertion;
115 class RegExpBackReference;
117 class RegExpCharacterClass;
118 class RegExpCompiler;
119 class RegExpDisjunction;
121 class RegExpLookahead;
122 class RegExpQuantifier;
125 #define DEF_FORWARD_DECLARATION(type) class type;
126 AST_NODE_LIST(DEF_FORWARD_DECLARATION)
127 #undef DEF_FORWARD_DECLARATION
130 // Typedef only introduced to avoid unreadable code.
131 typedef ZoneList<Handle<String>> ZoneStringList;
132 typedef ZoneList<Handle<Object>> ZoneObjectList;
135 #define DECLARE_NODE_TYPE(type) \
136 void Accept(AstVisitor* v) override; \
137 AstNode::NodeType node_type() const final { return AstNode::k##type; } \
138 friend class AstNodeFactory;
143 explicit ICSlotCache(Zone* zone)
145 hash_map_(HashMap::PointersMatch, ZoneHashMap::kDefaultHashMapCapacity,
146 ZoneAllocationPolicy(zone)) {}
148 void Put(Variable* variable, FeedbackVectorICSlot slot) {
149 ZoneHashMap::Entry* entry = hash_map_.LookupOrInsert(
150 variable, ComputePointerHash(variable), ZoneAllocationPolicy(zone_));
151 entry->value = reinterpret_cast<void*>(slot.ToInt());
154 ZoneHashMap::Entry* Get(Variable* variable) const {
155 return hash_map_.Lookup(variable, ComputePointerHash(variable));
160 ZoneHashMap hash_map_;
164 class AstProperties final BASE_EMBEDDED {
168 kDontSelfOptimize = 1 << 0,
169 kDontCrankshaft = 1 << 1
172 typedef base::Flags<Flag> Flags;
174 explicit AstProperties(Zone* zone) : node_count_(0), spec_(zone) {}
176 Flags& flags() { return flags_; }
177 Flags flags() const { return flags_; }
178 int node_count() { return node_count_; }
179 void add_node_count(int count) { node_count_ += count; }
181 const FeedbackVectorSpec* get_spec() const { return &spec_; }
182 FeedbackVectorSpec* get_spec() { return &spec_; }
187 FeedbackVectorSpec spec_;
190 DEFINE_OPERATORS_FOR_FLAGS(AstProperties::Flags)
193 class AstNode: public ZoneObject {
195 #define DECLARE_TYPE_ENUM(type) k##type,
197 AST_NODE_LIST(DECLARE_TYPE_ENUM)
200 #undef DECLARE_TYPE_ENUM
202 void* operator new(size_t size, Zone* zone) { return zone->New(size); }
204 explicit AstNode(int position): position_(position) {}
205 virtual ~AstNode() {}
207 virtual void Accept(AstVisitor* v) = 0;
208 virtual NodeType node_type() const = 0;
209 int position() const { return position_; }
211 // Type testing & conversion functions overridden by concrete subclasses.
212 #define DECLARE_NODE_FUNCTIONS(type) \
213 bool Is##type() const { return node_type() == AstNode::k##type; } \
215 return Is##type() ? reinterpret_cast<type*>(this) : NULL; \
217 const type* As##type() const { \
218 return Is##type() ? reinterpret_cast<const type*>(this) : NULL; \
220 AST_NODE_LIST(DECLARE_NODE_FUNCTIONS)
221 #undef DECLARE_NODE_FUNCTIONS
223 virtual BreakableStatement* AsBreakableStatement() { return NULL; }
224 virtual IterationStatement* AsIterationStatement() { return NULL; }
225 virtual MaterializedLiteral* AsMaterializedLiteral() { return NULL; }
227 // The interface for feedback slots, with default no-op implementations for
228 // node types which don't actually have this. Note that this is conceptually
229 // not really nice, but multiple inheritance would introduce yet another
230 // vtable entry per node, something we don't want for space reasons.
231 virtual void AssignFeedbackVectorSlots(Isolate* isolate,
232 FeedbackVectorSpec* spec,
233 ICSlotCache* cache) {}
235 // Each ICSlot stores a kind of IC which the participating node should know.
236 virtual FeedbackVectorSlotKind FeedbackICSlotKind(int index) {
238 return FeedbackVectorSlotKind::UNUSED;
242 // Hidden to prevent accidental usage. It would have to load the
243 // current zone from the TLS.
244 void* operator new(size_t size);
246 friend class CaseClause; // Generates AST IDs.
252 class Statement : public AstNode {
254 explicit Statement(Zone* zone, int position) : AstNode(position) {}
256 bool IsEmpty() { return AsEmptyStatement() != NULL; }
257 virtual bool IsJump() const { return false; }
261 class SmallMapList final {
264 SmallMapList(int capacity, Zone* zone) : list_(capacity, zone) {}
266 void Reserve(int capacity, Zone* zone) { list_.Reserve(capacity, zone); }
267 void Clear() { list_.Clear(); }
268 void Sort() { list_.Sort(); }
270 bool is_empty() const { return list_.is_empty(); }
271 int length() const { return list_.length(); }
273 void AddMapIfMissing(Handle<Map> map, Zone* zone) {
274 if (!Map::TryUpdate(map).ToHandle(&map)) return;
275 for (int i = 0; i < length(); ++i) {
276 if (at(i).is_identical_to(map)) return;
281 void FilterForPossibleTransitions(Map* root_map) {
282 for (int i = list_.length() - 1; i >= 0; i--) {
283 if (at(i)->FindRootMap() != root_map) {
284 list_.RemoveElement(list_.at(i));
289 void Add(Handle<Map> handle, Zone* zone) {
290 list_.Add(handle.location(), zone);
293 Handle<Map> at(int i) const {
294 return Handle<Map>(list_.at(i));
297 Handle<Map> first() const { return at(0); }
298 Handle<Map> last() const { return at(length() - 1); }
301 // The list stores pointers to Map*, that is Map**, so it's GC safe.
302 SmallPointerList<Map*> list_;
304 DISALLOW_COPY_AND_ASSIGN(SmallMapList);
308 class Expression : public AstNode {
311 // Not assigned a context yet, or else will not be visited during
314 // Evaluated for its side effects.
316 // Evaluated for its value (and side effects).
318 // Evaluated for control flow (and side effects).
322 // True iff the expression is a valid reference expression.
323 virtual bool IsValidReferenceExpression() const { return false; }
325 // Helpers for ToBoolean conversion.
326 virtual bool ToBooleanIsTrue() const { return false; }
327 virtual bool ToBooleanIsFalse() const { return false; }
329 // Symbols that cannot be parsed as array indices are considered property
330 // names. We do not treat symbols that can be array indexes as property
331 // names because [] for string objects is handled only by keyed ICs.
332 virtual bool IsPropertyName() const { return false; }
334 // True iff the expression is a literal represented as a smi.
335 bool IsSmiLiteral() const;
337 // True iff the expression is a string literal.
338 bool IsStringLiteral() const;
340 // True iff the expression is the null literal.
341 bool IsNullLiteral() const;
343 // True if we can prove that the expression is the undefined literal.
344 bool IsUndefinedLiteral(Isolate* isolate) const;
346 // True iff the expression is a valid target for an assignment.
347 bool IsValidReferenceExpressionOrThis() const;
349 // Expression type bounds
350 Bounds bounds() const { return bounds_; }
351 void set_bounds(Bounds bounds) { bounds_ = bounds; }
353 // Type feedback information for assignments and properties.
354 virtual bool IsMonomorphic() {
358 virtual SmallMapList* GetReceiverTypes() {
362 virtual KeyedAccessStoreMode GetStoreMode() const {
364 return STANDARD_STORE;
366 virtual IcCheckType GetKeyType() const {
371 // TODO(rossberg): this should move to its own AST node eventually.
372 virtual void RecordToBooleanTypeFeedback(TypeFeedbackOracle* oracle);
373 uint16_t to_boolean_types() const {
374 return ToBooleanTypesField::decode(bit_field_);
377 void set_base_id(int id) { base_id_ = id; }
378 static int num_ids() { return parent_num_ids() + 2; }
379 BailoutId id() const { return BailoutId(local_id(0)); }
380 TypeFeedbackId test_id() const { return TypeFeedbackId(local_id(1)); }
383 Expression(Zone* zone, int pos)
385 base_id_(BailoutId::None().ToInt()),
386 bounds_(Bounds::Unbounded()),
388 static int parent_num_ids() { return 0; }
389 void set_to_boolean_types(uint16_t types) {
390 bit_field_ = ToBooleanTypesField::update(bit_field_, types);
393 int base_id() const {
394 DCHECK(!BailoutId(base_id_).IsNone());
399 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
403 class ToBooleanTypesField : public BitField16<uint16_t, 0, 9> {};
405 // Ends with 16-bit field; deriving classes in turn begin with
406 // 16-bit fields for optimum packing efficiency.
410 class BreakableStatement : public Statement {
413 TARGET_FOR_ANONYMOUS,
414 TARGET_FOR_NAMED_ONLY
417 // The labels associated with this statement. May be NULL;
418 // if it is != NULL, guaranteed to contain at least one entry.
419 ZoneList<const AstRawString*>* labels() const { return labels_; }
421 // Type testing & conversion.
422 BreakableStatement* AsBreakableStatement() final { return this; }
425 Label* break_target() { return &break_target_; }
428 bool is_target_for_anonymous() const {
429 return breakable_type_ == TARGET_FOR_ANONYMOUS;
432 void set_base_id(int id) { base_id_ = id; }
433 static int num_ids() { return parent_num_ids() + 2; }
434 BailoutId EntryId() const { return BailoutId(local_id(0)); }
435 BailoutId ExitId() const { return BailoutId(local_id(1)); }
438 BreakableStatement(Zone* zone, ZoneList<const AstRawString*>* labels,
439 BreakableType breakable_type, int position)
440 : Statement(zone, position),
442 breakable_type_(breakable_type),
443 base_id_(BailoutId::None().ToInt()) {
444 DCHECK(labels == NULL || labels->length() > 0);
446 static int parent_num_ids() { return 0; }
448 int base_id() const {
449 DCHECK(!BailoutId(base_id_).IsNone());
454 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
456 ZoneList<const AstRawString*>* labels_;
457 BreakableType breakable_type_;
463 class Block final : public BreakableStatement {
465 DECLARE_NODE_TYPE(Block)
467 void AddStatement(Statement* statement, Zone* zone) {
468 statements_.Add(statement, zone);
471 ZoneList<Statement*>* statements() { return &statements_; }
472 bool ignore_completion_value() const { return ignore_completion_value_; }
474 static int num_ids() { return parent_num_ids() + 1; }
475 BailoutId DeclsId() const { return BailoutId(local_id(0)); }
477 bool IsJump() const override {
478 return !statements_.is_empty() && statements_.last()->IsJump()
479 && labels() == NULL; // Good enough as an approximation...
482 Scope* scope() const { return scope_; }
483 void set_scope(Scope* scope) { scope_ = scope; }
486 Block(Zone* zone, ZoneList<const AstRawString*>* labels, int capacity,
487 bool ignore_completion_value, int pos)
488 : BreakableStatement(zone, labels, TARGET_FOR_NAMED_ONLY, pos),
489 statements_(capacity, zone),
490 ignore_completion_value_(ignore_completion_value),
492 static int parent_num_ids() { return BreakableStatement::num_ids(); }
495 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
497 ZoneList<Statement*> statements_;
498 bool ignore_completion_value_;
503 class Declaration : public AstNode {
505 VariableProxy* proxy() const { return proxy_; }
506 VariableMode mode() const { return mode_; }
507 Scope* scope() const { return scope_; }
508 virtual InitializationFlag initialization() const = 0;
509 virtual bool IsInlineable() const;
512 Declaration(Zone* zone, VariableProxy* proxy, VariableMode mode, Scope* scope,
514 : AstNode(pos), mode_(mode), proxy_(proxy), scope_(scope) {
515 DCHECK(IsDeclaredVariableMode(mode));
520 VariableProxy* proxy_;
522 // Nested scope from which the declaration originated.
527 class VariableDeclaration final : public Declaration {
529 DECLARE_NODE_TYPE(VariableDeclaration)
531 InitializationFlag initialization() const override {
532 return mode() == VAR ? kCreatedInitialized : kNeedsInitialization;
535 bool is_class_declaration() const { return is_class_declaration_; }
537 // VariableDeclarations can be grouped into consecutive declaration
538 // groups. Each VariableDeclaration is associated with the start position of
539 // the group it belongs to. The positions are used for strong mode scope
540 // checks for classes and functions.
541 int declaration_group_start() const { return declaration_group_start_; }
544 VariableDeclaration(Zone* zone, VariableProxy* proxy, VariableMode mode,
545 Scope* scope, int pos, bool is_class_declaration = false,
546 int declaration_group_start = -1)
547 : Declaration(zone, proxy, mode, scope, pos),
548 is_class_declaration_(is_class_declaration),
549 declaration_group_start_(declaration_group_start) {}
551 bool is_class_declaration_;
552 int declaration_group_start_;
556 class FunctionDeclaration final : public Declaration {
558 DECLARE_NODE_TYPE(FunctionDeclaration)
560 FunctionLiteral* fun() const { return fun_; }
561 InitializationFlag initialization() const override {
562 return kCreatedInitialized;
564 bool IsInlineable() const override;
567 FunctionDeclaration(Zone* zone,
568 VariableProxy* proxy,
570 FunctionLiteral* fun,
573 : Declaration(zone, proxy, mode, scope, pos),
575 DCHECK(mode == VAR || mode == LET || mode == CONST);
580 FunctionLiteral* fun_;
584 class ImportDeclaration final : public Declaration {
586 DECLARE_NODE_TYPE(ImportDeclaration)
588 const AstRawString* import_name() const { return import_name_; }
589 const AstRawString* module_specifier() const { return module_specifier_; }
590 void set_module_specifier(const AstRawString* module_specifier) {
591 DCHECK(module_specifier_ == NULL);
592 module_specifier_ = module_specifier;
594 InitializationFlag initialization() const override {
595 return kNeedsInitialization;
599 ImportDeclaration(Zone* zone, VariableProxy* proxy,
600 const AstRawString* import_name,
601 const AstRawString* module_specifier, Scope* scope, int pos)
602 : Declaration(zone, proxy, IMPORT, scope, pos),
603 import_name_(import_name),
604 module_specifier_(module_specifier) {}
607 const AstRawString* import_name_;
608 const AstRawString* module_specifier_;
612 class ExportDeclaration final : public Declaration {
614 DECLARE_NODE_TYPE(ExportDeclaration)
616 InitializationFlag initialization() const override {
617 return kCreatedInitialized;
621 ExportDeclaration(Zone* zone, VariableProxy* proxy, Scope* scope, int pos)
622 : Declaration(zone, proxy, LET, scope, pos) {}
626 class Module : public AstNode {
628 ModuleDescriptor* descriptor() const { return descriptor_; }
629 Block* body() const { return body_; }
632 Module(Zone* zone, int pos)
633 : AstNode(pos), descriptor_(ModuleDescriptor::New(zone)), body_(NULL) {}
634 Module(Zone* zone, ModuleDescriptor* descriptor, int pos, Block* body = NULL)
635 : AstNode(pos), descriptor_(descriptor), body_(body) {}
638 ModuleDescriptor* descriptor_;
643 class IterationStatement : public BreakableStatement {
645 // Type testing & conversion.
646 IterationStatement* AsIterationStatement() final { return this; }
648 Statement* body() const { return body_; }
650 static int num_ids() { return parent_num_ids() + 1; }
651 BailoutId OsrEntryId() const { return BailoutId(local_id(0)); }
652 virtual BailoutId ContinueId() const = 0;
653 virtual BailoutId StackCheckId() const = 0;
656 Label* continue_target() { return &continue_target_; }
659 IterationStatement(Zone* zone, ZoneList<const AstRawString*>* labels, int pos)
660 : BreakableStatement(zone, labels, TARGET_FOR_ANONYMOUS, pos),
662 static int parent_num_ids() { return BreakableStatement::num_ids(); }
663 void Initialize(Statement* body) { body_ = body; }
666 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
669 Label continue_target_;
673 class DoWhileStatement final : public IterationStatement {
675 DECLARE_NODE_TYPE(DoWhileStatement)
677 void Initialize(Expression* cond, Statement* body) {
678 IterationStatement::Initialize(body);
682 Expression* cond() const { return cond_; }
684 static int num_ids() { return parent_num_ids() + 2; }
685 BailoutId ContinueId() const override { return BailoutId(local_id(0)); }
686 BailoutId StackCheckId() const override { return BackEdgeId(); }
687 BailoutId BackEdgeId() const { return BailoutId(local_id(1)); }
690 DoWhileStatement(Zone* zone, ZoneList<const AstRawString*>* labels, int pos)
691 : IterationStatement(zone, labels, pos), cond_(NULL) {}
692 static int parent_num_ids() { return IterationStatement::num_ids(); }
695 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
701 class WhileStatement final : public IterationStatement {
703 DECLARE_NODE_TYPE(WhileStatement)
705 void Initialize(Expression* cond, Statement* body) {
706 IterationStatement::Initialize(body);
710 Expression* cond() const { return cond_; }
712 static int num_ids() { return parent_num_ids() + 1; }
713 BailoutId ContinueId() const override { return EntryId(); }
714 BailoutId StackCheckId() const override { return BodyId(); }
715 BailoutId BodyId() const { return BailoutId(local_id(0)); }
718 WhileStatement(Zone* zone, ZoneList<const AstRawString*>* labels, int pos)
719 : IterationStatement(zone, labels, pos), cond_(NULL) {}
720 static int parent_num_ids() { return IterationStatement::num_ids(); }
723 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
729 class ForStatement final : public IterationStatement {
731 DECLARE_NODE_TYPE(ForStatement)
733 void Initialize(Statement* init,
737 IterationStatement::Initialize(body);
743 Statement* init() const { return init_; }
744 Expression* cond() const { return cond_; }
745 Statement* next() const { return next_; }
747 static int num_ids() { return parent_num_ids() + 2; }
748 BailoutId ContinueId() const override { return BailoutId(local_id(0)); }
749 BailoutId StackCheckId() const override { return BodyId(); }
750 BailoutId BodyId() const { return BailoutId(local_id(1)); }
753 ForStatement(Zone* zone, ZoneList<const AstRawString*>* labels, int pos)
754 : IterationStatement(zone, labels, pos),
758 static int parent_num_ids() { return IterationStatement::num_ids(); }
761 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
769 class ForEachStatement : public IterationStatement {
772 ENUMERATE, // for (each in subject) body;
773 ITERATE // for (each of subject) body;
776 void Initialize(Expression* each, Expression* subject, Statement* body) {
777 IterationStatement::Initialize(body);
782 Expression* each() const { return each_; }
783 Expression* subject() const { return subject_; }
785 void AssignFeedbackVectorSlots(Isolate* isolate, FeedbackVectorSpec* spec,
786 ICSlotCache* cache) override;
787 FeedbackVectorICSlot EachFeedbackSlot() const { return each_slot_; }
790 ForEachStatement(Zone* zone, ZoneList<const AstRawString*>* labels, int pos)
791 : IterationStatement(zone, labels, pos),
794 each_slot_(FeedbackVectorICSlot::Invalid()) {}
798 Expression* subject_;
799 FeedbackVectorICSlot each_slot_;
803 class ForInStatement final : public ForEachStatement {
805 DECLARE_NODE_TYPE(ForInStatement)
807 Expression* enumerable() const {
811 // Type feedback information.
812 void AssignFeedbackVectorSlots(Isolate* isolate, FeedbackVectorSpec* spec,
813 ICSlotCache* cache) override {
814 ForEachStatement::AssignFeedbackVectorSlots(isolate, spec, cache);
815 for_in_feedback_slot_ = spec->AddStubSlot();
818 FeedbackVectorSlot ForInFeedbackSlot() {
819 DCHECK(!for_in_feedback_slot_.IsInvalid());
820 return for_in_feedback_slot_;
823 enum ForInType { FAST_FOR_IN, SLOW_FOR_IN };
824 ForInType for_in_type() const { return for_in_type_; }
825 void set_for_in_type(ForInType type) { for_in_type_ = type; }
827 static int num_ids() { return parent_num_ids() + 6; }
828 BailoutId BodyId() const { return BailoutId(local_id(0)); }
829 BailoutId PrepareId() const { return BailoutId(local_id(1)); }
830 BailoutId EnumId() const { return BailoutId(local_id(2)); }
831 BailoutId ToObjectId() const { return BailoutId(local_id(3)); }
832 BailoutId FilterId() const { return BailoutId(local_id(4)); }
833 BailoutId AssignmentId() const { return BailoutId(local_id(5)); }
834 BailoutId ContinueId() const override { return EntryId(); }
835 BailoutId StackCheckId() const override { return BodyId(); }
838 ForInStatement(Zone* zone, ZoneList<const AstRawString*>* labels, int pos)
839 : ForEachStatement(zone, labels, pos),
840 for_in_type_(SLOW_FOR_IN),
841 for_in_feedback_slot_(FeedbackVectorSlot::Invalid()) {}
842 static int parent_num_ids() { return ForEachStatement::num_ids(); }
845 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
847 ForInType for_in_type_;
848 FeedbackVectorSlot for_in_feedback_slot_;
852 class ForOfStatement final : public ForEachStatement {
854 DECLARE_NODE_TYPE(ForOfStatement)
856 void Initialize(Expression* each,
859 Expression* assign_iterator,
860 Expression* next_result,
861 Expression* result_done,
862 Expression* assign_each) {
863 ForEachStatement::Initialize(each, subject, body);
864 assign_iterator_ = assign_iterator;
865 next_result_ = next_result;
866 result_done_ = result_done;
867 assign_each_ = assign_each;
870 Expression* iterable() const {
874 // iterator = subject[Symbol.iterator]()
875 Expression* assign_iterator() const {
876 return assign_iterator_;
879 // result = iterator.next() // with type check
880 Expression* next_result() const {
885 Expression* result_done() const {
889 // each = result.value
890 Expression* assign_each() const {
894 BailoutId ContinueId() const override { return EntryId(); }
895 BailoutId StackCheckId() const override { return BackEdgeId(); }
897 static int num_ids() { return parent_num_ids() + 1; }
898 BailoutId BackEdgeId() const { return BailoutId(local_id(0)); }
901 ForOfStatement(Zone* zone, ZoneList<const AstRawString*>* labels, int pos)
902 : ForEachStatement(zone, labels, pos),
903 assign_iterator_(NULL),
906 assign_each_(NULL) {}
907 static int parent_num_ids() { return ForEachStatement::num_ids(); }
910 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
912 Expression* assign_iterator_;
913 Expression* next_result_;
914 Expression* result_done_;
915 Expression* assign_each_;
919 class ExpressionStatement final : public Statement {
921 DECLARE_NODE_TYPE(ExpressionStatement)
923 void set_expression(Expression* e) { expression_ = e; }
924 Expression* expression() const { return expression_; }
925 bool IsJump() const override { return expression_->IsThrow(); }
928 ExpressionStatement(Zone* zone, Expression* expression, int pos)
929 : Statement(zone, pos), expression_(expression) { }
932 Expression* expression_;
936 class JumpStatement : public Statement {
938 bool IsJump() const final { return true; }
941 explicit JumpStatement(Zone* zone, int pos) : Statement(zone, pos) {}
945 class ContinueStatement final : public JumpStatement {
947 DECLARE_NODE_TYPE(ContinueStatement)
949 IterationStatement* target() const { return target_; }
952 explicit ContinueStatement(Zone* zone, IterationStatement* target, int pos)
953 : JumpStatement(zone, pos), target_(target) { }
956 IterationStatement* target_;
960 class BreakStatement final : public JumpStatement {
962 DECLARE_NODE_TYPE(BreakStatement)
964 BreakableStatement* target() const { return target_; }
967 explicit BreakStatement(Zone* zone, BreakableStatement* target, int pos)
968 : JumpStatement(zone, pos), target_(target) { }
971 BreakableStatement* target_;
975 class ReturnStatement final : public JumpStatement {
977 DECLARE_NODE_TYPE(ReturnStatement)
979 Expression* expression() const { return expression_; }
982 explicit ReturnStatement(Zone* zone, Expression* expression, int pos)
983 : JumpStatement(zone, pos), expression_(expression) { }
986 Expression* expression_;
990 class WithStatement final : public Statement {
992 DECLARE_NODE_TYPE(WithStatement)
994 Scope* scope() { return scope_; }
995 Expression* expression() const { return expression_; }
996 Statement* statement() const { return statement_; }
998 void set_base_id(int id) { base_id_ = id; }
999 static int num_ids() { return parent_num_ids() + 1; }
1000 BailoutId EntryId() const { return BailoutId(local_id(0)); }
1003 WithStatement(Zone* zone, Scope* scope, Expression* expression,
1004 Statement* statement, int pos)
1005 : Statement(zone, pos),
1007 expression_(expression),
1008 statement_(statement),
1009 base_id_(BailoutId::None().ToInt()) {}
1010 static int parent_num_ids() { return 0; }
1012 int base_id() const {
1013 DCHECK(!BailoutId(base_id_).IsNone());
1018 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
1021 Expression* expression_;
1022 Statement* statement_;
1027 class CaseClause final : public Expression {
1029 DECLARE_NODE_TYPE(CaseClause)
1031 bool is_default() const { return label_ == NULL; }
1032 Expression* label() const {
1033 CHECK(!is_default());
1036 Label* body_target() { return &body_target_; }
1037 ZoneList<Statement*>* statements() const { return statements_; }
1039 static int num_ids() { return parent_num_ids() + 2; }
1040 BailoutId EntryId() const { return BailoutId(local_id(0)); }
1041 TypeFeedbackId CompareId() { return TypeFeedbackId(local_id(1)); }
1043 Type* compare_type() { return compare_type_; }
1044 void set_compare_type(Type* type) { compare_type_ = type; }
1047 static int parent_num_ids() { return Expression::num_ids(); }
1050 CaseClause(Zone* zone, Expression* label, ZoneList<Statement*>* statements,
1052 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
1056 ZoneList<Statement*>* statements_;
1057 Type* compare_type_;
1061 class SwitchStatement final : public BreakableStatement {
1063 DECLARE_NODE_TYPE(SwitchStatement)
1065 void Initialize(Expression* tag, ZoneList<CaseClause*>* cases) {
1070 Expression* tag() const { return tag_; }
1071 ZoneList<CaseClause*>* cases() const { return cases_; }
1074 SwitchStatement(Zone* zone, ZoneList<const AstRawString*>* labels, int pos)
1075 : BreakableStatement(zone, labels, TARGET_FOR_ANONYMOUS, pos),
1081 ZoneList<CaseClause*>* cases_;
1085 // If-statements always have non-null references to their then- and
1086 // else-parts. When parsing if-statements with no explicit else-part,
1087 // the parser implicitly creates an empty statement. Use the
1088 // HasThenStatement() and HasElseStatement() functions to check if a
1089 // given if-statement has a then- or an else-part containing code.
1090 class IfStatement final : public Statement {
1092 DECLARE_NODE_TYPE(IfStatement)
1094 bool HasThenStatement() const { return !then_statement()->IsEmpty(); }
1095 bool HasElseStatement() const { return !else_statement()->IsEmpty(); }
1097 Expression* condition() const { return condition_; }
1098 Statement* then_statement() const { return then_statement_; }
1099 Statement* else_statement() const { return else_statement_; }
1101 bool IsJump() const override {
1102 return HasThenStatement() && then_statement()->IsJump()
1103 && HasElseStatement() && else_statement()->IsJump();
1106 void set_base_id(int id) { base_id_ = id; }
1107 static int num_ids() { return parent_num_ids() + 3; }
1108 BailoutId IfId() const { return BailoutId(local_id(0)); }
1109 BailoutId ThenId() const { return BailoutId(local_id(1)); }
1110 BailoutId ElseId() const { return BailoutId(local_id(2)); }
1113 IfStatement(Zone* zone, Expression* condition, Statement* then_statement,
1114 Statement* else_statement, int pos)
1115 : Statement(zone, pos),
1116 condition_(condition),
1117 then_statement_(then_statement),
1118 else_statement_(else_statement),
1119 base_id_(BailoutId::None().ToInt()) {}
1120 static int parent_num_ids() { return 0; }
1122 int base_id() const {
1123 DCHECK(!BailoutId(base_id_).IsNone());
1128 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
1130 Expression* condition_;
1131 Statement* then_statement_;
1132 Statement* else_statement_;
1137 class TryStatement : public Statement {
1139 Block* try_block() const { return try_block_; }
1141 void set_base_id(int id) { base_id_ = id; }
1142 static int num_ids() { return parent_num_ids() + 1; }
1143 BailoutId HandlerId() const { return BailoutId(local_id(0)); }
1146 TryStatement(Zone* zone, Block* try_block, int pos)
1147 : Statement(zone, pos),
1148 try_block_(try_block),
1149 base_id_(BailoutId::None().ToInt()) {}
1150 static int parent_num_ids() { return 0; }
1152 int base_id() const {
1153 DCHECK(!BailoutId(base_id_).IsNone());
1158 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
1165 class TryCatchStatement final : public TryStatement {
1167 DECLARE_NODE_TYPE(TryCatchStatement)
1169 Scope* scope() { return scope_; }
1170 Variable* variable() { return variable_; }
1171 Block* catch_block() const { return catch_block_; }
1174 TryCatchStatement(Zone* zone, Block* try_block, Scope* scope,
1175 Variable* variable, Block* catch_block, int pos)
1176 : TryStatement(zone, try_block, pos),
1178 variable_(variable),
1179 catch_block_(catch_block) {}
1183 Variable* variable_;
1184 Block* catch_block_;
1188 class TryFinallyStatement final : public TryStatement {
1190 DECLARE_NODE_TYPE(TryFinallyStatement)
1192 Block* finally_block() const { return finally_block_; }
1195 TryFinallyStatement(Zone* zone, Block* try_block, Block* finally_block,
1197 : TryStatement(zone, try_block, pos), finally_block_(finally_block) {}
1200 Block* finally_block_;
1204 class DebuggerStatement final : public Statement {
1206 DECLARE_NODE_TYPE(DebuggerStatement)
1208 void set_base_id(int id) { base_id_ = id; }
1209 static int num_ids() { return parent_num_ids() + 1; }
1210 BailoutId DebugBreakId() const { return BailoutId(local_id(0)); }
1213 explicit DebuggerStatement(Zone* zone, int pos)
1214 : Statement(zone, pos), base_id_(BailoutId::None().ToInt()) {}
1215 static int parent_num_ids() { return 0; }
1217 int base_id() const {
1218 DCHECK(!BailoutId(base_id_).IsNone());
1223 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
1229 class EmptyStatement final : public Statement {
1231 DECLARE_NODE_TYPE(EmptyStatement)
1234 explicit EmptyStatement(Zone* zone, int pos): Statement(zone, pos) {}
1238 // Delegates to another statement, which may be overwritten.
1239 // This was introduced to implement ES2015 Annex B3.3 for conditionally making
1240 // sloppy-mode block-scoped functions have a var binding, which is changed
1241 // from one statement to another during parsing.
1242 class SloppyBlockFunctionStatement final : public Statement {
1244 DECLARE_NODE_TYPE(SloppyBlockFunctionStatement)
1246 Statement* statement() const { return statement_; }
1247 void set_statement(Statement* statement) { statement_ = statement; }
1248 Scope* scope() const { return scope_; }
1251 SloppyBlockFunctionStatement(Zone* zone, Statement* statement, Scope* scope)
1252 : Statement(zone, RelocInfo::kNoPosition),
1253 statement_(statement),
1256 Statement* statement_;
1257 Scope* const scope_;
1261 class Literal final : public Expression {
1263 DECLARE_NODE_TYPE(Literal)
1265 bool IsPropertyName() const override { return value_->IsPropertyName(); }
1267 Handle<String> AsPropertyName() {
1268 DCHECK(IsPropertyName());
1269 return Handle<String>::cast(value());
1272 const AstRawString* AsRawPropertyName() {
1273 DCHECK(IsPropertyName());
1274 return value_->AsString();
1277 bool ToBooleanIsTrue() const override { return value()->BooleanValue(); }
1278 bool ToBooleanIsFalse() const override { return !value()->BooleanValue(); }
1280 Handle<Object> value() const { return value_->value(); }
1281 const AstValue* raw_value() const { return value_; }
1283 // Support for using Literal as a HashMap key. NOTE: Currently, this works
1284 // only for string and number literals!
1286 static bool Match(void* literal1, void* literal2);
1288 static int num_ids() { return parent_num_ids() + 1; }
1289 TypeFeedbackId LiteralFeedbackId() const {
1290 return TypeFeedbackId(local_id(0));
1294 Literal(Zone* zone, const AstValue* value, int position)
1295 : Expression(zone, position), value_(value) {}
1296 static int parent_num_ids() { return Expression::num_ids(); }
1299 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
1301 const AstValue* value_;
1305 class AstLiteralReindexer;
1307 // Base class for literals that needs space in the corresponding JSFunction.
1308 class MaterializedLiteral : public Expression {
1310 virtual MaterializedLiteral* AsMaterializedLiteral() { return this; }
1312 int literal_index() { return literal_index_; }
1315 // only callable after initialization.
1316 DCHECK(depth_ >= 1);
1320 bool is_strong() const { return is_strong_; }
1323 MaterializedLiteral(Zone* zone, int literal_index, bool is_strong, int pos)
1324 : Expression(zone, pos),
1325 literal_index_(literal_index),
1327 is_strong_(is_strong),
1330 // A materialized literal is simple if the values consist of only
1331 // constants and simple object and array literals.
1332 bool is_simple() const { return is_simple_; }
1333 void set_is_simple(bool is_simple) { is_simple_ = is_simple; }
1334 friend class CompileTimeValue;
1336 void set_depth(int depth) {
1341 // Populate the constant properties/elements fixed array.
1342 void BuildConstants(Isolate* isolate);
1343 friend class ArrayLiteral;
1344 friend class ObjectLiteral;
1346 // If the expression is a literal, return the literal value;
1347 // if the expression is a materialized literal and is simple return a
1348 // compile time value as encoded by CompileTimeValue::GetValue().
1349 // Otherwise, return undefined literal as the placeholder
1350 // in the object literal boilerplate.
1351 Handle<Object> GetBoilerplateValue(Expression* expression, Isolate* isolate);
1359 friend class AstLiteralReindexer;
1363 // Property is used for passing information
1364 // about an object literal's properties from the parser
1365 // to the code generator.
1366 class ObjectLiteralProperty final : public ZoneObject {
1369 CONSTANT, // Property with constant value (compile time).
1370 COMPUTED, // Property with computed value (execution time).
1371 MATERIALIZED_LITERAL, // Property value is a materialized literal.
1372 GETTER, SETTER, // Property is an accessor function.
1373 PROTOTYPE // Property is __proto__.
1376 Expression* key() { return key_; }
1377 Expression* value() { return value_; }
1378 Kind kind() { return kind_; }
1380 // Type feedback information.
1381 bool IsMonomorphic() { return !receiver_type_.is_null(); }
1382 Handle<Map> GetReceiverType() { return receiver_type_; }
1384 bool IsCompileTimeValue();
1386 void set_emit_store(bool emit_store);
1389 bool is_static() const { return is_static_; }
1390 bool is_computed_name() const { return is_computed_name_; }
1392 FeedbackVectorICSlot GetSlot(int offset = 0) const {
1393 if (slot_.IsInvalid()) return slot_;
1394 int slot = slot_.ToInt();
1395 return FeedbackVectorICSlot(slot + offset);
1397 FeedbackVectorICSlot slot() const { return slot_; }
1398 void set_slot(FeedbackVectorICSlot slot) { slot_ = slot; }
1400 void set_receiver_type(Handle<Map> map) { receiver_type_ = map; }
1403 friend class AstNodeFactory;
1405 ObjectLiteralProperty(Expression* key, Expression* value, Kind kind,
1406 bool is_static, bool is_computed_name);
1407 ObjectLiteralProperty(AstValueFactory* ast_value_factory, Expression* key,
1408 Expression* value, bool is_static,
1409 bool is_computed_name);
1414 FeedbackVectorICSlot slot_;
1418 bool is_computed_name_;
1419 Handle<Map> receiver_type_;
1423 // An object literal has a boilerplate object that is used
1424 // for minimizing the work when constructing it at runtime.
1425 class ObjectLiteral final : public MaterializedLiteral {
1427 typedef ObjectLiteralProperty Property;
1429 DECLARE_NODE_TYPE(ObjectLiteral)
1431 Handle<FixedArray> constant_properties() const {
1432 return constant_properties_;
1434 int properties_count() const { return constant_properties_->length() / 2; }
1435 ZoneList<Property*>* properties() const { return properties_; }
1436 bool fast_elements() const { return fast_elements_; }
1437 bool may_store_doubles() const { return may_store_doubles_; }
1438 bool has_function() const { return has_function_; }
1439 bool has_elements() const { return has_elements_; }
1441 // Decide if a property should be in the object boilerplate.
1442 static bool IsBoilerplateProperty(Property* property);
1444 // Populate the constant properties fixed array.
1445 void BuildConstantProperties(Isolate* isolate);
1447 // Mark all computed expressions that are bound to a key that
1448 // is shadowed by a later occurrence of the same key. For the
1449 // marked expressions, no store code is emitted.
1450 void CalculateEmitStore(Zone* zone);
1452 // Assemble bitfield of flags for the CreateObjectLiteral helper.
1453 int ComputeFlags(bool disable_mementos = false) const {
1454 int flags = fast_elements() ? kFastElements : kNoFlags;
1455 flags |= has_function() ? kHasFunction : kNoFlags;
1456 if (depth() == 1 && !has_elements() && !may_store_doubles()) {
1457 flags |= kShallowProperties;
1459 if (disable_mementos) {
1460 flags |= kDisableMementos;
1471 kHasFunction = 1 << 1,
1472 kShallowProperties = 1 << 2,
1473 kDisableMementos = 1 << 3,
1477 struct Accessors: public ZoneObject {
1478 Accessors() : getter(NULL), setter(NULL) {}
1479 ObjectLiteralProperty* getter;
1480 ObjectLiteralProperty* setter;
1483 BailoutId CreateLiteralId() const { return BailoutId(local_id(0)); }
1485 // Return an AST id for a property that is used in simulate instructions.
1486 BailoutId GetIdForProperty(int i) { return BailoutId(local_id(i + 1)); }
1488 // Unlike other AST nodes, this number of bailout IDs allocated for an
1489 // ObjectLiteral can vary, so num_ids() is not a static method.
1490 int num_ids() const { return parent_num_ids() + 1 + properties()->length(); }
1492 // Object literals need one feedback slot for each non-trivial value, as well
1493 // as some slots for home objects.
1494 void AssignFeedbackVectorSlots(Isolate* isolate, FeedbackVectorSpec* spec,
1495 ICSlotCache* cache) override;
1498 ObjectLiteral(Zone* zone, ZoneList<Property*>* properties, int literal_index,
1499 int boilerplate_properties, bool has_function, bool is_strong,
1501 : MaterializedLiteral(zone, literal_index, is_strong, pos),
1502 properties_(properties),
1503 boilerplate_properties_(boilerplate_properties),
1504 fast_elements_(false),
1505 has_elements_(false),
1506 may_store_doubles_(false),
1507 has_function_(has_function),
1508 slot_(FeedbackVectorICSlot::Invalid()) {
1510 static int parent_num_ids() { return MaterializedLiteral::num_ids(); }
1513 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
1514 Handle<FixedArray> constant_properties_;
1515 ZoneList<Property*>* properties_;
1516 int boilerplate_properties_;
1517 bool fast_elements_;
1519 bool may_store_doubles_;
1521 FeedbackVectorICSlot slot_;
1525 // Node for capturing a regexp literal.
1526 class RegExpLiteral final : public MaterializedLiteral {
1528 DECLARE_NODE_TYPE(RegExpLiteral)
1530 Handle<String> pattern() const { return pattern_->string(); }
1531 Handle<String> flags() const { return flags_->string(); }
1534 RegExpLiteral(Zone* zone, const AstRawString* pattern,
1535 const AstRawString* flags, int literal_index, bool is_strong,
1537 : MaterializedLiteral(zone, literal_index, is_strong, pos),
1544 const AstRawString* pattern_;
1545 const AstRawString* flags_;
1549 // An array literal has a literals object that is used
1550 // for minimizing the work when constructing it at runtime.
1551 class ArrayLiteral final : public MaterializedLiteral {
1553 DECLARE_NODE_TYPE(ArrayLiteral)
1555 Handle<FixedArray> constant_elements() const { return constant_elements_; }
1556 ElementsKind constant_elements_kind() const {
1557 DCHECK_EQ(2, constant_elements_->length());
1558 return static_cast<ElementsKind>(
1559 Smi::cast(constant_elements_->get(0))->value());
1562 ZoneList<Expression*>* values() const { return values_; }
1564 BailoutId CreateLiteralId() const { return BailoutId(local_id(0)); }
1566 // Return an AST id for an element that is used in simulate instructions.
1567 BailoutId GetIdForElement(int i) { return BailoutId(local_id(i + 1)); }
1569 // Unlike other AST nodes, this number of bailout IDs allocated for an
1570 // ArrayLiteral can vary, so num_ids() is not a static method.
1571 int num_ids() const { return parent_num_ids() + 1 + values()->length(); }
1573 // Populate the constant elements fixed array.
1574 void BuildConstantElements(Isolate* isolate);
1576 // Assemble bitfield of flags for the CreateArrayLiteral helper.
1577 int ComputeFlags(bool disable_mementos = false) const {
1578 int flags = depth() == 1 ? kShallowElements : kNoFlags;
1579 if (disable_mementos) {
1580 flags |= kDisableMementos;
1590 kShallowElements = 1,
1591 kDisableMementos = 1 << 1,
1596 ArrayLiteral(Zone* zone, ZoneList<Expression*>* values,
1597 int first_spread_index, int literal_index, bool is_strong,
1599 : MaterializedLiteral(zone, literal_index, is_strong, pos),
1601 first_spread_index_(first_spread_index) {}
1602 static int parent_num_ids() { return MaterializedLiteral::num_ids(); }
1605 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
1607 Handle<FixedArray> constant_elements_;
1608 ZoneList<Expression*>* values_;
1609 int first_spread_index_;
1613 class VariableProxy final : public Expression {
1615 DECLARE_NODE_TYPE(VariableProxy)
1617 bool IsValidReferenceExpression() const override {
1618 return !is_this() && !is_new_target();
1621 bool IsArguments() const { return is_resolved() && var()->is_arguments(); }
1623 Handle<String> name() const { return raw_name()->string(); }
1624 const AstRawString* raw_name() const {
1625 return is_resolved() ? var_->raw_name() : raw_name_;
1628 Variable* var() const {
1629 DCHECK(is_resolved());
1632 void set_var(Variable* v) {
1633 DCHECK(!is_resolved());
1638 bool is_this() const { return IsThisField::decode(bit_field_); }
1640 bool is_assigned() const { return IsAssignedField::decode(bit_field_); }
1641 void set_is_assigned() {
1642 bit_field_ = IsAssignedField::update(bit_field_, true);
1645 bool is_resolved() const { return IsResolvedField::decode(bit_field_); }
1646 void set_is_resolved() {
1647 bit_field_ = IsResolvedField::update(bit_field_, true);
1650 bool is_new_target() const { return IsNewTargetField::decode(bit_field_); }
1651 void set_is_new_target() {
1652 bit_field_ = IsNewTargetField::update(bit_field_, true);
1655 int end_position() const { return end_position_; }
1657 // Bind this proxy to the variable var.
1658 void BindTo(Variable* var);
1660 bool UsesVariableFeedbackSlot() const {
1661 return var()->IsUnallocated() || var()->IsLookupSlot();
1664 void AssignFeedbackVectorSlots(Isolate* isolate, FeedbackVectorSpec* spec,
1665 ICSlotCache* cache) override;
1667 FeedbackVectorICSlot VariableFeedbackSlot() {
1668 return variable_feedback_slot_;
1671 static int num_ids() { return parent_num_ids() + 1; }
1672 BailoutId BeforeId() const { return BailoutId(local_id(0)); }
1675 VariableProxy(Zone* zone, Variable* var, int start_position,
1678 VariableProxy(Zone* zone, const AstRawString* name,
1679 Variable::Kind variable_kind, int start_position,
1681 static int parent_num_ids() { return Expression::num_ids(); }
1682 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
1684 class IsThisField : public BitField8<bool, 0, 1> {};
1685 class IsAssignedField : public BitField8<bool, 1, 1> {};
1686 class IsResolvedField : public BitField8<bool, 2, 1> {};
1687 class IsNewTargetField : public BitField8<bool, 3, 1> {};
1689 // Start with 16-bit (or smaller) field, which should get packed together
1690 // with Expression's trailing 16-bit field.
1692 FeedbackVectorICSlot variable_feedback_slot_;
1694 const AstRawString* raw_name_; // if !is_resolved_
1695 Variable* var_; // if is_resolved_
1697 // Position is stored in the AstNode superclass, but VariableProxy needs to
1698 // know its end position too (for error messages). It cannot be inferred from
1699 // the variable name length because it can contain escapes.
1704 // Left-hand side can only be a property, a global or a (parameter or local)
1710 NAMED_SUPER_PROPERTY,
1711 KEYED_SUPER_PROPERTY
1715 class Property final : public Expression {
1717 DECLARE_NODE_TYPE(Property)
1719 bool IsValidReferenceExpression() const override { return true; }
1721 Expression* obj() const { return obj_; }
1722 Expression* key() const { return key_; }
1724 static int num_ids() { return parent_num_ids() + 1; }
1725 BailoutId LoadId() const { return BailoutId(local_id(0)); }
1727 bool IsStringAccess() const {
1728 return IsStringAccessField::decode(bit_field_);
1731 // Type feedback information.
1732 bool IsMonomorphic() override { return receiver_types_.length() == 1; }
1733 SmallMapList* GetReceiverTypes() override { return &receiver_types_; }
1734 KeyedAccessStoreMode GetStoreMode() const override { return STANDARD_STORE; }
1735 IcCheckType GetKeyType() const override {
1736 return KeyTypeField::decode(bit_field_);
1738 bool IsUninitialized() const {
1739 return !is_for_call() && HasNoTypeInformation();
1741 bool HasNoTypeInformation() const {
1742 return GetInlineCacheState() == UNINITIALIZED;
1744 InlineCacheState GetInlineCacheState() const {
1745 return InlineCacheStateField::decode(bit_field_);
1747 void set_is_string_access(bool b) {
1748 bit_field_ = IsStringAccessField::update(bit_field_, b);
1750 void set_key_type(IcCheckType key_type) {
1751 bit_field_ = KeyTypeField::update(bit_field_, key_type);
1753 void set_inline_cache_state(InlineCacheState state) {
1754 bit_field_ = InlineCacheStateField::update(bit_field_, state);
1756 void mark_for_call() {
1757 bit_field_ = IsForCallField::update(bit_field_, true);
1759 bool is_for_call() const { return IsForCallField::decode(bit_field_); }
1761 bool IsSuperAccess() { return obj()->IsSuperPropertyReference(); }
1763 void AssignFeedbackVectorSlots(Isolate* isolate, FeedbackVectorSpec* spec,
1764 ICSlotCache* cache) override {
1765 FeedbackVectorSlotKind kind = key()->IsPropertyName()
1766 ? FeedbackVectorSlotKind::LOAD_IC
1767 : FeedbackVectorSlotKind::KEYED_LOAD_IC;
1768 property_feedback_slot_ = spec->AddSlot(kind);
1771 FeedbackVectorICSlot PropertyFeedbackSlot() const {
1772 return property_feedback_slot_;
1775 static LhsKind GetAssignType(Property* property) {
1776 if (property == NULL) return VARIABLE;
1777 bool super_access = property->IsSuperAccess();
1778 return (property->key()->IsPropertyName())
1779 ? (super_access ? NAMED_SUPER_PROPERTY : NAMED_PROPERTY)
1780 : (super_access ? KEYED_SUPER_PROPERTY : KEYED_PROPERTY);
1784 Property(Zone* zone, Expression* obj, Expression* key, int pos)
1785 : Expression(zone, pos),
1786 bit_field_(IsForCallField::encode(false) |
1787 IsStringAccessField::encode(false) |
1788 InlineCacheStateField::encode(UNINITIALIZED)),
1789 property_feedback_slot_(FeedbackVectorICSlot::Invalid()),
1792 static int parent_num_ids() { return Expression::num_ids(); }
1795 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
1797 class IsForCallField : public BitField8<bool, 0, 1> {};
1798 class IsStringAccessField : public BitField8<bool, 1, 1> {};
1799 class KeyTypeField : public BitField8<IcCheckType, 2, 1> {};
1800 class InlineCacheStateField : public BitField8<InlineCacheState, 3, 4> {};
1802 FeedbackVectorICSlot property_feedback_slot_;
1805 SmallMapList receiver_types_;
1809 class Call final : public Expression {
1811 DECLARE_NODE_TYPE(Call)
1813 Expression* expression() const { return expression_; }
1814 ZoneList<Expression*>* arguments() const { return arguments_; }
1816 // Type feedback information.
1817 void AssignFeedbackVectorSlots(Isolate* isolate, FeedbackVectorSpec* spec,
1818 ICSlotCache* cache) override;
1820 FeedbackVectorSlot CallFeedbackSlot() const { return slot_; }
1822 FeedbackVectorICSlot CallFeedbackICSlot() const { return ic_slot_; }
1824 SmallMapList* GetReceiverTypes() override {
1825 if (expression()->IsProperty()) {
1826 return expression()->AsProperty()->GetReceiverTypes();
1831 bool IsMonomorphic() override {
1832 if (expression()->IsProperty()) {
1833 return expression()->AsProperty()->IsMonomorphic();
1835 return !target_.is_null();
1838 bool global_call() const {
1839 VariableProxy* proxy = expression_->AsVariableProxy();
1840 return proxy != NULL && proxy->var()->IsUnallocatedOrGlobalSlot();
1843 bool known_global_function() const {
1844 return global_call() && !target_.is_null();
1847 Handle<JSFunction> target() { return target_; }
1849 Handle<AllocationSite> allocation_site() { return allocation_site_; }
1851 void SetKnownGlobalTarget(Handle<JSFunction> target) {
1853 set_is_uninitialized(false);
1855 void set_target(Handle<JSFunction> target) { target_ = target; }
1856 void set_allocation_site(Handle<AllocationSite> site) {
1857 allocation_site_ = site;
1860 static int num_ids() { return parent_num_ids() + 3; }
1861 BailoutId ReturnId() const { return BailoutId(local_id(0)); }
1862 BailoutId EvalId() const { return BailoutId(local_id(1)); }
1863 BailoutId LookupId() const { return BailoutId(local_id(2)); }
1865 bool is_uninitialized() const {
1866 return IsUninitializedField::decode(bit_field_);
1868 void set_is_uninitialized(bool b) {
1869 bit_field_ = IsUninitializedField::update(bit_field_, b);
1881 // Helpers to determine how to handle the call.
1882 CallType GetCallType(Isolate* isolate) const;
1883 bool IsUsingCallFeedbackSlot(Isolate* isolate) const;
1884 bool IsUsingCallFeedbackICSlot(Isolate* isolate) const;
1887 // Used to assert that the FullCodeGenerator records the return site.
1888 bool return_is_recorded_;
1892 Call(Zone* zone, Expression* expression, ZoneList<Expression*>* arguments,
1894 : Expression(zone, pos),
1895 ic_slot_(FeedbackVectorICSlot::Invalid()),
1896 slot_(FeedbackVectorSlot::Invalid()),
1897 expression_(expression),
1898 arguments_(arguments),
1899 bit_field_(IsUninitializedField::encode(false)) {
1900 if (expression->IsProperty()) {
1901 expression->AsProperty()->mark_for_call();
1904 static int parent_num_ids() { return Expression::num_ids(); }
1907 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
1909 FeedbackVectorICSlot ic_slot_;
1910 FeedbackVectorSlot slot_;
1911 Expression* expression_;
1912 ZoneList<Expression*>* arguments_;
1913 Handle<JSFunction> target_;
1914 Handle<AllocationSite> allocation_site_;
1915 class IsUninitializedField : public BitField8<bool, 0, 1> {};
1920 class CallNew final : public Expression {
1922 DECLARE_NODE_TYPE(CallNew)
1924 Expression* expression() const { return expression_; }
1925 ZoneList<Expression*>* arguments() const { return arguments_; }
1927 // Type feedback information.
1928 void AssignFeedbackVectorSlots(Isolate* isolate, FeedbackVectorSpec* spec,
1929 ICSlotCache* cache) override {
1930 callnew_feedback_slot_ = spec->AddStubSlot();
1933 FeedbackVectorSlot CallNewFeedbackSlot() {
1934 DCHECK(!callnew_feedback_slot_.IsInvalid());
1935 return callnew_feedback_slot_;
1938 bool IsMonomorphic() override { return is_monomorphic_; }
1939 Handle<JSFunction> target() const { return target_; }
1940 Handle<AllocationSite> allocation_site() const {
1941 return allocation_site_;
1944 static int num_ids() { return parent_num_ids() + 1; }
1945 static int feedback_slots() { return 1; }
1946 BailoutId ReturnId() const { return BailoutId(local_id(0)); }
1948 void set_allocation_site(Handle<AllocationSite> site) {
1949 allocation_site_ = site;
1951 void set_is_monomorphic(bool monomorphic) { is_monomorphic_ = monomorphic; }
1952 void set_target(Handle<JSFunction> target) { target_ = target; }
1953 void SetKnownGlobalTarget(Handle<JSFunction> target) {
1955 is_monomorphic_ = true;
1959 CallNew(Zone* zone, Expression* expression, ZoneList<Expression*>* arguments,
1961 : Expression(zone, pos),
1962 expression_(expression),
1963 arguments_(arguments),
1964 is_monomorphic_(false),
1965 callnew_feedback_slot_(FeedbackVectorSlot::Invalid()) {}
1967 static int parent_num_ids() { return Expression::num_ids(); }
1970 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
1972 Expression* expression_;
1973 ZoneList<Expression*>* arguments_;
1974 bool is_monomorphic_;
1975 Handle<JSFunction> target_;
1976 Handle<AllocationSite> allocation_site_;
1977 FeedbackVectorSlot callnew_feedback_slot_;
1981 // The CallRuntime class does not represent any official JavaScript
1982 // language construct. Instead it is used to call a C or JS function
1983 // with a set of arguments. This is used from the builtins that are
1984 // implemented in JavaScript (see "v8natives.js").
1985 class CallRuntime final : public Expression {
1987 DECLARE_NODE_TYPE(CallRuntime)
1989 ZoneList<Expression*>* arguments() const { return arguments_; }
1990 bool is_jsruntime() const { return function_ == NULL; }
1992 int context_index() const {
1993 DCHECK(is_jsruntime());
1994 return context_index_;
1996 const Runtime::Function* function() const {
1997 DCHECK(!is_jsruntime());
2001 static int num_ids() { return parent_num_ids() + 1; }
2002 BailoutId CallId() { return BailoutId(local_id(0)); }
2004 const char* debug_name() {
2005 return is_jsruntime() ? "(context function)" : function_->name;
2009 CallRuntime(Zone* zone, const Runtime::Function* function,
2010 ZoneList<Expression*>* arguments, int pos)
2011 : Expression(zone, pos), function_(function), arguments_(arguments) {}
2013 CallRuntime(Zone* zone, int context_index, ZoneList<Expression*>* arguments,
2015 : Expression(zone, pos),
2017 context_index_(context_index),
2018 arguments_(arguments) {}
2020 static int parent_num_ids() { return Expression::num_ids(); }
2023 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
2025 const Runtime::Function* function_;
2027 ZoneList<Expression*>* arguments_;
2031 class UnaryOperation final : public Expression {
2033 DECLARE_NODE_TYPE(UnaryOperation)
2035 Token::Value op() const { return op_; }
2036 Expression* expression() const { return expression_; }
2038 // For unary not (Token::NOT), the AST ids where true and false will
2039 // actually be materialized, respectively.
2040 static int num_ids() { return parent_num_ids() + 2; }
2041 BailoutId MaterializeTrueId() const { return BailoutId(local_id(0)); }
2042 BailoutId MaterializeFalseId() const { return BailoutId(local_id(1)); }
2044 virtual void RecordToBooleanTypeFeedback(TypeFeedbackOracle* oracle) override;
2047 UnaryOperation(Zone* zone, Token::Value op, Expression* expression, int pos)
2048 : Expression(zone, pos), op_(op), expression_(expression) {
2049 DCHECK(Token::IsUnaryOp(op));
2051 static int parent_num_ids() { return Expression::num_ids(); }
2054 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
2057 Expression* expression_;
2061 class BinaryOperation final : public Expression {
2063 DECLARE_NODE_TYPE(BinaryOperation)
2065 Token::Value op() const { return static_cast<Token::Value>(op_); }
2066 Expression* left() const { return left_; }
2067 Expression* right() const { return right_; }
2068 Handle<AllocationSite> allocation_site() const { return allocation_site_; }
2069 void set_allocation_site(Handle<AllocationSite> allocation_site) {
2070 allocation_site_ = allocation_site;
2073 // The short-circuit logical operations need an AST ID for their
2074 // right-hand subexpression.
2075 static int num_ids() { return parent_num_ids() + 2; }
2076 BailoutId RightId() const { return BailoutId(local_id(0)); }
2078 TypeFeedbackId BinaryOperationFeedbackId() const {
2079 return TypeFeedbackId(local_id(1));
2081 Maybe<int> fixed_right_arg() const {
2082 return has_fixed_right_arg_ ? Just(fixed_right_arg_value_) : Nothing<int>();
2084 void set_fixed_right_arg(Maybe<int> arg) {
2085 has_fixed_right_arg_ = arg.IsJust();
2086 if (arg.IsJust()) fixed_right_arg_value_ = arg.FromJust();
2089 virtual void RecordToBooleanTypeFeedback(TypeFeedbackOracle* oracle) override;
2092 BinaryOperation(Zone* zone, Token::Value op, Expression* left,
2093 Expression* right, int pos)
2094 : Expression(zone, pos),
2095 op_(static_cast<byte>(op)),
2096 has_fixed_right_arg_(false),
2097 fixed_right_arg_value_(0),
2100 DCHECK(Token::IsBinaryOp(op));
2102 static int parent_num_ids() { return Expression::num_ids(); }
2105 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
2107 const byte op_; // actually Token::Value
2108 // TODO(rossberg): the fixed arg should probably be represented as a Constant
2109 // type for the RHS. Currenty it's actually a Maybe<int>
2110 bool has_fixed_right_arg_;
2111 int fixed_right_arg_value_;
2114 Handle<AllocationSite> allocation_site_;
2118 class CountOperation final : public Expression {
2120 DECLARE_NODE_TYPE(CountOperation)
2122 bool is_prefix() const { return IsPrefixField::decode(bit_field_); }
2123 bool is_postfix() const { return !is_prefix(); }
2125 Token::Value op() const { return TokenField::decode(bit_field_); }
2126 Token::Value binary_op() {
2127 return (op() == Token::INC) ? Token::ADD : Token::SUB;
2130 Expression* expression() const { return expression_; }
2132 bool IsMonomorphic() override { return receiver_types_.length() == 1; }
2133 SmallMapList* GetReceiverTypes() override { return &receiver_types_; }
2134 IcCheckType GetKeyType() const override {
2135 return KeyTypeField::decode(bit_field_);
2137 KeyedAccessStoreMode GetStoreMode() const override {
2138 return StoreModeField::decode(bit_field_);
2140 Type* type() const { return type_; }
2141 void set_key_type(IcCheckType type) {
2142 bit_field_ = KeyTypeField::update(bit_field_, type);
2144 void set_store_mode(KeyedAccessStoreMode mode) {
2145 bit_field_ = StoreModeField::update(bit_field_, mode);
2147 void set_type(Type* type) { type_ = type; }
2149 static int num_ids() { return parent_num_ids() + 4; }
2150 BailoutId AssignmentId() const { return BailoutId(local_id(0)); }
2151 BailoutId ToNumberId() const { return BailoutId(local_id(1)); }
2152 TypeFeedbackId CountBinOpFeedbackId() const {
2153 return TypeFeedbackId(local_id(2));
2155 TypeFeedbackId CountStoreFeedbackId() const {
2156 return TypeFeedbackId(local_id(3));
2159 void AssignFeedbackVectorSlots(Isolate* isolate, FeedbackVectorSpec* spec,
2160 ICSlotCache* cache) override;
2161 FeedbackVectorICSlot CountSlot() const { return slot_; }
2164 CountOperation(Zone* zone, Token::Value op, bool is_prefix, Expression* expr,
2166 : Expression(zone, pos),
2168 IsPrefixField::encode(is_prefix) | KeyTypeField::encode(ELEMENT) |
2169 StoreModeField::encode(STANDARD_STORE) | TokenField::encode(op)),
2172 slot_(FeedbackVectorICSlot::Invalid()) {}
2173 static int parent_num_ids() { return Expression::num_ids(); }
2176 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
2178 class IsPrefixField : public BitField16<bool, 0, 1> {};
2179 class KeyTypeField : public BitField16<IcCheckType, 1, 1> {};
2180 class StoreModeField : public BitField16<KeyedAccessStoreMode, 2, 3> {};
2181 class TokenField : public BitField16<Token::Value, 5, 8> {};
2183 // Starts with 16-bit field, which should get packed together with
2184 // Expression's trailing 16-bit field.
2185 uint16_t bit_field_;
2187 Expression* expression_;
2188 SmallMapList receiver_types_;
2189 FeedbackVectorICSlot slot_;
2193 class CompareOperation final : public Expression {
2195 DECLARE_NODE_TYPE(CompareOperation)
2197 Token::Value op() const { return op_; }
2198 Expression* left() const { return left_; }
2199 Expression* right() const { return right_; }
2201 // Type feedback information.
2202 static int num_ids() { return parent_num_ids() + 1; }
2203 TypeFeedbackId CompareOperationFeedbackId() const {
2204 return TypeFeedbackId(local_id(0));
2206 Type* combined_type() const { return combined_type_; }
2207 void set_combined_type(Type* type) { combined_type_ = type; }
2209 // Match special cases.
2210 bool IsLiteralCompareTypeof(Expression** expr, Handle<String>* check);
2211 bool IsLiteralCompareUndefined(Expression** expr, Isolate* isolate);
2212 bool IsLiteralCompareNull(Expression** expr);
2215 CompareOperation(Zone* zone, Token::Value op, Expression* left,
2216 Expression* right, int pos)
2217 : Expression(zone, pos),
2221 combined_type_(Type::None(zone)) {
2222 DCHECK(Token::IsCompareOp(op));
2224 static int parent_num_ids() { return Expression::num_ids(); }
2227 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
2233 Type* combined_type_;
2237 class Spread final : public Expression {
2239 DECLARE_NODE_TYPE(Spread)
2241 Expression* expression() const { return expression_; }
2243 static int num_ids() { return parent_num_ids(); }
2246 Spread(Zone* zone, Expression* expression, int pos)
2247 : Expression(zone, pos), expression_(expression) {}
2248 static int parent_num_ids() { return Expression::num_ids(); }
2251 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
2253 Expression* expression_;
2257 class Conditional final : public Expression {
2259 DECLARE_NODE_TYPE(Conditional)
2261 Expression* condition() const { return condition_; }
2262 Expression* then_expression() const { return then_expression_; }
2263 Expression* else_expression() const { return else_expression_; }
2265 static int num_ids() { return parent_num_ids() + 2; }
2266 BailoutId ThenId() const { return BailoutId(local_id(0)); }
2267 BailoutId ElseId() const { return BailoutId(local_id(1)); }
2270 Conditional(Zone* zone, Expression* condition, Expression* then_expression,
2271 Expression* else_expression, int position)
2272 : Expression(zone, position),
2273 condition_(condition),
2274 then_expression_(then_expression),
2275 else_expression_(else_expression) {}
2276 static int parent_num_ids() { return Expression::num_ids(); }
2279 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
2281 Expression* condition_;
2282 Expression* then_expression_;
2283 Expression* else_expression_;
2287 class Assignment final : public Expression {
2289 DECLARE_NODE_TYPE(Assignment)
2291 Assignment* AsSimpleAssignment() { return !is_compound() ? this : NULL; }
2293 Token::Value binary_op() const;
2295 Token::Value op() const { return TokenField::decode(bit_field_); }
2296 Expression* target() const { return target_; }
2297 Expression* value() const { return value_; }
2298 BinaryOperation* binary_operation() const { return binary_operation_; }
2300 // This check relies on the definition order of token in token.h.
2301 bool is_compound() const { return op() > Token::ASSIGN; }
2303 static int num_ids() { return parent_num_ids() + 2; }
2304 BailoutId AssignmentId() const { return BailoutId(local_id(0)); }
2306 // Type feedback information.
2307 TypeFeedbackId AssignmentFeedbackId() { return TypeFeedbackId(local_id(1)); }
2308 bool IsMonomorphic() override { return receiver_types_.length() == 1; }
2309 bool IsUninitialized() const {
2310 return IsUninitializedField::decode(bit_field_);
2312 bool HasNoTypeInformation() {
2313 return IsUninitializedField::decode(bit_field_);
2315 SmallMapList* GetReceiverTypes() override { return &receiver_types_; }
2316 IcCheckType GetKeyType() const override {
2317 return KeyTypeField::decode(bit_field_);
2319 KeyedAccessStoreMode GetStoreMode() const override {
2320 return StoreModeField::decode(bit_field_);
2322 void set_is_uninitialized(bool b) {
2323 bit_field_ = IsUninitializedField::update(bit_field_, b);
2325 void set_key_type(IcCheckType key_type) {
2326 bit_field_ = KeyTypeField::update(bit_field_, key_type);
2328 void set_store_mode(KeyedAccessStoreMode mode) {
2329 bit_field_ = StoreModeField::update(bit_field_, mode);
2332 void AssignFeedbackVectorSlots(Isolate* isolate, FeedbackVectorSpec* spec,
2333 ICSlotCache* cache) override;
2334 FeedbackVectorICSlot AssignmentSlot() const { return slot_; }
2337 Assignment(Zone* zone, Token::Value op, Expression* target, Expression* value,
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 class IsUninitializedField : public BitField16<bool, 0, 1> {};
2345 class KeyTypeField : public BitField16<IcCheckType, 1, 1> {};
2346 class StoreModeField : public BitField16<KeyedAccessStoreMode, 2, 3> {};
2347 class TokenField : public BitField16<Token::Value, 5, 8> {};
2349 // Starts with 16-bit field, which should get packed together with
2350 // Expression's trailing 16-bit field.
2351 uint16_t bit_field_;
2352 Expression* target_;
2354 BinaryOperation* binary_operation_;
2355 SmallMapList receiver_types_;
2356 FeedbackVectorICSlot slot_;
2360 class Yield final : public Expression {
2362 DECLARE_NODE_TYPE(Yield)
2365 kInitial, // The initial yield that returns the unboxed generator object.
2366 kSuspend, // A normal yield: { value: EXPRESSION, done: false }
2367 kDelegating, // A yield*.
2368 kFinal // A return: { value: EXPRESSION, done: true }
2371 Expression* generator_object() const { return generator_object_; }
2372 Expression* expression() const { return expression_; }
2373 Kind yield_kind() const { return yield_kind_; }
2375 // Type feedback information.
2376 bool HasFeedbackSlots() const { return yield_kind() == kDelegating; }
2377 void AssignFeedbackVectorSlots(Isolate* isolate, FeedbackVectorSpec* spec,
2378 ICSlotCache* cache) override {
2379 if (HasFeedbackSlots()) {
2380 yield_first_feedback_slot_ = spec->AddKeyedLoadICSlot();
2381 spec->AddLoadICSlots(2);
2385 FeedbackVectorICSlot KeyedLoadFeedbackSlot() {
2386 DCHECK(!HasFeedbackSlots() || !yield_first_feedback_slot_.IsInvalid());
2387 return yield_first_feedback_slot_;
2390 FeedbackVectorICSlot DoneFeedbackSlot() {
2391 return KeyedLoadFeedbackSlot().next();
2394 FeedbackVectorICSlot ValueFeedbackSlot() { return DoneFeedbackSlot().next(); }
2397 Yield(Zone* zone, Expression* generator_object, Expression* expression,
2398 Kind yield_kind, int pos)
2399 : Expression(zone, pos),
2400 generator_object_(generator_object),
2401 expression_(expression),
2402 yield_kind_(yield_kind),
2403 yield_first_feedback_slot_(FeedbackVectorICSlot::Invalid()) {}
2406 Expression* generator_object_;
2407 Expression* expression_;
2409 FeedbackVectorICSlot yield_first_feedback_slot_;
2413 class Throw final : public Expression {
2415 DECLARE_NODE_TYPE(Throw)
2417 Expression* exception() const { return exception_; }
2420 Throw(Zone* zone, Expression* exception, int pos)
2421 : Expression(zone, pos), exception_(exception) {}
2424 Expression* exception_;
2428 class FunctionLiteral final : public Expression {
2431 ANONYMOUS_EXPRESSION,
2436 enum ParameterFlag {
2437 kNoDuplicateParameters = 0,
2438 kHasDuplicateParameters = 1
2441 enum IsFunctionFlag {
2446 enum EagerCompileHint { kShouldEagerCompile, kShouldLazyCompile };
2448 enum ShouldBeUsedOnceHint { kShouldBeUsedOnce, kDontKnowIfShouldBeUsedOnce };
2450 enum ArityRestriction {
2456 DECLARE_NODE_TYPE(FunctionLiteral)
2458 Handle<String> name() const { return raw_name_->string(); }
2459 const AstRawString* raw_name() const { return raw_name_; }
2460 Scope* scope() const { return scope_; }
2461 ZoneList<Statement*>* body() const { return body_; }
2462 void set_function_token_position(int pos) { function_token_position_ = pos; }
2463 int function_token_position() const { return function_token_position_; }
2464 int start_position() const;
2465 int end_position() const;
2466 int SourceSize() const { return end_position() - start_position(); }
2467 bool is_expression() const { return IsExpression::decode(bitfield_); }
2468 bool is_anonymous() const { return IsAnonymous::decode(bitfield_); }
2469 LanguageMode language_mode() const;
2471 static bool NeedsHomeObject(Expression* expr);
2473 int materialized_literal_count() { return materialized_literal_count_; }
2474 int expected_property_count() { return expected_property_count_; }
2475 int parameter_count() { return parameter_count_; }
2477 bool AllowsLazyCompilation();
2478 bool AllowsLazyCompilationWithoutContext();
2480 Handle<String> debug_name() const {
2481 if (raw_name_ != NULL && !raw_name_->IsEmpty()) {
2482 return raw_name_->string();
2484 return inferred_name();
2487 Handle<String> inferred_name() const {
2488 if (!inferred_name_.is_null()) {
2489 DCHECK(raw_inferred_name_ == NULL);
2490 return inferred_name_;
2492 if (raw_inferred_name_ != NULL) {
2493 return raw_inferred_name_->string();
2496 return Handle<String>();
2499 // Only one of {set_inferred_name, set_raw_inferred_name} should be called.
2500 void set_inferred_name(Handle<String> inferred_name) {
2501 DCHECK(!inferred_name.is_null());
2502 inferred_name_ = inferred_name;
2503 DCHECK(raw_inferred_name_== NULL || raw_inferred_name_->IsEmpty());
2504 raw_inferred_name_ = NULL;
2507 void set_raw_inferred_name(const AstString* raw_inferred_name) {
2508 DCHECK(raw_inferred_name != NULL);
2509 raw_inferred_name_ = raw_inferred_name;
2510 DCHECK(inferred_name_.is_null());
2511 inferred_name_ = Handle<String>();
2514 bool pretenure() { return Pretenure::decode(bitfield_); }
2515 void set_pretenure() { bitfield_ |= Pretenure::encode(true); }
2517 bool has_duplicate_parameters() {
2518 return HasDuplicateParameters::decode(bitfield_);
2521 bool is_function() { return IsFunction::decode(bitfield_) == kIsFunction; }
2523 // This is used as a heuristic on when to eagerly compile a function
2524 // literal. We consider the following constructs as hints that the
2525 // function will be called immediately:
2526 // - (function() { ... })();
2527 // - var x = function() { ... }();
2528 bool should_eager_compile() const {
2529 return EagerCompileHintBit::decode(bitfield_) == kShouldEagerCompile;
2531 void set_should_eager_compile() {
2532 bitfield_ = EagerCompileHintBit::update(bitfield_, kShouldEagerCompile);
2535 // A hint that we expect this function to be called (exactly) once,
2536 // i.e. we suspect it's an initialization function.
2537 bool should_be_used_once_hint() const {
2538 return ShouldBeUsedOnceHintBit::decode(bitfield_) == kShouldBeUsedOnce;
2540 void set_should_be_used_once_hint() {
2541 bitfield_ = ShouldBeUsedOnceHintBit::update(bitfield_, kShouldBeUsedOnce);
2544 FunctionKind kind() const { return FunctionKindBits::decode(bitfield_); }
2546 int ast_node_count() { return ast_properties_.node_count(); }
2547 AstProperties::Flags flags() const { return ast_properties_.flags(); }
2548 void set_ast_properties(AstProperties* ast_properties) {
2549 ast_properties_ = *ast_properties;
2551 const FeedbackVectorSpec* feedback_vector_spec() const {
2552 return ast_properties_.get_spec();
2554 bool dont_optimize() { return dont_optimize_reason_ != kNoReason; }
2555 BailoutReason dont_optimize_reason() { return dont_optimize_reason_; }
2556 void set_dont_optimize_reason(BailoutReason reason) {
2557 dont_optimize_reason_ = reason;
2561 FunctionLiteral(Zone* zone, const AstRawString* name,
2562 AstValueFactory* ast_value_factory, Scope* scope,
2563 ZoneList<Statement*>* body, int materialized_literal_count,
2564 int expected_property_count, int parameter_count,
2565 FunctionType function_type,
2566 ParameterFlag has_duplicate_parameters,
2567 IsFunctionFlag is_function,
2568 EagerCompileHint eager_compile_hint, FunctionKind kind,
2570 : Expression(zone, position),
2574 raw_inferred_name_(ast_value_factory->empty_string()),
2575 ast_properties_(zone),
2576 dont_optimize_reason_(kNoReason),
2577 materialized_literal_count_(materialized_literal_count),
2578 expected_property_count_(expected_property_count),
2579 parameter_count_(parameter_count),
2580 function_token_position_(RelocInfo::kNoPosition) {
2581 bitfield_ = IsExpression::encode(function_type != DECLARATION) |
2582 IsAnonymous::encode(function_type == ANONYMOUS_EXPRESSION) |
2583 Pretenure::encode(false) |
2584 HasDuplicateParameters::encode(has_duplicate_parameters) |
2585 IsFunction::encode(is_function) |
2586 EagerCompileHintBit::encode(eager_compile_hint) |
2587 FunctionKindBits::encode(kind) |
2588 ShouldBeUsedOnceHintBit::encode(kDontKnowIfShouldBeUsedOnce);
2589 DCHECK(IsValidFunctionKind(kind));
2593 const AstRawString* raw_name_;
2594 Handle<String> name_;
2596 ZoneList<Statement*>* body_;
2597 const AstString* raw_inferred_name_;
2598 Handle<String> inferred_name_;
2599 AstProperties ast_properties_;
2600 BailoutReason dont_optimize_reason_;
2602 int materialized_literal_count_;
2603 int expected_property_count_;
2604 int parameter_count_;
2605 int function_token_position_;
2608 class IsExpression : public BitField<bool, 0, 1> {};
2609 class IsAnonymous : public BitField<bool, 1, 1> {};
2610 class Pretenure : public BitField<bool, 2, 1> {};
2611 class HasDuplicateParameters : public BitField<ParameterFlag, 3, 1> {};
2612 class IsFunction : public BitField<IsFunctionFlag, 4, 1> {};
2613 class EagerCompileHintBit : public BitField<EagerCompileHint, 5, 1> {};
2614 class FunctionKindBits : public BitField<FunctionKind, 6, 8> {};
2615 class ShouldBeUsedOnceHintBit : public BitField<ShouldBeUsedOnceHint, 15, 1> {
2620 class ClassLiteral final : public Expression {
2622 typedef ObjectLiteralProperty Property;
2624 DECLARE_NODE_TYPE(ClassLiteral)
2626 Handle<String> name() const { return raw_name_->string(); }
2627 const AstRawString* raw_name() const { return raw_name_; }
2628 Scope* scope() const { return scope_; }
2629 VariableProxy* class_variable_proxy() const { return class_variable_proxy_; }
2630 Expression* extends() const { return extends_; }
2631 FunctionLiteral* constructor() const { return constructor_; }
2632 ZoneList<Property*>* properties() const { return properties_; }
2633 int start_position() const { return position(); }
2634 int end_position() const { return end_position_; }
2636 BailoutId EntryId() const { return BailoutId(local_id(0)); }
2637 BailoutId DeclsId() const { return BailoutId(local_id(1)); }
2638 BailoutId ExitId() { return BailoutId(local_id(2)); }
2639 BailoutId CreateLiteralId() const { return BailoutId(local_id(3)); }
2641 // Return an AST id for a property that is used in simulate instructions.
2642 BailoutId GetIdForProperty(int i) { return BailoutId(local_id(i + 4)); }
2644 // Unlike other AST nodes, this number of bailout IDs allocated for an
2645 // ClassLiteral can vary, so num_ids() is not a static method.
2646 int num_ids() const { return parent_num_ids() + 4 + properties()->length(); }
2648 // Object literals need one feedback slot for each non-trivial value, as well
2649 // as some slots for home objects.
2650 void AssignFeedbackVectorSlots(Isolate* isolate, FeedbackVectorSpec* spec,
2651 ICSlotCache* cache) override;
2653 bool NeedsProxySlot() const {
2654 return FLAG_vector_stores && scope() != NULL &&
2655 class_variable_proxy()->var()->IsUnallocated();
2658 FeedbackVectorICSlot ProxySlot() const { return slot_; }
2661 ClassLiteral(Zone* zone, const AstRawString* name, Scope* scope,
2662 VariableProxy* class_variable_proxy, Expression* extends,
2663 FunctionLiteral* constructor, ZoneList<Property*>* properties,
2664 int start_position, int end_position)
2665 : Expression(zone, start_position),
2668 class_variable_proxy_(class_variable_proxy),
2670 constructor_(constructor),
2671 properties_(properties),
2672 end_position_(end_position),
2673 slot_(FeedbackVectorICSlot::Invalid()) {
2676 static int parent_num_ids() { return Expression::num_ids(); }
2679 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
2681 const AstRawString* raw_name_;
2683 VariableProxy* class_variable_proxy_;
2684 Expression* extends_;
2685 FunctionLiteral* constructor_;
2686 ZoneList<Property*>* properties_;
2688 FeedbackVectorICSlot slot_;
2692 class NativeFunctionLiteral final : public Expression {
2694 DECLARE_NODE_TYPE(NativeFunctionLiteral)
2696 Handle<String> name() const { return name_->string(); }
2697 v8::Extension* extension() const { return extension_; }
2700 NativeFunctionLiteral(Zone* zone, const AstRawString* name,
2701 v8::Extension* extension, int pos)
2702 : Expression(zone, pos), name_(name), extension_(extension) {}
2705 const AstRawString* name_;
2706 v8::Extension* extension_;
2710 class ThisFunction final : public Expression {
2712 DECLARE_NODE_TYPE(ThisFunction)
2715 ThisFunction(Zone* zone, int pos) : Expression(zone, pos) {}
2719 class SuperPropertyReference final : public Expression {
2721 DECLARE_NODE_TYPE(SuperPropertyReference)
2723 VariableProxy* this_var() const { return this_var_; }
2724 Expression* home_object() const { return home_object_; }
2727 SuperPropertyReference(Zone* zone, VariableProxy* this_var,
2728 Expression* home_object, int pos)
2729 : Expression(zone, pos), this_var_(this_var), home_object_(home_object) {
2730 DCHECK(this_var->is_this());
2731 DCHECK(home_object->IsProperty());
2735 VariableProxy* this_var_;
2736 Expression* home_object_;
2740 class SuperCallReference final : public Expression {
2742 DECLARE_NODE_TYPE(SuperCallReference)
2744 VariableProxy* this_var() const { return this_var_; }
2745 VariableProxy* new_target_var() const { return new_target_var_; }
2746 VariableProxy* this_function_var() const { return this_function_var_; }
2749 SuperCallReference(Zone* zone, VariableProxy* this_var,
2750 VariableProxy* new_target_var,
2751 VariableProxy* this_function_var, int pos)
2752 : Expression(zone, pos),
2753 this_var_(this_var),
2754 new_target_var_(new_target_var),
2755 this_function_var_(this_function_var) {
2756 DCHECK(this_var->is_this());
2757 DCHECK(new_target_var->raw_name()->IsOneByteEqualTo(".new.target"));
2758 DCHECK(this_function_var->raw_name()->IsOneByteEqualTo(".this_function"));
2762 VariableProxy* this_var_;
2763 VariableProxy* new_target_var_;
2764 VariableProxy* this_function_var_;
2768 // This class is produced when parsing the () in arrow functions without any
2769 // arguments and is not actually a valid expression.
2770 class EmptyParentheses final : public Expression {
2772 DECLARE_NODE_TYPE(EmptyParentheses)
2775 EmptyParentheses(Zone* zone, int pos) : Expression(zone, pos) {}
2779 #undef DECLARE_NODE_TYPE
2782 // ----------------------------------------------------------------------------
2783 // Regular expressions
2786 class RegExpVisitor BASE_EMBEDDED {
2788 virtual ~RegExpVisitor() { }
2789 #define MAKE_CASE(Name) \
2790 virtual void* Visit##Name(RegExp##Name*, void* data) = 0;
2791 FOR_EACH_REG_EXP_TREE_TYPE(MAKE_CASE)
2796 class RegExpTree : public ZoneObject {
2798 static const int kInfinity = kMaxInt;
2799 virtual ~RegExpTree() {}
2800 virtual void* Accept(RegExpVisitor* visitor, void* data) = 0;
2801 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
2802 RegExpNode* on_success) = 0;
2803 virtual bool IsTextElement() { return false; }
2804 virtual bool IsAnchoredAtStart() { return false; }
2805 virtual bool IsAnchoredAtEnd() { return false; }
2806 virtual int min_match() = 0;
2807 virtual int max_match() = 0;
2808 // Returns the interval of registers used for captures within this
2810 virtual Interval CaptureRegisters() { return Interval::Empty(); }
2811 virtual void AppendToText(RegExpText* text, Zone* zone);
2812 std::ostream& Print(std::ostream& os, Zone* zone); // NOLINT
2813 #define MAKE_ASTYPE(Name) \
2814 virtual RegExp##Name* As##Name(); \
2815 virtual bool Is##Name();
2816 FOR_EACH_REG_EXP_TREE_TYPE(MAKE_ASTYPE)
2821 class RegExpDisjunction final : public RegExpTree {
2823 explicit RegExpDisjunction(ZoneList<RegExpTree*>* alternatives);
2824 void* Accept(RegExpVisitor* visitor, void* data) override;
2825 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
2826 RegExpNode* on_success) override;
2827 RegExpDisjunction* AsDisjunction() override;
2828 Interval CaptureRegisters() override;
2829 bool IsDisjunction() override;
2830 bool IsAnchoredAtStart() override;
2831 bool IsAnchoredAtEnd() override;
2832 int min_match() override { return min_match_; }
2833 int max_match() override { return max_match_; }
2834 ZoneList<RegExpTree*>* alternatives() { return alternatives_; }
2836 bool SortConsecutiveAtoms(RegExpCompiler* compiler);
2837 void RationalizeConsecutiveAtoms(RegExpCompiler* compiler);
2838 void FixSingleCharacterDisjunctions(RegExpCompiler* compiler);
2839 ZoneList<RegExpTree*>* alternatives_;
2845 class RegExpAlternative final : public RegExpTree {
2847 explicit RegExpAlternative(ZoneList<RegExpTree*>* nodes);
2848 void* Accept(RegExpVisitor* visitor, void* data) override;
2849 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
2850 RegExpNode* on_success) override;
2851 RegExpAlternative* AsAlternative() override;
2852 Interval CaptureRegisters() override;
2853 bool IsAlternative() override;
2854 bool IsAnchoredAtStart() override;
2855 bool IsAnchoredAtEnd() override;
2856 int min_match() override { return min_match_; }
2857 int max_match() override { return max_match_; }
2858 ZoneList<RegExpTree*>* nodes() { return nodes_; }
2860 ZoneList<RegExpTree*>* nodes_;
2866 class RegExpAssertion final : public RegExpTree {
2868 enum AssertionType {
2876 explicit RegExpAssertion(AssertionType type) : assertion_type_(type) { }
2877 void* Accept(RegExpVisitor* visitor, void* data) override;
2878 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
2879 RegExpNode* on_success) override;
2880 RegExpAssertion* AsAssertion() override;
2881 bool IsAssertion() override;
2882 bool IsAnchoredAtStart() override;
2883 bool IsAnchoredAtEnd() override;
2884 int min_match() override { return 0; }
2885 int max_match() override { return 0; }
2886 AssertionType assertion_type() { return assertion_type_; }
2888 AssertionType assertion_type_;
2892 class CharacterSet final BASE_EMBEDDED {
2894 explicit CharacterSet(uc16 standard_set_type)
2896 standard_set_type_(standard_set_type) {}
2897 explicit CharacterSet(ZoneList<CharacterRange>* ranges)
2899 standard_set_type_(0) {}
2900 ZoneList<CharacterRange>* ranges(Zone* zone);
2901 uc16 standard_set_type() { return standard_set_type_; }
2902 void set_standard_set_type(uc16 special_set_type) {
2903 standard_set_type_ = special_set_type;
2905 bool is_standard() { return standard_set_type_ != 0; }
2906 void Canonicalize();
2908 ZoneList<CharacterRange>* ranges_;
2909 // If non-zero, the value represents a standard set (e.g., all whitespace
2910 // characters) without having to expand the ranges.
2911 uc16 standard_set_type_;
2915 class RegExpCharacterClass final : public RegExpTree {
2917 RegExpCharacterClass(ZoneList<CharacterRange>* ranges, bool is_negated)
2919 is_negated_(is_negated) { }
2920 explicit RegExpCharacterClass(uc16 type)
2922 is_negated_(false) { }
2923 void* Accept(RegExpVisitor* visitor, void* data) override;
2924 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
2925 RegExpNode* on_success) override;
2926 RegExpCharacterClass* AsCharacterClass() override;
2927 bool IsCharacterClass() override;
2928 bool IsTextElement() override { return true; }
2929 int min_match() override { return 1; }
2930 int max_match() override { return 1; }
2931 void AppendToText(RegExpText* text, Zone* zone) override;
2932 CharacterSet character_set() { return set_; }
2933 // TODO(lrn): Remove need for complex version if is_standard that
2934 // recognizes a mangled standard set and just do { return set_.is_special(); }
2935 bool is_standard(Zone* zone);
2936 // Returns a value representing the standard character set if is_standard()
2938 // Currently used values are:
2939 // s : unicode whitespace
2940 // S : unicode non-whitespace
2941 // w : ASCII word character (digit, letter, underscore)
2942 // W : non-ASCII word character
2944 // D : non-ASCII digit
2945 // . : non-unicode non-newline
2946 // * : All characters
2947 uc16 standard_type() { return set_.standard_set_type(); }
2948 ZoneList<CharacterRange>* ranges(Zone* zone) { return set_.ranges(zone); }
2949 bool is_negated() { return is_negated_; }
2957 class RegExpAtom final : public RegExpTree {
2959 explicit RegExpAtom(Vector<const uc16> data) : data_(data) { }
2960 void* Accept(RegExpVisitor* visitor, void* data) override;
2961 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
2962 RegExpNode* on_success) override;
2963 RegExpAtom* AsAtom() override;
2964 bool IsAtom() override;
2965 bool IsTextElement() override { return true; }
2966 int min_match() override { return data_.length(); }
2967 int max_match() override { return data_.length(); }
2968 void AppendToText(RegExpText* text, Zone* zone) override;
2969 Vector<const uc16> data() { return data_; }
2970 int length() { return data_.length(); }
2972 Vector<const uc16> data_;
2976 class RegExpText final : public RegExpTree {
2978 explicit RegExpText(Zone* zone) : elements_(2, zone), length_(0) {}
2979 void* Accept(RegExpVisitor* visitor, void* data) override;
2980 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
2981 RegExpNode* on_success) override;
2982 RegExpText* AsText() override;
2983 bool IsText() override;
2984 bool IsTextElement() override { return true; }
2985 int min_match() override { return length_; }
2986 int max_match() override { return length_; }
2987 void AppendToText(RegExpText* text, Zone* zone) override;
2988 void AddElement(TextElement elm, Zone* zone) {
2989 elements_.Add(elm, zone);
2990 length_ += elm.length();
2992 ZoneList<TextElement>* elements() { return &elements_; }
2994 ZoneList<TextElement> elements_;
2999 class RegExpQuantifier final : public RegExpTree {
3001 enum QuantifierType { GREEDY, NON_GREEDY, POSSESSIVE };
3002 RegExpQuantifier(int min, int max, QuantifierType type, RegExpTree* body)
3006 min_match_(min * body->min_match()),
3007 quantifier_type_(type) {
3008 if (max > 0 && body->max_match() > kInfinity / max) {
3009 max_match_ = kInfinity;
3011 max_match_ = max * body->max_match();
3014 void* Accept(RegExpVisitor* visitor, void* data) override;
3015 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
3016 RegExpNode* on_success) override;
3017 static RegExpNode* ToNode(int min,
3021 RegExpCompiler* compiler,
3022 RegExpNode* on_success,
3023 bool not_at_start = false);
3024 RegExpQuantifier* AsQuantifier() override;
3025 Interval CaptureRegisters() override;
3026 bool IsQuantifier() override;
3027 int min_match() override { return min_match_; }
3028 int max_match() override { return max_match_; }
3029 int min() { return min_; }
3030 int max() { return max_; }
3031 bool is_possessive() { return quantifier_type_ == POSSESSIVE; }
3032 bool is_non_greedy() { return quantifier_type_ == NON_GREEDY; }
3033 bool is_greedy() { return quantifier_type_ == GREEDY; }
3034 RegExpTree* body() { return body_; }
3042 QuantifierType quantifier_type_;
3046 class RegExpCapture final : public RegExpTree {
3048 explicit RegExpCapture(RegExpTree* body, int index)
3049 : body_(body), index_(index) { }
3050 void* Accept(RegExpVisitor* visitor, void* data) override;
3051 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
3052 RegExpNode* on_success) override;
3053 static RegExpNode* ToNode(RegExpTree* body,
3055 RegExpCompiler* compiler,
3056 RegExpNode* on_success);
3057 RegExpCapture* AsCapture() override;
3058 bool IsAnchoredAtStart() override;
3059 bool IsAnchoredAtEnd() override;
3060 Interval CaptureRegisters() override;
3061 bool IsCapture() override;
3062 int min_match() override { return body_->min_match(); }
3063 int max_match() override { return body_->max_match(); }
3064 RegExpTree* body() { return body_; }
3065 int index() { return index_; }
3066 static int StartRegister(int index) { return index * 2; }
3067 static int EndRegister(int index) { return index * 2 + 1; }
3075 class RegExpLookahead final : public RegExpTree {
3077 RegExpLookahead(RegExpTree* body,
3082 is_positive_(is_positive),
3083 capture_count_(capture_count),
3084 capture_from_(capture_from) { }
3086 void* Accept(RegExpVisitor* visitor, void* data) override;
3087 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
3088 RegExpNode* on_success) override;
3089 RegExpLookahead* AsLookahead() override;
3090 Interval CaptureRegisters() override;
3091 bool IsLookahead() override;
3092 bool IsAnchoredAtStart() override;
3093 int min_match() override { return 0; }
3094 int max_match() override { return 0; }
3095 RegExpTree* body() { return body_; }
3096 bool is_positive() { return is_positive_; }
3097 int capture_count() { return capture_count_; }
3098 int capture_from() { return capture_from_; }
3108 class RegExpBackReference final : public RegExpTree {
3110 explicit RegExpBackReference(RegExpCapture* capture)
3111 : capture_(capture) { }
3112 void* Accept(RegExpVisitor* visitor, void* data) override;
3113 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
3114 RegExpNode* on_success) override;
3115 RegExpBackReference* AsBackReference() override;
3116 bool IsBackReference() override;
3117 int min_match() override { return 0; }
3118 int max_match() override { return capture_->max_match(); }
3119 int index() { return capture_->index(); }
3120 RegExpCapture* capture() { return capture_; }
3122 RegExpCapture* capture_;
3126 class RegExpEmpty final : public RegExpTree {
3129 void* Accept(RegExpVisitor* visitor, void* data) override;
3130 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
3131 RegExpNode* on_success) override;
3132 RegExpEmpty* AsEmpty() override;
3133 bool IsEmpty() override;
3134 int min_match() override { return 0; }
3135 int max_match() override { return 0; }
3139 // ----------------------------------------------------------------------------
3141 // - leaf node visitors are abstract.
3143 class AstVisitor BASE_EMBEDDED {
3146 virtual ~AstVisitor() {}
3148 // Stack overflow check and dynamic dispatch.
3149 virtual void Visit(AstNode* node) = 0;
3151 // Iteration left-to-right.
3152 virtual void VisitDeclarations(ZoneList<Declaration*>* declarations);
3153 virtual void VisitStatements(ZoneList<Statement*>* statements);
3154 virtual void VisitExpressions(ZoneList<Expression*>* expressions);
3156 // Individual AST nodes.
3157 #define DEF_VISIT(type) \
3158 virtual void Visit##type(type* node) = 0;
3159 AST_NODE_LIST(DEF_VISIT)
3164 #define DEFINE_AST_VISITOR_SUBCLASS_MEMBERS() \
3166 void Visit(AstNode* node) final { \
3167 if (!CheckStackOverflow()) node->Accept(this); \
3170 void SetStackOverflow() { stack_overflow_ = true; } \
3171 void ClearStackOverflow() { stack_overflow_ = false; } \
3172 bool HasStackOverflow() const { return stack_overflow_; } \
3174 bool CheckStackOverflow() { \
3175 if (stack_overflow_) return true; \
3176 StackLimitCheck check(isolate_); \
3177 if (!check.HasOverflowed()) return false; \
3178 stack_overflow_ = true; \
3183 void InitializeAstVisitor(Isolate* isolate, Zone* zone) { \
3184 isolate_ = isolate; \
3186 stack_overflow_ = false; \
3188 Zone* zone() { return zone_; } \
3189 Isolate* isolate() { return isolate_; } \
3191 Isolate* isolate_; \
3193 bool stack_overflow_
3196 // ----------------------------------------------------------------------------
3199 class AstNodeFactory final BASE_EMBEDDED {
3201 explicit AstNodeFactory(AstValueFactory* ast_value_factory)
3202 : local_zone_(ast_value_factory->zone()),
3203 parser_zone_(ast_value_factory->zone()),
3204 ast_value_factory_(ast_value_factory) {}
3206 VariableDeclaration* NewVariableDeclaration(
3207 VariableProxy* proxy, VariableMode mode, Scope* scope, int pos,
3208 bool is_class_declaration = false, int declaration_group_start = -1) {
3209 return new (parser_zone_)
3210 VariableDeclaration(parser_zone_, proxy, mode, scope, pos,
3211 is_class_declaration, declaration_group_start);
3214 FunctionDeclaration* NewFunctionDeclaration(VariableProxy* proxy,
3216 FunctionLiteral* fun,
3219 return new (parser_zone_)
3220 FunctionDeclaration(parser_zone_, proxy, mode, fun, scope, pos);
3223 ImportDeclaration* NewImportDeclaration(VariableProxy* proxy,
3224 const AstRawString* import_name,
3225 const AstRawString* module_specifier,
3226 Scope* scope, int pos) {
3227 return new (parser_zone_) ImportDeclaration(
3228 parser_zone_, proxy, import_name, module_specifier, scope, pos);
3231 ExportDeclaration* NewExportDeclaration(VariableProxy* proxy,
3234 return new (parser_zone_)
3235 ExportDeclaration(parser_zone_, proxy, scope, pos);
3238 Block* NewBlock(ZoneList<const AstRawString*>* labels, int capacity,
3239 bool ignore_completion_value, int pos) {
3240 return new (local_zone_)
3241 Block(local_zone_, labels, capacity, ignore_completion_value, pos);
3244 #define STATEMENT_WITH_LABELS(NodeType) \
3245 NodeType* New##NodeType(ZoneList<const AstRawString*>* labels, int pos) { \
3246 return new (local_zone_) NodeType(local_zone_, labels, pos); \
3248 STATEMENT_WITH_LABELS(DoWhileStatement)
3249 STATEMENT_WITH_LABELS(WhileStatement)
3250 STATEMENT_WITH_LABELS(ForStatement)
3251 STATEMENT_WITH_LABELS(SwitchStatement)
3252 #undef STATEMENT_WITH_LABELS
3254 ForEachStatement* NewForEachStatement(ForEachStatement::VisitMode visit_mode,
3255 ZoneList<const AstRawString*>* labels,
3257 switch (visit_mode) {
3258 case ForEachStatement::ENUMERATE: {
3259 return new (local_zone_) ForInStatement(local_zone_, labels, pos);
3261 case ForEachStatement::ITERATE: {
3262 return new (local_zone_) ForOfStatement(local_zone_, labels, pos);
3269 ExpressionStatement* NewExpressionStatement(Expression* expression, int pos) {
3270 return new (local_zone_) ExpressionStatement(local_zone_, expression, pos);
3273 ContinueStatement* NewContinueStatement(IterationStatement* target, int pos) {
3274 return new (local_zone_) ContinueStatement(local_zone_, target, pos);
3277 BreakStatement* NewBreakStatement(BreakableStatement* target, int pos) {
3278 return new (local_zone_) BreakStatement(local_zone_, target, pos);
3281 ReturnStatement* NewReturnStatement(Expression* expression, int pos) {
3282 return new (local_zone_) ReturnStatement(local_zone_, expression, pos);
3285 WithStatement* NewWithStatement(Scope* scope,
3286 Expression* expression,
3287 Statement* statement,
3289 return new (local_zone_)
3290 WithStatement(local_zone_, scope, expression, statement, pos);
3293 IfStatement* NewIfStatement(Expression* condition,
3294 Statement* then_statement,
3295 Statement* else_statement,
3297 return new (local_zone_) IfStatement(local_zone_, condition, then_statement,
3298 else_statement, pos);
3301 TryCatchStatement* NewTryCatchStatement(Block* try_block, Scope* scope,
3303 Block* catch_block, int pos) {
3304 return new (local_zone_) TryCatchStatement(local_zone_, try_block, scope,
3305 variable, catch_block, pos);
3308 TryFinallyStatement* NewTryFinallyStatement(Block* try_block,
3309 Block* finally_block, int pos) {
3310 return new (local_zone_)
3311 TryFinallyStatement(local_zone_, try_block, finally_block, pos);
3314 DebuggerStatement* NewDebuggerStatement(int pos) {
3315 return new (local_zone_) DebuggerStatement(local_zone_, pos);
3318 EmptyStatement* NewEmptyStatement(int pos) {
3319 return new (local_zone_) EmptyStatement(local_zone_, pos);
3322 SloppyBlockFunctionStatement* NewSloppyBlockFunctionStatement(
3323 Statement* statement, Scope* scope) {
3324 return new (local_zone_)
3325 SloppyBlockFunctionStatement(local_zone_, statement, scope);
3328 CaseClause* NewCaseClause(
3329 Expression* label, ZoneList<Statement*>* statements, int pos) {
3330 return new (local_zone_) CaseClause(local_zone_, label, statements, pos);
3333 Literal* NewStringLiteral(const AstRawString* string, int pos) {
3334 return new (local_zone_)
3335 Literal(local_zone_, ast_value_factory_->NewString(string), pos);
3338 // A JavaScript symbol (ECMA-262 edition 6).
3339 Literal* NewSymbolLiteral(const char* name, int pos) {
3340 return new (local_zone_)
3341 Literal(local_zone_, ast_value_factory_->NewSymbol(name), pos);
3344 Literal* NewNumberLiteral(double number, int pos, bool with_dot = false) {
3345 return new (local_zone_) Literal(
3346 local_zone_, ast_value_factory_->NewNumber(number, with_dot), pos);
3349 Literal* NewSmiLiteral(int number, int pos) {
3350 return new (local_zone_)
3351 Literal(local_zone_, ast_value_factory_->NewSmi(number), pos);
3354 Literal* NewBooleanLiteral(bool b, int pos) {
3355 return new (local_zone_)
3356 Literal(local_zone_, ast_value_factory_->NewBoolean(b), pos);
3359 Literal* NewNullLiteral(int pos) {
3360 return new (local_zone_)
3361 Literal(local_zone_, ast_value_factory_->NewNull(), pos);
3364 Literal* NewUndefinedLiteral(int pos) {
3365 return new (local_zone_)
3366 Literal(local_zone_, ast_value_factory_->NewUndefined(), pos);
3369 Literal* NewTheHoleLiteral(int pos) {
3370 return new (local_zone_)
3371 Literal(local_zone_, ast_value_factory_->NewTheHole(), pos);
3374 ObjectLiteral* NewObjectLiteral(
3375 ZoneList<ObjectLiteral::Property*>* properties,
3377 int boilerplate_properties,
3381 return new (local_zone_)
3382 ObjectLiteral(local_zone_, properties, literal_index,
3383 boilerplate_properties, has_function, is_strong, pos);
3386 ObjectLiteral::Property* NewObjectLiteralProperty(
3387 Expression* key, Expression* value, ObjectLiteralProperty::Kind kind,
3388 bool is_static, bool is_computed_name) {
3389 return new (local_zone_)
3390 ObjectLiteral::Property(key, value, kind, is_static, is_computed_name);
3393 ObjectLiteral::Property* NewObjectLiteralProperty(Expression* key,
3396 bool is_computed_name) {
3397 return new (local_zone_) ObjectLiteral::Property(
3398 ast_value_factory_, key, value, is_static, is_computed_name);
3401 RegExpLiteral* NewRegExpLiteral(const AstRawString* pattern,
3402 const AstRawString* flags,
3406 return new (local_zone_) RegExpLiteral(local_zone_, pattern, flags,
3407 literal_index, is_strong, pos);
3410 ArrayLiteral* NewArrayLiteral(ZoneList<Expression*>* values,
3414 return new (local_zone_)
3415 ArrayLiteral(local_zone_, values, -1, literal_index, is_strong, pos);
3418 ArrayLiteral* NewArrayLiteral(ZoneList<Expression*>* values,
3419 int first_spread_index, int literal_index,
3420 bool is_strong, int pos) {
3421 return new (local_zone_) ArrayLiteral(
3422 local_zone_, values, first_spread_index, literal_index, is_strong, pos);
3425 VariableProxy* NewVariableProxy(Variable* var,
3426 int start_position = RelocInfo::kNoPosition,
3427 int end_position = RelocInfo::kNoPosition) {
3428 return new (parser_zone_)
3429 VariableProxy(parser_zone_, var, start_position, end_position);
3432 VariableProxy* NewVariableProxy(const AstRawString* name,
3433 Variable::Kind variable_kind,
3434 int start_position = RelocInfo::kNoPosition,
3435 int end_position = RelocInfo::kNoPosition) {
3436 DCHECK_NOT_NULL(name);
3437 return new (parser_zone_) VariableProxy(parser_zone_, name, variable_kind,
3438 start_position, end_position);
3441 Property* NewProperty(Expression* obj, Expression* key, int pos) {
3442 return new (local_zone_) Property(local_zone_, obj, key, pos);
3445 Call* NewCall(Expression* expression,
3446 ZoneList<Expression*>* arguments,
3448 return new (local_zone_) Call(local_zone_, expression, arguments, pos);
3451 CallNew* NewCallNew(Expression* expression,
3452 ZoneList<Expression*>* arguments,
3454 return new (local_zone_) CallNew(local_zone_, expression, arguments, pos);
3457 CallRuntime* NewCallRuntime(Runtime::FunctionId id,
3458 ZoneList<Expression*>* arguments, int pos) {
3459 return new (local_zone_)
3460 CallRuntime(local_zone_, Runtime::FunctionForId(id), arguments, pos);
3463 CallRuntime* NewCallRuntime(const Runtime::Function* function,
3464 ZoneList<Expression*>* arguments, int pos) {
3465 return new (local_zone_) CallRuntime(local_zone_, function, arguments, pos);
3468 CallRuntime* NewCallRuntime(int context_index,
3469 ZoneList<Expression*>* arguments, int pos) {
3470 return new (local_zone_)
3471 CallRuntime(local_zone_, context_index, arguments, pos);
3474 UnaryOperation* NewUnaryOperation(Token::Value op,
3475 Expression* expression,
3477 return new (local_zone_) UnaryOperation(local_zone_, op, expression, pos);
3480 BinaryOperation* NewBinaryOperation(Token::Value op,
3484 return new (local_zone_) BinaryOperation(local_zone_, op, left, right, pos);
3487 CountOperation* NewCountOperation(Token::Value op,
3491 return new (local_zone_)
3492 CountOperation(local_zone_, op, is_prefix, expr, pos);
3495 CompareOperation* NewCompareOperation(Token::Value op,
3499 return new (local_zone_)
3500 CompareOperation(local_zone_, op, left, right, pos);
3503 Spread* NewSpread(Expression* expression, int pos) {
3504 return new (local_zone_) Spread(local_zone_, expression, pos);
3507 Conditional* NewConditional(Expression* condition,
3508 Expression* then_expression,
3509 Expression* else_expression,
3511 return new (local_zone_) Conditional(
3512 local_zone_, condition, then_expression, else_expression, position);
3515 Assignment* NewAssignment(Token::Value op,
3519 DCHECK(Token::IsAssignmentOp(op));
3520 Assignment* assign =
3521 new (local_zone_) Assignment(local_zone_, op, target, value, pos);
3522 if (assign->is_compound()) {
3523 DCHECK(Token::IsAssignmentOp(op));
3524 assign->binary_operation_ =
3525 NewBinaryOperation(assign->binary_op(), target, value, pos + 1);
3530 Yield* NewYield(Expression *generator_object,
3531 Expression* expression,
3532 Yield::Kind yield_kind,
3534 if (!expression) expression = NewUndefinedLiteral(pos);
3535 return new (local_zone_)
3536 Yield(local_zone_, generator_object, expression, yield_kind, pos);
3539 Throw* NewThrow(Expression* exception, int pos) {
3540 return new (local_zone_) Throw(local_zone_, exception, pos);
3543 FunctionLiteral* NewFunctionLiteral(
3544 const AstRawString* name, AstValueFactory* ast_value_factory,
3545 Scope* scope, ZoneList<Statement*>* body, int materialized_literal_count,
3546 int expected_property_count, int parameter_count,
3547 FunctionLiteral::ParameterFlag has_duplicate_parameters,
3548 FunctionLiteral::FunctionType function_type,
3549 FunctionLiteral::IsFunctionFlag is_function,
3550 FunctionLiteral::EagerCompileHint eager_compile_hint, FunctionKind kind,
3552 return new (parser_zone_) FunctionLiteral(
3553 parser_zone_, name, ast_value_factory, scope, body,
3554 materialized_literal_count, expected_property_count, parameter_count,
3555 function_type, has_duplicate_parameters, is_function,
3556 eager_compile_hint, kind, position);
3559 ClassLiteral* NewClassLiteral(const AstRawString* name, Scope* scope,
3560 VariableProxy* proxy, Expression* extends,
3561 FunctionLiteral* constructor,
3562 ZoneList<ObjectLiteral::Property*>* properties,
3563 int start_position, int end_position) {
3564 return new (parser_zone_)
3565 ClassLiteral(parser_zone_, name, scope, proxy, extends, constructor,
3566 properties, start_position, end_position);
3569 NativeFunctionLiteral* NewNativeFunctionLiteral(const AstRawString* name,
3570 v8::Extension* extension,
3572 return new (parser_zone_)
3573 NativeFunctionLiteral(parser_zone_, name, extension, pos);
3576 ThisFunction* NewThisFunction(int pos) {
3577 return new (local_zone_) ThisFunction(local_zone_, pos);
3580 SuperPropertyReference* NewSuperPropertyReference(VariableProxy* this_var,
3581 Expression* home_object,
3583 return new (parser_zone_)
3584 SuperPropertyReference(parser_zone_, this_var, home_object, pos);
3587 SuperCallReference* NewSuperCallReference(VariableProxy* this_var,
3588 VariableProxy* new_target_var,
3589 VariableProxy* this_function_var,
3591 return new (parser_zone_) SuperCallReference(
3592 parser_zone_, this_var, new_target_var, this_function_var, pos);
3595 EmptyParentheses* NewEmptyParentheses(int pos) {
3596 return new (local_zone_) EmptyParentheses(local_zone_, pos);
3599 Zone* zone() const { return local_zone_; }
3601 // Handles use of temporary zones when parsing inner function bodies.
3604 BodyScope(AstNodeFactory* factory, Zone* temp_zone, bool use_temp_zone)
3605 : factory_(factory), prev_zone_(factory->local_zone_) {
3606 if (use_temp_zone) {
3607 factory->local_zone_ = temp_zone;
3611 ~BodyScope() { factory_->local_zone_ = prev_zone_; }
3614 AstNodeFactory* factory_;
3619 // This zone may be deallocated upon returning from parsing a function body
3620 // which we can guarantee is not going to be compiled or have its AST
3622 // See ParseFunctionLiteral in parser.cc for preconditions.
3624 // ZoneObjects which need to persist until scope analysis must be allocated in
3625 // the parser-level zone.
3627 AstValueFactory* ast_value_factory_;
3631 } } // namespace v8::internal