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(ModuleDeclaration) \
46 V(ImportDeclaration) \
49 #define MODULE_NODE_LIST(V) \
54 #define STATEMENT_NODE_LIST(V) \
57 V(ExpressionStatement) \
60 V(ContinueStatement) \
70 V(TryCatchStatement) \
71 V(TryFinallyStatement) \
74 #define EXPRESSION_NODE_LIST(V) \
77 V(NativeFunctionLiteral) \
100 #define AST_NODE_LIST(V) \
101 DECLARATION_NODE_LIST(V) \
102 MODULE_NODE_LIST(V) \
103 STATEMENT_NODE_LIST(V) \
104 EXPRESSION_NODE_LIST(V)
106 // Forward declarations
107 class AstNodeFactory;
111 class BreakableStatement;
113 class IterationStatement;
114 class MaterializedLiteral;
116 class TypeFeedbackOracle;
118 class RegExpAlternative;
119 class RegExpAssertion;
121 class RegExpBackReference;
123 class RegExpCharacterClass;
124 class RegExpCompiler;
125 class RegExpDisjunction;
127 class RegExpLookahead;
128 class RegExpQuantifier;
131 #define DEF_FORWARD_DECLARATION(type) class type;
132 AST_NODE_LIST(DEF_FORWARD_DECLARATION)
133 #undef DEF_FORWARD_DECLARATION
136 // Typedef only introduced to avoid unreadable code.
137 // Please do appreciate the required space in "> >".
138 typedef ZoneList<Handle<String> > ZoneStringList;
139 typedef ZoneList<Handle<Object> > ZoneObjectList;
142 #define DECLARE_NODE_TYPE(type) \
143 void Accept(AstVisitor* v) override; \
144 AstNode::NodeType node_type() const final { return AstNode::k##type; } \
145 friend class AstNodeFactory;
148 enum AstPropertiesFlag {
155 class FeedbackVectorRequirements {
157 FeedbackVectorRequirements(int slots, int ic_slots)
158 : slots_(slots), ic_slots_(ic_slots) {}
160 int slots() const { return slots_; }
161 int ic_slots() const { return ic_slots_; }
169 class VariableICSlotPair final {
171 VariableICSlotPair(Variable* variable, FeedbackVectorICSlot slot)
172 : variable_(variable), slot_(slot) {}
174 : variable_(NULL), slot_(FeedbackVectorICSlot::Invalid()) {}
176 Variable* variable() const { return variable_; }
177 FeedbackVectorICSlot slot() const { return slot_; }
181 FeedbackVectorICSlot slot_;
185 typedef List<VariableICSlotPair> ICSlotCache;
188 class AstProperties final BASE_EMBEDDED {
190 class Flags : public EnumSet<AstPropertiesFlag, int> {};
192 explicit AstProperties(Zone* zone) : node_count_(0), spec_(zone) {}
194 Flags* flags() { return &flags_; }
195 int node_count() { return node_count_; }
196 void add_node_count(int count) { node_count_ += count; }
198 int slots() const { return spec_.slots(); }
199 void increase_slots(int count) { spec_.increase_slots(count); }
201 int ic_slots() const { return spec_.ic_slots(); }
202 void increase_ic_slots(int count) { spec_.increase_ic_slots(count); }
203 void SetKind(int ic_slot, Code::Kind kind) { spec_.SetKind(ic_slot, kind); }
204 const ZoneFeedbackVectorSpec* get_spec() const { return &spec_; }
209 ZoneFeedbackVectorSpec spec_;
213 class AstNode: public ZoneObject {
215 #define DECLARE_TYPE_ENUM(type) k##type,
217 AST_NODE_LIST(DECLARE_TYPE_ENUM)
220 #undef DECLARE_TYPE_ENUM
222 void* operator new(size_t size, Zone* zone) { return zone->New(size); }
224 explicit AstNode(int position): position_(position) {}
225 virtual ~AstNode() {}
227 virtual void Accept(AstVisitor* v) = 0;
228 virtual NodeType node_type() const = 0;
229 int position() const { return position_; }
231 // Type testing & conversion functions overridden by concrete subclasses.
232 #define DECLARE_NODE_FUNCTIONS(type) \
233 bool Is##type() const { return node_type() == AstNode::k##type; } \
235 return Is##type() ? reinterpret_cast<type*>(this) : NULL; \
237 const type* As##type() const { \
238 return Is##type() ? reinterpret_cast<const type*>(this) : NULL; \
240 AST_NODE_LIST(DECLARE_NODE_FUNCTIONS)
241 #undef DECLARE_NODE_FUNCTIONS
243 virtual BreakableStatement* AsBreakableStatement() { return NULL; }
244 virtual IterationStatement* AsIterationStatement() { return NULL; }
245 virtual MaterializedLiteral* AsMaterializedLiteral() { return NULL; }
247 // The interface for feedback slots, with default no-op implementations for
248 // node types which don't actually have this. Note that this is conceptually
249 // not really nice, but multiple inheritance would introduce yet another
250 // vtable entry per node, something we don't want for space reasons.
251 virtual FeedbackVectorRequirements ComputeFeedbackRequirements(
252 Isolate* isolate, const ICSlotCache* cache) {
253 return FeedbackVectorRequirements(0, 0);
255 virtual void SetFirstFeedbackSlot(FeedbackVectorSlot slot) { UNREACHABLE(); }
256 virtual void SetFirstFeedbackICSlot(FeedbackVectorICSlot slot,
257 ICSlotCache* cache) {
260 // Each ICSlot stores a kind of IC which the participating node should know.
261 virtual Code::Kind FeedbackICSlotKind(int index) {
263 return Code::NUMBER_OF_KINDS;
267 // Hidden to prevent accidental usage. It would have to load the
268 // current zone from the TLS.
269 void* operator new(size_t size);
271 friend class CaseClause; // Generates AST IDs.
277 class Statement : public AstNode {
279 explicit Statement(Zone* zone, int position) : AstNode(position) {}
281 bool IsEmpty() { return AsEmptyStatement() != NULL; }
282 virtual bool IsJump() const { return false; }
286 class SmallMapList final {
289 SmallMapList(int capacity, Zone* zone) : list_(capacity, zone) {}
291 void Reserve(int capacity, Zone* zone) { list_.Reserve(capacity, zone); }
292 void Clear() { list_.Clear(); }
293 void Sort() { list_.Sort(); }
295 bool is_empty() const { return list_.is_empty(); }
296 int length() const { return list_.length(); }
298 void AddMapIfMissing(Handle<Map> map, Zone* zone) {
299 if (!Map::TryUpdate(map).ToHandle(&map)) return;
300 for (int i = 0; i < length(); ++i) {
301 if (at(i).is_identical_to(map)) return;
306 void FilterForPossibleTransitions(Map* root_map) {
307 for (int i = list_.length() - 1; i >= 0; i--) {
308 if (at(i)->FindRootMap() != root_map) {
309 list_.RemoveElement(list_.at(i));
314 void Add(Handle<Map> handle, Zone* zone) {
315 list_.Add(handle.location(), zone);
318 Handle<Map> at(int i) const {
319 return Handle<Map>(list_.at(i));
322 Handle<Map> first() const { return at(0); }
323 Handle<Map> last() const { return at(length() - 1); }
326 // The list stores pointers to Map*, that is Map**, so it's GC safe.
327 SmallPointerList<Map*> list_;
329 DISALLOW_COPY_AND_ASSIGN(SmallMapList);
333 class Expression : public AstNode {
336 // Not assigned a context yet, or else will not be visited during
339 // Evaluated for its side effects.
341 // Evaluated for its value (and side effects).
343 // Evaluated for control flow (and side effects).
347 virtual bool IsValidReferenceExpression() const { return false; }
349 // Helpers for ToBoolean conversion.
350 virtual bool ToBooleanIsTrue() const { return false; }
351 virtual bool ToBooleanIsFalse() const { return false; }
353 // Symbols that cannot be parsed as array indices are considered property
354 // names. We do not treat symbols that can be array indexes as property
355 // names because [] for string objects is handled only by keyed ICs.
356 virtual bool IsPropertyName() const { return false; }
358 // True iff the expression is a literal represented as a smi.
359 bool IsSmiLiteral() const;
361 // True iff the expression is a string literal.
362 bool IsStringLiteral() const;
364 // True iff the expression is the null literal.
365 bool IsNullLiteral() const;
367 // True if we can prove that the expression is the undefined literal.
368 bool IsUndefinedLiteral(Isolate* isolate) const;
370 // Expression type bounds
371 Bounds bounds() const { return bounds_; }
372 void set_bounds(Bounds bounds) { bounds_ = bounds; }
374 // Whether the expression is parenthesized
375 bool is_single_parenthesized() const {
376 return IsSingleParenthesizedField::decode(bit_field_);
378 bool is_multi_parenthesized() const {
379 return IsMultiParenthesizedField::decode(bit_field_);
381 void increase_parenthesization_level() {
382 bit_field_ = IsMultiParenthesizedField::update(bit_field_,
383 is_single_parenthesized());
384 bit_field_ = IsSingleParenthesizedField::update(bit_field_, true);
387 // Type feedback information for assignments and properties.
388 virtual bool IsMonomorphic() {
392 virtual SmallMapList* GetReceiverTypes() {
396 virtual KeyedAccessStoreMode GetStoreMode() const {
398 return STANDARD_STORE;
400 virtual IcCheckType GetKeyType() const {
405 // TODO(rossberg): this should move to its own AST node eventually.
406 virtual void RecordToBooleanTypeFeedback(TypeFeedbackOracle* oracle);
407 byte to_boolean_types() const {
408 return ToBooleanTypesField::decode(bit_field_);
411 void set_base_id(int id) { base_id_ = id; }
412 static int num_ids() { return parent_num_ids() + 2; }
413 BailoutId id() const { return BailoutId(local_id(0)); }
414 TypeFeedbackId test_id() const { return TypeFeedbackId(local_id(1)); }
417 Expression(Zone* zone, int pos)
419 base_id_(BailoutId::None().ToInt()),
420 bounds_(Bounds::Unbounded(zone)),
422 static int parent_num_ids() { return 0; }
423 void set_to_boolean_types(byte types) {
424 bit_field_ = ToBooleanTypesField::update(bit_field_, types);
427 int base_id() const {
428 DCHECK(!BailoutId(base_id_).IsNone());
433 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
437 class ToBooleanTypesField : public BitField16<byte, 0, 8> {};
438 class IsSingleParenthesizedField : public BitField16<bool, 8, 1> {};
439 class IsMultiParenthesizedField : public BitField16<bool, 9, 1> {};
441 // Ends with 16-bit field; deriving classes in turn begin with
442 // 16-bit fields for optimum packing efficiency.
446 class BreakableStatement : public Statement {
449 TARGET_FOR_ANONYMOUS,
450 TARGET_FOR_NAMED_ONLY
453 // The labels associated with this statement. May be NULL;
454 // if it is != NULL, guaranteed to contain at least one entry.
455 ZoneList<const AstRawString*>* labels() const { return labels_; }
457 // Type testing & conversion.
458 BreakableStatement* AsBreakableStatement() final { return this; }
461 Label* break_target() { return &break_target_; }
464 bool is_target_for_anonymous() const {
465 return breakable_type_ == TARGET_FOR_ANONYMOUS;
468 void set_base_id(int id) { base_id_ = id; }
469 static int num_ids() { return parent_num_ids() + 2; }
470 BailoutId EntryId() const { return BailoutId(local_id(0)); }
471 BailoutId ExitId() const { return BailoutId(local_id(1)); }
474 BreakableStatement(Zone* zone, ZoneList<const AstRawString*>* labels,
475 BreakableType breakable_type, int position)
476 : Statement(zone, position),
478 breakable_type_(breakable_type),
479 base_id_(BailoutId::None().ToInt()) {
480 DCHECK(labels == NULL || labels->length() > 0);
482 static int parent_num_ids() { return 0; }
484 int base_id() const {
485 DCHECK(!BailoutId(base_id_).IsNone());
490 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
492 ZoneList<const AstRawString*>* labels_;
493 BreakableType breakable_type_;
499 class Block final : public BreakableStatement {
501 DECLARE_NODE_TYPE(Block)
503 void AddStatement(Statement* statement, Zone* zone) {
504 statements_.Add(statement, zone);
507 ZoneList<Statement*>* statements() { return &statements_; }
508 bool is_initializer_block() const { return is_initializer_block_; }
510 static int num_ids() { return parent_num_ids() + 1; }
511 BailoutId DeclsId() const { return BailoutId(local_id(0)); }
513 bool IsJump() const override {
514 return !statements_.is_empty() && statements_.last()->IsJump()
515 && labels() == NULL; // Good enough as an approximation...
518 Scope* scope() const { return scope_; }
519 void set_scope(Scope* scope) { scope_ = scope; }
522 Block(Zone* zone, ZoneList<const AstRawString*>* labels, int capacity,
523 bool is_initializer_block, int pos)
524 : BreakableStatement(zone, labels, TARGET_FOR_NAMED_ONLY, pos),
525 statements_(capacity, zone),
526 is_initializer_block_(is_initializer_block),
528 static int parent_num_ids() { return BreakableStatement::num_ids(); }
531 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
533 ZoneList<Statement*> statements_;
534 bool is_initializer_block_;
539 class Declaration : public AstNode {
541 VariableProxy* proxy() const { return proxy_; }
542 VariableMode mode() const { return mode_; }
543 Scope* scope() const { return scope_; }
544 virtual InitializationFlag initialization() const = 0;
545 virtual bool IsInlineable() const;
548 Declaration(Zone* zone, VariableProxy* proxy, VariableMode mode, Scope* scope,
550 : AstNode(pos), mode_(mode), proxy_(proxy), scope_(scope) {
551 DCHECK(IsDeclaredVariableMode(mode));
556 VariableProxy* proxy_;
558 // Nested scope from which the declaration originated.
563 class VariableDeclaration final : public Declaration {
565 DECLARE_NODE_TYPE(VariableDeclaration)
567 InitializationFlag initialization() const override {
568 return mode() == VAR ? kCreatedInitialized : kNeedsInitialization;
571 bool is_class_declaration() const { return is_class_declaration_; }
573 // VariableDeclarations can be grouped into consecutive declaration
574 // groups. Each VariableDeclaration is associated with the start position of
575 // the group it belongs to. The positions are used for strong mode scope
576 // checks for classes and functions.
577 int declaration_group_start() const { return declaration_group_start_; }
580 VariableDeclaration(Zone* zone, VariableProxy* proxy, VariableMode mode,
581 Scope* scope, int pos, bool is_class_declaration = false,
582 int declaration_group_start = -1)
583 : Declaration(zone, proxy, mode, scope, pos),
584 is_class_declaration_(is_class_declaration),
585 declaration_group_start_(declaration_group_start) {}
587 bool is_class_declaration_;
588 int declaration_group_start_;
592 class FunctionDeclaration final : public Declaration {
594 DECLARE_NODE_TYPE(FunctionDeclaration)
596 FunctionLiteral* fun() const { return fun_; }
597 InitializationFlag initialization() const override {
598 return kCreatedInitialized;
600 bool IsInlineable() const override;
603 FunctionDeclaration(Zone* zone,
604 VariableProxy* proxy,
606 FunctionLiteral* fun,
609 : Declaration(zone, proxy, mode, scope, pos),
611 DCHECK(mode == VAR || mode == LET || mode == CONST);
616 FunctionLiteral* fun_;
620 class ModuleDeclaration final : public Declaration {
622 DECLARE_NODE_TYPE(ModuleDeclaration)
624 Module* module() const { return module_; }
625 InitializationFlag initialization() const override {
626 return kCreatedInitialized;
630 ModuleDeclaration(Zone* zone, VariableProxy* proxy, Module* module,
631 Scope* scope, int pos)
632 : Declaration(zone, proxy, CONST, scope, pos), module_(module) {}
639 class ImportDeclaration final : public Declaration {
641 DECLARE_NODE_TYPE(ImportDeclaration)
643 const AstRawString* import_name() const { return import_name_; }
644 const AstRawString* module_specifier() const { return module_specifier_; }
645 void set_module_specifier(const AstRawString* module_specifier) {
646 DCHECK(module_specifier_ == NULL);
647 module_specifier_ = module_specifier;
649 InitializationFlag initialization() const override {
650 return kNeedsInitialization;
654 ImportDeclaration(Zone* zone, VariableProxy* proxy,
655 const AstRawString* import_name,
656 const AstRawString* module_specifier, Scope* scope, int pos)
657 : Declaration(zone, proxy, IMPORT, scope, pos),
658 import_name_(import_name),
659 module_specifier_(module_specifier) {}
662 const AstRawString* import_name_;
663 const AstRawString* module_specifier_;
667 class ExportDeclaration final : public Declaration {
669 DECLARE_NODE_TYPE(ExportDeclaration)
671 InitializationFlag initialization() const override {
672 return kCreatedInitialized;
676 ExportDeclaration(Zone* zone, VariableProxy* proxy, Scope* scope, int pos)
677 : Declaration(zone, proxy, LET, scope, pos) {}
681 class Module : public AstNode {
683 ModuleDescriptor* descriptor() const { return descriptor_; }
684 Block* body() const { return body_; }
687 Module(Zone* zone, int pos)
688 : AstNode(pos), descriptor_(ModuleDescriptor::New(zone)), body_(NULL) {}
689 Module(Zone* zone, ModuleDescriptor* descriptor, int pos, Block* body = NULL)
690 : AstNode(pos), descriptor_(descriptor), body_(body) {}
693 ModuleDescriptor* descriptor_;
698 class ModuleLiteral final : public Module {
700 DECLARE_NODE_TYPE(ModuleLiteral)
703 ModuleLiteral(Zone* zone, Block* body, ModuleDescriptor* descriptor, int pos)
704 : Module(zone, descriptor, pos, body) {}
708 class ModulePath final : public Module {
710 DECLARE_NODE_TYPE(ModulePath)
712 Module* module() const { return module_; }
713 Handle<String> name() const { return name_->string(); }
716 ModulePath(Zone* zone, Module* module, const AstRawString* name, int pos)
717 : Module(zone, pos), module_(module), name_(name) {}
721 const AstRawString* name_;
725 class ModuleUrl final : public Module {
727 DECLARE_NODE_TYPE(ModuleUrl)
729 Handle<String> url() const { return url_; }
732 ModuleUrl(Zone* zone, Handle<String> url, int pos)
733 : Module(zone, pos), url_(url) {
741 class ModuleStatement final : public Statement {
743 DECLARE_NODE_TYPE(ModuleStatement)
745 Block* body() const { return body_; }
748 ModuleStatement(Zone* zone, Block* body, int pos)
749 : Statement(zone, pos), body_(body) {}
756 class IterationStatement : public BreakableStatement {
758 // Type testing & conversion.
759 IterationStatement* AsIterationStatement() final { return this; }
761 Statement* body() const { return body_; }
763 static int num_ids() { return parent_num_ids() + 1; }
764 BailoutId OsrEntryId() const { return BailoutId(local_id(0)); }
765 virtual BailoutId ContinueId() const = 0;
766 virtual BailoutId StackCheckId() const = 0;
769 Label* continue_target() { return &continue_target_; }
772 IterationStatement(Zone* zone, ZoneList<const AstRawString*>* labels, int pos)
773 : BreakableStatement(zone, labels, TARGET_FOR_ANONYMOUS, pos),
775 static int parent_num_ids() { return BreakableStatement::num_ids(); }
776 void Initialize(Statement* body) { body_ = body; }
779 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
782 Label continue_target_;
786 class DoWhileStatement final : public IterationStatement {
788 DECLARE_NODE_TYPE(DoWhileStatement)
790 void Initialize(Expression* cond, Statement* body) {
791 IterationStatement::Initialize(body);
795 Expression* cond() const { return cond_; }
797 static int num_ids() { return parent_num_ids() + 2; }
798 BailoutId ContinueId() const override { return BailoutId(local_id(0)); }
799 BailoutId StackCheckId() const override { return BackEdgeId(); }
800 BailoutId BackEdgeId() const { return BailoutId(local_id(1)); }
803 DoWhileStatement(Zone* zone, ZoneList<const AstRawString*>* labels, int pos)
804 : IterationStatement(zone, labels, pos), cond_(NULL) {}
805 static int parent_num_ids() { return IterationStatement::num_ids(); }
808 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
814 class WhileStatement final : public IterationStatement {
816 DECLARE_NODE_TYPE(WhileStatement)
818 void Initialize(Expression* cond, Statement* body) {
819 IterationStatement::Initialize(body);
823 Expression* cond() const { return cond_; }
825 static int num_ids() { return parent_num_ids() + 1; }
826 BailoutId ContinueId() const override { return EntryId(); }
827 BailoutId StackCheckId() const override { return BodyId(); }
828 BailoutId BodyId() const { return BailoutId(local_id(0)); }
831 WhileStatement(Zone* zone, ZoneList<const AstRawString*>* labels, int pos)
832 : IterationStatement(zone, labels, pos), cond_(NULL) {}
833 static int parent_num_ids() { return IterationStatement::num_ids(); }
836 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
842 class ForStatement final : public IterationStatement {
844 DECLARE_NODE_TYPE(ForStatement)
846 void Initialize(Statement* init,
850 IterationStatement::Initialize(body);
856 Statement* init() const { return init_; }
857 Expression* cond() const { return cond_; }
858 Statement* next() const { return next_; }
860 static int num_ids() { return parent_num_ids() + 2; }
861 BailoutId ContinueId() const override { return BailoutId(local_id(0)); }
862 BailoutId StackCheckId() const override { return BodyId(); }
863 BailoutId BodyId() const { return BailoutId(local_id(1)); }
866 ForStatement(Zone* zone, ZoneList<const AstRawString*>* labels, int pos)
867 : IterationStatement(zone, labels, pos),
871 static int parent_num_ids() { return IterationStatement::num_ids(); }
874 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
882 class ForEachStatement : public IterationStatement {
885 ENUMERATE, // for (each in subject) body;
886 ITERATE // for (each of subject) body;
889 void Initialize(Expression* each, Expression* subject, Statement* body) {
890 IterationStatement::Initialize(body);
895 Expression* each() const { return each_; }
896 Expression* subject() const { return subject_; }
899 ForEachStatement(Zone* zone, ZoneList<const AstRawString*>* labels, int pos)
900 : IterationStatement(zone, labels, pos), each_(NULL), subject_(NULL) {}
904 Expression* subject_;
908 class ForInStatement final : public ForEachStatement {
910 DECLARE_NODE_TYPE(ForInStatement)
912 Expression* enumerable() const {
916 // Type feedback information.
917 virtual FeedbackVectorRequirements ComputeFeedbackRequirements(
918 Isolate* isolate, const ICSlotCache* cache) override {
919 return FeedbackVectorRequirements(1, 0);
921 void SetFirstFeedbackSlot(FeedbackVectorSlot slot) override {
922 for_in_feedback_slot_ = slot;
925 FeedbackVectorSlot ForInFeedbackSlot() {
926 DCHECK(!for_in_feedback_slot_.IsInvalid());
927 return for_in_feedback_slot_;
930 enum ForInType { FAST_FOR_IN, SLOW_FOR_IN };
931 ForInType for_in_type() const { return for_in_type_; }
932 void set_for_in_type(ForInType type) { for_in_type_ = type; }
934 static int num_ids() { return parent_num_ids() + 6; }
935 BailoutId BodyId() const { return BailoutId(local_id(0)); }
936 BailoutId PrepareId() const { return BailoutId(local_id(1)); }
937 BailoutId EnumId() const { return BailoutId(local_id(2)); }
938 BailoutId ToObjectId() const { return BailoutId(local_id(3)); }
939 BailoutId FilterId() const { return BailoutId(local_id(4)); }
940 BailoutId AssignmentId() const { return BailoutId(local_id(5)); }
941 BailoutId ContinueId() const override { return EntryId(); }
942 BailoutId StackCheckId() const override { return BodyId(); }
945 ForInStatement(Zone* zone, ZoneList<const AstRawString*>* labels, int pos)
946 : ForEachStatement(zone, labels, pos),
947 for_in_type_(SLOW_FOR_IN),
948 for_in_feedback_slot_(FeedbackVectorSlot::Invalid()) {}
949 static int parent_num_ids() { return ForEachStatement::num_ids(); }
952 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
954 ForInType for_in_type_;
955 FeedbackVectorSlot for_in_feedback_slot_;
959 class ForOfStatement final : public ForEachStatement {
961 DECLARE_NODE_TYPE(ForOfStatement)
963 void Initialize(Expression* each,
966 Expression* assign_iterator,
967 Expression* next_result,
968 Expression* result_done,
969 Expression* assign_each) {
970 ForEachStatement::Initialize(each, subject, body);
971 assign_iterator_ = assign_iterator;
972 next_result_ = next_result;
973 result_done_ = result_done;
974 assign_each_ = assign_each;
977 Expression* iterable() const {
981 // iterator = subject[Symbol.iterator]()
982 Expression* assign_iterator() const {
983 return assign_iterator_;
986 // result = iterator.next() // with type check
987 Expression* next_result() const {
992 Expression* result_done() const {
996 // each = result.value
997 Expression* assign_each() const {
1001 BailoutId ContinueId() const override { return EntryId(); }
1002 BailoutId StackCheckId() const override { return BackEdgeId(); }
1004 static int num_ids() { return parent_num_ids() + 1; }
1005 BailoutId BackEdgeId() const { return BailoutId(local_id(0)); }
1008 ForOfStatement(Zone* zone, ZoneList<const AstRawString*>* labels, int pos)
1009 : ForEachStatement(zone, labels, pos),
1010 assign_iterator_(NULL),
1013 assign_each_(NULL) {}
1014 static int parent_num_ids() { return ForEachStatement::num_ids(); }
1017 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
1019 Expression* assign_iterator_;
1020 Expression* next_result_;
1021 Expression* result_done_;
1022 Expression* assign_each_;
1026 class ExpressionStatement final : public Statement {
1028 DECLARE_NODE_TYPE(ExpressionStatement)
1030 void set_expression(Expression* e) { expression_ = e; }
1031 Expression* expression() const { return expression_; }
1032 bool IsJump() const override { return expression_->IsThrow(); }
1035 ExpressionStatement(Zone* zone, Expression* expression, int pos)
1036 : Statement(zone, pos), expression_(expression) { }
1039 Expression* expression_;
1043 class JumpStatement : public Statement {
1045 bool IsJump() const final { return true; }
1048 explicit JumpStatement(Zone* zone, int pos) : Statement(zone, pos) {}
1052 class ContinueStatement final : public JumpStatement {
1054 DECLARE_NODE_TYPE(ContinueStatement)
1056 IterationStatement* target() const { return target_; }
1059 explicit ContinueStatement(Zone* zone, IterationStatement* target, int pos)
1060 : JumpStatement(zone, pos), target_(target) { }
1063 IterationStatement* target_;
1067 class BreakStatement final : public JumpStatement {
1069 DECLARE_NODE_TYPE(BreakStatement)
1071 BreakableStatement* target() const { return target_; }
1074 explicit BreakStatement(Zone* zone, BreakableStatement* target, int pos)
1075 : JumpStatement(zone, pos), target_(target) { }
1078 BreakableStatement* target_;
1082 class ReturnStatement final : public JumpStatement {
1084 DECLARE_NODE_TYPE(ReturnStatement)
1086 Expression* expression() const { return expression_; }
1089 explicit ReturnStatement(Zone* zone, Expression* expression, int pos)
1090 : JumpStatement(zone, pos), expression_(expression) { }
1093 Expression* expression_;
1097 class WithStatement final : public Statement {
1099 DECLARE_NODE_TYPE(WithStatement)
1101 Scope* scope() { return scope_; }
1102 Expression* expression() const { return expression_; }
1103 Statement* statement() const { return statement_; }
1105 void set_base_id(int id) { base_id_ = id; }
1106 static int num_ids() { return parent_num_ids() + 1; }
1107 BailoutId EntryId() const { return BailoutId(local_id(0)); }
1110 WithStatement(Zone* zone, Scope* scope, Expression* expression,
1111 Statement* statement, int pos)
1112 : Statement(zone, pos),
1114 expression_(expression),
1115 statement_(statement),
1116 base_id_(BailoutId::None().ToInt()) {}
1117 static int parent_num_ids() { return 0; }
1119 int base_id() const {
1120 DCHECK(!BailoutId(base_id_).IsNone());
1125 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
1128 Expression* expression_;
1129 Statement* statement_;
1134 class CaseClause final : public Expression {
1136 DECLARE_NODE_TYPE(CaseClause)
1138 bool is_default() const { return label_ == NULL; }
1139 Expression* label() const {
1140 CHECK(!is_default());
1143 Label* body_target() { return &body_target_; }
1144 ZoneList<Statement*>* statements() const { return statements_; }
1146 static int num_ids() { return parent_num_ids() + 2; }
1147 BailoutId EntryId() const { return BailoutId(local_id(0)); }
1148 TypeFeedbackId CompareId() { return TypeFeedbackId(local_id(1)); }
1150 Type* compare_type() { return compare_type_; }
1151 void set_compare_type(Type* type) { compare_type_ = type; }
1154 static int parent_num_ids() { return Expression::num_ids(); }
1157 CaseClause(Zone* zone, Expression* label, ZoneList<Statement*>* statements,
1159 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
1163 ZoneList<Statement*>* statements_;
1164 Type* compare_type_;
1168 class SwitchStatement final : public BreakableStatement {
1170 DECLARE_NODE_TYPE(SwitchStatement)
1172 void Initialize(Expression* tag, ZoneList<CaseClause*>* cases) {
1177 Expression* tag() const { return tag_; }
1178 ZoneList<CaseClause*>* cases() const { return cases_; }
1181 SwitchStatement(Zone* zone, ZoneList<const AstRawString*>* labels, int pos)
1182 : BreakableStatement(zone, labels, TARGET_FOR_ANONYMOUS, pos),
1188 ZoneList<CaseClause*>* cases_;
1192 // If-statements always have non-null references to their then- and
1193 // else-parts. When parsing if-statements with no explicit else-part,
1194 // the parser implicitly creates an empty statement. Use the
1195 // HasThenStatement() and HasElseStatement() functions to check if a
1196 // given if-statement has a then- or an else-part containing code.
1197 class IfStatement final : public Statement {
1199 DECLARE_NODE_TYPE(IfStatement)
1201 bool HasThenStatement() const { return !then_statement()->IsEmpty(); }
1202 bool HasElseStatement() const { return !else_statement()->IsEmpty(); }
1204 Expression* condition() const { return condition_; }
1205 Statement* then_statement() const { return then_statement_; }
1206 Statement* else_statement() const { return else_statement_; }
1208 bool IsJump() const override {
1209 return HasThenStatement() && then_statement()->IsJump()
1210 && HasElseStatement() && else_statement()->IsJump();
1213 void set_base_id(int id) { base_id_ = id; }
1214 static int num_ids() { return parent_num_ids() + 3; }
1215 BailoutId IfId() const { return BailoutId(local_id(0)); }
1216 BailoutId ThenId() const { return BailoutId(local_id(1)); }
1217 BailoutId ElseId() const { return BailoutId(local_id(2)); }
1220 IfStatement(Zone* zone, Expression* condition, Statement* then_statement,
1221 Statement* else_statement, int pos)
1222 : Statement(zone, pos),
1223 condition_(condition),
1224 then_statement_(then_statement),
1225 else_statement_(else_statement),
1226 base_id_(BailoutId::None().ToInt()) {}
1227 static int parent_num_ids() { return 0; }
1229 int base_id() const {
1230 DCHECK(!BailoutId(base_id_).IsNone());
1235 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
1237 Expression* condition_;
1238 Statement* then_statement_;
1239 Statement* else_statement_;
1244 class TryStatement : public Statement {
1246 int index() const { return index_; }
1247 Block* try_block() const { return try_block_; }
1250 TryStatement(Zone* zone, int index, Block* try_block, int pos)
1251 : Statement(zone, pos), index_(index), try_block_(try_block) {}
1254 // Unique (per-function) index of this handler. This is not an AST ID.
1261 class TryCatchStatement final : public TryStatement {
1263 DECLARE_NODE_TYPE(TryCatchStatement)
1265 Scope* scope() { return scope_; }
1266 Variable* variable() { return variable_; }
1267 Block* catch_block() const { return catch_block_; }
1270 TryCatchStatement(Zone* zone,
1277 : TryStatement(zone, index, try_block, pos),
1279 variable_(variable),
1280 catch_block_(catch_block) {
1285 Variable* variable_;
1286 Block* catch_block_;
1290 class TryFinallyStatement final : public TryStatement {
1292 DECLARE_NODE_TYPE(TryFinallyStatement)
1294 Block* finally_block() const { return finally_block_; }
1297 TryFinallyStatement(
1298 Zone* zone, int index, Block* try_block, Block* finally_block, int pos)
1299 : TryStatement(zone, index, try_block, pos),
1300 finally_block_(finally_block) { }
1303 Block* finally_block_;
1307 class DebuggerStatement final : public Statement {
1309 DECLARE_NODE_TYPE(DebuggerStatement)
1311 void set_base_id(int id) { base_id_ = id; }
1312 static int num_ids() { return parent_num_ids() + 1; }
1313 BailoutId DebugBreakId() const { return BailoutId(local_id(0)); }
1316 explicit DebuggerStatement(Zone* zone, int pos)
1317 : Statement(zone, pos), base_id_(BailoutId::None().ToInt()) {}
1318 static int parent_num_ids() { return 0; }
1320 int base_id() const {
1321 DCHECK(!BailoutId(base_id_).IsNone());
1326 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
1332 class EmptyStatement final : public Statement {
1334 DECLARE_NODE_TYPE(EmptyStatement)
1337 explicit EmptyStatement(Zone* zone, int pos): Statement(zone, pos) {}
1341 class Literal final : public Expression {
1343 DECLARE_NODE_TYPE(Literal)
1345 bool IsPropertyName() const override { return value_->IsPropertyName(); }
1347 Handle<String> AsPropertyName() {
1348 DCHECK(IsPropertyName());
1349 return Handle<String>::cast(value());
1352 const AstRawString* AsRawPropertyName() {
1353 DCHECK(IsPropertyName());
1354 return value_->AsString();
1357 bool ToBooleanIsTrue() const override { return value()->BooleanValue(); }
1358 bool ToBooleanIsFalse() const override { return !value()->BooleanValue(); }
1360 Handle<Object> value() const { return value_->value(); }
1361 const AstValue* raw_value() const { return value_; }
1363 // Support for using Literal as a HashMap key. NOTE: Currently, this works
1364 // only for string and number literals!
1366 static bool Match(void* literal1, void* literal2);
1368 static int num_ids() { return parent_num_ids() + 1; }
1369 TypeFeedbackId LiteralFeedbackId() const {
1370 return TypeFeedbackId(local_id(0));
1374 Literal(Zone* zone, const AstValue* value, int position)
1375 : Expression(zone, position), value_(value) {}
1376 static int parent_num_ids() { return Expression::num_ids(); }
1379 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
1381 const AstValue* value_;
1385 // Base class for literals that needs space in the corresponding JSFunction.
1386 class MaterializedLiteral : public Expression {
1388 virtual MaterializedLiteral* AsMaterializedLiteral() { return this; }
1390 int literal_index() { return literal_index_; }
1393 // only callable after initialization.
1394 DCHECK(depth_ >= 1);
1399 MaterializedLiteral(Zone* zone, int literal_index, int pos)
1400 : Expression(zone, pos),
1401 literal_index_(literal_index),
1405 // A materialized literal is simple if the values consist of only
1406 // constants and simple object and array literals.
1407 bool is_simple() const { return is_simple_; }
1408 void set_is_simple(bool is_simple) { is_simple_ = is_simple; }
1409 friend class CompileTimeValue;
1411 void set_depth(int depth) {
1416 // Populate the constant properties/elements fixed array.
1417 void BuildConstants(Isolate* isolate);
1418 friend class ArrayLiteral;
1419 friend class ObjectLiteral;
1421 // If the expression is a literal, return the literal value;
1422 // if the expression is a materialized literal and is simple return a
1423 // compile time value as encoded by CompileTimeValue::GetValue().
1424 // Otherwise, return undefined literal as the placeholder
1425 // in the object literal boilerplate.
1426 Handle<Object> GetBoilerplateValue(Expression* expression, Isolate* isolate);
1435 // Property is used for passing information
1436 // about an object literal's properties from the parser
1437 // to the code generator.
1438 class ObjectLiteralProperty final : public ZoneObject {
1441 CONSTANT, // Property with constant value (compile time).
1442 COMPUTED, // Property with computed value (execution time).
1443 MATERIALIZED_LITERAL, // Property value is a materialized literal.
1444 GETTER, SETTER, // Property is an accessor function.
1445 PROTOTYPE // Property is __proto__.
1448 Expression* key() { return key_; }
1449 Expression* value() { return value_; }
1450 Kind kind() { return kind_; }
1452 // Type feedback information.
1453 bool IsMonomorphic() { return !receiver_type_.is_null(); }
1454 Handle<Map> GetReceiverType() { return receiver_type_; }
1456 bool IsCompileTimeValue();
1458 void set_emit_store(bool emit_store);
1461 bool is_static() const { return is_static_; }
1462 bool is_computed_name() const { return is_computed_name_; }
1464 void set_receiver_type(Handle<Map> map) { receiver_type_ = map; }
1467 friend class AstNodeFactory;
1469 ObjectLiteralProperty(Expression* key, Expression* value, Kind kind,
1470 bool is_static, bool is_computed_name);
1471 ObjectLiteralProperty(AstValueFactory* ast_value_factory, Expression* key,
1472 Expression* value, bool is_static,
1473 bool is_computed_name);
1481 bool is_computed_name_;
1482 Handle<Map> receiver_type_;
1486 // An object literal has a boilerplate object that is used
1487 // for minimizing the work when constructing it at runtime.
1488 class ObjectLiteral final : public MaterializedLiteral {
1490 typedef ObjectLiteralProperty Property;
1492 DECLARE_NODE_TYPE(ObjectLiteral)
1494 Handle<FixedArray> constant_properties() const {
1495 return constant_properties_;
1497 int properties_count() const { return constant_properties_->length() / 2; }
1498 ZoneList<Property*>* properties() const { return properties_; }
1499 bool fast_elements() const { return fast_elements_; }
1500 bool may_store_doubles() const { return may_store_doubles_; }
1501 bool has_function() const { return has_function_; }
1502 bool has_elements() const { return has_elements_; }
1504 // Decide if a property should be in the object boilerplate.
1505 static bool IsBoilerplateProperty(Property* property);
1507 // Populate the constant properties fixed array.
1508 void BuildConstantProperties(Isolate* isolate);
1510 // Mark all computed expressions that are bound to a key that
1511 // is shadowed by a later occurrence of the same key. For the
1512 // marked expressions, no store code is emitted.
1513 void CalculateEmitStore(Zone* zone);
1515 // Assemble bitfield of flags for the CreateObjectLiteral helper.
1516 int ComputeFlags(bool disable_mementos = false) const {
1517 int flags = fast_elements() ? kFastElements : kNoFlags;
1518 flags |= has_function() ? kHasFunction : kNoFlags;
1519 if (depth() == 1 && !has_elements() && !may_store_doubles()) {
1520 flags |= kShallowProperties;
1522 if (disable_mementos) {
1523 flags |= kDisableMementos;
1531 kHasFunction = 1 << 1,
1532 kShallowProperties = 1 << 2,
1533 kDisableMementos = 1 << 3
1536 struct Accessors: public ZoneObject {
1537 Accessors() : getter(NULL), setter(NULL) {}
1542 BailoutId CreateLiteralId() const { return BailoutId(local_id(0)); }
1544 // Return an AST id for a property that is used in simulate instructions.
1545 BailoutId GetIdForProperty(int i) { return BailoutId(local_id(i + 1)); }
1547 // Unlike other AST nodes, this number of bailout IDs allocated for an
1548 // ObjectLiteral can vary, so num_ids() is not a static method.
1549 int num_ids() const { return parent_num_ids() + 1 + properties()->length(); }
1552 ObjectLiteral(Zone* zone, ZoneList<Property*>* properties, int literal_index,
1553 int boilerplate_properties, bool has_function, int pos)
1554 : MaterializedLiteral(zone, literal_index, pos),
1555 properties_(properties),
1556 boilerplate_properties_(boilerplate_properties),
1557 fast_elements_(false),
1558 has_elements_(false),
1559 may_store_doubles_(false),
1560 has_function_(has_function) {}
1561 static int parent_num_ids() { return MaterializedLiteral::num_ids(); }
1564 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
1565 Handle<FixedArray> constant_properties_;
1566 ZoneList<Property*>* properties_;
1567 int boilerplate_properties_;
1568 bool fast_elements_;
1570 bool may_store_doubles_;
1575 // Node for capturing a regexp literal.
1576 class RegExpLiteral final : public MaterializedLiteral {
1578 DECLARE_NODE_TYPE(RegExpLiteral)
1580 Handle<String> pattern() const { return pattern_->string(); }
1581 Handle<String> flags() const { return flags_->string(); }
1584 RegExpLiteral(Zone* zone, const AstRawString* pattern,
1585 const AstRawString* flags, int literal_index, int pos)
1586 : MaterializedLiteral(zone, literal_index, pos),
1593 const AstRawString* pattern_;
1594 const AstRawString* flags_;
1598 // An array literal has a literals object that is used
1599 // for minimizing the work when constructing it at runtime.
1600 class ArrayLiteral final : public MaterializedLiteral {
1602 DECLARE_NODE_TYPE(ArrayLiteral)
1604 Handle<FixedArray> constant_elements() const { return constant_elements_; }
1605 ElementsKind constant_elements_kind() const {
1606 DCHECK_EQ(2, constant_elements_->length());
1607 return static_cast<ElementsKind>(
1608 Smi::cast(constant_elements_->get(0))->value());
1611 ZoneList<Expression*>* values() const { return values_; }
1613 BailoutId CreateLiteralId() const { return BailoutId(local_id(0)); }
1615 // Return an AST id for an element that is used in simulate instructions.
1616 BailoutId GetIdForElement(int i) { return BailoutId(local_id(i + 1)); }
1618 // Unlike other AST nodes, this number of bailout IDs allocated for an
1619 // ArrayLiteral can vary, so num_ids() is not a static method.
1620 int num_ids() const { return parent_num_ids() + 1 + values()->length(); }
1622 // Populate the constant elements fixed array.
1623 void BuildConstantElements(Isolate* isolate);
1625 // Assemble bitfield of flags for the CreateArrayLiteral helper.
1626 int ComputeFlags(bool disable_mementos = false) const {
1627 int flags = depth() == 1 ? kShallowElements : kNoFlags;
1628 if (disable_mementos) {
1629 flags |= kDisableMementos;
1636 kShallowElements = 1,
1637 kDisableMementos = 1 << 1
1641 ArrayLiteral(Zone* zone, ZoneList<Expression*>* values, int literal_index,
1643 : MaterializedLiteral(zone, literal_index, pos), values_(values) {}
1644 static int parent_num_ids() { return MaterializedLiteral::num_ids(); }
1647 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
1649 Handle<FixedArray> constant_elements_;
1650 ZoneList<Expression*>* values_;
1654 class VariableProxy final : public Expression {
1656 DECLARE_NODE_TYPE(VariableProxy)
1658 bool IsValidReferenceExpression() const override { return !is_this(); }
1660 bool IsArguments() const { return is_resolved() && var()->is_arguments(); }
1662 Handle<String> name() const { return raw_name()->string(); }
1663 const AstRawString* raw_name() const {
1664 return is_resolved() ? var_->raw_name() : raw_name_;
1667 Variable* var() const {
1668 DCHECK(is_resolved());
1671 void set_var(Variable* v) {
1672 DCHECK(!is_resolved());
1677 bool is_this() const { return IsThisField::decode(bit_field_); }
1679 bool is_assigned() const { return IsAssignedField::decode(bit_field_); }
1680 void set_is_assigned() {
1681 bit_field_ = IsAssignedField::update(bit_field_, true);
1684 bool is_resolved() const { return IsResolvedField::decode(bit_field_); }
1685 void set_is_resolved() {
1686 bit_field_ = IsResolvedField::update(bit_field_, true);
1689 int end_position() const { return end_position_; }
1691 // Bind this proxy to the variable var.
1692 void BindTo(Variable* var);
1694 bool UsesVariableFeedbackSlot() const {
1695 return FLAG_vector_ics && (var()->IsUnallocated() || var()->IsLookupSlot());
1698 virtual FeedbackVectorRequirements ComputeFeedbackRequirements(
1699 Isolate* isolate, const ICSlotCache* cache) override;
1701 void SetFirstFeedbackICSlot(FeedbackVectorICSlot slot,
1702 ICSlotCache* cache) override;
1703 Code::Kind FeedbackICSlotKind(int index) override { return Code::LOAD_IC; }
1704 FeedbackVectorICSlot VariableFeedbackSlot() {
1705 DCHECK(!UsesVariableFeedbackSlot() || !variable_feedback_slot_.IsInvalid());
1706 return variable_feedback_slot_;
1710 VariableProxy(Zone* zone, Variable* var, int start_position,
1713 VariableProxy(Zone* zone, const AstRawString* name,
1714 Variable::Kind variable_kind, int start_position,
1717 class IsThisField : public BitField8<bool, 0, 1> {};
1718 class IsAssignedField : public BitField8<bool, 1, 1> {};
1719 class IsResolvedField : public BitField8<bool, 2, 1> {};
1721 // Start with 16-bit (or smaller) field, which should get packed together
1722 // with Expression's trailing 16-bit field.
1724 FeedbackVectorICSlot variable_feedback_slot_;
1726 const AstRawString* raw_name_; // if !is_resolved_
1727 Variable* var_; // if is_resolved_
1729 // Position is stored in the AstNode superclass, but VariableProxy needs to
1730 // know its end position too (for error messages). It cannot be inferred from
1731 // the variable name length because it can contain escapes.
1736 class Property final : public Expression {
1738 DECLARE_NODE_TYPE(Property)
1740 bool IsValidReferenceExpression() const override { return true; }
1742 Expression* obj() const { return obj_; }
1743 Expression* key() const { return key_; }
1745 static int num_ids() { return parent_num_ids() + 2; }
1746 BailoutId LoadId() const { return BailoutId(local_id(0)); }
1747 TypeFeedbackId PropertyFeedbackId() { return TypeFeedbackId(local_id(1)); }
1749 bool IsStringAccess() const {
1750 return IsStringAccessField::decode(bit_field_);
1753 // Type feedback information.
1754 bool IsMonomorphic() override { return receiver_types_.length() == 1; }
1755 SmallMapList* GetReceiverTypes() override { return &receiver_types_; }
1756 KeyedAccessStoreMode GetStoreMode() const override { return STANDARD_STORE; }
1757 IcCheckType GetKeyType() const override {
1758 return KeyTypeField::decode(bit_field_);
1760 bool IsUninitialized() const {
1761 return !is_for_call() && HasNoTypeInformation();
1763 bool HasNoTypeInformation() const {
1764 return GetInlineCacheState() == UNINITIALIZED;
1766 InlineCacheState GetInlineCacheState() const {
1767 return InlineCacheStateField::decode(bit_field_);
1769 void set_is_string_access(bool b) {
1770 bit_field_ = IsStringAccessField::update(bit_field_, b);
1772 void set_key_type(IcCheckType key_type) {
1773 bit_field_ = KeyTypeField::update(bit_field_, key_type);
1775 void set_inline_cache_state(InlineCacheState state) {
1776 bit_field_ = InlineCacheStateField::update(bit_field_, state);
1778 void mark_for_call() {
1779 bit_field_ = IsForCallField::update(bit_field_, true);
1781 bool is_for_call() const { return IsForCallField::decode(bit_field_); }
1783 bool IsSuperAccess() {
1784 return obj()->IsSuperReference();
1787 virtual FeedbackVectorRequirements ComputeFeedbackRequirements(
1788 Isolate* isolate, const ICSlotCache* cache) override {
1789 return FeedbackVectorRequirements(0, FLAG_vector_ics ? 1 : 0);
1791 void SetFirstFeedbackICSlot(FeedbackVectorICSlot slot,
1792 ICSlotCache* cache) override {
1793 property_feedback_slot_ = slot;
1795 Code::Kind FeedbackICSlotKind(int index) override {
1796 return key()->IsPropertyName() ? Code::LOAD_IC : Code::KEYED_LOAD_IC;
1799 FeedbackVectorICSlot PropertyFeedbackSlot() const {
1800 DCHECK(!FLAG_vector_ics || !property_feedback_slot_.IsInvalid());
1801 return property_feedback_slot_;
1805 Property(Zone* zone, Expression* obj, Expression* key, int pos)
1806 : Expression(zone, pos),
1807 bit_field_(IsForCallField::encode(false) |
1808 IsStringAccessField::encode(false) |
1809 InlineCacheStateField::encode(UNINITIALIZED)),
1810 property_feedback_slot_(FeedbackVectorICSlot::Invalid()),
1813 static int parent_num_ids() { return Expression::num_ids(); }
1816 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
1818 class IsForCallField : public BitField8<bool, 0, 1> {};
1819 class IsStringAccessField : public BitField8<bool, 1, 1> {};
1820 class KeyTypeField : public BitField8<IcCheckType, 2, 1> {};
1821 class InlineCacheStateField : public BitField8<InlineCacheState, 3, 4> {};
1823 FeedbackVectorICSlot property_feedback_slot_;
1826 SmallMapList receiver_types_;
1830 class Call final : public Expression {
1832 DECLARE_NODE_TYPE(Call)
1834 Expression* expression() const { return expression_; }
1835 ZoneList<Expression*>* arguments() const { return arguments_; }
1837 // Type feedback information.
1838 virtual FeedbackVectorRequirements ComputeFeedbackRequirements(
1839 Isolate* isolate, const ICSlotCache* cache) override;
1840 void SetFirstFeedbackICSlot(FeedbackVectorICSlot slot,
1841 ICSlotCache* cache) override {
1842 ic_slot_or_slot_ = slot.ToInt();
1844 void SetFirstFeedbackSlot(FeedbackVectorSlot slot) override {
1845 ic_slot_or_slot_ = slot.ToInt();
1847 Code::Kind FeedbackICSlotKind(int index) override { return Code::CALL_IC; }
1849 FeedbackVectorSlot CallFeedbackSlot() const {
1850 DCHECK(ic_slot_or_slot_ != FeedbackVectorSlot::Invalid().ToInt());
1851 return FeedbackVectorSlot(ic_slot_or_slot_);
1854 FeedbackVectorICSlot CallFeedbackICSlot() const {
1855 DCHECK(ic_slot_or_slot_ != FeedbackVectorICSlot::Invalid().ToInt());
1856 return FeedbackVectorICSlot(ic_slot_or_slot_);
1859 SmallMapList* GetReceiverTypes() override {
1860 if (expression()->IsProperty()) {
1861 return expression()->AsProperty()->GetReceiverTypes();
1866 bool IsMonomorphic() override {
1867 if (expression()->IsProperty()) {
1868 return expression()->AsProperty()->IsMonomorphic();
1870 return !target_.is_null();
1873 bool global_call() const {
1874 VariableProxy* proxy = expression_->AsVariableProxy();
1875 return proxy != NULL && proxy->var()->IsUnallocated();
1878 bool known_global_function() const {
1879 return global_call() && !target_.is_null();
1882 Handle<JSFunction> target() { return target_; }
1884 Handle<AllocationSite> allocation_site() { return allocation_site_; }
1886 void SetKnownGlobalTarget(Handle<JSFunction> target) {
1888 set_is_uninitialized(false);
1890 void set_target(Handle<JSFunction> target) { target_ = target; }
1891 void set_allocation_site(Handle<AllocationSite> site) {
1892 allocation_site_ = site;
1895 static int num_ids() { return parent_num_ids() + 2; }
1896 BailoutId ReturnId() const { return BailoutId(local_id(0)); }
1897 BailoutId EvalOrLookupId() const { return BailoutId(local_id(1)); }
1899 bool is_uninitialized() const {
1900 return IsUninitializedField::decode(bit_field_);
1902 void set_is_uninitialized(bool b) {
1903 bit_field_ = IsUninitializedField::update(bit_field_, b);
1915 // Helpers to determine how to handle the call.
1916 CallType GetCallType(Isolate* isolate) const;
1917 bool IsUsingCallFeedbackSlot(Isolate* isolate) const;
1918 bool IsUsingCallFeedbackICSlot(Isolate* isolate) const;
1921 // Used to assert that the FullCodeGenerator records the return site.
1922 bool return_is_recorded_;
1926 Call(Zone* zone, Expression* expression, ZoneList<Expression*>* arguments,
1928 : Expression(zone, pos),
1929 ic_slot_or_slot_(FeedbackVectorICSlot::Invalid().ToInt()),
1930 expression_(expression),
1931 arguments_(arguments),
1932 bit_field_(IsUninitializedField::encode(false)) {
1933 if (expression->IsProperty()) {
1934 expression->AsProperty()->mark_for_call();
1937 static int parent_num_ids() { return Expression::num_ids(); }
1940 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
1942 // We store this as an integer because we don't know if we have a slot or
1943 // an ic slot until scoping time.
1944 int ic_slot_or_slot_;
1945 Expression* expression_;
1946 ZoneList<Expression*>* arguments_;
1947 Handle<JSFunction> target_;
1948 Handle<AllocationSite> allocation_site_;
1949 class IsUninitializedField : public BitField8<bool, 0, 1> {};
1954 class CallNew final : public Expression {
1956 DECLARE_NODE_TYPE(CallNew)
1958 Expression* expression() const { return expression_; }
1959 ZoneList<Expression*>* arguments() const { return arguments_; }
1961 // Type feedback information.
1962 virtual FeedbackVectorRequirements ComputeFeedbackRequirements(
1963 Isolate* isolate, const ICSlotCache* cache) override {
1964 return FeedbackVectorRequirements(FLAG_pretenuring_call_new ? 2 : 1, 0);
1966 void SetFirstFeedbackSlot(FeedbackVectorSlot slot) override {
1967 callnew_feedback_slot_ = slot;
1970 FeedbackVectorSlot CallNewFeedbackSlot() {
1971 DCHECK(!callnew_feedback_slot_.IsInvalid());
1972 return callnew_feedback_slot_;
1974 FeedbackVectorSlot AllocationSiteFeedbackSlot() {
1975 DCHECK(FLAG_pretenuring_call_new);
1976 return CallNewFeedbackSlot().next();
1979 bool IsMonomorphic() override { return is_monomorphic_; }
1980 Handle<JSFunction> target() const { return target_; }
1981 Handle<AllocationSite> allocation_site() const {
1982 return allocation_site_;
1985 static int num_ids() { return parent_num_ids() + 1; }
1986 static int feedback_slots() { return 1; }
1987 BailoutId ReturnId() const { return BailoutId(local_id(0)); }
1989 void set_allocation_site(Handle<AllocationSite> site) {
1990 allocation_site_ = site;
1992 void set_is_monomorphic(bool monomorphic) { is_monomorphic_ = monomorphic; }
1993 void set_target(Handle<JSFunction> target) { target_ = target; }
1994 void SetKnownGlobalTarget(Handle<JSFunction> target) {
1996 is_monomorphic_ = true;
2000 CallNew(Zone* zone, Expression* expression, ZoneList<Expression*>* arguments,
2002 : Expression(zone, pos),
2003 expression_(expression),
2004 arguments_(arguments),
2005 is_monomorphic_(false),
2006 callnew_feedback_slot_(FeedbackVectorSlot::Invalid()) {}
2008 static int parent_num_ids() { return Expression::num_ids(); }
2011 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
2013 Expression* expression_;
2014 ZoneList<Expression*>* arguments_;
2015 bool is_monomorphic_;
2016 Handle<JSFunction> target_;
2017 Handle<AllocationSite> allocation_site_;
2018 FeedbackVectorSlot callnew_feedback_slot_;
2022 // The CallRuntime class does not represent any official JavaScript
2023 // language construct. Instead it is used to call a C or JS function
2024 // with a set of arguments. This is used from the builtins that are
2025 // implemented in JavaScript (see "v8natives.js").
2026 class CallRuntime final : public Expression {
2028 DECLARE_NODE_TYPE(CallRuntime)
2030 Handle<String> name() const { return raw_name_->string(); }
2031 const AstRawString* raw_name() const { return raw_name_; }
2032 const Runtime::Function* function() const { return function_; }
2033 ZoneList<Expression*>* arguments() const { return arguments_; }
2034 bool is_jsruntime() const { return function_ == NULL; }
2036 // Type feedback information.
2037 bool HasCallRuntimeFeedbackSlot() const {
2038 return FLAG_vector_ics && is_jsruntime();
2040 virtual FeedbackVectorRequirements ComputeFeedbackRequirements(
2041 Isolate* isolate, const ICSlotCache* cache) override {
2042 return FeedbackVectorRequirements(0, HasCallRuntimeFeedbackSlot() ? 1 : 0);
2044 void SetFirstFeedbackICSlot(FeedbackVectorICSlot slot,
2045 ICSlotCache* cache) override {
2046 callruntime_feedback_slot_ = slot;
2048 Code::Kind FeedbackICSlotKind(int index) override { return Code::LOAD_IC; }
2050 FeedbackVectorICSlot CallRuntimeFeedbackSlot() {
2051 DCHECK(!HasCallRuntimeFeedbackSlot() ||
2052 !callruntime_feedback_slot_.IsInvalid());
2053 return callruntime_feedback_slot_;
2056 static int num_ids() { return parent_num_ids() + 1; }
2057 TypeFeedbackId CallRuntimeFeedbackId() const {
2058 return TypeFeedbackId(local_id(0));
2062 CallRuntime(Zone* zone, const AstRawString* name,
2063 const Runtime::Function* function,
2064 ZoneList<Expression*>* arguments, int pos)
2065 : Expression(zone, pos),
2067 function_(function),
2068 arguments_(arguments),
2069 callruntime_feedback_slot_(FeedbackVectorICSlot::Invalid()) {}
2070 static int parent_num_ids() { return Expression::num_ids(); }
2073 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
2075 const AstRawString* raw_name_;
2076 const Runtime::Function* function_;
2077 ZoneList<Expression*>* arguments_;
2078 FeedbackVectorICSlot callruntime_feedback_slot_;
2082 class UnaryOperation final : public Expression {
2084 DECLARE_NODE_TYPE(UnaryOperation)
2086 Token::Value op() const { return op_; }
2087 Expression* expression() const { return expression_; }
2089 // For unary not (Token::NOT), the AST ids where true and false will
2090 // actually be materialized, respectively.
2091 static int num_ids() { return parent_num_ids() + 2; }
2092 BailoutId MaterializeTrueId() const { return BailoutId(local_id(0)); }
2093 BailoutId MaterializeFalseId() const { return BailoutId(local_id(1)); }
2095 virtual void RecordToBooleanTypeFeedback(TypeFeedbackOracle* oracle) override;
2098 UnaryOperation(Zone* zone, Token::Value op, Expression* expression, int pos)
2099 : Expression(zone, pos), op_(op), expression_(expression) {
2100 DCHECK(Token::IsUnaryOp(op));
2102 static int parent_num_ids() { return Expression::num_ids(); }
2105 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
2108 Expression* expression_;
2112 class BinaryOperation final : public Expression {
2114 DECLARE_NODE_TYPE(BinaryOperation)
2116 Token::Value op() const { return static_cast<Token::Value>(op_); }
2117 Expression* left() const { return left_; }
2118 Expression* right() const { return right_; }
2119 Handle<AllocationSite> allocation_site() const { return allocation_site_; }
2120 void set_allocation_site(Handle<AllocationSite> allocation_site) {
2121 allocation_site_ = allocation_site;
2124 // The short-circuit logical operations need an AST ID for their
2125 // right-hand subexpression.
2126 static int num_ids() { return parent_num_ids() + 2; }
2127 BailoutId RightId() const { return BailoutId(local_id(0)); }
2129 TypeFeedbackId BinaryOperationFeedbackId() const {
2130 return TypeFeedbackId(local_id(1));
2132 Maybe<int> fixed_right_arg() const {
2133 return has_fixed_right_arg_ ? Just(fixed_right_arg_value_) : Nothing<int>();
2135 void set_fixed_right_arg(Maybe<int> arg) {
2136 has_fixed_right_arg_ = arg.IsJust();
2137 if (arg.IsJust()) fixed_right_arg_value_ = arg.FromJust();
2140 virtual void RecordToBooleanTypeFeedback(TypeFeedbackOracle* oracle) override;
2143 BinaryOperation(Zone* zone, Token::Value op, Expression* left,
2144 Expression* right, int pos)
2145 : Expression(zone, pos),
2146 op_(static_cast<byte>(op)),
2147 has_fixed_right_arg_(false),
2148 fixed_right_arg_value_(0),
2151 DCHECK(Token::IsBinaryOp(op));
2153 static int parent_num_ids() { return Expression::num_ids(); }
2156 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
2158 const byte op_; // actually Token::Value
2159 // TODO(rossberg): the fixed arg should probably be represented as a Constant
2160 // type for the RHS. Currenty it's actually a Maybe<int>
2161 bool has_fixed_right_arg_;
2162 int fixed_right_arg_value_;
2165 Handle<AllocationSite> allocation_site_;
2169 class CountOperation final : public Expression {
2171 DECLARE_NODE_TYPE(CountOperation)
2173 bool is_prefix() const { return IsPrefixField::decode(bit_field_); }
2174 bool is_postfix() const { return !is_prefix(); }
2176 Token::Value op() const { return TokenField::decode(bit_field_); }
2177 Token::Value binary_op() {
2178 return (op() == Token::INC) ? Token::ADD : Token::SUB;
2181 Expression* expression() const { return expression_; }
2183 bool IsMonomorphic() override { return receiver_types_.length() == 1; }
2184 SmallMapList* GetReceiverTypes() override { return &receiver_types_; }
2185 IcCheckType GetKeyType() const override {
2186 return KeyTypeField::decode(bit_field_);
2188 KeyedAccessStoreMode GetStoreMode() const override {
2189 return StoreModeField::decode(bit_field_);
2191 Type* type() const { return type_; }
2192 void set_key_type(IcCheckType type) {
2193 bit_field_ = KeyTypeField::update(bit_field_, type);
2195 void set_store_mode(KeyedAccessStoreMode mode) {
2196 bit_field_ = StoreModeField::update(bit_field_, mode);
2198 void set_type(Type* type) { type_ = type; }
2200 static int num_ids() { return parent_num_ids() + 4; }
2201 BailoutId AssignmentId() const { return BailoutId(local_id(0)); }
2202 BailoutId ToNumberId() const { return BailoutId(local_id(1)); }
2203 TypeFeedbackId CountBinOpFeedbackId() const {
2204 return TypeFeedbackId(local_id(2));
2206 TypeFeedbackId CountStoreFeedbackId() const {
2207 return TypeFeedbackId(local_id(3));
2211 CountOperation(Zone* zone, Token::Value op, bool is_prefix, Expression* expr,
2213 : Expression(zone, pos),
2214 bit_field_(IsPrefixField::encode(is_prefix) |
2215 KeyTypeField::encode(ELEMENT) |
2216 StoreModeField::encode(STANDARD_STORE) |
2217 TokenField::encode(op)),
2219 expression_(expr) {}
2220 static int parent_num_ids() { return Expression::num_ids(); }
2223 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
2225 class IsPrefixField : public BitField16<bool, 0, 1> {};
2226 class KeyTypeField : public BitField16<IcCheckType, 1, 1> {};
2227 class StoreModeField : public BitField16<KeyedAccessStoreMode, 2, 4> {};
2228 class TokenField : public BitField16<Token::Value, 6, 8> {};
2230 // Starts with 16-bit field, which should get packed together with
2231 // Expression's trailing 16-bit field.
2232 uint16_t bit_field_;
2234 Expression* expression_;
2235 SmallMapList receiver_types_;
2239 class CompareOperation final : public Expression {
2241 DECLARE_NODE_TYPE(CompareOperation)
2243 Token::Value op() const { return op_; }
2244 Expression* left() const { return left_; }
2245 Expression* right() const { return right_; }
2247 // Type feedback information.
2248 static int num_ids() { return parent_num_ids() + 1; }
2249 TypeFeedbackId CompareOperationFeedbackId() const {
2250 return TypeFeedbackId(local_id(0));
2252 Type* combined_type() const { return combined_type_; }
2253 void set_combined_type(Type* type) { combined_type_ = type; }
2255 // Match special cases.
2256 bool IsLiteralCompareTypeof(Expression** expr, Handle<String>* check);
2257 bool IsLiteralCompareUndefined(Expression** expr, Isolate* isolate);
2258 bool IsLiteralCompareNull(Expression** expr);
2261 CompareOperation(Zone* zone, Token::Value op, Expression* left,
2262 Expression* right, int pos)
2263 : Expression(zone, pos),
2267 combined_type_(Type::None(zone)) {
2268 DCHECK(Token::IsCompareOp(op));
2270 static int parent_num_ids() { return Expression::num_ids(); }
2273 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
2279 Type* combined_type_;
2283 class Spread final : public Expression {
2285 DECLARE_NODE_TYPE(Spread)
2287 Expression* expression() const { return expression_; }
2289 static int num_ids() { return parent_num_ids(); }
2292 Spread(Zone* zone, Expression* expression, int pos)
2293 : Expression(zone, pos), expression_(expression) {}
2294 static int parent_num_ids() { return Expression::num_ids(); }
2297 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
2299 Expression* expression_;
2303 class Conditional final : public Expression {
2305 DECLARE_NODE_TYPE(Conditional)
2307 Expression* condition() const { return condition_; }
2308 Expression* then_expression() const { return then_expression_; }
2309 Expression* else_expression() const { return else_expression_; }
2311 static int num_ids() { return parent_num_ids() + 2; }
2312 BailoutId ThenId() const { return BailoutId(local_id(0)); }
2313 BailoutId ElseId() const { return BailoutId(local_id(1)); }
2316 Conditional(Zone* zone, Expression* condition, Expression* then_expression,
2317 Expression* else_expression, int position)
2318 : Expression(zone, position),
2319 condition_(condition),
2320 then_expression_(then_expression),
2321 else_expression_(else_expression) {}
2322 static int parent_num_ids() { return Expression::num_ids(); }
2325 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
2327 Expression* condition_;
2328 Expression* then_expression_;
2329 Expression* else_expression_;
2333 class Assignment final : public Expression {
2335 DECLARE_NODE_TYPE(Assignment)
2337 Assignment* AsSimpleAssignment() { return !is_compound() ? this : NULL; }
2339 Token::Value binary_op() const;
2341 Token::Value op() const { return TokenField::decode(bit_field_); }
2342 Expression* target() const { return target_; }
2343 Expression* value() const { return value_; }
2344 BinaryOperation* binary_operation() const { return binary_operation_; }
2346 // This check relies on the definition order of token in token.h.
2347 bool is_compound() const { return op() > Token::ASSIGN; }
2349 static int num_ids() { return parent_num_ids() + 2; }
2350 BailoutId AssignmentId() const { return BailoutId(local_id(0)); }
2352 // Type feedback information.
2353 TypeFeedbackId AssignmentFeedbackId() { return TypeFeedbackId(local_id(1)); }
2354 bool IsMonomorphic() override { return receiver_types_.length() == 1; }
2355 bool IsUninitialized() const {
2356 return IsUninitializedField::decode(bit_field_);
2358 bool HasNoTypeInformation() {
2359 return IsUninitializedField::decode(bit_field_);
2361 SmallMapList* GetReceiverTypes() override { return &receiver_types_; }
2362 IcCheckType GetKeyType() const override {
2363 return KeyTypeField::decode(bit_field_);
2365 KeyedAccessStoreMode GetStoreMode() const override {
2366 return StoreModeField::decode(bit_field_);
2368 void set_is_uninitialized(bool b) {
2369 bit_field_ = IsUninitializedField::update(bit_field_, b);
2371 void set_key_type(IcCheckType key_type) {
2372 bit_field_ = KeyTypeField::update(bit_field_, key_type);
2374 void set_store_mode(KeyedAccessStoreMode mode) {
2375 bit_field_ = StoreModeField::update(bit_field_, mode);
2379 Assignment(Zone* zone, Token::Value op, Expression* target, Expression* value,
2381 static int parent_num_ids() { return Expression::num_ids(); }
2384 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
2386 class IsUninitializedField : public BitField16<bool, 0, 1> {};
2387 class KeyTypeField : public BitField16<IcCheckType, 1, 1> {};
2388 class StoreModeField : public BitField16<KeyedAccessStoreMode, 2, 4> {};
2389 class TokenField : public BitField16<Token::Value, 6, 8> {};
2391 // Starts with 16-bit field, which should get packed together with
2392 // Expression's trailing 16-bit field.
2393 uint16_t bit_field_;
2394 Expression* target_;
2396 BinaryOperation* binary_operation_;
2397 SmallMapList receiver_types_;
2401 class Yield final : public Expression {
2403 DECLARE_NODE_TYPE(Yield)
2406 kInitial, // The initial yield that returns the unboxed generator object.
2407 kSuspend, // A normal yield: { value: EXPRESSION, done: false }
2408 kDelegating, // A yield*.
2409 kFinal // A return: { value: EXPRESSION, done: true }
2412 Expression* generator_object() const { return generator_object_; }
2413 Expression* expression() const { return expression_; }
2414 Kind yield_kind() const { return yield_kind_; }
2416 // Delegating yield surrounds the "yield" in a "try/catch". This index
2417 // locates the catch handler in the handler table, and is equivalent to
2418 // TryCatchStatement::index().
2420 DCHECK_EQ(kDelegating, yield_kind());
2423 void set_index(int index) {
2424 DCHECK_EQ(kDelegating, yield_kind());
2428 // Type feedback information.
2429 bool HasFeedbackSlots() const {
2430 return FLAG_vector_ics && (yield_kind() == kDelegating);
2432 virtual FeedbackVectorRequirements ComputeFeedbackRequirements(
2433 Isolate* isolate, const ICSlotCache* cache) override {
2434 return FeedbackVectorRequirements(0, HasFeedbackSlots() ? 3 : 0);
2436 void SetFirstFeedbackICSlot(FeedbackVectorICSlot slot,
2437 ICSlotCache* cache) override {
2438 yield_first_feedback_slot_ = slot;
2440 Code::Kind FeedbackICSlotKind(int index) override {
2441 return index == 0 ? Code::KEYED_LOAD_IC : Code::LOAD_IC;
2444 FeedbackVectorICSlot KeyedLoadFeedbackSlot() {
2445 DCHECK(!HasFeedbackSlots() || !yield_first_feedback_slot_.IsInvalid());
2446 return yield_first_feedback_slot_;
2449 FeedbackVectorICSlot DoneFeedbackSlot() {
2450 return KeyedLoadFeedbackSlot().next();
2453 FeedbackVectorICSlot ValueFeedbackSlot() { return DoneFeedbackSlot().next(); }
2456 Yield(Zone* zone, Expression* generator_object, Expression* expression,
2457 Kind yield_kind, int pos)
2458 : Expression(zone, pos),
2459 generator_object_(generator_object),
2460 expression_(expression),
2461 yield_kind_(yield_kind),
2463 yield_first_feedback_slot_(FeedbackVectorICSlot::Invalid()) {}
2466 Expression* generator_object_;
2467 Expression* expression_;
2470 FeedbackVectorICSlot yield_first_feedback_slot_;
2474 class Throw final : public Expression {
2476 DECLARE_NODE_TYPE(Throw)
2478 Expression* exception() const { return exception_; }
2481 Throw(Zone* zone, Expression* exception, int pos)
2482 : Expression(zone, pos), exception_(exception) {}
2485 Expression* exception_;
2489 class FunctionLiteral final : public Expression {
2492 ANONYMOUS_EXPRESSION,
2497 enum ParameterFlag {
2498 kNoDuplicateParameters = 0,
2499 kHasDuplicateParameters = 1
2502 enum IsFunctionFlag {
2507 enum EagerCompileHint { kShouldEagerCompile, kShouldLazyCompile };
2509 enum ArityRestriction {
2515 DECLARE_NODE_TYPE(FunctionLiteral)
2517 Handle<String> name() const { return raw_name_->string(); }
2518 const AstRawString* raw_name() const { return raw_name_; }
2519 Scope* scope() const { return scope_; }
2520 ZoneList<Statement*>* body() const { return body_; }
2521 void set_function_token_position(int pos) { function_token_position_ = pos; }
2522 int function_token_position() const { return function_token_position_; }
2523 int start_position() const;
2524 int end_position() const;
2525 int SourceSize() const { return end_position() - start_position(); }
2526 bool is_expression() const { return IsExpression::decode(bitfield_); }
2527 bool is_anonymous() const { return IsAnonymous::decode(bitfield_); }
2528 LanguageMode language_mode() const;
2529 bool uses_super_property() const;
2531 static bool NeedsHomeObject(Expression* literal) {
2532 return literal != NULL && literal->IsFunctionLiteral() &&
2533 literal->AsFunctionLiteral()->uses_super_property();
2536 int materialized_literal_count() { return materialized_literal_count_; }
2537 int expected_property_count() { return expected_property_count_; }
2538 int handler_count() { return handler_count_; }
2539 int parameter_count() { return parameter_count_; }
2541 bool AllowsLazyCompilation();
2542 bool AllowsLazyCompilationWithoutContext();
2544 void InitializeSharedInfo(Handle<Code> code);
2546 Handle<String> debug_name() const {
2547 if (raw_name_ != NULL && !raw_name_->IsEmpty()) {
2548 return raw_name_->string();
2550 return inferred_name();
2553 Handle<String> inferred_name() const {
2554 if (!inferred_name_.is_null()) {
2555 DCHECK(raw_inferred_name_ == NULL);
2556 return inferred_name_;
2558 if (raw_inferred_name_ != NULL) {
2559 return raw_inferred_name_->string();
2562 return Handle<String>();
2565 // Only one of {set_inferred_name, set_raw_inferred_name} should be called.
2566 void set_inferred_name(Handle<String> inferred_name) {
2567 DCHECK(!inferred_name.is_null());
2568 inferred_name_ = inferred_name;
2569 DCHECK(raw_inferred_name_== NULL || raw_inferred_name_->IsEmpty());
2570 raw_inferred_name_ = NULL;
2573 void set_raw_inferred_name(const AstString* raw_inferred_name) {
2574 DCHECK(raw_inferred_name != NULL);
2575 raw_inferred_name_ = raw_inferred_name;
2576 DCHECK(inferred_name_.is_null());
2577 inferred_name_ = Handle<String>();
2580 // shared_info may be null if it's not cached in full code.
2581 Handle<SharedFunctionInfo> shared_info() { return shared_info_; }
2583 bool pretenure() { return Pretenure::decode(bitfield_); }
2584 void set_pretenure() { bitfield_ |= Pretenure::encode(true); }
2586 bool has_duplicate_parameters() {
2587 return HasDuplicateParameters::decode(bitfield_);
2590 bool is_function() { return IsFunction::decode(bitfield_) == kIsFunction; }
2592 // This is used as a heuristic on when to eagerly compile a function
2593 // literal. We consider the following constructs as hints that the
2594 // function will be called immediately:
2595 // - (function() { ... })();
2596 // - var x = function() { ... }();
2597 bool should_eager_compile() const {
2598 return EagerCompileHintBit::decode(bitfield_) == kShouldEagerCompile;
2600 void set_should_eager_compile() {
2601 bitfield_ = EagerCompileHintBit::update(bitfield_, kShouldEagerCompile);
2604 FunctionKind kind() { return FunctionKindBits::decode(bitfield_); }
2606 int ast_node_count() { return ast_properties_.node_count(); }
2607 AstProperties::Flags* flags() { return ast_properties_.flags(); }
2608 void set_ast_properties(AstProperties* ast_properties) {
2609 ast_properties_ = *ast_properties;
2611 const ZoneFeedbackVectorSpec* feedback_vector_spec() const {
2612 return ast_properties_.get_spec();
2614 bool dont_optimize() { return dont_optimize_reason_ != kNoReason; }
2615 BailoutReason dont_optimize_reason() { return dont_optimize_reason_; }
2616 void set_dont_optimize_reason(BailoutReason reason) {
2617 dont_optimize_reason_ = reason;
2621 FunctionLiteral(Zone* zone, const AstRawString* name,
2622 AstValueFactory* ast_value_factory, Scope* scope,
2623 ZoneList<Statement*>* body, int materialized_literal_count,
2624 int expected_property_count, int handler_count,
2625 int parameter_count, FunctionType function_type,
2626 ParameterFlag has_duplicate_parameters,
2627 IsFunctionFlag is_function,
2628 EagerCompileHint eager_compile_hint, FunctionKind kind,
2630 : Expression(zone, position),
2634 raw_inferred_name_(ast_value_factory->empty_string()),
2635 ast_properties_(zone),
2636 dont_optimize_reason_(kNoReason),
2637 materialized_literal_count_(materialized_literal_count),
2638 expected_property_count_(expected_property_count),
2639 handler_count_(handler_count),
2640 parameter_count_(parameter_count),
2641 function_token_position_(RelocInfo::kNoPosition) {
2642 bitfield_ = IsExpression::encode(function_type != DECLARATION) |
2643 IsAnonymous::encode(function_type == ANONYMOUS_EXPRESSION) |
2644 Pretenure::encode(false) |
2645 HasDuplicateParameters::encode(has_duplicate_parameters) |
2646 IsFunction::encode(is_function) |
2647 EagerCompileHintBit::encode(eager_compile_hint) |
2648 FunctionKindBits::encode(kind);
2649 DCHECK(IsValidFunctionKind(kind));
2653 const AstRawString* raw_name_;
2654 Handle<String> name_;
2655 Handle<SharedFunctionInfo> shared_info_;
2657 ZoneList<Statement*>* body_;
2658 const AstString* raw_inferred_name_;
2659 Handle<String> inferred_name_;
2660 AstProperties ast_properties_;
2661 BailoutReason dont_optimize_reason_;
2663 int materialized_literal_count_;
2664 int expected_property_count_;
2666 int parameter_count_;
2667 int function_token_position_;
2670 class IsExpression : public BitField<bool, 0, 1> {};
2671 class IsAnonymous : public BitField<bool, 1, 1> {};
2672 class Pretenure : public BitField<bool, 2, 1> {};
2673 class HasDuplicateParameters : public BitField<ParameterFlag, 3, 1> {};
2674 class IsFunction : public BitField<IsFunctionFlag, 4, 1> {};
2675 class EagerCompileHintBit : public BitField<EagerCompileHint, 5, 1> {};
2676 class FunctionKindBits : public BitField<FunctionKind, 6, 8> {};
2680 class ClassLiteral final : public Expression {
2682 typedef ObjectLiteralProperty Property;
2684 DECLARE_NODE_TYPE(ClassLiteral)
2686 Handle<String> name() const { return raw_name_->string(); }
2687 const AstRawString* raw_name() const { return raw_name_; }
2688 Scope* scope() const { return scope_; }
2689 VariableProxy* class_variable_proxy() const { return class_variable_proxy_; }
2690 Expression* extends() const { return extends_; }
2691 FunctionLiteral* constructor() const { return constructor_; }
2692 ZoneList<Property*>* properties() const { return properties_; }
2693 int start_position() const { return position(); }
2694 int end_position() const { return end_position_; }
2696 BailoutId EntryId() const { return BailoutId(local_id(0)); }
2697 BailoutId DeclsId() const { return BailoutId(local_id(1)); }
2698 BailoutId ExitId() { return BailoutId(local_id(2)); }
2700 // Return an AST id for a property that is used in simulate instructions.
2701 BailoutId GetIdForProperty(int i) { return BailoutId(local_id(i + 3)); }
2703 // Unlike other AST nodes, this number of bailout IDs allocated for an
2704 // ClassLiteral can vary, so num_ids() is not a static method.
2705 int num_ids() const { return parent_num_ids() + 3 + properties()->length(); }
2708 ClassLiteral(Zone* zone, const AstRawString* name, Scope* scope,
2709 VariableProxy* class_variable_proxy, Expression* extends,
2710 FunctionLiteral* constructor, ZoneList<Property*>* properties,
2711 int start_position, int end_position)
2712 : Expression(zone, start_position),
2715 class_variable_proxy_(class_variable_proxy),
2717 constructor_(constructor),
2718 properties_(properties),
2719 end_position_(end_position) {}
2720 static int parent_num_ids() { return Expression::num_ids(); }
2723 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
2725 const AstRawString* raw_name_;
2727 VariableProxy* class_variable_proxy_;
2728 Expression* extends_;
2729 FunctionLiteral* constructor_;
2730 ZoneList<Property*>* properties_;
2735 class NativeFunctionLiteral final : public Expression {
2737 DECLARE_NODE_TYPE(NativeFunctionLiteral)
2739 Handle<String> name() const { return name_->string(); }
2740 v8::Extension* extension() const { return extension_; }
2743 NativeFunctionLiteral(Zone* zone, const AstRawString* name,
2744 v8::Extension* extension, int pos)
2745 : Expression(zone, pos), name_(name), extension_(extension) {}
2748 const AstRawString* name_;
2749 v8::Extension* extension_;
2753 class ThisFunction final : public Expression {
2755 DECLARE_NODE_TYPE(ThisFunction)
2758 ThisFunction(Zone* zone, int pos) : Expression(zone, pos) {}
2762 class SuperReference final : public Expression {
2764 DECLARE_NODE_TYPE(SuperReference)
2766 VariableProxy* this_var() const { return this_var_; }
2768 static int num_ids() { return parent_num_ids() + 1; }
2769 TypeFeedbackId HomeObjectFeedbackId() { return TypeFeedbackId(local_id(0)); }
2771 // Type feedback information.
2772 virtual FeedbackVectorRequirements ComputeFeedbackRequirements(
2773 Isolate* isolate, const ICSlotCache* cache) override {
2774 return FeedbackVectorRequirements(0, FLAG_vector_ics ? 1 : 0);
2776 void SetFirstFeedbackICSlot(FeedbackVectorICSlot slot,
2777 ICSlotCache* cache) override {
2778 homeobject_feedback_slot_ = slot;
2780 Code::Kind FeedbackICSlotKind(int index) override { return Code::LOAD_IC; }
2782 FeedbackVectorICSlot HomeObjectFeedbackSlot() {
2783 DCHECK(!FLAG_vector_ics || !homeobject_feedback_slot_.IsInvalid());
2784 return homeobject_feedback_slot_;
2788 SuperReference(Zone* zone, VariableProxy* this_var, int pos)
2789 : Expression(zone, pos),
2790 this_var_(this_var),
2791 homeobject_feedback_slot_(FeedbackVectorICSlot::Invalid()) {
2792 DCHECK(this_var->is_this());
2794 static int parent_num_ids() { return Expression::num_ids(); }
2797 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
2799 VariableProxy* this_var_;
2800 FeedbackVectorICSlot homeobject_feedback_slot_;
2804 #undef DECLARE_NODE_TYPE
2807 // ----------------------------------------------------------------------------
2808 // Regular expressions
2811 class RegExpVisitor BASE_EMBEDDED {
2813 virtual ~RegExpVisitor() { }
2814 #define MAKE_CASE(Name) \
2815 virtual void* Visit##Name(RegExp##Name*, void* data) = 0;
2816 FOR_EACH_REG_EXP_TREE_TYPE(MAKE_CASE)
2821 class RegExpTree : public ZoneObject {
2823 static const int kInfinity = kMaxInt;
2824 virtual ~RegExpTree() {}
2825 virtual void* Accept(RegExpVisitor* visitor, void* data) = 0;
2826 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
2827 RegExpNode* on_success) = 0;
2828 virtual bool IsTextElement() { return false; }
2829 virtual bool IsAnchoredAtStart() { return false; }
2830 virtual bool IsAnchoredAtEnd() { return false; }
2831 virtual int min_match() = 0;
2832 virtual int max_match() = 0;
2833 // Returns the interval of registers used for captures within this
2835 virtual Interval CaptureRegisters() { return Interval::Empty(); }
2836 virtual void AppendToText(RegExpText* text, Zone* zone);
2837 std::ostream& Print(std::ostream& os, Zone* zone); // NOLINT
2838 #define MAKE_ASTYPE(Name) \
2839 virtual RegExp##Name* As##Name(); \
2840 virtual bool Is##Name();
2841 FOR_EACH_REG_EXP_TREE_TYPE(MAKE_ASTYPE)
2846 class RegExpDisjunction final : public RegExpTree {
2848 explicit RegExpDisjunction(ZoneList<RegExpTree*>* alternatives);
2849 void* Accept(RegExpVisitor* visitor, void* data) override;
2850 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
2851 RegExpNode* on_success) override;
2852 RegExpDisjunction* AsDisjunction() override;
2853 Interval CaptureRegisters() override;
2854 bool IsDisjunction() override;
2855 bool IsAnchoredAtStart() override;
2856 bool IsAnchoredAtEnd() override;
2857 int min_match() override { return min_match_; }
2858 int max_match() override { return max_match_; }
2859 ZoneList<RegExpTree*>* alternatives() { return alternatives_; }
2861 ZoneList<RegExpTree*>* alternatives_;
2867 class RegExpAlternative final : public RegExpTree {
2869 explicit RegExpAlternative(ZoneList<RegExpTree*>* nodes);
2870 void* Accept(RegExpVisitor* visitor, void* data) override;
2871 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
2872 RegExpNode* on_success) override;
2873 RegExpAlternative* AsAlternative() override;
2874 Interval CaptureRegisters() override;
2875 bool IsAlternative() override;
2876 bool IsAnchoredAtStart() override;
2877 bool IsAnchoredAtEnd() override;
2878 int min_match() override { return min_match_; }
2879 int max_match() override { return max_match_; }
2880 ZoneList<RegExpTree*>* nodes() { return nodes_; }
2882 ZoneList<RegExpTree*>* nodes_;
2888 class RegExpAssertion final : public RegExpTree {
2890 enum AssertionType {
2898 explicit RegExpAssertion(AssertionType type) : assertion_type_(type) { }
2899 void* Accept(RegExpVisitor* visitor, void* data) override;
2900 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
2901 RegExpNode* on_success) override;
2902 RegExpAssertion* AsAssertion() override;
2903 bool IsAssertion() override;
2904 bool IsAnchoredAtStart() override;
2905 bool IsAnchoredAtEnd() override;
2906 int min_match() override { return 0; }
2907 int max_match() override { return 0; }
2908 AssertionType assertion_type() { return assertion_type_; }
2910 AssertionType assertion_type_;
2914 class CharacterSet final BASE_EMBEDDED {
2916 explicit CharacterSet(uc16 standard_set_type)
2918 standard_set_type_(standard_set_type) {}
2919 explicit CharacterSet(ZoneList<CharacterRange>* ranges)
2921 standard_set_type_(0) {}
2922 ZoneList<CharacterRange>* ranges(Zone* zone);
2923 uc16 standard_set_type() { return standard_set_type_; }
2924 void set_standard_set_type(uc16 special_set_type) {
2925 standard_set_type_ = special_set_type;
2927 bool is_standard() { return standard_set_type_ != 0; }
2928 void Canonicalize();
2930 ZoneList<CharacterRange>* ranges_;
2931 // If non-zero, the value represents a standard set (e.g., all whitespace
2932 // characters) without having to expand the ranges.
2933 uc16 standard_set_type_;
2937 class RegExpCharacterClass final : public RegExpTree {
2939 RegExpCharacterClass(ZoneList<CharacterRange>* ranges, bool is_negated)
2941 is_negated_(is_negated) { }
2942 explicit RegExpCharacterClass(uc16 type)
2944 is_negated_(false) { }
2945 void* Accept(RegExpVisitor* visitor, void* data) override;
2946 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
2947 RegExpNode* on_success) override;
2948 RegExpCharacterClass* AsCharacterClass() override;
2949 bool IsCharacterClass() override;
2950 bool IsTextElement() override { return true; }
2951 int min_match() override { return 1; }
2952 int max_match() override { return 1; }
2953 void AppendToText(RegExpText* text, Zone* zone) override;
2954 CharacterSet character_set() { return set_; }
2955 // TODO(lrn): Remove need for complex version if is_standard that
2956 // recognizes a mangled standard set and just do { return set_.is_special(); }
2957 bool is_standard(Zone* zone);
2958 // Returns a value representing the standard character set if is_standard()
2960 // Currently used values are:
2961 // s : unicode whitespace
2962 // S : unicode non-whitespace
2963 // w : ASCII word character (digit, letter, underscore)
2964 // W : non-ASCII word character
2966 // D : non-ASCII digit
2967 // . : non-unicode non-newline
2968 // * : All characters
2969 uc16 standard_type() { return set_.standard_set_type(); }
2970 ZoneList<CharacterRange>* ranges(Zone* zone) { return set_.ranges(zone); }
2971 bool is_negated() { return is_negated_; }
2979 class RegExpAtom final : public RegExpTree {
2981 explicit RegExpAtom(Vector<const uc16> data) : data_(data) { }
2982 void* Accept(RegExpVisitor* visitor, void* data) override;
2983 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
2984 RegExpNode* on_success) override;
2985 RegExpAtom* AsAtom() override;
2986 bool IsAtom() override;
2987 bool IsTextElement() override { return true; }
2988 int min_match() override { return data_.length(); }
2989 int max_match() override { return data_.length(); }
2990 void AppendToText(RegExpText* text, Zone* zone) override;
2991 Vector<const uc16> data() { return data_; }
2992 int length() { return data_.length(); }
2994 Vector<const uc16> data_;
2998 class RegExpText final : public RegExpTree {
3000 explicit RegExpText(Zone* zone) : elements_(2, zone), length_(0) {}
3001 void* Accept(RegExpVisitor* visitor, void* data) override;
3002 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
3003 RegExpNode* on_success) override;
3004 RegExpText* AsText() override;
3005 bool IsText() override;
3006 bool IsTextElement() override { return true; }
3007 int min_match() override { return length_; }
3008 int max_match() override { return length_; }
3009 void AppendToText(RegExpText* text, Zone* zone) override;
3010 void AddElement(TextElement elm, Zone* zone) {
3011 elements_.Add(elm, zone);
3012 length_ += elm.length();
3014 ZoneList<TextElement>* elements() { return &elements_; }
3016 ZoneList<TextElement> elements_;
3021 class RegExpQuantifier final : public RegExpTree {
3023 enum QuantifierType { GREEDY, NON_GREEDY, POSSESSIVE };
3024 RegExpQuantifier(int min, int max, QuantifierType type, RegExpTree* body)
3028 min_match_(min * body->min_match()),
3029 quantifier_type_(type) {
3030 if (max > 0 && body->max_match() > kInfinity / max) {
3031 max_match_ = kInfinity;
3033 max_match_ = max * body->max_match();
3036 void* Accept(RegExpVisitor* visitor, void* data) override;
3037 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
3038 RegExpNode* on_success) override;
3039 static RegExpNode* ToNode(int min,
3043 RegExpCompiler* compiler,
3044 RegExpNode* on_success,
3045 bool not_at_start = false);
3046 RegExpQuantifier* AsQuantifier() override;
3047 Interval CaptureRegisters() override;
3048 bool IsQuantifier() override;
3049 int min_match() override { return min_match_; }
3050 int max_match() override { return max_match_; }
3051 int min() { return min_; }
3052 int max() { return max_; }
3053 bool is_possessive() { return quantifier_type_ == POSSESSIVE; }
3054 bool is_non_greedy() { return quantifier_type_ == NON_GREEDY; }
3055 bool is_greedy() { return quantifier_type_ == GREEDY; }
3056 RegExpTree* body() { return body_; }
3064 QuantifierType quantifier_type_;
3068 class RegExpCapture final : public RegExpTree {
3070 explicit RegExpCapture(RegExpTree* body, int index)
3071 : body_(body), index_(index) { }
3072 void* Accept(RegExpVisitor* visitor, void* data) override;
3073 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
3074 RegExpNode* on_success) override;
3075 static RegExpNode* ToNode(RegExpTree* body,
3077 RegExpCompiler* compiler,
3078 RegExpNode* on_success);
3079 RegExpCapture* AsCapture() override;
3080 bool IsAnchoredAtStart() override;
3081 bool IsAnchoredAtEnd() override;
3082 Interval CaptureRegisters() override;
3083 bool IsCapture() override;
3084 int min_match() override { return body_->min_match(); }
3085 int max_match() override { return body_->max_match(); }
3086 RegExpTree* body() { return body_; }
3087 int index() { return index_; }
3088 static int StartRegister(int index) { return index * 2; }
3089 static int EndRegister(int index) { return index * 2 + 1; }
3097 class RegExpLookahead final : public RegExpTree {
3099 RegExpLookahead(RegExpTree* body,
3104 is_positive_(is_positive),
3105 capture_count_(capture_count),
3106 capture_from_(capture_from) { }
3108 void* Accept(RegExpVisitor* visitor, void* data) override;
3109 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
3110 RegExpNode* on_success) override;
3111 RegExpLookahead* AsLookahead() override;
3112 Interval CaptureRegisters() override;
3113 bool IsLookahead() override;
3114 bool IsAnchoredAtStart() override;
3115 int min_match() override { return 0; }
3116 int max_match() override { return 0; }
3117 RegExpTree* body() { return body_; }
3118 bool is_positive() { return is_positive_; }
3119 int capture_count() { return capture_count_; }
3120 int capture_from() { return capture_from_; }
3130 class RegExpBackReference final : public RegExpTree {
3132 explicit RegExpBackReference(RegExpCapture* capture)
3133 : capture_(capture) { }
3134 void* Accept(RegExpVisitor* visitor, void* data) override;
3135 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
3136 RegExpNode* on_success) override;
3137 RegExpBackReference* AsBackReference() override;
3138 bool IsBackReference() override;
3139 int min_match() override { return 0; }
3140 int max_match() override { return capture_->max_match(); }
3141 int index() { return capture_->index(); }
3142 RegExpCapture* capture() { return capture_; }
3144 RegExpCapture* capture_;
3148 class RegExpEmpty final : public RegExpTree {
3151 void* Accept(RegExpVisitor* visitor, void* data) override;
3152 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
3153 RegExpNode* on_success) override;
3154 RegExpEmpty* AsEmpty() override;
3155 bool IsEmpty() override;
3156 int min_match() override { return 0; }
3157 int max_match() override { return 0; }
3161 // ----------------------------------------------------------------------------
3163 // - leaf node visitors are abstract.
3165 class AstVisitor BASE_EMBEDDED {
3168 virtual ~AstVisitor() {}
3170 // Stack overflow check and dynamic dispatch.
3171 virtual void Visit(AstNode* node) = 0;
3173 // Iteration left-to-right.
3174 virtual void VisitDeclarations(ZoneList<Declaration*>* declarations);
3175 virtual void VisitStatements(ZoneList<Statement*>* statements);
3176 virtual void VisitExpressions(ZoneList<Expression*>* expressions);
3178 // Individual AST nodes.
3179 #define DEF_VISIT(type) \
3180 virtual void Visit##type(type* node) = 0;
3181 AST_NODE_LIST(DEF_VISIT)
3186 #define DEFINE_AST_VISITOR_SUBCLASS_MEMBERS() \
3188 void Visit(AstNode* node) final { \
3189 if (!CheckStackOverflow()) node->Accept(this); \
3192 void SetStackOverflow() { stack_overflow_ = true; } \
3193 void ClearStackOverflow() { stack_overflow_ = false; } \
3194 bool HasStackOverflow() const { return stack_overflow_; } \
3196 bool CheckStackOverflow() { \
3197 if (stack_overflow_) return true; \
3198 StackLimitCheck check(isolate_); \
3199 if (!check.HasOverflowed()) return false; \
3200 stack_overflow_ = true; \
3205 void InitializeAstVisitor(Isolate* isolate, Zone* zone) { \
3206 isolate_ = isolate; \
3208 stack_overflow_ = false; \
3210 Zone* zone() { return zone_; } \
3211 Isolate* isolate() { return isolate_; } \
3213 Isolate* isolate_; \
3215 bool stack_overflow_
3218 // ----------------------------------------------------------------------------
3221 class AstNodeFactory final BASE_EMBEDDED {
3223 explicit AstNodeFactory(AstValueFactory* ast_value_factory)
3224 : zone_(ast_value_factory->zone()),
3225 ast_value_factory_(ast_value_factory) {}
3227 VariableDeclaration* NewVariableDeclaration(
3228 VariableProxy* proxy, VariableMode mode, Scope* scope, int pos,
3229 bool is_class_declaration = false, int declaration_group_start = -1) {
3231 VariableDeclaration(zone_, proxy, mode, scope, pos,
3232 is_class_declaration, declaration_group_start);
3235 FunctionDeclaration* NewFunctionDeclaration(VariableProxy* proxy,
3237 FunctionLiteral* fun,
3240 return new (zone_) FunctionDeclaration(zone_, proxy, mode, fun, scope, pos);
3243 ModuleDeclaration* NewModuleDeclaration(VariableProxy* proxy,
3247 return new (zone_) ModuleDeclaration(zone_, proxy, module, scope, pos);
3250 ImportDeclaration* NewImportDeclaration(VariableProxy* proxy,
3251 const AstRawString* import_name,
3252 const AstRawString* module_specifier,
3253 Scope* scope, int pos) {
3254 return new (zone_) ImportDeclaration(zone_, proxy, import_name,
3255 module_specifier, scope, pos);
3258 ExportDeclaration* NewExportDeclaration(VariableProxy* proxy,
3261 return new (zone_) ExportDeclaration(zone_, proxy, scope, pos);
3264 ModuleLiteral* NewModuleLiteral(Block* body, ModuleDescriptor* descriptor,
3266 return new (zone_) ModuleLiteral(zone_, body, descriptor, pos);
3269 ModulePath* NewModulePath(Module* origin, const AstRawString* name, int pos) {
3270 return new (zone_) ModulePath(zone_, origin, name, pos);
3273 ModuleUrl* NewModuleUrl(Handle<String> url, int pos) {
3274 return new (zone_) ModuleUrl(zone_, url, pos);
3277 Block* NewBlock(ZoneList<const AstRawString*>* labels,
3279 bool is_initializer_block,
3282 Block(zone_, labels, capacity, is_initializer_block, pos);
3285 #define STATEMENT_WITH_LABELS(NodeType) \
3286 NodeType* New##NodeType(ZoneList<const AstRawString*>* labels, int pos) { \
3287 return new (zone_) NodeType(zone_, labels, pos); \
3289 STATEMENT_WITH_LABELS(DoWhileStatement)
3290 STATEMENT_WITH_LABELS(WhileStatement)
3291 STATEMENT_WITH_LABELS(ForStatement)
3292 STATEMENT_WITH_LABELS(SwitchStatement)
3293 #undef STATEMENT_WITH_LABELS
3295 ForEachStatement* NewForEachStatement(ForEachStatement::VisitMode visit_mode,
3296 ZoneList<const AstRawString*>* labels,
3298 switch (visit_mode) {
3299 case ForEachStatement::ENUMERATE: {
3300 return new (zone_) ForInStatement(zone_, labels, pos);
3302 case ForEachStatement::ITERATE: {
3303 return new (zone_) ForOfStatement(zone_, labels, pos);
3310 ModuleStatement* NewModuleStatement(Block* body, int pos) {
3311 return new (zone_) ModuleStatement(zone_, body, pos);
3314 ExpressionStatement* NewExpressionStatement(Expression* expression, int pos) {
3315 return new (zone_) ExpressionStatement(zone_, expression, pos);
3318 ContinueStatement* NewContinueStatement(IterationStatement* target, int pos) {
3319 return new (zone_) ContinueStatement(zone_, target, pos);
3322 BreakStatement* NewBreakStatement(BreakableStatement* target, int pos) {
3323 return new (zone_) BreakStatement(zone_, target, pos);
3326 ReturnStatement* NewReturnStatement(Expression* expression, int pos) {
3327 return new (zone_) ReturnStatement(zone_, expression, pos);
3330 WithStatement* NewWithStatement(Scope* scope,
3331 Expression* expression,
3332 Statement* statement,
3334 return new (zone_) WithStatement(zone_, scope, expression, statement, pos);
3337 IfStatement* NewIfStatement(Expression* condition,
3338 Statement* then_statement,
3339 Statement* else_statement,
3342 IfStatement(zone_, condition, then_statement, else_statement, pos);
3345 TryCatchStatement* NewTryCatchStatement(int index,
3351 return new (zone_) TryCatchStatement(zone_, index, try_block, scope,
3352 variable, catch_block, pos);
3355 TryFinallyStatement* NewTryFinallyStatement(int index,
3357 Block* finally_block,
3360 TryFinallyStatement(zone_, index, try_block, finally_block, pos);
3363 DebuggerStatement* NewDebuggerStatement(int pos) {
3364 return new (zone_) DebuggerStatement(zone_, pos);
3367 EmptyStatement* NewEmptyStatement(int pos) {
3368 return new(zone_) EmptyStatement(zone_, pos);
3371 CaseClause* NewCaseClause(
3372 Expression* label, ZoneList<Statement*>* statements, int pos) {
3373 return new (zone_) CaseClause(zone_, label, statements, pos);
3376 Literal* NewStringLiteral(const AstRawString* string, int pos) {
3378 Literal(zone_, ast_value_factory_->NewString(string), pos);
3381 // A JavaScript symbol (ECMA-262 edition 6).
3382 Literal* NewSymbolLiteral(const char* name, int pos) {
3383 return new (zone_) Literal(zone_, ast_value_factory_->NewSymbol(name), pos);
3386 Literal* NewNumberLiteral(double number, int pos) {
3388 Literal(zone_, ast_value_factory_->NewNumber(number), pos);
3391 Literal* NewSmiLiteral(int number, int pos) {
3392 return new (zone_) Literal(zone_, ast_value_factory_->NewSmi(number), pos);
3395 Literal* NewBooleanLiteral(bool b, int pos) {
3396 return new (zone_) Literal(zone_, ast_value_factory_->NewBoolean(b), pos);
3399 Literal* NewNullLiteral(int pos) {
3400 return new (zone_) Literal(zone_, ast_value_factory_->NewNull(), pos);
3403 Literal* NewUndefinedLiteral(int pos) {
3404 return new (zone_) Literal(zone_, ast_value_factory_->NewUndefined(), pos);
3407 Literal* NewTheHoleLiteral(int pos) {
3408 return new (zone_) Literal(zone_, ast_value_factory_->NewTheHole(), pos);
3411 ObjectLiteral* NewObjectLiteral(
3412 ZoneList<ObjectLiteral::Property*>* properties,
3414 int boilerplate_properties,
3417 return new (zone_) ObjectLiteral(zone_, properties, literal_index,
3418 boilerplate_properties, has_function, pos);
3421 ObjectLiteral::Property* NewObjectLiteralProperty(
3422 Expression* key, Expression* value, ObjectLiteralProperty::Kind kind,
3423 bool is_static, bool is_computed_name) {
3425 ObjectLiteral::Property(key, value, kind, is_static, is_computed_name);
3428 ObjectLiteral::Property* NewObjectLiteralProperty(Expression* key,
3431 bool is_computed_name) {
3432 return new (zone_) ObjectLiteral::Property(ast_value_factory_, key, value,
3433 is_static, is_computed_name);
3436 RegExpLiteral* NewRegExpLiteral(const AstRawString* pattern,
3437 const AstRawString* flags,
3440 return new (zone_) RegExpLiteral(zone_, pattern, flags, literal_index, pos);
3443 ArrayLiteral* NewArrayLiteral(ZoneList<Expression*>* values,
3446 return new (zone_) ArrayLiteral(zone_, values, literal_index, pos);
3449 VariableProxy* NewVariableProxy(Variable* var,
3450 int start_position = RelocInfo::kNoPosition,
3451 int end_position = RelocInfo::kNoPosition) {
3452 return new (zone_) VariableProxy(zone_, var, start_position, end_position);
3455 VariableProxy* NewVariableProxy(const AstRawString* name,
3456 Variable::Kind variable_kind,
3457 int start_position = RelocInfo::kNoPosition,
3458 int end_position = RelocInfo::kNoPosition) {
3460 VariableProxy(zone_, name, variable_kind, start_position, end_position);
3463 Property* NewProperty(Expression* obj, Expression* key, int pos) {
3464 return new (zone_) Property(zone_, obj, key, pos);
3467 Call* NewCall(Expression* expression,
3468 ZoneList<Expression*>* arguments,
3470 return new (zone_) Call(zone_, expression, arguments, pos);
3473 CallNew* NewCallNew(Expression* expression,
3474 ZoneList<Expression*>* arguments,
3476 return new (zone_) CallNew(zone_, expression, arguments, pos);
3479 CallRuntime* NewCallRuntime(const AstRawString* name,
3480 const Runtime::Function* function,
3481 ZoneList<Expression*>* arguments,
3483 return new (zone_) CallRuntime(zone_, name, function, arguments, pos);
3486 UnaryOperation* NewUnaryOperation(Token::Value op,
3487 Expression* expression,
3489 return new (zone_) UnaryOperation(zone_, op, expression, pos);
3492 BinaryOperation* NewBinaryOperation(Token::Value op,
3496 return new (zone_) BinaryOperation(zone_, op, left, right, pos);
3499 CountOperation* NewCountOperation(Token::Value op,
3503 return new (zone_) CountOperation(zone_, op, is_prefix, expr, pos);
3506 CompareOperation* NewCompareOperation(Token::Value op,
3510 return new (zone_) CompareOperation(zone_, op, left, right, pos);
3513 Spread* NewSpread(Expression* expression, int pos) {
3514 return new (zone_) Spread(zone_, expression, pos);
3517 Conditional* NewConditional(Expression* condition,
3518 Expression* then_expression,
3519 Expression* else_expression,
3521 return new (zone_) Conditional(zone_, condition, then_expression,
3522 else_expression, position);
3525 Assignment* NewAssignment(Token::Value op,
3529 DCHECK(Token::IsAssignmentOp(op));
3530 Assignment* assign = new (zone_) Assignment(zone_, op, target, value, pos);
3531 if (assign->is_compound()) {
3532 DCHECK(Token::IsAssignmentOp(op));
3533 assign->binary_operation_ =
3534 NewBinaryOperation(assign->binary_op(), target, value, pos + 1);
3539 Yield* NewYield(Expression *generator_object,
3540 Expression* expression,
3541 Yield::Kind yield_kind,
3543 if (!expression) expression = NewUndefinedLiteral(pos);
3545 Yield(zone_, generator_object, expression, yield_kind, pos);
3548 Throw* NewThrow(Expression* exception, int pos) {
3549 return new (zone_) Throw(zone_, exception, pos);
3552 FunctionLiteral* NewFunctionLiteral(
3553 const AstRawString* name, AstValueFactory* ast_value_factory,
3554 Scope* scope, ZoneList<Statement*>* body, int materialized_literal_count,
3555 int expected_property_count, int handler_count, int parameter_count,
3556 FunctionLiteral::ParameterFlag has_duplicate_parameters,
3557 FunctionLiteral::FunctionType function_type,
3558 FunctionLiteral::IsFunctionFlag is_function,
3559 FunctionLiteral::EagerCompileHint eager_compile_hint, FunctionKind kind,
3561 return new (zone_) FunctionLiteral(
3562 zone_, name, ast_value_factory, scope, body, materialized_literal_count,
3563 expected_property_count, handler_count, parameter_count, function_type,
3564 has_duplicate_parameters, is_function, eager_compile_hint, kind,
3568 ClassLiteral* NewClassLiteral(const AstRawString* name, Scope* scope,
3569 VariableProxy* proxy, Expression* extends,
3570 FunctionLiteral* constructor,
3571 ZoneList<ObjectLiteral::Property*>* properties,
3572 int start_position, int end_position) {
3574 ClassLiteral(zone_, name, scope, proxy, extends, constructor,
3575 properties, start_position, end_position);
3578 NativeFunctionLiteral* NewNativeFunctionLiteral(const AstRawString* name,
3579 v8::Extension* extension,
3581 return new (zone_) NativeFunctionLiteral(zone_, name, extension, pos);
3584 ThisFunction* NewThisFunction(int pos) {
3585 return new (zone_) ThisFunction(zone_, pos);
3588 SuperReference* NewSuperReference(VariableProxy* this_var, int pos) {
3589 return new (zone_) SuperReference(zone_, this_var, pos);
3594 AstValueFactory* ast_value_factory_;
3598 } } // namespace v8::internal