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;
572 VariableDeclaration(Zone* zone,
573 VariableProxy* proxy,
577 : Declaration(zone, proxy, mode, scope, pos) {
582 class FunctionDeclaration FINAL : public Declaration {
584 DECLARE_NODE_TYPE(FunctionDeclaration)
586 FunctionLiteral* fun() const { return fun_; }
587 InitializationFlag initialization() const OVERRIDE {
588 return kCreatedInitialized;
590 bool IsInlineable() const OVERRIDE;
593 FunctionDeclaration(Zone* zone,
594 VariableProxy* proxy,
596 FunctionLiteral* fun,
599 : Declaration(zone, proxy, mode, scope, pos),
601 DCHECK(mode == VAR || mode == LET || mode == CONST);
606 FunctionLiteral* fun_;
610 class ModuleDeclaration FINAL : public Declaration {
612 DECLARE_NODE_TYPE(ModuleDeclaration)
614 Module* module() const { return module_; }
615 InitializationFlag initialization() const OVERRIDE {
616 return kCreatedInitialized;
620 ModuleDeclaration(Zone* zone, VariableProxy* proxy, Module* module,
621 Scope* scope, int pos)
622 : Declaration(zone, proxy, CONST, scope, pos), module_(module) {}
629 class ImportDeclaration FINAL : public Declaration {
631 DECLARE_NODE_TYPE(ImportDeclaration)
633 const AstRawString* import_name() const { return import_name_; }
634 const AstRawString* module_specifier() const { return module_specifier_; }
635 void set_module_specifier(const AstRawString* module_specifier) {
636 DCHECK(module_specifier_ == NULL);
637 module_specifier_ = module_specifier;
639 InitializationFlag initialization() const OVERRIDE {
640 return kNeedsInitialization;
644 ImportDeclaration(Zone* zone, VariableProxy* proxy,
645 const AstRawString* import_name,
646 const AstRawString* module_specifier, Scope* scope, int pos)
647 : Declaration(zone, proxy, IMPORT, scope, pos),
648 import_name_(import_name),
649 module_specifier_(module_specifier) {}
652 const AstRawString* import_name_;
653 const AstRawString* module_specifier_;
657 class ExportDeclaration FINAL : public Declaration {
659 DECLARE_NODE_TYPE(ExportDeclaration)
661 InitializationFlag initialization() const OVERRIDE {
662 return kCreatedInitialized;
666 ExportDeclaration(Zone* zone, VariableProxy* proxy, Scope* scope, int pos)
667 : Declaration(zone, proxy, LET, scope, pos) {}
671 class Module : public AstNode {
673 ModuleDescriptor* descriptor() const { return descriptor_; }
674 Block* body() const { return body_; }
677 Module(Zone* zone, int pos)
678 : AstNode(pos), descriptor_(ModuleDescriptor::New(zone)), body_(NULL) {}
679 Module(Zone* zone, ModuleDescriptor* descriptor, int pos, Block* body = NULL)
680 : AstNode(pos), descriptor_(descriptor), body_(body) {}
683 ModuleDescriptor* descriptor_;
688 class ModuleLiteral FINAL : public Module {
690 DECLARE_NODE_TYPE(ModuleLiteral)
693 ModuleLiteral(Zone* zone, Block* body, ModuleDescriptor* descriptor, int pos)
694 : Module(zone, descriptor, pos, body) {}
698 class ModulePath FINAL : public Module {
700 DECLARE_NODE_TYPE(ModulePath)
702 Module* module() const { return module_; }
703 Handle<String> name() const { return name_->string(); }
706 ModulePath(Zone* zone, Module* module, const AstRawString* name, int pos)
707 : Module(zone, pos), module_(module), name_(name) {}
711 const AstRawString* name_;
715 class ModuleUrl FINAL : public Module {
717 DECLARE_NODE_TYPE(ModuleUrl)
719 Handle<String> url() const { return url_; }
722 ModuleUrl(Zone* zone, Handle<String> url, int pos)
723 : Module(zone, pos), url_(url) {
731 class ModuleStatement FINAL : public Statement {
733 DECLARE_NODE_TYPE(ModuleStatement)
735 Block* body() const { return body_; }
738 ModuleStatement(Zone* zone, Block* body, int pos)
739 : Statement(zone, pos), body_(body) {}
746 class IterationStatement : public BreakableStatement {
748 // Type testing & conversion.
749 IterationStatement* AsIterationStatement() FINAL { return this; }
751 Statement* body() const { return body_; }
753 static int num_ids() { return parent_num_ids() + 1; }
754 BailoutId OsrEntryId() const { return BailoutId(local_id(0)); }
755 virtual BailoutId ContinueId() const = 0;
756 virtual BailoutId StackCheckId() const = 0;
759 Label* continue_target() { return &continue_target_; }
762 IterationStatement(Zone* zone, ZoneList<const AstRawString*>* labels, int pos)
763 : BreakableStatement(zone, labels, TARGET_FOR_ANONYMOUS, pos),
765 static int parent_num_ids() { return BreakableStatement::num_ids(); }
766 void Initialize(Statement* body) { body_ = body; }
769 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
772 Label continue_target_;
776 class DoWhileStatement FINAL : public IterationStatement {
778 DECLARE_NODE_TYPE(DoWhileStatement)
780 void Initialize(Expression* cond, Statement* body) {
781 IterationStatement::Initialize(body);
785 Expression* cond() const { return cond_; }
787 static int num_ids() { return parent_num_ids() + 2; }
788 BailoutId ContinueId() const OVERRIDE { return BailoutId(local_id(0)); }
789 BailoutId StackCheckId() const OVERRIDE { return BackEdgeId(); }
790 BailoutId BackEdgeId() const { return BailoutId(local_id(1)); }
793 DoWhileStatement(Zone* zone, ZoneList<const AstRawString*>* labels, int pos)
794 : IterationStatement(zone, labels, pos), cond_(NULL) {}
795 static int parent_num_ids() { return IterationStatement::num_ids(); }
798 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
804 class WhileStatement FINAL : public IterationStatement {
806 DECLARE_NODE_TYPE(WhileStatement)
808 void Initialize(Expression* cond, Statement* body) {
809 IterationStatement::Initialize(body);
813 Expression* cond() const { return cond_; }
815 static int num_ids() { return parent_num_ids() + 1; }
816 BailoutId ContinueId() const OVERRIDE { return EntryId(); }
817 BailoutId StackCheckId() const OVERRIDE { return BodyId(); }
818 BailoutId BodyId() const { return BailoutId(local_id(0)); }
821 WhileStatement(Zone* zone, ZoneList<const AstRawString*>* labels, int pos)
822 : IterationStatement(zone, labels, pos), cond_(NULL) {}
823 static int parent_num_ids() { return IterationStatement::num_ids(); }
826 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
832 class ForStatement FINAL : public IterationStatement {
834 DECLARE_NODE_TYPE(ForStatement)
836 void Initialize(Statement* init,
840 IterationStatement::Initialize(body);
846 Statement* init() const { return init_; }
847 Expression* cond() const { return cond_; }
848 Statement* next() const { return next_; }
850 static int num_ids() { return parent_num_ids() + 2; }
851 BailoutId ContinueId() const OVERRIDE { return BailoutId(local_id(0)); }
852 BailoutId StackCheckId() const OVERRIDE { return BodyId(); }
853 BailoutId BodyId() const { return BailoutId(local_id(1)); }
856 ForStatement(Zone* zone, ZoneList<const AstRawString*>* labels, int pos)
857 : IterationStatement(zone, labels, pos),
861 static int parent_num_ids() { return IterationStatement::num_ids(); }
864 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
872 class ForEachStatement : public IterationStatement {
875 ENUMERATE, // for (each in subject) body;
876 ITERATE // for (each of subject) body;
879 void Initialize(Expression* each, Expression* subject, Statement* body) {
880 IterationStatement::Initialize(body);
885 Expression* each() const { return each_; }
886 Expression* subject() const { return subject_; }
889 ForEachStatement(Zone* zone, ZoneList<const AstRawString*>* labels, int pos)
890 : IterationStatement(zone, labels, pos), each_(NULL), subject_(NULL) {}
894 Expression* subject_;
898 class ForInStatement FINAL : public ForEachStatement {
900 DECLARE_NODE_TYPE(ForInStatement)
902 Expression* enumerable() const {
906 // Type feedback information.
907 virtual FeedbackVectorRequirements ComputeFeedbackRequirements(
908 Isolate* isolate, const ICSlotCache* cache) OVERRIDE {
909 return FeedbackVectorRequirements(1, 0);
911 void SetFirstFeedbackSlot(FeedbackVectorSlot slot) OVERRIDE {
912 for_in_feedback_slot_ = slot;
915 FeedbackVectorSlot ForInFeedbackSlot() {
916 DCHECK(!for_in_feedback_slot_.IsInvalid());
917 return for_in_feedback_slot_;
920 enum ForInType { FAST_FOR_IN, SLOW_FOR_IN };
921 ForInType for_in_type() const { return for_in_type_; }
922 void set_for_in_type(ForInType type) { for_in_type_ = type; }
924 static int num_ids() { return parent_num_ids() + 6; }
925 BailoutId BodyId() const { return BailoutId(local_id(0)); }
926 BailoutId PrepareId() const { return BailoutId(local_id(1)); }
927 BailoutId EnumId() const { return BailoutId(local_id(2)); }
928 BailoutId ToObjectId() const { return BailoutId(local_id(3)); }
929 BailoutId FilterId() const { return BailoutId(local_id(4)); }
930 BailoutId AssignmentId() const { return BailoutId(local_id(5)); }
931 BailoutId ContinueId() const OVERRIDE { return EntryId(); }
932 BailoutId StackCheckId() const OVERRIDE { return BodyId(); }
935 ForInStatement(Zone* zone, ZoneList<const AstRawString*>* labels, int pos)
936 : ForEachStatement(zone, labels, pos),
937 for_in_type_(SLOW_FOR_IN),
938 for_in_feedback_slot_(FeedbackVectorSlot::Invalid()) {}
939 static int parent_num_ids() { return ForEachStatement::num_ids(); }
942 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
944 ForInType for_in_type_;
945 FeedbackVectorSlot for_in_feedback_slot_;
949 class ForOfStatement FINAL : public ForEachStatement {
951 DECLARE_NODE_TYPE(ForOfStatement)
953 void Initialize(Expression* each,
956 Expression* assign_iterator,
957 Expression* next_result,
958 Expression* result_done,
959 Expression* assign_each) {
960 ForEachStatement::Initialize(each, subject, body);
961 assign_iterator_ = assign_iterator;
962 next_result_ = next_result;
963 result_done_ = result_done;
964 assign_each_ = assign_each;
967 Expression* iterable() const {
971 // iterator = subject[Symbol.iterator]()
972 Expression* assign_iterator() const {
973 return assign_iterator_;
976 // result = iterator.next() // with type check
977 Expression* next_result() const {
982 Expression* result_done() const {
986 // each = result.value
987 Expression* assign_each() const {
991 BailoutId ContinueId() const OVERRIDE { return EntryId(); }
992 BailoutId StackCheckId() const OVERRIDE { return BackEdgeId(); }
994 static int num_ids() { return parent_num_ids() + 1; }
995 BailoutId BackEdgeId() const { return BailoutId(local_id(0)); }
998 ForOfStatement(Zone* zone, ZoneList<const AstRawString*>* labels, int pos)
999 : ForEachStatement(zone, labels, pos),
1000 assign_iterator_(NULL),
1003 assign_each_(NULL) {}
1004 static int parent_num_ids() { return ForEachStatement::num_ids(); }
1007 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
1009 Expression* assign_iterator_;
1010 Expression* next_result_;
1011 Expression* result_done_;
1012 Expression* assign_each_;
1016 class ExpressionStatement FINAL : public Statement {
1018 DECLARE_NODE_TYPE(ExpressionStatement)
1020 void set_expression(Expression* e) { expression_ = e; }
1021 Expression* expression() const { return expression_; }
1022 bool IsJump() const OVERRIDE { return expression_->IsThrow(); }
1025 ExpressionStatement(Zone* zone, Expression* expression, int pos)
1026 : Statement(zone, pos), expression_(expression) { }
1029 Expression* expression_;
1033 class JumpStatement : public Statement {
1035 bool IsJump() const FINAL { return true; }
1038 explicit JumpStatement(Zone* zone, int pos) : Statement(zone, pos) {}
1042 class ContinueStatement FINAL : public JumpStatement {
1044 DECLARE_NODE_TYPE(ContinueStatement)
1046 IterationStatement* target() const { return target_; }
1049 explicit ContinueStatement(Zone* zone, IterationStatement* target, int pos)
1050 : JumpStatement(zone, pos), target_(target) { }
1053 IterationStatement* target_;
1057 class BreakStatement FINAL : public JumpStatement {
1059 DECLARE_NODE_TYPE(BreakStatement)
1061 BreakableStatement* target() const { return target_; }
1064 explicit BreakStatement(Zone* zone, BreakableStatement* target, int pos)
1065 : JumpStatement(zone, pos), target_(target) { }
1068 BreakableStatement* target_;
1072 class ReturnStatement FINAL : public JumpStatement {
1074 DECLARE_NODE_TYPE(ReturnStatement)
1076 Expression* expression() const { return expression_; }
1079 explicit ReturnStatement(Zone* zone, Expression* expression, int pos)
1080 : JumpStatement(zone, pos), expression_(expression) { }
1083 Expression* expression_;
1087 class WithStatement FINAL : public Statement {
1089 DECLARE_NODE_TYPE(WithStatement)
1091 Scope* scope() { return scope_; }
1092 Expression* expression() const { return expression_; }
1093 Statement* statement() const { return statement_; }
1095 void set_base_id(int id) { base_id_ = id; }
1096 static int num_ids() { return parent_num_ids() + 1; }
1097 BailoutId EntryId() const { return BailoutId(local_id(0)); }
1100 WithStatement(Zone* zone, Scope* scope, Expression* expression,
1101 Statement* statement, int pos)
1102 : Statement(zone, pos),
1104 expression_(expression),
1105 statement_(statement),
1106 base_id_(BailoutId::None().ToInt()) {}
1107 static int parent_num_ids() { return 0; }
1109 int base_id() const {
1110 DCHECK(!BailoutId(base_id_).IsNone());
1115 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
1118 Expression* expression_;
1119 Statement* statement_;
1124 class CaseClause FINAL : public Expression {
1126 DECLARE_NODE_TYPE(CaseClause)
1128 bool is_default() const { return label_ == NULL; }
1129 Expression* label() const {
1130 CHECK(!is_default());
1133 Label* body_target() { return &body_target_; }
1134 ZoneList<Statement*>* statements() const { return statements_; }
1136 static int num_ids() { return parent_num_ids() + 2; }
1137 BailoutId EntryId() const { return BailoutId(local_id(0)); }
1138 TypeFeedbackId CompareId() { return TypeFeedbackId(local_id(1)); }
1140 Type* compare_type() { return compare_type_; }
1141 void set_compare_type(Type* type) { compare_type_ = type; }
1144 static int parent_num_ids() { return Expression::num_ids(); }
1147 CaseClause(Zone* zone, Expression* label, ZoneList<Statement*>* statements,
1149 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
1153 ZoneList<Statement*>* statements_;
1154 Type* compare_type_;
1158 class SwitchStatement FINAL : public BreakableStatement {
1160 DECLARE_NODE_TYPE(SwitchStatement)
1162 void Initialize(Expression* tag, ZoneList<CaseClause*>* cases) {
1167 Expression* tag() const { return tag_; }
1168 ZoneList<CaseClause*>* cases() const { return cases_; }
1171 SwitchStatement(Zone* zone, ZoneList<const AstRawString*>* labels, int pos)
1172 : BreakableStatement(zone, labels, TARGET_FOR_ANONYMOUS, pos),
1178 ZoneList<CaseClause*>* cases_;
1182 // If-statements always have non-null references to their then- and
1183 // else-parts. When parsing if-statements with no explicit else-part,
1184 // the parser implicitly creates an empty statement. Use the
1185 // HasThenStatement() and HasElseStatement() functions to check if a
1186 // given if-statement has a then- or an else-part containing code.
1187 class IfStatement FINAL : public Statement {
1189 DECLARE_NODE_TYPE(IfStatement)
1191 bool HasThenStatement() const { return !then_statement()->IsEmpty(); }
1192 bool HasElseStatement() const { return !else_statement()->IsEmpty(); }
1194 Expression* condition() const { return condition_; }
1195 Statement* then_statement() const { return then_statement_; }
1196 Statement* else_statement() const { return else_statement_; }
1198 bool IsJump() const OVERRIDE {
1199 return HasThenStatement() && then_statement()->IsJump()
1200 && HasElseStatement() && else_statement()->IsJump();
1203 void set_base_id(int id) { base_id_ = id; }
1204 static int num_ids() { return parent_num_ids() + 3; }
1205 BailoutId IfId() const { return BailoutId(local_id(0)); }
1206 BailoutId ThenId() const { return BailoutId(local_id(1)); }
1207 BailoutId ElseId() const { return BailoutId(local_id(2)); }
1210 IfStatement(Zone* zone, Expression* condition, Statement* then_statement,
1211 Statement* else_statement, int pos)
1212 : Statement(zone, pos),
1213 condition_(condition),
1214 then_statement_(then_statement),
1215 else_statement_(else_statement),
1216 base_id_(BailoutId::None().ToInt()) {}
1217 static int parent_num_ids() { return 0; }
1219 int base_id() const {
1220 DCHECK(!BailoutId(base_id_).IsNone());
1225 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
1227 Expression* condition_;
1228 Statement* then_statement_;
1229 Statement* else_statement_;
1234 class TryStatement : public Statement {
1236 int index() const { return index_; }
1237 Block* try_block() const { return try_block_; }
1240 TryStatement(Zone* zone, int index, Block* try_block, int pos)
1241 : Statement(zone, pos), index_(index), try_block_(try_block) {}
1244 // Unique (per-function) index of this handler. This is not an AST ID.
1251 class TryCatchStatement FINAL : public TryStatement {
1253 DECLARE_NODE_TYPE(TryCatchStatement)
1255 Scope* scope() { return scope_; }
1256 Variable* variable() { return variable_; }
1257 Block* catch_block() const { return catch_block_; }
1260 TryCatchStatement(Zone* zone,
1267 : TryStatement(zone, index, try_block, pos),
1269 variable_(variable),
1270 catch_block_(catch_block) {
1275 Variable* variable_;
1276 Block* catch_block_;
1280 class TryFinallyStatement FINAL : public TryStatement {
1282 DECLARE_NODE_TYPE(TryFinallyStatement)
1284 Block* finally_block() const { return finally_block_; }
1287 TryFinallyStatement(
1288 Zone* zone, int index, Block* try_block, Block* finally_block, int pos)
1289 : TryStatement(zone, index, try_block, pos),
1290 finally_block_(finally_block) { }
1293 Block* finally_block_;
1297 class DebuggerStatement FINAL : public Statement {
1299 DECLARE_NODE_TYPE(DebuggerStatement)
1301 void set_base_id(int id) { base_id_ = id; }
1302 static int num_ids() { return parent_num_ids() + 1; }
1303 BailoutId DebugBreakId() const { return BailoutId(local_id(0)); }
1306 explicit DebuggerStatement(Zone* zone, int pos)
1307 : Statement(zone, pos), base_id_(BailoutId::None().ToInt()) {}
1308 static int parent_num_ids() { return 0; }
1310 int base_id() const {
1311 DCHECK(!BailoutId(base_id_).IsNone());
1316 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
1322 class EmptyStatement FINAL : public Statement {
1324 DECLARE_NODE_TYPE(EmptyStatement)
1327 explicit EmptyStatement(Zone* zone, int pos): Statement(zone, pos) {}
1331 class Literal FINAL : public Expression {
1333 DECLARE_NODE_TYPE(Literal)
1335 bool IsPropertyName() const OVERRIDE { return value_->IsPropertyName(); }
1337 Handle<String> AsPropertyName() {
1338 DCHECK(IsPropertyName());
1339 return Handle<String>::cast(value());
1342 const AstRawString* AsRawPropertyName() {
1343 DCHECK(IsPropertyName());
1344 return value_->AsString();
1347 bool ToBooleanIsTrue() const OVERRIDE { return value()->BooleanValue(); }
1348 bool ToBooleanIsFalse() const OVERRIDE { return !value()->BooleanValue(); }
1350 Handle<Object> value() const { return value_->value(); }
1351 const AstValue* raw_value() const { return value_; }
1353 // Support for using Literal as a HashMap key. NOTE: Currently, this works
1354 // only for string and number literals!
1356 static bool Match(void* literal1, void* literal2);
1358 static int num_ids() { return parent_num_ids() + 1; }
1359 TypeFeedbackId LiteralFeedbackId() const {
1360 return TypeFeedbackId(local_id(0));
1364 Literal(Zone* zone, const AstValue* value, int position)
1365 : Expression(zone, position), value_(value) {}
1366 static int parent_num_ids() { return Expression::num_ids(); }
1369 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
1371 const AstValue* value_;
1375 // Base class for literals that needs space in the corresponding JSFunction.
1376 class MaterializedLiteral : public Expression {
1378 virtual MaterializedLiteral* AsMaterializedLiteral() { return this; }
1380 int literal_index() { return literal_index_; }
1383 // only callable after initialization.
1384 DCHECK(depth_ >= 1);
1389 MaterializedLiteral(Zone* zone, int literal_index, int pos)
1390 : Expression(zone, pos),
1391 literal_index_(literal_index),
1395 // A materialized literal is simple if the values consist of only
1396 // constants and simple object and array literals.
1397 bool is_simple() const { return is_simple_; }
1398 void set_is_simple(bool is_simple) { is_simple_ = is_simple; }
1399 friend class CompileTimeValue;
1401 void set_depth(int depth) {
1406 // Populate the constant properties/elements fixed array.
1407 void BuildConstants(Isolate* isolate);
1408 friend class ArrayLiteral;
1409 friend class ObjectLiteral;
1411 // If the expression is a literal, return the literal value;
1412 // if the expression is a materialized literal and is simple return a
1413 // compile time value as encoded by CompileTimeValue::GetValue().
1414 // Otherwise, return undefined literal as the placeholder
1415 // in the object literal boilerplate.
1416 Handle<Object> GetBoilerplateValue(Expression* expression, Isolate* isolate);
1425 // Property is used for passing information
1426 // about an object literal's properties from the parser
1427 // to the code generator.
1428 class ObjectLiteralProperty FINAL : public ZoneObject {
1431 CONSTANT, // Property with constant value (compile time).
1432 COMPUTED, // Property with computed value (execution time).
1433 MATERIALIZED_LITERAL, // Property value is a materialized literal.
1434 GETTER, SETTER, // Property is an accessor function.
1435 PROTOTYPE // Property is __proto__.
1438 Expression* key() { return key_; }
1439 Expression* value() { return value_; }
1440 Kind kind() { return kind_; }
1442 // Type feedback information.
1443 bool IsMonomorphic() { return !receiver_type_.is_null(); }
1444 Handle<Map> GetReceiverType() { return receiver_type_; }
1446 bool IsCompileTimeValue();
1448 void set_emit_store(bool emit_store);
1451 bool is_static() const { return is_static_; }
1452 bool is_computed_name() const { return is_computed_name_; }
1454 void set_receiver_type(Handle<Map> map) { receiver_type_ = map; }
1457 friend class AstNodeFactory;
1459 ObjectLiteralProperty(Expression* key, Expression* value, Kind kind,
1460 bool is_static, bool is_computed_name);
1461 ObjectLiteralProperty(AstValueFactory* ast_value_factory, Expression* key,
1462 Expression* value, bool is_static,
1463 bool is_computed_name);
1471 bool is_computed_name_;
1472 Handle<Map> receiver_type_;
1476 // An object literal has a boilerplate object that is used
1477 // for minimizing the work when constructing it at runtime.
1478 class ObjectLiteral FINAL : public MaterializedLiteral {
1480 typedef ObjectLiteralProperty Property;
1482 DECLARE_NODE_TYPE(ObjectLiteral)
1484 Handle<FixedArray> constant_properties() const {
1485 return constant_properties_;
1487 int properties_count() const { return constant_properties_->length() / 2; }
1488 ZoneList<Property*>* properties() const { return properties_; }
1489 bool fast_elements() const { return fast_elements_; }
1490 bool may_store_doubles() const { return may_store_doubles_; }
1491 bool has_function() const { return has_function_; }
1492 bool has_elements() const { return has_elements_; }
1494 // Decide if a property should be in the object boilerplate.
1495 static bool IsBoilerplateProperty(Property* property);
1497 // Populate the constant properties fixed array.
1498 void BuildConstantProperties(Isolate* isolate);
1500 // Mark all computed expressions that are bound to a key that
1501 // is shadowed by a later occurrence of the same key. For the
1502 // marked expressions, no store code is emitted.
1503 void CalculateEmitStore(Zone* zone);
1505 // Assemble bitfield of flags for the CreateObjectLiteral helper.
1506 int ComputeFlags(bool disable_mementos = false) const {
1507 int flags = fast_elements() ? kFastElements : kNoFlags;
1508 flags |= has_function() ? kHasFunction : kNoFlags;
1509 if (disable_mementos) {
1510 flags |= kDisableMementos;
1518 kHasFunction = 1 << 1,
1519 kDisableMementos = 1 << 2
1522 struct Accessors: public ZoneObject {
1523 Accessors() : getter(NULL), setter(NULL) {}
1528 BailoutId CreateLiteralId() const { return BailoutId(local_id(0)); }
1530 // Return an AST id for a property that is used in simulate instructions.
1531 BailoutId GetIdForProperty(int i) { return BailoutId(local_id(i + 1)); }
1533 // Unlike other AST nodes, this number of bailout IDs allocated for an
1534 // ObjectLiteral can vary, so num_ids() is not a static method.
1535 int num_ids() const { return parent_num_ids() + 1 + properties()->length(); }
1538 ObjectLiteral(Zone* zone, ZoneList<Property*>* properties, int literal_index,
1539 int boilerplate_properties, bool has_function, int pos)
1540 : MaterializedLiteral(zone, literal_index, pos),
1541 properties_(properties),
1542 boilerplate_properties_(boilerplate_properties),
1543 fast_elements_(false),
1544 has_elements_(false),
1545 may_store_doubles_(false),
1546 has_function_(has_function) {}
1547 static int parent_num_ids() { return MaterializedLiteral::num_ids(); }
1550 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
1551 Handle<FixedArray> constant_properties_;
1552 ZoneList<Property*>* properties_;
1553 int boilerplate_properties_;
1554 bool fast_elements_;
1556 bool may_store_doubles_;
1561 // Node for capturing a regexp literal.
1562 class RegExpLiteral FINAL : public MaterializedLiteral {
1564 DECLARE_NODE_TYPE(RegExpLiteral)
1566 Handle<String> pattern() const { return pattern_->string(); }
1567 Handle<String> flags() const { return flags_->string(); }
1570 RegExpLiteral(Zone* zone, const AstRawString* pattern,
1571 const AstRawString* flags, int literal_index, int pos)
1572 : MaterializedLiteral(zone, literal_index, pos),
1579 const AstRawString* pattern_;
1580 const AstRawString* flags_;
1584 // An array literal has a literals object that is used
1585 // for minimizing the work when constructing it at runtime.
1586 class ArrayLiteral FINAL : public MaterializedLiteral {
1588 DECLARE_NODE_TYPE(ArrayLiteral)
1590 Handle<FixedArray> constant_elements() const { return constant_elements_; }
1591 ElementsKind constant_elements_kind() const {
1592 DCHECK_EQ(2, constant_elements_->length());
1593 return static_cast<ElementsKind>(
1594 Smi::cast(constant_elements_->get(0))->value());
1597 ZoneList<Expression*>* values() const { return values_; }
1599 BailoutId CreateLiteralId() const { return BailoutId(local_id(0)); }
1601 // Return an AST id for an element that is used in simulate instructions.
1602 BailoutId GetIdForElement(int i) { return BailoutId(local_id(i + 1)); }
1604 // Unlike other AST nodes, this number of bailout IDs allocated for an
1605 // ArrayLiteral can vary, so num_ids() is not a static method.
1606 int num_ids() const { return parent_num_ids() + 1 + values()->length(); }
1608 // Populate the constant elements fixed array.
1609 void BuildConstantElements(Isolate* isolate);
1611 // Assemble bitfield of flags for the CreateArrayLiteral helper.
1612 int ComputeFlags(bool disable_mementos = false) const {
1613 int flags = depth() == 1 ? kShallowElements : kNoFlags;
1614 if (disable_mementos) {
1615 flags |= kDisableMementos;
1622 kShallowElements = 1,
1623 kDisableMementos = 1 << 1
1627 ArrayLiteral(Zone* zone, ZoneList<Expression*>* values, int literal_index,
1629 : MaterializedLiteral(zone, literal_index, pos), values_(values) {}
1630 static int parent_num_ids() { return MaterializedLiteral::num_ids(); }
1633 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
1635 Handle<FixedArray> constant_elements_;
1636 ZoneList<Expression*>* values_;
1640 class VariableProxy FINAL : public Expression {
1642 DECLARE_NODE_TYPE(VariableProxy)
1644 bool IsValidReferenceExpression() const OVERRIDE { return !is_this(); }
1646 bool IsArguments() const { return is_resolved() && var()->is_arguments(); }
1648 Handle<String> name() const { return raw_name()->string(); }
1649 const AstRawString* raw_name() const {
1650 return is_resolved() ? var_->raw_name() : raw_name_;
1653 Variable* var() const {
1654 DCHECK(is_resolved());
1657 void set_var(Variable* v) {
1658 DCHECK(!is_resolved());
1663 bool is_this() const { return IsThisField::decode(bit_field_); }
1665 bool is_assigned() const { return IsAssignedField::decode(bit_field_); }
1666 void set_is_assigned() {
1667 bit_field_ = IsAssignedField::update(bit_field_, true);
1670 bool is_resolved() const { return IsResolvedField::decode(bit_field_); }
1671 void set_is_resolved() {
1672 bit_field_ = IsResolvedField::update(bit_field_, true);
1675 int end_position() const { return end_position_; }
1677 // Bind this proxy to the variable var.
1678 void BindTo(Variable* var);
1680 bool UsesVariableFeedbackSlot() const {
1681 return FLAG_vector_ics && (var()->IsUnallocated() || var()->IsLookupSlot());
1684 virtual FeedbackVectorRequirements ComputeFeedbackRequirements(
1685 Isolate* isolate, const ICSlotCache* cache) OVERRIDE;
1687 void SetFirstFeedbackICSlot(FeedbackVectorICSlot slot,
1688 ICSlotCache* cache) OVERRIDE;
1689 Code::Kind FeedbackICSlotKind(int index) OVERRIDE { return Code::LOAD_IC; }
1690 FeedbackVectorICSlot VariableFeedbackSlot() {
1691 DCHECK(!UsesVariableFeedbackSlot() || !variable_feedback_slot_.IsInvalid());
1692 return variable_feedback_slot_;
1696 VariableProxy(Zone* zone, Variable* var, int start_position,
1699 VariableProxy(Zone* zone, const AstRawString* name,
1700 Variable::Kind variable_kind, int start_position,
1703 class IsThisField : public BitField8<bool, 0, 1> {};
1704 class IsAssignedField : public BitField8<bool, 1, 1> {};
1705 class IsResolvedField : public BitField8<bool, 2, 1> {};
1707 // Start with 16-bit (or smaller) field, which should get packed together
1708 // with Expression's trailing 16-bit field.
1710 FeedbackVectorICSlot variable_feedback_slot_;
1712 const AstRawString* raw_name_; // if !is_resolved_
1713 Variable* var_; // if is_resolved_
1715 // Position is stored in the AstNode superclass, but VariableProxy needs to
1716 // know its end position too (for error messages). It cannot be inferred from
1717 // the variable name length because it can contain escapes.
1722 class Property FINAL : public Expression {
1724 DECLARE_NODE_TYPE(Property)
1726 bool IsValidReferenceExpression() const OVERRIDE { return true; }
1728 Expression* obj() const { return obj_; }
1729 Expression* key() const { return key_; }
1731 static int num_ids() { return parent_num_ids() + 2; }
1732 BailoutId LoadId() const { return BailoutId(local_id(0)); }
1733 TypeFeedbackId PropertyFeedbackId() { return TypeFeedbackId(local_id(1)); }
1735 bool IsStringAccess() const {
1736 return IsStringAccessField::decode(bit_field_);
1739 // Type feedback information.
1740 bool IsMonomorphic() OVERRIDE { return receiver_types_.length() == 1; }
1741 SmallMapList* GetReceiverTypes() OVERRIDE { return &receiver_types_; }
1742 KeyedAccessStoreMode GetStoreMode() const OVERRIDE { return STANDARD_STORE; }
1743 IcCheckType GetKeyType() const OVERRIDE {
1744 return KeyTypeField::decode(bit_field_);
1746 bool IsUninitialized() const {
1747 return !is_for_call() && HasNoTypeInformation();
1749 bool HasNoTypeInformation() const {
1750 return GetInlineCacheState() == UNINITIALIZED;
1752 InlineCacheState GetInlineCacheState() const {
1753 return InlineCacheStateField::decode(bit_field_);
1755 void set_is_string_access(bool b) {
1756 bit_field_ = IsStringAccessField::update(bit_field_, b);
1758 void set_key_type(IcCheckType key_type) {
1759 bit_field_ = KeyTypeField::update(bit_field_, key_type);
1761 void set_inline_cache_state(InlineCacheState state) {
1762 bit_field_ = InlineCacheStateField::update(bit_field_, state);
1764 void mark_for_call() {
1765 bit_field_ = IsForCallField::update(bit_field_, true);
1767 bool is_for_call() const { return IsForCallField::decode(bit_field_); }
1769 bool IsSuperAccess() {
1770 return obj()->IsSuperReference();
1773 virtual FeedbackVectorRequirements ComputeFeedbackRequirements(
1774 Isolate* isolate, const ICSlotCache* cache) OVERRIDE {
1775 return FeedbackVectorRequirements(0, FLAG_vector_ics ? 1 : 0);
1777 void SetFirstFeedbackICSlot(FeedbackVectorICSlot slot,
1778 ICSlotCache* cache) OVERRIDE {
1779 property_feedback_slot_ = slot;
1781 Code::Kind FeedbackICSlotKind(int index) OVERRIDE {
1782 return key()->IsPropertyName() ? Code::LOAD_IC : Code::KEYED_LOAD_IC;
1785 FeedbackVectorICSlot PropertyFeedbackSlot() const {
1786 DCHECK(!FLAG_vector_ics || !property_feedback_slot_.IsInvalid());
1787 return property_feedback_slot_;
1791 Property(Zone* zone, Expression* obj, Expression* key, int pos)
1792 : Expression(zone, pos),
1793 bit_field_(IsForCallField::encode(false) |
1794 IsStringAccessField::encode(false) |
1795 InlineCacheStateField::encode(UNINITIALIZED)),
1796 property_feedback_slot_(FeedbackVectorICSlot::Invalid()),
1799 static int parent_num_ids() { return Expression::num_ids(); }
1802 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
1804 class IsForCallField : public BitField8<bool, 0, 1> {};
1805 class IsStringAccessField : public BitField8<bool, 1, 1> {};
1806 class KeyTypeField : public BitField8<IcCheckType, 2, 1> {};
1807 class InlineCacheStateField : public BitField8<InlineCacheState, 3, 4> {};
1809 FeedbackVectorICSlot property_feedback_slot_;
1812 SmallMapList receiver_types_;
1816 class Call FINAL : public Expression {
1818 DECLARE_NODE_TYPE(Call)
1820 Expression* expression() const { return expression_; }
1821 ZoneList<Expression*>* arguments() const { return arguments_; }
1823 // Type feedback information.
1824 virtual FeedbackVectorRequirements ComputeFeedbackRequirements(
1825 Isolate* isolate, const ICSlotCache* cache) OVERRIDE;
1826 void SetFirstFeedbackICSlot(FeedbackVectorICSlot slot,
1827 ICSlotCache* cache) OVERRIDE {
1828 ic_slot_or_slot_ = slot.ToInt();
1830 void SetFirstFeedbackSlot(FeedbackVectorSlot slot) OVERRIDE {
1831 ic_slot_or_slot_ = slot.ToInt();
1833 Code::Kind FeedbackICSlotKind(int index) OVERRIDE { return Code::CALL_IC; }
1835 FeedbackVectorSlot CallFeedbackSlot() const {
1836 DCHECK(ic_slot_or_slot_ != FeedbackVectorSlot::Invalid().ToInt());
1837 return FeedbackVectorSlot(ic_slot_or_slot_);
1840 FeedbackVectorICSlot CallFeedbackICSlot() const {
1841 DCHECK(ic_slot_or_slot_ != FeedbackVectorICSlot::Invalid().ToInt());
1842 return FeedbackVectorICSlot(ic_slot_or_slot_);
1845 SmallMapList* GetReceiverTypes() OVERRIDE {
1846 if (expression()->IsProperty()) {
1847 return expression()->AsProperty()->GetReceiverTypes();
1852 bool IsMonomorphic() OVERRIDE {
1853 if (expression()->IsProperty()) {
1854 return expression()->AsProperty()->IsMonomorphic();
1856 return !target_.is_null();
1859 bool global_call() const {
1860 VariableProxy* proxy = expression_->AsVariableProxy();
1861 return proxy != NULL && proxy->var()->IsUnallocated();
1864 bool known_global_function() const {
1865 return global_call() && !target_.is_null();
1868 Handle<JSFunction> target() { return target_; }
1870 Handle<AllocationSite> allocation_site() { return allocation_site_; }
1872 void SetKnownGlobalTarget(Handle<JSFunction> target) {
1874 set_is_uninitialized(false);
1876 void set_target(Handle<JSFunction> target) { target_ = target; }
1877 void set_allocation_site(Handle<AllocationSite> site) {
1878 allocation_site_ = site;
1881 static int num_ids() { return parent_num_ids() + 2; }
1882 BailoutId ReturnId() const { return BailoutId(local_id(0)); }
1883 BailoutId EvalOrLookupId() const { return BailoutId(local_id(1)); }
1885 bool is_uninitialized() const {
1886 return IsUninitializedField::decode(bit_field_);
1888 void set_is_uninitialized(bool b) {
1889 bit_field_ = IsUninitializedField::update(bit_field_, b);
1901 // Helpers to determine how to handle the call.
1902 CallType GetCallType(Isolate* isolate) const;
1903 bool IsUsingCallFeedbackSlot(Isolate* isolate) const;
1904 bool IsUsingCallFeedbackICSlot(Isolate* isolate) const;
1907 // Used to assert that the FullCodeGenerator records the return site.
1908 bool return_is_recorded_;
1912 Call(Zone* zone, Expression* expression, ZoneList<Expression*>* arguments,
1914 : Expression(zone, pos),
1915 ic_slot_or_slot_(FeedbackVectorICSlot::Invalid().ToInt()),
1916 expression_(expression),
1917 arguments_(arguments),
1918 bit_field_(IsUninitializedField::encode(false)) {
1919 if (expression->IsProperty()) {
1920 expression->AsProperty()->mark_for_call();
1923 static int parent_num_ids() { return Expression::num_ids(); }
1926 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
1928 // We store this as an integer because we don't know if we have a slot or
1929 // an ic slot until scoping time.
1930 int ic_slot_or_slot_;
1931 Expression* expression_;
1932 ZoneList<Expression*>* arguments_;
1933 Handle<JSFunction> target_;
1934 Handle<AllocationSite> allocation_site_;
1935 class IsUninitializedField : public BitField8<bool, 0, 1> {};
1940 class CallNew FINAL : public Expression {
1942 DECLARE_NODE_TYPE(CallNew)
1944 Expression* expression() const { return expression_; }
1945 ZoneList<Expression*>* arguments() const { return arguments_; }
1947 // Type feedback information.
1948 virtual FeedbackVectorRequirements ComputeFeedbackRequirements(
1949 Isolate* isolate, const ICSlotCache* cache) OVERRIDE {
1950 return FeedbackVectorRequirements(FLAG_pretenuring_call_new ? 2 : 1, 0);
1952 void SetFirstFeedbackSlot(FeedbackVectorSlot slot) OVERRIDE {
1953 callnew_feedback_slot_ = slot;
1956 FeedbackVectorSlot CallNewFeedbackSlot() {
1957 DCHECK(!callnew_feedback_slot_.IsInvalid());
1958 return callnew_feedback_slot_;
1960 FeedbackVectorSlot AllocationSiteFeedbackSlot() {
1961 DCHECK(FLAG_pretenuring_call_new);
1962 return CallNewFeedbackSlot().next();
1965 bool IsMonomorphic() OVERRIDE { return is_monomorphic_; }
1966 Handle<JSFunction> target() const { return target_; }
1967 Handle<AllocationSite> allocation_site() const {
1968 return allocation_site_;
1971 static int num_ids() { return parent_num_ids() + 1; }
1972 static int feedback_slots() { return 1; }
1973 BailoutId ReturnId() const { return BailoutId(local_id(0)); }
1975 void set_allocation_site(Handle<AllocationSite> site) {
1976 allocation_site_ = site;
1978 void set_is_monomorphic(bool monomorphic) { is_monomorphic_ = monomorphic; }
1979 void set_target(Handle<JSFunction> target) { target_ = target; }
1980 void SetKnownGlobalTarget(Handle<JSFunction> target) {
1982 is_monomorphic_ = true;
1986 CallNew(Zone* zone, Expression* expression, ZoneList<Expression*>* arguments,
1988 : Expression(zone, pos),
1989 expression_(expression),
1990 arguments_(arguments),
1991 is_monomorphic_(false),
1992 callnew_feedback_slot_(FeedbackVectorSlot::Invalid()) {}
1994 static int parent_num_ids() { return Expression::num_ids(); }
1997 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
1999 Expression* expression_;
2000 ZoneList<Expression*>* arguments_;
2001 bool is_monomorphic_;
2002 Handle<JSFunction> target_;
2003 Handle<AllocationSite> allocation_site_;
2004 FeedbackVectorSlot callnew_feedback_slot_;
2008 // The CallRuntime class does not represent any official JavaScript
2009 // language construct. Instead it is used to call a C or JS function
2010 // with a set of arguments. This is used from the builtins that are
2011 // implemented in JavaScript (see "v8natives.js").
2012 class CallRuntime FINAL : public Expression {
2014 DECLARE_NODE_TYPE(CallRuntime)
2016 Handle<String> name() const { return raw_name_->string(); }
2017 const AstRawString* raw_name() const { return raw_name_; }
2018 const Runtime::Function* function() const { return function_; }
2019 ZoneList<Expression*>* arguments() const { return arguments_; }
2020 bool is_jsruntime() const { return function_ == NULL; }
2022 // Type feedback information.
2023 bool HasCallRuntimeFeedbackSlot() const {
2024 return FLAG_vector_ics && is_jsruntime();
2026 virtual FeedbackVectorRequirements ComputeFeedbackRequirements(
2027 Isolate* isolate, const ICSlotCache* cache) OVERRIDE {
2028 return FeedbackVectorRequirements(0, HasCallRuntimeFeedbackSlot() ? 1 : 0);
2030 void SetFirstFeedbackICSlot(FeedbackVectorICSlot slot,
2031 ICSlotCache* cache) OVERRIDE {
2032 callruntime_feedback_slot_ = slot;
2034 Code::Kind FeedbackICSlotKind(int index) OVERRIDE { return Code::LOAD_IC; }
2036 FeedbackVectorICSlot CallRuntimeFeedbackSlot() {
2037 DCHECK(!HasCallRuntimeFeedbackSlot() ||
2038 !callruntime_feedback_slot_.IsInvalid());
2039 return callruntime_feedback_slot_;
2042 static int num_ids() { return parent_num_ids() + 1; }
2043 TypeFeedbackId CallRuntimeFeedbackId() const {
2044 return TypeFeedbackId(local_id(0));
2048 CallRuntime(Zone* zone, const AstRawString* name,
2049 const Runtime::Function* function,
2050 ZoneList<Expression*>* arguments, int pos)
2051 : Expression(zone, pos),
2053 function_(function),
2054 arguments_(arguments),
2055 callruntime_feedback_slot_(FeedbackVectorICSlot::Invalid()) {}
2056 static int parent_num_ids() { return Expression::num_ids(); }
2059 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
2061 const AstRawString* raw_name_;
2062 const Runtime::Function* function_;
2063 ZoneList<Expression*>* arguments_;
2064 FeedbackVectorICSlot callruntime_feedback_slot_;
2068 class UnaryOperation FINAL : public Expression {
2070 DECLARE_NODE_TYPE(UnaryOperation)
2072 Token::Value op() const { return op_; }
2073 Expression* expression() const { return expression_; }
2075 // For unary not (Token::NOT), the AST ids where true and false will
2076 // actually be materialized, respectively.
2077 static int num_ids() { return parent_num_ids() + 2; }
2078 BailoutId MaterializeTrueId() const { return BailoutId(local_id(0)); }
2079 BailoutId MaterializeFalseId() const { return BailoutId(local_id(1)); }
2081 virtual void RecordToBooleanTypeFeedback(
2082 TypeFeedbackOracle* oracle) OVERRIDE;
2085 UnaryOperation(Zone* zone, Token::Value op, Expression* expression, int pos)
2086 : Expression(zone, pos), op_(op), expression_(expression) {
2087 DCHECK(Token::IsUnaryOp(op));
2089 static int parent_num_ids() { return Expression::num_ids(); }
2092 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
2095 Expression* expression_;
2099 class BinaryOperation FINAL : public Expression {
2101 DECLARE_NODE_TYPE(BinaryOperation)
2103 Token::Value op() const { return static_cast<Token::Value>(op_); }
2104 Expression* left() const { return left_; }
2105 Expression* right() const { return right_; }
2106 Handle<AllocationSite> allocation_site() const { return allocation_site_; }
2107 void set_allocation_site(Handle<AllocationSite> allocation_site) {
2108 allocation_site_ = allocation_site;
2111 // The short-circuit logical operations need an AST ID for their
2112 // right-hand subexpression.
2113 static int num_ids() { return parent_num_ids() + 2; }
2114 BailoutId RightId() const { return BailoutId(local_id(0)); }
2116 TypeFeedbackId BinaryOperationFeedbackId() const {
2117 return TypeFeedbackId(local_id(1));
2119 Maybe<int> fixed_right_arg() const {
2120 return has_fixed_right_arg_ ? Just(fixed_right_arg_value_) : Nothing<int>();
2122 void set_fixed_right_arg(Maybe<int> arg) {
2123 has_fixed_right_arg_ = arg.IsJust();
2124 if (arg.IsJust()) fixed_right_arg_value_ = arg.FromJust();
2127 virtual void RecordToBooleanTypeFeedback(
2128 TypeFeedbackOracle* oracle) OVERRIDE;
2131 BinaryOperation(Zone* zone, Token::Value op, Expression* left,
2132 Expression* right, int pos)
2133 : Expression(zone, pos),
2134 op_(static_cast<byte>(op)),
2135 has_fixed_right_arg_(false),
2136 fixed_right_arg_value_(0),
2139 DCHECK(Token::IsBinaryOp(op));
2141 static int parent_num_ids() { return Expression::num_ids(); }
2144 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
2146 const byte op_; // actually Token::Value
2147 // TODO(rossberg): the fixed arg should probably be represented as a Constant
2148 // type for the RHS. Currenty it's actually a Maybe<int>
2149 bool has_fixed_right_arg_;
2150 int fixed_right_arg_value_;
2153 Handle<AllocationSite> allocation_site_;
2157 class CountOperation FINAL : public Expression {
2159 DECLARE_NODE_TYPE(CountOperation)
2161 bool is_prefix() const { return IsPrefixField::decode(bit_field_); }
2162 bool is_postfix() const { return !is_prefix(); }
2164 Token::Value op() const { return TokenField::decode(bit_field_); }
2165 Token::Value binary_op() {
2166 return (op() == Token::INC) ? Token::ADD : Token::SUB;
2169 Expression* expression() const { return expression_; }
2171 bool IsMonomorphic() OVERRIDE { return receiver_types_.length() == 1; }
2172 SmallMapList* GetReceiverTypes() OVERRIDE { return &receiver_types_; }
2173 IcCheckType GetKeyType() const OVERRIDE {
2174 return KeyTypeField::decode(bit_field_);
2176 KeyedAccessStoreMode GetStoreMode() const OVERRIDE {
2177 return StoreModeField::decode(bit_field_);
2179 Type* type() const { return type_; }
2180 void set_key_type(IcCheckType type) {
2181 bit_field_ = KeyTypeField::update(bit_field_, type);
2183 void set_store_mode(KeyedAccessStoreMode mode) {
2184 bit_field_ = StoreModeField::update(bit_field_, mode);
2186 void set_type(Type* type) { type_ = type; }
2188 static int num_ids() { return parent_num_ids() + 4; }
2189 BailoutId AssignmentId() const { return BailoutId(local_id(0)); }
2190 BailoutId ToNumberId() const { return BailoutId(local_id(1)); }
2191 TypeFeedbackId CountBinOpFeedbackId() const {
2192 return TypeFeedbackId(local_id(2));
2194 TypeFeedbackId CountStoreFeedbackId() const {
2195 return TypeFeedbackId(local_id(3));
2199 CountOperation(Zone* zone, Token::Value op, bool is_prefix, Expression* expr,
2201 : Expression(zone, pos),
2202 bit_field_(IsPrefixField::encode(is_prefix) |
2203 KeyTypeField::encode(ELEMENT) |
2204 StoreModeField::encode(STANDARD_STORE) |
2205 TokenField::encode(op)),
2207 expression_(expr) {}
2208 static int parent_num_ids() { return Expression::num_ids(); }
2211 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
2213 class IsPrefixField : public BitField16<bool, 0, 1> {};
2214 class KeyTypeField : public BitField16<IcCheckType, 1, 1> {};
2215 class StoreModeField : public BitField16<KeyedAccessStoreMode, 2, 4> {};
2216 class TokenField : public BitField16<Token::Value, 6, 8> {};
2218 // Starts with 16-bit field, which should get packed together with
2219 // Expression's trailing 16-bit field.
2220 uint16_t bit_field_;
2222 Expression* expression_;
2223 SmallMapList receiver_types_;
2227 class CompareOperation FINAL : public Expression {
2229 DECLARE_NODE_TYPE(CompareOperation)
2231 Token::Value op() const { return op_; }
2232 Expression* left() const { return left_; }
2233 Expression* right() const { return right_; }
2235 // Type feedback information.
2236 static int num_ids() { return parent_num_ids() + 1; }
2237 TypeFeedbackId CompareOperationFeedbackId() const {
2238 return TypeFeedbackId(local_id(0));
2240 Type* combined_type() const { return combined_type_; }
2241 void set_combined_type(Type* type) { combined_type_ = type; }
2243 // Match special cases.
2244 bool IsLiteralCompareTypeof(Expression** expr, Handle<String>* check);
2245 bool IsLiteralCompareUndefined(Expression** expr, Isolate* isolate);
2246 bool IsLiteralCompareNull(Expression** expr);
2249 CompareOperation(Zone* zone, Token::Value op, Expression* left,
2250 Expression* right, int pos)
2251 : Expression(zone, pos),
2255 combined_type_(Type::None(zone)) {
2256 DCHECK(Token::IsCompareOp(op));
2258 static int parent_num_ids() { return Expression::num_ids(); }
2261 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
2267 Type* combined_type_;
2271 class Spread FINAL : public Expression {
2273 DECLARE_NODE_TYPE(Spread)
2275 Expression* expression() const { return expression_; }
2277 static int num_ids() { return parent_num_ids(); }
2280 Spread(Zone* zone, Expression* expression, int pos)
2281 : Expression(zone, pos), expression_(expression) {}
2282 static int parent_num_ids() { return Expression::num_ids(); }
2285 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
2287 Expression* expression_;
2291 class Conditional FINAL : public Expression {
2293 DECLARE_NODE_TYPE(Conditional)
2295 Expression* condition() const { return condition_; }
2296 Expression* then_expression() const { return then_expression_; }
2297 Expression* else_expression() const { return else_expression_; }
2299 static int num_ids() { return parent_num_ids() + 2; }
2300 BailoutId ThenId() const { return BailoutId(local_id(0)); }
2301 BailoutId ElseId() const { return BailoutId(local_id(1)); }
2304 Conditional(Zone* zone, Expression* condition, Expression* then_expression,
2305 Expression* else_expression, int position)
2306 : Expression(zone, position),
2307 condition_(condition),
2308 then_expression_(then_expression),
2309 else_expression_(else_expression) {}
2310 static int parent_num_ids() { return Expression::num_ids(); }
2313 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
2315 Expression* condition_;
2316 Expression* then_expression_;
2317 Expression* else_expression_;
2321 class Assignment FINAL : public Expression {
2323 DECLARE_NODE_TYPE(Assignment)
2325 Assignment* AsSimpleAssignment() { return !is_compound() ? this : NULL; }
2327 Token::Value binary_op() const;
2329 Token::Value op() const { return TokenField::decode(bit_field_); }
2330 Expression* target() const { return target_; }
2331 Expression* value() const { return value_; }
2332 BinaryOperation* binary_operation() const { return binary_operation_; }
2334 // This check relies on the definition order of token in token.h.
2335 bool is_compound() const { return op() > Token::ASSIGN; }
2337 static int num_ids() { return parent_num_ids() + 2; }
2338 BailoutId AssignmentId() const { return BailoutId(local_id(0)); }
2340 // Type feedback information.
2341 TypeFeedbackId AssignmentFeedbackId() { return TypeFeedbackId(local_id(1)); }
2342 bool IsMonomorphic() OVERRIDE { return receiver_types_.length() == 1; }
2343 bool IsUninitialized() const {
2344 return IsUninitializedField::decode(bit_field_);
2346 bool HasNoTypeInformation() {
2347 return IsUninitializedField::decode(bit_field_);
2349 SmallMapList* GetReceiverTypes() OVERRIDE { return &receiver_types_; }
2350 IcCheckType GetKeyType() const OVERRIDE {
2351 return KeyTypeField::decode(bit_field_);
2353 KeyedAccessStoreMode GetStoreMode() const OVERRIDE {
2354 return StoreModeField::decode(bit_field_);
2356 void set_is_uninitialized(bool b) {
2357 bit_field_ = IsUninitializedField::update(bit_field_, b);
2359 void set_key_type(IcCheckType key_type) {
2360 bit_field_ = KeyTypeField::update(bit_field_, key_type);
2362 void set_store_mode(KeyedAccessStoreMode mode) {
2363 bit_field_ = StoreModeField::update(bit_field_, mode);
2367 Assignment(Zone* zone, Token::Value op, Expression* target, Expression* value,
2369 static int parent_num_ids() { return Expression::num_ids(); }
2372 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
2374 class IsUninitializedField : public BitField16<bool, 0, 1> {};
2375 class KeyTypeField : public BitField16<IcCheckType, 1, 1> {};
2376 class StoreModeField : public BitField16<KeyedAccessStoreMode, 2, 4> {};
2377 class TokenField : public BitField16<Token::Value, 6, 8> {};
2379 // Starts with 16-bit field, which should get packed together with
2380 // Expression's trailing 16-bit field.
2381 uint16_t bit_field_;
2382 Expression* target_;
2384 BinaryOperation* binary_operation_;
2385 SmallMapList receiver_types_;
2389 class Yield FINAL : public Expression {
2391 DECLARE_NODE_TYPE(Yield)
2394 kInitial, // The initial yield that returns the unboxed generator object.
2395 kSuspend, // A normal yield: { value: EXPRESSION, done: false }
2396 kDelegating, // A yield*.
2397 kFinal // A return: { value: EXPRESSION, done: true }
2400 Expression* generator_object() const { return generator_object_; }
2401 Expression* expression() const { return expression_; }
2402 Kind yield_kind() const { return yield_kind_; }
2404 // Delegating yield surrounds the "yield" in a "try/catch". This index
2405 // locates the catch handler in the handler table, and is equivalent to
2406 // TryCatchStatement::index().
2408 DCHECK_EQ(kDelegating, yield_kind());
2411 void set_index(int index) {
2412 DCHECK_EQ(kDelegating, yield_kind());
2416 // Type feedback information.
2417 bool HasFeedbackSlots() const {
2418 return FLAG_vector_ics && (yield_kind() == kDelegating);
2420 virtual FeedbackVectorRequirements ComputeFeedbackRequirements(
2421 Isolate* isolate, const ICSlotCache* cache) OVERRIDE {
2422 return FeedbackVectorRequirements(0, HasFeedbackSlots() ? 3 : 0);
2424 void SetFirstFeedbackICSlot(FeedbackVectorICSlot slot,
2425 ICSlotCache* cache) OVERRIDE {
2426 yield_first_feedback_slot_ = slot;
2428 Code::Kind FeedbackICSlotKind(int index) OVERRIDE {
2429 return index == 0 ? Code::KEYED_LOAD_IC : Code::LOAD_IC;
2432 FeedbackVectorICSlot KeyedLoadFeedbackSlot() {
2433 DCHECK(!HasFeedbackSlots() || !yield_first_feedback_slot_.IsInvalid());
2434 return yield_first_feedback_slot_;
2437 FeedbackVectorICSlot DoneFeedbackSlot() {
2438 return KeyedLoadFeedbackSlot().next();
2441 FeedbackVectorICSlot ValueFeedbackSlot() { return DoneFeedbackSlot().next(); }
2444 Yield(Zone* zone, Expression* generator_object, Expression* expression,
2445 Kind yield_kind, int pos)
2446 : Expression(zone, pos),
2447 generator_object_(generator_object),
2448 expression_(expression),
2449 yield_kind_(yield_kind),
2451 yield_first_feedback_slot_(FeedbackVectorICSlot::Invalid()) {}
2454 Expression* generator_object_;
2455 Expression* expression_;
2458 FeedbackVectorICSlot yield_first_feedback_slot_;
2462 class Throw FINAL : public Expression {
2464 DECLARE_NODE_TYPE(Throw)
2466 Expression* exception() const { return exception_; }
2469 Throw(Zone* zone, Expression* exception, int pos)
2470 : Expression(zone, pos), exception_(exception) {}
2473 Expression* exception_;
2477 class FunctionLiteral FINAL : public Expression {
2480 ANONYMOUS_EXPRESSION,
2485 enum ParameterFlag {
2486 kNoDuplicateParameters = 0,
2487 kHasDuplicateParameters = 1
2490 enum IsFunctionFlag {
2495 enum IsParenthesizedFlag {
2500 enum ArityRestriction {
2506 DECLARE_NODE_TYPE(FunctionLiteral)
2508 Handle<String> name() const { return raw_name_->string(); }
2509 const AstRawString* raw_name() const { return raw_name_; }
2510 Scope* scope() const { return scope_; }
2511 ZoneList<Statement*>* body() const { return body_; }
2512 void set_function_token_position(int pos) { function_token_position_ = pos; }
2513 int function_token_position() const { return function_token_position_; }
2514 int start_position() const;
2515 int end_position() const;
2516 int SourceSize() const { return end_position() - start_position(); }
2517 bool is_expression() const { return IsExpression::decode(bitfield_); }
2518 bool is_anonymous() const { return IsAnonymous::decode(bitfield_); }
2519 LanguageMode language_mode() const;
2520 bool uses_super_property() const;
2522 static bool NeedsHomeObject(Expression* literal) {
2523 return literal != NULL && literal->IsFunctionLiteral() &&
2524 literal->AsFunctionLiteral()->uses_super_property();
2527 int materialized_literal_count() { return materialized_literal_count_; }
2528 int expected_property_count() { return expected_property_count_; }
2529 int handler_count() { return handler_count_; }
2530 int parameter_count() { return parameter_count_; }
2532 bool AllowsLazyCompilation();
2533 bool AllowsLazyCompilationWithoutContext();
2535 void InitializeSharedInfo(Handle<Code> code);
2537 Handle<String> debug_name() const {
2538 if (raw_name_ != NULL && !raw_name_->IsEmpty()) {
2539 return raw_name_->string();
2541 return inferred_name();
2544 Handle<String> inferred_name() const {
2545 if (!inferred_name_.is_null()) {
2546 DCHECK(raw_inferred_name_ == NULL);
2547 return inferred_name_;
2549 if (raw_inferred_name_ != NULL) {
2550 return raw_inferred_name_->string();
2553 return Handle<String>();
2556 // Only one of {set_inferred_name, set_raw_inferred_name} should be called.
2557 void set_inferred_name(Handle<String> inferred_name) {
2558 DCHECK(!inferred_name.is_null());
2559 inferred_name_ = inferred_name;
2560 DCHECK(raw_inferred_name_== NULL || raw_inferred_name_->IsEmpty());
2561 raw_inferred_name_ = NULL;
2564 void set_raw_inferred_name(const AstString* raw_inferred_name) {
2565 DCHECK(raw_inferred_name != NULL);
2566 raw_inferred_name_ = raw_inferred_name;
2567 DCHECK(inferred_name_.is_null());
2568 inferred_name_ = Handle<String>();
2571 // shared_info may be null if it's not cached in full code.
2572 Handle<SharedFunctionInfo> shared_info() { return shared_info_; }
2574 bool pretenure() { return Pretenure::decode(bitfield_); }
2575 void set_pretenure() { bitfield_ |= Pretenure::encode(true); }
2577 bool has_duplicate_parameters() {
2578 return HasDuplicateParameters::decode(bitfield_);
2581 bool is_function() { return IsFunction::decode(bitfield_) == kIsFunction; }
2583 // This is used as a heuristic on when to eagerly compile a function
2584 // literal. We consider the following constructs as hints that the
2585 // function will be called immediately:
2586 // - (function() { ... })();
2587 // - var x = function() { ... }();
2588 bool is_parenthesized() {
2589 return IsParenthesized::decode(bitfield_) == kIsParenthesized;
2591 void set_parenthesized() {
2592 bitfield_ = IsParenthesized::update(bitfield_, kIsParenthesized);
2595 FunctionKind kind() { return FunctionKindBits::decode(bitfield_); }
2597 int ast_node_count() { return ast_properties_.node_count(); }
2598 AstProperties::Flags* flags() { return ast_properties_.flags(); }
2599 void set_ast_properties(AstProperties* ast_properties) {
2600 ast_properties_ = *ast_properties;
2602 const ZoneFeedbackVectorSpec* feedback_vector_spec() const {
2603 return ast_properties_.get_spec();
2605 bool dont_optimize() { return dont_optimize_reason_ != kNoReason; }
2606 BailoutReason dont_optimize_reason() { return dont_optimize_reason_; }
2607 void set_dont_optimize_reason(BailoutReason reason) {
2608 dont_optimize_reason_ = reason;
2612 FunctionLiteral(Zone* zone, const AstRawString* name,
2613 AstValueFactory* ast_value_factory, Scope* scope,
2614 ZoneList<Statement*>* body, int materialized_literal_count,
2615 int expected_property_count, int handler_count,
2616 int parameter_count, FunctionType function_type,
2617 ParameterFlag has_duplicate_parameters,
2618 IsFunctionFlag is_function,
2619 IsParenthesizedFlag is_parenthesized, FunctionKind kind,
2621 : Expression(zone, position),
2625 raw_inferred_name_(ast_value_factory->empty_string()),
2626 ast_properties_(zone),
2627 dont_optimize_reason_(kNoReason),
2628 materialized_literal_count_(materialized_literal_count),
2629 expected_property_count_(expected_property_count),
2630 handler_count_(handler_count),
2631 parameter_count_(parameter_count),
2632 function_token_position_(RelocInfo::kNoPosition) {
2633 bitfield_ = IsExpression::encode(function_type != DECLARATION) |
2634 IsAnonymous::encode(function_type == ANONYMOUS_EXPRESSION) |
2635 Pretenure::encode(false) |
2636 HasDuplicateParameters::encode(has_duplicate_parameters) |
2637 IsFunction::encode(is_function) |
2638 IsParenthesized::encode(is_parenthesized) |
2639 FunctionKindBits::encode(kind);
2640 DCHECK(IsValidFunctionKind(kind));
2644 const AstRawString* raw_name_;
2645 Handle<String> name_;
2646 Handle<SharedFunctionInfo> shared_info_;
2648 ZoneList<Statement*>* body_;
2649 const AstString* raw_inferred_name_;
2650 Handle<String> inferred_name_;
2651 AstProperties ast_properties_;
2652 BailoutReason dont_optimize_reason_;
2654 int materialized_literal_count_;
2655 int expected_property_count_;
2657 int parameter_count_;
2658 int function_token_position_;
2661 class IsExpression : public BitField<bool, 0, 1> {};
2662 class IsAnonymous : public BitField<bool, 1, 1> {};
2663 class Pretenure : public BitField<bool, 2, 1> {};
2664 class HasDuplicateParameters : public BitField<ParameterFlag, 3, 1> {};
2665 class IsFunction : public BitField<IsFunctionFlag, 4, 1> {};
2666 class IsParenthesized : public BitField<IsParenthesizedFlag, 5, 1> {};
2667 class FunctionKindBits : public BitField<FunctionKind, 6, 8> {};
2671 class ClassLiteral FINAL : public Expression {
2673 typedef ObjectLiteralProperty Property;
2675 DECLARE_NODE_TYPE(ClassLiteral)
2677 Handle<String> name() const { return raw_name_->string(); }
2678 const AstRawString* raw_name() const { return raw_name_; }
2679 Scope* scope() const { return scope_; }
2680 VariableProxy* class_variable_proxy() const { return class_variable_proxy_; }
2681 Expression* extends() const { return extends_; }
2682 FunctionLiteral* constructor() const { return constructor_; }
2683 ZoneList<Property*>* properties() const { return properties_; }
2684 int start_position() const { return position(); }
2685 int end_position() const { return end_position_; }
2687 BailoutId EntryId() const { return BailoutId(local_id(0)); }
2688 BailoutId DeclsId() const { return BailoutId(local_id(1)); }
2689 BailoutId ExitId() { return BailoutId(local_id(2)); }
2691 // Return an AST id for a property that is used in simulate instructions.
2692 BailoutId GetIdForProperty(int i) { return BailoutId(local_id(i + 3)); }
2694 // Unlike other AST nodes, this number of bailout IDs allocated for an
2695 // ClassLiteral can vary, so num_ids() is not a static method.
2696 int num_ids() const { return parent_num_ids() + 3 + properties()->length(); }
2699 ClassLiteral(Zone* zone, const AstRawString* name, Scope* scope,
2700 VariableProxy* class_variable_proxy, Expression* extends,
2701 FunctionLiteral* constructor, ZoneList<Property*>* properties,
2702 int start_position, int end_position)
2703 : Expression(zone, start_position),
2706 class_variable_proxy_(class_variable_proxy),
2708 constructor_(constructor),
2709 properties_(properties),
2710 end_position_(end_position) {}
2711 static int parent_num_ids() { return Expression::num_ids(); }
2714 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
2716 const AstRawString* raw_name_;
2718 VariableProxy* class_variable_proxy_;
2719 Expression* extends_;
2720 FunctionLiteral* constructor_;
2721 ZoneList<Property*>* properties_;
2726 class NativeFunctionLiteral FINAL : public Expression {
2728 DECLARE_NODE_TYPE(NativeFunctionLiteral)
2730 Handle<String> name() const { return name_->string(); }
2731 v8::Extension* extension() const { return extension_; }
2734 NativeFunctionLiteral(Zone* zone, const AstRawString* name,
2735 v8::Extension* extension, int pos)
2736 : Expression(zone, pos), name_(name), extension_(extension) {}
2739 const AstRawString* name_;
2740 v8::Extension* extension_;
2744 class ThisFunction FINAL : public Expression {
2746 DECLARE_NODE_TYPE(ThisFunction)
2749 ThisFunction(Zone* zone, int pos) : Expression(zone, pos) {}
2753 class SuperReference FINAL : public Expression {
2755 DECLARE_NODE_TYPE(SuperReference)
2757 VariableProxy* this_var() const { return this_var_; }
2759 static int num_ids() { return parent_num_ids() + 1; }
2760 TypeFeedbackId HomeObjectFeedbackId() { return TypeFeedbackId(local_id(0)); }
2762 // Type feedback information.
2763 virtual FeedbackVectorRequirements ComputeFeedbackRequirements(
2764 Isolate* isolate, const ICSlotCache* cache) OVERRIDE {
2765 return FeedbackVectorRequirements(0, FLAG_vector_ics ? 1 : 0);
2767 void SetFirstFeedbackICSlot(FeedbackVectorICSlot slot,
2768 ICSlotCache* cache) OVERRIDE {
2769 homeobject_feedback_slot_ = slot;
2771 Code::Kind FeedbackICSlotKind(int index) OVERRIDE { return Code::LOAD_IC; }
2773 FeedbackVectorICSlot HomeObjectFeedbackSlot() {
2774 DCHECK(!FLAG_vector_ics || !homeobject_feedback_slot_.IsInvalid());
2775 return homeobject_feedback_slot_;
2779 SuperReference(Zone* zone, VariableProxy* this_var, int pos)
2780 : Expression(zone, pos),
2781 this_var_(this_var),
2782 homeobject_feedback_slot_(FeedbackVectorICSlot::Invalid()) {
2783 DCHECK(this_var->is_this());
2785 static int parent_num_ids() { return Expression::num_ids(); }
2788 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
2790 VariableProxy* this_var_;
2791 FeedbackVectorICSlot homeobject_feedback_slot_;
2795 #undef DECLARE_NODE_TYPE
2798 // ----------------------------------------------------------------------------
2799 // Regular expressions
2802 class RegExpVisitor BASE_EMBEDDED {
2804 virtual ~RegExpVisitor() { }
2805 #define MAKE_CASE(Name) \
2806 virtual void* Visit##Name(RegExp##Name*, void* data) = 0;
2807 FOR_EACH_REG_EXP_TREE_TYPE(MAKE_CASE)
2812 class RegExpTree : public ZoneObject {
2814 static const int kInfinity = kMaxInt;
2815 virtual ~RegExpTree() {}
2816 virtual void* Accept(RegExpVisitor* visitor, void* data) = 0;
2817 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
2818 RegExpNode* on_success) = 0;
2819 virtual bool IsTextElement() { return false; }
2820 virtual bool IsAnchoredAtStart() { return false; }
2821 virtual bool IsAnchoredAtEnd() { return false; }
2822 virtual int min_match() = 0;
2823 virtual int max_match() = 0;
2824 // Returns the interval of registers used for captures within this
2826 virtual Interval CaptureRegisters() { return Interval::Empty(); }
2827 virtual void AppendToText(RegExpText* text, Zone* zone);
2828 std::ostream& Print(std::ostream& os, Zone* zone); // NOLINT
2829 #define MAKE_ASTYPE(Name) \
2830 virtual RegExp##Name* As##Name(); \
2831 virtual bool Is##Name();
2832 FOR_EACH_REG_EXP_TREE_TYPE(MAKE_ASTYPE)
2837 class RegExpDisjunction FINAL : public RegExpTree {
2839 explicit RegExpDisjunction(ZoneList<RegExpTree*>* alternatives);
2840 void* Accept(RegExpVisitor* visitor, void* data) OVERRIDE;
2841 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
2842 RegExpNode* on_success) OVERRIDE;
2843 RegExpDisjunction* AsDisjunction() OVERRIDE;
2844 Interval CaptureRegisters() OVERRIDE;
2845 bool IsDisjunction() OVERRIDE;
2846 bool IsAnchoredAtStart() OVERRIDE;
2847 bool IsAnchoredAtEnd() OVERRIDE;
2848 int min_match() OVERRIDE { return min_match_; }
2849 int max_match() OVERRIDE { return max_match_; }
2850 ZoneList<RegExpTree*>* alternatives() { return alternatives_; }
2852 ZoneList<RegExpTree*>* alternatives_;
2858 class RegExpAlternative FINAL : public RegExpTree {
2860 explicit RegExpAlternative(ZoneList<RegExpTree*>* nodes);
2861 void* Accept(RegExpVisitor* visitor, void* data) OVERRIDE;
2862 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
2863 RegExpNode* on_success) OVERRIDE;
2864 RegExpAlternative* AsAlternative() OVERRIDE;
2865 Interval CaptureRegisters() OVERRIDE;
2866 bool IsAlternative() OVERRIDE;
2867 bool IsAnchoredAtStart() OVERRIDE;
2868 bool IsAnchoredAtEnd() OVERRIDE;
2869 int min_match() OVERRIDE { return min_match_; }
2870 int max_match() OVERRIDE { return max_match_; }
2871 ZoneList<RegExpTree*>* nodes() { return nodes_; }
2873 ZoneList<RegExpTree*>* nodes_;
2879 class RegExpAssertion FINAL : public RegExpTree {
2881 enum AssertionType {
2889 explicit RegExpAssertion(AssertionType type) : assertion_type_(type) { }
2890 void* Accept(RegExpVisitor* visitor, void* data) OVERRIDE;
2891 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
2892 RegExpNode* on_success) OVERRIDE;
2893 RegExpAssertion* AsAssertion() OVERRIDE;
2894 bool IsAssertion() OVERRIDE;
2895 bool IsAnchoredAtStart() OVERRIDE;
2896 bool IsAnchoredAtEnd() OVERRIDE;
2897 int min_match() OVERRIDE { return 0; }
2898 int max_match() OVERRIDE { return 0; }
2899 AssertionType assertion_type() { return assertion_type_; }
2901 AssertionType assertion_type_;
2905 class CharacterSet FINAL BASE_EMBEDDED {
2907 explicit CharacterSet(uc16 standard_set_type)
2909 standard_set_type_(standard_set_type) {}
2910 explicit CharacterSet(ZoneList<CharacterRange>* ranges)
2912 standard_set_type_(0) {}
2913 ZoneList<CharacterRange>* ranges(Zone* zone);
2914 uc16 standard_set_type() { return standard_set_type_; }
2915 void set_standard_set_type(uc16 special_set_type) {
2916 standard_set_type_ = special_set_type;
2918 bool is_standard() { return standard_set_type_ != 0; }
2919 void Canonicalize();
2921 ZoneList<CharacterRange>* ranges_;
2922 // If non-zero, the value represents a standard set (e.g., all whitespace
2923 // characters) without having to expand the ranges.
2924 uc16 standard_set_type_;
2928 class RegExpCharacterClass FINAL : public RegExpTree {
2930 RegExpCharacterClass(ZoneList<CharacterRange>* ranges, bool is_negated)
2932 is_negated_(is_negated) { }
2933 explicit RegExpCharacterClass(uc16 type)
2935 is_negated_(false) { }
2936 void* Accept(RegExpVisitor* visitor, void* data) OVERRIDE;
2937 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
2938 RegExpNode* on_success) OVERRIDE;
2939 RegExpCharacterClass* AsCharacterClass() OVERRIDE;
2940 bool IsCharacterClass() OVERRIDE;
2941 bool IsTextElement() OVERRIDE { return true; }
2942 int min_match() OVERRIDE { return 1; }
2943 int max_match() OVERRIDE { return 1; }
2944 void AppendToText(RegExpText* text, Zone* zone) OVERRIDE;
2945 CharacterSet character_set() { return set_; }
2946 // TODO(lrn): Remove need for complex version if is_standard that
2947 // recognizes a mangled standard set and just do { return set_.is_special(); }
2948 bool is_standard(Zone* zone);
2949 // Returns a value representing the standard character set if is_standard()
2951 // Currently used values are:
2952 // s : unicode whitespace
2953 // S : unicode non-whitespace
2954 // w : ASCII word character (digit, letter, underscore)
2955 // W : non-ASCII word character
2957 // D : non-ASCII digit
2958 // . : non-unicode non-newline
2959 // * : All characters
2960 uc16 standard_type() { return set_.standard_set_type(); }
2961 ZoneList<CharacterRange>* ranges(Zone* zone) { return set_.ranges(zone); }
2962 bool is_negated() { return is_negated_; }
2970 class RegExpAtom FINAL : public RegExpTree {
2972 explicit RegExpAtom(Vector<const uc16> data) : data_(data) { }
2973 void* Accept(RegExpVisitor* visitor, void* data) OVERRIDE;
2974 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
2975 RegExpNode* on_success) OVERRIDE;
2976 RegExpAtom* AsAtom() OVERRIDE;
2977 bool IsAtom() OVERRIDE;
2978 bool IsTextElement() OVERRIDE { return true; }
2979 int min_match() OVERRIDE { return data_.length(); }
2980 int max_match() OVERRIDE { return data_.length(); }
2981 void AppendToText(RegExpText* text, Zone* zone) OVERRIDE;
2982 Vector<const uc16> data() { return data_; }
2983 int length() { return data_.length(); }
2985 Vector<const uc16> data_;
2989 class RegExpText FINAL : public RegExpTree {
2991 explicit RegExpText(Zone* zone) : elements_(2, zone), length_(0) {}
2992 void* Accept(RegExpVisitor* visitor, void* data) OVERRIDE;
2993 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
2994 RegExpNode* on_success) OVERRIDE;
2995 RegExpText* AsText() OVERRIDE;
2996 bool IsText() OVERRIDE;
2997 bool IsTextElement() OVERRIDE { return true; }
2998 int min_match() OVERRIDE { return length_; }
2999 int max_match() OVERRIDE { return length_; }
3000 void AppendToText(RegExpText* text, Zone* zone) OVERRIDE;
3001 void AddElement(TextElement elm, Zone* zone) {
3002 elements_.Add(elm, zone);
3003 length_ += elm.length();
3005 ZoneList<TextElement>* elements() { return &elements_; }
3007 ZoneList<TextElement> elements_;
3012 class RegExpQuantifier FINAL : public RegExpTree {
3014 enum QuantifierType { GREEDY, NON_GREEDY, POSSESSIVE };
3015 RegExpQuantifier(int min, int max, QuantifierType type, RegExpTree* body)
3019 min_match_(min * body->min_match()),
3020 quantifier_type_(type) {
3021 if (max > 0 && body->max_match() > kInfinity / max) {
3022 max_match_ = kInfinity;
3024 max_match_ = max * body->max_match();
3027 void* Accept(RegExpVisitor* visitor, void* data) OVERRIDE;
3028 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
3029 RegExpNode* on_success) OVERRIDE;
3030 static RegExpNode* ToNode(int min,
3034 RegExpCompiler* compiler,
3035 RegExpNode* on_success,
3036 bool not_at_start = false);
3037 RegExpQuantifier* AsQuantifier() OVERRIDE;
3038 Interval CaptureRegisters() OVERRIDE;
3039 bool IsQuantifier() OVERRIDE;
3040 int min_match() OVERRIDE { return min_match_; }
3041 int max_match() OVERRIDE { return max_match_; }
3042 int min() { return min_; }
3043 int max() { return max_; }
3044 bool is_possessive() { return quantifier_type_ == POSSESSIVE; }
3045 bool is_non_greedy() { return quantifier_type_ == NON_GREEDY; }
3046 bool is_greedy() { return quantifier_type_ == GREEDY; }
3047 RegExpTree* body() { return body_; }
3055 QuantifierType quantifier_type_;
3059 class RegExpCapture FINAL : public RegExpTree {
3061 explicit RegExpCapture(RegExpTree* body, int index)
3062 : body_(body), index_(index) { }
3063 void* Accept(RegExpVisitor* visitor, void* data) OVERRIDE;
3064 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
3065 RegExpNode* on_success) OVERRIDE;
3066 static RegExpNode* ToNode(RegExpTree* body,
3068 RegExpCompiler* compiler,
3069 RegExpNode* on_success);
3070 RegExpCapture* AsCapture() OVERRIDE;
3071 bool IsAnchoredAtStart() OVERRIDE;
3072 bool IsAnchoredAtEnd() OVERRIDE;
3073 Interval CaptureRegisters() OVERRIDE;
3074 bool IsCapture() OVERRIDE;
3075 int min_match() OVERRIDE { return body_->min_match(); }
3076 int max_match() OVERRIDE { return body_->max_match(); }
3077 RegExpTree* body() { return body_; }
3078 int index() { return index_; }
3079 static int StartRegister(int index) { return index * 2; }
3080 static int EndRegister(int index) { return index * 2 + 1; }
3088 class RegExpLookahead FINAL : public RegExpTree {
3090 RegExpLookahead(RegExpTree* body,
3095 is_positive_(is_positive),
3096 capture_count_(capture_count),
3097 capture_from_(capture_from) { }
3099 void* Accept(RegExpVisitor* visitor, void* data) OVERRIDE;
3100 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
3101 RegExpNode* on_success) OVERRIDE;
3102 RegExpLookahead* AsLookahead() OVERRIDE;
3103 Interval CaptureRegisters() OVERRIDE;
3104 bool IsLookahead() OVERRIDE;
3105 bool IsAnchoredAtStart() OVERRIDE;
3106 int min_match() OVERRIDE { return 0; }
3107 int max_match() OVERRIDE { return 0; }
3108 RegExpTree* body() { return body_; }
3109 bool is_positive() { return is_positive_; }
3110 int capture_count() { return capture_count_; }
3111 int capture_from() { return capture_from_; }
3121 class RegExpBackReference FINAL : public RegExpTree {
3123 explicit RegExpBackReference(RegExpCapture* capture)
3124 : capture_(capture) { }
3125 void* Accept(RegExpVisitor* visitor, void* data) OVERRIDE;
3126 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
3127 RegExpNode* on_success) OVERRIDE;
3128 RegExpBackReference* AsBackReference() OVERRIDE;
3129 bool IsBackReference() OVERRIDE;
3130 int min_match() OVERRIDE { return 0; }
3131 int max_match() OVERRIDE { return capture_->max_match(); }
3132 int index() { return capture_->index(); }
3133 RegExpCapture* capture() { return capture_; }
3135 RegExpCapture* capture_;
3139 class RegExpEmpty FINAL : public RegExpTree {
3142 void* Accept(RegExpVisitor* visitor, void* data) OVERRIDE;
3143 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
3144 RegExpNode* on_success) OVERRIDE;
3145 RegExpEmpty* AsEmpty() OVERRIDE;
3146 bool IsEmpty() OVERRIDE;
3147 int min_match() OVERRIDE { return 0; }
3148 int max_match() OVERRIDE { return 0; }
3152 // ----------------------------------------------------------------------------
3154 // - leaf node visitors are abstract.
3156 class AstVisitor BASE_EMBEDDED {
3159 virtual ~AstVisitor() {}
3161 // Stack overflow check and dynamic dispatch.
3162 virtual void Visit(AstNode* node) = 0;
3164 // Iteration left-to-right.
3165 virtual void VisitDeclarations(ZoneList<Declaration*>* declarations);
3166 virtual void VisitStatements(ZoneList<Statement*>* statements);
3167 virtual void VisitExpressions(ZoneList<Expression*>* expressions);
3169 // Individual AST nodes.
3170 #define DEF_VISIT(type) \
3171 virtual void Visit##type(type* node) = 0;
3172 AST_NODE_LIST(DEF_VISIT)
3177 #define DEFINE_AST_VISITOR_SUBCLASS_MEMBERS() \
3179 void Visit(AstNode* node) FINAL { \
3180 if (!CheckStackOverflow()) node->Accept(this); \
3183 void SetStackOverflow() { stack_overflow_ = true; } \
3184 void ClearStackOverflow() { stack_overflow_ = false; } \
3185 bool HasStackOverflow() const { return stack_overflow_; } \
3187 bool CheckStackOverflow() { \
3188 if (stack_overflow_) return true; \
3189 StackLimitCheck check(isolate_); \
3190 if (!check.HasOverflowed()) return false; \
3191 stack_overflow_ = true; \
3196 void InitializeAstVisitor(Isolate* isolate, Zone* zone) { \
3197 isolate_ = isolate; \
3199 stack_overflow_ = false; \
3201 Zone* zone() { return zone_; } \
3202 Isolate* isolate() { return isolate_; } \
3204 Isolate* isolate_; \
3206 bool stack_overflow_
3209 // ----------------------------------------------------------------------------
3212 class AstNodeFactory FINAL BASE_EMBEDDED {
3214 explicit AstNodeFactory(AstValueFactory* ast_value_factory)
3215 : zone_(ast_value_factory->zone()),
3216 ast_value_factory_(ast_value_factory) {}
3218 VariableDeclaration* NewVariableDeclaration(VariableProxy* proxy,
3222 return new (zone_) VariableDeclaration(zone_, proxy, mode, scope, pos);
3225 FunctionDeclaration* NewFunctionDeclaration(VariableProxy* proxy,
3227 FunctionLiteral* fun,
3230 return new (zone_) FunctionDeclaration(zone_, proxy, mode, fun, scope, pos);
3233 ModuleDeclaration* NewModuleDeclaration(VariableProxy* proxy,
3237 return new (zone_) ModuleDeclaration(zone_, proxy, module, scope, pos);
3240 ImportDeclaration* NewImportDeclaration(VariableProxy* proxy,
3241 const AstRawString* import_name,
3242 const AstRawString* module_specifier,
3243 Scope* scope, int pos) {
3244 return new (zone_) ImportDeclaration(zone_, proxy, import_name,
3245 module_specifier, scope, pos);
3248 ExportDeclaration* NewExportDeclaration(VariableProxy* proxy,
3251 return new (zone_) ExportDeclaration(zone_, proxy, scope, pos);
3254 ModuleLiteral* NewModuleLiteral(Block* body, ModuleDescriptor* descriptor,
3256 return new (zone_) ModuleLiteral(zone_, body, descriptor, pos);
3259 ModulePath* NewModulePath(Module* origin, const AstRawString* name, int pos) {
3260 return new (zone_) ModulePath(zone_, origin, name, pos);
3263 ModuleUrl* NewModuleUrl(Handle<String> url, int pos) {
3264 return new (zone_) ModuleUrl(zone_, url, pos);
3267 Block* NewBlock(ZoneList<const AstRawString*>* labels,
3269 bool is_initializer_block,
3272 Block(zone_, labels, capacity, is_initializer_block, pos);
3275 #define STATEMENT_WITH_LABELS(NodeType) \
3276 NodeType* New##NodeType(ZoneList<const AstRawString*>* labels, int pos) { \
3277 return new (zone_) NodeType(zone_, labels, pos); \
3279 STATEMENT_WITH_LABELS(DoWhileStatement)
3280 STATEMENT_WITH_LABELS(WhileStatement)
3281 STATEMENT_WITH_LABELS(ForStatement)
3282 STATEMENT_WITH_LABELS(SwitchStatement)
3283 #undef STATEMENT_WITH_LABELS
3285 ForEachStatement* NewForEachStatement(ForEachStatement::VisitMode visit_mode,
3286 ZoneList<const AstRawString*>* labels,
3288 switch (visit_mode) {
3289 case ForEachStatement::ENUMERATE: {
3290 return new (zone_) ForInStatement(zone_, labels, pos);
3292 case ForEachStatement::ITERATE: {
3293 return new (zone_) ForOfStatement(zone_, labels, pos);
3300 ModuleStatement* NewModuleStatement(Block* body, int pos) {
3301 return new (zone_) ModuleStatement(zone_, body, pos);
3304 ExpressionStatement* NewExpressionStatement(Expression* expression, int pos) {
3305 return new (zone_) ExpressionStatement(zone_, expression, pos);
3308 ContinueStatement* NewContinueStatement(IterationStatement* target, int pos) {
3309 return new (zone_) ContinueStatement(zone_, target, pos);
3312 BreakStatement* NewBreakStatement(BreakableStatement* target, int pos) {
3313 return new (zone_) BreakStatement(zone_, target, pos);
3316 ReturnStatement* NewReturnStatement(Expression* expression, int pos) {
3317 return new (zone_) ReturnStatement(zone_, expression, pos);
3320 WithStatement* NewWithStatement(Scope* scope,
3321 Expression* expression,
3322 Statement* statement,
3324 return new (zone_) WithStatement(zone_, scope, expression, statement, pos);
3327 IfStatement* NewIfStatement(Expression* condition,
3328 Statement* then_statement,
3329 Statement* else_statement,
3332 IfStatement(zone_, condition, then_statement, else_statement, pos);
3335 TryCatchStatement* NewTryCatchStatement(int index,
3341 return new (zone_) TryCatchStatement(zone_, index, try_block, scope,
3342 variable, catch_block, pos);
3345 TryFinallyStatement* NewTryFinallyStatement(int index,
3347 Block* finally_block,
3350 TryFinallyStatement(zone_, index, try_block, finally_block, pos);
3353 DebuggerStatement* NewDebuggerStatement(int pos) {
3354 return new (zone_) DebuggerStatement(zone_, pos);
3357 EmptyStatement* NewEmptyStatement(int pos) {
3358 return new(zone_) EmptyStatement(zone_, pos);
3361 CaseClause* NewCaseClause(
3362 Expression* label, ZoneList<Statement*>* statements, int pos) {
3363 return new (zone_) CaseClause(zone_, label, statements, pos);
3366 Literal* NewStringLiteral(const AstRawString* string, int pos) {
3368 Literal(zone_, ast_value_factory_->NewString(string), pos);
3371 // A JavaScript symbol (ECMA-262 edition 6).
3372 Literal* NewSymbolLiteral(const char* name, int pos) {
3373 return new (zone_) Literal(zone_, ast_value_factory_->NewSymbol(name), pos);
3376 Literal* NewNumberLiteral(double number, int pos) {
3378 Literal(zone_, ast_value_factory_->NewNumber(number), pos);
3381 Literal* NewSmiLiteral(int number, int pos) {
3382 return new (zone_) Literal(zone_, ast_value_factory_->NewSmi(number), pos);
3385 Literal* NewBooleanLiteral(bool b, int pos) {
3386 return new (zone_) Literal(zone_, ast_value_factory_->NewBoolean(b), pos);
3389 Literal* NewNullLiteral(int pos) {
3390 return new (zone_) Literal(zone_, ast_value_factory_->NewNull(), pos);
3393 Literal* NewUndefinedLiteral(int pos) {
3394 return new (zone_) Literal(zone_, ast_value_factory_->NewUndefined(), pos);
3397 Literal* NewTheHoleLiteral(int pos) {
3398 return new (zone_) Literal(zone_, ast_value_factory_->NewTheHole(), pos);
3401 ObjectLiteral* NewObjectLiteral(
3402 ZoneList<ObjectLiteral::Property*>* properties,
3404 int boilerplate_properties,
3407 return new (zone_) ObjectLiteral(zone_, properties, literal_index,
3408 boilerplate_properties, has_function, pos);
3411 ObjectLiteral::Property* NewObjectLiteralProperty(
3412 Expression* key, Expression* value, ObjectLiteralProperty::Kind kind,
3413 bool is_static, bool is_computed_name) {
3415 ObjectLiteral::Property(key, value, kind, is_static, is_computed_name);
3418 ObjectLiteral::Property* NewObjectLiteralProperty(Expression* key,
3421 bool is_computed_name) {
3422 return new (zone_) ObjectLiteral::Property(ast_value_factory_, key, value,
3423 is_static, is_computed_name);
3426 RegExpLiteral* NewRegExpLiteral(const AstRawString* pattern,
3427 const AstRawString* flags,
3430 return new (zone_) RegExpLiteral(zone_, pattern, flags, literal_index, pos);
3433 ArrayLiteral* NewArrayLiteral(ZoneList<Expression*>* values,
3436 return new (zone_) ArrayLiteral(zone_, values, literal_index, pos);
3439 VariableProxy* NewVariableProxy(Variable* var,
3440 int start_position = RelocInfo::kNoPosition,
3441 int end_position = RelocInfo::kNoPosition) {
3442 return new (zone_) VariableProxy(zone_, var, start_position, end_position);
3445 VariableProxy* NewVariableProxy(const AstRawString* name,
3446 Variable::Kind variable_kind,
3447 int start_position = RelocInfo::kNoPosition,
3448 int end_position = RelocInfo::kNoPosition) {
3450 VariableProxy(zone_, name, variable_kind, start_position, end_position);
3453 Property* NewProperty(Expression* obj, Expression* key, int pos) {
3454 return new (zone_) Property(zone_, obj, key, pos);
3457 Call* NewCall(Expression* expression,
3458 ZoneList<Expression*>* arguments,
3460 return new (zone_) Call(zone_, expression, arguments, pos);
3463 CallNew* NewCallNew(Expression* expression,
3464 ZoneList<Expression*>* arguments,
3466 return new (zone_) CallNew(zone_, expression, arguments, pos);
3469 CallRuntime* NewCallRuntime(const AstRawString* name,
3470 const Runtime::Function* function,
3471 ZoneList<Expression*>* arguments,
3473 return new (zone_) CallRuntime(zone_, name, function, arguments, pos);
3476 UnaryOperation* NewUnaryOperation(Token::Value op,
3477 Expression* expression,
3479 return new (zone_) UnaryOperation(zone_, op, expression, pos);
3482 BinaryOperation* NewBinaryOperation(Token::Value op,
3486 return new (zone_) BinaryOperation(zone_, op, left, right, pos);
3489 CountOperation* NewCountOperation(Token::Value op,
3493 return new (zone_) CountOperation(zone_, op, is_prefix, expr, pos);
3496 CompareOperation* NewCompareOperation(Token::Value op,
3500 return new (zone_) CompareOperation(zone_, op, left, right, pos);
3503 Spread* NewSpread(Expression* expression, int pos) {
3504 return new (zone_) Spread(zone_, expression, pos);
3507 Conditional* NewConditional(Expression* condition,
3508 Expression* then_expression,
3509 Expression* else_expression,
3511 return new (zone_) Conditional(zone_, condition, then_expression,
3512 else_expression, position);
3515 Assignment* NewAssignment(Token::Value op,
3519 DCHECK(Token::IsAssignmentOp(op));
3520 Assignment* assign = new (zone_) Assignment(zone_, op, target, value, pos);
3521 if (assign->is_compound()) {
3522 DCHECK(Token::IsAssignmentOp(op));
3523 assign->binary_operation_ =
3524 NewBinaryOperation(assign->binary_op(), target, value, pos + 1);
3529 Yield* NewYield(Expression *generator_object,
3530 Expression* expression,
3531 Yield::Kind yield_kind,
3533 if (!expression) expression = NewUndefinedLiteral(pos);
3535 Yield(zone_, generator_object, expression, yield_kind, pos);
3538 Throw* NewThrow(Expression* exception, int pos) {
3539 return new (zone_) Throw(zone_, exception, pos);
3542 FunctionLiteral* NewFunctionLiteral(
3543 const AstRawString* name, AstValueFactory* ast_value_factory,
3544 Scope* scope, ZoneList<Statement*>* body, int materialized_literal_count,
3545 int expected_property_count, int handler_count, int parameter_count,
3546 FunctionLiteral::ParameterFlag has_duplicate_parameters,
3547 FunctionLiteral::FunctionType function_type,
3548 FunctionLiteral::IsFunctionFlag is_function,
3549 FunctionLiteral::IsParenthesizedFlag is_parenthesized, FunctionKind kind,
3551 return new (zone_) FunctionLiteral(
3552 zone_, name, ast_value_factory, scope, body, materialized_literal_count,
3553 expected_property_count, handler_count, parameter_count, function_type,
3554 has_duplicate_parameters, is_function, is_parenthesized, kind,
3558 ClassLiteral* NewClassLiteral(const AstRawString* name, Scope* scope,
3559 VariableProxy* proxy, Expression* extends,
3560 FunctionLiteral* constructor,
3561 ZoneList<ObjectLiteral::Property*>* properties,
3562 int start_position, int end_position) {
3564 ClassLiteral(zone_, name, scope, proxy, extends, constructor,
3565 properties, start_position, end_position);
3568 NativeFunctionLiteral* NewNativeFunctionLiteral(const AstRawString* name,
3569 v8::Extension* extension,
3571 return new (zone_) NativeFunctionLiteral(zone_, name, extension, pos);
3574 ThisFunction* NewThisFunction(int pos) {
3575 return new (zone_) ThisFunction(zone_, pos);
3578 SuperReference* NewSuperReference(VariableProxy* this_var, int pos) {
3579 return new (zone_) SuperReference(zone_, this_var, pos);
3584 AstValueFactory* ast_value_factory_;
3588 } } // namespace v8::internal