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() + 5; }
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 AssignmentId() const { return BailoutId(local_id(4)); }
930 BailoutId ContinueId() const OVERRIDE { return EntryId(); }
931 BailoutId StackCheckId() const OVERRIDE { return BodyId(); }
934 ForInStatement(Zone* zone, ZoneList<const AstRawString*>* labels, int pos)
935 : ForEachStatement(zone, labels, pos),
936 for_in_type_(SLOW_FOR_IN),
937 for_in_feedback_slot_(FeedbackVectorSlot::Invalid()) {}
938 static int parent_num_ids() { return ForEachStatement::num_ids(); }
941 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
943 ForInType for_in_type_;
944 FeedbackVectorSlot for_in_feedback_slot_;
948 class ForOfStatement FINAL : public ForEachStatement {
950 DECLARE_NODE_TYPE(ForOfStatement)
952 void Initialize(Expression* each,
955 Expression* assign_iterator,
956 Expression* next_result,
957 Expression* result_done,
958 Expression* assign_each) {
959 ForEachStatement::Initialize(each, subject, body);
960 assign_iterator_ = assign_iterator;
961 next_result_ = next_result;
962 result_done_ = result_done;
963 assign_each_ = assign_each;
966 Expression* iterable() const {
970 // iterator = subject[Symbol.iterator]()
971 Expression* assign_iterator() const {
972 return assign_iterator_;
975 // result = iterator.next() // with type check
976 Expression* next_result() const {
981 Expression* result_done() const {
985 // each = result.value
986 Expression* assign_each() const {
990 BailoutId ContinueId() const OVERRIDE { return EntryId(); }
991 BailoutId StackCheckId() const OVERRIDE { return BackEdgeId(); }
993 static int num_ids() { return parent_num_ids() + 1; }
994 BailoutId BackEdgeId() const { return BailoutId(local_id(0)); }
997 ForOfStatement(Zone* zone, ZoneList<const AstRawString*>* labels, int pos)
998 : ForEachStatement(zone, labels, pos),
999 assign_iterator_(NULL),
1002 assign_each_(NULL) {}
1003 static int parent_num_ids() { return ForEachStatement::num_ids(); }
1006 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
1008 Expression* assign_iterator_;
1009 Expression* next_result_;
1010 Expression* result_done_;
1011 Expression* assign_each_;
1015 class ExpressionStatement FINAL : public Statement {
1017 DECLARE_NODE_TYPE(ExpressionStatement)
1019 void set_expression(Expression* e) { expression_ = e; }
1020 Expression* expression() const { return expression_; }
1021 bool IsJump() const OVERRIDE { return expression_->IsThrow(); }
1024 ExpressionStatement(Zone* zone, Expression* expression, int pos)
1025 : Statement(zone, pos), expression_(expression) { }
1028 Expression* expression_;
1032 class JumpStatement : public Statement {
1034 bool IsJump() const FINAL { return true; }
1037 explicit JumpStatement(Zone* zone, int pos) : Statement(zone, pos) {}
1041 class ContinueStatement FINAL : public JumpStatement {
1043 DECLARE_NODE_TYPE(ContinueStatement)
1045 IterationStatement* target() const { return target_; }
1048 explicit ContinueStatement(Zone* zone, IterationStatement* target, int pos)
1049 : JumpStatement(zone, pos), target_(target) { }
1052 IterationStatement* target_;
1056 class BreakStatement FINAL : public JumpStatement {
1058 DECLARE_NODE_TYPE(BreakStatement)
1060 BreakableStatement* target() const { return target_; }
1063 explicit BreakStatement(Zone* zone, BreakableStatement* target, int pos)
1064 : JumpStatement(zone, pos), target_(target) { }
1067 BreakableStatement* target_;
1071 class ReturnStatement FINAL : public JumpStatement {
1073 DECLARE_NODE_TYPE(ReturnStatement)
1075 Expression* expression() const { return expression_; }
1078 explicit ReturnStatement(Zone* zone, Expression* expression, int pos)
1079 : JumpStatement(zone, pos), expression_(expression) { }
1082 Expression* expression_;
1086 class WithStatement FINAL : public Statement {
1088 DECLARE_NODE_TYPE(WithStatement)
1090 Scope* scope() { return scope_; }
1091 Expression* expression() const { return expression_; }
1092 Statement* statement() const { return statement_; }
1094 void set_base_id(int id) { base_id_ = id; }
1095 static int num_ids() { return parent_num_ids() + 1; }
1096 BailoutId EntryId() const { return BailoutId(local_id(0)); }
1099 WithStatement(Zone* zone, Scope* scope, Expression* expression,
1100 Statement* statement, int pos)
1101 : Statement(zone, pos),
1103 expression_(expression),
1104 statement_(statement),
1105 base_id_(BailoutId::None().ToInt()) {}
1106 static int parent_num_ids() { return 0; }
1108 int base_id() const {
1109 DCHECK(!BailoutId(base_id_).IsNone());
1114 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
1117 Expression* expression_;
1118 Statement* statement_;
1123 class CaseClause FINAL : public Expression {
1125 DECLARE_NODE_TYPE(CaseClause)
1127 bool is_default() const { return label_ == NULL; }
1128 Expression* label() const {
1129 CHECK(!is_default());
1132 Label* body_target() { return &body_target_; }
1133 ZoneList<Statement*>* statements() const { return statements_; }
1135 static int num_ids() { return parent_num_ids() + 2; }
1136 BailoutId EntryId() const { return BailoutId(local_id(0)); }
1137 TypeFeedbackId CompareId() { return TypeFeedbackId(local_id(1)); }
1139 Type* compare_type() { return compare_type_; }
1140 void set_compare_type(Type* type) { compare_type_ = type; }
1143 static int parent_num_ids() { return Expression::num_ids(); }
1146 CaseClause(Zone* zone, Expression* label, ZoneList<Statement*>* statements,
1148 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
1152 ZoneList<Statement*>* statements_;
1153 Type* compare_type_;
1157 class SwitchStatement FINAL : public BreakableStatement {
1159 DECLARE_NODE_TYPE(SwitchStatement)
1161 void Initialize(Expression* tag, ZoneList<CaseClause*>* cases) {
1166 Expression* tag() const { return tag_; }
1167 ZoneList<CaseClause*>* cases() const { return cases_; }
1170 SwitchStatement(Zone* zone, ZoneList<const AstRawString*>* labels, int pos)
1171 : BreakableStatement(zone, labels, TARGET_FOR_ANONYMOUS, pos),
1177 ZoneList<CaseClause*>* cases_;
1181 // If-statements always have non-null references to their then- and
1182 // else-parts. When parsing if-statements with no explicit else-part,
1183 // the parser implicitly creates an empty statement. Use the
1184 // HasThenStatement() and HasElseStatement() functions to check if a
1185 // given if-statement has a then- or an else-part containing code.
1186 class IfStatement FINAL : public Statement {
1188 DECLARE_NODE_TYPE(IfStatement)
1190 bool HasThenStatement() const { return !then_statement()->IsEmpty(); }
1191 bool HasElseStatement() const { return !else_statement()->IsEmpty(); }
1193 Expression* condition() const { return condition_; }
1194 Statement* then_statement() const { return then_statement_; }
1195 Statement* else_statement() const { return else_statement_; }
1197 bool IsJump() const OVERRIDE {
1198 return HasThenStatement() && then_statement()->IsJump()
1199 && HasElseStatement() && else_statement()->IsJump();
1202 void set_base_id(int id) { base_id_ = id; }
1203 static int num_ids() { return parent_num_ids() + 3; }
1204 BailoutId IfId() const { return BailoutId(local_id(0)); }
1205 BailoutId ThenId() const { return BailoutId(local_id(1)); }
1206 BailoutId ElseId() const { return BailoutId(local_id(2)); }
1209 IfStatement(Zone* zone, Expression* condition, Statement* then_statement,
1210 Statement* else_statement, int pos)
1211 : Statement(zone, pos),
1212 condition_(condition),
1213 then_statement_(then_statement),
1214 else_statement_(else_statement),
1215 base_id_(BailoutId::None().ToInt()) {}
1216 static int parent_num_ids() { return 0; }
1218 int base_id() const {
1219 DCHECK(!BailoutId(base_id_).IsNone());
1224 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
1226 Expression* condition_;
1227 Statement* then_statement_;
1228 Statement* else_statement_;
1233 class TryStatement : public Statement {
1235 int index() const { return index_; }
1236 Block* try_block() const { return try_block_; }
1239 TryStatement(Zone* zone, int index, Block* try_block, int pos)
1240 : Statement(zone, pos), index_(index), try_block_(try_block) {}
1243 // Unique (per-function) index of this handler. This is not an AST ID.
1250 class TryCatchStatement FINAL : public TryStatement {
1252 DECLARE_NODE_TYPE(TryCatchStatement)
1254 Scope* scope() { return scope_; }
1255 Variable* variable() { return variable_; }
1256 Block* catch_block() const { return catch_block_; }
1259 TryCatchStatement(Zone* zone,
1266 : TryStatement(zone, index, try_block, pos),
1268 variable_(variable),
1269 catch_block_(catch_block) {
1274 Variable* variable_;
1275 Block* catch_block_;
1279 class TryFinallyStatement FINAL : public TryStatement {
1281 DECLARE_NODE_TYPE(TryFinallyStatement)
1283 Block* finally_block() const { return finally_block_; }
1286 TryFinallyStatement(
1287 Zone* zone, int index, Block* try_block, Block* finally_block, int pos)
1288 : TryStatement(zone, index, try_block, pos),
1289 finally_block_(finally_block) { }
1292 Block* finally_block_;
1296 class DebuggerStatement FINAL : public Statement {
1298 DECLARE_NODE_TYPE(DebuggerStatement)
1300 void set_base_id(int id) { base_id_ = id; }
1301 static int num_ids() { return parent_num_ids() + 1; }
1302 BailoutId DebugBreakId() const { return BailoutId(local_id(0)); }
1305 explicit DebuggerStatement(Zone* zone, int pos)
1306 : Statement(zone, pos), base_id_(BailoutId::None().ToInt()) {}
1307 static int parent_num_ids() { return 0; }
1309 int base_id() const {
1310 DCHECK(!BailoutId(base_id_).IsNone());
1315 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
1321 class EmptyStatement FINAL : public Statement {
1323 DECLARE_NODE_TYPE(EmptyStatement)
1326 explicit EmptyStatement(Zone* zone, int pos): Statement(zone, pos) {}
1330 class Literal FINAL : public Expression {
1332 DECLARE_NODE_TYPE(Literal)
1334 bool IsPropertyName() const OVERRIDE { return value_->IsPropertyName(); }
1336 Handle<String> AsPropertyName() {
1337 DCHECK(IsPropertyName());
1338 return Handle<String>::cast(value());
1341 const AstRawString* AsRawPropertyName() {
1342 DCHECK(IsPropertyName());
1343 return value_->AsString();
1346 bool ToBooleanIsTrue() const OVERRIDE { return value()->BooleanValue(); }
1347 bool ToBooleanIsFalse() const OVERRIDE { return !value()->BooleanValue(); }
1349 Handle<Object> value() const { return value_->value(); }
1350 const AstValue* raw_value() const { return value_; }
1352 // Support for using Literal as a HashMap key. NOTE: Currently, this works
1353 // only for string and number literals!
1355 static bool Match(void* literal1, void* literal2);
1357 static int num_ids() { return parent_num_ids() + 1; }
1358 TypeFeedbackId LiteralFeedbackId() const {
1359 return TypeFeedbackId(local_id(0));
1363 Literal(Zone* zone, const AstValue* value, int position)
1364 : Expression(zone, position), value_(value) {}
1365 static int parent_num_ids() { return Expression::num_ids(); }
1368 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
1370 const AstValue* value_;
1374 // Base class for literals that needs space in the corresponding JSFunction.
1375 class MaterializedLiteral : public Expression {
1377 virtual MaterializedLiteral* AsMaterializedLiteral() { return this; }
1379 int literal_index() { return literal_index_; }
1382 // only callable after initialization.
1383 DCHECK(depth_ >= 1);
1388 MaterializedLiteral(Zone* zone, int literal_index, int pos)
1389 : Expression(zone, pos),
1390 literal_index_(literal_index),
1394 // A materialized literal is simple if the values consist of only
1395 // constants and simple object and array literals.
1396 bool is_simple() const { return is_simple_; }
1397 void set_is_simple(bool is_simple) { is_simple_ = is_simple; }
1398 friend class CompileTimeValue;
1400 void set_depth(int depth) {
1405 // Populate the constant properties/elements fixed array.
1406 void BuildConstants(Isolate* isolate);
1407 friend class ArrayLiteral;
1408 friend class ObjectLiteral;
1410 // If the expression is a literal, return the literal value;
1411 // if the expression is a materialized literal and is simple return a
1412 // compile time value as encoded by CompileTimeValue::GetValue().
1413 // Otherwise, return undefined literal as the placeholder
1414 // in the object literal boilerplate.
1415 Handle<Object> GetBoilerplateValue(Expression* expression, Isolate* isolate);
1424 // Property is used for passing information
1425 // about an object literal's properties from the parser
1426 // to the code generator.
1427 class ObjectLiteralProperty FINAL : public ZoneObject {
1430 CONSTANT, // Property with constant value (compile time).
1431 COMPUTED, // Property with computed value (execution time).
1432 MATERIALIZED_LITERAL, // Property value is a materialized literal.
1433 GETTER, SETTER, // Property is an accessor function.
1434 PROTOTYPE // Property is __proto__.
1437 Expression* key() { return key_; }
1438 Expression* value() { return value_; }
1439 Kind kind() { return kind_; }
1441 // Type feedback information.
1442 bool IsMonomorphic() { return !receiver_type_.is_null(); }
1443 Handle<Map> GetReceiverType() { return receiver_type_; }
1445 bool IsCompileTimeValue();
1447 void set_emit_store(bool emit_store);
1450 bool is_static() const { return is_static_; }
1451 bool is_computed_name() const { return is_computed_name_; }
1453 void set_receiver_type(Handle<Map> map) { receiver_type_ = map; }
1456 friend class AstNodeFactory;
1458 ObjectLiteralProperty(Expression* key, Expression* value, Kind kind,
1459 bool is_static, bool is_computed_name);
1460 ObjectLiteralProperty(AstValueFactory* ast_value_factory, Expression* key,
1461 Expression* value, bool is_static,
1462 bool is_computed_name);
1470 bool is_computed_name_;
1471 Handle<Map> receiver_type_;
1475 // An object literal has a boilerplate object that is used
1476 // for minimizing the work when constructing it at runtime.
1477 class ObjectLiteral FINAL : public MaterializedLiteral {
1479 typedef ObjectLiteralProperty Property;
1481 DECLARE_NODE_TYPE(ObjectLiteral)
1483 Handle<FixedArray> constant_properties() const {
1484 return constant_properties_;
1486 int properties_count() const { return constant_properties_->length() / 2; }
1487 ZoneList<Property*>* properties() const { return properties_; }
1488 bool fast_elements() const { return fast_elements_; }
1489 bool may_store_doubles() const { return may_store_doubles_; }
1490 bool has_function() const { return has_function_; }
1491 bool has_elements() const { return has_elements_; }
1493 // Decide if a property should be in the object boilerplate.
1494 static bool IsBoilerplateProperty(Property* property);
1496 // Populate the constant properties fixed array.
1497 void BuildConstantProperties(Isolate* isolate);
1499 // Mark all computed expressions that are bound to a key that
1500 // is shadowed by a later occurrence of the same key. For the
1501 // marked expressions, no store code is emitted.
1502 void CalculateEmitStore(Zone* zone);
1504 // Assemble bitfield of flags for the CreateObjectLiteral helper.
1505 int ComputeFlags(bool disable_mementos = false) const {
1506 int flags = fast_elements() ? kFastElements : kNoFlags;
1507 flags |= has_function() ? kHasFunction : kNoFlags;
1508 if (disable_mementos) {
1509 flags |= kDisableMementos;
1517 kHasFunction = 1 << 1,
1518 kDisableMementos = 1 << 2
1521 struct Accessors: public ZoneObject {
1522 Accessors() : getter(NULL), setter(NULL) {}
1527 BailoutId CreateLiteralId() const { return BailoutId(local_id(0)); }
1529 // Return an AST id for a property that is used in simulate instructions.
1530 BailoutId GetIdForProperty(int i) { return BailoutId(local_id(i + 1)); }
1532 // Unlike other AST nodes, this number of bailout IDs allocated for an
1533 // ObjectLiteral can vary, so num_ids() is not a static method.
1534 int num_ids() const { return parent_num_ids() + 1 + properties()->length(); }
1537 ObjectLiteral(Zone* zone, ZoneList<Property*>* properties, int literal_index,
1538 int boilerplate_properties, bool has_function, int pos)
1539 : MaterializedLiteral(zone, literal_index, pos),
1540 properties_(properties),
1541 boilerplate_properties_(boilerplate_properties),
1542 fast_elements_(false),
1543 has_elements_(false),
1544 may_store_doubles_(false),
1545 has_function_(has_function) {}
1546 static int parent_num_ids() { return MaterializedLiteral::num_ids(); }
1549 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
1550 Handle<FixedArray> constant_properties_;
1551 ZoneList<Property*>* properties_;
1552 int boilerplate_properties_;
1553 bool fast_elements_;
1555 bool may_store_doubles_;
1560 // Node for capturing a regexp literal.
1561 class RegExpLiteral FINAL : public MaterializedLiteral {
1563 DECLARE_NODE_TYPE(RegExpLiteral)
1565 Handle<String> pattern() const { return pattern_->string(); }
1566 Handle<String> flags() const { return flags_->string(); }
1569 RegExpLiteral(Zone* zone, const AstRawString* pattern,
1570 const AstRawString* flags, int literal_index, int pos)
1571 : MaterializedLiteral(zone, literal_index, pos),
1578 const AstRawString* pattern_;
1579 const AstRawString* flags_;
1583 // An array literal has a literals object that is used
1584 // for minimizing the work when constructing it at runtime.
1585 class ArrayLiteral FINAL : public MaterializedLiteral {
1587 DECLARE_NODE_TYPE(ArrayLiteral)
1589 Handle<FixedArray> constant_elements() const { return constant_elements_; }
1590 ElementsKind constant_elements_kind() const {
1591 DCHECK_EQ(2, constant_elements_->length());
1592 return static_cast<ElementsKind>(
1593 Smi::cast(constant_elements_->get(0))->value());
1596 ZoneList<Expression*>* values() const { return values_; }
1598 BailoutId CreateLiteralId() const { return BailoutId(local_id(0)); }
1600 // Return an AST id for an element that is used in simulate instructions.
1601 BailoutId GetIdForElement(int i) { return BailoutId(local_id(i + 1)); }
1603 // Unlike other AST nodes, this number of bailout IDs allocated for an
1604 // ArrayLiteral can vary, so num_ids() is not a static method.
1605 int num_ids() const { return parent_num_ids() + 1 + values()->length(); }
1607 // Populate the constant elements fixed array.
1608 void BuildConstantElements(Isolate* isolate);
1610 // Assemble bitfield of flags for the CreateArrayLiteral helper.
1611 int ComputeFlags(bool disable_mementos = false) const {
1612 int flags = depth() == 1 ? kShallowElements : kNoFlags;
1613 if (disable_mementos) {
1614 flags |= kDisableMementos;
1621 kShallowElements = 1,
1622 kDisableMementos = 1 << 1
1626 ArrayLiteral(Zone* zone, ZoneList<Expression*>* values, int literal_index,
1628 : MaterializedLiteral(zone, literal_index, pos), values_(values) {}
1629 static int parent_num_ids() { return MaterializedLiteral::num_ids(); }
1632 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
1634 Handle<FixedArray> constant_elements_;
1635 ZoneList<Expression*>* values_;
1639 class VariableProxy FINAL : public Expression {
1641 DECLARE_NODE_TYPE(VariableProxy)
1643 bool IsValidReferenceExpression() const OVERRIDE { return !is_this(); }
1645 bool IsArguments() const { return is_resolved() && var()->is_arguments(); }
1647 Handle<String> name() const { return raw_name()->string(); }
1648 const AstRawString* raw_name() const {
1649 return is_resolved() ? var_->raw_name() : raw_name_;
1652 Variable* var() const {
1653 DCHECK(is_resolved());
1656 void set_var(Variable* v) {
1657 DCHECK(!is_resolved());
1662 bool is_this() const { return IsThisField::decode(bit_field_); }
1664 bool is_assigned() const { return IsAssignedField::decode(bit_field_); }
1665 void set_is_assigned() {
1666 bit_field_ = IsAssignedField::update(bit_field_, true);
1669 bool is_resolved() const { return IsResolvedField::decode(bit_field_); }
1670 void set_is_resolved() {
1671 bit_field_ = IsResolvedField::update(bit_field_, true);
1674 int end_position() const { return end_position_; }
1676 // Bind this proxy to the variable var.
1677 void BindTo(Variable* var);
1679 bool UsesVariableFeedbackSlot() const {
1680 return FLAG_vector_ics && (var()->IsUnallocated() || var()->IsLookupSlot());
1683 virtual FeedbackVectorRequirements ComputeFeedbackRequirements(
1684 Isolate* isolate, const ICSlotCache* cache) OVERRIDE;
1686 void SetFirstFeedbackICSlot(FeedbackVectorICSlot slot,
1687 ICSlotCache* cache) OVERRIDE;
1688 Code::Kind FeedbackICSlotKind(int index) OVERRIDE { return Code::LOAD_IC; }
1689 FeedbackVectorICSlot VariableFeedbackSlot() {
1690 DCHECK(!UsesVariableFeedbackSlot() || !variable_feedback_slot_.IsInvalid());
1691 return variable_feedback_slot_;
1695 VariableProxy(Zone* zone, Variable* var, int start_position,
1698 VariableProxy(Zone* zone, const AstRawString* name,
1699 Variable::Kind variable_kind, int start_position,
1702 class IsThisField : public BitField8<bool, 0, 1> {};
1703 class IsAssignedField : public BitField8<bool, 1, 1> {};
1704 class IsResolvedField : public BitField8<bool, 2, 1> {};
1706 // Start with 16-bit (or smaller) field, which should get packed together
1707 // with Expression's trailing 16-bit field.
1709 FeedbackVectorICSlot variable_feedback_slot_;
1711 const AstRawString* raw_name_; // if !is_resolved_
1712 Variable* var_; // if is_resolved_
1714 // Position is stored in the AstNode superclass, but VariableProxy needs to
1715 // know its end position too (for error messages). It cannot be inferred from
1716 // the variable name length because it can contain escapes.
1721 class Property FINAL : public Expression {
1723 DECLARE_NODE_TYPE(Property)
1725 bool IsValidReferenceExpression() const OVERRIDE { return true; }
1727 Expression* obj() const { return obj_; }
1728 Expression* key() const { return key_; }
1730 static int num_ids() { return parent_num_ids() + 2; }
1731 BailoutId LoadId() const { return BailoutId(local_id(0)); }
1732 TypeFeedbackId PropertyFeedbackId() { return TypeFeedbackId(local_id(1)); }
1734 bool IsStringAccess() const {
1735 return IsStringAccessField::decode(bit_field_);
1738 // Type feedback information.
1739 bool IsMonomorphic() OVERRIDE { return receiver_types_.length() == 1; }
1740 SmallMapList* GetReceiverTypes() OVERRIDE { return &receiver_types_; }
1741 KeyedAccessStoreMode GetStoreMode() const OVERRIDE { return STANDARD_STORE; }
1742 IcCheckType GetKeyType() const OVERRIDE {
1743 return KeyTypeField::decode(bit_field_);
1745 bool IsUninitialized() const {
1746 return !is_for_call() && HasNoTypeInformation();
1748 bool HasNoTypeInformation() const {
1749 return IsUninitializedField::decode(bit_field_);
1751 void set_is_uninitialized(bool b) {
1752 bit_field_ = IsUninitializedField::update(bit_field_, b);
1754 void set_is_string_access(bool b) {
1755 bit_field_ = IsStringAccessField::update(bit_field_, b);
1757 void set_key_type(IcCheckType key_type) {
1758 bit_field_ = KeyTypeField::update(bit_field_, key_type);
1760 void mark_for_call() {
1761 bit_field_ = IsForCallField::update(bit_field_, true);
1763 bool is_for_call() const { return IsForCallField::decode(bit_field_); }
1765 bool IsSuperAccess() {
1766 return obj()->IsSuperReference();
1769 virtual FeedbackVectorRequirements ComputeFeedbackRequirements(
1770 Isolate* isolate, const ICSlotCache* cache) OVERRIDE {
1771 return FeedbackVectorRequirements(0, FLAG_vector_ics ? 1 : 0);
1773 void SetFirstFeedbackICSlot(FeedbackVectorICSlot slot,
1774 ICSlotCache* cache) OVERRIDE {
1775 property_feedback_slot_ = slot;
1777 Code::Kind FeedbackICSlotKind(int index) OVERRIDE {
1778 return key()->IsPropertyName() ? Code::LOAD_IC : Code::KEYED_LOAD_IC;
1781 FeedbackVectorICSlot PropertyFeedbackSlot() const {
1782 DCHECK(!FLAG_vector_ics || !property_feedback_slot_.IsInvalid());
1783 return property_feedback_slot_;
1787 Property(Zone* zone, Expression* obj, Expression* key, int pos)
1788 : Expression(zone, pos),
1789 bit_field_(IsForCallField::encode(false) |
1790 IsUninitializedField::encode(false) |
1791 IsStringAccessField::encode(false)),
1792 property_feedback_slot_(FeedbackVectorICSlot::Invalid()),
1795 static int parent_num_ids() { return Expression::num_ids(); }
1798 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
1800 class IsForCallField : public BitField8<bool, 0, 1> {};
1801 class IsUninitializedField : public BitField8<bool, 1, 1> {};
1802 class IsStringAccessField : public BitField8<bool, 2, 1> {};
1803 class KeyTypeField : public BitField8<IcCheckType, 3, 1> {};
1805 FeedbackVectorICSlot property_feedback_slot_;
1808 SmallMapList receiver_types_;
1812 class Call FINAL : public Expression {
1814 DECLARE_NODE_TYPE(Call)
1816 Expression* expression() const { return expression_; }
1817 ZoneList<Expression*>* arguments() const { return arguments_; }
1819 // Type feedback information.
1820 virtual FeedbackVectorRequirements ComputeFeedbackRequirements(
1821 Isolate* isolate, const ICSlotCache* cache) OVERRIDE;
1822 void SetFirstFeedbackICSlot(FeedbackVectorICSlot slot,
1823 ICSlotCache* cache) OVERRIDE {
1824 ic_slot_or_slot_ = slot.ToInt();
1826 void SetFirstFeedbackSlot(FeedbackVectorSlot slot) OVERRIDE {
1827 ic_slot_or_slot_ = slot.ToInt();
1829 Code::Kind FeedbackICSlotKind(int index) OVERRIDE { return Code::CALL_IC; }
1831 FeedbackVectorSlot CallFeedbackSlot() const {
1832 DCHECK(ic_slot_or_slot_ != FeedbackVectorSlot::Invalid().ToInt());
1833 return FeedbackVectorSlot(ic_slot_or_slot_);
1836 FeedbackVectorICSlot CallFeedbackICSlot() const {
1837 DCHECK(ic_slot_or_slot_ != FeedbackVectorICSlot::Invalid().ToInt());
1838 return FeedbackVectorICSlot(ic_slot_or_slot_);
1841 SmallMapList* GetReceiverTypes() OVERRIDE {
1842 if (expression()->IsProperty()) {
1843 return expression()->AsProperty()->GetReceiverTypes();
1848 bool IsMonomorphic() OVERRIDE {
1849 if (expression()->IsProperty()) {
1850 return expression()->AsProperty()->IsMonomorphic();
1852 return !target_.is_null();
1855 bool global_call() const {
1856 VariableProxy* proxy = expression_->AsVariableProxy();
1857 return proxy != NULL && proxy->var()->IsUnallocated();
1860 bool known_global_function() const {
1861 return global_call() && !target_.is_null();
1864 Handle<JSFunction> target() { return target_; }
1866 Handle<AllocationSite> allocation_site() { return allocation_site_; }
1868 void SetKnownGlobalTarget(Handle<JSFunction> target) {
1870 set_is_uninitialized(false);
1872 void set_target(Handle<JSFunction> target) { target_ = target; }
1873 void set_allocation_site(Handle<AllocationSite> site) {
1874 allocation_site_ = site;
1877 static int num_ids() { return parent_num_ids() + 2; }
1878 BailoutId ReturnId() const { return BailoutId(local_id(0)); }
1879 BailoutId EvalOrLookupId() const { return BailoutId(local_id(1)); }
1881 bool is_uninitialized() const {
1882 return IsUninitializedField::decode(bit_field_);
1884 void set_is_uninitialized(bool b) {
1885 bit_field_ = IsUninitializedField::update(bit_field_, b);
1897 // Helpers to determine how to handle the call.
1898 CallType GetCallType(Isolate* isolate) const;
1899 bool IsUsingCallFeedbackSlot(Isolate* isolate) const;
1900 bool IsUsingCallFeedbackICSlot(Isolate* isolate) const;
1903 // Used to assert that the FullCodeGenerator records the return site.
1904 bool return_is_recorded_;
1908 Call(Zone* zone, Expression* expression, ZoneList<Expression*>* arguments,
1910 : Expression(zone, pos),
1911 ic_slot_or_slot_(FeedbackVectorICSlot::Invalid().ToInt()),
1912 expression_(expression),
1913 arguments_(arguments),
1914 bit_field_(IsUninitializedField::encode(false)) {
1915 if (expression->IsProperty()) {
1916 expression->AsProperty()->mark_for_call();
1919 static int parent_num_ids() { return Expression::num_ids(); }
1922 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
1924 // We store this as an integer because we don't know if we have a slot or
1925 // an ic slot until scoping time.
1926 int ic_slot_or_slot_;
1927 Expression* expression_;
1928 ZoneList<Expression*>* arguments_;
1929 Handle<JSFunction> target_;
1930 Handle<AllocationSite> allocation_site_;
1931 class IsUninitializedField : public BitField8<bool, 0, 1> {};
1936 class CallNew FINAL : public Expression {
1938 DECLARE_NODE_TYPE(CallNew)
1940 Expression* expression() const { return expression_; }
1941 ZoneList<Expression*>* arguments() const { return arguments_; }
1943 // Type feedback information.
1944 virtual FeedbackVectorRequirements ComputeFeedbackRequirements(
1945 Isolate* isolate, const ICSlotCache* cache) OVERRIDE {
1946 return FeedbackVectorRequirements(FLAG_pretenuring_call_new ? 2 : 1, 0);
1948 void SetFirstFeedbackSlot(FeedbackVectorSlot slot) OVERRIDE {
1949 callnew_feedback_slot_ = slot;
1952 FeedbackVectorSlot CallNewFeedbackSlot() {
1953 DCHECK(!callnew_feedback_slot_.IsInvalid());
1954 return callnew_feedback_slot_;
1956 FeedbackVectorSlot AllocationSiteFeedbackSlot() {
1957 DCHECK(FLAG_pretenuring_call_new);
1958 return CallNewFeedbackSlot().next();
1961 bool IsMonomorphic() OVERRIDE { return is_monomorphic_; }
1962 Handle<JSFunction> target() const { return target_; }
1963 Handle<AllocationSite> allocation_site() const {
1964 return allocation_site_;
1967 static int num_ids() { return parent_num_ids() + 1; }
1968 static int feedback_slots() { return 1; }
1969 BailoutId ReturnId() const { return BailoutId(local_id(0)); }
1971 void set_allocation_site(Handle<AllocationSite> site) {
1972 allocation_site_ = site;
1974 void set_is_monomorphic(bool monomorphic) { is_monomorphic_ = monomorphic; }
1975 void set_target(Handle<JSFunction> target) { target_ = target; }
1976 void SetKnownGlobalTarget(Handle<JSFunction> target) {
1978 is_monomorphic_ = true;
1982 CallNew(Zone* zone, Expression* expression, ZoneList<Expression*>* arguments,
1984 : Expression(zone, pos),
1985 expression_(expression),
1986 arguments_(arguments),
1987 is_monomorphic_(false),
1988 callnew_feedback_slot_(FeedbackVectorSlot::Invalid()) {}
1990 static int parent_num_ids() { return Expression::num_ids(); }
1993 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
1995 Expression* expression_;
1996 ZoneList<Expression*>* arguments_;
1997 bool is_monomorphic_;
1998 Handle<JSFunction> target_;
1999 Handle<AllocationSite> allocation_site_;
2000 FeedbackVectorSlot callnew_feedback_slot_;
2004 // The CallRuntime class does not represent any official JavaScript
2005 // language construct. Instead it is used to call a C or JS function
2006 // with a set of arguments. This is used from the builtins that are
2007 // implemented in JavaScript (see "v8natives.js").
2008 class CallRuntime FINAL : public Expression {
2010 DECLARE_NODE_TYPE(CallRuntime)
2012 Handle<String> name() const { return raw_name_->string(); }
2013 const AstRawString* raw_name() const { return raw_name_; }
2014 const Runtime::Function* function() const { return function_; }
2015 ZoneList<Expression*>* arguments() const { return arguments_; }
2016 bool is_jsruntime() const { return function_ == NULL; }
2018 // Type feedback information.
2019 bool HasCallRuntimeFeedbackSlot() const {
2020 return FLAG_vector_ics && is_jsruntime();
2022 virtual FeedbackVectorRequirements ComputeFeedbackRequirements(
2023 Isolate* isolate, const ICSlotCache* cache) OVERRIDE {
2024 return FeedbackVectorRequirements(0, HasCallRuntimeFeedbackSlot() ? 1 : 0);
2026 void SetFirstFeedbackICSlot(FeedbackVectorICSlot slot,
2027 ICSlotCache* cache) OVERRIDE {
2028 callruntime_feedback_slot_ = slot;
2030 Code::Kind FeedbackICSlotKind(int index) OVERRIDE { return Code::LOAD_IC; }
2032 FeedbackVectorICSlot CallRuntimeFeedbackSlot() {
2033 DCHECK(!HasCallRuntimeFeedbackSlot() ||
2034 !callruntime_feedback_slot_.IsInvalid());
2035 return callruntime_feedback_slot_;
2038 static int num_ids() { return parent_num_ids() + 1; }
2039 TypeFeedbackId CallRuntimeFeedbackId() const {
2040 return TypeFeedbackId(local_id(0));
2044 CallRuntime(Zone* zone, const AstRawString* name,
2045 const Runtime::Function* function,
2046 ZoneList<Expression*>* arguments, int pos)
2047 : Expression(zone, pos),
2049 function_(function),
2050 arguments_(arguments),
2051 callruntime_feedback_slot_(FeedbackVectorICSlot::Invalid()) {}
2052 static int parent_num_ids() { return Expression::num_ids(); }
2055 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
2057 const AstRawString* raw_name_;
2058 const Runtime::Function* function_;
2059 ZoneList<Expression*>* arguments_;
2060 FeedbackVectorICSlot callruntime_feedback_slot_;
2064 class UnaryOperation FINAL : public Expression {
2066 DECLARE_NODE_TYPE(UnaryOperation)
2068 Token::Value op() const { return op_; }
2069 Expression* expression() const { return expression_; }
2071 // For unary not (Token::NOT), the AST ids where true and false will
2072 // actually be materialized, respectively.
2073 static int num_ids() { return parent_num_ids() + 2; }
2074 BailoutId MaterializeTrueId() const { return BailoutId(local_id(0)); }
2075 BailoutId MaterializeFalseId() const { return BailoutId(local_id(1)); }
2077 virtual void RecordToBooleanTypeFeedback(
2078 TypeFeedbackOracle* oracle) OVERRIDE;
2081 UnaryOperation(Zone* zone, Token::Value op, Expression* expression, int pos)
2082 : Expression(zone, pos), op_(op), expression_(expression) {
2083 DCHECK(Token::IsUnaryOp(op));
2085 static int parent_num_ids() { return Expression::num_ids(); }
2088 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
2091 Expression* expression_;
2095 class BinaryOperation FINAL : public Expression {
2097 DECLARE_NODE_TYPE(BinaryOperation)
2099 Token::Value op() const { return static_cast<Token::Value>(op_); }
2100 Expression* left() const { return left_; }
2101 Expression* right() const { return right_; }
2102 Handle<AllocationSite> allocation_site() const { return allocation_site_; }
2103 void set_allocation_site(Handle<AllocationSite> allocation_site) {
2104 allocation_site_ = allocation_site;
2107 // The short-circuit logical operations need an AST ID for their
2108 // right-hand subexpression.
2109 static int num_ids() { return parent_num_ids() + 2; }
2110 BailoutId RightId() const { return BailoutId(local_id(0)); }
2112 TypeFeedbackId BinaryOperationFeedbackId() const {
2113 return TypeFeedbackId(local_id(1));
2115 Maybe<int> fixed_right_arg() const {
2116 return has_fixed_right_arg_ ? Just(fixed_right_arg_value_) : Nothing<int>();
2118 void set_fixed_right_arg(Maybe<int> arg) {
2119 has_fixed_right_arg_ = arg.IsJust();
2120 if (arg.IsJust()) fixed_right_arg_value_ = arg.FromJust();
2123 virtual void RecordToBooleanTypeFeedback(
2124 TypeFeedbackOracle* oracle) OVERRIDE;
2127 BinaryOperation(Zone* zone, Token::Value op, Expression* left,
2128 Expression* right, int pos)
2129 : Expression(zone, pos),
2130 op_(static_cast<byte>(op)),
2131 has_fixed_right_arg_(false),
2132 fixed_right_arg_value_(0),
2135 DCHECK(Token::IsBinaryOp(op));
2137 static int parent_num_ids() { return Expression::num_ids(); }
2140 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
2142 const byte op_; // actually Token::Value
2143 // TODO(rossberg): the fixed arg should probably be represented as a Constant
2144 // type for the RHS. Currenty it's actually a Maybe<int>
2145 bool has_fixed_right_arg_;
2146 int fixed_right_arg_value_;
2149 Handle<AllocationSite> allocation_site_;
2153 class CountOperation FINAL : public Expression {
2155 DECLARE_NODE_TYPE(CountOperation)
2157 bool is_prefix() const { return IsPrefixField::decode(bit_field_); }
2158 bool is_postfix() const { return !is_prefix(); }
2160 Token::Value op() const { return TokenField::decode(bit_field_); }
2161 Token::Value binary_op() {
2162 return (op() == Token::INC) ? Token::ADD : Token::SUB;
2165 Expression* expression() const { return expression_; }
2167 bool IsMonomorphic() OVERRIDE { return receiver_types_.length() == 1; }
2168 SmallMapList* GetReceiverTypes() OVERRIDE { return &receiver_types_; }
2169 IcCheckType GetKeyType() const OVERRIDE {
2170 return KeyTypeField::decode(bit_field_);
2172 KeyedAccessStoreMode GetStoreMode() const OVERRIDE {
2173 return StoreModeField::decode(bit_field_);
2175 Type* type() const { return type_; }
2176 void set_key_type(IcCheckType type) {
2177 bit_field_ = KeyTypeField::update(bit_field_, type);
2179 void set_store_mode(KeyedAccessStoreMode mode) {
2180 bit_field_ = StoreModeField::update(bit_field_, mode);
2182 void set_type(Type* type) { type_ = type; }
2184 static int num_ids() { return parent_num_ids() + 4; }
2185 BailoutId AssignmentId() const { return BailoutId(local_id(0)); }
2186 BailoutId ToNumberId() const { return BailoutId(local_id(1)); }
2187 TypeFeedbackId CountBinOpFeedbackId() const {
2188 return TypeFeedbackId(local_id(2));
2190 TypeFeedbackId CountStoreFeedbackId() const {
2191 return TypeFeedbackId(local_id(3));
2195 CountOperation(Zone* zone, Token::Value op, bool is_prefix, Expression* expr,
2197 : Expression(zone, pos),
2198 bit_field_(IsPrefixField::encode(is_prefix) |
2199 KeyTypeField::encode(ELEMENT) |
2200 StoreModeField::encode(STANDARD_STORE) |
2201 TokenField::encode(op)),
2203 expression_(expr) {}
2204 static int parent_num_ids() { return Expression::num_ids(); }
2207 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
2209 class IsPrefixField : public BitField16<bool, 0, 1> {};
2210 class KeyTypeField : public BitField16<IcCheckType, 1, 1> {};
2211 class StoreModeField : public BitField16<KeyedAccessStoreMode, 2, 4> {};
2212 class TokenField : public BitField16<Token::Value, 6, 8> {};
2214 // Starts with 16-bit field, which should get packed together with
2215 // Expression's trailing 16-bit field.
2216 uint16_t bit_field_;
2218 Expression* expression_;
2219 SmallMapList receiver_types_;
2223 class CompareOperation FINAL : public Expression {
2225 DECLARE_NODE_TYPE(CompareOperation)
2227 Token::Value op() const { return op_; }
2228 Expression* left() const { return left_; }
2229 Expression* right() const { return right_; }
2231 // Type feedback information.
2232 static int num_ids() { return parent_num_ids() + 1; }
2233 TypeFeedbackId CompareOperationFeedbackId() const {
2234 return TypeFeedbackId(local_id(0));
2236 Type* combined_type() const { return combined_type_; }
2237 void set_combined_type(Type* type) { combined_type_ = type; }
2239 // Match special cases.
2240 bool IsLiteralCompareTypeof(Expression** expr, Handle<String>* check);
2241 bool IsLiteralCompareUndefined(Expression** expr, Isolate* isolate);
2242 bool IsLiteralCompareNull(Expression** expr);
2245 CompareOperation(Zone* zone, Token::Value op, Expression* left,
2246 Expression* right, int pos)
2247 : Expression(zone, pos),
2251 combined_type_(Type::None(zone)) {
2252 DCHECK(Token::IsCompareOp(op));
2254 static int parent_num_ids() { return Expression::num_ids(); }
2257 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
2263 Type* combined_type_;
2267 class Spread FINAL : public Expression {
2269 DECLARE_NODE_TYPE(Spread)
2271 Expression* expression() const { return expression_; }
2273 static int num_ids() { return parent_num_ids(); }
2276 Spread(Zone* zone, Expression* expression, int pos)
2277 : Expression(zone, pos), expression_(expression) {}
2278 static int parent_num_ids() { return Expression::num_ids(); }
2281 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
2283 Expression* expression_;
2287 class Conditional FINAL : public Expression {
2289 DECLARE_NODE_TYPE(Conditional)
2291 Expression* condition() const { return condition_; }
2292 Expression* then_expression() const { return then_expression_; }
2293 Expression* else_expression() const { return else_expression_; }
2295 static int num_ids() { return parent_num_ids() + 2; }
2296 BailoutId ThenId() const { return BailoutId(local_id(0)); }
2297 BailoutId ElseId() const { return BailoutId(local_id(1)); }
2300 Conditional(Zone* zone, Expression* condition, Expression* then_expression,
2301 Expression* else_expression, int position)
2302 : Expression(zone, position),
2303 condition_(condition),
2304 then_expression_(then_expression),
2305 else_expression_(else_expression) {}
2306 static int parent_num_ids() { return Expression::num_ids(); }
2309 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
2311 Expression* condition_;
2312 Expression* then_expression_;
2313 Expression* else_expression_;
2317 class Assignment FINAL : public Expression {
2319 DECLARE_NODE_TYPE(Assignment)
2321 Assignment* AsSimpleAssignment() { return !is_compound() ? this : NULL; }
2323 Token::Value binary_op() const;
2325 Token::Value op() const { return TokenField::decode(bit_field_); }
2326 Expression* target() const { return target_; }
2327 Expression* value() const { return value_; }
2328 BinaryOperation* binary_operation() const { return binary_operation_; }
2330 // This check relies on the definition order of token in token.h.
2331 bool is_compound() const { return op() > Token::ASSIGN; }
2333 static int num_ids() { return parent_num_ids() + 2; }
2334 BailoutId AssignmentId() const { return BailoutId(local_id(0)); }
2336 // Type feedback information.
2337 TypeFeedbackId AssignmentFeedbackId() { return TypeFeedbackId(local_id(1)); }
2338 bool IsMonomorphic() OVERRIDE { return receiver_types_.length() == 1; }
2339 bool IsUninitialized() const {
2340 return IsUninitializedField::decode(bit_field_);
2342 bool HasNoTypeInformation() {
2343 return IsUninitializedField::decode(bit_field_);
2345 SmallMapList* GetReceiverTypes() OVERRIDE { return &receiver_types_; }
2346 IcCheckType GetKeyType() const OVERRIDE {
2347 return KeyTypeField::decode(bit_field_);
2349 KeyedAccessStoreMode GetStoreMode() const OVERRIDE {
2350 return StoreModeField::decode(bit_field_);
2352 void set_is_uninitialized(bool b) {
2353 bit_field_ = IsUninitializedField::update(bit_field_, b);
2355 void set_key_type(IcCheckType key_type) {
2356 bit_field_ = KeyTypeField::update(bit_field_, key_type);
2358 void set_store_mode(KeyedAccessStoreMode mode) {
2359 bit_field_ = StoreModeField::update(bit_field_, mode);
2363 Assignment(Zone* zone, Token::Value op, Expression* target, Expression* value,
2365 static int parent_num_ids() { return Expression::num_ids(); }
2368 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
2370 class IsUninitializedField : public BitField16<bool, 0, 1> {};
2371 class KeyTypeField : public BitField16<IcCheckType, 1, 1> {};
2372 class StoreModeField : public BitField16<KeyedAccessStoreMode, 2, 4> {};
2373 class TokenField : public BitField16<Token::Value, 6, 8> {};
2375 // Starts with 16-bit field, which should get packed together with
2376 // Expression's trailing 16-bit field.
2377 uint16_t bit_field_;
2378 Expression* target_;
2380 BinaryOperation* binary_operation_;
2381 SmallMapList receiver_types_;
2385 class Yield FINAL : public Expression {
2387 DECLARE_NODE_TYPE(Yield)
2390 kInitial, // The initial yield that returns the unboxed generator object.
2391 kSuspend, // A normal yield: { value: EXPRESSION, done: false }
2392 kDelegating, // A yield*.
2393 kFinal // A return: { value: EXPRESSION, done: true }
2396 Expression* generator_object() const { return generator_object_; }
2397 Expression* expression() const { return expression_; }
2398 Kind yield_kind() const { return yield_kind_; }
2400 // Delegating yield surrounds the "yield" in a "try/catch". This index
2401 // locates the catch handler in the handler table, and is equivalent to
2402 // TryCatchStatement::index().
2404 DCHECK_EQ(kDelegating, yield_kind());
2407 void set_index(int index) {
2408 DCHECK_EQ(kDelegating, yield_kind());
2412 // Type feedback information.
2413 bool HasFeedbackSlots() const {
2414 return FLAG_vector_ics && (yield_kind() == kDelegating);
2416 virtual FeedbackVectorRequirements ComputeFeedbackRequirements(
2417 Isolate* isolate, const ICSlotCache* cache) OVERRIDE {
2418 return FeedbackVectorRequirements(0, HasFeedbackSlots() ? 3 : 0);
2420 void SetFirstFeedbackICSlot(FeedbackVectorICSlot slot,
2421 ICSlotCache* cache) OVERRIDE {
2422 yield_first_feedback_slot_ = slot;
2424 Code::Kind FeedbackICSlotKind(int index) OVERRIDE {
2425 return index == 0 ? Code::KEYED_LOAD_IC : Code::LOAD_IC;
2428 FeedbackVectorICSlot KeyedLoadFeedbackSlot() {
2429 DCHECK(!HasFeedbackSlots() || !yield_first_feedback_slot_.IsInvalid());
2430 return yield_first_feedback_slot_;
2433 FeedbackVectorICSlot DoneFeedbackSlot() {
2434 return KeyedLoadFeedbackSlot().next();
2437 FeedbackVectorICSlot ValueFeedbackSlot() { return DoneFeedbackSlot().next(); }
2440 Yield(Zone* zone, Expression* generator_object, Expression* expression,
2441 Kind yield_kind, int pos)
2442 : Expression(zone, pos),
2443 generator_object_(generator_object),
2444 expression_(expression),
2445 yield_kind_(yield_kind),
2447 yield_first_feedback_slot_(FeedbackVectorICSlot::Invalid()) {}
2450 Expression* generator_object_;
2451 Expression* expression_;
2454 FeedbackVectorICSlot yield_first_feedback_slot_;
2458 class Throw FINAL : public Expression {
2460 DECLARE_NODE_TYPE(Throw)
2462 Expression* exception() const { return exception_; }
2465 Throw(Zone* zone, Expression* exception, int pos)
2466 : Expression(zone, pos), exception_(exception) {}
2469 Expression* exception_;
2473 class FunctionLiteral FINAL : public Expression {
2476 ANONYMOUS_EXPRESSION,
2481 enum ParameterFlag {
2482 kNoDuplicateParameters = 0,
2483 kHasDuplicateParameters = 1
2486 enum IsFunctionFlag {
2491 enum IsParenthesizedFlag {
2496 enum ArityRestriction {
2502 DECLARE_NODE_TYPE(FunctionLiteral)
2504 Handle<String> name() const { return raw_name_->string(); }
2505 const AstRawString* raw_name() const { return raw_name_; }
2506 Scope* scope() const { return scope_; }
2507 ZoneList<Statement*>* body() const { return body_; }
2508 void set_function_token_position(int pos) { function_token_position_ = pos; }
2509 int function_token_position() const { return function_token_position_; }
2510 int start_position() const;
2511 int end_position() const;
2512 int SourceSize() const { return end_position() - start_position(); }
2513 bool is_expression() const { return IsExpression::decode(bitfield_); }
2514 bool is_anonymous() const { return IsAnonymous::decode(bitfield_); }
2515 LanguageMode language_mode() const;
2516 bool uses_super_property() const;
2518 static bool NeedsHomeObject(Expression* literal) {
2519 return literal != NULL && literal->IsFunctionLiteral() &&
2520 literal->AsFunctionLiteral()->uses_super_property();
2523 int materialized_literal_count() { return materialized_literal_count_; }
2524 int expected_property_count() { return expected_property_count_; }
2525 int handler_count() { return handler_count_; }
2526 int parameter_count() { return parameter_count_; }
2528 bool AllowsLazyCompilation();
2529 bool AllowsLazyCompilationWithoutContext();
2531 void InitializeSharedInfo(Handle<Code> code);
2533 Handle<String> debug_name() const {
2534 if (raw_name_ != NULL && !raw_name_->IsEmpty()) {
2535 return raw_name_->string();
2537 return inferred_name();
2540 Handle<String> inferred_name() const {
2541 if (!inferred_name_.is_null()) {
2542 DCHECK(raw_inferred_name_ == NULL);
2543 return inferred_name_;
2545 if (raw_inferred_name_ != NULL) {
2546 return raw_inferred_name_->string();
2549 return Handle<String>();
2552 // Only one of {set_inferred_name, set_raw_inferred_name} should be called.
2553 void set_inferred_name(Handle<String> inferred_name) {
2554 DCHECK(!inferred_name.is_null());
2555 inferred_name_ = inferred_name;
2556 DCHECK(raw_inferred_name_== NULL || raw_inferred_name_->IsEmpty());
2557 raw_inferred_name_ = NULL;
2560 void set_raw_inferred_name(const AstString* raw_inferred_name) {
2561 DCHECK(raw_inferred_name != NULL);
2562 raw_inferred_name_ = raw_inferred_name;
2563 DCHECK(inferred_name_.is_null());
2564 inferred_name_ = Handle<String>();
2567 // shared_info may be null if it's not cached in full code.
2568 Handle<SharedFunctionInfo> shared_info() { return shared_info_; }
2570 bool pretenure() { return Pretenure::decode(bitfield_); }
2571 void set_pretenure() { bitfield_ |= Pretenure::encode(true); }
2573 bool has_duplicate_parameters() {
2574 return HasDuplicateParameters::decode(bitfield_);
2577 bool is_function() { return IsFunction::decode(bitfield_) == kIsFunction; }
2579 // This is used as a heuristic on when to eagerly compile a function
2580 // literal. We consider the following constructs as hints that the
2581 // function will be called immediately:
2582 // - (function() { ... })();
2583 // - var x = function() { ... }();
2584 bool is_parenthesized() {
2585 return IsParenthesized::decode(bitfield_) == kIsParenthesized;
2587 void set_parenthesized() {
2588 bitfield_ = IsParenthesized::update(bitfield_, kIsParenthesized);
2591 FunctionKind kind() { return FunctionKindBits::decode(bitfield_); }
2593 int ast_node_count() { return ast_properties_.node_count(); }
2594 AstProperties::Flags* flags() { return ast_properties_.flags(); }
2595 void set_ast_properties(AstProperties* ast_properties) {
2596 ast_properties_ = *ast_properties;
2598 const ZoneFeedbackVectorSpec* feedback_vector_spec() const {
2599 return ast_properties_.get_spec();
2601 bool dont_optimize() { return dont_optimize_reason_ != kNoReason; }
2602 BailoutReason dont_optimize_reason() { return dont_optimize_reason_; }
2603 void set_dont_optimize_reason(BailoutReason reason) {
2604 dont_optimize_reason_ = reason;
2608 FunctionLiteral(Zone* zone, const AstRawString* name,
2609 AstValueFactory* ast_value_factory, Scope* scope,
2610 ZoneList<Statement*>* body, int materialized_literal_count,
2611 int expected_property_count, int handler_count,
2612 int parameter_count, FunctionType function_type,
2613 ParameterFlag has_duplicate_parameters,
2614 IsFunctionFlag is_function,
2615 IsParenthesizedFlag is_parenthesized, FunctionKind kind,
2617 : Expression(zone, position),
2621 raw_inferred_name_(ast_value_factory->empty_string()),
2622 ast_properties_(zone),
2623 dont_optimize_reason_(kNoReason),
2624 materialized_literal_count_(materialized_literal_count),
2625 expected_property_count_(expected_property_count),
2626 handler_count_(handler_count),
2627 parameter_count_(parameter_count),
2628 function_token_position_(RelocInfo::kNoPosition) {
2629 bitfield_ = IsExpression::encode(function_type != DECLARATION) |
2630 IsAnonymous::encode(function_type == ANONYMOUS_EXPRESSION) |
2631 Pretenure::encode(false) |
2632 HasDuplicateParameters::encode(has_duplicate_parameters) |
2633 IsFunction::encode(is_function) |
2634 IsParenthesized::encode(is_parenthesized) |
2635 FunctionKindBits::encode(kind);
2636 DCHECK(IsValidFunctionKind(kind));
2640 const AstRawString* raw_name_;
2641 Handle<String> name_;
2642 Handle<SharedFunctionInfo> shared_info_;
2644 ZoneList<Statement*>* body_;
2645 const AstString* raw_inferred_name_;
2646 Handle<String> inferred_name_;
2647 AstProperties ast_properties_;
2648 BailoutReason dont_optimize_reason_;
2650 int materialized_literal_count_;
2651 int expected_property_count_;
2653 int parameter_count_;
2654 int function_token_position_;
2657 class IsExpression : public BitField<bool, 0, 1> {};
2658 class IsAnonymous : public BitField<bool, 1, 1> {};
2659 class Pretenure : public BitField<bool, 2, 1> {};
2660 class HasDuplicateParameters : public BitField<ParameterFlag, 3, 1> {};
2661 class IsFunction : public BitField<IsFunctionFlag, 4, 1> {};
2662 class IsParenthesized : public BitField<IsParenthesizedFlag, 5, 1> {};
2663 class FunctionKindBits : public BitField<FunctionKind, 6, 8> {};
2667 class ClassLiteral FINAL : public Expression {
2669 typedef ObjectLiteralProperty Property;
2671 DECLARE_NODE_TYPE(ClassLiteral)
2673 Handle<String> name() const { return raw_name_->string(); }
2674 const AstRawString* raw_name() const { return raw_name_; }
2675 Scope* scope() const { return scope_; }
2676 VariableProxy* class_variable_proxy() const { return class_variable_proxy_; }
2677 Expression* extends() const { return extends_; }
2678 FunctionLiteral* constructor() const { return constructor_; }
2679 ZoneList<Property*>* properties() const { return properties_; }
2680 int start_position() const { return position(); }
2681 int end_position() const { return end_position_; }
2683 BailoutId EntryId() const { return BailoutId(local_id(0)); }
2684 BailoutId DeclsId() const { return BailoutId(local_id(1)); }
2685 BailoutId ExitId() { return BailoutId(local_id(2)); }
2687 // Return an AST id for a property that is used in simulate instructions.
2688 BailoutId GetIdForProperty(int i) { return BailoutId(local_id(i + 3)); }
2690 // Unlike other AST nodes, this number of bailout IDs allocated for an
2691 // ClassLiteral can vary, so num_ids() is not a static method.
2692 int num_ids() const { return parent_num_ids() + 3 + properties()->length(); }
2695 ClassLiteral(Zone* zone, const AstRawString* name, Scope* scope,
2696 VariableProxy* class_variable_proxy, Expression* extends,
2697 FunctionLiteral* constructor, ZoneList<Property*>* properties,
2698 int start_position, int end_position)
2699 : Expression(zone, start_position),
2702 class_variable_proxy_(class_variable_proxy),
2704 constructor_(constructor),
2705 properties_(properties),
2706 end_position_(end_position) {}
2707 static int parent_num_ids() { return Expression::num_ids(); }
2710 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
2712 const AstRawString* raw_name_;
2714 VariableProxy* class_variable_proxy_;
2715 Expression* extends_;
2716 FunctionLiteral* constructor_;
2717 ZoneList<Property*>* properties_;
2722 class NativeFunctionLiteral FINAL : public Expression {
2724 DECLARE_NODE_TYPE(NativeFunctionLiteral)
2726 Handle<String> name() const { return name_->string(); }
2727 v8::Extension* extension() const { return extension_; }
2730 NativeFunctionLiteral(Zone* zone, const AstRawString* name,
2731 v8::Extension* extension, int pos)
2732 : Expression(zone, pos), name_(name), extension_(extension) {}
2735 const AstRawString* name_;
2736 v8::Extension* extension_;
2740 class ThisFunction FINAL : public Expression {
2742 DECLARE_NODE_TYPE(ThisFunction)
2745 ThisFunction(Zone* zone, int pos) : Expression(zone, pos) {}
2749 class SuperReference FINAL : public Expression {
2751 DECLARE_NODE_TYPE(SuperReference)
2753 VariableProxy* this_var() const { return this_var_; }
2755 static int num_ids() { return parent_num_ids() + 1; }
2756 TypeFeedbackId HomeObjectFeedbackId() { return TypeFeedbackId(local_id(0)); }
2758 // Type feedback information.
2759 virtual FeedbackVectorRequirements ComputeFeedbackRequirements(
2760 Isolate* isolate, const ICSlotCache* cache) OVERRIDE {
2761 return FeedbackVectorRequirements(0, FLAG_vector_ics ? 1 : 0);
2763 void SetFirstFeedbackICSlot(FeedbackVectorICSlot slot,
2764 ICSlotCache* cache) OVERRIDE {
2765 homeobject_feedback_slot_ = slot;
2767 Code::Kind FeedbackICSlotKind(int index) OVERRIDE { return Code::LOAD_IC; }
2769 FeedbackVectorICSlot HomeObjectFeedbackSlot() {
2770 DCHECK(!FLAG_vector_ics || !homeobject_feedback_slot_.IsInvalid());
2771 return homeobject_feedback_slot_;
2775 SuperReference(Zone* zone, VariableProxy* this_var, int pos)
2776 : Expression(zone, pos),
2777 this_var_(this_var),
2778 homeobject_feedback_slot_(FeedbackVectorICSlot::Invalid()) {
2779 DCHECK(this_var->is_this());
2781 static int parent_num_ids() { return Expression::num_ids(); }
2784 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
2786 VariableProxy* this_var_;
2787 FeedbackVectorICSlot homeobject_feedback_slot_;
2791 #undef DECLARE_NODE_TYPE
2794 // ----------------------------------------------------------------------------
2795 // Regular expressions
2798 class RegExpVisitor BASE_EMBEDDED {
2800 virtual ~RegExpVisitor() { }
2801 #define MAKE_CASE(Name) \
2802 virtual void* Visit##Name(RegExp##Name*, void* data) = 0;
2803 FOR_EACH_REG_EXP_TREE_TYPE(MAKE_CASE)
2808 class RegExpTree : public ZoneObject {
2810 static const int kInfinity = kMaxInt;
2811 virtual ~RegExpTree() {}
2812 virtual void* Accept(RegExpVisitor* visitor, void* data) = 0;
2813 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
2814 RegExpNode* on_success) = 0;
2815 virtual bool IsTextElement() { return false; }
2816 virtual bool IsAnchoredAtStart() { return false; }
2817 virtual bool IsAnchoredAtEnd() { return false; }
2818 virtual int min_match() = 0;
2819 virtual int max_match() = 0;
2820 // Returns the interval of registers used for captures within this
2822 virtual Interval CaptureRegisters() { return Interval::Empty(); }
2823 virtual void AppendToText(RegExpText* text, Zone* zone);
2824 std::ostream& Print(std::ostream& os, Zone* zone); // NOLINT
2825 #define MAKE_ASTYPE(Name) \
2826 virtual RegExp##Name* As##Name(); \
2827 virtual bool Is##Name();
2828 FOR_EACH_REG_EXP_TREE_TYPE(MAKE_ASTYPE)
2833 class RegExpDisjunction FINAL : public RegExpTree {
2835 explicit RegExpDisjunction(ZoneList<RegExpTree*>* alternatives);
2836 void* Accept(RegExpVisitor* visitor, void* data) OVERRIDE;
2837 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
2838 RegExpNode* on_success) OVERRIDE;
2839 RegExpDisjunction* AsDisjunction() OVERRIDE;
2840 Interval CaptureRegisters() OVERRIDE;
2841 bool IsDisjunction() OVERRIDE;
2842 bool IsAnchoredAtStart() OVERRIDE;
2843 bool IsAnchoredAtEnd() OVERRIDE;
2844 int min_match() OVERRIDE { return min_match_; }
2845 int max_match() OVERRIDE { return max_match_; }
2846 ZoneList<RegExpTree*>* alternatives() { return alternatives_; }
2848 ZoneList<RegExpTree*>* alternatives_;
2854 class RegExpAlternative FINAL : public RegExpTree {
2856 explicit RegExpAlternative(ZoneList<RegExpTree*>* nodes);
2857 void* Accept(RegExpVisitor* visitor, void* data) OVERRIDE;
2858 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
2859 RegExpNode* on_success) OVERRIDE;
2860 RegExpAlternative* AsAlternative() OVERRIDE;
2861 Interval CaptureRegisters() OVERRIDE;
2862 bool IsAlternative() OVERRIDE;
2863 bool IsAnchoredAtStart() OVERRIDE;
2864 bool IsAnchoredAtEnd() OVERRIDE;
2865 int min_match() OVERRIDE { return min_match_; }
2866 int max_match() OVERRIDE { return max_match_; }
2867 ZoneList<RegExpTree*>* nodes() { return nodes_; }
2869 ZoneList<RegExpTree*>* nodes_;
2875 class RegExpAssertion FINAL : public RegExpTree {
2877 enum AssertionType {
2885 explicit RegExpAssertion(AssertionType type) : assertion_type_(type) { }
2886 void* Accept(RegExpVisitor* visitor, void* data) OVERRIDE;
2887 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
2888 RegExpNode* on_success) OVERRIDE;
2889 RegExpAssertion* AsAssertion() OVERRIDE;
2890 bool IsAssertion() OVERRIDE;
2891 bool IsAnchoredAtStart() OVERRIDE;
2892 bool IsAnchoredAtEnd() OVERRIDE;
2893 int min_match() OVERRIDE { return 0; }
2894 int max_match() OVERRIDE { return 0; }
2895 AssertionType assertion_type() { return assertion_type_; }
2897 AssertionType assertion_type_;
2901 class CharacterSet FINAL BASE_EMBEDDED {
2903 explicit CharacterSet(uc16 standard_set_type)
2905 standard_set_type_(standard_set_type) {}
2906 explicit CharacterSet(ZoneList<CharacterRange>* ranges)
2908 standard_set_type_(0) {}
2909 ZoneList<CharacterRange>* ranges(Zone* zone);
2910 uc16 standard_set_type() { return standard_set_type_; }
2911 void set_standard_set_type(uc16 special_set_type) {
2912 standard_set_type_ = special_set_type;
2914 bool is_standard() { return standard_set_type_ != 0; }
2915 void Canonicalize();
2917 ZoneList<CharacterRange>* ranges_;
2918 // If non-zero, the value represents a standard set (e.g., all whitespace
2919 // characters) without having to expand the ranges.
2920 uc16 standard_set_type_;
2924 class RegExpCharacterClass FINAL : public RegExpTree {
2926 RegExpCharacterClass(ZoneList<CharacterRange>* ranges, bool is_negated)
2928 is_negated_(is_negated) { }
2929 explicit RegExpCharacterClass(uc16 type)
2931 is_negated_(false) { }
2932 void* Accept(RegExpVisitor* visitor, void* data) OVERRIDE;
2933 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
2934 RegExpNode* on_success) OVERRIDE;
2935 RegExpCharacterClass* AsCharacterClass() OVERRIDE;
2936 bool IsCharacterClass() OVERRIDE;
2937 bool IsTextElement() OVERRIDE { return true; }
2938 int min_match() OVERRIDE { return 1; }
2939 int max_match() OVERRIDE { return 1; }
2940 void AppendToText(RegExpText* text, Zone* zone) OVERRIDE;
2941 CharacterSet character_set() { return set_; }
2942 // TODO(lrn): Remove need for complex version if is_standard that
2943 // recognizes a mangled standard set and just do { return set_.is_special(); }
2944 bool is_standard(Zone* zone);
2945 // Returns a value representing the standard character set if is_standard()
2947 // Currently used values are:
2948 // s : unicode whitespace
2949 // S : unicode non-whitespace
2950 // w : ASCII word character (digit, letter, underscore)
2951 // W : non-ASCII word character
2953 // D : non-ASCII digit
2954 // . : non-unicode non-newline
2955 // * : All characters
2956 uc16 standard_type() { return set_.standard_set_type(); }
2957 ZoneList<CharacterRange>* ranges(Zone* zone) { return set_.ranges(zone); }
2958 bool is_negated() { return is_negated_; }
2966 class RegExpAtom FINAL : public RegExpTree {
2968 explicit RegExpAtom(Vector<const uc16> data) : data_(data) { }
2969 void* Accept(RegExpVisitor* visitor, void* data) OVERRIDE;
2970 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
2971 RegExpNode* on_success) OVERRIDE;
2972 RegExpAtom* AsAtom() OVERRIDE;
2973 bool IsAtom() OVERRIDE;
2974 bool IsTextElement() OVERRIDE { return true; }
2975 int min_match() OVERRIDE { return data_.length(); }
2976 int max_match() OVERRIDE { return data_.length(); }
2977 void AppendToText(RegExpText* text, Zone* zone) OVERRIDE;
2978 Vector<const uc16> data() { return data_; }
2979 int length() { return data_.length(); }
2981 Vector<const uc16> data_;
2985 class RegExpText FINAL : public RegExpTree {
2987 explicit RegExpText(Zone* zone) : elements_(2, zone), length_(0) {}
2988 void* Accept(RegExpVisitor* visitor, void* data) OVERRIDE;
2989 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
2990 RegExpNode* on_success) OVERRIDE;
2991 RegExpText* AsText() OVERRIDE;
2992 bool IsText() OVERRIDE;
2993 bool IsTextElement() OVERRIDE { return true; }
2994 int min_match() OVERRIDE { return length_; }
2995 int max_match() OVERRIDE { return length_; }
2996 void AppendToText(RegExpText* text, Zone* zone) OVERRIDE;
2997 void AddElement(TextElement elm, Zone* zone) {
2998 elements_.Add(elm, zone);
2999 length_ += elm.length();
3001 ZoneList<TextElement>* elements() { return &elements_; }
3003 ZoneList<TextElement> elements_;
3008 class RegExpQuantifier FINAL : public RegExpTree {
3010 enum QuantifierType { GREEDY, NON_GREEDY, POSSESSIVE };
3011 RegExpQuantifier(int min, int max, QuantifierType type, RegExpTree* body)
3015 min_match_(min * body->min_match()),
3016 quantifier_type_(type) {
3017 if (max > 0 && body->max_match() > kInfinity / max) {
3018 max_match_ = kInfinity;
3020 max_match_ = max * body->max_match();
3023 void* Accept(RegExpVisitor* visitor, void* data) OVERRIDE;
3024 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
3025 RegExpNode* on_success) OVERRIDE;
3026 static RegExpNode* ToNode(int min,
3030 RegExpCompiler* compiler,
3031 RegExpNode* on_success,
3032 bool not_at_start = false);
3033 RegExpQuantifier* AsQuantifier() OVERRIDE;
3034 Interval CaptureRegisters() OVERRIDE;
3035 bool IsQuantifier() OVERRIDE;
3036 int min_match() OVERRIDE { return min_match_; }
3037 int max_match() OVERRIDE { return max_match_; }
3038 int min() { return min_; }
3039 int max() { return max_; }
3040 bool is_possessive() { return quantifier_type_ == POSSESSIVE; }
3041 bool is_non_greedy() { return quantifier_type_ == NON_GREEDY; }
3042 bool is_greedy() { return quantifier_type_ == GREEDY; }
3043 RegExpTree* body() { return body_; }
3051 QuantifierType quantifier_type_;
3055 class RegExpCapture FINAL : public RegExpTree {
3057 explicit RegExpCapture(RegExpTree* body, int index)
3058 : body_(body), index_(index) { }
3059 void* Accept(RegExpVisitor* visitor, void* data) OVERRIDE;
3060 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
3061 RegExpNode* on_success) OVERRIDE;
3062 static RegExpNode* ToNode(RegExpTree* body,
3064 RegExpCompiler* compiler,
3065 RegExpNode* on_success);
3066 RegExpCapture* AsCapture() OVERRIDE;
3067 bool IsAnchoredAtStart() OVERRIDE;
3068 bool IsAnchoredAtEnd() OVERRIDE;
3069 Interval CaptureRegisters() OVERRIDE;
3070 bool IsCapture() OVERRIDE;
3071 int min_match() OVERRIDE { return body_->min_match(); }
3072 int max_match() OVERRIDE { return body_->max_match(); }
3073 RegExpTree* body() { return body_; }
3074 int index() { return index_; }
3075 static int StartRegister(int index) { return index * 2; }
3076 static int EndRegister(int index) { return index * 2 + 1; }
3084 class RegExpLookahead FINAL : public RegExpTree {
3086 RegExpLookahead(RegExpTree* body,
3091 is_positive_(is_positive),
3092 capture_count_(capture_count),
3093 capture_from_(capture_from) { }
3095 void* Accept(RegExpVisitor* visitor, void* data) OVERRIDE;
3096 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
3097 RegExpNode* on_success) OVERRIDE;
3098 RegExpLookahead* AsLookahead() OVERRIDE;
3099 Interval CaptureRegisters() OVERRIDE;
3100 bool IsLookahead() OVERRIDE;
3101 bool IsAnchoredAtStart() OVERRIDE;
3102 int min_match() OVERRIDE { return 0; }
3103 int max_match() OVERRIDE { return 0; }
3104 RegExpTree* body() { return body_; }
3105 bool is_positive() { return is_positive_; }
3106 int capture_count() { return capture_count_; }
3107 int capture_from() { return capture_from_; }
3117 class RegExpBackReference FINAL : public RegExpTree {
3119 explicit RegExpBackReference(RegExpCapture* capture)
3120 : capture_(capture) { }
3121 void* Accept(RegExpVisitor* visitor, void* data) OVERRIDE;
3122 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
3123 RegExpNode* on_success) OVERRIDE;
3124 RegExpBackReference* AsBackReference() OVERRIDE;
3125 bool IsBackReference() OVERRIDE;
3126 int min_match() OVERRIDE { return 0; }
3127 int max_match() OVERRIDE { return capture_->max_match(); }
3128 int index() { return capture_->index(); }
3129 RegExpCapture* capture() { return capture_; }
3131 RegExpCapture* capture_;
3135 class RegExpEmpty FINAL : public RegExpTree {
3138 void* Accept(RegExpVisitor* visitor, void* data) OVERRIDE;
3139 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
3140 RegExpNode* on_success) OVERRIDE;
3141 RegExpEmpty* AsEmpty() OVERRIDE;
3142 bool IsEmpty() OVERRIDE;
3143 int min_match() OVERRIDE { return 0; }
3144 int max_match() OVERRIDE { return 0; }
3148 // ----------------------------------------------------------------------------
3150 // - leaf node visitors are abstract.
3152 class AstVisitor BASE_EMBEDDED {
3155 virtual ~AstVisitor() {}
3157 // Stack overflow check and dynamic dispatch.
3158 virtual void Visit(AstNode* node) = 0;
3160 // Iteration left-to-right.
3161 virtual void VisitDeclarations(ZoneList<Declaration*>* declarations);
3162 virtual void VisitStatements(ZoneList<Statement*>* statements);
3163 virtual void VisitExpressions(ZoneList<Expression*>* expressions);
3165 // Individual AST nodes.
3166 #define DEF_VISIT(type) \
3167 virtual void Visit##type(type* node) = 0;
3168 AST_NODE_LIST(DEF_VISIT)
3173 #define DEFINE_AST_VISITOR_SUBCLASS_MEMBERS() \
3175 void Visit(AstNode* node) FINAL { \
3176 if (!CheckStackOverflow()) node->Accept(this); \
3179 void SetStackOverflow() { stack_overflow_ = true; } \
3180 void ClearStackOverflow() { stack_overflow_ = false; } \
3181 bool HasStackOverflow() const { return stack_overflow_; } \
3183 bool CheckStackOverflow() { \
3184 if (stack_overflow_) return true; \
3185 StackLimitCheck check(isolate_); \
3186 if (!check.HasOverflowed()) return false; \
3187 stack_overflow_ = true; \
3192 void InitializeAstVisitor(Isolate* isolate, Zone* zone) { \
3193 isolate_ = isolate; \
3195 stack_overflow_ = false; \
3197 Zone* zone() { return zone_; } \
3198 Isolate* isolate() { return isolate_; } \
3200 Isolate* isolate_; \
3202 bool stack_overflow_
3205 // ----------------------------------------------------------------------------
3208 class AstNodeFactory FINAL BASE_EMBEDDED {
3210 explicit AstNodeFactory(AstValueFactory* ast_value_factory)
3211 : zone_(ast_value_factory->zone()),
3212 ast_value_factory_(ast_value_factory) {}
3214 VariableDeclaration* NewVariableDeclaration(VariableProxy* proxy,
3218 return new (zone_) VariableDeclaration(zone_, proxy, mode, scope, pos);
3221 FunctionDeclaration* NewFunctionDeclaration(VariableProxy* proxy,
3223 FunctionLiteral* fun,
3226 return new (zone_) FunctionDeclaration(zone_, proxy, mode, fun, scope, pos);
3229 ModuleDeclaration* NewModuleDeclaration(VariableProxy* proxy,
3233 return new (zone_) ModuleDeclaration(zone_, proxy, module, scope, pos);
3236 ImportDeclaration* NewImportDeclaration(VariableProxy* proxy,
3237 const AstRawString* import_name,
3238 const AstRawString* module_specifier,
3239 Scope* scope, int pos) {
3240 return new (zone_) ImportDeclaration(zone_, proxy, import_name,
3241 module_specifier, scope, pos);
3244 ExportDeclaration* NewExportDeclaration(VariableProxy* proxy,
3247 return new (zone_) ExportDeclaration(zone_, proxy, scope, pos);
3250 ModuleLiteral* NewModuleLiteral(Block* body, ModuleDescriptor* descriptor,
3252 return new (zone_) ModuleLiteral(zone_, body, descriptor, pos);
3255 ModulePath* NewModulePath(Module* origin, const AstRawString* name, int pos) {
3256 return new (zone_) ModulePath(zone_, origin, name, pos);
3259 ModuleUrl* NewModuleUrl(Handle<String> url, int pos) {
3260 return new (zone_) ModuleUrl(zone_, url, pos);
3263 Block* NewBlock(ZoneList<const AstRawString*>* labels,
3265 bool is_initializer_block,
3268 Block(zone_, labels, capacity, is_initializer_block, pos);
3271 #define STATEMENT_WITH_LABELS(NodeType) \
3272 NodeType* New##NodeType(ZoneList<const AstRawString*>* labels, int pos) { \
3273 return new (zone_) NodeType(zone_, labels, pos); \
3275 STATEMENT_WITH_LABELS(DoWhileStatement)
3276 STATEMENT_WITH_LABELS(WhileStatement)
3277 STATEMENT_WITH_LABELS(ForStatement)
3278 STATEMENT_WITH_LABELS(SwitchStatement)
3279 #undef STATEMENT_WITH_LABELS
3281 ForEachStatement* NewForEachStatement(ForEachStatement::VisitMode visit_mode,
3282 ZoneList<const AstRawString*>* labels,
3284 switch (visit_mode) {
3285 case ForEachStatement::ENUMERATE: {
3286 return new (zone_) ForInStatement(zone_, labels, pos);
3288 case ForEachStatement::ITERATE: {
3289 return new (zone_) ForOfStatement(zone_, labels, pos);
3296 ModuleStatement* NewModuleStatement(Block* body, int pos) {
3297 return new (zone_) ModuleStatement(zone_, body, pos);
3300 ExpressionStatement* NewExpressionStatement(Expression* expression, int pos) {
3301 return new (zone_) ExpressionStatement(zone_, expression, pos);
3304 ContinueStatement* NewContinueStatement(IterationStatement* target, int pos) {
3305 return new (zone_) ContinueStatement(zone_, target, pos);
3308 BreakStatement* NewBreakStatement(BreakableStatement* target, int pos) {
3309 return new (zone_) BreakStatement(zone_, target, pos);
3312 ReturnStatement* NewReturnStatement(Expression* expression, int pos) {
3313 return new (zone_) ReturnStatement(zone_, expression, pos);
3316 WithStatement* NewWithStatement(Scope* scope,
3317 Expression* expression,
3318 Statement* statement,
3320 return new (zone_) WithStatement(zone_, scope, expression, statement, pos);
3323 IfStatement* NewIfStatement(Expression* condition,
3324 Statement* then_statement,
3325 Statement* else_statement,
3328 IfStatement(zone_, condition, then_statement, else_statement, pos);
3331 TryCatchStatement* NewTryCatchStatement(int index,
3337 return new (zone_) TryCatchStatement(zone_, index, try_block, scope,
3338 variable, catch_block, pos);
3341 TryFinallyStatement* NewTryFinallyStatement(int index,
3343 Block* finally_block,
3346 TryFinallyStatement(zone_, index, try_block, finally_block, pos);
3349 DebuggerStatement* NewDebuggerStatement(int pos) {
3350 return new (zone_) DebuggerStatement(zone_, pos);
3353 EmptyStatement* NewEmptyStatement(int pos) {
3354 return new(zone_) EmptyStatement(zone_, pos);
3357 CaseClause* NewCaseClause(
3358 Expression* label, ZoneList<Statement*>* statements, int pos) {
3359 return new (zone_) CaseClause(zone_, label, statements, pos);
3362 Literal* NewStringLiteral(const AstRawString* string, int pos) {
3364 Literal(zone_, ast_value_factory_->NewString(string), pos);
3367 // A JavaScript symbol (ECMA-262 edition 6).
3368 Literal* NewSymbolLiteral(const char* name, int pos) {
3369 return new (zone_) Literal(zone_, ast_value_factory_->NewSymbol(name), pos);
3372 Literal* NewNumberLiteral(double number, int pos) {
3374 Literal(zone_, ast_value_factory_->NewNumber(number), pos);
3377 Literal* NewSmiLiteral(int number, int pos) {
3378 return new (zone_) Literal(zone_, ast_value_factory_->NewSmi(number), pos);
3381 Literal* NewBooleanLiteral(bool b, int pos) {
3382 return new (zone_) Literal(zone_, ast_value_factory_->NewBoolean(b), pos);
3385 Literal* NewNullLiteral(int pos) {
3386 return new (zone_) Literal(zone_, ast_value_factory_->NewNull(), pos);
3389 Literal* NewUndefinedLiteral(int pos) {
3390 return new (zone_) Literal(zone_, ast_value_factory_->NewUndefined(), pos);
3393 Literal* NewTheHoleLiteral(int pos) {
3394 return new (zone_) Literal(zone_, ast_value_factory_->NewTheHole(), pos);
3397 ObjectLiteral* NewObjectLiteral(
3398 ZoneList<ObjectLiteral::Property*>* properties,
3400 int boilerplate_properties,
3403 return new (zone_) ObjectLiteral(zone_, properties, literal_index,
3404 boilerplate_properties, has_function, pos);
3407 ObjectLiteral::Property* NewObjectLiteralProperty(
3408 Expression* key, Expression* value, ObjectLiteralProperty::Kind kind,
3409 bool is_static, bool is_computed_name) {
3411 ObjectLiteral::Property(key, value, kind, is_static, is_computed_name);
3414 ObjectLiteral::Property* NewObjectLiteralProperty(Expression* key,
3417 bool is_computed_name) {
3418 return new (zone_) ObjectLiteral::Property(ast_value_factory_, key, value,
3419 is_static, is_computed_name);
3422 RegExpLiteral* NewRegExpLiteral(const AstRawString* pattern,
3423 const AstRawString* flags,
3426 return new (zone_) RegExpLiteral(zone_, pattern, flags, literal_index, pos);
3429 ArrayLiteral* NewArrayLiteral(ZoneList<Expression*>* values,
3432 return new (zone_) ArrayLiteral(zone_, values, literal_index, pos);
3435 VariableProxy* NewVariableProxy(Variable* var,
3436 int start_position = RelocInfo::kNoPosition,
3437 int end_position = RelocInfo::kNoPosition) {
3438 return new (zone_) VariableProxy(zone_, var, start_position, end_position);
3441 VariableProxy* NewVariableProxy(const AstRawString* name,
3442 Variable::Kind variable_kind,
3443 int start_position = RelocInfo::kNoPosition,
3444 int end_position = RelocInfo::kNoPosition) {
3446 VariableProxy(zone_, name, variable_kind, start_position, end_position);
3449 Property* NewProperty(Expression* obj, Expression* key, int pos) {
3450 return new (zone_) Property(zone_, obj, key, pos);
3453 Call* NewCall(Expression* expression,
3454 ZoneList<Expression*>* arguments,
3456 return new (zone_) Call(zone_, expression, arguments, pos);
3459 CallNew* NewCallNew(Expression* expression,
3460 ZoneList<Expression*>* arguments,
3462 return new (zone_) CallNew(zone_, expression, arguments, pos);
3465 CallRuntime* NewCallRuntime(const AstRawString* name,
3466 const Runtime::Function* function,
3467 ZoneList<Expression*>* arguments,
3469 return new (zone_) CallRuntime(zone_, name, function, arguments, pos);
3472 UnaryOperation* NewUnaryOperation(Token::Value op,
3473 Expression* expression,
3475 return new (zone_) UnaryOperation(zone_, op, expression, pos);
3478 BinaryOperation* NewBinaryOperation(Token::Value op,
3482 return new (zone_) BinaryOperation(zone_, op, left, right, pos);
3485 CountOperation* NewCountOperation(Token::Value op,
3489 return new (zone_) CountOperation(zone_, op, is_prefix, expr, pos);
3492 CompareOperation* NewCompareOperation(Token::Value op,
3496 return new (zone_) CompareOperation(zone_, op, left, right, pos);
3499 Spread* NewSpread(Expression* expression, int pos) {
3500 return new (zone_) Spread(zone_, expression, pos);
3503 Conditional* NewConditional(Expression* condition,
3504 Expression* then_expression,
3505 Expression* else_expression,
3507 return new (zone_) Conditional(zone_, condition, then_expression,
3508 else_expression, position);
3511 Assignment* NewAssignment(Token::Value op,
3515 DCHECK(Token::IsAssignmentOp(op));
3516 Assignment* assign = new (zone_) Assignment(zone_, op, target, value, pos);
3517 if (assign->is_compound()) {
3518 DCHECK(Token::IsAssignmentOp(op));
3519 assign->binary_operation_ =
3520 NewBinaryOperation(assign->binary_op(), target, value, pos + 1);
3525 Yield* NewYield(Expression *generator_object,
3526 Expression* expression,
3527 Yield::Kind yield_kind,
3529 if (!expression) expression = NewUndefinedLiteral(pos);
3531 Yield(zone_, generator_object, expression, yield_kind, pos);
3534 Throw* NewThrow(Expression* exception, int pos) {
3535 return new (zone_) Throw(zone_, exception, pos);
3538 FunctionLiteral* NewFunctionLiteral(
3539 const AstRawString* name, AstValueFactory* ast_value_factory,
3540 Scope* scope, ZoneList<Statement*>* body, int materialized_literal_count,
3541 int expected_property_count, int handler_count, int parameter_count,
3542 FunctionLiteral::ParameterFlag has_duplicate_parameters,
3543 FunctionLiteral::FunctionType function_type,
3544 FunctionLiteral::IsFunctionFlag is_function,
3545 FunctionLiteral::IsParenthesizedFlag is_parenthesized, FunctionKind kind,
3547 return new (zone_) FunctionLiteral(
3548 zone_, name, ast_value_factory, scope, body, materialized_literal_count,
3549 expected_property_count, handler_count, parameter_count, function_type,
3550 has_duplicate_parameters, is_function, is_parenthesized, kind,
3554 ClassLiteral* NewClassLiteral(const AstRawString* name, Scope* scope,
3555 VariableProxy* proxy, Expression* extends,
3556 FunctionLiteral* constructor,
3557 ZoneList<ObjectLiteral::Property*>* properties,
3558 int start_position, int end_position) {
3560 ClassLiteral(zone_, name, scope, proxy, extends, constructor,
3561 properties, start_position, end_position);
3564 NativeFunctionLiteral* NewNativeFunctionLiteral(const AstRawString* name,
3565 v8::Extension* extension,
3567 return new (zone_) NativeFunctionLiteral(zone_, name, extension, pos);
3570 ThisFunction* NewThisFunction(int pos) {
3571 return new (zone_) ThisFunction(zone_, pos);
3574 SuperReference* NewSuperReference(VariableProxy* this_var, int pos) {
3575 return new (zone_) SuperReference(zone_, this_var, pos);
3580 AstValueFactory* ast_value_factory_;
3584 } } // namespace v8::internal