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_parenthesized() const {
376 return IsParenthesizedField::decode(bit_field_);
378 bool is_multi_parenthesized() const {
379 return IsMultiParenthesizedField::decode(bit_field_);
381 void increase_parenthesization_level() {
383 IsMultiParenthesizedField::update(bit_field_, is_parenthesized());
384 bit_field_ = IsParenthesizedField::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 IsParenthesizedField : 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_; }
574 VariableDeclaration(Zone* zone, VariableProxy* proxy, VariableMode mode,
575 Scope* scope, int pos, bool is_class_declaration = false)
576 : Declaration(zone, proxy, mode, scope, pos),
577 is_class_declaration_(is_class_declaration) {}
579 bool is_class_declaration_;
583 class FunctionDeclaration final : public Declaration {
585 DECLARE_NODE_TYPE(FunctionDeclaration)
587 FunctionLiteral* fun() const { return fun_; }
588 InitializationFlag initialization() const override {
589 return kCreatedInitialized;
591 bool IsInlineable() const override;
594 FunctionDeclaration(Zone* zone,
595 VariableProxy* proxy,
597 FunctionLiteral* fun,
600 : Declaration(zone, proxy, mode, scope, pos),
602 DCHECK(mode == VAR || mode == LET || mode == CONST);
607 FunctionLiteral* fun_;
611 class ModuleDeclaration final : public Declaration {
613 DECLARE_NODE_TYPE(ModuleDeclaration)
615 Module* module() const { return module_; }
616 InitializationFlag initialization() const override {
617 return kCreatedInitialized;
621 ModuleDeclaration(Zone* zone, VariableProxy* proxy, Module* module,
622 Scope* scope, int pos)
623 : Declaration(zone, proxy, CONST, scope, pos), module_(module) {}
630 class ImportDeclaration final : public Declaration {
632 DECLARE_NODE_TYPE(ImportDeclaration)
634 const AstRawString* import_name() const { return import_name_; }
635 const AstRawString* module_specifier() const { return module_specifier_; }
636 void set_module_specifier(const AstRawString* module_specifier) {
637 DCHECK(module_specifier_ == NULL);
638 module_specifier_ = module_specifier;
640 InitializationFlag initialization() const override {
641 return kNeedsInitialization;
645 ImportDeclaration(Zone* zone, VariableProxy* proxy,
646 const AstRawString* import_name,
647 const AstRawString* module_specifier, Scope* scope, int pos)
648 : Declaration(zone, proxy, IMPORT, scope, pos),
649 import_name_(import_name),
650 module_specifier_(module_specifier) {}
653 const AstRawString* import_name_;
654 const AstRawString* module_specifier_;
658 class ExportDeclaration final : public Declaration {
660 DECLARE_NODE_TYPE(ExportDeclaration)
662 InitializationFlag initialization() const override {
663 return kCreatedInitialized;
667 ExportDeclaration(Zone* zone, VariableProxy* proxy, Scope* scope, int pos)
668 : Declaration(zone, proxy, LET, scope, pos) {}
672 class Module : public AstNode {
674 ModuleDescriptor* descriptor() const { return descriptor_; }
675 Block* body() const { return body_; }
678 Module(Zone* zone, int pos)
679 : AstNode(pos), descriptor_(ModuleDescriptor::New(zone)), body_(NULL) {}
680 Module(Zone* zone, ModuleDescriptor* descriptor, int pos, Block* body = NULL)
681 : AstNode(pos), descriptor_(descriptor), body_(body) {}
684 ModuleDescriptor* descriptor_;
689 class ModuleLiteral final : public Module {
691 DECLARE_NODE_TYPE(ModuleLiteral)
694 ModuleLiteral(Zone* zone, Block* body, ModuleDescriptor* descriptor, int pos)
695 : Module(zone, descriptor, pos, body) {}
699 class ModulePath final : public Module {
701 DECLARE_NODE_TYPE(ModulePath)
703 Module* module() const { return module_; }
704 Handle<String> name() const { return name_->string(); }
707 ModulePath(Zone* zone, Module* module, const AstRawString* name, int pos)
708 : Module(zone, pos), module_(module), name_(name) {}
712 const AstRawString* name_;
716 class ModuleUrl final : public Module {
718 DECLARE_NODE_TYPE(ModuleUrl)
720 Handle<String> url() const { return url_; }
723 ModuleUrl(Zone* zone, Handle<String> url, int pos)
724 : Module(zone, pos), url_(url) {
732 class ModuleStatement final : public Statement {
734 DECLARE_NODE_TYPE(ModuleStatement)
736 Block* body() const { return body_; }
739 ModuleStatement(Zone* zone, Block* body, int pos)
740 : Statement(zone, pos), body_(body) {}
747 class IterationStatement : public BreakableStatement {
749 // Type testing & conversion.
750 IterationStatement* AsIterationStatement() final { return this; }
752 Statement* body() const { return body_; }
754 static int num_ids() { return parent_num_ids() + 1; }
755 BailoutId OsrEntryId() const { return BailoutId(local_id(0)); }
756 virtual BailoutId ContinueId() const = 0;
757 virtual BailoutId StackCheckId() const = 0;
760 Label* continue_target() { return &continue_target_; }
763 IterationStatement(Zone* zone, ZoneList<const AstRawString*>* labels, int pos)
764 : BreakableStatement(zone, labels, TARGET_FOR_ANONYMOUS, pos),
766 static int parent_num_ids() { return BreakableStatement::num_ids(); }
767 void Initialize(Statement* body) { body_ = body; }
770 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
773 Label continue_target_;
777 class DoWhileStatement final : public IterationStatement {
779 DECLARE_NODE_TYPE(DoWhileStatement)
781 void Initialize(Expression* cond, Statement* body) {
782 IterationStatement::Initialize(body);
786 Expression* cond() const { return cond_; }
788 static int num_ids() { return parent_num_ids() + 2; }
789 BailoutId ContinueId() const override { return BailoutId(local_id(0)); }
790 BailoutId StackCheckId() const override { return BackEdgeId(); }
791 BailoutId BackEdgeId() const { return BailoutId(local_id(1)); }
794 DoWhileStatement(Zone* zone, ZoneList<const AstRawString*>* labels, int pos)
795 : IterationStatement(zone, labels, pos), cond_(NULL) {}
796 static int parent_num_ids() { return IterationStatement::num_ids(); }
799 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
805 class WhileStatement final : public IterationStatement {
807 DECLARE_NODE_TYPE(WhileStatement)
809 void Initialize(Expression* cond, Statement* body) {
810 IterationStatement::Initialize(body);
814 Expression* cond() const { return cond_; }
816 static int num_ids() { return parent_num_ids() + 1; }
817 BailoutId ContinueId() const override { return EntryId(); }
818 BailoutId StackCheckId() const override { return BodyId(); }
819 BailoutId BodyId() const { return BailoutId(local_id(0)); }
822 WhileStatement(Zone* zone, ZoneList<const AstRawString*>* labels, int pos)
823 : IterationStatement(zone, labels, pos), cond_(NULL) {}
824 static int parent_num_ids() { return IterationStatement::num_ids(); }
827 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
833 class ForStatement final : public IterationStatement {
835 DECLARE_NODE_TYPE(ForStatement)
837 void Initialize(Statement* init,
841 IterationStatement::Initialize(body);
847 Statement* init() const { return init_; }
848 Expression* cond() const { return cond_; }
849 Statement* next() const { return next_; }
851 static int num_ids() { return parent_num_ids() + 2; }
852 BailoutId ContinueId() const override { return BailoutId(local_id(0)); }
853 BailoutId StackCheckId() const override { return BodyId(); }
854 BailoutId BodyId() const { return BailoutId(local_id(1)); }
857 ForStatement(Zone* zone, ZoneList<const AstRawString*>* labels, int pos)
858 : IterationStatement(zone, labels, pos),
862 static int parent_num_ids() { return IterationStatement::num_ids(); }
865 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
873 class ForEachStatement : public IterationStatement {
876 ENUMERATE, // for (each in subject) body;
877 ITERATE // for (each of subject) body;
880 void Initialize(Expression* each, Expression* subject, Statement* body) {
881 IterationStatement::Initialize(body);
886 Expression* each() const { return each_; }
887 Expression* subject() const { return subject_; }
890 ForEachStatement(Zone* zone, ZoneList<const AstRawString*>* labels, int pos)
891 : IterationStatement(zone, labels, pos), each_(NULL), subject_(NULL) {}
895 Expression* subject_;
899 class ForInStatement final : public ForEachStatement {
901 DECLARE_NODE_TYPE(ForInStatement)
903 Expression* enumerable() const {
907 // Type feedback information.
908 virtual FeedbackVectorRequirements ComputeFeedbackRequirements(
909 Isolate* isolate, const ICSlotCache* cache) override {
910 return FeedbackVectorRequirements(1, 0);
912 void SetFirstFeedbackSlot(FeedbackVectorSlot slot) override {
913 for_in_feedback_slot_ = slot;
916 FeedbackVectorSlot ForInFeedbackSlot() {
917 DCHECK(!for_in_feedback_slot_.IsInvalid());
918 return for_in_feedback_slot_;
921 enum ForInType { FAST_FOR_IN, SLOW_FOR_IN };
922 ForInType for_in_type() const { return for_in_type_; }
923 void set_for_in_type(ForInType type) { for_in_type_ = type; }
925 static int num_ids() { return parent_num_ids() + 6; }
926 BailoutId BodyId() const { return BailoutId(local_id(0)); }
927 BailoutId PrepareId() const { return BailoutId(local_id(1)); }
928 BailoutId EnumId() const { return BailoutId(local_id(2)); }
929 BailoutId ToObjectId() const { return BailoutId(local_id(3)); }
930 BailoutId FilterId() const { return BailoutId(local_id(4)); }
931 BailoutId AssignmentId() const { return BailoutId(local_id(5)); }
932 BailoutId ContinueId() const override { return EntryId(); }
933 BailoutId StackCheckId() const override { return BodyId(); }
936 ForInStatement(Zone* zone, ZoneList<const AstRawString*>* labels, int pos)
937 : ForEachStatement(zone, labels, pos),
938 for_in_type_(SLOW_FOR_IN),
939 for_in_feedback_slot_(FeedbackVectorSlot::Invalid()) {}
940 static int parent_num_ids() { return ForEachStatement::num_ids(); }
943 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
945 ForInType for_in_type_;
946 FeedbackVectorSlot for_in_feedback_slot_;
950 class ForOfStatement final : public ForEachStatement {
952 DECLARE_NODE_TYPE(ForOfStatement)
954 void Initialize(Expression* each,
957 Expression* assign_iterator,
958 Expression* next_result,
959 Expression* result_done,
960 Expression* assign_each) {
961 ForEachStatement::Initialize(each, subject, body);
962 assign_iterator_ = assign_iterator;
963 next_result_ = next_result;
964 result_done_ = result_done;
965 assign_each_ = assign_each;
968 Expression* iterable() const {
972 // iterator = subject[Symbol.iterator]()
973 Expression* assign_iterator() const {
974 return assign_iterator_;
977 // result = iterator.next() // with type check
978 Expression* next_result() const {
983 Expression* result_done() const {
987 // each = result.value
988 Expression* assign_each() const {
992 BailoutId ContinueId() const override { return EntryId(); }
993 BailoutId StackCheckId() const override { return BackEdgeId(); }
995 static int num_ids() { return parent_num_ids() + 1; }
996 BailoutId BackEdgeId() const { return BailoutId(local_id(0)); }
999 ForOfStatement(Zone* zone, ZoneList<const AstRawString*>* labels, int pos)
1000 : ForEachStatement(zone, labels, pos),
1001 assign_iterator_(NULL),
1004 assign_each_(NULL) {}
1005 static int parent_num_ids() { return ForEachStatement::num_ids(); }
1008 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
1010 Expression* assign_iterator_;
1011 Expression* next_result_;
1012 Expression* result_done_;
1013 Expression* assign_each_;
1017 class ExpressionStatement final : public Statement {
1019 DECLARE_NODE_TYPE(ExpressionStatement)
1021 void set_expression(Expression* e) { expression_ = e; }
1022 Expression* expression() const { return expression_; }
1023 bool IsJump() const override { return expression_->IsThrow(); }
1026 ExpressionStatement(Zone* zone, Expression* expression, int pos)
1027 : Statement(zone, pos), expression_(expression) { }
1030 Expression* expression_;
1034 class JumpStatement : public Statement {
1036 bool IsJump() const final { return true; }
1039 explicit JumpStatement(Zone* zone, int pos) : Statement(zone, pos) {}
1043 class ContinueStatement final : public JumpStatement {
1045 DECLARE_NODE_TYPE(ContinueStatement)
1047 IterationStatement* target() const { return target_; }
1050 explicit ContinueStatement(Zone* zone, IterationStatement* target, int pos)
1051 : JumpStatement(zone, pos), target_(target) { }
1054 IterationStatement* target_;
1058 class BreakStatement final : public JumpStatement {
1060 DECLARE_NODE_TYPE(BreakStatement)
1062 BreakableStatement* target() const { return target_; }
1065 explicit BreakStatement(Zone* zone, BreakableStatement* target, int pos)
1066 : JumpStatement(zone, pos), target_(target) { }
1069 BreakableStatement* target_;
1073 class ReturnStatement final : public JumpStatement {
1075 DECLARE_NODE_TYPE(ReturnStatement)
1077 Expression* expression() const { return expression_; }
1080 explicit ReturnStatement(Zone* zone, Expression* expression, int pos)
1081 : JumpStatement(zone, pos), expression_(expression) { }
1084 Expression* expression_;
1088 class WithStatement final : public Statement {
1090 DECLARE_NODE_TYPE(WithStatement)
1092 Scope* scope() { return scope_; }
1093 Expression* expression() const { return expression_; }
1094 Statement* statement() const { return statement_; }
1096 void set_base_id(int id) { base_id_ = id; }
1097 static int num_ids() { return parent_num_ids() + 1; }
1098 BailoutId EntryId() const { return BailoutId(local_id(0)); }
1101 WithStatement(Zone* zone, Scope* scope, Expression* expression,
1102 Statement* statement, int pos)
1103 : Statement(zone, pos),
1105 expression_(expression),
1106 statement_(statement),
1107 base_id_(BailoutId::None().ToInt()) {}
1108 static int parent_num_ids() { return 0; }
1110 int base_id() const {
1111 DCHECK(!BailoutId(base_id_).IsNone());
1116 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
1119 Expression* expression_;
1120 Statement* statement_;
1125 class CaseClause final : public Expression {
1127 DECLARE_NODE_TYPE(CaseClause)
1129 bool is_default() const { return label_ == NULL; }
1130 Expression* label() const {
1131 CHECK(!is_default());
1134 Label* body_target() { return &body_target_; }
1135 ZoneList<Statement*>* statements() const { return statements_; }
1137 static int num_ids() { return parent_num_ids() + 2; }
1138 BailoutId EntryId() const { return BailoutId(local_id(0)); }
1139 TypeFeedbackId CompareId() { return TypeFeedbackId(local_id(1)); }
1141 Type* compare_type() { return compare_type_; }
1142 void set_compare_type(Type* type) { compare_type_ = type; }
1145 static int parent_num_ids() { return Expression::num_ids(); }
1148 CaseClause(Zone* zone, Expression* label, ZoneList<Statement*>* statements,
1150 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
1154 ZoneList<Statement*>* statements_;
1155 Type* compare_type_;
1159 class SwitchStatement final : public BreakableStatement {
1161 DECLARE_NODE_TYPE(SwitchStatement)
1163 void Initialize(Expression* tag, ZoneList<CaseClause*>* cases) {
1168 Expression* tag() const { return tag_; }
1169 ZoneList<CaseClause*>* cases() const { return cases_; }
1172 SwitchStatement(Zone* zone, ZoneList<const AstRawString*>* labels, int pos)
1173 : BreakableStatement(zone, labels, TARGET_FOR_ANONYMOUS, pos),
1179 ZoneList<CaseClause*>* cases_;
1183 // If-statements always have non-null references to their then- and
1184 // else-parts. When parsing if-statements with no explicit else-part,
1185 // the parser implicitly creates an empty statement. Use the
1186 // HasThenStatement() and HasElseStatement() functions to check if a
1187 // given if-statement has a then- or an else-part containing code.
1188 class IfStatement final : public Statement {
1190 DECLARE_NODE_TYPE(IfStatement)
1192 bool HasThenStatement() const { return !then_statement()->IsEmpty(); }
1193 bool HasElseStatement() const { return !else_statement()->IsEmpty(); }
1195 Expression* condition() const { return condition_; }
1196 Statement* then_statement() const { return then_statement_; }
1197 Statement* else_statement() const { return else_statement_; }
1199 bool IsJump() const override {
1200 return HasThenStatement() && then_statement()->IsJump()
1201 && HasElseStatement() && else_statement()->IsJump();
1204 void set_base_id(int id) { base_id_ = id; }
1205 static int num_ids() { return parent_num_ids() + 3; }
1206 BailoutId IfId() const { return BailoutId(local_id(0)); }
1207 BailoutId ThenId() const { return BailoutId(local_id(1)); }
1208 BailoutId ElseId() const { return BailoutId(local_id(2)); }
1211 IfStatement(Zone* zone, Expression* condition, Statement* then_statement,
1212 Statement* else_statement, int pos)
1213 : Statement(zone, pos),
1214 condition_(condition),
1215 then_statement_(then_statement),
1216 else_statement_(else_statement),
1217 base_id_(BailoutId::None().ToInt()) {}
1218 static int parent_num_ids() { return 0; }
1220 int base_id() const {
1221 DCHECK(!BailoutId(base_id_).IsNone());
1226 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
1228 Expression* condition_;
1229 Statement* then_statement_;
1230 Statement* else_statement_;
1235 class TryStatement : public Statement {
1237 int index() const { return index_; }
1238 Block* try_block() const { return try_block_; }
1241 TryStatement(Zone* zone, int index, Block* try_block, int pos)
1242 : Statement(zone, pos), index_(index), try_block_(try_block) {}
1245 // Unique (per-function) index of this handler. This is not an AST ID.
1252 class TryCatchStatement final : public TryStatement {
1254 DECLARE_NODE_TYPE(TryCatchStatement)
1256 Scope* scope() { return scope_; }
1257 Variable* variable() { return variable_; }
1258 Block* catch_block() const { return catch_block_; }
1261 TryCatchStatement(Zone* zone,
1268 : TryStatement(zone, index, try_block, pos),
1270 variable_(variable),
1271 catch_block_(catch_block) {
1276 Variable* variable_;
1277 Block* catch_block_;
1281 class TryFinallyStatement final : public TryStatement {
1283 DECLARE_NODE_TYPE(TryFinallyStatement)
1285 Block* finally_block() const { return finally_block_; }
1288 TryFinallyStatement(
1289 Zone* zone, int index, Block* try_block, Block* finally_block, int pos)
1290 : TryStatement(zone, index, try_block, pos),
1291 finally_block_(finally_block) { }
1294 Block* finally_block_;
1298 class DebuggerStatement final : public Statement {
1300 DECLARE_NODE_TYPE(DebuggerStatement)
1302 void set_base_id(int id) { base_id_ = id; }
1303 static int num_ids() { return parent_num_ids() + 1; }
1304 BailoutId DebugBreakId() const { return BailoutId(local_id(0)); }
1307 explicit DebuggerStatement(Zone* zone, int pos)
1308 : Statement(zone, pos), base_id_(BailoutId::None().ToInt()) {}
1309 static int parent_num_ids() { return 0; }
1311 int base_id() const {
1312 DCHECK(!BailoutId(base_id_).IsNone());
1317 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
1323 class EmptyStatement final : public Statement {
1325 DECLARE_NODE_TYPE(EmptyStatement)
1328 explicit EmptyStatement(Zone* zone, int pos): Statement(zone, pos) {}
1332 class Literal final : public Expression {
1334 DECLARE_NODE_TYPE(Literal)
1336 bool IsPropertyName() const override { return value_->IsPropertyName(); }
1338 Handle<String> AsPropertyName() {
1339 DCHECK(IsPropertyName());
1340 return Handle<String>::cast(value());
1343 const AstRawString* AsRawPropertyName() {
1344 DCHECK(IsPropertyName());
1345 return value_->AsString();
1348 bool ToBooleanIsTrue() const override { return value()->BooleanValue(); }
1349 bool ToBooleanIsFalse() const override { return !value()->BooleanValue(); }
1351 Handle<Object> value() const { return value_->value(); }
1352 const AstValue* raw_value() const { return value_; }
1354 // Support for using Literal as a HashMap key. NOTE: Currently, this works
1355 // only for string and number literals!
1357 static bool Match(void* literal1, void* literal2);
1359 static int num_ids() { return parent_num_ids() + 1; }
1360 TypeFeedbackId LiteralFeedbackId() const {
1361 return TypeFeedbackId(local_id(0));
1365 Literal(Zone* zone, const AstValue* value, int position)
1366 : Expression(zone, position), value_(value) {}
1367 static int parent_num_ids() { return Expression::num_ids(); }
1370 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
1372 const AstValue* value_;
1376 // Base class for literals that needs space in the corresponding JSFunction.
1377 class MaterializedLiteral : public Expression {
1379 virtual MaterializedLiteral* AsMaterializedLiteral() { return this; }
1381 int literal_index() { return literal_index_; }
1384 // only callable after initialization.
1385 DCHECK(depth_ >= 1);
1390 MaterializedLiteral(Zone* zone, int literal_index, int pos)
1391 : Expression(zone, pos),
1392 literal_index_(literal_index),
1396 // A materialized literal is simple if the values consist of only
1397 // constants and simple object and array literals.
1398 bool is_simple() const { return is_simple_; }
1399 void set_is_simple(bool is_simple) { is_simple_ = is_simple; }
1400 friend class CompileTimeValue;
1402 void set_depth(int depth) {
1407 // Populate the constant properties/elements fixed array.
1408 void BuildConstants(Isolate* isolate);
1409 friend class ArrayLiteral;
1410 friend class ObjectLiteral;
1412 // If the expression is a literal, return the literal value;
1413 // if the expression is a materialized literal and is simple return a
1414 // compile time value as encoded by CompileTimeValue::GetValue().
1415 // Otherwise, return undefined literal as the placeholder
1416 // in the object literal boilerplate.
1417 Handle<Object> GetBoilerplateValue(Expression* expression, Isolate* isolate);
1426 // Property is used for passing information
1427 // about an object literal's properties from the parser
1428 // to the code generator.
1429 class ObjectLiteralProperty final : public ZoneObject {
1432 CONSTANT, // Property with constant value (compile time).
1433 COMPUTED, // Property with computed value (execution time).
1434 MATERIALIZED_LITERAL, // Property value is a materialized literal.
1435 GETTER, SETTER, // Property is an accessor function.
1436 PROTOTYPE // Property is __proto__.
1439 Expression* key() { return key_; }
1440 Expression* value() { return value_; }
1441 Kind kind() { return kind_; }
1443 // Type feedback information.
1444 bool IsMonomorphic() { return !receiver_type_.is_null(); }
1445 Handle<Map> GetReceiverType() { return receiver_type_; }
1447 bool IsCompileTimeValue();
1449 void set_emit_store(bool emit_store);
1452 bool is_static() const { return is_static_; }
1453 bool is_computed_name() const { return is_computed_name_; }
1455 void set_receiver_type(Handle<Map> map) { receiver_type_ = map; }
1458 friend class AstNodeFactory;
1460 ObjectLiteralProperty(Expression* key, Expression* value, Kind kind,
1461 bool is_static, bool is_computed_name);
1462 ObjectLiteralProperty(AstValueFactory* ast_value_factory, Expression* key,
1463 Expression* value, bool is_static,
1464 bool is_computed_name);
1472 bool is_computed_name_;
1473 Handle<Map> receiver_type_;
1477 // An object literal has a boilerplate object that is used
1478 // for minimizing the work when constructing it at runtime.
1479 class ObjectLiteral final : public MaterializedLiteral {
1481 typedef ObjectLiteralProperty Property;
1483 DECLARE_NODE_TYPE(ObjectLiteral)
1485 Handle<FixedArray> constant_properties() const {
1486 return constant_properties_;
1488 int properties_count() const { return constant_properties_->length() / 2; }
1489 ZoneList<Property*>* properties() const { return properties_; }
1490 bool fast_elements() const { return fast_elements_; }
1491 bool may_store_doubles() const { return may_store_doubles_; }
1492 bool has_function() const { return has_function_; }
1493 bool has_elements() const { return has_elements_; }
1495 // Decide if a property should be in the object boilerplate.
1496 static bool IsBoilerplateProperty(Property* property);
1498 // Populate the constant properties fixed array.
1499 void BuildConstantProperties(Isolate* isolate);
1501 // Mark all computed expressions that are bound to a key that
1502 // is shadowed by a later occurrence of the same key. For the
1503 // marked expressions, no store code is emitted.
1504 void CalculateEmitStore(Zone* zone);
1506 // Assemble bitfield of flags for the CreateObjectLiteral helper.
1507 int ComputeFlags(bool disable_mementos = false) const {
1508 int flags = fast_elements() ? kFastElements : kNoFlags;
1509 flags |= has_function() ? kHasFunction : kNoFlags;
1510 if (depth() == 1 && !has_elements() && !may_store_doubles()) {
1511 flags |= kShallowProperties;
1513 if (disable_mementos) {
1514 flags |= kDisableMementos;
1522 kHasFunction = 1 << 1,
1523 kShallowProperties = 1 << 2,
1524 kDisableMementos = 1 << 3
1527 struct Accessors: public ZoneObject {
1528 Accessors() : getter(NULL), setter(NULL) {}
1533 BailoutId CreateLiteralId() const { return BailoutId(local_id(0)); }
1535 // Return an AST id for a property that is used in simulate instructions.
1536 BailoutId GetIdForProperty(int i) { return BailoutId(local_id(i + 1)); }
1538 // Unlike other AST nodes, this number of bailout IDs allocated for an
1539 // ObjectLiteral can vary, so num_ids() is not a static method.
1540 int num_ids() const { return parent_num_ids() + 1 + properties()->length(); }
1543 ObjectLiteral(Zone* zone, ZoneList<Property*>* properties, int literal_index,
1544 int boilerplate_properties, bool has_function, int pos)
1545 : MaterializedLiteral(zone, literal_index, pos),
1546 properties_(properties),
1547 boilerplate_properties_(boilerplate_properties),
1548 fast_elements_(false),
1549 has_elements_(false),
1550 may_store_doubles_(false),
1551 has_function_(has_function) {}
1552 static int parent_num_ids() { return MaterializedLiteral::num_ids(); }
1555 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
1556 Handle<FixedArray> constant_properties_;
1557 ZoneList<Property*>* properties_;
1558 int boilerplate_properties_;
1559 bool fast_elements_;
1561 bool may_store_doubles_;
1566 // Node for capturing a regexp literal.
1567 class RegExpLiteral final : public MaterializedLiteral {
1569 DECLARE_NODE_TYPE(RegExpLiteral)
1571 Handle<String> pattern() const { return pattern_->string(); }
1572 Handle<String> flags() const { return flags_->string(); }
1575 RegExpLiteral(Zone* zone, const AstRawString* pattern,
1576 const AstRawString* flags, int literal_index, int pos)
1577 : MaterializedLiteral(zone, literal_index, pos),
1584 const AstRawString* pattern_;
1585 const AstRawString* flags_;
1589 // An array literal has a literals object that is used
1590 // for minimizing the work when constructing it at runtime.
1591 class ArrayLiteral final : public MaterializedLiteral {
1593 DECLARE_NODE_TYPE(ArrayLiteral)
1595 Handle<FixedArray> constant_elements() const { return constant_elements_; }
1596 ElementsKind constant_elements_kind() const {
1597 DCHECK_EQ(2, constant_elements_->length());
1598 return static_cast<ElementsKind>(
1599 Smi::cast(constant_elements_->get(0))->value());
1602 ZoneList<Expression*>* values() const { return values_; }
1604 BailoutId CreateLiteralId() const { return BailoutId(local_id(0)); }
1606 // Return an AST id for an element that is used in simulate instructions.
1607 BailoutId GetIdForElement(int i) { return BailoutId(local_id(i + 1)); }
1609 // Unlike other AST nodes, this number of bailout IDs allocated for an
1610 // ArrayLiteral can vary, so num_ids() is not a static method.
1611 int num_ids() const { return parent_num_ids() + 1 + values()->length(); }
1613 // Populate the constant elements fixed array.
1614 void BuildConstantElements(Isolate* isolate);
1616 // Assemble bitfield of flags for the CreateArrayLiteral helper.
1617 int ComputeFlags(bool disable_mementos = false) const {
1618 int flags = depth() == 1 ? kShallowElements : kNoFlags;
1619 if (disable_mementos) {
1620 flags |= kDisableMementos;
1627 kShallowElements = 1,
1628 kDisableMementos = 1 << 1
1632 ArrayLiteral(Zone* zone, ZoneList<Expression*>* values, int literal_index,
1634 : MaterializedLiteral(zone, literal_index, pos), values_(values) {}
1635 static int parent_num_ids() { return MaterializedLiteral::num_ids(); }
1638 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
1640 Handle<FixedArray> constant_elements_;
1641 ZoneList<Expression*>* values_;
1645 class VariableProxy final : public Expression {
1647 DECLARE_NODE_TYPE(VariableProxy)
1649 bool IsValidReferenceExpression() const override { return !is_this(); }
1651 bool IsArguments() const { return is_resolved() && var()->is_arguments(); }
1653 Handle<String> name() const { return raw_name()->string(); }
1654 const AstRawString* raw_name() const {
1655 return is_resolved() ? var_->raw_name() : raw_name_;
1658 Variable* var() const {
1659 DCHECK(is_resolved());
1662 void set_var(Variable* v) {
1663 DCHECK(!is_resolved());
1668 bool is_this() const { return IsThisField::decode(bit_field_); }
1670 bool is_assigned() const { return IsAssignedField::decode(bit_field_); }
1671 void set_is_assigned() {
1672 bit_field_ = IsAssignedField::update(bit_field_, true);
1675 bool is_resolved() const { return IsResolvedField::decode(bit_field_); }
1676 void set_is_resolved() {
1677 bit_field_ = IsResolvedField::update(bit_field_, true);
1680 int end_position() const { return end_position_; }
1682 // Bind this proxy to the variable var.
1683 void BindTo(Variable* var);
1685 bool UsesVariableFeedbackSlot() const {
1686 return FLAG_vector_ics && (var()->IsUnallocated() || var()->IsLookupSlot());
1689 virtual FeedbackVectorRequirements ComputeFeedbackRequirements(
1690 Isolate* isolate, const ICSlotCache* cache) override;
1692 void SetFirstFeedbackICSlot(FeedbackVectorICSlot slot,
1693 ICSlotCache* cache) override;
1694 Code::Kind FeedbackICSlotKind(int index) override { return Code::LOAD_IC; }
1695 FeedbackVectorICSlot VariableFeedbackSlot() {
1696 DCHECK(!UsesVariableFeedbackSlot() || !variable_feedback_slot_.IsInvalid());
1697 return variable_feedback_slot_;
1701 VariableProxy(Zone* zone, Variable* var, int start_position,
1704 VariableProxy(Zone* zone, const AstRawString* name,
1705 Variable::Kind variable_kind, int start_position,
1708 class IsThisField : public BitField8<bool, 0, 1> {};
1709 class IsAssignedField : public BitField8<bool, 1, 1> {};
1710 class IsResolvedField : public BitField8<bool, 2, 1> {};
1712 // Start with 16-bit (or smaller) field, which should get packed together
1713 // with Expression's trailing 16-bit field.
1715 FeedbackVectorICSlot variable_feedback_slot_;
1717 const AstRawString* raw_name_; // if !is_resolved_
1718 Variable* var_; // if is_resolved_
1720 // Position is stored in the AstNode superclass, but VariableProxy needs to
1721 // know its end position too (for error messages). It cannot be inferred from
1722 // the variable name length because it can contain escapes.
1727 class Property final : public Expression {
1729 DECLARE_NODE_TYPE(Property)
1731 bool IsValidReferenceExpression() const override { return true; }
1733 Expression* obj() const { return obj_; }
1734 Expression* key() const { return key_; }
1736 static int num_ids() { return parent_num_ids() + 2; }
1737 BailoutId LoadId() const { return BailoutId(local_id(0)); }
1738 TypeFeedbackId PropertyFeedbackId() { return TypeFeedbackId(local_id(1)); }
1740 bool IsStringAccess() const {
1741 return IsStringAccessField::decode(bit_field_);
1744 // Type feedback information.
1745 bool IsMonomorphic() override { return receiver_types_.length() == 1; }
1746 SmallMapList* GetReceiverTypes() override { return &receiver_types_; }
1747 KeyedAccessStoreMode GetStoreMode() const override { return STANDARD_STORE; }
1748 IcCheckType GetKeyType() const override {
1749 return KeyTypeField::decode(bit_field_);
1751 bool IsUninitialized() const {
1752 return !is_for_call() && HasNoTypeInformation();
1754 bool HasNoTypeInformation() const {
1755 return GetInlineCacheState() == UNINITIALIZED;
1757 InlineCacheState GetInlineCacheState() const {
1758 return InlineCacheStateField::decode(bit_field_);
1760 void set_is_string_access(bool b) {
1761 bit_field_ = IsStringAccessField::update(bit_field_, b);
1763 void set_key_type(IcCheckType key_type) {
1764 bit_field_ = KeyTypeField::update(bit_field_, key_type);
1766 void set_inline_cache_state(InlineCacheState state) {
1767 bit_field_ = InlineCacheStateField::update(bit_field_, state);
1769 void mark_for_call() {
1770 bit_field_ = IsForCallField::update(bit_field_, true);
1772 bool is_for_call() const { return IsForCallField::decode(bit_field_); }
1774 bool IsSuperAccess() {
1775 return obj()->IsSuperReference();
1778 virtual FeedbackVectorRequirements ComputeFeedbackRequirements(
1779 Isolate* isolate, const ICSlotCache* cache) override {
1780 return FeedbackVectorRequirements(0, FLAG_vector_ics ? 1 : 0);
1782 void SetFirstFeedbackICSlot(FeedbackVectorICSlot slot,
1783 ICSlotCache* cache) override {
1784 property_feedback_slot_ = slot;
1786 Code::Kind FeedbackICSlotKind(int index) override {
1787 return key()->IsPropertyName() ? Code::LOAD_IC : Code::KEYED_LOAD_IC;
1790 FeedbackVectorICSlot PropertyFeedbackSlot() const {
1791 DCHECK(!FLAG_vector_ics || !property_feedback_slot_.IsInvalid());
1792 return property_feedback_slot_;
1796 Property(Zone* zone, Expression* obj, Expression* key, int pos)
1797 : Expression(zone, pos),
1798 bit_field_(IsForCallField::encode(false) |
1799 IsStringAccessField::encode(false) |
1800 InlineCacheStateField::encode(UNINITIALIZED)),
1801 property_feedback_slot_(FeedbackVectorICSlot::Invalid()),
1804 static int parent_num_ids() { return Expression::num_ids(); }
1807 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
1809 class IsForCallField : public BitField8<bool, 0, 1> {};
1810 class IsStringAccessField : public BitField8<bool, 1, 1> {};
1811 class KeyTypeField : public BitField8<IcCheckType, 2, 1> {};
1812 class InlineCacheStateField : public BitField8<InlineCacheState, 3, 4> {};
1814 FeedbackVectorICSlot property_feedback_slot_;
1817 SmallMapList receiver_types_;
1821 class Call final : public Expression {
1823 DECLARE_NODE_TYPE(Call)
1825 Expression* expression() const { return expression_; }
1826 ZoneList<Expression*>* arguments() const { return arguments_; }
1828 // Type feedback information.
1829 virtual FeedbackVectorRequirements ComputeFeedbackRequirements(
1830 Isolate* isolate, const ICSlotCache* cache) override;
1831 void SetFirstFeedbackICSlot(FeedbackVectorICSlot slot,
1832 ICSlotCache* cache) override {
1833 ic_slot_or_slot_ = slot.ToInt();
1835 void SetFirstFeedbackSlot(FeedbackVectorSlot slot) override {
1836 ic_slot_or_slot_ = slot.ToInt();
1838 Code::Kind FeedbackICSlotKind(int index) override { return Code::CALL_IC; }
1840 FeedbackVectorSlot CallFeedbackSlot() const {
1841 DCHECK(ic_slot_or_slot_ != FeedbackVectorSlot::Invalid().ToInt());
1842 return FeedbackVectorSlot(ic_slot_or_slot_);
1845 FeedbackVectorICSlot CallFeedbackICSlot() const {
1846 DCHECK(ic_slot_or_slot_ != FeedbackVectorICSlot::Invalid().ToInt());
1847 return FeedbackVectorICSlot(ic_slot_or_slot_);
1850 SmallMapList* GetReceiverTypes() override {
1851 if (expression()->IsProperty()) {
1852 return expression()->AsProperty()->GetReceiverTypes();
1857 bool IsMonomorphic() override {
1858 if (expression()->IsProperty()) {
1859 return expression()->AsProperty()->IsMonomorphic();
1861 return !target_.is_null();
1864 bool global_call() const {
1865 VariableProxy* proxy = expression_->AsVariableProxy();
1866 return proxy != NULL && proxy->var()->IsUnallocated();
1869 bool known_global_function() const {
1870 return global_call() && !target_.is_null();
1873 Handle<JSFunction> target() { return target_; }
1875 Handle<AllocationSite> allocation_site() { return allocation_site_; }
1877 void SetKnownGlobalTarget(Handle<JSFunction> target) {
1879 set_is_uninitialized(false);
1881 void set_target(Handle<JSFunction> target) { target_ = target; }
1882 void set_allocation_site(Handle<AllocationSite> site) {
1883 allocation_site_ = site;
1886 static int num_ids() { return parent_num_ids() + 2; }
1887 BailoutId ReturnId() const { return BailoutId(local_id(0)); }
1888 BailoutId EvalOrLookupId() const { return BailoutId(local_id(1)); }
1890 bool is_uninitialized() const {
1891 return IsUninitializedField::decode(bit_field_);
1893 void set_is_uninitialized(bool b) {
1894 bit_field_ = IsUninitializedField::update(bit_field_, b);
1906 // Helpers to determine how to handle the call.
1907 CallType GetCallType(Isolate* isolate) const;
1908 bool IsUsingCallFeedbackSlot(Isolate* isolate) const;
1909 bool IsUsingCallFeedbackICSlot(Isolate* isolate) const;
1912 // Used to assert that the FullCodeGenerator records the return site.
1913 bool return_is_recorded_;
1917 Call(Zone* zone, Expression* expression, ZoneList<Expression*>* arguments,
1919 : Expression(zone, pos),
1920 ic_slot_or_slot_(FeedbackVectorICSlot::Invalid().ToInt()),
1921 expression_(expression),
1922 arguments_(arguments),
1923 bit_field_(IsUninitializedField::encode(false)) {
1924 if (expression->IsProperty()) {
1925 expression->AsProperty()->mark_for_call();
1928 static int parent_num_ids() { return Expression::num_ids(); }
1931 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
1933 // We store this as an integer because we don't know if we have a slot or
1934 // an ic slot until scoping time.
1935 int ic_slot_or_slot_;
1936 Expression* expression_;
1937 ZoneList<Expression*>* arguments_;
1938 Handle<JSFunction> target_;
1939 Handle<AllocationSite> allocation_site_;
1940 class IsUninitializedField : public BitField8<bool, 0, 1> {};
1945 class CallNew final : public Expression {
1947 DECLARE_NODE_TYPE(CallNew)
1949 Expression* expression() const { return expression_; }
1950 ZoneList<Expression*>* arguments() const { return arguments_; }
1952 // Type feedback information.
1953 virtual FeedbackVectorRequirements ComputeFeedbackRequirements(
1954 Isolate* isolate, const ICSlotCache* cache) override {
1955 return FeedbackVectorRequirements(FLAG_pretenuring_call_new ? 2 : 1, 0);
1957 void SetFirstFeedbackSlot(FeedbackVectorSlot slot) override {
1958 callnew_feedback_slot_ = slot;
1961 FeedbackVectorSlot CallNewFeedbackSlot() {
1962 DCHECK(!callnew_feedback_slot_.IsInvalid());
1963 return callnew_feedback_slot_;
1965 FeedbackVectorSlot AllocationSiteFeedbackSlot() {
1966 DCHECK(FLAG_pretenuring_call_new);
1967 return CallNewFeedbackSlot().next();
1970 bool IsMonomorphic() override { return is_monomorphic_; }
1971 Handle<JSFunction> target() const { return target_; }
1972 Handle<AllocationSite> allocation_site() const {
1973 return allocation_site_;
1976 static int num_ids() { return parent_num_ids() + 1; }
1977 static int feedback_slots() { return 1; }
1978 BailoutId ReturnId() const { return BailoutId(local_id(0)); }
1980 void set_allocation_site(Handle<AllocationSite> site) {
1981 allocation_site_ = site;
1983 void set_is_monomorphic(bool monomorphic) { is_monomorphic_ = monomorphic; }
1984 void set_target(Handle<JSFunction> target) { target_ = target; }
1985 void SetKnownGlobalTarget(Handle<JSFunction> target) {
1987 is_monomorphic_ = true;
1991 CallNew(Zone* zone, Expression* expression, ZoneList<Expression*>* arguments,
1993 : Expression(zone, pos),
1994 expression_(expression),
1995 arguments_(arguments),
1996 is_monomorphic_(false),
1997 callnew_feedback_slot_(FeedbackVectorSlot::Invalid()) {}
1999 static int parent_num_ids() { return Expression::num_ids(); }
2002 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
2004 Expression* expression_;
2005 ZoneList<Expression*>* arguments_;
2006 bool is_monomorphic_;
2007 Handle<JSFunction> target_;
2008 Handle<AllocationSite> allocation_site_;
2009 FeedbackVectorSlot callnew_feedback_slot_;
2013 // The CallRuntime class does not represent any official JavaScript
2014 // language construct. Instead it is used to call a C or JS function
2015 // with a set of arguments. This is used from the builtins that are
2016 // implemented in JavaScript (see "v8natives.js").
2017 class CallRuntime final : public Expression {
2019 DECLARE_NODE_TYPE(CallRuntime)
2021 Handle<String> name() const { return raw_name_->string(); }
2022 const AstRawString* raw_name() const { return raw_name_; }
2023 const Runtime::Function* function() const { return function_; }
2024 ZoneList<Expression*>* arguments() const { return arguments_; }
2025 bool is_jsruntime() const { return function_ == NULL; }
2027 // Type feedback information.
2028 bool HasCallRuntimeFeedbackSlot() const {
2029 return FLAG_vector_ics && is_jsruntime();
2031 virtual FeedbackVectorRequirements ComputeFeedbackRequirements(
2032 Isolate* isolate, const ICSlotCache* cache) override {
2033 return FeedbackVectorRequirements(0, HasCallRuntimeFeedbackSlot() ? 1 : 0);
2035 void SetFirstFeedbackICSlot(FeedbackVectorICSlot slot,
2036 ICSlotCache* cache) override {
2037 callruntime_feedback_slot_ = slot;
2039 Code::Kind FeedbackICSlotKind(int index) override { return Code::LOAD_IC; }
2041 FeedbackVectorICSlot CallRuntimeFeedbackSlot() {
2042 DCHECK(!HasCallRuntimeFeedbackSlot() ||
2043 !callruntime_feedback_slot_.IsInvalid());
2044 return callruntime_feedback_slot_;
2047 static int num_ids() { return parent_num_ids() + 1; }
2048 TypeFeedbackId CallRuntimeFeedbackId() const {
2049 return TypeFeedbackId(local_id(0));
2053 CallRuntime(Zone* zone, const AstRawString* name,
2054 const Runtime::Function* function,
2055 ZoneList<Expression*>* arguments, int pos)
2056 : Expression(zone, pos),
2058 function_(function),
2059 arguments_(arguments),
2060 callruntime_feedback_slot_(FeedbackVectorICSlot::Invalid()) {}
2061 static int parent_num_ids() { return Expression::num_ids(); }
2064 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
2066 const AstRawString* raw_name_;
2067 const Runtime::Function* function_;
2068 ZoneList<Expression*>* arguments_;
2069 FeedbackVectorICSlot callruntime_feedback_slot_;
2073 class UnaryOperation final : public Expression {
2075 DECLARE_NODE_TYPE(UnaryOperation)
2077 Token::Value op() const { return op_; }
2078 Expression* expression() const { return expression_; }
2080 // For unary not (Token::NOT), the AST ids where true and false will
2081 // actually be materialized, respectively.
2082 static int num_ids() { return parent_num_ids() + 2; }
2083 BailoutId MaterializeTrueId() const { return BailoutId(local_id(0)); }
2084 BailoutId MaterializeFalseId() const { return BailoutId(local_id(1)); }
2086 virtual void RecordToBooleanTypeFeedback(TypeFeedbackOracle* oracle) override;
2089 UnaryOperation(Zone* zone, Token::Value op, Expression* expression, int pos)
2090 : Expression(zone, pos), op_(op), expression_(expression) {
2091 DCHECK(Token::IsUnaryOp(op));
2093 static int parent_num_ids() { return Expression::num_ids(); }
2096 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
2099 Expression* expression_;
2103 class BinaryOperation final : public Expression {
2105 DECLARE_NODE_TYPE(BinaryOperation)
2107 Token::Value op() const { return static_cast<Token::Value>(op_); }
2108 Expression* left() const { return left_; }
2109 Expression* right() const { return right_; }
2110 Handle<AllocationSite> allocation_site() const { return allocation_site_; }
2111 void set_allocation_site(Handle<AllocationSite> allocation_site) {
2112 allocation_site_ = allocation_site;
2115 // The short-circuit logical operations need an AST ID for their
2116 // right-hand subexpression.
2117 static int num_ids() { return parent_num_ids() + 2; }
2118 BailoutId RightId() const { return BailoutId(local_id(0)); }
2120 TypeFeedbackId BinaryOperationFeedbackId() const {
2121 return TypeFeedbackId(local_id(1));
2123 Maybe<int> fixed_right_arg() const {
2124 return has_fixed_right_arg_ ? Just(fixed_right_arg_value_) : Nothing<int>();
2126 void set_fixed_right_arg(Maybe<int> arg) {
2127 has_fixed_right_arg_ = arg.IsJust();
2128 if (arg.IsJust()) fixed_right_arg_value_ = arg.FromJust();
2131 virtual void RecordToBooleanTypeFeedback(TypeFeedbackOracle* oracle) override;
2134 BinaryOperation(Zone* zone, Token::Value op, Expression* left,
2135 Expression* right, int pos)
2136 : Expression(zone, pos),
2137 op_(static_cast<byte>(op)),
2138 has_fixed_right_arg_(false),
2139 fixed_right_arg_value_(0),
2142 DCHECK(Token::IsBinaryOp(op));
2144 static int parent_num_ids() { return Expression::num_ids(); }
2147 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
2149 const byte op_; // actually Token::Value
2150 // TODO(rossberg): the fixed arg should probably be represented as a Constant
2151 // type for the RHS. Currenty it's actually a Maybe<int>
2152 bool has_fixed_right_arg_;
2153 int fixed_right_arg_value_;
2156 Handle<AllocationSite> allocation_site_;
2160 class CountOperation final : public Expression {
2162 DECLARE_NODE_TYPE(CountOperation)
2164 bool is_prefix() const { return IsPrefixField::decode(bit_field_); }
2165 bool is_postfix() const { return !is_prefix(); }
2167 Token::Value op() const { return TokenField::decode(bit_field_); }
2168 Token::Value binary_op() {
2169 return (op() == Token::INC) ? Token::ADD : Token::SUB;
2172 Expression* expression() const { return expression_; }
2174 bool IsMonomorphic() override { return receiver_types_.length() == 1; }
2175 SmallMapList* GetReceiverTypes() override { return &receiver_types_; }
2176 IcCheckType GetKeyType() const override {
2177 return KeyTypeField::decode(bit_field_);
2179 KeyedAccessStoreMode GetStoreMode() const override {
2180 return StoreModeField::decode(bit_field_);
2182 Type* type() const { return type_; }
2183 void set_key_type(IcCheckType type) {
2184 bit_field_ = KeyTypeField::update(bit_field_, type);
2186 void set_store_mode(KeyedAccessStoreMode mode) {
2187 bit_field_ = StoreModeField::update(bit_field_, mode);
2189 void set_type(Type* type) { type_ = type; }
2191 static int num_ids() { return parent_num_ids() + 4; }
2192 BailoutId AssignmentId() const { return BailoutId(local_id(0)); }
2193 BailoutId ToNumberId() const { return BailoutId(local_id(1)); }
2194 TypeFeedbackId CountBinOpFeedbackId() const {
2195 return TypeFeedbackId(local_id(2));
2197 TypeFeedbackId CountStoreFeedbackId() const {
2198 return TypeFeedbackId(local_id(3));
2202 CountOperation(Zone* zone, Token::Value op, bool is_prefix, Expression* expr,
2204 : Expression(zone, pos),
2205 bit_field_(IsPrefixField::encode(is_prefix) |
2206 KeyTypeField::encode(ELEMENT) |
2207 StoreModeField::encode(STANDARD_STORE) |
2208 TokenField::encode(op)),
2210 expression_(expr) {}
2211 static int parent_num_ids() { return Expression::num_ids(); }
2214 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
2216 class IsPrefixField : public BitField16<bool, 0, 1> {};
2217 class KeyTypeField : public BitField16<IcCheckType, 1, 1> {};
2218 class StoreModeField : public BitField16<KeyedAccessStoreMode, 2, 4> {};
2219 class TokenField : public BitField16<Token::Value, 6, 8> {};
2221 // Starts with 16-bit field, which should get packed together with
2222 // Expression's trailing 16-bit field.
2223 uint16_t bit_field_;
2225 Expression* expression_;
2226 SmallMapList receiver_types_;
2230 class CompareOperation final : public Expression {
2232 DECLARE_NODE_TYPE(CompareOperation)
2234 Token::Value op() const { return op_; }
2235 Expression* left() const { return left_; }
2236 Expression* right() const { return right_; }
2238 // Type feedback information.
2239 static int num_ids() { return parent_num_ids() + 1; }
2240 TypeFeedbackId CompareOperationFeedbackId() const {
2241 return TypeFeedbackId(local_id(0));
2243 Type* combined_type() const { return combined_type_; }
2244 void set_combined_type(Type* type) { combined_type_ = type; }
2246 // Match special cases.
2247 bool IsLiteralCompareTypeof(Expression** expr, Handle<String>* check);
2248 bool IsLiteralCompareUndefined(Expression** expr, Isolate* isolate);
2249 bool IsLiteralCompareNull(Expression** expr);
2252 CompareOperation(Zone* zone, Token::Value op, Expression* left,
2253 Expression* right, int pos)
2254 : Expression(zone, pos),
2258 combined_type_(Type::None(zone)) {
2259 DCHECK(Token::IsCompareOp(op));
2261 static int parent_num_ids() { return Expression::num_ids(); }
2264 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
2270 Type* combined_type_;
2274 class Spread final : public Expression {
2276 DECLARE_NODE_TYPE(Spread)
2278 Expression* expression() const { return expression_; }
2280 static int num_ids() { return parent_num_ids(); }
2283 Spread(Zone* zone, Expression* expression, int pos)
2284 : Expression(zone, pos), expression_(expression) {}
2285 static int parent_num_ids() { return Expression::num_ids(); }
2288 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
2290 Expression* expression_;
2294 class Conditional final : public Expression {
2296 DECLARE_NODE_TYPE(Conditional)
2298 Expression* condition() const { return condition_; }
2299 Expression* then_expression() const { return then_expression_; }
2300 Expression* else_expression() const { return else_expression_; }
2302 static int num_ids() { return parent_num_ids() + 2; }
2303 BailoutId ThenId() const { return BailoutId(local_id(0)); }
2304 BailoutId ElseId() const { return BailoutId(local_id(1)); }
2307 Conditional(Zone* zone, Expression* condition, Expression* then_expression,
2308 Expression* else_expression, int position)
2309 : Expression(zone, position),
2310 condition_(condition),
2311 then_expression_(then_expression),
2312 else_expression_(else_expression) {}
2313 static int parent_num_ids() { return Expression::num_ids(); }
2316 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
2318 Expression* condition_;
2319 Expression* then_expression_;
2320 Expression* else_expression_;
2324 class Assignment final : public Expression {
2326 DECLARE_NODE_TYPE(Assignment)
2328 Assignment* AsSimpleAssignment() { return !is_compound() ? this : NULL; }
2330 Token::Value binary_op() const;
2332 Token::Value op() const { return TokenField::decode(bit_field_); }
2333 Expression* target() const { return target_; }
2334 Expression* value() const { return value_; }
2335 BinaryOperation* binary_operation() const { return binary_operation_; }
2337 // This check relies on the definition order of token in token.h.
2338 bool is_compound() const { return op() > Token::ASSIGN; }
2340 static int num_ids() { return parent_num_ids() + 2; }
2341 BailoutId AssignmentId() const { return BailoutId(local_id(0)); }
2343 // Type feedback information.
2344 TypeFeedbackId AssignmentFeedbackId() { return TypeFeedbackId(local_id(1)); }
2345 bool IsMonomorphic() override { return receiver_types_.length() == 1; }
2346 bool IsUninitialized() const {
2347 return IsUninitializedField::decode(bit_field_);
2349 bool HasNoTypeInformation() {
2350 return IsUninitializedField::decode(bit_field_);
2352 SmallMapList* GetReceiverTypes() override { return &receiver_types_; }
2353 IcCheckType GetKeyType() const override {
2354 return KeyTypeField::decode(bit_field_);
2356 KeyedAccessStoreMode GetStoreMode() const override {
2357 return StoreModeField::decode(bit_field_);
2359 void set_is_uninitialized(bool b) {
2360 bit_field_ = IsUninitializedField::update(bit_field_, b);
2362 void set_key_type(IcCheckType key_type) {
2363 bit_field_ = KeyTypeField::update(bit_field_, key_type);
2365 void set_store_mode(KeyedAccessStoreMode mode) {
2366 bit_field_ = StoreModeField::update(bit_field_, mode);
2370 Assignment(Zone* zone, Token::Value op, Expression* target, Expression* value,
2372 static int parent_num_ids() { return Expression::num_ids(); }
2375 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
2377 class IsUninitializedField : public BitField16<bool, 0, 1> {};
2378 class KeyTypeField : public BitField16<IcCheckType, 1, 1> {};
2379 class StoreModeField : public BitField16<KeyedAccessStoreMode, 2, 4> {};
2380 class TokenField : public BitField16<Token::Value, 6, 8> {};
2382 // Starts with 16-bit field, which should get packed together with
2383 // Expression's trailing 16-bit field.
2384 uint16_t bit_field_;
2385 Expression* target_;
2387 BinaryOperation* binary_operation_;
2388 SmallMapList receiver_types_;
2392 class Yield final : public Expression {
2394 DECLARE_NODE_TYPE(Yield)
2397 kInitial, // The initial yield that returns the unboxed generator object.
2398 kSuspend, // A normal yield: { value: EXPRESSION, done: false }
2399 kDelegating, // A yield*.
2400 kFinal // A return: { value: EXPRESSION, done: true }
2403 Expression* generator_object() const { return generator_object_; }
2404 Expression* expression() const { return expression_; }
2405 Kind yield_kind() const { return yield_kind_; }
2407 // Delegating yield surrounds the "yield" in a "try/catch". This index
2408 // locates the catch handler in the handler table, and is equivalent to
2409 // TryCatchStatement::index().
2411 DCHECK_EQ(kDelegating, yield_kind());
2414 void set_index(int index) {
2415 DCHECK_EQ(kDelegating, yield_kind());
2419 // Type feedback information.
2420 bool HasFeedbackSlots() const {
2421 return FLAG_vector_ics && (yield_kind() == kDelegating);
2423 virtual FeedbackVectorRequirements ComputeFeedbackRequirements(
2424 Isolate* isolate, const ICSlotCache* cache) override {
2425 return FeedbackVectorRequirements(0, HasFeedbackSlots() ? 3 : 0);
2427 void SetFirstFeedbackICSlot(FeedbackVectorICSlot slot,
2428 ICSlotCache* cache) override {
2429 yield_first_feedback_slot_ = slot;
2431 Code::Kind FeedbackICSlotKind(int index) override {
2432 return index == 0 ? Code::KEYED_LOAD_IC : Code::LOAD_IC;
2435 FeedbackVectorICSlot KeyedLoadFeedbackSlot() {
2436 DCHECK(!HasFeedbackSlots() || !yield_first_feedback_slot_.IsInvalid());
2437 return yield_first_feedback_slot_;
2440 FeedbackVectorICSlot DoneFeedbackSlot() {
2441 return KeyedLoadFeedbackSlot().next();
2444 FeedbackVectorICSlot ValueFeedbackSlot() { return DoneFeedbackSlot().next(); }
2447 Yield(Zone* zone, Expression* generator_object, Expression* expression,
2448 Kind yield_kind, int pos)
2449 : Expression(zone, pos),
2450 generator_object_(generator_object),
2451 expression_(expression),
2452 yield_kind_(yield_kind),
2454 yield_first_feedback_slot_(FeedbackVectorICSlot::Invalid()) {}
2457 Expression* generator_object_;
2458 Expression* expression_;
2461 FeedbackVectorICSlot yield_first_feedback_slot_;
2465 class Throw final : public Expression {
2467 DECLARE_NODE_TYPE(Throw)
2469 Expression* exception() const { return exception_; }
2472 Throw(Zone* zone, Expression* exception, int pos)
2473 : Expression(zone, pos), exception_(exception) {}
2476 Expression* exception_;
2480 class FunctionLiteral final : public Expression {
2483 ANONYMOUS_EXPRESSION,
2488 enum ParameterFlag {
2489 kNoDuplicateParameters = 0,
2490 kHasDuplicateParameters = 1
2493 enum IsFunctionFlag {
2498 enum IsParenthesizedFlag {
2503 enum ArityRestriction {
2509 DECLARE_NODE_TYPE(FunctionLiteral)
2511 Handle<String> name() const { return raw_name_->string(); }
2512 const AstRawString* raw_name() const { return raw_name_; }
2513 Scope* scope() const { return scope_; }
2514 ZoneList<Statement*>* body() const { return body_; }
2515 void set_function_token_position(int pos) { function_token_position_ = pos; }
2516 int function_token_position() const { return function_token_position_; }
2517 int start_position() const;
2518 int end_position() const;
2519 int SourceSize() const { return end_position() - start_position(); }
2520 bool is_expression() const { return IsExpression::decode(bitfield_); }
2521 bool is_anonymous() const { return IsAnonymous::decode(bitfield_); }
2522 LanguageMode language_mode() const;
2523 bool uses_super_property() const;
2525 static bool NeedsHomeObject(Expression* literal) {
2526 return literal != NULL && literal->IsFunctionLiteral() &&
2527 literal->AsFunctionLiteral()->uses_super_property();
2530 int materialized_literal_count() { return materialized_literal_count_; }
2531 int expected_property_count() { return expected_property_count_; }
2532 int handler_count() { return handler_count_; }
2533 int parameter_count() { return parameter_count_; }
2535 bool AllowsLazyCompilation();
2536 bool AllowsLazyCompilationWithoutContext();
2538 void InitializeSharedInfo(Handle<Code> code);
2540 Handle<String> debug_name() const {
2541 if (raw_name_ != NULL && !raw_name_->IsEmpty()) {
2542 return raw_name_->string();
2544 return inferred_name();
2547 Handle<String> inferred_name() const {
2548 if (!inferred_name_.is_null()) {
2549 DCHECK(raw_inferred_name_ == NULL);
2550 return inferred_name_;
2552 if (raw_inferred_name_ != NULL) {
2553 return raw_inferred_name_->string();
2556 return Handle<String>();
2559 // Only one of {set_inferred_name, set_raw_inferred_name} should be called.
2560 void set_inferred_name(Handle<String> inferred_name) {
2561 DCHECK(!inferred_name.is_null());
2562 inferred_name_ = inferred_name;
2563 DCHECK(raw_inferred_name_== NULL || raw_inferred_name_->IsEmpty());
2564 raw_inferred_name_ = NULL;
2567 void set_raw_inferred_name(const AstString* raw_inferred_name) {
2568 DCHECK(raw_inferred_name != NULL);
2569 raw_inferred_name_ = raw_inferred_name;
2570 DCHECK(inferred_name_.is_null());
2571 inferred_name_ = Handle<String>();
2574 // shared_info may be null if it's not cached in full code.
2575 Handle<SharedFunctionInfo> shared_info() { return shared_info_; }
2577 bool pretenure() { return Pretenure::decode(bitfield_); }
2578 void set_pretenure() { bitfield_ |= Pretenure::encode(true); }
2580 bool has_duplicate_parameters() {
2581 return HasDuplicateParameters::decode(bitfield_);
2584 bool is_function() { return IsFunction::decode(bitfield_) == kIsFunction; }
2586 // This is used as a heuristic on when to eagerly compile a function
2587 // literal. We consider the following constructs as hints that the
2588 // function will be called immediately:
2589 // - (function() { ... })();
2590 // - var x = function() { ... }();
2591 bool is_parenthesized() {
2592 return IsParenthesized::decode(bitfield_) == kIsParenthesized;
2594 void set_parenthesized() {
2595 bitfield_ = IsParenthesized::update(bitfield_, kIsParenthesized);
2598 FunctionKind kind() { return FunctionKindBits::decode(bitfield_); }
2600 int ast_node_count() { return ast_properties_.node_count(); }
2601 AstProperties::Flags* flags() { return ast_properties_.flags(); }
2602 void set_ast_properties(AstProperties* ast_properties) {
2603 ast_properties_ = *ast_properties;
2605 const ZoneFeedbackVectorSpec* feedback_vector_spec() const {
2606 return ast_properties_.get_spec();
2608 bool dont_optimize() { return dont_optimize_reason_ != kNoReason; }
2609 BailoutReason dont_optimize_reason() { return dont_optimize_reason_; }
2610 void set_dont_optimize_reason(BailoutReason reason) {
2611 dont_optimize_reason_ = reason;
2615 FunctionLiteral(Zone* zone, const AstRawString* name,
2616 AstValueFactory* ast_value_factory, Scope* scope,
2617 ZoneList<Statement*>* body, int materialized_literal_count,
2618 int expected_property_count, int handler_count,
2619 int parameter_count, FunctionType function_type,
2620 ParameterFlag has_duplicate_parameters,
2621 IsFunctionFlag is_function,
2622 IsParenthesizedFlag is_parenthesized, FunctionKind kind,
2624 : Expression(zone, position),
2628 raw_inferred_name_(ast_value_factory->empty_string()),
2629 ast_properties_(zone),
2630 dont_optimize_reason_(kNoReason),
2631 materialized_literal_count_(materialized_literal_count),
2632 expected_property_count_(expected_property_count),
2633 handler_count_(handler_count),
2634 parameter_count_(parameter_count),
2635 function_token_position_(RelocInfo::kNoPosition) {
2636 bitfield_ = IsExpression::encode(function_type != DECLARATION) |
2637 IsAnonymous::encode(function_type == ANONYMOUS_EXPRESSION) |
2638 Pretenure::encode(false) |
2639 HasDuplicateParameters::encode(has_duplicate_parameters) |
2640 IsFunction::encode(is_function) |
2641 IsParenthesized::encode(is_parenthesized) |
2642 FunctionKindBits::encode(kind);
2643 DCHECK(IsValidFunctionKind(kind));
2647 const AstRawString* raw_name_;
2648 Handle<String> name_;
2649 Handle<SharedFunctionInfo> shared_info_;
2651 ZoneList<Statement*>* body_;
2652 const AstString* raw_inferred_name_;
2653 Handle<String> inferred_name_;
2654 AstProperties ast_properties_;
2655 BailoutReason dont_optimize_reason_;
2657 int materialized_literal_count_;
2658 int expected_property_count_;
2660 int parameter_count_;
2661 int function_token_position_;
2664 class IsExpression : public BitField<bool, 0, 1> {};
2665 class IsAnonymous : public BitField<bool, 1, 1> {};
2666 class Pretenure : public BitField<bool, 2, 1> {};
2667 class HasDuplicateParameters : public BitField<ParameterFlag, 3, 1> {};
2668 class IsFunction : public BitField<IsFunctionFlag, 4, 1> {};
2669 class IsParenthesized : public BitField<IsParenthesizedFlag, 5, 1> {};
2670 class FunctionKindBits : public BitField<FunctionKind, 6, 8> {};
2674 class ClassLiteral final : public Expression {
2676 typedef ObjectLiteralProperty Property;
2678 DECLARE_NODE_TYPE(ClassLiteral)
2680 Handle<String> name() const { return raw_name_->string(); }
2681 const AstRawString* raw_name() const { return raw_name_; }
2682 Scope* scope() const { return scope_; }
2683 VariableProxy* class_variable_proxy() const { return class_variable_proxy_; }
2684 Expression* extends() const { return extends_; }
2685 FunctionLiteral* constructor() const { return constructor_; }
2686 ZoneList<Property*>* properties() const { return properties_; }
2687 int start_position() const { return position(); }
2688 int end_position() const { return end_position_; }
2690 BailoutId EntryId() const { return BailoutId(local_id(0)); }
2691 BailoutId DeclsId() const { return BailoutId(local_id(1)); }
2692 BailoutId ExitId() { return BailoutId(local_id(2)); }
2694 // Return an AST id for a property that is used in simulate instructions.
2695 BailoutId GetIdForProperty(int i) { return BailoutId(local_id(i + 3)); }
2697 // Unlike other AST nodes, this number of bailout IDs allocated for an
2698 // ClassLiteral can vary, so num_ids() is not a static method.
2699 int num_ids() const { return parent_num_ids() + 3 + properties()->length(); }
2702 ClassLiteral(Zone* zone, const AstRawString* name, Scope* scope,
2703 VariableProxy* class_variable_proxy, Expression* extends,
2704 FunctionLiteral* constructor, ZoneList<Property*>* properties,
2705 int start_position, int end_position)
2706 : Expression(zone, start_position),
2709 class_variable_proxy_(class_variable_proxy),
2711 constructor_(constructor),
2712 properties_(properties),
2713 end_position_(end_position) {}
2714 static int parent_num_ids() { return Expression::num_ids(); }
2717 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
2719 const AstRawString* raw_name_;
2721 VariableProxy* class_variable_proxy_;
2722 Expression* extends_;
2723 FunctionLiteral* constructor_;
2724 ZoneList<Property*>* properties_;
2729 class NativeFunctionLiteral final : public Expression {
2731 DECLARE_NODE_TYPE(NativeFunctionLiteral)
2733 Handle<String> name() const { return name_->string(); }
2734 v8::Extension* extension() const { return extension_; }
2737 NativeFunctionLiteral(Zone* zone, const AstRawString* name,
2738 v8::Extension* extension, int pos)
2739 : Expression(zone, pos), name_(name), extension_(extension) {}
2742 const AstRawString* name_;
2743 v8::Extension* extension_;
2747 class ThisFunction final : public Expression {
2749 DECLARE_NODE_TYPE(ThisFunction)
2752 ThisFunction(Zone* zone, int pos) : Expression(zone, pos) {}
2756 class SuperReference final : public Expression {
2758 DECLARE_NODE_TYPE(SuperReference)
2760 VariableProxy* this_var() const { return this_var_; }
2762 static int num_ids() { return parent_num_ids() + 1; }
2763 TypeFeedbackId HomeObjectFeedbackId() { return TypeFeedbackId(local_id(0)); }
2765 // Type feedback information.
2766 virtual FeedbackVectorRequirements ComputeFeedbackRequirements(
2767 Isolate* isolate, const ICSlotCache* cache) override {
2768 return FeedbackVectorRequirements(0, FLAG_vector_ics ? 1 : 0);
2770 void SetFirstFeedbackICSlot(FeedbackVectorICSlot slot,
2771 ICSlotCache* cache) override {
2772 homeobject_feedback_slot_ = slot;
2774 Code::Kind FeedbackICSlotKind(int index) override { return Code::LOAD_IC; }
2776 FeedbackVectorICSlot HomeObjectFeedbackSlot() {
2777 DCHECK(!FLAG_vector_ics || !homeobject_feedback_slot_.IsInvalid());
2778 return homeobject_feedback_slot_;
2782 SuperReference(Zone* zone, VariableProxy* this_var, int pos)
2783 : Expression(zone, pos),
2784 this_var_(this_var),
2785 homeobject_feedback_slot_(FeedbackVectorICSlot::Invalid()) {
2786 DCHECK(this_var->is_this());
2788 static int parent_num_ids() { return Expression::num_ids(); }
2791 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
2793 VariableProxy* this_var_;
2794 FeedbackVectorICSlot homeobject_feedback_slot_;
2798 #undef DECLARE_NODE_TYPE
2801 // ----------------------------------------------------------------------------
2802 // Regular expressions
2805 class RegExpVisitor BASE_EMBEDDED {
2807 virtual ~RegExpVisitor() { }
2808 #define MAKE_CASE(Name) \
2809 virtual void* Visit##Name(RegExp##Name*, void* data) = 0;
2810 FOR_EACH_REG_EXP_TREE_TYPE(MAKE_CASE)
2815 class RegExpTree : public ZoneObject {
2817 static const int kInfinity = kMaxInt;
2818 virtual ~RegExpTree() {}
2819 virtual void* Accept(RegExpVisitor* visitor, void* data) = 0;
2820 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
2821 RegExpNode* on_success) = 0;
2822 virtual bool IsTextElement() { return false; }
2823 virtual bool IsAnchoredAtStart() { return false; }
2824 virtual bool IsAnchoredAtEnd() { return false; }
2825 virtual int min_match() = 0;
2826 virtual int max_match() = 0;
2827 // Returns the interval of registers used for captures within this
2829 virtual Interval CaptureRegisters() { return Interval::Empty(); }
2830 virtual void AppendToText(RegExpText* text, Zone* zone);
2831 std::ostream& Print(std::ostream& os, Zone* zone); // NOLINT
2832 #define MAKE_ASTYPE(Name) \
2833 virtual RegExp##Name* As##Name(); \
2834 virtual bool Is##Name();
2835 FOR_EACH_REG_EXP_TREE_TYPE(MAKE_ASTYPE)
2840 class RegExpDisjunction final : public RegExpTree {
2842 explicit RegExpDisjunction(ZoneList<RegExpTree*>* alternatives);
2843 void* Accept(RegExpVisitor* visitor, void* data) override;
2844 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
2845 RegExpNode* on_success) override;
2846 RegExpDisjunction* AsDisjunction() override;
2847 Interval CaptureRegisters() override;
2848 bool IsDisjunction() override;
2849 bool IsAnchoredAtStart() override;
2850 bool IsAnchoredAtEnd() override;
2851 int min_match() override { return min_match_; }
2852 int max_match() override { return max_match_; }
2853 ZoneList<RegExpTree*>* alternatives() { return alternatives_; }
2855 ZoneList<RegExpTree*>* alternatives_;
2861 class RegExpAlternative final : public RegExpTree {
2863 explicit RegExpAlternative(ZoneList<RegExpTree*>* nodes);
2864 void* Accept(RegExpVisitor* visitor, void* data) override;
2865 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
2866 RegExpNode* on_success) override;
2867 RegExpAlternative* AsAlternative() override;
2868 Interval CaptureRegisters() override;
2869 bool IsAlternative() override;
2870 bool IsAnchoredAtStart() override;
2871 bool IsAnchoredAtEnd() override;
2872 int min_match() override { return min_match_; }
2873 int max_match() override { return max_match_; }
2874 ZoneList<RegExpTree*>* nodes() { return nodes_; }
2876 ZoneList<RegExpTree*>* nodes_;
2882 class RegExpAssertion final : public RegExpTree {
2884 enum AssertionType {
2892 explicit RegExpAssertion(AssertionType type) : assertion_type_(type) { }
2893 void* Accept(RegExpVisitor* visitor, void* data) override;
2894 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
2895 RegExpNode* on_success) override;
2896 RegExpAssertion* AsAssertion() override;
2897 bool IsAssertion() override;
2898 bool IsAnchoredAtStart() override;
2899 bool IsAnchoredAtEnd() override;
2900 int min_match() override { return 0; }
2901 int max_match() override { return 0; }
2902 AssertionType assertion_type() { return assertion_type_; }
2904 AssertionType assertion_type_;
2908 class CharacterSet final BASE_EMBEDDED {
2910 explicit CharacterSet(uc16 standard_set_type)
2912 standard_set_type_(standard_set_type) {}
2913 explicit CharacterSet(ZoneList<CharacterRange>* ranges)
2915 standard_set_type_(0) {}
2916 ZoneList<CharacterRange>* ranges(Zone* zone);
2917 uc16 standard_set_type() { return standard_set_type_; }
2918 void set_standard_set_type(uc16 special_set_type) {
2919 standard_set_type_ = special_set_type;
2921 bool is_standard() { return standard_set_type_ != 0; }
2922 void Canonicalize();
2924 ZoneList<CharacterRange>* ranges_;
2925 // If non-zero, the value represents a standard set (e.g., all whitespace
2926 // characters) without having to expand the ranges.
2927 uc16 standard_set_type_;
2931 class RegExpCharacterClass final : public RegExpTree {
2933 RegExpCharacterClass(ZoneList<CharacterRange>* ranges, bool is_negated)
2935 is_negated_(is_negated) { }
2936 explicit RegExpCharacterClass(uc16 type)
2938 is_negated_(false) { }
2939 void* Accept(RegExpVisitor* visitor, void* data) override;
2940 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
2941 RegExpNode* on_success) override;
2942 RegExpCharacterClass* AsCharacterClass() override;
2943 bool IsCharacterClass() override;
2944 bool IsTextElement() override { return true; }
2945 int min_match() override { return 1; }
2946 int max_match() override { return 1; }
2947 void AppendToText(RegExpText* text, Zone* zone) override;
2948 CharacterSet character_set() { return set_; }
2949 // TODO(lrn): Remove need for complex version if is_standard that
2950 // recognizes a mangled standard set and just do { return set_.is_special(); }
2951 bool is_standard(Zone* zone);
2952 // Returns a value representing the standard character set if is_standard()
2954 // Currently used values are:
2955 // s : unicode whitespace
2956 // S : unicode non-whitespace
2957 // w : ASCII word character (digit, letter, underscore)
2958 // W : non-ASCII word character
2960 // D : non-ASCII digit
2961 // . : non-unicode non-newline
2962 // * : All characters
2963 uc16 standard_type() { return set_.standard_set_type(); }
2964 ZoneList<CharacterRange>* ranges(Zone* zone) { return set_.ranges(zone); }
2965 bool is_negated() { return is_negated_; }
2973 class RegExpAtom final : public RegExpTree {
2975 explicit RegExpAtom(Vector<const uc16> data) : data_(data) { }
2976 void* Accept(RegExpVisitor* visitor, void* data) override;
2977 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
2978 RegExpNode* on_success) override;
2979 RegExpAtom* AsAtom() override;
2980 bool IsAtom() override;
2981 bool IsTextElement() override { return true; }
2982 int min_match() override { return data_.length(); }
2983 int max_match() override { return data_.length(); }
2984 void AppendToText(RegExpText* text, Zone* zone) override;
2985 Vector<const uc16> data() { return data_; }
2986 int length() { return data_.length(); }
2988 Vector<const uc16> data_;
2992 class RegExpText final : public RegExpTree {
2994 explicit RegExpText(Zone* zone) : elements_(2, zone), length_(0) {}
2995 void* Accept(RegExpVisitor* visitor, void* data) override;
2996 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
2997 RegExpNode* on_success) override;
2998 RegExpText* AsText() override;
2999 bool IsText() override;
3000 bool IsTextElement() override { return true; }
3001 int min_match() override { return length_; }
3002 int max_match() override { return length_; }
3003 void AppendToText(RegExpText* text, Zone* zone) override;
3004 void AddElement(TextElement elm, Zone* zone) {
3005 elements_.Add(elm, zone);
3006 length_ += elm.length();
3008 ZoneList<TextElement>* elements() { return &elements_; }
3010 ZoneList<TextElement> elements_;
3015 class RegExpQuantifier final : public RegExpTree {
3017 enum QuantifierType { GREEDY, NON_GREEDY, POSSESSIVE };
3018 RegExpQuantifier(int min, int max, QuantifierType type, RegExpTree* body)
3022 min_match_(min * body->min_match()),
3023 quantifier_type_(type) {
3024 if (max > 0 && body->max_match() > kInfinity / max) {
3025 max_match_ = kInfinity;
3027 max_match_ = max * body->max_match();
3030 void* Accept(RegExpVisitor* visitor, void* data) override;
3031 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
3032 RegExpNode* on_success) override;
3033 static RegExpNode* ToNode(int min,
3037 RegExpCompiler* compiler,
3038 RegExpNode* on_success,
3039 bool not_at_start = false);
3040 RegExpQuantifier* AsQuantifier() override;
3041 Interval CaptureRegisters() override;
3042 bool IsQuantifier() override;
3043 int min_match() override { return min_match_; }
3044 int max_match() override { return max_match_; }
3045 int min() { return min_; }
3046 int max() { return max_; }
3047 bool is_possessive() { return quantifier_type_ == POSSESSIVE; }
3048 bool is_non_greedy() { return quantifier_type_ == NON_GREEDY; }
3049 bool is_greedy() { return quantifier_type_ == GREEDY; }
3050 RegExpTree* body() { return body_; }
3058 QuantifierType quantifier_type_;
3062 class RegExpCapture final : public RegExpTree {
3064 explicit RegExpCapture(RegExpTree* body, int index)
3065 : body_(body), index_(index) { }
3066 void* Accept(RegExpVisitor* visitor, void* data) override;
3067 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
3068 RegExpNode* on_success) override;
3069 static RegExpNode* ToNode(RegExpTree* body,
3071 RegExpCompiler* compiler,
3072 RegExpNode* on_success);
3073 RegExpCapture* AsCapture() override;
3074 bool IsAnchoredAtStart() override;
3075 bool IsAnchoredAtEnd() override;
3076 Interval CaptureRegisters() override;
3077 bool IsCapture() override;
3078 int min_match() override { return body_->min_match(); }
3079 int max_match() override { return body_->max_match(); }
3080 RegExpTree* body() { return body_; }
3081 int index() { return index_; }
3082 static int StartRegister(int index) { return index * 2; }
3083 static int EndRegister(int index) { return index * 2 + 1; }
3091 class RegExpLookahead final : public RegExpTree {
3093 RegExpLookahead(RegExpTree* body,
3098 is_positive_(is_positive),
3099 capture_count_(capture_count),
3100 capture_from_(capture_from) { }
3102 void* Accept(RegExpVisitor* visitor, void* data) override;
3103 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
3104 RegExpNode* on_success) override;
3105 RegExpLookahead* AsLookahead() override;
3106 Interval CaptureRegisters() override;
3107 bool IsLookahead() override;
3108 bool IsAnchoredAtStart() override;
3109 int min_match() override { return 0; }
3110 int max_match() override { return 0; }
3111 RegExpTree* body() { return body_; }
3112 bool is_positive() { return is_positive_; }
3113 int capture_count() { return capture_count_; }
3114 int capture_from() { return capture_from_; }
3124 class RegExpBackReference final : public RegExpTree {
3126 explicit RegExpBackReference(RegExpCapture* capture)
3127 : capture_(capture) { }
3128 void* Accept(RegExpVisitor* visitor, void* data) override;
3129 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
3130 RegExpNode* on_success) override;
3131 RegExpBackReference* AsBackReference() override;
3132 bool IsBackReference() override;
3133 int min_match() override { return 0; }
3134 int max_match() override { return capture_->max_match(); }
3135 int index() { return capture_->index(); }
3136 RegExpCapture* capture() { return capture_; }
3138 RegExpCapture* capture_;
3142 class RegExpEmpty final : public RegExpTree {
3145 void* Accept(RegExpVisitor* visitor, void* data) override;
3146 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
3147 RegExpNode* on_success) override;
3148 RegExpEmpty* AsEmpty() override;
3149 bool IsEmpty() override;
3150 int min_match() override { return 0; }
3151 int max_match() override { return 0; }
3155 // ----------------------------------------------------------------------------
3157 // - leaf node visitors are abstract.
3159 class AstVisitor BASE_EMBEDDED {
3162 virtual ~AstVisitor() {}
3164 // Stack overflow check and dynamic dispatch.
3165 virtual void Visit(AstNode* node) = 0;
3167 // Iteration left-to-right.
3168 virtual void VisitDeclarations(ZoneList<Declaration*>* declarations);
3169 virtual void VisitStatements(ZoneList<Statement*>* statements);
3170 virtual void VisitExpressions(ZoneList<Expression*>* expressions);
3172 // Individual AST nodes.
3173 #define DEF_VISIT(type) \
3174 virtual void Visit##type(type* node) = 0;
3175 AST_NODE_LIST(DEF_VISIT)
3180 #define DEFINE_AST_VISITOR_SUBCLASS_MEMBERS() \
3182 void Visit(AstNode* node) final { \
3183 if (!CheckStackOverflow()) node->Accept(this); \
3186 void SetStackOverflow() { stack_overflow_ = true; } \
3187 void ClearStackOverflow() { stack_overflow_ = false; } \
3188 bool HasStackOverflow() const { return stack_overflow_; } \
3190 bool CheckStackOverflow() { \
3191 if (stack_overflow_) return true; \
3192 StackLimitCheck check(isolate_); \
3193 if (!check.HasOverflowed()) return false; \
3194 stack_overflow_ = true; \
3199 void InitializeAstVisitor(Isolate* isolate, Zone* zone) { \
3200 isolate_ = isolate; \
3202 stack_overflow_ = false; \
3204 Zone* zone() { return zone_; } \
3205 Isolate* isolate() { return isolate_; } \
3207 Isolate* isolate_; \
3209 bool stack_overflow_
3212 // ----------------------------------------------------------------------------
3215 class AstNodeFactory final BASE_EMBEDDED {
3217 explicit AstNodeFactory(AstValueFactory* ast_value_factory)
3218 : zone_(ast_value_factory->zone()),
3219 ast_value_factory_(ast_value_factory) {}
3221 VariableDeclaration* NewVariableDeclaration(
3222 VariableProxy* proxy, VariableMode mode, Scope* scope, int pos,
3223 bool is_class_declaration = false) {
3224 return new (zone_) VariableDeclaration(zone_, proxy, mode, scope, pos,
3225 is_class_declaration);
3228 FunctionDeclaration* NewFunctionDeclaration(VariableProxy* proxy,
3230 FunctionLiteral* fun,
3233 return new (zone_) FunctionDeclaration(zone_, proxy, mode, fun, scope, pos);
3236 ModuleDeclaration* NewModuleDeclaration(VariableProxy* proxy,
3240 return new (zone_) ModuleDeclaration(zone_, proxy, module, scope, pos);
3243 ImportDeclaration* NewImportDeclaration(VariableProxy* proxy,
3244 const AstRawString* import_name,
3245 const AstRawString* module_specifier,
3246 Scope* scope, int pos) {
3247 return new (zone_) ImportDeclaration(zone_, proxy, import_name,
3248 module_specifier, scope, pos);
3251 ExportDeclaration* NewExportDeclaration(VariableProxy* proxy,
3254 return new (zone_) ExportDeclaration(zone_, proxy, scope, pos);
3257 ModuleLiteral* NewModuleLiteral(Block* body, ModuleDescriptor* descriptor,
3259 return new (zone_) ModuleLiteral(zone_, body, descriptor, pos);
3262 ModulePath* NewModulePath(Module* origin, const AstRawString* name, int pos) {
3263 return new (zone_) ModulePath(zone_, origin, name, pos);
3266 ModuleUrl* NewModuleUrl(Handle<String> url, int pos) {
3267 return new (zone_) ModuleUrl(zone_, url, pos);
3270 Block* NewBlock(ZoneList<const AstRawString*>* labels,
3272 bool is_initializer_block,
3275 Block(zone_, labels, capacity, is_initializer_block, pos);
3278 #define STATEMENT_WITH_LABELS(NodeType) \
3279 NodeType* New##NodeType(ZoneList<const AstRawString*>* labels, int pos) { \
3280 return new (zone_) NodeType(zone_, labels, pos); \
3282 STATEMENT_WITH_LABELS(DoWhileStatement)
3283 STATEMENT_WITH_LABELS(WhileStatement)
3284 STATEMENT_WITH_LABELS(ForStatement)
3285 STATEMENT_WITH_LABELS(SwitchStatement)
3286 #undef STATEMENT_WITH_LABELS
3288 ForEachStatement* NewForEachStatement(ForEachStatement::VisitMode visit_mode,
3289 ZoneList<const AstRawString*>* labels,
3291 switch (visit_mode) {
3292 case ForEachStatement::ENUMERATE: {
3293 return new (zone_) ForInStatement(zone_, labels, pos);
3295 case ForEachStatement::ITERATE: {
3296 return new (zone_) ForOfStatement(zone_, labels, pos);
3303 ModuleStatement* NewModuleStatement(Block* body, int pos) {
3304 return new (zone_) ModuleStatement(zone_, body, pos);
3307 ExpressionStatement* NewExpressionStatement(Expression* expression, int pos) {
3308 return new (zone_) ExpressionStatement(zone_, expression, pos);
3311 ContinueStatement* NewContinueStatement(IterationStatement* target, int pos) {
3312 return new (zone_) ContinueStatement(zone_, target, pos);
3315 BreakStatement* NewBreakStatement(BreakableStatement* target, int pos) {
3316 return new (zone_) BreakStatement(zone_, target, pos);
3319 ReturnStatement* NewReturnStatement(Expression* expression, int pos) {
3320 return new (zone_) ReturnStatement(zone_, expression, pos);
3323 WithStatement* NewWithStatement(Scope* scope,
3324 Expression* expression,
3325 Statement* statement,
3327 return new (zone_) WithStatement(zone_, scope, expression, statement, pos);
3330 IfStatement* NewIfStatement(Expression* condition,
3331 Statement* then_statement,
3332 Statement* else_statement,
3335 IfStatement(zone_, condition, then_statement, else_statement, pos);
3338 TryCatchStatement* NewTryCatchStatement(int index,
3344 return new (zone_) TryCatchStatement(zone_, index, try_block, scope,
3345 variable, catch_block, pos);
3348 TryFinallyStatement* NewTryFinallyStatement(int index,
3350 Block* finally_block,
3353 TryFinallyStatement(zone_, index, try_block, finally_block, pos);
3356 DebuggerStatement* NewDebuggerStatement(int pos) {
3357 return new (zone_) DebuggerStatement(zone_, pos);
3360 EmptyStatement* NewEmptyStatement(int pos) {
3361 return new(zone_) EmptyStatement(zone_, pos);
3364 CaseClause* NewCaseClause(
3365 Expression* label, ZoneList<Statement*>* statements, int pos) {
3366 return new (zone_) CaseClause(zone_, label, statements, pos);
3369 Literal* NewStringLiteral(const AstRawString* string, int pos) {
3371 Literal(zone_, ast_value_factory_->NewString(string), pos);
3374 // A JavaScript symbol (ECMA-262 edition 6).
3375 Literal* NewSymbolLiteral(const char* name, int pos) {
3376 return new (zone_) Literal(zone_, ast_value_factory_->NewSymbol(name), pos);
3379 Literal* NewNumberLiteral(double number, int pos) {
3381 Literal(zone_, ast_value_factory_->NewNumber(number), pos);
3384 Literal* NewSmiLiteral(int number, int pos) {
3385 return new (zone_) Literal(zone_, ast_value_factory_->NewSmi(number), pos);
3388 Literal* NewBooleanLiteral(bool b, int pos) {
3389 return new (zone_) Literal(zone_, ast_value_factory_->NewBoolean(b), pos);
3392 Literal* NewNullLiteral(int pos) {
3393 return new (zone_) Literal(zone_, ast_value_factory_->NewNull(), pos);
3396 Literal* NewUndefinedLiteral(int pos) {
3397 return new (zone_) Literal(zone_, ast_value_factory_->NewUndefined(), pos);
3400 Literal* NewTheHoleLiteral(int pos) {
3401 return new (zone_) Literal(zone_, ast_value_factory_->NewTheHole(), pos);
3404 ObjectLiteral* NewObjectLiteral(
3405 ZoneList<ObjectLiteral::Property*>* properties,
3407 int boilerplate_properties,
3410 return new (zone_) ObjectLiteral(zone_, properties, literal_index,
3411 boilerplate_properties, has_function, pos);
3414 ObjectLiteral::Property* NewObjectLiteralProperty(
3415 Expression* key, Expression* value, ObjectLiteralProperty::Kind kind,
3416 bool is_static, bool is_computed_name) {
3418 ObjectLiteral::Property(key, value, kind, is_static, is_computed_name);
3421 ObjectLiteral::Property* NewObjectLiteralProperty(Expression* key,
3424 bool is_computed_name) {
3425 return new (zone_) ObjectLiteral::Property(ast_value_factory_, key, value,
3426 is_static, is_computed_name);
3429 RegExpLiteral* NewRegExpLiteral(const AstRawString* pattern,
3430 const AstRawString* flags,
3433 return new (zone_) RegExpLiteral(zone_, pattern, flags, literal_index, pos);
3436 ArrayLiteral* NewArrayLiteral(ZoneList<Expression*>* values,
3439 return new (zone_) ArrayLiteral(zone_, values, literal_index, pos);
3442 VariableProxy* NewVariableProxy(Variable* var,
3443 int start_position = RelocInfo::kNoPosition,
3444 int end_position = RelocInfo::kNoPosition) {
3445 return new (zone_) VariableProxy(zone_, var, start_position, end_position);
3448 VariableProxy* NewVariableProxy(const AstRawString* name,
3449 Variable::Kind variable_kind,
3450 int start_position = RelocInfo::kNoPosition,
3451 int end_position = RelocInfo::kNoPosition) {
3453 VariableProxy(zone_, name, variable_kind, start_position, end_position);
3456 Property* NewProperty(Expression* obj, Expression* key, int pos) {
3457 return new (zone_) Property(zone_, obj, key, pos);
3460 Call* NewCall(Expression* expression,
3461 ZoneList<Expression*>* arguments,
3463 return new (zone_) Call(zone_, expression, arguments, pos);
3466 CallNew* NewCallNew(Expression* expression,
3467 ZoneList<Expression*>* arguments,
3469 return new (zone_) CallNew(zone_, expression, arguments, pos);
3472 CallRuntime* NewCallRuntime(const AstRawString* name,
3473 const Runtime::Function* function,
3474 ZoneList<Expression*>* arguments,
3476 return new (zone_) CallRuntime(zone_, name, function, arguments, pos);
3479 UnaryOperation* NewUnaryOperation(Token::Value op,
3480 Expression* expression,
3482 return new (zone_) UnaryOperation(zone_, op, expression, pos);
3485 BinaryOperation* NewBinaryOperation(Token::Value op,
3489 return new (zone_) BinaryOperation(zone_, op, left, right, pos);
3492 CountOperation* NewCountOperation(Token::Value op,
3496 return new (zone_) CountOperation(zone_, op, is_prefix, expr, pos);
3499 CompareOperation* NewCompareOperation(Token::Value op,
3503 return new (zone_) CompareOperation(zone_, op, left, right, pos);
3506 Spread* NewSpread(Expression* expression, int pos) {
3507 return new (zone_) Spread(zone_, expression, pos);
3510 Conditional* NewConditional(Expression* condition,
3511 Expression* then_expression,
3512 Expression* else_expression,
3514 return new (zone_) Conditional(zone_, condition, then_expression,
3515 else_expression, position);
3518 Assignment* NewAssignment(Token::Value op,
3522 DCHECK(Token::IsAssignmentOp(op));
3523 Assignment* assign = new (zone_) Assignment(zone_, op, target, value, pos);
3524 if (assign->is_compound()) {
3525 DCHECK(Token::IsAssignmentOp(op));
3526 assign->binary_operation_ =
3527 NewBinaryOperation(assign->binary_op(), target, value, pos + 1);
3532 Yield* NewYield(Expression *generator_object,
3533 Expression* expression,
3534 Yield::Kind yield_kind,
3536 if (!expression) expression = NewUndefinedLiteral(pos);
3538 Yield(zone_, generator_object, expression, yield_kind, pos);
3541 Throw* NewThrow(Expression* exception, int pos) {
3542 return new (zone_) Throw(zone_, exception, pos);
3545 FunctionLiteral* NewFunctionLiteral(
3546 const AstRawString* name, AstValueFactory* ast_value_factory,
3547 Scope* scope, ZoneList<Statement*>* body, int materialized_literal_count,
3548 int expected_property_count, int handler_count, int parameter_count,
3549 FunctionLiteral::ParameterFlag has_duplicate_parameters,
3550 FunctionLiteral::FunctionType function_type,
3551 FunctionLiteral::IsFunctionFlag is_function,
3552 FunctionLiteral::IsParenthesizedFlag is_parenthesized, FunctionKind kind,
3554 return new (zone_) FunctionLiteral(
3555 zone_, name, ast_value_factory, scope, body, materialized_literal_count,
3556 expected_property_count, handler_count, parameter_count, function_type,
3557 has_duplicate_parameters, is_function, is_parenthesized, kind,
3561 ClassLiteral* NewClassLiteral(const AstRawString* name, Scope* scope,
3562 VariableProxy* proxy, Expression* extends,
3563 FunctionLiteral* constructor,
3564 ZoneList<ObjectLiteral::Property*>* properties,
3565 int start_position, int end_position) {
3567 ClassLiteral(zone_, name, scope, proxy, extends, constructor,
3568 properties, start_position, end_position);
3571 NativeFunctionLiteral* NewNativeFunctionLiteral(const AstRawString* name,
3572 v8::Extension* extension,
3574 return new (zone_) NativeFunctionLiteral(zone_, name, extension, pos);
3577 ThisFunction* NewThisFunction(int pos) {
3578 return new (zone_) ThisFunction(zone_, pos);
3581 SuperReference* NewSuperReference(VariableProxy* this_var, int pos) {
3582 return new (zone_) SuperReference(zone_, this_var, pos);
3587 AstValueFactory* ast_value_factory_;
3591 } } // namespace v8::internal