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.
10 #include "src/assembler.h"
11 #include "src/ast-value-factory.h"
12 #include "src/bailout-reason.h"
13 #include "src/factory.h"
14 #include "src/isolate.h"
15 #include "src/jsregexp.h"
16 #include "src/list-inl.h"
17 #include "src/modules.h"
18 #include "src/runtime/runtime.h"
19 #include "src/small-pointer-list.h"
20 #include "src/smart-pointers.h"
21 #include "src/token.h"
22 #include "src/types.h"
23 #include "src/utils.h"
24 #include "src/variables.h"
29 // The abstract syntax tree is an intermediate, light-weight
30 // representation of the parsed JavaScript code suitable for
31 // compilation to native code.
33 // Nodes are allocated in a separate zone, which allows faster
34 // allocation and constant-time deallocation of the entire syntax
38 // ----------------------------------------------------------------------------
39 // Nodes of the abstract syntax tree. Only concrete classes are
42 #define DECLARATION_NODE_LIST(V) \
43 V(VariableDeclaration) \
44 V(FunctionDeclaration) \
45 V(ImportDeclaration) \
48 #define STATEMENT_NODE_LIST(V) \
50 V(ExpressionStatement) \
53 V(ContinueStatement) \
63 V(TryCatchStatement) \
64 V(TryFinallyStatement) \
67 #define EXPRESSION_NODE_LIST(V) \
70 V(NativeFunctionLiteral) \
93 #define AST_NODE_LIST(V) \
94 DECLARATION_NODE_LIST(V) \
95 STATEMENT_NODE_LIST(V) \
96 EXPRESSION_NODE_LIST(V)
98 // Forward declarations
103 class BreakableStatement;
105 class IterationStatement;
106 class MaterializedLiteral;
108 class TypeFeedbackOracle;
110 class RegExpAlternative;
111 class RegExpAssertion;
113 class RegExpBackReference;
115 class RegExpCharacterClass;
116 class RegExpCompiler;
117 class RegExpDisjunction;
119 class RegExpLookahead;
120 class RegExpQuantifier;
123 #define DEF_FORWARD_DECLARATION(type) class type;
124 AST_NODE_LIST(DEF_FORWARD_DECLARATION)
125 #undef DEF_FORWARD_DECLARATION
128 // Typedef only introduced to avoid unreadable code.
129 // Please do appreciate the required space in "> >".
130 typedef ZoneList<Handle<String> > ZoneStringList;
131 typedef ZoneList<Handle<Object> > ZoneObjectList;
134 #define DECLARE_NODE_TYPE(type) \
135 void Accept(AstVisitor* v) override; \
136 AstNode::NodeType node_type() const final { return AstNode::k##type; } \
137 friend class AstNodeFactory;
140 enum AstPropertiesFlag {
147 class FeedbackVectorRequirements {
149 FeedbackVectorRequirements(int slots, int ic_slots)
150 : slots_(slots), ic_slots_(ic_slots) {}
152 int slots() const { return slots_; }
153 int ic_slots() const { return ic_slots_; }
161 class VariableICSlotPair final {
163 VariableICSlotPair(Variable* variable, FeedbackVectorICSlot slot)
164 : variable_(variable), slot_(slot) {}
166 : variable_(NULL), slot_(FeedbackVectorICSlot::Invalid()) {}
168 Variable* variable() const { return variable_; }
169 FeedbackVectorICSlot slot() const { return slot_; }
173 FeedbackVectorICSlot slot_;
177 typedef List<VariableICSlotPair> ICSlotCache;
180 class AstProperties final BASE_EMBEDDED {
182 class Flags : public EnumSet<AstPropertiesFlag, int> {};
184 explicit AstProperties(Zone* zone) : node_count_(0), spec_(zone) {}
186 Flags* flags() { return &flags_; }
187 int node_count() { return node_count_; }
188 void add_node_count(int count) { node_count_ += count; }
190 int slots() const { return spec_.slots(); }
191 void increase_slots(int count) { spec_.increase_slots(count); }
193 int ic_slots() const { return spec_.ic_slots(); }
194 void increase_ic_slots(int count) { spec_.increase_ic_slots(count); }
195 void SetKind(int ic_slot, Code::Kind kind) { spec_.SetKind(ic_slot, kind); }
196 const ZoneFeedbackVectorSpec* get_spec() const { return &spec_; }
201 ZoneFeedbackVectorSpec spec_;
205 class AstNode: public ZoneObject {
207 #define DECLARE_TYPE_ENUM(type) k##type,
209 AST_NODE_LIST(DECLARE_TYPE_ENUM)
212 #undef DECLARE_TYPE_ENUM
214 void* operator new(size_t size, Zone* zone) { return zone->New(size); }
216 explicit AstNode(int position): position_(position) {}
217 virtual ~AstNode() {}
219 virtual void Accept(AstVisitor* v) = 0;
220 virtual NodeType node_type() const = 0;
221 int position() const { return position_; }
223 // Type testing & conversion functions overridden by concrete subclasses.
224 #define DECLARE_NODE_FUNCTIONS(type) \
225 bool Is##type() const { return node_type() == AstNode::k##type; } \
227 return Is##type() ? reinterpret_cast<type*>(this) : NULL; \
229 const type* As##type() const { \
230 return Is##type() ? reinterpret_cast<const type*>(this) : NULL; \
232 AST_NODE_LIST(DECLARE_NODE_FUNCTIONS)
233 #undef DECLARE_NODE_FUNCTIONS
235 virtual BreakableStatement* AsBreakableStatement() { return NULL; }
236 virtual IterationStatement* AsIterationStatement() { return NULL; }
237 virtual MaterializedLiteral* AsMaterializedLiteral() { return NULL; }
239 // The interface for feedback slots, with default no-op implementations for
240 // node types which don't actually have this. Note that this is conceptually
241 // not really nice, but multiple inheritance would introduce yet another
242 // vtable entry per node, something we don't want for space reasons.
243 virtual FeedbackVectorRequirements ComputeFeedbackRequirements(
244 Isolate* isolate, const ICSlotCache* cache) {
245 return FeedbackVectorRequirements(0, 0);
247 virtual void SetFirstFeedbackSlot(FeedbackVectorSlot slot) { UNREACHABLE(); }
248 virtual void SetFirstFeedbackICSlot(FeedbackVectorICSlot slot,
249 ICSlotCache* cache) {
252 // Each ICSlot stores a kind of IC which the participating node should know.
253 virtual Code::Kind FeedbackICSlotKind(int index) {
255 return Code::NUMBER_OF_KINDS;
259 // Hidden to prevent accidental usage. It would have to load the
260 // current zone from the TLS.
261 void* operator new(size_t size);
263 friend class CaseClause; // Generates AST IDs.
269 class Statement : public AstNode {
271 explicit Statement(Zone* zone, int position) : AstNode(position) {}
273 bool IsEmpty() { return AsEmptyStatement() != NULL; }
274 virtual bool IsJump() const { return false; }
278 class SmallMapList final {
281 SmallMapList(int capacity, Zone* zone) : list_(capacity, zone) {}
283 void Reserve(int capacity, Zone* zone) { list_.Reserve(capacity, zone); }
284 void Clear() { list_.Clear(); }
285 void Sort() { list_.Sort(); }
287 bool is_empty() const { return list_.is_empty(); }
288 int length() const { return list_.length(); }
290 void AddMapIfMissing(Handle<Map> map, Zone* zone) {
291 if (!Map::TryUpdate(map).ToHandle(&map)) return;
292 for (int i = 0; i < length(); ++i) {
293 if (at(i).is_identical_to(map)) return;
298 void FilterForPossibleTransitions(Map* root_map) {
299 for (int i = list_.length() - 1; i >= 0; i--) {
300 if (at(i)->FindRootMap() != root_map) {
301 list_.RemoveElement(list_.at(i));
306 void Add(Handle<Map> handle, Zone* zone) {
307 list_.Add(handle.location(), zone);
310 Handle<Map> at(int i) const {
311 return Handle<Map>(list_.at(i));
314 Handle<Map> first() const { return at(0); }
315 Handle<Map> last() const { return at(length() - 1); }
318 // The list stores pointers to Map*, that is Map**, so it's GC safe.
319 SmallPointerList<Map*> list_;
321 DISALLOW_COPY_AND_ASSIGN(SmallMapList);
325 class Expression : public AstNode {
328 // Not assigned a context yet, or else will not be visited during
331 // Evaluated for its side effects.
333 // Evaluated for its value (and side effects).
335 // Evaluated for control flow (and side effects).
339 virtual bool IsValidReferenceExpression() const { return false; }
341 // Helpers for ToBoolean conversion.
342 virtual bool ToBooleanIsTrue() const { return false; }
343 virtual bool ToBooleanIsFalse() const { return false; }
345 // Symbols that cannot be parsed as array indices are considered property
346 // names. We do not treat symbols that can be array indexes as property
347 // names because [] for string objects is handled only by keyed ICs.
348 virtual bool IsPropertyName() const { return false; }
350 // True iff the expression is a literal represented as a smi.
351 bool IsSmiLiteral() const;
353 // True iff the expression is a string literal.
354 bool IsStringLiteral() const;
356 // True iff the expression is the null literal.
357 bool IsNullLiteral() const;
359 // True if we can prove that the expression is the undefined literal.
360 bool IsUndefinedLiteral(Isolate* isolate) const;
362 // Expression type bounds
363 Bounds bounds() const { return bounds_; }
364 void set_bounds(Bounds bounds) { bounds_ = bounds; }
366 // Whether the expression is parenthesized
367 bool is_single_parenthesized() const {
368 return IsSingleParenthesizedField::decode(bit_field_);
370 bool is_multi_parenthesized() const {
371 return IsMultiParenthesizedField::decode(bit_field_);
373 void increase_parenthesization_level() {
374 bit_field_ = IsMultiParenthesizedField::update(bit_field_,
375 is_single_parenthesized());
376 bit_field_ = IsSingleParenthesizedField::update(bit_field_, true);
379 // Type feedback information for assignments and properties.
380 virtual bool IsMonomorphic() {
384 virtual SmallMapList* GetReceiverTypes() {
388 virtual KeyedAccessStoreMode GetStoreMode() const {
390 return STANDARD_STORE;
392 virtual IcCheckType GetKeyType() const {
397 // TODO(rossberg): this should move to its own AST node eventually.
398 virtual void RecordToBooleanTypeFeedback(TypeFeedbackOracle* oracle);
399 byte to_boolean_types() const {
400 return ToBooleanTypesField::decode(bit_field_);
403 void set_base_id(int id) { base_id_ = id; }
404 static int num_ids() { return parent_num_ids() + 2; }
405 BailoutId id() const { return BailoutId(local_id(0)); }
406 TypeFeedbackId test_id() const { return TypeFeedbackId(local_id(1)); }
409 Expression(Zone* zone, int pos)
411 base_id_(BailoutId::None().ToInt()),
412 bounds_(Bounds::Unbounded(zone)),
414 static int parent_num_ids() { return 0; }
415 void set_to_boolean_types(byte types) {
416 bit_field_ = ToBooleanTypesField::update(bit_field_, types);
419 int base_id() const {
420 DCHECK(!BailoutId(base_id_).IsNone());
425 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
429 class ToBooleanTypesField : public BitField16<byte, 0, 8> {};
430 class IsSingleParenthesizedField : public BitField16<bool, 8, 1> {};
431 class IsMultiParenthesizedField : public BitField16<bool, 9, 1> {};
433 // Ends with 16-bit field; deriving classes in turn begin with
434 // 16-bit fields for optimum packing efficiency.
438 class BreakableStatement : public Statement {
441 TARGET_FOR_ANONYMOUS,
442 TARGET_FOR_NAMED_ONLY
445 // The labels associated with this statement. May be NULL;
446 // if it is != NULL, guaranteed to contain at least one entry.
447 ZoneList<const AstRawString*>* labels() const { return labels_; }
449 // Type testing & conversion.
450 BreakableStatement* AsBreakableStatement() final { return this; }
453 Label* break_target() { return &break_target_; }
456 bool is_target_for_anonymous() const {
457 return breakable_type_ == TARGET_FOR_ANONYMOUS;
460 void set_base_id(int id) { base_id_ = id; }
461 static int num_ids() { return parent_num_ids() + 2; }
462 BailoutId EntryId() const { return BailoutId(local_id(0)); }
463 BailoutId ExitId() const { return BailoutId(local_id(1)); }
466 BreakableStatement(Zone* zone, ZoneList<const AstRawString*>* labels,
467 BreakableType breakable_type, int position)
468 : Statement(zone, position),
470 breakable_type_(breakable_type),
471 base_id_(BailoutId::None().ToInt()) {
472 DCHECK(labels == NULL || labels->length() > 0);
474 static int parent_num_ids() { return 0; }
476 int base_id() const {
477 DCHECK(!BailoutId(base_id_).IsNone());
482 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
484 ZoneList<const AstRawString*>* labels_;
485 BreakableType breakable_type_;
491 class Block final : public BreakableStatement {
493 DECLARE_NODE_TYPE(Block)
495 void AddStatement(Statement* statement, Zone* zone) {
496 statements_.Add(statement, zone);
499 ZoneList<Statement*>* statements() { return &statements_; }
500 bool is_initializer_block() const { return is_initializer_block_; }
502 static int num_ids() { return parent_num_ids() + 1; }
503 BailoutId DeclsId() const { return BailoutId(local_id(0)); }
505 bool IsJump() const override {
506 return !statements_.is_empty() && statements_.last()->IsJump()
507 && labels() == NULL; // Good enough as an approximation...
510 Scope* scope() const { return scope_; }
511 void set_scope(Scope* scope) { scope_ = scope; }
514 Block(Zone* zone, ZoneList<const AstRawString*>* labels, int capacity,
515 bool is_initializer_block, int pos)
516 : BreakableStatement(zone, labels, TARGET_FOR_NAMED_ONLY, pos),
517 statements_(capacity, zone),
518 is_initializer_block_(is_initializer_block),
520 static int parent_num_ids() { return BreakableStatement::num_ids(); }
523 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
525 ZoneList<Statement*> statements_;
526 bool is_initializer_block_;
531 class Declaration : public AstNode {
533 VariableProxy* proxy() const { return proxy_; }
534 VariableMode mode() const { return mode_; }
535 Scope* scope() const { return scope_; }
536 virtual InitializationFlag initialization() const = 0;
537 virtual bool IsInlineable() const;
540 Declaration(Zone* zone, VariableProxy* proxy, VariableMode mode, Scope* scope,
542 : AstNode(pos), mode_(mode), proxy_(proxy), scope_(scope) {
543 DCHECK(IsDeclaredVariableMode(mode));
548 VariableProxy* proxy_;
550 // Nested scope from which the declaration originated.
555 class VariableDeclaration final : public Declaration {
557 DECLARE_NODE_TYPE(VariableDeclaration)
559 InitializationFlag initialization() const override {
560 return mode() == VAR ? kCreatedInitialized : kNeedsInitialization;
563 bool is_class_declaration() const { return is_class_declaration_; }
565 // VariableDeclarations can be grouped into consecutive declaration
566 // groups. Each VariableDeclaration is associated with the start position of
567 // the group it belongs to. The positions are used for strong mode scope
568 // checks for classes and functions.
569 int declaration_group_start() const { return declaration_group_start_; }
572 VariableDeclaration(Zone* zone, VariableProxy* proxy, VariableMode mode,
573 Scope* scope, int pos, bool is_class_declaration = false,
574 int declaration_group_start = -1)
575 : Declaration(zone, proxy, mode, scope, pos),
576 is_class_declaration_(is_class_declaration),
577 declaration_group_start_(declaration_group_start) {}
579 bool is_class_declaration_;
580 int declaration_group_start_;
584 class FunctionDeclaration final : public Declaration {
586 DECLARE_NODE_TYPE(FunctionDeclaration)
588 FunctionLiteral* fun() const { return fun_; }
589 InitializationFlag initialization() const override {
590 return kCreatedInitialized;
592 bool IsInlineable() const override;
595 FunctionDeclaration(Zone* zone,
596 VariableProxy* proxy,
598 FunctionLiteral* fun,
601 : Declaration(zone, proxy, mode, scope, pos),
603 DCHECK(mode == VAR || mode == LET || mode == CONST);
608 FunctionLiteral* fun_;
612 class ImportDeclaration final : public Declaration {
614 DECLARE_NODE_TYPE(ImportDeclaration)
616 const AstRawString* import_name() const { return import_name_; }
617 const AstRawString* module_specifier() const { return module_specifier_; }
618 void set_module_specifier(const AstRawString* module_specifier) {
619 DCHECK(module_specifier_ == NULL);
620 module_specifier_ = module_specifier;
622 InitializationFlag initialization() const override {
623 return kNeedsInitialization;
627 ImportDeclaration(Zone* zone, VariableProxy* proxy,
628 const AstRawString* import_name,
629 const AstRawString* module_specifier, Scope* scope, int pos)
630 : Declaration(zone, proxy, IMPORT, scope, pos),
631 import_name_(import_name),
632 module_specifier_(module_specifier) {}
635 const AstRawString* import_name_;
636 const AstRawString* module_specifier_;
640 class ExportDeclaration final : public Declaration {
642 DECLARE_NODE_TYPE(ExportDeclaration)
644 InitializationFlag initialization() const override {
645 return kCreatedInitialized;
649 ExportDeclaration(Zone* zone, VariableProxy* proxy, Scope* scope, int pos)
650 : Declaration(zone, proxy, LET, scope, pos) {}
654 class Module : public AstNode {
656 ModuleDescriptor* descriptor() const { return descriptor_; }
657 Block* body() const { return body_; }
660 Module(Zone* zone, int pos)
661 : AstNode(pos), descriptor_(ModuleDescriptor::New(zone)), body_(NULL) {}
662 Module(Zone* zone, ModuleDescriptor* descriptor, int pos, Block* body = NULL)
663 : AstNode(pos), descriptor_(descriptor), body_(body) {}
666 ModuleDescriptor* descriptor_;
671 class IterationStatement : public BreakableStatement {
673 // Type testing & conversion.
674 IterationStatement* AsIterationStatement() final { return this; }
676 Statement* body() const { return body_; }
678 static int num_ids() { return parent_num_ids() + 1; }
679 BailoutId OsrEntryId() const { return BailoutId(local_id(0)); }
680 virtual BailoutId ContinueId() const = 0;
681 virtual BailoutId StackCheckId() const = 0;
684 Label* continue_target() { return &continue_target_; }
687 IterationStatement(Zone* zone, ZoneList<const AstRawString*>* labels, int pos)
688 : BreakableStatement(zone, labels, TARGET_FOR_ANONYMOUS, pos),
690 static int parent_num_ids() { return BreakableStatement::num_ids(); }
691 void Initialize(Statement* body) { body_ = body; }
694 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
697 Label continue_target_;
701 class DoWhileStatement final : public IterationStatement {
703 DECLARE_NODE_TYPE(DoWhileStatement)
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() + 2; }
713 BailoutId ContinueId() const override { return BailoutId(local_id(0)); }
714 BailoutId StackCheckId() const override { return BackEdgeId(); }
715 BailoutId BackEdgeId() const { return BailoutId(local_id(1)); }
718 DoWhileStatement(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 WhileStatement final : public IterationStatement {
731 DECLARE_NODE_TYPE(WhileStatement)
733 void Initialize(Expression* cond, Statement* body) {
734 IterationStatement::Initialize(body);
738 Expression* cond() const { return cond_; }
740 static int num_ids() { return parent_num_ids() + 1; }
741 BailoutId ContinueId() const override { return EntryId(); }
742 BailoutId StackCheckId() const override { return BodyId(); }
743 BailoutId BodyId() const { return BailoutId(local_id(0)); }
746 WhileStatement(Zone* zone, ZoneList<const AstRawString*>* labels, int pos)
747 : IterationStatement(zone, labels, pos), cond_(NULL) {}
748 static int parent_num_ids() { return IterationStatement::num_ids(); }
751 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
757 class ForStatement final : public IterationStatement {
759 DECLARE_NODE_TYPE(ForStatement)
761 void Initialize(Statement* init,
765 IterationStatement::Initialize(body);
771 Statement* init() const { return init_; }
772 Expression* cond() const { return cond_; }
773 Statement* next() const { return next_; }
775 static int num_ids() { return parent_num_ids() + 2; }
776 BailoutId ContinueId() const override { return BailoutId(local_id(0)); }
777 BailoutId StackCheckId() const override { return BodyId(); }
778 BailoutId BodyId() const { return BailoutId(local_id(1)); }
781 ForStatement(Zone* zone, ZoneList<const AstRawString*>* labels, int pos)
782 : IterationStatement(zone, labels, pos),
786 static int parent_num_ids() { return IterationStatement::num_ids(); }
789 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
797 class ForEachStatement : public IterationStatement {
800 ENUMERATE, // for (each in subject) body;
801 ITERATE // for (each of subject) body;
804 void Initialize(Expression* each, Expression* subject, Statement* body) {
805 IterationStatement::Initialize(body);
810 Expression* each() const { return each_; }
811 Expression* subject() const { return subject_; }
814 ForEachStatement(Zone* zone, ZoneList<const AstRawString*>* labels, int pos)
815 : IterationStatement(zone, labels, pos), each_(NULL), subject_(NULL) {}
819 Expression* subject_;
823 class ForInStatement final : public ForEachStatement {
825 DECLARE_NODE_TYPE(ForInStatement)
827 Expression* enumerable() const {
831 // Type feedback information.
832 virtual FeedbackVectorRequirements ComputeFeedbackRequirements(
833 Isolate* isolate, const ICSlotCache* cache) override {
834 return FeedbackVectorRequirements(1, 0);
836 void SetFirstFeedbackSlot(FeedbackVectorSlot slot) override {
837 for_in_feedback_slot_ = slot;
840 FeedbackVectorSlot ForInFeedbackSlot() {
841 DCHECK(!for_in_feedback_slot_.IsInvalid());
842 return for_in_feedback_slot_;
845 enum ForInType { FAST_FOR_IN, SLOW_FOR_IN };
846 ForInType for_in_type() const { return for_in_type_; }
847 void set_for_in_type(ForInType type) { for_in_type_ = type; }
849 static int num_ids() { return parent_num_ids() + 6; }
850 BailoutId BodyId() const { return BailoutId(local_id(0)); }
851 BailoutId PrepareId() const { return BailoutId(local_id(1)); }
852 BailoutId EnumId() const { return BailoutId(local_id(2)); }
853 BailoutId ToObjectId() const { return BailoutId(local_id(3)); }
854 BailoutId FilterId() const { return BailoutId(local_id(4)); }
855 BailoutId AssignmentId() const { return BailoutId(local_id(5)); }
856 BailoutId ContinueId() const override { return EntryId(); }
857 BailoutId StackCheckId() const override { return BodyId(); }
860 ForInStatement(Zone* zone, ZoneList<const AstRawString*>* labels, int pos)
861 : ForEachStatement(zone, labels, pos),
862 for_in_type_(SLOW_FOR_IN),
863 for_in_feedback_slot_(FeedbackVectorSlot::Invalid()) {}
864 static int parent_num_ids() { return ForEachStatement::num_ids(); }
867 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
869 ForInType for_in_type_;
870 FeedbackVectorSlot for_in_feedback_slot_;
874 class ForOfStatement final : public ForEachStatement {
876 DECLARE_NODE_TYPE(ForOfStatement)
878 void Initialize(Expression* each,
881 Expression* assign_iterator,
882 Expression* next_result,
883 Expression* result_done,
884 Expression* assign_each) {
885 ForEachStatement::Initialize(each, subject, body);
886 assign_iterator_ = assign_iterator;
887 next_result_ = next_result;
888 result_done_ = result_done;
889 assign_each_ = assign_each;
892 Expression* iterable() const {
896 // iterator = subject[Symbol.iterator]()
897 Expression* assign_iterator() const {
898 return assign_iterator_;
901 // result = iterator.next() // with type check
902 Expression* next_result() const {
907 Expression* result_done() const {
911 // each = result.value
912 Expression* assign_each() const {
916 BailoutId ContinueId() const override { return EntryId(); }
917 BailoutId StackCheckId() const override { return BackEdgeId(); }
919 static int num_ids() { return parent_num_ids() + 1; }
920 BailoutId BackEdgeId() const { return BailoutId(local_id(0)); }
923 ForOfStatement(Zone* zone, ZoneList<const AstRawString*>* labels, int pos)
924 : ForEachStatement(zone, labels, pos),
925 assign_iterator_(NULL),
928 assign_each_(NULL) {}
929 static int parent_num_ids() { return ForEachStatement::num_ids(); }
932 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
934 Expression* assign_iterator_;
935 Expression* next_result_;
936 Expression* result_done_;
937 Expression* assign_each_;
941 class ExpressionStatement final : public Statement {
943 DECLARE_NODE_TYPE(ExpressionStatement)
945 void set_expression(Expression* e) { expression_ = e; }
946 Expression* expression() const { return expression_; }
947 bool IsJump() const override { return expression_->IsThrow(); }
950 ExpressionStatement(Zone* zone, Expression* expression, int pos)
951 : Statement(zone, pos), expression_(expression) { }
954 Expression* expression_;
958 class JumpStatement : public Statement {
960 bool IsJump() const final { return true; }
963 explicit JumpStatement(Zone* zone, int pos) : Statement(zone, pos) {}
967 class ContinueStatement final : public JumpStatement {
969 DECLARE_NODE_TYPE(ContinueStatement)
971 IterationStatement* target() const { return target_; }
974 explicit ContinueStatement(Zone* zone, IterationStatement* target, int pos)
975 : JumpStatement(zone, pos), target_(target) { }
978 IterationStatement* target_;
982 class BreakStatement final : public JumpStatement {
984 DECLARE_NODE_TYPE(BreakStatement)
986 BreakableStatement* target() const { return target_; }
989 explicit BreakStatement(Zone* zone, BreakableStatement* target, int pos)
990 : JumpStatement(zone, pos), target_(target) { }
993 BreakableStatement* target_;
997 class ReturnStatement final : public JumpStatement {
999 DECLARE_NODE_TYPE(ReturnStatement)
1001 Expression* expression() const { return expression_; }
1004 explicit ReturnStatement(Zone* zone, Expression* expression, int pos)
1005 : JumpStatement(zone, pos), expression_(expression) { }
1008 Expression* expression_;
1012 class WithStatement final : public Statement {
1014 DECLARE_NODE_TYPE(WithStatement)
1016 Scope* scope() { return scope_; }
1017 Expression* expression() const { return expression_; }
1018 Statement* statement() const { return statement_; }
1020 void set_base_id(int id) { base_id_ = id; }
1021 static int num_ids() { return parent_num_ids() + 1; }
1022 BailoutId EntryId() const { return BailoutId(local_id(0)); }
1025 WithStatement(Zone* zone, Scope* scope, Expression* expression,
1026 Statement* statement, int pos)
1027 : Statement(zone, pos),
1029 expression_(expression),
1030 statement_(statement),
1031 base_id_(BailoutId::None().ToInt()) {}
1032 static int parent_num_ids() { return 0; }
1034 int base_id() const {
1035 DCHECK(!BailoutId(base_id_).IsNone());
1040 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
1043 Expression* expression_;
1044 Statement* statement_;
1049 class CaseClause final : public Expression {
1051 DECLARE_NODE_TYPE(CaseClause)
1053 bool is_default() const { return label_ == NULL; }
1054 Expression* label() const {
1055 CHECK(!is_default());
1058 Label* body_target() { return &body_target_; }
1059 ZoneList<Statement*>* statements() const { return statements_; }
1061 static int num_ids() { return parent_num_ids() + 2; }
1062 BailoutId EntryId() const { return BailoutId(local_id(0)); }
1063 TypeFeedbackId CompareId() { return TypeFeedbackId(local_id(1)); }
1065 Type* compare_type() { return compare_type_; }
1066 void set_compare_type(Type* type) { compare_type_ = type; }
1069 static int parent_num_ids() { return Expression::num_ids(); }
1072 CaseClause(Zone* zone, Expression* label, ZoneList<Statement*>* statements,
1074 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
1078 ZoneList<Statement*>* statements_;
1079 Type* compare_type_;
1083 class SwitchStatement final : public BreakableStatement {
1085 DECLARE_NODE_TYPE(SwitchStatement)
1087 void Initialize(Expression* tag, ZoneList<CaseClause*>* cases) {
1092 Expression* tag() const { return tag_; }
1093 ZoneList<CaseClause*>* cases() const { return cases_; }
1096 SwitchStatement(Zone* zone, ZoneList<const AstRawString*>* labels, int pos)
1097 : BreakableStatement(zone, labels, TARGET_FOR_ANONYMOUS, pos),
1103 ZoneList<CaseClause*>* cases_;
1107 // If-statements always have non-null references to their then- and
1108 // else-parts. When parsing if-statements with no explicit else-part,
1109 // the parser implicitly creates an empty statement. Use the
1110 // HasThenStatement() and HasElseStatement() functions to check if a
1111 // given if-statement has a then- or an else-part containing code.
1112 class IfStatement final : public Statement {
1114 DECLARE_NODE_TYPE(IfStatement)
1116 bool HasThenStatement() const { return !then_statement()->IsEmpty(); }
1117 bool HasElseStatement() const { return !else_statement()->IsEmpty(); }
1119 Expression* condition() const { return condition_; }
1120 Statement* then_statement() const { return then_statement_; }
1121 Statement* else_statement() const { return else_statement_; }
1123 bool IsJump() const override {
1124 return HasThenStatement() && then_statement()->IsJump()
1125 && HasElseStatement() && else_statement()->IsJump();
1128 void set_base_id(int id) { base_id_ = id; }
1129 static int num_ids() { return parent_num_ids() + 3; }
1130 BailoutId IfId() const { return BailoutId(local_id(0)); }
1131 BailoutId ThenId() const { return BailoutId(local_id(1)); }
1132 BailoutId ElseId() const { return BailoutId(local_id(2)); }
1135 IfStatement(Zone* zone, Expression* condition, Statement* then_statement,
1136 Statement* else_statement, int pos)
1137 : Statement(zone, pos),
1138 condition_(condition),
1139 then_statement_(then_statement),
1140 else_statement_(else_statement),
1141 base_id_(BailoutId::None().ToInt()) {}
1142 static int parent_num_ids() { return 0; }
1144 int base_id() const {
1145 DCHECK(!BailoutId(base_id_).IsNone());
1150 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
1152 Expression* condition_;
1153 Statement* then_statement_;
1154 Statement* else_statement_;
1159 class TryStatement : public Statement {
1161 int index() const { return index_; }
1162 Block* try_block() const { return try_block_; }
1165 TryStatement(Zone* zone, int index, Block* try_block, int pos)
1166 : Statement(zone, pos), index_(index), try_block_(try_block) {}
1169 // Unique (per-function) index of this handler. This is not an AST ID.
1176 class TryCatchStatement final : public TryStatement {
1178 DECLARE_NODE_TYPE(TryCatchStatement)
1180 Scope* scope() { return scope_; }
1181 Variable* variable() { return variable_; }
1182 Block* catch_block() const { return catch_block_; }
1185 TryCatchStatement(Zone* zone,
1192 : TryStatement(zone, index, try_block, pos),
1194 variable_(variable),
1195 catch_block_(catch_block) {
1200 Variable* variable_;
1201 Block* catch_block_;
1205 class TryFinallyStatement final : public TryStatement {
1207 DECLARE_NODE_TYPE(TryFinallyStatement)
1209 Block* finally_block() const { return finally_block_; }
1212 TryFinallyStatement(
1213 Zone* zone, int index, Block* try_block, Block* finally_block, int pos)
1214 : TryStatement(zone, index, try_block, pos),
1215 finally_block_(finally_block) { }
1218 Block* finally_block_;
1222 class DebuggerStatement final : public Statement {
1224 DECLARE_NODE_TYPE(DebuggerStatement)
1226 void set_base_id(int id) { base_id_ = id; }
1227 static int num_ids() { return parent_num_ids() + 1; }
1228 BailoutId DebugBreakId() const { return BailoutId(local_id(0)); }
1231 explicit DebuggerStatement(Zone* zone, int pos)
1232 : Statement(zone, pos), base_id_(BailoutId::None().ToInt()) {}
1233 static int parent_num_ids() { return 0; }
1235 int base_id() const {
1236 DCHECK(!BailoutId(base_id_).IsNone());
1241 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
1247 class EmptyStatement final : public Statement {
1249 DECLARE_NODE_TYPE(EmptyStatement)
1252 explicit EmptyStatement(Zone* zone, int pos): Statement(zone, pos) {}
1256 class Literal final : public Expression {
1258 DECLARE_NODE_TYPE(Literal)
1260 bool IsPropertyName() const override { return value_->IsPropertyName(); }
1262 Handle<String> AsPropertyName() {
1263 DCHECK(IsPropertyName());
1264 return Handle<String>::cast(value());
1267 const AstRawString* AsRawPropertyName() {
1268 DCHECK(IsPropertyName());
1269 return value_->AsString();
1272 bool ToBooleanIsTrue() const override { return value()->BooleanValue(); }
1273 bool ToBooleanIsFalse() const override { return !value()->BooleanValue(); }
1275 Handle<Object> value() const { return value_->value(); }
1276 const AstValue* raw_value() const { return value_; }
1278 // Support for using Literal as a HashMap key. NOTE: Currently, this works
1279 // only for string and number literals!
1281 static bool Match(void* literal1, void* literal2);
1283 static int num_ids() { return parent_num_ids() + 1; }
1284 TypeFeedbackId LiteralFeedbackId() const {
1285 return TypeFeedbackId(local_id(0));
1289 Literal(Zone* zone, const AstValue* value, int position)
1290 : Expression(zone, position), value_(value) {}
1291 static int parent_num_ids() { return Expression::num_ids(); }
1294 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
1296 const AstValue* value_;
1300 // Base class for literals that needs space in the corresponding JSFunction.
1301 class MaterializedLiteral : public Expression {
1303 virtual MaterializedLiteral* AsMaterializedLiteral() { return this; }
1305 int literal_index() { return literal_index_; }
1308 // only callable after initialization.
1309 DCHECK(depth_ >= 1);
1314 MaterializedLiteral(Zone* zone, int literal_index, int pos)
1315 : Expression(zone, pos),
1316 literal_index_(literal_index),
1320 // A materialized literal is simple if the values consist of only
1321 // constants and simple object and array literals.
1322 bool is_simple() const { return is_simple_; }
1323 void set_is_simple(bool is_simple) { is_simple_ = is_simple; }
1324 friend class CompileTimeValue;
1326 void set_depth(int depth) {
1331 // Populate the constant properties/elements fixed array.
1332 void BuildConstants(Isolate* isolate);
1333 friend class ArrayLiteral;
1334 friend class ObjectLiteral;
1336 // If the expression is a literal, return the literal value;
1337 // if the expression is a materialized literal and is simple return a
1338 // compile time value as encoded by CompileTimeValue::GetValue().
1339 // Otherwise, return undefined literal as the placeholder
1340 // in the object literal boilerplate.
1341 Handle<Object> GetBoilerplateValue(Expression* expression, Isolate* isolate);
1350 // Property is used for passing information
1351 // about an object literal's properties from the parser
1352 // to the code generator.
1353 class ObjectLiteralProperty final : public ZoneObject {
1356 CONSTANT, // Property with constant value (compile time).
1357 COMPUTED, // Property with computed value (execution time).
1358 MATERIALIZED_LITERAL, // Property value is a materialized literal.
1359 GETTER, SETTER, // Property is an accessor function.
1360 PROTOTYPE // Property is __proto__.
1363 Expression* key() { return key_; }
1364 Expression* value() { return value_; }
1365 Kind kind() { return kind_; }
1367 // Type feedback information.
1368 bool IsMonomorphic() { return !receiver_type_.is_null(); }
1369 Handle<Map> GetReceiverType() { return receiver_type_; }
1371 bool IsCompileTimeValue();
1373 void set_emit_store(bool emit_store);
1376 bool is_static() const { return is_static_; }
1377 bool is_computed_name() const { return is_computed_name_; }
1379 void set_receiver_type(Handle<Map> map) { receiver_type_ = map; }
1382 friend class AstNodeFactory;
1384 ObjectLiteralProperty(Expression* key, Expression* value, Kind kind,
1385 bool is_static, bool is_computed_name);
1386 ObjectLiteralProperty(AstValueFactory* ast_value_factory, Expression* key,
1387 Expression* value, bool is_static,
1388 bool is_computed_name);
1396 bool is_computed_name_;
1397 Handle<Map> receiver_type_;
1401 // An object literal has a boilerplate object that is used
1402 // for minimizing the work when constructing it at runtime.
1403 class ObjectLiteral final : public MaterializedLiteral {
1405 typedef ObjectLiteralProperty Property;
1407 DECLARE_NODE_TYPE(ObjectLiteral)
1409 Handle<FixedArray> constant_properties() const {
1410 return constant_properties_;
1412 int properties_count() const { return constant_properties_->length() / 2; }
1413 ZoneList<Property*>* properties() const { return properties_; }
1414 bool fast_elements() const { return fast_elements_; }
1415 bool may_store_doubles() const { return may_store_doubles_; }
1416 bool has_function() const { return has_function_; }
1417 bool has_elements() const { return has_elements_; }
1419 // Decide if a property should be in the object boilerplate.
1420 static bool IsBoilerplateProperty(Property* property);
1422 // Populate the constant properties fixed array.
1423 void BuildConstantProperties(Isolate* isolate);
1425 // Mark all computed expressions that are bound to a key that
1426 // is shadowed by a later occurrence of the same key. For the
1427 // marked expressions, no store code is emitted.
1428 void CalculateEmitStore(Zone* zone);
1430 // Assemble bitfield of flags for the CreateObjectLiteral helper.
1431 int ComputeFlags(bool disable_mementos = false) const {
1432 int flags = fast_elements() ? kFastElements : kNoFlags;
1433 flags |= has_function() ? kHasFunction : kNoFlags;
1434 if (depth() == 1 && !has_elements() && !may_store_doubles()) {
1435 flags |= kShallowProperties;
1437 if (disable_mementos) {
1438 flags |= kDisableMementos;
1446 kHasFunction = 1 << 1,
1447 kShallowProperties = 1 << 2,
1448 kDisableMementos = 1 << 3
1451 struct Accessors: public ZoneObject {
1452 Accessors() : getter(NULL), setter(NULL) {}
1457 BailoutId CreateLiteralId() const { return BailoutId(local_id(0)); }
1459 // Return an AST id for a property that is used in simulate instructions.
1460 BailoutId GetIdForProperty(int i) { return BailoutId(local_id(i + 1)); }
1462 // Unlike other AST nodes, this number of bailout IDs allocated for an
1463 // ObjectLiteral can vary, so num_ids() is not a static method.
1464 int num_ids() const { return parent_num_ids() + 1 + properties()->length(); }
1467 ObjectLiteral(Zone* zone, ZoneList<Property*>* properties, int literal_index,
1468 int boilerplate_properties, bool has_function, int pos)
1469 : MaterializedLiteral(zone, literal_index, pos),
1470 properties_(properties),
1471 boilerplate_properties_(boilerplate_properties),
1472 fast_elements_(false),
1473 has_elements_(false),
1474 may_store_doubles_(false),
1475 has_function_(has_function) {}
1476 static int parent_num_ids() { return MaterializedLiteral::num_ids(); }
1479 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
1480 Handle<FixedArray> constant_properties_;
1481 ZoneList<Property*>* properties_;
1482 int boilerplate_properties_;
1483 bool fast_elements_;
1485 bool may_store_doubles_;
1490 // Node for capturing a regexp literal.
1491 class RegExpLiteral final : public MaterializedLiteral {
1493 DECLARE_NODE_TYPE(RegExpLiteral)
1495 Handle<String> pattern() const { return pattern_->string(); }
1496 Handle<String> flags() const { return flags_->string(); }
1499 RegExpLiteral(Zone* zone, const AstRawString* pattern,
1500 const AstRawString* flags, int literal_index, int pos)
1501 : MaterializedLiteral(zone, literal_index, pos),
1508 const AstRawString* pattern_;
1509 const AstRawString* flags_;
1513 // An array literal has a literals object that is used
1514 // for minimizing the work when constructing it at runtime.
1515 class ArrayLiteral final : public MaterializedLiteral {
1517 DECLARE_NODE_TYPE(ArrayLiteral)
1519 Handle<FixedArray> constant_elements() const { return constant_elements_; }
1520 ElementsKind constant_elements_kind() const {
1521 DCHECK_EQ(2, constant_elements_->length());
1522 return static_cast<ElementsKind>(
1523 Smi::cast(constant_elements_->get(0))->value());
1526 ZoneList<Expression*>* values() const { return values_; }
1528 BailoutId CreateLiteralId() const { return BailoutId(local_id(0)); }
1530 // Return an AST id for an element that is used in simulate instructions.
1531 BailoutId GetIdForElement(int i) { return BailoutId(local_id(i + 1)); }
1533 // Unlike other AST nodes, this number of bailout IDs allocated for an
1534 // ArrayLiteral can vary, so num_ids() is not a static method.
1535 int num_ids() const { return parent_num_ids() + 1 + values()->length(); }
1537 // Populate the constant elements fixed array.
1538 void BuildConstantElements(Isolate* isolate);
1540 // Assemble bitfield of flags for the CreateArrayLiteral helper.
1541 int ComputeFlags(bool disable_mementos = false) const {
1542 int flags = depth() == 1 ? kShallowElements : kNoFlags;
1543 if (disable_mementos) {
1544 flags |= kDisableMementos;
1551 kShallowElements = 1,
1552 kDisableMementos = 1 << 1
1556 ArrayLiteral(Zone* zone, ZoneList<Expression*>* values, int literal_index,
1558 : MaterializedLiteral(zone, literal_index, pos), values_(values) {}
1559 static int parent_num_ids() { return MaterializedLiteral::num_ids(); }
1562 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
1564 Handle<FixedArray> constant_elements_;
1565 ZoneList<Expression*>* values_;
1569 class VariableProxy final : public Expression {
1571 DECLARE_NODE_TYPE(VariableProxy)
1573 bool IsValidReferenceExpression() const override { return !is_this(); }
1575 bool IsArguments() const { return is_resolved() && var()->is_arguments(); }
1577 Handle<String> name() const { return raw_name()->string(); }
1578 const AstRawString* raw_name() const {
1579 return is_resolved() ? var_->raw_name() : raw_name_;
1582 Variable* var() const {
1583 DCHECK(is_resolved());
1586 void set_var(Variable* v) {
1587 DCHECK(!is_resolved());
1592 bool is_this() const { return IsThisField::decode(bit_field_); }
1594 bool is_assigned() const { return IsAssignedField::decode(bit_field_); }
1595 void set_is_assigned() {
1596 bit_field_ = IsAssignedField::update(bit_field_, true);
1599 bool is_resolved() const { return IsResolvedField::decode(bit_field_); }
1600 void set_is_resolved() {
1601 bit_field_ = IsResolvedField::update(bit_field_, true);
1604 int end_position() const { return end_position_; }
1606 // Bind this proxy to the variable var.
1607 void BindTo(Variable* var);
1609 bool UsesVariableFeedbackSlot() const {
1610 return FLAG_vector_ics && (var()->IsUnallocated() || var()->IsLookupSlot());
1613 virtual FeedbackVectorRequirements ComputeFeedbackRequirements(
1614 Isolate* isolate, const ICSlotCache* cache) override;
1616 void SetFirstFeedbackICSlot(FeedbackVectorICSlot slot,
1617 ICSlotCache* cache) override;
1618 Code::Kind FeedbackICSlotKind(int index) override { return Code::LOAD_IC; }
1619 FeedbackVectorICSlot VariableFeedbackSlot() {
1620 DCHECK(!UsesVariableFeedbackSlot() || !variable_feedback_slot_.IsInvalid());
1621 return variable_feedback_slot_;
1624 static int num_ids() { return parent_num_ids() + 1; }
1625 BailoutId BeforeId() const { return BailoutId(local_id(0)); }
1628 VariableProxy(Zone* zone, Variable* var, int start_position,
1631 VariableProxy(Zone* zone, const AstRawString* name,
1632 Variable::Kind variable_kind, int start_position,
1634 static int parent_num_ids() { return Expression::num_ids(); }
1635 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
1637 class IsThisField : public BitField8<bool, 0, 1> {};
1638 class IsAssignedField : public BitField8<bool, 1, 1> {};
1639 class IsResolvedField : public BitField8<bool, 2, 1> {};
1641 // Start with 16-bit (or smaller) field, which should get packed together
1642 // with Expression's trailing 16-bit field.
1644 FeedbackVectorICSlot variable_feedback_slot_;
1646 const AstRawString* raw_name_; // if !is_resolved_
1647 Variable* var_; // if is_resolved_
1649 // Position is stored in the AstNode superclass, but VariableProxy needs to
1650 // know its end position too (for error messages). It cannot be inferred from
1651 // the variable name length because it can contain escapes.
1656 class Property final : public Expression {
1658 DECLARE_NODE_TYPE(Property)
1660 bool IsValidReferenceExpression() const override { return true; }
1662 Expression* obj() const { return obj_; }
1663 Expression* key() const { return key_; }
1665 static int num_ids() { return parent_num_ids() + 2; }
1666 BailoutId LoadId() const { return BailoutId(local_id(0)); }
1667 TypeFeedbackId PropertyFeedbackId() { return TypeFeedbackId(local_id(1)); }
1669 bool IsStringAccess() const {
1670 return IsStringAccessField::decode(bit_field_);
1673 // Type feedback information.
1674 bool IsMonomorphic() override { return receiver_types_.length() == 1; }
1675 SmallMapList* GetReceiverTypes() override { return &receiver_types_; }
1676 KeyedAccessStoreMode GetStoreMode() const override { return STANDARD_STORE; }
1677 IcCheckType GetKeyType() const override {
1678 return KeyTypeField::decode(bit_field_);
1680 bool IsUninitialized() const {
1681 return !is_for_call() && HasNoTypeInformation();
1683 bool HasNoTypeInformation() const {
1684 return GetInlineCacheState() == UNINITIALIZED;
1686 InlineCacheState GetInlineCacheState() const {
1687 return InlineCacheStateField::decode(bit_field_);
1689 void set_is_string_access(bool b) {
1690 bit_field_ = IsStringAccessField::update(bit_field_, b);
1692 void set_key_type(IcCheckType key_type) {
1693 bit_field_ = KeyTypeField::update(bit_field_, key_type);
1695 void set_inline_cache_state(InlineCacheState state) {
1696 bit_field_ = InlineCacheStateField::update(bit_field_, state);
1698 void mark_for_call() {
1699 bit_field_ = IsForCallField::update(bit_field_, true);
1701 bool is_for_call() const { return IsForCallField::decode(bit_field_); }
1703 bool IsSuperAccess() {
1704 return obj()->IsSuperReference();
1707 virtual FeedbackVectorRequirements ComputeFeedbackRequirements(
1708 Isolate* isolate, const ICSlotCache* cache) override {
1709 return FeedbackVectorRequirements(0, FLAG_vector_ics ? 1 : 0);
1711 void SetFirstFeedbackICSlot(FeedbackVectorICSlot slot,
1712 ICSlotCache* cache) override {
1713 property_feedback_slot_ = slot;
1715 Code::Kind FeedbackICSlotKind(int index) override {
1716 return key()->IsPropertyName() ? Code::LOAD_IC : Code::KEYED_LOAD_IC;
1719 FeedbackVectorICSlot PropertyFeedbackSlot() const {
1720 DCHECK(!FLAG_vector_ics || !property_feedback_slot_.IsInvalid());
1721 return property_feedback_slot_;
1725 Property(Zone* zone, Expression* obj, Expression* key, int pos)
1726 : Expression(zone, pos),
1727 bit_field_(IsForCallField::encode(false) |
1728 IsStringAccessField::encode(false) |
1729 InlineCacheStateField::encode(UNINITIALIZED)),
1730 property_feedback_slot_(FeedbackVectorICSlot::Invalid()),
1733 static int parent_num_ids() { return Expression::num_ids(); }
1736 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
1738 class IsForCallField : public BitField8<bool, 0, 1> {};
1739 class IsStringAccessField : public BitField8<bool, 1, 1> {};
1740 class KeyTypeField : public BitField8<IcCheckType, 2, 1> {};
1741 class InlineCacheStateField : public BitField8<InlineCacheState, 3, 4> {};
1743 FeedbackVectorICSlot property_feedback_slot_;
1746 SmallMapList receiver_types_;
1750 class Call final : public Expression {
1752 DECLARE_NODE_TYPE(Call)
1754 Expression* expression() const { return expression_; }
1755 ZoneList<Expression*>* arguments() const { return arguments_; }
1757 // Type feedback information.
1758 virtual FeedbackVectorRequirements ComputeFeedbackRequirements(
1759 Isolate* isolate, const ICSlotCache* cache) override;
1760 void SetFirstFeedbackICSlot(FeedbackVectorICSlot slot,
1761 ICSlotCache* cache) override {
1762 ic_slot_or_slot_ = slot.ToInt();
1764 void SetFirstFeedbackSlot(FeedbackVectorSlot slot) override {
1765 ic_slot_or_slot_ = slot.ToInt();
1767 Code::Kind FeedbackICSlotKind(int index) override { return Code::CALL_IC; }
1769 FeedbackVectorSlot CallFeedbackSlot() const {
1770 DCHECK(ic_slot_or_slot_ != FeedbackVectorSlot::Invalid().ToInt());
1771 return FeedbackVectorSlot(ic_slot_or_slot_);
1774 FeedbackVectorICSlot CallFeedbackICSlot() const {
1775 DCHECK(ic_slot_or_slot_ != FeedbackVectorICSlot::Invalid().ToInt());
1776 return FeedbackVectorICSlot(ic_slot_or_slot_);
1779 SmallMapList* GetReceiverTypes() override {
1780 if (expression()->IsProperty()) {
1781 return expression()->AsProperty()->GetReceiverTypes();
1786 bool IsMonomorphic() override {
1787 if (expression()->IsProperty()) {
1788 return expression()->AsProperty()->IsMonomorphic();
1790 return !target_.is_null();
1793 bool global_call() const {
1794 VariableProxy* proxy = expression_->AsVariableProxy();
1795 return proxy != NULL && proxy->var()->IsUnallocated();
1798 bool known_global_function() const {
1799 return global_call() && !target_.is_null();
1802 Handle<JSFunction> target() { return target_; }
1804 Handle<AllocationSite> allocation_site() { return allocation_site_; }
1806 void SetKnownGlobalTarget(Handle<JSFunction> target) {
1808 set_is_uninitialized(false);
1810 void set_target(Handle<JSFunction> target) { target_ = target; }
1811 void set_allocation_site(Handle<AllocationSite> site) {
1812 allocation_site_ = site;
1815 static int num_ids() { return parent_num_ids() + 2; }
1816 BailoutId ReturnId() const { return BailoutId(local_id(0)); }
1817 BailoutId EvalOrLookupId() const { return BailoutId(local_id(1)); }
1819 bool is_uninitialized() const {
1820 return IsUninitializedField::decode(bit_field_);
1822 void set_is_uninitialized(bool b) {
1823 bit_field_ = IsUninitializedField::update(bit_field_, b);
1835 // Helpers to determine how to handle the call.
1836 CallType GetCallType(Isolate* isolate) const;
1837 bool IsUsingCallFeedbackSlot(Isolate* isolate) const;
1838 bool IsUsingCallFeedbackICSlot(Isolate* isolate) const;
1841 // Used to assert that the FullCodeGenerator records the return site.
1842 bool return_is_recorded_;
1846 Call(Zone* zone, Expression* expression, ZoneList<Expression*>* arguments,
1848 : Expression(zone, pos),
1849 ic_slot_or_slot_(FeedbackVectorICSlot::Invalid().ToInt()),
1850 expression_(expression),
1851 arguments_(arguments),
1852 bit_field_(IsUninitializedField::encode(false)) {
1853 if (expression->IsProperty()) {
1854 expression->AsProperty()->mark_for_call();
1857 static int parent_num_ids() { return Expression::num_ids(); }
1860 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
1862 // We store this as an integer because we don't know if we have a slot or
1863 // an ic slot until scoping time.
1864 int ic_slot_or_slot_;
1865 Expression* expression_;
1866 ZoneList<Expression*>* arguments_;
1867 Handle<JSFunction> target_;
1868 Handle<AllocationSite> allocation_site_;
1869 class IsUninitializedField : public BitField8<bool, 0, 1> {};
1874 class CallNew final : public Expression {
1876 DECLARE_NODE_TYPE(CallNew)
1878 Expression* expression() const { return expression_; }
1879 ZoneList<Expression*>* arguments() const { return arguments_; }
1881 // Type feedback information.
1882 virtual FeedbackVectorRequirements ComputeFeedbackRequirements(
1883 Isolate* isolate, const ICSlotCache* cache) override {
1884 return FeedbackVectorRequirements(FLAG_pretenuring_call_new ? 2 : 1, 0);
1886 void SetFirstFeedbackSlot(FeedbackVectorSlot slot) override {
1887 callnew_feedback_slot_ = slot;
1890 FeedbackVectorSlot CallNewFeedbackSlot() {
1891 DCHECK(!callnew_feedback_slot_.IsInvalid());
1892 return callnew_feedback_slot_;
1894 FeedbackVectorSlot AllocationSiteFeedbackSlot() {
1895 DCHECK(FLAG_pretenuring_call_new);
1896 return CallNewFeedbackSlot().next();
1899 bool IsMonomorphic() override { return is_monomorphic_; }
1900 Handle<JSFunction> target() const { return target_; }
1901 Handle<AllocationSite> allocation_site() const {
1902 return allocation_site_;
1905 static int num_ids() { return parent_num_ids() + 1; }
1906 static int feedback_slots() { return 1; }
1907 BailoutId ReturnId() const { return BailoutId(local_id(0)); }
1909 void set_allocation_site(Handle<AllocationSite> site) {
1910 allocation_site_ = site;
1912 void set_is_monomorphic(bool monomorphic) { is_monomorphic_ = monomorphic; }
1913 void set_target(Handle<JSFunction> target) { target_ = target; }
1914 void SetKnownGlobalTarget(Handle<JSFunction> target) {
1916 is_monomorphic_ = true;
1920 CallNew(Zone* zone, Expression* expression, ZoneList<Expression*>* arguments,
1922 : Expression(zone, pos),
1923 expression_(expression),
1924 arguments_(arguments),
1925 is_monomorphic_(false),
1926 callnew_feedback_slot_(FeedbackVectorSlot::Invalid()) {}
1928 static int parent_num_ids() { return Expression::num_ids(); }
1931 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
1933 Expression* expression_;
1934 ZoneList<Expression*>* arguments_;
1935 bool is_monomorphic_;
1936 Handle<JSFunction> target_;
1937 Handle<AllocationSite> allocation_site_;
1938 FeedbackVectorSlot callnew_feedback_slot_;
1942 // The CallRuntime class does not represent any official JavaScript
1943 // language construct. Instead it is used to call a C or JS function
1944 // with a set of arguments. This is used from the builtins that are
1945 // implemented in JavaScript (see "v8natives.js").
1946 class CallRuntime final : public Expression {
1948 DECLARE_NODE_TYPE(CallRuntime)
1950 Handle<String> name() const { return raw_name_->string(); }
1951 const AstRawString* raw_name() const { return raw_name_; }
1952 const Runtime::Function* function() const { return function_; }
1953 ZoneList<Expression*>* arguments() const { return arguments_; }
1954 bool is_jsruntime() const { return function_ == NULL; }
1956 // Type feedback information.
1957 bool HasCallRuntimeFeedbackSlot() const {
1958 return FLAG_vector_ics && is_jsruntime();
1960 virtual FeedbackVectorRequirements ComputeFeedbackRequirements(
1961 Isolate* isolate, const ICSlotCache* cache) override {
1962 return FeedbackVectorRequirements(0, HasCallRuntimeFeedbackSlot() ? 1 : 0);
1964 void SetFirstFeedbackICSlot(FeedbackVectorICSlot slot,
1965 ICSlotCache* cache) override {
1966 callruntime_feedback_slot_ = slot;
1968 Code::Kind FeedbackICSlotKind(int index) override { return Code::LOAD_IC; }
1970 FeedbackVectorICSlot CallRuntimeFeedbackSlot() {
1971 DCHECK(!HasCallRuntimeFeedbackSlot() ||
1972 !callruntime_feedback_slot_.IsInvalid());
1973 return callruntime_feedback_slot_;
1976 static int num_ids() { return parent_num_ids() + 1; }
1977 TypeFeedbackId CallRuntimeFeedbackId() const {
1978 return TypeFeedbackId(local_id(0));
1982 CallRuntime(Zone* zone, const AstRawString* name,
1983 const Runtime::Function* function,
1984 ZoneList<Expression*>* arguments, int pos)
1985 : Expression(zone, pos),
1987 function_(function),
1988 arguments_(arguments),
1989 callruntime_feedback_slot_(FeedbackVectorICSlot::Invalid()) {}
1990 static int parent_num_ids() { return Expression::num_ids(); }
1993 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
1995 const AstRawString* raw_name_;
1996 const Runtime::Function* function_;
1997 ZoneList<Expression*>* arguments_;
1998 FeedbackVectorICSlot callruntime_feedback_slot_;
2002 class UnaryOperation final : public Expression {
2004 DECLARE_NODE_TYPE(UnaryOperation)
2006 Token::Value op() const { return op_; }
2007 Expression* expression() const { return expression_; }
2009 // For unary not (Token::NOT), the AST ids where true and false will
2010 // actually be materialized, respectively.
2011 static int num_ids() { return parent_num_ids() + 2; }
2012 BailoutId MaterializeTrueId() const { return BailoutId(local_id(0)); }
2013 BailoutId MaterializeFalseId() const { return BailoutId(local_id(1)); }
2015 virtual void RecordToBooleanTypeFeedback(TypeFeedbackOracle* oracle) override;
2018 UnaryOperation(Zone* zone, Token::Value op, Expression* expression, int pos)
2019 : Expression(zone, pos), op_(op), expression_(expression) {
2020 DCHECK(Token::IsUnaryOp(op));
2022 static int parent_num_ids() { return Expression::num_ids(); }
2025 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
2028 Expression* expression_;
2032 class BinaryOperation final : public Expression {
2034 DECLARE_NODE_TYPE(BinaryOperation)
2036 Token::Value op() const { return static_cast<Token::Value>(op_); }
2037 Expression* left() const { return left_; }
2038 Expression* right() const { return right_; }
2039 Handle<AllocationSite> allocation_site() const { return allocation_site_; }
2040 void set_allocation_site(Handle<AllocationSite> allocation_site) {
2041 allocation_site_ = allocation_site;
2044 // The short-circuit logical operations need an AST ID for their
2045 // right-hand subexpression.
2046 static int num_ids() { return parent_num_ids() + 2; }
2047 BailoutId RightId() const { return BailoutId(local_id(0)); }
2049 TypeFeedbackId BinaryOperationFeedbackId() const {
2050 return TypeFeedbackId(local_id(1));
2052 Maybe<int> fixed_right_arg() const {
2053 return has_fixed_right_arg_ ? Just(fixed_right_arg_value_) : Nothing<int>();
2055 void set_fixed_right_arg(Maybe<int> arg) {
2056 has_fixed_right_arg_ = arg.IsJust();
2057 if (arg.IsJust()) fixed_right_arg_value_ = arg.FromJust();
2060 virtual void RecordToBooleanTypeFeedback(TypeFeedbackOracle* oracle) override;
2063 BinaryOperation(Zone* zone, Token::Value op, Expression* left,
2064 Expression* right, int pos)
2065 : Expression(zone, pos),
2066 op_(static_cast<byte>(op)),
2067 has_fixed_right_arg_(false),
2068 fixed_right_arg_value_(0),
2071 DCHECK(Token::IsBinaryOp(op));
2073 static int parent_num_ids() { return Expression::num_ids(); }
2076 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
2078 const byte op_; // actually Token::Value
2079 // TODO(rossberg): the fixed arg should probably be represented as a Constant
2080 // type for the RHS. Currenty it's actually a Maybe<int>
2081 bool has_fixed_right_arg_;
2082 int fixed_right_arg_value_;
2085 Handle<AllocationSite> allocation_site_;
2089 class CountOperation final : public Expression {
2091 DECLARE_NODE_TYPE(CountOperation)
2093 bool is_prefix() const { return IsPrefixField::decode(bit_field_); }
2094 bool is_postfix() const { return !is_prefix(); }
2096 Token::Value op() const { return TokenField::decode(bit_field_); }
2097 Token::Value binary_op() {
2098 return (op() == Token::INC) ? Token::ADD : Token::SUB;
2101 Expression* expression() const { return expression_; }
2103 bool IsMonomorphic() override { return receiver_types_.length() == 1; }
2104 SmallMapList* GetReceiverTypes() override { return &receiver_types_; }
2105 IcCheckType GetKeyType() const override {
2106 return KeyTypeField::decode(bit_field_);
2108 KeyedAccessStoreMode GetStoreMode() const override {
2109 return StoreModeField::decode(bit_field_);
2111 Type* type() const { return type_; }
2112 void set_key_type(IcCheckType type) {
2113 bit_field_ = KeyTypeField::update(bit_field_, type);
2115 void set_store_mode(KeyedAccessStoreMode mode) {
2116 bit_field_ = StoreModeField::update(bit_field_, mode);
2118 void set_type(Type* type) { type_ = type; }
2120 static int num_ids() { return parent_num_ids() + 4; }
2121 BailoutId AssignmentId() const { return BailoutId(local_id(0)); }
2122 BailoutId ToNumberId() const { return BailoutId(local_id(1)); }
2123 TypeFeedbackId CountBinOpFeedbackId() const {
2124 return TypeFeedbackId(local_id(2));
2126 TypeFeedbackId CountStoreFeedbackId() const {
2127 return TypeFeedbackId(local_id(3));
2131 CountOperation(Zone* zone, Token::Value op, bool is_prefix, Expression* expr,
2133 : Expression(zone, pos),
2134 bit_field_(IsPrefixField::encode(is_prefix) |
2135 KeyTypeField::encode(ELEMENT) |
2136 StoreModeField::encode(STANDARD_STORE) |
2137 TokenField::encode(op)),
2139 expression_(expr) {}
2140 static int parent_num_ids() { return Expression::num_ids(); }
2143 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
2145 class IsPrefixField : public BitField16<bool, 0, 1> {};
2146 class KeyTypeField : public BitField16<IcCheckType, 1, 1> {};
2147 class StoreModeField : public BitField16<KeyedAccessStoreMode, 2, 4> {};
2148 class TokenField : public BitField16<Token::Value, 6, 8> {};
2150 // Starts with 16-bit field, which should get packed together with
2151 // Expression's trailing 16-bit field.
2152 uint16_t bit_field_;
2154 Expression* expression_;
2155 SmallMapList receiver_types_;
2159 class CompareOperation final : public Expression {
2161 DECLARE_NODE_TYPE(CompareOperation)
2163 Token::Value op() const { return op_; }
2164 Expression* left() const { return left_; }
2165 Expression* right() const { return right_; }
2167 // Type feedback information.
2168 static int num_ids() { return parent_num_ids() + 1; }
2169 TypeFeedbackId CompareOperationFeedbackId() const {
2170 return TypeFeedbackId(local_id(0));
2172 Type* combined_type() const { return combined_type_; }
2173 void set_combined_type(Type* type) { combined_type_ = type; }
2175 // Match special cases.
2176 bool IsLiteralCompareTypeof(Expression** expr, Handle<String>* check);
2177 bool IsLiteralCompareUndefined(Expression** expr, Isolate* isolate);
2178 bool IsLiteralCompareNull(Expression** expr);
2181 CompareOperation(Zone* zone, Token::Value op, Expression* left,
2182 Expression* right, int pos)
2183 : Expression(zone, pos),
2187 combined_type_(Type::None(zone)) {
2188 DCHECK(Token::IsCompareOp(op));
2190 static int parent_num_ids() { return Expression::num_ids(); }
2193 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
2199 Type* combined_type_;
2203 class Spread final : public Expression {
2205 DECLARE_NODE_TYPE(Spread)
2207 Expression* expression() const { return expression_; }
2209 static int num_ids() { return parent_num_ids(); }
2212 Spread(Zone* zone, Expression* expression, int pos)
2213 : Expression(zone, pos), expression_(expression) {}
2214 static int parent_num_ids() { return Expression::num_ids(); }
2217 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
2219 Expression* expression_;
2223 class Conditional final : public Expression {
2225 DECLARE_NODE_TYPE(Conditional)
2227 Expression* condition() const { return condition_; }
2228 Expression* then_expression() const { return then_expression_; }
2229 Expression* else_expression() const { return else_expression_; }
2231 static int num_ids() { return parent_num_ids() + 2; }
2232 BailoutId ThenId() const { return BailoutId(local_id(0)); }
2233 BailoutId ElseId() const { return BailoutId(local_id(1)); }
2236 Conditional(Zone* zone, Expression* condition, Expression* then_expression,
2237 Expression* else_expression, int position)
2238 : Expression(zone, position),
2239 condition_(condition),
2240 then_expression_(then_expression),
2241 else_expression_(else_expression) {}
2242 static int parent_num_ids() { return Expression::num_ids(); }
2245 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
2247 Expression* condition_;
2248 Expression* then_expression_;
2249 Expression* else_expression_;
2253 class Assignment final : public Expression {
2255 DECLARE_NODE_TYPE(Assignment)
2257 Assignment* AsSimpleAssignment() { return !is_compound() ? this : NULL; }
2259 Token::Value binary_op() const;
2261 Token::Value op() const { return TokenField::decode(bit_field_); }
2262 Expression* target() const { return target_; }
2263 Expression* value() const { return value_; }
2264 BinaryOperation* binary_operation() const { return binary_operation_; }
2266 // This check relies on the definition order of token in token.h.
2267 bool is_compound() const { return op() > Token::ASSIGN; }
2269 static int num_ids() { return parent_num_ids() + 2; }
2270 BailoutId AssignmentId() const { return BailoutId(local_id(0)); }
2272 // Type feedback information.
2273 TypeFeedbackId AssignmentFeedbackId() { return TypeFeedbackId(local_id(1)); }
2274 bool IsMonomorphic() override { return receiver_types_.length() == 1; }
2275 bool IsUninitialized() const {
2276 return IsUninitializedField::decode(bit_field_);
2278 bool HasNoTypeInformation() {
2279 return IsUninitializedField::decode(bit_field_);
2281 SmallMapList* GetReceiverTypes() override { return &receiver_types_; }
2282 IcCheckType GetKeyType() const override {
2283 return KeyTypeField::decode(bit_field_);
2285 KeyedAccessStoreMode GetStoreMode() const override {
2286 return StoreModeField::decode(bit_field_);
2288 void set_is_uninitialized(bool b) {
2289 bit_field_ = IsUninitializedField::update(bit_field_, b);
2291 void set_key_type(IcCheckType key_type) {
2292 bit_field_ = KeyTypeField::update(bit_field_, key_type);
2294 void set_store_mode(KeyedAccessStoreMode mode) {
2295 bit_field_ = StoreModeField::update(bit_field_, mode);
2299 Assignment(Zone* zone, Token::Value op, Expression* target, Expression* value,
2301 static int parent_num_ids() { return Expression::num_ids(); }
2304 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
2306 class IsUninitializedField : public BitField16<bool, 0, 1> {};
2307 class KeyTypeField : public BitField16<IcCheckType, 1, 1> {};
2308 class StoreModeField : public BitField16<KeyedAccessStoreMode, 2, 4> {};
2309 class TokenField : public BitField16<Token::Value, 6, 8> {};
2311 // Starts with 16-bit field, which should get packed together with
2312 // Expression's trailing 16-bit field.
2313 uint16_t bit_field_;
2314 Expression* target_;
2316 BinaryOperation* binary_operation_;
2317 SmallMapList receiver_types_;
2321 class Yield final : public Expression {
2323 DECLARE_NODE_TYPE(Yield)
2326 kInitial, // The initial yield that returns the unboxed generator object.
2327 kSuspend, // A normal yield: { value: EXPRESSION, done: false }
2328 kDelegating, // A yield*.
2329 kFinal // A return: { value: EXPRESSION, done: true }
2332 Expression* generator_object() const { return generator_object_; }
2333 Expression* expression() const { return expression_; }
2334 Kind yield_kind() const { return yield_kind_; }
2336 // Delegating yield surrounds the "yield" in a "try/catch". This index
2337 // locates the catch handler in the handler table, and is equivalent to
2338 // TryCatchStatement::index().
2340 DCHECK_EQ(kDelegating, yield_kind());
2343 void set_index(int index) {
2344 DCHECK_EQ(kDelegating, yield_kind());
2348 // Type feedback information.
2349 bool HasFeedbackSlots() const {
2350 return FLAG_vector_ics && (yield_kind() == kDelegating);
2352 virtual FeedbackVectorRequirements ComputeFeedbackRequirements(
2353 Isolate* isolate, const ICSlotCache* cache) override {
2354 return FeedbackVectorRequirements(0, HasFeedbackSlots() ? 3 : 0);
2356 void SetFirstFeedbackICSlot(FeedbackVectorICSlot slot,
2357 ICSlotCache* cache) override {
2358 yield_first_feedback_slot_ = slot;
2360 Code::Kind FeedbackICSlotKind(int index) override {
2361 return index == 0 ? Code::KEYED_LOAD_IC : Code::LOAD_IC;
2364 FeedbackVectorICSlot KeyedLoadFeedbackSlot() {
2365 DCHECK(!HasFeedbackSlots() || !yield_first_feedback_slot_.IsInvalid());
2366 return yield_first_feedback_slot_;
2369 FeedbackVectorICSlot DoneFeedbackSlot() {
2370 return KeyedLoadFeedbackSlot().next();
2373 FeedbackVectorICSlot ValueFeedbackSlot() { return DoneFeedbackSlot().next(); }
2376 Yield(Zone* zone, Expression* generator_object, Expression* expression,
2377 Kind yield_kind, int pos)
2378 : Expression(zone, pos),
2379 generator_object_(generator_object),
2380 expression_(expression),
2381 yield_kind_(yield_kind),
2383 yield_first_feedback_slot_(FeedbackVectorICSlot::Invalid()) {}
2386 Expression* generator_object_;
2387 Expression* expression_;
2390 FeedbackVectorICSlot yield_first_feedback_slot_;
2394 class Throw final : public Expression {
2396 DECLARE_NODE_TYPE(Throw)
2398 Expression* exception() const { return exception_; }
2401 Throw(Zone* zone, Expression* exception, int pos)
2402 : Expression(zone, pos), exception_(exception) {}
2405 Expression* exception_;
2409 class FunctionLiteral final : public Expression {
2412 ANONYMOUS_EXPRESSION,
2417 enum ParameterFlag {
2418 kNoDuplicateParameters = 0,
2419 kHasDuplicateParameters = 1
2422 enum IsFunctionFlag {
2427 enum EagerCompileHint { kShouldEagerCompile, kShouldLazyCompile };
2429 enum ShouldBeUsedOnceHint { kShouldBeUsedOnce, kDontKnowIfShouldBeUsedOnce };
2431 enum ArityRestriction {
2437 DECLARE_NODE_TYPE(FunctionLiteral)
2439 Handle<String> name() const { return raw_name_->string(); }
2440 const AstRawString* raw_name() const { return raw_name_; }
2441 Scope* scope() const { return scope_; }
2442 ZoneList<Statement*>* body() const { return body_; }
2443 void set_function_token_position(int pos) { function_token_position_ = pos; }
2444 int function_token_position() const { return function_token_position_; }
2445 int start_position() const;
2446 int end_position() const;
2447 int SourceSize() const { return end_position() - start_position(); }
2448 bool is_expression() const { return IsExpression::decode(bitfield_); }
2449 bool is_anonymous() const { return IsAnonymous::decode(bitfield_); }
2450 LanguageMode language_mode() const;
2451 bool uses_super_property() const;
2453 static bool NeedsHomeObject(Expression* literal) {
2454 return literal != NULL && literal->IsFunctionLiteral() &&
2455 literal->AsFunctionLiteral()->uses_super_property();
2458 int materialized_literal_count() { return materialized_literal_count_; }
2459 int expected_property_count() { return expected_property_count_; }
2460 int handler_count() { return handler_count_; }
2461 int parameter_count() { return parameter_count_; }
2463 bool AllowsLazyCompilation();
2464 bool AllowsLazyCompilationWithoutContext();
2466 void InitializeSharedInfo(Handle<Code> code);
2468 Handle<String> debug_name() const {
2469 if (raw_name_ != NULL && !raw_name_->IsEmpty()) {
2470 return raw_name_->string();
2472 return inferred_name();
2475 Handle<String> inferred_name() const {
2476 if (!inferred_name_.is_null()) {
2477 DCHECK(raw_inferred_name_ == NULL);
2478 return inferred_name_;
2480 if (raw_inferred_name_ != NULL) {
2481 return raw_inferred_name_->string();
2484 return Handle<String>();
2487 // Only one of {set_inferred_name, set_raw_inferred_name} should be called.
2488 void set_inferred_name(Handle<String> inferred_name) {
2489 DCHECK(!inferred_name.is_null());
2490 inferred_name_ = inferred_name;
2491 DCHECK(raw_inferred_name_== NULL || raw_inferred_name_->IsEmpty());
2492 raw_inferred_name_ = NULL;
2495 void set_raw_inferred_name(const AstString* raw_inferred_name) {
2496 DCHECK(raw_inferred_name != NULL);
2497 raw_inferred_name_ = raw_inferred_name;
2498 DCHECK(inferred_name_.is_null());
2499 inferred_name_ = Handle<String>();
2502 // shared_info may be null if it's not cached in full code.
2503 Handle<SharedFunctionInfo> shared_info() { return shared_info_; }
2505 bool pretenure() { return Pretenure::decode(bitfield_); }
2506 void set_pretenure() { bitfield_ |= Pretenure::encode(true); }
2508 bool has_duplicate_parameters() {
2509 return HasDuplicateParameters::decode(bitfield_);
2512 bool is_function() { return IsFunction::decode(bitfield_) == kIsFunction; }
2514 // This is used as a heuristic on when to eagerly compile a function
2515 // literal. We consider the following constructs as hints that the
2516 // function will be called immediately:
2517 // - (function() { ... })();
2518 // - var x = function() { ... }();
2519 bool should_eager_compile() const {
2520 return EagerCompileHintBit::decode(bitfield_) == kShouldEagerCompile;
2522 void set_should_eager_compile() {
2523 bitfield_ = EagerCompileHintBit::update(bitfield_, kShouldEagerCompile);
2526 // A hint that we expect this function to be called (exactly) once,
2527 // i.e. we suspect it's an initialization function.
2528 bool should_be_used_once_hint() const {
2529 return ShouldBeUsedOnceHintBit::decode(bitfield_) == kShouldBeUsedOnce;
2531 void set_should_be_used_once_hint() {
2532 bitfield_ = ShouldBeUsedOnceHintBit::update(bitfield_, kShouldBeUsedOnce);
2535 FunctionKind kind() { return FunctionKindBits::decode(bitfield_); }
2537 int ast_node_count() { return ast_properties_.node_count(); }
2538 AstProperties::Flags* flags() { return ast_properties_.flags(); }
2539 void set_ast_properties(AstProperties* ast_properties) {
2540 ast_properties_ = *ast_properties;
2542 const ZoneFeedbackVectorSpec* feedback_vector_spec() const {
2543 return ast_properties_.get_spec();
2545 bool dont_optimize() { return dont_optimize_reason_ != kNoReason; }
2546 BailoutReason dont_optimize_reason() { return dont_optimize_reason_; }
2547 void set_dont_optimize_reason(BailoutReason reason) {
2548 dont_optimize_reason_ = reason;
2552 FunctionLiteral(Zone* zone, const AstRawString* name,
2553 AstValueFactory* ast_value_factory, Scope* scope,
2554 ZoneList<Statement*>* body, int materialized_literal_count,
2555 int expected_property_count, int handler_count,
2556 int parameter_count, FunctionType function_type,
2557 ParameterFlag has_duplicate_parameters,
2558 IsFunctionFlag is_function,
2559 EagerCompileHint eager_compile_hint, FunctionKind kind,
2561 : Expression(zone, position),
2565 raw_inferred_name_(ast_value_factory->empty_string()),
2566 ast_properties_(zone),
2567 dont_optimize_reason_(kNoReason),
2568 materialized_literal_count_(materialized_literal_count),
2569 expected_property_count_(expected_property_count),
2570 handler_count_(handler_count),
2571 parameter_count_(parameter_count),
2572 function_token_position_(RelocInfo::kNoPosition) {
2573 bitfield_ = IsExpression::encode(function_type != DECLARATION) |
2574 IsAnonymous::encode(function_type == ANONYMOUS_EXPRESSION) |
2575 Pretenure::encode(false) |
2576 HasDuplicateParameters::encode(has_duplicate_parameters) |
2577 IsFunction::encode(is_function) |
2578 EagerCompileHintBit::encode(eager_compile_hint) |
2579 FunctionKindBits::encode(kind) |
2580 ShouldBeUsedOnceHintBit::encode(kDontKnowIfShouldBeUsedOnce);
2581 DCHECK(IsValidFunctionKind(kind));
2585 const AstRawString* raw_name_;
2586 Handle<String> name_;
2587 Handle<SharedFunctionInfo> shared_info_;
2589 ZoneList<Statement*>* body_;
2590 const AstString* raw_inferred_name_;
2591 Handle<String> inferred_name_;
2592 AstProperties ast_properties_;
2593 BailoutReason dont_optimize_reason_;
2595 int materialized_literal_count_;
2596 int expected_property_count_;
2598 int parameter_count_;
2599 int function_token_position_;
2602 class IsExpression : public BitField<bool, 0, 1> {};
2603 class IsAnonymous : public BitField<bool, 1, 1> {};
2604 class Pretenure : public BitField<bool, 2, 1> {};
2605 class HasDuplicateParameters : public BitField<ParameterFlag, 3, 1> {};
2606 class IsFunction : public BitField<IsFunctionFlag, 4, 1> {};
2607 class EagerCompileHintBit : public BitField<EagerCompileHint, 5, 1> {};
2608 class FunctionKindBits : public BitField<FunctionKind, 6, 8> {};
2609 class ShouldBeUsedOnceHintBit : public BitField<ShouldBeUsedOnceHint, 15, 1> {
2614 class ClassLiteral final : public Expression {
2616 typedef ObjectLiteralProperty Property;
2618 DECLARE_NODE_TYPE(ClassLiteral)
2620 Handle<String> name() const { return raw_name_->string(); }
2621 const AstRawString* raw_name() const { return raw_name_; }
2622 Scope* scope() const { return scope_; }
2623 VariableProxy* class_variable_proxy() const { return class_variable_proxy_; }
2624 Expression* extends() const { return extends_; }
2625 FunctionLiteral* constructor() const { return constructor_; }
2626 ZoneList<Property*>* properties() const { return properties_; }
2627 int start_position() const { return position(); }
2628 int end_position() const { return end_position_; }
2630 BailoutId EntryId() const { return BailoutId(local_id(0)); }
2631 BailoutId DeclsId() const { return BailoutId(local_id(1)); }
2632 BailoutId ExitId() { return BailoutId(local_id(2)); }
2633 BailoutId CreateLiteralId() const { return BailoutId(local_id(3)); }
2635 // Return an AST id for a property that is used in simulate instructions.
2636 BailoutId GetIdForProperty(int i) { return BailoutId(local_id(i + 4)); }
2638 // Unlike other AST nodes, this number of bailout IDs allocated for an
2639 // ClassLiteral can vary, so num_ids() is not a static method.
2640 int num_ids() const { return parent_num_ids() + 4 + properties()->length(); }
2643 ClassLiteral(Zone* zone, const AstRawString* name, Scope* scope,
2644 VariableProxy* class_variable_proxy, Expression* extends,
2645 FunctionLiteral* constructor, ZoneList<Property*>* properties,
2646 int start_position, int end_position)
2647 : Expression(zone, start_position),
2650 class_variable_proxy_(class_variable_proxy),
2652 constructor_(constructor),
2653 properties_(properties),
2654 end_position_(end_position) {}
2655 static int parent_num_ids() { return Expression::num_ids(); }
2658 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
2660 const AstRawString* raw_name_;
2662 VariableProxy* class_variable_proxy_;
2663 Expression* extends_;
2664 FunctionLiteral* constructor_;
2665 ZoneList<Property*>* properties_;
2670 class NativeFunctionLiteral final : public Expression {
2672 DECLARE_NODE_TYPE(NativeFunctionLiteral)
2674 Handle<String> name() const { return name_->string(); }
2675 v8::Extension* extension() const { return extension_; }
2678 NativeFunctionLiteral(Zone* zone, const AstRawString* name,
2679 v8::Extension* extension, int pos)
2680 : Expression(zone, pos), name_(name), extension_(extension) {}
2683 const AstRawString* name_;
2684 v8::Extension* extension_;
2688 class ThisFunction final : public Expression {
2690 DECLARE_NODE_TYPE(ThisFunction)
2693 ThisFunction(Zone* zone, int pos) : Expression(zone, pos) {}
2697 class SuperReference final : public Expression {
2699 DECLARE_NODE_TYPE(SuperReference)
2701 VariableProxy* this_var() const { return this_var_; }
2703 static int num_ids() { return parent_num_ids() + 1; }
2704 TypeFeedbackId HomeObjectFeedbackId() { return TypeFeedbackId(local_id(0)); }
2706 // Type feedback information.
2707 virtual FeedbackVectorRequirements ComputeFeedbackRequirements(
2708 Isolate* isolate, const ICSlotCache* cache) override {
2709 return FeedbackVectorRequirements(0, FLAG_vector_ics ? 1 : 0);
2711 void SetFirstFeedbackICSlot(FeedbackVectorICSlot slot,
2712 ICSlotCache* cache) override {
2713 homeobject_feedback_slot_ = slot;
2715 Code::Kind FeedbackICSlotKind(int index) override { return Code::LOAD_IC; }
2717 FeedbackVectorICSlot HomeObjectFeedbackSlot() {
2718 DCHECK(!FLAG_vector_ics || !homeobject_feedback_slot_.IsInvalid());
2719 return homeobject_feedback_slot_;
2723 SuperReference(Zone* zone, VariableProxy* this_var, int pos)
2724 : Expression(zone, pos),
2725 this_var_(this_var),
2726 homeobject_feedback_slot_(FeedbackVectorICSlot::Invalid()) {
2727 DCHECK(this_var->is_this());
2729 static int parent_num_ids() { return Expression::num_ids(); }
2732 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
2734 VariableProxy* this_var_;
2735 FeedbackVectorICSlot homeobject_feedback_slot_;
2739 #undef DECLARE_NODE_TYPE
2742 // ----------------------------------------------------------------------------
2743 // Regular expressions
2746 class RegExpVisitor BASE_EMBEDDED {
2748 virtual ~RegExpVisitor() { }
2749 #define MAKE_CASE(Name) \
2750 virtual void* Visit##Name(RegExp##Name*, void* data) = 0;
2751 FOR_EACH_REG_EXP_TREE_TYPE(MAKE_CASE)
2756 class RegExpTree : public ZoneObject {
2758 static const int kInfinity = kMaxInt;
2759 virtual ~RegExpTree() {}
2760 virtual void* Accept(RegExpVisitor* visitor, void* data) = 0;
2761 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
2762 RegExpNode* on_success) = 0;
2763 virtual bool IsTextElement() { return false; }
2764 virtual bool IsAnchoredAtStart() { return false; }
2765 virtual bool IsAnchoredAtEnd() { return false; }
2766 virtual int min_match() = 0;
2767 virtual int max_match() = 0;
2768 // Returns the interval of registers used for captures within this
2770 virtual Interval CaptureRegisters() { return Interval::Empty(); }
2771 virtual void AppendToText(RegExpText* text, Zone* zone);
2772 std::ostream& Print(std::ostream& os, Zone* zone); // NOLINT
2773 #define MAKE_ASTYPE(Name) \
2774 virtual RegExp##Name* As##Name(); \
2775 virtual bool Is##Name();
2776 FOR_EACH_REG_EXP_TREE_TYPE(MAKE_ASTYPE)
2781 class RegExpDisjunction final : public RegExpTree {
2783 explicit RegExpDisjunction(ZoneList<RegExpTree*>* alternatives);
2784 void* Accept(RegExpVisitor* visitor, void* data) override;
2785 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
2786 RegExpNode* on_success) override;
2787 RegExpDisjunction* AsDisjunction() override;
2788 Interval CaptureRegisters() override;
2789 bool IsDisjunction() override;
2790 bool IsAnchoredAtStart() override;
2791 bool IsAnchoredAtEnd() override;
2792 int min_match() override { return min_match_; }
2793 int max_match() override { return max_match_; }
2794 ZoneList<RegExpTree*>* alternatives() { return alternatives_; }
2796 ZoneList<RegExpTree*>* alternatives_;
2802 class RegExpAlternative final : public RegExpTree {
2804 explicit RegExpAlternative(ZoneList<RegExpTree*>* nodes);
2805 void* Accept(RegExpVisitor* visitor, void* data) override;
2806 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
2807 RegExpNode* on_success) override;
2808 RegExpAlternative* AsAlternative() override;
2809 Interval CaptureRegisters() override;
2810 bool IsAlternative() override;
2811 bool IsAnchoredAtStart() override;
2812 bool IsAnchoredAtEnd() override;
2813 int min_match() override { return min_match_; }
2814 int max_match() override { return max_match_; }
2815 ZoneList<RegExpTree*>* nodes() { return nodes_; }
2817 ZoneList<RegExpTree*>* nodes_;
2823 class RegExpAssertion final : public RegExpTree {
2825 enum AssertionType {
2833 explicit RegExpAssertion(AssertionType type) : assertion_type_(type) { }
2834 void* Accept(RegExpVisitor* visitor, void* data) override;
2835 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
2836 RegExpNode* on_success) override;
2837 RegExpAssertion* AsAssertion() override;
2838 bool IsAssertion() override;
2839 bool IsAnchoredAtStart() override;
2840 bool IsAnchoredAtEnd() override;
2841 int min_match() override { return 0; }
2842 int max_match() override { return 0; }
2843 AssertionType assertion_type() { return assertion_type_; }
2845 AssertionType assertion_type_;
2849 class CharacterSet final BASE_EMBEDDED {
2851 explicit CharacterSet(uc16 standard_set_type)
2853 standard_set_type_(standard_set_type) {}
2854 explicit CharacterSet(ZoneList<CharacterRange>* ranges)
2856 standard_set_type_(0) {}
2857 ZoneList<CharacterRange>* ranges(Zone* zone);
2858 uc16 standard_set_type() { return standard_set_type_; }
2859 void set_standard_set_type(uc16 special_set_type) {
2860 standard_set_type_ = special_set_type;
2862 bool is_standard() { return standard_set_type_ != 0; }
2863 void Canonicalize();
2865 ZoneList<CharacterRange>* ranges_;
2866 // If non-zero, the value represents a standard set (e.g., all whitespace
2867 // characters) without having to expand the ranges.
2868 uc16 standard_set_type_;
2872 class RegExpCharacterClass final : public RegExpTree {
2874 RegExpCharacterClass(ZoneList<CharacterRange>* ranges, bool is_negated)
2876 is_negated_(is_negated) { }
2877 explicit RegExpCharacterClass(uc16 type)
2879 is_negated_(false) { }
2880 void* Accept(RegExpVisitor* visitor, void* data) override;
2881 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
2882 RegExpNode* on_success) override;
2883 RegExpCharacterClass* AsCharacterClass() override;
2884 bool IsCharacterClass() override;
2885 bool IsTextElement() override { return true; }
2886 int min_match() override { return 1; }
2887 int max_match() override { return 1; }
2888 void AppendToText(RegExpText* text, Zone* zone) override;
2889 CharacterSet character_set() { return set_; }
2890 // TODO(lrn): Remove need for complex version if is_standard that
2891 // recognizes a mangled standard set and just do { return set_.is_special(); }
2892 bool is_standard(Zone* zone);
2893 // Returns a value representing the standard character set if is_standard()
2895 // Currently used values are:
2896 // s : unicode whitespace
2897 // S : unicode non-whitespace
2898 // w : ASCII word character (digit, letter, underscore)
2899 // W : non-ASCII word character
2901 // D : non-ASCII digit
2902 // . : non-unicode non-newline
2903 // * : All characters
2904 uc16 standard_type() { return set_.standard_set_type(); }
2905 ZoneList<CharacterRange>* ranges(Zone* zone) { return set_.ranges(zone); }
2906 bool is_negated() { return is_negated_; }
2914 class RegExpAtom final : public RegExpTree {
2916 explicit RegExpAtom(Vector<const uc16> data) : data_(data) { }
2917 void* Accept(RegExpVisitor* visitor, void* data) override;
2918 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
2919 RegExpNode* on_success) override;
2920 RegExpAtom* AsAtom() override;
2921 bool IsAtom() override;
2922 bool IsTextElement() override { return true; }
2923 int min_match() override { return data_.length(); }
2924 int max_match() override { return data_.length(); }
2925 void AppendToText(RegExpText* text, Zone* zone) override;
2926 Vector<const uc16> data() { return data_; }
2927 int length() { return data_.length(); }
2929 Vector<const uc16> data_;
2933 class RegExpText final : public RegExpTree {
2935 explicit RegExpText(Zone* zone) : elements_(2, zone), length_(0) {}
2936 void* Accept(RegExpVisitor* visitor, void* data) override;
2937 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
2938 RegExpNode* on_success) override;
2939 RegExpText* AsText() override;
2940 bool IsText() override;
2941 bool IsTextElement() override { return true; }
2942 int min_match() override { return length_; }
2943 int max_match() override { return length_; }
2944 void AppendToText(RegExpText* text, Zone* zone) override;
2945 void AddElement(TextElement elm, Zone* zone) {
2946 elements_.Add(elm, zone);
2947 length_ += elm.length();
2949 ZoneList<TextElement>* elements() { return &elements_; }
2951 ZoneList<TextElement> elements_;
2956 class RegExpQuantifier final : public RegExpTree {
2958 enum QuantifierType { GREEDY, NON_GREEDY, POSSESSIVE };
2959 RegExpQuantifier(int min, int max, QuantifierType type, RegExpTree* body)
2963 min_match_(min * body->min_match()),
2964 quantifier_type_(type) {
2965 if (max > 0 && body->max_match() > kInfinity / max) {
2966 max_match_ = kInfinity;
2968 max_match_ = max * body->max_match();
2971 void* Accept(RegExpVisitor* visitor, void* data) override;
2972 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
2973 RegExpNode* on_success) override;
2974 static RegExpNode* ToNode(int min,
2978 RegExpCompiler* compiler,
2979 RegExpNode* on_success,
2980 bool not_at_start = false);
2981 RegExpQuantifier* AsQuantifier() override;
2982 Interval CaptureRegisters() override;
2983 bool IsQuantifier() override;
2984 int min_match() override { return min_match_; }
2985 int max_match() override { return max_match_; }
2986 int min() { return min_; }
2987 int max() { return max_; }
2988 bool is_possessive() { return quantifier_type_ == POSSESSIVE; }
2989 bool is_non_greedy() { return quantifier_type_ == NON_GREEDY; }
2990 bool is_greedy() { return quantifier_type_ == GREEDY; }
2991 RegExpTree* body() { return body_; }
2999 QuantifierType quantifier_type_;
3003 class RegExpCapture final : public RegExpTree {
3005 explicit RegExpCapture(RegExpTree* body, int index)
3006 : body_(body), index_(index) { }
3007 void* Accept(RegExpVisitor* visitor, void* data) override;
3008 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
3009 RegExpNode* on_success) override;
3010 static RegExpNode* ToNode(RegExpTree* body,
3012 RegExpCompiler* compiler,
3013 RegExpNode* on_success);
3014 RegExpCapture* AsCapture() override;
3015 bool IsAnchoredAtStart() override;
3016 bool IsAnchoredAtEnd() override;
3017 Interval CaptureRegisters() override;
3018 bool IsCapture() override;
3019 int min_match() override { return body_->min_match(); }
3020 int max_match() override { return body_->max_match(); }
3021 RegExpTree* body() { return body_; }
3022 int index() { return index_; }
3023 static int StartRegister(int index) { return index * 2; }
3024 static int EndRegister(int index) { return index * 2 + 1; }
3032 class RegExpLookahead final : public RegExpTree {
3034 RegExpLookahead(RegExpTree* body,
3039 is_positive_(is_positive),
3040 capture_count_(capture_count),
3041 capture_from_(capture_from) { }
3043 void* Accept(RegExpVisitor* visitor, void* data) override;
3044 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
3045 RegExpNode* on_success) override;
3046 RegExpLookahead* AsLookahead() override;
3047 Interval CaptureRegisters() override;
3048 bool IsLookahead() override;
3049 bool IsAnchoredAtStart() override;
3050 int min_match() override { return 0; }
3051 int max_match() override { return 0; }
3052 RegExpTree* body() { return body_; }
3053 bool is_positive() { return is_positive_; }
3054 int capture_count() { return capture_count_; }
3055 int capture_from() { return capture_from_; }
3065 class RegExpBackReference final : public RegExpTree {
3067 explicit RegExpBackReference(RegExpCapture* capture)
3068 : capture_(capture) { }
3069 void* Accept(RegExpVisitor* visitor, void* data) override;
3070 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
3071 RegExpNode* on_success) override;
3072 RegExpBackReference* AsBackReference() override;
3073 bool IsBackReference() override;
3074 int min_match() override { return 0; }
3075 int max_match() override { return capture_->max_match(); }
3076 int index() { return capture_->index(); }
3077 RegExpCapture* capture() { return capture_; }
3079 RegExpCapture* capture_;
3083 class RegExpEmpty final : public RegExpTree {
3086 void* Accept(RegExpVisitor* visitor, void* data) override;
3087 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
3088 RegExpNode* on_success) override;
3089 RegExpEmpty* AsEmpty() override;
3090 bool IsEmpty() override;
3091 int min_match() override { return 0; }
3092 int max_match() override { return 0; }
3096 // ----------------------------------------------------------------------------
3098 // - leaf node visitors are abstract.
3100 class AstVisitor BASE_EMBEDDED {
3103 virtual ~AstVisitor() {}
3105 // Stack overflow check and dynamic dispatch.
3106 virtual void Visit(AstNode* node) = 0;
3108 // Iteration left-to-right.
3109 virtual void VisitDeclarations(ZoneList<Declaration*>* declarations);
3110 virtual void VisitStatements(ZoneList<Statement*>* statements);
3111 virtual void VisitExpressions(ZoneList<Expression*>* expressions);
3113 // Individual AST nodes.
3114 #define DEF_VISIT(type) \
3115 virtual void Visit##type(type* node) = 0;
3116 AST_NODE_LIST(DEF_VISIT)
3121 #define DEFINE_AST_VISITOR_SUBCLASS_MEMBERS() \
3123 void Visit(AstNode* node) final { \
3124 if (!CheckStackOverflow()) node->Accept(this); \
3127 void SetStackOverflow() { stack_overflow_ = true; } \
3128 void ClearStackOverflow() { stack_overflow_ = false; } \
3129 bool HasStackOverflow() const { return stack_overflow_; } \
3131 bool CheckStackOverflow() { \
3132 if (stack_overflow_) return true; \
3133 StackLimitCheck check(isolate_); \
3134 if (!check.HasOverflowed()) return false; \
3135 stack_overflow_ = true; \
3140 void InitializeAstVisitor(Isolate* isolate, Zone* zone) { \
3141 isolate_ = isolate; \
3143 stack_overflow_ = false; \
3145 Zone* zone() { return zone_; } \
3146 Isolate* isolate() { return isolate_; } \
3148 Isolate* isolate_; \
3150 bool stack_overflow_
3153 // ----------------------------------------------------------------------------
3156 class AstNodeFactory final BASE_EMBEDDED {
3158 explicit AstNodeFactory(AstValueFactory* ast_value_factory)
3159 : zone_(ast_value_factory->zone()),
3160 ast_value_factory_(ast_value_factory) {}
3162 VariableDeclaration* NewVariableDeclaration(
3163 VariableProxy* proxy, VariableMode mode, Scope* scope, int pos,
3164 bool is_class_declaration = false, int declaration_group_start = -1) {
3166 VariableDeclaration(zone_, proxy, mode, scope, pos,
3167 is_class_declaration, declaration_group_start);
3170 FunctionDeclaration* NewFunctionDeclaration(VariableProxy* proxy,
3172 FunctionLiteral* fun,
3175 return new (zone_) FunctionDeclaration(zone_, proxy, mode, fun, scope, pos);
3178 ImportDeclaration* NewImportDeclaration(VariableProxy* proxy,
3179 const AstRawString* import_name,
3180 const AstRawString* module_specifier,
3181 Scope* scope, int pos) {
3182 return new (zone_) ImportDeclaration(zone_, proxy, import_name,
3183 module_specifier, scope, pos);
3186 ExportDeclaration* NewExportDeclaration(VariableProxy* proxy,
3189 return new (zone_) ExportDeclaration(zone_, proxy, scope, pos);
3192 Block* NewBlock(ZoneList<const AstRawString*>* labels,
3194 bool is_initializer_block,
3197 Block(zone_, labels, capacity, is_initializer_block, pos);
3200 #define STATEMENT_WITH_LABELS(NodeType) \
3201 NodeType* New##NodeType(ZoneList<const AstRawString*>* labels, int pos) { \
3202 return new (zone_) NodeType(zone_, labels, pos); \
3204 STATEMENT_WITH_LABELS(DoWhileStatement)
3205 STATEMENT_WITH_LABELS(WhileStatement)
3206 STATEMENT_WITH_LABELS(ForStatement)
3207 STATEMENT_WITH_LABELS(SwitchStatement)
3208 #undef STATEMENT_WITH_LABELS
3210 ForEachStatement* NewForEachStatement(ForEachStatement::VisitMode visit_mode,
3211 ZoneList<const AstRawString*>* labels,
3213 switch (visit_mode) {
3214 case ForEachStatement::ENUMERATE: {
3215 return new (zone_) ForInStatement(zone_, labels, pos);
3217 case ForEachStatement::ITERATE: {
3218 return new (zone_) ForOfStatement(zone_, labels, pos);
3225 ExpressionStatement* NewExpressionStatement(Expression* expression, int pos) {
3226 return new (zone_) ExpressionStatement(zone_, expression, pos);
3229 ContinueStatement* NewContinueStatement(IterationStatement* target, int pos) {
3230 return new (zone_) ContinueStatement(zone_, target, pos);
3233 BreakStatement* NewBreakStatement(BreakableStatement* target, int pos) {
3234 return new (zone_) BreakStatement(zone_, target, pos);
3237 ReturnStatement* NewReturnStatement(Expression* expression, int pos) {
3238 return new (zone_) ReturnStatement(zone_, expression, pos);
3241 WithStatement* NewWithStatement(Scope* scope,
3242 Expression* expression,
3243 Statement* statement,
3245 return new (zone_) WithStatement(zone_, scope, expression, statement, pos);
3248 IfStatement* NewIfStatement(Expression* condition,
3249 Statement* then_statement,
3250 Statement* else_statement,
3253 IfStatement(zone_, condition, then_statement, else_statement, pos);
3256 TryCatchStatement* NewTryCatchStatement(int index,
3262 return new (zone_) TryCatchStatement(zone_, index, try_block, scope,
3263 variable, catch_block, pos);
3266 TryFinallyStatement* NewTryFinallyStatement(int index,
3268 Block* finally_block,
3271 TryFinallyStatement(zone_, index, try_block, finally_block, pos);
3274 DebuggerStatement* NewDebuggerStatement(int pos) {
3275 return new (zone_) DebuggerStatement(zone_, pos);
3278 EmptyStatement* NewEmptyStatement(int pos) {
3279 return new(zone_) EmptyStatement(zone_, pos);
3282 CaseClause* NewCaseClause(
3283 Expression* label, ZoneList<Statement*>* statements, int pos) {
3284 return new (zone_) CaseClause(zone_, label, statements, pos);
3287 Literal* NewStringLiteral(const AstRawString* string, int pos) {
3289 Literal(zone_, ast_value_factory_->NewString(string), pos);
3292 // A JavaScript symbol (ECMA-262 edition 6).
3293 Literal* NewSymbolLiteral(const char* name, int pos) {
3294 return new (zone_) Literal(zone_, ast_value_factory_->NewSymbol(name), pos);
3297 Literal* NewNumberLiteral(double number, int pos) {
3299 Literal(zone_, ast_value_factory_->NewNumber(number), pos);
3302 Literal* NewSmiLiteral(int number, int pos) {
3303 return new (zone_) Literal(zone_, ast_value_factory_->NewSmi(number), pos);
3306 Literal* NewBooleanLiteral(bool b, int pos) {
3307 return new (zone_) Literal(zone_, ast_value_factory_->NewBoolean(b), pos);
3310 Literal* NewNullLiteral(int pos) {
3311 return new (zone_) Literal(zone_, ast_value_factory_->NewNull(), pos);
3314 Literal* NewUndefinedLiteral(int pos) {
3315 return new (zone_) Literal(zone_, ast_value_factory_->NewUndefined(), pos);
3318 Literal* NewTheHoleLiteral(int pos) {
3319 return new (zone_) Literal(zone_, ast_value_factory_->NewTheHole(), pos);
3322 ObjectLiteral* NewObjectLiteral(
3323 ZoneList<ObjectLiteral::Property*>* properties,
3325 int boilerplate_properties,
3328 return new (zone_) ObjectLiteral(zone_, properties, literal_index,
3329 boilerplate_properties, has_function, pos);
3332 ObjectLiteral::Property* NewObjectLiteralProperty(
3333 Expression* key, Expression* value, ObjectLiteralProperty::Kind kind,
3334 bool is_static, bool is_computed_name) {
3336 ObjectLiteral::Property(key, value, kind, is_static, is_computed_name);
3339 ObjectLiteral::Property* NewObjectLiteralProperty(Expression* key,
3342 bool is_computed_name) {
3343 return new (zone_) ObjectLiteral::Property(ast_value_factory_, key, value,
3344 is_static, is_computed_name);
3347 RegExpLiteral* NewRegExpLiteral(const AstRawString* pattern,
3348 const AstRawString* flags,
3351 return new (zone_) RegExpLiteral(zone_, pattern, flags, literal_index, pos);
3354 ArrayLiteral* NewArrayLiteral(ZoneList<Expression*>* values,
3357 return new (zone_) ArrayLiteral(zone_, values, literal_index, pos);
3360 VariableProxy* NewVariableProxy(Variable* var,
3361 int start_position = RelocInfo::kNoPosition,
3362 int end_position = RelocInfo::kNoPosition) {
3363 return new (zone_) VariableProxy(zone_, var, start_position, end_position);
3366 VariableProxy* NewVariableProxy(const AstRawString* name,
3367 Variable::Kind variable_kind,
3368 int start_position = RelocInfo::kNoPosition,
3369 int end_position = RelocInfo::kNoPosition) {
3371 VariableProxy(zone_, name, variable_kind, start_position, end_position);
3374 Property* NewProperty(Expression* obj, Expression* key, int pos) {
3375 return new (zone_) Property(zone_, obj, key, pos);
3378 Call* NewCall(Expression* expression,
3379 ZoneList<Expression*>* arguments,
3381 return new (zone_) Call(zone_, expression, arguments, pos);
3384 CallNew* NewCallNew(Expression* expression,
3385 ZoneList<Expression*>* arguments,
3387 return new (zone_) CallNew(zone_, expression, arguments, pos);
3390 CallRuntime* NewCallRuntime(const AstRawString* name,
3391 const Runtime::Function* function,
3392 ZoneList<Expression*>* arguments,
3394 return new (zone_) CallRuntime(zone_, name, function, arguments, pos);
3397 UnaryOperation* NewUnaryOperation(Token::Value op,
3398 Expression* expression,
3400 return new (zone_) UnaryOperation(zone_, op, expression, pos);
3403 BinaryOperation* NewBinaryOperation(Token::Value op,
3407 return new (zone_) BinaryOperation(zone_, op, left, right, pos);
3410 CountOperation* NewCountOperation(Token::Value op,
3414 return new (zone_) CountOperation(zone_, op, is_prefix, expr, pos);
3417 CompareOperation* NewCompareOperation(Token::Value op,
3421 return new (zone_) CompareOperation(zone_, op, left, right, pos);
3424 Spread* NewSpread(Expression* expression, int pos) {
3425 return new (zone_) Spread(zone_, expression, pos);
3428 Conditional* NewConditional(Expression* condition,
3429 Expression* then_expression,
3430 Expression* else_expression,
3432 return new (zone_) Conditional(zone_, condition, then_expression,
3433 else_expression, position);
3436 Assignment* NewAssignment(Token::Value op,
3440 DCHECK(Token::IsAssignmentOp(op));
3441 Assignment* assign = new (zone_) Assignment(zone_, op, target, value, pos);
3442 if (assign->is_compound()) {
3443 DCHECK(Token::IsAssignmentOp(op));
3444 assign->binary_operation_ =
3445 NewBinaryOperation(assign->binary_op(), target, value, pos + 1);
3450 Yield* NewYield(Expression *generator_object,
3451 Expression* expression,
3452 Yield::Kind yield_kind,
3454 if (!expression) expression = NewUndefinedLiteral(pos);
3456 Yield(zone_, generator_object, expression, yield_kind, pos);
3459 Throw* NewThrow(Expression* exception, int pos) {
3460 return new (zone_) Throw(zone_, exception, pos);
3463 FunctionLiteral* NewFunctionLiteral(
3464 const AstRawString* name, AstValueFactory* ast_value_factory,
3465 Scope* scope, ZoneList<Statement*>* body, int materialized_literal_count,
3466 int expected_property_count, int handler_count, int parameter_count,
3467 FunctionLiteral::ParameterFlag has_duplicate_parameters,
3468 FunctionLiteral::FunctionType function_type,
3469 FunctionLiteral::IsFunctionFlag is_function,
3470 FunctionLiteral::EagerCompileHint eager_compile_hint, FunctionKind kind,
3472 return new (zone_) FunctionLiteral(
3473 zone_, name, ast_value_factory, scope, body, materialized_literal_count,
3474 expected_property_count, handler_count, parameter_count, function_type,
3475 has_duplicate_parameters, is_function, eager_compile_hint, kind,
3479 ClassLiteral* NewClassLiteral(const AstRawString* name, Scope* scope,
3480 VariableProxy* proxy, Expression* extends,
3481 FunctionLiteral* constructor,
3482 ZoneList<ObjectLiteral::Property*>* properties,
3483 int start_position, int end_position) {
3485 ClassLiteral(zone_, name, scope, proxy, extends, constructor,
3486 properties, start_position, end_position);
3489 NativeFunctionLiteral* NewNativeFunctionLiteral(const AstRawString* name,
3490 v8::Extension* extension,
3492 return new (zone_) NativeFunctionLiteral(zone_, name, extension, pos);
3495 ThisFunction* NewThisFunction(int pos) {
3496 return new (zone_) ThisFunction(zone_, pos);
3499 SuperReference* NewSuperReference(VariableProxy* this_var, int pos) {
3500 return new (zone_) SuperReference(zone_, this_var, pos);
3505 AstValueFactory* ast_value_factory_;
3509 } } // namespace v8::internal